From b660a4b04d0a88e86d15c1235a4d3bdf1efcd12c Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Wed, 8 Feb 2017 13:59:19 +1100 Subject: [PATCH] cmd/link: add all pe section names to pe symbol table 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 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/cmd/link/internal/ld/data.go | 6 ---- src/cmd/link/internal/ld/ldpe.go | 1 + src/cmd/link/internal/ld/pe.go | 62 ++++++++++++++++---------------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index e7200d1aa0..582a68ea08 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -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 diff --git a/src/cmd/link/internal/ld/ldpe.go b/src/cmd/link/internal/ld/ldpe.go index f9c49d0dce..f867dbf7ad 100644 --- a/src/cmd/link/internal/ld/ldpe.go +++ b/src/cmd/link/internal/ld/ldpe.go @@ -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 diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index 950261fd9a..876bc70939 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -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) } }