authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-24 22:13:57+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-06-24 08:12:17+02:00
log0606fbbc4bb7a006e2dcbf151c9505c920b735be
treed0024039d30d24ea0d07269c2cf53a44fdd8bd1d
parent4d3715d89f97f3f9b6e366bbabe01f9d64ed56cf

wasm-linker: Implement Archive parsing

This implements a very basic archive file parser that validates the magic bytes, and then parses the symbol table and stores the symbol and their position.

1 files changed, 180 insertions(+), 0 deletions(-)

src/link/Wasm/Archive.zig created+180
...@@ -0,0 +1,180 @@
1const Archive = @This();
2
3const std = @import("std");
4const assert = std.debug.assert;
5const fs = std.fs;
6const log = std.log.scoped(.archive);
7const macho = std.macho;
8const mem = std.mem;
9
10const Allocator = mem.Allocator;
11const Object = @import("Object.zig");
12
13file: fs.File,
14name: []const u8,
15
16header: ar_hdr = undefined,
17
18/// Parsed table of contents.
19/// Each symbol name points to a list of all definition
20/// sites within the current static archive.
21toc: std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(u32)) = .{},
22
23// Archive files start with the ARMAG identifying string. Then follows a
24// `struct ar_hdr', and as many bytes of member file data as its `ar_size'
25// member indicates, for each member file.
26/// String that begins an archive file.
27const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n";
28/// Size of that string.
29const SARMAG: u4 = 8;
30
31/// String in ar_fmag at the end of each header.
32const ARFMAG: *const [2:0]u8 = "`\n";
33
34const ar_hdr = extern struct {
35 /// Member file name, sometimes / terminated.
36 ar_name: [16]u8,
37
38 /// File date, decimal seconds since Epoch.
39 ar_date: [12]u8,
40
41 /// User ID, in ASCII format.
42 ar_uid: [6]u8,
43
44 /// Group ID, in ASCII format.
45 ar_gid: [6]u8,
46
47 /// File mode, in ASCII octal.
48 ar_mode: [8]u8,
49
50 /// File size, in ASCII decimal.
51 ar_size: [10]u8,
52
53 /// Always contains ARFMAG.
54 ar_fmag: [2]u8,
55
56 const NameOrLength = union(enum) {
57 Name: []const u8,
58 Length: u32,
59 };
60 fn nameOrLength(self: ar_hdr) !NameOrLength {
61 const value = getValue(&self.ar_name);
62 const slash_index = mem.indexOfScalar(u8, value, '/') orelse return error.MalformedArchive;
63 const len = value.len;
64 if (slash_index == len - 1) {
65 // Name stored directly
66 return NameOrLength{ .Name = value };
67 } else {
68 // Name follows the header directly and its length is encoded in
69 // the name field.
70 const length = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10);
71 return NameOrLength{ .Length = length };
72 }
73 }
74
75 fn date(self: ar_hdr) !u64 {
76 const value = getValue(&self.ar_date);
77 return std.fmt.parseInt(u64, value, 10);
78 }
79
80 fn size(self: ar_hdr) !u32 {
81 const value = getValue(&self.ar_size);
82 return std.fmt.parseInt(u32, value, 10);
83 }
84
85 fn getValue(raw: []const u8) []const u8 {
86 return mem.trimRight(u8, raw, &[_]u8{@as(u8, 0x20)});
87 }
88};
89
90pub fn deinit(self: *Archive, allocator: Allocator) void {
91 for (self.toc.keys()) |*key| {
92 allocator.free(key.*);
93 }
94 for (self.toc.values()) |*value| {
95 value.deinit(allocator);
96 }
97 self.toc.deinit(allocator);
98 allocator.free(self.name);
99}
100
101pub fn parse(self: *Archive, allocator: Allocator) !void {
102 const reader = self.file.reader();
103
104 const magic = try reader.readBytesNoEof(SARMAG);
105 if (!mem.eql(u8, &magic, ARMAG)) {
106 log.debug("invalid magic: expected '{s}', found '{s}'", .{ ARMAG, magic });
107 return error.NotArchive;
108 }
109
110 self.header = try reader.readStruct(ar_hdr);
111 if (!mem.eql(u8, &self.header.ar_fmag, ARFMAG)) {
112 log.debug("invalid header delimiter: expected '{s}', found '{s}'", .{ ARFMAG, self.header.ar_fmag });
113 return error.NotArchive;
114 }
115
116 log.debug("parsing archive '{s}' at '{s}'", .{ std.mem.sliceTo(&self.header.ar_name, 0), self.name });
117
118 try self.parseTableOfContents(allocator, reader);
119}
120
121fn parseName(allocator: Allocator, header: ar_hdr, reader: anytype) ![]u8 {
122 const name_or_length = try header.nameOrLength();
123 var name: []u8 = undefined;
124 switch (name_or_length) {
125 .Name => |n| {
126 name = try allocator.dupe(u8, n);
127 },
128 .Length => |len| {
129 var n = try allocator.alloc(u8, len);
130 defer allocator.free(n);
131 try reader.readNoEof(n);
132 const actual_len = mem.indexOfScalar(u8, n, @as(u8, 0)) orelse n.len;
133 name = try allocator.dupe(u8, n[0..actual_len]);
134 },
135 }
136 return name;
137}
138
139fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) !void {
140 log.debug("parsing table of contents for archive file '{s}'", .{self.name});
141 // size field can have extra spaces padded in front as well as the end,
142 // so we trim those first before parsing the ASCII value.
143 const size_trimmed = std.mem.trim(u8, &self.header.ar_size, " ");
144 const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10);
145
146 const num_symbols = try reader.readIntBig(u32);
147 const symbol_positions = try allocator.alloc(u32, num_symbols);
148 defer allocator.free(symbol_positions);
149 for (symbol_positions) |*index| {
150 index.* = try reader.readIntBig(u32);
151 }
152
153 const sym_tab = try allocator.alloc(u8, sym_tab_size - 4 - (4 * num_symbols));
154 defer allocator.free(sym_tab);
155
156 reader.readNoEof(sym_tab) catch {
157 log.err("incomplete symbol table: expected symbol table of length 0x{x}", .{sym_tab.len});
158 return error.MalformedArchive;
159 };
160
161 var i: usize = 0;
162 while (i < sym_tab.len) {
163 const string = std.mem.sliceTo(sym_tab[i..], 0);
164 if (string.len == 0) {
165 i += 1;
166 continue;
167 }
168 i += string.len;
169 const name = try allocator.dupe(u8, string);
170 errdefer allocator.free(name);
171 const gop = try self.toc.getOrPut(allocator, name);
172 if (gop.found_existing) {
173 allocator.free(name);
174 } else {
175 gop.value_ptr.* = .{};
176 }
177 try gop.value_ptr.append(allocator, symbol_positions[gop.index]);
178 log.debug(" parsed symbol '{s}' for position {d}", .{ string, symbol_positions[gop.index] });
179 }
180}