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