[lld] r328640 - Force SHF_MERGE optimizations with -r.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 27 10:09:23 PDT 2018
Author: rafael
Date: Tue Mar 27 10:09:23 2018
New Revision: 328640
URL: http://llvm.org/viewvc/llvm-project?rev=328640&view=rev
Log:
Force SHF_MERGE optimizations with -r.
Some tools (dwarfdump for example) get confused by the current -O0 -r
output since it has multiple copies of .debug_str.
We cannot just merge sections with the same name as they can have
different sh_entsize.
We could have duplicated logic for merging sections based on name and
sh_entsize, but it seems better to just use the existing logic by
enabling optimizations.
Added:
lld/trunk/test/ELF/merge-reloc-O0.s
Modified:
lld/trunk/ELF/InputFiles.cpp
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=328640&r1=328639&r2=328640&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Mar 27 10:09:23 2018
@@ -312,9 +312,19 @@ ObjFile<ELFT>::getShtGroupEntries(const
}
template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
- // We don't merge sections if -O0 (default is -O1). This makes sometimes
- // the linker significantly faster, although the output will be bigger.
- if (Config->Optimize == 0)
+ // On a regular link we don't merge sections if -O0 (default is -O1). This
+ // sometimes makes the linker significantly faster, although the output will
+ // be bigger.
+ //
+ // Doing the same for -r would create a problem as it would combine sections
+ // with different sh_entsize. One option would be to just copy every SHF_MERGE
+ // section as is to the output. While this would produce a valid ELF file with
+ // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
+ // they see two .debug_str. We could have separate logic for combining
+ // SHF_MERGE sections based both on their name and sh_entsize, but that seems
+ // to be more trouble than it is worth. Instead, we just use the regular (-O1)
+ // logic for -r.
+ if (Config->Optimize == 0 && !Config->Relocatable)
return false;
// A mergeable section with size 0 is useless because they don't have
Added: lld/trunk/test/ELF/merge-reloc-O0.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/merge-reloc-O0.s?rev=328640&view=auto
==============================================================================
--- lld/trunk/test/ELF/merge-reloc-O0.s (added)
+++ lld/trunk/test/ELF/merge-reloc-O0.s Tue Mar 27 10:09:23 2018
@@ -0,0 +1,48 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld %t.o -r -o %t2.o -O0
+# RUN: llvm-readobj -s -section-data %t2.o | FileCheck %s
+
+# We combine just the sections with the same name and sh_entsize.
+
+# CHECK: Name: .foo
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: SHF_MERGE
+# CHECK-NEXT: ]
+# CHECK-NEXT: Address:
+# CHECK-NEXT: Offset:
+# CHECK-NEXT: Size: 16
+# CHECK-NEXT: Link:
+# CHECK-NEXT: Info:
+# CHECK-NEXT: AddressAlignment: 8
+# CHECK-NEXT: EntrySize: 8
+# CHECK-NEXT: SectionData (
+# CHECK-NEXT: 0000: 41000000 00000000 42000000 00000000
+# CHECK-NEXT: )
+
+# CHECK: Name: .foo
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: SHF_MERGE
+# CHECK-NEXT: ]
+# CHECK-NEXT: Address:
+# CHECK-NEXT: Offset:
+# CHECK-NEXT: Size: 8
+# CHECK-NEXT: Link:
+# CHECK-NEXT: Info:
+# CHECK-NEXT: AddressAlignment: 4
+# CHECK-NEXT: EntrySize: 4
+# CHECK-NEXT: SectionData (
+# CHECK-NEXT: 0000: 41000000 42000000
+# CHECK-NEXT: )
+
+ .section .foo, "aM", at progbits,8,unique,0
+ .quad 0x41
+ .section .foo, "aM", at progbits,8,unique,1
+ .quad 0x42
+ .section .foo, "aM", at progbits,4,unique,2
+ .long 0x41
+ .long 0x42
More information about the llvm-commits
mailing list