Commit Graph

2088 Commits

Author SHA1 Message Date
Rafael Auler 1d73ae03c9 [ELF] Never mark the dynamic linker as DT_NEEDED
This patch adds logic to avoid putting the dynamic linker library (ld.so) as a
DT_NEEDED entry in the dynamic table. It should only appear in PT_INTERP.

This patch fixes SPEC programs 433, 445, 450, 453, 456, 462 when running on
Ubuntu Linux x86_64 and when linking SPEC programs with LLD and glibc 2.19.

http://reviews.llvm.org/D5573

llvm-svn: 218847
2014-10-02 03:52:54 +00:00
Rafael Auler 8400fa06b5 [lld] [ELF] [MIPS] Remove duplicate logic
Summary: With r218633, the logic that monitors which shared library symbols were used was copied from the MIPS lld backend to ELF classes, making it available to all ELF backends. However, this made the isDynSymEntryRequired() functions in MipsDynamicLibraryWriter.h/MipsELFWriters.h/MipsExecutableWriter.h to be duplicated logic, since this is already implemented in OutputELFWriter<>/DefaultLayout. This patch removes this duplicated code from MIPS.

Reviewers: Bigcheese, shankarke

Reviewed By: shankarke

Subscribers: llvm-commits

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

llvm-svn: 218846
2014-10-02 01:30:29 +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
Rui Ueyama 14798caac6 [PECOFF] Fix x64 export table.
Export table entry is 64 bit wide in x64. If MSB is 1, it means it's
imported by ordinal. The shift value was wrong.

llvm-svn: 218728
2014-10-01 01:39:34 +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
Rui Ueyama 07fae9691b [PECOFF] Fix /entry option.
This is yet another edge case of ambiguous name resolution.
When a symbol is specified with /entry:SYM, SYM may be resolved
to the C++ mangled function name (?SYM@@YAXXZ).

llvm-svn: 218706
2014-09-30 21:39:49 +00:00
Rui Ueyama dbddf11649 [PECOFF] Move helper function out of class
No functionality change intended.

llvm-svn: 218705
2014-09-30 21:39:46 +00:00
Tim Northover 3fc12bf860 [mach-o] add file comment to compact unwind pass
llvm-svn: 218704
2014-09-30 21:32:46 +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
Rui Ueyama fa67adc28d [PECOFF] Allow /export:<symbol>,PRTVATE.
PRIVATE option is also an undocumented feature.

llvm-svn: 218696
2014-09-30 20:09:31 +00:00
Rui Ueyama 3837e10002 [PECOFF] Fix /export option.
MSDN doesn't say about /export:foo=bar style option, but
it turned out MSVC link.exe actually accepts that. So we need that
too.

It also means that the export directive in the module definition
file and /export command line option are functionally equivalent.

llvm-svn: 218695
2014-09-30 20:03:11 +00:00
Rui Ueyama 3041443b5c [PECOFF] Fix __imp_ prefix on x64.
"__imp_" prefix always starts with double underscores.
When I was writing the original code I misunderstood
that it's "_imp_" on x64.

llvm-svn: 218690
2014-09-30 19:42:04 +00:00
Nick Kledzik a4ff361c6f update library dependency
llvm-svn: 218646
2014-09-29 23:52:50 +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 d20d44fbe6 Use DenseMap::lookup. No functionality change.
llvm-svn: 218554
2014-09-26 23:21:10 +00:00
Rui Ueyama b774a0e750 Fix crash bug on Windows.
Mutating the DenseMap here seems to cause the Windows executable
to crash. Don't use operator[] to access possibly nonexistent key.

llvm-svn: 218548
2014-09-26 22:27:42 +00:00
Rui Ueyama 1b0e68353d [PECOFF] Fix module definition file output
Previously we emit two or more identical definitions for an
exported symbol if the same /export option is given more than
once. This patch fixes that bug.

llvm-svn: 218433
2014-09-25 00:52:38 +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
Chad Rosier 073cbd47f3 [AArch64] Fix an incorrect PLT entry.
This patch is difficult to test in isolation, so a subsequent patch will test
further.

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

llvm-svn: 218418
2014-09-24 21:52:31 +00:00
Rui Ueyama 1c244f5db3 [PECOFF] Do not print @<ordinal> if the symbol is private
lib.exe prints a warning if a symbol in a module definition file has
both the PRIVATE attribute and an ordinal like this.

  EXPORTS
    foo @1 PRIVATE

This patch suppresses that.

llvm-svn: 218395
2014-09-24 17:51:51 +00:00
Rui Ueyama a370c3ee35 [PECOFF] Exported name should match C++ mangled name
Currently you can omit the leading underscore from exported
symbol name. LLD will look for mangled name for you. But it won't
look for C++ mangled name.

This patch is to support that.

If "sym" is specified to be exported, the linker looks for not
only "sym", but also "_sym" and "?sym@@<whatever>", so that you
can export a C++ function without decorating it.

llvm-svn: 218355
2014-09-24 02:01:10 +00:00
Rui Ueyama 359a47cee6 [PECOFF] Keep renamed undefined symbol name in export descriptor
Exported symbol name resolution is two-pass. In the first pass,
we try to resolve that as a regular undefined symbol. If it fails,
we look for mangled name for the symbol and rename the undefined
symbol and try again.

After all name resolution is done, we look for an atom for each
exported symbol again, to construct the export table. In this
process we try the regular names first, and then try mangled names.
But at this moment we should have knew which name is correct.

This patch is to keep the information we get in the first process
to use it later.

llvm-svn: 218354
2014-09-24 01:44:59 +00:00
Rui Ueyama de18f9657b [PECOFF] Keep unmangled name in the export table descriptor
The export table descriptor is a data structure to keep information
about the export table. It contains a symbol name, and the name may
or may not be mangled.

We need unmangled names for the export table, so we demangle them
before writing them to the export table.

Obviously this is not a correct round-trip conversion. That could
drop a leading underscore from a symbol because that's
indistinguishable from a mangled name.

What we need to do is to keep unmangled names. This patch does that.

llvm-svn: 218345
2014-09-24 00:55:15 +00:00
Rui Ueyama 251d9a34e2 [PECOFF] Simplify /machine option handling
/machine:ebc was previously recognized but rejected. Unknown architecture
names were handled differently but eventually rejected too. We don't need
to distinguish them.

llvm-svn: 218344
2014-09-24 00:21:45 +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
Rui Ueyama 7ea46bab26 [PECOFF] Fix duplicate /export options
If two or more /export options are given for the same symbol, we should
always print a warning message and use the first one regardless of other
parameters.
Previously there was a case that the first parameter is not used.

llvm-svn: 218342
2014-09-23 23:49:41 +00:00
Rui Ueyama a3eff3afd9 Make -core/-flavor options have higher priority than linker name
Also allows -core/flavor to appear at any position in the command line.
Patch from Oleg Ranevskyy!

http://reviews.llvm.org/D5384

llvm-svn: 218321
2014-09-23 18:09:38 +00:00
Rui Ueyama 117ef70c98 [PECOFF] Handle PRIVATE keyword in the module definition file
A symbol in a module definition file may be annotated with the
PRIVATE keyword like this.

  EXPORTS
    func PRIVATE

The PRIVATE keyword does not affect the resulting .dll file.
But it prevents the symbol to be listed in the .lib (import
library) file.

llvm-svn: 218273
2014-09-22 20:50:46 +00:00
Rui Ueyama 45f4d54c07 Re-commit r218259.
llvm-svn: 218272
2014-09-22 20:48:04 +00:00
Rui Ueyama 869c0019b1 Revert "[ELF] Fix linking when a regular object defines a symbol that is used in a DSO"
This commit reverts r218259 because it needed to be checked in with
a few binary files for the test.

llvm-svn: 218262
2014-09-22 18:08:34 +00:00
Rui Ueyama 508a007ae6 [ELF] Fix linking when a regular object defines a symbol that is used in a DSO
Patch from Rafael Auler!

When a shared lib has an undefined symbol that is defined in a regular object
(the program), the final executable must export this symbol in the dynamic
symbol table. However, in the current logic, lld only puts the symbol in the
dynamic symbol table if the symbol is weak. This patch fixes lld to put the
symbol in the dynamic symbol table regardless if it is weak or not.

This caused a problem in FreeBSD10, whose programs link against a crt1.o
that defines the symbol __progname, which is, in turn, undefined in libc.so.7
and will only be resolved in runtime.

http://reviews.llvm.org/D5424

llvm-svn: 218259
2014-09-22 17:52:50 +00:00
Yaron Keren 9682c8553e Modified per David Blakie suggestion.
llvm-svn: 218198
2014-09-21 05:07:44 +00:00
Yaron Keren 84f3816f85 Silence these C4715 warnings from Visual C++ (NFC)
llvm\tools\lld\lib\readerwriter\macho\macholinkingcontext.cpp(647):
warning C4715: 'lld::MachOLinkingContext::exportSymbolNamed' :
not all control paths return a value

llvm\tools\lld\lib\readerwriter\macho\machonormalizedfilefromatoms.cpp(723):
warning C4715: '`anonymous namespace'::Util::getSymbolTableRegion' :
not all control paths return a value

While all enum values do appear in the switch, an uninitialized or corrupted
enum variable would not be caught without the default: case in the switch.

llvm-svn: 218197
2014-09-21 04:13:45 +00:00
Rui Ueyama 44a7c7f1aa [PECOFF] Set ordinal to alias atoms
Atoms are ordered in the output file by ordinal. File has file ordinal,
and atom has atom ordinal which is unique within the file.
No two atoms should have the same combination of ordinals.

However that contract was not satisifed for alias atoms. Alias atom
is defined by /alternatename:sym1=sym2. In this case sym1 is defined
as an alias for sym2. sym1 always got ordinal 0.

As a result LLD failed with an assertion failure.

This patch assigns ordinal to alias atoms.

llvm-svn: 218158
2014-09-19 21:58:54 +00:00
Saleem Abdulrasool 752c9cb12f PECOFF: loosen another assumption of x86 only
Cache the machine type value of the linking context.  We need this in order to
calculate the virtual address of the atom when resolving function symbols.
Windows on ARM must check if the atom is a function and if so, set the Thumb bit
for the returned virtual address.  Failure to do so will result in an abnormal
exit due to a trap caused by invalid instruction decoding.  The same information
can be used to determine the relocation type that was previously being done via
is64 to select between x86 and x86_64.

llvm-svn: 218106
2014-09-19 06:09:33 +00:00
Saleem Abdulrasool c2c2937d55 Driver: accept /machine:arm for Windows linker
Accept /machine:arm as an argument.  This is changed to support ARM NT.
Although there is no way to differentiate between ARM (Windows CE) and ARM NT
(Windows on ARM), since LLVM currently only supports Windows on ARM, simply take
/machine:arm to mean Windows on ARM.

llvm-svn: 218105
2014-09-19 06:09:30 +00:00
Saleem Abdulrasool b9e9e0d5f3 PECOFF: loosen assumptions about x86-only targets
Rather than saving whether we are targeting 64-bit x86 (x86_64), simply convert
the single use of that information to the actual relocation type.  This will
permit the selection of non-x86 relocation types (e.g. for WoA support).

Inline the access of the machine type field as it is relatively cheap (a couple
of pointer dereferences) rather than storing the relocation type as a member
variable.

llvm-svn: 218104
2014-09-19 06:09:25 +00:00
Saleem Abdulrasool 42c7aab748 ReaderWriter: print magic in hex
When we encounter an unknown machine type, we print out the machine type magic.
However, we would print out the magic in decimal rather than hex.  Perform this
conversion to make it easier to identify what machine is unsupported.

llvm-svn: 218103
2014-09-19 06:09:18 +00:00
Rui Ueyama 68085fda00 [PECOFF] __tls_used is _tls_used on x64.
llvm-svn: 218090
2014-09-19 00:22:22 +00:00
Rui Ueyama 2ea8639696 Fixes wrong Twine uses in FileNode::errStr() and in LayoutPass.cpp
Patch from Rafael Auler!

llvm-svn: 218088
2014-09-18 23:21:39 +00:00
Rui Ueyama 1f684518c8 Fix buggy Twine storage in ELFLinkingContext::searchLibrary()
This patch fixes a forbidden use of Twine. It should only be used
as an intermediary value, but never stored.

This caused a bug in lld when running on Linux and compiled with
optimizations - it couldn't properly search libs.

Patch from Rafael Auler!

llvm-svn: 218083
2014-09-18 22:05:37 +00:00
Rui Ueyama 6bf091c656 [PECOFF] /safeseh:no on x64 is not an error
I made LLD to report an error if /safeseh:no option is given on x64,
but it turned out MSVC link.exe doesn't report error on it.
Removing the check.

llvm-svn: 218077
2014-09-18 21:18:05 +00:00
Rui Ueyama 9f1215b2d8 [PECOFF] Support TLS callbacks.
The contents from section .CRT$XLA to .CRT$XLZ is an array of function
pointers. They are called by the runtime when a new thread is created
or (gracefully) terminated.

You can make your own initialization function to be called by that
mechanism. All you have to do is:

- Define a pointer to a function in a .CRT$XL* section using pragma
- Make an external reference to "__tls_used" symbol

That technique is used in many projects. This patch is to support that.

What this patch does is to set the relative virtual address of
"__tls_used" to the PECOFF directory table. __tls_used is actually a
struct containing pointers to a symbol in .CRT$XLA and another symbol
in .CRT$XLZ. The runtime looks at the directory table, gets the address
of the struct, and call the function pointers between XLA and XLZ.

llvm-svn: 218007
2014-09-18 02:02:52 +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
David Majnemer 3588c6b198 Adjust lld to handle LLVM r217812
llvm-svn: 217815
2014-09-15 19:54:53 +00:00
Rui Ueyama f47c7fab71 Make anonymous namespace as small as possible.
LLVM coding style says that "static" is preferred for file-scope
functions.

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

llvm-svn: 217692
2014-09-12 17:30:13 +00:00
Ed Maste 933daef54f Add FreeBSD to system-linker-elf case
llvm-svn: 217672
2014-09-12 13:16:30 +00:00
Nick Kledzik ad0184056f [mach-o] support "0x" or "0X" as prefix on hex numbers on command line
This matches the strtoull() behavior in ld64.

llvm-svn: 217650
2014-09-12 00:16:29 +00:00