Skip to content

Commit 0735048

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
1 parent a54d88f commit 0735048

File tree

8 files changed

+86
-4
lines changed

8 files changed

+86
-4
lines changed

lld/Common/ErrorHandler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,21 @@ void ErrorHandler::fatal(const Twine &msg) {
335335
error(msg);
336336
exitLld(1);
337337
}
338+
339+
SyncStream::~SyncStream() {
340+
os.flush();
341+
switch (level) {
342+
case DiagLevel::Log:
343+
e.log(buf);
344+
break;
345+
case DiagLevel::Warn:
346+
e.warn(buf);
347+
break;
348+
case DiagLevel::Err:
349+
e.error(buf);
350+
break;
351+
case DiagLevel::Fatal:
352+
e.fatal(buf);
353+
break;
354+
}
355+
}

lld/ELF/Config.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ struct Ctx {
547547
LinkerScript *script;
548548
std::unique_ptr<TargetInfo> target;
549549

550+
ErrorHandler *e;
551+
550552
// These variables are initialized by Writer and should not be used before
551553
// Writer is initialized.
552554
uint8_t *bufferStart;
@@ -679,6 +681,25 @@ static inline void internalLinkerError(StringRef loc, const Twine &msg) {
679681
llvm::getBugReportMsg());
680682
}
681683

684+
struct ELFSyncStream : SyncStream {
685+
Ctx &ctx;
686+
ELFSyncStream(Ctx &ctx, DiagLevel level)
687+
: SyncStream(*ctx.e, level), ctx(ctx) {}
688+
};
689+
690+
template <typename T>
691+
std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>,
692+
const ELFSyncStream &>
693+
operator<<(const ELFSyncStream &s, T &&v) {
694+
s.os << std::forward<T>(v);
695+
return s;
696+
}
697+
698+
ELFSyncStream Log(Ctx &ctx);
699+
ELFSyncStream Warn(Ctx &ctx);
700+
ELFSyncStream Err(Ctx &ctx);
701+
ELFSyncStream Fatal(Ctx &ctx);
702+
682703
} // namespace lld::elf
683704

684705
#endif

lld/ELF/Driver.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ void elf::errorOrWarn(const Twine &msg) {
9191
error(msg);
9292
}
9393

94+
ELFSyncStream elf::Log(Ctx &ctx) { return {ctx, DiagLevel::Log}; }
95+
ELFSyncStream elf::Warn(Ctx &ctx) { return {ctx, DiagLevel::Warn}; }
96+
ELFSyncStream elf::Err(Ctx &ctx) {
97+
return {ctx, ctx.arg.noinhibitExec ? DiagLevel::Warn : DiagLevel::Err};
98+
}
99+
ELFSyncStream elf::Fatal(Ctx &ctx) { return {ctx, DiagLevel::Fatal}; }
100+
94101
Ctx::Ctx() : driver(*this) {}
95102

96103
void Ctx::reset() {
@@ -101,6 +108,8 @@ void Ctx::reset() {
101108
script = nullptr;
102109
target.reset();
103110

111+
e = nullptr;
112+
104113
bufferStart = nullptr;
105114
mainPart = nullptr;
106115
tlsPhdr = nullptr;
@@ -169,6 +178,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
169178
Ctx &ctx = elf::ctx;
170179
LinkerScript script(ctx);
171180
ctx.script = &script;
181+
ctx.e = &context->e;
172182
ctx.symAux.emplace_back();
173183
ctx.symtab = std::make_unique<SymbolTable>(ctx);
174184

@@ -2780,8 +2790,9 @@ static void readSecurityNotes(Ctx &ctx) {
27802790
if (ctx.arg.zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
27812791
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
27822792
if (ctx.arg.zBtiReport == "none")
2783-
warn(toString(f) + ": -z force-bti: file does not have "
2784-
"GNU_PROPERTY_AARCH64_FEATURE_1_BTI property");
2793+
Warn(ctx) << f
2794+
<< ": -z force-bti: file does not have "
2795+
"GNU_PROPERTY_AARCH64_FEATURE_1_BTI property";
27852796
} else if (ctx.arg.zForceIbt &&
27862797
!(features & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
27872798
if (ctx.arg.zCetReport == "none")
@@ -3048,7 +3059,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
30483059
oldFilenames.insert(f->getName());
30493060
for (InputFile *newFile : newInputFiles)
30503061
if (!oldFilenames.contains(newFile->getName()))
3051-
errorOrWarn("input file '" + newFile->getName() + "' added after LTO");
3062+
Err(ctx) << "input file '" << newFile->getName() << "' added after LTO";
30523063
}
30533064

30543065
// Handle --exclude-libs again because lto.tmp may reference additional

lld/ELF/InputFiles.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ std::string lld::toString(const InputFile *f) {
6666
return std::string(f->toStringCache);
6767
}
6868

69+
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
70+
const InputFile *f) {
71+
s << toString(f);
72+
return s;
73+
}
74+
6975
static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
7076
unsigned char size;
7177
unsigned char endian;

lld/ELF/InputFiles.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ ELFFileBase *createObjFile(Ctx &, MemoryBufferRef mb,
386386

387387
std::string replaceThinLTOSuffix(Ctx &, StringRef path);
388388

389+
const ELFSyncStream &operator<<(const ELFSyncStream &, const InputFile *);
390+
389391
} // namespace elf
390392
} // namespace lld
391393

lld/ELF/Target.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ std::string lld::toString(RelType type) {
4545
return std::string(s);
4646
}
4747

48+
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, RelType type) {
49+
StringRef buf = getELFRelocationTypeName(s.ctx.arg.emachine, type);
50+
if (buf == "Unknown")
51+
s << ("Unknown (" + Twine(type) + ")").str();
52+
else
53+
s << buf;
54+
return s;
55+
}
56+
4857
void elf::setTarget(Ctx &ctx) {
4958
switch (ctx.arg.emachine) {
5059
case EM_386:

lld/ELF/Target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ inline uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val) {
327327
*bufLoc = val;
328328
return val;
329329
}
330+
331+
const ELFSyncStream &operator<<(const ELFSyncStream &, RelType);
330332
} // namespace elf
331333
} // namespace lld
332334

lld/include/lld/Common/ErrorHandler.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@
7373
#include "llvm/ADT/STLExtras.h"
7474
#include "llvm/Support/Error.h"
7575
#include "llvm/Support/FileOutputBuffer.h"
76+
#include "llvm/Support/raw_ostream.h"
7677
#include <mutex>
7778

7879
namespace llvm {
7980
class DiagnosticInfo;
80-
class raw_ostream;
8181
}
8282

8383
namespace lld {
@@ -152,6 +152,19 @@ void message(const Twine &msg, llvm::raw_ostream &s = outs());
152152
void warn(const Twine &msg);
153153
uint64_t errorCount();
154154

155+
enum class DiagLevel { Log, Warn, Err, Fatal };
156+
157+
class SyncStream {
158+
ErrorHandler &e;
159+
DiagLevel level;
160+
std::string buf;
161+
162+
public:
163+
mutable llvm::raw_string_ostream os{buf};
164+
SyncStream(ErrorHandler &e, DiagLevel level) : e(e), level(level) {}
165+
~SyncStream();
166+
};
167+
155168
[[noreturn]] void exitLld(int val);
156169

157170
void diagnosticHandler(const llvm::DiagnosticInfo &di);

0 commit comments

Comments
 (0)