mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
shared library fixes
This commit is contained in:
@@ -1238,12 +1238,18 @@ void lb_generate_code(lbGenerator *gen) {
|
|||||||
// NOTE(bill, 2021-05-04): Target machines must be unique to each module because they are not thread safe
|
// NOTE(bill, 2021-05-04): Target machines must be unique to each module because they are not thread safe
|
||||||
auto target_machines = array_make<LLVMTargetMachineRef>(permanent_allocator(), gen->modules.entries.count);
|
auto target_machines = array_make<LLVMTargetMachineRef>(permanent_allocator(), gen->modules.entries.count);
|
||||||
|
|
||||||
|
// NOTE(dweiler): Dynamic libraries require position-independent code.
|
||||||
|
LLVMRelocMode reloc_mode = LLVMRelocDefault;
|
||||||
|
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||||
|
reloc_mode = LLVMRelocPIC;
|
||||||
|
}
|
||||||
|
|
||||||
for_array(i, gen->modules.entries) {
|
for_array(i, gen->modules.entries) {
|
||||||
target_machines[i] = LLVMCreateTargetMachine(
|
target_machines[i] = LLVMCreateTargetMachine(
|
||||||
target, target_triple, llvm_cpu,
|
target, target_triple, llvm_cpu,
|
||||||
llvm_features,
|
llvm_features,
|
||||||
code_gen_level,
|
code_gen_level,
|
||||||
LLVMRelocDefault,
|
reloc_mode,
|
||||||
code_mode);
|
code_mode);
|
||||||
LLVMSetModuleDataLayout(gen->modules.entries[i].value->mod, LLVMCreateTargetDataLayout(target_machines[i]));
|
LLVMSetModuleDataLayout(gen->modules.entries[i].value->mod, LLVMCreateTargetDataLayout(target_machines[i]));
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-26
@@ -432,40 +432,39 @@ i32 linker_stage(lbGenerator *gen) {
|
|||||||
// typically executable files on *NIX systems don't have extensions.
|
// typically executable files on *NIX systems don't have extensions.
|
||||||
String output_ext = {};
|
String output_ext = {};
|
||||||
gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
|
gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
|
||||||
char const *linker;
|
|
||||||
|
// NOTE(dweiler): We use clang as a frontend for the linker as there are
|
||||||
|
// other runtime and compiler support libraries that need to be linked in
|
||||||
|
// very specific orders such as libgcc_s, ld-linux-so, unwind, etc.
|
||||||
|
// These are not always typically inside /lib, /lib64, or /usr versions
|
||||||
|
// of that, e.g libgcc.a is in /usr/lib/gcc/{version}, and can vary on
|
||||||
|
// the distribution of Linux even. The gcc or clang specs is the only
|
||||||
|
// reliable way to query this information to call ld directly.
|
||||||
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||||
// NOTE(tetra, 2020-11-06): __$startup_runtime must be called at DLL load time.
|
// NOTE(dweiler): Let the frontend know we're building a shared library
|
||||||
// Clang, for some reason, won't let us pass the '-init' flag that lets us do this,
|
// so it doesn't generate symbols which cannot be relocated.
|
||||||
// so use ld instead.
|
link_settings = gb_string_appendc(link_settings, "-shared ");
|
||||||
// :UseLDForShared
|
|
||||||
linker = "ld";
|
// NOTE(dweiler): __$startup_runtime must be called at initialization
|
||||||
|
// time of the shared object, we can pass -init to the linker by using
|
||||||
|
// a comma separated list of arguments to -Wl.
|
||||||
|
//
|
||||||
|
// This previously used ld but ld cannot actually build a shared library
|
||||||
|
// correctly this way since all the other dependencies provided implicitly
|
||||||
|
// by the compiler frontend are still needed and most of the command
|
||||||
|
// line arguments prepared previously are incompatible with ld.
|
||||||
|
//
|
||||||
// Shared libraries are .dylib on MacOS and .so on Linux.
|
// Shared libraries are .dylib on MacOS and .so on Linux.
|
||||||
#if defined(GB_SYSTEM_OSX)
|
#if defined(GB_SYSTEM_OSX)
|
||||||
output_ext = STR_LIT(".dylib");
|
output_ext = STR_LIT(".dylib");
|
||||||
link_settings = gb_string_appendc(link_settings, "-init '___$startup_runtime' ");
|
link_settings = gb_string_appendc(link_settings, "-Wl,-init,'___$startup_runtime' ");
|
||||||
link_settings = gb_string_appendc(link_settings, "-dylib -dynamic ");
|
|
||||||
#else
|
#else
|
||||||
output_ext = STR_LIT(".so");
|
output_ext = STR_LIT(".so");
|
||||||
link_settings = gb_string_appendc(link_settings, "-init '__$startup_runtime' ");
|
link_settings = gb_string_appendc(link_settings, "-Wl,-init,'__$startup_runtime' ");
|
||||||
link_settings = gb_string_appendc(link_settings, "-shared ");
|
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
#if defined(GB_SYSTEM_OSX)
|
|
||||||
linker = "ld";
|
|
||||||
#else
|
|
||||||
// TODO(zangent): Figure out how to make ld work on Linux.
|
|
||||||
// It probably has to do with including the entire CRT, but
|
|
||||||
// that's quite a complicated issue to solve while remaining distro-agnostic.
|
|
||||||
// Clang can figure out linker flags for us, and that's good enough _for now_.
|
|
||||||
linker = "clang -Wno-unused-command-line-argument";
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (build_context.metrics.os == TargetOs_linux) {
|
|
||||||
link_settings = gb_string_appendc(link_settings, "-no-pie ");
|
link_settings = gb_string_appendc(link_settings, "-no-pie ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (build_context.out_filepath.len > 0) {
|
if (build_context.out_filepath.len > 0) {
|
||||||
//NOTE(thebirk): We have a custom -out arguments, so we should use the extension from that
|
//NOTE(thebirk): We have a custom -out arguments, so we should use the extension from that
|
||||||
isize pos = string_extension_position(build_context.out_filepath);
|
isize pos = string_extension_position(build_context.out_filepath);
|
||||||
@@ -475,7 +474,7 @@ i32 linker_stage(lbGenerator *gen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result = system_exec_command_line_app("ld-link",
|
result = system_exec_command_line_app("ld-link",
|
||||||
"%s %s -o \"%.*s%.*s\" %s "
|
"clang -Wunused-command-line-argument %s -o \"%.*s%.*s\" %s "
|
||||||
" %s "
|
" %s "
|
||||||
" %.*s "
|
" %.*s "
|
||||||
" %.*s "
|
" %.*s "
|
||||||
@@ -492,7 +491,7 @@ i32 linker_stage(lbGenerator *gen) {
|
|||||||
// This points the linker to where the entry point is
|
// This points the linker to where the entry point is
|
||||||
" -e _main "
|
" -e _main "
|
||||||
#endif
|
#endif
|
||||||
, linker, object_files, LIT(output_base), LIT(output_ext),
|
, object_files, LIT(output_base), LIT(output_ext),
|
||||||
#if defined(GB_SYSTEM_OSX)
|
#if defined(GB_SYSTEM_OSX)
|
||||||
"-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib",
|
"-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib",
|
||||||
#else
|
#else
|
||||||
|
|||||||
Reference in New Issue
Block a user