VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrELFRelocatable.cpp.h@ 17082

Last change on this file since 17082 was 16933, checked in by vboxsync, 16 years ago

IPRT/PDM,SUPLIb,REM: Extended RTLdrOpen with an architecture argument for use with FAT R0.r0 images later some day. Also added fFlags argument that's currently MBZ case.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.3 KB
Line 
1/* $Id: ldrELFRelocatable.cpp.h 16933 2009-02-18 23:42:57Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Template for ELF Relocatable Images.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Defined Constants And Macros *
33*******************************************************************************/
34#if ELF_MODE == 32
35#define RTLDRELF_NAME(name) rtldrELF32##name
36#define RTLDRELF_SUFF(name) name##32
37#define RTLDRELF_MID(pre,suff) pre##32##suff
38#define FMT_ELF_ADDR "%08RX32"
39#define FMT_ELF_HALF "%04RX16"
40#define FMT_ELF_OFF "%08RX32"
41#define FMT_ELF_SIZE "%08RX32"
42#define FMT_ELF_SWORD "%RI32"
43#define FMT_ELF_WORD "%08RX32"
44#define FMT_ELF_XWORD "%08RX32"
45#define FMT_ELF_SXWORD "%RI32"
46
47#elif ELF_MODE == 64
48#define RTLDRELF_NAME(name) rtldrELF64##name
49#define RTLDRELF_SUFF(name) name##64
50#define RTLDRELF_MID(pre,suff) pre##64##suff
51#define FMT_ELF_ADDR "%016RX64"
52#define FMT_ELF_HALF "%04RX16"
53#define FMT_ELF_SHALF "%RI16"
54#define FMT_ELF_OFF "%016RX64"
55#define FMT_ELF_SIZE "%016RX64"
56#define FMT_ELF_SWORD "%RI32"
57#define FMT_ELF_WORD "%08RX32"
58#define FMT_ELF_XWORD "%016RX64"
59#define FMT_ELF_SXWORD "%RI64"
60#endif
61
62#define Elf_Ehdr RTLDRELF_MID(Elf,_Ehdr)
63#define Elf_Phdr RTLDRELF_MID(Elf,_Phdr)
64#define Elf_Shdr RTLDRELF_MID(Elf,_Shdr)
65#define Elf_Sym RTLDRELF_MID(Elf,_Sym)
66#define Elf_Rel RTLDRELF_MID(Elf,_Rel)
67#define Elf_Rela RTLDRELF_MID(Elf,_Rela)
68#define Elf_Nhdr RTLDRELF_MID(Elf,_Nhdr)
69#define Elf_Dyn RTLDRELF_MID(Elf,_Dyn)
70#define Elf_Addr RTLDRELF_MID(Elf,_Addr)
71#define Elf_Half RTLDRELF_MID(Elf,_Half)
72#define Elf_Off RTLDRELF_MID(Elf,_Off)
73#define Elf_Size RTLDRELF_MID(Elf,_Size)
74#define Elf_Sword RTLDRELF_MID(Elf,_Sword)
75#define Elf_Word RTLDRELF_MID(Elf,_Word)
76
77#define RTLDRMODELF RTLDRELF_MID(RTLDRMODELF,)
78#define PRTLDRMODELF RTLDRELF_MID(PRTLDRMODELF,)
79
80#define ELF_R_SYM(info) RTLDRELF_MID(ELF,_R_SYM)(info)
81#define ELF_R_TYPE(info) RTLDRELF_MID(ELF,_R_TYPE)(info)
82#define ELF_R_INFO(sym, type) RTLDRELF_MID(ELF,_R_INFO)(sym, type)
83
84#define ELF_ST_BIND(info) RTLDRELF_MID(ELF,_ST_BIND)(info)
85
86
87
88/*******************************************************************************
89* Structures and Typedefs *
90*******************************************************************************/
91/**
92 * The ELF loader structure.
93 */
94typedef struct RTLDRMODELF
95{
96 /** Core module structure. */
97 RTLDRMODINTERNAL Core;
98 /** Pointer to the reader instance. */
99 PRTLDRREADER pReader;
100 /** Pointer to readonly mapping of the image bits.
101 * This mapping is provided by the pReader. */
102 const void *pvBits;
103
104 /** The ELF header. */
105 Elf_Ehdr Ehdr;
106 /** Pointer to our copy of the section headers.
107 * The virtual addresses in this array is the 0 based assignments we've given the image.
108 * Not valid if the image is DONE. */
109 Elf_Shdr *paShdrs;
110 /** The size of the loaded image. */
111 size_t cbImage;
112
113 /** The symbol section index. */
114 unsigned iSymSh;
115 /** Number of symbols in the table. */
116 unsigned cSyms;
117 /** Pointer to symbol table within RTLDRMODELF::pvBits. */
118 const Elf_Sym *paSyms;
119
120 /** The string section index. */
121 unsigned iStrSh;
122 /** Size of the string table. */
123 unsigned cbStr;
124 /** Pointer to string table within RTLDRMODELF::pvBits. */
125 const char *pStr;
126} RTLDRMODELF, *PRTLDRMODELF;
127
128
129/**
130 * Maps the image bits into memory and resolve pointers into it.
131 *
132 * @returns iprt status code.
133 * @param pModElf The ELF loader module instance data.
134 * @param fNeedsBits Set if we actually need the pvBits member.
135 * If we don't, we can simply read the string and symbol sections, thus saving memory.
136 */
137static int RTLDRELF_NAME(MapBits)(PRTLDRMODELF pModElf, bool fNeedsBits)
138{
139 NOREF(fNeedsBits);
140 if (pModElf->pvBits)
141 return VINF_SUCCESS;
142 int rc = pModElf->pReader->pfnMap(pModElf->pReader, &pModElf->pvBits);
143 if (RT_SUCCESS(rc))
144 {
145 const uint8_t *pu8 = (const uint8_t *)pModElf->pvBits;
146 pModElf->paSyms = (const Elf_Sym *)(pu8 + pModElf->paShdrs[pModElf->iSymSh].sh_offset);
147 pModElf->pStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->iStrSh].sh_offset);
148 }
149 return rc;
150}
151
152
153/**
154 * Get the symbol and symbol value.
155 *
156 * @returns iprt status code.
157 * @param pModElf The ELF loader module instance data.
158 * @param BaseAddr The base address which the module is being fixedup to.
159 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
160 * @param pvUser User argument to pass to the callback.
161 * @param iSym The symbol to get.
162 * @param ppSym Where to store the symbol pointer on success. (read only)
163 * @param pSymValue Where to store the symbol value on success.
164 */
165static int RTLDRELF_NAME(Symbol)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
166 Elf_Size iSym, const Elf_Sym **ppSym, Elf_Addr *pSymValue)
167{
168 /*
169 * Validate and find the symbol.
170 */
171 if (iSym >= pModElf->cSyms)
172 {
173 AssertMsgFailed(("iSym=%d is an invalid symbol index!\n", iSym));
174 return VERR_LDRELF_INVALID_SYMBOL_INDEX;
175 }
176 const Elf_Sym *pSym = &pModElf->paSyms[iSym];
177 *ppSym = pSym;
178
179 if (pSym->st_name >= pModElf->cbStr)
180 {
181 AssertMsgFailed(("iSym=%d st_name=%d str sh_size=%d\n", iSym, pSym->st_name, pModElf->cbStr));
182 return VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET;
183 }
184 const char *pszName = ELF_STR(pModElf, pSym->st_name);
185
186 /*
187 * Determin the symbol value.
188 *
189 * Symbols needs different treatment depending on which section their are in.
190 * Undefined and absolute symbols goes into special non-existing sections.
191 */
192 switch (pSym->st_shndx)
193 {
194 /*
195 * Undefined symbol, needs resolving.
196 *
197 * Since ELF has no generic concept of importing from specific module (the OS/2 ELF format
198 * has but that's a OS extension and only applies to programs and dlls), we'll have to ask
199 * the resolver callback to do a global search.
200 */
201 case SHN_UNDEF:
202 {
203 /* Try to resolve the symbol. */
204 RTUINTPTR Value;
205 int rc = pfnGetImport(&pModElf->Core, "", pszName, ~0, &Value, pvUser);
206 if (RT_FAILURE(rc))
207 {
208 AssertMsgFailed(("Failed to resolve '%s' rc=%Rrc\n", pszName, rc));
209 return rc;
210 }
211 *pSymValue = (Elf_Addr)Value;
212 if ((RTUINTPTR)*pSymValue != Value)
213 {
214 AssertMsgFailed(("Symbol value overflowed! '%s'\n", pszName));
215 return VERR_SYMBOL_VALUE_TOO_BIG;
216 }
217
218 Log2(("rtldrELF: #%-3d - UNDEF " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
219 break;
220 }
221
222 /*
223 * Absolute symbols needs no fixing since they are, well, absolute.
224 */
225 case SHN_ABS:
226 *pSymValue = pSym->st_value;
227 Log2(("rtldrELF: #%-3d - ABS " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
228 break;
229
230 /*
231 * All other symbols are adressed relative to their section and need to be fixed up.
232 */
233 default:
234 if (pSym->st_shndx >= pModElf->Ehdr.e_shnum)
235 {
236 /* what about common symbols? */
237 AssertMsg(pSym->st_shndx < pModElf->Ehdr.e_shnum,
238 ("iSym=%d st_shndx=%d e_shnum=%d pszName=%s\n", iSym, pSym->st_shndx, pModElf->Ehdr.e_shnum, pszName));
239 return VERR_BAD_EXE_FORMAT;
240 }
241 *pSymValue = pSym->st_value + pModElf->paShdrs[pSym->st_shndx].sh_addr + BaseAddr;
242 Log2(("rtldrELF: #%-3d - %5d " FMT_ELF_ADDR " '%s'\n", iSym, pSym->st_shndx, *pSymValue, pszName));
243 break;
244 }
245
246 return VINF_SUCCESS;
247}
248
249
250/**
251 * Applies the fixups for a sections.
252 *
253 * @returns iprt status code.
254 * @param pModElf The ELF loader module instance data.
255 * @param BaseAddr The base address which the module is being fixedup to.
256 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
257 * @param pvUser User argument to pass to the callback.
258 * @param SecAddr The section address. This is the address the relocations are relative to.
259 * @param cbSec The section size. The relocations must be inside this.
260 * @param pu8SecBaseR Where we read section bits from.
261 * @param pu8SecBaseW Where we write section bits to.
262 * @param pvRelocs Pointer to where we read the relocations from.
263 * @param cbRelocs Size of the relocations.
264 */
265static int RTLDRELF_NAME(RelocateSection)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
266 const Elf_Addr SecAddr, Elf_Size cbSec, const uint8_t *pu8SecBaseR, uint8_t *pu8SecBaseW,
267 const void *pvRelocs, Elf_Size cbRelocs)
268{
269 /*
270 * Iterate the relocations.
271 * The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
272 */
273 const Elf_Reloc *paRels = (const Elf_Reloc *)pvRelocs;
274 const unsigned iRelMax = (unsigned)(cbRelocs / sizeof(paRels[0]));
275 AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])), VERR_IMAGE_TOO_BIG);
276 for (unsigned iRel = 0; iRel < iRelMax; iRel++)
277 {
278 /*
279 * Get the symbol.
280 */
281 const Elf_Sym *pSym;
282 Elf_Addr SymValue = 0; /* shut up gcc-4 */
283 int rc = RTLDRELF_NAME(Symbol)(pModElf, BaseAddr, pfnGetImport, pvUser, ELF_R_SYM(paRels[iRel].r_info), &pSym, &SymValue);
284 if (RT_FAILURE(rc))
285 return rc;
286
287 Log3(("rtldrELF: " FMT_ELF_ADDR " %02x %06x - " FMT_ELF_ADDR " %3d %02x %s\n",
288 paRels[iRel].r_offset, ELF_R_TYPE(paRels[iRel].r_info), (unsigned)ELF_R_SYM(paRels[iRel].r_info),
289 SymValue, (unsigned)pSym->st_shndx, pSym->st_info, ELF_STR(pModElf, pSym->st_name)));
290
291 /*
292 * Apply the fixup.
293 */
294 AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec), VERR_LDRELF_INVALID_RELOCATION_OFFSET);
295#if ELF_MODE == 32
296 const Elf_Addr *pAddrR = (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset); /* Where to read the addend. */
297#endif
298 Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
299 switch (ELF_R_TYPE(paRels[iRel].r_info))
300 {
301#if ELF_MODE == 32
302 /*
303 * Absolute addressing.
304 */
305 case R_386_32:
306 {
307 const Elf_Addr Value = SymValue + *pAddrR;
308 *(uint32_t *)pAddrW = Value;
309 Log4((FMT_ELF_ADDR": R_386_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
310 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
311 break;
312 }
313
314 /*
315 * PC relative addressing.
316 */
317 case R_386_PC32:
318 {
319 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
320 const Elf_Addr Value = SymValue + *(uint32_t *)pAddrR - SourceAddr;
321 *(uint32_t *)pAddrW = Value;
322 Log4((FMT_ELF_ADDR": R_386_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
323 SourceAddr, Value, SymValue));
324 break;
325 }
326
327 /* ignore */
328 case R_386_NONE:
329 break;
330
331#elif ELF_MODE == 64
332
333 /*
334 * Absolute addressing
335 */
336 case R_X86_64_64:
337 {
338 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
339 *(uint64_t *)pAddrW = Value;
340 Log4((FMT_ELF_ADDR": R_X86_64_64 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
341 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
342 break;
343 }
344
345 /*
346 * Trunacated 32-bit value (zero-extendedable to the 64-bit value).
347 */
348 case R_X86_64_32:
349 {
350 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
351 *(uint32_t *)pAddrW = (uint32_t)Value;
352 Log4((FMT_ELF_ADDR": R_X86_64_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
353 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
354 AssertMsgReturn((Elf_Addr)*(uint32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
355 break;
356 }
357
358 /*
359 * Truncated 32-bit value (sign-extendedable to the 64-bit value).
360 */
361 case R_X86_64_32S:
362 {
363 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
364 *(int32_t *)pAddrW = (int32_t)Value;
365 Log4((FMT_ELF_ADDR": R_X86_64_32S Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
366 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
367 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
368 break;
369 }
370
371 /*
372 * PC relative addressing.
373 */
374 case R_X86_64_PC32:
375 {
376 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
377 const Elf_Addr Value = SymValue + paRels[iRel].r_addend - SourceAddr;
378 *(int32_t *)pAddrW = (int32_t)Value;
379 Log4((FMT_ELF_ADDR": R_X86_64_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
380 SourceAddr, Value, SymValue));
381 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
382 break;
383 }
384
385 /* ignore */
386 case R_X86_64_NONE:
387 break;
388#endif
389
390 default:
391 AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
392 ELF_R_TYPE(paRels[iRel].r_info), iRel, iRelMax));
393 return VERR_LDRELF_RELOCATION_NOT_SUPPORTED;
394 }
395 }
396
397 return VINF_SUCCESS;
398}
399
400
401
402/** @copydoc RTLDROPS::pfnClose */
403static DECLCALLBACK(int) RTLDRELF_NAME(Close)(PRTLDRMODINTERNAL pMod)
404{
405 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
406
407 if (pModElf->paShdrs)
408 {
409 RTMemFree(pModElf->paShdrs);
410 pModElf->paShdrs = NULL;
411 }
412
413 if (pModElf->pReader)
414 {
415 pModElf->pReader->pfnDestroy(pModElf->pReader);
416 pModElf->pReader = NULL;
417 }
418
419 pModElf->pvBits = NULL;
420
421 return VINF_SUCCESS;
422}
423
424
425/** @copydoc RTLDROPS::Done */
426static DECLCALLBACK(int) RTLDRELF_NAME(Done)(PRTLDRMODINTERNAL pMod)
427{
428 //PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
429 /** @todo Have to think more about this .... */
430 return -1;
431}
432
433
434/** @copydoc RTLDROPS::EnumSymbols */
435static DECLCALLBACK(int) RTLDRELF_NAME(EnumSymbols)(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
436 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
437{
438 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
439
440 /*
441 * Validate the input.
442 */
443 Elf_Addr BaseAddr = (Elf_Addr)BaseAddress;
444 AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
445
446 /*
447 * Make sure we've got the string and symbol tables. (We don't need the pvBits.)
448 */
449 int rc = RTLDRELF_NAME(MapBits)(pModElf, false);
450 if (RT_FAILURE(rc))
451 return rc;
452
453 /*
454 * Enumerate the symbol table.
455 */
456 const Elf_Sym *paSyms = pModElf->paSyms;
457 unsigned cSyms = pModElf->cSyms;
458 for (unsigned iSym = 1; iSym < cSyms; iSym++)
459 {
460 /*
461 * Skip imports (undefined).
462 */
463 if (paSyms[iSym].st_shndx != SHN_UNDEF)
464 {
465 /*
466 * Calc value and get name.
467 */
468 Elf_Addr Value;
469 if (paSyms[iSym].st_shndx == SHN_ABS)
470 /* absolute symbols are not subject to any relocation. */
471 Value = paSyms[iSym].st_value;
472 else if (paSyms[iSym].st_shndx < pModElf->Ehdr.e_shnum)
473 /* relative to the section. */
474 Value = BaseAddr + paSyms[iSym].st_value + pModElf->paShdrs[paSyms[iSym].st_shndx].sh_addr;
475 else
476 {
477 AssertMsgFailed(("Arg! paSyms[%u].st_shndx=" FMT_ELF_HALF "\n", iSym, paSyms[iSym].st_shndx));
478 return VERR_BAD_EXE_FORMAT;
479 }
480 const char *pszName = ELF_STR(pModElf, paSyms[iSym].st_name);
481 if ( (pszName && *pszName)
482 && ( (fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)
483 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL)
484 )
485 {
486 /*
487 * Call back.
488 */
489 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
490 int rc = pfnCallback(pMod, pszName, ~0, (RTUINTPTR)Value, pvUser);
491 if (rc)
492 return rc;
493 }
494 }
495 }
496
497 return VINF_SUCCESS;
498}
499
500
501/** @copydoc RTLDROPS::GetImageSize */
502static DECLCALLBACK(size_t) RTLDRELF_NAME(GetImageSize)(PRTLDRMODINTERNAL pMod)
503{
504 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
505
506 return pModElf->cbImage;
507}
508
509
510/** @copydoc RTLDROPS::GetBits */
511static DECLCALLBACK(int) RTLDRELF_NAME(GetBits)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
512{
513 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
514
515 /*
516 * Load the bits into pvBits.
517 */
518 const Elf_Shdr *paShdrs = pModElf->paShdrs;
519 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
520 {
521 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
522 {
523 AssertMsgReturn((size_t)paShdrs[iShdr].sh_size == (size_t)paShdrs[iShdr].sh_size, (FMT_ELF_SIZE "\n", paShdrs[iShdr].sh_size), VERR_IMAGE_TOO_BIG);
524 switch (paShdrs[iShdr].sh_type)
525 {
526 case SHT_NOBITS:
527 memset((uint8_t *)pvBits + paShdrs[iShdr].sh_addr, 0, (size_t)paShdrs[iShdr].sh_size);
528 break;
529
530 case SHT_PROGBITS:
531 default:
532 {
533 int rc = pModElf->pReader->pfnRead(pModElf->pReader, (uint8_t *)pvBits + paShdrs[iShdr].sh_addr,
534 (size_t)paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset);
535 if (RT_FAILURE(rc))
536 {
537 Log(("RTLdrELF: %s: Read error when reading " FMT_ELF_SIZE " bytes at " FMT_ELF_OFF ", iShdr=%d\n",
538 pModElf->pReader->pfnLogName(pModElf->pReader),
539 paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset, iShdr));
540 return rc;
541 }
542 }
543 }
544 }
545 }
546
547 /*
548 * Relocate the image.
549 */
550 return pModElf->Core.pOps->pfnRelocate(pMod, pvBits, BaseAddress, ~(RTUINTPTR)0, pfnGetImport, pvUser);
551}
552
553
554/** @copydoc RTLDROPS::Relocate */
555static DECLCALLBACK(int) RTLDRELF_NAME(Relocate)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
556{
557 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
558
559 /*
560 * Validate the input.
561 */
562 Elf_Addr BaseAddr = (Elf_Addr)NewBaseAddress;
563 AssertMsgReturn((RTUINTPTR)BaseAddr == NewBaseAddress, ("#RTptr", NewBaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
564
565 /*
566 * Map the image bits if not already done and setup pointer into it.
567 */
568 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
569 if (RT_FAILURE(rc))
570 return rc;
571
572 /*
573 * Iterate the sections looking for interesting SHT_REL[A] sections.
574 * SHT_REL[A] sections have the section index of the section they contain fixups
575 * for in the sh_info member.
576 */
577 const Elf_Shdr *paShdrs = pModElf->paShdrs;
578#ifdef LOG_ENABLED
579 const char *pszLogName = pModElf->pReader->pfnLogName(pModElf->pReader);
580 Log2(("rtLdrElf: %s: Fixing up image\n", pszLogName));
581#endif
582 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
583 {
584 const Elf_Shdr *pShdrRel = &paShdrs[iShdr];
585
586 /*
587 * Skip sections without interest to us.
588 */
589#if ELF_MODE == 32
590 if (pShdrRel->sh_type != SHT_REL)
591#else
592 if (pShdrRel->sh_type != SHT_RELA)
593#endif
594 continue;
595 if (pShdrRel->sh_info >= pModElf->Ehdr.e_shnum)
596 continue;
597 const Elf_Shdr *pShdr = &paShdrs[pShdrRel->sh_info]; /* the section to fixup. */
598 if (!(pShdr->sh_flags & SHF_ALLOC))
599 continue;
600
601 /*
602 * Relocate the section.
603 */
604 Log2(("rtldrELF: %s: Relocation records for #%d [%s] (sh_info=%d sh_link=%d) found in #%d [%s] (sh_info=%d sh_link=%d)\n",
605 pszLogName, (int)pShdrRel->sh_info, ELF_STR(pModElf, pShdr->sh_name), (int)pShdr->sh_info, (int)pShdr->sh_link,
606 iShdr, ELF_STR(pModElf, pShdrRel->sh_name), (int)pShdrRel->sh_info, (int)pShdrRel->sh_link));
607
608 /** @todo Make RelocateSection a function pointer so we can select the one corresponding to the machine when opening the image. */
609 rc = RTLDRELF_NAME(RelocateSection)(pModElf, BaseAddr, pfnGetImport, pvUser,
610 pShdr->sh_addr,
611 pShdr->sh_size,
612 (const uint8_t *)pModElf->pvBits + pShdr->sh_offset,
613 (uint8_t *)pvBits + pShdr->sh_addr,
614 (const uint8_t *)pModElf->pvBits + pShdrRel->sh_offset,
615 pShdrRel->sh_size);
616 if (RT_FAILURE(rc))
617 return rc;
618 }
619 return VINF_SUCCESS;
620}
621
622
623/** @copydoc RTLDROPS::pfnGetSymbolEx */
624static DECLCALLBACK(int) RTLDRELF_NAME(GetSymbolEx)(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue)
625{
626 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
627 NOREF(pvBits);
628
629 /*
630 * Validate the input.
631 */
632 Elf_Addr BaseAddr = (Elf_Addr)BaseAddress;
633 AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
634
635 /*
636 * Map the image bits if not already done and setup pointer into it.
637 */
638 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
639 if (RT_FAILURE(rc))
640 return rc;
641
642 /*
643 * Calc all kinds of pointers before we start iterating the symbol table.
644 */
645 const char *pStr = pModElf->pStr;
646 const Elf_Sym *paSyms = pModElf->paSyms;
647 unsigned cSyms = pModElf->cSyms;
648 for (unsigned iSym = 1; iSym < cSyms; iSym++)
649 {
650 /* Undefined symbols are not exports, they are imports. */
651 if ( paSyms[iSym].st_shndx != SHN_UNDEF
652 && ( ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
653 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_WEAK))
654 {
655 /* Validate the name string and try match with it. */
656 if (paSyms[iSym].st_name < pModElf->cbStr)
657 {
658 if (!strcmp(pszSymbol, pStr + paSyms[iSym].st_name))
659 {
660 /* matched! */
661 Elf_Addr Value;
662 if (paSyms[iSym].st_shndx == SHN_ABS)
663 /* absolute symbols are not subject to any relocation. */
664 Value = paSyms[iSym].st_value;
665 else if (paSyms[iSym].st_shndx < pModElf->Ehdr.e_shnum)
666 /* relative to the section. */
667 Value = BaseAddr + paSyms[iSym].st_value + pModElf->paShdrs[paSyms[iSym].st_shndx].sh_addr;
668 else
669 {
670 AssertMsgFailed(("Arg. paSyms[iSym].st_shndx=%d\n", paSyms[iSym].st_shndx));
671 return VERR_BAD_EXE_FORMAT;
672 }
673 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
674 *pValue = (RTUINTPTR)Value;
675 return VINF_SUCCESS;
676 }
677 }
678 else
679 {
680 AssertMsgFailed(("String outside string table! iSym=%d paSyms[iSym].st_name=%#x\n", iSym, paSyms[iSym].st_name));
681 return VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET;
682 }
683 }
684 }
685
686 return VERR_SYMBOL_NOT_FOUND;
687}
688
689
690
691/**
692 * The ELF module operations.
693 */
694static RTLDROPS RTLDRELF_MID(s_rtldrElf,Ops) =
695{
696#if ELF_MODE == 32
697 "elf32",
698#elif ELF_MODE == 64
699 "elf64",
700#endif
701 RTLDRELF_NAME(Close),
702 NULL, /* Get Symbol */
703 RTLDRELF_NAME(Done),
704 RTLDRELF_NAME(EnumSymbols),
705 /* ext: */
706 RTLDRELF_NAME(GetImageSize),
707 RTLDRELF_NAME(GetBits),
708 RTLDRELF_NAME(Relocate),
709 RTLDRELF_NAME(GetSymbolEx),
710 0
711};
712
713
714
715/**
716 * Validates the ELF header.
717 *
718 * @returns iprt status code.
719 * @param pEhdr Pointer to the ELF header.
720 * @param pszLogName The log name.
721 * @param cbRawImage The size of the raw image.
722 */
723static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, const char *pszLogName, uint64_t cbRawImage, PRTLDRARCH penmArch)
724{
725 Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
726 "RTLdrELF: e_type: " FMT_ELF_HALF "\n"
727 "RTLdrELF: e_version: " FMT_ELF_HALF "\n"
728 "RTLdrELF: e_entry: " FMT_ELF_ADDR "\n"
729 "RTLdrELF: e_phoff: " FMT_ELF_OFF "\n"
730 "RTLdrELF: e_shoff: " FMT_ELF_OFF "\n"
731 "RTLdrELF: e_flags: " FMT_ELF_WORD "\n"
732 "RTLdrELF: e_ehsize: " FMT_ELF_HALF "\n"
733 "RTLdrELF: e_phentsize: " FMT_ELF_HALF "\n"
734 "RTLdrELF: e_phnum: " FMT_ELF_HALF "\n"
735 "RTLdrELF: e_shentsize: " FMT_ELF_HALF "\n"
736 "RTLdrELF: e_shnum: " FMT_ELF_HALF "\n"
737 "RTLdrELF: e_shstrndx: " FMT_ELF_HALF "\n",
738 RT_ELEMENTS(pEhdr->e_ident), &pEhdr->e_ident[0], pEhdr->e_type, pEhdr->e_version,
739 pEhdr->e_entry, pEhdr->e_phoff, pEhdr->e_shoff,pEhdr->e_flags, pEhdr->e_ehsize, pEhdr->e_phentsize,
740 pEhdr->e_phnum, pEhdr->e_shentsize, pEhdr->e_shnum, pEhdr->e_shstrndx));
741
742 if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
743 || pEhdr->e_ident[EI_MAG1] != ELFMAG1
744 || pEhdr->e_ident[EI_MAG2] != ELFMAG2
745 || pEhdr->e_ident[EI_MAG3] != ELFMAG3
746 )
747 {
748 Log(("RTLdrELF: %s: Invalid ELF magic (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
749 return VERR_BAD_EXE_FORMAT;
750 }
751 if (pEhdr->e_ident[EI_CLASS] != RTLDRELF_SUFF(ELFCLASS))
752 {
753 Log(("RTLdrELF: %s: Invalid ELF class (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
754 return VERR_BAD_EXE_FORMAT;
755 }
756 if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
757 {
758 Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", pEhdr->e_ident[EI_DATA]));
759 return VERR_LDRELF_ODD_ENDIAN;
760 }
761 if (pEhdr->e_version != EV_CURRENT)
762 {
763 Log(("RTLdrELF: %s: ELF version %x is unsupported\n", pEhdr->e_version));
764 return VERR_LDRELF_VERSION;
765 }
766
767 if (sizeof(Elf_Ehdr) != pEhdr->e_ehsize)
768 {
769 Log(("RTLdrELF: %s: Elf header e_ehsize is %d expected %d!\n",
770 pszLogName, pEhdr->e_ehsize, sizeof(Elf_Ehdr)));
771 return VERR_BAD_EXE_FORMAT;
772 }
773 if ( sizeof(Elf_Phdr) != pEhdr->e_phentsize
774 && ( pEhdr->e_phnum != 0
775 || pEhdr->e_type == ET_DYN))
776 {
777 Log(("RTLdrELF: %s: Elf header e_phentsize is %d expected %d!\n",
778 pszLogName, pEhdr->e_phentsize, sizeof(Elf_Phdr)));
779 return VERR_BAD_EXE_FORMAT;
780 }
781 if (sizeof(Elf_Shdr) != pEhdr->e_shentsize)
782 {
783 Log(("RTLdrELF: %s: Elf header e_shentsize is %d expected %d!\n",
784 pszLogName, pEhdr->e_shentsize, sizeof(Elf_Shdr)));
785 return VERR_BAD_EXE_FORMAT;
786 }
787
788 switch (pEhdr->e_type)
789 {
790 case ET_REL:
791 break;
792 case ET_EXEC:
793 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pszLogName));
794 return VERR_LDRELF_EXEC;
795 case ET_DYN:
796 Log(("RTLdrELF: %s: Dynamic images are not supported yet!\n", pszLogName));
797 return VERR_LDRELF_DYN;
798 default:
799 Log(("RTLdrELF: %s: image type %#x is not supported!\n", pszLogName, pEhdr->e_type));
800 return VERR_BAD_EXE_FORMAT;
801 }
802
803 switch (pEhdr->e_machine)
804 {
805#if ELF_MODE == 32
806 case EM_386:
807 case EM_486:
808 *penmArch = RTLDRARCH_X86_32;
809 break;
810#elif ELF_MODE == 64
811 case EM_X86_64:
812 *penmArch = RTLDRARCH_AMD64;
813 break;
814#endif
815 default:
816 Log(("RTLdrELF: %s: machine type %u is not supported!\n", pEhdr->e_machine));
817 return VERR_LDRELF_MACHINE;
818 }
819
820 if ( pEhdr->e_phoff < pEhdr->e_ehsize
821 && !(pEhdr->e_phoff && pEhdr->e_phnum)
822 && pEhdr->e_phnum)
823 {
824 Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n",
825 pszLogName, pEhdr->e_phoff));
826 return VERR_BAD_EXE_FORMAT;
827 }
828 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
829 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
830 {
831 Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
832 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum));
833 return VERR_BAD_EXE_FORMAT;
834 }
835
836
837 if ( pEhdr->e_shoff < pEhdr->e_ehsize
838 && !(pEhdr->e_shoff && pEhdr->e_shnum))
839 {
840 Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n",
841 pszLogName, pEhdr->e_shoff));
842 return VERR_BAD_EXE_FORMAT;
843 }
844 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
845 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
846 {
847 Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
848 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum));
849 return VERR_BAD_EXE_FORMAT;
850 }
851
852 return VINF_SUCCESS;
853}
854
855/**
856 * Gets the section header name.
857 *
858 * @returns pszName.
859 * @param pReader The loader reader instance.
860 * @param pEhdr The elf header.
861 * @param offName The offset of the section header name.
862 * @param pszName Where to store the name.
863 * @param cbName The size of the buffer pointed to by pszName.
864 */
865const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
866{
867 RTFOFF off = pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset + offName;
868 int rc = pModElf->pReader->pfnRead(pModElf->pReader, pszName, cbName - 1, off);
869 if (RT_FAILURE(rc))
870 {
871 /* read by for byte. */
872 for (unsigned i = 0; i < cbName; i++, off++)
873 {
874 int rc = pModElf->pReader->pfnRead(pModElf->pReader, pszName + i, 1, off);
875 if (RT_FAILURE(rc))
876 {
877 pszName[i] = '\0';
878 break;
879 }
880 }
881 }
882
883 pszName[cbName - 1] = '\0';
884 return pszName;
885}
886
887
888/**
889 * Validates a section header.
890 *
891 * @returns iprt status code.
892 * @param pModElf Pointer to the module structure.
893 * @param iShdr The index of section header which should be validated.
894 * The section headers are found in the pModElf->paShdrs array.
895 * @param pszLogName The log name.
896 * @param cbRawImage The size of the raw image.
897 */
898static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, const char *pszLogName, RTFOFF cbRawImage)
899{
900 const Elf_Shdr *pShdr = &pModElf->paShdrs[iShdr];
901 char szSectionName[80]; NOREF(szSectionName);
902 Log3(("RTLdrELF: Section Header #%d:\n"
903 "RTLdrELF: sh_name: " FMT_ELF_WORD " - %s\n"
904 "RTLdrELF: sh_type: " FMT_ELF_WORD " (%s)\n"
905 "RTLdrELF: sh_flags: " FMT_ELF_XWORD "\n"
906 "RTLdrELF: sh_addr: " FMT_ELF_ADDR "\n"
907 "RTLdrELF: sh_offset: " FMT_ELF_OFF "\n"
908 "RTLdrELF: sh_size: " FMT_ELF_XWORD "\n"
909 "RTLdrELF: sh_link: " FMT_ELF_WORD "\n"
910 "RTLdrELF: sh_info: " FMT_ELF_WORD "\n"
911 "RTLdrELF: sh_addralign: " FMT_ELF_XWORD "\n"
912 "RTLdrELF: sh_entsize: " FMT_ELF_XWORD "\n",
913 iShdr,
914 pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
915 pShdr->sh_type, rtldrElfGetShdrType(pShdr->sh_type), pShdr->sh_flags, pShdr->sh_addr,
916 pShdr->sh_offset, pShdr->sh_size, pShdr->sh_link, pShdr->sh_info, pShdr->sh_addralign,
917 pShdr->sh_entsize));
918
919 if (pShdr->sh_link >= pModElf->Ehdr.e_shnum)
920 {
921 Log(("RTLdrELF: %s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!\n",
922 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
923 return VERR_BAD_EXE_FORMAT;
924 }
925
926 switch (pShdr->sh_type)
927 {
928 /** @todo find specs and check up which sh_info fields indicates section table entries */
929 case 12301230:
930 if (pShdr->sh_info >= pModElf->Ehdr.e_shnum)
931 {
932 Log(("RTLdrELF: %s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!\n",
933 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
934 return VERR_BAD_EXE_FORMAT;
935 }
936 break;
937
938 case SHT_NULL:
939 case SHT_PROGBITS:
940 case SHT_SYMTAB:
941 case SHT_STRTAB:
942 case SHT_RELA:
943 case SHT_HASH:
944 case SHT_DYNAMIC:
945 case SHT_NOTE:
946 case SHT_NOBITS:
947 case SHT_REL:
948 case SHT_SHLIB:
949 case SHT_DYNSYM:
950 /*
951 * For these types sh_info doesn't have any special meaning, or anything which
952 * we need/can validate now.
953 */
954 break;
955
956
957 default:
958 Log(("RTLdrELF: %s: Warning, unknown type %d!\n", pszLogName, pShdr->sh_type));
959 break;
960 }
961
962 if ( pShdr->sh_type != SHT_NOBITS
963 && pShdr->sh_size)
964 {
965 RTFOFF offEnd = pShdr->sh_offset + pShdr->sh_size;
966 if ( offEnd > cbRawImage
967 || offEnd < (RTFOFF)pShdr->sh_offset)
968 {
969 Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD " = %RTfoff) is beyond the end of the file (%RTfoff)!\n",
970 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, offEnd, cbRawImage));
971 return VERR_BAD_EXE_FORMAT;
972 }
973 if (pShdr->sh_offset < sizeof(Elf_Ehdr))
974 {
975 Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!\n",
976 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, cbRawImage));
977 return VERR_BAD_EXE_FORMAT;
978 }
979 }
980
981 return VINF_SUCCESS;
982}
983
984
985
986/**
987 * Opens an ELF image, fixed bitness.
988 *
989 * @returns iprt status code.
990 * @param pReader The loader reader instance which will provide the raw image bits.
991 * @param fFlags Reserved, MBZ.
992 * @param enmArch Architecture specifier.
993 * @param phLdrMod Where to store the handle.
994 */
995static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
996{
997 const char *pszLogName = pReader->pfnLogName(pReader);
998 RTFOFF cbRawImage = pReader->pfnSize(pReader);
999
1000 /*
1001 * Create the loader module instance.
1002 */
1003 PRTLDRMODELF pModElf = (PRTLDRMODELF)RTMemAllocZ(sizeof(*pModElf));
1004 if (!pModElf)
1005 return VERR_NO_MEMORY;
1006
1007 pModElf->Core.u32Magic = RTLDRMOD_MAGIC;
1008 pModElf->Core.eState = LDR_STATE_INVALID;
1009 pModElf->pReader = pReader;
1010 //pModElf->pvBits = NULL;
1011 //pModElf->Ehdr = {0};
1012 //pModElf->paShdrs = NULL;
1013 //pModElf->paSyms = NULL;
1014 pModElf->iSymSh = ~0U;
1015 pModElf->cSyms = 0;
1016 pModElf->iStrSh = ~0U;
1017 pModElf->cbStr = 0;
1018 pModElf->cbImage = 0;
1019 //pModElf->pStr = NULL;
1020
1021 /*
1022 * Read and validate the ELF header and match up the CPU architecture.
1023 */
1024 int rc = pReader->pfnRead(pReader, &pModElf->Ehdr, sizeof(pModElf->Ehdr), 0);
1025 if (RT_SUCCESS(rc))
1026 {
1027 RTLDRARCH enmArchImage;
1028 rc = RTLDRELF_NAME(ValidateElfHeader)(&pModElf->Ehdr, pszLogName, cbRawImage, &enmArchImage);
1029 if (RT_SUCCESS(rc))
1030 {
1031 if ( enmArch != RTLDRARCH_WHATEVER
1032 && enmArch != enmArchImage)
1033 rc = VERR_LDR_ARCH_MISMATCH;
1034 }
1035 }
1036 if (RT_SUCCESS(rc))
1037 {
1038 /*
1039 * Read the section headers.
1040 */
1041 Elf_Shdr *paShdrs = (Elf_Shdr *)RTMemAlloc(pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr));
1042 if (paShdrs)
1043 {
1044 pModElf->paShdrs = paShdrs;
1045 rc = pReader->pfnRead(pReader, paShdrs, pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr),
1046 pModElf->Ehdr.e_shoff);
1047 if (RT_SUCCESS(rc))
1048 {
1049 /*
1050 * Validate the section headers, allocate memory for the sections (determin the image size),
1051 * and find relevant sections.
1052 */
1053 for (unsigned i = 0; i < pModElf->Ehdr.e_shnum; i++)
1054 {
1055 rc = RTLDRELF_NAME(ValidateSectionHeader)(pModElf, i, pszLogName, cbRawImage);
1056 if (RT_FAILURE(rc))
1057 break;
1058
1059 /* Allocate memory addresses for the section. */
1060 if (paShdrs[i].sh_flags & SHF_ALLOC)
1061 {
1062 paShdrs[i].sh_addr = paShdrs[i].sh_addralign
1063 ? RT_ALIGN_T(pModElf->cbImage, paShdrs[i].sh_addralign, Elf_Addr)
1064 : (Elf_Addr)pModElf->cbImage;
1065 pModElf->cbImage = (size_t)paShdrs[i].sh_addr + (size_t)paShdrs[i].sh_size;
1066 AssertMsgReturn(pModElf->cbImage == paShdrs[i].sh_addr + paShdrs[i].sh_size,
1067 (FMT_ELF_ADDR "\n", paShdrs[i].sh_addr + paShdrs[i].sh_size),
1068 VERR_IMAGE_TOO_BIG);
1069 Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
1070 }
1071
1072 /* We're looking for symbol tables. */
1073 if (paShdrs[i].sh_type == SHT_SYMTAB)
1074 {
1075 if (pModElf->iSymSh != ~0U)
1076 {
1077 Log(("RTLdrElf: %s: Multiple symbol tabs! iSymSh=%d i=%d\n", pszLogName, pModElf->iSymSh, i));
1078 rc = VERR_LDRELF_MULTIPLE_SYMTABS;
1079 break;
1080 }
1081 pModElf->iSymSh = i;
1082 pModElf->cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
1083 AssertReturn(pModElf->cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), VERR_IMAGE_TOO_BIG);
1084 pModElf->iStrSh = paShdrs[i].sh_link;
1085 pModElf->cbStr = (unsigned)paShdrs[pModElf->iStrSh].sh_size;
1086 AssertReturn(pModElf->cbStr == paShdrs[pModElf->iStrSh].sh_size, VERR_IMAGE_TOO_BIG);
1087 }
1088 } /* for each section header */
1089
1090 Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx\n",
1091 pModElf->iSymSh, pModElf->cSyms, pModElf->iStrSh, pModElf->cbStr, rc, pModElf->cbImage));
1092
1093 /*
1094 * Are the section headers fine?
1095 * We require there to be symbol & string tables (at least for the time being).
1096 */
1097 if ( pModElf->iSymSh == ~0U
1098 || pModElf->iStrSh == ~0U)
1099 rc = VERR_LDRELF_NO_SYMBOL_OR_NO_STRING_TABS;
1100 if (RT_SUCCESS(rc))
1101 {
1102 pModElf->Core.pOps = &RTLDRELF_MID(s_rtldrElf,Ops);
1103 pModElf->Core.eState = LDR_STATE_OPENED;
1104 *phLdrMod = &pModElf->Core;
1105
1106 LogFlow(("%s: %s: returns VINF_SUCCESS *phLdrMod=%p\n", __FUNCTION__, pszLogName, *phLdrMod));
1107 return VINF_SUCCESS;
1108 }
1109 }
1110
1111 RTMemFree(paShdrs);
1112 }
1113 else
1114 rc = VERR_NO_MEMORY;
1115 }
1116
1117 RTMemFree(pModElf);
1118 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
1119 return rc;
1120}
1121
1122
1123
1124
1125/*******************************************************************************
1126* Cleanup Constants And Macros *
1127*******************************************************************************/
1128#undef RTLDRELF_NAME
1129#undef RTLDRELF_SUFF
1130#undef RTLDRELF_MID
1131
1132#undef FMT_ELF_ADDR
1133#undef FMT_ELF_HALF
1134#undef FMT_ELF_SHALF
1135#undef FMT_ELF_OFF
1136#undef FMT_ELF_SIZE
1137#undef FMT_ELF_SWORD
1138#undef FMT_ELF_WORD
1139#undef FMT_ELF_XWORD
1140#undef FMT_ELF_SXWORD
1141
1142#undef Elf_Ehdr
1143#undef Elf_Phdr
1144#undef Elf_Shdr
1145#undef Elf_Sym
1146#undef Elf_Rel
1147#undef Elf_Rela
1148#undef Elf_Reloc
1149#undef Elf_Nhdr
1150#undef Elf_Dyn
1151
1152#undef Elf_Addr
1153#undef Elf_Half
1154#undef Elf_Off
1155#undef Elf_Size
1156#undef Elf_Sword
1157#undef Elf_Word
1158
1159#undef RTLDRMODELF
1160#undef PRTLDRMODELF
1161
1162#undef ELF_R_SYM
1163#undef ELF_R_TYPE
1164#undef ELF_R_INFO
1165
1166#undef ELF_ST_BIND
1167
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette