Commit Graph

5581 Commits

Author SHA1 Message Date
Rui Ueyama bfc1d9d976 Remove DefinedElf class.
DefinedElf was a superclass of DefinedRegular and SharedSymbol classes
and represented the notion of defined symbols created for ELF symbols.

It turned out that we didn't use that class often. We had only two
occurrences of dyn_cast'ing to DefinedElf, and both were easily
rewritten without it.

The class was also a bit confusing. The concept of "created for ELF
symbol" is orthogonal to defined/undefined types. However, we had
two distinct classes, DefinedElf and UndefinedElf.

This patch simply removes the class. Now the class hierarchy is one
level shallower.

llvm-svn: 265234
2016-04-02 18:06:18 +00:00
Sean Silva abd25c9b3e Call cl::ParseCommandLineOptions from the driver.
Thanks to Rui for the suggestion; this simplifies things.

llvm-svn: 265213
2016-04-02 04:08:02 +00:00
Davide Italiano 4fda708624 [LTO] Fix symbols which were internalized incorrectly.
If a symbol is defined in an archive, when we replace its body
the isUsedInRegularObj wasn't set correctly. Internalize makes
its decision based on that bit so we ended up internalizing
symbols that we shouldn't (because they're referenced).
This should fix. Thanks to Peter and Rafael for discussion
and help diagnosing the issue!

Found during LTO of unittests.

llvm-svn: 265208
2016-04-02 02:10:40 +00:00
Sean Silva 242bbdb7bf Fix MSVC build after r265206
c:\b\slave\sanitizer-windows\llvm\tools\lld\elf\Config.h(94) : error C2797: 'lld:🧝:Configuration::MLlvm': list initialization inside member initializer list or non-static data member initializer is not implemented

llvm-svn: 265207
2016-04-02 01:51:51 +00:00
Sean Silva 8e4ad5dbf8 PR27104: Add -mllvm option
The argv[0] is based on the analogous thing in clang.

llvm-svn: 265206
2016-04-02 01:39:56 +00:00
Rafael Espindola 059f3fb8fb Don't create a plt when LD access is optimized.
llvm-svn: 265203
2016-04-02 00:19:22 +00:00
Rafael Espindola cf3b04dab9 Don't create a PLT when we optimize out the plt use.
llvm-svn: 265202
2016-04-01 23:36:56 +00:00
Rui Ueyama e8a45e48f9 Rename a few variables. NFC.
We had Phdr, PHdr and Phdrs in one line. That was a bit confusing.

llvm-svn: 265194
2016-04-01 22:42:04 +00:00
Rafael Espindola 36404d0030 Simplify if. NFC.
llvm-svn: 265166
2016-04-01 18:04:21 +00:00
George Rimar 687788c90e [ELF] - Split Writer::assignAddresses(): extract code for initializing dummies sections
Extracts code for initializing dummies sections
to avoid possible duplication in following patches.

Differential review: http://reviews.llvm.org/D18691

llvm-svn: 265159
2016-04-01 17:30:52 +00:00
Rui Ueyama f7f52ef65d Make error handling consistent.
Some functions in Writer reports error using HasError, and some reports
their return values. This patch makes them to consistently use HasError.

llvm-svn: 265156
2016-04-01 17:24:19 +00:00
Rui Ueyama 8ecc2ec0e3 Remove dead code.
Thanks to George Rimor for pointing it out.

llvm-svn: 265155
2016-04-01 17:17:14 +00:00
Rui Ueyama a63baf176f Don't do extra work if -r is given.
fixAbsoluteSymbols fixes linker-created symbol addresses. Since we don't
create such symbols for relocatable output, we don't need to call this
function.

llvm-svn: 265154
2016-04-01 17:11:42 +00:00
Rui Ueyama e044e9cf80 Tidy up address and file offset assignments in the Writer.
assignAddressesRelocatable function did not set addresses to sections
despite its name. What it actually did is to set file offsets to sections.
assignAddresses function assigned addresses and file offsets to sections.
So there was a confusion what they were doing, and they had duplicate code.

This patch separates file offset assignments from address assignments.
A new function, assignFileOffsets assign file offsets. assignAddresses
do not care about file offsets anymore.

llvm-svn: 265151
2016-04-01 17:07:17 +00:00
Rafael Espindola 790db9c458 Bring r264761 back with an extra fix.
The extra fix is to note that it still requires copy relocations.

Original message:

Change how we handle R_MIPS_LO16.

Mips aligns PT_LOAD to 16 bits (0x10000). That means that the lower 16
bits are always the same, so we can, effectively, say that the
relocation is relative.

P.S.: Suggestions for a better name for the predicate are welcome :-)

llvm-svn: 265150
2016-04-01 17:00:36 +00:00
Rafael Espindola 2ee0092053 Don't apply a recolation that the dynamic linker will rewrite.
This matches the behavior of both bfd and gold. Looks like we just got
here for mips because of a bad ordering of an if else chain.

llvm-svn: 265147
2016-04-01 16:21:09 +00:00
Rafael Espindola 666625b78c Pass a const pointer to getImplicitAddend. NFC.
llvm-svn: 265143
2016-04-01 14:36:09 +00:00
Rafael Espindola 31d2ada6d5 Refactor duplicated code.
We had almost identical code to handle creating a plt entry in two
places.

llvm-svn: 265142
2016-04-01 14:14:48 +00:00
Rafael Espindola b97f4beec4 Avoid creating duplicated relocations.
llvm-svn: 265139
2016-04-01 12:54:27 +00:00
George Rimar 5a5c39ca2a Fix linux buildbot after r265136
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/20138

Fix: Move method above class that uses it.
Works fine with MSVS.

llvm-svn: 265137
2016-04-01 12:24:28 +00:00
George Rimar e2da42a762 Make dummySectionsNum() to be not a member of writer. NFC.
llvm-svn: 265136
2016-04-01 11:59:32 +00:00
George Rimar b345e0d15c Split Writer::assignAddresses(): extract common code to fixFileOff(). NFC.
llvm-svn: 265131
2016-04-01 11:04:47 +00:00
George Rimar 900a260357 Split Writer::assignAddresses(): extract assignPhdrs(). NFC.
llvm-svn: 265130
2016-04-01 10:49:14 +00:00
George Rimar efded31a61 [ELF] - Move calculation of _end to fixAbsoluteSymbols()
That is consistent with other symbols: _edata, _etext
and can help to avoid duplicate code.

Differential revision: http://reviews.llvm.org/D18655

llvm-svn: 265129
2016-04-01 10:23:32 +00:00
Davide Italiano 8eca282dc9 [LTO] Inherit options from Codegen before initializing TargetMachine.
This fixes bootstrap of llvm-tblgen (with LTO) and PR27150.
Slightly longer explanation follows.

Emission of .init_array instead of .ctors is supported only on a
subset of the Target LLVM supports. Codegen needs to be conservative
and always emit .ctors unless instructed otherwise (based on target).
If the dynamic linker sees .init_array it completely ignores
what's inside .ctors and therefore some constructors are not called
(and this causes llvm-tblgen to crash on startup).

Teach LLD/LTO about the Codegen options so we end up always emitting
.init_array and avoid this issue.
In future, we might end up supporting mix of .ctors and .init_array
in different input files if this shows up as a real-world use case.
The way gold handles this case is mapping .ctors from input into
.init_array in output. There's also another caveat because
as far as I understand .ctors run in reverse order so when we do
the copy/mapping we need to reverse copy in the output if there's
more than one ctor. That's why I'd rather avoid this complicate logic
unless there's a real need.

An analogous reasoning holds for .dtors/.fini_array.

llvm-svn: 265085
2016-04-01 00:35:29 +00:00
Peter Collingbourne d418b1da38 ELF: Correctly handle --whole-archive for thin archives.
Differential Revision: http://reviews.llvm.org/D18669

llvm-svn: 265073
2016-03-31 23:12:18 +00:00
Simon Atanasyan 13f6da1d2c [ELF] Implement infrastructure for thunk code creation
Some targets might require creation of thunks. For example, MIPS targets
require stubs to call PIC code from non-PIC one. The patch implements
infrastructure for thunk code creation and provides support for MIPS
LA25 stubs. Any MIPS PIC code function is invoked with its address
in register $t9. So if we have a branch instruction from non-PIC code
to the PIC one we cannot make the jump directly and need to create a small
stub to save the target function address.
See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

- In relocation scanning phase we ask target about thunk creation necessity
by calling `TagetInfo::needsThunk` method. The `InputSection` class
maintains list of Symbols requires thunk creation.

- Reassigning offsets performed for each input sections after relocation
scanning complete because position of each section might change due
thunk creation.

- The patch introduces new dedicated value for DefinedSynthetic symbols
DefinedSynthetic::SectionEnd. Synthetic symbol with that value always
points to the end of the corresponding output section. That allows to
escape updating synthetic symbols if output sections sizes changes after
relocation scanning due thunk creation.

- In the `InputSection::writeTo` method we write thunks after corresponding
input section. Each thunk is written by calling `TargetInfo::writeThunk` method.

- The patch supports the only type of thunk code for each target. For now,
it is enough.

Differential Revision: http://reviews.llvm.org/D17934

llvm-svn: 265059
2016-03-31 21:26:23 +00:00
Peter Collingbourne 098c3fcb03 ELF: Add more variants of ignored flags.
Differential Revision: http://reviews.llvm.org/D18668

llvm-svn: 265058
2016-03-31 21:18:34 +00:00
Rui Ueyama b339b6de0d Define a utility function to read -O and lto-O options.
llvm-svn: 265056
2016-03-31 21:15:31 +00:00
Peter Collingbourne ed22f9b6f1 ELF: Add flag for controlling LTO optimization level.
Differential Revision: http://reviews.llvm.org/D18667

llvm-svn: 265053
2016-03-31 21:00:27 +00:00
Ed Maste e2b7677c9e lld: accept and ignore _fbsd suffix in emulation name
Differential Revision:	http://reviews.llvm.org/D18661

llvm-svn: 265044
2016-03-31 20:26:30 +00:00
Rafael Espindola 4e5ab42e21 Pass a const SymbolBody to needsGot. NFC.
llvm-svn: 265012
2016-03-31 13:38:28 +00:00
Rafael Espindola 66ea7bb2c1 Fix the alignment check.
We have to check the final value that is written.

I don't think this has any real word implications (unless something
supports unaligned instructions), but unblocks simplifying the handling
of PC relative relocations.

llvm-svn: 265009
2016-03-31 12:09:36 +00:00
Pete Cooper f4d7688e4b Revert "Remove useless unreachable. Switch coverage already gives us this. NFC"
This reverts commit r264945.

The commit only removed an unreachable in a method with a covered switch, but
GCC is likely to warn on this, and the coding standards recommend just leaving
in the unreachable.

llvm-svn: 264983
2016-03-31 01:23:23 +00:00
Pete Cooper eadf9e4ec9 Remove unused variants of make_dynamic_error_code. NFC.
make_dynamic_error_code was used to create a std::error_code with
a std::string message.  Now that we are migrating to llvm::Error,
there are no calls to these make_dynamic_error_code methods.

There is one single call to make_dynamic_error_code remaining, the one
inside GenericError::convertToErrorCode().  That method is only called
from File::doParse() which should be a temporary situation.  We need
to work out how to deal with File::parse() caching the error result from
doParse().  Caching errors isn't supported in the new scheme, and probably
isn't needed here, but we need to work that out.

Once thats done, dynamic error and all utilities around it can be deleted.

llvm-svn: 264982
2016-03-31 01:21:50 +00:00
Pete Cooper 2f6216c181 Use Expected<T> instead of ErrorOr<T>in yaml reader. NFC
llvm-svn: 264981
2016-03-31 01:13:04 +00:00
Pete Cooper d0a643e7dc Change library search methods to return Optional instead of ErrorOr.
These methods weren't really throwing errors.  The only error used
was that a file could not be found, which isn't really an error at all
as we are searching paths and libraries for a file.  All of the callers
also ignored errors and just used the returned path if one was available.

Changing to return Optional<StringRef> as that actually reflects what
we are trying to do here: optionally find a given path.

llvm-svn: 264979
2016-03-31 01:09:35 +00:00
Pete Cooper dc59c794d0 Fix a bunch more of -Wpessimizing-move issues.
Thanks to Rui for pointing out this warning was firing.

llvm-svn: 264978
2016-03-31 00:38:02 +00:00
Pete Cooper e487da165c Fix a bunch of -Wpessimizing-move issues.
Thanks to Rui for pointing out this warning was firing.

llvm-svn: 264977
2016-03-31 00:35:50 +00:00
Pete Cooper 514594bdd3 Convert a few macho reader/writer helpers to new error handling. NFC.
These methods were responsible for some of the few remaining calls
to llvm::errorCodeToError.  Converting them makes us have more Error's
in the api and fewer error_code's.

llvm-svn: 264974
2016-03-31 00:08:16 +00:00
Pete Cooper c6e7b8146a Convert readBinary to llvm::Error. NFC
llvm-svn: 264973
2016-03-30 23:58:24 +00:00
Pete Cooper ec4e166a5a Convert normalized file to atoms methods to new error handling. NFC.
This converts almost all of the error handling in atom creation
to llvm::Error instead of std::error_code.

llvm-svn: 264968
2016-03-30 23:43:27 +00:00
Pete Cooper 8eaf62ca5d Fix MachO test which is failing on a Windows bot.
This is breaking http://lab.llvm.org:8011/builders/lld-x86_64-win7/builds/31647/steps/test%20lld/logs/stdio.

The issue seems to be that it can't write to a file in /tmp, probably because that path doesn't
exist on Windows.  This was failing after I added EXPECT_FALSE(ec) in r264961 for the error
handling migration.

llvm-svn: 264962
2016-03-30 23:28:49 +00:00
Pete Cooper fefbd22814 Convert lld file writing to llvm::Error. NFC.
This converts the writeFile method, as well as some of the ones it calls
in the normalized binary file writer and yaml writer.

llvm-svn: 264961
2016-03-30 23:10:39 +00:00
Simon Atanasyan cf8c42f546 [ELF][MIPS] Revert r264761 and add test case to demonstrate the problem
If we make R_MIPS_LO16 a relative relocation, linker:
- never creates R_MIPS_COPY relocation for it
- attempts to create R_MIPS_REL32 dynamic relocation if R_MIPS_LO16's
  target is a preemptible symbol

Differential Revision: http://reviews.llvm.org/D18607

llvm-svn: 264956
2016-03-30 22:43:14 +00:00
Paul Robinson 56a97912cb Update copyright year to 2016.
llvm-svn: 264953
2016-03-30 22:40:59 +00:00
Rui Ueyama 1e0f022f05 Fix -Wpessimizing-move warnings.
llvm-svn: 264951
2016-03-30 22:40:16 +00:00
Pete Cooper 63d1aba2ce Remove useless unreachable. Switch coverage already gives us this. NFC
llvm-svn: 264945
2016-03-30 22:34:37 +00:00
Paul Robinson a8305830db Docs: keep copyright years up-to-date.
llvm-svn: 264942
2016-03-30 22:25:04 +00:00
Davide Italiano bbaf5e0acf [LTO] Add a test to ensure we treat externally available symbols correctly.
We already get it right, but there was no coverage for it.

llvm-svn: 264925
2016-03-30 21:01:14 +00:00
Pete Cooper 3c40b5b750 Convert file handle* methods to llvm::Error instead of std::error_code. NFC.
This updates most of the file handling methods in the linking context and
resolver to use the new API.

llvm-svn: 264924
2016-03-30 20:56:54 +00:00
Pete Cooper 96be123198 Change loadFileList to llvm::Error. NFC
llvm-svn: 264921
2016-03-30 20:44:14 +00:00
Pete Cooper c73c9d273d Convert lld Pass::runOnFile to llvm::Error from std::error_code. NFC.
Pretty mechanical change here.  Just replacing all the std::error_code() with
llvm::Error() and make_dynamic_error_code with make_error<GenericError>

llvm-svn: 264917
2016-03-30 20:36:31 +00:00
Rui Ueyama 8dbbb2555b Attempt to fix test failure on Windows.
Windows seems to complain that the file cannot be removed because
it is still in use. We don't have to remove the file but instead
just overwrite it, so do that.

llvm-svn: 264915
2016-03-30 20:25:26 +00:00
Reid Kleckner 750e76d245 Fix the detection of the shell feature and disable some tests when its not present
llvm-svn: 264912
2016-03-30 20:15:50 +00:00
Reid Kleckner 4f88914c5b Remove unused fwd decl for LLVM IR stuff that lives in LTO now
llvm-svn: 264911
2016-03-30 20:15:41 +00:00
Pete Cooper 1e00911ee0 Change getReferenceInfo/getPairReferenceInfo to use new Error handling. NFC.
Adds a GenericError class to lld/Core which can carry a string.  This is
analygous to the dynamic_error we currently use in lld/Core.

Use this GenericError instead of make_dynamic_error_code.  Also, provide
an implemention of GenericError::convertToErrorCode which for now converts
it in to the dynamic_error_code we used to have.  This will go away once
all the APIs are converted.

llvm-svn: 264910
2016-03-30 20:15:06 +00:00
Rui Ueyama 4709190376 Split Writer::assignAddresses. NFC.
llvm-svn: 264905
2016-03-30 19:41:51 +00:00
Adhemerval Zanella 69b29b2b7c [lld] [ELF/AArch64] Add aarch64 TLS IE to LE relax for local symbol test
This patch add a TLS relax optimization test when transforming
Initial-Exec to Local-Exec for local symbols (which can not be preempted).

llvm-svn: 264903
2016-03-30 19:12:18 +00:00
Rui Ueyama 38dc83417b Include line number in error message for linker scripts.
This patch is based on http://reviews.llvm.org/D18545 written
by George Rimar.

llvm-svn: 264878
2016-03-30 16:51:57 +00:00
Rafael Espindola 287e100db2 No relocation needs bot SA and ZA.
Pass only one of them to relocateOne.

llvm-svn: 264866
2016-03-30 13:27:50 +00:00
Rafael Espindola 8cc68c313b Implement getImplicitAddend for mips.
llvm-svn: 264865
2016-03-30 13:18:08 +00:00
Rafael Espindola abc9a12929 Simplify mips addend processing.
It is now added to the addend in the same way as a regular Elf_Rel
addend.

llvm-svn: 264864
2016-03-30 12:45:58 +00:00
Rafael Espindola da99df366d Fix handling of addends on i386.
Because of merge sections it is not sufficient to just add them while
applying a relocation.

llvm-svn: 264863
2016-03-30 12:40:38 +00:00
Ismail Donmez 22921c9cf5 Fix shared build after r264790
llvm-svn: 264844
2016-03-30 08:31:46 +00:00
George Rimar f1c0bf5b40 [ELF] - Do not keep undefined locals in .symtab
gold and bfd do not include the undefined locals in symtab.
We have no reasons to support that either.

That fixes PR27016

Differential revision: http://reviews.llvm.org/D18554

llvm-svn: 264843
2016-03-30 08:16:11 +00:00
Rui Ueyama f43d15009c Fix comments.
The original comments were separated by new code that is irrelevant to
the comment. This patch moves the comment to the right place and update it.

llvm-svn: 264816
2016-03-30 00:43:49 +00:00
Davide Italiano 49fe4eda4c [LTO] Add a comment to explain how we handle @llvm.used.
Requested by: Rui Ueyama.

llvm-svn: 264809
2016-03-29 23:57:22 +00:00
Rafael Espindola 163974dd33 Simplify AHL handling.
This simplifies a few things

* Read the value as early as possible, instead of passing a pointer to
  the location.
* Print the warning for missing pair close to where we find out it is
  missing.
* Don't pass the value to relocateOne.

llvm-svn: 264802
2016-03-29 23:05:59 +00:00
Davide Italiano 47c33f0387 [ELF] Drive-by cleanup, make LTO.cpp clang-format clean.
llvm-svn: 264791
2016-03-29 21:48:25 +00:00
Davide Italiano 86f2bd5ca1 [LTO] Teach LTO about @llvm.used global.
If a symbol appears in @llvm.used, the linker is forced to treat
the symbol as there's a reference to it.

llvm-svn: 264790
2016-03-29 21:46:35 +00:00
Rui Ueyama 319d7304c3 Simplify. NFC.
llvm-svn: 264785
2016-03-29 20:53:21 +00:00
Rui Ueyama 4e62db4167 Replace a FIXME with a regular comment.
Because it doesn't have to be fixed even though it is probably
better to do.

llvm-svn: 264772
2016-03-29 19:19:03 +00:00
Rui Ueyama 01ddc06484 Make BitcodeCompiler::compile a non-template function. NFC.
llvm-svn: 264770
2016-03-29 19:08:46 +00:00
Rafael Espindola cba3e8b4b5 Change how we handle R_MIPS_LO16.
Mips aligns PT_LOAD to 16 bits (0x10000). That means that the lower 16
bits are always the same, so we can, effectively, say that the
relocation is relative.

llvm-svn: 264761
2016-03-29 18:18:19 +00:00
Simon Atanasyan 121e5bc95f [ELF][MIPS] Remove redundant comments from the test. NFC.
Now llvm-objdump prints target function name for `jal` instruction by itself.

llvm-svn: 264732
2016-03-29 14:26:24 +00:00
Simon Atanasyan d2980d3e20 [ELF][MIPS] Reduce number of redundant entries in the local part of MIPS GOT
Local symbol which requires GOT entry initialized by "page" address.
This address is high 16 bits of sum of the symbol value and the relocation
addend. In the relocation scanning phase final values of symbols are unknown
so to reduce number of allocated GOT entries do the following trick. Save
all output sections referenced by GOT relocations during the relocation
scanning phase. Then later in the `GotSection::finalize` method calculate
number of "pages" required to cover all saved output sections and allocate
appropriate number of GOT entries. We assume the worst case - each 64kb
page of the output section has at least one GOT relocation against it.

Differential Revision: http://reviews.llvm.org/D18349

llvm-svn: 264730
2016-03-29 14:07:22 +00:00
George Rimar 9e18c7f81a Forgot to address last minor review comment in r264708. Doing that now.
llvm-svn: 264711
2016-03-29 09:37:17 +00:00
George Rimar 1e4b39f184 [ELF, PR27091] - Implemented -t/--trace option
-t/--trace
Print the names of the input files as ld processes them.
This fixes https://llvm.org/bugs/show_bug.cgi?id=27091.

Differential revision: http://reviews.llvm.org/D18517

llvm-svn: 264708
2016-03-29 08:45:40 +00:00
George Rimar 86971050cb [ELF] - Error out when R_X86_64_PC32/R_X86_64_32 are used against preemptible symbol when linking shared object.
When R_X86_64_PC32/R_X86_64_32 relocations are
used against preemptible symbol and output is position independent,
error should be generated.

Differential revision: http://reviews.llvm.org/D18190

llvm-svn: 264707
2016-03-29 08:35:42 +00:00
Davide Italiano 3acdfeee11 [LTO] Don't internalize if --export-dynamic is passed.
We treat that in the same way we treat shared libraries.

llvm-svn: 264698
2016-03-29 04:34:09 +00:00
Davide Italiano f6523aecd7 Revert r264961. I didn't have asserts enable when testing.
llvm-svn: 264692
2016-03-29 02:20:10 +00:00
Davide Italiano a50e0b97f1 [LTO] Include bitcode symbol name in unreachable messages.
llvm-svn: 264691
2016-03-29 01:40:07 +00:00
Davide Italiano 04d6aa2b38 [LTO] Include in .symtab/.dynsym symbols introduced by optimizations.
Some optimizations, e.g. SimplifyLibCalls, can replace functions with
others as part of the lowering, e.g. printf => puts.
The new symbols don't have the isUsedInRegularObj flag set so they
don't get included in the final symbol table (and dynamic symbol
table), and the dynamic linker gets confused. Include  them as a fix.

Differential Revision: http://reviews.llvm.org/D18357

llvm-svn: 264688
2016-03-29 00:15:52 +00:00
Rui Ueyama 46a8e29587 Remove dead flags.
searchArchivesToOverrideTentativeDefinitions and
searchSharedLibrariesToOverrideTentativeDefinitions are always false.
For the dead flags, we have a fairly large amount of code which is
never be executed.

http://reviews.llvm.org/D17791

llvm-svn: 264653
2016-03-28 20:59:47 +00:00
Davide Italiano 828ac541ef [LTO] Internalize symbols.
IPO doesn't work very well across symbols referenced
by others TUs. The linker here tries to evaluate
which symbols are safe to internalize and switches
their linkage.

Differential Revision:  http://reviews.llvm.org/D18415

llvm-svn: 264585
2016-03-28 15:44:21 +00:00
Rafael Espindola 0727553405 R_AARCH64_PREL64 is relative.
llvm-svn: 264560
2016-03-28 01:31:11 +00:00
Davide Italiano 1460e9f1ca [ELF/LTO] Refactor to reduce indentation.
Suggested by: Rui Ueyama.

llvm-svn: 264518
2016-03-26 18:33:09 +00:00
Reid Kleckner 20e24193f3 Remove declaration of SymbolTable::codegen, this method was deleted in r264091
llvm-svn: 264441
2016-03-25 18:20:33 +00:00
Reid Kleckner 9cd77ce169 [coff] Accept and ignore another link.exe flag for compatibility
This flag disables link.exe's crash handler so that normal windows error
reporting and crash dumping occurs. For now it is reasonable for LLD to
ignore the flag.

Chromium is currently using this flag to collect minidumps of link.exe
crashing, and it breaks the LLD build.

llvm-svn: 264439
2016-03-25 18:09:29 +00:00
George Rimar 836dfffabd Trailing whitespaces removed. NFC.
llvm-svn: 264401
2016-03-25 13:57:20 +00:00
Adhemerval Zanella 3db3f6d83a Simplify AArch64 isRelRelative
This patch simplifies the isRelRelative for AArch64 and add the missing
ones for bootstrap and test-suite.  It also adds more testing for
shared object creation.

llvm-svn: 264322
2016-03-24 19:12:14 +00:00
Pete Cooper 107ac9e1aa Use None to construct an empty ArrayRef. NFC.
Suggested by David Blaikie in response to r264234.

llvm-svn: 264311
2016-03-24 17:39:34 +00:00
Simon Atanasyan 628fe404f7 [ELF] Follow-up to r264300. Update test case expectations to reflect recent changes in llvm-readobj. NFC.
llvm-svn: 264304
2016-03-24 16:30:20 +00:00
Rafael Espindola 5432287bad Make needsPlt a plain function instead of a template.
llvm-svn: 264267
2016-03-24 12:55:27 +00:00
Rafael Espindola 88a9d7608f Mark R_AARCH64_CONDBR19 as relative.
llvm-svn: 264262
2016-03-24 12:16:46 +00:00
Pete Cooper b29ecbd4e9 Avoid UB when creating empty atoms. NFC.
The stack-size.yaml test had an empty atom content array.  This is
legal, but asking a BumpPtrAllocator for 0 sized data may not be
legal.  Instead just avoid requesting any data when we can just return
an empty ArrayRef instead.

llvm-svn: 264234
2016-03-24 01:16:06 +00:00
Pete Cooper 07134ae364 Avoid UB deref of nullptr to reference. NFC.
Its possible for file to have no entry atom which means that there
is no atom to check for being a thumb function.  Instead just skip
the thumb check and set the entry address to 0, which matches the
current behaviour of getting a default initialised int from a map.

llvm-svn: 264233
2016-03-24 01:14:10 +00:00
Pete Cooper 07601d33f8 Use a memcpy to avoid unaligned store UB.
On a 32-bit output, we may write LC_MAIN (which contains a uint64_t) to
an unaligned address.  This changes it to use a memcpy instead which is UB safe.

llvm-svn: 264232
2016-03-24 01:05:17 +00:00
Pete Cooper a13f62f5f8 Use unaligned read to fix UB. NFC.
We were casting a potentially unaligned pointer to uint32_t and
dereferencing.  As the pointer ultimately comes from the object file,
there's no way to guarantee alignment, so use the little32_t read instead.

Also, little32_t knows about endianness, so in theory this may have broken on
big endian machines.

llvm-svn: 264231
2016-03-24 01:03:44 +00:00
Pete Cooper 3f564a52d0 Parsed alignment should be a power of 2.
The .o path always makes sure to store a power of 2 value in the
Section alignment.  However, the YAML code didn't verify this.

Added verification and updated all the tests which had a 3 but meant
to have 2^3.

llvm-svn: 264228
2016-03-24 00:36:37 +00:00
Pete Cooper 47e5399cd3 Fix more cases of UB from allocating 0 sized data. NFC.
The size of a section can be zero, even when it contains atoms, so
long as all of the atoms are also size 0.  In this case we were
allocating space for a 0 sized buffer.

Changed this to only allocate when we need the space, but also cleaned
up all the code to use MutableArrayRef instead of uint8_t* so its much much
safer as we get bounds checking on all of our section creation logic.

llvm-svn: 264204
2016-03-23 22:19:16 +00:00
Pete Cooper b565bdfb6e Use a memcpy to avoid unaligned store UB.
On a 32-bit output, we may write LC_SOURCE_VERSION (which contains a uint64_t) to
an unaligned address.  This changes it to use a memcpy instead which is UB safe.

llvm-svn: 264202
2016-03-23 22:00:09 +00:00
Rui Ueyama 961f2ff21e ELF: Split BitcodeCompiler::compile.
http://reviews.llvm.org/D18410

llvm-svn: 264193
2016-03-23 21:19:27 +00:00
Kevin Enderby 2112b2c07d Add the needed lld change for r264187 in llvm.
Sorry had this fixed in my check out but failed mention it in my
commit message for r264187.

llvm-svn: 264188
2016-03-23 20:37:23 +00:00
Davide Italiano bcd660a908 [LTO] Keep linkonce symbols when required.
Similarly to how we do with linkonce_odr symbols already, but
change their linkage to weak.

llvm-svn: 264181
2016-03-23 18:41:48 +00:00
Davide Italiano 6af61dec1c [LTO/Tests] Fix typo. Visibility -> Linkage. Pointy-hat to me.
llvm-svn: 264176
2016-03-23 18:15:06 +00:00
Davide Italiano 1de78202e8 [LTO/tests] Check optimized bitcode instead of produced shared library.
As requested by Rafael.

llvm-svn: 264175
2016-03-23 18:13:32 +00:00
Pete Cooper e82f3a099f Copy MachO struct to temporary to avoid unaligned load UB.
We were already copying this data to a temporary for endian swaps.  Now
we just always copy it, but still only do the endian swaps when needed.

llvm-svn: 264172
2016-03-23 18:00:10 +00:00
Davide Italiano f20a55fcfd [LTO] Only change linkage if we keep the symbol.
llvm-svn: 264171
2016-03-23 17:59:07 +00:00
Rafael Espindola ffcad441fd Not every x86 relocation is relative.
Without this predicate we were not producing R_386_RELATIVE relocations.

llvm-svn: 264160
2016-03-23 14:58:25 +00:00
Simon Atanasyan bea20c3502 [ELF][MIPS] Add comment with MIPS GOT relocations handling description. NFC.
llvm-svn: 264145
2016-03-23 09:28:02 +00:00
Davide Italiano da45298e58 [Tests] Remove the correct files.
llvm-svn: 264136
2016-03-23 03:20:46 +00:00
Davide Italiano 7cd8e65187 [LTO] Remove dead code.
llvm-svn: 264133
2016-03-23 02:45:53 +00:00
Sean Silva 009ea26358 Tidy up this test a bit.
llvm-svn: 264117
2016-03-22 23:19:40 +00:00
Pete Cooper 10bf15ee8e Move empty atom check to target independent code. NFC.
Turns out that checking only x86 for empty atoms to fix UBSan then
requires the same code in the other targets too.  Better to just
check this in the main run loop instead of in each target.

Should be NFC, other than fixing UBSan failures.

llvm-svn: 264116
2016-03-22 23:07:34 +00:00
Pete Cooper b9a9d7163f Avoid memcpy from nullptr. NFC.
This was caught by the UBSan bot.  When the atom has no size, we would
issue a memcpy with size0 and a nullptr for the source.

Also, this code should never have references inside an empty atom so
add an assert for that while we're here.

llvm-svn: 264115
2016-03-22 22:59:35 +00:00
Pete Cooper 8e1b9a17a6 Don't memcpy from a null source. Found by UBSan
llvm-svn: 264114
2016-03-22 22:51:03 +00:00
Davide Italiano d4c2a03c32 [LTO] Keep linkonce_odr symbols when appropriate.
Ensure we keep the symbol we need to before it reaches
the Writer (and hit an assertion), changing its linkage
from linkonce_odr to weak. For a more detailed description
of the problem, see PR19901 where a similar problem was
fixed for the gold plugin. Thanks to Rafael for providing
a testcase.

llvm-svn: 264111
2016-03-22 22:31:34 +00:00
Pete Cooper 85698f2568 Fix operator= on OwningAtomPtr to call destructor when needed.
If the LHS of 'a = b' already had an atom in it then we wouldn't
call the destructor.  This happens when we use something like
std::remove_if which is done in the CompactUnwindPass.  Should fix
the leaks on the mach-o/unwind-info-simple-x86_64.yaml test case.

Lang and I are going to take a look at removing OwningAtomPtr in
favour of a std::unique_ptr but just trying to get the bots green
so we have a good baseline first.

llvm-svn: 264097
2016-03-22 21:08:39 +00:00
Sean Silva e32368fc6e Mark SymbolBody::getSymbol as `const`.
llvm-svn: 264094
2016-03-22 21:04:03 +00:00
Rui Ueyama 7a32d9a653 We do not use libLTO, so fix the error.
llvm-svn: 264093
2016-03-22 20:58:15 +00:00
Rui Ueyama 259924869b ELF: Create LTO.{cpp,h} and move LTO-related code to that file.
The code for LTO has been growing, so now is probably a good time to
move it to its own file. SymbolTable.cpp is for symbol table, and
because compiling bitcode files are semantically not a part of
symbol table, this is I think a good thing to do.

http://reviews.llvm.org/D18370

llvm-svn: 264091
2016-03-22 20:52:10 +00:00
Sean Silva 3baa23b50c [lto] Provide a file name for the combined LTO object.
Otherwise, we get diagnostics like:

    undefined symbol: foo in

Which isn't particularly useful.

llvm-svn: 264089
2016-03-22 20:25:32 +00:00
Pete Cooper 1edff82f82 More MSVC bot appeasement. Explicitly define rvalue methods on SortKey.
OwningAtomPtr does not have OwningAtomPtr(OwningAtomPtr&) or the equivalent
operator= as we only want to use rvalue references in it.

SortKey didn't like this on MSVC as it was synthesizing SortKey(SortKey&) and
trying to use the OwningAtomPtr(OwningAtomPtr&) method which was private an
unimplemented.

Now we explicitly have the methods on SortKey so hopefully the bot will be
happier.

llvm-svn: 264077
2016-03-22 18:09:55 +00:00
Pete Cooper c2d21cb763 Appease MSVC bots by changing visibility of AtomVector.
The AtomVector class is an internal detail of File so I moved it
to be protected in r264067.  However, the MSVC bots don't like the
global declarations of type File::AtomVector in File.cpp so it needs
to go back to being public for now.

llvm-svn: 264070
2016-03-22 17:39:44 +00:00
Pete Cooper 8ad55fb2d0 Use owning pointers instead of raw pointers for Atom's to fix leaks.
This is a re-commit of r264022 with a fix for MSVC.  The issue there was
that the code was running DefinedAtom::~Atom() for some value and instead
needed to cast to Atom before running ~Atom.  Original commit message follows.

Currently each File contains an BumpPtrAllocator in which Atom's are
allocated.  Some Atom's contain data structures like std::vector which
leak as we don't run ~Atom when they are BumpPtrAllocate'd.

Now each File actually owns its Atom's using an OwningAtomPtr.  This
is analygous to std::unique_ptr and may be replaced by it if possible.

An Atom can therefore only be owned by a single File, so the Resolver now
moves them from one File to another.  The MachOLinkingContext owns the File's
and so clears all the Atom's in ~MachOLinkingContext, then delete's all the
File's.  This makes sure all Atom's have been destructed before any of the
BumpPtrAllocator's in which they run have gone away.

Should hopefully fix the remaining leaks.  Will keep an eye on the bots to
make sure.

llvm-svn: 264067
2016-03-22 17:15:50 +00:00
Rafael Espindola 26d239c293 Skip some relocations in scanRelocs.
When a tls access is optimized, a group of relocations is converted at a
time.

We were already skipping relocations that were optimized out in
relocate, but not in scanRelocs.

This is a small optimization. I got here while working on a patch that
will always keep scanRelocs and relocate in sync.

llvm-svn: 264048
2016-03-22 13:24:29 +00:00
George Rimar 9f8f4e3944 [ELF] - Process R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX relocations.
R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX relocations were added in latest ABI:
https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-r249.pdf

They should be generated instead of R_X86_64_GOTPCREL for cases 
when relaxation is possible. Currently this patch just process them in the 
same way like R_X86_64_GOTPCREL. That should work for now
and we can implement relaxations later.

There is no testcases provided as I think there is no way to generate
such relocations using llvm-mc atm.

Differential revision: http://reviews.llvm.org/D18301

llvm-svn: 264043
2016-03-22 12:15:26 +00:00
Simon Atanasyan f3ec3be178 [ELF][MIPS] Delete GotSection::addMipsLocalEntry method
Now local symbols have SymbolBody so we can handle all kind of symbols
in the GotSection::addEntry method. The patch moves the code from
addMipsLocalEntry to addEntry. NFC.

Differential Revision: http://reviews.llvm.org/D18302

llvm-svn: 264032
2016-03-22 08:36:48 +00:00
Mehdi Amini d595b08998 DenseMap resize() is now named reserved(), adapt the call sites
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 264028
2016-03-22 07:31:35 +00:00
Pete Cooper 3e4d732dd0 Revert "Use owning pointers instead of raw pointers for Atom's to fix leaks."
This reverts commit r264022.

This breaks the Window's bots which don't like that i'm calling ~Atom when
the this pointer is a sublcass of Atom.

Reverting for now until I try find a better fix.  I tried using std::unique_ptr with
a custom deleter as a quick fix, but it didn't work well in the YAML parser.

llvm-svn: 264023
2016-03-22 04:00:41 +00:00
Pete Cooper 572a87e2aa Use owning pointers instead of raw pointers for Atom's to fix leaks.
Currently each File contains an BumpPtrAllocator in which Atom's are
allocated.  Some Atom's contain data structures like std::vector which
leak as we don't run ~Atom when they are BumpPtrAllocate'd.

Now each File actually owns its Atom's using an OwningAtomPtr.  This
is analygous to std::unique_ptr and may be replaced by it if possible.

An Atom can therefore only be owned by a single File, so the Resolver now
moves them from one File to another.  The MachOLinkingContext owns the File's
and so clears all the Atom's in ~MachOLinkingContext, then delete's all the
File's.  This makes sure all Atom's have been destructed before any of the
BumpPtrAllocator's in which they run have gone away.

Should hopefully fix the remaining leaks.  Will keep an eye on the bots to
make sure.

llvm-svn: 264022
2016-03-22 03:44:32 +00:00
Pete Cooper c0c464cac1 Move ownership of Pass File's to the MachoLinkingContext.
In trying to fix the leaks in the MachO lld codebase, we need to have
a better model for file and atom ownership.  Having the context own
everything seems like the simplest model, so change all the passes to
allocate File's on the context instead of owning files as a member.

llvm-svn: 264004
2016-03-21 23:17:47 +00:00
Davide Italiano 901de03fe2 [ELF] Simplify code a bit. No functional change.
llvm-svn: 263999
2016-03-21 22:44:24 +00:00
Eugene Zelenko 2bf5ed5670 Fix Clang-tidy modernize-deprecated-headers warnings; other minor fixes.
Differential revision: http://reviews.llvm.org/D18232

llvm-svn: 263968
2016-03-21 18:32:35 +00:00
Rafael Espindola 0d2ad420df Revert "[ELF] SHF_MERGE section with 0 entsize is not fatal"
This reverts commit r263664.

The reason we were getting broken files was lld -r, and that has been
fixed.

llvm-svn: 263944
2016-03-21 14:57:20 +00:00
George Rimar f9c2f81812 Eliminated trailing whitespaces from test. NFC.
llvm-svn: 263933
2016-03-21 11:23:53 +00:00
David Majnemer 08af4e56e1 [COFF] Don't call memcpy with a NULL argument
Some declarations of memcpy (like glibc's for example) are attributed
with notnull which makes it UB for NULL to get passed in, even if the
memcpy count is zero.

To account for this, guard the memcpy with an appropriate precondition.

This should fix the last UBSan bug, exposed by the test suite, in the
COFF linker.

llvm-svn: 263919
2016-03-20 23:10:12 +00:00
David Majnemer 2aa29ee7c1 [COFF] Remove undefined behavior from ObjectFile::createWeakExternal
LLD type-punned an integral type and a pointer type using a pointer
field.  This is problematic because the pointer type has alignment
greater than some of the integral values.

This would be less problematic if a union was used but it turns out the
integral values are only present for a short, transient, amount of time.

Let's remove this undefined behavior by skipping the punning altogether
by storing the state in a separate memory location: a vector which
informs us which symbols to process for weak externs.

llvm-svn: 263918
2016-03-20 22:56:31 +00:00
Simon Atanasyan 6dd7061d15 [ELF][MIPS] Add case demonstrates creation redundant MIPS GOT entries for non-local symbols. NFC.
llvm-svn: 263897
2016-03-20 11:53:39 +00:00
George Rimar 4f1dae295f [ELF] - ignore the -rpath-link option
Just ignore the -rpath-link command line
option in the same way like gold do.

Behavior of lld/gold differs from gnu ld here.
GNU ld tries to resolve undefined symbols in all
shared object files at link time.

Differential revision: http://reviews.llvm.org/D18269

llvm-svn: 263876
2016-03-19 11:15:54 +00:00
Davide Italiano 67e03a1ce6 [LTO] Ignore -plugin/-plugin-opt options.
This is required to get 'clang -flto' to work transparently
with lld. Please refer to the short comment in the code
for a more detailed explanation.

llvm-svn: 263862
2016-03-19 00:40:09 +00:00
Davide Italiano ac8737ac60 [ELF] Refactor run of LTO passes into an helper. NFC.
Requested by: Rui Ueyama.

llvm-svn: 263851
2016-03-18 23:22:40 +00:00
David Majnemer 18558b6d9e [COFF] Correct tests with bogus alignment
llvm-svn: 263838
2016-03-18 21:47:12 +00:00
Rafael Espindola 69082f051d Revert "bar"
This reverts commit r263799.
It was a mistake. Sorry about that.

llvm-svn: 263801
2016-03-18 18:11:26 +00:00
Rafael Espindola 2f055f9881 The ELF lld doesn't depend on LTO.
Tested with a shared build.

llvm-svn: 263800
2016-03-18 18:09:35 +00:00
Rafael Espindola c2cfd9fa34 bar
llvm-svn: 263799
2016-03-18 18:09:32 +00:00
Jan Vesely 3ac9501c3f [ELF] Link with Analysis, IPO, and LTO
Fixes SHARED_LIB build broken by r263761

llvm-svn: 263796
2016-03-18 17:12:31 +00:00
George Rimar 0baa1d3dbe [ELF] - Set the sh_entsize for mergable sections
Previously sh_entsize field was not set for MergeOutputSection.
Patch fixes that.

That should resolve the https://llvm.org/bugs/show_bug.cgi?id=26975

Differential revision: http://reviews.llvm.org/D18248

llvm-svn: 263780
2016-03-18 09:28:39 +00:00