Commit Graph

2027 Commits

Author SHA1 Message Date
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 1944bb6cf9 [mach-o] Fix missing { }
llvm-svn: 217411
2014-09-09 02:20:32 +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
Rui Ueyama 553ac40f2e Partially roll back r217324.
llvm-svn: 217392
2014-09-08 18:01:42 +00:00
Simon Atanasyan e747a44fe8 Update current LLD status for MIPS target.
llvm-svn: 217372
2014-09-08 14:56:20 +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
Shankar Easwaran 0971629764 [ELF][Cleanup] Remove unused functions
Remove unused functions in the Target relocation handler.

llvm-svn: 217354
2014-09-08 02:43:01 +00:00
Rui Ueyama 930bc70b75 Remove dead code.
llvm-svn: 217324
2014-09-07 01:23:16 +00:00
Rui Ueyama 47a8a83dc3 FileArchive cleanup
Moved code used only by isDataSymbol from find to isDataSymbol member
function. Also changed the return type of isDataSymbol because
previously "if (isDataSymbol(...))" meant "if it is *not* a data symbol"
which is opposite from what you'd expect.

llvm-svn: 217285
2014-09-05 22:06:07 +00:00
Rui Ueyama 2feff785fc [PECOFF] Run lib.exe with /machine:x64.
llvm-svn: 217219
2014-09-05 01:35:23 +00:00
Rui Ueyama 30a6bc286b [PECOFF] Run cvtres.exe with /machine:x64.
If we are creating a PE+ executable, we need to run cvtres with
/machine:x64 instead of /machine:x86. Otherwise the resulting executable
would be invalid.

llvm-svn: 217214
2014-09-05 00:26:28 +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
Rui Ueyama 879b0126f4 Move function. No functionality change.
llvm-svn: 217121
2014-09-04 02:52:26 +00:00
Nick Kledzik 54e0614efd Fix unit test now that 'ld' is host specific
llvm-svn: 217114
2014-09-04 00:16:04 +00:00
Nick Kledzik cec33b84f2 If lld binary is named 'ld' on darwin, use darwin driver mode
llvm-svn: 217112
2014-09-04 00:03:36 +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 768cc6d31d Use LLD naming style.
llvm-svn: 216402
2014-08-25 19:38:57 +00:00
Rafael Espindola 0941b568ce Update for LLVM api change
llvm-svn: 216395
2014-08-25 18:16:56 +00:00
Rui Ueyama 4c6847aab9 [PECOFF] Explicitly pass machine type
x86 and x64 are created equal -- taking x86 as the default argument
made it hard to find bugs.

llvm-svn: 216295
2014-08-22 22:03:16 +00:00
Rui Ueyama 839fb2f10c Simplify. No functionality changes.
llvm-svn: 216257
2014-08-22 02:00:58 +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
Hans Wennborg 2bf6c5c371 Fix build after r216223: Explicitly pass ownership of the MemoryBuffer to AddNewSourceBuffer using std::unique_ptr
llvm-svn: 216252
2014-08-22 01:13:29 +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 e484075d82 [mach-o] Add support for -single_module and -multi_module
These are both obsolete options that controled how dylibs were built.

llvm-svn: 216205
2014-08-21 18:57:42 +00:00
Dan Liew 31f2f1befe Fixed typo (from r215544) that caused the lld-sphinx-docs buildbot
to fail.

llvm-svn: 216165
2014-08-21 10:06:34 +00:00
Rui Ueyama 6f579ac5f5 [PECOFF] Don't use default argument.
I'm adding new parameters to the function, and the existing
parameter with a default argument is found confusing.

llvm-svn: 216153
2014-08-21 03:40:33 +00:00
Rui Ueyama 5b6532fa49 Remove dead code.
llvm-svn: 216152
2014-08-21 03:05:12 +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
Nick Kledzik bb38f7bdaa Add notifier hooks to symbol table.
This is the one interesting aspect from:
   http://reviews.llvm.org/D4965

These hooks are useful for flavor specific processing, such as recording that
a DefinedAtom replaced a weak SharedLibraryAtom.

llvm-svn: 216122
2014-08-20 20:46:28 +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
Rafael Espindola 2ed1c57b34 Update for llvm api change.
llvm-svn: 216003
2014-08-19 18:44:51 +00:00
Nick Kledzik 0cc040780e [mach-o] improve darwin driver 'usage' message when run with no args
llvm-svn: 215781
2014-08-15 22:42:46 +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