]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: add all pe section names to pe symbol table
authorAlex Brainman <alex.brainman@gmail.com>
Wed, 8 Feb 2017 02:59:19 +0000 (13:59 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Tue, 21 Feb 2017 05:52:04 +0000 (05:52 +0000)
dwarf relocations refer to dwarf section symbols, so dwarf
section symbols must be present in pe symbol table before we
write dwarf relocations.

.ctors pe section already refer to .text symbol.

Write all pe section name symbols into symbol table, so we
can use them whenever we need them.

This CL also simplified some code.

For #10776.

Change-Id: I9b8c680ea75904af90c797a06bbb1f4df19e34b6
Reviewed-on: https://go-review.googlesource.com/36978
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/ldpe.go
src/cmd/link/internal/ld/pe.go

index e7200d1aa0b4b09b40bea5d6479f027c99bfbbbc..582a68ea0820441d87b5da7631e968f03e019e1b 100644 (file)
@@ -2033,9 +2033,6 @@ func (ctxt *Link) textaddress() {
                ctxt.Textp[0] = text
        }
 
-       if Headtype == obj.Hwindows || Headtype == obj.Hwindowsgui {
-               ctxt.Syms.Lookup(".text", 0).Sect = sect
-       }
        va := uint64(*FlagTextAddr)
        n := 1
        sect.Vaddr = va
@@ -2295,9 +2292,6 @@ func (ctxt *Link) address() {
 
        ctxt.xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
        ctxt.xdefine("runtime.etext", obj.STEXT, int64(lasttext.Vaddr+lasttext.Length))
-       if Headtype == obj.Hwindows || Headtype == obj.Hwindowsgui {
-               ctxt.xdefine(".text", obj.STEXT, int64(text.Vaddr))
-       }
 
        // If there are multiple text sections, create runtime.text.n for
        // their section Vaddr, using n for index
index f9c49d0dcec3a6734c4bcc16f1602a6a8cee1f3b..f867dbf7ad7177658d05caf911907e9d0de794f7 100644 (file)
@@ -18,6 +18,7 @@ import (
 )
 
 const (
+       // TODO: the Microsoft doco says IMAGE_SYM_DTYPE_ARRAY is 3 (same with IMAGE_SYM_DTYPE_POINTER and IMAGE_SYM_DTYPE_FUNCTION)
        IMAGE_SYM_UNDEFINED              = 0
        IMAGE_SYM_ABSOLUTE               = -1
        IMAGE_SYM_DEBUG                  = -2
index 950261fd9a562e40613124a5c2962323f2b8ee6f..876bc70939c49ca9dd11463347f0a543d6939ab6 100644 (file)
@@ -964,6 +964,25 @@ func newPEDWARFSection(ctxt *Link, name string, size int64) *IMAGE_SECTION_HEADE
 func writePESymTableRecords(ctxt *Link) int {
        var symcnt int
 
+       writeOneSymbol := func(s *Symbol, addr int64, sectidx int, typ uint16, class uint8) {
+               // write COFF symbol table record
+               if len(s.Name) > 8 {
+                       Lputl(0)
+                       Lputl(uint32(strtbladd(s.Name)))
+               } else {
+                       strnput(s.Name, 8)
+               }
+               Lputl(uint32(addr))
+               Wputl(uint16(sectidx))
+               Wputl(typ)
+               Cput(class)
+               Cput(0) // no aux entries
+
+               s.Dynid = int32(symcnt)
+
+               symcnt++
+       }
+
        put := func(ctxt *Link, s *Symbol, name string, type_ SymbolType, addr int64, gotype *Symbol) {
                if s == nil {
                        return
@@ -978,16 +997,13 @@ func writePESymTableRecords(ctxt *Link) int {
                }
 
                // Only windows/386 requires underscore prefix on external symbols.
-               // Include .text symbol as external, because .ctors section relocations refer to it.
                if SysArch.Family == sys.I386 &&
                        Linkmode == LinkExternal &&
-                       (s.Type == obj.SHOSTOBJ ||
-                               s.Attr.CgoExport() ||
-                               s.Name == ".text") {
+                       (s.Type == obj.SHOSTOBJ || s.Attr.CgoExport()) {
                        s.Name = "_" + s.Name
                }
 
-               var typ uint16
+               typ := uint16(IMAGE_SYM_TYPE_NULL)
                var sect int
                var value int64
                // Note: although address of runtime.edata (type SDATA) is at the start of .bss section
@@ -1008,35 +1024,21 @@ func writePESymTableRecords(ctxt *Link) int {
                } else {
                        Errorf(s, "addpesym %#x", addr)
                }
-
-               // write COFF symbol table record
-               if len(s.Name) > 8 {
-                       Lputl(0)
-                       Lputl(uint32(strtbladd(s.Name)))
-               } else {
-                       strnput(s.Name, 8)
+               if typ != IMAGE_SYM_TYPE_NULL {
+               } else if Linkmode != LinkExternal {
+                       // TODO: fix IMAGE_SYM_DTYPE_ARRAY value and use following expression, instead of 0x0308
+                       typ = IMAGE_SYM_DTYPE_ARRAY<<8 + IMAGE_SYM_TYPE_STRUCT
+                       typ = 0x0308 // "array of structs"
                }
-               Lputl(uint32(value))
-               Wputl(uint16(sect))
-               if typ != 0 {
-                       Wputl(typ)
-               } else if Linkmode == LinkExternal {
-                       Wputl(0)
-               } else {
-                       Wputl(0x0308) // "array of structs"
-               }
-               Cput(2) // storage class: external
-               Cput(0) // no aux entries
-
-               s.Dynid = int32(symcnt)
-
-               symcnt++
+               writeOneSymbol(s, value, sect, typ, IMAGE_SYM_CLASS_EXTERNAL)
        }
 
        if Linkmode == LinkExternal {
-               s := ctxt.Syms.Lookup(".text", 0)
-               if s.Type == obj.STEXT {
-                       put(ctxt, s, s.Name, TextSym, s.Value, nil)
+               // Include section symbols as external, because
+               // .ctors and .debug_* section relocations refer to it.
+               for idx, name := range shNames {
+                       sym := ctxt.Syms.Lookup(name, 0)
+                       writeOneSymbol(sym, 0, idx+1, IMAGE_SYM_TYPE_NULL, IMAGE_SYM_CLASS_STATIC)
                }
        }