2015-12-30 11:57:38 +00:00
|
|
|
//===- Error.cpp ----------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
2016-07-23 20:48:50 +00:00
|
|
|
#include "Config.h"
|
2015-12-30 11:57:38 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2016-07-23 20:48:50 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-01-02 19:19:15 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2015-12-30 11:57:38 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-02 19:19:15 +00:00
|
|
|
#include <mutex>
|
2015-12-30 11:57:38 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2016-07-23 20:48:50 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-04-16 16:03:39 +00:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-07-23 20:48:50 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
uint64_t elf::ErrorCount;
|
|
|
|
raw_ostream *elf::ErrorOS;
|
|
|
|
StringRef elf::Argv0;
|
|
|
|
|
|
|
|
// The functions defined in this file can be called from multiple threads,
|
|
|
|
// but outs() or errs() are not thread-safe. We protect them using a mutex.
|
|
|
|
static std::mutex Mu;
|
|
|
|
|
2017-04-16 16:03:39 +00:00
|
|
|
// Prints "\n" or does nothing, depending on Msg contents of
|
|
|
|
// the previous call of this function.
|
|
|
|
static void newline(const Twine &Msg) {
|
|
|
|
// True if the previous error message contained "\n".
|
|
|
|
// We want to separate multi-line error messages with a newline.
|
|
|
|
static bool Flag;
|
|
|
|
|
|
|
|
if (Flag)
|
|
|
|
*ErrorOS << "\n";
|
|
|
|
Flag = (StringRef(Msg.str()).find('\n') != StringRef::npos);
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
static void print(StringRef S, raw_ostream::Colors C) {
|
|
|
|
*ErrorOS << Argv0 + ": ";
|
|
|
|
if (Config->ColorDiagnostics) {
|
|
|
|
ErrorOS->changeColor(C, true);
|
|
|
|
*ErrorOS << S;
|
|
|
|
ErrorOS->resetColor();
|
|
|
|
} else {
|
|
|
|
*ErrorOS << S;
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 11:57:38 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
void elf::log(const Twine &Msg) {
|
2017-04-16 16:03:39 +00:00
|
|
|
if (Config->Verbose) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2017-01-02 19:19:15 +00:00
|
|
|
outs() << Argv0 << ": " << Msg << "\n";
|
2017-05-03 21:54:55 +00:00
|
|
|
outs().flush();
|
2017-04-16 16:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::message(const Twine &Msg) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
outs() << Msg << "\n";
|
|
|
|
outs().flush();
|
2016-07-23 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
void elf::warn(const Twine &Msg) {
|
|
|
|
if (Config->FatalWarnings) {
|
2016-07-23 20:48:50 +00:00
|
|
|
error(Msg);
|
2017-01-02 19:19:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-16 16:03:39 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2017-04-16 16:03:39 +00:00
|
|
|
newline(Msg);
|
2017-01-02 19:19:15 +00:00
|
|
|
print("warning: ", raw_ostream::MAGENTA);
|
|
|
|
*ErrorOS << Msg << "\n";
|
2016-07-23 20:48:50 +00:00
|
|
|
}
|
2015-12-30 11:57:38 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
void elf::error(const Twine &Msg) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2017-04-16 16:03:39 +00:00
|
|
|
newline(Msg);
|
2017-01-02 19:19:15 +00:00
|
|
|
|
|
|
|
if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
|
|
|
|
print("error: ", raw_ostream::RED);
|
|
|
|
*ErrorOS << Msg << "\n";
|
|
|
|
} else if (ErrorCount == Config->ErrorLimit) {
|
|
|
|
print("error: ", raw_ostream::RED);
|
|
|
|
*ErrorOS << "too many errors emitted, stopping now"
|
|
|
|
<< " (use -error-limit=0 to see all errors)\n";
|
|
|
|
if (Config->ExitEarly)
|
|
|
|
exitLld(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
++ErrorCount;
|
2015-12-30 11:57:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
void elf::exitLld(int Val) {
|
|
|
|
// Dealloc/destroy ManagedStatic variables before calling
|
|
|
|
// _exit(). In a non-LTO build, this is a nop. In an LTO
|
|
|
|
// build allows us to get the output of -time-passes.
|
|
|
|
llvm_shutdown();
|
2016-07-23 20:48:50 +00:00
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
outs().flush();
|
|
|
|
errs().flush();
|
|
|
|
_exit(Val);
|
2016-07-23 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:19:15 +00:00
|
|
|
void elf::fatal(const Twine &Msg) {
|
2017-04-16 16:03:39 +00:00
|
|
|
error(Msg);
|
2017-01-02 19:19:15 +00:00
|
|
|
exitLld(1);
|
2016-07-23 20:48:50 +00:00
|
|
|
}
|