2468 Commits

Author SHA1 Message Date
Jakub Konka
720dd80007 elf+riscv: implement enough to get basic hello world in C working 2024-02-21 23:04:43 +01:00
Jakub Konka
ca6f41ee30 elf+riscv: skip parsing .riscv.attributes section for now 2024-02-21 22:50:55 +01:00
Jakub Konka
d19001abac elf: skip STT_NOTYPE only if SHN_UNDEF from symtab inclusion 2024-02-21 22:49:58 +01:00
Jakub Konka
9fd112804f link: commit missing files 2024-02-21 22:48:19 +01:00
Jakub Konka
8ba31ed39a elf: sort input relocs if targeting riscv64 2024-02-21 22:47:52 +01:00
Jakub Konka
7fd4576596 elf+aarch64: resolve .eh_frame relocs 2024-02-21 22:28:02 +01:00
Jakub Konka
7aeba3a3d1 elf+aarch64: implement some resolveRelocNonAlloc logic 2024-02-21 22:25:25 +01:00
Jakub Konka
887709ed5c elf+aarch64: implement some resolveRelocAlloc logic 2024-02-21 22:22:40 +01:00
Jakub Konka
d7d47a2c0c elf+aarch64: implement some scanReloc logic 2024-02-21 22:07:51 +01:00
Jakub Konka
ee364d542a link: introduce common set of aarch64 abstractions 2024-02-21 22:00:28 +01:00
Jakub Konka
60a8f9b989 elf: make GOT arch aware when resolving relocs 2024-02-21 20:58:43 +01:00
Jakub Konka
775a161794 elf: simplify logic for resolving .eh_frame relocs on different arches 2024-02-21 20:50:29 +01:00
Jakub Konka
06c191a4ff elf: simplify logic for resolving nonalloc relocs on different arches 2024-02-21 20:29:56 +01:00
Jakub Konka
4cde47a169 elf: simplify logic for resolving alloc relocs on different arches 2024-02-21 20:11:32 +01:00
Jakub Konka
60bc2e7616 elf: simplify logic for handling scanning relocs on different arches 2024-02-21 19:06:10 +01:00
Jakub Konka
1ca004176f elf+riscv: resolve synthetic __global_pointer$ symbol 2024-02-21 18:09:23 +01:00
Jacob Young
57b2b3df52 Dwarf: use a user tag for padding 2024-02-18 14:11:03 +01:00
Jakub Konka
99584906bb elf: fix typo in resolving @"32" and @"32S" relocs on x86_64 2024-02-17 19:39:11 +01:00
Jakub Konka
57e71ce189 elf: @"64" is equivalent to GLOB_DAT on riscv 2024-02-17 19:39:11 +01:00
Jakub Konka
79e99c401c
Merge pull request #18973 from ziglang/elf-riscv
lib/std/elf: refactor relocation types + add RISCV relocs
2024-02-17 19:35:09 +01:00
Robin Voetter
dd4d320eb9
Merge pull request #18948 from alichraghi/vector
spirv: use extended instructions whenever possible
2024-02-17 14:01:07 +01:00
Jakub Konka
d1429a8fa9 lib/std/elf: refactor reloc enum values 2024-02-17 13:13:03 +01:00
Jakub Konka
509c7149b9 elf: fix formatting of relocs when reloc can be Zig specific 2024-02-17 12:35:16 +01:00
Jakub Konka
9542ee9bf8 elf: create Zig specific reloc type shared across ISAs 2024-02-17 11:46:01 +01:00
Jakub Konka
ace1a69a55 elf: add new R_RISCV_TLSDESC reloc type 2024-02-17 11:41:18 +01:00
Jakub Konka
975862aca9 elf: add riscv dynamic relocs 2024-02-17 11:29:06 +01:00
Michael Dusan
50330ec22a cbe: do not set execute bits on emitted file 2024-02-17 10:50:46 +01:00
Jakub Konka
601aa10b82 elf: add riscv64 dynamic relocs mapping 2024-02-17 09:04:06 +01:00
Jakub Konka
fc7dd3e285 elf: enable adding support for additional cpu archs 2024-02-16 21:54:43 +01:00
Jacob Young
6f08e17229 InternPool: make more use of NullTerminatedString.Slice
This should avoid the random pointer invalidation crashes.

Closes #18954
2024-02-16 00:27:25 -08:00
Ali Chraghi
44c31194e3 spirv: use extended instructions whenever possible 2024-02-15 17:25:44 +03:30
Igor Anić
d645114f7e add deflate implemented from first principles
Zig deflate compression/decompression implementation. It supports compression and decompression of gzip, zlib and raw deflate format.

Fixes #18062.

This PR replaces current compress/gzip and compress/zlib packages. Deflate package is renamed to flate. Flate is common name for deflate/inflate where deflate is compression and inflate decompression.

There are breaking change. Methods signatures are changed because of removal of the allocator, and I also unified API for all three namespaces (flate, gzip, zlib).

Currently I put old packages under v1 namespace they are still available as compress/v1/gzip, compress/v1/zlib, compress/v1/deflate. Idea is to give users of the current API little time to postpone analyzing what they had to change. Although that rises question when it is safe to remove that v1 namespace.

Here is current API in the compress package:

```Zig
// deflate
    fn compressor(allocator, writer, options) !Compressor(@TypeOf(writer))
    fn Compressor(comptime WriterType) type

    fn decompressor(allocator, reader, null) !Decompressor(@TypeOf(reader))
    fn Decompressor(comptime ReaderType: type) type

// gzip
    fn compress(allocator, writer, options) !Compress(@TypeOf(writer))
    fn Compress(comptime WriterType: type) type

    fn decompress(allocator, reader) !Decompress(@TypeOf(reader))
    fn Decompress(comptime ReaderType: type) type

// zlib
    fn compressStream(allocator, writer, options) !CompressStream(@TypeOf(writer))
    fn CompressStream(comptime WriterType: type) type

    fn decompressStream(allocator, reader) !DecompressStream(@TypeOf(reader))
    fn DecompressStream(comptime ReaderType: type) type

// xz
   fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader))
   fn Decompress(comptime ReaderType: type) type

// lzma
    fn decompress(allocator, reader) !Decompress(@TypeOf(reader))
    fn Decompress(comptime ReaderType: type) type

// lzma2
    fn decompress(allocator, reader, writer !void

// zstandard:
    fn DecompressStream(ReaderType, options) type
    fn decompressStream(allocator, reader) DecompressStream(@TypeOf(reader), .{})
    struct decompress
```

The proposed naming convention:
 - Compressor/Decompressor for functions which return type, like Reader/Writer/GeneralPurposeAllocator
 - compressor/compressor for functions which are initializers for that type, like reader/writer/allocator
 - compress/decompress for one shot operations, accepts reader/writer pair, like read/write/alloc

```Zig
/// Compress from reader and write compressed data to the writer.
fn compress(reader: anytype, writer: anytype, options: Options) !void

/// Create Compressor which outputs the writer.
fn compressor(writer: anytype, options: Options) !Compressor(@TypeOf(writer))

/// Compressor type
fn Compressor(comptime WriterType: type) type

/// Decompress from reader and write plain data to the writer.
fn decompress(reader: anytype, writer: anytype) !void

/// Create Decompressor which reads from reader.
fn decompressor(reader: anytype) Decompressor(@TypeOf(reader)

/// Decompressor type
fn Decompressor(comptime ReaderType: type) type

```

Comparing this implementation with the one we currently have in Zig's standard library (std).
Std is roughly 1.2-1.4 times slower in decompression, and 1.1-1.2 times slower in compression. Compressed sizes are pretty much same in both cases.
More resutls in [this](https://github.com/ianic/flate) repo.

This library uses static allocations for all structures, doesn't require allocator. That makes sense especially for deflate where all structures, internal buffers are allocated to the full size. Little less for inflate where we std version uses less memory by not preallocating to theoretical max size array which are usually not fully used.

For deflate this library allocates 395K while std 779K.
For inflate this library allocates 74.5K while std around 36K.

Inflate difference is because we here use 64K history instead of 32K in std.

If merged existing usage of compress gzip/zlib/deflate need some changes. Here is example with necessary changes in comments:

```Zig

const std = @import("std");

// To get this file:
// wget -nc -O war_and_peace.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8
const data = @embedFile("war_and_peace.txt");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(gpa.deinit() == .ok);
    const allocator = gpa.allocator();

    try oldDeflate(allocator);
    try new(std.compress.flate, allocator);

    try oldZlib(allocator);
    try new(std.compress.zlib, allocator);

    try oldGzip(allocator);
    try new(std.compress.gzip, allocator);
}

pub fn new(comptime pkg: type, allocator: std.mem.Allocator) !void {
    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    var cmp = try pkg.compressor(buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.finish();

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    var dcp = pkg.decompressor(fbs.reader());

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldDeflate(allocator: std.mem.Allocator) !void {
    const deflate = std.compress.v1.deflate;

    // Compressor
    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();
    // Remove allocator
    // Rename deflate -> flate
    var cmp = try deflate.compressor(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.close(); // Rename to finish
    cmp.deinit(); // Remove

    // Decompressor
    var fbs = std.io.fixedBufferStream(buf.items);
    // Remove allocator and last param
    // Rename deflate -> flate
    // Remove try
    var dcp = try deflate.decompressor(allocator, fbs.reader(), null);
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldZlib(allocator: std.mem.Allocator) !void {
    const zlib = std.compress.v1.zlib;

    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    // Rename compressStream => compressor
    // Remove allocator
    var cmp = try zlib.compressStream(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.finish();
    cmp.deinit(); // Remove

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    // decompressStream => decompressor
    // Remove allocator
    // Remove try
    var dcp = try zlib.decompressStream(allocator, fbs.reader());
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldGzip(allocator: std.mem.Allocator) !void {
    const gzip = std.compress.v1.gzip;

    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    // Rename compress => compressor
    // Remove allocator
    var cmp = try gzip.compress(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.close(); // Rename to finisho
    cmp.deinit(); // Remove

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    // Rename decompress => decompressor
    // Remove allocator
    // Remove try
    var dcp = try gzip.decompress(allocator, fbs.reader());
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

```
2024-02-14 18:28:20 +01:00
Jakub Konka
216a5594f6 elf: use u32 for all section indexes 2024-02-13 20:33:08 +01:00
Jakub Konka
e401930fa8 elf: store relative offsets in atom and symbol 2024-02-13 20:33:01 +01:00
Jakub Konka
c22bb38058 macho: scrap reader for preads when parsing archives 2024-02-13 19:18:27 +01:00
Jakub Konka
de30b30202 elf: scrap reader for preads when parsing archives 2024-02-13 18:57:49 +01:00
Jakub Konka
e5483b4ffc elf: fix 32bit build 2024-02-13 10:48:10 +01:00
Jakub Konka
8bd01eb7a9 elf: refactor archive specific object parsing logic 2024-02-12 23:59:19 +01:00
Jakub Konka
616a8f9853 elf: move code paths responsible for emitting object and archive into relocatable module 2024-02-12 23:37:51 +01:00
Jakub Konka
a94d5895cf elf: do not prealloc input objects, pread selectively 2024-02-12 23:07:51 +01:00
Jakub Konka
d18f52197d macho: include compiler-rt in static lib if requested 2024-02-10 08:12:06 +01:00
Jakub Konka
d12c8db642
Merge pull request #18875 from ziglang/macho-zo-dwarf
macho: emit DWARF for ZigObject relocatable
2024-02-09 23:12:04 +01:00
Veikka Tuominen
ddcea2cad4
Merge pull request #18857 from alichraghi/shader
spirv: make rusticl the primary testing implementation
2024-02-09 14:11:31 +02:00
Jakub Konka
b5d2be1082 macho: undo invalid fix for allocating sections for relocatable
This is way too buggy for my taste. I will need to revisit
segment/section alloc logic.
2024-02-09 12:48:25 +01:00
Ali Chraghi
eb2d61d02e spirv: merge construct(Struct/Vector/Array) into constructComposite 2024-02-09 09:27:05 +03:30
Jakub Konka
925273bcc8 macho: align memory size with file size when emitting relocatable 2024-02-09 00:20:11 +01:00
Jakub Konka
3bfda3d791 macho: fix alignment of objects in archive 2024-02-08 23:51:30 +01:00
Jakub Konka
8c0e5435b3 macho: do not close file on error - it will happen automatically anyhow 2024-02-08 23:51:30 +01:00
Jakub Konka
5da9d250ff macho: fix incorrect skip conditions for zig and dwarf sections 2024-02-08 22:08:51 +01:00
Jakub Konka
dcb7f5791a macho: alloc improvement for relocatable 2024-02-08 13:22:48 +01:00