1473 Commits

Author SHA1 Message Date
Veikka Tuominen
4e64373fc0 fix generic function arg debug info referencing wrong parameter
Closes #14123
2022-12-30 17:00:50 +02:00
Andrew Kelley
40ba4d4a89
Merge pull request #14102 from Luukdegram/wasm-undefined-symbols
WebAssembly: remove unconditional --allow-undefined flag
2022-12-29 17:59:44 -05:00
Jacob Young
74b14edea8 link: fix memory leaks
* Fix linker memory leaks found while running `zig build test-cases`.
 * Add missing target to test manifest.
2022-12-28 14:24:27 -05:00
Luuk de Gram
8403612adc
test/link: update linker tests
Force importing symbols to show the correct functions are being
imported from the host environment.
2022-12-28 16:49:36 +01:00
Luuk de Gram
4aab8118a7
WebAssembly: don't append --export for functions
No longer automatically append the `--export` flag for each exported
function unconditionally. This was essentially a hack to prevent
binary bloat caused by compiler-rt symbols being always included in
the final binary as they were exported and therefore not garbage-
collected. This is no longer needed as we now support the ability to
set the visibility of exports.
This essentially reverts 6d951aff7e32b1b0252d341e66517a9a9ee98a2d
2022-12-28 14:41:27 +01:00
Jakub Konka
601ab9a251
Merge pull request #14086 from ziglang/issue-14082
macho: ensure LINKEDIT layout follows Apple strict validation rules
2022-12-27 21:39:05 +01:00
Jakub Konka
c0c5acf074 macho+zld: fix 32bit build 2022-12-27 17:04:57 +01:00
Jakub Konka
6925ef0f1a zld: exclude strtab padding from uuid calculation 2022-12-27 16:43:35 +01:00
Jakub Konka
2620146834 macho+zld: align to 8 and pad out strtab 2022-12-27 16:43:35 +01:00
Jakub Konka
d83d39297c macho+zld: align to 8 and pad out dysymtab 2022-12-27 16:43:35 +01:00
Jakub Konka
0409dda46b zld: align to 8 and pad out function starts and data in code sections 2022-12-27 16:43:35 +01:00
Jakub Konka
246e3e0c75 macho+zld: align to 8 and pad out dyld info sections 2022-12-27 16:43:35 +01:00
Veikka Tuominen
622311fb9a update uses of overflow arithmetic builtins 2022-12-27 15:13:14 +02:00
Frank Denis
7802c26449 WebAssembly: do not link with --allow-undefined unconditionally
In #1622, when targeting WebAsembly, the --allow-undefined flag
became unconditionally added to the linker.

This is not always desirable.

First, this is error prone. Code with references to unkown symbols
will link just fine, but then fail at run-time.

This behavior is inconsistent with all other targets.

For freestanding wasm applications, and applications that only use
WASI, undefined references are better reported at compile-time.

This behavior is also inconsistent with clang itself. Autoconf and
cmake scripts checking for function presence think that all tested
functions exist, but then resulting application cannot run.

For example, this is one of the reasons compilation of Ruby 3.2.0
to WASI fails with zig cc, while it works out of the box with clang.
But all applications checking for symbol existence before compilation
are affected.

This reverts the behavior to the one Zig had before #1622, and
introduces an `import_symbols` flag to ignore undefined symbols,
assuming that the webassembly runtime will define them.
2022-12-25 22:32:21 +01:00
Jakub Konka
bb62d5105c
Merge pull request #14049 from ziglang/issue-14045
macho+zld: write code signature padding before committing load commands
2022-12-23 19:07:36 +01:00
Jakub Konka
22b39f034b macho+zld: properly deinit code signature object 2022-12-23 14:35:45 +01:00
Jakub Konka
550ebcce9a macho+zld: write code signature padding before commiting LCs
Otherwise, we were prematurely committing `__LINKEDIT` segment LC
with outdated size (i.e., without code signature being taken into account).
This would scaffold into strict validation failures by Apple tooling.
2022-12-23 12:50:03 +01:00
Jakub Konka
dc22c3b9a5
Merge pull request #14006 from Luukdegram/wasm-no-lld
wasm-linker: allow usage of the in-house WebAssembly linker
2022-12-20 18:42:14 +01:00
Luuk de Gram
6f44e2d1d3
wasm-linker: consolidate writing to file
This merges the paths from flushModule and linkWithZld to a single
function that will write the entire WebAssembly module to the file.
This reduces the chance of mistakes as we do not have to duplicate
the logic. A similar action may be needed later for linkWithLLD.
2022-12-19 16:50:25 +01:00
Andrew Kelley
aca9c74e80
Merge pull request #13914 from Vexu/variadic
implement defining C variadic functions
2022-12-18 16:24:13 -05:00
Luuk de Gram
8eac2e30c9
wasm-linker: Add caching + more into zld path 2022-12-18 16:41:57 +01:00
Luuk de Gram
dd85092982
wasm-linker: Fix relocations for alias'd atoms
When an atom has one or multiple aliasses, we we could not find the
target atom from the alias'd symbol. This is solved by ensuring that
we also insert each alias symbol in the symbol-atom map.
2022-12-18 16:37:00 +01:00
Luuk de Gram
2a62dbda0b
wasm-linker: fix type index relocations
Previously we used the relocation index to find the corresponding
symbol that represents the type. However, the index actually
represents the index into the list of types. We solved this by
first retrieving the original type, and then finding its location
in the new list of types. When the atom file is 'null', it means
the type originates from a Zig function pointer or a synthetic
function. In both cases, the final type index was already resolved
and therefore equals to relocation's index value.
2022-12-17 17:17:34 +01:00
r00ster91
aac2d6b56f std.builtin: rename Type.UnionField and Type.StructField's field_type to type 2022-12-17 14:11:33 +01:00
Veikka Tuominen
58caed1c71 Sema: make is_non_{null,err} stricter about types
Closes #13023
2022-12-17 13:22:09 +02:00
Jakub Konka
270b6c4c2f
Merge pull request #13964 from ziglang/issue-11737
Misc MachO linker improvements and link-tests refactor
2022-12-17 10:26:56 +01:00
Luuk de Gram
476202eec0
wasm-linker: Fix archive symbols parsing
When parsing the table of contents containing the symbols and their
positions we initially used the index within the map to retrieve
the offset. However, during resizing of the underlaying array this
would invalidate those indexes which meant incorrect offsets were
being stored for symbols. We now use the current symbol index
to also get the index into the symbol position instead.
2022-12-16 18:31:24 +01:00
Jakub Konka
f7266e03a8 macho: identifier string in code signature should be just basename 2022-12-16 18:05:58 +01:00
Jakub Konka
bd6d951f63 macho: fix 32bit build 2022-12-16 18:05:58 +01:00
Jakub Konka
585c21e54d macho: move parallel file hashing back to CodeSignature
I need to think some more how to calculate UUID in parallel, if
it is even possible, to preserve UUID's determinism.
2022-12-16 18:05:58 +01:00
Jakub Konka
1928ed7dab macho: RingBuffer experiment to crack down the bug 2022-12-16 18:05:58 +01:00
Jakub Konka
79b92f9eb8 macho: remove temp debugging routines 2022-12-16 18:05:58 +01:00
Jakub Konka
d00094dd45 macho: exclude all content of the binary that could cause non-deterministic UUID 2022-12-16 18:05:58 +01:00
Jakub Konka
b323e14b1c macho: exclude linkedit and symtab/dysymtab load commands from the uuid calc 2022-12-16 18:05:58 +01:00
Jakub Konka
660270b7a9 macho: calculate UUID excluding stabs and part of contributing strtab 2022-12-16 18:05:58 +01:00
Jakub Konka
09dee74414 macho: store LC headers to often updated LINKEDIT sections 2022-12-16 18:05:58 +01:00
Jakub Konka
3af6a4e887 macho: generate stabs last to aid in deterministic uuid calculation 2022-12-16 18:05:58 +01:00
Motiejus Jakštys
b3f4e0d091 Elf: switch link order of libcompiler_rt and libc
Given `main.go`:

    package main
    import  _ "os/user"
    func main() {}

Compiling it to linux/arm64:

    $ CGO_CFLAGS='-O0' GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC="zig cc -target aarch64-linux-gnu.2.28" go build main.go

Results in this error:

    runtime/cgo(.text): unknown symbol memset in callarm64
    runtime/cgo(.text): unknown symbol memset in callarm64
    runtime/cgo(.text): relocation target memset not defined

In the midst of intermediate compilations files we can see this commmand:

    ld.lld -o _cgo_.o <...> /tmp/go-build206961058/b043/_x009.o <...> ~/.cache/zig/.../libcompiler_rt.a <...> ~/.cache/.../libc.so.6

`_x009.o` needs memset:

    $ readelf -Ws ./b043/_x009.o | grep memset
        22: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memset

Both `libcompiler_rt.a` and `libc.so.6` provide it:

    $ readelf -Ws ~/.cache/zig/.../libcompiler_rt.a | grep memset
       870: 0000000000000000   318 FUNC    WEAK   DEFAULT  519 memset

    $ readelf -Ws ~/.cache/zig/.../libc.so.6 | grep -w memset
       476: 000000000001d34c     0 FUNC    GLOBAL DEFAULT    7 memset@@GLIBC_2.2.5

Since `libcompiler_rt.a` comes before libc in the linker line, the
resulting `_cgo_.o` still links to a weak, unversioned memset:

    $ readelf -Ws ./b043/_cgo_.o | grep -w memset
        40: 000000000022c07c   160 FUNC    WEAK   DEFAULT   14 memset
       719: 000000000022c07c   160 FUNC    WEAK   DEFAULT   14 memset

Since the final linking step is done by Golang's linker, it does not
know of `libcompiler_rt.a`, and fails to link with the error message
above. However, Go linker does recognize memset from glibc. If we
specify an `-lc` equivalent before the `libcompiler_rt.a`, it will link
to memset from libc:

    $ readelf -Wa ./b043/_x009.o |grep memset
        14: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND memset@GLIBC_2.17 (2)
       157: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND memset@GLIBC_2.17

... and then `main.go` will compile+link successfully.

Why doesn't Go linker take memset from glibc? An educated guess: Go
determines whether to link with glibc from what the program asks (I
presume `.dynsym`). Since `memset` is no longer attributed to glibc, Go
skips linking to glibc altogether.

Bonus question: curious why `-O0` is necessary? Because when
optimizations are enabled (the default), the C compiler replaces
`memset` function call with plain `stp` instructions (on aarch64).
2022-12-16 07:46:47 -05:00
Jakub Konka
6b99aab0eb macho: do not assume exe or dylib when flushing module 2022-12-16 00:01:04 +01:00
Jakub Konka
79457fc76a macho: generalize parallel hasher; impl parallel MD5-like hash
By pulling out the parallel hashing setup from `CodeSignature.zig`,
we can now reuse it different places across MachO linker (for now;
I can totally see its usefulness beyond MachO, eg. in COFF or ELF too).
The parallel hasher is generic over actual hasher such as Sha256 or MD5.
The implementation is kept as it was.

For UUID calculation, depending on the linking mode:
* incremental - since it only supports debug mode, we don't bother with MD5
  hashing of the contents, and populate it with random data but only once
  per a sequence of in-place binary patches
* traditional - in debug, we use random string (for speed); in release,
  we calculate the hash, however we use LLVM/LLD's trick in that we
  calculate a series of MD5 hashes in parallel and then one an MD5 of MD5
  final hash to generate digest.
2022-12-16 00:01:04 +01:00
Jakub Konka
ec40c6b28f macho: calculate UUID as a streaming MD5 hash of the file contents 2022-12-16 00:01:04 +01:00
Jakub Konka
db2052bc35 macho: dedup LC emitting logic
Fix path written to `LC_ID_DYLIB` to include the current CWD (if any).
2022-12-16 00:01:04 +01:00
Luuk de Gram
ae106db889
wasm-linker: Fix debug info relocations 2022-12-15 20:28:30 +01:00
Luuk de Gram
f95549ddc7
wasm-linker: Export data symbols as global
When a data symbol is required to be exported, we instead generate
a global that will be exported. This global is immutable and contains
the address of the data symbol.
2022-12-14 16:55:23 +01:00
Luuk de Gram
41199bba4b
wasm-linker: Create separate path for one-shot
When invoking the self-hosted linker using `-fno-LLD` while using the
LLVM backend or invoking it as a linker, we create a seperate path.
This path will link the object file generated by LLVM and the
supplied object files just once, allowing to simplify the
implementation between incremental and regular linking.
2022-12-13 19:19:05 +01:00
Jakub Konka
f7fea080b2 macho+zld: skip atomless synthetic globals in dead_strip
They are implicitly marked live.
2022-12-09 21:12:38 +01:00
Jakub Konka
182751ba27 Revert "coff: specify default base path for relative source paths in pdb" 2022-12-09 14:16:44 +01:00
Jakub Konka
4d640f9bb9 dwarf: resolve all relative paths when generating include_dirs and file_names lists 2022-12-09 14:16:44 +01:00
Jakub Konka
9735953ae2 wasm: implement moving debug_line program when header too big 2022-12-09 09:24:25 +01:00
Jakub Konka
742aa94280 dsym: hint linker when file range copy is not necessary 2022-12-09 09:24:25 +01:00