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,...@@ -12,65 +12,54 @@ toc: Toc,
12const Toc = std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(u32));12const Toc = std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(u32));
1313
14// Archive files start with the ARMAG identifying string. Then follows a14// 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'
16// member indicates, for each member file.16// member indicates, for each member file.
17/// String that begins an archive file.17/// String that begins an archive file.
18const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n";18const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n";
19/// Size of that string.19/// Size of that string.
20const SARMAG: u4 = 8;20const 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.
23const ARFMAG: *const [2:0]u8 = "`\n";23const ARFMAG: *const [2:0]u8 = "`\n";
2424
25const ar_hdr = extern struct {25const Header = extern struct {
26 /// Member file name, sometimes / terminated.26 /// Member file name, sometimes / terminated.
27 ar_name: [16]u8,27 name: [16]u8,
28
29 /// File date, decimal seconds since Epoch.28 /// File date, decimal seconds since Epoch.
30 ar_date: [12]u8,29 date: [12]u8,
31
32 /// User ID, in ASCII format.30 /// User ID, in ASCII format.
33 ar_uid: [6]u8,31 uid: [6]u8,
34
35 /// Group ID, in ASCII format.32 /// Group ID, in ASCII format.
36 ar_gid: [6]u8,33 gid: [6]u8,
37
38 /// File mode, in ASCII octal.34 /// File mode, in ASCII octal.
39 ar_mode: [8]u8,35 mode: [8]u8,
40
41 /// File size, in ASCII decimal.36 /// File size, in ASCII decimal.
42 ar_size: [10]u8,37 size: [10]u8,
43
44 /// Always contains ARFMAG.38 /// Always contains ARFMAG.
45 ar_fmag: [2]u8,39 fmag: [2]u8,
4640
47 const NameOrIndex = union(enum) {41 const NameOrIndex = union(enum) {
48 name: []const u8,42 name: []const u8,
49 index: u32,43 index: u32,
50 };44 };
5145
52 fn nameOrIndex(archive: ar_hdr) !NameOrIndex {46 fn nameOrIndex(archive: Header) !NameOrIndex {
53 const value = getValue(&archive.ar_name);47 const value = getValue(&archive.name);
54 const slash_index = mem.indexOfScalar(u8, value, '/') orelse return error.MalformedArchive;48 const slash_index = mem.indexOfScalar(u8, value, '/') orelse return error.MalformedArchive;
55 const len = value.len;49 const len = value.len;
56 if (slash_index == len - 1) {50 if (slash_index == len - 1) {
57 // Name stored directly51 // Name stored directly
58 return NameOrIndex{ .name = value };52 return .{ .name = value };
59 } else {53 } else {
60 // Name follows the header directly and its length is encoded in54 // Name follows the header directly and its length is encoded in
61 // the name field.55 // the name field.
62 const index = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10);56 const index = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10);
63 return NameOrIndex{ .index = index };57 return .{ .index = index };
64 }58 }
65 }59 }
6660
67 fn date(archive: ar_hdr) !u64 {61 fn parsedSize(archive: Header) !u32 {
68 const value = getValue(&archive.ar_date);62 const value = getValue(&archive.size);
69 return std.fmt.parseInt(u64, value, 10);
70 }
71
72 fn size(archive: ar_hdr) !u32 {
73 const value = getValue(&archive.ar_size);
74 return std.fmt.parseInt(u32, value, 10);63 return std.fmt.parseInt(u32, value, 10);
75 }64 }
7665
...@@ -98,8 +87,8 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {...@@ -98,8 +87,8 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {
98 const magic = try reader.readBytesNoEof(SARMAG);87 const magic = try reader.readBytesNoEof(SARMAG);
99 if (!mem.eql(u8, &magic, ARMAG)) return error.BadArchiveMagic;88 if (!mem.eql(u8, &magic, ARMAG)) return error.BadArchiveMagic;
10089
101 const header = try reader.readStruct(ar_hdr);90 const header = try reader.readStruct(Header);
102 if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) return error.BadHeaderDelimiter;91 if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadHeaderDelimiter;
10392
104 var toc = try parseTableOfContents(gpa, header, reader);93 var toc = try parseTableOfContents(gpa, header, reader);
105 errdefer deinitToc(gpa, &toc);94 errdefer deinitToc(gpa, &toc);
...@@ -113,7 +102,7 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {...@@ -113,7 +102,7 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive {
113 };102 };
114}103}
115104
116fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 {105fn parseName(archive: *const Archive, header: Header) ![]const u8 {
117 const name_or_index = try header.nameOrIndex();106 const name_or_index = try header.nameOrIndex();
118 switch (name_or_index) {107 switch (name_or_index) {
119 .name => |name| return name,108 .name => |name| return name,
...@@ -124,10 +113,10 @@ fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 {...@@ -124,10 +113,10 @@ fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 {
124 }113 }
125}114}
126115
127fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc {116fn parseTableOfContents(gpa: Allocator, header: Header, reader: anytype) !Toc {
128 // size field can have extra spaces padded in front as well as the end,117 // size field can have extra spaces padded in front as well as the end,
129 // so we trim those first before parsing the ASCII value.118 // 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, " ");
131 const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10);120 const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10);
132121
133 const num_symbols = try reader.readInt(u32, .big);122 const num_symbols = try reader.readInt(u32, .big);
...@@ -167,14 +156,14 @@ fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc {...@@ -167,14 +156,14 @@ fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc {
167}156}
168157
169fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {158fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {
170 const header: ar_hdr = try reader.readStruct(ar_hdr);159 const header: Header = try reader.readStruct(Header);
171 if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) {160 if (!mem.eql(u8, &header.fmag, ARFMAG)) {
172 return error.InvalidHeaderDelimiter;161 return error.InvalidHeaderDelimiter;
173 }162 }
174 if (!mem.eql(u8, header.ar_name[0..2], "//")) {163 if (!mem.eql(u8, header.name[0..2], "//")) {
175 return error.MissingTableName;164 return error.MissingTableName;
176 }165 }
177 const table_size = try header.size();166 const table_size = try header.parsedSize();
178 const long_file_names = try gpa.alloc(u8, table_size);167 const long_file_names = try gpa.alloc(u8, table_size);
179 errdefer gpa.free(long_file_names);168 errdefer gpa.free(long_file_names);
180 try reader.readNoEof(long_file_names);169 try reader.readNoEof(long_file_names);
...@@ -186,14 +175,14 @@ fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {...@@ -186,14 +175,14 @@ fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 {
186/// When found, parses the object file into an `Object` and returns it.175/// When found, parses the object file into an `Object` and returns it.
187pub fn parseObject(archive: Archive, wasm: *const Wasm, file_contents: []const u8, path: Path) !Object {176pub fn parseObject(archive: Archive, wasm: *const Wasm, file_contents: []const u8, path: Path) !Object {
188 var fbs = std.io.fixedBufferStream(file_contents);177 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
193 const object_name = try archive.parseName(header);182 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);
197}186}
198187
199const std = @import("std");188const std = @import("std");