Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 7e6fca9

Browse files
authored
cmake: rework elf detection (#1058)
1 parent 8d13b3b commit 7e6fca9

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ if (Unwind_FOUND)
125125
endif (Unwind_FOUND)
126126

127127
check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
128+
check_include_file_cxx (elf.h HAVE_ELF_H)
128129
check_include_file_cxx (glob.h HAVE_GLOB_H)
130+
check_include_file_cxx (link.h HAVE_LINK_H)
129131
check_include_file_cxx (pwd.h HAVE_PWD_H)
132+
check_include_file_cxx (sys/exec_elf.h HAVE_SYS_EXEC_ELF_H)
130133
check_include_file_cxx (sys/syscall.h HAVE_SYS_SYSCALL_H)
131134
check_include_file_cxx (sys/time.h HAVE_SYS_TIME_H)
132135
check_include_file_cxx (sys/types.h HAVE_SYS_TYPES_H)
@@ -275,9 +278,9 @@ if (WITH_SYMBOLIZE)
275278
set (HAVE_STACKTRACE 1)
276279
endif (HAVE_SYMBOLIZE)
277280
elseif (UNIX)
278-
cmake_push_check_state (RESET)
279-
check_cxx_symbol_exists (__ELF__ "" HAVE_SYMBOLIZE)
280-
cmake_pop_check_state ()
281+
if (HAVE_ELF_H OR HAVE_SYS_EXEC_ELF_H)
282+
set (HAVE_SYMBOLIZE 1)
283+
endif (HAVE_ELF_H OR HAVE_SYS_EXEC_ELF_H)
281284
elseif (APPLE AND HAVE_DLADDR)
282285
set (HAVE_SYMBOLIZE 1)
283286
endif (WIN32 OR CYGWIN)

bazel/glog.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ def glog_library(with_gflags = 1, **kwargs):
8686
freebsd_only_copts = [
8787
# Enable declaration of _Unwind_Backtrace
8888
"-D_GNU_SOURCE",
89+
"-DHAVE_LINK_H",
8990
]
9091

9192
linux_only_copts = [
9293
# For utilities.h.
9394
"-DHAVE_EXECINFO_H",
95+
"-DHAVE_LINK_H",
9496
]
9597

9698
darwin_only_copts = [

src/config.h.cmake.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
/* Define to 1 if you have the <syslog.h> header file. */
5353
#cmakedefine HAVE_SYSLOG_H
5454

55+
/* Define to 1 if you have the <elf.h> header file. */
56+
#cmakedefine HAVE_ELF_H
57+
58+
/* Define to 1 if you have the <sys/exec_elf.h> header file. */
59+
#cmakedefine HAVE_SYS_EXEC_ELF_H
60+
61+
/* Define to 1 if you have the <link.h> header file. */
62+
#cmakedefine HAVE_LINK_H
63+
5564
/* Define to 1 if you have the <sys/syscall.h> header file. */
5665
#cmakedefine HAVE_SYS_SYSCALL_H
5766

src/symbolize.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,11 @@ void InstallSymbolizeOpenObjectFileCallback(
109109
} // namespace glog_internal_namespace_
110110
} // namespace google
111111

112-
# if defined(__ELF__)
112+
# if defined(HAVE_LINK_H)
113113

114114
# if defined(HAVE_DLFCN_H)
115115
# include <dlfcn.h>
116116
# endif
117-
# if defined(GLOG_OS_OPENBSD)
118-
# include <sys/exec_elf.h>
119-
# else
120-
# include <elf.h>
121-
# endif
122117
# include <fcntl.h>
123118
# include <sys/stat.h>
124119
# include <sys/types.h>

src/symbolize.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
#include "config.h"
6262
#include "glog/platform.h"
6363

64+
#if defined(HAVE_LINK_H)
65+
# include <link.h> // For ElfW() macro.
66+
#elif defined(HAVE_ELF_H)
67+
# include <elf.h>
68+
#elif defined(HAVE_SYS_EXEC_ELF_H)
69+
# include <sys/exec_elf.h>
70+
#endif
71+
6472
#if defined(GLOG_USE_GLOG_EXPORT)
6573
# include "glog/export.h"
6674
#endif
@@ -72,7 +80,7 @@
7280
#ifndef GLOG_NO_SYMBOLIZE_DETECTION
7381
# ifndef HAVE_SYMBOLIZE
7482
// defined by gcc
75-
# if defined(__ELF__) && defined(GLOG_OS_LINUX)
83+
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
7684
# define HAVE_SYMBOLIZE
7785
# elif defined(GLOG_OS_MACOSX) && defined(HAVE_DLADDR)
7886
// Use dladdr to symbolize.
@@ -86,26 +94,11 @@
8694

8795
#ifdef HAVE_SYMBOLIZE
8896

89-
# if defined(__ELF__) // defined by gcc
90-
# if defined(__OpenBSD__)
91-
# include <sys/exec_elf.h>
92-
# else
93-
# include <elf.h>
94-
# endif
95-
96-
# if !defined(GLOG_OS_ANDROID)
97-
# include <link.h> // For ElfW() macro.
98-
# endif
97+
# if !defined(SIZEOF_VOID_P) && defined(__SIZEOF_POINTER__)
98+
# define SIZEOF_VOID_P __SIZEOF_POINTER__
99+
# endif
99100

100-
// For systems where SIZEOF_VOID_P is not defined, determine it
101-
// based on __LP64__ (defined by gcc on 64-bit systems)
102-
# if !defined(SIZEOF_VOID_P)
103-
# if defined(__LP64__)
104-
# define SIZEOF_VOID_P 8
105-
# else
106-
# define SIZEOF_VOID_P 4
107-
# endif
108-
# endif
101+
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
109102

110103
// If there is no ElfW macro, let's define it by ourself.
111104
# ifndef ElfW
@@ -130,7 +123,7 @@ bool GetSectionHeaderByName(int fd, const char* name, size_t name_len,
130123
} // namespace glog_internal_namespace_
131124
} // namespace google
132125

133-
# endif /* __ELF__ */
126+
# endif
134127

135128
namespace google {
136129
inline namespace glog_internal_namespace_ {

src/symbolize_unittest.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "glog/logging.h"
4141
#include "googletest.h"
4242
#include "utilities.h"
43+
#include "stacktrace.h"
4344

4445
#ifdef GLOG_USE_GFLAGS
4546
# include <gflags/gflags.h>
@@ -60,7 +61,8 @@ using namespace google;
6061

6162
# define always_inline
6263

63-
# if defined(__ELF__) || defined(GLOG_OS_WINDOWS) || defined(GLOG_OS_CYGWIN)
64+
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H) || \
65+
defined(GLOG_OS_WINDOWS) || defined(GLOG_OS_CYGWIN)
6466
// A wrapper function for Symbolize() to make the unit test simple.
6567
static const char* TrySymbolize(void* pc, google::SymbolizeOptions options =
6668
google::SymbolizeOptions::kNone) {
@@ -73,8 +75,7 @@ static const char* TrySymbolize(void* pc, google::SymbolizeOptions options =
7375
}
7476
# endif
7577

76-
# if defined(__ELF__)
77-
78+
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
7879
// This unit tests make sense only with GCC.
7980
// Uses lots of GCC specific features.
8081
# if defined(__GNUC__) && !defined(__OPENCC__)
@@ -449,15 +450,15 @@ __declspec(noinline) void TestWithReturnAddress() {
449450
# endif
450451
cout << "Test case TestWithReturnAddress passed." << endl;
451452
}
452-
# endif // __ELF__
453-
#endif // HAVE_STACKTRACE
453+
# endif
454+
#endif // HAVE_STACKTRACE
454455

455456
int main(int argc, char** argv) {
456457
FLAGS_logtostderr = true;
457458
InitGoogleLogging(argv[0]);
458459
InitGoogleTest(&argc, argv);
459460
#if defined(HAVE_SYMBOLIZE) && defined(HAVE_STACKTRACE)
460-
# if defined(__ELF__)
461+
# if defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
461462
// We don't want to get affected by the callback interface, that may be
462463
// used to install some callback function at InitGoogle() time.
463464
InstallSymbolizeCallback(nullptr);
@@ -472,7 +473,7 @@ int main(int argc, char** argv) {
472473
# else // GLOG_OS_WINDOWS
473474
printf("PASS (no symbolize_unittest support)\n");
474475
return 0;
475-
# endif // __ELF__
476+
# endif // defined(HAVE_ELF_H) || defined(HAVE_SYS_EXEC_ELF_H)
476477
#else
477478
printf("PASS (no symbolize support)\n");
478479
return 0;

0 commit comments

Comments
 (0)