VirtualBox

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

Last change on this file since 38515 was 38515, checked in by vboxsync, 14 years ago

IPRT: Working on debug info again.

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

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