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 {...@@ -1394,8 +1394,7 @@ pub const Zld = struct {
1394 // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER.1394 // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER.
1395 log.debug("collecting export data", .{});1395 log.debug("collecting export data", .{});
13961396
1397 const segment_index = self.getSegmentByName("__TEXT").?;1397 const exec_segment = self.segments.items[self.header_segment_cmd_index.?];
1398 const exec_segment = self.segments.items[segment_index];
1399 const base_address = exec_segment.vmaddr;1398 const base_address = exec_segment.vmaddr;
14001399
1401 for (self.globals.items) |global| {1400 for (self.globals.items) |global| {
...@@ -1517,10 +1516,8 @@ pub const Zld = struct {...@@ -1517,10 +1516,8 @@ pub const Zld = struct {
1517 }1516 }
15181517
1519 fn writeFunctionStarts(self: *Zld) !void {1518 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
1523 const gpa = self.gpa;1519 const gpa = self.gpa;
1520 const seg = self.segments.items[self.header_segment_cmd_index.?];
15241521
1525 // We need to sort by address first1522 // We need to sort by address first
1526 var addresses = std.ArrayList(u64).init(gpa);1523 var addresses = std.ArrayList(u64).init(gpa);
...@@ -1547,7 +1544,7 @@ pub const Zld = struct {...@@ -1547,7 +1544,7 @@ pub const Zld = struct {
15471544
1548 var last_off: u32 = 0;1545 var last_off: u32 = 0;
1549 for (addresses.items) |addr| {1546 for (addresses.items) |addr| {
1550 const offset = @as(u32, @intCast(addr - text_seg.vmaddr));1547 const offset = @as(u32, @intCast(addr - seg.vmaddr));
1551 const diff = offset - last_off;1548 const diff = offset - last_off;
15521549
1553 if (diff == 0) continue;1550 if (diff == 0) continue;
...@@ -1908,7 +1905,7 @@ pub const Zld = struct {...@@ -1908,7 +1905,7 @@ pub const Zld = struct {
1908 }1905 }
19091906
1910 fn writeCodeSignature(self: *Zld, comp: *const Compilation, code_sig: *CodeSignature) !void {1907 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.?;
1912 const seg = self.segments.items[seg_id];1909 const seg = self.segments.items[seg_id];
19131910
1914 var buffer = std.ArrayList(u8).init(self.gpa);1911 var buffer = std.ArrayList(u8).init(self.gpa);
...@@ -3026,7 +3023,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr...@@ -3026,7 +3023,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
3026 // that the free space between the end of the last non-zerofill section of __DATA3023 // that the free space between the end of the last non-zerofill section of __DATA
3027 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will3024 // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will
3028 // copy-paste this space into memory for quicker zerofill operation.3025 // 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: {
3030 var physical_zerofill_start: ?u64 = null;3027 var physical_zerofill_start: ?u64 = null;
3031 const section_indexes = zld.getSectionIndexes(data_seg_id);3028 const section_indexes = zld.getSectionIndexes(data_seg_id);
3032 for (zld.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| {3029 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...@@ -3080,7 +3077,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
3080 try load_commands.writeDylinkerLC(lc_writer);3077 try load_commands.writeDylinkerLC(lc_writer);
30813078
3082 if (zld.options.output_mode == .Exe) {3079 if (zld.options.output_mode == .Exe) {
3083 const seg_id = zld.getSegmentByName("__TEXT").?;3080 const seg_id = zld.header_segment_cmd_index.?;
3084 const seg = zld.segments.items[seg_id];3081 const seg = zld.segments.items[seg_id];
3085 const global = zld.getEntryPoint();3082 const global = zld.getEntryPoint();
3086 const sym = zld.getSymbol(global);3083 const sym = zld.getSymbol(global);