VirtualBox

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

Last change on this file since 73494 was 73494, checked in by vboxsync, 7 years ago

IPRT: Added single stack frame unwind function to RTDbgMod and RTLdr, copying over the PoC from DBGFRStack.cpp. bugref:3897

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