38 Commits

Author SHA1 Message Date
Jacob Young
f3d0fc7a66 backends: port to new std.io.BufferedWriter API 2025-07-01 16:35:26 -07:00
Jacob Young
917640810e Target: pass and use locals by pointer instead of by value
This struct is larger than 256 bytes and code that copies it
consistently shows up in profiles of the compiler.
2025-06-19 11:45:06 -04:00
Jacob Young
5be8a5fe5f link: fix memory bugs 2024-11-16 21:29:17 -05:00
Andrew Kelley
344a21b14f fix 32-bit build 2024-10-12 10:44:17 -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
14c8e270bb link: fix false positive crtbegin/crtend detection
Embrace the Path abstraction, doing more operations based on directory
handles rather than absolute file paths. Most of the diff noise here
comes from this one.

Fix sorting of crtbegin/crtend atoms. Previously it would look at all
path components for those strings.

Make the C runtime path detection partially a pure function, and move
some logic to glibc.zig where it belongs.
2024-10-10 14:21:52 -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
Linus Groh
8588964972 Replace deprecated default initializations with decl literals 2024-09-12 16:01:23 +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
Alex Rønne Petersen
fb6f5a30b2
link: Rename InvalidCpuArch error to InvalidMachineType. 2024-08-23 19:56:29 +02: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
137d43c0ea elf: get hello-world glibc working again 2024-08-07 10:21:02 +02:00
Jakub Konka
26da7c8207 elf: fix symbol resolution for Objects 2024-08-07 10:21:02 +02:00
Jakub Konka
04f8f8ad3e elf: do not re-init Symbol when initializing 2024-08-07 10:21:02 +02:00
Jakub Konka
41e9b8b6c8 elf: fix compile errors 2024-08-07 10:21:02 +02:00
Jakub Konka
deeaa1bb0c elf: redo symbol mgmt and ownership in ZigObject 2024-08-07 10:21:02 +02:00
Jakub Konka
de80e4fec2 elf: move symbol ownership to LinkerDefined 2024-08-07 10:21:02 +02:00
Jakub Konka
9fe69cc0b5 elf: move symbol ownership to SharedObject 2024-08-07 10:21:02 +02:00
Jakub Konka
0701646beb elf: move merge subsections ownership into merge sections 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
9e0bca73e2 link/elf: implement string merging 2024-04-20 23:36:41 +02:00
Jakub Konka
d5fdb7315f link/elf: port macho symbol extras handling 2024-04-20 23:36:41 +02:00
Jakub Konka
a94d5895cf elf: do not prealloc input objects, pread selectively 2024-02-12 23:07:51 +01:00
David Rubin
9097bcc06e add SHT_NOBITS check 2024-01-15 05:27:54 +01:00
Andrew Kelley
4629708787 linker: fix some allocator references 2024-01-01 17:51:20 -07:00
Andrew Kelley
5a6a1f8a8a linker: update target references 2024-01-01 17:51:19 -07:00
Jakub Konka
e349bb2b66 elf: upcast e_shnum to u64 to check for valid ranges 2023-12-05 14:27:03 +01:00
Jakub Konka
52959bba7c elf: re-instate basic error reporting for LD script parser 2023-12-05 14:08:04 +01:00
Jakub Konka
2e1dd1e554 elf: move basic parse error reporting to SharedObject 2023-12-05 13:53:11 +01:00
Jakub Konka
6f3bbd5eaa elf: we were writing too many symbols in the symtab 2023-11-15 19:00:13 +01:00
Jakub Konka
5e78600f0f elf: actually track output symtab index of symbols 2023-11-07 23:18:41 +01:00
Jakub Konka
5c48236103 elf: init objects after parsing them 2023-11-05 12:37:15 +01:00
Jakub Konka
25c53f08a6 elf: redo strings management in the linker
* atom names - are stored locally and pulled from defining object's
  strtab
* local symbols - same
* global symbols - in principle, we could store them locally, but
  for better debugging experience - when things go wrong - we
  store the offsets in a global strtab used by the symbol resolver
2023-11-04 09:08:16 +01:00
Jakub Konka
8087ec8e8c elf: improve parsing of ld scripts and actually test linking against them 2023-10-24 19:03:00 +02:00
Jakub Konka
52e0ca1312 elf: parse GNU ld script as system lib indirection 2023-10-18 13:54:43 +02:00
Jakub Konka
d2727b808c elf: fix 32bit build 2023-10-16 19:56:47 +02:00
Jakub Konka
5fa90afb64 elf: fix synthetic section handling and actually parse DSOs 2023-10-16 19:33:04 +02:00
Jakub Konka
d1446565a1 elf: re-enable dynamic linking codepaths 2023-10-16 19:33:04 +02:00