VirtualBox

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

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

IPRT: Changed RTLDRSEG::pchName to pszName and make sure it's always set to something. Started on implementing a codeview reader.

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

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