VirtualBox

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

Last change on this file since 38547 was 38547, checked in by vboxsync, 13 years ago

IPRT: More debug info hacking.

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