server: Bump priority on server to process messages faster
[wine/multimedia.git] / dlls / dbghelp / elf_module.c
blobe18c9effeb3fc159a227c8ab74a67dee64df556e
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->addr_size = fmap->u.elf.elfhdr.e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
328 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
329 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
330 if (!fmap->u.elf.sect) goto done;
332 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
334 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
335 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
337 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
338 fmap->u.elf.sect = NULL;
339 goto done;
341 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
344 /* grab size of module once loaded in memory */
345 fmap->u.elf.elf_size = 0;
346 fmap->u.elf.elf_start = ~0L;
347 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
349 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
350 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
351 phdr.p_type == PT_LOAD)
353 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
354 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
355 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
358 /* if non relocatable ELF, then remove fixed address from computation
359 * otherwise, all addresses are zero based and start has no effect
361 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
363 switch (emfd->kind)
365 case from_file: break;
366 case from_process:
367 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
369 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
370 goto done;
372 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
373 fmap->u.elf.elf_size, NULL))
375 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
376 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
377 goto done;
379 break;
381 ret = TRUE;
382 done:
383 HeapFree(GetProcessHeap(), 0, filename);
384 return ret;
387 /******************************************************************
388 * elf_unmap_file
390 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
392 static void elf_unmap_file(struct image_file_map* fmap)
394 while (fmap)
396 if (fmap->u.elf.fd != -1)
398 struct image_section_map ism;
399 ism.fmap = fmap;
400 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
402 elf_unmap_section(&ism);
404 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
405 close(fmap->u.elf.fd);
407 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
408 fmap = fmap->u.elf.alternate;
412 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
414 elf_unmap_file(&modfmt->u.elf_info->file_map);
415 HeapFree(GetProcessHeap(), 0, modfmt);
418 /******************************************************************
419 * elf_is_in_thunk_area
421 * Check whether an address lies within one of the thunk area we
422 * know of.
424 int elf_is_in_thunk_area(unsigned long addr,
425 const struct elf_thunk_area* thunks)
427 unsigned i;
429 if (thunks) for (i = 0; thunks[i].symname; i++)
431 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
432 return i;
434 return -1;
437 /******************************************************************
438 * elf_hash_symtab
440 * creating an internal hash table to ease use ELF symtab information lookup
442 static void elf_hash_symtab(struct module* module, struct pool* pool,
443 struct hash_table* ht_symtab, struct image_file_map* fmap,
444 struct elf_thunk_area* thunks)
446 int i, j, nsym;
447 const char* strp;
448 const char* symname;
449 struct symt_compiland* compiland = NULL;
450 const char* ptr;
451 const Elf_Sym* symp;
452 struct symtab_elt* ste;
453 struct image_section_map ism, ism_str;
455 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
456 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
457 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
458 ism_str.fmap = ism.fmap;
459 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
460 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
462 image_unmap_section(&ism);
463 return;
466 nsym = image_get_map_size(&ism) / sizeof(*symp);
468 for (j = 0; thunks[j].symname; j++)
469 thunks[j].rva_start = thunks[j].rva_end = 0;
471 for (i = 0; i < nsym; i++, symp++)
473 /* Ignore certain types of entries which really aren't of that much
474 * interest.
476 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
477 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
478 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
479 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
480 symp->st_shndx == SHN_UNDEF)
482 continue;
485 symname = strp + symp->st_name;
487 /* handle some specific symtab (that we'll throw away when done) */
488 switch (ELF32_ST_TYPE(symp->st_info))
490 case STT_FILE:
491 if (symname)
492 compiland = symt_new_compiland(module, symp->st_value,
493 source_new(module, NULL, symname));
494 else
495 compiland = NULL;
496 continue;
497 case STT_NOTYPE:
498 /* we are only interested in wine markers inserted by winebuild */
499 for (j = 0; thunks[j].symname; j++)
501 if (!strcmp(symname, thunks[j].symname))
503 thunks[j].rva_start = symp->st_value;
504 thunks[j].rva_end = symp->st_value + symp->st_size;
505 break;
508 continue;
511 /* FIXME: we don't need to handle them (GCC internals)
512 * Moreover, they screw up our symbol lookup :-/
514 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
515 continue;
517 ste = pool_alloc(pool, sizeof(*ste));
518 ste->ht_elt.name = symname;
519 /* GCC emits, in some cases, a .<digit>+ suffix.
520 * This is used for static variable inside functions, so
521 * that we can have several such variables with same name in
522 * the same compilation unit
523 * We simply ignore that suffix when present (we also get rid
524 * of it in stabs parsing)
526 ptr = symname + strlen(symname) - 1;
527 if (isdigit(*ptr))
529 while (isdigit(*ptr) && ptr >= symname) ptr--;
530 if (ptr > symname && *ptr == '.')
532 char* n = pool_alloc(pool, ptr - symname + 1);
533 memcpy(n, symname, ptr - symname + 1);
534 n[ptr - symname] = '\0';
535 ste->ht_elt.name = n;
538 ste->symp = symp;
539 ste->compiland = compiland;
540 ste->used = 0;
541 hash_table_add(ht_symtab, &ste->ht_elt);
543 /* as we added in the ht_symtab pointers to the symbols themselves,
544 * we cannot unmap yet the sections, it will be done when we're over
545 * with this ELF file
549 /******************************************************************
550 * elf_lookup_symtab
552 * lookup a symbol by name in our internal hash table for the symtab
554 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
555 const struct hash_table* ht_symtab,
556 const char* name, const struct symt* compiland)
558 struct symtab_elt* weak_result = NULL; /* without compiland name */
559 struct symtab_elt* result = NULL;
560 struct hash_table_iter hti;
561 struct symtab_elt* ste;
562 const char* compiland_name;
563 const char* compiland_basename;
564 const char* base;
566 /* we need weak match up (at least) when symbols of same name,
567 * defined several times in different compilation units,
568 * are merged in a single one (hence a different filename for c.u.)
570 if (compiland)
572 compiland_name = source_get(module,
573 ((const struct symt_compiland*)compiland)->source);
574 compiland_basename = strrchr(compiland_name, '/');
575 if (!compiland_basename++) compiland_basename = compiland_name;
577 else compiland_name = compiland_basename = NULL;
579 hash_table_iter_init(ht_symtab, &hti, name);
580 while ((ste = hash_table_iter_up(&hti)))
582 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
584 weak_result = ste;
585 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
586 continue;
587 if (ste->compiland && compiland_name)
589 const char* filename = source_get(module, ste->compiland->source);
590 if (strcmp(filename, compiland_name))
592 base = strrchr(filename, '/');
593 if (!base++) base = filename;
594 if (strcmp(base, compiland_basename)) continue;
597 if (result)
599 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
600 name, compiland_name,
601 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
602 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
604 else
606 result = ste;
607 ste->used = 1;
610 if (!result && !(result = weak_result))
612 FIXME("Couldn't find symbol %s!%s in symtab\n",
613 debugstr_w(module->module.ModuleName), name);
614 return NULL;
616 return result->symp;
619 /******************************************************************
620 * elf_finish_stabs_info
622 * - get any relevant information (address & size) from the bits we got from the
623 * stabs debugging information
625 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
627 struct hash_table_iter hti;
628 void* ptr;
629 struct symt_ht* sym;
630 const Elf_Sym* symp;
631 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
633 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
634 while ((ptr = hash_table_iter_up(&hti)))
636 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
637 switch (sym->symt.tag)
639 case SymTagFunction:
640 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
641 ((struct symt_function*)sym)->size)
643 break;
645 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
646 ((struct symt_function*)sym)->container);
647 if (symp)
649 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
650 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
651 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
652 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
653 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
654 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
655 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
656 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
657 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
659 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
660 ((struct symt_function*)sym)->size = symp->st_size;
661 } else
662 FIXME("Couldn't find %s!%s\n",
663 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
664 break;
665 case SymTagData:
666 switch (((struct symt_data*)sym)->kind)
668 case DataIsGlobal:
669 case DataIsFileStatic:
670 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
671 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
672 break;
673 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
674 ((struct symt_data*)sym)->container);
675 if (symp)
677 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
678 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
679 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
680 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
681 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
682 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
683 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
684 DataIsFileStatic : DataIsGlobal;
685 } else
686 FIXME("Couldn't find %s!%s\n",
687 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
688 break;
689 default:;
691 break;
692 default:
693 FIXME("Unsupported tag %u\n", sym->symt.tag);
694 break;
697 /* since we may have changed some addresses & sizes, mark the module to be resorted */
698 module->sortlist_valid = FALSE;
701 /******************************************************************
702 * elf_load_wine_thunks
704 * creating the thunk objects for a wine native DLL
706 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
707 const struct elf_thunk_area* thunks)
709 int j;
710 struct hash_table_iter hti;
711 struct symtab_elt* ste;
712 DWORD_PTR addr;
713 struct symt_ht* symt;
715 hash_table_iter_init(ht_symtab, &hti, NULL);
716 while ((ste = hash_table_iter_up(&hti)))
718 if (ste->used) continue;
720 addr = module->reloc_delta + ste->symp->st_value;
722 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
723 if (j >= 0) /* thunk found */
725 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
726 addr, ste->symp->st_size);
728 else
730 ULONG64 ref_addr;
731 struct location loc;
733 symt = symt_find_nearest(module, addr);
734 if (symt && !symt_get_address(&symt->symt, &ref_addr))
735 ref_addr = addr;
736 if (!symt || addr != ref_addr)
738 /* creating public symbols for all the ELF symbols which haven't been
739 * used yet (ie we have no debug information on them)
740 * That's the case, for example, of the .spec.c files
742 switch (ELF32_ST_TYPE(ste->symp->st_info))
744 case STT_FUNC:
745 symt_new_function(module, ste->compiland, ste->ht_elt.name,
746 addr, ste->symp->st_size, NULL);
747 break;
748 case STT_OBJECT:
749 loc.kind = loc_absolute;
750 loc.reg = 0;
751 loc.offset = addr;
752 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
753 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
754 loc, ste->symp->st_size, NULL);
755 break;
756 default:
757 FIXME("Shouldn't happen\n");
758 break;
760 /* FIXME: this is a hack !!!
761 * we are adding new symbols, but as we're parsing a symbol table
762 * (hopefully without duplicate symbols) we delay rebuilding the sorted
763 * module table until we're done with the symbol table
764 * Otherwise, as we intertwine symbols' add and lookup, performance
765 * is rather bad
767 module->sortlist_valid = TRUE;
771 /* see comment above */
772 module->sortlist_valid = FALSE;
773 return TRUE;
776 /******************************************************************
777 * elf_new_public_symbols
779 * Creates a set of public symbols from an ELF symtab
781 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
783 struct hash_table_iter hti;
784 struct symtab_elt* ste;
786 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
788 /* FIXME: we're missing the ELF entry point here */
790 hash_table_iter_init(symtab, &hti, NULL);
791 while ((ste = hash_table_iter_up(&hti)))
793 symt_new_public(module, ste->compiland, ste->ht_elt.name,
794 module->reloc_delta + ste->symp->st_value,
795 ste->symp->st_size);
797 return TRUE;
800 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
802 BOOL ret;
803 struct elf_map_file_data emfd;
805 emfd.kind = from_file;
806 emfd.u.file.filename = file;
807 if (!elf_map_file(&emfd, fmap)) return FALSE;
808 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
810 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
811 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
812 elf_unmap_file(fmap);
814 return ret;
817 /******************************************************************
818 * elf_locate_debug_link
820 * Locate a filename from a .gnu_debuglink section, using the same
821 * strategy as gdb:
822 * "If the full name of the directory containing the executable is
823 * execdir, and the executable has a debug link that specifies the
824 * name debugfile, then GDB will automatically search for the
825 * debugging information file in three places:
826 * - the directory containing the executable file (that is, it
827 * will look for a file named `execdir/debugfile',
828 * - a subdirectory of that directory named `.debug' (that is, the
829 * file `execdir/.debug/debugfile', and
830 * - a subdirectory of the global debug file directory that includes
831 * the executable's full path, and the name from the link (that is,
832 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
833 * is the global debug file directory, and execdir has been turned
834 * into a relative path)." (from GDB manual)
836 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
837 const WCHAR* loaded_file, DWORD crc)
839 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
840 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
841 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
842 size_t filename_len;
843 WCHAR* p = NULL;
844 WCHAR* slash;
845 struct image_file_map* fmap_link = NULL;
847 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
848 if (!fmap_link) return FALSE;
850 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
851 p = HeapAlloc(GetProcessHeap(), 0,
852 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
853 if (!p) goto found;
855 /* we prebuild the string with "execdir" */
856 strcpyW(p, loaded_file);
857 slash = strrchrW(p, '/');
858 if (slash == NULL) slash = p; else slash++;
860 /* testing execdir/filename */
861 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
862 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
864 /* testing execdir/.debug/filename */
865 memcpy(slash, dotDebugW, sizeof(dotDebugW));
866 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
867 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
869 /* testing globaldebugdir/execdir/filename */
870 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
871 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
872 slash += globalDebugDirLen;
873 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
874 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
876 /* finally testing filename */
877 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
880 WARN("Couldn't locate or map %s\n", filename);
881 HeapFree(GetProcessHeap(), 0, p);
882 HeapFree(GetProcessHeap(), 0, fmap_link);
883 return FALSE;
885 found:
886 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
887 HeapFree(GetProcessHeap(), 0, p);
888 fmap->u.elf.alternate = fmap_link;
889 return TRUE;
892 /******************************************************************
893 * elf_debuglink_parse
895 * Parses a .gnu_debuglink section and loads the debug info from
896 * the external file specified there.
898 static BOOL elf_debuglink_parse(struct image_file_map* fmap, const struct module* module,
899 const BYTE* debuglink)
901 /* The content of a debug link section is:
902 * 1/ a NULL terminated string, containing the file name for the
903 * debug info
904 * 2/ padding on 4 byte boundary
905 * 3/ CRC of the linked ELF file
907 const char* dbg_link = (const char*)debuglink;
908 DWORD crc;
910 crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
911 return elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
914 /******************************************************************
915 * elf_load_debug_info_from_map
917 * Loads the symbolic information from ELF module which mapping is described
918 * in fmap
919 * the module has been loaded at 'load_offset' address, so symbols' address
920 * relocation is performed.
921 * CRC is checked if fmap->with_crc is TRUE
922 * returns
923 * 0 if the file doesn't contain symbolic info (or this info cannot be
924 * read or parsed)
925 * 1 on success
927 static BOOL elf_load_debug_info_from_map(struct module* module,
928 struct image_file_map* fmap,
929 struct pool* pool,
930 struct hash_table* ht_symtab)
932 BOOL ret = FALSE, lret;
933 struct elf_thunk_area thunks[] =
935 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
936 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
937 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
938 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
939 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
940 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
941 {NULL, 0, 0, 0}
944 module->module.SymType = SymExport;
946 /* create a hash table for the symtab */
947 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
949 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
951 struct image_section_map stab_sect, stabstr_sect;
952 struct image_section_map debuglink_sect;
954 /* if present, add the .gnu_debuglink file as an alternate to current one */
955 if (elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
957 const BYTE* dbg_link;
959 dbg_link = (const BYTE*)image_map_section(&debuglink_sect);
960 if (dbg_link != IMAGE_NO_MAP)
962 lret = elf_debuglink_parse(fmap, module, dbg_link);
963 if (!lret)
964 WARN("Couldn't load linked debug file for %s\n",
965 debugstr_w(module->module.ModuleName));
966 ret = ret || lret;
968 image_unmap_section(&debuglink_sect);
970 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
971 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
973 const char* stab;
974 const char* stabstr;
976 stab = image_map_section(&stab_sect);
977 stabstr = image_map_section(&stabstr_sect);
978 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
980 /* OK, now just parse all of the stabs. */
981 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
982 stab, image_get_map_size(&stab_sect),
983 stabstr, image_get_map_size(&stabstr_sect),
984 NULL, NULL);
985 if (lret)
986 /* and fill in the missing information for stabs */
987 elf_finish_stabs_info(module, ht_symtab);
988 else
989 WARN("Couldn't correctly read stabs\n");
990 ret = ret || lret;
992 image_unmap_section(&stab_sect);
993 image_unmap_section(&stabstr_sect);
995 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
996 ret = ret || lret;
998 if (strstrW(module->module.ModuleName, S_ElfW) ||
999 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1001 /* add the thunks for native libraries */
1002 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1003 elf_new_wine_thunks(module, ht_symtab, thunks);
1005 /* add all the public symbols from symtab */
1006 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1008 return ret;
1011 /******************************************************************
1012 * elf_load_debug_info
1014 * Loads ELF debugging information from the module image file.
1016 BOOL elf_load_debug_info(struct module* module)
1018 BOOL ret = TRUE;
1019 struct pool pool;
1020 struct hash_table ht_symtab;
1021 struct module_format* modfmt;
1023 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1025 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1026 return FALSE;
1029 pool_init(&pool, 65536);
1030 hash_table_init(&pool, &ht_symtab, 256);
1032 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1034 pool_destroy(&pool);
1035 return ret;
1038 /******************************************************************
1039 * elf_fetch_file_info
1041 * Gathers some more information for an ELF module from a given file
1043 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1044 DWORD* size, DWORD* checksum)
1046 struct image_file_map fmap;
1048 struct elf_map_file_data emfd;
1050 emfd.kind = from_file;
1051 emfd.u.file.filename = name;
1052 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1053 if (base) *base = fmap.u.elf.elf_start;
1054 *size = fmap.u.elf.elf_size;
1055 *checksum = calc_crc32(fmap.u.elf.fd);
1056 elf_unmap_file(&fmap);
1057 return TRUE;
1060 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1061 struct image_file_map* fmap, unsigned long load_offset,
1062 unsigned long dyn_addr, struct elf_info* elf_info)
1064 BOOL ret = FALSE;
1066 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1068 struct image_section_map ism;
1070 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1072 Elf_Dyn dyn;
1073 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1074 unsigned long len;
1078 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1079 len != sizeof(dyn))
1080 return ret;
1081 if (dyn.d_tag == DT_DEBUG)
1083 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1084 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1085 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1086 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1087 break;
1089 ptr += sizeof(dyn);
1090 } while (dyn.d_tag != DT_NULL);
1091 if (dyn.d_tag == DT_NULL) return ret;
1093 elf_end_find(fmap);
1096 if (elf_info->flags & ELF_INFO_MODULE)
1098 struct elf_module_info *elf_module_info;
1099 struct module_format* modfmt;
1100 struct image_section_map ism;
1101 unsigned long modbase = load_offset;
1103 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1105 unsigned long rva_dyn = elf_get_map_rva(&ism);
1107 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1108 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1109 load_offset, dyn_addr);
1110 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1112 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1113 modbase = dyn_addr - rva_dyn;
1115 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1116 elf_end_find(fmap);
1118 modfmt = HeapAlloc(GetProcessHeap(), 0,
1119 sizeof(struct module_format) + sizeof(struct elf_module_info));
1120 if (!modfmt) return FALSE;
1121 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1122 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1123 if (!elf_info->module)
1125 HeapFree(GetProcessHeap(), 0, modfmt);
1126 return FALSE;
1128 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1129 elf_module_info = (void*)(modfmt + 1);
1130 elf_info->module->format_info[DFI_ELF] = modfmt;
1131 modfmt->module = elf_info->module;
1132 modfmt->remove = elf_module_remove;
1133 modfmt->loc_compute = NULL;
1134 modfmt->u.elf_info = elf_module_info;
1136 elf_module_info->elf_addr = load_offset;
1138 elf_module_info->file_map = *fmap;
1139 elf_reset_file_map(fmap);
1140 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1142 elf_info->module->module.SymType = SymDeferred;
1143 ret = TRUE;
1145 else ret = elf_load_debug_info(elf_info->module);
1147 elf_module_info->elf_mark = 1;
1148 elf_module_info->elf_loader = 0;
1149 } else ret = TRUE;
1151 if (elf_info->flags & ELF_INFO_NAME)
1153 WCHAR* ptr;
1154 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1155 if (ptr)
1157 strcpyW(ptr, filename);
1158 elf_info->module_name = ptr;
1160 else ret = FALSE;
1163 return ret;
1166 /******************************************************************
1167 * elf_load_file
1169 * Loads the information for ELF module stored in 'filename'
1170 * the module has been loaded at 'load_offset' address
1171 * returns
1172 * -1 if the file cannot be found/opened
1173 * 0 if the file doesn't contain symbolic info (or this info cannot be
1174 * read or parsed)
1175 * 1 on success
1177 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1178 unsigned long load_offset, unsigned long dyn_addr,
1179 struct elf_info* elf_info)
1181 BOOL ret = FALSE;
1182 struct image_file_map fmap;
1183 struct elf_map_file_data emfd;
1185 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1187 emfd.kind = from_file;
1188 emfd.u.file.filename = filename;
1189 if (!elf_map_file(&emfd, &fmap)) return ret;
1191 /* Next, we need to find a few of the internal ELF headers within
1192 * this thing. We need the main executable header, and the section
1193 * table.
1195 if (!fmap.u.elf.elf_start && !load_offset)
1196 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1197 debugstr_w(filename));
1199 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1201 elf_unmap_file(&fmap);
1203 return ret;
1206 /******************************************************************
1207 * elf_load_file_from_path
1208 * tries to load an ELF file from a set of paths (separated by ':')
1210 static BOOL elf_load_file_from_path(HANDLE hProcess,
1211 const WCHAR* filename,
1212 unsigned long load_offset,
1213 unsigned long dyn_addr,
1214 const char* path,
1215 struct elf_info* elf_info)
1217 BOOL ret = FALSE;
1218 WCHAR *s, *t, *fn;
1219 WCHAR* pathW = NULL;
1220 unsigned len;
1222 if (!path) return FALSE;
1224 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1225 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1226 if (!pathW) return FALSE;
1227 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1229 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1231 t = strchrW(s, ':');
1232 if (t) *t = '\0';
1233 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1234 if (!fn) break;
1235 strcpyW(fn, s);
1236 strcatW(fn, S_SlashW);
1237 strcatW(fn, filename);
1238 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1239 HeapFree(GetProcessHeap(), 0, fn);
1240 if (ret) break;
1243 HeapFree(GetProcessHeap(), 0, pathW);
1244 return ret;
1247 /******************************************************************
1248 * elf_load_file_from_dll_path
1250 * Tries to load an ELF file from the dll path
1252 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1253 const WCHAR* filename,
1254 unsigned long load_offset,
1255 unsigned long dyn_addr,
1256 struct elf_info* elf_info)
1258 BOOL ret = FALSE;
1259 unsigned int index = 0;
1260 const char *path;
1262 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1264 WCHAR *name;
1265 unsigned len;
1267 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1269 name = HeapAlloc( GetProcessHeap(), 0,
1270 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1272 if (!name) break;
1273 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1274 strcatW( name, S_SlashW );
1275 strcatW( name, filename );
1276 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1277 HeapFree( GetProcessHeap(), 0, name );
1279 return ret;
1282 #ifdef AT_SYSINFO_EHDR
1283 /******************************************************************
1284 * elf_search_auxv
1286 * locate some a value from the debuggee auxiliary vector
1288 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1290 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1291 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1292 void* addr;
1293 void* str;
1294 void* str_max;
1295 Elf_auxv_t auxv;
1297 si->SizeOfStruct = sizeof(*si);
1298 si->MaxNameLen = MAX_SYM_NAME;
1299 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1300 !(addr = (void*)(DWORD_PTR)si->Address) ||
1301 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1302 !addr)
1304 FIXME("can't find symbol in module\n");
1305 return FALSE;
1307 /* walk through envp[] */
1308 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1309 str_max = (void*)(DWORD_PTR)~0L;
1310 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1311 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1312 str_max = min(str_max, str);
1314 /* Walk through the end of envp[] array.
1315 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1316 * deleted, the last entry is replaced by an extra NULL.
1318 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1319 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1321 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1323 if (auxv.a_type == type)
1325 *val = auxv.a_un.a_val;
1326 return TRUE;
1328 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1331 return FALSE;
1333 #endif
1335 /******************************************************************
1336 * elf_search_and_load_file
1338 * lookup a file in standard ELF locations, and if found, load it
1340 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1341 unsigned long load_offset, unsigned long dyn_addr,
1342 struct elf_info* elf_info)
1344 BOOL ret = FALSE;
1345 struct module* module;
1346 static WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1348 if (filename == NULL || *filename == '\0') return FALSE;
1349 if ((module = module_is_already_loaded(pcs, filename)))
1351 elf_info->module = module;
1352 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1353 return module->module.SymType;
1356 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1357 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1358 /* if relative pathname, try some absolute base dirs */
1359 if (!ret && !strchrW(filename, '/'))
1361 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1362 getenv("PATH"), elf_info) ||
1363 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1364 getenv("LD_LIBRARY_PATH"), elf_info);
1365 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1366 load_offset, dyn_addr, elf_info);
1369 return ret;
1372 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1373 unsigned long dyn_addr, BOOL is_system, void* user);
1375 /******************************************************************
1376 * elf_enum_modules_internal
1378 * Enumerate ELF modules from a running process
1380 static BOOL elf_enum_modules_internal(const struct process* pcs,
1381 const WCHAR* main_name,
1382 enum_elf_modules_cb cb, void* user)
1384 struct r_debug dbg_hdr;
1385 void* lm_addr;
1386 struct link_map lm;
1387 char bufstr[256];
1388 WCHAR bufstrW[MAX_PATH];
1390 if (!pcs->dbg_hdr_addr ||
1391 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1392 &dbg_hdr, sizeof(dbg_hdr), NULL))
1393 return FALSE;
1395 /* Now walk the linked list. In all known ELF implementations,
1396 * the dynamic loader maintains this linked list for us. In some
1397 * cases the first entry doesn't appear with a name, in other cases it
1398 * does.
1400 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1402 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1403 return FALSE;
1405 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1406 lm.l_name != NULL &&
1407 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1409 bufstr[sizeof(bufstr) - 1] = '\0';
1410 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1411 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1412 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1416 #ifdef AT_SYSINFO_EHDR
1417 if (!lm_addr)
1419 unsigned long ehdr_addr;
1421 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1423 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1424 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1427 #endif
1428 return TRUE;
1431 /******************************************************************
1432 * elf_search_loader
1434 * Lookup in a running ELF process the loader, and sets its ELF link
1435 * address (for accessing the list of loaded .so libs) in pcs.
1436 * If flags is ELF_INFO_MODULE, the module for the loader is also
1437 * added as a module into pcs.
1439 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1441 return elf_search_and_load_file(pcs, get_wine_loader_name(), 0, 0, elf_info);
1444 /******************************************************************
1445 * elf_read_wine_loader_dbg_info
1447 * Try to find a decent wine executable which could have loaded the debuggee
1449 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1451 struct elf_info elf_info;
1453 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1454 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1455 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1456 module_set_module(elf_info.module, S_WineLoaderW);
1457 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1460 struct elf_enum_user
1462 enum_modules_cb cb;
1463 void* user;
1466 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1467 unsigned long dyn_addr, BOOL is_system, void* user)
1469 struct elf_enum_user* eeu = user;
1470 return eeu->cb(name, load_addr, eeu->user);
1473 /******************************************************************
1474 * elf_enum_modules
1476 * Enumerates the ELF loaded modules from a running target (hProc)
1477 * This function doesn't require that someone has called SymInitialize
1478 * on this very process.
1480 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1482 struct process pcs;
1483 struct elf_info elf_info;
1484 BOOL ret;
1485 struct elf_enum_user eeu;
1487 memset(&pcs, 0, sizeof(pcs));
1488 pcs.handle = hProc;
1489 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1490 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1491 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1492 eeu.cb = cb;
1493 eeu.user = user;
1494 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1495 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1496 return ret;
1499 struct elf_load
1501 struct process* pcs;
1502 struct elf_info elf_info;
1503 const WCHAR* name;
1504 BOOL ret;
1507 /******************************************************************
1508 * elf_load_cb
1510 * Callback for elf_load_module, used to walk the list of loaded
1511 * modules.
1513 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1514 unsigned long dyn_addr, BOOL is_system, void* user)
1516 struct elf_load* el = user;
1517 BOOL ret = TRUE;
1518 const WCHAR* p;
1520 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1522 struct module* module;
1523 struct elf_map_file_data emfd;
1524 struct image_file_map fmap;
1526 if ((module = module_is_already_loaded(el->pcs, name)))
1528 el->elf_info.module = module;
1529 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1530 return module->module.SymType;
1533 emfd.kind = from_process;
1534 emfd.u.process.handle = el->pcs->handle;
1535 emfd.u.process.load_addr = (void*)load_addr;
1537 if (elf_map_file(&emfd, &fmap))
1538 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1539 return TRUE;
1541 if (el->name)
1543 /* memcmp is needed for matches when bufstr contains also version information
1544 * el->name: libc.so, name: libc.so.6.0
1546 p = strrchrW(name, '/');
1547 if (!p++) p = name;
1550 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1552 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1553 if (el->name) ret = FALSE;
1556 return ret;
1559 /******************************************************************
1560 * elf_load_module
1562 * loads an ELF module and stores it in process' module list
1563 * Also, find module real name and load address from
1564 * the real loaded modules list in pcs address space
1566 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1568 struct elf_load el;
1570 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1572 el.elf_info.flags = ELF_INFO_MODULE;
1573 el.ret = FALSE;
1575 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1577 el.pcs = pcs;
1578 /* do only the lookup from the filename, not the path (as we lookup module
1579 * name in the process' loaded module list)
1581 el.name = strrchrW(name, '/');
1582 if (!el.name++) el.name = name;
1583 el.ret = FALSE;
1585 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1586 return NULL;
1588 else if (addr)
1590 el.name = name;
1591 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1593 if (!el.ret) return NULL;
1594 assert(el.elf_info.module);
1595 return el.elf_info.module;
1598 /******************************************************************
1599 * elf_synchronize_module_list
1601 * this functions rescans the debuggee module's list and synchronizes it with
1602 * the one from 'pcs', ie:
1603 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1604 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1606 BOOL elf_synchronize_module_list(struct process* pcs)
1608 struct module* module;
1609 struct elf_load el;
1611 for (module = pcs->lmodules; module; module = module->next)
1613 if (module->type == DMT_ELF && !module->is_virtual)
1614 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1617 el.pcs = pcs;
1618 el.elf_info.flags = ELF_INFO_MODULE;
1619 el.ret = FALSE;
1620 el.name = NULL; /* fetch all modules */
1622 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1623 return FALSE;
1625 module = pcs->lmodules;
1626 while (module)
1628 if (module->type == DMT_ELF && !module->is_virtual)
1630 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1632 if (!elf_info->elf_mark && !elf_info->elf_loader)
1634 module_remove(pcs, module);
1635 /* restart all over */
1636 module = pcs->lmodules;
1637 continue;
1640 module = module->next;
1642 return TRUE;
1645 #else /* !__ELF__ */
1647 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1648 unsigned sht, struct image_section_map* ism)
1650 return FALSE;
1653 const char* elf_map_section(struct image_section_map* ism)
1655 return NULL;
1658 void elf_unmap_section(struct image_section_map* ism)
1661 unsigned elf_get_map_size(const struct image_section_map* ism)
1663 return 0;
1666 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1668 return 0;
1671 BOOL elf_synchronize_module_list(struct process* pcs)
1673 return FALSE;
1676 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1677 DWORD* size, DWORD* checksum)
1679 return FALSE;
1682 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1684 return FALSE;
1687 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1689 return FALSE;
1692 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1694 return NULL;
1697 BOOL elf_load_debug_info(struct module* module)
1699 return FALSE;
1702 int elf_is_in_thunk_area(unsigned long addr,
1703 const struct elf_thunk_area* thunks)
1705 return -1;
1707 #endif /* __ELF__ */