VirtualBox

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

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

IPRT: -Wunused-parameter.

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