ELF64 support (#220)

* include: Introduce elf64.h and elf.h

    Added elf64.h for 64bit ELF support and moved common definitions
    from elf32.h to elf.h. Also introduced Elf_xxx to be used in
    common libraries such as binfmt.

  * binfmt, include, modlib, module: Add support for ELF64

    Elf_xxx must be used instead of Elf32_xxx to support ELF64.
    To use ELF64, CONFIG_ELF_64BIT must be enabled.

  * binfmt, modlib: Add support for relocate address

  * arch: risc-v: Add include/elf.h

  * libs: machine: Add risc-v related files.

    NOTE: Currently only supports ELF64

  * boards: maix-bit: Add elf and posix_spawn configurations

  * boards: maix-bit: Add support for module configuration
This commit is contained in:
Masayuki Ishikawa
2020-02-07 17:10:23 -06:00
committed by Gregory Nutt
parent e21c30cf9d
commit 81f1133174
40 changed files with 1847 additions and 300 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo)
{
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
{
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
binfo("Sections %d:\n", i);
binfo(" sh_name: %08x\n", shdr->sh_name);
binfo(" sh_type: %08x\n", shdr->sh_type);
+6
View File
@@ -3,6 +3,12 @@
# see the file kconfig-language.txt in the NuttX tools repository.
#
config ELF_64BIT
bool "64bit ELF support"
default n
---help---
This option is used to load 64bit ELF files
config ELF_ALIGN_LOG2
int "Log2 Section Alignment"
default 2
+3 -3
View File
@@ -64,7 +64,7 @@
*
****************************************************************************/
int elf_verifyheader(FAR const Elf32_Ehdr *header);
int elf_verifyheader(FAR const Elf_Ehdr *header);
/****************************************************************************
* Name: elf_read
@@ -150,7 +150,7 @@ int elf_findsymtab(FAR struct elf_loadinfo_s *loadinfo);
****************************************************************************/
int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
FAR Elf32_Sym *sym);
FAR Elf_Sym *sym);
/****************************************************************************
* Name: elf_symvalue
@@ -177,7 +177,7 @@ int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
*
****************************************************************************/
int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym,
int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf_Sym *sym,
FAR const struct symtab_s *exports, int nexports);
/****************************************************************************
+247 -28
View File
@@ -74,14 +74,14 @@
* Private Types
****************************************************************************/
struct elf32_symcache_s
struct elf_symcache_s
{
dq_entry_t entry;
Elf32_Sym sym;
Elf_Sym sym;
int idx;
};
typedef struct elf32_symcache_s elf32_symcache_t;
typedef struct elf_symcache_s elf_symcache_t;
/****************************************************************************
* Private Data
@@ -95,13 +95,13 @@ typedef struct elf32_symcache_s elf32_symcache_t;
* Name: elf_readrels
*
* Description:
* Read the (ELF32_Rel structure * buffer count) into memory.
* Read the (ELF_Rel structure * buffer count) into memory.
*
****************************************************************************/
static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
FAR const Elf32_Shdr *relsec,
int index, FAR Elf32_Rel *rels,
FAR const Elf_Shdr *relsec,
int index, FAR Elf_Rel *rels,
int count)
{
off_t offset;
@@ -109,7 +109,7 @@ static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
/* Verify that the symbol table index lies within symbol table */
if (index < 0 || index > (relsec->sh_size / sizeof(Elf32_Rel)))
if (index < 0 || index > (relsec->sh_size / sizeof(Elf_Rel)))
{
berr("Bad relocation symbol index: %d\n", index);
return -EINVAL;
@@ -117,8 +117,9 @@ static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
/* Get the file offset to the symbol table entry */
offset = sizeof(Elf32_Rel) * index;
size = sizeof(Elf32_Rel) * count;
offset = sizeof(Elf_Rel) * index;
size = sizeof(Elf_Rel) * count;
if (offset + size > relsec->sh_size)
{
size = relsec->sh_size - offset;
@@ -130,6 +131,46 @@ static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
relsec->sh_offset + offset);
}
/****************************************************************************
* Name: elf_readrelas
*
* Description:
* Read the (ELF_Rela structure * buffer count) into memory.
*
****************************************************************************/
static inline int elf_readrelas(FAR struct elf_loadinfo_s *loadinfo,
FAR const Elf_Shdr *relsec,
int index, FAR Elf_Rela *relas,
int count)
{
off_t offset;
int size;
/* Verify that the symbol table index lies within symbol table */
if (index < 0 || index > (relsec->sh_size / sizeof(Elf_Rela)))
{
berr("Bad relocation symbol index: %d\n", index);
return -EINVAL;
}
/* Get the file offset to the symbol table entry */
offset = sizeof(Elf_Rela) * index;
size = sizeof(Elf_Rela) * count;
if (offset + size > relsec->sh_size)
{
size = relsec->sh_size - offset;
}
/* And, finally, read the symbol table entry into memory */
return elf_read(loadinfo, (FAR uint8_t *)relas, size,
relsec->sh_offset + offset);
}
/****************************************************************************
* Name: elf_relocate and elf_relocateadd
*
@@ -144,14 +185,13 @@ static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
FAR const struct symtab_s *exports, int nexports)
{
FAR Elf32_Shdr *relsec = &loadinfo->shdr[relidx];
FAR Elf32_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
FAR Elf32_Rel *rels;
FAR Elf32_Rel *rel;
FAR elf32_symcache_t *cache;
FAR Elf32_Sym *sym;
FAR Elf_Shdr *relsec = &loadinfo->shdr[relidx];
FAR Elf_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
FAR Elf_Rel *rels;
FAR Elf_Rel *rel;
FAR elf_symcache_t *cache;
FAR Elf_Sym *sym;
FAR dq_entry_t *e;
dq_queue_t q;
uintptr_t addr;
@@ -160,7 +200,7 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
int i;
int j;
rels = kmm_malloc(CONFIG_ELF_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
rels = kmm_malloc(CONFIG_ELF_RELOCATION_BUFFERCOUNT * sizeof(Elf_Rel));
if (rels == NULL)
{
berr("Failed to allocate memory for elf relocation\n");
@@ -176,7 +216,7 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
ret = OK;
for (i = j = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++)
for (i = j = 0; i < relsec->sh_size / sizeof(Elf_Rel); i++)
{
/* Read the relocation entry into memory */
@@ -198,14 +238,14 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
* in a bit-field within the r_info element.
*/
symidx = ELF32_R_SYM(rel->r_info);
symidx = ELF_R_SYM(rel->r_info);
/* First try the cache */
sym = NULL;
for (e = dq_peek(&q); e; e = dq_next(e))
{
cache = (FAR elf32_symcache_t *)e;
cache = (FAR elf_symcache_t *)e;
if (cache->idx == symidx)
{
dq_rem(&cache->entry, &q);
@@ -223,7 +263,7 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
{
if (j < CONFIG_ELF_SYMBOL_CACHECOUNT)
{
cache = kmm_malloc(sizeof(elf32_symcache_t));
cache = kmm_malloc(sizeof(elf_symcache_t));
if (!cache)
{
berr("Failed to allocate memory for elf symbols\n");
@@ -235,7 +275,7 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
}
else
{
cache = (FAR elf32_symcache_t *)dq_remlast(&q);
cache = (FAR elf_symcache_t *)dq_remlast(&q);
}
sym = &cache->sym;
@@ -268,12 +308,14 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
if (ret == -ESRCH)
{
berr("Section %d reloc %d: Undefined symbol[%d] has no name: %d\n",
relidx, i, symidx, ret);
berr("Section %d reloc %d: "
"Undefined symbol[%d] has no name: %d\n",
relidx, i, symidx, ret);
}
else
{
berr("Section %d reloc %d: Failed to get value of symbol[%d]: %d\n",
berr("Section %d reloc %d: "
"Failed to get value of symbol[%d]: %d\n",
relidx, i, symidx, ret);
kmm_free(cache);
break;
@@ -308,7 +350,8 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
ret = up_relocate(rel, sym, addr);
if (ret < 0)
{
berr("ERROR: Section %d reloc %d: Relocation failed: %d\n", relidx, i, ret);
berr("ERROR: Section %d reloc %d: Relocation failed: %d\n",
relidx, i, ret);
break;
}
}
@@ -326,8 +369,184 @@ static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
static int elf_relocateadd(FAR struct elf_loadinfo_s *loadinfo, int relidx,
FAR const struct symtab_s *exports, int nexports)
{
berr("Not implemented\n");
return -ENOSYS;
FAR Elf_Shdr *relsec = &loadinfo->shdr[relidx];
FAR Elf_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
FAR Elf_Rela *relas;
FAR Elf_Rela *rela;
FAR elf_symcache_t *cache;
FAR Elf_Sym *sym;
FAR dq_entry_t *e;
dq_queue_t q;
uintptr_t addr;
int symidx;
int ret;
int i;
int j;
relas = kmm_malloc(CONFIG_ELF_RELOCATION_BUFFERCOUNT * sizeof(Elf_Rela));
if (relas == NULL)
{
berr("Failed to allocate memory for elf relocation\n");
return -ENOMEM;
}
dq_init(&q);
/* Examine each relocation in the section. 'relsec' is the section
* containing the relations. 'dstsec' is the section containing the data
* to be relocated.
*/
ret = OK;
for (i = j = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++)
{
/* Read the relocation entry into memory */
rela = &relas[i % CONFIG_ELF_RELOCATION_BUFFERCOUNT];
if (!(i % CONFIG_ELF_RELOCATION_BUFFERCOUNT))
{
ret = elf_readrelas(loadinfo, relsec, i, relas,
CONFIG_ELF_RELOCATION_BUFFERCOUNT);
if (ret < 0)
{
berr("Section %d reloc %d: Failed to read relocation entry: %d\n",
relidx, i, ret);
break;
}
}
/* Get the symbol table index for the relocation. This is contained
* in a bit-field within the r_info element.
*/
symidx = ELF_R_SYM(rela->r_info);
/* First try the cache */
sym = NULL;
for (e = dq_peek(&q); e; e = dq_next(e))
{
cache = (FAR elf_symcache_t *)e;
if (cache->idx == symidx)
{
dq_rem(&cache->entry, &q);
dq_addfirst(&cache->entry, &q);
sym = &cache->sym;
break;
}
}
/* If the symbol was not found in the cache, we will need to read the
* symbol from the file.
*/
if (sym == NULL)
{
if (j < CONFIG_ELF_SYMBOL_CACHECOUNT)
{
cache = kmm_malloc(sizeof(elf_symcache_t));
if (!cache)
{
berr("Failed to allocate memory for elf symbols\n");
ret = -ENOMEM;
break;
}
j++;
}
else
{
cache = (FAR elf_symcache_t *)dq_remlast(&q);
}
sym = &cache->sym;
/* Read the symbol table entry into memory */
ret = elf_readsym(loadinfo, symidx, sym);
if (ret < 0)
{
berr("Section %d reloc %d: Failed to read symbol[%d]: %d\n",
relidx, i, symidx, ret);
kmm_free(cache);
break;
}
/* Get the value of the symbol (in sym.st_value) */
ret = elf_symvalue(loadinfo, sym, exports, nexports);
if (ret < 0)
{
/* The special error -ESRCH is returned only in one condition: The
* symbol has no name.
*
* There are a few relocations for a few architectures that do
* no depend upon a named symbol. We don't know if that is the
* case here, but we will use a NULL symbol pointer to indicate
* that case to up_relocate(). That function can then do what
* is best.
*/
if (ret == -ESRCH)
{
berr("Section %d reloc %d: "
"Undefined symbol[%d] has no name: %d\n",
relidx, i, symidx, ret);
}
else
{
berr("Section %d reloc %d: "
"Failed to get value of symbol[%d]: %d\n",
relidx, i, symidx, ret);
kmm_free(cache);
break;
}
}
cache->idx = symidx;
dq_addfirst(&cache->entry, &q);
}
if (sym->st_shndx == SHN_UNDEF && sym->st_name == 0)
{
sym = NULL;
}
/* Calculate the relocation address. */
if (rela->r_offset < 0 ||
rela->r_offset > dstsec->sh_size)
{
berr("Section %d reloc %d: Relocation address out of range, "
"offset %d size %d\n",
relidx, i, rela->r_offset, dstsec->sh_size);
ret = -EINVAL;
break;
}
addr = dstsec->sh_addr + rela->r_offset;
/* Now perform the architecture-specific relocation */
ret = up_relocateadd(rela, sym, addr);
if (ret < 0)
{
berr("ERROR: Section %d reloc %d: Relocation failed: %d\n",
relidx, i, ret);
break;
}
}
kmm_free(relas);
while ((e = dq_peek(&q)))
{
dq_rem(e, &q);
kmm_free(e);
}
return ret;
}
/****************************************************************************
+1 -1
View File
@@ -88,7 +88,7 @@
int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
{
FAR Elf32_Shdr *shdr;
FAR Elf_Shdr *shdr;
size_t ctorsize;
int ctoridx;
int ret;
+1 -1
View File
@@ -88,7 +88,7 @@
int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo)
{
FAR Elf32_Shdr *shdr;
FAR Elf_Shdr *shdr;
size_t dtorsize;
int dtoridx;
int ret;
+2 -2
View File
@@ -173,7 +173,7 @@ int elf_init(FAR const char *filename, FAR struct elf_loadinfo_s *loadinfo)
/* Read the ELF ehdr from offset 0 */
ret = elf_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr), 0);
sizeof(Elf_Ehdr), 0);
if (ret < 0)
{
berr("Failed to read ELF header: %d\n", ret);
@@ -182,7 +182,7 @@ int elf_init(FAR const char *filename, FAR struct elf_loadinfo_s *loadinfo)
}
elf_dumpbuffer("ELF header", (FAR const uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr));
sizeof(Elf_Ehdr));
/* Verify the ELF header */
+4 -3
View File
@@ -106,7 +106,7 @@ static void elf_elfsize(struct elf_loadinfo_s *loadinfo)
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
{
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
/* SHF_ALLOC indicates that the section requires memory during
* execution.
@@ -164,7 +164,7 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
{
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
/* SHF_ALLOC indicates that the section requires memory during
* execution.
@@ -286,7 +286,8 @@ int elf_load(FAR struct elf_loadinfo_s *loadinfo)
/* Allocate (and zero) memory for the ELF file. */
ret = elf_addrenv_alloc(loadinfo, loadinfo->textsize, loadinfo->datasize, heapsize);
ret = elf_addrenv_alloc(loadinfo, loadinfo->textsize, loadinfo->datasize,
heapsize);
if (ret < 0)
{
berr("ERROR: elf_addrenv_alloc() failed: %d\n", ret);
+4 -4
View File
@@ -75,9 +75,9 @@
****************************************************************************/
static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo,
FAR const Elf32_Shdr *shdr)
FAR const Elf_Shdr *shdr)
{
FAR Elf32_Shdr *shstr;
FAR Elf_Shdr *shstr;
FAR uint8_t *buffer;
off_t offset;
size_t readlen;
@@ -210,7 +210,7 @@ int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo)
/* Allocate memory to hold a working copy of the sector header table */
loadinfo->shdr = (FAR FAR Elf32_Shdr *)kmm_malloc(shdrsize);
loadinfo->shdr = (FAR FAR Elf_Shdr *)kmm_malloc(shdrsize);
if (!loadinfo->shdr)
{
berr("Failed to allocate the section header table. Size: %ld\n",
@@ -249,7 +249,7 @@ int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo)
int elf_findsection(FAR struct elf_loadinfo_s *loadinfo,
FAR const char *sectname)
{
FAR const Elf32_Shdr *shdr;
FAR const Elf_Shdr *shdr;
int ret;
int i;
+10 -9
View File
@@ -78,7 +78,7 @@
****************************************************************************/
static int elf_symname(FAR struct elf_loadinfo_s *loadinfo,
FAR const Elf32_Sym *sym)
FAR const Elf_Sym *sym)
{
FAR uint8_t *buffer;
off_t offset;
@@ -215,14 +215,14 @@ int elf_findsymtab(FAR struct elf_loadinfo_s *loadinfo)
****************************************************************************/
int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
FAR Elf32_Sym *sym)
FAR Elf_Sym *sym)
{
FAR Elf32_Shdr *symtab = &loadinfo->shdr[loadinfo->symtabidx];
FAR Elf_Shdr *symtab = &loadinfo->shdr[loadinfo->symtabidx];
off_t offset;
/* Verify that the symbol table index lies within symbol table */
if (index < 0 || index > (symtab->sh_size / sizeof(Elf32_Sym)))
if (index < 0 || index > (symtab->sh_size / sizeof(Elf_Sym)))
{
berr("Bad relocation symbol index: %d\n", index);
return -EINVAL;
@@ -230,11 +230,11 @@ int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
/* Get the file offset to the symbol table entry */
offset = symtab->sh_offset + sizeof(Elf32_Sym) * index;
offset = symtab->sh_offset + sizeof(Elf_Sym) * index;
/* And, finally, read the symbol table entry into memory */
return elf_read(loadinfo, (FAR uint8_t *)sym, sizeof(Elf32_Sym), offset);
return elf_read(loadinfo, (FAR uint8_t *)sym, sizeof(Elf_Sym), offset);
}
/****************************************************************************
@@ -260,7 +260,7 @@ int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
*
****************************************************************************/
int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym,
int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf_Sym *sym,
FAR const struct symtab_s *exports, int nexports)
{
FAR const struct symtab_s *symbol;
@@ -314,7 +314,8 @@ int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym,
#endif
if (!symbol)
{
berr("SHN_UNDEF: Exported symbol \"%s\" not found\n", loadinfo->iobuffer);
berr("SHN_UNDEF: Exported symbol \"%s\" not found\n",
loadinfo->iobuffer);
return -ENOENT;
}
@@ -324,7 +325,7 @@ int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym,
loadinfo->iobuffer, sym->st_value, symbol->sym_value,
sym->st_value + symbol->sym_value);
sym->st_value += (Elf32_Word)((uintptr_t)symbol->sym_value);
sym->st_value += (Elf_Word)((uintptr_t)symbol->sym_value);
}
break;
+1 -1
View File
@@ -84,7 +84,7 @@ static const char g_elfmagic[EI_MAGIC_SIZE] =
*
****************************************************************************/
int elf_verifyheader(FAR const Elf32_Ehdr *ehdr)
int elf_verifyheader(FAR const Elf_Ehdr *ehdr)
{
if (!ehdr)
{