summaryrefslogtreecommitdiffstats
path: root/lld/ELF/InputSection.cpp
Commit message (Collapse)AuthorAgeFilesLines
* [ELF] Fix a null pointer dereference when --emit-relocs and --strip-debug ↵Fangrui Song2020-04-101-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | are used together Fixes https://bugs.llvm.org//show_bug.cgi?id=44878 When --strip-debug is specified, .debug* are removed from inputSections while .rel[a].debug* (incorrectly) remain. LinkerScript::addOrphanSections() requires the output section of a relocated InputSectionBase to be created first. .debug* are not in inputSections -> output sections .debug* are not created -> getOutputSectionName(.rel[a].debug*) dereferences a null pointer. Fix the null pointer dereference by deleting .rel[a].debug* from inputSections as well. Reviewed By: grimar, nickdesaulniers Differential Revision: https://reviews.llvm.org/D74510 (cherry picked from commit 6c73246179376442705b3a545f4e1f1478777a04)
* [ELF][PPC32] Support --emit-relocs link of R_PPC_PLTREL24Fangrui Song2020-01-281-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Similar to R_MIPS_GPREL16 and R_MIPS_GPREL32 (D45972). If the addend of an R_PPC_PLTREL24 is >= 0x8000, it indicates that r30 is relative to the input section .got2. ``` addis 30, 30, .got2+0x8000-.L1$pb@ha addi 30, 30, .got2+0x8000-.L1$pb@l ... bl foo+0x8000@PLT ``` After linking, the relocation will be relative to the output section .got2. To compensate for the shift `address(input section .got2) - address(output section .got2) = ppc32Got2OutSecOff`, adjust by `ppc32Got2OutSecOff`: ``` addis 30, 30, .got2+0x8000-.L1+ppc32Got2OutSecOff$pb@ha addi 30, 30, .got2+0x8000-.L1+ppc32Got2OutSecOff$pb@ha$pb@l ... bl foo+0x8000+ppc32Got2OutSecOff@PLT ``` This rule applys to a relocatable link or a non-relocatable link with --emit-relocs. Reviewed By: Bdragon28 Differential Revision: https://reviews.llvm.org/D73532 (cherry picked from commit e11b709b1922ca46b443fcfa5d76b87edca48721)
* [lld] Fix trivial typos in commentsKazuaki Ishizaki2020-01-061-1/+1
| | | | | | Reviewed By: ruiu, MaskRay Differential Revision: https://reviews.llvm.org/D72196
* Add TPREL relocation support to HexagonSid Manning2020-01-021-0/+1
| | | | Differential Revision: https://reviews.llvm.org/D71069
* [ELF][PPC64] Improve "call lacks nop" diagnostic and make it compatible with ↵Fangrui Song2019-12-291-2/+10
| | | | | | | | | | | | | | | | GCC<5.5 and GCC<6.4 GCC before r245813 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79439) did not emit nop after b/bl. This can happen with recursive calls. r245813 was back ported to GCC 5.5 and GCC 6.4. This is common, for example, libstdc++.a(locale.o) shipped with GCC 4.9 and many objects in netlib lapack can cause lld to error. gold allows such calls to the same section. Our __plt_foo symbol's `section` field is used for ThunkSection, so we can't implement a similar loosen rule easily. But we can make use of its `file` field which is currently NULL. Differential Revision: https://reviews.llvm.org/D71639
* comment typo fix to cycle botsNico Weber2019-10-311-1/+1
|
* Fix a few typos in lld/ELF to cycle botsNico Weber2019-10-281-3/+3
|
* [ELF] Wrap things in `namespace lld { namespace elf {`, NFCFangrui Song2019-10-071-8/+10
| | | | | | | | | | | 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
* [ELF][PPC] Allow PT_LOAD to have overlapping p_offset rangesFangrui Song2019-08-201-14/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This change affects the non-linker script case (precisely, when the `SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD boundaries for the default case: the size of a powerpc64 binary can be decreased by at most 192kb. The technique can be ported to other targets. Let me demonstrate the idea with a maxPageSize=65536 example: When assigning the address to the first output section of a new PT_LOAD, if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020, 0x20000) in the output. Alternatively, if we advance to 0x20020, the new PT_LOAD will have p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset! Obviously 0x10020 is the choice because it leaves no gap. At runtime, p_vaddr will be rounded down by pagesize (65536 if pagesize=maxPageSize). This PT_LOAD will load additional initial contents from p_offset ranges [0x10000,0x10020), which will also be loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in effect or if we are not transiting between executable and non-executable segments. ld.bfd -z noseparate-code leverages this technique to keep output small. This patch implements the technique in lld, which is mostly effective on targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3 removed alignments can save almost 3*65536 bytes. Two places that rely on p_vaddr%pagesize = 0 have to be updated. 1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero. The updated formula takes account of that factor. 2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0. Fix them. See the updated comments in InputSection.cpp for details. On targets that we enable the technique (only PPC64 now), we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0` if `sh_addralign(.tdata) < sh_addralign(.tbss)` This exposes many problems in ld.so implementations, especially the offsets of dynamic TLS blocks. Known issues: FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64) glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606 musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...) So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS). The technique will be enabled (with updated tests) for other targets in subsequent patches. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D64906 llvm-svn: 369343
* [ELF][Hexagon] Replace R_HEXAGON_GOT with R_GOTPLTFangrui Song2019-08-161-2/+0
| | | | | | | | | | | R_GOTPLT is relative to .got.plt since D59594. Since R_HEXAGON_GOT relocations always have 0 r_addend, they can use R_GOTPLT instead. Reviewed By: sidneym Differential Revision: https://reviews.llvm.org/D66274 llvm-svn: 369128
* [mips] Assign R_TLS type to the R_MIPS_TLS_TPREL_XXX relocations. NFCSimon Atanasyan2019-07-241-0/+1
| | | | | | | | That allows to remove duplicated code which subtracts 0x7000 from the R_MIPS_TLS_TPREL_XXX relocations values in the `MIPS::relocateOne` function. llvm-svn: 366888
* [ELF] Fix variable names in comments after VariableName -> variableName changeFangrui Song2019-07-161-3/+3
| | | | | | Also fix some typos. llvm-svn: 366181
* [Coding style change][lld] Rename variables for non-ELF portsRui Ueyama2019-07-111-2/+2
| | | | | | | | | | | 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
* [Coding style change] Rename variables so that they start with a lowercase ↵Rui Ueyama2019-07-101-522/+522
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* [ELF][RISCV] Error on R_RISCV_PCREL_LO12_[IS] that point to absolute symbolsFangrui Song2019-07-031-0/+5
| | | | | | | | | | | | | | The referenced symbol is expected to point to an R_RISCV_*_HI20 relocation. An absolute symbol has no associated section, therefore there cannot be a matching R_RISCV_*_HI20. This fixes the crash reported by PR42038. For reference, ld.bfd errors: (.init+0x4): dangerous relocation: %pcrel_lo missing matching %pcrel_hi Differential Revision: https://reviews.llvm.org/D63273 llvm-svn: 365049
* [ELF][RISCV] Allow R_RISCV_ADD in relocateNonAlloc()Fangrui Song2019-07-031-1/+1
| | | | | | | | | | | | gcc may generate .debug_info/.debug_aranges/.debug_line/etc that are relocated by R_RISCV_ADD*/R_RISCV_SUB* pairs. Allow R_RISCV_ADD in non-SHF_ALLOC section to fix link errors like: ld.lld: error: print.c:(.debug_frame+0x60): has non-ABS relocation R_RISCV_ADD64 against symbol '.L0 ' Differential Revision: https://reviews.llvm.org/D63259 llvm-svn: 365035
* [ELF][RISCV] Support GD/LD/IE/LE TLS modelsFangrui Song2019-07-011-1/+4
| | | | | | | | | | | | RISC-V psABI doesn't specify TLS relaxation. It can be handled the same way as we handle ARM TLS. RISC-V TLS is even simpler because GD/LD use the same relocation type. Reviewed By: jrtc27, ruiu Differential Revision: https://reviews.llvm.org/D63220 llvm-svn: 364813
* [ELF][RISCV] Support PLT, GOT, copy and relative relocationsFangrui Song2019-07-011-1/+1
| | | | | | | | | | | | | | | | | | | | * Handle initial relocation types: R_RISCV_CALL_PLT and R_RISCV_GOT_HI20. * Produce dynamic relocation types: R_RISCV_COPY, R_RISCV_RELATIVE, R_RISCV_JUMP_SLOT. * Define SymbolRel as R_RISCV_{32,64} * Generate PLT header: it is used by lazy binding PLT in glibc. * R_RISCV_CALL is changed from R_PC to R_PC_PLT. If the target symbol is preemptable, this will suppress an unnecessary "canonical PLT". This behavior is different from ld.bfd but it is agreed the current lld behavior is favored. I have received positive responses from the binutils maintainer that the ABI/binutils implementation can be improved, see: https://github.com/riscv/riscv-elf-psabi-doc/issues/98 https://sourceware.org/bugzilla/show_bug.cgi?id=24685 Many -no-pie/-pie/-shared programs linked against musl or glibc should work with this patch. Reviewed By: jrtc27 Differential Revision: https://reviews.llvm.org/D63076 llvm-svn: 364812
* Cleanup: llvm::bsearch -> llvm::partition_point after r364719Fangrui Song2019-06-301-2/+2
| | | | llvm-svn: 364720
* Reland D61583 [ELF] Error on relocations to STT_SECTION symbols if the ↵Fangrui Song2019-06-261-3/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | sections were discarded This restores r361830 "[ELF] Error on relocations to STT_SECTION symbols if the sections were discarded" and dependent commits (r362218, r362497) which were reverted by r364321, with a fix of a --gdb-index issue. .rela.debug_ranges contains relocations of range list entries: // start address of a range list entry // old: 0; after r361830: 0 00000000000033a0 R_X86_64_64 .text._ZN2v88internal7Isolate7factoryEv + 0 // end address of a range list entry // old: 0xe; after r361830: 0 00000000000033a8 R_X86_64_64 .text._ZN2v88internal7Isolate7factoryEv + e If both start and end addresses of a range list entry resolve to 0, DWARFDebugRangeList::isEndOfListEntry() will return true, then the .debug_range decoding loop will terminate prematurely: while (true) { decode StartAddress decode EndAddress if (Entry.isEndOfListEntry()) // prematurely break; Entries.push_back(Entry); } In lld/ELF/SyntheticSections.cpp, readAddressAreas() will read incomplete address ranges and the resulting .gdb_index will be incomplete. For files that gdb hasn't loaded their debug info, gdb uses .gdb_index to map addresses to CUs. The absent entries make gdb fail to symbolize some addresses. To address this issue, we simply allow relocations to undefined symbols in DWARF.cpp:findAux() and let RelocationResolver resolve them. This patch should fix: [1] http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190603/659848.html [2] https://bugs.chromium.org/p/chromium/issues/detail?id=978067 llvm-svn: 364391
* Revert r362743 "Revert "Revert "Reland D61583 [ELF] Error on relocations to ↵Hans Wennborg2019-06-251-17/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | STT_SECTION symbols if the sections were discarded""" (In effect, reverting "[ELF] Error on relocations to STT_SECTION symbols if the sections were discarded".) It caused debug info problems in LibreOffice [1] and Chromium/V8 [2]. Reverting until those can be fixed. It also reverts r362497 "STT_SECTION symbol should be defined" on .eh_frame, .debug*, .zdebug* and .gcc_except_table" which was landed as a follow-up to the above. > With -r or --emit-relocs, we warn `STT_SECTION symbol should be defined` > on relocations to discarded section symbol. This was added as an error > in rLLD319404, but was not so effective before D61583 (it turned the > error to a warning). > > Relocations from .eh_frame .debug* .zdebug* .gcc_except_table to > discarded .text are very common and somewhat expected. Don't warn/error > on them. As a reference, ld.bfd has a similar logic in > _bfd_elf_default_action_discarded() to allow these cases. > > Delete invalid-undef-section-symbol.test because what it intended to > check is now covered by the updated comdat-discarded-reloc.s > > Delete relocatable-eh-frame.s because we allow relocations from > .eh_frame as a special case now. And finally it reverts r362218 "[ELF] Replace a dead test in getSymVA() with assert()" as that also depended on the main change reverted here. > Symbols relative to discarded comdat sections are Undefined instead of > Defined now (after D59649 and D61583). The `== &InputSection::Discarded` > test becomes dead. I cannot find a test related to this behavior. [1] http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190603/659848.html [2] https://bugs.chromium.org/p/chromium/issues/detail?id=978067 llvm-svn: 364321
* [ELF][RISCV] Treat R_RISCV_{ADD,SET,SUB}* as link-time constantsFangrui Song2019-06-121-0/+1
| | | | | | | | | | | | | | | | | | | | | | | R_RISCV_{ADD,SET,SUB}* are used for local label computation. Add a new RelExpr member R_RISCV_ADD to represent them. R_RISCV_ADD is treated as a link-time constant because otherwise R_RISCV_{ADD,SET,SUB}* are not allowed in -pie/-shared mode. In glibc Scrt1.o, .rela.eh_frame contains such relocations. Because .eh_frame is not writable, we get this error: ld.lld: error: can't create dynamic relocation R_RISCV_ADD32 against symbol: .L0 in readonly segment; recompil object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output >>> defined in ..../riscv64-linux-gnu/lib/Scrt1.o With D63076 and this patch, I can run -pie/-shared programs linked against glibc. Note llvm-mc cannot currently produce R_RISCV_SET* so they are not tested. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D63183 llvm-svn: 363128
* [ELF][PPC64] Don't report "relocation refers to a discarded section" for .tocFangrui Song2019-06-121-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc which references an embedded switch table in a discarded .rodata/.text section. The .toc and the .rela.toc are incorrectly not placed in the comdat. Technically a relocation from outside the group is not allowed by the ELF spec: > A symbol table entry with STB_LOCAL binding that is defined relative > to one of a group's sections, and that is contained in a symbol table > section that is not part of the group, must be discarded if the group > members are discarded. References to this symbol table entry from > outside the group are not allowed. Don't report errors to work around the bug. This should fix the ppc64le-lld-multistage-test bot while linking llvm-tblgen: ld.lld: error: relocation refers to a discarded section: .rodata._ZNK4llvm3MVT13getSizeInBitsEv >>> defined in utils/TableGen/CMakeFiles/llvm-tblgen.dir/CodeGenRegisters.cpp.o >>> referenced by CodeGenRegisters.cpp >>> utils/TableGen/CMakeFiles/llvm-tblgen.dir/CodeGenRegisters.cpp.o:(.toc+0x0) Some other PPC specific sections may have similar problems. We can blacklist more section names when problems occur. // A simple program that reproduces the bug. // Note .rela.toc (outside the group) references a section symbol (STB_LOCAL) in a group. void puts(const char *); struct A { void foo(int a) { switch (a) { case 0: puts("0"); break; case 1: puts("1"); puts("1"); break; case 2: puts("2"); break; case 3: puts("3"); puts("4"); break; case 4: puts("4"); break; case 5: puts("5"); puts("5"); break; case 6: puts("6"); break; } } int a; }; void foo(A x) { x.foo(x.a); } Reviewers: ruiu, sfertile, espindola Reviewed By: ruiu Subscribers: emaste, nemanjai, arichardson, kbarton, jsji, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63182 llvm-svn: 363126
* Revert "Revert "[ELF] Suppress "STT_SECTION symbol should be defined" on ↵Sean Fertile2019-06-061-3/+14
| | | | | | | | .eh_frame, .debug*, .zdebug* and .gcc_except_table"" This reverts commit f49f58527a6d8147524d8d6f2eb1feb70f856292. llvm-svn: 362744
* Revert "Revert "Reland D61583 [ELF] Error on relocations to STT_SECTION ↵Sean Fertile2019-06-061-1/+2
| | | | | | | | | | symbols if the sections were discarded"" This reverts commit 729111cf1824159bb4dd331cab8a829eab30313f. Reverting the previous commit breaks other LLD buildbots. llvm-svn: 362743
* Revert "Reland D61583 [ELF] Error on relocations to STT_SECTION symbols if ↵Sean Fertile2019-06-061-2/+1
| | | | | | | | | | the sections were discarded" This reverts commit 5d3b3188f722456a6470c7effcacf17656406429. Breaks the PowerPC multi-stage buildbot. llvm-svn: 362739
* Revert "[ELF] Suppress "STT_SECTION symbol should be defined" on .eh_frame, ↵Sean Fertile2019-06-061-14/+3
| | | | | | | | | | | .debug*, .zdebug* and .gcc_except_table" This reverts commit dcba4828a9ead5f5b1fa27f0853823618075d0e0. This commit builds on dcba4828a9ead5f5b1fa27f0853823618075d0e0 which breaks the multi-staged PowerPC buildbot. llvm-svn: 362738
* [PPC32] Support GD/LD/IE/LE TLS models and their relaxationsFangrui Song2019-06-061-0/+1
| | | | | | | | Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62940 llvm-svn: 362722
* [PPC32] Improve the 32-bit PowerPC portFangrui Song2019-06-061-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Many -static/-no-pie/-shared/-pie applications linked against glibc or musl should work with this patch. This also helps FreeBSD PowerPC64 to migrate their lib32 (PR40888). * Fix default image base and max page size. * Support new-style Secure PLT (see below). Old-style BSS PLT is not implemented, so it is not suitable for FreeBSD rtld now because it doesn't support Secure PLT yet. * Support more initial relocation types: R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16. The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type but it should be ignored for the computation of target symbol VA. * Support GNU ifunc * Support .glink used for lazy PLT resolution in glibc * Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub. It is used by R_PPC_REL24 and R_PPC_PLTREL24. A PLT stub used in -fPIE/-fPIC usually loads an address relative to .got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative addresses). Two .got2 sections in two object files have different addresses, thus a PLT stub can't be shared by two object files. To handle this incompatibility, change the parameters of Thunk::isCompatibleWith to `const InputSection &, const Relocation &`. PowerPC psABI specified an old-style .plt (BSS PLT) that is both writable and executable. Linkers don't make separate RW- and RWE segments, which causes all initially writable memory (think .data) executable. This is a big security concern so a new PLT scheme (secure PLT) was developed to address the security issue. TLS will be implemented in D62940. glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack (not included in this patch) in LinkerScript.cpp addOrphanSections() to work around the issue: if (Config->EMachine == EM_PPC) { // Older glibc assumes .rela.dyn includes .rela.plt Add(In.RelaDyn); if (In.RelaPlt->isLive() && !In.RelaPlt->Parent) In.RelaDyn->getParent()->addSection(In.RelaPlt); } Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62464 llvm-svn: 362721
* [ELF] Suppress "STT_SECTION symbol should be defined" on .eh_frame, .debug*, ↵Fangrui Song2019-06-041-3/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | .zdebug* and .gcc_except_table Summary: With -r or --emit-relocs, we warn `STT_SECTION symbol should be defined` on relocations to discarded section symbol. This was added as an error in rLLD319404, but was not so effective before D61583 (it turned the error to a warning). Relocations from .eh_frame .debug* .zdebug* .gcc_except_table to discarded .text are very common and somewhat expected. Don't warn/error on them. As a reference, ld.bfd has a similar logic in _bfd_elf_default_action_discarded() to allow these cases. Delete invalid-undef-section-symbol.test because what it intended to check is now covered by the updated comdat-discarded-reloc.s Delete relocatable-eh-frame.s because we allow relocations from .eh_frame as a special case now. Reviewers: grimar, phosek, ruiu, espindola Reviewed By: ruiu Subscribers: emaste, arichardson, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62840 llvm-svn: 362497
* [ELF][PPC64] Rename some PPC64 ELFv2 specific RelExpr from R_PPC_* to R_PPC64_*Fangrui Song2019-06-031-4/+4
| | | | | | | | | | | | | | | | | | The following abstract relocation types (RelExpr) are PPC64 ELFv2 ABI specific, not used by PPC32. So rename them to prevent confusion when the PPC32 port is improved. * R_PPC_CALL R_PPC_CALL_PLT: R_PPC_CALL_PLT represents R_PPC64_REL14 and R_PPC64_REL24. If the function is not preemptable, R_PPC_CALL_PLT can be optimized to R_PPC_CALL: the formula adjusts the symbol VA from the global entry point to the local entry point. * R_PPC_TOC: represents R_PPC64_TOC. We don't have a test. Add one to ppc64-relocs.s Rename it to R_PPC64_TOCBASE because `@tocbase` is the assembly form. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62800 llvm-svn: 362359
* [ELF] Implement Local Dynamic style TLSDESC for x86-64Fangrui Song2019-05-301-6/+10
| | | | | | | | | | | | | | | | | | | | | | | For the Local Dynamic case of TLSDESC, _TLS_MODULE_BASE_ is defined as a special TLS symbol that makes: 1) Without relaxation: it produces a dynamic TLSDESC relocation that computes 0. Adding @dtpoff to access a TLS symbol. 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in the TLS block). Adding @tpoff to access a TLS symbol. For 1), this saves dynamic relocations and GOT slots as otherwise (General Dynamic) we would create an R_X86_64_TLSDESC and reserve two GOT slots for each symbol. Add ElfSym::TlsModuleBase and change the signature of getTlsTpOffset() to special case _TLS_MODULE_BASE_. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62577 llvm-svn: 362078
* ELF: Add basic partition data structures and behaviours.Peter Collingbourne2019-05-291-2/+13
| | | | | | | | | | | | | | 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
* [ELF] Implement General Dynamic style TLSDESC for x86-64Fangrui Song2019-05-291-0/+2
| | | | | | | | | | | This handles two initial relocation types R_X86_64_GOTPC32_TLSDESC and R_X86_64_TLSDESC_CALL, as well as the GD->LE and GD->IE relaxations. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62513 llvm-svn: 361911
* Reland D61583 [ELF] Error on relocations to STT_SECTION symbols if the ↵Fangrui Song2019-05-281-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | sections were discarded This is implemented by creating Undefined (instead of Defined) for such local STT_SECTION symbols. It allows us to catch errors when there are relocations to such discarded sections (e.g. in PR41693, ld.bfd and gold error but we don't). Updated comdat-discarded-error.s checks we emit friendly error message. For relocatable-eh-frame.s, ld.lld -r a.o a.o will now error "STT_SECTION symbol should be defined" because the section .eh_frame refers to is now an Undefined instead of a Defined. So I have to change `error()` to `warn()` to retain the output. rLLD361144 inadvertently enabled the error for --gdb-index (in LLDDwarfObj<ELFT>::findAux()). Relocations from .debug_info (not in comdat) to .text.* (in comdat) for DW_AT_low_pc are common. If an .text.* was discarded, rLLD361144 would error, which was unexpected. (Note, if we don't error as this patch does, InputSection::relocateNonAlloc() will resolve such relocations). llvm-svn: 361830
* Revert [ELF] Error on relocations to STT_SECTION symbols if the sections ↵Haojian Wu2019-05-281-2/+1
| | | | | | | | | | were discarded This reverts r361792 (git commit cfca5095df0209c60109696d6cc368d49e2c5939), the revision causes link errors internally, will share more details with the author. llvm-svn: 361806
* [ELF] Error on relocations to STT_SECTION symbols if the sections were discardedFangrui Song2019-05-281-1/+2
| | | | | | | | | | | | | | | | | | | This is implemented by creating Undefined (instead of Defined) for such local STT_SECTION symbols. It allows us to catch errors when there are relocations to such discarded sections (e.g. in PR41693, ld.bfd and gold error but we don't). Updated comdat-discarded-error.s checks we emit friendly error message. For relocatable-eh-frame.s, ld.lld -r a.o a.o will now error "STT_SECTION symbol should be defined" because the section .eh_frame refers to is now an Undefined instead of a Defined. So I have to change `error()` to `warn()` to retain the output. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D61583 llvm-svn: 361792
* [ELF] Fix getRelocTargetVA formulae of R_TLS and R_NEG_TLSFangrui Song2019-05-201-8/+9
| | | | | | | | | | | | | | | | | | | | | | For R_TLS: 1) Delete Sym.isTls() . The assembler ensures the symbol is STT_TLS. If not (the input is broken), we would crash (dereferencing null Out::TlsPhdr). 2) Change Sym.isUndefWeak() to Sym.isUndefined(), otherwise with --noinhibit-exec we would still evaluate the symbol and crash. 3) Return A if the symbol is undefined. This is PR40570. The case is probably unrealistic but returning A matches R_ABS and the behavior of several dynamic loaders. R_NEG_TLS is obsoleted Sun TLS we don't fully support, but R_RELAX_TLS_GD_TO_LE_NEG is still used by GD->LE relaxation (subl $var@tpoff,%eax). They should add the addend. Unfortunately I can't test it as compilers don't seem to generate non-zero implicit addends. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D62098 llvm-svn: 361146
* [ARM][AArch64] Revert Android Bionic PT_TLS overaligning hackFangrui Song2019-05-181-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts D53906. D53906 increased p_align of PT_TLS on ARM/AArch64 to 32/64 to make the static TLS layout compatible with Android Bionic's ELF TLS. However, this may cause glibc ARM/AArch64 programs to crash (see PR41527). The faulty PT_TLS in the executable satisfies p_vaddr%p_align != 0. The remainder is normally 0 but may be non-zero with the hack in place. The problem is that we increase PT_TLS's p_align after OutputSections' addresses are fixed (assignAddress()). It is possible that p_vaddr%old_p_align = 0 while p_vaddr%new_p_align != 0. For a thread local variable defined in the executable, lld computed TLS offset (local exec) is different from glibc computed TLS offset from another module (initial exec/generic dynamic). Note: PR41527 said the bug affects initial exec but actually generic dynamic is affected as well. (glibc is correct in that it compute offsets that satisfy `offset%p_align == p_vaddr%p_align`, which is a basic ELF requirement. This hack appears to work on FreeBSD rtld, musl<=1.1.22, and Bionic, but that is just because they (and lld) incorrectly compute offsets that satisfy `offset%p_align = 0` instead.) Android developers are fine to revert this patch, carry this patch in their tree before figuring out a long-term solution (e.g. a dummy .tdata with sh_addralign=64 sh_size={0,1} in crtbegin*.o files. The overhead is now insignificant after D62059). Reviewed By: rprichard, srhines Differential Revision: https://reviews.llvm.org/D62055 llvm-svn: 361090
* [ELF][X86] Fix R_RELAX_TLS_GD_TO_LE_NEG and R_NEG_TLS after D62059Fangrui Song2019-05-181-1/+1
| | | | | | | | | | | | | After D62059, we don't align p_memsz of PT_TLS to p_align. The getRelocTargetVA formula should align it instead. It becomes clear that R_NEG_TLS and R_TLS are opposite from each other. In i386-tls-le-align.s, I put ret after call ___tls_get_addr@plt as otherwise ld.bfd would reject the relaxation: TLS transition from R_386_TLS_GD to R_386_TLS_LE_32 against `a' at 0x3 in section `.text' failed llvm-svn: 361088
* [ELF] Fix TP offset of TLS Variant I after D62059Fangrui Song2019-05-181-1/+1
| | | | | | | | | | | As Ryan Prichard pointed out, after D62059, the TP offset is incorrect. Add x86-64-tls-le-align.s to check this. Better formulae for both variants should take p_vaddr%p_align into account (offset%p_align = p_vaddr%p_align is a basic ELF requirement), but I can't find a way to test the behavior. llvm-svn: 361084
* [ELF] -r: fix R_*_NONE to section symbols on Elf*_Rel targetsFangrui Song2019-05-171-1/+1
| | | | | | | | | | | | | | | | | On Elf*_Rel targets, for a relocation to a section symbol, an R_ABS is added which will be used by relocateOne() to compute the implicit addend. Addends of R_*_NONE should be ignored, so don't emit an R_ABS. This fixes crashes on X86 and ARM because their relocateOne() do not handle R_*_NONE. Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D62052 llvm-svn: 361036
* Revert r358069 "Discard debuginfo for object files empty after GC"Bob Haarman2019-05-161-1/+0
| | | | | | | | The change broke some scenarios where debug information is still needed, although MarkLive cannot see it, including the Chromium/Android build. Reverting to unbreak that build. llvm-svn: 360955
* [PPC64] toc-indirect to toc-relative relaxationFangrui Song2019-05-071-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is based on D54720 by Sean Fertile. When accessing a global symbol which is not defined in the translation unit, compilers will generate instructions that load the address from the toc entry. If the symbol is defined, non-preemptable, and addressable with a 32-bit signed offset from the toc pointer, the address can be computed directly. e.g. addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry ld/lwa 3, 0(3) # load the value from the address .section .toc,"aw",@progbits .LC0: .tc var[TC],var can be relaxed to addis 3,2,var@toc@ha # this may be relaxed to a nop, addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc ld/lwa 3, 0(3) # load the value from the address We can delete the test ppc64-got-indirect.s as its purpose is covered by newly added ppc64-toc-relax.s and ppc64-toc-relax-constants.s Reviewed By: ruiu, sfertile Differential Revision: https://reviews.llvm.org/D60958 llvm-svn: 360112
* [ELF] Change std::max<uint64_t> to uint32_t for section alignmentFangrui Song2019-04-261-3/+3
| | | | | | | | | | | | | Summary: We use `uint32_t SectionBase::Alignment` and `uint32_t PhdrEntry::p_align` despite alignments being 64 bits in ELF64. Fix the std::max template arguments accordingly. The currently 160-byte InputSection will become 168 bytes if we make SectionBase::Alignment uint64_t. Differential Revision: https://reviews.llvm.org/D61171 llvm-svn: 359268
* [PPC64] Allow R_PPC64_DTPREL* to preemptable local-dynamic symbolsFangrui Song2019-04-231-2/+1
| | | | | | | | Similar to D60945. Differential Revision: https://reviews.llvm.org/D60994 llvm-svn: 358950
* [LLD][ELF] - Do not forget to use ch_addralign field after decompressing the ↵George Rimar2019-04-221-0/+2
| | | | | | | | | | | | | sections. LLD did not use ELF::Chdr::ch_addralign for decompressed sections. This resulted in a broken output. Fixes https://bugs.llvm.org/show_bug.cgi?id=40482. Differential revision: https://reviews.llvm.org/D60959 llvm-svn: 358885
* [ELF][X86] Allow R_386_TLS_LDO_32 and R_X86_64_DTPOFF{32,64} to preemptable ↵Fangrui Song2019-04-221-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | local-dynamic symbols Summary: Fixes PR35242. A simplified reproduce: thread_local int i; int f() { return i; } % {g++,clang++} -fPIC -shared -ftls-model=local-dynamic -fuse-ld=lld a.cc ld.lld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: i in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output In isStaticLinkTimeConstant(), Syn.IsPreemptible is true, so it is not seen as a constant. The error is then issued in processRelocAux(). A symbol of the local-dynamic TLS model cannot be preempted but it can preempt symbols of the global-dynamic TLS model in other DSOs. So it makes some sense that the variable is not static. This patch fixes the linking error by changing getRelExpr() on R_386_TLS_LDO_32 and R_X86_64_DTPOFF{32,64} from R_ABS to R_DTPREL. R_PPC64_DTPREL_* and R_MIPS_TLS_DTPREL_* need similar fixes, but they are not handled in this patch. As a bonus, we use `if (Expr == R_ABS && !Config->Shared)` to find ld-to-le opportunities. R_ABS is overloaded here for such STT_TLS symbols. A dedicated R_DTPREL is clearer. Differential Revision: https://reviews.llvm.org/D60945 llvm-svn: 358870
* [ELF][X86] Rename R_RELAX_TLS_GD_TO_IE_END to R_RELAX_TLS_GD_TO_IE_GOTPLTFangrui Song2019-04-221-2/+2
| | | | | | | | | | Summary: This relocation type is used by R_386_TLS_GD. Its formula is the same as R_GOTPLT (e.g R_X86_64_GOT{32,64} R_386_TLS_GOTIE). Rename it to be clearer. Differential Revision: https://reviews.llvm.org/D60941 llvm-svn: 358868
* [ELF] Use llvm::bsearch. NFCFangrui Song2019-04-171-5/+3
| | | | | | Differential Revision: https://reviews.llvm.org/D60813 llvm-svn: 358565
OpenPOWER on IntegriCloud