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, kind
= -1;
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
);
404 symt_get_info(&module
->addr_sorttab
[idx
]->symt
, TI_GET_DATAKIND
, &kind
);
406 /* If none of symbols has a correct size, we consider they are both markers
407 * Hence, we can silence this warning
408 * Also, we check that we don't have two symbols, one local, the other
409 * global which is legal
411 if ((xsize
|| ste
->symp
->st_size
) &&
412 (kind
== (ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
) ? DataIsFileStatic
: DataIsGlobal
))
413 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%08lx-%08lx>\n",
414 module
->module
.ModuleName
,
415 ste
->ht_elt
.name
, addr
, ste
->symp
->st_size
,
416 module
->addr_sorttab
[idx
]->hash_elt
.name
, xaddr
, xsize
);
420 /* see comment above */
421 module
->sortlist_valid
= FALSE
;
425 /******************************************************************
426 * elf_new_public_symbols
428 * Creates a set of public symbols from an ELF symtab
430 static int elf_new_public_symbols(struct module
* module
, struct hash_table
* symtab
)
432 struct symt_compiland
* compiland
= NULL
;
433 const char* compiland_name
= NULL
;
434 struct hash_table_iter hti
;
435 struct symtab_elt
* ste
;
437 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
439 /* FIXME: we're missing the ELF entry point here */
441 hash_table_iter_init(symtab
, &hti
, NULL
);
442 while ((ste
= hash_table_iter_up(&hti
)))
444 /* FIXME: this is not a good idea anyway... we are creating several
445 * compiland objects for a same compilation unit
446 * We try to cache the last compiland used, but it's not enough
447 * (we should here only create compilands if they are not yet
450 if (!compiland_name
|| compiland_name
!= ste
->filename
)
451 compiland
= symt_new_compiland(module
,
452 compiland_name
= ste
->filename
);
454 symt_new_public(module
, compiland
, ste
->ht_elt
.name
,
455 module
->elf_info
->elf_addr
+ ste
->symp
->st_value
,
456 ste
->symp
->st_size
, TRUE
/* FIXME */,
457 ELF32_ST_TYPE(ste
->symp
->st_info
) == STT_FUNC
);
462 /******************************************************************
463 * elf_load_debug_info
465 * Loads the symbolic information from ELF module stored in 'filename'
466 * the module has been loaded at 'load_offset' address, so symbols' address
467 * relocation is performed
469 * -1 if the file cannot be found/opened
470 * 0 if the file doesn't contain symbolic info (or this info cannot be
474 BOOL
elf_load_debug_info(struct module
* module
)
477 char* addr
= (char*)0xffffffff;
480 const Elf32_Ehdr
* ehptr
;
481 const Elf32_Shdr
* spnt
;
482 const char* shstrtab
;
484 int symtab_sect
, dynsym_sect
, stab_sect
, stabstr_sect
, debug_sect
;
486 struct hash_table ht_symtab
;
487 struct thunk_area thunks
[] =
489 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
490 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
491 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
492 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
493 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
494 {"__wine_spec_thunk_data_16", -16, 0, 0}, /* 16 => 32 thunks */
495 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
496 {"__wine_spec_thunk_data_32", -32, 0, 0}, /* 32 => 16 thunks */
499 if (module
->type
!= DMT_ELF
|| !module
->elf_info
)
501 ERR("Bad elf module '%s'\n", module
->module
.LoadedImageName
);
505 TRACE("%s\n", module
->module
.LoadedImageName
);
506 /* check that the file exists, and that the module hasn't been loaded yet */
507 if (stat(module
->module
.LoadedImageName
, &statbuf
) == -1) goto leave
;
508 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
511 * Now open the file, so that we can mmap() it.
513 if ((fd
= open(module
->module
.LoadedImageName
, O_RDONLY
)) == -1) goto leave
;
516 * Now mmap() the file.
518 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
519 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 if (symtab_sect
== -1)
550 /* if we don't have a symtab but a dynsym, process the dynsym
551 * section instead. It'll contain less (relevant) information,
552 * but it'll be better than nothing
554 if (dynsym_sect
== -1) goto leave
;
555 symtab_sect
= dynsym_sect
;
558 module
->module
.SymType
= SymExport
;
559 /* FIXME: guess a better size from ELF info */
560 pool_init(&pool
, 65536);
561 hash_table_init(&pool
, &ht_symtab
, 256);
563 /* create a hash table for the symtab */
564 elf_hash_symtab(module
, &pool
, &ht_symtab
, addr
,
565 spnt
+ symtab_sect
, spnt
+ spnt
[symtab_sect
].sh_link
,
566 sizeof(thunks
) / sizeof(thunks
[0]), thunks
);
568 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
570 if (stab_sect
!= -1 && stabstr_sect
!= -1)
572 /* OK, now just parse all of the stabs. */
573 ret
= stabs_parse(module
, addr
, module
->elf_info
->elf_addr
,
574 spnt
[stab_sect
].sh_offset
, spnt
[stab_sect
].sh_size
,
575 spnt
[stabstr_sect
].sh_offset
,
576 spnt
[stabstr_sect
].sh_size
);
579 WARN("Couldn't read correctly read stabs\n");
582 /* and fill in the missing information for stabs */
583 elf_finish_stabs_info(module
, &ht_symtab
);
585 else if (debug_sect
!= -1)
587 /* Dwarf 2 debug information */
588 FIXME("Unsupported Dwarf2 information for %s\n", module
->module
.ModuleName
);
591 if (strstr(module
->module
.ModuleName
, "<elf>") ||
592 !strcmp(module
->module
.ModuleName
, "<wine-loader>"))
594 /* add the thunks for native libraries */
595 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
596 elf_new_wine_thunks(module
, &ht_symtab
,
597 sizeof(thunks
) / sizeof(thunks
[0]), thunks
);
599 /* add all the public symbols from symtab */
600 if (elf_new_public_symbols(module
, &ht_symtab
) && !ret
) ret
= TRUE
;
605 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
606 if (fd
!= -1) close(fd
);
611 /******************************************************************
613 * returns true iff the section tag is valid
615 static unsigned is_dt_flag_valid(unsigned d_tag
)
621 #define DT_EXTRANUM 0
623 return (d_tag
>= 0 && d_tag
< DT_NUM
+ DT_PROCNUM
+ DT_EXTRANUM
)
624 #if defined(DT_LOOS) && defined(DT_HIOS)
625 || (d_tag
>= DT_LOOS
&& d_tag
< DT_HIOS
)
627 #if defined(DT_LOPROC) && defined(DT_HIPROC)
628 || (d_tag
>= DT_LOPROC
&& d_tag
< DT_HIPROC
)
633 /******************************************************************
636 * Loads the information for ELF module stored in 'filename'
637 * the module has been loaded at 'load_offset' address
639 * -1 if the file cannot be found/opened
640 * 0 if the file doesn't contain symbolic info (or this info cannot be
644 static BOOL
elf_load_file(struct process
* pcs
, const char* filename
,
645 unsigned long load_offset
, struct elf_info
* elf_info
)
647 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
649 const char* addr
= (char*)0xffffffff;
652 const Elf32_Ehdr
* ehptr
;
653 const Elf32_Shdr
* spnt
;
654 const Elf32_Phdr
* ppnt
;
655 const char* shstrtab
;
658 unsigned tmp
, page_mask
= getpagesize() - 1;
660 TRACE("Processing elf file '%s' at %08lx\n", filename
, load_offset
);
662 /* check that the file exists, and that the module hasn't been loaded yet */
663 if (stat(filename
, &statbuf
) == -1) goto leave
;
665 /* Now open the file, so that we can mmap() it. */
666 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
668 /* Now mmap() the file. */
669 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
670 if (addr
== (char*)-1) goto leave
;
672 /* Next, we need to find a few of the internal ELF headers within
673 * this thing. We need the main executable header, and the section
676 ehptr
= (const Elf32_Ehdr
*)addr
;
677 if (memcmp(ehptr
->e_ident
, elf_signature
, sizeof(elf_signature
))) goto leave
;
679 spnt
= (const Elf32_Shdr
*)(addr
+ ehptr
->e_shoff
);
680 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
682 /* grab size of module once loaded in memory */
683 ppnt
= (const Elf32_Phdr
*)(addr
+ ehptr
->e_phoff
);
684 size
= 0; start
= ~0L;
686 for (i
= 0; i
< ehptr
->e_phnum
; i
++)
688 if (ppnt
[i
].p_type
== PT_LOAD
)
690 tmp
= (ppnt
[i
].p_vaddr
+ ppnt
[i
].p_memsz
+ page_mask
) & ~page_mask
;
691 if (size
< tmp
) size
= tmp
;
692 if (ppnt
[i
].p_vaddr
< start
) start
= ppnt
[i
].p_vaddr
;
696 /* if non relocatable ELF, then remove fixed address from computation
697 * otherwise, all addresses are zero based and start has no effect
700 if (!start
&& !load_offset
)
701 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
703 if (start
&& load_offset
)
705 WARN("Non-relocatable ELF %s, but load address of 0x%08lx supplied. "
706 "Assuming load address is corrupt\n", filename
, load_offset
);
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
) goto leave
;
729 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
734 if (elf_info
->flags
& ELF_INFO_MODULE
)
736 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
,
737 (load_offset
) ? load_offset
: start
,
739 if (!elf_info
->module
) goto leave
;
740 elf_info
->module
->elf_info
= HeapAlloc(GetProcessHeap(),
741 0, sizeof(struct elf_module_info
));
742 if (elf_info
->module
->elf_info
== NULL
)
747 elf_info
->module
->elf_info
->elf_addr
= load_offset
;
749 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
751 elf_info
->module
->module
.SymType
= SymDeferred
;
754 else ret
= 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;
761 if (addr
!= (char*)0xffffffff) munmap((void*)addr
, statbuf
.st_size
);
762 if (fd
!= -1) close(fd
);
767 /******************************************************************
768 * elf_load_file_from_path
769 * tries to load an ELF file from a set of paths (separated by ':')
771 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
772 const char* filename
,
773 unsigned long load_offset
,
775 struct elf_info
* elf_info
)
781 if (!path
) return FALSE
;
783 paths
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(path
) + 1), path
);
784 for (s
= paths
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
788 fn
= HeapAlloc(GetProcessHeap(), 0, strlen(filename
) + 1 + strlen(s
) + 1);
792 strcat(fn
, filename
);
793 ret
= elf_load_file(hProcess
, fn
, load_offset
, elf_info
);
794 HeapFree(GetProcessHeap(), 0, fn
);
796 s
= (t
) ? (t
+1) : NULL
;
799 HeapFree(GetProcessHeap(), 0, paths
);
803 /******************************************************************
804 * elf_search_and_load_file
806 * lookup a file in standard ELF locations, and if found, load it
808 static BOOL
elf_search_and_load_file(struct process
* pcs
, const char* filename
,
809 unsigned long load_offset
,
810 struct elf_info
* elf_info
)
813 struct module
* module
;
815 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
816 if ((module
= module_find_by_name(pcs
, filename
, DMT_ELF
)))
818 elf_info
->module
= module
;
819 module
->elf_info
->elf_mark
= 1;
820 return module
->module
.SymType
;
823 if (strstr(filename
, "libstdc++")) return FALSE
; /* We know we can't do it */
824 ret
= elf_load_file(pcs
, filename
, load_offset
, elf_info
);
825 /* if relative pathname, try some absolute base dirs */
826 if (!ret
&& !strchr(filename
, '/'))
828 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
,
829 getenv("PATH"), elf_info
) ||
830 elf_load_file_from_path(pcs
, filename
, load_offset
,
831 getenv("LD_LIBRARY_PATH"), elf_info
) ||
832 elf_load_file_from_path(pcs
, filename
, load_offset
,
833 getenv("WINEDLLPATH"), elf_info
);
839 /******************************************************************
840 * elf_synchronize_module_list
842 * this functions rescans the debuggee module's list and synchronizes it with
843 * the one from 'pcs', ie:
844 * - if a module is in debuggee and not in pcs, it's loaded into pcs
845 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
847 BOOL
elf_synchronize_module_list(struct process
* pcs
)
849 struct r_debug dbg_hdr
;
853 struct elf_info elf_info
;
854 struct module
* module
;
856 if (!pcs
->dbg_hdr_addr
) return FALSE
;
857 if (!read_mem(pcs
->handle
, pcs
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
860 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
862 if (module
->type
== DMT_ELF
) module
->elf_info
->elf_mark
= 0;
865 elf_info
.flags
= ELF_INFO_MODULE
;
866 /* Now walk the linked list. In all known ELF implementations,
867 * the dynamic loader maintains this linked list for us. In some
868 * cases the first entry doesn't appear with a name, in other cases it
871 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
873 if (!read_mem(pcs
->handle
, (ULONG
)lm_addr
, &lm
, sizeof(lm
)))
876 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
878 read_mem(pcs
->handle
, (ULONG
)lm
.l_name
, bufstr
, sizeof(bufstr
)))
880 bufstr
[sizeof(bufstr
) - 1] = '\0';
881 elf_search_and_load_file(pcs
, bufstr
, (unsigned long)lm
.l_addr
,
886 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
888 if (module
->type
== DMT_ELF
&& !module
->elf_info
->elf_mark
&&
889 !module
->elf_info
->elf_loader
)
891 module_remove(pcs
, module
);
892 /* restart all over */
893 module
= pcs
->lmodules
;
899 /******************************************************************
900 * elf_read_wine_loader_dbg_info
902 * Try to find a decent wine executable which could have loaded the debuggee
904 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
907 struct elf_info elf_info
;
910 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
911 /* All binaries are loaded with WINELOADER (if run from tree) or by the
912 * main executable (either wine-kthread or wine-pthread)
913 * Note: the heuristic use to know whether we need to load wine-pthread or
914 * wine-kthread is not 100% safe
916 if ((ptr
= getenv("WINELOADER")))
917 ret
= elf_search_and_load_file(pcs
, ptr
, 0, &elf_info
);
920 ret
= elf_search_and_load_file(pcs
, "wine-kthread", 0, &elf_info
) ||
921 elf_search_and_load_file(pcs
, "wine-pthread", 0, &elf_info
);
923 if (!ret
) return FALSE
;
924 elf_info
.module
->elf_info
->elf_loader
= 1;
925 strcpy(elf_info
.module
->module
.ModuleName
, "<wine-loader>");
926 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
929 /******************************************************************
932 * loads an ELF module and stores it in process' module list
933 * if 'sync' is TRUE, let's find module real name and load address from
934 * the real loaded modules list in pcs address space
936 struct module
* elf_load_module(struct process
* pcs
, const char* name
)
938 struct elf_info elf_info
;
942 struct r_debug dbg_hdr
;
947 TRACE("(%p %s)\n", pcs
, name
);
949 elf_info
.flags
= ELF_INFO_MODULE
;
951 /* do only the lookup from the filename, not the path (as we lookup module name
952 * in the process' loaded module list)
954 xname
= strrchr(name
, '/');
955 if (!xname
++) xname
= name
;
957 if (!read_mem(pcs
->handle
, pcs
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
960 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
962 if (!read_mem(pcs
->handle
, (ULONG
)lm_addr
, &lm
, sizeof(lm
)))
965 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
967 read_mem(pcs
->handle
, (ULONG
)lm
.l_name
, bufstr
, sizeof(bufstr
)))
969 bufstr
[sizeof(bufstr
) - 1] = '\0';
970 /* memcmp is needed for matches when bufstr contains also version information
971 * name: libc.so, bufstr: libc.so.6.0
973 p
= strrchr(bufstr
, '/');
974 if (!p
++) p
= bufstr
;
975 if (!memcmp(p
, xname
, strlen(xname
)))
977 ret
= elf_search_and_load_file(pcs
, bufstr
,
978 (unsigned long)lm
.l_addr
, &elf_info
);
983 if (!lm_addr
|| !ret
) return NULL
;
984 assert(elf_info
.module
);
985 return elf_info
.module
;
990 BOOL
elf_synchronize_module_list(struct process
* pcs
)
995 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1000 struct module
* elf_load_module(struct process
* pcs
, const char* name
)
1005 BOOL
elf_load_debug_info(struct module
* module
)
1009 #endif /* __ELF__ */