CMake: Embed ELF into executable

2020-05-29 05:31发布

问题:

I have a project that needs access to an ELF file embedded into the executable in a special section.

I was handcrafting Makefiles before and simply had a shell script where I used objcopy to copy the target I wanted to embed into an .o file, then link to this file in the executable.

# Create a new section and copy the binary there ($1=input $2=output name)
objcopy --input-target binary --output-target elf64-x86-64 \
        --binary-architecture i386 $1 $2.o

Now I want to get rid of the custom Makefiles and use CMake to generate them. However, I don't see an easy way to link to such a file. I am able to create and add this file, but not to link against it:

# Invoke script to package module as a library
add_custom_command(OUTPUT ${PACKAGED_FILE}
  COMMAND ./package.sh ${MODULE_FILE} ${PACKAGED_FILE}
  WORKING_DIRECTORY ${MODULE_DIR}
  DEPENDS ${MODULE_FILE}
  COMMENT packaging file into ELF object
  VERBATIM
)

add_custom_target(${PACKAGED_NAME} ALL DEPENDS ${PACKAGED_FILE})

I have tried to add it with:

target_link_libraries(binary ${PROJECT_BINARY_DIR}/${PACKAGED_FILE})

However, this fails because the file isn't there yet. It will be, but CMake doesn't know that. Adding the target name as a link library doesn't help either because it can't be found. Adding it as a also dependency doesn't help. Does anyone have an idea how this could be accomplished?

回答1:

We are doing a similar thing in our project - the following part of our CMakeLists.txt does the trick:

set(PROJECT_EMBED_OBJ_FILES "")
set(PROJECT_EMBED_FILES "file1.elf" "file2.elf")
foreach(FILENAME ${PROJECT_EMBED_FILES})
    get_filename_component(FILENAME_ONLY ${FILENAME} NAME)
    get_filename_component(FILEPATH_ONLY ${FILENAME} PATH)
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME_ONLY}.o 
                       WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/${FILEPATH_ONLY} 
                       COMMAND ${CMAKE_OBJCOPY} 
                       ARGS -I binary -O elf64-x86-64 -B i386 ${FILENAME_ONLY} ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME_ONLY}.o )
    list(APPEND PROJECT_EMBED_OBJ_FILES ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME_ONLY}.o)
    message(STATUS "Objcopy'ing ${FILENAME}")
endforeach(FILENAME)

And then in the call to add_executable:

add_executable(projectname ${PROJECT_SOURCES} ${PROJECT_EMBED_OBJ_FILES})


回答2:

You may try

add_custom_command(TARGET $(PROJECT_NAME).elf
    POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} ARGS -O binary ${PROJECT_NAME}.elf \ 
${PROJECT_NAME}.bin)

Put this after your add_executable(). The POST_BUILD means execute after build.