A little more ELF loader logic

git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5257 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2012-10-25 16:18:20 +00:00
parent a91b6552cd
commit 630862cd04
15 changed files with 568 additions and 153 deletions
+37 -37
View File
@@ -231,73 +231,73 @@
/* Figure 4.2: 32-Bit Data Types */
typedef uint32_t ELF32_Addr /* Unsigned program address */
typedef uint16_t ELF32_Half /* Unsigned medium integer */
typedef uint32_t ELF32_Off /* Unsigned file offset */
typedef int32_t ELF32_Sword /* Signed large integer */
typedef uint32_t ELF32_Word /* Unsigned large integer */
typedef uint32_t Elf32_Addr; /* Unsigned program address */
typedef uint16_t Elf32_Half; /* Unsigned medium integer */
typedef uint32_t Elf32_Off; /* Unsigned file offset */
typedef int32_t Elf32_Sword; /* Signed large integer */
typedef uint32_t Elf32_Word; /* Unsigned large integer */
/* Figure 4-3: ELF Header */
typedef struct
{
unsigned char e_ident[EI_NIDENT];
ELF32_Half e_type;
ELF32_Half e_machine;
ELF32_Word e_version;
ELF32_Addr e_entry;
ELF32_Off e_phoff;
ELF32_Off e_shoff;
ELF32_Word e_flags;
ELF32_Half e_ehsize;
ELF32_Half e_phentsize;
ELF32_Half e_phnum;
ELF32_Half e_shentsize;
ELF32_Half e_shnum;
ELF32_Half e_shstrndx;
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
/* Figure 4-8: Section Header */
typedef struct
{
ELF32_Word sh_name;
ELF32_Word sh_type;
ELF32_Word sh_flags;
ELF32_Addr sh_addr;
ELF32_Off sh_offset;
ELF32_Word sh_size;
ELF32_Word sh_link;
ELF32_Word sh_info;
ELF32_Word sh_addralign;
ELF32_Word sh_entsize;
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
/* Figure 4-15: Symbol Table Entry */
typedef struct
{
ELF32_Word st_name;
ELF32_Addr st_value;
ELF32_Word st_size;
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
ELF32_Half st_shndx;
Elf32_Half st_shndx;
} Elf32_Sym;
/* Figure 4-19: Relocation Entries */
typedef struct
{
ELF32_Addr r_offset;
ELF32_Word r_info;
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;
typedef struct
{
ELF32_Addr r_offset;
ELF32_Word r_info;
ELF32_Sword r_addend;
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;
} Elf32_Rela;
/* Figure 5-1: Program Header */
+63 -32
View File
@@ -42,9 +42,10 @@
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <elf.h>
#include <nuttx/sched.h>
/****************************************************************************
* Pre-processor Definitions
@@ -72,6 +73,8 @@ struct elf_loadinfo_s
uintptr_t ctors; /* Static constructors */
#endif
int filfd; /* Descriptor for the file being loaded */
uint16_t symtabidx; /* Symbol table section index */
uint16_t strtabidx; /* String table section index */
Elf32_Ehdr ehdr; /* Buffered ELF file header */
FAR Elf32_Shdr *shdr; /* Buffered ELF section headers */
};
@@ -89,24 +92,9 @@ extern "C" {
#endif
/****************************************************************************
* These are APIs exported by libelf (and may be used outside of NuttX):
* These are APIs exported by libelf (but are used only by the binfmt logic):
****************************************************************************/
/****************************************************************************
* Name: elf_verifyheader
*
* Description:
* Given the header from a possible ELF executable, verify that it is
* an ELF executable.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
EXTERN int elf_verifyheader(FAR const Elf32_Ehdr *header);
/****************************************************************************
* Name: elf_init
*
@@ -153,21 +141,6 @@ EXTERN int elf_uninit(FAR struct elf_loadinfo_s *loadinfo);
EXTERN int elf_load(FAR struct elf_loadinfo_s *loadinfo);
/****************************************************************************
* Name: elf_read
*
* Description:
* Read 'readsize' bytes from the object file at 'offset'
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
EXTER int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer,
size_t readsize, off_t offset);
/****************************************************************************
* Name: elf_bind
*
@@ -233,6 +206,64 @@ EXTERN int elf_initialize(void);
EXTERN void elf_uninitialize(void);
/****************************************************************************
* Name: arch_checkarch
*
* Description:
* Given the ELF header in 'hdr', verify that the ELF file is appropriate
* for the current, configured architecture. Every architecture that uses
* the ELF loader must provide this function.
*
* Input Parameters:
* hdr - The ELF header read from the ELF file.
*
* Returned Value:
* True if the architecture supports this ELF file.
*
****************************************************************************/
EXTERN bool arch_checkarch(FAR const Elf32_Ehdr *hdr);
/****************************************************************************
* Name: arch_relocate and arch_relocateadd
*
* Description:
* Perform on architecture-specific ELF relocation. Every architecture
* that uses the ELF loader must provide this function.
*
* Input Parameters:
* rel - The relocation type
* sym - The ELF symbol structure containing the fully resolved value.
* addr - The address that requires the relocation.
*
* Returned Value:
* Zero (OK) if the relocation was successful. Otherwise, a negated errno
* value indicating the cause of the relocation failure.
*
****************************************************************************/
EXTERN int arch_relocate(FAR const Elf32_Rel *rel, FAR const Elf32_Sym *sym,
uintptr_t addr);
EXTERN int arch_relocateadd(FAR const Elf32_Rela *rel,
FAR const Elf32_Sym *sym, uintptr_t addr);
/****************************************************************************
* Name: arch_flushicache
*
* Description:
* Flush the instruction cache.
*
* Input Parameters:
* addr - Start address to flush
* len - Number of bytes to flush
*
* Returned Value:
* True if the architecture supports this ELF file.
*
****************************************************************************/
EXTERN bool arch_flushicache(FAR void *addr, size_t len);
#undef EXTERN
#if defined(__cplusplus)
}