authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-12 11:39:23+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-12 11:39:23+02:00
log05b5e49bc07b7c26dc1eea28d6a4a3a1cb42585e
tree45a968fad4c0a656ce911fa7566c79d6bd5ea2d3
parentb5c117d0516d13019fc79b80bc6be1aa31d51d85
parent6aa9c63f632a284069c88475cbde3cc6b114750b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9086 from ziglang/fix-9050

zig ld: act as linker for golang

2 files changed, 72 insertions(+), 22 deletions(-)

src/link/MachO.zig+5
......@@ -807,6 +807,11 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
807807 rpaths.appendAssumeCapacity(key.*);
808808 }
809809
810 // frameworks
811 for (self.base.options.frameworks) |framework| {
812 log.warn("frameworks not yet supported for '-framework {s}'", .{framework});
813 }
814
810815 if (self.base.options.verbose_link) {
811816 var argv = std.ArrayList([]const u8).init(arena);
812817
src/link/MachO/Zld.zig+67-22
......@@ -442,6 +442,25 @@ fn updateMetadata(self: *Zld) !void {
442442 const flags = source_sect.flags;
443443
444444 switch (flags) {
445 macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS => {
446 if (self.text_section_index != null) continue;
447
448 self.text_section_index = @intCast(u16, text_seg.sections.items.len);
449 try text_seg.addSection(self.allocator, .{
450 .sectname = makeStaticString("__text"),
451 .segname = makeStaticString("__TEXT"),
452 .addr = 0,
453 .size = 0,
454 .offset = 0,
455 .@"align" = 0,
456 .reloff = 0,
457 .nreloc = 0,
458 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
459 .reserved1 = 0,
460 .reserved2 = 0,
461 .reserved3 = 0,
462 });
463 },
445464 macho.S_REGULAR, macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => {
446465 if (mem.eql(u8, segname, "__TEXT")) {
447466 if (mem.eql(u8, sectname, "__ustring")) {
......@@ -481,8 +500,7 @@ fn updateMetadata(self: *Zld) !void {
481500 .reserved3 = 0,
482501 });
483502 }
484 } else if (mem.eql(u8, segname, "__DATA")) {
485 if (!mem.eql(u8, sectname, "__const")) continue;
503 } else if (mem.eql(u8, segname, "__DATA") or mem.eql(u8, segname, "__DATA_CONST")) {
486504 if (self.data_const_section_index != null) continue;
487505
488506 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -503,7 +521,6 @@ fn updateMetadata(self: *Zld) !void {
503521 }
504522 },
505523 macho.S_CSTRING_LITERALS => {
506 if (!mem.eql(u8, segname, "__TEXT")) continue;
507524 if (self.cstring_section_index != null) continue;
508525
509526 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
......@@ -523,7 +540,6 @@ fn updateMetadata(self: *Zld) !void {
523540 });
524541 },
525542 macho.S_MOD_INIT_FUNC_POINTERS => {
526 if (!mem.eql(u8, segname, "__DATA")) continue;
527543 if (self.mod_init_func_section_index != null) continue;
528544
529545 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -543,7 +559,6 @@ fn updateMetadata(self: *Zld) !void {
543559 });
544560 },
545561 macho.S_MOD_TERM_FUNC_POINTERS => {
546 if (!mem.eql(u8, segname, "__DATA")) continue;
547562 if (self.mod_term_func_section_index != null) continue;
548563
549564 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -563,7 +578,6 @@ fn updateMetadata(self: *Zld) !void {
563578 });
564579 },
565580 macho.S_ZEROFILL => {
566 if (!mem.eql(u8, segname, "__DATA")) continue;
567581 if (mem.eql(u8, sectname, "__common")) {
568582 if (self.common_section_index != null) continue;
569583
......@@ -603,7 +617,6 @@ fn updateMetadata(self: *Zld) !void {
603617 }
604618 },
605619 macho.S_THREAD_LOCAL_VARIABLES => {
606 if (!mem.eql(u8, segname, "__DATA")) continue;
607620 if (self.tlv_section_index != null) continue;
608621
609622 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -623,7 +636,6 @@ fn updateMetadata(self: *Zld) !void {
623636 });
624637 },
625638 macho.S_THREAD_LOCAL_REGULAR => {
626 if (!mem.eql(u8, segname, "__DATA")) continue;
627639 if (self.tlv_data_section_index != null) continue;
628640
629641 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -643,7 +655,6 @@ fn updateMetadata(self: *Zld) !void {
643655 });
644656 },
645657 macho.S_THREAD_LOCAL_ZEROFILL => {
646 if (!mem.eql(u8, segname, "__DATA")) continue;
647658 if (self.tlv_bss_section_index != null) continue;
648659
649660 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -662,8 +673,32 @@ fn updateMetadata(self: *Zld) !void {
662673 .reserved3 = 0,
663674 });
664675 },
676 macho.S_COALESCED |
677 macho.S_ATTR_NO_TOC |
678 macho.S_ATTR_STRIP_STATIC_SYMS |
679 macho.S_ATTR_LIVE_SUPPORT => {
680 log.debug("TODO __eh_frame section: type 0x{x}, name '{s},{s}'", .{
681 flags, segname, sectname,
682 });
683 continue;
684 },
685 macho.S_REGULAR | macho.S_ATTR_DEBUG => {
686 if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) {
687 log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{
688 flags, segname, sectname,
689 });
690 }
691 continue;
692 },
665693 else => {
666 log.debug("unhandled section type 0x{x} for '{s}/{s}'", .{ flags, segname, sectname });
694 if (mem.eql(u8, "__LLVM", segname) and mem.eql(u8, "__asm", sectname)) {
695 log.debug("TODO LLVM bitcode section: type 0x{x}, name '{s},{s}'", .{
696 flags, segname, sectname,
697 });
698 continue;
699 }
700 log.err("unhandled section type 0x{x} for '{s},{s}'", .{ flags, segname, sectname });
701 return error.UnhandledSection;
667702 },
668703 }
669704 }
......@@ -857,24 +892,29 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
857892 .seg = self.text_segment_cmd_index.?,
858893 .sect = self.ustring_section_index.?,
859894 };
860 } else {
861 break :blk .{
862 .seg = self.text_segment_cmd_index.?,
863 .sect = self.text_const_section_index.?,
864 };
865895 }
866 } else if (mem.eql(u8, segname, "__DATA")) {
867 if (mem.eql(u8, sectname, "__data")) {
868 break :blk .{
869 .seg = self.data_segment_cmd_index.?,
870 .sect = self.data_section_index.?,
871 };
872 } else if (mem.eql(u8, sectname, "__const")) {
896 break :blk .{
897 .seg = self.text_segment_cmd_index.?,
898 .sect = self.text_const_section_index.?,
899 };
900 }
901 if (mem.eql(u8, segname, "__DATA_CONST")) {
902 break :blk .{
903 .seg = self.data_const_segment_cmd_index.?,
904 .sect = self.data_const_section_index.?,
905 };
906 }
907 if (mem.eql(u8, segname, "__DATA")) {
908 if (mem.eql(u8, sectname, "__const")) {
873909 break :blk .{
874910 .seg = self.data_const_segment_cmd_index.?,
875911 .sect = self.data_const_section_index.?,
876912 };
877913 }
914 break :blk .{
915 .seg = self.data_segment_cmd_index.?,
916 .sect = self.data_section_index.?,
917 };
878918 }
879919 break :blk null;
880920 },
......@@ -1901,7 +1941,12 @@ fn relocTargetAddr(self: *Zld, object: *const Object, target: reloc.Relocation.T
19011941 }
19021942 },
19031943 .section => |sect_id| {
1944 log.debug(" | section offset", .{});
19041945 const source_sect = object.sections.items[sect_id];
1946 log.debug(" | section '{s},{s}'", .{
1947 parseName(&source_sect.inner.segname),
1948 parseName(&source_sect.inner.sectname),
1949 });
19051950 const target_map = source_sect.target_map orelse unreachable;
19061951 const target_seg = self.load_commands.items[target_map.segment_id].Segment;
19071952 const target_sect = target_seg.sections.items[target_map.section_id];