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-12-10#$
14   $Rev: 9620 $
15   $Author: shirait $
16  *---------------------------------------------------------------------------*/
17 #ifndef _ELF_LOADER_H_
18 #define _ELF_LOADER_H_
19 
20 
21 
22 
23 #ifdef SDK_TWL
24 #include <twl.h>
25 #else
26 #include <nitro.h>
27 #endif
28 //#include <stdio.h>
29 #include "elf.h"
30 #include <twl/el.h>
31 
32 
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
39 /*------------------------------------------------------
40   Extended section header (support for load addresses and so on)
41  -----------------------------------------------------*/
42 typedef struct {
43   void*         next;
44   u16           index;
45   u16           debug_flag;    /*0: Not debugging information. 1: Debugging information*/
46   u32           loaded_adr;
47   u32           alloc_adr;
48   u32           loaded_size;
49   Elf32_Shdr    Shdr;
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   Linked list of veneers (used by ELi_DoRelocate)
67  -----------------------------------------------------*/
68 typedef struct {
69   void* next;
70   u32   adr;     /* Starting address of veneer code */
71   u32   data;    /* Jump destination address stored in a veneer's literal pool */
72 }ELVeneer;
73 
74 
75 /*------------------------------------------------------
76   Table with relocation data to use for importing
77 
78   If the address table is accessed later, addresses will be resolved as follows.
79   S_ = AdrEntry.adr;
80   T_ = (u32)(AdrEntry.thumb_flag);
81  -----------------------------------------------------*/
82 typedef struct {
83   void*       next;             /*Next entry*/
84   char*       sym_str;          /*Name of an unresolved external symbol reference*/
85   u32         r_type;           /*Relocation type ( ELF32_R_TYPE(Rela.r_info) )*/
86   u32         S_;               /*Address of an unresolved external symbol reference*/
87   s32         A_;               /*Resolved*/
88   u32         P_;               /*Resolved*/
89   u32         T_;               /*ARM or Thumb flag for an unresolved external symbol reference*/
90   u32         sh_type;          /*SHT_REL or SHT_RELA*/
91   struct ELObject*   Dlld;      /*Object with the imported entry when it is resolved. NULL when it is unresolved*/
92 }ELImportEntry;
93 
94 
95 
96 /* Process value for ELDesc */
97 typedef enum ELProcess {
98   EL_PROC_NOTHING      = 0,
99   EL_PROC_INITIALIZED = 0x5A,
100   EL_PROC_COPIED      = 0xF0,
101   EL_PROC_RELOCATED
102 } ELProcess;
103 
104 
105 /*------------------------------------------------------
106   Individual ELF object management
107  -----------------------------------------------------*/
108 typedef struct {
109   void*          next;                   /* Next object entry */
110   void*          lib_start;              /* Address relocated as a library */
111   u32            lib_size;               /* Size after being made into a library */
112 //  u32            lib_size_limit;         /* Size of the buffer given to use for relocation */
113   void*          buf_current;            /* Buffer pointer */
114   ELAdrEntry*    ExportAdrEnt;           /* Export information */
115   ELAdrEntry*    HiddenAdrEnt;           /* Location to store export information when unlinking */
116   ELImportEntry* ResolvedImportAdrEnt;   /* Resolved import information */
117   ELImportEntry* UnresolvedImportAdrEnt; /* Unresolved import information */
118   ELVeneer*      ELVenEntStart;          /* Linked list of veneers */
119   ELVeneer*      ELV4tVenEntStart;       /* Linked list of v4t veneers */
120   u32            stat;                   /* Status */
121   u32            file_id;                /* File ID when a DLL was placed through a ROM */
122   u32            process;                /* Casted and saved ELProcess type */
123   u32            result;                 /* Casted and saved ELResult type */
124 }ELObject;
125 
126 
127 /*------------------------------------------------------
128   Overall ELF object management
129  -----------------------------------------------------*/
130 typedef BOOL (*ELi_ReadFunc)( void* buf, void* file_struct, u32 file_base, u32 file_offset, u32 size);
131 typedef struct {
132   void*         ar_head;        /* Starting address for the AR or ELF file */
133   void*         elf_offset;     /* Offset to the start of the ELF object */
134 
135   u16           reserve;
136   u16           shentsize;      /* Size of a section header */
137   u32           process;        /* Cast the ELProcess type and save */
138   u32           result;         /* Cast the ELResult type and save */
139 
140   ELShdrEx*     ShdrEx;         /* Start of the ShdrEx list */
141 
142   Elf32_Ehdr    CurrentEhdr;    /* ELF header */
143 
144   Elf32_Rel     Rel;            /* Relocation entry */
145   Elf32_Rela    Rela;
146   Elf32_Sym     Sym;            /* Symbol entry */
147   Elf32_Shdr    SymShdr;
148 
149   ELSymEx*      SymEx;          /* Start of the SymEx list (connect only non-debugging symbols) */
150   ELSymEx**     SymExTbl;       /* SymEx address table (for all symbols)*/
151   u32           SymExTarget;    /* Number of the symbol section that created the SymEx list */
152 
153   ELi_ReadFunc  i_elReadStub;   /* Read stub function */
154   void*         FileStruct;     /* File structure */
155 
156   u32           entry_adr;      /* Entry address */
157 
158   ELObject*     ELObjectStart;  /* Linked list of objects */
159   ELObject*     ELStaticObj;    /* Pointer to the static structure connected to the linked list */
160 }ELDesc;
161 
162 
163 /*---------------------------------------------------------
164  Finds the size of an ELF object
165  --------------------------------------------------------*/
166 u32 EL_GetElfSize( const void* buf);
167 
168 /*---------------------------------------------------------
169  Finds the size of a linked library
170  --------------------------------------------------------*/
171 u32 EL_GetLibSize( ELDlld my_dlld);
172 
173 
174 /*------------------------------------------------------
175   Initializes the dynamic link system
176  -----------------------------------------------------*/
177 #if 0
178 //#ifndef SDK_TWL
179 void ELi_Init( void);
180 #else
181 void ELi_Init( ELAlloc alloc, ELFree free);
182 void* ELi_Malloc( ELDesc* elElfDesc, ELObject* MYObject, size_t size);
183 #endif
184 
185 /*------------------------------------------------------
186   Initializes an ELDesc structure
187  -----------------------------------------------------*/
188 BOOL ELi_InitDesc( ELDesc* elElfDesc);
189 
190 /*------------------------------------------------------
191   Relocates an ELF object or its archive from a file into a buffer
192  -----------------------------------------------------*/
193 ELDlld EL_LoadLibraryfromFile( ELDesc* elElfDesc, const char* FilePath, void* buf);
194 
195 /*------------------------------------------------------
196   Relocates an ELF object or its archive through the user read API
197  -----------------------------------------------------*/
198 ELDlld EL_LoadLibrary( ELDesc* elElfDesc, ELReadImage readfunc, u32 len, void* buf);
199 
200 /*------------------------------------------------------
201   Relocates an ELF object or its archive from memory into a buffer
202  -----------------------------------------------------*/
203 ELDlld EL_LoadLibraryfromMem( ELDesc* elElfDesc, void* obj_image, u32 obj_len, void* buf);
204 
205 /*------------------------------------------------------
206   Uses the address table to resolve unresolved symbols
207  -----------------------------------------------------*/
208 ELProcess ELi_ResolveAllLibrary( ELDesc* elElfDesc);
209 
210 
211 /*------------------------------------------------------
212   Adds an entry to the address table from an application
213  -----------------------------------------------------*/
214 BOOL ELi_Export( ELDesc* elElfDesc, ELAdrEntry* AdrEnt);
215 
216 /*------------------------------------------------------
217   Adds a static entry to the address table
218   (This is defined as a weak symbol in the ELF library, and will be overwritten by the definition in files created by makelst)
219 
220  -----------------------------------------------------*/
221 void EL_AddStaticSym( void);
222 
223 
224 /*------------------------------------------------------
225   Returns the address corresponding to the specified string from the address table
226  -----------------------------------------------------*/
227 void* ELi_GetGlobalAdr( ELDesc* elElfDesc, ELDlld my_dlld, const char* ent_name);
228 
229 
230 /*------------------------------------------------------
231   Unlinks an object
232  -----------------------------------------------------*/
233 BOOL ELi_Unlink( ELDesc* elElfDesc, ELDlld my_dlld);
234 
235 
236 /*------------------------------------------------------
237   Checks whether an object still has unresolved references
238  -----------------------------------------------------*/
239 BOOL EL_IsResolved( ELDlld my_dlld);
240 
241 
242 /*------------------------------------------------------
243   Sets error codes and process codes
244  -----------------------------------------------------*/
245 void ELi_SetResultCode( ELDesc* elElfDesc, ELObject* MYObject, ELResult result);
246 void ELi_SetProcCode( ELDesc* elElfDesc, ELObject* MYObject, ELProcess process);
247 
248 
249 /*------------------------------------------------------
250   Allocates the heap
251  -----------------------------------------------------*/
252 void* ELi_Malloc( ELDesc* elElfDesc, ELObject* MYObject, size_t size);
253 
254 
255 #ifdef __cplusplus
256 }    /* extern "C" */
257 #endif
258 
259 
260 #endif    /*_ELF_LOADER_H_*/
261