From f8f054b354088eb9e76d9207972022bc1d3dfc28 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jun 2019 17:23:24 -0400 Subject: fix `@export` for arrays not respecting the symbol name Previously, the symbol name parameter of `@export` would be ignored for variables, and the variable name would be used for the symbol name. Now it works as expected. See #2679 --- src/codegen.cpp | 67 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 29 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 12b07ea6bc..bdc8de63d1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -475,7 +475,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) { symbol_name = get_mangled_name(g, unmangled_name, false); linkage = GlobalLinkageIdInternal; } else { - FnExport *fn_export = &fn_table_entry->export_list.items[0]; + GlobalExport *fn_export = &fn_table_entry->export_list.items[0]; symbol_name = &fn_export->name; linkage = fn_export->linkage; } @@ -529,7 +529,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) { } for (size_t i = 1; i < fn_table_entry->export_list.length; i += 1) { - FnExport *fn_export = &fn_table_entry->export_list.items[i]; + GlobalExport *fn_export = &fn_table_entry->export_list.items[i]; LLVMAddAlias(g->module, LLVMTypeOf(fn_table_entry->llvm_value), fn_table_entry->llvm_value, buf_ptr(&fn_export->name)); } @@ -6691,27 +6691,14 @@ static void validate_inline_fns(CodeGen *g) { } static void set_global_tls(CodeGen *g, ZigVar *var, LLVMValueRef global_value) { - if (var->is_thread_local && (!g->is_single_threaded || var->linkage != VarLinkageInternal)) { + bool is_extern = var->decl_node->data.variable_declaration.is_extern; + bool is_export = var->decl_node->data.variable_declaration.is_export; + bool is_internal_linkage = !is_extern && !is_export; + if (var->is_thread_local && (!g->is_single_threaded || !is_internal_linkage)) { LLVMSetThreadLocalMode(global_value, LLVMGeneralDynamicTLSModel); } } -static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) { - switch (var_linkage) { - case VarLinkageInternal: - return LLVMInternalLinkage; - case VarLinkageExportStrong: - return LLVMExternalLinkage; - case VarLinkageExportWeak: - return LLVMWeakODRLinkage; - case VarLinkageExportLinkOnce: - return LLVMLinkOnceODRLinkage; - case VarLinkageExternal: - return LLVMExternalLinkage; - } - zig_unreachable(); -} - static void do_code_gen(CodeGen *g) { assert(!g->errors.length); @@ -6761,31 +6748,48 @@ static void do_code_gen(CodeGen *g) { assert(var->decl_node); + GlobalLinkageId linkage; + Buf *unmangled_name = &var->name; + Buf *symbol_name; + if (var->export_list.length == 0) { + if (var->decl_node->data.variable_declaration.is_extern) { + symbol_name = unmangled_name; + linkage = GlobalLinkageIdStrong; + } else { + symbol_name = get_mangled_name(g, unmangled_name, false); + linkage = GlobalLinkageIdInternal; + } + } else { + GlobalExport *global_export = &var->export_list.items[0]; + symbol_name = &global_export->name; + linkage = global_export->linkage; + } + LLVMValueRef global_value; - if (var->linkage == VarLinkageExternal) { - LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name)); + bool externally_initialized = var->decl_node->data.variable_declaration.expr == nullptr; + if (externally_initialized) { + LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(symbol_name)); if (existing_llvm_var) { global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(get_llvm_type(g, var->var_type), 0)); } else { - global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(&var->name)); + global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(symbol_name)); // TODO debug info for the extern variable - LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage)); + LLVMSetLinkage(global_value, to_llvm_linkage(linkage)); maybe_import_dll(g, global_value, GlobalLinkageIdStrong); LLVMSetAlignment(global_value, var->align_bytes); LLVMSetGlobalConstant(global_value, var->gen_is_const); set_global_tls(g, var, global_value); } } else { - bool exported = (var->linkage != VarLinkageInternal); - const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported)); - render_const_val(g, var->const_value, mangled_name); - render_const_val_global(g, var->const_value, mangled_name); + bool exported = (linkage != GlobalLinkageIdInternal); + render_const_val(g, var->const_value, buf_ptr(symbol_name)); + render_const_val_global(g, var->const_value, buf_ptr(symbol_name)); global_value = var->const_value->global_refs->llvm_global; if (exported) { - LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage)); + LLVMSetLinkage(global_value, to_llvm_linkage(linkage)); maybe_export_dll(g, global_value, GlobalLinkageIdStrong); } if (tld_var->section_name) { @@ -6805,6 +6809,11 @@ static void do_code_gen(CodeGen *g) { } var->value_ref = global_value; + + for (size_t export_i = 1; export_i < var->export_list.length; export_i += 1) { + GlobalExport *global_export = &var->export_list.items[export_i]; + LLVMAddAlias(g->module, LLVMTypeOf(var->value_ref), var->value_ref, buf_ptr(&global_export->name)); + } } // Generate function definitions. @@ -9168,7 +9177,7 @@ static void gen_h_file(CodeGen *g) { if (fn_table_entry->export_list.length == 0) { symbol_name = &fn_table_entry->symbol_name; } else { - FnExport *fn_export = &fn_table_entry->export_list.items[0]; + GlobalExport *fn_export = &fn_table_entry->export_list.items[0]; symbol_name = &fn_export->name; } -- cgit v1.2.3 From 362c79140fc0638b0f85ed9b2a49f3fead14bb9b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jun 2019 18:18:43 -0400 Subject: expose builtin.strip_debug_info zig code now can be made aware that it will not have any debug information available at runtime. --- src/codegen.cpp | 2 ++ src/main.cpp | 1 + 2 files changed, 3 insertions(+) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index bdc8de63d1..88168caff8 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7948,6 +7948,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing)); buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g))); buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); + buf_appendf(contents, "pub const strip_debug_info = %s;\n", bool_to_str(g->strip_debug_symbols)); { TargetSubsystem detected_subsystem = detect_subsystem(g); @@ -7988,6 +7989,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { // Only a few things affect builtin.zig cache_buf(&cache_hash, compiler_id); cache_int(&cache_hash, g->build_mode); + cache_bool(&cache_hash, g->strip_debug_symbols); cache_bool(&cache_hash, g->is_test_build); cache_bool(&cache_hash, g->is_single_threaded); cache_int(&cache_hash, g->zig_target->is_native); diff --git a/src/main.cpp b/src/main.cpp index 29c17212cf..9c14ba3250 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -954,6 +954,7 @@ int main(int argc, char **argv) { case CmdBuiltin: { CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); + codegen_set_strip(g, strip); g->subsystem = subsystem; g->valgrind_support = valgrind_support; g->want_pic = want_pic; -- cgit v1.2.3 From 7c5ceb0c4cdf5fafadd92b13048bc9878554abc4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jun 2019 18:45:41 -0400 Subject: standard library integrates with knowledge of stripped debug info --- src/codegen.cpp | 9 ++++++++- src/target.cpp | 4 ++++ src/target.hpp | 1 + std/debug.zig | 11 +++++++---- 4 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 88168caff8..cee8c0ec0c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -203,6 +203,10 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget get_target_triple(&g->triple_str, g->zig_target); g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8; + if (!target_has_debug_info(g->zig_target)) { + g->strip_debug_symbols = true; + } + return g; } @@ -248,6 +252,9 @@ void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) { void codegen_set_strip(CodeGen *g, bool strip) { g->strip_debug_symbols = strip; + if (!target_has_debug_info(g->zig_target)) { + g->strip_debug_symbols = true; + } } void codegen_set_out_name(CodeGen *g, Buf *out_name) { @@ -7514,7 +7521,7 @@ static bool detect_single_threaded(CodeGen *g) { } static bool detect_err_ret_tracing(CodeGen *g) { - return !target_is_wasm(g->zig_target) && + return !g->strip_debug_symbols && g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease; } diff --git a/src/target.cpp b/src/target.cpp index 7862f6d449..1d74304584 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -1585,3 +1585,7 @@ void target_libc_enum(size_t index, ZigTarget *out_target) { out_target->vendor = ZigLLVM_UnknownVendor; out_target->is_native = false; } + +bool target_has_debug_info(const ZigTarget *target) { + return !target_is_wasm(target); +} diff --git a/src/target.hpp b/src/target.hpp index 7fa99bcda8..7fca430df6 100644 --- a/src/target.hpp +++ b/src/target.hpp @@ -177,6 +177,7 @@ bool target_is_musl(const ZigTarget *target); bool target_is_wasm(const ZigTarget *target); bool target_is_single_threaded(const ZigTarget *target); bool target_supports_stack_probing(const ZigTarget *target); +bool target_has_debug_info(const ZigTarget *target); uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch); diff --git a/std/debug.zig b/std/debug.zig index 253f250713..ae9a5227c9 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -85,8 +85,8 @@ fn wantTtyColor() bool { /// TODO multithreaded awareness pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream() catch return; - if (os.wasi.is_the_target) { - stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + if (builtin.strip_debug_info) { + stderr.print("Unable to dump stack trace: debug info stripped\n") catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { @@ -151,8 +151,8 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace /// TODO multithreaded awareness pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void { const stderr = getStderrStream() catch return; - if (os.wasi.is_the_target) { - stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + if (builtin.strip_debug_info) { + stderr.print("Unable to dump stack trace: debug info stripped\n") catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { @@ -223,6 +223,7 @@ pub fn writeStackTrace( debug_info: *DebugInfo, tty_color: bool, ) !void { + if (builtin.strip_debug_info) return error.MissingDebugInfo; var frame_index: usize = 0; var frames_left: usize = std.math.min(stack_trace.index, stack_trace.instruction_addresses.len); @@ -783,6 +784,8 @@ pub const OpenSelfDebugInfoError = error{ }; pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { + if (builtin.strip_debug_info) + return error.MissingDebugInfo; if (windows.is_the_target) { return openSelfDebugInfoWindows(allocator); } -- cgit v1.2.3