objcopy / binary inclusion weirdness (repost)

Nick Clifton nickc@cambridge.redhat.com
Wed Feb 27 01:13:00 GMT 2002


Hi Matthew,

> objcopy -I binary -O elf32-arm sprite sprite.o
> gcc -o test.elf test.c
> objcopy -O binary test.elf test.gba

I assume that you are including sprite.o on that gcc command line as
well ?

> Right, now, my problem is how to reference the data in sprite.o from
> test.c.  I notice that trying to dereference:
> 
>   extern u8* _binary_sprite_start;
> 
> causes a segfault, but
> 
>   extern u8  _binary_sprite_start[];
> 
> allows me to reference the data as I'd expect.  What's the
> difference from the linker's point of view?

Nothing.  From the *compiler's* point of view however the first
version refers to a 4-byte memory location that contains an address
of the start of a block of memory, whereas the second version refers
to the address of the start of that block of memory.

Think of symbols as having two attributes, a "location" or an address
in memory and a "value" or the contents of that address.  When objcopy
creates '_binary_sprite_start' it sets the location to be start
address of the block of memory containing the sprite.  The value of
this symbol is the first word in the sprite's block of memory.

        |. . . . |
        |--------|
        |pointer |    <--- extern u8* _binary_sprite_start
        |------- |
        |. . . . |
        |. . . . |
        |. . . . |
        |--------|
        | block  |   <--- extern u8 _binary_sprite_start[]
        |   of   |
        | memory |
        |--------|
        |. . . . |

So, if you access _binary_sprite_start as a "u8 *" it is treated as a
pointer.  The "value" of a pointer is an address of some other bit of
memory, so it would be taking the first word of the sprite's block of
memory and treating it as an address.  Whereas if you treat
_binary_sprite_start as an "u8 []" then the compiler knows that it is
the address of the block of memory, not the address of a pointer to a
block of memory.


> Question 2 is how to get to the size attribute:

>   printf("%d", _binary_sprite_size);
> fails
>   printf("%d", (u32) &_binary_sprite_size);
> Which works, but looks like shit!

The same thing has happened here.  Objcopy has created a symbol whose
address is the size of the binary sprite block of memory and whose
value is whatever happens to reside at that address.  I know that this
seems weird but it keeps the code for handling binary files simpler.

> Can somebody tell me whether I should be passing different arguments
> to objcopy to get it to generate binaries that can be referenced
> sensibly from C,

Sorry - objcopy does not have any way to change the construction of
these symbols.

> or otherwise tell me what the correct way of getting at these two
> symbols is?

Well for the size you could try this:

  extern u8 _binary_sprite_size[];

  printf ("%d", _binary_sprite_size);

Cheers
        Nick




More information about the Binutils mailing list