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"
48 #include "image_private.h"
50 #include "wine/library.h"
51 #include "wine/debug.h"
55 #define ELF_INFO_DEBUG_HEADER 0x0001
56 #define ELF_INFO_MODULE 0x0002
57 #define ELF_INFO_NAME 0x0004
59 #ifndef NT_GNU_BUILD_ID
60 #define NT_GNU_BUILD_ID 3
63 #ifndef HAVE_STRUCT_R_DEBUG
67 struct link_map
*r_map
;
77 #endif /* HAVE_STRUCT_R_DEBUG */
79 #ifndef HAVE_STRUCT_LINK_MAP
85 struct link_map
*l_next
, *l_prev
;
87 #endif /* HAVE_STRUCT_LINK_MAP */
89 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
93 unsigned flags
; /* IN one (or several) of the ELF_INFO constants */
94 DWORD_PTR dbg_hdr_addr
; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
95 struct module
* module
; /* OUT loaded module (if ELF_INFO_MODULE is set) */
96 const WCHAR
* module_name
; /* OUT found module name (if ELF_INFO_NAME is set) */
101 struct hash_table_elt ht_elt
;
103 struct symt_compiland
* compiland
;
107 struct elf_thunk_area
110 THUNK_ORDINAL ordinal
;
111 unsigned long rva_start
;
112 unsigned long rva_end
;
115 struct elf_module_info
117 unsigned long elf_addr
;
118 unsigned short elf_mark
: 1,
120 struct image_file_map file_map
;
123 /******************************************************************
126 * Maps a single section into memory from an ELF file
128 const char* elf_map_section(struct image_section_map
* ism
)
130 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
131 size_t ofst
, size
, pgsz
= sysconf( _SC_PAGESIZE
);
133 assert(ism
->fmap
->modtype
== DMT_ELF
);
134 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
||
135 fmap
->sect
[ism
->sidx
].shdr
.sh_type
== SHT_NOBITS
)
138 if (fmap
->target_copy
)
140 return fmap
->target_copy
+ fmap
->sect
[ism
->sidx
].shdr
.sh_offset
;
142 /* align required information on page size (we assume pagesize is a power of 2) */
143 ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
144 size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
145 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
146 fmap
->sect
[ism
->sidx
].mapped
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
,
148 if (fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
) return IMAGE_NO_MAP
;
149 return fmap
->sect
[ism
->sidx
].mapped
+ (fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& (pgsz
- 1));
152 /******************************************************************
155 * Finds a section by name (and type) into memory from an ELF file
156 * or its alternate if any
158 BOOL
elf_find_section(struct image_file_map
* _fmap
, const char* name
,
159 unsigned sht
, struct image_section_map
* ism
)
161 struct elf_file_map
* fmap
;
166 fmap
= &_fmap
->u
.elf
;
167 if (fmap
->shstrtab
== IMAGE_NO_MAP
)
169 struct image_section_map hdr_ism
= {_fmap
, fmap
->elfhdr
.e_shstrndx
};
170 if ((fmap
->shstrtab
= elf_map_section(&hdr_ism
)) == IMAGE_NO_MAP
) break;
172 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
174 if (strcmp(fmap
->shstrtab
+ fmap
->sect
[i
].shdr
.sh_name
, name
) == 0 &&
175 (sht
== SHT_NULL
|| sht
== fmap
->sect
[i
].shdr
.sh_type
))
182 _fmap
= fmap
->alternate
;
189 /******************************************************************
192 * Unmaps a single section from memory
194 void elf_unmap_section(struct image_section_map
* ism
)
196 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
198 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->elfhdr
.e_shnum
&& !fmap
->target_copy
&&
199 fmap
->sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
201 size_t pgsz
= sysconf( _SC_PAGESIZE
);
202 size_t ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
203 size_t size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
204 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
205 if (munmap((char*)fmap
->sect
[ism
->sidx
].mapped
, size
) < 0)
206 WARN("Couldn't unmap the section\n");
207 fmap
->sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
211 static void elf_end_find(struct image_file_map
* fmap
)
213 struct image_section_map ism
;
218 ism
.sidx
= fmap
->u
.elf
.elfhdr
.e_shstrndx
;
219 elf_unmap_section(&ism
);
220 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
221 fmap
= fmap
->u
.elf
.alternate
;
225 /******************************************************************
228 * Get the RVA of an ELF section
230 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
232 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
234 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_addr
- ism
->fmap
->u
.elf
.elf_start
;
237 /******************************************************************
240 * Get the size of an ELF section
242 unsigned elf_get_map_size(const struct image_section_map
* ism
)
244 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
246 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_size
;
249 static inline void elf_reset_file_map(struct image_file_map
* fmap
)
252 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
253 fmap
->u
.elf
.alternate
= NULL
;
254 fmap
->u
.elf
.target_copy
= NULL
;
257 struct elf_map_file_data
259 enum {from_file
, from_process
} kind
;
264 const WCHAR
* filename
;
274 static BOOL
elf_map_file_read(struct image_file_map
* fmap
, struct elf_map_file_data
* emfd
,
275 void* buf
, size_t len
, off_t off
)
282 return pread(fmap
->u
.elf
.fd
, buf
, len
, off
) == len
;
284 return ReadProcessMemory(emfd
->u
.process
.handle
,
285 (void*)((unsigned long)emfd
->u
.process
.load_addr
+ (unsigned long)off
),
286 buf
, len
, &dw
) && dw
== len
;
293 /******************************************************************
296 * Maps an ELF file into memory (and checks it's a real ELF file)
298 static BOOL
elf_map_file(struct elf_map_file_data
* emfd
, struct image_file_map
* fmap
)
300 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
304 size_t tmp
, page_mask
= sysconf( _SC_PAGESIZE
) - 1;
312 len
= WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, NULL
, 0, NULL
, NULL
);
313 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
314 WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, filename
, len
, NULL
, NULL
);
323 elf_reset_file_map(fmap
);
325 fmap
->modtype
= DMT_ELF
;
327 fmap
->u
.elf
.target_copy
= NULL
;
332 /* check that the file exists, and that the module hasn't been loaded yet */
333 if (stat(filename
, &statbuf
) == -1 || S_ISDIR(statbuf
.st_mode
)) goto done
;
335 /* Now open the file, so that we can mmap() it. */
336 if ((fmap
->u
.elf
.fd
= open(filename
, O_RDONLY
)) == -1) goto done
;
341 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.elfhdr
, sizeof(fmap
->u
.elf
.elfhdr
), 0))
344 /* and check for an ELF header */
345 if (memcmp(fmap
->u
.elf
.elfhdr
.e_ident
,
346 elf_signature
, sizeof(elf_signature
))) goto done
;
347 /* and check 32 vs 64 size according to current machine */
349 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) goto done
;
351 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) goto done
;
353 fmap
->addr_size
= fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
354 fmap
->u
.elf
.sect
= HeapAlloc(GetProcessHeap(), 0,
355 fmap
->u
.elf
.elfhdr
.e_shnum
* sizeof(fmap
->u
.elf
.sect
[0]));
356 if (!fmap
->u
.elf
.sect
) goto done
;
358 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_shnum
; i
++)
360 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.sect
[i
].shdr
, sizeof(fmap
->u
.elf
.sect
[i
].shdr
),
361 fmap
->u
.elf
.elfhdr
.e_shoff
+ i
* sizeof(fmap
->u
.elf
.sect
[i
].shdr
)))
363 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
364 fmap
->u
.elf
.sect
= NULL
;
367 fmap
->u
.elf
.sect
[i
].mapped
= IMAGE_NO_MAP
;
370 /* grab size of module once loaded in memory */
371 fmap
->u
.elf
.elf_size
= 0;
372 fmap
->u
.elf
.elf_start
= ~0L;
373 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_phnum
; i
++)
375 if (elf_map_file_read(fmap
, emfd
, &phdr
, sizeof(phdr
),
376 fmap
->u
.elf
.elfhdr
.e_phoff
+ i
* sizeof(phdr
)) &&
377 phdr
.p_type
== PT_LOAD
)
379 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
380 if (fmap
->u
.elf
.elf_size
< tmp
) fmap
->u
.elf
.elf_size
= tmp
;
381 if (phdr
.p_vaddr
< fmap
->u
.elf
.elf_start
) fmap
->u
.elf
.elf_start
= phdr
.p_vaddr
;
384 /* if non relocatable ELF, then remove fixed address from computation
385 * otherwise, all addresses are zero based and start has no effect
387 fmap
->u
.elf
.elf_size
-= fmap
->u
.elf
.elf_start
;
391 case from_file
: break;
393 if (!(fmap
->u
.elf
.target_copy
= HeapAlloc(GetProcessHeap(), 0, fmap
->u
.elf
.elf_size
)))
395 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
398 if (!ReadProcessMemory(emfd
->u
.process
.handle
, emfd
->u
.process
.load_addr
, fmap
->u
.elf
.target_copy
,
399 fmap
->u
.elf
.elf_size
, NULL
))
401 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
402 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
409 HeapFree(GetProcessHeap(), 0, filename
);
413 /******************************************************************
416 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
418 static void elf_unmap_file(struct image_file_map
* fmap
)
422 if (fmap
->u
.elf
.fd
!= -1)
424 struct image_section_map ism
;
426 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.elf
.elfhdr
.e_shnum
; ism
.sidx
++)
428 elf_unmap_section(&ism
);
430 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
431 close(fmap
->u
.elf
.fd
);
433 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
434 fmap
= fmap
->u
.elf
.alternate
;
438 static void elf_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
440 elf_unmap_file(&modfmt
->u
.elf_info
->file_map
);
441 HeapFree(GetProcessHeap(), 0, modfmt
);
444 /******************************************************************
445 * elf_is_in_thunk_area
447 * Check whether an address lies within one of the thunk area we
450 int elf_is_in_thunk_area(unsigned long addr
,
451 const struct elf_thunk_area
* thunks
)
455 if (thunks
) for (i
= 0; thunks
[i
].symname
; i
++)
457 if (addr
>= thunks
[i
].rva_start
&& addr
< thunks
[i
].rva_end
)
463 /******************************************************************
466 * creating an internal hash table to ease use ELF symtab information lookup
468 static void elf_hash_symtab(struct module
* module
, struct pool
* pool
,
469 struct hash_table
* ht_symtab
, struct image_file_map
* fmap
,
470 struct elf_thunk_area
* thunks
)
475 struct symt_compiland
* compiland
= NULL
;
478 struct symtab_elt
* ste
;
479 struct image_section_map ism
, ism_str
;
481 if (!elf_find_section(fmap
, ".symtab", SHT_SYMTAB
, &ism
) &&
482 !elf_find_section(fmap
, ".dynsym", SHT_DYNSYM
, &ism
)) return;
483 if ((symp
= (const Elf_Sym
*)image_map_section(&ism
)) == IMAGE_NO_MAP
) return;
484 ism_str
.fmap
= ism
.fmap
;
485 ism_str
.sidx
= fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_link
;
486 if ((strp
= image_map_section(&ism_str
)) == IMAGE_NO_MAP
)
488 image_unmap_section(&ism
);
492 nsym
= image_get_map_size(&ism
) / sizeof(*symp
);
494 for (j
= 0; thunks
[j
].symname
; j
++)
495 thunks
[j
].rva_start
= thunks
[j
].rva_end
= 0;
497 for (i
= 0; i
< nsym
; i
++, symp
++)
499 /* Ignore certain types of entries which really aren't of that much
502 if ((ELF32_ST_TYPE(symp
->st_info
) != STT_NOTYPE
&&
503 ELF32_ST_TYPE(symp
->st_info
) != STT_FILE
&&
504 ELF32_ST_TYPE(symp
->st_info
) != STT_OBJECT
&&
505 ELF32_ST_TYPE(symp
->st_info
) != STT_FUNC
) ||
506 symp
->st_shndx
== SHN_UNDEF
)
511 symname
= strp
+ symp
->st_name
;
513 /* handle some specific symtab (that we'll throw away when done) */
514 switch (ELF32_ST_TYPE(symp
->st_info
))
518 compiland
= symt_new_compiland(module
, symp
->st_value
,
519 source_new(module
, NULL
, symname
));
524 /* we are only interested in wine markers inserted by winebuild */
525 for (j
= 0; thunks
[j
].symname
; j
++)
527 if (!strcmp(symname
, thunks
[j
].symname
))
529 thunks
[j
].rva_start
= symp
->st_value
;
530 thunks
[j
].rva_end
= symp
->st_value
+ symp
->st_size
;
537 /* FIXME: we don't need to handle them (GCC internals)
538 * Moreover, they screw up our symbol lookup :-/
540 if (symname
[0] == '.' && symname
[1] == 'L' && isdigit(symname
[2]))
543 ste
= pool_alloc(pool
, sizeof(*ste
));
544 ste
->ht_elt
.name
= symname
;
545 /* GCC emits, in some cases, a .<digit>+ suffix.
546 * This is used for static variable inside functions, so
547 * that we can have several such variables with same name in
548 * the same compilation unit
549 * We simply ignore that suffix when present (we also get rid
550 * of it in stabs parsing)
552 ptr
= symname
+ strlen(symname
) - 1;
555 while (isdigit(*ptr
) && ptr
>= symname
) ptr
--;
556 if (ptr
> symname
&& *ptr
== '.')
558 char* n
= pool_alloc(pool
, ptr
- symname
+ 1);
559 memcpy(n
, symname
, ptr
- symname
+ 1);
560 n
[ptr
- symname
] = '\0';
561 ste
->ht_elt
.name
= n
;
565 ste
->compiland
= compiland
;
567 hash_table_add(ht_symtab
, &ste
->ht_elt
);
569 /* as we added in the ht_symtab pointers to the symbols themselves,
570 * we cannot unmap yet the sections, it will be done when we're over
575 /******************************************************************
578 * lookup a symbol by name in our internal hash table for the symtab
580 static const Elf_Sym
* elf_lookup_symtab(const struct module
* module
,
581 const struct hash_table
* ht_symtab
,
582 const char* name
, const struct symt
* compiland
)
584 struct symtab_elt
* weak_result
= NULL
; /* without compiland name */
585 struct symtab_elt
* result
= NULL
;
586 struct hash_table_iter hti
;
587 struct symtab_elt
* ste
;
588 const char* compiland_name
;
589 const char* compiland_basename
;
592 /* we need weak match up (at least) when symbols of same name,
593 * defined several times in different compilation units,
594 * are merged in a single one (hence a different filename for c.u.)
598 compiland_name
= source_get(module
,
599 ((const struct symt_compiland
*)compiland
)->source
);
600 compiland_basename
= strrchr(compiland_name
, '/');
601 if (!compiland_basename
++) compiland_basename
= compiland_name
;
603 else compiland_name
= compiland_basename
= NULL
;
605 hash_table_iter_init(ht_symtab
, &hti
, name
);
606 while ((ste
= hash_table_iter_up(&hti
)))
608 if (ste
->used
|| strcmp(ste
->ht_elt
.name
, name
)) continue;
611 if ((ste
->compiland
&& !compiland_name
) || (!ste
->compiland
&& compiland_name
))
613 if (ste
->compiland
&& compiland_name
)
615 const char* filename
= source_get(module
, ste
->compiland
->source
);
616 if (strcmp(filename
, compiland_name
))
618 base
= strrchr(filename
, '/');
619 if (!base
++) base
= filename
;
620 if (strcmp(base
, compiland_basename
)) continue;
625 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
626 name
, compiland_name
,
627 source_get(module
, result
->compiland
->source
), (unsigned int)result
->symp
->st_value
,
628 source_get(module
, ste
->compiland
->source
), (unsigned int)ste
->symp
->st_value
);
636 if (!result
&& !(result
= weak_result
))
638 FIXME("Couldn't find symbol %s!%s in symtab\n",
639 debugstr_w(module
->module
.ModuleName
), name
);
645 /******************************************************************
646 * elf_finish_stabs_info
648 * - get any relevant information (address & size) from the bits we got from the
649 * stabs debugging information
651 static void elf_finish_stabs_info(struct module
* module
, const struct hash_table
* symtab
)
653 struct hash_table_iter hti
;
657 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
659 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
660 while ((ptr
= hash_table_iter_up(&hti
)))
662 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
663 switch (sym
->symt
.tag
)
666 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
667 ((struct symt_function
*)sym
)->size
)
671 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
672 ((struct symt_function
*)sym
)->container
);
675 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
676 ((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
+ symp
->st_value
)
677 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
678 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
679 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
680 if (((struct symt_function
*)sym
)->size
&& ((struct symt_function
*)sym
)->size
!= symp
->st_size
)
681 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
682 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
683 ((struct symt_function
*)sym
)->size
, (unsigned int)symp
->st_size
);
685 ((struct symt_function
*)sym
)->address
= elf_info
->elf_addr
+ symp
->st_value
;
686 ((struct symt_function
*)sym
)->size
= symp
->st_size
;
688 FIXME("Couldn't find %s!%s\n",
689 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
692 switch (((struct symt_data
*)sym
)->kind
)
695 case DataIsFileStatic
:
696 if (((struct symt_data
*)sym
)->u
.var
.kind
!= loc_absolute
||
697 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
)
699 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
700 ((struct symt_data
*)sym
)->container
);
703 if (((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
&&
704 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
+ symp
->st_value
)
705 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
706 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
707 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
708 ((struct symt_data
*)sym
)->u
.var
.offset
= elf_info
->elf_addr
+ symp
->st_value
;
709 ((struct symt_data
*)sym
)->kind
= (ELF32_ST_BIND(symp
->st_info
) == STB_LOCAL
) ?
710 DataIsFileStatic
: DataIsGlobal
;
712 FIXME("Couldn't find %s!%s\n",
713 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
719 FIXME("Unsupported tag %u\n", sym
->symt
.tag
);
723 /* since we may have changed some addresses & sizes, mark the module to be resorted */
724 module
->sortlist_valid
= FALSE
;
727 /******************************************************************
728 * elf_load_wine_thunks
730 * creating the thunk objects for a wine native DLL
732 static int elf_new_wine_thunks(struct module
* module
, const struct hash_table
* ht_symtab
,
733 const struct elf_thunk_area
* thunks
)
736 struct hash_table_iter hti
;
737 struct symtab_elt
* ste
;
739 struct symt_ht
* symt
;
741 hash_table_iter_init(ht_symtab
, &hti
, NULL
);
742 while ((ste
= hash_table_iter_up(&hti
)))
744 if (ste
->used
) continue;
746 addr
= module
->reloc_delta
+ ste
->symp
->st_value
;
748 j
= elf_is_in_thunk_area(ste
->symp
->st_value
, thunks
);
749 if (j
>= 0) /* thunk found */
751 symt_new_thunk(module
, ste
->compiland
, ste
->ht_elt
.name
, thunks
[j
].ordinal
,
752 addr
, ste
->symp
->st_size
);
759 symt
= symt_find_nearest(module
, addr
);
760 if (symt
&& !symt_get_address(&symt
->symt
, &ref_addr
))
762 if (!symt
|| addr
!= ref_addr
)
764 /* creating public symbols for all the ELF symbols which haven't been
765 * used yet (ie we have no debug information on them)
766 * That's the case, for example, of the .spec.c files
768 switch (ELF32_ST_TYPE(ste
->symp
->st_info
))
771 symt_new_function(module
, ste
->compiland
, ste
->ht_elt
.name
,
772 addr
, ste
->symp
->st_size
, NULL
);
775 loc
.kind
= loc_absolute
;
778 symt_new_global_variable(module
, ste
->compiland
, ste
->ht_elt
.name
,
779 ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
,
780 loc
, ste
->symp
->st_size
, NULL
);
783 FIXME("Shouldn't happen\n");
786 /* FIXME: this is a hack !!!
787 * we are adding new symbols, but as we're parsing a symbol table
788 * (hopefully without duplicate symbols) we delay rebuilding the sorted
789 * module table until we're done with the symbol table
790 * Otherwise, as we intertwine symbols' add and lookup, performance
793 module
->sortlist_valid
= TRUE
;
797 /* see comment above */
798 module
->sortlist_valid
= FALSE
;
802 /******************************************************************
803 * elf_new_public_symbols
805 * Creates a set of public symbols from an ELF symtab
807 static int elf_new_public_symbols(struct module
* module
, const struct hash_table
* symtab
)
809 struct hash_table_iter hti
;
810 struct symtab_elt
* ste
;
812 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
814 /* FIXME: we're missing the ELF entry point here */
816 hash_table_iter_init(symtab
, &hti
, NULL
);
817 while ((ste
= hash_table_iter_up(&hti
)))
819 symt_new_public(module
, ste
->compiland
, ste
->ht_elt
.name
,
820 module
->reloc_delta
+ ste
->symp
->st_value
,
826 static BOOL
elf_check_debug_link(const WCHAR
* file
, struct image_file_map
* fmap
, DWORD crc
)
829 struct elf_map_file_data emfd
;
831 emfd
.kind
= from_file
;
832 emfd
.u
.file
.filename
= file
;
833 if (!elf_map_file(&emfd
, fmap
)) return FALSE
;
834 if (!(ret
= crc
== calc_crc32(fmap
->u
.elf
.fd
)))
836 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
837 debugstr_w(file
), calc_crc32(fmap
->u
.elf
.fd
), crc
);
838 elf_unmap_file(fmap
);
843 /******************************************************************
844 * elf_locate_debug_link
846 * Locate a filename from a .gnu_debuglink section, using the same
848 * "If the full name of the directory containing the executable is
849 * execdir, and the executable has a debug link that specifies the
850 * name debugfile, then GDB will automatically search for the
851 * debugging information file in three places:
852 * - the directory containing the executable file (that is, it
853 * will look for a file named `execdir/debugfile',
854 * - a subdirectory of that directory named `.debug' (that is, the
855 * file `execdir/.debug/debugfile', and
856 * - a subdirectory of the global debug file directory that includes
857 * the executable's full path, and the name from the link (that is,
858 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
859 * is the global debug file directory, and execdir has been turned
860 * into a relative path)." (from GDB manual)
862 static BOOL
elf_locate_debug_link(struct image_file_map
* fmap
, const char* filename
,
863 const WCHAR
* loaded_file
, DWORD crc
)
865 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
866 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
867 const size_t globalDebugDirLen
= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
871 struct image_file_map
* fmap_link
= NULL
;
873 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
874 if (!fmap_link
) return FALSE
;
876 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
877 p
= HeapAlloc(GetProcessHeap(), 0,
878 (globalDebugDirLen
+ strlenW(loaded_file
) + 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
881 /* we prebuild the string with "execdir" */
882 strcpyW(p
, loaded_file
);
883 slash
= strrchrW(p
, '/');
884 if (slash
== NULL
) slash
= p
; else slash
++;
886 /* testing execdir/filename */
887 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
888 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
890 /* testing execdir/.debug/filename */
891 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
892 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ sizeof(dotDebugW
) / sizeof(WCHAR
), filename_len
);
893 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
895 /* testing globaldebugdir/execdir/filename */
896 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
897 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
898 slash
+= globalDebugDirLen
;
899 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
900 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
902 /* finally testing filename */
903 if (elf_check_debug_link(slash
, fmap_link
, crc
)) goto found
;
906 WARN("Couldn't locate or map %s\n", filename
);
907 HeapFree(GetProcessHeap(), 0, p
);
908 HeapFree(GetProcessHeap(), 0, fmap_link
);
912 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
913 HeapFree(GetProcessHeap(), 0, p
);
914 fmap
->u
.elf
.alternate
= fmap_link
;
918 /******************************************************************
919 * elf_locate_build_id_target
921 * Try to find the .so file containing the debug info out of the build-id note information
923 static BOOL
elf_locate_build_id_target(struct image_file_map
* fmap
, const BYTE
* id
, unsigned idlen
)
925 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
926 static const WCHAR buildidW
[] = {'.','b','u','i','l','d','-','i','d','/'};
927 static const WCHAR dotDebug0W
[] = {'.','d','e','b','u','g',0};
928 struct image_file_map
* fmap_link
= NULL
;
931 const BYTE
* idend
= id
+ idlen
;
932 struct elf_map_file_data emfd
;
934 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
935 if (!fmap_link
) return FALSE
;
937 p
= HeapAlloc(GetProcessHeap(), 0,
938 sizeof(globalDebugDirW
) + sizeof(buildidW
) +
939 (idlen
* 2 + 1) * sizeof(WCHAR
) + sizeof(dotDebug0W
));
941 memcpy(z
, globalDebugDirW
, sizeof(globalDebugDirW
));
942 z
+= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
943 memcpy(z
, buildidW
, sizeof(buildidW
));
944 z
+= sizeof(buildidW
) / sizeof(WCHAR
);
948 *z
++ = "0123456789abcdef"[*id
>> 4 ];
949 *z
++ = "0123456789abcdef"[*id
& 0x0F];
956 *z
++ = "0123456789abcdef"[*id
>> 4 ];
957 *z
++ = "0123456789abcdef"[*id
& 0x0F];
960 memcpy(z
, dotDebug0W
, sizeof(dotDebug0W
));
961 TRACE("checking %s\n", wine_dbgstr_w(p
));
963 emfd
.kind
= from_file
;
964 emfd
.u
.file
.filename
= p
;
965 if (elf_map_file(&emfd
, fmap_link
))
967 struct image_section_map buildid_sect
;
968 if (elf_find_section(fmap_link
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
970 const uint32_t* note
;
972 note
= (const uint32_t*)image_map_section(&buildid_sect
);
973 if (note
!= IMAGE_NO_MAP
)
975 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
976 if (note
[2] == NT_GNU_BUILD_ID
)
978 if (note
[1] == idlen
&&
979 !memcmp(note
+ 3 + ((note
[0] + 3) >> 2), idend
- idlen
, idlen
))
981 TRACE("Located debug information file at %s\n", debugstr_w(p
));
982 HeapFree(GetProcessHeap(), 0, p
);
983 fmap
->u
.elf
.alternate
= fmap_link
;
986 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(p
));
989 image_unmap_section(&buildid_sect
);
991 elf_unmap_file(fmap_link
);
994 TRACE("not found\n");
995 HeapFree(GetProcessHeap(), 0, p
);
996 HeapFree(GetProcessHeap(), 0, fmap_link
);
1000 /******************************************************************
1001 * elf_check_alternate
1003 * Load alternate files for a given ELF file, looking at either .note.gnu_build-id
1004 * or .gnu_debuglink sections.
1006 static BOOL
elf_check_alternate(struct image_file_map
* fmap
, const struct module
* module
)
1010 struct image_section_map buildid_sect
, debuglink_sect
;
1012 /* if present, add the .gnu_debuglink file as an alternate to current one */
1013 if (elf_find_section(fmap
, ".note.gnu.build-id", SHT_NULL
, &buildid_sect
))
1015 const uint32_t* note
;
1018 note
= (const uint32_t*)image_map_section(&buildid_sect
);
1019 if (note
!= IMAGE_NO_MAP
)
1021 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
1022 if (note
[2] == NT_GNU_BUILD_ID
)
1024 ret
= elf_locate_build_id_target(fmap
, (const BYTE
*)(note
+ 3 + ((note
[0] + 3) >> 2)), note
[1]);
1027 image_unmap_section(&buildid_sect
);
1029 /* if present, add the .gnu_debuglink file as an alternate to current one */
1030 if (!ret
&& elf_find_section(fmap
, ".gnu_debuglink", SHT_NULL
, &debuglink_sect
))
1032 const char* dbg_link
;
1035 dbg_link
= (const char*)image_map_section(&debuglink_sect
);
1036 if (dbg_link
!= IMAGE_NO_MAP
)
1038 /* The content of a debug link section is:
1039 * 1/ a NULL terminated string, containing the file name for the
1041 * 2/ padding on 4 byte boundary
1042 * 3/ CRC of the linked ELF file
1044 DWORD crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
1045 ret
= elf_locate_debug_link(fmap
, dbg_link
, module
->module
.LoadedImageName
, crc
);
1047 WARN("Couldn't load linked debug file for %s\n",
1048 debugstr_w(module
->module
.ModuleName
));
1050 image_unmap_section(&debuglink_sect
);
1052 return found
? ret
: TRUE
;
1055 /******************************************************************
1056 * elf_load_debug_info_from_map
1058 * Loads the symbolic information from ELF module which mapping is described
1060 * the module has been loaded at 'load_offset' address, so symbols' address
1061 * relocation is performed.
1062 * CRC is checked if fmap->with_crc is TRUE
1064 * 0 if the file doesn't contain symbolic info (or this info cannot be
1068 static BOOL
elf_load_debug_info_from_map(struct module
* module
,
1069 struct image_file_map
* fmap
,
1071 struct hash_table
* ht_symtab
)
1073 BOOL ret
= FALSE
, lret
;
1074 struct elf_thunk_area thunks
[] =
1076 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
1077 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1078 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1079 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
1080 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
1081 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
1085 module
->module
.SymType
= SymExport
;
1087 /* create a hash table for the symtab */
1088 elf_hash_symtab(module
, pool
, ht_symtab
, fmap
, thunks
);
1090 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1092 struct image_section_map stab_sect
, stabstr_sect
;
1094 /* check if we need an alternate file (from debuglink or build-id) */
1095 ret
= elf_check_alternate(fmap
, module
);
1097 if (elf_find_section(fmap
, ".stab", SHT_NULL
, &stab_sect
) &&
1098 elf_find_section(fmap
, ".stabstr", SHT_NULL
, &stabstr_sect
))
1101 const char* stabstr
;
1103 stab
= image_map_section(&stab_sect
);
1104 stabstr
= image_map_section(&stabstr_sect
);
1105 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
1107 /* OK, now just parse all of the stabs. */
1108 lret
= stabs_parse(module
, module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_addr
,
1109 stab
, image_get_map_size(&stab_sect
),
1110 stabstr
, image_get_map_size(&stabstr_sect
),
1113 /* and fill in the missing information for stabs */
1114 elf_finish_stabs_info(module
, ht_symtab
);
1116 WARN("Couldn't correctly read stabs\n");
1119 image_unmap_section(&stab_sect
);
1120 image_unmap_section(&stabstr_sect
);
1122 lret
= dwarf2_parse(module
, module
->reloc_delta
, thunks
, fmap
);
1125 if (strstrW(module
->module
.ModuleName
, S_ElfW
) ||
1126 !strcmpW(module
->module
.ModuleName
, S_WineLoaderW
))
1128 /* add the thunks for native libraries */
1129 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1130 elf_new_wine_thunks(module
, ht_symtab
, thunks
);
1132 /* add all the public symbols from symtab */
1133 if (elf_new_public_symbols(module
, ht_symtab
) && !ret
) ret
= TRUE
;
1138 /******************************************************************
1139 * elf_load_debug_info
1141 * Loads ELF debugging information from the module image file.
1143 BOOL
elf_load_debug_info(struct module
* module
)
1147 struct hash_table ht_symtab
;
1148 struct module_format
* modfmt
;
1150 if (module
->type
!= DMT_ELF
|| !(modfmt
= module
->format_info
[DFI_ELF
]) || !modfmt
->u
.elf_info
)
1152 ERR("Bad elf module '%s'\n", debugstr_w(module
->module
.LoadedImageName
));
1156 pool_init(&pool
, 65536);
1157 hash_table_init(&pool
, &ht_symtab
, 256);
1159 ret
= elf_load_debug_info_from_map(module
, &modfmt
->u
.elf_info
->file_map
, &pool
, &ht_symtab
);
1161 pool_destroy(&pool
);
1165 /******************************************************************
1166 * elf_fetch_file_info
1168 * Gathers some more information for an ELF module from a given file
1170 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1171 DWORD
* size
, DWORD
* checksum
)
1173 struct image_file_map fmap
;
1175 struct elf_map_file_data emfd
;
1177 emfd
.kind
= from_file
;
1178 emfd
.u
.file
.filename
= name
;
1179 if (!elf_map_file(&emfd
, &fmap
)) return FALSE
;
1180 if (base
) *base
= fmap
.u
.elf
.elf_start
;
1181 *size
= fmap
.u
.elf
.elf_size
;
1182 *checksum
= calc_crc32(fmap
.u
.elf
.fd
);
1183 elf_unmap_file(&fmap
);
1187 static BOOL
elf_load_file_from_fmap(struct process
* pcs
, const WCHAR
* filename
,
1188 struct image_file_map
* fmap
, unsigned long load_offset
,
1189 unsigned long dyn_addr
, struct elf_info
* elf_info
)
1193 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
1195 struct image_section_map ism
;
1197 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1200 char* ptr
= (char*)fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1205 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1208 if (dyn
.d_tag
== DT_DEBUG
)
1210 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1211 if (load_offset
== 0 && dyn_addr
== 0) /* likely the case */
1212 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1213 dyn_addr
= ism
.fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1217 } while (dyn
.d_tag
!= DT_NULL
);
1218 if (dyn
.d_tag
== DT_NULL
) return ret
;
1223 if (elf_info
->flags
& ELF_INFO_MODULE
)
1225 struct elf_module_info
*elf_module_info
;
1226 struct module_format
* modfmt
;
1227 struct image_section_map ism
;
1228 unsigned long modbase
= load_offset
;
1230 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1232 unsigned long rva_dyn
= elf_get_map_rva(&ism
);
1234 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1235 debugstr_w(filename
), (unsigned long)fmap
->u
.elf
.elf_start
, rva_dyn
,
1236 load_offset
, dyn_addr
);
1237 if (dyn_addr
&& load_offset
+ rva_dyn
!= dyn_addr
)
1239 WARN("\thave to relocate: %lx\n", dyn_addr
- rva_dyn
);
1240 modbase
= dyn_addr
- rva_dyn
;
1242 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename
));
1245 modfmt
= HeapAlloc(GetProcessHeap(), 0,
1246 sizeof(struct module_format
) + sizeof(struct elf_module_info
));
1247 if (!modfmt
) return FALSE
;
1248 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
, FALSE
, modbase
,
1249 fmap
->u
.elf
.elf_size
, 0, calc_crc32(fmap
->u
.elf
.fd
));
1250 if (!elf_info
->module
)
1252 HeapFree(GetProcessHeap(), 0, modfmt
);
1255 elf_info
->module
->reloc_delta
= elf_info
->module
->module
.BaseOfImage
- fmap
->u
.elf
.elf_start
;
1256 elf_module_info
= (void*)(modfmt
+ 1);
1257 elf_info
->module
->format_info
[DFI_ELF
] = modfmt
;
1258 modfmt
->module
= elf_info
->module
;
1259 modfmt
->remove
= elf_module_remove
;
1260 modfmt
->loc_compute
= NULL
;
1261 modfmt
->u
.elf_info
= elf_module_info
;
1263 elf_module_info
->elf_addr
= load_offset
;
1265 elf_module_info
->file_map
= *fmap
;
1266 elf_reset_file_map(fmap
);
1267 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
1269 elf_info
->module
->module
.SymType
= SymDeferred
;
1272 else ret
= elf_load_debug_info(elf_info
->module
);
1274 elf_module_info
->elf_mark
= 1;
1275 elf_module_info
->elf_loader
= 0;
1278 if (elf_info
->flags
& ELF_INFO_NAME
)
1281 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1) * sizeof(WCHAR
));
1284 strcpyW(ptr
, filename
);
1285 elf_info
->module_name
= ptr
;
1293 /******************************************************************
1296 * Loads the information for ELF module stored in 'filename'
1297 * the module has been loaded at 'load_offset' address
1299 * -1 if the file cannot be found/opened
1300 * 0 if the file doesn't contain symbolic info (or this info cannot be
1304 static BOOL
elf_load_file(struct process
* pcs
, const WCHAR
* filename
,
1305 unsigned long load_offset
, unsigned long dyn_addr
,
1306 struct elf_info
* elf_info
)
1309 struct image_file_map fmap
;
1310 struct elf_map_file_data emfd
;
1312 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename
), load_offset
);
1314 emfd
.kind
= from_file
;
1315 emfd
.u
.file
.filename
= filename
;
1316 if (!elf_map_file(&emfd
, &fmap
)) return ret
;
1318 /* Next, we need to find a few of the internal ELF headers within
1319 * this thing. We need the main executable header, and the section
1322 if (!fmap
.u
.elf
.elf_start
&& !load_offset
)
1323 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1324 debugstr_w(filename
));
1326 ret
= elf_load_file_from_fmap(pcs
, filename
, &fmap
, load_offset
, dyn_addr
, elf_info
);
1328 elf_unmap_file(&fmap
);
1333 /******************************************************************
1334 * elf_load_file_from_path
1335 * tries to load an ELF file from a set of paths (separated by ':')
1337 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
1338 const WCHAR
* filename
,
1339 unsigned long load_offset
,
1340 unsigned long dyn_addr
,
1342 struct elf_info
* elf_info
)
1346 WCHAR
* pathW
= NULL
;
1349 if (!path
) return FALSE
;
1351 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1352 pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1353 if (!pathW
) return FALSE
;
1354 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, pathW
, len
);
1356 for (s
= pathW
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
1358 t
= strchrW(s
, ':');
1360 fn
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1 + lstrlenW(s
) + 1) * sizeof(WCHAR
));
1363 strcatW(fn
, S_SlashW
);
1364 strcatW(fn
, filename
);
1365 ret
= elf_load_file(hProcess
, fn
, load_offset
, dyn_addr
, elf_info
);
1366 HeapFree(GetProcessHeap(), 0, fn
);
1370 HeapFree(GetProcessHeap(), 0, pathW
);
1374 /******************************************************************
1375 * elf_load_file_from_dll_path
1377 * Tries to load an ELF file from the dll path
1379 static BOOL
elf_load_file_from_dll_path(HANDLE hProcess
,
1380 const WCHAR
* filename
,
1381 unsigned long load_offset
,
1382 unsigned long dyn_addr
,
1383 struct elf_info
* elf_info
)
1386 unsigned int index
= 0;
1389 while (!ret
&& (path
= wine_dll_enum_load_path( index
++ )))
1394 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1396 name
= HeapAlloc( GetProcessHeap(), 0,
1397 (len
+ lstrlenW(filename
) + 2) * sizeof(WCHAR
) );
1400 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, name
, len
);
1401 strcatW( name
, S_SlashW
);
1402 strcatW( name
, filename
);
1403 ret
= elf_load_file(hProcess
, name
, load_offset
, dyn_addr
, elf_info
);
1404 HeapFree( GetProcessHeap(), 0, name
);
1409 #ifdef AT_SYSINFO_EHDR
1410 /******************************************************************
1413 * locate some a value from the debuggee auxiliary vector
1415 static BOOL
elf_search_auxv(const struct process
* pcs
, unsigned type
, unsigned long* val
)
1417 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1418 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1424 si
->SizeOfStruct
= sizeof(*si
);
1425 si
->MaxNameLen
= MAX_SYM_NAME
;
1426 if (!SymFromName(pcs
->handle
, "libwine.so.1!__wine_main_environ", si
) ||
1427 !(addr
= (void*)(DWORD_PTR
)si
->Address
) ||
1428 !ReadProcessMemory(pcs
->handle
, addr
, &addr
, sizeof(addr
), NULL
) ||
1431 FIXME("can't find symbol in module\n");
1434 /* walk through envp[] */
1435 /* envp[] strings are located after the auxiliary vector, so protect the walk */
1436 str_max
= (void*)(DWORD_PTR
)~0L;
1437 while (ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) &&
1438 (addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
))) != NULL
&& str
!= NULL
)
1439 str_max
= min(str_max
, str
);
1441 /* Walk through the end of envp[] array.
1442 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1443 * deleted, the last entry is replaced by an extra NULL.
1445 while (addr
< str_max
&& ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) && str
== NULL
)
1446 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
));
1448 while (ReadProcessMemory(pcs
->handle
, addr
, &auxv
, sizeof(auxv
), NULL
) && auxv
.a_type
!= AT_NULL
)
1450 if (auxv
.a_type
== type
)
1452 *val
= auxv
.a_un
.a_val
;
1455 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(auxv
));
1462 /******************************************************************
1463 * elf_search_and_load_file
1465 * lookup a file in standard ELF locations, and if found, load it
1467 static BOOL
elf_search_and_load_file(struct process
* pcs
, const WCHAR
* filename
,
1468 unsigned long load_offset
, unsigned long dyn_addr
,
1469 struct elf_info
* elf_info
)
1472 struct module
* module
;
1473 static const WCHAR S_libstdcPPW
[] = {'l','i','b','s','t','d','c','+','+','\0'};
1475 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
1476 if ((module
= module_is_already_loaded(pcs
, filename
)))
1478 elf_info
->module
= module
;
1479 elf_info
->module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1480 return module
->module
.SymType
;
1483 if (strstrW(filename
, S_libstdcPPW
)) return FALSE
; /* We know we can't do it */
1484 ret
= elf_load_file(pcs
, filename
, load_offset
, dyn_addr
, elf_info
);
1485 /* if relative pathname, try some absolute base dirs */
1486 if (!ret
&& !strchrW(filename
, '/'))
1488 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1489 getenv("PATH"), elf_info
) ||
1490 elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1491 getenv("LD_LIBRARY_PATH"), elf_info
);
1492 if (!ret
) ret
= elf_load_file_from_dll_path(pcs
, filename
,
1493 load_offset
, dyn_addr
, elf_info
);
1499 typedef BOOL (*enum_elf_modules_cb
)(const WCHAR
*, unsigned long load_addr
,
1500 unsigned long dyn_addr
, BOOL is_system
, void* user
);
1502 /******************************************************************
1503 * elf_enum_modules_internal
1505 * Enumerate ELF modules from a running process
1507 static BOOL
elf_enum_modules_internal(const struct process
* pcs
,
1508 const WCHAR
* main_name
,
1509 enum_elf_modules_cb cb
, void* user
)
1511 struct r_debug dbg_hdr
;
1515 WCHAR bufstrW
[MAX_PATH
];
1517 if (!pcs
->dbg_hdr_addr
||
1518 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1519 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1522 /* Now walk the linked list. In all known ELF implementations,
1523 * the dynamic loader maintains this linked list for us. In some
1524 * cases the first entry doesn't appear with a name, in other cases it
1527 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
1529 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1532 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
1533 lm
.l_name
!= NULL
&&
1534 ReadProcessMemory(pcs
->handle
, lm
.l_name
, bufstr
, sizeof(bufstr
), NULL
))
1536 bufstr
[sizeof(bufstr
) - 1] = '\0';
1537 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, sizeof(bufstrW
) / sizeof(WCHAR
));
1538 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1539 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, (unsigned long)lm
.l_ld
, FALSE
, user
)) break;
1543 #ifdef AT_SYSINFO_EHDR
1546 unsigned long ehdr_addr
;
1548 if (elf_search_auxv(pcs
, AT_SYSINFO_EHDR
, &ehdr_addr
))
1550 static const WCHAR vdsoW
[] = {'[','v','d','s','o',']','.','s','o',0};
1551 cb(vdsoW
, ehdr_addr
, 0, TRUE
, user
);
1558 /******************************************************************
1561 * Lookup in a running ELF process the loader, and sets its ELF link
1562 * address (for accessing the list of loaded .so libs) in pcs.
1563 * If flags is ELF_INFO_MODULE, the module for the loader is also
1564 * added as a module into pcs.
1566 static BOOL
elf_search_loader(struct process
* pcs
, struct elf_info
* elf_info
)
1568 return elf_search_and_load_file(pcs
, get_wine_loader_name(), 0, 0, elf_info
);
1571 /******************************************************************
1572 * elf_read_wine_loader_dbg_info
1574 * Try to find a decent wine executable which could have loaded the debuggee
1576 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1578 struct elf_info elf_info
;
1580 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
1581 if (!elf_search_loader(pcs
, &elf_info
)) return FALSE
;
1582 elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_loader
= 1;
1583 module_set_module(elf_info
.module
, S_WineLoaderW
);
1584 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
1587 struct elf_enum_user
1593 static BOOL
elf_enum_modules_translate(const WCHAR
* name
, unsigned long load_addr
,
1594 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1596 struct elf_enum_user
* eeu
= user
;
1597 return eeu
->cb(name
, load_addr
, eeu
->user
);
1600 /******************************************************************
1603 * Enumerates the ELF loaded modules from a running target (hProc)
1604 * This function doesn't require that someone has called SymInitialize
1605 * on this very process.
1607 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1610 struct elf_info elf_info
;
1612 struct elf_enum_user eeu
;
1614 memset(&pcs
, 0, sizeof(pcs
));
1616 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_NAME
;
1617 if (!elf_search_loader(&pcs
, &elf_info
)) return FALSE
;
1618 pcs
.dbg_hdr_addr
= elf_info
.dbg_hdr_addr
;
1621 ret
= elf_enum_modules_internal(&pcs
, elf_info
.module_name
, elf_enum_modules_translate
, &eeu
);
1622 HeapFree(GetProcessHeap(), 0, (char*)elf_info
.module_name
);
1628 struct process
* pcs
;
1629 struct elf_info elf_info
;
1634 /******************************************************************
1637 * Callback for elf_load_module, used to walk the list of loaded
1640 static BOOL
elf_load_cb(const WCHAR
* name
, unsigned long load_addr
,
1641 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1643 struct elf_load
* el
= user
;
1647 if (is_system
) /* virtual ELF module, created by system. handle it from memory */
1649 struct module
* module
;
1650 struct elf_map_file_data emfd
;
1651 struct image_file_map fmap
;
1653 if ((module
= module_is_already_loaded(el
->pcs
, name
)))
1655 el
->elf_info
.module
= module
;
1656 el
->elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1657 return module
->module
.SymType
;
1660 emfd
.kind
= from_process
;
1661 emfd
.u
.process
.handle
= el
->pcs
->handle
;
1662 emfd
.u
.process
.load_addr
= (void*)load_addr
;
1664 if (elf_map_file(&emfd
, &fmap
))
1665 el
->ret
= elf_load_file_from_fmap(el
->pcs
, name
, &fmap
, load_addr
, 0, &el
->elf_info
);
1670 /* memcmp is needed for matches when bufstr contains also version information
1671 * el->name: libc.so, name: libc.so.6.0
1673 p
= strrchrW(name
, '/');
1677 if (!el
->name
|| !memcmp(p
, el
->name
, lstrlenW(el
->name
) * sizeof(WCHAR
)))
1679 el
->ret
= elf_search_and_load_file(el
->pcs
, name
, load_addr
, dyn_addr
, &el
->elf_info
);
1680 if (el
->name
) ret
= FALSE
;
1686 /******************************************************************
1689 * loads an ELF module and stores it in process' module list
1690 * Also, find module real name and load address from
1691 * the real loaded modules list in pcs address space
1693 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1697 TRACE("(%p %s %08lx)\n", pcs
, debugstr_w(name
), addr
);
1699 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1702 if (pcs
->dbg_hdr_addr
) /* we're debugging a life target */
1705 /* do only the lookup from the filename, not the path (as we lookup module
1706 * name in the process' loaded module list)
1708 el
.name
= strrchrW(name
, '/');
1709 if (!el
.name
++) el
.name
= name
;
1712 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1718 el
.ret
= elf_search_and_load_file(pcs
, el
.name
, addr
, 0, &el
.elf_info
);
1720 if (!el
.ret
) return NULL
;
1721 assert(el
.elf_info
.module
);
1722 return el
.elf_info
.module
;
1725 /******************************************************************
1726 * elf_synchronize_module_list
1728 * this function rescans the debuggee module's list and synchronizes it with
1729 * the one from 'pcs', i.e.:
1730 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1731 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1733 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1735 struct module
* module
;
1738 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1740 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1741 module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 0;
1745 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1747 el
.name
= NULL
; /* fetch all modules */
1749 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1752 module
= pcs
->lmodules
;
1755 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1757 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
1759 if (!elf_info
->elf_mark
&& !elf_info
->elf_loader
)
1761 module_remove(pcs
, module
);
1762 /* restart all over */
1763 module
= pcs
->lmodules
;
1767 module
= module
->next
;
1772 #else /* !__ELF__ */
1774 BOOL
elf_find_section(struct image_file_map
* fmap
, const char* name
,
1775 unsigned sht
, struct image_section_map
* ism
)
1780 const char* elf_map_section(struct image_section_map
* ism
)
1785 void elf_unmap_section(struct image_section_map
* ism
)
1788 unsigned elf_get_map_size(const struct image_section_map
* ism
)
1793 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
1798 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1803 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1804 DWORD
* size
, DWORD
* checksum
)
1809 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1814 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1819 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1824 BOOL
elf_load_debug_info(struct module
* module
)
1829 int elf_is_in_thunk_area(unsigned long addr
,
1830 const struct elf_thunk_area
* thunks
)
1834 #endif /* __ELF__ */