899 Commits

Author SHA1 Message Date
Jakub Konka
510357355a stage2: fix premature atom deinit in macho linker 2021-12-15 17:28:48 +01:00
Jakub Konka
3ff05b79b9 macho: handle TLS imported from dylib
This is a missing feature which requires `__thread_ptrs` section
to be synthesised for any extern reference to a global TLS variable.
2021-12-15 08:59:20 +01:00
Jakub Konka
2e7a48d6bf macho: fix section size calculation when no prealloc
Add test for common symbol alignment.
2021-12-15 08:59:20 +01:00
Jakub Konka
ab328aca33 macho: put LC_* consts in a typed enum(u32) LC
repeat for `PLATFORM_*` and `TOOL_*` sets
2021-12-15 08:59:20 +01:00
Luuk de Gram
50201e1c30 wasm-linker: Allow specifying symbols to be exported
Notating a symbol to be exported in code will only tell the linker
where to find this symbol, so other object files can find it. However, this does not mean
said symbol will also be exported to the host environment. Currently, we 'fix' this by force
exporting every single symbol that is visible. This creates bigger binaries and means host environments
have access to symbols that they perhaps shouldn't have. Now, users can tell Zig which symbols
are to be exported, meaning all other symbols that are not specified will not be exported.

Another change is we now support `-rdynamic` in the wasm linker as well, meaning all symbols will
be put in the dynamic symbol table. This is the same behavior as with ELF. This means there's a 3rd strategy
users will have to build their wasm binary.
2021-12-14 14:02:23 -08:00
Jakub Konka
4ce6208750 macho: if lib or framework not found, wait until syms resolved
This way, we will inform the user that there are unresolved symbols
in addition to missing library/framework as requested on the linker
line. If all symbols were resolved on the other hand, we still
flag up that the library/framework cannot be found.

Example behaviour:

```
$ zig cc hello.c -framework MyFoundation --verbose
warning(link): framework not found for '-framework MyFoundation'
warning(link): Framework search paths:
warning(link):   /Library/Frameworks
warning(link):   /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
thread 1079397 panic: attempt to unwrap error: FrameworkNotFound
...stack trace...
```

and

```
❯ zig cc hello.c -lWAT --verbose
warning(link): library not found for '-lWAT'
warning(link): Library search paths:
warning(link):   /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
warning(link):   /usr/local/lib
thread 1079824 panic: attempt to unwrap error: LibraryNotFound
...stack trace...
```
2021-12-12 13:53:59 +01:00
Jakub Konka
1abae06432 Merge branch 'framework-not-found' of git://github.com/motiejus/zig into motiejus-framework-not-found 2021-12-12 13:31:26 +01:00
Jakub Konka
dbfcebf8d8 macho: allow undefined symbols in dylibs
We now respect both `-fallow-shlib-undefined` and
`-Wl,"-undefined=dynamic_lookup"` flags. This is the first step
towards solving issues #8180 and #3000. We currently do not expose
any other ld64 equivalent flag for `-undefined` flag - we basically
throw an error should the user specify a different flag. Support for
those is conditional on closing #8180. As a result of this change,
it is now possible to generate a valid native Node.js addon with Zig
for macOS.
2021-12-12 00:32:03 +01:00
Motiejus Jakštys
82a7069344 macho: fail if requested -framework is not found
If `-framework` is requested, but not found, the linker will err
instead of creating a strange executable.

https://github.com/ziglang/zig/issues/10299#issuecomment-990404953

Refs #9542
Refs #10299
Refs #10158
2021-12-11 10:40:57 +02:00
Jakub Konka
828f61e8df macho: move all helpers from commands.zig into std.macho
This way we will finally be able to share common parsing logic
between different Zig components and 3rd party packages.
2021-12-10 18:18:28 +01:00
Jakub Konka
81e7d8505c macho: move helper functions to libstd
Helper functions such as `commands.sectionName`, etc. should really
belong in `std.macho.section_64` extern struct.
2021-12-10 11:56:51 +01:00
Jakub Konka
64e2bfaa23 macho: write out LC_FUNCTION_START data 2021-12-08 23:24:40 +01:00
Jakub Konka
98e36ea6b9 macho: handle non-extern UNSIGNED relocs on arm64 2021-12-08 20:48:12 +01:00
Jakub Konka
f0ec70edd1 macho: do not write null symbols into undef symbols section
Also, skip creating stub entries for resolved globally defined
regular symbols.
2021-12-08 16:58:29 +01:00
Andrew Kelley
83a6681955 link: fix build for 32-bit targets
This wasn't caught by the CI checks because this function is is only
called for the `use_stage1` codepath.
2021-12-07 17:03:29 -07:00
Jakub Konka
71d19318e7 macho: do not preserve temp symbol names
and do not write out local symbol in MachO's symbol table if global
symbol exists with the same name.
2021-12-06 00:09:51 +01:00
Jakub Konka
3ac973c706 macho: fix incremental codepath for linking objects 2021-12-06 00:00:50 +01:00
Jakub Konka
124e942714 macho: refactor logging and function names 2021-12-05 23:26:30 +01:00
Jakub Konka
397a11c107 macho: sort sections when linking stage1 binary 2021-12-05 22:46:53 +01:00
Jakub Konka
c86f2402d0 macho: don't prealloc sections when stage1 2021-12-05 22:46:46 +01:00
Luuk de Gram
9e03cf9489 wasm: Initial behavior tests succeeding
- Correctly load slice value on stack
- Implement WrapErrorUnionErr and payload
- Implement trunc, fix sliceLen and write undefined
- Implement slice as return type and argument

Note: This also fixes a memory leak for inferred error sets, and for usingnamespace
2021-12-05 12:19:01 -08:00
Andrew Kelley
f7cbd92e6c Revert "Merge pull request #10270 from Luukdegram/behaviour-tests"
This reverts commit 725267f7c20f0ba588b472048a8c1fe1a328c714, reversing
changes made to 2dae860de3494f97c9477af9282fe0131ff5c4cb.

This test is failing:

```zig
pub fn main() u8 {
    var e = foo();
    const i = e catch 69;
    return i;
}

fn foo() anyerror!u8 {
    return 5;
}
```

It's returning 69 instead of the expected value 5.
2021-12-04 21:55:50 -07:00
Luuk de Gram
96a4692f94
wasm: Correctly load slice value on stack 2021-12-04 19:22:08 +01:00
Luuk de Gram
1777fb25bc
wasm: Implement WrapErrorUnionErr(payload) 2021-12-04 19:22:07 +01:00
Lee Cannon
066eaa5e9c
allocgate: change resize to return optional instead of error 2021-11-30 23:45:01 +00:00
Lee Cannon
1093b09a98
allocgate: renamed getAllocator function to allocator 2021-11-30 23:32:47 +00:00
Lee Cannon
75548b50ff
allocgate: stage 1 and 2 building 2021-11-30 23:32:47 +00:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00
Jakub Konka
86fe47235e macho: move nlist_64 type/flags helpers to std.macho 2021-11-30 13:59:33 +01:00
Jakub Konka
2873e19366 macho: ignore undefined symbols marked for discarding
If a symbol is undefined after we tried resolving it in static and
dynamic libraries, and it is annotated with N_DESC_DISCARDED flag,
we simply ignore it rather than flagging an undefined symbol error.
2021-11-30 10:22:26 +01:00
Andrew Kelley
902df103c6 std lib API deprecations for the upcoming 0.9.0 release
See #3811
2021-11-30 00:13:07 -07:00
Jakub Konka
da0ea909bc macho: POINTER_TO_GOT signals a need for a GOT entry
It also signals the need for GOT indirection (unless it can
be optimised away via link-time constant optimisation).
2021-11-29 11:51:27 -08:00
Jakub Konka
7a7df392d1
Merge pull request #10240 from Luukdegram/stage2-wasm-behaviour
Stage2: wasm - Implement 'zig test'
2021-11-29 10:52:04 +01:00
Jakub Konka
58a552aaf9 macho: save all undef symbols even if null 2021-11-28 20:48:54 +01:00
Jakub Konka
7e27ab0947 macho: fix parsing addend for non-extern SIGNED_X reloc
If `r_extern == 0` (the relocation is non-extern, meaning it targets
a specific memory offset within the object's section) and if the
relocation type signifies that the relocation requires correction
for RIP such as SIGNED_1, then we need to subtract the correction,
here 1 for SIGNED_1, from the calculated addend value as it's
implicitly included.
2021-11-28 20:35:00 +01:00
Luuk de Gram
dd49eca342
wasm: Implement 'zig test'
- This implements the required codegen for decl types such as pointers, arrays, structs and more.
- Wasm's start function can now use both a 'u8' and 'void' as return type. This will help us with writing tests
using the stage2 testing backend. (Until all tests of behavioural tests pass).
- Now correctly generates relocations for function pointers.
- Also implements unwrapping error union error, as well as return pointers.
2021-11-28 20:25:33 +01:00
Luuk de Gram
7226ad2670
wasm-link: Implement indirect function table
The function table contains all function pointers that are called
by using call_indirect. During codegen, we create a relocation
where the linker will resolve the correct index into the table and stores
this value within the data section at the location of the pointer.
2021-11-28 12:49:04 +01:00
Luuk de Gram
6e88df44a2
wasm-linker: Link into binary during flush
This contains a few additions:
- Proper stack pointer calculation keeping alignment in mind.
- Setting up memory layout (including user flags).
- Export or import memory
- Handle 'easy' linker tasks during incremental compilation, while offloading
heavy-tracking/computation tasks to `flush()`
- This architecture allows us to easily integrate with the rest of 'zwld' to
implement linking stage2 code with external object files.
2021-11-27 15:11:03 +01:00
Luuk de Gram
a54ac08885
wasm-linker: Resolve relocations
We now resolve relocations for globals, memory addresses and function indexes.
Besides above, we now also emit imported functions correctly and create a
corresponding undefined symbol for it, where as we create a defined symbol
for all other cases.

TODO: Make incrememental compilation work again with new linker infrastructure
2021-11-27 15:02:05 +01:00
Luuk de Gram
f56ae69edd
wasm-linker: Upstream zwld into stage2
- Converts previous `DeclBlock` into `Atom`'s to also make them compatible when
the rest of zlwd gets upstreamed and we can link with other object files.
- Resolves function signatures and removes any duplicates, saving us a lot of
potential bytes for larger projects.
- We now create symbols for each decl of the respective type
- We can now (but not implemented yet) perform proper relocations.
- Having symbols and segment_info allows us to create an object file
for wasm.
2021-11-27 15:02:01 +01:00
Andrew Kelley
eec825cea2 zig cc: support -Bdynamic and -Bstatic parameters
Related: #10050
2021-11-26 16:26:19 -07:00
Jakub Konka
a956958ba9 macho: define __mh_execute_header as a linker synthetic global 2021-11-26 17:04:04 +01:00
Jakub Konka
8317dbd1cb macos: detect SDK path and version, then pass to the linker
Since we are already detecting the path to the native SDK,
if available, also fetch SDK's version and route that to the linker.
The linker can then use it to correctly populate LC_BUILD_VERSION
load command.
2021-11-26 16:26:44 +01:00
Jakub Konka
d6f43a1eac bpf: do not invoke lld when linking eBPF relocatables
Due to a deficiency in LLD, we need to special-case BPF to a simple
file copy when generating relocatables. Normally, we would expect
`lld -r` to work. However, because LLD wants to resolve BPF relocations
which it shouldn't, it fails before even generating the relocatable.

Co-authored-by: Matthew Knight <mattnite@protonmail.com>
2021-11-26 10:53:30 +01:00
Jakub Konka
3a3576da60 Version libSystem shipped with zig toolchain
We will mimick the same solution as with the headers:
* `libSystem.10.tbd`
* `libSystem.11.tbd`
* `libSystem.12.tbd`

and so on...
2021-11-25 17:10:08 +01:00
Andrew Kelley
20cc7af8e6 stage2: support LLD -O flags on ELF
In 7e23b3245a9bf6e002009e6c18c10a9995671afa I made -O flags to the
linker emit a warning that the argument does nothing. That was not
correct however; LLD does have some logic that does different things
depending on -O0, -O1, and -O2. It defaults to -O1, and it does less
optimizations with -O0 and more with -O2.

With this commit, e.g. `-Wl,-O1` is supported by the `zig cc` frontend,
and by default we pass `-O0` to LLD in debug mode, and `-O3` in release
modes.

I also fixed a bug in the LLD ELF linker line which was incorrectly
passing `-O` flags instead of `--lto-O` flags for LTO.
2021-11-24 18:46:32 -07:00
Andrew Kelley
27c5c7fb23 stage2: proper -femit-implib frontend support
* Improve the logic for determining whether emitting an import lib is
   eligible, and improve the error message when the user provides
   contradictory arguments.
 * Integrate with the EmitLoc / Emit system that already exists, and use
   the `-femit-implib[=path]`/`-fno-emit-implib` convention that already
   exists.
 * Proper integration with the caching system.
 * CLI: fix bug in error reporting for resolving EmitLoc values for
   other parameters.
2021-11-24 18:12:56 -07:00
Andrew Kelley
7e23b3245a stage2: remove extra_lld_args
This mechanism for sending arbitrary linker args to LLD has no place in
the Zig frontend, because our goal is for the frontend to understand all
the arguments and not treat linker args like a black box.

For example we have self-hosted linking in addition to LLD, so we want to
have the options make sense to both linking codepaths, not just the LLD one.

Passing -O linker args will now result in a warning that the arg does
nothing.
2021-11-24 17:14:20 -07:00
Kurt Kartaltepe
fd369bcb0b Coff Linker: Pass extra lld args
Previously these were added to the hash but not actually appended to the
linker arguments.
2021-11-24 17:14:20 -07:00
Kurt Kartaltepe
a950cb42bd Coff linker: Add IMPLIB support
Allow --out-implib and -implib as passed by cmake and meson to be
correctly passed through to the linker to generate import libraries.
2021-11-24 17:14:20 -07:00