| ... | ... | @@ -130,10 +130,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 130 | 130 | .entsize = 1, |
| 131 | 131 | .type = elf.SHT_PROGBITS, |
| 132 | 132 | .addralign = 1, |
| 133 | .offset = std.math.maxInt(u64), |
| 133 | 134 | }); |
| 134 | 135 | self.debug_str_section_dirty = true; |
| 135 | 136 | self.debug_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_str", .@"1", osec); |
| 136 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_str_index.?).ref; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | if (self.debug_info_index == null) { |
| ... | ... | @@ -141,10 +141,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 141 | 141 | .name = try elf_file.insertShString(".debug_info"), |
| 142 | 142 | .type = elf.SHT_PROGBITS, |
| 143 | 143 | .addralign = 1, |
| 144 | .offset = std.math.maxInt(u64), |
| 144 | 145 | }); |
| 145 | 146 | self.debug_info_section_dirty = true; |
| 146 | 147 | self.debug_info_index = try addSectionSymbolWithAtom(self, gpa, ".debug_info", .@"1", osec); |
| 147 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_info_index.?).ref; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | if (self.debug_abbrev_index == null) { |
| ... | ... | @@ -152,10 +152,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 152 | 152 | .name = try elf_file.insertShString(".debug_abbrev"), |
| 153 | 153 | .type = elf.SHT_PROGBITS, |
| 154 | 154 | .addralign = 1, |
| 155 | .offset = std.math.maxInt(u64), |
| 155 | 156 | }); |
| 156 | 157 | self.debug_abbrev_section_dirty = true; |
| 157 | 158 | self.debug_abbrev_index = try addSectionSymbolWithAtom(self, gpa, ".debug_abbrev", .@"1", osec); |
| 158 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_abbrev_index.?).ref; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | if (self.debug_aranges_index == null) { |
| ... | ... | @@ -163,10 +163,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 163 | 163 | .name = try elf_file.insertShString(".debug_aranges"), |
| 164 | 164 | .type = elf.SHT_PROGBITS, |
| 165 | 165 | .addralign = 16, |
| 166 | .offset = std.math.maxInt(u64), |
| 166 | 167 | }); |
| 167 | 168 | self.debug_aranges_section_dirty = true; |
| 168 | 169 | self.debug_aranges_index = try addSectionSymbolWithAtom(self, gpa, ".debug_aranges", .@"16", osec); |
| 169 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_aranges_index.?).ref; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | if (self.debug_line_index == null) { |
| ... | ... | @@ -174,10 +174,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 174 | 174 | .name = try elf_file.insertShString(".debug_line"), |
| 175 | 175 | .type = elf.SHT_PROGBITS, |
| 176 | 176 | .addralign = 1, |
| 177 | .offset = std.math.maxInt(u64), |
| 177 | 178 | }); |
| 178 | 179 | self.debug_line_section_dirty = true; |
| 179 | 180 | self.debug_line_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line", .@"1", osec); |
| 180 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_index.?).ref; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | if (self.debug_line_str_index == null) { |
| ... | ... | @@ -187,10 +187,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 187 | 187 | .entsize = 1, |
| 188 | 188 | .type = elf.SHT_PROGBITS, |
| 189 | 189 | .addralign = 1, |
| 190 | .offset = std.math.maxInt(u64), |
| 190 | 191 | }); |
| 191 | 192 | self.debug_line_str_section_dirty = true; |
| 192 | 193 | self.debug_line_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line_str", .@"1", osec); |
| 193 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_str_index.?).ref; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | if (self.debug_loclists_index == null) { |
| ... | ... | @@ -198,10 +198,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 198 | 198 | .name = try elf_file.insertShString(".debug_loclists"), |
| 199 | 199 | .type = elf.SHT_PROGBITS, |
| 200 | 200 | .addralign = 1, |
| 201 | .offset = std.math.maxInt(u64), |
| 201 | 202 | }); |
| 202 | 203 | self.debug_loclists_section_dirty = true; |
| 203 | 204 | self.debug_loclists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_loclists", .@"1", osec); |
| 204 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_loclists_index.?).ref; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | if (self.debug_rnglists_index == null) { |
| ... | ... | @@ -209,10 +209,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 209 | 209 | .name = try elf_file.insertShString(".debug_rnglists"), |
| 210 | 210 | .type = elf.SHT_PROGBITS, |
| 211 | 211 | .addralign = 1, |
| 212 | .offset = std.math.maxInt(u64), |
| 212 | 213 | }); |
| 213 | 214 | self.debug_rnglists_section_dirty = true; |
| 214 | 215 | self.debug_rnglists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_rnglists", .@"1", osec); |
| 215 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_rnglists_index.?).ref; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | if (self.eh_frame_index == null) { |
| ... | ... | @@ -224,10 +224,10 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 224 | 224 | elf.SHT_PROGBITS, |
| 225 | 225 | .flags = elf.SHF_ALLOC, |
| 226 | 226 | .addralign = ptr_size, |
| 227 | .offset = std.math.maxInt(u64), |
| 227 | 228 | }); |
| 228 | 229 | self.eh_frame_section_dirty = true; |
| 229 | 230 | self.eh_frame_index = try addSectionSymbolWithAtom(self, gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), osec); |
| 230 | | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.eh_frame_index.?).ref; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | try dwarf.initMetadata(); |
| ... | ... | @@ -1318,7 +1318,7 @@ fn updateNavCode( |
| 1318 | 1318 | const capacity = atom_ptr.capacity(elf_file); |
| 1319 | 1319 | const need_realloc = code.len > capacity or !required_alignment.check(@intCast(atom_ptr.value)); |
| 1320 | 1320 | if (need_realloc) { |
| 1321 | | try self.growAtom(atom_ptr, elf_file); |
| 1321 | try self.allocateAtom(atom_ptr, true, elf_file); |
| 1322 | 1322 | log.debug("growing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), old_vaddr, atom_ptr.value }); |
| 1323 | 1323 | if (old_vaddr != atom_ptr.value) { |
| 1324 | 1324 | sym.value = 0; |
| ... | ... | @@ -1328,7 +1328,7 @@ fn updateNavCode( |
| 1328 | 1328 | // TODO shrink section size |
| 1329 | 1329 | } |
| 1330 | 1330 | } else { |
| 1331 | | try self.allocateAtom(atom_ptr, elf_file); |
| 1331 | try self.allocateAtom(atom_ptr, true, elf_file); |
| 1332 | 1332 | errdefer self.freeNavMetadata(elf_file, sym_index); |
| 1333 | 1333 | sym.value = 0; |
| 1334 | 1334 | esym.st_value = 0; |
| ... | ... | @@ -1403,7 +1403,7 @@ fn updateTlv( |
| 1403 | 1403 | const gop = try self.tls_variables.getOrPut(gpa, atom_ptr.atom_index); |
| 1404 | 1404 | assert(!gop.found_existing); // TODO incremental updates |
| 1405 | 1405 | |
| 1406 | | try self.allocateAtom(atom_ptr, elf_file); |
| 1406 | try self.allocateAtom(atom_ptr, true, elf_file); |
| 1407 | 1407 | sym.value = 0; |
| 1408 | 1408 | esym.st_value = 0; |
| 1409 | 1409 | |
| ... | ... | @@ -1729,7 +1729,7 @@ fn updateLazySymbol( |
| 1729 | 1729 | atom_ptr.size = code.len; |
| 1730 | 1730 | atom_ptr.output_section_index = output_section_index; |
| 1731 | 1731 | |
| 1732 | | try self.allocateAtom(atom_ptr, elf_file); |
| 1732 | try self.allocateAtom(atom_ptr, true, elf_file); |
| 1733 | 1733 | errdefer self.freeNavMetadata(elf_file, symbol_index); |
| 1734 | 1734 | |
| 1735 | 1735 | local_sym.value = 0; |
| ... | ... | @@ -1784,7 +1784,7 @@ fn lowerConst( |
| 1784 | 1784 | atom_ptr.size = code.len; |
| 1785 | 1785 | atom_ptr.output_section_index = output_section_index; |
| 1786 | 1786 | |
| 1787 | | try self.allocateAtom(atom_ptr, elf_file); |
| 1787 | try self.allocateAtom(atom_ptr, true, elf_file); |
| 1788 | 1788 | errdefer self.freeNavMetadata(elf_file, sym_index); |
| 1789 | 1789 | |
| 1790 | 1790 | try elf_file.base.file.?.pwriteAll(code, atom_ptr.offset(elf_file)); |
| ... | ... | @@ -1981,17 +1981,27 @@ fn writeTrampoline(tr_sym: Symbol, target: Symbol, elf_file: *Elf) !void { |
| 1981 | 1981 | } |
| 1982 | 1982 | } |
| 1983 | 1983 | |
| 1984 | | fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 1984 | pub fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, requires_padding: bool, elf_file: *Elf) !void { |
| 1985 | const slice = elf_file.sections.slice(); |
| 1986 | const shdr = &slice.items(.shdr)[atom_ptr.output_section_index]; |
| 1987 | const last_atom_ref = &slice.items(.last_atom)[atom_ptr.output_section_index]; |
| 1988 | |
| 1989 | // FIXME:JK this only works if this atom is the only atom in the output section |
| 1990 | // In every other case, we need to redo the prev/next links |
| 1991 | if (last_atom_ref.eql(atom_ptr.ref())) last_atom_ref.* = .{}; |
| 1992 | |
| 1985 | 1993 | const alloc_res = try elf_file.allocateChunk(.{ |
| 1986 | 1994 | .shndx = atom_ptr.output_section_index, |
| 1987 | 1995 | .size = atom_ptr.size, |
| 1988 | 1996 | .alignment = atom_ptr.alignment, |
| 1997 | .requires_padding = requires_padding, |
| 1989 | 1998 | }); |
| 1990 | 1999 | atom_ptr.value = @intCast(alloc_res.value); |
| 1991 | | |
| 1992 | | const slice = elf_file.sections.slice(); |
| 1993 | | const shdr = &slice.items(.shdr)[atom_ptr.output_section_index]; |
| 1994 | | const last_atom_ref = &slice.items(.last_atom)[atom_ptr.output_section_index]; |
| 2000 | log.debug("allocated {s} at {x}\n placement {?}", .{ |
| 2001 | atom_ptr.name(elf_file), |
| 2002 | atom_ptr.offset(elf_file), |
| 2003 | alloc_res.placement, |
| 2004 | }); |
| 1995 | 2005 | |
| 1996 | 2006 | const expand_section = if (elf_file.atom(alloc_res.placement)) |placement_atom| |
| 1997 | 2007 | placement_atom.nextAtom(elf_file) == null |
| ... | ... | @@ -2013,12 +2023,6 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2013 | 2023 | } |
| 2014 | 2024 | shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?); |
| 2015 | 2025 | |
| 2016 | | if (self.sectionSymbol(atom_ptr.output_section_index, elf_file)) |sym| { |
| 2017 | | assert(sym.atom(elf_file) == null and sym.mergeSubsection(elf_file) == null); |
| 2018 | | const esym = &self.symtab.items(.elf_sym)[sym.esym_index]; |
| 2019 | | esym.st_size += atom_ptr.size + Elf.padToIdeal(atom_ptr.size); |
| 2020 | | } |
| 2021 | | |
| 2022 | 2026 | // This function can also reallocate an atom. |
| 2023 | 2027 | // In this case we need to "unplug" it from its previous location before |
| 2024 | 2028 | // plugging it in to its new location. |
| ... | ... | @@ -2037,12 +2041,8 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2037 | 2041 | atom_ptr.prev_atom_ref = .{ .index = 0, .file = 0 }; |
| 2038 | 2042 | atom_ptr.next_atom_ref = .{ .index = 0, .file = 0 }; |
| 2039 | 2043 | } |
| 2040 | | } |
| 2041 | 2044 | |
| 2042 | | fn growAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2043 | | if (!atom_ptr.alignment.check(@intCast(atom_ptr.value)) or atom_ptr.size > atom_ptr.capacity(elf_file)) { |
| 2044 | | try self.allocateAtom(atom_ptr, elf_file); |
| 2045 | | } |
| 2045 | log.debug(" prev {?}, next {?}", .{ atom_ptr.prev_atom_ref, atom_ptr.next_atom_ref }); |
| 2046 | 2046 | } |
| 2047 | 2047 | |
| 2048 | 2048 | pub fn resetShdrIndexes(self: *ZigObject, backlinks: anytype) void { |