2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2007 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "dbghelp_private.h"
24 #if defined(__svr4__) || defined(__sun)
26 /* large files are not supported by libelf */
27 #undef _FILE_OFFSET_BITS
28 #define _FILE_OFFSET_BITS 32
34 #include <wine/library.h>
38 #define ELF_INFO_DEBUG_HEADER 0x0001
39 #define ELF_INFO_MODULE 0x0002
40 #define ELF_INFO_NAME 0x0004
42 #ifndef NT_GNU_BUILD_ID
43 #define NT_GNU_BUILD_ID 3
46 #ifndef HAVE_STRUCT_R_DEBUG
50 struct link_map
*r_map
;
60 #endif /* HAVE_STRUCT_R_DEBUG */
62 #ifndef HAVE_STRUCT_LINK_MAP
68 struct link_map
*l_next
, *l_prev
;
70 #endif /* HAVE_STRUCT_LINK_MAP */
72 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
76 unsigned flags
; /* IN one (or several) of the ELF_INFO constants */
77 DWORD_PTR dbg_hdr_addr
; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
78 struct module
* module
; /* OUT loaded module (if ELF_INFO_MODULE is set) */
79 const WCHAR
* module_name
; /* OUT found module name (if ELF_INFO_NAME is set) */
84 struct hash_table_elt ht_elt
;
86 struct symt_compiland
* compiland
;
93 THUNK_ORDINAL ordinal
;
94 unsigned long rva_start
;
95 unsigned long rva_end
;
98 struct elf_module_info
100 unsigned long elf_addr
;
101 unsigned short elf_mark
: 1,
103 struct image_file_map file_map
;
106 /******************************************************************
109 * Maps a single section into memory from an ELF file
111 const char* elf_map_section(struct image_section_map
* ism
)
113 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
114 size_t ofst
, size
, pgsz
= sysconf( _SC_PAGESIZE
);
116 assert(ism
->fmap
->modtype
== DMT_ELF
);
117 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
||
118 fmap
->sect
[ism
->sidx
].shdr
.sh_type
== SHT_NOBITS
)
121 if (fmap
->target_copy
)
123 return fmap
->target_copy
+ fmap
->sect
[ism
->sidx
].shdr
.sh_offset
;
125 /* align required information on page size (we assume pagesize is a power of 2) */
126 ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
127 size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
128 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
129 fmap
->sect
[ism
->sidx
].mapped
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
,
131 if (fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
) return IMAGE_NO_MAP
;
132 return fmap
->sect
[ism
->sidx
].mapped
+ (fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& (pgsz
- 1));
135 /******************************************************************
138 * Finds a section by name (and type) into memory from an ELF file
139 * or its alternate if any
141 BOOL
elf_find_section(struct image_file_map
* _fmap
, const char* name
,
142 unsigned sht
, struct image_section_map
* ism
)
144 struct elf_file_map
* fmap
;
149 fmap
= &_fmap
->u
.elf
;
150 if (fmap
->shstrtab
== IMAGE_NO_MAP
)
152 struct image_section_map hdr_ism
= {_fmap
, fmap
->elfhdr
.e_shstrndx
};
153 if ((fmap
->shstrtab
= elf_map_section(&hdr_ism
)) == IMAGE_NO_MAP
) break;
155 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
157 if (strcmp(fmap
->shstrtab
+ fmap
->sect
[i
].shdr
.sh_name
, name
) == 0 &&
158 (sht
== SHT_NULL
|| sht
== fmap
->sect
[i
].shdr
.sh_type
))
165 _fmap
= fmap
->alternate
;
172 /******************************************************************
175 * Unmaps a single section from memory
177 void elf_unmap_section(struct image_section_map
* ism
)
179 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
181 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->elfhdr
.e_shnum
&& !fmap
->target_copy
&&
182 fmap
->sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
184 size_t pgsz
= sysconf( _SC_PAGESIZE
);
185 size_t ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
186 size_t size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
187 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
188 if (munmap((char*)fmap
->sect
[ism
->sidx
].mapped
, size
) < 0)
189 WARN("Couldn't unmap the section\n");
190 fmap
->sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
194 static void elf_end_find(struct image_file_map
* fmap
)
196 struct image_section_map ism
;
201 ism
.sidx
= fmap
->u
.elf
.elfhdr
.e_shstrndx
;
202 elf_unmap_section(&ism
);
203 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
204 fmap
= fmap
->u
.elf
.alternate
;
208 /******************************************************************
211 * Get the RVA of an ELF section
213 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
215 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
217 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_addr
- ism
->fmap
->u
.elf
.elf_start
;
220 /******************************************************************
223 * Get the size of an ELF section
225 unsigned elf_get_map_size(const struct image_section_map
* ism
)
227 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
229 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_size
;
232 static inline void elf_reset_file_map(struct image_file_map
* fmap
)
235 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
236 fmap
->u
.elf
.alternate
= NULL
;
237 fmap
->u
.elf
.target_copy
= NULL
;
240 struct elf_map_file_data
242 enum {from_file
, from_process
} kind
;
247 const WCHAR
* filename
;
257 static BOOL
elf_map_file_read(struct image_file_map
* fmap
, struct elf_map_file_data
* emfd
,
258 void* buf
, size_t len
, off_t off
)
265 return pread(fmap
->u
.elf
.fd
, buf
, len
, off
) == len
;
267 return ReadProcessMemory(emfd
->u
.process
.handle
,
268 (void*)((unsigned long)emfd
->u
.process
.load_addr
+ (unsigned long)off
),
269 buf
, len
, &dw
) && dw
== len
;
276 /******************************************************************
279 * Maps an ELF file into memory (and checks it's a real ELF file)
281 static BOOL
elf_map_file(struct elf_map_file_data
* emfd
, struct image_file_map
* fmap
)
283 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
287 size_t tmp
, page_mask
= sysconf( _SC_PAGESIZE
) - 1;
295 len
= WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, NULL
, 0, NULL
, NULL
);
296 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
297 WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, filename
, len
, NULL
, NULL
);
306 elf_reset_file_map(fmap
);
308 fmap
->modtype
= DMT_ELF
;
310 fmap
->u
.elf
.target_copy
= NULL
;
315 /* check that the file exists, and that the module hasn't been loaded yet */
316 if (stat(filename
, &statbuf
) == -1 || S_ISDIR(statbuf
.st_mode
)) goto done
;
318 /* Now open the file, so that we can mmap() it. */
319 if ((fmap
->u
.elf
.fd
= open(filename
, O_RDONLY
)) == -1) goto done
;
324 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.elfhdr
, sizeof(fmap
->u
.elf
.elfhdr
), 0))
327 /* and check for an ELF header */
328 if (memcmp(fmap
->u
.elf
.elfhdr
.e_ident
,
329 elf_signature
, sizeof(elf_signature
))) goto done
;
330 /* and check 32 vs 64 size according to current machine */
332 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) goto done
;
334 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) goto done
;
336 fmap
->addr_size
= fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
337 fmap
->u
.elf
.sect
= HeapAlloc(GetProcessHeap(), 0,
338 fmap
->u
.elf
.elfhdr
.e_shnum
* sizeof(fmap
->u
.elf
.sect
[0]));
339 if (!fmap
->u
.elf
.sect
) goto done
;
341 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_shnum
; i
++)
343 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.sect
[i
].shdr
, sizeof(fmap
->u
.elf
.sect
[i
].shdr
),
344 fmap
->u
.elf
.elfhdr
.e_shoff
+ i
* sizeof(fmap
->u
.elf
.sect
[i
].shdr
)))
346 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
347 fmap
->u
.elf
.sect
= NULL
;
350 fmap
->u
.elf
.sect
[i
].mapped
= IMAGE_NO_MAP
;
353 /* grab size of module once loaded in memory */
354 fmap
->u
.elf
.elf_size
= 0;
355 fmap
->u
.elf
.elf_start
= ~0L;
356 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_phnum
; i
++)
358 if (elf_map_file_read(fmap
, emfd
, &phdr
, sizeof(phdr
),
359 fmap
->u
.elf
.elfhdr
.e_phoff
+ i
* sizeof(phdr
)) &&
360 phdr
.p_type
== PT_LOAD
)
362 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
363 if (fmap
->u
.elf
.elf_size
< tmp
) fmap
->u
.elf
.elf_size
= tmp
;
364 if (phdr
.p_vaddr
< fmap
->u
.elf
.elf_start
) fmap
->u
.elf
.elf_start
= phdr
.p_vaddr
;
367 /* if non relocatable ELF, then remove fixed address from computation
368 * otherwise, all addresses are zero based and start has no effect
370 fmap
->u
.elf
.elf_size
-= fmap
->u
.elf
.elf_start
;
374 case from_file
: break;
376 if (!(fmap
->u
.elf
.target_copy
= HeapAlloc(GetProcessHeap(), 0, fmap
->u
.elf
.elf_size
)))
378 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
381 if (!ReadProcessMemory(emfd
->u
.process
.handle
, emfd
->u
.process
.load_addr
, fmap
->u
.elf
.target_copy
,
382 fmap
->u
.elf
.elf_size
, NULL
))
384 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
385 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
392 HeapFree(GetProcessHeap(), 0, filename
);
396 /******************************************************************
399 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
401 static void elf_unmap_file(struct image_file_map
* fmap
)
405 if (fmap
->u
.elf
.fd
!= -1)
407 struct image_section_map ism
;
409 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.elf
.elfhdr
.e_shnum
; ism
.sidx
++)
411 elf_unmap_section(&ism
);
413 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
414 close(fmap
->u
.elf
.fd
);
416 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
417 fmap
= fmap
->u
.elf
.alternate
;
421 static void elf_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
423 elf_unmap_file(&modfmt
->u
.elf_info
->file_map
);
424 HeapFree(GetProcessHeap(), 0, modfmt
);
427 /******************************************************************
428 * elf_is_in_thunk_area
430 * Check whether an address lies within one of the thunk area we
433 int elf_is_in_thunk_area(unsigned long addr
,
434 const struct elf_thunk_area
* thunks
)
438 if (thunks
) for (i
= 0; thunks
[i
].symname
; i
++)
440 if (addr
>= thunks
[i
].rva_start
&& addr
< thunks
[i
].rva_end
)
446 /******************************************************************
449 * creating an internal hash table to ease use ELF symtab information lookup
451 static void elf_hash_symtab(struct module
* module
, struct pool
* pool
,
452 struct hash_table
* ht_symtab
, struct image_file_map
* fmap
,
453 struct elf_thunk_area
* thunks
)
458 struct symt_compiland
* compiland
= NULL
;
461 struct symtab_elt
* ste
;
462 struct image_section_map ism
, ism_str
;
464 if (!elf_find_section(fmap
, ".symtab", SHT_SYMTAB
, &ism
) &&
465 !elf_find_section(fmap
, ".dynsym", SHT_DYNSYM
, &ism
)) return;
466 if ((symp
= (const Elf_Sym
*)image_map_section(&ism
)) == IMAGE_NO_MAP
) return;
467 ism_str
.fmap
= ism
.fmap
;
468 ism_str
.sidx
= fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_link
;
469 if ((strp
= image_map_section(&ism_str
)) == IMAGE_NO_MAP
)
471 image_unmap_section(&ism
);
475 nsym
= image_get_map_size(&ism
) / sizeof(*symp
);
477 for (j
= 0; thunks
[j
].symname
; j
++)
478 thunks
[j
].rva_start
= thunks
[j
].rva_end
= 0;
480 for (i
= 0; i
< nsym
; i
++, symp
++)
482 /* Ignore certain types of entries which really aren't of that much
485 if ((ELF32_ST_TYPE(symp
->st_info
) != STT_NOTYPE
&&
486 ELF32_ST_TYPE(symp
->st_info
) != STT_FILE
&&
487 ELF32_ST_TYPE(symp
->st_info
) != STT_OBJECT
&&
488 ELF32_ST_TYPE(symp
->st_info
) != STT_FUNC
) ||
489 symp
->st_shndx
== SHN_UNDEF
)
494 symname
= strp
+ symp
->st_name
;
496 /* handle some specific symtab (that we'll throw away when done) */
497 switch (ELF32_ST_TYPE(symp
->st_info
))
501 compiland
= symt_new_compiland(module
, symp
->st_value
,
502 source_new(module
, NULL
, symname
));
507 /* we are only interested in wine markers inserted by winebuild */
508 for (j
= 0; thunks
[j
].symname
; j
++)
510 if (!strcmp(symname
, thunks
[j
].symname
))
512 thunks
[j
].rva_start
= symp
->st_value
;
513 thunks
[j
].rva_end
= symp
->st_value
+ symp
->st_size
;
520 /* FIXME: we don't need to handle them (GCC internals)
521 * Moreover, they screw up our symbol lookup :-/
523 if (symname
[0] == '.' && symname
[1] == 'L' && isdigit(symname
[2]))
526 ste
= pool_alloc(pool
, sizeof(*ste
));
527 ste
->ht_elt
.name
= symname
;
528 /* GCC emits, in some cases, a .<digit>+ suffix.
529 * This is used for static variable inside functions, so
530 * that we can have several such variables with same name in
531 * the same compilation unit
532 * We simply ignore that suffix when present (we also get rid
533 * of it in stabs parsing)
535 ptr
= symname
+ strlen(symname
) - 1;
538 while (isdigit(*ptr
) && ptr
>= symname
) ptr
--;
539 if (ptr
> symname
&& *ptr
== '.')
541 char* n
= pool_alloc(pool
, ptr
- symname
+ 1);
542 memcpy(n
, symname
, ptr
- symname
+ 1);
543 n
[ptr
- symname
] = '\0';
544 ste
->ht_elt
.name
= n
;
548 ste
->compiland
= compiland
;
550 hash_table_add(ht_symtab
, &ste
->ht_elt
);
552 /* as we added in the ht_symtab pointers to the symbols themselves,
553 * we cannot unmap yet the sections, it will be done when we're over
558 /******************************************************************
561 * lookup a symbol by name in our internal hash table for the symtab
563 static const Elf_Sym
* elf_lookup_symtab(const struct module
* module
,
564 const struct hash_table
* ht_symtab
,
565 const char* name
, const struct symt
* compiland
)
567 struct symtab_elt
* weak_result
= NULL
; /* without compiland name */
568 struct symtab_elt
* result
= NULL
;
569 struct hash_table_iter hti
;
570 struct symtab_elt
* ste
;
571 const char* compiland_name
;
572 const char* compiland_basename
;
575 /* we need weak match up (at least) when symbols of same name,
576 * defined several times in different compilation units,
577 * are merged in a single one (hence a different filename for c.u.)
581 compiland_name
= source_get(module
,
582 ((const struct symt_compiland
*)compiland
)->source
);
583 compiland_basename
= strrchr(compiland_name
, '/');
584 if (!compiland_basename
++) compiland_basename
= compiland_name
;
586 else compiland_name
= compiland_basename
= NULL
;
588 hash_table_iter_init(ht_symtab
, &hti
, name
);
589 while ((ste
= hash_table_iter_up(&hti
)))
591 if (ste
->used
|| strcmp(ste
->ht_elt
.name
, name
)) continue;
594 if ((ste
->compiland
&& !compiland_name
) || (!ste
->compiland
&& compiland_name
))
596 if (ste
->compiland
&& compiland_name
)
598 const char* filename
= source_get(module
, ste
->compiland
->source
);
599 if (strcmp(filename
, compiland_name
))
601 base
= strrchr(filename
, '/');
602 if (!base
++) base
= filename
;
603 if (strcmp(base
, compiland_basename
)) continue;
608 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
609 name
, compiland_name
,
610 source_get(module
, result
->compiland
->source
), (unsigned int)result
->symp
->st_value
,
611 source_get(module
, ste
->compiland
->source
), (unsigned int)ste
->symp
->st_value
);
619 if (!result
&& !(result
= weak_result
))
621 FIXME("Couldn't find symbol %s!%s in symtab\n",
622 debugstr_w(module
->module
.ModuleName
), name
);
628 /******************************************************************
629 * elf_finish_stabs_info
631 * - get any relevant information (address & size) from the bits we got from the
632 * stabs debugging information
634 static void elf_finish_stabs_info(struct module
* module
, const struct hash_table
* symtab
)
636 struct hash_table_iter hti
;
640 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
642 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
643 while ((ptr
= hash_table_iter_up(&hti
)))
645 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
646 switch (sym
->symt
.tag
)
649 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
650 ((struct symt_function
*)sym
)->size
)
654 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
655 ((struct symt_function
*)sym
)->container
);
658 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
659 ((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
+ symp
->st_value
)
660 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
661 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
662 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
663 if (((struct symt_function
*)sym
)->size
&& ((struct symt_function
*)sym
)->size
!= symp
->st_size
)
664 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
665 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
666 ((struct symt_function
*)sym
)->size
, (unsigned int)symp
->st_size
);
668 ((struct symt_function
*)sym
)->address
= elf_info
->elf_addr
+ symp
->st_value
;
669 ((struct symt_function
*)sym
)->size
= symp
->st_size
;
671 FIXME("Couldn't find %s!%s\n",
672 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
675 switch (((struct symt_data
*)sym
)->kind
)
678 case DataIsFileStatic
:
679 if (((struct symt_data
*)sym
)->u
.var
.kind
!= loc_absolute
||
680 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
)
682 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
683 ((struct symt_data
*)sym
)->container
);
686 if (((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
&&
687 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
+ symp
->st_value
)
688 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
689 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
690 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
691 ((struct symt_data
*)sym
)->u
.var
.offset
= elf_info
->elf_addr
+ symp
->st_value
;
692 ((struct symt_data
*)sym
)->kind
= (ELF32_ST_BIND(symp
->st_info
) == STB_LOCAL
) ?
693 DataIsFileStatic
: DataIsGlobal
;
695 FIXME("Couldn't find %s!%s\n",
696 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
702 FIXME("Unsupported tag %u\n", sym
->symt
.tag
);
706 /* since we may have changed some addresses & sizes, mark the module to be resorted */
707 module
->sortlist_valid
= FALSE
;
710 /******************************************************************
711 * elf_load_wine_thunks
713 * creating the thunk objects for a wine native DLL
715 static int elf_new_wine_thunks(struct module
* module
, const struct hash_table
* ht_symtab
,
716 const struct elf_thunk_area
* thunks
)
719 struct hash_table_iter hti
;
720 struct symtab_elt
* ste
;
722 struct symt_ht
* symt
;
724 hash_table_iter_init(ht_symtab
, &hti
, NULL
);
725 while ((ste
= hash_table_iter_up(&hti
)))
727 if (ste
->used
) continue;
729 addr
= module
->reloc_delta
+ ste
->symp
->st_value
;
731 j
= elf_is_in_thunk_area(ste
->symp
->st_value
, thunks
);
732 if (j
>= 0) /* thunk found */
734 symt_new_thunk(module
, ste
->compiland
, ste
->ht_elt
.name
, thunks
[j
].ordinal
,
735 addr
, ste
->symp
->st_size
);
742 symt
= symt_find_nearest(module
, addr
);
743 if (symt
&& !symt_get_address(&symt
->symt
, &ref_addr
))
745 if (!symt
|| addr
!= ref_addr
)
747 /* creating public symbols for all the ELF symbols which haven't been
748 * used yet (ie we have no debug information on them)
749 * That's the case, for example, of the .spec.c files
751 switch (ELF32_ST_TYPE(ste
->symp
->st_info
))
754 symt_new_function(module
, ste
->compiland
, ste
->ht_elt
.name
,
755 addr
, ste
->symp
->st_size
, NULL
);
758 loc
.kind
= loc_absolute
;
761 symt_new_global_variable(module
, ste
->compiland
, ste
->ht_elt
.name
,
762 ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
,
763 loc
, ste
->symp
->st_size
, NULL
);
766 FIXME("Shouldn't happen\n");
769 /* FIXME: this is a hack !!!
770 * we are adding new symbols, but as we're parsing a symbol table
771 * (hopefully without duplicate symbols) we delay rebuilding the sorted
772 * module table until we're done with the symbol table
773 * Otherwise, as we intertwine symbols' add and lookup, performance
776 module
->sortlist_valid
= TRUE
;
780 /* see comment above */
781 module
->sortlist_valid
= FALSE
;
785 /******************************************************************
786 * elf_new_public_symbols
788 * Creates a set of public symbols from an ELF symtab
790 static int elf_new_public_symbols(struct module
* module
, const struct hash_table
* symtab
)
792 struct hash_table_iter hti
;
793 struct symtab_elt
* ste
;
795 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
797 /* FIXME: we're missing the ELF entry point here */
799 hash_table_iter_init(symtab
, &hti
, NULL
);
800 while ((ste
= hash_table_iter_up(&hti
)))
802 symt_new_public(module
, ste
->compiland
, ste
->ht_elt
.name
,
803 module
->reloc_delta
+ ste
->symp
->st_value
,
809 static BOOL
elf_check_debug_link(const WCHAR
* file
, struct image_file_map
* fmap
, DWORD crc
)
812 struct elf_map_file_data emfd
;
814 emfd
.kind
= from_file
;
815 emfd
.u
.file
.filename
= file
;
816 if (!elf_map_file(&emfd
, fmap
)) return FALSE
;
817 if (!(ret
= crc
== calc_crc32(fmap
->u
.elf
.fd
)))
819 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
820 debugstr_w(file
), calc_crc32(fmap
->u
.elf
.fd
), crc
);
821 elf_unmap_file(fmap
);
826 /******************************************************************
827 * elf_locate_debug_link
829 * Locate a filename from a .gnu_debuglink section, using the same
831 * "If the full name of the directory containing the executable is
832 * execdir, and the executable has a debug link that specifies the
833 * name debugfile, then GDB will automatically search for the
834 * debugging information file in three places:
835 * - the directory containing the executable file (that is, it
836 * will look for a file named `execdir/debugfile',
837 * - a subdirectory of that directory named `.debug' (that is, the
838 * file `execdir/.debug/debugfile', and
839 * - a subdirectory of the global debug file directory that includes
840 * the executable's full path, and the name from the link (that is,
841 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
842 * is the global debug file directory, and execdir has been turned
843 * into a relative path)." (from GDB manual)
845 static BOOL
elf_locate_debug_link(struct image_file_map
* fmap
, const char* filename
,
846 const WCHAR
* loaded_file
, DWORD crc
)
848 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
849 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
850 const size_t globalDebugDirLen
= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
854 struct image_file_map
* fmap_link
= NULL
;
856 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
857 if (!fmap_link
) return FALSE
;
859 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
860 p
= HeapAlloc(GetProcessHeap(), 0,
861 (globalDebugDirLen
+ strlenW(loaded_file
) + 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
864 /* we prebuild the string with "execdir" */
865 strcpyW(p
, loaded_file
);
866 slash
= strrchrW(p
, '/');
867 if (slash
== NULL
) slash
= p
; else slash
++;
869 /* testing execdir/filename */
870 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
871 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
873 /* testing execdir/.debug/filename */
874 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
875 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ sizeof(dotDebugW
) / sizeof(WCHAR
), filename_len
);
876 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
878 /* testing globaldebugdir/execdir/filename */
879 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
880 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
881 slash
+= globalDebugDirLen
;
882 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
883 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
885 /* finally testing filename */
886 if (elf_check_debug_link(slash
, fmap_link
, crc
)) goto found
;
889 WARN("Couldn't locate or map %s\n", filename
);
890 HeapFree(GetProcessHeap(), 0, p
);
891 HeapFree(GetProcessHeap(), 0, fmap_link
);
895 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
896 HeapFree(GetProcessHeap(), 0, p
);
897 fmap
->u
.elf
.alternate
= fmap_link
;
901 /******************************************************************
902 * elf_locate_build_id_target
904 * Try to find the .so file containing the debug info out of the build-id note information
906 static BOOL
elf_locate_build_id_target(struct image_file_map
* fmap
, const BYTE
* id
, unsigned idlen
)
908 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
909 static const WCHAR buildidW
[] = {'.','b','u','i','l','d','-','i','d','/'};
910 static const WCHAR dotDebug0W
[] = {'.','d','e','b','u','g',0};
911 struct image_file_map
* fmap_link
= NULL
;
914 const BYTE
* idend
= id
+ idlen
;
915 struct elf_map_file_data emfd
;
917 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
918 if (!fmap_link
) return FALSE
;
920 p
= HeapAlloc(GetProcessHeap(), 0,
921 sizeof(globalDebugDirW
) + sizeof(buildidW
) +
922 (idlen
* 2 + 1) * sizeof(WCHAR
) + sizeof(dotDebug0W
));
924 memcpy(z
, globalDebugDirW
, sizeof(globalDebugDirW
));
925 z
+= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
926 memcpy(z
, buildidW
, sizeof(buildidW
));
927 z
+= sizeof(buildidW
) / sizeof(WCHAR
);
931 *z
++ = "0123456789abcdef"[*id
>> 4 ];
932 *z
++ = "0123456789abcdef"[*id
& 0x0F];
939 *z
++ = "0123456789abcdef"[*id
>> 4 ];
940 *z
++ = "0123456789abcdef"[*id
& 0x0F];
943 memcpy(z
, dotDebug0W
, sizeof(dotDebug0W
));
944 TRACE("checking %s\n", wine_dbgstr_w(p
));
946 emfd
.kind
= from_file
;
947 emfd
.u
.file
.filename
= p
;
948 if (elf_map_file(&emfd
, fmap_link
))
950 struct image_section_map buildid_sect
;
951 if (elf_find_section(fmap_link
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
953 const uint32_t* note
;
955 note
= (const uint32_t*)image_map_section(&buildid_sect
);
956 if (note
!= IMAGE_NO_MAP
)
958 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
959 if (note
[2] == NT_GNU_BUILD_ID
)
961 if (note
[1] == idlen
&&
962 !memcmp(note
+ 3 + ((note
[0] + 3) >> 2), idend
- idlen
, idlen
))
964 TRACE("Located debug information file at %s\n", debugstr_w(p
));
965 HeapFree(GetProcessHeap(), 0, p
);
966 fmap
->u
.elf
.alternate
= fmap_link
;
969 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p
));
972 image_unmap_section(&buildid_sect
);
974 elf_unmap_file(fmap_link
);
977 TRACE("not found\n");
978 HeapFree(GetProcessHeap(), 0, p
);
979 HeapFree(GetProcessHeap(), 0, fmap_link
);
983 /******************************************************************
984 * elf_check_alternate
986 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
987 * or .gnu_debuglink sections.
989 static BOOL
elf_check_alternate(struct image_file_map
* fmap
, const struct module
* module
)
993 struct image_section_map buildid_sect
, debuglink_sect
;
995 /* if present, add the .gnu_debuglink file as an alternate to current one */
996 if (elf_find_section(fmap
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
998 const uint32_t* note
;
1001 note
= (const uint32_t*)image_map_section(&buildid_sect
);
1002 if (note
!= IMAGE_NO_MAP
)
1004 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1005 if (note
[2] == NT_GNU_BUILD_ID
)
1007 ret
= elf_locate_build_id_target(fmap
, (const BYTE
*)(note
+ 3 + ((note
[0] + 3) >> 2)), note
[1]);
1010 image_unmap_section(&buildid_sect
);
1012 /* if present, add the .gnu_debuglink file as an alternate to current one */
1013 if (!ret
&& elf_find_section(fmap
, ".gnu_debuglink", SHT_NULL
, &debuglink_sect
))
1015 const char* dbg_link
;
1018 dbg_link
= (const char*)image_map_section(&debuglink_sect
);
1019 if (dbg_link
!= IMAGE_NO_MAP
)
1021 /* The content of a debug link section is:
1022 * 1/ a NULL terminated string, containing the file name for the
1024 * 2/ padding on 4 byte boundary
1025 * 3/ CRC of the linked ELF file
1027 DWORD crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
1028 ret
= elf_locate_debug_link(fmap
, dbg_link
, module
->module
.LoadedImageName
, crc
);
1030 WARN("Couldn't load linked debug file for %s\n",
1031 debugstr_w(module
->module
.ModuleName
));
1033 image_unmap_section(&debuglink_sect
);
1035 return found
? ret
: TRUE
;
1038 /******************************************************************
1039 * elf_load_debug_info_from_map
1041 * Loads the symbolic information from ELF module which mapping is described
1043 * the module has been loaded at 'load_offset' address, so symbols' address
1044 * relocation is performed.
1045 * CRC is checked if fmap->with_crc is TRUE
1047 * 0 if the file doesn't contain symbolic info (or this info cannot be
1051 static BOOL
elf_load_debug_info_from_map(struct module
* module
,
1052 struct image_file_map
* fmap
,
1054 struct hash_table
* ht_symtab
)
1056 BOOL ret
= FALSE
, lret
;
1057 struct elf_thunk_area thunks
[] =
1059 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
1060 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1061 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1062 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1063 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1064 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1068 module
->module
.SymType
= SymExport
;
1070 /* create a hash table for the symtab */
1071 elf_hash_symtab(module
, pool
, ht_symtab
, fmap
, thunks
);
1073 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1075 struct image_section_map stab_sect
, stabstr_sect
;
1077 /* check if we need an alternate file (from debuglink or build-id) */
1078 ret
= elf_check_alternate(fmap
, module
);
1080 if (elf_find_section(fmap
, ".stab", SHT_NULL
, &stab_sect
) &&
1081 elf_find_section(fmap
, ".stabstr", SHT_NULL
, &stabstr_sect
))
1084 const char* stabstr
;
1086 stab
= image_map_section(&stab_sect
);
1087 stabstr
= image_map_section(&stabstr_sect
);
1088 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
1090 /* OK, now just parse all of the stabs. */
1091 lret
= stabs_parse(module
, module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_addr
,
1092 stab
, image_get_map_size(&stab_sect
),
1093 stabstr
, image_get_map_size(&stabstr_sect
),
1096 /* and fill in the missing information for stabs */
1097 elf_finish_stabs_info(module
, ht_symtab
);
1099 WARN("Couldn't correctly read stabs\n");
1102 image_unmap_section(&stab_sect
);
1103 image_unmap_section(&stabstr_sect
);
1105 lret
= dwarf2_parse(module
, module
->reloc_delta
, thunks
, fmap
);
1108 if (strstrW(module
->module
.ModuleName
, S_ElfW
) ||
1109 !strcmpW(module
->module
.ModuleName
, S_WineLoaderW
))
1111 /* add the thunks for native libraries */
1112 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1113 elf_new_wine_thunks(module
, ht_symtab
, thunks
);
1115 /* add all the public symbols from symtab */
1116 if (elf_new_public_symbols(module
, ht_symtab
) && !ret
) ret
= TRUE
;
1121 /******************************************************************
1122 * elf_load_debug_info
1124 * Loads ELF debugging information from the module image file.
1126 BOOL
elf_load_debug_info(struct module
* module
)
1130 struct hash_table ht_symtab
;
1131 struct module_format
* modfmt
;
1133 if (module
->type
!= DMT_ELF
|| !(modfmt
= module
->format_info
[DFI_ELF
]) || !modfmt
->u
.elf_info
)
1135 ERR("Bad elf module '%s'\n", debugstr_w(module
->module
.LoadedImageName
));
1139 pool_init(&pool
, 65536);
1140 hash_table_init(&pool
, &ht_symtab
, 256);
1142 ret
= elf_load_debug_info_from_map(module
, &modfmt
->u
.elf_info
->file_map
, &pool
, &ht_symtab
);
1144 pool_destroy(&pool
);
1148 /******************************************************************
1149 * elf_fetch_file_info
1151 * Gathers some more information for an ELF module from a given file
1153 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1154 DWORD
* size
, DWORD
* checksum
)
1156 struct image_file_map fmap
;
1158 struct elf_map_file_data emfd
;
1160 emfd
.kind
= from_file
;
1161 emfd
.u
.file
.filename
= name
;
1162 if (!elf_map_file(&emfd
, &fmap
)) return FALSE
;
1163 if (base
) *base
= fmap
.u
.elf
.elf_start
;
1164 *size
= fmap
.u
.elf
.elf_size
;
1165 *checksum
= calc_crc32(fmap
.u
.elf
.fd
);
1166 elf_unmap_file(&fmap
);
1170 static BOOL
elf_load_file_from_fmap(struct process
* pcs
, const WCHAR
* filename
,
1171 struct image_file_map
* fmap
, unsigned long load_offset
,
1172 unsigned long dyn_addr
, struct elf_info
* elf_info
)
1176 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
1178 struct image_section_map ism
;
1180 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1183 char* ptr
= (char*)fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1188 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1191 if (dyn
.d_tag
== DT_DEBUG
)
1193 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1194 if (load_offset
== 0 && dyn_addr
== 0) /* likely the case */
1195 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1196 dyn_addr
= ism
.fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1200 } while (dyn
.d_tag
!= DT_NULL
);
1201 if (dyn
.d_tag
== DT_NULL
) return ret
;
1206 if (elf_info
->flags
& ELF_INFO_MODULE
)
1208 struct elf_module_info
*elf_module_info
;
1209 struct module_format
* modfmt
;
1210 struct image_section_map ism
;
1211 unsigned long modbase
= load_offset
;
1213 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1215 unsigned long rva_dyn
= elf_get_map_rva(&ism
);
1217 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1218 debugstr_w(filename
), (unsigned long)fmap
->u
.elf
.elf_start
, rva_dyn
,
1219 load_offset
, dyn_addr
);
1220 if (dyn_addr
&& load_offset
+ rva_dyn
!= dyn_addr
)
1222 WARN("\thave to relocate: %lx\n", dyn_addr
- rva_dyn
);
1223 modbase
= dyn_addr
- rva_dyn
;
1225 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename
));
1228 modfmt
= HeapAlloc(GetProcessHeap(), 0,
1229 sizeof(struct module_format
) + sizeof(struct elf_module_info
));
1230 if (!modfmt
) return FALSE
;
1231 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
, FALSE
, modbase
,
1232 fmap
->u
.elf
.elf_size
, 0, calc_crc32(fmap
->u
.elf
.fd
));
1233 if (!elf_info
->module
)
1235 HeapFree(GetProcessHeap(), 0, modfmt
);
1238 elf_info
->module
->reloc_delta
= elf_info
->module
->module
.BaseOfImage
- fmap
->u
.elf
.elf_start
;
1239 elf_module_info
= (void*)(modfmt
+ 1);
1240 elf_info
->module
->format_info
[DFI_ELF
] = modfmt
;
1241 modfmt
->module
= elf_info
->module
;
1242 modfmt
->remove
= elf_module_remove
;
1243 modfmt
->loc_compute
= NULL
;
1244 modfmt
->u
.elf_info
= elf_module_info
;
1246 elf_module_info
->elf_addr
= load_offset
;
1248 elf_module_info
->file_map
= *fmap
;
1249 elf_reset_file_map(fmap
);
1250 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
1252 elf_info
->module
->module
.SymType
= SymDeferred
;
1255 else ret
= elf_load_debug_info(elf_info
->module
);
1257 elf_module_info
->elf_mark
= 1;
1258 elf_module_info
->elf_loader
= 0;
1261 if (elf_info
->flags
& ELF_INFO_NAME
)
1264 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1) * sizeof(WCHAR
));
1267 strcpyW(ptr
, filename
);
1268 elf_info
->module_name
= ptr
;
1276 /******************************************************************
1279 * Loads the information for ELF module stored in 'filename'
1280 * the module has been loaded at 'load_offset' address
1282 * -1 if the file cannot be found/opened
1283 * 0 if the file doesn't contain symbolic info (or this info cannot be
1287 static BOOL
elf_load_file(struct process
* pcs
, const WCHAR
* filename
,
1288 unsigned long load_offset
, unsigned long dyn_addr
,
1289 struct elf_info
* elf_info
)
1292 struct image_file_map fmap
;
1293 struct elf_map_file_data emfd
;
1295 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename
), load_offset
);
1297 emfd
.kind
= from_file
;
1298 emfd
.u
.file
.filename
= filename
;
1299 if (!elf_map_file(&emfd
, &fmap
)) return ret
;
1301 /* Next, we need to find a few of the internal ELF headers within
1302 * this thing. We need the main executable header, and the section
1305 if (!fmap
.u
.elf
.elf_start
&& !load_offset
)
1306 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1307 debugstr_w(filename
));
1309 ret
= elf_load_file_from_fmap(pcs
, filename
, &fmap
, load_offset
, dyn_addr
, elf_info
);
1311 elf_unmap_file(&fmap
);
1316 /******************************************************************
1317 * elf_load_file_from_path
1318 * tries to load an ELF file from a set of paths (separated by ':')
1320 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
1321 const WCHAR
* filename
,
1322 unsigned long load_offset
,
1323 unsigned long dyn_addr
,
1325 struct elf_info
* elf_info
)
1329 WCHAR
* pathW
= NULL
;
1332 if (!path
) return FALSE
;
1334 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1335 pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1336 if (!pathW
) return FALSE
;
1337 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, pathW
, len
);
1339 for (s
= pathW
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
1341 t
= strchrW(s
, ':');
1343 fn
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1 + lstrlenW(s
) + 1) * sizeof(WCHAR
));
1346 strcatW(fn
, S_SlashW
);
1347 strcatW(fn
, filename
);
1348 ret
= elf_load_file(hProcess
, fn
, load_offset
, dyn_addr
, elf_info
);
1349 HeapFree(GetProcessHeap(), 0, fn
);
1353 HeapFree(GetProcessHeap(), 0, pathW
);
1357 /******************************************************************
1358 * elf_load_file_from_dll_path
1360 * Tries to load an ELF file from the dll path
1362 static BOOL
elf_load_file_from_dll_path(HANDLE hProcess
,
1363 const WCHAR
* filename
,
1364 unsigned long load_offset
,
1365 unsigned long dyn_addr
,
1366 struct elf_info
* elf_info
)
1369 unsigned int index
= 0;
1372 while (!ret
&& (path
= wine_dll_enum_load_path( index
++ )))
1377 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1379 name
= HeapAlloc( GetProcessHeap(), 0,
1380 (len
+ lstrlenW(filename
) + 2) * sizeof(WCHAR
) );
1383 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, name
, len
);
1384 strcatW( name
, S_SlashW
);
1385 strcatW( name
, filename
);
1386 ret
= elf_load_file(hProcess
, name
, load_offset
, dyn_addr
, elf_info
);
1387 HeapFree( GetProcessHeap(), 0, name
);
1392 #ifdef AT_SYSINFO_EHDR
1393 /******************************************************************
1396 * locate some a value from the debuggee auxiliary vector
1398 static BOOL
elf_search_auxv(const struct process
* pcs
, unsigned type
, unsigned long* val
)
1400 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1401 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1407 si
->SizeOfStruct
= sizeof(*si
);
1408 si
->MaxNameLen
= MAX_SYM_NAME
;
1409 if (!SymFromName(pcs
->handle
, "libwine.so.1!__wine_main_environ", si
) ||
1410 !(addr
= (void*)(DWORD_PTR
)si
->Address
) ||
1411 !ReadProcessMemory(pcs
->handle
, addr
, &addr
, sizeof(addr
), NULL
) ||
1414 FIXME("can't find symbol in module\n");
1417 /* walk through envp[] */
1418 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1419 str_max
= (void*)(DWORD_PTR
)~0L;
1420 while (ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) &&
1421 (addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
))) != NULL
&& str
!= NULL
)
1422 str_max
= min(str_max
, str
);
1424 /* Walk through the end of envp[] array.
1425 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1426 * deleted, the last entry is replaced by an extra NULL.
1428 while (addr
< str_max
&& ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) && str
== NULL
)
1429 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
));
1431 while (ReadProcessMemory(pcs
->handle
, addr
, &auxv
, sizeof(auxv
), NULL
) && auxv
.a_type
!= AT_NULL
)
1433 if (auxv
.a_type
== type
)
1435 *val
= auxv
.a_un
.a_val
;
1438 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(auxv
));
1445 /******************************************************************
1446 * elf_search_and_load_file
1448 * lookup a file in standard ELF locations, and if found, load it
1450 static BOOL
elf_search_and_load_file(struct process
* pcs
, const WCHAR
* filename
,
1451 unsigned long load_offset
, unsigned long dyn_addr
,
1452 struct elf_info
* elf_info
)
1455 struct module
* module
;
1456 static const WCHAR S_libstdcPPW
[] = {'l','i','b','s','t','d','c','+','+','\0'};
1458 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
1459 if ((module
= module_is_already_loaded(pcs
, filename
)))
1461 elf_info
->module
= module
;
1462 elf_info
->module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1463 return module
->module
.SymType
;
1466 if (strstrW(filename
, S_libstdcPPW
)) return FALSE
; /* We know we can't do it */
1467 ret
= elf_load_file(pcs
, filename
, load_offset
, dyn_addr
, elf_info
);
1468 /* if relative pathname, try some absolute base dirs */
1469 if (!ret
&& !strchrW(filename
, '/'))
1471 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1472 getenv("PATH"), elf_info
) ||
1473 elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1474 getenv("LD_LIBRARY_PATH"), elf_info
);
1475 if (!ret
) ret
= elf_load_file_from_dll_path(pcs
, filename
,
1476 load_offset
, dyn_addr
, elf_info
);
1482 typedef BOOL (*enum_elf_modules_cb
)(const WCHAR
*, unsigned long load_addr
,
1483 unsigned long dyn_addr
, BOOL is_system
, void* user
);
1485 /******************************************************************
1486 * elf_enum_modules_internal
1488 * Enumerate ELF modules from a running process
1490 static BOOL
elf_enum_modules_internal(const struct process
* pcs
,
1491 const WCHAR
* main_name
,
1492 enum_elf_modules_cb cb
, void* user
)
1494 struct r_debug dbg_hdr
;
1498 WCHAR bufstrW
[MAX_PATH
];
1500 if (!pcs
->dbg_hdr_addr
||
1501 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1502 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1505 /* Now walk the linked list. In all known ELF implementations,
1506 * the dynamic loader maintains this linked list for us. In some
1507 * cases the first entry doesn't appear with a name, in other cases it
1510 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
1512 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1515 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
1516 lm
.l_name
!= NULL
&&
1517 ReadProcessMemory(pcs
->handle
, lm
.l_name
, bufstr
, sizeof(bufstr
), NULL
))
1519 bufstr
[sizeof(bufstr
) - 1] = '\0';
1520 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, sizeof(bufstrW
) / sizeof(WCHAR
));
1521 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1522 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, (unsigned long)lm
.l_ld
, FALSE
, user
)) break;
1526 #ifdef AT_SYSINFO_EHDR
1529 unsigned long ehdr_addr
;
1531 if (elf_search_auxv(pcs
, AT_SYSINFO_EHDR
, &ehdr_addr
))
1533 static const WCHAR vdsoW
[] = {'[','v','d','s','o',']','.','s','o',0};
1534 cb(vdsoW
, ehdr_addr
, 0, TRUE
, user
);
1541 /******************************************************************
1544 * Lookup in a running ELF process the loader, and sets its ELF link
1545 * address (for accessing the list of loaded .so libs) in pcs.
1546 * If flags is ELF_INFO_MODULE, the module for the loader is also
1547 * added as a module into pcs.
1549 static BOOL
elf_search_loader(struct process
* pcs
, struct elf_info
* elf_info
)
1551 return elf_search_and_load_file(pcs
, get_wine_loader_name(), 0, 0, elf_info
);
1554 /******************************************************************
1555 * elf_read_wine_loader_dbg_info
1557 * Try to find a decent wine executable which could have loaded the debuggee
1559 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1561 struct elf_info elf_info
;
1563 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
1564 if (!elf_search_loader(pcs
, &elf_info
)) return FALSE
;
1565 elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_loader
= 1;
1566 module_set_module(elf_info
.module
, S_WineLoaderW
);
1567 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
1570 struct elf_enum_user
1576 static BOOL
elf_enum_modules_translate(const WCHAR
* name
, unsigned long load_addr
,
1577 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1579 struct elf_enum_user
* eeu
= user
;
1580 return eeu
->cb(name
, load_addr
, eeu
->user
);
1583 /******************************************************************
1586 * Enumerates the ELF loaded modules from a running target (hProc)
1587 * This function doesn't require that someone has called SymInitialize
1588 * on this very process.
1590 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1593 struct elf_info elf_info
;
1595 struct elf_enum_user eeu
;
1597 memset(&pcs
, 0, sizeof(pcs
));
1599 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_NAME
;
1600 if (!elf_search_loader(&pcs
, &elf_info
)) return FALSE
;
1601 pcs
.dbg_hdr_addr
= elf_info
.dbg_hdr_addr
;
1604 ret
= elf_enum_modules_internal(&pcs
, elf_info
.module_name
, elf_enum_modules_translate
, &eeu
);
1605 HeapFree(GetProcessHeap(), 0, (char*)elf_info
.module_name
);
1611 struct process
* pcs
;
1612 struct elf_info elf_info
;
1617 /******************************************************************
1620 * Callback for elf_load_module, used to walk the list of loaded
1623 static BOOL
elf_load_cb(const WCHAR
* name
, unsigned long load_addr
,
1624 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1626 struct elf_load
* el
= user
;
1630 if (is_system
) /* virtual ELF module, created by system. handle it from memory */
1632 struct module
* module
;
1633 struct elf_map_file_data emfd
;
1634 struct image_file_map fmap
;
1636 if ((module
= module_is_already_loaded(el
->pcs
, name
)))
1638 el
->elf_info
.module
= module
;
1639 el
->elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1640 return module
->module
.SymType
;
1643 emfd
.kind
= from_process
;
1644 emfd
.u
.process
.handle
= el
->pcs
->handle
;
1645 emfd
.u
.process
.load_addr
= (void*)load_addr
;
1647 if (elf_map_file(&emfd
, &fmap
))
1648 el
->ret
= elf_load_file_from_fmap(el
->pcs
, name
, &fmap
, load_addr
, 0, &el
->elf_info
);
1653 /* memcmp is needed for matches when bufstr contains also version information
1654 * el->name: libc.so, name: libc.so.6.0
1656 p
= strrchrW(name
, '/');
1660 if (!el
->name
|| !memcmp(p
, el
->name
, lstrlenW(el
->name
) * sizeof(WCHAR
)))
1662 el
->ret
= elf_search_and_load_file(el
->pcs
, name
, load_addr
, dyn_addr
, &el
->elf_info
);
1663 if (el
->name
) ret
= FALSE
;
1669 /******************************************************************
1672 * loads an ELF module and stores it in process' module list
1673 * Also, find module real name and load address from
1674 * the real loaded modules list in pcs address space
1676 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1680 TRACE("(%p %s %08lx)\n", pcs
, debugstr_w(name
), addr
);
1682 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1685 if (pcs
->dbg_hdr_addr
) /* we're debugging a life target */
1688 /* do only the lookup from the filename, not the path (as we lookup module
1689 * name in the process' loaded module list)
1691 el
.name
= strrchrW(name
, '/');
1692 if (!el
.name
++) el
.name
= name
;
1695 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1701 el
.ret
= elf_search_and_load_file(pcs
, el
.name
, addr
, 0, &el
.elf_info
);
1703 if (!el
.ret
) return NULL
;
1704 assert(el
.elf_info
.module
);
1705 return el
.elf_info
.module
;
1708 /******************************************************************
1709 * elf_synchronize_module_list
1711 * this function rescans the debuggee module's list and synchronizes it with
1712 * the one from 'pcs', i.e.:
1713 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1714 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1716 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1718 struct module
* module
;
1721 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1723 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1724 module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 0;
1728 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1730 el
.name
= NULL
; /* fetch all modules */
1732 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1735 module
= pcs
->lmodules
;
1738 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1740 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
1742 if (!elf_info
->elf_mark
&& !elf_info
->elf_loader
)
1744 module_remove(pcs
, module
);
1745 /* restart all over */
1746 module
= pcs
->lmodules
;
1750 module
= module
->next
;
1755 #else /* !__ELF__ */
1757 BOOL
elf_find_section(struct image_file_map
* fmap
, const char* name
,
1758 unsigned sht
, struct image_section_map
* ism
)
1763 const char* elf_map_section(struct image_section_map
* ism
)
1768 void elf_unmap_section(struct image_section_map
* ism
)
1771 unsigned elf_get_map_size(const struct image_section_map
* ism
)
1776 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
1781 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1786 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1787 DWORD
* size
, DWORD
* checksum
)
1792 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1797 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1802 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1807 BOOL
elf_load_debug_info(struct module
* module
)
1812 int elf_is_in_thunk_area(unsigned long addr
,
1813 const struct elf_thunk_area
* thunks
)
1817 #endif /* __ELF__ */