[lld] 05ff6e7 - [ELF] Use lower case offset in getObjMsg

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 29 13:00:43 PST 2024


Author: Fangrui Song
Date: 2024-11-29T13:00:36-08:00
New Revision: 05ff6e7948df3c152982814ea2367c7b83138cea

URL: https://github.com/llvm/llvm-project/commit/05ff6e7948df3c152982814ea2367c7b83138cea
DIFF: https://github.com/llvm/llvm-project/commit/05ff6e7948df3c152982814ea2367c7b83138cea.diff

LOG: [ELF] Use lower case offset in getObjMsg

to improve consistency with other diagnostics. While here, migrate to
use ELFSyncStream to drop toStr/getCtx uses and avoid string overhead.

Added: 
    

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/InputSection.h
    lld/ELF/Symbols.cpp
    lld/test/ELF/invalid-eh-frame3.s
    lld/test/ELF/invalid-eh-frame6.s
    lld/test/ELF/tls.s
    lld/test/ELF/undef-multi.s
    lld/test/ELF/undef.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 75121285b7b23d..dc2a0edd9bf248 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -336,22 +336,22 @@ std::string InputSectionBase::getSrcMsg(const Symbol &sym,
 // or
 //
 //   path/to/foo.o:(function bar) in archive path/to/bar.a
-std::string InputSectionBase::getObjMsg(uint64_t off) const {
-  std::string filename = std::string(file->getName());
-
-  std::string archive;
-  if (!file->archiveName.empty())
-    archive = (" in archive " + file->archiveName).str();
+const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
+                                     InputSectionBase::ObjMsg &&msg) {
+  auto *sec = msg.sec;
+  s << sec->file->getName() << ":(";
 
   // Find a symbol that encloses a given location. getObjMsg may be called
   // before ObjFile::initSectionsAndLocalSyms where local symbols are
   // initialized.
-  if (Defined *d = getEnclosingSymbol(off))
-    return filename + ":(" + toStr(getCtx(), *d) + ")" + archive;
-
-  // If there's no symbol, print out the offset in the section.
-  return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive)
-      .str();
+  if (Defined *d = sec->getEnclosingSymbol(msg.offset))
+    s << d;
+  else
+    s << sec->name << "+0x" << Twine::utohexstr(msg.offset);
+  s << ')';
+  if (!sec->file->archiveName.empty())
+    s << (" in archive " + sec->file->archiveName).str();
+  return s;
 }
 
 PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source,

diff  --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index 268caa547ffed9..91678aeeaf7671 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -143,6 +143,11 @@ struct RelaxAux {
 // This corresponds to a section of an input file.
 class InputSectionBase : public SectionBase {
 public:
+  struct ObjMsg {
+    const InputSectionBase *sec;
+    uint64_t offset;
+  };
+
   template <class ELFT>
   InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
                    StringRef name, Kind sectionKind);
@@ -248,7 +253,7 @@ class InputSectionBase : public SectionBase {
   // Returns a source location string. Used to construct an error message.
   std::string getLocation(uint64_t offset) const;
   std::string getSrcMsg(const Symbol &sym, uint64_t offset) const;
-  std::string getObjMsg(uint64_t offset) const;
+  ObjMsg getObjMsg(uint64_t offset) const { return {this, offset}; }
 
   // Each section knows how to relocate itself. These functions apply
   // relocations, assuming that Buf points to this section's copy in
@@ -515,6 +520,8 @@ inline bool isDebugSection(const InputSectionBase &sec) {
 std::string toStr(elf::Ctx &, const elf::InputSectionBase *);
 const ELFSyncStream &operator<<(const ELFSyncStream &,
                                 const InputSectionBase *);
+const ELFSyncStream &operator<<(const ELFSyncStream &,
+                                InputSectionBase::ObjMsg &&);
 } // namespace elf
 } // namespace lld
 

diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index faf7d61ffbe0bf..82215f305978f2 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -544,18 +544,16 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
   //   >>>            baz.o in archive libbaz.a
   auto *sec1 = cast<InputSectionBase>(d->section);
   std::string src1 = sec1->getSrcMsg(sym, d->value);
-  std::string obj1 = sec1->getObjMsg(d->value);
   std::string src2 = errSec->getSrcMsg(sym, errOffset);
-  std::string obj2 = errSec->getObjMsg(errOffset);
 
   auto diag = Err(ctx);
   diag << "duplicate symbol: " << &sym << "\n>>> defined at ";
   if (!src1.empty())
     diag << src1 << "\n>>>            ";
-  diag << obj1 << "\n>>> defined at ";
+  diag << sec1->getObjMsg(d->value) << "\n>>> defined at ";
   if (!src2.empty())
     diag << src2 << "\n>>>            ";
-  diag << obj2;
+  diag << errSec->getObjMsg(errOffset);
 }
 
 void Symbol::checkDuplicate(Ctx &ctx, const Defined &other) const {

diff  --git a/lld/test/ELF/invalid-eh-frame3.s b/lld/test/ELF/invalid-eh-frame3.s
index 7cc27fc4222947..6f7c7d957ab694 100644
--- a/lld/test/ELF/invalid-eh-frame3.s
+++ b/lld/test/ELF/invalid-eh-frame3.s
@@ -4,7 +4,7 @@
 # RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK:      error: corrupted .eh_frame: corrupted CIE (failed to read LEB128)
-# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xC)
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xc)
 
 .section .eh_frame,"a", at unwind
   .byte 0x08

diff  --git a/lld/test/ELF/invalid-eh-frame6.s b/lld/test/ELF/invalid-eh-frame6.s
index 68b79c725d0c60..77be15f54e6b12 100644
--- a/lld/test/ELF/invalid-eh-frame6.s
+++ b/lld/test/ELF/invalid-eh-frame6.s
@@ -4,7 +4,7 @@
 # RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: corrupted .eh_frame: unknown FDE encoding
-# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xE)
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xe)
 
 .section .eh_frame,"a", at unwind
   .byte 0x0E

diff  --git a/lld/test/ELF/tls.s b/lld/test/ELF/tls.s
index 4f50e9d29c030f..63118905911a7e 100644
--- a/lld/test/ELF/tls.s
+++ b/lld/test/ELF/tls.s
@@ -14,7 +14,7 @@
 // ERR-EMPTY:
 // ERR-NEXT:  error: relocation R_X86_64_TPOFF32 against b cannot be used with -shared
 // ERR-NEXT:  defined in {{.*}}
-// ERR-NEXT:  referenced by {{.*}}:(.text+0xC)
+// ERR-NEXT:  referenced by {{.*}}:(.text+0xc)
 // ERR-EMPTY:
 // ERR-NEXT:  error: relocation R_X86_64_TPOFF32 against c cannot be used with -shared
 // ERR-NEXT:  defined in {{.*}}
@@ -22,7 +22,7 @@
 // ERR-EMPTY:
 // ERR-NEXT:  error: relocation R_X86_64_TPOFF32 against d cannot be used with -shared
 // ERR-NEXT:  defined in {{.*}}
-// ERR-NEXT:  referenced by {{.*}}:(.text+0x1C)
+// ERR-NEXT:  referenced by {{.*}}:(.text+0x1c)
 
 .global _start
 _start:

diff  --git a/lld/test/ELF/undef-multi.s b/lld/test/ELF/undef-multi.s
index af21693d089136..68f6a9ca79a710 100644
--- a/lld/test/ELF/undef-multi.s
+++ b/lld/test/ELF/undef-multi.s
@@ -9,7 +9,7 @@
 # CHECK-NEXT: >>> referenced by undef-multi.s
 # CHECK-NEXT: >>>               {{.*}}:(.text+0x6)
 # CHECK-NEXT: >>> referenced by undef-multi.s
-# CHECK-NEXT: >>>               {{.*}}:(.text+0xB)
+# CHECK-NEXT: >>>               {{.*}}:(.text+0xb)
 # CHECK-NEXT: >>> referenced 2 more times
 
 # All references to a single undefined symbol count as a single error -- but
@@ -33,7 +33,7 @@
 # LIMIT-NEXT: >>> referenced by undef-multi.s
 # LIMIT-NEXT: >>>               {{.*}}:(.text+0x6)
 # LIMIT-NEXT: >>> referenced by undef-multi.s
-# LIMIT-NEXT: >>>               {{.*}}:(.text+0xB)
+# LIMIT-NEXT: >>>               {{.*}}:(.text+0xb)
 # LIMIT-NEXT: >>> referenced 9 more times
 
 .file "undef-multi.s"

diff  --git a/lld/test/ELF/undef.s b/lld/test/ELF/undef.s
index 009b8bf6d23b41..5762d3b7a689c6 100644
--- a/lld/test/ELF/undef.s
+++ b/lld/test/ELF/undef.s
@@ -30,11 +30,11 @@
 
 # CHECK:      error: undefined symbol: foo(int)
 # CHECK-NEXT: >>> referenced by undef.s
-# CHECK-NEXT: >>>               {{.*}}:(.text+0x1A)
+# CHECK-NEXT: >>>               {{.*}}:(.text+0x1a)
 
 # CHECK:      error: undefined symbol: Pi
 # CHECK-NEXT: >>> referenced by undef.s
-# CHECK-NEXT: >>>               {{.*}}:(.text+0x1F)
+# CHECK-NEXT: >>>               {{.*}}:(.text+0x1f)
 
 # CHECK:      error: undefined symbol: D main
 # CHECK-NEXT: >>> referenced by undef.s


        


More information about the llvm-commits mailing list