Commit Graph

532 Commits

Author SHA1 Message Date
Sam Clegg
e52881a287 [lld][WebAssembly] Split __wasm_apply_relocs function in two
We have two types of relocations that we apply on startup:
1. Relocations that apply to wasm globals
2. Relocations that apply to wasm memory

The first set of relocations use only the `__memory_base` import to
update a set of internal globals.  Because wasm globals are thread local
these need to run on each thread.  Memory relocations, like static
constructors, must only be run once.

To ensure global relocations run on all threads and because the only
depend on the immutable `__memory_base` import we can run them during
the WebAssembly start functions, instead of waiting until the
post-instantiation __wasm_call_ctors.

Differential Revision: https://reviews.llvm.org/D93066
2020-12-10 17:07:39 -08:00
Sam Clegg
199497086e [lld][WebAssembly] Delay creation of internal __wasm_memory_init function
This also allows for its creation to be conditional so it is completely
elided when not needed.

Differential Revision: https://reviews.llvm.org/D93035
2020-12-10 10:47:18 -08:00
Sam Clegg
d8ed639a6a [lld][WebAssembly] Don't emit names for data segments that we omit
Followup to https://reviews.llvm.org/D92909

Differential Revision: https://reviews.llvm.org/D92997
2020-12-09 20:59:26 -08:00
Sam Clegg
9a72d3e3e4 [WebAssembly] Add support for named data sections in wasm binaries
Followup to https://reviews.llvm.org/D91769 which added support
for names globals.

Differential Revision: https://reviews.llvm.org/D92909
2020-12-09 12:57:07 -08:00
Fangrui Song
4701cb41ed [lld] Delete unused declarations
Notes:

* runMSVCLinker: remnant of r338615
* wasm markSymbol: remnant of r374275
* wasm addDataAddressGlobal: accidentally added by r372779
* MachO Writer::createSymtabContents: accidentally added by D76839
2020-12-06 15:26:37 -08:00
Sam Clegg
ab58e4cb51 [lld][WebAssembly] Add suppport for PIC + passive data initialization
This change improves our support for shared memory to include
PIC executables (and shared libraries).

To handle this case the linker-generated `__wasm_init_memory`
function (that only exists in shared memory builds) must be
capable of loading memory segements at non-const offsets based
on the runtime value of `__memory_base`.

Differential Revision: https://reviews.llvm.org/D92620
2020-12-04 17:28:23 -08:00
Sam Clegg
1bb79875e4 [lld][WebAssembly] Set memory limits correctly for PIC + shared memory
Don't early return from layoutMemory in PIC mode before we have set the
memory limits.

This matters in particular with shared-memory + PIC because shared
memories require maximum size.

Secondly, when we need a maximum, but the user does not supply one,
default to MAX_INT rather than 0 (defaulting to zero is completely
useless and means that building with -shared didn't previously work at
all without --maximum-memory, because zero is never big enough).

This is part of an ongoing effort to enable dynamic linking with
threads in emscripten.

See https://github.com/emscripten-core/emscripten/issues/3494

Differential Revision: https://reviews.llvm.org/D92528
2020-12-03 18:14:28 -08:00
Wouter van Oortmerssen
fd65e4815c [WebAssembly] Fixed Writer::createInitMemoryFunction to work for wasm64
Differential Revision: https://reviews.llvm.org/D92348
2020-12-03 16:20:55 -08:00
Sam Clegg
701fa0b5ab [lld][WebAssembly] Fix malformed output with -pie + --shared-memory
The conditional guarding createInitMemoryFunction was incorrect and
didn't match that guarding the creation of the associated symbol.

Rather that reproduce the same conditions in multiple places we can
simply use the presence of the associated symbol.

Also, add an assertion that would have caught this bug.

Also, add a new test for this flag combination.

This is part of an ongoing effort to enable dynamic linking with
threads in emscripten.

See https://github.com/emscripten-core/emscripten/issues/3494

Differential Revision: https://reviews.llvm.org/D92520
2020-12-03 11:06:07 -08:00
Eric Leese
8b8088ac6c [lld] Use -1 as tombstone value for discarded code ranges
Under existing behavior discarded functions are relocated to have the start pc
0. This causes problems when debugging as they typically overlap the first
function and lldb symbol resolution frequently chooses a discarded function
instead of the correct one. Using the value -1 or -2 (depending on which DWARF
section we are writing) is sufficient to prevent lldb from resolving to these
symbols.

Reviewed By: MaskRay, yurydelendik, sbc100

Differential Revision: https://reviews.llvm.org/D91803
2020-12-01 17:06:32 -08:00
Fangrui Song
31e03a9bd9 [WebAssembly] Rename --lto-no-new-pass-manager to --no-lto-new-pass-manager
In addition, disallow `-lto-new-pass-manager` (see D79371).

Note: the ELF port has also adopted --no-lto-new-pass-manager

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D92422
2020-12-01 16:52:37 -08:00
Sam Clegg
a38ed62ea8 [lld][WebAssembly] Feedback from D92038. NFC
Differential Revision: https://reviews.llvm.org/D92429
2020-12-01 14:53:59 -08:00
Arthur Eubanks
1314a4938f [LTO][wasm][NewPM] Allow using new pass manager for wasm LTO
Reviewed By: sbc100

Differential Revision: https://reviews.llvm.org/D92150
2020-12-01 12:22:40 -08:00
Sam Clegg
48ddf5e182 [lld][WebAssembly] Ensure stub symbols always get address 0
Without this extra flag we can't distingish between stub functions and
functions that happen to have address 0 (relative to __table_base).

Adding this flag bit the base symbol class actually avoids growing the
SymbolUnion struct which would not be true if we added it to the
FunctionSymbol subclass (due to bitbacking).

The previous approach of setting it's table index to zero worked for
normal static relocations but not for `-fPIC` code.

See https://github.com/emscripten-core/emscripten/issues/12819

Differential Revision: https://reviews.llvm.org/D92038
2020-11-25 18:26:34 -08:00
Andy Wingo
1933c9d41a [WebAssembly] Factor out WasmTableType in binary format
This commit factors out a WasmTableType definition from WasmTable, as is
the case for WasmGlobal and other data types.  Also add support for
extracting the SymbolName for a table from the linking section's symbol
table.

Differential Revision: https://reviews.llvm.org/D91849
2020-11-25 08:00:08 -08:00
Sam Clegg
1827005cfc [WebAssembly] Add support for named globals in the object format.
Differential Revision: https://reviews.llvm.org/D91769
2020-11-19 00:17:22 -08:00
Sam Clegg
206884bf90 [lld][WebAssembly] Implement --unresolved-symbols
This is a more full featured version of ``--allow-undefined``.
The semantics of the different methods are as follows:

report-all:

   Report all unresolved symbols.  This is the default.  Normally the
   linker will generate an error message for each reported unresolved
   symbol but the option ``--warn-unresolved-symbols`` can change this
   to a warning.

ignore-all:

   Resolve all undefined symbols to zero.  For data and function
   addresses this is trivial.  For direct function calls, the linker
   will generate a trapping stub function in place of the undefined
   function.

import-functions:

   Generate WebAssembly imports for any undefined functions.  Undefined
   data symbols are resolved to zero as in `ignore-all`.  This
   corresponds to the legacy ``--allow-undefined`` flag.

The plan is to followup with a new mode called `import-dynamic` which
allows for statically linked binaries to refer to both data and
functions symbols from the embedder.

Differential Revision: https://reviews.llvm.org/D79248
2020-11-17 16:27:06 -08:00
Nico Weber
baa2aa28f5 lld: Add --color-diagnostic to MachO port, harmonize others
This adds `--[no-]color-diagnostics[=auto,never,always]` to
the MachO port and harmonizes the flag in the other ports:
- Consistently use MetaVarName
- Consistently document the non-eq version as alias of the eq version
- Use B<> in the ports that have it (no-op, shorter)
- Fix oversight in COFF port that made the --no flag have the wrong
  prefix

Differential Revision: https://reviews.llvm.org/D91640
2020-11-17 12:58:30 -05:00
Wouter van Oortmerssen
16f02431dc [WebAssembly] Added R_WASM_FUNCTION_OFFSET_I64 for use with DWARF DW_AT_low_pc
Needed for wasm64, see discussion in https://reviews.llvm.org/D91203

Differential Revision: https://reviews.llvm.org/D91395
2020-11-13 09:32:31 -08:00
Sam Clegg
a28a466210 [WebAssembly] Add new relocation type for TLS data symbols
These relocations represent offsets from the __tls_base symbol.

Previously we were just using normal MEMORY_ADDR relocations and relying
on the linker to select a segment-offset rather and absolute value in
Symbol::getVirtualAddress().  Using an explicit relocation type allows
allow us to clearly distinguish absolute from relative relocations based
on the relocation information alone.

One place this is useful is being able to reject absolute relocation in
the PIC case, but still accept TLS relocations.

Differential Revision: https://reviews.llvm.org/D91276
2020-11-13 07:59:29 -08:00
Sam Clegg
29a3056bb5 [lld][WebAssembly] Allow references to __tls_base without shared memory
Previously we limited the use of atomics and TLS to programs
linked with `--shared-memory`.

However, as of https://reviews.llvm.org/D79530 we now allow
programs that use atomic to be linked without `--shared-memory`.
For this to be useful we also want to all TLS usage in such
programs.  In this case, since we know we are single threaded
we simply include the TLS data as a regular active segment
and create an immutable `__tls_base` global that point to the
start of this segment.

Fixes: https://github.com/emscripten-core/emscripten/issues/12489

Differential Revision: https://reviews.llvm.org/D91115
2020-11-10 17:58:06 -08:00
Sam Clegg
9d1409df87 [lld][WebAssembly] Give better warnings on bad relocation sites
Differential Revision: https://reviews.llvm.org/D90443
2020-10-30 10:11:04 -07:00
Wouter van Oortmerssen
b8c2d60df5 [WebAssembly] Improved LLD error messages in case of mixed wasm32/wasm64 object files
Differential Revision: https://reviews.llvm.org/D90428
2020-10-29 17:15:59 -07:00
Sam Clegg
84129150ce [lld][WebAssembly] Fix memory size in dylink section for -pie exectuables
This field to represents the amount of static data needed by
an dynamic library or executable it should not include things
like heap or stack areas, which in the case of `-pie` are
not determined until runtime (e.g. __stack_pointer is imported).

Differential Revision: https://reviews.llvm.org/D90261
2020-10-27 16:05:52 -07:00
Paulo Matos
388fb67b0d [WebAssembly] Added .tabletype to asm and multiple table support in obj files
Adds more testing in basic-assembly.s and a new test tables.s.
Adds support to yaml reading and writing of tables as well.

Differential Revision: https://reviews.llvm.org/D88815
2020-10-13 07:52:23 -07:00
Sam Clegg
b3b4cda104 [lld][WebAssembly] Don't GC library objects under --whole-archive
Followup on https://reviews.llvm.org/D85062 which ignores
entire library objects when no symbols are used within them.
This is shouldn't apply with `--whole-archive` since this
is specified to treat them like direct object inputs.

Differential Revision: https://reviews.llvm.org/D89290
2020-10-12 21:19:19 -07:00
Dan Gohman
950ae43091 [WebAssembly] GC constructor functions in otherwise unused archive objects
This allows `__wasilibc_populate_libpreopen` to be GC'd in more cases
where it isn't needed, including when linked from Rust's libstd.

Differential Revision: https://reviews.llvm.org/D85062
2020-10-12 18:54:57 -07:00
Sam Clegg
2513407d39 [lld][WebAssembly] Add support for -Bsymbolic flag
This flag works in a similar way to the ELF linker in that it
will resolve any defined symbols to their local definition with
a shared library or -pie executable.

This flag has no effect on static linking.

Differential Revision: https://reviews.llvm.org/D89152
2020-10-12 17:25:04 -07:00
Dan Gohman
6cd8511e59 [WebAssembly] New-style command support
This adds support for new-style command support. In this mode, all exports
are considered command entrypoints, and the linker inserts calls to
`__wasm_call_ctors` and `__wasm_call_dtors` for all such entrypoints.

This enables support for:

 - Command entrypoints taking arguments other than strings and return values
   other than `int`.
 - Multicall executables without requiring on the use of string-based
   command-line arguments.

This new behavior is disabled when the input has an explicit call to
`__wasm_call_ctors`, indicating code not expecting new-style command
support.

This change does mean that wasm-ld no longer supports DCE-ing the
`__wasm_call_ctors` function when there are no calls to it. If there are no
calls to it, and there are ctors present, we assume it's wasm-ld's job to
insert the calls. This seems ok though, because if there are ctors present,
the program is expecting them to be called. This change affects the
init-fini-gc.ll test.
2020-09-30 19:02:40 -07:00
Sam Clegg
3c45a06f26 [lld][WebAssembly] Allow exporting of mutable globals
In particular allow explict exporting of `__stack_pointer` but
exclud this from `--export-all` to avoid requiring the mutable
globals feature whenenve `--export-all` is used.

This uncovered a bug in populateTargetFeatures regarding checking
if the mutable-globals feature is allowed.

See: https://github.com/WebAssembly/binaryen/issues/2934

Differential Revision: https://reviews.llvm.org/D88506
2020-09-30 17:53:27 -07:00
Benjamin Kramer
b59dff4b16 [wasm] Move WasmTraits.h to BinaryFormat
There's no dependency on Object in there and this avoids a cyclic
dependency between libMC and libObject.
2020-09-28 22:07:28 +02:00
Thomas Lively
15a5e86fb3 [lld][WebAssembly] Allow atomics feature with unshared memory
https://github.com/WebAssembly/threads/issues/144 updated the
WebAssembly threads proposal to make atomic operations on unshared memories
valid. This change updates the feature checking in the linker accordingly.
Production WebAssembly engines have recently been updated to allow this
behvaior, but after this change users who accidentally use atomics with unshared
memories on older versions of the engines will get validation errors at runtime
rather than link errors.

Differential Revision: https://reviews.llvm.org/D79530
2020-09-24 20:35:29 -07:00
Alexandre Ganea
f2efb5742c [LLD][COFF] Cover usage of LLD-as-a-library in tests
In lit tests, we run each LLD invocation twice (LLD_IN_TEST=2), without shutting down the process in-between. This ensures a full cleanup is properly done between runs.
Only active for the COFF driver for now. Other drivers still use LLD_IN_TEST=1 which executes just one iteration with full cleanup, like before.
When the environment variable LLD_IN_TEST is unset, a shortcut is taken, only one iteration is executed, no cleanup for faster exit, like before.
A public API, lld::safeLldMain(), is also available when using LLD as a library.

Differential Revision: https://reviews.llvm.org/D70378
2020-09-24 15:07:50 -04:00
Fangrui Song
51b75b87db [lld][WebAssembly] Fix -Wunused-variable after D87663 2020-09-18 16:10:39 -07:00
Sam Clegg
3f411e9773 [lld][WebAssembly] Fix --export-all when __stack_pointer is present
With https://reviews.llvm.org/D87537 we made it an error
to import or export a mutable global with the +mutable-globals
feature present.  However the scan was of the entire symbol
table rather than just the imports or exports and the filter
didn't match exaclyt meaning the `__stack_pointer` (a mutable
global) was always triggering with error when the `--export-all`
flag was used.

This also revealed that we didn't have any test coverage for
the `--export-all` flag.

This change fixes the current breakage on the emscripten-releases
roller.

Differential Revision: https://reviews.llvm.org/D87663
2020-09-15 06:17:01 -07:00
Sam Clegg
2c12b056be [lld][WebAssembly] Allow globals imports via import_name/import_module
This feature already exists but was limited to function
symbols.

Differential Revision: https://reviews.llvm.org/D87666
2020-09-14 20:35:03 -07:00
Sam Clegg
cc2da5554b [lld][WebAssembly] Add initial support for -Map/--print-map
Differential Revision: https://reviews.llvm.org/D77187
2020-09-12 16:10:51 -07:00
Sam Clegg
04febd30a8 [lld][WebAssembly] Error on import/export of mutable global without mutable-globals feature
Also add the +mutable-globals features in clang when
building with `-fPIC` since the linker will generate mutable
globals imports and exports in that case.

Differential Revision: https://reviews.llvm.org/D87537
2020-09-12 14:28:14 -07:00
Samuel Kostial
304264e73d [lld][WebAssembly] Emit all return types of multivalue functions
We previously were incorrectly emitting only the first result type.

Differential Revision: https://reviews.llvm.org/D85783
2020-08-12 13:14:15 -07:00
Wouter van Oortmerssen
b52fc59e17 [WebAssembly] Fixed memory.init always using 64-bit ptr
(because the is64 flag was tested incorrectly in LLD).
2020-08-12 12:23:36 -07:00
Wouter van Oortmerssen
582fd474dd [WebAssembly] wasm64: fix memory.init operand types
I had assumed they would all become in i64, but this is not necessary as long as data segments stay 32-bit, see:
https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md

Differential Revision: https://reviews.llvm.org/D85552
2020-08-10 10:15:20 -07:00
Sam Clegg
b34ec5969f [lld][WebAssembly] Handle weakly referenced symbols when lazy (archive) version is see first
When a weak reference of a lazy symbol occurs we were not correctly
updating the lazy symbol.  We need to tag the existing lazy symbol
as weak and, in the case of a function symbol, give it a signature.

Without the signature we can't then create the dummy function which
is needed when an weakly undefined function is called.

We had tests for weakly referenced lazy symbols but we were only
tests in the case where the reference was seen before the lazy
symbol.

See: https://github.com/WebAssembly/wasi-libc/pull/214

Differential Revision: https://reviews.llvm.org/D85567
2020-08-10 08:16:55 -07:00
Michele Scandale
53880b8cb9 [CMake] Make intrinsics_gen dependency unconditional.
The `intrinsics_gen` target exists in the CMake exports since r309389
(see LLVMConfig.cmake.in), hence projects can depend on `intrinsics_gen`
even it they are built separately from LLVM.

Reviewed By: MaskRay, JDevlieghere

Differential Revision: https://reviews.llvm.org/D83454
2020-07-17 16:43:17 -07:00
Wouter van Oortmerssen
cc1b9b680f [WebAssembly] 64-bit (function) pointer fixes.
Accounting for the fact that Wasm function indices are 32-bit, but in wasm64 we want uniform 64-bit pointers.
Includes reloc types for 64-bit table indices.

Differential Revision: https://reviews.llvm.org/D83729
2020-07-16 14:10:22 -07:00
Wouter van Oortmerssen
29f8c9f6c2 [WebAssembly] Triple::wasm64 related cleanup
Differential Revision: https://reviews.llvm.org/D83713
2020-07-16 12:01:10 -07:00
Wouter van Oortmerssen
4d135b0446 [WebAssembly] 64-bit memory limits 2020-07-06 12:40:45 -07:00
Sam Clegg
73e575a88e [lld][WebAssembly] Give better error message on bad archive member
Include the archive name as well as the member name when an error
is encountered parsing bitcode archives.

Differential Revision: https://reviews.llvm.org/D82884
2020-06-30 21:36:36 -07:00
Dan Gohman
46a3268312 [WebAssembly] Add warnings for -shared and -pie
The meaning of -shared and -pie are expected to be changed in the
future when Module Linking-style libraries are implemented. Begin
issuing warnings to give people a heads-up that they will be changing.

For compatibility with Emscripten, add a --experimental-pic flag which
disables these warnings.

Differential Revision: https://reviews.llvm.org/D81760
2020-06-25 15:55:46 -07:00
Wouter van Oortmerssen
b9a539c010 [WebAssembly] Adding 64-bit versions of __stack_pointer and other globals
We have 6 globals, all of which except for __table_base are 64-bit under wasm64.

Differential Revision: https://reviews.llvm.org/D82130
2020-06-25 15:52:44 -07:00
Sam Clegg
79aad89d8d [WebAssembly] Add support for externalref to MC and wasm-ld
This allows code for handling externref values to be processed by the
assembler and linker.

Differential Revision: https://reviews.llvm.org/D81977
2020-06-22 15:57:24 -07:00