VirtualBox

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

Last change on this file since 56978 was 56978, checked in by vboxsync, 9 years ago

Runtime: Formatting fixes, time.cpp being the most significant.

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