Commit Graph

9985 Commits

Author SHA1 Message Date
Rui Ueyama 971f87a806 Fix retpoline PLT header size for i386.
Differential Revision: https://reviews.llvm.org/D42397

llvm-svn: 323288
2018-01-24 00:26:57 +00:00
Rui Ueyama b9b1a522ea Remove trailing whitespace.
llvm-svn: 323287
2018-01-24 00:22:53 +00:00
Sam Clegg 7e8795fde4 [WebAssembly] Remove excess debugging. NFC.
llvm-svn: 323286
2018-01-24 00:15:53 +00:00
Sam Clegg 03bb127569 [WebAssembly] Add --relocatable test to test/wasm/weak-alias.ll. NFC.
There seems to be an bug related to table relocations not being
written correctly in this case.  This change is intended simply
to increase the coverage, not fix the issue.

llvm-svn: 323282
2018-01-23 23:36:10 +00:00
Peter Smith 5bb90135e6 [ELF] Make --fix-cortex-a53-843419 work on big endian hosts
The reinterpret cast to uint32_t to read the little-endian instructions 
will only work on a little endian system. Use ulittle32_t to always read
little-endian (AArch64 instructions are always little endian).

Fixes PR36056

Differential Revision: https://reviews.llvm.org/D42421

llvm-svn: 323243
2018-01-23 19:26:52 +00:00
Rafael Espindola 8e2fc4f3f8 Don't mark a shared library as needed because of a lazy symbol.
Fixes PR36029.

llvm-svn: 323221
2018-01-23 16:59:20 +00:00
Hans Wennborg b1d949fa0c Accept iso date format in COFF/unchanged-importlib.test
llvm-svn: 323203
2018-01-23 14:41:39 +00:00
Rafael Espindola 6b2b45020c Use 4 as the alignment of .eh_frame_hdr.
It includes 32 bit values and this matches both gold and bfd.

llvm-svn: 323172
2018-01-23 05:23:23 +00:00
Sam Clegg ab604a9882 [WebAssembly] Update to match llvm changes to TABLE relocations
TABLE relocations now store the function that is being refered
to indirectly.

See rL323165.

Also extend the call-indirect.ll a little.

Based on a patch by Nicholas Wilson!

llvm-svn: 323168
2018-01-23 01:25:56 +00:00
Bob Haarman 4ce341ffb6 [COFF] don't replace import library if contents are unchanged
Summary:
This detects when an import library is about to be overwritten with a
newly built one with the same contents, and keeps the old library
instead. The use case for this is to avoid needlessly rebuilding
targets that depend on the import library in build systems that rely
on timestamps to determine whether a target requires rebuilding.

This feature was requested in PR35917.

Reviewers: rnk, ruiu, zturner, pcc

Reviewed By: ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D42326

llvm-svn: 323164
2018-01-23 00:36:42 +00:00
Chandler Carruth c58f2166ab Introduce the "retpoline" x86 mitigation technique for variant #2 of the speculative execution vulnerabilities disclosed today, specifically identified by CVE-2017-5715, "Branch Target Injection", and is one of the two halves to Spectre..
Summary:
First, we need to explain the core of the vulnerability. Note that this
is a very incomplete description, please see the Project Zero blog post
for details:
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

The basis for branch target injection is to direct speculative execution
of the processor to some "gadget" of executable code by poisoning the
prediction of indirect branches with the address of that gadget. The
gadget in turn contains an operation that provides a side channel for
reading data. Most commonly, this will look like a load of secret data
followed by a branch on the loaded value and then a load of some
predictable cache line. The attacker then uses timing of the processors
cache to determine which direction the branch took *in the speculative
execution*, and in turn what one bit of the loaded value was. Due to the
nature of these timing side channels and the branch predictor on Intel
processors, this allows an attacker to leak data only accessible to
a privileged domain (like the kernel) back into an unprivileged domain.

The goal is simple: avoid generating code which contains an indirect
branch that could have its prediction poisoned by an attacker. In many
cases, the compiler can simply use directed conditional branches and
a small search tree. LLVM already has support for lowering switches in
this way and the first step of this patch is to disable jump-table
lowering of switches and introduce a pass to rewrite explicit indirectbr
sequences into a switch over integers.

However, there is no fully general alternative to indirect calls. We
introduce a new construct we call a "retpoline" to implement indirect
calls in a non-speculatable way. It can be thought of loosely as
a trampoline for indirect calls which uses the RET instruction on x86.
Further, we arrange for a specific call->ret sequence which ensures the
processor predicts the return to go to a controlled, known location. The
retpoline then "smashes" the return address pushed onto the stack by the
call with the desired target of the original indirect call. The result
is a predicted return to the next instruction after a call (which can be
used to trap speculative execution within an infinite loop) and an
actual indirect branch to an arbitrary address.

On 64-bit x86 ABIs, this is especially easily done in the compiler by
using a guaranteed scratch register to pass the target into this device.
For 32-bit ABIs there isn't a guaranteed scratch register and so several
different retpoline variants are introduced to use a scratch register if
one is available in the calling convention and to otherwise use direct
stack push/pop sequences to pass the target address.

This "retpoline" mitigation is fully described in the following blog
post: https://support.google.com/faqs/answer/7625886

We also support a target feature that disables emission of the retpoline
thunk by the compiler to allow for custom thunks if users want them.
These are particularly useful in environments like kernels that
routinely do hot-patching on boot and want to hot-patch their thunk to
different code sequences. They can write this custom thunk and use
`-mretpoline-external-thunk` *in addition* to `-mretpoline`. In this
case, on x86-64 thu thunk names must be:
```
  __llvm_external_retpoline_r11
```
or on 32-bit:
```
  __llvm_external_retpoline_eax
  __llvm_external_retpoline_ecx
  __llvm_external_retpoline_edx
  __llvm_external_retpoline_push
```
And the target of the retpoline is passed in the named register, or in
the case of the `push` suffix on the top of the stack via a `pushl`
instruction.

There is one other important source of indirect branches in x86 ELF
binaries: the PLT. These patches also include support for LLD to
generate PLT entries that perform a retpoline-style indirection.

The only other indirect branches remaining that we are aware of are from
precompiled runtimes (such as crt0.o and similar). The ones we have
found are not really attackable, and so we have not focused on them
here, but eventually these runtimes should also be replicated for
retpoline-ed configurations for completeness.

For kernels or other freestanding or fully static executables, the
compiler switch `-mretpoline` is sufficient to fully mitigate this
particular attack. For dynamic executables, you must compile *all*
libraries with `-mretpoline` and additionally link the dynamic
executable and all shared libraries with LLD and pass `-z retpolineplt`
(or use similar functionality from some other linker). We strongly
recommend also using `-z now` as non-lazy binding allows the
retpoline-mitigated PLT to be substantially smaller.

When manually apply similar transformations to `-mretpoline` to the
Linux kernel we observed very small performance hits to applications
running typical workloads, and relatively minor hits (approximately 2%)
even for extremely syscall-heavy applications. This is largely due to
the small number of indirect branches that occur in performance
sensitive paths of the kernel.

When using these patches on statically linked applications, especially
C++ applications, you should expect to see a much more dramatic
performance hit. For microbenchmarks that are switch, indirect-, or
virtual-call heavy we have seen overheads ranging from 10% to 50%.

However, real-world workloads exhibit substantially lower performance
impact. Notably, techniques such as PGO and ThinLTO dramatically reduce
the impact of hot indirect calls (by speculatively promoting them to
direct calls) and allow optimized search trees to be used to lower
switches. If you need to deploy these techniques in C++ applications, we
*strongly* recommend that you ensure all hot call targets are statically
linked (avoiding PLT indirection) and use both PGO and ThinLTO. Well
tuned servers using all of these techniques saw 5% - 10% overhead from
the use of retpoline.

We will add detailed documentation covering these components in
subsequent patches, but wanted to make the core functionality available
as soon as possible. Happy for more code review, but we'd really like to
get these patches landed and backported ASAP for obvious reasons. We're
planning to backport this to both 6.0 and 5.0 release streams and get
a 5.0 release with just this cherry picked ASAP for distros and vendors.

This patch is the work of a number of people over the past month: Eric, Reid,
Rui, and myself. I'm mailing it out as a single commit due to the time
sensitive nature of landing this and the need to backport it. Huge thanks to
everyone who helped out here, and everyone at Intel who helped out in
discussions about how to craft this. Also, credit goes to Paul Turner (at
Google, but not an LLVM contributor) for much of the underlying retpoline
design.

Reviewers: echristo, rnk, ruiu, craig.topper, DavidKreitzer

Subscribers: sanjoy, emaste, mcrosier, mgorny, mehdi_amini, hiraditya, llvm-commits

Differential Revision: https://reviews.llvm.org/D41723

llvm-svn: 323155
2018-01-22 22:05:25 +00:00
Sam Clegg ff2b12216b [WebAssembly] Remove --emit-relocs
This was added to mimic ELF, but maintaining it has cost
and we currently don't have any use for it outside of the
test code.

Differential Revision: https://reviews.llvm.org/D42324

llvm-svn: 323154
2018-01-22 21:55:43 +00:00
James Henderson 0018ca8462 Add test for interaction of --gc-sections and undefined references
It is possible for a link to fail with an undefined reference, unless
--gc-sections is specified, removing the reference in the process. This
doesn't look to be tested anywhere explicitly, so I thought it useful
to add a test for it to ensure the behaviour is maintained.

Reviewers: ruiu

Differential Revision: https://reviews.llvm.org/D42299

llvm-svn: 323099
2018-01-22 10:58:06 +00:00
Martin Storsjo 3b611fa93f [COFF] Keep the underscore on exported decorated stdcall functions in MSVC mode
This fixes PR35733.

Differential Revision: https://reviews.llvm.org/D41632

llvm-svn: 323036
2018-01-20 11:44:42 +00:00
Sam Clegg 0f0a428738 [WebAssembly] Remove special handling of entry point export.
Its much easier to export it via setHidden(false), now that
that is a thing.

As a side effect the start function is not longer always exports first
(becuase its being exported just like all the other function).

Differential Revision: https://reviews.llvm.org/D42321

llvm-svn: 323025
2018-01-20 01:44:45 +00:00
Sam Clegg 096aa50ba4 [WebAssembly] Remove redundant function: addSyntheticUndefinedFunction. NFC.
Differential Revision: https://reviews.llvm.org/D42327

llvm-svn: 323024
2018-01-20 01:40:17 +00:00
Sam Clegg 77ee17d191 [WebAssembly] Remove custom handling for undefined entry
This code was needed back when we were not able to write
out the synthetic symbol for main.

Add tests to make sure we can handle this now.

Differential Revision: https://reviews.llvm.org/D42322

llvm-svn: 323020
2018-01-20 00:52:51 +00:00
Rui Ueyama 2f8af79927 Avoid divisions.
Compiler doesn't know the fact that Config->WordSize * 8 is always a
power of two, so it had to use the div instruction to divide some
number with C.

llvm-svn: 323014
2018-01-20 00:14:16 +00:00
Rui Ueyama 517366c7e0 Make the bloom filter a bit larger.
I created https://reviews.llvm.org/D42202 to see how large the bloom
filter should be. With that patch, I tested various bloom filter sizes
with the following commands:

  $ cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_LLD=true \
    -DLLVM_ENABLE_PROJECTS='clang;lld' -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_SHARED_LINKER_FLAGS=-Wl,-bloom-filter-bits=<some integer> \
    ../llvm-project/llvm
  $ rm -f $(find . -name \*.so.7.0.0svn)
  $ ninja lld
  $ LD_BIND_NOW=1 perf stat bin/ld.lld

Here is the result:

  -bloom-filter-bits=8   0.220351609 seconds
  -bloom-filter-bits=10  0.217146597 seconds
  -bloom-filter-bits=12  0.206870826 seconds
  -bloom-filter-bits=16  0.209456312 seconds
  -bloom-filter-bits=32  0.195092075 seconds

Currently we allocate 8 bits for a symbol, but according to the above
result, that number is not optimal. Even though the numbers follow the
diminishing return rule, the point where a marginal improvement becomes
too small is not -bloom-filter-bits=8 but 12. So this patch sets it to 12.

Differential Revision: https://reviews.llvm.org/D42204

llvm-svn: 323010
2018-01-19 23:54:31 +00:00
Sam Clegg 04b76f4077 [WebAssembly] Include SYMBOL_INFO for imports as well as exports
Only effects --emit-relocs/--relocatable

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42306

llvm-svn: 322994
2018-01-19 21:49:41 +00:00
Sam Clegg 729a864dfa [WebAssembly] Include weak imports when linking with --relocatable
We need these import since relocations are generated against them.

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42305

llvm-svn: 322990
2018-01-19 20:56:20 +00:00
Sam Clegg 272c70179a [WebAssembly] Allow non-zero table offset in input object
Summary: This change enables D42284 to land without breaking lld

Subscribers: jfb, dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

Differential Revision: https://reviews.llvm.org/D42285

llvm-svn: 322976
2018-01-19 18:21:42 +00:00
Benjamin Kramer 3f47fcf102 [ELF] Keep tests from wrinting to the test directory.
llvm-svn: 322943
2018-01-19 14:15:13 +00:00
Sam Clegg 811236c36b [WebAssembly] Fix variable whose name is also a type name
Some compilers don't like this and generate a warning.

llvm-svn: 322921
2018-01-19 03:31:07 +00:00
Sam Clegg 14ae6e7c5c [WebAssembly] Export the stack pointer when using --emit-relocs
This solves the problem that --emit-relocs needs the stack-pointer
to be exported, in order to write out any relocations that reference
the __stack_pointer symbol by its symbol index.

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42237

llvm-svn: 322911
2018-01-18 23:57:55 +00:00
Rui Ueyama 4b7cf1601a Add a missing file header.
llvm-svn: 322909
2018-01-18 23:46:28 +00:00
Sam Clegg d3052d5522 [WebAssembly] Add missing function exports and SYM_INFO to --relocatable output
When writing relocatable files we were exporting for all globals
(including file-local syms), but not for functions. Oops. To be
consistent with non-relocatable output, all symbols (file-local
and global) should be exported. Any symbol targetted by further
relocations needs to be exported. The lack of local function
exports was just an omission, I think.

Second bug: Local symbol names can collide, causing an illegal
Wasm file to be generated! Oops again. This only previously affected
producing relocatable output from two files, where each had a global
with the same name. We need to "budge" the symbol names for locals
that are exported on relocatable output.

Third bug: LLD's relocatable output wasn't writing out any symbol
flags! Thus the local globals weren't being marked as local, and
the hidden flag was also stripped...

Added tests to exercise colliding local names with/without
relocatable flag

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42105

llvm-svn: 322908
2018-01-18 23:40:49 +00:00
Zachary Turner 1bc2ce6b9b Speed up iteration of CodeView record streams.
There's some abstraction overhead in the underlying
mechanisms that were being used, and it was leading to an
abundance of small but not-free copies being made.  This
showed up on a profile.  Eliminating this and going back to
a low-level byte-based implementation speeds up lld with
/DEBUG between 10 and 15%.

Differential Revision: https://reviews.llvm.org/D42148

llvm-svn: 322871
2018-01-18 18:35:01 +00:00
Simon Atanasyan 712cd41fa0 [ELF][MIPS] Rename function. NFC
llvm-svn: 322861
2018-01-18 15:59:10 +00:00
Simon Atanasyan ceddcdf01c [ELF][MIPS] Decompose relocation type for N32 / N64 earlier. NFC
We need to decompose relocation type for N32 / N64 ABI. Let's do it
before any other manipulations with relocation type in the `relocateOne`
routine.

llvm-svn: 322860
2018-01-18 15:59:05 +00:00
Rafael Espindola b8bf8f2129 Add a lld test for a llvm fix.
This is tested in llvm, but it seems reasonable to have a small
integration test in lld.

llvm-svn: 322815
2018-01-18 05:40:43 +00:00
Rafael Espindola 2e4d7026dc Update for llvm change.
llvm-svn: 322807
2018-01-18 02:08:32 +00:00
Rafael Espindola 5e9c77624c Handle parsing AT(ADDR(.foo-bar)).
The problem we had with it is that anything inside an AT is an
expression, so we failed to parse the section name because of the - in
it.

llvm-svn: 322801
2018-01-18 01:14:57 +00:00
Sam Clegg 1963d71cb8 [WebAssembly] Simplify generation of "names" section
Simplify generation of "names" section by simply iterating
over the DefinedFunctions array.

This even fixes some bugs, judging by the test changes required.
Some tests are asserting that functions are named multiple times,
other tests are asserting that the "names" section contains the
function's alias rather than its original name

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42076

llvm-svn: 322751
2018-01-17 20:19:04 +00:00
Sam Clegg 48d030d5c7 [WebAssembly] Remove DEBUG_FUNCTION_NAME after llvm change
Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42193

llvm-svn: 322745
2018-01-17 19:35:16 +00:00
Zachary Turner 727f153b6f [coff] Print detailed timing information with /TIME.
The classes used to print and update time information are in
common, so other linkers could use this as well if desired.

Differential Revision: https://reviews.llvm.org/D41915

llvm-svn: 322736
2018-01-17 19:16:26 +00:00
Sam Clegg bd2bd5a92e [WebAssembly] Remove non-existent method declaration. NFC.
Patch by Nicholas Wilson!

llvm-svn: 322732
2018-01-17 19:01:52 +00:00
Sam Clegg 4e07ecb5ab [WebAssembly] Remove unused member variable. NFC.
Patch by Nicholas Wilson!

llvm-svn: 322731
2018-01-17 18:58:50 +00:00
Sam Clegg 54c3891328 [WebAssembly] Fix typo in comment
Patch by Nicholas Wilson!

llvm-svn: 322727
2018-01-17 18:50:30 +00:00
Sam Clegg fc0723c73f [WebAssembly] Refactor InputChunk.getSize(). NFC
Also, remove trailing semicolons.

Patch by Nicholas Wilson!

llvm-svn: 322726
2018-01-17 18:49:11 +00:00
George Rimar 0b89c55aea [ELF] - Stop mixing order of -defsym/-script commands.
Previously we always handled -defsym after other commands in command line.
That made impossible to overload values set by -defsym from linker script:

 test.script:            
  foo = 0x22;
-defsym=foo=0x11 -script t.script
would always set foo to 0x11.

That is inconstent with common logic which allows to override command line
options. it is inconsistent with bfd behavior and seems breaks assumption that
-defsym is the same as linker script assignment, as -defsyms always handled out of
command line order.

Patch fixes the handling order.

Differential revision: https://reviews.llvm.org/D42054

llvm-svn: 322625
2018-01-17 10:24:49 +00:00
Sam Clegg 51bcdc2d49 [WebAssembly] Define __heap_base global
This is an immutable exported global representing
the start of the heap area.  It is a page aligned.

Differential Revision: https://reviews.llvm.org/D42030

llvm-svn: 322609
2018-01-17 01:34:31 +00:00
Sam Clegg 5a11dd9690 [WebAssembly] Create synthetic __dso_handle symbol
This is used by __cxa_ataxit to determine the currently
executing DLL.  Once we fully support DLLs this will need
to be set to some address within the DLL.

The ELF linker added support for this symbol here:
https://reviews.llvm.org/D33856

Differential Revision: https://reviews.llvm.org/D42024

llvm-svn: 322606
2018-01-17 01:10:28 +00:00
Rui Ueyama 2c95e798a0 [LLD][COFF] Report error when file will exceed Windows maximum image size (4GB)
Patch by Colden Cullen.

Currently, when a large PE (>4 GiB) is to be produced, a crash occurs
because:

1. Calling setOffset with a number greater than UINT32_MAX causes the
   PointerToRawData to overflow

2. When adding the symbol table to the end of the file, the last section's
   offset was used to calculate file size. Because this had overflowed,
   this number was too low, and the file created would not be large enough.
   This lead to the actual crash I saw, which was a buffer overrun.

This change:

1. Adds comment to setOffset, clarifying that overflow can occur, but it's
   somewhat safe because the error will be handled elsewhere

2. Adds file size check after all output data has been created This matches
   the MS link.exe error, which looks prints as: "LINK : fatal error
   LNK1248: image size (10000EFC9) exceeds maximum allowable size
   (FFFFFFFF)"

3. Changes calculate of the symbol table offset to just use the existing
   FileSize. This should match the previous calculations, but doesn't rely
   on the use of a u32 that can overflow.

4. Removes trivial usage of a magic number that bugged me while I was
   debugging the issue

I'm not sure how to add a test for this outside of adding 4GB of object
files to the repo. If there's an easier way, let me know and I'll be
happy to add a test.

Differential Revision: https://reviews.llvm.org/D42010

llvm-svn: 322605
2018-01-17 01:08:02 +00:00
Rafael Espindola 62003fbb02 Inline foot gun into only valid use.
Symbol had both Visibility and getVisibility() and they had different
meanings. That is just too easy to get wrong.

getVisibility() would compute the visibility of a particular symbol
(foo in bar.o), and Visibility stores the computed value we will put
in the output.

There is only one case when we want what getVisibility() provides, so
inline it.

llvm-svn: 322590
2018-01-16 19:28:28 +00:00
Rafael Espindola 7e6aeb614c Fix another case we used the wrong visibility.
In here too we want the computed output visibility.

llvm-svn: 322586
2018-01-16 19:02:46 +00:00
Rafael Espindola d3ec3e5684 Add an extra test. NFC.
Without this all test would pass if the visibility checks were removed
from SymbolTable::addShared and SymbolTable::addUndefined.

llvm-svn: 322583
2018-01-16 18:53:09 +00:00
Rafael Espindola 3c3544652b Fix another case we were using the wrong visibility.
llvm-svn: 322580
2018-01-16 18:21:23 +00:00
Rafael Espindola 37e4e695e9 Use the combined visibility when computing dso_local.
We track both the combined visibility that will be used for the output
symbol and the original input visibility of the selected symbol.

Almost everything should use the computed visibility.

I will make the names less confusing an a followup patch.

llvm-svn: 322576
2018-01-16 17:34:26 +00:00
Rafael Espindola c6df38c985 Set dso_local in lld.
We were already doing this in gold, but not in lld.

llvm-svn: 322572
2018-01-16 16:49:05 +00:00