Skip to content

Commit f21c35d

Browse files
committed
[ELF] Replace some Fatal with Err
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no output even if the process exits with code 1. Change a few Fatal to recoverable Err.
1 parent b7195e8 commit f21c35d

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

lld/ELF/Driver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,8 +2425,10 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
24252425
unsigned size;
24262426
const char *err = nullptr;
24272427
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
2428-
if (err)
2429-
Fatal(ctx) << f << ": could not decode addrsig section: " << err;
2428+
if (err) {
2429+
Err(ctx) << f << ": could not decode addrsig section: " << err;
2430+
break;
2431+
}
24302432
markAddrsig(icfSafe, syms[symIndex]);
24312433
cur += size;
24322434
}

lld/ELF/InputFiles.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -921,17 +921,18 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
921921
using Elf_Note = typename ELFT::Note;
922922

923923
ArrayRef<uint8_t> data = sec.content();
924-
auto reportFatal = [&](const uint8_t *place, const Twine &msg) {
925-
Fatal(ctx) << sec.file << ":(" << sec.name << "+0x"
926-
<< Twine::utohexstr(place - sec.content().data())
927-
<< "): " << msg;
924+
auto err = [&](const uint8_t *place) -> ELFSyncStream {
925+
auto diag = Err(ctx);
926+
diag << sec.file << ":(" << sec.name << "+0x"
927+
<< Twine::utohexstr(place - sec.content().data()) << "): ";
928+
return diag;
928929
};
929930
while (!data.empty()) {
930931
// Read one NOTE record.
931932
auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
932933
if (data.size() < sizeof(Elf_Nhdr) ||
933934
data.size() < nhdr->getSize(sec.addralign))
934-
reportFatal(data.data(), "data is too short");
935+
return void(err(data.data()) << "data is too short");
935936

936937
Elf_Note note(*nhdr);
937938
if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
@@ -948,30 +949,32 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
948949
while (!desc.empty()) {
949950
const uint8_t *place = desc.data();
950951
if (desc.size() < 8)
951-
reportFatal(place, "program property is too short");
952+
return void(err(place) << "program property is too short");
952953
uint32_t type = read32<ELFT::Endianness>(desc.data());
953954
uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
954955
desc = desc.slice(8);
955956
if (desc.size() < size)
956-
reportFatal(place, "program property is too short");
957+
return void(err(place) << "program property is too short");
957958

958959
if (type == featureAndType) {
959960
// We found a FEATURE_1_AND field. There may be more than one of these
960961
// in a .note.gnu.property section, for a relocatable object we
961962
// accumulate the bits set.
962963
if (size < 4)
963-
reportFatal(place, "FEATURE_1_AND entry is too short");
964+
return void(err(place) << "FEATURE_1_AND entry is too short");
964965
f.andFeatures |= read32<ELFT::Endianness>(desc.data());
965966
} else if (ctx.arg.emachine == EM_AARCH64 &&
966967
type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
967968
if (!f.aarch64PauthAbiCoreInfo.empty()) {
968-
reportFatal(data.data(),
969-
"multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
970-
"not supported");
969+
return void(
970+
err(data.data())
971+
<< "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
972+
"not supported");
971973
} else if (size != 16) {
972-
reportFatal(data.data(), "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
973-
"is invalid: expected 16 bytes, but got " +
974-
Twine(size));
974+
return void(err(data.data())
975+
<< "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
976+
"is invalid: expected 16 bytes, but got "
977+
<< size);
975978
}
976979
f.aarch64PauthAbiCoreInfo = desc;
977980
}
@@ -1173,8 +1176,10 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
11731176
secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
11741177
else if (secIdx >= SHN_LORESERVE)
11751178
secIdx = 0;
1176-
if (LLVM_UNLIKELY(secIdx >= sections.size()))
1177-
Fatal(ctx) << this << ": invalid section index: " << secIdx;
1179+
if (LLVM_UNLIKELY(secIdx >= sections.size())) {
1180+
Err(ctx) << this << ": invalid section index: " << secIdx;
1181+
secIdx = 0;
1182+
}
11781183
if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
11791184
ErrAlways(ctx) << this << ": non-local symbol (" << i
11801185
<< ") found at index < .symtab's sh_info (" << end << ")";
@@ -1183,9 +1188,12 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
11831188
uint8_t type = eSym.getType();
11841189
if (type == STT_FILE)
11851190
sourceFile = CHECK2(eSym.getName(stringTable), this);
1186-
if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name))
1187-
Fatal(ctx) << this << ": invalid symbol name offset";
1188-
StringRef name(stringTable.data() + eSym.st_name);
1191+
unsigned stName = eSym.st_name;
1192+
if (LLVM_UNLIKELY(stringTable.size() <= stName)) {
1193+
Err(ctx) << this << ": invalid symbol name offset";
1194+
stName = 0;
1195+
}
1196+
StringRef name(stringTable.data() + stName);
11891197

11901198
symbols[i] = reinterpret_cast<Symbol *>(locals + i);
11911199
if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)
@@ -1236,8 +1244,10 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
12361244
secIdx = 0;
12371245
}
12381246

1239-
if (LLVM_UNLIKELY(secIdx >= sections.size()))
1240-
Fatal(ctx) << this << ": invalid section index: " << secIdx;
1247+
if (LLVM_UNLIKELY(secIdx >= sections.size())) {
1248+
Err(ctx) << this << ": invalid section index: " << secIdx;
1249+
continue;
1250+
}
12411251
InputSectionBase *sec = sections[secIdx];
12421252
if (sec == &InputSection::discarded) {
12431253
if (sym.traced) {

lld/ELF/Relocations.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,10 @@ class OffsetGetter {
428428
if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) {
429429
while (i != cies.end() && i->inputOff <= off)
430430
++i;
431-
if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off)
432-
Fatal(ctx) << ".eh_frame: relocation is not in any piece";
431+
if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) {
432+
Err(ctx) << ".eh_frame: relocation is not in any piece";
433+
return 0;
434+
}
433435
it = i;
434436
}
435437

lld/test/ELF/invalid/section-index.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# RUN: yaml2obj %s -o %t1.o
55
# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s
6+
# RUN: ld.lld %t1.o -o /dev/null --noinhibit-exec 2>&1 | FileCheck %s
67
# CHECK: {{.*}}1.o: invalid section index: 256
78

89
!ELF

lld/test/ELF/invalid/symbol-name.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: yaml2obj %s -o %t.o
22
# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
3+
# RUN: ld.lld %t.o -o /dev/null --noinhibit-exec
34
# CHECK: error: {{.*}}.o: invalid symbol name offset
45

56
## YAML below contains symbol with name offset in st_name

0 commit comments

Comments
 (0)