157 Commits

Author SHA1 Message Date
Andrew Kelley
bc2cf0c173 eliminate all uses of std.io.Writer.count except for CBE 2025-07-09 09:32:07 -07:00
Andrew Kelley
30c2921eb8 compiler: update a bunch of format strings 2025-07-07 22:43:52 -07:00
Andrew Kelley
d5c97fded5 compiler: fix a bunch of format strings 2025-07-07 22:43:52 -07:00
Andrew Kelley
ec3b5f0c74 compiler: upgrade various std.io API usage 2025-07-07 22:43:52 -07:00
Andrew Kelley
941bc37193 compiler: update all instances of std.fmt.Formatter 2025-07-07 22:43:52 -07:00
Jacob Young
0bf8617d96 x86_64: add support for pie executables 2025-06-06 23:42:14 -07:00
Андрей Краевский
aac800ec65
std.meta.FieldType -> @FieldType 2025-03-27 03:49:38 +00:00
David Rubin
914f822025 link.Elf: implement non-alloc {SET,SUB}_ULEB128 2025-02-11 20:41:52 +01:00
Jacob Young
ac1a975f9b x86_64: implement clz and not 2025-01-16 20:42:08 -05:00
Jacob Young
a1828ebcda x86_64: demolish the old 2025-01-16 20:42:08 -05:00
Andrew Kelley
1a4c5837fe wasm linker: fix crashes when parsing compiler_rt 2025-01-15 15:11:36 -08:00
Jacob Young
6d97185bc6 Elf.Atom: fix truncated dyn abs relocs 2024-12-14 23:20:15 -05:00
Andrew Kelley
2dcfa72376 link.Elf: untangle parseObject and parseArchive
from link.Elf, so that they can be used earlier in the pipeline
2024-10-23 16:27:38 -07: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
Andrew Kelley
13fb68c064 link: consolidate diagnostics
By organizing linker diagnostics into this struct, it becomes possible
to share more code between linker backends, and more importantly it
becomes possible to pass only the Diag struct to some functions, rather
than passing the entire linker state object in. This makes data
dependencies more obvious, making it easier to rearrange code and to
multithread.

Also fix MachO code abusing an atomic variable. Not only was it using
the wrong atomic operation, it is unnecessary additional state since
the state is already being protected by a mutex.
2024-10-11 10:36:19 -07:00
Andrew Kelley
2e2927735d
Merge pull request #21629 from ziglang/elf-incr
elf: more incremental progress
2024-10-09 17:43:17 -07:00
Andrew Kelley
c86a334d43 link.Elf.Object.initAtoms: reduce state access and indirection
The initAtoms function now only uses the `elf_file` parameter for
reporting linker error messages, making it easier to see that the
function has no data dependencies other than the Object struct itself,
making it easier to parallelize or otherwise move that logic around.

Also removed an indirect call via `addExtra` since we already know the
atom's file is the current Object instance. All calls to `Atom.addExtra`
should be audited for similar reasons.

Also removed unjustified use of `inline fn`.
2024-10-09 15:40:59 -07:00
Jakub Konka
e448fb9601 elf: change how we manage debug atoms in Dwarf linker 2024-10-09 12:38:53 -07:00
Jakub Konka
c92c72d08c elf: do not create atoms for section symbols that do not require it 2024-10-09 12:38:53 -07:00
Jakub Konka
cf2e462d91 elf: add some extra logging for created dynamic relocs 2024-10-09 12:38:48 -07:00
Jakub Konka
3e100c5dab dwarf: make Section.off a function 2024-09-04 13:34:25 +02:00
Jakub Konka
b44dd599ad elf: split Atom.allocate into Atom-independent parts 2024-09-04 13:34:25 +02:00
mlugg
c62487da76
compiler: avoid field/decl name conflicts
Most of the required renames here are net wins for readaibility, I'd
say. The ones in `arch` are a little more verbose, but I think better. I
didn't bother renaming the non-conflicting functions in
`arch/arm/bits.zig` and `arch/aarch64/bits.zig`, since these backends
are pretty bit-rotted anyway AIUI.
2024-08-29 23:43:52 +01: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
Jakub Konka
1254509d78 elf: make Atom.allocate and related ZigObject-independent 2024-08-26 22:19:51 +02:00
Jakub Konka
7173ea1a15 elf: simplify output section symbol tracking 2024-08-25 10:20:15 +02:00
Jakub Konka
a9d3885ac7 elf: streamline sections container 2024-08-25 10:20:14 +02:00
Jakub Konka
d3d5ed992c elf: emit .rela.debug* sections for relocatable if required 2024-08-21 01:43:21 -04:00
Jacob Young
ef11bc9899 Dwarf: rework self-hosted debug info from scratch
This is in preparation for incremental and actually being able to debug
executables built by the x86_64 backend.
2024-08-16 15:22:55 -04:00
Jakub Konka
c79290467f elf: fix GOT32 reloc resolution logic 2024-08-15 10:36:37 +02:00
Jakub Konka
0fd0b11bc4 riscv: do not emit GOT relocations for special linker symbols 2024-08-15 10:21:20 +02:00
Jakub Konka
f26573fddf elf: re-use old atom slot for a trampoline to that atom
This is the initial implementation of Jacob Young's idea of
re-using old function slots as trampolines for new function's
location. This way the trampoline is guaranteed to be aligned
to the function's alignment.

The only edge case is if an incremental update further overaligns
the function in which case we skip/delete the trampoline and
re-evaluate all references.
2024-08-14 12:10:40 +02:00
Jakub Konka
49d78cc793 elf: only apply zig jump table indirection to function calls (PLT32) 2024-08-13 13:30:24 +02:00
Jakub Konka
df80ccf760 elf: pretty print alingment when pretty printing atoms 2024-08-13 13:30:24 +02:00
Jakub Konka
ce8886d57d elf: make zig jump table indirection implicit via Symbol.address 2024-08-13 13:30:24 +02:00
Jakub Konka
d25c93a868 x86_64: emit call rel32 for near calls with linker reloc 2024-08-13 13:30:24 +02:00
Jakub Konka
78b1c73602 elf: rename OffsetTable to JumpTable 2024-08-13 13:30:24 +02:00
Jakub Konka
d328140858 elf: nuke ZigGotSection from existence 2024-08-13 13:30:24 +02:00
Jakub Konka
4c2b34e8ab elf: relax R_X86_64_32 into jump table indirection if zig_offset_table set 2024-08-13 13:30:24 +02:00
Jakub Konka
7556b32840 elf: indirect via offset table in the linker away from backend 2024-08-13 13:30:24 +02:00
Jakub Konka
41e9b8b6c8 elf: fix compile errors 2024-08-07 10:21:02 +02:00
Jakub Konka
518c7908f0 elf: always create symbol extra 2024-08-07 10:21:02 +02:00
David Rubin
9c7aade488
riscv: fix .got symbol loading 2024-07-31 13:39:57 -07:00
Jakub Konka
ef7bbcd80f elf: do not store merge section output section name in strings buffer 2024-07-30 10:00:50 +02:00
Jakub Konka
24126f5382 elf: simplify output section tracking for symbols 2024-07-30 10:00:50 +02:00
Jakub Konka
e8d008a8a8 elf: atom is always assigned output section index 2024-07-30 10:00:50 +02:00
Jakub Konka
96c20adeee elf: remove obsolete flags from atom 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
669f285943 elf: move ownership of atoms into objects 2024-07-30 10:00:50 +02:00
Jakub Konka
cba3389d90 macho: redo input file parsing in prep for multithreading 2024-07-22 12:05:56 +02:00