[lld] 56decd9 - [ELF] Allow invalid sh_size%sh_entsize!=0 for non-SHF_MERGE sections

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 08:48:41 PDT 2020


Author: Fangrui Song
Date: 2020-04-03T08:48:30-07:00
New Revision: 56decd982dc03a74d1796d9d4dbd7d9e0cea98dc

URL: https://github.com/llvm/llvm-project/commit/56decd982dc03a74d1796d9d4dbd7d9e0cea98dc
DIFF: https://github.com/llvm/llvm-project/commit/56decd982dc03a74d1796d9d4dbd7d9e0cea98dc.diff

LOG: [ELF] Allow invalid sh_size%sh_entsize!=0 for non-SHF_MERGE sections

Fixes https://bugs.llvm.org/show_bug.cgi?id=45370
Fixes https://github.com/Clozure/ccl/issues/273

.stab holds a table of 12-byte entries. GNU as before 2.35 incorrectly
sets sh_entsize(.stab) to 20 on 64-bit architectures:
https://sourceware.org/bugzilla/show_bug.cgi?id=25768

We should not emit the confusing error:
"SHF_MERGE section size (...) must be a multiple of sh_entsize (20)

Reviewed By: grimar, psmith

Differential Revision: https://reviews.llvm.org/D77368

Added: 
    lld/test/ELF/invalid/entsize.yaml

Modified: 
    lld/ELF/InputFiles.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 6f63a34e40c1..f3dd11fde3f6 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -424,6 +424,9 @@ StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
 
 template <class ELFT>
 bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
+  if (!(sec.sh_flags & SHF_MERGE))
+    return false;
+
   // 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.
@@ -459,10 +462,7 @@ bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
           Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
           Twine(entSize) + ")");
 
-  uint64_t flags = sec.sh_flags;
-  if (!(flags & SHF_MERGE))
-    return false;
-  if (flags & SHF_WRITE)
+  if (sec.sh_flags & SHF_WRITE)
     fatal(toString(this) + ":(" + name +
           "): writable SHF_MERGE section is not supported");
 

diff  --git a/lld/test/ELF/invalid/entsize.yaml b/lld/test/ELF/invalid/entsize.yaml
new file mode 100644
index 000000000000..a5fb7a90dca8
--- /dev/null
+++ b/lld/test/ELF/invalid/entsize.yaml
@@ -0,0 +1,17 @@
+## Ignore invalid sh_entsize (sh_size%sh_entsize!=0) for non-SHF_MERGE sections.
+## GNU as before 2.35 had such a bug for .stab
+## https://sourceware.org/bugzilla/show_bug.cgi?id=25768
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld -e 0 --fatal-warnings %t.o -o /dev/null
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name:    .stab
+    Type:    SHT_PROGBITS
+    EntSize: 20
+    Size:    24


        


More information about the llvm-commits mailing list