authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-31 16:46:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-31 22:01:10-07:00
logbf9978a57a9d939aefb3babcca28f87cb34331b0
treebda26a608ad5b524b4d7e93c24266bfec861da8e
parent2b868bb6dd430386bc97e9e387d3b43c673f0dac

link.File.Wasm: conform to naming conventions


1 files changed, 29 insertions(+), 40 deletions(-)

src/link/Wasm/Archive.zig+29-40
......@@ -12,65 +12,54 @@ toc: Toc,
1212const Toc = std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(u32));
1313
1414// Archive files start with the ARMAG identifying string. Then follows a
15// `struct ar_hdr', and as many bytes of member file data as its `ar_size'
15// `struct Header', and as many bytes of member file data as its `size'
1616// member indicates, for each member file.
1717/// String that begins an archive file.
1818const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n";
1919/// Size of that string.
2020const SARMAG: u4 = 8;
2121
22/// String in ar_fmag at the end of each header.
22/// String in fmag at the end of each header.
2323const ARFMAG: *const [2:0]u8 = "`\n";
2424
25const ar_hdr = extern struct {
25const Header = extern struct {
2626 /// Member file name, sometimes / terminated.
27 ar_name: [16]u8,
28
27 name: [16]u8,
2928 /// File date, decimal seconds since Epoch.
30 ar_date: [12]u8,
31
29 date: [12]u8,
3230 /// User ID, in ASCII format.
33 ar_uid: [6]u8,
34
31 uid: [6]u8,
3532 /// Group ID, in ASCII format.
36 ar_gid: [6]u8,
37
33 gid: [6]u8,
3834 /// File mode, in ASCII octal.
39 ar_mode: [8]u8,
40
35 mode: [8]u8,
4136 /// File size, in ASCII decimal.
42 ar_size: [10]u8,
43
37 size: [10]u8,
4438 /// Always contains ARFMAG.
45 ar_fmag: [2]u8,
39 fmag: [2]u8,
4640
4741 const NameOrIndex = union(enum) {
4842 name: []const u8,
4943 index: u32,
5044 };
5145
52 fn nameOrIndex(archive: ar_hdr) !NameOrIndex {
53 const value = getValue(&archive.ar_name);
46 fn nameOrIndex(archive: Header) !NameOrIndex {
47 const value = getValue(&archive.name);
5448 const slash_index = mem.indexOfScalar(u8, value, '/') orelse return error.MalformedArchive;
5549 const len = value.len;
5650 if (slash_index == len - 1) {
5751 // Name stored directly
58 return NameOrIndex{ .name = value };
52 return .{ .name = value };
5953 } else {
6054 // Name follows the header directly and its length is encoded in
6155 // the name field.
6256 const index = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10);
63 return NameOrIndex{ .index = index };
57 return .{ .index = index };
6458 }
6559 }
6660
67 fn date(archive: ar_hdr) !u64 {
68 const value = getValue(&archive.ar_date);
69 return std.fmt.parseInt(u64, value, 10);
70 }
71
72 fn size(archive: ar_hdr) !u32 {
73 const value = getValue(&archive.ar_size);
61 fn parsedSize(archive: Header) !u32 {
62 const value = getValue(&archive.size);
7463 return std.fmt.parseInt(u32, value, 10);
7564 }
7665
......@@ -98,8 +87,8 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {
9887 const magic = try reader.readBytesNoEof(SARMAG);
9988 if (!mem.eql(u8, &magic, ARMAG)) return error.BadArchiveMagic;
10089
101 const header = try reader.readStruct(ar_hdr);
102 if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) return error.BadHeaderDelimiter;
90 const header = try reader.readStruct(Header);
91 if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadHeaderDelimiter;
10392
10493 var toc = try parseTableOfContents(gpa, header, reader);
10594 errdefer deinitToc(gpa, &toc);
......@@ -113,7 +102,7 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {
113102 };
114103}
115104
116fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 {
105fn parseName(archive: *const Archive, header: Header) ![]const u8 {
117106 const name_or_index = try header.nameOrIndex();
118107 switch (name_or_index) {
119108 .name => |name| return name,
......@@ -124,10 +113,10 @@ fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 {
124113 }
125114}
126115
127fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc {
116fn parseTableOfContents(gpa: Allocator, header: Header, reader: anytype) !Toc {
128117 // size field can have extra spaces padded in front as well as the end,
129118 // so we trim those first before parsing the ASCII value.
130 const size_trimmed = mem.trim(u8, &header.ar_size, " ");
119 const size_trimmed = mem.trim(u8, &header.size, " ");
131120 const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10);
132121
133122 const num_symbols = try reader.readInt(u32, .big);
......@@ -167,14 +156,14 @@ fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc {
167156}
168157
169158fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {
170 const header: ar_hdr = try reader.readStruct(ar_hdr);
171 if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) {
159 const header: Header = try reader.readStruct(Header);
160 if (!mem.eql(u8, &header.fmag, ARFMAG)) {
172161 return error.InvalidHeaderDelimiter;
173162 }
174 if (!mem.eql(u8, header.ar_name[0..2], "//")) {
163 if (!mem.eql(u8, header.name[0..2], "//")) {
175164 return error.MissingTableName;
176165 }
177 const table_size = try header.size();
166 const table_size = try header.parsedSize();
178167 const long_file_names = try gpa.alloc(u8, table_size);
179168 errdefer gpa.free(long_file_names);
180169 try reader.readNoEof(long_file_names);
......@@ -186,14 +175,14 @@ fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {
186175/// When found, parses the object file into an `Object` and returns it.
187176pub fn parseObject(archive: Archive, wasm: *const Wasm, file_contents: []const u8, path: Path) !Object {
188177 var fbs = std.io.fixedBufferStream(file_contents);
189 const header = try fbs.reader().readStruct(ar_hdr);
178 const header = try fbs.reader().readStruct(Header);
190179
191 if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) return error.BadArchiveHeaderDelimiter;
180 if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadArchiveHeaderDelimiter;
192181
193182 const object_name = try archive.parseName(header);
194 const object_file_size = try header.size();
183 const object_file_size = try header.parsedSize();
195184
196 return Object.create(wasm, file_contents[@sizeOf(ar_hdr)..][0..object_file_size], path, object_name);
185 return Object.create(wasm, file_contents[@sizeOf(Header)..][0..object_file_size], path, object_name);
197186}
198187
199188const std = @import("std");