Skip to content

Commit 05ff6e7

Browse files
committed
[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.
1 parent 6bfb6d4 commit 05ff6e7

File tree

8 files changed

+30
-25
lines changed

8 files changed

+30
-25
lines changed

lld/ELF/InputSection.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,22 @@ std::string InputSectionBase::getSrcMsg(const Symbol &sym,
336336
// or
337337
//
338338
// path/to/foo.o:(function bar) in archive path/to/bar.a
339-
std::string InputSectionBase::getObjMsg(uint64_t off) const {
340-
std::string filename = std::string(file->getName());
341-
342-
std::string archive;
343-
if (!file->archiveName.empty())
344-
archive = (" in archive " + file->archiveName).str();
339+
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
340+
InputSectionBase::ObjMsg &&msg) {
341+
auto *sec = msg.sec;
342+
s << sec->file->getName() << ":(";
345343

346344
// Find a symbol that encloses a given location. getObjMsg may be called
347345
// before ObjFile::initSectionsAndLocalSyms where local symbols are
348346
// initialized.
349-
if (Defined *d = getEnclosingSymbol(off))
350-
return filename + ":(" + toStr(getCtx(), *d) + ")" + archive;
351-
352-
// If there's no symbol, print out the offset in the section.
353-
return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive)
354-
.str();
347+
if (Defined *d = sec->getEnclosingSymbol(msg.offset))
348+
s << d;
349+
else
350+
s << sec->name << "+0x" << Twine::utohexstr(msg.offset);
351+
s << ')';
352+
if (!sec->file->archiveName.empty())
353+
s << (" in archive " + sec->file->archiveName).str();
354+
return s;
355355
}
356356

357357
PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source,

lld/ELF/InputSection.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ struct RelaxAux {
143143
// This corresponds to a section of an input file.
144144
class InputSectionBase : public SectionBase {
145145
public:
146+
struct ObjMsg {
147+
const InputSectionBase *sec;
148+
uint64_t offset;
149+
};
150+
146151
template <class ELFT>
147152
InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
148153
StringRef name, Kind sectionKind);
@@ -248,7 +253,7 @@ class InputSectionBase : public SectionBase {
248253
// Returns a source location string. Used to construct an error message.
249254
std::string getLocation(uint64_t offset) const;
250255
std::string getSrcMsg(const Symbol &sym, uint64_t offset) const;
251-
std::string getObjMsg(uint64_t offset) const;
256+
ObjMsg getObjMsg(uint64_t offset) const { return {this, offset}; }
252257

253258
// Each section knows how to relocate itself. These functions apply
254259
// relocations, assuming that Buf points to this section's copy in
@@ -515,6 +520,8 @@ inline bool isDebugSection(const InputSectionBase &sec) {
515520
std::string toStr(elf::Ctx &, const elf::InputSectionBase *);
516521
const ELFSyncStream &operator<<(const ELFSyncStream &,
517522
const InputSectionBase *);
523+
const ELFSyncStream &operator<<(const ELFSyncStream &,
524+
InputSectionBase::ObjMsg &&);
518525
} // namespace elf
519526
} // namespace lld
520527

lld/ELF/Symbols.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,16 @@ void elf::reportDuplicate(Ctx &ctx, const Symbol &sym, const InputFile *newFile,
544544
// >>> baz.o in archive libbaz.a
545545
auto *sec1 = cast<InputSectionBase>(d->section);
546546
std::string src1 = sec1->getSrcMsg(sym, d->value);
547-
std::string obj1 = sec1->getObjMsg(d->value);
548547
std::string src2 = errSec->getSrcMsg(sym, errOffset);
549-
std::string obj2 = errSec->getObjMsg(errOffset);
550548

551549
auto diag = Err(ctx);
552550
diag << "duplicate symbol: " << &sym << "\n>>> defined at ";
553551
if (!src1.empty())
554552
diag << src1 << "\n>>> ";
555-
diag << obj1 << "\n>>> defined at ";
553+
diag << sec1->getObjMsg(d->value) << "\n>>> defined at ";
556554
if (!src2.empty())
557555
diag << src2 << "\n>>> ";
558-
diag << obj2;
556+
diag << errSec->getObjMsg(errOffset);
559557
}
560558

561559
void Symbol::checkDuplicate(Ctx &ctx, const Defined &other) const {

lld/test/ELF/invalid-eh-frame3.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
55

66
# CHECK: error: corrupted .eh_frame: corrupted CIE (failed to read LEB128)
7-
# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xC)
7+
# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xc)
88

99
.section .eh_frame,"a",@unwind
1010
.byte 0x08

lld/test/ELF/invalid-eh-frame6.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
55

66
# CHECK: error: corrupted .eh_frame: unknown FDE encoding
7-
# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xE)
7+
# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xe)
88

99
.section .eh_frame,"a",@unwind
1010
.byte 0x0E

lld/test/ELF/tls.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
// ERR-EMPTY:
1515
// ERR-NEXT: error: relocation R_X86_64_TPOFF32 against b cannot be used with -shared
1616
// ERR-NEXT: defined in {{.*}}
17-
// ERR-NEXT: referenced by {{.*}}:(.text+0xC)
17+
// ERR-NEXT: referenced by {{.*}}:(.text+0xc)
1818
// ERR-EMPTY:
1919
// ERR-NEXT: error: relocation R_X86_64_TPOFF32 against c cannot be used with -shared
2020
// ERR-NEXT: defined in {{.*}}
2121
// ERR-NEXT: referenced by {{.*}}:(.text+0x14)
2222
// ERR-EMPTY:
2323
// ERR-NEXT: error: relocation R_X86_64_TPOFF32 against d cannot be used with -shared
2424
// ERR-NEXT: defined in {{.*}}
25-
// ERR-NEXT: referenced by {{.*}}:(.text+0x1C)
25+
// ERR-NEXT: referenced by {{.*}}:(.text+0x1c)
2626

2727
.global _start
2828
_start:

lld/test/ELF/undef-multi.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# CHECK-NEXT: >>> referenced by undef-multi.s
1010
# CHECK-NEXT: >>> {{.*}}:(.text+0x6)
1111
# CHECK-NEXT: >>> referenced by undef-multi.s
12-
# CHECK-NEXT: >>> {{.*}}:(.text+0xB)
12+
# CHECK-NEXT: >>> {{.*}}:(.text+0xb)
1313
# CHECK-NEXT: >>> referenced 2 more times
1414

1515
# All references to a single undefined symbol count as a single error -- but
@@ -33,7 +33,7 @@
3333
# LIMIT-NEXT: >>> referenced by undef-multi.s
3434
# LIMIT-NEXT: >>> {{.*}}:(.text+0x6)
3535
# LIMIT-NEXT: >>> referenced by undef-multi.s
36-
# LIMIT-NEXT: >>> {{.*}}:(.text+0xB)
36+
# LIMIT-NEXT: >>> {{.*}}:(.text+0xb)
3737
# LIMIT-NEXT: >>> referenced 9 more times
3838

3939
.file "undef-multi.s"

lld/test/ELF/undef.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030

3131
# CHECK: error: undefined symbol: foo(int)
3232
# CHECK-NEXT: >>> referenced by undef.s
33-
# CHECK-NEXT: >>> {{.*}}:(.text+0x1A)
33+
# CHECK-NEXT: >>> {{.*}}:(.text+0x1a)
3434

3535
# CHECK: error: undefined symbol: Pi
3636
# CHECK-NEXT: >>> referenced by undef.s
37-
# CHECK-NEXT: >>> {{.*}}:(.text+0x1F)
37+
# CHECK-NEXT: >>> {{.*}}:(.text+0x1f)
3838

3939
# CHECK: error: undefined symbol: D main
4040
# CHECK-NEXT: >>> referenced by undef.s

0 commit comments

Comments
 (0)