* Sync up to trunk head (r64921).
[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 "dbghelp_private.h"
23
24 #if defined(__svr4__) || defined(__sun)
25 #define __ELF__ 1
26 /* large files are not supported by libelf */
27 #undef _FILE_OFFSET_BITS
28 #define _FILE_OFFSET_BITS 32
29 #endif
30
31 #include <stdlib.h>
32 #include <fcntl.h>
33
34 #include <wine/library.h>
35
36 #ifdef __ELF__
37
38 #define ELF_INFO_DEBUG_HEADER 0x0001
39 #define ELF_INFO_MODULE 0x0002
40 #define ELF_INFO_NAME 0x0004
41
42 #ifndef NT_GNU_BUILD_ID
43 #define NT_GNU_BUILD_ID 3
44 #endif
45
46 #ifndef HAVE_STRUCT_R_DEBUG
47 struct r_debug
48 {
49 int r_version;
50 struct link_map *r_map;
51 ElfW(Addr) r_brk;
52 enum
53 {
54 RT_CONSISTENT,
55 RT_ADD,
56 RT_DELETE
57 } r_state;
58 ElfW(Addr) r_ldbase;
59 };
60 #endif /* HAVE_STRUCT_R_DEBUG */
61
62 #ifndef HAVE_STRUCT_LINK_MAP
63 struct link_map
64 {
65 ElfW(Addr) l_addr;
66 char *l_name;
67 ElfW(Dyn) *l_ld;
68 struct link_map *l_next, *l_prev;
69 };
70 #endif /* HAVE_STRUCT_LINK_MAP */
71
72 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
73
74 struct elf_info
75 {
76 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
77 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
78 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
79 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
80 };
81
82 struct symtab_elt
83 {
84 struct hash_table_elt ht_elt;
85 const Elf_Sym* symp;
86 struct symt_compiland* compiland;
87 unsigned used;
88 };
89
90 struct elf_thunk_area
91 {
92 const char* symname;
93 THUNK_ORDINAL ordinal;
94 unsigned long rva_start;
95 unsigned long rva_end;
96 };
97
98 struct elf_module_info
99 {
100 unsigned long elf_addr;
101 unsigned short elf_mark : 1,
102 elf_loader : 1;
103 struct image_file_map file_map;
104 };
105
106 /******************************************************************
107 * elf_map_section
108 *
109 * Maps a single section into memory from an ELF file
110 */
111 const char* elf_map_section(struct image_section_map* ism)
112 {
113 struct elf_file_map* fmap = &ism->fmap->u.elf;
114 size_t ofst, size, pgsz = sysconf( _SC_PAGESIZE );
115
116 assert(ism->fmap->modtype == DMT_ELF);
117 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
118 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
119 return IMAGE_NO_MAP;
120
121 if (fmap->target_copy)
122 {
123 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
124 }
125 /* align required information on page size (we assume pagesize is a power of 2) */
126 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
127 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
128 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
129 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
130 fmap->fd, ofst);
131 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
132 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
133 }
134
135 /******************************************************************
136 * elf_find_section
137 *
138 * Finds a section by name (and type) into memory from an ELF file
139 * or its alternate if any
140 */
141 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
142 unsigned sht, struct image_section_map* ism)
143 {
144 struct elf_file_map* fmap;
145 unsigned i;
146
147 while (_fmap)
148 {
149 fmap = &_fmap->u.elf;
150 if (fmap->shstrtab == IMAGE_NO_MAP)
151 {
152 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
153 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
154 }
155 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
156 {
157 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
158 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
159 {
160 ism->fmap = _fmap;
161 ism->sidx = i;
162 return TRUE;
163 }
164 }
165 _fmap = fmap->alternate;
166 }
167 ism->fmap = NULL;
168 ism->sidx = -1;
169 return FALSE;
170 }
171
172 /******************************************************************
173 * elf_unmap_section
174 *
175 * Unmaps a single section from memory
176 */
177 void elf_unmap_section(struct image_section_map* ism)
178 {
179 struct elf_file_map* fmap = &ism->fmap->u.elf;
180
181 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
182 fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
183 {
184 size_t pgsz = sysconf( _SC_PAGESIZE );
185 size_t ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
186 size_t size = ((fmap->sect[ism->sidx].shdr.sh_offset +
187 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
188 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
189 WARN("Couldn't unmap the section\n");
190 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
191 }
192 }
193
194 static void elf_end_find(struct image_file_map* fmap)
195 {
196 struct image_section_map ism;
197
198 while (fmap)
199 {
200 ism.fmap = fmap;
201 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
202 elf_unmap_section(&ism);
203 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
204 fmap = fmap->u.elf.alternate;
205 }
206 }
207
208 /******************************************************************
209 * elf_get_map_rva
210 *
211 * Get the RVA of an ELF section
212 */
213 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
214 {
215 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
216 return 0;
217 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
218 }
219
220 /******************************************************************
221 * elf_get_map_size
222 *
223 * Get the size of an ELF section
224 */
225 unsigned elf_get_map_size(const struct image_section_map* ism)
226 {
227 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
228 return 0;
229 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
230 }
231
232 static inline void elf_reset_file_map(struct image_file_map* fmap)
233 {
234 fmap->u.elf.fd = -1;
235 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
236 fmap->u.elf.alternate = NULL;
237 fmap->u.elf.target_copy = NULL;
238 }
239
240 struct elf_map_file_data
241 {
242 enum {from_file, from_process} kind;
243 union
244 {
245 struct
246 {
247 const WCHAR* filename;
248 } file;
249 struct
250 {
251 HANDLE handle;
252 void* load_addr;
253 } process;
254 } u;
255 };
256
257 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
258 void* buf, size_t len, off_t off)
259 {
260 SIZE_T dw;
261
262 switch (emfd->kind)
263 {
264 case from_file:
265 return pread(fmap->u.elf.fd, buf, len, off) == len;
266 case from_process:
267 return ReadProcessMemory(emfd->u.process.handle,
268 (void*)((unsigned long)emfd->u.process.load_addr + (unsigned long)off),
269 buf, len, &dw) && dw == len;
270 default:
271 assert(0);
272 return FALSE;
273 }
274 }
275
276 /******************************************************************
277 * elf_map_file
278 *
279 * Maps an ELF file into memory (and checks it's a real ELF file)
280 */
281 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
282 {
283 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
284 struct stat statbuf;
285 unsigned int i;
286 Elf_Phdr phdr;
287 size_t tmp, page_mask = sysconf( _SC_PAGESIZE ) - 1;
288 char* filename;
289 unsigned len;
290 BOOL ret = FALSE;
291
292 switch (emfd->kind)
293 {
294 case from_file:
295 len = WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, NULL, 0, NULL, NULL);
296 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
297 WideCharToMultiByte(CP_UNIXCP, 0, emfd->u.file.filename, -1, filename, len, NULL, NULL);
298 break;
299 case from_process:
300 filename = NULL;
301 break;
302 default: assert(0);
303 return FALSE;
304 }
305
306 elf_reset_file_map(fmap);
307
308 fmap->modtype = DMT_ELF;
309 fmap->u.elf.fd = -1;
310 fmap->u.elf.target_copy = NULL;
311
312 switch (emfd->kind)
313 {
314 case from_file:
315 /* check that the file exists, and that the module hasn't been loaded yet */
316 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
317
318 /* Now open the file, so that we can mmap() it. */
319 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
320 break;
321 case from_process:
322 break;
323 }
324 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
325 goto done;
326
327 /* and check for an ELF header */
328 if (memcmp(fmap->u.elf.elfhdr.e_ident,
329 elf_signature, sizeof(elf_signature))) goto done;
330 /* and check 32 vs 64 size according to current machine */
331 #ifdef _WIN64
332 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
333 #else
334 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
335 #endif
336 fmap->addr_size = fmap->u.elf.elfhdr.e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
337 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
338 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
339 if (!fmap->u.elf.sect) goto done;
340
341 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
342 {
343 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
344 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
345 {
346 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
347 fmap->u.elf.sect = NULL;
348 goto done;
349 }
350 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
351 }
352
353 /* grab size of module once loaded in memory */
354 fmap->u.elf.elf_size = 0;
355 fmap->u.elf.elf_start = ~0L;
356 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
357 {
358 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
359 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
360 phdr.p_type == PT_LOAD)
361 {
362 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
363 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
364 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
365 }
366 }
367 /* if non relocatable ELF, then remove fixed address from computation
368 * otherwise, all addresses are zero based and start has no effect
369 */
370 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
371
372 switch (emfd->kind)
373 {
374 case from_file: break;
375 case from_process:
376 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
377 {
378 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
379 goto done;
380 }
381 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
382 fmap->u.elf.elf_size, NULL))
383 {
384 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
385 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
386 goto done;
387 }
388 break;
389 }
390 ret = TRUE;
391 done:
392 HeapFree(GetProcessHeap(), 0, filename);
393 return ret;
394 }
395
396 /******************************************************************
397 * elf_unmap_file
398 *
399 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
400 */
401 static void elf_unmap_file(struct image_file_map* fmap)
402 {
403 while (fmap)
404 {
405 if (fmap->u.elf.fd != -1)
406 {
407 struct image_section_map ism;
408 ism.fmap = fmap;
409 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
410 {
411 elf_unmap_section(&ism);
412 }
413 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
414 close(fmap->u.elf.fd);
415 }
416 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
417 fmap = fmap->u.elf.alternate;
418 }
419 }
420
421 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
422 {
423 elf_unmap_file(&modfmt->u.elf_info->file_map);
424 HeapFree(GetProcessHeap(), 0, modfmt);
425 }
426
427 /******************************************************************
428 * elf_is_in_thunk_area
429 *
430 * Check whether an address lies within one of the thunk area we
431 * know of.
432 */
433 int elf_is_in_thunk_area(unsigned long addr,
434 const struct elf_thunk_area* thunks)
435 {
436 unsigned i;
437
438 if (thunks) for (i = 0; thunks[i].symname; i++)
439 {
440 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
441 return i;
442 }
443 return -1;
444 }
445
446 /******************************************************************
447 * elf_hash_symtab
448 *
449 * creating an internal hash table to ease use ELF symtab information lookup
450 */
451 static void elf_hash_symtab(struct module* module, struct pool* pool,
452 struct hash_table* ht_symtab, struct image_file_map* fmap,
453 struct elf_thunk_area* thunks)
454 {
455 int i, j, nsym;
456 const char* strp;
457 const char* symname;
458 struct symt_compiland* compiland = NULL;
459 const char* ptr;
460 const Elf_Sym* symp;
461 struct symtab_elt* ste;
462 struct image_section_map ism, ism_str;
463
464 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
465 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
466 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
467 ism_str.fmap = ism.fmap;
468 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
469 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
470 {
471 image_unmap_section(&ism);
472 return;
473 }
474
475 nsym = image_get_map_size(&ism) / sizeof(*symp);
476
477 for (j = 0; thunks[j].symname; j++)
478 thunks[j].rva_start = thunks[j].rva_end = 0;
479
480 for (i = 0; i < nsym; i++, symp++)
481 {
482 /* Ignore certain types of entries which really aren't of that much
483 * interest.
484 */
485 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
486 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
487 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
488 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
489 symp->st_shndx == SHN_UNDEF)
490 {
491 continue;
492 }
493
494 symname = strp + symp->st_name;
495
496 /* handle some specific symtab (that we'll throw away when done) */
497 switch (ELF32_ST_TYPE(symp->st_info))
498 {
499 case STT_FILE:
500 if (symname)
501 compiland = symt_new_compiland(module, symp->st_value,
502 source_new(module, NULL, symname));
503 else
504 compiland = NULL;
505 continue;
506 case STT_NOTYPE:
507 /* we are only interested in wine markers inserted by winebuild */
508 for (j = 0; thunks[j].symname; j++)
509 {
510 if (!strcmp(symname, thunks[j].symname))
511 {
512 thunks[j].rva_start = symp->st_value;
513 thunks[j].rva_end = symp->st_value + symp->st_size;
514 break;
515 }
516 }
517 continue;
518 }
519
520 /* FIXME: we don't need to handle them (GCC internals)
521 * Moreover, they screw up our symbol lookup :-/
522 */
523 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
524 continue;
525
526 ste = pool_alloc(pool, sizeof(*ste));
527 ste->ht_elt.name = symname;
528 /* GCC emits, in some cases, a .<digit>+ suffix.
529 * This is used for static variable inside functions, so
530 * that we can have several such variables with same name in
531 * the same compilation unit
532 * We simply ignore that suffix when present (we also get rid
533 * of it in stabs parsing)
534 */
535 ptr = symname + strlen(symname) - 1;
536 if (isdigit(*ptr))
537 {
538 while (isdigit(*ptr) && ptr >= symname) ptr--;
539 if (ptr > symname && *ptr == '.')
540 {
541 char* n = pool_alloc(pool, ptr - symname + 1);
542 memcpy(n, symname, ptr - symname + 1);
543 n[ptr - symname] = '\0';
544 ste->ht_elt.name = n;
545 }
546 }
547 ste->symp = symp;
548 ste->compiland = compiland;
549 ste->used = 0;
550 hash_table_add(ht_symtab, &ste->ht_elt);
551 }
552 /* as we added in the ht_symtab pointers to the symbols themselves,
553 * we cannot unmap yet the sections, it will be done when we're over
554 * with this ELF file
555 */
556 }
557
558 /******************************************************************
559 * elf_lookup_symtab
560 *
561 * lookup a symbol by name in our internal hash table for the symtab
562 */
563 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
564 const struct hash_table* ht_symtab,
565 const char* name, const struct symt* compiland)
566 {
567 struct symtab_elt* weak_result = NULL; /* without compiland name */
568 struct symtab_elt* result = NULL;
569 struct hash_table_iter hti;
570 struct symtab_elt* ste;
571 const char* compiland_name;
572 const char* compiland_basename;
573 const char* base;
574
575 /* we need weak match up (at least) when symbols of same name,
576 * defined several times in different compilation units,
577 * are merged in a single one (hence a different filename for c.u.)
578 */
579 if (compiland)
580 {
581 compiland_name = source_get(module,
582 ((const struct symt_compiland*)compiland)->source);
583 compiland_basename = strrchr(compiland_name, '/');
584 if (!compiland_basename++) compiland_basename = compiland_name;
585 }
586 else compiland_name = compiland_basename = NULL;
587
588 hash_table_iter_init(ht_symtab, &hti, name);
589 while ((ste = hash_table_iter_up(&hti)))
590 {
591 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
592
593 weak_result = ste;
594 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
595 continue;
596 if (ste->compiland && compiland_name)
597 {
598 const char* filename = source_get(module, ste->compiland->source);
599 if (strcmp(filename, compiland_name))
600 {
601 base = strrchr(filename, '/');
602 if (!base++) base = filename;
603 if (strcmp(base, compiland_basename)) continue;
604 }
605 }
606 if (result)
607 {
608 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
609 name, compiland_name,
610 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
611 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
612 }
613 else
614 {
615 result = ste;
616 ste->used = 1;
617 }
618 }
619 if (!result && !(result = weak_result))
620 {
621 FIXME("Couldn't find symbol %s!%s in symtab\n",
622 debugstr_w(module->module.ModuleName), name);
623 return NULL;
624 }
625 return result->symp;
626 }
627
628 /******************************************************************
629 * elf_finish_stabs_info
630 *
631 * - get any relevant information (address & size) from the bits we got from the
632 * stabs debugging information
633 */
634 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
635 {
636 struct hash_table_iter hti;
637 void* ptr;
638 struct symt_ht* sym;
639 const Elf_Sym* symp;
640 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
641
642 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
643 while ((ptr = hash_table_iter_up(&hti)))
644 {
645 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
646 switch (sym->symt.tag)
647 {
648 case SymTagFunction:
649 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
650 ((struct symt_function*)sym)->size)
651 {
652 break;
653 }
654 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
655 ((struct symt_function*)sym)->container);
656 if (symp)
657 {
658 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
659 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
660 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
661 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
662 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
663 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
664 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
665 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
666 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
667
668 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
669 ((struct symt_function*)sym)->size = symp->st_size;
670 } else
671 FIXME("Couldn't find %s!%s\n",
672 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
673 break;
674 case SymTagData:
675 switch (((struct symt_data*)sym)->kind)
676 {
677 case DataIsGlobal:
678 case DataIsFileStatic:
679 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
680 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
681 break;
682 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
683 ((struct symt_data*)sym)->container);
684 if (symp)
685 {
686 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
687 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
688 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
689 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
690 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
691 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
692 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
693 DataIsFileStatic : DataIsGlobal;
694 } else
695 FIXME("Couldn't find %s!%s\n",
696 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
697 break;
698 default:;
699 }
700 break;
701 default:
702 FIXME("Unsupported tag %u\n", sym->symt.tag);
703 break;
704 }
705 }
706 /* since we may have changed some addresses & sizes, mark the module to be resorted */
707 module->sortlist_valid = FALSE;
708 }
709
710 /******************************************************************
711 * elf_load_wine_thunks
712 *
713 * creating the thunk objects for a wine native DLL
714 */
715 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
716 const struct elf_thunk_area* thunks)
717 {
718 int j;
719 struct hash_table_iter hti;
720 struct symtab_elt* ste;
721 DWORD_PTR addr;
722 struct symt_ht* symt;
723
724 hash_table_iter_init(ht_symtab, &hti, NULL);
725 while ((ste = hash_table_iter_up(&hti)))
726 {
727 if (ste->used) continue;
728
729 addr = module->reloc_delta + ste->symp->st_value;
730
731 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
732 if (j >= 0) /* thunk found */
733 {
734 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
735 addr, ste->symp->st_size);
736 }
737 else
738 {
739 ULONG64 ref_addr;
740 struct location loc;
741
742 symt = symt_find_nearest(module, addr);
743 if (symt && !symt_get_address(&symt->symt, &ref_addr))
744 ref_addr = addr;
745 if (!symt || addr != ref_addr)
746 {
747 /* creating public symbols for all the ELF symbols which haven't been
748 * used yet (ie we have no debug information on them)
749 * That's the case, for example, of the .spec.c files
750 */
751 switch (ELF32_ST_TYPE(ste->symp->st_info))
752 {
753 case STT_FUNC:
754 symt_new_function(module, ste->compiland, ste->ht_elt.name,
755 addr, ste->symp->st_size, NULL);
756 break;
757 case STT_OBJECT:
758 loc.kind = loc_absolute;
759 loc.reg = 0;
760 loc.offset = addr;
761 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
762 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
763 loc, ste->symp->st_size, NULL);
764 break;
765 default:
766 FIXME("Shouldn't happen\n");
767 break;
768 }
769 /* FIXME: this is a hack !!!
770 * we are adding new symbols, but as we're parsing a symbol table
771 * (hopefully without duplicate symbols) we delay rebuilding the sorted
772 * module table until we're done with the symbol table
773 * Otherwise, as we intertwine symbols' add and lookup, performance
774 * is rather bad
775 */
776 module->sortlist_valid = TRUE;
777 }
778 }
779 }
780 /* see comment above */
781 module->sortlist_valid = FALSE;
782 return TRUE;
783 }
784
785 /******************************************************************
786 * elf_new_public_symbols
787 *
788 * Creates a set of public symbols from an ELF symtab
789 */
790 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
791 {
792 struct hash_table_iter hti;
793 struct symtab_elt* ste;
794
795 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
796
797 /* FIXME: we're missing the ELF entry point here */
798
799 hash_table_iter_init(symtab, &hti, NULL);
800 while ((ste = hash_table_iter_up(&hti)))
801 {
802 symt_new_public(module, ste->compiland, ste->ht_elt.name,
803 module->reloc_delta + ste->symp->st_value,
804 ste->symp->st_size);
805 }
806 return TRUE;
807 }
808
809 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
810 {
811 BOOL ret;
812 struct elf_map_file_data emfd;
813
814 emfd.kind = from_file;
815 emfd.u.file.filename = file;
816 if (!elf_map_file(&emfd, fmap)) return FALSE;
817 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
818 {
819 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
820 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
821 elf_unmap_file(fmap);
822 }
823 return ret;
824 }
825
826 /******************************************************************
827 * elf_locate_debug_link
828 *
829 * Locate a filename from a .gnu_debuglink section, using the same
830 * strategy as gdb:
831 * "If the full name of the directory containing the executable is
832 * execdir, and the executable has a debug link that specifies the
833 * name debugfile, then GDB will automatically search for the
834 * debugging information file in three places:
835 * - the directory containing the executable file (that is, it
836 * will look for a file named `execdir/debugfile',
837 * - a subdirectory of that directory named `.debug' (that is, the
838 * file `execdir/.debug/debugfile', and
839 * - a subdirectory of the global debug file directory that includes
840 * the executable's full path, and the name from the link (that is,
841 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
842 * is the global debug file directory, and execdir has been turned
843 * into a relative path)." (from GDB manual)
844 */
845 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
846 const WCHAR* loaded_file, DWORD crc)
847 {
848 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
849 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
850 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
851 size_t filename_len;
852 WCHAR* p = NULL;
853 WCHAR* slash;
854 struct image_file_map* fmap_link = NULL;
855
856 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
857 if (!fmap_link) return FALSE;
858
859 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
860 p = HeapAlloc(GetProcessHeap(), 0,
861 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
862 if (!p) goto found;
863
864 /* we prebuild the string with "execdir" */
865 strcpyW(p, loaded_file);
866 slash = strrchrW(p, '/');
867 if (slash == NULL) slash = p; else slash++;
868
869 /* testing execdir/filename */
870 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
871 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
872
873 /* testing execdir/.debug/filename */
874 memcpy(slash, dotDebugW, sizeof(dotDebugW));
875 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
876 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
877
878 /* testing globaldebugdir/execdir/filename */
879 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
880 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
881 slash += globalDebugDirLen;
882 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
883 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
884
885 /* finally testing filename */
886 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
887
888
889 WARN("Couldn't locate or map %s\n", filename);
890 HeapFree(GetProcessHeap(), 0, p);
891 HeapFree(GetProcessHeap(), 0, fmap_link);
892 return FALSE;
893
894 found:
895 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
896 HeapFree(GetProcessHeap(), 0, p);
897 fmap->u.elf.alternate = fmap_link;
898 return TRUE;
899 }
900
901 /******************************************************************
902 * elf_locate_build_id_target
903 *
904 * Try to find the .so file containing the debug info out of the build-id note information
905 */
906 static BOOL elf_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
907 {
908 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
909 static const WCHAR buildidW[] = {'.','b','u','i','l','d','-','i','d','/'};
910 static const WCHAR dotDebug0W[] = {'.','d','e','b','u','g',0};
911 struct image_file_map* fmap_link = NULL;
912 WCHAR* p;
913 WCHAR* z;
914 const BYTE* idend = id + idlen;
915 struct elf_map_file_data emfd;
916
917 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
918 if (!fmap_link) return FALSE;
919
920 p = HeapAlloc(GetProcessHeap(), 0,
921 sizeof(globalDebugDirW) + sizeof(buildidW) +
922 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(dotDebug0W));
923 z = p;
924 memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
925 z += sizeof(globalDebugDirW) / sizeof(WCHAR);
926 memcpy(z, buildidW, sizeof(buildidW));
927 z += sizeof(buildidW) / sizeof(WCHAR);
928
929 if (id < idend)
930 {
931 *z++ = "0123456789abcdef"[*id >> 4 ];
932 *z++ = "0123456789abcdef"[*id & 0x0F];
933 id++;
934 }
935 if (id < idend)
936 *z++ = '/';
937 while (id < idend)
938 {
939 *z++ = "0123456789abcdef"[*id >> 4 ];
940 *z++ = "0123456789abcdef"[*id & 0x0F];
941 id++;
942 }
943 memcpy(z, dotDebug0W, sizeof(dotDebug0W));
944 TRACE("checking %s\n", wine_dbgstr_w(p));
945
946 emfd.kind = from_file;
947 emfd.u.file.filename = p;
948 if (elf_map_file(&emfd, fmap_link))
949 {
950 struct image_section_map buildid_sect;
951 if (elf_find_section(fmap_link, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
952 {
953 const uint32_t* note;
954
955 note = (const uint32_t*)image_map_section(&buildid_sect);
956 if (note != IMAGE_NO_MAP)
957 {
958 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
959 if (note[2] == NT_GNU_BUILD_ID)
960 {
961 if (note[1] == idlen &&
962 !memcmp(note + 3 + ((note[0] + 3) >> 2), idend - idlen, idlen))
963 {
964 TRACE("Located debug information file at %s\n", debugstr_w(p));
965 HeapFree(GetProcessHeap(), 0, p);
966 fmap->u.elf.alternate = fmap_link;
967 return TRUE;
968 }
969 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p));
970 }
971 }
972 image_unmap_section(&buildid_sect);
973 }
974 elf_unmap_file(fmap_link);
975 }
976
977 TRACE("not found\n");
978 HeapFree(GetProcessHeap(), 0, p);
979 HeapFree(GetProcessHeap(), 0, fmap_link);
980 return FALSE;
981 }
982
983 /******************************************************************
984 * elf_check_alternate
985 *
986 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
987 * or .gnu_debuglink sections.
988 */
989 static BOOL elf_check_alternate(struct image_file_map* fmap, const struct module* module)
990 {
991 BOOL ret = FALSE;
992 BOOL found = FALSE;
993 struct image_section_map buildid_sect, debuglink_sect;
994
995 /* if present, add the .gnu_debuglink file as an alternate to current one */
996 if (elf_find_section(fmap, ".note.gnu.build-id", SHT_NULL, &buildid_sect))
997 {
998 const uint32_t* note;
999
1000 found = TRUE;
1001 note = (const uint32_t*)image_map_section(&buildid_sect);
1002 if (note != IMAGE_NO_MAP)
1003 {
1004 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1005 if (note[2] == NT_GNU_BUILD_ID)
1006 {
1007 ret = elf_locate_build_id_target(fmap, (const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
1008 }
1009 }
1010 image_unmap_section(&buildid_sect);
1011 }
1012 /* if present, add the .gnu_debuglink file as an alternate to current one */
1013 if (!ret && elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
1014 {
1015 const char* dbg_link;
1016
1017 found = TRUE;
1018 dbg_link = (const char*)image_map_section(&debuglink_sect);
1019 if (dbg_link != IMAGE_NO_MAP)
1020 {
1021 /* The content of a debug link section is:
1022 * 1/ a NULL terminated string, containing the file name for the
1023 * debug info
1024 * 2/ padding on 4 byte boundary
1025 * 3/ CRC of the linked ELF file
1026 */
1027 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
1028 ret = elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
1029 if (!ret)
1030 WARN("Couldn't load linked debug file for %s\n",
1031 debugstr_w(module->module.ModuleName));
1032 }
1033 image_unmap_section(&debuglink_sect);
1034 }
1035 return found ? ret : TRUE;
1036 }
1037
1038 /******************************************************************
1039 * elf_load_debug_info_from_map
1040 *
1041 * Loads the symbolic information from ELF module which mapping is described
1042 * in fmap
1043 * the module has been loaded at 'load_offset' address, so symbols' address
1044 * relocation is performed.
1045 * CRC is checked if fmap->with_crc is TRUE
1046 * returns
1047 * 0 if the file doesn't contain symbolic info (or this info cannot be
1048 * read or parsed)
1049 * 1 on success
1050 */
1051 static BOOL elf_load_debug_info_from_map(struct module* module,
1052 struct image_file_map* fmap,
1053 struct pool* pool,
1054 struct hash_table* ht_symtab)
1055 {
1056 BOOL ret = FALSE, lret;
1057 struct elf_thunk_area thunks[] =
1058 {
1059 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
1060 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1061 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1062 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1063 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1064 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1065 {NULL, 0, 0, 0}
1066 };
1067
1068 module->module.SymType = SymExport;
1069
1070 /* create a hash table for the symtab */
1071 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
1072
1073 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1074 {
1075 struct image_section_map stab_sect, stabstr_sect;
1076
1077 /* check if we need an alternate file (from debuglink or build-id) */
1078 ret = elf_check_alternate(fmap, module);
1079
1080 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
1081 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
1082 {
1083 const char* stab;
1084 const char* stabstr;
1085
1086 stab = image_map_section(&stab_sect);
1087 stabstr = image_map_section(&stabstr_sect);
1088 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
1089 {
1090 /* OK, now just parse all of the stabs. */
1091 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
1092 stab, image_get_map_size(&stab_sect),
1093 stabstr, image_get_map_size(&stabstr_sect),
1094 NULL, NULL);
1095 if (lret)
1096 /* and fill in the missing information for stabs */
1097 elf_finish_stabs_info(module, ht_symtab);
1098 else
1099 WARN("Couldn't correctly read stabs\n");
1100 ret = ret || lret;
1101 }
1102 image_unmap_section(&stab_sect);
1103 image_unmap_section(&stabstr_sect);
1104 }
1105 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
1106 ret = ret || lret;
1107 }
1108 if (strstrW(module->module.ModuleName, S_ElfW) ||
1109 !strcmpW(module->module.ModuleName, S_WineLoaderW))
1110 {
1111 /* add the thunks for native libraries */
1112 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1113 elf_new_wine_thunks(module, ht_symtab, thunks);
1114 }
1115 /* add all the public symbols from symtab */
1116 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1117
1118 return ret;
1119 }
1120
1121 /******************************************************************
1122 * elf_load_debug_info
1123 *
1124 * Loads ELF debugging information from the module image file.
1125 */
1126 BOOL elf_load_debug_info(struct module* module)
1127 {
1128 BOOL ret = TRUE;
1129 struct pool pool;
1130 struct hash_table ht_symtab;
1131 struct module_format* modfmt;
1132
1133 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1134 {
1135 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1136 return FALSE;
1137 }
1138
1139 pool_init(&pool, 65536);
1140 hash_table_init(&pool, &ht_symtab, 256);
1141
1142 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1143
1144 pool_destroy(&pool);
1145 return ret;
1146 }
1147
1148 /******************************************************************
1149 * elf_fetch_file_info
1150 *
1151 * Gathers some more information for an ELF module from a given file
1152 */
1153 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1154 DWORD* size, DWORD* checksum)
1155 {
1156 struct image_file_map fmap;
1157
1158 struct elf_map_file_data emfd;
1159
1160 emfd.kind = from_file;
1161 emfd.u.file.filename = name;
1162 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1163 if (base) *base = fmap.u.elf.elf_start;
1164 *size = fmap.u.elf.elf_size;
1165 *checksum = calc_crc32(fmap.u.elf.fd);
1166 elf_unmap_file(&fmap);
1167 return TRUE;
1168 }
1169
1170 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1171 struct image_file_map* fmap, unsigned long load_offset,
1172 unsigned long dyn_addr, struct elf_info* elf_info)
1173 {
1174 BOOL ret = FALSE;
1175
1176 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1177 {
1178 struct image_section_map ism;
1179
1180 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1181 {
1182 Elf_Dyn dyn;
1183 char* ptr = (char*)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1184 unsigned long len;
1185
1186 do
1187 {
1188 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1189 len != sizeof(dyn))
1190 return ret;
1191 if (dyn.d_tag == DT_DEBUG)
1192 {
1193 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1194 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1195 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1196 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1197 break;
1198 }
1199 ptr += sizeof(dyn);
1200 } while (dyn.d_tag != DT_NULL);
1201 if (dyn.d_tag == DT_NULL) return ret;
1202 }
1203 elf_end_find(fmap);
1204 }
1205
1206 if (elf_info->flags & ELF_INFO_MODULE)
1207 {
1208 struct elf_module_info *elf_module_info;
1209 struct module_format* modfmt;
1210 struct image_section_map ism;
1211 unsigned long modbase = load_offset;
1212
1213 if (elf_find_section(fmap, ".dynamic", SHT_DYNAMIC, &ism))
1214 {
1215 unsigned long rva_dyn = elf_get_map_rva(&ism);
1216
1217 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1218 debugstr_w(filename), (unsigned long)fmap->u.elf.elf_start, rva_dyn,
1219 load_offset, dyn_addr);
1220 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1221 {
1222 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1223 modbase = dyn_addr - rva_dyn;
1224 }
1225 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1226 elf_end_find(fmap);
1227
1228 modfmt = HeapAlloc(GetProcessHeap(), 0,
1229 sizeof(struct module_format) + sizeof(struct elf_module_info));
1230 if (!modfmt) return FALSE;
1231 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1232 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.fd));
1233 if (!elf_info->module)
1234 {
1235 HeapFree(GetProcessHeap(), 0, modfmt);
1236 return FALSE;
1237 }
1238 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1239 elf_module_info = (void*)(modfmt + 1);
1240 elf_info->module->format_info[DFI_ELF] = modfmt;
1241 modfmt->module = elf_info->module;
1242 modfmt->remove = elf_module_remove;
1243 modfmt->loc_compute = NULL;
1244 modfmt->u.elf_info = elf_module_info;
1245
1246 elf_module_info->elf_addr = load_offset;
1247
1248 elf_module_info->file_map = *fmap;
1249 elf_reset_file_map(fmap);
1250 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1251 {
1252 elf_info->module->module.SymType = SymDeferred;
1253 ret = TRUE;
1254 }
1255 else ret = elf_load_debug_info(elf_info->module);
1256
1257 elf_module_info->elf_mark = 1;
1258 elf_module_info->elf_loader = 0;
1259 } else ret = TRUE;
1260
1261 if (elf_info->flags & ELF_INFO_NAME)
1262 {
1263 WCHAR* ptr;
1264 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1265 if (ptr)
1266 {
1267 strcpyW(ptr, filename);
1268 elf_info->module_name = ptr;
1269 }
1270 else ret = FALSE;
1271 }
1272
1273 return ret;
1274 }
1275
1276 /******************************************************************
1277 * elf_load_file
1278 *
1279 * Loads the information for ELF module stored in 'filename'
1280 * the module has been loaded at 'load_offset' address
1281 * returns
1282 * -1 if the file cannot be found/opened
1283 * 0 if the file doesn't contain symbolic info (or this info cannot be
1284 * read or parsed)
1285 * 1 on success
1286 */
1287 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1288 unsigned long load_offset, unsigned long dyn_addr,
1289 struct elf_info* elf_info)
1290 {
1291 BOOL ret = FALSE;
1292 struct image_file_map fmap;
1293 struct elf_map_file_data emfd;
1294
1295 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1296
1297 emfd.kind = from_file;
1298 emfd.u.file.filename = filename;
1299 if (!elf_map_file(&emfd, &fmap)) return ret;
1300
1301 /* Next, we need to find a few of the internal ELF headers within
1302 * this thing. We need the main executable header, and the section
1303 * table.
1304 */
1305 if (!fmap.u.elf.elf_start && !load_offset)
1306 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1307 debugstr_w(filename));
1308
1309 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1310
1311 elf_unmap_file(&fmap);
1312
1313 return ret;
1314 }
1315
1316 /******************************************************************
1317 * elf_load_file_from_path
1318 * tries to load an ELF file from a set of paths (separated by ':')
1319 */
1320 static BOOL elf_load_file_from_path(HANDLE hProcess,
1321 const WCHAR* filename,
1322 unsigned long load_offset,
1323 unsigned long dyn_addr,
1324 const char* path,
1325 struct elf_info* elf_info)
1326 {
1327 BOOL ret = FALSE;
1328 WCHAR *s, *t, *fn;
1329 WCHAR* pathW = NULL;
1330 unsigned len;
1331
1332 if (!path) return FALSE;
1333
1334 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1335 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1336 if (!pathW) return FALSE;
1337 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1338
1339 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1340 {
1341 t = strchrW(s, ':');
1342 if (t) *t = '\0';
1343 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1344 if (!fn) break;
1345 strcpyW(fn, s);
1346 strcatW(fn, S_SlashW);
1347 strcatW(fn, filename);
1348 ret = elf_load_file(hProcess, fn, load_offset, dyn_addr, elf_info);
1349 HeapFree(GetProcessHeap(), 0, fn);
1350 if (ret) break;
1351 }
1352
1353 HeapFree(GetProcessHeap(), 0, pathW);
1354 return ret;
1355 }
1356
1357 /******************************************************************
1358 * elf_load_file_from_dll_path
1359 *
1360 * Tries to load an ELF file from the dll path
1361 */
1362 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1363 const WCHAR* filename,
1364 unsigned long load_offset,
1365 unsigned long dyn_addr,
1366 struct elf_info* elf_info)
1367 {
1368 BOOL ret = FALSE;
1369 unsigned int index = 0;
1370 const char *path;
1371
1372 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1373 {
1374 WCHAR *name;
1375 unsigned len;
1376
1377 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1378
1379 name = HeapAlloc( GetProcessHeap(), 0,
1380 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1381
1382 if (!name) break;
1383 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1384 strcatW( name, S_SlashW );
1385 strcatW( name, filename );
1386 ret = elf_load_file(hProcess, name, load_offset, dyn_addr, elf_info);
1387 HeapFree( GetProcessHeap(), 0, name );
1388 }
1389 return ret;
1390 }
1391
1392 #ifdef AT_SYSINFO_EHDR
1393 /******************************************************************
1394 * elf_search_auxv
1395 *
1396 * locate some a value from the debuggee auxiliary vector
1397 */
1398 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, unsigned long* val)
1399 {
1400 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1401 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1402 void* addr;
1403 void* str;
1404 void* str_max;
1405 Elf_auxv_t auxv;
1406
1407 si->SizeOfStruct = sizeof(*si);
1408 si->MaxNameLen = MAX_SYM_NAME;
1409 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1410 !(addr = (void*)(DWORD_PTR)si->Address) ||
1411 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1412 !addr)
1413 {
1414 FIXME("can't find symbol in module\n");
1415 return FALSE;
1416 }
1417 /* walk through envp[] */
1418 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1419 str_max = (void*)(DWORD_PTR)~0L;
1420 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1421 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1422 str_max = min(str_max, str);
1423
1424 /* Walk through the end of envp[] array.
1425 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1426 * deleted, the last entry is replaced by an extra NULL.
1427 */
1428 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1429 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1430
1431 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type != AT_NULL)
1432 {
1433 if (auxv.a_type == type)
1434 {
1435 *val = auxv.a_un.a_val;
1436 return TRUE;
1437 }
1438 addr = (void*)((DWORD_PTR)addr + sizeof(auxv));
1439 }
1440
1441 return FALSE;
1442 }
1443 #endif
1444
1445 /******************************************************************
1446 * elf_search_and_load_file
1447 *
1448 * lookup a file in standard ELF locations, and if found, load it
1449 */
1450 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1451 unsigned long load_offset, unsigned long dyn_addr,
1452 struct elf_info* elf_info)
1453 {
1454 BOOL ret = FALSE;
1455 struct module* module;
1456 static const WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1457
1458 if (filename == NULL || *filename == '\0') return FALSE;
1459 if ((module = module_is_already_loaded(pcs, filename)))
1460 {
1461 elf_info->module = module;
1462 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1463 return module->module.SymType;
1464 }
1465
1466 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1467 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1468 /* if relative pathname, try some absolute base dirs */
1469 if (!ret && !strchrW(filename, '/'))
1470 {
1471 ret = elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1472 getenv("PATH"), elf_info) ||
1473 elf_load_file_from_path(pcs, filename, load_offset, dyn_addr,
1474 getenv("LD_LIBRARY_PATH"), elf_info);
1475 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename,
1476 load_offset, dyn_addr, elf_info);
1477 }
1478
1479 return ret;
1480 }
1481
1482 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, unsigned long load_addr,
1483 unsigned long dyn_addr, BOOL is_system, void* user);
1484
1485 /******************************************************************
1486 * elf_enum_modules_internal
1487 *
1488 * Enumerate ELF modules from a running process
1489 */
1490 static BOOL elf_enum_modules_internal(const struct process* pcs,
1491 const WCHAR* main_name,
1492 enum_elf_modules_cb cb, void* user)
1493 {
1494 struct r_debug dbg_hdr;
1495 void* lm_addr;
1496 struct link_map lm;
1497 char bufstr[256];
1498 WCHAR bufstrW[MAX_PATH];
1499
1500 if (!pcs->dbg_hdr_addr ||
1501 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1502 &dbg_hdr, sizeof(dbg_hdr), NULL))
1503 return FALSE;
1504
1505 /* Now walk the linked list. In all known ELF implementations,
1506 * the dynamic loader maintains this linked list for us. In some
1507 * cases the first entry doesn't appear with a name, in other cases it
1508 * does.
1509 */
1510 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1511 {
1512 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1513 return FALSE;
1514
1515 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1516 lm.l_name != NULL &&
1517 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1518 {
1519 bufstr[sizeof(bufstr) - 1] = '\0';
1520 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1521 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1522 if (!cb(bufstrW, (unsigned long)lm.l_addr, (unsigned long)lm.l_ld, FALSE, user)) break;
1523 }
1524 }
1525
1526 #ifdef AT_SYSINFO_EHDR
1527 if (!lm_addr)
1528 {
1529 unsigned long ehdr_addr;
1530
1531 if (elf_search_auxv(pcs, AT_SYSINFO_EHDR, &ehdr_addr))
1532 {
1533 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1534 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1535 }
1536 }
1537 #endif
1538 return TRUE;
1539 }
1540
1541 /******************************************************************
1542 * elf_search_loader
1543 *
1544 * Lookup in a running ELF process the loader, and sets its ELF link
1545 * address (for accessing the list of loaded .so libs) in pcs.
1546 * If flags is ELF_INFO_MODULE, the module for the loader is also
1547 * added as a module into pcs.
1548 */
1549 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1550 {
1551 return elf_search_and_load_file(pcs, get_wine_loader_name(), 0, 0, elf_info);
1552 }
1553
1554 /******************************************************************
1555 * elf_read_wine_loader_dbg_info
1556 *
1557 * Try to find a decent wine executable which could have loaded the debuggee
1558 */
1559 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1560 {
1561 struct elf_info elf_info;
1562
1563 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1564 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1565 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1566 module_set_module(elf_info.module, S_WineLoaderW);
1567 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1568 }
1569
1570 struct elf_enum_user
1571 {
1572 enum_modules_cb cb;
1573 void* user;
1574 };
1575
1576 static BOOL elf_enum_modules_translate(const WCHAR* name, unsigned long load_addr,
1577 unsigned long dyn_addr, BOOL is_system, void* user)
1578 {
1579 struct elf_enum_user* eeu = user;
1580 return eeu->cb(name, load_addr, eeu->user);
1581 }
1582
1583 /******************************************************************
1584 * elf_enum_modules
1585 *
1586 * Enumerates the ELF loaded modules from a running target (hProc)
1587 * This function doesn't require that someone has called SymInitialize
1588 * on this very process.
1589 */
1590 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1591 {
1592 struct process pcs;
1593 struct elf_info elf_info;
1594 BOOL ret;
1595 struct elf_enum_user eeu;
1596
1597 memset(&pcs, 0, sizeof(pcs));
1598 pcs.handle = hProc;
1599 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1600 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1601 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1602 eeu.cb = cb;
1603 eeu.user = user;
1604 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, elf_enum_modules_translate, &eeu);
1605 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1606 return ret;
1607 }
1608
1609 struct elf_load
1610 {
1611 struct process* pcs;
1612 struct elf_info elf_info;
1613 const WCHAR* name;
1614 BOOL ret;
1615 };
1616
1617 /******************************************************************
1618 * elf_load_cb
1619 *
1620 * Callback for elf_load_module, used to walk the list of loaded
1621 * modules.
1622 */
1623 static BOOL elf_load_cb(const WCHAR* name, unsigned long load_addr,
1624 unsigned long dyn_addr, BOOL is_system, void* user)
1625 {
1626 struct elf_load* el = user;
1627 BOOL ret = TRUE;
1628 const WCHAR* p;
1629
1630 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1631 {
1632 struct module* module;
1633 struct elf_map_file_data emfd;
1634 struct image_file_map fmap;
1635
1636 if ((module = module_is_already_loaded(el->pcs, name)))
1637 {
1638 el->elf_info.module = module;
1639 el->elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1640 return module->module.SymType;
1641 }
1642
1643 emfd.kind = from_process;
1644 emfd.u.process.handle = el->pcs->handle;
1645 emfd.u.process.load_addr = (void*)load_addr;
1646
1647 if (elf_map_file(&emfd, &fmap))
1648 el->ret = elf_load_file_from_fmap(el->pcs, name, &fmap, load_addr, 0, &el->elf_info);
1649 return TRUE;
1650 }
1651 if (el->name)
1652 {
1653 /* memcmp is needed for matches when bufstr contains also version information
1654 * el->name: libc.so, name: libc.so.6.0
1655 */
1656 p = strrchrW(name, '/');
1657 if (!p++) p = name;
1658 }
1659
1660 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1661 {
1662 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1663 if (el->name) ret = FALSE;
1664 }
1665
1666 return ret;
1667 }
1668
1669 /******************************************************************
1670 * elf_load_module
1671 *
1672 * loads an ELF module and stores it in process' module list
1673 * Also, find module real name and load address from
1674 * the real loaded modules list in pcs address space
1675 */
1676 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1677 {
1678 struct elf_load el;
1679
1680 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1681
1682 el.elf_info.flags = ELF_INFO_MODULE;
1683 el.ret = FALSE;
1684
1685 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1686 {
1687 el.pcs = pcs;
1688 /* do only the lookup from the filename, not the path (as we lookup module
1689 * name in the process' loaded module list)
1690 */
1691 el.name = strrchrW(name, '/');
1692 if (!el.name++) el.name = name;
1693 el.ret = FALSE;
1694
1695 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1696 return NULL;
1697 }
1698 else if (addr)
1699 {
1700 el.name = name;
1701 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1702 }
1703 if (!el.ret) return NULL;
1704 assert(el.elf_info.module);
1705 return el.elf_info.module;
1706 }
1707
1708 /******************************************************************
1709 * elf_synchronize_module_list
1710 *
1711 * this function rescans the debuggee module's list and synchronizes it with
1712 * the one from 'pcs', i.e.:
1713 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1714 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1715 */
1716 BOOL elf_synchronize_module_list(struct process* pcs)
1717 {
1718 struct module* module;
1719 struct elf_load el;
1720
1721 for (module = pcs->lmodules; module; module = module->next)
1722 {
1723 if (module->type == DMT_ELF && !module->is_virtual)
1724 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1725 }
1726
1727 el.pcs = pcs;
1728 el.elf_info.flags = ELF_INFO_MODULE;
1729 el.ret = FALSE;
1730 el.name = NULL; /* fetch all modules */
1731
1732 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1733 return FALSE;
1734
1735 module = pcs->lmodules;
1736 while (module)
1737 {
1738 if (module->type == DMT_ELF && !module->is_virtual)
1739 {
1740 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1741
1742 if (!elf_info->elf_mark && !elf_info->elf_loader)
1743 {
1744 module_remove(pcs, module);
1745 /* restart all over */
1746 module = pcs->lmodules;
1747 continue;
1748 }
1749 }
1750 module = module->next;
1751 }
1752 return TRUE;
1753 }
1754
1755 #else /* !__ELF__ */
1756
1757 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1758 unsigned sht, struct image_section_map* ism)
1759 {
1760 return FALSE;
1761 }
1762
1763 const char* elf_map_section(struct image_section_map* ism)
1764 {
1765 return NULL;
1766 }
1767
1768 void elf_unmap_section(struct image_section_map* ism)
1769 {}
1770
1771 unsigned elf_get_map_size(const struct image_section_map* ism)
1772 {
1773 return 0;
1774 }
1775
1776 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1777 {
1778 return 0;
1779 }
1780
1781 BOOL elf_synchronize_module_list(struct process* pcs)
1782 {
1783 return FALSE;
1784 }
1785
1786 BOOL elf_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1787 DWORD* size, DWORD* checksum)
1788 {
1789 return FALSE;
1790 }
1791
1792 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1793 {
1794 return FALSE;
1795 }
1796
1797 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1798 {
1799 return FALSE;
1800 }
1801
1802 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1803 {
1804 return NULL;
1805 }
1806
1807 BOOL elf_load_debug_info(struct module* module)
1808 {
1809 return FALSE;
1810 }
1811
1812 int elf_is_in_thunk_area(unsigned long addr,
1813 const struct elf_thunk_area* thunks)
1814 {
1815 return -1;
1816 }
1817 #endif /* __ELF__ */