| ... | ... | @@ -1,32 +1,7 @@ |
| 1 | | const Dwarf = @This(); |
| 2 | | |
| 3 | | const std = @import("std"); |
| 4 | | const builtin = @import("builtin"); |
| 5 | | const assert = std.debug.assert; |
| 6 | | const fs = std.fs; |
| 7 | | const leb128 = std.leb; |
| 8 | | const log = std.log.scoped(.dwarf); |
| 9 | | const mem = std.mem; |
| 10 | | |
| 11 | | const link = @import("../link.zig"); |
| 12 | | const trace = @import("../tracy.zig").trace; |
| 13 | | |
| 14 | | const Allocator = mem.Allocator; |
| 15 | | const DW = std.dwarf; |
| 16 | | const File = link.File; |
| 17 | | const LinkBlock = File.LinkBlock; |
| 18 | | const LinkFn = File.LinkFn; |
| 19 | | const LinkerLoad = @import("../codegen.zig").LinkerLoad; |
| 20 | | const Module = @import("../Module.zig"); |
| 21 | | const InternPool = @import("../InternPool.zig"); |
| 22 | | const StringTable = @import("strtab.zig").StringTable; |
| 23 | | const Type = @import("../type.zig").Type; |
| 24 | | const Value = @import("../value.zig").Value; |
| 25 | | |
| 26 | 1 | allocator: Allocator, |
| 27 | 2 | bin_file: *File, |
| 3 | format: Format, |
| 28 | 4 | ptr_width: PtrWidth, |
| 29 | | target: std.Target, |
| 30 | 5 | |
| 31 | 6 | /// A list of `Atom`s whose Line Number Programs have surplus capacity. |
| 32 | 7 | /// This is the same concept as `Section.free_list` in Elf; see those doc comments. |
| ... | ... | @@ -983,17 +958,17 @@ const min_nop_size = 2; |
| 983 | 958 | /// actual_capacity + (actual_capacity / ideal_factor) |
| 984 | 959 | const ideal_factor = 3; |
| 985 | 960 | |
| 986 | | pub fn init(allocator: Allocator, bin_file: *File, target: std.Target) Dwarf { |
| 987 | | const ptr_width: PtrWidth = switch (target.ptrBitWidth()) { |
| 961 | pub fn init(allocator: Allocator, bin_file: *File, format: Format) Dwarf { |
| 962 | const ptr_width: PtrWidth = switch (bin_file.options.target.ptrBitWidth()) { |
| 988 | 963 | 0...32 => .p32, |
| 989 | 964 | 33...64 => .p64, |
| 990 | 965 | else => unreachable, |
| 991 | 966 | }; |
| 992 | | return Dwarf{ |
| 967 | return .{ |
| 993 | 968 | .allocator = allocator, |
| 994 | 969 | .bin_file = bin_file, |
| 970 | .format = format, |
| 995 | 971 | .ptr_width = ptr_width, |
| 996 | | .target = target, |
| 997 | 972 | }; |
| 998 | 973 | } |
| 999 | 974 | |
| ... | ... | @@ -1129,7 +1104,7 @@ pub fn commitDeclState( |
| 1129 | 1104 | const decl = mod.declPtr(decl_index); |
| 1130 | 1105 | const ip = &mod.intern_pool; |
| 1131 | 1106 | |
| 1132 | | const target_endian = self.target.cpu.arch.endian(); |
| 1107 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 1133 | 1108 | |
| 1134 | 1109 | assert(decl.has_tv); |
| 1135 | 1110 | switch (decl.ty.zigTypeTag(mod)) { |
| ... | ... | @@ -1837,12 +1812,10 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u |
| 1837 | 1812 | var di_buf = try std.ArrayList(u8).initCapacity(self.allocator, needed_bytes); |
| 1838 | 1813 | defer di_buf.deinit(); |
| 1839 | 1814 | |
| 1840 | | const target_endian = self.target.cpu.arch.endian(); |
| 1841 | | const init_len_size: usize = if (self.bin_file.tag == .macho) |
| 1842 | | 4 |
| 1843 | | else switch (self.ptr_width) { |
| 1844 | | .p32 => @as(usize, 4), |
| 1845 | | .p64 => 12, |
| 1815 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 1816 | const init_len_size: usize = switch (self.format) { |
| 1817 | .dwarf32 => 4, |
| 1818 | .dwarf64 => 12, |
| 1846 | 1819 | }; |
| 1847 | 1820 | |
| 1848 | 1821 | // initial length - length of the .debug_info contribution for this compilation unit, |
| ... | ... | @@ -1851,32 +1824,16 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u |
| 1851 | 1824 | const after_init_len = di_buf.items.len + init_len_size; |
| 1852 | 1825 | const dbg_info_end = self.getDebugInfoEnd().?; |
| 1853 | 1826 | const init_len = dbg_info_end - after_init_len; |
| 1854 | | if (self.bin_file.tag == .macho) { |
| 1855 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(init_len))); |
| 1856 | | } else switch (self.ptr_width) { |
| 1857 | | .p32 => { |
| 1858 | | mem.writeInt(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(init_len)), target_endian); |
| 1859 | | }, |
| 1860 | | .p64 => { |
| 1861 | | di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 1862 | | mem.writeInt(u64, di_buf.addManyAsArrayAssumeCapacity(8), init_len, target_endian); |
| 1863 | | }, |
| 1864 | | } |
| 1827 | |
| 1828 | if (self.format == .dwarf64) di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 1829 | self.writeOffsetAssumeCapacity(&di_buf, init_len); |
| 1830 | |
| 1865 | 1831 | mem.writeInt(u16, di_buf.addManyAsArrayAssumeCapacity(2), 4, target_endian); // DWARF version |
| 1866 | 1832 | const abbrev_offset = self.abbrev_table_offset.?; |
| 1867 | | if (self.bin_file.tag == .macho) { |
| 1868 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(abbrev_offset))); |
| 1869 | | di_buf.appendAssumeCapacity(8); // address size |
| 1870 | | } else switch (self.ptr_width) { |
| 1871 | | .p32 => { |
| 1872 | | mem.writeInt(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(abbrev_offset)), target_endian); |
| 1873 | | di_buf.appendAssumeCapacity(4); // address size |
| 1874 | | }, |
| 1875 | | .p64 => { |
| 1876 | | mem.writeInt(u64, di_buf.addManyAsArrayAssumeCapacity(8), abbrev_offset, target_endian); |
| 1877 | | di_buf.appendAssumeCapacity(8); // address size |
| 1878 | | }, |
| 1879 | | } |
| 1833 | |
| 1834 | self.writeOffsetAssumeCapacity(&di_buf, abbrev_offset); |
| 1835 | di_buf.appendAssumeCapacity(self.ptrWidthBytes()); // address size |
| 1836 | |
| 1880 | 1837 | // Write the form for the compile unit, which must match the abbrev table above. |
| 1881 | 1838 | const name_strp = try self.strtab.insert(self.allocator, module.root_mod.root_src_path); |
| 1882 | 1839 | var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | ... | @@ -1885,21 +1842,13 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u |
| 1885 | 1842 | const producer_strp = try self.strtab.insert(self.allocator, link.producer_string); |
| 1886 | 1843 | |
| 1887 | 1844 | di_buf.appendAssumeCapacity(@intFromEnum(AbbrevKind.compile_unit)); |
| 1888 | | if (self.bin_file.tag == .macho) { |
| 1889 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0); // DW.AT.stmt_list, DW.FORM.sec_offset |
| 1890 | | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), low_pc); |
| 1891 | | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), high_pc); |
| 1892 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(name_strp))); |
| 1893 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(comp_dir_strp))); |
| 1894 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(producer_strp))); |
| 1895 | | } else { |
| 1896 | | self.writeAddrAssumeCapacity(&di_buf, 0); // DW.AT.stmt_list, DW.FORM.sec_offset |
| 1897 | | self.writeAddrAssumeCapacity(&di_buf, low_pc); |
| 1898 | | self.writeAddrAssumeCapacity(&di_buf, high_pc); |
| 1899 | | self.writeAddrAssumeCapacity(&di_buf, name_strp); |
| 1900 | | self.writeAddrAssumeCapacity(&di_buf, comp_dir_strp); |
| 1901 | | self.writeAddrAssumeCapacity(&di_buf, producer_strp); |
| 1902 | | } |
| 1845 | self.writeOffsetAssumeCapacity(&di_buf, 0); // DW.AT.stmt_list, DW.FORM.sec_offset |
| 1846 | self.writeAddrAssumeCapacity(&di_buf, low_pc); |
| 1847 | self.writeAddrAssumeCapacity(&di_buf, high_pc); |
| 1848 | self.writeOffsetAssumeCapacity(&di_buf, name_strp); |
| 1849 | self.writeOffsetAssumeCapacity(&di_buf, comp_dir_strp); |
| 1850 | self.writeOffsetAssumeCapacity(&di_buf, producer_strp); |
| 1851 | |
| 1903 | 1852 | // We are still waiting on dwarf-std.org to assign DW_LANG_Zig a number: |
| 1904 | 1853 | // http://dwarfstd.org/ShowIssue.php?issue=171115.1 |
| 1905 | 1854 | // Until then we say it is C99. |
| ... | ... | @@ -1952,13 +1901,26 @@ fn resolveCompilationDir(module: *Module, buffer: *[std.fs.MAX_PATH_BYTES]u8) [] |
| 1952 | 1901 | } |
| 1953 | 1902 | |
| 1954 | 1903 | fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) void { |
| 1955 | | const target_endian = self.target.cpu.arch.endian(); |
| 1904 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 1956 | 1905 | switch (self.ptr_width) { |
| 1957 | 1906 | .p32 => mem.writeInt(u32, buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(addr)), target_endian), |
| 1958 | 1907 | .p64 => mem.writeInt(u64, buf.addManyAsArrayAssumeCapacity(8), addr, target_endian), |
| 1959 | 1908 | } |
| 1960 | 1909 | } |
| 1961 | 1910 | |
| 1911 | fn writeOffsetAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), off: u64) void { |
| 1912 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 1913 | switch (self.format) { |
| 1914 | .dwarf32 => mem.writeInt( |
| 1915 | u32, |
| 1916 | buf.addManyAsArrayAssumeCapacity(4), |
| 1917 | @as(u32, @intCast(off)), |
| 1918 | target_endian, |
| 1919 | ), |
| 1920 | .dwarf64 => mem.writeInt(u64, buf.addManyAsArrayAssumeCapacity(8), off, target_endian), |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1962 | 1924 | /// Writes to the file a buffer, prefixed and suffixed by the specified number of |
| 1963 | 1925 | /// bytes of NOPs. Asserts each padding size is at least `min_nop_size` and total padding bytes |
| 1964 | 1926 | /// are less than 1044480 bytes (if this limit is ever reached, this function can be |
| ... | ... | @@ -2174,13 +2136,7 @@ fn writeDbgInfoNopsToArrayList( |
| 2174 | 2136 | } |
| 2175 | 2137 | |
| 2176 | 2138 | pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { |
| 2177 | | const target_endian = self.target.cpu.arch.endian(); |
| 2178 | | const init_len_size: usize = if (self.bin_file.tag == .macho) |
| 2179 | | 4 |
| 2180 | | else switch (self.ptr_width) { |
| 2181 | | .p32 => @as(usize, 4), |
| 2182 | | .p64 => 12, |
| 2183 | | }; |
| 2139 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 2184 | 2140 | const ptr_width_bytes = self.ptrWidthBytes(); |
| 2185 | 2141 | |
| 2186 | 2142 | // Enough for all the data without resizing. When support for more compilation units |
| ... | ... | @@ -2191,17 +2147,15 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { |
| 2191 | 2147 | // initial length - length of the .debug_aranges contribution for this compilation unit, |
| 2192 | 2148 | // not including the initial length itself. |
| 2193 | 2149 | // We have to come back and write it later after we know the size. |
| 2150 | if (self.format == .dwarf64) di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 2194 | 2151 | const init_len_index = di_buf.items.len; |
| 2195 | | di_buf.items.len += init_len_size; |
| 2152 | self.writeOffsetAssumeCapacity(&di_buf, 0); |
| 2196 | 2153 | const after_init_len = di_buf.items.len; |
| 2197 | 2154 | mem.writeInt(u16, di_buf.addManyAsArrayAssumeCapacity(2), 2, target_endian); // version |
| 2155 | |
| 2198 | 2156 | // When more than one compilation unit is supported, this will be the offset to it. |
| 2199 | 2157 | // For now it is always at offset 0 in .debug_info. |
| 2200 | | if (self.bin_file.tag == .macho) { |
| 2201 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0); // __debug_info offset |
| 2202 | | } else { |
| 2203 | | self.writeAddrAssumeCapacity(&di_buf, 0); // .debug_info offset |
| 2204 | | } |
| 2158 | self.writeOffsetAssumeCapacity(&di_buf, 0); // .debug_info offset |
| 2205 | 2159 | di_buf.appendAssumeCapacity(ptr_width_bytes); // address_size |
| 2206 | 2160 | di_buf.appendAssumeCapacity(0); // segment_selector_size |
| 2207 | 2161 | |
| ... | ... | @@ -2220,18 +2174,14 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { |
| 2220 | 2174 | |
| 2221 | 2175 | // Go back and populate the initial length. |
| 2222 | 2176 | const init_len = di_buf.items.len - after_init_len; |
| 2223 | | if (self.bin_file.tag == .macho) { |
| 2224 | | mem.writeIntLittle(u32, di_buf.items[init_len_index..][0..4], @as(u32, @intCast(init_len))); |
| 2225 | | } else switch (self.ptr_width) { |
| 2226 | | .p32 => { |
| 2227 | | mem.writeInt(u32, di_buf.items[init_len_index..][0..4], @as(u32, @intCast(init_len)), target_endian); |
| 2228 | | }, |
| 2229 | | .p64 => { |
| 2230 | | // initial length - length of the .debug_aranges contribution for this compilation unit, |
| 2231 | | // not including the initial length itself. |
| 2232 | | di_buf.items[init_len_index..][0..4].* = [_]u8{ 0xff, 0xff, 0xff, 0xff }; |
| 2233 | | mem.writeInt(u64, di_buf.items[init_len_index + 4 ..][0..8], init_len, target_endian); |
| 2234 | | }, |
| 2177 | switch (self.format) { |
| 2178 | .dwarf32 => mem.writeInt( |
| 2179 | u32, |
| 2180 | di_buf.items[init_len_index..][0..4], |
| 2181 | @as(u32, @intCast(init_len)), |
| 2182 | target_endian, |
| 2183 | ), |
| 2184 | .dwarf64 => mem.writeInt(u64, di_buf.items[init_len_index..][0..8], init_len, target_endian), |
| 2235 | 2185 | } |
| 2236 | 2186 | |
| 2237 | 2187 | const needed_size = @as(u32, @intCast(di_buf.items.len)); |
| ... | ... | @@ -2265,12 +2215,10 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { |
| 2265 | 2215 | pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2266 | 2216 | const gpa = self.allocator; |
| 2267 | 2217 | |
| 2268 | | const target_endian = self.target.cpu.arch.endian(); |
| 2269 | | const init_len_size: usize = if (self.bin_file.tag == .macho) |
| 2270 | | 4 |
| 2271 | | else switch (self.ptr_width) { |
| 2272 | | .p32 => @as(usize, 4), |
| 2273 | | .p64 => 12, |
| 2218 | const target_endian = self.bin_file.options.target.cpu.arch.endian(); |
| 2219 | const init_len_size: usize = switch (self.format) { |
| 2220 | .dwarf32 => 4, |
| 2221 | .dwarf64 => 12, |
| 2274 | 2222 | }; |
| 2275 | 2223 | |
| 2276 | 2224 | const dbg_line_prg_off = self.getDebugLineProgramOff() orelse return; |
| ... | ... | @@ -2288,20 +2236,8 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2288 | 2236 | var di_buf = try std.ArrayList(u8).initCapacity(gpa, needed_bytes); |
| 2289 | 2237 | defer di_buf.deinit(); |
| 2290 | 2238 | |
| 2291 | | switch (self.bin_file.tag) { |
| 2292 | | .macho => { |
| 2293 | | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, 0)); |
| 2294 | | }, |
| 2295 | | else => switch (self.ptr_width) { |
| 2296 | | .p32 => { |
| 2297 | | mem.writeInt(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, 0), target_endian); |
| 2298 | | }, |
| 2299 | | .p64 => { |
| 2300 | | di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 2301 | | mem.writeInt(u64, di_buf.addManyAsArrayAssumeCapacity(8), @as(u64, 0), target_endian); |
| 2302 | | }, |
| 2303 | | }, |
| 2304 | | } |
| 2239 | if (self.format == .dwarf64) di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 2240 | self.writeOffsetAssumeCapacity(&di_buf, 0); |
| 2305 | 2241 | |
| 2306 | 2242 | mem.writeInt(u16, di_buf.addManyAsArrayAssumeCapacity(2), 4, target_endian); // version |
| 2307 | 2243 | |
| ... | ... | @@ -2310,16 +2246,7 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2310 | 2246 | // Therefore we rely on the NOP jump at the beginning of the Line Number Program for |
| 2311 | 2247 | // padding rather than this field. |
| 2312 | 2248 | const before_header_len = di_buf.items.len; |
| 2313 | | |
| 2314 | | // We will come back and write this. |
| 2315 | | switch (self.bin_file.tag) { |
| 2316 | | .macho => di_buf.appendNTimesAssumeCapacity(0, 4), |
| 2317 | | else => switch (self.ptr_width) { |
| 2318 | | .p32 => di_buf.appendNTimesAssumeCapacity(0, 4), |
| 2319 | | .p64 => di_buf.appendNTimesAssumeCapacity(0, 8), |
| 2320 | | }, |
| 2321 | | } |
| 2322 | | |
| 2249 | self.writeOffsetAssumeCapacity(&di_buf, 0); // We will come back and write this. |
| 2323 | 2250 | const after_header_len = di_buf.items.len; |
| 2324 | 2251 | |
| 2325 | 2252 | const opcode_base = DW.LNS.set_isa + 1; |
| ... | ... | @@ -2372,19 +2299,14 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2372 | 2299 | di_buf.appendAssumeCapacity(0); // file names sentinel |
| 2373 | 2300 | |
| 2374 | 2301 | const header_len = di_buf.items.len - after_header_len; |
| 2375 | | |
| 2376 | | switch (self.bin_file.tag) { |
| 2377 | | .macho => { |
| 2378 | | mem.writeIntLittle(u32, di_buf.items[before_header_len..][0..4], @as(u32, @intCast(header_len))); |
| 2379 | | }, |
| 2380 | | else => switch (self.ptr_width) { |
| 2381 | | .p32 => { |
| 2382 | | mem.writeInt(u32, di_buf.items[before_header_len..][0..4], @as(u32, @intCast(header_len)), target_endian); |
| 2383 | | }, |
| 2384 | | .p64 => { |
| 2385 | | mem.writeInt(u64, di_buf.items[before_header_len..][0..8], header_len, target_endian); |
| 2386 | | }, |
| 2387 | | }, |
| 2302 | switch (self.format) { |
| 2303 | .dwarf32 => mem.writeInt( |
| 2304 | u32, |
| 2305 | di_buf.items[before_header_len..][0..4], |
| 2306 | @as(u32, @intCast(header_len)), |
| 2307 | target_endian, |
| 2308 | ), |
| 2309 | .dwarf64 => mem.writeInt(u64, di_buf.items[before_header_len..][0..8], header_len, target_endian), |
| 2388 | 2310 | } |
| 2389 | 2311 | |
| 2390 | 2312 | assert(needed_bytes == di_buf.items.len); |
| ... | ... | @@ -2453,17 +2375,12 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2453 | 2375 | |
| 2454 | 2376 | // Backpatch actual length of the debug line program |
| 2455 | 2377 | const init_len = self.getDebugLineProgramEnd().? - init_len_size; |
| 2456 | | switch (self.bin_file.tag) { |
| 2457 | | .macho => { |
| 2458 | | mem.writeIntLittle(u32, di_buf.items[0..4], @as(u32, @intCast(init_len))); |
| 2378 | switch (self.format) { |
| 2379 | .dwarf32 => { |
| 2380 | mem.writeInt(u32, di_buf.items[0..4], @as(u32, @intCast(init_len)), target_endian); |
| 2459 | 2381 | }, |
| 2460 | | else => switch (self.ptr_width) { |
| 2461 | | .p32 => { |
| 2462 | | mem.writeInt(u32, di_buf.items[0..4], @as(u32, @intCast(init_len)), target_endian); |
| 2463 | | }, |
| 2464 | | .p64 => { |
| 2465 | | mem.writeInt(u64, di_buf.items[4..][0..8], init_len, target_endian); |
| 2466 | | }, |
| 2382 | .dwarf64 => { |
| 2383 | mem.writeInt(u64, di_buf.items[4..][0..8], init_len, target_endian); |
| 2467 | 2384 | }, |
| 2468 | 2385 | } |
| 2469 | 2386 | |
| ... | ... | @@ -2524,17 +2441,14 @@ fn ptrWidthBytes(self: Dwarf) u8 { |
| 2524 | 2441 | } |
| 2525 | 2442 | |
| 2526 | 2443 | fn dbgLineNeededHeaderBytes(self: Dwarf, dirs: []const []const u8, files: []const []const u8) u32 { |
| 2527 | | var size = switch (self.bin_file.tag) { // length field |
| 2528 | | .macho => @sizeOf(u32), |
| 2529 | | else => switch (self.ptr_width) { |
| 2530 | | .p32 => @as(usize, @sizeOf(u32)), |
| 2531 | | .p64 => @sizeOf(u32) + @sizeOf(u64), |
| 2532 | | }, |
| 2444 | var size: usize = switch (self.format) { // length field |
| 2445 | .dwarf32 => @as(usize, 4), |
| 2446 | .dwarf64 => 12, |
| 2533 | 2447 | }; |
| 2534 | 2448 | size += @sizeOf(u16); // version field |
| 2535 | | size += switch (self.bin_file.tag) { // offset to end-of-header |
| 2536 | | .macho => @sizeOf(u32), |
| 2537 | | else => self.ptrWidthBytes(), |
| 2449 | size += switch (self.format) { // offset to end-of-header |
| 2450 | .dwarf32 => @as(usize, 4), |
| 2451 | .dwarf64 => 8, |
| 2538 | 2452 | }; |
| 2539 | 2453 | size += 18; // opcodes |
| 2540 | 2454 | |
| ... | ... | @@ -2570,6 +2484,8 @@ fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 2570 | 2484 | } |
| 2571 | 2485 | |
| 2572 | 2486 | pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2487 | const target = self.bin_file.options.target; |
| 2488 | |
| 2573 | 2489 | if (self.global_abbrev_relocs.items.len > 0) { |
| 2574 | 2490 | const gpa = self.allocator; |
| 2575 | 2491 | var arena_alloc = std.heap.ArenaAllocator.init(gpa); |
| ... | ... | @@ -2581,7 +2497,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2581 | 2497 | module, |
| 2582 | 2498 | Type.anyerror, |
| 2583 | 2499 | module.global_error_set.keys(), |
| 2584 | | self.target, |
| 2500 | target, |
| 2585 | 2501 | &dbg_info_buffer, |
| 2586 | 2502 | ); |
| 2587 | 2503 | |
| ... | ... | @@ -2610,7 +2526,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2610 | 2526 | }; |
| 2611 | 2527 | |
| 2612 | 2528 | var buf: [@sizeOf(u32)]u8 = undefined; |
| 2613 | | mem.writeInt(u32, &buf, self.getAtom(.di_atom, di_atom_index).off, self.target.cpu.arch.endian()); |
| 2529 | mem.writeInt(u32, &buf, self.getAtom(.di_atom, di_atom_index).off, target.cpu.arch.endian()); |
| 2614 | 2530 | |
| 2615 | 2531 | while (self.global_abbrev_relocs.popOrNull()) |reloc| { |
| 2616 | 2532 | const atom = self.getAtom(.di_atom, reloc.atom_index); |
| ... | ... | @@ -2805,3 +2721,33 @@ fn getAtomPtr(self: *Dwarf, comptime kind: Kind, index: Atom.Index) *Atom { |
| 2805 | 2721 | .di_atom => &self.di_atoms.items[index], |
| 2806 | 2722 | }; |
| 2807 | 2723 | } |
| 2724 | |
| 2725 | pub const Format = enum { |
| 2726 | dwarf32, |
| 2727 | dwarf64, |
| 2728 | }; |
| 2729 | |
| 2730 | const Dwarf = @This(); |
| 2731 | |
| 2732 | const std = @import("std"); |
| 2733 | const builtin = @import("builtin"); |
| 2734 | const assert = std.debug.assert; |
| 2735 | const fs = std.fs; |
| 2736 | const leb128 = std.leb; |
| 2737 | const log = std.log.scoped(.dwarf); |
| 2738 | const mem = std.mem; |
| 2739 | |
| 2740 | const link = @import("../link.zig"); |
| 2741 | const trace = @import("../tracy.zig").trace; |
| 2742 | |
| 2743 | const Allocator = mem.Allocator; |
| 2744 | const DW = std.dwarf; |
| 2745 | const File = link.File; |
| 2746 | const LinkBlock = File.LinkBlock; |
| 2747 | const LinkFn = File.LinkFn; |
| 2748 | const LinkerLoad = @import("../codegen.zig").LinkerLoad; |
| 2749 | const Module = @import("../Module.zig"); |
| 2750 | const InternPool = @import("../InternPool.zig"); |
| 2751 | const StringTable = @import("strtab.zig").StringTable; |
| 2752 | const Type = @import("../type.zig").Type; |
| 2753 | const Value = @import("../value.zig").Value; |