20 #ifndef KERNEL_LINKER_ELF_H 21 #define KERNEL_LINKER_ELF_H 23 #ifdef IN_PEDIGREE_KERNEL 24 #include "pedigree/kernel/compiler.h" 25 #include "pedigree/kernel/processor/types.h" 26 #include "pedigree/kernel/utilities/String.h" 27 #include "pedigree/kernel/utilities/new" 30 #include "pedigree/kernel/linker/SymbolTable.h" 31 #include "pedigree/kernel/utilities/List.h" 50 #define SHT_PROGBITS 0x1 // The data is contained in the program file. 51 #define SHT_SYMTAB 0x2 // Symbol table 52 #define SHT_STRTAB 0x3 // String table 54 #define SHT_HASH 0x5 // Symbol hash table 55 #define SHT_DYNAMIC 0x6 // Dynamic linking information 57 #define SHT_NOBITS 0x8 // The data is not contained in the program file. 59 #define SHT_DYNSYM 0xb 60 #define SHT_INIT_ARRAY 0xe 61 #define SHT_FINI_ARRAY 0xf 62 #define SHT_PREINIT_ARRAY 0x10 67 #define SHF_EXECINSTR 0x4 68 #define SHF_MASKPROC 0xf0000000 103 #define DT_SYMBOLIC 16 109 #define DT_TEXTREL 22 111 #define DT_BIND_NOW 24 112 #define DT_INIT_ARRAY 25 113 #define DT_FINI_ARRAY 26 114 #define DT_INIT_ARRAYSZ 27 115 #define DT_FINI_ARRAYSZ 28 116 #define DT_RUNPATH 29 118 #define DT_ENCODING 32 119 #define DT_PREINIT_ARRAY 32 120 #define DT_PREINIT_ARRAYSZ 33 126 #define STT_SECTION 3 137 #define STV_DEFAULT 0 138 #define STV_INTERNAL 1 140 #define STV_PROTECTED 3 144 #define R_SYM(val) ((val) >> 8) 145 #define R_TYPE(val) ((val) &0xff) 147 #define ST_BIND(i) ((i) >> 4) 148 #define ST_TYPE(i) ((i) &0xf) 149 #define ST_INFO(b, t) (((b) << 4) + ((t) &0xf)) 151 typedef uint32_t Elf_Addr;
152 typedef uint32_t Elf_Off;
153 typedef uint16_t Elf_Half;
154 typedef uint32_t Elf_Word;
155 typedef int32_t Elf_Sword;
159 typedef uint32_t Elf_Xword;
160 typedef int32_t Elf_Sxword;
165 #define R_SYM(val) ((val) >> 32) 166 #define R_TYPE(val) ((val) &0xffffffffUL) 168 #define ST_BIND(i) ((i) >> 4) 169 #define ST_TYPE(i) ((i) &0xf) 170 #define ST_INFO(b, t) (((b) << 4) + ((t) &0xf)) 172 typedef uint64_t Elf_Addr;
173 typedef uint64_t Elf_Off;
174 typedef uint16_t Elf_Half;
175 typedef uint32_t Elf_Word;
176 typedef int32_t Elf_Sword;
177 typedef uint64_t Elf_Xword;
178 typedef int64_t Elf_Sxword;
181 typedef uint32_t Elf32_Addr;
182 typedef uint32_t Elf32_Off;
183 typedef uint16_t Elf32_Half;
184 typedef uint32_t Elf32_Word;
185 typedef int32_t Elf32_Sword;
186 typedef uint32_t Elf32_Xword;
187 typedef int32_t Elf32_Sxword;
192 #define ST_TYPEOK(x) (ST_TYPE((x)) <= STT_FUNC) 194 #ifndef _NO_ELF_CLASS 222 bool validate(uint8_t *pBuffer,
size_t length);
226 bool create(uint8_t *pBuffer,
size_t length);
230 bool createNeededOnly(uint8_t *pBuffer,
size_t length);
236 uint8_t *pBuffer,
size_t length, uintptr_t &loadBase,
size_t &loadSize,
244 bool finaliseModule(uint8_t *pBuffer, uintptr_t length);
251 uint8_t *pBuffer,
size_t length, uintptr_t &loadBase,
252 SymbolTable *pSymtab = 0,
bool bAllocate =
true,
size_t *pSize = 0);
262 uint8_t *pBuffer,
size_t length, uintptr_t loadBase,
263 SymbolTable *pSymtab = 0, uintptr_t nStart = 0, uintptr_t nEnd = ~0,
264 bool relocate =
true);
268 extractEntryPoint(uint8_t *pBuffer,
size_t length, uintptr_t &entry);
271 static bool extractInformation(
272 uint8_t *pBuffer,
size_t length,
size_t &phdrCount,
273 size_t &phdrEntrySize, uintptr_t &phdrAddress);
284 uintptr_t getLastAddress();
286 uintptr_t getInitFunc()
290 uintptr_t getFiniFunc()
300 template <
class T = ElfSymbol_t>
302 lookupSymbol(uintptr_t addr, uintptr_t *startAddr, T *symbolTable);
306 const char *lookupSymbol(uintptr_t addr, uintptr_t *startAddr);
309 uintptr_t lookupSymbol(
const char *pName);
313 uintptr_t lookupDynamicSymbolAddress(
const char *str, uintptr_t loadBase);
317 uintptr_t applySpecificRelocation(
318 uintptr_t off,
SymbolTable *pSymtab, uintptr_t loadBase,
323 uintptr_t getGlobalOffsetTable();
333 void populateSymbolTable(
SymbolTable *pSymtab, uintptr_t loadBase);
336 void preallocateSymbols(
342 return &m_SymbolTable;
346 uintptr_t getEntryPoint();
348 uintptr_t debugFrameTable();
349 uintptr_t debugFrameTableLength();
425 Elf32_Xword addralign;
494 #ifndef _NO_ELF_CLASS 496 template <
typename T>
499 bool relocate(uint8_t *pBuffer, uintptr_t length);
500 bool relocateModinfo(uint8_t *pBuffer, uintptr_t length);
510 bool applyRelocation(
512 uintptr_t loadBase = 0,
523 bool applyRelocation(
525 uintptr_t loadBase = 0,
529 void rebaseDynamic();
533 size_t m_nSymbolTableSize;
534 char *m_pStringTable;
535 size_t m_nStringTableSize;
537 size_t m_nShstrtabSize;
538 uintptr_t *m_pGotTable;
541 size_t m_nRelTableSize;
542 size_t m_nRelaTableSize;
546 uint32_t *m_pDebugTable;
547 size_t m_nDebugTableSize;
549 size_t m_nDynamicSymbolTableSize;
550 char *m_pDynamicStringTable;
551 size_t m_nDynamicStringTableSize;
553 size_t m_nSectionHeaders;
555 size_t m_nProgramHeaders;
560 uintptr_t m_InitFunc;
561 uintptr_t m_FiniFunc;
565 uintptr_t m_LoadBase;
570 Elf &operator=(
const Elf &);
574 extern template const char *Elf::lookupSymbol<Elf::ElfSymbol_t>(
575 uintptr_t addr, uintptr_t *startAddr = 0,
ElfSymbol_t *symbolTable = 0);
577 extern template const char *Elf::lookupSymbol<Elf::Elf32Symbol_t>(
578 uintptr_t addr, uintptr_t *startAddr = 0,
Elf32Symbol_t *symbolTable = 0);
const String & getName() const
void setName(const String &s)