[PATCH] elf/x86-64: Adjust R_AMD64_DIR64/R_AMD64_DIR32 for PE/x86-64 inputs

H.J. Lu hjl.tools@gmail.com
Tue Jan 12 13:08:43 GMT 2021


Subtract the value of the section contents for R_AMD64_DIR64 and
R_AMD64_DIR32 relocations when generating ELF output from PE/x86-64
inputs.

bfd/

	PR ld/27171
	* reloc.c (bfd_perform_relocation): Adjust R_AMD64_DIR64 and
	R_AMD64_DIR32 relocations for PE/x86-64 inputs.

ld/

	PR ld/27171
	* testsuite/ld-x86-64/pe-x86-64-5.obj.bz2: New file.
	* testsuite/ld-x86-64/pe-x86-64-5.od: Likewise.
	* testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise.
	* testsuite/ld-x86-64/pe-x86-64.exp: Run PR ld/27171 test.
---
 bfd/reloc.c                                |   7 +++++
 ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 | Bin 0 -> 685 bytes
 ld/testsuite/ld-x86-64/pe-x86-64-5.od      |  34 +++++++++++++++++++++
 ld/testsuite/ld-x86-64/pe-x86-64-5.rd      |  19 ++++++++++++
 ld/testsuite/ld-x86-64/pe-x86-64.exp       |  10 ++++++
 5 files changed, 70 insertions(+)
 create mode 100644 ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2
 create mode 100644 ld/testsuite/ld-x86-64/pe-x86-64-5.od
 create mode 100644 ld/testsuite/ld-x86-64/pe-x86-64-5.rd

diff --git a/bfd/reloc.c b/bfd/reloc.c
index 46c996ffbea..4f4b95a0b7f 100644
--- a/bfd/reloc.c
+++ b/bfd/reloc.c
@@ -921,6 +921,13 @@ space consuming.  For each target:
       if (howto->type >= R_AMD64_PCRLONG_1
 	  && howto->type <= R_AMD64_PCRLONG_5)
 	relocation -= (bfd_vma)(howto->type - R_AMD64_PCRLONG);
+      else if (howto->type == R_AMD64_DIR64
+	       || howto->type == R_AMD64_DIR32)
+	{
+	  bfd_vma val = read_reloc (abfd, (bfd_byte *) data + octets,
+				    howto);
+	  relocation -= val & howto->src_mask;
+	}
     }
 
   /* FIXME: This overflow checking is incomplete, because the value
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 b/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2
new file mode 100644
index 0000000000000000000000000000000000000000..bb0d38ce7f3ad9698c1c376edecb39b72cd4dec2
GIT binary patch
literal 685
zcmV;e0#f}#T4*^jL0KkKSyla#qyPax|NsBrde>b0#oT}A--Q4F-|#^~LSRe~$Q9@W
zLlpp_BdNdv^M)+2YNx7xNs??NJxqpx&}h&A003=4>HrNI8hR6I0i#18(@#^<4I*lp
zqb7q>(@jByG->J?0MIlv000008UO$Q06~)gCIA9p0000J1i%Eq0231c0231s2~8x`
zG}Ql99+T6_Luz`4nWz~6Xah!#0000q0BNR;9-}6j7)&tkb0)6roF>_b?zB?6qd>w@
zq|UVGs=08Qo?KF!({coPC!&UO4H1_y2+bj?7w_DHl%z#WfKj9#DVQ|%IlxrZ%B2M8
zDb{CC-(7E3yW2A~>6=VLFiXzmWW%Ot_Z^t*rfvEd36K=fa?@p~E%4)sOKG!zC1}k}
z!b<M<Sr2RzkiZ=G*>SRwfWLodG0<EjlW@w=Xi?07{r8y-1Ug}^AoHe~SV4|-seImS
zwaraJC*U~8ZkyRduCjb9P(4HfAHUh=aIqf0Az0VYiwGcaKx=Kge2t0*5E;3SyY!G_
zON50GV6IupDNHxF_y=g0IX7w!UVvCcNP&l<QaRmDAD)e3u)`6AxgM%W5szgd@Cx9^
zUJLjmbFR9SuO%lv%cW56h{#h88|6gU6#`v`VtkC9u9pbNf%*IvjEz|tPlFPd6!N)~
zF>)xApp=PQ<sC?gqr-9o#5ADCe%j&em`oE3%L)`x23_JQdRbaPG<Tpg39j(!4T|X<
zG?*145Ozp0jtoKa!IYpdz-F=Fi415G!pT*`=*1$Z=%_@cX4>Sb#w7aJI}{WMh241S
ziq;NI1gI+PQLhspGu3oJN;)BDr!?IK*I8r4@;bziB-m6bD7PgbVHL|oYqDg+G@pZU
T8>2KF^ZZ@O6yZWaSN2MfR^c;U

literal 0
HcmV?d00001

diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.od b/ld/testsuite/ld-x86-64/pe-x86-64-5.od
new file mode 100644
index 00000000000..8a4f4a633ac
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pe-x86-64-5.od
@@ -0,0 +1,34 @@
+
+.*: +file format .*
+
+SYMBOL TABLE:
+0+402014 g       .bss	0000000000000000 non_initdummy
+0+402010 g       .data	0000000000000000 initdummy
+0+401000 g       .text\$mn	0000000000000000 begin
+0+402012 g       .bss	0000000000000000 __bss_start
+0+402000 g       .data	0000000000000000 Struct
+0+402011 g       .data	0000000000000000 initializedVar
+0+402012 g       .data	0000000000000000 _edata
+0+402018 g       .bss	0000000000000000 _end
+0+402015 g       .bss	0000000000000000 non_initialVar
+
+
+
+Disassembly of section .text\$mn:
+
+0+401000 <begin>:
+ +[a-f0-9]+:	66 90                	xchg   %ax,%ax
+ +[a-f0-9]+:	cc                   	int3   
+ +[a-f0-9]+:	48 8d 05 07 10 00 00 	lea    0x1007\(%rip\),%rax        # 402011 <initializedVar>
+ +[a-f0-9]+:	48 3b 05 ef 0f 00 00 	cmp    0xfef\(%rip\),%rax        # 402000 <Struct>
+ +[a-f0-9]+:	74 01                	je     401014 <begin\+0x14>
+ +[a-f0-9]+:	cc                   	int3   
+ +[a-f0-9]+:	48 8d 05 fa 0f 00 00 	lea    0xffa\(%rip\),%rax        # 402015 <non_initialVar>
+ +[a-f0-9]+:	48 3b 05 e6 0f 00 00 	cmp    0xfe6\(%rip\),%rax        # 402008 <Struct\+0x8>
+ +[a-f0-9]+:	74 01                	je     401025 <begin\+0x25>
+ +[a-f0-9]+:	cc                   	int3   
+ +[a-f0-9]+:	66 ba 80 00          	mov    \$0x80,%dx
+ +[a-f0-9]+:	b0 12                	mov    \$0x12,%al
+ +[a-f0-9]+:	ee                   	out    %al,\(%dx\)
+ +[a-f0-9]+:	c3                   	ret    
+#pass
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64-5.rd b/ld/testsuite/ld-x86-64/pe-x86-64-5.rd
new file mode 100644
index 00000000000..8370665f99f
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pe-x86-64-5.rd
@@ -0,0 +1,19 @@
+
+Symbol table '.symtab' contains 10 entries:
+   Num:    Value          Size Type    Bind   Vis      Ndx Name
+ +[a-f0-9]+: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
+ +[a-f0-9]+: 0000000000402014     0 NOTYPE  GLOBAL DEFAULT    3 non_initdummy
+ +[a-f0-9]+: 0000000000402010     0 NOTYPE  GLOBAL DEFAULT    2 initdummy
+ +[a-f0-9]+: 0000000000401000     0 NOTYPE  GLOBAL DEFAULT    1 begin
+ +[a-f0-9]+: 0000000000402012     0 NOTYPE  GLOBAL DEFAULT    3 __bss_start
+ +[a-f0-9]+: 0000000000402000     0 NOTYPE  GLOBAL DEFAULT    2 Struct
+ +[a-f0-9]+: 0000000000402011     0 NOTYPE  GLOBAL DEFAULT    2 initializedVar
+ +[a-f0-9]+: 0000000000402012     0 NOTYPE  GLOBAL DEFAULT    2 _edata
+ +[a-f0-9]+: 0000000000402018     0 NOTYPE  GLOBAL DEFAULT    3 _end
+ +[a-f0-9]+: 0000000000402015     0 NOTYPE  GLOBAL DEFAULT    3 non_initialVar
+
+Hex dump of section '.data':
+  0x00402000 11204000 00000000 15204000 00000000 . @...... @.....
+  0x00402010 aa55                                .U
+
+#pass
diff --git a/ld/testsuite/ld-x86-64/pe-x86-64.exp b/ld/testsuite/ld-x86-64/pe-x86-64.exp
index e81a0b4ae08..ccfcdfaddfb 100644
--- a/ld/testsuite/ld-x86-64/pe-x86-64.exp
+++ b/ld/testsuite/ld-x86-64/pe-x86-64.exp
@@ -63,4 +63,14 @@ run_ld_link_tests [list \
 	{{objdump {-dw --sym} pe-x86-64-4.od}} \
 	"pe-x86-64-4" \
     ] \
+    [list \
+	"Build pe-x86-64-5" \
+	"-m elf_x86_64 --entry=begin" \
+	"" \
+	"" \
+	{pe-x86-64-5.obj.bz2 } \
+	{{objdump {-dw --sym} pe-x86-64-5.od} \
+	 {readelf {-s -x .data} pe-x86-64-5.rd}} \
+	"pe-x86-64-5" \
+    ] \
 ]
-- 
2.29.2



More information about the Binutils mailing list