[lld] 213d184 - [ELF] Improve sh_info=0 and sh_info>=num_sections diagnostic for SHT_REL/SHT_RELA
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 9 09:54:17 PST 2021
Author: Fangrui Song
Date: 2021-11-09T09:54:12-08:00
New Revision: 213d1849a4b9f5579c964086097391907df37a04
URL: https://github.com/llvm/llvm-project/commit/213d1849a4b9f5579c964086097391907df37a04
DIFF: https://github.com/llvm/llvm-project/commit/213d1849a4b9f5579c964086097391907df37a04.diff
LOG: [ELF] Improve sh_info=0 and sh_info>=num_sections diagnostic for SHT_REL/SHT_RELA
PR52408 reported an sh_info=0 instance. I have seen sh_info=0
independently before.
sh_info>=num_sections is probably very rare. Just use one diagnostic for
the two types of errors.
Delete invalid-relocations.test which is covered by invalid/bad-reloc-target.test
Differential Revision: https://reviews.llvm.org/D113466
Added:
Modified:
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h
lld/test/ELF/invalid/bad-reloc-target.test
Removed:
lld/test/ELF/invalid-relocations.test
################################################################################
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index b458b3a2a500..e8a4188ec775 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -837,21 +837,25 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
}
template <class ELFT>
-InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &sec) {
- uint32_t idx = sec.sh_info;
- if (idx >= this->sections.size())
- fatal(toString(this) + ": invalid relocated section index: " + Twine(idx));
- InputSectionBase *target = this->sections[idx];
-
- // Strictly speaking, a relocation section must be included in the
- // group of the section it relocates. However, LLVM 3.3 and earlier
- // would fail to do so, so we gracefully handle that case.
- if (target == &InputSection::discarded)
- return nullptr;
-
- if (!target)
- fatal(toString(this) + ": unsupported relocation reference");
- return target;
+InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, StringRef name,
+ const Elf_Shdr &sec) {
+ uint32_t info = sec.sh_info;
+ if (info < this->sections.size()) {
+ InputSectionBase *target = this->sections[info];
+
+ // Strictly speaking, a relocation section must be included in the
+ // group of the section it relocates. However, LLVM 3.3 and earlier
+ // would fail to do so, so we gracefully handle that case.
+ if (target == &InputSection::discarded)
+ return nullptr;
+
+ if (target != nullptr)
+ return target;
+ }
+
+ error(toString(this) + Twine(": relocation section ") + name + " (index " +
+ Twine(idx) + ") has invalid sh_info (" + Twine(info) + ")");
+ return nullptr;
}
// Create a regular InputSection class that has the same contents
@@ -939,7 +943,7 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
// and the group is discarded, even though it's a violation of the
// spec. We handle that situation gracefully by discarding dangling
// relocation sections.
- InputSectionBase *target = getRelocTarget(sec);
+ InputSectionBase *target = getRelocTarget(idx, name, sec);
if (!target)
return nullptr;
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index e9566a2dda6a..fb4d46b43f35 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -259,7 +259,8 @@ template <class ELFT> class ObjFile : public ELFFileBase {
void initializeSymbols();
void initializeJustSymbols();
- InputSectionBase *getRelocTarget(const Elf_Shdr &sec);
+ InputSectionBase *getRelocTarget(uint32_t idx, StringRef name,
+ const Elf_Shdr &sec);
InputSectionBase *createInputSection(uint32_t idx, const Elf_Shdr &sec,
StringRef shstrtab);
diff --git a/lld/test/ELF/invalid-relocations.test b/lld/test/ELF/invalid-relocations.test
deleted file mode 100644
index 53a7981262f1..000000000000
--- a/lld/test/ELF/invalid-relocations.test
+++ /dev/null
@@ -1,23 +0,0 @@
-# RUN: yaml2obj %s -o %t
-# RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
-
-!ELF
-FileHeader:
- Class: ELFCLASS64
- Data: ELFDATA2LSB
- Type: ET_REL
- Machine: EM_X86_64
-Sections:
- - Type: SHT_PROGBITS
- - Name: .rela.text
- Type: SHT_RELA
- Info: 12 # Invalid index
- Relocations:
- - Offset: 0x0000000000000001
- Symbol: lulz
- Type: R_X86_64_PC32
-Symbols:
- - Name: lulz
- Binding: STB_GLOBAL
-
-# CHECK: invalid relocated section index
diff --git a/lld/test/ELF/invalid/bad-reloc-target.test b/lld/test/ELF/invalid/bad-reloc-target.test
index e0948a2c4c76..6177910d881e 100644
--- a/lld/test/ELF/invalid/bad-reloc-target.test
+++ b/lld/test/ELF/invalid/bad-reloc-target.test
@@ -1,6 +1,6 @@
# RUN: yaml2obj --docnum=1 %s -o %t1.o
# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s
-# CHECK: error: {{.*}}.o: unsupported relocation reference
+# CHECK: error: {{.*}}.o: relocation section .rela.text (index 2) has invalid sh_info (0)
--- !ELF
FileHeader:
@@ -25,7 +25,7 @@ Symbols:
# RUN: yaml2obj --docnum=2 %s -o %t2.o
# RUN: not ld.lld %t2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR2
-# ERR2: error: {{.*}}.o: invalid relocated section index: 99
+# ERR2: error: {{.*}}.o: relocation section .rela.text (index 2) has invalid sh_info (99)
--- !ELF
FileHeader:
More information about the llvm-commits
mailing list