Skip to content

Commit b32170a

Browse files
committed
cmd/link: simplify peemitreloc
No functional changes. For #10776. Change-Id: If9a5ef832af116c5802b06a38e0c050d7363f2d5 Reviewed-on: https://go-review.googlesource.com/36981 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d0a978f commit b32170a

File tree

1 file changed

+45
-49
lines changed
  • src/cmd/link/internal/ld

1 file changed

+45
-49
lines changed

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

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -854,72 +854,68 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
854854
return relocs
855855
}
856856

857-
// peemitreloc emits relocation entries for go.o in external linking.
858-
func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
859-
for coutbuf.Offset()&7 != 0 {
860-
Cput(0)
861-
}
862-
863-
text.PointerToRelocations = uint32(coutbuf.Offset())
857+
// peemitsectreloc emits the relocation entries for sect.
858+
// The actual relocations are emitted by relocfn.
859+
// This updates the corresponding PE section table entry
860+
// with the relocation offset and count.
861+
func peemitsectreloc(sect *IMAGE_SECTION_HEADER, relocfn func() int) {
862+
sect.PointerToRelocations = uint32(coutbuf.Offset())
864863
// first entry: extended relocs
865864
Lputl(0) // placeholder for number of relocation + 1
866865
Lputl(0)
867866
Wputl(0)
868867

869-
n := perelocsect(ctxt, Segtext.Sect, ctxt.Textp) + 1
870-
for sect := Segtext.Sect.Next; sect != nil; sect = sect.Next {
871-
n += perelocsect(ctxt, sect, datap)
872-
}
868+
n := relocfn() + 1
873869

874870
cpos := coutbuf.Offset()
875-
Cseek(int64(text.PointerToRelocations))
871+
Cseek(int64(sect.PointerToRelocations))
876872
Lputl(uint32(n))
877873
Cseek(cpos)
878874
if n > 0x10000 {
879875
n = 0x10000
880-
text.Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
876+
sect.Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
881877
} else {
882-
text.PointerToRelocations += 10 // skip the extend reloc entry
878+
sect.PointerToRelocations += 10 // skip the extend reloc entry
883879
}
884-
text.NumberOfRelocations = uint16(n - 1)
885-
886-
data.PointerToRelocations = uint32(cpos)
887-
// first entry: extended relocs
888-
Lputl(0) // placeholder for number of relocation + 1
889-
Lputl(0)
890-
Wputl(0)
880+
sect.NumberOfRelocations = uint16(n - 1)
881+
}
891882

892-
n = 1
893-
for sect := Segdata.Sect; sect != nil; sect = sect.Next {
894-
n += perelocsect(ctxt, sect, datap)
883+
// peemitreloc emits relocation entries for go.o in external linking.
884+
func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
885+
for coutbuf.Offset()&7 != 0 {
886+
Cput(0)
895887
}
896888

897-
cpos = coutbuf.Offset()
898-
Cseek(int64(data.PointerToRelocations))
899-
Lputl(uint32(n))
900-
Cseek(cpos)
901-
if n > 0x10000 {
902-
n = 0x10000
903-
data.Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
904-
} else {
905-
data.PointerToRelocations += 10 // skip the extend reloc entry
906-
}
907-
data.NumberOfRelocations = uint16(n - 1)
889+
peemitsectreloc(text, func() int {
890+
n := perelocsect(ctxt, Segtext.Sect, ctxt.Textp)
891+
for sect := Segtext.Sect.Next; sect != nil; sect = sect.Next {
892+
n += perelocsect(ctxt, sect, datap)
893+
}
894+
return n
895+
})
908896

909-
dottext := ctxt.Syms.Lookup(".text", 0)
910-
ctors.NumberOfRelocations = 1
911-
ctors.PointerToRelocations = uint32(coutbuf.Offset())
912-
Lputl(0)
913-
Lputl(uint32(dottext.Dynid))
914-
switch obj.GOARCH {
915-
default:
916-
fmt.Fprintf(os.Stderr, "link: unknown architecture for PE: %q\n", obj.GOARCH)
917-
os.Exit(2)
918-
case "386":
919-
Wputl(IMAGE_REL_I386_DIR32)
920-
case "amd64":
921-
Wputl(IMAGE_REL_AMD64_ADDR64)
922-
}
897+
peemitsectreloc(data, func() int {
898+
var n int
899+
for sect := Segdata.Sect; sect != nil; sect = sect.Next {
900+
n += perelocsect(ctxt, sect, datap)
901+
}
902+
return n
903+
})
904+
905+
peemitsectreloc(ctors, func() int {
906+
dottext := ctxt.Syms.Lookup(".text", 0)
907+
Lputl(0)
908+
Lputl(uint32(dottext.Dynid))
909+
switch obj.GOARCH {
910+
default:
911+
Errorf(dottext, "unknown architecture for PE: %q\n", obj.GOARCH)
912+
case "386":
913+
Wputl(IMAGE_REL_I386_DIR32)
914+
case "amd64":
915+
Wputl(IMAGE_REL_AMD64_ADDR64)
916+
}
917+
return 1
918+
})
923919
}
924920

925921
func (ctxt *Link) dope() {

0 commit comments

Comments
 (0)