2015-08-06 15:08:23 +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-02-25 18:56:01 +00:00
|
|
|
#include "Config.h"
|
2015-08-06 15:08:23 +00:00
|
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2016-07-14 02:35:18 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-11-10 19:39:05 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2015-08-06 15:08:23 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-11-23 18:34:28 +00:00
|
|
|
#include <mutex>
|
2015-08-06 15:08:23 +00:00
|
|
|
|
2016-10-27 13:32:32 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-25 20:27:32 +00:00
|
|
|
using namespace lld::elf;
|
2016-07-07 14:06:38 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
2015-08-06 15:08:23 +00:00
|
|
|
namespace lld {
|
|
|
|
|
|
2016-11-23 18:15:37 +00:00
|
|
|
uint64_t elf::ErrorCount;
|
2016-07-18 18:24:41 +00:00
|
|
|
raw_ostream *elf::ErrorOS;
|
2016-10-19 20:05:43 +00:00
|
|
|
StringRef elf::Argv0;
|
2016-01-28 18:40:06 +00:00
|
|
|
|
2016-11-23 18:34:28 +00:00
|
|
|
// 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;
|
|
|
|
|
|
2016-11-25 20:27:32 +00:00
|
|
|
static void print(StringRef S, raw_ostream::Colors C) {
|
2016-11-25 20:37:16 +00:00
|
|
|
*ErrorOS << Argv0 + ": ";
|
2016-11-26 15:10:01 +00:00
|
|
|
if (Config->ColorDiagnostics) {
|
2016-11-25 20:27:32 +00:00
|
|
|
ErrorOS->changeColor(C, true);
|
|
|
|
|
*ErrorOS << S;
|
|
|
|
|
ErrorOS->resetColor();
|
|
|
|
|
} else {
|
2016-11-25 20:37:16 +00:00
|
|
|
*ErrorOS << S;
|
2016-11-25 20:27:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-18 18:24:41 +00:00
|
|
|
void elf::log(const Twine &Msg) {
|
2016-11-23 18:34:28 +00:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2016-02-25 18:56:01 +00:00
|
|
|
if (Config->Verbose)
|
2016-10-19 20:05:43 +00:00
|
|
|
outs() << Argv0 << ": " << Msg << "\n";
|
2016-02-25 18:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-29 21:00:23 +00:00
|
|
|
void elf::warn(const Twine &Msg) {
|
2016-11-23 18:34:28 +00:00
|
|
|
if (Config->FatalWarnings) {
|
2016-07-04 13:43:12 +00:00
|
|
|
error(Msg);
|
2016-11-23 18:34:28 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2016-11-25 20:27:32 +00:00
|
|
|
print("warning: ", raw_ostream::MAGENTA);
|
|
|
|
|
*ErrorOS << Msg << "\n";
|
2016-07-04 13:43:12 +00:00
|
|
|
}
|
2015-09-24 18:55:33 +00:00
|
|
|
|
2016-07-18 18:24:41 +00:00
|
|
|
void elf::error(const Twine &Msg) {
|
2016-11-23 18:34:28 +00:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
|
|
|
|
|
2016-11-23 18:15:37 +00:00
|
|
|
if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
|
2016-11-25 20:27:32 +00:00
|
|
|
print("error: ", raw_ostream::RED);
|
|
|
|
|
*ErrorOS << Msg << "\n";
|
2016-11-23 18:15:37 +00:00
|
|
|
} else if (ErrorCount == Config->ErrorLimit) {
|
2016-11-25 20:27:32 +00:00
|
|
|
print("error: ", raw_ostream::RED);
|
|
|
|
|
*ErrorOS << "too many errors emitted, stopping now"
|
2016-11-24 20:31:43 +00:00
|
|
|
<< " (use -error-limit=0 to see all errors)\n";
|
2016-11-23 18:15:37 +00:00
|
|
|
if (Config->ExitEarly)
|
|
|
|
|
exitLld(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++ErrorCount;
|
2015-08-06 15:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-27 13:32:32 +00:00
|
|
|
void elf::exitLld(int Val) {
|
2016-11-10 19:39:05 +00:00
|
|
|
// 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-11-11 02:16:15 +00:00
|
|
|
outs().flush();
|
|
|
|
|
errs().flush();
|
2016-10-27 13:32:32 +00:00
|
|
|
_exit(Val);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-18 18:24:41 +00:00
|
|
|
void elf::fatal(const Twine &Msg) {
|
2016-11-23 18:34:28 +00:00
|
|
|
std::lock_guard<std::mutex> Lock(Mu);
|
2016-11-25 20:27:32 +00:00
|
|
|
print("error: ", raw_ostream::RED);
|
|
|
|
|
*ErrorOS << Msg << "\n";
|
2016-10-27 13:32:32 +00:00
|
|
|
exitLld(1);
|
2016-01-28 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-06 15:08:23 +00:00
|
|
|
} // namespace lld
|