Commit Graph

676 Commits

Author SHA1 Message Date
Ed Maste 933daef54f Add FreeBSD to system-linker-elf case
llvm-svn: 217672
2014-09-12 13:16:30 +00:00
Tim Northover 1684a614b3 [mach-o]: support optional "0x" prefix for -image_base
llvm-svn: 217578
2014-09-11 10:31:46 +00:00
Tim Northover 5d95bd7037 [mach-o]: tighten up diagnostics for -image_base option
The provided base must also be a multiple of the system's page size, which is a
reasonable enough demand.

Also check the other diagnostics more thoroughly.

llvm-svn: 217577
2014-09-11 10:31:42 +00:00
Nick Kledzik 50bda292c8 If lld is renamed (or symlinked) to "ld" automatically pick the right flavor.
The existing system linkers on Darwin and Linux are called "ld".  We'd like to
eventually drop in lld as "ld" and have it just work.  But lld is a universal
linker that requires the first option to be -flavor to know which command line
mode to emulate (gnu or darwin).

This change tests if argv[0] is "ld" and if so, if the tool was built on MacOSX
then assume the darwin flavor otherwise the gnu flavor.  There are two test
cases which copy lld to "ld" and then run it. One for darwin and one for linux.

llvm-svn: 217566
2014-09-11 00:52:05 +00:00
Tim Northover af3075b93f [mach-o]: implement -image_base option on Darwin.
As suggested by Nick, this will make __unwind_info implementation more natural,
and it'd have to be done at some point anyway.

llvm-svn: 217486
2014-09-10 10:39:57 +00:00
Nick Kledzik 1bebb2832e [mach-o] Add support for arm64 (AAarch64)
Most of the changes are in the new file ArchHandler_arm64.cpp.  But a few
things had to be fixed to support 16KB pages (instead of 4KB) which iOS arm64
requires.  In addition the StubInfo struct had to be expanded because
arm64 uses two instruction (ADRP/LDR) to load a global which requires two
relocations.  The other mach-o arches just needed one relocation.

llvm-svn: 217469
2014-09-09 23:52:59 +00:00
Nick Kledzik 8a8c3ba38c [mach-o] add testing coverage for -single_module
llvm-svn: 217412
2014-09-09 02:20:34 +00:00
Nick Kledzik 4ce63c2a54 [mach-o] Fix so no options causes usage message to be printed
llvm-svn: 217409
2014-09-09 00:45:35 +00:00
Nick Kledzik b7035ae367 [mach-o] Add support for -pie and -no_pie
There is a bit (MH_PIE) in the flags field of the mach_header which tells
the kernel is a program was built position independent (for ASLR).  The linker
automatically attempts to build programs PIE if they are built for a recent
OS version.  But the -pie and -no_pie options override that default behavior.

llvm-svn: 217408
2014-09-09 00:17:52 +00:00
Simon Atanasyan 07000878f8 [ELF] Export strong defined symbol if it coalesces away a weak symbol
defined in a shared library.

Now LLD does not export a strong defined symbol if it coalesces away a
weak symbol defined in a shared library. This bug affects all ELF
architectures and leads to segfault:

  % cat foo.c
  extern int __attribute__((weak)) flag;
  int foo() { return flag; }

  % cat main.c
  int flag = 1;
  int foo();
  int main() { return foo() == 1 ? 0 : -1; }

  % clang -c -fPIC foo.c main.c
  % lld -flavor gnu -target x86_64 -shared -o libfoo.so ... foo.o
  % lld -flavor gnu -target x86_64 -o a.out ... main.o libfoo.so
  % ./a.out
  Segmentation fault

The problem is caused by the fact that we lose all information about
coalesced symbols after the `Resolver::resolve()` method is finished.

The patch solves the problem by overriding the
`LinkingContext::notifySymbolTableCoalesce()` method and saving names
of coalesced symbols. Later in the `buildDynamicSymbolTable()` routine
we use this information to export these symbols.

llvm-svn: 217363
2014-09-08 09:43:38 +00:00
Shankar Easwaran c7701e9a8c [ELF][Driver] Produce a proper error when file is not found
When a file is not found, produce a proper error message. The previous error
message produced a file format error, which made me wonder for a while why
there is a file format error, but essentially the file was not found.

This fixes the problem by producing a proper error message.

llvm-svn: 217359
2014-09-08 04:18:11 +00:00
Shankar Easwaran 595e056dce [ELF] Implement --rosegment
By default linker would not create a separate segment to hold read only data.

This option overrides that behavior by creating the a separate read only segment
for read only data.

llvm-svn: 217358
2014-09-08 04:05:52 +00:00
Shankar Easwaran 24511fc426 [ELF] Undefined symbols should be allowed when building dynamic libraries
When dynamic libraries are built, undefined symbols should always be allowed and
the linker should not exit with an error.

llvm-svn: 217356
2014-09-08 03:11:50 +00:00
Nick Kledzik 635f9c7158 [mach-o] Let darwin driver infer arch from .o files if -arch not used.
Mach-O has a "fat" (or "universal") variant where the same contents built for
different architectures are concatenated into one file with a table-of-contents
header at the start.  But this leaves a dilemma for the linker - which
architecture to use.

Normally, the linker command line -arch is used to force which slice of any fat
files are used.  The clang compiler always passes -arch to the linker when
invoking it.  But some Makefiles invoke the linker directly and don’t specify
the -arch option.  For those cases, the linker scans all input files in command
line order and finds the first non-fat object file.  Whatever architecture it
is becomes the architecture for the link.

llvm-svn: 217189
2014-09-04 20:08:30 +00:00
Nick Kledzik 0e3e6155fe [mach-o] Improve x86_64 reference kind switch statements.
The use of default: was disabling the warning about unused enumerators. Fix
that, then fix the one enumerator that was not handled. Add coverage for
it in test suite.

llvm-svn: 217078
2014-09-03 21:09:24 +00:00
Nick Kledzik 141330aef6 [mach-o] Add support for using export tries
On Darwin at runtime, dyld will prefer to use the export trie of a dylib instead
of the traditional symbol table (which is large and requires a binary search).

This change enables the linker to generate an export trie and to prefer it if
found in a dylib being linked against.  This also simples the yaml for dylibs
because the yaml form of the trie can be reduced to just a sequence of names.

llvm-svn: 217066
2014-09-03 19:52:50 +00:00
Rui Ueyama 495a031ce4 [PECOFF] Fix AMD64_REL_[1-5] and AMD64_SECTION relocations
I hope this is the last fix for x64 relocations as I've wasted
a few days on this.

This caused a mysterious issue that some C++ programs crash on
startup. It was because a null pointer is passed as argv to main.
__tmainCRTStartup calls main, but before that it calls all
initialization routines between .text$xc_a and .text$xc_z.
pre_cpp_init is one of such routines, and it is the one who
initializes a heap pointer for argv for later use. That routine
was not called for some reason.

It turned out that __tmainCRTStartup was skipping a block of
code because of the relocation bug. A condition in the function
depends on a memory load, and that memory load was referring
a wrong location. As a result a jump instruction took the
wrong branch, skipping pre_cpp_init and so on.

This patch fixes the issue. Also added more tests to fix them
once and for all.

llvm-svn: 216772
2014-08-29 20:33:27 +00:00
Rui Ueyama 4a6ead7a9f [PECOFF] Another Win64 relocation bug fix
When a relocation is applied to a location, the new value needs
to be added to the existing value at the location. Existing
value is in most cases zero, but if not, the current code does
not work.

llvm-svn: 216680
2014-08-28 19:00:40 +00:00
Rui Ueyama f4b0d5e050 [PECOFF] Implement Win64 base relocations
Image Base field in the PE/COFF header is used as hint for the loader.
If the loader can load the executable at the specified address, that's
fine, but if not, it has to load it at a different address.

If that happens, the loader has to fix up the addresses in the
executable by adding the offset. The list of addresses that need to
be fixed is in .reloc section.

This patch is to emit x64 .reloc section contents.

llvm-svn: 216636
2014-08-28 00:47:11 +00:00
Rui Ueyama 06970fe7fd [PECOFF] Fix AMD64_ADDR64 relocation.
IMAGE_REL_AMD64_ADDR64 relocation should set 64-bit *VA* (virtual
address) instead of *RVA* (relative virtual address), so we have
to add the iamge base to the target's RVA.

llvm-svn: 216512
2014-08-27 01:10:01 +00:00
Rui Ueyama d718c73f0b [PECOFF] Add /HighEntropyVA.
This is yet another command line flag to set a bit in
DLLCharacteristics. Default on Win64 is "on".

llvm-svn: 216414
2014-08-25 22:23:34 +00:00
Rui Ueyama 5711df44b8 [PECOFF] Fix PE+ relocations
The implementation of AMD64 relocations was imcomplete
and wrong. On AMD64, we of course have to use AMD64
relocations instead of i386 ones. This patch fixes the
issue.

LLD is now able to link hello64.obj (created from
hello64.asm) against user32.lib and kernel32.lib to
create a Win64 binary.

llvm-svn: 216253
2014-08-22 01:15:43 +00:00
Nick Kledzik 7820c80f7d [mach-o] support N_NO_DEAD_STRIP nlist.desc bit
Mach-O symbols can have an attribute on them means their content should never be
dead code stripped.  This translates to deadStrip() == deadStripNever.

llvm-svn: 216234
2014-08-21 22:18:30 +00:00
Nick Kledzik 77afc71426 [mach-o] Fix initial live atoms with -dead_strip
When -dead_strip is used with -exported_symbols_list the initial set of
live atoms are those in the export list.

llvm-svn: 216213
2014-08-21 20:25:50 +00:00
Nick Kledzik 8c0bf75ef5 [mach-o] Add support for -exported_symbols_list and -keep_private_externs
Both options control the final scope of atoms.

When -exported_symbols_list <file> is used, the file is parsed into one
symbol per line in the file.  Only those symbols will be exported (global)
in the final linked image.

The -keep_private_externs option is only used with -r mode. Normally, -r
mode reduces private extern (scopeLinkageUnit) symbols to non-external. But
add the -keep_private_externs option keeps them private external.

llvm-svn: 216146
2014-08-21 01:59:11 +00:00
Rui Ueyama a0b988cb63 [PECOFF] Emit PE+ idata tables.
Import tables in the PE+ format is an array of 64 bit numbers,
although the executable size is still limited to 4GB in PE+.

llvm-svn: 216039
2014-08-20 01:09:42 +00:00
Nick Kledzik 94174f755c [mach-o] Support -filelist option in darwin driver
The darwin linker has an option, heavily used by Xcode, in which, instead
of listing all input files on the command line, the input file paths are
written to a text file and the path of that text file is passed to the linker
with the -filelist option (similar to @file).

In order to make test cases for this, I generalized the -test_libresolution
option to become -test_file_usage.

llvm-svn: 215762
2014-08-15 19:53:41 +00:00
Nick Kledzik 2d835dad0a [mach-o] Support -F and -framework options in darwin driver
Darwin has a packaging mechanism for shared libraries and headers called
frameworks.  A directory Foo.framework contains a shared library binary file
"Foo" and a subdirectory "Headers". Most OS frameworks are all in one
directory /System/Library/Frameworks/.  As a linking convenience, the linker
option "-framework Foo" means search the framework directories specified
with -F (analogous to -L) looking for a shared library Foo.framework/Foo.

llvm-svn: 215680
2014-08-14 22:20:41 +00:00
Nick Kledzik 8fc67fba01 [mach-o] Support re-exported dylibs
In general two-level namespace means each program records exactly which dylib
each undefined (imported) symbol comes from.  But, sometimes the implementor
wants to hide the implementation dylib.  For instance libSytem.dylib is the base
dylib all Darwin programs must link with.  A few years ago it was split up
into two dozen dylibs by all are hidden behind libSystem.dylib which re-exports
each sub-dylib.  All clients still think libSystem.dylib is the implementor.

To support this, the linker must load "indirect" dylibs and not just the
"direct" dylibs specified on the command line.  This is done in the
createImplicitFiles() method after all command line specified files are
loaded.  Since an indirect dylib may have already been loaded as a direct dylib
(or indirectly via a previous direct dylib), the MachOLinkingContext keeps
a list of all loaded dylibs.

With this change hello world can now be linked against the real OS or SDK.

llvm-svn: 215605
2014-08-13 23:55:41 +00:00
Nick Kledzik ebf09360ec [mach-o] Fix stub generation to work for dylibs and bundles
Split up the CRuntimeFile into one part for output types that need an entry
point and another part for output types that use stubs.

Add file 'test/mach-o/Inputs/libSystem.yaml' for use by test cases that
use -dylib and therefore may now need the helper symbol in libSystem.dylib.

llvm-svn: 215602
2014-08-13 23:31:07 +00:00
Nick Kledzik b47683223f [mach-o] set ordinal in n_desc for undefined symbols
Mach-o uses "two-level namespace" where each undefined symbols is associated
with a specific dylib. This means at runtime the loader (dyld) does need to
search all loaded dylibs for that symbol but rather just the one specified.

Now that llvm-nm -m prints out that info, properly set that info, and test
it in the hello world test cases.

llvm-svn: 215598
2014-08-13 23:11:42 +00:00
Chad Rosier 69e2881070 [AArch64] Initial ELF/AArch64 Support
This patch adds the initial ELF/AArch64 support to lld. Only a basic "Hello
World" app has been successfully tested for both dynamic and static compiling.

Differential Revision: http://reviews.llvm.org/D4778
Patch by Daniel Stewart <stewartd@codeaurora.org>!

llvm-svn: 215544
2014-08-13 13:16:38 +00:00
Daniel Sanders df153c1a7e [mips] Remove .end and .end from test/elf/Mips/hilo16-3.test
The commit of the .ent/.end implementation will change the result of the
relocation evaluations (because of a new section and additional relocations)
which will lead to a failure if the .ent/.end directives are present in this
test.
We don't really need the .ent/.end directives in this test so let's just remove
them to preserve the current output.

llvm-svn: 215534
2014-08-13 10:04:59 +00:00
Rui Ueyama 145ef0186c [PECOFF] Fix /include option in .drectve section.
/INCLUDE arguments passed as command line options are handled in the
same way as Unix -u. All option values are converted to an undefined
symbol and added to a dummy input file, so that the specified symbols
are resolved.

One tricky thing on Windows is that the option is also allowed to
appear in the object file's directive section. At the time when
it's being read, all (regular) command line options have already
been processed. We cannot add undefined atoms to the dummy file
anymore.

Previously, we added such /INCLUDE to a set that has already been
processed. As a result the options were ignored.

This patch fixes the issue. Now, /INCLUDE symbols in the directive
section are handled as real undefined symbol in the COFF file.
We create an undefined symbol for each /INCLUDE argument and add
it to the file being parsed.

llvm-svn: 214824
2014-08-04 23:48:57 +00:00
Simon Atanasyan 0670abdd2e [Mips] Replace assembler code by YAML to make the 'gotsym.test' test
target independent.

llvm-svn: 214641
2014-08-02 20:18:31 +00:00
Simon Atanasyan 91af42fb7b [Mips] Replace assembler code by YAML to make the 'interpreter.test' test
target independent.

llvm-svn: 214495
2014-08-01 09:47:21 +00:00
Rui Ueyama 140f6029ce [PECOFF] Fix section header.
The PE/COFF spec says that SizeOfRawData field in the section
header must be a multiple of FileAlignment from the optional
header. LLD emits 512 as FileAlignment, so it must have been
a multiple of 512.

LLD did not follow that. It emitted the actual section size
without the last padding as the SizeOfRawData. Although it's
not correct as per the spec, the Windows loader doesn't seem
to actually bother to check that. Executables created by LLD
worked fine.

However, tools dealing with executalbe files may expect it
to be the correct value, and one instance of it is mt.exe
tool distributed as a part of Windows SDK.

If CMake is invoked with "-E vs_link_exe" option, it silently
run mt.exe to embed a resource file to the resulting file.
And mt.exe sometimes breaks an input file if it's section
header does not follow the standard. That caused a misterous
error that CMake with Ninja occasionally produces a broken
executable.

This patch fixes the section header to make mt.exe and
other tools happy.

llvm-svn: 214453
2014-07-31 22:40:35 +00:00
Simon Atanasyan d7bc5d0119 XFAIL the ARM test when we don't have this target.
llvm-svn: 214420
2014-07-31 19:02:10 +00:00
Nick Kledzik 76689d0202 [mach-o] Fix test case comment and stray file copy
llvm-svn: 214278
2014-07-30 01:43:21 +00:00
Nick Kledzik 9b7b8ff32d [mach-o] Fix arm interworking with movw/movt
In some cases the address of a function will be materialized with a movw/movt
pair.  If the function is a thumb function, the low bit needs to be set on
the movw immediate value.

llvm-svn: 214277
2014-07-30 01:41:38 +00:00
Nick Kledzik 2fcbe822c0 [mach-o] Add support for -sectalign option
The -sectalign option is used to increase the alignment required for a section.
It required some reworking of how the __TEXT segment is laid out because that
segment also contains the mach_header and load commands. And the size of load
commands depend on the number of segments, sections, and dependent dylibs used.

Using this option will simplify some future test cases because the final
address of code can be pinned down, making tests of its content easier.

llvm-svn: 214268
2014-07-30 00:58:06 +00:00
Nick Kledzik 54fd4e5fcb [mach-o] Implement interworking between thumb and arm code
All iOS arm processor support switching between arm and thumb mode at call sites
by using the BLX instruction (instead of BL).  But the compiler does not know
the implementation mode for extern functions, so the linker must update BL/BLX
instructions to match what is linked is actually linked together.  In addition,
pointers to functions (such as vtables) must have the low bit set if the target
of the pointer is a thumb mode function.

llvm-svn: 214140
2014-07-28 23:06:09 +00:00
Rui Ueyama 7385120d65 [PECOFF] Fix failing test.
This should be covered by the Driver's unit tests.

llvm-svn: 214134
2014-07-28 22:31:37 +00:00
Simon Atanasyan 6052f045d7 [Mips] Remove redundant REQUIRES clause.
The exe-got.test test case is target independent.

llvm-svn: 214085
2014-07-28 14:19:18 +00:00
Nick Kledzik 21921375cc [mach-o] Add support for LC_DATA_IN_CODE
Sometimes compilers emit data into code sections (e.g. constant pools or
jump tables). These runs of data can throw off disassemblers.  The solution
in mach-o is that ranges of data-in-code are encoded into a table pointed to
by the LC_DATA_IN_CODE load command.

The way the data-in-code information is encoded into lld's Atom model is that
that start and end of each data run is marked with a Reference whose offset
is the start/end of the data run.  For arm, the switch back to code also marks
whether it is thumb or arm code.

llvm-svn: 213901
2014-07-24 23:06:56 +00:00
Simon Atanasyan 4b6d090bd3 [Mips] Replace assembler code by YAML to make the 'exe-dynsym.test' test
target independent.

llvm-svn: 213868
2014-07-24 15:42:11 +00:00
Rui Ueyama 23314e1ca6 [PECOFF] Fix entry point address.
Because of a bug, the entry point address in the PE/COFF header
was not correct.

llvm-svn: 213802
2014-07-23 20:51:04 +00:00
Rui Ueyama a27bb08e73 [PECOFF] Fix entry point functions selection
On Windows there are four "main" functions -- main, wmain, WinMain,
or wWinMain. Their parameter types are diffferent. The standard
library provides four different entry functions (i.e.
{w,}{WinMain,main}CRTStartup) for them. You need to use the right
entry routine for your "main" function.

If you give an /entry option, the specified name is used
unconditionally.

Otherwise, the linker needs to select the right one based on
user-supplied entry point function. This can be done after the
linker reads all the input files.

This patch moves the code to determine the entry point function
from the driver to a virtual input file. It also implements the
correct logic for the entry point function selection.

llvm-svn: 213713
2014-07-23 00:57:57 +00:00
Nick Kledzik 7e9808f7de [mach-o] add initial support for modes in arm code.
This patch just supports marking ranges that are thumb code (vs arm code).
Future patches will mark data and jump table ranges. The ranges are encoded
as References with offsetInAtom being the start of the range and the target
being the same atom.

llvm-svn: 213712
2014-07-23 00:51:37 +00:00
Nick Kledzik b78ad899e5 [mach-o] add support for round tripping all arm/thumb relocations
Update the parse-arm-relocs.yaml test case to run the linker back to back
to ensure all relocations round trip in and out of mach-o.

llvm-svn: 213700
2014-07-22 23:07:49 +00:00