Skip to content

Commit

Permalink
update to latest master build of Zig
Browse files Browse the repository at this point in the history
Updates to the new build API as well as the new for-loop syntax.
Syntax changes have been performed by running 'zig fmt'.
The build changes no longer support depending on the same file within
more than 1 module. To prevent having to fully restructure the test
suite, we set the main_pkg_path to the root directory as to allow for
imports outside the 'src' directory.
  • Loading branch information
Luukdegram committed Mar 6, 2023
1 parent d36f403 commit 12306f4
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 101 deletions.
9 changes: 2 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ pub fn build(b: *Builder) void {
const is_qemu_enabled = b.option(bool, "enable-qemu", "Use QEMU to run cross compiled foreign architecture tests") orelse false;
const enable_tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");

b.addModule(.{
.name = "dis_x86_64",
const dis_x86_64 = b.addModule("dis_x86_64", .{
.source_file = .{ .path = "zig-dis-x86_64/src/dis_x86_64.zig" },
});
const dis_x86_64 = b.modules.get("dis_x86_64").?;

const exe = b.addExecutable(.{
.name = "zld",
Expand Down Expand Up @@ -76,11 +74,8 @@ pub fn build(b: *Builder) void {
.root_source_file = .{ .path = "src/test.zig" },
.optimize = mode,
});
const test_base = b.createModule(.{ .source_file = .{ .path = "src/test.zig" } });
const e2e_tests = b.createModule(.{ .source_file = .{ .path = "test/test.zig" } });
e2e_tests.dependencies.put("test_base", test_base) catch @panic("OOM");
tests.addModule("end_to_end_tests", e2e_tests);
tests.addModule("dis_x86_64", dis_x86_64);
tests.main_pkg_path = "."; // set root directory as main package path for our tests

const test_opts = b.addOptions();
tests.addOptions("build_options", test_opts);
Expand Down
22 changes: 11 additions & 11 deletions src/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn flush(self: *Elf) !void {
try self.parsePositionals(positionals.items);
try self.parseLibs(libs.keys());

for (self.objects.items) |_, object_id| {
for (self.objects.items, 0..) |_, object_id| {
try self.resolveSymbolsInObject(@intCast(u16, object_id));
}
try self.resolveSymbolsInArchives();
Expand All @@ -236,7 +236,7 @@ pub fn flush(self: *Elf) !void {
try object.scanInputSections(self);
}

for (self.objects.items) |*object, object_id| {
for (self.objects.items, 0..) |*object, object_id| {
try object.splitIntoAtoms(self.base.allocator, @intCast(u16, object_id), self);
}

Expand Down Expand Up @@ -517,7 +517,7 @@ fn insertSection(self: *Elf, shdr: elf.Elf64_Shdr, shdr_name: []const u8) !u16 {
// allocated within each respective segment. Of course, it is good practice to have
// the sections sorted, but it's a useful hack we can use for the debug builds in
// self-hosted Zig compiler.
const insertion_index = for (self.sections.items(.shdr)) |oshdr, i| {
const insertion_index = for (self.sections.items(.shdr), 0..) |oshdr, i| {
const oshdr_name = self.shstrtab.getAssumeExists(oshdr.sh_name);
if (getSectionPrecedence(oshdr, oshdr_name) > precedence) break @intCast(u16, i);
} else @intCast(u16, self.sections.items(.shdr).len);
Expand Down Expand Up @@ -725,7 +725,7 @@ fn resolveSymbolsInObject(self: *Elf, object_id: u16) !void {

log.debug("resolving symbols in {s}", .{object.name});

for (object.symtab.items) |sym, i| {
for (object.symtab.items, 0..) |sym, i| {
const sym_id = @intCast(u32, i);
const sym_name = self.getSymbolName(.{ .sym_index = sym_id, .file = object_id });
const st_bind = sym.st_info >> 4;
Expand Down Expand Up @@ -1115,7 +1115,7 @@ fn allocateNonAllocSections(self: *Elf) !void {

fn allocateAtoms(self: *Elf) !void {
const slice = self.sections.slice();
for (slice.items(.last_atom)) |last_atom, i| {
for (slice.items(.last_atom), 0..) |last_atom, i| {
var atom = last_atom orelse continue;
const shdr_ndx = @intCast(u16, i);
const shdr = slice.items(.shdr)[shdr_ndx];
Expand Down Expand Up @@ -1194,7 +1194,7 @@ pub fn logAtom(self: *Elf, atom: *const Atom, comptime logger: anytype) void {

fn logAtoms(self: *Elf) void {
const slice = self.sections.slice();
for (slice.items(.last_atom)) |last_atom, i| {
for (slice.items(.last_atom), 0..) |last_atom, i| {
var atom = last_atom orelse continue;
const ndx = @intCast(u16, i);
const shdr = slice.items(.shdr)[ndx];
Expand All @@ -1216,7 +1216,7 @@ fn logAtoms(self: *Elf) void {

fn writeAtoms(self: *Elf) !void {
const slice = self.sections.slice();
for (slice.items(.last_atom)) |last_atom, i| {
for (slice.items(.last_atom), 0..) |last_atom, i| {
var atom = last_atom orelse continue;
const shdr_ndx = @intCast(u16, i);
const shdr = slice.items(.shdr)[shdr_ndx];
Expand Down Expand Up @@ -1301,7 +1301,7 @@ fn writeSymtab(self: *Elf) !void {
});

for (self.objects.items) |object| {
for (object.symtab.items) |sym, sym_id| {
for (object.symtab.items, 0..) |sym, sym_id| {
if (sym.st_name == 0) continue;
const st_bind = sym.st_info >> 4;
const st_type = sym.st_info & 0xf;
Expand Down Expand Up @@ -1421,7 +1421,7 @@ fn writeHeader(self: *Elf) !void {
}

pub fn getSectionByName(self: *Elf, name: []const u8) ?u16 {
for (self.sections.items(.shdr)) |shdr, i| {
for (self.sections.items(.shdr), 0..) |shdr, i| {
const this_name = self.shstrtab.getAssumeExists(shdr.sh_name);
if (mem.eql(u8, this_name, name)) return @intCast(u16, i);
} else return null;
Expand Down Expand Up @@ -1478,7 +1478,7 @@ pub fn getEntryPoint(self: Elf) error{EntrypointNotFound}!SymbolWithLoc {

fn logSections(self: Elf) void {
log.debug("sections:", .{});
for (self.sections.items(.shdr)) |shdr, i| {
for (self.sections.items(.shdr), 0..) |shdr, i| {
log.debug(" sect({d}): {s} @{x}, sizeof({x})", .{
i,
self.shstrtab.getAssumeExists(shdr.sh_name),
Expand All @@ -1491,7 +1491,7 @@ fn logSections(self: Elf) void {
fn logSymtab(self: Elf) void {
for (self.objects.items) |object| {
log.debug("locals in {s}", .{object.name});
for (object.symtab.items) |sym, i| {
for (object.symtab.items, 0..) |sym, i| {
// const st_type = sym.st_info & 0xf;
const st_bind = sym.st_info >> 4;
// if (st_bind != elf.STB_LOCAL or st_type != elf.STT_SECTION) continue;
Expand Down
10 changes: 5 additions & 5 deletions src/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)

if (self.header.e_shnum == 0) return;

for (self.getShdrs()) |shdr, i| switch (shdr.sh_type) {
for (self.getShdrs(), 0..) |shdr, i| switch (shdr.sh_type) {
elf.SHT_SYMTAB => {
self.symtab_index = @intCast(u16, i);
const nsyms = @divExact(shdr.sh_size, @sizeOf(elf.Elf64_Sym));
Expand Down Expand Up @@ -137,28 +137,28 @@ pub fn splitIntoAtoms(self: *Object, allocator: Allocator, object_id: u16, elf_f
var rel_shdrs = std.AutoHashMap(u16, u16).init(allocator);
defer rel_shdrs.deinit();

for (shdrs) |shdr, i| switch (shdr.sh_type) {
for (shdrs, 0..) |shdr, i| switch (shdr.sh_type) {
elf.SHT_REL, elf.SHT_RELA => {
try rel_shdrs.putNoClobber(@intCast(u16, shdr.sh_info), @intCast(u16, i));
},
else => {},
};

for (shdrs) |shdr, i| switch (shdr.sh_type) {
for (shdrs, 0..) |shdr, i| switch (shdr.sh_type) {
elf.SHT_PROGBITS, elf.SHT_NOBITS => {
try symbols_by_shndx.putNoClobber(@intCast(u16, i), std.ArrayList(u32).init(allocator));
},
else => {},
};

for (self.getSourceSymtab()) |sym, sym_id| {
for (self.getSourceSymtab(), 0..) |sym, sym_id| {
if (sym.st_shndx == elf.SHN_UNDEF) continue;
if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) continue;
const map = symbols_by_shndx.getPtr(sym.st_shndx) orelse continue;
try map.append(@intCast(u32, sym_id));
}

for (shdrs) |shdr, i| switch (shdr.sh_type) {
for (shdrs, 0..) |shdr, i| switch (shdr.sh_type) {
elf.SHT_PROGBITS, elf.SHT_NOBITS => {
const ndx = @intCast(u16, i);
const shdr_name = self.getShString(shdr.sh_name);
Expand Down
Loading

0 comments on commit 12306f4

Please sign in to comment.