aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF/ScriptParser.cpp
AgeCommit message (Collapse)AuthorFilesLines
2019-04-22[LLD][ELF] - Handle quoted strings in the linker scripts correctly.George Rimar1-1/+1
This is the https://bugs.llvm.org/show_bug.cgi?id=41356, Seems it is kind of unusual case but it is possible to have sections that require quotes for their namings. Like "aaa bbb". This patch adds support for those. Differential revision: https://reviews.llvm.org/D60901 llvm-svn: 358874
2019-04-18[ELF] Respect NonAlloc when copying flags from the previous sectionsFangrui Song1-0/+1
Summary: If the output section contains only symbol assignments, we copy flags from the previous sections. Don't set SHF_ALLOC if NonAlloc is true. We also have to change the type from SHT_NOBITS to SHT_PROGBITS. In ld.bfd, bfd_elf_get_default_section_type maps non-alloctable sections to SHT_PROGBITS. Non-alloctable SHT_NOBITS sections do not make sense. Fixes PR38626 Differential Revision: https://reviews.llvm.org/D59986 llvm-svn: 358650
2019-02-13Recover elf32-bigmips and elf32-powerpc support in LLDRui Ueyama1-1/+2
This fixes a 7.0 -> 8.0 regression when parsing OUTPUT_FORMAT("elf32-powerpc"); or elf32-bigmips directive in ldscripts as well as an unknown emulation error when lld is invoked by clang due to missed elf32ppclinux case. Patch by vit9696 Differential Revision: https://reviews.llvm.org/D58005 llvm-svn: 353968
2019-02-11lld: unquote possibly quoted `EXTERN("symbol")` entry in linker script.Rui Ueyama1-1/+1
gold accepts quoted strings. binutils requires quoted strings for some kinds of symbols, e.g.: it accepts quoted symbols with @ in name: $ echo 'EXTERN("__libc_start_main@@GLIBC_2.2.5")' > a.script $ g++ a.script /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status but rejects them if unquoted: $ echo 'EXTERN(__libc_start_main@@GLIBC_2.2.5)' > a.script $ g++ a.script a.script: file not recognized: File format not recognized collect2: error: ld returned 1 exit status To maintain compatibility with existing linker scripts support quoted strings in lld as well. Patch by Lucian Adrian Grijincu. Differential Revision: https://reviews.llvm.org/D57987 llvm-svn: 353756
2019-01-30Recognize FreeBSD specific BFD names in OUTPUT_FORMATDimitry Andric1-2/+6
Summary: After rLLD344952 ("Add OUTPUT_FORMAT linker script directive support"), using BFD names such as `elf64-x86-64-freebsd` the `OUTPUT_FORMAT` linker script command does not work anymore, resulting in errors like: ``` ld: error: /home/dim/src/clang800-import/stand/efi/loader/arch/amd64/ldscript.amd64:2: unknown output format name: elf64-x86-64-freebsd >>> OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd") >>> ^ ``` To fix this, recognize a `-freebsd` suffix in BFD names, and also set `Configuration::OSABI` to `ELFOSABI_FREEBSD` for those cases. Add and/or update several test cases to check for the correct results of these new `OUTPUT_FORMAT` arguments. Reviewers: ruiu, atanasyan, grimar, hokein, emaste, espindola Reviewed By: ruiu Subscribers: nemanjai, javed.absar, arichardson, krytarowski, kristof.beyls, kbarton, llvm-commits Differential Revision: https://reviews.llvm.org/D57283 llvm-svn: 352606
2019-01-29[ELF] - Remove dead `readBfdName` declaration. NFC.George Rimar1-1/+0
`readBfdName` was removed recently. llvm-svn: 352482
2019-01-28Attempt to fix build failure with GCC 5.4.Rui Ueyama1-2/+2
llvm-svn: 352435
2019-01-28Refactoring. NFC.Rui Ueyama1-36/+24
llvm-svn: 352407
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
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-12-06[LLD][ELF] - Use auto. NFC.George Rimar1-1/+1
This addresses the missed review comment. llvm-svn: 348480
2018-12-06[LLD][ELF] - Linker script: accept using a file name without a list of sections.George Rimar1-1/+6
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39885 Linker script specification says: "You can specify a file name to include sections from a particular file. You would do this if one or more of your files contain special data that needs to be at a particular location in memory." LLD did not accept this syntax. The patch implements it. Differential revision: https://reviews.llvm.org/D55324 llvm-svn: 348463
2018-11-28Fix a typo.Haojian Wu1-1/+1
llvm-svn: 347751
2018-11-28Fix build error.Haojian Wu1-15/+15
llvm-svn: 347750
2018-11-28[ELF][MIPS] Handle mips in the OUTPUT_FORMAT directiveSimon Atanasyan1-16/+26
Differential Revision: http://reviews.llvm.org/D54920 llvm-svn: 347742
2018-11-26[LLD][ELF] - Do not crash when parsing the -defsym option from a error state.George Rimar1-0/+2
When we are in a error state, script parser will not parse the -defsym expression and hence will not tokenize it. Then ScriptLexer::Pos will be 0 and LLD will assert and crash here: MemoryBufferRef ScriptLexer::getCurrentMB() { assert(!MBs.empty() && Pos > 0); // Bang ! Solution - stop parsing the defsym in a error state. That is consistent with the regular case (when we parse the linker script). llvm-svn: 347549
2018-11-14[ELF] Make TrapInstr and Filler byte arrays. NFC.Simon Atanasyan1-7/+7
The uint32_t type does not clearly convey that these fields are interpreted in the target endianness. Converting them to byte arrays should make this more obvious and less error-prone. Patch by James Clarke Differential Revision: http://reviews.llvm.org/D54207 llvm-svn: 346893
2018-11-03Handle OUTPUT_FORMAT arguments in quotation marksGalina Kistanova1-1/+1
llvm-svn: 346068
2018-10-30[PPC64] Handle powerpc64 in OUTPUT_FORMAT.Sean Fertile1-0/+4
Differential Revision: https://reviews.llvm.org/D53652 llvm-svn: 345604
2018-10-22[ELF] Handle elf32-littlearm in OUTPUT_FORMATShoaib Meenai1-0/+2
We need this to support 32-bit ARM. Add test cases for emulation handling for this architecture as well. Differential Revision: https://reviews.llvm.org/D53539 llvm-svn: 344976
2018-10-22Add OUTPUT_FORMAT linker script directive support.Rui Ueyama1-2/+27
This patch adds a support for OUTPUT_FORMAT linker script directive. Since I'm not 100% confident with BFD names you can use in the directive for all architectures, I added only a few in this patch. We can add other names for other archtiectures later. We still do not support triple-style OUTPUT_FORMAT directive, namely, OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian) or -EB (big endian) to the linker, GNU linkers pick up big or little as a BFD name, correspondingly, so that you can use a single linker script for bi-endian processor. I'm not sure if we really need to support that, so I'll leave it alone for now. Note that -m takes precedence over OUTPUT_FORAMT, but we always parse a BFD name given to OUTPUT_FORMAT for error checking. You cannot write an invalid name in the OUTPUT_FORMAT directive. Differential Revision: https://reviews.llvm.org/D53495 llvm-svn: 344952
2018-10-12[lld] Add more complete support for the INCLUDE command.Rui Ueyama1-5/+13
Patch by Ian Tessier. This change adds INCLUDE support to the MEMORY and SECTION commands, and to output sections, as per: https://sourceware.org/binutils/docs/ld/File-Commands.html#File-Commands Differential Revision: https://reviews.llvm.org/D52951 llvm-svn: 344368
2018-08-28[LLD][ELD] - Do not reject INFO output section type when used with a start ↵George Rimar1-21/+26
address. This is https://bugs.llvm.org/show_bug.cgi?id=38625 LLD accept this: ".stack (INFO) : {", but not this: ".stack address_expression (INFO) :" The patch fixes it. Differential revision: https://reviews.llvm.org/D51027 llvm-svn: 340804
2018-08-06Add TARGET(foo) linker script directive.Rui Ueyama1-0/+20
GNU ld's manual says that TARGET(foo) is basically an alias for `--format foo` where foo is a BFD target name such as elf64-x86-64. Unlike GNU linkers, lld doesn't allow arbitrary BFD target name for --format. We accept only "default", "elf" or "binary". This makes situation a bit tricky because we can't simply make TARGET an alias for --target. A quick code search revealed that the usage number of TARGET is very small, and the only meaningful usage is to switch to the binary mode. Thus, in this patch, we handle only TARGET(elf.*) and TARGET(binary). Differential Revision: https://reviews.llvm.org/D48153 llvm-svn: 339060
2018-07-25[ELF] Use search paths for --version-script=Fangrui Song1-1/+1
Summary: This behavior matches ld.bfd -Ld --version-script=t.script a.o Reviewers: ruiu, espindola Subscribers: emaste, arichardson, llvm-commits Differential Revision: https://reviews.llvm.org/D49820 llvm-svn: 337969
2018-07-03[ELF] - Add support for '||' and '&&' in linker scripts.George Rimar1-6/+12
This is https://bugs.llvm.org//show_bug.cgi?id=37976, we had no support, but seems someone faced it. llvm-svn: 336197
2018-06-27[ELF] - Implement linker script OVERLAYs.George Rimar1-0/+62
This is PR36768. Linker script OVERLAYs are described in 4.6.9. Overlay Description of the spec: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html They are used to allow output sections which have different LMAs but the same VAs and used for embedded programming. Currently, LLD restricts overlapping of sections and that seems to be the most desired behaviour for defaults. My thoughts about possible approaches for PR36768 are on the bug page, this patch implements OVERLAY keyword and allows VAs overlapping for sections that within the overlay. Differential revision: https://reviews.llvm.org/D44780 llvm-svn: 335714
2018-04-25[ELF] - Eliminate the AssertCommand.George Rimar1-30/+20
Currently, LLD supports ASSERT as a separate command. We support two forms now. Assign expression-form: . = ASSERT(0x100) (old GNU ld required it and some scripts in the wild are still using something like . = ASSERT((_end - _text <= (512 * 1024 * 1024)), "kernel image bigger than KERNEL_IMAGE_SIZE"); Nowadays above is not a mandatory form and command-like form is commonly used: ASSERT(<expr>, "text); The return value of the ASSERT is Dot. That was implemented in D30171. It looks like (2) is just a short version of (1) then. GNU ld does *not* list ASSERT as a SECTIONS command: https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS Given above we probably can change ASSERT to be an assignment to Dot. That makes the rest of the code much simpler. Patch do that. Differential revision: https://reviews.llvm.org/D45434 llvm-svn: 330814
2018-04-19[ELF] Increase NextGroupId with --end-groupFangrui Song1-0/+2
Reviewers: ruiu, espindola Subscribers: emaste, arichardson, llvm-commits Differential Revision: https://reviews.llvm.org/D45846 llvm-svn: 330379
2018-04-09Add --warn-backrefs to maintain compatibility with other linkersRui Ueyama1-8/+18
I'm proposing a new command line flag, --warn-backrefs in this patch. The flag and the feature proposed below don't exist in GNU linkers nor the current lld. --warn-backrefs is an option to detect reverse or cyclic dependencies between static archives, and it can be used to keep your program compatible with GNU linkers after you switch to lld. I'll explain the feature and why you may find it useful below. lld's symbol resolution semantics is more relaxed than traditional Unix linkers. Therefore, ld.lld foo.a bar.o succeeds even if bar.o contains an undefined symbol that have to be resolved by some object file in foo.a. Traditional Unix linkers don't allow this kind of backward reference, as they visit each file only once from left to right in the command line while resolving all undefined symbol at the moment of visiting. In the above case, since there's no undefined symbol when a linker visits foo.a, no files are pulled out from foo.a, and because the linker forgets about foo.a after visiting, it can't resolve undefined symbols that could have been resolved otherwise. That lld accepts more relaxed form means (besides it makes more sense) that you can accidentally write a command line or a build file that works only with lld, even if you have a plan to distribute it to wider users who may be using GNU linkers. With --check-library-dependency, you can detect a library order that doesn't work with other Unix linkers. The option is also useful to detect cyclic dependencies between static archives. Again, lld accepts ld.lld foo.a bar.a even if foo.a and bar.a depend on each other. With --warn-backrefs it is handled as an error. Here is how the option works. We assign a group ID to each file. A file with a smaller group ID can pull out object files from an archive file with an equal or greater group ID. Otherwise, it is a reverse dependency and an error. A file outside --{start,end}-group gets a fresh ID when instantiated. All files within the same --{start,end}-group get the same group ID. E.g. ld.lld A B --start-group C D --end-group E A and B form group 0, C, D and their member object files form group 1, and E forms group 2. I think that you can see how this group assignment rule simulates the traditional linker's semantics. Differential Revision: https://reviews.llvm.org/D45195 llvm-svn: 329636
2018-04-05[ELF] - Reveal more information in -Map file about assignments.George Rimar1-14/+13
Currently, LLD print symbol assignment commands to the map file, but it does not do that for assignments that are outside of the section descriptions. Such assignments can affect the layout though. The patch implements the following: * Teaches LLD to print symbol assignments outside of section declaration. * Teaches LLD to print PROVIDE/HIDDEN/PROVIDE hidden commands. In case when symbol is not provided, nothing will be printed. Differential revision: https://reviews.llvm.org/D44894 llvm-svn: 329272
2018-03-28[ELF] - Linkerscript: support MIN and MAX.George Rimar1-0/+10
Sample for the OVERLAY command from the spec (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html) uses MAX command that we do not support currently: . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1)); This patch implements support for MIN and MAX. Differential revision: https://reviews.llvm.org/D44734 llvm-svn: 328696
2018-03-15Fix linker script operator precedence.Rui Ueyama1-5/+6
"&" should have higher priority than "|" [1]. Previously, they had the same priority. [1] https://sourceware.org/binutils/docs/ld/Operators.html Differential Revision: https://reviews.llvm.org/D43880 llvm-svn: 327684
2018-03-15[ELF] - Show data and assignment commands in the map file.George Rimar1-3/+15
Patch teaches LLD to print BYTE/SHORT/LONG/QUAD and location move commands to the map file. Differential revision: https://reviews.llvm.org/D44004 llvm-svn: 327612
2018-03-13[ELF] - Implement INSERT BEFORE.George Rimar1-3/+9
This finishes PR35877. INSERT BEFORE used similar to INSERT AFTER, it inserts sections before the given target section. Differential revision: https://reviews.llvm.org/D44380 llvm-svn: 327378
2018-03-12[ELF] - Removed excessive lookup. NFC.George Rimar1-3/+2
llvm-svn: 327272
2018-03-12[ELF] - Change consume()->expect() in INSERT AFTER parsing.George Rimar1-1/+1
AFTER keyword is mandatory and consume() was used by mistake here. We accepted broken script before this patch, testcase shows the issue. llvm-svn: 327260
2018-03-08[ELF] - Support "INSERT AFTER" statement.George Rimar1-1/+12
This implements INSERT AFTER in a following way: During reading scripts it collects all insert statements. After we done and read all files it inserts statements into script commands list. With that: * Rest of code does know nothing about INSERT. * Approach is straightforward and have no visible limitations. * It is also easy to support INSERT BEFORE (was seen in clang code once). * Should work for PR35877 and similar cases. Cons: * It assumes we have "main" scripts that describes sections. Differential revision: https://reviews.llvm.org/D43468 llvm-svn: 327003
2018-03-05Remove redundant casts.Rui Ueyama1-2/+2
llvm-svn: 326753
2018-03-05[ELF] - Report location for div/mod by zero.George Rimar1-19/+20
"division by zero" or "modulo by zero" are not very informative errors and even probably confusing as does not let to know that error is coming from linker script. Patch adds location reporting. Differential revision: https://reviews.llvm.org/D43934 llvm-svn: 326686
2018-03-01[ELF] - Don't crash on broken MEMORY declaration.George Rimar1-3/+7
LLD crashes with broken scripts shown in testcase, because fails to read memory regon name and accesses MemoryRegions's element which is nullptr. Patch fixes it. Differential revision: https://reviews.llvm.org/D43866 llvm-svn: 326431
2018-03-01[ELF] - Do not remove empty sections that use symbols in expressions.George Rimar1-0/+4
This is PR36515. Currenly if we have a script like .debug_info 0 : { *(.debug_info) }, we would not remove this section and keep it in the output. That does not work, because it is common case for debug sections to have a zero address expression. Patch changes behavior so that we remove only sections that do not use symbols in its expressions. Differential revision: https://reviews.llvm.org/D43863 llvm-svn: 326430
2018-02-28Add "%" operator to the linker script.Rui Ueyama1-1/+10
This patch improves compatibility with GNU linkers. Differential Revision: https://reviews.llvm.org/D43883 llvm-svn: 326348
2018-02-28Inline a trivial function. NFC.Rui Ueyama1-5/+1
llvm-svn: 326347
2018-02-16[ELF] - Fix comment. NFC.George Rimar1-1/+1
Addresses forgotten comment for D43071. llvm-svn: 325332
2018-02-16[ELF] - Support COPY, INFO, OVERLAY output sections attributes.George Rimar1-4/+11
This is PR36298. (COPY), (INFO), (OVERLAY) all have the same effect: section should be marked as non-allocatable. (https://www.eecs.umich.edu/courses/eecs373/readings/Linker.pdf, 3.6.8.1 Output Section Type) Differential revision: https://reviews.llvm.org/D43071 llvm-svn: 325331
2018-02-01Relax the grammar of the version script.Rui Ueyama1-0/+5
In GNU linkers, the last semicolon is optional. We can't link libstdc++ with lld because of that difference. Differential Revision: https://reviews.llvm.org/D42820 llvm-svn: 324036
2018-01-25Remove MemRegionOffset. NFC.Rafael Espindola1-2/+2
We can just use a member variable in MemoryRegion. llvm-svn: 323399
2018-01-18Handle parsing AT(ADDR(.foo-bar)).Rafael Espindola1-0/+3
The problem we had with it is that anything inside an AT is an expression, so we failed to parse the section name because of the - in it. llvm-svn: 322801
2018-01-14Remove dead code.Rui Ueyama1-7/+0
parseInt assumed that it could take a negative number literal (e.g. "-123"). However, such number is in reality already handled as a unary operator '-' followed by a number literal, so the number literal is always non-negative. Thus, this code is dead. llvm-svn: 322453
2018-01-12[ELF] - Fix for ld.lld does not accept "AT" syntax for declaring LMA regionGeorge Rimar1-0/+8
AT> lma_region expression allows to specify the memory region for section load address. Should fix PR35684. Differential revision: https://reviews.llvm.org/D41397 llvm-svn: 322359