VirtualBox

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

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

IPRT: Fixed unused variable warnings.

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