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.

llvm-svn: 250315
This commit is contained in:
Rui Ueyama 2015-10-14 19:21:25 +00:00
parent a7f8f25264
commit 4fcadaf5e7
2 changed files with 44 additions and 1 deletions

View File

@ -369,6 +369,18 @@ static void addCommonSymbols(std::vector<DefinedCommon<ELFT> *> &Syms) {
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>::createSections() {
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())

View File

@ -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,"",@nobits
.section .bss,"",@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