The Pedigree Project  0.1
arm/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 #include "support.h"
24 
25 #define SHT_PROGBITS 0x1 // The data is contained in the program file.
26 #define SHT_SYMTAB 0x2 // Symbol table
27 #define SHT_STRTAB 0x3 // String table
28 #define SHT_RELA 0x4
29 #define SHT_HASH 0x5 // Symbol hash table
30 #define SHT_DYNAMIC 0x6 // Dynamic linking information
31 #define SHT_NOTE 0x7
32 #define SHT_NOBITS 0x8 // The data is not contained in the program file.
33 #define SHT_REL 0x9
34 #define SHT_DYNSYM 0xb
35 #define SHT_INIT_ARRAY 0xe
36 #define SHT_FINI_ARRAY 0xf
37 #define SHT_PREINIT_ARRAY 0x10
38 
39 #define SHF_WRITE 0x1
40 #define SHF_ALLOC 0x2
41 #define SHF_EXECINSTR 0x4
42 #define SHF_MASKPROC 0xf0000000
43 
44 #define ELF32_R_SYM(val) ((val) >> 8)
45 #define ELF32_R_TYPE(val) ((val) &0xff)
46 
47 #define ELF32_ST_BIND(i) ((i) >> 4)
48 #define ELF32_ST_TYPE(i) ((i) &0xf)
49 #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) &0xf))
50 #define PACKED __attribute__((packed))
51 
52 class BootstrapInfo;
53 
61 class Elf32
62 {
63  public:
69  Elf32(const char *name);
70 
74  ~Elf32();
75 
80  bool load(uint8_t *pBuffer, unsigned int nBufferLength);
81 
85  bool load(BootstrapInfo *pBootstrap);
86 
91  bool writeSections();
92 
97  unsigned int getLastAddress();
98 
106  const char *lookupSymbol(unsigned int addr, unsigned int *startAddr = 0);
107 
112  uint32_t lookupDynamicSymbolAddress(uint32_t off);
113 
118  char *lookupDynamicSymbolName(uint32_t off);
119 
124  uint32_t getGlobalOffsetTable();
125 
129  uint32_t getEntryPoint();
130 
131  // private:
134  Elf32(const Elf32 &);
137  Elf32 &operator=(const Elf32 &);
138 
140  {
141  uint8_t ident[16];
142  uint16_t type;
143  uint16_t machine;
144  uint32_t version;
145  uint32_t entry;
146  uint32_t phoff;
147  uint32_t shoff;
148  uint32_t flags;
149  uint16_t ehsize;
150  uint16_t phentsize;
151  uint16_t phnum;
152  uint16_t shentsize;
153  uint16_t shnum;
154  uint16_t shstrndx;
155  } PACKED;
156 
158  {
159  uint32_t type;
160  uint32_t offset;
161  uint32_t vaddr;
162  uint32_t paddr;
163  uint32_t filesz;
164  uint32_t memsz;
165  uint32_t flags;
166  uint32_t align;
167  } PACKED;
168 
170  {
171  uint32_t name;
172  uint32_t type;
173  uint32_t flags;
174  uint32_t addr;
175  uint32_t offset;
176  uint32_t size;
177  uint32_t link;
178  uint32_t info;
179  uint32_t addralign;
180  uint32_t entsize;
181  } PACKED;
182 
184  {
185  uint32_t name;
186  uint32_t value;
187  uint32_t size;
188  uint8_t info;
189  uint8_t other;
190  uint16_t shndx;
191  } PACKED;
192 
193  struct Elf32Dyn_t
194  {
195  int32_t tag;
196  union
197  {
198  int32_t val;
199  uint32_t ptr;
200  } un;
201  } PACKED;
202 
203  struct Elf32Rel_t
204  {
205  uint32_t offset;
206  uint32_t info;
207  } PACKED;
208 
209  Elf32Header_t *m_pHeader;
210  Elf32SectionHeader_t *m_pSymbolTable;
211  Elf32SectionHeader_t *m_pStringTable;
212  Elf32SectionHeader_t *m_pShstrtab;
213  Elf32SectionHeader_t *m_pGotTable; // Global offset table.
214  Elf32SectionHeader_t *m_pRelTable;
215  Elf32SectionHeader_t *m_pSectionHeaders;
216  char m_pId[128];
217  uint8_t *m_pBuffer;
218 };
219 
220 #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()