[lld] 9b058bb - [ELF] Replace errorOrWarn(...) with Err

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 6 22:33:56 PST 2024


Author: Fangrui Song
Date: 2024-11-06T22:33:51-08:00
New Revision: 9b058bb42d49afb61b07a7eeeea1ad3d1407f1c9

URL: https://github.com/llvm/llvm-project/commit/9b058bb42d49afb61b07a7eeeea1ad3d1407f1c9
DIFF: https://github.com/llvm/llvm-project/commit/9b058bb42d49afb61b07a7eeeea1ad3d1407f1c9.diff

LOG: [ELF] Replace errorOrWarn(...) with Err

Added: 
    

Modified: 
    lld/ELF/Arch/ARM.cpp
    lld/ELF/Arch/LoongArch.cpp
    lld/ELF/Arch/PPC64.cpp
    lld/ELF/Arch/RISCV.cpp
    lld/ELF/Arch/X86_64.cpp
    lld/ELF/Driver.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/InputSection.cpp
    lld/ELF/LinkerScript.cpp
    lld/ELF/OutputSections.cpp
    lld/ELF/Relocations.cpp
    lld/ELF/ScriptParser.cpp
    lld/ELF/SymbolTable.cpp
    lld/ELF/Symbols.cpp
    lld/ELF/SyntheticSections.cpp
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index a6da1f3365a4e0..27986ddafeeb48 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1199,8 +1199,8 @@ template <class ELFT> void ObjFile<ELFT>::importCmseSymbols() {
   ArrayRef<Elf_Sym> eSyms = getELFSyms<ELFT>();
   // Error for local symbols. The symbol at index 0 is LOCAL. So skip it.
   for (size_t i = 1, end = firstGlobal; i != end; ++i) {
-    errorOrWarn("CMSE symbol '" + CHECK(eSyms[i].getName(stringTable), this) +
-                "' in import library '" + toString(this) + "' is not global");
+    Err(ctx) << "CMSE symbol '" << CHECK(eSyms[i].getName(stringTable), this)
+             << "' in import library '" << this << "' is not global";
   }
 
   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {

diff  --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index b357b22f1b25f3..edde5f65363a3b 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -165,7 +165,7 @@ static void handleUleb128(Ctx &ctx, uint8_t *loc, uint64_t val) {
   const char *error = nullptr;
   uint64_t orig = decodeULEB128(loc, &count, nullptr, &error);
   if (count > maxcount || (count == maxcount && error))
-    errorOrWarn(getErrorLoc(ctx, loc) + "extra space for uleb128");
+    Err(ctx) << getErrorLoc(ctx, loc) << "extra space for uleb128";
   uint64_t mask = count < maxcount ? (1ULL << 7 * count) - 1 : -1ULL;
   encodeULEB128((orig + val) & mask, loc, count);
 }
@@ -774,10 +774,10 @@ static bool relax(Ctx &ctx, InputSection &sec) {
         remove = allBytes - curBytes;
       // If we can't satisfy this alignment, we've found a bad input.
       if (LLVM_UNLIKELY(static_cast<int32_t>(remove) < 0)) {
-        errorOrWarn(getErrorLoc(ctx, (const uint8_t *)loc) +
-                    "insufficient padding bytes for " + lld::toString(r.type) +
-                    ": " + Twine(allBytes) + " bytes available for " +
-                    "requested alignment of " + Twine(align) + " bytes");
+        Err(ctx) << getErrorLoc(ctx, (const uint8_t *)loc)
+                 << "insufficient padding bytes for " << lld::toString(r.type)
+                 << ": " << Twine(allBytes) << " bytes available for "
+                 << "requested alignment of " << Twine(align) << " bytes";
         remove = 0;
       }
       break;

diff  --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 624b5f5e19f789..21790fb7aca511 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -694,9 +694,9 @@ void PPC64::relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const {
     // to ensure we don't miss these opportunities in real code. It can be
     // removed at a later date.
     if (pcRelInsn == UINT64_C(-1)) {
-      errorOrWarn(
-          "unrecognized instruction for R_PPC64_PCREL_OPT relaxation: 0x" +
-          Twine::utohexstr(accessInsn));
+      Err(ctx)
+          << "unrecognized instruction for R_PPC64_PCREL_OPT relaxation: 0x"
+          << Twine::utohexstr(accessInsn);
       break;
     }
 
@@ -770,7 +770,7 @@ void PPC64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
     } else if (locAsInt % 4 == 1) {
       write32(ctx, loc - 1, NOP);
     } else {
-      errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment");
+      Err(ctx) << "R_PPC64_TLSGD has unexpected byte alignment";
     }
     break;
   }
@@ -826,7 +826,7 @@ void PPC64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
     } else if (locAsInt % 4 == 1) {
       write32(ctx, loc - 1, NOP);
     } else {
-      errorOrWarn("R_PPC64_TLSLD has unexpected byte alignment");
+      Err(ctx) << "R_PPC64_TLSLD has unexpected byte alignment";
     }
     break;
   }
@@ -956,7 +956,7 @@ void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
       uint32_t tlsInstr = read32(ctx, loc - 1);
       uint32_t primaryOp = getPrimaryOpCode(tlsInstr);
       if (primaryOp != 31)
-        errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS");
+        Err(ctx) << "unrecognized instruction for IE to LE R_PPC64_TLS";
       uint32_t secondaryOp = (tlsInstr & 0x000007FE) >> 1; // bits 21-30
       // The add is a special case and should be turned into a nop. The paddi
       // that comes before it will already have computed the address of the
@@ -977,13 +977,13 @@ void PPC64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
         if (dFormOp == 0) { // Expecting a DS-Form instruction.
           dFormOp = getPPCDSFormOp(secondaryOp);
           if (dFormOp == 0)
-            errorOrWarn("unrecognized instruction for IE to LE R_PPC64_TLS");
+            Err(ctx) << "unrecognized instruction for IE to LE R_PPC64_TLS";
         }
         write32(ctx, loc - 1, (dFormOp | (tlsInstr & 0x03ff0000)));
       }
     } else {
-      errorOrWarn("R_PPC64_TLS must be either 4 byte aligned or one byte "
-                  "offset from 4 byte aligned");
+      Err(ctx) << "R_PPC64_TLS must be either 4 byte aligned or one byte "
+                  "offset from 4 byte aligned";
     }
     break;
   }
@@ -1556,7 +1556,7 @@ void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
       // bl __tls_get_addr(sym at tlsgd) --> add r3, r3, r13
       write32(ctx, loc - 1, 0x7c636a14);
     } else {
-      errorOrWarn("R_PPC64_TLSGD has unexpected byte alignment");
+      Err(ctx) << "R_PPC64_TLSGD has unexpected byte alignment";
     }
     return;
   }
@@ -1617,9 +1617,9 @@ void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
              read32(ctx, loc + 4) != 0x60000000) &&
             rel.sym->file != sec.file) {
           // Use substr(6) to remove the "__plt_" prefix.
-          errorOrWarn(getErrorLoc(ctx, loc) + "call to " +
-                      lld::toString(*rel.sym).substr(6) +
-                      " lacks nop, can't restore toc");
+          Err(ctx) << getErrorLoc(ctx, loc) << "call to "
+                   << lld::toString(*rel.sym).substr(6)
+                   << " lacks nop, can't restore toc";
           break;
         }
         write32(ctx, loc + 4, 0xe8410018); // ld %r2, 24(%r1)

diff  --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index e01725b96cbb21..4e5c4ae9c2f14c 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -660,15 +660,15 @@ void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
           auto val = rel.sym->getVA(ctx, rel.addend) -
                      rel1.sym->getVA(ctx, rel1.addend);
           if (overwriteULEB128(loc, val) >= 0x80)
-            errorOrWarn(sec.getLocation(rel.offset) + ": ULEB128 value " +
-                        Twine(val) + " exceeds available space; references '" +
-                        lld::toString(*rel.sym) + "'");
+            Err(ctx) << sec.getLocation(rel.offset) << ": ULEB128 value "
+                     << Twine(val) << " exceeds available space; references '"
+                     << lld::toString(*rel.sym) << "'";
           ++i;
           continue;
         }
       }
-      errorOrWarn(sec.getLocation(rel.offset) +
-                  ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
+      Err(ctx) << sec.getLocation(rel.offset)
+               << ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128";
       return;
     default:
       break;
@@ -832,12 +832,12 @@ static bool relax(Ctx &ctx, InputSection &sec) {
       remove = nextLoc - ((loc + align - 1) & -align);
       // If we can't satisfy this alignment, we've found a bad input.
       if (LLVM_UNLIKELY(static_cast<int32_t>(remove) < 0)) {
-        errorOrWarn(getErrorLoc(ctx, (const uint8_t *)loc) +
-                    "insufficient padding bytes for " + lld::toString(r.type) +
-                    ": " + Twine(r.addend) +
-                    " bytes available "
-                    "for requested alignment of " +
-                    Twine(align) + " bytes");
+        Err(ctx) << getErrorLoc(ctx, (const uint8_t *)loc)
+                 << "insufficient padding bytes for " << lld::toString(r.type)
+                 << ": " << Twine(r.addend)
+                 << " bytes available "
+                    "for requested alignment of "
+                 << Twine(align) << " bytes";
         remove = 0;
       }
       break;
@@ -1064,8 +1064,8 @@ static void mergeArch(RISCVISAUtils::OrderedExtensionMap &mergedExts,
                       StringRef s) {
   auto maybeInfo = RISCVISAInfo::parseNormalizedArchString(s);
   if (!maybeInfo) {
-    errorOrWarn(toString(sec) + ": " + s + ": " +
-                llvm::toString(maybeInfo.takeError()));
+    Err(ctx) << sec << ": " << s << ": "
+             << llvm::toString(maybeInfo.takeError());
     return;
   }
 
@@ -1097,11 +1097,11 @@ static void mergeAtomic(DenseMap<unsigned, unsigned>::iterator it,
     return;
 
   auto reportAbiError = [&]() {
-    errorOrWarn("atomic abi mismatch for " + oldSection->name + "\n>>> " +
-                toString(oldSection) +
-                ": atomic_abi=" + Twine(static_cast<unsigned>(oldTag)) +
-                "\n>>> " + toString(newSection) +
-                ": atomic_abi=" + Twine(static_cast<unsigned>(newTag)));
+    Err(ctx) << "atomic abi mismatch for " << oldSection->name << "\n>>> "
+             << oldSection
+             << ": atomic_abi=" << Twine(static_cast<unsigned>(oldTag))
+             << "\n>>> " << newSection
+             << ": atomic_abi=" << Twine(static_cast<unsigned>(newTag));
   };
 
   auto reportUnknownAbiError = [](const InputSectionBase *section,
@@ -1113,9 +1113,8 @@ static void mergeAtomic(DenseMap<unsigned, unsigned>::iterator it,
     case RISCVAtomicAbiTag::A7:
       return;
     };
-    errorOrWarn("unknown atomic abi for " + section->name + "\n>>> " +
-                toString(section) +
-                ": atomic_abi=" + Twine(static_cast<unsigned>(tag)));
+    Err(ctx) << "unknown atomic abi for " << section->name << "\n>>> "
+             << section << ": atomic_abi=" << Twine(static_cast<unsigned>(tag));
   };
   switch (oldTag) {
   case RISCVAtomicAbiTag::UNKNOWN:
@@ -1200,9 +1199,9 @@ mergeAttributesSection(Ctx &ctx,
             firstStackAlign = sec;
             firstStackAlignValue = *i;
           } else if (r.first->second != *i) {
-            errorOrWarn(toString(sec) + " has stack_align=" + Twine(*i) +
-                        " but " + toString(firstStackAlign) +
-                        " has stack_align=" + Twine(firstStackAlignValue));
+            Err(ctx) << sec << " has stack_align=" << Twine(*i) << " but "
+                     << firstStackAlign
+                     << " has stack_align=" << Twine(firstStackAlignValue);
           }
         }
         continue;
@@ -1262,7 +1261,7 @@ mergeAttributesSection(Ctx &ctx,
       merged.strAttr.try_emplace(RISCVAttrs::ARCH,
                                  saver().save((*result)->toString()));
     } else {
-      errorOrWarn(llvm::toString(result.takeError()));
+      Err(ctx) << llvm::toString(result.takeError());
     }
   }
 

diff  --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index c3031b1f9a68a8..b4caf99e881a76 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -490,9 +490,9 @@ void X86_64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
     // Convert leaq x at tlsdesc(%rip), %REG to movq $x at tpoff, %REG.
     if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d ||
         (loc[-1] & 0xc7) != 0x05) {
-      errorOrWarn(getErrorLoc(ctx, loc - 3) +
-                  "R_X86_64_GOTPC32_TLSDESC must be used "
-                  "in leaq x at tlsdesc(%rip), %REG");
+      Err(ctx) << getErrorLoc(ctx, loc - 3)
+               << "R_X86_64_GOTPC32_TLSDESC must be used "
+                  "in leaq x at tlsdesc(%rip), %REG";
       return;
     }
     loc[-3] = 0x48 | ((loc[-3] >> 2) & 1);
@@ -532,9 +532,9 @@ void X86_64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
     assert(rel.type == R_X86_64_GOTPC32_TLSDESC);
     if ((loc[-3] & 0xfb) != 0x48 || loc[-2] != 0x8d ||
         (loc[-1] & 0xc7) != 0x05) {
-      errorOrWarn(getErrorLoc(ctx, loc - 3) +
-                  "R_X86_64_GOTPC32_TLSDESC must be used "
-                  "in leaq x at tlsdesc(%rip), %REG");
+      Err(ctx) << getErrorLoc(ctx, loc - 3)
+               << "R_X86_64_GOTPC32_TLSDESC must be used "
+                  "in leaq x at tlsdesc(%rip), %REG";
       return;
     }
     loc[-2] = 0x8b;

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 157e6289df78fb..711000dcd4be12 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2851,11 +2851,11 @@ static void readSecurityNotes(Ctx &ctx) {
     }
 
     if (ctx.aarch64PauthAbiCoreInfo != f->aarch64PauthAbiCoreInfo)
-      errorOrWarn("incompatible values of AArch64 PAuth core info found\n>>> " +
-                  referenceFileName + ": 0x" +
-                  toHex(ctx.aarch64PauthAbiCoreInfo, /*LowerCase=*/true) +
-                  "\n>>> " + toString(f) + ": 0x" +
-                  toHex(f->aarch64PauthAbiCoreInfo, /*LowerCase=*/true));
+      Err(ctx) << "incompatible values of AArch64 PAuth core info found\n>>> "
+               << referenceFileName << ": 0x"
+               << toHex(ctx.aarch64PauthAbiCoreInfo, /*LowerCase=*/true)
+               << "\n>>> " << f << ": 0x"
+               << toHex(f->aarch64PauthAbiCoreInfo, /*LowerCase=*/true);
   }
 
   // Force enable Shadow Stack.

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 6e6d7bfa516067..3b63e46edd0984 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -863,8 +863,8 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
         ctx.hasSympart.store(true, std::memory_order_relaxed);
       else if (ctx.arg.rejectMismatch &&
                !isKnownSpecificSectionType(type, sec.sh_flags))
-        errorOrWarn(toString(this->sections[i]) + ": unknown section type 0x" +
-                    Twine::utohexstr(type));
+        Err(ctx) << this->sections[i] << ": unknown section type 0x"
+                 << Twine::utohexstr(type);
       break;
     }
   }
@@ -1247,8 +1247,8 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
     uint8_t binding = eSym.getBinding();
     if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&
                       binding != STB_GNU_UNIQUE))
-      errorOrWarn(toString(this) + ": symbol (" + Twine(i) +
-                  ") has invalid binding: " + Twine((int)binding));
+      Err(ctx) << this << ": symbol (" << Twine(i)
+               << ") has invalid binding: " << Twine((int)binding);
 
     // st_value of STT_TLS represents the assigned offset, not the actual
     // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can
@@ -1256,8 +1256,8 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
     // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa.
     if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS &&
         eSym.getType() != STT_NOTYPE)
-      errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " +
-                  toString(sym.file) + "\n>>> in " + toString(this));
+      Err(ctx) << "TLS attribute mismatch: " << &sym << "\n>>> in " << sym.file
+               << "\n>>> in " << this;
 
     // Handle non-COMMON defined symbol below. !sym.file allows a symbol
     // assignment to redefine a symbol without an error.
@@ -1563,8 +1563,8 @@ template <class ELFT> void SharedFile::parse() {
     // symbol, that's a violation of the spec.
     StringRef name = CHECK(sym.getName(stringTable), this);
     if (sym.getBinding() == STB_LOCAL) {
-      errorOrWarn(toString(this) + ": invalid local symbol '" + name +
-                  "' in global part of symbol table");
+      Err(ctx) << this << ": invalid local symbol '" << name
+               << "' in global part of symbol table";
       continue;
     }
 

diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index bbdce28d64138d..d246c647181ec4 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -646,19 +646,18 @@ static Relocation *getRISCVPCRelHi20(const InputSectionBase *loSec,
 
   const Defined *d = cast<Defined>(sym);
   if (!d->section) {
-    errorOrWarn(
-        loSec->getLocation(loReloc.offset) +
-        ": R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " +
-        sym->getName());
+    Err(ctx) << loSec->getLocation(loReloc.offset)
+             << ": R_RISCV_PCREL_LO12 relocation points to an absolute symbol: "
+             << sym->getName();
     return nullptr;
   }
   InputSection *hiSec = cast<InputSection>(d->section);
 
   if (hiSec != loSec)
-    errorOrWarn(loSec->getLocation(loReloc.offset) +
-                ": R_RISCV_PCREL_LO12 relocation points to a symbol '" +
-                sym->getName() + "' in a 
diff erent section '" + hiSec->name +
-                "'");
+    Err(ctx) << loSec->getLocation(loReloc.offset)
+             << ": R_RISCV_PCREL_LO12 relocation points to a symbol '"
+             << sym->getName() << "' in a 
diff erent section '" << hiSec->name
+             << "'";
 
   if (addend != 0)
     Warn(ctx) << loSec->getLocation(loReloc.offset)
@@ -680,10 +679,10 @@ static Relocation *getRISCVPCRelHi20(const InputSectionBase *loSec,
         it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20)
       return &*it;
 
-  errorOrWarn(loSec->getLocation(loReloc.offset) +
-              ": R_RISCV_PCREL_LO12 relocation points to " +
-              hiSec->getObjMsg(d->value) +
-              " without an associated R_RISCV_PCREL_HI20 relocation");
+  Err(ctx) << loSec->getLocation(loReloc.offset)
+           << ": R_RISCV_PCREL_LO12 relocation points to "
+           << hiSec->getObjMsg(d->value)
+           << " without an associated R_RISCV_PCREL_HI20 relocation";
   return nullptr;
 }
 
@@ -1031,13 +1030,13 @@ void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,
                 (f->getRelocTargetSym(*it).getVA(ctx) + getAddend<ELFT>(*it));
         }
         if (overwriteULEB128(bufLoc, val) >= 0x80)
-          errorOrWarn(getLocation(offset) + ": ULEB128 value " + Twine(val) +
-                      " exceeds available space; references '" +
-                      lld::toString(sym) + "'");
+          Err(ctx) << getLocation(offset) << ": ULEB128 value " << Twine(val)
+                   << " exceeds available space; references '"
+                   << lld::toString(sym) << "'";
         continue;
       }
-      errorOrWarn(getLocation(offset) +
-                  ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
+      Err(ctx) << getLocation(offset)
+               << ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128";
       return;
     }
 
@@ -1109,7 +1108,7 @@ void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,
                       toString(type) + " against symbol '" + toString(sym) +
                       "'";
     if (expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC)) {
-      errorOrWarn(msg);
+      Err(ctx) << msg;
       return;
     }
 
@@ -1369,8 +1368,8 @@ void EhInputSection::split(ArrayRef<RelTy> rels) {
     d = d.slice(size);
   }
   if (msg)
-    errorOrWarn("corrupted .eh_frame: " + Twine(msg) + "\n>>> defined in " +
-                getObjMsg(d.data() - content().data()));
+    Err(ctx) << "corrupted .eh_frame: " << Twine(msg) << "\n>>> defined in "
+             << getObjMsg(d.data() - content().data());
 }
 
 // Return the offset in an output section for a given input offset.

diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index e415c998956b31..cd549067084ede 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -599,12 +599,12 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
     SectionClassDesc *scd =
         sectionClasses.lookup(CachedHashStringRef(cmd->classRef));
     if (!scd) {
-      errorOrWarn("undefined section class '" + cmd->classRef + "'");
+      Err(ctx) << "undefined section class '" << cmd->classRef << "'";
       return ret;
     }
     if (!scd->sc.assigned) {
-      errorOrWarn("section class '" + cmd->classRef + "' referenced by '" +
-                  outCmd.name + "' before class definition");
+      Err(ctx) << "section class '" << cmd->classRef << "' referenced by '"
+               << outCmd.name << "' before class definition";
       return ret;
     }
 
@@ -614,8 +614,8 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
           continue;
         bool isSpill = sec->parent && isa<OutputSection>(sec->parent);
         if (!sec->parent || (isSpill && outCmd.name == "/DISCARD/")) {
-          errorOrWarn("section '" + sec->name +
-                      "' cannot spill from/to /DISCARD/");
+          Err(ctx) << "section '" << sec->name
+                   << "' cannot spill from/to /DISCARD/";
           continue;
         }
         if (isSpill)
@@ -796,9 +796,9 @@ void LinkerScript::processSectionCommands() {
         for (InputSectionBase *isec : isd->sectionBases)
           if (isa<PotentialSpillSection>(isec) ||
               potentialSpillLists.contains(isec))
-            errorOrWarn("section '" + isec->name +
-                        "' cannot spill from/to INSERT section '" + os->name +
-                        "'");
+            Err(ctx) << "section '" << isec->name
+                     << "' cannot spill from/to INSERT section '" << os->name
+                     << "'";
       }
     }
   }
@@ -816,8 +816,8 @@ void LinkerScript::processSectionCommands() {
     for (InputSectionDescription *isd : sc->sc.commands) {
       for (InputSectionBase *sec : isd->sectionBases) {
         if (sec->parent && isa<SectionClass>(sec->parent)) {
-          errorOrWarn("section class '" + sec->parent->name +
-                      "' is unreferenced");
+          Err(ctx) << "section class '" << sec->parent->name
+                   << "' is unreferenced";
           goto nextClass;
         }
       }
@@ -1779,7 +1779,7 @@ static void checkMemoryRegion(const MemoryRegion *region,
 
 void LinkerScript::checkFinalScriptConditions() const {
   for (StringRef err : recordedErrors)
-    errorOrWarn(err);
+    Err(ctx) << err;
   for (const OutputSection *sec : ctx.outputSections) {
     if (const MemoryRegion *memoryRegion = sec->memRegion)
       checkMemoryRegion(memoryRegion, sec, sec->addr);

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index fb4d7130b765bf..6e8a055922b83c 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -131,11 +131,11 @@ void OutputSection::commitSection(InputSection *isec) {
         // https://github.com/ClangBuiltLinux/linux/issues/1597) rely on the
         // behavior. Other types get an error.
         if (type != SHT_NOBITS) {
-          errorOrWarn("section type mismatch for " + isec->name + "\n>>> " +
-                      toString(isec) + ": " +
-                      getELFSectionTypeName(ctx.arg.emachine, isec->type) +
-                      "\n>>> output section " + name + ": " +
-                      getELFSectionTypeName(ctx.arg.emachine, type));
+          Err(ctx) << "section type mismatch for " << isec->name << "\n>>> "
+                   << isec << ": "
+                   << getELFSectionTypeName(ctx.arg.emachine, isec->type)
+                   << "\n>>> output section " << name << ": "
+                   << getELFSectionTypeName(ctx.arg.emachine, type);
         }
       }
       if (!typeIsSet)
@@ -315,7 +315,7 @@ static SmallVector<uint8_t, 0> deflateShard(ArrayRef<uint8_t> in, int level,
   z_stream s = {};
   auto res = deflateInit2(&s, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
   if (res != 0) {
-    errorOrWarn("--compress-sections: deflateInit2 returned " + Twine(res));
+    Err(ctx) << "--compress-sections: deflateInit2 returned " << Twine(res);
     return {};
   }
   s.next_in = const_cast<uint8_t *>(in.data());
@@ -362,8 +362,8 @@ template <class ELFT> void OutputSection::maybeCompress(Ctx &ctx) {
   if (ctype == DebugCompressionType::None)
     return;
   if (flags & SHF_ALLOC) {
-    errorOrWarn("--compress-sections: section '" + name +
-                "' with the SHF_ALLOC flag cannot be compressed");
+    Err(ctx) << "--compress-sections: section '" << name
+             << "' with the SHF_ALLOC flag cannot be compressed";
     return;
   }
 
@@ -663,7 +663,7 @@ static size_t relToCrel(Ctx &ctx, raw_svector_ostream &os,
   const auto &file = *cast<ELFFileBase>(relSec->file);
   if (relSec->type == SHT_REL) {
     // REL conversion is complex and unsupported yet.
-    errorOrWarn(toString(relSec) + ": REL cannot be converted to CREL");
+    Err(ctx) << relSec << ": REL cannot be converted to CREL";
     return 0;
   }
   auto rels = relSec->getDataAs<typename ELFT::Rela>();

diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index d1e5849551ff7a..bd2cfcc7f8fb45 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -123,9 +123,9 @@ void elf::reportRangeError(Ctx &ctx, uint8_t *loc, const Relocation &rel,
     hint += "; consider recompiling with -fdebug-types-section to reduce size "
             "of debug sections";
 
-  errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) +
-              " out of range: " + v.str() + " is not in [" + Twine(min).str() +
-              ", " + Twine(max).str() + "]" + hint);
+  Err(ctx) << errPlace.loc << "relocation " << lld::toString(rel.type)
+           << " out of range: " << v.str() << " is not in [" << Twine(min).str()
+           << ", " << Twine(max).str() << "]" << hint;
 }
 
 void elf::reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,
@@ -135,9 +135,9 @@ void elf::reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,
   if (!sym.getName().empty())
     hint = "; references '" + lld::toString(sym) + '\'' +
            getDefinedLocation(ctx, sym);
-  errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) +
-              " is not in [" + Twine(llvm::minIntN(n)) + ", " +
-              Twine(llvm::maxIntN(n)) + "]" + hint);
+  Err(ctx) << errPlace.loc << msg << " is out of range: " << Twine(v)
+           << " is not in [" << Twine(llvm::minIntN(n)) << ", "
+           << Twine(llvm::maxIntN(n)) << "]" << hint;
 }
 
 // Build a bitmask with one bit set for each 64 subset of RelExpr.
@@ -1209,8 +1209,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
   if (!ctx.arg.shared && sym.isShared() &&
       !(ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64)) {
     if (!canDefineSymbolInExecutable(ctx, sym)) {
-      errorOrWarn("cannot preempt symbol: " + toString(sym) +
-                  getLocation(ctx, *sec, sym, offset));
+      Err(ctx) << "cannot preempt symbol: " << &sym
+               << getLocation(ctx, *sec, sym, offset);
       return;
     }
 
@@ -1256,19 +1256,19 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
     //   the wrong ebx value.
     if (sym.isFunc()) {
       if (ctx.arg.pie && ctx.arg.emachine == EM_386)
-        errorOrWarn("symbol '" + toString(sym) +
-                    "' cannot be preempted; recompile with -fPIE" +
-                    getLocation(ctx, *sec, sym, offset));
+        Err(ctx) << "symbol '" << &sym
+                 << "' cannot be preempted; recompile with -fPIE"
+                 << getLocation(ctx, *sec, sym, offset);
       sym.setFlags(NEEDS_COPY | NEEDS_PLT);
       sec->addReloc({expr, type, offset, addend, &sym});
       return;
     }
   }
 
-  errorOrWarn("relocation " + toString(type) + " cannot be used against " +
-              (sym.getName().empty() ? "local symbol"
-                                     : "symbol '" + toString(sym) + "'") +
-              "; recompile with -fPIC" + getLocation(ctx, *sec, sym, offset));
+  Err(ctx) << "relocation " << type << " cannot be used against "
+           << (sym.getName().empty() ? "local symbol"
+                                     : ("symbol '" + toString(sym) + "'"))
+           << "; recompile with -fPIC" << getLocation(ctx, *sec, sym, offset);
 }
 
 // This function is similar to the `handleTlsRelocation`. MIPS does not
@@ -1305,9 +1305,9 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
                                                 int64_t addend) {
   if (expr == R_TPREL || expr == R_TPREL_NEG) {
     if (ctx.arg.shared) {
-      errorOrWarn("relocation " + toString(type) + " against " + toString(sym) +
-                  " cannot be used with -shared" +
-                  getLocation(ctx, *sec, sym, offset));
+      Err(ctx) << "relocation " << type << " against " << &sym
+               << " cannot be used with -shared"
+               << getLocation(ctx, *sec, sym, offset);
       return 1;
     }
     return 0;
@@ -1514,9 +1514,9 @@ void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
       // Skip the error check for CREL, which does not set `end`.
       if constexpr (!RelTy::IsCrel) {
         if (i == end) {
-          errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last "
-                      "relocation" +
-                      getLocation(ctx, *sec, sym, offset));
+          Err(ctx) << "R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last "
+                      "relocation"
+                   << getLocation(ctx, *sec, sym, offset);
           return;
         }
       }
@@ -2460,9 +2460,9 @@ static void scanCrossRefs(Ctx &ctx, const NoCrossRefCommand &cmd,
       toSymName = toString(sym);
     else if (auto *d = dyn_cast<Defined>(&sym))
       toSymName = d->section->name;
-    errorOrWarn(sec->getLocation(r.r_offset) +
-                ": prohibited cross reference from '" + osec->name + "' to '" +
-                toSymName + "' in '" + dstOsec->name + "'");
+    Err(ctx) << sec->getLocation(r.r_offset)
+             << ": prohibited cross reference from '" << osec->name << "' to '"
+             << toSymName << "' in '" << dstOsec->name << "'";
   }
 }
 

diff  --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index f57b042534feac..85b45f6198ea59 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -892,9 +892,9 @@ Expr ScriptParser::readAssert() {
   StringRef msg = readName();
   expect(")");
 
-  return [=, s = ctx.script]() -> ExprValue {
+  return [=, s = ctx.script, &ctx = ctx]() -> ExprValue {
     if (!e().getValue())
-      errorOrWarn(msg);
+      Err(ctx) << msg;
     return s->getDot();
   };
 }

diff  --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp
index c3b8c853a2011e..f76ca0a9faf52f 100644
--- a/lld/ELF/SymbolTable.cpp
+++ b/lld/ELF/SymbolTable.cpp
@@ -276,8 +276,8 @@ void SymbolTable::scanVersionScript() {
                                    pat.isExternCpp, /*hasWildCard=*/false},
                                   id, ver, /*includeNonDefault=*/true);
       if (!found && !ctx.arg.undefinedVersion)
-        errorOrWarn("version script assignment of '" + ver + "' to symbol '" +
-                    pat.name + "' failed: symbol not defined");
+        Err(ctx) << "version script assignment of '" << ver << "' to symbol '"
+                 << pat.name << "' failed: symbol not defined";
     };
     for (SymbolVersion &pat : v.nonLocalPatterns)
       if (!pat.hasWildcard)

diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 4eff11447c1996..cfaa1a1ec1637f 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -125,8 +125,8 @@ static uint64_t getSymVA(Ctx &ctx, const Symbol &sym, int64_t addend) {
       // for Android relocation packing requires knowing TLS symbol addresses
       // during section finalization.)
       if (!ctx.tlsPhdr || !ctx.tlsPhdr->firstSec) {
-        errorOrWarn(toString(d.file) +
-                    " has an STT_TLS symbol but doesn't have a PT_TLS segment");
+        Err(ctx) << d.file
+                 << " has an STT_TLS symbol but doesn't have a PT_TLS segment";
         return 0;
       }
       return va - ctx.tlsPhdr->firstSec->addr;
@@ -530,8 +530,8 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
   if (!d->section && !errSec && errOffset && d->value == errOffset)
     return;
   if (!d->section || !errSec) {
-    errorOrWarn("duplicate symbol: " + toString(sym) + "\n>>> defined in " +
-                toString(sym.file) + "\n>>> defined in " + toString(newFile));
+    Err(ctx) << "duplicate symbol: " << &sym << "\n>>> defined in " << sym.file
+             << "\n>>> defined in " << newFile;
     return;
   }
 
@@ -555,7 +555,7 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
   if (!src2.empty())
     msg += src2 + "\n>>>            ";
   msg += obj2;
-  errorOrWarn(msg);
+  Err(ctx) << msg;
 }
 
 void Symbol::checkDuplicate(Ctx &ctx, const Defined &other) const {

diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 3a87d69b1bf334..f502c859113d47 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -572,8 +572,8 @@ SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {
       uint64_t pc = getFdePc(buf, fde->outputOff, enc);
       uint64_t fdeVA = getParent()->addr + fde->outputOff;
       if (!isInt<32>(pc - va)) {
-        errorOrWarn(toString(fde->sec) + ": PC offset is too large: 0x" +
-                    Twine::utohexstr(pc - va));
+        Err(ctx) << fde->sec << ": PC offset is too large: 0x"
+                 << Twine::utohexstr(pc - va);
         continue;
       }
       ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});
@@ -2861,6 +2861,7 @@ void DebugNamesBaseSection::parseDebugNames(
         uint32_t numCus, const DWARFDebugNames::Header &,
         const DWARFDebugNames::DWARFDebugNamesOffsets &)>
         readOffsets) {
+  Ctx &ctx = elf::ctx;
   const LLDDWARFSection &namesSec = inputChunk.section;
   DenseMap<uint32_t, IndexEntry *> offsetMap;
   // Number of CUs seen in previous NameIndex sections within current chunk.
@@ -2869,20 +2870,20 @@ void DebugNamesBaseSection::parseDebugNames(
     NameData &nd = inputChunk.nameData.emplace_back();
     nd.hdr = ni.getHeader();
     if (nd.hdr.Format != DwarfFormat::DWARF32) {
-      errorOrWarn(toString(namesSec.sec) +
-                  Twine(": found DWARF64, which is currently unsupported"));
+      Err(ctx) << namesSec.sec
+               << Twine(": found DWARF64, which is currently unsupported");
       return;
     }
     if (nd.hdr.Version != 5) {
-      errorOrWarn(toString(namesSec.sec) + Twine(": unsupported version: ") +
-                  Twine(nd.hdr.Version));
+      Err(ctx) << namesSec.sec << Twine(": unsupported version: ")
+               << Twine(nd.hdr.Version);
       return;
     }
     uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize(DwarfFormat::DWARF32);
     DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();
     if (locs.EntriesBase > namesExtractor.getData().size()) {
-      errorOrWarn(toString(namesSec.sec) +
-                  Twine(": entry pool start is beyond end of section"));
+      Err(ctx) << namesSec.sec
+               << Twine(": entry pool start is beyond end of section");
       return;
     }
 
@@ -2907,15 +2908,13 @@ void DebugNamesBaseSection::parseDebugNames(
         Expected<IndexEntry *> ieOrErr =
             readEntry(offset, ni, locs.EntriesBase, namesExtractor, namesSec);
         if (!ieOrErr) {
-          errorOrWarn(toString(namesSec.sec) + ": " +
-                      toString(ieOrErr.takeError()));
+          Err(ctx) << namesSec.sec << ": " << ieOrErr.takeError();
           return;
         }
         ne.indexEntries.push_back(std::move(*ieOrErr));
       }
       if (offset >= namesSec.Data.size())
-        errorOrWarn(toString(namesSec.sec) +
-                    Twine(": index entry is out of bounds"));
+        Err(ctx) << namesSec.sec << Twine(": index entry is out of bounds");
 
       for (IndexEntry &ie : ne.entries())
         offsetMap[ie.poolOffset] = &ie;
@@ -3197,7 +3196,7 @@ void DebugNamesBaseSection::init(
 template <class ELFT>
 DebugNamesSection<ELFT>::DebugNamesSection(Ctx &ctx)
     : DebugNamesBaseSection(ctx) {
-  init([](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {
+  init([&](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {
     auto *file = cast<ObjFile<ELFT>>(f);
     DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
     auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
@@ -3213,8 +3212,7 @@ DebugNamesSection<ELFT>::DebugNamesSection(Ctx &ctx)
 
     inputChunk.llvmDebugNames.emplace(namesExtractor, strExtractor);
     if (Error e = inputChunk.llvmDebugNames->extract()) {
-      errorOrWarn(toString(dobj.getNamesSection().sec) + Twine(": ") +
-                  toString(std::move(e)));
+      Err(ctx) << dobj.getNamesSection().sec << Twine(": ") << std::move(e);
     }
     parseDebugNames(
         inputChunk, chunk, namesExtractor, strExtractor,
@@ -3515,8 +3513,8 @@ createSymbols(
   }
   // If off overflows, the last symbol's nameOff likely overflows.
   if (!isUInt<32>(off))
-    errorOrWarn("--gdb-index: constant pool size (" + Twine(off) +
-                ") exceeds UINT32_MAX");
+    Err(ctx) << "--gdb-index: constant pool size (" << Twine(off)
+             << ") exceeds UINT32_MAX";
 
   return {ret, off};
 }
@@ -4625,20 +4623,20 @@ createMemtagGlobalDescriptors(Ctx &ctx,
     const uint64_t size = sym->getSize();
 
     if (addr <= kMemtagGranuleSize && buf != nullptr)
-      errorOrWarn("address of the tagged symbol \"" + sym->getName() +
-                  "\" falls in the ELF header. This is indicative of a "
-                  "compiler/linker bug");
+      Err(ctx) << "address of the tagged symbol \"" << sym->getName()
+               << "\" falls in the ELF header. This is indicative of a "
+                  "compiler/linker bug";
     if (addr % kMemtagGranuleSize != 0)
-      errorOrWarn("address of the tagged symbol \"" + sym->getName() +
-                  "\" at 0x" + Twine::utohexstr(addr) +
-                  "\" is not granule (16-byte) aligned");
+      Err(ctx) << "address of the tagged symbol \"" << sym->getName()
+               << "\" at 0x" << Twine::utohexstr(addr)
+               << "\" is not granule (16-byte) aligned";
     if (size == 0)
-      errorOrWarn("size of the tagged symbol \"" + sym->getName() +
-                  "\" is not allowed to be zero");
+      Err(ctx) << "size of the tagged symbol \"" << sym->getName()
+               << "\" is not allowed to be zero";
     if (size % kMemtagGranuleSize != 0)
-      errorOrWarn("size of the tagged symbol \"" + sym->getName() +
-                  "\" (size 0x" + Twine::utohexstr(size) +
-                  ") is not granule (16-byte) aligned");
+      Err(ctx) << "size of the tagged symbol \"" << sym->getName()
+               << "\" (size 0x" << Twine::utohexstr(size)
+               << ") is not granule (16-byte) aligned";
 
     const uint64_t sizeToEncode = size / kMemtagGranuleSize;
     const uint64_t stepToEncode = ((addr - lastGlobalEnd) / kMemtagGranuleSize)

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 0ca94286e291b5..0a53137c3ea4c8 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1538,12 +1538,12 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
         break;
       if (++assignPasses == 5) {
         if (changes.first)
-          errorOrWarn("address (0x" + Twine::utohexstr(changes.first->addr) +
-                      ") of section '" + changes.first->name +
-                      "' does not converge");
+          Err(ctx) << "address (0x" << Twine::utohexstr(changes.first->addr)
+                   << ") of section '" << changes.first->name
+                   << "' does not converge";
         if (changes.second)
-          errorOrWarn("assignment to symbol " + toString(*changes.second) +
-                      " does not converge");
+          Err(ctx) << "assignment to symbol " << changes.second
+                   << " does not converge";
         break;
       }
     } else if (spilled) {
@@ -2644,11 +2644,11 @@ static void checkOverlap(StringRef name, std::vector<SectionOffset> &sections,
     if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
       continue;
 
-    errorOrWarn("section " + a.sec->name + " " + name +
-                " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
-                " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
-                b.sec->name + " range is " +
-                rangeToString(b.offset, b.sec->size));
+    Err(ctx) << "section " << a.sec->name << " " << name
+             << " range overlaps with " << b.sec->name << "\n>>> "
+             << a.sec->name << " range is "
+             << rangeToString(a.offset, a.sec->size) << "\n>>> " << b.sec->name
+             << " range is " << rangeToString(b.offset, b.sec->size);
   }
 }
 
@@ -2662,9 +2662,9 @@ template <class ELFT> void Writer<ELFT>::checkSections() {
   for (OutputSection *os : ctx.outputSections)
     if ((os->addr + os->size < os->addr) ||
         (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))
-      errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
-                  " of size 0x" + utohexstr(os->size) +
-                  " exceeds available address space");
+      Err(ctx) << "section " << os->name << " at 0x" << utohexstr(os->addr)
+               << " of size 0x" << utohexstr(os->size)
+               << " exceeds available address space";
 
   // Check for overlapping file offsets. In this case we need to skip any
   // section marked as SHT_NOBITS. These sections don't actually occupy space in


        


More information about the llvm-commits mailing list