Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle section groups #5

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage: zelf [options] file

General Options:
-a, --all Equivalent of having all flags on
-g, --section-groups Display the section groups
-h, --file-header Display ELF file header
-l, --program-headers Display program headers (if present)
-S, --section-headers Display section headers
Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {

const exe = b.addExecutable(.{
.name = "zelf",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
.use_llvm = use_llvm,
Expand Down
44 changes: 44 additions & 0 deletions src/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,50 @@ pub fn printPhdrs(self: Object, writer: anytype) !void {
}
}

pub fn printSectionGroups(self: Object, writer: anytype) !void {
const has_section_groups = for (self.shdrs.items) |shdr| switch (shdr.sh_type) {
elf.SHT_GROUP => break true,
else => {},
} else false;
if (!has_section_groups) return writer.print("There are no section groups in this file.\n", .{});

for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) {
elf.SHT_GROUP => {
const raw = self.getSectionContents(shdr);
const num_members = std.math.divExact(usize, raw.len, @sizeOf(u32)) catch {
// TODO convert into an error
return writer.print("Section {d}, '{s}' does not divide by group member size.\n", .{
i, self.getShString(shdr.sh_name),
});
};
if (num_members == 0) return writer.print("Empty section {d}, '{s}'.\n", .{
i, self.getShString(shdr.sh_name),
});
const members = @as([*]align(1) const u32, @ptrCast(raw.ptr))[0..num_members];
if (members[0] != elf.GRP_COMDAT) return writer.print("Unknown SHT_GROUP format in section {d}, '{s}'.\n", .{
i, self.getShString(shdr.sh_name),
});
const group_info_sym = self.symtab.items[shdr.sh_info];
const group_signature = if (group_info_sym.st_name == 0 and group_info_sym.st_type() == elf.STT_SECTION)
self.getShString(self.shdrs.items[group_info_sym.st_shndx].sh_name)
else
getString(self.strtab, group_info_sym.st_name);
try writer.print("COMDAT group section [{d: >5}] `{s}' [{s}] contains {d} sections:\n", .{
i, self.getShString(shdr.sh_name), group_signature, num_members - 1,
});
try writer.writeAll(" [Index] Name\n");
for (members[1..], 1..) |shndx, n| {
try writer.print(" [{d: >5}] {s}", .{
shndx,
self.getShString(self.shdrs.items[shndx].sh_name),
});
if (n < members.len - 1) try writer.writeByte('\n');
}
},
else => {},
};
}

pub fn printRelocs(self: Object, writer: anytype) !void {
const has_relocs = for (self.shdrs.items) |shdr| switch (shdr.sh_type) {
elf.SHT_RELA, elf.SHT_REL => break true,
Expand Down
10 changes: 10 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const usage =
\\
\\General Options:
\\-a, --all Equivalent of having all flags on
\\-g, --section-groups Display the section groups
\\-h, --file-header Display ELF file header
\\-l, --program-headers Display program headers (if present)
\\-S, --section-headers Display section headers
Expand Down Expand Up @@ -139,6 +140,7 @@ pub fn main() anyerror!void {
'-' => break :blk,
'a' => tmp = PrintMatrix.enableAll(),
'c' => tmp.archive_index = true,
'g' => tmp.section_groups = true,
'h' => tmp.header = true,
'l' => tmp.phdrs = true,
'S' => tmp.shdrs = true,
Expand Down Expand Up @@ -166,6 +168,8 @@ pub fn main() anyerror!void {
print_matrix.phdrs = true;
} else if (p.flag2("section-headers")) {
print_matrix.shdrs = true;
} else if (p.flag2("section-groups")) {
print_matrix.section_groups = true;
} else if (p.flag2("symbols")) {
print_matrix.dynamic_symbols = true;
print_matrix.symbols = true;
Expand Down Expand Up @@ -264,6 +268,10 @@ fn printObject(object: Object, print_matrix: PrintMatrix, arg_data: ArgData, std
try object.printPhdrs(stdout);
try stdout.writeAll("\n");
}
if (print_matrix.section_groups) {
try object.printSectionGroups(stdout);
try stdout.writeAll("\n");
}
if (print_matrix.relocs) {
try object.printRelocs(stdout);
try stdout.writeAll("\n");
Expand Down Expand Up @@ -319,6 +327,7 @@ const PrintMatrix = packed struct {
archive_index: bool = false,
phdrs: bool = false,
shdrs: bool = false,
section_groups: bool = false,
symbols: bool = false,
dynamic_symbols: bool = false,
dynamic_section: bool = false,
Expand Down Expand Up @@ -346,6 +355,7 @@ const PrintMatrix = packed struct {
.archive_index = true,
.phdrs = true,
.shdrs = true,
.section_groups = true,
.symbols = true,
.dynamic_symbols = true,
.dynamic_section = true,
Expand Down
Loading