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