VirtualBox

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

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

grumble.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 59.3 KB
Line 
1/* $Id: ldrELFRelocatable.cpp.h 46151 2013-05-17 19:28:09Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Template for ELF Relocatable Images.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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 readonly mapping of the image bits.
96 * This mapping is provided by the pReader. */
97 const void *pvBits;
98
99 /** The ELF header. */
100 Elf_Ehdr Ehdr;
101 /** Pointer to our copy of the section headers.
102 * The virtual addresses in this array is the 0 based assignments we've given the image.
103 * Not valid if the image is DONE. */
104 Elf_Shdr *paShdrs;
105 /** Unmodified section headers (allocated after paShdrs, so no need to free).
106 * Not valid if the image is DONE. */
107 Elf_Shdr const *paOrgShdrs;
108 /** The size of the loaded image. */
109 size_t cbImage;
110
111 /** The symbol section index. */
112 unsigned iSymSh;
113 /** Number of symbols in the table. */
114 unsigned cSyms;
115 /** Pointer to symbol table within RTLDRMODELF::pvBits. */
116 const Elf_Sym *paSyms;
117
118 /** The string section index. */
119 unsigned iStrSh;
120 /** Size of the string table. */
121 unsigned cbStr;
122 /** Pointer to string table within RTLDRMODELF::pvBits. */
123 const char *pStr;
124
125 /** Size of the section header string table. */
126 unsigned cbShStr;
127 /** Pointer to section header string table within RTLDRMODELF::pvBits. */
128 const char *pShStr;
129} RTLDRMODELF, *PRTLDRMODELF;
130
131
132/**
133 * Maps the image bits into memory and resolve pointers into it.
134 *
135 * @returns iprt status code.
136 * @param pModElf The ELF loader module instance data.
137 * @param fNeedsBits Set if we actually need the pvBits member.
138 * If we don't, we can simply read the string and symbol sections, thus saving memory.
139 */
140static int RTLDRELF_NAME(MapBits)(PRTLDRMODELF pModElf, bool fNeedsBits)
141{
142 NOREF(fNeedsBits);
143 if (pModElf->pvBits)
144 return VINF_SUCCESS;
145 int rc = pModElf->Core.pReader->pfnMap(pModElf->Core.pReader, &pModElf->pvBits);
146 if (RT_SUCCESS(rc))
147 {
148 const uint8_t *pu8 = (const uint8_t *)pModElf->pvBits;
149 if (pModElf->iSymSh != ~0U)
150 pModElf->paSyms = (const Elf_Sym *)(pu8 + pModElf->paShdrs[pModElf->iSymSh].sh_offset);
151 if (pModElf->iStrSh != ~0U)
152 pModElf->pStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->iStrSh].sh_offset);
153 pModElf->pShStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset);
154 }
155 return rc;
156}
157
158
159/**
160 * Get the symbol and symbol value.
161 *
162 * @returns iprt status code.
163 * @param pModElf The ELF loader module instance data.
164 * @param BaseAddr The base address which the module is being fixedup to.
165 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
166 * @param pvUser User argument to pass to the callback.
167 * @param iSym The symbol to get.
168 * @param ppSym Where to store the symbol pointer on success. (read only)
169 * @param pSymValue Where to store the symbol value on success.
170 */
171static int RTLDRELF_NAME(Symbol)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
172 Elf_Size iSym, const Elf_Sym **ppSym, Elf_Addr *pSymValue)
173{
174 /*
175 * Validate and find the symbol.
176 */
177 if (iSym >= pModElf->cSyms)
178 {
179 AssertMsgFailed(("iSym=%d is an invalid symbol index!\n", iSym));
180 return VERR_LDRELF_INVALID_SYMBOL_INDEX;
181 }
182 const Elf_Sym *pSym = &pModElf->paSyms[iSym];
183 *ppSym = pSym;
184
185 if (pSym->st_name >= pModElf->cbStr)
186 {
187 AssertMsgFailed(("iSym=%d st_name=%d str sh_size=%d\n", iSym, pSym->st_name, pModElf->cbStr));
188 return VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET;
189 }
190 const char *pszName = ELF_STR(pModElf, pSym->st_name);
191
192 /*
193 * Determine the symbol value.
194 *
195 * Symbols needs different treatment depending on which section their are in.
196 * Undefined and absolute symbols goes into special non-existing sections.
197 */
198 switch (pSym->st_shndx)
199 {
200 /*
201 * Undefined symbol, needs resolving.
202 *
203 * Since ELF has no generic concept of importing from specific module (the OS/2 ELF format
204 * has but that's a OS extension and only applies to programs and dlls), we'll have to ask
205 * the resolver callback to do a global search.
206 */
207 case SHN_UNDEF:
208 {
209 /* Try to resolve the symbol. */
210 RTUINTPTR Value;
211 int rc = pfnGetImport(&pModElf->Core, "", pszName, ~0, &Value, pvUser);
212 if (RT_FAILURE(rc))
213 {
214 AssertMsgFailed(("Failed to resolve '%s' rc=%Rrc\n", pszName, rc));
215 return rc;
216 }
217 *pSymValue = (Elf_Addr)Value;
218 if ((RTUINTPTR)*pSymValue != Value)
219 {
220 AssertMsgFailed(("Symbol value overflowed! '%s'\n", pszName));
221 return VERR_SYMBOL_VALUE_TOO_BIG;
222 }
223
224 Log2(("rtldrELF: #%-3d - UNDEF " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
225 break;
226 }
227
228 /*
229 * Absolute symbols needs no fixing since they are, well, absolute.
230 */
231 case SHN_ABS:
232 *pSymValue = pSym->st_value;
233 Log2(("rtldrELF: #%-3d - ABS " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
234 break;
235
236 /*
237 * All other symbols are addressed relative to their section and need to be fixed up.
238 */
239 default:
240 if (pSym->st_shndx >= pModElf->Ehdr.e_shnum)
241 {
242 /* what about common symbols? */
243 AssertMsg(pSym->st_shndx < pModElf->Ehdr.e_shnum,
244 ("iSym=%d st_shndx=%d e_shnum=%d pszName=%s\n", iSym, pSym->st_shndx, pModElf->Ehdr.e_shnum, pszName));
245 return VERR_BAD_EXE_FORMAT;
246 }
247 *pSymValue = pSym->st_value + pModElf->paShdrs[pSym->st_shndx].sh_addr + BaseAddr;
248 Log2(("rtldrELF: #%-3d - %5d " FMT_ELF_ADDR " '%s'\n", iSym, pSym->st_shndx, *pSymValue, pszName));
249 break;
250 }
251
252 return VINF_SUCCESS;
253}
254
255
256/**
257 * Applies the fixups for a sections.
258 *
259 * @returns iprt status code.
260 * @param pModElf The ELF loader module instance data.
261 * @param BaseAddr The base address which the module is being fixedup to.
262 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
263 * @param pvUser User argument to pass to the callback.
264 * @param SecAddr The section address. This is the address the relocations are relative to.
265 * @param cbSec The section size. The relocations must be inside this.
266 * @param pu8SecBaseR Where we read section bits from.
267 * @param pu8SecBaseW Where we write section bits to.
268 * @param pvRelocs Pointer to where we read the relocations from.
269 * @param cbRelocs Size of the relocations.
270 */
271static int RTLDRELF_NAME(RelocateSection)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
272 const Elf_Addr SecAddr, Elf_Size cbSec, const uint8_t *pu8SecBaseR, uint8_t *pu8SecBaseW,
273 const void *pvRelocs, Elf_Size cbRelocs)
274{
275#if ELF_MODE != 32
276 NOREF(pu8SecBaseR);
277#endif
278
279 /*
280 * Iterate the relocations.
281 * The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
282 */
283 const Elf_Reloc *paRels = (const Elf_Reloc *)pvRelocs;
284 const unsigned iRelMax = (unsigned)(cbRelocs / sizeof(paRels[0]));
285 AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])), VERR_IMAGE_TOO_BIG);
286 for (unsigned iRel = 0; iRel < iRelMax; iRel++)
287 {
288 /*
289 * Get the symbol.
290 */
291 const Elf_Sym *pSym = NULL; /* shut up gcc */
292 Elf_Addr SymValue = 0; /* shut up gcc-4 */
293 int rc = RTLDRELF_NAME(Symbol)(pModElf, BaseAddr, pfnGetImport, pvUser, ELF_R_SYM(paRels[iRel].r_info), &pSym, &SymValue);
294 if (RT_FAILURE(rc))
295 return rc;
296
297 Log3(("rtldrELF: " FMT_ELF_ADDR " %02x %06x - " FMT_ELF_ADDR " %3d %02x %s\n",
298 paRels[iRel].r_offset, ELF_R_TYPE(paRels[iRel].r_info), (unsigned)ELF_R_SYM(paRels[iRel].r_info),
299 SymValue, (unsigned)pSym->st_shndx, pSym->st_info, ELF_STR(pModElf, pSym->st_name)));
300
301 /*
302 * Apply the fixup.
303 */
304 AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec), VERR_LDRELF_INVALID_RELOCATION_OFFSET);
305#if ELF_MODE == 32
306 const Elf_Addr *pAddrR = (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset); /* Where to read the addend. */
307#endif
308 Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
309 switch (ELF_R_TYPE(paRels[iRel].r_info))
310 {
311#if ELF_MODE == 32
312 /*
313 * Absolute addressing.
314 */
315 case R_386_32:
316 {
317 const Elf_Addr Value = SymValue + *pAddrR;
318 *(uint32_t *)pAddrW = Value;
319 Log4((FMT_ELF_ADDR": R_386_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
320 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
321 break;
322 }
323
324 /*
325 * PC relative addressing.
326 */
327 case R_386_PC32:
328 {
329 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
330 const Elf_Addr Value = SymValue + *(uint32_t *)pAddrR - SourceAddr;
331 *(uint32_t *)pAddrW = Value;
332 Log4((FMT_ELF_ADDR": R_386_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
333 SourceAddr, Value, SymValue));
334 break;
335 }
336
337 /* ignore */
338 case R_386_NONE:
339 break;
340
341#elif ELF_MODE == 64
342
343 /*
344 * Absolute addressing
345 */
346 case R_X86_64_64:
347 {
348 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
349 *(uint64_t *)pAddrW = Value;
350 Log4((FMT_ELF_ADDR": R_X86_64_64 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
351 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
352 break;
353 }
354
355 /*
356 * Truncated 32-bit value (zero-extendedable to the 64-bit value).
357 */
358 case R_X86_64_32:
359 {
360 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
361 *(uint32_t *)pAddrW = (uint32_t)Value;
362 Log4((FMT_ELF_ADDR": R_X86_64_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
363 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
364 AssertMsgReturn((Elf_Addr)*(uint32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
365 break;
366 }
367
368 /*
369 * Truncated 32-bit value (sign-extendedable to the 64-bit value).
370 */
371 case R_X86_64_32S:
372 {
373 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
374 *(int32_t *)pAddrW = (int32_t)Value;
375 Log4((FMT_ELF_ADDR": R_X86_64_32S Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
376 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
377 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
378 break;
379 }
380
381 /*
382 * PC relative addressing.
383 */
384 case R_X86_64_PC32:
385 {
386 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
387 const Elf_Addr Value = SymValue + paRels[iRel].r_addend - SourceAddr;
388 *(int32_t *)pAddrW = (int32_t)Value;
389 Log4((FMT_ELF_ADDR": R_X86_64_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
390 SourceAddr, Value, SymValue));
391 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
392 break;
393 }
394
395 /* ignore */
396 case R_X86_64_NONE:
397 break;
398#endif
399
400 default:
401 AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
402 ELF_R_TYPE(paRels[iRel].r_info), iRel, iRelMax));
403 return VERR_LDRELF_RELOCATION_NOT_SUPPORTED;
404 }
405 }
406
407 return VINF_SUCCESS;
408}
409
410
411
412/** @copydoc RTLDROPS::pfnClose */
413static DECLCALLBACK(int) RTLDRELF_NAME(Close)(PRTLDRMODINTERNAL pMod)
414{
415 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
416
417 if (pModElf->paShdrs)
418 {
419 RTMemFree(pModElf->paShdrs);
420 pModElf->paShdrs = NULL;
421 }
422
423 pModElf->pvBits = NULL;
424
425 return VINF_SUCCESS;
426}
427
428
429/** @copydoc RTLDROPS::Done */
430static DECLCALLBACK(int) RTLDRELF_NAME(Done)(PRTLDRMODINTERNAL pMod)
431{
432 NOREF(pMod); /*PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;*/
433 /** @todo Have to think more about this .... */
434 return -1;
435}
436
437
438/** @copydoc RTLDROPS::EnumSymbols */
439static DECLCALLBACK(int) RTLDRELF_NAME(EnumSymbols)(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
440 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
441{
442 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
443 NOREF(pvBits);
444
445 /*
446 * Validate the input.
447 */
448 Elf_Addr BaseAddr = (Elf_Addr)BaseAddress;
449 AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
450
451 /*
452 * Make sure we've got the string and symbol tables. (We don't need the pvBits.)
453 */
454 int rc = RTLDRELF_NAME(MapBits)(pModElf, false);
455 if (RT_FAILURE(rc))
456 return rc;
457
458 /*
459 * Enumerate the symbol table.
460 */
461 const Elf_Sym *paSyms = pModElf->paSyms;
462 unsigned cSyms = pModElf->cSyms;
463 for (unsigned iSym = 1; iSym < cSyms; iSym++)
464 {
465 /*
466 * Skip imports (undefined).
467 */
468 if (paSyms[iSym].st_shndx != SHN_UNDEF)
469 {
470 /*
471 * Calc value and get name.
472 */
473 Elf_Addr Value;
474 if (paSyms[iSym].st_shndx == SHN_ABS)
475 /* absolute symbols are not subject to any relocation. */
476 Value = paSyms[iSym].st_value;
477 else if (paSyms[iSym].st_shndx < pModElf->Ehdr.e_shnum)
478 /* relative to the section. */
479 Value = BaseAddr + paSyms[iSym].st_value + pModElf->paShdrs[paSyms[iSym].st_shndx].sh_addr;
480 else
481 {
482 AssertMsgFailed(("Arg! paSyms[%u].st_shndx=" FMT_ELF_HALF "\n", iSym, paSyms[iSym].st_shndx));
483 return VERR_BAD_EXE_FORMAT;
484 }
485 const char *pszName = ELF_STR(pModElf, paSyms[iSym].st_name);
486 if ( (pszName && *pszName)
487 && ( (fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)
488 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL)
489 )
490 {
491 /*
492 * Call back.
493 */
494 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
495 rc = pfnCallback(pMod, pszName, ~0, (RTUINTPTR)Value, pvUser);
496 if (rc)
497 return rc;
498 }
499 }
500 }
501
502 return VINF_SUCCESS;
503}
504
505
506/** @copydoc RTLDROPS::GetImageSize */
507static DECLCALLBACK(size_t) RTLDRELF_NAME(GetImageSize)(PRTLDRMODINTERNAL pMod)
508{
509 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
510
511 return pModElf->cbImage;
512}
513
514
515/** @copydoc RTLDROPS::GetBits */
516static DECLCALLBACK(int) RTLDRELF_NAME(GetBits)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
517{
518 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
519
520 /*
521 * This operation is currently only available on relocatable images.
522 */
523 switch (pModElf->Ehdr.e_type)
524 {
525 case ET_REL:
526 break;
527 case ET_EXEC:
528 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader)));
529 return VERR_LDRELF_EXEC;
530 case ET_DYN:
531 Log(("RTLdrELF: %s: Dynamic images are not supported yet!\n", pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader)));
532 return VERR_LDRELF_DYN;
533 default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
534 }
535
536 /*
537 * Load the bits into pvBits.
538 */
539 const Elf_Shdr *paShdrs = pModElf->paShdrs;
540 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
541 {
542 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
543 {
544 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);
545 switch (paShdrs[iShdr].sh_type)
546 {
547 case SHT_NOBITS:
548 memset((uint8_t *)pvBits + paShdrs[iShdr].sh_addr, 0, (size_t)paShdrs[iShdr].sh_size);
549 break;
550
551 case SHT_PROGBITS:
552 default:
553 {
554 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, (uint8_t *)pvBits + paShdrs[iShdr].sh_addr,
555 (size_t)paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset);
556 if (RT_FAILURE(rc))
557 {
558 Log(("RTLdrELF: %s: Read error when reading " FMT_ELF_SIZE " bytes at " FMT_ELF_OFF ", iShdr=%d\n",
559 pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader),
560 paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset, iShdr));
561 return rc;
562 }
563 }
564 }
565 }
566 }
567
568 /*
569 * Relocate the image.
570 */
571 return pModElf->Core.pOps->pfnRelocate(pMod, pvBits, BaseAddress, ~(RTUINTPTR)0, pfnGetImport, pvUser);
572}
573
574
575/** @copydoc RTLDROPS::Relocate */
576static DECLCALLBACK(int) RTLDRELF_NAME(Relocate)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress,
577 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
578{
579 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
580#ifdef LOG_ENABLED
581 const char *pszLogName = pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader);
582#endif
583 NOREF(OldBaseAddress);
584
585 /*
586 * This operation is currently only available on relocatable images.
587 */
588 switch (pModElf->Ehdr.e_type)
589 {
590 case ET_REL:
591 break;
592 case ET_EXEC:
593 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pszLogName));
594 return VERR_LDRELF_EXEC;
595 case ET_DYN:
596 Log(("RTLdrELF: %s: Dynamic images are not supported yet!\n", pszLogName));
597 return VERR_LDRELF_DYN;
598 default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
599 }
600
601 /*
602 * Validate the input.
603 */
604 Elf_Addr BaseAddr = (Elf_Addr)NewBaseAddress;
605 AssertMsgReturn((RTUINTPTR)BaseAddr == NewBaseAddress, ("#RTptr", NewBaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
606
607 /*
608 * Map the image bits if not already done and setup pointer into it.
609 */
610 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
611 if (RT_FAILURE(rc))
612 return rc;
613
614 /*
615 * Iterate the sections looking for interesting SHT_REL[A] sections.
616 * SHT_REL[A] sections have the section index of the section they contain fixups
617 * for in the sh_info member.
618 */
619 const Elf_Shdr *paShdrs = pModElf->paShdrs;
620 Log2(("rtLdrElf: %s: Fixing up image\n", pszLogName));
621 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
622 {
623 const Elf_Shdr *pShdrRel = &paShdrs[iShdr];
624
625 /*
626 * Skip sections without interest to us.
627 */
628#if ELF_MODE == 32
629 if (pShdrRel->sh_type != SHT_REL)
630#else
631 if (pShdrRel->sh_type != SHT_RELA)
632#endif
633 continue;
634 if (pShdrRel->sh_info >= pModElf->Ehdr.e_shnum)
635 continue;
636 const Elf_Shdr *pShdr = &paShdrs[pShdrRel->sh_info]; /* the section to fixup. */
637 if (!(pShdr->sh_flags & SHF_ALLOC))
638 continue;
639
640 /*
641 * Relocate the section.
642 */
643 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",
644 pszLogName, (int)pShdrRel->sh_info, ELF_SH_STR(pModElf, pShdr->sh_name), (int)pShdr->sh_info, (int)pShdr->sh_link,
645 iShdr, ELF_SH_STR(pModElf, pShdrRel->sh_name), (int)pShdrRel->sh_info, (int)pShdrRel->sh_link));
646
647 /** @todo Make RelocateSection a function pointer so we can select the one corresponding to the machine when opening the image. */
648 rc = RTLDRELF_NAME(RelocateSection)(pModElf, BaseAddr, pfnGetImport, pvUser,
649 pShdr->sh_addr,
650 pShdr->sh_size,
651 (const uint8_t *)pModElf->pvBits + pShdr->sh_offset,
652 (uint8_t *)pvBits + pShdr->sh_addr,
653 (const uint8_t *)pModElf->pvBits + pShdrRel->sh_offset,
654 pShdrRel->sh_size);
655 if (RT_FAILURE(rc))
656 return rc;
657 }
658 return VINF_SUCCESS;
659}
660
661
662/** @copydoc RTLDROPS::pfnGetSymbolEx */
663static DECLCALLBACK(int) RTLDRELF_NAME(GetSymbolEx)(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue)
664{
665 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
666 NOREF(pvBits);
667
668 /*
669 * Validate the input.
670 */
671 Elf_Addr BaseAddr = (Elf_Addr)BaseAddress;
672 AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("#RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
673
674 /*
675 * Map the image bits if not already done and setup pointer into it.
676 */
677 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
678 if (RT_FAILURE(rc))
679 return rc;
680
681 /*
682 * Calc all kinds of pointers before we start iterating the symbol table.
683 */
684 const char *pStr = pModElf->pStr;
685 const Elf_Sym *paSyms = pModElf->paSyms;
686 unsigned cSyms = pModElf->cSyms;
687 for (unsigned iSym = 1; iSym < cSyms; iSym++)
688 {
689 /* Undefined symbols are not exports, they are imports. */
690 if ( paSyms[iSym].st_shndx != SHN_UNDEF
691 && ( ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
692 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_WEAK))
693 {
694 /* Validate the name string and try match with it. */
695 if (paSyms[iSym].st_name < pModElf->cbStr)
696 {
697 if (!strcmp(pszSymbol, pStr + paSyms[iSym].st_name))
698 {
699 /* matched! */
700 Elf_Addr Value;
701 if (paSyms[iSym].st_shndx == SHN_ABS)
702 /* absolute symbols are not subject to any relocation. */
703 Value = paSyms[iSym].st_value;
704 else if (paSyms[iSym].st_shndx < pModElf->Ehdr.e_shnum)
705 /* relative to the section. */
706 Value = BaseAddr + paSyms[iSym].st_value + pModElf->paShdrs[paSyms[iSym].st_shndx].sh_addr;
707 else
708 {
709 AssertMsgFailed(("Arg. paSyms[iSym].st_shndx=%d\n", paSyms[iSym].st_shndx));
710 return VERR_BAD_EXE_FORMAT;
711 }
712 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
713 *pValue = (RTUINTPTR)Value;
714 return VINF_SUCCESS;
715 }
716 }
717 else
718 {
719 AssertMsgFailed(("String outside string table! iSym=%d paSyms[iSym].st_name=%#x\n", iSym, paSyms[iSym].st_name));
720 return VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET;
721 }
722 }
723 }
724
725 return VERR_SYMBOL_NOT_FOUND;
726}
727
728
729/** @copydoc RTLDROPS::pfnEnumDbgInfo */
730static DECLCALLBACK(int) RTLDRELF_NAME(EnumDbgInfo)(PRTLDRMODINTERNAL pMod, const void *pvBits,
731 PFNRTLDRENUMDBG pfnCallback, void *pvUser)
732{
733 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
734
735 /*
736 * Map the image bits if not already done and setup pointer into it.
737 */
738 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
739 if (RT_FAILURE(rc))
740 return rc;
741
742 /*
743 * Do the enumeration.
744 */
745 const Elf_Shdr *paShdrs = pModElf->paOrgShdrs;
746 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
747 {
748 /* Debug sections are expected to be PROGBITS and not allocated. */
749 if (paShdrs[iShdr].sh_type != SHT_PROGBITS)
750 continue;
751 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
752 continue;
753
754 RTLDRDBGINFO DbgInfo;
755 const char *pszSectName = ELF_SH_STR(pModElf, paShdrs[iShdr].sh_name);
756 if ( !strncmp(pszSectName, RT_STR_TUPLE(".debug_"))
757 || !strcmp(pszSectName, ".WATCOM_references") )
758 {
759 RT_ZERO(DbgInfo.u);
760 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF;
761 DbgInfo.pszExtFile = NULL;
762 DbgInfo.offFile = paShdrs[iShdr].sh_offset;
763 DbgInfo.cb = paShdrs[iShdr].sh_size;
764 DbgInfo.u.Dwarf.pszSection = pszSectName;
765 }
766 else if (!strcmp(pszSectName, ".gnu_debuglink"))
767 {
768 if ((paShdrs[iShdr].sh_size & 3) || paShdrs[iShdr].sh_size < 8)
769 return VERR_BAD_EXE_FORMAT;
770
771 RT_ZERO(DbgInfo.u);
772 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF_DWO;
773 DbgInfo.pszExtFile = (const char *)((uintptr_t)pModElf->pvBits + paShdrs[iShdr].sh_offset);
774 if (!RTStrEnd(DbgInfo.pszExtFile, paShdrs[iShdr].sh_size))
775 return VERR_BAD_EXE_FORMAT;
776 DbgInfo.u.Dwo.uCrc32 = *(uint32_t *)((uintptr_t)DbgInfo.pszExtFile + paShdrs[iShdr].sh_size - sizeof(uint32_t));
777 DbgInfo.offFile = -1;
778 DbgInfo.cb = 0;
779 }
780 else
781 continue;
782
783 DbgInfo.LinkAddress = NIL_RTLDRADDR;
784 DbgInfo.iDbgInfo = iShdr - 1;
785
786 rc = pfnCallback(pMod, &DbgInfo, pvUser);
787 if (rc != VINF_SUCCESS)
788 return rc;
789
790 }
791
792 return VINF_SUCCESS;
793}
794
795
796/**
797 * Helper that locates the first allocated section.
798 *
799 * @returns Pointer to the section header if found, NULL if none.
800 * @param pShdr The section header to start searching at.
801 * @param cLeft The number of section headers left to search. Can be 0.
802 */
803static const Elf_Shdr *RTLDRELF_NAME(GetFirstAllocatedSection)(const Elf_Shdr *pShdr, unsigned cLeft)
804{
805 while (cLeft-- > 0)
806 {
807 if (pShdr->sh_flags & SHF_ALLOC)
808 return pShdr;
809 pShdr++;
810 }
811 return NULL;
812}
813
814/** @copydoc RTLDROPS::pfnEnumSegments. */
815static DECLCALLBACK(int) RTLDRELF_NAME(EnumSegments)(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
816{
817 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
818
819 /*
820 * Map the image bits if not already done and setup pointer into it.
821 */
822 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
823 if (RT_FAILURE(rc))
824 return rc;
825
826 /*
827 * Do the enumeration.
828 */
829 const Elf_Shdr *paShdrs = pModElf->paShdrs;
830 const Elf_Shdr *paOrgShdrs = pModElf->paOrgShdrs;
831 for (unsigned iShdr = 1; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
832 {
833 RTLDRSEG Seg;
834 Seg.pchName = ELF_SH_STR(pModElf, paShdrs[iShdr].sh_name);
835 Seg.cchName = (uint32_t)strlen(Seg.pchName);
836 Seg.SelFlat = 0;
837 Seg.Sel16bit = 0;
838 Seg.fFlags = 0;
839 Seg.fProt = RTMEM_PROT_READ;
840 if (paShdrs[iShdr].sh_flags & SHF_WRITE)
841 Seg.fProt |= RTMEM_PROT_WRITE;
842 if (paShdrs[iShdr].sh_flags & SHF_EXECINSTR)
843 Seg.fProt |= RTMEM_PROT_EXEC;
844 Seg.cb = paShdrs[iShdr].sh_size;
845 Seg.Alignment = paShdrs[iShdr].sh_addralign;
846 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
847 {
848 Seg.LinkAddress = paOrgShdrs[iShdr].sh_addr;
849 Seg.RVA = paShdrs[iShdr].sh_addr;
850 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetFirstAllocatedSection)(&paShdrs[iShdr + 1],
851 pModElf->Ehdr.e_shnum - iShdr - 1);
852 Seg.cbMapped = pShdr2 ? pShdr2->sh_addr - paShdrs[iShdr].sh_addr : paShdrs[iShdr].sh_size;
853 }
854 else
855 {
856 Seg.LinkAddress = NIL_RTLDRADDR;
857 Seg.RVA = NIL_RTLDRADDR;
858 Seg.cbMapped = NIL_RTLDRADDR;
859 }
860 if (paShdrs[iShdr].sh_type != SHT_NOBITS)
861 {
862 Seg.offFile = paShdrs[iShdr].sh_offset;
863 Seg.cbFile = paShdrs[iShdr].sh_size;
864 }
865 else
866 {
867 Seg.offFile = -1;
868 Seg.cbFile = 0;
869 }
870
871 rc = pfnCallback(pMod, &Seg, pvUser);
872 if (rc != VINF_SUCCESS)
873 return rc;
874 }
875
876 return VINF_SUCCESS;
877}
878
879
880/** @copydoc RTLDROPS::pfnLinkAddressToSegOffset. */
881static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress,
882 uint32_t *piSeg, PRTLDRADDR poffSeg)
883{
884 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
885
886 const Elf_Shdr *pShdrEnd = NULL;
887 unsigned cLeft = pModElf->Ehdr.e_shnum - 1;
888 const Elf_Shdr *pShdr = &pModElf->paOrgShdrs[cLeft];
889 while (cLeft-- > 0)
890 {
891 if (pShdr->sh_flags & SHF_ALLOC)
892 {
893 RTLDRADDR offSeg = LinkAddress - pShdr->sh_addr;
894 if (offSeg < pShdr->sh_size)
895 {
896 *poffSeg = offSeg;
897 *piSeg = cLeft;
898 return VINF_SUCCESS;
899 }
900 if (offSeg == pShdr->sh_size)
901 pShdrEnd = pShdr;
902 }
903 pShdr--;
904 }
905
906 if (pShdrEnd)
907 {
908 *poffSeg = pShdrEnd->sh_size;
909 *piSeg = pShdrEnd - pModElf->paOrgShdrs - 1;
910 return VINF_SUCCESS;
911 }
912
913 return VERR_LDR_INVALID_LINK_ADDRESS;
914}
915
916
917/** @copydoc RTLDROPS::pfnLinkAddressToRva. */
918static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToRva)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
919{
920 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
921 uint32_t iSeg;
922 RTLDRADDR offSeg;
923 int rc = RTLDRELF_NAME(LinkAddressToSegOffset)(pMod, LinkAddress, &iSeg, &offSeg);
924 if (RT_SUCCESS(rc))
925 *pRva = pModElf->paShdrs[iSeg + 1].sh_addr + offSeg;
926 return rc;
927}
928
929
930/** @copydoc RTLDROPS::pfnSegOffsetToRva. */
931static DECLCALLBACK(int) RTLDRELF_NAME(SegOffsetToRva)(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg,
932 PRTLDRADDR pRva)
933{
934 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
935 if (iSeg >= pModElf->Ehdr.e_shnum - 1U)
936 return VERR_LDR_INVALID_SEG_OFFSET;
937
938 iSeg++; /* skip section 0 */
939 if (offSeg > pModElf->paShdrs[iSeg].sh_size)
940 {
941 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetFirstAllocatedSection)(&pModElf->paShdrs[iSeg + 1],
942 pModElf->Ehdr.e_shnum - iSeg - 1);
943 if ( !pShdr2
944 || offSeg > (pShdr2->sh_addr - pModElf->paShdrs[iSeg].sh_addr))
945 return VERR_LDR_INVALID_SEG_OFFSET;
946 }
947
948 if (!(pModElf->paShdrs[iSeg].sh_flags & SHF_ALLOC))
949 return VERR_LDR_INVALID_SEG_OFFSET;
950
951 *pRva = pModElf->paShdrs[iSeg].sh_addr;
952 return VINF_SUCCESS;
953}
954
955
956/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
957static DECLCALLBACK(int) RTLDRELF_NAME(RvaToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva,
958 uint32_t *piSeg, PRTLDRADDR poffSeg)
959{
960 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
961
962 Elf_Addr PrevAddr = 0;
963 unsigned cLeft = pModElf->Ehdr.e_shnum - 1;
964 const Elf_Shdr *pShdr = &pModElf->paShdrs[cLeft];
965 while (cLeft-- > 0)
966 {
967 if (pShdr->sh_flags & SHF_ALLOC)
968 {
969 Elf_Addr cbSeg = PrevAddr ? PrevAddr - pShdr->sh_addr : pShdr->sh_size;
970 RTLDRADDR offSeg = Rva - pShdr->sh_addr;
971 if (offSeg <= cbSeg)
972 {
973 *poffSeg = offSeg;
974 *piSeg = cLeft;
975 return VINF_SUCCESS;
976 }
977 PrevAddr = pShdr->sh_addr;
978 }
979 pShdr--;
980 }
981
982 return VERR_LDR_INVALID_RVA;
983}
984
985
986/** @callback_method_impl{FNRTLDRIMPORT, Stub used by ReadDbgInfo.} */
987static DECLCALLBACK(int) RTLDRELF_NAME(GetImportStubCallback)(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
988 unsigned uSymbol, PRTLDRADDR pValue, void *pvUser)
989{
990 return VERR_SYMBOL_NOT_FOUND;
991}
992
993
994/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
995static DECLCALLBACK(int) RTLDRELF_NAME(ReadDbgInfo)(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off,
996 size_t cb, void *pvBuf)
997{
998 PRTLDRMODELF pThis = (PRTLDRMODELF)pMod;
999
1000 /*
1001 * Input validation.
1002 */
1003 AssertReturn(iDbgInfo < pThis->Ehdr.e_shnum && iDbgInfo + 1 < pThis->Ehdr.e_shnum, VERR_INVALID_PARAMETER);
1004 iDbgInfo++;
1005 AssertReturn(!(pThis->paShdrs[iDbgInfo].sh_flags & SHF_ALLOC), VERR_INVALID_PARAMETER);
1006 AssertReturn(pThis->paShdrs[iDbgInfo].sh_type == SHT_PROGBITS, VERR_INVALID_PARAMETER);
1007 AssertReturn(pThis->paShdrs[iDbgInfo].sh_offset == (uint64_t)off, VERR_INVALID_PARAMETER);
1008 AssertReturn(pThis->paShdrs[iDbgInfo].sh_size == cb, VERR_INVALID_PARAMETER);
1009 RTFOFF cbRawImage = pThis->Core.pReader->pfnSize(pThis->Core.pReader);
1010 AssertReturn(cbRawImage >= 0, VERR_INVALID_PARAMETER);
1011 AssertReturn(off >= 0 && cb <= (uint64_t)cbRawImage && off + cb <= (uint64_t)cbRawImage, VERR_INVALID_PARAMETER);
1012
1013 /*
1014 * Read it from the file and look for fixup sections.
1015 */
1016 int rc;
1017 if (pThis->pvBits)
1018 memcpy(pvBuf, (const uint8_t *)pThis->pvBits + (size_t)off, cb);
1019 else
1020 {
1021 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off);
1022 if (RT_FAILURE(rc))
1023 return rc;
1024 }
1025
1026 uint32_t iRelocs = iDbgInfo + 1;
1027 if ( iRelocs >= pThis->Ehdr.e_shnum
1028 || pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1029 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1030 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA) )
1031 {
1032 iRelocs = 0;
1033 while ( iRelocs < pThis->Ehdr.e_shnum
1034 && ( pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1035 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1036 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA)) )
1037 iRelocs++;
1038 }
1039 if ( iRelocs < pThis->Ehdr.e_shnum
1040 && pThis->paShdrs[iRelocs].sh_size > 0)
1041 {
1042 /*
1043 * Load the relocations.
1044 */
1045 uint8_t *pbRelocsBuf = NULL;
1046 const uint8_t *pbRelocs;
1047 if (pThis->pvBits)
1048 pbRelocs = (const uint8_t *)pThis->pvBits + pThis->paShdrs[iRelocs].sh_offset;
1049 else
1050 {
1051 pbRelocs = pbRelocsBuf = (uint8_t *)RTMemTmpAlloc(pThis->paShdrs[iRelocs].sh_size);
1052 if (!pbRelocsBuf)
1053 return VERR_NO_TMP_MEMORY;
1054 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pbRelocsBuf,
1055 pThis->paShdrs[iRelocs].sh_size,
1056 pThis->paShdrs[iRelocs].sh_offset);
1057 if (RT_FAILURE(rc))
1058 {
1059 RTMemTmpFree(pbRelocsBuf);
1060 return rc;
1061 }
1062 }
1063
1064 /*
1065 * Apply the relocations.
1066 */
1067 rc = RTLDRELF_NAME(RelocateSection)(pThis, 0 /*BaseAddress*/,
1068 RTLDRELF_NAME(GetImportStubCallback), NULL /*pvUser*/,
1069 pThis->paShdrs[iDbgInfo].sh_addr,
1070 pThis->paShdrs[iDbgInfo].sh_size,
1071 (const uint8_t *)pvBuf,
1072 (uint8_t *)pvBuf,
1073 pbRelocs,
1074 pThis->paShdrs[iRelocs].sh_size);
1075 RTMemTmpFree(pbRelocsBuf);
1076 }
1077 else
1078 rc = VINF_SUCCESS;
1079 return rc;
1080}
1081
1082
1083
1084/**
1085 * The ELF module operations.
1086 */
1087static RTLDROPS RTLDRELF_MID(s_rtldrElf,Ops) =
1088{
1089#if ELF_MODE == 32
1090 "elf32",
1091#elif ELF_MODE == 64
1092 "elf64",
1093#endif
1094 RTLDRELF_NAME(Close),
1095 NULL, /* Get Symbol */
1096 RTLDRELF_NAME(Done),
1097 RTLDRELF_NAME(EnumSymbols),
1098 /* ext: */
1099 RTLDRELF_NAME(GetImageSize),
1100 RTLDRELF_NAME(GetBits),
1101 RTLDRELF_NAME(Relocate),
1102 RTLDRELF_NAME(GetSymbolEx),
1103 RTLDRELF_NAME(EnumDbgInfo),
1104 RTLDRELF_NAME(EnumSegments),
1105 RTLDRELF_NAME(LinkAddressToSegOffset),
1106 RTLDRELF_NAME(LinkAddressToRva),
1107 RTLDRELF_NAME(SegOffsetToRva),
1108 RTLDRELF_NAME(RvaToSegOffset),
1109 RTLDRELF_NAME(ReadDbgInfo),
1110 42
1111};
1112
1113
1114
1115/**
1116 * Validates the ELF header.
1117 *
1118 * @returns iprt status code.
1119 * @param pEhdr Pointer to the ELF header.
1120 * @param pszLogName The log name.
1121 * @param cbRawImage The size of the raw image.
1122 */
1123static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, const char *pszLogName, uint64_t cbRawImage,
1124 PRTLDRARCH penmArch)
1125{
1126 Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
1127 "RTLdrELF: e_type: " FMT_ELF_HALF "\n"
1128 "RTLdrELF: e_version: " FMT_ELF_HALF "\n"
1129 "RTLdrELF: e_entry: " FMT_ELF_ADDR "\n"
1130 "RTLdrELF: e_phoff: " FMT_ELF_OFF "\n"
1131 "RTLdrELF: e_shoff: " FMT_ELF_OFF "\n"
1132 "RTLdrELF: e_flags: " FMT_ELF_WORD "\n"
1133 "RTLdrELF: e_ehsize: " FMT_ELF_HALF "\n"
1134 "RTLdrELF: e_phentsize: " FMT_ELF_HALF "\n"
1135 "RTLdrELF: e_phnum: " FMT_ELF_HALF "\n"
1136 "RTLdrELF: e_shentsize: " FMT_ELF_HALF "\n"
1137 "RTLdrELF: e_shnum: " FMT_ELF_HALF "\n"
1138 "RTLdrELF: e_shstrndx: " FMT_ELF_HALF "\n",
1139 RT_ELEMENTS(pEhdr->e_ident), &pEhdr->e_ident[0], pEhdr->e_type, pEhdr->e_version,
1140 pEhdr->e_entry, pEhdr->e_phoff, pEhdr->e_shoff,pEhdr->e_flags, pEhdr->e_ehsize, pEhdr->e_phentsize,
1141 pEhdr->e_phnum, pEhdr->e_shentsize, pEhdr->e_shnum, pEhdr->e_shstrndx));
1142
1143 if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
1144 || pEhdr->e_ident[EI_MAG1] != ELFMAG1
1145 || pEhdr->e_ident[EI_MAG2] != ELFMAG2
1146 || pEhdr->e_ident[EI_MAG3] != ELFMAG3
1147 )
1148 {
1149 Log(("RTLdrELF: %s: Invalid ELF magic (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident)); NOREF(pszLogName);
1150 return VERR_BAD_EXE_FORMAT;
1151 }
1152 if (pEhdr->e_ident[EI_CLASS] != RTLDRELF_SUFF(ELFCLASS))
1153 {
1154 Log(("RTLdrELF: %s: Invalid ELF class (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
1155 return VERR_BAD_EXE_FORMAT;
1156 }
1157 if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
1158 {
1159 Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", pEhdr->e_ident[EI_DATA]));
1160 return VERR_LDRELF_ODD_ENDIAN;
1161 }
1162 if (pEhdr->e_version != EV_CURRENT)
1163 {
1164 Log(("RTLdrELF: %s: ELF version %x is unsupported\n", pEhdr->e_version));
1165 return VERR_LDRELF_VERSION;
1166 }
1167
1168 if (sizeof(Elf_Ehdr) != pEhdr->e_ehsize)
1169 {
1170 Log(("RTLdrELF: %s: Elf header e_ehsize is %d expected %d!\n",
1171 pszLogName, pEhdr->e_ehsize, sizeof(Elf_Ehdr)));
1172 return VERR_BAD_EXE_FORMAT;
1173 }
1174 if ( sizeof(Elf_Phdr) != pEhdr->e_phentsize
1175 && ( pEhdr->e_phnum != 0
1176 || pEhdr->e_type == ET_DYN))
1177 {
1178 Log(("RTLdrELF: %s: Elf header e_phentsize is %d expected %d!\n",
1179 pszLogName, pEhdr->e_phentsize, sizeof(Elf_Phdr)));
1180 return VERR_BAD_EXE_FORMAT;
1181 }
1182 if (sizeof(Elf_Shdr) != pEhdr->e_shentsize)
1183 {
1184 Log(("RTLdrELF: %s: Elf header e_shentsize is %d expected %d!\n",
1185 pszLogName, pEhdr->e_shentsize, sizeof(Elf_Shdr)));
1186 return VERR_BAD_EXE_FORMAT;
1187 }
1188
1189 switch (pEhdr->e_type)
1190 {
1191 case ET_REL:
1192 case ET_EXEC:
1193 case ET_DYN:
1194 break;
1195 default:
1196 Log(("RTLdrELF: %s: image type %#x is not supported!\n", pszLogName, pEhdr->e_type));
1197 return VERR_BAD_EXE_FORMAT;
1198 }
1199
1200 switch (pEhdr->e_machine)
1201 {
1202#if ELF_MODE == 32
1203 case EM_386:
1204 case EM_486:
1205 *penmArch = RTLDRARCH_X86_32;
1206 break;
1207#elif ELF_MODE == 64
1208 case EM_X86_64:
1209 *penmArch = RTLDRARCH_AMD64;
1210 break;
1211#endif
1212 default:
1213 Log(("RTLdrELF: %s: machine type %u is not supported!\n", pEhdr->e_machine));
1214 return VERR_LDRELF_MACHINE;
1215 }
1216
1217 if ( pEhdr->e_phoff < pEhdr->e_ehsize
1218 && !(pEhdr->e_phoff && pEhdr->e_phnum)
1219 && pEhdr->e_phnum)
1220 {
1221 Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n",
1222 pszLogName, pEhdr->e_phoff));
1223 return VERR_BAD_EXE_FORMAT;
1224 }
1225 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
1226 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
1227 {
1228 Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
1229 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum));
1230 return VERR_BAD_EXE_FORMAT;
1231 }
1232
1233
1234 if ( pEhdr->e_shoff < pEhdr->e_ehsize
1235 && !(pEhdr->e_shoff && pEhdr->e_shnum))
1236 {
1237 Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n",
1238 pszLogName, pEhdr->e_shoff));
1239 return VERR_BAD_EXE_FORMAT;
1240 }
1241 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
1242 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
1243 {
1244 Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
1245 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum));
1246 return VERR_BAD_EXE_FORMAT;
1247 }
1248
1249 if (pEhdr->e_shstrndx == 0 || pEhdr->e_shstrndx > pEhdr->e_shnum)
1250 {
1251 Log(("RTLdrELF: %s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF "\n",
1252 pszLogName, pEhdr->e_shstrndx, pEhdr->e_shnum));
1253 return VERR_BAD_EXE_FORMAT;
1254 }
1255
1256 return VINF_SUCCESS;
1257}
1258
1259/**
1260 * Gets the section header name.
1261 *
1262 * @returns pszName.
1263 * @param pEhdr The elf header.
1264 * @param offName The offset of the section header name.
1265 * @param pszName Where to store the name.
1266 * @param cbName The size of the buffer pointed to by pszName.
1267 */
1268const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
1269{
1270 RTFOFF off = pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset + offName;
1271 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName, cbName - 1, off);
1272 if (RT_FAILURE(rc))
1273 {
1274 /* read by for byte. */
1275 for (unsigned i = 0; i < cbName; i++, off++)
1276 {
1277 rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName + i, 1, off);
1278 if (RT_FAILURE(rc))
1279 {
1280 pszName[i] = '\0';
1281 break;
1282 }
1283 }
1284 }
1285
1286 pszName[cbName - 1] = '\0';
1287 return pszName;
1288}
1289
1290
1291/**
1292 * Validates a section header.
1293 *
1294 * @returns iprt status code.
1295 * @param pModElf Pointer to the module structure.
1296 * @param iShdr The index of section header which should be validated.
1297 * The section headers are found in the pModElf->paShdrs array.
1298 * @param pszLogName The log name.
1299 * @param cbRawImage The size of the raw image.
1300 */
1301static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, const char *pszLogName, RTFOFF cbRawImage)
1302{
1303 const Elf_Shdr *pShdr = &pModElf->paShdrs[iShdr];
1304 char szSectionName[80]; NOREF(szSectionName);
1305 Log3(("RTLdrELF: Section Header #%d:\n"
1306 "RTLdrELF: sh_name: " FMT_ELF_WORD " - %s\n"
1307 "RTLdrELF: sh_type: " FMT_ELF_WORD " (%s)\n"
1308 "RTLdrELF: sh_flags: " FMT_ELF_XWORD "\n"
1309 "RTLdrELF: sh_addr: " FMT_ELF_ADDR "\n"
1310 "RTLdrELF: sh_offset: " FMT_ELF_OFF "\n"
1311 "RTLdrELF: sh_size: " FMT_ELF_XWORD "\n"
1312 "RTLdrELF: sh_link: " FMT_ELF_WORD "\n"
1313 "RTLdrELF: sh_info: " FMT_ELF_WORD "\n"
1314 "RTLdrELF: sh_addralign: " FMT_ELF_XWORD "\n"
1315 "RTLdrELF: sh_entsize: " FMT_ELF_XWORD "\n",
1316 iShdr,
1317 pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
1318 pShdr->sh_type, rtldrElfGetShdrType(pShdr->sh_type), pShdr->sh_flags, pShdr->sh_addr,
1319 pShdr->sh_offset, pShdr->sh_size, pShdr->sh_link, pShdr->sh_info, pShdr->sh_addralign,
1320 pShdr->sh_entsize));
1321
1322 if (iShdr == 0)
1323 {
1324 if ( pShdr->sh_name != 0
1325 || pShdr->sh_type != SHT_NULL
1326 || pShdr->sh_flags != 0
1327 || pShdr->sh_addr != 0
1328 || pShdr->sh_size != 0
1329 || pShdr->sh_offset != 0
1330 || pShdr->sh_link != SHN_UNDEF
1331 || pShdr->sh_addralign != 0
1332 || pShdr->sh_entsize != 0 )
1333 {
1334 Log(("RTLdrELF: %s: Bad #0 section: %.*Rhxs\n", pszLogName, sizeof(*pShdr), pShdr ));
1335 return VERR_BAD_EXE_FORMAT;
1336 }
1337 return VINF_SUCCESS;
1338 }
1339
1340 if (pShdr->sh_name >= pModElf->cbShStr)
1341 {
1342 Log(("RTLdrELF: %s: Shdr #%d: sh_name (%d) is beyond the end of the section header string table (%d)!\n",
1343 pszLogName, iShdr, pShdr->sh_name, pModElf->cbShStr)); NOREF(pszLogName);
1344 return VERR_BAD_EXE_FORMAT;
1345 }
1346
1347 if (pShdr->sh_link >= pModElf->Ehdr.e_shnum)
1348 {
1349 Log(("RTLdrELF: %s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!\n",
1350 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum)); NOREF(pszLogName);
1351 return VERR_BAD_EXE_FORMAT;
1352 }
1353
1354 switch (pShdr->sh_type)
1355 {
1356 /** @todo find specs and check up which sh_info fields indicates section table entries */
1357 case 12301230:
1358 if (pShdr->sh_info >= pModElf->Ehdr.e_shnum)
1359 {
1360 Log(("RTLdrELF: %s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!\n",
1361 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
1362 return VERR_BAD_EXE_FORMAT;
1363 }
1364 break;
1365
1366 case SHT_NULL:
1367 break;
1368 case SHT_PROGBITS:
1369 case SHT_SYMTAB:
1370 case SHT_STRTAB:
1371 case SHT_RELA:
1372 case SHT_HASH:
1373 case SHT_DYNAMIC:
1374 case SHT_NOTE:
1375 case SHT_NOBITS:
1376 case SHT_REL:
1377 case SHT_SHLIB:
1378 case SHT_DYNSYM:
1379 /*
1380 * For these types sh_info doesn't have any special meaning, or anything which
1381 * we need/can validate now.
1382 */
1383 break;
1384
1385
1386 default:
1387 Log(("RTLdrELF: %s: Warning, unknown type %d!\n", pszLogName, pShdr->sh_type));
1388 break;
1389 }
1390
1391 if ( pShdr->sh_type != SHT_NOBITS
1392 && pShdr->sh_size)
1393 {
1394 RTFOFF offEnd = pShdr->sh_offset + pShdr->sh_size;
1395 if ( offEnd > cbRawImage
1396 || offEnd < (RTFOFF)pShdr->sh_offset)
1397 {
1398 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",
1399 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, offEnd, cbRawImage));
1400 return VERR_BAD_EXE_FORMAT;
1401 }
1402 if (pShdr->sh_offset < sizeof(Elf_Ehdr))
1403 {
1404 Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!\n",
1405 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, cbRawImage));
1406 return VERR_BAD_EXE_FORMAT;
1407 }
1408 }
1409
1410 return VINF_SUCCESS;
1411}
1412
1413
1414
1415/**
1416 * Opens an ELF image, fixed bitness.
1417 *
1418 * @returns iprt status code.
1419 * @param pReader The loader reader instance which will provide the raw image bits.
1420 * @param fFlags Reserved, MBZ.
1421 * @param enmArch Architecture specifier.
1422 * @param phLdrMod Where to store the handle.
1423 */
1424static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
1425{
1426 const char *pszLogName = pReader->pfnLogName(pReader);
1427 RTFOFF cbRawImage = pReader->pfnSize(pReader);
1428
1429 /*
1430 * Create the loader module instance.
1431 */
1432 PRTLDRMODELF pModElf = (PRTLDRMODELF)RTMemAllocZ(sizeof(*pModElf));
1433 if (!pModElf)
1434 return VERR_NO_MEMORY;
1435
1436 pModElf->Core.u32Magic = RTLDRMOD_MAGIC;
1437 pModElf->Core.eState = LDR_STATE_INVALID;
1438 pModElf->Core.pReader = pReader;
1439 //pModElf->pvBits = NULL;
1440 //pModElf->Ehdr = {0};
1441 //pModElf->paShdrs = NULL;
1442 //pModElf->paSyms = NULL;
1443 pModElf->iSymSh = ~0U;
1444 //pModElf->cSyms = 0;
1445 pModElf->iStrSh = ~0U;
1446 //pModElf->cbStr = 0;
1447 //pModElf->cbImage = 0;
1448 //pModElf->pStr = NULL;
1449 //pModElf->cbShStr = 0;
1450 //pModElf->pShStr = NULL;
1451
1452 /*
1453 * Read and validate the ELF header and match up the CPU architecture.
1454 */
1455 int rc = pReader->pfnRead(pReader, &pModElf->Ehdr, sizeof(pModElf->Ehdr), 0);
1456 if (RT_SUCCESS(rc))
1457 {
1458 RTLDRARCH enmArchImage = RTLDRARCH_INVALID; /* shut up gcc */
1459 rc = RTLDRELF_NAME(ValidateElfHeader)(&pModElf->Ehdr, pszLogName, cbRawImage, &enmArchImage);
1460 if (RT_SUCCESS(rc))
1461 {
1462 if ( enmArch != RTLDRARCH_WHATEVER
1463 && enmArch != enmArchImage)
1464 rc = VERR_LDR_ARCH_MISMATCH;
1465 }
1466 }
1467 if (RT_SUCCESS(rc))
1468 {
1469 /*
1470 * Read the section headers, keeping a prestine copy for the module
1471 * introspection methods.
1472 */
1473 size_t const cbShdrs = pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr);
1474 Elf_Shdr *paShdrs = (Elf_Shdr *)RTMemAlloc(cbShdrs * 2);
1475 if (paShdrs)
1476 {
1477 pModElf->paShdrs = paShdrs;
1478 rc = pReader->pfnRead(pReader, paShdrs, cbShdrs, pModElf->Ehdr.e_shoff);
1479 if (RT_SUCCESS(rc))
1480 {
1481 memcpy(&paShdrs[pModElf->Ehdr.e_shnum], paShdrs, cbShdrs);
1482 pModElf->paOrgShdrs = &paShdrs[pModElf->Ehdr.e_shnum];
1483
1484 pModElf->cbShStr = paShdrs[pModElf->Ehdr.e_shstrndx].sh_size;
1485
1486 /*
1487 * Validate the section headers, allocate memory for the sections (determine the image size),
1488 * and find relevant sections.
1489 */
1490 for (unsigned i = 0; i < pModElf->Ehdr.e_shnum; i++)
1491 {
1492 rc = RTLDRELF_NAME(ValidateSectionHeader)(pModElf, i, pszLogName, cbRawImage);
1493 if (RT_FAILURE(rc))
1494 break;
1495
1496 /* Allocate memory addresses for the section. */
1497 if (paShdrs[i].sh_flags & SHF_ALLOC)
1498 {
1499 paShdrs[i].sh_addr = paShdrs[i].sh_addralign
1500 ? RT_ALIGN_T(pModElf->cbImage, paShdrs[i].sh_addralign, Elf_Addr)
1501 : (Elf_Addr)pModElf->cbImage;
1502 pModElf->cbImage = (size_t)paShdrs[i].sh_addr + (size_t)paShdrs[i].sh_size;
1503 AssertMsgReturn(pModElf->cbImage == paShdrs[i].sh_addr + paShdrs[i].sh_size,
1504 (FMT_ELF_ADDR "\n", paShdrs[i].sh_addr + paShdrs[i].sh_size),
1505 VERR_IMAGE_TOO_BIG);
1506 Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
1507 }
1508
1509 /* We're looking for symbol tables. */
1510 if (paShdrs[i].sh_type == SHT_SYMTAB)
1511 {
1512 if (pModElf->iSymSh != ~0U)
1513 {
1514 Log(("RTLdrElf: %s: Multiple symbol tabs! iSymSh=%d i=%d\n", pszLogName, pModElf->iSymSh, i));
1515 rc = VERR_LDRELF_MULTIPLE_SYMTABS;
1516 break;
1517 }
1518 pModElf->iSymSh = i;
1519 pModElf->cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
1520 AssertReturn(pModElf->cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), VERR_IMAGE_TOO_BIG);
1521 pModElf->iStrSh = paShdrs[i].sh_link;
1522 pModElf->cbStr = (unsigned)paShdrs[pModElf->iStrSh].sh_size;
1523 AssertReturn(pModElf->cbStr == paShdrs[pModElf->iStrSh].sh_size, VERR_IMAGE_TOO_BIG);
1524 }
1525
1526 /* Special checks for the section string table. */
1527 if (i == pModElf->Ehdr.e_shstrndx)
1528 {
1529 if (paShdrs[i].sh_type != SHT_STRTAB)
1530 {
1531 Log(("RTLdrElf: Section header string table is not a SHT_STRTAB: %#x\n", paShdrs[i].sh_type));
1532 rc = VERR_BAD_EXE_FORMAT;
1533 break;
1534 }
1535 if (paShdrs[i].sh_size == 0)
1536 {
1537 Log(("RTLdrElf: Section header string table is empty\n"));
1538 rc = VERR_BAD_EXE_FORMAT;
1539 break;
1540 }
1541 }
1542
1543 } /* for each section header */
1544
1545 Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx\n",
1546 pModElf->iSymSh, pModElf->cSyms, pModElf->iStrSh, pModElf->cbStr, rc, pModElf->cbImage));
1547 if (RT_SUCCESS(rc))
1548 {
1549 pModElf->Core.pOps = &RTLDRELF_MID(s_rtldrElf,Ops);
1550 pModElf->Core.eState = LDR_STATE_OPENED;
1551 *phLdrMod = &pModElf->Core;
1552
1553 LogFlow(("%s: %s: returns VINF_SUCCESS *phLdrMod=%p\n", __FUNCTION__, pszLogName, *phLdrMod));
1554 return VINF_SUCCESS;
1555 }
1556 }
1557
1558 RTMemFree(paShdrs);
1559 }
1560 else
1561 rc = VERR_NO_MEMORY;
1562 }
1563
1564 RTMemFree(pModElf);
1565 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
1566 return rc;
1567}
1568
1569
1570
1571
1572/*******************************************************************************
1573* Cleanup Constants And Macros *
1574*******************************************************************************/
1575#undef RTLDRELF_NAME
1576#undef RTLDRELF_SUFF
1577#undef RTLDRELF_MID
1578
1579#undef FMT_ELF_ADDR
1580#undef FMT_ELF_HALF
1581#undef FMT_ELF_SHALF
1582#undef FMT_ELF_OFF
1583#undef FMT_ELF_SIZE
1584#undef FMT_ELF_SWORD
1585#undef FMT_ELF_WORD
1586#undef FMT_ELF_XWORD
1587#undef FMT_ELF_SXWORD
1588
1589#undef Elf_Ehdr
1590#undef Elf_Phdr
1591#undef Elf_Shdr
1592#undef Elf_Sym
1593#undef Elf_Rel
1594#undef Elf_Rela
1595#undef Elf_Reloc
1596#undef Elf_Nhdr
1597#undef Elf_Dyn
1598
1599#undef Elf_Addr
1600#undef Elf_Half
1601#undef Elf_Off
1602#undef Elf_Size
1603#undef Elf_Sword
1604#undef Elf_Word
1605
1606#undef RTLDRMODELF
1607#undef PRTLDRMODELF
1608
1609#undef ELF_R_SYM
1610#undef ELF_R_TYPE
1611#undef ELF_R_INFO
1612
1613#undef ELF_ST_BIND
1614
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