2edab86b704ff50cc37d94ddfcf9e8d6625e4a2c
[reactos.git] / dll / win32 / dbghelp / elf_module.c
1 /*
2 * File elf.c - processing of ELF files
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2007 Eric Pouech
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "dbghelp_private.h"
23
24 #if defined(__svr4__) || defined(__sun)
25 #define __ELF__ 1
26 /* large files are not supported by libelf */
27 #undef _FILE_OFFSET_BITS
28 #define _FILE_OFFSET_BITS 32
29 #endif
30
31 #include <stdlib.h>
32 #include <fcntl.h>
33
34 #include <wine/library.h>
35
36 #ifdef __ELF__
37
38 #define ELF_INFO_DEBUG_HEADER 0x0001
39 #define ELF_INFO_MODULE 0x0002
40 #define ELF_INFO_NAME 0x0004
41
42 #ifndef NT_GNU_BUILD_ID
43 #define NT_GNU_BUILD_ID 3
44 #endif
45
46 #ifndef HAVE_STRUCT_R_DEBUG
47 struct r_debug
48 {
49 int r_version;
50 struct link_map *r_map;
51 ElfW(Addr) r_brk;
52 enum
53 {
54 RT_CONSISTENT,
55 RT_ADD,
56 RT_DELETE
57 } r_state;
58 ElfW(Addr) r_ldbase;
59 };
60 #endif /* HAVE_STRUCT_R_DEBUG */
61
62 #ifndef HAVE_STRUCT_LINK_MAP
63 struct link_map
64 {
65 ElfW(Addr) l_addr;
66 char *l_name;
67 ElfW(Dyn) *l_ld;
68 struct link_map *l_next, *l_prev;
69 };
70 #endif /* HAVE_STRUCT_LINK_MAP */
71
72 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
73
74 struct elf_info
75 {
76 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
77 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
78 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
79 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
80 };
81
82 struct symtab_elt
83 {
84 struct hash_table_elt ht_elt;
85 const Elf_Sym* symp;
86 struct symt_compiland* compiland;
87 unsigned used;
88 };
89
90 struct elf_thunk_area
91 {
92 const char* symname;
93 THUNK_ORDINAL ordinal;
94 unsigned long rva_start;
95 unsigned long rva_end;
96 };
97
98 struct elf_module_info
99 {
100 unsigned long elf_addr;
101 unsigned short elf_mark : 1,
102 elf_loader : 1;
103 struct image_file_map file_map;
104 };
105
106 /******************************************************************
107 * elf_map_section
108 *
109 * Maps a single section into memory from an ELF file
110 */
111 const char* elf_map_section(struct image_section_map* ism)
112 {
113 struct elf_file_map* fmap = &ism->fmap->u.elf;
114 size_t ofst, size, pgsz = sysconf( _SC_PAGESIZE );
115
116 assert(ism->fmap->modtype == DMT_ELF);
117 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
118 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
119 return IMAGE_NO_MAP;
120
121 if (fmap->target_copy)
122 {
123 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
124 }
125 /* align required information on page size (we assume pagesize is a power of 2) */
126 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
127 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
128 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
129 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
130 fmap->fd, ofst);
131 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
132 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
133 }
134
135 /******************************************************************
136 * elf_find_section
137 *
138 * Finds a section by name (and type) into memory from an ELF file
139 * or its alternate if any
140 */
141 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
142 unsigned sht, struct image_section_map* ism)
143 {
144 struct elf_file_map* fmap;
145 unsigned i;
146
147 while (_fmap)
148 {
149 fmap = &_fmap->u.elf;
150 if (fmap->shstrtab == IMAGE_NO_MAP)
151 {
152 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
153 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
154 }
155 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
156 {
157 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
158 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
159 {
160 ism->fmap = _fmap;
161 ism->sidx = i;
162 return TRUE;
163 }
164 }
165 _fmap = fmap->alternate;
166 }
167 ism->fmap = NULL;
168 ism->sidx = -1;
169 return FALSE;
170 }
171
172 /******************************************************************
173 * elf_unmap_section
174 *
175 * Unmaps a single section from memory
176 */
177 void elf_unmap_section(struct image_section_map* ism)
178 {
179 struct elf_file_map* fmap = &ism->fmap->u.elf;
180
181 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
182 fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
183 {
184 size_t pgsz = sysconf( _SC_PAGESIZE );
185 size_t ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
186 size_t size = ((fmap->sect[ism->sidx].shdr.sh_offset +
187 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
188 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
189 WARN("Couldn't unmap the section\n");
190 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
191 }
192 }
193
194 static void elf_end_find(struct image_file_map* fmap)
195 {
196 struct image_section_map ism;
197
198 while (fmap)
199 {
200 ism.fmap = fmap;
201 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
202 elf_unmap_section(&ism);
203 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
204 fmap = fmap->u.elf.alternate;
205 }
206 }
207
208 /******************************************************************
209 * elf_get_map_rva
210 *
211 * Get the RVA of an ELF section
212 */
213 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
214 {
215 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
216 return 0;
217 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
218 }
219
220 /******************************************************************
221 * elf_get_map_size
222 *
223 * Get the size of an ELF section
224 */
225 unsigned elf_get_map_size(const struct image_section_map* ism)
226 {
227 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
228 return 0;
229 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
230 }
231
232 static inline void elf_reset_file_map(struct image_file_map* fmap)
233 {
234 fmap->u.elf.fd = -1;
235 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
236 fmap->u.elf.alternate = NULL;
237 fmap->u.elf.target_copy = NULL;
238 }
239
240 struct elf_map_file_data
241 {
242 enum {from_file, from_process} kind;
243 union
244 {
245 struct
246 {
247 const WCHAR* filename;
248 } file;
249 struct
250 {
251 HANDLE handle;
252 void* load_addr;
253 } process;
254 } u;
255 };
256
257 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
258 void* buf, size_t len, off_t off)
259 {
260 SIZE_T dw;
261
262 switch (emfd->kind)
263 {
264 case from_file:
265 return pread(fmap->u.elf.fd, buf, len, off) == len;
266 case from_process:
267 return ReadProcessMemory(emfd->u.process.handle,
268 (void*)((unsigned long)emfd->u.process.load_addr + (unsigned long)off),
269 buf, len, &dw) && dw == len;
270 default:
271 assert(0);
272 return FALSE;
273 }
274 }
275
276 /******************************************************************
277 * elf_map_file
278 *
279 * Maps an ELF file into memory (and checks it's a real ELF file)
280 */
281 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
282 {
283 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
284 struct stat statbuf;
285 unsigned int i;
286 Elf_Phdr phdr;
287 size_t tmp, page_mask = sysconf( _SC_PAGESIZE ) - 1;
288 char* filename;
289 unsigned len;
290 BOOL ret = FALSE;
291
292 switch (emfd->kind)
293 {
294 case from_file:
295 len = WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, NULL, 0, NULL, NULL);
296 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
297 WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, filename, len, NULL, NULL);
298 break;
299 case from_process:
300 filename = NULL;
301 break;
302 default: assert(0);
303 return FALSE;
304 }
305
306 elf_reset_file_map(fmap);
307
308 fmap->modtype = DMT_ELF;
309 fmap->u.elf.fd = -1;
310 fmap->u.elf.target_copy = NULL;
311
312 switch (emfd->kind)
313 {
314 case from_file:
315 /* check that the file exists, and that the module hasn't been loaded yet */
316 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
317
318 /* Now open the file, so that we can mmap() it. */
319 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
320 break;
321 case from_process:
322 break;
323 }
324 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
325 goto done;
326
327 /* and check for an ELF header */
328 if (memcmp(fmap->u.elf.elfhdr.e_ident,
329 elf_signature, sizeof(elf_signature))) goto done;
330 /* and check 32 vs 64 size according to current machine */
331 #ifdef _WIN64
332 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
333 #else
334 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
335 #endif
336 fmap->addr_size = fmap->u.elf.elfhdr.e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
337 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
338 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
339 if (!fmap->u.elf.sect) goto done;
340
341 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
342 {
343 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
344 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
345 {
346 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
347 fmap->u.elf.sect = NULL;
348 goto done;
349 }
350 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
351 }
352
353 /* grab size of module once loaded in memory */
354 fmap->u.elf.elf_size = 0;
355 fmap->u.elf.elf_start = ~0L;
356 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
357 {
358 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
359 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
360 phdr.p_type == PT_LOAD)
361 {
362 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
363 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
364 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
365 }
366 }
367 /* if non relocatable ELF, then remove fixed address from computation
368 * otherwise, all addresses are zero based and start has no effect
369 */
370 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
371
372 switch (emfd->kind)
373 {
374 case from_file: break;
375 case from_process:
376 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
377 {
378 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
379 goto done;
380 }
381 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
382 fmap->u.elf.elf_size, NULL))
383 {
384 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
385 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
386 goto done;
387 }
388 break;
389 }
390 ret = TRUE;
391 done:
392 HeapFree(GetProcessHeap(), 0, filename);
393 return ret;
394 }
395
396 /******************************************************************
397 * elf_unmap_file
398 *
399 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
400 */
401 static void elf_unmap_file(struct image_file_map* fmap)
402 {
403 while (fmap)
404 {
405 if (fmap->u.elf.fd != -1)
406 {
407 struct image_section_map ism;
408 ism.fmap = fmap;
409 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
410 {
411 elf_unmap_section(&ism);
412 }
413 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
414 close(fmap->u.elf.fd);
415 }
416 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
417 fmap = fmap->u.elf.alternate;
418 }
419 }
420
421 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
422 {
423 elf_unmap_file(&modfmt->u.elf_info->file_map);
424 HeapFree(GetProcessHeap(), 0, modfmt);
425 }
426
427 /******************************************************************
428 * elf_is_in_thunk_area
429 *
430 * Check whether an address lies within one of the thunk area we
431 * know of.
432 */
433 int elf_is_in_thunk_area(unsigned long addr,
434 const struct elf_thunk_area* thunks)
435 {
436 unsigned i;
437
438 if (thunks) for (i = 0; thunks[i].symname; i++)
439 {
440 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
441 return i;
442 }
443 return -1;
444 }
445
446 /******************************************************************
447 * elf_hash_symtab
448 *
449 * creating an internal hash table to ease use ELF symtab information lookup
450 */
451 static void elf_hash_symtab(struct module* module, struct pool* pool,
452 struct hash_table* ht_symtab, struct image_file_map* fmap,
453 struct elf_thunk_area* thunks)
454 {
455 int i, j, nsym;
456 const char* strp;
457 const char* symname;
458 struct symt_compiland* compiland = NULL;
459 const char* ptr;
460 const Elf_Sym* symp;
461 struct symtab_elt* ste;
462 struct image_section_map ism, ism_str;
463
464 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
465 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
466 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
467 ism_str.fmap = ism.fmap;
468 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
469 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
470 {
471 image_unmap_section(&ism);
472 return;
473 }
474
475 nsym = image_get_map_size(&ism) / sizeof(*symp);
476
477 for (j = 0; thunks[j].symname; j++)
478 thunks[j].rva_start = thunks[j].rva_end = 0;
479
480 for (i = 0; i < nsym; i++, symp++)
481 {
482 /* Ignore certain types of entries which really aren't of that much
483 * interest.
484 */
485 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
486 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
487 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
488 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
489 symp->st_shndx == SHN_UNDEF)
490 {
491 continue;
492 }
493
494 symname = strp + symp->st_name;
495
496 /* handle some specific symtab (that we'll throw away when done) */
497 switch (ELF32_ST_TYPE(symp->st_info))
498 {
499 case STT_FILE:
500 if (symname)
501 compiland = symt_new_compiland(module, symp->st_value,
502 source_new(module, NULL, symname));
503 else
504 compiland = NULL;
505 continue;
506 case STT_NOTYPE:
507 /* we are only interested in wine markers inserted by winebuild */
508 for (j = 0; thunks[j].symname; j++)
509 {
510 if (!strcmp(symname, thunks[j].symname))
511 {
512 thunks[j].rva_start = symp->st_value;
513 thunks[j].rva_end = symp->st_value + symp->st_size;
514 break;
515 }
516 }
517 continue;
518 }
519
520 /* FIXME: we don't need to handle them (GCC internals)
521 * Moreover, they screw up our symbol lookup :-/
522 */
523 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
524 continue;
525
526 ste = pool_alloc(pool, sizeof(*ste));
527 ste->ht_elt.name = symname;
528 /* GCC emits, in some cases, a .<digit>+ suffix.
529 * This is used for static variable inside functions, so
530 * that we can have several such variables with same name in
531 * the same compilation unit
532 * We simply ignore that suffix when present (we also get rid
533 * of it in stabs parsing)
534 */
535 ptr = symname + strlen(symname) - 1;
536 if (isdigit(*ptr))
537 {
538 while (isdigit(*ptr) && ptr >= symname) ptr--;
539 if (ptr > symname && *ptr == '.')
540 {
541 char* n = pool_alloc(pool, ptr - symname + 1);
542 memcpy(n, symname, ptr - symname + 1);
543 n[ptr - symname] = '\0';
544 ste->ht_elt.name = n;
545 }
546 }
547 ste->symp = symp;
548 ste->compiland = compiland;
549 ste->used = 0;
550 hash_table_add(ht_symtab, &ste->ht_elt);
551 }
552 /* as we added in the ht_symtab pointers to the symbols themselves,
553 * we cannot unmap yet the sections, it will be done when we're over
554 * with this ELF file
555 */
556 }
557
558 /******************************************************************
559 * elf_lookup_symtab
560 *
561 * lookup a symbol by name in our internal hash table for the symtab
562 */
563 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
564 const struct hash_table* ht_symtab,
565 const char* name, const struct symt* compiland)
566 {
567 struct symtab_elt* weak_result = NULL; /* without compiland name */
568 struct symtab_elt* result = NULL;
569 struct hash_table_iter hti;
570 struct symtab_elt* ste;
571 const char* compiland_name;
572 const char* compiland_basename;
573 const char* base;
574
575 /* we need weak match up (at least) when symbols of same name,
576 * defined several times in different compilation units,
577 * are merged in a single one (hence a different filename for c.u.)
578 */
579 if (compiland)
580 {
581 compiland_name = source_get(module,
582 ((const struct symt_compiland*)compiland)->source);
583 compiland_basename = strrchr(compiland_name, '/');
584 if (!compiland_basename++) compiland_basename = compiland_name;
585 }
586 else compiland_name = compiland_basename = NULL;
587
588 hash_table_iter_init(ht_symtab, &hti, name);
589 while ((ste = hash_table_iter_up(&hti)))
590 {
591 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
592
593 weak_result = ste;
594 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
595 continue;
596 if (ste->compiland && compiland_name)
597 {
598 const char* filename = source_get(module, ste->compiland->source);
599 if (strcmp(filename, compiland_name))
600 {
601 base = strrchr(filename, '/');
602 if (!base++) base = filename;
603 if (strcmp(base, compiland_basename)) continue;
604 }
605 }
606 if (result)
607 {
608 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
609 name, compiland_name,
610 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
611 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
612 }
613 else
614 {
615 result = ste;
616 ste->used = 1;
617 }
618 }
619 if (!result && !(result = weak_result))
620 {
621 FIXME("Couldn't find symbol %s!%s in symtab\n",
622 debugstr_w(module->module.ModuleName), name);
623 return NULL;
624 }
625 return result->symp;
626 }
627
628 /******************************************************************
629 * elf_finish_stabs_info
630 *
631 * - get any relevant information (address & size) from the bits we got from the
632 * stabs debugging information
633 */
634 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
635 {
636 struct hash_table_iter hti;
637 void* ptr;
638 struct symt_ht* sym;
639 const Elf_Sym* symp;
640 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
641
642 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
643 while ((ptr = hash_table_iter_up(&hti)))
644 {
645 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
646 switch (sym->symt.tag)
647 {
648 case SymTagFunction:
649 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
650 ((struct symt_function*)sym)->size)
651 {
652 break;
653 }
654 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
655 ((struct symt_function*)sym)->container);
656 if (symp)
657 {
658 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
659 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
660 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
661 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
662 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
663 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
664 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
665 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
666 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
667
668 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
669 ((struct symt_function*)sym)->size = symp->st_size;
670 } else
671 FIXME("Couldn't find %s!%s\n",
672 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
673 break;
674 case SymTagData:
675 switch (((struct symt_data*)sym)->kind)
676 {
677 case DataIsGlobal:
678 case DataIsFileStatic:
679 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
680 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
681 break;
682 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
683 ((struct symt_data*)sym)->container);
684 if (symp)
685 {
686 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
687 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
688 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
689 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
690 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
691 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
692 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
693 DataIsFileStatic : DataIsGlobal;
694 } else
695 FIXME("Couldn't find %s!%s\n",
696 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
697 break;
698 default:;
699 }
700 break;
701 default:
702 FIXME("Unsupported tag %u\n", sym->symt.tag);
703 break;
704 }
705 }
706 /* since we may have changed some addresses & sizes, mark the module to be resorted */
707 module->sortlist_valid = FALSE;
708 }
709
710 /******************************************************************
711 * elf_load_wine_thunks
712 *
713 * creating the thunk objects for a wine native DLL
714 */
715 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
716 const struct elf_thunk_area* thunks)
717 {
718 int j;
719 struct hash_table_iter hti;
720 struct symtab_elt* ste;
721 DWORD_PTR addr;
722 struct symt_ht* symt;
723
724 hash_table_iter_init(ht_symtab, &hti, NULL);
725 while ((ste = hash_table_iter_up(&hti)))
726 {
727 if (ste->used) continue;
728
729 addr = module->reloc_delta + ste->symp->st_value;
730
731 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
732 if (j >= 0) /* thunk found */
733 {
734 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
735 addr, ste->symp->st_size);
736 }
737 else
738 {
739 ULONG64 ref_addr;
740 struct location loc;
741
742 symt = symt_find_nearest(module, addr);
743 if (symt && !symt_get_address(&symt->symt, &ref_addr))
744 ref_addr = addr;
745 if (!symt || addr != ref_addr)
746 {
747 /* creating public symbols for all the ELF symbols which haven't been
748 * used yet (ie we have no debug information on them)
749 * That's the case, for example, of the .spec.c files
750 */
751 switch (ELF32_ST_TYPE(ste->symp->st_info))
752 {
753 case STT_FUNC:
754 symt_new_function(module, ste->compiland, ste->ht_elt.name,
755 addr, ste->symp->st_size, NULL);
756 break;
757 case STT_OBJECT:
758 loc.kind = loc_absolute;
759 loc.reg = 0;
760 loc.offset = addr;
761 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
762 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
763 loc, ste->symp->st_size, NULL);
764 break;
765 default:
766 FIXME("Shouldn't happen\n");
767 break;
768 }
769 /* FIXME: this is a hack !!!
770 * we are adding new symbols, but as we're parsing a symbol table
771 * (hopefully without duplicate symbols) we delay rebuilding the sorted
772 * module table until we're done with the symbol table
773 * Otherwise, as we intertwine symbols' add and lookup, performance
774 * is rather bad
775 */
776 module->sortlist_valid = TRUE;
777 }
778 }
779 }
780 /* see comment above */
781 module->sortlist_valid = FALSE;
782 return TRUE;
783 }
784
785 /******************************************************************
786 * elf_new_public_symbols
787 *
788 * Creates a set of public symbols from an ELF symtab
789 */
790 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
791 {
792 struct hash_table_iter hti;
793 struct symtab_elt* ste;
794
795 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
796
797 /* FIXME: we're missing the ELF entry point here */
798
799 hash_table_iter_init(symtab, &hti, NULL);
800 while ((ste = hash_table_iter_up(&hti)))
801 {
802 symt_new_public(module, ste->compiland, ste->ht_elt.name,
803 module->reloc_delta + ste->symp->st_value,
804 ste->symp->st_size);
805 }
806 return TRUE;
807 }
808
809 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
810 {
811 BOOL ret;
812 struct elf_map_file_data emfd;
813
814 emfd.kind = from_file;
815 emfd.u.file.filename = file;
816 if (!elf_map_file(&emfd, fmap)) return FALSE;
817 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
818 {
819 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
820 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
821 elf_unmap_file(fmap);
822 }
823 return ret;
824 }
825
826 /******************************************************************
827 * elf_locate_debug_link
828 *
829 * Locate a filename from a .gnu_debuglink section, using the same
830 * strategy as gdb:
831 * "If the full name of the directory containing the executable is
832 * execdir, and the executable has a debug link that specifies the
833 * name debugfile, then GDB will automatically search for the
834 * debugging information file in three places:
835 * - the directory containing the executable file (that is, it
836 * will look for a file named `execdir/debugfile',
837 * - a subdirectory of that directory named `.debug' (that is, the
838 * file `execdir/.debug/debugfile', and
839 * - a subdirectory of the global debug file directory that includes
840 * the executable's full path, and the name from the link (that is,
841 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
842 * is the global debug file directory, and execdir has been turned
843 * into a relative path)." (from GDB manual)
844 */
845 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
846 const WCHAR* loaded_file, DWORD crc)
847 {
848 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
849 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
850 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
851 size_t filename_len;
852 WCHAR* p = NULL;
853 WCHAR* slash;
854 struct image_file_map* fmap_link = NULL;
855
856 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
857 if (!fmap_link) return FALSE;
858
859 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
860 p = HeapAlloc(GetProcessHeap(), 0,
861 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
862 if (!p) goto found;
863
864 /* we prebuild the string with "execdir" */
865 strcpyW(p, loaded_file);
866 slash = strrchrW(p, '/');
867 if (slash == NULL) slash = p; else slash++;
868
869 /* testing execdir/filename */
870 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
871 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
872
873 /* testing execdir/.debug/filename */
874 memcpy(slash, dotDebugW, sizeof(dotDebugW));
875 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
876 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
877
878 /* testing globaldebugdir/execdir/filename */
879 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
880 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
881 slash += globalDebugDirLen;
882 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
883 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
884
885 /* finally testing filename */
886 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
887
888
889 WARN("Couldn't locate or map %s\n", filename);
890 HeapFree(GetProcessHeap(), 0, p);
891 HeapFree(GetProcessHeap(), 0, fmap_link);
892 return FALSE;
893
894 found:
895 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
896 HeapFree(GetProcessHeap(), 0, p);
897 fmap->u.elf.alternate = fmap_link;
898 return TRUE;
899 }
900
901 /******************************************************************
902 * elf_locate_build_id_target
903 *
904 * Try to find the .so file containing the debug info out of the build-id note information
905 */
906 static BOOL elf_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
907 {
908 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
909 static const WCHAR buildidW[] = {'.','b','u','i','l','d','-','i','d','/'};
910 static const WCHAR dotDebug0W[] = {'.','d','e','b','u','g',0};
911 struct image_file_map* fmap_link = NULL;
912 WCHAR* p;
913 WCHAR* z;
914 const BYTE* idend = id + idlen;
915 struct elf_map_file_data emfd;
916
917 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
918 if (!fmap_link) return FALSE;
919
920 p = HeapAlloc(GetProcessHeap(), 0,
921 sizeof(globalDebugDirW) + sizeof(buildidW) +
922 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(dotDebug0W));
923 z = p;
924 memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
925 z += sizeof(globalDebugDirW) / sizeof(WCHAR);
926 memcpy(z, buildidW, sizeof(buildidW));
927 z += sizeof(buildidW) / sizeof(WCHAR);
928
929 if (id < idend)
930 {
931 *z++ = "0123456789abcdef"[*id >> 4 ];
932 *z++ = "0123456789abcdef"[*id & 0x0F];
933 id++;
934 }
935 if (id < idend)
936 *z++ = '/';
937 while (id < idend)
938 {
939 *z++ = "0123456789abcdef"[*id >> 4 ];
940 *z++ = "0123456789abcdef"[*id & 0x0F];
941 id++;
942 }
943 memcpy(z, dotDebug0W, sizeof(dotDebug0W));
944 TRACE("checking %s\n", wine_dbgstr_w(p));
945
946 emfd.kind = from_file;
947 emfd.u.file.filename = p;
948 if (elf_map_file(&emfd, fmap_link))
949 {
950 struct image_section_map buildid_sect;
951 if (elf_find_section(fmap_link, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
952 {
953 const uint32_t* note;
954
955 note = (const uint32_t*)image_map_section(&buildid_sect);
956 if (note != IMAGE_NO_MAP)
957 {
958 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
959 if (note[2] == NT_GNU_BUILD_ID)
960 {
961 if (note[1] == idlen &&
962 !memcmp(note + 3 + ((note[0] + 3) >> 2), idend - idlen, idlen))
963 {
964 TRACE("Located debug information file at %s\n", debugstr_w(p));
965 HeapFree(GetProcessHeap(), 0, p);
966 fmap->u.elf.alternate = fmap_link;
967 return TRUE;
968 }
969 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p));
970 }
971 }
972 image_unmap_section(&buildid_sect);
973 }
974 elf_unmap_file(fmap_link);
975 }
976
977 TRACE("not found\n");
978 HeapFree(GetProcessHeap(), 0, p);
979 HeapFree(GetProcessHeap(), 0, fmap_link);
980 return FALSE;
981 }
982
983 /******************************************************************
984 * elf_check_alternate
985 *
986 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
987 * or .gnu_debuglink sections.
988 */
989 static BOOL elf_check_alternate(struct image_file_map* fmap, const struct module* module)
990 {
991 BOOL ret = FALSE;
992 BOOL found = FALSE;
993 struct image_section_map buildid_sect, debuglink_sect;
994
995 /* if present, add the .gnu_debuglink file as an alternate to current one */
996 if (elf_find_section(fmap, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
997 {
998 const uint32_t* note;
999
1000 found = TRUE;
1001 note = (const uint32_t*)image_map_section(&buildid_sect);
1002 if (note != IMAGE_NO_MAP)
1003 {
1004 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1005 if (note[2] == NT_GNU_BUILD_ID)
1006 {
1007 ret = elf_locate_build_id_target(fmap, (const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
1008 }
1009 }
1010 image_unmap_section(&buildid_sect);
1011 }
1012 /* if present, add the .gnu_debuglink file as an alternate to current one */
1013 if (!ret && elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
1014 {
1015 const char* dbg_link;
1016
1017 found = TRUE;
1018 dbg_link = (const char*)image_map_section(&debuglink_sect);
1019 if (dbg_link != IMAGE_NO_MAP)
1020 {
1021 /* The content of a debug link section is:
1022 * 1/ a NULL terminated string, containing the file name for the
1023 * debug info
1024 * 2/ padding on 4 byte boundary
1025 * 3/ CRC of the linked ELF file
1026 */
1027 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
1028 ret = elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
1029 if (!ret)
1030 WARN("Couldn't load linked debug file for %s\n",
1031 debugstr_w(module->module.ModuleName));
1032 }
1033 image_unmap_section(&debuglink_sect);
1034 }
1035 return found ? ret : TRUE;
1036 }
1037
1038 /******************************************************************
1039 * elf_load_debug_info_from_map
1040 *
1041 * Loads the symbolic information from ELF module which mapping is described
1042 * in fmap
1043 * the module has been loaded at 'load_offset' address, so symbols' address
1044 * relocation is performed.
1045 * CRC is checked if fmap->with_crc is TRUE
1046 * returns
1047 * 0 if the file doesn't contain symbolic info (or this info cannot be
1048 * read or parsed)
1049 * 1 on success
1050 */
1051 static BOOL elf_load_debug_info_from_map(struct module* module,
1052 struct image_file_map* fmap,
1053 struct pool* pool,
1054 struct hash_table* ht_symtab)
1055 {
1056 BOOL ret = FALSE, lret;
1057 struct elf_thunk_area thunks[] =
1058 {
1059 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
1060 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1061 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1062 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1063 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1064 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1065 {NULL, 0, 0, 0}
1066 };
1067
1068 module->module.SymType = SymExport;
1069
1070 /* create a hash table for the symtab */
1071 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
1072
1073 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1074 {
1075 struct image_section_map stab_sect, stabstr_sect;
1076
1077 /* check if we need an alternate file (from debuglink or build-id) */
1078 ret = elf_check_alternate(fmap, module);
1079
1080 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
1081 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
1082 {
1083 const char* stab;
1084 const char* stabstr;
1085
1086 stab = image_map_section(&stab_sect);
1087 stabstr = image_map_section(&stabstr_sect);
1088 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
1089 {
1090 /* OK, now just parse all of the stabs. */
1091 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
1092 stab, image_get_map_size(&stab_sect),
1093 stabstr, image_get_map_size(&stabstr_sect),
1094 NULL, NULL);
1095 if (lret)
1096 /* and fill in the missing information for stabs */
1097 elf_finish_stabs_info(module, ht_symtab);
1098 else
1099 WARN("Couldn't correctly read stabs\n");
1100 ret = ret || lret;
1101 }
1102 image_unmap_section(&stab_sect);
1103 image_unmap_section(&stabstr_sect);
1104 }
1105 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
1106 ret = ret || lret;
1107 }
1108 if (strstrW(module->module.ModuleName, S_ElfW) ||
1109 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1110 {
1111 /* add the thunks for native libraries */
1112 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1113 elf_new_wine_thunks(module, ht_symtab, thunks);
1114 }
1115 /* add all the public symbols from symtab */
1116 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1117
1118 return ret;
1119 }
1120
1121 /******************************************************************
1122 * elf_load_debug_info
1123 *
1124 * Loads ELF debugging information from the module image file.
1125 */
1126 BOOL elf_load_debug_info(struct module* module)
1127 {
1128 BOOL ret = TRUE;
1129 struct pool pool;
1130 struct hash_table ht_symtab;
1131 struct module_format* modfmt;
1132
1133 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1134 {
1135 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1136 return FALSE;
1137 }
1138
1139 pool_init(&pool, 65536);
1140 hash_table_init(&pool, &ht_symtab, 256);
1141
1142 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1143
1144 pool_destroy(&pool);
1145 return ret;
1146 }
1147
1148 /******************************************************************
1149 * elf_fetch_file_info
1150 *
1151 * Gathers some more information for an ELF module from a given file
1152 */
1153 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1154 DWORD* size, DWORD* checksum)
1155 {
1156 struct image_file_map fmap;
1157
1158 struct elf_map_file_data emfd;
1159
1160 emfd.kind = from_file;
1161 emfd.u.file.filename = name;
1162 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1163 if (base) *base = fmap.u.elf.elf_start;
1164 *size = fmap.u.elf.elf_size;
1165 *checksum = calc_crc32(fmap.u.elf.fd);
1166 elf_unmap_file(&fmap);
1167 return TRUE;
1168 }
1169
1170 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1171 struct image_file_map* fmap, unsigned long load_offset,
1172 unsigned long dyn_addr, struct elf_info* elf_info)
1173 {
1174 BOOL ret = FALSE;
1175
1176 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1177 {
1178 struct image_section_map ism;
1179
1180 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1181 {
1182 Elf_Dyn dyn;
1183 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1184 unsigned long len;
1185
1186 if (load_offset) ptr += load_offset - fmap->u.elf.elf_start;
1187
1188 do
1189 {
1190 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1191 len != sizeof(dyn))
1192 return ret;
1193 if (dyn.d_tag == DT_DEBUG)
1194 {
1195 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1196 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1197 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1198 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1199 break;
1200 }
1201 ptr += sizeof(dyn);
1202 } while (dyn.d_tag != DT_NULL);
1203 if (dyn.d_tag == DT_NULL) return ret;
1204 }
1205 elf_end_find(fmap);
1206 }
1207
1208 if (elf_info->flags & ELF_INFO_MODULE)
1209 {
1210 struct elf_module_info *elf_module_info;
1211 struct module_format* modfmt;
1212 struct image_section_map ism;
1213 unsigned long modbase = load_offset;
1214
1215 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1216 {
1217 unsigned long rva_dyn = elf_get_map_rva(&ism);
1218
1219 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1220 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1221 load_offset, dyn_addr);
1222 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1223 {
1224 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1225 modbase = dyn_addr - rva_dyn;
1226 }
1227 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1228 elf_end_find(fmap);
1229
1230 modfmt = HeapAlloc(GetProcessHeap(), 0,
1231 sizeof(struct module_format) + sizeof(struct elf_module_info));
1232 if (!modfmt) return FALSE;
1233 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1234 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1235 if (!elf_info->module)
1236 {
1237 HeapFree(GetProcessHeap(), 0, modfmt);
1238 return FALSE;
1239 }
1240 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1241 elf_module_info = (void*)(modfmt + 1);
1242 elf_info->module->format_info[DFI_ELF] = modfmt;
1243 modfmt->module = elf_info->module;
1244 modfmt->remove = elf_module_remove;
1245 modfmt->loc_compute = NULL;
1246 modfmt->u.elf_info = elf_module_info;
1247
1248 elf_module_info->elf_addr = load_offset;
1249
1250 elf_module_info->file_map = *fmap;
1251 elf_reset_file_map(fmap);
1252 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1253 {
1254 elf_info->module->module.SymType = SymDeferred;
1255 ret = TRUE;
1256 }
1257 else ret = elf_load_debug_info(elf_info->module);
1258
1259 elf_module_info->elf_mark = 1;
1260 elf_module_info->elf_loader = 0;
1261 } else ret = TRUE;
1262
1263 if (elf_info->flags & ELF_INFO_NAME)
1264 {
1265 WCHAR* ptr;
1266 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1267 if (ptr)
1268 {
1269 strcpyW(ptr, filename);
1270 elf_info->module_name = ptr;
1271 }
1272 else ret = FALSE;
1273 }
1274
1275 return ret;
1276 }
1277
1278 /******************************************************************
1279 * elf_load_file
1280 *
1281 * Loads the information for ELF module stored in 'filename'
1282 * the module has been loaded at 'load_offset' address
1283 * returns
1284 * -1 if the file cannot be found/opened
1285 * 0 if the file doesn't contain symbolic info (or this info cannot be
1286 * read or parsed)
1287 * 1 on success
1288 */
1289 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1290 unsigned long load_offset, unsigned long dyn_addr,
1291 struct elf_info* elf_info)
1292 {
1293 BOOL ret = FALSE;
1294 struct image_file_map fmap;
1295 struct elf_map_file_data emfd;
1296
1297 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1298
1299 emfd.kind = from_file;
1300 emfd.u.file.filename = filename;
1301 if (!elf_map_file(&emfd, &fmap)) return ret;
1302
1303 /* Next, we need to find a few of the internal ELF headers within
1304 * this thing. We need the main executable header, and the section
1305 * table.
1306 */
1307 if (!fmap.u.elf.elf_start && !load_offset)
1308 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1309 debugstr_w(filename));
1310
1311 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1312
1313 elf_unmap_file(&fmap);
1314
1315 return ret;
1316 }
1317
1318 /******************************************************************
1319 * elf_load_file_from_path
1320 * tries to load an ELF file from a set of paths (separated by ':')
1321 */
1322 static BOOL elf_load_file_from_path(HANDLE hProcess,
1323 const WCHAR* filename,
1324 unsigned long load_offset,
1325 unsigned long dyn_addr,
1326 const char* path,
1327 struct elf_info* elf_info)
1328 {
1329 BOOL ret = FALSE;
1330 WCHAR *s, *t, *fn;
1331 WCHAR* pathW = NULL;
1332 unsigned len;
1333
1334 if (!path) return FALSE;
1335
1336 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1337 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1338 if (!pathW) return FALSE;
1339 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1340
1341 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1342 {
1343 t = strchrW(s, ':');
1344 if (t) *t = '\0';
1345 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1346 if (!fn) break;
1347 strcpyW(fn, s);
1348 strcatW(fn, S_SlashW);
1349 strcatW(fn, filename);
1350 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1351 HeapFree(GetProcessHeap(), 0, fn);
1352 if (ret) break;
1353 }
1354
1355 HeapFree(GetProcessHeap(), 0, pathW);
1356 return ret;
1357 }
1358
1359 /******************************************************************
1360 * elf_load_file_from_dll_path
1361 *
1362 * Tries to load an ELF file from the dll path
1363 */
1364 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1365 const WCHAR* filename,
1366 unsigned long load_offset,
1367 unsigned long dyn_addr,
1368 struct elf_info* elf_info)
1369 {
1370 BOOL ret = FALSE;
1371 unsigned int index = 0;
1372 const char *path;
1373
1374 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1375 {
1376 WCHAR *name;
1377 unsigned len;
1378
1379 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1380
1381 name = HeapAlloc( GetProcessHeap(), 0,
1382 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1383
1384 if (!name) break;
1385 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1386 strcatW( name, S_SlashW );
1387 strcatW( name, filename );
1388 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1389 HeapFree( GetProcessHeap(), 0, name );
1390 }
1391 return ret;
1392 }
1393
1394 #ifdef AT_SYSINFO_EHDR
1395 /******************************************************************
1396 * elf_search_auxv
1397 *
1398 * locate some a value from the debuggee auxiliary vector
1399 */
1400 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1401 {
1402 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1403 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1404 void* addr;
1405 void* str;
1406 void* str_max;
1407 Elf_auxv_t auxv;
1408
1409 si->SizeOfStruct = sizeof(*si);
1410 si->MaxNameLen = MAX_SYM_NAME;
1411 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1412 !(addr = (void*)(DWORD_PTR)si->Address) ||
1413 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1414 !addr)
1415 {
1416 FIXME("can't find symbol in module\n");
1417 return FALSE;
1418 }
1419 /* walk through envp[] */
1420 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1421 str_max = (void*)(DWORD_PTR)~0L;
1422 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1423 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1424 str_max = min(str_max, str);
1425
1426 /* Walk through the end of envp[] array.
1427 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1428 * deleted, the last entry is replaced by an extra NULL.
1429 */
1430 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1431 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1432
1433 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1434 {
1435 if (auxv.a_type == type)
1436 {
1437 *val = auxv.a_un.a_val;
1438 return TRUE;
1439 }
1440 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1441 }
1442
1443 return FALSE;
1444 }
1445 #endif
1446
1447 /******************************************************************
1448 * elf_search_and_load_file
1449 *
1450 * lookup a file in standard ELF locations, and if found, load it
1451 */
1452 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1453 unsigned long load_offset, unsigned long dyn_addr,
1454 struct elf_info* elf_info)
1455 {
1456 BOOL ret = FALSE;
1457 struct module* module;
1458 static const WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1459
1460 if (filename == NULL || *filename == '\0') return FALSE;
1461 if ((module = module_is_already_loaded(pcs, filename)))
1462 {
1463 elf_info->module = module;
1464 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1465 return module->module.SymType;
1466 }
1467
1468 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1469 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1470 /* if relative pathname, try some absolute base dirs */
1471 if (!ret && !strchrW(filename, '/'))
1472 {
1473 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1474 getenv("PATH"), elf_info) ||
1475 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1476 getenv("LD_LIBRARY_PATH"), elf_info);
1477 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1478 load_offset, dyn_addr, elf_info);
1479 }
1480
1481 return ret;
1482 }
1483
1484 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1485 unsigned long dyn_addr, BOOL is_system, void* user);
1486
1487 /******************************************************************
1488 * elf_enum_modules_internal
1489 *
1490 * Enumerate ELF modules from a running process
1491 */
1492 static BOOL elf_enum_modules_internal(const struct process* pcs,
1493 const WCHAR* main_name,
1494 enum_elf_modules_cb cb, void* user)
1495 {
1496 struct r_debug dbg_hdr;
1497 void* lm_addr;
1498 struct link_map lm;
1499 char bufstr[256];
1500 WCHAR bufstrW[MAX_PATH];
1501
1502 if (!pcs->dbg_hdr_addr ||
1503 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1504 &dbg_hdr, sizeof(dbg_hdr), NULL))
1505 return FALSE;
1506
1507 /* Now walk the linked list. In all known ELF implementations,
1508 * the dynamic loader maintains this linked list for us. In some
1509 * cases the first entry doesn't appear with a name, in other cases it
1510 * does.
1511 */
1512 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1513 {
1514 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1515 return FALSE;
1516
1517 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1518 lm.l_name != NULL &&
1519 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1520 {
1521 bufstr[sizeof(bufstr) - 1] = '\0';
1522 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1523 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1524 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1525 }
1526 }
1527
1528 #ifdef AT_SYSINFO_EHDR
1529 if (!lm_addr)
1530 {
1531 unsigned long ehdr_addr;
1532
1533 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1534 {
1535 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1536 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1537 }
1538 }
1539 #endif
1540 return TRUE;
1541 }
1542
1543 /******************************************************************
1544 * elf_search_loader
1545 *
1546 * Lookup in a running ELF process the loader, and sets its ELF link
1547 * address (for accessing the list of loaded .so libs) in pcs.
1548 * If flags is ELF_INFO_MODULE, the module for the loader is also
1549 * added as a module into pcs.
1550 */
1551 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1552 {
1553 PROCESS_BASIC_INFORMATION pbi;
1554 ULONG_PTR base = 0;
1555
1556 if (!NtQueryInformationProcess( pcs->handle, ProcessBasicInformation, &pbi, sizeof(pbi), NULL ))
1557 ReadProcessMemory( pcs->handle, &pbi.PebBaseAddress->Reserved[0], &base, sizeof(base), NULL );
1558
1559 return elf_search_and_load_file(pcs, get_wine_loader_name(), base, 0, elf_info);
1560 }
1561
1562 /******************************************************************
1563 * elf_read_wine_loader_dbg_info
1564 *
1565 * Try to find a decent wine executable which could have loaded the debuggee
1566 */
1567 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1568 {
1569 struct elf_info elf_info;
1570
1571 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1572 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1573 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1574 module_set_module(elf_info.module, S_WineLoaderW);
1575 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1576 }
1577
1578 struct elf_enum_user
1579 {
1580 enum_modules_cb cb;
1581 void* user;
1582 };
1583
1584 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1585 unsigned long dyn_addr, BOOL is_system, void* user)
1586 {
1587 struct elf_enum_user* eeu = user;
1588 return eeu->cb(name, load_addr, eeu->user);
1589 }
1590
1591 /******************************************************************
1592 * elf_enum_modules
1593 *
1594 * Enumerates the ELF loaded modules from a running target (hProc)
1595 * This function doesn't require that someone has called SymInitialize
1596 * on this very process.
1597 */
1598 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1599 {
1600 struct process pcs;
1601 struct elf_info elf_info;
1602 BOOL ret;
1603 struct elf_enum_user eeu;
1604
1605 memset(&pcs, 0, sizeof(pcs));
1606 pcs.handle = hProc;
1607 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1608 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1609 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1610 eeu.cb = cb;
1611 eeu.user = user;
1612 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1613 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1614 return ret;
1615 }
1616
1617 struct elf_load
1618 {
1619 struct process* pcs;
1620 struct elf_info elf_info;
1621 const WCHAR* name;
1622 BOOL ret;
1623 };
1624
1625 /******************************************************************
1626 * elf_load_cb
1627 *
1628 * Callback for elf_load_module, used to walk the list of loaded
1629 * modules.
1630 */
1631 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1632 unsigned long dyn_addr, BOOL is_system, void* user)
1633 {
1634 struct elf_load* el = user;
1635 BOOL ret = TRUE;
1636 const WCHAR* p;
1637
1638 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1639 {
1640 struct module* module;
1641 struct elf_map_file_data emfd;
1642 struct image_file_map fmap;
1643
1644 if ((module = module_is_already_loaded(el->pcs, name)))
1645 {
1646 el->elf_info.module = module;
1647 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1648 return module->module.SymType;
1649 }
1650
1651 emfd.kind = from_process;
1652 emfd.u.process.handle = el->pcs->handle;
1653 emfd.u.process.load_addr = (void*)load_addr;
1654
1655 if (elf_map_file(&emfd, &fmap))
1656 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1657 return TRUE;
1658 }
1659 if (el->name)
1660 {
1661 /* memcmp is needed for matches when bufstr contains also version information
1662 * el->name: libc.so, name: libc.so.6.0
1663 */
1664 p = strrchrW(name, '/');
1665 if (!p++) p = name;
1666 }
1667
1668 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1669 {
1670 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1671 if (el->name) ret = FALSE;
1672 }
1673
1674 return ret;
1675 }
1676
1677 /******************************************************************
1678 * elf_load_module
1679 *
1680 * loads an ELF module and stores it in process' module list
1681 * Also, find module real name and load address from
1682 * the real loaded modules list in pcs address space
1683 */
1684 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1685 {
1686 struct elf_load el;
1687
1688 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1689
1690 el.elf_info.flags = ELF_INFO_MODULE;
1691 el.ret = FALSE;
1692
1693 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1694 {
1695 el.pcs = pcs;
1696 /* do only the lookup from the filename, not the path (as we lookup module
1697 * name in the process' loaded module list)
1698 */
1699 el.name = strrchrW(name, '/');
1700 if (!el.name++) el.name = name;
1701 el.ret = FALSE;
1702
1703 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1704 return NULL;
1705 }
1706 else if (addr)
1707 {
1708 el.name = name;
1709 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1710 }
1711 if (!el.ret) return NULL;
1712 assert(el.elf_info.module);
1713 return el.elf_info.module;
1714 }
1715
1716 /******************************************************************
1717 * elf_synchronize_module_list
1718 *
1719 * this function rescans the debuggee module's list and synchronizes it with
1720 * the one from 'pcs', i.e.:
1721 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1722 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1723 */
1724 BOOL elf_synchronize_module_list(struct process* pcs)
1725 {
1726 struct module* module;
1727 struct elf_load el;
1728
1729 for (module = pcs->lmodules; module; module = module->next)
1730 {
1731 if (module->type == DMT_ELF && !module->is_virtual)
1732 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1733 }
1734
1735 el.pcs = pcs;
1736 el.elf_info.flags = ELF_INFO_MODULE;
1737 el.ret = FALSE;
1738 el.name = NULL; /* fetch all modules */
1739
1740 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1741 return FALSE;
1742
1743 module = pcs->lmodules;
1744 while (module)
1745 {
1746 if (module->type == DMT_ELF && !module->is_virtual)
1747 {
1748 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1749
1750 if (!elf_info->elf_mark && !elf_info->elf_loader)
1751 {
1752 module_remove(pcs, module);
1753 /* restart all over */
1754 module = pcs->lmodules;
1755 continue;
1756 }
1757 }
1758 module = module->next;
1759 }
1760 return TRUE;
1761 }
1762
1763 #else /* !__ELF__ */
1764
1765 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1766 unsigned sht, struct image_section_map* ism)
1767 {
1768 return FALSE;
1769 }
1770
1771 const char* elf_map_section(struct image_section_map* ism)
1772 {
1773 return NULL;
1774 }
1775
1776 void elf_unmap_section(struct image_section_map* ism)
1777 {}
1778
1779 unsigned elf_get_map_size(const struct image_section_map* ism)
1780 {
1781 return 0;
1782 }
1783
1784 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1785 {
1786 return 0;
1787 }
1788
1789 BOOL elf_synchronize_module_list(struct process* pcs)
1790 {
1791 return FALSE;
1792 }
1793
1794 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1795 DWORD* size, DWORD* checksum)
1796 {
1797 return FALSE;
1798 }
1799
1800 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1801 {
1802 return FALSE;
1803 }
1804
1805 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1806 {
1807 return FALSE;
1808 }
1809
1810 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1811 {
1812 return NULL;
1813 }
1814
1815 BOOL elf_load_debug_info(struct module* module)
1816 {
1817 return FALSE;
1818 }
1819
1820 int elf_is_in_thunk_area(unsigned long addr,
1821 const struct elf_thunk_area* thunks)
1822 {
1823 return -1;
1824 }
1825 #endif /* __ELF__ */