d3d10/effect: Use case-insensitive comparison in GetMemberTypeBySemantic().
[wine.git] / dlls / dbghelp / elf_module.c
blob553b212c526a82341bbe080c1a6e099d938c1090
1 /*
2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2007 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include "dbghelp_private.h"
27 #include "image_private.h"
28 #include "winternl.h"
30 #include "wine/debug.h"
31 #include "wine/heap.h"
33 #define ELF_INFO_DEBUG_HEADER 0x0001
34 #define ELF_INFO_MODULE 0x0002
35 #define ELF_INFO_NAME 0x0004
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 struct elf_info
41 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
42 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
43 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
44 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
47 struct elf_sym32
49 UINT32 st_name; /* Symbol name (string tbl index) */
50 UINT32 st_value; /* Symbol value */
51 UINT32 st_size; /* Symbol size */
52 UINT8 st_info; /* Symbol type and binding */
53 UINT8 st_other; /* Symbol visibility */
54 UINT16 st_shndx; /* Section index */
57 struct elf_sym
59 UINT32 st_name; /* Symbol name (string tbl index) */
60 UINT8 st_info; /* Symbol type and binding */
61 UINT8 st_other; /* Symbol visibility */
62 UINT16 st_shndx; /* Section index */
63 UINT64 st_value; /* Symbol value */
64 UINT64 st_size; /* Symbol size */
67 struct symtab_elt
69 struct hash_table_elt ht_elt;
70 struct elf_sym sym;
71 struct symt_compiland* compiland;
72 unsigned used;
75 struct elf_thunk_area
77 const char* symname;
78 THUNK_ORDINAL ordinal;
79 ULONG_PTR rva_start;
80 ULONG_PTR rva_end;
83 struct elf_module_info
85 ULONG_PTR elf_addr;
86 unsigned short elf_mark : 1,
87 elf_loader : 1;
88 struct image_file_map file_map;
91 /* Legal values for sh_type (section type). */
92 #define ELF_SHT_NULL 0 /* Section header table entry unused */
93 #define ELF_SHT_PROGBITS 1 /* Program data */
94 #define ELF_SHT_SYMTAB 2 /* Symbol table */
95 #define ELF_SHT_STRTAB 3 /* String table */
96 #define ELF_SHT_RELA 4 /* Relocation entries with addends */
97 #define ELF_SHT_HASH 5 /* Symbol hash table */
98 #define ELF_SHT_DYNAMIC 6 /* Dynamic linking information */
99 #define ELF_SHT_NOTE 7 /* Notes */
100 #define ELF_SHT_NOBITS 8 /* Program space with no data (bss) */
101 #define ELF_SHT_REL 9 /* Relocation entries, no addends */
102 #define ELF_SHT_SHLIB 10 /* Reserved */
103 #define ELF_SHT_DYNSYM 11 /* Dynamic linker symbol table */
104 #define ELF_SHT_INIT_ARRAY 14 /* Array of constructors */
105 #define ELF_SHT_FINI_ARRAY 15 /* Array of destructors */
106 #define ELF_SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */
107 #define ELF_SHT_GROUP 17 /* Section group */
108 #define ELF_SHT_SYMTAB_SHNDX 18 /* Extended section indices */
109 #define ELF_SHT_NUM 19 /* Number of defined types. */
111 /* Legal values for ST_TYPE subfield of st_info (symbol type). */
112 #define ELF_STT_NOTYPE 0 /* Symbol type is unspecified */
113 #define ELF_STT_OBJECT 1 /* Symbol is a data object */
114 #define ELF_STT_FUNC 2 /* Symbol is a code object */
115 #define ELF_STT_SECTION 3 /* Symbol associated with a section */
116 #define ELF_STT_FILE 4 /* Symbol's name is file name */
118 #define ELF_PT_LOAD 1 /* Loadable program segment */
120 #define ELF_DT_DEBUG 21 /* For debugging; unspecified */
122 #define ELF_AT_SYSINFO_EHDR 33
124 static DWORD elf_get_machine(unsigned mach)
126 switch (mach)
128 default:
129 FIXME("No mapping yet for ELF e_machine %u\n", mach);
130 /* fall through */
131 case /*EM_NONE*/ 0: return IMAGE_FILE_MACHINE_UNKNOWN;
132 case /*EM_386*/ 3: return IMAGE_FILE_MACHINE_I386;
133 case /*EM_ARM*/ 40: return IMAGE_FILE_MACHINE_ARMNT;
134 case /*EM_X86_64*/ 62: return IMAGE_FILE_MACHINE_AMD64;
135 case /*EM_AARCH64*/ 183: return IMAGE_FILE_MACHINE_ARM64;
139 /******************************************************************
140 * elf_map_section
142 * Maps a single section into memory from an ELF file
144 static const char* elf_map_section(struct image_section_map* ism)
146 struct elf_file_map* fmap = &ism->fmap->u.elf;
147 SIZE_T ofst, size;
148 HANDLE mapping;
150 assert(ism->fmap->modtype == DMT_ELF);
151 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
152 fmap->sect[ism->sidx].shdr.sh_type == ELF_SHT_NOBITS)
153 return IMAGE_NO_MAP;
155 if (fmap->target_copy)
157 return fmap->target_copy + fmap->sect[ism->sidx].shdr.sh_offset;
160 /* align required information on allocation granularity */
161 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(sysinfo.dwAllocationGranularity - 1);
162 size = fmap->sect[ism->sidx].shdr.sh_offset + fmap->sect[ism->sidx].shdr.sh_size - ofst;
163 if (!(mapping = CreateFileMappingW(fmap->handle, NULL, PAGE_READONLY, 0, ofst + size, NULL)))
165 ERR("map creation %p failed %u offset %lu %lu size %lu\n", fmap->handle, GetLastError(), ofst, ofst % 4096, size);
166 return IMAGE_NO_MAP;
168 fmap->sect[ism->sidx].mapped = MapViewOfFile(mapping, FILE_MAP_READ, 0, ofst, size);
169 CloseHandle(mapping);
170 if (!fmap->sect[ism->sidx].mapped)
172 ERR("map %p failed %u offset %lu %lu size %lu\n", fmap->handle, GetLastError(), ofst, ofst % 4096, size);
173 return IMAGE_NO_MAP;
175 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (sysinfo.dwAllocationGranularity - 1));
178 /******************************************************************
179 * elf_find_section
181 * Finds a section by name (and type) into memory from an ELF file
182 * or its alternate if any
184 static BOOL elf_find_section(struct image_file_map* _fmap, const char* name, struct image_section_map* ism)
186 struct elf_file_map* fmap = &_fmap->u.elf;
187 unsigned i;
189 if (fmap->shstrtab == IMAGE_NO_MAP)
191 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
192 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) return FALSE;
194 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
196 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0)
198 ism->fmap = _fmap;
199 ism->sidx = i;
200 return TRUE;
203 return FALSE;
206 static BOOL elf_find_section_type(struct image_file_map* _fmap, const char* name, unsigned sht, struct image_section_map* ism)
208 struct elf_file_map* fmap;
209 unsigned i;
211 while (_fmap)
213 if (_fmap->modtype != DMT_ELF) break;
214 fmap = &_fmap->u.elf;
215 if (fmap->shstrtab == IMAGE_NO_MAP)
217 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
218 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
220 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
222 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 && sht == fmap->sect[i].shdr.sh_type)
224 ism->fmap = _fmap;
225 ism->sidx = i;
226 return TRUE;
229 _fmap = _fmap->alternate;
231 ism->fmap = NULL;
232 ism->sidx = -1;
233 return FALSE;
236 /******************************************************************
237 * elf_unmap_section
239 * Unmaps a single section from memory
241 static void elf_unmap_section(struct image_section_map* ism)
243 struct elf_file_map* fmap = &ism->fmap->u.elf;
245 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && !fmap->target_copy &&
246 fmap->sect[ism->sidx].mapped)
248 if (!UnmapViewOfFile(fmap->sect[ism->sidx].mapped))
249 WARN("Couldn't unmap the section\n");
250 fmap->sect[ism->sidx].mapped = NULL;
254 static void elf_end_find(struct image_file_map* fmap)
256 struct image_section_map ism;
258 while (fmap && fmap->modtype == DMT_ELF)
260 ism.fmap = fmap;
261 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
262 elf_unmap_section(&ism);
263 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
264 fmap = fmap->alternate;
268 /******************************************************************
269 * elf_get_map_rva
271 * Get the RVA of an ELF section
273 static DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
275 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
276 return 0;
277 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
280 /******************************************************************
281 * elf_get_map_size
283 * Get the size of an ELF section
285 static unsigned elf_get_map_size(const struct image_section_map* ism)
287 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
288 return 0;
289 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
292 /******************************************************************
293 * elf_unmap_file
295 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
297 static void elf_unmap_file(struct image_file_map* fmap)
299 if (fmap->u.elf.handle != INVALID_HANDLE_VALUE)
301 struct image_section_map ism;
302 ism.fmap = fmap;
303 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
305 elf_unmap_section(&ism);
307 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
308 CloseHandle(fmap->u.elf.handle);
310 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
313 static const struct image_file_map_ops elf_file_map_ops =
315 elf_map_section,
316 elf_unmap_section,
317 elf_find_section,
318 elf_get_map_rva,
319 elf_get_map_size,
320 elf_unmap_file,
323 static inline void elf_reset_file_map(struct image_file_map* fmap)
325 fmap->ops = &elf_file_map_ops;
326 fmap->alternate = NULL;
327 fmap->u.elf.handle = INVALID_HANDLE_VALUE;
328 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
329 fmap->u.elf.target_copy = NULL;
332 struct elf_map_file_data
334 enum {from_file, from_process, from_handle} kind;
335 union
337 struct
339 const WCHAR* filename;
340 } file;
341 struct
343 HANDLE handle;
344 void* load_addr;
345 } process;
346 HANDLE handle;
347 } u;
350 static BOOL elf_map_file_read(struct image_file_map* fmap, struct elf_map_file_data* emfd,
351 void* buf, size_t len, size_t off)
353 LARGE_INTEGER li;
354 DWORD bytes_read;
355 SIZE_T dw;
357 switch (emfd->kind)
359 case from_file:
360 case from_handle:
361 li.QuadPart = off;
362 if (!SetFilePointerEx(fmap->u.elf.handle, li, NULL, FILE_BEGIN)) return FALSE;
363 return ReadFile(fmap->u.elf.handle, buf, len, &bytes_read, NULL);
364 case from_process:
365 return ReadProcessMemory(emfd->u.process.handle,
366 (void*)((ULONG_PTR)emfd->u.process.load_addr + (ULONG_PTR)off),
367 buf, len, &dw) && dw == len;
368 default:
369 assert(0);
370 return FALSE;
374 static BOOL elf_map_shdr(struct elf_map_file_data* emfd, struct image_file_map* fmap, unsigned int i)
376 if (fmap->addr_size == 32)
378 struct
380 UINT32 sh_name; /* Section name (string tbl index) */
381 UINT32 sh_type; /* Section type */
382 UINT32 sh_flags; /* Section flags */
383 UINT32 sh_addr; /* Section virtual addr at execution */
384 UINT32 sh_offset; /* Section file offset */
385 UINT32 sh_size; /* Section size in bytes */
386 UINT32 sh_link; /* Link to another section */
387 UINT32 sh_info; /* Additional section information */
388 UINT32 sh_addralign; /* Section alignment */
389 UINT32 sh_entsize; /* Entry size if section holds table */
390 } shdr32;
392 if (!elf_map_file_read(fmap, emfd, &shdr32, sizeof(shdr32),
393 fmap->u.elf.elfhdr.e_shoff + i * sizeof(shdr32)))
394 return FALSE;
396 fmap->u.elf.sect[i].shdr.sh_name = shdr32.sh_name;
397 fmap->u.elf.sect[i].shdr.sh_type = shdr32.sh_type;
398 fmap->u.elf.sect[i].shdr.sh_flags = shdr32.sh_flags;
399 fmap->u.elf.sect[i].shdr.sh_addr = shdr32.sh_addr;
400 fmap->u.elf.sect[i].shdr.sh_offset = shdr32.sh_offset;
401 fmap->u.elf.sect[i].shdr.sh_size = shdr32.sh_size;
402 fmap->u.elf.sect[i].shdr.sh_link = shdr32.sh_link;
403 fmap->u.elf.sect[i].shdr.sh_info = shdr32.sh_info;
404 fmap->u.elf.sect[i].shdr.sh_addralign = shdr32.sh_addralign;
405 fmap->u.elf.sect[i].shdr.sh_entsize = shdr32.sh_entsize;
407 else
409 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr),
410 fmap->u.elf.elfhdr.e_shoff + i * sizeof(fmap->u.elf.sect[i].shdr)))
411 return FALSE;
413 return TRUE;
416 /******************************************************************
417 * elf_map_file
419 * Maps an ELF file into memory (and checks it's a real ELF file)
421 static BOOL elf_map_file(struct elf_map_file_data* emfd, struct image_file_map* fmap)
423 unsigned int i;
424 size_t tmp, page_mask = sysinfo.dwPageSize - 1;
425 WCHAR *dos_path;
426 unsigned char e_ident[ARRAY_SIZE(fmap->u.elf.elfhdr.e_ident)];
428 elf_reset_file_map(fmap);
430 fmap->modtype = DMT_ELF;
431 fmap->u.elf.handle = INVALID_HANDLE_VALUE;
432 fmap->u.elf.target_copy = NULL;
434 switch (emfd->kind)
436 case from_file:
437 if (!(dos_path = get_dos_file_name(emfd->u.file.filename))) return FALSE;
438 fmap->u.elf.handle = CreateFileW(dos_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
439 heap_free(dos_path);
440 if (fmap->u.elf.handle == INVALID_HANDLE_VALUE) return FALSE;
441 break;
442 case from_handle:
443 if (!DuplicateHandle(GetCurrentProcess(), emfd->u.handle, GetCurrentProcess(), &fmap->u.elf.handle, GENERIC_READ, FALSE, 0))
444 return FALSE;
445 break;
446 case from_process:
447 break;
450 if (!elf_map_file_read(fmap, emfd, e_ident, sizeof(e_ident), 0))
451 return FALSE;
453 /* and check for an ELF header */
454 if (memcmp(e_ident, "\177ELF", 4))
455 return FALSE;
457 fmap->addr_size = e_ident[4] == 2 /* ELFCLASS64 */ ? 64 : 32;
459 if (fmap->addr_size == 32)
461 struct
463 UINT8 e_ident[16]; /* Magic number and other info */
464 UINT16 e_type; /* Object file type */
465 UINT16 e_machine; /* Architecture */
466 UINT32 e_version; /* Object file version */
467 UINT32 e_entry; /* Entry point virtual address */
468 UINT32 e_phoff; /* Program header table file offset */
469 UINT32 e_shoff; /* Section header table file offset */
470 UINT32 e_flags; /* Processor-specific flags */
471 UINT16 e_ehsize; /* ELF header size in bytes */
472 UINT16 e_phentsize; /* Program header table entry size */
473 UINT16 e_phnum; /* Program header table entry count */
474 UINT16 e_shentsize; /* Section header table entry size */
475 UINT16 e_shnum; /* Section header table entry count */
476 UINT16 e_shstrndx; /* Section header string table index */
477 } elfhdr32;
479 if (!elf_map_file_read(fmap, emfd, &elfhdr32, sizeof(elfhdr32), 0))
480 return FALSE;
482 memcpy(fmap->u.elf.elfhdr.e_ident, elfhdr32.e_ident, sizeof(e_ident));
483 fmap->u.elf.elfhdr.e_type = elfhdr32.e_type;
484 fmap->u.elf.elfhdr.e_machine = elfhdr32.e_machine;
485 fmap->u.elf.elfhdr.e_version = elfhdr32.e_version;
486 fmap->u.elf.elfhdr.e_entry = elfhdr32.e_entry;
487 fmap->u.elf.elfhdr.e_phoff = elfhdr32.e_phoff;
488 fmap->u.elf.elfhdr.e_shoff = elfhdr32.e_shoff;
489 fmap->u.elf.elfhdr.e_flags = elfhdr32.e_flags;
490 fmap->u.elf.elfhdr.e_ehsize = elfhdr32.e_ehsize;
491 fmap->u.elf.elfhdr.e_phentsize = elfhdr32.e_phentsize;
492 fmap->u.elf.elfhdr.e_phnum = elfhdr32.e_phnum;
493 fmap->u.elf.elfhdr.e_shentsize = elfhdr32.e_shentsize;
494 fmap->u.elf.elfhdr.e_shnum = elfhdr32.e_shnum;
495 fmap->u.elf.elfhdr.e_shstrndx = elfhdr32.e_shstrndx;
497 else
499 if (!elf_map_file_read(fmap, emfd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr), 0))
500 return FALSE;
503 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
504 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
505 if (!fmap->u.elf.sect) return FALSE;
507 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
509 if (!elf_map_shdr(emfd, fmap, i))
511 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
512 fmap->u.elf.sect = NULL;
513 return FALSE;
515 fmap->u.elf.sect[i].mapped = NULL;
518 /* grab size of module once loaded in memory */
519 fmap->u.elf.elf_size = 0;
520 fmap->u.elf.elf_start = ~0L;
521 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
523 if (fmap->addr_size == 32)
525 struct
527 UINT32 p_type; /* Segment type */
528 UINT32 p_offset; /* Segment file offset */
529 UINT32 p_vaddr; /* Segment virtual address */
530 UINT32 p_paddr; /* Segment physical address */
531 UINT32 p_filesz; /* Segment size in file */
532 UINT32 p_memsz; /* Segment size in memory */
533 UINT32 p_flags; /* Segment flags */
534 UINT32 p_align; /* Segment alignment */
535 } phdr;
537 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
538 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
539 phdr.p_type == ELF_PT_LOAD)
541 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
542 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
543 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
546 else
548 struct
550 UINT32 p_type; /* Segment type */
551 UINT32 p_flags; /* Segment flags */
552 UINT64 p_offset; /* Segment file offset */
553 UINT64 p_vaddr; /* Segment virtual address */
554 UINT64 p_paddr; /* Segment physical address */
555 UINT64 p_filesz; /* Segment size in file */
556 UINT64 p_memsz; /* Segment size in memory */
557 UINT64 p_align; /* Segment alignment */
558 } phdr;
560 if (elf_map_file_read(fmap, emfd, &phdr, sizeof(phdr),
561 fmap->u.elf.elfhdr.e_phoff + i * sizeof(phdr)) &&
562 phdr.p_type == ELF_PT_LOAD)
564 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
565 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
566 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
570 /* if non relocatable ELF, then remove fixed address from computation
571 * otherwise, all addresses are zero based and start has no effect
573 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
575 switch (emfd->kind)
577 case from_handle:
578 case from_file: break;
579 case from_process:
580 if (!(fmap->u.elf.target_copy = HeapAlloc(GetProcessHeap(), 0, fmap->u.elf.elf_size)))
582 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
583 return FALSE;
585 if (!ReadProcessMemory(emfd->u.process.handle, emfd->u.process.load_addr, fmap->u.elf.target_copy,
586 fmap->u.elf.elf_size, NULL))
588 HeapFree(GetProcessHeap(), 0, fmap->u.elf.target_copy);
589 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
590 return FALSE;
592 break;
594 return TRUE;
597 BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap)
599 struct elf_map_file_data emfd;
600 emfd.kind = from_handle;
601 emfd.u.handle = handle;
602 return elf_map_file(&emfd, fmap);
605 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
607 image_unmap_file(&modfmt->u.elf_info->file_map);
608 HeapFree(GetProcessHeap(), 0, modfmt);
611 /******************************************************************
612 * elf_is_in_thunk_area
614 * Check whether an address lies within one of the thunk area we
615 * know of.
617 int elf_is_in_thunk_area(ULONG_PTR addr,
618 const struct elf_thunk_area* thunks)
620 unsigned i;
622 if (thunks) for (i = 0; thunks[i].symname; i++)
624 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
625 return i;
627 return -1;
630 /******************************************************************
631 * elf_hash_symtab
633 * creating an internal hash table to ease use ELF symtab information lookup
635 static void elf_hash_symtab(struct module* module, struct pool* pool,
636 struct hash_table* ht_symtab, struct image_file_map* fmap,
637 struct elf_thunk_area* thunks)
639 int i, j, nsym;
640 const char* strp;
641 const char* symname;
642 struct symt_compiland* compiland = NULL;
643 const char* ptr;
644 struct symtab_elt* ste;
645 struct image_section_map ism, ism_str;
646 const char *symtab;
648 if (!elf_find_section_type(fmap, ".symtab", ELF_SHT_SYMTAB, &ism) &&
649 !elf_find_section_type(fmap, ".dynsym", ELF_SHT_DYNSYM, &ism)) return;
650 if ((symtab = image_map_section(&ism)) == IMAGE_NO_MAP) return;
651 ism_str.fmap = ism.fmap;
652 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
653 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
655 image_unmap_section(&ism);
656 return;
659 nsym = image_get_map_size(&ism) /
660 (fmap->addr_size == 32 ? sizeof(struct elf_sym32) : sizeof(struct elf_sym));
662 for (j = 0; thunks[j].symname; j++)
663 thunks[j].rva_start = thunks[j].rva_end = 0;
665 for (i = 0; i < nsym; i++)
667 struct elf_sym sym;
668 unsigned int type;
670 if (fmap->addr_size == 32)
672 struct elf_sym32 *sym32 = &((struct elf_sym32 *)symtab)[i];
674 sym.st_name = sym32->st_name;
675 sym.st_value = sym32->st_value;
676 sym.st_size = sym32->st_size;
677 sym.st_info = sym32->st_info;
678 sym.st_other = sym32->st_other;
679 sym.st_shndx = sym32->st_shndx;
681 else
682 sym = ((struct elf_sym *)symtab)[i];
684 type = sym.st_info & 0xf;
686 /* Ignore certain types of entries which really aren't of that much
687 * interest.
689 if ((type != ELF_STT_NOTYPE && type != ELF_STT_FILE && type != ELF_STT_OBJECT && type != ELF_STT_FUNC)
690 || !sym.st_shndx)
692 continue;
695 symname = strp + sym.st_name;
697 /* handle some specific symtab (that we'll throw away when done) */
698 switch (type)
700 case ELF_STT_FILE:
701 if (symname)
702 compiland = symt_new_compiland(module, sym.st_value,
703 source_new(module, NULL, symname));
704 else
705 compiland = NULL;
706 continue;
707 case ELF_STT_NOTYPE:
708 /* we are only interested in wine markers inserted by winebuild */
709 for (j = 0; thunks[j].symname; j++)
711 if (!strcmp(symname, thunks[j].symname))
713 thunks[j].rva_start = sym.st_value;
714 thunks[j].rva_end = sym.st_value + sym.st_size;
715 break;
718 continue;
721 /* FIXME: we don't need to handle them (GCC internals)
722 * Moreover, they screw up our symbol lookup :-/
724 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
725 continue;
727 ste = pool_alloc(pool, sizeof(*ste));
728 ste->ht_elt.name = symname;
729 /* GCC emits, in some cases, a .<digit>+ suffix.
730 * This is used for static variable inside functions, so
731 * that we can have several such variables with same name in
732 * the same compilation unit
733 * We simply ignore that suffix when present (we also get rid
734 * of it in stabs parsing)
736 ptr = symname + strlen(symname) - 1;
737 if (isdigit(*ptr))
739 while (isdigit(*ptr) && ptr >= symname) ptr--;
740 if (ptr > symname && *ptr == '.')
742 char* n = pool_alloc(pool, ptr - symname + 1);
743 memcpy(n, symname, ptr - symname + 1);
744 n[ptr - symname] = '\0';
745 ste->ht_elt.name = n;
748 ste->sym = sym;
749 ste->compiland = compiland;
750 ste->used = 0;
751 hash_table_add(ht_symtab, &ste->ht_elt);
753 /* as we added in the ht_symtab pointers to the symbols themselves,
754 * we cannot unmap yet the sections, it will be done when we're over
755 * with this ELF file
759 /******************************************************************
760 * elf_lookup_symtab
762 * lookup a symbol by name in our internal hash table for the symtab
764 static const struct elf_sym *elf_lookup_symtab(const struct module* module,
765 const struct hash_table* ht_symtab,
766 const char* name, const struct symt* compiland)
768 struct symtab_elt* weak_result = NULL; /* without compiland name */
769 struct symtab_elt* result = NULL;
770 struct hash_table_iter hti;
771 struct symtab_elt* ste;
772 const char* compiland_name;
773 const char* compiland_basename;
774 const char* base;
776 /* we need weak match up (at least) when symbols of same name,
777 * defined several times in different compilation units,
778 * are merged in a single one (hence a different filename for c.u.)
780 if (compiland)
782 compiland_name = source_get(module,
783 ((const struct symt_compiland*)compiland)->source);
784 compiland_basename = file_nameA(compiland_name);
786 else compiland_name = compiland_basename = NULL;
788 hash_table_iter_init(ht_symtab, &hti, name);
789 while ((ste = hash_table_iter_up(&hti)))
791 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
793 weak_result = ste;
794 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
795 continue;
796 if (ste->compiland && compiland_name)
798 const char* filename = source_get(module, ste->compiland->source);
799 if (strcmp(filename, compiland_name))
801 base = file_nameA(filename);
802 if (strcmp(base, compiland_basename)) continue;
805 if (result)
807 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
808 name, compiland_name,
809 source_get(module, result->compiland->source), (unsigned int)result->sym.st_value,
810 source_get(module, ste->compiland->source), (unsigned int)ste->sym.st_value);
812 else
814 result = ste;
815 ste->used = 1;
818 if (!result && !(result = weak_result))
820 FIXME("Couldn't find symbol %s!%s in symtab\n",
821 debugstr_w(module->modulename), name);
822 return NULL;
824 return &result->sym;
827 static BOOL elf_is_local_symbol(unsigned int info)
829 return !(info >> 4);
832 /******************************************************************
833 * elf_finish_stabs_info
835 * - get any relevant information (address & size) from the bits we got from the
836 * stabs debugging information
838 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
840 struct hash_table_iter hti;
841 void* ptr;
842 struct symt_ht* sym;
843 const struct elf_sym* symp;
844 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
846 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
847 while ((ptr = hash_table_iter_up(&hti)))
849 sym = CONTAINING_RECORD(ptr, struct symt_ht, hash_elt);
850 switch (sym->symt.tag)
852 case SymTagFunction:
853 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
854 ((struct symt_function*)sym)->size)
856 break;
858 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
859 ((struct symt_function*)sym)->container);
860 if (symp)
862 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
863 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
864 FIXME("Changing address for %p/%s!%s from %08lx to %s\n",
865 sym, debugstr_w(module->modulename), sym->hash_elt.name,
866 ((struct symt_function*)sym)->address,
867 wine_dbgstr_longlong(elf_info->elf_addr + symp->st_value));
868 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
869 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
870 sym, debugstr_w(module->modulename), sym->hash_elt.name,
871 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
873 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
874 ((struct symt_function*)sym)->size = symp->st_size;
875 } else
876 FIXME("Couldn't find %s!%s\n",
877 debugstr_w(module->modulename), sym->hash_elt.name);
878 break;
879 case SymTagData:
880 switch (((struct symt_data*)sym)->kind)
882 case DataIsGlobal:
883 case DataIsFileStatic:
884 if (((struct symt_data*)sym)->u.var.kind != loc_absolute ||
885 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
886 break;
887 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
888 ((struct symt_data*)sym)->container);
889 if (symp)
891 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
892 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
893 FIXME("Changing address for %p/%s!%s from %08lx to %s\n",
894 sym, debugstr_w(module->modulename), sym->hash_elt.name,
895 ((struct symt_function*)sym)->address,
896 wine_dbgstr_longlong(elf_info->elf_addr + symp->st_value));
897 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
898 ((struct symt_data*)sym)->kind = elf_is_local_symbol(symp->st_info) ?
899 DataIsFileStatic : DataIsGlobal;
900 } else
901 FIXME("Couldn't find %s!%s\n",
902 debugstr_w(module->modulename), sym->hash_elt.name);
903 break;
904 default:;
906 break;
907 default:
908 FIXME("Unsupported tag %u\n", sym->symt.tag);
909 break;
912 /* since we may have changed some addresses & sizes, mark the module to be resorted */
913 module->sortlist_valid = FALSE;
916 /******************************************************************
917 * elf_load_wine_thunks
919 * creating the thunk objects for a wine native DLL
921 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
922 const struct elf_thunk_area* thunks)
924 int j;
925 struct hash_table_iter hti;
926 struct symtab_elt* ste;
927 DWORD_PTR addr;
928 struct symt_ht* symt;
930 hash_table_iter_init(ht_symtab, &hti, NULL);
931 while ((ste = hash_table_iter_up(&hti)))
933 if (ste->used) continue;
935 addr = module->reloc_delta + ste->sym.st_value;
937 j = elf_is_in_thunk_area(ste->sym.st_value, thunks);
938 if (j >= 0) /* thunk found */
940 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
941 addr, ste->sym.st_size);
943 else
945 ULONG64 ref_addr;
946 struct location loc;
948 symt = symt_find_nearest(module, addr);
949 if (symt && !symt_get_address(&symt->symt, &ref_addr))
950 ref_addr = addr;
951 if (!symt || addr != ref_addr)
953 /* creating public symbols for all the ELF symbols which haven't been
954 * used yet (ie we have no debug information on them)
955 * That's the case, for example, of the .spec.c files
957 switch (ste->sym.st_info & 0xf)
959 case ELF_STT_FUNC:
960 symt_new_function(module, ste->compiland, ste->ht_elt.name,
961 addr, ste->sym.st_size, NULL);
962 break;
963 case ELF_STT_OBJECT:
964 loc.kind = loc_absolute;
965 loc.reg = 0;
966 loc.offset = addr;
967 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
968 elf_is_local_symbol(ste->sym.st_info),
969 loc, ste->sym.st_size, NULL);
970 break;
971 default:
972 FIXME("Shouldn't happen\n");
973 break;
975 /* FIXME: this is a hack !!!
976 * we are adding new symbols, but as we're parsing a symbol table
977 * (hopefully without duplicate symbols) we delay rebuilding the sorted
978 * module table until we're done with the symbol table
979 * Otherwise, as we intertwine symbols' add and lookup, performance
980 * is rather bad
982 module->sortlist_valid = TRUE;
986 /* see comment above */
987 module->sortlist_valid = FALSE;
988 return TRUE;
991 /******************************************************************
992 * elf_new_public_symbols
994 * Creates a set of public symbols from an ELF symtab
996 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
998 struct hash_table_iter hti;
999 struct symtab_elt* ste;
1001 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
1003 /* FIXME: we're missing the ELF entry point here */
1005 hash_table_iter_init(symtab, &hti, NULL);
1006 while ((ste = hash_table_iter_up(&hti)))
1008 symt_new_public(module, ste->compiland, ste->ht_elt.name,
1009 FALSE,
1010 module->reloc_delta + ste->sym.st_value,
1011 ste->sym.st_size);
1013 return TRUE;
1016 /******************************************************************
1017 * elf_load_debug_info_from_map
1019 * Loads the symbolic information from ELF module which mapping is described
1020 * in fmap
1021 * the module has been loaded at 'load_offset' address, so symbols' address
1022 * relocation is performed.
1023 * CRC is checked if fmap->with_crc is TRUE
1024 * returns
1025 * 0 if the file doesn't contain symbolic info (or this info cannot be
1026 * read or parsed)
1027 * 1 on success
1029 static BOOL elf_load_debug_info_from_map(struct module* module,
1030 struct image_file_map* fmap,
1031 struct pool* pool,
1032 struct hash_table* ht_symtab)
1034 BOOL ret = FALSE, lret;
1035 struct elf_thunk_area thunks[] =
1037 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
1038 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1039 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1040 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
1041 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1042 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1043 {NULL, 0, 0, 0}
1046 module->module.SymType = SymExport;
1048 /* create a hash table for the symtab */
1049 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
1051 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1053 struct image_section_map stab_sect, stabstr_sect;
1055 /* check if we need an alternate file (from debuglink or build-id) */
1056 ret = image_check_alternate(fmap, module);
1058 if (image_find_section(fmap, ".stab", &stab_sect) &&
1059 image_find_section(fmap, ".stabstr", &stabstr_sect))
1061 const char* stab;
1062 const char* stabstr;
1064 stab = image_map_section(&stab_sect);
1065 stabstr = image_map_section(&stabstr_sect);
1066 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
1068 /* OK, now just parse all of the stabs. */
1069 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
1070 stab, image_get_map_size(&stab_sect) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
1071 stabstr, image_get_map_size(&stabstr_sect),
1072 NULL, NULL);
1073 if (lret)
1074 /* and fill in the missing information for stabs */
1075 elf_finish_stabs_info(module, ht_symtab);
1076 else
1077 WARN("Couldn't correctly read stabs\n");
1078 ret = ret || lret;
1080 image_unmap_section(&stab_sect);
1081 image_unmap_section(&stabstr_sect);
1083 lret = dwarf2_parse(module, module->reloc_delta, thunks, fmap);
1084 ret = ret || lret;
1086 if (wcsstr(module->modulename, S_ElfW) || !wcscmp(module->modulename, S_WineLoaderW))
1088 /* add the thunks for native libraries */
1089 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
1090 elf_new_wine_thunks(module, ht_symtab, thunks);
1092 /* add all the public symbols from symtab */
1093 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
1095 return ret;
1098 /******************************************************************
1099 * elf_load_debug_info
1101 * Loads ELF debugging information from the module image file.
1103 static BOOL elf_load_debug_info(struct process* process, struct module* module)
1105 BOOL ret = TRUE;
1106 struct pool pool;
1107 struct hash_table ht_symtab;
1108 struct module_format* modfmt;
1110 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
1112 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
1113 return FALSE;
1116 pool_init(&pool, 65536);
1117 hash_table_init(&pool, &ht_symtab, 256);
1119 ret = elf_load_debug_info_from_map(module, &modfmt->u.elf_info->file_map, &pool, &ht_symtab);
1121 pool_destroy(&pool);
1122 return ret;
1125 /******************************************************************
1126 * elf_fetch_file_info
1128 * Gathers some more information for an ELF module from a given file
1130 static BOOL elf_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base, DWORD* size, DWORD* checksum)
1132 struct image_file_map fmap;
1134 struct elf_map_file_data emfd;
1136 emfd.kind = from_file;
1137 emfd.u.file.filename = name;
1138 if (!elf_map_file(&emfd, &fmap)) return FALSE;
1139 if (base) *base = fmap.u.elf.elf_start;
1140 *size = fmap.u.elf.elf_size;
1141 *checksum = calc_crc32(fmap.u.elf.handle);
1142 image_unmap_file(&fmap);
1143 return TRUE;
1146 static BOOL elf_load_file_from_fmap(struct process* pcs, const WCHAR* filename,
1147 struct image_file_map* fmap, ULONG_PTR load_offset,
1148 ULONG_PTR dyn_addr, struct elf_info* elf_info)
1150 BOOL ret = FALSE;
1152 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1154 struct image_section_map ism;
1156 if (elf_find_section_type(fmap, ".dynamic", ELF_SHT_DYNAMIC, &ism))
1158 char* ptr = (char*)(ULONG_PTR)fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1159 ULONG_PTR len;
1161 if (load_offset) ptr += load_offset - fmap->u.elf.elf_start;
1163 if (fmap->addr_size == 32)
1165 struct
1167 INT32 d_tag; /* Dynamic entry type */
1168 UINT32 d_val; /* Integer or address value */
1169 } dyn;
1173 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1174 len != sizeof(dyn))
1175 return ret;
1176 if (dyn.d_tag == ELF_DT_DEBUG)
1178 elf_info->dbg_hdr_addr = dyn.d_val;
1179 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1180 /* Assume this module (the Wine loader) has been
1181 * loaded at its preferred address */
1182 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1183 break;
1185 ptr += sizeof(dyn);
1186 } while (dyn.d_tag);
1187 if (!dyn.d_tag) return ret;
1189 else
1191 struct
1193 INT64 d_tag; /* Dynamic entry type */
1194 UINT64 d_val; /* Integer or address value */
1195 } dyn;
1199 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1200 len != sizeof(dyn))
1201 return ret;
1202 if (dyn.d_tag == ELF_DT_DEBUG)
1204 elf_info->dbg_hdr_addr = dyn.d_val;
1205 if (load_offset == 0 && dyn_addr == 0) /* likely the case */
1206 /* Assume this module (the Wine loader) has been
1207 * loaded at its preferred address */
1208 dyn_addr = ism.fmap->u.elf.sect[ism.sidx].shdr.sh_addr;
1209 break;
1211 ptr += sizeof(dyn);
1212 } while (dyn.d_tag);
1213 if (!dyn.d_tag) return ret;
1216 elf_end_find(fmap);
1219 if (elf_info->flags & ELF_INFO_MODULE)
1221 struct elf_module_info *elf_module_info;
1222 struct module_format* modfmt;
1223 struct image_section_map ism;
1224 ULONG_PTR modbase = load_offset;
1226 if (elf_find_section_type(fmap, ".dynamic", ELF_SHT_DYNAMIC, &ism))
1228 ULONG_PTR rva_dyn = elf_get_map_rva(&ism);
1230 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1231 debugstr_w(filename), (ULONG_PTR)fmap->u.elf.elf_start, rva_dyn,
1232 load_offset, dyn_addr);
1233 if (dyn_addr && load_offset + rva_dyn != dyn_addr)
1235 WARN("\thave to relocate: %lx\n", dyn_addr - rva_dyn);
1236 modbase = dyn_addr - rva_dyn;
1238 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename));
1239 elf_end_find(fmap);
1241 modfmt = HeapAlloc(GetProcessHeap(), 0,
1242 sizeof(struct module_format) + sizeof(struct elf_module_info));
1243 if (!modfmt) return FALSE;
1244 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE, modbase,
1245 fmap->u.elf.elf_size, 0, calc_crc32(fmap->u.elf.handle),
1246 elf_get_machine(fmap->u.elf.elfhdr.e_machine));
1247 if (!elf_info->module)
1249 HeapFree(GetProcessHeap(), 0, modfmt);
1250 return FALSE;
1252 elf_info->module->reloc_delta = elf_info->module->module.BaseOfImage - fmap->u.elf.elf_start;
1253 elf_module_info = (void*)(modfmt + 1);
1254 elf_info->module->format_info[DFI_ELF] = modfmt;
1255 modfmt->module = elf_info->module;
1256 modfmt->remove = elf_module_remove;
1257 modfmt->loc_compute = NULL;
1258 modfmt->u.elf_info = elf_module_info;
1260 elf_module_info->elf_addr = load_offset;
1262 elf_module_info->file_map = *fmap;
1263 elf_reset_file_map(fmap);
1264 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1266 elf_info->module->module.SymType = SymDeferred;
1267 ret = TRUE;
1269 else ret = elf_load_debug_info(pcs, elf_info->module);
1271 elf_module_info->elf_mark = 1;
1272 elf_module_info->elf_loader = 0;
1273 } else ret = TRUE;
1275 if (elf_info->flags & ELF_INFO_NAME)
1277 WCHAR* ptr;
1278 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1279 if (ptr)
1281 lstrcpyW(ptr, filename);
1282 elf_info->module_name = ptr;
1284 else ret = FALSE;
1287 return ret;
1290 /******************************************************************
1291 * elf_load_file
1293 * Loads the information for ELF module stored in 'filename'
1294 * the module has been loaded at 'load_offset' address
1295 * returns
1296 * -1 if the file cannot be found/opened
1297 * 0 if the file doesn't contain symbolic info (or this info cannot be
1298 * read or parsed)
1299 * 1 on success
1301 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1302 ULONG_PTR load_offset, ULONG_PTR dyn_addr,
1303 struct elf_info* elf_info)
1305 BOOL ret = FALSE;
1306 struct image_file_map fmap;
1307 struct elf_map_file_data emfd;
1309 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1311 emfd.kind = from_file;
1312 emfd.u.file.filename = filename;
1313 if (!elf_map_file(&emfd, &fmap)) return ret;
1315 /* Next, we need to find a few of the internal ELF headers within
1316 * this thing. We need the main executable header, and the section
1317 * table.
1319 if (!fmap.u.elf.elf_start && !load_offset)
1320 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1321 debugstr_w(filename));
1323 ret = elf_load_file_from_fmap(pcs, filename, &fmap, load_offset, dyn_addr, elf_info);
1325 image_unmap_file(&fmap);
1327 return ret;
1330 struct elf_load_file_params
1332 struct process *process;
1333 ULONG_PTR load_offset;
1334 ULONG_PTR dyn_addr;
1335 struct elf_info *elf_info;
1338 static BOOL elf_load_file_cb(void *param, HANDLE handle, const WCHAR *filename)
1340 struct elf_load_file_params *load_file = param;
1341 return elf_load_file(load_file->process, filename, load_file->load_offset, load_file->dyn_addr, load_file->elf_info);
1344 /******************************************************************
1345 * elf_search_auxv
1347 * locate some a value from the debuggee auxiliary vector
1349 static BOOL elf_search_auxv(const struct process* pcs, unsigned type, ULONG_PTR* val)
1351 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
1352 SYMBOL_INFO*si = (SYMBOL_INFO*)buffer;
1353 BYTE* addr;
1354 BYTE* str;
1355 BYTE* str_max;
1357 si->SizeOfStruct = sizeof(*si);
1358 si->MaxNameLen = MAX_SYM_NAME;
1359 if (!SymFromName(pcs->handle, "libwine.so.1!__wine_main_environ", si) ||
1360 !(addr = (void*)(DWORD_PTR)si->Address) ||
1361 !ReadProcessMemory(pcs->handle, addr, &addr, sizeof(addr), NULL) ||
1362 !addr)
1364 FIXME("can't find symbol in module\n");
1365 return FALSE;
1367 /* walk through envp[] */
1368 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1369 str_max = (void*)(DWORD_PTR)~0L;
1370 while (ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) &&
1371 (addr = (void*)((DWORD_PTR)addr + sizeof(str))) != NULL && str != NULL)
1372 str_max = min(str_max, str);
1374 /* Walk through the end of envp[] array.
1375 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1376 * deleted, the last entry is replaced by an extra NULL.
1378 while (addr < str_max && ReadProcessMemory(pcs->handle, addr, &str, sizeof(str), NULL) && str == NULL)
1379 addr = (void*)((DWORD_PTR)addr + sizeof(str));
1381 if (pcs->is_64bit)
1383 struct
1385 UINT64 a_type;
1386 UINT64 a_val;
1387 } auxv;
1389 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type)
1391 if (auxv.a_type == type)
1393 *val = auxv.a_val;
1394 return TRUE;
1396 addr += sizeof(auxv);
1399 else
1401 struct
1403 UINT32 a_type;
1404 UINT32 a_val;
1405 } auxv;
1407 while (ReadProcessMemory(pcs->handle, addr, &auxv, sizeof(auxv), NULL) && auxv.a_type)
1409 if (auxv.a_type == type)
1411 *val = auxv.a_val;
1412 return TRUE;
1414 addr += sizeof(auxv);
1418 return FALSE;
1421 /******************************************************************
1422 * elf_search_and_load_file
1424 * lookup a file in standard ELF locations, and if found, load it
1426 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1427 ULONG_PTR load_offset, ULONG_PTR dyn_addr,
1428 struct elf_info* elf_info)
1430 BOOL ret = FALSE;
1431 struct module* module;
1432 static const WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1434 if (filename == NULL || *filename == '\0') return FALSE;
1435 if ((module = module_is_already_loaded(pcs, filename)))
1437 elf_info->module = module;
1438 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1439 return module->module.SymType;
1442 if (wcsstr(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1443 ret = elf_load_file(pcs, filename, load_offset, dyn_addr, elf_info);
1444 /* if relative pathname, try some absolute base dirs */
1445 if (!ret && filename == file_name(filename))
1447 struct elf_load_file_params load_elf;
1448 load_elf.process = pcs;
1449 load_elf.load_offset = load_offset;
1450 load_elf.dyn_addr = dyn_addr;
1451 load_elf.elf_info = elf_info;
1453 ret = search_unix_path(filename, process_getenv(pcs, L"LD_LIBRARY_PATH"), elf_load_file_cb, &load_elf)
1454 || search_dll_path(pcs, filename, elf_load_file_cb, &load_elf);
1457 return ret;
1460 typedef BOOL (*enum_elf_modules_cb)(const WCHAR*, ULONG_PTR load_addr,
1461 ULONG_PTR dyn_addr, BOOL is_system, void* user);
1463 /******************************************************************
1464 * elf_enum_modules_internal
1466 * Enumerate ELF modules from a running process
1468 static BOOL elf_enum_modules_internal(const struct process* pcs,
1469 const WCHAR* main_name,
1470 enum_elf_modules_cb cb, void* user)
1472 WCHAR bufstrW[MAX_PATH];
1473 char bufstr[256];
1474 ULONG_PTR lm_addr;
1476 if (pcs->is_64bit)
1478 struct
1480 UINT32 r_version;
1481 UINT64 r_map;
1482 UINT64 r_brk;
1483 UINT32 r_state;
1484 UINT64 r_ldbase;
1485 } dbg_hdr;
1486 struct
1488 UINT64 l_addr;
1489 UINT64 l_name;
1490 UINT64 l_ld;
1491 UINT64 l_next, l_prev;
1492 } lm;
1494 if (!pcs->dbg_hdr_addr || !read_process_memory(pcs, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1495 return FALSE;
1497 /* Now walk the linked list. In all known ELF implementations,
1498 * the dynamic loader maintains this linked list for us. In some
1499 * cases the first entry doesn't appear with a name, in other cases it
1500 * does.
1502 for (lm_addr = dbg_hdr.r_map; lm_addr; lm_addr = lm.l_next)
1504 if (!read_process_memory(pcs, lm_addr, &lm, sizeof(lm)))
1505 return FALSE;
1507 if (lm.l_prev && /* skip first entry, normally debuggee itself */
1508 lm.l_name && read_process_memory(pcs, lm.l_name, bufstr, sizeof(bufstr)))
1510 bufstr[sizeof(bufstr) - 1] = '\0';
1511 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, ARRAY_SIZE(bufstrW));
1512 if (main_name && !bufstrW[0]) lstrcpyW(bufstrW, main_name);
1513 if (!cb(bufstrW, (ULONG_PTR)lm.l_addr, (ULONG_PTR)lm.l_ld, FALSE, user))
1514 break;
1518 else
1520 struct
1522 UINT32 r_version;
1523 UINT32 r_map;
1524 UINT32 r_brk;
1525 UINT32 r_state;
1526 UINT32 r_ldbase;
1527 } dbg_hdr;
1528 struct
1530 UINT32 l_addr;
1531 UINT32 l_name;
1532 UINT32 l_ld;
1533 UINT32 l_next, l_prev;
1534 } lm;
1536 if (!pcs->dbg_hdr_addr || !read_process_memory(pcs, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1537 return FALSE;
1539 /* Now walk the linked list. In all known ELF implementations,
1540 * the dynamic loader maintains this linked list for us. In some
1541 * cases the first entry doesn't appear with a name, in other cases it
1542 * does.
1544 for (lm_addr = dbg_hdr.r_map; lm_addr; lm_addr = lm.l_next)
1546 if (!read_process_memory(pcs, lm_addr, &lm, sizeof(lm)))
1547 return FALSE;
1549 if (lm.l_prev && /* skip first entry, normally debuggee itself */
1550 lm.l_name && read_process_memory(pcs, lm.l_name, bufstr, sizeof(bufstr)))
1552 bufstr[sizeof(bufstr) - 1] = '\0';
1553 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, ARRAY_SIZE(bufstrW));
1554 if (main_name && !bufstrW[0]) lstrcpyW(bufstrW, main_name);
1555 if (!cb(bufstrW, (ULONG_PTR)lm.l_addr, (ULONG_PTR)lm.l_ld, FALSE, user))
1556 break;
1561 if (!lm_addr)
1563 ULONG_PTR ehdr_addr;
1565 if (elf_search_auxv(pcs, ELF_AT_SYSINFO_EHDR, &ehdr_addr))
1567 static const WCHAR vdsoW[] = {'[','v','d','s','o',']','.','s','o',0};
1568 cb(vdsoW, ehdr_addr, 0, TRUE, user);
1571 return TRUE;
1574 struct elf_enum_user
1576 enum_modules_cb cb;
1577 void* user;
1580 static BOOL elf_enum_modules_translate(const WCHAR* name, ULONG_PTR load_addr,
1581 ULONG_PTR dyn_addr, BOOL is_system, void* user)
1583 struct elf_enum_user* eeu = user;
1584 return eeu->cb(name, load_addr, eeu->user);
1587 /******************************************************************
1588 * elf_enum_modules
1590 * Enumerates the ELF loaded modules from a running target (hProc)
1591 * This function doesn't require that someone has called SymInitialize
1592 * on this very process.
1594 static BOOL elf_enum_modules(struct process* process, enum_modules_cb cb, void* user)
1596 struct elf_info elf_info;
1597 BOOL ret;
1598 struct elf_enum_user eeu;
1600 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1601 elf_info.module_name = NULL;
1602 eeu.cb = cb;
1603 eeu.user = user;
1604 ret = elf_enum_modules_internal(process, elf_info.module_name, elf_enum_modules_translate, &eeu);
1605 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1606 return ret;
1609 struct elf_load
1611 struct process* pcs;
1612 struct elf_info elf_info;
1613 const WCHAR* name;
1614 BOOL ret;
1617 /******************************************************************
1618 * elf_load_cb
1620 * Callback for elf_load_module, used to walk the list of loaded
1621 * modules.
1623 static BOOL elf_load_cb(const WCHAR* name, ULONG_PTR load_addr,
1624 ULONG_PTR dyn_addr, BOOL is_system, void* user)
1626 struct elf_load* el = user;
1627 BOOL ret = TRUE;
1628 const WCHAR* p;
1630 if (is_system) /* virtual ELF module, created by system. handle it from memory */
1632 struct module* module;
1633 struct elf_map_file_data emfd;
1634 struct image_file_map fmap;
1636 if ((module = module_is_already_loaded(el->pcs, name)))
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;
1643 emfd.kind = from_process;
1644 emfd.u.process.handle = el->pcs->handle;
1645 emfd.u.process.load_addr = (void*)load_addr;
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;
1651 if (el->name)
1653 /* memcmp is needed for matches when bufstr contains also version information
1654 * el->name: libc.so, name: libc.so.6.0
1656 p = file_name(name);
1659 if (!el->name || !memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1661 el->ret = elf_search_and_load_file(el->pcs, name, load_addr, dyn_addr, &el->elf_info);
1662 if (el->name) ret = FALSE;
1665 return ret;
1668 /******************************************************************
1669 * elf_load_module
1671 * loads an ELF module and stores it in process' module list
1672 * Also, find module real name and load address from
1673 * the real loaded modules list in pcs address space
1675 static struct module* elf_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1677 struct elf_load el;
1679 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1681 el.elf_info.flags = ELF_INFO_MODULE;
1682 el.ret = FALSE;
1684 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1686 el.pcs = pcs;
1687 /* do only the lookup from the filename, not the path (as we lookup module
1688 * name in the process' loaded module list)
1690 el.name = file_name(name);
1691 el.ret = FALSE;
1693 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1694 return NULL;
1696 else if (addr)
1698 el.name = name;
1699 el.ret = elf_search_and_load_file(pcs, el.name, addr, 0, &el.elf_info);
1701 if (!el.ret) return NULL;
1702 assert(el.elf_info.module);
1703 return el.elf_info.module;
1706 /******************************************************************
1707 * elf_synchronize_module_list
1709 * this function rescans the debuggee module's list and synchronizes it with
1710 * the one from 'pcs', i.e.:
1711 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1712 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1714 static BOOL elf_synchronize_module_list(struct process* pcs)
1716 struct module* module;
1717 struct elf_load el;
1719 for (module = pcs->lmodules; module; module = module->next)
1721 if (module->type == DMT_ELF && !module->is_virtual)
1722 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1725 el.pcs = pcs;
1726 el.elf_info.flags = ELF_INFO_MODULE;
1727 el.ret = FALSE;
1728 el.name = NULL; /* fetch all modules */
1730 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1731 return FALSE;
1733 module = pcs->lmodules;
1734 while (module)
1736 if (module->type == DMT_ELF && !module->is_virtual)
1738 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1740 if (!elf_info->elf_mark && !elf_info->elf_loader)
1742 module_remove(pcs, module);
1743 /* restart all over */
1744 module = pcs->lmodules;
1745 continue;
1748 module = module->next;
1750 return TRUE;
1753 static const struct loader_ops elf_loader_ops =
1755 elf_synchronize_module_list,
1756 elf_load_module,
1757 elf_load_debug_info,
1758 elf_enum_modules,
1759 elf_fetch_file_info,
1762 /******************************************************************
1763 * elf_read_wine_loader_dbg_info
1765 * Try to find a decent wine executable which could have loaded the debuggee
1767 BOOL elf_read_wine_loader_dbg_info(struct process* pcs, ULONG_PTR addr)
1769 struct elf_info elf_info;
1770 BOOL ret = FALSE;
1771 WCHAR* loader;
1773 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1774 loader = get_wine_loader_name(pcs);
1775 if (loader)
1777 ret = elf_search_and_load_file(pcs, loader, addr, 0, &elf_info);
1778 HeapFree(GetProcessHeap(), 0, loader);
1780 if (!ret || !elf_info.dbg_hdr_addr) return FALSE;
1782 TRACE("Found ELF debug header %#lx\n", elf_info.dbg_hdr_addr);
1783 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1784 module_set_module(elf_info.module, S_WineLoaderW);
1785 pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr;
1786 pcs->loader = &elf_loader_ops;
1787 return TRUE;