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 #include "dbghelp_private.h"
49 #include "image_private.h"
51 #include "wine/library.h"
52 #include "wine/debug.h"
53 #include "wine/heap.h"
57 #define ELF_INFO_DEBUG_HEADER 0x0001
58 #define ELF_INFO_MODULE 0x0002
59 #define ELF_INFO_NAME 0x0004
61 #ifndef NT_GNU_BUILD_ID
62 #define NT_GNU_BUILD_ID 3
65 #ifndef HAVE_STRUCT_R_DEBUG
69 struct link_map
*r_map
;
79 #endif /* HAVE_STRUCT_R_DEBUG */
90 #ifndef HAVE_STRUCT_LINK_MAP
96 struct link_map
*l_next
, *l_prev
;
98 #endif /* HAVE_STRUCT_LINK_MAP */
105 DWORD l_next
, l_prev
;
108 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
112 unsigned flags
; /* IN one (or several) of the ELF_INFO constants */
113 DWORD_PTR dbg_hdr_addr
; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
114 struct module
* module
; /* OUT loaded module (if ELF_INFO_MODULE is set) */
115 const WCHAR
* module_name
; /* OUT found module name (if ELF_INFO_NAME is set) */
120 struct hash_table_elt ht_elt
;
122 struct symt_compiland
* compiland
;
126 struct elf_thunk_area
129 THUNK_ORDINAL ordinal
;
130 unsigned long rva_start
;
131 unsigned long rva_end
;
134 struct elf_module_info
136 unsigned long elf_addr
;
137 unsigned short elf_mark
: 1,
139 struct image_file_map file_map
;
142 /******************************************************************
145 * Maps a single section into memory from an ELF file
147 const char* elf_map_section(struct image_section_map
* ism
)
149 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
150 size_t ofst
, size
, pgsz
= sysconf( _SC_PAGESIZE
);
152 assert(ism
->fmap
->modtype
== DMT_ELF
);
153 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
||
154 fmap
->sect
[ism
->sidx
].shdr
.sh_type
== SHT_NOBITS
)
157 if (fmap
->target_copy
)
159 return fmap
->target_copy
+ fmap
->sect
[ism
->sidx
].shdr
.sh_offset
;
161 /* align required information on page size (we assume pagesize is a power of 2) */
162 ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
163 size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
164 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
165 fmap
->sect
[ism
->sidx
].mapped
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
,
167 if (fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
) return IMAGE_NO_MAP
;
168 return fmap
->sect
[ism
->sidx
].mapped
+ (fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& (pgsz
- 1));
171 /******************************************************************
174 * Finds a section by name (and type) into memory from an ELF file
175 * or its alternate if any
177 BOOL
elf_find_section(struct image_file_map
* _fmap
, const char* name
,
178 unsigned sht
, struct image_section_map
* ism
)
180 struct elf_file_map
* fmap
;
185 fmap
= &_fmap
->u
.elf
;
186 if (fmap
->shstrtab
== IMAGE_NO_MAP
)
188 struct image_section_map hdr_ism
= {_fmap
, fmap
->elfhdr
.e_shstrndx
};
189 if ((fmap
->shstrtab
= elf_map_section(&hdr_ism
)) == IMAGE_NO_MAP
) break;
191 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
193 if (strcmp(fmap
->shstrtab
+ fmap
->sect
[i
].shdr
.sh_name
, name
) == 0 &&
194 (sht
== SHT_NULL
|| sht
== fmap
->sect
[i
].shdr
.sh_type
))
201 _fmap
= fmap
->alternate
;
208 /******************************************************************
211 * Unmaps a single section from memory
213 void elf_unmap_section(struct image_section_map
* ism
)
215 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
217 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->elfhdr
.e_shnum
&& !fmap
->target_copy
&&
218 fmap
->sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
220 size_t pgsz
= sysconf( _SC_PAGESIZE
);
221 size_t ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
222 size_t size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
223 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
224 if (munmap((char*)fmap
->sect
[ism
->sidx
].mapped
, size
) < 0)
225 WARN("Couldn't unmap the section\n");
226 fmap
->sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
230 static void elf_end_find(struct image_file_map
* fmap
)
232 struct image_section_map ism
;
237 ism
.sidx
= fmap
->u
.elf
.elfhdr
.e_shstrndx
;
238 elf_unmap_section(&ism
);
239 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
240 fmap
= fmap
->u
.elf
.alternate
;
244 /******************************************************************
247 * Get the RVA of an ELF section
249 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
251 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
253 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_addr
- ism
->fmap
->u
.elf
.elf_start
;
256 /******************************************************************
259 * Get the size of an ELF section
261 unsigned elf_get_map_size(const struct image_section_map
* ism
)
263 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
265 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_size
;
268 static inline void elf_reset_file_map(struct image_file_map
* fmap
)
271 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
272 fmap
->u
.elf
.alternate
= NULL
;
273 fmap
->u
.elf
.target_copy
= NULL
;
276 struct elf_map_file_data
278 enum {from_file
, from_process
} kind
;
283 const WCHAR
* filename
;
293 static BOOL
elf_map_file_read(struct image_file_map
* fmap
, struct elf_map_file_data
* emfd
,
294 void* buf
, size_t len
, off_t off
)
301 return pread(fmap
->u
.elf
.fd
, buf
, len
, off
) == len
;
303 return ReadProcessMemory(emfd
->u
.process
.handle
,
304 (void*)((unsigned long)emfd
->u
.process
.load_addr
+ (unsigned long)off
),
305 buf
, len
, &dw
) && dw
== len
;
312 static BOOL
elf_map_shdr(struct elf_map_file_data
* emfd
, struct image_file_map
* fmap
, unsigned int i
)
314 if (fmap
->addr_size
== 32)
318 if (!elf_map_file_read(fmap
, emfd
, &shdr32
, sizeof(shdr32
),
319 fmap
->u
.elf
.elfhdr
.e_shoff
+ i
* sizeof(Elf32_Shdr
)))
322 fmap
->u
.elf
.sect
[i
].shdr
.sh_name
= shdr32
.sh_name
;
323 fmap
->u
.elf
.sect
[i
].shdr
.sh_type
= shdr32
.sh_type
;
324 fmap
->u
.elf
.sect
[i
].shdr
.sh_flags
= shdr32
.sh_flags
;
325 fmap
->u
.elf
.sect
[i
].shdr
.sh_addr
= shdr32
.sh_addr
;
326 fmap
->u
.elf
.sect
[i
].shdr
.sh_offset
= shdr32
.sh_offset
;
327 fmap
->u
.elf
.sect
[i
].shdr
.sh_size
= shdr32
.sh_size
;
328 fmap
->u
.elf
.sect
[i
].shdr
.sh_link
= shdr32
.sh_link
;
329 fmap
->u
.elf
.sect
[i
].shdr
.sh_info
= shdr32
.sh_info
;
330 fmap
->u
.elf
.sect
[i
].shdr
.sh_addralign
= shdr32
.sh_addralign
;
331 fmap
->u
.elf
.sect
[i
].shdr
.sh_entsize
= shdr32
.sh_entsize
;
335 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.sect
[i
].shdr
, sizeof(fmap
->u
.elf
.sect
[i
].shdr
),
336 fmap
->u
.elf
.elfhdr
.e_shoff
+ i
* sizeof(Elf64_Shdr
)))
342 /******************************************************************
345 * Maps an ELF file into memory (and checks it's a real ELF file)
347 static BOOL
elf_map_file(struct elf_map_file_data
* emfd
, struct image_file_map
* fmap
)
349 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
352 size_t tmp
, page_mask
= sysconf( _SC_PAGESIZE
) - 1;
356 unsigned char e_ident
[EI_NIDENT
];
361 len
= WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, NULL
, 0, NULL
, NULL
);
362 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
363 WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, filename
, len
, NULL
, NULL
);
372 elf_reset_file_map(fmap
);
374 fmap
->modtype
= DMT_ELF
;
376 fmap
->u
.elf
.target_copy
= NULL
;
381 /* check that the file exists, and that the module hasn't been loaded yet */
382 if (stat(filename
, &statbuf
) == -1 || S_ISDIR(statbuf
.st_mode
)) goto done
;
384 /* Now open the file, so that we can mmap() it. */
385 if ((fmap
->u
.elf
.fd
= open(filename
, O_RDONLY
)) == -1) goto done
;
391 if (!elf_map_file_read(fmap
, emfd
, e_ident
, sizeof(e_ident
), 0))
394 /* and check for an ELF header */
395 if (memcmp(e_ident
, elf_signature
, sizeof(elf_signature
)))
398 fmap
->addr_size
= e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
400 if (fmap
->addr_size
== 32)
404 if (!elf_map_file_read(fmap
, emfd
, &elfhdr32
, sizeof(elfhdr32
), 0))
407 memcpy(fmap
->u
.elf
.elfhdr
.e_ident
, elfhdr32
.e_ident
, EI_NIDENT
);
408 fmap
->u
.elf
.elfhdr
.e_type
= elfhdr32
.e_type
;
409 fmap
->u
.elf
.elfhdr
.e_machine
= elfhdr32
.e_machine
;
410 fmap
->u
.elf
.elfhdr
.e_version
= elfhdr32
.e_version
;
411 fmap
->u
.elf
.elfhdr
.e_entry
= elfhdr32
.e_entry
;
412 fmap
->u
.elf
.elfhdr
.e_phoff
= elfhdr32
.e_phoff
;
413 fmap
->u
.elf
.elfhdr
.e_shoff
= elfhdr32
.e_shoff
;
414 fmap
->u
.elf
.elfhdr
.e_flags
= elfhdr32
.e_flags
;
415 fmap
->u
.elf
.elfhdr
.e_ehsize
= elfhdr32
.e_ehsize
;
416 fmap
->u
.elf
.elfhdr
.e_phentsize
= elfhdr32
.e_phentsize
;
417 fmap
->u
.elf
.elfhdr
.e_phnum
= elfhdr32
.e_phnum
;
418 fmap
->u
.elf
.elfhdr
.e_shentsize
= elfhdr32
.e_shentsize
;
419 fmap
->u
.elf
.elfhdr
.e_shnum
= elfhdr32
.e_shnum
;
420 fmap
->u
.elf
.elfhdr
.e_shstrndx
= elfhdr32
.e_shstrndx
;
424 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.elfhdr
, sizeof(fmap
->u
.elf
.elfhdr
), 0))
428 fmap
->u
.elf
.sect
= HeapAlloc(GetProcessHeap(), 0,
429 fmap
->u
.elf
.elfhdr
.e_shnum
* sizeof(fmap
->u
.elf
.sect
[0]));
430 if (!fmap
->u
.elf
.sect
) goto done
;
432 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_shnum
; i
++)
434 if (!elf_map_shdr(emfd
, fmap
, i
))
436 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
437 fmap
->u
.elf
.sect
= NULL
;
440 fmap
->u
.elf
.sect
[i
].mapped
= IMAGE_NO_MAP
;
443 /* grab size of module once loaded in memory */
444 fmap
->u
.elf
.elf_size
= 0;
445 fmap
->u
.elf
.elf_start
= ~0L;
446 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_phnum
; i
++)
448 if (fmap
->addr_size
== 32)
452 if (elf_map_file_read(fmap
, emfd
, &phdr
, sizeof(phdr
),
453 fmap
->u
.elf
.elfhdr
.e_phoff
+ i
* sizeof(phdr
)) &&
454 phdr
.p_type
== PT_LOAD
)
456 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
457 if (fmap
->u
.elf
.elf_size
< tmp
) fmap
->u
.elf
.elf_size
= tmp
;
458 if (phdr
.p_vaddr
< fmap
->u
.elf
.elf_start
) fmap
->u
.elf
.elf_start
= phdr
.p_vaddr
;
465 if (elf_map_file_read(fmap
, emfd
, &phdr
, sizeof(phdr
),
466 fmap
->u
.elf
.elfhdr
.e_phoff
+ i
* sizeof(phdr
)) &&
467 phdr
.p_type
== PT_LOAD
)
469 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
470 if (fmap
->u
.elf
.elf_size
< tmp
) fmap
->u
.elf
.elf_size
= tmp
;
471 if (phdr
.p_vaddr
< fmap
->u
.elf
.elf_start
) fmap
->u
.elf
.elf_start
= phdr
.p_vaddr
;
475 /* if non relocatable ELF, then remove fixed address from computation
476 * otherwise, all addresses are zero based and start has no effect
478 fmap
->u
.elf
.elf_size
-= fmap
->u
.elf
.elf_start
;
482 case from_file
: break;
484 if (!(fmap
->u
.elf
.target_copy
= HeapAlloc(GetProcessHeap(), 0, fmap
->u
.elf
.elf_size
)))
486 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
489 if (!ReadProcessMemory(emfd
->u
.process
.handle
, emfd
->u
.process
.load_addr
, fmap
->u
.elf
.target_copy
,
490 fmap
->u
.elf
.elf_size
, NULL
))
492 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
493 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
500 HeapFree(GetProcessHeap(), 0, filename
);
504 /******************************************************************
507 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
509 static void elf_unmap_file(struct image_file_map
* fmap
)
513 if (fmap
->u
.elf
.fd
!= -1)
515 struct image_section_map ism
;
517 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.elf
.elfhdr
.e_shnum
; ism
.sidx
++)
519 elf_unmap_section(&ism
);
521 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
522 close(fmap
->u
.elf
.fd
);
524 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
525 fmap
= fmap
->u
.elf
.alternate
;
529 static void elf_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
531 elf_unmap_file(&modfmt
->u
.elf_info
->file_map
);
532 HeapFree(GetProcessHeap(), 0, modfmt
);
535 /******************************************************************
536 * elf_is_in_thunk_area
538 * Check whether an address lies within one of the thunk area we
541 int elf_is_in_thunk_area(unsigned long addr
,
542 const struct elf_thunk_area
* thunks
)
546 if (thunks
) for (i
= 0; thunks
[i
].symname
; i
++)
548 if (addr
>= thunks
[i
].rva_start
&& addr
< thunks
[i
].rva_end
)
554 /******************************************************************
557 * creating an internal hash table to ease use ELF symtab information lookup
559 static void elf_hash_symtab(struct module
* module
, struct pool
* pool
,
560 struct hash_table
* ht_symtab
, struct image_file_map
* fmap
,
561 struct elf_thunk_area
* thunks
)
566 struct symt_compiland
* compiland
= NULL
;
568 struct symtab_elt
* ste
;
569 struct image_section_map ism
, ism_str
;
572 if (!elf_find_section(fmap
, ".symtab", SHT_SYMTAB
, &ism
) &&
573 !elf_find_section(fmap
, ".dynsym", SHT_DYNSYM
, &ism
)) return;
574 if ((symtab
= image_map_section(&ism
)) == IMAGE_NO_MAP
) return;
575 ism_str
.fmap
= ism
.fmap
;
576 ism_str
.sidx
= fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_link
;
577 if ((strp
= image_map_section(&ism_str
)) == IMAGE_NO_MAP
)
579 image_unmap_section(&ism
);
583 nsym
= image_get_map_size(&ism
) /
584 (fmap
->addr_size
== 32 ? sizeof(Elf32_Sym
) : sizeof(Elf64_Sym
));
586 for (j
= 0; thunks
[j
].symname
; j
++)
587 thunks
[j
].rva_start
= thunks
[j
].rva_end
= 0;
589 for (i
= 0; i
< nsym
; i
++)
593 if (fmap
->addr_size
== 32)
595 Elf32_Sym
*sym32
= &((Elf32_Sym
*)symtab
)[i
];
597 sym
.st_name
= sym32
->st_name
;
598 sym
.st_value
= sym32
->st_value
;
599 sym
.st_size
= sym32
->st_size
;
600 sym
.st_info
= sym32
->st_info
;
601 sym
.st_other
= sym32
->st_other
;
602 sym
.st_shndx
= sym32
->st_shndx
;
605 sym
= ((Elf64_Sym
*)symtab
)[i
];
607 /* Ignore certain types of entries which really aren't of that much
610 if ((ELF32_ST_TYPE(sym
.st_info
) != STT_NOTYPE
&&
611 ELF32_ST_TYPE(sym
.st_info
) != STT_FILE
&&
612 ELF32_ST_TYPE(sym
.st_info
) != STT_OBJECT
&&
613 ELF32_ST_TYPE(sym
.st_info
) != STT_FUNC
) ||
614 sym
.st_shndx
== SHN_UNDEF
)
619 symname
= strp
+ sym
.st_name
;
621 /* handle some specific symtab (that we'll throw away when done) */
622 switch (ELF32_ST_TYPE(sym
.st_info
))
626 compiland
= symt_new_compiland(module
, sym
.st_value
,
627 source_new(module
, NULL
, symname
));
632 /* we are only interested in wine markers inserted by winebuild */
633 for (j
= 0; thunks
[j
].symname
; j
++)
635 if (!strcmp(symname
, thunks
[j
].symname
))
637 thunks
[j
].rva_start
= sym
.st_value
;
638 thunks
[j
].rva_end
= sym
.st_value
+ sym
.st_size
;
645 /* FIXME: we don't need to handle them (GCC internals)
646 * Moreover, they screw up our symbol lookup :-/
648 if (symname
[0] == '.' && symname
[1] == 'L' && isdigit(symname
[2]))
651 ste
= pool_alloc(pool
, sizeof(*ste
));
652 ste
->ht_elt
.name
= symname
;
653 /* GCC emits, in some cases, a .<digit>+ suffix.
654 * This is used for static variable inside functions, so
655 * that we can have several such variables with same name in
656 * the same compilation unit
657 * We simply ignore that suffix when present (we also get rid
658 * of it in stabs parsing)
660 ptr
= symname
+ strlen(symname
) - 1;
663 while (isdigit(*ptr
) && ptr
>= symname
) ptr
--;
664 if (ptr
> symname
&& *ptr
== '.')
666 char* n
= pool_alloc(pool
, ptr
- symname
+ 1);
667 memcpy(n
, symname
, ptr
- symname
+ 1);
668 n
[ptr
- symname
] = '\0';
669 ste
->ht_elt
.name
= n
;
673 ste
->compiland
= compiland
;
675 hash_table_add(ht_symtab
, &ste
->ht_elt
);
677 /* as we added in the ht_symtab pointers to the symbols themselves,
678 * we cannot unmap yet the sections, it will be done when we're over
683 /******************************************************************
686 * lookup a symbol by name in our internal hash table for the symtab
688 static const Elf64_Sym
*elf_lookup_symtab(const struct module
* module
,
689 const struct hash_table
* ht_symtab
,
690 const char* name
, const struct symt
* compiland
)
692 struct symtab_elt
* weak_result
= NULL
; /* without compiland name */
693 struct symtab_elt
* result
= NULL
;
694 struct hash_table_iter hti
;
695 struct symtab_elt
* ste
;
696 const char* compiland_name
;
697 const char* compiland_basename
;
700 /* we need weak match up (at least) when symbols of same name,
701 * defined several times in different compilation units,
702 * are merged in a single one (hence a different filename for c.u.)
706 compiland_name
= source_get(module
,
707 ((const struct symt_compiland
*)compiland
)->source
);
708 compiland_basename
= strrchr(compiland_name
, '/');
709 if (!compiland_basename
++) compiland_basename
= compiland_name
;
711 else compiland_name
= compiland_basename
= NULL
;
713 hash_table_iter_init(ht_symtab
, &hti
, name
);
714 while ((ste
= hash_table_iter_up(&hti
)))
716 if (ste
->used
|| strcmp(ste
->ht_elt
.name
, name
)) continue;
719 if ((ste
->compiland
&& !compiland_name
) || (!ste
->compiland
&& compiland_name
))
721 if (ste
->compiland
&& compiland_name
)
723 const char* filename
= source_get(module
, ste
->compiland
->source
);
724 if (strcmp(filename
, compiland_name
))
726 base
= strrchr(filename
, '/');
727 if (!base
++) base
= filename
;
728 if (strcmp(base
, compiland_basename
)) continue;
733 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
734 name
, compiland_name
,
735 source_get(module
, result
->compiland
->source
), (unsigned int)result
->sym
.st_value
,
736 source_get(module
, ste
->compiland
->source
), (unsigned int)ste
->sym
.st_value
);
744 if (!result
&& !(result
= weak_result
))
746 FIXME("Couldn't find symbol %s!%s in symtab\n",
747 debugstr_w(module
->module
.ModuleName
), name
);
753 /******************************************************************
754 * elf_finish_stabs_info
756 * - get any relevant information (address & size) from the bits we got from the
757 * stabs debugging information
759 static void elf_finish_stabs_info(struct module
* module
, const struct hash_table
* symtab
)
761 struct hash_table_iter hti
;
764 const Elf64_Sym
* symp
;
765 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
767 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
768 while ((ptr
= hash_table_iter_up(&hti
)))
770 sym
= CONTAINING_RECORD(ptr
, struct symt_ht
, hash_elt
);
771 switch (sym
->symt
.tag
)
774 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
775 ((struct symt_function
*)sym
)->size
)
779 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
780 ((struct symt_function
*)sym
)->container
);
783 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
784 ((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
+ symp
->st_value
)
785 FIXME("Changing address for %p/%s!%s from %08lx to %s\n",
786 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
787 ((struct symt_function
*)sym
)->address
,
788 wine_dbgstr_longlong(elf_info
->elf_addr
+ symp
->st_value
));
789 if (((struct symt_function
*)sym
)->size
&& ((struct symt_function
*)sym
)->size
!= symp
->st_size
)
790 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
791 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
792 ((struct symt_function
*)sym
)->size
, (unsigned int)symp
->st_size
);
794 ((struct symt_function
*)sym
)->address
= elf_info
->elf_addr
+ symp
->st_value
;
795 ((struct symt_function
*)sym
)->size
= symp
->st_size
;
797 FIXME("Couldn't find %s!%s\n",
798 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
801 switch (((struct symt_data
*)sym
)->kind
)
804 case DataIsFileStatic
:
805 if (((struct symt_data
*)sym
)->u
.var
.kind
!= loc_absolute
||
806 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
)
808 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
809 ((struct symt_data
*)sym
)->container
);
812 if (((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
&&
813 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
+ symp
->st_value
)
814 FIXME("Changing address for %p/%s!%s from %08lx to %s\n",
815 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
816 ((struct symt_function
*)sym
)->address
,
817 wine_dbgstr_longlong(elf_info
->elf_addr
+ symp
->st_value
));
818 ((struct symt_data
*)sym
)->u
.var
.offset
= elf_info
->elf_addr
+ symp
->st_value
;
819 ((struct symt_data
*)sym
)->kind
= (ELF32_ST_BIND(symp
->st_info
) == STB_LOCAL
) ?
820 DataIsFileStatic
: DataIsGlobal
;
822 FIXME("Couldn't find %s!%s\n",
823 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
829 FIXME("Unsupported tag %u\n", sym
->symt
.tag
);
833 /* since we may have changed some addresses & sizes, mark the module to be resorted */
834 module
->sortlist_valid
= FALSE
;
837 /******************************************************************
838 * elf_load_wine_thunks
840 * creating the thunk objects for a wine native DLL
842 static int elf_new_wine_thunks(struct module
* module
, const struct hash_table
* ht_symtab
,
843 const struct elf_thunk_area
* thunks
)
846 struct hash_table_iter hti
;
847 struct symtab_elt
* ste
;
849 struct symt_ht
* symt
;
851 hash_table_iter_init(ht_symtab
, &hti
, NULL
);
852 while ((ste
= hash_table_iter_up(&hti
)))
854 if (ste
->used
) continue;
856 addr
= module
->reloc_delta
+ ste
->sym
.st_value
;
858 j
= elf_is_in_thunk_area(ste
->sym
.st_value
, thunks
);
859 if (j
>= 0) /* thunk found */
861 symt_new_thunk(module
, ste
->compiland
, ste
->ht_elt
.name
, thunks
[j
].ordinal
,
862 addr
, ste
->sym
.st_size
);
869 symt
= symt_find_nearest(module
, addr
);
870 if (symt
&& !symt_get_address(&symt
->symt
, &ref_addr
))
872 if (!symt
|| addr
!= ref_addr
)
874 /* creating public symbols for all the ELF symbols which haven't been
875 * used yet (ie we have no debug information on them)
876 * That's the case, for example, of the .spec.c files
878 switch (ELF32_ST_TYPE(ste
->sym
.st_info
))
881 symt_new_function(module
, ste
->compiland
, ste
->ht_elt
.name
,
882 addr
, ste
->sym
.st_size
, NULL
);
885 loc
.kind
= loc_absolute
;
888 symt_new_global_variable(module
, ste
->compiland
, ste
->ht_elt
.name
,
889 ELF32_ST_BIND(ste
->sym
.st_info
) == STB_LOCAL
,
890 loc
, ste
->sym
.st_size
, NULL
);
893 FIXME("Shouldn't happen\n");
896 /* FIXME: this is a hack !!!
897 * we are adding new symbols, but as we're parsing a symbol table
898 * (hopefully without duplicate symbols) we delay rebuilding the sorted
899 * module table until we're done with the symbol table
900 * Otherwise, as we intertwine symbols' add and lookup, performance
903 module
->sortlist_valid
= TRUE
;
907 /* see comment above */
908 module
->sortlist_valid
= FALSE
;
912 /******************************************************************
913 * elf_new_public_symbols
915 * Creates a set of public symbols from an ELF symtab
917 static int elf_new_public_symbols(struct module
* module
, const struct hash_table
* symtab
)
919 struct hash_table_iter hti
;
920 struct symtab_elt
* ste
;
922 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
924 /* FIXME: we're missing the ELF entry point here */
926 hash_table_iter_init(symtab
, &hti
, NULL
);
927 while ((ste
= hash_table_iter_up(&hti
)))
929 symt_new_public(module
, ste
->compiland
, ste
->ht_elt
.name
,
931 module
->reloc_delta
+ ste
->sym
.st_value
,
937 static BOOL
elf_check_debug_link(const WCHAR
* file
, struct image_file_map
* fmap
, DWORD crc
)
940 struct elf_map_file_data emfd
;
942 emfd
.kind
= from_file
;
943 emfd
.u
.file
.filename
= file
;
944 if (!elf_map_file(&emfd
, fmap
)) return FALSE
;
945 if (!(ret
= crc
== calc_crc32(fmap
->u
.elf
.fd
)))
947 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
948 debugstr_w(file
), calc_crc32(fmap
->u
.elf
.fd
), crc
);
949 elf_unmap_file(fmap
);
954 /******************************************************************
955 * elf_locate_debug_link
957 * Locate a filename from a .gnu_debuglink section, using the same
959 * "If the full name of the directory containing the executable is
960 * execdir, and the executable has a debug link that specifies the
961 * name debugfile, then GDB will automatically search for the
962 * debugging information file in three places:
963 * - the directory containing the executable file (that is, it
964 * will look for a file named `execdir/debugfile',
965 * - a subdirectory of that directory named `.debug' (that is, the
966 * file `execdir/.debug/debugfile', and
967 * - a subdirectory of the global debug file directory that includes
968 * the executable's full path, and the name from the link (that is,
969 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
970 * is the global debug file directory, and execdir has been turned
971 * into a relative path)." (from GDB manual)
973 static BOOL
elf_locate_debug_link(struct image_file_map
* fmap
, const char* filename
,
974 const WCHAR
* loaded_file
, DWORD crc
)
976 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
977 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
978 const size_t globalDebugDirLen
= ARRAY_SIZE(globalDebugDirW
);
982 struct image_file_map
* fmap_link
= NULL
;
984 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
985 if (!fmap_link
) return FALSE
;
987 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
988 p
= HeapAlloc(GetProcessHeap(), 0,
989 (globalDebugDirLen
+ strlenW(loaded_file
) + 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
992 /* we prebuild the string with "execdir" */
993 strcpyW(p
, loaded_file
);
994 slash
= strrchrW(p
, '/');
995 if (slash
== NULL
) slash
= p
; else slash
++;
997 /* testing execdir/filename */
998 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
999 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
1001 /* testing execdir/.debug/filename */
1002 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
1003 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ ARRAY_SIZE(dotDebugW
), filename_len
);
1004 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
1006 /* testing globaldebugdir/execdir/filename */
1007 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
1008 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
1009 slash
+= globalDebugDirLen
;
1010 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
1011 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
1013 /* finally testing filename */
1014 if (elf_check_debug_link(slash
, fmap_link
, crc
)) goto found
;
1017 WARN("Couldn't locate or map %s\n", filename
);
1018 HeapFree(GetProcessHeap(), 0, p
);
1019 HeapFree(GetProcessHeap(), 0, fmap_link
);
1023 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
1024 HeapFree(GetProcessHeap(), 0, p
);
1025 fmap
->u
.elf
.alternate
= fmap_link
;
1029 /******************************************************************
1030 * elf_locate_build_id_target
1032 * Try to find the .so file containing the debug info out of the build-id note information
1034 static BOOL
elf_locate_build_id_target(struct image_file_map
* fmap
, const BYTE
* id
, unsigned idlen
)
1036 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
1037 static const WCHAR buildidW
[] = {'.','b','u','i','l','d','-','i','d','/'};
1038 static const WCHAR dotDebug0W
[] = {'.','d','e','b','u','g',0};
1039 struct image_file_map
* fmap_link
= NULL
;
1042 const BYTE
* idend
= id
+ idlen
;
1043 struct elf_map_file_data emfd
;
1045 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
1046 if (!fmap_link
) return FALSE
;
1048 p
= HeapAlloc(GetProcessHeap(), 0,
1049 sizeof(globalDebugDirW
) + sizeof(buildidW
) +
1050 (idlen
* 2 + 1) * sizeof(WCHAR
) + sizeof(dotDebug0W
));
1052 memcpy(z
, globalDebugDirW
, sizeof(globalDebugDirW
));
1053 z
+= ARRAY_SIZE(globalDebugDirW
);
1054 memcpy(z
, buildidW
, sizeof(buildidW
));
1055 z
+= ARRAY_SIZE(buildidW
);
1059 *z
++ = "0123456789abcdef"[*id
>> 4 ];
1060 *z
++ = "0123456789abcdef"[*id
& 0x0F];
1067 *z
++ = "0123456789abcdef"[*id
>> 4 ];
1068 *z
++ = "0123456789abcdef"[*id
& 0x0F];
1071 memcpy(z
, dotDebug0W
, sizeof(dotDebug0W
));
1072 TRACE("checking %s\n", wine_dbgstr_w(p
));
1074 emfd
.kind
= from_file
;
1075 emfd
.u
.file
.filename
= p
;
1076 if (elf_map_file(&emfd
, fmap_link
))
1078 struct image_section_map buildid_sect
;
1079 if (elf_find_section(fmap_link
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
1081 const uint32_t* note
;
1083 note
= (const uint32_t*)image_map_section(&buildid_sect
);
1084 if (note
!= IMAGE_NO_MAP
)
1086 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1087 if (note
[2] == NT_GNU_BUILD_ID
)
1089 if (note
[1] == idlen
&&
1090 !memcmp(note
+ 3 + ((note
[0] + 3) >> 2), idend
- idlen
, idlen
))
1092 TRACE("Located debug information file at %s\n", debugstr_w(p
));
1093 HeapFree(GetProcessHeap(), 0, p
);
1094 fmap
->u
.elf
.alternate
= fmap_link
;
1097 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p
));
1100 image_unmap_section(&buildid_sect
);
1102 elf_unmap_file(fmap_link
);
1105 TRACE("not found\n");
1106 HeapFree(GetProcessHeap(), 0, p
);
1107 HeapFree(GetProcessHeap(), 0, fmap_link
);
1111 /******************************************************************
1112 * elf_check_alternate
1114 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
1115 * or .gnu_debuglink sections.
1117 static BOOL
elf_check_alternate(struct image_file_map
* fmap
, const struct module
* module
)
1121 struct image_section_map buildid_sect
, debuglink_sect
;
1123 /* if present, add the .gnu_debuglink file as an alternate to current one */
1124 if (elf_find_section(fmap
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
1126 const uint32_t* note
;
1129 note
= (const uint32_t*)image_map_section(&buildid_sect
);
1130 if (note
!= IMAGE_NO_MAP
)
1132 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1133 if (note
[2] == NT_GNU_BUILD_ID
)
1135 ret
= elf_locate_build_id_target(fmap
, (const BYTE
*)(note
+ 3 + ((note
[0] + 3) >> 2)), note
[1]);
1138 image_unmap_section(&buildid_sect
);
1140 /* if present, add the .gnu_debuglink file as an alternate to current one */
1141 if (!ret
&& elf_find_section(fmap
, ".gnu_debuglink", SHT_NULL
, &debuglink_sect
))
1143 const char* dbg_link
;
1146 dbg_link
= (const char*)image_map_section(&debuglink_sect
);
1147 if (dbg_link
!= IMAGE_NO_MAP
)
1149 /* The content of a debug link section is:
1150 * 1/ a NULL terminated string, containing the file name for the
1152 * 2/ padding on 4 byte boundary
1153 * 3/ CRC of the linked ELF file
1155 DWORD crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
1156 ret
= elf_locate_debug_link(fmap
, dbg_link
, module
->module
.LoadedImageName
, crc
);
1158 WARN("Couldn't load linked debug file for %s\n",
1159 debugstr_w(module
->module
.ModuleName
));
1161 image_unmap_section(&debuglink_sect
);
1163 return found
? ret
: TRUE
;
1166 /******************************************************************
1167 * elf_load_debug_info_from_map
1169 * Loads the symbolic information from ELF module which mapping is described
1171 * the module has been loaded at 'load_offset' address, so symbols' address
1172 * relocation is performed.
1173 * CRC is checked if fmap->with_crc is TRUE
1175 * 0 if the file doesn't contain symbolic info (or this info cannot be
1179 static BOOL
elf_load_debug_info_from_map(struct module
* module
,
1180 struct image_file_map
* fmap
,
1182 struct hash_table
* ht_symtab
)
1184 BOOL ret
= FALSE
, lret
;
1185 struct elf_thunk_area thunks
[] =
1187 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
1188 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1189 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1190 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1191 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1192 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1196 module
->module
.SymType
= SymExport
;
1198 /* create a hash table for the symtab */
1199 elf_hash_symtab(module
, pool
, ht_symtab
, fmap
, thunks
);
1201 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1203 struct image_section_map stab_sect
, stabstr_sect
;
1205 /* check if we need an alternate file (from debuglink or build-id) */
1206 ret
= elf_check_alternate(fmap
, module
);
1208 if (elf_find_section(fmap
, ".stab", SHT_NULL
, &stab_sect
) &&
1209 elf_find_section(fmap
, ".stabstr", SHT_NULL
, &stabstr_sect
))
1212 const char* stabstr
;
1214 stab
= image_map_section(&stab_sect
);
1215 stabstr
= image_map_section(&stabstr_sect
);
1216 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
1218 /* OK, now just parse all of the stabs. */
1219 lret
= stabs_parse(module
, module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_addr
,
1220 stab
, image_get_map_size(&stab_sect
),
1221 stabstr
, image_get_map_size(&stabstr_sect
),
1224 /* and fill in the missing information for stabs */
1225 elf_finish_stabs_info(module
, ht_symtab
);
1227 WARN("Couldn't correctly read stabs\n");
1230 image_unmap_section(&stab_sect
);
1231 image_unmap_section(&stabstr_sect
);
1233 lret
= dwarf2_parse(module
, module
->reloc_delta
, thunks
, fmap
);
1236 if (strstrW(module
->module
.ModuleName
, S_ElfW
) ||
1237 !strcmpW(module
->module
.ModuleName
, S_WineLoaderW
))
1239 /* add the thunks for native libraries */
1240 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1241 elf_new_wine_thunks(module
, ht_symtab
, thunks
);
1243 /* add all the public symbols from symtab */
1244 if (elf_new_public_symbols(module
, ht_symtab
) && !ret
) ret
= TRUE
;
1249 /******************************************************************
1250 * elf_load_debug_info
1252 * Loads ELF debugging information from the module image file.
1254 BOOL
elf_load_debug_info(struct module
* module
)
1258 struct hash_table ht_symtab
;
1259 struct module_format
* modfmt
;
1261 if (module
->type
!= DMT_ELF
|| !(modfmt
= module
->format_info
[DFI_ELF
]) || !modfmt
->u
.elf_info
)
1263 ERR("Bad elf module '%s'\n", debugstr_w(module
->module
.LoadedImageName
));
1267 pool_init(&pool
, 65536);
1268 hash_table_init(&pool
, &ht_symtab
, 256);
1270 ret
= elf_load_debug_info_from_map(module
, &modfmt
->u
.elf_info
->file_map
, &pool
, &ht_symtab
);
1272 pool_destroy(&pool
);
1276 /******************************************************************
1277 * elf_fetch_file_info
1279 * Gathers some more information for an ELF module from a given file
1281 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1282 DWORD
* size
, DWORD
* checksum
)
1284 struct image_file_map fmap
;
1286 struct elf_map_file_data emfd
;
1288 emfd
.kind
= from_file
;
1289 emfd
.u
.file
.filename
= name
;
1290 if (!elf_map_file(&emfd
, &fmap
)) return FALSE
;
1291 if (base
) *base
= fmap
.u
.elf
.elf_start
;
1292 *size
= fmap
.u
.elf
.elf_size
;
1293 *checksum
= calc_crc32(fmap
.u
.elf
.fd
);
1294 elf_unmap_file(&fmap
);
1298 static BOOL
elf_load_file_from_fmap(struct process
* pcs
, const WCHAR
* filename
,
1299 struct image_file_map
* fmap
, unsigned long load_offset
,
1300 unsigned long dyn_addr
, struct elf_info
* elf_info
)
1304 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
1306 struct image_section_map ism
;
1308 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1310 char* ptr
= (char*)(ULONG_PTR
)fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1313 if (load_offset
) ptr
+= load_offset
- fmap
->u
.elf
.elf_start
;
1315 if (fmap
->addr_size
== 32)
1321 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1324 if (dyn
.d_tag
== DT_DEBUG
)
1326 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1327 if (load_offset
== 0 && dyn_addr
== 0) /* likely the case */
1328 /* Assume this module (the Wine loader) has been
1329 * loaded at its preferred address */
1330 dyn_addr
= ism
.fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1334 } while (dyn
.d_tag
!= DT_NULL
);
1335 if (dyn
.d_tag
== DT_NULL
) return ret
;
1343 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1346 if (dyn
.d_tag
== DT_DEBUG
)
1348 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1349 if (load_offset
== 0 && dyn_addr
== 0) /* likely the case */
1350 /* Assume this module (the Wine loader) has been
1351 * loaded at its preferred address */
1352 dyn_addr
= ism
.fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1356 } while (dyn
.d_tag
!= DT_NULL
);
1357 if (dyn
.d_tag
== DT_NULL
) return ret
;
1363 if (elf_info
->flags
& ELF_INFO_MODULE
)
1365 struct elf_module_info
*elf_module_info
;
1366 struct module_format
* modfmt
;
1367 struct image_section_map ism
;
1368 unsigned long modbase
= load_offset
;
1370 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1372 unsigned long rva_dyn
= elf_get_map_rva(&ism
);
1374 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1375 debugstr_w(filename
), (unsigned long)fmap
->u
.elf
.elf_start
, rva_dyn
,
1376 load_offset
, dyn_addr
);
1377 if (dyn_addr
&& load_offset
+ rva_dyn
!= dyn_addr
)
1379 WARN("\thave to relocate: %lx\n", dyn_addr
- rva_dyn
);
1380 modbase
= dyn_addr
- rva_dyn
;
1382 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename
));
1385 modfmt
= HeapAlloc(GetProcessHeap(), 0,
1386 sizeof(struct module_format
) + sizeof(struct elf_module_info
));
1387 if (!modfmt
) return FALSE
;
1388 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
, FALSE
, modbase
,
1389 fmap
->u
.elf
.elf_size
, 0, calc_crc32(fmap
->u
.elf
.fd
));
1390 if (!elf_info
->module
)
1392 HeapFree(GetProcessHeap(), 0, modfmt
);
1395 elf_info
->module
->reloc_delta
= elf_info
->module
->module
.BaseOfImage
- fmap
->u
.elf
.elf_start
;
1396 elf_module_info
= (void*)(modfmt
+ 1);
1397 elf_info
->module
->format_info
[DFI_ELF
] = modfmt
;
1398 modfmt
->module
= elf_info
->module
;
1399 modfmt
->remove
= elf_module_remove
;
1400 modfmt
->loc_compute
= NULL
;
1401 modfmt
->u
.elf_info
= elf_module_info
;
1403 elf_module_info
->elf_addr
= load_offset
;
1405 elf_module_info
->file_map
= *fmap
;
1406 elf_reset_file_map(fmap
);
1407 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
1409 elf_info
->module
->module
.SymType
= SymDeferred
;
1412 else ret
= elf_load_debug_info(elf_info
->module
);
1414 elf_module_info
->elf_mark
= 1;
1415 elf_module_info
->elf_loader
= 0;
1418 if (elf_info
->flags
& ELF_INFO_NAME
)
1421 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1) * sizeof(WCHAR
));
1424 strcpyW(ptr
, filename
);
1425 elf_info
->module_name
= ptr
;
1433 /******************************************************************
1436 * Loads the information for ELF module stored in 'filename'
1437 * the module has been loaded at 'load_offset' address
1439 * -1 if the file cannot be found/opened
1440 * 0 if the file doesn't contain symbolic info (or this info cannot be
1444 static BOOL
elf_load_file(struct process
* pcs
, const WCHAR
* filename
,
1445 unsigned long load_offset
, unsigned long dyn_addr
,
1446 struct elf_info
* elf_info
)
1449 struct image_file_map fmap
;
1450 struct elf_map_file_data emfd
;
1452 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename
), load_offset
);
1454 emfd
.kind
= from_file
;
1455 emfd
.u
.file
.filename
= filename
;
1456 if (!elf_map_file(&emfd
, &fmap
)) return ret
;
1458 /* Next, we need to find a few of the internal ELF headers within
1459 * this thing. We need the main executable header, and the section
1462 if (!fmap
.u
.elf
.elf_start
&& !load_offset
)
1463 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1464 debugstr_w(filename
));
1466 ret
= elf_load_file_from_fmap(pcs
, filename
, &fmap
, load_offset
, dyn_addr
, elf_info
);
1468 elf_unmap_file(&fmap
);
1473 /******************************************************************
1474 * elf_load_file_from_path
1475 * tries to load an ELF file from a set of paths (separated by ':')
1477 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
1478 const WCHAR
* filename
,
1479 unsigned long load_offset
,
1480 unsigned long dyn_addr
,
1482 struct elf_info
* elf_info
)
1486 WCHAR
* pathW
= NULL
;
1489 if (!path
) return FALSE
;
1491 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1492 pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1493 if (!pathW
) return FALSE
;
1494 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, pathW
, len
);
1496 for (s
= pathW
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
1498 t
= strchrW(s
, ':');
1500 fn
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1 + lstrlenW(s
) + 1) * sizeof(WCHAR
));
1503 strcatW(fn
, S_SlashW
);
1504 strcatW(fn
, filename
);
1505 ret
= elf_load_file(hProcess
, fn
, load_offset
, dyn_addr
, elf_info
);
1506 HeapFree(GetProcessHeap(), 0, fn
);
1510 HeapFree(GetProcessHeap(), 0, pathW
);
1514 /******************************************************************
1515 * elf_load_file_from_dll_path
1517 * Tries to load an ELF file from the dll path
1519 static BOOL
elf_load_file_from_dll_path(HANDLE hProcess
,
1520 const WCHAR
* filename
,
1521 unsigned long load_offset
,
1522 unsigned long dyn_addr
,
1523 struct elf_info
* elf_info
)
1526 unsigned int index
= 0;
1529 while (!ret
&& (path
= wine_dll_enum_load_path( index
++ )))
1534 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1536 name
= HeapAlloc( GetProcessHeap(), 0,
1537 (len
+ lstrlenW(filename
) + 2) * sizeof(WCHAR
) );
1540 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, name
, len
);
1541 strcatW( name
, S_SlashW
);
1542 strcatW( name
, filename
);
1543 ret
= elf_load_file(hProcess
, name
, load_offset
, dyn_addr
, elf_info
);
1544 HeapFree( GetProcessHeap(), 0, name
);
1549 #ifdef AT_SYSINFO_EHDR
1550 /******************************************************************
1553 * locate some a value from the debuggee auxiliary vector
1555 static BOOL
elf_search_auxv(const struct process
* pcs
, unsigned type
, unsigned long* val
)
1557 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1558 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1563 si
->SizeOfStruct
= sizeof(*si
);
1564 si
->MaxNameLen
= MAX_SYM_NAME
;
1565 if (!SymFromName(pcs
->handle
, "libwine.so.1!__wine_main_environ", si
) ||
1566 !(addr
= (void*)(DWORD_PTR
)si
->Address
) ||
1567 !ReadProcessMemory(pcs
->handle
, addr
, &addr
, sizeof(addr
), NULL
) ||
1570 FIXME("can't find symbol in module\n");
1573 /* walk through envp[] */
1574 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1575 str_max
= (void*)(DWORD_PTR
)~0L;
1576 while (ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) &&
1577 (addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
))) != NULL
&& str
!= NULL
)
1578 str_max
= min(str_max
, str
);
1580 /* Walk through the end of envp[] array.
1581 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1582 * deleted, the last entry is replaced by an extra NULL.
1584 while (addr
< str_max
&& ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) && str
== NULL
)
1585 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
));
1591 while (ReadProcessMemory(pcs
->handle
, addr
, &auxv
, sizeof(auxv
), NULL
) && auxv
.a_type
!= AT_NULL
)
1593 if (auxv
.a_type
== type
)
1595 *val
= auxv
.a_un
.a_val
;
1598 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(auxv
));
1605 while (ReadProcessMemory(pcs
->handle
, addr
, &auxv
, sizeof(auxv
), NULL
) && auxv
.a_type
!= AT_NULL
)
1607 if (auxv
.a_type
== type
)
1609 *val
= auxv
.a_un
.a_val
;
1612 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(auxv
));
1620 /******************************************************************
1621 * elf_search_and_load_file
1623 * lookup a file in standard ELF locations, and if found, load it
1625 static BOOL
elf_search_and_load_file(struct process
* pcs
, const WCHAR
* filename
,
1626 unsigned long load_offset
, unsigned long dyn_addr
,
1627 struct elf_info
* elf_info
)
1630 struct module
* module
;
1631 static const WCHAR S_libstdcPPW
[] = {'l','i','b','s','t','d','c','+','+','\0'};
1633 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
1634 if ((module
= module_is_already_loaded(pcs
, filename
)))
1636 elf_info
->module
= module
;
1637 elf_info
->module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1638 return module
->module
.SymType
;
1641 if (strstrW(filename
, S_libstdcPPW
)) return FALSE
; /* We know we can't do it */
1642 ret
= elf_load_file(pcs
, filename
, load_offset
, dyn_addr
, elf_info
);
1643 /* if relative pathname, try some absolute base dirs */
1644 if (!ret
&& !strchrW(filename
, '/'))
1646 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1647 getenv("PATH"), elf_info
) ||
1648 elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1649 getenv("LD_LIBRARY_PATH"), elf_info
);
1650 if (!ret
) ret
= elf_load_file_from_dll_path(pcs
, filename
,
1651 load_offset
, dyn_addr
, elf_info
);
1657 typedef BOOL (*enum_elf_modules_cb
)(const WCHAR
*, unsigned long load_addr
,
1658 unsigned long dyn_addr
, BOOL is_system
, void* user
);
1660 /******************************************************************
1661 * elf_enum_modules_internal
1663 * Enumerate ELF modules from a running process
1665 static BOOL
elf_enum_modules_internal(const struct process
* pcs
,
1666 const WCHAR
* main_name
,
1667 enum_elf_modules_cb cb
, void* user
)
1669 WCHAR bufstrW
[MAX_PATH
];
1675 struct r_debug dbg_hdr
;
1678 if (!pcs
->dbg_hdr_addr
||
1679 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1680 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1683 /* Now walk the linked list. In all known ELF implementations,
1684 * the dynamic loader maintains this linked list for us. In some
1685 * cases the first entry doesn't appear with a name, in other cases it
1688 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
1690 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1693 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
1694 lm
.l_name
!= NULL
&&
1695 ReadProcessMemory(pcs
->handle
, lm
.l_name
, bufstr
, sizeof(bufstr
), NULL
))
1697 bufstr
[sizeof(bufstr
) - 1] = '\0';
1698 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, ARRAY_SIZE(bufstrW
));
1699 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1700 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, (unsigned long)lm
.l_ld
, FALSE
, user
))
1707 struct r_debug32 dbg_hdr
;
1708 struct link_map32 lm
;
1710 if (!pcs
->dbg_hdr_addr
||
1711 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1712 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1715 /* Now walk the linked list. In all known ELF implementations,
1716 * the dynamic loader maintains this linked list for us. In some
1717 * cases the first entry doesn't appear with a name, in other cases it
1720 for (lm_addr
= (void *)(DWORD_PTR
)dbg_hdr
.r_map
; lm_addr
;
1721 lm_addr
= (void *)(DWORD_PTR
)lm
.l_next
)
1723 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1726 if (lm
.l_prev
&& /* skip first entry, normally debuggee itself */
1728 ReadProcessMemory(pcs
->handle
, (void *)(DWORD_PTR
)lm
.l_name
,
1729 bufstr
, sizeof(bufstr
), NULL
))
1731 bufstr
[sizeof(bufstr
) - 1] = '\0';
1732 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, ARRAY_SIZE(bufstrW
));
1733 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1734 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, (unsigned long)lm
.l_ld
, FALSE
, user
))
1740 #ifdef AT_SYSINFO_EHDR
1743 unsigned long ehdr_addr
;
1745 if (elf_search_auxv(pcs
, AT_SYSINFO_EHDR
, &ehdr_addr
))
1747 static const WCHAR vdsoW
[] = {'[','v','d','s','o',']','.','s','o',0};
1748 cb(vdsoW
, ehdr_addr
, 0, TRUE
, user
);
1755 /******************************************************************
1758 * Lookup in a running ELF process the loader, and sets its ELF link
1759 * address (for accessing the list of loaded .so libs) in pcs.
1760 * If flags is ELF_INFO_MODULE, the module for the loader is also
1761 * added as a module into pcs.
1763 static BOOL
elf_search_loader(struct process
* pcs
, struct elf_info
* elf_info
)
1765 WCHAR
*loader
= get_wine_loader_name(pcs
);
1766 PROCESS_BASIC_INFORMATION pbi
;
1770 if (NtQueryInformationProcess( pcs
->handle
, ProcessBasicInformation
,
1771 &pbi
, sizeof(pbi
), NULL
))
1776 PEB32
*peb32
= (PEB32
*)pbi
.PebBaseAddress
;
1779 if (!ReadProcessMemory( pcs
->handle
, &peb32
->Reserved
[0], &base32
,
1780 sizeof(base32
), NULL
))
1787 if (!ReadProcessMemory( pcs
->handle
, &pbi
.PebBaseAddress
->Reserved
[0],
1788 &base
, sizeof(base
), NULL
))
1792 ret
= elf_search_and_load_file(pcs
, loader
, base
, 0, elf_info
);
1797 /******************************************************************
1798 * elf_read_wine_loader_dbg_info
1800 * Try to find a decent wine executable which could have loaded the debuggee
1802 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1804 struct elf_info elf_info
;
1806 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
1807 if (!elf_search_loader(pcs
, &elf_info
)) return FALSE
;
1808 elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_loader
= 1;
1809 module_set_module(elf_info
.module
, S_WineLoaderW
);
1810 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
1813 struct elf_enum_user
1819 static BOOL
elf_enum_modules_translate(const WCHAR
* name
, unsigned long load_addr
,
1820 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1822 struct elf_enum_user
* eeu
= user
;
1823 return eeu
->cb(name
, load_addr
, eeu
->user
);
1826 /******************************************************************
1829 * Enumerates the ELF loaded modules from a running target (hProc)
1830 * This function doesn't require that someone has called SymInitialize
1831 * on this very process.
1833 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1836 struct elf_info elf_info
;
1838 struct elf_enum_user eeu
;
1840 memset(&pcs
, 0, sizeof(pcs
));
1842 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_NAME
;
1843 if (!elf_search_loader(&pcs
, &elf_info
)) return FALSE
;
1844 pcs
.dbg_hdr_addr
= elf_info
.dbg_hdr_addr
;
1847 ret
= elf_enum_modules_internal(&pcs
, elf_info
.module_name
, elf_enum_modules_translate
, &eeu
);
1848 HeapFree(GetProcessHeap(), 0, (char*)elf_info
.module_name
);
1854 struct process
* pcs
;
1855 struct elf_info elf_info
;
1860 /******************************************************************
1863 * Callback for elf_load_module, used to walk the list of loaded
1866 static BOOL
elf_load_cb(const WCHAR
* name
, unsigned long load_addr
,
1867 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1869 struct elf_load
* el
= user
;
1873 if (is_system
) /* virtual ELF module, created by system. handle it from memory */
1875 struct module
* module
;
1876 struct elf_map_file_data emfd
;
1877 struct image_file_map fmap
;
1879 if ((module
= module_is_already_loaded(el
->pcs
, name
)))
1881 el
->elf_info
.module
= module
;
1882 el
->elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1883 return module
->module
.SymType
;
1886 emfd
.kind
= from_process
;
1887 emfd
.u
.process
.handle
= el
->pcs
->handle
;
1888 emfd
.u
.process
.load_addr
= (void*)load_addr
;
1890 if (elf_map_file(&emfd
, &fmap
))
1891 el
->ret
= elf_load_file_from_fmap(el
->pcs
, name
, &fmap
, load_addr
, 0, &el
->elf_info
);
1896 /* memcmp is needed for matches when bufstr contains also version information
1897 * el->name: libc.so, name: libc.so.6.0
1899 p
= strrchrW(name
, '/');
1903 if (!el
->name
|| !memcmp(p
, el
->name
, lstrlenW(el
->name
) * sizeof(WCHAR
)))
1905 el
->ret
= elf_search_and_load_file(el
->pcs
, name
, load_addr
, dyn_addr
, &el
->elf_info
);
1906 if (el
->name
) ret
= FALSE
;
1912 /******************************************************************
1915 * loads an ELF module and stores it in process' module list
1916 * Also, find module real name and load address from
1917 * the real loaded modules list in pcs address space
1919 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1923 TRACE("(%p %s %08lx)\n", pcs
, debugstr_w(name
), addr
);
1925 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1928 if (pcs
->dbg_hdr_addr
) /* we're debugging a life target */
1931 /* do only the lookup from the filename, not the path (as we lookup module
1932 * name in the process' loaded module list)
1934 el
.name
= strrchrW(name
, '/');
1935 if (!el
.name
++) el
.name
= name
;
1938 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1944 el
.ret
= elf_search_and_load_file(pcs
, el
.name
, addr
, 0, &el
.elf_info
);
1946 if (!el
.ret
) return NULL
;
1947 assert(el
.elf_info
.module
);
1948 return el
.elf_info
.module
;
1951 /******************************************************************
1952 * elf_synchronize_module_list
1954 * this function rescans the debuggee module's list and synchronizes it with
1955 * the one from 'pcs', i.e.:
1956 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1957 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1959 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1961 struct module
* module
;
1964 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1966 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1967 module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 0;
1971 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1973 el
.name
= NULL
; /* fetch all modules */
1975 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1978 module
= pcs
->lmodules
;
1981 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1983 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
1985 if (!elf_info
->elf_mark
&& !elf_info
->elf_loader
)
1987 module_remove(pcs
, module
);
1988 /* restart all over */
1989 module
= pcs
->lmodules
;
1993 module
= module
->next
;
1998 #else /* !__ELF__ */
2000 BOOL
elf_find_section(struct image_file_map
* fmap
, const char* name
,
2001 unsigned sht
, struct image_section_map
* ism
)
2006 const char* elf_map_section(struct image_section_map
* ism
)
2011 void elf_unmap_section(struct image_section_map
* ism
)
2014 unsigned elf_get_map_size(const struct image_section_map
* ism
)
2019 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
2024 BOOL
elf_synchronize_module_list(struct process
* pcs
)
2029 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
2030 DWORD
* size
, DWORD
* checksum
)
2035 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
2040 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
2045 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
2050 BOOL
elf_load_debug_info(struct module
* module
)
2055 int elf_is_in_thunk_area(unsigned long addr
,
2056 const struct elf_thunk_area
* thunks
)
2060 #endif /* __ELF__ */