Commit Graph
154 Commits
Author SHA1 Message Date
Alex Rønne Petersen 71590ad8c9 std.elf: fix Elf64_auxv_t layout on NetBSD and OpenBSD 2026-10-04 18:33:19 +02:00
Matthew Lugg 9101d0395e Elf2: implement GNU symbol versioning
Resolves: https://codeberg.org/ziglang/zig/issues/36665
2026-09-22 12:31:01 +01:00
Alex Rønne Petersen 5dfebdcc07 std.elf: add missing AT_* constants for linux 2026-09-07 01:29:52 +02:00
Alex Rønne Petersen 85953a78a3 std.elf: add AT_* constants for more OSs and namespace them properly 2026-09-07 01:29:49 +02:00
Alex Rønne Petersen 4f15073e4f std.elf: update hash structures for the 64-bit variant 2026-08-16 10:37:20 +01:00
Matthew Lugg c7a169280d std.elf: add definitions for SHT_HASH header and hash function 2026-08-16 09:30:30 +01:00
David Senoner 9bc9544bc8 elf: remove deprecated Phdr structs 2026-07-30 21:23:58 +02:00
David Senoner 574c80351d elf: remove usages of deprecated Elf64_Phdr 2026-07-30 21:23:58 +02:00
David Senoner 4843120b4c elf: remove usages of deprecated Elf32_Phdr 2026-07-30 21:23:58 +02:00
David Senoner ea006188aa elf: remove all deprecated usages and definitions of std.elf.PT_* 2026-07-30 21:23:58 +02:00
Jacob Young 39c5d3a205 Elf2: start implementing archives
Allows building static libraries with the new linker.
2026-07-27 20:28:07 +02:00
Justus Klausecker 69a26976ed zig fmt
Converts usages of `@intFromEnum` and `@enumFromInt` to instead use the
new `@backingInt` and `@fromBackingInt` builtins.
2026-07-17 23:30:26 +02:00
Matthew Lugg 58a94eaae2 Elf2: big refactors and enhancements
These mainly concern relocation handling. This introduces a way to
represent most relocation types---those which write to a single
bit-field in a 8/16/32/64-bit backing integer---in a target-agnostic
manner, without tons of copy-and-pasted logic. It isn't quite as
generalized as what GNU ld does, but it can still handle *most* common
cases, and it represents these cases in 16 bits of state without any
lookup table.

I've documented the new relocation types in detail in doc comments on
`SymbolReloc.Type`, so take a look at those if you're interested. Also
see the new code in `addRelocAssumeCapacity`, which is responsible for
mapping the ELF relocation enums to this system.

The logic for emitting runtime relocations has been greatly simplified.
It no longer requires any target-specific logic, because even on targets
with many complex static relocations, there are usually only a handful
of dynamic relocations, so the code emitting them can quite easily be
abstracted across target architectures.

More generally, target-specific logic has been cleaned up and pulled
together to make it easier to add support for new targets. For instance,
a new function `targetPltInfo` is introduced which just returns a bunch
of information about the structure of the PLT on this particular target.
(I also filled in a bit more target-specific logic, e.g. lowerings for a
few relocations and some missing cases in `MachineRelocType`.)

`ehdrField` is replaced with more specialized functions, which return
slightly-modified enum types with impossible tags omitted. This makes it
much easier to use exhaustive `switch` statements in the linker when
branching on things like the target ELF machine.

The linker now has basic detection and error reporting for misaligned or
overflowed relocation values. The error reporting isn't very good yet
(you just get told that the overflow/misalignment happened and for how
many relocations), but it's there!

There are probably a few other smaller refactors and bugfixes here which
I don't remember. Awfully sorry to throw all of this in one commit, I
kept finding yaks to shave mid-way through the relocation type stuff!

Resolves: https://codeberg.org/ziglang/zig/issues/36066
2026-07-17 15:57:25 +02:00
Alex Rønne Petersen e863bf3bea std.elf.EM: add AIECTRLCODE (269) and PPU (270) 2026-07-17 00:41:11 +02:00
Alex Rønne Petersen c7d5569546 std.elf: add helper methods on CLASS and DATA 2026-07-08 18:08:25 +02:00
Alex Rønne Petersen 58fb611589 std.elf: add sparc.EFlags 2026-07-08 18:08:25 +02:00
Alex Rønne Petersen baf6327973 std.elf: add R_SPARC 2026-07-08 18:08:25 +02:00
xtex 360deedcf1 std.elf: add LoongArch e_flags definition
Link: https://github.com/loongson/la-abi-specs/blob/44ef5f63e1755e1a165915ea3b2d98d35f68eed0/laelf.adoc?plain=1#L74-L93
2026-06-25 22:12:55 +08:00
xtex e048c90545 std.elf: add R_LARCH relocations 2026-06-25 22:06:24 +08:00
Krzysztof Wolicki 9e80795623 all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
Sertonix 8455563ed8 Fix build script compilation when usize is u32
On 32-bit systems usize is not u64 but it was assumed in multiple places
of the build script.

Sometimes using u64 instead of usize since available and used memory can
exceed usize on 32-bit systems (just like totalSystemMemory)

Example errors:

lib/compiler/build_runner.zig:508:26: error: expected type 'usize', found 'u64'
        .available_rss = max_rss,
                         ^~~~~~~
lib/compiler/build_runner.zig:508:26: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
referenced by:
    callMain [inlined]: lib/std/start.zig:699:88
    callMainWithArgs [inlined]: lib/std/start.zig:638:20
    posixCallMainAndExit: lib/std/start.zig:590:38
    2 reference(s) hidden; use '-freference-trace=5' to see all references
lib/std/Build/WebServer.zig:849:38: error: expected type 'usize', found 'u64'
    const buf = gpa.realloc(old_buf, new_len) catch @panic("out of memory");
                                     ^~~~~~~
lib/std/Build/WebServer.zig:849:38: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
lib/std/mem/Allocator.zig:399:58: note: parameter type declared here
pub fn realloc(self: Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(old_mem) {
                                                         ^~~~~
build.zig:548:10: error: type 'usize' cannot represent integer value '9300000000'
        .max_rss = 9_300_000_000,
        ~^~~~~~~~~~~~~~~~~~~~~~~
build.zig:778:10: error: type 'usize' cannot represent integer value '8000000000'
        .max_rss = 8_000_000_000,
        ~^~~~~~~~~~~~~~~~~~~~~~~
2026-05-26 09:43:39 -07:00
Alex Rønne Petersen c96d1cc0c1 std.elf: add DT_XTENSA_GOT_LOC_{OFF,SZ} 2026-05-26 08:11:04 +02:00
Matthew Lugg 55cdf6c28b std.elf: add Ident struct
This is much nicer than indexing a raw 16-byte buffer.
2026-05-17 18:08:57 +01:00
Matthew Lugg fdac89d6cd remove uses of array multiplication
In preparation for its removal as accepted in
https://github.com/ziglang/zig/issues/24738.
2026-04-30 08:57:51 +01:00
Matthew Lugg 792830d69c std: work around language changes 2026-03-10 10:26:07 +00:00
Marcel W. Wysocki 0e3c6514a4 link: recognize thin archives in ld script detection
Needed for linking the Linux kernel.
2026-02-17 23:15:32 +01:00
Adrià Arrufat 02c5f05e2f std: replace usages of std.mem.indexOf with std.mem.find 2025-12-05 14:31:27 +01:00
Giuseppe Cesarano 0b5b35c696 std.elf implemented DynamicSectionBufferIterator 2025-11-26 11:30:03 +01:00
Jacob Young 57889cae80 posix: reduce the number of assumptions made by dl_iterate_phdr
Not yet fully compatible with the new linker, but still progress.

Closes #25786
2025-11-09 03:31:26 -05:00
Jacob Young 80c961159b Elf2: elide unused nodes when emitting objects 2025-11-09 03:31:26 -05:00
Jacob Young 7542c3260f Elf2: load relocations from input objects 2025-10-29 18:06:17 -04:00
Andrew Kelley 47aa5a70a5 std: updating to std.Io interface
got the build runner compiling
2025-10-29 06:20:48 -07:00
Andrew Kelley 066864a0bf std.zig.system: upgrade to std.Io.Reader 2025-10-29 06:20:48 -07:00
mlugg 9901b9389e std: fix 32-bit build and some unsafe casts 2025-09-30 13:44:53 +01:00
Jacob Young f58200e3f2 Elf2: create a new linker from scratch
This iteration already has significantly better incremental support.

Closes #24110
2025-09-21 14:09:14 -07:00
Alex Rønne Petersen a82f446d91 std.elf: change STV enum from u2 to u3
In gABI 4.3, st_other was changed such that the lower 3 bits are reserved for
the visibility, up from the previous 2 bits.
2025-09-04 01:16:23 +02:00
Alex Rønne Petersen 9e076d8c75 std.elf: add new EM values from gABI 4.3 2025-09-04 01:16:22 +02:00
Alex Rønne Petersen 1ca446abc3 std.elf: document which EM values are not assigned in gABI 2025-09-04 01:13:59 +02:00
Alex Rønne Petersen 1c090d3d67 std.elf: document which OSABI values are not assigned in gABI 2025-09-04 01:13:59 +02:00
Andrew Kelley 79f267f6b9 std.Io: delete GenericReader
and delete deprecated alias std.io
2025-08-29 17:14:26 -07:00
Giuseppe Cesarano 3ee4252183 Fix: std.elf offset computation 2025-08-08 12:47:00 -07:00
Giuseppe Cesarano 3914eaf357 std.elf: buffer header iterator API (#24691)
Closes #24666.
2025-08-05 10:00:33 -07:00
Jacob Young 5060ab99c9 aarch64: add new from scratch self-hosted backend 2025-07-22 19:43:47 -07:00
Andrew Kelley f1576ef14c objcopy: delete most of it
this code is not up to zig project standards

tracked by #24522

oh, and fix not adjusting buffer seek position in std.fs.File.Reader
2025-07-21 12:32:37 -07:00
Andrew Kelley d8e26275f2 update standalone and incremental tests to new API 2025-07-07 22:43:53 -07:00
Shun Sakai 5fc4448e45 chore(std.mem): Rename trimLeft and trimRight
Rename `trimLeft` to `trimStart`, and `trimRight` to `trimEnd`.
`trimLeft` and `trimRight` functions remain as deprecated aliases for
these new names.
2025-04-27 18:03:59 +09:00
geemili 6ef2384c07 std.DynLib: add support for DT_GNU_HASH sections in elf files 2025-02-22 17:33:49 -05:00
Alex Rønne Petersen b541a7af11 std.Target: Remove Cpu.Arch.spu_2.
This was for a hobby project that appears to be dormant for now. This can be
added back if the project is resumed in the future.
2025-02-17 19:17:55 +01:00
Rafael Batiati 33f0d458cf std.elf: fix panic while parsing header
When parsing an invalid (e.g., corrupted) ELF header, `@enumFromInt` can panic casting the exhaustive enum `ET`.
2025-02-11 21:12:44 +00:00
Andrew Kelley 1ba3fc90be link.Elf: eliminate an O(N^2) algorithm in flush()
Make shared_objects a StringArrayHashMap so that deduping does not
need to happen in flush. That deduping code also was using an O(N^2)
algorithm, which is not allowed in this codebase. There is another
violation of this rule in resolveSymbols but this commit does not
address it.

This required reworking shared object parsing, breaking it into
independent components so that we could access soname earlier.

Shared object parsing had a few problems that I noticed and fixed in
this commit:
* Many instances of incorrect use of align(1).
* `shnum * @sizeOf(elf.Elf64_Shdr)` can overflow based on user data.
* `@divExact` can cause illegal behavior based on user data.
* Strange versyms logic that wasn't present in mold nor lld. The logic
  was not commented and there is no git blame information in ziglang/zig
  nor kubkon/zld. I changed it to match mold and lld instead.
* Use of ArrayList for slices of memory that are never resized.
* finding DT_VERDEFNUM in a different loop than finding DT_SONAME.
  Ultimately I think we should follow mold's lead and ignore this
  integer, relying on null termination instead.
* Doing logic based on VER_FLG_BASE rather than ignoring it like mold
  and LLD do. No comment explaining why the behavior is different.
* Mutating the original ELF symbols rather than only storing the mangled
  name on the new Symbol struct.

I noticed something that I didn't try to address in this commit: Symbol
stores a lot of redundant information that is already present in the ELF
symbols. I suspect that the codebase could benefit from reworking Symbol
to not store redundant information.

Additionally:
* Add some type safety to std.elf.
* Eliminate 1-3 file system reads for determining the kind of input
  files, by taking advantage of file name extension and handling error
  codes properly.
* Move more error handling methods to link.Diags and make them
  infallible and thread-safe
* Make the data dependencies obvious in the parameters of
  parseSharedObject. It's now clear that the first two steps (Header and
  Parsed) can be done during the main Compilation pipeline, rather than
  waiting for flush().
2024-10-12 10:44:17 -07:00