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