Search code examples
armgnu-assembler

ARM-GAS: how to load address of static array defined in some c-file (PIC and regular code)


I have some simple static array defined in c-file (const int data_input[1024];)and I need to access it from my assembly code. What's the right way to do it?

So far, I've been doing it this way:


.global data_input

data_input_ptr:
    .word data_input

my_function:
    adr r1, data_input_ptr
    bx lr

AFAIK, adr is pseudo-op stands to ldr r1, =data_input_ptr or something like that.

To me the way I do it seems not to be very correct: first of all that adr r1, data_input might potentially use pc relative addressing directly if it detected at link time that it's possible. Another issue is about PIC: what if the code has to be position independent. How then does it work if value of data_input_ptr has to be initialized by the loader (am I correct about that?)


Solution

  • The way you are doing it should work, but another way of handling it would be to use the address of the array as a second argument to the assembly function. Something like this:

    Call from c-file:
    my_function(original_argument, data_input);
    
    my_function.h:
    void my_function(void *original_argument, int *array_address);
    
    my_function.S:
    my_function:
        /* r1 already contains data_input_ptr since second argument ends up in r1 */
        bx lr