1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static Elf32_Sym *symbols[1000];
static int n_sym = 0;
/* Updates symbol value *symval associated with relocation entry *rela
 * returns 0 on success, -1 on error
 */
static int get_symbol_value_rela(int fd, Elf32_Rela *rela, Elf32_Addr *symval)
{
    static int i;

    Elf32_Sym *sym = (symtab + ELF32_R_SYM(elf2h32(rela->r_info)));

    for (i=0; i<n_sym; i++)
    {
        if (symbols[i] == sym)
            break;
    }

    if (i == n_sym)
    {
        symbols[n_sym++] = sym;
    }

    DEBUGF("symbol[%d]: st_name=0x%0x st_value=0x%0x "
           "st_size=0x%0x st_info=0x%0x st_shndx=0x%0x\n",
           ELF32_R_SYM(elf2h32(rela->r_info)), elf2h32(sym->st_name), elf2h32(sym->st_value),
           elf2h32(sym->st_size), sym->st_info, elf2h16(sym->st_shndx));

    if (elf2h16(sym->st_shndx) != STN_UNDEF)
    {
        *symval = (Elf32_Addr)sect_addr[elf2h16(sym->st_shndx)] + elf2h32(sym->st_value);
    }
    else
    {
        return -1;
    }

    return 0;
}