1 /*---------------------------------------------------------------------------* 2 Project: TwlSDK - ELF Loader 3 File: elf_loader.h 4 5 Copyright 2006-2008 Nintendo. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Date:: 2008-09-17#$ 14 $Rev: 8556 $ 15 $Author: okubata_ryoma $ 16 *---------------------------------------------------------------------------*/ 17 #ifndef _ELF_LOADER_H_ 18 #define _ELF_LOADER_H_ 19 20 #include <stdio.h> 21 #include "types.h" 22 #include "elf.h" 23 24 25 26 /*------------------------------------------------------ 27 Special section (table that supports section numbers before and after stripdebug) 28 -----------------------------------------------------*/ 29 #define SPECIAL_SECTION_ENABLE (1) 30 #define SPECIAL_SECTION_TYPE (SHT_LOUSER) 31 #define SPECIAL_SECTION_NAME ".section_table" 32 33 34 35 /*------------------------------------------------------ 36 Extended section header (support for load addresses and so on) 37 -----------------------------------------------------*/ 38 typedef struct { 39 void* next; 40 u16 index; 41 u16 debug_flag; /*0: Not debugging information. 1: Debugging information*/ 42 u32 loaded_adr; 43 u32 alloc_adr; 44 u32 loaded_size; 45 Elf32_Shdr Shdr; 46 char* str; /*A copy of the section name string*/ 47 u32* sym_table; /*Table that is valid for a symbol section, containing old and new values*/ 48 void* str_table; /*New string table that is valid for a STR section*/ 49 u32 str_table_size; /*Size of the new string table that is valid for a STR section*/ 50 }ELShdrEx; 51 52 53 /*------------------------------------------------------ 54 Extended symbol entry (support for load addresses and so on) 55 -----------------------------------------------------*/ 56 typedef struct { 57 void* next; 58 u16 debug_flag; /*0: Not debugging information. 1: Debugging information*/ 59 u16 thumb_flag; 60 u32 relocation_val; 61 Elf32_Sym Sym; 62 }ELSymEx; 63 64 65 66 /*------------------------------------------------------ 67 ELF object management 68 -----------------------------------------------------*/ 69 typedef void (*ELi_ReadFunc)( void* buf, void* file_struct, u32 file_base, u32 file_offset, u32 size); 70 typedef struct { 71 void* ar_head; /* Starting address for the AR or ELF file */ 72 void* elf_offset; /* Offset to the start of the ELF object */ 73 void* buf_current; /* For Loader operations */ 74 u16 shentsize; /* Size of a section header */ 75 u16 process; /* Relocatable state */ 76 77 ELShdrEx* ShdrEx; /* Start of the ShdrEx list */ 78 79 Elf32_Ehdr CurrentEhdr; /* ELF header */ 80 81 Elf32_Rel Rel; /* Relocation entry */ 82 Elf32_Rela Rela; 83 Elf32_Sym Sym; /* Symbol entry */ 84 Elf32_Shdr SymShdr; 85 86 ELSymEx* SymEx; /* Beginning of the SymEx list */ 87 ELSymEx** SymExTbl; /* SymEx address table (for all symbols)*/ 88 u32 SymExTarget; /* Number of the symbol section that created the SymEx list */ 89 90 ELi_ReadFunc ELi_ReadStub; /* Read stub function */ 91 void* FileStruct; /* File structure */ 92 93 u32 mem_adr; /*This will contain the sh_addr of the section that was loaded first (parameters for DS ROM headers)*/ 94 u32 newelf_size; 95 }ELHandle; 96 97 98 99 /*------------------------------------------------------ 100 Address table 101 -----------------------------------------------------*/ 102 typedef struct { 103 void* next; /*Next address entry*/ 104 char* name; /*String*/ 105 void* adr; /*Address*/ 106 u16 func_flag; /*0: Data. 1: Function.*/ 107 u16 thumb_flag; /*0: ARM code. 1: Thumb code.*/ 108 }ELAdrEntry; 109 110 111 /*------------------------------------------------------ 112 Unresolved relocation data table 113 114 If the address table is accessed later, addresses will be resolved as follows. 115 S_ = AdrEntry.adr; 116 T_ = (u32)(AdrEntry.thumb_flag); 117 -----------------------------------------------------*/ 118 typedef struct { 119 void* next; /*Next entry*/ 120 char* sym_str; /*Name of an unresolved external symbol reference*/ 121 u32 r_type; /*Relocation type ( ELF32_R_TYPE(Rela.r_info) )*/ 122 u32 S_; /*Address of an unresolved external symbol reference*/ 123 s32 A_; /*Resolved*/ 124 u32 P_; /*Resolved*/ 125 u32 T_; /*ARM or Thumb flag for an unresolved external symbol reference*/ 126 u32 sh_type; /*SHT_REL or SHT_RELA*/ 127 u32 remove_flag; /*Flag to set on resolution (identifies what may be deleted)*/ 128 ELAdrEntry* AdrEnt; /*Location of the entry found in the address table*/ 129 }ELUnresolvedEntry; 130 131 132 133 /* Process value for ELHandle */ 134 #define EL_FAILED 0x00 135 #define EL_INITIALIZED 0x5A 136 #define EL_COPIED 0xF0 137 #define EL_RELOCATED 0xF1 138 139 140 141 /*--------------------------------------------------------- 142 Finds the size of an ELF object 143 --------------------------------------------------------*/ 144 u32 EL_GetElfSize( const void* buf); 145 146 /*------------------------------------------------------ 147 Initializes the dynamic link system 148 -----------------------------------------------------*/ 149 void EL_Init( void); 150 151 /*------------------------------------------------------ 152 Initializes an ELHandle structure. 153 -----------------------------------------------------*/ 154 BOOL EL_InitHandle( ELHandle* ElfHandle); 155 156 /*------------------------------------------------------ 157 Relocates an ELF object or its archive from a file into a buffer 158 -----------------------------------------------------*/ 159 u16 EL_LoadLibraryfromFile( ELHandle* ElfHandle, FILE* ObjFile, void* buf, u16 type); 160 161 /*------------------------------------------------------ 162 Relocates an ELF object or its archive from memory into a buffer 163 -----------------------------------------------------*/ 164 u16 EL_LoadLibraryfromMem( ELHandle* ElfHandle, void* obj_image, u32 obj_len, void* buf); 165 166 /*------------------------------------------------------ 167 Uses the address table to resolve unresolved symbols 168 -----------------------------------------------------*/ 169 u16 EL_ResolveAllLibrary( void); 170 171 172 /*------------------------------------------------------ 173 Writes out marked symbols as structures to a public file 174 -----------------------------------------------------*/ 175 u16 EL_ExtractStaticSym1( void); 176 /*------------------------------------------------------ 177 Writes out marked symbols as functions to a public file 178 -----------------------------------------------------*/ 179 u16 EL_ExtractStaticSym2( void); 180 181 182 /*------------------------------------------------------ 183 Removes an entry from the address table 184 -----------------------------------------------------*/ 185 BOOL EL_RemoveAdrEntry( ELAdrEntry* AdrEnt); 186 187 /*------------------------------------------------------ 188 Adds an entry to the address table 189 -----------------------------------------------------*/ 190 void EL_AddAdrEntry( ELAdrEntry* AdrEnt); 191 192 /*------------------------------------------------------ 193 Finds the entry corresponding to the specified string from the address table 194 -----------------------------------------------------*/ 195 ELAdrEntry* EL_GetAdrEntry( char* ent_name); 196 197 /*------------------------------------------------------ 198 Returns the address corresponding to the specified string from the address table 199 -----------------------------------------------------*/ 200 void* EL_GetGlobalAdr( char* ent_name); 201 202 203 204 205 206 /*Other functions that are likely to be required*/ 207 //Function that calculates the number of bytes in memory required for loading 208 //EL_FreeLibrary 209 210 #endif /*_ELF_LOADER_H_*/ 211