initiallizing global pointers with the address of a string literal, in PI code.

Hello all,

I am working on a position independent program whose startup procedure is handled manually.

when I declare an global pointer expression like

char *pTest = "TEST";

its strangely being located in bss and it is not possible to initialize pTest pointer with the address of constant string.


If I force it to locate in a particular section, like char *pTest __attribute((section(.data.pTest)))__ = "TEST"

lval is located in .data section and it has an execution adress, but address of rval is missing. I see 4 bytes of zero in the load address of execution region instead address of "TEST" string.

I can see "TEST" string in binary file but its address is not exist in LOAD$$"section"$$RW$$Base area, thus startup procedure initialize pTest pointer with zero and this couse wrong access in the runtime.


This problem exist only in position independed code, for global pointer initialization with string literals,

if I use initialized global arrays such as, char testarray[4] = {1, 2, 3, 4}

or global pointer initialized with numeric literal such as char *pTest __attribute((section(.data.pTest)))__ = (char *)0x11223344;

or in normal programs which has a main()

everything is located in correct places and startup procedure works correct.


so how can initialize global pointers with the address of a string literal in PI code.

thanks for any help

Parents
  • I tried with "char *pTest = "TEST";" in my little PI test project, that has a main() function. And there it worked as expected. When reaching main(), pTest did point to the correct string.

    When looking closer at this, the Arm Compiler 6, that I use, creates a little init function for this, as seen in the compiler list file:

    @ %bb.0:
    	movw	r0, :lower16:pTest(sbrel)
    	movt	r0, :upper16:pTest(sbrel)
    	adr	r1, .LCPI2_0
    	str.w	r1, [r9, r0]
    	bx	lr
    

    This function is handled like a C++ constructor. So it gets called as part of __cpp_initialize_aeabi_ by the library startup code before main(). So, you can try to manually call __cpp_initialize_aeabi_ before your own code runs or simply add a main() and start your program there.

Reply
  • I tried with "char *pTest = "TEST";" in my little PI test project, that has a main() function. And there it worked as expected. When reaching main(), pTest did point to the correct string.

    When looking closer at this, the Arm Compiler 6, that I use, creates a little init function for this, as seen in the compiler list file:

    @ %bb.0:
    	movw	r0, :lower16:pTest(sbrel)
    	movt	r0, :upper16:pTest(sbrel)
    	adr	r1, .LCPI2_0
    	str.w	r1, [r9, r0]
    	bx	lr
    

    This function is handled like a C++ constructor. So it gets called as part of __cpp_initialize_aeabi_ by the library startup code before main(). So, you can try to manually call __cpp_initialize_aeabi_ before your own code runs or simply add a main() and start your program there.

Children
No data