authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-20 07:23:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-20 16:55:32+02:00
log7bc6554a58274730f762bffb1f031af8dcfdb1fb
treebb579da41605be61ed172c36d0882a8e175c2b6c
parente5b83056ae335b12caf5b8912ec40e9962ce27f7

zld: map __DATA,__common as __DATA_CONST,__common


1 files changed, 55 insertions(+), 16 deletions(-)

src/link/MachO/Zld.zig+55-16
......@@ -64,6 +64,7 @@ got_section_index: ?u16 = null,
6464mod_init_func_section_index: ?u16 = null,
6565mod_term_func_section_index: ?u16 = null,
6666data_const_section_index: ?u16 = null,
67common_section_index: ?u16 = null,
6768
6869// __DATA segment sections
6970tlv_section_index: ?u16 = null,
......@@ -483,23 +484,43 @@ fn updateMetadata(self: *Zld) !void {
483484 },
484485 macho.S_ZEROFILL => {
485486 if (!mem.eql(u8, segname, "__DATA")) continue;
486 if (self.bss_section_index != null) continue;
487 if (mem.eql(u8, sectname, "__common")) {
488 if (self.common_section_index != null) continue;
487489
488 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
489 try data_seg.addSection(self.allocator, .{
490 .sectname = makeStaticString("__bss"),
491 .segname = makeStaticString("__DATA"),
492 .addr = 0,
493 .size = 0,
494 .offset = 0,
495 .@"align" = 0,
496 .reloff = 0,
497 .nreloc = 0,
498 .flags = macho.S_ZEROFILL,
499 .reserved1 = 0,
500 .reserved2 = 0,
501 .reserved3 = 0,
502 });
490 self.common_section_index = @intCast(u16, data_const_seg.sections.items.len);
491 try data_const_seg.addSection(self.allocator, .{
492 .sectname = makeStaticString("__common"),
493 .segname = makeStaticString("__DATA_CONST"),
494 .addr = 0,
495 .size = 0,
496 .offset = 0,
497 .@"align" = 0,
498 .reloff = 0,
499 .nreloc = 0,
500 .flags = macho.S_ZEROFILL,
501 .reserved1 = 0,
502 .reserved2 = 0,
503 .reserved3 = 0,
504 });
505 } else {
506 if (self.bss_section_index != null) continue;
507
508 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
509 try data_seg.addSection(self.allocator, .{
510 .sectname = makeStaticString("__bss"),
511 .segname = makeStaticString("__DATA"),
512 .addr = 0,
513 .size = 0,
514 .offset = 0,
515 .@"align" = 0,
516 .reloff = 0,
517 .nreloc = 0,
518 .flags = macho.S_ZEROFILL,
519 .reserved1 = 0,
520 .reserved2 = 0,
521 .reserved3 = 0,
522 });
523 }
503524 },
504525 macho.S_THREAD_LOCAL_VARIABLES => {
505526 if (!mem.eql(u8, segname, "__DATA")) continue;
......@@ -586,7 +607,9 @@ fn updateMetadata(self: *Zld) !void {
586607
587608 const segname = parseName(&source_sect.segname);
588609 const sectname = parseName(&source_sect.sectname);
610
589611 log.debug("section '{s}/{s}' will be unmapped", .{ segname, sectname });
612
590613 try self.unhandled_sections.putNoClobber(self.allocator, .{
591614 .object_id = object_id,
592615 .source_sect_id = source_sect_id,
......@@ -603,6 +626,7 @@ const MatchingSection = struct {
603626fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
604627 const segname = parseName(&section.segname);
605628 const sectname = parseName(&section.sectname);
629
606630 const res: ?MatchingSection = blk: {
607631 switch (section.flags) {
608632 macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => {
......@@ -630,6 +654,12 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
630654 };
631655 },
632656 macho.S_ZEROFILL => {
657 if (mem.eql(u8, sectname, "__common")) {
658 break :blk .{
659 .seg = self.data_const_segment_cmd_index.?,
660 .sect = self.common_section_index.?,
661 };
662 }
633663 break :blk .{
634664 .seg = self.data_segment_cmd_index.?,
635665 .sect = self.bss_section_index.?,
......@@ -685,6 +715,7 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
685715 },
686716 }
687717 };
718
688719 return res;
689720}
690721
......@@ -733,6 +764,7 @@ fn sortSections(self: *Zld) !void {
733764 &self.mod_init_func_section_index,
734765 &self.mod_term_func_section_index,
735766 &self.data_const_section_index,
767 &self.common_section_index,
736768 };
737769 for (indices) |maybe_index| {
738770 const new_index: u16 = if (maybe_index.*) |index| blk: {
......@@ -1634,6 +1666,7 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void {
16341666 target_sect_off,
16351667 target_sect_off + sect.code.len,
16361668 });
1669
16371670 // Zero-out the space
16381671 var zeroes = try self.allocator.alloc(u8, sect.code.len);
16391672 defer self.allocator.free(zeroes);
......@@ -2118,6 +2151,12 @@ fn populateMetadata(self: *Zld) !void {
21182151}
21192152
21202153fn flush(self: *Zld) !void {
2154 if (self.common_section_index) |index| {
2155 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
2156 const sect = &seg.sections.items[index];
2157 sect.offset = 0;
2158 }
2159
21212160 if (self.bss_section_index) |index| {
21222161 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
21232162 const sect = &seg.sections.items[index];