Skip to content

Commit aada490

Browse files
committed
cmd/link: write dwarf relocations
For #10776. Change-Id: I11dd441d8e5d6316889ffa8418df8b58c57c677d Reviewed-on: https://go-review.googlesource.com/36982 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1544217 commit aada490

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/cmd/link/internal/amd64/asm.go

+3
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ func pereloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
500500
default:
501501
return false
502502

503+
case obj.R_DWARFREF:
504+
v = ld.IMAGE_REL_AMD64_SECREL
505+
503506
case obj.R_ADDR:
504507
if r.Siz == 8 {
505508
v = ld.IMAGE_REL_AMD64_ADDR64

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,14 @@ func relocsym(ctxt *Link, s *Symbol) {
575575
}
576576
if Linkmode == LinkExternal {
577577
r.Done = 0
578-
r.Type = obj.R_ADDR
578+
// PE code emits IMAGE_REL_I386_SECREL and IMAGE_REL_AMD64_SECREL
579+
// for R_DWARFREF relocations, while R_ADDR is replaced with
580+
// IMAGE_REL_I386_DIR32, IMAGE_REL_AMD64_ADDR64 and IMAGE_REL_AMD64_ADDR32.
581+
// Do not replace R_DWARFREF with R_ADDR for windows -
582+
// let PE code emit correct relocations.
583+
if Headtype != obj.Hwindows && Headtype != obj.Hwindowsgui {
584+
r.Type = obj.R_ADDR
585+
}
579586

580587
r.Xsym = ctxt.Syms.ROLookup(r.Sym.Sect.Name, 0)
581588
r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr)

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ func addexports(ctxt *Link) {
801801

802802
// perelocsect relocates symbols from first in section sect, and returns
803803
// the total number of relocations emitted.
804-
func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
804+
func perelocsect(ctxt *Link, sect *Section, syms []*Symbol, base uint64) int {
805805
// If main section has no bits, nothing to relocate.
806806
if sect.Vaddr >= sect.Seg.Vaddr+sect.Seg.Filelen {
807807
return 0
@@ -841,7 +841,7 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
841841
if r.Xsym.Dynid < 0 {
842842
Errorf(sym, "reloc %d to non-coff symbol %s (outer=%s) %d", r.Type, r.Sym.Name, r.Xsym.Name, r.Sym.Type)
843843
}
844-
if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-sect.Seg.Vaddr)) {
844+
if !Thearch.PEreloc1(sym, r, int64(uint64(sym.Value+int64(r.Off))-base)) {
845845
Errorf(sym, "unsupported obj reloc %d/%d to %s", r.Type, r.Siz, r.Sym.Name)
846846
}
847847

@@ -887,21 +887,34 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
887887
}
888888

889889
peemitsectreloc(text, func() int {
890-
n := perelocsect(ctxt, Segtext.Sect, ctxt.Textp)
890+
n := perelocsect(ctxt, Segtext.Sect, ctxt.Textp, Segtext.Vaddr)
891891
for sect := Segtext.Sect.Next; sect != nil; sect = sect.Next {
892-
n += perelocsect(ctxt, sect, datap)
892+
n += perelocsect(ctxt, sect, datap, Segtext.Vaddr)
893893
}
894894
return n
895895
})
896896

897897
peemitsectreloc(data, func() int {
898898
var n int
899899
for sect := Segdata.Sect; sect != nil; sect = sect.Next {
900-
n += perelocsect(ctxt, sect, datap)
900+
n += perelocsect(ctxt, sect, datap, Segdata.Vaddr)
901901
}
902902
return n
903903
})
904904

905+
dwarfLoop:
906+
for sect := Segdwarf.Sect; sect != nil; sect = sect.Next {
907+
for i, name := range shNames {
908+
if sect.Name == name {
909+
peemitsectreloc(&sh[i], func() int {
910+
return perelocsect(ctxt, sect, dwarfp, sect.Vaddr)
911+
})
912+
continue dwarfLoop
913+
}
914+
}
915+
Errorf(nil, "peemitsectreloc: could not find %q section", sect.Name)
916+
}
917+
905918
peemitsectreloc(ctors, func() int {
906919
dottext := ctxt.Syms.Lookup(".text", 0)
907920
Lputl(0)

src/cmd/link/internal/x86/asm.go

+3
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ func pereloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
482482
default:
483483
return false
484484

485+
case obj.R_DWARFREF:
486+
v = ld.IMAGE_REL_I386_SECREL
487+
485488
case obj.R_ADDR:
486489
v = ld.IMAGE_REL_I386_DIR32
487490

0 commit comments

Comments
 (0)