Skip to content

Commit

Permalink
Update to latest zig master
Browse files Browse the repository at this point in the history
git-rev 6b9f609bd01ad737b3e00825e065032e8a330535
  • Loading branch information
kubkon committed Jul 23, 2022
1 parent f0de089 commit 9dcaee1
Show file tree
Hide file tree
Showing 24 changed files with 5,602 additions and 2,962 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
submodules: true
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.1
version: master
- run: zig fmt --check src
- run: zig build test
- run: zig build install
Expand Down
12 changes: 11 additions & 1 deletion src/Coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ pub fn closeFiles(self: Coff) void {
}

pub fn flush(self: *Coff) !void {
try self.parsePositionals(self.base.options.positionals);
const gpa = self.base.allocator;

var positionals = std.ArrayList([]const u8).init(gpa);
defer positionals.deinit();
try positionals.ensureTotalCapacity(self.base.options.positionals.len);

for (self.base.options.positionals) |obj| {
positionals.appendAssumeCapacity(obj.path);
}

try self.parsePositionals(positionals.items);
}

fn parsePositionals(self: *Coff, files: []const []const u8) !void {
Expand Down
8 changes: 4 additions & 4 deletions src/Coff/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ pub const IMAGE_SYM_CLASS_SECTION = 104;
pub const IMAGE_SYM_CLASS_WEAK_EXTERNAL = 105;
pub const IMAGE_SYM_CLASS_CLR_TOKEN = 107;

comptime {
assert(@sizeOf(Symbol) == 18);
assert(@sizeOf(CoffHeader) == 20);
}
// comptime {
// assert(@sizeOf(Symbol) == 18);
// assert(@sizeOf(CoffHeader) == 20);
// }

pub fn deinit(self: *Object, allocator: Allocator) void {
self.symtab.deinit(allocator);
Expand Down
25 changes: 17 additions & 8 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub fn flush(self: *Elf) !void {
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();

const gc_sections = self.base.options.gc_sections orelse false;

var lib_dirs = std.ArrayList([]const u8).init(arena);
for (self.base.options.lib_dirs) |dir| {
// Verify that search path actually exists
Expand All @@ -204,12 +206,12 @@ pub fn flush(self: *Elf) !void {
try lib_dirs.append(dir);
}

var libs = std.ArrayList([]const u8).init(arena);
var libs = std.StringArrayHashMap(Zld.SystemLib).init(arena);
var lib_not_found = false;
for (self.base.options.libs) |lib_name| {
for (self.base.options.libs.keys()) |lib_name| {
for (&[_][]const u8{ ".dylib", ".a" }) |ext| {
if (try resolveLib(arena, lib_dirs.items, lib_name, ext)) |full_path| {
try libs.append(full_path);
try libs.put(full_path, self.base.options.libs.get(lib_name).?);
break;
}
} else {
Expand All @@ -224,8 +226,14 @@ pub fn flush(self: *Elf) !void {
}
}

try self.parsePositionals(self.base.options.positionals);
try self.parseLibs(libs.items);
var positionals = std.ArrayList([]const u8).init(arena);
try positionals.ensureTotalCapacity(self.base.options.positionals.len);
for (self.base.options.positionals) |obj| {
positionals.appendAssumeCapacity(obj.path);
}

try self.parsePositionals(positionals.items);
try self.parseLibs(libs.keys());

for (self.objects.items) |_, object_id| {
try self.resolveSymbolsInObject(@intCast(u16, object_id));
Expand All @@ -249,7 +257,10 @@ pub fn flush(self: *Elf) !void {
try object.parseIntoAtoms(self.base.allocator, @intCast(u16, object_id), self);
}

try self.gcAtoms();
if (gc_sections) {
try self.gcAtoms();
}

try self.sortShdrs();
try self.setStackSize();
try self.allocateLoadRSeg();
Expand Down Expand Up @@ -987,8 +998,6 @@ fn pruneShdrs(self: *Elf) !void {
}

fn gcAtoms(self: *Elf) !void {
if (!self.base.options.gc_sections) return;

try self.assignShndxToSymbols();

// TODO this just beginning of GC implementation. Consult with the docs of LLD which section is
Expand Down
26 changes: 10 additions & 16 deletions src/Elf/Atom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,22 @@ pub fn resolveRelocs(self: *Atom, elf_file: *Elf) !void {
},
elf.R_X86_64_32 => {
const target = self.getTargetAddress(r_sym, elf_file);
const scaled = math.cast(u32, @intCast(i64, target) + rel.r_addend) catch |err| switch (err) {
error.Overflow => {
log.err("R_X86_64_32: target value overflows 32bits", .{});
log.err(" target value 0x{x}", .{@intCast(i64, target) + rel.r_addend});
log.err(" target symbol {s}", .{tsym_name});
return error.RelocationOverflow;
},
else => |e| return e,
const scaled = math.cast(u32, @intCast(i64, target) + rel.r_addend) orelse {
log.err("R_X86_64_32: target value overflows 32bits", .{});
log.err(" target value 0x{x}", .{@intCast(i64, target) + rel.r_addend});
log.err(" target symbol {s}", .{tsym_name});
return error.RelocationOverflow;
};
log.debug("R_X86_64_32: {x}: [() => 0x{x}] ({s})", .{ rel.r_offset, scaled, tsym_name });
mem.writeIntLittle(u32, self.code.items[rel.r_offset..][0..4], scaled);
},
elf.R_X86_64_32S => {
const target = self.getTargetAddress(r_sym, elf_file);
const scaled = math.cast(i32, @intCast(i64, target) + rel.r_addend) catch |err| switch (err) {
error.Overflow => {
log.err("R_X86_64_32: target value overflows 32bits", .{});
log.err(" target value 0x{x}", .{@intCast(i64, target) + rel.r_addend});
log.err(" target symbol {s}", .{tsym_name});
return error.RelocationOverflow;
},
else => |e| return e,
const scaled = math.cast(i32, @intCast(i64, target) + rel.r_addend) orelse {
log.err("R_X86_64_32: target value overflows 32bits", .{});
log.err(" target value 0x{x}", .{@intCast(i64, target) + rel.r_addend});
log.err(" target symbol {s}", .{tsym_name});
return error.RelocationOverflow;
};
log.debug("R_X86_64_32S: {x}: [() => 0x{x}] ({s})", .{ rel.r_offset, scaled, tsym_name });
mem.writeIntLittle(i32, self.code.items[rel.r_offset..][0..4], scaled);
Expand Down
Loading

0 comments on commit 9dcaee1

Please sign in to comment.