macho: migrate InternalObject and Dylib
This commit is contained in:
@@ -3823,145 +3823,6 @@ pub fn getFileHandle(self: MachO, index: File.HandleIndex) File.Handle {
|
||||
return self.file_handles.items[index];
|
||||
}
|
||||
|
||||
pub fn addAtom(self: *MachO) error{OutOfMemory}!Atom.Index {
|
||||
const index = @as(Atom.Index, @intCast(self.atoms.items.len));
|
||||
const atom = try self.atoms.addOne(self.base.comp.gpa);
|
||||
atom.* = .{};
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getAtom(self: *MachO, index: Atom.Index) ?*Atom {
|
||||
if (index == 0) return null;
|
||||
assert(index < self.atoms.items.len);
|
||||
return &self.atoms.items[index];
|
||||
}
|
||||
|
||||
pub fn addAtomExtra(self: *MachO, extra: Atom.Extra) !u32 {
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
try self.atoms_extra.ensureUnusedCapacity(self.base.comp.gpa, fields.len);
|
||||
return self.addAtomExtraAssumeCapacity(extra);
|
||||
}
|
||||
|
||||
pub fn addAtomExtraAssumeCapacity(self: *MachO, extra: Atom.Extra) u32 {
|
||||
const index = @as(u32, @intCast(self.atoms_extra.items.len));
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
inline for (fields) |field| {
|
||||
self.atoms_extra.appendAssumeCapacity(switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
});
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getAtomExtra(self: *MachO, index: u32) ?Atom.Extra {
|
||||
if (index == 0) return null;
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
var i: usize = index;
|
||||
var result: Atom.Extra = undefined;
|
||||
inline for (fields) |field| {
|
||||
@field(result, field.name) = switch (field.type) {
|
||||
u32 => self.atoms_extra.items[i],
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
i += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn setAtomExtra(self: *MachO, index: u32, extra: Atom.Extra) void {
|
||||
assert(index > 0);
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
inline for (fields, 0..) |field, i| {
|
||||
self.atoms_extra.items[index + i] = switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn addSymbol(self: *MachO) !Symbol.Index {
|
||||
const index = @as(Symbol.Index, @intCast(self.symbols.items.len));
|
||||
const symbol = try self.symbols.addOne(self.base.comp.gpa);
|
||||
symbol.* = .{};
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbol(self: *MachO, index: Symbol.Index) *Symbol {
|
||||
assert(index < self.symbols.items.len);
|
||||
return &self.symbols.items[index];
|
||||
}
|
||||
|
||||
pub fn addSymbolExtra(self: *MachO, extra: Symbol.Extra) !u32 {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
try self.symbols_extra.ensureUnusedCapacity(self.base.comp.gpa, fields.len);
|
||||
return self.addSymbolExtraAssumeCapacity(extra);
|
||||
}
|
||||
|
||||
pub fn addSymbolExtraAssumeCapacity(self: *MachO, extra: Symbol.Extra) u32 {
|
||||
const index = @as(u32, @intCast(self.symbols_extra.items.len));
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields) |field| {
|
||||
self.symbols_extra.appendAssumeCapacity(switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
});
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbolExtra(self: MachO, index: u32) ?Symbol.Extra {
|
||||
if (index == 0) return null;
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
var i: usize = index;
|
||||
var result: Symbol.Extra = undefined;
|
||||
inline for (fields) |field| {
|
||||
@field(result, field.name) = switch (field.type) {
|
||||
u32 => self.symbols_extra.items[i],
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
i += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn setSymbolExtra(self: *MachO, index: u32, extra: Symbol.Extra) void {
|
||||
assert(index > 0);
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields, 0..) |field, i| {
|
||||
self.symbols_extra.items[index + i] = switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const GetOrCreateGlobalResult = struct {
|
||||
found_existing: bool,
|
||||
index: Symbol.Index,
|
||||
};
|
||||
|
||||
pub fn getOrCreateGlobal(self: *MachO, off: u32) !GetOrCreateGlobalResult {
|
||||
const gpa = self.base.comp.gpa;
|
||||
const gop = try self.globals.getOrPut(gpa, off);
|
||||
if (!gop.found_existing) {
|
||||
const index = try self.addSymbol();
|
||||
const global = self.getSymbol(index);
|
||||
global.name = off;
|
||||
global.flags.global = true;
|
||||
gop.value_ptr.* = index;
|
||||
}
|
||||
return .{
|
||||
.found_existing = gop.found_existing,
|
||||
.index = gop.value_ptr.*,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getGlobalByName(self: *MachO, name: []const u8) ?Symbol.Index {
|
||||
const off = self.strings.getOffset(name) orelse return null;
|
||||
return self.globals.get(off);
|
||||
}
|
||||
|
||||
pub fn addThunk(self: *MachO) !Thunk.Index {
|
||||
const index = @as(Thunk.Index, @intCast(self.thunks.items.len));
|
||||
const thunk = try self.thunks.addOne(self.base.comp.gpa);
|
||||
|
||||
@@ -7,6 +7,8 @@ id: ?Id = null,
|
||||
ordinal: u16 = 0,
|
||||
|
||||
symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
symbols_extra: std.ArrayListUnmanaged(u32) = .{},
|
||||
globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{},
|
||||
dependents: std.ArrayListUnmanaged(Id) = .{},
|
||||
rpaths: std.StringArrayHashMapUnmanaged(void) = .{},
|
||||
umbrella: File.Index = 0,
|
||||
@@ -37,6 +39,8 @@ pub fn deinit(self: *Dylib, allocator: Allocator) void {
|
||||
self.strtab.deinit(allocator);
|
||||
if (self.id) |*id| id.deinit(allocator);
|
||||
self.symbols.deinit(allocator);
|
||||
self.symbols_extra.deinit(allocator);
|
||||
self.globals.deinit(allocator);
|
||||
for (self.dependents.items) |*id| {
|
||||
id.deinit(allocator);
|
||||
}
|
||||
@@ -494,13 +498,21 @@ fn addObjCExport(
|
||||
pub fn initSymbols(self: *Dylib, macho_file: *MachO) !void {
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
|
||||
try self.symbols.ensureTotalCapacityPrecise(gpa, self.exports.items(.name).len);
|
||||
const nsyms = self.exports.items(.name).len;
|
||||
try self.symbols.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
try self.symbols_extra.ensureTotalCapacityPrecise(gpa, nsyms * @sizeOf(Symbol.Extra));
|
||||
try self.globals.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
self.globals.resize(gpa, nsyms) catch unreachable;
|
||||
@memset(self.globals.items, 0);
|
||||
|
||||
for (self.exports.items(.name)) |noff| {
|
||||
const name = self.getString(noff);
|
||||
const off = try macho_file.strings.insert(gpa, name);
|
||||
const gop = try macho_file.getOrCreateGlobal(off);
|
||||
self.symbols.addOneAssumeCapacity().* = gop.index;
|
||||
for (self.exports.items(.name), self.exports.items(.flags)) |noff, flags| {
|
||||
const index = self.addSymbolAssumeCapacity();
|
||||
const symbol = &self.symbols.items[index];
|
||||
symbol.name = noff;
|
||||
symbol.extra = self.addSymbolExtraAssumeCapacity(.{});
|
||||
symbol.flags.weak = flags.weak;
|
||||
symbol.flags.tlv = flags.tlv;
|
||||
symbol.visibility = .global;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,37 +522,31 @@ pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void {
|
||||
|
||||
if (!self.explicit and !self.hoisted) return;
|
||||
|
||||
for (self.symbols.items, self.exports.items(.flags)) |index, flags| {
|
||||
const global = macho_file.getSymbol(index);
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
|
||||
for (self.exports.items(.flags), self.globals.items, 0..) |flags, *global, i| {
|
||||
const gop = try macho_file.resolver.getOrPut(gpa, .{
|
||||
.index = @intCast(i),
|
||||
.file = self.index,
|
||||
}, macho_file);
|
||||
if (!gop.found_existing) {
|
||||
gop.ref.* = .{ .index = 0, .file = 0 };
|
||||
}
|
||||
global.* = gop.index;
|
||||
|
||||
if (gop.ref.getFile(macho_file) == null) {
|
||||
gop.ref.* = .{ .index = @intCast(i), .file = self.index };
|
||||
continue;
|
||||
}
|
||||
|
||||
if (self.asFile().getSymbolRank(.{
|
||||
.weak = flags.weak,
|
||||
}) < global.getSymbolRank(macho_file)) {
|
||||
global.value = 0;
|
||||
global.atom = 0;
|
||||
global.nlist_idx = 0;
|
||||
global.file = self.index;
|
||||
global.flags.weak = flags.weak;
|
||||
global.flags.tlv = flags.tlv;
|
||||
global.flags.dyn_ref = false;
|
||||
global.flags.tentative = false;
|
||||
global.visibility = .global;
|
||||
}) < gop.ref.getSymbol(macho_file).?.getSymbolRank(macho_file)) {
|
||||
gop.ref.* = .{ .index = @intCast(i), .file = self.index };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resetGlobals(self: *Dylib, macho_file: *MachO) void {
|
||||
for (self.symbols.items) |sym_index| {
|
||||
const sym = macho_file.getSymbol(sym_index);
|
||||
const name = sym.name;
|
||||
const global = sym.flags.global;
|
||||
const weak_ref = sym.flags.weak_ref;
|
||||
sym.* = .{};
|
||||
sym.name = name;
|
||||
sym.flags.global = global;
|
||||
sym.flags.weak_ref = weak_ref;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isAlive(self: Dylib, macho_file: *MachO) bool {
|
||||
if (!macho_file.dead_strip_dylibs) return self.explicit or self.referenced or self.needed;
|
||||
return self.referenced or self.needed;
|
||||
@@ -550,48 +556,52 @@ pub fn markReferenced(self: *Dylib, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (self.symbols.items) |global_index| {
|
||||
const global = macho_file.getSymbol(global_index);
|
||||
const file_ptr = global.getFile(macho_file) orelse continue;
|
||||
if (file_ptr.getIndex() != self.index) continue;
|
||||
for (0..self.symbols.items.len) |i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
const global = ref.getSymbol(macho_file).?;
|
||||
if (global.isLocal()) continue;
|
||||
self.referenced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) !void {
|
||||
pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (self.symbols.items) |global_index| {
|
||||
const global = macho_file.getSymbol(global_index);
|
||||
const file_ptr = global.getFile(macho_file) orelse continue;
|
||||
if (file_ptr.getIndex() != self.index) continue;
|
||||
if (global.isLocal()) continue;
|
||||
assert(global.flags.import);
|
||||
global.flags.output_symtab = true;
|
||||
try global.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
|
||||
for (self.symbols.items, 0..) |*sym, i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
if (sym.isLocal()) continue;
|
||||
assert(sym.flags.import);
|
||||
sym.flags.output_symtab = true;
|
||||
sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
|
||||
self.output_symtab_ctx.nimports += 1;
|
||||
self.output_symtab_ctx.strsize += @as(u32, @intCast(global.getName(macho_file).len + 1));
|
||||
self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeSymtab(self: Dylib, macho_file: *MachO, ctx: anytype) void {
|
||||
pub fn writeSymtab(self: Dylib, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (self.symbols.items) |global_index| {
|
||||
const global = macho_file.getSymbol(global_index);
|
||||
const file = global.getFile(macho_file) orelse continue;
|
||||
var n_strx = self.output_symtab_ctx.stroff;
|
||||
for (self.symbols.items, 0..) |sym, i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
const idx = global.getOutputSymtabIndex(macho_file) orelse continue;
|
||||
const n_strx = @as(u32, @intCast(ctx.strtab.items.len));
|
||||
ctx.strtab.appendSliceAssumeCapacity(global.getName(macho_file));
|
||||
ctx.strtab.appendAssumeCapacity(0);
|
||||
const out_sym = &ctx.symtab.items[idx];
|
||||
const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
|
||||
const out_sym = &macho_file.symtab.items[idx];
|
||||
out_sym.n_strx = n_strx;
|
||||
global.setOutputSym(macho_file, out_sym);
|
||||
sym.setOutputSym(macho_file, out_sym);
|
||||
const name = sym.getName(macho_file);
|
||||
@memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
|
||||
n_strx += @intCast(name.len);
|
||||
macho_file.strtab.items[n_strx] = 0;
|
||||
n_strx += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,7 +615,7 @@ fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !u32 {
|
||||
return off;
|
||||
}
|
||||
|
||||
pub inline fn getString(self: Dylib, off: u32) [:0]const u8 {
|
||||
pub fn getString(self: Dylib, off: u32) [:0]const u8 {
|
||||
assert(off < self.strtab.items.len);
|
||||
return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
|
||||
}
|
||||
@@ -614,6 +624,66 @@ pub fn asFile(self: *Dylib) File {
|
||||
return .{ .dylib = self };
|
||||
}
|
||||
|
||||
fn addSymbol(self: *Dylib, allocator: Allocator) !Symbol.Index {
|
||||
try self.symbols.ensureUnusedCapacity(allocator, 1);
|
||||
return self.addSymbolAssumeCapacity();
|
||||
}
|
||||
|
||||
fn addSymbolAssumeCapacity(self: *Dylib) Symbol.Index {
|
||||
const index: Symbol.Index = @intCast(self.symbols.items.len);
|
||||
const symbol = self.symbols.addOneAssumeCapacity();
|
||||
symbol.* = .{ .file = self.index };
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbolRef(self: Dylib, index: Symbol.Index, macho_file: *MachO) MachO.Ref {
|
||||
const global_index = self.globals.items[index];
|
||||
if (macho_file.resolver.get(global_index)) |ref| return ref;
|
||||
return .{ .index = index, .file = self.index };
|
||||
}
|
||||
|
||||
pub fn addSymbolExtra(self: *Dylib, allocator: Allocator, extra: Symbol.Extra) !u32 {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
try self.symbols_extra.ensureUnusedCapacity(allocator, fields.len);
|
||||
return self.addSymbolExtraAssumeCapacity(extra);
|
||||
}
|
||||
|
||||
fn addSymbolExtraAssumeCapacity(self: *Dylib, extra: Symbol.Extra) u32 {
|
||||
const index = @as(u32, @intCast(self.symbols_extra.items.len));
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields) |field| {
|
||||
self.symbols_extra.appendAssumeCapacity(switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
});
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbolExtra(self: Dylib, index: u32) Symbol.Extra {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
var i: usize = index;
|
||||
var result: Symbol.Extra = undefined;
|
||||
inline for (fields) |field| {
|
||||
@field(result, field.name) = switch (field.type) {
|
||||
u32 => self.symbols_extra.items[i],
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
i += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn setSymbolExtra(self: *Dylib, index: u32, extra: Symbol.Extra) void {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields, 0..) |field, i| {
|
||||
self.symbols_extra.items[index + i] = switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: *Dylib,
|
||||
comptime unused_fmt_string: []const u8,
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
index: File.Index,
|
||||
|
||||
sections: std.MultiArrayList(Section) = .{},
|
||||
atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
|
||||
symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
atoms: std.ArrayListUnmanaged(Atom) = .{},
|
||||
atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{},
|
||||
atoms_extra: std.ArrayListUnmanaged(u32) = .{},
|
||||
symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{},
|
||||
strtab: std.ArrayListUnmanaged(u8) = .{},
|
||||
symbols: std.ArrayListUnmanaged(Symbol) = .{},
|
||||
symbols_extra: std.ArrayListUnmanaged(u32) = .{},
|
||||
globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{},
|
||||
|
||||
objc_methnames: std.ArrayListUnmanaged(u8) = .{},
|
||||
objc_selrefs: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64),
|
||||
|
||||
force_undefined: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
entry_index: ?Symbol.Index = null,
|
||||
dyld_stub_binder_index: ?Symbol.Index = null,
|
||||
dyld_private: ?Symbol.Index = null,
|
||||
objc_msg_send_index: ?Symbol.Index = null,
|
||||
mh_execute_header_index: ?Symbol.Index = null,
|
||||
mh_dylib_header_index: ?Symbol.Index = null,
|
||||
dso_handle_index: ?Symbol.Index = null,
|
||||
boundary_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
|
||||
output_symtab_ctx: MachO.SymtabCtx = .{},
|
||||
|
||||
pub fn deinit(self: *InternalObject, allocator: Allocator) void {
|
||||
@@ -15,39 +31,224 @@ pub fn deinit(self: *InternalObject, allocator: Allocator) void {
|
||||
}
|
||||
self.sections.deinit(allocator);
|
||||
self.atoms.deinit(allocator);
|
||||
self.atoms_indexes.deinit(allocator);
|
||||
self.atoms_extra.deinit(allocator);
|
||||
self.symtab.deinit(allocator);
|
||||
self.strtab.deinit(allocator);
|
||||
self.symbols.deinit(allocator);
|
||||
self.symbols_extra.deinit(allocator);
|
||||
self.globals.deinit(allocator);
|
||||
self.objc_methnames.deinit(allocator);
|
||||
self.force_undefined.deinit(allocator);
|
||||
self.boundary_symbols.deinit(allocator);
|
||||
}
|
||||
|
||||
pub fn addSymbol(self: *InternalObject, name: [:0]const u8, macho_file: *MachO) !Symbol.Index {
|
||||
pub fn init(self: *InternalObject, allocator: Allocator) !void {
|
||||
// Atom at index 0 is reserved as null atom.
|
||||
try self.atoms.append(allocator, .{});
|
||||
try self.atoms_extra.append(allocator, 0);
|
||||
// Null byte in strtab
|
||||
try self.strtab.append(allocator, 0);
|
||||
}
|
||||
|
||||
pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void {
|
||||
const createSymbol = struct {
|
||||
fn createSymbol(obj: *InternalObject, name: u32, args: struct {
|
||||
type: u8 = macho.N_UNDF | macho.N_EXT,
|
||||
desc: u16 = 0,
|
||||
}) Symbol.Index {
|
||||
const index = obj.addSymbolAssumeCapacity();
|
||||
const symbol = &obj.symbols.items[index];
|
||||
symbol.name = name;
|
||||
symbol.extra = obj.addSymbolExtraAssumeCapacity(.{});
|
||||
symbol.flags.dyn_ref = args.desc & macho.REFERENCED_DYNAMICALLY != 0;
|
||||
symbol.visibility = if (args.type & macho.N_EXT != 0) blk: {
|
||||
break :blk if (args.type & macho.N_PEXT != 0) .hidden else .global;
|
||||
} else .local;
|
||||
|
||||
const nlist_idx: u32 = @intCast(obj.symtab.items.len);
|
||||
const nlist = obj.symtab.addOneAssumeCapacity();
|
||||
nlist.* = .{
|
||||
.n_strx = name,
|
||||
.n_type = args.type,
|
||||
.n_sect = 0,
|
||||
.n_desc = args.desc,
|
||||
.n_value = 0,
|
||||
};
|
||||
symbol.nlist_idx = nlist_idx;
|
||||
return index;
|
||||
}
|
||||
}.createSymbol;
|
||||
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
try self.symbols.ensureUnusedCapacity(gpa, 1);
|
||||
const off = try macho_file.strings.insert(gpa, name);
|
||||
const gop = try macho_file.getOrCreateGlobal(off);
|
||||
self.symbols.addOneAssumeCapacity().* = gop.index;
|
||||
const sym = macho_file.getSymbol(gop.index);
|
||||
sym.file = self.index;
|
||||
sym.value = 0;
|
||||
sym.atom = 0;
|
||||
sym.nlist_idx = 0;
|
||||
sym.flags = .{ .global = true };
|
||||
return gop.index;
|
||||
var nsyms = macho_file.base.comp.force_undefined_symbols.keys().len;
|
||||
nsyms += 1; // dyld_stub_binder
|
||||
nsyms += 1; // _objc_msgSend
|
||||
if (!macho_file.base.isDynLib()) {
|
||||
nsyms += 1; // entry
|
||||
nsyms += 1; // __mh_execute_header
|
||||
} else {
|
||||
nsyms += 1; // __mh_dylib_header
|
||||
}
|
||||
nsyms += 1; // ___dso_handle
|
||||
nsyms += 1; // dyld_private
|
||||
|
||||
try self.symbols.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
try self.symbols_extra.ensureTotalCapacityPrecise(gpa, nsyms * @sizeOf(Symbol.Extra));
|
||||
try self.symtab.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
try self.globals.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
self.globals.resize(gpa, nsyms) catch unreachable;
|
||||
@memset(self.globals.items, 0);
|
||||
|
||||
try self.force_undefined.ensureTotalCapacityPrecise(gpa, macho_file.base.comp.force_undefined_symbols.keys().len);
|
||||
for (macho_file.base.comp.force_undefined_symbols.keys()) |name| {
|
||||
self.force_undefined.addOneAssumeCapacity().* = createSymbol(self, try self.addString(gpa, name), .{});
|
||||
}
|
||||
|
||||
self.dyld_stub_binder_index = createSymbol(self, try self.addString(gpa, "dyld_stub_binder"), .{});
|
||||
self.objc_msg_send_index = createSymbol(self, try self.addString(gpa, "_objc_msgSend"), .{});
|
||||
|
||||
if (!macho_file.base.isDynLib()) {
|
||||
self.entry_index = createSymbol(self, try self.addString(gpa, macho_file.entry_name orelse "_main"), .{});
|
||||
self.mh_execute_header_index = createSymbol(self, try self.addString(gpa, "__mh_execute_header"), .{
|
||||
.type = macho.N_SECT | macho.N_EXT,
|
||||
.desc = macho.REFERENCED_DYNAMICALLY,
|
||||
});
|
||||
} else {
|
||||
self.mh_dylib_header_index = createSymbol(self, try self.addString(gpa, "__mh_dylib_header"), .{
|
||||
.type = macho.N_SECT | macho.N_EXT,
|
||||
});
|
||||
}
|
||||
|
||||
self.dso_handle_index = createSymbol(self, try self.addString(gpa, "___dso_handle"), .{
|
||||
.type = macho.N_SECT | macho.N_EXT,
|
||||
});
|
||||
self.dyld_private_index = createSymbol(self, try self.addString(gpa, "dyld_private"), .{
|
||||
.type = macho.N_SECT,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn resolveSymbols(self: *InternalObject, macho_file: *MachO) !void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
|
||||
for (self.symtab.items, self.globals.items, 0..) |nlist, *global, i| {
|
||||
const gop = try macho_file.resolver.getOrPut(gpa, .{
|
||||
.index = @intCast(i),
|
||||
.file = self.index,
|
||||
}, macho_file);
|
||||
if (!gop.found_existing) {
|
||||
gop.ref.* = .{ .index = 0, .file = 0 };
|
||||
}
|
||||
global.* = gop.index;
|
||||
|
||||
if (nlist.undf()) continue;
|
||||
if (gop.ref.getFile(macho_file) == null) {
|
||||
gop.ref.* = .{ .index = @intCast(i), .file = self.index };
|
||||
continue;
|
||||
}
|
||||
|
||||
if (self.asFile().getSymbolRank(.{
|
||||
.archive = false,
|
||||
.weak = false,
|
||||
.tentative = false,
|
||||
}) < gop.ref.getSymbol(macho_file).?.getSymbolRank(macho_file)) {
|
||||
gop.ref.* = .{ .index = @intCast(i), .file = self.index };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
var boundary_symbols = std.StringArrayHashMap(MachO.Ref).init(gpa);
|
||||
defer boundary_symbols.deinit();
|
||||
|
||||
for (macho_file.objects.items) |index| {
|
||||
const object = macho_file.getFile(index).?.object;
|
||||
for (object.symbols.items, 0..) |sym, i| {
|
||||
const nlist = object.symtab.items(.nlist)[i];
|
||||
if (!nlist.undf() or !nlist.ext()) continue;
|
||||
const ref = object.getSymbolRef(@intCast(i), macho_file);
|
||||
if (ref.getFile(macho_file) != null) continue;
|
||||
const name = sym.getName(macho_file);
|
||||
if (mem.startsWith(u8, name, "segment$start$") or
|
||||
mem.startsWith(u8, name, "segment$stop$") or
|
||||
mem.startsWith(u8, name, "section$start$") or
|
||||
mem.startsWith(u8, name, "section$stop$"))
|
||||
{
|
||||
const gop = try boundary_symbols.getOrPut(name);
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{ .index = @intCast(i), .file = index };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const nsyms = boundary_symbols.values().len;
|
||||
try self.boundary_symbols.ensureTotalCapacityPrecise(gpa, nsyms);
|
||||
try self.symbols.ensureUnusedCapacity(gpa, nsyms);
|
||||
try self.symtab.ensureUnusedCapacity(gpa, nsyms);
|
||||
try self.symbols_extra.ensureUnusedCapacity(gpa, nsyms * @sizeOf(Symbol.Extra));
|
||||
try self.globals.ensureUnusedCapacity(gpa, nsyms);
|
||||
|
||||
for (boundary_symbols.keys(), boundary_symbols.values()) |name, ref| {
|
||||
const name_off = try self.addString(gpa, name);
|
||||
const sym_index = self.addSymbolAssumeCapacity();
|
||||
self.boundary_symbols.appendAssumeCapacity(sym_index);
|
||||
const sym = &self.symbols.items[sym_index];
|
||||
sym.name = name_off;
|
||||
sym.visibility = .local;
|
||||
const nlist_idx: u32 = @intCast(self.symtab.items.len);
|
||||
const nlist = self.symtab.addOneAssumeCapacity();
|
||||
nlist.* = .{
|
||||
.n_strx = name_off.pos,
|
||||
.n_type = macho.N_SECT,
|
||||
.n_sect = 0,
|
||||
.n_desc = 0,
|
||||
.n_value = 0,
|
||||
};
|
||||
sym.nlist_idx = nlist_idx;
|
||||
sym.extra = self.addSymbolExtraAssumeCapacity(.{});
|
||||
|
||||
const idx = ref.getFile(macho_file).?.object.globals.items[ref.index];
|
||||
self.globals.addOneAssumeCapacity().* = idx;
|
||||
macho_file.resolver.values.items[idx - 1] = .{ .index = sym_index, .file = self.index };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn markLive(self: *InternalObject, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (0..self.symbols.items.len) |i| {
|
||||
const nlist = self.symtab.items[i];
|
||||
if (!nlist.ext()) continue;
|
||||
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file == .object and !file.object.alive) {
|
||||
file.object.alive = true;
|
||||
file.object.markLive(macho_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a fake input sections __TEXT,__objc_methname and __DATA,__objc_selrefs.
|
||||
pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho_file: *MachO) !Atom.Index {
|
||||
const methname_atom_index = try self.addObjcMethnameSection(sym_name, macho_file);
|
||||
return try self.addObjcSelrefsSection(methname_atom_index, macho_file);
|
||||
pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho_file: *MachO) !Symbol.Index {
|
||||
const methname_sym_index = try self.addObjcMethnameSection(sym_name, macho_file);
|
||||
return try self.addObjcSelrefsSection(methname_sym_index, macho_file);
|
||||
}
|
||||
|
||||
fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_file: *MachO) !Atom.Index {
|
||||
fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_file: *MachO) !Symbol.Index {
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
const atom_index = try macho_file.addAtom();
|
||||
try self.atoms.append(gpa, atom_index);
|
||||
|
||||
const atom = macho_file.getAtom(atom_index).?;
|
||||
atom.atom_index = atom_index;
|
||||
atom.file = self.index;
|
||||
const atom_index = try self.addAtom(gpa);
|
||||
try self.atoms_indexes.append(gpa, atom_index);
|
||||
const atom = self.getAtom(atom_index).?;
|
||||
atom.size = methname.len + 1;
|
||||
atom.alignment = .@"1";
|
||||
|
||||
@@ -63,19 +264,34 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
|
||||
try self.objc_methnames.ensureUnusedCapacity(gpa, methname.len + 1);
|
||||
self.objc_methnames.writer(gpa).print("{s}\x00", .{methname}) catch unreachable;
|
||||
|
||||
const name_str = try self.addString(gpa, "ltmp");
|
||||
const sym_index = try self.addSymbol(gpa);
|
||||
const sym = &self.symbols.items[sym_index];
|
||||
sym.name = name_str;
|
||||
sym.atom_ref = .{ .index = atom_index, .file = self.index };
|
||||
sym.extra = try self.addSymbolExtra(gpa, .{});
|
||||
const nlist_idx: u32 = @intCast(self.symtab.items.len);
|
||||
const nlist = try self.symtab.addOne(gpa);
|
||||
nlist.* = .{
|
||||
.n_strx = name_str.pos,
|
||||
.n_type = macho.N_SECT,
|
||||
.n_sect = @intCast(n_sect + 1),
|
||||
.n_desc = 0,
|
||||
.n_value = 0,
|
||||
};
|
||||
sym.nlist_idx = nlist_idx;
|
||||
try self.globals.append(gpa, 0);
|
||||
|
||||
return atom_index;
|
||||
}
|
||||
|
||||
fn addObjcSelrefsSection(self: *InternalObject, methname_atom_index: Atom.Index, macho_file: *MachO) !Atom.Index {
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
const atom_index = try macho_file.addAtom();
|
||||
try self.atoms.append(gpa, atom_index);
|
||||
|
||||
const atom = macho_file.getAtom(atom_index).?;
|
||||
atom.atom_index = atom_index;
|
||||
atom.file = self.index;
|
||||
fn addObjcSelrefsSection(self: *InternalObject, methname_sym_index: Symbol.Index, macho_file: *MachO) !Symbol.Index {
|
||||
const gpa = macho_file.base.allocator;
|
||||
const atom_index = try self.addAtom(gpa);
|
||||
try self.atoms_indexes.append(gpa, atom_index);
|
||||
const atom = self.getAtom(atom_index).?;
|
||||
atom.size = @sizeOf(u64);
|
||||
atom.alignment = .@"8";
|
||||
atom.alignment = 3;
|
||||
|
||||
const n_sect = try self.addSection(gpa, "__DATA", "__objc_selrefs");
|
||||
const sect = &self.sections.items(.header)[n_sect];
|
||||
@@ -89,9 +305,9 @@ fn addObjcSelrefsSection(self: *InternalObject, methname_atom_index: Atom.Index,
|
||||
const relocs = &self.sections.items(.relocs)[n_sect];
|
||||
try relocs.ensureUnusedCapacity(gpa, 1);
|
||||
relocs.appendAssumeCapacity(.{
|
||||
.tag = .local,
|
||||
.tag = .@"extern",
|
||||
.offset = 0,
|
||||
.target = methname_atom_index,
|
||||
.target = methname_sym_index,
|
||||
.addend = 0,
|
||||
.type = .unsigned,
|
||||
.meta = .{
|
||||
@@ -101,139 +317,283 @@ fn addObjcSelrefsSection(self: *InternalObject, methname_atom_index: Atom.Index,
|
||||
.has_subtractor = false,
|
||||
},
|
||||
});
|
||||
try atom.addExtra(.{ .rel_index = 0, .rel_count = 1 }, macho_file);
|
||||
atom.flags.relocs = true;
|
||||
atom.addExtra(.{ .rel_index = 0, .rel_count = 1 }, macho_file);
|
||||
|
||||
return atom_index;
|
||||
const sym_index = try self.addSymbol(gpa);
|
||||
const sym = &self.symbols.items[sym_index];
|
||||
sym.atom_ref = .{ .index = atom_index, .file = self.index };
|
||||
sym.extra = try self.addSymbolExtra(gpa, .{});
|
||||
const nlist_idx: u32 = @intCast(self.symtab.items.len);
|
||||
const nlist = try self.symtab.addOne(gpa);
|
||||
nlist.* = .{
|
||||
.n_strx = 0,
|
||||
.n_type = macho.N_SECT,
|
||||
.n_sect = @intCast(n_sect + 1),
|
||||
.n_desc = 0,
|
||||
.n_value = 0,
|
||||
};
|
||||
sym.nlist_idx = nlist_idx;
|
||||
try self.globals.append(gpa, 0);
|
||||
atom.addExtra(.{ .literal_symbol_index = sym_index }, macho_file);
|
||||
|
||||
return sym_index;
|
||||
}
|
||||
|
||||
pub fn resolveLiterals(self: InternalObject, lp: *MachO.LiteralPool, macho_file: *MachO) !void {
|
||||
pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
|
||||
var objc_msgsend_syms = std.StringArrayHashMap(MachO.Ref).init(gpa);
|
||||
defer objc_msgsend_syms.deinit();
|
||||
|
||||
for (macho_file.objects.items) |index| {
|
||||
const object = macho_file.getFile(index).?.object;
|
||||
|
||||
for (object.symbols.items, 0..) |sym, i| {
|
||||
const nlist = object.symtab.items(.nlist)[i];
|
||||
if (!nlist.ext()) continue;
|
||||
if (!nlist.undf()) continue;
|
||||
|
||||
const ref = object.getSymbolRef(@intCast(i), macho_file);
|
||||
if (ref.getFile(macho_file) != null) continue;
|
||||
|
||||
const name = sym.getName(macho_file);
|
||||
if (mem.startsWith(u8, name, "_objc_msgSend$")) {
|
||||
const gop = try objc_msgsend_syms.getOrPut(name);
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{ .index = @intCast(i), .file = index };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (objc_msgsend_syms.keys(), objc_msgsend_syms.values()) |sym_name, ref| {
|
||||
const name = MachO.eatPrefix(sym_name, "_objc_msgSend$").?;
|
||||
const selrefs_index = try self.addObjcMsgsendSections(name, macho_file);
|
||||
|
||||
const name_off = try self.addString(gpa, sym_name);
|
||||
const sym_index = try self.addSymbol(gpa);
|
||||
const sym = &self.symbols.items[sym_index];
|
||||
sym.name = name_off;
|
||||
sym.visibility = .hidden;
|
||||
const nlist_idx: u32 = @intCast(self.symtab.items.len);
|
||||
const nlist = try self.symtab.addOne(gpa);
|
||||
nlist.* = .{
|
||||
.n_strx = name_off,
|
||||
.n_type = macho.N_SECT | macho.N_EXT | macho.N_PEXT,
|
||||
.n_sect = 0,
|
||||
.n_desc = 0,
|
||||
.n_value = 0,
|
||||
};
|
||||
sym.nlist_idx = nlist_idx;
|
||||
sym.extra = try self.addSymbolExtra(gpa, .{ .objc_selrefs = selrefs_index });
|
||||
sym.setSectionFlags(.{ .objc_stubs = true });
|
||||
|
||||
const idx = ref.getFile(macho_file).?.object.globals.items[ref.index];
|
||||
try self.globals.append(gpa, idx);
|
||||
macho_file.resolver.values.items[idx - 1] = .{ .index = sym_index, .file = self.index };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file: *MachO) !void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const gpa = macho_file.base.comp.gpa;
|
||||
|
||||
var buffer = std.ArrayList(u8).init(gpa);
|
||||
defer buffer.deinit();
|
||||
|
||||
const slice = self.sections.slice();
|
||||
for (slice.items(.header), self.atoms.items, 0..) |header, atom_index, n_sect| {
|
||||
if (Object.isCstringLiteral(header) or Object.isFixedSizeLiteral(header)) {
|
||||
const data = try self.getSectionData(@intCast(n_sect));
|
||||
const atom = macho_file.getAtom(atom_index).?;
|
||||
const res = try lp.insert(gpa, header.type(), data);
|
||||
if (!res.found_existing) {
|
||||
res.atom.* = atom_index;
|
||||
}
|
||||
atom.flags.literal_pool = true;
|
||||
try atom.addExtra(.{ .literal_index = res.index }, macho_file);
|
||||
} else if (Object.isPtrLiteral(header)) {
|
||||
const atom = macho_file.getAtom(atom_index).?;
|
||||
const relocs = atom.getRelocs(macho_file);
|
||||
assert(relocs.len == 1);
|
||||
const rel = relocs[0];
|
||||
assert(rel.tag == .local);
|
||||
const target = macho_file.getAtom(rel.target).?;
|
||||
const addend = std.math.cast(u32, rel.addend) orelse return error.Overflow;
|
||||
const target_size = std.math.cast(usize, target.size) orelse return error.Overflow;
|
||||
try buffer.ensureUnusedCapacity(target_size);
|
||||
buffer.resize(target_size) catch unreachable;
|
||||
try target.getData(macho_file, buffer.items);
|
||||
const res = try lp.insert(gpa, header.type(), buffer.items[addend..]);
|
||||
buffer.clearRetainingCapacity();
|
||||
if (!res.found_existing) {
|
||||
res.atom.* = atom_index;
|
||||
}
|
||||
atom.flags.literal_pool = true;
|
||||
try atom.addExtra(.{ .literal_index = res.index }, macho_file);
|
||||
for (slice.items(.header), self.getAtoms()) |header, atom_index| {
|
||||
if (!Object.isPtrLiteral(header)) continue;
|
||||
const atom = self.getAtom(atom_index).?;
|
||||
const relocs = atom.getRelocs(macho_file);
|
||||
assert(relocs.len == 1);
|
||||
const rel = relocs[0];
|
||||
assert(rel.tag == .@"extern");
|
||||
const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;
|
||||
try buffer.ensureUnusedCapacity(target.size);
|
||||
buffer.resize(target.size) catch unreachable;
|
||||
@memcpy(buffer.items, self.getSectionData(target.n_sect));
|
||||
const res = try lp.insert(gpa, header.type(), buffer.items);
|
||||
buffer.clearRetainingCapacity();
|
||||
if (!res.found_existing) {
|
||||
res.ref.* = .{ .index = atom.getExtra(macho_file).literal_symbol_index, .file = self.index };
|
||||
} else {
|
||||
const lp_sym = lp.getSymbol(res.index, macho_file);
|
||||
const lp_atom = lp_sym.getAtom(macho_file).?;
|
||||
lp_atom.alignment = @max(lp_atom.alignment, atom.alignment);
|
||||
atom.flags.alive = false;
|
||||
}
|
||||
atom.addExtra(.{ .literal_pool_index = res.index }, macho_file);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dedupLiterals(self: InternalObject, lp: MachO.LiteralPool, macho_file: *MachO) void {
|
||||
for (self.atoms.items) |atom_index| {
|
||||
const atom = macho_file.getAtom(atom_index) orelse continue;
|
||||
if (!atom.flags.alive) continue;
|
||||
if (!atom.flags.relocs) continue;
|
||||
pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (self.getAtoms()) |atom_index| {
|
||||
const atom = self.getAtom(atom_index) orelse continue;
|
||||
if (!atom.alive.load(.seq_cst)) continue;
|
||||
|
||||
const relocs = blk: {
|
||||
const extra = atom.getExtra(macho_file).?;
|
||||
const extra = atom.getExtra(macho_file);
|
||||
const relocs = self.sections.items(.relocs)[atom.n_sect].items;
|
||||
break :blk relocs[extra.rel_index..][0..extra.rel_count];
|
||||
};
|
||||
for (relocs) |*rel| switch (rel.tag) {
|
||||
.local => {
|
||||
const target = macho_file.getAtom(rel.target).?;
|
||||
if (target.getLiteralPoolIndex(macho_file)) |lp_index| {
|
||||
const lp_atom = lp.getAtom(lp_index, macho_file);
|
||||
if (target.atom_index != lp_atom.atom_index) {
|
||||
lp_atom.alignment = lp_atom.alignment.max(target.alignment);
|
||||
target.flags.alive = false;
|
||||
rel.target = lp_atom.atom_index;
|
||||
}
|
||||
}
|
||||
},
|
||||
.@"extern" => {
|
||||
const target_sym = rel.getTargetSymbol(macho_file);
|
||||
if (target_sym.getAtom(macho_file)) |target_atom| {
|
||||
if (target_atom.getLiteralPoolIndex(macho_file)) |lp_index| {
|
||||
const lp_atom = lp.getAtom(lp_index, macho_file);
|
||||
if (target_atom.atom_index != lp_atom.atom_index) {
|
||||
lp_atom.alignment = lp_atom.alignment.max(target_atom.alignment);
|
||||
target_atom.flags.alive = false;
|
||||
target_sym.atom = lp_atom.atom_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
for (relocs) |*rel| {
|
||||
if (rel.tag != .@"extern") continue;
|
||||
const target_sym_ref = rel.getTargetSymbolRef(atom.*, macho_file);
|
||||
const file = target_sym_ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
const target_sym = target_sym_ref.getSymbol(macho_file).?;
|
||||
const target_atom = target_sym.getAtom(macho_file) orelse continue;
|
||||
if (!Object.isPtrLiteral(target_atom.getInputSection(macho_file))) continue;
|
||||
const lp_index = target_atom.getExtra(macho_file).literal_pool_index;
|
||||
const lp_sym = lp.getSymbol(lp_index, macho_file);
|
||||
const lp_atom_ref = lp_sym.atom_ref;
|
||||
if (target_atom.atom_index != lp_atom_ref.index or target_atom.file != lp_atom_ref.file) {
|
||||
target_sym.atom_ref = lp_atom_ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (self.symbols.items) |sym_index| {
|
||||
const sym = macho_file.getSymbol(sym_index);
|
||||
if (!sym.flags.objc_stubs) continue;
|
||||
var extra = sym.getExtra(macho_file).?;
|
||||
const atom = macho_file.getAtom(extra.objc_selrefs).?;
|
||||
if (atom.getLiteralPoolIndex(macho_file)) |lp_index| {
|
||||
const lp_atom = lp.getAtom(lp_index, macho_file);
|
||||
if (atom.atom_index != lp_atom.atom_index) {
|
||||
lp_atom.alignment = lp_atom.alignment.max(atom.alignment);
|
||||
atom.flags.alive = false;
|
||||
extra.objc_selrefs = lp_atom.atom_index;
|
||||
sym.setExtra(extra, macho_file);
|
||||
for (self.symbols.items) |*sym| {
|
||||
if (!sym.getSectionFlags().objc_stubs) continue;
|
||||
const extra = sym.getExtra(macho_file);
|
||||
const file = sym.getFile(macho_file).?;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
const tsym = switch (file) {
|
||||
.dylib => unreachable,
|
||||
inline else => |x| &x.symbols.items[extra.objc_selrefs],
|
||||
};
|
||||
const atom = tsym.getAtom(macho_file) orelse continue;
|
||||
if (!Object.isPtrLiteral(atom.getInputSection(macho_file))) continue;
|
||||
const lp_index = atom.getExtra(macho_file).literal_pool_index;
|
||||
const lp_sym = lp.getSymbol(lp_index, macho_file);
|
||||
const lp_atom_ref = lp_sym.atom_ref;
|
||||
if (atom.atom_index != lp_atom_ref.index or atom.file != lp_atom_ref.file) {
|
||||
tsym.atom_ref = lp_atom_ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
if (self.getEntryRef(macho_file)) |ref| {
|
||||
if (ref.getFile(macho_file) != null) {
|
||||
const sym = ref.getSymbol(macho_file).?;
|
||||
if (sym.flags.import) sym.flags.stubs = true;
|
||||
}
|
||||
}
|
||||
if (self.getDyldStubBinderRef(macho_file)) |ref| {
|
||||
if (ref.getFile(macho_file) != null) {
|
||||
const sym = ref.getSymbol(macho_file).?;
|
||||
sym.flags.got = true;
|
||||
}
|
||||
}
|
||||
if (self.getObjcMsgSendRef(macho_file)) |ref| {
|
||||
if (ref.getFile(macho_file) != null) {
|
||||
const sym = ref.getSymbol(macho_file).?;
|
||||
// TODO is it always needed, or only if we are synthesising fast stubs
|
||||
sym.flags.got = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocateSyntheticSymbols(self: *InternalObject, macho_file: *MachO) void {
|
||||
const text_seg = macho_file.getTextSegment();
|
||||
|
||||
if (self.mh_execute_header_index) |index| {
|
||||
const ref = self.getSymbolRef(index, macho_file);
|
||||
if (ref.getFile(macho_file)) |file| {
|
||||
if (file.getIndex() == self.index) {
|
||||
const sym = &self.symbols.items[index];
|
||||
sym.value = text_seg.vmaddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (macho_file.data_sect_index) |idx| {
|
||||
const sect = macho_file.sections.items(.header)[idx];
|
||||
for (&[_]?Symbol.Index{
|
||||
self.dso_handle_index,
|
||||
self.mh_dylib_header_index,
|
||||
self.dyld_private_index,
|
||||
}) |maybe_index| {
|
||||
if (maybe_index) |index| {
|
||||
const ref = self.getSymbolRef(index, macho_file);
|
||||
if (ref.getFile(macho_file)) |file| {
|
||||
if (file.getIndex() == self.index) {
|
||||
const sym = &self.symbols.items[index];
|
||||
sym.value = sect.addr;
|
||||
sym.out_n_sect = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calcSymtabSize(self: *InternalObject, macho_file: *MachO) !void {
|
||||
for (self.symbols.items) |sym_index| {
|
||||
const sym = macho_file.getSymbol(sym_index);
|
||||
if (sym.getFile(macho_file)) |file| if (file.getIndex() != self.index) continue;
|
||||
pub fn calcSymtabSize(self: *InternalObject, macho_file: *MachO) void {
|
||||
for (self.symbols.items, 0..) |*sym, i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
if (sym.getName(macho_file).len == 0) continue;
|
||||
sym.flags.output_symtab = true;
|
||||
if (sym.isLocal()) {
|
||||
try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
|
||||
sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
|
||||
self.output_symtab_ctx.nlocals += 1;
|
||||
} else if (sym.flags.@"export") {
|
||||
try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file);
|
||||
sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file);
|
||||
self.output_symtab_ctx.nexports += 1;
|
||||
} else {
|
||||
assert(sym.flags.import);
|
||||
try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
|
||||
sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
|
||||
self.output_symtab_ctx.nimports += 1;
|
||||
}
|
||||
self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeSymtab(self: InternalObject, macho_file: *MachO, ctx: anytype) void {
|
||||
for (self.symbols.items) |sym_index| {
|
||||
const sym = macho_file.getSymbol(sym_index);
|
||||
if (sym.getFile(macho_file)) |file| if (file.getIndex() != self.index) continue;
|
||||
pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
for (self.getAtoms()) |atom_index| {
|
||||
const atom = self.getAtom(atom_index) orelse continue;
|
||||
if (!atom.alive.load(.seq_cst)) continue;
|
||||
const sect = atom.getInputSection(macho_file);
|
||||
if (sect.isZerofill()) continue;
|
||||
const off = atom.value;
|
||||
const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size];
|
||||
@memcpy(buffer, self.getSectionData(atom.n_sect));
|
||||
try atom.resolveRelocs(macho_file, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeSymtab(self: InternalObject, macho_file: *MachO) void {
|
||||
var n_strx = self.output_symtab_ctx.stroff;
|
||||
for (self.symbols.items, 0..) |sym, i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
const file = ref.getFile(macho_file) orelse continue;
|
||||
if (file.getIndex() != self.index) continue;
|
||||
const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
|
||||
const n_strx = @as(u32, @intCast(ctx.strtab.items.len));
|
||||
ctx.strtab.appendSliceAssumeCapacity(sym.getName(macho_file));
|
||||
ctx.strtab.appendAssumeCapacity(0);
|
||||
const out_sym = &ctx.symtab.items[idx];
|
||||
const out_sym = &macho_file.symtab.items[idx];
|
||||
out_sym.n_strx = n_strx;
|
||||
sym.setOutputSym(macho_file, out_sym);
|
||||
const name = sym.getName(macho_file);
|
||||
@memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
|
||||
n_strx += @intCast(name.len);
|
||||
macho_file.strtab.items[n_strx] = 0;
|
||||
n_strx += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,32 +622,167 @@ fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]con
|
||||
@panic("ref to non-existent section");
|
||||
}
|
||||
|
||||
pub fn getAtomData(self: *const InternalObject, atom: Atom, buffer: []u8) error{Overflow}!void {
|
||||
assert(buffer.len == atom.size);
|
||||
const data = try self.getSectionData(atom.n_sect);
|
||||
const off = std.math.cast(usize, atom.off) orelse return error.Overflow;
|
||||
const size = std.math.cast(usize, atom.size) orelse return error.Overflow;
|
||||
@memcpy(buffer, data[off..][0..size]);
|
||||
}
|
||||
|
||||
pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO) []const Relocation {
|
||||
if (!atom.flags.relocs) return &[0]Relocation{};
|
||||
const extra = atom.getExtra(macho_file).?;
|
||||
const relocs = self.sections.items(.relocs)[atom.n_sect];
|
||||
return relocs.items[extra.rel_index..][0..extra.rel_count];
|
||||
pub fn addString(self: *InternalObject, allocator: Allocator, name: []const u8) !u32 {
|
||||
const off: u32 = @intCast(self.strtab.items.len);
|
||||
try self.strtab.ensureUnusedCapacity(allocator, name.len + 1);
|
||||
self.strtab.appendSliceAssumeCapacity(name);
|
||||
self.strtab.appendAssumeCapacity(0);
|
||||
return off;
|
||||
}
|
||||
|
||||
pub fn getString(self: InternalObject, off: u32) [:0]const u8 {
|
||||
_ = self;
|
||||
_ = off;
|
||||
// We don't have any local strings for synthetic atoms.
|
||||
return "";
|
||||
assert(off < self.strtab.items.len);
|
||||
return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
|
||||
}
|
||||
|
||||
pub fn asFile(self: *InternalObject) File {
|
||||
return .{ .internal = self };
|
||||
}
|
||||
|
||||
fn addAtom(self: *InternalObject, allocator: Allocator) !Atom.Index {
|
||||
const atom_index: Atom.Index = @intCast(self.atoms.items.len);
|
||||
const atom = try self.atoms.addOne(allocator);
|
||||
atom.* = .{
|
||||
.file = self.index,
|
||||
.atom_index = atom_index,
|
||||
.extra = try self.addAtomExtra(allocator, .{}),
|
||||
};
|
||||
return atom_index;
|
||||
}
|
||||
|
||||
pub fn getAtom(self: *InternalObject, atom_index: Atom.Index) ?*Atom {
|
||||
if (atom_index == 0) return null;
|
||||
assert(atom_index < self.atoms.items.len);
|
||||
return &self.atoms.items[atom_index];
|
||||
}
|
||||
|
||||
pub fn getAtoms(self: InternalObject) []const Atom.Index {
|
||||
return self.atoms_indexes.items;
|
||||
}
|
||||
|
||||
fn addAtomExtra(self: *InternalObject, allocator: Allocator, extra: Atom.Extra) !u32 {
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
try self.atoms_extra.ensureUnusedCapacity(allocator, fields.len);
|
||||
return self.addAtomExtraAssumeCapacity(extra);
|
||||
}
|
||||
|
||||
fn addAtomExtraAssumeCapacity(self: *InternalObject, extra: Atom.Extra) u32 {
|
||||
const index = @as(u32, @intCast(self.atoms_extra.items.len));
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
inline for (fields) |field| {
|
||||
self.atoms_extra.appendAssumeCapacity(switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
});
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getAtomExtra(self: InternalObject, index: u32) Atom.Extra {
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
var i: usize = index;
|
||||
var result: Atom.Extra = undefined;
|
||||
inline for (fields) |field| {
|
||||
@field(result, field.name) = switch (field.type) {
|
||||
u32 => self.atoms_extra.items[i],
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
i += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn setAtomExtra(self: *InternalObject, index: u32, extra: Atom.Extra) void {
|
||||
assert(index > 0);
|
||||
const fields = @typeInfo(Atom.Extra).Struct.fields;
|
||||
inline for (fields, 0..) |field, i| {
|
||||
self.atoms_extra.items[index + i] = switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getEntryRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
|
||||
const index = self.entry_index orelse return null;
|
||||
return self.getSymbolRef(index, macho_file);
|
||||
}
|
||||
|
||||
pub fn getDyldStubBinderRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
|
||||
const index = self.dyld_stub_binder_index orelse return null;
|
||||
return self.getSymbolRef(index, macho_file);
|
||||
}
|
||||
|
||||
pub fn getDyldPrivateRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
|
||||
const index = self.dyld_private_index orelse return null;
|
||||
return self.getSymbolRef(index, macho_file);
|
||||
}
|
||||
|
||||
pub fn getObjcMsgSendRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
|
||||
const index = self.objc_msg_send_index orelse return null;
|
||||
return self.getSymbolRef(index, macho_file);
|
||||
}
|
||||
|
||||
pub fn addSymbol(self: *InternalObject, allocator: Allocator) !Symbol.Index {
|
||||
try self.symbols.ensureUnusedCapacity(allocator, 1);
|
||||
return self.addSymbolAssumeCapacity();
|
||||
}
|
||||
|
||||
pub fn addSymbolAssumeCapacity(self: *InternalObject) Symbol.Index {
|
||||
const index: Symbol.Index = @intCast(self.symbols.items.len);
|
||||
const symbol = self.symbols.addOneAssumeCapacity();
|
||||
symbol.* = .{ .file = self.index };
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbolRef(self: InternalObject, index: Symbol.Index, macho_file: *MachO) MachO.Ref {
|
||||
const global_index = self.globals.items[index];
|
||||
if (macho_file.resolver.get(global_index)) |ref| return ref;
|
||||
return .{ .index = index, .file = self.index };
|
||||
}
|
||||
|
||||
pub fn addSymbolExtra(self: *InternalObject, allocator: Allocator, extra: Symbol.Extra) !u32 {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
try self.symbols_extra.ensureUnusedCapacity(allocator, fields.len);
|
||||
return self.addSymbolExtraAssumeCapacity(extra);
|
||||
}
|
||||
|
||||
fn addSymbolExtraAssumeCapacity(self: *InternalObject, extra: Symbol.Extra) u32 {
|
||||
const index = @as(u32, @intCast(self.symbols_extra.items.len));
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields) |field| {
|
||||
self.symbols_extra.appendAssumeCapacity(switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
});
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn getSymbolExtra(self: InternalObject, index: u32) Symbol.Extra {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
var i: usize = index;
|
||||
var result: Symbol.Extra = undefined;
|
||||
inline for (fields) |field| {
|
||||
@field(result, field.name) = switch (field.type) {
|
||||
u32 => self.symbols_extra.items[i],
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
i += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn setSymbolExtra(self: *InternalObject, index: u32, extra: Symbol.Extra) void {
|
||||
const fields = @typeInfo(Symbol.Extra).Struct.fields;
|
||||
inline for (fields, 0..) |field, i| {
|
||||
self.symbols_extra.items[index + i] = switch (field.type) {
|
||||
u32 => @field(extra, field.name),
|
||||
else => @compileError("bad field type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const FormatContext = struct {
|
||||
self: *InternalObject,
|
||||
macho_file: *MachO,
|
||||
@@ -309,8 +804,8 @@ fn formatAtoms(
|
||||
_ = unused_fmt_string;
|
||||
_ = options;
|
||||
try writer.writeAll(" atoms\n");
|
||||
for (ctx.self.atoms.items) |atom_index| {
|
||||
const atom = ctx.macho_file.getAtom(atom_index).?;
|
||||
for (ctx.self.getAtoms()) |atom_index| {
|
||||
const atom = ctx.self.getAtom(atom_index) orelse continue;
|
||||
try writer.print(" {}\n", .{atom.fmt(ctx.macho_file)});
|
||||
}
|
||||
}
|
||||
@@ -330,10 +825,17 @@ fn formatSymtab(
|
||||
) !void {
|
||||
_ = unused_fmt_string;
|
||||
_ = options;
|
||||
const macho_file = ctx.macho_file;
|
||||
const self = ctx.self;
|
||||
try writer.writeAll(" symbols\n");
|
||||
for (ctx.self.symbols.items) |index| {
|
||||
const global = ctx.macho_file.getSymbol(index);
|
||||
try writer.print(" {}\n", .{global.fmt(ctx.macho_file)});
|
||||
for (self.symbols.items, 0..) |sym, i| {
|
||||
const ref = self.getSymbolRef(@intCast(i), macho_file);
|
||||
if (ref.getFile(macho_file) == null) {
|
||||
// TODO any better way of handling this?
|
||||
try writer.print(" {s} : unclaimed\n", .{sym.getName(macho_file)});
|
||||
} else {
|
||||
try writer.print(" {}\n", .{ref.getSymbol(macho_file).?.fmt(macho_file)});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,6 +854,7 @@ const assert = std.debug.assert;
|
||||
const macho = std.macho;
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Atom = @import("Atom.zig");
|
||||
|
||||
Reference in New Issue
Block a user