VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.2 KB
Line 
1/* $Id: ldrELFRelocatable.cpp.h 28800 2010-04-27 08:22:32Z 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 * Determin 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 adressed 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 * Trunacated 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
688/**
689 * The ELF module operations.
690 */
691static RTLDROPS RTLDRELF_MID(s_rtldrElf,Ops) =
692{
693#if ELF_MODE == 32
694 "elf32",
695#elif ELF_MODE == 64
696 "elf64",
697#endif
698 RTLDRELF_NAME(Close),
699 NULL, /* Get Symbol */
700 RTLDRELF_NAME(Done),
701 RTLDRELF_NAME(EnumSymbols),
702 /* ext: */
703 RTLDRELF_NAME(GetImageSize),
704 RTLDRELF_NAME(GetBits),
705 RTLDRELF_NAME(Relocate),
706 RTLDRELF_NAME(GetSymbolEx),
707 0
708};
709
710
711
712/**
713 * Validates the ELF header.
714 *
715 * @returns iprt status code.
716 * @param pEhdr Pointer to the ELF header.
717 * @param pszLogName The log name.
718 * @param cbRawImage The size of the raw image.
719 */
720static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, const char *pszLogName, uint64_t cbRawImage, PRTLDRARCH penmArch)
721{
722 Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
723 "RTLdrELF: e_type: " FMT_ELF_HALF "\n"
724 "RTLdrELF: e_version: " FMT_ELF_HALF "\n"
725 "RTLdrELF: e_entry: " FMT_ELF_ADDR "\n"
726 "RTLdrELF: e_phoff: " FMT_ELF_OFF "\n"
727 "RTLdrELF: e_shoff: " FMT_ELF_OFF "\n"
728 "RTLdrELF: e_flags: " FMT_ELF_WORD "\n"
729 "RTLdrELF: e_ehsize: " FMT_ELF_HALF "\n"
730 "RTLdrELF: e_phentsize: " FMT_ELF_HALF "\n"
731 "RTLdrELF: e_phnum: " FMT_ELF_HALF "\n"
732 "RTLdrELF: e_shentsize: " FMT_ELF_HALF "\n"
733 "RTLdrELF: e_shnum: " FMT_ELF_HALF "\n"
734 "RTLdrELF: e_shstrndx: " FMT_ELF_HALF "\n",
735 RT_ELEMENTS(pEhdr->e_ident), &pEhdr->e_ident[0], pEhdr->e_type, pEhdr->e_version,
736 pEhdr->e_entry, pEhdr->e_phoff, pEhdr->e_shoff,pEhdr->e_flags, pEhdr->e_ehsize, pEhdr->e_phentsize,
737 pEhdr->e_phnum, pEhdr->e_shentsize, pEhdr->e_shnum, pEhdr->e_shstrndx));
738
739 if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
740 || pEhdr->e_ident[EI_MAG1] != ELFMAG1
741 || pEhdr->e_ident[EI_MAG2] != ELFMAG2
742 || pEhdr->e_ident[EI_MAG3] != ELFMAG3
743 )
744 {
745 Log(("RTLdrELF: %s: Invalid ELF magic (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
746 return VERR_BAD_EXE_FORMAT;
747 }
748 if (pEhdr->e_ident[EI_CLASS] != RTLDRELF_SUFF(ELFCLASS))
749 {
750 Log(("RTLdrELF: %s: Invalid ELF class (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
751 return VERR_BAD_EXE_FORMAT;
752 }
753 if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
754 {
755 Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", pEhdr->e_ident[EI_DATA]));
756 return VERR_LDRELF_ODD_ENDIAN;
757 }
758 if (pEhdr->e_version != EV_CURRENT)
759 {
760 Log(("RTLdrELF: %s: ELF version %x is unsupported\n", pEhdr->e_version));
761 return VERR_LDRELF_VERSION;
762 }
763
764 if (sizeof(Elf_Ehdr) != pEhdr->e_ehsize)
765 {
766 Log(("RTLdrELF: %s: Elf header e_ehsize is %d expected %d!\n",
767 pszLogName, pEhdr->e_ehsize, sizeof(Elf_Ehdr)));
768 return VERR_BAD_EXE_FORMAT;
769 }
770 if ( sizeof(Elf_Phdr) != pEhdr->e_phentsize
771 && ( pEhdr->e_phnum != 0
772 || pEhdr->e_type == ET_DYN))
773 {
774 Log(("RTLdrELF: %s: Elf header e_phentsize is %d expected %d!\n",
775 pszLogName, pEhdr->e_phentsize, sizeof(Elf_Phdr)));
776 return VERR_BAD_EXE_FORMAT;
777 }
778 if (sizeof(Elf_Shdr) != pEhdr->e_shentsize)
779 {
780 Log(("RTLdrELF: %s: Elf header e_shentsize is %d expected %d!\n",
781 pszLogName, pEhdr->e_shentsize, sizeof(Elf_Shdr)));
782 return VERR_BAD_EXE_FORMAT;
783 }
784
785 switch (pEhdr->e_type)
786 {
787 case ET_REL:
788 break;
789 case ET_EXEC:
790 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pszLogName));
791 return VERR_LDRELF_EXEC;
792 case ET_DYN:
793 Log(("RTLdrELF: %s: Dynamic images are not supported yet!\n", pszLogName));
794 return VERR_LDRELF_DYN;
795 default:
796 Log(("RTLdrELF: %s: image type %#x is not supported!\n", pszLogName, pEhdr->e_type));
797 return VERR_BAD_EXE_FORMAT;
798 }
799
800 switch (pEhdr->e_machine)
801 {
802#if ELF_MODE == 32
803 case EM_386:
804 case EM_486:
805 *penmArch = RTLDRARCH_X86_32;
806 break;
807#elif ELF_MODE == 64
808 case EM_X86_64:
809 *penmArch = RTLDRARCH_AMD64;
810 break;
811#endif
812 default:
813 Log(("RTLdrELF: %s: machine type %u is not supported!\n", pEhdr->e_machine));
814 return VERR_LDRELF_MACHINE;
815 }
816
817 if ( pEhdr->e_phoff < pEhdr->e_ehsize
818 && !(pEhdr->e_phoff && pEhdr->e_phnum)
819 && pEhdr->e_phnum)
820 {
821 Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n",
822 pszLogName, pEhdr->e_phoff));
823 return VERR_BAD_EXE_FORMAT;
824 }
825 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
826 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
827 {
828 Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
829 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum));
830 return VERR_BAD_EXE_FORMAT;
831 }
832
833
834 if ( pEhdr->e_shoff < pEhdr->e_ehsize
835 && !(pEhdr->e_shoff && pEhdr->e_shnum))
836 {
837 Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n",
838 pszLogName, pEhdr->e_shoff));
839 return VERR_BAD_EXE_FORMAT;
840 }
841 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
842 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
843 {
844 Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
845 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum));
846 return VERR_BAD_EXE_FORMAT;
847 }
848
849 return VINF_SUCCESS;
850}
851
852/**
853 * Gets the section header name.
854 *
855 * @returns pszName.
856 * @param pReader The loader reader instance.
857 * @param pEhdr The elf header.
858 * @param offName The offset of the section header name.
859 * @param pszName Where to store the name.
860 * @param cbName The size of the buffer pointed to by pszName.
861 */
862const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
863{
864 RTFOFF off = pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset + offName;
865 int rc = pModElf->pReader->pfnRead(pModElf->pReader, pszName, cbName - 1, off);
866 if (RT_FAILURE(rc))
867 {
868 /* read by for byte. */
869 for (unsigned i = 0; i < cbName; i++, off++)
870 {
871 rc = pModElf->pReader->pfnRead(pModElf->pReader, pszName + i, 1, off);
872 if (RT_FAILURE(rc))
873 {
874 pszName[i] = '\0';
875 break;
876 }
877 }
878 }
879
880 pszName[cbName - 1] = '\0';
881 return pszName;
882}
883
884
885/**
886 * Validates a section header.
887 *
888 * @returns iprt status code.
889 * @param pModElf Pointer to the module structure.
890 * @param iShdr The index of section header which should be validated.
891 * The section headers are found in the pModElf->paShdrs array.
892 * @param pszLogName The log name.
893 * @param cbRawImage The size of the raw image.
894 */
895static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, const char *pszLogName, RTFOFF cbRawImage)
896{
897 const Elf_Shdr *pShdr = &pModElf->paShdrs[iShdr];
898 char szSectionName[80]; NOREF(szSectionName);
899 Log3(("RTLdrELF: Section Header #%d:\n"
900 "RTLdrELF: sh_name: " FMT_ELF_WORD " - %s\n"
901 "RTLdrELF: sh_type: " FMT_ELF_WORD " (%s)\n"
902 "RTLdrELF: sh_flags: " FMT_ELF_XWORD "\n"
903 "RTLdrELF: sh_addr: " FMT_ELF_ADDR "\n"
904 "RTLdrELF: sh_offset: " FMT_ELF_OFF "\n"
905 "RTLdrELF: sh_size: " FMT_ELF_XWORD "\n"
906 "RTLdrELF: sh_link: " FMT_ELF_WORD "\n"
907 "RTLdrELF: sh_info: " FMT_ELF_WORD "\n"
908 "RTLdrELF: sh_addralign: " FMT_ELF_XWORD "\n"
909 "RTLdrELF: sh_entsize: " FMT_ELF_XWORD "\n",
910 iShdr,
911 pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
912 pShdr->sh_type, rtldrElfGetShdrType(pShdr->sh_type), pShdr->sh_flags, pShdr->sh_addr,
913 pShdr->sh_offset, pShdr->sh_size, pShdr->sh_link, pShdr->sh_info, pShdr->sh_addralign,
914 pShdr->sh_entsize));
915
916 if (pShdr->sh_link >= pModElf->Ehdr.e_shnum)
917 {
918 Log(("RTLdrELF: %s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!\n",
919 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
920 return VERR_BAD_EXE_FORMAT;
921 }
922
923 switch (pShdr->sh_type)
924 {
925 /** @todo find specs and check up which sh_info fields indicates section table entries */
926 case 12301230:
927 if (pShdr->sh_info >= pModElf->Ehdr.e_shnum)
928 {
929 Log(("RTLdrELF: %s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!\n",
930 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
931 return VERR_BAD_EXE_FORMAT;
932 }
933 break;
934
935 case SHT_NULL:
936 case SHT_PROGBITS:
937 case SHT_SYMTAB:
938 case SHT_STRTAB:
939 case SHT_RELA:
940 case SHT_HASH:
941 case SHT_DYNAMIC:
942 case SHT_NOTE:
943 case SHT_NOBITS:
944 case SHT_REL:
945 case SHT_SHLIB:
946 case SHT_DYNSYM:
947 /*
948 * For these types sh_info doesn't have any special meaning, or anything which
949 * we need/can validate now.
950 */
951 break;
952
953
954 default:
955 Log(("RTLdrELF: %s: Warning, unknown type %d!\n", pszLogName, pShdr->sh_type));
956 break;
957 }
958
959 if ( pShdr->sh_type != SHT_NOBITS
960 && pShdr->sh_size)
961 {
962 RTFOFF offEnd = pShdr->sh_offset + pShdr->sh_size;
963 if ( offEnd > cbRawImage
964 || offEnd < (RTFOFF)pShdr->sh_offset)
965 {
966 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",
967 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, offEnd, cbRawImage));
968 return VERR_BAD_EXE_FORMAT;
969 }
970 if (pShdr->sh_offset < sizeof(Elf_Ehdr))
971 {
972 Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!\n",
973 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, cbRawImage));
974 return VERR_BAD_EXE_FORMAT;
975 }
976 }
977
978 return VINF_SUCCESS;
979}
980
981
982
983/**
984 * Opens an ELF image, fixed bitness.
985 *
986 * @returns iprt status code.
987 * @param pReader The loader reader instance which will provide the raw image bits.
988 * @param fFlags Reserved, MBZ.
989 * @param enmArch Architecture specifier.
990 * @param phLdrMod Where to store the handle.
991 */
992static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
993{
994 const char *pszLogName = pReader->pfnLogName(pReader);
995 RTFOFF cbRawImage = pReader->pfnSize(pReader);
996
997 /*
998 * Create the loader module instance.
999 */
1000 PRTLDRMODELF pModElf = (PRTLDRMODELF)RTMemAllocZ(sizeof(*pModElf));
1001 if (!pModElf)
1002 return VERR_NO_MEMORY;
1003
1004 pModElf->Core.u32Magic = RTLDRMOD_MAGIC;
1005 pModElf->Core.eState = LDR_STATE_INVALID;
1006 pModElf->pReader = pReader;
1007 //pModElf->pvBits = NULL;
1008 //pModElf->Ehdr = {0};
1009 //pModElf->paShdrs = NULL;
1010 //pModElf->paSyms = NULL;
1011 pModElf->iSymSh = ~0U;
1012 pModElf->cSyms = 0;
1013 pModElf->iStrSh = ~0U;
1014 pModElf->cbStr = 0;
1015 pModElf->cbImage = 0;
1016 //pModElf->pStr = NULL;
1017
1018 /*
1019 * Read and validate the ELF header and match up the CPU architecture.
1020 */
1021 int rc = pReader->pfnRead(pReader, &pModElf->Ehdr, sizeof(pModElf->Ehdr), 0);
1022 if (RT_SUCCESS(rc))
1023 {
1024 RTLDRARCH enmArchImage = RTLDRARCH_INVALID; /* shut up gcc */
1025 rc = RTLDRELF_NAME(ValidateElfHeader)(&pModElf->Ehdr, pszLogName, cbRawImage, &enmArchImage);
1026 if (RT_SUCCESS(rc))
1027 {
1028 if ( enmArch != RTLDRARCH_WHATEVER
1029 && enmArch != enmArchImage)
1030 rc = VERR_LDR_ARCH_MISMATCH;
1031 }
1032 }
1033 if (RT_SUCCESS(rc))
1034 {
1035 /*
1036 * Read the section headers.
1037 */
1038 Elf_Shdr *paShdrs = (Elf_Shdr *)RTMemAlloc(pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr));
1039 if (paShdrs)
1040 {
1041 pModElf->paShdrs = paShdrs;
1042 rc = pReader->pfnRead(pReader, paShdrs, pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr),
1043 pModElf->Ehdr.e_shoff);
1044 if (RT_SUCCESS(rc))
1045 {
1046 /*
1047 * Validate the section headers, allocate memory for the sections (determin the image size),
1048 * and find relevant sections.
1049 */
1050 for (unsigned i = 0; i < pModElf->Ehdr.e_shnum; i++)
1051 {
1052 rc = RTLDRELF_NAME(ValidateSectionHeader)(pModElf, i, pszLogName, cbRawImage);
1053 if (RT_FAILURE(rc))
1054 break;
1055
1056 /* Allocate memory addresses for the section. */
1057 if (paShdrs[i].sh_flags & SHF_ALLOC)
1058 {
1059 paShdrs[i].sh_addr = paShdrs[i].sh_addralign
1060 ? RT_ALIGN_T(pModElf->cbImage, paShdrs[i].sh_addralign, Elf_Addr)
1061 : (Elf_Addr)pModElf->cbImage;
1062 pModElf->cbImage = (size_t)paShdrs[i].sh_addr + (size_t)paShdrs[i].sh_size;
1063 AssertMsgReturn(pModElf->cbImage == paShdrs[i].sh_addr + paShdrs[i].sh_size,
1064 (FMT_ELF_ADDR "\n", paShdrs[i].sh_addr + paShdrs[i].sh_size),
1065 VERR_IMAGE_TOO_BIG);
1066 Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
1067 }
1068
1069 /* We're looking for symbol tables. */
1070 if (paShdrs[i].sh_type == SHT_SYMTAB)
1071 {
1072 if (pModElf->iSymSh != ~0U)
1073 {
1074 Log(("RTLdrElf: %s: Multiple symbol tabs! iSymSh=%d i=%d\n", pszLogName, pModElf->iSymSh, i));
1075 rc = VERR_LDRELF_MULTIPLE_SYMTABS;
1076 break;
1077 }
1078 pModElf->iSymSh = i;
1079 pModElf->cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
1080 AssertReturn(pModElf->cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), VERR_IMAGE_TOO_BIG);
1081 pModElf->iStrSh = paShdrs[i].sh_link;
1082 pModElf->cbStr = (unsigned)paShdrs[pModElf->iStrSh].sh_size;
1083 AssertReturn(pModElf->cbStr == paShdrs[pModElf->iStrSh].sh_size, VERR_IMAGE_TOO_BIG);
1084 }
1085 } /* for each section header */
1086
1087 Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx\n",
1088 pModElf->iSymSh, pModElf->cSyms, pModElf->iStrSh, pModElf->cbStr, rc, pModElf->cbImage));
1089
1090 /*
1091 * Are the section headers fine?
1092 * We require there to be symbol & string tables (at least for the time being).
1093 */
1094 if ( pModElf->iSymSh == ~0U
1095 || pModElf->iStrSh == ~0U)
1096 rc = VERR_LDRELF_NO_SYMBOL_OR_NO_STRING_TABS;
1097 if (RT_SUCCESS(rc))
1098 {
1099 pModElf->Core.pOps = &RTLDRELF_MID(s_rtldrElf,Ops);
1100 pModElf->Core.eState = LDR_STATE_OPENED;
1101 *phLdrMod = &pModElf->Core;
1102
1103 LogFlow(("%s: %s: returns VINF_SUCCESS *phLdrMod=%p\n", __FUNCTION__, pszLogName, *phLdrMod));
1104 return VINF_SUCCESS;
1105 }
1106 }
1107
1108 RTMemFree(paShdrs);
1109 }
1110 else
1111 rc = VERR_NO_MEMORY;
1112 }
1113
1114 RTMemFree(pModElf);
1115 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
1116 return rc;
1117}
1118
1119
1120
1121
1122/*******************************************************************************
1123* Cleanup Constants And Macros *
1124*******************************************************************************/
1125#undef RTLDRELF_NAME
1126#undef RTLDRELF_SUFF
1127#undef RTLDRELF_MID
1128
1129#undef FMT_ELF_ADDR
1130#undef FMT_ELF_HALF
1131#undef FMT_ELF_SHALF
1132#undef FMT_ELF_OFF
1133#undef FMT_ELF_SIZE
1134#undef FMT_ELF_SWORD
1135#undef FMT_ELF_WORD
1136#undef FMT_ELF_XWORD
1137#undef FMT_ELF_SXWORD
1138
1139#undef Elf_Ehdr
1140#undef Elf_Phdr
1141#undef Elf_Shdr
1142#undef Elf_Sym
1143#undef Elf_Rel
1144#undef Elf_Rela
1145#undef Elf_Reloc
1146#undef Elf_Nhdr
1147#undef Elf_Dyn
1148
1149#undef Elf_Addr
1150#undef Elf_Half
1151#undef Elf_Off
1152#undef Elf_Size
1153#undef Elf_Sword
1154#undef Elf_Word
1155
1156#undef RTLDRMODELF
1157#undef PRTLDRMODELF
1158
1159#undef ELF_R_SYM
1160#undef ELF_R_TYPE
1161#undef ELF_R_INFO
1162
1163#undef ELF_ST_BIND
1164
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