Skip to content

Commit

Permalink
elf: move ownership of InputMergeSections into owning objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Sep 9, 2024
1 parent 2401d66 commit dc6c8db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
19 changes: 0 additions & 19 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ thunks: std.ArrayListUnmanaged(Thunk) = .{},

merge_sections: std.ArrayListUnmanaged(MergeSection) = .{},
merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
merge_input_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},

comdat_groups: std.ArrayListUnmanaged(ComdatGroup) = .{},
comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},
Expand Down Expand Up @@ -155,10 +154,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);
self.comdat_groups.deinit(gpa);
self.comdat_groups_owners.deinit(gpa);
self.comdat_groups_table.deinit(gpa);
Expand Down Expand Up @@ -296,8 +291,6 @@ pub fn flush(self: *Elf) !void {
try self.symbols_extra.append(gpa, 0);
// Append null file.
try self.files.append(gpa, .null);
// Append null input merge section.
try self.merge_input_sections.append(gpa, .{});

var arena_allocator = std.heap.ArenaAllocator.init(gpa);
defer arena_allocator.deinit();
Expand Down Expand Up @@ -2986,18 +2979,6 @@ pub fn getGlobalByName(self: *Elf, name: []const u8) ?u32 {
return self.globals.get(off);
}

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.allocator);
msec.* = .{};
return index;
}

pub fn getInputMergeSection(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.allocator);
Expand Down
45 changes: 33 additions & 12 deletions src/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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) = .{},
merge_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
merge_sections_indexes: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},

fdes: std.ArrayListUnmanaged(Fde) = .{},
cies: std.ArrayListUnmanaged(Cie) = .{},
Expand Down Expand Up @@ -50,7 +51,11 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
self.relocs.deinit(allocator);
self.fdes.deinit(allocator);
self.cies.deinit(allocator);
for (self.merge_sections.items) |*sec| {
sec.deinit(allocator);
}
self.merge_sections.deinit(allocator);
self.merge_sections_indexes.deinit(allocator);
}

pub fn parse(self: *Object, elf_file: *Elf) !void {
Expand Down Expand Up @@ -107,6 +112,9 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
try self.strtab.appendSlice(gpa, strtab);
}

// Append null input merge section
try self.merge_sections.append(gpa, .{});

try self.initAtoms(gpa, file, elf_file);
try self.initSymtab(gpa, elf_file);

Expand Down Expand Up @@ -600,8 +608,9 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {

const gpa = elf_file.base.allocator;

try self.merge_sections.resize(gpa, self.shdrs.items.len);
@memset(self.merge_sections.items, 0);
try self.merge_sections.ensureUnusedCapacity(gpa, self.shdrs.items.len);
try self.merge_sections_indexes.resize(gpa, self.shdrs.items.len);
@memset(self.merge_sections_indexes.items, 0);

for (self.shdrs.items, 0..) |shdr, shndx| {
if (shdr.sh_flags & elf.SHF_MERGE == 0) continue;
Expand All @@ -611,9 +620,9 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
if (!atom.flags.alive) continue;
if (atom.getRelocs(elf_file).len > 0) continue;

const imsec_idx = try elf_file.addInputMergeSection();
const imsec = elf_file.getInputMergeSection(imsec_idx).?;
self.merge_sections.items[shndx] = imsec_idx;
const imsec_idx = try self.addInputMergeSection(gpa);
const imsec = self.getInputMergeSection(imsec_idx).?;
self.merge_sections_indexes.items[shndx] = imsec_idx;

imsec.merge_section = try elf_file.getOrCreateMergeSection(atom.getName(elf_file), shdr.sh_flags, shdr.sh_type);
imsec.atom = atom_index;
Expand Down Expand Up @@ -679,8 +688,8 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
const gpa = elf_file.base.allocator;

for (self.merge_sections.items) |index| {
const imsec = elf_file.getInputMergeSection(index) orelse continue;
for (self.merge_sections_indexes.items) |index| {
const imsec = self.getInputMergeSection(index) orelse continue;
if (imsec.offsets.items.len == 0) continue;
const msec = elf_file.getMergeSection(imsec.merge_section);
const atom = elf_file.getAtom(imsec.atom).?;
Expand Down Expand Up @@ -714,8 +723,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.getInputMergeSection(imsec_index) orelse continue;
const imsec_index = self.merge_sections_indexes.items[esym.st_shndx];
const imsec = self.getInputMergeSection(imsec_index) orelse continue;
if (imsec.offsets.items.len == 0) continue;
const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {
elf_file.base.fatal("{}: invalid symbol value: {s}:{x}", .{
Expand All @@ -741,8 +750,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.getInputMergeSection(imsec_index) orelse continue;
const imsec_index = self.merge_sections_indexes.items[esym.st_shndx];
const imsec = self.getInputMergeSection(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 {
elf_file.base.fatal("{}: {s}: invalid relocation at offset 0x{x}", .{
Expand Down Expand Up @@ -957,6 +966,18 @@ pub fn asFile(self: *Object) File {
return .{ .object = self };
}

fn addInputMergeSection(self: *Object, allocator: Allocator) !InputMergeSection.Index {
const index: InputMergeSection.Index = @intCast(self.merge_sections.items.len);
const msec = try self.merge_sections.addOne(allocator);
msec.* = .{};
return index;
}

fn getInputMergeSection(self: *Object, index: InputMergeSection.Index) ?*InputMergeSection {
if (index == 0) return null;
return &self.merge_sections.items[index];
}

pub fn format(
self: *Object,
comptime unused_fmt_string: []const u8,
Expand Down

0 comments on commit dc6c8db

Please sign in to comment.