From 3558c16d48452d2f7e725dbefe5938f1575dd980 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Mon, 28 Aug 2017 13:14:43 +1000 Subject: [PATCH] cmd/link: introduce and use peFile.emitRelocations Change-Id: I2458d1391af063d99ba1e446bcf12f7c41ae5f6b Reviewed-on: https://go-review.googlesource.com/59419 Reviewed-by: Ian Lance Taylor --- src/cmd/link/internal/ld/pe.go | 104 ++++++++++++++++----------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index 71dc1eac72..b92885333b 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -548,6 +548,57 @@ func (f *peFile) addInitArray(ctxt *Link) *peSection { return sect } +// emitRelocations emits relocation entries for go.o in external linking. +func (f *peFile) emitRelocations(ctxt *Link, text, data, ctors *peSection) { + for coutbuf.Offset()&7 != 0 { + Cput(0) + } + + text.emitRelocations(func() int { + n := perelocsect(ctxt, Segtext.Sections[0], ctxt.Textp, Segtext.Vaddr) + for _, sect := range Segtext.Sections[1:] { + n += perelocsect(ctxt, sect, datap, Segtext.Vaddr) + } + return n + }) + + data.emitRelocations(func() int { + var n int + for _, sect := range Segdata.Sections { + n += perelocsect(ctxt, sect, datap, Segdata.Vaddr) + } + return n + }) + +dwarfLoop: + for _, sect := range Segdwarf.Sections { + for _, pesect := range f.sections { + if sect.Name == pesect.name { + pesect.emitRelocations(func() int { + return perelocsect(ctxt, sect, dwarfp, sect.Vaddr) + }) + continue dwarfLoop + } + } + Errorf(nil, "emitRelocations: could not find %q section", sect.Name) + } + + ctors.emitRelocations(func() int { + dottext := ctxt.Syms.Lookup(".text", 0) + Lputl(0) + Lputl(uint32(dottext.Dynid)) + switch objabi.GOARCH { + default: + Errorf(dottext, "unknown architecture for PE: %q\n", objabi.GOARCH) + case "386": + Wputl(IMAGE_REL_I386_DIR32) + case "amd64": + Wputl(IMAGE_REL_AMD64_ADDR64) + } + return 1 + }) +} + var pefile peFile func Peinit(ctxt *Link) { @@ -976,57 +1027,6 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol, base uint64) int { return relocs } -// peemitreloc emits relocation entries for go.o in external linking. -func peemitreloc(ctxt *Link, text, data, ctors *peSection) { - for coutbuf.Offset()&7 != 0 { - Cput(0) - } - - text.emitRelocations(func() int { - n := perelocsect(ctxt, Segtext.Sections[0], ctxt.Textp, Segtext.Vaddr) - for _, sect := range Segtext.Sections[1:] { - n += perelocsect(ctxt, sect, datap, Segtext.Vaddr) - } - return n - }) - - data.emitRelocations(func() int { - var n int - for _, sect := range Segdata.Sections { - n += perelocsect(ctxt, sect, datap, Segdata.Vaddr) - } - return n - }) - -dwarfLoop: - for _, sect := range Segdwarf.Sections { - for _, pesect := range pefile.sections { - if sect.Name == pesect.name { - pesect.emitRelocations(func() int { - return perelocsect(ctxt, sect, dwarfp, sect.Vaddr) - }) - continue dwarfLoop - } - } - Errorf(nil, "peemitreloc: could not find %q section", sect.Name) - } - - ctors.emitRelocations(func() int { - dottext := ctxt.Syms.Lookup(".text", 0) - Lputl(0) - Lputl(uint32(dottext.Dynid)) - switch objabi.GOARCH { - default: - Errorf(dottext, "unknown architecture for PE: %q\n", objabi.GOARCH) - case "386": - Wputl(IMAGE_REL_I386_DIR32) - case "amd64": - Wputl(IMAGE_REL_AMD64_ADDR64) - } - return 1 - }) -} - func (ctxt *Link) dope() { /* relocation table */ rel := ctxt.Syms.Lookup(".rel", 0) @@ -1257,7 +1257,7 @@ func Asmbpe(ctxt *Link) { addpesymtable(ctxt) addpersrc(ctxt) if Linkmode == LinkExternal { - peemitreloc(ctxt, t, d, c) + pefile.emitRelocations(ctxt, t, d, c) } fh.NumberOfSections = uint16(len(pefile.sections))