aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF/MapFile.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata1-1/+0
2024-11-23[ELF] Reorder SectionBase/InputSectionBase membersFangrui Song1-1/+3
Move `sectionKind` outside the bitfield and move bss/keepUnique to InputSectionBase. * sizeof(InputSection) decreases from 160 to 152 on 64-bit systems. * The numerous `sectionKind` accesses are faster.
2024-11-16[ELF] Replace contex-less toString(x) with toStr(ctx, x)Fangrui Song1-6/+6
so that we can remove the global `ctx` from toString implementations. Rename lld::toString (to lld::elf::toStr) to simplify name lookup (we have many llvm::toString and another lld::toString(const llvm::opt::Arg &)).
2024-11-06[ELF] Replace error(...) with ErrAlways or ErrFangrui Song1-1/+1
Most are migrated to ErrAlways mechanically. In the future we should change most to Err.
2024-10-19[ELF] Pass Ctx &Fangrui Song1-2/+2
2024-10-19[ELF] Pass Ctx & to Symbol::getVAFangrui Song1-2/+2
2024-10-11[ELF] Pass Ctx &Fangrui Song1-2/+2
2024-09-29[ELF] Pass Ctx & to MapFileFangrui Song1-17/+18
2024-09-21[ELF] Replace config-> with ctx.arg.Fangrui Song1-6/+6
2024-08-21[ELF] Move script into Ctx. NFCFangrui Song1-1/+1
Ctx was introduced in March 2022 as a more suitable place for such singletons. We now use default-initialization for `LinkerScript` and should pay attention to non-class types (e.g. `dot` is initialized by commit 503907dc505db1e439e7061113bf84dd105f2e35).
2024-08-05[LLD] Add CLASS syntax to SECTIONS (#95323)Daniel Thornburgh1-0/+2
This allows the input section matching algorithm to be separated from output section descriptions. This allows a group of sections to be assigned to multiple output sections, providing an explicit version of --enable-non-contiguous-regions's spilling that doesn't require altering global linker script matching behavior with a flag. It also makes the linker script language more expressive even if spilling is not intended, since input section matching can be done in a different order than sections are placed in an output section. The implementation reuses the backend mechanism provided by --enable-non-contiguous-regions, so it has roughly similar semantics and limitations. In particular, sections cannot be spilled into or out of INSERT, OVERWRITE_SECTIONS, or /DISCARD/. The former two aren't intrinsic, so it may be possible to relax those restrictions later.
2024-01-25[LLD][ELF] Silence warning when building with Clang ToTAlexandre Ganea1-1/+1
This fixes: ``` [5839/7452] Building CXX object tools\lld\ELF\CMakeFiles\lldELF.dir\MapFile.cpp.obj C:\git\llvm-project\lld\ELF\MapFile.cpp(124,38): warning: comparison of integers of different signs: 'uint32_t' (aka 'unsigned int') and 'const ssize_t' (aka 'const int') [-Wsign-compare] 124 | last.outputOff + last.size == p.outputOff) { | ~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~ 1 warning generated. ```
2023-10-17[ELF] Demote symbols in /DISCARD/ discarded sections to Undefined (#69295)Fangrui Song1-1/+1
When an input section is matched by /DISCARD/ in a linker script, GNU ld reports errors for relocations referencing symbols defined in the section: `.aaa' referenced in section `.bbb' of a.o: defined in discarded section `.aaa' of a.o Implement the error by demoting eligible symbols to `Undefined` and changing STB_WEAK to STB_GLOBAL. As a side benefit, in relocatable links, relocations referencing symbols defined relative to /DISCARD/ discarded sections no longer set symbol/type to zeros. It's arguable whether a weak reference to a discarded symbol should lead to errors. GNU ld reports an error and our demoting approach reports an error as well. Close #58891 Co-authored-by: Bevin Hansson <bevin.hansson@ericsson.com>
2023-07-11[ELF] Make subsequent opens to auxiliary files appendAlex Brachet1-1/+1
Previously, the same file could be used across diagnostic options but the file would be silently overwritten by the whichever option gets handled last. Differential Revision: https://reviews.llvm.org/D153873
2023-01-26[NFC] Optimize vector usage in lldJez Ng1-1/+1
By using emplace_back, as well as converting some loops to for-each, we can do more efficient vectorization. Make copy constructor for TemporaryFile noexcept. Reviewed By: #lld-macho, int3 Differential Revision: https://reviews.llvm.org/D139552
2022-12-01[lld][NFC] rename ELF alignment into addralignGuillaume Chatelet1-2/+2
2022-10-01[ELF] Remove ctx indirection. NFCFangrui Song1-2/+2
Add LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr indirection. We can move other global variables into ctx without indirection concern. In the long term we may consider passing Ctx as a parameter to various functions and eliminate global state as much as possible and then remove `Ctx::reset`.
2022-09-09[ELF] Merge Symbol::needs* into uint16_t flags. NFCFangrui Song1-1/+1
Split off from D133003 ([ELF] Parallelize relocation scanning) to make its diff smaller.
2022-06-29[ELF] Move InputFiles global variables (memoryBuffers, objectFiles, etc) ↵Fangrui Song1-2/+2
into Ctx. NFC
2022-06-19Rename parallelForEachN to just parallelForNico Weber1-1/+1
Patch created by running: rg -l parallelForEachN | xargs sed -i '' -c 's/parallelForEachN/parallelFor/' No behavior change. Differential Revision: https://reviews.llvm.org/D128140
2022-03-08[ELF] Don't use multiple inheritance for OutputSection. NFCFangrui Song1-1/+1
Add an OutputDesc class inheriting from SectionCommand. An OutputDesc wraps an OutputSection. This change allows InputSection::getParent to be inlined. Differential Revision: https://reviews.llvm.org/D120650
2022-02-27[ELF] Move --print-archive-stats= and --why-extract= beside --warn-backrefs ↵Fangrui Song1-50/+0
report So that early errors don't suppress their output.
2022-02-15[ELF] Parse archives as --start-lib object filesFangrui Song1-3/+16
https://maskray.me/blog/2022-01-16-archives-and-start-lib For every definition in an extracted archive member, we intern the symbol twice, once for the archive index entry, once for the .o symbol table after extraction. This is inefficient. Symbols in a --start-lib ObjFile/BitcodeFile are only interned once because the result is cached in symbols[i]. Just handle an archive using the --start-lib code path. We can therefore remove ArchiveFile and LazyArchive. For many projects, archive member extraction ratio is high and it is a net performance win. Linking a Release build of clang is 1.01x as fast. Note: --start-lib scans symbols in the same order that llvm-ar adds them to the index, so in the common case the semantics should be identical. If the archive symbol table was created in a different order, or is incomplete, this strategy may have different semantics. Such cases are considered user error. The `is neither ET_REL nor LLVM bitcode` error is changed to a warning. Previously an archive may have such members without a diagnostic. Using a warning prevents breakage. * For some tests, the diagnostics get improved where we did not consider the archive member name: `b.a:` => `b.a(b.o):`. * `no-obj.s`: the link is now allowed, matching GNU ld * `archive-no-index.s`: the `is neither ET_REL nor LLVM bitcode` diagnostic is demoted to a warning. * `incompatible.s`: even when an archive is unextracted, we may report an "incompatible with" error. --- I recently decreased sizeof(SymbolUnion) by 8 and decreased memory usage quite a bit, so retaining `symbols` for un-extracted archive members should not cause a memory usage problem. Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D119074
2022-02-07[ELF] Clean up headers. NFCFangrui Song1-2/+2
2022-01-26[ELF] Optimize two vector. NFCFangrui Song1-3/+3
2022-01-26[ELF] Optimize -Map. NFCFangrui Song1-8/+8
getVA is slow. Avoid calling it in the llvm::sort comparator.
2021-12-15[ELF] Change objectFiles to ELFFileBase *Fangrui Song1-2/+2
This can sometimes avoid `cast<ObjFile<...>>`. I intentionally do not touch postScanRelocations to wait for its stabilization.
2021-12-14Reland D114783/D115603 [ELF] Split scanRelocations into ↵Fangrui Song1-1/+1
scanRelocations/postScanRelocations (Fixed an issue about GOT on a copy relocated alias.) (Fixed an issue about not creating r_addend=0 IRELATIVE for unreferenced non-preemptible ifunc.) The idea is to make scanRelocations mark some actions are needed (GOT/PLT/etc) and postpone the real work to postScanRelocations. It gives some flexibility: * Make it feasible to support .plt.got (PR32938): we need to know whether GLOB_DAT and JUMP_SLOT are both needed. * Make non-preemptible IFUNC handling slightly cleaner: avoid setting/clearing sym.gotInIgot * -z nocopyrel: report all copy relocation places for one symbol * Make GOT deduplication feasible * Make parallel relocation scanning feasible (if we can avoid all stateful operations and make Symbol attributes atomic), but parallelism may not be the appealing choice Since this patch moves a large chunk of code out of ELFT templates. My x86-64 executable is actually a few hundred bytes smaller. For ppc32-ifunc-nonpreemptible-pic.s: I remove absolute relocation references to non-preemptible ifunc because absolute relocation references are incorrect in -fpie mode. Reviewed By: peter.smith, ikudrin Differential Revision: https://reviews.llvm.org/D114783
2021-12-14Revert D114783 [ELF] Split scanRelocations into ↵Fangrui Song1-1/+1
scanRelocations/postScanRelocations May cause a failure for non-preemptible `bcmp` in a glibc -static link.
2021-12-14[ELF] -Map: Print symbols which needs canonical PLT entry/copy relocation ↵Fangrui Song1-1/+8
just once If a copy related symbol (say `copy`) is referenced in two .o files, this change removes a duplicated line from the -Map output: ``` 202470 202470 1 1 .bss.rel.ro 202470 202470 1 1 <internal>:(.bss.rel.ro) 202470 202470 1 1 copy removed 202470 202470 1 1 copy ``` Differential Revision: https://reviews.llvm.org/D115697
2021-12-14[ELF] Remove needsPltAddr in favor of needsCopyFangrui Song1-1/+1
needsPltAddr is equivalent to `needsCopy && isFunc`. In many places, it is equivalent to `needsCopy` because the non-STT_FUNC cases are ruled out. Reviewed By: ikudrin, peter.smith Differential Revision: https://reviews.llvm.org/D115603
2021-11-29[ELF] --cref: If -Map is specified, print to the map fileFangrui Song1-24/+29
PR48282: This behavior matches GNU ld and gold. Reviewed By: markj Differential Revision: https://reviews.llvm.org/D114663
2021-11-28[ELF] Inline InputSection::getOffset into callers and remove it. NFCFangrui Song1-1/+1
This is an unneeded abstraction which may cause confusion: SectionBase::getOffset has the same name but hard codes -1 as the size of OutputSection.
2021-11-26[ELF] Rename fetch to extractFangrui Song1-2/+2
The canonical term is "extract" (GNU ld documentation, Solaris's `-z *extract` options). Avoid inventing a term and match --why-extract. (ld64 prefers "load" but the word is overloaded too much) Mostly MFC, except for --help messages and the header row in --print-archive-stats output.
2021-11-25[ELF] Rename BaseCommand to SectionCommand. NFCFangrui Song1-18/+19
BaseCommand was picked when PHDRS/INSERT/etc were not implemented. Rename it to SectionCommand to match `sectionCommands` and make it clear that the commands are used in SECTIONS (except a special case for SymbolAssignment). Also, improve naming of some BaseCommand variables (base -> cmd).
2021-11-25[ELF] Rename OutputSection::sectionCommands to commands. NFCFangrui Song1-1/+1
This partially reverts r315409: the description applies to LinkerScript, but not to OutputSection. The name "sectionCommands" is used in both LinkerScript::sectionCommands and OutputSection::sectionCommands, which may lead to confusion. "commands" in OutputSection has no ambiguity because there are no other types of commands.
2021-09-20[ELF] Add --why-extract= to query why archive members/lazy object files are ↵Fangrui Song1-0/+19
extracted Similar to D69607 but for archive member extraction unrelated to GC. This patch adds --why-extract=. Prior art: GNU ld -M prints ``` Archive member included to satisfy reference by file (symbol) a.a(a.o) main.o (a) b.a(b.o) (b()) ``` -M is mainly for input section/symbol assignment <-> output section mapping (often huge output) and the information may appear ad-hoc. Apple ld64 ``` __Z1bv forced load of b.a(b.o) _a forced load of a.a(a.o) ``` It doesn't say the reference file. Arm's proprietary linker ``` Selecting member vsnprintf.o(c_wfu.l) to define vsnprintf. ... Loading member vsnprintf.o from c_wfu.l. definition: vsnprintf reference : _printf_a ``` --- --why-extract= gives the user the full data (which is much shorter than GNU ld -Map). It is easy to track a chain of references to one archive member with a one-liner, e.g. ``` % ld.lld main.o a_b.a b_c.a c.a -o /dev/null --why-extract=- | tee stdout reference extracted symbol main.o a_b.a(a_b.o) a a_b.a(a_b.o) b_c.a(b_c.o) b() b_c.a(b_c.o) c.a(c.o) c() % ruby -ane 'BEGIN{p={}}; p[$F[1]]=[$F[0],$F[2]] if $.>1; END{x="c.a(c.o)"; while y=p[x]; puts "#{y[0]} extracts #{x} to resolve #{y[1]}"; x=y[0] end}' stdout b_c.a(b_c.o) extracts c.a(c.o) to resolve c() a_b.a(a_b.o) extracts b_c.a(b_c.o) to resolve b() main.o extracts a_b.a(a_b.o) to resolve a ``` Archive member extraction happens before --gc-sections, so this may not be a live path under --gc-sections, but I think it is a good approximation in practice. * Specifying a file avoids output interleaving with --verbose. * Required `=` prevents accidental overwrite of an input if the user forgets `=`. (Most of compiler drivers' long options accept `=` but not ` `) Differential Revision: https://reviews.llvm.org/D109572
2020-11-10[lld][ELF] Add additional time trace categoriesJames Henderson1-0/+3
I noticed when running a large link with the --time-trace option that there were several areas which were missing any specific time trace categories (aside from the generic link/ExecuteLinker categories). This patch adds new categories to fill most of the "gaps", or to provide more detail than was previously provided. Reviewed by: MaskRay, grimar, russell.gallop Differential Revision: https://reviews.llvm.org/D90686
2020-05-15[ELF] Use namespace qualifiers (lld:: or elf::) instead of `namespace lld { ↵Fangrui Song1-8/+5
namespace elf {` Similar to D74882. This reverts much code from commit bd8cfe65f5fee4ad573adc2172359c9552e8cdc0 (D68323) and fixes some problems before D68323. Sorry for the churn but D68323 was a mistake. Namespace qualifiers avoid bugs where the definition does not match the declaration from the header. See https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions (D74515) Differential Revision: https://reviews.llvm.org/D79982
2020-05-05[Support] Move LLD's parallel algorithm wrappers to supportReid Kleckner1-1/+1
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-04-29[ELF] Add --print-archive-stats=Fangrui Song1-0/+18
gold has an option --print-symbol-counts= which prints: // For each archive archive $archive $members $fetched_members // For each object file symbols $object $defined_symbols $used_defined_symbols In most cases, `$defined_symbols = $used_defined_symbols` unless weak symbols are present. Strangely `$used_defined_symbols` includes symbols defined relative to --gc-sections discarded sections. The `symbols` lines do not appear to be useful. `archive` lines are useful: `$fetched_members=0` lines correspond to unused archives. The information can be used to trim dependencies. This patch implements --print-archive-stats= which prints the number of members and the number of fetched members for each archive. Reviewed By: grimar Differential Revision: https://reviews.llvm.org/D78983
2019-11-18Make it possible to redirect not only errs() but also outs()Rui Ueyama1-2/+2
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-10-07[ELF] Wrap things in `namespace lld { namespace elf {`, NFCFangrui Song1-5/+7
This makes it clear `ELF/**/*.cpp` files define things in the `lld::elf` namespace and simplifies `elf::foo` to `foo`. Reviewed By: atanasyan, grimar, ruiu Differential Revision: https://reviews.llvm.org/D68323 llvm-svn: 373885
2019-08-22Fight a bit against global initializers. NFC.Benjamin Kramer1-2/+2
llvm-svn: 369695
2019-08-05Rename F_{None,Text,Append} to OF_{None,Text,Append}. NFCFangrui Song1-1/+1
F_{None,Text,Append} are kept for compatibility since r334221. llvm-svn: 367800
2019-07-10[Coding style change] Rename variables so that they start with a lowercase ↵Rui Ueyama1-112/+112
letter This patch is mechanically generated by clang-llvm-rename tool that I wrote using Clang Refactoring Engine just for creating this patch. You can see the source code of the tool at https://reviews.llvm.org/D64123. There's no manual post-processing; you can generate the same patch by re-running the tool against lld's code base. Here is the main discussion thread to change the LLVM coding style: https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html In the discussion thread, I proposed we use lld as a testbed for variable naming scheme change, and this patch does that. I chose to rename variables so that they are in camelCase, just because that is a minimal change to make variables to start with a lowercase letter. Note to downstream patch maintainers: if you are maintaining a downstream lld repo, just rebasing ahead of this commit would cause massive merge conflicts because this patch essentially changes every line in the lld subdirectory. But there's a remedy. clang-llvm-rename tool is a batch tool, so you can rename variables in your downstream repo with the tool. Given that, here is how to rebase your repo to a commit after the mass renaming: 1. rebase to the commit just before the mass variable renaming, 2. apply the tool to your downstream repo to mass-rename variables locally, and 3. rebase again to the head. Most changes made by the tool should be identical for a downstream repo and for the head, so at the step 3, almost all changes should be merged and disappear. I'd expect that there would be some lines that you need to merge by hand, but that shouldn't be too many. Differential Revision: https://reviews.llvm.org/D64121 llvm-svn: 365595
2019-05-29ELF: Add basic partition data structures and behaviours.Peter Collingbourne1-2/+2
This change causes us to read partition specifications from partition specification sections and split output sections into partitions according to their reachability from partition entry points. This is only the first step towards a full implementation of partitions. Later changes will add additional synthetic sections to each partition so that they can be loaded independently. Differential Revision: https://reviews.llvm.org/D60353 llvm-svn: 361925
2019-05-23ELF: Remove a comparison against In.EhFrame. NFCI.Peter Collingbourne1-4/+5
This won't work once we have multiple .eh_frame sections. Differential Revision: https://reviews.llvm.org/D62280 llvm-svn: 361556
2019-04-23Use llvm::stable_sortFangrui Song1-4/+2
Make some small adjustment while touching the code: make parameters const, use less_first(), etc. Differential Revision: https://reviews.llvm.org/D60989 llvm-svn: 358943
2019-04-01Replace `typedef A B` with `using B = A`. NFC.Rui Ueyama1-1/+1
I did this using Perl. Differential Revision: https://reviews.llvm.org/D60003 llvm-svn: 357372