Skip to content

std.elf: stronger typing #23600

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 41 additions & 41 deletions lib/compiler/aro/backend/Object/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const Section = struct {
data: std.ArrayList(u8),
relocations: std.ArrayListUnmanaged(Relocation) = .empty,
flags: u64,
type: u32,
index: u16 = undefined,
type: std.elf.SHT,
index: std.elf.SHN = undefined,
};

const Symbol = struct {
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn getSection(elf: *Elf, section_kind: Object.Section) !*std.ArrayList(u8) {
const section = try elf.arena.allocator().create(Section);
section.* = .{
.data = std.ArrayList(u8).init(elf.arena.child_allocator),
.type = std.elf.SHT_PROGBITS,
.type = .PROGBITS,
.flags = switch (section_kind) {
.func, .custom => std.elf.SHF_ALLOC + std.elf.SHF_EXECINSTR,
.strings => std.elf.SHF_ALLOC + std.elf.SHF_MERGE + std.elf.SHF_STRINGS,
Expand Down Expand Up @@ -116,16 +116,16 @@ pub fn declareSymbol(
const section_name = sectionString(section_kind);
break :blk elf.sections.get(section_name);
};
const binding: u8 = switch (linkage) {
.Internal => std.elf.STB_LOCAL,
.Strong => std.elf.STB_GLOBAL,
.Weak => std.elf.STB_WEAK,
const binding: std.elf.STB = switch (linkage) {
.Internal => .LOCAL,
.Strong => .GLOBAL,
.Weak => .WEAK,
.LinkOnce => unreachable,
};
const sym_type: u8 = switch (@"type") {
.func => std.elf.STT_FUNC,
.variable => std.elf.STT_OBJECT,
.external => std.elf.STT_NOTYPE,
const sym_type: std.elf.STT = switch (@"type") {
.func => .FUNC,
.variable => .OBJECT,
.external => .NOTYPE,
};
const name = if (maybe_name) |some| some else blk: {
defer elf.unnamed_symbol_mangle += 1;
Expand Down Expand Up @@ -174,29 +174,29 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
var buf_writer = std.io.bufferedWriter(file.writer());
const w = buf_writer.writer();

var num_sections: std.elf.Elf64_Half = additional_sections;
var relocations_len: std.elf.Elf64_Off = 0;
var sections_len: std.elf.Elf64_Off = 0;
var num_sections: std.elf.Half = additional_sections;
var relocations_len: std.elf.elf64.Off = 0;
var sections_len: std.elf.elf64.Off = 0;
{
var it = elf.sections.valueIterator();
while (it.next()) |sect| {
sections_len += sect.*.data.items.len;
relocations_len += sect.*.relocations.items.len * @sizeOf(std.elf.Elf64_Rela);
sect.*.index = num_sections;
relocations_len += sect.*.relocations.items.len * @sizeOf(std.elf.elf64.Rela);
sect.*.index = @enumFromInt(num_sections);
num_sections += 1;
num_sections += @intFromBool(sect.*.relocations.items.len != 0);
}
}
const symtab_len = (elf.local_symbols.count() + elf.global_symbols.count() + 1) * @sizeOf(std.elf.Elf64_Sym);
const symtab_len = (elf.local_symbols.count() + elf.global_symbols.count() + 1) * @sizeOf(std.elf.elf64.Sym);

const symtab_offset = @sizeOf(std.elf.Elf64_Ehdr) + sections_len;
const symtab_offset = @sizeOf(std.elf.elf64.Ehdr) + sections_len;
const symtab_offset_aligned = std.mem.alignForward(u64, symtab_offset, 8);
const rela_offset = symtab_offset_aligned + symtab_len;
const strtab_offset = rela_offset + relocations_len;
const sh_offset = strtab_offset + elf.strtab_len;
const sh_offset_aligned = std.mem.alignForward(u64, sh_offset, 16);

const elf_header = std.elf.Elf64_Ehdr{
const elf_header = std.elf.elf64.Ehdr{
.e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
.e_type = std.elf.ET.REL, // we only produce relocatables
.e_machine = elf.obj.target.toElfMachine(),
Expand All @@ -205,10 +205,10 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
.e_phoff = 0, // no program header
.e_shoff = sh_offset_aligned, // section headers offset
.e_flags = 0, // no flags
.e_ehsize = @sizeOf(std.elf.Elf64_Ehdr),
.e_ehsize = @sizeOf(std.elf.elf64.Ehdr),
.e_phentsize = 0, // no program header
.e_phnum = 0, // no program header
.e_shentsize = @sizeOf(std.elf.Elf64_Shdr),
.e_shentsize = @sizeOf(std.elf.elf64.Shdr),
.e_shnum = num_sections,
.e_shstrndx = strtab_index,
};
Expand All @@ -227,36 +227,36 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
// write symbols
{
// first symbol must be null
try w.writeStruct(std.mem.zeroes(std.elf.Elf64_Sym));
try w.writeStruct(std.mem.zeroes(std.elf.elf64.Sym));

var sym_index: u16 = 1;
var it = elf.local_symbols.iterator();
while (it.next()) |entry| {
const sym = entry.value_ptr.*;
try w.writeStruct(std.elf.Elf64_Sym{
try w.writeStruct(std.elf.elf64.Sym{
.st_name = name_offset,
.st_info = sym.info,
.st_other = 0,
.st_shndx = if (sym.section) |some| some.index else 0,
.st_value = sym.offset,
.st_size = sym.size,
});
sym.index = sym_index;
sym.index = @enumFromInt(sym_index);
sym_index += 1;
name_offset += @intCast(entry.key_ptr.len + 1); // +1 for null byte
}
it = elf.global_symbols.iterator();
while (it.next()) |entry| {
const sym = entry.value_ptr.*;
try w.writeStruct(std.elf.Elf64_Sym{
try w.writeStruct(std.elf.elf64.Sym{
.st_name = name_offset,
.st_info = sym.info,
.st_other = 0,
.st_shndx = if (sym.section) |some| some.index else 0,
.st_value = sym.offset,
.st_size = sym.size,
});
sym.index = sym_index;
sym.index = @enumFromInt(sym_index);
sym_index += 1;
name_offset += @intCast(entry.key_ptr.len + 1); // +1 for null byte
}
Expand All @@ -267,7 +267,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
var it = elf.sections.valueIterator();
while (it.next()) |sect| {
for (sect.*.relocations.items) |rela| {
try w.writeStruct(std.elf.Elf64_Rela{
try w.writeStruct(std.elf.elf64.Rela{
.r_offset = rela.offset,
.r_addend = rela.addend,
.r_info = (@as(u64, rela.symbol.index) << 32) | rela.type,
Expand Down Expand Up @@ -295,13 +295,13 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
// pad to 16 bytes
try w.writeByteNTimes(0, @intCast(sh_offset_aligned - sh_offset));
// mandatory null header
try w.writeStruct(std.mem.zeroes(std.elf.Elf64_Shdr));
try w.writeStruct(std.mem.zeroes(std.elf.elf64.Shdr));

// write strtab section header
{
const sect_header = std.elf.Elf64_Shdr{
const sect_header = std.elf.elf64.Shdr{
.sh_name = strtab_name,
.sh_type = std.elf.SHT_STRTAB,
.sh_type = .STRTAB,
.sh_flags = 0,
.sh_addr = 0,
.sh_offset = strtab_offset,
Expand All @@ -316,31 +316,31 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {

// write symtab section header
{
const sect_header = std.elf.Elf64_Shdr{
const sect_header = std.elf.elf64.Shdr{
.sh_name = symtab_name,
.sh_type = std.elf.SHT_SYMTAB,
.sh_type = .SYMTAB,
.sh_flags = 0,
.sh_addr = 0,
.sh_offset = symtab_offset_aligned,
.sh_size = symtab_len,
.sh_link = strtab_index,
.sh_info = elf.local_symbols.size + 1,
.sh_addralign = 8,
.sh_entsize = @sizeOf(std.elf.Elf64_Sym),
.sh_entsize = @sizeOf(std.elf.elf64.Sym),
};
try w.writeStruct(sect_header);
}

// remaining section headers
{
var sect_offset: u64 = @sizeOf(std.elf.Elf64_Ehdr);
var sect_offset: u64 = @sizeOf(std.elf.elf64.Ehdr);
var rela_sect_offset: u64 = rela_offset;
var it = elf.sections.iterator();
while (it.next()) |entry| {
const sect = entry.value_ptr.*;
const rela_count = sect.relocations.items.len;
const rela_name_offset: u32 = if (rela_count != 0) @truncate(".rela".len) else 0;
try w.writeStruct(std.elf.Elf64_Shdr{
try w.writeStruct(std.elf.elf64.Shdr{
.sh_name = rela_name_offset + name_offset,
.sh_type = sect.type,
.sh_flags = sect.flags,
Expand All @@ -354,18 +354,18 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
});

if (rela_count != 0) {
const size = rela_count * @sizeOf(std.elf.Elf64_Rela);
try w.writeStruct(std.elf.Elf64_Shdr{
const size = rela_count * @sizeOf(std.elf.elf64.Rela);
try w.writeStruct(std.elf.elf64.Shdr{
.sh_name = name_offset,
.sh_type = std.elf.SHT_RELA,
.sh_type = .RELA,
.sh_flags = 0,
.sh_addr = 0,
.sh_offset = rela_sect_offset,
.sh_size = rela_count * @sizeOf(std.elf.Elf64_Rela),
.sh_size = size,
.sh_link = symtab_index,
.sh_info = sect.index,
.sh_info = @intFromEnum(sect.index),
.sh_addralign = 8,
.sh_entsize = @sizeOf(std.elf.Elf64_Rela),
.sh_entsize = @sizeOf(std.elf.elf64.Rela),
});
rela_sect_offset += size;
}
Expand Down
Loading