From 857c79a6b8557a4787ea89b61a9b5d0049ba0714 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sat, 9 Oct 2021 15:03:36 +0200 Subject: [PATCH] Introduce symbol_visibility.h with macros to define hidden functions These were used in function_remapper.cpp but can be used elsewhere too, so move them to a private header. Also use them for the stack protector hidden function definition (probably not so useful since gcc2 doesn't support using the stack protector anyway?). The gcc2 way to make a symbol hidden is to manually generate the .hidden directive in the assembler output. This is not perfect: it is hard to use for C++ functions and methods (manual mangling of the name is needed), and inline assembler can only be inserted inside functions. But the alternative is patching gcc2 to add support for the function attribute, and I don't want to dig into that today. --- headers/os/BeBuild.h | 1 + headers/private/system/symbol_visibility.h | 19 +++++++++++++++++++ src/build/libroot/Jamfile | 1 + src/build/libroot/function_remapper.cpp | 11 ++--------- src/system/libroot/os/stack_protector.cpp | 8 ++++---- 5 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 headers/private/system/symbol_visibility.h diff --git a/headers/os/BeBuild.h b/headers/os/BeBuild.h index d2198a6304..71736c8e8b 100644 --- a/headers/os/BeBuild.h +++ b/headers/os/BeBuild.h @@ -82,6 +82,7 @@ #define _PACKED __attribute__((packed)) #define _PRINTFLIKE(_format_, _args_) \ __attribute__((format(__printf__, _format_, _args_))) + #if __GNUC__ >= 4 # define _EXPORT __attribute__((visibility("default"))) # define B_ALWAYS_INLINE __attribute__((always_inline)) inline diff --git a/headers/private/system/symbol_visibility.h b/headers/private/system/symbol_visibility.h new file mode 100644 index 0000000000..c7fba849ab --- /dev/null +++ b/headers/private/system/symbol_visibility.h @@ -0,0 +1,19 @@ +/* + * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + +#ifndef SYMBOL_VISIBILITY_H +#define SYMBOL_VISIBILITY_H + + +#if __GNUC__ >= 4 +# define HIDDEN_FUNCTION(function) do {} while (0) +# define HIDDEN_FUNCTION_ATTRIBUTE __attribute__((visibility("hidden"))) +#else +# define HIDDEN_FUNCTION(function) asm volatile(".hidden " #function) +# define HIDDEN_FUNCTION_ATTRIBUTE +#endif + + +#endif /* !SYMBOL_VISIBILITY_H */ diff --git a/src/build/libroot/Jamfile b/src/build/libroot/Jamfile index 11f335159a..4d4ef88f76 100644 --- a/src/build/libroot/Jamfile +++ b/src/build/libroot/Jamfile @@ -10,6 +10,7 @@ UseHeaders [ FDirName $(HAIKU_TOP) headers build os storage ] : true ; UseHeaders [ FDirName $(HAIKU_TOP) headers build os support ] : true ; UsePrivateBuildHeaders kernel libroot system ; +UsePrivateHeaders system ; { local defines = [ FDefines diff --git a/src/build/libroot/function_remapper.cpp b/src/build/libroot/function_remapper.cpp index 247010ab88..f8af40dc65 100644 --- a/src/build/libroot/function_remapper.cpp +++ b/src/build/libroot/function_remapper.cpp @@ -7,18 +7,11 @@ #include #include +#include "symbol_visibility.h" + #include "remapped_functions.h" -#if __GNUC__ >= 4 -# define HIDDEN_FUNCTION(function) do {} while (0) -# define HIDDEN_FUNCTION_ATTRIBUTE __attribute__((visibility("hidden"))) -#else -# define HIDDEN_FUNCTION(function) asm volatile(".hidden " #function) -# define HIDDEN_FUNCTION_ATTRIBUTE -#endif - - extern "C" int HIDDEN_FUNCTION_ATTRIBUTE fchmod(int fd, mode_t mode) { diff --git a/src/system/libroot/os/stack_protector.cpp b/src/system/libroot/os/stack_protector.cpp index 1c803d08ec..78dd546e89 100644 --- a/src/system/libroot/os/stack_protector.cpp +++ b/src/system/libroot/os/stack_protector.cpp @@ -9,6 +9,8 @@ #include #include +#include "private/system/symbol_visibility.h" + extern "C" { @@ -41,6 +43,7 @@ __init_stack_protector() void __stack_chk_fail() { + HIDDEN_FUNCTION(__stack_chk_fail_local); sigset_t mask; sigfillset(&mask); sigdelset(&mask, SIGABRT); @@ -51,8 +54,5 @@ __stack_chk_fail() } - -#if __GNUC__ >= 4 -extern "C" void __stack_chk_fail_local() __attribute__((visibility("hidden"))); +extern "C" void __stack_chk_fail_local() HIDDEN_FUNCTION_ATTRIBUTE; __weak_reference(__stack_chk_fail, __stack_chk_fail_local); -#endif