Skip to content

Commit

Permalink
Merge pull request #104 from kubkon/elf-fixes
Browse files Browse the repository at this point in the history
elf: fix invalid logic in combining input sections into output sections
  • Loading branch information
kubkon authored Jan 26, 2024
2 parents d90c5c8 + c9b1c62 commit 9797926
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ fn initOutputSections(self: *Elf) !void {
atom.out_shndx = try object.initOutputSection(self, atom.getInputShdr(self));
}
}

self.text_sect_index = self.getSectionByName(".text");
}

fn initSyntheticSections(self: *Elf) !void {
Expand Down
9 changes: 3 additions & 6 deletions src/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ pub fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) !u1
const name = blk: {
const name = self.getShString(shdr.sh_name);
if (elf_file.options.relocatable) break :blk name;
if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name;
if (shdr.sh_flags & elf.SHF_MERGE != 0 and shdr.sh_flags & elf.SHF_STRINGS == 0)
break :blk name; // TODO: consider dropping SHF_STRINGS once ICF is implemented
const sh_name_prefixes: []const [:0]const u8 = &.{
".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table", ".ctors",
Expand Down Expand Up @@ -295,15 +296,11 @@ pub fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) !u1
else => flags,
};
};
const out_shndx = elf_file.getSectionByName(name) orelse try elf_file.addSection(.{
return elf_file.getSectionByName(name) orelse try elf_file.addSection(.{
.type = @"type",
.flags = flags,
.name = name,
});
if (mem.eql(u8, ".text", name)) {
elf_file.text_sect_index = out_shndx;
}
return out_shndx;
}

fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void {
Expand Down
2 changes: 1 addition & 1 deletion src/Elf/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn parse(arena: Allocator, args: []const []const u8, ctx: anytype) !Options
opts.emit.sub_path = path;
} else if (p.flag1("r") or p.flagAny("relocatable")) {
opts.relocatable = true;
} else if (p.arg1("image-base")) |value| {
} else if (p.argAny("image-base")) |value| {
opts.image_base = std.fmt.parseInt(u64, value, 0) catch
ctx.fatal("Could not parse value '{s}' into integer", .{value});
} else if (p.flagAny("gc-sections")) {
Expand Down

0 comments on commit 9797926

Please sign in to comment.