Skip to content

Commit

Permalink
cmake: rework elf detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Jan 10, 2024
1 parent 8d13b3b commit 69470ff
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ if (Unwind_FOUND)
endif (Unwind_FOUND)

check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
check_include_file_cxx (elf.h HAVE_ELF_H)
check_include_file_cxx (glob.h HAVE_GLOB_H)
check_include_file_cxx (link.h HAVE_LINK_H)
check_include_file_cxx (pwd.h HAVE_PWD_H)
check_include_file_cxx (sys/exec_elf.h HAVE_SYS_EXEC_ELF_H)
check_include_file_cxx (sys/syscall.h HAVE_SYS_SYSCALL_H)
check_include_file_cxx (sys/time.h HAVE_SYS_TIME_H)
check_include_file_cxx (sys/types.h HAVE_SYS_TYPES_H)
Expand Down Expand Up @@ -275,9 +278,9 @@ if (WITH_SYMBOLIZE)
set (HAVE_STACKTRACE 1)
endif (HAVE_SYMBOLIZE)
elseif (UNIX)
cmake_push_check_state (RESET)
check_cxx_symbol_exists (__ELF__ "" HAVE_SYMBOLIZE)
cmake_pop_check_state ()
if (HAVE_ELF_H OR HAVE_SYS_EXEC_ELF_H)
set (HAVE_SYMBOLIZE 1)
endif (HAVE_ELF_H OR HAVE_SYS_EXEC_ELF_H)
elseif (APPLE AND HAVE_DLADDR)
set (HAVE_SYMBOLIZE 1)
endif (WIN32 OR CYGWIN)
Expand Down
3 changes: 3 additions & 0 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def glog_library(with_gflags = 1, **kwargs):
"-Wno-unused-function",
"-Wno-unused-local-typedefs",
"-Wno-unused-variable",
"-DHAVE_ELF_H",
# Allows src/logging.cc to determine the host name.
"-DHAVE_SYS_UTSNAME_H",
# For src/utilities.cc.
Expand All @@ -71,6 +72,7 @@ def glog_library(with_gflags = 1, **kwargs):
linux_or_darwin_copts = wasm_copts + [
"-DGLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
"-DGLOG_NO_EXPORT=__attribute__((visibility(\\\"default\\\")))",
"-DHAVE_LINK_H",
"-DHAVE_MODE_T",
"-DHAVE_POSIX_FADVISE",
"-DHAVE_SSIZE_T",
Expand All @@ -86,6 +88,7 @@ def glog_library(with_gflags = 1, **kwargs):
freebsd_only_copts = [
# Enable declaration of _Unwind_Backtrace
"-D_GNU_SOURCE",
"-DHAVE_ELF_H",
]

linux_only_copts = [
Expand Down
9 changes: 9 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
/* Define to 1 if you have the <syslog.h> header file. */
#cmakedefine HAVE_SYSLOG_H

/* Define to 1 if you have the <elf.h> header file. */
#cmakedefine HAVE_ELF_H

/* Define to 1 if you have the <sys/exec_elf.h> header file. */
#cmakedefine HAVE_SYS_EXEC_ELF_H

/* Define to 1 if you have the <link.h> header file. */
#cmakedefine HAVE_LINK_H

/* Define to 1 if you have the <sys/syscall.h> header file. */
#cmakedefine HAVE_SYS_SYSCALL_H

Expand Down
7 changes: 1 addition & 6 deletions src/symbolize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,11 @@ void InstallSymbolizeOpenObjectFileCallback(
} // namespace glog_internal_namespace_
} // namespace google

# if defined(__ELF__)
# if defined(HAVE_LINK_H)

# if defined(HAVE_DLFCN_H)
# include <dlfcn.h>
# endif
# if defined(GLOG_OS_OPENBSD)
# include <sys/exec_elf.h>
# else
# include <elf.h>
# endif
# include <fcntl.h>
# include <sys/stat.h>
# include <sys/types.h>
Expand Down
53 changes: 23 additions & 30 deletions src/symbolize.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
#include "config.h"
#include "glog/platform.h"

#if defined(HAVE_ELF_H)
# include <elf.h>
#elif defined(HAVE_SYS_EXEC_ELF_H)
# include <sys/exec_elf.h>
#endif
#if defined(HAVE_LINK_H)
# include <link.h> // For ElfW() macro.
#endif

#if defined(GLOG_USE_GLOG_EXPORT)
# include "glog/export.h"
#endif
Expand All @@ -72,7 +81,7 @@
#ifndef GLOG_NO_SYMBOLIZE_DETECTION
# ifndef HAVE_SYMBOLIZE
// defined by gcc
# if defined(__ELF__) && defined(GLOG_OS_LINUX)
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
# define HAVE_SYMBOLIZE
# elif defined(GLOG_OS_MACOSX) && defined(HAVE_DLADDR)
// Use dladdr to symbolize.
Expand All @@ -86,38 +95,22 @@

#ifdef HAVE_SYMBOLIZE

# if defined(__ELF__) // defined by gcc
# if defined(__OpenBSD__)
# include <sys/exec_elf.h>
# else
# include <elf.h>
# endif

# if !defined(GLOG_OS_ANDROID)
# include <link.h> // For ElfW() macro.
# endif

// For systems where SIZEOF_VOID_P is not defined, determine it
// based on __LP64__ (defined by gcc on 64-bit systems)
# if !defined(SIZEOF_VOID_P)
# if defined(__LP64__)
# define SIZEOF_VOID_P 8
# else
# define SIZEOF_VOID_P 4
# endif
# endif
# if !defined(SIZEOF_VOID_P) && defined(__SIZEOF_POINTER__)
# define SIZEOF_VOID_P __SIZEOF_POINTER__
# endif

// If there is no ElfW macro, let's define it by ourself.
# ifndef ElfW
# if SIZEOF_VOID_P == 4
# define ElfW(type) Elf32_##type
# elif SIZEOF_VOID_P == 8
# define ElfW(type) Elf64_##type
# else
# error "Unknown sizeof(void *)"
# endif
# ifndef ElfW
# if SIZEOF_VOID_P == 4
# define ElfW(type) Elf32_##type
# elif SIZEOF_VOID_P == 8
# define ElfW(type) Elf64_##type
# else
# error "Unknown sizeof(void *)"
# endif
# endif

# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
namespace google {
inline namespace glog_internal_namespace_ {

Expand All @@ -130,7 +123,7 @@ bool GetSectionHeaderByName(int fd, const char* name, size_t name_len,
} // namespace glog_internal_namespace_
} // namespace google

# endif /* __ELF__ */
# endif

namespace google {
inline namespace glog_internal_namespace_ {
Expand Down
11 changes: 6 additions & 5 deletions src/symbolize_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ using namespace google;

# define always_inline

# if defined(__ELF__) || defined(GLOG_OS_WINDOWS) || defined(GLOG_OS_CYGWIN)
# if defined(HAVE_LINK_H) || defined(GLOG_OS_WINDOWS) || \
defined(GLOG_OS_CYGWIN)
// A wrapper function for Symbolize() to make the unit test simple.
static const char* TrySymbolize(void* pc, google::SymbolizeOptions options =
google::SymbolizeOptions::kNone) {
Expand All @@ -73,7 +74,7 @@ static const char* TrySymbolize(void* pc, google::SymbolizeOptions options =
}
# endif

# if defined(__ELF__)
# if defined(HAVE_LINK_H)

// This unit tests make sense only with GCC.
// Uses lots of GCC specific features.
Expand Down Expand Up @@ -449,15 +450,15 @@ __declspec(noinline) void TestWithReturnAddress() {
# endif
cout << "Test case TestWithReturnAddress passed." << endl;
}
# endif // __ELF__
# endif // HAVE_LINK_H
#endif // HAVE_STACKTRACE

int main(int argc, char** argv) {
FLAGS_logtostderr = true;
InitGoogleLogging(argv[0]);
InitGoogleTest(&argc, argv);
#if defined(HAVE_SYMBOLIZE) && defined(HAVE_STACKTRACE)
# if defined(__ELF__)
# if defined(HAVE_LINK_H)
// We don't want to get affected by the callback interface, that may be
// used to install some callback function at InitGoogle() time.
InstallSymbolizeCallback(nullptr);
Expand All @@ -472,7 +473,7 @@ int main(int argc, char** argv) {
# else // GLOG_OS_WINDOWS
printf("PASS (no symbolize_unittest support)\n");
return 0;
# endif // __ELF__
# endif // HAVE_LINK_H
#else
printf("PASS (no symbolize support)\n");
return 0;
Expand Down

0 comments on commit 69470ff

Please sign in to comment.