authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-15 15:33:10+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-02 22:05:21-05:00
log808306f49ac8f7fd57c10a48a0126fefd07ab690
treec5ca44b3f8e898d4a0446fbecec9beaef808efc0
parentc013f45ad08c2c6d727bf336767e23d988f5f30b

macho: rename dwarf.zig to Dwarf.zig

Separate commit since macOS is case-insensitive by default and so I had to do it from Linux.

2 files changed, 286 insertions(+), 286 deletions(-)

src/link/MachO/Dwarf.zig created+286
......@@ -0,0 +1,286 @@
1pub const InfoReader = struct {
2 bytes: []const u8,
3 strtab: []const u8,
4 pos: usize = 0,
5
6 pub fn readCompileUnitHeader(p: *InfoReader) !CompileUnitHeader {
7 var length: u64 = try p.readInt(u32);
8 const is_64bit = length == 0xffffffff;
9 if (is_64bit) {
10 length = try p.readInt(u64);
11 }
12 const dw_fmt: DwarfFormat = if (is_64bit) .dwarf64 else .dwarf32;
13 return .{
14 .format = dw_fmt,
15 .length = length,
16 .version = try p.readInt(u16),
17 .debug_abbrev_offset = try p.readOffset(dw_fmt),
18 .address_size = try p.readByte(),
19 };
20 }
21
22 pub fn seekToDie(p: *InfoReader, code: Code, cuh: CompileUnitHeader, abbrev_reader: *AbbrevReader) !void {
23 const cuh_length = math.cast(usize, cuh.length) orelse return error.Overflow;
24 const end_pos = p.pos + switch (cuh.format) {
25 .dwarf32 => @as(usize, 4),
26 .dwarf64 => 12,
27 } + cuh_length;
28 while (p.pos < end_pos) {
29 const di_code = try p.readUleb128(u64);
30 if (di_code == 0) return error.Eof;
31 if (di_code == code) return;
32
33 while (try abbrev_reader.readAttr()) |attr| switch (attr.at) {
34 dwarf.FORM.sec_offset,
35 dwarf.FORM.ref_addr,
36 => {
37 _ = try p.readOffset(cuh.format);
38 },
39
40 dwarf.FORM.addr => {
41 _ = try p.readNBytes(cuh.address_size);
42 },
43
44 dwarf.FORM.block1,
45 dwarf.FORM.block2,
46 dwarf.FORM.block4,
47 dwarf.FORM.block,
48 => {
49 _ = try p.readBlock(attr.form);
50 },
51
52 dwarf.FORM.exprloc => {
53 _ = try p.readExprLoc();
54 },
55
56 dwarf.FORM.flag_present => {},
57
58 dwarf.FORM.data1,
59 dwarf.FORM.ref1,
60 dwarf.FORM.flag,
61 dwarf.FORM.data2,
62 dwarf.FORM.ref2,
63 dwarf.FORM.data4,
64 dwarf.FORM.ref4,
65 dwarf.FORM.data8,
66 dwarf.FORM.ref8,
67 dwarf.FORM.ref_sig8,
68 dwarf.FORM.udata,
69 dwarf.FORM.ref_udata,
70 dwarf.FORM.sdata,
71 => {
72 _ = try p.readConstant(attr.form);
73 },
74
75 dwarf.FORM.strp,
76 dwarf.FORM.string,
77 => {
78 _ = try p.readString(attr.form, cuh);
79 },
80
81 else => {
82 // TODO better errors
83 log.err("unhandled DW_FORM_* value with identifier {x}", .{attr.form});
84 return error.UnhandledDwFormValue;
85 },
86 };
87 }
88 }
89
90 pub fn readBlock(p: *InfoReader, form: Form) ![]const u8 {
91 const len: u64 = switch (form) {
92 dwarf.FORM.block1 => try p.readByte(),
93 dwarf.FORM.block2 => try p.readInt(u16),
94 dwarf.FORM.block4 => try p.readInt(u32),
95 dwarf.FORM.block => try p.readUleb128(u64),
96 else => unreachable,
97 };
98 return p.readNBytes(len);
99 }
100
101 pub fn readExprLoc(p: *InfoReader) ![]const u8 {
102 const len: u64 = try p.readUleb128(u64);
103 return p.readNBytes(len);
104 }
105
106 pub fn readConstant(p: *InfoReader, form: Form) !u64 {
107 return switch (form) {
108 dwarf.FORM.data1, dwarf.FORM.ref1, dwarf.FORM.flag => try p.readByte(),
109 dwarf.FORM.data2, dwarf.FORM.ref2 => try p.readInt(u16),
110 dwarf.FORM.data4, dwarf.FORM.ref4 => try p.readInt(u32),
111 dwarf.FORM.data8, dwarf.FORM.ref8, dwarf.FORM.ref_sig8 => try p.readInt(u64),
112 dwarf.FORM.udata, dwarf.FORM.ref_udata => try p.readUleb128(u64),
113 dwarf.FORM.sdata => @bitCast(try p.readIleb128(i64)),
114 else => return error.UnhandledConstantForm,
115 };
116 }
117
118 pub fn readString(p: *InfoReader, form: Form, cuh: CompileUnitHeader) ![:0]const u8 {
119 switch (form) {
120 dwarf.FORM.strp => {
121 const off = try p.readOffset(cuh.format);
122 const off_u = math.cast(usize, off) orelse return error.Overflow;
123 return mem.sliceTo(@as([*:0]const u8, @ptrCast(p.strtab.ptr + off_u)), 0);
124 },
125 dwarf.FORM.string => {
126 const start = p.pos;
127 while (p.pos < p.bytes.len) : (p.pos += 1) {
128 if (p.bytes[p.pos] == 0) break;
129 }
130 if (p.bytes[p.pos] != 0) return error.Eof;
131 return p.bytes[start..p.pos :0];
132 },
133 else => unreachable,
134 }
135 }
136
137 pub fn readByte(p: *InfoReader) !u8 {
138 if (p.pos + 1 > p.bytes.len) return error.Eof;
139 defer p.pos += 1;
140 return p.bytes[p.pos];
141 }
142
143 pub fn readNBytes(p: *InfoReader, num: u64) ![]const u8 {
144 const num_usize = math.cast(usize, num) orelse return error.Overflow;
145 if (p.pos + num_usize > p.bytes.len) return error.Eof;
146 defer p.pos += num_usize;
147 return p.bytes[p.pos..][0..num_usize];
148 }
149
150 pub fn readInt(p: *InfoReader, comptime Int: type) !Int {
151 if (p.pos + @sizeOf(Int) > p.bytes.len) return error.Eof;
152 defer p.pos += @sizeOf(Int);
153 return mem.readInt(Int, p.bytes[p.pos..][0..@sizeOf(Int)], .little);
154 }
155
156 pub fn readOffset(p: *InfoReader, dw_fmt: DwarfFormat) !u64 {
157 return switch (dw_fmt) {
158 .dwarf32 => try p.readInt(u32),
159 .dwarf64 => try p.readInt(u64),
160 };
161 }
162
163 pub fn readUleb128(p: *InfoReader, comptime Type: type) !Type {
164 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
165 var creader = std.io.countingReader(stream.reader());
166 const value: Type = try leb.readUleb128(Type, creader.reader());
167 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
168 return value;
169 }
170
171 pub fn readIleb128(p: *InfoReader, comptime Type: type) !Type {
172 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
173 var creader = std.io.countingReader(stream.reader());
174 const value: Type = try leb.readIleb128(Type, creader.reader());
175 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
176 return value;
177 }
178
179 pub fn seekTo(p: *InfoReader, off: u64) !void {
180 p.pos = math.cast(usize, off) orelse return error.Overflow;
181 }
182};
183
184pub const AbbrevReader = struct {
185 bytes: []const u8,
186 pos: usize = 0,
187
188 pub fn hasMore(p: AbbrevReader) bool {
189 return p.pos < p.bytes.len;
190 }
191
192 pub fn readDecl(p: *AbbrevReader) !?AbbrevDecl {
193 const pos = p.pos;
194 const code = try p.readUleb128(Code);
195 if (code == 0) return null;
196
197 const tag = try p.readUleb128(Tag);
198 const has_children = (try p.readByte()) > 0;
199 return .{
200 .code = code,
201 .pos = pos,
202 .len = p.pos - pos,
203 .tag = tag,
204 .has_children = has_children,
205 };
206 }
207
208 pub fn readAttr(p: *AbbrevReader) !?AbbrevAttr {
209 const pos = p.pos;
210 const at = try p.readUleb128(At);
211 const form = try p.readUleb128(Form);
212 return if (at == 0 and form == 0) null else .{
213 .at = at,
214 .form = form,
215 .pos = pos,
216 .len = p.pos - pos,
217 };
218 }
219
220 pub fn readByte(p: *AbbrevReader) !u8 {
221 if (p.pos + 1 > p.bytes.len) return error.Eof;
222 defer p.pos += 1;
223 return p.bytes[p.pos];
224 }
225
226 pub fn readUleb128(p: *AbbrevReader, comptime Type: type) !Type {
227 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
228 var creader = std.io.countingReader(stream.reader());
229 const value: Type = try leb.readUleb128(Type, creader.reader());
230 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
231 return value;
232 }
233
234 pub fn seekTo(p: *AbbrevReader, off: u64) !void {
235 p.pos = math.cast(usize, off) orelse return error.Overflow;
236 }
237};
238
239const AbbrevDecl = struct {
240 code: Code,
241 pos: usize,
242 len: usize,
243 tag: Tag,
244 has_children: bool,
245};
246
247const AbbrevAttr = struct {
248 at: At,
249 form: Form,
250 pos: usize,
251 len: usize,
252};
253
254const CompileUnitHeader = struct {
255 format: DwarfFormat,
256 length: u64,
257 version: u16,
258 debug_abbrev_offset: u64,
259 address_size: u8,
260};
261
262const Die = struct {
263 pos: usize,
264 len: usize,
265};
266
267const DwarfFormat = enum {
268 dwarf32,
269 dwarf64,
270};
271
272const dwarf = std.dwarf;
273const leb = std.leb;
274const log = std.log.scoped(.link);
275const math = std.math;
276const mem = std.mem;
277const std = @import("std");
278
279const At = u64;
280const Code = u64;
281const Form = u64;
282const Tag = u64;
283
284pub const AT = dwarf.AT;
285pub const FORM = dwarf.FORM;
286pub const TAG = dwarf.TAG;
src/link/MachO/dwarf.zig deleted-286
......@@ -1,286 +0,0 @@
1pub const InfoReader = struct {
2 bytes: []const u8,
3 strtab: []const u8,
4 pos: usize = 0,
5
6 pub fn readCompileUnitHeader(p: *InfoReader) !CompileUnitHeader {
7 var length: u64 = try p.readInt(u32);
8 const is_64bit = length == 0xffffffff;
9 if (is_64bit) {
10 length = try p.readInt(u64);
11 }
12 const dw_fmt: DwarfFormat = if (is_64bit) .dwarf64 else .dwarf32;
13 return .{
14 .format = dw_fmt,
15 .length = length,
16 .version = try p.readInt(u16),
17 .debug_abbrev_offset = try p.readOffset(dw_fmt),
18 .address_size = try p.readByte(),
19 };
20 }
21
22 pub fn seekToDie(p: *InfoReader, code: Code, cuh: CompileUnitHeader, abbrev_reader: *AbbrevReader) !void {
23 const cuh_length = math.cast(usize, cuh.length) orelse return error.Overflow;
24 const end_pos = p.pos + switch (cuh.format) {
25 .dwarf32 => @as(usize, 4),
26 .dwarf64 => 12,
27 } + cuh_length;
28 while (p.pos < end_pos) {
29 const di_code = try p.readUleb128(u64);
30 if (di_code == 0) return error.Eof;
31 if (di_code == code) return;
32
33 while (try abbrev_reader.readAttr()) |attr| switch (attr.at) {
34 dwarf.FORM.sec_offset,
35 dwarf.FORM.ref_addr,
36 => {
37 _ = try p.readOffset(cuh.format);
38 },
39
40 dwarf.FORM.addr => {
41 _ = try p.readNBytes(cuh.address_size);
42 },
43
44 dwarf.FORM.block1,
45 dwarf.FORM.block2,
46 dwarf.FORM.block4,
47 dwarf.FORM.block,
48 => {
49 _ = try p.readBlock(attr.form);
50 },
51
52 dwarf.FORM.exprloc => {
53 _ = try p.readExprLoc();
54 },
55
56 dwarf.FORM.flag_present => {},
57
58 dwarf.FORM.data1,
59 dwarf.FORM.ref1,
60 dwarf.FORM.flag,
61 dwarf.FORM.data2,
62 dwarf.FORM.ref2,
63 dwarf.FORM.data4,
64 dwarf.FORM.ref4,
65 dwarf.FORM.data8,
66 dwarf.FORM.ref8,
67 dwarf.FORM.ref_sig8,
68 dwarf.FORM.udata,
69 dwarf.FORM.ref_udata,
70 dwarf.FORM.sdata,
71 => {
72 _ = try p.readConstant(attr.form);
73 },
74
75 dwarf.FORM.strp,
76 dwarf.FORM.string,
77 => {
78 _ = try p.readString(attr.form, cuh);
79 },
80
81 else => {
82 // TODO better errors
83 log.err("unhandled DW_FORM_* value with identifier {x}", .{attr.form});
84 return error.UnhandledDwFormValue;
85 },
86 };
87 }
88 }
89
90 pub fn readBlock(p: *InfoReader, form: Form) ![]const u8 {
91 const len: u64 = switch (form) {
92 dwarf.FORM.block1 => try p.readByte(),
93 dwarf.FORM.block2 => try p.readInt(u16),
94 dwarf.FORM.block4 => try p.readInt(u32),
95 dwarf.FORM.block => try p.readUleb128(u64),
96 else => unreachable,
97 };
98 return p.readNBytes(len);
99 }
100
101 pub fn readExprLoc(p: *InfoReader) ![]const u8 {
102 const len: u64 = try p.readUleb128(u64);
103 return p.readNBytes(len);
104 }
105
106 pub fn readConstant(p: *InfoReader, form: Form) !u64 {
107 return switch (form) {
108 dwarf.FORM.data1, dwarf.FORM.ref1, dwarf.FORM.flag => try p.readByte(),
109 dwarf.FORM.data2, dwarf.FORM.ref2 => try p.readInt(u16),
110 dwarf.FORM.data4, dwarf.FORM.ref4 => try p.readInt(u32),
111 dwarf.FORM.data8, dwarf.FORM.ref8, dwarf.FORM.ref_sig8 => try p.readInt(u64),
112 dwarf.FORM.udata, dwarf.FORM.ref_udata => try p.readUleb128(u64),
113 dwarf.FORM.sdata => @bitCast(try p.readIleb128(i64)),
114 else => return error.UnhandledConstantForm,
115 };
116 }
117
118 pub fn readString(p: *InfoReader, form: Form, cuh: CompileUnitHeader) ![:0]const u8 {
119 switch (form) {
120 dwarf.FORM.strp => {
121 const off = try p.readOffset(cuh.format);
122 const off_u = math.cast(usize, off) orelse return error.Overflow;
123 return mem.sliceTo(@as([*:0]const u8, @ptrCast(p.strtab.ptr + off_u)), 0);
124 },
125 dwarf.FORM.string => {
126 const start = p.pos;
127 while (p.pos < p.bytes.len) : (p.pos += 1) {
128 if (p.bytes[p.pos] == 0) break;
129 }
130 if (p.bytes[p.pos] != 0) return error.Eof;
131 return p.bytes[start..p.pos :0];
132 },
133 else => unreachable,
134 }
135 }
136
137 pub fn readByte(p: *InfoReader) !u8 {
138 if (p.pos + 1 > p.bytes.len) return error.Eof;
139 defer p.pos += 1;
140 return p.bytes[p.pos];
141 }
142
143 pub fn readNBytes(p: *InfoReader, num: u64) ![]const u8 {
144 const num_usize = math.cast(usize, num) orelse return error.Overflow;
145 if (p.pos + num_usize > p.bytes.len) return error.Eof;
146 defer p.pos += num_usize;
147 return p.bytes[p.pos..][0..num_usize];
148 }
149
150 pub fn readInt(p: *InfoReader, comptime Int: type) !Int {
151 if (p.pos + @sizeOf(Int) > p.bytes.len) return error.Eof;
152 defer p.pos += @sizeOf(Int);
153 return mem.readInt(Int, p.bytes[p.pos..][0..@sizeOf(Int)], .little);
154 }
155
156 pub fn readOffset(p: *InfoReader, dw_fmt: DwarfFormat) !u64 {
157 return switch (dw_fmt) {
158 .dwarf32 => try p.readInt(u32),
159 .dwarf64 => try p.readInt(u64),
160 };
161 }
162
163 pub fn readUleb128(p: *InfoReader, comptime Type: type) !Type {
164 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
165 var creader = std.io.countingReader(stream.reader());
166 const value: Type = try leb.readUleb128(Type, creader.reader());
167 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
168 return value;
169 }
170
171 pub fn readIleb128(p: *InfoReader, comptime Type: type) !Type {
172 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
173 var creader = std.io.countingReader(stream.reader());
174 const value: Type = try leb.readIleb128(Type, creader.reader());
175 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
176 return value;
177 }
178
179 pub fn seekTo(p: *InfoReader, off: u64) !void {
180 p.pos = math.cast(usize, off) orelse return error.Overflow;
181 }
182};
183
184pub const AbbrevReader = struct {
185 bytes: []const u8,
186 pos: usize = 0,
187
188 pub fn hasMore(p: AbbrevReader) bool {
189 return p.pos < p.bytes.len;
190 }
191
192 pub fn readDecl(p: *AbbrevReader) !?AbbrevDecl {
193 const pos = p.pos;
194 const code = try p.readUleb128(Code);
195 if (code == 0) return null;
196
197 const tag = try p.readUleb128(Tag);
198 const has_children = (try p.readByte()) > 0;
199 return .{
200 .code = code,
201 .pos = pos,
202 .len = p.pos - pos,
203 .tag = tag,
204 .has_children = has_children,
205 };
206 }
207
208 pub fn readAttr(p: *AbbrevReader) !?AbbrevAttr {
209 const pos = p.pos;
210 const at = try p.readUleb128(At);
211 const form = try p.readUleb128(Form);
212 return if (at == 0 and form == 0) null else .{
213 .at = at,
214 .form = form,
215 .pos = pos,
216 .len = p.pos - pos,
217 };
218 }
219
220 pub fn readByte(p: *AbbrevReader) !u8 {
221 if (p.pos + 1 > p.bytes.len) return error.Eof;
222 defer p.pos += 1;
223 return p.bytes[p.pos];
224 }
225
226 pub fn readUleb128(p: *AbbrevReader, comptime Type: type) !Type {
227 var stream = std.io.fixedBufferStream(p.bytes[p.pos..]);
228 var creader = std.io.countingReader(stream.reader());
229 const value: Type = try leb.readUleb128(Type, creader.reader());
230 p.pos += math.cast(usize, creader.bytes_read) orelse return error.Overflow;
231 return value;
232 }
233
234 pub fn seekTo(p: *AbbrevReader, off: u64) !void {
235 p.pos = math.cast(usize, off) orelse return error.Overflow;
236 }
237};
238
239const AbbrevDecl = struct {
240 code: Code,
241 pos: usize,
242 len: usize,
243 tag: Tag,
244 has_children: bool,
245};
246
247const AbbrevAttr = struct {
248 at: At,
249 form: Form,
250 pos: usize,
251 len: usize,
252};
253
254const CompileUnitHeader = struct {
255 format: DwarfFormat,
256 length: u64,
257 version: u16,
258 debug_abbrev_offset: u64,
259 address_size: u8,
260};
261
262const Die = struct {
263 pos: usize,
264 len: usize,
265};
266
267const DwarfFormat = enum {
268 dwarf32,
269 dwarf64,
270};
271
272const dwarf = std.dwarf;
273const leb = std.leb;
274const log = std.log.scoped(.link);
275const math = std.math;
276const mem = std.mem;
277const std = @import("std");
278
279const At = u64;
280const Code = u64;
281const Form = u64;
282const Tag = u64;
283
284pub const AT = dwarf.AT;
285pub const FORM = dwarf.FORM;
286pub const TAG = dwarf.TAG;