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