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"
54 #ifdef HAVE_SYS_ELF32_H
55 # include <sys/elf32.h>
57 #ifdef HAVE_SYS_EXEC_ELF_H
58 # include <sys/exec_elf.h>
61 # if defined(DT_COUNT)
62 # define DT_NUM DT_COUNT
64 /* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
71 #ifdef HAVE_SYS_LINK_H
72 # include <sys/link.h>
75 #include "wine/library.h"
76 #include "wine/debug.h"
78 struct elf_module_info
81 unsigned short elf_mark
: 1,
87 #define ELF_INFO_DEBUG_HEADER 0x0001
88 #define ELF_INFO_MODULE 0x0002
89 #define ELF_INFO_NAME 0x0004
91 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
95 unsigned flags
; /* IN one (or several) of the ELF_INFO constants */
96 DWORD_PTR dbg_hdr_addr
; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
97 struct module
* module
; /* OUT loaded module (if ELF_INFO_MODULE is set) */
98 const WCHAR
* module_name
; /* OUT found module name (if ELF_INFO_NAME is set) */
102 #define Elf_Ehdr Elf64_Ehdr
103 #define Elf_Shdr Elf64_Shdr
104 #define Elf_Phdr Elf64_Phdr
105 #define Elf_Dyn Elf64_Dyn
106 #define Elf_Sym Elf64_Sym
108 #define Elf_Ehdr Elf32_Ehdr
109 #define Elf_Shdr Elf32_Shdr
110 #define Elf_Phdr Elf32_Phdr
111 #define Elf_Dyn Elf32_Dyn
112 #define Elf_Sym Elf32_Sym
115 /* structure holding information while handling an ELF image
116 * allows one by one section mapping for memory savings
129 const char* shstrtab
;
130 struct elf_file_map
* alternate
; /* another ELF file (linked to this one) */
133 struct elf_section_map
135 struct elf_file_map
* fmap
;
141 struct hash_table_elt ht_elt
;
143 struct symt_compiland
* compiland
;
147 struct elf_thunk_area
150 THUNK_ORDINAL ordinal
;
151 unsigned long rva_start
;
152 unsigned long rva_end
;
155 /******************************************************************
158 * Maps a single section into memory from an ELF file
160 static const char* elf_map_section(struct elf_section_map
* esm
)
162 unsigned pgsz
= getpagesize();
165 if (esm
->sidx
< 0 || esm
->sidx
>= esm
->fmap
->elfhdr
.e_shnum
||
166 esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_type
== SHT_NOBITS
)
169 /* align required information on page size (we assume pagesize is a power of 2) */
170 ofst
= esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
171 size
= ((esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_offset
+
172 esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
173 esm
->fmap
->sect
[esm
->sidx
].mapped
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
,
174 esm
->fmap
->fd
, ofst
);
175 if (esm
->fmap
->sect
[esm
->sidx
].mapped
== ELF_NO_MAP
) return ELF_NO_MAP
;
176 return esm
->fmap
->sect
[esm
->sidx
].mapped
+ (esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_offset
& (pgsz
- 1));
179 /******************************************************************
182 * Finds a section by name (and type) into memory from an ELF file
183 * or its alternate if any
185 static BOOL
elf_find_section(struct elf_file_map
* fmap
, const char* name
,
186 unsigned sht
, struct elf_section_map
* esm
)
192 if (fmap
->shstrtab
== ELF_NO_MAP
)
194 struct elf_section_map hdr_esm
= {fmap
, fmap
->elfhdr
.e_shstrndx
};
195 if ((fmap
->shstrtab
= elf_map_section(&hdr_esm
)) == ELF_NO_MAP
) break;
197 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
199 if (strcmp(fmap
->shstrtab
+ fmap
->sect
[i
].shdr
.sh_name
, name
) == 0 &&
200 (sht
== SHT_NULL
|| sht
== fmap
->sect
[i
].shdr
.sh_type
))
207 fmap
= fmap
->alternate
;
214 /******************************************************************
217 * Unmaps a single section from memory
219 static void elf_unmap_section(struct elf_section_map
* esm
)
221 if (esm
->sidx
>= 0 && esm
->sidx
< esm
->fmap
->elfhdr
.e_shnum
&& esm
->fmap
->sect
[esm
->sidx
].mapped
!= ELF_NO_MAP
)
223 unsigned pgsz
= getpagesize();
226 ofst
= esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
227 size
= ((esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_offset
+
228 esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
229 if (munmap((char*)esm
->fmap
->sect
[esm
->sidx
].mapped
, size
) < 0)
230 WARN("Couldn't unmap the section\n");
231 esm
->fmap
->sect
[esm
->sidx
].mapped
= ELF_NO_MAP
;
235 static void elf_end_find(struct elf_file_map
* fmap
)
237 struct elf_section_map esm
;
242 esm
.sidx
= fmap
->elfhdr
.e_shstrndx
;
243 elf_unmap_section(&esm
);
244 fmap
->shstrtab
= ELF_NO_MAP
;
245 fmap
= fmap
->alternate
;
249 /******************************************************************
252 * Get the size of an ELF section
254 static inline unsigned elf_get_map_size(const struct elf_section_map
* esm
)
256 if (esm
->sidx
< 0 || esm
->sidx
>= esm
->fmap
->elfhdr
.e_shnum
)
258 return esm
->fmap
->sect
[esm
->sidx
].shdr
.sh_size
;
261 /******************************************************************
264 * Maps an ELF file into memory (and checks it's a real ELF file)
266 static BOOL
elf_map_file(const WCHAR
* filenameW
, struct elf_file_map
* fmap
)
268 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
272 unsigned tmp
, page_mask
= getpagesize() - 1;
277 len
= WideCharToMultiByte(CP_UNIXCP
, 0, filenameW
, -1, NULL
, 0, NULL
, NULL
);
278 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
279 WideCharToMultiByte(CP_UNIXCP
, 0, filenameW
, -1, filename
, len
, NULL
, NULL
);
282 fmap
->shstrtab
= ELF_NO_MAP
;
283 fmap
->alternate
= NULL
;
285 /* check that the file exists, and that the module hasn't been loaded yet */
286 if (stat(filename
, &statbuf
) == -1 || S_ISDIR(statbuf
.st_mode
)) goto done
;
288 /* Now open the file, so that we can mmap() it. */
289 if ((fmap
->fd
= open(filename
, O_RDONLY
)) == -1) goto done
;
291 if (read(fmap
->fd
, &fmap
->elfhdr
, sizeof(fmap
->elfhdr
)) != sizeof(fmap
->elfhdr
))
293 /* and check for an ELF header */
294 if (memcmp(fmap
->elfhdr
.e_ident
,
295 elf_signature
, sizeof(elf_signature
))) goto done
;
296 /* and check 32 vs 64 size according to current machine */
298 if (fmap
->elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) goto done
;
300 if (fmap
->elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) goto done
;
302 fmap
->sect
= HeapAlloc(GetProcessHeap(), 0,
303 fmap
->elfhdr
.e_shnum
* sizeof(fmap
->sect
[0]));
304 if (!fmap
->sect
) goto done
;
306 lseek(fmap
->fd
, fmap
->elfhdr
.e_shoff
, SEEK_SET
);
307 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
309 read(fmap
->fd
, &fmap
->sect
[i
].shdr
, sizeof(fmap
->sect
[i
].shdr
));
310 fmap
->sect
[i
].mapped
= ELF_NO_MAP
;
313 /* grab size of module once loaded in memory */
314 lseek(fmap
->fd
, fmap
->elfhdr
.e_phoff
, SEEK_SET
);
316 fmap
->elf_start
= ~0L;
317 for (i
= 0; i
< fmap
->elfhdr
.e_phnum
; i
++)
319 if (read(fmap
->fd
, &phdr
, sizeof(phdr
)) == sizeof(phdr
) &&
320 phdr
.p_type
== PT_LOAD
)
322 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
323 if (fmap
->elf_size
< tmp
) fmap
->elf_size
= tmp
;
324 if (phdr
.p_vaddr
< fmap
->elf_start
) fmap
->elf_start
= phdr
.p_vaddr
;
327 /* if non relocatable ELF, then remove fixed address from computation
328 * otherwise, all addresses are zero based and start has no effect
330 fmap
->elf_size
-= fmap
->elf_start
;
333 HeapFree(GetProcessHeap(), 0, filename
);
337 /******************************************************************
340 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
342 static void elf_unmap_file(struct elf_file_map
* fmap
)
348 struct elf_section_map esm
;
350 for (esm
.sidx
= 0; esm
.sidx
< fmap
->elfhdr
.e_shnum
; esm
.sidx
++)
352 elf_unmap_section(&esm
);
354 HeapFree(GetProcessHeap(), 0, fmap
->sect
);
357 fmap
= fmap
->alternate
;
361 /******************************************************************
362 * elf_is_in_thunk_area
364 * Check whether an address lies within one of the thunk area we
367 int elf_is_in_thunk_area(unsigned long addr
,
368 const struct elf_thunk_area
* thunks
)
372 if (thunks
) for (i
= 0; thunks
[i
].symname
; i
++)
374 if (addr
>= thunks
[i
].rva_start
&& addr
< thunks
[i
].rva_end
)
380 /******************************************************************
383 * creating an internal hash table to ease use ELF symtab information lookup
385 static void elf_hash_symtab(struct module
* module
, struct pool
* pool
,
386 struct hash_table
* ht_symtab
, struct elf_file_map
* fmap
,
387 struct elf_thunk_area
* thunks
)
392 struct symt_compiland
* compiland
= NULL
;
395 struct symtab_elt
* ste
;
396 struct elf_section_map esm
, esm_str
;
398 if (!elf_find_section(fmap
, ".symtab", SHT_SYMTAB
, &esm
) &&
399 !elf_find_section(fmap
, ".dynsym", SHT_DYNSYM
, &esm
)) return;
400 if ((symp
= (const Elf_Sym
*)elf_map_section(&esm
)) == ELF_NO_MAP
) return;
402 esm_str
.sidx
= fmap
->sect
[esm
.sidx
].shdr
.sh_link
;
403 if ((strp
= elf_map_section(&esm_str
)) == ELF_NO_MAP
) return;
405 nsym
= elf_get_map_size(&esm
) / sizeof(*symp
);
407 for (j
= 0; thunks
[j
].symname
; j
++)
408 thunks
[j
].rva_start
= thunks
[j
].rva_end
= 0;
410 for (i
= 0; i
< nsym
; i
++, symp
++)
412 /* Ignore certain types of entries which really aren't of that much
415 if ((ELF32_ST_TYPE(symp
->st_info
) != STT_NOTYPE
&&
416 ELF32_ST_TYPE(symp
->st_info
) != STT_FILE
&&
417 ELF32_ST_TYPE(symp
->st_info
) != STT_OBJECT
&&
418 ELF32_ST_TYPE(symp
->st_info
) != STT_FUNC
) ||
419 symp
->st_shndx
== SHN_UNDEF
)
424 symname
= strp
+ symp
->st_name
;
426 /* handle some specific symtab (that we'll throw away when done) */
427 switch (ELF32_ST_TYPE(symp
->st_info
))
431 compiland
= symt_new_compiland(module
, symp
->st_value
,
432 source_new(module
, NULL
, symname
));
437 /* we are only interested in wine markers inserted by winebuild */
438 for (j
= 0; thunks
[j
].symname
; j
++)
440 if (!strcmp(symname
, thunks
[j
].symname
))
442 thunks
[j
].rva_start
= symp
->st_value
;
443 thunks
[j
].rva_end
= symp
->st_value
+ symp
->st_size
;
450 /* FIXME: we don't need to handle them (GCC internals)
451 * Moreover, they screw up our symbol lookup :-/
453 if (symname
[0] == '.' && symname
[1] == 'L' && isdigit(symname
[2]))
456 ste
= pool_alloc(pool
, sizeof(*ste
));
457 ste
->ht_elt
.name
= symname
;
458 /* GCC emits, in some cases, a .<digit>+ suffix.
459 * This is used for static variable inside functions, so
460 * that we can have several such variables with same name in
461 * the same compilation unit
462 * We simply ignore that suffix when present (we also get rid
463 * of it in stabs parsing)
465 ptr
= symname
+ strlen(symname
) - 1;
468 while (isdigit(*ptr
) && ptr
>= symname
) ptr
--;
469 if (ptr
> symname
&& *ptr
== '.')
471 char* n
= pool_alloc(pool
, ptr
- symname
+ 1);
472 memcpy(n
, symname
, ptr
- symname
+ 1);
473 n
[ptr
- symname
] = '\0';
474 ste
->ht_elt
.name
= n
;
478 ste
->compiland
= compiland
;
480 hash_table_add(ht_symtab
, &ste
->ht_elt
);
482 /* as we added in the ht_symtab pointers to the symbols themselves,
483 * we cannot unmap yet the sections, it will be done when we're over
488 /******************************************************************
491 * lookup a symbol by name in our internal hash table for the symtab
493 static const Elf_Sym
* elf_lookup_symtab(const struct module
* module
,
494 const struct hash_table
* ht_symtab
,
495 const char* name
, const struct symt
* compiland
)
497 struct symtab_elt
* weak_result
= NULL
; /* without compiland name */
498 struct symtab_elt
* result
= NULL
;
499 struct hash_table_iter hti
;
500 struct symtab_elt
* ste
;
501 const char* compiland_name
;
502 const char* compiland_basename
;
505 /* we need weak match up (at least) when symbols of same name,
506 * defined several times in different compilation units,
507 * are merged in a single one (hence a different filename for c.u.)
511 compiland_name
= source_get(module
,
512 ((const struct symt_compiland
*)compiland
)->source
);
513 compiland_basename
= strrchr(compiland_name
, '/');
514 if (!compiland_basename
++) compiland_basename
= compiland_name
;
516 else compiland_name
= compiland_basename
= NULL
;
518 hash_table_iter_init(ht_symtab
, &hti
, name
);
519 while ((ste
= hash_table_iter_up(&hti
)))
521 if (ste
->used
|| strcmp(ste
->ht_elt
.name
, name
)) continue;
524 if ((ste
->compiland
&& !compiland_name
) || (!ste
->compiland
&& compiland_name
))
526 if (ste
->compiland
&& compiland_name
)
528 const char* filename
= source_get(module
, ste
->compiland
->source
);
529 if (strcmp(filename
, compiland_name
))
531 base
= strrchr(filename
, '/');
532 if (!base
++) base
= filename
;
533 if (strcmp(base
, compiland_basename
)) continue;
538 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
539 name
, compiland_name
,
540 source_get(module
, result
->compiland
->source
), (unsigned int)result
->symp
->st_value
,
541 source_get(module
, ste
->compiland
->source
), (unsigned int)ste
->symp
->st_value
);
549 if (!result
&& !(result
= weak_result
))
551 FIXME("Couldn't find symbol %s!%s in symtab\n",
552 debugstr_w(module
->module
.ModuleName
), name
);
558 /******************************************************************
559 * elf_finish_stabs_info
561 * - get any relevant information (address & size) from the bits we got from the
562 * stabs debugging information
564 static void elf_finish_stabs_info(struct module
* module
, const struct hash_table
* symtab
)
566 struct hash_table_iter hti
;
571 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
572 while ((ptr
= hash_table_iter_up(&hti
)))
574 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
575 switch (sym
->symt
.tag
)
578 if (((struct symt_function
*)sym
)->address
!= module
->elf_info
->elf_addr
&&
579 ((struct symt_function
*)sym
)->size
)
583 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
584 ((struct symt_function
*)sym
)->container
);
587 if (((struct symt_function
*)sym
)->address
!= module
->elf_info
->elf_addr
&&
588 ((struct symt_function
*)sym
)->address
!= module
->elf_info
->elf_addr
+ symp
->st_value
)
589 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
590 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
591 ((struct symt_function
*)sym
)->address
, module
->elf_info
->elf_addr
+ symp
->st_value
);
592 if (((struct symt_function
*)sym
)->size
&& ((struct symt_function
*)sym
)->size
!= symp
->st_size
)
593 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
594 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
595 ((struct symt_function
*)sym
)->size
, (unsigned int)symp
->st_size
);
597 ((struct symt_function
*)sym
)->address
= module
->elf_info
->elf_addr
+
599 ((struct symt_function
*)sym
)->size
= symp
->st_size
;
601 FIXME("Couldn't find %s!%s\n",
602 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
605 switch (((struct symt_data
*)sym
)->kind
)
608 case DataIsFileStatic
:
609 if (((struct symt_data
*)sym
)->u
.var
.offset
!= module
->elf_info
->elf_addr
)
611 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
612 ((struct symt_data
*)sym
)->container
);
615 if (((struct symt_data
*)sym
)->u
.var
.offset
!= module
->elf_info
->elf_addr
&&
616 ((struct symt_data
*)sym
)->u
.var
.offset
!= module
->elf_info
->elf_addr
+ symp
->st_value
)
617 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
618 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
619 ((struct symt_function
*)sym
)->address
, module
->elf_info
->elf_addr
+ symp
->st_value
);
620 ((struct symt_data
*)sym
)->u
.var
.offset
= module
->elf_info
->elf_addr
+
622 ((struct symt_data
*)sym
)->kind
= (ELF32_ST_BIND(symp
->st_info
) == STB_LOCAL
) ?
623 DataIsFileStatic
: DataIsGlobal
;
625 FIXME("Couldn't find %s!%s\n",
626 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
632 FIXME("Unsupported tag %u\n", sym
->symt
.tag
);
636 /* since we may have changed some addresses & sizes, mark the module to be resorted */
637 module
->sortlist_valid
= FALSE
;
640 /******************************************************************
641 * elf_load_wine_thunks
643 * creating the thunk objects for a wine native DLL
645 static int elf_new_wine_thunks(struct module
* module
, const struct hash_table
* ht_symtab
,
646 const struct elf_thunk_area
* thunks
)
649 struct hash_table_iter hti
;
650 struct symtab_elt
* ste
;
652 struct symt_ht
* symt
;
654 hash_table_iter_init(ht_symtab
, &hti
, NULL
);
655 while ((ste
= hash_table_iter_up(&hti
)))
657 if (ste
->used
) continue;
659 addr
= module
->elf_info
->elf_addr
+ ste
->symp
->st_value
;
661 j
= elf_is_in_thunk_area(ste
->symp
->st_value
, thunks
);
662 if (j
>= 0) /* thunk found */
664 symt_new_thunk(module
, ste
->compiland
, ste
->ht_elt
.name
, thunks
[j
].ordinal
,
665 addr
, ste
->symp
->st_size
);
671 symt
= symt_find_nearest(module
, addr
);
672 if (symt
&& !symt_get_info(module
, &symt
->symt
, TI_GET_ADDRESS
, &ref_addr
))
674 if (!symt
|| addr
!= ref_addr
)
676 /* creating public symbols for all the ELF symbols which haven't been
677 * used yet (ie we have no debug information on them)
678 * That's the case, for example, of the .spec.c files
680 switch (ELF32_ST_TYPE(ste
->symp
->st_info
))
683 symt_new_function(module
, ste
->compiland
, ste
->ht_elt
.name
,
684 addr
, ste
->symp
->st_size
, NULL
);
687 symt_new_global_variable(module
, ste
->compiland
, ste
->ht_elt
.name
,
688 ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
,
689 addr
, ste
->symp
->st_size
, NULL
);
692 FIXME("Shouldn't happen\n");
695 /* FIXME: this is a hack !!!
696 * we are adding new symbols, but as we're parsing a symbol table
697 * (hopefully without duplicate symbols) we delay rebuilding the sorted
698 * module table until we're done with the symbol table
699 * Otherwise, as we intertwine symbols's add and lookup, performance
702 module
->sortlist_valid
= TRUE
;
704 else if (strcmp(ste
->ht_elt
.name
, symt
->hash_elt
.name
))
706 ULONG64 xaddr
= 0, xsize
= 0;
709 symt_get_info(module
, &symt
->symt
, TI_GET_ADDRESS
, &xaddr
);
710 symt_get_info(module
, &symt
->symt
, TI_GET_LENGTH
, &xsize
);
711 symt_get_info(module
, &symt
->symt
, TI_GET_DATAKIND
, &kind
);
713 /* If none of symbols has a correct size, we consider they are both markers
714 * Hence, we can silence this warning
715 * Also, we check that we don't have two symbols, one local, the other
716 * global which is legal
718 if ((xsize
|| ste
->symp
->st_size
) &&
719 (kind
== (ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
) ? DataIsFileStatic
: DataIsGlobal
))
720 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%s-%s>\n",
721 debugstr_w(module
->module
.ModuleName
),
722 ste
->ht_elt
.name
, addr
, (unsigned int)ste
->symp
->st_size
,
724 wine_dbgstr_longlong(xaddr
), wine_dbgstr_longlong(xsize
));
728 /* see comment above */
729 module
->sortlist_valid
= FALSE
;
733 /******************************************************************
734 * elf_new_public_symbols
736 * Creates a set of public symbols from an ELF symtab
738 static int elf_new_public_symbols(struct module
* module
, const struct hash_table
* symtab
)
740 struct hash_table_iter hti
;
741 struct symtab_elt
* ste
;
743 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
745 /* FIXME: we're missing the ELF entry point here */
747 hash_table_iter_init(symtab
, &hti
, NULL
);
748 while ((ste
= hash_table_iter_up(&hti
)))
750 symt_new_public(module
, ste
->compiland
, ste
->ht_elt
.name
,
751 module
->elf_info
->elf_addr
+ ste
->symp
->st_value
,
757 static BOOL
elf_check_debug_link(const WCHAR
* file
, struct elf_file_map
* fmap
, DWORD crc
)
760 if (!elf_map_file(file
, fmap
)) return FALSE
;
761 if (!(ret
= crc
== calc_crc32(fmap
->fd
)))
763 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
764 debugstr_w(file
), calc_crc32(fmap
->fd
), crc
);
765 elf_unmap_file(fmap
);
770 /******************************************************************
771 * elf_locate_debug_link
773 * Locate a filename from a .gnu_debuglink section, using the same
775 * "If the full name of the directory containing the executable is
776 * execdir, and the executable has a debug link that specifies the
777 * name debugfile, then GDB will automatically search for the
778 * debugging information file in three places:
779 * - the directory containing the executable file (that is, it
780 * will look for a file named `execdir/debugfile',
781 * - a subdirectory of that directory named `.debug' (that is, the
782 * file `execdir/.debug/debugfile', and
783 * - a subdirectory of the global debug file directory that includes
784 * the executable's full path, and the name from the link (that is,
785 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
786 * is the global debug file directory, and execdir has been turned
787 * into a relative path)." (from GDB manual)
789 static BOOL
elf_locate_debug_link(struct elf_file_map
* fmap
, const char* filename
,
790 const WCHAR
* loaded_file
, DWORD crc
)
792 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
793 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
794 const size_t globalDebugDirLen
= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
798 struct elf_file_map
* fmap_link
= NULL
;
800 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
801 if (!fmap_link
) return FALSE
;
803 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
804 p
= HeapAlloc(GetProcessHeap(), 0,
805 (globalDebugDirLen
+ strlenW(loaded_file
) + 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
808 /* we prebuild the string with "execdir" */
809 strcpyW(p
, loaded_file
);
810 slash
= strrchrW(p
, '/');
811 if (slash
== NULL
) slash
= p
; else slash
++;
813 /* testing execdir/filename */
814 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
815 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
817 /* testing execdir/.debug/filename */
818 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
819 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ sizeof(dotDebugW
) / sizeof(WCHAR
), filename_len
);
820 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
822 /* testing globaldebugdir/execdir/filename */
823 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
824 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
825 slash
+= globalDebugDirLen
;
826 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
827 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
829 /* finally testing filename */
830 if (elf_check_debug_link(slash
, fmap_link
, crc
)) goto found
;
833 WARN("Couldn't locate or map %s\n", filename
);
834 HeapFree(GetProcessHeap(), 0, p
);
835 HeapFree(GetProcessHeap(), 0, fmap_link
);
839 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
840 HeapFree(GetProcessHeap(), 0, p
);
841 fmap
->alternate
= fmap_link
;
845 /******************************************************************
846 * elf_debuglink_parse
848 * Parses a .gnu_debuglink section and loads the debug info from
849 * the external file specified there.
851 static BOOL
elf_debuglink_parse(struct elf_file_map
* fmap
, const struct module
* module
,
852 const BYTE
* debuglink
)
854 /* The content of a debug link section is:
855 * 1/ a NULL terminated string, containing the file name for the
857 * 2/ padding on 4 byte boundary
858 * 3/ CRC of the linked ELF file
860 const char* dbg_link
= (const char*)debuglink
;
863 crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
864 return elf_locate_debug_link(fmap
, dbg_link
, module
->module
.LoadedImageName
, crc
);
867 /******************************************************************
868 * elf_load_debug_info_from_map
870 * Loads the symbolic information from ELF module which mapping is described
872 * the module has been loaded at 'load_offset' address, so symbols' address
873 * relocation is performed.
874 * CRC is checked if fmap->with_crc is TRUE
876 * 0 if the file doesn't contain symbolic info (or this info cannot be
880 static BOOL
elf_load_debug_info_from_map(struct module
* module
,
881 struct elf_file_map
* fmap
,
883 struct hash_table
* ht_symtab
)
885 BOOL ret
= FALSE
, lret
;
886 struct elf_thunk_area thunks
[] =
888 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
889 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
890 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
891 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
892 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
893 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
897 module
->module
.SymType
= SymExport
;
899 /* create a hash table for the symtab */
900 elf_hash_symtab(module
, pool
, ht_symtab
, fmap
, thunks
);
902 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
904 struct elf_section_map stab_sect
, stabstr_sect
;
905 struct elf_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
906 debug_line_sect
, debug_loclist_sect
;
907 struct elf_section_map debuglink_sect
;
909 /* if present, add the .gnu_debuglink file as an alternate to current one */
910 if (elf_find_section(fmap
, ".gnu_debuglink", SHT_NULL
, &debuglink_sect
))
912 const BYTE
* dbg_link
;
914 dbg_link
= (const BYTE
*)elf_map_section(&debuglink_sect
);
915 if (dbg_link
!= ELF_NO_MAP
)
917 lret
= elf_debuglink_parse(fmap
, module
, dbg_link
);
919 WARN("Couldn't load linked debug file for %s\n",
920 debugstr_w(module
->module
.ModuleName
));
923 elf_unmap_section(&debuglink_sect
);
925 if (elf_find_section(fmap
, ".stab", SHT_NULL
, &stab_sect
) &&
926 elf_find_section(fmap
, ".stabstr", SHT_NULL
, &stabstr_sect
))
931 stab
= elf_map_section(&stab_sect
);
932 stabstr
= elf_map_section(&stabstr_sect
);
933 if (stab
!= ELF_NO_MAP
&& stabstr
!= ELF_NO_MAP
)
935 /* OK, now just parse all of the stabs. */
936 lret
= stabs_parse(module
, module
->elf_info
->elf_addr
,
937 stab
, elf_get_map_size(&stab_sect
),
938 stabstr
, elf_get_map_size(&stabstr_sect
),
941 /* and fill in the missing information for stabs */
942 elf_finish_stabs_info(module
, ht_symtab
);
944 WARN("Couldn't correctly read stabs\n");
948 elf_unmap_section(&stab_sect
);
949 elf_unmap_section(&stabstr_sect
);
951 if (elf_find_section(fmap
, ".debug_info", SHT_NULL
, &debug_sect
))
953 /* Dwarf 2 debug information */
954 const BYTE
* dw2_debug
;
955 const BYTE
* dw2_debug_abbrev
;
956 const BYTE
* dw2_debug_str
;
957 const BYTE
* dw2_debug_line
;
958 const BYTE
* dw2_debug_loclist
;
960 /* debug info might have a different base address than .so file
961 * when elf file is prelinked after splitting off debug info
962 * adjust symbol base addresses accordingly
964 unsigned long load_offset
= module
->elf_info
->elf_addr
+
965 fmap
->elf_start
- debug_sect
.fmap
->elf_start
;
967 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->module
.ModuleName
));
969 elf_find_section(fmap
, ".debug_str", SHT_NULL
, &debug_str_sect
);
970 elf_find_section(fmap
, ".debug_abbrev", SHT_NULL
, &debug_abbrev_sect
);
971 elf_find_section(fmap
, ".debug_line", SHT_NULL
, &debug_line_sect
);
972 elf_find_section(fmap
, ".debug_loc", SHT_NULL
, &debug_loclist_sect
);
974 dw2_debug
= (const BYTE
*)elf_map_section(&debug_sect
);
975 dw2_debug_abbrev
= (const BYTE
*)elf_map_section(&debug_abbrev_sect
);
976 dw2_debug_str
= (const BYTE
*)elf_map_section(&debug_str_sect
);
977 dw2_debug_line
= (const BYTE
*)elf_map_section(&debug_line_sect
);
978 dw2_debug_loclist
= (const BYTE
*)elf_map_section(&debug_loclist_sect
);
979 if (dw2_debug
!= ELF_NO_MAP
&& dw2_debug_abbrev
!= ELF_NO_MAP
&& dw2_debug_str
!= ELF_NO_MAP
)
981 /* OK, now just parse dwarf2 debug infos. */
982 lret
= dwarf2_parse(module
, load_offset
, thunks
,
983 dw2_debug
, elf_get_map_size(&debug_sect
),
984 dw2_debug_abbrev
, elf_get_map_size(&debug_abbrev_sect
),
985 dw2_debug_str
, elf_get_map_size(&debug_str_sect
),
986 dw2_debug_line
, elf_get_map_size(&debug_line_sect
),
987 dw2_debug_loclist
, elf_get_map_size(&debug_loclist_sect
));
990 WARN("Couldn't correctly read dwarf2\n");
993 elf_unmap_section(&debug_sect
);
994 elf_unmap_section(&debug_abbrev_sect
);
995 elf_unmap_section(&debug_str_sect
);
996 elf_unmap_section(&debug_line_sect
);
997 elf_unmap_section(&debug_loclist_sect
);
1000 if (strstrW(module
->module
.ModuleName
, S_ElfW
) ||
1001 !strcmpW(module
->module
.ModuleName
, S_WineLoaderW
))
1003 /* add the thunks for native libraries */
1004 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1005 elf_new_wine_thunks(module
, ht_symtab
, thunks
);
1007 /* add all the public symbols from symtab */
1008 if (elf_new_public_symbols(module
, ht_symtab
) && !ret
) ret
= TRUE
;
1013 /******************************************************************
1014 * elf_load_debug_info
1016 * Loads ELF debugging information from the module image file.
1018 BOOL
elf_load_debug_info(struct module
* module
, struct elf_file_map
* fmap
)
1022 struct hash_table ht_symtab
;
1023 struct elf_file_map my_fmap
;
1025 if (module
->type
!= DMT_ELF
|| !module
->elf_info
)
1027 ERR("Bad elf module '%s'\n", debugstr_w(module
->module
.LoadedImageName
));
1031 pool_init(&pool
, 65536);
1032 hash_table_init(&pool
, &ht_symtab
, 256);
1037 ret
= elf_map_file(module
->module
.LoadedImageName
, fmap
);
1040 ret
= elf_load_debug_info_from_map(module
, fmap
, &pool
, &ht_symtab
);
1042 pool_destroy(&pool
);
1043 if (fmap
== &my_fmap
) elf_unmap_file(fmap
);
1047 /******************************************************************
1048 * elf_fetch_file_info
1050 * Gathers some more information for an ELF module from a given file
1052 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD
* base
,
1053 DWORD
* size
, DWORD
* checksum
)
1055 struct elf_file_map fmap
;
1057 if (!elf_map_file(name
, &fmap
)) return FALSE
;
1058 if (base
) *base
= fmap
.elf_start
;
1059 *size
= fmap
.elf_size
;
1060 *checksum
= calc_crc32(fmap
.fd
);
1061 elf_unmap_file(&fmap
);
1065 /******************************************************************
1068 * Loads the information for ELF module stored in 'filename'
1069 * the module has been loaded at 'load_offset' address
1071 * -1 if the file cannot be found/opened
1072 * 0 if the file doesn't contain symbolic info (or this info cannot be
1076 static BOOL
elf_load_file(struct process
* pcs
, const WCHAR
* filename
,
1077 unsigned long load_offset
, struct elf_info
* elf_info
)
1080 struct elf_file_map fmap
;
1082 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename
), load_offset
);
1084 if (!elf_map_file(filename
, &fmap
)) return ret
;
1086 /* Next, we need to find a few of the internal ELF headers within
1087 * this thing. We need the main executable header, and the section
1090 if (!fmap
.elf_start
&& !load_offset
)
1091 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1092 debugstr_w(filename
));
1093 if (fmap
.elf_start
&& load_offset
)
1095 WARN("Non-relocatable ELF %s, but load address of 0x%08lx supplied. "
1096 "Assuming load address is corrupt\n", debugstr_w(filename
), load_offset
);
1100 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
1102 struct elf_section_map esm
;
1104 if (elf_find_section(&fmap
, ".dynamic", SHT_DYNAMIC
, &esm
))
1107 char* ptr
= (char*)fmap
.sect
[esm
.sidx
].shdr
.sh_addr
;
1112 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1115 if (dyn
.d_tag
== DT_DEBUG
)
1117 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1121 } while (dyn
.d_tag
!= DT_NULL
);
1122 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1124 elf_end_find(&fmap
);
1127 if (elf_info
->flags
& ELF_INFO_MODULE
)
1129 struct elf_module_info
*elf_module_info
=
1130 HeapAlloc(GetProcessHeap(), 0, sizeof(struct elf_module_info
));
1131 if (!elf_module_info
) goto leave
;
1132 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
, FALSE
,
1133 (load_offset
) ? load_offset
: fmap
.elf_start
,
1134 fmap
.elf_size
, 0, calc_crc32(fmap
.fd
));
1135 if (!elf_info
->module
)
1137 HeapFree(GetProcessHeap(), 0, elf_module_info
);
1140 elf_info
->module
->elf_info
= elf_module_info
;
1141 elf_info
->module
->elf_info
->elf_addr
= load_offset
;
1143 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
1145 elf_info
->module
->module
.SymType
= SymDeferred
;
1148 else ret
= elf_load_debug_info(elf_info
->module
, &fmap
);
1150 elf_info
->module
->elf_info
->elf_mark
= 1;
1151 elf_info
->module
->elf_info
->elf_loader
= 0;
1154 if (elf_info
->flags
& ELF_INFO_NAME
)
1157 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1) * sizeof(WCHAR
));
1160 strcpyW(ptr
, filename
);
1161 elf_info
->module_name
= ptr
;
1166 elf_unmap_file(&fmap
);
1171 /******************************************************************
1172 * elf_load_file_from_path
1173 * tries to load an ELF file from a set of paths (separated by ':')
1175 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
1176 const WCHAR
* filename
,
1177 unsigned long load_offset
,
1179 struct elf_info
* elf_info
)
1183 WCHAR
* pathW
= NULL
;
1186 if (!path
) return FALSE
;
1188 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1189 pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1190 if (!pathW
) return FALSE
;
1191 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, pathW
, len
);
1193 for (s
= pathW
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
1195 t
= strchrW(s
, ':');
1197 fn
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1 + lstrlenW(s
) + 1) * sizeof(WCHAR
));
1200 strcatW(fn
, S_SlashW
);
1201 strcatW(fn
, filename
);
1202 ret
= elf_load_file(hProcess
, fn
, load_offset
, elf_info
);
1203 HeapFree(GetProcessHeap(), 0, fn
);
1205 s
= (t
) ? (t
+1) : NULL
;
1208 HeapFree(GetProcessHeap(), 0, pathW
);
1212 /******************************************************************
1213 * elf_load_file_from_dll_path
1215 * Tries to load an ELF file from the dll path
1217 static BOOL
elf_load_file_from_dll_path(HANDLE hProcess
,
1218 const WCHAR
* filename
,
1219 unsigned long load_offset
,
1220 struct elf_info
* elf_info
)
1223 unsigned int index
= 0;
1226 while (!ret
&& (path
= wine_dll_enum_load_path( index
++ )))
1231 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1233 name
= HeapAlloc( GetProcessHeap(), 0,
1234 (len
+ lstrlenW(filename
) + 2) * sizeof(WCHAR
) );
1237 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, name
, len
);
1238 strcatW( name
, S_SlashW
);
1239 strcatW( name
, filename
);
1240 ret
= elf_load_file(hProcess
, name
, load_offset
, elf_info
);
1241 HeapFree( GetProcessHeap(), 0, name
);
1246 /******************************************************************
1247 * elf_search_and_load_file
1249 * lookup a file in standard ELF locations, and if found, load it
1251 static BOOL
elf_search_and_load_file(struct process
* pcs
, const WCHAR
* filename
,
1252 unsigned long load_offset
,
1253 struct elf_info
* elf_info
)
1256 struct module
* module
;
1257 static WCHAR S_libstdcPPW
[] = {'l','i','b','s','t','d','c','+','+','\0'};
1259 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
1260 if ((module
= module_is_already_loaded(pcs
, filename
)))
1262 elf_info
->module
= module
;
1263 module
->elf_info
->elf_mark
= 1;
1264 return module
->module
.SymType
;
1267 if (strstrW(filename
, S_libstdcPPW
)) return FALSE
; /* We know we can't do it */
1268 ret
= elf_load_file(pcs
, filename
, load_offset
, elf_info
);
1269 /* if relative pathname, try some absolute base dirs */
1270 if (!ret
&& !strchrW(filename
, '/'))
1272 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
,
1273 getenv("PATH"), elf_info
) ||
1274 elf_load_file_from_path(pcs
, filename
, load_offset
,
1275 getenv("LD_LIBRARY_PATH"), elf_info
);
1276 if (!ret
) ret
= elf_load_file_from_dll_path(pcs
, filename
, load_offset
, elf_info
);
1282 /******************************************************************
1283 * elf_enum_modules_internal
1285 * Enumerate ELF modules from a running process
1287 static BOOL
elf_enum_modules_internal(const struct process
* pcs
,
1288 const WCHAR
* main_name
,
1289 enum_modules_cb cb
, void* user
)
1291 struct r_debug dbg_hdr
;
1295 WCHAR bufstrW
[MAX_PATH
];
1297 if (!pcs
->dbg_hdr_addr
||
1298 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1299 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1302 /* Now walk the linked list. In all known ELF implementations,
1303 * the dynamic loader maintains this linked list for us. In some
1304 * cases the first entry doesn't appear with a name, in other cases it
1307 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
1309 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1312 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
1313 lm
.l_name
!= NULL
&&
1314 ReadProcessMemory(pcs
->handle
, lm
.l_name
, bufstr
, sizeof(bufstr
), NULL
))
1316 bufstr
[sizeof(bufstr
) - 1] = '\0';
1317 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, sizeof(bufstrW
) / sizeof(WCHAR
));
1318 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1319 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, user
)) break;
1327 struct process
* pcs
;
1328 struct elf_info elf_info
;
1331 static BOOL
elf_enum_sync_cb(const WCHAR
* name
, unsigned long addr
, void* user
)
1333 struct elf_sync
* es
= user
;
1335 elf_search_and_load_file(es
->pcs
, name
, addr
, &es
->elf_info
);
1339 /******************************************************************
1340 * elf_synchronize_module_list
1342 * this functions rescans the debuggee module's list and synchronizes it with
1343 * the one from 'pcs', ie:
1344 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1345 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1347 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1349 struct module
* module
;
1352 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1354 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1355 module
->elf_info
->elf_mark
= 0;
1359 es
.elf_info
.flags
= ELF_INFO_MODULE
;
1360 if (!elf_enum_modules_internal(pcs
, NULL
, elf_enum_sync_cb
, &es
))
1363 module
= pcs
->lmodules
;
1366 if (module
->type
== DMT_ELF
&& !module
->is_virtual
&&
1367 !module
->elf_info
->elf_mark
&& !module
->elf_info
->elf_loader
)
1369 module_remove(pcs
, module
);
1370 /* restart all over */
1371 module
= pcs
->lmodules
;
1373 else module
= module
->next
;
1378 /******************************************************************
1381 * Lookup in a running ELF process the loader, and sets its ELF link
1382 * address (for accessing the list of loaded .so libs) in pcs.
1383 * If flags is ELF_INFO_MODULE, the module for the loader is also
1384 * added as a module into pcs.
1386 static BOOL
elf_search_loader(struct process
* pcs
, struct elf_info
* elf_info
)
1391 /* All binaries are loaded with WINELOADER (if run from tree) or by the
1394 if ((ptr
= getenv("WINELOADER")))
1396 WCHAR tmp
[MAX_PATH
];
1397 MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, tmp
, sizeof(tmp
) / sizeof(WCHAR
));
1398 ret
= elf_search_and_load_file(pcs
, tmp
, 0, elf_info
);
1402 ret
= elf_search_and_load_file(pcs
, S_WineW
, 0, elf_info
);
1407 /******************************************************************
1408 * elf_read_wine_loader_dbg_info
1410 * Try to find a decent wine executable which could have loaded the debuggee
1412 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1414 struct elf_info elf_info
;
1416 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
1417 if (!elf_search_loader(pcs
, &elf_info
)) return FALSE
;
1418 elf_info
.module
->elf_info
->elf_loader
= 1;
1419 module_set_module(elf_info
.module
, S_WineLoaderW
);
1420 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
1423 /******************************************************************
1426 * Enumerates the ELF loaded modules from a running target (hProc)
1427 * This function doesn't require that someone has called SymInitialize
1428 * on this very process.
1430 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1433 struct elf_info elf_info
;
1436 memset(&pcs
, 0, sizeof(pcs
));
1438 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_NAME
;
1439 if (!elf_search_loader(&pcs
, &elf_info
)) return FALSE
;
1440 pcs
.dbg_hdr_addr
= elf_info
.dbg_hdr_addr
;
1441 ret
= elf_enum_modules_internal(&pcs
, elf_info
.module_name
, cb
, user
);
1442 HeapFree(GetProcessHeap(), 0, (char*)elf_info
.module_name
);
1448 struct process
* pcs
;
1449 struct elf_info elf_info
;
1454 /******************************************************************
1457 * Callback for elf_load_module, used to walk the list of loaded
1460 static BOOL
elf_load_cb(const WCHAR
* name
, unsigned long addr
, void* user
)
1462 struct elf_load
* el
= user
;
1465 /* memcmp is needed for matches when bufstr contains also version information
1466 * el->name: libc.so, name: libc.so.6.0
1468 p
= strrchrW(name
, '/');
1470 if (!memcmp(p
, el
->name
, lstrlenW(el
->name
) * sizeof(WCHAR
)))
1472 el
->ret
= elf_search_and_load_file(el
->pcs
, name
, addr
, &el
->elf_info
);
1478 /******************************************************************
1481 * loads an ELF module and stores it in process' module list
1482 * Also, find module real name and load address from
1483 * the real loaded modules list in pcs address space
1485 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1489 TRACE("(%p %s %08lx)\n", pcs
, debugstr_w(name
), addr
);
1491 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1494 if (pcs
->dbg_hdr_addr
) /* we're debugging a life target */
1497 /* do only the lookup from the filename, not the path (as we lookup module
1498 * name in the process' loaded module list)
1500 el
.name
= strrchrW(name
, '/');
1501 if (!el
.name
++) el
.name
= name
;
1504 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1510 el
.ret
= elf_search_and_load_file(pcs
, el
.name
, addr
, &el
.elf_info
);
1512 if (!el
.ret
) return NULL
;
1513 assert(el
.elf_info
.module
);
1514 return el
.elf_info
.module
;
1517 #else /* !__ELF__ */
1519 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1524 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD
* base
,
1525 DWORD
* size
, DWORD
* checksum
)
1530 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1535 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1540 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1545 BOOL
elf_load_debug_info(struct module
* module
, struct elf_file_map
* fmap
)
1550 int elf_is_in_thunk_area(unsigned long addr
,
1551 const struct elf_thunk_area
* thunks
)
1555 #endif /* __ELF__ */