@@ -854,72 +854,68 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
854
854
return relocs
855
855
}
856
856
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 ())
864
863
// first entry: extended relocs
865
864
Lputl (0 ) // placeholder for number of relocation + 1
866
865
Lputl (0 )
867
866
Wputl (0 )
868
867
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
873
869
874
870
cpos := coutbuf .Offset ()
875
- Cseek (int64 (text .PointerToRelocations ))
871
+ Cseek (int64 (sect .PointerToRelocations ))
876
872
Lputl (uint32 (n ))
877
873
Cseek (cpos )
878
874
if n > 0x10000 {
879
875
n = 0x10000
880
- text .Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
876
+ sect .Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
881
877
} else {
882
- text .PointerToRelocations += 10 // skip the extend reloc entry
878
+ sect .PointerToRelocations += 10 // skip the extend reloc entry
883
879
}
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
+ }
891
882
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 )
895
887
}
896
888
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
+ })
908
896
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
+ })
923
919
}
924
920
925
921
func (ctxt * Link ) dope () {
0 commit comments