authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-26 22:47:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 11:39:35+02:00
log7f6973fbae3b01827a71497f647ee27e64845f51
tree4cd4dde1087f7d29580c2083985afc1ad76d8b18
parentd17867939ee8a9de6bb96748e047b5e561b7606c

macho: unify segment handling


1 files changed, 6 insertions(+), 9 deletions(-)

src/link/MachO/zld.zig+6-9
......@@ -1394,8 +1394,7 @@ pub const Zld = struct {
13941394 // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER.
13951395 log.debug("collecting export data", .{});
13961396
1397 const segment_index = self.getSegmentByName("__TEXT").?;
1398 const exec_segment = self.segments.items[segment_index];
1397 const exec_segment = self.segments.items[self.header_segment_cmd_index.?];
13991398 const base_address = exec_segment.vmaddr;
14001399
14011400 for (self.globals.items) |global| {
......@@ -1517,10 +1516,8 @@ pub const Zld = struct {
15171516 }
15181517
15191518 fn writeFunctionStarts(self: *Zld) !void {
1520 const text_seg_index = self.getSegmentByName("__TEXT") orelse return;
1521 const text_seg = self.segments.items[text_seg_index];
1522
15231519 const gpa = self.gpa;
1520 const seg = self.segments.items[self.header_segment_cmd_index.?];
15241521
15251522 // We need to sort by address first
15261523 var addresses = std.ArrayList(u64).init(gpa);
......@@ -1547,7 +1544,7 @@ pub const Zld = struct {
15471544
15481545 var last_off: u32 = 0;
15491546 for (addresses.items) |addr| {
1550 const offset = @as(u32, @intCast(addr - text_seg.vmaddr));
1547 const offset = @as(u32, @intCast(addr - seg.vmaddr));
15511548 const diff = offset - last_off;
15521549
15531550 if (diff == 0) continue;
......@@ -1908,7 +1905,7 @@ pub const Zld = struct {
19081905 }
19091906
19101907 fn writeCodeSignature(self: *Zld, comp: *const Compilation, code_sig: *CodeSignature) !void {
1911 const seg_id = self.getSegmentByName("__TEXT").?;
1908 const seg_id = self.header_segment_cmd_index.?;
19121909 const seg = self.segments.items[seg_id];
19131910
19141911 var buffer = std.ArrayList(u8).init(self.gpa);
......@@ -3026,7 +3023,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
30263023 // that the free space between the end of the last non-zerofill section of __DATA
30273024 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will
30283025 // copy-paste this space into memory for quicker zerofill operation.
3029 if (zld.getSegmentByName("__DATA")) |data_seg_id| blk: {
3026 if (zld.data_segment_cmd_index) |data_seg_id| blk: {
30303027 var physical_zerofill_start: ?u64 = null;
30313028 const section_indexes = zld.getSectionIndexes(data_seg_id);
30323029 for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {
......@@ -3080,7 +3077,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
30803077 try load_commands.writeDylinkerLC(lc_writer);
30813078
30823079 if (zld.options.output_mode == .Exe) {
3083 const seg_id = zld.getSegmentByName("__TEXT").?;
3080 const seg_id = zld.header_segment_cmd_index.?;
30843081 const seg = zld.segments.items[seg_id];
30853082 const global = zld.getEntryPoint();
30863083 const sym = zld.getSymbol(global);