aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/link/internal/ld/errors.go
blob: c2c191d05845eb3dc41e112543ce5110893c13dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ld

import (
	"cmd/internal/obj"
	"cmd/link/internal/loader"
	"cmd/link/internal/sym"
	"sync"
)

type unresolvedSymKey struct {
	from loader.Sym // Symbol that referenced unresolved "to"
	to   loader.Sym // Unresolved symbol referenced by "from"
}

type unresolvedSymKey2 struct {
	from *sym.Symbol // Symbol that referenced unresolved "to"
	to   *sym.Symbol // Unresolved symbol referenced by "from"
}

type lookupFn func(name string, version int) *sym.Symbol
type symNameFn func(s loader.Sym) string

// ErrorReporter is used to make error reporting thread safe.
type ErrorReporter struct {
	loader.ErrorReporter
	unresOnce  sync.Once
	unresSyms  map[unresolvedSymKey]bool
	unresSyms2 map[unresolvedSymKey2]bool
	unresMutex sync.Mutex
	lookup     lookupFn
	SymName    symNameFn
}

// errorUnresolved prints unresolved symbol error for rs that is referenced from s.
func (reporter *ErrorReporter) errorUnresolved(ldr *loader.Loader, s, rs loader.Sym) {
	reporter.unresOnce.Do(func() { reporter.unresSyms = make(map[unresolvedSymKey]bool) })

	k := unresolvedSymKey{from: s, to: rs}
	reporter.unresMutex.Lock()
	defer reporter.unresMutex.Unlock()
	if !reporter.unresSyms[k] {
		reporter.unresSyms[k] = true
		name := ldr.SymName(rs)

		// Try to find symbol under another ABI.
		var reqABI, haveABI obj.ABI
		haveABI = ^obj.ABI(0)
		reqABI, ok := sym.VersionToABI(ldr.SymVersion(rs))
		if ok {
			for abi := obj.ABI(0); abi < obj.ABICount; abi++ {
				v := sym.ABIToVersion(abi)
				if v == -1 {
					continue
				}
				if rs1 := ldr.Lookup(name, v); rs1 != 0 && ldr.SymType(rs1) != sym.Sxxx && ldr.SymType(rs1) != sym.SXREF {
					haveABI = abi
				}
			}
		}

		// Give a special error message for main symbol (see #24809).
		if name == "main.main" {
			reporter.Errorf(s, "function main is undeclared in the main package")
		} else if haveABI != ^obj.ABI(0) {
			reporter.Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", name, reqABI, haveABI)
		} else {
			reporter.Errorf(s, "relocation target %s not defined", name)
		}
	}
}

// errorUnresolved2 prints unresolved symbol error for r.Sym that is referenced from s.
func (reporter *ErrorReporter) errorUnresolved2(s *sym.Symbol, r *sym.Reloc) {
	reporter.unresOnce.Do(func() { reporter.unresSyms2 = make(map[unresolvedSymKey2]bool) })

	k := unresolvedSymKey2{from: s, to: r.Sym}
	reporter.unresMutex.Lock()
	defer reporter.unresMutex.Unlock()
	if !reporter.unresSyms2[k] {
		reporter.unresSyms2[k] = true

		// Try to find symbol under another ABI.
		var reqABI, haveABI obj.ABI
		haveABI = ^obj.ABI(0)
		reqABI, ok := sym.VersionToABI(int(r.Sym.Version))
		if ok {
			for abi := obj.ABI(0); abi < obj.ABICount; abi++ {
				v := sym.ABIToVersion(abi)
				if v == -1 {
					continue
				}
				if rs := reporter.lookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx && rs.Type != sym.SXREF {
					haveABI = abi
				}
			}
		}

		// Give a special error message for main symbol (see #24809).
		if r.Sym.Name == "main.main" {
			Errorf(s, "function main is undeclared in the main package")
		} else if haveABI != ^obj.ABI(0) {
			Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", r.Sym.Name, reqABI, haveABI)
		} else {
			Errorf(s, "relocation target %s not defined", r.Sym.Name)
		}
	}
}