This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Specifying symbol names under which binary values are added


> Thanks for the suggestion. I am trying for now to make it compatible
> data-wise with the output from before, only symbol names should be
> different. I made:
>
> .section images
> .global _binary_image_jpg_start
> .type _binary_image_jpg_start, @object
> .align 4
> _binary_image_jpg_start:
> .incbin "/long/path/to/image.jpg"
> .global _binary_image_jpg_end
> .type _binary_image_jpg_end, @object
> .align 4
> _binary_image_jpg_end:
> .global _binary_image_jpg_size
> .type _binary_image_jpg_size, @object
> .align 4
> _binary_image_jpg_size:
> .int _binary_image_jpg_end - _binary_image_jpg_start
>
> And I assemble it with "as image.as -o image.o". But something does
> not work. Resolving symbols in my other code and accessing data
> returns me garbage now. Or at least something decoder fails to parse.
> Is there anything more to do?

You need an "a" flag on the .section directive:

    .section images, "a"

Without it, the linker places the section in the output object file as
unloadable data (like debug info), and your running program has no
access to it.

To replicate the way the linker creates the _size symbol, you need to
declare the symbol as an absolute symbol, rather than a label on a
word in memory. Note the difference between this:

    .global _binary_image_jpg_size
    .type _binary_image_jpg_size, @object
    .align 4
    _binary_image_jpg_size:
    .int _binary_image_jpg_end - _binary_image_jpg_start

and this:

    .global _binary_image_jpg_size
    _binary_image_jpg_size = _binary_image_jpg_end - _binary_image_jpg_start

In the former case, the size has been added as an additional 4 bytes
at the end of your images section, and you would access it like this:

    extern int _binary_image_jpg_size;
    ...
    printf ("Size = %d\n", _binary_image_jpg_size);

In the latter case, it's an absolute symbol whose value itself is the
size (the images section contains only the jpg contents), and you
access it like this:

    extern char _binary_image_jpg_size;
    ...
    printf ("Size = %d\n", &_binary_image_jpg_size);

(Even though we're using the & operator, we're not really getting the
*address* of anything -- we're simply getting the value of the symbol.
Usually, the value of a symbol is an address, so C's & operator is
called "address-of", but this is an exception.)

-cary


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]