VirtualBox

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

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

Ldr,Dbg: DWARF and ELF hacking in progress.

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