Skip to content

Commit a7e2556

Browse files
committed
cmd/link: set VirtualAddress to 0 for external linker
This is what gcc does when it generates object files. And pecoff.doc says: "for simplicity, compilers should set this to zero". It is easier to count everything, when it starts from 0. Make go linker do the same. For #10776. Change-Id: Iffa4b3ad86160624ed34adf1c6ba13feba34c658 Reviewed-on: https://go-review.googlesource.com/36976 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a37f9d8 commit a7e2556

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

-3
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,6 @@ func relocsym(ctxt *Link, s *Symbol) {
656656
// PE/COFF's PC32 relocation uses the address after the relocated
657657
// bytes as the base. Compensate by skewing the addend.
658658
o += int64(r.Siz)
659-
// GNU ld always add VirtualAddress of the .text section to the
660-
// relocated address, compensate that.
661-
o -= int64(s.Sect.Vaddr - PEBASE)
662659
} else {
663660
Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, Headtype)
664661
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ func pewrite() {
485485
} else {
486486
binary.Write(&coutbuf, binary.LittleEndian, &oh)
487487
}
488+
if Linkmode == LinkExternal {
489+
for i := range sh[:pensect] {
490+
sh[i].VirtualAddress = 0
491+
}
492+
}
488493
binary.Write(&coutbuf, binary.LittleEndian, sh[:pensect])
489494
}
490495

@@ -828,7 +833,7 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
828833
if r.Xsym.Dynid < 0 {
829834
Errorf(sym, "reloc %d to non-coff symbol %s (outer=%s) %d", r.Type, r.Sym.Name, r.Xsym.Name, r.Sym.Type)
830835
}
831-
if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-PEBASE)) {
836+
if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-sect.Seg.Vaddr)) {
832837
Errorf(sym, "unsupported obj reloc %d/%d to %s", r.Type, r.Siz, r.Sym.Name)
833838
}
834839

@@ -896,8 +901,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
896901
dottext := ctxt.Syms.Lookup(".text", 0)
897902
ctors.NumberOfRelocations = 1
898903
ctors.PointerToRelocations = uint32(coutbuf.Offset())
899-
sectoff := ctors.VirtualAddress
900-
Lputl(sectoff)
904+
Lputl(0)
901905
Lputl(uint32(dottext.Dynid))
902906
switch obj.GOARCH {
903907
default:

0 commit comments

Comments
 (0)