github.com/bir3/gocompiler@v0.3.205/src/cmd/link/internal/loadpe/ldpe.go (about)

     1  // Copyright 2010 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 loadpe implements a PE/COFF file reader.
     6  package loadpe
     7  
     8  import (
     9  	"bytes"
    10  	"github.com/bir3/gocompiler/src/cmd/internal/bio"
    11  	"github.com/bir3/gocompiler/src/cmd/internal/objabi"
    12  	"github.com/bir3/gocompiler/src/cmd/internal/sys"
    13  	"github.com/bir3/gocompiler/src/cmd/link/internal/loader"
    14  	"github.com/bir3/gocompiler/src/cmd/link/internal/sym"
    15  	"debug/pe"
    16  	"encoding/binary"
    17  	"errors"
    18  	"fmt"
    19  	"io"
    20  	"strings"
    21  )
    22  
    23  const (
    24  	// TODO: the Microsoft doco says IMAGE_SYM_DTYPE_ARRAY is 3 (same with IMAGE_SYM_DTYPE_POINTER and IMAGE_SYM_DTYPE_FUNCTION)
    25  	IMAGE_SYM_UNDEFINED              = 0
    26  	IMAGE_SYM_ABSOLUTE               = -1
    27  	IMAGE_SYM_DEBUG                  = -2
    28  	IMAGE_SYM_TYPE_NULL              = 0
    29  	IMAGE_SYM_TYPE_VOID              = 1
    30  	IMAGE_SYM_TYPE_CHAR              = 2
    31  	IMAGE_SYM_TYPE_SHORT             = 3
    32  	IMAGE_SYM_TYPE_INT               = 4
    33  	IMAGE_SYM_TYPE_LONG              = 5
    34  	IMAGE_SYM_TYPE_FLOAT             = 6
    35  	IMAGE_SYM_TYPE_DOUBLE            = 7
    36  	IMAGE_SYM_TYPE_STRUCT            = 8
    37  	IMAGE_SYM_TYPE_UNION             = 9
    38  	IMAGE_SYM_TYPE_ENUM              = 10
    39  	IMAGE_SYM_TYPE_MOE               = 11
    40  	IMAGE_SYM_TYPE_BYTE              = 12
    41  	IMAGE_SYM_TYPE_WORD              = 13
    42  	IMAGE_SYM_TYPE_UINT              = 14
    43  	IMAGE_SYM_TYPE_DWORD             = 15
    44  	IMAGE_SYM_TYPE_PCODE             = 32768
    45  	IMAGE_SYM_DTYPE_NULL             = 0
    46  	IMAGE_SYM_DTYPE_POINTER          = 0x10
    47  	IMAGE_SYM_DTYPE_FUNCTION         = 0x20
    48  	IMAGE_SYM_DTYPE_ARRAY            = 0x30
    49  	IMAGE_SYM_CLASS_END_OF_FUNCTION  = -1
    50  	IMAGE_SYM_CLASS_NULL             = 0
    51  	IMAGE_SYM_CLASS_AUTOMATIC        = 1
    52  	IMAGE_SYM_CLASS_EXTERNAL         = 2
    53  	IMAGE_SYM_CLASS_STATIC           = 3
    54  	IMAGE_SYM_CLASS_REGISTER         = 4
    55  	IMAGE_SYM_CLASS_EXTERNAL_DEF     = 5
    56  	IMAGE_SYM_CLASS_LABEL            = 6
    57  	IMAGE_SYM_CLASS_UNDEFINED_LABEL  = 7
    58  	IMAGE_SYM_CLASS_MEMBER_OF_STRUCT = 8
    59  	IMAGE_SYM_CLASS_ARGUMENT         = 9
    60  	IMAGE_SYM_CLASS_STRUCT_TAG       = 10
    61  	IMAGE_SYM_CLASS_MEMBER_OF_UNION  = 11
    62  	IMAGE_SYM_CLASS_UNION_TAG        = 12
    63  	IMAGE_SYM_CLASS_TYPE_DEFINITION  = 13
    64  	IMAGE_SYM_CLASS_UNDEFINED_STATIC = 14
    65  	IMAGE_SYM_CLASS_ENUM_TAG         = 15
    66  	IMAGE_SYM_CLASS_MEMBER_OF_ENUM   = 16
    67  	IMAGE_SYM_CLASS_REGISTER_PARAM   = 17
    68  	IMAGE_SYM_CLASS_BIT_FIELD        = 18
    69  	IMAGE_SYM_CLASS_FAR_EXTERNAL     = 68 /* Not in PECOFF v8 spec */
    70  	IMAGE_SYM_CLASS_BLOCK            = 100
    71  	IMAGE_SYM_CLASS_FUNCTION         = 101
    72  	IMAGE_SYM_CLASS_END_OF_STRUCT    = 102
    73  	IMAGE_SYM_CLASS_FILE             = 103
    74  	IMAGE_SYM_CLASS_SECTION          = 104
    75  	IMAGE_SYM_CLASS_WEAK_EXTERNAL    = 105
    76  	IMAGE_SYM_CLASS_CLR_TOKEN        = 107
    77  	IMAGE_REL_I386_ABSOLUTE          = 0x0000
    78  	IMAGE_REL_I386_DIR16             = 0x0001
    79  	IMAGE_REL_I386_REL16             = 0x0002
    80  	IMAGE_REL_I386_DIR32             = 0x0006
    81  	IMAGE_REL_I386_DIR32NB           = 0x0007
    82  	IMAGE_REL_I386_SEG12             = 0x0009
    83  	IMAGE_REL_I386_SECTION           = 0x000A
    84  	IMAGE_REL_I386_SECREL            = 0x000B
    85  	IMAGE_REL_I386_TOKEN             = 0x000C
    86  	IMAGE_REL_I386_SECREL7           = 0x000D
    87  	IMAGE_REL_I386_REL32             = 0x0014
    88  	IMAGE_REL_AMD64_ABSOLUTE         = 0x0000
    89  	IMAGE_REL_AMD64_ADDR64           = 0x0001
    90  	IMAGE_REL_AMD64_ADDR32           = 0x0002
    91  	IMAGE_REL_AMD64_ADDR32NB         = 0x0003
    92  	IMAGE_REL_AMD64_REL32            = 0x0004
    93  	IMAGE_REL_AMD64_REL32_1          = 0x0005
    94  	IMAGE_REL_AMD64_REL32_2          = 0x0006
    95  	IMAGE_REL_AMD64_REL32_3          = 0x0007
    96  	IMAGE_REL_AMD64_REL32_4          = 0x0008
    97  	IMAGE_REL_AMD64_REL32_5          = 0x0009
    98  	IMAGE_REL_AMD64_SECTION          = 0x000A
    99  	IMAGE_REL_AMD64_SECREL           = 0x000B
   100  	IMAGE_REL_AMD64_SECREL7          = 0x000C
   101  	IMAGE_REL_AMD64_TOKEN            = 0x000D
   102  	IMAGE_REL_AMD64_SREL32           = 0x000E
   103  	IMAGE_REL_AMD64_PAIR             = 0x000F
   104  	IMAGE_REL_AMD64_SSPAN32          = 0x0010
   105  	IMAGE_REL_ARM_ABSOLUTE           = 0x0000
   106  	IMAGE_REL_ARM_ADDR32             = 0x0001
   107  	IMAGE_REL_ARM_ADDR32NB           = 0x0002
   108  	IMAGE_REL_ARM_BRANCH24           = 0x0003
   109  	IMAGE_REL_ARM_BRANCH11           = 0x0004
   110  	IMAGE_REL_ARM_SECTION            = 0x000E
   111  	IMAGE_REL_ARM_SECREL             = 0x000F
   112  	IMAGE_REL_ARM_MOV32              = 0x0010
   113  	IMAGE_REL_THUMB_MOV32            = 0x0011
   114  	IMAGE_REL_THUMB_BRANCH20         = 0x0012
   115  	IMAGE_REL_THUMB_BRANCH24         = 0x0014
   116  	IMAGE_REL_THUMB_BLX23            = 0x0015
   117  	IMAGE_REL_ARM_PAIR               = 0x0016
   118  	IMAGE_REL_ARM64_ABSOLUTE         = 0x0000
   119  	IMAGE_REL_ARM64_ADDR32           = 0x0001
   120  	IMAGE_REL_ARM64_ADDR32NB         = 0x0002
   121  	IMAGE_REL_ARM64_BRANCH26         = 0x0003
   122  	IMAGE_REL_ARM64_PAGEBASE_REL21   = 0x0004
   123  	IMAGE_REL_ARM64_REL21            = 0x0005
   124  	IMAGE_REL_ARM64_PAGEOFFSET_12A   = 0x0006
   125  	IMAGE_REL_ARM64_PAGEOFFSET_12L   = 0x0007
   126  	IMAGE_REL_ARM64_SECREL           = 0x0008
   127  	IMAGE_REL_ARM64_SECREL_LOW12A    = 0x0009
   128  	IMAGE_REL_ARM64_SECREL_HIGH12A   = 0x000A
   129  	IMAGE_REL_ARM64_SECREL_LOW12L    = 0x000B
   130  	IMAGE_REL_ARM64_TOKEN            = 0x000C
   131  	IMAGE_REL_ARM64_SECTION          = 0x000D
   132  	IMAGE_REL_ARM64_ADDR64           = 0x000E
   133  	IMAGE_REL_ARM64_BRANCH19         = 0x000F
   134  	IMAGE_REL_ARM64_BRANCH14         = 0x0010
   135  	IMAGE_REL_ARM64_REL32            = 0x0011
   136  )
   137  
   138  const (
   139  	// When stored into the PLT value for a symbol, this token tells
   140  	// windynrelocsym to redirect direct references to this symbol to a stub
   141  	// that loads from the corresponding import symbol and then does
   142  	// a jump to the loaded value.
   143  	CreateImportStubPltToken = -2
   144  
   145  	// When stored into the GOT value for a import symbol __imp_X this
   146  	// token tells windynrelocsym to redirect references to the
   147  	// underlying DYNIMPORT symbol X.
   148  	RedirectToDynImportGotToken = -2
   149  )
   150  
   151  // TODO(brainman): maybe just add ReadAt method to bio.Reader instead of creating peBiobuf
   152  
   153  // peBiobuf makes bio.Reader look like io.ReaderAt.
   154  type peBiobuf bio.Reader
   155  
   156  func (f *peBiobuf) ReadAt(p []byte, off int64) (int, error) {
   157  	ret := ((*bio.Reader)(f)).MustSeek(off, 0)
   158  	if ret < 0 {
   159  		return 0, errors.New("fail to seek")
   160  	}
   161  	n, err := f.Read(p)
   162  	if err != nil {
   163  		return 0, err
   164  	}
   165  	return n, nil
   166  }
   167  
   168  // makeUpdater creates a loader.SymbolBuilder if one hasn't been created previously.
   169  // We use this to lazily make SymbolBuilders as we don't always need a builder, and creating them for all symbols might be an error.
   170  func makeUpdater(l *loader.Loader, bld *loader.SymbolBuilder, s loader.Sym) *loader.SymbolBuilder {
   171  	if bld != nil {
   172  		return bld
   173  	}
   174  	bld = l.MakeSymbolUpdater(s)
   175  	return bld
   176  }
   177  
   178  // peImportSymsState tracks the set of DLL import symbols we've seen
   179  // while reading host objects. We create a singleton instance of this
   180  // type, which will persist across multiple host objects.
   181  type peImportSymsState struct {
   182  
   183  	// Text and non-text sections read in by the host object loader.
   184  	secSyms []loader.Sym
   185  
   186  	// SDYNIMPORT symbols encountered along the way
   187  	dynimports map[loader.Sym]struct{}
   188  
   189  	// Loader and arch, for use in postprocessing.
   190  	l    *loader.Loader
   191  	arch *sys.Arch
   192  }
   193  
   194  var importSymsState *peImportSymsState
   195  
   196  func createImportSymsState(l *loader.Loader, arch *sys.Arch) {
   197  	if importSymsState != nil {
   198  		return
   199  	}
   200  	importSymsState = &peImportSymsState{
   201  		dynimports: make(map[loader.Sym]struct{}),
   202  		l:          l,
   203  		arch:       arch,
   204  	}
   205  }
   206  
   207  // peLoaderState holds various bits of useful state information needed
   208  // while loading a single PE object file.
   209  type peLoaderState struct {
   210  	l               *loader.Loader
   211  	arch            *sys.Arch
   212  	f               *pe.File
   213  	pn              string
   214  	sectsyms        map[*pe.Section]loader.Sym
   215  	comdats         map[uint16]int64 // key is section index, val is size
   216  	sectdata        map[*pe.Section][]byte
   217  	localSymVersion int
   218  }
   219  
   220  // comdatDefinitions records the names of symbols for which we've
   221  // previously seen a definition in COMDAT. Key is symbol name, value
   222  // is symbol size (or -1 if we're using the "any" strategy).
   223  var comdatDefinitions = make(map[string]int64)
   224  
   225  // Load loads the PE file pn from input.
   226  // Symbols from the object file are created via the loader 'l', and
   227  // and a slice of the text symbols is returned.
   228  // If an .rsrc section or set of .rsrc$xx sections is found, its symbols are
   229  // returned as rsrc.
   230  func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, input *bio.Reader, pkg string, length int64, pn string) (textp []loader.Sym, rsrc []loader.Sym, err error) {
   231  	state := &peLoaderState{
   232  		l:               l,
   233  		arch:            arch,
   234  		sectsyms:        make(map[*pe.Section]loader.Sym),
   235  		sectdata:        make(map[*pe.Section][]byte),
   236  		localSymVersion: localSymVersion,
   237  		pn:              pn,
   238  	}
   239  	createImportSymsState(state.l, state.arch)
   240  
   241  	// Some input files are archives containing multiple of
   242  	// object files, and pe.NewFile seeks to the start of
   243  	// input file and get confused. Create section reader
   244  	// to stop pe.NewFile looking before current position.
   245  	sr := io.NewSectionReader((*peBiobuf)(input), input.Offset(), 1<<63-1)
   246  
   247  	// TODO: replace pe.NewFile with pe.Load (grep for "add Load function" in debug/pe for details)
   248  	f, err := pe.NewFile(sr)
   249  	if err != nil {
   250  		return nil, nil, err
   251  	}
   252  	defer f.Close()
   253  	state.f = f
   254  
   255  	// TODO return error if found .cormeta
   256  
   257  	// create symbols for mapped sections
   258  	for _, sect := range f.Sections {
   259  		if sect.Characteristics&pe.IMAGE_SCN_MEM_DISCARDABLE != 0 {
   260  			continue
   261  		}
   262  
   263  		if sect.Characteristics&(pe.IMAGE_SCN_CNT_CODE|pe.IMAGE_SCN_CNT_INITIALIZED_DATA|pe.IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 {
   264  			// This has been seen for .idata sections, which we
   265  			// want to ignore. See issues 5106 and 5273.
   266  			continue
   267  		}
   268  
   269  		name := fmt.Sprintf("%s(%s)", pkg, sect.Name)
   270  		s := state.l.LookupOrCreateCgoExport(name, localSymVersion)
   271  		bld := l.MakeSymbolUpdater(s)
   272  
   273  		switch sect.Characteristics & (pe.IMAGE_SCN_CNT_UNINITIALIZED_DATA | pe.IMAGE_SCN_CNT_INITIALIZED_DATA | pe.IMAGE_SCN_MEM_READ | pe.IMAGE_SCN_MEM_WRITE | pe.IMAGE_SCN_CNT_CODE | pe.IMAGE_SCN_MEM_EXECUTE) {
   274  		case pe.IMAGE_SCN_CNT_INITIALIZED_DATA | pe.IMAGE_SCN_MEM_READ: //.rdata
   275  			bld.SetType(sym.SRODATA)
   276  
   277  		case pe.IMAGE_SCN_CNT_UNINITIALIZED_DATA | pe.IMAGE_SCN_MEM_READ | pe.IMAGE_SCN_MEM_WRITE: //.bss
   278  			bld.SetType(sym.SNOPTRBSS)
   279  
   280  		case pe.IMAGE_SCN_CNT_INITIALIZED_DATA | pe.IMAGE_SCN_MEM_READ | pe.IMAGE_SCN_MEM_WRITE: //.data
   281  			bld.SetType(sym.SNOPTRDATA)
   282  
   283  		case pe.IMAGE_SCN_CNT_CODE | pe.IMAGE_SCN_MEM_EXECUTE | pe.IMAGE_SCN_MEM_READ: //.text
   284  			bld.SetType(sym.STEXT)
   285  
   286  		default:
   287  			return nil, nil, fmt.Errorf("unexpected flags %#06x for PE section %s", sect.Characteristics, sect.Name)
   288  		}
   289  
   290  		if bld.Type() != sym.SNOPTRBSS {
   291  			data, err := sect.Data()
   292  			if err != nil {
   293  				return nil, nil, err
   294  			}
   295  			state.sectdata[sect] = data
   296  			bld.SetData(data)
   297  		}
   298  		bld.SetSize(int64(sect.Size))
   299  		state.sectsyms[sect] = s
   300  		if sect.Name == ".rsrc" || strings.HasPrefix(sect.Name, ".rsrc$") {
   301  			rsrc = append(rsrc, s)
   302  		}
   303  	}
   304  
   305  	// Make a prepass over the symbols to collect info about COMDAT symbols.
   306  	if err := state.preprocessSymbols(); err != nil {
   307  		return nil, nil, err
   308  	}
   309  
   310  	// load relocations
   311  	for _, rsect := range f.Sections {
   312  		if _, found := state.sectsyms[rsect]; !found {
   313  			continue
   314  		}
   315  		if rsect.NumberOfRelocations == 0 {
   316  			continue
   317  		}
   318  		if rsect.Characteristics&pe.IMAGE_SCN_MEM_DISCARDABLE != 0 {
   319  			continue
   320  		}
   321  		if rsect.Characteristics&(pe.IMAGE_SCN_CNT_CODE|pe.IMAGE_SCN_CNT_INITIALIZED_DATA|pe.IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 {
   322  			// This has been seen for .idata sections, which we
   323  			// want to ignore. See issues 5106 and 5273.
   324  			continue
   325  		}
   326  
   327  		splitResources := strings.HasPrefix(rsect.Name, ".rsrc$")
   328  		sb := l.MakeSymbolUpdater(state.sectsyms[rsect])
   329  		for j, r := range rsect.Relocs {
   330  			if int(r.SymbolTableIndex) >= len(f.COFFSymbols) {
   331  				return nil, nil, fmt.Errorf("relocation number %d symbol index idx=%d cannot be large then number of symbols %d", j, r.SymbolTableIndex, len(f.COFFSymbols))
   332  			}
   333  			pesym := &f.COFFSymbols[r.SymbolTableIndex]
   334  			_, gosym, err := state.readpesym(pesym)
   335  			if err != nil {
   336  				return nil, nil, err
   337  			}
   338  			if gosym == 0 {
   339  				name, err := pesym.FullName(f.StringTable)
   340  				if err != nil {
   341  					name = string(pesym.Name[:])
   342  				}
   343  				return nil, nil, fmt.Errorf("reloc of invalid sym %s idx=%d type=%d", name, r.SymbolTableIndex, pesym.Type)
   344  			}
   345  
   346  			rSym := gosym
   347  			rSize := uint8(4)
   348  			rOff := int32(r.VirtualAddress)
   349  			var rAdd int64
   350  			var rType objabi.RelocType
   351  			switch arch.Family {
   352  			default:
   353  				return nil, nil, fmt.Errorf("%s: unsupported arch %v", pn, arch.Family)
   354  			case sys.I386, sys.AMD64:
   355  				switch r.Type {
   356  				default:
   357  					return nil, nil, fmt.Errorf("%s: %v: unknown relocation type %v", pn, state.sectsyms[rsect], r.Type)
   358  
   359  				case IMAGE_REL_I386_REL32, IMAGE_REL_AMD64_REL32,
   360  					IMAGE_REL_AMD64_ADDR32, // R_X86_64_PC32
   361  					IMAGE_REL_AMD64_ADDR32NB:
   362  					rType = objabi.R_PCREL
   363  
   364  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   365  
   366  				case IMAGE_REL_I386_DIR32NB, IMAGE_REL_I386_DIR32:
   367  					rType = objabi.R_ADDR
   368  
   369  					// load addend from image
   370  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   371  
   372  				case IMAGE_REL_AMD64_ADDR64: // R_X86_64_64
   373  					rSize = 8
   374  
   375  					rType = objabi.R_ADDR
   376  
   377  					// load addend from image
   378  					rAdd = int64(binary.LittleEndian.Uint64(state.sectdata[rsect][rOff:]))
   379  				}
   380  
   381  			case sys.ARM:
   382  				switch r.Type {
   383  				default:
   384  					return nil, nil, fmt.Errorf("%s: %v: unknown ARM relocation type %v", pn, state.sectsyms[rsect], r.Type)
   385  
   386  				case IMAGE_REL_ARM_SECREL:
   387  					rType = objabi.R_PCREL
   388  
   389  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   390  
   391  				case IMAGE_REL_ARM_ADDR32, IMAGE_REL_ARM_ADDR32NB:
   392  					rType = objabi.R_ADDR
   393  
   394  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   395  
   396  				case IMAGE_REL_ARM_BRANCH24:
   397  					rType = objabi.R_CALLARM
   398  
   399  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   400  				}
   401  
   402  			case sys.ARM64:
   403  				switch r.Type {
   404  				default:
   405  					return nil, nil, fmt.Errorf("%s: %v: unknown ARM64 relocation type %v", pn, state.sectsyms[rsect], r.Type)
   406  
   407  				case IMAGE_REL_ARM64_ADDR32, IMAGE_REL_ARM64_ADDR32NB:
   408  					rType = objabi.R_ADDR
   409  
   410  					rAdd = int64(int32(binary.LittleEndian.Uint32(state.sectdata[rsect][rOff:])))
   411  				}
   412  			}
   413  
   414  			// ld -r could generate multiple section symbols for the
   415  			// same section but with different values, we have to take
   416  			// that into account, or in the case of split resources,
   417  			// the section and its symbols are split into two sections.
   418  			if issect(pesym) || splitResources {
   419  				rAdd += int64(pesym.Value)
   420  			}
   421  
   422  			rel, _ := sb.AddRel(rType)
   423  			rel.SetOff(rOff)
   424  			rel.SetSiz(rSize)
   425  			rel.SetSym(rSym)
   426  			rel.SetAdd(rAdd)
   427  		}
   428  
   429  		sb.SortRelocs()
   430  	}
   431  
   432  	// enter sub-symbols into symbol table.
   433  	for i, numaux := 0, 0; i < len(f.COFFSymbols); i += numaux + 1 {
   434  		pesym := &f.COFFSymbols[i]
   435  
   436  		numaux = int(pesym.NumberOfAuxSymbols)
   437  
   438  		name, err := pesym.FullName(f.StringTable)
   439  		if err != nil {
   440  			return nil, nil, err
   441  		}
   442  		if name == "" {
   443  			continue
   444  		}
   445  		if issect(pesym) {
   446  			continue
   447  		}
   448  		if int(pesym.SectionNumber) > len(f.Sections) {
   449  			continue
   450  		}
   451  		if pesym.SectionNumber == IMAGE_SYM_DEBUG {
   452  			continue
   453  		}
   454  		if pesym.SectionNumber == IMAGE_SYM_ABSOLUTE && bytes.Equal(pesym.Name[:], []byte("@feat.00")) {
   455  			// Microsoft's linker looks at whether all input objects have an empty
   456  			// section called @feat.00. If all of them do, then it enables SEH;
   457  			// otherwise it doesn't enable that feature. So, since around the Windows
   458  			// XP SP2 era, most tools that make PE objects just tack on that section,
   459  			// so that it won't gimp Microsoft's linker logic. Go doesn't support SEH,
   460  			// so in theory, none of this really matters to us. But actually, if the
   461  			// linker tries to ingest an object with @feat.00 -- which are produced by
   462  			// LLVM's resource compiler, for example -- it chokes because of the
   463  			// IMAGE_SYM_ABSOLUTE section that it doesn't know how to deal with. Since
   464  			// @feat.00 is just a marking anyway, skip IMAGE_SYM_ABSOLUTE sections that
   465  			// are called @feat.00.
   466  			continue
   467  		}
   468  		var sect *pe.Section
   469  		if pesym.SectionNumber > 0 {
   470  			sect = f.Sections[pesym.SectionNumber-1]
   471  			if _, found := state.sectsyms[sect]; !found {
   472  				continue
   473  			}
   474  		}
   475  
   476  		bld, s, err := state.readpesym(pesym)
   477  		if err != nil {
   478  			return nil, nil, err
   479  		}
   480  
   481  		if pesym.SectionNumber == 0 { // extern
   482  			if l.SymType(s) == sym.SXREF && pesym.Value > 0 { // global data
   483  				bld = makeUpdater(l, bld, s)
   484  				bld.SetType(sym.SNOPTRDATA)
   485  				bld.SetSize(int64(pesym.Value))
   486  			}
   487  
   488  			continue
   489  		} else if pesym.SectionNumber > 0 && int(pesym.SectionNumber) <= len(f.Sections) {
   490  			sect = f.Sections[pesym.SectionNumber-1]
   491  			if _, found := state.sectsyms[sect]; !found {
   492  				return nil, nil, fmt.Errorf("%s: %v: missing sect.sym", pn, s)
   493  			}
   494  		} else {
   495  			return nil, nil, fmt.Errorf("%s: %v: sectnum < 0!", pn, s)
   496  		}
   497  
   498  		if sect == nil {
   499  			return nil, nil, nil
   500  		}
   501  
   502  		// Check for COMDAT symbol.
   503  		if sz, ok1 := state.comdats[uint16(pesym.SectionNumber-1)]; ok1 {
   504  			if psz, ok2 := comdatDefinitions[l.SymName(s)]; ok2 {
   505  				if sz == psz {
   506  					//  OK to discard, we've seen an instance
   507  					// already.
   508  					continue
   509  				}
   510  			}
   511  		}
   512  		if l.OuterSym(s) != 0 {
   513  			if l.AttrDuplicateOK(s) {
   514  				continue
   515  			}
   516  			outerName := l.SymName(l.OuterSym(s))
   517  			sectName := l.SymName(state.sectsyms[sect])
   518  			return nil, nil, fmt.Errorf("%s: duplicate symbol reference: %s in both %s and %s", pn, l.SymName(s), outerName, sectName)
   519  		}
   520  
   521  		bld = makeUpdater(l, bld, s)
   522  		sectsym := state.sectsyms[sect]
   523  		bld.SetType(l.SymType(sectsym))
   524  		l.AddInteriorSym(sectsym, s)
   525  		bld.SetValue(int64(pesym.Value))
   526  		bld.SetSize(4)
   527  		if l.SymType(sectsym) == sym.STEXT {
   528  			if bld.External() && !bld.DuplicateOK() {
   529  				return nil, nil, fmt.Errorf("%s: duplicate symbol definition", l.SymName(s))
   530  			}
   531  			bld.SetExternal(true)
   532  		}
   533  		if sz, ok := state.comdats[uint16(pesym.SectionNumber-1)]; ok {
   534  			// This is a COMDAT definition. Record that we're picking
   535  			// this instance so that we can ignore future defs.
   536  			if _, ok := comdatDefinitions[l.SymName(s)]; ok {
   537  				return nil, nil, fmt.Errorf("internal error: preexisting COMDAT definition for %q", name)
   538  			}
   539  			comdatDefinitions[l.SymName(s)] = sz
   540  		}
   541  	}
   542  
   543  	// Sort outer lists by address, adding to textp.
   544  	// This keeps textp in increasing address order.
   545  	for _, sect := range f.Sections {
   546  		s := state.sectsyms[sect]
   547  		if s == 0 {
   548  			continue
   549  		}
   550  		l.SortSub(s)
   551  		importSymsState.secSyms = append(importSymsState.secSyms, s)
   552  		if l.SymType(s) == sym.STEXT {
   553  			for ; s != 0; s = l.SubSym(s) {
   554  				if l.AttrOnList(s) {
   555  					return nil, nil, fmt.Errorf("symbol %s listed multiple times", l.SymName(s))
   556  				}
   557  				l.SetAttrOnList(s, true)
   558  				textp = append(textp, s)
   559  			}
   560  		}
   561  	}
   562  
   563  	return textp, rsrc, nil
   564  }
   565  
   566  // PostProcessImports works to resolve inconsistencies with DLL import
   567  // symbols; it is needed when building with more "modern" C compilers
   568  // with internal linkage.
   569  //
   570  // Background: DLL import symbols are data (SNOPTRDATA) symbols whose
   571  // name is of the form "__imp_XXX", which contain a pointer/reference
   572  // to symbol XXX. It's possible to have import symbols for both data
   573  // symbols ("__imp__fmode") and text symbols ("__imp_CreateEventA").
   574  // In some case import symbols are just references to some external
   575  // thing, and in other cases we see actual definitions of import
   576  // symbols when reading host objects.
   577  //
   578  // Previous versions of the linker would in most cases immediately
   579  // "forward" import symbol references, e.g. treat a references to
   580  // "__imp_XXX" a references to "XXX", however this doesn't work well
   581  // with more modern compilers, where you can sometimes see import
   582  // symbols that are defs (as opposed to external refs).
   583  //
   584  // The main actions taken below are to search for references to
   585  // SDYNIMPORT symbols in host object text/data sections and flag the
   586  // symbols for later fixup. When we see a reference to an import
   587  // symbol __imp_XYZ where XYZ corresponds to some SDYNIMPORT symbol,
   588  // we flag the symbol (via GOT setting) so that it can be redirected
   589  // to XYZ later in windynrelocsym. When we see a direct reference to
   590  // an SDYNIMPORT symbol XYZ, we also flag the symbol (via PLT setting)
   591  // to indicated that the reference will need to be redirected to a
   592  // stub.
   593  func PostProcessImports() error {
   594  	ldr := importSymsState.l
   595  	arch := importSymsState.arch
   596  	keeprelocneeded := make(map[loader.Sym]loader.Sym)
   597  	for _, s := range importSymsState.secSyms {
   598  		isText := ldr.SymType(s) == sym.STEXT
   599  		relocs := ldr.Relocs(s)
   600  		for i := 0; i < relocs.Count(); i++ {
   601  			r := relocs.At(i)
   602  			rs := r.Sym()
   603  			if ldr.SymType(rs) == sym.SDYNIMPORT {
   604  				// Tag the symbol for later stub generation.
   605  				ldr.SetPlt(rs, CreateImportStubPltToken)
   606  				continue
   607  			}
   608  			isym, err := LookupBaseFromImport(rs, ldr, arch)
   609  			if err != nil {
   610  				return err
   611  			}
   612  			if isym == 0 {
   613  				continue
   614  			}
   615  			if ldr.SymType(isym) != sym.SDYNIMPORT {
   616  				continue
   617  			}
   618  			// For non-text symbols, forward the reference from __imp_X to
   619  			// X immediately.
   620  			if !isText {
   621  				r.SetSym(isym)
   622  				continue
   623  			}
   624  			// Flag this imp symbol to be processed later in windynrelocsym.
   625  			ldr.SetGot(rs, RedirectToDynImportGotToken)
   626  			// Consistency check: should be no PLT token here.
   627  			splt := ldr.SymPlt(rs)
   628  			if splt != -1 {
   629  				return fmt.Errorf("internal error: import symbol %q has invalid PLT setting %d", ldr.SymName(rs), splt)
   630  			}
   631  			// Flag for dummy relocation.
   632  			keeprelocneeded[rs] = isym
   633  		}
   634  	}
   635  	for k, v := range keeprelocneeded {
   636  		sb := ldr.MakeSymbolUpdater(k)
   637  		r, _ := sb.AddRel(objabi.R_KEEP)
   638  		r.SetSym(v)
   639  	}
   640  	importSymsState = nil
   641  	return nil
   642  }
   643  
   644  func issect(s *pe.COFFSymbol) bool {
   645  	return s.StorageClass == IMAGE_SYM_CLASS_STATIC && s.Type == 0 && s.Name[0] == '.'
   646  }
   647  
   648  func (state *peLoaderState) readpesym(pesym *pe.COFFSymbol) (*loader.SymbolBuilder, loader.Sym, error) {
   649  	symname, err := pesym.FullName(state.f.StringTable)
   650  	if err != nil {
   651  		return nil, 0, err
   652  	}
   653  	var name string
   654  	if issect(pesym) {
   655  		name = state.l.SymName(state.sectsyms[state.f.Sections[pesym.SectionNumber-1]])
   656  	} else {
   657  		name = symname
   658  		// A note on the "_main" exclusion below: the main routine
   659  		// defined by the Go runtime is named "_main", not "main", so
   660  		// when reading references to _main from a host object we want
   661  		// to avoid rewriting "_main" to "main" in this specific
   662  		// instance. See #issuecomment-1143698749 on #35006 for more
   663  		// details on this problem.
   664  		if state.arch.Family == sys.I386 && name[0] == '_' && name != "_main" && !strings.HasPrefix(name, "__imp_") {
   665  			name = name[1:] // _Name => Name
   666  		}
   667  	}
   668  
   669  	// remove last @XXX
   670  	if i := strings.LastIndex(name, "@"); i >= 0 {
   671  		name = name[:i]
   672  	}
   673  
   674  	var s loader.Sym
   675  	var bld *loader.SymbolBuilder
   676  	switch pesym.Type {
   677  	default:
   678  		return nil, 0, fmt.Errorf("%s: invalid symbol type %d", symname, pesym.Type)
   679  
   680  	case IMAGE_SYM_DTYPE_FUNCTION, IMAGE_SYM_DTYPE_NULL:
   681  		switch pesym.StorageClass {
   682  		case IMAGE_SYM_CLASS_EXTERNAL: //global
   683  			s = state.l.LookupOrCreateCgoExport(name, 0)
   684  
   685  		case IMAGE_SYM_CLASS_NULL, IMAGE_SYM_CLASS_STATIC, IMAGE_SYM_CLASS_LABEL:
   686  			s = state.l.LookupOrCreateCgoExport(name, state.localSymVersion)
   687  			bld = makeUpdater(state.l, bld, s)
   688  			bld.SetDuplicateOK(true)
   689  
   690  		default:
   691  			return nil, 0, fmt.Errorf("%s: invalid symbol binding %d", symname, pesym.StorageClass)
   692  		}
   693  	}
   694  
   695  	if s != 0 && state.l.SymType(s) == 0 && (pesym.StorageClass != IMAGE_SYM_CLASS_STATIC || pesym.Value != 0) {
   696  		bld = makeUpdater(state.l, bld, s)
   697  		bld.SetType(sym.SXREF)
   698  	}
   699  
   700  	return bld, s, nil
   701  }
   702  
   703  // preprocessSymbols walks the COFF symbols for the PE file we're
   704  // reading and looks for cases where we have both a symbol definition
   705  // for "XXX" and an "__imp_XXX" symbol, recording these cases in a map
   706  // in the state struct. This information will be used in readpesym()
   707  // above to give such symbols special treatment. This function also
   708  // gathers information about COMDAT sections/symbols for later use
   709  // in readpesym().
   710  func (state *peLoaderState) preprocessSymbols() error {
   711  
   712  	// Locate comdat sections.
   713  	state.comdats = make(map[uint16]int64)
   714  	for i, s := range state.f.Sections {
   715  		if s.Characteristics&uint32(pe.IMAGE_SCN_LNK_COMDAT) != 0 {
   716  			state.comdats[uint16(i)] = int64(s.Size)
   717  		}
   718  	}
   719  
   720  	// Examine symbol defs.
   721  	for i, numaux := 0, 0; i < len(state.f.COFFSymbols); i += numaux + 1 {
   722  		pesym := &state.f.COFFSymbols[i]
   723  		numaux = int(pesym.NumberOfAuxSymbols)
   724  		if pesym.SectionNumber == 0 { // extern
   725  			continue
   726  		}
   727  		symname, err := pesym.FullName(state.f.StringTable)
   728  		if err != nil {
   729  			return err
   730  		}
   731  		if _, isc := state.comdats[uint16(pesym.SectionNumber-1)]; !isc {
   732  			continue
   733  		}
   734  		if pesym.StorageClass != uint8(IMAGE_SYM_CLASS_STATIC) {
   735  			continue
   736  		}
   737  		// This symbol corresponds to a COMDAT section. Read the
   738  		// aux data for it.
   739  		auxsymp, err := state.f.COFFSymbolReadSectionDefAux(i)
   740  		if err != nil {
   741  			return fmt.Errorf("unable to read aux info for section def symbol %d %s: pe.COFFSymbolReadComdatInfo returns %v", i, symname, err)
   742  		}
   743  		if auxsymp.Selection == pe.IMAGE_COMDAT_SELECT_SAME_SIZE {
   744  			// This is supported.
   745  		} else if auxsymp.Selection == pe.IMAGE_COMDAT_SELECT_ANY {
   746  			// Also supported.
   747  			state.comdats[uint16(pesym.SectionNumber-1)] = int64(-1)
   748  		} else {
   749  			// We don't support any of the other strategies at the
   750  			// moment. I suspect that we may need to also support
   751  			// "associative", we'll see.
   752  			return fmt.Errorf("internal error: unsupported COMDAT selection strategy found in path=%s sec=%d strategy=%d idx=%d, please file a bug", state.pn, auxsymp.SecNum, auxsymp.Selection, i)
   753  		}
   754  	}
   755  	return nil
   756  }
   757  
   758  // LookupBaseFromImport examines the symbol "s" to see if it
   759  // corresponds to an import symbol (name of the form "__imp_XYZ") and
   760  // if so, it looks up the underlying target of the import symbol and
   761  // returns it. An error is returned if the symbol is of the form
   762  // "__imp_XYZ" but no XYZ can be found.
   763  func LookupBaseFromImport(s loader.Sym, ldr *loader.Loader, arch *sys.Arch) (loader.Sym, error) {
   764  	sname := ldr.SymName(s)
   765  	if !strings.HasPrefix(sname, "__imp_") {
   766  		return 0, nil
   767  	}
   768  	basename := sname[len("__imp_"):]
   769  	if arch.Family == sys.I386 && basename[0] == '_' {
   770  		basename = basename[1:] // _Name => Name
   771  	}
   772  	isym := ldr.Lookup(basename, 0)
   773  	if isym == 0 {
   774  		return 0, fmt.Errorf("internal error: import symbol %q with no underlying sym", sname)
   775  	}
   776  	return isym, nil
   777  }