Skip to content

Commit

Permalink
elf: fix handling of section names in shstrtab
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Sep 20, 2024
1 parent 2c928eb commit 7c8c9af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
51 changes: 25 additions & 26 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn flush(self: *Elf) !void {
try self.strtab.append(gpa, 0);
try self.dynstrtab.append(gpa, 0);
// Append null section.
_ = try self.addSection(.{ .name = "" });
_ = try self.addSection(.{});
// Append null symbol.
try self.symtab.append(gpa, null_sym);
// Append null file.
Expand Down Expand Up @@ -483,9 +483,8 @@ fn initOutputSections(self: *Elf) !void {

for (self.merge_sections.items) |*msec| {
if (msec.subsections.items.len == 0) continue;
const name = msec.getName(self);
const shndx = self.getSectionByName(name) orelse try self.addSection(.{
.name = name,
const shndx = self.getSectionByName(msec.getName(self)) orelse try self.addSection(.{
.name = msec.name,
.type = msec.type,
.flags = msec.flags,
});
Expand All @@ -509,15 +508,15 @@ fn initSyntheticSections(self: *Elf) !void {
} else false;
if (needs_eh_frame) {
self.eh_frame_sect_index = try self.addSection(.{
.name = ".eh_frame",
.name = try self.insertShString(".eh_frame"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC,
.addralign = @alignOf(u64),
});

if (self.options.eh_frame_hdr) {
self.eh_frame_hdr_sect_index = try self.addSection(.{
.name = ".eh_frame_hdr",
.name = try self.insertShString(".eh_frame_hdr"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC,
.addralign = @alignOf(u32),
Expand All @@ -527,15 +526,15 @@ fn initSyntheticSections(self: *Elf) !void {

if (self.got.entries.items.len > 0) {
self.got_sect_index = try self.addSection(.{
.name = ".got",
.name = try self.insertShString(".got"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC | elf.SHF_WRITE,
.addralign = @alignOf(u64),
});
}

self.got_plt_sect_index = try self.addSection(.{
.name = ".got.plt",
.name = try self.insertShString(".got.plt"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC | elf.SHF_WRITE,
.addralign = @alignOf(u64),
Expand All @@ -551,7 +550,7 @@ fn initSyntheticSections(self: *Elf) !void {
};
if (needs_rela_dyn) {
self.rela_dyn_sect_index = try self.addSection(.{
.name = ".rela.dyn",
.name = try self.insertShString(".rela.dyn"),
.type = elf.SHT_RELA,
.flags = elf.SHF_ALLOC,
.addralign = @alignOf(elf.Elf64_Rela),
Expand All @@ -561,13 +560,13 @@ fn initSyntheticSections(self: *Elf) !void {

if (self.plt.symbols.items.len > 0) {
self.plt_sect_index = try self.addSection(.{
.name = ".plt",
.name = try self.insertShString(".plt"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
.addralign = 16,
});
self.rela_plt_sect_index = try self.addSection(.{
.name = ".rela.plt",
.name = try self.insertShString(".rela.plt"),
.type = elf.SHT_RELA,
.flags = elf.SHF_ALLOC,
.addralign = @alignOf(elf.Elf64_Rela),
Expand All @@ -577,7 +576,7 @@ fn initSyntheticSections(self: *Elf) !void {

if (self.plt_got.symbols.items.len > 0) {
self.plt_got_sect_index = try self.addSection(.{
.name = ".plt.got",
.name = try self.insertShString(".plt.got"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
.addralign = 16,
Expand All @@ -586,7 +585,7 @@ fn initSyntheticSections(self: *Elf) !void {

if (self.copy_rel.symbols.items.len > 0) {
self.copy_rel_sect_index = try self.addSection(.{
.name = ".copyrel",
.name = try self.insertShString(".copyrel"),
.type = elf.SHT_NOBITS,
.flags = elf.SHF_ALLOC | elf.SHF_WRITE,
});
Expand All @@ -609,7 +608,7 @@ fn initSyntheticSections(self: *Elf) !void {
};
if (needs_interp) {
self.interp_sect_index = try self.addSection(.{
.name = ".interp",
.name = try self.insertShString(".interp"),
.type = elf.SHT_PROGBITS,
.flags = elf.SHF_ALLOC,
.addralign = 1,
Expand All @@ -618,35 +617,35 @@ fn initSyntheticSections(self: *Elf) !void {

if (self.options.shared or self.shared_objects.items.len > 0 or self.options.pie) {
self.dynstrtab_sect_index = try self.addSection(.{
.name = ".dynstr",
.name = try self.insertShString(".dynstr"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_STRTAB,
.entsize = 1,
.addralign = 1,
});
self.dynamic_sect_index = try self.addSection(.{
.name = ".dynamic",
.name = try self.insertShString(".dynamic"),
.flags = elf.SHF_ALLOC | elf.SHF_WRITE,
.type = elf.SHT_DYNAMIC,
.entsize = @sizeOf(elf.Elf64_Dyn),
.addralign = @alignOf(elf.Elf64_Dyn),
});
self.dynsymtab_sect_index = try self.addSection(.{
.name = ".dynsym",
.name = try self.insertShString(".dynsym"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_DYNSYM,
.addralign = @alignOf(elf.Elf64_Sym),
.entsize = @sizeOf(elf.Elf64_Sym),
});
self.hash_sect_index = try self.addSection(.{
.name = ".hash",
.name = try self.insertShString(".hash"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_HASH,
.addralign = 4,
.entsize = 4,
});
self.gnu_hash_sect_index = try self.addSection(.{
.name = ".gnu.hash",
.name = try self.insertShString(".gnu.hash"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_GNU_HASH,
.addralign = 8,
Expand All @@ -658,14 +657,14 @@ fn initSyntheticSections(self: *Elf) !void {
} else false;
if (needs_versions) {
self.versym_sect_index = try self.addSection(.{
.name = ".gnu.version",
.name = try self.insertShString(".gnu.version"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_GNU_VERSYM,
.addralign = @alignOf(elf.Elf64_Versym),
.entsize = @sizeOf(elf.Elf64_Versym),
});
self.verneed_sect_index = try self.addSection(.{
.name = ".gnu.version_r",
.name = try self.insertShString(".gnu.version_r"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_GNU_VERNEED,
.addralign = @alignOf(elf.Elf64_Verneed),
Expand All @@ -676,13 +675,13 @@ fn initSyntheticSections(self: *Elf) !void {

pub fn initSymtab(self: *Elf) !void {
self.strtab_sect_index = try self.addSection(.{
.name = ".strtab",
.name = try self.insertShString(".strtab"),
.type = elf.SHT_STRTAB,
.entsize = 1,
.addralign = 1,
});
self.symtab_sect_index = try self.addSection(.{
.name = ".symtab",
.name = try self.insertShString(".symtab"),
.type = elf.SHT_SYMTAB,
.addralign = @alignOf(elf.Elf64_Sym),
.entsize = @sizeOf(elf.Elf64_Sym),
Expand All @@ -691,7 +690,7 @@ pub fn initSymtab(self: *Elf) !void {

pub fn initShStrtab(self: *Elf) !void {
self.shstrtab_sect_index = try self.addSection(.{
.name = ".shstrtab",
.name = try self.insertShString(".shstrtab"),
.type = elf.SHT_STRTAB,
.entsize = 1,
.addralign = 1,
Expand Down Expand Up @@ -2332,7 +2331,7 @@ fn writeHeader(self: *Elf) !void {
}

pub const AddSectionOpts = struct {
name: [:0]const u8,
name: u32 = 0,
type: u32 = elf.SHT_NULL,
flags: u64 = 0,
link: u32 = 0,
Expand All @@ -2347,7 +2346,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {
const index = @as(u32, @intCast(try self.sections.addOne(gpa)));
self.sections.set(index, .{
.shdr = .{
.sh_name = try self.insertShString(opts.name),
.sh_name = opts.name,
.sh_type = opts.type,
.sh_flags = opts.flags,
.sh_addr = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) !u3
return elf_file.getSectionByName(name) orelse try elf_file.addSection(.{
.type = @"type",
.flags = flags,
.name = name,
.name = try elf_file.insertShString(name),
});
}

Expand Down
11 changes: 5 additions & 6 deletions src/Elf/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ fn initSections(elf_file: *Elf) !void {

for (elf_file.merge_sections.items) |*msec| {
if (msec.subsections.items.len == 0) continue;
const name = msec.getName(elf_file);
const shndx = elf_file.getSectionByName(name) orelse try elf_file.addSection(.{
.name = name,
const shndx = elf_file.getSectionByName(msec.getName(elf_file)) orelse try elf_file.addSection(.{
.name = msec.name,
.type = msec.type,
.flags = msec.flags,
});
Expand All @@ -85,13 +84,13 @@ fn initSections(elf_file: *Elf) !void {
} else false;
if (needs_eh_frame) {
elf_file.eh_frame_sect_index = try elf_file.addSection(.{
.name = ".eh_frame",
.name = try elf_file.insertShString(".eh_frame"),
.flags = elf.SHF_ALLOC,
.type = elf.SHT_PROGBITS,
.addralign = @alignOf(u64),
});
const rela_shndx = try elf_file.addSection(.{
.name = ".rela.eh_frame",
.name = try elf_file.insertShString(".rela.eh_frame"),
.type = elf.SHT_RELA,
.flags = elf.SHF_INFO_LINK,
.entsize = @sizeOf(elf.Elf64_Rela),
Expand All @@ -115,7 +114,7 @@ fn initComdatGroups(elf_file: *Elf) !void {
const cg_sec = try elf_file.comdat_group_sections.addOne(gpa);
cg_sec.* = .{
.shndx = try elf_file.addSection(.{
.name = ".group",
.name = try elf_file.insertShString(".group"),
.type = elf.SHT_GROUP,
.entsize = @sizeOf(u32),
.addralign = @alignOf(u32),
Expand Down

0 comments on commit 7c8c9af

Please sign in to comment.