348 Commits

Author SHA1 Message Date
Alex Rønne Petersen
5c57e90ffb test: Disable test-elf-ld-script-path-error for now.
https://github.com/ziglang/zig/issues/23125
2025-03-08 07:04:27 +01:00
Linus Groh
79460d4a3e Remove uses of deprecated callconv aliases 2025-03-05 03:01:43 +00:00
David Rubin
ca83f52fd9 ubsan: update wording 2025-02-25 11:22:33 -08:00
David Rubin
44d3b5a6e4 build: add comments explaining why we disable ubsan 2025-02-25 11:22:33 -08:00
David Rubin
35b9db3b15 correct some bugs 2025-02-25 11:22:33 -08:00
Andrew Kelley
eb3c7f5706 zig build fmt 2025-02-22 17:09:20 -08:00
Andrew Kelley
61b69a418d
Merge pull request #22659 from ifreund/linker-script-fix
link: fix ambiguous names in linker scripts
2025-02-22 18:18:24 -05:00
Alex Rønne Petersen
f87b443af1 link.MachO: Add support for the -x flag (discard local symbols).
This can also be extended to ELF later as it means roughly the same thing there.

This addresses the main issue in #21721 but as I don't have a macOS machine to
do further testing on, I can't confirm whether zig cc is able to pass the entire
cgo test suite after this commit. It can, however, cross-compile a basic program
that uses cgo to x86_64-macos-none which previously failed due to lack of -x
support. Unlike previously, the resulting symbol table does not contain local
symbols (such as C static functions).

I believe this satisfies the related donor bounty: https://ziglang.org/news/second-donor-bounty
2025-02-22 06:35:19 +01:00
Isaac Freund
819716b59f
link: fix ambiguous names in linker scripts
Currently zig fails to build while linking the system LLVM/C++ libraries
on my Chimera Linux system due to the fact that libc++.so is a linker
script with the following contents:

INPUT(libc++.so.1 -lc++abi -lunwind)

Prior to this commit, zig would try to convert "ambiguous names" in
linker scripts such as libc++.so.1 in this example into -lfoo style
flags. This fails in this case due to the so version number as zig
checks for exactly the .so suffix.

Furthermore, I do not think that this conversion is semantically correct
since converting libfoo.so to -lfoo could theoretically end up resulting
in libfoo.a getting linked which seems wrong when a different file is
specified in the linker script.

With this patch, this attempted conversion is removed. Instead, zig
always first checks if the exact file/path in the linker script exists
relative to the current working directory.

If the file is classified as a library (including versioned shared
objects such as libfoo.so.1), zig then falls back to checking if
the exact file/path in the linker script exists relative to each
directory in the library search path, selecting the first match or
erroring out if none is found.

This behavior fixes the regression that prevents building zig while
linking the system LLVM/C++ libraries on Chimera Linux.
2025-02-10 23:19:48 +01:00
BratishkaErik
941677e083
std.Build: add addLibrary function (#22554)
Acts as a replacement for `addSharedLibrary` and `addStaticLibrary`, but
linking mode can be changed more easily in build.zig, for example:

In library:
```zig
const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode for a foo_bar library") orelse .static; // or other default

const lib = b.addLibrary(.{
    .linkage = linkage,
    .name = "foo_bar",
    .root_module = mod,
});
```

In consumer:
```zig
const dep_foo_bar = b.dependency("foo_bar", .{
    .target = target,
    .optimize = optimize,
    .linkage = .static // or dynamic
});

mod.linkLibrary(dep_foor_bar.artifact("foo_bar"));
```

It also matches nicely with `linkLibrary` name.

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
2025-01-22 02:29:21 +00:00
Andrew Kelley
f5485a52bc reject crti.o/crtn.o, embrace the future
crti.o/crtn.o is a legacy strategy for calling constructor functions
upon object loading that has been superseded by the
init_array/fini_array mechanism.

Zig code depends on neither, since the language intentionally has no way
to initialize data at runtime, but alas the Zig linker still must
support this feature since popular languages depend on it.

Anyway, the way it works is that crti.o has the machine code prelude of
two functions called _init and _fini, each in their own section with the
respective name. crtn.o has the machine code instructions comprising the
exitlude for each function. In between, objects use the .init and .fini
link section to populate the function body.

This function is then expected to be called upon object initialization
and deinitialization.

This mechanism is depended on by libc, for example musl and glibc, but
only for older ISAs. By the time the libcs gained support for newer
ISAs, they had moved on to the init_array/fini_array mechanism instead.

For the Zig linker, we are trying to move the linker towards
order-independent objects which is incompatible with the legacy
crti/crtn mechanism.

Therefore, this commit drops support entirely for crti/crtn mechanism,
which is necessary since the other commits in this branch make it
nondeterministic in which order the libc objects and the other link
inputs are sent to the linker.

The linker is still expected to produce a deterministic output, however,
by ignoring object input order for the purposes of symbol resolution.
2025-01-20 20:59:52 -08:00
Andrew Kelley
24bdb42a23 test/link/wasm/shared-memory: update to better linker behavior
now it's smarter about omitting tls stuff if there end up being no
TLS data sections
2025-01-15 19:44:20 -08:00
Andrew Kelley
744bb5d16a wasm linker: change rules about symbol visibility
export by default means export, as expected. if you want hidden
visibility then use hidden visibility.
2025-01-15 18:31:44 -08:00
Andrew Kelley
9dd6efb7e4 wasm linker: fix TLS data segments
fix calculation of alignment and size

include __tls_align and __tls_size globals along with __tls_base

include them only if the TLS segment is emitted

add missing reloc logic for memory_addr_tls_sleb

fix name of data segments to include only the prefix
2025-01-15 18:17:37 -08:00
Andrew Kelley
204e689bdb tests: remove dead code 2025-01-15 15:11:37 -08:00
Andrew Kelley
42602dc96b test/link/wasm/export-data: update expected behavior
Object being linked has neither functions nor globals named "foo" or
"bar" and so these names correctly fail to be exported when creating an
executable.
2025-01-15 15:11:37 -08:00
Andrew Kelley
50d053f31c test/wasm/infer-features: update to expected behavior
I intentionally simplified the target features functionality to use the
target features that are explicitly specified to the linker and ignore
the "tooling conventions"

this makes the wasm linker behave the same as ELF, COFF, and MachO.
2025-01-15 15:11:37 -08:00
Andrew Kelley
ec5fc6a2a8 test/link/wasm/function-table: delete bad test
this tests for importing a function table, but the example source does
not try to use an imported table, so it's a useless check. it's unclear
what the behavior is even supposed to do in this case.

the other two cases are left alone.
2025-01-15 15:11:37 -08:00
Andrew Kelley
ae119e395a test/link/wasm/segment: delete bad test
- doesn't run the exe
- checks for data segment named .rodata which is not a thing
- checks for data segment named .bss which is not needed
2025-01-15 15:11:37 -08:00
Andrew Kelley
c565191f30 test/link/wasm/type: remove redundant tests
This test passes now, but let's not run it for the other optimization
modes since they don't affect linker behavior.
2025-01-15 15:11:36 -08:00
Andrew Kelley
9143575ec3 test/wasm/export: delete redundant tests
the other optimization modes don't affect linking
2025-01-15 15:11:36 -08:00
Andrew Kelley
7ae2f21e2b delete bad linker test: bss
The purpose of this test is unclear. It checks for the existence of bss
section which is completely unnecessary since those zeroes can be
omitted from the binary.

Furthermore the code generated for __wasm_init_memory looks wrong.
Finally, the CheckObject DSL is brittle, it only checks for exact
matches of entire lines in an ad-hoc text format. Conclusion, it's a bad
test, delete it.
2025-01-15 15:11:36 -08:00
Andrew Kelley
c535422423 wasm linker: implement hidden visibility 2025-01-15 15:11:36 -08:00
mlugg
7408679234
compiler: disallow callconv etc from depending on function parameters
Resolves: #22261
2024-12-18 23:06:35 +00:00
mlugg
82659c4594
test-link: migrate from deprecated std.Build APIs 2024-12-18 01:49:10 +05:00
mlugg
3aa8020904
std.Build.Step.Compile.Options: change root_module field type to *Module 2024-12-18 01:47:51 +05:00
Alex Rønne Petersen
24ecf45569
std.Target: Add Os.HurdVersionRange for Os.Tag.hurd.
This is necessary since isGnuLibC() is true for hurd, so we need to be able to
represent a glibc version for it.

Also add an Os.TaggedVersionRange.gnuLibCVersion() convenience function.
2024-11-24 22:11:16 +01:00
Jakub Konka
6ff267dc26 link/Elf: ensure we always sort all relocations by r_offset in -r mode
According to a comment in mold, this is the expected (and desired)
condition by the linkers, except for some architectures (RISCV and
Loongarch) where this condition does not have to upheld.

If you follow the changes in this patch and in particular doc comments
I have linked the comment/code in mold that explains and implements
this.

I have also modified `testEhFrameRelocatable` test to now test both
cases such that `zig ld -r a.o b.o -o c.o` and `zig ld -r b.o a.o -o
d.o`. In both cases, `c.o` and `d.o` should produce valid object
files which was not the case before this patch.
2024-10-29 17:27:42 -07:00
Andrew Kelley
2530d638d6 more helpful message when testTlsOffsetAlignment fails 2024-10-23 16:27:39 -07:00
Andrew Kelley
f2efe05155 use deterministic order in relocatable-eh-frame tests
This test does not pass in master branch either if you flip the object
order around.
2024-10-23 16:27:39 -07:00
Andrew Kelley
22948616ff split a fat test case 2024-10-23 16:27:39 -07:00
Andrew Kelley
3cc19cd865 better error messages 2024-10-23 16:27:38 -07:00
Andrew Kelley
4706ec81d4 introduce a CLI flag to enable .so scripts; default off
The compiler defaults this value to off so that users whose system
shared libraries are all ELF files don't have to pay the cost of
checking every file to find out if it is a text file instead.

When a GNU ld script is encountered, the error message instructs users
about the CLI flag that will immediately solve their problem.
2024-10-23 16:27:38 -07:00
Andrew Kelley
5b016e290a move ld script processing to the frontend
along with the relevant logic, making the libraries within subject to
the same search criteria as all the other libraries.

this unfortunately means doing file system access on all .so files when
targeting ELF to determine if they are linker scripts, however, I have a
plan to address this.
2024-10-23 16:27:38 -07:00
Andrew Kelley
2857a3bcb6 link.Elf tests: update for new static lib behavior
the new code in this branch correctly only stores basenames in the
static archive; update the test to reflect that.
2024-10-10 14:21:52 -07:00
Andrew Kelley
9fa0a44de9 linker tests: avoid trivially unnecessary allocation 2024-10-10 14:21:52 -07:00
Jakub Konka
33401ff3bb test/link/macho: test reporting undefined special symbols 2024-10-09 21:39:45 +02:00
Andrew Kelley
22661f3d67 link tests: add a way to check prefix and use it 2024-10-08 21:57:08 -07:00
Andrew Kelley
31d70cb1e1 link.Elf: avoid needless file system reads in flush()
flush() must not do anything more than necessary. Determining the type
of input files must be done only once, before flush. Fortunately, we
don't even need any file system accesses to do this since that
information is statically known in most cases, and in the rest of the
cases can be determined by file extension alone.

This commit also updates the nearby code to conform to the convention
for error handling where there is exactly one error code to represent
the fact that error messages have already been emitted. This had the
side effect of improving the error message for a linker script parse
error.

"positionals" is not a linker concept; it is a command line interface
concept. Zig's linker implementation should not mention "positionals".
This commit deletes that array list in favor of directly making function
calls, eliminating that heap allocation during flush().
2024-10-08 18:02:59 -07:00
Alex Rønne Petersen
a99c883cf0 test: Update wasm/infer-features based on what LLVM produces. 2024-09-19 18:20:21 -07:00
Jakub Konka
6ec8b15918 elf: fix emitting static lib when ZigObject is present 2024-09-04 13:34:26 +02:00
Jakub Konka
8c76a61ef5 test/link/elf: test --gc-sections on Zig input with LLVM too 2024-09-04 13:34:26 +02:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00
mlugg
a3a737e9a6
lib,test,tools,doc: update usages of @export 2024-08-27 00:44:35 +01:00
Alex Rønne Petersen
a69f55a7cc
std.{coff,elf}: Remove the {MachineType,EM}.toTargetCpuArch() functions.
These are fundamentally incapable of producing accurate information for reasons
I've laid out in #20771. Since our only use of these functions is to check that
object files have the correct machine type, and since #21020 made
`std.Target.to{Coff,Elf}Machine()` more accurate, just switch these checks over
to that and compare the machine type tags instead.

Closes #20771.
2024-08-23 19:56:24 +02:00
Jakub Konka
fa79f53f92 test/macho: simplify testing range extension thunks 2024-08-22 00:36:47 +02:00
Jakub Konka
ca417e1e32 link/elf: simplify how we test thunks 2024-08-21 23:05:28 +02:00
Jakub Konka
9473d76449 test/elf: enhance testImportingDataDynamic 2024-08-15 21:47:34 +02:00
Jakub Konka
24126f5382 elf: simplify output section tracking for symbols 2024-07-30 10:00:50 +02:00
Jakub Konka
c575e3daa4 elf: resolve COMDATs in more parallel-friendly way 2024-07-30 10:00:50 +02:00