VirtualBox

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

Last change on this file since 44042 was 44042, checked in by vboxsync, 12 years ago

shut up gcc

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