Skip to content

Commit 213d184

Browse files
committed
[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
1 parent 384b4e0 commit 213d184

File tree

4 files changed

+24
-42
lines changed

4 files changed

+24
-42
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -837,21 +837,25 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
837837
}
838838

839839
template <class ELFT>
840-
InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &sec) {
841-
uint32_t idx = sec.sh_info;
842-
if (idx >= this->sections.size())
843-
fatal(toString(this) + ": invalid relocated section index: " + Twine(idx));
844-
InputSectionBase *target = this->sections[idx];
845-
846-
// Strictly speaking, a relocation section must be included in the
847-
// group of the section it relocates. However, LLVM 3.3 and earlier
848-
// would fail to do so, so we gracefully handle that case.
849-
if (target == &InputSection::discarded)
850-
return nullptr;
851-
852-
if (!target)
853-
fatal(toString(this) + ": unsupported relocation reference");
854-
return target;
840+
InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, StringRef name,
841+
const Elf_Shdr &sec) {
842+
uint32_t info = sec.sh_info;
843+
if (info < this->sections.size()) {
844+
InputSectionBase *target = this->sections[info];
845+
846+
// Strictly speaking, a relocation section must be included in the
847+
// group of the section it relocates. However, LLVM 3.3 and earlier
848+
// would fail to do so, so we gracefully handle that case.
849+
if (target == &InputSection::discarded)
850+
return nullptr;
851+
852+
if (target != nullptr)
853+
return target;
854+
}
855+
856+
error(toString(this) + Twine(": relocation section ") + name + " (index " +
857+
Twine(idx) + ") has invalid sh_info (" + Twine(info) + ")");
858+
return nullptr;
855859
}
856860

857861
// Create a regular InputSection class that has the same contents
@@ -939,7 +943,7 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
939943
// and the group is discarded, even though it's a violation of the
940944
// spec. We handle that situation gracefully by discarding dangling
941945
// relocation sections.
942-
InputSectionBase *target = getRelocTarget(sec);
946+
InputSectionBase *target = getRelocTarget(idx, name, sec);
943947
if (!target)
944948
return nullptr;
945949

lld/ELF/InputFiles.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ template <class ELFT> class ObjFile : public ELFFileBase {
259259
void initializeSymbols();
260260
void initializeJustSymbols();
261261

262-
InputSectionBase *getRelocTarget(const Elf_Shdr &sec);
262+
InputSectionBase *getRelocTarget(uint32_t idx, StringRef name,
263+
const Elf_Shdr &sec);
263264
InputSectionBase *createInputSection(uint32_t idx, const Elf_Shdr &sec,
264265
StringRef shstrtab);
265266

lld/test/ELF/invalid-relocations.test

Lines changed: 0 additions & 23 deletions
This file was deleted.

lld/test/ELF/invalid/bad-reloc-target.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RUN: yaml2obj --docnum=1 %s -o %t1.o
22
# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s
3-
# CHECK: error: {{.*}}.o: unsupported relocation reference
3+
# CHECK: error: {{.*}}.o: relocation section .rela.text (index 2) has invalid sh_info (0)
44

55
--- !ELF
66
FileHeader:
@@ -25,7 +25,7 @@ Symbols:
2525

2626
# RUN: yaml2obj --docnum=2 %s -o %t2.o
2727
# RUN: not ld.lld %t2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR2
28-
# ERR2: error: {{.*}}.o: invalid relocated section index: 99
28+
# ERR2: error: {{.*}}.o: relocation section .rela.text (index 2) has invalid sh_info (99)
2929

3030
--- !ELF
3131
FileHeader:

0 commit comments

Comments
 (0)