[PATCH] D39242: [ELF] - Stop naming relocation sections with first input section name.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 06:58:58 PDT 2017


grimar created this revision.
Herald added a subscriber: emaste.

It was reported (https://reviews.llvm.org/D38724#902841) that when we use 
-ffunction-sections --emit-relocs build, REL[A] output section receives the name of first
input section, like .rela.text.first_function_in_text rather than .rela.text.

It is probably not really an issue as sh_info still points to correct target section, but
it does not look clean in output and allows internal section name to leak there,
what at least looks confusing and is not consistent with ld.bfd.

Patch changes this behavior so that target output section name is used as a base.


https://reviews.llvm.org/D39242

Files:
  ELF/Writer.cpp
  test/ELF/emit-relocs-merge.s
  test/ELF/emit-relocs.s


Index: test/ELF/emit-relocs.s
===================================================================
--- test/ELF/emit-relocs.s
+++ test/ELF/emit-relocs.s
@@ -83,7 +83,7 @@
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
-.section .text,"ax", at progbits,unique,0
+.section .text.fn,"ax", at progbits,unique,0
 .globl fn
 .type fn, at function
 fn:
@@ -94,7 +94,7 @@
   callq fn at PLT
   nop
 
-.section .text,"ax", at progbits,unique,1
+.section .text.fn2,"ax", at progbits,unique,1
 .globl fn2
 .type fn2, at function
 fn2:
Index: test/ELF/emit-relocs-merge.s
===================================================================
--- test/ELF/emit-relocs-merge.s
+++ test/ELF/emit-relocs-merge.s
@@ -8,7 +8,7 @@
 # CHECK-NEXT:     0x1000 R_X86_64_64 zed 0x0
 # CHECK-NEXT:     0x1008 R_X86_64_64 zed 0x0
 # CHECK-NEXT:   }
-# CHECK-NEXT:   Section ({{.*}}) .rela.data.foo {
+# CHECK-NEXT:   Section ({{.*}}) .rela.data {
 # CHECK-NEXT:     0x1000 R_X86_64_64 zed 0x0
 # CHECK-NEXT:     0x1008 R_X86_64_64 zed 0x0
 # CHECK-NEXT:   }
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -98,6 +98,17 @@
   if (Config->Relocatable)
     return Name;
 
+  // This is normally used for --emit-relocs. With that we want
+  // to name relocation section similar to it's target section.
+  // That makes output cleaner and consistent with GNU linkers.
+  for (StringRef V : {".rel.", ".rela."}) {
+    if (!Name.startswith(V))
+      continue;
+    StringRef Target = Name.drop_front(V.size() - 1);
+    size_t Size = V.size() + getOutputSectionName(Target).size() - 1;
+    return Name.take_front(Size);
+  }
+
   for (StringRef V :
        {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
         ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39242.120071.patch
Type: text/x-patch
Size: 1827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171024/46878570/attachment.bin>


More information about the llvm-commits mailing list