zld: correctly set n_sect for sections as symbols

This commit is contained in:
Jakub Konka
2021-07-17 11:29:40 +02:00
parent db8020ac0d
commit 71384a383e
3 changed files with 18 additions and 14 deletions

View File

@@ -703,20 +703,25 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void {
// Since there is no symbol to refer to this block, we create
// a temp one, unless we already did that when working out the relocations
// of other text blocks.
const block_local_sym_index = @intCast(u32, zld.locals.items.len);
const sym_name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{
self.name.?,
segmentName(sect),
sectionName(sect),
});
defer self.allocator.free(sym_name);
try zld.locals.append(zld.allocator, .{
.n_strx = try zld.makeString(sym_name),
.n_type = macho.N_SECT,
.n_sect = zld.sectionId(match),
.n_desc = 0,
.n_value = sect.addr,
});
const block_local_sym_index = self.sections_as_symbols.get(sect_id) orelse blk: {
const block_local_sym_index = @intCast(u32, zld.locals.items.len);
try zld.locals.append(zld.allocator, .{
.n_strx = try zld.makeString(sym_name),
.n_type = macho.N_SECT,
.n_sect = zld.sectionId(match),
.n_desc = 0,
.n_value = sect.addr,
});
try self.sections_as_symbols.putNoClobber(self.allocator, sect_id, block_local_sym_index);
break :blk block_local_sym_index;
};
const block = try self.allocator.create(TextBlock);
errdefer self.allocator.destroy(block);

View File

@@ -548,6 +548,7 @@ fn initRelocFromObject(rel: macho.relocation_info, object: *Object, ctx: RelocCo
const local_sym_index = object.sections_as_symbols.get(sect_id) orelse blk: {
const seg = object.load_commands.items[object.segment_cmd_index.?].Segment;
const sect = seg.sections.items[sect_id];
const match = (try ctx.zld.getMatchingSection(sect)) orelse unreachable;
const local_sym_index = @intCast(u32, ctx.zld.locals.items.len);
const sym_name = try std.fmt.allocPrint(ctx.zld.allocator, "l_{s}_{s}_{s}", .{
object.name.?,
@@ -559,9 +560,9 @@ fn initRelocFromObject(rel: macho.relocation_info, object: *Object, ctx: RelocCo
try ctx.zld.locals.append(ctx.zld.allocator, .{
.n_strx = try ctx.zld.makeString(sym_name),
.n_type = macho.N_SECT,
.n_sect = 0,
.n_sect = ctx.zld.sectionId(match),
.n_desc = 0,
.n_value = 0,
.n_value = sect.addr,
});
try object.sections_as_symbols.putNoClobber(object.allocator, sect_id, local_sym_index);
break :blk local_sym_index;
@@ -759,7 +760,6 @@ pub fn parseRelocsFromObject(
} else if (parsed_rel.payload == .unsigned) {
switch (parsed_rel.where) {
.import => {
log.warn("WAT {s}", .{ctx.zld.getString(ctx.zld.imports.items[parsed_rel.where_index].n_strx)});
try self.bindings.append(.{
.local_sym_index = parsed_rel.where_index,
.offset = parsed_rel.offset,

View File

@@ -232,6 +232,8 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg
try self.parseInputFiles(files, args.syslibroot);
try self.parseLibs(args.libs, args.syslibroot);
try self.resolveSymbols();
try self.parseTextBlocks();
// try self.sortSections();
log.warn("locals", .{});
for (self.locals.items) |sym, id| {
@@ -276,8 +278,6 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg
}
}
try self.parseTextBlocks();
var it = self.blocks.iterator();
while (it.next()) |entry| {
const seg = self.load_commands.items[entry.key_ptr.seg].Segment;
@@ -289,7 +289,6 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg
}
return error.TODO;
// try self.sortSections();
// try self.addRpaths(args.rpaths);
// try self.addDataInCodeLC();
// try self.addCodeSignatureLC();