elf: move ownership of input merge sections to Object

This commit is contained in:
Jakub Konka 2024-07-24 06:42:43 +02:00
parent f219286573
commit 733d25000b
2 changed files with 35 additions and 33 deletions

View File

@ -215,8 +215,6 @@ merge_sections: std.ArrayListUnmanaged(MergeSection) = .{},
/// List of output merge subsections.
/// Each subsection is akin to Atom but belongs to a MergeSection.
merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
/// List of input merge sections as parsed from input relocatables.
merge_input_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
/// Table of last atom index in a section and matching atom free list if any.
last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
@ -390,8 +388,6 @@ pub fn createEmpty(
_ = try self.addSection(.{ .name = "" });
// Append null symbol in output symtab
try self.symtab.append(gpa, null_sym);
// Append null input merge section.
try self.merge_input_sections.append(gpa, .{});
if (!is_obj_or_ar) {
try self.dynstrtab.append(gpa, 0);
@ -515,10 +511,6 @@ pub fn deinit(self: *Elf) void {
}
self.merge_sections.deinit(gpa);
self.merge_subsections.deinit(gpa);
for (self.merge_input_sections.items) |*sect| {
sect.deinit(gpa);
}
self.merge_input_sections.deinit(gpa);
for (self.last_atom_and_free_list_table.values()) |*value| {
value.free_list.deinit(gpa);
}
@ -5847,18 +5839,6 @@ pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupO
return &self.comdat_groups_owners.items[index];
}
pub fn addInputMergeSection(self: *Elf) !InputMergeSection.Index {
const index: InputMergeSection.Index = @intCast(self.merge_input_sections.items.len);
const msec = try self.merge_input_sections.addOne(self.base.comp.gpa);
msec.* = .{};
return index;
}
pub fn inputMergeSection(self: *Elf, index: InputMergeSection.Index) ?*InputMergeSection {
if (index == 0) return null;
return &self.merge_input_sections.items[index];
}
pub fn addMergeSubsection(self: *Elf) !MergeSubsection.Index {
const index: MergeSubsection.Index = @intCast(self.merge_subsections.items.len);
const msec = try self.merge_subsections.addOne(self.base.comp.gpa);

View File

@ -15,7 +15,8 @@ comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup.Index) = .{},
comdat_group_data: std.ArrayListUnmanaged(u32) = .{},
relocs: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
merge_sections: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
input_merge_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
input_merge_sections_indexes: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
fdes: std.ArrayListUnmanaged(Fde) = .{},
cies: std.ArrayListUnmanaged(Cie) = .{},
@ -53,7 +54,11 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
self.fdes.deinit(allocator);
self.cies.deinit(allocator);
self.eh_frame_data.deinit(allocator);
self.merge_sections.deinit(allocator);
for (self.input_merge_sections.items) |*isec| {
isec.deinit(allocator);
}
self.input_merge_sections.deinit(allocator);
self.input_merge_sections_indexes.deinit(allocator);
}
pub fn parse(self: *Object, elf_file: *Elf) !void {
@ -62,6 +67,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
const handle = elf_file.fileHandle(self.file_handle);
try self.parseCommon(gpa, handle, elf_file);
// Append null input merge section
try self.input_merge_sections.append(gpa, .{});
try self.initAtoms(gpa, handle, elf_file);
try self.initSymtab(gpa, elf_file);
@ -664,8 +673,9 @@ pub fn checkDuplicates(self: *Object, dupes: anytype, elf_file: *Elf) error{OutO
pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
const gpa = elf_file.base.comp.gpa;
try self.merge_sections.resize(gpa, self.shdrs.items.len);
@memset(self.merge_sections.items, 0);
try self.input_merge_sections.ensureUnusedCapacity(gpa, self.shdrs.items.len);
try self.input_merge_sections_indexes.resize(gpa, self.shdrs.items.len);
@memset(self.input_merge_sections_indexes.items, 0);
for (self.shdrs.items, 0..) |shdr, shndx| {
if (shdr.sh_flags & elf.SHF_MERGE == 0) continue;
@ -675,9 +685,9 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
if (!atom_ptr.flags.alive) continue;
if (atom_ptr.relocs(elf_file).len > 0) continue;
const imsec_idx = try elf_file.addInputMergeSection();
const imsec = elf_file.inputMergeSection(imsec_idx).?;
self.merge_sections.items[shndx] = imsec_idx;
const imsec_idx = try self.addInputMergeSection(gpa);
const imsec = self.inputMergeSection(imsec_idx).?;
self.input_merge_sections_indexes.items[shndx] = imsec_idx;
imsec.merge_section_index = try elf_file.getOrCreateMergeSection(atom_ptr.name(elf_file), shdr.sh_flags, shdr.sh_type);
imsec.atom_index = atom_index;
@ -741,8 +751,8 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
const gpa = elf_file.base.comp.gpa;
for (self.merge_sections.items) |index| {
const imsec = elf_file.inputMergeSection(index) orelse continue;
for (self.input_merge_sections_indexes.items) |index| {
const imsec = self.inputMergeSection(index) orelse continue;
if (imsec.offsets.items.len == 0) continue;
const msec = elf_file.mergeSection(imsec.merge_section_index);
const atom_ptr = elf_file.atom(imsec.atom_index).?;
@ -776,8 +786,8 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
if (esym.st_shndx == elf.SHN_COMMON or esym.st_shndx == elf.SHN_UNDEF or esym.st_shndx == elf.SHN_ABS) continue;
const imsec_index = self.merge_sections.items[esym.st_shndx];
const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
const imsec_index = self.input_merge_sections_indexes.items[esym.st_shndx];
const imsec = self.inputMergeSection(imsec_index) orelse continue;
if (imsec.offsets.items.len == 0) continue;
const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {
var err = try elf_file.base.addErrorWithNotes(2);
@ -801,8 +811,8 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
const esym = self.symtab.items[rel.r_sym()];
if (esym.st_type() != elf.STT_SECTION) continue;
const imsec_index = self.merge_sections.items[esym.st_shndx];
const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
const imsec_index = self.input_merge_sections_indexes.items[esym.st_shndx];
const imsec = self.inputMergeSection(imsec_index) orelse continue;
if (imsec.offsets.items.len == 0) continue;
const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse {
var err = try elf_file.base.addErrorWithNotes(1);
@ -1184,6 +1194,18 @@ fn preadRelocsAlloc(self: Object, allocator: Allocator, handle: std.fs.File, shn
return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num];
}
fn addInputMergeSection(self: *Object, allocator: Allocator) !InputMergeSection.Index {
const index: InputMergeSection.Index = @intCast(self.input_merge_sections.items.len);
const msec = try self.input_merge_sections.addOne(allocator);
msec.* = .{};
return index;
}
fn inputMergeSection(self: *Object, index: InputMergeSection.Index) ?*InputMergeSection {
if (index == 0) return null;
return &self.input_merge_sections.items[index];
}
pub fn format(
self: *Object,
comptime unused_fmt_string: []const u8,