authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-21 14:31:20+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-24 18:56:17+02:00
logd1fcb998484c8dec6e931b51a89ce4b2999254f5
tree14b61163ed9d8c8482eeea520073c8529949f1a0
parenta480ae6e37199a3b210a4029f3d3c728e959a21d

zld+macho: populate segname from SegmentCommand when adding section


4 files changed, 52 insertions(+), 51 deletions(-)

src/link/MachO.zig+6-6
...@@ -1825,7 +1825,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1825,7 +1825,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18251825
1826 log.debug("found __text section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1826 log.debug("found __text section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
18271827
1828 try text_segment.addSection(self.base.allocator, "__text", "__TEXT", .{1828 try text_segment.addSection(self.base.allocator, "__text", .{
1829 .addr = text_segment.inner.vmaddr + off,1829 .addr = text_segment.inner.vmaddr + off,
1830 .size = @intCast(u32, needed_size),1830 .size = @intCast(u32, needed_size),
1831 .offset = @intCast(u32, off),1831 .offset = @intCast(u32, off),
...@@ -1856,7 +1856,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1856,7 +1856,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18561856
1857 log.debug("found __stubs section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1857 log.debug("found __stubs section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
18581858
1859 try text_segment.addSection(self.base.allocator, "__stubs", "__TEXT", .{1859 try text_segment.addSection(self.base.allocator, "__stubs", .{
1860 .addr = text_segment.inner.vmaddr + off,1860 .addr = text_segment.inner.vmaddr + off,
1861 .size = needed_size,1861 .size = needed_size,
1862 .offset = @intCast(u32, off),1862 .offset = @intCast(u32, off),
...@@ -1883,7 +1883,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1883,7 +1883,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18831883
1884 log.debug("found __stub_helper section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1884 log.debug("found __stub_helper section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
18851885
1886 try text_segment.addSection(self.base.allocator, "__stub_helper", "__TEXT", .{1886 try text_segment.addSection(self.base.allocator, "__stub_helper", .{
1887 .addr = text_segment.inner.vmaddr + off,1887 .addr = text_segment.inner.vmaddr + off,
1888 .size = needed_size,1888 .size = needed_size,
1889 .offset = @intCast(u32, off),1889 .offset = @intCast(u32, off),
...@@ -1928,7 +1928,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1928,7 +1928,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19281928
1929 log.debug("found __got section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1929 log.debug("found __got section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
19301930
1931 try dc_segment.addSection(self.base.allocator, "__got", "__DATA_CONST", .{1931 try dc_segment.addSection(self.base.allocator, "__got", .{
1932 .addr = dc_segment.inner.vmaddr + off - dc_segment.inner.fileoff,1932 .addr = dc_segment.inner.vmaddr + off - dc_segment.inner.fileoff,
1933 .size = needed_size,1933 .size = needed_size,
1934 .offset = @intCast(u32, off),1934 .offset = @intCast(u32, off),
...@@ -1973,7 +1973,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1973,7 +1973,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19731973
1974 log.debug("found __la_symbol_ptr section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1974 log.debug("found __la_symbol_ptr section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
19751975
1976 try data_segment.addSection(self.base.allocator, "__la_symbol_ptr", "__DATA", .{1976 try data_segment.addSection(self.base.allocator, "__la_symbol_ptr", .{
1977 .addr = data_segment.inner.vmaddr + off - data_segment.inner.fileoff,1977 .addr = data_segment.inner.vmaddr + off - data_segment.inner.fileoff,
1978 .size = needed_size,1978 .size = needed_size,
1979 .offset = @intCast(u32, off),1979 .offset = @intCast(u32, off),
...@@ -1993,7 +1993,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1993,7 +1993,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19931993
1994 log.debug("found __data section free space 0x{x} to 0x{x}", .{ off, off + needed_size });1994 log.debug("found __data section free space 0x{x} to 0x{x}", .{ off, off + needed_size });
19951995
1996 try data_segment.addSection(self.base.allocator, "__data", "__DATA", .{1996 try data_segment.addSection(self.base.allocator, "__data", .{
1997 .addr = data_segment.inner.vmaddr + off - data_segment.inner.fileoff,1997 .addr = data_segment.inner.vmaddr + off - data_segment.inner.fileoff,
1998 .size = needed_size,1998 .size = needed_size,
1999 .offset = @intCast(u32, off),1999 .offset = @intCast(u32, off),
src/link/MachO/DebugSymbols.zig+5-5
...@@ -226,7 +226,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void...@@ -226,7 +226,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
226 self.debug_str_section_index = @intCast(u16, dwarf_segment.sections.items.len);226 self.debug_str_section_index = @intCast(u16, dwarf_segment.sections.items.len);
227 assert(self.debug_string_table.items.len == 0);227 assert(self.debug_string_table.items.len == 0);
228228
229 try dwarf_segment.addSection(allocator, "__debug_str", "__DWARF", .{229 try dwarf_segment.addSection(allocator, "__debug_str", .{
230 .addr = dwarf_segment.inner.vmaddr,230 .addr = dwarf_segment.inner.vmaddr,
231 .size = @intCast(u32, self.debug_string_table.items.len),231 .size = @intCast(u32, self.debug_string_table.items.len),
232 .offset = @intCast(u32, dwarf_segment.inner.fileoff),232 .offset = @intCast(u32, dwarf_segment.inner.fileoff),
...@@ -246,7 +246,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void...@@ -246,7 +246,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
246246
247 log.debug("found dSym __debug_info free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });247 log.debug("found dSym __debug_info free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });
248248
249 try dwarf_segment.addSection(allocator, "__debug_info", "__DWARF", .{249 try dwarf_segment.addSection(allocator, "__debug_info", .{
250 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,250 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
251 .size = file_size_hint,251 .size = file_size_hint,
252 .offset = @intCast(u32, off),252 .offset = @intCast(u32, off),
...@@ -266,7 +266,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void...@@ -266,7 +266,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
266266
267 log.debug("found dSym __debug_abbrev free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });267 log.debug("found dSym __debug_abbrev free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });
268268
269 try dwarf_segment.addSection(allocator, "__debug_abbrev", "__DWARF", .{269 try dwarf_segment.addSection(allocator, "__debug_abbrev", .{
270 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,270 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
271 .size = file_size_hint,271 .size = file_size_hint,
272 .offset = @intCast(u32, off),272 .offset = @intCast(u32, off),
...@@ -286,7 +286,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void...@@ -286,7 +286,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
286286
287 log.debug("found dSym __debug_aranges free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });287 log.debug("found dSym __debug_aranges free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });
288288
289 try dwarf_segment.addSection(allocator, "__debug_aranges", "__DWARF", .{289 try dwarf_segment.addSection(allocator, "__debug_aranges", .{
290 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,290 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
291 .size = file_size_hint,291 .size = file_size_hint,
292 .offset = @intCast(u32, off),292 .offset = @intCast(u32, off),
...@@ -306,7 +306,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void...@@ -306,7 +306,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
306306
307 log.debug("found dSym __debug_line free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });307 log.debug("found dSym __debug_line free space 0x{x} to 0x{x}", .{ off, off + file_size_hint });
308308
309 try dwarf_segment.addSection(allocator, "__debug_line", "__DWARF", .{309 try dwarf_segment.addSection(allocator, "__debug_line", .{
310 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,310 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
311 .size = file_size_hint,311 .size = file_size_hint,
312 .offset = @intCast(u32, off),312 .offset = @intCast(u32, off),
src/link/MachO/Zld.zig+36-36
...@@ -540,7 +540,7 @@ fn updateMetadata(self: *Zld) !void {...@@ -540,7 +540,7 @@ fn updateMetadata(self: *Zld) !void {
540 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;540 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
541 const common_section_index = self.common_section_index orelse ind: {541 const common_section_index = self.common_section_index orelse ind: {
542 self.common_section_index = @intCast(u16, data_seg.sections.items.len);542 self.common_section_index = @intCast(u16, data_seg.sections.items.len);
543 try data_seg.addSection(self.allocator, "__common", "__DATA", .{543 try data_seg.addSection(self.allocator, "__common", .{
544 .flags = macho.S_ZEROFILL,544 .flags = macho.S_ZEROFILL,
545 });545 });
546 break :ind self.common_section_index.?;546 break :ind self.common_section_index.?;
...@@ -627,7 +627,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -627,7 +627,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
627 macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => {627 macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => {
628 if (self.text_const_section_index == null) {628 if (self.text_const_section_index == null) {
629 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);629 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);
630 try text_seg.addSection(self.allocator, "__const", "__TEXT", .{});630 try text_seg.addSection(self.allocator, "__const", .{});
631 }631 }
632632
633 break :blk .{633 break :blk .{
...@@ -641,7 +641,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -641,7 +641,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
641 // on merging the sections' contents.641 // on merging the sections' contents.
642 if (self.objc_methname_section_index == null) {642 if (self.objc_methname_section_index == null) {
643 self.objc_methname_section_index = @intCast(u16, text_seg.sections.items.len);643 self.objc_methname_section_index = @intCast(u16, text_seg.sections.items.len);
644 try text_seg.addSection(self.allocator, "__objc_methname", "__TEXT", .{644 try text_seg.addSection(self.allocator, "__objc_methname", .{
645 .flags = macho.S_CSTRING_LITERALS,645 .flags = macho.S_CSTRING_LITERALS,
646 });646 });
647 }647 }
...@@ -653,7 +653,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -653,7 +653,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
653 } else if (mem.eql(u8, sectname, "__objc_methtype")) {653 } else if (mem.eql(u8, sectname, "__objc_methtype")) {
654 if (self.objc_methtype_section_index == null) {654 if (self.objc_methtype_section_index == null) {
655 self.objc_methtype_section_index = @intCast(u16, text_seg.sections.items.len);655 self.objc_methtype_section_index = @intCast(u16, text_seg.sections.items.len);
656 try text_seg.addSection(self.allocator, "__objc_methtype", "__TEXT", .{656 try text_seg.addSection(self.allocator, "__objc_methtype", .{
657 .flags = macho.S_CSTRING_LITERALS,657 .flags = macho.S_CSTRING_LITERALS,
658 });658 });
659 }659 }
...@@ -665,7 +665,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -665,7 +665,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
665 } else if (mem.eql(u8, sectname, "__objc_classname")) {665 } else if (mem.eql(u8, sectname, "__objc_classname")) {
666 if (self.objc_classname_section_index == null) {666 if (self.objc_classname_section_index == null) {
667 self.objc_classname_section_index = @intCast(u16, text_seg.sections.items.len);667 self.objc_classname_section_index = @intCast(u16, text_seg.sections.items.len);
668 try text_seg.addSection(self.allocator, "__objc_classname", "__TEXT", .{});668 try text_seg.addSection(self.allocator, "__objc_classname", .{});
669 }669 }
670670
671 break :blk .{671 break :blk .{
...@@ -676,7 +676,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -676,7 +676,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
676676
677 if (self.cstring_section_index == null) {677 if (self.cstring_section_index == null) {
678 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);678 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
679 try text_seg.addSection(self.allocator, "__cstring", "__TEXT", .{679 try text_seg.addSection(self.allocator, "__cstring", .{
680 .flags = macho.S_CSTRING_LITERALS,680 .flags = macho.S_CSTRING_LITERALS,
681 });681 });
682 }682 }
...@@ -690,7 +690,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -690,7 +690,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
690 if (mem.eql(u8, segname, "__DATA") and mem.eql(u8, sectname, "__objc_selrefs")) {690 if (mem.eql(u8, segname, "__DATA") and mem.eql(u8, sectname, "__objc_selrefs")) {
691 if (self.objc_selrefs_section_index == null) {691 if (self.objc_selrefs_section_index == null) {
692 self.objc_selrefs_section_index = @intCast(u16, data_seg.sections.items.len);692 self.objc_selrefs_section_index = @intCast(u16, data_seg.sections.items.len);
693 try data_seg.addSection(self.allocator, "__objc_selrefs", "__DATA", .{693 try data_seg.addSection(self.allocator, "__objc_selrefs", .{
694 .flags = macho.S_LITERAL_POINTERS,694 .flags = macho.S_LITERAL_POINTERS,
695 });695 });
696 }696 }
...@@ -707,7 +707,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -707,7 +707,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
707 macho.S_MOD_INIT_FUNC_POINTERS => {707 macho.S_MOD_INIT_FUNC_POINTERS => {
708 if (self.mod_init_func_section_index == null) {708 if (self.mod_init_func_section_index == null) {
709 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);709 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
710 try data_const_seg.addSection(self.allocator, "__mod_init_func", "__DATA_CONST", .{710 try data_const_seg.addSection(self.allocator, "__mod_init_func", .{
711 .flags = macho.S_MOD_INIT_FUNC_POINTERS,711 .flags = macho.S_MOD_INIT_FUNC_POINTERS,
712 });712 });
713 }713 }
...@@ -720,7 +720,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -720,7 +720,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
720 macho.S_MOD_TERM_FUNC_POINTERS => {720 macho.S_MOD_TERM_FUNC_POINTERS => {
721 if (self.mod_term_func_section_index == null) {721 if (self.mod_term_func_section_index == null) {
722 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);722 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
723 try data_const_seg.addSection(self.allocator, "__mod_term_func", "__DATA_CONST", .{723 try data_const_seg.addSection(self.allocator, "__mod_term_func", .{
724 .flags = macho.S_MOD_TERM_FUNC_POINTERS,724 .flags = macho.S_MOD_TERM_FUNC_POINTERS,
725 });725 });
726 }726 }
...@@ -734,7 +734,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -734,7 +734,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
734 if (mem.eql(u8, sectname, "__common")) {734 if (mem.eql(u8, sectname, "__common")) {
735 if (self.common_section_index == null) {735 if (self.common_section_index == null) {
736 self.common_section_index = @intCast(u16, data_seg.sections.items.len);736 self.common_section_index = @intCast(u16, data_seg.sections.items.len);
737 try data_seg.addSection(self.allocator, "__common", "__DATA", .{737 try data_seg.addSection(self.allocator, "__common", .{
738 .flags = macho.S_ZEROFILL,738 .flags = macho.S_ZEROFILL,
739 });739 });
740 }740 }
...@@ -746,7 +746,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -746,7 +746,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
746 } else {746 } else {
747 if (self.bss_section_index == null) {747 if (self.bss_section_index == null) {
748 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);748 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
749 try data_seg.addSection(self.allocator, "__bss", "__DATA", .{749 try data_seg.addSection(self.allocator, "__bss", .{
750 .flags = macho.S_ZEROFILL,750 .flags = macho.S_ZEROFILL,
751 });751 });
752 }752 }
...@@ -760,7 +760,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -760,7 +760,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
760 macho.S_THREAD_LOCAL_VARIABLES => {760 macho.S_THREAD_LOCAL_VARIABLES => {
761 if (self.tlv_section_index == null) {761 if (self.tlv_section_index == null) {
762 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);762 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);
763 try data_seg.addSection(self.allocator, "__thread_vars", "__DATA", .{763 try data_seg.addSection(self.allocator, "__thread_vars", .{
764 .flags = macho.S_THREAD_LOCAL_VARIABLES,764 .flags = macho.S_THREAD_LOCAL_VARIABLES,
765 });765 });
766 }766 }
...@@ -773,7 +773,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -773,7 +773,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
773 macho.S_THREAD_LOCAL_REGULAR => {773 macho.S_THREAD_LOCAL_REGULAR => {
774 if (self.tlv_data_section_index == null) {774 if (self.tlv_data_section_index == null) {
775 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);775 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);
776 try data_seg.addSection(self.allocator, "__thread_data", "__DATA", .{776 try data_seg.addSection(self.allocator, "__thread_data", .{
777 .flags = macho.S_THREAD_LOCAL_REGULAR,777 .flags = macho.S_THREAD_LOCAL_REGULAR,
778 });778 });
779 }779 }
...@@ -786,7 +786,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -786,7 +786,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
786 macho.S_THREAD_LOCAL_ZEROFILL => {786 macho.S_THREAD_LOCAL_ZEROFILL => {
787 if (self.tlv_bss_section_index == null) {787 if (self.tlv_bss_section_index == null) {
788 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);788 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);
789 try data_seg.addSection(self.allocator, "__thread_bss", "__DATA", .{789 try data_seg.addSection(self.allocator, "__thread_bss", .{
790 .flags = macho.S_THREAD_LOCAL_ZEROFILL,790 .flags = macho.S_THREAD_LOCAL_ZEROFILL,
791 });791 });
792 }792 }
...@@ -802,7 +802,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -802,7 +802,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
802 // in the latest ld64 output.802 // in the latest ld64 output.
803 if (self.eh_frame_section_index == null) {803 if (self.eh_frame_section_index == null) {
804 self.eh_frame_section_index = @intCast(u16, text_seg.sections.items.len);804 self.eh_frame_section_index = @intCast(u16, text_seg.sections.items.len);
805 try text_seg.addSection(self.allocator, "__eh_frame", "__TEXT", .{});805 try text_seg.addSection(self.allocator, "__eh_frame", .{});
806 }806 }
807807
808 break :blk .{808 break :blk .{
...@@ -814,7 +814,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -814,7 +814,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
814 // TODO audit this: is this the right mapping?814 // TODO audit this: is this the right mapping?
815 if (self.data_const_section_index == null) {815 if (self.data_const_section_index == null) {
816 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);816 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
817 try data_const_seg.addSection(self.allocator, "__const", "__DATA_CONST", .{});817 try data_const_seg.addSection(self.allocator, "__const", .{});
818 }818 }
819819
820 break :blk .{820 break :blk .{
...@@ -826,7 +826,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -826,7 +826,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
826 if (sect.isCode()) {826 if (sect.isCode()) {
827 if (self.text_section_index == null) {827 if (self.text_section_index == null) {
828 self.text_section_index = @intCast(u16, text_seg.sections.items.len);828 self.text_section_index = @intCast(u16, text_seg.sections.items.len);
829 try text_seg.addSection(self.allocator, "__text", "__TEXT", .{829 try text_seg.addSection(self.allocator, "__text", .{
830 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,830 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
831 });831 });
832 }832 }
...@@ -850,7 +850,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -850,7 +850,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
850 if (mem.eql(u8, sectname, "__ustring")) {850 if (mem.eql(u8, sectname, "__ustring")) {
851 if (self.ustring_section_index == null) {851 if (self.ustring_section_index == null) {
852 self.ustring_section_index = @intCast(u16, text_seg.sections.items.len);852 self.ustring_section_index = @intCast(u16, text_seg.sections.items.len);
853 try text_seg.addSection(self.allocator, "__ustring", "__TEXT", .{});853 try text_seg.addSection(self.allocator, "__ustring", .{});
854 }854 }
855855
856 break :blk .{856 break :blk .{
...@@ -860,7 +860,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -860,7 +860,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
860 } else if (mem.eql(u8, sectname, "__gcc_except_tab")) {860 } else if (mem.eql(u8, sectname, "__gcc_except_tab")) {
861 if (self.gcc_except_tab_section_index == null) {861 if (self.gcc_except_tab_section_index == null) {
862 self.gcc_except_tab_section_index = @intCast(u16, text_seg.sections.items.len);862 self.gcc_except_tab_section_index = @intCast(u16, text_seg.sections.items.len);
863 try text_seg.addSection(self.allocator, "__gcc_except_tab", "__TEXT", .{});863 try text_seg.addSection(self.allocator, "__gcc_except_tab", .{});
864 }864 }
865865
866 break :blk .{866 break :blk .{
...@@ -870,7 +870,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -870,7 +870,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
870 } else if (mem.eql(u8, sectname, "__objc_methlist")) {870 } else if (mem.eql(u8, sectname, "__objc_methlist")) {
871 if (self.objc_methlist_section_index == null) {871 if (self.objc_methlist_section_index == null) {
872 self.objc_methlist_section_index = @intCast(u16, text_seg.sections.items.len);872 self.objc_methlist_section_index = @intCast(u16, text_seg.sections.items.len);
873 try text_seg.addSection(self.allocator, "__objc_methlist", "__TEXT", .{});873 try text_seg.addSection(self.allocator, "__objc_methlist", .{});
874 }874 }
875875
876 break :blk .{876 break :blk .{
...@@ -880,7 +880,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -880,7 +880,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
880 } else {880 } else {
881 if (self.text_const_section_index == null) {881 if (self.text_const_section_index == null) {
882 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);882 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);
883 try text_seg.addSection(self.allocator, "__const", "__TEXT", .{});883 try text_seg.addSection(self.allocator, "__const", .{});
884 }884 }
885885
886 break :blk .{886 break :blk .{
...@@ -893,7 +893,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -893,7 +893,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
893 if (mem.eql(u8, segname, "__DATA_CONST")) {893 if (mem.eql(u8, segname, "__DATA_CONST")) {
894 if (self.data_const_section_index == null) {894 if (self.data_const_section_index == null) {
895 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);895 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
896 try data_const_seg.addSection(self.allocator, "__const", "__DATA_CONST", .{});896 try data_const_seg.addSection(self.allocator, "__const", .{});
897 }897 }
898898
899 break :blk .{899 break :blk .{
...@@ -906,7 +906,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -906,7 +906,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
906 if (mem.eql(u8, sectname, "__const")) {906 if (mem.eql(u8, sectname, "__const")) {
907 if (self.data_const_section_index == null) {907 if (self.data_const_section_index == null) {
908 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);908 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
909 try data_const_seg.addSection(self.allocator, "__const", "__DATA_CONST", .{});909 try data_const_seg.addSection(self.allocator, "__const", .{});
910 }910 }
911911
912 break :blk .{912 break :blk .{
...@@ -916,7 +916,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -916,7 +916,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
916 } else if (mem.eql(u8, sectname, "__cfstring")) {916 } else if (mem.eql(u8, sectname, "__cfstring")) {
917 if (self.objc_cfstring_section_index == null) {917 if (self.objc_cfstring_section_index == null) {
918 self.objc_cfstring_section_index = @intCast(u16, data_const_seg.sections.items.len);918 self.objc_cfstring_section_index = @intCast(u16, data_const_seg.sections.items.len);
919 try data_const_seg.addSection(self.allocator, "__cfstring", "__DATA_CONST", .{});919 try data_const_seg.addSection(self.allocator, "__cfstring", .{});
920 }920 }
921921
922 break :blk .{922 break :blk .{
...@@ -926,7 +926,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -926,7 +926,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
926 } else if (mem.eql(u8, sectname, "__objc_classlist")) {926 } else if (mem.eql(u8, sectname, "__objc_classlist")) {
927 if (self.objc_classlist_section_index == null) {927 if (self.objc_classlist_section_index == null) {
928 self.objc_classlist_section_index = @intCast(u16, data_const_seg.sections.items.len);928 self.objc_classlist_section_index = @intCast(u16, data_const_seg.sections.items.len);
929 try data_const_seg.addSection(self.allocator, "__objc_classlist", "__DATA_CONST", .{});929 try data_const_seg.addSection(self.allocator, "__objc_classlist", .{});
930 }930 }
931931
932 break :blk .{932 break :blk .{
...@@ -936,7 +936,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -936,7 +936,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
936 } else if (mem.eql(u8, sectname, "__objc_imageinfo")) {936 } else if (mem.eql(u8, sectname, "__objc_imageinfo")) {
937 if (self.objc_imageinfo_section_index == null) {937 if (self.objc_imageinfo_section_index == null) {
938 self.objc_imageinfo_section_index = @intCast(u16, data_const_seg.sections.items.len);938 self.objc_imageinfo_section_index = @intCast(u16, data_const_seg.sections.items.len);
939 try data_const_seg.addSection(self.allocator, "__objc_imageinfo", "__DATA_CONST", .{});939 try data_const_seg.addSection(self.allocator, "__objc_imageinfo", .{});
940 }940 }
941941
942 break :blk .{942 break :blk .{
...@@ -946,7 +946,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -946,7 +946,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
946 } else if (mem.eql(u8, sectname, "__objc_const")) {946 } else if (mem.eql(u8, sectname, "__objc_const")) {
947 if (self.objc_const_section_index == null) {947 if (self.objc_const_section_index == null) {
948 self.objc_const_section_index = @intCast(u16, data_seg.sections.items.len);948 self.objc_const_section_index = @intCast(u16, data_seg.sections.items.len);
949 try data_seg.addSection(self.allocator, "__objc_const", "__DATA", .{});949 try data_seg.addSection(self.allocator, "__objc_const", .{});
950 }950 }
951951
952 break :blk .{952 break :blk .{
...@@ -956,7 +956,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -956,7 +956,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
956 } else if (mem.eql(u8, sectname, "__objc_classrefs")) {956 } else if (mem.eql(u8, sectname, "__objc_classrefs")) {
957 if (self.objc_classrefs_section_index == null) {957 if (self.objc_classrefs_section_index == null) {
958 self.objc_classrefs_section_index = @intCast(u16, data_seg.sections.items.len);958 self.objc_classrefs_section_index = @intCast(u16, data_seg.sections.items.len);
959 try data_seg.addSection(self.allocator, "__objc_classrefs", "__DATA", .{});959 try data_seg.addSection(self.allocator, "__objc_classrefs", .{});
960 }960 }
961961
962 break :blk .{962 break :blk .{
...@@ -966,7 +966,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -966,7 +966,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
966 } else if (mem.eql(u8, sectname, "__objc_data")) {966 } else if (mem.eql(u8, sectname, "__objc_data")) {
967 if (self.objc_data_section_index == null) {967 if (self.objc_data_section_index == null) {
968 self.objc_data_section_index = @intCast(u16, data_seg.sections.items.len);968 self.objc_data_section_index = @intCast(u16, data_seg.sections.items.len);
969 try data_seg.addSection(self.allocator, "__objc_data", "__DATA", .{});969 try data_seg.addSection(self.allocator, "__objc_data", .{});
970 }970 }
971971
972 break :blk .{972 break :blk .{
...@@ -976,7 +976,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {...@@ -976,7 +976,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
976 } else {976 } else {
977 if (self.data_section_index == null) {977 if (self.data_section_index == null) {
978 self.data_section_index = @intCast(u16, data_seg.sections.items.len);978 self.data_section_index = @intCast(u16, data_seg.sections.items.len);
979 try data_seg.addSection(self.allocator, "__data", "__DATA", .{});979 try data_seg.addSection(self.allocator, "__data", .{});
980 }980 }
981981
982 break :blk .{982 break :blk .{
...@@ -2124,7 +2124,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2124,7 +2124,7 @@ fn populateMetadata(self: *Zld) !void {
2124 .aarch64 => 2,2124 .aarch64 => 2,
2125 else => unreachable, // unhandled architecture type2125 else => unreachable, // unhandled architecture type
2126 };2126 };
2127 try text_seg.addSection(self.allocator, "__text", "__TEXT", .{2127 try text_seg.addSection(self.allocator, "__text", .{
2128 .@"align" = alignment,2128 .@"align" = alignment,
2129 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,2129 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
2130 });2130 });
...@@ -2143,7 +2143,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2143,7 +2143,7 @@ fn populateMetadata(self: *Zld) !void {
2143 .aarch64 => 3 * @sizeOf(u32),2143 .aarch64 => 3 * @sizeOf(u32),
2144 else => unreachable, // unhandled architecture type2144 else => unreachable, // unhandled architecture type
2145 };2145 };
2146 try text_seg.addSection(self.allocator, "__stubs", "__TEXT", .{2146 try text_seg.addSection(self.allocator, "__stubs", .{
2147 .@"align" = alignment,2147 .@"align" = alignment,
2148 .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,2148 .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
2149 .reserved2 = stub_size,2149 .reserved2 = stub_size,
...@@ -2163,7 +2163,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2163,7 +2163,7 @@ fn populateMetadata(self: *Zld) !void {
2163 .aarch64 => 6 * @sizeOf(u32),2163 .aarch64 => 6 * @sizeOf(u32),
2164 else => unreachable,2164 else => unreachable,
2165 };2165 };
2166 try text_seg.addSection(self.allocator, "__stub_helper", "__TEXT", .{2166 try text_seg.addSection(self.allocator, "__stub_helper", .{
2167 .size = stub_helper_size,2167 .size = stub_helper_size,
2168 .@"align" = alignment,2168 .@"align" = alignment,
2169 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,2169 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
...@@ -2183,7 +2183,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2183,7 +2183,7 @@ fn populateMetadata(self: *Zld) !void {
2183 if (self.got_section_index == null) {2183 if (self.got_section_index == null) {
2184 const data_const_seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;2184 const data_const_seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
2185 self.got_section_index = @intCast(u16, data_const_seg.sections.items.len);2185 self.got_section_index = @intCast(u16, data_const_seg.sections.items.len);
2186 try data_const_seg.addSection(self.allocator, "__got", "__DATA_CONST", .{2186 try data_const_seg.addSection(self.allocator, "__got", .{
2187 .@"align" = 3, // 2^3 = @sizeOf(u64)2187 .@"align" = 3, // 2^3 = @sizeOf(u64)
2188 .flags = macho.S_NON_LAZY_SYMBOL_POINTERS,2188 .flags = macho.S_NON_LAZY_SYMBOL_POINTERS,
2189 });2189 });
...@@ -2202,7 +2202,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2202,7 +2202,7 @@ fn populateMetadata(self: *Zld) !void {
2202 if (self.la_symbol_ptr_section_index == null) {2202 if (self.la_symbol_ptr_section_index == null) {
2203 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;2203 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2204 self.la_symbol_ptr_section_index = @intCast(u16, data_seg.sections.items.len);2204 self.la_symbol_ptr_section_index = @intCast(u16, data_seg.sections.items.len);
2205 try data_seg.addSection(self.allocator, "__la_symbol_ptr", "__DATA", .{2205 try data_seg.addSection(self.allocator, "__la_symbol_ptr", .{
2206 .@"align" = 3, // 2^3 = @sizeOf(u64)2206 .@"align" = 3, // 2^3 = @sizeOf(u64)
2207 .flags = macho.S_LAZY_SYMBOL_POINTERS,2207 .flags = macho.S_LAZY_SYMBOL_POINTERS,
2208 });2208 });
...@@ -2211,7 +2211,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -2211,7 +2211,7 @@ fn populateMetadata(self: *Zld) !void {
2211 if (self.data_section_index == null) {2211 if (self.data_section_index == null) {
2212 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;2212 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2213 self.data_section_index = @intCast(u16, data_seg.sections.items.len);2213 self.data_section_index = @intCast(u16, data_seg.sections.items.len);
2214 try data_seg.addSection(self.allocator, "__data", "__DATA", .{2214 try data_seg.addSection(self.allocator, "__data", .{
2215 .@"align" = 3, // 2^3 = @sizeOf(u64)2215 .@"align" = 3, // 2^3 = @sizeOf(u64)
2216 });2216 });
2217 }2217 }
src/link/MachO/commands.zig+5-4
...@@ -233,12 +233,11 @@ pub const SegmentCommand = struct {...@@ -233,12 +233,11 @@ pub const SegmentCommand = struct {
233 self: *SegmentCommand,233 self: *SegmentCommand,
234 alloc: *Allocator,234 alloc: *Allocator,
235 comptime sectname: []const u8,235 comptime sectname: []const u8,
236 comptime segname: []const u8,
237 opts: SectionOptions,236 opts: SectionOptions,
238 ) !void {237 ) !void {
239 try self.sections.append(alloc, .{238 var section = macho.section_64{
240 .sectname = makeStaticString(sectname),239 .sectname = makeStaticString(sectname),
241 .segname = makeStaticString(segname),240 .segname = undefined,
242 .addr = opts.addr,241 .addr = opts.addr,
243 .size = opts.size,242 .size = opts.size,
244 .offset = opts.offset,243 .offset = opts.offset,
...@@ -249,7 +248,9 @@ pub const SegmentCommand = struct {...@@ -249,7 +248,9 @@ pub const SegmentCommand = struct {
249 .reserved1 = opts.reserved1,248 .reserved1 = opts.reserved1,
250 .reserved2 = opts.reserved2,249 .reserved2 = opts.reserved2,
251 .reserved3 = opts.reserved3,250 .reserved3 = opts.reserved3,
252 });251 };
252 mem.copy(u8, &section.segname, &self.inner.segname);
253 try self.sections.append(alloc, section);
253 self.inner.cmdsize += @sizeOf(macho.section_64);254 self.inner.cmdsize += @sizeOf(macho.section_64);
254 self.inner.nsects += 1;255 self.inner.nsects += 1;
255 }256 }