| ... | @@ -0,0 +1,314 @@ |
| 1 | const std = @import("std"); |
| 2 | const c = @import("c.zig"); |
| 3 | const builtin = @import("builtin"); |
| 4 | const ObjectFormat = builtin.ObjectFormat; |
| 5 | const Compilation = @import("compilation.zig").Compilation; |
| 6 | |
| 7 | const Context = struct { |
| 8 | comp: *Compilation, |
| 9 | arena: std.heap.ArenaAllocator, |
| 10 | args: std.ArrayList([*]const u8), |
| 11 | link_in_crt: bool, |
| 12 | |
| 13 | link_err: error{OutOfMemory}!void, |
| 14 | link_msg: std.Buffer, |
| 15 | }; |
| 16 | |
| 17 | pub fn link(comp: *Compilation) !void { |
| 18 | var ctx = Context{ |
| 19 | .comp = comp, |
| 20 | .arena = std.heap.ArenaAllocator.init(comp.gpa()), |
| 21 | .args = undefined, |
| 22 | .link_in_crt = comp.haveLibC() and comp.kind == Compilation.Kind.Exe, |
| 23 | .link_err = {}, |
| 24 | .link_msg = undefined, |
| 25 | }; |
| 26 | defer ctx.arena.deinit(); |
| 27 | ctx.args = std.ArrayList([*]const u8).init(&ctx.arena.allocator); |
| 28 | ctx.link_msg = std.Buffer.initNull(&ctx.arena.allocator); |
| 29 | |
| 30 | // even though we're calling LLD as a library it thinks the first |
| 31 | // argument is its own exe name |
| 32 | try ctx.args.append(c"lld"); |
| 33 | |
| 34 | try constructLinkerArgs(&ctx); |
| 35 | |
| 36 | if (comp.verbose_link) { |
| 37 | for (ctx.args.toSliceConst()) |arg, i| { |
| 38 | const space = if (i == 0) "" else " "; |
| 39 | std.debug.warn("{}{s}", space, arg); |
| 40 | } |
| 41 | std.debug.warn("\n"); |
| 42 | } |
| 43 | |
| 44 | const extern_ofmt = toExternObjectFormatType(comp.target.getObjectFormat()); |
| 45 | const args_slice = ctx.args.toSlice(); |
| 46 | if (!ZigLLDLink(extern_ofmt, args_slice.ptr, args_slice.len, linkDiagCallback, @ptrCast(*c_void, &ctx))) { |
| 47 | if (!ctx.link_msg.isNull()) { |
| 48 | // TODO capture these messages and pass them through the system, reporting them through the |
| 49 | // event system instead of printing them directly here. |
| 50 | // perhaps try to parse and understand them. |
| 51 | std.debug.warn("{}\n", ctx.link_msg.toSliceConst()); |
| 52 | } |
| 53 | return error.LinkFailed; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | extern fn ZigLLDLink( |
| 58 | oformat: c.ZigLLVM_ObjectFormatType, |
| 59 | args: [*]const [*]const u8, |
| 60 | arg_count: usize, |
| 61 | append_diagnostic: extern fn (*c_void, [*]const u8, usize) void, |
| 62 | context: *c_void, |
| 63 | ) bool; |
| 64 | |
| 65 | extern fn linkDiagCallback(context: *c_void, ptr: [*]const u8, len: usize) void { |
| 66 | const ctx = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); |
| 67 | ctx.link_err = linkDiagCallbackErrorable(ctx, ptr[0..len]); |
| 68 | } |
| 69 | |
| 70 | fn linkDiagCallbackErrorable(ctx: *Context, msg: []const u8) !void { |
| 71 | if (ctx.link_msg.isNull()) { |
| 72 | try ctx.link_msg.resize(0); |
| 73 | } |
| 74 | try ctx.link_msg.append(msg); |
| 75 | } |
| 76 | |
| 77 | fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType { |
| 78 | return switch (ofmt) { |
| 79 | ObjectFormat.unknown => c.ZigLLVM_UnknownObjectFormat, |
| 80 | ObjectFormat.coff => c.ZigLLVM_COFF, |
| 81 | ObjectFormat.elf => c.ZigLLVM_ELF, |
| 82 | ObjectFormat.macho => c.ZigLLVM_MachO, |
| 83 | ObjectFormat.wasm => c.ZigLLVM_Wasm, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | fn constructLinkerArgs(ctx: *Context) !void { |
| 88 | switch (ctx.comp.target.getObjectFormat()) { |
| 89 | ObjectFormat.unknown => unreachable, |
| 90 | ObjectFormat.coff => return constructLinkerArgsCoff(ctx), |
| 91 | ObjectFormat.elf => return constructLinkerArgsElf(ctx), |
| 92 | ObjectFormat.macho => return constructLinkerArgsMachO(ctx), |
| 93 | ObjectFormat.wasm => return constructLinkerArgsWasm(ctx), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn constructLinkerArgsElf(ctx: *Context) !void { |
| 98 | //if (g->libc_link_lib != nullptr) { |
| 99 | // find_libc_lib_path(g); |
| 100 | //} |
| 101 | |
| 102 | //if (g->linker_script) { |
| 103 | // lj->args.append("-T"); |
| 104 | // lj->args.append(g->linker_script); |
| 105 | //} |
| 106 | |
| 107 | //if (g->no_rosegment_workaround) { |
| 108 | // lj->args.append("--no-rosegment"); |
| 109 | //} |
| 110 | //lj->args.append("--gc-sections"); |
| 111 | |
| 112 | //lj->args.append("-m"); |
| 113 | //lj->args.append(getLDMOption(&g->zig_target)); |
| 114 | |
| 115 | //bool is_lib = g->out_type == OutTypeLib; |
| 116 | //bool shared = !g->is_static && is_lib; |
| 117 | //Buf *soname = nullptr; |
| 118 | //if (g->is_static) { |
| 119 | // if (g->zig_target.arch.arch == ZigLLVM_arm || g->zig_target.arch.arch == ZigLLVM_armeb || |
| 120 | // g->zig_target.arch.arch == ZigLLVM_thumb || g->zig_target.arch.arch == ZigLLVM_thumbeb) |
| 121 | // { |
| 122 | // lj->args.append("-Bstatic"); |
| 123 | // } else { |
| 124 | // lj->args.append("-static"); |
| 125 | // } |
| 126 | //} else if (shared) { |
| 127 | // lj->args.append("-shared"); |
| 128 | |
| 129 | // if (buf_len(&lj->out_file) == 0) { |
| 130 | // buf_appendf(&lj->out_file, "lib%s.so.%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize "", |
| 131 | // buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch); |
| 132 | // } |
| 133 | // soname = buf_sprintf("lib%s.so.%" ZIG_PRI_usize "", buf_ptr(g->root_out_name), g->version_major); |
| 134 | //} |
| 135 | |
| 136 | //lj->args.append("-o"); |
| 137 | //lj->args.append(buf_ptr(&lj->out_file)); |
| 138 | |
| 139 | //if (lj->link_in_crt) { |
| 140 | // const char *crt1o; |
| 141 | // const char *crtbegino; |
| 142 | // if (g->is_static) { |
| 143 | // crt1o = "crt1.o"; |
| 144 | // crtbegino = "crtbeginT.o"; |
| 145 | // } else { |
| 146 | // crt1o = "Scrt1.o"; |
| 147 | // crtbegino = "crtbegin.o"; |
| 148 | // } |
| 149 | // lj->args.append(get_libc_file(g, crt1o)); |
| 150 | // lj->args.append(get_libc_file(g, "crti.o")); |
| 151 | // lj->args.append(get_libc_static_file(g, crtbegino)); |
| 152 | //} |
| 153 | |
| 154 | //for (size_t i = 0; i < g->rpath_list.length; i += 1) { |
| 155 | // Buf *rpath = g->rpath_list.at(i); |
| 156 | // add_rpath(lj, rpath); |
| 157 | //} |
| 158 | //if (g->each_lib_rpath) { |
| 159 | // for (size_t i = 0; i < g->lib_dirs.length; i += 1) { |
| 160 | // const char *lib_dir = g->lib_dirs.at(i); |
| 161 | // for (size_t i = 0; i < g->link_libs_list.length; i += 1) { |
| 162 | // LinkLib *link_lib = g->link_libs_list.at(i); |
| 163 | // if (buf_eql_str(link_lib->name, "c")) { |
| 164 | // continue; |
| 165 | // } |
| 166 | // bool does_exist; |
| 167 | // Buf *test_path = buf_sprintf("%s/lib%s.so", lib_dir, buf_ptr(link_lib->name)); |
| 168 | // if (os_file_exists(test_path, &does_exist) != ErrorNone) { |
| 169 | // zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); |
| 170 | // } |
| 171 | // if (does_exist) { |
| 172 | // add_rpath(lj, buf_create_from_str(lib_dir)); |
| 173 | // break; |
| 174 | // } |
| 175 | // } |
| 176 | // } |
| 177 | //} |
| 178 | |
| 179 | //for (size_t i = 0; i < g->lib_dirs.length; i += 1) { |
| 180 | // const char *lib_dir = g->lib_dirs.at(i); |
| 181 | // lj->args.append("-L"); |
| 182 | // lj->args.append(lib_dir); |
| 183 | //} |
| 184 | |
| 185 | //if (g->libc_link_lib != nullptr) { |
| 186 | // lj->args.append("-L"); |
| 187 | // lj->args.append(buf_ptr(g->libc_lib_dir)); |
| 188 | |
| 189 | // lj->args.append("-L"); |
| 190 | // lj->args.append(buf_ptr(g->libc_static_lib_dir)); |
| 191 | //} |
| 192 | |
| 193 | //if (!g->is_static) { |
| 194 | // if (g->dynamic_linker != nullptr) { |
| 195 | // assert(buf_len(g->dynamic_linker) != 0); |
| 196 | // lj->args.append("-dynamic-linker"); |
| 197 | // lj->args.append(buf_ptr(g->dynamic_linker)); |
| 198 | // } else { |
| 199 | // Buf *resolved_dynamic_linker = get_dynamic_linker_path(g); |
| 200 | // lj->args.append("-dynamic-linker"); |
| 201 | // lj->args.append(buf_ptr(resolved_dynamic_linker)); |
| 202 | // } |
| 203 | //} |
| 204 | |
| 205 | //if (shared) { |
| 206 | // lj->args.append("-soname"); |
| 207 | // lj->args.append(buf_ptr(soname)); |
| 208 | //} |
| 209 | |
| 210 | // .o files |
| 211 | for (ctx.comp.link_objects) |link_object| { |
| 212 | const link_obj_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, link_object); |
| 213 | try ctx.args.append(link_obj_with_null.ptr); |
| 214 | } |
| 215 | try addFnObjects(ctx); |
| 216 | |
| 217 | //if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { |
| 218 | // if (g->libc_link_lib == nullptr) { |
| 219 | // Buf *builtin_o_path = build_o(g, "builtin"); |
| 220 | // lj->args.append(buf_ptr(builtin_o_path)); |
| 221 | // } |
| 222 | |
| 223 | // // sometimes libgcc is missing stuff, so we still build compiler_rt and rely on weak linkage |
| 224 | // Buf *compiler_rt_o_path = build_compiler_rt(g); |
| 225 | // lj->args.append(buf_ptr(compiler_rt_o_path)); |
| 226 | //} |
| 227 | |
| 228 | //for (size_t i = 0; i < g->link_libs_list.length; i += 1) { |
| 229 | // LinkLib *link_lib = g->link_libs_list.at(i); |
| 230 | // if (buf_eql_str(link_lib->name, "c")) { |
| 231 | // continue; |
| 232 | // } |
| 233 | // Buf *arg; |
| 234 | // if (buf_starts_with_str(link_lib->name, "/") || buf_ends_with_str(link_lib->name, ".a") || |
| 235 | // buf_ends_with_str(link_lib->name, ".so")) |
| 236 | // { |
| 237 | // arg = link_lib->name; |
| 238 | // } else { |
| 239 | // arg = buf_sprintf("-l%s", buf_ptr(link_lib->name)); |
| 240 | // } |
| 241 | // lj->args.append(buf_ptr(arg)); |
| 242 | //} |
| 243 | |
| 244 | //// libc dep |
| 245 | //if (g->libc_link_lib != nullptr) { |
| 246 | // if (g->is_static) { |
| 247 | // lj->args.append("--start-group"); |
| 248 | // lj->args.append("-lgcc"); |
| 249 | // lj->args.append("-lgcc_eh"); |
| 250 | // lj->args.append("-lc"); |
| 251 | // lj->args.append("-lm"); |
| 252 | // lj->args.append("--end-group"); |
| 253 | // } else { |
| 254 | // lj->args.append("-lgcc"); |
| 255 | // lj->args.append("--as-needed"); |
| 256 | // lj->args.append("-lgcc_s"); |
| 257 | // lj->args.append("--no-as-needed"); |
| 258 | // lj->args.append("-lc"); |
| 259 | // lj->args.append("-lm"); |
| 260 | // lj->args.append("-lgcc"); |
| 261 | // lj->args.append("--as-needed"); |
| 262 | // lj->args.append("-lgcc_s"); |
| 263 | // lj->args.append("--no-as-needed"); |
| 264 | // } |
| 265 | //} |
| 266 | |
| 267 | //// crt end |
| 268 | //if (lj->link_in_crt) { |
| 269 | // lj->args.append(get_libc_static_file(g, "crtend.o")); |
| 270 | // lj->args.append(get_libc_file(g, "crtn.o")); |
| 271 | //} |
| 272 | |
| 273 | //if (!g->is_native_target) { |
| 274 | // lj->args.append("--allow-shlib-undefined"); |
| 275 | //} |
| 276 | |
| 277 | //if (g->zig_target.os == OsZen) { |
| 278 | // lj->args.append("-e"); |
| 279 | // lj->args.append("_start"); |
| 280 | |
| 281 | // lj->args.append("--image-base=0x10000000"); |
| 282 | //} |
| 283 | } |
| 284 | |
| 285 | fn constructLinkerArgsCoff(ctx: *Context) void { |
| 286 | @panic("TODO"); |
| 287 | } |
| 288 | |
| 289 | fn constructLinkerArgsMachO(ctx: *Context) void { |
| 290 | @panic("TODO"); |
| 291 | } |
| 292 | |
| 293 | fn constructLinkerArgsWasm(ctx: *Context) void { |
| 294 | @panic("TODO"); |
| 295 | } |
| 296 | |
| 297 | fn addFnObjects(ctx: *Context) !void { |
| 298 | // at this point it's guaranteed nobody else has this lock, so we circumvent it |
| 299 | // and avoid having to be a coroutine |
| 300 | const fn_link_set = &ctx.comp.fn_link_set.private_data; |
| 301 | |
| 302 | var it = fn_link_set.first; |
| 303 | while (it) |node| { |
| 304 | const fn_val = node.data orelse { |
| 305 | // handle the tombstone. See Value.Fn.destroy. |
| 306 | it = node.next; |
| 307 | fn_link_set.remove(node); |
| 308 | ctx.comp.gpa().destroy(node); |
| 309 | continue; |
| 310 | }; |
| 311 | try ctx.args.append(fn_val.containing_object.ptr()); |
| 312 | it = node.next; |
| 313 | } |
| 314 | } |