Commit Graph

567 Commits

Author SHA1 Message Date
Nick Kledzik de9563d31b Add BumpPtrAllocator to lld::File. Switch SimpleDefinedAtom to allocate
its SimpleRefernces using the BumpPtrAllocator.

llvm-svn: 223528
2014-12-05 22:03:23 +00:00
Rui Ueyama 5ae2050420 Revert "Rewrite InputGraph's Group"
This reverts commit r223330 because it broke Darwin and ELF
linkers in a way that we couldn't have caught with the existing
test cases.

llvm-svn: 223373
2014-12-04 18:29:03 +00:00
Rui Ueyama 0152732ef4 [PECOFF] Improve /export compatibility.
Looks like the rule of /export is more complicated than
I was thinking. If /export:foo, for example, is given, and
if the actual symbol name in an object file is _foo@<number>,
we need to export that symbol as foo, not as the mangled name.

If only /export:_foo@<number> is given, the symbol is exported
as _foo@<number>.

If both /export:foo and /export:_foo@<number> are given,
they are considered as duplicates, and the linker needs to
choose the unmangled name.

The basic idea seems that the linker needs to export a symbol
with the same name as given as /export.

We exported mangled symbols. This patch fixes that issue.

llvm-svn: 223341
2014-12-04 06:09:39 +00:00
Rui Ueyama 60df72ff61 Rewrite InputGraph's Group
The aim of this patch is to reduce the excessive abstraction from
the InputGraph. We found that even a simple thing, such as sorting
input files (Mach-O) or adding a new file to the input file list
(PE/COFF), is nearly impossible with the InputGraph abstraction,
because it hides too much information behind it. As a result,
we invented complex interactions between components (e.g.
notifyProgress() mechanism) and tricky code to work around that
limitation. There were many occasions that we needed to write
awkward code.

This patch is a first step to make it cleaner. As a first step,
this removes Group class from the InputGraph. The grouping feature
is now directly handled by the Resolver. notifyProgress is removed
since we no longer need that. I could have cleaned it up even more,
but in order to keep the patch minimum, I focused on Group.

SimpleFileNode class, a container of File objects, is now limited
to have only one File. We shold have done this earlier.
We used to allow putting multiple File objects to FileNode.
Although SimpleFileNode usually has only one file, the Driver class
actually used that capability. I modified the Driver class a bit,
so that one FileNode is created for each input File.

We should now probably remove SimpleFileNode and directly store
File objects to the InputGraph in some way, because a container
that can contain only one object is useless. This is a TODO.

Mach-O input files are now sorted before they are passe to the
Resolver. DarwinInputGraph class is no longer needed, so removed.

PECOFF still has hacky code to add a new file to the input file list.
This will be cleaned up in another patch.

llvm-svn: 223330
2014-12-04 01:09:06 +00:00
Shankar Easwaran 7a64ec24d6 [Core] Remove function to not override RoundTripPasses.
RoundTripPasses should always be called in DEBUG mode if the environment
variable "LLD_RUN_ROUNDTRIP_TEST" is set.

Flavors should not be able to override this behavior.

llvm-svn: 223073
2014-12-01 20:28:54 +00:00
Shankar Easwaran 2895527ec2 [Core] Add flag to check if RoundTripPasses need to be run.
This would allow other flavor specific contexts to override the default value,
if they want to optionally run the round trip passes.

There is some information lost like the original file owner of the atom with
RoundTripPasses. The Gnu flavor needs this information inorder to implement
LinkerScript matching and for other diagnostic outputs such as Map files.

The flag also can be used to record information in the Atom if the information
to the Writer needs to be conveyed through References too.

llvm-svn: 222983
2014-12-01 01:04:11 +00:00
Rui Ueyama 0d9a181d9d [PECOFF] Create an empty PDB file if debug option is enabled.
There are many build files in the wild that depend on the fact that
link.exe produces a PDB file if /DEBUG option is given. They fail
if the file is not created.

This patch is to make LLD create an empty (dummy) file to satisfy
such build targets. This doesn't do anything other than "touching"
the file.

If a target depends on the content of the PDB file, this workaround
is no help, of course. Otherwise this patch should help build some
stuff.

llvm-svn: 222773
2014-11-25 19:01:01 +00:00
Nick Kledzik 5b9e48b4ce [mach-o] propagate dylib version numbers
Mach-o does not use a simple SO_NEEDED to track dependent dylibs.  Instead,
the linker copies four things from each dylib to each client: the runtime path
(aka "install name"), the build time, current version (dylib build number), and
compatibility version  The build time is no longer used (it cause every rebuild
of a dylib to be different).  The compatibility version is usually just 1.0
and never changes, or the dylib becomes incompatible.

This patch copies that information into the NormalizedMachO format and
propagates it to clients.

llvm-svn: 222300
2014-11-19 02:21:53 +00:00
Simon Atanasyan c77f5fa4f4 [ELF] Add CodeModel attribute to the DefinedAtom class
MIPS ELF symbols might contain some additional MIPS-specific flags
in the st_other field besides visibility ones. These flags indicate
code properties like microMIPS / MIPS16 encoding, position independent
code etc. We need to transfer the flags from input objects to the
output linked file to write them into the symbol table, adjust symbols
addresses etc.

I add new attribute CodeModel to the DefinedAtom class to hold target
specific flag and to get over YAML/Native format conversion barrier.
Other architectures/targets can extend CodeModel enumeration by their
own flags.

MIPS specific part of this patch adds support for STO_MIPS_MICROMIPS
flag. This flag marks microMIPS symbols. Such symbol should:
a) Has STO_MIPS_MICROMIPS in the corresponding .symtab record.
b) Has adjusted (odd) address in the corresponding .symtab
   and .dynsym records.

llvm-svn: 221864
2014-11-13 07:03:41 +00:00
Shankar Easwaran b43804b6f4 [ELF] Fix max-page-size option.
The user can use the max-page-size option and set the maximum page size. Dont
check for maximum allowed values for page size, as its what the kernel is
configured with.

Fix the test as well.

llvm-svn: 221858
2014-11-13 03:25:38 +00:00
Nick Kledzik 16cfa60cbd [mach-o] Support linker synthesized mach_header symbols.
On darwin in final linked images, the __TEXT segment covers that start of the
file.  That means in memory a process can see the mach_header (and load commands)
for every loaded image in a process.  There are APIs that take and return the
mach_header addresses as a way to specify a particular loaded image.

For completeness, any code can get the address of the mach_header of the image
it is in by using &__dso_handle.  In addition there are mach-o type specific
symbols like __mh_execute_header.

The linker needs to supply a definition for any of these symbols if used.  But
the address the symbol it resolves to is not in any section.  Instead it is the
address of the start of the __TEXT segment.

I needed to make a small change to SimpleFileNode to not override
resetNextIndex() because the Driver creates a SimpleFileNode to hold the
internal/implicit files that the context/writer can create. For some reason
SimpleFileNode overrode resetNextIndex() to do nothing instead of reseting
the index (which mach-o needs if the internal file is an archive).

llvm-svn: 221822
2014-11-12 22:21:56 +00:00
Shankar Easwaran 48b9fc7981 Revert "Add support library."
This reverts commit r221583.

llvm-svn: 221649
2014-11-11 00:40:36 +00:00
Shankar Easwaran 7757f1ace6 [Gnu] Support --image-base option
The value for --image-base is used as the base address of the program.

llvm-svn: 221589
2014-11-10 14:55:21 +00:00
Shankar Easwaran ba154afa06 [ELF] Support -z max-page-size option
The GNU linker allows the user to change the page size by using the option -z
max-page-size.

llvm-svn: 221584
2014-11-10 14:54:43 +00:00
Shankar Easwaran c937b4923e Add support library.
The parsing routines in the linker script to parse strings encoded in various
formats(hexadecimal, octal, decimal, etc), is needed by the GNU driver too. This
library provides helper functions for all flavors and flavors to add helper
functions which other flavors may make use of.

llvm-svn: 221583
2014-11-10 14:54:34 +00:00
Shankar Easwaran 22c76a5d79 [ELF] Support --no-align-segments.
lld generates an ELF by adhering to the ELF spec by aligning vma/fileoffset to a
page boundary, but this becomes an issue when dealing with large pages. This
adds support so that lld generated executables adheres to the ELF spec with the
rule vma % p_align = offset % p_align.

This is supported by the flag --no-align-segments.

This could be the default in few targets like X86_64 to save space on disk.

llvm-svn: 221571
2014-11-08 03:44:49 +00:00
Nick Kledzik f04ab0a244 [mach-o] Fix MachOFileNode to own archives same as ELFFileNode
My previous fix to have FileArchive own the member MemoryBuffers was not a
complete solution for darwin because nothing owned the FileArchive object.
Fixed MachOFileNode to be like ELFFileNode and have the graph node own the
archive object.

llvm-svn: 221552
2014-11-07 22:00:23 +00:00
Nick Kledzik 82d24bc932 [mach-o] Add support for -order_file option
The darwin linker lets you rearrange functions and data for better locality
(less paging).  You do this with the -order_file option which supplies a text
file containing one symbol per line.

Implementing this required a small change to LayoutPass to add a custom sorter
hook.

llvm-svn: 221545
2014-11-07 21:01:21 +00:00
Shankar Easwaran 35cab83409 [ELF] Remove is64bits() and isLittlEndian().
ELFLinkingContext had these two functions, which is really not needed since
the Writer uses a llvm::object template composed of Endianness, Alignment,
Is32bit/64bit. We could just use that and not duplicate functionality.

No Change In Functionality.

llvm-svn: 221523
2014-11-07 14:08:43 +00:00
Nick Kledzik 8f75da0db3 [mach-o] Add support for -S option
The darwin linker does not process dwarf debug info.  Instead it produces a
"debug map" in the output file which points back to the original .o files for
anything that wants debug info (e.g. debugger).

The -S option means "don't add a debug map".  lld for mach-o currently does
not generate the debug map, so there is nothing to do when this option is used.
But we need to process the option to get existing projects building.

llvm-svn: 221432
2014-11-06 03:03:42 +00:00
Shankar Easwaran e27c59d9e5 [ELF] Implement isLittleEndian for all architectures
llvm-svn: 221427
2014-11-06 02:03:38 +00:00
Nick Kledzik 5c4693d8d2 [mach-o] Add support for interposing tuples section
Darwin uses two-level-namespace lookup for symbols which means the static
linker records where each symbol must be found at runtime.  Thus defining a
symbol in a dylib loaded earlier will not effect where symbols needed by
later dylibs will be found.  Instead overriding is done through a section
of type S_INTERPOSING which contains tuples of <interposer, interposee>.

llvm-svn: 221421
2014-11-06 01:09:13 +00:00
Eric Christopher de4277a076 Fix an [-Werror,-Winconsistent-missing-override] error.
llvm-svn: 221312
2014-11-04 23:21:07 +00:00
Tim Northover f98b1c9960 [mach-o] remove __compact_unwind atoms once __unwind_info has been generated
The job of the CompactUnwind pass is to turn __compact_unwind data (and
__eh_frame) into the compressed final form in __unwind_info. After it's done,
the original atoms are no longer relevant and should be deleted (they cause
problems during actual execution, quite apart from the fact that they're not
needed).

llvm-svn: 221301
2014-11-04 21:57:32 +00:00
Rui Ueyama 5b95b72bb7 Remove virtual from a member function that's not overridden.
llvm-svn: 221230
2014-11-04 02:40:55 +00:00
Rafael Auler 347fb02093 [lld] Teach LLD how to parse most linker scripts
This patch does *not* implement any semantic actions, but it is a first step to
teach LLD how to read complete linker scripts. The additional linker scripts
statements whose parsing is now supported are:

* SEARCH_DIR directive
* SECTIONS directive
* Symbol definitions inside SECTIONS including PROVIDE and PROVIDE_HIDDEN
* C-like expressions used in many places in linker scripts
* Input to output sections mapping

The goal of this commit was guided towards completely parsing a default GNU ld
linker script and the linker script used to link the FreeBSD kernel. Thus, it
also adds a test case based on the default linker script used in GNU ld for
x86_64 ELF targets. I tested SPEC userland programs linked by GNU ld, using the
linker script dump'ed by this parser, and everything went fine. I then tested
linking the FreeBSD kernel with a dump'ed linker script, installed the new
kernel and booted it, everything went fine.

Directives that still need to be implemented:
* MEMORY
* PHDRS

Reviewers: silvas, shankarke and ruiu

http://reviews.llvm.org/D5852

llvm-svn: 221126
2014-11-03 04:09:51 +00:00
Rui Ueyama 2f28686ca0 [PECOFF] Fix /nodefaultlib option.
/nodefaultlib argument is a path name, so that needs to be compared
case-insensitive way. Also the ".lib" extension should be ignored.

llvm-svn: 220508
2014-10-23 20:42:07 +00:00
Rui Ueyama 8bae8189b2 [PECOFF] Fix exported symbol in the import library
There are two ways to specify a symbol to be exported in the module
definition file.

1) EXPORT <external name> = <symbol>
2) EXPORT <symbol>

In (1), you give both external name and internal name. In that case,
the linker tries to find a symbol using the internal name, and write
that address to the export table with the external name. Thus, from
the outer world, the symbol seems to be exported as the external name.

In (2), internal name is basically the same as the external name
with an exception: if you give an undecorated symbol to the EXPORT
directive, and if the linker finds a decorated symbol, the external
name for the symbol will become the decorated symbol.

LLD didn't implement that exception correctly. This patch fixes that.

llvm-svn: 220333
2014-10-21 21:41:28 +00:00
Nick Kledzik a721db24c0 Subclass InputGraph to get darwin linker library semantics
The darwin linker operates differently than the gnu linker with respect to
libraries. The darwin linker first links in all object files from the command
line, then to resolve any remaining undefines, it repeatedly iterates over
libraries on the command line until either all undefines are resolved or no
undefines were resolved in the last pass.

When Shankar made the InputGraph model, the plan for darwin was for the darwin
driver to place all libraries in a group at the end of the InputGraph. Thus
making the darwin model a subset of the gnu model. But it turns out that does
not work because the driver cannot tell if a file is an object or library until
it has been loaded, which happens later.

This solution is to subclass InputGraph for darwin and just iterate the graph
the way darwin linker needs.

llvm-svn: 220330
2014-10-21 21:14:11 +00:00
Shankar Easwaran 3dadd20fe5 [ELF] Implement demangle.
This adds functionality in the GNU flavor to demangle symbols when
undefined symbols are displayed to the user.

llvm-svn: 220184
2014-10-20 05:04:53 +00:00
Shankar Easwaran 2b67fca033 Sort include files according to convention.
llvm-svn: 220131
2014-10-18 05:33:55 +00:00
Nick Kledzik 5172067391 [mach-o] Add support for upward linking
To deal with cycles in shared library dependencies, the darwin linker supports
marking specific link dependencies as "upward".  An upward link is when a
lower level library links against a higher level library.

llvm-svn: 219949
2014-10-16 19:31:28 +00:00
Rui Ueyama b38b96ab4c [PECOFF] Support delay-load import table for x86
This patch creates the import address table and sets its
address to the delay-load import table. This also creates
wrapper functions for __delayLoadHelper2.

x86 only for now.

llvm-svn: 219948
2014-10-16 19:30:44 +00:00
Tim Northover 1cc4fb76da [mach-o] make __unwind_info defer to __eh_frame when necessary.
Not all situations are representable in the compressed __unwind_info format,
and when this happens the entry needs to point to the more general __eh_frame
description.

Just x86_64 implementation for now.

rdar://problem/18208653

llvm-svn: 219836
2014-10-15 19:32:21 +00:00
Nick Kledzik 4121bce3f6 [mach-o] Add Pass to create are shim Atoms for ARM interworking.
Arm code has two instruction encodings "thumb" and "arm".  When branching from
one code encoding to another, you need to use an instruction that switches
the instruction mode.  Usually the transition only happens at call sites, and
the linker can transform a BL instruction in BLX (or vice versa).  But if the
compiler did a tail call optimization and a function ends with a branch (not
branch and link), there is no pc-rel BX instruction.

The ShimPass looks for pc-rel B instructions that will need to switch mode.
For those cases it synthesizes a shim which does the transition, then modifies
the original atom with the B instruction to target to the shim atom.

llvm-svn: 219655
2014-10-14 01:51:42 +00:00
Rafael Auler b722e31906 Reapply [ELF] Only mark as DT_NEEDED libs that are strictly necessary (r219353)
When committed in r219353, this patch originally caused problems because it was
not tested in debug build. In such scenarios, Driver.cpp adds two additional
passes. These passes serialize all atoms via YAML and reads it back. Since the
patch changed ObjectAtom to hold a new reference, the serialization was removing
the extra data.

This commit implements r219853 in another way, similar to the original MIPS way,
by using a StringSet that holds the names of all copied atoms instead of
directly holding a reference to the copied atom. In this way, this commit is
simpler and eliminate the necessity of changing the DefinedAtom hierarchy to
hold a new data.

Reviewers: shankarke

http://reviews.llvm.org/D5713

llvm-svn: 219449
2014-10-09 22:06:38 +00:00
Rui Ueyama b3f97ba815 Revert "[ELF] Only mark as DT_NEEDED libs that are strictly necessary"
This reverts commit r219353 because that seems to break buildbots.

llvm-svn: 219369
2014-10-09 00:05:55 +00:00
Rafael Auler 7a18a385f0 [ELF] Only mark as DT_NEEDED libs that are strictly necessary
Enhances the creation of an ELF dynamic executable by avoiding recording
unnecessary shared libraries as NEEDED to load a program.

To do this, we must keep track of not only symbols that were referenced but
also of COPY relocations, which steal the symbol from a shared library but does
not store from which lib this symbol came from. To fix this, this commit changes
ObjectSymbol to store the original library from which this symbol came. With
this information, we are able to build a list of the exact shared libraries that
must be marked as DT_NEEDED, instead of blindly marking all shared libraries as
needed.

This logic originally came from the MIPS backend with some adaptation.

Reviewers: atanasyan, shankar.easwaran

http://reviews.llvm.org/D5574

llvm-svn: 219353
2014-10-08 22:53:49 +00:00
Rui Ueyama ecbb309e5e [PECOFF] Remember DLL names given with /delayload option.
This is a step toward full support of /delayload.

llvm-svn: 219344
2014-10-08 21:38:10 +00:00
Rafael Auler ce1af1a201 [ELF] Implement --export-dynamic/-E
When creating a dynamic executable and receiving the -E flag, the linker should
export all globally visible symbols in its dynamic symbol table.

This commit also moves the logic that exports symbols in the dynamic symbol
table from OutputELFWriter to the ExecutableWriter class. It is not correct to
leave this at OutputELFWriter because DynamicLibraryWriter, another subclass of
OutputELFWriter, already exports all symbols, meaning we can potentially end up
with duplicated symbols in the dynamic symbol table when creating shared libs.

Reviewers: shankarke

http://reviews.llvm.org/D5585

llvm-svn: 219334
2014-10-08 18:54:26 +00:00
Shankar Easwaran c3550f9231 Add support to print version.
Summary: Add support in the universal driver to print the lld version and the
repository version.

Test Plan: A driver test is added

Reviewers: kledzik, ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

Projects: #lld

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

llvm-svn: 219277
2014-10-08 03:47:51 +00:00
Nick Kledzik 14b5d208cb [mach-o] Support fat archives
mach-o supports "fat" files which are a header/table-of-contents followed by a
concatenation of mach-o files (or archives of mach-o files) built for
different architectures.  Previously, the support for fat files was in the
MachOReader, but that only supported fat .o files and dylibs (not archives).

The fix is to put the fat handing into MachOFileNode.  That way any input file
kind (including archives) can be fat.  MachOFileNode selects the sub-range
of the fat file that matches the arch being linked and creates a MemoryBuffer
for just that subrange.

llvm-svn: 219268
2014-10-08 01:48:10 +00:00
Saleem Abdulrasool 6211740789 PE/COFF: add a check to ensure that we dont mix up architectures
Previously, we would not check the target machine type and the module (object)
machine type.  Add a check to ensure that we do not attempt to use an object
file with a different target architecture.

This change identified a couple of tests which were incorrectly mixing up
architecture types, using x86 input for a x64 target.  Adjust the tests
appropriately.  The renaming of the input and the architectures covers the
changes to the existing tests.

One significant change to the existing tests is that the newly added test input
for x64 uses the correct user label prefix for X64.

llvm-svn: 219093
2014-10-05 23:43:59 +00:00
Nick Kledzik 09d00bb4d7 [mach-o] Add support for -dependency_info command line option
This option is added by Xcode when it runs the linker.  It produces a binary
file which contains the file the linker used.  Xcode uses the info to
dynamically update it dependency tracking.

To check the content of the binary file, the test case uses a python script
to dump the binary file as text which FileCheck can check.

llvm-svn: 219039
2014-10-04 00:16:13 +00:00
Nick Kledzik 22c9073ada Add MachOLinkingContext parameter to MachOFileNode constructor.
No functionality change.  This removes a down-cast from LinkingContext to
MachOLinkingContext.

Also, remove const from LinkingContext::createImplicitFiles() to remove
the need for another const cast.  Seems reasonable for createImplicitFiles()
to need to modify the context (MachOLinkingContext does).

llvm-svn: 218796
2014-10-01 20:24:30 +00:00
Nick Kledzik be43d7ef29 [mach-o] Implement -demangle.
The darwin linker has the -demangle option which directs it to demangle C++
(and soon Swift) mangled symbol names. Long term we need some Diagnostics object
for formatting errors and warnings. But for now we have the Core linker just
writing messages to llvm::errs(). So, to enable demangling, I changed the
Resolver to call a LinkingContext method on the symbol name.

To make this more interesting, the demangling code is done via __cxa_demangle()
which is part of the C++ ABI, which is only supported on some platforms, so I
had to conditionalize the code with the config generated HAVE_CXXABI_H.

llvm-svn: 218718
2014-09-30 23:15:39 +00:00
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 b166d45730 [mach-o] Move implementation of MachOFileNode::parse
Move method implementation from header file to .cpp file.  No functionality
change.

llvm-svn: 218639
2014-09-29 23:04:24 +00:00
Rafael Auler 5a1000dddc [lld] [ELF] Support for general dynamic TLS relocations on X86_64
Summary:
This patch adds support for the general dynamic TLS access model for X86_64 (see www.akkadia.org/drepper/tls.pdf).

To properly support TLS, the patch also changes the __tls_get_addr atom to be a shared library atom instead of a regularly defined atom (the previous lld approach). This closely models the reality of a function that will be resolved at runtime by the dynamic linker and loader itself (ld.so). I was tempted to force LLD to link against ld.so itself to resolve these symbols, but since GNU ld does not need the ld.so library to resolve this symbol, I decided to mimic its behavior and keep hardwired a definition of __tls_get_addr in the lld code.

This patch also moves some important logic that previously was only available to the MIPS lld backend to be used to all ELF backends. This logic, which now lives in the DefaultLayout class, will monitor which external (shared lib) symbols are really imported by the current module and will only populate the dynamic symbol table with used symbols, as opposed to the previous approach of dumping all shared lib symbols in the dynamic symbol table. This is important to this patch to avoid __tls_get_addr from getting injected into all dynamic symbol tables.

By solving the previous problem of always adding __tls_get_addr, now the produced symbol tables are slightly smaller. But this impacted several tests that relied on hardwired/predefined sizes of the symbol table, requiring this patch to update such tests.

Test Plan: Added a LIT test case that exercises a simple use case of TLS variable in a shared library.

Reviewers: ruiu, rafael, Bigcheese, shankarke

Reviewed By: Bigcheese, shankarke

Subscribers: emaste, shankarke, joerg, kledzik, mcrosier, llvm-commits

Projects: #lld

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

llvm-svn: 218633
2014-09-29 22:05:26 +00:00
Rui Ueyama 75c0127bb3 [PECOFF] Change export table type.
This patch changes the type of export table set from std::set to
std::vector. The new code is slightly inefficient, but because
export table elements are actually mutable, std::vector is better
here. No functionality change.

llvm-svn: 218343
2014-09-24 00:09:36 +00:00