Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update</strong> I have grown to prefer the control <a href="https://stackoverflow.com/a/4910421/182675" title="John Ripley&#39;s assembly `.incbin` based solution">John Ripley's assembly <code>.incbin</code> based solution</a> offers and now use a variant on that.</p> <p>I have used objcopy (GNU binutils) to link the binary data from a file foo-data.bin into the data section of the executable:</p> <pre><code>objcopy -B i386 -I binary -O elf32-i386 foo-data.bin foo-data.o </code></pre> <p>This gives you a <code>foo-data.o</code> object file which you can link into your executable. The C interface looks something like</p> <pre><code>/** created from binary via objcopy */ extern uint8_t foo_data[] asm("_binary_foo_data_bin_start"); extern uint8_t foo_data_size[] asm("_binary_foo_data_bin_size"); extern uint8_t foo_data_end[] asm("_binary_foo_data_bin_end"); </code></pre> <p>so you can do stuff like</p> <pre><code>for (uint8_t *byte=foo_data; byte&lt;foo_data_end; ++byte) { transmit_single_byte(*byte); } </code></pre> <p>or</p> <pre><code>size_t foo_size = (size_t)((void *)foo_data_size); void *foo_copy = malloc(foo_size); assert(foo_copy); memcpy(foo_copy, foo_data, foo_size); </code></pre> <p>If your target architecture has special constraints as to where constant and variable data is stored, or you want to store that data in the <code>.text</code> segment to make it fit into the same memory type as your program code, you can play with the <code>objcopy</code> parameters some more.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload