Commit Graph

3861 Commits

Author SHA1 Message Date
Rui Ueyama c9fefaac67 Add comments for AVR support.
AVR support is somewhat exotic as generated ELF executables are not
directly consumed but objcopy'ed to write it to on-chip flush memory.
This comment describes it for those why a full-fledged ELF linker is
used to link programs for the 8-bit microcontroller.

llvm-svn: 305567
2017-06-16 17:53:26 +00:00
Rui Ueyama 21c0a9ceeb Split Target.cpp into small files.
Target.cpp contains code for all the targets that LLD supports. It was
simple and easy, but as the number of supported targets increased,
it got messy.

This patch splits the file into per-target files under ELF/arch directory.

Differential Revision: https://reviews.llvm.org/D34222

llvm-svn: 305565
2017-06-16 17:32:43 +00:00
Peter Smith 3298027951 [ELF] Enable createThunks to be called more than once.
In preparation for supporting range extension thunks we now continually
call createThunks() until no more thunks are added. This requires us to
record the thunks we add on each pass and only merge the new ones into the
OutputSection. We also need to check if a Relocation is targeting a thunk
to prevent us from infinitely creating more thunks.
    
Differential Revision: https://reviews.llvm.org/D34034

llvm-svn: 305555
2017-06-16 13:10:08 +00:00
Rafael Espindola c54b1c8b4d Try to fix MSVC build.
llvm-svn: 305514
2017-06-15 22:03:06 +00:00
Rafael Espindola 383971d2a7 Move clearOutputSections before sortSections.
This is probably the main patch left in unifying our intermediary
representation.

It moves the creation of default commands before section sorting. This
has the nice effect that we now have one location where we decide
where an orphan section should be placed.

Before this patch sortSections would decide the relative location of
orphan sections to other sections, but it was up to placeOrphanSection
to decide on the exact location.

We now only sort sections we created since the linker script is
already in the correct order.

llvm-svn: 305512
2017-06-15 21:51:01 +00:00
Petr Hosek 40f2866a67 [ELF] Mark symbols referenced from linker script as live
This is necessary to ensure that sections containing symbols referenced
from linker scripts (e.g. in data commands) don't get GC'd.

Differential Revision: https://reviews.llvm.org/D34195

llvm-svn: 305452
2017-06-15 05:34:31 +00:00
Leslie Zhai 49ba795d15 [ELF] Initial migration of AVR target
Reviewers: ruiu, rafael, grimar, atanasyan, psmith, dylanmckay

Reviewed By: ruiu, rafael, grimar, dylanmckay

Differential Revision: https://reviews.llvm.org/D32991

llvm-svn: 305444
2017-06-15 02:27:50 +00:00
Zachary Turner a207f6476b Remove lld toStringRef function.
I added the same function in llvm, so this is causing an
ambiguous symbol.

llvm-svn: 305412
2017-06-14 20:20:11 +00:00
Eugene Leviant ad270ec314 [ELF] Add armelf emulation mode
Differential revision: https://reviews.llvm.org/D34140

llvm-svn: 305375
2017-06-14 08:25:38 +00:00
Yuka Takahashi 1f1378dcbc Use getLastArgValue instead of getString
Summary: I found that getString defined in the LLD's Driver.cpp is
exactly the same as Arg::getLastArgValue defined in
llvm/Option/ArgLIst.h. This patch removes that local function and use
the function that the Arg class provides.

Reviewers: ruiu

Subscribers: emaste, llvm-commits

Differential Revision: https://reviews.llvm.org/D34196

llvm-svn: 305374
2017-06-14 08:01:26 +00:00
Rui Ueyama d3a4a3e3e4 Use StringRef::split instead of StringRef::find and StringRef::substr.
Summary: Pointed out by Yuka Takahashi.

Differential Revision: https://reviews.llvm.org/D34192

llvm-svn: 305364
2017-06-14 04:02:40 +00:00
Rafael Espindola e86fddd651 Simplify. NFC.
llvm-svn: 305341
2017-06-13 23:54:34 +00:00
Rafael Espindola f51c80559c Make OutputSections and OutputSectionCommands globals.
This is similar to what we do for InputSections and makes them easier
to access.

llvm-svn: 305337
2017-06-13 23:26:31 +00:00
Rafael Espindola cdf813bbd1 Move clearOutputSections earlier. NFC.
llvm-svn: 305333
2017-06-13 22:36:20 +00:00
Rafael Espindola dece28087e Set non alloc section address to 0 earlier.
Currently we do layout as if non alloc sections had an actual address
and then set it to zero. This produces a few odd results where a
symbol has an address that is inconsistent with the section address.

The simplest way to fix it is probably to just set the address earlier.

The behavior of bfd seems to be similar, but it only sets the non
alloc section address is missing from the linker script or if the
script has an explicit " : 0" setting the address of the output
section (which the default script does).

llvm-svn: 305323
2017-06-13 20:57:43 +00:00
Rui Ueyama f9f69548a9 Allow the GNU gold-style nonstandard SHT_GROUP section.
The ELF standard defines that the SHT_GROUP section as follows:

 - its sh_link has the symbol index, and
 - the symbol name is used to uniquify section groups.

Object files created by GNU gold does not seem to comply with the
standard. They have this additional rule:

 - if the symbol has no name and a STT_SECTION symbol, a section
   name is used instead of a symbol name.

If we don't do anything for this, the linker fails with a mysterious
error message if input files are generated by gas. It is unfortunate
but I think we need to support it.

Differential Revision: https://reviews.llvm.org/D34064

llvm-svn: 305218
2017-06-12 18:46:33 +00:00
Rafael Espindola f846ce259a Fix weak symbols on arm and aarch64.
Given

.weak target
 .global _start
_start:
 b target

The intention is that the branch goes to the instruction after the
branch, effectively turning it on a nop.  The branch adds the runtime
PC, but we were adding it statically too.

I noticed the oddity by inspection, but llvm-objdump seems to agree,
since it now prints things like:

b       #-4 <_start+0x4>

llvm-svn: 305212
2017-06-12 18:05:01 +00:00
Peter Collingbourne bfd5113ca1 ELF: Teach ICF about relocations referring to merge input sections.
Relocations referring to merge sections are considered equal if they
resolve to the same offset in the same output section.

Differential Revision: https://reviews.llvm.org/D34094

llvm-svn: 305177
2017-06-12 00:05:54 +00:00
Peter Collingbourne dc7936eced ELF: Move section merging before ICF. NFCI.
Differential Revision: https://reviews.llvm.org/D34093

llvm-svn: 305176
2017-06-12 00:00:51 +00:00
Rafael Espindola 7ff9329b7c Move clearOutputSections before createPhdrs. NFC.
llvm-svn: 305156
2017-06-10 22:12:32 +00:00
Rafael Espindola 07c8741644 Relax the overflow checking of R_386_PC16.
Currently the freebsd early boot code fails to link. The issue reduces
to 16 bit code at position 0x7000 wanting to jump to position
0x9000. That is represented in the .o file as a relocation with no
symbol and an addend of 0x9000 - 2 (The -2 is because i386 uses the ip
after the current instruction for jumps).

If the addend is interpreted as signed (it should), it is -28674. In a
32 bit architecture, that is the address 0xffff8ffe. To get there from
0x7000 we have to add 4294909950 (too big) or subtract 57346 (too
small). We then produce an error.

What lld is missing is the fact that at runtime this will actually be
a 16 bit architecture and adding 0x1ffe produces 0x8ffe which is the
correct result in 16 bits (-28674).

Since we have a 16 bit addend and a 16 bit PC, the relocation can move
the PC to any 16 bit address and that is the only thing we really need
to check: if the address we are pointing to fits in 16 bits. This is
unfortunately hard to do since we have to delay subtracting the PC and
if we want to do that outside of Target.cpp, we have to move the
overflow check out too.  An incomplete patch that tries to do that is
at https://reviews.llvm.org/D34070

This patch instead just relaxes the check. Since the value we have is
the destination minus the PC and the PC is 16 bits, it should fit in
17 bits if the destination fits in 16 too.

bfd had a similar issue for some time and got a similar fix:
https://sourceware.org/ml/binutils/2005-08/msg00001.html

llvm-svn: 305135
2017-06-10 01:56:58 +00:00
Rui Ueyama 1c837b5fb1 [ICF] Ignore SHF_GROUP flag when comparing two sections.
SHF_GROUP bit doesn't make sense in executables or DSOs, so linkers are
expected to remove that bit from section flags. We did that when we create
output sections.

This patch is to do that earlier than before. Now the flag is dropped when
we instantiate input section objects.

This change improves ICF. Previously, two sections that differ only in
SHF_GROUP flag were not merged, because when the control reached ICF,
the flag was still there. Now the flag is dropped before reaching to ICF,
so the difference is ignored naturally.

This issue was found by pcc.

Differential Revision: https://reviews.llvm.org/D34074

llvm-svn: 305134
2017-06-10 00:38:55 +00:00
Rui Ueyama d97265f792 Simplify. NFC.
llvm-svn: 305112
2017-06-09 21:09:08 +00:00
George Rimar 1840901a2f [ELF] - Allow producing -r output if only empty archive is given.
This is used by linux kernel build system.

(https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt "3.2 Built-in object goals")

It has for example next configuration for linking built-in.o files:
drivers-y	:= $(patsubst %/, %/built-in.o, $(drivers-y))
drivers-$(CONFIG_PCI)		+= arch/ia64/pci/
...
drivers-$(CONFIG_OPROFILE)	+= arch/ia64/oprofile/

Im most simple case all CONFIG_* options are off. That means linker is called with empty input archive, 
emulation option and no inputs and expected to generate some relocatable output. 
ld.bfd is able to do that, we dont.

Patch allows to support this case.

Differential revision: https://reviews.llvm.org/D33937

llvm-svn: 305069
2017-06-09 12:26:57 +00:00
Peter Smith 28285576cb [ELF] Be more precise about Thumb state bit in ARM thunks
The symbols generated for Thunks have type STT_FUNC, to permit a thunk to
be reused via a blx instruction the Thumb bit (0) needs to be set properly.

Differential Revision: https://reviews.llvm.org/D34036

llvm-svn: 305065
2017-06-09 09:51:51 +00:00
George Rimar b4b7b74b5a [ELF] - Fix build bot.
SyntheticSections.cpp:1773:29: error: chosen constructor is explicit in copy-initialization
        CuVectors.push_back({});
                            ^~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/set:428:14: note: constructor declared here
    explicit set(const value_compare& __comp = value_compare())
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:702:59: note: passing argument to parameter '__x' here
    _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);

llvm-svn: 305054
2017-06-09 04:48:56 +00:00
Rui Ueyama f08b38cbe1 Simplify. NFC.
llvm-svn: 305048
2017-06-09 03:19:08 +00:00
Rui Ueyama a1ba859ad3 Simplify. NFC.
llvm-svn: 305047
2017-06-09 02:42:20 +00:00
Rui Ueyama 38a2841665 Use LLVM_FALLTHROUGH.
llvm-svn: 305010
2017-06-08 20:16:21 +00:00
Rui Ueyama 3271d3704a Fix a bug in output section directive.
Previously, it couldn't parse

  SECTIONS .text (0x1000) : { *(.text) }

because "(" was interpreted as the begining of the "(NOLOAD)" directive.

llvm-svn: 305006
2017-06-08 19:47:16 +00:00
Rafael Espindola e76231b647 Move fabricateDefaultCommands earlier.
This then requires delaying a call to getHeaderSize.

llvm-svn: 304961
2017-06-08 04:17:04 +00:00
Rafael Espindola 7204620d32 Use OutputSectionCommands in assignOffsets.
This allows moving clearOutputSections earlier.

llvm-svn: 304952
2017-06-07 23:08:55 +00:00
Rafael Espindola f2c41271ca Delete dead code.
The real offsets are computed in assignAddresses and we compute the
special cases we need earlier after this.

llvm-svn: 304950
2017-06-07 22:57:52 +00:00
Rafael Espindola fa6fcff2e3 Move clearOutputSections earlier. NFC.
llvm-svn: 304948
2017-06-07 22:27:51 +00:00
Rafael Espindola 2356c9ffae Convert an use of OutputSections to OutputSectionCommands.
This allows moving clearOutputSections a bit earlier.

llvm-svn: 304947
2017-06-07 22:23:01 +00:00
Sam Clegg c1e7a0dde5 Add BinaryFormat to lld libraries
Without this, when building with shared BUILD_SHARED_LIBS=ON
I get errors such as:

lib/Core/Reader.cpp:40: error: undefined reference to
'llvm::identify_magic(llvm::StringRef)'

Differential Revision: https://reviews.llvm.org/D34004

llvm-svn: 304932
2017-06-07 18:06:11 +00:00
George Rimar 8666562e89 [ELF] - Make implementation of .gdb index to be more natural for futher paralleling.
This patch reimplements .gdb_index in more natural way,
that makes proccess of switching to multithreaded index building to
be trivial. 

Differential revision: https://reviews.llvm.org/D33552

llvm-svn: 304927
2017-06-07 16:59:11 +00:00
George Rimar fbb0463f39 [ELF] - Linkerscript: implement NOLOAD section type.
This is PR32351

Each output section may have a type. The type is a keyword in parentheses.
(https://sourceware.org/binutils/docs/ld/Output-Section-Type.html#Output-Section-Type)
This patch support only one type, it is NOLOAD.
If output section has such type, we force it to be SHT_NOBITS. 

More details are available on a review page.

Differential revision: https://reviews.llvm.org/D33647

llvm-svn: 304925
2017-06-07 16:31:08 +00:00
Rafael Espindola 66d98495b0 Delete dead function.
llvm-svn: 304914
2017-06-07 14:24:32 +00:00
George Rimar 0641b8b4d3 [ELF] - Simplify readAddressArea() implementation.
Approach significantly simplifies LLD .gdb_index code and makes it much faster.
Also it should resolve issues, like D33176 tries to address once and forever in a clean way.

LLC binary linking without patch and without --gdb-index: 1,599241063
LLC binary linking without patch and with --gdb-index: 6,064316262
LLC binary linking with patch and with --gdb-index: 4,116792104

Time spent for building gdbindex changes from (6,064316262 - 1,599241063 == 4,465075199)
to (4,116792104- 1,599241063 == 2,517551041).
That is 2,517551041/4,465075199 = 0,564 or about 44% speedup.

Differential revision: https://reviews.llvm.org/D33183

llvm-svn: 304895
2017-06-07 10:52:02 +00:00
Peter Smith 8e791463ef [ELF] Convert Thunks to use InputSectionDescriptions
Thunks are now generated per InputSectionDescription instead of per
OutputSection. This allows created ThunkSections to be inserted directly
into InputSectionDescription.

Changes in this patch:
- Loop over InputSectionDescriptions to find relocations to Thunks
- Generate a ThunkSection per InputSectionDescription
- Remove synchronize() as we no longer need it
- Move fabricateDefaultCommands() before createThunks

Differential Revision: https://reviews.llvm.org/D33835

llvm-svn: 304887
2017-06-07 09:35:14 +00:00
George Rimar 990c9cb2bf [ELF] - Do not merge relocation sections by name when using --emit-relocs.
Previously we would merge relocation sections by name.
That did not work in some cases, like testcase shows.

Patch implements logic to merge relocation sections if their target
sections were merged into the same output section.

Differential revision: https://reviews.llvm.org/D33824

llvm-svn: 304886
2017-06-07 09:20:35 +00:00
George Rimar 41c7ab4a3d [ELF] - Linkerscript: improved error reporting.
When linking linux kernel LLD currently reports next errors:

ld: error: unable to evaluate expression: input section .head.text has no output section assigned
ld: error: At least one side of the expression must be absolute
ld: error: At least one side of the expression must be absolute

That does not provide file/line information and overall looks unclear. 
Patch adds location information to ExprValue and that allows
to provide more clear error messages.

Differential revision: https://reviews.llvm.org/D33943

llvm-svn: 304881
2017-06-07 08:54:43 +00:00
Zachary Turner 264b5d9e88 Move Object format code to lib/BinaryFormat.
This creates a new library called BinaryFormat that has all of
the headers from llvm/Support containing structure and layout
definitions for various types of binary formats like dwarf, coff,
elf, etc as well as the code for identifying a file from its
magic.

Differential Revision: https://reviews.llvm.org/D33843

llvm-svn: 304864
2017-06-07 03:48:56 +00:00
Rafael Espindola d57c58d75d Move clearOutputSections earlier. NFC.
This now just requires not calling assignOffsets after it.

llvm-svn: 304861
2017-06-07 02:31:19 +00:00
Rafael Espindola 189860c317 Use assignAddresses with -r.
Before this patch in -r we compute the OutputSection sizes early in
the various calls to assignOffsets. With this change we can remove
most of those calls.

llvm-svn: 304860
2017-06-07 02:24:08 +00:00
Rafael Espindola 658a0c74ec Avoid using OutputSection::Sections. NFC.
We now used the InputSectionDescriptions in
OutputSectionCommand::finalize. This will allow moving
clearOutputSections earlier.

llvm-svn: 304827
2017-06-06 20:13:19 +00:00
Rafael Espindola 8c284acf14 Move finalize to OutputSectionCommands. NFC.
This removes a mapping from OutputSection to OutputSectionCommand and
is another step in moving clearOutputSections earlier.

llvm-svn: 304821
2017-06-06 19:40:01 +00:00
Rafael Espindola 13f412f6a2 Convert a use of OutputSections. NFC.
llvm-svn: 304820
2017-06-06 19:29:36 +00:00
Rafael Espindola 0e454a9837 Define __executable_start.
This is defined by both bfd and gold and used by the android libc.

llvm-svn: 304803
2017-06-06 16:18:48 +00:00