Commit b660a4b0 authored by Alex Brainman's avatar Alex Brainman

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/36978Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e9abf1a7
...@@ -2033,9 +2033,6 @@ func (ctxt *Link) textaddress() { ...@@ -2033,9 +2033,6 @@ func (ctxt *Link) textaddress() {
ctxt.Textp[0] = text ctxt.Textp[0] = text
} }
if Headtype == obj.Hwindows || Headtype == obj.Hwindowsgui {
ctxt.Syms.Lookup(".text", 0).Sect = sect
}
va := uint64(*FlagTextAddr) va := uint64(*FlagTextAddr)
n := 1 n := 1
sect.Vaddr = va sect.Vaddr = va
...@@ -2295,9 +2292,6 @@ func (ctxt *Link) address() { ...@@ -2295,9 +2292,6 @@ func (ctxt *Link) address() {
ctxt.xdefine("runtime.text", obj.STEXT, int64(text.Vaddr)) ctxt.xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
ctxt.xdefine("runtime.etext", obj.STEXT, int64(lasttext.Vaddr+lasttext.Length)) 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 // If there are multiple text sections, create runtime.text.n for
// their section Vaddr, using n for index // their section Vaddr, using n for index
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
) )
const ( 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_UNDEFINED = 0
IMAGE_SYM_ABSOLUTE = -1 IMAGE_SYM_ABSOLUTE = -1
IMAGE_SYM_DEBUG = -2 IMAGE_SYM_DEBUG = -2
......
...@@ -964,6 +964,25 @@ func newPEDWARFSection(ctxt *Link, name string, size int64) *IMAGE_SECTION_HEADE ...@@ -964,6 +964,25 @@ func newPEDWARFSection(ctxt *Link, name string, size int64) *IMAGE_SECTION_HEADE
func writePESymTableRecords(ctxt *Link) int { func writePESymTableRecords(ctxt *Link) int {
var symcnt 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) { put := func(ctxt *Link, s *Symbol, name string, type_ SymbolType, addr int64, gotype *Symbol) {
if s == nil { if s == nil {
return return
...@@ -978,16 +997,13 @@ func writePESymTableRecords(ctxt *Link) int { ...@@ -978,16 +997,13 @@ func writePESymTableRecords(ctxt *Link) int {
} }
// Only windows/386 requires underscore prefix on external symbols. // 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 && if SysArch.Family == sys.I386 &&
Linkmode == LinkExternal && Linkmode == LinkExternal &&
(s.Type == obj.SHOSTOBJ || (s.Type == obj.SHOSTOBJ || s.Attr.CgoExport()) {
s.Attr.CgoExport() ||
s.Name == ".text") {
s.Name = "_" + s.Name s.Name = "_" + s.Name
} }
var typ uint16 typ := uint16(IMAGE_SYM_TYPE_NULL)
var sect int var sect int
var value int64 var value int64
// Note: although address of runtime.edata (type SDATA) is at the start of .bss section // Note: although address of runtime.edata (type SDATA) is at the start of .bss section
...@@ -1008,35 +1024,21 @@ func writePESymTableRecords(ctxt *Link) int { ...@@ -1008,35 +1024,21 @@ func writePESymTableRecords(ctxt *Link) int {
} else { } else {
Errorf(s, "addpesym %#x", addr) Errorf(s, "addpesym %#x", addr)
} }
if typ != IMAGE_SYM_TYPE_NULL {
// write COFF symbol table record } else if Linkmode != LinkExternal {
if len(s.Name) > 8 { // TODO: fix IMAGE_SYM_DTYPE_ARRAY value and use following expression, instead of 0x0308
Lputl(0) typ = IMAGE_SYM_DTYPE_ARRAY<<8 + IMAGE_SYM_TYPE_STRUCT
Lputl(uint32(strtbladd(s.Name))) typ = 0x0308 // "array of structs"
} else {
strnput(s.Name, 8)
}
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 writeOneSymbol(s, value, sect, typ, IMAGE_SYM_CLASS_EXTERNAL)
Cput(0) // no aux entries
s.Dynid = int32(symcnt)
symcnt++
} }
if Linkmode == LinkExternal { if Linkmode == LinkExternal {
s := ctxt.Syms.Lookup(".text", 0) // Include section symbols as external, because
if s.Type == obj.STEXT { // .ctors and .debug_* section relocations refer to it.
put(ctxt, s, s.Name, TextSym, s.Value, nil) for idx, name := range shNames {
sym := ctxt.Syms.Lookup(name, 0)
writeOneSymbol(sym, 0, idx+1, IMAGE_SYM_TYPE_NULL, IMAGE_SYM_CLASS_STATIC)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment