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
23 #include "wine/port.h"
25 #if defined(__svr4__) || defined(__sun)
27 /* large files are not supported by libelf */
28 #undef _FILE_OFFSET_BITS
29 #define _FILE_OFFSET_BITS 32
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
39 #ifdef HAVE_SYS_MMAN_H
46 #define PATH_MAX MAX_PATH
49 #include "dbghelp_private.h"
51 #include "image_private.h"
53 #include "wine/library.h"
54 #include "wine/debug.h"
58 #define ELF_INFO_DEBUG_HEADER 0x0001
59 #define ELF_INFO_MODULE 0x0002
60 #define ELF_INFO_NAME 0x0004
62 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
66 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
67 DWORD_PTR dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
68 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
69 const WCHAR* module_name; /* OUT found module name (if ELF_INFO_NAME is set) */
74 struct hash_table_elt ht_elt;
76 struct symt_compiland* compiland;
83 THUNK_ORDINAL ordinal;
84 unsigned long rva_start;
85 unsigned long rva_end;
88 struct elf_module_info
90 unsigned long elf_addr;
91 unsigned short elf_mark : 1,
93 struct image_file_map file_map;
96 /******************************************************************
99 * Maps a single section into memory from an ELF file
101 const char* elf_map_section(struct image_section_map* ism)
103 struct elf_file_map* fmap = &ism->fmap->u.elf;
105 unsigned pgsz = getpagesize();
108 assert(ism->fmap->modtype == DMT_ELF);
109 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum ||
110 fmap->sect[ism->sidx].shdr.sh_type == SHT_NOBITS)
113 /* align required information on page size (we assume pagesize is a power of 2) */
114 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
115 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
116 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
117 fmap->sect[ism->sidx].mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE,
119 if (fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP) return IMAGE_NO_MAP;
120 return fmap->sect[ism->sidx].mapped + (fmap->sect[ism->sidx].shdr.sh_offset & (pgsz - 1));
123 /******************************************************************
126 * Finds a section by name (and type) into memory from an ELF file
127 * or its alternate if any
129 BOOL elf_find_section(struct image_file_map* _fmap, const char* name,
130 unsigned sht, struct image_section_map* ism)
132 struct elf_file_map* fmap;
137 fmap = &_fmap->u.elf;
138 if (fmap->shstrtab == IMAGE_NO_MAP)
140 struct image_section_map hdr_ism = {_fmap, fmap->elfhdr.e_shstrndx};
141 if ((fmap->shstrtab = elf_map_section(&hdr_ism)) == IMAGE_NO_MAP) break;
143 for (i = 0; i < fmap->elfhdr.e_shnum; i++)
145 if (strcmp(fmap->shstrtab + fmap->sect[i].shdr.sh_name, name) == 0 &&
146 (sht == SHT_NULL || sht == fmap->sect[i].shdr.sh_type))
153 _fmap = fmap->alternate;
160 /******************************************************************
163 * Unmaps a single section from memory
165 void elf_unmap_section(struct image_section_map* ism)
167 struct elf_file_map* fmap = &ism->fmap->u.elf;
169 if (ism->sidx >= 0 && ism->sidx < fmap->elfhdr.e_shnum && fmap->sect[ism->sidx].mapped != IMAGE_NO_MAP)
171 unsigned pgsz = getpagesize();
174 ofst = fmap->sect[ism->sidx].shdr.sh_offset & ~(pgsz - 1);
175 size = ((fmap->sect[ism->sidx].shdr.sh_offset +
176 fmap->sect[ism->sidx].shdr.sh_size + pgsz - 1) & ~(pgsz - 1)) - ofst;
177 if (munmap((char*)fmap->sect[ism->sidx].mapped, size) < 0)
178 WARN("Couldn't unmap the section\n");
179 fmap->sect[ism->sidx].mapped = IMAGE_NO_MAP;
183 static void elf_end_find(struct image_file_map* fmap)
185 struct image_section_map ism;
190 ism.sidx = fmap->u.elf.elfhdr.e_shstrndx;
191 elf_unmap_section(&ism);
192 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
193 fmap = fmap->u.elf.alternate;
197 /******************************************************************
200 * Get the RVA of an ELF section
202 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
204 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
206 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_addr - ism->fmap->u.elf.elf_start;
209 /******************************************************************
212 * Get the size of an ELF section
214 unsigned elf_get_map_size(const struct image_section_map* ism)
216 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum)
218 return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size;
221 static inline void elf_reset_file_map(struct image_file_map* fmap)
224 fmap->u.elf.shstrtab = IMAGE_NO_MAP;
225 fmap->u.elf.alternate = NULL;
228 /******************************************************************
231 * Maps an ELF file into memory (and checks it's a real ELF file)
233 static BOOL elf_map_file(const WCHAR* filenameW, struct image_file_map* fmap)
235 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
239 unsigned tmp, page_mask = getpagesize() - 1;
244 len = WideCharToMultiByte(CP_UNIXCP, 0, filenameW, -1, NULL, 0, NULL, NULL);
245 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len))) return FALSE;
246 WideCharToMultiByte(CP_UNIXCP, 0, filenameW, -1, filename, len, NULL, NULL);
248 elf_reset_file_map(fmap);
250 fmap->modtype = DMT_ELF;
251 /* check that the file exists, and that the module hasn't been loaded yet */
252 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode)) goto done;
254 /* Now open the file, so that we can mmap() it. */
255 if ((fmap->u.elf.fd = open(filename, O_RDONLY)) == -1) goto done;
257 if (read(fmap->u.elf.fd, &fmap->u.elf.elfhdr, sizeof(fmap->u.elf.elfhdr)) != sizeof(fmap->u.elf.elfhdr))
259 /* and check for an ELF header */
260 if (memcmp(fmap->u.elf.elfhdr.e_ident,
261 elf_signature, sizeof(elf_signature))) goto done;
262 /* and check 32 vs 64 size according to current machine */
264 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS64) goto done;
266 if (fmap->u.elf.elfhdr.e_ident[EI_CLASS] != ELFCLASS32) goto done;
268 fmap->u.elf.sect = HeapAlloc(GetProcessHeap(), 0,
269 fmap->u.elf.elfhdr.e_shnum * sizeof(fmap->u.elf.sect[0]));
270 if (!fmap->u.elf.sect) goto done;
272 lseek(fmap->u.elf.fd, fmap->u.elf.elfhdr.e_shoff, SEEK_SET);
273 for (i = 0; i < fmap->u.elf.elfhdr.e_shnum; i++)
275 if (read(fmap->u.elf.fd, &fmap->u.elf.sect[i].shdr, sizeof(fmap->u.elf.sect[i].shdr)) != sizeof(fmap->u.elf.sect[i].shdr))
277 HeapFree(GetProcessHeap, 0, fmap->u.elf.sect);
278 fmap->u.elf.sect = NULL;
281 fmap->u.elf.sect[i].mapped = IMAGE_NO_MAP;
284 /* grab size of module once loaded in memory */
285 lseek(fmap->u.elf.fd, fmap->u.elf.elfhdr.e_phoff, SEEK_SET);
286 fmap->u.elf.elf_size = 0;
287 fmap->u.elf.elf_start = ~0L;
288 for (i = 0; i < fmap->u.elf.elfhdr.e_phnum; i++)
290 if (read(fmap->u.elf.fd, &phdr, sizeof(phdr)) == sizeof(phdr) &&
291 phdr.p_type == PT_LOAD)
293 tmp = (phdr.p_vaddr + phdr.p_memsz + page_mask) & ~page_mask;
294 if (fmap->u.elf.elf_size < tmp) fmap->u.elf.elf_size = tmp;
295 if (phdr.p_vaddr < fmap->u.elf.elf_start) fmap->u.elf.elf_start = phdr.p_vaddr;
298 /* if non relocatable ELF, then remove fixed address from computation
299 * otherwise, all addresses are zero based and start has no effect
301 fmap->u.elf.elf_size -= fmap->u.elf.elf_start;
304 HeapFree(GetProcessHeap(), 0, filename);
308 /******************************************************************
311 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
313 static void elf_unmap_file(struct image_file_map* fmap)
317 if (fmap->u.elf.fd != -1)
319 struct image_section_map ism;
321 for (ism.sidx = 0; ism.sidx < fmap->u.elf.elfhdr.e_shnum; ism.sidx++)
323 elf_unmap_section(&ism);
325 HeapFree(GetProcessHeap(), 0, fmap->u.elf.sect);
326 close(fmap->u.elf.fd);
328 fmap = fmap->u.elf.alternate;
332 static void elf_module_remove(struct process* pcs, struct module_format* modfmt)
334 elf_unmap_file(&modfmt->u.elf_info->file_map);
335 HeapFree(GetProcessHeap(), 0, modfmt);
338 /******************************************************************
339 * elf_is_in_thunk_area
341 * Check whether an address lies within one of the thunk area we
344 int elf_is_in_thunk_area(unsigned long addr,
345 const struct elf_thunk_area* thunks)
349 if (thunks) for (i = 0; thunks[i].symname; i++)
351 if (addr >= thunks[i].rva_start && addr < thunks[i].rva_end)
357 /******************************************************************
360 * creating an internal hash table to ease use ELF symtab information lookup
362 static void elf_hash_symtab(struct module* module, struct pool* pool,
363 struct hash_table* ht_symtab, struct image_file_map* fmap,
364 struct elf_thunk_area* thunks)
369 struct symt_compiland* compiland = NULL;
372 struct symtab_elt* ste;
373 struct image_section_map ism, ism_str;
375 if (!elf_find_section(fmap, ".symtab", SHT_SYMTAB, &ism) &&
376 !elf_find_section(fmap, ".dynsym", SHT_DYNSYM, &ism)) return;
377 if ((symp = (const Elf_Sym*)image_map_section(&ism)) == IMAGE_NO_MAP) return;
378 ism_str.fmap = ism.fmap;
379 ism_str.sidx = fmap->u.elf.sect[ism.sidx].shdr.sh_link;
380 if ((strp = image_map_section(&ism_str)) == IMAGE_NO_MAP)
382 image_unmap_section(&ism);
386 nsym = image_get_map_size(&ism) / sizeof(*symp);
388 for (j = 0; thunks[j].symname; j++)
389 thunks[j].rva_start = thunks[j].rva_end = 0;
391 for (i = 0; i < nsym; i++, symp++)
393 /* Ignore certain types of entries which really aren't of that much
396 if ((ELF32_ST_TYPE(symp->st_info) != STT_NOTYPE &&
397 ELF32_ST_TYPE(symp->st_info) != STT_FILE &&
398 ELF32_ST_TYPE(symp->st_info) != STT_OBJECT &&
399 ELF32_ST_TYPE(symp->st_info) != STT_FUNC) ||
400 symp->st_shndx == SHN_UNDEF)
405 symname = strp + symp->st_name;
407 /* handle some specific symtab (that we'll throw away when done) */
408 switch (ELF32_ST_TYPE(symp->st_info))
412 compiland = symt_new_compiland(module, symp->st_value,
413 source_new(module, NULL, symname));
418 /* we are only interested in wine markers inserted by winebuild */
419 for (j = 0; thunks[j].symname; j++)
421 if (!strcmp(symname, thunks[j].symname))
423 thunks[j].rva_start = symp->st_value;
424 thunks[j].rva_end = symp->st_value + symp->st_size;
431 /* FIXME: we don't need to handle them (GCC internals)
432 * Moreover, they screw up our symbol lookup :-/
434 if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
437 ste = pool_alloc(pool, sizeof(*ste));
438 ste->ht_elt.name = symname;
439 /* GCC emits, in some cases, a .<digit>+ suffix.
440 * This is used for static variable inside functions, so
441 * that we can have several such variables with same name in
442 * the same compilation unit
443 * We simply ignore that suffix when present (we also get rid
444 * of it in stabs parsing)
446 ptr = symname + strlen(symname) - 1;
449 while (isdigit(*ptr) && ptr >= symname) ptr--;
450 if (ptr > symname && *ptr == '.')
452 char* n = pool_alloc(pool, ptr - symname + 1);
453 memcpy(n, symname, ptr - symname + 1);
454 n[ptr - symname] = '\0';
455 ste->ht_elt.name = n;
459 ste->compiland = compiland;
461 hash_table_add(ht_symtab, &ste->ht_elt);
463 /* as we added in the ht_symtab pointers to the symbols themselves,
464 * we cannot unmap yet the sections, it will be done when we're over
469 /******************************************************************
472 * lookup a symbol by name in our internal hash table for the symtab
474 static const Elf_Sym* elf_lookup_symtab(const struct module* module,
475 const struct hash_table* ht_symtab,
476 const char* name, const struct symt* compiland)
478 struct symtab_elt* weak_result = NULL; /* without compiland name */
479 struct symtab_elt* result = NULL;
480 struct hash_table_iter hti;
481 struct symtab_elt* ste;
482 const char* compiland_name;
483 const char* compiland_basename;
486 /* we need weak match up (at least) when symbols of same name,
487 * defined several times in different compilation units,
488 * are merged in a single one (hence a different filename for c.u.)
492 compiland_name = source_get(module,
493 ((const struct symt_compiland*)compiland)->source);
494 compiland_basename = strrchr(compiland_name, '/');
495 if (!compiland_basename++) compiland_basename = compiland_name;
497 else compiland_name = compiland_basename = NULL;
499 hash_table_iter_init(ht_symtab, &hti, name);
500 while ((ste = hash_table_iter_up(&hti)))
502 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
505 if ((ste->compiland && !compiland_name) || (!ste->compiland && compiland_name))
507 if (ste->compiland && compiland_name)
509 const char* filename = source_get(module, ste->compiland->source);
510 if (strcmp(filename, compiland_name))
512 base = strrchr(filename, '/');
513 if (!base++) base = filename;
514 if (strcmp(base, compiland_basename)) continue;
519 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
520 name, compiland_name,
521 source_get(module, result->compiland->source), (unsigned int)result->symp->st_value,
522 source_get(module, ste->compiland->source), (unsigned int)ste->symp->st_value);
530 if (!result && !(result = weak_result))
532 FIXME("Couldn't find symbol %s!%s in symtab\n",
533 debugstr_w(module->module.ModuleName), name);
539 /******************************************************************
540 * elf_finish_stabs_info
542 * - get any relevant information (address & size) from the bits we got from the
543 * stabs debugging information
545 static void elf_finish_stabs_info(struct module* module, const struct hash_table* symtab)
547 struct hash_table_iter hti;
551 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
553 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
554 while ((ptr = hash_table_iter_up(&hti)))
556 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
557 switch (sym->symt.tag)
560 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
561 ((struct symt_function*)sym)->size)
565 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
566 ((struct symt_function*)sym)->container);
569 if (((struct symt_function*)sym)->address != elf_info->elf_addr &&
570 ((struct symt_function*)sym)->address != elf_info->elf_addr + symp->st_value)
571 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
572 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
573 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
574 if (((struct symt_function*)sym)->size && ((struct symt_function*)sym)->size != symp->st_size)
575 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
576 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
577 ((struct symt_function*)sym)->size, (unsigned int)symp->st_size);
579 ((struct symt_function*)sym)->address = elf_info->elf_addr + symp->st_value;
580 ((struct symt_function*)sym)->size = symp->st_size;
582 FIXME("Couldn't find %s!%s\n",
583 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
586 switch (((struct symt_data*)sym)->kind)
589 case DataIsFileStatic:
590 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr)
592 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
593 ((struct symt_data*)sym)->container);
596 if (((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr &&
597 ((struct symt_data*)sym)->u.var.offset != elf_info->elf_addr + symp->st_value)
598 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
599 sym, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
600 ((struct symt_function*)sym)->address, elf_info->elf_addr + symp->st_value);
601 ((struct symt_data*)sym)->u.var.offset = elf_info->elf_addr + symp->st_value;
602 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
603 DataIsFileStatic : DataIsGlobal;
605 FIXME("Couldn't find %s!%s\n",
606 debugstr_w(module->module.ModuleName), sym->hash_elt.name);
612 FIXME("Unsupported tag %u\n", sym->symt.tag);
616 /* since we may have changed some addresses & sizes, mark the module to be resorted */
617 module->sortlist_valid = FALSE;
620 /******************************************************************
621 * elf_load_wine_thunks
623 * creating the thunk objects for a wine native DLL
625 static int elf_new_wine_thunks(struct module* module, const struct hash_table* ht_symtab,
626 const struct elf_thunk_area* thunks)
629 struct hash_table_iter hti;
630 struct symtab_elt* ste;
632 struct symt_ht* symt;
634 hash_table_iter_init(ht_symtab, &hti, NULL);
635 while ((ste = hash_table_iter_up(&hti)))
637 if (ste->used) continue;
639 addr = module->format_info[DFI_ELF]->u.elf_info->elf_addr + ste->symp->st_value;
641 j = elf_is_in_thunk_area(ste->symp->st_value, thunks);
642 if (j >= 0) /* thunk found */
644 symt_new_thunk(module, ste->compiland, ste->ht_elt.name, thunks[j].ordinal,
645 addr, ste->symp->st_size);
651 symt = symt_find_nearest(module, addr);
652 if (symt && !symt_get_info(module, &symt->symt, TI_GET_ADDRESS, &ref_addr))
654 if (!symt || addr != ref_addr)
656 /* creating public symbols for all the ELF symbols which haven't been
657 * used yet (ie we have no debug information on them)
658 * That's the case, for example, of the .spec.c files
660 switch (ELF32_ST_TYPE(ste->symp->st_info))
663 symt_new_function(module, ste->compiland, ste->ht_elt.name,
664 addr, ste->symp->st_size, NULL);
667 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
668 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
669 addr, ste->symp->st_size, NULL);
672 FIXME("Shouldn't happen\n");
675 /* FIXME: this is a hack !!!
676 * we are adding new symbols, but as we're parsing a symbol table
677 * (hopefully without duplicate symbols) we delay rebuilding the sorted
678 * module table until we're done with the symbol table
679 * Otherwise, as we intertwine symbols's add and lookup, performance
682 module->sortlist_valid = TRUE;
684 else if (strcmp(ste->ht_elt.name, symt->hash_elt.name))
686 ULONG64 xaddr = 0, xsize = 0;
689 symt_get_info(module, &symt->symt, TI_GET_ADDRESS, &xaddr);
690 symt_get_info(module, &symt->symt, TI_GET_LENGTH, &xsize);
691 symt_get_info(module, &symt->symt, TI_GET_DATAKIND, &kind);
693 /* If none of symbols has a correct size, we consider they are both markers
694 * Hence, we can silence this warning
695 * Also, we check that we don't have two symbols, one local, the other
696 * global which is legal
698 if ((xsize || ste->symp->st_size) &&
699 (kind == (ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL) ? DataIsFileStatic : DataIsGlobal))
700 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%s-%s>\n",
701 debugstr_w(module->module.ModuleName),
702 ste->ht_elt.name, addr, (unsigned int)ste->symp->st_size,
704 wine_dbgstr_longlong(xaddr), wine_dbgstr_longlong(xsize));
708 /* see comment above */
709 module->sortlist_valid = FALSE;
713 /******************************************************************
714 * elf_new_public_symbols
716 * Creates a set of public symbols from an ELF symtab
718 static int elf_new_public_symbols(struct module* module, const struct hash_table* symtab)
720 struct hash_table_iter hti;
721 struct symtab_elt* ste;
723 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
725 /* FIXME: we're missing the ELF entry point here */
727 hash_table_iter_init(symtab, &hti, NULL);
728 while ((ste = hash_table_iter_up(&hti)))
730 symt_new_public(module, ste->compiland, ste->ht_elt.name,
731 module->format_info[DFI_ELF]->u.elf_info->elf_addr + ste->symp->st_value,
737 static BOOL elf_check_debug_link(const WCHAR* file, struct image_file_map* fmap, DWORD crc)
740 if (!elf_map_file(file, fmap)) return FALSE;
741 if (!(ret = crc == calc_crc32(fmap->u.elf.fd)))
743 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
744 debugstr_w(file), calc_crc32(fmap->u.elf.fd), crc);
745 elf_unmap_file(fmap);
750 /******************************************************************
751 * elf_locate_debug_link
753 * Locate a filename from a .gnu_debuglink section, using the same
755 * "If the full name of the directory containing the executable is
756 * execdir, and the executable has a debug link that specifies the
757 * name debugfile, then GDB will automatically search for the
758 * debugging information file in three places:
759 * - the directory containing the executable file (that is, it
760 * will look for a file named `execdir/debugfile',
761 * - a subdirectory of that directory named `.debug' (that is, the
762 * file `execdir/.debug/debugfile', and
763 * - a subdirectory of the global debug file directory that includes
764 * the executable's full path, and the name from the link (that is,
765 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
766 * is the global debug file directory, and execdir has been turned
767 * into a relative path)." (from GDB manual)
769 static BOOL elf_locate_debug_link(struct image_file_map* fmap, const char* filename,
770 const WCHAR* loaded_file, DWORD crc)
772 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
773 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
774 const size_t globalDebugDirLen = sizeof(globalDebugDirW) / sizeof(WCHAR);
778 struct image_file_map* fmap_link = NULL;
780 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
781 if (!fmap_link) return FALSE;
783 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
784 p = HeapAlloc(GetProcessHeap(), 0,
785 (globalDebugDirLen + strlenW(loaded_file) + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
788 /* we prebuild the string with "execdir" */
789 strcpyW(p, loaded_file);
790 slash = strrchrW(p, '/');
791 if (slash == NULL) slash = p; else slash++;
793 /* testing execdir/filename */
794 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
795 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
797 /* testing execdir/.debug/filename */
798 memcpy(slash, dotDebugW, sizeof(dotDebugW));
799 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + sizeof(dotDebugW) / sizeof(WCHAR), filename_len);
800 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
802 /* testing globaldebugdir/execdir/filename */
803 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
804 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
805 slash += globalDebugDirLen;
806 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
807 if (elf_check_debug_link(p, fmap_link, crc)) goto found;
809 /* finally testing filename */
810 if (elf_check_debug_link(slash, fmap_link, crc)) goto found;
813 WARN("Couldn't locate or map %s\n", filename);
814 HeapFree(GetProcessHeap(), 0, p);
815 HeapFree(GetProcessHeap(), 0, fmap_link);
819 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
820 HeapFree(GetProcessHeap(), 0, p);
821 fmap->u.elf.alternate = fmap_link;
825 /******************************************************************
826 * elf_debuglink_parse
828 * Parses a .gnu_debuglink section and loads the debug info from
829 * the external file specified there.
831 static BOOL elf_debuglink_parse(struct image_file_map* fmap, const struct module* module,
832 const BYTE* debuglink)
834 /* The content of a debug link section is:
835 * 1/ a NULL terminated string, containing the file name for the
837 * 2/ padding on 4 byte boundary
838 * 3/ CRC of the linked ELF file
840 const char* dbg_link = (const char*)debuglink;
843 crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
844 return elf_locate_debug_link(fmap, dbg_link, module->module.LoadedImageName, crc);
847 /******************************************************************
848 * elf_load_debug_info_from_map
850 * Loads the symbolic information from ELF module which mapping is described
852 * the module has been loaded at 'load_offset' address, so symbols' address
853 * relocation is performed.
854 * CRC is checked if fmap->with_crc is TRUE
856 * 0 if the file doesn't contain symbolic info (or this info cannot be
860 static BOOL elf_load_debug_info_from_map(struct module* module,
861 struct image_file_map* fmap,
863 struct hash_table* ht_symtab)
865 BOOL ret = FALSE, lret;
866 struct elf_thunk_area thunks[] =
868 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
869 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
870 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
871 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
872 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
873 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
877 module->module.SymType = SymExport;
879 /* create a hash table for the symtab */
880 elf_hash_symtab(module, pool, ht_symtab, fmap, thunks);
882 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
884 struct image_section_map stab_sect, stabstr_sect;
885 struct image_section_map debuglink_sect;
887 /* if present, add the .gnu_debuglink file as an alternate to current one */
888 if (elf_find_section(fmap, ".gnu_debuglink", SHT_NULL, &debuglink_sect))
890 const BYTE* dbg_link;
892 dbg_link = (const BYTE*)image_map_section(&debuglink_sect);
893 if (dbg_link != IMAGE_NO_MAP)
895 lret = elf_debuglink_parse(fmap, module, dbg_link);
897 WARN("Couldn't load linked debug file for %s\n",
898 debugstr_w(module->module.ModuleName));
901 image_unmap_section(&debuglink_sect);
903 if (elf_find_section(fmap, ".stab", SHT_NULL, &stab_sect) &&
904 elf_find_section(fmap, ".stabstr", SHT_NULL, &stabstr_sect))
909 stab = image_map_section(&stab_sect);
910 stabstr = image_map_section(&stabstr_sect);
911 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
913 /* OK, now just parse all of the stabs. */
914 lret = stabs_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
915 stab, image_get_map_size(&stab_sect),
916 stabstr, image_get_map_size(&stabstr_sect),
919 /* and fill in the missing information for stabs */
920 elf_finish_stabs_info(module, ht_symtab);
922 WARN("Couldn't correctly read stabs\n");
926 image_unmap_section(&stab_sect);
927 image_unmap_section(&stabstr_sect);
929 lret = dwarf2_parse(module, module->format_info[DFI_ELF]->u.elf_info->elf_addr,
933 if (strstrW(module->module.ModuleName, S_ElfW) ||
934 !strcmpW(module->module.ModuleName, S_WineLoaderW))
936 /* add the thunks for native libraries */
937 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
938 elf_new_wine_thunks(module, ht_symtab, thunks);
940 /* add all the public symbols from symtab */
941 if (elf_new_public_symbols(module, ht_symtab) && !ret) ret = TRUE;
946 /******************************************************************
947 * elf_load_debug_info
949 * Loads ELF debugging information from the module image file.
951 BOOL elf_load_debug_info(struct module* module, struct image_file_map* fmap)
955 struct hash_table ht_symtab;
956 struct image_file_map my_fmap;
957 struct module_format* modfmt;
959 if (module->type != DMT_ELF || !(modfmt = module->format_info[DFI_ELF]) || !modfmt->u.elf_info)
961 ERR("Bad elf module '%s'\n", debugstr_w(module->module.LoadedImageName));
965 pool_init(&pool, 65536);
966 hash_table_init(&pool, &ht_symtab, 256);
971 ret = elf_map_file(module->module.LoadedImageName, fmap);
974 ret = elf_load_debug_info_from_map(module, fmap, &pool, &ht_symtab);
978 modfmt->u.elf_info->file_map = *fmap;
979 elf_reset_file_map(fmap);
983 if (fmap == &my_fmap) elf_unmap_file(fmap);
987 /******************************************************************
988 * elf_fetch_file_info
990 * Gathers some more information for an ELF module from a given file
992 BOOL elf_fetch_file_info(const WCHAR* name, DWORD* base,
993 DWORD* size, DWORD* checksum)
995 struct image_file_map fmap;
997 if (!elf_map_file(name, &fmap)) return FALSE;
998 if (base) *base = fmap.u.elf.elf_start;
999 *size = fmap.u.elf.elf_size;
1000 *checksum = calc_crc32(fmap.u.elf.fd);
1001 elf_unmap_file(&fmap);
1005 /******************************************************************
1008 * Loads the information for ELF module stored in 'filename'
1009 * the module has been loaded at 'load_offset' address
1011 * -1 if the file cannot be found/opened
1012 * 0 if the file doesn't contain symbolic info (or this info cannot be
1016 static BOOL elf_load_file(struct process* pcs, const WCHAR* filename,
1017 unsigned long load_offset, struct elf_info* elf_info)
1020 struct image_file_map fmap;
1022 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename), load_offset);
1024 if (!elf_map_file(filename, &fmap)) return ret;
1026 /* Next, we need to find a few of the internal ELF headers within
1027 * this thing. We need the main executable header, and the section
1030 if (!fmap.u.elf.elf_start && !load_offset)
1031 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1032 debugstr_w(filename));
1033 if (fmap.u.elf.elf_start && load_offset)
1035 WARN("Non-relocatable ELF %s, but load address of 0x%08lx supplied. "
1036 "Assuming load address is corrupt\n", debugstr_w(filename), load_offset);
1040 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
1042 struct image_section_map ism;
1044 if (elf_find_section(&fmap, ".dynamic", SHT_DYNAMIC, &ism))
1047 char* ptr = (char*)fmap.u.elf.sect[ism.sidx].shdr.sh_addr;
1052 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
1055 if (dyn.d_tag == DT_DEBUG)
1057 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
1061 } while (dyn.d_tag != DT_NULL);
1062 if (dyn.d_tag == DT_NULL) goto leave;
1064 elf_end_find(&fmap);
1067 if (elf_info->flags & ELF_INFO_MODULE)
1069 struct elf_module_info *elf_module_info;
1070 struct module_format* modfmt;
1072 modfmt = HeapAlloc(GetProcessHeap(), 0,
1073 sizeof(struct module_format) + sizeof(struct elf_module_info));
1074 if (!modfmt) goto leave;
1075 elf_info->module = module_new(pcs, filename, DMT_ELF, FALSE,
1076 (load_offset) ? load_offset : fmap.u.elf.elf_start,
1077 fmap.u.elf.elf_size, 0, calc_crc32(fmap.u.elf.fd));
1078 if (!elf_info->module)
1080 HeapFree(GetProcessHeap(), 0, modfmt);
1083 elf_module_info = (void*)(modfmt + 1);
1084 elf_info->module->format_info[DFI_ELF] = modfmt;
1085 modfmt->module = elf_info->module;
1086 modfmt->remove = elf_module_remove;
1087 modfmt->loc_compute = NULL;
1088 modfmt->u.elf_info = elf_module_info;
1090 elf_module_info->elf_addr = load_offset;
1092 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1094 elf_info->module->module.SymType = SymDeferred;
1095 elf_module_info->file_map = fmap;
1096 elf_reset_file_map(&fmap);
1099 else ret = elf_load_debug_info(elf_info->module, &fmap);
1101 elf_module_info->elf_mark = 1;
1102 elf_module_info->elf_loader = 0;
1105 if (elf_info->flags & ELF_INFO_NAME)
1108 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1111 strcpyW(ptr, filename);
1112 elf_info->module_name = ptr;
1117 elf_unmap_file(&fmap);
1122 /******************************************************************
1123 * elf_load_file_from_path
1124 * tries to load an ELF file from a set of paths (separated by ':')
1126 static BOOL elf_load_file_from_path(HANDLE hProcess,
1127 const WCHAR* filename,
1128 unsigned long load_offset,
1130 struct elf_info* elf_info)
1134 WCHAR* pathW = NULL;
1137 if (!path) return FALSE;
1139 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1140 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1141 if (!pathW) return FALSE;
1142 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1144 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1146 t = strchrW(s, ':');
1148 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1151 strcatW(fn, S_SlashW);
1152 strcatW(fn, filename);
1153 ret = elf_load_file(hProcess, fn, load_offset, elf_info);
1154 HeapFree(GetProcessHeap(), 0, fn);
1156 s = (t) ? (t+1) : NULL;
1159 HeapFree(GetProcessHeap(), 0, pathW);
1163 /******************************************************************
1164 * elf_load_file_from_dll_path
1166 * Tries to load an ELF file from the dll path
1168 static BOOL elf_load_file_from_dll_path(HANDLE hProcess,
1169 const WCHAR* filename,
1170 unsigned long load_offset,
1171 struct elf_info* elf_info)
1174 unsigned int index = 0;
1177 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1182 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1184 name = HeapAlloc( GetProcessHeap(), 0,
1185 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1188 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1189 strcatW( name, S_SlashW );
1190 strcatW( name, filename );
1191 ret = elf_load_file(hProcess, name, load_offset, elf_info);
1192 HeapFree( GetProcessHeap(), 0, name );
1197 /******************************************************************
1198 * elf_search_and_load_file
1200 * lookup a file in standard ELF locations, and if found, load it
1202 static BOOL elf_search_and_load_file(struct process* pcs, const WCHAR* filename,
1203 unsigned long load_offset,
1204 struct elf_info* elf_info)
1207 struct module* module;
1208 static WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1210 if (filename == NULL || *filename == '\0') return FALSE;
1211 if ((module = module_is_already_loaded(pcs, filename)))
1213 elf_info->module = module;
1214 elf_info->module->format_info[DFI_ELF]->u.elf_info->elf_mark = 1;
1215 return module->module.SymType;
1218 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1219 ret = elf_load_file(pcs, filename, load_offset, elf_info);
1220 /* if relative pathname, try some absolute base dirs */
1221 if (!ret && !strchrW(filename, '/'))
1223 ret = elf_load_file_from_path(pcs, filename, load_offset,
1224 getenv("PATH"), elf_info) ||
1225 elf_load_file_from_path(pcs, filename, load_offset,
1226 getenv("LD_LIBRARY_PATH"), elf_info);
1227 if (!ret) ret = elf_load_file_from_dll_path(pcs, filename, load_offset, elf_info);
1233 /******************************************************************
1234 * elf_enum_modules_internal
1236 * Enumerate ELF modules from a running process
1238 static BOOL elf_enum_modules_internal(const struct process* pcs,
1239 const WCHAR* main_name,
1240 enum_modules_cb cb, void* user)
1242 struct r_debug dbg_hdr;
1246 WCHAR bufstrW[MAX_PATH];
1248 if (!pcs->dbg_hdr_addr ||
1249 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1250 &dbg_hdr, sizeof(dbg_hdr), NULL))
1253 /* Now walk the linked list. In all known ELF implementations,
1254 * the dynamic loader maintains this linked list for us. In some
1255 * cases the first entry doesn't appear with a name, in other cases it
1258 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
1260 if (!ReadProcessMemory(pcs->handle, lm_addr, &lm, sizeof(lm), NULL))
1263 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
1264 lm.l_name != NULL &&
1265 ReadProcessMemory(pcs->handle, lm.l_name, bufstr, sizeof(bufstr), NULL))
1267 bufstr[sizeof(bufstr) - 1] = '\0';
1268 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1269 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1270 if (!cb(bufstrW, (unsigned long)lm.l_addr, user)) break;
1278 struct process* pcs;
1279 struct elf_info elf_info;
1282 static BOOL elf_enum_sync_cb(const WCHAR* name, unsigned long addr, void* user)
1284 struct elf_sync* es = user;
1286 elf_search_and_load_file(es->pcs, name, addr, &es->elf_info);
1290 /******************************************************************
1291 * elf_synchronize_module_list
1293 * this functions rescans the debuggee module's list and synchronizes it with
1294 * the one from 'pcs', ie:
1295 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1296 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1298 BOOL elf_synchronize_module_list(struct process* pcs)
1300 struct module* module;
1303 for (module = pcs->lmodules; module; module = module->next)
1305 if (module->type == DMT_ELF && !module->is_virtual)
1306 module->format_info[DFI_ELF]->u.elf_info->elf_mark = 0;
1310 es.elf_info.flags = ELF_INFO_MODULE;
1311 if (!elf_enum_modules_internal(pcs, NULL, elf_enum_sync_cb, &es))
1314 module = pcs->lmodules;
1317 if (module->type == DMT_ELF && !module->is_virtual)
1319 struct elf_module_info* elf_info = module->format_info[DFI_ELF]->u.elf_info;
1321 if (!elf_info->elf_mark && !elf_info->elf_loader)
1323 module_remove(pcs, module);
1324 /* restart all over */
1325 module = pcs->lmodules;
1329 module = module->next;
1334 /******************************************************************
1337 * Lookup in a running ELF process the loader, and sets its ELF link
1338 * address (for accessing the list of loaded .so libs) in pcs.
1339 * If flags is ELF_INFO_MODULE, the module for the loader is also
1340 * added as a module into pcs.
1342 static BOOL elf_search_loader(struct process* pcs, struct elf_info* elf_info)
1347 /* All binaries are loaded with WINELOADER (if run from tree) or by the
1350 if ((ptr = getenv("WINELOADER")))
1352 WCHAR tmp[MAX_PATH];
1353 MultiByteToWideChar(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp) / sizeof(WCHAR));
1354 ret = elf_search_and_load_file(pcs, tmp, 0, elf_info);
1358 ret = elf_search_and_load_file(pcs, S_WineW, 0, elf_info);
1363 /******************************************************************
1364 * elf_read_wine_loader_dbg_info
1366 * Try to find a decent wine executable which could have loaded the debuggee
1368 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1370 struct elf_info elf_info;
1372 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
1373 if (!elf_search_loader(pcs, &elf_info)) return FALSE;
1374 elf_info.module->format_info[DFI_ELF]->u.elf_info->elf_loader = 1;
1375 module_set_module(elf_info.module, S_WineLoaderW);
1376 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
1379 /******************************************************************
1382 * Enumerates the ELF loaded modules from a running target (hProc)
1383 * This function doesn't require that someone has called SymInitialize
1384 * on this very process.
1386 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1389 struct elf_info elf_info;
1392 memset(&pcs, 0, sizeof(pcs));
1394 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_NAME;
1395 if (!elf_search_loader(&pcs, &elf_info)) return FALSE;
1396 pcs.dbg_hdr_addr = elf_info.dbg_hdr_addr;
1397 ret = elf_enum_modules_internal(&pcs, elf_info.module_name, cb, user);
1398 HeapFree(GetProcessHeap(), 0, (char*)elf_info.module_name);
1404 struct process* pcs;
1405 struct elf_info elf_info;
1410 /******************************************************************
1413 * Callback for elf_load_module, used to walk the list of loaded
1416 static BOOL elf_load_cb(const WCHAR* name, unsigned long addr, void* user)
1418 struct elf_load* el = user;
1421 /* memcmp is needed for matches when bufstr contains also version information
1422 * el->name: libc.so, name: libc.so.6.0
1424 p = strrchrW(name, '/');
1426 if (!memcmp(p, el->name, lstrlenW(el->name) * sizeof(WCHAR)))
1428 el->ret = elf_search_and_load_file(el->pcs, name, addr, &el->elf_info);
1434 /******************************************************************
1437 * loads an ELF module and stores it in process' module list
1438 * Also, find module real name and load address from
1439 * the real loaded modules list in pcs address space
1441 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1445 TRACE("(%p %s %08lx)\n", pcs, debugstr_w(name), addr);
1447 el.elf_info.flags = ELF_INFO_MODULE;
1450 if (pcs->dbg_hdr_addr) /* we're debugging a life target */
1453 /* do only the lookup from the filename, not the path (as we lookup module
1454 * name in the process' loaded module list)
1456 el.name = strrchrW(name, '/');
1457 if (!el.name++) el.name = name;
1460 if (!elf_enum_modules_internal(pcs, NULL, elf_load_cb, &el))
1466 el.ret = elf_search_and_load_file(pcs, el.name, addr, &el.elf_info);
1468 if (!el.ret) return NULL;
1469 assert(el.elf_info.module);
1470 return el.elf_info.module;
1473 #else /* !__ELF__ */
1475 BOOL elf_find_section(struct image_file_map* fmap, const char* name,
1476 unsigned sht, struct image_section_map* ism)
1481 const char* elf_map_section(struct image_section_map* ism)
1486 void elf_unmap_section(struct image_section_map* ism)
1489 unsigned elf_get_map_size(const struct image_section_map* ism)
1494 DWORD_PTR elf_get_map_rva(const struct image_section_map* ism)
1499 BOOL elf_synchronize_module_list(struct process* pcs)
1504 BOOL elf_fetch_file_info(const WCHAR* name, DWORD* base,
1505 DWORD* size, DWORD* checksum)
1510 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1515 BOOL elf_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1520 struct module* elf_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1525 BOOL elf_load_debug_info(struct module* module, struct image_file_map* fmap)
1530 int elf_is_in_thunk_area(unsigned long addr,
1531 const struct elf_thunk_area* thunks)
1535 #endif /* __ELF__ */