dbghelp: Added support for variables in thread storage.
[wine/multimedia.git] / dlls / dbghelp / elf_module.c
blobde61fc10095a719092934face293803959bd5665
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
45 #ifndef PATH_MAX
46 #define PATH_MAX MAX_PATH
47 #endif
49 #include "dbghelp_private.h"
51 #include "image_private.h"
53 #include "wine/library.h"
54 #include "wine/debug.h"
56 #ifdef __ELF__
58 #define ELF_INFO_DEBUG_HEADER 0x0001
59 #define ELF_INFO_MODULE 0x0002
60 #define ELF_INFO_NAME 0x0004
62 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
64 struct elf_info
66 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
67 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
68 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
69 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
72 struct symtab_elt
74 struct hash_table_elt ht_elt;
75 const Elf_Sym* symp;
76 struct symt_compiland* compiland;
77 unsigned used;
80 struct elf_thunk_area
82 const char* symname;
83 THUNK_ORDINAL ordinal;
84 unsigned long rva_start;
85 unsigned long rva_end;
88 struct elf_module_info
90 unsigned long elf_addr;
91 unsigned short elf_mark : 1,
92 elf_loader : 1;
93 struct image_file_map file_map;
96 /******************************************************************
97 * elf_map_section
99 * Maps a single section into memory from an ELF file
101 const char* elf_map_section(struct image_section_map* ism)
103 struct elf_file_map* fmap = &ism->fmap->u.elf;
105 unsigned long pgsz = getpagesize();
106 unsigned long ofst, size;
108 assert(ism->fmap->modtype == DMT_ELF);
109 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
110 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
111 return IMAGE_NO_MAP;
113 if (fmap->target_copy)
115 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
117 /* align required information on page size (we assume pagesize is a power of 2) */
118 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
119 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
120 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
121 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
122 fmap->fd, ofst);
123 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
124 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
127 /******************************************************************
128 * elf_find_section
130 * Finds a section by name (and type) into memory from an ELF file
131 * or its alternate if any
133 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
134 unsigned sht, struct image_section_map* ism)
136 struct elf_file_map* fmap;
137 unsigned i;
139 while (_fmap)
141 fmap = &_fmap->u.elf;
142 if (fmap->shstrtab == IMAGE_NO_MAP)
144 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
145 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
147 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
149 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
150 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
152 ism->fmap = _fmap;
153 ism->sidx = i;
154 return TRUE;
157 _fmap = fmap->alternate;
159 ism->fmap = NULL;
160 ism->sidx = -1;
161 return FALSE;
164 /******************************************************************
165 * elf_unmap_section
167 * Unmaps a single section from memory
169 void elf_unmap_section(struct image_section_map* ism)
171 struct elf_file_map* fmap = &ism->fmap->u.elf;
173 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
174 fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
176 unsigned long pgsz = getpagesize();
177 unsigned long ofst, size;
179 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
180 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
181 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
182 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
183 WARN("Couldn't unmap the section\n");
184 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
188 static void elf_end_find(struct image_file_map* fmap)
190 struct image_section_map ism;
192 while (fmap)
194 ism.fmap = fmap;
195 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
196 elf_unmap_section(&ism);
197 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
198 fmap = fmap->u.elf.alternate;
202 /******************************************************************
203 * elf_get_map_rva
205 * Get the RVA of an ELF section
207 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
209 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
210 return 0;
211 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
214 /******************************************************************
215 * elf_get_map_size
217 * Get the size of an ELF section
219 unsigned elf_get_map_size(const struct image_section_map* ism)
221 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
222 return 0;
223 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
226 static inline void elf_reset_file_map(struct image_file_map* fmap)
228 fmap->u.elf.fd = -1;
229 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
230 fmap->u.elf.alternate = NULL;
231 fmap->u.elf.target_copy = NULL;
234 struct elf_map_file_data
236 enum {from_file, from_process} kind;
237 union
239 struct
241 const WCHAR* filename;
242 } file;
243 struct
245 HANDLE handle;
246 void* load_addr;
247 } process;
248 } u;
251 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
252 void* buf, size_t len, off_t off)
254 SIZE_T dw;
256 switch (emfd->kind)
258 case from_file:
259 return pread(fmap->u.elf.fd, buf, len, off) == len;
260 case from_process:
261 return ReadProcessMemory(emfd->u.process.handle,
262 (void*)((unsigned long)emfd->u.process.load_addr + (unsigned long)off),
263 buf, len, &dw) && dw == len;
264 default:
265 assert(0);
266 return FALSE;
270 /******************************************************************
271 * elf_map_file
273 * Maps an ELF file into memory (and checks it's a real ELF file)
275 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
277 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
278 struct stat statbuf;
279 int i;
280 Elf_Phdr phdr;
281 unsigned long tmp, page_mask = getpagesize() - 1;
282 char* filename;
283 unsigned len;
284 BOOL ret = FALSE;
286 switch (emfd->kind)
288 case from_file:
289 len = WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, NULL, 0, NULL, NULL);
290 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
291 WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, filename, len, NULL, NULL);
292 break;
293 case from_process:
294 filename = NULL;
295 break;
296 default: assert(0);
299 elf_reset_file_map(fmap);
301 fmap->modtype = DMT_ELF;
302 fmap->u.elf.fd = -1;
303 fmap->u.elf.target_copy = NULL;
305 switch (emfd->kind)
307 case from_file:
308 /* check that the file exists, and that the module hasn't been loaded yet */
309 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
311 /* Now open the file, so that we can mmap() it. */
312 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
313 break;
314 case from_process:
315 break;
317 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
318 goto done;
320 /* and check for an ELF header */
321 if (memcmp(fmap->u.elf.elfhdr.e_ident,
322 elf_signature, sizeof(elf_signature))) goto done;
323 /* and check 32 vs 64 size according to current machine */
324 #ifdef _WIN64
325 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
326 #else
327 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
328 #endif
329 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
330 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
331 if (!fmap->u.elf.sect) goto done;
333 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
335 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
336 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
338 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
339 fmap->u.elf.sect = NULL;
340 goto done;
342 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
345 /* grab size of module once loaded in memory */
346 fmap->u.elf.elf_size = 0;
347 fmap->u.elf.elf_start = ~0L;
348 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
350 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
351 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
352 phdr.p_type == PT_LOAD)
354 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
355 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
356 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
359 /* if non relocatable ELF, then remove fixed address from computation
360 * otherwise, all addresses are zero based and start has no effect
362 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
364 switch (emfd->kind)
366 case from_file: break;
367 case from_process:
368 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
370 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
371 goto done;
373 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
374 fmap->u.elf.elf_size, NULL))
376 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
377 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
378 goto done;
380 break;
382 ret = TRUE;
383 done:
384 HeapFree(GetProcessHeap(), 0, filename);
385 return ret;
388 /******************************************************************
389 * elf_unmap_file
391 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
393 static void elf_unmap_file(struct image_file_map* fmap)
395 while (fmap)
397 if (fmap->u.elf.fd != -1)
399 struct image_section_map ism;
400 ism.fmap = fmap;
401 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
403 elf_unmap_section(&ism);
405 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
406 close(fmap->u.elf.fd);
408 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
409 fmap = fmap->u.elf.alternate;
413 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
415 elf_unmap_file(&modfmt->u.elf_info->file_map);
416 HeapFree(GetProcessHeap(), 0, modfmt);
419 /******************************************************************
420 * elf_is_in_thunk_area
422 * Check whether an address lies within one of the thunk area we
423 * know of.
425 int elf_is_in_thunk_area(unsigned long addr,
426 const struct elf_thunk_area* thunks)
428 unsigned i;
430 if (thunks) for (i = 0; thunks[i].symname; i++)
432 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
433 return i;
435 return -1;
438 /******************************************************************
439 * elf_hash_symtab
441 * creating an internal hash table to ease use ELF symtab information lookup
443 static void elf_hash_symtab(struct module* module, struct pool* pool,
444 struct hash_table* ht_symtab, struct image_file_map* fmap,
445 struct elf_thunk_area* thunks)
447 int i, j, nsym;
448 const char* strp;
449 const char* symname;
450 struct symt_compiland* compiland = NULL;
451 const char* ptr;
452 const Elf_Sym* symp;
453 struct symtab_elt* ste;
454 struct image_section_map ism, ism_str;
456 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
457 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
458 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
459 ism_str.fmap = ism.fmap;
460 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
461 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
463 image_unmap_section(&ism);
464 return;
467 nsym = image_get_map_size(&ism) / sizeof(*symp);
469 for (j = 0; thunks[j].symname; j++)
470 thunks[j].rva_start = thunks[j].rva_end = 0;
472 for (i = 0; i < nsym; i++, symp++)
474 /* Ignore certain types of entries which really aren't of that much
475 * interest.
477 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
478 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
479 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
480 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
481 symp->st_shndx == SHN_UNDEF)
483 continue;
486 symname = strp + symp->st_name;
488 /* handle some specific symtab (that we'll throw away when done) */
489 switch (ELF32_ST_TYPE(symp->st_info))
491 case STT_FILE:
492 if (symname)
493 compiland = symt_new_compiland(module, symp->st_value,
494 source_new(module, NULL, symname));
495 else
496 compiland = NULL;
497 continue;
498 case STT_NOTYPE:
499 /* we are only interested in wine markers inserted by winebuild */
500 for (j = 0; thunks[j].symname; j++)
502 if (!strcmp(symname, thunks[j].symname))
504 thunks[j].rva_start = symp->st_value;
505 thunks[j].rva_end = symp->st_value + symp->st_size;
506 break;
509 continue;
512 /* FIXME: we don't need to handle them (GCC internals)
513 * Moreover, they screw up our symbol lookup :-/
515 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
516 continue;
518 ste = pool_alloc(pool, sizeof(*ste));
519 ste->ht_elt.name = symname;
520 /* GCC emits, in some cases, a .<digit>+ suffix.
521 * This is used for static variable inside functions, so
522 * that we can have several such variables with same name in
523 * the same compilation unit
524 * We simply ignore that suffix when present (we also get rid
525 * of it in stabs parsing)
527 ptr = symname + strlen(symname) - 1;
528 if (isdigit(*ptr))
530 while (isdigit(*ptr) && ptr >= symname) ptr--;
531 if (ptr > symname && *ptr == '.')
533 char* n = pool_alloc(pool, ptr - symname + 1);
534 memcpy(n, symname, ptr - symname + 1);
535 n[ptr - symname] = '\0';
536 ste->ht_elt.name = n;
539 ste->symp = symp;
540 ste->compiland = compiland;
541 ste->used = 0;
542 hash_table_add(ht_symtab, &ste->ht_elt);
544 /* as we added in the ht_symtab pointers to the symbols themselves,
545 * we cannot unmap yet the sections, it will be done when we're over
546 * with this ELF file
550 /******************************************************************
551 * elf_lookup_symtab
553 * lookup a symbol by name in our internal hash table for the symtab
555 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
556 const struct hash_table* ht_symtab,
557 const char* name, const struct symt* compiland)
559 struct symtab_elt* weak_result = NULL; /* without compiland name */
560 struct symtab_elt* result = NULL;
561 struct hash_table_iter hti;
562 struct symtab_elt* ste;
563 const char* compiland_name;
564 const char* compiland_basename;
565 const char* base;
567 /* we need weak match up (at least) when symbols of same name,
568 * defined several times in different compilation units,
569 * are merged in a single one (hence a different filename for c.u.)
571 if (compiland)
573 compiland_name = source_get(module,
574 ((const struct symt_compiland*)compiland)->source);
575 compiland_basename = strrchr(compiland_name, '/');
576 if (!compiland_basename++) compiland_basename = compiland_name;
578 else compiland_name = compiland_basename = NULL;
580 hash_table_iter_init(ht_symtab, &hti, name);
581 while ((ste = hash_table_iter_up(&hti)))
583 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
585 weak_result = ste;
586 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
587 continue;
588 if (ste->compiland && compiland_name)
590 const char* filename = source_get(module, ste->compiland->source);
591 if (strcmp(filename, compiland_name))
593 base = strrchr(filename, '/');
594 if (!base++) base = filename;
595 if (strcmp(base, compiland_basename)) continue;
598 if (result)
600 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
601 name, compiland_name,
602 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
603 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
605 else
607 result = ste;
608 ste->used = 1;
611 if (!result && !(result = weak_result))
613 FIXME("Couldn't find symbol %s!%s in symtab\n",
614 debugstr_w(module->module.ModuleName), name);
615 return NULL;
617 return result->symp;
620 /******************************************************************
621 * elf_finish_stabs_info
623 * - get any relevant information (address & size) from the bits we got from the
624 * stabs debugging information
626 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
628 struct hash_table_iter hti;
629 void* ptr;
630 struct symt_ht* sym;
631 const Elf_Sym* symp;
632 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
634 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
635 while ((ptr = hash_table_iter_up(&hti)))
637 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
638 switch (sym->symt.tag)
640 case SymTagFunction:
641 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
642 ((struct symt_function*)sym)->size)
644 break;
646 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
647 ((struct symt_function*)sym)->container);
648 if (symp)
650 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
651 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
652 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
653 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
654 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
655 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
656 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
657 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
658 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
660 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
661 ((struct symt_function*)sym)->size = symp->st_size;
662 } else
663 FIXME("Couldn't find %s!%s\n",
664 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
665 break;
666 case SymTagData:
667 switch (((struct symt_data*)sym)->kind)
669 case DataIsGlobal:
670 case DataIsFileStatic:
671 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
672 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
673 break;
674 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
675 ((struct symt_data*)sym)->container);
676 if (symp)
678 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
679 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
680 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
681 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
682 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
683 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
684 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
685 DataIsFileStatic : DataIsGlobal;
686 } else
687 FIXME("Couldn't find %s!%s\n",
688 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
689 break;
690 default:;
692 break;
693 default:
694 FIXME("Unsupported tag %u\n", sym->symt.tag);
695 break;
698 /* since we may have changed some addresses & sizes, mark the module to be resorted */
699 module->sortlist_valid = FALSE;
702 /******************************************************************
703 * elf_load_wine_thunks
705 * creating the thunk objects for a wine native DLL
707 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
708 const struct elf_thunk_area* thunks)
710 int j;
711 struct hash_table_iter hti;
712 struct symtab_elt* ste;
713 DWORD_PTR addr;
714 struct symt_ht* symt;
716 hash_table_iter_init(ht_symtab, &hti, NULL);
717 while ((ste = hash_table_iter_up(&hti)))
719 if (ste->used) continue;
721 addr = module->reloc_delta + ste->symp->st_value;
723 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
724 if (j >= 0) /* thunk found */
726 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
727 addr, ste->symp->st_size);
729 else
731 ULONG64 ref_addr;
732 struct location loc;
734 symt = symt_find_nearest(module, addr);
735 if (symt && !symt_get_info(module, &symt->symt, TI_GET_ADDRESS, &ref_addr))
736 ref_addr = addr;
737 if (!symt || addr != ref_addr)
739 /* creating public symbols for all the ELF symbols which haven't been
740 * used yet (ie we have no debug information on them)
741 * That's the case, for example, of the .spec.c files
743 switch (ELF32_ST_TYPE(ste->symp->st_info))
745 case STT_FUNC:
746 symt_new_function(module, ste->compiland, ste->ht_elt.name,
747 addr, ste->symp->st_size, NULL);
748 break;
749 case STT_OBJECT:
750 loc.kind = loc_absolute;
751 loc.reg = 0;
752 loc.offset = addr;
753 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
754 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
755 loc, ste->symp->st_size, NULL);
756 break;
757 default:
758 FIXME("Shouldn't happen\n");
759 break;
761 /* FIXME: this is a hack !!!
762 * we are adding new symbols, but as we're parsing a symbol table
763 * (hopefully without duplicate symbols) we delay rebuilding the sorted
764 * module table until we're done with the symbol table
765 * Otherwise, as we intertwine symbols's add and lookup, performance
766 * is rather bad
768 module->sortlist_valid = TRUE;
770 else if (strcmp(ste->ht_elt.name, symt->hash_elt.name))
772 ULONG64 xaddr = 0, xsize = 0;
773 DWORD kind = -1;
775 symt_get_info(module, &symt->symt, TI_GET_ADDRESS, &xaddr);
776 symt_get_info(module, &symt->symt, TI_GET_LENGTH, &xsize);
777 symt_get_info(module, &symt->symt, TI_GET_DATAKIND, &kind);
779 /* If none of symbols has a correct size, we consider they are both markers
780 * Hence, we can silence this warning
781 * Also, we check that we don't have two symbols, one local, the other
782 * global which is legal
784 if ((xsize || ste->symp->st_size) &&
785 (kind == (ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL) ? DataIsFileStatic : DataIsGlobal))
786 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%s-%s>\n",
787 debugstr_w(module->module.ModuleName),
788 ste->ht_elt.name, addr, (unsigned int)ste->symp->st_size,
789 symt->hash_elt.name,
790 wine_dbgstr_longlong(xaddr), wine_dbgstr_longlong(xsize));
794 /* see comment above */
795 module->sortlist_valid = FALSE;
796 return TRUE;
799 /******************************************************************
800 * elf_new_public_symbols
802 * Creates a set of public symbols from an ELF symtab
804 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
806 struct hash_table_iter hti;
807 struct symtab_elt* ste;
809 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
811 /* FIXME: we're missing the ELF entry point here */
813 hash_table_iter_init(symtab, &hti, NULL);
814 while ((ste = hash_table_iter_up(&hti)))
816 symt_new_public(module, ste->compiland, ste->ht_elt.name,
817 module->reloc_delta + ste->symp->st_value,
818 ste->symp->st_size);
820 return TRUE;
823 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
825 BOOL ret;
826 struct elf_map_file_data emfd;
828 emfd.kind = from_file;
829 emfd.u.file.filename = file;
830 if (!elf_map_file(&emfd, fmap)) return FALSE;
831 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
833 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
834 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
835 elf_unmap_file(fmap);
837 return ret;
840 /******************************************************************
841 * elf_locate_debug_link
843 * Locate a filename from a .gnu_debuglink section, using the same
844 * strategy as gdb:
845 * "If the full name of the directory containing the executable is
846 * execdir, and the executable has a debug link that specifies the
847 * name debugfile, then GDB will automatically search for the
848 * debugging information file in three places:
849 * - the directory containing the executable file (that is, it
850 * will look for a file named `execdir/debugfile',
851 * - a subdirectory of that directory named `.debug' (that is, the
852 * file `execdir/.debug/debugfile', and
853 * - a subdirectory of the global debug file directory that includes
854 * the executable's full path, and the name from the link (that is,
855 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
856 * is the global debug file directory, and execdir has been turned
857 * into a relative path)." (from GDB manual)
859 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
860 const WCHAR* loaded_file, DWORD crc)
862 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
863 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
864 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
865 size_t filename_len;
866 WCHAR* p = NULL;
867 WCHAR* slash;
868 struct image_file_map* fmap_link = NULL;
870 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
871 if (!fmap_link) return FALSE;
873 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
874 p = HeapAlloc(GetProcessHeap(), 0,
875 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
876 if (!p) goto found;
878 /* we prebuild the string with "execdir" */
879 strcpyW(p, loaded_file);
880 slash = strrchrW(p, '/');
881 if (slash == NULL) slash = p; else slash++;
883 /* testing execdir/filename */
884 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
885 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
887 /* testing execdir/.debug/filename */
888 memcpy(slash, dotDebugW, sizeof(dotDebugW));
889 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
890 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
892 /* testing globaldebugdir/execdir/filename */
893 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
894 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
895 slash += globalDebugDirLen;
896 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
897 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
899 /* finally testing filename */
900 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
903 WARN("Couldn't locate or map %s\n", filename);
904 HeapFree(GetProcessHeap(), 0, p);
905 HeapFree(GetProcessHeap(), 0, fmap_link);
906 return FALSE;
908 found:
909 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
910 HeapFree(GetProcessHeap(), 0, p);
911 fmap->u.elf.alternate = fmap_link;
912 return TRUE;
915 /******************************************************************
916 * elf_debuglink_parse
918 * Parses a .gnu_debuglink section and loads the debug info from
919 * the external file specified there.
921 static BOOL elf_debuglink_parse(struct image_file_map* fmap, const struct module* module,
922 const BYTE* debuglink)
924 /* The content of a debug link section is:
925 * 1/ a NULL terminated string, containing the file name for the
926 * debug info
927 * 2/ padding on 4 byte boundary
928 * 3/ CRC of the linked ELF file
930 const char* dbg_link = (const char*)debuglink;
931 DWORD crc;
933 crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
934 return elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
937 /******************************************************************
938 * elf_load_debug_info_from_map
940 * Loads the symbolic information from ELF module which mapping is described
941 * in fmap
942 * the module has been loaded at 'load_offset' address, so symbols' address
943 * relocation is performed.
944 * CRC is checked if fmap->with_crc is TRUE
945 * returns
946 * 0 if the file doesn't contain symbolic info (or this info cannot be
947 * read or parsed)
948 * 1 on success
950 static BOOL elf_load_debug_info_from_map(struct module* module,
951 struct image_file_map* fmap,
952 struct pool* pool,
953 struct hash_table* ht_symtab)
955 BOOL ret = FALSE, lret;
956 struct elf_thunk_area thunks[] =
958 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
959 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
960 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
961 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
962 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
963 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
964 {NULL, 0, 0, 0}
967 module->module.SymType = SymExport;
969 /* create a hash table for the symtab */
970 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
972 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
974 struct image_section_map stab_sect, stabstr_sect;
975 struct image_section_map debuglink_sect;
977 /* if present, add the .gnu_debuglink file as an alternate to current one */
978 if (elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
980 const BYTE* dbg_link;
982 dbg_link = (const BYTE*)image_map_section(&debuglink_sect);
983 if (dbg_link != IMAGE_NO_MAP)
985 lret = elf_debuglink_parse(fmap, module, dbg_link);
986 if (!lret)
987 WARN("Couldn't load linked debug file for %s\n",
988 debugstr_w(module->module.ModuleName));
989 ret = ret || lret;
991 image_unmap_section(&debuglink_sect);
993 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
994 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
996 const char* stab;
997 const char* stabstr;
999 stab = image_map_section(&stab_sect);
1000 stabstr = image_map_section(&stabstr_sect);
1001 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
1003 /* OK, now just parse all of the stabs. */
1004 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
1005 stab, image_get_map_size(&stab_sect),
1006 stabstr, image_get_map_size(&stabstr_sect),
1007 NULL, NULL);
1008 if (lret)
1009 /* and fill in the missing information for stabs */
1010 elf_finish_stabs_info(module, ht_symtab);
1011 else
1012 WARN("Couldn't correctly read stabs\n");
1013 ret = ret || lret;
1015 else lret = FALSE;
1016 image_unmap_section(&stab_sect);
1017 image_unmap_section(&stabstr_sect);
1019 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
1020 ret = ret || lret;
1022 if (strstrW(module->module.ModuleName, S_ElfW) ||
1023 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1025 /* add the thunks for native libraries */
1026 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1027 elf_new_wine_thunks(module, ht_symtab, thunks);
1029 /* add all the public symbols from symtab */
1030 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1032 return ret;
1035 /******************************************************************
1036 * elf_load_debug_info
1038 * Loads ELF debugging information from the module image file.
1040 BOOL elf_load_debug_info(struct module* module)
1042 BOOL ret = TRUE;
1043 struct pool pool;
1044 struct hash_table ht_symtab;
1045 struct module_format* modfmt;
1047 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1049 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1050 return FALSE;
1053 pool_init(&pool, 65536);
1054 hash_table_init(&pool, &ht_symtab, 256);
1056 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1058 pool_destroy(&pool);
1059 return ret;
1062 /******************************************************************
1063 * elf_fetch_file_info
1065 * Gathers some more information for an ELF module from a given file
1067 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1068 DWORD* size, DWORD* checksum)
1070 struct image_file_map fmap;
1072 struct elf_map_file_data emfd;
1074 emfd.kind = from_file;
1075 emfd.u.file.filename = name;
1076 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1077 if (base) *base = fmap.u.elf.elf_start;
1078 *size = fmap.u.elf.elf_size;
1079 *checksum = calc_crc32(fmap.u.elf.fd);
1080 elf_unmap_file(&fmap);
1081 return TRUE;
1084 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1085 struct image_file_map* fmap, unsigned long load_offset,
1086 unsigned long dyn_addr, struct elf_info* elf_info)
1088 BOOL ret = FALSE;
1090 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1092 struct image_section_map ism;
1094 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1096 Elf_Dyn dyn;
1097 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1098 unsigned long len;
1102 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1103 len != sizeof(dyn))
1104 return ret;
1105 if (dyn.d_tag == DT_DEBUG)
1107 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1108 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1109 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1110 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1111 break;
1113 ptr += sizeof(dyn);
1114 } while (dyn.d_tag != DT_NULL);
1115 if (dyn.d_tag == DT_NULL) return ret;
1117 elf_end_find(fmap);
1120 if (elf_info->flags & ELF_INFO_MODULE)
1122 struct elf_module_info *elf_module_info;
1123 struct module_format* modfmt;
1124 struct image_section_map ism;
1125 unsigned long modbase = load_offset;
1127 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1129 unsigned long rva_dyn = elf_get_map_rva(&ism);
1131 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1132 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1133 load_offset, dyn_addr);
1134 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1136 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1137 modbase = dyn_addr - rva_dyn;
1139 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1140 elf_end_find(fmap);
1142 modfmt = HeapAlloc(GetProcessHeap(), 0,
1143 sizeof(struct module_format) + sizeof(struct elf_module_info));
1144 if (!modfmt) return FALSE;
1145 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1146 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1147 if (!elf_info->module)
1149 HeapFree(GetProcessHeap(), 0, modfmt);
1150 return FALSE;
1152 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1153 elf_module_info = (void*)(modfmt + 1);
1154 elf_info->module->format_info[DFI_ELF] = modfmt;
1155 modfmt->module = elf_info->module;
1156 modfmt->remove = elf_module_remove;
1157 modfmt->loc_compute = NULL;
1158 modfmt->u.elf_info = elf_module_info;
1160 elf_module_info->elf_addr = load_offset;
1162 elf_module_info->file_map = *fmap;
1163 elf_reset_file_map(fmap);
1164 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1166 elf_info->module->module.SymType = SymDeferred;
1167 ret = TRUE;
1169 else ret = elf_load_debug_info(elf_info->module);
1171 elf_module_info->elf_mark = 1;
1172 elf_module_info->elf_loader = 0;
1173 } else ret = TRUE;
1175 if (elf_info->flags & ELF_INFO_NAME)
1177 WCHAR* ptr;
1178 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1179 if (ptr)
1181 strcpyW(ptr, filename);
1182 elf_info->module_name = ptr;
1184 else ret = FALSE;
1187 return ret;
1190 /******************************************************************
1191 * elf_load_file
1193 * Loads the information for ELF module stored in 'filename'
1194 * the module has been loaded at 'load_offset' address
1195 * returns
1196 * -1 if the file cannot be found/opened
1197 * 0 if the file doesn't contain symbolic info (or this info cannot be
1198 * read or parsed)
1199 * 1 on success
1201 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1202 unsigned long load_offset, unsigned long dyn_addr,
1203 struct elf_info* elf_info)
1205 BOOL ret = FALSE;
1206 struct image_file_map fmap;
1207 struct elf_map_file_data emfd;
1209 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1211 emfd.kind = from_file;
1212 emfd.u.file.filename = filename;
1213 if (!elf_map_file(&emfd, &fmap)) return ret;
1215 /* Next, we need to find a few of the internal ELF headers within
1216 * this thing. We need the main executable header, and the section
1217 * table.
1219 if (!fmap.u.elf.elf_start && !load_offset)
1220 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1221 debugstr_w(filename));
1223 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1225 elf_unmap_file(&fmap);
1227 return ret;
1230 /******************************************************************
1231 * elf_load_file_from_path
1232 * tries to load an ELF file from a set of paths (separated by ':')
1234 static BOOL elf_load_file_from_path(HANDLE hProcess,
1235 const WCHAR* filename,
1236 unsigned long load_offset,
1237 unsigned long dyn_addr,
1238 const char* path,
1239 struct elf_info* elf_info)
1241 BOOL ret = FALSE;
1242 WCHAR *s, *t, *fn;
1243 WCHAR* pathW = NULL;
1244 unsigned len;
1246 if (!path) return FALSE;
1248 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1249 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1250 if (!pathW) return FALSE;
1251 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1253 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1255 t = strchrW(s, ':');
1256 if (t) *t = '\0';
1257 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1258 if (!fn) break;
1259 strcpyW(fn, s);
1260 strcatW(fn, S_SlashW);
1261 strcatW(fn, filename);
1262 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1263 HeapFree(GetProcessHeap(), 0, fn);
1264 if (ret) break;
1265 s = (t) ? (t+1) : NULL;
1268 HeapFree(GetProcessHeap(), 0, pathW);
1269 return ret;
1272 /******************************************************************
1273 * elf_load_file_from_dll_path
1275 * Tries to load an ELF file from the dll path
1277 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1278 const WCHAR* filename,
1279 unsigned long load_offset,
1280 unsigned long dyn_addr,
1281 struct elf_info* elf_info)
1283 BOOL ret = FALSE;
1284 unsigned int index = 0;
1285 const char *path;
1287 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1289 WCHAR *name;
1290 unsigned len;
1292 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1294 name = HeapAlloc( GetProcessHeap(), 0,
1295 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1297 if (!name) break;
1298 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1299 strcatW( name, S_SlashW );
1300 strcatW( name, filename );
1301 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1302 HeapFree( GetProcessHeap(), 0, name );
1304 return ret;
1307 #ifdef AT_SYSINFO_EHDR
1308 /******************************************************************
1309 * elf_search_auxv
1311 * locate some a value from the debuggee auxillary vector
1313 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1315 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1316 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1317 void* addr;
1318 void* str;
1319 void* str_max;
1320 Elf_auxv_t auxv;
1322 si->SizeOfStruct = sizeof(*si);
1323 si->MaxNameLen = MAX_SYM_NAME;
1324 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1325 !(addr = (void*)(DWORD_PTR)si->Address) ||
1326 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1327 !addr)
1329 FIXME("can't find symbol in module\n");
1330 return FALSE;
1332 /* walk through envp[] */
1333 /* envp[] strings are located after the auxillary vector, so protect the walk */
1334 str_max = (void*)(DWORD_PTR)~0L;
1335 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1336 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1337 str_max = min(str_max, str);
1339 /* Walk through the end of envp[] array.
1340 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1341 * deleted, the last entry is replaced by an extra NULL.
1343 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1344 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1346 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1348 if (auxv.a_type == type)
1350 *val = auxv.a_un.a_val;
1351 return TRUE;
1353 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1356 return FALSE;
1358 #endif
1360 /******************************************************************
1361 * elf_search_and_load_file
1363 * lookup a file in standard ELF locations, and if found, load it
1365 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1366 unsigned long load_offset, unsigned long dyn_addr,
1367 struct elf_info* elf_info)
1369 BOOL ret = FALSE;
1370 struct module* module;
1371 static WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1373 if (filename == NULL || *filename == '\0') return FALSE;
1374 if ((module = module_is_already_loaded(pcs, filename)))
1376 elf_info->module = module;
1377 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1378 return module->module.SymType;
1381 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1382 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1383 /* if relative pathname, try some absolute base dirs */
1384 if (!ret && !strchrW(filename, '/'))
1386 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1387 getenv("PATH"), elf_info) ||
1388 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1389 getenv("LD_LIBRARY_PATH"), elf_info);
1390 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1391 load_offset, dyn_addr, elf_info);
1394 return ret;
1397 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1398 unsigned long dyn_addr, BOOL is_system, void* user);
1400 /******************************************************************
1401 * elf_enum_modules_internal
1403 * Enumerate ELF modules from a running process
1405 static BOOL elf_enum_modules_internal(const struct process* pcs,
1406 const WCHAR* main_name,
1407 enum_elf_modules_cb cb, void* user)
1409 struct r_debug dbg_hdr;
1410 void* lm_addr;
1411 struct link_map lm;
1412 char bufstr[256];
1413 WCHAR bufstrW[MAX_PATH];
1415 if (!pcs->dbg_hdr_addr ||
1416 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1417 &dbg_hdr, sizeof(dbg_hdr), NULL))
1418 return FALSE;
1420 /* Now walk the linked list. In all known ELF implementations,
1421 * the dynamic loader maintains this linked list for us. In some
1422 * cases the first entry doesn't appear with a name, in other cases it
1423 * does.
1425 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1427 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1428 return FALSE;
1430 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1431 lm.l_name != NULL &&
1432 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1434 bufstr[sizeof(bufstr) - 1] = '\0';
1435 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1436 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1437 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1441 #ifdef AT_SYSINFO_EHDR
1442 if (!lm_addr)
1444 unsigned long ehdr_addr;
1446 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1448 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1449 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1452 #endif
1453 return TRUE;
1456 /******************************************************************
1457 * elf_search_loader
1459 * Lookup in a running ELF process the loader, and sets its ELF link
1460 * address (for accessing the list of loaded .so libs) in pcs.
1461 * If flags is ELF_INFO_MODULE, the module for the loader is also
1462 * added as a module into pcs.
1464 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1466 return elf_search_and_load_file(pcs, get_wine_loader_name(), 0, 0, elf_info);
1469 /******************************************************************
1470 * elf_read_wine_loader_dbg_info
1472 * Try to find a decent wine executable which could have loaded the debuggee
1474 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1476 struct elf_info elf_info;
1478 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1479 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1480 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1481 module_set_module(elf_info.module, S_WineLoaderW);
1482 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1485 struct elf_enum_user
1487 enum_modules_cb cb;
1488 void* user;
1491 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1492 unsigned long dyn_addr, BOOL is_system, void* user)
1494 struct elf_enum_user* eeu = user;
1495 return eeu->cb(name, load_addr, eeu->user);
1498 /******************************************************************
1499 * elf_enum_modules
1501 * Enumerates the ELF loaded modules from a running target (hProc)
1502 * This function doesn't require that someone has called SymInitialize
1503 * on this very process.
1505 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1507 struct process pcs;
1508 struct elf_info elf_info;
1509 BOOL ret;
1510 struct elf_enum_user eeu;
1512 memset(&pcs, 0, sizeof(pcs));
1513 pcs.handle = hProc;
1514 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1515 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1516 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1517 eeu.cb = cb;
1518 eeu.user = user;
1519 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1520 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1521 return ret;
1524 struct elf_load
1526 struct process* pcs;
1527 struct elf_info elf_info;
1528 const WCHAR* name;
1529 BOOL ret;
1532 /******************************************************************
1533 * elf_load_cb
1535 * Callback for elf_load_module, used to walk the list of loaded
1536 * modules.
1538 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1539 unsigned long dyn_addr, BOOL is_system, void* user)
1541 struct elf_load* el = user;
1542 BOOL ret = TRUE;
1543 const WCHAR* p;
1545 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1547 struct module* module;
1548 struct elf_map_file_data emfd;
1549 struct image_file_map fmap;
1551 if ((module = module_is_already_loaded(el->pcs, name)))
1553 el->elf_info.module = module;
1554 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1555 return module->module.SymType;
1558 emfd.kind = from_process;
1559 emfd.u.process.handle = el->pcs->handle;
1560 emfd.u.process.load_addr = (void*)load_addr;
1562 if (elf_map_file(&emfd, &fmap))
1563 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1564 return TRUE;
1566 if (el->name)
1568 /* memcmp is needed for matches when bufstr contains also version information
1569 * el->name: libc.so, name: libc.so.6.0
1571 p = strrchrW(name, '/');
1572 if (!p++) p = name;
1575 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1577 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1578 if (el->name) ret = FALSE;
1581 return ret;
1584 /******************************************************************
1585 * elf_load_module
1587 * loads an ELF module and stores it in process' module list
1588 * Also, find module real name and load address from
1589 * the real loaded modules list in pcs address space
1591 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1593 struct elf_load el;
1595 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1597 el.elf_info.flags = ELF_INFO_MODULE;
1598 el.ret = FALSE;
1600 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1602 el.pcs = pcs;
1603 /* do only the lookup from the filename, not the path (as we lookup module
1604 * name in the process' loaded module list)
1606 el.name = strrchrW(name, '/');
1607 if (!el.name++) el.name = name;
1608 el.ret = FALSE;
1610 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1611 return NULL;
1613 else if (addr)
1615 el.name = name;
1616 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1618 if (!el.ret) return NULL;
1619 assert(el.elf_info.module);
1620 return el.elf_info.module;
1623 /******************************************************************
1624 * elf_synchronize_module_list
1626 * this functions rescans the debuggee module's list and synchronizes it with
1627 * the one from 'pcs', ie:
1628 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1629 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1631 BOOL elf_synchronize_module_list(struct process* pcs)
1633 struct module* module;
1634 struct elf_load el;
1636 for (module = pcs->lmodules; module; module = module->next)
1638 if (module->type == DMT_ELF && !module->is_virtual)
1639 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1642 el.pcs = pcs;
1643 el.elf_info.flags = ELF_INFO_MODULE;
1644 el.ret = FALSE;
1645 el.name = NULL; /* fetch all modules */
1647 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1648 return FALSE;
1650 module = pcs->lmodules;
1651 while (module)
1653 if (module->type == DMT_ELF && !module->is_virtual)
1655 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1657 if (!elf_info->elf_mark && !elf_info->elf_loader)
1659 module_remove(pcs, module);
1660 /* restart all over */
1661 module = pcs->lmodules;
1662 continue;
1665 module = module->next;
1667 return TRUE;
1670 #else /* !__ELF__ */
1672 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1673 unsigned sht, struct image_section_map* ism)
1675 return FALSE;
1678 const char* elf_map_section(struct image_section_map* ism)
1680 return NULL;
1683 void elf_unmap_section(struct image_section_map* ism)
1686 unsigned elf_get_map_size(const struct image_section_map* ism)
1688 return 0;
1691 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1693 return 0;
1696 BOOL elf_synchronize_module_list(struct process* pcs)
1698 return FALSE;
1701 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1702 DWORD* size, DWORD* checksum)
1704 return FALSE;
1707 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1709 return FALSE;
1712 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1714 return FALSE;
1717 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1719 return NULL;
1722 BOOL elf_load_debug_info(struct module* module)
1724 return FALSE;
1727 int elf_is_in_thunk_area(unsigned long addr,
1728 const struct elf_thunk_area* thunks)
1730 return -1;
1732 #endif /* __ELF__ */