Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/link/internal/ld/errors.go

Documentation: cmd/link/internal/ld

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"cmd/internal/obj"
     9  	"cmd/link/internal/loader"
    10  	"cmd/link/internal/sym"
    11  	"sync"
    12  )
    13  
    14  type unresolvedSymKey struct {
    15  	from loader.Sym // Symbol that referenced unresolved "to"
    16  	to   loader.Sym // Unresolved symbol referenced by "from"
    17  }
    18  
    19  type symNameFn func(s loader.Sym) string
    20  
    21  // ErrorReporter is used to make error reporting thread safe.
    22  type ErrorReporter struct {
    23  	loader.ErrorReporter
    24  	unresOnce  sync.Once
    25  	unresSyms  map[unresolvedSymKey]bool
    26  	unresMutex sync.Mutex
    27  	SymName    symNameFn
    28  }
    29  
    30  // errorUnresolved prints unresolved symbol error for rs that is referenced from s.
    31  func (reporter *ErrorReporter) errorUnresolved(ldr *loader.Loader, s, rs loader.Sym) {
    32  	reporter.unresOnce.Do(func() { reporter.unresSyms = make(map[unresolvedSymKey]bool) })
    33  
    34  	k := unresolvedSymKey{from: s, to: rs}
    35  	reporter.unresMutex.Lock()
    36  	defer reporter.unresMutex.Unlock()
    37  	if !reporter.unresSyms[k] {
    38  		reporter.unresSyms[k] = true
    39  		name := ldr.SymName(rs)
    40  
    41  		// Try to find symbol under another ABI.
    42  		var reqABI, haveABI obj.ABI
    43  		haveABI = ^obj.ABI(0)
    44  		reqABI, ok := sym.VersionToABI(ldr.SymVersion(rs))
    45  		if ok {
    46  			for abi := obj.ABI(0); abi < obj.ABICount; abi++ {
    47  				v := sym.ABIToVersion(abi)
    48  				if v == -1 {
    49  					continue
    50  				}
    51  				if rs1 := ldr.Lookup(name, v); rs1 != 0 && ldr.SymType(rs1) != sym.Sxxx && ldr.SymType(rs1) != sym.SXREF {
    52  					haveABI = abi
    53  				}
    54  			}
    55  		}
    56  
    57  		// Give a special error message for main symbol (see #24809).
    58  		if name == "main.main" {
    59  			reporter.Errorf(s, "function main is undeclared in the main package")
    60  		} else if haveABI != ^obj.ABI(0) {
    61  			reporter.Errorf(s, "relocation target %s not defined for %s (but is defined for %s)", name, reqABI, haveABI)
    62  		} else {
    63  			reporter.Errorf(s, "relocation target %s not defined", name)
    64  		}
    65  	}
    66  }
    67  

View as plain text