]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: set VirtualAddress to 0 for external linker
authorAlex Brainman <alex.brainman@gmail.com>
Mon, 6 Feb 2017 04:18:26 +0000 (15:18 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Tue, 21 Feb 2017 01:05:55 +0000 (01:05 +0000)
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 <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/pe.go

index aca8973a8598823c6295288ab8cdf6774b36f003..e7200d1aa0b4b09b40bea5d6479f027c99bfbbbc 100644 (file)
@@ -656,9 +656,6 @@ func relocsym(ctxt *Link, s *Symbol) {
                                        // PE/COFF's PC32 relocation uses the address after the relocated
                                        // bytes as the base. Compensate by skewing the addend.
                                        o += int64(r.Siz)
-                                       // GNU ld always add VirtualAddress of the .text section to the
-                                       // relocated address, compensate that.
-                                       o -= int64(s.Sect.Vaddr - PEBASE)
                                } else {
                                        Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, Headtype)
                                }
index 6a1c19520c79e9d0ee1faedfd037d59e49fb68ea..924e18e1e5b512b88a9f351d556824d5b5e026c4 100644 (file)
@@ -485,6 +485,11 @@ func pewrite() {
        } else {
                binary.Write(&coutbuf, binary.LittleEndian, &oh)
        }
+       if Linkmode == LinkExternal {
+               for i := range sh[:pensect] {
+                       sh[i].VirtualAddress = 0
+               }
+       }
        binary.Write(&coutbuf, binary.LittleEndian, sh[:pensect])
 }
 
@@ -828,7 +833,7 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
                        if r.Xsym.Dynid < 0 {
                                Errorf(sym, "reloc %d to non-coff symbol %s (outer=%s) %d", r.Type, r.Sym.Name, r.Xsym.Name, r.Sym.Type)
                        }
-                       if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-PEBASE)) {
+                       if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-sect.Seg.Vaddr)) {
                                Errorf(sym, "unsupported obj reloc %d/%d to %s", r.Type, r.Siz, r.Sym.Name)
                        }
 
@@ -896,8 +901,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
        dottext := ctxt.Syms.Lookup(".text", 0)
        ctors.NumberOfRelocations = 1
        ctors.PointerToRelocations = uint32(coutbuf.Offset())
-       sectoff := ctors.VirtualAddress
-       Lputl(sectoff)
+       Lputl(0)
        Lputl(uint32(dottext.Dynid))
        switch obj.GOARCH {
        default: