From 60e71f652198ce3e62f6343aac342e3a5590f842 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Mon, 20 Nov 2023 00:09:02 +0100 Subject: [PATCH] elf: move struct definition to header --- core/elf.c | 38 ---------------------- core/elf.h | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 38 deletions(-) diff --git a/core/elf.c b/core/elf.c index d3d0f89..f87572d 100644 --- a/core/elf.c +++ b/core/elf.c @@ -18,35 +18,7 @@ uaddr_t loadElfProg(const char *prog, struct process * proc) int i; uaddr_t lastUserAddr = 0; - /** - * Typedefs, constants and structure definitions as given by the ELF - * standard specifications. - */ - typedef unsigned long Elf32_Addr; - typedef unsigned long Elf32_Word; - typedef unsigned short Elf32_Half; - typedef unsigned long Elf32_Off; - // typedef signed long Elf32_Sword; - /* Elf identification */ - -#define EI_NIDENT 16 - 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; - } __attribute__((packed)) Elf32_Ehdr_t; /* e_ident value */ #define ELFMAG0 0x7f @@ -97,16 +69,6 @@ uaddr_t loadElfProg(const char *prog, struct process * proc) #define EV_NONE 0 /* invalid version */ #define EV_CURRENT 1 /* current version */ - 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; - } __attribute__((packed)) Elf32_Phdr_t; /* Reserved segment types p_type */ #define PT_NULL 0 diff --git a/core/elf.h b/core/elf.h index 808ce3a..3ea82dc 100644 --- a/core/elf.h +++ b/core/elf.h @@ -3,4 +3,97 @@ #include "process.h" #include "thread.h" #include "types.h" + +/** + * Typedefs, constants and structure definitions as given by the ELF + * standard specifications. + */ +typedef unsigned long Elf32_Addr; +typedef unsigned long Elf32_Word; +typedef unsigned short Elf32_Half; +typedef unsigned long Elf32_Off; +// typedef signed long Elf32_Sword; + +/** + * ELF 32-bit format related structures. + * See http://www.cs.cmu.edu/afs/cs/academic/class/15213-f00/docs/elf.pdf + */ + +#define SHT_NULL 0 +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHT_STRTAB 3 +#define SHT_RELA 4 +#define SHT_HASH 5 +#define SHT_DYNAMIC 6 +#define SHT_NOTE 7 +#define SHT_NOBITS 8 +#define SHT_REL 9 +#define SHT_SHLIB 10 +#define SHT_DYNSYM 11 +#define SHT_LOPROC 0x70000000 +#define SHT_HIPROC 0x7fffffff +#define SHT_LOUSER 0x80000000 +#define SHT_HIUSER 0xffffffff + + +/** ELF Header **/ +#define EI_NIDENT 16 +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; +} __attribute__((packed)) Elf32_Ehdr_t; + +/** ELF 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; +} __attribute__((packed)) Elf32_Shdr_t; + +/** ELF Symbol **/ +typedef struct { + Elf32_Word st_name; + Elf32_Addr st_value; + Elf32_Word st_size; + unsigned char st_info; + unsigned char st_other; + Elf32_Half st_shndx; +} __attribute__((packed)) Elf32_Sym_t; + +/** ELF 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; +} __attribute__((packed)) Elf32_Phdr_t; + +#define ELF_SYM_TYPE(info) ((info) & 0xf) +#define ELF_SYM_TYPE_FUNC 0x2 + + uaddr_t loadElfProg(const char *prog, struct process *proc);