VirtualBox

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

Last change on this file since 107713 was 107668, checked in by vboxsync, 3 weeks ago

Runtime/common/ldr/ldrELFRelocatable.cpp.: Remove unused variable, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 134.9 KB
Line 
1/* $Id: ldrELFRelocatable.cpp.h 107668 2025-01-10 15:26:53Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Template for ELF Relocatable Images.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*******************************************************************************
39* Defined Constants And Macros *
40*******************************************************************************/
41#if ELF_MODE == 32
42# define RTLDRELF_NAME(name) rtldrELF32##name
43# define RTLDRELF_SUFF(name) name##32
44# define RTLDRELF_MID(pre,suff) pre##32##suff
45# define FMT_ELF_ADDR "%08RX32"
46# define FMT_ELF_ADDR7 "%07RX32"
47# define FMT_ELF_HALF "%04RX16"
48# define FMT_ELF_OFF "%08RX32"
49# define FMT_ELF_SIZE "%08RX32"
50# define FMT_ELF_SWORD "%RI32"
51# define FMT_ELF_WORD "%08RX32"
52# define FMT_ELF_XWORD "%08RX32"
53# define FMT_ELF_SXWORD "%RI32"
54# define Elf_Xword Elf32_Word
55# define Elf_Sxword Elf32_Sword
56
57#elif ELF_MODE == 64
58# define RTLDRELF_NAME(name) rtldrELF64##name
59# define RTLDRELF_SUFF(name) name##64
60# define RTLDRELF_MID(pre,suff) pre##64##suff
61# define FMT_ELF_ADDR "%016RX64"
62# define FMT_ELF_ADDR7 "%08RX64"
63# define FMT_ELF_HALF "%04RX16"
64# define FMT_ELF_SHALF "%RI16"
65# define FMT_ELF_OFF "%016RX64"
66# define FMT_ELF_SIZE "%016RX64"
67# define FMT_ELF_SWORD "%RI32"
68# define FMT_ELF_WORD "%08RX32"
69# define FMT_ELF_XWORD "%016RX64"
70# define FMT_ELF_SXWORD "%RI64"
71# define Elf_Xword Elf64_Xword
72# define Elf_Sxword Elf64_Sxword
73#endif
74
75#define Elf_Ehdr RTLDRELF_MID(Elf,_Ehdr)
76#define Elf_Phdr RTLDRELF_MID(Elf,_Phdr)
77#define Elf_Shdr RTLDRELF_MID(Elf,_Shdr)
78#define Elf_Sym RTLDRELF_MID(Elf,_Sym)
79#define Elf_Rel RTLDRELF_MID(Elf,_Rel)
80#define Elf_Rela RTLDRELF_MID(Elf,_Rela)
81#define Elf_Nhdr RTLDRELF_MID(Elf,_Nhdr)
82#define Elf_Dyn RTLDRELF_MID(Elf,_Dyn)
83#define Elf_Addr RTLDRELF_MID(Elf,_Addr)
84#define Elf_Half RTLDRELF_MID(Elf,_Half)
85#define Elf_Off RTLDRELF_MID(Elf,_Off)
86#define Elf_Size RTLDRELF_MID(Elf,_Size)
87#define Elf_Sword RTLDRELF_MID(Elf,_Sword)
88#define Elf_Word RTLDRELF_MID(Elf,_Word)
89
90#define RTLDRMODELF RTLDRELF_MID(RTLDRMODELF,RT_NOTHING)
91#define PRTLDRMODELF RTLDRELF_MID(PRTLDRMODELF,RT_NOTHING)
92
93#define RTLDRMODELFSHX RTLDRELF_MID(RTLDRMODELFSHX,RT_NOTHING)
94#define PRTLDRMODELFSHX RTLDRELF_MID(PRTLDRMODELFSHX,RT_NOTHING)
95
96#define ELF_R_SYM(info) RTLDRELF_MID(ELF,_R_SYM)(info)
97#define ELF_R_TYPE(info) RTLDRELF_MID(ELF,_R_TYPE)(info)
98#define ELF_R_INFO(sym, type) RTLDRELF_MID(ELF,_R_INFO)(sym, type)
99
100#define ELF_ST_BIND(info) RTLDRELF_MID(ELF,_ST_BIND)(info)
101
102
103
104/*******************************************************************************
105* Structures and Typedefs *
106*******************************************************************************/
107/**
108 * Extra section info.
109 */
110typedef struct RTLDRMODELFSHX
111{
112 /** The corresponding program header. */
113 uint16_t idxPhdr;
114 /** The corresponding dynamic section entry (address). */
115 uint16_t idxDt;
116 /** The DT tag. */
117 uint32_t uDtTag;
118} RTLDRMODELFSHX;
119typedef RTLDRMODELFSHX *PRTLDRMODELFSHX;
120
121/**
122 * The ELF loader structure.
123 */
124typedef struct RTLDRMODELF
125{
126 /** Core module structure. */
127 RTLDRMODINTERNAL Core;
128 /** Pointer to readonly mapping of the image bits.
129 * This mapping is provided by the pReader. */
130 const void *pvBits;
131
132 /** The ELF header. */
133 Elf_Ehdr Ehdr;
134 /** Pointer to our copy of the section headers with sh_addr as RVAs.
135 * The virtual addresses in this array is the 0 based assignments we've given the image.
136 * Not valid if the image is DONE. */
137 Elf_Shdr *paShdrs;
138 /** Unmodified section headers (allocated after paShdrs, so no need to free).
139 * Not valid if the image is DONE. */
140 Elf_Shdr const *paOrgShdrs;
141 /** Runs parallel to paShdrs and is part of the same allocation. */
142 PRTLDRMODELFSHX paShdrExtras;
143 /** Base section number, either 1 or zero depending on whether we've
144 * re-used the NULL entry for .elf.headers in ET_EXEC/ET_DYN. */
145 unsigned iFirstSect;
146 /** Set if the SHF_ALLOC section headers are in order of sh_addr. */
147 bool fShdrInOrder;
148 /** The size of the loaded image. */
149 size_t cbImage;
150
151 /** The image base address if it's an EXEC or DYN image. */
152 Elf_Addr LinkAddress;
153
154 struct
155 {
156 /** The symbol section index. */
157 unsigned iSymSh;
158 /** Number of symbols in the table. */
159 unsigned cSyms;
160 /** Pointer to symbol table within RTLDRMODELF::pvBits. */
161 const Elf_Sym *paSyms;
162
163 /** The string section index. */
164 unsigned iStrSh;
165 /** Size of the string table. */
166 unsigned cbStr;
167 /** Pointer to string table within RTLDRMODELF::pvBits. */
168 const char *pStr;
169 } Rel /**< Regular symbols and strings. */
170 , Dyn /**< Dynamic symbols and strings. */;
171
172 /** Pointer to section header string table within RTLDRMODELF::pvBits. */
173 const char *pShStr;
174 /** Size of the section header string table. */
175 unsigned cbShStr;
176
177 /** The '.eh_frame' section index. Zero if not searched for, ~0U if not found. */
178 unsigned iShEhFrame;
179 /** The '.eh_frame_hdr' section index. Zero if not searched for, ~0U if not found. */
180 unsigned iShEhFrameHdr;
181
182 /** The '.dynamic' / SHT_DYNAMIC section index. ~0U if not present. */
183 unsigned iShDynamic;
184 /** Number of entries in paDynamic. */
185 unsigned cDynamic;
186 /** The dynamic section (NULL for ET_REL). */
187 Elf_Dyn *paDynamic;
188 /** Program headers (NULL for ET_REL). */
189 Elf_Phdr *paPhdrs;
190
191 /** Info extracted from PT_DYNAMIC and the program headers. */
192 struct
193 {
194 /** DT_RELA/DT_REL. */
195 Elf_Addr uPtrRelocs;
196 /** DT_RELASZ/DT_RELSZ. */
197 Elf_Xword cbRelocs;
198 /** Non-zero if we've seen DT_RELAENT/DT_RELENT. */
199 unsigned cbRelocEntry;
200 /** DT_RELA or DT_REL. */
201 unsigned uRelocType;
202 /** The index of the section header matching DT_RELA/DT_REL. */
203 unsigned idxShRelocs;
204
205 /** DT_JMPREL. */
206 Elf_Addr uPtrJmpRelocs;
207 /** DT_PLTRELSZ. */
208 Elf_Xword cbJmpRelocs;
209 /** DT_RELA or DT_REL (if we've seen DT_PLTREL). */
210 unsigned uJmpRelocType;
211 /** The index of the section header matching DT_JMPREL. */
212 unsigned idxShJmpRelocs;
213 } DynInfo;
214} RTLDRMODELF;
215/** Pointer to an ELF module instance. */
216typedef RTLDRMODELF *PRTLDRMODELF;
217
218
219/**
220 * Maps the image bits into memory and resolve pointers into it.
221 *
222 * @returns iprt status code.
223 * @param pModElf The ELF loader module instance data.
224 * @param fNeedsBits Set if we actually need the pvBits member.
225 * If we don't, we can simply read the string and symbol sections, thus saving memory.
226 */
227static int RTLDRELF_NAME(MapBits)(PRTLDRMODELF pModElf, bool fNeedsBits)
228{
229 NOREF(fNeedsBits);
230 if (pModElf->pvBits)
231 return VINF_SUCCESS;
232 int rc = pModElf->Core.pReader->pfnMap(pModElf->Core.pReader, &pModElf->pvBits);
233 if (RT_SUCCESS(rc))
234 {
235 const uint8_t *pu8 = (const uint8_t *)pModElf->pvBits;
236 if (pModElf->Rel.iSymSh != ~0U)
237 pModElf->Rel.paSyms = (const Elf_Sym *)(pu8 + pModElf->paShdrs[pModElf->Rel.iSymSh].sh_offset);
238 if (pModElf->Rel.iStrSh != ~0U)
239 pModElf->Rel.pStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->Rel.iStrSh].sh_offset);
240 if (pModElf->Dyn.iSymSh != ~0U)
241 pModElf->Dyn.paSyms = (const Elf_Sym *)(pu8 + pModElf->paShdrs[pModElf->Dyn.iSymSh].sh_offset);
242 if (pModElf->Dyn.iStrSh != ~0U)
243 pModElf->Dyn.pStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->Dyn.iStrSh].sh_offset);
244 pModElf->pShStr = (const char *)(pu8 + pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset);
245
246 /*
247 * Verify that the ends of the string tables have a zero terminator
248 * (this avoids duplicating the appropriate checks later in the code accessing the string tables).
249 *
250 * sh_offset and sh_size were verfied in RTLDRELF_NAME(ValidateSectionHeader)() already so they
251 * are safe to use.
252 */
253 AssertMsgStmt( pModElf->Rel.iStrSh == ~0U
254 || pModElf->Rel.pStr[pModElf->paShdrs[pModElf->Rel.iStrSh].sh_size - 1] == '\0',
255 ("The string table is not zero terminated!\n"),
256 rc = VERR_LDRELF_UNTERMINATED_STRING_TAB);
257 AssertMsgStmt( pModElf->Dyn.iStrSh == ~0U
258 || pModElf->Dyn.pStr[pModElf->paShdrs[pModElf->Dyn.iStrSh].sh_size - 1] == '\0',
259 ("The string table is not zero terminated!\n"),
260 rc = VERR_LDRELF_UNTERMINATED_STRING_TAB);
261 AssertMsgStmt(pModElf->pShStr[pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_size - 1] == '\0',
262 ("The section header string table is not zero terminated!\n"),
263 rc = VERR_LDRELF_UNTERMINATED_STRING_TAB);
264
265 if (RT_FAILURE(rc))
266 {
267 /* Unmap. */
268 int rc2 = pModElf->Core.pReader->pfnUnmap(pModElf->Core.pReader, pModElf->pvBits);
269 AssertRC(rc2);
270 pModElf->pvBits = NULL;
271 pModElf->Rel.paSyms = NULL;
272 pModElf->Rel.pStr = NULL;
273 pModElf->Dyn.paSyms = NULL;
274 pModElf->Dyn.pStr = NULL;
275 pModElf->pShStr = NULL;
276 }
277 }
278 return rc;
279}
280
281
282/*
283 *
284 * EXEC & DYN.
285 * EXEC & DYN.
286 * EXEC & DYN.
287 * EXEC & DYN.
288 * EXEC & DYN.
289 *
290 */
291
292/**
293 * Get the symbol and symbol value.
294 *
295 * @returns iprt status code.
296 * @param pModElf The ELF loader module instance data.
297 * @param BaseAddr The base address which the module is being fixedup to.
298 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
299 * @param pvUser User argument to pass to the callback.
300 * @param iSym The symbol to get.
301 * @param ppSym Where to store the symbol pointer on success. (read only)
302 * @param pSymValue Where to store the symbol value on success.
303 */
304static int RTLDRELF_NAME(SymbolExecDyn)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
305 Elf_Size iSym, const Elf_Sym **ppSym, Elf_Addr *pSymValue)
306{
307 /*
308 * Validate and find the symbol.
309 */
310 AssertMsgReturn(iSym < pModElf->Dyn.cSyms, ("iSym=%d is an invalid symbol index!\n", iSym), VERR_LDRELF_INVALID_SYMBOL_INDEX);
311 const Elf_Sym *pSym = &pModElf->Dyn.paSyms[iSym];
312 *ppSym = pSym;
313
314 AssertMsgReturn(pSym->st_name < pModElf->Dyn.cbStr,
315 ("iSym=%d st_name=%d str sh_size=%d\n", iSym, pSym->st_name, pModElf->Dyn.cbStr),
316 VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET);
317 const char * const pszName = pModElf->Dyn.pStr + pSym->st_name;
318
319 /*
320 * Determine the symbol value.
321 *
322 * Symbols needs different treatment depending on which section their are in.
323 * Undefined and absolute symbols goes into special non-existing sections.
324 */
325 switch (pSym->st_shndx)
326 {
327 /*
328 * Undefined symbol, needs resolving.
329 *
330 * Since ELF has no generic concept of importing from specific module (the OS/2 ELF format
331 * has but that's an OS extension and only applies to programs and dlls), we'll have to ask
332 * the resolver callback to do a global search.
333 */
334 case SHN_UNDEF:
335 {
336 /* Try to resolve the symbol. */
337 RTUINTPTR Value;
338 int rc = pfnGetImport(&pModElf->Core, "", pszName, ~0U, &Value, pvUser);
339 AssertMsgRCReturn(rc, ("Failed to resolve '%s' (iSym=" FMT_ELF_SIZE " rc=%Rrc\n", pszName, iSym, rc), rc);
340
341 *pSymValue = (Elf_Addr)Value;
342 AssertMsgReturn((RTUINTPTR)*pSymValue == Value,
343 ("Symbol value overflowed! '%s' (iSym=" FMT_ELF_SIZE "\n", pszName, iSym), VERR_SYMBOL_VALUE_TOO_BIG);
344
345 Log2(("rtldrELF: #%-3d - UNDEF " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
346 break;
347 }
348
349 /*
350 * Absolute symbols needs no fixing since they are, well, absolute.
351 */
352 case SHN_ABS:
353 *pSymValue = pSym->st_value;
354 Log2(("rtldrELF: #%-3d - ABS " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
355 break;
356
357 /*
358 * All other symbols are addressed relative the image base in DYN and EXEC binaries.
359 */
360 default:
361 AssertMsgReturn(pSym->st_shndx < pModElf->Ehdr.e_shnum,
362 ("iSym=%d st_shndx=%d e_shnum=%d pszName=%s\n", iSym, pSym->st_shndx, pModElf->Ehdr.e_shnum, pszName),
363 VERR_BAD_EXE_FORMAT);
364 *pSymValue = pSym->st_value + BaseAddr;
365 Log2(("rtldrELF: #%-3d - %5d " FMT_ELF_ADDR " '%s'\n", iSym, pSym->st_shndx, *pSymValue, pszName));
366 break;
367 }
368
369 return VINF_SUCCESS;
370}
371
372
373#if ELF_MODE == 32
374/** Helper for RelocateSectionExecDyn. */
375DECLINLINE(const Elf_Shdr *) RTLDRELF_NAME(RvaToSectionHeader)(PRTLDRMODELF pModElf, Elf_Addr uRva)
376{
377 const Elf_Shdr * const pShdrFirst = pModElf->paShdrs;
378 const Elf_Shdr *pShdr = pShdrFirst + pModElf->Ehdr.e_shnum;
379 while (--pShdr != pShdrFirst)
380 if (uRva - pShdr->sh_addr /*rva*/ < pShdr->sh_size)
381 return pShdr;
382 AssertFailed();
383 return pShdr;
384}
385#endif
386
387
388/**
389 * Applies the fixups for a section in an executable image.
390 *
391 * @returns iprt status code.
392 * @param pModElf The ELF loader module instance data.
393 * @param BaseAddr The base address which the module is being fixedup to.
394 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
395 * @param pvUser User argument to pass to the callback.
396 * @param SecAddr The section address. This is the address the relocations are relative to.
397 * @param cbSec The section size. The relocations must be inside this.
398 * @param pu8SecBaseR Where we read section bits from.
399 * @param pu8SecBaseW Where we write section bits to.
400 * @param pvRelocs Pointer to where we read the relocations from.
401 * @param cbRelocs Size of the relocations.
402 */
403static int RTLDRELF_NAME(RelocateSectionExecDyn)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr,
404 PFNRTLDRIMPORT pfnGetImport, void *pvUser,
405 const Elf_Addr SecAddr, Elf_Size cbSec,
406 const uint8_t *pu8SecBaseR, uint8_t *pu8SecBaseW,
407 const void *pvRelocs, Elf_Size cbRelocs)
408{
409#if ELF_MODE != 32
410 NOREF(pu8SecBaseR);
411#endif
412
413 /*
414 * Iterate the relocations.
415 * The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
416 */
417#if ELF_MODE == 32
418 const Elf_Shdr *pShdr = pModElf->paShdrs;
419 const Elf_Addr offDelta = BaseAddr - pModElf->LinkAddress;
420#endif
421 const Elf_Reloc *paRels = (const Elf_Reloc *)pvRelocs;
422 const unsigned iRelMax = (unsigned)(cbRelocs / sizeof(paRels[0]));
423 AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])),
424 VERR_IMAGE_TOO_BIG);
425 for (unsigned iRel = 0; iRel < iRelMax; iRel++)
426 {
427 /*
428 * Apply fixups not taking a symbol (will 'continue' rather than 'break').
429 */
430 AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec),
431 VERR_LDRELF_INVALID_RELOCATION_OFFSET);
432#if ELF_MODE == 32
433 if (paRels[iRel].r_offset - pShdr->sh_addr /*rva*/ >= pShdr->sh_size)
434 pShdr = RTLDRELF_NAME(RvaToSectionHeader)(pModElf, paRels[iRel].r_offset);
435 static const Elf_Addr s_uZero = 0;
436 const Elf_Addr *pAddrR = RT_LIKELY(pShdr->sh_type != SHT_NOBITS) /* Where to read the addend. */
437 ? (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset - pShdr->sh_addr /*rva*/
438 + pShdr->sh_offset)
439 : &s_uZero;
440#endif
441 Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
442 switch (ELF_R_TYPE(paRels[iRel].r_info))
443 {
444 /*
445 * Image relative (addend + base).
446 */
447#if ELF_MODE == 32
448 case R_386_RELATIVE:
449 {
450 const Elf_Addr Value = *pAddrR + BaseAddr;
451 *(uint32_t *)pAddrW = Value;
452 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_386_RELATIVE Value=" FMT_ELF_ADDR "\n",
453 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value));
454 AssertCompile(sizeof(Value) == sizeof(uint32_t));
455 continue;
456 }
457#elif ELF_MODE == 64
458 case R_X86_64_RELATIVE:
459 {
460 const Elf_Addr Value = paRels[iRel].r_addend + BaseAddr;
461 *(uint64_t *)pAddrW = (uint64_t)Value;
462 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_RELATIVE Value=" FMT_ELF_ADDR "\n",
463 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value));
464 AssertCompile(sizeof(Value) == sizeof(uint64_t));
465 continue;
466 }
467#endif
468
469 /*
470 * R_XXX_NONE.
471 */
472#if ELF_MODE == 32
473 case R_386_NONE:
474#elif ELF_MODE == 64
475 case R_X86_64_NONE:
476#endif
477 continue;
478 }
479
480 /*
481 * Validate and find the symbol, resolve undefined ones.
482 */
483 const Elf_Sym *pSym = NULL; /* shut up gcc */
484 Elf_Addr SymValue = 0; /* shut up gcc-4 */
485 int rc = RTLDRELF_NAME(SymbolExecDyn)(pModElf, BaseAddr, pfnGetImport, pvUser, ELF_R_SYM(paRels[iRel].r_info), &pSym, &SymValue);
486 if (RT_FAILURE(rc))
487 return rc;
488
489 /*
490 * Apply the fixup.
491 */
492 switch (ELF_R_TYPE(paRels[iRel].r_info))
493 {
494#if ELF_MODE == 32
495 /*
496 * GOT/PLT.
497 */
498 case R_386_GLOB_DAT:
499 {
500 *(uint32_t *)pAddrW = (uint32_t)SymValue;
501 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_386_GLOB_DAT Value=" FMT_ELF_ADDR "\n",
502 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, SymValue));
503 AssertCompile(sizeof(SymValue) == sizeof(uint32_t));
504 break;
505 }
506
507 case R_386_JMP_SLOT:
508 {
509 *(uint32_t *)pAddrW = (uint32_t)SymValue;
510 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_386_JMP_SLOT Value=" FMT_ELF_ADDR "\n",
511 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, SymValue));
512 AssertCompile(sizeof(SymValue) == sizeof(uint32_t));
513 break;
514 }
515
516 /*
517 * Absolute addressing.
518 */
519 case R_386_32:
520 {
521 Elf_Addr Value;
522 if (pSym->st_shndx < pModElf->Ehdr.e_shnum)
523 Value = *pAddrR + offDelta; /* Simplified. */
524 else if (pSym->st_shndx == SHN_ABS)
525 continue; /* Internal fixup, no need to apply it. */
526 else if (pSym->st_shndx == SHN_UNDEF)
527 Value = SymValue + *pAddrR;
528 else
529 AssertFailedReturn(VERR_LDR_GENERAL_FAILURE); /** @todo SHN_COMMON */
530 *(uint32_t *)pAddrW = Value;
531 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_386_32 Value=" FMT_ELF_ADDR "\n",
532 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value));
533 break;
534 }
535
536 /*
537 * PC relative addressing.
538 */
539 case R_386_PC32:
540 {
541 Elf_Addr Value;
542 if (pSym->st_shndx < pModElf->Ehdr.e_shnum)
543 continue; /* Internal fixup, no need to apply it. */
544 else if (pSym->st_shndx == SHN_ABS)
545 Value = *pAddrR + offDelta; /* Simplified. */
546 else if (pSym->st_shndx == SHN_UNDEF)
547 {
548 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
549 Value = SymValue + *(uint32_t *)pAddrR - SourceAddr;
550 *(uint32_t *)pAddrW = Value;
551 }
552 else
553 AssertFailedReturn(VERR_LDR_GENERAL_FAILURE); /** @todo SHN_COMMON */
554 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_386_PC32 Value=" FMT_ELF_ADDR "\n",
555 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value));
556 break;
557 }
558
559#elif ELF_MODE == 64
560 /*
561 * GOT/PLT.
562 */
563 case R_X86_64_GLOB_DAT:
564 {
565 *(uint64_t *)pAddrW = (uint64_t)SymValue;
566 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_GLOB_DAT Value=" FMT_ELF_ADDR "\n",
567 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, SymValue));
568 AssertCompile(sizeof(SymValue) == sizeof(uint64_t));
569 break;
570 }
571
572 case R_X86_64_JMP_SLOT:
573 {
574 *(uint64_t *)pAddrW = (uint64_t)SymValue;
575 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_JMP_SLOT Value=" FMT_ELF_ADDR "\n",
576 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, SymValue));
577 AssertCompile(sizeof(SymValue) == sizeof(uint64_t));
578 break;
579 }
580
581 /*
582 * Absolute addressing.
583 */
584 case R_X86_64_64:
585 {
586 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
587 *(uint64_t *)pAddrW = Value;
588 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_64 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
589 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value, SymValue));
590 break;
591 }
592
593 /*
594 * Truncated 32-bit value (zero-extendedable to the 64-bit value).
595 */
596 case R_X86_64_32:
597 {
598 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
599 *(uint32_t *)pAddrW = (uint32_t)Value;
600 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
601 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value, SymValue));
602 AssertMsgReturn((Elf_Addr)*(uint32_t *)pAddrW == SymValue, ("Value=" FMT_ELF_ADDR "\n", SymValue),
603 VERR_SYMBOL_VALUE_TOO_BIG);
604 break;
605 }
606
607 /*
608 * Truncated 32-bit value (sign-extendedable to the 64-bit value).
609 */
610 case R_X86_64_32S:
611 {
612 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
613 *(int32_t *)pAddrW = (int32_t)Value;
614 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_32S Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
615 SecAddr + paRels[iRel].r_offset + BaseAddr, paRels[iRel].r_offset, Value, SymValue));
616 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
617 break;
618 }
619
620 /*
621 * PC relative addressing.
622 */
623 case R_X86_64_PC32:
624 {
625 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
626 const Elf_Addr Value = SymValue + paRels[iRel].r_addend - SourceAddr;
627 *(int32_t *)pAddrW = (int32_t)Value;
628 Log4((FMT_ELF_ADDR "/" FMT_ELF_ADDR7 ": R_X86_64_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
629 SourceAddr, paRels[iRel].r_offset, Value, SymValue));
630 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
631 break;
632 }
633
634#endif
635 default:
636 AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
637 ELF_R_TYPE(paRels[iRel].r_info), iRel, iRelMax));
638 return VERR_LDRELF_RELOCATION_NOT_SUPPORTED;
639 }
640 }
641
642 return VINF_SUCCESS;
643}
644
645
646
647/*
648 *
649 * REL
650 * REL
651 * REL
652 * REL
653 * REL
654 *
655 */
656
657/**
658 * Get the symbol and symbol value.
659 *
660 * @returns iprt status code.
661 * @param pModElf The ELF loader module instance data.
662 * @param BaseAddr The base address which the module is being fixedup to.
663 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
664 * @param pvUser User argument to pass to the callback.
665 * @param iSym The symbol to get.
666 * @param ppSym Where to store the symbol pointer on success. (read only)
667 * @param pSymValue Where to store the symbol value on success.
668 */
669static int RTLDRELF_NAME(Symbol)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
670 Elf_Size iSym, const Elf_Sym **ppSym, Elf_Addr *pSymValue)
671{
672 /*
673 * Validate and find the symbol.
674 */
675 AssertMsgReturn(iSym < pModElf->Rel.cSyms, ("iSym=%d is an invalid symbol index!\n", iSym), VERR_LDRELF_INVALID_SYMBOL_INDEX);
676 const Elf_Sym *pSym = &pModElf->Rel.paSyms[iSym];
677 *ppSym = pSym;
678
679 AssertMsgReturn(pSym->st_name < pModElf->Rel.cbStr,
680 ("iSym=%d st_name=%d str sh_size=%d\n", iSym, pSym->st_name, pModElf->Rel.cbStr),
681 VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET);
682 const char *pszName = ELF_STR(pModElf, pSym->st_name);
683
684 /*
685 * Determine the symbol value.
686 *
687 * Symbols needs different treatment depending on which section their are in.
688 * Undefined and absolute symbols goes into special non-existing sections.
689 */
690 switch (pSym->st_shndx)
691 {
692 /*
693 * Undefined symbol, needs resolving.
694 *
695 * Since ELF has no generic concept of importing from specific module (the OS/2 ELF format
696 * has but that's an OS extension and only applies to programs and dlls), we'll have to ask
697 * the resolver callback to do a global search.
698 */
699 case SHN_UNDEF:
700 {
701 /* Try to resolve the symbol. */
702 RTUINTPTR Value;
703 int rc = pfnGetImport(&pModElf->Core, "", pszName, ~0U, &Value, pvUser);
704 AssertMsgRCReturn(rc, ("Failed to resolve '%s' (iSym=" FMT_ELF_SIZE " rc=%Rrc\n", pszName, iSym, rc), rc);
705 *pSymValue = (Elf_Addr)Value;
706
707 AssertMsgReturn((RTUINTPTR)*pSymValue == Value,
708 ("Symbol value overflowed! '%s' (iSym=" FMT_ELF_SIZE ")\n", pszName, iSym),
709 VERR_SYMBOL_VALUE_TOO_BIG);
710
711 Log2(("rtldrELF: #%-3d - UNDEF " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
712 break;
713 }
714
715 /*
716 * Absolute symbols needs no fixing since they are, well, absolute.
717 */
718 case SHN_ABS:
719 *pSymValue = pSym->st_value;
720 Log2(("rtldrELF: #%-3d - ABS " FMT_ELF_ADDR " '%s'\n", iSym, *pSymValue, pszName));
721 break;
722
723 /*
724 * All other symbols are addressed relative to their section and need to be fixed up.
725 */
726 default:
727 if (pSym->st_shndx >= pModElf->Ehdr.e_shnum)
728 {
729 /* what about common symbols? */
730 AssertMsg(pSym->st_shndx < pModElf->Ehdr.e_shnum,
731 ("iSym=%d st_shndx=%d e_shnum=%d pszName=%s\n", iSym, pSym->st_shndx, pModElf->Ehdr.e_shnum, pszName));
732 return VERR_BAD_EXE_FORMAT;
733 }
734 *pSymValue = pSym->st_value + pModElf->paShdrs[pSym->st_shndx].sh_addr + BaseAddr;
735 Log2(("rtldrELF: #%-3d - %5d " FMT_ELF_ADDR " '%s'\n", iSym, pSym->st_shndx, *pSymValue, pszName));
736 break;
737 }
738
739 return VINF_SUCCESS;
740}
741
742
743/**
744 * Applies the fixups for a sections.
745 *
746 * @returns iprt status code.
747 * @param pModElf The ELF loader module instance data.
748 * @param BaseAddr The base address which the module is being fixedup to.
749 * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
750 * @param pvUser User argument to pass to the callback.
751 * @param SecAddr The section address. This is the address the relocations are relative to.
752 * @param cbSec The section size. The relocations must be inside this.
753 * @param pu8SecBaseR Where we read section bits from.
754 * @param pu8SecBaseW Where we write section bits to.
755 * @param pvRelocs Pointer to where we read the relocations from.
756 * @param cbRelocs Size of the relocations.
757 */
758static int RTLDRELF_NAME(RelocateSectionRel)(PRTLDRMODELF pModElf, Elf_Addr BaseAddr, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
759 const Elf_Addr SecAddr, Elf_Size cbSec, const uint8_t *pu8SecBaseR,
760 uint8_t *pu8SecBaseW, const void *pvRelocs, Elf_Size cbRelocs)
761{
762#if ELF_MODE != 32
763 NOREF(pu8SecBaseR);
764#endif
765
766 /*
767 * Iterate the relocations.
768 * The relocations are stored in an array of Elf32_Rel records and covers the entire relocation section.
769 */
770 const Elf_Reloc *paRels = (const Elf_Reloc *)pvRelocs;
771 const unsigned iRelMax = (unsigned)(cbRelocs / sizeof(paRels[0]));
772 AssertMsgReturn(iRelMax == cbRelocs / sizeof(paRels[0]), (FMT_ELF_SIZE "\n", cbRelocs / sizeof(paRels[0])), VERR_IMAGE_TOO_BIG);
773 for (unsigned iRel = 0; iRel < iRelMax; iRel++)
774 {
775 /*
776 * Skip R_XXX_NONE entries early to avoid confusion in the symbol
777 * getter code.
778 */
779#if ELF_MODE == 32
780 if (ELF_R_TYPE(paRels[iRel].r_info) == R_386_NONE)
781 continue;
782#elif ELF_MODE == 64
783 if (ELF_R_TYPE(paRels[iRel].r_info) == R_X86_64_NONE)
784 continue;
785#endif
786
787
788 /*
789 * Get the symbol.
790 */
791 const Elf_Sym *pSym = NULL; /* shut up gcc */
792 Elf_Addr SymValue = 0; /* shut up gcc-4 */
793 int rc = RTLDRELF_NAME(Symbol)(pModElf, BaseAddr, pfnGetImport, pvUser, ELF_R_SYM(paRels[iRel].r_info), &pSym, &SymValue);
794 if (RT_FAILURE(rc))
795 return rc;
796
797 Log3(("rtldrELF: " FMT_ELF_ADDR " %02x %06x - " FMT_ELF_ADDR " %3d %02x %s\n",
798 paRels[iRel].r_offset, ELF_R_TYPE(paRels[iRel].r_info), (unsigned)ELF_R_SYM(paRels[iRel].r_info),
799 SymValue, (unsigned)pSym->st_shndx, pSym->st_info, ELF_STR(pModElf, pSym->st_name)));
800
801 /*
802 * Apply the fixup.
803 */
804 AssertMsgReturn(paRels[iRel].r_offset < cbSec, (FMT_ELF_ADDR " " FMT_ELF_SIZE "\n", paRels[iRel].r_offset, cbSec), VERR_LDRELF_INVALID_RELOCATION_OFFSET);
805#if ELF_MODE == 32
806 const Elf_Addr *pAddrR = (const Elf_Addr *)(pu8SecBaseR + paRels[iRel].r_offset); /* Where to read the addend. */
807#endif
808 Elf_Addr *pAddrW = (Elf_Addr *)(pu8SecBaseW + paRels[iRel].r_offset); /* Where to write the fixup. */
809 switch (ELF_R_TYPE(paRels[iRel].r_info))
810 {
811#if ELF_MODE == 32
812 /*
813 * Absolute addressing.
814 */
815 case R_386_32:
816 {
817 const Elf_Addr Value = SymValue + *pAddrR;
818 *(uint32_t *)pAddrW = Value;
819 Log4((FMT_ELF_ADDR": R_386_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
820 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
821 break;
822 }
823
824 /*
825 * PC relative addressing.
826 */
827 case R_386_PC32:
828 {
829 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
830 const Elf_Addr Value = SymValue + *(uint32_t *)pAddrR - SourceAddr;
831 *(uint32_t *)pAddrW = Value;
832 Log4((FMT_ELF_ADDR": R_386_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
833 SourceAddr, Value, SymValue));
834 break;
835 }
836
837 /* ignore */
838 case R_386_NONE:
839 break;
840
841#elif ELF_MODE == 64
842
843 /*
844 * Absolute addressing
845 */
846 case R_X86_64_64:
847 {
848 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
849 *(uint64_t *)pAddrW = Value;
850 Log4((FMT_ELF_ADDR": R_X86_64_64 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
851 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
852 break;
853 }
854
855 /*
856 * Truncated 32-bit value (zero-extendedable to the 64-bit value).
857 */
858 case R_X86_64_32:
859 {
860 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
861 *(uint32_t *)pAddrW = (uint32_t)Value;
862 Log4((FMT_ELF_ADDR": R_X86_64_32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
863 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
864 AssertMsgReturn((Elf_Addr)*(uint32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
865 break;
866 }
867
868 /*
869 * Truncated 32-bit value (sign-extendedable to the 64-bit value).
870 */
871 case R_X86_64_32S:
872 {
873 const Elf_Addr Value = SymValue + paRels[iRel].r_addend;
874 *(int32_t *)pAddrW = (int32_t)Value;
875 Log4((FMT_ELF_ADDR": R_X86_64_32S Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
876 SecAddr + paRels[iRel].r_offset + BaseAddr, Value, SymValue));
877 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
878 break;
879 }
880
881 /*
882 * PC relative addressing.
883 */
884 case R_X86_64_PC32:
885 case R_X86_64_PLT32: /* binutils commit 451875b4f976a527395e9303224c7881b65e12ed feature/regression. */
886 {
887 const Elf_Addr SourceAddr = SecAddr + paRels[iRel].r_offset + BaseAddr; /* Where the source really is. */
888 const Elf_Addr Value = SymValue + paRels[iRel].r_addend - SourceAddr;
889 *(int32_t *)pAddrW = (int32_t)Value;
890 Log4((FMT_ELF_ADDR": R_X86_64_PC32 Value=" FMT_ELF_ADDR " SymValue=" FMT_ELF_ADDR "\n",
891 SourceAddr, Value, SymValue));
892 AssertMsgReturn((Elf_Addr)*(int32_t *)pAddrW == Value, ("Value=" FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG); /** @todo check the sign-extending here. */
893 break;
894 }
895
896 /* ignore */
897 case R_X86_64_NONE:
898 break;
899#endif
900
901 default:
902 AssertMsgFailed(("Unknown relocation type: %d (iRel=%d iRelMax=%d)\n",
903 ELF_R_TYPE(paRels[iRel].r_info), iRel, iRelMax));
904 return VERR_LDRELF_RELOCATION_NOT_SUPPORTED;
905 }
906 }
907
908 return VINF_SUCCESS;
909}
910
911
912
913/** @copydoc RTLDROPS::pfnClose */
914static DECLCALLBACK(int) RTLDRELF_NAME(Close)(PRTLDRMODINTERNAL pMod)
915{
916 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
917
918 if (pModElf->paShdrs)
919 {
920 RTMemFree(pModElf->paShdrs);
921 pModElf->paShdrs = NULL;
922 }
923
924 if (pModElf->paPhdrs)
925 {
926 RTMemFree(pModElf->paPhdrs);
927 pModElf->paPhdrs = NULL;
928 }
929
930 if (pModElf->paDynamic)
931 {
932 RTMemFree(pModElf->paDynamic);
933 pModElf->paDynamic = NULL;
934 }
935
936 if (pModElf->pvBits)
937 {
938 pModElf->Core.pReader->pfnUnmap(pModElf->Core.pReader, pModElf->pvBits);
939 pModElf->pvBits = NULL;
940 }
941
942 return VINF_SUCCESS;
943}
944
945
946/** @copydoc RTLDROPS::Done */
947static DECLCALLBACK(int) RTLDRELF_NAME(Done)(PRTLDRMODINTERNAL pMod)
948{
949 NOREF(pMod); /*PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;*/
950 /** @todo Have to think more about this .... */
951 return -1;
952}
953
954
955/** @copydoc RTLDROPS::pfnEnumSymbols */
956static DECLCALLBACK(int) RTLDRELF_NAME(EnumSymbols)(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits,
957 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
958{
959 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
960 NOREF(pvBits);
961
962 /*
963 * Validate the input.
964 */
965 Elf_Addr BaseAddr = (Elf_Addr)BaseAddress;
966 AssertMsgReturn((RTUINTPTR)BaseAddr == BaseAddress, ("%RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
967
968 /*
969 * Make sure we've got the string and symbol tables. (We don't need the pvBits.)
970 */
971 int rc = RTLDRELF_NAME(MapBits)(pModElf, false);
972 if (RT_FAILURE(rc))
973 return rc;
974
975 /*
976 * Enumerate the symbol table.
977 */
978 const Elf_Sym *paSyms = pModElf->Rel.paSyms;
979 unsigned cSyms = pModElf->Rel.cSyms;
980 const char *pszzStr = pModElf->Rel.pStr;
981 unsigned cbStr = pModElf->Rel.cbStr;
982 if ( ( !(fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)
983 && pModElf->Dyn.cSyms > 0)
984 || cSyms == 0)
985 {
986 paSyms = pModElf->Dyn.paSyms;
987 cSyms = pModElf->Dyn.cSyms;
988 pszzStr = pModElf->Dyn.pStr;
989 cbStr = pModElf->Dyn.cbStr;
990 }
991
992 for (unsigned iSym = 1; iSym < cSyms; iSym++)
993 {
994 /*
995 * Skip imports (undefined).
996 */
997 if (paSyms[iSym].st_shndx != SHN_UNDEF)
998 {
999 /*
1000 * Calc value and get name.
1001 */
1002 Elf_Addr Value;
1003 if (paSyms[iSym].st_shndx == SHN_ABS)
1004 /* absolute symbols are not subject to any relocation. */
1005 Value = paSyms[iSym].st_value;
1006 else if (paSyms[iSym].st_shndx < pModElf->Ehdr.e_shnum)
1007 {
1008 if (pModElf->Ehdr.e_type == ET_REL)
1009 /* relative to the section. */
1010 Value = BaseAddr + paSyms[iSym].st_value + pModElf->paShdrs[paSyms[iSym].st_shndx].sh_addr;
1011 else /* Fixed up for link address. */
1012 Value = BaseAddr + paSyms[iSym].st_value - pModElf->LinkAddress;
1013 }
1014 else
1015 {
1016 AssertMsgFailed(("Arg! paSyms[%u].st_shndx=" FMT_ELF_HALF "\n", iSym, paSyms[iSym].st_shndx));
1017 return VERR_BAD_EXE_FORMAT;
1018 }
1019
1020 AssertMsgReturn(paSyms[iSym].st_name < cbStr,
1021 ("String outside string table! iSym=%d paSyms[iSym].st_name=%#x\n", iSym, paSyms[iSym].st_name),
1022 VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET);
1023 const char * const pszName = pszzStr + paSyms[iSym].st_name;
1024
1025 /* String termination was already checked when the string table was mapped. */
1026 if ( *pszName != '\0'
1027 && ( (fFlags & RTLDR_ENUM_SYMBOL_FLAGS_ALL)
1028 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL) )
1029 {
1030 /*
1031 * Call back.
1032 */
1033 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
1034 rc = pfnCallback(pMod, pszName, iSym, (RTUINTPTR)Value, pvUser);
1035 if (rc)
1036 return rc;
1037 }
1038 }
1039 }
1040
1041 return VINF_SUCCESS;
1042}
1043
1044
1045/** @copydoc RTLDROPS::GetImageSize */
1046static DECLCALLBACK(size_t) RTLDRELF_NAME(GetImageSize)(PRTLDRMODINTERNAL pMod)
1047{
1048 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1049
1050 return pModElf->cbImage;
1051}
1052
1053
1054/** @copydoc RTLDROPS::GetBits */
1055static DECLCALLBACK(int) RTLDRELF_NAME(GetBits)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
1056{
1057 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1058
1059 /*
1060 * This operation is currently only available on relocatable images.
1061 */
1062 switch (pModElf->Ehdr.e_type)
1063 {
1064 case ET_REL:
1065 case ET_DYN:
1066 break;
1067 case ET_EXEC:
1068 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader)));
1069 return VERR_LDRELF_EXEC;
1070 default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
1071 }
1072
1073 /*
1074 * Load the bits into pvBits.
1075 */
1076 const Elf_Shdr *paShdrs = pModElf->paShdrs;
1077 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
1078 {
1079 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
1080 {
1081 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);
1082 switch (paShdrs[iShdr].sh_type)
1083 {
1084 case SHT_NOBITS:
1085 memset((uint8_t *)pvBits + paShdrs[iShdr].sh_addr, 0, (size_t)paShdrs[iShdr].sh_size);
1086 break;
1087
1088 case SHT_PROGBITS:
1089 default:
1090 {
1091 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, (uint8_t *)pvBits + paShdrs[iShdr].sh_addr,
1092 (size_t)paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset);
1093 if (RT_FAILURE(rc))
1094 {
1095 Log(("RTLdrELF: %s: Read error when reading " FMT_ELF_SIZE " bytes at " FMT_ELF_OFF ", iShdr=%d\n",
1096 pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader),
1097 paShdrs[iShdr].sh_size, paShdrs[iShdr].sh_offset, iShdr));
1098 return rc;
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1105 /*
1106 * Relocate the image.
1107 */
1108 return pModElf->Core.pOps->pfnRelocate(pMod, pvBits, BaseAddress, ~(RTUINTPTR)0, pfnGetImport, pvUser);
1109}
1110
1111
1112/** @copydoc RTLDROPS::Relocate */
1113static DECLCALLBACK(int) RTLDRELF_NAME(Relocate)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress,
1114 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
1115{
1116 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1117#ifdef LOG_ENABLED
1118 const char *pszLogName = pModElf->Core.pReader->pfnLogName(pModElf->Core.pReader);
1119#endif
1120 NOREF(OldBaseAddress);
1121
1122 /*
1123 * This operation is currently only available on relocatable images.
1124 */
1125 switch (pModElf->Ehdr.e_type)
1126 {
1127 case ET_REL:
1128 case ET_DYN:
1129 break;
1130 case ET_EXEC:
1131 Log(("RTLdrELF: %s: Executable images are not supported yet!\n", pszLogName));
1132 return VERR_LDRELF_EXEC;
1133 default: AssertFailedReturn(VERR_BAD_EXE_FORMAT);
1134 }
1135
1136 /*
1137 * Validate the input.
1138 */
1139 Elf_Addr BaseAddr = (Elf_Addr)NewBaseAddress;
1140 AssertMsgReturn((RTUINTPTR)BaseAddr == NewBaseAddress, ("%RTptr", NewBaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
1141
1142 /*
1143 * Map the image bits if not already done and setup pointer into it.
1144 */
1145 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
1146 if (RT_FAILURE(rc))
1147 return rc;
1148
1149 /*
1150 * Iterate the sections looking for interesting SHT_REL[A] sections.
1151 *
1152 * In ET_REL files the SHT_REL[A] sections have the section index of
1153 * the section they contain fixups for in the sh_info member.
1154 */
1155 const Elf_Shdr *paShdrs = pModElf->paShdrs;
1156 Log2(("rtLdrElf: %s: Fixing up image\n", pszLogName));
1157 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
1158 {
1159 const Elf_Shdr *pShdrRel = &paShdrs[iShdr];
1160
1161 /*
1162 * Skip sections without interest to us.
1163 */
1164#if ELF_MODE == 32
1165 if (pShdrRel->sh_type != SHT_REL)
1166#else
1167 if (pShdrRel->sh_type != SHT_RELA)
1168#endif
1169 continue;
1170 if (pModElf->Ehdr.e_type == ET_REL)
1171 {
1172 if (pShdrRel->sh_info >= pModElf->Ehdr.e_shnum)
1173 continue;
1174 const Elf_Shdr *pShdr = &paShdrs[pShdrRel->sh_info]; /* the section to fixup. */
1175 if (!(pShdr->sh_flags & SHF_ALLOC))
1176 continue;
1177
1178 /*
1179 * Relocate the section.
1180 */
1181 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",
1182 pszLogName, (int)pShdrRel->sh_info, ELF_SH_STR(pModElf, pShdr->sh_name), (int)pShdr->sh_info, (int)pShdr->sh_link,
1183 iShdr, ELF_SH_STR(pModElf, pShdrRel->sh_name), (int)pShdrRel->sh_info, (int)pShdrRel->sh_link));
1184
1185 rc = RTLDRELF_NAME(RelocateSectionRel)(pModElf, BaseAddr, pfnGetImport, pvUser,
1186 pShdr->sh_addr,
1187 pShdr->sh_size,
1188 (const uint8_t *)pModElf->pvBits + pShdr->sh_offset,
1189 (uint8_t *)pvBits + pShdr->sh_addr,
1190 (const uint8_t *)pModElf->pvBits + pShdrRel->sh_offset,
1191 pShdrRel->sh_size);
1192 }
1193 else
1194 rc = RTLDRELF_NAME(RelocateSectionExecDyn)(pModElf, BaseAddr, pfnGetImport, pvUser,
1195 0, (Elf_Size)pModElf->cbImage,
1196 (const uint8_t *)pModElf->pvBits /** @todo file offset ?? */,
1197 (uint8_t *)pvBits,
1198 (const uint8_t *)pModElf->pvBits + pShdrRel->sh_offset,
1199 pShdrRel->sh_size);
1200
1201 if (RT_FAILURE(rc))
1202 return rc;
1203 }
1204 return VINF_SUCCESS;
1205}
1206
1207
1208/**
1209 * Worker for pfnGetSymbolEx.
1210 */
1211static int RTLDRELF_NAME(ReturnSymbol)(PRTLDRMODELF pThis, const Elf_Sym *pSym, Elf_Addr uBaseAddr, PRTUINTPTR pValue)
1212{
1213 Elf_Addr Value;
1214 if (pSym->st_shndx == SHN_ABS)
1215 /* absolute symbols are not subject to any relocation. */
1216 Value = pSym->st_value;
1217 else if (pSym->st_shndx < pThis->Ehdr.e_shnum)
1218 {
1219 if (pThis->Ehdr.e_type == ET_REL)
1220 /* relative to the section. */
1221 Value = uBaseAddr + pSym->st_value + pThis->paShdrs[pSym->st_shndx].sh_addr;
1222 else /* Fixed up for link address. */
1223 Value = uBaseAddr + pSym->st_value - pThis->LinkAddress;
1224 }
1225 else
1226 {
1227 AssertMsgFailed(("Arg! pSym->st_shndx=%d\n", pSym->st_shndx));
1228 return VERR_BAD_EXE_FORMAT;
1229 }
1230 AssertMsgReturn(Value == (RTUINTPTR)Value, (FMT_ELF_ADDR "\n", Value), VERR_SYMBOL_VALUE_TOO_BIG);
1231 *pValue = (RTUINTPTR)Value;
1232 return VINF_SUCCESS;
1233}
1234
1235
1236/** @copydoc RTLDROPS::pfnGetSymbolEx */
1237static DECLCALLBACK(int) RTLDRELF_NAME(GetSymbolEx)(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress,
1238 uint32_t iOrdinal, const char *pszSymbol, RTUINTPTR *pValue)
1239{
1240 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1241 NOREF(pvBits);
1242
1243 /*
1244 * Validate the input.
1245 */
1246 Elf_Addr uBaseAddr = (Elf_Addr)BaseAddress;
1247 AssertMsgReturn((RTUINTPTR)uBaseAddr == BaseAddress, ("%RTptr", BaseAddress), VERR_IMAGE_BASE_TOO_HIGH);
1248
1249 /*
1250 * Map the image bits if not already done and setup pointer into it.
1251 */
1252 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
1253 if (RT_FAILURE(rc))
1254 return rc;
1255
1256 /*
1257 * Calc all kinds of pointers before we start iterating the symbol table.
1258 */
1259 const Elf_Sym *paSyms = pModElf->Rel.paSyms;
1260 unsigned cSyms = pModElf->Rel.cSyms;
1261 const char *pszzStr = pModElf->Rel.pStr;
1262 unsigned cbStr = pModElf->Rel.cbStr;
1263 if (pModElf->Dyn.cSyms > 0)
1264 {
1265 paSyms = pModElf->Dyn.paSyms;
1266 cSyms = pModElf->Dyn.cSyms;
1267 pszzStr = pModElf->Dyn.pStr;
1268 cbStr = pModElf->Dyn.cbStr;
1269 }
1270
1271 if (iOrdinal == UINT32_MAX)
1272 {
1273 for (unsigned iSym = 1; iSym < cSyms; iSym++)
1274 {
1275 /* Undefined symbols are not exports, they are imports. */
1276 if ( paSyms[iSym].st_shndx != SHN_UNDEF
1277 && ( ELF_ST_BIND(paSyms[iSym].st_info) == STB_GLOBAL
1278 || ELF_ST_BIND(paSyms[iSym].st_info) == STB_WEAK))
1279 {
1280 /* Validate the name string and try match with it. */
1281 AssertMsgReturn(paSyms[iSym].st_name < cbStr,
1282 ("String outside string table! iSym=%d paSyms[iSym].st_name=%#x\n", iSym, paSyms[iSym].st_name),
1283 VERR_LDRELF_INVALID_SYMBOL_NAME_OFFSET);
1284 if (!strcmp(pszSymbol, pszzStr + paSyms[iSym].st_name))
1285 {
1286 /* matched! */
1287 return RTLDRELF_NAME(ReturnSymbol)(pModElf, &paSyms[iSym], uBaseAddr, pValue);
1288 }
1289 }
1290 }
1291 }
1292 else if (iOrdinal < cSyms)
1293 {
1294 if ( paSyms[iOrdinal].st_shndx != SHN_UNDEF
1295 && ( ELF_ST_BIND(paSyms[iOrdinal].st_info) == STB_GLOBAL
1296 || ELF_ST_BIND(paSyms[iOrdinal].st_info) == STB_WEAK))
1297 return RTLDRELF_NAME(ReturnSymbol)(pModElf, &paSyms[iOrdinal], uBaseAddr, pValue);
1298 }
1299
1300 return VERR_SYMBOL_NOT_FOUND;
1301}
1302
1303
1304/** @copydoc RTLDROPS::pfnEnumDbgInfo */
1305static DECLCALLBACK(int) RTLDRELF_NAME(EnumDbgInfo)(PRTLDRMODINTERNAL pMod, const void *pvBits,
1306 PFNRTLDRENUMDBG pfnCallback, void *pvUser)
1307{
1308 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1309 RT_NOREF_PV(pvBits);
1310
1311 /*
1312 * Map the image bits if not already done and setup pointer into it.
1313 */
1314 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
1315 if (RT_FAILURE(rc))
1316 return rc;
1317
1318 /*
1319 * Do the enumeration.
1320 */
1321 const Elf_Shdr *paShdrs = pModElf->paOrgShdrs;
1322 for (unsigned iShdr = 0; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
1323 {
1324 /* Debug sections are expected to be PROGBITS and not allocated. */
1325 if (paShdrs[iShdr].sh_type != SHT_PROGBITS)
1326 continue;
1327 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
1328 continue;
1329
1330 RTLDRDBGINFO DbgInfo;
1331 const char *pszSectName = ELF_SH_STR(pModElf, paShdrs[iShdr].sh_name);
1332 if ( !strncmp(pszSectName, RT_STR_TUPLE(".debug_"))
1333 || !strcmp(pszSectName, ".WATCOM_references") )
1334 {
1335 RT_ZERO(DbgInfo.u);
1336 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF;
1337 DbgInfo.pszExtFile = NULL;
1338 DbgInfo.offFile = paShdrs[iShdr].sh_offset;
1339 DbgInfo.cb = paShdrs[iShdr].sh_size;
1340 DbgInfo.u.Dwarf.pszSection = pszSectName;
1341 }
1342 else if (!strcmp(pszSectName, ".gnu_debuglink"))
1343 {
1344 if ((paShdrs[iShdr].sh_size & 3) || paShdrs[iShdr].sh_size < 8)
1345 return VERR_BAD_EXE_FORMAT;
1346
1347 RT_ZERO(DbgInfo.u);
1348 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF_DWO;
1349 DbgInfo.pszExtFile = (const char *)((uintptr_t)pModElf->pvBits + (uintptr_t)paShdrs[iShdr].sh_offset);
1350 if (!RTStrEnd(DbgInfo.pszExtFile, paShdrs[iShdr].sh_size))
1351 return VERR_BAD_EXE_FORMAT;
1352 DbgInfo.u.Dwo.uCrc32 = *(uint32_t *)((uintptr_t)DbgInfo.pszExtFile + (uintptr_t)paShdrs[iShdr].sh_size
1353 - sizeof(uint32_t));
1354 DbgInfo.offFile = -1;
1355 DbgInfo.cb = 0;
1356 }
1357 else
1358 continue;
1359
1360 DbgInfo.LinkAddress = NIL_RTLDRADDR;
1361 DbgInfo.iDbgInfo = iShdr - 1;
1362
1363 rc = pfnCallback(pMod, &DbgInfo, pvUser);
1364 if (rc != VINF_SUCCESS)
1365 return rc;
1366
1367 }
1368
1369 return VINF_SUCCESS;
1370}
1371
1372
1373/**
1374 * Locate the next allocated section by RVA (sh_addr).
1375 *
1376 * This is a helper for EnumSegments and SegOffsetToRva.
1377 *
1378 * @returns Pointer to the section header if found, NULL if none.
1379 * @param pModElf The module instance.
1380 * @param iShdrCur The current section header.
1381 */
1382static const Elf_Shdr *RTLDRELF_NAME(GetNextAllocatedSection)(PRTLDRMODELF pModElf, unsigned iShdrCur)
1383{
1384 unsigned const cShdrs = pModElf->Ehdr.e_shnum;
1385 const Elf_Shdr * const paShdrs = pModElf->paShdrs;
1386 if (pModElf->fShdrInOrder)
1387 {
1388 for (unsigned iShdr = iShdrCur + 1; iShdr < cShdrs; iShdr++)
1389 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
1390 return &paShdrs[iShdr];
1391 }
1392 else
1393 {
1394 Elf_Addr const uEndCur = paShdrs[iShdrCur].sh_addr + paShdrs[iShdrCur].sh_size;
1395 Elf_Addr offBest = ~(Elf_Addr)0;
1396 unsigned iBest = cShdrs;
1397 for (unsigned iShdr = pModElf->iFirstSect; iShdr < cShdrs; iShdr++)
1398 if ((paShdrs[iShdr].sh_flags & SHF_ALLOC) && iShdr != iShdrCur)
1399 {
1400 Elf_Addr const offDelta = paShdrs[iShdr].sh_addr - uEndCur;
1401 if ( offDelta < offBest
1402 && paShdrs[iShdr].sh_addr >= uEndCur)
1403 {
1404 offBest = offDelta;
1405 iBest = iShdr;
1406 }
1407 }
1408 if (iBest < cShdrs)
1409 return &paShdrs[iBest];
1410 }
1411 return NULL;
1412}
1413
1414
1415/** @copydoc RTLDROPS::pfnEnumSegments. */
1416static DECLCALLBACK(int) RTLDRELF_NAME(EnumSegments)(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
1417{
1418 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1419
1420 /*
1421 * Map the image bits if not already done and setup pointer into it.
1422 */
1423 int rc = RTLDRELF_NAME(MapBits)(pModElf, true);
1424 if (RT_FAILURE(rc))
1425 return rc;
1426
1427 /*
1428 * Do the enumeration.
1429 */
1430 char szName[32];
1431 const Elf_Shdr *paShdrs = pModElf->paShdrs;
1432 const Elf_Shdr *paOrgShdrs = pModElf->paOrgShdrs;
1433 for (unsigned iShdr = pModElf->iFirstSect; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
1434 {
1435 RTLDRSEG Seg;
1436 if (iShdr != 0)
1437 {
1438 Seg.pszName = ELF_SH_STR(pModElf, paShdrs[iShdr].sh_name);
1439 Seg.cchName = (uint32_t)strlen(Seg.pszName);
1440 if (Seg.cchName == 0)
1441 {
1442 Seg.pszName = szName;
1443 Seg.cchName = (uint32_t)RTStrPrintf(szName, sizeof(szName), "UnamedSect%02u", iShdr);
1444 }
1445 }
1446 else
1447 {
1448 Seg.pszName = ".elf.headers";
1449 Seg.cchName = 12;
1450 }
1451 Seg.SelFlat = 0;
1452 Seg.Sel16bit = 0;
1453 Seg.fFlags = 0;
1454 Seg.fProt = RTMEM_PROT_READ;
1455 if (paShdrs[iShdr].sh_flags & SHF_WRITE)
1456 Seg.fProt |= RTMEM_PROT_WRITE;
1457 if (paShdrs[iShdr].sh_flags & SHF_EXECINSTR)
1458 Seg.fProt |= RTMEM_PROT_EXEC;
1459 Seg.cb = paShdrs[iShdr].sh_size;
1460 Seg.Alignment = paShdrs[iShdr].sh_addralign;
1461 if (paShdrs[iShdr].sh_flags & SHF_ALLOC)
1462 {
1463 Seg.LinkAddress = paOrgShdrs[iShdr].sh_addr;
1464 Seg.RVA = paShdrs[iShdr].sh_addr;
1465 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetNextAllocatedSection)(pModElf, iShdr);
1466 if (pShdr2)
1467 Seg.cbMapped = pShdr2->sh_addr - paShdrs[iShdr].sh_addr;
1468 else
1469 Seg.cbMapped = pModElf->cbImage - paShdrs[iShdr].sh_addr;
1470 }
1471 else
1472 {
1473 Seg.LinkAddress = NIL_RTLDRADDR;
1474 Seg.RVA = NIL_RTLDRADDR;
1475 Seg.cbMapped = NIL_RTLDRADDR;
1476 }
1477 if (paShdrs[iShdr].sh_type != SHT_NOBITS)
1478 {
1479 Seg.offFile = paShdrs[iShdr].sh_offset;
1480 Seg.cbFile = paShdrs[iShdr].sh_size;
1481 }
1482 else
1483 {
1484 Seg.offFile = -1;
1485 Seg.cbFile = 0;
1486 }
1487
1488 rc = pfnCallback(pMod, &Seg, pvUser);
1489 if (rc != VINF_SUCCESS)
1490 return rc;
1491 }
1492
1493 return VINF_SUCCESS;
1494}
1495
1496
1497/** @copydoc RTLDROPS::pfnLinkAddressToSegOffset. */
1498static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress,
1499 uint32_t *piSeg, PRTLDRADDR poffSeg)
1500{
1501 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1502
1503 const Elf_Shdr *pShdrEnd = NULL;
1504 unsigned cLeft = pModElf->Ehdr.e_shnum - pModElf->iFirstSect;
1505 const Elf_Shdr *pShdr = &pModElf->paOrgShdrs[pModElf->Ehdr.e_shnum];
1506 while (cLeft-- > 0)
1507 {
1508 pShdr--;
1509 if (pShdr->sh_flags & SHF_ALLOC)
1510 {
1511 RTLDRADDR offSeg = LinkAddress - pShdr->sh_addr;
1512 if (offSeg < pShdr->sh_size)
1513 {
1514 *poffSeg = offSeg;
1515 *piSeg = cLeft;
1516 return VINF_SUCCESS;
1517 }
1518 if (offSeg == pShdr->sh_size)
1519 pShdrEnd = pShdr;
1520 }
1521 }
1522
1523 if (pShdrEnd)
1524 {
1525 *poffSeg = pShdrEnd->sh_size;
1526 *piSeg = pShdrEnd - pModElf->paOrgShdrs - pModElf->iFirstSect;
1527 return VINF_SUCCESS;
1528 }
1529
1530 return VERR_LDR_INVALID_LINK_ADDRESS;
1531}
1532
1533
1534/** @copydoc RTLDROPS::pfnLinkAddressToRva. */
1535static DECLCALLBACK(int) RTLDRELF_NAME(LinkAddressToRva)(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
1536{
1537 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1538 uint32_t iSeg;
1539 RTLDRADDR offSeg;
1540 int rc = RTLDRELF_NAME(LinkAddressToSegOffset)(pMod, LinkAddress, &iSeg, &offSeg);
1541 if (RT_SUCCESS(rc))
1542 *pRva = pModElf->paShdrs[iSeg + pModElf->iFirstSect].sh_addr + offSeg;
1543 return rc;
1544}
1545
1546
1547/** @copydoc RTLDROPS::pfnSegOffsetToRva. */
1548static DECLCALLBACK(int) RTLDRELF_NAME(SegOffsetToRva)(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg,
1549 PRTLDRADDR pRva)
1550{
1551 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1552 if (iSeg >= pModElf->Ehdr.e_shnum - pModElf->iFirstSect)
1553 return VERR_LDR_INVALID_SEG_OFFSET;
1554
1555 iSeg += pModElf->iFirstSect; /* skip section 0 if not used */
1556 if (offSeg > pModElf->paShdrs[iSeg].sh_size)
1557 {
1558 const Elf_Shdr *pShdr2 = RTLDRELF_NAME(GetNextAllocatedSection)(pModElf, iSeg);
1559 if ( !pShdr2
1560 || offSeg > (pShdr2->sh_addr - pModElf->paShdrs[iSeg].sh_addr))
1561 return VERR_LDR_INVALID_SEG_OFFSET;
1562 }
1563
1564 if (!(pModElf->paShdrs[iSeg].sh_flags & SHF_ALLOC))
1565 return VERR_LDR_INVALID_SEG_OFFSET;
1566
1567 *pRva = pModElf->paShdrs[iSeg].sh_addr;
1568 return VINF_SUCCESS;
1569}
1570
1571
1572/** @copydoc RTLDROPS::pfnRvaToSegOffset. */
1573static DECLCALLBACK(int) RTLDRELF_NAME(RvaToSegOffset)(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva,
1574 uint32_t *piSeg, PRTLDRADDR poffSeg)
1575{
1576 PRTLDRMODELF pModElf = (PRTLDRMODELF)pMod;
1577 Elf_Addr PrevAddr = 0;
1578 unsigned cLeft = pModElf->Ehdr.e_shnum - pModElf->iFirstSect;
1579 const Elf_Shdr *pShdr = &pModElf->paShdrs[pModElf->Ehdr.e_shnum];
1580 while (cLeft-- > 0)
1581 {
1582 pShdr--;
1583 if (pShdr->sh_flags & SHF_ALLOC)
1584 {
1585 Elf_Addr cbSeg = PrevAddr ? PrevAddr - pShdr->sh_addr : pShdr->sh_size;
1586 RTLDRADDR offSeg = Rva - pShdr->sh_addr;
1587 if (offSeg <= cbSeg)
1588 {
1589 *poffSeg = offSeg;
1590 *piSeg = cLeft;
1591 return VINF_SUCCESS;
1592 }
1593 PrevAddr = pShdr->sh_addr;
1594 }
1595 }
1596
1597 return VERR_LDR_INVALID_RVA;
1598}
1599
1600
1601/** @callback_method_impl{FNRTLDRIMPORT, Stub used by ReadDbgInfo.} */
1602static DECLCALLBACK(int) RTLDRELF_NAME(GetImportStubCallback)(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
1603 unsigned uSymbol, PRTLDRADDR pValue, void *pvUser)
1604{
1605 RT_NOREF_PV(hLdrMod); RT_NOREF_PV(pszModule); RT_NOREF_PV(pszSymbol);
1606 RT_NOREF_PV(uSymbol); RT_NOREF_PV(pValue); RT_NOREF_PV(pvUser);
1607 return VERR_SYMBOL_NOT_FOUND;
1608}
1609
1610
1611/** @copydoc RTLDROPS::pfnReadDbgInfo. */
1612static DECLCALLBACK(int) RTLDRELF_NAME(ReadDbgInfo)(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off,
1613 size_t cb, void *pvBuf)
1614{
1615 PRTLDRMODELF pThis = (PRTLDRMODELF)pMod;
1616 LogFlow(("%s: iDbgInfo=%#x off=%RTfoff cb=%#zu\n", __FUNCTION__, iDbgInfo, off, cb));
1617
1618 /*
1619 * Input validation.
1620 */
1621 AssertReturn(iDbgInfo < pThis->Ehdr.e_shnum && iDbgInfo + 1 < pThis->Ehdr.e_shnum, VERR_INVALID_PARAMETER);
1622 iDbgInfo++;
1623 AssertReturn(!(pThis->paShdrs[iDbgInfo].sh_flags & SHF_ALLOC), VERR_INVALID_PARAMETER);
1624 AssertReturn(pThis->paShdrs[iDbgInfo].sh_type == SHT_PROGBITS, VERR_INVALID_PARAMETER);
1625 AssertReturn(pThis->paShdrs[iDbgInfo].sh_offset == (uint64_t)off, VERR_INVALID_PARAMETER);
1626 AssertReturn(pThis->paShdrs[iDbgInfo].sh_size == cb, VERR_INVALID_PARAMETER);
1627 uint64_t cbRawImage = pThis->Core.pReader->pfnSize(pThis->Core.pReader);
1628 AssertReturn(off >= 0 && cb <= cbRawImage && (uint64_t)off + cb <= cbRawImage, VERR_INVALID_PARAMETER);
1629
1630 /*
1631 * Read it from the file and look for fixup sections.
1632 */
1633 int rc;
1634 if (pThis->pvBits)
1635 memcpy(pvBuf, (const uint8_t *)pThis->pvBits + (size_t)off, cb);
1636 else
1637 {
1638 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off);
1639 if (RT_FAILURE(rc))
1640 return rc;
1641 }
1642
1643 uint32_t iRelocs = iDbgInfo + 1;
1644 if ( iRelocs >= pThis->Ehdr.e_shnum
1645 || pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1646 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1647 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA) )
1648 {
1649 iRelocs = 0;
1650 while ( iRelocs < pThis->Ehdr.e_shnum
1651 && ( pThis->paShdrs[iRelocs].sh_info != iDbgInfo
1652 || ( pThis->paShdrs[iRelocs].sh_type != SHT_REL
1653 && pThis->paShdrs[iRelocs].sh_type != SHT_RELA)) )
1654 iRelocs++;
1655 }
1656 if ( iRelocs < pThis->Ehdr.e_shnum
1657 && pThis->paShdrs[iRelocs].sh_size > 0)
1658 {
1659 /*
1660 * Load the relocations.
1661 */
1662 uint8_t *pbRelocsBuf = NULL;
1663 const uint8_t *pbRelocs;
1664 if (pThis->pvBits)
1665 pbRelocs = (const uint8_t *)pThis->pvBits + pThis->paShdrs[iRelocs].sh_offset;
1666 else
1667 {
1668 pbRelocs = pbRelocsBuf = (uint8_t *)RTMemTmpAlloc(pThis->paShdrs[iRelocs].sh_size);
1669 if (!pbRelocsBuf)
1670 return VERR_NO_TMP_MEMORY;
1671 rc = pThis->Core.pReader->pfnRead(pThis->Core.pReader, pbRelocsBuf,
1672 pThis->paShdrs[iRelocs].sh_size,
1673 pThis->paShdrs[iRelocs].sh_offset);
1674 if (RT_FAILURE(rc))
1675 {
1676 RTMemTmpFree(pbRelocsBuf);
1677 return rc;
1678 }
1679 }
1680
1681 /*
1682 * Apply the relocations.
1683 */
1684 if (pThis->Ehdr.e_type == ET_REL)
1685 rc = RTLDRELF_NAME(RelocateSectionRel)(pThis, pThis->LinkAddress,
1686 RTLDRELF_NAME(GetImportStubCallback), NULL /*pvUser*/,
1687 pThis->paShdrs[iDbgInfo].sh_addr,
1688 pThis->paShdrs[iDbgInfo].sh_size,
1689 (const uint8_t *)pvBuf,
1690 (uint8_t *)pvBuf,
1691 pbRelocs,
1692 pThis->paShdrs[iRelocs].sh_size);
1693 else
1694 rc = RTLDRELF_NAME(RelocateSectionExecDyn)(pThis, pThis->LinkAddress,
1695 RTLDRELF_NAME(GetImportStubCallback), NULL /*pvUser*/,
1696 pThis->paShdrs[iDbgInfo].sh_addr,
1697 pThis->paShdrs[iDbgInfo].sh_size,
1698 (const uint8_t *)pvBuf,
1699 (uint8_t *)pvBuf,
1700 pbRelocs,
1701 pThis->paShdrs[iRelocs].sh_size);
1702
1703 RTMemTmpFree(pbRelocsBuf);
1704 }
1705 else
1706 rc = VINF_SUCCESS;
1707 return rc;
1708}
1709
1710
1711/**
1712 * Handles RTLDRPROP_BUILDID queries.
1713 */
1714static int RTLDRELF_NAME(QueryPropBuildId)(PRTLDRMODELF pThis, void *pvBuf, size_t cbBuf, size_t *pcbRet)
1715{
1716 /*
1717 * Map the image bits if not already done and setup pointer into it.
1718 */
1719 int rc = RTLDRELF_NAME(MapBits)(pThis, true);
1720 if (RT_FAILURE(rc))
1721 return rc;
1722
1723 /*
1724 * Search for the build ID.
1725 */
1726 const Elf_Shdr *paShdrs = pThis->paOrgShdrs;
1727 for (unsigned iShdr = 0; iShdr < pThis->Ehdr.e_shnum; iShdr++)
1728 {
1729 const char *pszSectName = ELF_SH_STR(pThis, paShdrs[iShdr].sh_name);
1730
1731 if (!strcmp(pszSectName, ".note.gnu.build-id"))
1732 {
1733 if ((paShdrs[iShdr].sh_size & 3) || paShdrs[iShdr].sh_size < sizeof(Elf_Nhdr))
1734 return VERR_BAD_EXE_FORMAT;
1735
1736 Elf_Nhdr *pNHdr = (Elf_Nhdr *)((uintptr_t)pThis->pvBits + (uintptr_t)paShdrs[iShdr].sh_offset);
1737 if ( pNHdr->n_namesz > paShdrs[iShdr].sh_size
1738 || pNHdr->n_descsz > paShdrs[iShdr].sh_size
1739 || (paShdrs[iShdr].sh_size - pNHdr->n_descsz) < pNHdr->n_namesz
1740 || pNHdr->n_type != NT_GNU_BUILD_ID)
1741 return VERR_BAD_EXE_FORMAT;
1742
1743 const char *pszOwner = (const char *)(pNHdr + 1);
1744 if ( !RTStrEnd(pszOwner, pNHdr->n_namesz)
1745 || strcmp(pszOwner, "GNU"))
1746 return VERR_BAD_EXE_FORMAT;
1747
1748 if (cbBuf < pNHdr->n_descsz)
1749 return VERR_BUFFER_OVERFLOW;
1750
1751 memcpy(pvBuf, pszOwner + pNHdr->n_namesz, pNHdr->n_descsz);
1752 *pcbRet = pNHdr->n_descsz;
1753 return VINF_SUCCESS;
1754 }
1755 }
1756
1757 return VERR_NOT_FOUND;
1758}
1759
1760
1761/** @interface_method_impl{RTLDROPS,pfnQueryProp} */
1762static DECLCALLBACK(int) RTLDRELF_NAME(QueryProp)(PRTLDRMODINTERNAL pMod, RTLDRPROP enmProp, void const *pvBits,
1763 void *pvBuf, size_t cbBuf, size_t *pcbRet)
1764{
1765 PRTLDRMODELF pThis = (PRTLDRMODELF)pMod;
1766 RT_NOREF(pvBits);
1767 switch (enmProp)
1768 {
1769 case RTLDRPROP_BUILDID:
1770 return RTLDRELF_NAME(QueryPropBuildId)(pThis, pvBuf, cbBuf, pcbRet);
1771
1772 case RTLDRPROP_IS_SIGNED:
1773 *pcbRet = sizeof(bool);
1774 return rtLdrELFLnxKModQueryPropIsSigned(pThis->Core.pReader, (bool *)pvBuf);
1775
1776 case RTLDRPROP_PKCS7_SIGNED_DATA:
1777 *pcbRet = sizeof(bool);
1778 return rtLdrELFLnxKModQueryPropPkcs7SignedData(pThis->Core.pReader, pvBuf, cbBuf, pcbRet);
1779
1780 default:
1781 return VERR_NOT_FOUND;
1782 }
1783}
1784
1785
1786/**
1787 * @interface_method_impl{RTLDROPS,pfnUnwindFrame}
1788 */
1789static DECLCALLBACK(int)
1790RTLDRELF_NAME(UnwindFrame)(PRTLDRMODINTERNAL pMod, void const *pvBits, uint32_t iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
1791{
1792 PRTLDRMODELF pThis = (PRTLDRMODELF)pMod;
1793 LogFlow(("%s: iSeg=%#x off=%RTptr\n", __FUNCTION__, iSeg, off));
1794
1795 /*
1796 * Process the input address, making us both RVA and proper seg:offset out of it.
1797 */
1798 int rc;
1799 RTLDRADDR uRva = off;
1800 if (iSeg == UINT32_MAX)
1801 rc = RTLDRELF_NAME(RvaToSegOffset)(pMod, uRva, &iSeg, &off);
1802 else
1803 rc = RTLDRELF_NAME(SegOffsetToRva)(pMod, iSeg, off, &uRva);
1804 AssertRCReturn(rc, rc);
1805
1806 /*
1807 * Map the image bits if not already done and setup pointer into it.
1808 */
1809 RT_NOREF(pvBits); /** @todo Try use passed in pvBits? */
1810 rc = RTLDRELF_NAME(MapBits)(pThis, true);
1811 if (RT_FAILURE(rc))
1812 return rc;
1813
1814 /*
1815 * Do we need to search for .eh_frame and .eh_frame_hdr?
1816 */
1817 if (pThis->iShEhFrame == 0)
1818 {
1819 pThis->iShEhFrame = ~0U;
1820 pThis->iShEhFrameHdr = ~0U;
1821 unsigned cLeft = 2;
1822 for (unsigned iShdr = 1; iShdr < pThis->Ehdr.e_shnum; iShdr++)
1823 {
1824 const char *pszName = ELF_SH_STR(pThis, pThis->paShdrs[iShdr].sh_name);
1825 if ( pszName[0] == '.'
1826 && pszName[1] == 'e'
1827 && pszName[2] == 'h'
1828 && pszName[3] == '_'
1829 && pszName[4] == 'f'
1830 && pszName[5] == 'r'
1831 && pszName[6] == 'a'
1832 && pszName[7] == 'm'
1833 && pszName[8] == 'e')
1834 {
1835 if (pszName[9] == '\0')
1836 pThis->iShEhFrame = iShdr;
1837 else if ( pszName[9] == '_'
1838 && pszName[10] == 'h'
1839 && pszName[11] == 'd'
1840 && pszName[12] == 'r'
1841 && pszName[13] == '\0')
1842 pThis->iShEhFrameHdr = iShdr;
1843 else
1844 continue;
1845 if (--cLeft == 0)
1846 break;
1847 }
1848 }
1849 }
1850
1851 /*
1852 * Any info present?
1853 */
1854 unsigned iShdr = pThis->iShEhFrame;
1855 if ( iShdr != ~0U
1856 && pThis->paShdrs[iShdr].sh_size > 0)
1857 {
1858 if (pThis->paShdrs[iShdr].sh_flags & SHF_ALLOC)
1859 return rtDwarfUnwind_EhData((uint8_t const *)pThis->pvBits + pThis->paShdrs[iShdr].sh_addr,
1860 pThis->paShdrs[iShdr].sh_size, pThis->paShdrs[iShdr].sh_addr,
1861 iSeg, off, uRva, pState, pThis->Core.enmArch);
1862 }
1863 return VERR_DBG_NO_UNWIND_INFO;
1864}
1865
1866
1867/**
1868 * The ELF module operations.
1869 */
1870static RTLDROPS RTLDRELF_MID(s_rtldrElf,Ops) =
1871{
1872#if ELF_MODE == 32
1873 "elf32",
1874#elif ELF_MODE == 64
1875 "elf64",
1876#endif
1877 RTLDRELF_NAME(Close),
1878 NULL, /* Get Symbol */
1879 RTLDRELF_NAME(Done),
1880 RTLDRELF_NAME(EnumSymbols),
1881 /* ext: */
1882 RTLDRELF_NAME(GetImageSize),
1883 RTLDRELF_NAME(GetBits),
1884 RTLDRELF_NAME(Relocate),
1885 RTLDRELF_NAME(GetSymbolEx),
1886 NULL /*pfnQueryForwarderInfo*/,
1887 RTLDRELF_NAME(EnumDbgInfo),
1888 RTLDRELF_NAME(EnumSegments),
1889 RTLDRELF_NAME(LinkAddressToSegOffset),
1890 RTLDRELF_NAME(LinkAddressToRva),
1891 RTLDRELF_NAME(SegOffsetToRva),
1892 RTLDRELF_NAME(RvaToSegOffset),
1893 RTLDRELF_NAME(ReadDbgInfo),
1894 RTLDRELF_NAME(QueryProp),
1895 NULL /*pfnVerifySignature*/,
1896 rtldrELFLnxKModHashImage,
1897 RTLDRELF_NAME(UnwindFrame),
1898 42
1899};
1900
1901
1902
1903/**
1904 * Validates the ELF header.
1905 *
1906 * @returns iprt status code.
1907 * @param pEhdr Pointer to the ELF header.
1908 * @param cbRawImage The size of the raw image.
1909 * @param pszLogName The log name.
1910 * @param penmArch Where to return the architecture.
1911 * @param pErrInfo Where to return extended error info. Optional.
1912 */
1913static int RTLDRELF_NAME(ValidateElfHeader)(const Elf_Ehdr *pEhdr, uint64_t cbRawImage, const char *pszLogName,
1914 PRTLDRARCH penmArch, PRTERRINFO pErrInfo)
1915{
1916 Log3(("RTLdrELF: e_ident: %.*Rhxs\n"
1917 "RTLdrELF: e_type: " FMT_ELF_HALF "\n"
1918 "RTLdrELF: e_version: " FMT_ELF_HALF "\n"
1919 "RTLdrELF: e_entry: " FMT_ELF_ADDR "\n"
1920 "RTLdrELF: e_phoff: " FMT_ELF_OFF "\n"
1921 "RTLdrELF: e_shoff: " FMT_ELF_OFF "\n"
1922 "RTLdrELF: e_flags: " FMT_ELF_WORD "\n"
1923 "RTLdrELF: e_ehsize: " FMT_ELF_HALF "\n"
1924 "RTLdrELF: e_phentsize: " FMT_ELF_HALF "\n"
1925 "RTLdrELF: e_phnum: " FMT_ELF_HALF "\n"
1926 "RTLdrELF: e_shentsize: " FMT_ELF_HALF "\n"
1927 "RTLdrELF: e_shnum: " FMT_ELF_HALF "\n"
1928 "RTLdrELF: e_shstrndx: " FMT_ELF_HALF "\n",
1929 RT_ELEMENTS(pEhdr->e_ident), &pEhdr->e_ident[0], pEhdr->e_type, pEhdr->e_version,
1930 pEhdr->e_entry, pEhdr->e_phoff, pEhdr->e_shoff,pEhdr->e_flags, pEhdr->e_ehsize, pEhdr->e_phentsize,
1931 pEhdr->e_phnum, pEhdr->e_shentsize, pEhdr->e_shnum, pEhdr->e_shstrndx));
1932
1933 if ( pEhdr->e_ident[EI_MAG0] != ELFMAG0
1934 || pEhdr->e_ident[EI_MAG1] != ELFMAG1
1935 || pEhdr->e_ident[EI_MAG2] != ELFMAG2
1936 || pEhdr->e_ident[EI_MAG3] != ELFMAG3)
1937 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
1938 "%s: Invalid ELF magic (%.*Rhxs)", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident);
1939 if (pEhdr->e_ident[EI_CLASS] != RTLDRELF_SUFF(ELFCLASS))
1940 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
1941 "%s: Invalid ELF class (%.*Rhxs)", pszLogName, sizeof(pEhdr->e_ident), pEhdr->e_ident);
1942 if (pEhdr->e_ident[EI_DATA] != ELFDATA2LSB)
1943 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_ODD_ENDIAN,
1944 "%s: ELF endian %x is unsupported", pszLogName, pEhdr->e_ident[EI_DATA]);
1945 if (pEhdr->e_version != EV_CURRENT)
1946 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_VERSION,
1947 "%s: ELF version %x is unsupported", pszLogName, pEhdr->e_version);
1948
1949 if (sizeof(Elf_Ehdr) != pEhdr->e_ehsize)
1950 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
1951 "%s: Elf header e_ehsize is %d expected %d!", pszLogName, pEhdr->e_ehsize, sizeof(Elf_Ehdr));
1952 if ( sizeof(Elf_Phdr) != pEhdr->e_phentsize
1953 && ( pEhdr->e_phnum != 0
1954 || pEhdr->e_type == ET_DYN
1955 || pEhdr->e_type == ET_EXEC))
1956 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Elf header e_phentsize is %d expected %d!",
1957 pszLogName, pEhdr->e_phentsize, sizeof(Elf_Phdr));
1958 if (sizeof(Elf_Shdr) != pEhdr->e_shentsize)
1959 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Elf header e_shentsize is %d expected %d!",
1960 pszLogName, pEhdr->e_shentsize, sizeof(Elf_Shdr));
1961
1962 switch (pEhdr->e_type)
1963 {
1964 case ET_REL:
1965 case ET_EXEC:
1966 case ET_DYN:
1967 break;
1968 default:
1969 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: image type %#x is not supported!",
1970 pszLogName, pEhdr->e_type);
1971 }
1972
1973 switch (pEhdr->e_machine)
1974 {
1975#if ELF_MODE == 32
1976 case EM_386:
1977 case EM_486:
1978 *penmArch = RTLDRARCH_X86_32;
1979 break;
1980#elif ELF_MODE == 64
1981 case EM_X86_64:
1982 *penmArch = RTLDRARCH_AMD64;
1983 break;
1984 case EM_AARCH64:
1985 *penmArch = RTLDRARCH_ARM64;
1986 break;
1987#endif
1988 default:
1989 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_MACHINE,
1990 "%s: machine type %u is not supported!", pszLogName, pEhdr->e_machine);
1991 }
1992
1993 if ( pEhdr->e_phoff < pEhdr->e_ehsize
1994 && !(pEhdr->e_phoff && pEhdr->e_phnum)
1995 && pEhdr->e_phnum)
1996 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
1997 "%s: The program headers overlap with the ELF header! e_phoff=" FMT_ELF_OFF,
1998 pszLogName, pEhdr->e_phoff);
1999 if ( pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize > cbRawImage
2000 || pEhdr->e_phoff + pEhdr->e_phnum * pEhdr->e_phentsize < pEhdr->e_phoff)
2001 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2002 "%s: The program headers extends beyond the file! e_phoff=" FMT_ELF_OFF " e_phnum=" FMT_ELF_HALF,
2003 pszLogName, pEhdr->e_phoff, pEhdr->e_phnum);
2004
2005
2006 if ( pEhdr->e_shoff < pEhdr->e_ehsize
2007 && !(pEhdr->e_shoff && pEhdr->e_shnum))
2008 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2009 "%s: The section headers overlap with the ELF header! e_shoff=" FMT_ELF_OFF,
2010 pszLogName, pEhdr->e_shoff);
2011 if ( pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize > cbRawImage
2012 || pEhdr->e_shoff + pEhdr->e_shnum * pEhdr->e_shentsize < pEhdr->e_shoff)
2013 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2014 "%s: The section headers extends beyond the file! e_shoff=" FMT_ELF_OFF " e_shnum=" FMT_ELF_HALF,
2015 pszLogName, pEhdr->e_shoff, pEhdr->e_shnum);
2016
2017 if (pEhdr->e_shstrndx == 0 || pEhdr->e_shstrndx > pEhdr->e_shnum)
2018 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2019 "%s: The section headers string table is out of bounds! e_shstrndx=" FMT_ELF_HALF " e_shnum=" FMT_ELF_HALF,
2020 pszLogName, pEhdr->e_shstrndx, pEhdr->e_shnum);
2021
2022 return VINF_SUCCESS;
2023}
2024
2025
2026/**
2027 * Gets the section header name.
2028 *
2029 * @returns pszName.
2030 * @param pEhdr The elf header.
2031 * @param offName The offset of the section header name.
2032 * @param pszName Where to store the name.
2033 * @param cbName The size of the buffer pointed to by pszName.
2034 */
2035static const char *RTLDRELF_NAME(GetSHdrName)(PRTLDRMODELF pModElf, Elf_Word offName, char *pszName, size_t cbName)
2036{
2037 RTFOFF off = pModElf->paShdrs[pModElf->Ehdr.e_shstrndx].sh_offset + offName;
2038 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName, cbName - 1, off);
2039 if (RT_FAILURE(rc))
2040 {
2041 /* read by for byte. */
2042 for (unsigned i = 0; i < cbName; i++, off++)
2043 {
2044 rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, pszName + i, 1, off);
2045 if (RT_FAILURE(rc))
2046 {
2047 pszName[i] = '\0';
2048 break;
2049 }
2050 }
2051 }
2052
2053 pszName[cbName - 1] = '\0';
2054 return pszName;
2055}
2056
2057
2058/**
2059 * Validates a section header.
2060 *
2061 * @returns iprt status code.
2062 * @param pModElf Pointer to the module structure.
2063 * @param iShdr The index of section header which should be validated.
2064 * The section headers are found in the pModElf->paShdrs array.
2065 * @param cbRawImage The size of the raw image.
2066 * @param pszLogName The log name.
2067 * @param pErrInfo Where to return extended error info. Optional.
2068 */
2069static int RTLDRELF_NAME(ValidateSectionHeader)(PRTLDRMODELF pModElf, unsigned iShdr, uint64_t cbRawImage,
2070 const char *pszLogName, PRTERRINFO pErrInfo)
2071{
2072 const Elf_Shdr *pShdr = &pModElf->paShdrs[iShdr];
2073 char szSectionName[80]; NOREF(szSectionName);
2074 Log3(("RTLdrELF: Section Header #%d:\n"
2075 "RTLdrELF: sh_name: " FMT_ELF_WORD " - %s\n"
2076 "RTLdrELF: sh_type: " FMT_ELF_WORD " (%s)\n"
2077 "RTLdrELF: sh_flags: " FMT_ELF_XWORD "\n"
2078 "RTLdrELF: sh_addr: " FMT_ELF_ADDR "\n"
2079 "RTLdrELF: sh_offset: " FMT_ELF_OFF "\n"
2080 "RTLdrELF: sh_size: " FMT_ELF_XWORD "\n"
2081 "RTLdrELF: sh_link: " FMT_ELF_WORD "\n"
2082 "RTLdrELF: sh_info: " FMT_ELF_WORD "\n"
2083 "RTLdrELF: sh_addralign: " FMT_ELF_XWORD "\n"
2084 "RTLdrELF: sh_entsize: " FMT_ELF_XWORD "\n",
2085 iShdr,
2086 pShdr->sh_name, RTLDRELF_NAME(GetSHdrName)(pModElf, pShdr->sh_name, szSectionName, sizeof(szSectionName)),
2087 pShdr->sh_type, rtldrElfGetShdrType(pShdr->sh_type), pShdr->sh_flags, pShdr->sh_addr,
2088 pShdr->sh_offset, pShdr->sh_size, pShdr->sh_link, pShdr->sh_info, pShdr->sh_addralign,
2089 pShdr->sh_entsize));
2090
2091 if (iShdr == 0)
2092 {
2093 if ( pShdr->sh_name != 0
2094 || pShdr->sh_type != SHT_NULL
2095 || pShdr->sh_flags != 0
2096 || pShdr->sh_addr != 0
2097 || pShdr->sh_size != 0
2098 || pShdr->sh_offset != 0
2099 || pShdr->sh_link != SHN_UNDEF
2100 || pShdr->sh_addralign != 0
2101 || pShdr->sh_entsize != 0 )
2102 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2103 "%s: Bad #0 section: %.*Rhxs", pszLogName, sizeof(*pShdr), pShdr);
2104 return VINF_SUCCESS;
2105 }
2106
2107 if (pShdr->sh_name >= pModElf->cbShStr)
2108 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2109 "%s: Shdr #%d: sh_name (%d) is beyond the end of the section header string table (%d)!",
2110 pszLogName, iShdr, pShdr->sh_name, pModElf->cbShStr);
2111
2112 if (pShdr->sh_link >= pModElf->Ehdr.e_shnum)
2113 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2114 "%s: Shdr #%d: sh_link (%d) is beyond the end of the section table (%d)!",
2115 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum);
2116
2117 switch (pShdr->sh_type)
2118 {
2119 /** @todo find specs and check up which sh_info fields indicates section table entries */
2120 case 12301230:
2121 if (pShdr->sh_info >= pModElf->Ehdr.e_shnum)
2122 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2123 "%s: Shdr #%d: sh_info (%d) is beyond the end of the section table (%d)!",
2124 pszLogName, iShdr, pShdr->sh_link, pModElf->Ehdr.e_shnum);
2125 break;
2126
2127 case SHT_NULL:
2128 break;
2129 case SHT_PROGBITS:
2130 case SHT_SYMTAB:
2131 case SHT_STRTAB:
2132 case SHT_RELA:
2133 case SHT_HASH:
2134 case SHT_DYNAMIC:
2135 case SHT_NOTE:
2136 case SHT_NOBITS:
2137 case SHT_REL:
2138 case SHT_SHLIB:
2139 case SHT_DYNSYM:
2140 /*
2141 * For these types sh_info doesn't have any special meaning, or anything which
2142 * we need/can validate now.
2143 */
2144 break;
2145
2146
2147 default:
2148 Log(("RTLdrELF: %s: Warning, unknown type %d!\n", pszLogName, pShdr->sh_type));
2149 break;
2150 }
2151
2152 if ( pShdr->sh_type != SHT_NOBITS
2153 && pShdr->sh_size)
2154 {
2155 uint64_t offEnd = pShdr->sh_offset + pShdr->sh_size;
2156 if ( offEnd > cbRawImage
2157 || offEnd < (uint64_t)pShdr->sh_offset)
2158 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2159 "%s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD " = %RX64) is beyond the end of the file (%RX64)!",
2160 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size, offEnd, cbRawImage);
2161 if (pShdr->sh_offset < sizeof(Elf_Ehdr))
2162 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2163 "%s: Shdr #%d: sh_offset (" FMT_ELF_OFF ") + sh_size (" FMT_ELF_XWORD ") is starting in the ELF header!",
2164 pszLogName, iShdr, pShdr->sh_offset, pShdr->sh_size);
2165 }
2166
2167 return VINF_SUCCESS;
2168}
2169
2170
2171/**
2172 * Process the section headers.
2173 *
2174 * @returns iprt status code.
2175 * @param pModElf Pointer to the module structure.
2176 * @param paShdrs The section headers.
2177 * @param cbRawImage The size of the raw image.
2178 * @param pszLogName The log name.
2179 * @param pErrInfo Where to return extended error info. Optional.
2180 */
2181static int RTLDRELF_NAME(ValidateAndProcessSectionHeaders)(PRTLDRMODELF pModElf, Elf_Shdr *paShdrs, uint64_t cbRawImage,
2182 const char *pszLogName, PRTERRINFO pErrInfo)
2183{
2184 Elf_Addr uNextAddr = 0;
2185 for (unsigned i = 0; i < pModElf->Ehdr.e_shnum; i++)
2186 {
2187 int rc = RTLDRELF_NAME(ValidateSectionHeader)(pModElf, i, cbRawImage, pszLogName, pErrInfo);
2188 if (RT_FAILURE(rc))
2189 return rc;
2190
2191 /*
2192 * We're looking for symbol tables.
2193 */
2194 if (paShdrs[i].sh_type == SHT_SYMTAB)
2195 {
2196 if (pModElf->Rel.iSymSh != ~0U)
2197 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_MULTIPLE_SYMTABS,
2198 "%s: Multiple symbol tabs! iSymSh=%d i=%d", pszLogName, pModElf->Rel.iSymSh, i);
2199 pModElf->Rel.iSymSh = i;
2200 pModElf->Rel.cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
2201 AssertBreakStmt(pModElf->Rel.cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), rc = VERR_IMAGE_TOO_BIG);
2202 pModElf->Rel.iStrSh = paShdrs[i].sh_link;
2203 pModElf->Rel.cbStr = (unsigned)paShdrs[pModElf->Rel.iStrSh].sh_size;
2204 AssertBreakStmt(pModElf->Rel.cbStr == paShdrs[pModElf->Rel.iStrSh].sh_size, rc = VERR_IMAGE_TOO_BIG);
2205 }
2206 else if (paShdrs[i].sh_type == SHT_DYNSYM)
2207 {
2208 if (pModElf->Dyn.iSymSh != ~0U)
2209 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_LDRELF_MULTIPLE_SYMTABS,
2210 "%s: Multiple dynamic symbol tabs! iSymSh=%d i=%d", pszLogName, pModElf->Dyn.iSymSh, i);
2211 if (pModElf->Ehdr.e_type != ET_DYN && pModElf->Ehdr.e_type != ET_EXEC)
2212 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2213 "%s: Unexpected SHT_DYNSYM (i=%d) for e_type=%d", pszLogName, i, pModElf->Ehdr.e_type);
2214 pModElf->Dyn.iSymSh = i;
2215 pModElf->Dyn.cSyms = (unsigned)(paShdrs[i].sh_size / sizeof(Elf_Sym));
2216 AssertBreakStmt(pModElf->Dyn.cSyms == paShdrs[i].sh_size / sizeof(Elf_Sym), rc = VERR_IMAGE_TOO_BIG);
2217 pModElf->Dyn.iStrSh = paShdrs[i].sh_link;
2218 pModElf->Dyn.cbStr = (unsigned)paShdrs[pModElf->Dyn.iStrSh].sh_size;
2219 AssertBreakStmt(pModElf->Dyn.cbStr == paShdrs[pModElf->Dyn.iStrSh].sh_size, rc = VERR_IMAGE_TOO_BIG);
2220 }
2221 /*
2222 * We're also look for the dynamic section.
2223 */
2224 else if (paShdrs[i].sh_type == SHT_DYNAMIC)
2225 {
2226 if (pModElf->iShDynamic != ~0U)
2227 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2228 "%s: Multiple dynamic sections! iShDynamic=%d i=%d",
2229 pszLogName, pModElf->iShDynamic, i);
2230 if (pModElf->Ehdr.e_type != ET_DYN && pModElf->Ehdr.e_type != ET_EXEC)
2231 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2232 "%s: Unexpected SHT_DYNAMIC (i=%d) for e_type=%d", pszLogName, i, pModElf->Ehdr.e_type);
2233 if (paShdrs[i].sh_entsize != sizeof(Elf_Dyn))
2234 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2235 "%s: SHT_DYNAMIC (i=%d) sh_entsize=" FMT_ELF_XWORD ", expected %#zx",
2236 pszLogName, i, paShdrs[i].sh_entsize, sizeof(Elf_Dyn));
2237 pModElf->iShDynamic = i;
2238 Elf_Xword const cDynamic = paShdrs[i].sh_size / sizeof(Elf_Dyn);
2239 if (cDynamic > _64K || cDynamic < 2)
2240 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2241 "%s: SHT_DYNAMIC (i=%d) sh_size=" FMT_ELF_XWORD " is out of range (2..64K)",
2242 pszLogName, i, paShdrs[i].sh_size);
2243 pModElf->cDynamic = (unsigned)cDynamic;
2244 }
2245
2246 /*
2247 * Special checks for the section string table.
2248 */
2249 if (i == pModElf->Ehdr.e_shstrndx)
2250 {
2251 if (paShdrs[i].sh_type != SHT_STRTAB)
2252 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2253 "%s: Section header string table is not a SHT_STRTAB: %#x",
2254 pszLogName, paShdrs[i].sh_type);
2255 if (paShdrs[i].sh_size == 0)
2256 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Section header string table is empty", pszLogName);
2257 }
2258
2259 /*
2260 * Kluge for the .data..percpu segment in 64-bit linux kernels.
2261 */
2262 if (paShdrs[i].sh_flags & SHF_ALLOC)
2263 {
2264 if ( paShdrs[i].sh_addr == 0
2265 && paShdrs[i].sh_addr < uNextAddr)
2266 {
2267 Elf_Addr uAddr = RT_ALIGN_T(uNextAddr, paShdrs[i].sh_addralign, Elf_Addr);
2268 Log(("RTLdrElf: Out of order section #%d; adjusting sh_addr from " FMT_ELF_ADDR " to " FMT_ELF_ADDR "\n",
2269 i, paShdrs[i].sh_addr, uAddr));
2270 paShdrs[i].sh_addr = uAddr;
2271 }
2272 uNextAddr = paShdrs[i].sh_addr + paShdrs[i].sh_size;
2273 }
2274 } /* for each section header */
2275
2276 return VINF_SUCCESS;
2277}
2278
2279
2280/**
2281 * Process the section headers.
2282 *
2283 * @returns iprt status code.
2284 * @param pModElf Pointer to the module structure.
2285 * @param paShdrs The section headers.
2286 * @param cbRawImage The size of the raw image.
2287 * @param pszLogName The log name.
2288 * @param pErrInfo Where to return extended error info. Optional.
2289 */
2290static int RTLDRELF_NAME(ValidateAndProcessDynamicInfo)(PRTLDRMODELF pModElf, uint64_t cbRawImage, uint32_t fFlags,
2291 const char *pszLogName, PRTERRINFO pErrInfo)
2292{
2293 /*
2294 * Check preconditions.
2295 */
2296 AssertReturn(pModElf->Ehdr.e_type == ET_DYN || pModElf->Ehdr.e_type == ET_EXEC, VERR_INTERNAL_ERROR_2);
2297 if (pModElf->Ehdr.e_phnum <= 1 || pModElf->Ehdr.e_phnum >= _32K)
2298 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2299 "%s: e_phnum=%u is out of bounds (2..32K)", pszLogName, pModElf->Ehdr.e_phnum);
2300 if (pModElf->iShDynamic == ~0U)
2301 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: no .dynamic section", pszLogName);
2302 AssertReturn(pModElf->cDynamic > 1 && pModElf->cDynamic <= _64K, VERR_INTERNAL_ERROR_3);
2303
2304 /* ASSUME that the sections are ordered by address. That simplifies
2305 validation code further down. */
2306 AssertReturn(pModElf->Ehdr.e_shnum >= 2, VERR_INTERNAL_ERROR_4);
2307 Elf_Shdr const *paShdrs = pModElf->paShdrs;
2308 Elf_Addr uPrevEnd = paShdrs[1].sh_addr + paShdrs[1].sh_size;
2309 for (unsigned i = 2; i < pModElf->Ehdr.e_shnum; i++)
2310 if (paShdrs[i].sh_flags & SHF_ALLOC)
2311 {
2312 if (uPrevEnd > paShdrs[i].sh_addr)
2313 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2314 "%s: section %u is out of order: uPrevEnd=" FMT_ELF_ADDR " sh_addr=" FMT_ELF_ADDR,
2315 pszLogName, i, uPrevEnd, paShdrs[i].sh_addr);
2316 uPrevEnd = paShdrs[i].sh_addr + paShdrs[i].sh_size;
2317 }
2318
2319 /* Must have string and symbol tables. */
2320 if (pModElf->Dyn.iStrSh == ~0U)
2321 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: No dynamic string table section", pszLogName);
2322 if (pModElf->Dyn.iSymSh == ~0U)
2323 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: No dynamic symbol table section", pszLogName);
2324
2325 /*
2326 * Load the program headers.
2327 */
2328 size_t const cbPhdrs = sizeof(pModElf->paPhdrs[0]) * pModElf->Ehdr.e_phnum;
2329 Elf_Phdr *paPhdrs = (Elf_Phdr *)RTMemAllocZ(cbPhdrs);
2330 pModElf->paPhdrs = paPhdrs;
2331 AssertReturn(paPhdrs, VERR_NO_MEMORY);
2332
2333 int rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, paPhdrs, cbPhdrs, pModElf->Ehdr.e_phoff);
2334 if (RT_FAILURE(rc))
2335 return RTERRINFO_LOG_SET_F(pErrInfo, rc, "%s: pfnRead(,,%#zx, " FMT_ELF_OFF ") -> %Rrc",
2336 pszLogName, cbPhdrs, pModElf->Ehdr.e_phoff, rc);
2337
2338 /*
2339 * Validate them.
2340 */
2341 unsigned cbPage = _4K; /** @todo generalize architecture specific stuff using its own code template header. */
2342 switch (pModElf->Core.enmArch)
2343 {
2344 case RTLDRARCH_AMD64:
2345 case RTLDRARCH_X86_32:
2346 break;
2347 default:
2348 AssertFailedBreak(/** @todo page size for got.plt hacks */);
2349 }
2350 unsigned iLoad = 0;
2351 unsigned iLoadShdr = 1; /* ASSUMES ordered (checked above). */
2352 unsigned cDynamic = 0;
2353 Elf_Addr cbImage = 0;
2354 Elf_Addr uLinkAddress = ~(Elf_Addr)0;
2355 for (unsigned i = 0; i < pModElf->Ehdr.e_phnum; i++)
2356 {
2357 const Elf_Phdr * const pPhdr = &paPhdrs[i];
2358 Log3(("RTLdrELF: Program Header #%d:\n"
2359 "RTLdrELF: p_type: " FMT_ELF_WORD " (%s)\n"
2360 "RTLdrELF: p_flags: " FMT_ELF_WORD "\n"
2361 "RTLdrELF: p_offset: " FMT_ELF_OFF "\n"
2362 "RTLdrELF: p_vaddr: " FMT_ELF_ADDR "\n"
2363 "RTLdrELF: p_paddr: " FMT_ELF_ADDR "\n"
2364 "RTLdrELF: p_filesz: " FMT_ELF_XWORD "\n"
2365 "RTLdrELF: p_memsz: " FMT_ELF_XWORD "\n"
2366 "RTLdrELF: p_align: " FMT_ELF_XWORD "\n",
2367 i,
2368 pPhdr->p_type, rtldrElfGetPhdrType(pPhdr->p_type), pPhdr->p_flags, pPhdr->p_offset,
2369 pPhdr->p_vaddr, pPhdr->p_paddr, pPhdr->p_filesz, pPhdr->p_memsz, pPhdr->p_align));
2370
2371 if (pPhdr->p_type == DT_NULL)
2372 continue;
2373
2374 if ( pPhdr->p_filesz != 0
2375 && ( pPhdr->p_offset >= cbRawImage
2376 || pPhdr->p_filesz > cbRawImage
2377 || pPhdr->p_offset + pPhdr->p_filesz > cbRawImage))
2378 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2379 "%s: Prog Hdr #%u: bogus p_offset=" FMT_ELF_OFF " & p_filesz=" FMT_ELF_XWORD " (file size %#RX64)",
2380 pszLogName, i, pPhdr->p_offset, pPhdr->p_filesz, cbRawImage);
2381
2382 if (pPhdr->p_flags & ~(Elf64_Word)(PF_X | PF_R | PF_W))
2383 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Prog Hdr #%u: bogus p_flags=" FMT_ELF_WORD,
2384 pszLogName, i, pPhdr->p_flags);
2385
2386 if (!RT_IS_POWER_OF_TWO(pPhdr->p_align))
2387 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Prog Hdr #%u: bogus p_align=" FMT_ELF_XWORD,
2388 pszLogName, i, pPhdr->p_align);
2389
2390 if ( pPhdr->p_align > 1
2391 && pPhdr->p_memsz > 0
2392 && pPhdr->p_filesz > 0
2393 && (pPhdr->p_offset & (pPhdr->p_align - 1)) != (pPhdr->p_vaddr & (pPhdr->p_align - 1)))
2394 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2395 "%s: Prog Hdr #%u: misaligned p_offset=" FMT_ELF_OFF " p_vaddr=" FMT_ELF_ADDR " p_align=" FMT_ELF_XWORD,
2396 pszLogName, i, pPhdr->p_offset, pPhdr->p_vaddr, pPhdr->p_align);
2397
2398 /* Do some type specfic checks: */
2399 switch (pPhdr->p_type)
2400 {
2401 case PT_LOAD:
2402 {
2403 if (pPhdr->p_memsz < pPhdr->p_filesz)
2404 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2405 "%s: Prog Hdr #%u/LOAD#%u: bogus p_memsz=" FMT_ELF_XWORD " or p_filesz=" FMT_ELF_XWORD,
2406 pszLogName, i, iLoad, pPhdr->p_memsz, pPhdr->p_filesz);
2407 cbImage = pPhdr->p_vaddr + pPhdr->p_memsz;
2408 if (iLoad == 0)
2409 uLinkAddress = pPhdr->p_vaddr;
2410
2411 /* Find the corresponding sections, checking their addresses and
2412 file offsets since the rest of the code is still section based
2413 rather than using program headers as it should... */
2414 Elf_Off off = pPhdr->p_offset;
2415 Elf_Addr uAddr = pPhdr->p_vaddr;
2416 Elf_Xword cbMem = pPhdr->p_memsz;
2417 Elf_Xword cbFile = pPhdr->p_filesz;
2418
2419 /* HACK to allow loading isolinux-debug.elf where program headers aren't
2420 sorted by virtual address. */
2421 if ( (fFlags & RTLDR_O_FOR_DEBUG)
2422 && uAddr != paShdrs[iLoadShdr].sh_addr)
2423 {
2424 for (unsigned iShdr = 1; iShdr < pModElf->Ehdr.e_shnum; iShdr++)
2425 if (uAddr == paShdrs[iShdr].sh_addr)
2426 {
2427 iLoadShdr = iShdr;
2428 break;
2429 }
2430 }
2431
2432 while (cbMem > 0)
2433 {
2434 if (iLoadShdr < pModElf->Ehdr.e_shnum)
2435 { /* likely */ }
2436 else if (iLoadShdr == pModElf->Ehdr.e_shnum)
2437 {
2438 /** @todo anything else to check here? */
2439 iLoadShdr++;
2440 break;
2441 }
2442 else
2443 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2444 "%s: Prog Hdr #%u/LOAD#%u: Out of sections at " FMT_ELF_ADDR " LB " FMT_ELF_XWORD,
2445 pszLogName, i, iLoad, uAddr, cbMem);
2446 if (!(paShdrs[iLoadShdr].sh_flags & SHF_ALLOC))
2447 {
2448 if ( paShdrs[iLoadShdr].sh_type != SHT_NOBITS
2449 && paShdrs[iLoadShdr].sh_size > 0
2450 && off < paShdrs[iLoadShdr].sh_offset + paShdrs[iLoadShdr].sh_size
2451 && paShdrs[iLoadShdr].sh_offset < off + cbMem)
2452 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2453 "%s: Prog Hdr #%u/LOAD#%u: Overlaps with !SHF_ALLOC section at " FMT_ELF_OFF " LB " FMT_ELF_XWORD,
2454 pszLogName, i, iLoad, paShdrs[iLoadShdr].sh_offset, paShdrs[iLoadShdr].sh_size);
2455 pModElf->paShdrExtras[iLoadShdr].idxPhdr = UINT16_MAX;
2456 iLoadShdr++;
2457 continue;
2458 }
2459
2460 if (uAddr != paShdrs[iLoadShdr].sh_addr)
2461 {
2462 /* Before the first section we expect headers to be loaded, so
2463 that the file is simply mapped from file offset zero. */
2464 if ( iLoadShdr == 1
2465 && iLoad == 0
2466 && paShdrs[1].sh_addr == paShdrs[1].sh_offset
2467 && cbFile >= paShdrs[1].sh_offset
2468 && cbMem >= paShdrs[1].sh_offset)
2469 {
2470 /* Modify paShdrs[0] to describe the gap. ".elf.headers" */
2471 pModElf->iFirstSect = 0;
2472 pModElf->paShdrs[0].sh_name = 0;
2473 pModElf->paShdrs[0].sh_type = SHT_PROGBITS;
2474 pModElf->paShdrs[0].sh_flags = SHF_ALLOC
2475 | (pPhdr->p_flags & PF_W ? SHF_WRITE : 0)
2476 | (pPhdr->p_flags & PF_X ? SHF_EXECINSTR : 0);
2477 pModElf->paShdrs[0].sh_addr = uAddr;
2478 pModElf->paShdrs[0].sh_offset = off;
2479 pModElf->paShdrs[0].sh_size = paShdrs[1].sh_offset;
2480 pModElf->paShdrs[0].sh_link = 0;
2481 pModElf->paShdrs[0].sh_info = 0;
2482 pModElf->paShdrs[0].sh_addralign = pPhdr->p_align;
2483 pModElf->paShdrs[0].sh_entsize = 0;
2484 *(Elf_Shdr *)pModElf->paOrgShdrs = pModElf->paShdrs[0]; /* (necessary for segment enumeration) */
2485
2486 uAddr += paShdrs[1].sh_offset;
2487 cbMem -= paShdrs[1].sh_offset;
2488 cbFile -= paShdrs[1].sh_offset;
2489 off = paShdrs[1].sh_offset;
2490 }
2491 /* Alignment padding? Allow up to a page size. */
2492 else if ( paShdrs[iLoadShdr].sh_addr > uAddr
2493 && paShdrs[iLoadShdr].sh_addr - uAddr
2494 < RT_MAX(paShdrs[iLoadShdr].sh_addralign, cbPage /*got.plt hack*/))
2495 {
2496 Elf_Xword cbAlignPadding = paShdrs[iLoadShdr].sh_addr - uAddr;
2497 if (cbAlignPadding >= cbMem)
2498 break;
2499 cbMem -= cbAlignPadding;
2500 uAddr += cbAlignPadding;
2501 if (cbFile > cbAlignPadding)
2502 {
2503 off += cbAlignPadding;
2504 cbFile -= cbAlignPadding;
2505 }
2506 else
2507 {
2508 off += cbFile;
2509 cbFile = 0;
2510 }
2511 }
2512 }
2513
2514 if ( uAddr == paShdrs[iLoadShdr].sh_addr
2515 && cbMem >= paShdrs[iLoadShdr].sh_size
2516 && ( paShdrs[iLoadShdr].sh_type != SHT_NOBITS
2517 ? off == paShdrs[iLoadShdr].sh_offset
2518 && cbFile >= paShdrs[iLoadShdr].sh_size /* this might be too strict... */
2519 : cbFile == 0
2520 || cbMem > paShdrs[iLoadShdr].sh_size /* isolinux.elf: linker merge no-bits and progbits sections */) )
2521 {
2522 if ( paShdrs[iLoadShdr].sh_type != SHT_NOBITS
2523 || cbFile != 0)
2524 {
2525 off += paShdrs[iLoadShdr].sh_size;
2526 cbFile -= paShdrs[iLoadShdr].sh_size;
2527 }
2528 uAddr += paShdrs[iLoadShdr].sh_size;
2529 cbMem -= paShdrs[iLoadShdr].sh_size;
2530 }
2531 else
2532 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2533 "%s: Prog Hdr #%u/LOAD#%u: Mismatch at " FMT_ELF_ADDR " LB " FMT_ELF_XWORD " (file " FMT_ELF_OFF " LB " FMT_ELF_XWORD ") with section #%u " FMT_ELF_ADDR " LB " FMT_ELF_XWORD " (file " FMT_ELF_OFF " sh_type=" FMT_ELF_WORD ")",
2534 pszLogName, i, iLoad, uAddr, cbMem, off, cbFile,
2535 iLoadShdr, paShdrs[iLoadShdr].sh_addr, paShdrs[iLoadShdr].sh_size,
2536 paShdrs[iLoadShdr].sh_offset, paShdrs[iLoadShdr].sh_type);
2537
2538 pModElf->paShdrExtras[iLoadShdr].idxPhdr = iLoad;
2539 iLoadShdr++;
2540 } /* section loop */
2541
2542 iLoad++;
2543 break;
2544 }
2545
2546 case PT_DYNAMIC:
2547 {
2548 const Elf_Shdr *pShdr = &pModElf->paShdrs[pModElf->iShDynamic];
2549 if (pPhdr->p_offset != pShdr->sh_offset)
2550 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2551 "%s: Prog Hdr #%u/DYNAMIC: p_offset=" FMT_ELF_OFF " expected " FMT_ELF_OFF,
2552 pszLogName, i, pPhdr->p_offset, pShdr->sh_offset);
2553 if (RT_MAX(pPhdr->p_memsz, pPhdr->p_filesz) != pShdr->sh_size)
2554 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2555 "%s: Prog Hdr #%u/DYNAMIC: expected " FMT_ELF_XWORD " for RT_MAX(p_memsz=" FMT_ELF_XWORD ", p_filesz=" FMT_ELF_XWORD ")",
2556 pszLogName, i, pShdr->sh_size, pPhdr->p_memsz, pPhdr->p_filesz);
2557 cDynamic++;
2558 break;
2559 }
2560 }
2561 }
2562
2563 if (iLoad == 0)
2564 return RTERRINFO_LOG_SET_F(pErrInfo, rc, "%s: No PT_LOAD program headers", pszLogName);
2565 if (cDynamic != 1)
2566 return RTERRINFO_LOG_SET_F(pErrInfo, rc, "%s: No program header for the DYNAMIC section", pszLogName);
2567
2568 cbImage -= uLinkAddress;
2569 pModElf->cbImage = (uint64_t)cbImage;
2570 pModElf->LinkAddress = uLinkAddress;
2571 AssertReturn(pModElf->cbImage == cbImage, VERR_INTERNAL_ERROR_5);
2572 Log3(("RTLdrELF: LinkAddress=" FMT_ELF_ADDR " cbImage=" FMT_ELF_ADDR " (from PT_LOAD)\n", uLinkAddress, cbImage));
2573
2574 for (; iLoadShdr < pModElf->Ehdr.e_shnum; iLoadShdr++)
2575 if ( !(paShdrs[iLoadShdr].sh_flags & SHF_ALLOC)
2576 || paShdrs[iLoadShdr].sh_size == 0)
2577 pModElf->paShdrExtras[iLoadShdr].idxPhdr = UINT16_MAX;
2578 else
2579 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2580 "%s: No PT_LOAD for section #%u " FMT_ELF_ADDR " LB " FMT_ELF_XWORD " (file " FMT_ELF_OFF " sh_type=" FMT_ELF_WORD ")",
2581 pszLogName, iLoadShdr, paShdrs[iLoadShdr].sh_addr, paShdrs[iLoadShdr].sh_size,
2582 paShdrs[iLoadShdr].sh_offset, paShdrs[iLoadShdr].sh_type);
2583
2584 /*
2585 * Load and validate the dynamic table. We have got / will get most of the
2586 * info we need from the section table, so we must make sure this matches up.
2587 */
2588 Log3(("RTLdrELF: Dynamic section - %u entries\n", pModElf->cDynamic));
2589 size_t const cbDynamic = pModElf->cDynamic * sizeof(pModElf->paDynamic[0]);
2590 Elf_Dyn * const paDynamic = (Elf_Dyn *)RTMemAlloc(cbDynamic);
2591 AssertReturn(paDynamic, VERR_NO_MEMORY);
2592 pModElf->paDynamic = paDynamic;
2593
2594 rc = pModElf->Core.pReader->pfnRead(pModElf->Core.pReader, paDynamic, cbDynamic, paShdrs[pModElf->iShDynamic].sh_offset);
2595 if (RT_FAILURE(rc))
2596 return RTERRINFO_LOG_SET_F(pErrInfo, rc, "%s: pfnRead(,,%#zx, " FMT_ELF_OFF ") -> %Rrc",
2597 pszLogName, cbDynamic, paShdrs[pModElf->iShDynamic].sh_offset, rc);
2598
2599 for (uint32_t i = 0; i < pModElf->cDynamic; i++)
2600 {
2601#define LOG_VALIDATE_PTR_RET(szName) do { \
2602 Log3(("RTLdrELF: DT[%u]: %16s " FMT_ELF_ADDR "\n", i, szName, paDynamic[i].d_un.d_ptr)); \
2603 if ((uint64_t)paDynamic[i].d_un.d_ptr - uLinkAddress < cbImage) { /* likely */ } \
2604 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" szName ": Invalid address " FMT_ELF_ADDR " (valid range: " FMT_ELF_ADDR " LB " FMT_ELF_ADDR ")", \
2605 pszLogName, i, paDynamic[i].d_un.d_ptr, uLinkAddress, cbImage); \
2606 } while (0)
2607#define LOG_VALIDATE_PTR_VAL_RET(szName, uExpected) do { \
2608 Log3(("RTLdrELF: DT[%u]: %16s " FMT_ELF_ADDR "\n", i, szName, (uint64_t)paDynamic[i].d_un.d_ptr)); \
2609 if (paDynamic[i].d_un.d_ptr == (Elf_Addr)(uExpected)) { /* likely */ } \
2610 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" szName ": " FMT_ELF_ADDR ", expected " FMT_ELF_ADDR, \
2611 pszLogName, i, paDynamic[i].d_un.d_ptr, (Elf_Addr)(uExpected)); \
2612 } while (0)
2613#define LOG_VALIDATE_STR_RET(szName) do { \
2614 Log3(("RTLdrELF: DT[%u]: %16s %#RX64\n", i, szName, (uint64_t)paDynamic[i].d_un.d_val)); \
2615 if ((uint64_t)paDynamic[i].d_un.d_val < pModElf->Dyn.cbStr) { /* likely */ } \
2616 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" szName ": Invalid string table offset %#RX64 (max %#x)", \
2617 pszLogName, i, (uint64_t)paDynamic[i].d_un.d_val, pModElf->Dyn.cbStr); \
2618 } while (0)
2619#define LOG_VALIDATE_VAL_RET(szName, uExpected) do { \
2620 Log3(("RTLdrELF: DT[%u]: %16s %#RX64\n", i, szName, (uint64_t)paDynamic[i].d_un.d_val)); \
2621 if ((uint64_t)paDynamic[i].d_un.d_val == (uint64_t)(uExpected)) { /* likely */ } \
2622 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" szName ": %#RX64, expected %#RX64", \
2623 pszLogName, i, (uint64_t)paDynamic[i].d_un.d_val, (uint64_t)(uExpected)); \
2624 } while (0)
2625#define SET_RELOC_TYPE_RET(a_szName, a_uType) do { \
2626 if (pModElf->DynInfo.uRelocType == 0 || pModElf->DynInfo.uRelocType == (a_uType)) \
2627 pModElf->DynInfo.uRelocType = (a_uType); \
2628 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" a_szName ": Mixing DT_RELA and DT_REL", pszLogName, i); \
2629 } while (0)
2630#define SET_INFO_FIELD_RET(a_szName, a_Field, a_Value, a_UnsetValue, a_szFmt) do { \
2631 if ((a_Field) == (a_UnsetValue) && (a_Value) != (a_UnsetValue)) \
2632 (a_Field) = (a_Value); /* likely */ \
2633 else if ((a_Field) != (a_UnsetValue)) \
2634 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" a_szName ": Multiple entries (first value " a_szFmt ", second " a_szFmt ")", pszLogName, i, (a_Field), (a_Value)); \
2635 else if ((a_Value) != (a_UnsetValue)) \
2636 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" a_szName ": Unexpected value " a_szFmt, pszLogName, i, (a_Value)); \
2637 } while (0)
2638#define FIND_MATCHING_SECTION_RET(a_szName, a_ExtraMatchExpr, a_idxShFieldToSet) do { \
2639 unsigned iSh; \
2640 for (iSh = 1; iSh < pModElf->Ehdr.e_shnum; iSh++) \
2641 if ( paShdrs[iSh].sh_addr == paDynamic[i].d_un.d_ptr \
2642 && (a_ExtraMatchExpr)) \
2643 { \
2644 (a_idxShFieldToSet) = iSh; \
2645 if (pModElf->paShdrExtras[iSh].idxDt != UINT16_MAX) \
2646 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, \
2647 "%s: DT[%u]/" a_szName ": section #%u (" FMT_ELF_ADDR ") already referenced by DT[%u]", \
2648 pszLogName, i, iSh, paShdrs[iSh].sh_addr, pModElf->paShdrExtras[iSh].idxDt); \
2649 pModElf->paShdrExtras[iSh].idxDt = i; \
2650 pModElf->paShdrExtras[iSh].uDtTag = (uint32_t)paDynamic[i].d_tag; \
2651 break; \
2652 } \
2653 if (iSh < pModElf->Ehdr.e_shnum) { /* likely */ } \
2654 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" a_szName ": No matching section for " FMT_ELF_ADDR, pszLogName, i, paDynamic[i].d_un.d_ptr); \
2655 } while (0)
2656#define ONLY_FOR_DEBUG_OR_VALIDATION_RET(a_szName) do { \
2657 if (fFlags & (RTLDR_O_FOR_DEBUG | RTLDR_O_FOR_VALIDATION)) { /* likely */ } \
2658 else return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/" a_szName ": Not supported (" FMT_ELF_ADDR ")", pszLogName, i, paDynamic[i].d_un.d_ptr); \
2659 } while (0)
2660#define LOG_NON_VALUE_ENTRY(a_szName) Log3(("RTLdrELF: DT[%u]: %16s (%#RX64)\n", i, a_szName, (uint64_t)paDynamic[i].d_un.d_val))
2661
2662 switch (paDynamic[i].d_tag)
2663 {
2664 case DT_NULL:
2665 LOG_NON_VALUE_ENTRY("DT_NULL");
2666 for (unsigned iNull = i + 1; iNull < pModElf->cDynamic; iNull++)
2667 if (paDynamic[i].d_tag == DT_NULL) /* Not technically a bug, but let's try being extremely strict for now */
2668 LOG_NON_VALUE_ENTRY("DT_NULL");
2669 else if (!(fFlags & (RTLDR_O_FOR_DEBUG | RTLDR_O_FOR_VALIDATION)))
2670 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2671 "%s: DT[%u]/DT_NULL: Dynamic section isn't zero padded (extra #%u of #%u)",
2672 pszLogName, i, iNull - i, pModElf->cDynamic - i);
2673 i = pModElf->cDynamic;
2674 break;
2675 case DT_NEEDED:
2676 LOG_VALIDATE_STR_RET("DT_NEEDED");
2677 break;
2678 case DT_PLTRELSZ:
2679 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_PLTRELSZ", (uint64_t)paDynamic[i].d_un.d_val));
2680 SET_INFO_FIELD_RET("DT_PLTRELSZ", pModElf->DynInfo.cbJmpRelocs, (Elf_Xword)paDynamic[i].d_un.d_val, 0, FMT_ELF_XWORD);
2681 break;
2682 case DT_PLTGOT:
2683 LOG_VALIDATE_PTR_RET("DT_PLTGOT");
2684 break;
2685 case DT_HASH:
2686 LOG_VALIDATE_PTR_RET("DT_HASH");
2687 break;
2688 case DT_STRTAB:
2689 LOG_VALIDATE_PTR_VAL_RET("DT_STRTAB", paShdrs[pModElf->Dyn.iStrSh].sh_addr);
2690 pModElf->paShdrExtras[pModElf->Dyn.iStrSh].idxDt = i;
2691 pModElf->paShdrExtras[pModElf->Dyn.iSymSh].uDtTag = DT_STRTAB;
2692 break;
2693 case DT_SYMTAB:
2694 LOG_VALIDATE_PTR_VAL_RET("DT_SYMTAB", paShdrs[pModElf->Dyn.iSymSh].sh_addr);
2695 pModElf->paShdrExtras[pModElf->Dyn.iSymSh].idxDt = i;
2696 pModElf->paShdrExtras[pModElf->Dyn.iSymSh].uDtTag = DT_SYMTAB;
2697 break;
2698 case DT_RELA:
2699 LOG_VALIDATE_PTR_RET("DT_RELA");
2700 SET_RELOC_TYPE_RET("DT_RELA", DT_RELA);
2701 SET_INFO_FIELD_RET("DT_RELA", pModElf->DynInfo.uPtrRelocs, paDynamic[i].d_un.d_ptr, ~(Elf_Addr)0, FMT_ELF_ADDR);
2702 FIND_MATCHING_SECTION_RET("DT_RELA", paShdrs[iSh].sh_type == SHT_RELA, pModElf->DynInfo.idxShRelocs);
2703 break;
2704 case DT_RELASZ:
2705 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_RELASZ", (uint64_t)paDynamic[i].d_un.d_val));
2706 SET_RELOC_TYPE_RET("DT_RELASZ", DT_RELA);
2707 SET_INFO_FIELD_RET("DT_RELASZ", pModElf->DynInfo.cbRelocs, (Elf_Xword)paDynamic[i].d_un.d_val, 0, FMT_ELF_XWORD);
2708 break;
2709 case DT_RELAENT:
2710 LOG_VALIDATE_VAL_RET("DT_RELAENT", sizeof(Elf_Rela));
2711 SET_RELOC_TYPE_RET("DT_RELAENT", DT_RELA);
2712 SET_INFO_FIELD_RET("DT_RELAENT", pModElf->DynInfo.cbRelocEntry, (unsigned)sizeof(Elf_Rela), 0, "%u");
2713 break;
2714 case DT_STRSZ:
2715 LOG_VALIDATE_VAL_RET("DT_STRSZ", pModElf->Dyn.cbStr);
2716 break;
2717 case DT_SYMENT:
2718 LOG_VALIDATE_VAL_RET("DT_SYMENT", sizeof(Elf_Sym));
2719 break;
2720 case DT_INIT:
2721 LOG_VALIDATE_PTR_RET("DT_INIT");
2722 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_INIT");
2723 break;
2724 case DT_FINI:
2725 LOG_VALIDATE_PTR_RET("DT_FINI");
2726 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_FINI");
2727 break;
2728 case DT_SONAME:
2729 LOG_VALIDATE_STR_RET("DT_SONAME");
2730 break;
2731 case DT_RPATH:
2732 LOG_VALIDATE_STR_RET("DT_RPATH");
2733 break;
2734 case DT_SYMBOLIC:
2735 LOG_NON_VALUE_ENTRY("DT_SYMBOLIC");
2736 break;
2737 case DT_REL:
2738 LOG_VALIDATE_PTR_RET("DT_REL");
2739 SET_RELOC_TYPE_RET("DT_REL", DT_REL);
2740 SET_INFO_FIELD_RET("DT_REL", pModElf->DynInfo.uPtrRelocs, paDynamic[i].d_un.d_ptr, ~(Elf_Addr)0, FMT_ELF_ADDR);
2741 FIND_MATCHING_SECTION_RET("DT_REL", paShdrs[iSh].sh_type == SHT_REL, pModElf->DynInfo.idxShRelocs);
2742 break;
2743 case DT_RELSZ:
2744 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_RELSZ", (uint64_t)paDynamic[i].d_un.d_val));
2745 SET_RELOC_TYPE_RET("DT_RELSZ", DT_REL);
2746 SET_INFO_FIELD_RET("DT_RELSZ", pModElf->DynInfo.cbRelocs, (Elf_Xword)paDynamic[i].d_un.d_val, 0, FMT_ELF_XWORD);
2747 break;
2748 case DT_RELENT:
2749 LOG_VALIDATE_VAL_RET("DT_RELENT", sizeof(Elf_Rel));
2750 SET_RELOC_TYPE_RET("DT_RELENT", DT_REL);
2751 SET_INFO_FIELD_RET("DT_RELENT", pModElf->DynInfo.cbRelocEntry, (unsigned)sizeof(Elf_Rel), 0, "%u");
2752 break;
2753 case DT_PLTREL:
2754 if (paDynamic[i].d_un.d_val != DT_RELA && paDynamic[i].d_un.d_val != DT_REL)
2755 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT[%u]/DT_PLTREL: Invalid value %#RX64",
2756 pszLogName, i, (uint64_t)paDynamic[i].d_un.d_val);
2757 Log3(("RTLdrELF: DT[%u]: %16s DT_REL%s\n", i, "DT_PLTREL", paDynamic[i].d_un.d_val == DT_RELA ? "A" : ""));
2758 SET_INFO_FIELD_RET("DT_PLTREL", pModElf->DynInfo.uJmpRelocType, (unsigned)paDynamic[i].d_un.d_val, 0, "%u");
2759 break;
2760 case DT_DEBUG:
2761 /*
2762 * DT_DEBUG is filled in by the dynamic linker to point a debugger to the head of the link map,
2763 * it can point anywhere in userspace. For binaries not being executed it will be 0,
2764 * so there is nothing we can validate here (and it is not required as we don't use
2765 * this dynamic section). See https://ypl.coffee/dl-resolve-full-relro/ for more information.
2766 */
2767 break;
2768 case DT_TEXTREL:
2769 LOG_NON_VALUE_ENTRY("DT_TEXTREL");
2770 break;
2771 case DT_JMPREL:
2772 LOG_VALIDATE_PTR_RET("DT_JMPREL");
2773 SET_INFO_FIELD_RET("DT_JMPREL", pModElf->DynInfo.uPtrJmpRelocs, paDynamic[i].d_un.d_ptr, ~(Elf_Addr)0, FMT_ELF_ADDR);
2774 FIND_MATCHING_SECTION_RET("DT_JMPREL", 1, pModElf->DynInfo.idxShJmpRelocs);
2775 break;
2776 case DT_BIND_NOW:
2777 LOG_NON_VALUE_ENTRY("DT_BIND_NOW");
2778 break;
2779 case DT_INIT_ARRAY:
2780 LOG_VALIDATE_PTR_RET("DT_INIT_ARRAY");
2781 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_INIT_ARRAY");
2782 break;
2783 case DT_FINI_ARRAY:
2784 LOG_VALIDATE_PTR_RET("DT_FINI_ARRAY");
2785 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_FINI_ARRAY");
2786 break;
2787 case DT_INIT_ARRAYSZ:
2788 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_INIT_ARRAYSZ", (uint64_t)paDynamic[i].d_un.d_val));
2789 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_INIT_ARRAYSZ");
2790 break;
2791 case DT_FINI_ARRAYSZ:
2792 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_FINI_ARRAYSZ", (uint64_t)paDynamic[i].d_un.d_val));
2793 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_FINI_ARRAYSZ");
2794 break;
2795 case DT_RUNPATH:
2796 LOG_VALIDATE_STR_RET("DT_RUNPATH");
2797 break;
2798 case DT_FLAGS:
2799 Log3(("RTLdrELF: DT[%u]: %16s %#RX64\n", i, "DT_FLAGS", (uint64_t)paDynamic[i].d_un.d_val));
2800 break;
2801 case DT_PREINIT_ARRAY:
2802 LOG_VALIDATE_PTR_RET("DT_PREINIT_ARRAY");
2803 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_PREINIT_ARRAY");
2804 break;
2805 case DT_PREINIT_ARRAYSZ:
2806 Log3(("RTLdrELF: DT[%u]: %16s %#RX64 bytes\n", i, "DT_PREINIT_ARRAYSZ", (uint64_t)paDynamic[i].d_un.d_val));
2807 ONLY_FOR_DEBUG_OR_VALIDATION_RET("DT_PREINIT_ARRAYSZ");
2808 break;
2809 default:
2810 if ( paDynamic[i].d_tag < DT_ENCODING
2811 || paDynamic[i].d_tag >= DT_LOOS
2812 || (paDynamic[i].d_tag & 1))
2813 Log3(("RTLdrELF: DT[%u]: %#010RX64 %#RX64%s\n", i, (uint64_t)paDynamic[i].d_tag,
2814 (uint64_t)paDynamic[i].d_un.d_val, paDynamic[i].d_un.d_val >= DT_ENCODING ? " (val)" : ""));
2815 else
2816 {
2817 Log3(("RTLdrELF: DT[%u]: %#010RX64 " FMT_ELF_ADDR " (addr)\n",
2818 i, (uint64_t)paDynamic[i].d_tag, paDynamic[i].d_un.d_ptr));
2819 if ((uint64_t)paDynamic[i].d_un.d_ptr - uLinkAddress >= cbImage)
2820 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2821 "%s: DT[%u]/%#RX64: Invalid address " FMT_ELF_ADDR " (valid range: " FMT_ELF_ADDR " LB " FMT_ELF_ADDR ")",
2822 pszLogName, i, (uint64_t)paDynamic[i].d_tag,
2823 paDynamic[i].d_un.d_ptr, uLinkAddress, cbImage);
2824 }
2825 break;
2826 }
2827#undef LOG_VALIDATE_VAL_RET
2828#undef LOG_VALIDATE_STR_RET
2829#undef LOG_VALIDATE_PTR_VAL_RET
2830#undef LOG_VALIDATE_PTR_RET
2831#undef SET_RELOC_TYPE_RET
2832#undef SET_INFO_FIELD_RET
2833#undef FIND_MATCHING_SECTION_RET
2834#undef ONLY_FOR_DEBUG_OR_VALIDATION_RET
2835 }
2836
2837 /*
2838 * Validate the relocation information we've gathered.
2839 */
2840 Elf_Word uShTypeArch = SHT_RELA; /** @todo generalize architecture specific stuff using its own code template header. */
2841 switch (pModElf->Core.enmArch)
2842 {
2843 case RTLDRARCH_AMD64:
2844 break;
2845 case RTLDRARCH_X86_32:
2846 uShTypeArch = SHT_REL;
2847 break;
2848 default:
2849 AssertFailedBreak(/** @todo page size for got.plt hacks */);
2850
2851 }
2852
2853 if (pModElf->DynInfo.uRelocType != 0)
2854 {
2855 const char * const pszModifier = pModElf->DynInfo.uRelocType == DT_RELA ? "A" : "";
2856 if (pModElf->DynInfo.uPtrRelocs == ~(Elf_Addr)0)
2857 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_REL%s", pszLogName, pszModifier);
2858 if (pModElf->DynInfo.cbRelocs == 0)
2859 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_REL%sSZ", pszLogName, pszModifier);
2860 if (pModElf->DynInfo.cbRelocEntry == 0)
2861 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_REL%sENT", pszLogName, pszModifier);
2862 Elf_Shdr const *pShdrRelocs = &paShdrs[pModElf->DynInfo.idxShRelocs];
2863 Elf_Word const uShType = pModElf->DynInfo.uJmpRelocType == DT_RELA ? SHT_RELA : SHT_REL;
2864 if (pShdrRelocs->sh_type != uShType)
2865 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_REL%s* does not match section type: %u vs %u",
2866 pszLogName, pszModifier, pShdrRelocs->sh_type, uShType);
2867 if (pShdrRelocs->sh_size != pModElf->DynInfo.cbRelocs)
2868 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_REL%sSZ does not match section size: %u vs %u",
2869 pszLogName, pszModifier, pShdrRelocs->sh_size, pModElf->DynInfo.cbRelocs);
2870 if (uShType != uShTypeArch)
2871 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_REL%s* does not match architecture: %u, arch wants %u",
2872 pszLogName, pszModifier, uShType, uShTypeArch);
2873 }
2874
2875 if ( pModElf->DynInfo.uPtrJmpRelocs != ~(Elf_Addr)0
2876 || pModElf->DynInfo.cbJmpRelocs != 0
2877 || pModElf->DynInfo.uJmpRelocType != 0)
2878 {
2879 if (pModElf->DynInfo.uPtrJmpRelocs == ~(Elf_Addr)0)
2880 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_JMPREL", pszLogName);
2881 if (pModElf->DynInfo.cbJmpRelocs == 0)
2882 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_PLTRELSZ", pszLogName);
2883 if (pModElf->DynInfo.uJmpRelocType == 0)
2884 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: Missing DT_PLTREL", pszLogName);
2885 Elf_Shdr const *pShdrRelocs = &paShdrs[pModElf->DynInfo.idxShJmpRelocs];
2886 Elf_Word const uShType = pModElf->DynInfo.uJmpRelocType == DT_RELA ? SHT_RELA : SHT_REL;
2887 if (pShdrRelocs->sh_type != uShType)
2888 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_PLTREL does not match section type: %u vs %u",
2889 pszLogName, pShdrRelocs->sh_type, uShType);
2890 if (pShdrRelocs->sh_size != pModElf->DynInfo.cbJmpRelocs)
2891 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_PLTRELSZ does not match section size: %u vs %u",
2892 pszLogName, pShdrRelocs->sh_size, pModElf->DynInfo.cbJmpRelocs);
2893 if (uShType != uShTypeArch)
2894 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT, "%s: DT_PLTREL does not match architecture: %u, arch wants %u",
2895 pszLogName, uShType, uShTypeArch);
2896 }
2897
2898 /*
2899 * Check that there aren't any other relocations hiding in the section table.
2900 */
2901 for (uint32_t i = 1; i < pModElf->Ehdr.e_shnum; i++)
2902 if ( (paShdrs[i].sh_type == SHT_REL || paShdrs[i].sh_type == SHT_RELA)
2903 && pModElf->paShdrExtras[i].uDtTag != DT_REL
2904 && pModElf->paShdrExtras[i].uDtTag != DT_RELA
2905 && pModElf->paShdrExtras[i].uDtTag != DT_JMPREL)
2906 {
2907 char szSecHdrNm[80];
2908 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_BAD_EXE_FORMAT,
2909 "%s: section header #%u (%s type=" FMT_ELF_WORD " size=" FMT_ELF_XWORD ") contains relocations not referenced by the dynamic section",
2910 pszLogName, i,
2911 RTLDRELF_NAME(GetSHdrName)(pModElf, paShdrs[i].sh_name, szSecHdrNm, sizeof(szSecHdrNm)),
2912 paShdrs[i].sh_type, paShdrs[i].sh_size);
2913 }
2914
2915 return VINF_SUCCESS;
2916}
2917
2918
2919
2920/**
2921 * Opens an ELF image, fixed bitness.
2922 *
2923 * @returns iprt status code.
2924 * @param pReader The loader reader instance which will provide the raw image bits.
2925 * @param fFlags Reserved, MBZ.
2926 * @param enmArch Architecture specifier.
2927 * @param phLdrMod Where to store the handle.
2928 * @param pErrInfo Where to return extended error info. Optional.
2929 */
2930static int RTLDRELF_NAME(Open)(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
2931{
2932 const char *pszLogName = pReader->pfnLogName(pReader);
2933 uint64_t cbRawImage = pReader->pfnSize(pReader);
2934 RT_NOREF_PV(fFlags);
2935
2936 /*
2937 * Create the loader module instance.
2938 */
2939 PRTLDRMODELF pModElf = (PRTLDRMODELF)RTMemAllocZ(sizeof(*pModElf));
2940 if (!pModElf)
2941 return VERR_NO_MEMORY;
2942
2943 pModElf->Core.u32Magic = RTLDRMOD_MAGIC;
2944 pModElf->Core.eState = LDR_STATE_INVALID;
2945 pModElf->Core.pReader = pReader;
2946 pModElf->Core.enmFormat = RTLDRFMT_ELF;
2947 pModElf->Core.enmType = RTLDRTYPE_OBJECT;
2948 pModElf->Core.enmEndian = RTLDRENDIAN_LITTLE;
2949#if ELF_MODE == 32
2950 pModElf->Core.enmArch = RTLDRARCH_X86_32;
2951#else
2952 pModElf->Core.enmArch = RTLDRARCH_AMD64;
2953#endif
2954 //pModElf->pvBits = NULL;
2955 //pModElf->Ehdr = {0};
2956 //pModElf->paShdrs = NULL;
2957 //pModElf->Rel.paSyms = NULL;
2958 pModElf->Rel.iSymSh = ~0U;
2959 //pModElf->Rel.cSyms = 0;
2960 pModElf->Rel.iStrSh = ~0U;
2961 //pModElf->Rel.cbStr = 0;
2962 //pModElf->Rel.pStr = NULL;
2963 //pModElf->Dyn.paSyms = NULL;
2964 pModElf->Dyn.iSymSh = ~0U;
2965 //pModElf->Dyn.cSyms = 0;
2966 pModElf->Dyn.iStrSh = ~0U;
2967 //pModElf->Dyn.cbStr = 0;
2968 //pModElf->Dyn.pStr = NULL;
2969 pModElf->iFirstSect = 1;
2970 //pModElf->fShdrInOrder = false;
2971 //pModElf->cbImage = 0;
2972 pModElf->LinkAddress = ~(Elf_Addr)0;
2973 //pModElf->cbShStr = 0;
2974 //pModElf->pShStr = NULL;
2975 //pModElf->iShEhFrame = 0;
2976 //pModElf->iShEhFrameHdr= 0;
2977 pModElf->iShDynamic = ~0U;
2978 //pModElf->cDynamic = 0;
2979 //pModElf->paDynamic = NULL;
2980 //pModElf->paPhdrs = NULL;
2981 pModElf->DynInfo.uPtrRelocs = ~(Elf_Addr)0;
2982 //pModElf->DynInfo.cbRelocs = 0;
2983 //pModElf->DynInfo.cbRelocEntry = 0;
2984 //pModElf->DynInfo.uRelocType = 0;
2985 //pModElf->DynInfo.idxShRelocs = 0;
2986 pModElf->DynInfo.uPtrJmpRelocs = ~(Elf_Addr)0;
2987 //pModElf->DynInfo.cbJmpRelocs = 0;
2988 //pModElf->DynInfo.uJmpRelocType = 0;
2989 //pModElf->DynInfo.idxShJmpRelocs = 0;
2990
2991 /*
2992 * Read and validate the ELF header and match up the CPU architecture.
2993 */
2994 int rc = pReader->pfnRead(pReader, &pModElf->Ehdr, sizeof(pModElf->Ehdr), 0);
2995 if (RT_SUCCESS(rc))
2996 {
2997 RTLDRARCH enmArchImage = RTLDRARCH_INVALID; /* shut up gcc */
2998 rc = RTLDRELF_NAME(ValidateElfHeader)(&pModElf->Ehdr, cbRawImage, pszLogName, &enmArchImage, pErrInfo);
2999 if (RT_SUCCESS(rc))
3000 {
3001 if ( enmArch != RTLDRARCH_WHATEVER
3002 && enmArch != enmArchImage)
3003 rc = VERR_LDR_ARCH_MISMATCH;
3004 }
3005 }
3006 if (RT_SUCCESS(rc))
3007 {
3008 /*
3009 * Read the section headers, keeping a prestine copy for the module
3010 * introspection methods.
3011 */
3012 size_t const cbShdrs = pModElf->Ehdr.e_shnum * sizeof(Elf_Shdr);
3013 Elf_Shdr *paShdrs = (Elf_Shdr *)RTMemAlloc(cbShdrs * 2 + sizeof(RTLDRMODELFSHX) * pModElf->Ehdr.e_shnum);
3014 if (paShdrs)
3015 {
3016 pModElf->paShdrs = paShdrs;
3017 rc = pReader->pfnRead(pReader, paShdrs, cbShdrs, pModElf->Ehdr.e_shoff);
3018 if (RT_SUCCESS(rc))
3019 {
3020 memcpy(&paShdrs[pModElf->Ehdr.e_shnum], paShdrs, cbShdrs);
3021 pModElf->paOrgShdrs = &paShdrs[pModElf->Ehdr.e_shnum];
3022
3023 pModElf->paShdrExtras = (PRTLDRMODELFSHX)&pModElf->paOrgShdrs[pModElf->Ehdr.e_shnum];
3024 memset(pModElf->paShdrExtras, 0xff, sizeof(RTLDRMODELFSHX) * pModElf->Ehdr.e_shnum);
3025
3026 pModElf->cbShStr = paShdrs[pModElf->Ehdr.e_shstrndx].sh_size;
3027
3028 /*
3029 * Validate the section headers and find relevant sections.
3030 */
3031 rc = RTLDRELF_NAME(ValidateAndProcessSectionHeaders)(pModElf, paShdrs, cbRawImage, pszLogName, pErrInfo);
3032
3033 /*
3034 * Read validate and process program headers if ET_DYN or ET_EXEC.
3035 */
3036 if (RT_SUCCESS(rc) && (pModElf->Ehdr.e_type == ET_DYN || pModElf->Ehdr.e_type == ET_EXEC))
3037 rc = RTLDRELF_NAME(ValidateAndProcessDynamicInfo)(pModElf, cbRawImage, fFlags, pszLogName, pErrInfo);
3038
3039 /*
3040 * Massage the section headers.
3041 */
3042 if (RT_SUCCESS(rc))
3043 {
3044 if (pModElf->Ehdr.e_type == ET_REL)
3045 {
3046 /* Do allocations and figure the image size: */
3047 pModElf->LinkAddress = 0;
3048 for (unsigned i = 1; i < pModElf->Ehdr.e_shnum; i++)
3049 if (paShdrs[i].sh_flags & SHF_ALLOC)
3050 {
3051 paShdrs[i].sh_addr = paShdrs[i].sh_addralign
3052 ? RT_ALIGN_T(pModElf->cbImage, paShdrs[i].sh_addralign, Elf_Addr)
3053 : (Elf_Addr)pModElf->cbImage;
3054 Elf_Addr EndAddr = paShdrs[i].sh_addr + paShdrs[i].sh_size;
3055 if (pModElf->cbImage < EndAddr)
3056 {
3057 pModElf->cbImage = (size_t)EndAddr;
3058 AssertMsgBreakStmt(pModElf->cbImage == EndAddr, (FMT_ELF_ADDR "\n", EndAddr), rc = VERR_IMAGE_TOO_BIG);
3059 }
3060 Log2(("RTLdrElf: %s: Assigned " FMT_ELF_ADDR " to section #%d\n", pszLogName, paShdrs[i].sh_addr, i));
3061 }
3062 }
3063 else
3064 {
3065 /* Convert sh_addr to RVA: */
3066 Assert(pModElf->LinkAddress != ~(Elf_Addr)0);
3067 for (unsigned i = 0 /*!*/; i < pModElf->Ehdr.e_shnum; i++)
3068 if (paShdrs[i].sh_flags & SHF_ALLOC)
3069 paShdrs[i].sh_addr -= pModElf->LinkAddress;
3070 }
3071 }
3072
3073 /*
3074 * Check if the sections are in order by address, as that will simplify
3075 * enumeration and address translation.
3076 */
3077 pModElf->fShdrInOrder = true;
3078 Elf_Addr uEndAddr = 0;
3079 for (unsigned i = pModElf->iFirstSect; i < pModElf->Ehdr.e_shnum; i++)
3080 if (paShdrs[i].sh_flags & SHF_ALLOC)
3081 {
3082 if (uEndAddr <= paShdrs[i].sh_addr)
3083 uEndAddr = paShdrs[i].sh_addr + paShdrs[i].sh_size;
3084 else
3085 {
3086 pModElf->fShdrInOrder = false;
3087 break;
3088 }
3089 }
3090
3091 Log2(("RTLdrElf: iSymSh=%u cSyms=%u iStrSh=%u cbStr=%u rc=%Rrc cbImage=%#zx LinkAddress=" FMT_ELF_ADDR " fShdrInOrder=%RTbool\n",
3092 pModElf->Rel.iSymSh, pModElf->Rel.cSyms, pModElf->Rel.iStrSh, pModElf->Rel.cbStr, rc,
3093 pModElf->cbImage, pModElf->LinkAddress, pModElf->fShdrInOrder));
3094 if (RT_SUCCESS(rc))
3095 {
3096 pModElf->Core.pOps = &RTLDRELF_MID(s_rtldrElf,Ops);
3097 pModElf->Core.eState = LDR_STATE_OPENED;
3098 *phLdrMod = &pModElf->Core;
3099
3100 LogFlow(("%s: %s: returns VINF_SUCCESS *phLdrMod=%p\n", __FUNCTION__, pszLogName, *phLdrMod));
3101 return VINF_SUCCESS;
3102 }
3103 }
3104
3105 RTMemFree(paShdrs);
3106 }
3107 else
3108 rc = VERR_NO_MEMORY;
3109 }
3110
3111 RTMemFree(pModElf);
3112 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
3113 return rc;
3114}
3115
3116
3117
3118
3119/*******************************************************************************
3120* Cleanup Constants And Macros *
3121*******************************************************************************/
3122#undef RTLDRELF_NAME
3123#undef RTLDRELF_SUFF
3124#undef RTLDRELF_MID
3125
3126#undef FMT_ELF_ADDR
3127#undef FMT_ELF_ADDR7
3128#undef FMT_ELF_HALF
3129#undef FMT_ELF_SHALF
3130#undef FMT_ELF_OFF
3131#undef FMT_ELF_SIZE
3132#undef FMT_ELF_SWORD
3133#undef FMT_ELF_WORD
3134#undef FMT_ELF_XWORD
3135#undef FMT_ELF_SXWORD
3136
3137#undef Elf_Ehdr
3138#undef Elf_Phdr
3139#undef Elf_Shdr
3140#undef Elf_Sym
3141#undef Elf_Rel
3142#undef Elf_Rela
3143#undef Elf_Reloc
3144#undef Elf_Nhdr
3145#undef Elf_Dyn
3146
3147#undef Elf_Addr
3148#undef Elf_Half
3149#undef Elf_Off
3150#undef Elf_Size
3151#undef Elf_Sword
3152#undef Elf_Word
3153#undef Elf_Xword
3154#undef Elf_Sxword
3155
3156#undef RTLDRMODELF
3157#undef PRTLDRMODELF
3158
3159#undef ELF_R_SYM
3160#undef ELF_R_TYPE
3161#undef ELF_R_INFO
3162
3163#undef ELF_ST_BIND
3164
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