The Pedigree Project  0.1
ppc/Elf32.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef ELF32_H
21 #define ELF32_H
22 
23 #define SHT_PROGBITS 0x1 // The data is contained in the program file.
24 #define SHT_SYMTAB 0x2 // Symbol table
25 #define SHT_STRTAB 0x3 // String table
26 #define SHT_RELA 0x4
27 #define SHT_HASH 0x5 // Symbol hash table
28 #define SHT_DYNAMIC 0x6 // Dynamic linking information
29 #define SHT_NOTE 0x7
30 #define SHT_NOBITS 0x8 // The data is not contained in the program file.
31 #define SHT_REL 0x9
32 #define SHT_DYNSYM 0xb
33 #define SHT_INIT_ARRAY 0xe
34 #define SHT_FINI_ARRAY 0xf
35 #define SHT_PREINIT_ARRAY 0x10
36 
37 #define SHF_WRITE 0x1
38 #define SHF_ALLOC 0x2
39 #define SHF_EXECINSTR 0x4
40 #define SHF_MASKPROC 0xf0000000
41 
42 #define ELF32_R_SYM(val) ((val) >> 8)
43 #define ELF32_R_TYPE(val) ((val) &0xff)
44 
45 #define ELF32_ST_BIND(i) ((i) >> 4)
46 #define ELF32_ST_TYPE(i) ((i) &0xf)
47 #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) &0xf))
48 #define PACKED __attribute__((packed))
49 class BootstrapInfo;
50 
51 typedef unsigned char uint8_t;
52 typedef unsigned short uint16_t;
53 typedef unsigned int uint32_t;
54 typedef int int32_t;
55 typedef unsigned int size_t;
56 
64 class Elf32
65 {
66  public:
72  Elf32(const char *name);
73 
77  ~Elf32();
78 
83  bool load(uint8_t *pBuffer, unsigned int nBufferLength);
84 
88  bool load(BootstrapInfo *pBootstrap);
89 
94  bool writeSections();
95 
100  unsigned int getLastAddress();
101 
109  const char *lookupSymbol(unsigned int addr, unsigned int *startAddr = 0);
110 
115  uint32_t lookupDynamicSymbolAddress(uint32_t off);
116 
121  char *lookupDynamicSymbolName(uint32_t off);
122 
127  uint32_t getGlobalOffsetTable();
128 
132  uint32_t getEntryPoint();
133 
134  // private:
137  Elf32(const Elf32 &);
140  Elf32 &operator=(const Elf32 &);
141 
142  struct Elf32Header_t
143  {
144  uint8_t ident[16];
145  uint16_t type;
146  uint16_t machine;
147  uint32_t version;
148  uint32_t entry;
149  uint32_t phoff;
150  uint32_t shoff;
151  uint32_t flags;
152  uint16_t ehsize;
153  uint16_t phentsize;
154  uint16_t phnum;
155  uint16_t shentsize;
156  uint16_t shnum;
157  uint16_t shstrndx;
158  } PACKED;
159 
160  struct Elf32ProcessHeader_t
161  {
162  uint32_t type;
163  uint32_t offset;
164  uint32_t vaddr;
165  uint32_t paddr;
166  uint32_t filesz;
167  uint32_t memsz;
168  uint32_t flags;
169  uint32_t align;
170  } PACKED;
171 
172  struct Elf32SectionHeader_t
173  {
174  uint32_t name;
175  uint32_t type;
176  uint32_t flags;
177  uint32_t addr;
178  uint32_t offset;
179  uint32_t size;
180  uint32_t link;
181  uint32_t info;
182  uint32_t addralign;
183  uint32_t entsize;
184  } PACKED;
185 
186  struct Elf32Symbol_t
187  {
188  uint32_t name;
189  uint32_t value;
190  uint32_t size;
191  uint8_t info;
192  uint8_t other;
193  uint16_t shndx;
194  } PACKED;
195 
196  struct Elf32Dyn_t
197  {
198  int32_t tag;
199  union
200  {
201  int32_t val;
202  uint32_t ptr;
203  } un;
204  } PACKED;
205 
206  struct Elf32Rel_t
207  {
208  uint32_t offset;
209  uint32_t info;
210  } PACKED;
211 
212  Elf32Header_t *m_pHeader;
213  Elf32SectionHeader_t *m_pSymbolTable;
214  Elf32SectionHeader_t *m_pStringTable;
215  Elf32SectionHeader_t *m_pShstrtab;
216  Elf32SectionHeader_t *m_pGotTable; // Global offset table.
217  Elf32SectionHeader_t *m_pRelTable;
218  Elf32SectionHeader_t *m_pSectionHeaders;
219  char m_pId[128];
220  uint8_t *m_pBuffer;
221 };
222 
223 #endif
unsigned int getLastAddress()
uint32_t getGlobalOffsetTable()
const char * lookupSymbol(unsigned int addr, unsigned int *startAddr=0)
char * lookupDynamicSymbolName(uint32_t off)
uint32_t lookupDynamicSymbolAddress(uint32_t off)
Elf32(const char *name)
Elf32 & operator=(const Elf32 &)
uint32_t getEntryPoint()
uint8_t * m_pBuffer
Offset of the file in memory.
Definition: arm/Elf32.h:217
bool load(uint8_t *pBuffer, unsigned int nBufferLength)
bool writeSections()