Skip to content

Commit 63c6fe4

Browse files
committed
[ELF] Replace fatal(...) with Fatal or Err
1 parent f7ef7b2 commit 63c6fe4

16 files changed

+102
-72
lines changed

lld/ELF/Arch/ARM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,8 +1542,8 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
15421542
}
15431543

15441544
if (auto e = buffer->commit())
1545-
fatal("failed to write output '" + buffer->getPath() +
1546-
"': " + toString(std::move(e)));
1545+
Fatal(ctx) << "failed to write output '" << buffer->getPath()
1546+
<< "': " << std::move(e);
15471547
}
15481548

15491549
void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }

lld/ELF/Arch/LoongArch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ static bool relax(Ctx &ctx, InputSection &sec) {
808808
}
809809
// Inform assignAddresses that the size has changed.
810810
if (!isUInt<32>(delta))
811-
fatal("section size decrease is too large: " + Twine(delta));
811+
Fatal(ctx) << "section size decrease is too large: " << Twine(delta);
812812
sec.bytesDropped = delta;
813813
return changed;
814814
}

lld/ELF/Arch/RISCV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ static bool relax(Ctx &ctx, InputSection &sec) {
901901
}
902902
// Inform assignAddresses that the size has changed.
903903
if (!isUInt<32>(delta))
904-
fatal("section size decrease is too large: " + Twine(delta));
904+
Fatal(ctx) << "section size decrease is too large: " << Twine(delta);
905905
sec.bytesDropped = delta;
906906
return changed;
907907
}

lld/ELF/Config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,11 @@ operator<<(const ELFSyncStream &s, T &&v) {
695695
return s;
696696
}
697697

698+
inline const ELFSyncStream &operator<<(const ELFSyncStream &s, const char *v) {
699+
s.os << v;
700+
return s;
701+
}
702+
698703
// Report a log if --verbose is specified.
699704
ELFSyncStream Log(Ctx &ctx);
700705

lld/ELF/Driver.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
265265
v.push_back(std::make_pair(mbref, c.getChildOffset()));
266266
}
267267
if (err)
268-
fatal(mb.getBufferIdentifier() + ": Archive::children failed: " +
269-
toString(std::move(err)));
268+
Fatal(ctx) << mb.getBufferIdentifier()
269+
<< ": Archive::children failed: " << std::move(err);
270270

271271
// Take ownership of memory buffers created for members of thin archives.
272272
std::vector<std::unique_ptr<MemoryBuffer>> mbs = file->takeThinBuffers();
@@ -1064,7 +1064,7 @@ template <class ELFT> static void readCallGraphsFromObjectFiles(Ctx &ctx) {
10641064
continue;
10651065

10661066
if (symbolIndices.size() != cgProfile.size() * 2)
1067-
fatal("number of relocations doesn't match Weights");
1067+
Fatal(ctx) << "number of relocations doesn't match Weights";
10681068

10691069
for (uint32_t i = 0, size = cgProfile.size(); i < size; ++i) {
10701070
const Elf_CGProfile_Impl<ELFT> &cgpe = cgProfile[i];
@@ -2445,7 +2445,7 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
24452445
const char *err = nullptr;
24462446
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
24472447
if (err)
2448-
fatal(toString(f) + ": could not decode addrsig section: " + err);
2448+
Fatal(ctx) << f << ": could not decode addrsig section: " << err;
24492449
markAddrsig(icfSafe, syms[symIndex]);
24502450
cur += size;
24512451
}
@@ -2507,7 +2507,7 @@ static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
25072507
// sizes of the Partition fields in InputSectionBase and Symbol, as well as
25082508
// the amount of space devoted to the partition number in RankFlags.
25092509
if (ctx.partitions.size() == 254)
2510-
fatal("may not have more than 254 partitions");
2510+
Fatal(ctx) << "may not have more than 254 partitions";
25112511

25122512
ctx.partitions.emplace_back(ctx);
25132513
Partition &newPart = ctx.partitions.back();

lld/ELF/EhFrame.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ class EhReader {
4141

4242
private:
4343
template <class P> void failOn(const P *loc, const Twine &msg) {
44-
fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " +
45-
isec->getObjMsg((const uint8_t *)loc - isec->content().data()));
44+
Fatal(ctx) << "corrupted .eh_frame: " << msg << "\n>>> defined in "
45+
<< isec->getObjMsg((const uint8_t *)loc -
46+
isec->content().data());
4647
}
4748

4849
uint8_t readByte();

lld/ELF/InputFiles.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ std::string lld::toString(const InputFile *f) {
6868

6969
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
7070
const InputFile *f) {
71-
s << toString(f);
72-
return s;
71+
return s << toString(f);
7372
}
7473

7574
static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
@@ -80,9 +79,9 @@ static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
8079
auto report = [&](StringRef msg) {
8180
StringRef filename = mb.getBufferIdentifier();
8281
if (archiveName.empty())
83-
fatal(filename + ": " + msg);
82+
Fatal(ctx) << filename << ": " << msg;
8483
else
85-
fatal(archiveName + "(" + filename + "): " + msg);
84+
Fatal(ctx) << archiveName << "(" << filename << "): " << msg;
8685
};
8786

8887
if (!mb.getBuffer().starts_with(ElfMagic))
@@ -573,7 +572,7 @@ template <class ELFT> void ELFFileBase::init(InputFile::Kind k) {
573572

574573
ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this);
575574
if (firstGlobal == 0 || firstGlobal > eSyms.size())
576-
fatal(toString(this) + ": invalid sh_info in symbol table");
575+
Fatal(ctx) << this << ": invalid sh_info in symbol table";
577576

578577
elfSyms = reinterpret_cast<const void *>(eSyms.data());
579578
numELFSyms = uint32_t(eSyms.size());
@@ -666,11 +665,11 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
666665
ArrayRef<Elf_Word> entries =
667666
CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
668667
if (entries.empty())
669-
fatal(toString(this) + ": empty SHT_GROUP");
668+
Fatal(ctx) << this << ": empty SHT_GROUP";
670669

671670
Elf_Word flag = entries[0];
672671
if (flag && flag != GRP_COMDAT)
673-
fatal(toString(this) + ": unsupported SHT_GROUP format");
672+
Fatal(ctx) << this << ": unsupported SHT_GROUP format";
674673

675674
bool keepGroup = (flag & GRP_COMDAT) == 0 || ignoreComdats ||
676675
ctx.symtab->comdatGroups
@@ -686,8 +685,8 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
686685
// Otherwise, discard group members.
687686
for (uint32_t secIndex : entries.slice(1)) {
688687
if (secIndex >= size)
689-
fatal(toString(this) +
690-
": invalid section index in group: " + Twine(secIndex));
688+
Fatal(ctx) << this
689+
<< ": invalid section index in group: " << Twine(secIndex);
691690
this->sections[secIndex] = &InputSection::discarded;
692691
}
693692
}
@@ -704,7 +703,7 @@ StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
704703
const Elf_Shdr &sec) {
705704
typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
706705
if (sec.sh_info >= symbols.size())
707-
fatal(toString(this) + ": invalid symbol index");
706+
Fatal(ctx) << this << ": invalid symbol index";
708707
const typename ELFT::Sym &sym = symbols[sec.sh_info];
709708
return CHECK(sym.getName(this->stringTable), this);
710709
}
@@ -742,13 +741,13 @@ bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
742741
if (entSize == 0)
743742
return false;
744743
if (sec.sh_size % entSize)
745-
fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" +
746-
Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
747-
Twine(entSize) + ")");
744+
Fatal(ctx) << this << ":(" << name << "): SHF_MERGE section size ("
745+
<< Twine(sec.sh_size) << ") must be a multiple of sh_entsize ("
746+
<< Twine(entSize) << ")";
748747

749748
if (sec.sh_flags & SHF_WRITE)
750-
fatal(toString(this) + ":(" + name +
751-
"): writable SHF_MERGE section is not supported");
749+
Fatal(ctx) << this << ":(" << name
750+
<< "): writable SHF_MERGE section is not supported";
752751

753752
return true;
754753
}
@@ -934,7 +933,7 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
934933
if (sec.sh_link < size)
935934
linkSec = this->sections[sec.sh_link];
936935
if (!linkSec)
937-
fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link));
936+
Fatal(ctx) << this << ": invalid sh_link index: " << Twine(sec.sh_link);
938937

939938
// A SHF_LINK_ORDER section is discarded if its linked-to section is
940939
// discarded.
@@ -963,8 +962,9 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
963962

964963
ArrayRef<uint8_t> data = sec.content();
965964
auto reportFatal = [&](const uint8_t *place, const Twine &msg) {
966-
fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
967-
Twine::utohexstr(place - sec.content().data()) + "): " + msg);
965+
Fatal(ctx) << sec.file << ":(" << sec.name << "+0x"
966+
<< Twine::utohexstr(place - sec.content().data())
967+
<< "): " << msg;
968968
};
969969
while (!data.empty()) {
970970
// Read one NOTE record.
@@ -1159,8 +1159,8 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
11591159
sym->isUsedInRegularObj = true;
11601160
if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
11611161
if (value == 0 || value >= UINT32_MAX)
1162-
fatal(toString(this) + ": common symbol '" + sym->getName() +
1163-
"' has invalid alignment: " + Twine(value));
1162+
Fatal(ctx) << this << ": common symbol '" << sym->getName()
1163+
<< "' has invalid alignment: " << Twine(value);
11641164
hasCommonSyms = true;
11651165
sym->resolve(ctx, CommonSymbol{ctx, this, StringRef(), binding, stOther,
11661166
type, value, size});
@@ -1207,7 +1207,7 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
12071207
else if (secIdx >= SHN_LORESERVE)
12081208
secIdx = 0;
12091209
if (LLVM_UNLIKELY(secIdx >= sections.size()))
1210-
fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
1210+
Fatal(ctx) << this << ": invalid section index: " << Twine(secIdx);
12111211
if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
12121212
error(toString(this) + ": non-local symbol (" + Twine(i) +
12131213
") found at index < .symtab's sh_info (" + Twine(end) + ")");
@@ -1217,7 +1217,7 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
12171217
if (type == STT_FILE)
12181218
sourceFile = CHECK(eSym.getName(stringTable), this);
12191219
if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name))
1220-
fatal(toString(this) + ": invalid symbol name offset");
1220+
Fatal(ctx) << this << ": invalid symbol name offset";
12211221
StringRef name(stringTable.data() + eSym.st_name);
12221222

12231223
symbols[i] = reinterpret_cast<Symbol *>(locals + i);
@@ -1267,7 +1267,7 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
12671267
else if (secIdx >= SHN_LORESERVE)
12681268
secIdx = 0;
12691269
if (LLVM_UNLIKELY(secIdx >= sections.size()))
1270-
fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
1270+
Fatal(ctx) << this << ": invalid section index: " << Twine(secIdx);
12711271
InputSectionBase *sec = sections[secIdx];
12721272
if (sec == &InputSection::discarded) {
12731273
if (sym.traced) {
@@ -1405,15 +1405,15 @@ std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
14051405
const uint8_t *verneedBuf = data.begin();
14061406
for (unsigned i = 0; i != sec->sh_info; ++i) {
14071407
if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end())
1408-
fatal(toString(this) + " has an invalid Verneed");
1408+
Fatal(ctx) << this << " has an invalid Verneed";
14091409
auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);
14101410
const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;
14111411
for (unsigned j = 0; j != vn->vn_cnt; ++j) {
14121412
if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end())
1413-
fatal(toString(this) + " has an invalid Vernaux");
1413+
Fatal(ctx) << this << " has an invalid Vernaux";
14141414
auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);
14151415
if (aux->vna_name >= this->stringTable.size())
1416-
fatal(toString(this) + " has a Vernaux with an invalid vna_name");
1416+
Fatal(ctx) << this << " has a Vernaux with an invalid vna_name";
14171417
uint16_t version = aux->vna_other & VERSYM_VERSION;
14181418
if (version >= verneeds.size())
14191419
verneeds.resize(version + 1);
@@ -1501,12 +1501,12 @@ template <class ELFT> void SharedFile::parse() {
15011501
if (dyn.d_tag == DT_NEEDED) {
15021502
uint64_t val = dyn.getVal();
15031503
if (val >= this->stringTable.size())
1504-
fatal(toString(this) + ": invalid DT_NEEDED entry");
1504+
Fatal(ctx) << this << ": invalid DT_NEEDED entry";
15051505
dtNeeded.push_back(this->stringTable.data() + val);
15061506
} else if (dyn.d_tag == DT_SONAME) {
15071507
uint64_t val = dyn.getVal();
15081508
if (val >= this->stringTable.size())
1509-
fatal(toString(this) + ": invalid DT_SONAME entry");
1509+
Fatal(ctx) << this << ": invalid DT_SONAME entry";
15101510
soName = this->stringTable.data() + val;
15111511
}
15121512
}

lld/ELF/InputSection.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ std::string lld::toString(const InputSectionBase *sec) {
3939
return (toString(sec->file) + ":(" + sec->name + ")").str();
4040
}
4141

42+
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
43+
const InputSectionBase *sec) {
44+
return s << toString(sec);
45+
}
46+
4247
template <class ELFT>
4348
static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,
4449
const typename ELFT::Shdr &hdr) {
@@ -65,7 +70,7 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
6570
// no alignment constraints.
6671
uint32_t v = std::max<uint32_t>(addralign, 1);
6772
if (!isPowerOf2_64(v))
68-
fatal(toString(this) + ": sh_addralign is not a power of 2");
73+
Fatal(ctx) << this << ": sh_addralign is not a power of 2";
6974
this->addralign = v;
7075

7176
// If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no
@@ -98,7 +103,7 @@ InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
98103
// they are allowed by the spec. I think 4GB is a reasonable limitation.
99104
// We might want to relax this in the future.
100105
if (hdr.sh_addralign > UINT32_MAX)
101-
fatal(toString(&file) + ": section sh_addralign is too large");
106+
Fatal(ctx) << &file << ": section sh_addralign is too large";
102107
}
103108

104109
size_t InputSectionBase::getSize() const {
@@ -116,8 +121,8 @@ static void decompressAux(const InputSectionBase &sec, uint8_t *out,
116121
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
117122
? compression::zlib::decompress(compressed, out, size)
118123
: compression::zstd::decompress(compressed, out, size))
119-
fatal(toString(&sec) +
120-
": decompress failed: " + llvm::toString(std::move(e)));
124+
Fatal(ctx) << &sec
125+
<< ": decompress failed: " << llvm::toString(std::move(e));
121126
}
122127

123128
void InputSectionBase::decompress() const {
@@ -621,7 +626,8 @@ static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) {
621626
static uint64_t getARMStaticBase(const Symbol &sym) {
622627
OutputSection *os = sym.getOutputSection();
623628
if (!os || !os->ptLoad || !os->ptLoad->firstSec)
624-
fatal("SBREL relocation to " + sym.getName() + " without static base");
629+
Fatal(ctx) << "SBREL relocation to " << sym.getName()
630+
<< " without static base";
625631
return os->ptLoad->firstSec->addr;
626632
}
627633

@@ -1269,8 +1275,8 @@ template <class ELFT> void InputSection::writeTo(Ctx &ctx, uint8_t *buf) {
12691275
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
12701276
? compression::zlib::decompress(compressed, buf, size)
12711277
: compression::zstd::decompress(compressed, buf, size))
1272-
fatal(toString(this) +
1273-
": decompress failed: " + llvm::toString(std::move(e)));
1278+
Fatal(ctx) << this
1279+
<< ": decompress failed: " << llvm::toString(std::move(e));
12741280
uint8_t *bufEnd = buf + size;
12751281
relocate<ELFT>(ctx, buf, bufEnd);
12761282
return;
@@ -1394,7 +1400,7 @@ void MergeInputSection::splitStrings(StringRef s, size_t entSize) {
13941400
const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;
13951401
const char *p = s.data(), *end = s.data() + s.size();
13961402
if (!std::all_of(end - entSize, end, [](char c) { return c == 0; }))
1397-
fatal(toString(this) + ": string is not null terminated");
1403+
Fatal(ctx) << this << ": string is not null terminated";
13981404
if (entSize == 1) {
13991405
// Optimize the common case.
14001406
do {
@@ -1454,7 +1460,7 @@ void MergeInputSection::splitIntoPieces() {
14541460

14551461
SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {
14561462
if (content().size() <= offset)
1457-
fatal(toString(this) + ": offset is outside the section");
1463+
Fatal(ctx) << this << ": offset is outside the section";
14581464
return partition_point(
14591465
pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];
14601466
}

lld/ELF/InputSection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ inline bool isDebugSection(const InputSectionBase &sec) {
501501
return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
502502
sec.name.starts_with(".debug");
503503
}
504+
505+
const ELFSyncStream &operator<<(const ELFSyncStream &,
506+
const InputSectionBase *);
504507
} // namespace elf
505508

506509
std::string toString(const elf::InputSectionBase *);

lld/ELF/Relocations.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ template <class ELFT> static void addCopyRelSymbol(Ctx &ctx, SharedSymbol &ss) {
376376
// Copy relocation against zero-sized symbol doesn't make sense.
377377
uint64_t symSize = ss.getSize();
378378
if (symSize == 0 || ss.alignment == 0)
379-
fatal("cannot create a copy relocation for symbol " + toString(ss));
379+
Err(ctx) << "cannot create a copy relocation for symbol " << &ss;
380380

381381
// See if this symbol is in a read-only segment. If so, preserve the symbol's
382382
// memory protection by reserving space in the .bss.rel.ro section.
@@ -441,7 +441,7 @@ class OffsetGetter {
441441
while (i != cies.end() && i->inputOff <= off)
442442
++i;
443443
if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off)
444-
fatal(".eh_frame: relocation is not in any piece");
444+
Fatal(ctx) << ".eh_frame: relocation is not in any piece";
445445
it = i;
446446
}
447447

@@ -2097,8 +2097,8 @@ ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
20972097
thunkSecOff = isec->outSecOff + isec->getSize();
20982098
if (!ctx.target->inBranchRange(rel.type, src,
20992099
os->addr + thunkSecOff + rel.addend))
2100-
fatal("InputSection too large for range extension thunk " +
2101-
isec->getObjMsg(src - (os->addr + isec->outSecOff)));
2100+
Fatal(ctx) << "InputSection too large for range extension thunk "
2101+
<< isec->getObjMsg(src - (os->addr << isec->outSecOff));
21022102
}
21032103
return addThunkSection(os, isd, thunkSecOff);
21042104
}

lld/ELF/Symbols.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ std::string lld::toString(const elf::Symbol &sym) {
5858
return ret;
5959
}
6060

61+
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
62+
const Symbol *sym) {
63+
return s << toString(*sym);
64+
}
65+
6166
static uint64_t getSymVA(Ctx &ctx, const Symbol &sym, int64_t addend) {
6267
switch (sym.kind()) {
6368
case Symbol::DefinedKind: {

0 commit comments

Comments
 (0)