Skip to contentSkip to conversation

CMakeLists.txt: Add STATIC_LINK option

PATCH In review Douglas RAILLARD 2 participants 10 messages Oct 15, 2021 – Oct 20, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CMakeLists.txt: Add STATIC_LINK option

From: Douglas Raillard <[email protected]>

Add a user-defined STATIC_LINK option that can be used to build a fully static binary for the executables:

cmake .. -DSTATIC_LINK=ON

This has been tested on Alpine Linux v3.14.

---
 CMakeLists.txt                | 21 +++++++++++++++------
 README                        |  3 +++
 cmake/modules/FindDWARF.cmake | 10 ++++++++++
 3 files changed, 28 insertions(+), 6 deletions(-)
CMakeLists.txt +15 -6
index 2ab66e4..ba467bf 100644
@@ -34,6 +34,15 @@ macro(_set_fancy _var _value _comment)
 	endif (NOT DEFINED ${_var})
 endmacro(_set_fancy)
 
+option(BUILD_SHARED_LIBS "Build internal libraries as shared libraries" ON)
+option(STATIC_LINK "Create statically linked executables" OFF)
+if (STATIC_LINK)
+  string(APPEND CMAKE_C_FLAGS " -static")
+  string(APPEND CMAKE_EXE_LINKER_FLAGS " -static")
+  set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
+  set(BUILD_SHARED_LIBS OFF)
+endif()
+
 # where to look first for cmake modules,
 # before ${CMAKE_ROOT}/Modules/ is checked
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
@@ -48,11 +57,6 @@ set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -ggdb -O0")
 set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
 
-if (NOT DEFINED BUILD_SHARED_LIBS)
-	set (BUILD_SHARED_LIBS ON)
-	message(STATUS "Setting BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
-endif (NOT DEFINED BUILD_SHARED_LIBS)
-
 # Just for grepping, DWARVES_VERSION isn't used anywhere anymore
 # add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.22")
 add_definitions(-D_GNU_SOURCE -DDWARVES_MAJOR_VERSION=1)
@@ -123,7 +127,7 @@ endif()
 add_library(dwarves ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
@@ -193,3 +197,8 @@ endif()
 install(PROGRAMS btfdiff fullcircle DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
 install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.blacklist.cu
 	DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime)
+
+# Avoid having a trailing -Wl,-Bdynamic that will make some linkers think we
+# need to link against a DSO for the libc.
+get_property(TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
+set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
README +3 -0
index c9f1737..5798458 100644
@@ -18,6 +18,9 @@ cmake Options:
     Default is to install to /usr/local, use -DCMAKE_INSTALL_PREFIX=
     when invoking cmake to specify another install location.
 
+  -DSTATIC_LINK
+    Build a statically linked binary. Default is OFF.
+
 Known to work scenarios:
 
 Mandriva Cooker:
cmake/modules/FindDWARF.cmake +10 -0
index 027d06e..1b4ac49 100644
@@ -37,6 +37,16 @@ find_library(ELF_LIBRARY
 	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
 )
 
+find_library(BZ2_LIBRARY
+	NAMES bz2
+	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
+find_library(LZMA_LIBRARY
+	NAMES lzma
+	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
 if (DWARF_INCLUDE_DIR AND LIBDW_INCLUDE_DIR AND DWARF_LIBRARY AND ELF_LIBRARY)
 	set(DWARF_FOUND TRUE)
 	set(DWARF_LIBRARIES ${DWARF_LIBRARY} ${ELF_LIBRARY})
Signature · 1 line
2.25.1
#1 lore raw

Em Fri, Oct 15, 2021 at 11:10:08AM +0100, Douglas RAILLARD escreveu:

Quoted 8 lines

From: Douglas Raillard <[email protected]>

Add a user-defined STATIC_LINK option that can be used to build a fully static binary for the executables:

cmake .. -DSTATIC_LINK=ON

This has been tested on Alpine Linux v3.14.

Cool, in addition to:

⬢[acme@toolbox pahole]$ cmake -D__LIB=lib -DBUILD_SHARED_LIBS=OFF .. <SNIP>
⬢[acme@toolbox pahole]$ ls -la build
total 11428
drwxr-xr-x. 1 acme acme 342 Oct 15 10:15 .
drwxrwxr-x. 1 acme acme 3228 Oct 15 10:15 ..
-rw-r--r--. 1 acme acme 14858 Oct 15 10:15 CMakeCache.txt
drwxr-xr-x. 1 acme acme 598 Oct 15 10:15 CMakeFiles
-rw-r--r--. 1 acme acme 20280 Oct 15 10:15 cmake_install.cmake
-rwxr-xr-x. 1 acme acme 1027016 Oct 15 10:15 codiff -rwxr-xr-x. 1 acme acme 1067304 Oct 15 10:15 ctracer -rwxr-xr-x. 1 acme acme 999648 Oct 15 10:15 dtagnames -rw-r--r--. 1 acme acme 2112500 Oct 15 10:15 libdwarves.a

-rw-r--r--. 1 acme acme   40086 Oct 15 10:15 libdwarves_emit.a
-rw-r--r--. 1 acme acme   40558 Oct 15 10:15 libdwarves_reorganize.a
-rw-r--r--. 1 acme acme   45792 Oct 15 10:15 Makefile

-rwxr-xr-x. 1 acme acme 1160712 Oct 15 10:15 pahole -rwxr-xr-x. 1 acme acme 1009408 Oct 15 10:15 pdwtags -rwxr-xr-x. 1 acme acme 1044160 Oct 15 10:15 pfunct -rwxr-xr-x. 1 acme acme 1011488 Oct 15 10:15 pglobal -rwxr-xr-x. 1 acme acme 1008784 Oct 15 10:15 prefcnt
-rwxr-xr-x. 1 acme acme 62096 Oct 15 10:15 scncopy
-rwxr-xr-x. 1 acme acme 1008392 Oct 15 10:15 syscse ⬢[acme@toolbox pahole]$ ldd build/pahole

	linux-vdso.so.1 (0x00007ffc3fd48000)
	libdw.so.1 => /lib64/libdw.so.1 (0x00007f7c9592b000)
	libelf.so.1 => /lib64/libelf.so.1 (0x00007f7c95910000)
	libz.so.1 => /lib64/libz.so.1 (0x00007f7c958f6000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f7c958d5000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f7c95706000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f7c956ff000)
	libzstd.so.1 => /lib64/libzstd.so.1 (0x00007f7c95607000)
	liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f7c955db000)
	libbz2.so.1 => /lib64/libbz2.so.1 (0x00007f7c955c8000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f7c959ce000)

⬢[acme@toolbox pahole]$

That is just for libdwarves. Humm, with your patch I'm getting this warning:

⬢[acme@toolbox pahole]$ rm -rf build
⬢[acme@toolbox pahole]$ mkdir build
⬢[acme@toolbox pahole]$ cd build
⬢[acme@toolbox build]$ cmake -DSTATIC_LINK=ON ..
-- The C compiler identification is GNU 11.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Setting BUILD_SHARED_LIBS = ON
-- Checking availability of DWARF and ELF development libraries -- Looking for dwfl_module_build_id in elf
-- Looking for dwfl_module_build_id in elf - found
-- Found dwarf.h header: /usr/include
-- Found elfutils/libdw.h header: /usr/include
-- Found libdw library: /usr/lib64/libdw.so
-- Found libelf library: /usr/lib64/libelf.so
-- Checking availability of DWARF and ELF development libraries - done -- Found ZLIB: /usr/lib64/libz.so (found version "1.2.11") -- Checking availability of argp library
-- Assuming argp is in libc
-- Checking availability of argp library - done
-- Checking availability of obstack library
-- Assuming obstack is in libc
-- Checking availability of obstack library - done
-- Submodule update
-- Submodule update - done
-- Performing Test HAVE_REALLOCARRAY_SUPPORT
-- Performing Test HAVE_REALLOCARRAY_SUPPORT - Success
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

STATIC_LINK

-- Build files have been written to: /var/home/acme/git/pahole/build ⬢[acme@toolbox build]$

And when I build I don't get a static binary, can you check? I'm doing a bit of investigation now.

- Arnaldo

Quoted 94 lines
---
 CMakeLists.txt                | 21 +++++++++++++++------
 README                        |  3 +++
 cmake/modules/FindDWARF.cmake | 10 ++++++++++
 3 files changed, 28 insertions(+), 6 deletions(-)
CMakeLists.txt +15 -6
index 2ab66e4..ba467bf 100644
@@ -34,6 +34,15 @@ macro(_set_fancy _var _value _comment)
 	endif (NOT DEFINED ${_var})
 endmacro(_set_fancy)
 
+option(BUILD_SHARED_LIBS "Build internal libraries as shared libraries" ON)
+option(STATIC_LINK "Create statically linked executables" OFF)
+if (STATIC_LINK)
+  string(APPEND CMAKE_C_FLAGS " -static")
+  string(APPEND CMAKE_EXE_LINKER_FLAGS " -static")
+  set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
+  set(BUILD_SHARED_LIBS OFF)
+endif()
+
 # where to look first for cmake modules,
 # before ${CMAKE_ROOT}/Modules/ is checked
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
@@ -48,11 +57,6 @@ set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -ggdb -O0")
 set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
 
-if (NOT DEFINED BUILD_SHARED_LIBS)
-	set (BUILD_SHARED_LIBS ON)
-	message(STATUS "Setting BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
-endif (NOT DEFINED BUILD_SHARED_LIBS)
-
 # Just for grepping, DWARVES_VERSION isn't used anywhere anymore
 # add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.22")
 add_definitions(-D_GNU_SOURCE -DDWARVES_MAJOR_VERSION=1)
@@ -123,7 +127,7 @@ endif()
 add_library(dwarves ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
@@ -193,3 +197,8 @@ endif()
 install(PROGRAMS btfdiff fullcircle DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
 install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.blacklist.cu
 	DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime)
+
+# Avoid having a trailing -Wl,-Bdynamic that will make some linkers think we
+# need to link against a DSO for the libc.
+get_property(TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
+set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
README +3 -0
index c9f1737..5798458 100644
@@ -18,6 +18,9 @@ cmake Options:
     Default is to install to /usr/local, use -DCMAKE_INSTALL_PREFIX=
     when invoking cmake to specify another install location.
 
+  -DSTATIC_LINK
+    Build a statically linked binary. Default is OFF.
+
 Known to work scenarios:
 
 Mandriva Cooker:
cmake/modules/FindDWARF.cmake +10 -0
index 027d06e..1b4ac49 100644
@@ -37,6 +37,16 @@ find_library(ELF_LIBRARY
 	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
 )
 
+find_library(BZ2_LIBRARY
+	NAMES bz2
+	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
+find_library(LZMA_LIBRARY
+	NAMES lzma
+	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
 if (DWARF_INCLUDE_DIR AND LIBDW_INCLUDE_DIR AND DWARF_LIBRARY AND ELF_LIBRARY)
 	set(DWARF_FOUND TRUE)
 	set(DWARF_LIBRARIES ${DWARF_LIBRARY} ${ELF_LIBRARY})
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
#2 lore raw

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 48 lines

Em Fri, Oct 15, 2021 at 11:10:08AM +0100, Douglas RAILLARD escreveu:

Quoted 8 lines

From: Douglas Raillard <[email protected]>

Add a user-defined STATIC_LINK option that can be used to build a fully static binary for the executables:

cmake .. -DSTATIC_LINK=ON

This has been tested on Alpine Linux v3.14.

That is just for libdwarves. Humm, with your patch I'm getting this warning:

⬢[acme@toolbox pahole]$ rm -rf build
⬢[acme@toolbox pahole]$ mkdir build
⬢[acme@toolbox pahole]$ cd build
⬢[acme@toolbox build]$ cmake -DSTATIC_LINK=ON ..
-- The C compiler identification is GNU 11.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Setting BUILD_SHARED_LIBS = ON
-- Checking availability of DWARF and ELF development libraries -- Looking for dwfl_module_build_id in elf
-- Looking for dwfl_module_build_id in elf - found
-- Found dwarf.h header: /usr/include
-- Found elfutils/libdw.h header: /usr/include
-- Found libdw library: /usr/lib64/libdw.so
-- Found libelf library: /usr/lib64/libelf.so
-- Checking availability of DWARF and ELF development libraries - done -- Found ZLIB: /usr/lib64/libz.so (found version "1.2.11") -- Checking availability of argp library
-- Assuming argp is in libc
-- Checking availability of argp library - done
-- Checking availability of obstack library
-- Assuming obstack is in libc
-- Checking availability of obstack library - done
-- Submodule update
-- Submodule update - done
-- Performing Test HAVE_REALLOCARRAY_SUPPORT
-- Performing Test HAVE_REALLOCARRAY_SUPPORT - Success
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

Quoted 8 lines

STATIC_LINK

-- Build files have been written to: /var/home/acme/git/pahole/build ⬢[acme@toolbox build]$

And when I build I don't get a static binary, can you check? I'm doing a bit of investigation now.

Nevermind, brown paper bag on my head, this is _without_ your patch, doh, with it I first get:

⬢[acme@toolbox build]$ cmake -DSTATIC_LINK=ON ..
-- The C compiler identification is GNU 11.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking availability of DWARF and ELF development libraries -- Please install the elfutils-libs package
-- Please install the elfutils-libelf package
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

- Arnaldo

#3 lore raw

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

that needs some more magic that I already forgot :-\

But now out to lunch.

- Arnaldo

#4 lore raw

Em Fri, Oct 15, 2021 at 11:20:40AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 45 lines

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 3 lines

that needs some more magic that I already forgot :-\

But now out to lunch.

Before that:

[root@cc85f3a7b96f dwarves-1.22]# file build/pahole
build/pahole: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=c972bee0f67994e0c161ac3cf8bcc8b7f609d6ee, for GNU/Linux 3.2.0, with debug_info, not stripped, too many notes (256) [root@cc85f3a7b96f dwarves-1.22]#

This after applying the patch below after yours, but then:

[root@cc85f3a7b96f dwarves-1.22]# build/pahole build/pahole Segmentation fault (core dumped)
[root@cc85f3a7b96f dwarves-1.22]#

That doesn't happen with a non-static build:

[root@cc85f3a7b96f dwarves-1.22]# build/pahole list_head

struct list_head {
	struct list_head *         next;                 /*     0     8 */
	struct list_head *         prev;                 /*     8     8 */

	/* size: 16, cachelines: 1, members: 2 */
	/* last cacheline: 16 bytes */
};
[root@cc85f3a7b96f dwarves-1.22]# ldd build/pahole
	linux-vdso.so.1 (0x00007fff08654000)
	libdwarves_reorganize.so.1 => /root/dwarves-1.22/build/libdwarves_reorganize.so.1 (0x00007f8af5b45000)
	libdwarves.so.1 => /root/dwarves-1.22/build/libdwarves.so.1 (0x00007f8af5aad000)
	libdw.so.1 => /lib64/libdw.so.1 (0x00007f8af5a07000)
	libelf.so.1 => /lib64/libelf.so.1 (0x00007f8af59ec000)
	libz.so.1 => /lib64/libz.so.1 (0x00007f8af59d2000)
	libbz2.so.1 => /lib64/libbz2.so.1 (0x00007f8af59bf000)
	liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f8af5993000)
	libzstd.so.1 => /lib64/libzstd.so.1 (0x00007f8af58b5000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f8af58ae000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8af588c000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f8af56c2000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f8af5b4d000)

[root@cc85f3a7b96f dwarves-1.22]#

- Arnaldo

CMakeLists.txt +1 -1
index ba467bf4dcff3106..73d8919056281f94 100644
@@ -127,7 +127,7 @@ endif()
 add_library(dwarves ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} ${ZSTD_LIBRARY} ${DL_LIBRARY})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
cmake/modules/FindDWARF.cmake +10 -0
index 1b4ac499ea07e267..8fa5cc47cea86486 100644
@@ -47,6 +47,16 @@ find_library(LZMA_LIBRARY
 	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
 )
 
+find_library(ZSTD_LIBRARY
+	NAMES zstd
+	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
+find_library(DL_LIBRARY
+	NAMES dl
+	PATHS /lib /lib64 /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
 if (DWARF_INCLUDE_DIR AND LIBDW_INCLUDE_DIR AND DWARF_LIBRARY AND ELF_LIBRARY)
 	set(DWARF_FOUND TRUE)
 	set(DWARF_LIBRARIES ${DWARF_LIBRARY} ${ELF_LIBRARY})
#5 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 45 lines · Arnaldo Carvalho de Melo

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 1 line

that needs some more magic that I already forgot :-\

When trying on Ubuntu it also appeared that find_library() for libebl was also needed, but not on Alpine Linux so I did not include it, as it does not seem necessary on Alpine. I'm not sure how we want to go about these:
1) Take the union of all the dependencies that seem to be necessary and deal gracefully in CMakeLists.txt with failure to find a lib. This will result in linker error if a lib is actually necessary and not found.
2) Select a blessed distro for static building and only maintain that
3) Find some cmake builtin "magic" so that the user can point at existing lib that seem to not be detected on their distro. I'm quite rusty in cmake so maybe there is a better way than sprinkling find_library() everywhere.

So far I opted for approach 2) with Alpine, as building on e.g. Ubuntu will not work in any case, because glibc does not support static linking (it kind of does until it does not anymore). Alpine uses the musl libc which supports static linking.

As for the dlopen/dlsym/dlclose issue I'm not sure. Maybe these symbols are just not present in static glibc ? The binary I produced on Alpine did not seem to segfault [1]. If you want to give it a go, this scripts set it up in ~20s:
https://github.com/alpinelinux/alpine-chroot-install

Packages: bash gcc git make cmake musl-dev zlib-static bzip2-static libelf-static libbpf-dev musl-obstack-dev argp-standalone linux-headers

[1] pahole static binaries:
https://github.com/ARM-software/lisa/pull/1740/files

Quoted 5 lines

But now out to lunch.

- Arnaldo

Cheer,
Douglas
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

#6 lore raw

Em Mon, Oct 18, 2021 at 10:57:43AM +0100, Douglas Raillard escreveu:

Quoted 66 lines · Arnaldo Carvalho de Melo
Quoted 45 lines

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 1 line

that needs some more magic that I already forgot :-\

When trying on Ubuntu it also appeared that find_library() for libebl was also needed, but not on Alpine Linux so I did not include it, as it does not seem necessary on Alpine. I'm not sure how we want to go about these:
1) Take the union of all the dependencies that seem to be necessary and deal gracefully in CMakeLists.txt with failure to find a lib. This will result in linker error if a lib is actually necessary and not found.
2) Select a blessed distro for static building and only maintain that
3) Find some cmake builtin "magic" so that the user can point at existing lib that seem to not be detected on their distro. I'm quite rusty in cmake so maybe there is a better way than sprinkling find_library() everywhere.

So far I opted for approach 2) with Alpine, as building on e.g. Ubuntu will not work in any case, because glibc does not support static linking (it kind of does until it does not anymore). Alpine uses the musl libc which supports static linking.

Quoted 8 lines

As for the dlopen/dlsym/dlclose issue I'm not sure. Maybe these symbols are just not present in static glibc ? The binary I produced on Alpine did not seem to segfault [1]. If you want to give it a go, this scripts set it up in ~20s:
https://github.com/alpinelinux/alpine-chroot-install

Packages: bash gcc git make cmake musl-dev zlib-static bzip2-static libelf-static libbpf-dev musl-obstack-dev argp-standalone linux-headers

I'll try with this later, thing is, I want to build on the CI for pahole that Andrii put in place in the libbpf repo, so we need to check how to build this static build.

- Arnaldo

Quoted 11 lines

[1] pahole static binaries:
https://github.com/ARM-software/lisa/pull/1740/files

Quoted 5 lines

But now out to lunch.

- Arnaldo

Cheer,
Douglas
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Signature · 1 line
- Arnaldo
#7 lore raw
Quoted 73 lines · Arnaldo Carvalho de Melo

Em Mon, Oct 18, 2021 at 10:57:43AM +0100, Douglas Raillard escreveu:

Quoted 72 lines · Arnaldo Carvalho de Melo
Quoted 45 lines

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 1 line

that needs some more magic that I already forgot :-\

When trying on Ubuntu it also appeared that find_library() for libebl was also needed, but not on Alpine Linux so I did not include it, as it does not seem necessary on Alpine. I'm not sure how we want to go about these:
1) Take the union of all the dependencies that seem to be necessary and deal gracefully in CMakeLists.txt with failure to find a lib. This will result in linker error if a lib is actually necessary and not found.
2) Select a blessed distro for static building and only maintain that
3) Find some cmake builtin "magic" so that the user can point at existing lib that seem to not be detected on their distro. I'm quite rusty in cmake so maybe there is a better way than sprinkling find_library() everywhere.

So far I opted for approach 2) with Alpine, as building on e.g. Ubuntu will not work in any case, because glibc does not support static linking (it kind of does until it does not anymore). Alpine uses the musl libc which supports static linking.

As for the dlopen/dlsym/dlclose issue I'm not sure. Maybe these symbols are just not present in static glibc ? The binary I produced on Alpine did not seem to segfault [1]. If you want to give it a go, this scripts set it up in ~20s:
https://github.com/alpinelinux/alpine-chroot-install

Quoted 6 lines
Quoted 2 lines

Packages: bash gcc git make cmake musl-dev zlib-static bzip2-static libelf-static libbpf-dev musl-obstack-dev argp-standalone linux-headers

I'll try with this later, thing is, I want to build on the CI for pahole that Andrii put in place in the libbpf repo, so we need to check how to build this static build.

Ok, I had a quick look and it seems to be Ubuntu/Debian based with github actions. libdwfl seems to have some sort of hard dependency on dlopen in __libdwfl_debuginfod_init. I'll give a go at the musl-gcc (musl-tools on Ubuntu) to see if we can get a working static bin.

Douglas

Quoted 13 lines
Quoted 12 lines

[1] pahole static binaries:
https://github.com/ARM-software/lisa/pull/1740/files

Quoted 5 lines

But now out to lunch.

- Arnaldo

Cheer,
Douglas
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

#8 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 73 lines · Arnaldo Carvalho de Melo

Em Mon, Oct 18, 2021 at 10:57:43AM +0100, Douglas Raillard escreveu:

Quoted 72 lines · Arnaldo Carvalho de Melo
Quoted 45 lines

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 1 line

that needs some more magic that I already forgot :-\

When trying on Ubuntu it also appeared that find_library() for libebl was also needed, but not on Alpine Linux so I did not include it, as it does not seem necessary on Alpine. I'm not sure how we want to go about these:
1) Take the union of all the dependencies that seem to be necessary and deal gracefully in CMakeLists.txt with failure to find a lib. This will result in linker error if a lib is actually necessary and not found.
2) Select a blessed distro for static building and only maintain that
3) Find some cmake builtin "magic" so that the user can point at existing lib that seem to not be detected on their distro. I'm quite rusty in cmake so maybe there is a better way than sprinkling find_library() everywhere.

So far I opted for approach 2) with Alpine, as building on e.g. Ubuntu will not work in any case, because glibc does not support static linking (it kind of does until it does not anymore). Alpine uses the musl libc which supports static linking.

As for the dlopen/dlsym/dlclose issue I'm not sure. Maybe these symbols are just not present in static glibc ? The binary I produced on Alpine did not seem to segfault [1]. If you want to give it a go, this scripts set it up in ~20s:
https://github.com/alpinelinux/alpine-chroot-install

Quoted 6 lines
Quoted 2 lines

Packages: bash gcc git make cmake musl-dev zlib-static bzip2-static libelf-static libbpf-dev musl-obstack-dev argp-standalone linux-headers

I'll try with this later, thing is, I want to build on the CI for pahole that Andrii put in place in the libbpf repo, so we need to check how to build this static build.

I tried to make it work on Ubuntu and came up with that. It is quite stinky and does not result in a fully static build, but it's probably as close we can get to it using glibc, and it does not segfault:

Quoted 1 line
Quoted 1 line
Quoted 1 line

ldd pahole

        linux-vdso.so.1 (0x00007ffd9b7e2000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f049a55e000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f049a53b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f049a349000)
        /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f049a584000)

Subject: [PATCH] CMakeLists.txt: Allow pseudo-static build on Ubuntu

Allow STATIC_LINK=ON on Ubuntu by:

* providing libebl
* avoiding a trailing -Wl,-Bstatic that makes the resulting binary segfault
* dynamically link to libdl. Static linking to that lib would impose that the same glibc version used for linking is present on the runtime system, which is not very safe and would mislead into thinking the binary is safe to use everywhere.

Apart from that, all the other libraries are statically linked to the executable.

Note: the user might need a symlink /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 The ld option --dynamic-linker=file is supposed to allow fixing that, but it's unfortunately ignored when used with -static.

---
 CMakeLists.txt                | 18 ++++++++++++++++--
 cmake/modules/FindDWARF.cmake |  8 ++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
CMakeLists.txt +16 -2
index ba467bf..adb6d74 100644
@@ -2,6 +2,11 @@ project(pahole C)
 cmake_minimum_required(VERSION 2.8.12)
 cmake_policy(SET CMP0005 NEW)
 
+execute_process(COMMAND lsb_release -si
+  OUTPUT_VARIABLE LINUX_DISTRO
+  OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
 option(LIBBPF_EMBEDDED "Use the embedded version of libbpf instead of searching it via pkg-config" ON)
 if (NOT LIBBPF_EMBEDDED)
 	find_package(PkgConfig REQUIRED)
@@ -127,7 +132,12 @@ endif()
 add_library(dwarves ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} ${EBL_LIBRARY})
+# On non-musl systems, we need to link to libdl in order to get
+# dlopen/dlclose/dlsym, as required by libdwfl and libebl
+if (LINUX_DISTRO STREQUAL "Ubuntu")
+  target_link_libraries(dwarves dl)
+endif()
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
@@ -201,4 +211,8 @@ install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.bla
 # Avoid having a trailing -Wl,-Bdynamic that will make some linkers think we
 # need to link against a DSO for the libc.
 get_property(TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
-set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
+# If we use a trailing -Wl,-Bstatic on ubuntu, the binary will segfault for some
+# reasons ...
+if (NOT LINUX_DISTRO STREQUAL "Ubuntu")
+  set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
+endif()
cmake/modules/FindDWARF.cmake +8 -0
index 1b4ac49..cd1f265 100644
@@ -27,6 +27,14 @@ find_path(LIBDW_INCLUDE_DIR elfutils/libdw.h
 	~/usr/local/include
 )
 
+# This does not seem to be necessary on Alpine Linux
+if (LINUX_DISTRO STREQUAL "Ubuntu")
+  find_library(EBL_LIBRARY
+    NAMES ebl
+    PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+  )
+endif()
+
 find_library(DWARF_LIBRARY
 	NAMES dw dwarf
 	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
Signature · 1 line
2.25.1
#9 lore raw

Em Tue, Oct 19, 2021 at 06:29:41PM +0100, Douglas Raillard escreveu:

Quoted 77 lines · Arnaldo Carvalho de Melo
Quoted 76 lines

Em Mon, Oct 18, 2021 at 10:57:43AM +0100, Douglas Raillard escreveu:

Quoted 74 lines · Arnaldo Carvalho de Melo
Quoted 45 lines

Em Fri, Oct 15, 2021 at 10:22:43AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Fri, Oct 15, 2021 at 10:18:41AM -0300, Arnaldo Carvalho de Melo escreveu:
CMake Error at cmake/modules/FindDWARF.cmake:103 (message):
Could NOT find some ELF and DWARF libraries, please install the missing packages
Call Stack (most recent call first):
CMakeLists.txt:64 (find_package)

Quoted 6 lines

-- Configuring incomplete, errors occurred!
See also "/var/home/acme/git/pahole/build/CMakeFiles/CMakeOutput.log". ⬢[acme@toolbox build]$

Which means I don't have those static library files, will try installing them.

I'm trying with the latest fedora that comes with static libraries, fedora:32, and there I noticed that we need to look for libzstd as well, I added it manually to:

target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} -lzstd)

[root@cc85f3a7b96f dwarves-1.22]# rpm -qa | grep -- -static zlib-static-1.2.11-21.fc32.x86_64 glibc-static-2.31-6.fc32.x86_64 elfutils-devel-static-0.183-1.fc32.x86_64 xz-static-5.2.5-1.fc32.x86_64 libxcrypt-static-4.4.20-2.fc32.x86_64 libzstd-static-1.4.9-1.fc32.x86_64 elfutils-libelf-devel-static-0.183-1.fc32.x86_64 bzip2-static-1.0.8-2.fc32.x86_64 [root@cc85f3a7b96f dwarves-1.22]# cat /etc/fedora-release Fedora release 32 (Thirty Two)
[root@cc85f3a7b96f dwarves-1.22]#

now I'm at:

/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/libdw.a(debuginfod-client.o): in function `__libdwfl_debuginfod_init':
(.text.startup[.text.startup.group]+0x1b): undefined reference to `dlopen'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x36): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x4f): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x68): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0x81): undefined reference to `dlsym'
/usr/bin/ld: (.text.startup[.text.startup.group]+0xe0): undefined reference to `dlclose'

Quoted 1 line

that needs some more magic that I already forgot :-\

When trying on Ubuntu it also appeared that find_library() for libebl was also needed, but not on Alpine Linux so I did not include it, as it does not seem necessary on Alpine. I'm not sure how we want to go about these:
1) Take the union of all the dependencies that seem to be necessary and deal gracefully in CMakeLists.txt with failure to find a lib. This will result in linker error if a lib is actually necessary and not found.
2) Select a blessed distro for static building and only maintain that
3) Find some cmake builtin "magic" so that the user can point at existing lib that seem to not be detected on their distro. I'm quite rusty in cmake so maybe there is a better way than sprinkling find_library() everywhere.

So far I opted for approach 2) with Alpine, as building on e.g. Ubuntu will not work in any case, because glibc does not support static linking (it kind of does until it does not anymore). Alpine uses the musl libc which supports static linking.

As for the dlopen/dlsym/dlclose issue I'm not sure. Maybe these symbols are just not present in static glibc ? The binary I produced on Alpine did not seem to segfault [1]. If you want to give it a go, this scripts set it up in ~20s:
https://github.com/alpinelinux/alpine-chroot-install

Packages: bash gcc git make cmake musl-dev zlib-static bzip2-static libelf-static libbpf-dev musl-obstack-dev argp-standalone linux-headers

Quoted 7 lines
Quoted 3 lines

I'll try with this later, thing is, I want to build on the CI for pahole that Andrii put in place in the libbpf repo, so we need to check how to build this static build.

I tried to make it work on Ubuntu and came up with that. It is quite stinky and does not result in a fully static build, but it's probably as close we can get to it using glibc, and it does not segfault:

I'll test it, the first time I tried the BZ2 (and others maybe) lib became a new requirement for building pahole even whe not requesting a static link, which is undesirable, I'll check this time around.

- Arnaldo

Quoted 90 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

ldd pahole

        linux-vdso.so.1 (0x00007ffd9b7e2000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f049a55e000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f049a53b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f049a349000)
        /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f049a584000)

Subject: [PATCH] CMakeLists.txt: Allow pseudo-static build on Ubuntu

Allow STATIC_LINK=ON on Ubuntu by:

* providing libebl
* avoiding a trailing -Wl,-Bstatic that makes the resulting binary segfault
* dynamically link to libdl. Static linking to that lib would impose that the same glibc version used for linking is present on the runtime system, which is not very safe and would mislead into thinking the binary is safe to use everywhere.

Apart from that, all the other libraries are statically linked to the executable.

Note: the user might need a symlink /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 The ld option --dynamic-linker=file is supposed to allow fixing that, but it's unfortunately ignored when used with -static.

---
 CMakeLists.txt                | 18 ++++++++++++++++--
 cmake/modules/FindDWARF.cmake |  8 ++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
CMakeLists.txt +16 -2
index ba467bf..adb6d74 100644
@@ -2,6 +2,11 @@ project(pahole C)
 cmake_minimum_required(VERSION 2.8.12)
 cmake_policy(SET CMP0005 NEW)
+execute_process(COMMAND lsb_release -si
+  OUTPUT_VARIABLE LINUX_DISTRO
+  OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
 option(LIBBPF_EMBEDDED "Use the embedded version of libbpf instead of searching it via pkg-config" ON)
 if (NOT LIBBPF_EMBEDDED)
 	find_package(PkgConfig REQUIRED)
@@ -127,7 +132,12 @@ endif()
 add_library(dwarves ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY} ${EBL_LIBRARY})
+# On non-musl systems, we need to link to libdl in order to get
+# dlopen/dlclose/dlsym, as required by libdwfl and libebl
+if (LINUX_DISTRO STREQUAL "Ubuntu")
+  target_link_libraries(dwarves dl)
+endif()
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
@@ -201,4 +211,8 @@ install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.bla
 # Avoid having a trailing -Wl,-Bdynamic that will make some linkers think we
 # need to link against a DSO for the libc.
 get_property(TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
-set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
+# If we use a trailing -Wl,-Bstatic on ubuntu, the binary will segfault for some
+# reasons ...
+if (NOT LINUX_DISTRO STREQUAL "Ubuntu")
+  set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
+endif()
cmake/modules/FindDWARF.cmake +8 -0
index 1b4ac49..cd1f265 100644
@@ -27,6 +27,14 @@ find_path(LIBDW_INCLUDE_DIR elfutils/libdw.h
 	~/usr/local/include
 )
+# This does not seem to be necessary on Alpine Linux
+if (LINUX_DISTRO STREQUAL "Ubuntu")
+  find_library(EBL_LIBRARY
+    NAMES ebl
+    PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+  )
+endif()
+
 find_library(DWARF_LIBRARY
 	NAMES dw dwarf
 	PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo