VirtualBox

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

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

ldrELFRelocatable.cpp.h: pointer calc warning fix for 32-bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 59.7 KB
Line 
1/* $Id: ldrELFRelocatable.cpp.h 46180 2013-05-20 21:40:43Z 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.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Defined Constants And Macros *
30*******************************************************************************/
31#if ELF_MODE == 32
32#define RTLDRELF_NAME(name) rtldrELF32##name
33#define RTLDRELF_SUFF(name) name##32
34#define RTLDRELF_MID(pre,suff) pre##32##suff
35#define FMT_ELF_ADDR "%08RX32"
36#define FMT_ELF_HALF "%04RX16"
37#define FMT_ELF_OFF "%08RX32"
38#define FMT_ELF_SIZE "%08RX32"
39#define FMT_ELF_SWORD "%RI32"
40#define FMT_ELF_WORD "%08RX32"
41#define FMT_ELF_XWORD "%08RX32"
42#define FMT_ELF_SXWORD "%RI32"
43
44#elif ELF_MODE == 64
45#define RTLDRELF_NAME(name) rtldrELF64##name
46#define RTLDRELF_SUFF(name) name##64
47#define RTLDRELF_MID(pre,suff) pre##64##suff
48#define FMT_ELF_ADDR "%016RX64"
49#define FMT_ELF_HALF "%04RX16"
50#define FMT_ELF_SHALF "%RI16"
51#define FMT_ELF_OFF "%016RX64"
52#define FMT_ELF_SIZE "%016RX64"
53#define FMT_ELF_SWORD "%RI32"
54#define FMT_ELF_WORD "%08RX32"
55#define FMT_ELF_XWORD "%016RX64"
56#define FMT_ELF_SXWORD "%RI64"
57#endif
58
59#define Elf_Ehdr RTLDRELF_MID(Elf,_Ehdr)
60#define Elf_Phdr RTLDRELF_MID(Elf,_Phdr)
61#define Elf_Shdr RTLDRELF_MID(Elf,_Shdr)
62#define Elf_Sym RTLDRELF_MID(Elf,_Sym)
63#define Elf_Rel RTLDRELF_MID(Elf,_Rel)
64#define Elf_Rela RTLDRELF_MID(Elf,_Rela)
65#define Elf_Nhdr RTLDRELF_MID(Elf,_Nhdr)
66#define Elf_Dyn RTLDRELF_MID(Elf,_Dyn)
67#define Elf_Addr RTLDRELF_MID(Elf,_Addr)
68#define Elf_Half RTLDRELF_MID(Elf,_Half)
69#define Elf_Off RTLDRELF_MID(Elf,_Off)
70#define Elf_Size RTLDRELF_MID(Elf,_Size)
71#define Elf_Sword RTLDRELF_MID(Elf,_Sword)
72#define Elf_Word RTLDRELF_MID(Elf,_Word)
73
74#define RTLDRMODELF RTLDRELF_MID(RTLDRMODELF,RT_NOTHING)
75#define PRTLDRMODELF RTLDRELF_MID(PRTLDRMODELF,RT_NOTHING)
76
77#define ELF_R_SYM(info) RTLDRELF_MID(ELF,_R_SYM)(info)
78#define ELF_R_TYPE(info) RTLDRELF_MID(ELF,_R_TYPE)(info)
79#define ELF_R_INFO(sym, type) RTLDRELF_MID(ELF,_R_INFO)(sym, type)
80
81#define ELF_ST_BIND(info) RTLDRELF_MID(ELF,_ST_BIND)(info)
82
83
84
85/*******************************************************************************
86* Structures and Typedefs *
87*******************************************************************************/
88/**
89 * The ELF loader structure.
90 */
91typedef struct RTLDRMODELF
92{
93 /** Core module structure. */
94 RTLDRMODINTERNAL Core;
95 /** Pointer to 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 + (uintptr_t)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 + (uintptr_t)paShdrs[iShdr].sh_size
777 - sizeof(uint32_t));
778 DbgInfo.offFile = -1;
779 DbgInfo.cb = 0;
780 }
781 else
782 continue;
783
784 DbgInfo.LinkAddress = NIL_RTLDRADDR;
785 DbgInfo.iDbgInfo = iShdr - 1;
786
787 rc = pfnCallback(pMod, &DbgInfo, pvUser);
788 if (rc != VINF_SUCCESS)
789 return rc;
790
791 }
792
793 return VINF_SUCCESS;
794}
795
796
797/**
798 * Helper that locates the first allocated section.
799 *
800 * @returns Pointer to the section header if found, NULL if none.
801 * @param pShdr The section header to start searching at.
802 * @param cLeft The number of section headers left to search. Can be 0.
803 */
804static const Elf_Shdr *RTLDRELF_NAME(GetFirstAllocatedSection)(const Elf_Shdr *pShdr, unsigned cLeft)
805{
806 while (cLeft-- > 0)
807 {
808 if (pShdr->sh_flags & SHF_ALLOC)
809 return pShdr;
810 pShdr++;
811 }
812 return NULL;
813}
814
815/** @copydoc RTLDROPS::pfnEnumSegments. */
816static DECLCALLBACK(int) RTLDRELF_NAME(EnumSegments)(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
817{
818 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
819
820 /*
821 * Map the image bits if not already done and setup pointer into it.
822 */
823 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
824 if (RT_FAILURE(rc))
825 return rc;
826
827 /*
828 * Do the enumeration.
829 */
830 const Elf_Shdr *paShdrs = pModElf->paShdrs;
831 const Elf_Shdr *paOrgShdrs = pModElf->paOrgShdrs;
832 for (unsigned iShdr = 1; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
833 {
834 RTLDRSEG Seg;
835 Seg.pchName = ELF_SH_STR(pModElf, paShdrs[iShdr].sh_name);
836 Seg.cchName = (uint32_t)strlen(Seg.pchName);
837 Seg.SelFlat = 0;
838 Seg.Sel16bit = 0;
839 Seg.fFlags = 0;
840 Seg.fProt = RTMEM_PROT_READ;
841 if (paShdrs[iShdr].sh_flags & SHF_WRITE)
842 Seg.fProt |= RTMEM_PROT_WRITE;
843 if (paShdrs[iShdr].sh_flags & SHF_EXECINSTR)
844 Seg.fProt |= RTMEM_PROT_EXEC;
845 Seg.cb = paShdrs[iShdr].sh_size;
846 Seg.Alignment = paShdrs[iShdr].sh_addralign;
847 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
848 {
849 Seg.LinkAddress = paOrgShdrs[iShdr].sh_addr;
850 Seg.RVA = paShdrs[iShdr].sh_addr;
851 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetFirstAllocatedSection)(&paShdrs[iShdr + 1],
852 pModElf->Ehdr.e_shnum - iShdr - 1);
853 Seg.cbMapped = pShdr2 ? pShdr2->sh_addr - paShdrs[iShdr].sh_addr : paShdrs[iShdr].sh_size;
854 }
855 else
856 {
857 Seg.LinkAddress = NIL_RTLDRADDR;
858 Seg.RVA = NIL_RTLDRADDR;
859 Seg.cbMapped = NIL_RTLDRADDR;
860 }
861 if (paShdrs[iShdr].sh_type != SHT_NOBITS)
862 {
863 Seg.offFile = paShdrs[iShdr].sh_offset;
864 Seg.cbFile = paShdrs[iShdr].sh_size;
865 }
866 else
867 {
868 Seg.offFile = -1;
869 Seg.cbFile = 0;
870 }
871
872 rc = pfnCallback(pMod, &Seg, pvUser);
873 if (rc != VINF_SUCCESS)
874 return rc;
875 }
876
877 return VINF_SUCCESS;
878}
879
880
881/** @copydoc RTLDROPS::pfnLinkAddressToSegOffset. */
882static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress,
883 uint32_t *piSeg, PRTLDRADDR poffSeg)
884{
885 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
886
887 const Elf_Shdr *pShdrEnd = NULL;
888 unsigned cLeft = pModElf->Ehdr.e_shnum - 1;
889 const Elf_Shdr *pShdr = &pModElf->paOrgShdrs[cLeft];
890 while (cLeft-- > 0)
891 {
892 if (pShdr->sh_flags & SHF_ALLOC)
893 {
894 RTLDRADDR offSeg = LinkAddress - pShdr->sh_addr;
895 if (offSeg < pShdr->sh_size)
896 {
897 *poffSeg = offSeg;
898 *piSeg = cLeft;
899 return VINF_SUCCESS;
900 }
901 if (offSeg == pShdr->sh_size)
902 pShdrEnd = pShdr;
903 }
904 pShdr--;
905 }
906
907 if (pShdrEnd)
908 {
909 *poffSeg = pShdrEnd->sh_size;
910 *piSeg = pShdrEnd - pModElf->paOrgShdrs - 1;
911 return VINF_SUCCESS;
912 }
913
914 return VERR_LDR_INVALID_LINK_ADDRESS;
915}
916
917
918/** @copydoc RTLDROPS::pfnLinkAddressToRva. */
919static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToRva)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
920{
921 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
922 uint32_t iSeg;
923 RTLDRADDR offSeg;
924 int rc = RTLDRELF_NAME(LinkAddressToSegOffset)(pMod, LinkAddress, &iSeg, &offSeg);
925 if (RT_SUCCESS(rc))
926 *pRva = pModElf->paShdrs[iSeg + 1].sh_addr + offSeg;
927 return rc;
928}
929
930
931/** @copydoc RTLDROPS::pfnSegOffsetToRva. */
932static DECLCALLBACK(int) RTLDRELF_NAME(SegOffsetToRva)(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg,
933 PRTLDRADDR pRva)
934{
935 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
936 if (iSeg >= pModElf->Ehdr.e_shnum - 1U)
937 return VERR_LDR_INVALID_SEG_OFFSET;
938
939 iSeg++; /* skip section 0 */
940 if (offSeg > pModElf->paShdrs[iSeg].sh_size)
941 {
942 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetFirstAllocatedSection)(&pModElf->paShdrs[iSeg + 1],
943 pModElf->Ehdr.e_shnum - iSeg - 1);
944 if ( !pShdr2
945 || offSeg > (pShdr2->sh_addr - pModElf->paShdrs[iSeg].sh_addr))
946 return VERR_LDR_INVALID_SEG_OFFSET;
947 }
948
949 if (!(pModElf->paShdrs[iSeg].sh_flags & SHF_ALLOC))
950 return VERR_LDR_INVALID_SEG_OFFSET;
951
952 *pRva = pModElf->paShdrs[iSeg].sh_addr;
953 return VINF_SUCCESS;
954}
955
956
957/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
958static DECLCALLBACK(int) RTLDRELF_NAME(RvaToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva,
959 uint32_t *piSeg, PRTLDRADDR poffSeg)
960{
961 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
962
963 Elf_Addr PrevAddr = 0;
964 unsigned cLeft = pModElf->Ehdr.e_shnum - 1;
965 const Elf_Shdr *pShdr = &pModElf->paShdrs[cLeft];
966 while (cLeft-- > 0)
967 {
968 if (pShdr->sh_flags & SHF_ALLOC)
969 {
970 Elf_Addr cbSeg = PrevAddr ? PrevAddr - pShdr->sh_addr : pShdr->sh_size;
971 RTLDRADDR offSeg = Rva - pShdr->sh_addr;
972 if (offSeg <= cbSeg)
973 {
974 *poffSeg = offSeg;
975 *piSeg = cLeft;
976 return VINF_SUCCESS;
977 }
978 PrevAddr = pShdr->sh_addr;
979 }
980 pShdr--;
981 }
982
983 return VERR_LDR_INVALID_RVA;
984}
985
986
987/** @callback_method_impl{FNRTLDRIMPORT, Stub used by ReadDbgInfo.} */
988static DECLCALLBACK(int) RTLDRELF_NAME(GetImportStubCallback)(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
989 unsigned uSymbol, PRTLDRADDR pValue, void *pvUser)
990{
991 return VERR_SYMBOL_NOT_FOUND;
992}
993
994
995/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
996static DECLCALLBACK(int) RTLDRELF_NAME(ReadDbgInfo)(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off,
997 size_t cb, void *pvBuf)
998{
999 PRTLDRMODELF pThis = (PRTLDRMODELF)pMod;
1000
1001 /*
1002 * Input validation.
1003 */
1004 AssertReturn(iDbgInfo < pThis->Ehdr.e_shnum && iDbgInfo + 1 < pThis->Ehdr.e_shnum, VERR_INVALID_PARAMETER);
1005 iDbgInfo++;
1006 AssertReturn(!(pThis->paShdrs[iDbgInfo].sh_flags & SHF_ALLOC), VERR_INVALID_PARAMETER);
1007 AssertReturn(pThis->paShdrs[iDbgInfo].sh_type == SHT_PROGBITS, VERR_INVALID_PARAMETER);
1008 AssertReturn(pThis->paShdrs[iDbgInfo].sh_offset == (uint64_t)off, VERR_INVALID_PARAMETER);
1009 AssertReturn(pThis->paShdrs[iDbgInfo].sh_size == cb, VERR_INVALID_PARAMETER);
1010 RTFOFF cbRawImage = pThis->Core.pReader->pfnSize(pThis->Core.pReader);
1011 AssertReturn(cbRawImage >= 0, VERR_INVALID_PARAMETER);
1012 AssertReturn(off >= 0 && cb <= (uint64_t)cbRawImage && (uint64_t)off + cb <= (uint64_t)cbRawImage, VERR_INVALID_PARAMETER);
1013
1014 /*
1015 * Read it from the file and look for fixup sections.
1016 */
1017 int rc;
1018 if (pThis->pvBits)
1019 memcpy(pvBuf, (const uint8_t *)pThis->pvBits + (size_t)off, cb);
1020 else
1021 {
1022 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off);
1023 if (RT_FAILURE(rc))
1024 return rc;
1025 }
1026
1027 uint32_t iRelocs = iDbgInfo + 1;
1028 if ( iRelocs >= pThis->Ehdr.e_shnum
1029 || pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1030 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1031 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA) )
1032 {
1033 iRelocs = 0;
1034 while ( iRelocs < pThis->Ehdr.e_shnum
1035 && ( pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1036 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1037 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA)) )
1038 iRelocs++;
1039 }
1040 if ( iRelocs < pThis->Ehdr.e_shnum
1041 && pThis->paShdrs[iRelocs].sh_size > 0)
1042 {
1043 /*
1044 * Load the relocations.
1045 */
1046 uint8_t *pbRelocsBuf = NULL;
1047 const uint8_t *pbRelocs;
1048 if (pThis->pvBits)
1049 pbRelocs = (const uint8_t *)pThis->pvBits + pThis->paShdrs[iRelocs].sh_offset;
1050 else
1051 {
1052 pbRelocs = pbRelocsBuf = (uint8_t *)RTMemTmpAlloc(pThis->paShdrs[iRelocs].sh_size);
1053 if (!pbRelocsBuf)
1054 return VERR_NO_TMP_MEMORY;
1055 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pbRelocsBuf,
1056 pThis->paShdrs[iRelocs].sh_size,
1057 pThis->paShdrs[iRelocs].sh_offset);
1058 if (RT_FAILURE(rc))
1059 {
1060 RTMemTmpFree(pbRelocsBuf);
1061 return rc;
1062 }
1063 }
1064
1065 /*
1066 * Apply the relocations.
1067 */
1068 rc = RTLDRELF_NAME(RelocateSection)(pThis, 0 /*BaseAddress*/,
1069 RTLDRELF_NAME(GetImportStubCallback), NULL /*pvUser*/,
1070 pThis->paShdrs[iDbgInfo].sh_addr,
1071 pThis->paShdrs[iDbgInfo].sh_size,
1072 (const uint8_t *)pvBuf,
1073 (uint8_t *)pvBuf,
1074 pbRelocs,
1075 pThis->paShdrs[iRelocs].sh_size);
1076 RTMemTmpFree(pbRelocsBuf);
1077 }
1078 else
1079 rc = VINF_SUCCESS;
1080 return rc;
1081}
1082
1083
1084
1085/**
1086 * The ELF module operations.
1087 */
1088static RTLDROPS RTLDRELF_MID(s_rtldrElf,Ops) =
1089{
1090#if ELF_MODE == 32
1091 "elf32",
1092#elif ELF_MODE == 64
1093 "elf64",
1094#endif
1095 RTLDRELF_NAME(Close),
1096 NULL, /* Get Symbol */
1097 RTLDRELF_NAME(Done),
1098 RTLDRELF_NAME(EnumSymbols),
1099 /* ext: */
1100 RTLDRELF_NAME(GetImageSize),
1101 RTLDRELF_NAME(GetBits),
1102 RTLDRELF_NAME(Relocate),
1103 RTLDRELF_NAME(GetSymbolEx),
1104 RTLDRELF_NAME(EnumDbgInfo),
1105 RTLDRELF_NAME(EnumSegments),
1106 RTLDRELF_NAME(LinkAddressToSegOffset),
1107 RTLDRELF_NAME(LinkAddressToRva),
1108 RTLDRELF_NAME(SegOffsetToRva),
1109 RTLDRELF_NAME(RvaToSegOffset),
1110 RTLDRELF_NAME(ReadDbgInfo),
1111 42
1112};
1113
1114
1115
1116/**
1117 * Validates the ELF header.
1118 *
1119 * @returns iprt status code.
1120 * @param pEhdr Pointer to the ELF header.
1121 * @param pszLogName The log name.
1122 * @param cbRawImage The size of the raw image.
1123 */
1124static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, const char *pszLogName, uint64_t cbRawImage,
1125 PRTLDRARCH penmArch)
1126{
1127 Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
1128 "RTLdrELF: e_type: " FMT_ELF_HALF "\n"
1129 "RTLdrELF: e_version: " FMT_ELF_HALF "\n"
1130 "RTLdrELF: e_entry: " FMT_ELF_ADDR "\n"
1131 "RTLdrELF: e_phoff: " FMT_ELF_OFF "\n"
1132 "RTLdrELF: e_shoff: " FMT_ELF_OFF "\n"
1133 "RTLdrELF: e_flags: " FMT_ELF_WORD "\n"
1134 "RTLdrELF: e_ehsize: " FMT_ELF_HALF "\n"
1135 "RTLdrELF: e_phentsize: " FMT_ELF_HALF "\n"
1136 "RTLdrELF: e_phnum: " FMT_ELF_HALF "\n"
1137 "RTLdrELF: e_shentsize: " FMT_ELF_HALF "\n"
1138 "RTLdrELF: e_shnum: " FMT_ELF_HALF "\n"
1139 "RTLdrELF: e_shstrndx: " FMT_ELF_HALF "\n",
1140 RT_ELEMENTS(pEhdr->e_ident), &pEhdr->e_ident[0], pEhdr->e_type, pEhdr->e_version,
1141 pEhdr->e_entry, pEhdr->e_phoff, pEhdr->e_shoff,pEhdr->e_flags, pEhdr->e_ehsize, pEhdr->e_phentsize,
1142 pEhdr->e_phnum, pEhdr->e_shentsize, pEhdr->e_shnum, pEhdr->e_shstrndx));
1143
1144 if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
1145 || pEhdr->e_ident[EI_MAG1] != ELFMAG1
1146 || pEhdr->e_ident[EI_MAG2] != ELFMAG2
1147 || pEhdr->e_ident[EI_MAG3] != ELFMAG3
1148 )
1149 {
1150 Log(("RTLdrELF: %s: Invalid ELF magic (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident)); NOREF(pszLogName);
1151 return VERR_BAD_EXE_FORMAT;
1152 }
1153 if (pEhdr->e_ident[EI_CLASS] != RTLDRELF_SUFF(ELFCLASS))
1154 {
1155 Log(("RTLdrELF: %s: Invalid ELF class (%.*Rhxs)\n", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident));
1156 return VERR_BAD_EXE_FORMAT;
1157 }
1158 if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
1159 {
1160 Log(("RTLdrELF: %s: ELF endian %x is unsupported\n", pEhdr->e_ident[EI_DATA]));
1161 return VERR_LDRELF_ODD_ENDIAN;
1162 }
1163 if (pEhdr->e_version != EV_CURRENT)
1164 {
1165 Log(("RTLdrELF: %s: ELF version %x is unsupported\n", pEhdr->e_version));
1166 return VERR_LDRELF_VERSION;
1167 }
1168
1169 if (sizeof(Elf_Ehdr) != pEhdr->e_ehsize)
1170 {
1171 Log(("RTLdrELF: %s: Elf header e_ehsize is %d expected %d!\n",
1172 pszLogName, pEhdr->e_ehsize, sizeof(Elf_Ehdr)));
1173 return VERR_BAD_EXE_FORMAT;
1174 }
1175 if ( sizeof(Elf_Phdr) != pEhdr->e_phentsize
1176 && ( pEhdr->e_phnum != 0
1177 || pEhdr->e_type == ET_DYN))
1178 {
1179 Log(("RTLdrELF: %s: Elf header e_phentsize is %d expected %d!\n",
1180 pszLogName, pEhdr->e_phentsize, sizeof(Elf_Phdr)));
1181 return VERR_BAD_EXE_FORMAT;
1182 }
1183 if (sizeof(Elf_Shdr) != pEhdr->e_shentsize)
1184 {
1185 Log(("RTLdrELF: %s: Elf header e_shentsize is %d expected %d!\n",
1186 pszLogName, pEhdr->e_shentsize, sizeof(Elf_Shdr)));
1187 return VERR_BAD_EXE_FORMAT;
1188 }
1189
1190 switch (pEhdr->e_type)
1191 {
1192 case ET_REL:
1193 case ET_EXEC:
1194 case ET_DYN:
1195 break;
1196 default:
1197 Log(("RTLdrELF: %s: image type %#x is not supported!\n", pszLogName, pEhdr->e_type));
1198 return VERR_BAD_EXE_FORMAT;
1199 }
1200
1201 switch (pEhdr->e_machine)
1202 {
1203#if ELF_MODE == 32
1204 case EM_386:
1205 case EM_486:
1206 *penmArch = RTLDRARCH_X86_32;
1207 break;
1208#elif ELF_MODE == 64
1209 case EM_X86_64:
1210 *penmArch = RTLDRARCH_AMD64;
1211 break;
1212#endif
1213 default:
1214 Log(("RTLdrELF: %s: machine type %u is not supported!\n", pEhdr->e_machine));
1215 return VERR_LDRELF_MACHINE;
1216 }
1217
1218 if ( pEhdr->e_phoff < pEhdr->e_ehsize
1219 && !(pEhdr->e_phoff && pEhdr->e_phnum)
1220 && pEhdr->e_phnum)
1221 {
1222 Log(("RTLdrELF: %s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF "\n",
1223 pszLogName, pEhdr->e_phoff));
1224 return VERR_BAD_EXE_FORMAT;
1225 }
1226 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
1227 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
1228 {
1229 Log(("RTLdrELF: %s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF "\n",
1230 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum));
1231 return VERR_BAD_EXE_FORMAT;
1232 }
1233
1234
1235 if ( pEhdr->e_shoff < pEhdr->e_ehsize
1236 && !(pEhdr->e_shoff && pEhdr->e_shnum))
1237 {
1238 Log(("RTLdrELF: %s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF "\n",
1239 pszLogName, pEhdr->e_shoff));
1240 return VERR_BAD_EXE_FORMAT;
1241 }
1242 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
1243 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
1244 {
1245 Log(("RTLdrELF: %s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF "\n",
1246 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum));
1247 return VERR_BAD_EXE_FORMAT;
1248 }
1249
1250 if (pEhdr->e_shstrndx == 0 || pEhdr->e_shstrndx > pEhdr->e_shnum)
1251 {
1252 Log(("RTLdrELF: %s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF "\n",
1253 pszLogName, pEhdr->e_shstrndx, pEhdr->e_shnum));
1254 return VERR_BAD_EXE_FORMAT;
1255 }
1256
1257 return VINF_SUCCESS;
1258}
1259
1260/**
1261 * Gets the section header name.
1262 *
1263 * @returns pszName.
1264 * @param pEhdr The elf header.
1265 * @param offName The offset of the section header name.
1266 * @param pszName Where to store the name.
1267 * @param cbName The size of the buffer pointed to by pszName.
1268 */
1269const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
1270{
1271 RTFOFF off = pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset + offName;
1272 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName, cbName - 1, off);
1273 if (RT_FAILURE(rc))
1274 {
1275 /* read by for byte. */
1276 for (unsigned i = 0; i < cbName; i++, off++)
1277 {
1278 rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName + i, 1, off);
1279 if (RT_FAILURE(rc))
1280 {
1281 pszName[i] = '\0';
1282 break;
1283 }
1284 }
1285 }
1286
1287 pszName[cbName - 1] = '\0';
1288 return pszName;
1289}
1290
1291
1292/**
1293 * Validates a section header.
1294 *
1295 * @returns iprt status code.
1296 * @param pModElf Pointer to the module structure.
1297 * @param iShdr The index of section header which should be validated.
1298 * The section headers are found in the pModElf->paShdrs array.
1299 * @param pszLogName The log name.
1300 * @param cbRawImage The size of the raw image.
1301 */
1302static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, const char *pszLogName, RTFOFF cbRawImage)
1303{
1304 const Elf_Shdr *pShdr = &pModElf->paShdrs[iShdr];
1305 char szSectionName[80]; NOREF(szSectionName);
1306 Log3(("RTLdrELF: Section Header #%d:\n"
1307 "RTLdrELF: sh_name: " FMT_ELF_WORD " - %s\n"
1308 "RTLdrELF: sh_type: " FMT_ELF_WORD " (%s)\n"
1309 "RTLdrELF: sh_flags: " FMT_ELF_XWORD "\n"
1310 "RTLdrELF: sh_addr: " FMT_ELF_ADDR "\n"
1311 "RTLdrELF: sh_offset: " FMT_ELF_OFF "\n"
1312 "RTLdrELF: sh_size: " FMT_ELF_XWORD "\n"
1313 "RTLdrELF: sh_link: " FMT_ELF_WORD "\n"
1314 "RTLdrELF: sh_info: " FMT_ELF_WORD "\n"
1315 "RTLdrELF: sh_addralign: " FMT_ELF_XWORD "\n"
1316 "RTLdrELF: sh_entsize: " FMT_ELF_XWORD "\n",
1317 iShdr,
1318 pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
1319 pShdr->sh_type, rtldrElfGetShdrType(pShdr->sh_type), pShdr->sh_flags, pShdr->sh_addr,
1320 pShdr->sh_offset, pShdr->sh_size, pShdr->sh_link, pShdr->sh_info, pShdr->sh_addralign,
1321 pShdr->sh_entsize));
1322
1323 if (iShdr == 0)
1324 {
1325 if ( pShdr->sh_name != 0
1326 || pShdr->sh_type != SHT_NULL
1327 || pShdr->sh_flags != 0
1328 || pShdr->sh_addr != 0
1329 || pShdr->sh_size != 0
1330 || pShdr->sh_offset != 0
1331 || pShdr->sh_link != SHN_UNDEF
1332 || pShdr->sh_addralign != 0
1333 || pShdr->sh_entsize != 0 )
1334 {
1335 Log(("RTLdrELF: %s: Bad #0 section: %.*Rhxs\n", pszLogName, sizeof(*pShdr), pShdr ));
1336 return VERR_BAD_EXE_FORMAT;
1337 }
1338 return VINF_SUCCESS;
1339 }
1340
1341 if (pShdr->sh_name >= pModElf->cbShStr)
1342 {
1343 Log(("RTLdrELF: %s: Shdr #%d: sh_name (%d) is beyond the end of the section header string table (%d)!\n",
1344 pszLogName, iShdr, pShdr->sh_name, pModElf->cbShStr)); NOREF(pszLogName);
1345 return VERR_BAD_EXE_FORMAT;
1346 }
1347
1348 if (pShdr->sh_link >= pModElf->Ehdr.e_shnum)
1349 {
1350 Log(("RTLdrELF: %s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!\n",
1351 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum)); NOREF(pszLogName);
1352 return VERR_BAD_EXE_FORMAT;
1353 }
1354
1355 switch (pShdr->sh_type)
1356 {
1357 /** @todo find specs and check up which sh_info fields indicates section table entries */
1358 case 12301230:
1359 if (pShdr->sh_info >= pModElf->Ehdr.e_shnum)
1360 {
1361 Log(("RTLdrELF: %s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!\n",
1362 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum));
1363 return VERR_BAD_EXE_FORMAT;
1364 }
1365 break;
1366
1367 case SHT_NULL:
1368 break;
1369 case SHT_PROGBITS:
1370 case SHT_SYMTAB:
1371 case SHT_STRTAB:
1372 case SHT_RELA:
1373 case SHT_HASH:
1374 case SHT_DYNAMIC:
1375 case SHT_NOTE:
1376 case SHT_NOBITS:
1377 case SHT_REL:
1378 case SHT_SHLIB:
1379 case SHT_DYNSYM:
1380 /*
1381 * For these types sh_info doesn't have any special meaning, or anything which
1382 * we need/can validate now.
1383 */
1384 break;
1385
1386
1387 default:
1388 Log(("RTLdrELF: %s: Warning, unknown type %d!\n", pszLogName, pShdr->sh_type));
1389 break;
1390 }
1391
1392 if ( pShdr->sh_type != SHT_NOBITS
1393 && pShdr->sh_size)
1394 {
1395 RTFOFF offEnd = pShdr->sh_offset + pShdr->sh_size;
1396 if ( offEnd > cbRawImage
1397 || offEnd < (RTFOFF)pShdr->sh_offset)
1398 {
1399 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",
1400 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, offEnd, cbRawImage));
1401 return VERR_BAD_EXE_FORMAT;
1402 }
1403 if (pShdr->sh_offset < sizeof(Elf_Ehdr))
1404 {
1405 Log(("RTLdrELF: %s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!\n",
1406 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, cbRawImage));
1407 return VERR_BAD_EXE_FORMAT;
1408 }
1409 }
1410
1411 return VINF_SUCCESS;
1412}
1413
1414
1415
1416/**
1417 * Opens an ELF image, fixed bitness.
1418 *
1419 * @returns iprt status code.
1420 * @param pReader The loader reader instance which will provide the raw image bits.
1421 * @param fFlags Reserved, MBZ.
1422 * @param enmArch Architecture specifier.
1423 * @param phLdrMod Where to store the handle.
1424 */
1425static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
1426{
1427 const char *pszLogName = pReader->pfnLogName(pReader);
1428 RTFOFF cbRawImage = pReader->pfnSize(pReader);
1429
1430 /*
1431 * Create the loader module instance.
1432 */
1433 PRTLDRMODELF pModElf = (PRTLDRMODELF)RTMemAllocZ(sizeof(*pModElf));
1434 if (!pModElf)
1435 return VERR_NO_MEMORY;
1436
1437 pModElf->Core.u32Magic = RTLDRMOD_MAGIC;
1438 pModElf->Core.eState = LDR_STATE_INVALID;
1439 pModElf->Core.pReader = pReader;
1440 pModElf->Core.enmFormat = RTLDRFMT_ELF;
1441 pModElf->Core.enmType = RTLDRTYPE_OBJECT;
1442 pModElf->Core.enmEndian = RTLDRENDIAN_LITTLE;
1443#if ELF_MODE == 32
1444 pModElf->Core.enmArch = RTLDRARCH_X86_32;
1445#else
1446 pModElf->Core.enmArch = RTLDRARCH_AMD64;
1447#endif
1448 //pModElf->pvBits = NULL;
1449 //pModElf->Ehdr = {0};
1450 //pModElf->paShdrs = NULL;
1451 //pModElf->paSyms = NULL;
1452 pModElf->iSymSh = ~0U;
1453 //pModElf->cSyms = 0;
1454 pModElf->iStrSh = ~0U;
1455 //pModElf->cbStr = 0;
1456 //pModElf->cbImage = 0;
1457 //pModElf->pStr = NULL;
1458 //pModElf->cbShStr = 0;
1459 //pModElf->pShStr = NULL;
1460
1461 /*
1462 * Read and validate the ELF header and match up the CPU architecture.
1463 */
1464 int rc = pReader->pfnRead(pReader, &pModElf->Ehdr, sizeof(pModElf->Ehdr), 0);
1465 if (RT_SUCCESS(rc))
1466 {
1467 RTLDRARCH enmArchImage = RTLDRARCH_INVALID; /* shut up gcc */
1468 rc = RTLDRELF_NAME(ValidateElfHeader)(&pModElf->Ehdr, pszLogName, cbRawImage, &enmArchImage);
1469 if (RT_SUCCESS(rc))
1470 {
1471 if ( enmArch != RTLDRARCH_WHATEVER
1472 && enmArch != enmArchImage)
1473 rc = VERR_LDR_ARCH_MISMATCH;
1474 }
1475 }
1476 if (RT_SUCCESS(rc))
1477 {
1478 /*
1479 * Read the section headers, keeping a prestine copy for the module
1480 * introspection methods.
1481 */
1482 size_t const cbShdrs = pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr);
1483 Elf_Shdr *paShdrs = (Elf_Shdr *)RTMemAlloc(cbShdrs * 2);
1484 if (paShdrs)
1485 {
1486 pModElf->paShdrs = paShdrs;
1487 rc = pReader->pfnRead(pReader, paShdrs, cbShdrs, pModElf->Ehdr.e_shoff);
1488 if (RT_SUCCESS(rc))
1489 {
1490 memcpy(&paShdrs[pModElf->Ehdr.e_shnum], paShdrs, cbShdrs);
1491 pModElf->paOrgShdrs = &paShdrs[pModElf->Ehdr.e_shnum];
1492
1493 pModElf->cbShStr = paShdrs[pModElf->Ehdr.e_shstrndx].sh_size;
1494
1495 /*
1496 * Validate the section headers, allocate memory for the sections (determine the image size),
1497 * and find relevant sections.
1498 */
1499 for (unsigned i = 0; i < pModElf->Ehdr.e_shnum; i++)
1500 {
1501 rc = RTLDRELF_NAME(ValidateSectionHeader)(pModElf, i, pszLogName, cbRawImage);
1502 if (RT_FAILURE(rc))
1503 break;
1504
1505 /* Allocate memory addresses for the section. */
1506 if (paShdrs[i].sh_flags & SHF_ALLOC)
1507 {
1508 paShdrs[i].sh_addr = paShdrs[i].sh_addralign
1509 ? RT_ALIGN_T(pModElf->cbImage, paShdrs[i].sh_addralign, Elf_Addr)
1510 : (Elf_Addr)pModElf->cbImage;
1511 pModElf->cbImage = (size_t)paShdrs[i].sh_addr + (size_t)paShdrs[i].sh_size;
1512 AssertMsgReturn(pModElf->cbImage == paShdrs[i].sh_addr + paShdrs[i].sh_size,
1513 (FMT_ELF_ADDR "\n", paShdrs[i].sh_addr + paShdrs[i].sh_size),
1514 VERR_IMAGE_TOO_BIG);
1515 Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
1516 }
1517
1518 /* We're looking for symbol tables. */
1519 if (paShdrs[i].sh_type == SHT_SYMTAB)
1520 {
1521 if (pModElf->iSymSh != ~0U)
1522 {
1523 Log(("RTLdrElf: %s: Multiple symbol tabs! iSymSh=%d i=%d\n", pszLogName, pModElf->iSymSh, i));
1524 rc = VERR_LDRELF_MULTIPLE_SYMTABS;
1525 break;
1526 }
1527 pModElf->iSymSh = i;
1528 pModElf->cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
1529 AssertReturn(pModElf->cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), VERR_IMAGE_TOO_BIG);
1530 pModElf->iStrSh = paShdrs[i].sh_link;
1531 pModElf->cbStr = (unsigned)paShdrs[pModElf->iStrSh].sh_size;
1532 AssertReturn(pModElf->cbStr == paShdrs[pModElf->iStrSh].sh_size, VERR_IMAGE_TOO_BIG);
1533 }
1534
1535 /* Special checks for the section string table. */
1536 if (i == pModElf->Ehdr.e_shstrndx)
1537 {
1538 if (paShdrs[i].sh_type != SHT_STRTAB)
1539 {
1540 Log(("RTLdrElf: Section header string table is not a SHT_STRTAB: %#x\n", paShdrs[i].sh_type));
1541 rc = VERR_BAD_EXE_FORMAT;
1542 break;
1543 }
1544 if (paShdrs[i].sh_size == 0)
1545 {
1546 Log(("RTLdrElf: Section header string table is empty\n"));
1547 rc = VERR_BAD_EXE_FORMAT;
1548 break;
1549 }
1550 }
1551
1552 } /* for each section header */
1553
1554 Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx\n",
1555 pModElf->iSymSh, pModElf->cSyms, pModElf->iStrSh, pModElf->cbStr, rc, pModElf->cbImage));
1556 if (RT_SUCCESS(rc))
1557 {
1558 pModElf->Core.pOps = &RTLDRELF_MID(s_rtldrElf,Ops);
1559 pModElf->Core.eState = LDR_STATE_OPENED;
1560 *phLdrMod = &pModElf->Core;
1561
1562 LogFlow(("%s: %s: returns VINF_SUCCESS *phLdrMod=%p\n", __FUNCTION__, pszLogName, *phLdrMod));
1563 return VINF_SUCCESS;
1564 }
1565 }
1566
1567 RTMemFree(paShdrs);
1568 }
1569 else
1570 rc = VERR_NO_MEMORY;
1571 }
1572
1573 RTMemFree(pModElf);
1574 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
1575 return rc;
1576}
1577
1578
1579
1580
1581/*******************************************************************************
1582* Cleanup Constants And Macros *
1583*******************************************************************************/
1584#undef RTLDRELF_NAME
1585#undef RTLDRELF_SUFF
1586#undef RTLDRELF_MID
1587
1588#undef FMT_ELF_ADDR
1589#undef FMT_ELF_HALF
1590#undef FMT_ELF_SHALF
1591#undef FMT_ELF_OFF
1592#undef FMT_ELF_SIZE
1593#undef FMT_ELF_SWORD
1594#undef FMT_ELF_WORD
1595#undef FMT_ELF_XWORD
1596#undef FMT_ELF_SXWORD
1597
1598#undef Elf_Ehdr
1599#undef Elf_Phdr
1600#undef Elf_Shdr
1601#undef Elf_Sym
1602#undef Elf_Rel
1603#undef Elf_Rela
1604#undef Elf_Reloc
1605#undef Elf_Nhdr
1606#undef Elf_Dyn
1607
1608#undef Elf_Addr
1609#undef Elf_Half
1610#undef Elf_Off
1611#undef Elf_Size
1612#undef Elf_Sword
1613#undef Elf_Word
1614
1615#undef RTLDRMODELF
1616#undef PRTLDRMODELF
1617
1618#undef ELF_R_SYM
1619#undef ELF_R_TYPE
1620#undef ELF_R_INFO
1621
1622#undef ELF_ST_BIND
1623
Note: See TracBrowser for help on using the repository browser.

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