From da908a7b7902f9aaace6d62f88feb78284828d14 Mon Sep 17 00:00:00 2001 From: otsmr Date: Mon, 30 Oct 2023 14:53:28 +0100 Subject: [PATCH 1/2] llc only supports one input file --- cmake/LLVMIRUtil.cmake | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/cmake/LLVMIRUtil.cmake b/cmake/LLVMIRUtil.cmake index 3afb6eb..57d5029 100644 --- a/cmake/LLVMIRUtil.cmake +++ b/cmake/LLVMIRUtil.cmake @@ -753,14 +753,26 @@ function(llvmir_attach_obj_target) list(APPEND IN_FULL_LLVMIR_FILES "${IN_LLVMIR_DIR}/${IN_LLVMIR_FILE}") endforeach() - set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${TRGT}.o") - if(SHORT_NAME) - set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${SHORT_NAME}.o") - endif() - get_filename_component(OUT_LLVMIR_FILE ${FULL_OUT_LLVMIR_FILE} NAME) + foreach(IN_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES}) + get_filename_component(OUTFILE ${IN_LLVMIR_FILE} NAME_WE) + set(OUT_LLVMIR_FILE "${OUTFILE}.o") + set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${OUT_LLVMIR_FILE}") - list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE}) - list(APPEND FULL_OUT_LLVMIR_FILES ${FULL_OUT_LLVMIR_FILE}) + add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE} + COMMAND llc + ARGS + -filetype=obj + -relocation-model=pic + -o=${FULL_OUT_LLVMIR_FILE} + ${LLVMIR_ATTACH_UNPARSED_ARGUMENTS} + ${IN_LLVMIR_FILE} + DEPENDS ${IN_LLVMIR_FILE} + COMMENT "Generating object ${OUT_LLVMIR_FILE}" + VERBATIM) + + list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE}) + list(APPEND FULL_OUT_LLVMIR_FILES ${FULL_OUT_LLVMIR_FILE}) + endforeach() # setup custom target add_custom_target(${TRGT} DEPENDS ${FULL_OUT_LLVMIR_FILES}) @@ -786,16 +798,6 @@ function(llvmir_attach_obj_target) set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On) set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME}) - add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE} - COMMAND llc - ARGS - -filetype=obj - ${LLVMIR_ATTACH_UNPARSED_ARGUMENTS} - -o ${FULL_OUT_LLVMIR_FILE} ${IN_FULL_LLVMIR_FILES} - DEPENDS ${IN_FULL_LLVMIR_FILES} - COMMENT "Generating object ${OUT_LLVMIR_FILE}" - VERBATIM) - ## postamble endfunction() From ec91368fd5f36d7e764aac542e40a16694392c89 Mon Sep 17 00:00:00 2001 From: otsmr Date: Mon, 30 Oct 2023 19:08:44 +0100 Subject: [PATCH 2/2] add example --- example04/CMakeLists.txt | 17 +++++++++++++++++ example04/README.txt | 9 +++++++++ example04/executable/CMakeLists.txt | 3 +++ example04/executable/main.c | 8 ++++++++ example04/staticlib/CMakeLists.txt | 18 ++++++++++++++++++ example04/staticlib/include/Test1.h | 4 ++++ example04/staticlib/include/Test2.h | 4 ++++ example04/staticlib/include/Test3.h | 3 +++ example04/staticlib/src/Test1.c | 7 +++++++ example04/staticlib/src/Test2.c | 7 +++++++ example04/staticlib/src/Test3.c | 6 ++++++ 11 files changed, 86 insertions(+) create mode 100644 example04/CMakeLists.txt create mode 100644 example04/README.txt create mode 100644 example04/executable/CMakeLists.txt create mode 100644 example04/executable/main.c create mode 100644 example04/staticlib/CMakeLists.txt create mode 100644 example04/staticlib/include/Test1.h create mode 100644 example04/staticlib/include/Test2.h create mode 100644 example04/staticlib/include/Test3.h create mode 100644 example04/staticlib/src/Test1.c create mode 100644 example04/staticlib/src/Test2.c create mode 100644 example04/staticlib/src/Test3.c diff --git a/example04/CMakeLists.txt b/example04/CMakeLists.txt new file mode 100644 index 0000000..0a42328 --- /dev/null +++ b/example04/CMakeLists.txt @@ -0,0 +1,17 @@ +# cmake file + +cmake_minimum_required(VERSION 3.9) + +find_program(CMAKE_C_COMPILER clang) +find_program(CMAKE_CXX_COMPILER clang++) + +project(staticlib) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake") + +message(STATUS ${CMAKE_MODULE_PATH}) +include(LLVMIRUtil) + +add_subdirectory(staticlib) +add_subdirectory(executable) + diff --git a/example04/README.txt b/example04/README.txt new file mode 100644 index 0000000..7277bba --- /dev/null +++ b/example04/README.txt @@ -0,0 +1,9 @@ +# Instrumenting an static library + +```sh +mkdir build && cd build +cmake .. +make testlib_instrumented +make main +./executable/main +``` \ No newline at end of file diff --git a/example04/executable/CMakeLists.txt b/example04/executable/CMakeLists.txt new file mode 100644 index 0000000..941a68c --- /dev/null +++ b/example04/executable/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(main main.c) +target_include_directories(main PUBLIC "./../staticlib/include") +target_link_libraries(main "${CMAKE_BINARY_DIR}/staticlib/libtestlib_instrumented.a") diff --git a/example04/executable/main.c b/example04/executable/main.c new file mode 100644 index 0000000..afb7d4b --- /dev/null +++ b/example04/executable/main.c @@ -0,0 +1,8 @@ +#include "Test1.h" + +int main(int argc, char **argv) +{ + puts("Calling Test1 from static lib!"); + Test1(); + return 0; +} diff --git a/example04/staticlib/CMakeLists.txt b/example04/staticlib/CMakeLists.txt new file mode 100644 index 0000000..f2f50f4 --- /dev/null +++ b/example04/staticlib/CMakeLists.txt @@ -0,0 +1,18 @@ +set(CMAKE_BUILD_TYPE Debug) +# set(LLVM_PASS_PLUGIN "") + +set(SOURCE_FILES + src/Test1.c + src/Test2.c + src/Test3.c +) + +add_library(testlib STATIC ${SOURCE_FILES}) +target_include_directories(testlib PUBLIC "./include") +set_target_properties(testlib PROPERTIES LINKER_LANGUAGE C) + +llvmir_attach_bc_target(testlib_bc testlib) +llvmir_attach_opt_pass_target(testlib_opt testlib_bc) # -load-pass-plugin=${LLVM_PASS_PLUGIN} -passes=ctrace) + +llvmir_attach_obj_target(testlib_obj testlib_opt) +llvmir_attach_library(testlib_instrumented testlib_obj STATIC) diff --git a/example04/staticlib/include/Test1.h b/example04/staticlib/include/Test1.h new file mode 100644 index 0000000..a8771fb --- /dev/null +++ b/example04/staticlib/include/Test1.h @@ -0,0 +1,4 @@ +#include +#include "Test2.h" + +void Test1(); diff --git a/example04/staticlib/include/Test2.h b/example04/staticlib/include/Test2.h new file mode 100644 index 0000000..2a99408 --- /dev/null +++ b/example04/staticlib/include/Test2.h @@ -0,0 +1,4 @@ +#include "Test3.h" +#include + +void Test2(); diff --git a/example04/staticlib/include/Test3.h b/example04/staticlib/include/Test3.h new file mode 100644 index 0000000..3a9c803 --- /dev/null +++ b/example04/staticlib/include/Test3.h @@ -0,0 +1,3 @@ +#include + +void Test3(); diff --git a/example04/staticlib/src/Test1.c b/example04/staticlib/src/Test1.c new file mode 100644 index 0000000..5501807 --- /dev/null +++ b/example04/staticlib/src/Test1.c @@ -0,0 +1,7 @@ +#include "Test1.h" + +void Test1() +{ + puts("Test1 called"); + Test2(); +} diff --git a/example04/staticlib/src/Test2.c b/example04/staticlib/src/Test2.c new file mode 100644 index 0000000..f39a6ad --- /dev/null +++ b/example04/staticlib/src/Test2.c @@ -0,0 +1,7 @@ +#include "Test2.h" + +void Test2() +{ + puts("Test2 called!"); + Test3(); +} \ No newline at end of file diff --git a/example04/staticlib/src/Test3.c b/example04/staticlib/src/Test3.c new file mode 100644 index 0000000..f50d6ce --- /dev/null +++ b/example04/staticlib/src/Test3.c @@ -0,0 +1,6 @@ +#include "Test3.h" + +void Test3() +{ + puts("Test3 called"); +} \ No newline at end of file