2 * AcessOS 1 - Dynamic Loader
\r
11 # define DEBUGS(v...) SysDebug(v)
\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"};
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
28 \fn int ElfRelocate(void *Base, char **envp, char *Filename)
\r
29 \brief Relocates a loaded ELF Executable
\r
31 int ElfRelocate(void *Base, char **envp, char *Filename)
\r
33 Elf32_Ehdr *hdr = Base;
\r
35 int i, j; // Counters
\r
37 Uint iRealBase = -1;
\r
41 Elf32_Rel *rel = NULL;
\r
42 Elf32_Rela *rela = NULL;
\r
43 Uint32 *pltgot = 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
52 DEBUGS("ElfRelocate: (Base=0x%x)\n", Base);
\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
59 // Determine linked base address
\r
60 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
\r
61 iRealBase = phtab[i].VAddr;
\r
63 // Find Dynamic Section
\r
64 if(phtab[i].Type == PT_DYNAMIC) {
\r
66 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n");
\r
69 dynamicTab = (void *) phtab[i].VAddr;
\r
70 j = i; // Save Dynamic Table ID
\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
78 // Adjust "Real" Base
\r
79 iBaseDiff = (Uint)Base - iRealBase;
\r
81 hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
\r
83 // Check if a PT_DYNAMIC segement was found
\r
85 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n");
\r
86 return hdr->entrypoint;
\r
89 // Adjust Dynamic Table
\r
90 dynamicTab = (void *) ((Uint)dynamicTab + iBaseDiff);
\r
92 // === Get Symbol table and String Table ===
\r
93 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
\r
95 switch(dynamicTab[j].d_tag)
97 // --- Symbol Table ---
\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
105 // --- String Table ---
\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
112 // --- Hash Table --
\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
121 if(dynsymtab == NULL) {
\r
122 SysDebug("WARNING: No Dynamic Symbol table, returning\n");
\r
123 return hdr->entrypoint;
\r
126 // Alter Symbols to true base
\r
127 for(i=0;i<iSymCount;i++)
\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
134 // === Add to loaded list (can be imported now) ===
135 AddLoaded( Filename, (Uint)Base );
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
141 switch(dynamicTab[j].d_tag)
\r
143 // --- Shared Library Name ---
\r
145 DEBUGS(" elf_relocate: .so Name '%s'\n", dynstrtab+dynamicTab[j].d_val);
\r
147 // --- Needed Library ---
\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
153 DEBUGS(" elf_relocate: Unable to load '%s'\n", libPath);
\r
155 SysDebug("Unable to load required library '%s'\n", libPath);
\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
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
174 // --- Symbol Table ---
\r
176 // --- Hash Table ---
\r
178 // --- String Table ---
\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
191 DEBUGS(" elf_relocate: Beginning Relocation\n");
\r
193 // Parse Relocation Entries
\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
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
206 // Parse Relocation Entries
\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
214 ptr = (void*)(iBaseDiff + rela[i].r_offset);
\r
215 elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, iBaseDiff);
\r
219 // === Process PLT (Procedure Linkage Table) ===
\r
223 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x\n", plt);
\r
224 if(pltType == DT_REL)
\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
231 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
\r
232 elf_doRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, iRealBase);
\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
242 ptr = (void*)(iRealBase + pltRela[i].r_offset);
\r
243 elf_doRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend, dynsymtab, iRealBase);
\r
248 DEBUGS("ElfRelocate: RETURN 0x%x", hdr->entrypoint);
\r
249 return hdr->entrypoint;
\r
252 void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base)
\r
254 int type = ELF32_R_TYPE(r_info);
\r
255 int sym = ELF32_R_SYM(r_info);
\r
259 // Standard 32 Bit Relocation (S+A)
\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
267 // 32 Bit Relocation wrt. Offset (S+A-P)
\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
277 // Absolute Value of a symbol (S)
\r
278 case R_386_GLOB_DAT:
\r
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
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
293 DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]);
\r
300 * \fn int ElfGetSymbol(Uint Base, char *name, Uint *ret)
\r
302 int ElfGetSymbol(Uint Base, char *Name, Uint *ret)
304 Elf32_Ehdr *hdr = (void*)Base;
\r
313 //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name);
\r
315 pBuckets = (void *) hdr->misc.HashTable;
\r
316 symtab = (void *) hdr->misc.SymTable;
\r
318 nbuckets = pBuckets[0];
\r
319 iSymCount = pBuckets[1];
\r
320 pBuckets = &pBuckets[2];
\r
321 pChains = &pBuckets[ nbuckets ];
\r
324 iNameHash = ElfHashString(Name);
\r
325 iNameHash %= nbuckets;
\r
326 //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash);
\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
336 //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets);
\r
337 while(pChains[i] != STN_UNDEF)
\r
339 //DEBUGS(" pChains[%i] = %i\n", 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
349 //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found\n", Name);
353 Uint ElfHashString(char *name)
\r
358 h = (h << 4) + *name++;
\r
359 if( (g = h & 0xf0000000) )
\r
367 unsigned long elf_hash(const unsigned char *name)
\r
369 unsigned long h = 0, g;
\r
372 h = (h << 4) + *name++;
\r
373 if (g = h & 0xf0000000)
\r