| ... | @@ -5,6 +5,8 @@ output_section_index: u32 = 0, | ... | @@ -5,6 +5,8 @@ output_section_index: u32 = 0, |
| 5 | // atoms: std.ArrayListUnmanaged(Elf.Ref) = .empty, | 5 | // atoms: std.ArrayListUnmanaged(Elf.Ref) = .empty, |
| 6 | atoms: std.AutoArrayHashMapUnmanaged(Elf.Ref, void) = .empty, | 6 | atoms: std.AutoArrayHashMapUnmanaged(Elf.Ref, void) = .empty, |
| 7 | | 7 | |
| | 8 | dirty: bool = true, |
| | 9 | |
| 8 | pub fn deinit(list: *AtomList, allocator: Allocator) void { | 10 | pub fn deinit(list: *AtomList, allocator: Allocator) void { |
| 9 | list.atoms.deinit(allocator); | 11 | list.atoms.deinit(allocator); |
| 10 | } | 12 | } |
| ... | @@ -20,9 +22,7 @@ pub fn offset(list: AtomList, elf_file: *Elf) u64 { | ... | @@ -20,9 +22,7 @@ pub fn offset(list: AtomList, elf_file: *Elf) u64 { |
| 20 | } | 22 | } |
| 21 | | 23 | |
| 22 | pub fn updateSize(list: *AtomList, elf_file: *Elf) void { | 24 | pub fn updateSize(list: *AtomList, elf_file: *Elf) void { |
| 23 | // TODO perhaps a 'stale' flag would be better here? | 25 | assert(list.dirty); |
| 24 | list.size = 0; | | |
| 25 | list.alignment = .@"1"; | | |
| 26 | for (list.atoms.keys()) |ref| { | 26 | for (list.atoms.keys()) |ref| { |
| 27 | const atom_ptr = elf_file.atom(ref).?; | 27 | const atom_ptr = elf_file.atom(ref).?; |
| 28 | assert(atom_ptr.alive); | 28 | assert(atom_ptr.alive); |
| ... | @@ -35,6 +35,8 @@ pub fn updateSize(list: *AtomList, elf_file: *Elf) void { | ... | @@ -35,6 +35,8 @@ pub fn updateSize(list: *AtomList, elf_file: *Elf) void { |
| 35 | } | 35 | } |
| 36 | | 36 | |
| 37 | pub fn allocate(list: *AtomList, elf_file: *Elf) !void { | 37 | pub fn allocate(list: *AtomList, elf_file: *Elf) !void { |
| | 38 | assert(list.dirty); |
| | 39 | |
| 38 | const alloc_res = try elf_file.allocateChunk(.{ | 40 | const alloc_res = try elf_file.allocateChunk(.{ |
| 39 | .shndx = list.output_section_index, | 41 | .shndx = list.output_section_index, |
| 40 | .size = list.size, | 42 | .size = list.size, |
| ... | @@ -43,6 +45,8 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { | ... | @@ -43,6 +45,8 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { |
| 43 | }); | 45 | }); |
| 44 | list.value = @intCast(alloc_res.value); | 46 | list.value = @intCast(alloc_res.value); |
| 45 | | 47 | |
| | 48 | log.debug("allocated atom_list({d}) at 0x{x}", .{ list.output_section_index, list.address(elf_file) }); |
| | 49 | |
| 46 | const slice = elf_file.sections.slice(); | 50 | const slice = elf_file.sections.slice(); |
| 47 | const shdr = &slice.items(.shdr)[list.output_section_index]; | 51 | const shdr = &slice.items(.shdr)[list.output_section_index]; |
| 48 | const last_atom_ref = &slice.items(.last_atom)[list.output_section_index]; | 52 | const last_atom_ref = &slice.items(.last_atom)[list.output_section_index]; |
| ... | @@ -80,12 +84,15 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { | ... | @@ -80,12 +84,15 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { |
| 80 | atom_ptr.output_section_index = list.output_section_index; | 84 | atom_ptr.output_section_index = list.output_section_index; |
| 81 | atom_ptr.value += list.value; | 85 | atom_ptr.value += list.value; |
| 82 | } | 86 | } |
| | 87 | |
| | 88 | list.dirty = false; |
| 83 | } | 89 | } |
| 84 | | 90 | |
| 85 | pub fn write(list: AtomList, buffer: *std.ArrayList(u8), undefs: anytype, elf_file: *Elf) !void { | 91 | pub fn write(list: AtomList, buffer: *std.ArrayList(u8), undefs: anytype, elf_file: *Elf) !void { |
| 86 | const gpa = elf_file.base.comp.gpa; | 92 | const gpa = elf_file.base.comp.gpa; |
| 87 | const osec = elf_file.sections.items(.shdr)[list.output_section_index]; | 93 | const osec = elf_file.sections.items(.shdr)[list.output_section_index]; |
| 88 | assert(osec.sh_type != elf.SHT_NOBITS); | 94 | assert(osec.sh_type != elf.SHT_NOBITS); |
| | 95 | assert(!list.dirty); |
| 89 | | 96 | |
| 90 | log.debug("writing atoms in section '{s}'", .{elf_file.getShString(osec.sh_name)}); | 97 | log.debug("writing atoms in section '{s}'", .{elf_file.getShString(osec.sh_name)}); |
| 91 | | 98 | |