2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifdef HAVE_SYS_MMAN_H
36 #define PATH_MAX MAX_PATH
39 #include "dbghelp_private.h"
41 #if defined(__svr4__) || defined(__sun)
48 #ifdef HAVE_SYS_ELF32_H
49 # include <sys/elf32.h>
51 #ifdef HAVE_SYS_EXEC_ELF_H
52 # include <sys/exec_elf.h>
55 # if defined(DT_COUNT)
56 # define DT_NUM DT_COUNT
58 /* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
65 #ifdef HAVE_SYS_LINK_H
66 # include <sys/link.h>
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
73 struct elf_module_info
75 unsigned long elf_addr;
76 unsigned short elf_mark : 1,
82 #define ELF_INFO_DEBUG_HEADER 0x0001
83 #define ELF_INFO_MODULE 0x0002
87 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
88 unsigned long dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
89 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
94 struct hash_table_elt ht_elt;
95 const Elf32_Sym* symp;
103 THUNK_ORDINAL ordinal;
104 unsigned long rva_start;
105 unsigned long rva_end;
108 /******************************************************************
111 * creating an internal hash table to ease use ELF symtab information lookup
113 static void elf_hash_symtab(const struct module* module, struct pool* pool,
114 struct hash_table* ht_symtab, const char* map_addr,
115 const Elf32_Shdr* symtab, const Elf32_Shdr* strtab,
116 unsigned num_areas, struct thunk_area* thunks)
121 const char* filename = NULL;
123 const Elf32_Sym* symp;
124 struct symtab_elt* ste;
126 symp = (const Elf32_Sym*)(map_addr + symtab->sh_offset);
127 nsym = symtab->sh_size / sizeof(*symp);
128 strp = (const char*)(map_addr + strtab->sh_offset);
130 for (j = 0; j < num_areas; j++)
131 thunks[j].rva_start = thunks[j].rva_end = 0;
133 for (i = 0; i < nsym; i++, symp++)
135 /* Ignore certain types of entries which really aren't of that much
138 if (ELF32_ST_TYPE(symp->st_info) == STT_SECTION || symp->st_shndx == SHN_UNDEF)
143 symname = strp + symp->st_name;
145 if (ELF32_ST_TYPE(symp->st_info) == STT_FILE)
150 for (j = 0; j < num_areas; j++)
152 if (!strcmp(symname, thunks[j].symname))
154 thunks[j].rva_start = symp->st_value;
155 thunks[j].rva_end = symp->st_value + symp->st_size;
159 if (j < num_areas) continue;
161 ste = pool_alloc(pool, sizeof(*ste));
162 /* GCC seems to emit, in some cases, a .<digit>+ suffix.
163 * This is used for static variable inside functions, so
164 * that we can have several such variables with same name in
165 * the same compilation unit
166 * We simply ignore that suffix when present (we also get rid
167 * of it in stabs parsing)
169 ptr = symname + strlen(symname) - 1;
170 ste->ht_elt.name = symname;
173 while (*ptr >= '0' && *ptr <= '9' && ptr >= symname) ptr--;
174 if (ptr > symname && *ptr == '.')
176 char* n = pool_alloc(pool, ptr - symname + 1);
177 memcpy(n, symname, ptr - symname + 1);
178 n[ptr - symname] = '\0';
179 ste->ht_elt.name = n;
183 ste->filename = filename;
185 hash_table_add(ht_symtab, &ste->ht_elt);
189 /******************************************************************
192 * lookup a symbol by name in our internal hash table for the symtab
194 static const Elf32_Sym* elf_lookup_symtab(const struct module* module,
195 const struct hash_table* ht_symtab,
196 const char* name, struct symt* compiland)
198 struct symtab_elt* weak_result = NULL; /* without compiland name */
199 struct symtab_elt* result = NULL;
200 struct hash_table_iter hti;
201 struct symtab_elt* ste;
202 const char* compiland_name;
203 const char* compiland_basename;
206 /* we need weak match up (at least) when symbols of same name,
207 * defined several times in different compilation units,
208 * are merged in a single one (hence a different filename for c.u.)
212 compiland_name = source_get(module,
213 ((struct symt_compiland*)compiland)->source);
214 compiland_basename = strrchr(compiland_name, '/');
215 if (!compiland_basename++) compiland_basename = compiland_name;
217 else compiland_name = compiland_basename = NULL;
219 hash_table_iter_init(ht_symtab, &hti, name);
220 while ((ste = hash_table_iter_up(&hti)))
222 if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
225 if ((ste->filename && !compiland_name) || (!ste->filename && compiland_name))
227 if (ste->filename && compiland_name)
229 if (strcmp(ste->filename, compiland_name))
231 base = strrchr(ste->filename, '/');
232 if (!base++) base = ste->filename;
233 if (strcmp(base, compiland_basename)) continue;
238 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
239 name, compiland_name, result->filename, result->symp->st_value,
240 ste->filename, ste->symp->st_value);
248 if (!result && !(result = weak_result))
250 FIXME("Couldn't find symbol %s.%s in symtab\n",
251 module->module.ModuleName, name);
257 /******************************************************************
258 * elf_finish_stabs_info
260 * - get any relevant information (address & size) from the bits we got from the
261 * stabs debugging information
263 static void elf_finish_stabs_info(struct module* module, struct hash_table* symtab)
265 struct hash_table_iter hti;
268 const Elf32_Sym* symp;
270 hash_table_iter_init(&module->ht_symbols, &hti, NULL);
271 while ((ptr = hash_table_iter_up(&hti)))
273 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
274 switch (sym->symt.tag)
277 if (((struct symt_function*)sym)->address != module->elf_info->elf_addr &&
278 ((struct symt_function*)sym)->size)
282 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
283 ((struct symt_function*)sym)->container);
286 ((struct symt_function*)sym)->address = module->elf_info->elf_addr +
288 ((struct symt_function*)sym)->size = symp->st_size;
292 switch (((struct symt_data*)sym)->kind)
295 case DataIsFileStatic:
296 if (((struct symt_data*)sym)->u.address != module->elf_info->elf_addr)
298 symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
299 ((struct symt_data*)sym)->container);
302 ((struct symt_data*)sym)->u.address = module->elf_info->elf_addr +
304 ((struct symt_data*)sym)->kind = (ELF32_ST_BIND(symp->st_info) == STB_LOCAL) ?
305 DataIsFileStatic : DataIsGlobal;
312 FIXME("Unsupported tag %u\n", sym->symt.tag);
316 /* since we may have changed some addresses & sizes, mark the module to be resorted */
317 module->sortlist_valid = FALSE;
320 /******************************************************************
321 * elf_load_wine_thunks
323 * creating the thunk objects for a wine native DLL
325 static int elf_new_wine_thunks(struct module* module, struct hash_table* ht_symtab,
326 unsigned num_areas, struct thunk_area* thunks)
329 struct symt_compiland* compiland = NULL;
330 const char* compiland_name = NULL;
331 struct hash_table_iter hti;
332 struct symtab_elt* ste;
336 hash_table_iter_init(ht_symtab, &hti, NULL);
337 while ((ste = hash_table_iter_up(&hti)))
339 if (ste->used) continue;
341 /* FIXME: this is not a good idea anyway... we are creating several
342 * compiland objects for a same compilation unit
343 * We try to cache the last compiland used, but it's not enough
344 * (we should here only create compilands if they are not yet
347 if (!compiland_name || compiland_name != ste->filename)
348 compiland = symt_new_compiland(module,
349 compiland_name = ste->filename);
351 addr = module->elf_info->elf_addr + ste->symp->st_value;
353 for (j = 0; j < num_areas; j++)
355 if (ste->symp->st_value >= thunks[j].rva_start &&
356 ste->symp->st_value < thunks[j].rva_end)
359 if (j < num_areas) /* thunk found */
361 symt_new_thunk(module, compiland, ste->ht_elt.name, thunks[j].ordinal,
362 addr, ste->symp->st_size);
368 idx = symt_find_nearest(module, addr);
370 symt_get_info(&module->addr_sorttab[idx]->symt,
371 TI_GET_ADDRESS, &ref_addr);
372 if (idx == -1 || addr != ref_addr)
374 /* creating public symbols for all the ELF symbols which haven't been
375 * used yet (ie we have no debug information on them)
376 * That's the case, for example, of the .spec.c files
378 if (ELF32_ST_TYPE(ste->symp->st_info) == STT_FUNC)
380 symt_new_function(module, compiland, ste->ht_elt.name,
381 addr, ste->symp->st_size, NULL);
385 symt_new_global_variable(module, compiland, ste->ht_elt.name,
386 ELF32_ST_BIND(ste->symp->st_info) == STB_LOCAL,
387 addr, ste->symp->st_size, NULL);
389 /* FIXME: this is a hack !!!
390 * we are adding new symbols, but as we're parsing a symbol table
391 * (hopefully without duplicate symbols) we delay rebuilding the sorted
392 * module table until we're done with the symbol table
393 * Otherwise, as we intertwine symbols's add and lookup, performance
396 module->sortlist_valid = TRUE;
398 else if (strcmp(ste->ht_elt.name, module->addr_sorttab[idx]->hash_elt.name))
400 DWORD xaddr = 0, xsize = 0;
402 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_ADDRESS, &xaddr);
403 symt_get_info(&module->addr_sorttab[idx]->symt, TI_GET_LENGTH, &xsize);
405 /* if none of symbols has a correct size, we consider they are both markers
406 * Hence, we can silence this warning
408 if (xsize || ste->symp->st_size)
409 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%08lx-%08lx>\n",
410 module->module.ModuleName,
411 ste->ht_elt.name, addr, ste->symp->st_size,
412 module->addr_sorttab[idx]->hash_elt.name, xaddr, xsize);
416 /* see comment above */
417 module->sortlist_valid = FALSE;
421 /******************************************************************
422 * elf_new_public_symbols
424 * Creates a set of public symbols from an ELF symtab
426 static int elf_new_public_symbols(struct module* module, struct hash_table* symtab,
429 struct symt_compiland* compiland = NULL;
430 const char* compiland_name = NULL;
431 struct hash_table_iter hti;
432 struct symtab_elt* ste;
434 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
436 hash_table_iter_init(symtab, &hti, NULL);
437 while ((ste = hash_table_iter_up(&hti)))
439 /* FIXME: this is not a good idea anyway... we are creating several
440 * compiland objects for a same compilation unit
441 * We try to cache the last compiland used, but it's not enough
442 * (we should here only create compilands if they are not yet
445 if (!compiland_name || compiland_name != ste->filename)
446 compiland = symt_new_compiland(module,
447 compiland_name = ste->filename);
449 if (dont_check || !(dbghelp_options & SYMOPT_AUTO_PUBLICS) ||
450 symt_find_nearest(module, module->elf_info->elf_addr + ste->symp->st_value) == -1)
452 symt_new_public(module, compiland, ste->ht_elt.name,
453 module->elf_info->elf_addr + ste->symp->st_value,
454 ste->symp->st_size, TRUE /* FIXME */,
455 ELF32_ST_TYPE(ste->symp->st_info) == STT_FUNC);
461 /******************************************************************
462 * elf_load_debug_info
464 * Loads the symbolic information from ELF module stored in 'filename'
465 * the module has been loaded at 'load_offset' address, so symbols' address
466 * relocation is performed
468 * -1 if the file cannot be found/opened
469 * 0 if the file doesn't contain symbolic info (or this info cannot be
473 SYM_TYPE elf_load_debug_info(struct module* module)
475 SYM_TYPE sym_type = -1;
476 char* addr = (char*)0xffffffff;
479 const Elf32_Ehdr* ehptr;
480 const Elf32_Shdr* spnt;
481 const char* shstrtab;
483 int symtab_sect, dynsym_sect, stab_sect, stabstr_sect, debug_sect;
485 struct hash_table ht_symtab;
486 struct thunk_area thunks[] =
488 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE, 0, 0}, /* inter DLL calls */
489 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
490 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
491 {"__wine_delay_load", THUNK_ORDINAL_LOAD, 0, 0}, /* delayed inter DLL calls */
492 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
493 {"__wine_spec_thunk_data_16", -16, 0, 0}, /* 16 => 32 thunks */
494 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
495 {"__wine_spec_thunk_data_32", -32, 0, 0}, /* 32 => 16 thunks */
498 if (module->type != DMT_ELF || !module->elf_info)
500 ERR("Bad elf module '%s'\n", module->module.LoadedImageName);
504 TRACE("%s\n", module->module.LoadedImageName);
505 /* check that the file exists, and that the module hasn't been loaded yet */
506 if (stat(module->module.LoadedImageName, &statbuf) == -1) goto leave;
507 if (S_ISDIR(statbuf.st_mode)) goto leave;
510 * Now open the file, so that we can mmap() it.
512 if ((fd = open(module->module.LoadedImageName, O_RDONLY)) == -1) goto leave;
515 * Now mmap() the file.
517 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
518 if (addr == (char*)0xffffffff) goto leave;
522 * Next, we need to find a few of the internal ELF headers within
523 * this thing. We need the main executable header, and the section
526 ehptr = (Elf32_Ehdr*)addr;
527 spnt = (Elf32_Shdr*)(addr + ehptr->e_shoff);
528 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
530 symtab_sect = dynsym_sect = stab_sect = stabstr_sect = debug_sect = -1;
532 for (i = 0; i < ehptr->e_shnum; i++)
534 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
536 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
538 if (strcmp(shstrtab + spnt[i].sh_name, ".debug_info") == 0)
540 if ((strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0) &&
541 (spnt[i].sh_type == SHT_SYMTAB))
543 if ((strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0) &&
544 (spnt[i].sh_type == SHT_DYNSYM))
548 sym_type = SymExport;
550 if (symtab_sect == -1)
552 /* if we don't have a symtab but a dynsym, process the dynsym
553 * section instead. It'll contain less (relevant) information,
554 * but it'll be better than nothing
556 if (dynsym_sect == -1) goto leave;
557 symtab_sect = dynsym_sect;
560 /* FIXME: guess a better size from ELF info */
561 pool_init(&pool, 65536);
562 hash_table_init(&pool, &ht_symtab, 256);
564 /* create a hash table for the symtab */
565 elf_hash_symtab(module, &pool, &ht_symtab, addr,
566 spnt + symtab_sect, spnt + spnt[symtab_sect].sh_link,
567 sizeof(thunks) / sizeof(thunks[0]), thunks);
569 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
571 if (stab_sect != -1 && stabstr_sect != -1)
573 /* OK, now just parse all of the stabs. */
574 sym_type = stabs_parse(module, addr, module->elf_info->elf_addr,
575 spnt[stab_sect].sh_offset, spnt[stab_sect].sh_size,
576 spnt[stabstr_sect].sh_offset,
577 spnt[stabstr_sect].sh_size);
580 WARN("Couldn't read correctly read stabs\n");
583 /* and fill in the missing information for stabs */
584 elf_finish_stabs_info(module, &ht_symtab);
586 else if (debug_sect != -1)
588 /* Dwarf 2 debug information */
589 FIXME("Unsupported Dwarf2 information\n");
593 if (strstr(module->module.ModuleName, "<elf>") ||
594 !strcmp(module->module.ModuleName, "<wine-loader>"))
596 /* add the thunks for native libraries */
597 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
598 elf_new_wine_thunks(module, &ht_symtab,
599 sizeof(thunks) / sizeof(thunks[0]), thunks);
600 /* add the public symbols from symtab
601 * (only if they haven't been defined yet)
603 elf_new_public_symbols(module, &ht_symtab, FALSE);
607 /* add all the public symbols from symtab */
608 elf_new_public_symbols(module, &ht_symtab, TRUE);
614 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
615 if (fd != -1) close(fd);
617 return module->module.SymType = sym_type;
620 /******************************************************************
622 * returns true iff the section tag is valid
624 static unsigned is_dt_flag_valid(unsigned d_tag)
630 #define DT_EXTRANUM 0
632 return (d_tag >= 0 && d_tag < DT_NUM + DT_PROCNUM + DT_EXTRANUM)
633 #if defined(DT_LOOS) && defined(DT_HIOS)
634 || (d_tag >= DT_LOOS && d_tag < DT_HIOS)
636 #if defined(DT_LOPROC) && defined(DT_HIPROC)
637 || (d_tag >= DT_LOPROC && d_tag < DT_HIPROC)
642 /******************************************************************
645 * Loads the information for ELF module stored in 'filename'
646 * the module has been loaded at 'load_offset' address
648 * -1 if the file cannot be found/opened
649 * 0 if the file doesn't contain symbolic info (or this info cannot be
653 static SYM_TYPE elf_load_file(struct process* pcs, const char* filename,
654 unsigned long load_offset, struct elf_info* elf_info)
656 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
657 SYM_TYPE sym_type = -1;
658 const char* addr = (char*)0xffffffff;
661 const Elf32_Ehdr* ehptr;
662 const Elf32_Shdr* spnt;
663 const Elf32_Phdr* ppnt;
664 const char* shstrtab;
667 unsigned tmp, page_mask = getpagesize() - 1;
669 TRACE("Processing elf file '%s' at %08lx\n", filename, load_offset);
671 /* check that the file exists, and that the module hasn't been loaded yet */
672 if (stat(filename, &statbuf) == -1) goto leave;
674 /* Now open the file, so that we can mmap() it. */
675 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
677 /* Now mmap() the file. */
678 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
679 if (addr == (char*)-1) goto leave;
683 /* Next, we need to find a few of the internal ELF headers within
684 * this thing. We need the main executable header, and the section
687 ehptr = (const Elf32_Ehdr*)addr;
688 if (memcmp(ehptr->e_ident, elf_signature, sizeof(elf_signature))) goto leave;
690 spnt = (const Elf32_Shdr*)(addr + ehptr->e_shoff);
691 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
693 /* if non relocatable ELF, then remove fixed address from computation
694 * otherwise, all addresses are zero based
696 delta = (load_offset == 0) ? ehptr->e_entry : 0;
698 /* grab size of module once loaded in memory */
699 ppnt = (const Elf32_Phdr*)(addr + ehptr->e_phoff);
701 for (i = 0; i < ehptr->e_phnum; i++)
703 if (ppnt[i].p_type == PT_LOAD)
705 tmp = (ppnt[i].p_vaddr + ppnt[i].p_memsz + page_mask) & ~page_mask;
706 if (size < tmp) size = tmp;
710 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
712 for (i = 0; i < ehptr->e_shnum; i++)
714 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
715 spnt[i].sh_type == SHT_DYNAMIC)
718 char* ptr = (char*)spnt[i].sh_addr;
723 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
724 len != sizeof(dyn) || !is_dt_flag_valid(dyn.d_tag))
727 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
728 if (dyn.d_tag == DT_NULL)
733 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
738 if (elf_info->flags & ELF_INFO_MODULE)
740 elf_info->module = module_new(pcs, filename, DMT_ELF,
741 (load_offset) ? load_offset : ehptr->e_entry,
743 if (elf_info->module)
745 elf_info->module->elf_info = HeapAlloc(GetProcessHeap(),
746 0, sizeof(struct elf_module_info));
747 if (elf_info->module->elf_info == NULL)
752 elf_info->module->elf_info->elf_addr = load_offset;
753 elf_info->module->module.SymType = sym_type =
754 (dbghelp_options & SYMOPT_DEFERRED_LOADS) ? SymDeferred :
755 elf_load_debug_info(elf_info->module);
756 elf_info->module->elf_info->elf_mark = 1;
757 elf_info->module->elf_info->elf_loader = 0;
763 if (addr != (char*)0xffffffff) munmap((void*)addr, statbuf.st_size);
764 if (fd != -1) close(fd);
769 /******************************************************************
770 * elf_load_file_from_path
771 * tries to load an ELF file from a set of paths (separated by ':')
773 static SYM_TYPE elf_load_file_from_path(HANDLE hProcess,
774 const char* filename,
775 unsigned long load_offset,
777 struct elf_info* elf_info)
779 SYM_TYPE sym_type = -1;
783 if (!path) return sym_type;
785 paths = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(path) + 1), path);
786 for (s = paths; s && *s; s = (t) ? (t+1) : NULL)
790 fn = HeapAlloc(GetProcessHeap(), 0, strlen(filename) + 1 + strlen(s) + 1);
794 strcat(fn, filename);
795 sym_type = elf_load_file(hProcess, fn, load_offset, elf_info);
796 HeapFree(GetProcessHeap(), 0, fn);
797 if (sym_type != -1) break;
798 s = (t) ? (t+1) : NULL;
801 HeapFree(GetProcessHeap(), 0, paths);
805 /******************************************************************
806 * elf_search_and_load_file
808 * lookup a file in standard ELF locations, and if found, load it
810 static SYM_TYPE elf_search_and_load_file(struct process* pcs, const char* filename,
811 unsigned long load_offset, struct elf_info* elf_info)
813 SYM_TYPE sym_type = -1;
814 struct module* module;
816 if (filename == NULL || *filename == '\0') return sym_type;
817 if ((module = module_find_by_name(pcs, filename, DMT_ELF)))
819 elf_info->module = module;
820 module->elf_info->elf_mark = 1;
821 return module->module.SymType;
824 if (strstr(filename, "libstdc++")) return -1; /* We know we can't do it */
825 sym_type = elf_load_file(pcs, filename, load_offset, elf_info);
826 /* if relative pathname, try some absolute base dirs */
827 if (sym_type == -1 && !strchr(filename, '/'))
829 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
830 getenv("PATH"), elf_info);
832 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
833 getenv("LD_LIBRARY_PATH"), elf_info);
835 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
836 getenv("WINEDLLPATH"), elf_info);
842 /******************************************************************
843 * elf_synchronize_module_list
845 * this functions rescans the debuggee module's list and synchronizes it with
846 * the one from 'pcs', ie:
847 * - if a module is in debuggee and not in pcs, it's loaded into pcs
848 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
850 BOOL elf_synchronize_module_list(struct process* pcs)
852 struct r_debug dbg_hdr;
856 struct elf_info elf_info;
857 struct module* module;
859 if (!pcs->dbg_hdr_addr) return FALSE;
860 if (!read_mem(pcs->handle, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
863 for (module = pcs->lmodules; module; module = module->next)
865 if (module->type == DMT_ELF) module->elf_info->elf_mark = 0;
868 elf_info.flags = ELF_INFO_MODULE;
869 /* Now walk the linked list. In all known ELF implementations,
870 * the dynamic loader maintains this linked list for us. In some
871 * cases the first entry doesn't appear with a name, in other cases it
874 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
876 if (!read_mem(pcs->handle, (ULONG)lm_addr, &lm, sizeof(lm)))
879 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
881 read_mem(pcs->handle, (ULONG)lm.l_name, bufstr, sizeof(bufstr)))
883 bufstr[sizeof(bufstr) - 1] = '\0';
884 elf_search_and_load_file(pcs, bufstr, (unsigned long)lm.l_addr,
889 for (module = pcs->lmodules; module; module = module->next)
891 if (module->type == DMT_ELF && !module->elf_info->elf_mark &&
892 !module->elf_info->elf_loader)
894 module_remove(pcs, module);
895 /* restart all over */
896 module = pcs->lmodules;
902 /******************************************************************
903 * elf_read_wine_loader_dbg_info
905 * Try to find a decent wine executable which could have loaded the debuggee
907 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
911 struct elf_info elf_info;
913 elf_info.flags = ELF_INFO_DEBUG_HEADER | ELF_INFO_MODULE;
914 /* All binaries are loaded with WINELOADER (if run from tree) or by the
915 * main executable (either wine-kthread or wine-pthread)
916 * Note: the heuristic use to know whether we need to load wine-pthread or
917 * wine-kthread is not 100% safe
919 if ((ptr = getenv("WINELOADER")))
920 sym_type = elf_search_and_load_file(pcs, ptr, 0, &elf_info);
923 if ((sym_type = elf_search_and_load_file(pcs, "wine-kthread", 0, &elf_info)) == -1)
924 sym_type = elf_search_and_load_file(pcs, "wine-pthread", 0, &elf_info);
926 if (sym_type < 0) return FALSE;
927 elf_info.module->elf_info->elf_loader = 1;
928 strcpy(elf_info.module->module.ModuleName, "<wine-loader>");
929 return (pcs->dbg_hdr_addr = elf_info.dbg_hdr_addr) != 0;
932 /******************************************************************
935 * loads an ELF module and stores it in process' module list
936 * if 'sync' is TRUE, let's find module real name and load address from
937 * the real loaded modules list in pcs address space
939 struct module* elf_load_module(struct process* pcs, const char* name)
941 struct elf_info elf_info;
942 SYM_TYPE sym_type = -1;
945 struct r_debug dbg_hdr;
950 TRACE("(%p %s)\n", pcs, name);
952 elf_info.flags = ELF_INFO_MODULE;
954 /* do only the lookup from the filename, not the path (as we lookup module name
955 * in the process' loaded module list)
957 xname = strrchr(name, '/');
958 if (!xname++) xname = name;
960 if (!read_mem(pcs->handle, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
963 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
965 if (!read_mem(pcs->handle, (ULONG)lm_addr, &lm, sizeof(lm)))
968 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
970 read_mem(pcs->handle, (ULONG)lm.l_name, bufstr, sizeof(bufstr)))
972 bufstr[sizeof(bufstr) - 1] = '\0';
973 /* memcmp is needed for matches when bufstr contains also version information
974 * name: libc.so, bufstr: libc.so.6.0
976 p = strrchr(bufstr, '/');
977 if (!p++) p = bufstr;
978 if (!memcmp(p, xname, strlen(xname)))
980 sym_type = elf_search_and_load_file(pcs, bufstr,
981 (unsigned long)lm.l_addr, &elf_info);
986 if (!lm_addr || sym_type == -1) return NULL;
987 assert(elf_info.module);
988 return elf_info.module;
993 BOOL elf_synchronize_module_list(struct process* pcs)
998 BOOL elf_read_wine_loader_dbg_info(struct process* pcs)
1003 struct module* elf_load_module(struct process* pcs, const char* name)
1008 SYM_TYPE elf_load_debug_info(struct module* module)
1012 #endif /* __ELF__ */