authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-06 13:22:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-06 13:22:36-05:00
logccdef8c0fb0a80d38ce4f4ac0c57aa2b66bfcfc2
tree1efb54cff3019d9b7221991b77031a5a0c625cba
parent697b1233f0c458b8ad8c374f6f7cce451142ca71
signaturelock-open Commit is signed but in an unrecognized format.

cross compile glibc startup files


3 files changed, 86 insertions(+), 22 deletions(-)

src/codegen.cpp+4-13
......@@ -190,6 +190,9 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
190190 g->link_libs_list.append(g->libc_link_lib);
191191 }
192192
193 get_target_triple(&g->triple_str, g->zig_target);
194 g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8;
195
193196 return g;
194197}
195198
......@@ -7786,16 +7789,6 @@ static void init(CodeGen *g) {
77867789 if (g->module)
77877790 return;
77887791
7789 if (g->llvm_argv_len > 0) {
7790 const char **args = allocate_nonzero<const char *>(g->llvm_argv_len + 2);
7791 args[0] = "zig (LLVM option parsing)";
7792 for (size_t i = 0; i < g->llvm_argv_len; i += 1) {
7793 args[i + 1] = g->llvm_argv[i];
7794 }
7795 args[g->llvm_argv_len + 1] = nullptr;
7796 ZigLLVMParseCommandLineOptions(g->llvm_argv_len + 1, args);
7797 }
7798
77997792 if (g->is_test_build) {
78007793 g->subsystem = TargetSubsystemConsole;
78017794 }
......@@ -7803,8 +7796,6 @@ static void init(CodeGen *g) {
78037796 assert(g->root_out_name);
78047797 g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));
78057798
7806 get_target_triple(&g->triple_str, g->zig_target);
7807
78087799 LLVMSetTarget(g->module, buf_ptr(&g->triple_str));
78097800
78107801 if (target_object_format(g->zig_target) == ZigLLVM_COFF) {
......@@ -7860,7 +7851,7 @@ static void init(CodeGen *g) {
78607851 LLVMSetDataLayout(g->module, layout_str);
78617852
78627853
7863 g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref);
7854 assert(g->pointer_size_bytes == LLVMPointerSize(g->target_data_ref));
78647855 g->is_big_endian = (LLVMByteOrder(g->target_data_ref) == LLVMBigEndian);
78657856
78667857 g->builder = LLVMCreateBuilder();
src/link.cpp+72-7
......@@ -32,6 +32,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou
3232 child_gen->verbose_llvm_ir = parent_gen->verbose_llvm_ir;
3333 child_gen->verbose_cimport = parent_gen->verbose_cimport;
3434 child_gen->verbose_cc = parent_gen->verbose_cc;
35 child_gen->llvm_argv = parent_gen->llvm_argv;
3536
3637 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
3738 child_gen->disable_pic = parent_gen->disable_pic;
......@@ -161,6 +162,7 @@ static void glibc_add_include_dirs_arch(CFile *c_file, ZigLLVM_ArchType arch, co
161162 bool is_arm = arch == ZigLLVM_arm || arch == ZigLLVM_armeb;
162163 bool is_ppc = arch == ZigLLVM_ppc || arch == ZigLLVM_ppc64 || arch == ZigLLVM_ppc64le;
163164 bool is_riscv = arch == ZigLLVM_riscv32 || arch == ZigLLVM_riscv64;
165 bool is_sparc = arch == ZigLLVM_sparc || arch == ZigLLVM_sparcel || arch == ZigLLVM_sparcv9;
164166 bool is_64 = target_arch_pointer_bit_width(arch) == 64;
165167
166168 if (is_x86) {
......@@ -195,14 +197,29 @@ static void glibc_add_include_dirs_arch(CFile *c_file, ZigLLVM_ArchType arch, co
195197 } else {
196198 if (is_64) {
197199 c_file->args.append("-I");
198 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "mips64", dir)));
200 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "mips" OS_SEP "mips64", dir)));
199201 } else {
200202 c_file->args.append("-I");
201 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "mips32", dir)));
203 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "mips" OS_SEP "mips32", dir)));
202204 }
203205 c_file->args.append("-I");
204206 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "mips", dir)));
205207 }
208 } else if (is_sparc) {
209 if (nptl != nullptr) {
210 c_file->args.append("-I");
211 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "sparc" OS_SEP "%s", dir, nptl)));
212 } else {
213 if (is_64) {
214 c_file->args.append("-I");
215 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "sparc" OS_SEP "sparc64", dir)));
216 } else {
217 c_file->args.append("-I");
218 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "sparc" OS_SEP "sparc32", dir)));
219 }
220 c_file->args.append("-I");
221 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "sparc", dir)));
222 }
206223 } else if (is_aarch64) {
207224 if (nptl != nullptr) {
208225 c_file->args.append("-I");
......@@ -218,10 +235,10 @@ static void glibc_add_include_dirs_arch(CFile *c_file, ZigLLVM_ArchType arch, co
218235 } else {
219236 if (is_64) {
220237 c_file->args.append("-I");
221 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "powerpc64", dir)));
238 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "powerpc" OS_SEP "powerpc64", dir)));
222239 } else {
223240 c_file->args.append("-I");
224 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "powerpc32", dir)));
241 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "powerpc" OS_SEP "powerpc32", dir)));
225242 }
226243 c_file->args.append("-I");
227244 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "powerpc", dir)));
......@@ -288,11 +305,59 @@ static void glibc_add_include_dirs(CodeGen *parent, CFile *c_file) {
288305 c_file->args.append(path_from_libc(parent, "glibc"));
289306}
290307
308static const char *glibc_start_asm_path(CodeGen *parent, const char *file) {
309 ZigLLVM_ArchType arch = parent->zig_target->arch;
310 bool is_aarch64 = arch == ZigLLVM_aarch64 || arch == ZigLLVM_aarch64_be;
311 bool is_mips = arch == ZigLLVM_mips || arch == ZigLLVM_mipsel ||
312 arch == ZigLLVM_mips64el || arch == ZigLLVM_mips64;
313 bool is_arm = arch == ZigLLVM_arm || arch == ZigLLVM_armeb;
314 bool is_ppc = arch == ZigLLVM_ppc || arch == ZigLLVM_ppc64 || arch == ZigLLVM_ppc64le;
315 bool is_riscv = arch == ZigLLVM_riscv32 || arch == ZigLLVM_riscv64;
316 bool is_sparc = arch == ZigLLVM_sparc || arch == ZigLLVM_sparcel || arch == ZigLLVM_sparcv9;
317 bool is_64 = target_arch_pointer_bit_width(arch) == 64;
318
319 Buf result = BUF_INIT;
320 buf_resize(&result, 0);
321 buf_append_buf(&result, parent->zig_lib_dir);
322 buf_append_str(&result, OS_SEP "libc" OS_SEP "glibc" OS_SEP "sysdeps" OS_SEP);
323 if (arch == ZigLLVM_nios2) {
324 buf_append_str(&result, "nios2");
325 } else if (is_sparc) {
326 if (is_64) {
327 buf_append_str(&result, "sparc" OS_SEP "sparc64");
328 } else {
329 buf_append_str(&result, "sparc" OS_SEP "sparc32");
330 }
331 } else if (is_arm) {
332 buf_append_str(&result, "arm");
333 } else if (is_mips) {
334 buf_append_str(&result, "mips");
335 } else if (arch == ZigLLVM_x86_64) {
336 buf_append_str(&result, "x86_64");
337 } else if (arch == ZigLLVM_x86) {
338 buf_append_str(&result, "i386");
339 } else if (is_aarch64) {
340 buf_append_str(&result, "aarch64");
341 } else if (is_riscv) {
342 buf_append_str(&result, "riscv");
343 } else if (is_ppc) {
344 if (is_64) {
345 buf_append_str(&result, "powerpc" OS_SEP "powerpc64");
346 } else {
347 buf_append_str(&result, "powerpc" OS_SEP "powerpc32");
348 }
349 }
350
351 buf_append_str(&result, OS_SEP);
352 buf_append_str(&result, file);
353 return buf_ptr(&result);
354}
355
291356static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
292357 if (parent->libc == nullptr && target_is_glibc(parent)) {
293358 if (strcmp(file, "crti.o") == 0) {
294359 CFile *c_file = allocate<CFile>(1);
295 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "sysdeps" OS_SEP "x86_64" OS_SEP "crti.S");
360 c_file->source_path = glibc_start_asm_path(parent, "crti.S");
296361 glibc_add_include_dirs(parent, c_file);
297362 c_file->args.append("-D_LIBC_REENTRANT");
298363 c_file->args.append("-include");
......@@ -314,7 +379,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
314379 return build_libc_object(parent, "crti", c_file);
315380 } else if (strcmp(file, "crtn.o") == 0) {
316381 CFile *c_file = allocate<CFile>(1);
317 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "sysdeps" OS_SEP "x86_64" OS_SEP "crtn.S");
382 c_file->source_path = glibc_start_asm_path(parent, "crtn.S");
318383 glibc_add_include_dirs(parent, c_file);
319384 c_file->args.append("-D_LIBC_REENTRANT");
320385 c_file->args.append("-DMODULE_NAME=libc");
......@@ -325,7 +390,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
325390 return build_libc_object(parent, "crtn", c_file);
326391 } else if (strcmp(file, "start.os") == 0) {
327392 CFile *c_file = allocate<CFile>(1);
328 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "sysdeps" OS_SEP "x86_64" OS_SEP "start.S");
393 c_file->source_path = glibc_start_asm_path(parent, "start.S");
329394 glibc_add_include_dirs(parent, c_file);
330395 c_file->args.append("-D_LIBC_REENTRANT");
331396 c_file->args.append("-include");
src/main.cpp+10-2
......@@ -409,7 +409,6 @@ int main(int argc, char **argv) {
409409 const char *dynamic_linker = nullptr;
410410 const char *libc_txt = nullptr;
411411 ZigList<const char *> clang_argv = {0};
412 ZigList<const char *> llvm_argv = {0};
413412 ZigList<const char *> lib_dirs = {0};
414413 ZigList<const char *> link_libs = {0};
415414 ZigList<const char *> forbidden_link_libs = {0};
......@@ -443,6 +442,9 @@ int main(int argc, char **argv) {
443442 Buf *main_pkg_path = nullptr;
444443 ValgrindSupport valgrind_support = ValgrindSupportAuto;
445444
445 ZigList<const char *> llvm_argv = {0};
446 llvm_argv.append("zig (LLVM option parsing)");
447
446448 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
447449 Buf zig_exe_path_buf = BUF_INIT;
448450 if ((err = os_self_exe_path(&zig_exe_path_buf))) {
......@@ -1027,7 +1029,13 @@ int main(int argc, char **argv) {
10271029 codegen_set_each_lib_rpath(g, each_lib_rpath);
10281030
10291031 codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
1030 codegen_set_llvm_argv(g, llvm_argv.items, llvm_argv.length);
1032
1033 if (llvm_argv.length > 1) {
1034 llvm_argv.append(nullptr);
1035 ZigLLVMParseCommandLineOptions(llvm_argv.length, llvm_argv.items);
1036 }
1037
1038 codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);
10311039 codegen_set_strip(g, strip);
10321040 codegen_set_is_static(g, is_static);
10331041 if (dynamic_linker != nullptr)