Skip to content

Commit

Permalink
elf: Move 32/64bit generic mapping from risc-v/arch_elf.c to elfxx.h
Browse files Browse the repository at this point in the history
Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 authored and acassis committed Jul 10, 2023
1 parent 5849cf7 commit 813b652
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 113 deletions.
11 changes: 4 additions & 7 deletions arch/arm/include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@
*/

#define EM_ARCH EM_ARM
#define ELF_CLASS ELFCLASS32

#ifdef CONFIG_ENDIAN_BIG
# define ELF_DATA ELFDATA2MSB
#else
# define ELF_DATA ELFDATA2LSB
#endif

/* Table 4-2, ARM-specific e_flags */

Expand Down Expand Up @@ -211,6 +204,10 @@
#define R_ARM_THM_TLS_DESCSEQ16 129 /* Thumb16 */
#define R_ARM_THM_TLS_DESCSEQ32 130 /* Thumb32 */

/* Processor specific values for the Phdr p_type field. */

#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */

/* 5.2.1 Platform architecture compatibility data */

#define PT_ARM_ARCHEXT_FMTMSK 0xff000000
Expand Down
17 changes: 3 additions & 14 deletions arch/sim/include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@
/* 4.3.1 ELF Identification. Should have:
*
* e_machine = EM_386
* e_ident[EI_CLASS] = ELFCLASS32
* e_ident[EI_DATA] = ELFDATA2LSB (little endian) or
* ELFDATA2MSB (big endian)
*/

#ifdef CONFIG_SIM_M32
# define EM_ARCH EM_386
# define ELF_CLASS ELFCLASS32
# define EM_ARCH EM_386
#else
# define EM_ARCH EM_X86_64
# define ELF_CLASS ELFCLASS32
# define EM_ARCH EM_X86_64
#endif

#ifdef CONFIG_ENDIAN_BIG
# define ELF_DATA ELFDATA2MSB
#else
# define ELF_DATA ELFDATA2LSB
#endif

#define EF_FLAG 0
#define EF_FLAG 0

#endif /* __ARCH_SIM_INCLUDE_ELF_H */
16 changes: 9 additions & 7 deletions include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,11 @@
#define PT_SHLIB 5
#define PT_PHDR 6

/* Processor specific values for the Phdr p_type field. */

#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */

/* GCC specific */

#define PT_GNU_EH_FRAME 0x6474e550 /* GCC exception handler frame */
#define PT_GNU_STACK 0x6474e551 /* Stack executability */
#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
#define PT_GNU_EH_FRAME 0x6474e550 /* GCC exception handler frame */
#define PT_GNU_STACK 0x6474e551 /* Stack executability */
#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */

#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
Expand Down Expand Up @@ -366,4 +362,10 @@

#define NT_VERSION 1 /* Contains a version string. */

#ifdef CONFIG_ENDIAN_BIG
# define ELF_DATA ELFDATA2MSB
#else
# define ELF_DATA ELFDATA2LSB
#endif

#endif /* __INCLUDE_ELF_H */
79 changes: 42 additions & 37 deletions include/elf32.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,32 @@
* Pre-processor Definitions
****************************************************************************/

#define ELF_CLASS ELFCLASS32

#define ELF32_ST_BIND(i) ((i) >> 4)
#define ELF32_ST_TYPE(i) ((i) & 0xf)
#define ELF32_ST_INFO(b,t) (((b) << 4) | ((t) & 0xf))

/* Definitions for Elf32_Rel*::r_info */

#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((i) & 0xff)
#define ELF32_R_INFO(s,t) (((s)<< 8) | ((t) & 0xff))
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((i) & 0xff)
#define ELF32_R_INFO(s,t) (((s)<< 8) | ((t) & 0xff))

#define ELF_R_SYM(i) ELF32_R_SYM(i)
#define ELF_R_SYM(i) ELF32_R_SYM(i)
#define ELF_R_TYPE(i) ELF32_R_TYPE(i)

/****************************************************************************
* Public Type Definitions
****************************************************************************/

/* 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 */

Expand Down Expand Up @@ -110,60 +113,62 @@ typedef struct

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 */

typedef struct
{
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;

/* Figure 5-7: Note Information */

typedef struct
{
Elf32_Word n_namesz; /* Length of the note's name. */
Elf32_Word n_descsz; /* Length of the note's descriptor. */
Elf32_Word n_type; /* Type of the note. */
Elf32_Word n_namesz; /* Length of the note's name. */
Elf32_Word n_descsz; /* Length of the note's descriptor. */
Elf32_Word n_type; /* Type of the note. */
} Elf32_Nhdr;

/* Figure 5-9: Dynamic Structure */

typedef struct
{
Elf32_Sword d_tag;
Elf32_Sword d_tag;
union
{
Elf32_Word d_val;
Elf32_Addr d_ptr;
Elf32_Word d_val;
Elf32_Addr d_ptr;
} d_un;
} Elf32_Dyn;

typedef Elf32_Addr Elf_Addr;
typedef Elf32_Ehdr Elf_Ehdr;
typedef Elf32_Rel Elf_Rel;
typedef Elf32_Rela Elf_Rela;
typedef Elf32_Nhdr Elf_Nhdr;
typedef Elf32_Phdr Elf_Phdr;
typedef Elf32_Sym Elf_Sym;
typedef Elf32_Shdr Elf_Shdr;
typedef Elf32_Word Elf_Word;
typedef Elf32_Addr Elf_Addr;
typedef Elf32_Ehdr Elf_Ehdr;
typedef Elf32_Rel Elf_Rel;
typedef Elf32_Rela Elf_Rela;
typedef Elf32_Nhdr Elf_Nhdr;
typedef Elf32_Phdr Elf_Phdr;
typedef Elf32_Sym Elf_Sym;
typedef Elf32_Shdr Elf_Shdr;
typedef Elf32_Off Elf_Off;
typedef Elf32_Word Elf_Word;
typedef Elf32_Dyn Elf_Dyn;

#endif /* __INCLUDE_ELF32_H */
59 changes: 32 additions & 27 deletions include/elf64.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* Pre-processor Definitions
****************************************************************************/

#define ELF_CLASS ELFCLASS64

/* See ELF-64 Object File Format: Version 1.5 Draft 2 */

/* Definitions for Elf64_Rel*::r_info */
Expand All @@ -43,20 +45,21 @@
#define ELF64_R_INFO(s,t) (((s)<< 32) + ((t) & 0xffffffffL))

#define ELF_R_SYM(i) ELF64_R_SYM(i)
#define ELF_R_TYPE(i) ELF64_R_TYPE(i)

/****************************************************************************
* Public Type Definitions
****************************************************************************/

/* Table 1: ELF-64 Data Types */

typedef uint64_t Elf64_Addr; /* Unsigned program address */
typedef uint64_t Elf64_Off; /* Unsigned file offset */
typedef uint16_t Elf64_Half; /* Unsigned medium integer */
typedef uint32_t Elf64_Word; /* Unsigned long integer */
typedef int32_t Elf64_Sword; /* Signed integer */
typedef uint64_t Elf64_Xword; /* Unsigned long integer */
typedef int64_t Elf64_Sxword; /* Signed large integer */
typedef uint64_t Elf64_Addr; /* Unsigned program address */
typedef uint64_t Elf64_Off; /* Unsigned file offset */
typedef uint16_t Elf64_Half; /* Unsigned medium integer */
typedef uint32_t Elf64_Word; /* Unsigned long integer */
typedef int32_t Elf64_Sword; /* Signed integer */
typedef uint64_t Elf64_Xword; /* Unsigned long integer */
typedef int64_t Elf64_Sxword; /* Signed large integer */

/* Figure 2: ELF-64 Header */

Expand Down Expand Up @@ -125,23 +128,23 @@ typedef struct

typedef struct
{
Elf64_Word p_type; /* Type of segment */
Elf64_Word p_flags; /* Segment attributes */
Elf64_Off p_offset; /* Offset in file */
Elf64_Addr p_vaddr; /* Virtual address in memory */
Elf64_Addr p_paddr; /* Reserved */
Elf64_Word p_filesz; /* Size of segment in file */
Elf64_Word p_memsz; /* Size of segment in memory */
Elf64_Word p_align; /* Alignment of segment */
Elf64_Word p_type; /* Type of segment */
Elf64_Word p_flags; /* Segment attributes */
Elf64_Off p_offset; /* Offset in file */
Elf64_Addr p_vaddr; /* Virtual address in memory */
Elf64_Addr p_paddr; /* Reserved */
Elf64_Word p_filesz; /* Size of segment in file */
Elf64_Word p_memsz; /* Size of segment in memory */
Elf64_Word p_align; /* Alignment of segment */
} Elf64_Phdr;

/* Figure 7. Format of a Note Section */

typedef struct
{
Elf64_Word n_namesz; /* Length of the note's name. */
Elf64_Word n_descsz; /* Length of the note's descriptor. */
Elf64_Word n_type; /* Type of the note. */
Elf64_Word n_namesz; /* Length of the note's name. */
Elf64_Word n_descsz; /* Length of the note's descriptor. */
Elf64_Word n_type; /* Type of the note. */
} Elf64_Nhdr;

/* Figure 8: Dynamic Table Structure */
Expand All @@ -156,14 +159,16 @@ typedef struct
} d_un;
} Elf64_Dyn;

typedef Elf64_Addr Elf_Addr;
typedef Elf64_Ehdr Elf_Ehdr;
typedef Elf64_Rel Elf_Rel;
typedef Elf64_Rela Elf_Rela;
typedef Elf64_Nhdr Elf_Nhdr;
typedef Elf64_Phdr Elf_Phdr;
typedef Elf64_Sym Elf_Sym;
typedef Elf64_Shdr Elf_Shdr;
typedef Elf64_Word Elf_Word;
typedef Elf64_Addr Elf_Addr;
typedef Elf64_Ehdr Elf_Ehdr;
typedef Elf64_Rel Elf_Rel;
typedef Elf64_Rela Elf_Rela;
typedef Elf64_Nhdr Elf_Nhdr;
typedef Elf64_Phdr Elf_Phdr;
typedef Elf64_Sym Elf_Sym;
typedef Elf64_Shdr Elf_Shdr;
typedef Elf64_Off Elf_Off;
typedef Elf64_Word Elf_Word;
typedef Elf64_Dyn Elf_Dyn;

#endif /* __INCLUDE_ELF64_H */
15 changes: 3 additions & 12 deletions include/nuttx/lib/modlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@
* Public Types
****************************************************************************/

#ifdef CONFIG_LIBC_ARCH_ELF_64BIT
typedef Elf64_Off Elf_Off;
typedef Elf64_Dyn Elf_Dyn;
#else
typedef Elf32_Off Elf_Off;
typedef Elf32_Dyn Elf_Dyn;
#endif

/* This is the type of the function that is called to uninitialize the
* the loaded module. This may mean, for example, un-registering a device
* driver. If the module is successfully uninitialized, its memory will be
Expand Down Expand Up @@ -208,11 +200,10 @@ struct mod_loadinfo_s
Elf_Ehdr ehdr; /* Buffered module file header */
FAR Elf_Phdr *phdr; /* Buffered module program headers */
FAR Elf_Shdr *shdr; /* Buffered module section headers */
FAR void *exported; /* Module exports */
FAR void *exported; /* Module exports */
uint8_t *iobuffer; /* File I/O buffer */
uintptr_t datasec; /* ET_DYN - data area start from Phdr */
uintptr_t segpad; /* Padding between text and data */

uintptr_t datasec; /* ET_DYN - data area start from Phdr */
uintptr_t segpad; /* Padding between text and data */
uint16_t symtabidx; /* Symbol table section index */
uint16_t strtabidx; /* String table section index */
uint16_t dsymtabidx; /* Dynamic symbol table section index */
Expand Down
14 changes: 5 additions & 9 deletions libs/libc/machine/risc-v/arch_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@
/* ELF32 and ELF64 definitions */

#ifdef CONFIG_LIBC_ARCH_ELF_64BIT
# define ARCH_ELF_TYP_STR "64"
# define ARCH_ELF_CLASS ELFCLASS64
# define ARCH_ELF_RELTYPE ELF64_R_TYPE
# define ARCH_ELF_TYP_STR "64"
#else /* !CONFIG_LIBC_ARCH_ELF_64BIT */
# define ARCH_ELF_TYP_STR "32"
# define ARCH_ELF_CLASS ELFCLASS32
# define ARCH_ELF_RELTYPE ELF32_R_TYPE
# define ARCH_ELF_TYP_STR "32"
#endif /* CONFIG_LIBC_ARCH_ELF_64BIT */

/****************************************************************************
Expand Down Expand Up @@ -214,7 +210,7 @@ bool up_checkarch(const Elf_Ehdr *ehdr)

/* Make sure that current objects are supported */

if (ehdr->e_ident[EI_CLASS] != ARCH_ELF_CLASS)
if (ehdr->e_ident[EI_CLASS] != ELF_CLASS)
{
berr("ERROR: Need " ARCH_ELF_TYP_STR "-bit "
"objects: e_ident[EI_CLASS]=%02x\n",
Expand Down Expand Up @@ -284,7 +280,7 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,

/* All relocations depend upon having valid symbol information */

relotype = ARCH_ELF_RELTYPE(rel->r_info);
relotype = ELF_R_TYPE(rel->r_info);

if (relotype == R_RISCV_RELAX)
{
Expand Down Expand Up @@ -585,7 +581,7 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
break;
default:
berr("ERROR: Unsupported relocation: %ld\n",
ARCH_ELF_RELTYPE(rel->r_info));
ELF_R_TYPE(rel->r_info));
PANIC();
return -EINVAL;
}
Expand Down

0 comments on commit 813b652

Please sign in to comment.