| author | |
| committer | |
| log | 5de2aae63cd75322e58204a6be8df49754e4851a |
| tree | c8e61e82649f412b6a7c4b5bb422f9855df61665 |
| parent | d98fc53b8fbe479f828114b0276d5290146cc2a3 |
19 files changed, 663 insertions(+), 665 deletions(-)
src/Compilation.zig+1-1| ... | ... | @@ -3299,7 +3299,7 @@ fn processOneJob(comp: *Compilation, job: Job) !void { |
| 3299 | 3299 | const gpa = comp.gpa; |
| 3300 | 3300 | const module = comp.bin_file.options.module.?; |
| 3301 | 3301 | const decl = module.declPtr(decl_index); |
| 3302 | comp.bin_file.updateDeclLineNumber(module, decl) catch |err| { | |
| 3302 | comp.bin_file.updateDeclLineNumber(module, decl_index) catch |err| { | |
| 3303 | 3303 | try module.failed_decls.ensureUnusedCapacity(gpa, 1); |
| 3304 | 3304 | module.failed_decls.putAssumeCapacityNoClobber(decl_index, try Module.ErrorMsg.create( |
| 3305 | 3305 | gpa, |
src/Module.zig+6-6| ... | ... | @@ -5186,12 +5186,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err |
| 5186 | 5186 | .coff => { |
| 5187 | 5187 | // TODO Implement for COFF |
| 5188 | 5188 | }, |
| 5189 | .elf => if (decl.fn_link.elf.len != 0) { | |
| 5189 | .elf => { | |
| 5190 | 5190 | // TODO Look into detecting when this would be unnecessary by storing enough state |
| 5191 | 5191 | // in `Decl` to notice that the line number did not change. |
| 5192 | 5192 | comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index }); |
| 5193 | 5193 | }, |
| 5194 | .macho => if (decl.fn_link.macho.len != 0) { | |
| 5194 | .macho => { | |
| 5195 | 5195 | // TODO Look into detecting when this would be unnecessary by storing enough state |
| 5196 | 5196 | // in `Decl` to notice that the line number did not change. |
| 5197 | 5197 | comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index }); |
| ... | ... | @@ -5285,8 +5285,8 @@ pub fn clearDecl( |
| 5285 | 5285 | }; |
| 5286 | 5286 | decl.fn_link = switch (mod.comp.bin_file.tag) { |
| 5287 | 5287 | .coff => .{ .coff = {} }, |
| 5288 | .elf => .{ .elf = link.File.Dwarf.SrcFn.empty }, | |
| 5289 | .macho => .{ .macho = link.File.Dwarf.SrcFn.empty }, | |
| 5288 | .elf => .{ .elf = {} }, | |
| 5289 | .macho => .{ .macho = {} }, | |
| 5290 | 5290 | .plan9 => .{ .plan9 = {} }, |
| 5291 | 5291 | .c => .{ .c = {} }, |
| 5292 | 5292 | .wasm => .{ .wasm = link.File.Wasm.FnData.empty }, |
| ... | ... | @@ -5705,8 +5705,8 @@ pub fn allocateNewDecl( |
| 5705 | 5705 | }, |
| 5706 | 5706 | .fn_link = switch (mod.comp.bin_file.tag) { |
| 5707 | 5707 | .coff => .{ .coff = {} }, |
| 5708 | .elf => .{ .elf = link.File.Dwarf.SrcFn.empty }, | |
| 5709 | .macho => .{ .macho = link.File.Dwarf.SrcFn.empty }, | |
| 5708 | .elf => .{ .elf = {} }, | |
| 5709 | .macho => .{ .macho = {} }, | |
| 5710 | 5710 | .plan9 => .{ .plan9 = {} }, |
| 5711 | 5711 | .c => .{ .c = {} }, |
| 5712 | 5712 | .wasm => .{ .wasm = link.File.Wasm.FnData.empty }, |
src/arch/aarch64/CodeGen.zig+2-15| ... | ... | @@ -203,13 +203,7 @@ const DbgInfoReloc = struct { |
| 203 | 203 | else => unreachable, // not a possible argument |
| 204 | 204 | |
| 205 | 205 | }; |
| 206 | try dw.genArgDbgInfo( | |
| 207 | reloc.name, | |
| 208 | reloc.ty, | |
| 209 | function.bin_file.tag, | |
| 210 | function.mod_fn.owner_decl, | |
| 211 | loc, | |
| 212 | ); | |
| 206 | try dw.genArgDbgInfo(reloc.name, reloc.ty, function.mod_fn.owner_decl, loc); | |
| 213 | 207 | }, |
| 214 | 208 | .plan9 => {}, |
| 215 | 209 | .none => {}, |
| ... | ... | @@ -255,14 +249,7 @@ const DbgInfoReloc = struct { |
| 255 | 249 | break :blk .nop; |
| 256 | 250 | }, |
| 257 | 251 | }; |
| 258 | try dw.genVarDbgInfo( | |
| 259 | reloc.name, | |
| 260 | reloc.ty, | |
| 261 | function.bin_file.tag, | |
| 262 | function.mod_fn.owner_decl, | |
| 263 | is_ptr, | |
| 264 | loc, | |
| 265 | ); | |
| 252 | try dw.genVarDbgInfo(reloc.name, reloc.ty, function.mod_fn.owner_decl, is_ptr, loc); | |
| 266 | 253 | }, |
| 267 | 254 | .plan9 => {}, |
| 268 | 255 | .none => {}, |
src/arch/arm/CodeGen.zig+2-15| ... | ... | @@ -282,13 +282,7 @@ const DbgInfoReloc = struct { |
| 282 | 282 | else => unreachable, // not a possible argument |
| 283 | 283 | }; |
| 284 | 284 | |
| 285 | try dw.genArgDbgInfo( | |
| 286 | reloc.name, | |
| 287 | reloc.ty, | |
| 288 | function.bin_file.tag, | |
| 289 | function.mod_fn.owner_decl, | |
| 290 | loc, | |
| 291 | ); | |
| 285 | try dw.genArgDbgInfo(reloc.name, reloc.ty, function.mod_fn.owner_decl, loc); | |
| 292 | 286 | }, |
| 293 | 287 | .plan9 => {}, |
| 294 | 288 | .none => {}, |
| ... | ... | @@ -331,14 +325,7 @@ const DbgInfoReloc = struct { |
| 331 | 325 | break :blk .nop; |
| 332 | 326 | }, |
| 333 | 327 | }; |
| 334 | try dw.genVarDbgInfo( | |
| 335 | reloc.name, | |
| 336 | reloc.ty, | |
| 337 | function.bin_file.tag, | |
| 338 | function.mod_fn.owner_decl, | |
| 339 | is_ptr, | |
| 340 | loc, | |
| 341 | ); | |
| 328 | try dw.genVarDbgInfo(reloc.name, reloc.ty, function.mod_fn.owner_decl, is_ptr, loc); | |
| 342 | 329 | }, |
| 343 | 330 | .plan9 => {}, |
| 344 | 331 | .none => {}, |
src/arch/riscv64/CodeGen.zig+3-7| ... | ... | @@ -1615,13 +1615,9 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 1615 | 1615 | |
| 1616 | 1616 | switch (self.debug_output) { |
| 1617 | 1617 | .dwarf => |dw| switch (mcv) { |
| 1618 | .register => |reg| try dw.genArgDbgInfo( | |
| 1619 | name, | |
| 1620 | ty, | |
| 1621 | self.bin_file.tag, | |
| 1622 | self.mod_fn.owner_decl, | |
| 1623 | .{ .register = reg.dwarfLocOp() }, | |
| 1624 | ), | |
| 1618 | .register => |reg| try dw.genArgDbgInfo(name, ty, self.mod_fn.owner_decl, .{ | |
| 1619 | .register = reg.dwarfLocOp(), | |
| 1620 | }), | |
| 1625 | 1621 | .stack_offset => {}, |
| 1626 | 1622 | else => {}, |
| 1627 | 1623 | }, |
src/arch/sparc64/CodeGen.zig+3-7| ... | ... | @@ -3412,13 +3412,9 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3412 | 3412 | |
| 3413 | 3413 | switch (self.debug_output) { |
| 3414 | 3414 | .dwarf => |dw| switch (mcv) { |
| 3415 | .register => |reg| try dw.genArgDbgInfo( | |
| 3416 | name, | |
| 3417 | ty, | |
| 3418 | self.bin_file.tag, | |
| 3419 | self.mod_fn.owner_decl, | |
| 3420 | .{ .register = reg.dwarfLocOp() }, | |
| 3421 | ), | |
| 3415 | .register => |reg| try dw.genArgDbgInfo(name, ty, self.mod_fn.owner_decl, .{ | |
| 3416 | .register = reg.dwarfLocOp(), | |
| 3417 | }), | |
| 3422 | 3418 | else => {}, |
| 3423 | 3419 | }, |
| 3424 | 3420 | else => {}, |
src/arch/wasm/CodeGen.zig+2-2| ... | ... | @@ -2475,7 +2475,7 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2475 | 2475 | .dwarf => |dwarf| { |
| 2476 | 2476 | const src_index = func.air.instructions.items(.data)[inst].arg.src_index; |
| 2477 | 2477 | const name = func.mod_fn.getParamName(func.bin_file.base.options.module.?, src_index); |
| 2478 | try dwarf.genArgDbgInfo(name, arg_ty, .wasm, func.mod_fn.owner_decl, .{ | |
| 2478 | try dwarf.genArgDbgInfo(name, arg_ty, func.mod_fn.owner_decl, .{ | |
| 2479 | 2479 | .wasm_local = arg.local.value, |
| 2480 | 2480 | }); |
| 2481 | 2481 | }, |
| ... | ... | @@ -5539,7 +5539,7 @@ fn airDbgVar(func: *CodeGen, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 5539 | 5539 | break :blk .nop; |
| 5540 | 5540 | }, |
| 5541 | 5541 | }; |
| 5542 | try func.debug_output.dwarf.genVarDbgInfo(name, ty, .wasm, func.mod_fn.owner_decl, is_ptr, loc); | |
| 5542 | try func.debug_output.dwarf.genVarDbgInfo(name, ty, func.mod_fn.owner_decl, is_ptr, loc); | |
| 5543 | 5543 | |
| 5544 | 5544 | func.finishAir(inst, .none, &.{}); |
| 5545 | 5545 | } |
src/arch/x86_64/CodeGen.zig+2-2| ... | ... | @@ -3836,7 +3836,7 @@ fn genArgDbgInfo(self: Self, ty: Type, name: [:0]const u8, mcv: MCValue) !void { |
| 3836 | 3836 | }, |
| 3837 | 3837 | else => unreachable, // not a valid function parameter |
| 3838 | 3838 | }; |
| 3839 | try dw.genArgDbgInfo(name, ty, self.bin_file.tag, self.mod_fn.owner_decl, loc); | |
| 3839 | try dw.genArgDbgInfo(name, ty, self.mod_fn.owner_decl, loc); | |
| 3840 | 3840 | }, |
| 3841 | 3841 | .plan9 => {}, |
| 3842 | 3842 | .none => {}, |
| ... | ... | @@ -3876,7 +3876,7 @@ fn genVarDbgInfo( |
| 3876 | 3876 | break :blk .nop; |
| 3877 | 3877 | }, |
| 3878 | 3878 | }; |
| 3879 | try dw.genVarDbgInfo(name, ty, self.bin_file.tag, self.mod_fn.owner_decl, is_ptr, loc); | |
| 3879 | try dw.genVarDbgInfo(name, ty, self.mod_fn.owner_decl, is_ptr, loc); | |
| 3880 | 3880 | }, |
| 3881 | 3881 | .plan9 => {}, |
| 3882 | 3882 | .none => {}, |
src/link.zig+12-11| ... | ... | @@ -273,9 +273,9 @@ pub const File = struct { |
| 273 | 273 | }; |
| 274 | 274 | |
| 275 | 275 | pub const LinkFn = union { |
| 276 | elf: Dwarf.SrcFn, | |
| 277 | coff: Coff.SrcFn, | |
| 278 | macho: Dwarf.SrcFn, | |
| 276 | elf: void, | |
| 277 | coff: void, | |
| 278 | macho: void, | |
| 279 | 279 | plan9: void, |
| 280 | 280 | c: void, |
| 281 | 281 | wasm: Wasm.FnData, |
| ... | ... | @@ -580,22 +580,23 @@ pub const File = struct { |
| 580 | 580 | } |
| 581 | 581 | } |
| 582 | 582 | |
| 583 | pub fn updateDeclLineNumber(base: *File, module: *Module, decl: *Module.Decl) UpdateDeclError!void { | |
| 583 | pub fn updateDeclLineNumber(base: *File, module: *Module, decl_index: Module.Decl.Index) UpdateDeclError!void { | |
| 584 | const decl = module.declPtr(decl_index); | |
| 584 | 585 | log.debug("updateDeclLineNumber {*} ({s}), line={}", .{ |
| 585 | 586 | decl, decl.name, decl.src_line + 1, |
| 586 | 587 | }); |
| 587 | 588 | assert(decl.has_tv); |
| 588 | 589 | if (build_options.only_c) { |
| 589 | 590 | assert(base.tag == .c); |
| 590 | return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl); | |
| 591 | return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl_index); | |
| 591 | 592 | } |
| 592 | 593 | switch (base.tag) { |
| 593 | .coff => return @fieldParentPtr(Coff, "base", base).updateDeclLineNumber(module, decl), | |
| 594 | .elf => return @fieldParentPtr(Elf, "base", base).updateDeclLineNumber(module, decl), | |
| 595 | .macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl), | |
| 596 | .c => return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl), | |
| 597 | .wasm => return @fieldParentPtr(Wasm, "base", base).updateDeclLineNumber(module, decl), | |
| 598 | .plan9 => return @fieldParentPtr(Plan9, "base", base).updateDeclLineNumber(module, decl), | |
| 594 | .coff => return @fieldParentPtr(Coff, "base", base).updateDeclLineNumber(module, decl_index), | |
| 595 | .elf => return @fieldParentPtr(Elf, "base", base).updateDeclLineNumber(module, decl_index), | |
| 596 | .macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl_index), | |
| 597 | .c => return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl_index), | |
| 598 | .wasm => return @fieldParentPtr(Wasm, "base", base).updateDeclLineNumber(module, decl_index), | |
| 599 | .plan9 => return @fieldParentPtr(Plan9, "base", base).updateDeclLineNumber(module, decl_index), | |
| 599 | 600 | .spirv, .nvptx => {}, |
| 600 | 601 | } |
| 601 | 602 | } |
src/link/C.zig+2-2| ... | ... | @@ -219,12 +219,12 @@ pub fn updateDecl(self: *C, module: *Module, decl_index: Module.Decl.Index) !voi |
| 219 | 219 | code.shrinkAndFree(module.gpa, code.items.len); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | pub fn updateDeclLineNumber(self: *C, module: *Module, decl: *Module.Decl) !void { | |
| 222 | pub fn updateDeclLineNumber(self: *C, module: *Module, decl_index: Module.Decl.Index) !void { | |
| 223 | 223 | // The C backend does not have the ability to fix line numbers without re-generating |
| 224 | 224 | // the entire Decl. |
| 225 | 225 | _ = self; |
| 226 | 226 | _ = module; |
| 227 | _ = decl; | |
| 227 | _ = decl_index; | |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | pub fn flush(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
src/link/Coff.zig+2-3| ... | ... | @@ -195,7 +195,6 @@ pub const PtrWidth = enum { |
| 195 | 195 | }; |
| 196 | 196 | } |
| 197 | 197 | }; |
| 198 | pub const SrcFn = void; | |
| 199 | 198 | |
| 200 | 199 | pub const SymbolWithLoc = struct { |
| 201 | 200 | // Index into the respective symbol table. |
| ... | ... | @@ -1545,10 +1544,10 @@ pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 { |
| 1545 | 1544 | return global_index; |
| 1546 | 1545 | } |
| 1547 | 1546 | |
| 1548 | pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void { | |
| 1547 | pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !void { | |
| 1549 | 1548 | _ = self; |
| 1550 | 1549 | _ = module; |
| 1551 | _ = decl; | |
| 1550 | _ = decl_index; | |
| 1552 | 1551 | log.debug("TODO implement updateDeclLineNumber", .{}); |
| 1553 | 1552 | } |
| 1554 | 1553 |
src/link/Dwarf.zig+292-224| ... | ... | @@ -27,17 +27,21 @@ bin_file: *File, |
| 27 | 27 | ptr_width: PtrWidth, |
| 28 | 28 | target: std.Target, |
| 29 | 29 | |
| 30 | /// A list of `File.LinkFn` whose Line Number Programs have surplus capacity. | |
| 31 | /// This is the same concept as `text_block_free_list`; see those doc comments. | |
| 32 | dbg_line_fn_free_list: std.AutoHashMapUnmanaged(*SrcFn, void) = .{}, | |
| 33 | dbg_line_fn_first: ?*SrcFn = null, | |
| 34 | dbg_line_fn_last: ?*SrcFn = null, | |
| 30 | /// A list of `Atom`s whose Line Number Programs have surplus capacity. | |
| 31 | /// This is the same concept as `Section.free_list` in Elf; see those doc comments. | |
| 32 | src_fn_free_list: std.AutoHashMapUnmanaged(Atom.Index, void) = .{}, | |
| 33 | src_fn_first_index: ?Atom.Index = null, | |
| 34 | src_fn_last_index: ?Atom.Index = null, | |
| 35 | src_fns: std.ArrayListUnmanaged(Atom) = .{}, | |
| 36 | src_fn_decls: AtomTable = .{}, | |
| 35 | 37 | |
| 36 | 38 | /// A list of `Atom`s whose corresponding .debug_info tags have surplus capacity. |
| 37 | 39 | /// This is the same concept as `text_block_free_list`; see those doc comments. |
| 38 | atom_free_list: std.AutoHashMapUnmanaged(*Atom, void) = .{}, | |
| 39 | atom_first: ?*Atom = null, | |
| 40 | atom_last: ?*Atom = null, | |
| 40 | di_atom_free_list: std.AutoHashMapUnmanaged(Atom.Index, void) = .{}, | |
| 41 | di_atom_first_index: ?Atom.Index = null, | |
| 42 | di_atom_last_index: ?Atom.Index = null, | |
| 43 | di_atoms: std.ArrayListUnmanaged(Atom) = .{}, | |
| 44 | di_atom_decls: AtomTable = .{}, | |
| 41 | 45 | |
| 42 | 46 | abbrev_table_offset: ?u64 = null, |
| 43 | 47 | |
| ... | ... | @@ -51,22 +55,23 @@ strtab: StringTable(.strtab) = .{}, |
| 51 | 55 | /// * []file_names |
| 52 | 56 | di_files: std.AutoArrayHashMapUnmanaged(*const Module.File, void) = .{}, |
| 53 | 57 | |
| 54 | /// List of atoms that are owned directly by the DWARF module. | |
| 55 | /// TODO convert links in DebugInfoAtom into indices and make | |
| 56 | /// sure every atom is owned by this module. | |
| 57 | managed_atoms: std.ArrayListUnmanaged(*Atom) = .{}, | |
| 58 | ||
| 59 | 58 | global_abbrev_relocs: std.ArrayListUnmanaged(AbbrevRelocation) = .{}, |
| 60 | 59 | |
| 61 | pub const Atom = struct { | |
| 62 | /// Previous/next linked list pointers. | |
| 63 | /// This is the linked list node for this Decl's corresponding .debug_info tag. | |
| 64 | prev: ?*Atom, | |
| 65 | next: ?*Atom, | |
| 66 | /// Offset into .debug_info pointing to the tag for this Decl. | |
| 60 | const AtomTable = std.AutoHashMapUnmanaged(Module.Decl.Index, Atom.Index); | |
| 61 | ||
| 62 | const Atom = struct { | |
| 63 | /// Offset into .debug_info pointing to the tag for this Decl, or | |
| 64 | /// offset from the beginning of the Debug Line Program header that contains this function. | |
| 67 | 65 | off: u32, |
| 68 | /// Size of the .debug_info tag for this Decl, not including padding. | |
| 66 | /// Size of the .debug_info tag for this Decl, not including padding, or | |
| 67 | /// size of the line number program component belonging to this function, not | |
| 68 | /// including padding. | |
| 69 | 69 | len: u32, |
| 70 | ||
| 71 | prev_index: ?Index, | |
| 72 | next_index: ?Index, | |
| 73 | ||
| 74 | pub const Index = u32; | |
| 70 | 75 | }; |
| 71 | 76 | |
| 72 | 77 | /// Represents state of the analysed Decl. |
| ... | ... | @@ -76,6 +81,7 @@ pub const Atom = struct { |
| 76 | 81 | pub const DeclState = struct { |
| 77 | 82 | gpa: Allocator, |
| 78 | 83 | mod: *Module, |
| 84 | di_atom_decls: *const AtomTable, | |
| 79 | 85 | dbg_line: std.ArrayList(u8), |
| 80 | 86 | dbg_info: std.ArrayList(u8), |
| 81 | 87 | abbrev_type_arena: std.heap.ArenaAllocator, |
| ... | ... | @@ -89,10 +95,11 @@ pub const DeclState = struct { |
| 89 | 95 | abbrev_relocs: std.ArrayListUnmanaged(AbbrevRelocation) = .{}, |
| 90 | 96 | exprloc_relocs: std.ArrayListUnmanaged(ExprlocRelocation) = .{}, |
| 91 | 97 | |
| 92 | fn init(gpa: Allocator, mod: *Module) DeclState { | |
| 98 | fn init(gpa: Allocator, mod: *Module, di_atom_decls: *const AtomTable) DeclState { | |
| 93 | 99 | return .{ |
| 94 | 100 | .gpa = gpa, |
| 95 | 101 | .mod = mod, |
| 102 | .di_atom_decls = di_atom_decls, | |
| 96 | 103 | .dbg_line = std.ArrayList(u8).init(gpa), |
| 97 | 104 | .dbg_info = std.ArrayList(u8).init(gpa), |
| 98 | 105 | .abbrev_type_arena = std.heap.ArenaAllocator.init(gpa), |
| ... | ... | @@ -120,11 +127,11 @@ pub const DeclState = struct { |
| 120 | 127 | |
| 121 | 128 | /// Adds local type relocation of the form: @offset => @this + addend |
| 122 | 129 | /// @this signifies the offset within the .debug_abbrev section of the containing atom. |
| 123 | fn addTypeRelocLocal(self: *DeclState, atom: *const Atom, offset: u32, addend: u32) !void { | |
| 130 | fn addTypeRelocLocal(self: *DeclState, atom_index: Atom.Index, offset: u32, addend: u32) !void { | |
| 124 | 131 | log.debug("{x}: @this + {x}", .{ offset, addend }); |
| 125 | 132 | try self.abbrev_relocs.append(self.gpa, .{ |
| 126 | 133 | .target = null, |
| 127 | .atom = atom, | |
| 134 | .atom_index = atom_index, | |
| 128 | 135 | .offset = offset, |
| 129 | 136 | .addend = addend, |
| 130 | 137 | }); |
| ... | ... | @@ -133,13 +140,13 @@ pub const DeclState = struct { |
| 133 | 140 | /// Adds global type relocation of the form: @offset => @symbol + 0 |
| 134 | 141 | /// @symbol signifies a type abbreviation posititioned somewhere in the .debug_abbrev section |
| 135 | 142 | /// which we use as our target of the relocation. |
| 136 | fn addTypeRelocGlobal(self: *DeclState, atom: *const Atom, ty: Type, offset: u32) !void { | |
| 143 | fn addTypeRelocGlobal(self: *DeclState, atom_index: Atom.Index, ty: Type, offset: u32) !void { | |
| 137 | 144 | const resolv = self.abbrev_resolver.getContext(ty, .{ |
| 138 | 145 | .mod = self.mod, |
| 139 | 146 | }) orelse blk: { |
| 140 | 147 | const sym_index = @intCast(u32, self.abbrev_table.items.len); |
| 141 | 148 | try self.abbrev_table.append(self.gpa, .{ |
| 142 | .atom = atom, | |
| 149 | .atom_index = atom_index, | |
| 143 | 150 | .type = ty, |
| 144 | 151 | .offset = undefined, |
| 145 | 152 | }); |
| ... | ... | @@ -154,7 +161,7 @@ pub const DeclState = struct { |
| 154 | 161 | log.debug("{x}: %{d} + 0", .{ offset, resolv }); |
| 155 | 162 | try self.abbrev_relocs.append(self.gpa, .{ |
| 156 | 163 | .target = resolv, |
| 157 | .atom = atom, | |
| 164 | .atom_index = atom_index, | |
| 158 | 165 | .offset = offset, |
| 159 | 166 | .addend = 0, |
| 160 | 167 | }); |
| ... | ... | @@ -163,7 +170,7 @@ pub const DeclState = struct { |
| 163 | 170 | fn addDbgInfoType( |
| 164 | 171 | self: *DeclState, |
| 165 | 172 | module: *Module, |
| 166 | atom: *Atom, | |
| 173 | atom_index: Atom.Index, | |
| 167 | 174 | ty: Type, |
| 168 | 175 | ) error{OutOfMemory}!void { |
| 169 | 176 | const arena = self.abbrev_type_arena.allocator(); |
| ... | ... | @@ -228,7 +235,7 @@ pub const DeclState = struct { |
| 228 | 235 | // DW.AT.type, DW.FORM.ref4 |
| 229 | 236 | var index = dbg_info_buffer.items.len; |
| 230 | 237 | try dbg_info_buffer.resize(index + 4); |
| 231 | try self.addTypeRelocGlobal(atom, Type.bool, @intCast(u32, index)); | |
| 238 | try self.addTypeRelocGlobal(atom_index, Type.bool, @intCast(u32, index)); | |
| 232 | 239 | // DW.AT.data_member_location, DW.FORM.sdata |
| 233 | 240 | try dbg_info_buffer.ensureUnusedCapacity(6); |
| 234 | 241 | dbg_info_buffer.appendAssumeCapacity(0); |
| ... | ... | @@ -240,7 +247,7 @@ pub const DeclState = struct { |
| 240 | 247 | // DW.AT.type, DW.FORM.ref4 |
| 241 | 248 | index = dbg_info_buffer.items.len; |
| 242 | 249 | try dbg_info_buffer.resize(index + 4); |
| 243 | try self.addTypeRelocGlobal(atom, payload_ty, @intCast(u32, index)); | |
| 250 | try self.addTypeRelocGlobal(atom_index, payload_ty, @intCast(u32, index)); | |
| 244 | 251 | // DW.AT.data_member_location, DW.FORM.sdata |
| 245 | 252 | const offset = abi_size - payload_ty.abiSize(target); |
| 246 | 253 | try leb128.writeULEB128(dbg_info_buffer.writer(), offset); |
| ... | ... | @@ -271,7 +278,7 @@ pub const DeclState = struct { |
| 271 | 278 | try dbg_info_buffer.resize(index + 4); |
| 272 | 279 | var buf = try arena.create(Type.SlicePtrFieldTypeBuffer); |
| 273 | 280 | const ptr_ty = ty.slicePtrFieldType(buf); |
| 274 | try self.addTypeRelocGlobal(atom, ptr_ty, @intCast(u32, index)); | |
| 281 | try self.addTypeRelocGlobal(atom_index, ptr_ty, @intCast(u32, index)); | |
| 275 | 282 | // DW.AT.data_member_location, DW.FORM.sdata |
| 276 | 283 | try dbg_info_buffer.ensureUnusedCapacity(6); |
| 277 | 284 | dbg_info_buffer.appendAssumeCapacity(0); |
| ... | ... | @@ -283,7 +290,7 @@ pub const DeclState = struct { |
| 283 | 290 | // DW.AT.type, DW.FORM.ref4 |
| 284 | 291 | index = dbg_info_buffer.items.len; |
| 285 | 292 | try dbg_info_buffer.resize(index + 4); |
| 286 | try self.addTypeRelocGlobal(atom, Type.usize, @intCast(u32, index)); | |
| 293 | try self.addTypeRelocGlobal(atom_index, Type.usize, @intCast(u32, index)); | |
| 287 | 294 | // DW.AT.data_member_location, DW.FORM.sdata |
| 288 | 295 | try dbg_info_buffer.ensureUnusedCapacity(2); |
| 289 | 296 | dbg_info_buffer.appendAssumeCapacity(ptr_bytes); |
| ... | ... | @@ -295,7 +302,7 @@ pub const DeclState = struct { |
| 295 | 302 | // DW.AT.type, DW.FORM.ref4 |
| 296 | 303 | const index = dbg_info_buffer.items.len; |
| 297 | 304 | try dbg_info_buffer.resize(index + 4); |
| 298 | try self.addTypeRelocGlobal(atom, ty.childType(), @intCast(u32, index)); | |
| 305 | try self.addTypeRelocGlobal(atom_index, ty.childType(), @intCast(u32, index)); | |
| 299 | 306 | } |
| 300 | 307 | }, |
| 301 | 308 | .Array => { |
| ... | ... | @@ -306,13 +313,13 @@ pub const DeclState = struct { |
| 306 | 313 | // DW.AT.type, DW.FORM.ref4 |
| 307 | 314 | var index = dbg_info_buffer.items.len; |
| 308 | 315 | try dbg_info_buffer.resize(index + 4); |
| 309 | try self.addTypeRelocGlobal(atom, ty.childType(), @intCast(u32, index)); | |
| 316 | try self.addTypeRelocGlobal(atom_index, ty.childType(), @intCast(u32, index)); | |
| 310 | 317 | // DW.AT.subrange_type |
| 311 | 318 | try dbg_info_buffer.append(@enumToInt(AbbrevKind.array_dim)); |
| 312 | 319 | // DW.AT.type, DW.FORM.ref4 |
| 313 | 320 | index = dbg_info_buffer.items.len; |
| 314 | 321 | try dbg_info_buffer.resize(index + 4); |
| 315 | try self.addTypeRelocGlobal(atom, Type.usize, @intCast(u32, index)); | |
| 322 | try self.addTypeRelocGlobal(atom_index, Type.usize, @intCast(u32, index)); | |
| 316 | 323 | // DW.AT.count, DW.FORM.udata |
| 317 | 324 | const len = ty.arrayLenIncludingSentinel(); |
| 318 | 325 | try leb128.writeULEB128(dbg_info_buffer.writer(), len); |
| ... | ... | @@ -340,7 +347,7 @@ pub const DeclState = struct { |
| 340 | 347 | // DW.AT.type, DW.FORM.ref4 |
| 341 | 348 | var index = dbg_info_buffer.items.len; |
| 342 | 349 | try dbg_info_buffer.resize(index + 4); |
| 343 | try self.addTypeRelocGlobal(atom, field, @intCast(u32, index)); | |
| 350 | try self.addTypeRelocGlobal(atom_index, field, @intCast(u32, index)); | |
| 344 | 351 | // DW.AT.data_member_location, DW.FORM.sdata |
| 345 | 352 | const field_off = ty.structFieldOffset(field_index, target); |
| 346 | 353 | try leb128.writeULEB128(dbg_info_buffer.writer(), field_off); |
| ... | ... | @@ -372,7 +379,7 @@ pub const DeclState = struct { |
| 372 | 379 | // DW.AT.type, DW.FORM.ref4 |
| 373 | 380 | var index = dbg_info_buffer.items.len; |
| 374 | 381 | try dbg_info_buffer.resize(index + 4); |
| 375 | try self.addTypeRelocGlobal(atom, field.ty, @intCast(u32, index)); | |
| 382 | try self.addTypeRelocGlobal(atom_index, field.ty, @intCast(u32, index)); | |
| 376 | 383 | // DW.AT.data_member_location, DW.FORM.sdata |
| 377 | 384 | const field_off = ty.structFieldOffset(field_index, target); |
| 378 | 385 | try leb128.writeULEB128(dbg_info_buffer.writer(), field_off); |
| ... | ... | @@ -455,7 +462,7 @@ pub const DeclState = struct { |
| 455 | 462 | // DW.AT.type, DW.FORM.ref4 |
| 456 | 463 | const inner_union_index = dbg_info_buffer.items.len; |
| 457 | 464 | try dbg_info_buffer.resize(inner_union_index + 4); |
| 458 | try self.addTypeRelocLocal(atom, @intCast(u32, inner_union_index), 5); | |
| 465 | try self.addTypeRelocLocal(atom_index, @intCast(u32, inner_union_index), 5); | |
| 459 | 466 | // DW.AT.data_member_location, DW.FORM.sdata |
| 460 | 467 | try leb128.writeULEB128(dbg_info_buffer.writer(), payload_offset); |
| 461 | 468 | } |
| ... | ... | @@ -482,7 +489,7 @@ pub const DeclState = struct { |
| 482 | 489 | // DW.AT.type, DW.FORM.ref4 |
| 483 | 490 | const index = dbg_info_buffer.items.len; |
| 484 | 491 | try dbg_info_buffer.resize(index + 4); |
| 485 | try self.addTypeRelocGlobal(atom, field.ty, @intCast(u32, index)); | |
| 492 | try self.addTypeRelocGlobal(atom_index, field.ty, @intCast(u32, index)); | |
| 486 | 493 | // DW.AT.data_member_location, DW.FORM.sdata |
| 487 | 494 | try dbg_info_buffer.append(0); |
| 488 | 495 | } |
| ... | ... | @@ -499,7 +506,7 @@ pub const DeclState = struct { |
| 499 | 506 | // DW.AT.type, DW.FORM.ref4 |
| 500 | 507 | const index = dbg_info_buffer.items.len; |
| 501 | 508 | try dbg_info_buffer.resize(index + 4); |
| 502 | try self.addTypeRelocGlobal(atom, union_obj.tag_ty, @intCast(u32, index)); | |
| 509 | try self.addTypeRelocGlobal(atom_index, union_obj.tag_ty, @intCast(u32, index)); | |
| 503 | 510 | // DW.AT.data_member_location, DW.FORM.sdata |
| 504 | 511 | try leb128.writeULEB128(dbg_info_buffer.writer(), tag_offset); |
| 505 | 512 | |
| ... | ... | @@ -542,7 +549,7 @@ pub const DeclState = struct { |
| 542 | 549 | // DW.AT.type, DW.FORM.ref4 |
| 543 | 550 | var index = dbg_info_buffer.items.len; |
| 544 | 551 | try dbg_info_buffer.resize(index + 4); |
| 545 | try self.addTypeRelocGlobal(atom, payload_ty, @intCast(u32, index)); | |
| 552 | try self.addTypeRelocGlobal(atom_index, payload_ty, @intCast(u32, index)); | |
| 546 | 553 | // DW.AT.data_member_location, DW.FORM.sdata |
| 547 | 554 | try leb128.writeULEB128(dbg_info_buffer.writer(), payload_off); |
| 548 | 555 | |
| ... | ... | @@ -555,7 +562,7 @@ pub const DeclState = struct { |
| 555 | 562 | // DW.AT.type, DW.FORM.ref4 |
| 556 | 563 | index = dbg_info_buffer.items.len; |
| 557 | 564 | try dbg_info_buffer.resize(index + 4); |
| 558 | try self.addTypeRelocGlobal(atom, error_ty, @intCast(u32, index)); | |
| 565 | try self.addTypeRelocGlobal(atom_index, error_ty, @intCast(u32, index)); | |
| 559 | 566 | // DW.AT.data_member_location, DW.FORM.sdata |
| 560 | 567 | try leb128.writeULEB128(dbg_info_buffer.writer(), error_off); |
| 561 | 568 | |
| ... | ... | @@ -588,12 +595,11 @@ pub const DeclState = struct { |
| 588 | 595 | self: *DeclState, |
| 589 | 596 | name: [:0]const u8, |
| 590 | 597 | ty: Type, |
| 591 | tag: File.Tag, | |
| 592 | 598 | owner_decl: Module.Decl.Index, |
| 593 | 599 | loc: DbgInfoLoc, |
| 594 | 600 | ) error{OutOfMemory}!void { |
| 595 | 601 | const dbg_info = &self.dbg_info; |
| 596 | const atom = getDbgInfoAtom(tag, self.mod, owner_decl); | |
| 602 | const atom_index = self.di_atom_decls.get(owner_decl).?; | |
| 597 | 603 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 598 | 604 | |
| 599 | 605 | switch (loc) { |
| ... | ... | @@ -638,7 +644,7 @@ pub const DeclState = struct { |
| 638 | 644 | try dbg_info.ensureUnusedCapacity(5 + name_with_null.len); |
| 639 | 645 | const index = dbg_info.items.len; |
| 640 | 646 | try dbg_info.resize(index + 4); // dw.at.type, dw.form.ref4 |
| 641 | try self.addTypeRelocGlobal(atom, ty, @intCast(u32, index)); // DW.AT.type, DW.FORM.ref4 | |
| 647 | try self.addTypeRelocGlobal(atom_index, ty, @intCast(u32, index)); // DW.AT.type, DW.FORM.ref4 | |
| 642 | 648 | dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string |
| 643 | 649 | } |
| 644 | 650 | |
| ... | ... | @@ -646,13 +652,12 @@ pub const DeclState = struct { |
| 646 | 652 | self: *DeclState, |
| 647 | 653 | name: [:0]const u8, |
| 648 | 654 | ty: Type, |
| 649 | tag: File.Tag, | |
| 650 | 655 | owner_decl: Module.Decl.Index, |
| 651 | 656 | is_ptr: bool, |
| 652 | 657 | loc: DbgInfoLoc, |
| 653 | 658 | ) error{OutOfMemory}!void { |
| 654 | 659 | const dbg_info = &self.dbg_info; |
| 655 | const atom = getDbgInfoAtom(tag, self.mod, owner_decl); | |
| 660 | const atom_index = self.di_atom_decls.get(owner_decl).?; | |
| 656 | 661 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 657 | 662 | try dbg_info.append(@enumToInt(AbbrevKind.variable)); |
| 658 | 663 | const target = self.mod.getTarget(); |
| ... | ... | @@ -782,7 +787,7 @@ pub const DeclState = struct { |
| 782 | 787 | try dbg_info.ensureUnusedCapacity(5 + name_with_null.len); |
| 783 | 788 | const index = dbg_info.items.len; |
| 784 | 789 | try dbg_info.resize(index + 4); // dw.at.type, dw.form.ref4 |
| 785 | try self.addTypeRelocGlobal(atom, child_ty, @intCast(u32, index)); | |
| 790 | try self.addTypeRelocGlobal(atom_index, child_ty, @intCast(u32, index)); | |
| 786 | 791 | dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string |
| 787 | 792 | } |
| 788 | 793 | |
| ... | ... | @@ -815,7 +820,7 @@ pub const DeclState = struct { |
| 815 | 820 | }; |
| 816 | 821 | |
| 817 | 822 | pub const AbbrevEntry = struct { |
| 818 | atom: *const Atom, | |
| 823 | atom_index: Atom.Index, | |
| 819 | 824 | type: Type, |
| 820 | 825 | offset: u32, |
| 821 | 826 | }; |
| ... | ... | @@ -824,7 +829,7 @@ pub const AbbrevRelocation = struct { |
| 824 | 829 | /// If target is null, we deal with a local relocation that is based on simple offset + addend |
| 825 | 830 | /// only. |
| 826 | 831 | target: ?u32, |
| 827 | atom: *const Atom, | |
| 832 | atom_index: Atom.Index, | |
| 828 | 833 | offset: u32, |
| 829 | 834 | addend: u32, |
| 830 | 835 | }; |
| ... | ... | @@ -841,26 +846,6 @@ pub const ExprlocRelocation = struct { |
| 841 | 846 | offset: u32, |
| 842 | 847 | }; |
| 843 | 848 | |
| 844 | pub const SrcFn = struct { | |
| 845 | /// Offset from the beginning of the Debug Line Program header that contains this function. | |
| 846 | off: u32, | |
| 847 | /// Size of the line number program component belonging to this function, not | |
| 848 | /// including padding. | |
| 849 | len: u32, | |
| 850 | ||
| 851 | /// Points to the previous and next neighbors, based on the offset from .debug_line. | |
| 852 | /// This can be used to find, for example, the capacity of this `SrcFn`. | |
| 853 | prev: ?*SrcFn, | |
| 854 | next: ?*SrcFn, | |
| 855 | ||
| 856 | pub const empty: SrcFn = .{ | |
| 857 | .off = 0, | |
| 858 | .len = 0, | |
| 859 | .prev = null, | |
| 860 | .next = null, | |
| 861 | }; | |
| 862 | }; | |
| 863 | ||
| 864 | 849 | pub const PtrWidth = enum { p32, p64 }; |
| 865 | 850 | |
| 866 | 851 | pub const AbbrevKind = enum(u8) { |
| ... | ... | @@ -910,16 +895,18 @@ pub fn init(allocator: Allocator, bin_file: *File, target: std.Target) Dwarf { |
| 910 | 895 | |
| 911 | 896 | pub fn deinit(self: *Dwarf) void { |
| 912 | 897 | const gpa = self.allocator; |
| 913 | self.dbg_line_fn_free_list.deinit(gpa); | |
| 914 | self.atom_free_list.deinit(gpa); | |
| 898 | ||
| 899 | self.src_fn_free_list.deinit(gpa); | |
| 900 | self.src_fns.deinit(gpa); | |
| 901 | self.src_fn_decls.deinit(gpa); | |
| 902 | ||
| 903 | self.di_atom_free_list.deinit(gpa); | |
| 904 | self.di_atoms.deinit(gpa); | |
| 905 | self.di_atom_decls.deinit(gpa); | |
| 906 | ||
| 915 | 907 | self.strtab.deinit(gpa); |
| 916 | 908 | self.di_files.deinit(gpa); |
| 917 | 909 | self.global_abbrev_relocs.deinit(gpa); |
| 918 | ||
| 919 | for (self.managed_atoms.items) |atom| { | |
| 920 | gpa.destroy(atom); | |
| 921 | } | |
| 922 | self.managed_atoms.deinit(gpa); | |
| 923 | 910 | } |
| 924 | 911 | |
| 925 | 912 | /// Initializes Decl's state and its matching output buffers. |
| ... | ... | @@ -935,15 +922,19 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index) |
| 935 | 922 | log.debug("initDeclState {s}{*}", .{ decl_name, decl }); |
| 936 | 923 | |
| 937 | 924 | const gpa = self.allocator; |
| 938 | var decl_state = DeclState.init(gpa, mod); | |
| 925 | var decl_state = DeclState.init(gpa, mod, &self.di_atom_decls); | |
| 939 | 926 | errdefer decl_state.deinit(); |
| 940 | 927 | const dbg_line_buffer = &decl_state.dbg_line; |
| 941 | 928 | const dbg_info_buffer = &decl_state.dbg_info; |
| 942 | 929 | |
| 930 | const di_atom_index = try self.getOrCreateAtomForDecl(.di_atom, decl_index); | |
| 931 | ||
| 943 | 932 | assert(decl.has_tv); |
| 944 | 933 | |
| 945 | 934 | switch (decl.ty.zigTypeTag()) { |
| 946 | 935 | .Fn => { |
| 936 | _ = try self.getOrCreateAtomForDecl(.src_fn, decl_index); | |
| 937 | ||
| 947 | 938 | // For functions we need to add a prologue to the debug line program. |
| 948 | 939 | try dbg_line_buffer.ensureTotalCapacity(26); |
| 949 | 940 | |
| ... | ... | @@ -1003,8 +994,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index) |
| 1003 | 994 | dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4 |
| 1004 | 995 | // |
| 1005 | 996 | if (fn_ret_has_bits) { |
| 1006 | const atom = getDbgInfoAtom(self.bin_file.tag, mod, decl_index); | |
| 1007 | try decl_state.addTypeRelocGlobal(atom, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len)); | |
| 997 | try decl_state.addTypeRelocGlobal(di_atom_index, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len)); | |
| 1008 | 998 | dbg_info_buffer.items.len += 4; // DW.AT.type, DW.FORM.ref4 |
| 1009 | 999 | } |
| 1010 | 1000 | |
| ... | ... | @@ -1076,26 +1066,23 @@ pub fn commitDeclState( |
| 1076 | 1066 | // This logic is nearly identical to the logic below in `updateDeclDebugInfo` for |
| 1077 | 1067 | // `TextBlock` and the .debug_info. If you are editing this logic, you |
| 1078 | 1068 | // probably need to edit that logic too. |
| 1079 | const src_fn = switch (self.bin_file.tag) { | |
| 1080 | .elf => &decl.fn_link.elf, | |
| 1081 | .macho => &decl.fn_link.macho, | |
| 1082 | .wasm => &decl.fn_link.wasm.src_fn, | |
| 1083 | else => unreachable, // TODO | |
| 1084 | }; | |
| 1069 | const src_fn_index = self.src_fn_decls.get(decl_index).?; | |
| 1070 | const src_fn = self.getAtomPtr(.src_fn, src_fn_index); | |
| 1085 | 1071 | src_fn.len = @intCast(u32, dbg_line_buffer.items.len); |
| 1086 | 1072 | |
| 1087 | if (self.dbg_line_fn_last) |last| blk: { | |
| 1088 | if (src_fn == last) break :blk; | |
| 1089 | if (src_fn.next) |next| { | |
| 1073 | if (self.src_fn_last_index) |last_index| blk: { | |
| 1074 | if (src_fn_index == last_index) break :blk; | |
| 1075 | if (src_fn.next_index) |next_index| { | |
| 1076 | const next = self.getAtomPtr(.src_fn, next_index); | |
| 1090 | 1077 | // Update existing function - non-last item. |
| 1091 | 1078 | if (src_fn.off + src_fn.len + min_nop_size > next.off) { |
| 1092 | 1079 | // It grew too big, so we move it to a new location. |
| 1093 | if (src_fn.prev) |prev| { | |
| 1094 | self.dbg_line_fn_free_list.put(gpa, prev, {}) catch {}; | |
| 1095 | prev.next = src_fn.next; | |
| 1080 | if (src_fn.prev_index) |prev_index| { | |
| 1081 | self.src_fn_free_list.put(gpa, prev_index, {}) catch {}; | |
| 1082 | self.getAtomPtr(.src_fn, prev_index).next_index = src_fn.next_index; | |
| 1096 | 1083 | } |
| 1097 | next.prev = src_fn.prev; | |
| 1098 | src_fn.next = null; | |
| 1084 | next.prev_index = src_fn.prev_index; | |
| 1085 | src_fn.next_index = null; | |
| 1099 | 1086 | // Populate where it used to be with NOPs. |
| 1100 | 1087 | switch (self.bin_file.tag) { |
| 1101 | 1088 | .elf => { |
| ... | ... | @@ -1118,33 +1105,42 @@ pub fn commitDeclState( |
| 1118 | 1105 | else => unreachable, |
| 1119 | 1106 | } |
| 1120 | 1107 | // TODO Look at the free list before appending at the end. |
| 1121 | src_fn.prev = last; | |
| 1122 | last.next = src_fn; | |
| 1123 | self.dbg_line_fn_last = src_fn; | |
| 1108 | src_fn.prev_index = last_index; | |
| 1109 | const last = self.getAtomPtr(.src_fn, last_index); | |
| 1110 | last.next_index = src_fn_index; | |
| 1111 | self.src_fn_last_index = src_fn_index; | |
| 1124 | 1112 | |
| 1125 | 1113 | src_fn.off = last.off + padToIdeal(last.len); |
| 1126 | 1114 | } |
| 1127 | } else if (src_fn.prev == null) { | |
| 1115 | } else if (src_fn.prev_index == null) { | |
| 1128 | 1116 | // Append new function. |
| 1129 | 1117 | // TODO Look at the free list before appending at the end. |
| 1130 | src_fn.prev = last; | |
| 1131 | last.next = src_fn; | |
| 1132 | self.dbg_line_fn_last = src_fn; | |
| 1118 | src_fn.prev_index = last_index; | |
| 1119 | const last = self.getAtomPtr(.src_fn, last_index); | |
| 1120 | last.next_index = src_fn_index; | |
| 1121 | self.src_fn_last_index = src_fn_index; | |
| 1133 | 1122 | |
| 1134 | 1123 | src_fn.off = last.off + padToIdeal(last.len); |
| 1135 | 1124 | } |
| 1136 | 1125 | } else { |
| 1137 | 1126 | // This is the first function of the Line Number Program. |
| 1138 | self.dbg_line_fn_first = src_fn; | |
| 1139 | self.dbg_line_fn_last = src_fn; | |
| 1127 | self.src_fn_first_index = src_fn_index; | |
| 1128 | self.src_fn_last_index = src_fn_index; | |
| 1140 | 1129 | |
| 1141 | 1130 | src_fn.off = padToIdeal(self.dbgLineNeededHeaderBytes(&[0][]u8{}, &[0][]u8{})); |
| 1142 | 1131 | } |
| 1143 | 1132 | |
| 1144 | const last_src_fn = self.dbg_line_fn_last.?; | |
| 1133 | const last_src_fn_index = self.src_fn_last_index.?; | |
| 1134 | const last_src_fn = self.getAtom(.src_fn, last_src_fn_index); | |
| 1145 | 1135 | const needed_size = last_src_fn.off + last_src_fn.len; |
| 1146 | const prev_padding_size: u32 = if (src_fn.prev) |prev| src_fn.off - (prev.off + prev.len) else 0; | |
| 1147 | const next_padding_size: u32 = if (src_fn.next) |next| next.off - (src_fn.off + src_fn.len) else 0; | |
| 1136 | const prev_padding_size: u32 = if (src_fn.prev_index) |prev_index| blk: { | |
| 1137 | const prev = self.getAtom(.src_fn, prev_index); | |
| 1138 | break :blk src_fn.off - (prev.off + prev.len); | |
| 1139 | } else 0; | |
| 1140 | const next_padding_size: u32 = if (src_fn.next_index) |next_index| blk: { | |
| 1141 | const next = self.getAtom(.src_fn, next_index); | |
| 1142 | break :blk next.off - (src_fn.off + src_fn.len); | |
| 1143 | } else 0; | |
| 1148 | 1144 | |
| 1149 | 1145 | // We only have support for one compilation unit so far, so the offsets are directly |
| 1150 | 1146 | // from the .debug_line section. |
| ... | ... | @@ -1213,7 +1209,7 @@ pub fn commitDeclState( |
| 1213 | 1209 | if (dbg_info_buffer.items.len == 0) |
| 1214 | 1210 | return; |
| 1215 | 1211 | |
| 1216 | const atom = getDbgInfoAtom(self.bin_file.tag, module, decl_index); | |
| 1212 | const di_atom_index = self.di_atom_decls.get(decl_index).?; | |
| 1217 | 1213 | if (decl_state.abbrev_table.items.len > 0) { |
| 1218 | 1214 | // Now we emit the .debug_info types of the Decl. These will count towards the size of |
| 1219 | 1215 | // the buffer, so we have to do it before computing the offset, and we can't perform the actual |
| ... | ... | @@ -1235,12 +1231,12 @@ pub fn commitDeclState( |
| 1235 | 1231 | if (deferred) continue; |
| 1236 | 1232 | |
| 1237 | 1233 | symbol.offset = @intCast(u32, dbg_info_buffer.items.len); |
| 1238 | try decl_state.addDbgInfoType(module, atom, ty); | |
| 1234 | try decl_state.addDbgInfoType(module, di_atom_index, ty); | |
| 1239 | 1235 | } |
| 1240 | 1236 | } |
| 1241 | 1237 | |
| 1242 | 1238 | log.debug("updateDeclDebugInfoAllocation for '{s}'", .{decl.name}); |
| 1243 | try self.updateDeclDebugInfoAllocation(atom, @intCast(u32, dbg_info_buffer.items.len)); | |
| 1239 | try self.updateDeclDebugInfoAllocation(di_atom_index, @intCast(u32, dbg_info_buffer.items.len)); | |
| 1244 | 1240 | |
| 1245 | 1241 | while (decl_state.abbrev_relocs.popOrNull()) |reloc| { |
| 1246 | 1242 | if (reloc.target) |target| { |
| ... | ... | @@ -1261,11 +1257,12 @@ pub fn commitDeclState( |
| 1261 | 1257 | try self.global_abbrev_relocs.append(gpa, .{ |
| 1262 | 1258 | .target = null, |
| 1263 | 1259 | .offset = reloc.offset, |
| 1264 | .atom = reloc.atom, | |
| 1260 | .atom_index = reloc.atom_index, | |
| 1265 | 1261 | .addend = reloc.addend, |
| 1266 | 1262 | }); |
| 1267 | 1263 | } else { |
| 1268 | const value = symbol.atom.off + symbol.offset + reloc.addend; | |
| 1264 | const atom = self.getAtom(.di_atom, symbol.atom_index); | |
| 1265 | const value = atom.off + symbol.offset + reloc.addend; | |
| 1269 | 1266 | log.debug("{x}: [() => {x}] (%{d}, '{}')", .{ reloc.offset, value, target, ty.fmtDebug() }); |
| 1270 | 1267 | mem.writeInt( |
| 1271 | 1268 | u32, |
| ... | ... | @@ -1275,10 +1272,11 @@ pub fn commitDeclState( |
| 1275 | 1272 | ); |
| 1276 | 1273 | } |
| 1277 | 1274 | } else { |
| 1275 | const atom = self.getAtom(.di_atom, reloc.atom_index); | |
| 1278 | 1276 | mem.writeInt( |
| 1279 | 1277 | u32, |
| 1280 | 1278 | dbg_info_buffer.items[reloc.offset..][0..@sizeOf(u32)], |
| 1281 | reloc.atom.off + reloc.offset + reloc.addend, | |
| 1279 | atom.off + reloc.offset + reloc.addend, | |
| 1282 | 1280 | target_endian, |
| 1283 | 1281 | ); |
| 1284 | 1282 | } |
| ... | ... | @@ -1294,7 +1292,7 @@ pub fn commitDeclState( |
| 1294 | 1292 | .got_load => .got_load, |
| 1295 | 1293 | }, |
| 1296 | 1294 | .target = reloc.target, |
| 1297 | .offset = reloc.offset + atom.off, | |
| 1295 | .offset = reloc.offset + self.getAtom(.di_atom, di_atom_index).off, | |
| 1298 | 1296 | .addend = 0, |
| 1299 | 1297 | .prev_vaddr = 0, |
| 1300 | 1298 | }); |
| ... | ... | @@ -1304,10 +1302,10 @@ pub fn commitDeclState( |
| 1304 | 1302 | } |
| 1305 | 1303 | |
| 1306 | 1304 | log.debug("writeDeclDebugInfo for '{s}", .{decl.name}); |
| 1307 | try self.writeDeclDebugInfo(atom, dbg_info_buffer.items); | |
| 1305 | try self.writeDeclDebugInfo(di_atom_index, dbg_info_buffer.items); | |
| 1308 | 1306 | } |
| 1309 | 1307 | |
| 1310 | fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void { | |
| 1308 | fn updateDeclDebugInfoAllocation(self: *Dwarf, atom_index: Atom.Index, len: u32) !void { | |
| 1311 | 1309 | const tracy = trace(@src()); |
| 1312 | 1310 | defer tracy.end(); |
| 1313 | 1311 | |
| ... | ... | @@ -1316,19 +1314,21 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void { |
| 1316 | 1314 | // probably need to edit that logic too. |
| 1317 | 1315 | const gpa = self.allocator; |
| 1318 | 1316 | |
| 1317 | const atom = self.getAtomPtr(.di_atom, atom_index); | |
| 1319 | 1318 | atom.len = len; |
| 1320 | if (self.atom_last) |last| blk: { | |
| 1321 | if (atom == last) break :blk; | |
| 1322 | if (atom.next) |next| { | |
| 1319 | if (self.di_atom_last_index) |last_index| blk: { | |
| 1320 | if (atom_index == last_index) break :blk; | |
| 1321 | if (atom.next_index) |next_index| { | |
| 1322 | const next = self.getAtomPtr(.di_atom, next_index); | |
| 1323 | 1323 | // Update existing Decl - non-last item. |
| 1324 | 1324 | if (atom.off + atom.len + min_nop_size > next.off) { |
| 1325 | 1325 | // It grew too big, so we move it to a new location. |
| 1326 | if (atom.prev) |prev| { | |
| 1327 | self.atom_free_list.put(gpa, prev, {}) catch {}; | |
| 1328 | prev.next = atom.next; | |
| 1326 | if (atom.prev_index) |prev_index| { | |
| 1327 | self.di_atom_free_list.put(gpa, prev_index, {}) catch {}; | |
| 1328 | self.getAtomPtr(.di_atom, prev_index).next_index = atom.next_index; | |
| 1329 | 1329 | } |
| 1330 | next.prev = atom.prev; | |
| 1331 | atom.next = null; | |
| 1330 | next.prev_index = atom.prev_index; | |
| 1331 | atom.next_index = null; | |
| 1332 | 1332 | // Populate where it used to be with NOPs. |
| 1333 | 1333 | switch (self.bin_file.tag) { |
| 1334 | 1334 | .elf => { |
| ... | ... | @@ -1351,31 +1351,33 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void { |
| 1351 | 1351 | else => unreachable, |
| 1352 | 1352 | } |
| 1353 | 1353 | // TODO Look at the free list before appending at the end. |
| 1354 | atom.prev = last; | |
| 1355 | last.next = atom; | |
| 1356 | self.atom_last = atom; | |
| 1354 | atom.prev_index = last_index; | |
| 1355 | const last = self.getAtomPtr(.di_atom, last_index); | |
| 1356 | last.next_index = atom_index; | |
| 1357 | self.di_atom_last_index = atom_index; | |
| 1357 | 1358 | |
| 1358 | 1359 | atom.off = last.off + padToIdeal(last.len); |
| 1359 | 1360 | } |
| 1360 | } else if (atom.prev == null) { | |
| 1361 | } else if (atom.prev_index == null) { | |
| 1361 | 1362 | // Append new Decl. |
| 1362 | 1363 | // TODO Look at the free list before appending at the end. |
| 1363 | atom.prev = last; | |
| 1364 | last.next = atom; | |
| 1365 | self.atom_last = atom; | |
| 1364 | atom.prev_index = last_index; | |
| 1365 | const last = self.getAtomPtr(.di_atom, last_index); | |
| 1366 | last.next_index = atom_index; | |
| 1367 | self.di_atom_last_index = atom_index; | |
| 1366 | 1368 | |
| 1367 | 1369 | atom.off = last.off + padToIdeal(last.len); |
| 1368 | 1370 | } |
| 1369 | 1371 | } else { |
| 1370 | 1372 | // This is the first Decl of the .debug_info |
| 1371 | self.atom_first = atom; | |
| 1372 | self.atom_last = atom; | |
| 1373 | self.di_atom_first_index = atom_index; | |
| 1374 | self.di_atom_last_index = atom_index; | |
| 1373 | 1375 | |
| 1374 | 1376 | atom.off = @intCast(u32, padToIdeal(self.dbgInfoHeaderBytes())); |
| 1375 | 1377 | } |
| 1376 | 1378 | } |
| 1377 | 1379 | |
| 1378 | fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void { | |
| 1380 | fn writeDeclDebugInfo(self: *Dwarf, atom_index: Atom.Index, dbg_info_buf: []const u8) !void { | |
| 1379 | 1381 | const tracy = trace(@src()); |
| 1380 | 1382 | defer tracy.end(); |
| 1381 | 1383 | |
| ... | ... | @@ -1384,14 +1386,22 @@ fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void |
| 1384 | 1386 | // probably need to edit that logic too. |
| 1385 | 1387 | const gpa = self.allocator; |
| 1386 | 1388 | |
| 1387 | const last_decl = self.atom_last.?; | |
| 1389 | const atom = self.getAtom(.di_atom, atom_index); | |
| 1390 | const last_decl_index = self.di_atom_last_index.?; | |
| 1391 | const last_decl = self.getAtom(.di_atom, last_decl_index); | |
| 1388 | 1392 | // +1 for a trailing zero to end the children of the decl tag. |
| 1389 | 1393 | const needed_size = last_decl.off + last_decl.len + 1; |
| 1390 | const prev_padding_size: u32 = if (atom.prev) |prev| atom.off - (prev.off + prev.len) else 0; | |
| 1391 | const next_padding_size: u32 = if (atom.next) |next| next.off - (atom.off + atom.len) else 0; | |
| 1394 | const prev_padding_size: u32 = if (atom.prev_index) |prev_index| blk: { | |
| 1395 | const prev = self.getAtom(.di_atom, prev_index); | |
| 1396 | break :blk atom.off - (prev.off + prev.len); | |
| 1397 | } else 0; | |
| 1398 | const next_padding_size: u32 = if (atom.next_index) |next_index| blk: { | |
| 1399 | const next = self.getAtom(.di_atom, next_index); | |
| 1400 | break :blk next.off - (atom.off + atom.len); | |
| 1401 | } else 0; | |
| 1392 | 1402 | |
| 1393 | 1403 | // To end the children of the decl tag. |
| 1394 | const trailing_zero = atom.next == null; | |
| 1404 | const trailing_zero = atom.next_index == null; | |
| 1395 | 1405 | |
| 1396 | 1406 | // We only have support for one compilation unit so far, so the offsets are directly |
| 1397 | 1407 | // from the .debug_info section. |
| ... | ... | @@ -1459,10 +1469,15 @@ fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void |
| 1459 | 1469 | } |
| 1460 | 1470 | } |
| 1461 | 1471 | |
| 1462 | pub fn updateDeclLineNumber(self: *Dwarf, decl: *const Module.Decl) !void { | |
| 1472 | pub fn updateDeclLineNumber(self: *Dwarf, module: *Module, decl_index: Module.Decl.Index) !void { | |
| 1463 | 1473 | const tracy = trace(@src()); |
| 1464 | 1474 | defer tracy.end(); |
| 1465 | 1475 | |
| 1476 | const atom_index = try self.getOrCreateAtomForDecl(.src_fn, decl_index); | |
| 1477 | const atom = self.getAtom(.src_fn, atom_index); | |
| 1478 | if (atom.len == 0) return; | |
| 1479 | ||
| 1480 | const decl = module.declPtr(decl_index); | |
| 1466 | 1481 | const func = decl.val.castTag(.function).?.data; |
| 1467 | 1482 | log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{ |
| 1468 | 1483 | decl.src_line, |
| ... | ... | @@ -1477,78 +1492,80 @@ pub fn updateDeclLineNumber(self: *Dwarf, decl: *const Module.Decl) !void { |
| 1477 | 1492 | .elf => { |
| 1478 | 1493 | const elf_file = self.bin_file.cast(File.Elf).?; |
| 1479 | 1494 | const shdr = elf_file.sections.items(.shdr)[elf_file.debug_line_section_index.?]; |
| 1480 | const file_pos = shdr.sh_offset + decl.fn_link.elf.off + self.getRelocDbgLineOff(); | |
| 1495 | const file_pos = shdr.sh_offset + atom.off + self.getRelocDbgLineOff(); | |
| 1481 | 1496 | try elf_file.base.file.?.pwriteAll(&data, file_pos); |
| 1482 | 1497 | }, |
| 1483 | 1498 | .macho => { |
| 1484 | 1499 | const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?; |
| 1485 | 1500 | const sect = d_sym.getSection(d_sym.debug_line_section_index.?); |
| 1486 | const file_pos = sect.offset + decl.fn_link.macho.off + self.getRelocDbgLineOff(); | |
| 1501 | const file_pos = sect.offset + atom.off + self.getRelocDbgLineOff(); | |
| 1487 | 1502 | try d_sym.file.pwriteAll(&data, file_pos); |
| 1488 | 1503 | }, |
| 1489 | 1504 | .wasm => { |
| 1490 | 1505 | const wasm_file = self.bin_file.cast(File.Wasm).?; |
| 1491 | const offset = decl.fn_link.wasm.src_fn.off + self.getRelocDbgLineOff(); | |
| 1492 | const atom = wasm_file.debug_line_atom.?; | |
| 1493 | mem.copy(u8, atom.code.items[offset..], &data); | |
| 1506 | const offset = atom.off + self.getRelocDbgLineOff(); | |
| 1507 | const atom_ = wasm_file.debug_line_atom.?; | |
| 1508 | mem.copy(u8, atom_.code.items[offset..], &data); | |
| 1494 | 1509 | }, |
| 1495 | 1510 | else => unreachable, |
| 1496 | 1511 | } |
| 1497 | 1512 | } |
| 1498 | 1513 | |
| 1499 | pub fn freeAtom(self: *Dwarf, atom: *Atom) void { | |
| 1500 | if (self.atom_first == atom) { | |
| 1501 | self.atom_first = atom.next; | |
| 1502 | } | |
| 1503 | if (self.atom_last == atom) { | |
| 1504 | // TODO shrink the .debug_info section size here | |
| 1505 | self.atom_last = atom.prev; | |
| 1506 | } | |
| 1507 | ||
| 1508 | if (atom.prev) |prev| { | |
| 1509 | prev.next = atom.next; | |
| 1514 | pub fn freeDecl(self: *Dwarf, decl_index: Module.Decl.Index) void { | |
| 1515 | const gpa = self.allocator; | |
| 1510 | 1516 | |
| 1511 | // TODO the free list logic like we do for text blocks above | |
| 1512 | } else { | |
| 1513 | atom.prev = null; | |
| 1517 | // Free SrcFn atom | |
| 1518 | if (self.src_fn_decls.fetchRemove(decl_index)) |kv| { | |
| 1519 | const src_fn_index = kv.value; | |
| 1520 | const src_fn = self.getAtom(.src_fn, src_fn_index); | |
| 1521 | _ = self.src_fn_free_list.remove(src_fn_index); | |
| 1522 | ||
| 1523 | if (src_fn.prev_index) |prev_index| { | |
| 1524 | self.src_fn_free_list.put(gpa, prev_index, {}) catch {}; | |
| 1525 | const prev = self.getAtomPtr(.src_fn, prev_index); | |
| 1526 | prev.next_index = src_fn.next_index; | |
| 1527 | if (src_fn.next_index) |next_index| { | |
| 1528 | self.getAtomPtr(.src_fn, next_index).prev_index = prev_index; | |
| 1529 | } else { | |
| 1530 | self.src_fn_last_index = prev_index; | |
| 1531 | } | |
| 1532 | } else if (src_fn.next_index) |next_index| { | |
| 1533 | self.src_fn_first_index = next_index; | |
| 1534 | self.getAtomPtr(.src_fn, next_index).prev_index = null; | |
| 1535 | } | |
| 1536 | if (self.src_fn_first_index == src_fn_index) { | |
| 1537 | self.src_fn_first_index = src_fn.next_index; | |
| 1538 | } | |
| 1539 | if (self.src_fn_last_index == src_fn_index) { | |
| 1540 | self.src_fn_last_index = src_fn.prev_index; | |
| 1541 | } | |
| 1514 | 1542 | } |
| 1515 | 1543 | |
| 1516 | if (atom.next) |next| { | |
| 1517 | next.prev = atom.prev; | |
| 1518 | } else { | |
| 1519 | atom.next = null; | |
| 1520 | } | |
| 1521 | } | |
| 1544 | // Free DI atom | |
| 1545 | if (self.di_atom_decls.fetchRemove(decl_index)) |kv| { | |
| 1546 | const di_atom_index = kv.value; | |
| 1547 | const di_atom = self.getAtomPtr(.di_atom, di_atom_index); | |
| 1522 | 1548 | |
| 1523 | pub fn freeDecl(self: *Dwarf, decl: *Module.Decl) void { | |
| 1524 | // TODO make this logic match freeTextBlock. Maybe abstract the logic out since the same thing | |
| 1525 | // is desired for both. | |
| 1526 | const gpa = self.allocator; | |
| 1527 | const fn_link = switch (self.bin_file.tag) { | |
| 1528 | .elf => &decl.fn_link.elf, | |
| 1529 | .macho => &decl.fn_link.macho, | |
| 1530 | .wasm => &decl.fn_link.wasm.src_fn, | |
| 1531 | else => unreachable, | |
| 1532 | }; | |
| 1533 | _ = self.dbg_line_fn_free_list.remove(fn_link); | |
| 1549 | if (self.di_atom_first_index == di_atom_index) { | |
| 1550 | self.di_atom_first_index = di_atom.next_index; | |
| 1551 | } | |
| 1552 | if (self.di_atom_last_index == di_atom_index) { | |
| 1553 | // TODO shrink the .debug_info section size here | |
| 1554 | self.di_atom_last_index = di_atom.prev_index; | |
| 1555 | } | |
| 1534 | 1556 | |
| 1535 | if (fn_link.prev) |prev| { | |
| 1536 | self.dbg_line_fn_free_list.put(gpa, prev, {}) catch {}; | |
| 1537 | prev.next = fn_link.next; | |
| 1538 | if (fn_link.next) |next| { | |
| 1539 | next.prev = prev; | |
| 1557 | if (di_atom.prev_index) |prev_index| { | |
| 1558 | self.getAtomPtr(.di_atom, prev_index).next_index = di_atom.next_index; | |
| 1559 | // TODO the free list logic like we do for SrcFn above | |
| 1540 | 1560 | } else { |
| 1541 | self.dbg_line_fn_last = prev; | |
| 1561 | di_atom.prev_index = null; | |
| 1562 | } | |
| 1563 | ||
| 1564 | if (di_atom.next_index) |next_index| { | |
| 1565 | self.getAtomPtr(.di_atom, next_index).prev_index = di_atom.prev_index; | |
| 1566 | } else { | |
| 1567 | di_atom.next_index = null; | |
| 1542 | 1568 | } |
| 1543 | } else if (fn_link.next) |next| { | |
| 1544 | self.dbg_line_fn_first = next; | |
| 1545 | next.prev = null; | |
| 1546 | } | |
| 1547 | if (self.dbg_line_fn_first == fn_link) { | |
| 1548 | self.dbg_line_fn_first = fn_link.next; | |
| 1549 | } | |
| 1550 | if (self.dbg_line_fn_last == fn_link) { | |
| 1551 | self.dbg_line_fn_last = fn_link.prev; | |
| 1552 | 1569 | } |
| 1553 | 1570 | } |
| 1554 | 1571 | |
| ... | ... | @@ -2276,10 +2293,14 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2276 | 2293 | const needed_with_padding = padToIdeal(needed_bytes); |
| 2277 | 2294 | const delta = needed_with_padding - dbg_line_prg_off; |
| 2278 | 2295 | |
| 2279 | var src_fn = self.dbg_line_fn_first.?; | |
| 2280 | const last_fn = self.dbg_line_fn_last.?; | |
| 2296 | const first_fn_index = self.src_fn_first_index.?; | |
| 2297 | const first_fn = self.getAtom(.src_fn, first_fn_index); | |
| 2298 | const last_fn_index = self.src_fn_last_index.?; | |
| 2299 | const last_fn = self.getAtom(.src_fn, last_fn_index); | |
| 2300 | ||
| 2301 | var src_fn_index = first_fn_index; | |
| 2281 | 2302 | |
| 2282 | var buffer = try gpa.alloc(u8, last_fn.off + last_fn.len - src_fn.off); | |
| 2303 | var buffer = try gpa.alloc(u8, last_fn.off + last_fn.len - first_fn.off); | |
| 2283 | 2304 | defer gpa.free(buffer); |
| 2284 | 2305 | |
| 2285 | 2306 | switch (self.bin_file.tag) { |
| ... | ... | @@ -2288,7 +2309,7 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2288 | 2309 | const shdr_index = elf_file.debug_line_section_index.?; |
| 2289 | 2310 | const needed_size = elf_file.sections.items(.shdr)[shdr_index].sh_size + delta; |
| 2290 | 2311 | try elf_file.growNonAllocSection(shdr_index, needed_size, 1, true); |
| 2291 | const file_pos = elf_file.sections.items(.shdr)[shdr_index].sh_offset + src_fn.off; | |
| 2312 | const file_pos = elf_file.sections.items(.shdr)[shdr_index].sh_offset + first_fn.off; | |
| 2292 | 2313 | |
| 2293 | 2314 | const amt = try elf_file.base.file.?.preadAll(buffer, file_pos); |
| 2294 | 2315 | if (amt != buffer.len) return error.InputOutput; |
| ... | ... | @@ -2300,7 +2321,7 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2300 | 2321 | const sect_index = d_sym.debug_line_section_index.?; |
| 2301 | 2322 | const needed_size = @intCast(u32, d_sym.getSection(sect_index).size + delta); |
| 2302 | 2323 | try d_sym.growSection(sect_index, needed_size, true); |
| 2303 | const file_pos = d_sym.getSection(sect_index).offset + src_fn.off; | |
| 2324 | const file_pos = d_sym.getSection(sect_index).offset + first_fn.off; | |
| 2304 | 2325 | |
| 2305 | 2326 | const amt = try d_sym.file.preadAll(buffer, file_pos); |
| 2306 | 2327 | if (amt != buffer.len) return error.InputOutput; |
| ... | ... | @@ -2310,18 +2331,19 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2310 | 2331 | .wasm => { |
| 2311 | 2332 | const wasm_file = self.bin_file.cast(File.Wasm).?; |
| 2312 | 2333 | const debug_line = &wasm_file.debug_line_atom.?.code; |
| 2313 | mem.copy(u8, buffer, debug_line.items[src_fn.off..]); | |
| 2334 | mem.copy(u8, buffer, debug_line.items[first_fn.off..]); | |
| 2314 | 2335 | try debug_line.resize(self.allocator, debug_line.items.len + delta); |
| 2315 | mem.copy(u8, debug_line.items[src_fn.off + delta ..], buffer); | |
| 2336 | mem.copy(u8, debug_line.items[first_fn.off + delta ..], buffer); | |
| 2316 | 2337 | }, |
| 2317 | 2338 | else => unreachable, |
| 2318 | 2339 | } |
| 2319 | 2340 | |
| 2320 | 2341 | while (true) { |
| 2342 | const src_fn = self.getAtomPtr(.src_fn, src_fn_index); | |
| 2321 | 2343 | src_fn.off += delta; |
| 2322 | 2344 | |
| 2323 | if (src_fn.next) |next| { | |
| 2324 | src_fn = next; | |
| 2345 | if (src_fn.next_index) |next_index| { | |
| 2346 | src_fn_index = next_index; | |
| 2325 | 2347 | } else break; |
| 2326 | 2348 | } |
| 2327 | 2349 | } |
| ... | ... | @@ -2367,22 +2389,26 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2367 | 2389 | } |
| 2368 | 2390 | |
| 2369 | 2391 | fn getDebugInfoOff(self: Dwarf) ?u32 { |
| 2370 | const first = self.atom_first orelse return null; | |
| 2392 | const first_index = self.di_atom_first_index orelse return null; | |
| 2393 | const first = self.getAtom(.di_atom, first_index); | |
| 2371 | 2394 | return first.off; |
| 2372 | 2395 | } |
| 2373 | 2396 | |
| 2374 | 2397 | fn getDebugInfoEnd(self: Dwarf) ?u32 { |
| 2375 | const last = self.atom_last orelse return null; | |
| 2398 | const last_index = self.di_atom_last_index orelse return null; | |
| 2399 | const last = self.getAtom(.di_atom, last_index); | |
| 2376 | 2400 | return last.off + last.len; |
| 2377 | 2401 | } |
| 2378 | 2402 | |
| 2379 | 2403 | fn getDebugLineProgramOff(self: Dwarf) ?u32 { |
| 2380 | const first = self.dbg_line_fn_first orelse return null; | |
| 2404 | const first_index = self.src_fn_first_index orelse return null; | |
| 2405 | const first = self.getAtom(.src_fn, first_index); | |
| 2381 | 2406 | return first.off; |
| 2382 | 2407 | } |
| 2383 | 2408 | |
| 2384 | 2409 | fn getDebugLineProgramEnd(self: Dwarf) ?u32 { |
| 2385 | const last = self.dbg_line_fn_last orelse return null; | |
| 2410 | const last_index = self.src_fn_last_index orelse return null; | |
| 2411 | const last = self.getAtom(.src_fn, last_index); | |
| 2386 | 2412 | return last.off + last.len; |
| 2387 | 2413 | } |
| 2388 | 2414 | |
| ... | ... | @@ -2457,23 +2483,14 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2457 | 2483 | } |
| 2458 | 2484 | error_set.names = names; |
| 2459 | 2485 | |
| 2460 | const atom = try gpa.create(Atom); | |
| 2461 | errdefer gpa.destroy(atom); | |
| 2462 | atom.* = .{ | |
| 2463 | .prev = null, | |
| 2464 | .next = null, | |
| 2465 | .off = 0, | |
| 2466 | .len = 0, | |
| 2467 | }; | |
| 2468 | ||
| 2469 | 2486 | var dbg_info_buffer = std.ArrayList(u8).init(arena); |
| 2470 | 2487 | try addDbgInfoErrorSet(arena, module, error_ty, self.target, &dbg_info_buffer); |
| 2471 | 2488 | |
| 2472 | try self.managed_atoms.append(gpa, atom); | |
| 2489 | const di_atom_index = try self.createAtom(.di_atom); | |
| 2473 | 2490 | log.debug("updateDeclDebugInfoAllocation in flushModule", .{}); |
| 2474 | try self.updateDeclDebugInfoAllocation(atom, @intCast(u32, dbg_info_buffer.items.len)); | |
| 2491 | try self.updateDeclDebugInfoAllocation(di_atom_index, @intCast(u32, dbg_info_buffer.items.len)); | |
| 2475 | 2492 | log.debug("writeDeclDebugInfo in flushModule", .{}); |
| 2476 | try self.writeDeclDebugInfo(atom, dbg_info_buffer.items); | |
| 2493 | try self.writeDeclDebugInfo(di_atom_index, dbg_info_buffer.items); | |
| 2477 | 2494 | |
| 2478 | 2495 | const file_pos = blk: { |
| 2479 | 2496 | switch (self.bin_file.tag) { |
| ... | ... | @@ -2494,22 +2511,23 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2494 | 2511 | }; |
| 2495 | 2512 | |
| 2496 | 2513 | var buf: [@sizeOf(u32)]u8 = undefined; |
| 2497 | mem.writeInt(u32, &buf, atom.off, self.target.cpu.arch.endian()); | |
| 2514 | mem.writeInt(u32, &buf, self.getAtom(.di_atom, di_atom_index).off, self.target.cpu.arch.endian()); | |
| 2498 | 2515 | |
| 2499 | 2516 | while (self.global_abbrev_relocs.popOrNull()) |reloc| { |
| 2517 | const atom = self.getAtom(.di_atom, reloc.atom_index); | |
| 2500 | 2518 | switch (self.bin_file.tag) { |
| 2501 | 2519 | .elf => { |
| 2502 | 2520 | const elf_file = self.bin_file.cast(File.Elf).?; |
| 2503 | try elf_file.base.file.?.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset); | |
| 2521 | try elf_file.base.file.?.pwriteAll(&buf, file_pos + atom.off + reloc.offset); | |
| 2504 | 2522 | }, |
| 2505 | 2523 | .macho => { |
| 2506 | 2524 | const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?; |
| 2507 | try d_sym.file.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset); | |
| 2525 | try d_sym.file.pwriteAll(&buf, file_pos + atom.off + reloc.offset); | |
| 2508 | 2526 | }, |
| 2509 | 2527 | .wasm => { |
| 2510 | 2528 | const wasm_file = self.bin_file.cast(File.Wasm).?; |
| 2511 | 2529 | const debug_info = wasm_file.debug_info_atom.?.code; |
| 2512 | mem.copy(u8, debug_info.items[reloc.atom.off + reloc.offset ..], &buf); | |
| 2530 | mem.copy(u8, debug_info.items[atom.off + reloc.offset ..], &buf); | |
| 2513 | 2531 | }, |
| 2514 | 2532 | else => unreachable, |
| 2515 | 2533 | } |
| ... | ... | @@ -2627,12 +2645,62 @@ fn addDbgInfoErrorSet( |
| 2627 | 2645 | try dbg_info_buffer.append(0); |
| 2628 | 2646 | } |
| 2629 | 2647 | |
| 2630 | fn getDbgInfoAtom(tag: File.Tag, mod: *Module, decl_index: Module.Decl.Index) *Atom { | |
| 2631 | const decl = mod.declPtr(decl_index); | |
| 2632 | return switch (tag) { | |
| 2633 | .elf => unreachable, | |
| 2634 | .macho => unreachable, | |
| 2635 | .wasm => &decl.link.wasm.dbg_info_atom, | |
| 2636 | else => unreachable, | |
| 2648 | const Kind = enum { src_fn, di_atom }; | |
| 2649 | ||
| 2650 | fn createAtom(self: *Dwarf, comptime kind: Kind) !Atom.Index { | |
| 2651 | const index = blk: { | |
| 2652 | switch (kind) { | |
| 2653 | .src_fn => { | |
| 2654 | const index = @intCast(Atom.Index, self.src_fns.items.len); | |
| 2655 | _ = try self.src_fns.addOne(self.allocator); | |
| 2656 | break :blk index; | |
| 2657 | }, | |
| 2658 | .di_atom => { | |
| 2659 | const index = @intCast(Atom.Index, self.di_atoms.items.len); | |
| 2660 | _ = try self.di_atoms.addOne(self.allocator); | |
| 2661 | break :blk index; | |
| 2662 | }, | |
| 2663 | } | |
| 2664 | }; | |
| 2665 | const atom = self.getAtomPtr(kind, index); | |
| 2666 | atom.* = .{ | |
| 2667 | .off = 0, | |
| 2668 | .len = 0, | |
| 2669 | .prev_index = null, | |
| 2670 | .next_index = null, | |
| 2671 | }; | |
| 2672 | return index; | |
| 2673 | } | |
| 2674 | ||
| 2675 | fn getOrCreateAtomForDecl(self: *Dwarf, comptime kind: Kind, decl_index: Module.Decl.Index) !Atom.Index { | |
| 2676 | switch (kind) { | |
| 2677 | .src_fn => { | |
| 2678 | const gop = try self.src_fn_decls.getOrPut(self.allocator, decl_index); | |
| 2679 | if (!gop.found_existing) { | |
| 2680 | gop.value_ptr.* = try self.createAtom(kind); | |
| 2681 | } | |
| 2682 | return gop.value_ptr.*; | |
| 2683 | }, | |
| 2684 | .di_atom => { | |
| 2685 | const gop = try self.di_atom_decls.getOrPut(self.allocator, decl_index); | |
| 2686 | if (!gop.found_existing) { | |
| 2687 | gop.value_ptr.* = try self.createAtom(kind); | |
| 2688 | } | |
| 2689 | return gop.value_ptr.*; | |
| 2690 | }, | |
| 2691 | } | |
| 2692 | } | |
| 2693 | ||
| 2694 | fn getAtom(self: *const Dwarf, comptime kind: Kind, index: Atom.Index) Atom { | |
| 2695 | return switch (kind) { | |
| 2696 | .src_fn => self.src_fns.items[index], | |
| 2697 | .di_atom => self.di_atoms.items[index], | |
| 2698 | }; | |
| 2699 | } | |
| 2700 | ||
| 2701 | fn getAtomPtr(self: *Dwarf, comptime kind: Kind, index: Atom.Index) *Atom { | |
| 2702 | return switch (kind) { | |
| 2703 | .src_fn => &self.src_fns.items[index], | |
| 2704 | .di_atom => &self.di_atoms.items[index], | |
| 2637 | 2705 | }; |
| 2638 | 2706 | } |
src/link/Elf.zig+267-275| ... | ... | @@ -344,9 +344,9 @@ pub fn deinit(self: *Elf) void { |
| 344 | 344 | self.relocs.deinit(gpa); |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | // if (self.dwarf) |*dw| { | |
| 348 | // dw.deinit(); | |
| 349 | // } | |
| 347 | if (self.dwarf) |*dw| { | |
| 348 | dw.deinit(); | |
| 349 | } | |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File.RelocInfo) !u64 { |
| ... | ... | @@ -685,146 +685,146 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 685 | 685 | try self.writeSymbol(0); |
| 686 | 686 | } |
| 687 | 687 | |
| 688 | // if (self.dwarf) |*dw| { | |
| 689 | // if (self.debug_str_section_index == null) { | |
| 690 | // self.debug_str_section_index = @intCast(u16, self.sections.slice().len); | |
| 691 | // assert(dw.strtab.buffer.items.len == 0); | |
| 692 | // try dw.strtab.buffer.append(gpa, 0); | |
| 693 | // try self.sections.append(gpa, .{ | |
| 694 | // .shdr = .{ | |
| 695 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), | |
| 696 | // .sh_type = elf.SHT_PROGBITS, | |
| 697 | // .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, | |
| 698 | // .sh_addr = 0, | |
| 699 | // .sh_offset = 0, | |
| 700 | // .sh_size = 0, | |
| 701 | // .sh_link = 0, | |
| 702 | // .sh_info = 0, | |
| 703 | // .sh_addralign = 1, | |
| 704 | // .sh_entsize = 1, | |
| 705 | // }, | |
| 706 | // .phdr_index = undefined, | |
| 707 | // }); | |
| 708 | // self.debug_strtab_dirty = true; | |
| 709 | // self.shdr_table_dirty = true; | |
| 710 | // } | |
| 711 | ||
| 712 | // if (self.debug_info_section_index == null) { | |
| 713 | // self.debug_info_section_index = @intCast(u16, self.sections.slice().len); | |
| 714 | ||
| 715 | // const file_size_hint = 200; | |
| 716 | // const p_align = 1; | |
| 717 | // const off = self.findFreeSpace(file_size_hint, p_align); | |
| 718 | // log.debug("found .debug_info free space 0x{x} to 0x{x}", .{ | |
| 719 | // off, | |
| 720 | // off + file_size_hint, | |
| 721 | // }); | |
| 722 | // try self.sections.append(gpa, .{ | |
| 723 | // .shdr = .{ | |
| 724 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), | |
| 725 | // .sh_type = elf.SHT_PROGBITS, | |
| 726 | // .sh_flags = 0, | |
| 727 | // .sh_addr = 0, | |
| 728 | // .sh_offset = off, | |
| 729 | // .sh_size = file_size_hint, | |
| 730 | // .sh_link = 0, | |
| 731 | // .sh_info = 0, | |
| 732 | // .sh_addralign = p_align, | |
| 733 | // .sh_entsize = 0, | |
| 734 | // }, | |
| 735 | // .phdr_index = undefined, | |
| 736 | // }); | |
| 737 | // self.shdr_table_dirty = true; | |
| 738 | // self.debug_info_header_dirty = true; | |
| 739 | // } | |
| 740 | ||
| 741 | // if (self.debug_abbrev_section_index == null) { | |
| 742 | // self.debug_abbrev_section_index = @intCast(u16, self.sections.slice().len); | |
| 743 | ||
| 744 | // const file_size_hint = 128; | |
| 745 | // const p_align = 1; | |
| 746 | // const off = self.findFreeSpace(file_size_hint, p_align); | |
| 747 | // log.debug("found .debug_abbrev free space 0x{x} to 0x{x}", .{ | |
| 748 | // off, | |
| 749 | // off + file_size_hint, | |
| 750 | // }); | |
| 751 | // try self.sections.append(gpa, .{ | |
| 752 | // .shdr = .{ | |
| 753 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), | |
| 754 | // .sh_type = elf.SHT_PROGBITS, | |
| 755 | // .sh_flags = 0, | |
| 756 | // .sh_addr = 0, | |
| 757 | // .sh_offset = off, | |
| 758 | // .sh_size = file_size_hint, | |
| 759 | // .sh_link = 0, | |
| 760 | // .sh_info = 0, | |
| 761 | // .sh_addralign = p_align, | |
| 762 | // .sh_entsize = 0, | |
| 763 | // }, | |
| 764 | // .phdr_index = undefined, | |
| 765 | // }); | |
| 766 | // self.shdr_table_dirty = true; | |
| 767 | // self.debug_abbrev_section_dirty = true; | |
| 768 | // } | |
| 769 | ||
| 770 | // if (self.debug_aranges_section_index == null) { | |
| 771 | // self.debug_aranges_section_index = @intCast(u16, self.sections.slice().len); | |
| 772 | ||
| 773 | // const file_size_hint = 160; | |
| 774 | // const p_align = 16; | |
| 775 | // const off = self.findFreeSpace(file_size_hint, p_align); | |
| 776 | // log.debug("found .debug_aranges free space 0x{x} to 0x{x}", .{ | |
| 777 | // off, | |
| 778 | // off + file_size_hint, | |
| 779 | // }); | |
| 780 | // try self.sections.append(gpa, .{ | |
| 781 | // .shdr = .{ | |
| 782 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), | |
| 783 | // .sh_type = elf.SHT_PROGBITS, | |
| 784 | // .sh_flags = 0, | |
| 785 | // .sh_addr = 0, | |
| 786 | // .sh_offset = off, | |
| 787 | // .sh_size = file_size_hint, | |
| 788 | // .sh_link = 0, | |
| 789 | // .sh_info = 0, | |
| 790 | // .sh_addralign = p_align, | |
| 791 | // .sh_entsize = 0, | |
| 792 | // }, | |
| 793 | // .phdr_index = undefined, | |
| 794 | // }); | |
| 795 | // self.shdr_table_dirty = true; | |
| 796 | // self.debug_aranges_section_dirty = true; | |
| 797 | // } | |
| 798 | ||
| 799 | // if (self.debug_line_section_index == null) { | |
| 800 | // self.debug_line_section_index = @intCast(u16, self.sections.slice().len); | |
| 801 | ||
| 802 | // const file_size_hint = 250; | |
| 803 | // const p_align = 1; | |
| 804 | // const off = self.findFreeSpace(file_size_hint, p_align); | |
| 805 | // log.debug("found .debug_line free space 0x{x} to 0x{x}", .{ | |
| 806 | // off, | |
| 807 | // off + file_size_hint, | |
| 808 | // }); | |
| 809 | // try self.sections.append(gpa, .{ | |
| 810 | // .shdr = .{ | |
| 811 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), | |
| 812 | // .sh_type = elf.SHT_PROGBITS, | |
| 813 | // .sh_flags = 0, | |
| 814 | // .sh_addr = 0, | |
| 815 | // .sh_offset = off, | |
| 816 | // .sh_size = file_size_hint, | |
| 817 | // .sh_link = 0, | |
| 818 | // .sh_info = 0, | |
| 819 | // .sh_addralign = p_align, | |
| 820 | // .sh_entsize = 0, | |
| 821 | // }, | |
| 822 | // .phdr_index = undefined, | |
| 823 | // }); | |
| 824 | // self.shdr_table_dirty = true; | |
| 825 | // self.debug_line_header_dirty = true; | |
| 826 | // } | |
| 827 | // } | |
| 688 | if (self.dwarf) |*dw| { | |
| 689 | if (self.debug_str_section_index == null) { | |
| 690 | self.debug_str_section_index = @intCast(u16, self.sections.slice().len); | |
| 691 | assert(dw.strtab.buffer.items.len == 0); | |
| 692 | try dw.strtab.buffer.append(gpa, 0); | |
| 693 | try self.sections.append(gpa, .{ | |
| 694 | .shdr = .{ | |
| 695 | .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), | |
| 696 | .sh_type = elf.SHT_PROGBITS, | |
| 697 | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, | |
| 698 | .sh_addr = 0, | |
| 699 | .sh_offset = 0, | |
| 700 | .sh_size = 0, | |
| 701 | .sh_link = 0, | |
| 702 | .sh_info = 0, | |
| 703 | .sh_addralign = 1, | |
| 704 | .sh_entsize = 1, | |
| 705 | }, | |
| 706 | .phdr_index = undefined, | |
| 707 | }); | |
| 708 | self.debug_strtab_dirty = true; | |
| 709 | self.shdr_table_dirty = true; | |
| 710 | } | |
| 711 | ||
| 712 | if (self.debug_info_section_index == null) { | |
| 713 | self.debug_info_section_index = @intCast(u16, self.sections.slice().len); | |
| 714 | ||
| 715 | const file_size_hint = 200; | |
| 716 | const p_align = 1; | |
| 717 | const off = self.findFreeSpace(file_size_hint, p_align); | |
| 718 | log.debug("found .debug_info free space 0x{x} to 0x{x}", .{ | |
| 719 | off, | |
| 720 | off + file_size_hint, | |
| 721 | }); | |
| 722 | try self.sections.append(gpa, .{ | |
| 723 | .shdr = .{ | |
| 724 | .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), | |
| 725 | .sh_type = elf.SHT_PROGBITS, | |
| 726 | .sh_flags = 0, | |
| 727 | .sh_addr = 0, | |
| 728 | .sh_offset = off, | |
| 729 | .sh_size = file_size_hint, | |
| 730 | .sh_link = 0, | |
| 731 | .sh_info = 0, | |
| 732 | .sh_addralign = p_align, | |
| 733 | .sh_entsize = 0, | |
| 734 | }, | |
| 735 | .phdr_index = undefined, | |
| 736 | }); | |
| 737 | self.shdr_table_dirty = true; | |
| 738 | self.debug_info_header_dirty = true; | |
| 739 | } | |
| 740 | ||
| 741 | if (self.debug_abbrev_section_index == null) { | |
| 742 | self.debug_abbrev_section_index = @intCast(u16, self.sections.slice().len); | |
| 743 | ||
| 744 | const file_size_hint = 128; | |
| 745 | const p_align = 1; | |
| 746 | const off = self.findFreeSpace(file_size_hint, p_align); | |
| 747 | log.debug("found .debug_abbrev free space 0x{x} to 0x{x}", .{ | |
| 748 | off, | |
| 749 | off + file_size_hint, | |
| 750 | }); | |
| 751 | try self.sections.append(gpa, .{ | |
| 752 | .shdr = .{ | |
| 753 | .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), | |
| 754 | .sh_type = elf.SHT_PROGBITS, | |
| 755 | .sh_flags = 0, | |
| 756 | .sh_addr = 0, | |
| 757 | .sh_offset = off, | |
| 758 | .sh_size = file_size_hint, | |
| 759 | .sh_link = 0, | |
| 760 | .sh_info = 0, | |
| 761 | .sh_addralign = p_align, | |
| 762 | .sh_entsize = 0, | |
| 763 | }, | |
| 764 | .phdr_index = undefined, | |
| 765 | }); | |
| 766 | self.shdr_table_dirty = true; | |
| 767 | self.debug_abbrev_section_dirty = true; | |
| 768 | } | |
| 769 | ||
| 770 | if (self.debug_aranges_section_index == null) { | |
| 771 | self.debug_aranges_section_index = @intCast(u16, self.sections.slice().len); | |
| 772 | ||
| 773 | const file_size_hint = 160; | |
| 774 | const p_align = 16; | |
| 775 | const off = self.findFreeSpace(file_size_hint, p_align); | |
| 776 | log.debug("found .debug_aranges free space 0x{x} to 0x{x}", .{ | |
| 777 | off, | |
| 778 | off + file_size_hint, | |
| 779 | }); | |
| 780 | try self.sections.append(gpa, .{ | |
| 781 | .shdr = .{ | |
| 782 | .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), | |
| 783 | .sh_type = elf.SHT_PROGBITS, | |
| 784 | .sh_flags = 0, | |
| 785 | .sh_addr = 0, | |
| 786 | .sh_offset = off, | |
| 787 | .sh_size = file_size_hint, | |
| 788 | .sh_link = 0, | |
| 789 | .sh_info = 0, | |
| 790 | .sh_addralign = p_align, | |
| 791 | .sh_entsize = 0, | |
| 792 | }, | |
| 793 | .phdr_index = undefined, | |
| 794 | }); | |
| 795 | self.shdr_table_dirty = true; | |
| 796 | self.debug_aranges_section_dirty = true; | |
| 797 | } | |
| 798 | ||
| 799 | if (self.debug_line_section_index == null) { | |
| 800 | self.debug_line_section_index = @intCast(u16, self.sections.slice().len); | |
| 801 | ||
| 802 | const file_size_hint = 250; | |
| 803 | const p_align = 1; | |
| 804 | const off = self.findFreeSpace(file_size_hint, p_align); | |
| 805 | log.debug("found .debug_line free space 0x{x} to 0x{x}", .{ | |
| 806 | off, | |
| 807 | off + file_size_hint, | |
| 808 | }); | |
| 809 | try self.sections.append(gpa, .{ | |
| 810 | .shdr = .{ | |
| 811 | .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), | |
| 812 | .sh_type = elf.SHT_PROGBITS, | |
| 813 | .sh_flags = 0, | |
| 814 | .sh_addr = 0, | |
| 815 | .sh_offset = off, | |
| 816 | .sh_size = file_size_hint, | |
| 817 | .sh_link = 0, | |
| 818 | .sh_info = 0, | |
| 819 | .sh_addralign = p_align, | |
| 820 | .sh_entsize = 0, | |
| 821 | }, | |
| 822 | .phdr_index = undefined, | |
| 823 | }); | |
| 824 | self.shdr_table_dirty = true; | |
| 825 | self.debug_line_header_dirty = true; | |
| 826 | } | |
| 827 | } | |
| 828 | 828 | |
| 829 | 829 | const shsize: u64 = switch (self.ptr_width) { |
| 830 | 830 | .p32 => @sizeOf(elf.Elf32_Shdr), |
| ... | ... | @@ -956,26 +956,25 @@ pub fn growNonAllocSection( |
| 956 | 956 | } |
| 957 | 957 | |
| 958 | 958 | pub fn markDirty(self: *Elf, shdr_index: u16, phdr_index: ?u16) void { |
| 959 | _ = shdr_index; | |
| 960 | 959 | self.shdr_table_dirty = true; // TODO look into only writing one section |
| 961 | 960 | |
| 962 | 961 | if (phdr_index) |_| { |
| 963 | 962 | self.phdr_table_dirty = true; // TODO look into making only the one program header dirty |
| 964 | 963 | } |
| 965 | 964 | |
| 966 | // if (self.dwarf) |_| { | |
| 967 | // if (self.debug_info_section_index.? == shdr_index) { | |
| 968 | // self.debug_info_header_dirty = true; | |
| 969 | // } else if (self.debug_line_section_index.? == shdr_index) { | |
| 970 | // self.debug_line_header_dirty = true; | |
| 971 | // } else if (self.debug_abbrev_section_index.? == shdr_index) { | |
| 972 | // self.debug_abbrev_section_dirty = true; | |
| 973 | // } else if (self.debug_str_section_index.? == shdr_index) { | |
| 974 | // self.debug_strtab_dirty = true; | |
| 975 | // } else if (self.debug_aranges_section_index.? == shdr_index) { | |
| 976 | // self.debug_aranges_section_dirty = true; | |
| 977 | // } | |
| 978 | // } | |
| 965 | if (self.dwarf) |_| { | |
| 966 | if (self.debug_info_section_index.? == shdr_index) { | |
| 967 | self.debug_info_header_dirty = true; | |
| 968 | } else if (self.debug_line_section_index.? == shdr_index) { | |
| 969 | self.debug_line_header_dirty = true; | |
| 970 | } else if (self.debug_abbrev_section_index.? == shdr_index) { | |
| 971 | self.debug_abbrev_section_dirty = true; | |
| 972 | } else if (self.debug_str_section_index.? == shdr_index) { | |
| 973 | self.debug_strtab_dirty = true; | |
| 974 | } else if (self.debug_aranges_section_index.? == shdr_index) { | |
| 975 | self.debug_aranges_section_dirty = true; | |
| 976 | } | |
| 977 | } | |
| 979 | 978 | } |
| 980 | 979 | |
| 981 | 980 | pub fn flush(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) link.File.FlushError!void { |
| ... | ... | @@ -1015,14 +1014,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1015 | 1014 | // TODO This linker code currently assumes there is only 1 compilation unit and it |
| 1016 | 1015 | // corresponds to the Zig source code. |
| 1017 | 1016 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 1018 | _ = module; | |
| 1019 | 1017 | |
| 1020 | 1018 | const target_endian = self.base.options.target.cpu.arch.endian(); |
| 1021 | 1019 | const foreign_endian = target_endian != builtin.cpu.arch.endian(); |
| 1022 | 1020 | |
| 1023 | // if (self.dwarf) |*dw| { | |
| 1024 | // try dw.flushModule(module); | |
| 1025 | // } | |
| 1021 | if (self.dwarf) |*dw| { | |
| 1022 | try dw.flushModule(module); | |
| 1023 | } | |
| 1026 | 1024 | |
| 1027 | 1025 | { |
| 1028 | 1026 | var it = self.relocs.iterator(); |
| ... | ... | @@ -1068,43 +1066,43 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1068 | 1066 | self.logSymtab(); |
| 1069 | 1067 | } |
| 1070 | 1068 | |
| 1071 | // if (self.dwarf) |*dw| { | |
| 1072 | // if (self.debug_abbrev_section_dirty) { | |
| 1073 | // try dw.writeDbgAbbrev(); | |
| 1074 | // if (!self.shdr_table_dirty) { | |
| 1075 | // // Then it won't get written with the others and we need to do it. | |
| 1076 | // try self.writeSectHeader(self.debug_abbrev_section_index.?); | |
| 1077 | // } | |
| 1078 | // self.debug_abbrev_section_dirty = false; | |
| 1079 | // } | |
| 1080 | ||
| 1081 | // if (self.debug_info_header_dirty) { | |
| 1082 | // // Currently only one compilation unit is supported, so the address range is simply | |
| 1083 | // // identical to the main program header virtual address and memory size. | |
| 1084 | // const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; | |
| 1085 | // const low_pc = text_phdr.p_vaddr; | |
| 1086 | // const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; | |
| 1087 | // try dw.writeDbgInfoHeader(module, low_pc, high_pc); | |
| 1088 | // self.debug_info_header_dirty = false; | |
| 1089 | // } | |
| 1090 | ||
| 1091 | // if (self.debug_aranges_section_dirty) { | |
| 1092 | // // Currently only one compilation unit is supported, so the address range is simply | |
| 1093 | // // identical to the main program header virtual address and memory size. | |
| 1094 | // const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; | |
| 1095 | // try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); | |
| 1096 | // if (!self.shdr_table_dirty) { | |
| 1097 | // // Then it won't get written with the others and we need to do it. | |
| 1098 | // try self.writeSectHeader(self.debug_aranges_section_index.?); | |
| 1099 | // } | |
| 1100 | // self.debug_aranges_section_dirty = false; | |
| 1101 | // } | |
| 1102 | ||
| 1103 | // if (self.debug_line_header_dirty) { | |
| 1104 | // try dw.writeDbgLineHeader(); | |
| 1105 | // self.debug_line_header_dirty = false; | |
| 1106 | // } | |
| 1107 | // } | |
| 1069 | if (self.dwarf) |*dw| { | |
| 1070 | if (self.debug_abbrev_section_dirty) { | |
| 1071 | try dw.writeDbgAbbrev(); | |
| 1072 | if (!self.shdr_table_dirty) { | |
| 1073 | // Then it won't get written with the others and we need to do it. | |
| 1074 | try self.writeSectHeader(self.debug_abbrev_section_index.?); | |
| 1075 | } | |
| 1076 | self.debug_abbrev_section_dirty = false; | |
| 1077 | } | |
| 1078 | ||
| 1079 | if (self.debug_info_header_dirty) { | |
| 1080 | // Currently only one compilation unit is supported, so the address range is simply | |
| 1081 | // identical to the main program header virtual address and memory size. | |
| 1082 | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; | |
| 1083 | const low_pc = text_phdr.p_vaddr; | |
| 1084 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; | |
| 1085 | try dw.writeDbgInfoHeader(module, low_pc, high_pc); | |
| 1086 | self.debug_info_header_dirty = false; | |
| 1087 | } | |
| 1088 | ||
| 1089 | if (self.debug_aranges_section_dirty) { | |
| 1090 | // Currently only one compilation unit is supported, so the address range is simply | |
| 1091 | // identical to the main program header virtual address and memory size. | |
| 1092 | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; | |
| 1093 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); | |
| 1094 | if (!self.shdr_table_dirty) { | |
| 1095 | // Then it won't get written with the others and we need to do it. | |
| 1096 | try self.writeSectHeader(self.debug_aranges_section_index.?); | |
| 1097 | } | |
| 1098 | self.debug_aranges_section_dirty = false; | |
| 1099 | } | |
| 1100 | ||
| 1101 | if (self.debug_line_header_dirty) { | |
| 1102 | try dw.writeDbgLineHeader(); | |
| 1103 | self.debug_line_header_dirty = false; | |
| 1104 | } | |
| 1105 | } | |
| 1108 | 1106 | |
| 1109 | 1107 | if (self.phdr_table_dirty) { |
| 1110 | 1108 | const phsize: u64 = switch (self.ptr_width) { |
| ... | ... | @@ -1162,15 +1160,15 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1162 | 1160 | } |
| 1163 | 1161 | } |
| 1164 | 1162 | |
| 1165 | // if (self.dwarf) |dwarf| { | |
| 1166 | // const shdr_index = self.debug_str_section_index.?; | |
| 1167 | // if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { | |
| 1168 | // try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false); | |
| 1169 | // const debug_strtab_sect = self.sections.items(.shdr)[shdr_index]; | |
| 1170 | // try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset); | |
| 1171 | // self.debug_strtab_dirty = false; | |
| 1172 | // } | |
| 1173 | // } | |
| 1163 | if (self.dwarf) |dwarf| { | |
| 1164 | const shdr_index = self.debug_str_section_index.?; | |
| 1165 | if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { | |
| 1166 | try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false); | |
| 1167 | const debug_strtab_sect = self.sections.items(.shdr)[shdr_index]; | |
| 1168 | try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset); | |
| 1169 | self.debug_strtab_dirty = false; | |
| 1170 | } | |
| 1171 | } | |
| 1174 | 1172 | |
| 1175 | 1173 | if (self.shdr_table_dirty) { |
| 1176 | 1174 | const shsize: u64 = switch (self.ptr_width) { |
| ... | ... | @@ -2100,10 +2098,6 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2100 | 2098 | self.getAtomPtr(atom_index).local_sym_index = 0; |
| 2101 | 2099 | |
| 2102 | 2100 | self.offset_table_free_list.append(self.base.allocator, atom.offset_table_index) catch {}; |
| 2103 | ||
| 2104 | // if (self.dwarf) |*dw| { | |
| 2105 | // dw.freeAtom(&atom.dbg_info_atom); | |
| 2106 | // } | |
| 2107 | 2101 | } |
| 2108 | 2102 | |
| 2109 | 2103 | fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void { |
| ... | ... | @@ -2133,7 +2127,6 @@ pub fn createAtom(self: *Elf) !Atom.Index { |
| 2133 | 2127 | .offset_table_index = offset_table_index, |
| 2134 | 2128 | .prev_index = null, |
| 2135 | 2129 | .next_index = null, |
| 2136 | .dbg_info_atom = undefined, | |
| 2137 | 2130 | }; |
| 2138 | 2131 | log.debug("creating ATOM(%{d}) at index {d}", .{ local_sym_index, atom_index }); |
| 2139 | 2132 | return atom_index; |
| ... | ... | @@ -2219,16 +2212,16 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme |
| 2219 | 2212 | try self.growAllocSection(sym.st_shndx, needed_size); |
| 2220 | 2213 | maybe_last_atom_index.* = atom_index; |
| 2221 | 2214 | |
| 2222 | // if (self.dwarf) |_| { | |
| 2223 | // // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address | |
| 2224 | // // range of the compilation unit. When we expand the text section, this range changes, | |
| 2225 | // // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty. | |
| 2226 | // self.debug_info_header_dirty = true; | |
| 2227 | // // This becomes dirty for the same reason. We could potentially make this more | |
| 2228 | // // fine-grained with the addition of support for more compilation units. It is planned to | |
| 2229 | // // model each package as a different compilation unit. | |
| 2230 | // self.debug_aranges_section_dirty = true; | |
| 2231 | // } | |
| 2215 | if (self.dwarf) |_| { | |
| 2216 | // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address | |
| 2217 | // range of the compilation unit. When we expand the text section, this range changes, | |
| 2218 | // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty. | |
| 2219 | self.debug_info_header_dirty = true; | |
| 2220 | // This becomes dirty for the same reason. We could potentially make this more | |
| 2221 | // fine-grained with the addition of support for more compilation units. It is planned to | |
| 2222 | // model each package as a different compilation unit. | |
| 2223 | self.debug_aranges_section_dirty = true; | |
| 2224 | } | |
| 2232 | 2225 | } |
| 2233 | 2226 | shdr.sh_addralign = math.max(shdr.sh_addralign, alignment); |
| 2234 | 2227 | |
| ... | ... | @@ -2333,9 +2326,9 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2333 | 2326 | kv.value.exports.deinit(self.base.allocator); |
| 2334 | 2327 | } |
| 2335 | 2328 | |
| 2336 | // if (self.dwarf) |*dw| { | |
| 2337 | // dw.freeDecl(decl); | |
| 2338 | // } | |
| 2329 | if (self.dwarf) |*dw| { | |
| 2330 | dw.freeDecl(decl_index); | |
| 2331 | } | |
| 2339 | 2332 | } |
| 2340 | 2333 | |
| 2341 | 2334 | pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index { |
| ... | ... | @@ -2471,15 +2464,15 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven |
| 2471 | 2464 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2472 | 2465 | defer code_buffer.deinit(); |
| 2473 | 2466 | |
| 2474 | // var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; | |
| 2475 | // defer if (decl_state) |*ds| ds.deinit(); | |
| 2467 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; | |
| 2468 | defer if (decl_state) |*ds| ds.deinit(); | |
| 2476 | 2469 | |
| 2477 | // const res = if (decl_state) |*ds| | |
| 2478 | // try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ | |
| 2479 | // .dwarf = ds, | |
| 2480 | // }) | |
| 2481 | // else | |
| 2482 | const res = try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); | |
| 2470 | const res = if (decl_state) |*ds| | |
| 2471 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ | |
| 2472 | .dwarf = ds, | |
| 2473 | }) | |
| 2474 | else | |
| 2475 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); | |
| 2483 | 2476 | |
| 2484 | 2477 | const code = switch (res) { |
| 2485 | 2478 | .ok => code_buffer.items, |
| ... | ... | @@ -2490,16 +2483,15 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven |
| 2490 | 2483 | }, |
| 2491 | 2484 | }; |
| 2492 | 2485 | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_FUNC); |
| 2493 | _ = local_sym; | |
| 2494 | // if (decl_state) |*ds| { | |
| 2495 | // try self.dwarf.?.commitDeclState( | |
| 2496 | // module, | |
| 2497 | // decl_index, | |
| 2498 | // local_sym.st_value, | |
| 2499 | // local_sym.st_size, | |
| 2500 | // ds, | |
| 2501 | // ); | |
| 2502 | // } | |
| 2486 | if (decl_state) |*ds| { | |
| 2487 | try self.dwarf.?.commitDeclState( | |
| 2488 | module, | |
| 2489 | decl_index, | |
| 2490 | local_sym.st_value, | |
| 2491 | local_sym.st_size, | |
| 2492 | ds, | |
| 2493 | ); | |
| 2494 | } | |
| 2503 | 2495 | |
| 2504 | 2496 | // Since we updated the vaddr and the size, each corresponding export |
| 2505 | 2497 | // symbol also needs to be updated. |
| ... | ... | @@ -2536,27 +2528,27 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v |
| 2536 | 2528 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2537 | 2529 | defer code_buffer.deinit(); |
| 2538 | 2530 | |
| 2539 | // var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; | |
| 2540 | // defer if (decl_state) |*ds| ds.deinit(); | |
| 2531 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; | |
| 2532 | defer if (decl_state) |*ds| ds.deinit(); | |
| 2541 | 2533 | |
| 2542 | 2534 | // TODO implement .debug_info for global variables |
| 2543 | 2535 | const decl_val = if (decl.val.castTag(.variable)) |payload| payload.data.init else decl.val; |
| 2544 | // const res = if (decl_state) |*ds| | |
| 2545 | // try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2546 | // .ty = decl.ty, | |
| 2547 | // .val = decl_val, | |
| 2548 | // }, &code_buffer, .{ | |
| 2549 | // .dwarf = ds, | |
| 2550 | // }, .{ | |
| 2551 | // .parent_atom_index = atom.getSymbolIndex().?, | |
| 2552 | // }) | |
| 2553 | // else | |
| 2554 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2555 | .ty = decl.ty, | |
| 2556 | .val = decl_val, | |
| 2557 | }, &code_buffer, .none, .{ | |
| 2558 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2559 | }); | |
| 2536 | const res = if (decl_state) |*ds| | |
| 2537 | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2538 | .ty = decl.ty, | |
| 2539 | .val = decl_val, | |
| 2540 | }, &code_buffer, .{ | |
| 2541 | .dwarf = ds, | |
| 2542 | }, .{ | |
| 2543 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2544 | }) | |
| 2545 | else | |
| 2546 | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2547 | .ty = decl.ty, | |
| 2548 | .val = decl_val, | |
| 2549 | }, &code_buffer, .none, .{ | |
| 2550 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2551 | }); | |
| 2560 | 2552 | |
| 2561 | 2553 | const code = switch (res) { |
| 2562 | 2554 | .ok => code_buffer.items, |
| ... | ... | @@ -2568,16 +2560,15 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v |
| 2568 | 2560 | }; |
| 2569 | 2561 | |
| 2570 | 2562 | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_OBJECT); |
| 2571 | _ = local_sym; | |
| 2572 | // if (decl_state) |*ds| { | |
| 2573 | // try self.dwarf.?.commitDeclState( | |
| 2574 | // module, | |
| 2575 | // decl_index, | |
| 2576 | // local_sym.st_value, | |
| 2577 | // local_sym.st_size, | |
| 2578 | // ds, | |
| 2579 | // ); | |
| 2580 | // } | |
| 2563 | if (decl_state) |*ds| { | |
| 2564 | try self.dwarf.?.commitDeclState( | |
| 2565 | module, | |
| 2566 | decl_index, | |
| 2567 | local_sym.st_value, | |
| 2568 | local_sym.st_size, | |
| 2569 | ds, | |
| 2570 | ); | |
| 2571 | } | |
| 2581 | 2572 | |
| 2582 | 2573 | // Since we updated the vaddr and the size, each corresponding export |
| 2583 | 2574 | // symbol also needs to be updated. |
| ... | ... | @@ -2737,19 +2728,20 @@ pub fn updateDeclExports( |
| 2737 | 2728 | } |
| 2738 | 2729 | |
| 2739 | 2730 | /// Must be called only after a successful call to `updateDecl`. |
| 2740 | pub fn updateDeclLineNumber(self: *Elf, mod: *Module, decl: *const Module.Decl) !void { | |
| 2731 | pub fn updateDeclLineNumber(self: *Elf, mod: *Module, decl_index: Module.Decl.Index) !void { | |
| 2741 | 2732 | const tracy = trace(@src()); |
| 2742 | 2733 | defer tracy.end(); |
| 2743 | 2734 | |
| 2735 | const decl = mod.declPtr(decl_index); | |
| 2744 | 2736 | const decl_name = try decl.getFullyQualifiedName(mod); |
| 2745 | 2737 | defer self.base.allocator.free(decl_name); |
| 2746 | 2738 | |
| 2747 | 2739 | log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl }); |
| 2748 | 2740 | |
| 2749 | 2741 | if (self.llvm_object) |_| return; |
| 2750 | // if (self.dwarf) |*dw| { | |
| 2751 | // try dw.updateDeclLineNumber(decl); | |
| 2752 | // } | |
| 2742 | if (self.dwarf) |*dw| { | |
| 2743 | try dw.updateDeclLineNumber(mod, decl_index); | |
| 2744 | } | |
| 2753 | 2745 | } |
| 2754 | 2746 | |
| 2755 | 2747 | pub fn deleteDeclExport(self: *Elf, decl_index: Module.Decl.Index, name: []const u8) void { |
src/link/Elf/Atom.zig-3| ... | ... | @@ -4,7 +4,6 @@ const std = @import("std"); |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | const elf = std.elf; |
| 6 | 6 | |
| 7 | const Dwarf = @import("../Dwarf.zig"); | |
| 8 | 7 | const Elf = @import("../Elf.zig"); |
| 9 | 8 | |
| 10 | 9 | /// Each decl always gets a local symbol with the fully qualified name. |
| ... | ... | @@ -23,8 +22,6 @@ offset_table_index: u32, |
| 23 | 22 | prev_index: ?Index, |
| 24 | 23 | next_index: ?Index, |
| 25 | 24 | |
| 26 | dbg_info_atom: Dwarf.Atom, | |
| 27 | ||
| 28 | 25 | pub const Index = u32; |
| 29 | 26 | |
| 30 | 27 | pub const Reloc = struct { |
src/link/MachO.zig+59-71| ... | ... | @@ -472,9 +472,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 472 | 472 | |
| 473 | 473 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 474 | 474 | |
| 475 | // if (self.d_sym) |*d_sym| { | |
| 476 | // try d_sym.dwarf.flushModule(module); | |
| 477 | // } | |
| 475 | if (self.d_sym) |*d_sym| { | |
| 476 | try d_sym.dwarf.flushModule(module); | |
| 477 | } | |
| 478 | 478 | |
| 479 | 479 | var libs = std.StringArrayHashMap(link.SystemLib).init(arena); |
| 480 | 480 | try resolveLibSystem( |
| ... | ... | @@ -664,10 +664,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 664 | 664 | try self.writeCodeSignature(comp, csig); // code signing always comes last |
| 665 | 665 | } |
| 666 | 666 | |
| 667 | // if (self.d_sym) |*d_sym| { | |
| 668 | // // Flush debug symbols bundle. | |
| 669 | // try d_sym.flushModule(self); | |
| 670 | // } | |
| 667 | if (self.d_sym) |*d_sym| { | |
| 668 | // Flush debug symbols bundle. | |
| 669 | try d_sym.flushModule(self); | |
| 670 | } | |
| 671 | 671 | |
| 672 | 672 | // if (build_options.enable_link_snapshots) { |
| 673 | 673 | // if (self.base.options.enable_link_snapshots) |
| ... | ... | @@ -1089,7 +1089,6 @@ pub fn createAtom(self: *MachO) !Atom.Index { |
| 1089 | 1089 | .alignment = 0, |
| 1090 | 1090 | .prev_index = null, |
| 1091 | 1091 | .next_index = null, |
| 1092 | .dbg_info_atom = undefined, | |
| 1093 | 1092 | }; |
| 1094 | 1093 | log.debug("creating ATOM(%{d}) at index {d}", .{ sym_index, atom_index }); |
| 1095 | 1094 | return atom_index; |
| ... | ... | @@ -1724,9 +1723,9 @@ pub fn deinit(self: *MachO) void { |
| 1724 | 1723 | if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 1725 | 1724 | } |
| 1726 | 1725 | |
| 1727 | // if (self.d_sym) |*d_sym| { | |
| 1728 | // d_sym.deinit(); | |
| 1729 | // } | |
| 1726 | if (self.d_sym) |*d_sym| { | |
| 1727 | d_sym.deinit(); | |
| 1728 | } | |
| 1730 | 1729 | |
| 1731 | 1730 | self.got_entries.deinit(gpa); |
| 1732 | 1731 | self.got_entries_free_list.deinit(gpa); |
| ... | ... | @@ -1804,9 +1803,8 @@ pub fn deinit(self: *MachO) void { |
| 1804 | 1803 | } |
| 1805 | 1804 | |
| 1806 | 1805 | fn freeAtom(self: *MachO, atom_index: Atom.Index) void { |
| 1807 | log.debug("freeAtom {d}", .{atom_index}); | |
| 1808 | ||
| 1809 | 1806 | const gpa = self.base.allocator; |
| 1807 | log.debug("freeAtom {d}", .{atom_index}); | |
| 1810 | 1808 | |
| 1811 | 1809 | // Remove any relocs and base relocs associated with this Atom |
| 1812 | 1810 | Atom.freeRelocations(self, atom_index); |
| ... | ... | @@ -1876,9 +1874,9 @@ fn freeAtom(self: *MachO, atom_index: Atom.Index) void { |
| 1876 | 1874 | }; |
| 1877 | 1875 | _ = self.got_entries_table.remove(got_target); |
| 1878 | 1876 | |
| 1879 | // if (self.d_sym) |*d_sym| { | |
| 1880 | // d_sym.swapRemoveRelocs(sym_index); | |
| 1881 | // } | |
| 1877 | if (self.d_sym) |*d_sym| { | |
| 1878 | d_sym.swapRemoveRelocs(sym_index); | |
| 1879 | } | |
| 1882 | 1880 | |
| 1883 | 1881 | log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index }); |
| 1884 | 1882 | } |
| ... | ... | @@ -1887,10 +1885,6 @@ fn freeAtom(self: *MachO, atom_index: Atom.Index) void { |
| 1887 | 1885 | _ = self.atom_by_index_table.remove(sym_index); |
| 1888 | 1886 | log.debug(" adding local symbol index {d} to free list", .{sym_index}); |
| 1889 | 1887 | self.getAtomPtr(atom_index).sym_index = 0; |
| 1890 | ||
| 1891 | // if (self.d_sym) |*d_sym| { | |
| 1892 | // d_sym.dwarf.freeAtom(&atom.dbg_info_atom); | |
| 1893 | // } | |
| 1894 | 1888 | } |
| 1895 | 1889 | |
| 1896 | 1890 | fn shrinkAtom(self: *MachO, atom_index: Atom.Index, new_block_size: u64) void { |
| ... | ... | @@ -2020,23 +2014,22 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv |
| 2020 | 2014 | Atom.freeRelocations(self, atom_index); |
| 2021 | 2015 | |
| 2022 | 2016 | const atom = self.getAtom(atom_index); |
| 2023 | _ = atom; | |
| 2024 | 2017 | |
| 2025 | 2018 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2026 | 2019 | defer code_buffer.deinit(); |
| 2027 | 2020 | |
| 2028 | // var decl_state = if (self.d_sym) |*d_sym| | |
| 2029 | // try d_sym.dwarf.initDeclState(module, decl_index) | |
| 2030 | // else | |
| 2031 | // null; | |
| 2032 | // defer if (decl_state) |*ds| ds.deinit(); | |
| 2021 | var decl_state = if (self.d_sym) |*d_sym| | |
| 2022 | try d_sym.dwarf.initDeclState(module, decl_index) | |
| 2023 | else | |
| 2024 | null; | |
| 2025 | defer if (decl_state) |*ds| ds.deinit(); | |
| 2033 | 2026 | |
| 2034 | // const res = if (decl_state) |*ds| | |
| 2035 | // try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ | |
| 2036 | // .dwarf = ds, | |
| 2037 | // }) | |
| 2038 | // else | |
| 2039 | const res = try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); | |
| 2027 | const res = if (decl_state) |*ds| | |
| 2028 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ | |
| 2029 | .dwarf = ds, | |
| 2030 | }) | |
| 2031 | else | |
| 2032 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); | |
| 2040 | 2033 | |
| 2041 | 2034 | const code = switch (res) { |
| 2042 | 2035 | .ok => code_buffer.items, |
| ... | ... | @@ -2048,11 +2041,10 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv |
| 2048 | 2041 | }; |
| 2049 | 2042 | |
| 2050 | 2043 | const addr = try self.updateDeclCode(decl_index, code); |
| 2051 | _ = addr; | |
| 2052 | 2044 | |
| 2053 | // if (decl_state) |*ds| { | |
| 2054 | // try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds); | |
| 2055 | // } | |
| 2045 | if (decl_state) |*ds| { | |
| 2046 | try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds); | |
| 2047 | } | |
| 2056 | 2048 | |
| 2057 | 2049 | // Since we updated the vaddr and the size, each corresponding export symbol also |
| 2058 | 2050 | // needs to be updated. |
| ... | ... | @@ -2154,29 +2146,29 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) |
| 2154 | 2146 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2155 | 2147 | defer code_buffer.deinit(); |
| 2156 | 2148 | |
| 2157 | // var decl_state: ?Dwarf.DeclState = if (self.d_sym) |*d_sym| | |
| 2158 | // try d_sym.dwarf.initDeclState(module, decl_index) | |
| 2159 | // else | |
| 2160 | // null; | |
| 2161 | // defer if (decl_state) |*ds| ds.deinit(); | |
| 2149 | var decl_state: ?Dwarf.DeclState = if (self.d_sym) |*d_sym| | |
| 2150 | try d_sym.dwarf.initDeclState(module, decl_index) | |
| 2151 | else | |
| 2152 | null; | |
| 2153 | defer if (decl_state) |*ds| ds.deinit(); | |
| 2162 | 2154 | |
| 2163 | 2155 | const decl_val = if (decl.val.castTag(.variable)) |payload| payload.data.init else decl.val; |
| 2164 | // const res = if (decl_state) |*ds| | |
| 2165 | // try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2166 | // .ty = decl.ty, | |
| 2167 | // .val = decl_val, | |
| 2168 | // }, &code_buffer, .{ | |
| 2169 | // .dwarf = ds, | |
| 2170 | // }, .{ | |
| 2171 | // .parent_atom_index = atom.getSymbolIndex().?, | |
| 2172 | // }) | |
| 2173 | // else | |
| 2174 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2175 | .ty = decl.ty, | |
| 2176 | .val = decl_val, | |
| 2177 | }, &code_buffer, .none, .{ | |
| 2178 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2179 | }); | |
| 2156 | const res = if (decl_state) |*ds| | |
| 2157 | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2158 | .ty = decl.ty, | |
| 2159 | .val = decl_val, | |
| 2160 | }, &code_buffer, .{ | |
| 2161 | .dwarf = ds, | |
| 2162 | }, .{ | |
| 2163 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2164 | }) | |
| 2165 | else | |
| 2166 | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ | |
| 2167 | .ty = decl.ty, | |
| 2168 | .val = decl_val, | |
| 2169 | }, &code_buffer, .none, .{ | |
| 2170 | .parent_atom_index = atom.getSymbolIndex().?, | |
| 2171 | }); | |
| 2180 | 2172 | |
| 2181 | 2173 | const code = switch (res) { |
| 2182 | 2174 | .ok => code_buffer.items, |
| ... | ... | @@ -2187,11 +2179,10 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) |
| 2187 | 2179 | }, |
| 2188 | 2180 | }; |
| 2189 | 2181 | const addr = try self.updateDeclCode(decl_index, code); |
| 2190 | _ = addr; | |
| 2191 | 2182 | |
| 2192 | // if (decl_state) |*ds| { | |
| 2193 | // try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds); | |
| 2194 | // } | |
| 2183 | if (decl_state) |*ds| { | |
| 2184 | try self.d_sym.?.dwarf.commitDeclState(module, decl_index, addr, atom.size, ds); | |
| 2185 | } | |
| 2195 | 2186 | |
| 2196 | 2187 | // Since we updated the vaddr and the size, each corresponding export symbol also |
| 2197 | 2188 | // needs to be updated. |
| ... | ... | @@ -2432,13 +2423,10 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8) |
| 2432 | 2423 | return atom.getSymbol(self).n_value; |
| 2433 | 2424 | } |
| 2434 | 2425 | |
| 2435 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void { | |
| 2436 | _ = decl; | |
| 2437 | _ = self; | |
| 2438 | _ = module; | |
| 2439 | // if (self.d_sym) |*d_sym| { | |
| 2440 | // try d_sym.dwarf.updateDeclLineNumber(decl); | |
| 2441 | // } | |
| 2426 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl_index: Module.Decl.Index) !void { | |
| 2427 | if (self.d_sym) |*d_sym| { | |
| 2428 | try d_sym.dwarf.updateDeclLineNumber(module, decl_index); | |
| 2429 | } | |
| 2442 | 2430 | } |
| 2443 | 2431 | |
| 2444 | 2432 | pub fn updateDeclExports( |
| ... | ... | @@ -2611,9 +2599,9 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { |
| 2611 | 2599 | kv.value.exports.deinit(self.base.allocator); |
| 2612 | 2600 | } |
| 2613 | 2601 | |
| 2614 | // if (self.d_sym) |*d_sym| { | |
| 2615 | // d_sym.dwarf.freeDecl(decl); | |
| 2616 | // } | |
| 2602 | if (self.d_sym) |*d_sym| { | |
| 2603 | d_sym.dwarf.freeDecl(decl_index); | |
| 2604 | } | |
| 2617 | 2605 | } |
| 2618 | 2606 | |
| 2619 | 2607 | pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: File.RelocInfo) !u64 { |
src/link/MachO/Atom.zig-3| ... | ... | @@ -13,7 +13,6 @@ const trace = @import("../../tracy.zig").trace; |
| 13 | 13 | |
| 14 | 14 | const Allocator = mem.Allocator; |
| 15 | 15 | const Arch = std.Target.Cpu.Arch; |
| 16 | const Dwarf = @import("../Dwarf.zig"); | |
| 17 | 16 | const MachO = @import("../MachO.zig"); |
| 18 | 17 | const Relocation = @import("Relocation.zig"); |
| 19 | 18 | const SymbolWithLoc = MachO.SymbolWithLoc; |
| ... | ... | @@ -43,8 +42,6 @@ alignment: u32, |
| 43 | 42 | next_index: ?Index, |
| 44 | 43 | prev_index: ?Index, |
| 45 | 44 | |
| 46 | dbg_info_atom: Dwarf.Atom, | |
| 47 | ||
| 48 | 45 | pub const Index = u32; |
| 49 | 46 | |
| 50 | 47 | pub const Binding = struct { |
src/link/Plan9.zig+2-2| ... | ... | @@ -1018,10 +1018,10 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | /// Must be called only after a successful call to `updateDecl`. |
| 1021 | pub fn updateDeclLineNumber(self: *Plan9, mod: *Module, decl: *const Module.Decl) !void { | |
| 1021 | pub fn updateDeclLineNumber(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void { | |
| 1022 | 1022 | _ = self; |
| 1023 | 1023 | _ = mod; |
| 1024 | _ = decl; | |
| 1024 | _ = decl_index; | |
| 1025 | 1025 | } |
| 1026 | 1026 | |
| 1027 | 1027 | pub fn getDeclVAddr( |
src/link/Wasm.zig+6-11| ... | ... | @@ -183,13 +183,9 @@ pub const Segment = struct { |
| 183 | 183 | pub const FnData = struct { |
| 184 | 184 | /// Reference to the wasm type that represents this function. |
| 185 | 185 | type_index: u32, |
| 186 | /// Contains debug information related to this function. | |
| 187 | /// For Wasm, the offset is relative to the code-section. | |
| 188 | src_fn: Dwarf.SrcFn, | |
| 189 | 186 | |
| 190 | 187 | pub const empty: FnData = .{ |
| 191 | 188 | .type_index = undefined, |
| 192 | .src_fn = Dwarf.SrcFn.empty, | |
| 193 | 189 | }; |
| 194 | 190 | }; |
| 195 | 191 | |
| ... | ... | @@ -1122,17 +1118,18 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi |
| 1122 | 1118 | return wasm.finishUpdateDecl(decl, code); |
| 1123 | 1119 | } |
| 1124 | 1120 | |
| 1125 | pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Module, decl: *const Module.Decl) !void { | |
| 1121 | pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !void { | |
| 1126 | 1122 | if (wasm.llvm_object) |_| return; |
| 1127 | 1123 | if (wasm.dwarf) |*dw| { |
| 1128 | 1124 | const tracy = trace(@src()); |
| 1129 | 1125 | defer tracy.end(); |
| 1130 | 1126 | |
| 1127 | const decl = mod.declPtr(decl_index); | |
| 1131 | 1128 | const decl_name = try decl.getFullyQualifiedName(mod); |
| 1132 | 1129 | defer wasm.base.allocator.free(decl_name); |
| 1133 | 1130 | |
| 1134 | 1131 | log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl }); |
| 1135 | try dw.updateDeclLineNumber(decl); | |
| 1132 | try dw.updateDeclLineNumber(mod, decl_index); | |
| 1136 | 1133 | } |
| 1137 | 1134 | } |
| 1138 | 1135 | |
| ... | ... | @@ -1460,10 +1457,9 @@ pub fn freeDecl(wasm: *Wasm, decl_index: Module.Decl.Index) void { |
| 1460 | 1457 | _ = wasm.resolved_symbols.swapRemove(atom.symbolLoc()); |
| 1461 | 1458 | _ = wasm.symbol_atom.remove(atom.symbolLoc()); |
| 1462 | 1459 | |
| 1463 | if (wasm.dwarf) |*dwarf| { | |
| 1464 | dwarf.freeDecl(decl); | |
| 1465 | dwarf.freeAtom(&atom.dbg_info_atom); | |
| 1466 | } | |
| 1460 | // if (wasm.dwarf) |*dwarf| { | |
| 1461 | // dwarf.freeDecl(decl_index); | |
| 1462 | // } | |
| 1467 | 1463 | |
| 1468 | 1464 | atom.deinit(wasm.base.allocator); |
| 1469 | 1465 | } |
| ... | ... | @@ -1882,7 +1878,6 @@ fn initializeCallCtorsFunction(wasm: *Wasm) !void { |
| 1882 | 1878 | .next = null, |
| 1883 | 1879 | .prev = null, |
| 1884 | 1880 | .code = function_body.moveToUnmanaged(), |
| 1885 | .dbg_info_atom = undefined, | |
| 1886 | 1881 | }; |
| 1887 | 1882 | try wasm.managed_atoms.append(wasm.base.allocator, atom); |
| 1888 | 1883 | try wasm.appendAtomAtIndex(wasm.code_section_index.?, atom); |
src/link/Wasm/Atom.zig-5| ... | ... | @@ -4,7 +4,6 @@ const std = @import("std"); |
| 4 | 4 | const types = @import("types.zig"); |
| 5 | 5 | const Wasm = @import("../Wasm.zig"); |
| 6 | 6 | const Symbol = @import("Symbol.zig"); |
| 7 | const Dwarf = @import("../Dwarf.zig"); | |
| 8 | 7 | |
| 9 | 8 | const leb = std.leb; |
| 10 | 9 | const log = std.log.scoped(.link); |
| ... | ... | @@ -39,9 +38,6 @@ prev: ?*Atom, |
| 39 | 38 | /// When the parent atom is being freed, it will also do so for all local atoms. |
| 40 | 39 | locals: std.ArrayListUnmanaged(Atom) = .{}, |
| 41 | 40 | |
| 42 | /// Represents the debug Atom that holds all debug information of this Atom. | |
| 43 | dbg_info_atom: Dwarf.Atom, | |
| 44 | ||
| 45 | 41 | /// Represents a default empty wasm `Atom` |
| 46 | 42 | pub const empty: Atom = .{ |
| 47 | 43 | .alignment = 0, |
| ... | ... | @@ -51,7 +47,6 @@ pub const empty: Atom = .{ |
| 51 | 47 | .prev = null, |
| 52 | 48 | .size = 0, |
| 53 | 49 | .sym_index = 0, |
| 54 | .dbg_info_atom = undefined, | |
| 55 | 50 | }; |
| 56 | 51 | |
| 57 | 52 | /// Frees all resources owned by this `Atom`. |