3

There is a classic way to embed resource files as a C language array into a binary file, so that we can store some external resource files such as .jpeg or .txt files into a binary.

For example, in the header file we can define an array:

const unsigned char xd_data[] = {
    77,90,144,0,3,0,0,0,4,0,0,0,255,255,0,0,184,0,0,0,0,0,0,0,64,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,0,0,
    0,14,31,186,14,0,180,9,205,33,184,1,76,205,33,84,104,105,115,32,112,114,
    111,103,114,97,109,32,99,97,110,110,111,116,32,98,101,32,114,117,110,
    32,105,110,32,68,79,83,32,109,111,100,101,46,13,13,10,36,0,0,0,0,0,0,
    0,66,163,223,218,6,194,177,137,6,194,177,137,6,194,177,137,105,221,187,
    137,13,194,177,137,133,222,191,137,3,194,177,137,105,221,181,137,4,194,
    177,137,136,202,238,137,4,194,177,137,6,194,176,137,73,194,177,137,133,
    202,236,137,13,194,177,137,48,228,187,137,11,194,177,137,193,196,183,
    137,7,194,177,137,82,105,99,104,6,194,177,137,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,80,69,0,0,76,1,4,0,65,162,32,86,0,0,0,0,0,0,0,
    0,224,0,47,1,11,1,6,0,0,100,0,0,0,74,0,0,0,0,0,0,228,113,0,0,0,16,0,0,
    0,128,0,0,0,0,64,0,0,16,0,0,0,2,0,0,4,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,
    224,0,0,0,4,0,0,0,0,0,0,2,0,0,0,0,0,16,0,0,16,0,0,0,0,16,0,0,16,0,0,0,
    0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,124,140,0,0,140,0,0,0,0,208,0,0,0,16,0
};

which contains the contents of the resource file, and it will be compile into the final binary.

There are lots of tools and tutorials on the web about this old trick, such as: http://www.rowleydownload.co.uk/arm/documentation/index.htm?http://www.rowleydownload.co.uk/arm/documentation/embed.htm, https://www.fourmilab.ch/xd/ and http://gareus.org/wiki/embedding_resources_in_executables#c_include_method.

However, looks like most of these pages are talking about how to embed the data into binary file using C style array.

My question is, what is the correct way to find the start address of the resource files in the compiled binary in order to extract them? I.e., how can I find the start address of xd_data in the compiled binary?

7
  • 5
    For the include method, just access that variable, xd_data, in your example. For the binary link option, the second reference has a paragraph starting with: "This data-section can be referenced from the C code simply by using: ...". Is that somehow not working or not suitable for you for some reason? Commented Oct 28, 2015 at 22:57
  • Are you asking about how a program inspecting the compiled binary would extract this data? It looks like the other commenters here are talking about how the code that was compiled to produce the binary would access the data. Commented Oct 28, 2015 at 23:02
  • @user2357112 Right... I am also a little bit confused. Maybe my expression is not clear in the original question. My question is: how can I know where the const variable xd_data is stored in the compiled binary? Commented Oct 28, 2015 at 23:04
  • 2
    You can use tools from Binutils. Such as readelf to list the sections, symbols etc - to get the symbol addresses. And objdump or objcopy to extract binary dumps of sections you are interested in. It may be good to clarify your question to say whether you want to do this programmatically (e.g. with C) or whether using existing command line tools is acceptable Commented Oct 28, 2015 at 23:16
  • @kaylum Thanks. Just realized as you've mentioned, the best way is to let the compiled binary to generate these files when it starts execution than using any external tools. Commented Oct 28, 2015 at 23:21

2 Answers 2

1

If you mean finding the byte address in the file where a data block starts just like objdump does but programmatically, then you can use the Binary File Descriptor library (BFD), see here and here.

Sign up to request clarification or add additional context in comments.

Comments

0

if you stored data for example an image and you want to load it (for printing or what ever you want) then if you have a function (library) that load it from memory, as example void loadResImage(void * mem); just do loadResImage(xd_data), if not but you have a function that load it from the file, in that case save it to a temp file eg:

int fd=open("tmpfile");
int ret=write(fd,xd_data, sizeof(xd_data));
close(fd);
loadImageFile("tmpfile");

but if you want to access the data outside the program itself (hex editor for example, or an other program), in that case you have to add a starting mark and optionally an ending mark or sizeof data. eg:

const unsigned char xd_data[]={
  ...
'M','A','G','I','C'};

in example above the end of the data is known, you just do a search to find it. same way, play around and find a suitable way to store the size of the data. but beware of the compiler optimizations.

2 Comments

I think OP wants to access resources colocated in the application binary, like is common on Windows
@sehe, i don't think so, because implementing resources as do windows, needs an external program that knows how and where (typically a section reserved for that) to save resouces at compile time as do rc of windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.