Is it possible to associate a simple file with an executable file?

Some frameworks (Qt, Windows, Gtk ...) offer functionality for adding resources to your binaries. I wonder if this can be achieved without a framework, since all that is really necessary is

  • character containing the address of the resource in binary (data segment)
  • character to represent resource length
  • resource itself

How can this be achieved with the gcc toolchain?

+46
c ++ c gcc linker embedded-resource
Dec 13
source share
2 answers

You can do it:

objcopy --input binary \ --output elf32-i386 \ --binary-architecture i386 my_file.xml myfile.o 

This creates an object file that you can link to your executable file. This file will contain these characters that you will need to declare in your C code to be able to use them

 00000550 D _binary_my_file_xml_end 00000550 A _binary_my_file_xml_size 00000000 D _binary_my_file_xml_start 
+48
Dec 13
source share

At its most basic, the equivalent is a char array full of bytes.

On Linux, you can use xxd -i <file> to “compile” files into char arrays, and then bind the arrays to your binary and use, however, composite bytes.

Here is an example from my own makefile code that creates a “resource file” called templates.h containing a bunch of char arrays representing HTML templates:

 templates.h: @echo "#ifndef REDACTED_TEMPLATES_H" > templates.h @echo "#define REDACTED_TEMPLATES_H" >> templates.h @echo "// Auto-generated file! Do not modify!" >> templates.h @echo "// NB: arrays are not null-terminated" >> templates.h @echo "// (anonymous namespace used to force internal linkage)" >> templates.h @echo "namespace {" >> templates.h @echo "namespace templates {" >> templates.h @cd templates;\ for i in * ;\ do \ echo "Compiling $$i...";\ xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h;\ done;\ cd .. @echo "}" >> templates.h @echo "}" >> templates.h @echo "#endif" >> templates.h 

( see also: How best can I programmatically apply `__attribute__ ((not used))` to these automatically generated objects? )

The result looks something like this:

 #ifndef REDACTED_TEMPLATES_H #define REDACTED_TEMPLATES_H // Auto-generated file! Do not modify! // NB: arrays are not null-terminated // (anonymous namespace used to force internal linkage) namespace { namespace templates { unsigned char alert_email_finished_events_html[] __attribute__((unused)) = { 0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x6e, 0x6f, 0x64, 0x65, 0x2d, 0x69, 0x6e, 0x66, 0x6f, 0x2d, [..] 0x7d, 0x7d, 0x0d, 0x0a, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e, 0x0d, 0x0a }; unsigned int alert_email_finished_events_html_len __attribute__((unused)) = 290; unsigned char alert_email_finished_events_list_html[] __attribute__((unused)) = { 0x3c, 0x74, 0x72, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x73, 0x65, 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x2d, 0x70, 0x72, 0x65, 0x76, [..] 0x73, 0x74, 0x7d, 0x7d, 0x0d, 0x0a }; unsigned int alert_email_finished_events_list_html_len __attribute__((unused)) = 42; } } #endif 

Please note that this specific example is optimal when using a resource in only one translation module, but the general approach can be tailored to your needs.

+30
Dec 13
source share



All Articles