[PATCH] D13728: ELF2: Merge .{text,rodata,data,bss}.* sections.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 14 08:05:28 PDT 2015


ruiu created this revision.
ruiu added a reviewer: rafael.
ruiu added a subscriber: llvm-commits.

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 is 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 just
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, and it's now
only 27.

http://reviews.llvm.org/D13728

Files:
  ELF/Writer.cpp
  test/elf2/section-name.s

Index: test/elf2/section-name.s
===================================================================
--- /dev/null
+++ test/elf2/section-name.s
@@ -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:     Name: .rodata
+// CHECK-NOT: Name: .rodata.a
+// CHECK:     Name: .text
+// CHECK-NOT: Name: .text.a
+// CHECK:     Name: .data
+// CHECK-NOT: Name: .data.a
+// CHECK:     Name: .foo
+// CHECK:     Name: .foo
+// CHECK:     Name: .bss
+// CHECK-NOT: Name: .bss.a
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -367,6 +367,20 @@
   Out<ELFT>::Bss->setSize(Off);
 }
 
+template <class ELFT>
+static StringRef getOutputSection(InputSection<ELFT> *Sec) {
+  StringRef S = Sec->getSectionName();
+  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.
@@ -401,7 +415,7 @@
         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{getOutputSection(C), H->sh_type, OutFlags};
       OutputSection<ELFT> *&Sec = Map[Key];
       if (!Sec) {
         Sec = new (CAlloc.Allocate())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13728.37357.patch
Type: text/x-patch
Size: 2008 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151014/53813aef/attachment.bin>


More information about the llvm-commits mailing list