feat: append section .interp for shared object, output version inforation like libQt5Core.so

This commit is contained in:
anjingyu 2024-10-20 22:46:05 +08:00
parent dff7029140
commit 58f49c36bc
3 changed files with 46 additions and 11 deletions

View File

@ -927,6 +927,35 @@ function (merge_lib)
endif ()
endfunction ()
function (set_interp TRT FUN)
get_target_property(target_type ${TRT} TYPE)
if (NOT (target_type STREQUAL SHARED_LIBRARY))
message_color ("Red" "-- ${TRT} is not a shared library")
return ()
endif ()
# Find ELF interpreter and define a macro for that:
if (LINUX AND NOT CMAKE_CROSSCOMPILING)
if (NOT DEFINED ELF_INTERPRETER)
execute_process (COMMAND ${CMAKE_COMMAND} -E env LC_ALL=C readelf -l /bin/sh
RESULT_VARIABLE readelf_ok
OUTPUT_VARIABLE readelf_output)
if ("${readelf_ok}" STREQUAL "0"
AND "${readelf_output}" MATCHES "program interpreter: (.*)]")
set (ELF_INTERPRETER "${CMAKE_MATCH_1}" CACHE INTERNAL "ELF interpreter location")
else ()
set (ELF_INTERPRETER "" CACHE INTERNAL "ELF interpreter location")
endif ()
endif ()
if (ELF_INTERPRETER)
message_color ("Yellow" "-- Append interpreter for ${TRT}: ${ELF_INTERPRETER}")
target_link_options (${TRT} PRIVATE "-Wl,-e,${FUN}")
target_compile_definitions (${TRT} PRIVATE ELF_INTERPRETER="${ELF_INTERPRETER}")
endif ()
endif()
endfunction ()
# Generate installation package for the standard AD project
#
# Required arguments:

View File

@ -27,7 +27,9 @@ if (NOT ADMAKE_BUILD_TEST)
add_library (${TARGET_NAME} SHARED ${SRC})
if (UNIX)
target_link_libraries (${TARGET_NAME} PUBLIC pthread dl dwarfrust)
target_link_options (${TARGET_NAME} PRIVATE "-Wl,--entry,VersionInfo")
# Append link options: -Wl,--entry,VersionInfo
# target_link_options (${TARGET_NAME} PRIVATE "-Wl,-e,VersionInfo")
set_interp (${TARGET_NAME} "VersionInfo")
else () # Windows, needed by rust
target_link_libraries (${TARGET_NAME} PUBLIC dwarfrust ws2_32 userenv ntdll)
endif ()

View File

@ -1,5 +1,6 @@
#include "dwarf_global_var.h"
#include <cstdlib>
#include <exception> /**< std::exception */
#include <map> /**< std::map */
@ -7,27 +8,29 @@
#if defined(OS_LINUX) || defined(__linux__)
#ifndef PT_INTERP
#ifndef ELF_INTERPRETER
#if defined(__x86_64__)
#define PT_INTERP "/lib64/ld-linux-x86-64.so.2"
#define ELF_INTERPRETER "/lib64/ld-linux-x86-64.so.2"
#elif defined(__aarch64__)
#define PT_INTERP "/lib/ld-linux-aarch64.so.1"
#define ELF_INTERPRETER "/lib/ld-linux-aarch64.so.1"
#endif /* ARCH */
#endif /* PT_INTERP */
#ifndef PT_INTERP
#endif /* ELF_INTERPRETER */
#ifdef ELF_INTERPRETER
#include <stdio.h>
#include <stdlib.h>
const char interp[] __attribute__((section(".interp"))) = PT_INTERP;
extern "C" VersionInfo()
extern const char interp[] __attribute__((section(".interp"))) = ELF_INTERPRETER;
extern "C" void VersionInfo() __attribute__((force_align_arg_pointer));
void VersionInfo()
{
// Output the version information
puts("Built at " __DATE__ " " __TIME__ ", version: 0.1.0");
exit(EXIT_SUCCESS);
}
#endif /* !PT_INTERP */
#endif /* !ELF_INTERPRETER */
#endif /* OS_LINUX || __linux__ */
namespace ad {
@ -142,7 +145,8 @@ static std::ostream& PrintVarInfo(std::ostream& out, const VarInfo& p, int inden
<< indentation << " type_name: " << p.type_name << ",\n"
<< indentation << " size: " << p.size << ",\n"
<< indentation << " offset: " << p.offset << " (0x" << std::hex << p.offset << std::dec
<< ")" << ",\n"
<< ")"
<< ",\n"
<< indentation << " count: [ ";
output_vector(out, p.count);
out << " ],\n";