00001
00002
00003 #ifndef _CORE_MODULE_ELF_HPP
00004 #define _CORE_MODULE_ELF_HPP
00005
00006
00007
00008
00009
00010
00011
00012
00013 const uint32 DEF_EI_NIDENT = 16;
00014
00015 const uint32 EI_CLASS = 4;
00016 const uint32 EI_DATA = 5;
00017 const uint32 EI_VERSION = 6;
00018 const uint32 ELFCLASSNONE = 0;
00019 const uint32 ELFCLASS32 = 1;
00020 const uint32 ELFCLASS64 = 2;
00021 const uint32 ELFDATANONE = 0;
00022 const uint32 ELFDATA2LSB = 1;
00023 const uint32 ELFDATA2MSB = 2;
00024 const uint32 EV_NONE = 0;
00025 const uint32 EV_CURRENT = 1;
00026 const uint32 ET_NONE = 0;
00027 const uint32 ET_REL = 1;
00028 const uint32 ET_EXEC = 2;
00029 const uint32 ET_DYN = 3;
00030 const uint32 ET_CORE = 4;
00031 const uint32 ET_LOPROC = 0xff00;
00032 const uint32 ET_HIPROC = 0xffff;
00033 const uint32 EM_NONE = 0;
00034 const uint32 EM_M32 = 1;
00035 const uint32 EM_SPARC = 2;
00036 const uint32 EM_386 = 3;
00037 const uint32 EM_68K = 4;
00038 const uint32 EM_88K = 5;
00039 const uint32 EM_860 = 7;
00040 const uint32 EM_MIPS = 8;
00041 const uint32 EM_MIPS_RS4_BE = 10;
00042
00043
00044 const uint32 SHT_NULL = 0;
00045 const uint32 SHT_PROGBITS = 1;
00046 const uint32 SHT_SYMTAB = 2;
00047 const uint32 SHT_STRTAB = 3;
00048 const uint32 SHT_RELA = 4;
00049 const uint32 SHT_HASH = 5;
00050 const uint32 SHT_DYNAMIC = 6;
00051 const uint32 SHT_NOTE = 7;
00052 const uint32 SHT_NOBITS = 8;
00053 const uint32 SHT_REL = 9;
00054 const uint32 SHT_SHLIB = 10;
00055 const uint32 SHT_DYNSYM = 11;
00056 const uint32 SHT_LOPROC = 0x70000000;
00057 const uint32 SHT_HIPROC = 0x7fffffff;
00058 const uint32 SHT_LOUSER = 0x80000000;
00059 const uint32 SHT_HIUSER = 0x8fffffff;
00060
00061
00062 const uint32 PT_NULL = 0;
00063 const uint32 PT_LOAD = 1;
00064 const uint32 PT_DYNAMIC = 2;
00065 const uint32 PT_INTERP = 3;
00066 const uint32 PT_NOTE = 4;
00067 const uint32 PT_SHLIB = 5;
00068 const uint32 PT_PHDR = 6;
00069 const uint32 PT_LOPROC = 0x70000000;
00070 const uint32 PT_HIPROC = 0x7fffffff;
00071
00072
00073 const uint32 STB_LOCAL = 0x0;
00074 const uint32 STB_GLOBAL = 0x1;
00075 const uint32 STB_WEAK = 0x2;
00076 const uint32 STB_LOPROC = 13;
00077 const uint32 STB_HIPROC = 15;
00078
00079
00080 const uint32 STT_NOTYPE = 0;
00081 const uint32 STT_OBJECT = 1;
00082 const uint32 STT_FUNC = 2;
00083 const uint32 STT_SECTION = 3;
00084 const uint32 STT_FILE = 4;
00085 const uint32 STT_LOPROC = 13;
00086 const uint32 STT_HIPROC = 15;
00087
00088
00089 const uint32 SHN_UNDEF = 0;
00090 const uint32 SHN_LORESERVE = 0xff00;
00091 const uint32 SHN_LOPROC = 0xff00;
00092 const uint32 SHN_HIPROC = 0xff1f;
00093 const uint32 SHN_ABS = 0xfff1;
00094 const uint32 SHN_COMMON = 0xfff2;
00095 const uint32 SHN_HIRESERVE = 0xffff;
00096
00097
00098 const uint32 R_386_NONE = 0;
00099 const uint32 R_386_32 = 1;
00100 const uint32 R_386_PC32 = 2;
00101
00102
00103 #define ELF32_ST_BIND(i) ((i)>>4)
00104 #define ELF32_ST_TYPE(i) ((i)&0xf)
00105 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
00106
00107
00108 struct Elf32_ehdr
00109 {
00110 unsigned char e_ident [DEF_EI_NIDENT];
00111 uint16 e_type;
00112 uint16 e_machine;
00113 uint32 e_version;
00114 uint32 e_entry;
00115 uint32 e_phoff;
00116 uint32 e_shoff;
00117 uint32 e_flags;
00118 uint16 e_ehsize;
00119 uint16 e_phentsize;
00120 uint16 e_phnum;
00121 uint16 e_shentsize;
00122 uint16 e_shnum;
00123 uint16 e_shstrndx;
00124 } __attribute__ ((packed));
00125
00126
00127 struct Elf32_shdr
00128 {
00129 uint32 sh_name;
00130 uint32 sh_type;
00131 uint32 sh_flags;
00132 uint32 sh_addr;
00133 uint32 sh_offset;
00134 uint32 sh_size;
00135 uint32 sh_link;
00136 uint32 sh_info;
00137 uint32 sh_addralign;
00138 uint32 sh_entsize;
00139 };
00140
00141
00142 struct Elf32_phdr
00143 {
00144 uint32 p_type;
00145 uint32 p_offset;
00146 uint32 p_vaddr;
00147 uint32 p_paddr;
00148 uint32 p_filesz;
00149 uint32 p_memsz;
00150 uint32 p_flags;
00151 uint32 p_align;
00152 } __attribute__ ((packed));
00153
00154
00155 struct Elf32_sym
00156 {
00157 uint32 st_name;
00158 uint32 st_value;
00159 uint32 st_size;
00160 uint8 st_info;
00161 uint8 st_other;
00162 uint16 st_shndx;
00163 } __attribute__ ((packed));
00164
00165
00166 struct Elf32_reloc
00167 {
00168 uint32 r_addr;
00169 uint8 r_type;
00170 uint32 r_symtabndx :24;
00171 uint32 r_addend;
00172 } __attribute__ ((packed));
00173
00174
00175
00176
00177 class elf_t
00178 {
00179 public:
00180 char *image;
00181 Elf32_ehdr *ehdr;
00182 uint32 size;
00183 char valid;
00184
00185
00186
00187 void init(char *ptr, uint32 sze);
00188 void set_ehdr(Elf32_ehdr *_ehdr, uint32 sze);
00189
00190 void done();
00191
00192 char ident();
00193
00194 Elf32_shdr *section(uint32 n);
00195 void *sect_addr(uint32 n);
00196 Elf32_phdr *segment(uint32 n);
00197 void *segm_addr(uint32 n);
00198
00199 Elf32_sym *symbol(uint32 sect, uint32 n);
00200 char *str(uint32 sect, uint32 off);
00201
00202 Elf32_reloc *reloc(uint32 sect, uint32 n);
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 };
00213
00214
00215 const uint32 DEFINED = 1;
00216 const uint32 UNDEFINED = 0;
00217
00218 int create_ehdr(Elf32_ehdr *elf_hdr, uint32 shnum, Elf32_shdr *shaddr,
00219 uint32 shentsize, uint32 shstrndx, uint32 e_type);
00220
00221 #endif