Commit Graph

321 Commits

Author SHA1 Message Date
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 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
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
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
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
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
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
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
Simon Atanasyan bcb865a8ce [Mips] Replace assembler code by YAML to make the 'entry-name.test' test target
independent.

llvm-svn: 213690
2014-07-22 21:47:34 +00:00
Simon Atanasyan 09f45ca39b [Mips] Replace assembler code by YAML to make the 'dynlib-fileheader.test'
test target independent.

llvm-svn: 213480
2014-07-20 20:03:46 +00:00
Simon Atanasyan 5ecadda642 [Mips] Replace assembler code by YAML to make the test 'dynlib-dynamic.test'
target independent.

llvm-svn: 213462
2014-07-19 20:18:46 +00:00
Simon Atanasyan f7ce3ea024 [Mips] Replace assembler code by YAML to make the test 'base-address.test'
target independent.

llvm-svn: 213390
2014-07-18 17:23:21 +00:00
Simon Atanasyan 64c0ac2b35 [ELF] Implement parsing `-l` prefixed items in the `GROUP` linker script command.
There are two forms of `-l` prefixed expression:

* -l<libname>
* -l:<filename>

In the first case a linker should construct a full library name
`lib + libname + .[so|a]` and search this library as usual. In the second case
a linker should use the `<filename>` as is and search this file through library
search directories.

The patch reviewed by Shankar Easwaran.

llvm-svn: 213077
2014-07-15 17:17:30 +00:00
Simon Atanasyan 35ad71adad [Mips] Fix test case expectations due the latest changes in the LLVM.
llvm-svn: 212949
2014-07-14 15:31:47 +00:00
Simon Atanasyan 355f7ed602 [Mips] Remove unused test input file.
llvm-svn: 212927
2014-07-14 08:54:40 +00:00
Simon Atanasyan c97126b1c9 [Mips] Make rel-dynamic-11.test test case independent from external input files.
llvm-svn: 212813
2014-07-11 13:42:04 +00:00
Simon Atanasyan d092f0e64a [Mips] Make rel-dynamic-10.test test case independent from external input files.
llvm-svn: 212758
2014-07-10 19:47:23 +00:00
Simon Atanasyan 0acd5447b4 [Mips] Make rel-dynamic-08.test test case independent from external input files.
llvm-svn: 212613
2014-07-09 12:31:11 +00:00
Nick Kledzik 2552a09978 Change llvm-nm to use -M instead of -s to work with latest llvm-nm
llvm-svn: 212579
2014-07-09 00:25:52 +00:00
Simon Atanasyan e693e8f8f0 [Mips] Make rel-dynamic-07.test test case independent from external input files.
llvm-svn: 212542
2014-07-08 15:47:55 +00:00
Simon Atanasyan 49eafe909b [Mips] Make rel-dynamic-05.test test case independent from external input files.
llvm-svn: 212480
2014-07-07 20:14:49 +00:00
Simon Atanasyan 611b7a11b5 [Mips] Make rel-dynamic-04.test test case independent from external input files.
llvm-svn: 212409
2014-07-06 17:44:40 +00:00
Simon Atanasyan 65e2464667 [Driver] Improve the `isPathUnderSysroot()` function. Now it returns a
correct result even if checking paths: a) symlinks and/or b) contains relative
parts like /dir1/dir2/../dir2.

llvm-svn: 211772
2014-06-26 10:48:52 +00:00
Simon Atanasyan d449fc8671 [ELF] Add two new virtual functions to the `OutputELFWriter` class to control
dynamic symbol table populating and DT_NEEDED tag creation.

The `isDynSymEntryRequired` function returns true if the specified shared
library atom requires a dynamic symbol table entry. The `isNeededTagRequired`
function returns true if we need to create DT_NEEDED tag for the shared
library defined specified shared atom.

By default the both functions return true. So there is no functional changes
for all targets except MIPS. Probably we need to spread the same modifications
on other ELF targets but I want to implement and fully tested complete set of
changes for MIPS target first.

For MIPS we create a dynamic symbol table entry for a shared library atom iif
this atom is referenced by a regular defined atom. For example, if library L1
defines symbol T1, library L2 defines symbol T2 and uses symbol T1
and executable file E1 uses symbol T2 but does not use symbol T1 we create
an entry in the E1 dynamic symbol table for symbol T2 and do not create
an entry for T1.

The patch creates DT_NEEDED tags for shared libraries contain shared library
atoms which a) referenced by regular defined atoms; b) have corresponding
copy dynamic relocations (R_MIPS_COPY).

Now the patch does not take in account --as-needed / --no-as-needed command
line options. So it is too restrictive and create DT_NEEDED tags for really
needed shared libraries only. I plan to fix that by subsequent patches.

llvm-svn: 211674
2014-06-25 07:55:55 +00:00
Simon Atanasyan 3b8c86b985 [Mips] Make rel-dynamic-03.test test case independent from external input files.
llvm-svn: 211491
2014-06-23 12:28:11 +00:00
Simon Atanasyan b1032ad260 [Mips] Make rel-dynamic-01.test test case independent from external input files.
llvm-svn: 211463
2014-06-22 11:58:08 +00:00
Simon Atanasyan e8a2875774 [Mips] Make rel-copy.test test case independent from external input files.
llvm-svn: 211355
2014-06-20 12:54:06 +00:00
Simon Atanasyan 825c5992d8 [Mips] Make r26-1.test test case independent from external input files.
llvm-svn: 211263
2014-06-19 10:18:58 +00:00
Simon Atanasyan e5b03cb790 [Mips] Make plt-header.test test case independent from external input files.
llvm-svn: 211099
2014-06-17 13:08:11 +00:00
Simon Atanasyan 0469dbd43f [Mips] Make hilo16-5.test test case independent from external input files.
llvm-svn: 211017
2014-06-16 11:54:42 +00:00
Simon Atanasyan 004c49b017 [Mips] Make gp-sym-2.test test case independent from external input files.
llvm-svn: 210987
2014-06-15 12:04:40 +00:00
Simon Atanasyan 2e67be3cc2 [Mips] Make gp-sym-1.test test case independent from external input files.
llvm-svn: 210981
2014-06-14 21:18:50 +00:00
Simon Atanasyan 4156ac2ecd [Mips] Make exe-got.test test case independent from external input files.
llvm-svn: 210977
2014-06-14 12:55:03 +00:00
Simon Atanasyan f0f8207995 [Mips] Make exe-fileheader.test test case independent from external input files.
llvm-svn: 210893
2014-06-13 11:44:23 +00:00
Simon Atanasyan 033ecd1cc8 [Mips] Make exe-dynamic.test test case independent from external input files.
llvm-svn: 210796
2014-06-12 16:46:47 +00:00
Rui Ueyama 8ab4e2ce25 group-cmd-search test is not supposed to pass on Windows.
llvm-svn: 210697
2014-06-11 20:00:22 +00:00
Simon Atanasyan 87a06716e6 [Mips] Fix the bug -- symbol referred by the R_MIPS_TLS_GD relocation
does not get an entry in the dynamic symbol table.

llvm-svn: 210648
2014-06-11 10:08:08 +00:00
Simon Atanasyan 1de1d63aec [Mips] Make dt-textrel.test test case independent from external input files.
llvm-svn: 210469
2014-06-09 13:45:58 +00:00
Rui Ueyama c97b661475 Re-commit r210425.
llvm-svn: 210456
2014-06-09 08:57:37 +00:00
Rui Ueyama 2ad8c22a55 Revert "[Mips] Make got16.test test case independent from external input files."
This reverts commit r210425 because the test added in the commit
is broken.

llvm-svn: 210451
2014-06-09 08:23:58 +00:00
Simon Atanasyan 8804e5cb49 [Mips] Make got16.test test case independent from external input files.
llvm-svn: 210425
2014-06-08 11:33:05 +00:00
Simon Atanasyan c8e86fb032 [Mips] Handle Mips TLS relocations R_MIPS_TLS_GOTTPREL / R_MIPS_TLS_GD / R_MIPS_TLS_LDM etc.
llvm-svn: 210394
2014-06-07 13:20:53 +00:00
Simon Atanasyan 8065fb761e [Mips] Make dynlib-dynsym.test self contained.
llvm-svn: 210258
2014-06-05 12:39:12 +00:00