aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF/SymbolTable.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata1-1/+0
2025-05-13Revert "[lld] Merge equivalent symbols found during ICF (#134342)"Hans Wennborg1-6/+0
The change would also merge *non-equivalent* symbols under some circumstances, see comment with a reproducer on the PR. > Fixes a correctness issue for AArch64 when ADRP and LDR instructions are > outlined in separate sections and sections are fed to ICF for > deduplication. > > See test case (based on > https://github.com/llvm/llvm-project/issues/129122) for details. All > rodata.* sections are folded into a single section with ICF. This leads > to all f2_* function sections getting folded into one (as their > relocation target symbols g* belong to .rodata.g* sections that have > already been folded into one). Since relocations still refer original g* > symbols, we end up creating duplicate GOT entry for all such symbols. > This PR addresses that by tracking such folded symbols and create one > GOT entry for all such symbols. > > Fixes https://github.com/llvm/llvm-project/issues/129122 > > Co-authored by: @jyknight This reverts commit 8389d6fad76bd880f02bddce7f0f2612ff0afc40.
2025-04-21[lld] Merge equivalent symbols found during ICF (#134342)Pranav Kant1-0/+6
Fixes a correctness issue for AArch64 when ADRP and LDR instructions are outlined in separate sections and sections are fed to ICF for deduplication. See test case (based on https://github.com/llvm/llvm-project/issues/129122) for details. All rodata.* sections are folded into a single section with ICF. This leads to all f2_* function sections getting folded into one (as their relocation target symbols g* belong to .rodata.g* sections that have already been folded into one). Since relocations still refer original g* symbols, we end up creating duplicate GOT entry for all such symbols. This PR addresses that by tracking such folded symbols and create one GOT entry for all such symbols. Fixes https://github.com/llvm/llvm-project/issues/129122 Co-authored by: @jyknight
2025-01-30[ELF] Merge exportDynamic/isExported and remove Symbol::includeInDynsymFangrui Song1-5/+3
Commit 3733ed6f1c6b0eef1e13e175ac81ad309fc0b080 introduced isExported to cache includeInDynsym. If we don't unnecessarily set isExported for undefined symbols, exportDynamic/includeInDynsym can be replaced with isExported.
2025-01-06[lld,NFC] Fix stale comments related to config->Fangrui Song1-1/+1
2024-12-08[ELF] Merge parseSymbolVersion and computeIspreemptibleFangrui Song1-7/+0
ICF needs isPreemptible, which can be combined with parseSymbolVersion.
2024-12-08Revert "[ELF] Optimize parseSymbolVersion"Fangrui Song1-15/+3
This reverts commit 2b129dacdde667137b5012d52f1d96e0ab26c749. parseSymbolVersion can be combined with computeIsPreemptible, making hasVersionSyms unneeded.
2024-12-08[ELF] Optimize parseSymbolVersionFangrui Song1-3/+15
We can just scan objectFiles and sharedFiles that have versioned symbols to skip scanning the global symtab. While we won't suggest __wrap_foo for undefined __wrap_foo@v1 when --wrap=foo@v1 is specified (internalFile isn't scanned), this edge case difference is acceptable.
2024-12-07[ELF] Simplify includeInDynsym for --dynamic-listFangrui Song1-1/+1
2024-11-06[ELF] Replace errorOrWarn(...) with ErrFangrui Song1-2/+2
2024-11-06[ELF] Replace warn(...) with WarnFangrui Song1-6/+8
2024-10-29Fix more lld warnings introduced in #111434 [-Wnontrivial-memaccess]NAKAMURA Takumi1-2/+2
2024-10-16[lld] Fix warning in SymbolTable.cpp (#112323)Karl-Johan Karlsson1-1/+1
Fix gcc warning: lld/ELF/SymbolTable.cpp:340:33: warning: enumeral and non-enumeral type in conditional expression [-Wextra]
2024-10-11[ELF] Pass Ctx & to SymbolFangrui Song1-6/+6
2024-10-10[lld][elf] Warn if '*' pattern is used multiple times in version scripts ↵Igor Kudrin1-2/+32
(#102669) If this pattern is used more than once in version script(s), only one will have an effect, so it's probably a user error and can be diagnosed.
2024-09-29[ELF] Pass Ctx & to ICF and SymbolTableFangrui Song1-1/+1
2024-09-23[ELF] Move elf::symtab into CtxFangrui Song1-2/+0
Remove the global variable `symtab` and add a member variable (`std::unique_ptr<SymbolTable>`) to `Ctx` instead. This is one step toward eliminating global states. Pull Request: https://github.com/llvm/llvm-project/pull/109612
2024-09-21[ELF] Replace config-> with ctx.arg. in [IS]*.cppFangrui Song1-6/+6
2024-03-25[ELF] Fix unnecessary inclusion of unreferenced provide symbolsParth Arora1-0/+4
Previously, linker was unnecessarily including a PROVIDE symbol which was referenced by another unused PROVIDE symbol. For example, if a linker script contained the below code and 'not_used_sym' provide symbol is not included, then linker was still unnecessarily including 'foo' PROVIDE symbol because it was referenced by 'not_used_sym'. This commit fixes this behavior. PROVIDE(not_used_sym = foo) PROVIDE(foo = 0x1000) This commit fixes this behavior by using dfs-like algorithm to find all the symbols referenced in provide expressions of included provide symbols. This commit also fixes the issue of unused section not being garbage-collected if a symbol of the section is referenced by an unused PROVIDE symbol. Closes #74771 Closes #84730 Co-authored-by: Fangrui Song <i@maskray.me>
2023-11-16[ELF] Merge verdefIndex into versionId. NFC (#72208)Fangrui Song1-7/+5
The two fields are similar. `versionId` is the Verdef index in the output file. It is set for `--exclude-id=`, version script patterns, and `sym@ver` symbols. `verdefIndex` is the Verdef index of a Sharedfile (SharedSymbol or a copy-relocated Defined), the default value -1 is also used to indicate that the symbol has not been matched by a version script pattern (https://reviews.llvm.org/D65716). It seems confusing to have two fields. Merge them so that we can allocate one bit for #70130 (suppress --no-allow-shlib-undefined error in the presence of a DSO definition).
2023-11-15Revert "[ELF] Merge verdefIndex into versionId. NFC" #72208 (#72484)Fangrui Song1-5/+7
Reverts llvm/llvm-project#72208 If a unversioned Defined preempts a versioned DSO definition, the version ID will not be reset.
2023-11-14[ELF] Merge verdefIndex into versionId. NFC (#72208)Fangrui Song1-7/+5
The two fields are similar. `versionId` is the Verdef index in the output file. It is set for version script patterns and `sym@ver` symbols. `verdefIndex` is the Verdef index of a SharedSymbol. The default value -1 is also used to indicate that the symbol has not been matched by a version script pattern (https://reviews.llvm.org/D65716). It seems confusing to have two fields. Merge them so that we can allocate one bit for #70130 (suppress --no-allow-shlib-undefined error in the presence of a DSO definition).
2023-09-09[ELF] Priorize the last catch-all pattern in version scriptsFangrui Song1-1/+1
When there are multiple catch-all patterns (i.e. a single `*`), GNU ld and gold select the last pattern. Match their behavior. This change was inspired by a correction made by Michael Kerrisk to a blog post I wrote at https://maskray.me/blog/2020-11-26-all-about-symbol-versioning , following the current lld rules. Note: GNU ld prefers global: patterns to local: patterns, which might seem awkward (https://www.airs.com/blog/archives/300). gold doesn't follow this behavior, and we do not either.
2023-06-06reland: [Demangle] make llvm::demangle take std::string_view rather than ↵Nick Desaulniers1-6/+9
const std::string& As suggested by @erichkeane in https://reviews.llvm.org/D141451#inline-1429549 There's potential for a lot more cleanups around these APIs. This is just a start. Callers need to be more careful about sub-expressions producing strings that don't outlast the expression using `llvm::demangle`. Add a release note. Differential Revision: https://reviews.llvm.org/D149104
2023-05-25[ELF] findAllByVersion: optimize a find('@') with hasVersionSuffix. NFCFangrui Song1-6/+6
2023-05-02Revert "[Demangle] make llvm::demangle take std::string_view rather than ↵Nick Desaulniers1-9/+6
const std::string&" This reverts commit c117c2c8ba4afd45a006043ec6dd858652b2ffcc. itaniumDemangle calls std::strlen with the results of std::string_view::data() which may not be NUL-terminated. This causes lld/test/wasm/why-extract.s to fail when "expensive checks" are enabled via -DLLVM_ENABLE_EXPENSIVE_CHECKS=ON. See D149675 for further discussion. Back this out until the individual demanglers are converted to use std::string_view.
2023-05-02[Demangle] make llvm::demangle take std::string_view rather than const ↵Nick Desaulniers1-6/+9
std::string& As suggested by @erichkeane in https://reviews.llvm.org/D141451#inline-1429549 There's potential for a lot more cleanups around these APIs. This is just a start. Callers need to be more careful about sub-expressions producing strings that don't outlast the expression using ``llvm::demangle``. Add a release note. Reviewed By: MaskRay, #lld-macho Differential Revision: https://reviews.llvm.org/D149104
2022-10-14[lld][nfc] Remove lld::demangle() (partial revert of D116279)Jez Ng1-5/+5
{D116279}, in addition to adding support for other demanglers, also factored out some of the demangling logic. However, I don't think the abstraction really carries its weight -- after {D135942}, only the ELF and WASM backends call it with anything other than a non-constant `shouldDemangle` argument. The COFF and Mach-O backends were already doing the should-demangle check before calling `demangle()`. Reviewed By: MaskRay, #lld-macho Differential Revision: https://reviews.llvm.org/D135943
2022-10-01[ELF] Remove symtab indirection. NFCFangrui Song1-1/+1
Add LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr indirection.
2022-09-29[ELF] Make symAux[0] a sentinelFangrui Song1-1/+0
And default auxIdx to 0.
2022-09-28[ELF] Remove resolve => resolve{Defined,Common,Shared,Lazy,Undefined} ↵Fangrui Song1-6/+0
indirection. NFC
2022-09-28[ELF] Refactor Symbol initialization and overwritingFangrui Song1-10/+4
Symbol::replace intends to overwrite a few fields (mostly Elf{32,64}_Sym fields), but the implementation copies all fields then restores some old fields. This is error-prone and wasteful. Add Symbol::overwrite to copy just the needed fields and add other overwrite member functions to copy the extra fields.
2022-09-04[ELF] Use stOther to track visibilityFangrui Song1-1/+1
This simplifies SymbolTableSection<ELFT>::writeTo. Add dsoProtected to be used in canDefineSymbolInExecutable and get the side benefit that the protected DSO preemption diagnostic is clearer.
2022-04-22[ELF] Drop unused original symbol after wrapping if not definedShoaib Meenai1-1/+4
We were previously only omitting the original of a wrapped symbol if it was not used by an object file and undefined. We can tighten the second condition to drop any symbol that isn't defined instead, which lets us drop a previous check (added in https://reviews.llvm.org/D118756) that was only covering some such symbols. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D124065
2022-04-22[ELF] Fix wrapping symbols produced during LTO codegenShoaib Meenai1-1/+6
We were previously not correctly wrapping symbols that were only produced during LTO codegen and unreferenced before then, or symbols only referenced from such symbols. The root cause was that we weren't marking the wrapped symbol as used if we only saw the use after LTO codegen, leading to the failed wrapping. Fix this by explicitly tracking whether a symbol will become referenced after wrapping is done. We can use this property to tell LTO to preserve such symbols, instead of overload isUsedInRegularObj for this purpose. Since we're no longer setting isUsedInRegularObj for all symbols which will be wrapped, its value at the time of performing the wrapping in the symbol table will accurately reflect whether the symbol was actually used in an object (including in an LTO-generated object), and we can propagate that value to the wrapped symbol and thereby ensure we wrap correctly. This incorrect wrapping was the only scenario I was aware of where we produced an invalid PLT relocation, which D123985 started diagnosing, and with it fixed, we lose the test for that diagnosis. I think it's worth keeping the diagnosis though, in case we run into other issues in the future which would be caught by it. Fixes PR50675. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D124056
2022-02-23[ELF] Move isUsedInRegularObj assignment from ctor to call sites. NFCFangrui Song1-0/+1
This removes the tricky `isUsedInRegularObj(!file || file->kind() == InputFile::ObjKind)` and the copy from `Symbol::mergeProperties`.
2022-02-23[ELF] Don't rely on Symbols.h's transitive inclusion of InputFiles.h. NFCFangrui Song1-0/+1
2022-02-22[ELF] Move duplicate symbol check after input file parsingFangrui Song1-0/+10
https://discourse.llvm.org/t/parallel-input-file-parsing/60164 To decouple symbol initialization and section initialization, `Defined::section` assignment should be postponed after input file parsing. To avoid spurious duplicate definition error due to two definitions in COMDAT groups of the same signature, we should postpone the duplicate symbol check. The function is called postScan instead of a more specific name like checkDuplicateSymbols, because we may merge Symbol::mergeProperties into postScan. It is placed after compileBitcodeFiles to apply to ET_REL files produced by LTO. This causes minor diagnostic regression for skipLinkedOutput configurations: ld.lld --thinlto-index-only a.bc b.o (bitcode definition prevails) won't detect duplicate symbol error. I think this is an acceptable compromise. The important cases where (a) both files are bitcode or (b) --thinlto-index-only is unused are still detected. Reviewed By: ikudrin Differential Revision: https://reviews.llvm.org/D119908
2022-02-07[ELF] Clean up headers. NFCFangrui Song1-2/+0
2022-02-05[ELF] Reorder Symbol members to improve access locality. NFCFangrui Song1-2/+2
* partition and isPreemptible are frequently used. Move it to the front * move used beside isUsedInRegularObj. They are similar and accessed together in .symtab finalizing * move auxIdx/dynsymIndex/verdefIndex to the end. This decreases code size.
2022-02-05[ELF] Merge canInline into scriptDefinedFangrui Song1-1/+0
They perform similar tasks and are essentially the same after d28c26bbdd9e6e55cc0a6156e9879f7e0ca36329.
2022-02-05[ELF] --wrap: don't copy exportDynamicFangrui Song1-2/+0
For -no-pie/-pie, when `__real_foo` is interposable in a shared object, `foo` is exported. This rule does not match GNU ld and is unneeded because: * the exported `foo` does not interpose `__real_foo` at run-time * the similar `__wrap_foo` <-> `foo` relation does not have the rule
2022-01-05[lld] Add support for other demanglers other than ItaniumLuís Ferreira1-4/+5
LLVM core library supports demangling other mangled symbols other than itanium, such as D and Rust. LLD should use those demanglers in order to output pretty demangled symbols on error messages. Reviewed By: MaskRay, #lld-macho Differential Revision: https://reviews.llvm.org/D116279
2021-12-30Revert "[lld] Add support for other demanglers other than Itanium"Luís Ferreira1-5/+4
This reverts commit e60d6dfd5acdc821d391ad5af2c706397bdfd36a. clang-ppc64le-rhel buildbot failed (https://lab.llvm.org/buildbot#builders/57/builds/13424): tools/lld/MachO/CMakeFiles/lldMachO.dir/Symbols.cpp.o: In function `lld::demangle(llvm::StringRef, bool)': Symbols.cpp:(.text._ZN3lld8demangleEN4llvm9StringRefEb[_ZN3lld8demangleEN4llvm9StringRefEb]+0x90): undefined reference to `llvm::demangle(std::string const&)'
2021-12-30[lld] Add support for other demanglers other than ItaniumLuís Ferreira1-4/+5
LLVM core library supports demangling other mangled symbols other than itanium, such as D and Rust. LLD should use those demanglers in order to output pretty demangled symbols on error messages. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D116279
2021-12-26[ELF] Add Symbol::hasVersionSuffixFangrui Song1-2/+7
"Process symbol versions" may take 2+% time. "Redirect symbols" may take 0.6% time. This change speeds up the two passes and makes `*sym.getVersionSuffix() == '@'` in the `undefined reference` diagnostic cleaner. Linking chrome (no debug info) and another large program is 1.5% faster. For empty-ver2.s: the behavior now matches GNU ld, though I'd consider the input invalid and the exact behavior does not matter.
2021-12-23[ELF] scanVersionScript: remove unused variableFangrui Song1-1/+0
2021-12-23[ELF] Simplify SymbolTable::insert. NFCFangrui Song1-5/+2
2021-12-23[ELF] SymbolTable: change some vector<Symbol *> to SmallVectorFangrui Song1-7/+7
The generated assembly for Symbol::insert is much shorter (std::vector resize is inefficient) and enables some inlining.
2021-12-22[ELF] Change some global pointers to unique_ptrFangrui Song1-1/+1
Currently the singleton `config` is assigned by `config = make<Configuration>()` and (if `canExitEarly` is false) destroyed by `lld::freeArena`. `make<Configuration>` allocates a stab with `malloc(4096)`. This both wastes memory and bloats the executable (every type instantiates `BumpPtrAllocator` which costs more than 1KiB code on x86-64). (No need to worry about `clang::no_destroy`. Regular invocations (`canExitEarly` is true) call `_Exit` via llvm::sys::Process::ExitNoCleanup.) Reviewed By: lichray Differential Revision: https://reviews.llvm.org/D116143