celf

Reading value from external ELF symbol in C


I have following ELF symbol (defined in assembly file):

Disassembly of section .memory_info:

10000bb8 <__memory_info>:
10000bb8:   10000bd4    ldrdne  r0, [r0], -r4
10000bbc:   20000000    andcs   r0, r0, r0
10000bc0:   00000000    andeq   r0, r0, r0
10000bc4:   20000000    andcs   r0, r0, r0
10000bc8:   00000000    andeq   r0, r0, r0
10000bcc:   20000000    andcs   r0, r0, r0
10000bd0:   000000a8    andeq   r0, r0, r8, lsr #1

I want to read this value in C as structure.

I tried to do it in the following way:

typedef struct {
    void *flash_end;
    void *ram_text_begin;
    void *ram_text_size;
    void *ram_data_begin;
    void *ram_data_size;
    void *ram_bss_begin;
    void *ram_bss_size;
} memory_info_t;

int main() {
    extern memory_info_t __memory_info;
    uint32_t *a = __memory_info.flash_end;
    
    asm("bkpt #0");
}

But according to gdb actual value of a on breakpoint is not what I expected:

(gdb) print a
$1 = (uint32_t *) 0xffffffff

Solution

  • After some research I found a solution. I use openocd to load ELF images to flash memory. I turns out that openocd obviously does not load sections without segment. So I added SHF_ALLOC flag to .memory_info section to automatically assign it to segment.

    Before:

    .section .memory_info
    

    After:

    .section .memory_info, "a"
    

    And it's working.