authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-27 11:30:02+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-27 11:30:02+02:00
log1e65d41a659894eed6beb92ae89ca531f6f9dfd0
tree58fb5ebde51d25e5edca40dbcf39ed5ccfd14a8d
parent705cd64080a5555425de7d13a9fbc6c19af65bde

macho: merge __common with __bss section


1 files changed, 25 insertions(+), 47 deletions(-)

src/link/MachO.zig+25-47
...@@ -127,7 +127,6 @@ tlv_bss_section_index: ?u16 = null,...@@ -127,7 +127,6 @@ tlv_bss_section_index: ?u16 = null,
127la_symbol_ptr_section_index: ?u16 = null,127la_symbol_ptr_section_index: ?u16 = null,
128data_section_index: ?u16 = null,128data_section_index: ?u16 = null,
129bss_section_index: ?u16 = null,129bss_section_index: ?u16 = null,
130common_section_index: ?u16 = null,
131130
132objc_const_section_index: ?u16 = null,131objc_const_section_index: ?u16 = null,
133objc_selrefs_section_index: ?u16 = null,132objc_selrefs_section_index: ?u16 = null,
...@@ -1222,31 +1221,17 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio...@@ -1222,31 +1221,17 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio
1222 };1221 };
1223 },1222 },
1224 macho.S_ZEROFILL => {1223 macho.S_ZEROFILL => {
1225 if (mem.eql(u8, sectname, "__common")) {1224 if (self.bss_section_index == null) {
1226 if (self.common_section_index == null) {1225 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
1227 self.common_section_index = @intCast(u16, data_seg.sections.items.len);1226 try data_seg.addSection(self.base.allocator, "__bss", .{
1228 try data_seg.addSection(self.base.allocator, "__common", .{1227 .flags = macho.S_ZEROFILL,
1229 .flags = macho.S_ZEROFILL,1228 });
1230 });
1231 }
1232
1233 break :blk .{
1234 .seg = self.data_segment_cmd_index.?,
1235 .sect = self.common_section_index.?,
1236 };
1237 } else {
1238 if (self.bss_section_index == null) {
1239 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
1240 try data_seg.addSection(self.base.allocator, "__bss", .{
1241 .flags = macho.S_ZEROFILL,
1242 });
1243 }
1244
1245 break :blk .{
1246 .seg = self.data_segment_cmd_index.?,
1247 .sect = self.bss_section_index.?,
1248 };
1249 }1229 }
1230
1231 break :blk .{
1232 .seg = self.data_segment_cmd_index.?,
1233 .sect = self.bss_section_index.?,
1234 };
1250 },1235 },
1251 macho.S_THREAD_LOCAL_VARIABLES => {1236 macho.S_THREAD_LOCAL_VARIABLES => {
1252 if (self.tlv_section_index == null) {1237 if (self.tlv_section_index == null) {
...@@ -1598,7 +1583,6 @@ fn sortSections(self: *MachO) !void {...@@ -1598,7 +1583,6 @@ fn sortSections(self: *MachO) !void {
1598 &self.tlv_data_section_index,1583 &self.tlv_data_section_index,
1599 &self.tlv_bss_section_index,1584 &self.tlv_bss_section_index,
1600 &self.bss_section_index,1585 &self.bss_section_index,
1601 &self.common_section_index,
1602 };1586 };
1603 for (indices) |maybe_index| {1587 for (indices) |maybe_index| {
1604 const new_index: u16 = if (maybe_index.*) |index| blk: {1588 const new_index: u16 = if (maybe_index.*) |index| blk: {
...@@ -2578,16 +2562,16 @@ fn resolveSymbols(self: *MachO) !void {...@@ -2578,16 +2562,16 @@ fn resolveSymbols(self: *MachO) !void {
2578 while (tentatives.popOrNull()) |entry| {2562 while (tentatives.popOrNull()) |entry| {
2579 const sym = &self.globals.items[entry.key];2563 const sym = &self.globals.items[entry.key];
2580 const match: MatchingSection = blk: {2564 const match: MatchingSection = blk: {
2581 if (self.common_section_index == null) {2565 if (self.bss_section_index == null) {
2582 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;2566 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2583 self.common_section_index = @intCast(u16, data_seg.sections.items.len);2567 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
2584 try data_seg.addSection(self.base.allocator, "__common", .{2568 try data_seg.addSection(self.base.allocator, "__bss", .{
2585 .flags = macho.S_ZEROFILL,2569 .flags = macho.S_ZEROFILL,
2586 });2570 });
2587 }2571 }
2588 break :blk .{2572 break :blk .{
2589 .seg = self.data_segment_cmd_index.?,2573 .seg = self.data_segment_cmd_index.?,
2590 .sect = self.common_section_index.?,2574 .sect = self.bss_section_index.?,
2591 };2575 };
2592 };2576 };
2593 _ = try self.section_ordinals.getOrPut(self.base.allocator, match);2577 _ = try self.section_ordinals.getOrPut(self.base.allocator, match);
...@@ -2890,23 +2874,17 @@ fn addLoadDylibLCs(self: *MachO) !void {...@@ -2890,23 +2874,17 @@ fn addLoadDylibLCs(self: *MachO) !void {
2890fn flushZld(self: *MachO) !void {2874fn flushZld(self: *MachO) !void {
2891 try self.writeTextBlocks();2875 try self.writeTextBlocks();
28922876
2893 if (self.common_section_index) |index| {2877 // if (self.bss_section_index) |index| {
2894 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;2878 // const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2895 const sect = &seg.sections.items[index];2879 // const sect = &seg.sections.items[index];
2896 sect.offset = 0;2880 // sect.offset = 0;
2897 }2881 // }
28982882
2899 if (self.bss_section_index) |index| {2883 // if (self.tlv_bss_section_index) |index| {
2900 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;2884 // const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2901 const sect = &seg.sections.items[index];2885 // const sect = &seg.sections.items[index];
2902 sect.offset = 0;2886 // sect.offset = 0;
2903 }2887 // }
2904
2905 if (self.tlv_bss_section_index) |index| {
2906 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2907 const sect = &seg.sections.items[index];
2908 sect.offset = 0;
2909 }
29102888
2911 try self.setEntryPoint();2889 try self.setEntryPoint();
2912 try self.writeRebaseInfoTableZld();2890 try self.writeRebaseInfoTableZld();