martinribelotta / elfloader

ARMv7M ELF loader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to read Text section,I can't understand the Elf32_Rel struct!

joeweiming opened this issue · comments

Hi, Thanks for your code, these days, I have a project to need load the elf file for my arm product, it is base the MTK sdk without MMU.
but when I read your code, somewhere I can't understand. Like this function:

static int relocate(ELFExec_t *e, Elf32_Shdr *h, ELFSection_t *s,
const char *name) {
if (s->data) {
Elf32_Rel rel;
size_t relEntries = h->sh_size / sizeof(rel);
size_t relCount;
(void) LOADER_SEEK_FROM_START(e->fd, h->sh_offset);
DBG(" Offset Info Type Name\n");
//重定位节中的数据
for (relCount = 0; relCount < relEntries; relCount++) {
if (LOADER_READ(e->fd, &rel, sizeof(rel)) == sizeof(rel)) {
Elf32_Sym sym;
Elf32_Addr symAddr;

    char name[33] = "<unnamed>";
    int symEntry = ELF32_R_SYM(rel.r_info);
    int relType = ELF32_R_TYPE(rel.r_info);
    Elf32_Addr relAddr = ((Elf32_Addr) s->data) + rel.r_offset;

    readSymbol(e, symEntry, &sym, name, sizeof(name));
    DBG(" %08X %08X %-16s %s\n", rel.r_offset, rel.r_info, typeStr(relType),
        name);

    symAddr = addressOf(e, &sym, name);
    if (symAddr != 0xffffffff) {
      DBG("  symAddr=%08X relAddr=%08X\n", symAddr, relAddr);
      if (relocateSymbol(relAddr, relType, symAddr) == -1)
        return -1;
    } else {
      DBG("  No symbol address of %s\n", name);
      return -1;
    }
  }
}
return 0;

} else
MSG("Section not loaded");
return -1;
}

it is like the text. data. bass section is all the same struct with the struct below.
typedef struct
{
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;
I read my elf file,
image
data like this picture, it is ER_RO section, so ,how I relocate this ? I can't understand this format!
thank you!

For load and execute elf into memory you simply use:

ElfEnv_t env = { ... };
elf_exec("name of executable", env);

In order to access executable you need define some macros into loader_config.h.

By example, this configuration use open/read/close calls from newlib:
https://github.com/martinribelotta/elfloader/blob/master/loader_config.h#L54

As I see, you hellaxf.bin is correct ELF file header.

What toolbox are you using? At this moment, the loader is only tested with gcc toolchans but any toolchain that generate R_ARM_ABS32 and R_ARM_THB_JMP/R_ARM_THB_CALL are supported teoricaly.

To help you need more info:

Wath is the source of ELF file? SD card? Network? external flash memory? Internal flash memory?
Wath is the compiler used to generate the ELF? gcc/Keil/IAR?

If you provide this data I can help on it.

Closed because it is not really a bug