Commit Graph

126 Commits

Author SHA1 Message Date
Tim Northover cf78d37fd6 [mach-o] create __unwind_info section on x86_64
This is a minimally useful pass to construct the __unwind_info section in a
final object from the various __compact_unwind inputs. Currently it doesn't
produce any compressed pages, only works for x86_64 and will fail if any
function ends up without __compact_unwind.

rdar://problem/18208653

llvm-svn: 218703
2014-09-30 21:29:54 +00:00
Nick Kledzik 38cd67624b [mach-o] fix test case to work with latest llvm-objdump output
Take opporunity to clean up test to only run llvm-objdump once now that
llvm-objdump can disassemble mixed thumb and arm code.

llvm-svn: 218429
2014-09-24 23:55:06 +00:00
Nick Kledzik b54bbe358a [mach-o] update test case to match new llvm-objdump output
llvm-svn: 217932
2014-09-17 00:51:18 +00:00
Nick Kledzik 1050b57a0b [mach-o] Fix two-level namespace ordinals
On darwin, the linker tools records which dylib (DSO) each undefined was found
in, and then at runtime, the loader (dyld) only looks in that one specific
dylib for each undefined symbol.  Now that llvm-objdump can display that info
I can write test cases.

llvm-svn: 217898
2014-09-16 20:27:28 +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
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
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
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
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
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
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
Nick Kledzik e1aaced0cb [mach-o] Add test case for armv6 (arm not thumb) hello world
llvm-svn: 213592
2014-07-22 00:49:49 +00:00
Nick Kledzik 03e16f2ab4 [mach-o] add support for old x86 __eh_frame sections
Over time the symbols and relocations have changed for dwarf unwind info
in the __eh_frame section.  Add test cases for older and new style.

llvm-svn: 213585
2014-07-21 22:06:57 +00:00
Nick Kledzik 7e246a47f9 [mach-o] Add support for x86 pointers which use scattered relocations
llvm-svn: 213344
2014-07-18 01:05:35 +00:00
Nick Kledzik 68a1abdf14 [mach-o] Add support for x86 CALL instruction that uses a scattered relocation
llvm-svn: 213340
2014-07-18 00:37:52 +00:00
Nick Kledzik 2d432353ba [mach-o] implement more x86 and x86_64 relocation support
Add support for adding section relocations in -r mode.  Enhance the test
cases which validate the parsing of .o files to also round trip.  They now
write out the .o file and then parse that, verifying all relocations survived
the round trip.

llvm-svn: 213333
2014-07-17 23:16:21 +00:00
Nick Kledzik 2458bec7e7 [mach-o] refactor KindHandler into ArchHandler and simplify passes.
All architecture specific handling is now done in the appropriate
ArchHandler subclass.

The StubsPass and GOTPass have been simplified.  All architecture specific
variations in stubs are now encoded in a table which is vended by the
current ArchHandler.

llvm-svn: 213187
2014-07-16 19:49:02 +00:00
Tim Northover 73c532e8a9 [mach-o]: support -Z option to skip standard library paths
llvm-svn: 212713
2014-07-10 11:46:13 +00:00
Tim Northover 18af0573df [mach-o]: support user-specified (-L) library search paths
llvm-svn: 212712
2014-07-10 11:46:08 +00:00
Tim Northover 77d82202d1 [mach-o]: support -syslibroot and -l options
These behave slightly idiosyncratically in the best of cases, and have
additional hacks layered on top of that for compatibility with badly behaved
build systems (via ld64).

For -lXYZ:
  + If XYZ is actually XY.o then search all library paths for XY.o
  + Otherwise search all library paths, first for libXYZ.dylib, then libXYZ.a
  + By default the library paths are /usr/lib and /usr/local/lib in that order.

For -syslibroot:
  + -syslibroot options apply to absolute paths in the search order.
  + All -syslibroot prefixes that exist are added to the search path *instead*
    of the original.
  + If no -syslibroot prefixed path exists, the original is kept.
  + Hacks^WExceptions:
      + If only 1 -syslibroot is given and doesn't contain /usr/lib or
        /usr/local/lib, that path is dropped entirely. (rdar://problem/6438270).
      + If the last -syslibroot is "/", all of them are ignored entirely.
        (rdar://problem/5829579).

At least, that's my best interpretation of what ld64 does in buildSearchPaths.

llvm-svn: 212706
2014-07-10 11:21:06 +00:00
Nick Kledzik 4576c115ac [mach-o] Add parsing of arm/thumb relocations
This converts the very complicated mach-o arm
relocations into the simple Reference Kinds in lld.

The next patch will use the internal Reference kinds
to fix up arm/thumb code.

llvm-svn: 212306
2014-07-04 00:11:09 +00:00
Tim Northover ca2e63daf8 [mach-o]: remove dylib install-name test with a.out
Unfortunately, the creation of (the default) output file, a.out races with all
the other tests in this directory. When the wrong one is read by macho-dump,
the test fails.

llvm-svn: 212269
2014-07-03 12:50:23 +00:00
Nick Kledzik b6e8ce8250 [mach-o] add x86 test case to build hello world. Fix bugs it uncovered.
llvm-svn: 212247
2014-07-03 02:01:21 +00:00
Nick Kledzik de0860aae4 [mach-o] add parsing of x86 relocations
llvm-svn: 212239
2014-07-02 23:52:22 +00:00
Tim Northover 9bdf90e5c2 [mach-o]: make the default dylib install-name the output filename.
llvm-svn: 212094
2014-07-01 08:41:45 +00:00
Tim Northover 301c4e690a [mach-o] add representation for LC_ID_DYLIB to MachONormalizedFile
It still needs to be tied into BinaryReader, but this allows reasonably
sensible creation of SharedLibrary atoms on MachO.

llvm-svn: 212093
2014-07-01 08:15:41 +00:00
Nick Kledzik 378066c80e [mach-o] improve errors when mixing architectures
llvm-svn: 212072
2014-06-30 22:57:33 +00:00
Tim Northover b5bf686b58 MachO: stop iterating past the end of an array
When trying to map atom types to sections, we were iterating through an array
until we hit a sentinel value. There's no need for such dances when range-based
for loops are available.

llvm-svn: 212035
2014-06-30 10:30:00 +00:00
Tim Northover 9ee9935358 MachO: re-enable writing of compact-unwind sections.
This isn't really the right place to put them in final object files (that would
be __TEXT,__unwind_info), but the format is different between relocatable and
final objects, which means we really need a pass to handle the translation.

For now, re-emitting in __LD,__compact_unwind is harmless (dyld ignores it and
moves straight on to inspecting __TEXT,__eh_frame), and sidesteps an assertion
failure when processing files containing compact-unwind info.

llvm-svn: 212032
2014-06-30 09:49:37 +00:00
Tim Northover 7b0a1306b4 MachO: align segment virtual addresses to page size.
Segments must occupy a multiple of the page size in memory (4096 currently). We
check for this when emitting files, but the placement algorithm broke down for
the second non-__TEXT segment encountered: the offset wasn't aligned up to 4096
before starting its layout.

llvm-svn: 212031
2014-06-30 09:49:33 +00:00
Tim Northover 08d6a7bfef MachO: calculate segment offsets in final MachO files properly.
Because of how we were calculating fileOffset and fileSize for segments, most
ended up at a single offset in a finalised MachO file. This meant the data
often didn't even get written in the final object, let alone where it would be
useful.

llvm-svn: 212030
2014-06-30 09:49:30 +00:00
Tim Northover f9b13d6766 MachO: support atomization of dylibs.
For .dylib files, we refrain from actually creating any atoms until they're
requested via the "exports" method.

llvm-svn: 212027
2014-06-30 09:11:38 +00:00
Nick Kledzik 3f69076278 [mach-o] refactor x86_64 relocation handling.
This is first step in reworking how mach-o relocations are processed.
The existing KindHandler is going to become a delgate/helper object for
processing architecture specific references.  The KindHandler knows how
to convert mach-o relocations into References and back, as well, as fixing
up the content the relocation is on.

One of the messy things about mach-o relocations is that they sometime 
come in pairs, but the pairs still convert to one lld::Reference. So, the
conversion has to detect pairs (arch specific) and change the stride.

llvm-svn: 211921
2014-06-27 18:25:01 +00:00
Tim Northover f48f0620ed [mach-o]: atomize zero-terminated literals correctly.
When looking through sections with zero-terminated string-literals (__cstring
or __ustring) we were constantly rechecking the first few bytes of the string
for '\0' rather than advancing along. This obviously failed unless all strings
within the section had the same length as that first one.

llvm-svn: 211682
2014-06-25 11:21:51 +00:00
Tim Northover 36bd9ab2c5 [mach-o] don't assume all sections have symbols
We were trying to examine the first symbol in a section that we wanted to
atomize by symbols, even when there wasn't one. Instead, we should make the
initial anonymous symbol cover the entire section in that situation.

llvm-svn: 211681
2014-06-25 10:59:37 +00:00
Nick Kledzik 936d5205bb [mach-o] fix use of resolver functions to not cause duplicate sections.
The previous commit uncovered a bug in the mach-o writer whereby two __text
sections were created.  But the test case did not catch that.  So I updated
the test case to run the linker a second time, reading the output of the
first pass.  

llvm-svn: 210624
2014-06-11 01:30:55 +00:00
Nick Kledzik 6085539bed [mach-o] add support for parsing resolver functions
llvm-svn: 210612
2014-06-11 00:24:16 +00:00
Nick Kledzik b423266e93 [mach-o] parse multiple symbols on same address into aliases
llvm-svn: 210502
2014-06-09 23:35:37 +00:00
Nick Kledzik a2d602560b [mach-o] Make anonymous atom out of section content before any symbol
In sections that are broken into atoms at symbols, if the first symbol in the
section is not at the start of the section, then make an anonymous atom for
the section content that is before the first symbol.

llvm-svn: 210142
2014-06-04 00:34:27 +00:00
Nick Kledzik cb9f890b77 [mach-o] Refactor sections -> atoms to be table driven.
Previously each section kind had its own code to loop over the section and
parse it into atoms.  This refactoring has two tables.  The first maps sections
to ContentType.  The second maps ContentType to information on how to find 
the atom boundaries.  

A few bugs in test cases were discovered as part of the refactoring.  
No change in functionality intended.

llvm-svn: 210138
2014-06-03 23:36:50 +00:00
Nick Kledzik acfad80250 [mach-o] Add support for custom sections
llvm-svn: 209928
2014-05-30 22:51:04 +00:00
Nick Kledzik f317d66c31 [mach-0] Add support for parsing compact unwind info section
llvm-svn: 209865
2014-05-29 23:50:48 +00:00
Nick Kledzik b367c25f28 [mach-o] Add support for parsing __eh_frame section. Generalize support for whether symbols in a section are ignored or illegal
llvm-svn: 209858
2014-05-29 23:07:20 +00:00
Nick Kledzik 9ede702bc4 [mach-o] Add support for parsing CFString sections
llvm-svn: 209844
2014-05-29 20:44:21 +00:00
Nick Kledzik cda69a279d [mach-o] Add support for parsing of weak-def symbols
llvm-svn: 209707
2014-05-28 02:04:45 +00:00
Nick Kledzik 8399d69cb2 [mach-o] rename test case
llvm-svn: 209705
2014-05-28 01:31:04 +00:00
Nick Kledzik 388f3d02de [mach-o] Support parsing of non-lazy-pointer sections
llvm-svn: 209704
2014-05-28 01:16:35 +00:00
Nick Kledzik a4a08d31cf [mach-o] Add support for initializers and terminators in object files
llvm-svn: 209700
2014-05-27 23:20:52 +00:00
Nick Kledzik 3e90e5f187 [mach-o] Add support for reading utf16 string literal sections
llvm-svn: 209684
2014-05-27 20:25:06 +00:00
Nick Kledzik 8a0bc44b71 [mach-o] Fix so that mach-o semantic errors return an error rather than assert
llvm-svn: 209469
2014-05-22 20:05:43 +00:00
Nick Kledzik e39685964b [mach-o] Use lit.local.cfg to enable mach-o test cases to be run
llvm-svn: 209383
2014-05-22 01:47:17 +00:00
Nick Kledzik a0c13a2420 [mach-o] parse literal sections into atoms
llvm-svn: 209379
2014-05-22 01:42:06 +00:00
Nick Kledzik e09cfc5f8a [mach-o] Add support for zero-fill sections.
llvm-svn: 208928
2014-05-15 23:03:50 +00:00
Nick Kledzik 61fdef6086 [mach-o] Add support and test cases for parsing tentative definitions
llvm-svn: 208919
2014-05-15 20:59:23 +00:00
Rui Ueyama 50c337a47c Remove trailing whitespaces.
llvm-svn: 208815
2014-05-14 21:49:38 +00:00
Nick Kledzik dba52ad488 [mach-o] Add test case for parsing nlist -> Scope
llvm-svn: 208814
2014-05-14 21:37:14 +00:00