0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
Commit graph

18,387 commits

Author SHA1 Message Date
Mathieu Suen
ba0cfda93d chore: Air, Unify type of ty for Data payload 2026-08-19 08:02:13 +02:00
Pavel Verigo
7051f8e73b wasm: add --growable-table 2026-08-18 17:43:59 +02:00
xtex
8e2d6e211b Elf2: fix default page alignment of LoongArch 2026-08-18 09:03:05 +02:00
levinqua
f73d7aa182 tank/tapi/parse.zig: prefer ensureUnusedCapacity over ensureTotalCapacity
ensureUnusedCapacity should be preferred over ensureTotalCapacity.
By using ensureTotalCapacity the parsing could fail the assumption that there is enough unused capacity if node.string_value would already have elements.
However afaict both functions were only called with string_value = .empty. Therefore behavior should remain unchanged.
2026-08-18 05:03:00 +02:00
Jacob Young
7f48903561 llvm: fix instrinsics allowed special case not checking abi
Closes #36505
2026-08-17 18:09:25 -04:00
Pavel Verigo
6716bf52e7 Merge pull request 'wasm: enable incremental tests for selfhosted' (#36534) from pavelverigo/zig:wasm-incr-tests-enable into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36534
2026-08-17 11:08:23 +02:00
mlugg
7b02ab7588 Merge pull request 'Elf2: many enhancements' (#36528) from mlugg/elf2-again into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36528
2026-08-17 10:23:45 +02:00
Pavel Verigo
46af37a1ad wasm: fix and test when dead function emitted after incremental update 2026-08-16 23:23:57 +02:00
Jan Procházka
75044cb04c llvm: synchronize target initialization 2026-08-16 22:19:28 +02:00
Pavel Verigo
23541774d6 wasm: enable incremental tests for selfhosted
Closes #31810
2026-08-16 19:01:56 +02:00
Matthew Lugg
a402fdc813
Elf2: fix 32-bit build 2026-08-16 11:26:24 +01:00
Matthew Lugg
e9e0ce6c60
Elf2: fix exporting UAVs which are otherwise dead 2026-08-16 10:37:20 +01:00
Matthew Lugg
ede0017eb9
Elf2: include the ehdr in the rodata segment
Although to my knowledge this is not strictly required by the format or
by any OS, it is highly conventional, and not doing so can definitely
break things in practice---including one of the standard library tests!

To achieve this, we add all of the segment nodes *before* the ehdr,
initializing rodata as a fixed "header" node in the `.elf` node, and
then the `.ehdr` node goes within that rodata segment node.
2026-08-16 10:37:20 +01:00
Matthew Lugg
507f2d0ab1
MappedFile: fix bug in growing fixed nodes 2026-08-16 10:37:20 +01:00
Alex Rønne Petersen
8a539f903b
Elf2: support the 64-bit hash table format
This is used on s390x and alpha. While `Elf2` doesn't yet support either
of those targets, we'll probably work on s390x soon, so it's worth
getting this support in now.
2026-08-16 10:37:20 +01:00
Matthew Lugg
a2ddff74f8
Elf2: fix incorrect validation in SPARC relocation
It turns out that SPARC has a strange definition of "PC-relative" where
the offsets are... unsigned? This results in some annoying restrictions
when linking: specifically, the GOT needs to appear after all code
segments, because these PC-relative relocations are used to access it.
When violating that, we should at least get relocation errors, but
because we were doing the wrong validation here, we were just triggering
miscompilations instead.
2026-08-16 10:37:19 +01:00
Matthew Lugg
58c84c59c0
Elf2: add segment for PLT on SPARC
On SPARC, because JUMP_SLOT relocations write directly to the PLT, the
PLT is required to be RWX. Conventionally, this is achieved by placing
it in the "data" segment and making that RWX instead of RW, but that
just seems unnecessarily dangerous. Instead, let's try making a segment
specifically for the PLT.
2026-08-16 10:37:19 +01:00
Matthew Lugg
99e54fc420
Elf2: many alignment fixes
Notably, this ensures that loadable segments are well-aligned in all
cases, and does so *without* requiring segments to have well-aligned
file offsets (which would result in needless bloat in the ELF file). It
also fixes a bunch of small alignment bugs across the backend, and adds
correct handling for the case of an input section / NAV / UAV forcing a
section, and possibly even segment, to increase its alignment.

Resolves: https://codeberg.org/ziglang/zig/issues/32135
2026-08-16 10:37:19 +01:00
Matthew Lugg
bd805e82b9
Elf2: populate fixed bits in SPARC relocations 2026-08-16 10:37:19 +01:00
Matthew Lugg
594a6d5665
Elf2: fix endianness bug in GOT 2026-08-16 10:37:19 +01:00
Matthew Lugg
52dd990f80
Elf2: PLT fixes 2026-08-16 10:37:19 +01:00
Matthew Lugg
67e525d546
Elf2: fix typo in relocation logic 2026-08-16 10:37:19 +01:00
Matthew Lugg
73a65674e4
link.MappedFile: clarify some logic
No functional changes, just a small refactor so I can properly
understand what's happening in this logic.
2026-08-16 10:37:19 +01:00
Matthew Lugg
c7fed8d0c6
Elf2: implement .hash
This section, of type `SHT_HASH`, is a hash table used for looking up
symbol names in DSOs. It is referenced in `.dynamic` by the `DT_HASH`
entry. Without this, glibc assumes we export no symbols, and musl
refuses to load our DSOs at all!

Modern ELF systems broadly consider `SHT_HASH`/`DT_HASH` deprecated in
favour of `SHT_GNU_HASH`/`DT_GNU_HASH`, another hash table with a
different format which is more efficient for misses. However, as with so
many things in ELF, GNU's hash table format seems to be carefully
engineered to make incremental linking as difficult as possible. Every
dynamic linker out there continues to support `DT_HASH`, which actually
plays pretty nicely with incremental compilation, so I think we'll stick
with `DT_HASH` when doing incremental links.

I would now like to take a moment to complain about these hash tables.

These tables exist in order to make symbol lookups efficient when
applying relocations. The ldso implementations for both glibc and musl
libc work by iterating all relocations, and, for each one, looking up
the target symbol in each candidate DSO based on their `DT_HASH` or
`DT_GNU_HASH` table. The `DT_GNU_HASH` format came about to speed up
this lookup, since it's happening very often, so a small speedup can
lead to major improvements in load time.

However, I don't understand why nobody first solved the really obvious
inefficiency: every single symbol lookup is doing N different hash table
lookups where N is the number of loaded DSOs! A clearly better approach
for a dynamic linker to take is to have a single global hash table, and
every time a DSO is loaded, to add its symbols to that table. Then each
symbol lookup requires only *one* hash map lookup instead of N! Plus,
this change doesn't affect ELF files at all---it could be made in libc
implementations tomorrow. Yes, you need to iterate each DSO's dynamic
symbol table once in full, but that shouldn't be a big deal when you're
already iterating their *relocations* (those are far more numerous than
dynamic symbols!).

Here's another idea for free---after building the hash map, iterate
everyone's dynamic symbol table a second time, and build a lookup table
from their dynamic symbol index to the resolved symbol in the hash map.
Now relocation application doesn't even need a hash map; just a lookup
table access! Maybe this one isn't worthwhile, since the difference
between one LUT access and one hash map lookup isn't enough to warrant
the extra allocation and iteration, but it seems worth a try...
computers are fast, y'know!

What drives me a little crazy is that when someone ran into performance
problems with relocation application, instead of just spending an hour
speeding up lookups in ldso, they chose to invent lazy PLT binding. In
doing so, they opted to inflict needless complexity, unpredictable
runtime performance characteristics, unreportable failures, and security
risks in the form of a mutable jump table, upon every ELF system, for
ever. But I guess at least no poor soul had to implement a hash map.

But fine, let's look at status quo, and compare the standard `SHT_HASH`
format with `SHT_GNU_HASH`. As we all know, GNU's track record terms of
high-quality contributions to the ELF ecosystem is flawless, so there
must have been some good reasons for the changes.

Some small things which seem fine: they changed the hash function, which
I'll assume was with fair reason, and they added a bloom filter to allow
lookups to fail early (important if you accept the design of looking up
symbol names in individual DSOs).

The next change was to add a header field called `symoffset`, so that
the hash table can avoid wasting space on chains for the first N symbol
table entries which aren't actually global symbols. Unfortunately, it
seems that someone missed a memo, because this field, in a format which
debuted in 2006, has been redundant since the mid-90s. ELF files pretty
much universally use a separate `SHT_DYNSYM` section for the dynamic
symbol table, with the express purpose of *omitting* all the `STB_LOCAL`
symbols to save space. If a symbol doesn't appear in the hash table, it
shouldn't be in the dynamic symbol table to begin with. This quirk of
the `SHT_GNU_HASH` section isn't particularly *offensive*, it's just...
not useful.

Then the actual hash table representation. The "chains" array no longer
forms a linked list; instead, `chains[sym_idx]` now holds the *hash* for
the symbol's name. The linked list is eliminated in favour of an
assumption that symbols in the same bucket are contiguous in the symbol
table, and the least-significant bit of `chains[sym_idx]` is repurposed
to indicate the last symbol in a bucket (so you know when to stop
iterating).

In general I'm all for replacing linked lists with arrays, but this
particular change is really annoying. Setting aside the fact that it's
kinda odd for metadata *about* the symbol table to mandate a specific
ordering *within* the symbol table, this rigid ordering requirement is
also terrible for incremental linkers! It means that adding or removing
a symbol requires shifting potentially the entire symbol table up or
down to make sure the new symbol is in the right place, unless you're
happy having gaps all over the table (which I've been trying to avoid in
`Elf2`). I'm sure the contiguous-symbols assumption and the caching of
the hashes does improve lookup performance, but it seems like a far more
complicated solution than just doing some kind of caching in ldso
implementations. How this became the accepted solution to slow symbol
lookups is truly beyond me.
2026-08-16 10:19:44 +01:00
Matthew Lugg
44449730c7
Elf2: fix crashes when emitting dynamic libraries 2026-08-16 09:30:29 +01:00
Matthew Lugg
c6d7ab8a23
Elf2: delete some dead code 2026-08-16 09:30:27 +01:00
Matthew Lugg
31500242f0 link: rework export API
The old API fundamentally did not make sense, because the frontend does
not track exports incrementally: it only knows the set of referenced
exports at the end of each incremental update. Attempting to represent
this as an incremental add/remove-style API was just a lie. It was also
buggy; `deleteExport` was called from the main thread, leading to races
in incremental compilation if `deleteExport` was actually implemented!

We also exposed data in an unnecessarily complicated way which did not
benefit most linkers. The only one which actually cared about knowing
all exports for a given nav/uav upfront was the C linker (which here I
have written to reconstruct that data), and even it can probably be
refactored at some point to avoid that.

So, replace the export API with a simpler one, where all the frontend
does is tell the linker about every `Zcu.Export.Index` which is alive,
just before calling `flush`. In theory this information could actually
be passed into `flush`, but using a separate function made for a simpler
implementation in practice.

Resolves: https://github.com/ziglang/zig/issues/23616
2026-08-16 10:29:59 +02:00
Marcel W. Wysocki
e25015bdce
cc: add --wrap=, --nmagic, --fatal-warnings, and -m linker args
Needed for linking the Linux kernel.
2026-08-16 08:41:27 +02:00
lg
767d252691 llvm: don't scalarize bitcast vectors on LE (#36512)
`scalarize_bit_cast_vector_non_elementwise` was previously unconditionally
enabled on both big-endian and little-endian targets, which led to
suboptimal emitting of vector-to-integer bitcasts (https://zig.godbolt.org/z/qEG8zqfqn). This patch only
enables it on big-endian targets as the little-endian semantics match
the zig semantics.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36512
2026-08-16 04:06:29 +02:00
Richalsu
f506a49142 std.Io: add error.ConnectionTimedOut for net read/write/send/receive 2026-08-15 10:46:35 +02:00
Guy Fischman
a72236ae8e compiler: lower array-to-vector coercion in the backend 2026-08-15 10:38:52 +02:00
Ryan Liptak
7b890ff978 Fix Compilation.Path checks when one path is a root path
Previously, it was assumed that the byte after the cwd/outer path had to be a path separator, but that's not true when the cwd/outer path is a root with no components (e.g. `/` or `C:\`)

Fixes #32005
2026-08-14 12:25:13 +02:00
Andrew Kelley
2e77ff7ca9 zig build: lock stderr before replacing process image
fixes not clearing `std.Progress` output before executing maker
2026-08-13 11:32:46 +02:00
Techatrix
3ec6d655dd fix compilation errors when building compiler for wasm32-wasi 2026-08-13 02:03:22 +02:00
Andrew Kelley
89e0881f11 remove bad advice from @import("builtin")
also update doc comments on `std.lang.CompilerBackend`.
2026-08-11 20:51:12 -07:00
Pavel Verigo
e02750fc0b wasm: add __global_base/__wasm_first_page_end handling + init_funcs fix 2026-08-11 20:00:12 +02:00
Pavel Verigo
cce0b43270 wasm: change symbol name for function if it uses library name
Fixes bug when `fn "js" log(...)` collide with compiler-rt `log` symbol, to fix I reuse logic/idea from llvm backend.
2026-08-11 20:00:12 +02:00
Miloš Kozák
a6420d9985 llvm: restore debug location scope after generating a nested body
Commit d28006153e dropped the deferred restore of the debug location's
scope in genBodyDebugScope. Since then, instructions emitted after a
nested body inherit the body's lexical block as their location scope.

This breaks variable info for locals initialized by a try expression:
their dbg_var_ptr is emitted right after the try's block, so the
dbg.declare gets a location scoped to the inner lexical block, and LLVM
places the variable's DIE inside a DW_TAG_lexical_block whose ranges end
with that block. Debuggers then consider the variable out of scope for
the rest of the function.

Closes #30705
2026-08-11 10:25:26 +02:00
rpkak
de207594e4 InternPool: don't differentiate between positive and negative zero for ints (#36451)
Added a test, which fails on master.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36451
Reviewed-by: mlugg <mlugg@mlugg.co.uk>
2026-08-11 10:22:28 +02:00
Matthew Lugg
83b57947e3 link: don't include static libraries in other static libraries
Follow-up to 9aa93a045e, which fixed this
bug for *shared* library inputs, but not *static* library inputs.

Supersedes https://codeberg.org/ziglang/zig/pulls/31383 by fixing the
bug at the compiler level instead of working around it in the build
system. This seems preferable because it is useful to the compiler to
have full information about a compilation's link inputs---for instance
this could interact with https://github.com/ziglang/zig/issues/20654 in
the future by having the compiler learn about a static library's ABI
even if that static library does not ultimately contribute to the link.

Resolves: https://codeberg.org/ziglang/zig/issues/35624
2026-08-09 02:44:15 +02:00
Andrew Kelley
8dfddf95fe std.meta: deprecate fieldInfo, fieldNames, fieldTypes
Technically there is one valid use case for `fieldNames` which is use
with an enum or union so that those types can be used interchangeably.
But in practice these functions are mainly abused, because the callsites
always know what kind of type it is.

This commit encourages Zig users to embrace using `@typeInfo` directly
when doing type reflection.
2026-08-08 19:15:10 +02:00
agave
2785bc3823 llvm: workaround to export associated .kd symbol for kernels on amdgcn 2026-08-08 13:48:58 +02:00
Michael Farber Brodsky
637504176b Sema: handle OPV types in zirSelect and analyzeShuffle
Fixes: https://codeberg.org/ziglang/zig/issues/35969
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35996
Reviewed-by: mlugg <mlugg@mlugg.co.uk>
2026-08-08 10:21:31 +02:00
Rue04
02c49f2911 Sema: fix crash on @as(comptime_int, @floatFromInt(1))
Fixes: https://codeberg.org/ziglang/zig/issues/32147
Co-authored-by: rue04 <rue04@rue04.de>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35663
Reviewed-by: mlugg <mlugg@mlugg.co.uk>
2026-08-08 09:54:04 +02:00
Rue04
94bdde8327 Sema: fix incorrect compile error for @as(u32, @trunc(@floor(runtime_float)))
If I had to guess, `Sema.zirRoundOpType` was probably written when `@trunc` etc. could only have a float as their destination type. As a result, for the ints they can cast to now, they'd expect the expression inside them to be a `comptime_float`, which made `@trunc(@floor(runtime_float))` or `@trunc(@floatFromInt(runtime_int))` impossible.
By changing the returned type in this case to be generic poison, showing we don't know the expected type as *any* float can be converted to an int, `@trunc(@floor(runtime_float))` lowers to effectively `@floor(runtime_float)` and `@trunc(@floatFromInt(runtime_int))` throws the expected error that `@floatFromInt`'s result type is unknown here.

Fixes: https://codeberg.org/ziglang/zig/issues/32111
Co-authored-by: rue04 <rue04@rue04.de>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35664
2026-08-08 09:49:12 +02:00
mlugg
3d35188fe8 Merge pull request 'remove haiku from defaultSingleThreaded' (#32137) from ypsvlq/zig:master into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/32137
Reviewed-by: mlugg <services@mlugg.co.uk>
2026-08-07 19:24:07 +02:00
Ryan Mehri
b9852d2308 Legalize: fix packed struct with OPV and multiple other fields
In legalization for packed struct init with an OPV field, we first try 
to see if any field accounts for the entire bit size of the struct and
otherwise fall back to a sequence of bitcasts and shifts on each field
(added in fc1c83a363). However, OPV fields are not accounted for
in the fallback case, which means that codegen eventually panics when
seeing the bit size of 0.

This change makes it so that we ignore all OPV fields in
`packedAggregateInitBlockPayload` since they have no runtime bits.
2026-08-07 15:29:04 +02:00
Matthew Lugg
17e07ffc63 llvm: represent bool as i8 in memory
Follow-up to https://codeberg.org/ziglang/zig/pulls/35711
2026-08-07 09:32:22 +02:00
Saurabh Mishra
21f2381438 std.ArrayListUnmanaged: last and lastPtr methods (#36318)
ArrayList:

- `getLastOrNull` has been deprecated and renamed to `last`
- `getLast` has been removed in favor of `last` combined with `.?`
- `lastPtr` has been added which returns `?*T`

Upgrade guide:

```zig
if (list.getLastOrNull()) |foo| {
    // ...
}
const foo = list.getLast();
```
⬇️
```zig
if (list.last()) |foo| {
    // ...
}
const foo = list.last().?;
```

Co-authored-by: Ryan Liptak <squeek502@hotmail.com>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36318
Reviewed-by: Ryan Liptak <squeek502@noreply.codeberg.org>
2026-08-07 07:30:10 +02:00
xtex
6e61a77ec8 aarch64: fix Select enum where enum tag is simple type
Tag type can be c_int or something similiar.

Change-Id: I41a03860327774586239c5d523180d85d7cc52d7
2026-08-06 14:55:59 +02:00