objcopy prepends directory pathname to symbol name

前端 未结 5 841
刺人心
刺人心 2020-12-31 10:30

I am tying to use objcopy to include a binary form of a text file into an executable. (At runtime I need the file as a string). This works fine until the linker

5条回答
  •  旧时难觅i
    2020-12-31 11:12

    I had to do this with cmake, and I ended up using /dev/stdin as input to get consistent symbols name, then redefining the symbols thanks to string(MAKE_C_IDENTIFIER ...) And then use objcopy --redefine-sym on the resulting object file.

    The resulting function is then :

    function(make_binary_object __file)
        get_filename_component(__file_name ${__file} NAME)
        set(__object ${CMAKE_CURRENT_BINARY_DIR}/${__file_name}.obj)
        string(MAKE_C_IDENTIFIER ${__file_name} __file_c_identifier)
        add_custom_command(OUTPUT ${__object}
            COMMAND ${CMAKE_OBJCOPY}
                --input-format binary
                --output-format elf64-x86-64
                --binary-architecture i386:x86-64
                /dev/stdin
                ${__object} < ${__file}
            COMMAND ${CMAKE_OBJCOPY}
                --redefine-sym _binary__dev_stdin_start=_binary_${__file_c_identifier}_start
                --redefine-sym _binary__dev_stdin_end=_binary_${__file_c_identifier}_end
                --redefine-sym _binary__dev_stdin_size=_binary_${__file_c_identifier}_size
                ${__object}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            DEPENDS ${__file})
        set_source_files_properties(${__object} PROPERTIES EXTERNAL_OBJECT TRUE)
    endfunction()
    

    And you can use it like this:

    make_binary_object(index.html)
    
    add_executable(my_server
        server.c
        ${CMAKE_CURRENT_BINARY_DIR}/index.html.obj)
    

提交回复
热议问题