Commit Graph

3153 Commits

Author SHA1 Message Date
Eugene Leviant 2968547997 [ELF] Fix error reporting for synthetic sections
Synthetic sections don't belong to any input file, but still they
are input sections. Whenever problem occurs with relocations in
these sections lld crashes in error reporting, trying to print
input file name.

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

llvm-svn: 297711
2017-03-14 08:33:45 +00:00
Rui Ueyama fed8b570b7 Make FileOutputBuffer fail early if you pass a directory.
Previously, it created a temporary directory and then failed when
FileOutputBuffer tried to rename that file to the destination file
(which is actually a directory name).

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

llvm-svn: 297679
2017-03-13 22:19:05 +00:00
Rui Ueyama eaeca5ed79 [ELF] Fail the link early if an output path is invalid
Patch from James Henderson.

If a user has a long link, e.g. due to a large LTO link, they do not
wish to run it and find that it failed because there was a mistake in
their command-line, after they waited for some significant amount of
time. This change adds some basic checking of the linker output file
path, which is run shortly after parsing the command-line and linker
script. An error is emitted if LLD cannot write to the specified path.

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

llvm-svn: 297645
2017-03-13 17:24:52 +00:00
Peter Collingbourne f8435a9bda ELF: Resolve _end symbols correctly.
Fix a bug introduced in r297313 which caused them to resolve to the end
of the ELF header in PIEs and DSOs.

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

llvm-svn: 297638
2017-03-13 16:40:20 +00:00
Petr Hosek c970acf29f [ELF] Handle NONE relocations on AArch64
Differential Revision: https://reviews.llvm.org/D30628

llvm-svn: 297525
2017-03-10 22:42:22 +00:00
Rui Ueyama 62d547441d Options such as -omagic or -opt-remarks-* should be interpreted as -o.
Options can start with `-` or `--` unless they start with "o".
Any option that starts with `-o` should be interpreted as an output
file name. This is a quote from the GNU ld man page.

  Note -- there is one exception to this rule.  Multiple letter
  options that start with a lower case 'o' can only be preceded by
  two dashes.  This is to reduce confusion with the -o option.
  So for example -omagic sets the output file name to magic whereas
  --omagic sets the NMAGIC flag on the output.

We didn't handle that properly before.

llvm-svn: 297508
2017-03-10 21:22:28 +00:00
Petr Hosek 7b79321e88 [ELF] Propely handle .eh_frame in linker scripts
Using .eh_frame input section pattern in linker script currently
causes a crash; this is because .eh_frame input sections require
special handling since they're all combined into a synthetic
section rather than regular output section.

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

llvm-svn: 297501
2017-03-10 20:00:42 +00:00
Rui Ueyama f5fce48679 Handle ":" as a regular token character in linker scripts.
This is an alternative to https://reviews.llvm.org/D30500 to simplify the
version definition parser and allow ":" in symbol names.

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

llvm-svn: 297402
2017-03-09 19:23:00 +00:00
George Rimar 0a7412f00b [ELF] - Implemented -znotext
gold linker manual describes them as:

-z text	Do not permit relocations in read-only segments
-z notext Permit relocations in read-only segments (default)

In LLD default is to not permit them. Patch implements -z notext.

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

llvm-svn: 297366
2017-03-09 08:48:34 +00:00
George Rimar 1c74c2f2a8 [ELF] - Do not try to create .eh_frame_hdr for relocatable output.
.eh_frame_hdr is a header constructed for .eh_frame sections.
We do not proccess .eh_frame when doing relocatable output,
so should not try to create .eh_frame_hdr too.
Previous behavior without this patch is segfault.

Fixes PR32118.

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

llvm-svn: 297365
2017-03-09 08:45:25 +00:00
Rui Ueyama 7d271ae21d Fix wrong assertion failure.
Previously, if you have foo=bar in a definition file, this assertion
could fire because when symbols are read from file they could be mangled.
It seems that due to historical reasons underscore mangling scheme is
really ad-hoc, and I cannot find a clean way to handle this. I had
to just de-mangle symbols to search again.

llvm-svn: 297357
2017-03-09 04:47:33 +00:00
Rui Ueyama 9accea6feb Pass archive files to link.exe if they contain at least one native object file.
Some archive files created during chromium build contains both BC
and native files. If that's the case, we want to pass the archive
file to link.exe. Otherwise, the MSVC linker would complain that
there's an unresolved symbol in a given set of files.

I cannot explain why link.exe doesn't complain about the presence
of bitcode files in this case, but it seems link.exe doesn't touch BC.

llvm-svn: 297229
2017-03-07 21:26:10 +00:00
Rui Ueyama e0341db179 Do not pass archive files containing bitcode files to the MSVC Linker.
If /msvclto is specified, we compile bitcode files and pass it to the
MSVC linker, stripping all bitcode files. We haven't stripped archive
files, because I was thinking that the MSVC linker wouldn't touch files
in archive files. When we pass an object file to link.exe, all symbols
have been resolved already, so link.exe shoulnd't need any of the files
in archives.

It turns out that even though link.exe doesn't need to do that, it
seems to try to read each file in all archives. And if there's a non-
COFF file in an archive, it exists with an error message. So we need
to remove archives from the command line too.

llvm-svn: 297191
2017-03-07 19:45:53 +00:00
Peter Smith 24fb250cad Fix Mips LA25 Thunks where Caller and Callee in different OS
This change fixes a bug in which the Mips LA25 Thunks are always assigned
to the same Output section as the caller and not the callee as expected.

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

llvm-svn: 297135
2017-03-07 09:45:04 +00:00
Rafael Espindola 692b2f88d3 Fully precise gc handling of __start and __stop symbols.
This puts us at parity with bfd, which could already gc this case.

I noticed the sections not being gced when linking a modified freebsd
kernel. A section that was not gced and not mentioned in the linker
script would end up breaking the expected layout. Since fixing the gc
is relatively simple and an improvement, that seems better than trying
to hack the orphan placement code.

There are 173 input section in the entire link whose names are valid C
identifiers, so this is probably not too performance critical.

llvm-svn: 297049
2017-03-06 18:48:18 +00:00
Rafael Espindola e937a828c4 Simplify test by producing an executable.
llvm-svn: 296786
2017-03-02 19:19:59 +00:00
George Rimar 3397948515 [ELF] - Fix version-script-extern-exact.s testcase
Previously it would not catch if exact symbol name
matching would change behavior to pattern matching.

llvm-svn: 296740
2017-03-02 11:03:58 +00:00
Peter Collingbourne 1fd02112df Address a few nits pointed out by Sean.
llvm-svn: 296728
2017-03-02 02:24:31 +00:00
Peter Collingbourne ab76a19afb LTO: When creating a local cache, create the cache directory if it does not already exist.
Differential Revision: https://reviews.llvm.org/D30519

llvm-svn: 296726
2017-03-02 02:02:38 +00:00
Rafael Espindola 4368bdb270 Make gc a bit more aggressive.
We were not gcing any section whose name was a C identifier. Both gold
and bfd only keep those if they are used.

To avoid having to create the __start/__stop symbols early or doing
string lookups in resolvedReloc, this patch just looks for undefined
symbols __start/__stop to decide if a section is needed or not.

llvm-svn: 296723
2017-03-02 01:50:34 +00:00
Peter Collingbourne 53aa7c131e Add missing test dependency.
llvm-svn: 296703
2017-03-01 23:11:58 +00:00
Peter Collingbourne e02775f068 ELF: Add ThinLTO caching support.
This patch adds an option named --thinlto-cache-dir, which specifies the
path to a directory in which to cache native object files for ThinLTO
incremental builds.

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

llvm-svn: 296702
2017-03-01 23:00:10 +00:00
Rui Ueyama 2787664db7 Set output section's st_entsize based on input section's st_entsize.
Each input section knows its sh_entsize value, so we can set output
section's sh_entsize based on input sections values.

llvm-svn: 296577
2017-03-01 04:04:23 +00:00
Martell Malone 6b43b7ad49 [ELF] - Allow the Code Model flag when using LTO
Differential Revision: https://reviews.llvm.org/D29445

llvm-svn: 296542
2017-02-28 23:43:26 +00:00
Rui Ueyama 13ed0b691e ELF ICF: Merge only functions.
Previously, LLD merged all read-only sections. So the following
program prints out "true" if -icf=all is specified.

  static const int foo = 1;
  static const int bar = 1;
  int main() { printf("%s\n", &foo == &bar ? "true" : "false"); }

This is somewhat counter-intuitive, and it actually caused nasty issues.
One example is https://bugs.chromium.org/p/chromium/issues/detail?id=682773#c24.

This patch changes the way how it works. Now ICF merges only functions
(i.e. executable sections).

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

llvm-svn: 296534
2017-02-28 22:42:49 +00:00
Rui Ueyama 4d574b324a Fix wrong TLS symbol values.
I do not fully understand why we had these values in the tests, but
the new value matches what ld.bfd and ld.gold set, so I guess that
was just a mistake.

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

llvm-svn: 296505
2017-02-28 19:06:32 +00:00
Rafael Espindola b691ccf0a5 Revert "Add terminator to .eh_frame sections"
This reverts commit r296378.

I am pretty sure this is incorrect. In particular, for just

        .cfi_startproc
        nop
        .cfi_endproc

We now add an extra 4 zeros that neither bfd nor gold add.

llvm-svn: 296503
2017-02-28 18:55:08 +00:00
George Rimar 8732e6a538 [ELF] - Fix confusing gc-debuginfo-tls.s testcase. NFC.
It checked name from one symbol and other 
data from another before.

llvm-svn: 296457
2017-02-28 09:58:18 +00:00
Rui Ueyama 1720ef1343 Add terminator to .eh_frame sections
Patch by Mark Kettenis.

Currenlty ld.lld does not add a terminator (a CIE with its length field
set to zero) to the .eh_frame sections it generates.  While the relevant
standards (the AMD64 SysV ABI and the Linux LSB) are not explicit about
this, such a terminator is expected by some unwinder implementations and
seems to be always emitted by ld.bfd. In addition to that, the Linux LSB

  https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html#EHFRAME

explicitly says that

  The .eh_frame section shall contain 1 or more Call Frame Information
  (CFI) records.

Currently, if the .eh_frame sections of the input files only contain
terminators, ld.lld emits a zero=sized .eh_frame section
which clearly doesn't meet that requirement.

The diff makes sure a terminator gets added to each .eh_frame section
and adjusts all the relevant tests to account for that.  An additional
test isn't needed as these adjustments mean that the existence of the
terminator is tested for by several tests already.

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

llvm-svn: 296378
2017-02-27 20:44:59 +00:00
Rafael Espindola cf8396e4aa Add a test we already get right.
It would have found a problem in a patch I am writing.

llvm-svn: 296339
2017-02-27 13:21:18 +00:00
George Rimar 87b0d68633 [ELF] - Implemented --no-dynamic-linker option
Feature is used for producing static-linked PIE executables
(https://gcc.gnu.org/ml/gcc/2015-06/msg00008.html)
And was implemented in GNU ld https://gcc.gnu.org/ml/gcc/2015-08/msg00099.html

I also found it in linux kernel build system,
though I think that x86/x64 bootloader does not really rely on it.
Seems it used for PPC though.

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

llvm-svn: 296097
2017-02-24 08:26:18 +00:00
Petr Hosek f087e312b6 [ELF] Make __ehdr_start point to ELF file headers
__ehdr_start should be pointing to ELF file headers, not program
headers.

This is a reland of D30319.

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

llvm-svn: 296085
2017-02-24 04:11:02 +00:00
Petr Hosek 9a2bba7251 Revert "[ELF] Make __ehdr_start point to ELF file headers"
This reverts commit r296079.

llvm-svn: 296083
2017-02-24 03:18:59 +00:00
Petr Hosek 701ad3fbf9 [ELF] Make __ehdr_start point to ELF file headers
__ehdr_start should be pointing to ELF file headers, not program
headers.

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

llvm-svn: 296079
2017-02-24 03:00:52 +00:00
Bob Haarman 851b1f866b [COFF] added test for thinlto
Summary: Creates bitcode files suitable for use with ThinLTO, then checks that the linker can build an executable from them.

Reviewers: ruiu, pcc

Reviewed By: pcc

Subscribers: mehdi_amini, Prazek, llvm-commits

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

llvm-svn: 296042
2017-02-23 23:42:24 +00:00
Rafael Espindola 66b4e21534 Convert EhOutputSection to be a synthetic section.
With this we complete the transition out of special output sections,
and with the previous patches it should be possible to merge
OutputSectionBase and OuputSection.

llvm-svn: 296023
2017-02-23 22:06:28 +00:00
Rafael Espindola 82d6725c3a Add a test showing that nocopyreloc is only about copy relocs.
For functions the linker uses a related hack: creating a plt in the
main executable that preempts the function.

Like bfd and gold, we don't disable it with nocopyreloc.

llvm-svn: 295976
2017-02-23 14:41:24 +00:00
Rui Ueyama a7e87252ce Always add PT_GNU_STACK.
If -z stack-size is given, we need to add PT_GNU_STACK even if
-z execstack is not given.

llvm-svn: 295945
2017-02-23 08:09:51 +00:00
Rui Ueyama b7f39b08f4 Slightly improve an error message.
llvm-svn: 295942
2017-02-23 07:35:30 +00:00
Petr Hosek b27bb59a5d [ELF] Ignore R_*_NONE relocs when relocating non-alloc sections
We shouldn't report an error for R_*_NONE relocs since we're emitting
them when writing relocations to discarded sections.

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

llvm-svn: 295936
2017-02-23 06:22:28 +00:00
Rui Ueyama 85d54b050e Fix /msvclto.
Previously, bitcode files in library paths were passed to the MSVC linker.
This patch strips them.

llvm-svn: 295913
2017-02-23 00:26:42 +00:00
Peter Collingbourne 6321251a4e ELF: Simplify the thinlto.ll test and verify that importing is working correctly.
llvm-svn: 295901
2017-02-22 23:06:49 +00:00
Rui Ueyama 99861f4730 Attempt to placate MSVC buildbot.
I really do not understand what is going on on some Windows buildbots,
but FileCheck command on some buildbot behaves like long lines were
truncated. I'll try to find a cause of the issue, but let me relax the
test so that they'll succeed on all buildbots.

llvm-svn: 295798
2017-02-22 00:32:56 +00:00
Petr Hosek 5e51f7d24e [ELF] Insert linkerscript symbols directly into symbol table
This change exposes the symbol table insert method and uses it to
insert the linkerscript defined symbols directly into the symbol
table to avoid unnecessarily pulling the object out of an archive.

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

llvm-svn: 295780
2017-02-21 22:32:51 +00:00
Rui Ueyama f9e8034c9c Add `-z nocopyreloc` option.
This option disable creating copy relocations. ld.bfd and ld.gold
have the same option.

llvm-svn: 295772
2017-02-21 21:41:50 +00:00
George Rimar 78ef645f94 [ELF] - Do not segfault when using --gc-sections with linker script
Patch fixes PR32024.

Sections that were not marked as Live has null output section.
Previously we tried to access that field and segfaulted.

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

llvm-svn: 295727
2017-02-21 15:46:43 +00:00
Ed Schouten c16bc13511 Add a test for the feature introduced in r295240.
r295240 tweaked LLD to generate a symbol table when passing in
--export-dynamic, even when creating static executables. Add a test to
make sure this never regresses.

Reviewed by:	ruiu, rafael
Differential Revision:	https://reviews.llvm.org/D30175

llvm-svn: 295725
2017-02-21 15:34:41 +00:00
George Rimar 6d8957b979 [ELF] - Shortify at-addr.s testcase.
llvm-svn: 295724
2017-02-21 15:10:30 +00:00
George Rimar ae4761c186 [ELF] - Postpone evaluation of LMA offset.
Previously we evaluated the values of LMA incorrectly for next cases:

.text : AT(ADDR(.text) - 0xffffffff80000000) { ... }
.data : AT(ADDR(.data) - 0xffffffff80000000) { ... }
.init.begin : AT(ADDR(.init.begin) - 0xffffffff80000000) { ... }

Reason was that we evaluated offset when VA was not assigned. For case above
we ended up with 3 loads that has similar LMA and it was incorrect.
That is critical for linux kernel.

Patch updates the offset after VA calculation. That fixes the issue.

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

llvm-svn: 295722
2017-02-21 15:08:18 +00:00
George Rimar 2ee2d2dcb5 [ELF] - Improve diagnostic messages for move location counter errors.
Previously LLD would error out just "ld.lld: error: unable to move location counter backward"
What does not really reveal the place of issue,
Patch adds location to the output.

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

llvm-svn: 295720
2017-02-21 14:50:38 +00:00