| ... | @@ -18,36 +18,66 @@ const File = link.File; | ... | @@ -18,36 +18,66 @@ const File = link.File; |
| 18 | | 18 | |
| 19 | pub const base_tag: File.Tag = File.Tag.macho; | 19 | pub const base_tag: File.Tag = File.Tag.macho; |
| 20 | | 20 | |
| | 21 | const LoadCommand = union(enum) { |
| | 22 | Segment: macho.segment_command_64, |
| | 23 | LinkeditData: macho.linkedit_data_command, |
| | 24 | Symtab: macho.symtab_command, |
| | 25 | Dysymtab: macho.dysymtab_command, |
| | 26 | |
| | 27 | pub fn cmdsize(self: LoadCommand) u32 { |
| | 28 | return switch (self) { |
| | 29 | .Segment => |x| x.cmdsize, |
| | 30 | .LinkeditData => |x| x.cmdsize, |
| | 31 | .Symtab => |x| x.cmdsize, |
| | 32 | .Dysymtab => |x| x.cmdsize, |
| | 33 | }; |
| | 34 | } |
| | 35 | }; |
| | 36 | |
| 21 | base: File, | 37 | base: File, |
| 22 | | 38 | |
| 23 | /// List of all load command headers that are in the file. | 39 | /// Table of all load commands |
| 24 | /// We use it to track number and size of all commands needed by the header. | 40 | load_commands: std.ArrayListUnmanaged(LoadCommand) = .{}, |
| 25 | commands: std.ArrayListUnmanaged(macho.load_command) = std.ArrayListUnmanaged(macho.load_command){}, | 41 | segment_cmd_index: ?u16 = null, |
| 26 | command_file_offset: ?u64 = null, | 42 | symtab_cmd_index: ?u16 = null, |
| | 43 | dysymtab_cmd_index: ?u16 = null, |
| | 44 | data_in_code_cmd_index: ?u16 = null, |
| 27 | | 45 | |
| 28 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. | 46 | /// Table of all sections |
| 29 | /// Same order as in the file. | 47 | sections: std.ArrayListUnmanaged(macho.section_64) = .{}, |
| 30 | segments: std.ArrayListUnmanaged(macho.segment_command_64) = std.ArrayListUnmanaged(macho.segment_command_64){}, | | |
| 31 | /// Section (headers) *always* follow segment (load commands) directly! | | |
| 32 | sections: std.ArrayListUnmanaged(macho.section_64) = std.ArrayListUnmanaged(macho.section_64){}, | | |
| 33 | | 48 | |
| 34 | /// Offset (index) into __TEXT segment load command. | 49 | /// __TEXT segment sections |
| 35 | text_segment_offset: ?u64 = null, | 50 | text_section_index: ?u16 = null, |
| 36 | /// Offset (index) into __LINKEDIT segment load command. | 51 | cstring_section_index: ?u16 = null, |
| 37 | linkedit_segment_offset: ?u664 = null, | 52 | const_text_section_index: ?u16 = null, |
| | 53 | stubs_section_index: ?u16 = null, |
| | 54 | stub_helper_section_index: ?u16 = null, |
| | 55 | |
| | 56 | /// __DATA segment sections |
| | 57 | got_section_index: ?u16 = null, |
| | 58 | const_data_section_index: ?u16 = null, |
| 38 | | 59 | |
| 39 | /// Entry point load command | | |
| 40 | entry_point_cmd: ?macho.entry_point_command = null, | | |
| 41 | entry_addr: ?u64 = null, | 60 | entry_addr: ?u64 = null, |
| 42 | | 61 | |
| 43 | /// The first 4GB of process' memory is reserved for the null (__PAGEZERO) segment. | 62 | /// Table of all symbols used. |
| 44 | /// This is also the start address for our binary. | 63 | /// Internally references string table for names (which are optional). |
| 45 | vm_start_address: u64 = 0x100000000, | 64 | symbol_table: std.ArrayListUnmanaged(macho.nlist_64) = .{}, |
| | 65 | |
| | 66 | /// Table of symbol names aka the string table. |
| | 67 | string_table: std.ArrayListUnmanaged(u8) = .{}, |
| 46 | | 68 | |
| 47 | seg_table_dirty: bool = false, | 69 | /// Table of symbol vaddr values. The values is the absolute vaddr value. |
| | 70 | /// If the vaddr of the executable __TEXT segment vaddr changes, the entire offset |
| | 71 | /// table needs to be rewritten. |
| | 72 | offset_table: std.ArrayListUnmanaged(u64) = .{}, |
| 48 | | 73 | |
| 49 | error_flags: File.ErrorFlags = File.ErrorFlags{}, | 74 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 50 | | 75 | |
| | 76 | cmd_table_dirty: bool = false, |
| | 77 | |
| | 78 | /// Pointer to the last allocated text block |
| | 79 | last_text_block: ?*TextBlock = null, |
| | 80 | |
| 51 | /// `alloc_num / alloc_den` is the factor of padding when allocating. | 81 | /// `alloc_num / alloc_den` is the factor of padding when allocating. |
| 52 | const alloc_num = 4; | 82 | const alloc_num = 4; |
| 53 | const alloc_den = 3; | 83 | const alloc_den = 3; |
| ... | @@ -67,7 +97,23 @@ const LIB_SYSTEM_NAME: [*:0]const u8 = "System"; | ... | @@ -67,7 +97,23 @@ const LIB_SYSTEM_NAME: [*:0]const u8 = "System"; |
| 67 | const LIB_SYSTEM_PATH: [*:0]const u8 = DEFAULT_LIB_SEARCH_PATH ++ "/libSystem.B.dylib"; | 97 | const LIB_SYSTEM_PATH: [*:0]const u8 = DEFAULT_LIB_SEARCH_PATH ++ "/libSystem.B.dylib"; |
| 68 | | 98 | |
| 69 | pub const TextBlock = struct { | 99 | pub const TextBlock = struct { |
| 70 | pub const empty = TextBlock{}; | 100 | /// Index into the symbol table |
| | 101 | symbol_table_index: ?u32, |
| | 102 | /// Index into offset table |
| | 103 | offset_table_index: ?u32, |
| | 104 | /// Size of this text block |
| | 105 | size: u64, |
| | 106 | /// Points to the previous and next neighbours |
| | 107 | prev: ?*TextBlock, |
| | 108 | next: ?*TextBlock, |
| | 109 | |
| | 110 | pub const empty = TextBlock{ |
| | 111 | .symbol_table_index = null, |
| | 112 | .offset_table_index = null, |
| | 113 | .size = 0, |
| | 114 | .prev = null, |
| | 115 | .next = null, |
| | 116 | }; |
| 71 | }; | 117 | }; |
| 72 | | 118 | |
| 73 | pub const SrcFn = struct { | 119 | pub const SrcFn = struct { |
| ... | @@ -117,6 +163,12 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO | ... | @@ -117,6 +163,12 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO |
| 117 | /// Truncates the existing file contents and overwrites the contents. | 163 | /// Truncates the existing file contents and overwrites the contents. |
| 118 | /// Returns an error if `file` is not already open with +read +write +seek abilities. | 164 | /// Returns an error if `file` is not already open with +read +write +seek abilities. |
| 119 | fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO { | 165 | fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO { |
| | 166 | switch (options.output_mode) { |
| | 167 | .Exe => {}, |
| | 168 | .Obj => {}, |
| | 169 | .Lib => return error.TODOImplementWritingLibFiles, |
| | 170 | } |
| | 171 | |
| 120 | var self: MachO = .{ | 172 | var self: MachO = .{ |
| 121 | .base = .{ | 173 | .base = .{ |
| 122 | .file = file, | 174 | .file = file, |
| ... | @@ -127,104 +179,15 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Mach | ... | @@ -127,104 +179,15 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Mach |
| 127 | }; | 179 | }; |
| 128 | errdefer self.deinit(); | 180 | errdefer self.deinit(); |
| 129 | | 181 | |
| 130 | switch (options.output_mode) { | | |
| 131 | .Exe => { | | |
| 132 | // The first segment command for executables is always a __PAGEZERO segment. | | |
| 133 | const pagezero = .{ | | |
| 134 | .cmd = macho.LC_SEGMENT_64, | | |
| 135 | .cmdsize = commandSize(@sizeOf(macho.segment_command_64)), | | |
| 136 | .segname = makeString("__PAGEZERO"), | | |
| 137 | .vmaddr = 0, | | |
| 138 | .vmsize = self.vm_start_address, | | |
| 139 | .fileoff = 0, | | |
| 140 | .filesize = 0, | | |
| 141 | .maxprot = macho.VM_PROT_NONE, | | |
| 142 | .initprot = macho.VM_PROT_NONE, | | |
| 143 | .nsects = 0, | | |
| 144 | .flags = 0, | | |
| 145 | }; | | |
| 146 | try self.commands.append(allocator, .{ | | |
| 147 | .cmd = pagezero.cmd, | | |
| 148 | .cmdsize = pagezero.cmdsize, | | |
| 149 | }); | | |
| 150 | try self.segments.append(allocator, pagezero); | | |
| 151 | }, | | |
| 152 | .Obj => return error.TODOImplementWritingObjFiles, | | |
| 153 | .Lib => return error.TODOImplementWritingLibFiles, | | |
| 154 | } | | |
| 155 | | | |
| 156 | try self.populateMissingMetadata(); | 182 | try self.populateMissingMetadata(); |
| 157 | | 183 | |
| 158 | return self; | 184 | return self; |
| 159 | } | 185 | } |
| 160 | | 186 | |
| 161 | fn writeMachOHeader(self: *MachO) !void { | | |
| 162 | var hdr: macho.mach_header_64 = undefined; | | |
| 163 | hdr.magic = macho.MH_MAGIC_64; | | |
| 164 | | | |
| 165 | const CpuInfo = struct { | | |
| 166 | cpu_type: macho.cpu_type_t, | | |
| 167 | cpu_subtype: macho.cpu_subtype_t, | | |
| 168 | }; | | |
| 169 | | | |
| 170 | const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) { | | |
| 171 | .aarch64 => .{ | | |
| 172 | .cpu_type = macho.CPU_TYPE_ARM64, | | |
| 173 | .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL, | | |
| 174 | }, | | |
| 175 | .x86_64 => .{ | | |
| 176 | .cpu_type = macho.CPU_TYPE_X86_64, | | |
| 177 | .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL, | | |
| 178 | }, | | |
| 179 | else => return error.UnsupportedMachOArchitecture, | | |
| 180 | }; | | |
| 181 | hdr.cputype = cpu_info.cpu_type; | | |
| 182 | hdr.cpusubtype = cpu_info.cpu_subtype; | | |
| 183 | | | |
| 184 | const filetype: u32 = switch (self.base.options.output_mode) { | | |
| 185 | .Exe => macho.MH_EXECUTE, | | |
| 186 | .Obj => macho.MH_OBJECT, | | |
| 187 | .Lib => switch (self.base.options.link_mode) { | | |
| 188 | .Static => return error.TODOStaticLibMachOType, | | |
| 189 | .Dynamic => macho.MH_DYLIB, | | |
| 190 | }, | | |
| 191 | }; | | |
| 192 | hdr.filetype = filetype; | | |
| 193 | | | |
| 194 | const ncmds = try math.cast(u32, self.commands.items.len); | | |
| 195 | hdr.ncmds = ncmds; | | |
| 196 | | | |
| 197 | var sizeof_cmds: u32 = 0; | | |
| 198 | for (self.commands.items) |cmd| { | | |
| 199 | sizeof_cmds += cmd.cmdsize; | | |
| 200 | } | | |
| 201 | hdr.sizeofcmds = sizeof_cmds; | | |
| 202 | | | |
| 203 | // TODO should these be set to something else? | | |
| 204 | hdr.flags = 0; | | |
| 205 | hdr.reserved = 0; | | |
| 206 | | | |
| 207 | try self.base.file.?.pwriteAll(@ptrCast([*]const u8, &hdr)[0..@sizeOf(macho.mach_header_64)], 0); | | |
| 208 | } | | |
| 209 | | | |
| 210 | pub fn flush(self: *MachO, module: *Module) !void { | 187 | pub fn flush(self: *MachO, module: *Module) !void { |
| 211 | // Save segments first | | |
| 212 | { | | |
| 213 | const buf = try self.base.allocator.alloc(macho.segment_command_64, self.segments.items.len); | | |
| 214 | defer self.base.allocator.free(buf); | | |
| 215 | | | |
| 216 | self.command_file_offset = @sizeOf(macho.mach_header_64); | | |
| 217 | | | |
| 218 | for (buf) |*seg, i| { | | |
| 219 | seg.* = self.segments.items[i]; | | |
| 220 | self.command_file_offset.? += self.segments.items[i].cmdsize; | | |
| 221 | } | | |
| 222 | | | |
| 223 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), @sizeOf(macho.mach_header_64)); | | |
| 224 | } | | |
| 225 | | | |
| 226 | switch (self.base.options.output_mode) { | 188 | switch (self.base.options.output_mode) { |
| 227 | .Exe => { | 189 | .Exe => { |
| | 190 | var last_cmd_offset: usize = @sizeOf(macho.mach_header_64); |
| 228 | { | 191 | { |
| 229 | // Specify path to dynamic linker dyld | 192 | // Specify path to dynamic linker dyld |
| 230 | const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH)); | 193 | const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH)); |
| ... | @@ -235,18 +198,14 @@ pub fn flush(self: *MachO, module: *Module) !void { | ... | @@ -235,18 +198,14 @@ pub fn flush(self: *MachO, module: *Module) !void { |
| 235 | .name = @sizeOf(macho.dylinker_command), | 198 | .name = @sizeOf(macho.dylinker_command), |
| 236 | }, | 199 | }, |
| 237 | }; | 200 | }; |
| 238 | try self.commands.append(self.base.allocator, .{ | | |
| 239 | .cmd = macho.LC_LOAD_DYLINKER, | | |
| 240 | .cmdsize = cmdsize, | | |
| 241 | }); | | |
| 242 | | 201 | |
| 243 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?); | 202 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), last_cmd_offset); |
| 244 | | 203 | |
| 245 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command); | 204 | const file_offset = last_cmd_offset + @sizeOf(macho.dylinker_command); |
| 246 | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); | 205 | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); |
| 247 | | 206 | |
| 248 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); | 207 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); |
| 249 | self.command_file_offset.? += cmdsize; | 208 | last_cmd_offset += cmdsize; |
| 250 | } | 209 | } |
| 251 | | 210 | |
| 252 | { | 211 | { |
| ... | @@ -268,21 +227,44 @@ pub fn flush(self: *MachO, module: *Module) !void { | ... | @@ -268,21 +227,44 @@ pub fn flush(self: *MachO, module: *Module) !void { |
| 268 | .dylib = dylib, | 227 | .dylib = dylib, |
| 269 | }, | 228 | }, |
| 270 | }; | 229 | }; |
| 271 | try self.commands.append(self.base.allocator, .{ | | |
| 272 | .cmd = macho.LC_LOAD_DYLIB, | | |
| 273 | .cmdsize = cmdsize, | | |
| 274 | }); | | |
| 275 | | 230 | |
| 276 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?); | 231 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), last_cmd_offset); |
| 277 | | 232 | |
| 278 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command); | 233 | const file_offset = last_cmd_offset + @sizeOf(macho.dylib_command); |
| 279 | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); | 234 | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); |
| 280 | | 235 | |
| 281 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); | 236 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); |
| 282 | self.command_file_offset.? += cmdsize; | 237 | last_cmd_offset += cmdsize; |
| 283 | } | 238 | } |
| 284 | }, | 239 | }, |
| 285 | .Obj => return error.TODOImplementWritingObjFiles, | 240 | .Obj => { |
| | 241 | { |
| | 242 | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| | 243 | symtab.nsyms = @intCast(u32, self.symbol_table.items.len); |
| | 244 | const allocated_size = self.allocatedSize(symtab.stroff); |
| | 245 | const needed_size = self.string_table.items.len; |
| | 246 | log.debug("allocated_size = 0x{x}, needed_size = 0x{x}\n", .{ allocated_size, needed_size }); |
| | 247 | |
| | 248 | if (needed_size > allocated_size) { |
| | 249 | symtab.strsize = 0; |
| | 250 | symtab.stroff = @intCast(u32, self.findFreeSpace(needed_size, 1)); |
| | 251 | } |
| | 252 | symtab.strsize = @intCast(u32, needed_size); |
| | 253 | |
| | 254 | log.debug("writing string table from 0x{x} to 0x{x}\n", .{ symtab.stroff, symtab.stroff + symtab.strsize }); |
| | 255 | |
| | 256 | try self.base.file.?.pwriteAll(self.string_table.items, symtab.stroff); |
| | 257 | } |
| | 258 | |
| | 259 | var last_cmd_offset: usize = @sizeOf(macho.mach_header_64); |
| | 260 | for (self.load_commands.items) |cmd| { |
| | 261 | const cmd_to_write = [1]@TypeOf(cmd){cmd}; |
| | 262 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(cmd_to_write[0..1]), last_cmd_offset); |
| | 263 | last_cmd_offset += cmd.cmdsize(); |
| | 264 | } |
| | 265 | const off = @sizeOf(macho.mach_header_64) + @sizeOf(macho.segment_command_64); |
| | 266 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items), off); |
| | 267 | }, |
| 286 | .Lib => return error.TODOImplementWritingLibFiles, | 268 | .Lib => return error.TODOImplementWritingLibFiles, |
| 287 | } | 269 | } |
| 288 | | 270 | |
| ... | @@ -297,14 +279,110 @@ pub fn flush(self: *MachO, module: *Module) !void { | ... | @@ -297,14 +279,110 @@ pub fn flush(self: *MachO, module: *Module) !void { |
| 297 | } | 279 | } |
| 298 | | 280 | |
| 299 | pub fn deinit(self: *MachO) void { | 281 | pub fn deinit(self: *MachO) void { |
| 300 | self.commands.deinit(self.base.allocator); | 282 | self.offset_table.deinit(self.base.allocator); |
| 301 | self.segments.deinit(self.base.allocator); | 283 | self.string_table.deinit(self.base.allocator); |
| | 284 | self.symbol_table.deinit(self.base.allocator); |
| 302 | self.sections.deinit(self.base.allocator); | 285 | self.sections.deinit(self.base.allocator); |
| | 286 | self.load_commands.deinit(self.base.allocator); |
| | 287 | } |
| | 288 | |
| | 289 | pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void { |
| | 290 | if (decl.link.macho.symbol_table_index) |_| return; |
| | 291 | |
| | 292 | try self.symbol_table.ensureCapacity(self.base.allocator, self.symbol_table.items.len + 1); |
| | 293 | try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1); |
| | 294 | |
| | 295 | log.debug("allocating symbol index {} for {}\n", .{ self.symbol_table.items.len, decl.name }); |
| | 296 | decl.link.macho.symbol_table_index = @intCast(u32, self.symbol_table.items.len); |
| | 297 | _ = self.symbol_table.addOneAssumeCapacity(); |
| | 298 | |
| | 299 | decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len); |
| | 300 | _ = self.offset_table.addOneAssumeCapacity(); |
| | 301 | |
| | 302 | self.symbol_table.items[decl.link.macho.symbol_table_index.?] = .{ |
| | 303 | .n_strx = 0, |
| | 304 | .n_type = 0, |
| | 305 | .n_sect = 0, |
| | 306 | .n_desc = 0, |
| | 307 | .n_value = 0, |
| | 308 | }; |
| | 309 | self.offset_table.items[decl.link.macho.offset_table_index.?] = 0; |
| 303 | } | 310 | } |
| 304 | | 311 | |
| 305 | pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {} | 312 | pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void { |
| | 313 | const tracy = trace(@src()); |
| | 314 | defer tracy.end(); |
| | 315 | |
| | 316 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| | 317 | defer code_buffer.deinit(); |
| 306 | | 318 | |
| 307 | pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {} | 319 | var dbg_line_buffer = std.ArrayList(u8).init(self.base.allocator); |
| | 320 | defer dbg_line_buffer.deinit(); |
| | 321 | |
| | 322 | var dbg_info_buffer = std.ArrayList(u8).init(self.base.allocator); |
| | 323 | defer dbg_info_buffer.deinit(); |
| | 324 | |
| | 325 | var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{}; |
| | 326 | defer { |
| | 327 | var it = dbg_info_type_relocs.iterator(); |
| | 328 | while (it.next()) |entry| { |
| | 329 | entry.value.relocs.deinit(self.base.allocator); |
| | 330 | } |
| | 331 | dbg_info_type_relocs.deinit(self.base.allocator); |
| | 332 | } |
| | 333 | |
| | 334 | const typed_value = decl.typed_value.most_recent.typed_value; |
| | 335 | const res = try codegen.generateSymbol( |
| | 336 | &self.base, |
| | 337 | decl.src(), |
| | 338 | typed_value, |
| | 339 | &code_buffer, |
| | 340 | &dbg_line_buffer, |
| | 341 | &dbg_info_buffer, |
| | 342 | &dbg_info_type_relocs, |
| | 343 | ); |
| | 344 | |
| | 345 | const code = switch (res) { |
| | 346 | .externally_managed => |x| x, |
| | 347 | .appended => code_buffer.items, |
| | 348 | .fail => |em| { |
| | 349 | decl.analysis = .codegen_failure; |
| | 350 | try module.failed_decls.put(module.gpa, decl, em); |
| | 351 | return; |
| | 352 | }, |
| | 353 | }; |
| | 354 | log.debug("generated code {}\n", .{code}); |
| | 355 | |
| | 356 | const required_alignment = typed_value.ty.abiAlignment(self.base.options.target); |
| | 357 | const symbol = &self.symbol_table.items[decl.link.macho.symbol_table_index.?]; |
| | 358 | |
| | 359 | const decl_name = mem.spanZ(decl.name); |
| | 360 | const name_str_index = try self.makeString(decl_name); |
| | 361 | const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment); |
| | 362 | log.debug("allocated text block for {} at 0x{x}\n", .{ decl_name, addr }); |
| | 363 | log.debug("updated text section {}\n", .{self.sections.items[self.text_section_index.?]}); |
| | 364 | |
| | 365 | symbol.* = .{ |
| | 366 | .n_strx = name_str_index, |
| | 367 | .n_type = macho.N_SECT, |
| | 368 | .n_sect = @intCast(u8, self.text_section_index.?) + 1, |
| | 369 | .n_desc = 0, |
| | 370 | .n_value = addr, |
| | 371 | }; |
| | 372 | self.offset_table.items[decl.link.macho.offset_table_index.?] = addr; |
| | 373 | |
| | 374 | try self.writeSymbol(decl.link.macho.symbol_table_index.?); |
| | 375 | |
| | 376 | const text_section = self.sections.items[self.text_section_index.?]; |
| | 377 | const section_offset = symbol.n_value - text_section.addr; |
| | 378 | const file_offset = text_section.offset + section_offset; |
| | 379 | log.debug("file_offset 0x{x}\n", .{file_offset}); |
| | 380 | try self.base.file.?.pwriteAll(code, file_offset); |
| | 381 | |
| | 382 | // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated. |
| | 383 | const decl_exports = module.decl_exports.get(decl) orelse &[0]*Module.Export{}; |
| | 384 | return self.updateDeclExports(module, decl, decl_exports); |
| | 385 | } |
| 308 | | 386 | |
| 309 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {} | 387 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {} |
| 310 | | 388 | |
| ... | @@ -313,51 +391,191 @@ pub fn updateDeclExports( | ... | @@ -313,51 +391,191 @@ pub fn updateDeclExports( |
| 313 | module: *Module, | 391 | module: *Module, |
| 314 | decl: *const Module.Decl, | 392 | decl: *const Module.Decl, |
| 315 | exports: []const *Module.Export, | 393 | exports: []const *Module.Export, |
| 316 | ) !void {} | 394 | ) !void { |
| | 395 | const tracy = trace(@src()); |
| | 396 | defer tracy.end(); |
| | 397 | |
| | 398 | if (decl.link.macho.symbol_table_index == null) return; |
| | 399 | |
| | 400 | var decl_sym = self.symbol_table.items[decl.link.macho.symbol_table_index.?]; |
| | 401 | // TODO implement |
| | 402 | if (exports.len == 0) return; |
| | 403 | |
| | 404 | const exp = exports[0]; |
| | 405 | self.entry_addr = decl_sym.n_value; |
| | 406 | decl_sym.n_type |= macho.N_EXT; |
| | 407 | exp.link.sym_index = 0; |
| | 408 | } |
| 317 | | 409 | |
| 318 | pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {} | 410 | pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {} |
| 319 | | 411 | |
| 320 | pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 { | 412 | pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 { |
| 321 | @panic("TODO implement getDeclVAddr for MachO"); | 413 | return self.symbol_table.items[decl.link.macho.symbol_table_index.?].n_value; |
| 322 | } | 414 | } |
| 323 | | 415 | |
| 324 | pub fn populateMissingMetadata(self: *MachO) !void { | 416 | pub fn populateMissingMetadata(self: *MachO) !void { |
| 325 | if (self.text_segment_offset == null) { | 417 | if (self.segment_cmd_index == null) { |
| 326 | self.text_segment_offset = @intCast(u64, self.segments.items.len); | 418 | self.segment_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 327 | const file_size = alignSize(u64, self.base.options.program_code_size_hint, 0x1000); | 419 | try self.load_commands.append(self.base.allocator, .{ |
| 328 | log.debug("vmsize/filesize = {}", .{file_size}); | 420 | .Segment = .{ |
| 329 | const file_offset = 0; | 421 | .cmd = macho.LC_SEGMENT_64, |
| 330 | const vm_address = self.vm_start_address; // the end of __PAGEZERO segment in VM | 422 | .cmdsize = @sizeOf(macho.segment_command_64), |
| 331 | const protection = macho.VM_PROT_READ | macho.VM_PROT_EXECUTE; | 423 | .segname = makeStaticString(""), |
| 332 | const cmdsize = commandSize(@sizeOf(macho.segment_command_64)); | 424 | .vmaddr = 0, |
| 333 | const text_segment = .{ | 425 | .vmsize = 0, |
| 334 | .cmd = macho.LC_SEGMENT_64, | 426 | .fileoff = 0, |
| 335 | .cmdsize = cmdsize, | 427 | .filesize = 0, |
| 336 | .segname = makeString("__TEXT"), | 428 | .maxprot = 0, |
| 337 | .vmaddr = vm_address, | 429 | .initprot = 0, |
| 338 | .vmsize = file_size, | 430 | .nsects = 0, |
| 339 | .fileoff = 0, // __TEXT segment *always* starts at 0 file offset | 431 | .flags = 0, |
| 340 | .filesize = 0, //file_size, | 432 | }, |
| 341 | .maxprot = protection, | 433 | }); |
| 342 | .initprot = protection, | 434 | self.cmd_table_dirty = true; |
| 343 | .nsects = 0, | 435 | } |
| 344 | .flags = 0, | 436 | if (self.symtab_cmd_index == null) { |
| 345 | }; | 437 | self.symtab_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 346 | try self.commands.append(self.base.allocator, .{ | 438 | try self.load_commands.append(self.base.allocator, .{ |
| 347 | .cmd = macho.LC_SEGMENT_64, | 439 | .Symtab = .{ |
| 348 | .cmdsize = cmdsize, | 440 | .cmd = macho.LC_SYMTAB, |
| | 441 | .cmdsize = @sizeOf(macho.symtab_command), |
| | 442 | .symoff = 0, |
| | 443 | .nsyms = 0, |
| | 444 | .stroff = 0, |
| | 445 | .strsize = 0, |
| | 446 | }, |
| 349 | }); | 447 | }); |
| 350 | try self.segments.append(self.base.allocator, text_segment); | 448 | self.cmd_table_dirty = true; |
| | 449 | } |
| | 450 | if (self.text_section_index == null) { |
| | 451 | self.text_section_index = @intCast(u16, self.sections.items.len); |
| | 452 | const segment = &self.load_commands.items[self.segment_cmd_index.?].Segment; |
| | 453 | segment.cmdsize += @sizeOf(macho.section_64); |
| | 454 | segment.nsects += 1; |
| | 455 | |
| | 456 | const file_size = self.base.options.program_code_size_hint; |
| | 457 | const off = @intCast(u32, self.findFreeSpace(file_size, 1)); |
| | 458 | const flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS; |
| | 459 | |
| | 460 | log.debug("found __text section free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| | 461 | |
| | 462 | try self.sections.append(self.base.allocator, .{ |
| | 463 | .sectname = makeStaticString("__text"), |
| | 464 | .segname = makeStaticString("__TEXT"), |
| | 465 | .addr = 0, |
| | 466 | .size = file_size, |
| | 467 | .offset = off, |
| | 468 | .@"align" = 0x1000, |
| | 469 | .reloff = 0, |
| | 470 | .nreloc = 0, |
| | 471 | .flags = flags, |
| | 472 | .reserved1 = 0, |
| | 473 | .reserved2 = 0, |
| | 474 | .reserved3 = 0, |
| | 475 | }); |
| | 476 | |
| | 477 | segment.vmsize += file_size; |
| | 478 | segment.filesize += file_size; |
| | 479 | segment.fileoff = off; |
| | 480 | |
| | 481 | log.debug("initial text section {}\n", .{self.sections.items[self.text_section_index.?]}); |
| | 482 | } |
| | 483 | { |
| | 484 | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| | 485 | if (symtab.symoff == 0) { |
| | 486 | const p_align = @sizeOf(macho.nlist_64); |
| | 487 | const nsyms = self.base.options.symbol_count_hint; |
| | 488 | const file_size = p_align * nsyms; |
| | 489 | const off = @intCast(u32, self.findFreeSpace(file_size, p_align)); |
| | 490 | log.debug("found symbol table free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| | 491 | symtab.symoff = off; |
| | 492 | symtab.nsyms = @intCast(u32, nsyms); |
| | 493 | } |
| | 494 | if (symtab.stroff == 0) { |
| | 495 | try self.string_table.append(self.base.allocator, 0); |
| | 496 | const file_size = @intCast(u32, self.string_table.items.len); |
| | 497 | const off = @intCast(u32, self.findFreeSpace(file_size, 1)); |
| | 498 | log.debug("found string table free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| | 499 | symtab.stroff = off; |
| | 500 | symtab.strsize = file_size; |
| | 501 | } |
| 351 | } | 502 | } |
| 352 | } | 503 | } |
| 353 | | 504 | |
| 354 | fn makeString(comptime bytes: []const u8) [16]u8 { | 505 | fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { |
| | 506 | const segment = &self.load_commands.items[self.segment_cmd_index.?].Segment; |
| | 507 | const text_section = &self.sections.items[self.text_section_index.?]; |
| | 508 | const new_block_ideal_capacity = new_block_size * alloc_num / alloc_den; |
| | 509 | |
| | 510 | var block_placement: ?*TextBlock = null; |
| | 511 | const addr = blk: { |
| | 512 | if (self.last_text_block) |last| { |
| | 513 | const last_symbol = self.symbol_table.items[last.symbol_table_index.?]; |
| | 514 | const ideal_capacity = last.size * alloc_num / alloc_den; |
| | 515 | const ideal_capacity_end_addr = last_symbol.n_value + ideal_capacity; |
| | 516 | const new_start_addr = mem.alignForwardGeneric(u64, ideal_capacity_end_addr, alignment); |
| | 517 | block_placement = last; |
| | 518 | break :blk new_start_addr; |
| | 519 | } else { |
| | 520 | break :blk text_section.addr; |
| | 521 | } |
| | 522 | }; |
| | 523 | log.debug("computed symbol address 0x{x}\n", .{addr}); |
| | 524 | |
| | 525 | const expand_text_section = block_placement == null or block_placement.?.next == null; |
| | 526 | if (expand_text_section) { |
| | 527 | const text_capacity = self.allocatedSize(text_section.offset); |
| | 528 | const needed_size = (addr + new_block_size) - text_section.addr; |
| | 529 | log.debug("text capacity 0x{x}, needed size 0x{x}\n", .{ text_capacity, needed_size }); |
| | 530 | |
| | 531 | if (needed_size > text_capacity) { |
| | 532 | // TODO handle growth |
| | 533 | } |
| | 534 | |
| | 535 | self.last_text_block = text_block; |
| | 536 | text_section.size = needed_size; |
| | 537 | segment.vmsize = needed_size; |
| | 538 | segment.filesize = needed_size; |
| | 539 | if (alignment < text_section.@"align") { |
| | 540 | text_section.@"align" = @intCast(u32, alignment); |
| | 541 | } |
| | 542 | } |
| | 543 | text_block.size = new_block_size; |
| | 544 | |
| | 545 | if (text_block.prev) |prev| { |
| | 546 | prev.next = text_block.next; |
| | 547 | } |
| | 548 | if (text_block.next) |next| { |
| | 549 | next.prev = text_block.prev; |
| | 550 | } |
| | 551 | |
| | 552 | if (block_placement) |big_block| { |
| | 553 | text_block.prev = big_block; |
| | 554 | text_block.next = big_block.next; |
| | 555 | big_block.next = text_block; |
| | 556 | } else { |
| | 557 | text_block.prev = null; |
| | 558 | text_block.next = null; |
| | 559 | } |
| | 560 | |
| | 561 | return addr; |
| | 562 | } |
| | 563 | |
| | 564 | fn makeStaticString(comptime bytes: []const u8) [16]u8 { |
| 355 | var buf = [_]u8{0} ** 16; | 565 | var buf = [_]u8{0} ** 16; |
| 356 | if (bytes.len > buf.len) @compileError("MachO segment/section name too long"); | 566 | if (bytes.len > buf.len) @compileError("string too long; max 16 bytes"); |
| 357 | mem.copy(u8, buf[0..], bytes); | 567 | mem.copy(u8, buf[0..], bytes); |
| 358 | return buf; | 568 | return buf; |
| 359 | } | 569 | } |
| 360 | | 570 | |
| | 571 | fn makeString(self: *MachO, bytes: []const u8) !u32 { |
| | 572 | try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1); |
| | 573 | const result = self.string_table.items.len; |
| | 574 | self.string_table.appendSliceAssumeCapacity(bytes); |
| | 575 | self.string_table.appendAssumeCapacity(0); |
| | 576 | return @intCast(u32, result); |
| | 577 | } |
| | 578 | |
| 361 | fn alignSize(comptime Int: type, min_size: anytype, alignment: Int) Int { | 579 | fn alignSize(comptime Int: type, min_size: anytype, alignment: Int) Int { |
| 362 | const size = @intCast(Int, min_size); | 580 | const size = @intCast(Int, min_size); |
| 363 | if (size % alignment == 0) return size; | 581 | if (size % alignment == 0) return size; |
| ... | @@ -370,7 +588,7 @@ fn commandSize(min_size: anytype) u32 { | ... | @@ -370,7 +588,7 @@ fn commandSize(min_size: anytype) u32 { |
| 370 | return alignSize(u32, min_size, @sizeOf(u64)); | 588 | return alignSize(u32, min_size, @sizeOf(u64)); |
| 371 | } | 589 | } |
| 372 | | 590 | |
| 373 | fn addPadding(self: *MachO, size: u32, file_offset: u64) !void { | 591 | fn addPadding(self: *MachO, size: u64, file_offset: u64) !void { |
| 374 | if (size == 0) return; | 592 | if (size == 0) return; |
| 375 | | 593 | |
| 376 | const buf = try self.base.allocator.alloc(u8, size); | 594 | const buf = try self.base.allocator.alloc(u8, size); |
| ... | @@ -380,3 +598,151 @@ fn addPadding(self: *MachO, size: u32, file_offset: u64) !void { | ... | @@ -380,3 +598,151 @@ fn addPadding(self: *MachO, size: u32, file_offset: u64) !void { |
| 380 | | 598 | |
| 381 | try self.base.file.?.pwriteAll(buf, file_offset); | 599 | try self.base.file.?.pwriteAll(buf, file_offset); |
| 382 | } | 600 | } |
| | 601 | |
| | 602 | fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 { |
| | 603 | const hdr_size: u64 = @sizeOf(macho.mach_header_64); |
| | 604 | if (start < hdr_size) |
| | 605 | return hdr_size; |
| | 606 | |
| | 607 | const end = start + satMul(size, alloc_num) / alloc_den; |
| | 608 | |
| | 609 | { |
| | 610 | const off = @sizeOf(macho.mach_header_64); |
| | 611 | var tight_size: u64 = 0; |
| | 612 | for (self.load_commands.items) |cmd| { |
| | 613 | tight_size += cmd.cmdsize(); |
| | 614 | } |
| | 615 | const increased_size = satMul(tight_size, alloc_num) / alloc_den; |
| | 616 | const test_end = off + increased_size; |
| | 617 | if (end > off and start < test_end) { |
| | 618 | return test_end; |
| | 619 | } |
| | 620 | } |
| | 621 | |
| | 622 | for (self.sections.items) |section| { |
| | 623 | const increased_size = satMul(section.size, alloc_num) / alloc_den; |
| | 624 | const test_end = section.offset + increased_size; |
| | 625 | if (end > section.offset and start < test_end) { |
| | 626 | return test_end; |
| | 627 | } |
| | 628 | } |
| | 629 | |
| | 630 | if (self.symtab_cmd_index) |symtab_index| { |
| | 631 | const symtab = self.load_commands.items[symtab_index].Symtab; |
| | 632 | { |
| | 633 | const tight_size = @sizeOf(macho.nlist_64) * symtab.nsyms; |
| | 634 | const increased_size = satMul(tight_size, alloc_num) / alloc_den; |
| | 635 | const test_end = symtab.symoff + increased_size; |
| | 636 | if (end > symtab.symoff and start < test_end) { |
| | 637 | return test_end; |
| | 638 | } |
| | 639 | } |
| | 640 | { |
| | 641 | const increased_size = satMul(symtab.strsize, alloc_num) / alloc_den; |
| | 642 | const test_end = symtab.stroff + increased_size; |
| | 643 | if (end > symtab.stroff and start < test_end) { |
| | 644 | return test_end; |
| | 645 | } |
| | 646 | } |
| | 647 | } |
| | 648 | |
| | 649 | return null; |
| | 650 | } |
| | 651 | |
| | 652 | fn allocatedSize(self: *MachO, start: u64) u64 { |
| | 653 | if (start == 0) |
| | 654 | return 0; |
| | 655 | var min_pos: u64 = std.math.maxInt(u64); |
| | 656 | { |
| | 657 | const off = @sizeOf(macho.mach_header_64); |
| | 658 | if (off > start and off < min_pos) min_pos = off; |
| | 659 | } |
| | 660 | for (self.sections.items) |section| { |
| | 661 | if (section.offset <= start) continue; |
| | 662 | if (section.offset < min_pos) min_pos = section.offset; |
| | 663 | } |
| | 664 | if (self.symtab_cmd_index) |symtab_index| { |
| | 665 | const symtab = self.load_commands.items[symtab_index].Symtab; |
| | 666 | if (symtab.symoff > start and symtab.symoff < min_pos) min_pos = symtab.symoff; |
| | 667 | if (symtab.stroff > start and symtab.stroff < min_pos) min_pos = symtab.stroff; |
| | 668 | } |
| | 669 | return min_pos - start; |
| | 670 | } |
| | 671 | |
| | 672 | fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u16) u64 { |
| | 673 | var start: u64 = 0; |
| | 674 | while (self.detectAllocCollision(start, object_size)) |item_end| { |
| | 675 | start = mem.alignForwardGeneric(u64, item_end, min_alignment); |
| | 676 | } |
| | 677 | return start; |
| | 678 | } |
| | 679 | |
| | 680 | fn writeSymbol(self: *MachO, index: usize) !void { |
| | 681 | const tracy = trace(@src()); |
| | 682 | defer tracy.end(); |
| | 683 | |
| | 684 | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| | 685 | var sym = [1]macho.nlist_64{self.symbol_table.items[index]}; |
| | 686 | const off = symtab.symoff + @sizeOf(macho.nlist_64) * index; |
| | 687 | log.debug("writing symbol {} at 0x{x}\n", .{ sym[0], off }); |
| | 688 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(sym[0..1]), off); |
| | 689 | } |
| | 690 | |
| | 691 | /// Writes Mach-O file header. |
| | 692 | /// Should be invoked last as it needs up-to-date values of ncmds and sizeof_cmds bookkeeping |
| | 693 | /// variables. |
| | 694 | fn writeMachOHeader(self: *MachO) !void { |
| | 695 | var hdr: macho.mach_header_64 = undefined; |
| | 696 | hdr.magic = macho.MH_MAGIC_64; |
| | 697 | |
| | 698 | const CpuInfo = struct { |
| | 699 | cpu_type: macho.cpu_type_t, |
| | 700 | cpu_subtype: macho.cpu_subtype_t, |
| | 701 | }; |
| | 702 | |
| | 703 | const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) { |
| | 704 | .aarch64 => .{ |
| | 705 | .cpu_type = macho.CPU_TYPE_ARM64, |
| | 706 | .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL, |
| | 707 | }, |
| | 708 | .x86_64 => .{ |
| | 709 | .cpu_type = macho.CPU_TYPE_X86_64, |
| | 710 | .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL, |
| | 711 | }, |
| | 712 | else => return error.UnsupportedMachOArchitecture, |
| | 713 | }; |
| | 714 | hdr.cputype = cpu_info.cpu_type; |
| | 715 | hdr.cpusubtype = cpu_info.cpu_subtype; |
| | 716 | |
| | 717 | const filetype: u32 = switch (self.base.options.output_mode) { |
| | 718 | .Exe => macho.MH_EXECUTE, |
| | 719 | .Obj => macho.MH_OBJECT, |
| | 720 | .Lib => switch (self.base.options.link_mode) { |
| | 721 | .Static => return error.TODOStaticLibMachOType, |
| | 722 | .Dynamic => macho.MH_DYLIB, |
| | 723 | }, |
| | 724 | }; |
| | 725 | hdr.filetype = filetype; |
| | 726 | hdr.ncmds = @intCast(u32, self.load_commands.items.len); |
| | 727 | |
| | 728 | var sizeofcmds: u32 = 0; |
| | 729 | for (self.load_commands.items) |cmd| { |
| | 730 | sizeofcmds += cmd.cmdsize(); |
| | 731 | } |
| | 732 | |
| | 733 | hdr.sizeofcmds = sizeofcmds; |
| | 734 | |
| | 735 | // TODO should these be set to something else? |
| | 736 | hdr.flags = 0; |
| | 737 | hdr.reserved = 0; |
| | 738 | |
| | 739 | log.debug("writing Mach-O header {}\n", .{hdr}); |
| | 740 | |
| | 741 | try self.base.file.?.pwriteAll(@ptrCast([*]const u8, &hdr)[0..@sizeOf(macho.mach_header_64)], 0); |
| | 742 | } |
| | 743 | |
| | 744 | /// Saturating multiplication |
| | 745 | fn satMul(a: anytype, b: anytype) @TypeOf(a, b) { |
| | 746 | const T = @TypeOf(a, b); |
| | 747 | return std.math.mul(T, a, b) catch std.math.maxInt(T); |
| | 748 | } |