[2/2] x86/COFF: support section-index relocations in insn operands

Message ID c55419eb-e3c0-4e9c-9d89-106cd1ab2729@suse.com
State New
Headers
Series x86/COFF: support further relocation types in insn operands |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Jan Beulich Nov. 19, 2024, 7:50 a.m. UTC
  On the grounds of the principle put down near the bottom of [1], along
with image and section relative operations, let's also support as insn
operands what .secidx is for on the data side (of course like elsewhere
the reloc operator can then also be used for data generation, albeit a
small tweak to x86_cons() is needed for this to work).

[1] https://sourceware.org/pipermail/binutils/2024-November/137617.html
  

Patch

--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -1410,6 +1410,9 @@  gotrel[] =
     { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
 				       BFD_RELOC_32_SECREL },
     OPERAND_TYPE_IMM32_32S_DISP32, false },
+    { STRING_COMMA_LEN ("SECIDX16"), { BFD_RELOC_16_SECIDX,
+				       BFD_RELOC_16_SECIDX },
+    { .bitfield = { .imm16 = 1, .disp16 = 1 } }, false },
     { STRING_COMMA_LEN ("RVA"), { BFD_RELOC_RVA,
 				       BFD_RELOC_RVA },
     OPERAND_TYPE_IMM32_32S_DISP32, false },
@@ -13129,7 +13132,11 @@  x86_cons (expressionS *exp, int size)
   expr_mode = expr_operator_none;
 
 #if defined (OBJ_ELF) || defined (TE_PE)
-  if (size == 4 || (object_64bit && size == 8))
+  if (size == 4
+# ifdef TE_PE
+      || (size == 2)
+# endif
+      || (object_64bit && size == 8))
     {
       /* Handle @GOTOFF and the like in an expression.  */
       char *save;
--- a/gas/testsuite/gas/i386/secidx.d
+++ b/gas/testsuite/gas/i386/secidx.d
@@ -3,6 +3,10 @@ 
 
 .*: +file format pe-i386
 
+RELOCATION RECORDS FOR \[\.text\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+22 secidx            \.text
+
 RELOCATION RECORDS FOR \[\.data\]:
 OFFSET[ 	]+TYPE[ 	]+VALUE
 0+24 secidx            \.text
@@ -22,9 +26,14 @@  OFFSET[ 	]+TYPE[ 	]+VALUE
 0+72 secidx            ext36
 0+75 secidx            ext3f
 
+RELOCATION RECORDS FOR \[\.rdata\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+24 secidx            \.rdata
+
 Contents of section \.text:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
+ 0020 66b80000 .*
 Contents of section \.data:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
@@ -37,4 +46,4 @@  Contents of section \.data:
 Contents of section \.rdata:
  0000 3e3e3e3e 3c3c3c3c 3e3e3e3e 3e3c3c3c  >>>><<<<>>>>><<<
  0010 3e3e3e3e 3e3e3c3c 3e3e3e3e 3e3e3e3c  >>>>>><<>>>>>>><
- 0020 3e3e3e3e 00000000 00000000 00000000  >>>>............
+ 0020 3e3e3e3e 24003c3c 3c3c0000 00000000  >>>>..<<<<......
--- a/gas/testsuite/gas/i386/secidx.s
+++ b/gas/testsuite/gas/i386/secidx.s
@@ -9,6 +9,8 @@  pre16:	.ascii "<<"
 	.ascii ">>>>>>>"
 pre1f:	.ascii "<"
 
+	mov	$.text@secidx16, %ax
+
 .data
 
 	.ascii ">>>>"
@@ -76,4 +78,8 @@  nex16:	.ascii "<<"
 nex1f:	.ascii "<"
 	.ascii ">>>>"
 
+	.word	.@secidx16
+
+	.ascii "<<<<"
+
 	.p2align 4,0
--- a/gas/testsuite/gas/i386/x86-64.exp
+++ b/gas/testsuite/gas/i386/x86-64.exp
@@ -60,6 +60,7 @@  if { ![istarget "*-*-*cygwin*"]
 } else {
     run_dump_test "x86-64-w64-pcrel"
     run_dump_test "x86-64-imgrel"
+    run_dump_test "x86-64-secidx"
 }
 run_list_test "pcrel64" "-al"
 run_dump_test "x86-64-rip"
--- /dev/null
+++ b/gas/testsuite/gas/i386/x86-64-secidx.d
@@ -0,0 +1,25 @@ 
+#objdump: -rs
+#name: x86-64 imgrel (RVA) reloc
+
+.*: +file format pe-x86-64
+
+RELOCATION RECORDS FOR \[\.text\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+02 IMAGE_REL_AMD64_SECTION  \.text
+0+07 IMAGE_REL_AMD64_SECTION  \.data
+0+0c IMAGE_REL_AMD64_SECTION  \.text
+0+11 IMAGE_REL_AMD64_SECTION  Xtrn
+
+RELOCATION RECORDS FOR \[\.data\]:
+OFFSET[ 	]+TYPE[ 	]+VALUE
+0+00 IMAGE_REL_AMD64_SECTION  \.text
+0+02 IMAGE_REL_AMD64_SECTION  \.data
+0+04 IMAGE_REL_AMD64_SECTION  \.data
+0+06 IMAGE_REL_AMD64_SECTION  Xtrn
+
+Contents of section \.text:
+ 0000 66b90000 6681c100 006681e9 09006681  .*
+ 0010 f10000.*
+
+Contents of section \.data:
+ 0000 00000000 04000000 00000000 00000000  .*
--- /dev/null
+++ b/gas/testsuite/gas/i386/x86-64-secidx.s
@@ -0,0 +1,13 @@ 
+	.text
+Text:
+	mov	$Text@secidx16, %cx
+	add	$Data@secidx16, %cx
+	sub	$.@secidx16, %cx
+	xor	$Xtrn@secidx16, %cx
+
+	.data
+Data:
+	.word	Text@secidx16
+	.word	Data@secidx16
+	.word	.@secidx16
+	.word	Xtrn@secidx16