2575 Commits

Author SHA1 Message Date
Jakub Konka
63a40bff47 link/elf: actually commit merge sections 2024-04-20 23:36:42 +02:00
Jakub Konka
b5a781d19d link/elf: fix generating invalid section symbol index for merged sections 2024-04-20 23:36:41 +02:00
Jakub Konka
65492b3d52 link/elf: skip empty merge sections when resolving 2024-04-20 23:36:41 +02:00
Jakub Konka
9e0bca73e2 link/elf: implement string merging 2024-04-20 23:36:41 +02:00
Jakub Konka
09820a96b6 link/elf: move relocs indexes into Atom extras 2024-04-20 23:36:41 +02:00
Jakub Konka
13b403cbf7 link/elf: move fde record indexes into Atom extras 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
700644d35d link/elf: introduce Atom extras for out-of-band storage 2024-04-20 23:36:41 +02:00
mlugg
d0e74ffe52
compiler: rework comptime pointer representation and access
We've got a big one here! This commit reworks how we represent pointers
in the InternPool, and rewrites the logic for loading and storing from
them at comptime.

Firstly, the pointer representation. Previously, pointers were
represented in a highly structured manner: pointers to fields, array
elements, etc, were explicitly represented. This works well for simple
cases, but is quite difficult to handle in the cases of unusual
reinterpretations, pointer casts, offsets, etc. Therefore, pointers are
now represented in a more "flat" manner. For types without well-defined
layouts -- such as comptime-only types, automatic-layout aggregates, and
so on -- we still use this "hierarchical" structure. However, for types
with well-defined layouts, we use a byte offset associated with the
pointer. This allows the comptime pointer access logic to deal with
reinterpreted pointers far more gracefully, because the "base address"
of a pointer -- for instance a `field` -- is a single value which
pointer accesses cannot exceed since the parent has undefined layout.
This strategy is also more useful to most backends -- see the updated
logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do
prefer a chain of field and elements accesses for lowering pointer
values, such as SPIR-V, there is a helpful function in `Value` which
creates a strategy to derive a pointer value using ideally only field
and element accesses. This is actually more correct than the previous
logic, since it correctly handles pointer casts which, after the dust
has settled, end up referring exactly to an aggregate field or array
element.

In terms of the pointer access code, it has been rewritten from the
ground up. The old logic had become rather a mess of special cases being
added whenever bugs were hit, and was still riddled with bugs. The new
logic was written to handle the "difficult" cases correctly, the most
notable of which is restructuring of a comptime-only array (for
instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`.
Currently, the logic for loading and storing work somewhat differently,
but a future change will likely improve the loading logic to bring it
more in line with the store strategy. As far as I can tell, the rewrite
has fixed all bugs exposed by #19414.

As a part of this, the comptime bitcast logic has also been rewritten.
Previously, bitcasts simply worked by serializing the entire value into
an in-memory buffer, then deserializing it. This strategy has two key
weaknesses: pointers, and undefined values. Representations of these
values at comptime cannot be easily serialized/deserialized whilst
preserving data, which means many bitcasts would become runtime-known if
pointers were involved, or would turn `undefined` values into `0xAA`.
The new logic works by "flattening" the datastructure to be cast into a
sequence of bit-packed atomic values, and then "unflattening" it; using
serialization when necessary, but with special handling for `undefined`
values and for pointers which align in virtual memory. The resulting
code is definitely slower -- more on this later -- but it is correct.

The pointer access and bitcast logic required some helper functions and
types which are not generally useful elsewhere, so I opted to split them
into separate files `Sema/comptime_ptr_access.zig` and
`Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small
public APIs.

Whilst working on this branch, I caught various unrelated bugs with
transitive Sema errors, and with the handling of `undefined` values.
These bugs have been fixed, and corresponding behavior test added.

In terms of performance, I do anticipate that this commit will regress
performance somewhat, because the new pointer access and bitcast logic
is necessarily more complex. I have not yet taken performance
measurements, but will do shortly, and post the results in this PR. If
the performance regression is severe, I will do work to to optimize the
new logic before merge.

Resolves: #19452
Resolves: #19460
2024-04-17 13:41:25 +01:00
Jacob Young
c4587dc9f4 Uri: propagate per-component encoding
This allows `std.Uri.resolve_inplace` to properly preserve the fact
that `new` is already escaped but `base` may not be.  I originally tried
just moving `raw_uri` around, but it made uri resolution unmanagably
complicated, so I instead added per-component information to `Uri` which
allows extra allocations to be avoided when constructing uris with
components from different sources, and in some cases, deferring the work
all the way to when the uri is printed, where an allocator may not even
be needed.

Closes #19587
2024-04-10 02:11:54 -07:00
Jakub Konka
bc0e2089bc link/elf: do not set soname unless only dynlib (reverts for haiku exec) 2024-04-08 22:20:49 +02:00
Jakub Konka
84a0110464 link/elf: handle symbols to begin/end of .eh_frame section 2024-04-08 22:09:49 +02:00
Jakub Konka
9875ab4ee8 link/elf: actually emit DYN when targetting haiku 2024-04-08 20:54:54 +02:00
Jacob Young
7611d90ba0 InternPool: remove slice from byte aggregate keys
This deletes a ton of lookups and avoids many UAF bugs.

Closes #19485
2024-04-08 13:24:08 -04:00
Jakub Konka
4cd92567e7 link/elf: propagate Haiku requirement of always passing -shared for images 2024-04-08 13:24:08 -04:00
Jacob Young
fcdb7027e9 haiku: fix linking issues 2024-04-08 13:20:14 -04:00
Robin Voetter
cbf2ee72e3
spirv: fix some recursive pointers edge cases in dedup pass 2024-04-06 13:37:41 +02:00
Robin Voetter
125d3324d9
spirv: add link progression 2024-04-06 13:37:41 +02:00
Robin Voetter
97a67762ba
spirv: remove cache usage for types 2024-04-06 13:37:39 +02:00
Jakub Konka
23f729aec9
Merge pull request #19260 from mikdusan/macos-zippered
macos: add zippered support
2024-04-05 23:37:51 +02:00
Robin Voetter
d2be725e4b
Merge pull request #19490 from Snektron/spirv-dedup
spirv: deduplication pass
2024-04-01 09:51:04 +02:00
Jacob Young
eb723a4070 Update uses of @fieldParentPtr to use RLS 2024-03-30 20:50:48 -04:00
Jacob Young
e409afb79b Update uses of @fieldParentPtr to pass a pointer type 2024-03-30 20:50:48 -04:00
Jacob Young
5a41704f7e cbe: rewrite CType
Closes #14904
2024-03-30 20:50:48 -04:00
Jacob Young
6f10b11658 cbe: fix bugs revealed by an upcoming commit
Closes #18023
2024-03-30 20:50:48 -04:00
Robin Voetter
f5ab3c93c9
spirv: handle annotations in deduplication pass 2024-03-30 19:47:55 +01:00
Robin Voetter
b4960394ef
spirv: avoid copying operands in dedup pass 2024-03-30 19:47:52 +01:00
Robin Voetter
393a805741
spirv: deduplicate prototype 2024-03-30 10:16:18 +01:00
Jakub Konka
fda9a32fef macho: fix double free 2024-03-28 20:17:36 -07:00
Andrew Kelley
3661133f98
Merge pull request #19399 from ypsvlq/mingw
mingw: support -municode
2024-03-28 14:16:30 -07:00
Jakub Konka
9dac8db2df
Merge pull request #19430 from ziglang/dwarf-ub
link: fix undefined memory being written out in dwarf and codegen
2024-03-27 17:29:30 +01:00
Elaine Gibson
add74427b9 mingw: support -municode 2024-03-27 10:05:57 +00:00
mlugg
a61def10c6
compiler: eliminate most usages of TypedValue 2024-03-26 13:48:07 +00:00
mlugg
c6f3e9d79c
Zcu.Decl: remove ty field
`Decl` can no longer store un-interned values, so this field is now
unnecessary. The type can instead be fetched with the new `typeOf`
helper method, which just gets the type of the Decl's `Value`.
2024-03-26 13:48:06 +00:00
Jakub Konka
ae1b2bfcad dwarf+codegen: use appendNTimes instead of writer().writeByteNTimes 2024-03-25 23:48:39 +01:00
Jakub Konka
69e9fe4ede dwarf: actually write zeroed out unresolved relocs into emitted DWARF in initDeclState 2024-03-25 23:22:20 +01:00
mlugg
9c3670fc93
compiler: implement analysis-local comptime-mutable memory
This commit changes how we represent comptime-mutable memory
(`comptime var`) in the compiler in order to implement the intended
behavior that references to such memory can only exist at comptime.

It does *not* clean up the representation of mutable values, improve the
representation of comptime-known pointers, or fix the many bugs in the
comptime pointer access code. These will be future enhancements.

Comptime memory lives for the duration of a single Sema, and is not
permitted to escape that one analysis, either by becoming runtime-known
or by becoming comptime-known to other analyses. These restrictions mean
that we can represent comptime allocations not via Decl, but with state
local to Sema - specifically, the new `Sema.comptime_allocs` field. All
comptime-mutable allocations, as well as any comptime-known const allocs
containing references to such memory, live in here. This allows for
relatively fast checking of whether a value references any
comptime-mtuable memory, since we need only traverse values up to
pointers: pointers to Decls can never reference comptime-mutable memory,
and pointers into `Sema.comptime_allocs` always do.

This change exposed some faulty pointer access logic in `Value.zig`.
I've fixed the important cases, but there are some TODOs I've put in
which are definitely possible to hit with sufficiently esoteric code. I
plan to resolve these by auditing all direct accesses to pointers (most
of them ought to use Sema to perform the pointer access!), but for now
this is sufficient for all realistic code and to get tests passing.

This change eliminates `Zcu.tmp_hack_arena`, instead using the Sema
arena for comptime memory mutations, which is possible since comptime
memory is now local to the current Sema.

This change should allow `Decl` to store only an `InternPool.Index`
rather than a full-blown `ty: Type, val: Value`. This commit does not
perform this refactor.
2024-03-25 14:49:41 +00:00
Jakub Konka
c5a5b420dc dwarf: actually write out well-defined placeholder bytes for relocs 2024-03-24 20:18:26 +01:00
Andrew Kelley
8c94950c24 fix compilation failures found by CI 2024-03-19 16:18:18 -07:00
Andrew Kelley
cd62005f19 extract std.posix from std.os
closes #5019
2024-03-19 11:45:09 -07:00
Robin Voetter
8ed134243a
spirv: unused instruction pruning linker pass 2024-03-18 19:13:50 +01:00
Robin Voetter
9b18125562
spirv: make generic globals invocation-local 2024-03-18 19:13:50 +01:00
Robin Voetter
e566158acf
spirv: make IdResult an enum 2024-03-18 19:13:49 +01:00
Robin Voetter
3bffa58012
Revert "spirv: merge construct(Struct/Vector/Array) into constructComposite"
This reverts commit eb2d61d02e503f01070c05e2e1fc87e827124d94.
2024-03-17 14:17:26 +01:00
zhylmzr
fe61b19b96 macos: archive size fix
fixed: #19154
2024-03-16 07:33:18 +01:00
Jakub Konka
d565c5dfcd macho: fix a sad typo in calculating the address of a TLV pointer 2024-03-15 08:17:10 +01:00
Elaine Gibson
6b38758594 coff: only store PDB basename 2024-03-14 18:56:02 -07:00
Michael Dusan
197ffff0b8
macos: add tbd-v3 zippered support
Support linking against tbd-v3 SDKs such as those bundled with
Xcode 10.3 → 11.3.1 .

- Map target os=`ios` and abi=`macabi` to macho.PLATFORM.MACCATALYST.
  This allows for matches against tbdv4 targets, eg. `x86_64-maccatalyst`.

- When parsing old tbdv3 files with `zippered` platform, we append
  [`ARCH-macos`, `ARCH-maccatalyst`] to list of "tbd" targets. This
  enables linking for standard targets like `ARCH-macos-none` and
  maccatalyst targets `ARCH-ios-macabi`.

- Update mappings for macho platform and zig target macabi. While this
  is not full maccatalyst support, a basic exe can be built as follows:

```
zig libc > libc.txt
zig build-exe z0.zig --libc libc.txt -target x86_64-ios-macabi
```

closes #19110
2024-03-13 02:17:28 -04:00
Michael Dusan
11bc3fb190
macho: log.debug dylib path 2024-03-13 02:17:28 -04:00
Jakub Konka
55c085b893 elf: re-use output buffer for emitting thunks 2024-03-12 13:25:29 +01:00