Skip to content

Commit

Permalink
modlib:bug fix,allow 64bit elf load and if sym is NULL not load symbol
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <[email protected]>
  • Loading branch information
anjiahao1 committed Oct 13, 2024
1 parent 223088d commit 03edfd4
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions libs/libc/modlib/modlib_bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ static int modlib_relocate(FAR struct module_s *modp,

/* Calculate the relocation address. */

if (rel->r_offset + sizeof(uint32_t) > dstsec->sh_size)
if (rel->r_offset < 0 ||
rel->r_offset > dstsec->sh_size)
{
berr("ERROR: Section %d reloc %d: "
"Relocation address out of range, "
Expand Down Expand Up @@ -528,7 +529,8 @@ static int modlib_relocateadd(FAR struct module_s *modp,

/* Calculate the relocation address. */

if (rela->r_offset + sizeof(uint32_t) > dstsec->sh_size)
if (rela->r_offset < 0 ||
rela->r_offset > dstsec->sh_size)
{
berr("ERROR: Section %d reloc %d: "
"Relocation address out of range, "
Expand Down Expand Up @@ -991,33 +993,36 @@ int modlib_bind(FAR struct module_s *modp,

symhdr = &loadinfo->shdr[loadinfo->symtabidx];
sym = lib_malloc(symhdr->sh_size);

ret = modlib_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size,
symhdr->sh_offset);

if (ret < 0)
if (sym != NULL)
{
berr("Failed to read symbol table\n");
lib_free(sym);
return ret;
}
ret = modlib_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size,
symhdr->sh_offset);

for (i = 0; i < symhdr->sh_size / sizeof(Elf_Sym); i++)
{
FAR Elf_Shdr *s = &loadinfo->shdr[sym[i].st_shndx];
if (ret < 0)
{
berr("Failed to read symbol table\n");
lib_free(sym);
return ret;
}

if (sym[i].st_shndx != SHN_UNDEF)
for (i = 0; i < symhdr->sh_size / sizeof(Elf_Sym); i++)
{
sym[i].st_value = sym[i].st_value + s->sh_addr;
if (sym[i].st_shndx != SHN_UNDEF &&
sym[i].st_shndx < loadinfo->ehdr.e_shnum)
{
FAR Elf_Shdr *s = &loadinfo->shdr[sym[i].st_shndx];

sym[i].st_value = sym[i].st_value + s->sh_addr;
}
}
}

ret = modlib_insertsymtab(modp, loadinfo, symhdr, sym);
lib_free(sym);
if (ret != 0)
{
binfo("Failed to export symbols program binary: %d\n", ret);
return ret;
ret = modlib_insertsymtab(modp, loadinfo, symhdr, sym);
lib_free(sym);
if (ret != 0)
{
binfo("Failed to export symbols program binary: %d\n", ret);
return ret;
}
}

/* Ensure that the I and D caches are coherent before starting the newly
Expand Down

0 comments on commit 03edfd4

Please sign in to comment.