msvcp90: Added _Locinfo::_Gettnames implementation.
[wine/multimedia.git] / dlls / dbghelp / elf_module.c
blobe4c6e448152f805256e901f90aeb5ac4c2ca3c73
1 /*
2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2007 Eric Pouech
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #if defined(__svr4__) || defined(__sun)
26 #define __ELF__ 1
27 /* large files are not supported by libelf */
28 #undef _FILE_OFFSET_BITS
29 #define _FILE_OFFSET_BITS 32
30 #endif
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #include <fcntl.h>
39 #ifdef HAVE_SYS_MMAN_H
40 #include <sys/mman.h>
41 #endif
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
46 #include "dbghelp_private.h"
48 #include "image_private.h"
50 #include "wine/library.h"
51 #include "wine/debug.h"
53 #ifdef __ELF__
55 #define ELF_INFO_DEBUG_HEADER 0x0001
56 #define ELF_INFO_MODULE 0x0002
57 #define ELF_INFO_NAME 0x0004
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
61 struct elf_info
63 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
64 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
65 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
66 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
69 struct symtab_elt
71 struct hash_table_elt ht_elt;
72 const Elf_Sym* symp;
73 struct symt_compiland* compiland;
74 unsigned used;
77 struct elf_thunk_area
79 const char* symname;
80 THUNK_ORDINAL ordinal;
81 unsigned long rva_start;
82 unsigned long rva_end;
85 struct elf_module_info
87 unsigned long elf_addr;
88 unsigned short elf_mark : 1,
89 elf_loader : 1;
90 struct image_file_map file_map;
93 /******************************************************************
94 * elf_map_section
96 * Maps a single section into memory from an ELF file
98 const char* elf_map_section(struct image_section_map* ism)
100 struct elf_file_map* fmap = &ism->fmap->u.elf;
102 unsigned long pgsz = getpagesize();
103 unsigned long ofst, size;
105 assert(ism->fmap->modtype == DMT_ELF);
106 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
107 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
108 return IMAGE_NO_MAP;
110 if (fmap->target_copy)
112 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
114 /* align required information on page size (we assume pagesize is a power of 2) */
115 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
116 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
117 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
118 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
119 fmap->fd, ofst);
120 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
121 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
124 /******************************************************************
125 * elf_find_section
127 * Finds a section by name (and type) into memory from an ELF file
128 * or its alternate if any
130 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
131 unsigned sht, struct image_section_map* ism)
133 struct elf_file_map* fmap;
134 unsigned i;
136 while (_fmap)
138 fmap = &_fmap->u.elf;
139 if (fmap->shstrtab == IMAGE_NO_MAP)
141 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
142 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
144 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
146 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
147 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
149 ism->fmap = _fmap;
150 ism->sidx = i;
151 return TRUE;
154 _fmap = fmap->alternate;
156 ism->fmap = NULL;
157 ism->sidx = -1;
158 return FALSE;
161 /******************************************************************
162 * elf_unmap_section
164 * Unmaps a single section from memory
166 void elf_unmap_section(struct image_section_map* ism)
168 struct elf_file_map* fmap = &ism->fmap->u.elf;
170 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
171 fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
173 unsigned long pgsz = getpagesize();
174 unsigned long ofst, size;
176 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
177 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
178 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
179 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
180 WARN("Couldn't unmap the section\n");
181 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
185 static void elf_end_find(struct image_file_map* fmap)
187 struct image_section_map ism;
189 while (fmap)
191 ism.fmap = fmap;
192 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
193 elf_unmap_section(&ism);
194 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
195 fmap = fmap->u.elf.alternate;
199 /******************************************************************
200 * elf_get_map_rva
202 * Get the RVA of an ELF section
204 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
206 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
207 return 0;
208 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
211 /******************************************************************
212 * elf_get_map_size
214 * Get the size of an ELF section
216 unsigned elf_get_map_size(const struct image_section_map* ism)
218 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
219 return 0;
220 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
223 static inline void elf_reset_file_map(struct image_file_map* fmap)
225 fmap->u.elf.fd = -1;
226 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
227 fmap->u.elf.alternate = NULL;
228 fmap->u.elf.target_copy = NULL;
231 struct elf_map_file_data
233 enum {from_file, from_process} kind;
234 union
236 struct
238 const WCHAR* filename;
239 } file;
240 struct
242 HANDLE handle;
243 void* load_addr;
244 } process;
245 } u;
248 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
249 void* buf, size_t len, off_t off)
251 SIZE_T dw;
253 switch (emfd->kind)
255 case from_file:
256 return pread(fmap->u.elf.fd, buf, len, off) == len;
257 case from_process:
258 return ReadProcessMemory(emfd->u.process.handle,
259 (void*)((unsigned long)emfd->u.process.load_addr + (unsigned long)off),
260 buf, len, &dw) && dw == len;
261 default:
262 assert(0);
263 return FALSE;
267 /******************************************************************
268 * elf_map_file
270 * Maps an ELF file into memory (and checks it's a real ELF file)
272 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
274 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
275 struct stat statbuf;
276 int i;
277 Elf_Phdr phdr;
278 unsigned long tmp, page_mask = getpagesize() - 1;
279 char* filename;
280 unsigned len;
281 BOOL ret = FALSE;
283 switch (emfd->kind)
285 case from_file:
286 len = WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, NULL, 0, NULL, NULL);
287 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
288 WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, filename, len, NULL, NULL);
289 break;
290 case from_process:
291 filename = NULL;
292 break;
293 default: assert(0);
294 return FALSE;
297 elf_reset_file_map(fmap);
299 fmap->modtype = DMT_ELF;
300 fmap->u.elf.fd = -1;
301 fmap->u.elf.target_copy = NULL;
303 switch (emfd->kind)
305 case from_file:
306 /* check that the file exists, and that the module hasn't been loaded yet */
307 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
309 /* Now open the file, so that we can mmap() it. */
310 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
311 break;
312 case from_process:
313 break;
315 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
316 goto done;
318 /* and check for an ELF header */
319 if (memcmp(fmap->u.elf.elfhdr.e_ident,
320 elf_signature, sizeof(elf_signature))) goto done;
321 /* and check 32 vs 64 size according to current machine */
322 #ifdef _WIN64
323 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
324 #else
325 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
326 #endif
327 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
328 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
329 if (!fmap->u.elf.sect) goto done;
331 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
333 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
334 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
336 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
337 fmap->u.elf.sect = NULL;
338 goto done;
340 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
343 /* grab size of module once loaded in memory */
344 fmap->u.elf.elf_size = 0;
345 fmap->u.elf.elf_start = ~0L;
346 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
348 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
349 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
350 phdr.p_type == PT_LOAD)
352 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
353 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
354 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
357 /* if non relocatable ELF, then remove fixed address from computation
358 * otherwise, all addresses are zero based and start has no effect
360 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
362 switch (emfd->kind)
364 case from_file: break;
365 case from_process:
366 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
368 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
369 goto done;
371 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
372 fmap->u.elf.elf_size, NULL))
374 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
375 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
376 goto done;
378 break;
380 ret = TRUE;
381 done:
382 HeapFree(GetProcessHeap(), 0, filename);
383 return ret;
386 /******************************************************************
387 * elf_unmap_file
389 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
391 static void elf_unmap_file(struct image_file_map* fmap)
393 while (fmap)
395 if (fmap->u.elf.fd != -1)
397 struct image_section_map ism;
398 ism.fmap = fmap;
399 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
401 elf_unmap_section(&ism);
403 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
404 close(fmap->u.elf.fd);
406 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
407 fmap = fmap->u.elf.alternate;
411 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
413 elf_unmap_file(&modfmt->u.elf_info->file_map);
414 HeapFree(GetProcessHeap(), 0, modfmt);
417 /******************************************************************
418 * elf_is_in_thunk_area
420 * Check whether an address lies within one of the thunk area we
421 * know of.
423 int elf_is_in_thunk_area(unsigned long addr,
424 const struct elf_thunk_area* thunks)
426 unsigned i;
428 if (thunks) for (i = 0; thunks[i].symname; i++)
430 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
431 return i;
433 return -1;
436 /******************************************************************
437 * elf_hash_symtab
439 * creating an internal hash table to ease use ELF symtab information lookup
441 static void elf_hash_symtab(struct module* module, struct pool* pool,
442 struct hash_table* ht_symtab, struct image_file_map* fmap,
443 struct elf_thunk_area* thunks)
445 int i, j, nsym;
446 const char* strp;
447 const char* symname;
448 struct symt_compiland* compiland = NULL;
449 const char* ptr;
450 const Elf_Sym* symp;
451 struct symtab_elt* ste;
452 struct image_section_map ism, ism_str;
454 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
455 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
456 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
457 ism_str.fmap = ism.fmap;
458 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
459 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
461 image_unmap_section(&ism);
462 return;
465 nsym = image_get_map_size(&ism) / sizeof(*symp);
467 for (j = 0; thunks[j].symname; j++)
468 thunks[j].rva_start = thunks[j].rva_end = 0;
470 for (i = 0; i < nsym; i++, symp++)
472 /* Ignore certain types of entries which really aren't of that much
473 * interest.
475 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
476 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
477 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
478 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
479 symp->st_shndx == SHN_UNDEF)
481 continue;
484 symname = strp + symp->st_name;
486 /* handle some specific symtab (that we'll throw away when done) */
487 switch (ELF32_ST_TYPE(symp->st_info))
489 case STT_FILE:
490 if (symname)
491 compiland = symt_new_compiland(module, symp->st_value,
492 source_new(module, NULL, symname));
493 else
494 compiland = NULL;
495 continue;
496 case STT_NOTYPE:
497 /* we are only interested in wine markers inserted by winebuild */
498 for (j = 0; thunks[j].symname; j++)
500 if (!strcmp(symname, thunks[j].symname))
502 thunks[j].rva_start = symp->st_value;
503 thunks[j].rva_end = symp->st_value + symp->st_size;
504 break;
507 continue;
510 /* FIXME: we don't need to handle them (GCC internals)
511 * Moreover, they screw up our symbol lookup :-/
513 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
514 continue;
516 ste = pool_alloc(pool, sizeof(*ste));
517 ste->ht_elt.name = symname;
518 /* GCC emits, in some cases, a .<digit>+ suffix.
519 * This is used for static variable inside functions, so
520 * that we can have several such variables with same name in
521 * the same compilation unit
522 * We simply ignore that suffix when present (we also get rid
523 * of it in stabs parsing)
525 ptr = symname + strlen(symname) - 1;
526 if (isdigit(*ptr))
528 while (isdigit(*ptr) && ptr >= symname) ptr--;
529 if (ptr > symname && *ptr == '.')
531 char* n = pool_alloc(pool, ptr - symname + 1);
532 memcpy(n, symname, ptr - symname + 1);
533 n[ptr - symname] = '\0';
534 ste->ht_elt.name = n;
537 ste->symp = symp;
538 ste->compiland = compiland;
539 ste->used = 0;
540 hash_table_add(ht_symtab, &ste->ht_elt);
542 /* as we added in the ht_symtab pointers to the symbols themselves,
543 * we cannot unmap yet the sections, it will be done when we're over
544 * with this ELF file
548 /******************************************************************
549 * elf_lookup_symtab
551 * lookup a symbol by name in our internal hash table for the symtab
553 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
554 const struct hash_table* ht_symtab,
555 const char* name, const struct symt* compiland)
557 struct symtab_elt* weak_result = NULL; /* without compiland name */
558 struct symtab_elt* result = NULL;
559 struct hash_table_iter hti;
560 struct symtab_elt* ste;
561 const char* compiland_name;
562 const char* compiland_basename;
563 const char* base;
565 /* we need weak match up (at least) when symbols of same name,
566 * defined several times in different compilation units,
567 * are merged in a single one (hence a different filename for c.u.)
569 if (compiland)
571 compiland_name = source_get(module,
572 ((const struct symt_compiland*)compiland)->source);
573 compiland_basename = strrchr(compiland_name, '/');
574 if (!compiland_basename++) compiland_basename = compiland_name;
576 else compiland_name = compiland_basename = NULL;
578 hash_table_iter_init(ht_symtab, &hti, name);
579 while ((ste = hash_table_iter_up(&hti)))
581 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
583 weak_result = ste;
584 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
585 continue;
586 if (ste->compiland && compiland_name)
588 const char* filename = source_get(module, ste->compiland->source);
589 if (strcmp(filename, compiland_name))
591 base = strrchr(filename, '/');
592 if (!base++) base = filename;
593 if (strcmp(base, compiland_basename)) continue;
596 if (result)
598 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
599 name, compiland_name,
600 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
601 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
603 else
605 result = ste;
606 ste->used = 1;
609 if (!result && !(result = weak_result))
611 FIXME("Couldn't find symbol %s!%s in symtab\n",
612 debugstr_w(module->module.ModuleName), name);
613 return NULL;
615 return result->symp;
618 /******************************************************************
619 * elf_finish_stabs_info
621 * - get any relevant information (address & size) from the bits we got from the
622 * stabs debugging information
624 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
626 struct hash_table_iter hti;
627 void* ptr;
628 struct symt_ht* sym;
629 const Elf_Sym* symp;
630 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
632 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
633 while ((ptr = hash_table_iter_up(&hti)))
635 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
636 switch (sym->symt.tag)
638 case SymTagFunction:
639 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
640 ((struct symt_function*)sym)->size)
642 break;
644 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
645 ((struct symt_function*)sym)->container);
646 if (symp)
648 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
649 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
650 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
651 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
652 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
653 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
654 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
655 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
656 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
658 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
659 ((struct symt_function*)sym)->size = symp->st_size;
660 } else
661 FIXME("Couldn't find %s!%s\n",
662 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
663 break;
664 case SymTagData:
665 switch (((struct symt_data*)sym)->kind)
667 case DataIsGlobal:
668 case DataIsFileStatic:
669 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
670 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
671 break;
672 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
673 ((struct symt_data*)sym)->container);
674 if (symp)
676 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
677 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
678 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
679 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
680 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
681 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
682 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
683 DataIsFileStatic : DataIsGlobal;
684 } else
685 FIXME("Couldn't find %s!%s\n",
686 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
687 break;
688 default:;
690 break;
691 default:
692 FIXME("Unsupported tag %u\n", sym->symt.tag);
693 break;
696 /* since we may have changed some addresses & sizes, mark the module to be resorted */
697 module->sortlist_valid = FALSE;
700 /******************************************************************
701 * elf_load_wine_thunks
703 * creating the thunk objects for a wine native DLL
705 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
706 const struct elf_thunk_area* thunks)
708 int j;
709 struct hash_table_iter hti;
710 struct symtab_elt* ste;
711 DWORD_PTR addr;
712 struct symt_ht* symt;
714 hash_table_iter_init(ht_symtab, &hti, NULL);
715 while ((ste = hash_table_iter_up(&hti)))
717 if (ste->used) continue;
719 addr = module->reloc_delta + ste->symp->st_value;
721 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
722 if (j >= 0) /* thunk found */
724 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
725 addr, ste->symp->st_size);
727 else
729 ULONG64 ref_addr;
730 struct location loc;
732 symt = symt_find_nearest(module, addr);
733 if (symt && !symt_get_address(&symt->symt, &ref_addr))
734 ref_addr = addr;
735 if (!symt || addr != ref_addr)
737 /* creating public symbols for all the ELF symbols which haven't been
738 * used yet (ie we have no debug information on them)
739 * That's the case, for example, of the .spec.c files
741 switch (ELF32_ST_TYPE(ste->symp->st_info))
743 case STT_FUNC:
744 symt_new_function(module, ste->compiland, ste->ht_elt.name,
745 addr, ste->symp->st_size, NULL);
746 break;
747 case STT_OBJECT:
748 loc.kind = loc_absolute;
749 loc.reg = 0;
750 loc.offset = addr;
751 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
752 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
753 loc, ste->symp->st_size, NULL);
754 break;
755 default:
756 FIXME("Shouldn't happen\n");
757 break;
759 /* FIXME: this is a hack !!!
760 * we are adding new symbols, but as we're parsing a symbol table
761 * (hopefully without duplicate symbols) we delay rebuilding the sorted
762 * module table until we're done with the symbol table
763 * Otherwise, as we intertwine symbols' add and lookup, performance
764 * is rather bad
766 module->sortlist_valid = TRUE;
770 /* see comment above */
771 module->sortlist_valid = FALSE;
772 return TRUE;
775 /******************************************************************
776 * elf_new_public_symbols
778 * Creates a set of public symbols from an ELF symtab
780 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
782 struct hash_table_iter hti;
783 struct symtab_elt* ste;
785 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
787 /* FIXME: we're missing the ELF entry point here */
789 hash_table_iter_init(symtab, &hti, NULL);
790 while ((ste = hash_table_iter_up(&hti)))
792 symt_new_public(module, ste->compiland, ste->ht_elt.name,
793 module->reloc_delta + ste->symp->st_value,
794 ste->symp->st_size);
796 return TRUE;
799 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
801 BOOL ret;
802 struct elf_map_file_data emfd;
804 emfd.kind = from_file;
805 emfd.u.file.filename = file;
806 if (!elf_map_file(&emfd, fmap)) return FALSE;
807 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
809 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
810 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
811 elf_unmap_file(fmap);
813 return ret;
816 /******************************************************************
817 * elf_locate_debug_link
819 * Locate a filename from a .gnu_debuglink section, using the same
820 * strategy as gdb:
821 * "If the full name of the directory containing the executable is
822 * execdir, and the executable has a debug link that specifies the
823 * name debugfile, then GDB will automatically search for the
824 * debugging information file in three places:
825 * - the directory containing the executable file (that is, it
826 * will look for a file named `execdir/debugfile',
827 * - a subdirectory of that directory named `.debug' (that is, the
828 * file `execdir/.debug/debugfile', and
829 * - a subdirectory of the global debug file directory that includes
830 * the executable's full path, and the name from the link (that is,
831 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
832 * is the global debug file directory, and execdir has been turned
833 * into a relative path)." (from GDB manual)
835 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
836 const WCHAR* loaded_file, DWORD crc)
838 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
839 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
840 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
841 size_t filename_len;
842 WCHAR* p = NULL;
843 WCHAR* slash;
844 struct image_file_map* fmap_link = NULL;
846 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
847 if (!fmap_link) return FALSE;
849 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
850 p = HeapAlloc(GetProcessHeap(), 0,
851 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
852 if (!p) goto found;
854 /* we prebuild the string with "execdir" */
855 strcpyW(p, loaded_file);
856 slash = strrchrW(p, '/');
857 if (slash == NULL) slash = p; else slash++;
859 /* testing execdir/filename */
860 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
861 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
863 /* testing execdir/.debug/filename */
864 memcpy(slash, dotDebugW, sizeof(dotDebugW));
865 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
866 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
868 /* testing globaldebugdir/execdir/filename */
869 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
870 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
871 slash += globalDebugDirLen;
872 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
873 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
875 /* finally testing filename */
876 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
879 WARN("Couldn't locate or map %s\n", filename);
880 HeapFree(GetProcessHeap(), 0, p);
881 HeapFree(GetProcessHeap(), 0, fmap_link);
882 return FALSE;
884 found:
885 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
886 HeapFree(GetProcessHeap(), 0, p);
887 fmap->u.elf.alternate = fmap_link;
888 return TRUE;
891 /******************************************************************
892 * elf_debuglink_parse
894 * Parses a .gnu_debuglink section and loads the debug info from
895 * the external file specified there.
897 static BOOL elf_debuglink_parse(struct image_file_map* fmap, const struct module* module,
898 const BYTE* debuglink)
900 /* The content of a debug link section is:
901 * 1/ a NULL terminated string, containing the file name for the
902 * debug info
903 * 2/ padding on 4 byte boundary
904 * 3/ CRC of the linked ELF file
906 const char* dbg_link = (const char*)debuglink;
907 DWORD crc;
909 crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
910 return elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
913 /******************************************************************
914 * elf_load_debug_info_from_map
916 * Loads the symbolic information from ELF module which mapping is described
917 * in fmap
918 * the module has been loaded at 'load_offset' address, so symbols' address
919 * relocation is performed.
920 * CRC is checked if fmap->with_crc is TRUE
921 * returns
922 * 0 if the file doesn't contain symbolic info (or this info cannot be
923 * read or parsed)
924 * 1 on success
926 static BOOL elf_load_debug_info_from_map(struct module* module,
927 struct image_file_map* fmap,
928 struct pool* pool,
929 struct hash_table* ht_symtab)
931 BOOL ret = FALSE, lret;
932 struct elf_thunk_area thunks[] =
934 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
935 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
936 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
937 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
938 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
939 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
940 {NULL, 0, 0, 0}
943 module->module.SymType = SymExport;
945 /* create a hash table for the symtab */
946 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
948 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
950 struct image_section_map stab_sect, stabstr_sect;
951 struct image_section_map debuglink_sect;
953 /* if present, add the .gnu_debuglink file as an alternate to current one */
954 if (elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
956 const BYTE* dbg_link;
958 dbg_link = (const BYTE*)image_map_section(&debuglink_sect);
959 if (dbg_link != IMAGE_NO_MAP)
961 lret = elf_debuglink_parse(fmap, module, dbg_link);
962 if (!lret)
963 WARN("Couldn't load linked debug file for %s\n",
964 debugstr_w(module->module.ModuleName));
965 ret = ret || lret;
967 image_unmap_section(&debuglink_sect);
969 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
970 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
972 const char* stab;
973 const char* stabstr;
975 stab = image_map_section(&stab_sect);
976 stabstr = image_map_section(&stabstr_sect);
977 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
979 /* OK, now just parse all of the stabs. */
980 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
981 stab, image_get_map_size(&stab_sect),
982 stabstr, image_get_map_size(&stabstr_sect),
983 NULL, NULL);
984 if (lret)
985 /* and fill in the missing information for stabs */
986 elf_finish_stabs_info(module, ht_symtab);
987 else
988 WARN("Couldn't correctly read stabs\n");
989 ret = ret || lret;
991 image_unmap_section(&stab_sect);
992 image_unmap_section(&stabstr_sect);
994 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
995 ret = ret || lret;
997 if (strstrW(module->module.ModuleName, S_ElfW) ||
998 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1000 /* add the thunks for native libraries */
1001 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1002 elf_new_wine_thunks(module, ht_symtab, thunks);
1004 /* add all the public symbols from symtab */
1005 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1007 return ret;
1010 /******************************************************************
1011 * elf_load_debug_info
1013 * Loads ELF debugging information from the module image file.
1015 BOOL elf_load_debug_info(struct module* module)
1017 BOOL ret = TRUE;
1018 struct pool pool;
1019 struct hash_table ht_symtab;
1020 struct module_format* modfmt;
1022 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1024 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1025 return FALSE;
1028 pool_init(&pool, 65536);
1029 hash_table_init(&pool, &ht_symtab, 256);
1031 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1033 pool_destroy(&pool);
1034 return ret;
1037 /******************************************************************
1038 * elf_fetch_file_info
1040 * Gathers some more information for an ELF module from a given file
1042 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1043 DWORD* size, DWORD* checksum)
1045 struct image_file_map fmap;
1047 struct elf_map_file_data emfd;
1049 emfd.kind = from_file;
1050 emfd.u.file.filename = name;
1051 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1052 if (base) *base = fmap.u.elf.elf_start;
1053 *size = fmap.u.elf.elf_size;
1054 *checksum = calc_crc32(fmap.u.elf.fd);
1055 elf_unmap_file(&fmap);
1056 return TRUE;
1059 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1060 struct image_file_map* fmap, unsigned long load_offset,
1061 unsigned long dyn_addr, struct elf_info* elf_info)
1063 BOOL ret = FALSE;
1065 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1067 struct image_section_map ism;
1069 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1071 Elf_Dyn dyn;
1072 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1073 unsigned long len;
1077 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1078 len != sizeof(dyn))
1079 return ret;
1080 if (dyn.d_tag == DT_DEBUG)
1082 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1083 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1084 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1085 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1086 break;
1088 ptr += sizeof(dyn);
1089 } while (dyn.d_tag != DT_NULL);
1090 if (dyn.d_tag == DT_NULL) return ret;
1092 elf_end_find(fmap);
1095 if (elf_info->flags & ELF_INFO_MODULE)
1097 struct elf_module_info *elf_module_info;
1098 struct module_format* modfmt;
1099 struct image_section_map ism;
1100 unsigned long modbase = load_offset;
1102 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1104 unsigned long rva_dyn = elf_get_map_rva(&ism);
1106 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1107 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1108 load_offset, dyn_addr);
1109 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1111 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1112 modbase = dyn_addr - rva_dyn;
1114 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1115 elf_end_find(fmap);
1117 modfmt = HeapAlloc(GetProcessHeap(), 0,
1118 sizeof(struct module_format) + sizeof(struct elf_module_info));
1119 if (!modfmt) return FALSE;
1120 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1121 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1122 if (!elf_info->module)
1124 HeapFree(GetProcessHeap(), 0, modfmt);
1125 return FALSE;
1127 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1128 elf_module_info = (void*)(modfmt + 1);
1129 elf_info->module->format_info[DFI_ELF] = modfmt;
1130 modfmt->module = elf_info->module;
1131 modfmt->remove = elf_module_remove;
1132 modfmt->loc_compute = NULL;
1133 modfmt->u.elf_info = elf_module_info;
1135 elf_module_info->elf_addr = load_offset;
1137 elf_module_info->file_map = *fmap;
1138 elf_reset_file_map(fmap);
1139 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1141 elf_info->module->module.SymType = SymDeferred;
1142 ret = TRUE;
1144 else ret = elf_load_debug_info(elf_info->module);
1146 elf_module_info->elf_mark = 1;
1147 elf_module_info->elf_loader = 0;
1148 } else ret = TRUE;
1150 if (elf_info->flags & ELF_INFO_NAME)
1152 WCHAR* ptr;
1153 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1154 if (ptr)
1156 strcpyW(ptr, filename);
1157 elf_info->module_name = ptr;
1159 else ret = FALSE;
1162 return ret;
1165 /******************************************************************
1166 * elf_load_file
1168 * Loads the information for ELF module stored in 'filename'
1169 * the module has been loaded at 'load_offset' address
1170 * returns
1171 * -1 if the file cannot be found/opened
1172 * 0 if the file doesn't contain symbolic info (or this info cannot be
1173 * read or parsed)
1174 * 1 on success
1176 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1177 unsigned long load_offset, unsigned long dyn_addr,
1178 struct elf_info* elf_info)
1180 BOOL ret = FALSE;
1181 struct image_file_map fmap;
1182 struct elf_map_file_data emfd;
1184 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1186 emfd.kind = from_file;
1187 emfd.u.file.filename = filename;
1188 if (!elf_map_file(&emfd, &fmap)) return ret;
1190 /* Next, we need to find a few of the internal ELF headers within
1191 * this thing. We need the main executable header, and the section
1192 * table.
1194 if (!fmap.u.elf.elf_start && !load_offset)
1195 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1196 debugstr_w(filename));
1198 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1200 elf_unmap_file(&fmap);
1202 return ret;
1205 /******************************************************************
1206 * elf_load_file_from_path
1207 * tries to load an ELF file from a set of paths (separated by ':')
1209 static BOOL elf_load_file_from_path(HANDLE hProcess,
1210 const WCHAR* filename,
1211 unsigned long load_offset,
1212 unsigned long dyn_addr,
1213 const char* path,
1214 struct elf_info* elf_info)
1216 BOOL ret = FALSE;
1217 WCHAR *s, *t, *fn;
1218 WCHAR* pathW = NULL;
1219 unsigned len;
1221 if (!path) return FALSE;
1223 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1224 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1225 if (!pathW) return FALSE;
1226 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1228 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1230 t = strchrW(s, ':');
1231 if (t) *t = '\0';
1232 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1233 if (!fn) break;
1234 strcpyW(fn, s);
1235 strcatW(fn, S_SlashW);
1236 strcatW(fn, filename);
1237 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1238 HeapFree(GetProcessHeap(), 0, fn);
1239 if (ret) break;
1242 HeapFree(GetProcessHeap(), 0, pathW);
1243 return ret;
1246 /******************************************************************
1247 * elf_load_file_from_dll_path
1249 * Tries to load an ELF file from the dll path
1251 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1252 const WCHAR* filename,
1253 unsigned long load_offset,
1254 unsigned long dyn_addr,
1255 struct elf_info* elf_info)
1257 BOOL ret = FALSE;
1258 unsigned int index = 0;
1259 const char *path;
1261 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1263 WCHAR *name;
1264 unsigned len;
1266 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1268 name = HeapAlloc( GetProcessHeap(), 0,
1269 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1271 if (!name) break;
1272 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1273 strcatW( name, S_SlashW );
1274 strcatW( name, filename );
1275 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1276 HeapFree( GetProcessHeap(), 0, name );
1278 return ret;
1281 #ifdef AT_SYSINFO_EHDR
1282 /******************************************************************
1283 * elf_search_auxv
1285 * locate some a value from the debuggee auxiliary vector
1287 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1289 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1290 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1291 void* addr;
1292 void* str;
1293 void* str_max;
1294 Elf_auxv_t auxv;
1296 si->SizeOfStruct = sizeof(*si);
1297 si->MaxNameLen = MAX_SYM_NAME;
1298 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1299 !(addr = (void*)(DWORD_PTR)si->Address) ||
1300 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1301 !addr)
1303 FIXME("can't find symbol in module\n");
1304 return FALSE;
1306 /* walk through envp[] */
1307 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1308 str_max = (void*)(DWORD_PTR)~0L;
1309 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1310 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1311 str_max = min(str_max, str);
1313 /* Walk through the end of envp[] array.
1314 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1315 * deleted, the last entry is replaced by an extra NULL.
1317 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1318 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1320 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1322 if (auxv.a_type == type)
1324 *val = auxv.a_un.a_val;
1325 return TRUE;
1327 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1330 return FALSE;
1332 #endif
1334 /******************************************************************
1335 * elf_search_and_load_file
1337 * lookup a file in standard ELF locations, and if found, load it
1339 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1340 unsigned long load_offset, unsigned long dyn_addr,
1341 struct elf_info* elf_info)
1343 BOOL ret = FALSE;
1344 struct module* module;
1345 static WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1347 if (filename == NULL || *filename == '\0') return FALSE;
1348 if ((module = module_is_already_loaded(pcs, filename)))
1350 elf_info->module = module;
1351 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1352 return module->module.SymType;
1355 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1356 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1357 /* if relative pathname, try some absolute base dirs */
1358 if (!ret && !strchrW(filename, '/'))
1360 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1361 getenv("PATH"), elf_info) ||
1362 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1363 getenv("LD_LIBRARY_PATH"), elf_info);
1364 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1365 load_offset, dyn_addr, elf_info);
1368 return ret;
1371 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1372 unsigned long dyn_addr, BOOL is_system, void* user);
1374 /******************************************************************
1375 * elf_enum_modules_internal
1377 * Enumerate ELF modules from a running process
1379 static BOOL elf_enum_modules_internal(const struct process* pcs,
1380 const WCHAR* main_name,
1381 enum_elf_modules_cb cb, void* user)
1383 struct r_debug dbg_hdr;
1384 void* lm_addr;
1385 struct link_map lm;
1386 char bufstr[256];
1387 WCHAR bufstrW[MAX_PATH];
1389 if (!pcs->dbg_hdr_addr ||
1390 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1391 &dbg_hdr, sizeof(dbg_hdr), NULL))
1392 return FALSE;
1394 /* Now walk the linked list. In all known ELF implementations,
1395 * the dynamic loader maintains this linked list for us. In some
1396 * cases the first entry doesn't appear with a name, in other cases it
1397 * does.
1399 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1401 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1402 return FALSE;
1404 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1405 lm.l_name != NULL &&
1406 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1408 bufstr[sizeof(bufstr) - 1] = '\0';
1409 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1410 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1411 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1415 #ifdef AT_SYSINFO_EHDR
1416 if (!lm_addr)
1418 unsigned long ehdr_addr;
1420 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1422 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1423 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1426 #endif
1427 return TRUE;
1430 /******************************************************************
1431 * elf_search_loader
1433 * Lookup in a running ELF process the loader, and sets its ELF link
1434 * address (for accessing the list of loaded .so libs) in pcs.
1435 * If flags is ELF_INFO_MODULE, the module for the loader is also
1436 * added as a module into pcs.
1438 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1440 return elf_search_and_load_file(pcs, get_wine_loader_name(), 0, 0, elf_info);
1443 /******************************************************************
1444 * elf_read_wine_loader_dbg_info
1446 * Try to find a decent wine executable which could have loaded the debuggee
1448 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1450 struct elf_info elf_info;
1452 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1453 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1454 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1455 module_set_module(elf_info.module, S_WineLoaderW);
1456 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1459 struct elf_enum_user
1461 enum_modules_cb cb;
1462 void* user;
1465 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1466 unsigned long dyn_addr, BOOL is_system, void* user)
1468 struct elf_enum_user* eeu = user;
1469 return eeu->cb(name, load_addr, eeu->user);
1472 /******************************************************************
1473 * elf_enum_modules
1475 * Enumerates the ELF loaded modules from a running target (hProc)
1476 * This function doesn't require that someone has called SymInitialize
1477 * on this very process.
1479 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1481 struct process pcs;
1482 struct elf_info elf_info;
1483 BOOL ret;
1484 struct elf_enum_user eeu;
1486 memset(&pcs, 0, sizeof(pcs));
1487 pcs.handle = hProc;
1488 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1489 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1490 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1491 eeu.cb = cb;
1492 eeu.user = user;
1493 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1494 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1495 return ret;
1498 struct elf_load
1500 struct process* pcs;
1501 struct elf_info elf_info;
1502 const WCHAR* name;
1503 BOOL ret;
1506 /******************************************************************
1507 * elf_load_cb
1509 * Callback for elf_load_module, used to walk the list of loaded
1510 * modules.
1512 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1513 unsigned long dyn_addr, BOOL is_system, void* user)
1515 struct elf_load* el = user;
1516 BOOL ret = TRUE;
1517 const WCHAR* p;
1519 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1521 struct module* module;
1522 struct elf_map_file_data emfd;
1523 struct image_file_map fmap;
1525 if ((module = module_is_already_loaded(el->pcs, name)))
1527 el->elf_info.module = module;
1528 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1529 return module->module.SymType;
1532 emfd.kind = from_process;
1533 emfd.u.process.handle = el->pcs->handle;
1534 emfd.u.process.load_addr = (void*)load_addr;
1536 if (elf_map_file(&emfd, &fmap))
1537 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1538 return TRUE;
1540 if (el->name)
1542 /* memcmp is needed for matches when bufstr contains also version information
1543 * el->name: libc.so, name: libc.so.6.0
1545 p = strrchrW(name, '/');
1546 if (!p++) p = name;
1549 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1551 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1552 if (el->name) ret = FALSE;
1555 return ret;
1558 /******************************************************************
1559 * elf_load_module
1561 * loads an ELF module and stores it in process' module list
1562 * Also, find module real name and load address from
1563 * the real loaded modules list in pcs address space
1565 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1567 struct elf_load el;
1569 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1571 el.elf_info.flags = ELF_INFO_MODULE;
1572 el.ret = FALSE;
1574 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1576 el.pcs = pcs;
1577 /* do only the lookup from the filename, not the path (as we lookup module
1578 * name in the process' loaded module list)
1580 el.name = strrchrW(name, '/');
1581 if (!el.name++) el.name = name;
1582 el.ret = FALSE;
1584 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1585 return NULL;
1587 else if (addr)
1589 el.name = name;
1590 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1592 if (!el.ret) return NULL;
1593 assert(el.elf_info.module);
1594 return el.elf_info.module;
1597 /******************************************************************
1598 * elf_synchronize_module_list
1600 * this functions rescans the debuggee module's list and synchronizes it with
1601 * the one from 'pcs', ie:
1602 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1603 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1605 BOOL elf_synchronize_module_list(struct process* pcs)
1607 struct module* module;
1608 struct elf_load el;
1610 for (module = pcs->lmodules; module; module = module->next)
1612 if (module->type == DMT_ELF && !module->is_virtual)
1613 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1616 el.pcs = pcs;
1617 el.elf_info.flags = ELF_INFO_MODULE;
1618 el.ret = FALSE;
1619 el.name = NULL; /* fetch all modules */
1621 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1622 return FALSE;
1624 module = pcs->lmodules;
1625 while (module)
1627 if (module->type == DMT_ELF && !module->is_virtual)
1629 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1631 if (!elf_info->elf_mark && !elf_info->elf_loader)
1633 module_remove(pcs, module);
1634 /* restart all over */
1635 module = pcs->lmodules;
1636 continue;
1639 module = module->next;
1641 return TRUE;
1644 #else /* !__ELF__ */
1646 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1647 unsigned sht, struct image_section_map* ism)
1649 return FALSE;
1652 const char* elf_map_section(struct image_section_map* ism)
1654 return NULL;
1657 void elf_unmap_section(struct image_section_map* ism)
1660 unsigned elf_get_map_size(const struct image_section_map* ism)
1662 return 0;
1665 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1667 return 0;
1670 BOOL elf_synchronize_module_list(struct process* pcs)
1672 return FALSE;
1675 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1676 DWORD* size, DWORD* checksum)
1678 return FALSE;
1681 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1683 return FALSE;
1686 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1688 return FALSE;
1691 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1693 return NULL;
1696 BOOL elf_load_debug_info(struct module* module)
1698 return FALSE;
1701 int elf_is_in_thunk_area(unsigned long addr,
1702 const struct elf_thunk_area* thunks)
1704 return -1;
1706 #endif /* __ELF__ */