kernel32: Add more tracing to GetDiskFreeSpaceW.
[wine/multimedia.git] / dlls / dbghelp / elf_module.c
blob52060e08c9b4de8ed2ceaf716d82d86c8a51a739
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 #ifndef NT_GNU_BUILD_ID
60 #define NT_GNU_BUILD_ID 3
61 #endif
63 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
65 struct elf_info
67 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
68 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
69 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
70 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
73 struct symtab_elt
75 struct hash_table_elt ht_elt;
76 const Elf_Sym* symp;
77 struct symt_compiland* compiland;
78 unsigned used;
81 struct elf_thunk_area
83 const char* symname;
84 THUNK_ORDINAL ordinal;
85 unsigned long rva_start;
86 unsigned long rva_end;
89 struct elf_module_info
91 unsigned long elf_addr;
92 unsigned short elf_mark : 1,
93 elf_loader : 1;
94 struct image_file_map file_map;
97 /******************************************************************
98 * elf_map_section
100 * Maps a single section into memory from an ELF file
102 const char* elf_map_section(struct image_section_map* ism)
104 struct elf_file_map* fmap = &ism->fmap->u.elf;
106 unsigned long pgsz = getpagesize();
107 unsigned long ofst, size;
109 assert(ism->fmap->modtype == DMT_ELF);
110 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
111 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
112 return IMAGE_NO_MAP;
114 if (fmap->target_copy)
116 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
118 /* align required information on page size (we assume pagesize is a power of 2) */
119 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
120 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
121 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
122 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
123 fmap->fd, ofst);
124 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
125 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
128 /******************************************************************
129 * elf_find_section
131 * Finds a section by name (and type) into memory from an ELF file
132 * or its alternate if any
134 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
135 unsigned sht, struct image_section_map* ism)
137 struct elf_file_map* fmap;
138 unsigned i;
140 while (_fmap)
142 fmap = &_fmap->u.elf;
143 if (fmap->shstrtab == IMAGE_NO_MAP)
145 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
146 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
148 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
150 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
151 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
153 ism->fmap = _fmap;
154 ism->sidx = i;
155 return TRUE;
158 _fmap = fmap->alternate;
160 ism->fmap = NULL;
161 ism->sidx = -1;
162 return FALSE;
165 /******************************************************************
166 * elf_unmap_section
168 * Unmaps a single section from memory
170 void elf_unmap_section(struct image_section_map* ism)
172 struct elf_file_map* fmap = &ism->fmap->u.elf;
174 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
175 fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
177 unsigned long pgsz = getpagesize();
178 unsigned long ofst, size;
180 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
181 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
182 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
183 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
184 WARN("Couldn't unmap the section\n");
185 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
189 static void elf_end_find(struct image_file_map* fmap)
191 struct image_section_map ism;
193 while (fmap)
195 ism.fmap = fmap;
196 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
197 elf_unmap_section(&ism);
198 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
199 fmap = fmap->u.elf.alternate;
203 /******************************************************************
204 * elf_get_map_rva
206 * Get the RVA of an ELF section
208 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
210 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
211 return 0;
212 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
215 /******************************************************************
216 * elf_get_map_size
218 * Get the size of an ELF section
220 unsigned elf_get_map_size(const struct image_section_map* ism)
222 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
223 return 0;
224 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
227 static inline void elf_reset_file_map(struct image_file_map* fmap)
229 fmap->u.elf.fd = -1;
230 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
231 fmap->u.elf.alternate = NULL;
232 fmap->u.elf.target_copy = NULL;
235 struct elf_map_file_data
237 enum {from_file, from_process} kind;
238 union
240 struct
242 const WCHAR* filename;
243 } file;
244 struct
246 HANDLE handle;
247 void* load_addr;
248 } process;
249 } u;
252 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
253 void* buf, size_t len, off_t off)
255 SIZE_T dw;
257 switch (emfd->kind)
259 case from_file:
260 return pread(fmap->u.elf.fd, buf, len, off) == len;
261 case from_process:
262 return ReadProcessMemory(emfd->u.process.handle,
263 (void*)((unsigned long)emfd->u.process.load_addr + (unsigned long)off),
264 buf, len, &dw) && dw == len;
265 default:
266 assert(0);
267 return FALSE;
271 /******************************************************************
272 * elf_map_file
274 * Maps an ELF file into memory (and checks it's a real ELF file)
276 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
278 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
279 struct stat statbuf;
280 int i;
281 Elf_Phdr phdr;
282 unsigned long tmp, page_mask = getpagesize() - 1;
283 char* filename;
284 unsigned len;
285 BOOL ret = FALSE;
287 switch (emfd->kind)
289 case from_file:
290 len = WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, NULL, 0, NULL, NULL);
291 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
292 WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, filename, len, NULL, NULL);
293 break;
294 case from_process:
295 filename = NULL;
296 break;
297 default: assert(0);
298 return FALSE;
301 elf_reset_file_map(fmap);
303 fmap->modtype = DMT_ELF;
304 fmap->u.elf.fd = -1;
305 fmap->u.elf.target_copy = NULL;
307 switch (emfd->kind)
309 case from_file:
310 /* check that the file exists, and that the module hasn't been loaded yet */
311 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
313 /* Now open the file, so that we can mmap() it. */
314 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
315 break;
316 case from_process:
317 break;
319 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
320 goto done;
322 /* and check for an ELF header */
323 if (memcmp(fmap->u.elf.elfhdr.e_ident,
324 elf_signature, sizeof(elf_signature))) goto done;
325 /* and check 32 vs 64 size according to current machine */
326 #ifdef _WIN64
327 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
328 #else
329 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
330 #endif
331 fmap->addr_size = fmap->u.elf.elfhdr.e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
332 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
333 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
334 if (!fmap->u.elf.sect) goto done;
336 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
338 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
339 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
341 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
342 fmap->u.elf.sect = NULL;
343 goto done;
345 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
348 /* grab size of module once loaded in memory */
349 fmap->u.elf.elf_size = 0;
350 fmap->u.elf.elf_start = ~0L;
351 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
353 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
354 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
355 phdr.p_type == PT_LOAD)
357 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
358 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
359 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
362 /* if non relocatable ELF, then remove fixed address from computation
363 * otherwise, all addresses are zero based and start has no effect
365 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
367 switch (emfd->kind)
369 case from_file: break;
370 case from_process:
371 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
373 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
374 goto done;
376 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
377 fmap->u.elf.elf_size, NULL))
379 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
380 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
381 goto done;
383 break;
385 ret = TRUE;
386 done:
387 HeapFree(GetProcessHeap(), 0, filename);
388 return ret;
391 /******************************************************************
392 * elf_unmap_file
394 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
396 static void elf_unmap_file(struct image_file_map* fmap)
398 while (fmap)
400 if (fmap->u.elf.fd != -1)
402 struct image_section_map ism;
403 ism.fmap = fmap;
404 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
406 elf_unmap_section(&ism);
408 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
409 close(fmap->u.elf.fd);
411 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
412 fmap = fmap->u.elf.alternate;
416 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
418 elf_unmap_file(&modfmt->u.elf_info->file_map);
419 HeapFree(GetProcessHeap(), 0, modfmt);
422 /******************************************************************
423 * elf_is_in_thunk_area
425 * Check whether an address lies within one of the thunk area we
426 * know of.
428 int elf_is_in_thunk_area(unsigned long addr,
429 const struct elf_thunk_area* thunks)
431 unsigned i;
433 if (thunks) for (i = 0; thunks[i].symname; i++)
435 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
436 return i;
438 return -1;
441 /******************************************************************
442 * elf_hash_symtab
444 * creating an internal hash table to ease use ELF symtab information lookup
446 static void elf_hash_symtab(struct module* module, struct pool* pool,
447 struct hash_table* ht_symtab, struct image_file_map* fmap,
448 struct elf_thunk_area* thunks)
450 int i, j, nsym;
451 const char* strp;
452 const char* symname;
453 struct symt_compiland* compiland = NULL;
454 const char* ptr;
455 const Elf_Sym* symp;
456 struct symtab_elt* ste;
457 struct image_section_map ism, ism_str;
459 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
460 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
461 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
462 ism_str.fmap = ism.fmap;
463 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
464 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
466 image_unmap_section(&ism);
467 return;
470 nsym = image_get_map_size(&ism) / sizeof(*symp);
472 for (j = 0; thunks[j].symname; j++)
473 thunks[j].rva_start = thunks[j].rva_end = 0;
475 for (i = 0; i < nsym; i++, symp++)
477 /* Ignore certain types of entries which really aren't of that much
478 * interest.
480 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
481 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
482 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
483 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
484 symp->st_shndx == SHN_UNDEF)
486 continue;
489 symname = strp + symp->st_name;
491 /* handle some specific symtab (that we'll throw away when done) */
492 switch (ELF32_ST_TYPE(symp->st_info))
494 case STT_FILE:
495 if (symname)
496 compiland = symt_new_compiland(module, symp->st_value,
497 source_new(module, NULL, symname));
498 else
499 compiland = NULL;
500 continue;
501 case STT_NOTYPE:
502 /* we are only interested in wine markers inserted by winebuild */
503 for (j = 0; thunks[j].symname; j++)
505 if (!strcmp(symname, thunks[j].symname))
507 thunks[j].rva_start = symp->st_value;
508 thunks[j].rva_end = symp->st_value + symp->st_size;
509 break;
512 continue;
515 /* FIXME: we don't need to handle them (GCC internals)
516 * Moreover, they screw up our symbol lookup :-/
518 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
519 continue;
521 ste = pool_alloc(pool, sizeof(*ste));
522 ste->ht_elt.name = symname;
523 /* GCC emits, in some cases, a .<digit>+ suffix.
524 * This is used for static variable inside functions, so
525 * that we can have several such variables with same name in
526 * the same compilation unit
527 * We simply ignore that suffix when present (we also get rid
528 * of it in stabs parsing)
530 ptr = symname + strlen(symname) - 1;
531 if (isdigit(*ptr))
533 while (isdigit(*ptr) && ptr >= symname) ptr--;
534 if (ptr > symname && *ptr == '.')
536 char* n = pool_alloc(pool, ptr - symname + 1);
537 memcpy(n, symname, ptr - symname + 1);
538 n[ptr - symname] = '\0';
539 ste->ht_elt.name = n;
542 ste->symp = symp;
543 ste->compiland = compiland;
544 ste->used = 0;
545 hash_table_add(ht_symtab, &ste->ht_elt);
547 /* as we added in the ht_symtab pointers to the symbols themselves,
548 * we cannot unmap yet the sections, it will be done when we're over
549 * with this ELF file
553 /******************************************************************
554 * elf_lookup_symtab
556 * lookup a symbol by name in our internal hash table for the symtab
558 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
559 const struct hash_table* ht_symtab,
560 const char* name, const struct symt* compiland)
562 struct symtab_elt* weak_result = NULL; /* without compiland name */
563 struct symtab_elt* result = NULL;
564 struct hash_table_iter hti;
565 struct symtab_elt* ste;
566 const char* compiland_name;
567 const char* compiland_basename;
568 const char* base;
570 /* we need weak match up (at least) when symbols of same name,
571 * defined several times in different compilation units,
572 * are merged in a single one (hence a different filename for c.u.)
574 if (compiland)
576 compiland_name = source_get(module,
577 ((const struct symt_compiland*)compiland)->source);
578 compiland_basename = strrchr(compiland_name, '/');
579 if (!compiland_basename++) compiland_basename = compiland_name;
581 else compiland_name = compiland_basename = NULL;
583 hash_table_iter_init(ht_symtab, &hti, name);
584 while ((ste = hash_table_iter_up(&hti)))
586 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
588 weak_result = ste;
589 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
590 continue;
591 if (ste->compiland && compiland_name)
593 const char* filename = source_get(module, ste->compiland->source);
594 if (strcmp(filename, compiland_name))
596 base = strrchr(filename, '/');
597 if (!base++) base = filename;
598 if (strcmp(base, compiland_basename)) continue;
601 if (result)
603 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
604 name, compiland_name,
605 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
606 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
608 else
610 result = ste;
611 ste->used = 1;
614 if (!result && !(result = weak_result))
616 FIXME("Couldn't find symbol %s!%s in symtab\n",
617 debugstr_w(module->module.ModuleName), name);
618 return NULL;
620 return result->symp;
623 /******************************************************************
624 * elf_finish_stabs_info
626 * - get any relevant information (address & size) from the bits we got from the
627 * stabs debugging information
629 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
631 struct hash_table_iter hti;
632 void* ptr;
633 struct symt_ht* sym;
634 const Elf_Sym* symp;
635 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
637 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
638 while ((ptr = hash_table_iter_up(&hti)))
640 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
641 switch (sym->symt.tag)
643 case SymTagFunction:
644 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
645 ((struct symt_function*)sym)->size)
647 break;
649 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
650 ((struct symt_function*)sym)->container);
651 if (symp)
653 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
654 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
655 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
656 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
657 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
658 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
659 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
660 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
661 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
663 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
664 ((struct symt_function*)sym)->size = symp->st_size;
665 } else
666 FIXME("Couldn't find %s!%s\n",
667 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
668 break;
669 case SymTagData:
670 switch (((struct symt_data*)sym)->kind)
672 case DataIsGlobal:
673 case DataIsFileStatic:
674 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
675 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
676 break;
677 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
678 ((struct symt_data*)sym)->container);
679 if (symp)
681 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
682 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
683 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
684 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
685 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
686 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
687 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
688 DataIsFileStatic : DataIsGlobal;
689 } else
690 FIXME("Couldn't find %s!%s\n",
691 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
692 break;
693 default:;
695 break;
696 default:
697 FIXME("Unsupported tag %u\n", sym->symt.tag);
698 break;
701 /* since we may have changed some addresses & sizes, mark the module to be resorted */
702 module->sortlist_valid = FALSE;
705 /******************************************************************
706 * elf_load_wine_thunks
708 * creating the thunk objects for a wine native DLL
710 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
711 const struct elf_thunk_area* thunks)
713 int j;
714 struct hash_table_iter hti;
715 struct symtab_elt* ste;
716 DWORD_PTR addr;
717 struct symt_ht* symt;
719 hash_table_iter_init(ht_symtab, &hti, NULL);
720 while ((ste = hash_table_iter_up(&hti)))
722 if (ste->used) continue;
724 addr = module->reloc_delta + ste->symp->st_value;
726 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
727 if (j >= 0) /* thunk found */
729 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
730 addr, ste->symp->st_size);
732 else
734 ULONG64 ref_addr;
735 struct location loc;
737 symt = symt_find_nearest(module, addr);
738 if (symt && !symt_get_address(&symt->symt, &ref_addr))
739 ref_addr = addr;
740 if (!symt || addr != ref_addr)
742 /* creating public symbols for all the ELF symbols which haven't been
743 * used yet (ie we have no debug information on them)
744 * That's the case, for example, of the .spec.c files
746 switch (ELF32_ST_TYPE(ste->symp->st_info))
748 case STT_FUNC:
749 symt_new_function(module, ste->compiland, ste->ht_elt.name,
750 addr, ste->symp->st_size, NULL);
751 break;
752 case STT_OBJECT:
753 loc.kind = loc_absolute;
754 loc.reg = 0;
755 loc.offset = addr;
756 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
757 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
758 loc, ste->symp->st_size, NULL);
759 break;
760 default:
761 FIXME("Shouldn't happen\n");
762 break;
764 /* FIXME: this is a hack !!!
765 * we are adding new symbols, but as we're parsing a symbol table
766 * (hopefully without duplicate symbols) we delay rebuilding the sorted
767 * module table until we're done with the symbol table
768 * Otherwise, as we intertwine symbols' add and lookup, performance
769 * is rather bad
771 module->sortlist_valid = TRUE;
775 /* see comment above */
776 module->sortlist_valid = FALSE;
777 return TRUE;
780 /******************************************************************
781 * elf_new_public_symbols
783 * Creates a set of public symbols from an ELF symtab
785 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
787 struct hash_table_iter hti;
788 struct symtab_elt* ste;
790 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
792 /* FIXME: we're missing the ELF entry point here */
794 hash_table_iter_init(symtab, &hti, NULL);
795 while ((ste = hash_table_iter_up(&hti)))
797 symt_new_public(module, ste->compiland, ste->ht_elt.name,
798 module->reloc_delta + ste->symp->st_value,
799 ste->symp->st_size);
801 return TRUE;
804 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
806 BOOL ret;
807 struct elf_map_file_data emfd;
809 emfd.kind = from_file;
810 emfd.u.file.filename = file;
811 if (!elf_map_file(&emfd, fmap)) return FALSE;
812 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
814 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
815 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
816 elf_unmap_file(fmap);
818 return ret;
821 /******************************************************************
822 * elf_locate_debug_link
824 * Locate a filename from a .gnu_debuglink section, using the same
825 * strategy as gdb:
826 * "If the full name of the directory containing the executable is
827 * execdir, and the executable has a debug link that specifies the
828 * name debugfile, then GDB will automatically search for the
829 * debugging information file in three places:
830 * - the directory containing the executable file (that is, it
831 * will look for a file named `execdir/debugfile',
832 * - a subdirectory of that directory named `.debug' (that is, the
833 * file `execdir/.debug/debugfile', and
834 * - a subdirectory of the global debug file directory that includes
835 * the executable's full path, and the name from the link (that is,
836 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
837 * is the global debug file directory, and execdir has been turned
838 * into a relative path)." (from GDB manual)
840 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
841 const WCHAR* loaded_file, DWORD crc)
843 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
844 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
845 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
846 size_t filename_len;
847 WCHAR* p = NULL;
848 WCHAR* slash;
849 struct image_file_map* fmap_link = NULL;
851 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
852 if (!fmap_link) return FALSE;
854 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
855 p = HeapAlloc(GetProcessHeap(), 0,
856 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
857 if (!p) goto found;
859 /* we prebuild the string with "execdir" */
860 strcpyW(p, loaded_file);
861 slash = strrchrW(p, '/');
862 if (slash == NULL) slash = p; else slash++;
864 /* testing execdir/filename */
865 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
866 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
868 /* testing execdir/.debug/filename */
869 memcpy(slash, dotDebugW, sizeof(dotDebugW));
870 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
871 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
873 /* testing globaldebugdir/execdir/filename */
874 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
875 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
876 slash += globalDebugDirLen;
877 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
878 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
880 /* finally testing filename */
881 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
884 WARN("Couldn't locate or map %s\n", filename);
885 HeapFree(GetProcessHeap(), 0, p);
886 HeapFree(GetProcessHeap(), 0, fmap_link);
887 return FALSE;
889 found:
890 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
891 HeapFree(GetProcessHeap(), 0, p);
892 fmap->u.elf.alternate = fmap_link;
893 return TRUE;
896 /******************************************************************
897 * elf_locate_build_id_target
899 * Try to find the .so file containing the debug info out of the build-id note information
901 static BOOL elf_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
903 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
904 static const WCHAR buildidW[] = {'.','b','u','i','l','d','-','i','d','/'};
905 static const WCHAR dotDebug0W[] = {'.','d','e','b','u','g',0};
906 struct image_file_map* fmap_link = NULL;
907 WCHAR* p;
908 WCHAR* z;
909 const BYTE* idend = id + idlen;
910 struct elf_map_file_data emfd;
912 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
913 if (!fmap_link) return FALSE;
915 p = HeapAlloc(GetProcessHeap(), 0,
916 sizeof(globalDebugDirW) + sizeof(buildidW) +
917 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(dotDebug0W));
918 z = p;
919 memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
920 z += sizeof(globalDebugDirW) / sizeof(WCHAR);
921 memcpy(z, buildidW, sizeof(buildidW));
922 z += sizeof(buildidW) / sizeof(WCHAR);
924 if (id < idend)
926 *z++ = "0123456789abcdef"[*id >> 4 ];
927 *z++ = "0123456789abcdef"[*id & 0x0F];
928 id++;
930 if (id < idend)
931 *z++ = '/';
932 while (id < idend)
934 *z++ = "0123456789abcdef"[*id >> 4 ];
935 *z++ = "0123456789abcdef"[*id & 0x0F];
936 id++;
938 memcpy(z, dotDebug0W, sizeof(dotDebug0W));
939 TRACE("checking %s\n", wine_dbgstr_w(p));
941 emfd.kind = from_file;
942 emfd.u.file.filename = p;
943 if (elf_map_file(&emfd, fmap_link))
945 struct image_section_map buildid_sect;
946 if (elf_find_section(fmap_link, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
948 const uint32_t* note;
950 note = (const uint32_t*)image_map_section(&buildid_sect);
951 if (note != IMAGE_NO_MAP)
953 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
954 if (note[2] == NT_GNU_BUILD_ID)
956 if (note[1] == idlen &&
957 !memcmp(note + 3 + ((note[0] + 3) >> 2), idend - idlen, idlen))
959 TRACE("Located debug information file at %s\n", debugstr_w(p));
960 HeapFree(GetProcessHeap(), 0, p);
961 fmap->u.elf.alternate = fmap_link;
962 return TRUE;
964 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p));
967 image_unmap_section(&buildid_sect);
969 elf_unmap_file(fmap_link);
972 TRACE("not found\n");
973 HeapFree(GetProcessHeap(), 0, p);
974 HeapFree(GetProcessHeap(), 0, fmap_link);
975 return FALSE;
978 /******************************************************************
979 * elf_check_alternate
981 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
982 * or .gnu_debuglink sections.
984 static BOOL elf_check_alternate(struct image_file_map* fmap, const struct module* module)
986 BOOL ret = FALSE;
987 BOOL found = FALSE;
988 struct image_section_map buildid_sect, debuglink_sect;
990 /* if present, add the .gnu_debuglink file as an alternate to current one */
991 if (elf_find_section(fmap, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
993 const uint32_t* note;
995 found = TRUE;
996 note = (const uint32_t*)image_map_section(&buildid_sect);
997 if (note != IMAGE_NO_MAP)
999 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1000 if (note[2] == NT_GNU_BUILD_ID)
1002 ret = elf_locate_build_id_target(fmap, (const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
1005 image_unmap_section(&buildid_sect);
1007 /* if present, add the .gnu_debuglink file as an alternate to current one */
1008 if (!ret && elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
1010 const char* dbg_link;
1012 found = TRUE;
1013 dbg_link = (const char*)image_map_section(&debuglink_sect);
1014 if (dbg_link != IMAGE_NO_MAP)
1016 /* The content of a debug link section is:
1017 * 1/ a NULL terminated string, containing the file name for the
1018 * debug info
1019 * 2/ padding on 4 byte boundary
1020 * 3/ CRC of the linked ELF file
1022 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
1023 ret = elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
1024 if (!ret)
1025 WARN("Couldn't load linked debug file for %s\n",
1026 debugstr_w(module->module.ModuleName));
1028 image_unmap_section(&debuglink_sect);
1030 return found ? ret : TRUE;
1033 /******************************************************************
1034 * elf_load_debug_info_from_map
1036 * Loads the symbolic information from ELF module which mapping is described
1037 * in fmap
1038 * the module has been loaded at 'load_offset' address, so symbols' address
1039 * relocation is performed.
1040 * CRC is checked if fmap->with_crc is TRUE
1041 * returns
1042 * 0 if the file doesn't contain symbolic info (or this info cannot be
1043 * read or parsed)
1044 * 1 on success
1046 static BOOL elf_load_debug_info_from_map(struct module* module,
1047 struct image_file_map* fmap,
1048 struct pool* pool,
1049 struct hash_table* ht_symtab)
1051 BOOL ret = FALSE, lret;
1052 struct elf_thunk_area thunks[] =
1054 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
1055 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1056 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1057 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1058 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1059 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1060 {NULL, 0, 0, 0}
1063 module->module.SymType = SymExport;
1065 /* create a hash table for the symtab */
1066 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
1068 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1070 struct image_section_map stab_sect, stabstr_sect;
1072 /* check if we need an alternate file (from debuglink or build-id) */
1073 ret = elf_check_alternate(fmap, module);
1075 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
1076 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
1078 const char* stab;
1079 const char* stabstr;
1081 stab = image_map_section(&stab_sect);
1082 stabstr = image_map_section(&stabstr_sect);
1083 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
1085 /* OK, now just parse all of the stabs. */
1086 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
1087 stab, image_get_map_size(&stab_sect),
1088 stabstr, image_get_map_size(&stabstr_sect),
1089 NULL, NULL);
1090 if (lret)
1091 /* and fill in the missing information for stabs */
1092 elf_finish_stabs_info(module, ht_symtab);
1093 else
1094 WARN("Couldn't correctly read stabs\n");
1095 ret = ret || lret;
1097 image_unmap_section(&stab_sect);
1098 image_unmap_section(&stabstr_sect);
1100 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
1101 ret = ret || lret;
1103 if (strstrW(module->module.ModuleName, S_ElfW) ||
1104 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1106 /* add the thunks for native libraries */
1107 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1108 elf_new_wine_thunks(module, ht_symtab, thunks);
1110 /* add all the public symbols from symtab */
1111 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1113 return ret;
1116 /******************************************************************
1117 * elf_load_debug_info
1119 * Loads ELF debugging information from the module image file.
1121 BOOL elf_load_debug_info(struct module* module)
1123 BOOL ret = TRUE;
1124 struct pool pool;
1125 struct hash_table ht_symtab;
1126 struct module_format* modfmt;
1128 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1130 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1131 return FALSE;
1134 pool_init(&pool, 65536);
1135 hash_table_init(&pool, &ht_symtab, 256);
1137 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1139 pool_destroy(&pool);
1140 return ret;
1143 /******************************************************************
1144 * elf_fetch_file_info
1146 * Gathers some more information for an ELF module from a given file
1148 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1149 DWORD* size, DWORD* checksum)
1151 struct image_file_map fmap;
1153 struct elf_map_file_data emfd;
1155 emfd.kind = from_file;
1156 emfd.u.file.filename = name;
1157 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1158 if (base) *base = fmap.u.elf.elf_start;
1159 *size = fmap.u.elf.elf_size;
1160 *checksum = calc_crc32(fmap.u.elf.fd);
1161 elf_unmap_file(&fmap);
1162 return TRUE;
1165 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1166 struct image_file_map* fmap, unsigned long load_offset,
1167 unsigned long dyn_addr, struct elf_info* elf_info)
1169 BOOL ret = FALSE;
1171 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1173 struct image_section_map ism;
1175 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1177 Elf_Dyn dyn;
1178 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1179 unsigned long len;
1183 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1184 len != sizeof(dyn))
1185 return ret;
1186 if (dyn.d_tag == DT_DEBUG)
1188 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1189 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1190 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1191 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1192 break;
1194 ptr += sizeof(dyn);
1195 } while (dyn.d_tag != DT_NULL);
1196 if (dyn.d_tag == DT_NULL) return ret;
1198 elf_end_find(fmap);
1201 if (elf_info->flags & ELF_INFO_MODULE)
1203 struct elf_module_info *elf_module_info;
1204 struct module_format* modfmt;
1205 struct image_section_map ism;
1206 unsigned long modbase = load_offset;
1208 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1210 unsigned long rva_dyn = elf_get_map_rva(&ism);
1212 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1213 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1214 load_offset, dyn_addr);
1215 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1217 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1218 modbase = dyn_addr - rva_dyn;
1220 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1221 elf_end_find(fmap);
1223 modfmt = HeapAlloc(GetProcessHeap(), 0,
1224 sizeof(struct module_format) + sizeof(struct elf_module_info));
1225 if (!modfmt) return FALSE;
1226 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1227 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1228 if (!elf_info->module)
1230 HeapFree(GetProcessHeap(), 0, modfmt);
1231 return FALSE;
1233 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1234 elf_module_info = (void*)(modfmt + 1);
1235 elf_info->module->format_info[DFI_ELF] = modfmt;
1236 modfmt->module = elf_info->module;
1237 modfmt->remove = elf_module_remove;
1238 modfmt->loc_compute = NULL;
1239 modfmt->u.elf_info = elf_module_info;
1241 elf_module_info->elf_addr = load_offset;
1243 elf_module_info->file_map = *fmap;
1244 elf_reset_file_map(fmap);
1245 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1247 elf_info->module->module.SymType = SymDeferred;
1248 ret = TRUE;
1250 else ret = elf_load_debug_info(elf_info->module);
1252 elf_module_info->elf_mark = 1;
1253 elf_module_info->elf_loader = 0;
1254 } else ret = TRUE;
1256 if (elf_info->flags & ELF_INFO_NAME)
1258 WCHAR* ptr;
1259 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1260 if (ptr)
1262 strcpyW(ptr, filename);
1263 elf_info->module_name = ptr;
1265 else ret = FALSE;
1268 return ret;
1271 /******************************************************************
1272 * elf_load_file
1274 * Loads the information for ELF module stored in 'filename'
1275 * the module has been loaded at 'load_offset' address
1276 * returns
1277 * -1 if the file cannot be found/opened
1278 * 0 if the file doesn't contain symbolic info (or this info cannot be
1279 * read or parsed)
1280 * 1 on success
1282 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1283 unsigned long load_offset, unsigned long dyn_addr,
1284 struct elf_info* elf_info)
1286 BOOL ret = FALSE;
1287 struct image_file_map fmap;
1288 struct elf_map_file_data emfd;
1290 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1292 emfd.kind = from_file;
1293 emfd.u.file.filename = filename;
1294 if (!elf_map_file(&emfd, &fmap)) return ret;
1296 /* Next, we need to find a few of the internal ELF headers within
1297 * this thing. We need the main executable header, and the section
1298 * table.
1300 if (!fmap.u.elf.elf_start && !load_offset)
1301 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1302 debugstr_w(filename));
1304 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1306 elf_unmap_file(&fmap);
1308 return ret;
1311 /******************************************************************
1312 * elf_load_file_from_path
1313 * tries to load an ELF file from a set of paths (separated by ':')
1315 static BOOL elf_load_file_from_path(HANDLE hProcess,
1316 const WCHAR* filename,
1317 unsigned long load_offset,
1318 unsigned long dyn_addr,
1319 const char* path,
1320 struct elf_info* elf_info)
1322 BOOL ret = FALSE;
1323 WCHAR *s, *t, *fn;
1324 WCHAR* pathW = NULL;
1325 unsigned len;
1327 if (!path) return FALSE;
1329 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1330 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1331 if (!pathW) return FALSE;
1332 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1334 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1336 t = strchrW(s, ':');
1337 if (t) *t = '\0';
1338 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1339 if (!fn) break;
1340 strcpyW(fn, s);
1341 strcatW(fn, S_SlashW);
1342 strcatW(fn, filename);
1343 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1344 HeapFree(GetProcessHeap(), 0, fn);
1345 if (ret) break;
1348 HeapFree(GetProcessHeap(), 0, pathW);
1349 return ret;
1352 /******************************************************************
1353 * elf_load_file_from_dll_path
1355 * Tries to load an ELF file from the dll path
1357 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1358 const WCHAR* filename,
1359 unsigned long load_offset,
1360 unsigned long dyn_addr,
1361 struct elf_info* elf_info)
1363 BOOL ret = FALSE;
1364 unsigned int index = 0;
1365 const char *path;
1367 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1369 WCHAR *name;
1370 unsigned len;
1372 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1374 name = HeapAlloc( GetProcessHeap(), 0,
1375 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1377 if (!name) break;
1378 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1379 strcatW( name, S_SlashW );
1380 strcatW( name, filename );
1381 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1382 HeapFree( GetProcessHeap(), 0, name );
1384 return ret;
1387 #ifdef AT_SYSINFO_EHDR
1388 /******************************************************************
1389 * elf_search_auxv
1391 * locate some a value from the debuggee auxiliary vector
1393 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1395 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1396 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1397 void* addr;
1398 void* str;
1399 void* str_max;
1400 Elf_auxv_t auxv;
1402 si->SizeOfStruct = sizeof(*si);
1403 si->MaxNameLen = MAX_SYM_NAME;
1404 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1405 !(addr = (void*)(DWORD_PTR)si->Address) ||
1406 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1407 !addr)
1409 FIXME("can't find symbol in module\n");
1410 return FALSE;
1412 /* walk through envp[] */
1413 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1414 str_max = (void*)(DWORD_PTR)~0L;
1415 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1416 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1417 str_max = min(str_max, str);
1419 /* Walk through the end of envp[] array.
1420 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1421 * deleted, the last entry is replaced by an extra NULL.
1423 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1424 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1426 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1428 if (auxv.a_type == type)
1430 *val = auxv.a_un.a_val;
1431 return TRUE;
1433 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1436 return FALSE;
1438 #endif
1440 /******************************************************************
1441 * elf_search_and_load_file
1443 * lookup a file in standard ELF locations, and if found, load it
1445 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1446 unsigned long load_offset, unsigned long dyn_addr,
1447 struct elf_info* elf_info)
1449 BOOL ret = FALSE;
1450 struct module* module;
1451 static WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1453 if (filename == NULL || *filename == '\0') return FALSE;
1454 if ((module = module_is_already_loaded(pcs, filename)))
1456 elf_info->module = module;
1457 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1458 return module->module.SymType;
1461 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1462 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1463 /* if relative pathname, try some absolute base dirs */
1464 if (!ret && !strchrW(filename, '/'))
1466 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1467 getenv("PATH"), elf_info) ||
1468 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1469 getenv("LD_LIBRARY_PATH"), elf_info);
1470 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1471 load_offset, dyn_addr, elf_info);
1474 return ret;
1477 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1478 unsigned long dyn_addr, BOOL is_system, void* user);
1480 /******************************************************************
1481 * elf_enum_modules_internal
1483 * Enumerate ELF modules from a running process
1485 static BOOL elf_enum_modules_internal(const struct process* pcs,
1486 const WCHAR* main_name,
1487 enum_elf_modules_cb cb, void* user)
1489 struct r_debug dbg_hdr;
1490 void* lm_addr;
1491 struct link_map lm;
1492 char bufstr[256];
1493 WCHAR bufstrW[MAX_PATH];
1495 if (!pcs->dbg_hdr_addr ||
1496 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1497 &dbg_hdr, sizeof(dbg_hdr), NULL))
1498 return FALSE;
1500 /* Now walk the linked list. In all known ELF implementations,
1501 * the dynamic loader maintains this linked list for us. In some
1502 * cases the first entry doesn't appear with a name, in other cases it
1503 * does.
1505 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1507 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1508 return FALSE;
1510 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1511 lm.l_name != NULL &&
1512 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1514 bufstr[sizeof(bufstr) - 1] = '\0';
1515 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1516 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1517 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1521 #ifdef AT_SYSINFO_EHDR
1522 if (!lm_addr)
1524 unsigned long ehdr_addr;
1526 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1528 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1529 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1532 #endif
1533 return TRUE;
1536 /******************************************************************
1537 * elf_search_loader
1539 * Lookup in a running ELF process the loader, and sets its ELF link
1540 * address (for accessing the list of loaded .so libs) in pcs.
1541 * If flags is ELF_INFO_MODULE, the module for the loader is also
1542 * added as a module into pcs.
1544 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1546 return elf_search_and_load_file(pcs, get_wine_loader_name(), 0, 0, elf_info);
1549 /******************************************************************
1550 * elf_read_wine_loader_dbg_info
1552 * Try to find a decent wine executable which could have loaded the debuggee
1554 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1556 struct elf_info elf_info;
1558 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1559 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1560 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1561 module_set_module(elf_info.module, S_WineLoaderW);
1562 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1565 struct elf_enum_user
1567 enum_modules_cb cb;
1568 void* user;
1571 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1572 unsigned long dyn_addr, BOOL is_system, void* user)
1574 struct elf_enum_user* eeu = user;
1575 return eeu->cb(name, load_addr, eeu->user);
1578 /******************************************************************
1579 * elf_enum_modules
1581 * Enumerates the ELF loaded modules from a running target (hProc)
1582 * This function doesn't require that someone has called SymInitialize
1583 * on this very process.
1585 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1587 struct process pcs;
1588 struct elf_info elf_info;
1589 BOOL ret;
1590 struct elf_enum_user eeu;
1592 memset(&pcs, 0, sizeof(pcs));
1593 pcs.handle = hProc;
1594 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1595 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1596 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1597 eeu.cb = cb;
1598 eeu.user = user;
1599 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1600 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1601 return ret;
1604 struct elf_load
1606 struct process* pcs;
1607 struct elf_info elf_info;
1608 const WCHAR* name;
1609 BOOL ret;
1612 /******************************************************************
1613 * elf_load_cb
1615 * Callback for elf_load_module, used to walk the list of loaded
1616 * modules.
1618 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1619 unsigned long dyn_addr, BOOL is_system, void* user)
1621 struct elf_load* el = user;
1622 BOOL ret = TRUE;
1623 const WCHAR* p;
1625 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1627 struct module* module;
1628 struct elf_map_file_data emfd;
1629 struct image_file_map fmap;
1631 if ((module = module_is_already_loaded(el->pcs, name)))
1633 el->elf_info.module = module;
1634 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1635 return module->module.SymType;
1638 emfd.kind = from_process;
1639 emfd.u.process.handle = el->pcs->handle;
1640 emfd.u.process.load_addr = (void*)load_addr;
1642 if (elf_map_file(&emfd, &fmap))
1643 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1644 return TRUE;
1646 if (el->name)
1648 /* memcmp is needed for matches when bufstr contains also version information
1649 * el->name: libc.so, name: libc.so.6.0
1651 p = strrchrW(name, '/');
1652 if (!p++) p = name;
1655 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1657 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1658 if (el->name) ret = FALSE;
1661 return ret;
1664 /******************************************************************
1665 * elf_load_module
1667 * loads an ELF module and stores it in process' module list
1668 * Also, find module real name and load address from
1669 * the real loaded modules list in pcs address space
1671 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1673 struct elf_load el;
1675 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1677 el.elf_info.flags = ELF_INFO_MODULE;
1678 el.ret = FALSE;
1680 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1682 el.pcs = pcs;
1683 /* do only the lookup from the filename, not the path (as we lookup module
1684 * name in the process' loaded module list)
1686 el.name = strrchrW(name, '/');
1687 if (!el.name++) el.name = name;
1688 el.ret = FALSE;
1690 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1691 return NULL;
1693 else if (addr)
1695 el.name = name;
1696 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1698 if (!el.ret) return NULL;
1699 assert(el.elf_info.module);
1700 return el.elf_info.module;
1703 /******************************************************************
1704 * elf_synchronize_module_list
1706 * this functions rescans the debuggee module's list and synchronizes it with
1707 * the one from 'pcs', ie:
1708 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1709 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1711 BOOL elf_synchronize_module_list(struct process* pcs)
1713 struct module* module;
1714 struct elf_load el;
1716 for (module = pcs->lmodules; module; module = module->next)
1718 if (module->type == DMT_ELF && !module->is_virtual)
1719 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1722 el.pcs = pcs;
1723 el.elf_info.flags = ELF_INFO_MODULE;
1724 el.ret = FALSE;
1725 el.name = NULL; /* fetch all modules */
1727 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1728 return FALSE;
1730 module = pcs->lmodules;
1731 while (module)
1733 if (module->type == DMT_ELF && !module->is_virtual)
1735 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1737 if (!elf_info->elf_mark && !elf_info->elf_loader)
1739 module_remove(pcs, module);
1740 /* restart all over */
1741 module = pcs->lmodules;
1742 continue;
1745 module = module->next;
1747 return TRUE;
1750 #else /* !__ELF__ */
1752 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1753 unsigned sht, struct image_section_map* ism)
1755 return FALSE;
1758 const char* elf_map_section(struct image_section_map* ism)
1760 return NULL;
1763 void elf_unmap_section(struct image_section_map* ism)
1766 unsigned elf_get_map_size(const struct image_section_map* ism)
1768 return 0;
1771 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1773 return 0;
1776 BOOL elf_synchronize_module_list(struct process* pcs)
1778 return FALSE;
1781 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1782 DWORD* size, DWORD* checksum)
1784 return FALSE;
1787 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1789 return FALSE;
1792 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1794 return FALSE;
1797 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1799 return NULL;
1802 BOOL elf_load_debug_info(struct module* module)
1804 return FALSE;
1807 int elf_is_in_thunk_area(unsigned long addr,
1808 const struct elf_thunk_area* thunks)
1810 return -1;
1812 #endif /* __ELF__ */