Commit Graph

1808 Commits

Author SHA1 Message Date
Sean Silva 09247f8955 Don't push relocation sections onto InputSection<ELFT>::Discarded.RelocSections
Summary:
LLVM3.3 (and earlier) would fail to include a relocation section in
the group that the section it was relocating is in. Object files
affected by this issue have been encountered in the wild when using LLD.

This would result in a siutation like:

  Section {
    Index: 5
    Name: .text._Z3fooIiEvv (6)
    Type: SHT_PROGBITS (0x1)
    Flags [ (0x206)
      SHF_ALLOC (0x2)
      SHF_EXECINSTR (0x4)
      SHF_GROUP (0x200)
    ]
    Address: 0x0
    Offset: 0x48
    Size: 5
    Link: 0
    Info: 0
    AddressAlignment: 1
    EntrySize: 0
  }
  Section {
    Index: 6
    Name: .rela.text._Z3fooIiEvv (1)
    Type: SHT_RELA (0x4)
    Flags [ (0x0)
    ]
    Address: 0x0
    Offset: 0x3F0
    Size: 24
    Link: 8
    Info: 5
    AddressAlignment: 8
    EntrySize: 24
  }

In LLD, during symbol resolution, we discard the section containing the
weak symbol, but this amounts to replacing it with
InputSection<ELFT>::Discarded.
When we later saw the corresponding relocation section, we would then
end up pusing onto InputSection<ELFT>::Discarded.RelocSections, which is
bogus.

Reviewers: ruiu, rafael

Subscribers: llvm-commits, Bigcheese

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

llvm-svn: 259831
2016-02-04 21:41:07 +00:00
Pete Cooper 8563e5a362 Set CPU_SUBTYPE_LIB64 in mach_header.
On Mac OS 10.5 and later, with X86_64 and outputting a dynamic executable,
ld64 set the CPU_SUBTYPE_LIB64 mask on the cpusubtype in the mach_header.

This adds the same functionality to lld.

rdar://problem/24507177

llvm-svn: 259826
2016-02-04 20:43:43 +00:00
Simon Atanasyan e364e2e9ce [ELF][MIPS] Support R_MIPS_PC32 relocation handling
llvm-svn: 259782
2016-02-04 12:31:39 +00:00
Simon Atanasyan 597df21eb2 [ELF][MIPS] Add handling for __gnu_local_gp symbol
This symbol is a "fake" symbol like "_gp_disp" and denotes
the GOT + 0x7FF0 value.

llvm-svn: 259781
2016-02-04 12:09:49 +00:00
Simon Atanasyan 170356ba32 [ELF][MIPS] Always create global GOT entry for symbols defined in DSO
If relocation against symbol requires GOT entry creation and this symbol
is defined in DSO, the GOT entry should be created in the 'global' part
of the GOT even if we link executable file. Also we do not need to create
a dynamic symbol table entry for global symbol corresponding to the
local GOT entry.

llvm-svn: 259778
2016-02-04 11:51:39 +00:00
Pete Cooper 4c3eee78f1 Add test missed from r259746
llvm-svn: 259747
2016-02-04 02:50:07 +00:00
Pete Cooper f8abe8c825 Set S_ATTR_SOME_INSTRUCTIONS on __text section.
ld64 sets both S_ATTR_PURE_INSTRUCTIONS and S_ATTR_SOME_INSTRUCTIONS
on __TEXT, __text.  We only had the S_ATTR_PURE_INSTRUCTIONS attribute.

rdar://problem/24495801

llvm-svn: 259744
2016-02-04 02:23:34 +00:00
Pete Cooper ceee5de088 Generate version min load commands when the platform is unknown.
In the case where we are emitting to an object file, the platform is
possibly unknown, and the source object files contained load commands
for version min, we can take the maximum of those min versions and
emit in in the output object file.

This test also tests r259739.

llvm-svn: 259742
2016-02-04 02:16:08 +00:00
Pete Cooper 451ec4b68e Add support for -sdk_version cmdline option.
This option is emitted in the min_version load commands.

Note, there's currently a difference in behaviour compared to ld64 in
that we emit a warning if we generate a min_version load command and
didn't give an sdk_version.  We need to decide what the correct behaviour
is here as its possible we want to emit an error and force clients to
provide the option.

llvm-svn: 259729
2016-02-03 23:39:05 +00:00
Pete Cooper 354809e139 Add generation of LC_VERSION_MIN load commands.
If the command line contains something like -macosx_version_min and we
don't explicitly disable generation with -no_version_load_command then
we generate the LC_VERSION_MIN command in the output file.

There's a couple of FIXME's in here.  These will be handled soon with
more tests but I didn't want to grow this patch any more than it already was.

rdar://problem/24472630

llvm-svn: 259718
2016-02-03 22:28:29 +00:00
Rafael Espindola 9e3f84bf95 Fix addend computation for IRELATIVE relocations.
llvm-svn: 259692
2016-02-03 21:02:48 +00:00
Rui Ueyama 169a6374dc Do not expect /dev/null (or NUL) non-mmap'able.
On some Windows environment, this test did not fail, because opening NUL
with FileOutputBuffer didn't fail. Thanks to George Rimar for reporting.

llvm-svn: 259669
2016-02-03 17:25:11 +00:00
Rui Ueyama cbe392629b ELF: Do not exit if it cannot open an output file.
It can fail to open an output file for various reasons, including
lack of permission, too long filename, or the output file is not
a mmap'able file.

llvm-svn: 259596
2016-02-02 22:48:04 +00:00
Pete Cooper 3a92c35539 __mh_execute_header atoms should be global and never dead strippped.
In r259574 I fixed some of the issues with the mach header symbols
and DSO handles.

This is the next issue whereby the __mh_execute_header has to not
be dead stripped, and (to match ld64) should be dynamically referenced.

The test here should also have been added in r259574 to make sure that
we emit this symbol.  But checking that it is not only emitted but also
has the correct reference type is fine.

llvm-svn: 259589
2016-02-02 22:19:01 +00:00
Rui Ueyama 327081e251 Add "REQUIRES: shell" to fix Windows buildbot.
llvm-svn: 259584
2016-02-02 21:58:39 +00:00
Pete Cooper d4f414583a Fix handling of mach header and DSO handle symbols.
The magic file which contained these symbols inherited from archive
which meant that the resolver didn't add the required atoms as archive
members only get added when referenced.  Instead we now inherit from
SimpleFile which always links in the atoms needed.

The second issue was in the handling of these symbols when we emit
the MachO.  The mach header symbol needs to be in the atom list as
it gets an offset (0), and being in the atom list makes sure it is
emitted to the symbol table.  DSO handles are not emitted to the
symbol table.

rdar://problem/24450654

llvm-svn: 259574
2016-02-02 21:37:15 +00:00
Rui Ueyama 21eecb4f14 Re-submit ELF: Report multiple errors from the driver.
This reverts r259395 which reverted r259143.

llvm-svn: 259572
2016-02-02 21:13:09 +00:00
Rui Ueyama 620170224f ELF: Rewrite "echo" command arguments in hope that would fix a test breakage.
Previously, we used both single quotes and double quotes, and
some single-quoted tokens are concatenated with next tokens because
there were no spaces between them. That may be a cause of test flakiness
on Windows, which is reported as https://llvm.org/bugs/show_bug.cgi?id=26388.

The new test commands are more straightforward than before.

llvm-svn: 259559
2016-02-02 21:03:56 +00:00
Rui Ueyama 025d59b16a ELF: Do not use fatal in LinkerScript.cpp.
This patch adds "Error" field to LinkerScript class. That field
is false by default, and set to true if there is a syntax error
in an input file. The linker script parser is a recursive-descedent
parser. Each function returns if Error is true -- so that
eventually the whole parser returns to a caller.

http://reviews.llvm.org/D16667

llvm-svn: 259557
2016-02-02 20:27:59 +00:00
Rui Ueyama fdcb3ceb75 Do not use filename in a lit test.
So that the file is move-safe.

llvm-svn: 259555
2016-02-02 20:24:33 +00:00
George Rimar 5c36e5938d [ELF] Implemented -Bsymbolic-functions command line option
-Bsymbolic-functions: 
When creating a shared library, bind references to global 
function symbols to the definition within the shared library, if any.

This patch also fixed behavior of already existent -Bsymbolic:
previously PLT entries were created even if -Bsymbolic was specified.

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

llvm-svn: 259481
2016-02-02 09:28:53 +00:00
Simon Atanasyan 40d25f3358 [ELF] Finalize .dynamic section at the end
Some dynamic table tags like RELSZ and PLTRELSZ depens on result of
finalizing corresponding relocation sections. Therefore we have to
finalize .dynamic section at the end.

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

llvm-svn: 259478
2016-02-02 09:07:47 +00:00
Rui Ueyama 71c066d8cf ELF: Include archive names in error messages.
If object files are drawn from archive files, the error message should
be something like "conflict symbols in foo.a(bar.o) and baz.o" instead
of "conflict symbols in bar.o and baz.o". This patch implements that.

llvm-svn: 259475
2016-02-02 08:22:41 +00:00
Rui Ueyama 21923996f8 ELF: Do not call fatal() if relocation contraints are not satisfied.
http://reviews.llvm.org/D16648

llvm-svn: 259435
2016-02-01 23:28:21 +00:00
Pete Cooper 55634d8e8d MachoFile should default to using subsections_via_symbols.
When we parse a MachoFile, we set a number of members from the parsed
file, for example, subsectionsViaSymbols.

However, a number of passes, such as ObjCPass, create local copies of
MachoFile and don't get the benefit of setting flags and other fields in
the parser.  Instead we can just give a more sensible default as the parser
will definitely get the correct value from the file anyway.

llvm-svn: 259426
2016-02-01 22:24:44 +00:00
Nico Weber f07bd3b72d Revert r259143, it broke check-lld on Windows (see PR26388).
llvm-svn: 259395
2016-02-01 20:03:53 +00:00
Pete Cooper 4a92469260 Atomize the ObjC category list section.
__DATA, __objc_catlist contains a list of pointers to categories.

We want to atomize it so that the ObjC pass can later optimize and remove
categories.  That will be a later patch.

llvm-svn: 259386
2016-02-01 19:10:10 +00:00
Rui Ueyama 16ba669c87 ELF: Report duplicate symbols as many as possible instead of the first one.
http://reviews.llvm.org/D16647

llvm-svn: 259233
2016-01-29 19:41:13 +00:00
Rui Ueyama 1940424632 ELF: Report multiple errors from the driver.
This patch let the driver keep going until it parses all
command line options.

http://reviews.llvm.org/D16645

llvm-svn: 259143
2016-01-29 01:54:52 +00:00
Rafael Espindola e2c2461a6b Merge identical strings.
This avoids the need to have reserve and addString in sync.

We avoid hashing the global symbols again. This means that we don't
merge a global symbol that has the same name as some other string, but
that doesn't seem very common. The string table size is the same in
clang an scylladb with or without hashing global symbols again.

llvm-svn: 259136
2016-01-29 01:24:25 +00:00
Rui Ueyama c2a0d7e351 ELF: Report more than one undefined symbols if exist.
http://reviews.llvm.org/D16643

llvm-svn: 259107
2016-01-28 22:56:29 +00:00
Rafael Espindola 81e05525e3 Handle local symbols in discarded sections.
We were reserving space for them but never writing them out.

llvm-svn: 258948
2016-01-27 17:09:37 +00:00
George Rimar 02ca17906d [ELF] - Symbols from object files that override symbols in DSO are added to .dynsym table.
Main executable did not export symbols that exist both in the main executable and in DSOs before this patch.
Symbols from object files that override symbols in DSO should be added to .dynsym table.

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

llvm-svn: 258672
2016-01-25 08:44:38 +00:00
Pete Cooper 6025933789 Fix bug in handling of quoted linker script tokens and update tests to use it.
There was an off by one error because the StringRef.substr functions
second argument is a length, not the end index.

Also updated a few ELF files which failed when run on Jenkins with paths
including the @ character.  This is often used in Jenkins for shared
workspace plugin.

Reviewed by Rui Ueyama

llvm-svn: 258583
2016-01-22 23:46:37 +00:00
Pete Cooper 351164504a Add support for export_dynamic cmdline option and behaviour.
This option matches the behaviour of ld64, that is it prevents globals
from being dead stripped in executables and dylibs.

Reviewed by Lang Hames

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

llvm-svn: 258554
2016-01-22 21:13:24 +00:00
Simon Atanasyan 56ab5f0289 [ELF][MIPS] Initial support of MIPS local GOT entries
Some MIPS relocation (for now R_MIPS_GOT16) requires creation of GOT
entries for symbol not included in the dynamic symbol table. They are
local symbols and non-local symbols with 'local' visibility. Local GOT
entries occupy continuous block between GOT header and regular GOT
entries.

The patch adds initial support for handling local GOT entries. The main
problem is allocating local GOT entries for local symbols. Such entries
should be initialized by high 16-bit of the symbol value. In ideal world
there should be no duplicated entries with the same values. But at the
moment of the `Writer::scanRelocs` call we do not know a value of the
symbol. In this patch we create new local GOT entry for each relocation
against local symbol, though we can exhaust GOT quickly. That needs to
be optimized later. When we calculate relocation we know a final symbol
value and request local GOT entry index. To do that we maintain map
between addresses and local GOT entry indexes. If we start to calculate
relocations in parallel we will have to serialize access to this map.

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

llvm-svn: 258388
2016-01-21 05:33:23 +00:00
Simon Atanasyan c3e54cf17b [ELF][MIPS] Update test case to reflect recent changes in llvm-readobj
llvm-svn: 258340
2016-01-20 20:01:28 +00:00
Simon Atanasyan 0d5e1b753e [ELF] Do not keep STT_FILE symbols in the symbol table
STT_FILE symbols usually contain source file names. It is redundant
to keep this information in the output file.

llvm-svn: 258331
2016-01-20 18:59:45 +00:00
Pete Cooper 90dbab0b0d Add an ObjCPass to the MachO linker.
This pass currently emits an objc image info section if one is required.

This section contains the aggregated version and flags for all of the input
files.

llvm-svn: 258197
2016-01-19 21:54:21 +00:00
Pete Cooper 0872e46c9d Set the objc constraint on the context based on the parsed files.
Like arch, os, etc, when we know we are going to use a file, we check
that the file has compatible objc constraints to the context, throw
appropriate errors where that is not the case, and hopefully set the
objc constraints on the context for use later.

Added 2 tests to ensure that we don't have incompatibilities between
host and simulator code as both will get x86 based architectures.

llvm-svn: 258173
2016-01-19 19:46:41 +00:00
George Rimar ffb673515e [ELF] - R_386_GOT32 relocation calculation fix.
R_386_GOT32 has multiple descriptions:

"System V Application Binary Interface Intel386 Architecture Processor Supplement Version 1.1" (https://github.com/hjl-tools/x86-psABI/wiki/intel386-psABI-1.1.pdf), p36 contains next calculation for R_386_GOT32: G + A - GOT.
SYSTEM V APPLICATION BINARY INTERFACE 4 (https://refspecs.linuxfoundation.org/elf/abi386-4.pdf, p78) tolds us its G + A - P.
Oracle docs (https://docs.oracle.com/cd/E19455-01/816-0559/chapter6-26/index.html) says its should be G + A.

gold/bfd calculates it as "G + A - GOT", but GOT means the end of the GOT section.
Patch fixes current calculation to gold/ld behavior.

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

llvm-svn: 258115
2016-01-19 11:00:48 +00:00
Rafael Espindola 0bc0c02387 Prefer symbols from .o over .so.
This matches the behavior of gold and bfd ld.

llvm-svn: 258102
2016-01-18 23:54:05 +00:00
Pete Cooper 12b094d5f4 Only emit files with subsections_via_symbols if all inputs had that set.
When generating a relocatable file, its only valid to set this flag if
all of the inputs also had the flag.  Otherwise we may atomize incorrectly
when we link the relocatable file again.

Reviewed by Lang Hames.

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

llvm-svn: 257976
2016-01-16 01:09:23 +00:00
Pete Cooper 4b6bed98e2 Give error on binaries containing GC objc image infos.
The image info struct contains flags for what kind of GC/retain/release is required.

Give an error if we parse GC flags as these are unsupported.

llvm-svn: 257974
2016-01-16 00:57:07 +00:00
Pete Cooper a014ffef87 Add checking of differing swift versions in input files.
Swift versions are part of the objc image info section, and must match
for all files linked which actually have an image info section

llvm-svn: 257964
2016-01-16 00:07:22 +00:00
Pete Cooper 78f7907953 Remove unnecessary extra linked file in RUN line. NFC.
This test case was already suitably self-contained that linking against the
helloworld file added no value.

llvm-svn: 257954
2016-01-15 23:25:40 +00:00
Pete Cooper 20de822232 Check that the objc image info version is exactly 0
llvm-svn: 257953
2016-01-15 23:25:37 +00:00
Pete Cooper 2735783090 Add the GC commandline options and throw errors if they are used
llvm-svn: 257907
2016-01-15 17:39:02 +00:00
Pete Cooper 7162e8c597 Remove out of bounds reloc from reduced file.
This file was failing to build with asan enabled.  The reason being that
applyFixupFinal was writing 4-bytes worth of fixup in to an atom only
a single byte in length.

The test case didn't actually need this particular reloc so i've removed
it, although i'll follow up with future commits to actually verify that
relocs are to an address with enough space for the fixup to be applied.

llvm-svn: 257906
2016-01-15 17:25:12 +00:00
George Rimar f6bc65a3b2 Reapply r257753 with fix:
Added check for terminator CIE/FDE which has zero data size.
void EHOutputSection<ELFT>::addSectionAux(
...
 // If CIE/FDE data length is zero then Length is 4, this
 // shall be considered a terminator and processing shall end.
    if (Length == 4)
      break;
...

After this "Bug 25923 - lld/ELF2 linked application crashes if exceptions were used." is fixed for me. Self link of clang also works.

Initial commit message:
[ELF] - implemented --eh-frame-hdr command line option.

--eh-frame-hdr
Request creation of ".eh_frame_hdr" section and ELF "PT_GNU_EH_FRAME" segment header.

Both gold and the GNU linker support an option --eh-frame-hdr which tell them to construct a header for all the .eh_frame sections. This header is placed in a section named .eh_frame_hdr and also in a PT_GNU_EH_FRAME segment. At runtime the unwinder can find all the PT_GNU_EH_FRAME segments by calling dl_iterate_phdr.
This section contains a lookup table for quick binary search of FDEs.
Detailed info can be found here:
http://www.airs.com/blog/archives/462

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

llvm-svn: 257889
2016-01-15 13:34:52 +00:00