[lld] r250315 - ELF2: Merge .{text,rodata,data,bss}.* sections.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 14 12:21:25 PDT 2015


Author: ruiu
Date: Wed Oct 14 14:21:25 2015
New Revision: 250315

URL: http://llvm.org/viewvc/llvm-project?rev=250315&view=rev
Log:
ELF2: Merge .{text,rodata,data,bss}.* sections.

Previously, we used input section names as output section names.
That resulted that we created lots of sections for comdat
or -f{function,data}-section sections.

This patch reduces the number of sections by dropping suffix from
all section names which start with ".text.", ".rodata.", ".data."
or ".bss.". GNU linker does this using the internal linker script,
but for LLD I chose to do that directly.

Interestingly, this makes the linker faster. Time to link Clang
is this.

Before:

  real    0m0.537s
  user    0m0.433s
  sys     0m0.104s

After:

  real    0m0.390s
  user    0m0.268s
  sys     0m0.120s

It make sense because previously we created 57659 sections now only 27.

Added:
    lld/trunk/test/elf2/section-name.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=250315&r1=250314&r2=250315&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Oct 14 14:21:25 2015
@@ -369,6 +369,18 @@ static void addCommonSymbols(std::vector
   Out<ELFT>::Bss->setSize(Off);
 }
 
+static StringRef getOutputName(StringRef S) {
+  if (S.startswith(".text."))
+    return ".text";
+  if (S.startswith(".rodata."))
+    return ".rodata";
+  if (S.startswith(".data."))
+    return ".data";
+  if (S.startswith(".bss."))
+    return ".bss";
+  return S;
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
   // .interp needs to be on the first page in the output file.
@@ -403,7 +415,8 @@ template <class ELFT> void Writer<ELFT>:
         continue;
       const Elf_Shdr *H = C->getSectionHdr();
       uintX_t OutFlags = H->sh_flags & ~SHF_GROUP;
-      SectionKey<ELFT::Is64Bits> Key{C->getSectionName(), H->sh_type, OutFlags};
+      SectionKey<ELFT::Is64Bits> Key{getOutputName(C->getSectionName()),
+                                     H->sh_type, OutFlags};
       OutputSection<ELFT> *&Sec = Map[Key];
       if (!Sec) {
         Sec = new (CAlloc.Allocate())

Added: lld/trunk/test/elf2/section-name.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/section-name.s?rev=250315&view=auto
==============================================================================
--- lld/trunk/test/elf2/section-name.s (added)
+++ lld/trunk/test/elf2/section-name.s Wed Oct 14 14:21:25 2015
@@ -0,0 +1,30 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld2 %t -o %tout
+# RUN: llvm-readobj -sections %tout | FileCheck %s
+# REQUIRES: x86
+
+.global _start
+.text
+_start:
+
+.section .text.a,"ax"
+.section .text.,"ax"
+.section .rodata.a,"a"
+.section .rodata,"a"
+.section .data.a,"aw"
+.section .data,"aw"
+.section .bss.a,"", at nobits
+.section .bss,"", at nobits
+.section .foo.a,"aw"
+.section .foo,"aw"
+
+// CHECK-NOT: Name: .rodata.a
+// CHECK:     Name: .rodata
+// CHECK-NOT: Name: .text.a
+// CHECK:     Name: .text
+// CHECK-NOT: Name: .data.a
+// CHECK:     Name: .data
+// CHECK:     Name: .foo.a
+// CHECK:     Name: .foo
+// CHECK-NOT: Name: .bss.a
+// CHECK:     Name: .bss




More information about the llvm-commits mailing list