Commit Graph

3627 Commits

Author SHA1 Message Date
George Rimar a0b1e2c3d8 [ELF] - Don't use uintX_t in DynamicReloc<ELFT>. NFC.
That allows to detemplate getGotPltOffset/getGotPltVA
methods of SymbolBody and also will help to detemplate
DynamicReloc itself later.

llvm-svn: 297946
2017-03-16 11:42:16 +00:00
George Rimar 7702bc237e [ELF] - Removed unused templates #4. NFC.
llvm-svn: 297944
2017-03-16 11:20:02 +00:00
George Rimar 69268a8ab3 [ELF] - Detemplate SymbolBody::getOutputSection(). NFC.
llvm-svn: 297943
2017-03-16 11:06:13 +00:00
George Rimar a92dc2bbd3 [ELF] - Removed unused template #3. NFC.
llvm-svn: 297942
2017-03-16 10:55:45 +00:00
George Rimar d3ea775894 [ELF] - Removed unused template #2. NFC.
llvm-svn: 297941
2017-03-16 10:48:40 +00:00
George Rimar 7b8270424e [ELF] Detemplate ThunkSection and Thunk classes. NFC.
llvm-svn: 297939
2017-03-16 10:40:50 +00:00
George Rimar 49a47f2a6d [ELF] - Removed unused template #1. NFC
llvm-svn: 297938
2017-03-16 10:29:44 +00:00
George Rimar f98c5c126c [ELF] - Detemplate OutputSection::assignOffsets. NFC.
llvm-svn: 297937
2017-03-16 10:24:54 +00:00
George Rimar 47bed8c9f7 Revert r297813 "[ELF] - Make Bss and BssRelRo sections to be synthetic (#3)."
I suppose it is the reason of BB fail:
http://lab.llvm.org:8011/builders/clang-cmake-aarch64-lld/builds/921

https://bugs.llvm.org/show_bug.cgi?id=32167

llvm-svn: 297933
2017-03-16 08:44:53 +00:00
George Rimar 20055d4cd2 [ELF] - Linkerscript: make Dot public and remove getDot(). NFC.
Suggested by Rui Ueyama,
also groups member variables in a single place, while I am here.

llvm-svn: 297850
2017-03-15 16:07:02 +00:00
George Rimar 503206c567 [ELF] - Move LinkerScript::discard to LinkerScriptBase. NFC.
Became possible after r297844

llvm-svn: 297848
2017-03-15 15:42:44 +00:00
George Rimar 9782ca5b68 [ELF] - Split struct In<ELFT> to have templated and non-templated sections areas.
Patch splits In<ELFT> into 2 classes: one for non-templated sections, 
second contains ELFT templated ones.
That allows to code that was detemplated to access non-templated sections freely,
and should open road for futher detemplation proccess.

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

llvm-svn: 297844
2017-03-15 15:29:29 +00:00
Rafael Espindola 4820060dc6 Use uint32_t for alignment.
llvm-svn: 297831
2017-03-15 12:43:54 +00:00
Rafael Espindola a156555b5e simplify. NFC
llvm-svn: 297829
2017-03-15 12:31:54 +00:00
George Rimar 42886c4b47 [ELF] - Detemplate MipsRldMapSection. NFC.
After introducing Config->is64Bit() and 
recent changes in LinkerScriptBase, some
sections can be detemplated trivially. This 
is one of such cases.

llvm-svn: 297825
2017-03-15 12:02:31 +00:00
George Rimar 49648004ed [ELF] - Detemplate StringTableSection.
StringTableSection was <ELFT> templated previously,

It disallow to de-template code that uses it,
for example LinkerScript<ELFT>::discard uses it as:
if (S == In<ELFT>::ShStrTab)
  error("discarding .shstrtab section is not allowed");

It seems we can try to detemplate some of synthetic sections
and somehow make them available for non-templated calls.
(move out of In<ELFT> struct probably).

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

llvm-svn: 297815
2017-03-15 09:32:36 +00:00
George Rimar 22e3e7d165 [ELF] - Make Bss and BssRelRo sections to be synthetic (#3).
That removes CopyRelSection class completely, making
Bss/BssRelRo to be just regular synthetics.

This is splitted from D30541 and polished.
Difference from D30541 that all logic of SharedSymbol
converting to DefinedRegular was removed for now and
probably will be posted as separate patch.

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

llvm-svn: 297814
2017-03-15 09:21:29 +00:00
George Rimar 10f74fc10b [ELF] - Detemplate GotPltSection and IgotPltSection sections.
Patch introduces Config->is64Bit() and with help of that detemplates
GotPltSection and IgotPltSection sections

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

llvm-svn: 297813
2017-03-15 09:12:56 +00:00
Petr Hosek 02ad516b2e Support ABSOLUTE on the right hand side in linker scripts
This also requires postponing the assignment the assignment of
symbols defined in input linker scripts since those can refer to
output sections and in case we don't have a SECTIONS command, we
need to wait until all output sections have been created and
assigned addresses.

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

llvm-svn: 297802
2017-03-15 03:33:23 +00:00
Rafael Espindola d3c22f08b3 Don't create copy relocs or dummy plt's if given -z notext.
Being passed -z notext is a pretty strong indication that the user is
OK with text relocations. This is not the same behavior as bfd, but
bfd defaults to -z notext, so it has to try to avoid text relocations
and use them as a last resort.

llvm-svn: 297789
2017-03-14 22:50:07 +00:00
George Rimar a2a1ef1abc [ELF] - Move members of LinkerScript to LinkerScriptBase. NFC.
That moves all members that s possible to move for now (all which
does not depend on ELFT templating).

After that change LinkerScript contains only 8 methods in total,
and I believe it is possible to move them all after tweaking other
parts of linker. And we will be able to have single class for 
linkerscript at the end.

llvm-svn: 297735
2017-03-14 12:03:34 +00:00
George Rimar 23e6a02450 [ELF] - Remove unnecessary template #6. NFC.
llvm-svn: 297734
2017-03-14 11:31:28 +00:00
George Rimar f7f0d08827 [ELF] - Remove unnecessary template #5. NFC.
llvm-svn: 297732
2017-03-14 11:23:33 +00:00
George Rimar d83ce1b49d [ELF] - Devirtualize LinkerScriptBase::getOutputSectionSize. NFC.
It does not use ELFT templates so can be non-virtual.

llvm-svn: 297727
2017-03-14 10:24:47 +00:00
George Rimar 851dc1e84d [ELF] - Devirtualize LinkerScriptBase::getOutputSection
It does not use ELFT templates so can be non-virtual.

llvm-svn: 297725
2017-03-14 10:15:53 +00:00
George Rimar d0bee506a0 [ELF] - Simplify LinkerScriptBase::getDot(). NFC.
That makes it not dependent on virtual call,
keeping logic the same.

llvm-svn: 297723
2017-03-14 10:05:43 +00:00
George Rimar 0c1c8085bc [ELF] - Move ThreadBssOffset and Dot to LinkerScriptBase. NFC.
One more step to combine LinkerScript and LinkerScriptBase.

llvm-svn: 297722
2017-03-14 10:00:19 +00:00
George Rimar e21c3af7e9 [ELF] - Remove unnecessary template #4. NFC.
OutputSectionFactory has no ELFT templates anymore.

llvm-svn: 297720
2017-03-14 09:30:25 +00:00
George Rimar 09268b7b1e [ELF] - Remove unnecessary template #3. NFC.
llvm-svn: 297719
2017-03-14 09:25:03 +00:00
George Rimar f08b592390 [ELF] - Remove unnecessary template #2. NFC.
llvm-svn: 297718
2017-03-14 09:19:34 +00:00
George Rimar 788fe38f99 [ELF] - Remove unnecessary template. NFC.
llvm-svn: 297717
2017-03-14 09:14:28 +00:00
George Rimar 2d2621090d [ELF] - Step to combine LinkerScript and LinkerScriptBase
We can move all not templated functionality to LinkerScriptBase.
Patch do that for hasPhdrsCommands() and shows how it helps to detemplate
things in other places.

Probably we should be able to merge these 2 classes into single one after such steps.
Even if not, it still looks as reasonable cleanup for me.

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

llvm-svn: 297714
2017-03-14 09:03:53 +00:00
Eugene Leviant 5784e96f5c [ELF] Fix LMA offset calculation
Differential revision: https://reviews.llvm.org/D30832

llvm-svn: 297713
2017-03-14 08:57:09 +00:00
Eugene Leviant 30c1b436ad [ELF] Fix crash when .eh_frame(_hdr) is discarded
lld crashes when .eh_frame or .eh_frame_hdr section is discarded
in linker script and there is no PHDRS directive.

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

llvm-svn: 297712
2017-03-14 08:49:09 +00:00
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 034a33a942 Remove a useless temporary variable.
llvm-svn: 297687
2017-03-13 23:26:37 +00:00
Rui Ueyama f327999a45 Simplify and add comments. NFC.
llvm-svn: 297686
2017-03-13 23:23:40 +00:00
Rui Ueyama 8ebf6588a5 Add a comment to say that -build-id=sha1 is a synonym for tree.
llvm-svn: 297677
2017-03-13 21:48:38 +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
George Rimar 78aa270041 [ELF] - Remove unnecessary template. NFC.
llvm-svn: 297622
2017-03-13 14:40:58 +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
Rafael Espindola 4595df94bb Don't pass Dot to every callback.
It is available from ScriptBase.

llvm-svn: 297472
2017-03-10 16:04:26 +00:00
Rafael Espindola 9bd4566dac Use SectionBase for linker script expressions.
This is a small step for fixing pr32031, which needs expressions that
point to input sections.

llvm-svn: 297431
2017-03-10 00:47:33 +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
Rafael Espindola 5616adf655 Remove DefinedSynthetic.
With this we have a single section hierarchy. It is a bit less code,
but the main advantage will be in a future patch being able to handle

foo = symbol_in_obj;

in a linker script. Currently that fails since we try to find the
output section of symbol_in_obj.  With this we should be able to just
return an InputSection from the expression.

llvm-svn: 297313
2017-03-08 22:36:28 +00:00
Rafael Espindola fcd208fdb3 Use uint32_t for alignment in more places, NFC.
llvm-svn: 297305
2017-03-08 19:35:29 +00:00
Rui Ueyama 007c002cb6 Revert r297008: [ELF] - Make Bss and BssRelRo sections to be synthetic (#2).
This reverts commit r297008 because it's reported that that
change broke AArch64 bots.

llvm-svn: 297297
2017-03-08 17:24:24 +00:00
Rafael Espindola 5e434b3f11 Remove unnecessary template.
llvm-svn: 297293
2017-03-08 16:08:36 +00:00
Rafael Espindola e1294091d3 Remove unnecessary template. NFC.
llvm-svn: 297292
2017-03-08 16:03:41 +00:00
Rafael Espindola 35ae65ee2b Rename one of the getOffset methods.
It is sufficiently different in that it returns an offset in the input
file, not the output section.

llvm-svn: 297290
2017-03-08 15:57:17 +00:00
Rafael Espindola 76b6bd355d Remove unnecessary template. NFC.
llvm-svn: 297287
2017-03-08 15:44:30 +00:00
Rafael Espindola c86b2cddc8 Convert a few more uses of uintX_t to uint64_t.
llvm-svn: 297286
2017-03-08 15:34:04 +00:00
Rafael Espindola 9371bab55a Convert a few uses of uintX_t to uint64_t.
llvm-svn: 297282
2017-03-08 15:21:32 +00:00
Rafael Espindola bdd2e3e36c Remove redundant member of InputSectionBase. NFC.
With this InputSectionBase is now 144 bytes.

llvm-svn: 297278
2017-03-08 14:12:52 +00:00
Peter Smith 1ec42d9a76 Finalize content of synthetic sections prior to Thunk Creation
This change moves the calls to finalizeContent() for each synthetic section
before createThunks(). This will allow us to assign addresses prior to
calling createThunks(). As addition of thunks may add to the static
symbol table and may affect the size of the mips got section we introduce a
couple of additional member functions to update these values.
    
Differential revision: https://reviews.llvm.org/D29983

llvm-svn: 297277
2017-03-08 14:06:24 +00:00
Rafael Espindola 8bb4087f88 Use 32 bits for alignment.
This is consistent with what we do for input sections.

llvm-svn: 297152
2017-03-07 15:51:09 +00:00
Rafael Espindola c8de15bdda Rename remaining uses of Addralign to Alignment.
llvm-svn: 297146
2017-03-07 15:11:21 +00:00
Rafael Espindola 3770763cda Rename Addralign to Alignment.
It now matches the name used in InputSectionBase.

llvm-svn: 297144
2017-03-07 14:55:52 +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
Rui Ueyama af6198d96d Remove Config->Rela and define Config->isRela() instead.
llvm-svn: 297108
2017-03-07 00:43:53 +00:00
Rui Ueyama df8eb17d21 Remove Config->Mips64EL and define Config->isMips64EL() instead.
llvm-svn: 297107
2017-03-07 00:43:33 +00:00
Rafael Espindola 2a80e1180e Make Discarded a InputSectionBase.
NFC, just a bit simpler.

llvm-svn: 297087
2017-03-06 22:36:19 +00:00
Rafael Espindola 5c02b741eb Detemplate EhInputSection. NFC.
llvm-svn: 297077
2017-03-06 21:17:18 +00:00
Evgeniy Stepanov 5ac5542544 Fix -Werror build error.
tools/lld/ELF/Symbols.cpp:215:13: error: unused variable 'S'
[-Werror,-Wunused-variable]
  if (auto *S = dyn_cast<SharedSymbol>(this)

llvm-svn: 297063
2017-03-06 20:27:38 +00:00
Rafael Espindola 6119b865ec Detemplate merge (input and synthetic) sections. NFC.
llvm-svn: 297061
2017-03-06 20:23:56 +00:00
Rafael Espindola 77408b067c Convert two uses of uintX_t. NFC.
llvm-svn: 297059
2017-03-06 20:04:47 +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
Rui Ueyama 967fc1cef4 Fix -Wc++11-narrowing.
llvm-svn: 297012
2017-03-06 15:16:18 +00:00
George Rimar c215a2ac40 [ELF] - Make Bss and BssRelRo sections to be synthetic (#2).
In compare with D30458, this makes Bss/BssRelRo to be pure
synthetic sections.

That removes CopyRelSection class completely, making
Bss/BssRelRo to be just regular synthetics.

SharedSymbols involved in creating copy relocations are
converted to DefinedRegular, what also simplifies things.

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

llvm-svn: 297008
2017-03-06 14:37:45 +00:00
Rafael Espindola 066d3d2141 Pass a callback to resolveReloc. NFC.
This is consistent with rest of the file and opens the way for a
relocation keeping multiple sections alive.

llvm-svn: 296788
2017-03-02 19:29:28 +00:00
Rui Ueyama 8139eab3c3 Simplify. NFC.
llvm-svn: 296773
2017-03-02 17:55:29 +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 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 ac2d815a2a Split GdbIndexBuilder class into non-member functions.
That class had three member functions, and all of them are just reader
methods that did not depend on class members, so they can be just non-
member functions.

Probably we should reorganize the functions themselves because their
return types doesn't make much sense to me, but for now I just moved
these functions out of the class.

llvm-svn: 296700
2017-03-01 22:54:50 +00:00
Rui Ueyama aab18c0cfc Remove useless variables and declarations.
llvm-svn: 296695
2017-03-01 22:24:46 +00:00
Rui Ueyama 97d7a74b35 Replace `auto` with its real type.
llvm-svn: 296694
2017-03-01 22:24:25 +00:00
Rui Ueyama e5e320de06 Style fix.
llvm-svn: 296689
2017-03-01 22:03:17 +00:00
Rui Ueyama a4d9a22ba2 Reduce nesting. NFC.
llvm-svn: 296688
2017-03-01 22:02:57 +00:00
Rui Ueyama e02cc60c72 Do not inherit LoadedObjectInfo.
GdbIndexBuilder class inherited LoadedObjectInfo, but that's not necessary.

llvm-svn: 296687
2017-03-01 22:02:37 +00:00
Rui Ueyama 475b1ddfe2 Inline a function that is too short to be an independent function.
llvm-svn: 296686
2017-03-01 22:02:17 +00:00
Rui Ueyama 962a83fbe9 Unbreak Windows bots.
llvm-svn: 296680
2017-03-01 21:28:41 +00:00
Rui Ueyama d0e07b919c Don't implement the gdb hash table as a generic in-memory hash table.
Looks like .gdb.index and its support classes do things that they don't
have to or shouldn't do do. This patch addresses one of these issues.

GdbHashTab class is a hash table class. Just like other in-memory hash
tables, that incrementally updates its internal data and resizes buckets
as new elements are added so that key lookup is always fast.
But that is completely not necessary.

Unlike debuggers, we only produce hash tables for .gdb.index and
never read them. So incrementally updating a hash table in memory is
just a waste of resource and complicates the code. What we should
do is to accumulate symbols and then create the final hash table
at once.

llvm-svn: 296678
2017-03-01 21:08:21 +00:00
Rui Ueyama 7986b4544b Add a reference to a Ulrich Drepper's paper.
llvm-svn: 296650
2017-03-01 18:09:09 +00:00
Rafael Espindola bedccb5e78 Simplify. NFC.
llvm-svn: 296620
2017-03-01 14:21:31 +00:00
Rafael Espindola bd12e2a0ce Simplify. NFC.
llvm-svn: 296619
2017-03-01 14:12:21 +00:00
George Rimar efc31dd9bb [ELF] - Reset output section size when assigning offsets.
In many places we reset Size to 0 before calling assignOffsets()
manually. Sometimes we don't do that. 
It looks we can just always do that inside.

Previous code had:
template <class ELFT> void OutputSection::assignOffsets() {
  uint64_t Off = Size;

And tests feels fine with Off = 0. 
I think Off = Size make no sence.

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

llvm-svn: 296609
2017-03-01 11:10:53 +00:00
George Rimar c84ca256d3 [ELF] - Fixed crash in GnuHashTableSection.
r296570 intorduced crash revealed by 
gnu-hash-table.s which crashed for me.

Reason was use of uninitialized NBuckets variable.

llvm-svn: 296597
2017-03-01 10:12:49 +00:00
Rui Ueyama 6e50fd53d4 Instead of passing an empty ArrayRef as contents, pass actual contents. NFC.
llvm-svn: 296586
2017-03-01 07:39:06 +00:00
Sean Silva d4e606273b Improve comment.
Thanks to Rafael for helping to dig down into what we're really trying
to communicate here.

llvm-svn: 296580
2017-03-01 04:44:04 +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
Rui Ueyama e13373b5b9 Partially rewrite .gnu.hash synthetic section and add comments.
This implementation is probably slightly inefficient than before,
but that should be negligible because this is not a performance-
critical pass.

llvm-svn: 296570
2017-03-01 02:51:42 +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 e412063397 Add comments about .hash and .gnu.hash.
llvm-svn: 296529
2017-02-28 22:05:13 +00:00
Rui Ueyama 73d29ab3a7 Minor refactoring. NFC.
llvm-svn: 296511
2017-02-28 19:43:54 +00:00
Rui Ueyama 175e81cf3a Use make<> instead of new (BAlloc). NFC.
We converted them before, but there were a few remaining occurrences.

llvm-svn: 296510
2017-02-28 19:36:30 +00:00
Rui Ueyama 80474a26b9 De-template DefinedRegular.
Differential Revision: https://reviews.llvm.org/D30348

llvm-svn: 296508
2017-02-28 19:29:55 +00:00
Rui Ueyama 1b00318371 Make it obvious that st_value for common symbols has a different meaning.
llvm-svn: 296507
2017-02-28 19:22:09 +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 9f57a4f14a [ELF] - Do not set flags for CopyRelSection. NFC.
It does not make sense. Them added either to
Out::BssRelRo or Out::Bss, which are always RW.

llvm-svn: 296475
2017-02-28 14:22:15 +00:00
George Rimar 6159f1290e [ELF] - Remove ElfSym::EhdrStart member. NFC.
We do not use it later, so don't have to store.

llvm-svn: 296466
2017-02-28 10:34:56 +00:00
Sean Silva 6ab3926ee6 Comment on the typical/simple case of VA calculation
There are many special cases and a layer of abstraction or two in the
way, but the VA calculation in the typical case is actually very simple
and probably makes perfect sense even to somebody new to linkers.

Also, this line brings together many components and is a good place to
start understanding the linker (or improve one's existing
understanding).

llvm-svn: 296451
2017-02-28 09:01:58 +00:00
Sean Silva a9ba450c52 Add a comment.
Naively it seemed at first like getVA had the responsibility of adding
the addend, and getSymVA had the responsibility of getting the symbol
VA.
So it was not obvious to me at first why getVA passes Addend to
getSymVA. In fact, it passes it as a mutable reference.

It turns out that it only matters for SHF_MERGE sections, and in
particular only for STT_SECTION symbols that are used as a hack for
reducing the number of local symbols (e.g. to avoid a local symbol for
each string in the string table).

llvm-svn: 296448
2017-02-28 08:32:56 +00:00
Rui Ueyama 6b776ad985 Refactor {Gnu,}HashTableSection classes.
In other places in LLD, we use write32<E> instead of Elf_Word.
This patch uses the same technique in the hash table classes.
The hash table classes needs improving as they have almost no
comments. We at least need to describe the hash table structure
and why we have to support two different on-disk hash tables
for the same purpose. I'll do that later.

llvm-svn: 296439
2017-02-28 05:53:47 +00:00
Rui Ueyama ab0a85ef4d Remove unused typedefs.
llvm-svn: 296437
2017-02-28 05:25:37 +00:00
Rui Ueyama c3726f8263 Remove useless assignments.
These assignments seem meaningful, but actually all tests pass without them.

llvm-svn: 296436
2017-02-28 04:41:20 +00:00
Rui Ueyama b8dcdb5900 Merge SymbolTableSection::add{Global,Local} into one function.
Previously, these two functions put their symbols in different queues.
Now that the queues have been merged. So there's no point to keep two
separate functions.

llvm-svn: 296435
2017-02-28 04:20:16 +00:00
Rui Ueyama 406331ebfd Return early. NFC.
llvm-svn: 296434
2017-02-28 04:11:01 +00:00
Rui Ueyama 968db48cee Move SymbolTableSection::getOutputSection to SymbolBody::getOutputSection.
That function doesn't use any member of SymbolTableSection, so I
couldn't see a reason to make it a member of that class. The function
takes a SymbolBody, so it is more natural to make it a member of
SymbolBody.

llvm-svn: 296433
2017-02-28 04:02:42 +00:00
Rui Ueyama 6e96734dd1 Simplify SymbolTableSection<ELFT>::finalizeContents().
Previously, there were three conditions: .symtab, .dynsym or we are
producing a relocatable output. Turned out that the third condition is
the same as the first one. This patch removes that third condition and
simplify code.

llvm-svn: 296431
2017-02-28 03:29:12 +00:00
Sean Silva 1d96185f57 Add Triple::thumb to getBitcodeMachineKind
We really need to find a way to get this info from a single point of
truth in the LLVM backend, but it seems that the EM_* constants are
buried deep inside the constructors of the MCAsmBackend's.

For now, just fill in entries as we run into cases. AFAIK these mappings
are largely immutable, so we get a 75% discount on the technical debt
(code is duplicated, but little chance of divergence).

llvm-svn: 296429
2017-02-28 03:00:48 +00:00
Rui Ueyama 1f03253767 Improve SymbolTableSection synthetic section.
The previous code was a bit hard to understand because it unnecessarily
distinguished local and non-local symbols. It had NumLocals member
variable, but that variable didn't have a number of local symbols but
had some value that I cannot describe easily.

This patch rewrites SynbolTableSection::finalizeContents and
SymbolTableSection::writeTo to make it easy to understand. NumLocals
member variable has been removed, and writeGlobalSymbols and
writeLocalSymbols have been merged into one function.

There's still a piece of code that I think unnecessary. I'm not removing
that code in this patch, but will do in a follow-up patch.

llvm-svn: 296423
2017-02-28 01:56:36 +00:00
Rui Ueyama 3bc39016cd Refactor write{Global,Local}Symbols.
This part of code is hard to understand because NumLocals does not
actually mean the number of local symbols but something else (!).
We need to rewrite. But before that we need to clean it up.

llvm-svn: 296400
2017-02-27 22:39:50 +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
Rui Ueyama 3ead2e73ee De-template ResolvedReloc as it doesn't actually depends on ELFT.
Pointed out by Bob Haarman.

llvm-svn: 296377
2017-02-27 20:32:34 +00:00
Rui Ueyama aa5c52744b Simplify sortMipsSymbols function a bit.
llvm-svn: 296319
2017-02-27 03:31:38 +00:00
Rui Ueyama bb07d10601 Add comments to SymbolTableSection.
llvm-svn: 296318
2017-02-27 03:31:19 +00:00
Rui Ueyama 0c001d9407 Remove dead function.
llvm-svn: 296316
2017-02-27 03:13:36 +00:00
Rui Ueyama 0fb284aa35 Remove unnecessary "this->".
llvm-svn: 296315
2017-02-27 03:10:06 +00:00
Rui Ueyama 945055aabf Rename SyntheticSection::finalize -> finalizeContents.
In LLD source code, too many functions are called "finalize", although
what they do are different. This patch gives it a better name.

llvm-svn: 296314
2017-02-27 03:07:41 +00:00
Rui Ueyama 9320cb0719 De-template SyntheticSection.
This class didn't use ELFT.

llvm-svn: 296313
2017-02-27 02:56:02 +00:00
Rui Ueyama 3255a52200 De-template InterpSection because it doesn't use ELFT.
llvm-svn: 296311
2017-02-27 02:32:49 +00:00
Rui Ueyama f8d47fda43 Update comments. Now thunks are synthetic sections.
llvm-svn: 296310
2017-02-27 02:32:29 +00:00
Rui Ueyama 536a26706f Move SymbolTable<ELFT>::Sections out of the class.
The list of all input sections was defined in SymbolTable class for a
historical reason. The list itself is not a template. However, because
SymbolTable class is a template, we needed to pass around ELFT to access
the list. This patch moves the list out of the class so that it doesn't
need ELFT.

llvm-svn: 296309
2017-02-27 02:32:08 +00:00
Rui Ueyama 02a036f2e6 De-template OutputSectionFactory.
Since OutputSection is no longer a template, it doesn't make much
sense to tempalte its factory class.

llvm-svn: 296308
2017-02-27 02:31:48 +00:00
Rui Ueyama 9d1bacb1b4 Remove useless template so that Out<ELFT> becomes just Out.
llvm-svn: 296307
2017-02-27 02:31:26 +00:00
Rui Ueyama 4076fa1e21 De-template SharedSymbol.
Differential Revision: https://reviews.llvm.org/D30351

llvm-svn: 296303
2017-02-26 23:35:34 +00:00
Rui Ueyama 28eada105f Factor out more code. NFC.
llvm-svn: 296232
2017-02-25 02:43:01 +00:00
Rui Ueyama 47926abfa6 Remove a default value that is always overwritten.
llvm-svn: 296230
2017-02-25 02:27:58 +00:00
Rui Ueyama 10571cd306 Make function names consistent.
Now a function to parse -foo-bar is always named getFooBar.

llvm-svn: 296229
2017-02-25 02:23:28 +00:00
Rui Ueyama 524d44c6fa Do not handle -strip in a special way. NFC.
llvm-svn: 296227
2017-02-25 02:12:37 +00:00
Rui Ueyama dfa3ad1313 Factor out code to parse -hash-style.
llvm-svn: 296226
2017-02-25 02:07:27 +00:00
Rui Ueyama a37ace8d89 Rename OMagic -> Omagic for consistency.
llvm-svn: 296225
2017-02-25 01:52:03 +00:00
Rui Ueyama 7edde294aa Factor out code to parse -L and -rpath.
llvm-svn: 296224
2017-02-25 01:51:44 +00:00
Rui Ueyama a4a643c6b9 Sort options. NFC.
llvm-svn: 296223
2017-02-25 01:51:25 +00:00
Rui Ueyama 9cc84384ad Use uint64_t instead of uintX_t where the larger type just works.
llvm-svn: 296162
2017-02-24 19:52:52 +00:00
Rafael Espindola 24e6f363c5 Merge OutputSectionBase and OutputSection. NFC.
Now that all special sections are SyntheticSections, we only need one
OutputSection class.

llvm-svn: 296127
2017-02-24 15:07:30 +00:00
Rafael Espindola 29c1afb880 Delete trivial setter.
llvm-svn: 296124
2017-02-24 14:34:12 +00:00
Rafael Espindola 40849419e2 Delete trivial getter.
llvm-svn: 296123
2017-02-24 14:28:00 +00:00
Rafael Espindola d224c08efc Delete unused enum values.
llvm-svn: 296118
2017-02-24 13:26:52 +00:00
Rafael Espindola a2e3fee072 Remove unnecessary template. NFC.
llvm-svn: 296117
2017-02-24 13:19:33 +00:00
Rafael Espindola 798ad9a1e8 Expand a comment. NFC.
llvm-svn: 296114
2017-02-24 13:06:59 +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
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 774ea7d0a9 Make InputSection a class. NFC.
With the current design an InputSection is basically anything that
goes directly in a OutputSection. That includes plain input section
but also synthetic sections, so this should probably not be a
template.

llvm-svn: 295993
2017-02-23 16:49:07 +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
George Rimar 2146787609 [ELF] - Refactoring of LMA offset handling code. NFC.
Thanks to Rui Ueyama for suggestion.

llvm-svn: 295943
2017-02-23 07:57:55 +00:00
Rui Ueyama b7f39b08f4 Slightly improve an error message.
llvm-svn: 295942
2017-02-23 07:35:30 +00:00
Rui Ueyama c1a0ac2c4a Add a comment about .note.GNU-split-stack.
llvm-svn: 295941
2017-02-23 07:35:11 +00:00
Rui Ueyama 65efe35632 Update comment: .note.GNU-stack is added by default today.
llvm-svn: 295939
2017-02-23 07:15:46 +00:00
Rui Ueyama 0633273d05 Update a comment about .note.GNU-stack.
llvm-svn: 295938
2017-02-23 07:06:43 +00:00
Rui Ueyama 6e0142d78c Use isa<MergeInputSection> to check if a section is a merge section.
We have InputSection, MergeInputSection and EhInputSection, so
isa<MergeInputSection> is equivalent to !isa<InputSection> && !isa<
EhInputSection>.

llvm-svn: 295937
2017-02-23 06:47:18 +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
Rafael Espindola c8d0c7a0e3 Add another missing instantiation.
llvm-svn: 295932
2017-02-23 03:38:14 +00:00
Rafael Espindola 277155b054 Add missing template instantiations.
llvm-svn: 295929
2017-02-23 03:18:03 +00:00
Rui Ueyama 65316d7b36 Use uint64_t instead of ELFT*::uint.
This shouldn't change the meaning of the code.

llvm-svn: 295927
2017-02-23 03:15:57 +00:00
Rafael Espindola 4b8baab907 Trying to fix the windows build.
llvm-svn: 295925
2017-02-23 02:50:50 +00:00
Rafael Espindola c404d50d7c Merge InputSectionData and InputSectionBase.
Now that InputSectionBase is not a template there is no reason to have
the two.

llvm-svn: 295924
2017-02-23 02:32:18 +00:00
Rafael Espindola b4c9b81aad Convert InputSectionBase to a class.
Removing this template is not a big win by itself, but opens the way
for removing more templates.

llvm-svn: 295923
2017-02-23 02:28:28 +00:00
Rafael Espindola a55b86c0e5 Reduce templating a bit. NFC.
llvm-svn: 295909
2017-02-23 00:02:03 +00:00
Rui Ueyama e9bc0b5a3c Add a comment about thread safety.
llvm-svn: 295876
2017-02-22 21:05:06 +00:00
George Rimar a8d8dcf6ef [ELF] - Addressed post commit review comments for D30187
* Added comment.
* Pass std::string copy instead using move semantic.

llvm-svn: 295817
2017-02-22 09:13:04 +00:00
Rui Ueyama e6e206d4b4 Do not use errs() or outs() directly. Instead use message(), log() or error()
LLD is a multi-threaded program. errs() or outs() are not guaranteed
to be thread-safe (they are actually not).

LLD's message(), log() or error() are thread-safe. We should use them.

llvm-svn: 295787
2017-02-21 23:22: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
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
George Rimar 60f1fe8438 [ELF] - Make ASSERT() return Dot instead of evaluated value.
Previously ASSERT we implemented returned expression value.
Ex:
. = ASSERT(0x100);
would set Dot value to 0x100

Form of assert when it is assigned to Dot was implemented for 
compatibility with very old GNU ld which required it.
Some scripts in the wild, including linux kernel scripts 
use such ASSERTs at the end for doing different checks.

Currently we fail with "unable to move location counter backward"
for such scripts. Patch changes ASSERT to return location counter 
value to fix that.

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

llvm-svn: 295703
2017-02-21 07:33:38 +00:00
Peter Smith 55865432b4 [ELF] Allow local symbols to be added after global symbols
This change moves the SymbolBodies with isLocal() == true before the global
symbols then calculating NumLocals rather than assuming all locals are
added before globals and the first NumLocals have isLocal() == true. This
permits Thunks to be moved after the pass that adds global symbols from
synthetics to the symbol table.

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

llvm-svn: 295650
2017-02-20 11:12:33 +00:00
Rui Ueyama 528d874583 Remove a dead function.
llvm-svn: 295633
2017-02-20 02:39:38 +00:00
Rui Ueyama 3fbf02ddd8 Add more comments about copy relocations.
llvm-svn: 295632
2017-02-20 02:22:56 +00:00
Rui Ueyama a80915853a Add a comment about the copy relocation.
llvm-svn: 295622
2017-02-19 22:48:33 +00:00
George Rimar 858a659a4f [ELF] - Added support of linkerscript's "/DISCARD/" for --emit-relocs
Previously LLD crashed on on provided testcases because "/DISCARD/" was
not supported. Patch implements that.

After this I think there is no known issues with --emit-relocs implementation
required for linux kernel linking.

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

llvm-svn: 295488
2017-02-17 19:46:47 +00:00
Rafael Espindola 3773bcac55 Fix --print-gc-sections with linker scripts.
Before it would never print anything.

Thanks to George Rimar for pointing it out.

llvm-svn: 295485
2017-02-17 19:37:30 +00:00
George Rimar 647c1685b6 [ELF] - Move DependentSections vector from InputSection to InputSectionBase
I splitted it from D29273.
Since we plan to make relocatable sections as dependent for target ones for
--emit-relocs implementation, this change is required to support .eh_frame case.

EhInputSection inherets from InputSectionBase and not from InputSection.
So for case when it has relocation section, it should be able to access DependentSections
vector.

This case is real for Linux kernel.

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

llvm-svn: 295483
2017-02-17 19:34:05 +00:00
Rafael Espindola ecbfd871f9 Don't print DISCARD sections as gced.
This is a small difference I noticed to gold and bfd. When given
--print-gc-sections, we print sections a linkerscript marks
DISCARD. The other linkers don't.

llvm-svn: 295467
2017-02-17 17:35:07 +00:00
Rafael Espindola 679828ff92 Diagnose another case of the location counter moving backwards.
This case should be possible to handle, but it is hard:

* In order to create program headers correctly, we have to scan the
  sections in the order they are in the file.

* To find that order, we have to "execute" the linker script.

* The linker script can contain SIZEOF_HEADERS.

So to support this we have to start with a guess of how many headers
we need (3), run the linker script and try to create the program
headers. If it turns out we need more headers, we run the script again
with a larger SIZEOF_HEADERS.

Also, running the linker script depends on knowing the size of the
sections, so we have to finalize them. But creating the program
headers can change the value stored in some sections, so we have to
split size finalization and content finalization.

Looks like the last part is also needed for range extension thunks, so
we might support this at some point. For now just report an error
instead of producing broken files.

llvm-svn: 295458
2017-02-17 16:26:13 +00:00
Rafael Espindola 4cd7352c4f Reject moving the location counter backwards.
We were only checking when the assignment was inside a section.

llvm-svn: 295454
2017-02-17 16:01:51 +00:00
Rafael Espindola 1a5c152b9c Add static. NFC.
llvm-svn: 295448
2017-02-17 14:32:15 +00:00
Rui Ueyama 85c2201fc1 Rename getAliases -> getSymbolsAt.
The previous name caused a little confusion because the function not
only returns aliases but a given symbol itself too.

llvm-svn: 295408
2017-02-17 03:34:17 +00:00
Rui Ueyama 5acac350e7 Add a comment.
llvm-svn: 295388
2017-02-17 00:40:44 +00:00
Rafael Espindola ee61d3589f Merge reloc sections in -emit-reloc mode.
Without this we would produce two relocation sections pointing to the
same section, which gnu tools reject.

This fixes pr31986.

The implementation of -r/--emit-reloc is getting fairly
complicated. But lets get the test passing before trying to refactor
it.

llvm-svn: 295385
2017-02-17 00:28:17 +00:00
Rafael Espindola 6386628aae Move specialization to try to fix the bots.
I cannot reproduce the issue locally, but for some reason some bots
want to instantiate this from the header.

llvm-svn: 295365
2017-02-16 19:23:15 +00:00
Rafael Espindola 8290274c13 Share more output section creation code.
We can do this now that the linker script and the writer agree on
which sections should be combined.

llvm-svn: 295341
2017-02-16 17:32:26 +00:00
George Rimar 505ac8dc41 [ELF] - Do not crash when discarding sections that are referenced by others.
SHF_LINK_ORDER sections adds special ordering requirements.
Such sections references other sections. Previously we would crash
if section that other were referenced to was discarded by script.

Patch fixes that by discarding all dependent sections in that case.
It supports chained dependencies, testcase is provided.

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

llvm-svn: 295332
2017-02-16 16:06:13 +00:00
Rafael Espindola 908a3d3420 Ignore relocation sections in linker scripts.
Unfortunately, the common way of writing linker scripts seems to be
to get the output of ld.bfd --verbose and edit it a bit.

Also unfortunately, the bfd default script contains things like

.rela.dyn : { *(... .rela.data ...) }

but bfd actually ignores that for -emit-relocs, so we have to do the
same.

llvm-svn: 295324
2017-02-16 14:36:09 +00:00
Rafael Espindola 82f00ec4a2 Fix crash with -emit-relocs -shared.
The code to handle the input SHT_REL/SHT_RELA sections was getting
confused with the linker generated relocation sections.

llvm-svn: 295322
2017-02-16 14:23:43 +00:00
George Rimar 09015fee3c [ELF] - Allow section to have multiple dependent sections.
That fixes a case when section has more than one metadata 
section. Previously GC would collect one of such sections 
because we had implementation that stored only last one as
dependent.

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

llvm-svn: 295298
2017-02-16 08:41:19 +00:00
Rui Ueyama cd19b039ce Use isRelExprOneOf.
llvm-svn: 295289
2017-02-16 06:24:16 +00:00
Rui Ueyama f829e8c97d Removes a trivial accessor.
llvm-svn: 295288
2017-02-16 06:12:41 +00:00
Rui Ueyama 924b361d01 Do not overload a one-bit variable, NeedsCopyOrPltAddr.
This patch removes NeedsCopyOrPltAddr and instead add two variables,
NeedsCopy and NeedsPltAddr. This uses one more bit in Symbol class,
but the actual size doesn't increase because we had unused bits.
This should improve code readability.

llvm-svn: 295287
2017-02-16 06:12:22 +00:00
Rui Ueyama 26ad057099 Add comments.
llvm-svn: 295283
2017-02-16 04:51:46 +00:00
Rui Ueyama 750c11c099 Split a function and add comments.
This is slightly inefficient than the previous code, but that is really
negligible as this function is usually called at most only a few times.

llvm-svn: 295282
2017-02-16 04:39:45 +00:00
Rui Ueyama dec4ab0d0d Add comments.
llvm-svn: 295280
2017-02-16 04:19:03 +00:00
Rui Ueyama da5cc84661 Add CopyRelSection instances to BSS in the regular way.
Previously, space in a BSS section for copy relocations are reserved
in a special way. We directly manipulated size of the BSS section.
r294577 changed the way of doing it. Now, we create an instance of
CopyRelSection (which is a synthetic input section) for each copy
relocation.

This patch removes the remains of the old way and add CopyRelSections
to BSS sections using `addSections` function, which is the usual
way to add an input section to an output section.

llvm-svn: 295278
2017-02-16 04:12:19 +00:00
Rafael Espindola 7386ceac74 Addends should always be signed.
In the target dependent code we already always return a int64_t. In
the target independent code we carefully use uintX_t, which has the
same result given 2 complement rules.

This just simplifies the code to use int64_t everywhere.

llvm-svn: 295263
2017-02-16 00:12:34 +00:00
Ed Schouten 3bf713043a Make --export-dynamic work on non-PIC/PIE targets.
For CloudABI I'm only interested in generating non-PIC/PIE executables
on armv6 and i686, as PIE introduces larger overhead than on aarch64 and
x86_64. Still, I want to be able to instruct the linker to generate a
dynamic symbol table if requested. One example use for this is that
dynamic symbol tables can be used by programs to print nicely formatted
stacktraces, including symbol names.

Right now there seems to be some logic in LLD that it only wants to emit
dynamic symbol tables when either linking against libraries or when
building PIC. Let's extend this to also take --export-dynamic into
account.

Reviewed by:	ruiu
Differential Revision:	https://reviews.llvm.org/D29982

llvm-svn: 295240
2017-02-15 21:51:25 +00:00
Rui Ueyama 731a66ae98 Apply different tokenization rules to linker script expressions.
The linker script lexer is context-sensitive. In the regular context,
arithmetic operator characters are regular characters, but in the
expression context, they are independent tokens. This afects how the
lexer tokenizes "3*4", for example. (This kind of expression is real;
the Linux kernel uses it.)

This patch defines function `maybeSplitExpr`. This function splits the
current token into multiple expression tokens if the lexer is in the
expression context.

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

llvm-svn: 295225
2017-02-15 19:58:17 +00:00
Simon Atanasyan 91e30ae205 [ELF][MIPS] Fix writing updated addend for R_MIPS_GOT16 relocation
If target of R_MIPS_GOT16 relocation is a local symbol its addend
is high 16 bits of complete addend. To calculate a final value, the addend
of this relocation is read, shifted to the left and combined with addend
of paired R_MIPS_LO16 relocation. To save updated addend when the linker
produces a relocatable output, we need to store high 16 bits of the
addend's value. It is different from the case of writing the relocation
result when the linker saves a 16-bit GOT index as-is.

llvm-svn: 295159
2017-02-15 08:33:03 +00:00
Rafael Espindola d50c8598fb Set the correct r_offset even when creating a R_*_NONE.
With this lld can use its own -r output when a fde is discarded.

llvm-svn: 295143
2017-02-15 01:53:23 +00:00
Rafael Espindola f340ca8963 Make lld able to handle gold's -r output.
This is still not sufficient for lld to handle its own output when a
fde points to a discarded section. I am investigating if it is better
to change the -r output or make lld able to read the current version.

llvm-svn: 295141
2017-02-15 01:29:23 +00:00
Rafael Espindola 60b02509dd Handle .eh_frame pointing to discarded section in -r.
This is a really horrible case. If a .eh_frame points to a discarded
section, it is not clear what is the correct thing to do.

It looks like ld.bfd discards the entire .eh_frame content and gold
discards the second relocation, leaving one frame with an fde that
refers to a bogus location. This is similar to what gold does.

llvm-svn: 295133
2017-02-15 00:59:50 +00:00
Rafael Espindola a8541675e0 Revert "Relax the restriction on what relocations can be in a non-alloc section."
This reverts commit r295102.

In the link of seabios the assumption seems to be that the section has
an actual address, so this is not sufficient. Changing the assembly
code to add a "a" flag seems like the correct thing to do instead of
extending this hack.

Sorry about the noise.

Original message:

    Relax the restriction on what relocations can be in a non-alloc section.

    The main thing that they can't have is relocations that require the
    creation of gots or plt.  For now also accept R_PC.

    Found while linking seabios.

llvm-svn: 295130
2017-02-15 00:27:47 +00:00
Rafael Espindola 1b36eeaafa Fix the st_name of section symbols.
If it is non-zero then objdump will print an empty name when printing
relocations against the section.

llvm-svn: 295129
2017-02-15 00:23:09 +00:00
Rui Ueyama 35723f09bb Explicitly return a new value instead of implicitly mutating a reference.
I think this is more readable than before.

llvm-svn: 295121
2017-02-14 23:35:42 +00:00
Rafael Espindola ee29e42b33 Relax the restriction on what relocations can be in a non-alloc section.
The main thing that they can't have is relocations that require the
creation of gots or plt.  For now also accept R_PC.

Found while linking seabios.

llvm-svn: 295102
2017-02-14 20:14:03 +00:00
Rui Ueyama f9a45628a3 Remove stray semicolon.
llvm-svn: 295077
2017-02-14 17:36:23 +00:00
George Rimar ee6f22ce0b [ELF] - Do not segfault when using -r and section groups.
If we had SHT_GROUP sections, then when -r was used we might crash.
This is PR31952.

Issue happened because we emited relocation section though its target was discared
because was a member of duplicated group. When we tried to get VA of target,
segfault happened.
Core cause is the bug that GNU as 2.27 (and probably later versions) has.
In compare with llvm-mc, it does not include relocation sections into the group, 
like shown in testcase. This patch covers that case.

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

llvm-svn: 295067
2017-02-14 16:42:38 +00:00
Rafael Espindola 17ba44519b Sign extend remaining implicit addends for consistency.
llvm-svn: 295062
2017-02-14 16:24:42 +00:00
George Rimar bdce4ad21d [ELF] - Do sign extend for addends of R_386_8, R_386_16 relocations
Patch makes addends for addends of R_386_8, R_386_16 relocations
be sign extended.

The same we did earlier for PC ones,
currenly LLD fails to link linux kernel, 
reporting relocation out of range because of this.

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

llvm-svn: 295052
2017-02-14 13:22:03 +00:00
Simon Atanasyan 7a2a1ff37f [ELF][MIPS] Use `InputSectionBase::getOutputSection()` to access output sections from the `MipsGotSection`
Follow-up to r294005. This patch restores handling of MISP GOT
relocations against merge sections.

llvm-svn: 295040
2017-02-14 09:56:16 +00:00
Rui Ueyama 104e2357de Do not store a computable attributes to Config.
llvm-svn: 295031
2017-02-14 05:45:47 +00:00
Rui Ueyama 4c82b4f6fa Add file comments for ScriptParser.cpp.
llvm-svn: 295023
2017-02-14 04:47:24 +00:00
Rui Ueyama 794366a237 Rename ScriptParser.{cpp,h} -> ScriptLexer.{cpp,h}.
These files contain a lexer, so the new names are better.
The parser is in LinkerScript.{cpp,h}.

llvm-svn: 295022
2017-02-14 04:47:05 +00:00
Rafael Espindola 195fba2967 Fix checks for R_386_8 and R_386_16.
Results created by these relocations are expected to be zero extended
at runtime.

llvm-svn: 294988
2017-02-13 21:29:56 +00:00
Peter Collingbourne 904c5ed558 ELF: Read all dynamic lists specified on the command line.
We were previously only reading the last one.

Fixes PR31939 (which was likely the issue underlying
https://github.com/google/oss-fuzz/issues/295).

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

llvm-svn: 294977
2017-02-13 18:31:12 +00:00
Davide Italiano db4b0a7194 [LTO] Add support for optimization remarks.
Differential Revision:  https://reviews.llvm.org/D29878

llvm-svn: 294971
2017-02-13 17:49:18 +00:00
Rafael Espindola d6e9ef7e21 Implement R_X86_64_16.
It is used by qemu.

llvm-svn: 294965
2017-02-13 16:21:34 +00:00
Rafael Espindola 08d6a3f133 Create only one section symbol per section.
Unfortunately some consumers of our .o files produced with -r expect
only one section symbol per section. That is true of at least of go's
own linker.

Combining them is a somewhat convoluted process. We have to create a
symbol for every section since we don't know which ones will be
needed. The relocation sections also have to be written first to
handle the Elf_Rel addend.

I did consider a completely different approach:

We could remove the -r special case of relocation sections when
reading. We would instead have a copyRelocs function that is used
instead of scanRelocs. It would create a DynamicReloc for each
relocation and a RelocationSection for each input relocation section.

A complication of such change is that DynamicReloc would have to take
a section index and a input section instead of a symbol since with
-emit-relocs some DynamicReloc would hold relocations referring to the
dynamic symbol table and other to the static symbol table.

That would be a pretty big change, and if we do it it is probably
better to do it as a refactoring.

llvm-svn: 294816
2017-02-11 01:40:49 +00:00
Eric Fiselier 87c87f4c30 [CMake] Fix pthread handling for out-of-tree builds
LLVM defines `PTHREAD_LIB` which is used by AddLLVM.cmake and various projects
to correctly link the threading library when needed. Unfortunately
`PTHREAD_LIB` is defined by LLVM's `config-ix.cmake` file which isn't installed
and therefore can't be used when configuring out-of-tree builds. This causes
such builds to fail since `pthread` isn't being correctly linked.

This patch attempts to fix that problem by renaming and exporting
`LLVM_PTHREAD_LIB` as part of`LLVMConfig.cmake`. I renamed `PTHREAD_LIB`
because It seemed likely to cause collisions with downstream users of
`LLVMConfig.cmake`.

llvm-svn: 294690
2017-02-10 01:59:20 +00:00
Rafael Espindola ed441c7060 Handle gcing user created metadata.
In particular, this should allow us to gc unused asan metadata.

llvm-svn: 294592
2017-02-09 15:17:56 +00:00
Peter Smith f09245a689 [ELF] Refactor PltSection and IPltSection into PltSection [NFC]
Much of the code in PltSection and IPltSection is similar, we identify
the IPlt by a HeaderSize of 0 and alter our behaviour in the member
functions appropriately:
-Iplt does not have a header
-Iplt always follows after the Plt
    
Differential Revision: https://reviews.llvm.org/D29664

llvm-svn: 294579
2017-02-09 10:56:15 +00:00
Peter Smith ebfe994142 [ELF] Use synthetic section to hold copy relocation
When we need a copy relocation we create a synthetic SHT_NOBITS
section that contains the right amount of ZI and assign it to either
.bss or .rel.ro.bss as appropriate. This allows the dynamic relocation
to be placed on the InputSection, removing the last case where a
dynamic relocation is stored as an offset from the OutputSection. This
has the side effect that we can run assignOffsets() after scanRelocs()
without losing the additional ZI needed for the copy relocations.

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

llvm-svn: 294577
2017-02-09 10:27:57 +00:00
George Rimar 82bd8be6d8 Recommit r294464 "[ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3"
with temporarily file name fix in testcase.

Original commit message:

-q, --emit-relocs - Generate relocations in output

Simplest implementation: 
* no GC case, 
* no "/DISCARD/" linkerscript command support.

This patch is extracted from D28612 / D29636,

Relative to PR31579.

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

llvm-svn: 294469
2017-02-08 16:18:10 +00:00
George Rimar 0b2cc8190d Reverted r294464 "[ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3"
Broked build bot:
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/5835/steps/test/logs/stdio

llvm-svn: 294466
2017-02-08 16:10:14 +00:00
George Rimar d6ae624552 [ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3
-q, --emit-relocs - Generate relocations in output

Simplest implementation: 
* no GC case, 
* no "/DISCARD/" linkerscript command support.

This patch is extracted from D28612 / D29636,

Relative to PR31579.

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

llvm-svn: 294464
2017-02-08 15:53:33 +00:00
Rafael Espindola ea590d91a0 Revert "Simplify symbol computation for non alloc sections."
This reverts commit r294346. Looks like it regressed the build of
magenta.

llvm-svn: 294460
2017-02-08 15:19:03 +00:00
George Rimar 4e01c3e8cd [ELF] - Linkerscript - fix handling of OUTPUT_ARCH command.
OUTPUT_ARCH command can contain architecture values separated with ":", like:
OUTPUT_ARCH(i386:x86-64)

We did not support that, because got 3 lexer tokens here after recent changes.

This trivial patch fixes the issue, now whole expression inside 
OUTPUT_ARCH is just ignored.

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

llvm-svn: 294432
2017-02-08 09:59:06 +00:00
Petr Hosek 165088aa5c [ELF] Handle output section alignment in linker scripts
LLD already parses ALIGN expression to specifiy alignment for output
sections in linker scripts but it never applies the alignment to the
output section. This change handles that.

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

llvm-svn: 294374
2017-02-07 23:42:31 +00:00
Rafael Espindola 193b158b39 Simplify symbol computation for non alloc sections.
We now just keep the address the section would have if it was
allocatable. Only the writer ignores it at the very end.

llvm-svn: 294346
2017-02-07 20:22:04 +00:00
Dmitry Mikulin f3965c0246 Handle the case where 'local' is the name of a global in a version script:
{ global : local; local: *; };

llvm-svn: 294343
2017-02-07 19:50:47 +00:00
George Rimar c6cf1f1f02 [ELF] - Assign proper values for DefinedSynthetic symbols attached to non-allocatable sections.
DefinedSynthetic symbols are attached to sections,
for the case when such symbol was attached to non-allocated section,
we calculated its value incorrectly.

We subtracted Body->Section->Addr, but non-allocatable sections
should have zero VA in output and therefore result value was wrong.

And at the same time we have Body->Section->Addr != 0 for them 
internally because  use it for calculation of section size.

Patch fixes calculation of such symbols values.

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

llvm-svn: 294322
2017-02-07 17:51:35 +00:00
George Rimar b2b70975e0 [ELF] - Refactoring: reuse similar method.
We had assignSymbol and assignSectionSymbol methods which has similar functionality.
Patch removes one of copy and reuses another in code.

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

llvm-svn: 294290
2017-02-07 10:23:28 +00:00
George Rimar 89108cc1c6 [ELF] - Use SignExtend when reading R_386_PC8, R_386_PC16 addends.
Previously we did not do that. For example, for R_386_PC8, 
0xFF addend was not treated as 0xFFFFFFFF(-1), 
but was 0x000000FF.

Recently added checks for R_386_PC8/R_386_PC16 failed because of calculation 
overflow as a result.

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

llvm-svn: 294289
2017-02-07 09:58:27 +00:00
Rui Ueyama 640724c1b2 Change the return type of getImplicitAddend to signed integer.
If relocations don't have addends, addends are embedded in operands.
getImplicitAddend is a function to read addends. Addends can be
negative numbers, so the return type of the function should be a
signed integer type.

llvm-svn: 294253
2017-02-06 22:32:45 +00:00
Rafael Espindola 06f4743a48 Handle symbol assignments before the first section switch.
We now create a dummy section with index 1 before processing the
linker script.

Thanks to George Rimar for finding the bug and providing the initial
testcase.

llvm-svn: 294252
2017-02-06 22:21:46 +00:00
Rui Ueyama 7ddd7a8b76 Use a utility function to reduce repetition. NFC.
llvm-svn: 294117
2017-02-05 05:18:58 +00:00
Rafael Espindola 2b07455315 Simplify. NFC.
Now that each OutputSectionCommand maps to just one OutputSection, we
can remove a few std::vectors.

llvm-svn: 294060
2017-02-03 22:27:05 +00:00
Rafael Espindola cfe53dff72 Simplify. NFC.
llvm-svn: 294057
2017-02-03 21:59:15 +00:00