Commit graph

122 commits

Author SHA1 Message Date
Alex Rønne Petersen
a8e77b7a05
test: disable test-link on big-endian hosts
https://github.com/ziglang/zig/issues/25961
2025-11-19 01:42:45 +01:00
skewb1k
26db54d69b zig fmt: fix extra whitespace in StructInit with multiline strings
68d2f68ed introduced special handling for StructInit fields
containing multiline strings to prevent inserting whitespace after =.
However, this logic didn't handle cases without a trailing comma,
which resulted in unwanted trailing whitespace.
2025-11-05 14:07:30 +02:00
alexrp
b2d2b441b2
test: disable test-link on FreeBSD
https://github.com/ziglang/zig/issues/25323
2025-09-22 01:37:32 +02:00
Jacob Young
09bc118c9f Elf: implement linksection
Closes #24330
2025-09-17 21:16:23 -07:00
Carl Åstholm
154bd2fd05 Migrate from deprecated Step.Compile APIs 2025-07-26 12:06:43 +02:00
Andrew Kelley
d8e26275f2 update standalone and incremental tests to new API 2025-07-07 22:43:53 -07:00
Andrew Kelley
0e37ff0d59 std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API

make std.testing.expectFmt work at compile-time

std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.

Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
  - anytype -> *std.io.Writer
  - inferred error set -> error{WriteFailed}
  - options -> (deleted)
* std.fmt.Formatted
  - now takes context type explicitly
  - no fmt string
2025-07-07 22:43:51 -07:00
Alex Rønne Petersen
b3537d0f4a compiler: Allow configuring UBSan mode at the module level.
* Accept -fsanitize-c=trap|full in addition to the existing form.
* Accept -f(no-)sanitize-trap=undefined in zig cc.
* Change type of std.Build.Module.sanitize_c to std.zig.SanitizeC.
* Add some missing Compilation.Config fields to the cache.

Closes #23216.
2025-04-26 22:54:34 +02:00
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
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
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
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
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
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
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
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
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
Jakub Konka
fa09276510 test/link/elf: test COMDAT elimination 2024-07-30 10:00:50 +02:00
Jakub Konka
66253e5b51 link/elf: populate current Zig version in .comment; test 2024-04-20 23:36:42 +02:00
Jakub Konka
bc0d84be0b test/link/elf: add merge strings test for -r mode 2024-04-20 23:36:42 +02:00
Jakub Konka
342235e5eb test/link/elf: enhance merge strings tests 2024-04-20 23:36:42 +02:00
Jakub Konka
457d84f45a link/elf: remove link.link.build as unused; add some merge strings tests 2024-04-20 23:36:42 +02:00
Jakub Konka
7ba2453b8e test/link/elf: do not check for $thunk in thunks tests for now
Andrew and I have discovered that on Linux max peak rss value
is taken to be `max(build_runner, test_suite)` and since the thunks
test emit a huge binary, we will easily exceed the declared maximum
for any of the test suites. This can be worked around for now by not
checking for $thunk symbols in this test since it doesn't really
yield any additional information; however ideally we would implement
per-thread local temp arena that can be freed.
2024-03-13 08:25:38 +01:00
Jakub Konka
faa4bdb017 elf+aarch64: fix off-by-one in converging on groups interleaved with thunks 2024-03-12 13:16:11 +01:00
Jakub Konka
b1eba5a996 elf+aarch64: actually write out thunks, and add a proper link test 2024-03-12 13:16:11 +01:00
Tristan Ross
c260b4c753
std.builtin: make global linkage fields lowercase 2024-03-11 07:09:10 -07:00
Jakub Konka
1a6b2e84ac test/link/elf: remove pie hack due to QEMU bug that is now fixed 2024-03-10 10:39:31 +01:00
Jakub Konka
b7b63855d0 test/link/elf: enable tests for aarch64 cpu arch 2024-03-09 19:26:26 +01:00
Jakub Konka
ddb33baa11 test/link/elf: test static linking C riscv64-musl 2024-02-21 23:08:29 +01:00
Jakub Konka
55dadc3d85 test/link/elf: test static linking C aarch64-musl 2024-02-21 22:40:03 +01:00
Jakub Konka
d93a0763d4 test/link/link: pass build options to elf and macho tests 2024-01-24 12:34:39 +01:00
Jakub Konka
aa50bca151 test/link/elf: make invalid input file test less janky 2024-01-24 12:34:39 +01:00
Jakub Konka
b1ffc2b8b3 test/link/elf: patch up relocatable test 2024-01-15 00:18:50 +01:00
Jakub Konka
d7c2324cdb test/link/elf: trigger build system bug testing relocatable mode 2024-01-14 20:51:03 +01:00