objcopy prepends directory pathname to symbol name

前端 未结 5 847
刺人心
刺人心 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条回答
  •  Happy的楠姐
    2020-12-31 11:01

    Generic method of including raw data into ELF is supported by .incbin assembler directive.

    The trick is to create template .S file that could look like this:

            .global foo_start
    foo_start:
            .incbin "foo.raw"
    
            .global foo_end
    foo_end:    
    

    This file is preprocessed via cpp so we don't have to hardcode file name there, eg. we can write:

            .incbin __raw_file_path__
    

    ... and then pass it while compiling:

    gcc -D__raw_file_path__='"data/foo.png"' foo.S -c -o data/foo.o
    

    Lastly, as we prepare .S file ourself we can add some extra data and/or information. If you include raw "text files" and want these to be available as C strings you can add '0' byte just after raw data:

            .global foo_start
    foo_start:
            .incbin "foo.raw"
    
            .global foo_end
    foo_end:    
            .byte 0
    
            .global foo_size
    foo_size:
            .int foo_end - foo_start
    

    If you want full-blown flexibility, you can of course pre-process file manually to alter any part of it, eg.

    .global @sym@_start
    @sym@_start:
           .incbin "@file@"
           .global @sym@_end
    @sym@_end:
    

    ... and then compile it:

    sed -e "s,@sym@,passwd,g" -e "s,@file@,/etc/passwd," 

提交回复
热议问题