[MC][X86] Support .reloc *, BFD_RELOC_{NONE,8,16,32,64}, *
authorFangrui Song <i@maskray.me>
Sat, 6 Mar 2021 05:01:32 +0000 (21:01 -0800)
committerFangrui Song <i@maskray.me>
Sat, 6 Mar 2021 05:31:05 +0000 (21:31 -0800)
The names are unfortunate, but BFD_RELOC_NONE provides a generic way indicating
a dependency between two sections, which is useful for ld --gc-sections.
See https://sourceware.org/bugzilla/show_bug.cgi?id=27530

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
llvm/test/MC/X86/reloc-directive-elf-32.s
llvm/test/MC/X86/reloc-directive-elf-64.s

index 73a77b8..628b8f5 100644 (file)
@@ -680,12 +680,21 @@ Optional<MCFixupKind> X86AsmBackend::getFixupKind(StringRef Name) const {
 #define ELF_RELOC(X, Y) .Case(#X, Y)
 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
 #undef ELF_RELOC
+                 .Case("BFD_RELOC_NONE", ELF::R_X86_64_NONE)
+                 .Case("BFD_RELOC_8", ELF::R_X86_64_8)
+                 .Case("BFD_RELOC_16", ELF::R_X86_64_16)
+                 .Case("BFD_RELOC_32", ELF::R_X86_64_32)
+                 .Case("BFD_RELOC_64", ELF::R_X86_64_64)
                  .Default(-1u);
     } else {
       Type = llvm::StringSwitch<unsigned>(Name)
 #define ELF_RELOC(X, Y) .Case(#X, Y)
 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
 #undef ELF_RELOC
+                 .Case("BFD_RELOC_NONE", ELF::R_386_NONE)
+                 .Case("BFD_RELOC_8", ELF::R_386_8)
+                 .Case("BFD_RELOC_16", ELF::R_386_16)
+                 .Case("BFD_RELOC_32", ELF::R_386_32)
                  .Default(-1u);
     }
     if (Type == -1u)
index ed7e6bb..b653e60 100644 (file)
 # PRINT-NEXT: .reloc 0, R_386_32, .data+2
 # PRINT-NEXT: .reloc 0, R_386_IRELATIVE, foo+3
 # PRINT-NEXT: .reloc 0, R_386_GOT32X, 5
+# PRINT-NEXT: .reloc 0, BFD_RELOC_NONE, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_8, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_16, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_32, 9
 
 # X86 relocations use the Elf32_Rel format. Addends are neither stored in the
 # relocation entries nor applied in the referenced locations.
 # CHECK-NEXT: 0x0 R_386_32 .data
 # CHECK-NEXT: 0x0 R_386_IRELATIVE foo
 # CHECK-NEXT: 0x0 R_386_GOT32X -
+# CHECK-NEXT: 0x0 R_386_NONE -
+# CHECK-NEXT: 0x0 R_386_8 -
+# CHECK-NEXT: 0x0 R_386_16 -
+# CHECK-NEXT: 0x0 R_386_32 -
 
 # HEX: 0x00000000 00000000 00000000
 
   .reloc 0, R_386_IRELATIVE, foo+3
   .reloc 0, R_386_GOT32X, 5
 
+  .reloc 0, BFD_RELOC_NONE, 9
+  .reloc 0, BFD_RELOC_8, 9
+  .reloc 0, BFD_RELOC_16, 9
+  .reloc 0, BFD_RELOC_32, 9
+
 .data
 .globl foo
 foo:
index 30a252f..0cfa060 100644 (file)
@@ -9,6 +9,11 @@
 # PRINT-NEXT: .reloc 0, R_X86_64_64, .data+2
 # PRINT-NEXT: .reloc 0, R_X86_64_GOTPCRELX, foo+3
 # PRINT-NEXT: .reloc 0, R_X86_64_REX_GOTPCRELX, 5
+# PRINT-NEXT: .reloc 0, BFD_RELOC_NONE, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_8, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_16, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_32, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_64, 9
 
 # CHECK:      0x2 R_X86_64_NONE .data 0x0
 # CHECK-NEXT: 0x1 R_X86_64_NONE foo 0x4
 # CHECK-NEXT: 0x0 R_X86_64_64 .data 0x2
 # CHECK-NEXT: 0x0 R_X86_64_GOTPCRELX foo 0x3
 # CHECK-NEXT: 0x0 R_X86_64_REX_GOTPCRELX - 0x5
+# CHECK-NEXT: 0x0 R_X86_64_NONE - 0x9
+# CHECK-NEXT: 0x0 R_X86_64_8 - 0x9
+# CHECK-NEXT: 0x0 R_X86_64_16 - 0x9
+# CHECK-NEXT: 0x0 R_X86_64_32 - 0x9
+# CHECK-NEXT: 0x0 R_X86_64_64 - 0x9
 
 .text
   ret
   .reloc 0, R_X86_64_GOTPCRELX, foo+3
   .reloc 0, R_X86_64_REX_GOTPCRELX, 5
 
+  .reloc 0, BFD_RELOC_NONE, 9
+  .reloc 0, BFD_RELOC_8, 9
+  .reloc 0, BFD_RELOC_16, 9
+  .reloc 0, BFD_RELOC_32, 9
+  .reloc 0, BFD_RELOC_64, 9
+
 .data
 .globl foo
 foo: