0

As per my understanding below command is loading value of "__image_copy_start" to r1 register.

    ldr     r1, =__image_copy_start

I am trying to understand how value is assigned to "__image_copy_start". I couldn't find any assignment statement which is assigning value to "__image_copy_start", but I could see following declarations in "arch/arc/lib/sections.c" file.

char __image_copy_start[0] __attribute__((section(".__image_copy_start")));
char __image_copy_end[0] __attribute__((section(".__image_copy_end")));

It seems __attribute__((section(""))) is used to place the variables in special sections(In the above statement "__image_copy_start[0]" will be placed at ".__image_copy_start" section). Can you help to understand the following.

  1. How value is assigned to "__image_copy_start[0]" ?
  2. Why "__image_copy_start" declared as array "char __image_copy_start[0]" why not "char __image_copy_start" ?
user3693586
  • 1,227
  • 5
  • 18
  • 40

1 Answers1

0

__image_copy_start is assigned later by the linker.

The value come from the linker script. The compiler just left the variable empty, just keeping the reference in the symbol table for the linker to make the job. When linking, the linker place there an address corresponding to the start ot the "__image_copy_start" section. A "section" in the context of the linker just means some address space. It can be fix address range or placed sequentially.

The trick in your C code is just telling the compiler & the linker to create a pointer that will be initialized pointing to the beginning of this section.

Antoine
  • 1,070
  • 7
  • 11
  • Thanks for the reply. Why it is declared as array? In your reply object table means symbol table? – user3693586 Feb 09 '16 at 09:03
  • Its a way of having on char reserved in the section. Yes I mean symbol table. – Antoine Feb 09 '16 at 09:51
  • I could see following two entries for "__image_copy_start" in symbol table. Can you help to understand this. #arm-linux-gnueabihf-objdump -t arch/arm/lib/sections.o; Entry 1: 00000000 l d .__image_copy_start 00000000 .__image_copy_start Entry 2: 00000000 g O .__image_copy_start 00000000 __image_copy_start – user3693586 Feb 09 '16 at 11:19
  • This is hard to read (maybe a good idea to place this in a pastebin). I presume one is the debug symbol, the other is the real variable. – Antoine Feb 09 '16 at 12:46
  • I have pasted the content in pastbin @ http://pastebin.com/f601rD35. The output is having 6 columns, I searched for symboltable but couldn't find info related to 6 columns. – user3693586 Feb 10 '16 at 06:49
  • 1
    Thanks for the paste. Yes, if you look at the flags, you will see that one of the symbol bears the "d" flags, which means its a debug symbol that will only be used by the debugger but not loaded at run-time (it helps putting a name on the address). The other has the "g" and "O" flags means that is a global object. see http://linux.die.net/man/1/objdump. – Antoine Feb 10 '16 at 08:27
  • Thanks for the reply. I have a doubht, as per the declaration `char __image_copy_start[0] __attribute__((section(".__image_copy_start")));` address will be stored in __image_copy_start[0], which is a char variable. char size is one byte, but the address can be more than one byte. – user3693586 Feb 10 '16 at 18:27
  • No. In C array are implemented as a pointer. So this line will declare a pointer on char called `__image_copy_start` pointing to the beginning of the section with the same name. But the pointer will have as usual the same size of the address bus (32bits = 4bytes or 64bits). The array notation is just a "trick" here. It says "points to some array of char in the __image_copy_start section". – Antoine Feb 11 '16 at 07:57
  • In C , array name will contain the starting address of that array and we can't assign different address to that variable, so we can understood that array name is a constant pointer. Now we can understood like " `__image_copy_start[0]` will be placed at starting of `.__image_copy_start` section, so that array name `__image_copy_start` will contain the starting address of the section `.__image_copy_start` ". – user3693586 Feb 11 '16 at 09:24
  • As per the symbol table entry in the final object file(linker generated) "u-boot", the `__image_copy_start` symbol size is zero `80800000 g O .text 00000000 __image_copy_start` (number after the scation name). I tried by initializing the variable `char __image_copy_start[0] __attribute__((section(".__image_copy_start"))) = {`c`};` but still size of the symbol is zero in the symbol table. Can you please help me to understand why size of symbol is zero. – user3693586 Feb 12 '16 at 12:33