summaryrefslogtreecommitdiff
path: root/lld/Common/ErrorHandler.cpp
AgeCommit message (Collapse)Author
2025-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata
2025-01-25[ELF] Disable error handling script if disableOutputFangrui Song
Fix ELF/error-handling-script-linux.test when LLD_IN_TEST=2 is set.
2024-12-02[lld] Move handling of message separator to reportDiagnostic()Alexander Richardson
This ensures that it is also updated when calling log() and not just for errors and warnings. This was noticed in the downstream CHERI fork where a test using CHECK-NEXT was seeing empty lines after single-line log() messages. Reviewed By: MaskRay Pull Request: https://github.com/llvm/llvm-project/pull/117587
2024-11-24[ELF] Simplif reportUndefinedSymbol. NFCFangrui Song
2024-11-23[ELF] Simplify reportMissingFeature. NFCFangrui Song
2024-11-17[ELF] Change Msg to respect stdoutOSFangrui Song
2024-11-16[lld] Use context-aware outs() and errs()Fangrui Song
For COFF and ELF that are mostly free of global states, lld::errs() and lld::outs() should not be used. This migration change allows us to remove lld::errs, which uses the global errorHandler().
2024-11-16[lld] Use context-aware outs() and errs()Fangrui Song
2024-11-16[ELF] Make checkError context-awareFangrui Song
2024-11-16[ELF] Replace message(...) with Msg(ctx)Fangrui Song
2024-11-06[ELF] Add context-aware diagnostic functions (#112319)Fangrui Song
The current diagnostic functions log/warn/error/fatal lack a context argument and call the global `lld::errorHandler()`, which prevents multiple lld instances in one process. This patch introduces context-aware replacements: * log => Log(ctx) * warn => Warn(ctx) * errorOrWarn => Err(ctx) * error => ErrAlways(ctx) * fatal => Fatal(ctx) Example: `errorOrWarn(toString(f) + "xxx")` => `Err(ctx) << f << "xxx"`. (`toString(f)` is shortened to `f` as a bonus and may access `ctx` without accessing the global variable (see `Target.cpp`)). `ctx.e = &context->e;` can be replaced with a non-global Errorhandler when `ctx` becomes a local variable. (For the ELF port, the long term goal is to eliminate `error`. Most can be straightforwardly converted to `Err(ctx)`.)
2023-02-04[lld] fix comment typos to cycle botsNico Weber
2022-06-11[lld-macho] Add support for -wKeith Smiley
This flag suppresses warnings produced by the linker. In ld64 this has an interesting interaction with -fatal_warnings, it silences the warnings but the link still fails. Instead of doing that here we still print the warning and eagerly fail the link in case both are passed, this seems more reasonable so users can understand why the link fails. Differential Revision: https://reviews.llvm.org/D127564
2022-02-17[lld] Make error handling functions opaqueFangrui Song
The inline `lld::error` expands to two function calls `errorHandler` and `error` where the latter is opaque. Move the functions to .cpp files to decrease code size. My x86-64 lld executable is 9KiB smaller. Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D120002
2022-01-28[lld] Add module name to LTO inline asm diagnosticFangrui Song
Close #52781: for LTO, the inline asm diagnostic uses `<inline asm>` as the file name (lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp) and it is unclear which module has the issue. With this patch, we will see the module name (say `asm.o`) before `<inline asm>` with ThinLTO. ``` % clang -flto=thin -c asm.c && myld.lld asm.o -e f ld.lld: error: asm.o <inline asm>:1:2: invalid instruction mnemonic 'invalid' invalid ^~~~~~~ ``` For regular LTO, unfortunately the original module name is lost and we only get ld-temp.o. Reviewed By: #lld-macho, ychen, Jez Ng Differential Revision: https://reviews.llvm.org/D118434
2022-01-20Re-land [LLD] Remove global state in lldCommonAlexandre Ganea
Move all variables at file-scope or function-static-scope into a hosting structure (lld::CommonLinkerContext) that lives at lldMain()-scope. Drivers will inherit from this structure and add their own global state, in the same way as for the existing COFFLinkerContext. See discussion in https://lists.llvm.org/pipermail/llvm-dev/2021-June/151184.html The previous land f860fe362282ed69b9d4503a20e5d20b9a041189 caused issues in https://lab.llvm.org/buildbot/#/builders/123/builds/8383, fixed by 22ee510dac9440a74b2e5b3fe3ff13ccdbf55af3. Differential Revision: https://reviews.llvm.org/D108850
2022-01-16Revert [LLD] Remove global state in lldCommonAlexandre Ganea
It seems to be causing issues on https://lab.llvm.org/buildbot/#/builders/123/builds/8383
2022-01-16[LLD] Remove global state in lldCommonAlexandre Ganea
Move all variables at file-scope or function-static-scope into a hosting structure (lld::CommonLinkerContext) that lives at lldMain()-scope. Drivers will inherit from this structure and add their own global state, in the same way as for the existing COFFLinkerContext. See discussion in https://lists.llvm.org/pipermail/llvm-dev/2021-June/151184.html Differential Revision: https://reviews.llvm.org/D108850
2021-11-02[lld/mac] Write -v output to stderrNico Weber
This matches ld64, and it's conceivable that projects try to read this information off stderr for that reason. --version keeps writing to stdout. Differential Revision: https://reviews.llvm.org/D113020
2021-09-09[lld] Buffer writes when composing a single diagnosticFangrui Song
llvm::errs() is unbuffered. On a POSIX platform, composing a diagnostic string may invoke the ::write syscall multiple times, which can be slow. Buffer writes to a temporary SmallString when composing a single diagnostic to reduce the number of ::write syscalls to one (also easier to read under strace/truss). For an invocation of ld.lld with 62000+ lines of `ld.lld: warning: symbol ordering file: no such symbol: ` warnings (D87121), the buffering decreases the write time from 1s to 0.4s (for /dev/tty) and from 0.4s to 0.1s (for a tmpfs file). This can speed up `relocation R_X86_64_PC32 out of range` diagnostic printing as well with `--noinhibit-exec --no-fatal-warnings`. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D87272
2020-11-12[LLD][COFF] When using LLD-as-a-library, always prevent re-entrance on failuresAlexandre Ganea
This is a follow-up for D70378 (Cover usage of LLD as a library). While debugging an intermittent failure on a bot, I recalled this scenario which causes the issue: 1.When executing lld/test/ELF/invalid/symtab-sh-info.s L45, we reach lld::elf::Obj-File::ObjFile() which goes straight into its base ELFFileBase(), then ELFFileBase::init(). 2.At that point fatal() is thrown in lld/ELF/InputFiles.cpp L381, leaving a half-initialized ObjFile instance. 3.We then end up in lld::exitLld() and since we are running with LLD_IN_TEST, we hapily restore the control flow to CrashRecoveryContext::RunSafely() then back in lld::safeLldMain(). 4.Before this patch, we called errorHandler().reset() just after, and this attempted to reset the associated SpecificAlloc<ObjFile<ELF64LE>>. That tried to free the half-initialized ObjFile instance, and more precisely its ObjFile::dwarf member. Sometimes that worked, sometimes it failed and was catched by the CrashRecoveryContext. This scenario was the reason we called errorHandler().reset() through a CrashRecoveryContext. But in some rare cases, the above repro somehow corrupted the heap, creating a stack overflow. When the CrashRecoveryContext's filter (that is, __except (ExceptionFilter(GetExceptionInformation()))) tried to handle the exception, it crashed again since the stack was exhausted -- and that took the whole application down. That is the issue seen on the bot. Locally it happens about 1 times out of 15. Now this situation can happen anywhere in LLD. Since catching stack overflows is not a reliable scenario ATM when using CrashRecoveryContext, we're now preventing further re-entrance when such failures occur, by signaling lld::SafeReturn::canRunAgain=false. When running with LLD_IN_TEST=2 (or above), only one iteration will be executed, instead of two. Differential Revision: https://reviews.llvm.org/D88348
2020-11-09[lld] Provide a hook to customize undefined symbols error handlingserge-sans-paille
This is a follow up to https://reviews.llvm.org/D87758, implementing the missing symbol part, as done by binutils. Differential Revision: https://reviews.llvm.org/D89687
2020-11-03Fix 'default label in switch which covers all enumeration values' warningserge-sans-paille
2020-11-03Provide a hook to customize missing library error handlingserge-sans-paille
Make it possible for lld users to provide a custom script that would help to find missing libraries. A possible scenario could be: % clang /tmp/a.c -fuse-ld=lld -loauth -Wl,--error-handling-script=/tmp/addLibrary.py unable to find library -loauth looking for relevant packages to provides that library liboauth-0.9.7-4.el7.i686 liboauth-devel-0.9.7-4.el7.i686 liboauth-0.9.7-4.el7.x86_64 liboauth-devel-0.9.7-4.el7.x86_64 pix-1.6.1-3.el7.x86_64 Where addLibrary would be called with the missing library name as first argument (in that case addLibrary.py oauth) Differential Revision: https://reviews.llvm.org/D87758
2020-09-24[LLD][COFF] Cover usage of LLD-as-a-library in testsAlexandre Ganea
In lit tests, we run each LLD invocation twice (LLD_IN_TEST=2), without shutting down the process in-between. This ensures a full cleanup is properly done between runs. Only active for the COFF driver for now. Other drivers still use LLD_IN_TEST=1 which executes just one iteration with full cleanup, like before. When the environment variable LLD_IN_TEST is unset, a shortcut is taken, only one iteration is executed, no cleanup for faster exit, like before. A public API, lld::safeLldMain(), is also available when using LLD as a library. Differential Revision: https://reviews.llvm.org/D70378
2020-05-05[Support] Move LLD's parallel algorithm wrappers to supportReid Kleckner
Essentially takes the lld/Common/Threads.h wrappers and moves them to the llvm/Support/Paralle.h algorithm header. The changes are: - Remove policy parameter, since all clients use `par`. - Rename the methods to `parallelSort` etc to match LLVM style, since they are no longer C++17 pstl compatible. - Move algorithms from llvm::parallel:: to llvm::, since they have "parallel" in the name and are no longer overloads of the regular algorithms. - Add range overloads - Use the sequential algorithm directly when 1 thread is requested (skips task grouping) - Fix the index type of parallelForEachN to size_t. Nobody in LLVM was using any other parameter, and it made overload resolution hard for for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t. Remove Threads.h and update LLD for that. This is a prerequisite for parallel public symbol processing in the PDB library, which is in LLVM. Reviewed By: MaskRay, aganea Differential Revision: https://reviews.llvm.org/D79390
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2020-01-28[LLD] Avoid exiting with a locked mutex NFCAndrew Ng
In ErrorHandler::error(), rearrange code to avoid calling exitLld with the mutex locked. Acquire mutex lock when flushing the output streams in exitLld. Differential Revision: https://reviews.llvm.org/D73281
2020-01-10[Support] ThreadPoolExecutor fixes for Windows/MinGWAndrew Ng
Changed ThreadPoolExecutor to no longer use detached threads and instead to join threads on destruction. This is to prevent intermittent crashing on Windows when doing a normal full exit, e.g. via exit(). Changed ThreadPoolExecutor to be a ManagedStatic so that it can be stopped on llvm_shutdown(). Without this, it would only be stopped in the destructor when doing a full exit. This is required to avoid intermittent crashing on Windows due to a race condition between the ThreadPoolExecutor starting up threads and the process doing a fast exit, e.g. via _exit(). The Windows crashes appear to only occur with the MSVC static runtimes and are more frequent with the debug static runtime. These changes also prevent intermittent deadlocks on exit with the MinGW runtime. Differential Revision: https://reviews.llvm.org/D70447
2019-11-21LLD: Don't use the stderrOS stream in link before it's reassigned.James Y Knight
Remove the lld::enableColors function, as it just obscures which stream it's affecting, and replace with explicit calls to the stream's enable_colors. Also, assign the stderrOS and stdoutOS globals first in link function, just to ensure nothing might use them. (Either change individually fixes the issue of using the old stream, but both together seems best.) Follow-up to b11386f9be9b2dc7276a758d64f66833da10bdea. Differential Revision: https://reviews.llvm.org/D70492
2019-11-18Make it possible to redirect not only errs() but also outs()Rui Ueyama
This change is for those who use lld as a library. Context: https://reviews.llvm.org/D70287 This patch adds a new parmeter to lld::*::link() so that we can pass an raw_ostream object representing stdout. Previously, lld::*::link() took only an stderr object. Justification for making stdoutOS and stderrOS mandatory: I wanted to make link() functions to take stdout and stderr in that order. However, if we change the function signature from bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stderrOS = llvm::errs()); to bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS = llvm::outs(), raw_ostream &stderrOS = llvm::errs()); , then the meaning of existing code that passes stderrOS silently changes (stderrOS would be interpreted as stdoutOS). So, I chose to make existing code not to compile, so that developers can fix their code. Differential Revision: https://reviews.llvm.org/D70292
2019-08-09[ELF] For VS-style diagnostics, prefer printing full paths in the header.Igor Kudrin
The filename part in the message header is used by Visual Studio to fill Error List so that a user can click on an item and jump to the mentioned location. If we use only the name of a source file and not the full path, Visual Studio might be unable to find the right file or, even worse, show a wrong one. Differential Revision: https://reviews.llvm.org/D65875 llvm-svn: 368409
2019-08-07[ELF] Fix splitting messages for duplicate symbols.Igor Kudrin
D65213 (rL367536) does not work for the case when a source file path includes subdirectories. Differential Revision: https://reviews.llvm.org/D65810 llvm-svn: 368153
2019-08-07Simplify error message output. NFC.Rui Ueyama
Differential Revision: https://reviews.llvm.org/D65855 llvm-svn: 368144
2019-08-07Re-submit r367649: Improve raw_ostream so that you can "write" colors using ↵Rui Ueyama
operator<< The original patch broke buildbots, perhaps because it changed the default setting whether colors are enabled or not. llvm-svn: 368131
2019-08-02Revert r367649: Improve raw_ostream so that you can "write" colors using ↵Rui Ueyama
operator<< This reverts commit r367649 in an attempt to unbreak Windows bots. llvm-svn: 367658
2019-08-02Add an assert() to catch possible regexp errors.Rui Ueyama
llvm-svn: 367651
2019-08-02Add a comment for --vs-diagnostics.Rui Ueyama
llvm-svn: 367650
2019-08-02Improve raw_ostream so that you can "write" colors using operator<<Rui Ueyama
1. raw_ostream supports ANSI colors so that you can write messages to the termina with colors. Previously, in order to change and reset color, you had to call `changeColor` and `resetColor` functions, respectively. So, if you print out "error: " in red, for example, you had to do something like this: OS.changeColor(raw_ostream::RED); OS << "error: "; OS.resetColor(); With this patch, you can write the same code as follows: OS << raw_ostream::RED << "error: " << raw_ostream::RESET; 2. Add a boolean flag to raw_ostream so that you can disable colored output. If you disable colors, changeColor, operator<<(Color), resetColor and other color-related functions have no effect. Most LLVM tools automatically prints out messages using colors, and you can disable it by passing a flag such as `--disable-colors`. This new flag makes it easy to write code that works that way. Differential Revision: https://reviews.llvm.org/D65564 llvm-svn: 367649
2019-08-01[ELF] With --vs-diagnostics, print a separate message for each location of a ↵Igor Kudrin
duplicate symbol. We extract and print the source location in the message header so that Visual Studio is able to parse it and jump there. As duplicate symbols are defined in several locations, it is more convenient to have separate error messages, which allows a user to easily access all the locations. Differential Revision: https://reviews.llvm.org/D65213 llvm-svn: 367536
2019-08-01[ELF] Fix finding locations in messages for undefined hidden symbols.Igor Kudrin
Previously, when `--vs-diagnostics` was used, the linker printed something like hidden(undef.s): error: undefined hidden symbol: foo >>> referenced by undef.s:15 Differential Revision: https://reviews.llvm.org/D65499 llvm-svn: 367515
2019-07-24[LLD] Do not print additional newlines after reaching error limitAlexander Richardson
Summary: This could previously happen if errors that are emitted after reaching the error limit. In that case, the flag inside the newline() function will be set to true which causes the next call to print a newline even though the actual message will be discarded. Reviewers: ruiu, grimar, MaskRay, espindola Reviewed By: ruiu Subscribers: emaste, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D65198 llvm-svn: 366944
2019-07-17[lld] Add Visual Studio compatible diagnosticsChris Jackson
Summary: Add a --vs-diagnostics flag that alters the format of diagnostic output to enable source hyperlinks in Visual Studio. Differential Revision: https://reviews.llvm.org/D58484 Reviewed by: ruiu llvm-svn: 366333
2019-07-11[Coding style change][lld] Rename variables for non-ELF portsRui Ueyama
This patch does the same thing as r365595 to other subdirectories, which completes the naming style change for the entire lld directory. With this, the naming style conversion is complete for lld. Differential Revision: https://reviews.llvm.org/D64473 llvm-svn: 365730
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-08-24[Common] Discard the temp file while keeping the memory mapping open, on errorsMartin Storsjo
Differential Revision: https://reviews.llvm.org/D51095 llvm-svn: 340635
2018-07-02[LTO] Errors in LLVM backend should manifest as lld errorsSam Clegg
Differential Revision: https://reviews.llvm.org/D48812 llvm-svn: 336143
2018-05-22Code cleanup in preparation for adding LTO for wasm. NFC.Sam Clegg
- Move some common code into Common/rrorHandler.cpp and Common/Strings.h. - Don't use `fatal` when incompatible bitcode files are encountered. - Rename NameRef variable to just Name See D47162 Differential Revision: https://reviews.llvm.org/D47206 llvm-svn: 333021
2017-12-11Use ErrorOS for log messages as well as errorSam Clegg
log are also diagnostics so it seems like they should to the same place as errors and debug messages. Without this change when I enable --verbose those messages go to stdout, but when I enable "-mllvm -debug" those messages go to stderr (because dbgs() goes to stderr by default). So I end up having to do this a lot: lld <args> > output_message 2>&1 Differential Revision: https://reviews.llvm.org/D41033 llvm-svn: 320427
2017-11-13Try harder to delete the temporary file.Rafael Espindola
It is really hard to cover restarts in a debugger, SIGKILL or power failures. I will try to handle them in a followup patch, but it will not support all the systems lld has to run on. RemoveFileOnSignal takes care of crashes. So what is left is making sure all regular exits delete the file. This patch does that by moving the buffer to error handling. That is a bit of a hack, but seemed better than to generalize it to take a callback on construction. I will implement this on COFF on the next patch. llvm-svn: 318060