b399f49b9330c4ee4880e108292980cee23509aa
[tpg/acess2.git] / elf.c
1 /*\r
2  * AcessOS 1 - Dynamic Loader\r
3  * By thePowersGang\r
4  */\r
5 #include "common.h"\r
6 #include "elf32.h"\r
7
8 #define DEBUG   0
9
10 #if DEBUG\r
11 # define        DEBUGS(v...)    SysDebug(v)\r
12 #else
13 # define        DEBUGS(...)     
14 #endif
15 \r
16 // === CONSTANTS ===
17 #if DEBUG\r
18 //static const char     *csaDT_NAMES[] = {"DT_NULL", "DT_NEEDED", "DT_PLTRELSZ", "DT_PLTGOT", "DT_HASH", "DT_STRTAB", "DT_SYMTAB", "DT_RELA", "DT_RELASZ", "DT_RELAENT", "DT_STRSZ", "DT_SYMENT", "DT_INIT", "DT_FINI", "DT_SONAME", "DT_RPATH", "DT_SYMBOLIC", "DT_REL", "DT_RELSZ", "DT_RELENT", "DT_PLTREL", "DT_DEBUG", "DT_TEXTREL", "DT_JMPREL"};\r
19 static const char       *csaR_NAMES[] = {"R_386_NONE", "R_386_32", "R_386_PC32", "R_386_GOT32", "R_386_PLT32", "R_386_COPY", "R_386_GLOB_DAT", "R_386_JMP_SLOT", "R_386_RELATIVE", "R_386_GOTOFF", "R_386_GOTPC", "R_386_LAST"};
20 #endif\r
21 \r
22 // === PROTOTYPES ===\r
23 void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base);\r
24 Uint ElfHashString(char *name);\r
25 \r
26 // === CODE ===\r
27 /**\r
28  \fn int ElfRelocate(void *Base, char **envp, char *Filename)\r
29  \brief Relocates a loaded ELF Executable\r
30 */\r
31 int ElfRelocate(void *Base, char **envp, char *Filename)\r
32 {\r
33         Elf32_Ehdr      *hdr = Base;\r
34         Elf32_Phdr      *phtab;\r
35          int    i, j;   // Counters\r
36         char    *libPath;\r
37         Uint    iRealBase = -1;\r
38         Uint    iBaseDiff;\r
39          int    iSegmentCount;\r
40          int    iSymCount;\r
41         Elf32_Rel       *rel = NULL;\r
42         Elf32_Rela      *rela = NULL;\r
43         Uint32  *pltgot = NULL;\r
44         void    *plt = NULL;\r
45          int    relSz=0, relEntSz=8;\r
46          int    relaSz=0, relaEntSz=8;\r
47          int    pltSz=0, pltType=0;\r
48         Elf32_Dyn       *dynamicTab = NULL;     // Dynamic Table Pointer\r
49         char    *dynstrtab = NULL;      // .dynamic String Table\r
50         Elf32_Sym       *dynsymtab;\r
51         \r
52         DEBUGS("ElfRelocate: (Base=0x%x)\n", Base);\r
53         \r
54         // Parse Program Header to get Dynamic Table\r
55         phtab = Base + hdr->phoff;\r
56         iSegmentCount = hdr->phentcount;\r
57         for(i=0;i<iSegmentCount;i++)\r
58         {\r
59                 // Determine linked base address\r
60                 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)\r
61                         iRealBase = phtab[i].VAddr;\r
62                 \r
63                 // Find Dynamic Section\r
64                 if(phtab[i].Type == PT_DYNAMIC) {\r
65                         if(dynamicTab) {\r
66                                 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n");\r
67                                 continue;\r
68                         }\r
69                         dynamicTab = (void *) phtab[i].VAddr;\r
70                         j = i;  // Save Dynamic Table ID\r
71                 }\r
72         }\r
73         \r
74         // Page Align real base\r
75         iRealBase &= ~0xFFF;\r
76         DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x\n", Base, iRealBase);\r
77         \r
78         // Adjust "Real" Base\r
79         iBaseDiff = (Uint)Base - iRealBase;\r
80         \r
81         hdr->entrypoint += iBaseDiff;   // Adjust Entrypoint\r
82         \r
83         // Check if a PT_DYNAMIC segement was found\r
84         if(!dynamicTab) {\r
85                 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n");\r
86                 return hdr->entrypoint;\r
87         }\r
88         \r
89         // Adjust Dynamic Table\r
90         dynamicTab = (void *) ((Uint)dynamicTab + iBaseDiff);\r
91         
92         // === Get Symbol table and String Table ===\r
93         for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)\r
94         {
95                 switch(dynamicTab[j].d_tag)
96                 {
97                 // --- Symbol Table ---\r
98                 case DT_SYMTAB:\r
99                         DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)\n",\r
100                                 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);\r
101                         dynamicTab[j].d_val += iBaseDiff;\r
102                         dynsymtab = (void*)(dynamicTab[j].d_val);\r
103                         hdr->misc.SymTable = dynamicTab[j].d_val;       // Saved in unused bytes of ident\r
104                         break;\r
105                 // --- String Table ---\r
106                 case DT_STRTAB:\r
107                         DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)\n",\r
108                                 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);\r
109                         dynamicTab[j].d_val += iBaseDiff;\r
110                         dynstrtab = (void*)(dynamicTab[j].d_val);\r
111                         break;\r
112                 // --- Hash Table --\r
113                 case DT_HASH:\r
114                         dynamicTab[j].d_val += iBaseDiff;\r
115                         iSymCount = ((Uint*)(dynamicTab[j].d_val))[1];\r
116                         hdr->misc.HashTable = dynamicTab[j].d_val;      // Saved in unused bytes of ident\r
117                         break;
118                 }
119         }
120 \r
121         if(dynsymtab == NULL) {\r
122                 SysDebug("WARNING: No Dynamic Symbol table, returning\n");\r
123                 return hdr->entrypoint;\r
124         }\r
125         \r
126         // Alter Symbols to true base\r
127         for(i=0;i<iSymCount;i++)\r
128         {\r
129                 dynsymtab[i].value += iBaseDiff;
130                 dynsymtab[i].nameOfs += (Uint)dynstrtab;\r
131                 //DEBUGS("elf_relocate: Sym '%s' = 0x%x (relocated)\n", dynsymtab[i].name, dynsymtab[i].value);\r
132         }\r
133         
134         // === Add to loaded list (can be imported now) ===
135         AddLoaded( Filename, (Uint)Base );
136
137         // === Parse Relocation Data ===\r
138         DEBUGS(" elf_relocate: dynamicTab = 0x%x\n", dynamicTab);\r
139         for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)\r
140         {\r
141                 switch(dynamicTab[j].d_tag)\r
142                 {\r
143                 // --- Shared Library Name ---\r
144                 case DT_SONAME:\r
145                         DEBUGS(" elf_relocate: .so Name '%s'\n", dynstrtab+dynamicTab[j].d_val);\r
146                         break;\r
147                 // --- Needed Library ---\r
148                 case DT_NEEDED:\r
149                         libPath = dynstrtab + dynamicTab[j].d_val;\r
150                         DEBUGS(" Required Library '%s'\n", libPath);\r
151                         if(LoadLibrary(libPath, NULL, envp) == 0) {\r
152                                 #if DEBUG\r
153                                         DEBUGS(" elf_relocate: Unable to load '%s'\n", libPath);\r
154                                 #else\r
155                                 SysDebug("Unable to load required library '%s'\n", libPath);\r
156                                 #endif\r
157                                 return 0;\r
158                         }\r
159                         break;\r
160                 // --- PLT/GOT ---\r
161                 case DT_PLTGOT: pltgot = (void*)iBaseDiff+(dynamicTab[j].d_val);        break;\r
162                 case DT_JMPREL: plt = (void*)(iBaseDiff+dynamicTab[j].d_val);   break;\r
163                 case DT_PLTREL: pltType = dynamicTab[j].d_val;  break;\r
164                 case DT_PLTRELSZ:       pltSz = dynamicTab[j].d_val;    break;\r
165                 \r
166                 // --- Relocation ---\r
167                 case DT_REL:    rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;\r
168                 case DT_RELSZ:  relSz = dynamicTab[j].d_val;    break;\r
169                 case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;\r
170                 case DT_RELA:   rela = (void*)(iBaseDiff + dynamicTab[j].d_val);        break;\r
171                 case DT_RELASZ: relaSz = dynamicTab[j].d_val;   break;\r
172                 case DT_RELAENT:        relaEntSz = dynamicTab[j].d_val;        break;\r
173                 \r
174                 // --- Symbol Table ---\r
175                 case DT_SYMTAB:\r
176                 // --- Hash Table ---\r
177                 case DT_HASH:\r
178                 // --- String Table ---\r
179                 case DT_STRTAB:\r
180                         break;\r
181                 \r
182                 // --- Unknown ---\r
183                 default:\r
184                         if(dynamicTab[j].d_tag > DT_JMPREL)     continue;\r
185                         //DEBUGS(" elf_relocate: %i-%i = %s,0x%x\n",\r
186                         //      i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);\r
187                         break;\r
188                 }\r
189         }\r
190         \r
191         DEBUGS(" elf_relocate: Beginning Relocation\n");\r
192         \r
193         // Parse Relocation Entries\r
194         if(rel && relSz)\r
195         {\r
196                 Uint32  *ptr;\r
197                 DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x\n", rel, relSz, relEntSz);\r
198                 j = relSz / relEntSz;\r
199                 for( i = 0; i < j; i++ )\r
200                 {
201                         //DEBUGS("  Rel %i: 0x%x+0x%x\n", i, iBaseDiff, rel[i].r_offset);\r
202                         ptr = (void*)(iBaseDiff + rel[i].r_offset);\r
203                         elf_doRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, iBaseDiff);\r
204                 }\r
205         }\r
206         // Parse Relocation Entries\r
207         if(rela && relaSz)\r
208         {\r
209                 Uint32  *ptr;\r
210                 DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x\n", rela, relaSz, relaEntSz);\r
211                 j = relaSz / relaEntSz;\r
212                 for( i = 0; i < j; i++ )\r
213                 {\r
214                         ptr = (void*)(iBaseDiff + rela[i].r_offset);\r
215                         elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, iBaseDiff);\r
216                 }\r
217         }\r
218         \r
219         // === Process PLT (Procedure Linkage Table) ===\r
220         if(plt && pltSz)\r
221         {\r
222                 Uint32  *ptr;\r
223                 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x\n", plt);\r
224                 if(pltType == DT_REL)\r
225                 {\r
226                         Elf32_Rel       *pltRel = plt;\r
227                         j = pltSz / sizeof(Elf32_Rel);\r
228                         DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries\n", j);\r
229                         for(i=0;i<j;i++)\r
230                         {\r
231                                 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);\r
232                                 elf_doRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, iRealBase);\r
233                         }\r
234                 }\r
235                 else\r
236                 {\r
237                         Elf32_Rela      *pltRela = plt;\r
238                         j = pltSz / sizeof(Elf32_Rela);\r
239                         DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries\n", j);\r
240                         for(i=0;i<j;i++)\r
241                         {\r
242                                 ptr = (void*)(iRealBase + pltRela[i].r_offset);\r
243                                 elf_doRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend, dynsymtab, iRealBase);\r
244                         }\r
245                 }\r
246         }\r
247         \r
248         DEBUGS("ElfRelocate: RETURN 0x%x", hdr->entrypoint);\r
249         return hdr->entrypoint;\r
250 }\r
251 \r
252 void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base)\r
253 {\r
254          int    type = ELF32_R_TYPE(r_info);\r
255          int    sym = ELF32_R_SYM(r_info);\r
256         Uint32  val;\r
257         switch( type )\r
258         {\r
259         // Standard 32 Bit Relocation (S+A)\r
260         case R_386_32:\r
261                 val = GetSymbol( symtab[sym].name );\r
262                 DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')\n",\r
263                         ptr, val, symtab[sym].name);\r
264                 *ptr = val + addend;\r
265                 break;\r
266                 \r
267         // 32 Bit Relocation wrt. Offset (S+A-P)\r
268         case R_386_PC32:\r
269                 DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name);\r
270                 val = GetSymbol( symtab[sym].name );\r
271                 DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x\n",\r
272                         ptr, *ptr, val, (Uint)ptr );\r
273                 *ptr = val + addend - (Uint)ptr;\r
274                 //*ptr = val + addend - ((Uint)ptr - base);\r
275                 break;
276 \r
277         // Absolute Value of a symbol (S)\r
278         case R_386_GLOB_DAT:\r
279         case R_386_JMP_SLOT:
280                 DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name);\r
281                 val = GetSymbol( symtab[sym].name );\r
282                 DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x\n", csaR_NAMES[type], ptr, val);\r
283                 *ptr = val;\r
284                 break;\r
285 \r
286         // Base Address (B+A)\r
287         case R_386_RELATIVE:\r
288                 DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x\n", ptr, base, addend);\r
289                 *ptr = base + addend;\r
290                 break;\r
291                 \r
292         default:\r
293                 DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]);\r
294                 break;\r
295         }\r
296         \r
297 }
298 \r
299 /**\r
300  * \fn int ElfGetSymbol(Uint Base, char *name, Uint *ret)\r
301  */
302 int ElfGetSymbol(Uint Base, char *Name, Uint *ret)
303 {
304         Elf32_Ehdr      *hdr = (void*)Base;\r
305         Elf32_Sym       *symtab;
306          int    nbuckets = 0;\r
307          int    iSymCount = 0;
308          int    i;\r
309         Uint    *pBuckets;\r
310         Uint    *pChains;\r
311         Uint    iNameHash;
312 \r
313         //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name);\r
314 \r
315         pBuckets = (void *) hdr->misc.HashTable;\r
316         symtab = (void *) hdr->misc.SymTable;\r
317         
318         nbuckets = pBuckets[0];\r
319         iSymCount = pBuckets[1];\r
320         pBuckets = &pBuckets[2];\r
321         pChains = &pBuckets[ nbuckets ];\r
322         \r
323         // Get hash
324         iNameHash = ElfHashString(Name);\r
325         iNameHash %= nbuckets;\r
326         //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash);\r
327
328         // Walk Chain\r
329         i = pBuckets[ iNameHash ];\r
330         //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[i].name);\r
331         if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) {\r
332                 *ret = symtab[ i ].value;\r
333                 return 1;\r
334         }\r
335         \r
336         //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets);\r
337         while(pChains[i] != STN_UNDEF)\r
338         {\r
339                 //DEBUGS(" pChains[%i] = %i\n", i, pChains[i]);\r
340                 i = pChains[i];\r
341                 //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[ i ].name);\r
342                 if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) {\r
343                         //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x\n", symtab[ i ].name, symtab[ i ].value);\r
344                         *ret = symtab[ i ].value;\r
345                         return 1;\r
346                 }\r
347         }\r
348         \r
349         //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found\n", Name);
350         return 0;
351 }\r
352 \r
353 Uint ElfHashString(char *name)\r
354 {\r
355         Uint    h = 0, g;\r
356         while(*name)\r
357         {\r
358                 h = (h << 4) + *name++;\r
359                 if( (g = h & 0xf0000000) )\r
360                         h ^= g >> 24;\r
361                 h &= ~g;\r
362         }\r
363         return h;\r
364 }\r
365 \r
366 #if 0\r
367 unsigned long elf_hash(const unsigned char *name)\r
368 {\r
369         unsigned long   h = 0, g;\r
370         while (*name)\r
371         {\r
372                 h = (h << 4) + *name++;\r
373                 if (g = h & 0xf0000000)\r
374                         h ^= g >> 24;\r
375                 h &= ~g;\r
376         }\r
377         return h;\r
378 }\r
379 #endif\r

UCC git Repository :: git.ucc.asn.au