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