From: H.J. Lu Date: Tue, 12 Jan 2021 13:10:58 +0000 (-0800) Subject: elf/x86-64: Adjust R_AMD64_DIR64/R_AMD64_DIR32 for PE/x86-64 inputs X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c2e9a4a3ed1efcbdec68372e7e889470870d6d48;p=binutils-gdb.git elf/x86-64: Adjust R_AMD64_DIR64/R_AMD64_DIR32 for PE/x86-64 inputs 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. --- diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 38123b44407..eaeb47d1662 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2021-01-12 H.J. Lu + + PR ld/27171 + * reloc.c (bfd_perform_relocation): Adjust R_AMD64_DIR64 and + R_AMD64_DIR32 relocations for PE/x86-64 inputs. + 2021-01-11 H.J. Lu PR ld/27173 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/ChangeLog b/ld/ChangeLog index bedd0fa326f..a4b0ea6e56b 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,11 @@ +2021-01-12 H.J. Lu + + 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. + 2021-01-11 H.J. Lu PR ld/27173 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 00000000000..bb0d38ce7f3 Binary files /dev/null and b/ld/testsuite/ld-x86-64/pe-x86-64-5.obj.bz2 differ 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 : + +[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 + +[a-f0-9]+: 48 3b 05 ef 0f 00 00 cmp 0xfef\(%rip\),%rax # 402000 + +[a-f0-9]+: 74 01 je 401014 + +[a-f0-9]+: cc int3 + +[a-f0-9]+: 48 8d 05 fa 0f 00 00 lea 0xffa\(%rip\),%rax # 402015 + +[a-f0-9]+: 48 3b 05 e6 0f 00 00 cmp 0xfe6\(%rip\),%rax # 402008 + +[a-f0-9]+: 74 01 je 401025 + +[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" \ + ] \ ]