diff --git a/lib/std/fs.zig b/lib/std/fs.zig index ef6f75601..a4a5a3a56 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1345,8 +1345,10 @@ pub const Dir = struct { mode: File.Mode = File.default_mode, }; - /// `dest_path` must remain valid for the lifetime of `AtomicFile`. - /// Call `AtomicFile.finish` to atomically replace `dest_path` with contents. + /// Directly access the `.file` field, and then call `AtomicFile.finish` + /// to atomically replace `dest_path` with contents. + /// Always call `AtomicFile.deinit` to clean up, regardless of whether `AtomicFile.finish` succeeded. + /// `dest_path` must remain valid until `AtomicFile.deinit` is called. pub fn atomicFile(self: Dir, dest_path: []const u8, options: AtomicFileOptions) !AtomicFile { if (path.dirname(dest_path)) |dirname| { const dir = try self.openDir(dirname, .{}); diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 2a6ad875c..63adfc964 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -93,7 +93,7 @@ pub const File = struct { /// This means that a process that does not respect the locking API can still get access /// to the file, despite the lock. /// - /// Windows' file locks are mandatory, and any process attempting to access the file will + /// Windows's file locks are mandatory, and any process attempting to access the file will /// receive an error. /// /// [1]: https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 1900a36df..5966f8bc9 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -2027,7 +2027,13 @@ test "sliceAsBytes and bytesAsSlice back" { /// Round an address up to the nearest aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignForward(addr: usize, alignment: usize) usize { - return alignBackward(addr + (alignment - 1), alignment); + return alignForwardGeneric(usize, addr, alignment); +} + +/// Round an address up to the nearest aligned address +/// The alignment must be a power of 2 and greater than 0. +pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T { + return alignBackwardGeneric(T, addr + (alignment - 1), alignment); } test "alignForward" { @@ -2048,7 +2054,13 @@ test "alignForward" { /// Round an address up to the previous aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignBackward(addr: usize, alignment: usize) usize { - assert(@popCount(usize, alignment) == 1); + return alignBackwardGeneric(usize, addr, alignment); +} + +/// Round an address up to the previous aligned address +/// The alignment must be a power of 2 and greater than 0. +pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T { + assert(@popCount(T, alignment) == 1); // 000010000 // example addr // 000001111 // subtract 1 // 111110000 // binary not diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index e33d468a2..cea772964 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -96,6 +96,7 @@ pub const Module = struct { errors: []ErrorMsg, arena: std.heap.ArenaAllocator, fns: []Fn, + target: Target, pub const Export = struct { name: []const u8, @@ -122,9 +123,7 @@ pub const ErrorMsg = struct { msg: []const u8, }; -pub fn analyze(allocator: *Allocator, old_module: text.Module) !Module { - const native_info = try std.zig.system.NativeTargetInfo.detect(allocator, .{}); - +pub fn analyze(allocator: *Allocator, old_module: text.Module, target: Target) !Module { var ctx = Analyze{ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator), @@ -133,7 +132,7 @@ pub fn analyze(allocator: *Allocator, old_module: text.Module) !Module { .decl_table = std.AutoHashMap(*text.Inst, Analyze.NewDecl).init(allocator), .exports = std.ArrayList(Module.Export).init(allocator), .fns = std.ArrayList(Module.Fn).init(allocator), - .target = native_info.target, + .target = target, }; defer ctx.errors.deinit(); defer ctx.decl_table.deinit(); @@ -152,6 +151,7 @@ pub fn analyze(allocator: *Allocator, old_module: text.Module) !Module { .errors = ctx.errors.toOwnedSlice(), .fns = ctx.fns.toOwnedSlice(), .arena = ctx.arena, + .target = target, }; } @@ -699,7 +699,9 @@ pub fn main() anyerror!void { std.process.exit(1); } - var analyzed_module = try analyze(allocator, zir_module); + const native_info = try std.zig.system.NativeTargetInfo.detect(allocator, .{}); + + var analyzed_module = try analyze(allocator, zir_module, native_info.target); defer analyzed_module.deinit(allocator); if (analyzed_module.errors.len != 0) { @@ -711,12 +713,18 @@ pub fn main() anyerror!void { std.process.exit(1); } - var new_zir_module = try text.emit_zir(allocator, analyzed_module); - defer new_zir_module.deinit(allocator); + const output_zir = false; + if (output_zir) { + var new_zir_module = try text.emit_zir(allocator, analyzed_module); + defer new_zir_module.deinit(allocator); - var bos = std.io.bufferedOutStream(std.io.getStdOut().outStream()); - try new_zir_module.writeToStream(allocator, bos.outStream()); - try bos.flush(); + var bos = std.io.bufferedOutStream(std.io.getStdOut().outStream()); + try new_zir_module.writeToStream(allocator, bos.outStream()); + try bos.flush(); + } + + const link = @import("link.zig"); + try link.updateExecutableFilePath(allocator, analyzed_module, std.fs.cwd(), "a.out"); } fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } { diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index 013a6248c..f358a4b64 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -1,576 +1,589 @@ const std = @import("std"); const mem = std.mem; -const c = @import("c.zig"); -const Compilation = @import("compilation.zig").Compilation; -const Target = std.Target; -const ObjectFormat = Target.ObjectFormat; -const LibCInstallation = @import("libc_installation.zig").LibCInstallation; const assert = std.debug.assert; -const util = @import("util.zig"); +const Allocator = std.mem.Allocator; +const ir = @import("ir.zig"); +const fs = std.fs; +const elf = std.elf; -const Context = struct { - comp: *Compilation, - arena: std.heap.ArenaAllocator, - args: std.ArrayList([*:0]const u8), - link_in_crt: bool, +const executable_mode = 0o755; +const default_entry_addr = 0x8000000; - link_err: error{OutOfMemory}!void, - link_msg: std.ArrayListSentineled(u8, 0), +/// Attempts incremental linking, if the file already exists. +/// If incremental linking fails, falls back to truncating the file and rewriting it. +/// A malicious file is detected as incremental link failure and does not cause Illegal Behavior. +/// This operation is not atomic. +pub fn updateExecutableFilePath(allocator: *Allocator, module: ir.Module, dir: fs.Dir, sub_path: []const u8) !void { + const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = executable_mode }); + defer file.close(); - libc: *LibCInstallation, - out_file_path: std.ArrayListSentineled(u8, 0), -}; - -pub fn link(comp: *Compilation) !void { - var ctx = Context{ - .comp = comp, - .arena = std.heap.ArenaAllocator.init(comp.gpa()), - .args = undefined, - .link_in_crt = comp.haveLibC() and comp.kind == .Exe, - .link_err = {}, - .link_msg = undefined, - .libc = undefined, - .out_file_path = undefined, - }; - defer ctx.arena.deinit(); - ctx.args = std.ArrayList([*:0]const u8).init(&ctx.arena.allocator); - ctx.link_msg = std.ArrayListSentineled(u8, 0).initNull(&ctx.arena.allocator); - - ctx.out_file_path = try std.ArrayListSentineled(u8, 0).init(&ctx.arena.allocator, comp.name.span()); - switch (comp.kind) { - .Exe => { - try ctx.out_file_path.append(comp.target.exeFileExt()); - }, - .Lib => { - try ctx.out_file_path.append(if (comp.is_static) comp.target.staticLibSuffix() else comp.target.dynamicLibSuffix()); - }, - .Obj => { - try ctx.out_file_path.append(comp.target.oFileExt()); - }, - } - - // even though we're calling LLD as a library it thinks the first - // argument is its own exe name - try ctx.args.append("lld"); - - if (comp.haveLibC()) { - // TODO https://github.com/ziglang/zig/issues/3190 - var libc = ctx.comp.override_libc orelse blk: { - @panic("this code has bitrotted"); - //switch (comp.target) { - // Target.Native => { - // break :blk comp.zig_compiler.getNativeLibC() catch return error.LibCRequiredButNotProvidedOrFound; - // }, - // else => return error.LibCRequiredButNotProvidedOrFound, - //} - }; - ctx.libc = libc; - } - - try constructLinkerArgs(&ctx); - - if (comp.verbose_link) { - for (ctx.args.span()) |arg, i| { - const space = if (i == 0) "" else " "; - std.debug.warn("{}{s}", .{ space, arg }); - } - std.debug.warn("\n", .{}); - } - - const extern_ofmt = toExternObjectFormatType(comp.target.getObjectFormat()); - const args_slice = ctx.args.span(); - - { - // LLD is not thread-safe, so we grab a global lock. - const held = comp.zig_compiler.lld_lock.acquire(); - defer held.release(); - - // Not evented I/O. LLD does its own multithreading internally. - if (!ZigLLDLink(extern_ofmt, args_slice.ptr, args_slice.len, linkDiagCallback, @ptrCast(*c_void, &ctx))) { - if (!ctx.link_msg.isNull()) { - // TODO capture these messages and pass them through the system, reporting them through the - // event system instead of printing them directly here. - // perhaps try to parse and understand them. - std.debug.warn("{}\n", .{ctx.link_msg.span()}); - } - return error.LinkFailed; - } - } + return updateExecutableFile(allocator, module, file); } -extern fn ZigLLDLink( - oformat: c.ZigLLVM_ObjectFormatType, - args: [*]const [*]const u8, - arg_count: usize, - append_diagnostic: extern fn (*c_void, [*]const u8, usize) void, - context: *c_void, -) bool; +/// Atomically overwrites the old file, if present. +pub fn writeExecutableFilePath(allocator: *Allocator, module: ir.Module, dir: fs.Dir, sub_path: []const u8) !void { + const af = try dir.atomicFile(sub_path, .{ .mode = executable_mode }); + defer af.deinit(); -fn linkDiagCallback(context: *c_void, ptr: [*]const u8, len: usize) callconv(.C) void { - const ctx = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); - ctx.link_err = linkDiagCallbackErrorable(ctx, ptr[0..len]); + try writeExecutableFile(allocator, module, af.file); + try af.finish(); } -fn linkDiagCallbackErrorable(ctx: *Context, msg: []const u8) !void { - if (ctx.link_msg.isNull()) { - try ctx.link_msg.resize(0); - } - try ctx.link_msg.append(msg); -} - -fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType { - return switch (ofmt) { - .unknown => .ZigLLVM_UnknownObjectFormat, - .coff => .ZigLLVM_COFF, - .elf => .ZigLLVM_ELF, - .macho => .ZigLLVM_MachO, - .wasm => .ZigLLVM_Wasm, +/// Attempts incremental linking, if the file already exists. +/// If incremental linking fails, falls back to truncating the file and rewriting it. +/// Returns an error if `file` is not already open with +read +write +seek abilities. +/// A malicious file is detected as incremental link failure and does not cause Illegal Behavior. +/// This operation is not atomic. +pub fn updateExecutableFile(allocator: *Allocator, module: ir.Module, file: fs.File) !void { + updateExecutableFileInner(allocator, module, file) catch |err| switch (err) { + error.IncrFailed => { + return writeExecutableFile(allocator, module, file); + }, + else => |e| return e, }; } -fn constructLinkerArgs(ctx: *Context) !void { - switch (ctx.comp.target.getObjectFormat()) { - .unknown => unreachable, - .coff => return constructLinkerArgsCoff(ctx), - .elf => return constructLinkerArgsElf(ctx), - .macho => return constructLinkerArgsMachO(ctx), - .wasm => return constructLinkerArgsWasm(ctx), - } -} +const Update = struct { + file: fs.File, + module: *const ir.Module, -fn constructLinkerArgsElf(ctx: *Context) !void { - // TODO commented out code in this function - //if (g->linker_script) { - // lj->args.append("-T"); - // lj->args.append(g->linker_script); - //} - try ctx.args.append("--gc-sections"); - if (ctx.comp.link_eh_frame_hdr) { - try ctx.args.append("--eh-frame-hdr"); + /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. + /// Same order as in the file. + sections: std.ArrayList(elf.Elf64_Shdr), + shdr_table_offset: ?u64, + + /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. + /// Same order as in the file. + program_headers: std.ArrayList(elf.Elf64_Phdr), + phdr_table_offset: ?u64, + /// The index into the program headers of a PT_LOAD program header with Read and Execute flags + phdr_load_re_index: ?u16, + entry_addr: ?u64, + + shstrtab: std.ArrayList(u8), + shstrtab_index: ?u16, + + text_section_index: ?u16, + symtab_section_index: ?u16, + + /// Key: index into strtab. Value: index into symbols. + symbol_table: std.AutoHashMap(usize, usize), + /// The same order as in the file + symbols: std.ArrayList(elf.Elf64_Sym), + /// Sorted by address, index into symbols + symbols_by_addr: std.ArrayList(usize), + + fn deinit(self: *Update) void { + self.sections.deinit(); + self.program_headers.deinit(); + self.shstrtab.deinit(); + self.symbol_table.deinit(); + self.symbols.deinit(); + self.symbols_by_addr.deinit(); } - //lj->args.append("-m"); - //lj->args.append(getLDMOption(&g->zig_target)); + // `expand_num / expand_den` is the factor of padding when allocation + const alloc_num = 4; + const alloc_den = 3; - //bool is_lib = g->out_type == OutTypeLib; - //bool shared = !g->is_static && is_lib; - //Buf *soname = nullptr; - if (ctx.comp.is_static) { - //if (util.isArmOrThumb(ctx.comp.target)) { - // try ctx.args.append("-Bstatic"); - //} else { - // try ctx.args.append("-static"); - //} - } - //} else if (shared) { - // lj->args.append("-shared"); + /// Returns end pos of collision, if any. + fn detectAllocCollision(self: *Update, start: u64, size: u64) ?u64 { + const small_ptr = self.module.target.cpu.arch.ptrBitWidth() == 32; + const ehdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Ehdr) else @sizeOf(elf.Elf64_Ehdr); + if (start < ehdr_size) + return ehdr_size; - // if (buf_len(&lj->out_file) == 0) { - // buf_appendf(&lj->out_file, "lib%s.so.%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize "", - // buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch); - // } - // soname = buf_sprintf("lib%s.so.%" ZIG_PRI_usize "", buf_ptr(g->root_out_name), g->version_major); - //} + const end = start + satMul(size, alloc_num) / alloc_den; - try ctx.args.append("-o"); - try ctx.args.append(ctx.out_file_path.span()); - - if (ctx.link_in_crt) { - const crt1o = if (ctx.comp.is_static) "crt1.o" else "Scrt1.o"; - try addPathJoin(ctx, ctx.libc.crt_dir.?, crt1o); - try addPathJoin(ctx, ctx.libc.crt_dir.?, "crti.o"); - } - - if (ctx.comp.haveLibC()) { - try ctx.args.append("-L"); - // TODO addNullByte should probably return [:0]u8 - try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, ctx.libc.crt_dir.?)).ptr)); - - //if (!ctx.comp.is_static) { - // const dl = blk: { - // //if (ctx.libc.dynamic_linker_path) |dl| break :blk dl; - // //if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl; - // return error.LibCMissingDynamicLinker; - // }; - // try ctx.args.append("-dynamic-linker"); - // try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, dl)).ptr)); - //} - } - - //if (shared) { - // lj->args.append("-soname"); - // lj->args.append(buf_ptr(soname)); - //} - - // .o files - for (ctx.comp.link_objects) |link_object| { - const link_obj_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, link_object); - try ctx.args.append(@ptrCast([*:0]const u8, link_obj_with_null.ptr)); - } - try addFnObjects(ctx); - - //if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { - // if (g->libc_link_lib == nullptr) { - // Buf *builtin_o_path = build_o(g, "builtin"); - // lj->args.append(buf_ptr(builtin_o_path)); - // } - - // // sometimes libgcc is missing stuff, so we still build compiler_rt and rely on weak linkage - // Buf *compiler_rt_o_path = build_compiler_rt(g); - // lj->args.append(buf_ptr(compiler_rt_o_path)); - //} - - //for (size_t i = 0; i < g->link_libs_list.length; i += 1) { - // LinkLib *link_lib = g->link_libs_list.at(i); - // if (buf_eql_str(link_lib->name, "c")) { - // continue; - // } - // Buf *arg; - // if (buf_starts_with_str(link_lib->name, "/") || buf_ends_with_str(link_lib->name, ".a") || - // buf_ends_with_str(link_lib->name, ".so")) - // { - // arg = link_lib->name; - // } else { - // arg = buf_sprintf("-l%s", buf_ptr(link_lib->name)); - // } - // lj->args.append(buf_ptr(arg)); - //} - - // libc dep - if (ctx.comp.haveLibC()) { - if (ctx.comp.is_static) { - try ctx.args.append("--start-group"); - try ctx.args.append("-lgcc"); - try ctx.args.append("-lgcc_eh"); - try ctx.args.append("-lc"); - try ctx.args.append("-lm"); - try ctx.args.append("--end-group"); - } else { - try ctx.args.append("-lgcc"); - try ctx.args.append("--as-needed"); - try ctx.args.append("-lgcc_s"); - try ctx.args.append("--no-as-needed"); - try ctx.args.append("-lc"); - try ctx.args.append("-lm"); - try ctx.args.append("-lgcc"); - try ctx.args.append("--as-needed"); - try ctx.args.append("-lgcc_s"); - try ctx.args.append("--no-as-needed"); - } - } - - // crt end - if (ctx.link_in_crt) { - try addPathJoin(ctx, ctx.libc.crt_dir.?, "crtn.o"); - } - - //if (ctx.comp.target != Target.Native) { - // try ctx.args.append("--allow-shlib-undefined"); - //} -} - -fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void { - const full_path = try std.fs.path.join(&ctx.arena.allocator, &[_][]const u8{ dirname, basename }); - const full_path_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, full_path); - try ctx.args.append(@ptrCast([*:0]const u8, full_path_with_null.ptr)); -} - -fn constructLinkerArgsCoff(ctx: *Context) !void { - try ctx.args.append("-NOLOGO"); - - if (!ctx.comp.strip) { - try ctx.args.append("-DEBUG"); - } - - switch (ctx.comp.target.cpu.arch) { - .i386 => try ctx.args.append("-MACHINE:X86"), - .x86_64 => try ctx.args.append("-MACHINE:X64"), - .aarch64 => try ctx.args.append("-MACHINE:ARM"), - else => return error.UnsupportedLinkArchitecture, - } - - const is_library = ctx.comp.kind == .Lib; - - const out_arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-OUT:{}\x00", .{ctx.out_file_path.span()}); - try ctx.args.append(@ptrCast([*:0]const u8, out_arg.ptr)); - - if (ctx.comp.haveLibC()) { - try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr)); - try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.kernel32_lib_dir.?})).ptr)); - try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.crt_dir.?})).ptr)); - } - - if (ctx.link_in_crt) { - const lib_str = if (ctx.comp.is_static) "lib" else ""; - const d_str = if (ctx.comp.build_mode == .Debug) "d" else ""; - - if (ctx.comp.is_static) { - const cmt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "libcmt{}.lib\x00", .{d_str}); - try ctx.args.append(@ptrCast([*:0]const u8, cmt_lib_name.ptr)); - } else { - const msvcrt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "msvcrt{}.lib\x00", .{d_str}); - try ctx.args.append(@ptrCast([*:0]const u8, msvcrt_lib_name.ptr)); - } - - const vcruntime_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}vcruntime{}.lib\x00", .{ - lib_str, - d_str, - }); - try ctx.args.append(@ptrCast([*:0]const u8, vcruntime_lib_name.ptr)); - - const crt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}ucrt{}.lib\x00", .{ lib_str, d_str }); - try ctx.args.append(@ptrCast([*:0]const u8, crt_lib_name.ptr)); - - // Visual C++ 2015 Conformance Changes - // https://msdn.microsoft.com/en-us/library/bb531344.aspx - try ctx.args.append("legacy_stdio_definitions.lib"); - - // msvcrt depends on kernel32 - try ctx.args.append("kernel32.lib"); - } else { - try ctx.args.append("-NODEFAULTLIB"); - if (!is_library) { - try ctx.args.append("-ENTRY:WinMainCRTStartup"); - } - } - - if (is_library and !ctx.comp.is_static) { - try ctx.args.append("-DLL"); - } - - for (ctx.comp.link_objects) |link_object| { - const link_obj_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, link_object); - try ctx.args.append(@ptrCast([*:0]const u8, link_obj_with_null.ptr)); - } - try addFnObjects(ctx); - - switch (ctx.comp.kind) { - .Exe, .Lib => { - if (!ctx.comp.haveLibC()) { - @panic("TODO"); - } - }, - .Obj => {}, - } -} - -fn constructLinkerArgsMachO(ctx: *Context) !void { - try ctx.args.append("-demangle"); - - if (ctx.comp.linker_rdynamic) { - try ctx.args.append("-export_dynamic"); - } - - const is_lib = ctx.comp.kind == .Lib; - const shared = !ctx.comp.is_static and is_lib; - if (ctx.comp.is_static) { - try ctx.args.append("-static"); - } else { - try ctx.args.append("-dynamic"); - } - - try ctx.args.append("-arch"); - try ctx.args.append(util.getDarwinArchString(ctx.comp.target)); - - const platform = try DarwinPlatform.get(ctx.comp); - switch (platform.kind) { - .MacOS => try ctx.args.append("-macosx_version_min"), - .IPhoneOS => try ctx.args.append("-iphoneos_version_min"), - .IPhoneOSSimulator => try ctx.args.append("-ios_simulator_version_min"), - } - const ver_str = try std.fmt.allocPrint(&ctx.arena.allocator, "{}.{}.{}\x00", .{ - platform.major, - platform.minor, - platform.micro, - }); - try ctx.args.append(@ptrCast([*:0]const u8, ver_str.ptr)); - - if (ctx.comp.kind == .Exe) { - if (ctx.comp.is_static) { - try ctx.args.append("-no_pie"); - } else { - try ctx.args.append("-pie"); - } - } - - try ctx.args.append("-o"); - try ctx.args.append(ctx.out_file_path.span()); - - if (shared) { - try ctx.args.append("-headerpad_max_install_names"); - } else if (ctx.comp.is_static) { - try ctx.args.append("-lcrt0.o"); - } else { - switch (platform.kind) { - .MacOS => { - if (platform.versionLessThan(10, 5)) { - try ctx.args.append("-lcrt1.o"); - } else if (platform.versionLessThan(10, 6)) { - try ctx.args.append("-lcrt1.10.5.o"); - } else if (platform.versionLessThan(10, 8)) { - try ctx.args.append("-lcrt1.10.6.o"); - } - }, - .IPhoneOS => { - if (ctx.comp.target.cpu.arch == .aarch64) { - // iOS does not need any crt1 files for arm64 - } else if (platform.versionLessThan(3, 1)) { - try ctx.args.append("-lcrt1.o"); - } else if (platform.versionLessThan(6, 0)) { - try ctx.args.append("-lcrt1.3.1.o"); - } - }, - .IPhoneOSSimulator => {}, // no crt1.o needed - } - } - - for (ctx.comp.link_objects) |link_object| { - const link_obj_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, link_object); - try ctx.args.append(@ptrCast([*:0]const u8, link_obj_with_null.ptr)); - } - try addFnObjects(ctx); - - // TODO - //if (ctx.comp.target == Target.Native) { - // for (ctx.comp.link_libs_list.span()) |lib| { - // if (mem.eql(u8, lib.name, "c")) { - // // on Darwin, libSystem has libc in it, but also you have to use it - // // to make syscalls because the syscall numbers are not documented - // // and change between versions. - // // so we always link against libSystem - // try ctx.args.append("-lSystem"); - // } else { - // if (mem.indexOfScalar(u8, lib.name, '/') == null) { - // const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name}); - // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr)); - // } else { - // const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name); - // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr)); - // } - // } - // } - //} else { - // try ctx.args.append("-undefined"); - // try ctx.args.append("dynamic_lookup"); - //} - - if (platform.kind == .MacOS) { - if (platform.versionLessThan(10, 5)) { - try ctx.args.append("-lgcc_s.10.4"); - } else if (platform.versionLessThan(10, 6)) { - try ctx.args.append("-lgcc_s.10.5"); - } - } else { - @panic("TODO"); - } -} - -fn constructLinkerArgsWasm(ctx: *Context) void { - @panic("TODO"); -} - -fn addFnObjects(ctx: *Context) !void { - const held = ctx.comp.fn_link_set.acquire(); - defer held.release(); - - var it = held.value.first; - while (it) |node| { - const fn_val = node.data orelse { - // handle the tombstone. See Value.Fn.destroy. - it = node.next; - held.value.remove(node); - ctx.comp.gpa().destroy(node); - continue; - }; - try ctx.args.append(fn_val.containing_object.span()); - it = node.next; - } -} - -const DarwinPlatform = struct { - kind: Kind, - major: u32, - minor: u32, - micro: u32, - - const Kind = enum { - MacOS, - IPhoneOS, - IPhoneOSSimulator, - }; - - fn get(comp: *Compilation) !DarwinPlatform { - var result: DarwinPlatform = undefined; - const ver_str = switch (comp.darwin_version_min) { - .MacOS => |ver| blk: { - result.kind = .MacOS; - break :blk ver; - }, - .Ios => |ver| blk: { - result.kind = .IPhoneOS; - break :blk ver; - }, - .None => blk: { - assert(comp.target.os.tag == .macosx); - result.kind = .MacOS; - break :blk "10.14"; - }, - }; - - var had_extra: bool = undefined; - try darwinGetReleaseVersion( - ver_str, - &result.major, - &result.minor, - &result.micro, - &had_extra, - ); - if (had_extra or result.major != 10 or result.minor >= 100 or result.micro >= 100) { - return error.InvalidDarwinVersionString; - } - - if (result.kind == .IPhoneOS) { - switch (comp.target.cpu.arch) { - .i386, - .x86_64, - => result.kind = .IPhoneOSSimulator, - else => {}, + if (self.shdr_table_offset) |off| { + const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr); + const tight_size = self.sections.items.len * shdr_size; + const increased_size = satMul(tight_size, alloc_num) / alloc_den; + const test_end = off + increased_size; + if (end > off and start < test_end) { + return test_end; } } - return result; + + if (self.phdr_table_offset) |off| { + const phdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Phdr) else @sizeOf(elf.Elf64_Phdr); + const tight_size = self.sections.items.len * phdr_size; + const increased_size = satMul(tight_size, alloc_num) / alloc_den; + const test_end = off + increased_size; + if (end > off and start < test_end) { + return test_end; + } + } + + for (self.sections.items) |section| { + const increased_size = satMul(section.sh_size, alloc_num) / alloc_den; + const test_end = section.sh_offset + increased_size; + if (end > section.sh_offset and start < test_end) { + return test_end; + } + } + for (self.program_headers.items) |program_header| { + const increased_size = satMul(program_header.p_filesz, alloc_num) / alloc_den; + const test_end = program_header.p_offset + increased_size; + if (end > program_header.p_offset and start < test_end) { + return test_end; + } + } + return null; } - fn versionLessThan(self: DarwinPlatform, major: u32, minor: u32) bool { - if (self.major < major) - return true; - if (self.major > major) - return false; - if (self.minor < minor) - return true; - return false; + fn allocatedSize(self: *Update, start: u64) u64 { + var min_pos: u64 = std.math.maxInt(u64); + if (self.shdr_table_offset) |off| { + if (off > start and off < min_pos) min_pos = off; + } + if (self.phdr_table_offset) |off| { + if (off > start and off < min_pos) min_pos = off; + } + for (self.sections.items) |section| { + if (section.sh_offset <= start) continue; + if (section.sh_offset < min_pos) min_pos = section.sh_offset; + } + for (self.program_headers.items) |program_header| { + if (program_header.p_offset <= start) continue; + if (program_header.p_offset < min_pos) min_pos = program_header.p_offset; + } + return min_pos; + } + + fn findFreeSpace(self: *Update, object_size: u64, min_alignment: u16) u64 { + var start: u64 = 0; + while (self.detectAllocCollision(start, object_size)) |item_end| { + start = mem.alignForwardGeneric(u64, item_end, min_alignment); + } + return start; + } + + fn makeString(self: *Update, bytes: []const u8) !u32 { + const result = self.shstrtab.items.len; + try self.shstrtab.appendSlice(bytes); + return @intCast(u32, result); + } + + fn perform(self: *Update) !void { + const ptr_width: enum { p32, p64 } = switch (self.module.target.cpu.arch.ptrBitWidth()) { + 32 => .p32, + 64 => .p64, + else => return error.UnsupportedArchitecture, + }; + const small_ptr = switch (ptr_width) { + .p32 => true, + .p64 => false, + }; + // This means the entire read-only executable program code needs to be rewritten. + var phdr_load_re_dirty = false; + var phdr_table_dirty = false; + var shdr_table_dirty = false; + var shstrtab_dirty = false; + var symtab_dirty = false; + + if (self.phdr_load_re_index == null) { + self.phdr_load_re_index = @intCast(u16, self.program_headers.items.len); + const file_size = 256 * 1024; + const p_align = 0x1000; + const off = self.findFreeSpace(file_size, p_align); + try self.program_headers.append(.{ + .p_type = elf.PT_LOAD, + .p_offset = off, + .p_filesz = file_size, + .p_vaddr = default_entry_addr, + .p_paddr = default_entry_addr, + .p_memsz = 0, + .p_align = 0x1000, + .p_flags = elf.PF_X | elf.PF_R, + }); + self.entry_addr = default_entry_addr; + phdr_load_re_dirty = true; + phdr_table_dirty = true; + } + if (self.sections.items.len == 0) { + // There must always be a null section in index 0 + try self.sections.append(.{ + .sh_name = 0, + .sh_type = 0, + .sh_flags = 0, + .sh_addr = 0, + .sh_offset = 0, + .sh_size = 0, + .sh_link = 0, + .sh_info = 0, + .sh_addralign = 0, + .sh_entsize = 0, + }); + shdr_table_dirty = true; + } + if (self.shstrtab_index == null) { + self.shstrtab_index = @intCast(u16, self.sections.items.len); + const off = self.findFreeSpace(self.shstrtab.items.len, 1); + try self.sections.append(.{ + .sh_name = try self.makeString(".shstrtab"), + .sh_type = elf.SHT_STRTAB, + .sh_flags = 0, + .sh_addr = 0, + .sh_offset = off, + .sh_size = self.shstrtab.items.len, + .sh_link = 0, + .sh_info = 0, + .sh_addralign = 1, + .sh_entsize = 0, + }); + shstrtab_dirty = true; + shdr_table_dirty = true; + } + if (self.text_section_index == null) { + self.text_section_index = @intCast(u16, self.sections.items.len); + const phdr = &self.program_headers.items[self.phdr_load_re_index.?]; + + try self.sections.append(.{ + .sh_name = try self.makeString(".text"), + .sh_type = elf.SHT_PROGBITS, + .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, + .sh_addr = phdr.p_vaddr, + .sh_offset = phdr.p_offset, + .sh_size = phdr.p_filesz, + .sh_link = 0, + .sh_info = 0, + .sh_addralign = phdr.p_align, + .sh_entsize = 0, + }); + shdr_table_dirty = true; + } + if (self.symtab_section_index == null) { + self.symtab_section_index = @intCast(u16, self.sections.items.len); + const min_align: u16 = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym); + const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); + const file_size = self.module.exports.len * each_size; + const off = self.findFreeSpace(file_size, min_align); + + try self.sections.append(.{ + .sh_name = try self.makeString(".symtab"), + .sh_type = elf.SHT_SYMTAB, + .sh_flags = 0, + .sh_addr = 0, + .sh_offset = off, + .sh_size = file_size, + // The section header index of the associated string table. + .sh_link = self.shstrtab_index.?, + // One greater than the symbol table index of the last local symbol (binding STB_LOCAL). + .sh_info = @intCast(u32, self.module.exports.len), + .sh_addralign = min_align, + .sh_entsize = each_size, + }); + symtab_dirty = true; + shdr_table_dirty = true; + } + const shsize: u64 = switch (ptr_width) { + .p32 => @sizeOf(elf.Elf32_Shdr), + .p64 => @sizeOf(elf.Elf64_Shdr), + }; + const shalign: u16 = switch (ptr_width) { + .p32 => @alignOf(elf.Elf32_Shdr), + .p64 => @alignOf(elf.Elf64_Shdr), + }; + if (self.shdr_table_offset == null) { + self.shdr_table_offset = self.findFreeSpace(self.sections.items.len * shsize, shalign); + shdr_table_dirty = true; + } + const phsize: u64 = switch (ptr_width) { + .p32 => @sizeOf(elf.Elf32_Phdr), + .p64 => @sizeOf(elf.Elf64_Phdr), + }; + const phalign: u16 = switch (ptr_width) { + .p32 => @alignOf(elf.Elf32_Phdr), + .p64 => @alignOf(elf.Elf64_Phdr), + }; + if (self.phdr_table_offset == null) { + self.phdr_table_offset = self.findFreeSpace(self.program_headers.items.len * phsize, phalign); + phdr_table_dirty = true; + } + const foreign_endian = self.module.target.cpu.arch.endian() != std.Target.current.cpu.arch.endian(); + if (phdr_table_dirty) { + const allocated_size = self.allocatedSize(self.phdr_table_offset.?); + const needed_size = self.program_headers.items.len * phsize; + + if (needed_size > allocated_size) { + self.phdr_table_offset = self.findFreeSpace(needed_size, phalign); + } + + const allocator = self.program_headers.allocator; + switch (ptr_width) { + .p32 => { + const buf = try allocator.alloc(elf.Elf32_Phdr, self.program_headers.items.len); + defer allocator.free(buf); + + for (buf) |*phdr, i| { + phdr.* = .{ + .p_type = self.program_headers.items[i].p_type, + .p_flags = self.program_headers.items[i].p_flags, + .p_offset = @intCast(u32, self.program_headers.items[i].p_offset), + .p_vaddr = @intCast(u32, self.program_headers.items[i].p_vaddr), + .p_paddr = @intCast(u32, self.program_headers.items[i].p_paddr), + .p_filesz = @intCast(u32, self.program_headers.items[i].p_filesz), + .p_memsz = @intCast(u32, self.program_headers.items[i].p_memsz), + .p_align = @intCast(u32, self.program_headers.items[i].p_align), + }; + if (foreign_endian) { + bswapAllFields(elf.Elf32_Phdr, phdr); + } + } + try self.file.pwriteAll(mem.sliceAsBytes(buf), self.phdr_table_offset.?); + }, + .p64 => { + const buf = try allocator.alloc(elf.Elf64_Phdr, self.program_headers.items.len); + defer allocator.free(buf); + + for (buf) |*phdr, i| { + phdr.* = .{ + .p_type = self.program_headers.items[i].p_type, + .p_flags = self.program_headers.items[i].p_flags, + .p_offset = self.program_headers.items[i].p_offset, + .p_vaddr = self.program_headers.items[i].p_vaddr, + .p_paddr = self.program_headers.items[i].p_paddr, + .p_filesz = self.program_headers.items[i].p_filesz, + .p_memsz = self.program_headers.items[i].p_memsz, + .p_align = self.program_headers.items[i].p_align, + }; + if (foreign_endian) { + bswapAllFields(elf.Elf64_Phdr, phdr); + } + } + try self.file.pwriteAll(mem.sliceAsBytes(buf), self.phdr_table_offset.?); + }, + } + } + if (shdr_table_dirty) { + const allocated_size = self.allocatedSize(self.shdr_table_offset.?); + const needed_size = self.sections.items.len * phsize; + + if (needed_size > allocated_size) { + self.shdr_table_offset = self.findFreeSpace(needed_size, phalign); + } + + const allocator = self.sections.allocator; + switch (ptr_width) { + .p32 => { + const buf = try allocator.alloc(elf.Elf32_Shdr, self.sections.items.len); + defer allocator.free(buf); + + for (buf) |*shdr, i| { + shdr.* = .{ + .sh_name = self.sections.items[i].sh_name, + .sh_type = self.sections.items[i].sh_type, + .sh_flags = @intCast(u32, self.sections.items[i].sh_flags), + .sh_addr = @intCast(u32, self.sections.items[i].sh_addr), + .sh_offset = @intCast(u32, self.sections.items[i].sh_offset), + .sh_size = @intCast(u32, self.sections.items[i].sh_size), + .sh_link = self.sections.items[i].sh_link, + .sh_info = self.sections.items[i].sh_info, + .sh_addralign = @intCast(u32, self.sections.items[i].sh_addralign), + .sh_entsize = @intCast(u32, self.sections.items[i].sh_entsize), + }; + if (foreign_endian) { + bswapAllFields(elf.Elf32_Shdr, shdr); + } + } + try self.file.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); + }, + .p64 => { + const buf = try allocator.alloc(elf.Elf64_Shdr, self.sections.items.len); + defer allocator.free(buf); + + for (buf) |*shdr, i| { + shdr.* = .{ + .sh_name = self.sections.items[i].sh_name, + .sh_type = self.sections.items[i].sh_type, + .sh_flags = self.sections.items[i].sh_flags, + .sh_addr = self.sections.items[i].sh_addr, + .sh_offset = self.sections.items[i].sh_offset, + .sh_size = self.sections.items[i].sh_size, + .sh_link = self.sections.items[i].sh_link, + .sh_info = self.sections.items[i].sh_info, + .sh_addralign = self.sections.items[i].sh_addralign, + .sh_entsize = self.sections.items[i].sh_entsize, + }; + if (foreign_endian) { + bswapAllFields(elf.Elf64_Shdr, shdr); + } + } + try self.file.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); + }, + } + } + if (shstrtab_dirty) { + try self.file.pwriteAll(self.shstrtab.items, self.sections.items[self.shstrtab_index.?].sh_offset); + } + try self.writeCodeAndSymbols(); + try self.writeElfHeader(); + // TODO find end pos and truncate + } + + fn writeElfHeader(self: *Update) !void { + var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined; + + var index: usize = 0; + hdr_buf[0..4].* = "\x7fELF".*; + index += 4; + + const ptr_width: enum { p32, p64 } = switch (self.module.target.cpu.arch.ptrBitWidth()) { + 32 => .p32, + 64 => .p64, + else => return error.UnsupportedArchitecture, + }; + hdr_buf[index] = switch (ptr_width) { + .p32 => elf.ELFCLASS32, + .p64 => elf.ELFCLASS64, + }; + index += 1; + + const endian = self.module.target.cpu.arch.endian(); + hdr_buf[index] = switch (endian) { + .Little => elf.ELFDATA2LSB, + .Big => elf.ELFDATA2MSB, + }; + index += 1; + + hdr_buf[index] = 1; // ELF version + index += 1; + + // OS ABI, often set to 0 regardless of target platform + // ABI Version, possibly used by glibc but not by static executables + // padding + mem.set(u8, hdr_buf[index..][0..9], 0); + index += 9; + + assert(index == 16); + + mem.writeInt(u16, hdr_buf[index..][0..2], @enumToInt(elf.ET.EXEC), endian); + index += 2; + + const machine = self.module.target.cpu.arch.toElfMachine(); + mem.writeInt(u16, hdr_buf[index..][0..2], @enumToInt(machine), endian); + index += 2; + + // ELF Version, again + mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian); + index += 4; + + switch (ptr_width) { + .p32 => { + // e_entry + mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(u32, self.entry_addr.?), endian); + index += 4; + + // e_phoff + mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(u32, self.phdr_table_offset.?), endian); + index += 4; + + // e_shoff + mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(u32, self.shdr_table_offset.?), endian); + index += 4; + }, + .p64 => { + // e_entry + mem.writeInt(u64, hdr_buf[index..][0..8], self.entry_addr.?, endian); + index += 8; + + // e_phoff + mem.writeInt(u64, hdr_buf[index..][0..8], self.phdr_table_offset.?, endian); + index += 8; + + // e_shoff + mem.writeInt(u64, hdr_buf[index..][0..8], self.shdr_table_offset.?, endian); + index += 8; + }, + } + + const e_flags = 0; + mem.writeInt(u32, hdr_buf[index..][0..4], e_flags, endian); + index += 4; + + const e_ehsize: u16 = switch (ptr_width) { + .p32 => @sizeOf(elf.Elf32_Ehdr), + .p64 => @sizeOf(elf.Elf64_Ehdr), + }; + mem.writeInt(u16, hdr_buf[index..][0..2], e_ehsize, endian); + index += 2; + + const e_phentsize: u16 = switch (ptr_width) { + .p32 => @sizeOf(elf.Elf32_Phdr), + .p64 => @sizeOf(elf.Elf64_Phdr), + }; + mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian); + index += 2; + + const e_phnum = @intCast(u16, self.program_headers.items.len); + mem.writeInt(u16, hdr_buf[index..][0..2], e_phnum, endian); + index += 2; + + const e_shentsize: u16 = switch (ptr_width) { + .p32 => @sizeOf(elf.Elf32_Shdr), + .p64 => @sizeOf(elf.Elf64_Shdr), + }; + mem.writeInt(u16, hdr_buf[index..][0..2], e_shentsize, endian); + index += 2; + + const e_shnum = @intCast(u16, self.sections.items.len); + mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian); + index += 2; + + mem.writeInt(u16, hdr_buf[index..][0..2], self.shstrtab_index.?, endian); + index += 2; + + assert(index == e_ehsize); + + try self.file.pwriteAll(hdr_buf[0..index], 0); + } + + fn writeCodeAndSymbols(self: *Update) !void { + @panic("TODO writeCodeAndSymbols"); } }; -/// Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the -/// grouped values as integers. Numbers which are not provided are set to 0. -/// return true if the entire string was parsed (9.2), or all groups were -/// parsed (10.3.5extrastuff). -fn darwinGetReleaseVersion(str: []const u8, major: *u32, minor: *u32, micro: *u32, had_extra: *bool) !void { - major.* = 0; - minor.* = 0; - micro.* = 0; - had_extra.* = false; +/// Truncates the existing file contents and overwrites the contents. +/// Returns an error if `file` is not already open with +read +write +seek abilities. +pub fn writeExecutableFile(allocator: *Allocator, module: ir.Module, file: fs.File) !void { + var update = Update{ + .file = file, + .module = &module, + .sections = std.ArrayList(elf.Elf64_Shdr).init(allocator), + .shdr_table_offset = null, + .program_headers = std.ArrayList(elf.Elf64_Phdr).init(allocator), + .phdr_table_offset = null, + .phdr_load_re_index = null, + .entry_addr = null, + .shstrtab = std.ArrayList(u8).init(allocator), + .shstrtab_index = null, + .text_section_index = null, + .symtab_section_index = null, - if (str.len == 0) - return error.InvalidDarwinVersionString; + .symbol_table = std.AutoHashMap(usize, usize).init(allocator), + .symbols = std.ArrayList(elf.Elf64_Sym).init(allocator), + .symbols_by_addr = std.ArrayList(usize).init(allocator), + }; + defer update.deinit(); - var start_pos: usize = 0; - for ([_]*u32{ major, minor, micro }) |v| { - const dot_pos = mem.indexOfScalarPos(u8, str, start_pos, '.'); - const end_pos = dot_pos orelse str.len; - v.* = std.fmt.parseUnsigned(u32, str[start_pos..end_pos], 10) catch return error.InvalidDarwinVersionString; - start_pos = (dot_pos orelse return) + 1; - if (start_pos == str.len) return; - } - had_extra.* = true; + return update.perform(); +} + +/// Returns error.IncrFailed if incremental update could not be performed. +fn updateExecutableFileInner(allocator: *Allocator, module: ir.Module, file: fs.File) !void { + //var ehdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined; + + // TODO implement incremental linking + return error.IncrFailed; +} + +/// Saturating multiplication +fn satMul(a: var, b: var) @TypeOf(a, b) { + const T = @TypeOf(a, b); + return std.math.mul(T, a, b) catch std.math.maxInt(T); +} + +fn bswapAllFields(comptime S: type, ptr: *S) void { + @panic("TODO implement bswapAllFields"); }