2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2007 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
25 #if defined(__svr4__) || defined(__sun)
27 /* large files are not supported by libelf */
28 #undef _FILE_OFFSET_BITS
29 #define _FILE_OFFSET_BITS 32
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
39 #ifdef HAVE_SYS_MMAN_H
46 #define PATH_MAX MAX_PATH
49 #include "dbghelp_private.h"
51 #include "image_private.h"
53 #include "wine/library.h"
54 #include "wine/debug.h"
58 #define ELF_INFO_DEBUG_HEADER 0x0001
59 #define ELF_INFO_MODULE 0x0002
60 #define ELF_INFO_NAME 0x0004
62 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
66 unsigned flags
; /* IN one (or several) of the ELF_INFO constants */
67 DWORD_PTR dbg_hdr_addr
; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
68 struct module
* module
; /* OUT loaded module (if ELF_INFO_MODULE is set) */
69 const WCHAR
* module_name
; /* OUT found module name (if ELF_INFO_NAME is set) */
74 struct hash_table_elt ht_elt
;
76 struct symt_compiland
* compiland
;
83 THUNK_ORDINAL ordinal
;
84 unsigned long rva_start
;
85 unsigned long rva_end
;
88 struct elf_module_info
90 unsigned long elf_addr
;
91 unsigned short elf_mark
: 1,
93 struct image_file_map file_map
;
96 /******************************************************************
99 * Maps a single section into memory from an ELF file
101 const char* elf_map_section(struct image_section_map
* ism
)
103 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
105 unsigned long pgsz
= getpagesize();
106 unsigned long ofst
, size
;
108 assert(ism
->fmap
->modtype
== DMT_ELF
);
109 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
||
110 fmap
->sect
[ism
->sidx
].shdr
.sh_type
== SHT_NOBITS
)
113 if (fmap
->target_copy
)
115 return fmap
->target_copy
+ fmap
->sect
[ism
->sidx
].shdr
.sh_offset
;
117 /* align required information on page size (we assume pagesize is a power of 2) */
118 ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
119 size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
120 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
121 fmap
->sect
[ism
->sidx
].mapped
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
,
123 if (fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
) return IMAGE_NO_MAP
;
124 return fmap
->sect
[ism
->sidx
].mapped
+ (fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& (pgsz
- 1));
127 /******************************************************************
130 * Finds a section by name (and type) into memory from an ELF file
131 * or its alternate if any
133 BOOL
elf_find_section(struct image_file_map
* _fmap
, const char* name
,
134 unsigned sht
, struct image_section_map
* ism
)
136 struct elf_file_map
* fmap
;
141 fmap
= &_fmap
->u
.elf
;
142 if (fmap
->shstrtab
== IMAGE_NO_MAP
)
144 struct image_section_map hdr_ism
= {_fmap
, fmap
->elfhdr
.e_shstrndx
};
145 if ((fmap
->shstrtab
= elf_map_section(&hdr_ism
)) == IMAGE_NO_MAP
) break;
147 for (i
= 0; i
< fmap
->elfhdr
.e_shnum
; i
++)
149 if (strcmp(fmap
->shstrtab
+ fmap
->sect
[i
].shdr
.sh_name
, name
) == 0 &&
150 (sht
== SHT_NULL
|| sht
== fmap
->sect
[i
].shdr
.sh_type
))
157 _fmap
= fmap
->alternate
;
164 /******************************************************************
167 * Unmaps a single section from memory
169 void elf_unmap_section(struct image_section_map
* ism
)
171 struct elf_file_map
* fmap
= &ism
->fmap
->u
.elf
;
173 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->elfhdr
.e_shnum
&& !fmap
->target_copy
&&
174 fmap
->sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
176 unsigned long pgsz
= getpagesize();
177 unsigned long ofst
, size
;
179 ofst
= fmap
->sect
[ism
->sidx
].shdr
.sh_offset
& ~(pgsz
- 1);
180 size
= ((fmap
->sect
[ism
->sidx
].shdr
.sh_offset
+
181 fmap
->sect
[ism
->sidx
].shdr
.sh_size
+ pgsz
- 1) & ~(pgsz
- 1)) - ofst
;
182 if (munmap((char*)fmap
->sect
[ism
->sidx
].mapped
, size
) < 0)
183 WARN("Couldn't unmap the section\n");
184 fmap
->sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
188 static void elf_end_find(struct image_file_map
* fmap
)
190 struct image_section_map ism
;
195 ism
.sidx
= fmap
->u
.elf
.elfhdr
.e_shstrndx
;
196 elf_unmap_section(&ism
);
197 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
198 fmap
= fmap
->u
.elf
.alternate
;
202 /******************************************************************
205 * Get the RVA of an ELF section
207 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
209 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
211 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_addr
- ism
->fmap
->u
.elf
.elf_start
;
214 /******************************************************************
217 * Get the size of an ELF section
219 unsigned elf_get_map_size(const struct image_section_map
* ism
)
221 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.elf
.elfhdr
.e_shnum
)
223 return ism
->fmap
->u
.elf
.sect
[ism
->sidx
].shdr
.sh_size
;
226 static inline void elf_reset_file_map(struct image_file_map
* fmap
)
229 fmap
->u
.elf
.shstrtab
= IMAGE_NO_MAP
;
230 fmap
->u
.elf
.alternate
= NULL
;
231 fmap
->u
.elf
.target_copy
= NULL
;
234 struct elf_map_file_data
236 enum {from_file
, from_process
} kind
;
241 const WCHAR
* filename
;
251 static BOOL
elf_map_file_read(struct image_file_map
* fmap
, struct elf_map_file_data
* emfd
,
252 void* buf
, size_t len
, off_t off
)
259 return pread(fmap
->u
.elf
.fd
, buf
, len
, off
) == len
;
261 return ReadProcessMemory(emfd
->u
.process
.handle
,
262 (void*)((unsigned long)emfd
->u
.process
.load_addr
+ (unsigned long)off
),
263 buf
, len
, &dw
) && dw
== len
;
270 /******************************************************************
273 * Maps an ELF file into memory (and checks it's a real ELF file)
275 static BOOL
elf_map_file(struct elf_map_file_data
* emfd
, struct image_file_map
* fmap
)
277 static const BYTE elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
281 unsigned long tmp
, page_mask
= getpagesize() - 1;
289 len
= WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, NULL
, 0, NULL
, NULL
);
290 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, len
))) return FALSE
;
291 WideCharToMultiByte(CP_UNIXCP
, 0, emfd
->u
.file
.filename
, -1, filename
, len
, NULL
, NULL
);
299 elf_reset_file_map(fmap
);
301 fmap
->modtype
= DMT_ELF
;
303 fmap
->u
.elf
.target_copy
= NULL
;
308 /* check that the file exists, and that the module hasn't been loaded yet */
309 if (stat(filename
, &statbuf
) == -1 || S_ISDIR(statbuf
.st_mode
)) goto done
;
311 /* Now open the file, so that we can mmap() it. */
312 if ((fmap
->u
.elf
.fd
= open(filename
, O_RDONLY
)) == -1) goto done
;
317 if (!elf_map_file_read(fmap
, emfd
, &fmap
->u
.elf
.elfhdr
, sizeof(fmap
->u
.elf
.elfhdr
), 0))
320 /* and check for an ELF header */
321 if (memcmp(fmap
->u
.elf
.elfhdr
.e_ident
,
322 elf_signature
, sizeof(elf_signature
))) goto done
;
323 /* and check 32 vs 64 size according to current machine */
325 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS64
) goto done
;
327 if (fmap
->u
.elf
.elfhdr
.e_ident
[EI_CLASS
] != ELFCLASS32
) goto done
;
329 fmap
->u
.elf
.sect
= HeapAlloc(GetProcessHeap(), 0,
330 fmap
->u
.elf
.elfhdr
.e_shnum
* sizeof(fmap
->u
.elf
.sect
[0]));
331 if (!fmap
->u
.elf
.sect
) goto done
;
333 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_shnum
; i
++)
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(fmap
->u
.elf
.sect
[i
].shdr
)))
338 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
339 fmap
->u
.elf
.sect
= NULL
;
342 fmap
->u
.elf
.sect
[i
].mapped
= IMAGE_NO_MAP
;
345 /* grab size of module once loaded in memory */
346 fmap
->u
.elf
.elf_size
= 0;
347 fmap
->u
.elf
.elf_start
= ~0L;
348 for (i
= 0; i
< fmap
->u
.elf
.elfhdr
.e_phnum
; i
++)
350 if (elf_map_file_read(fmap
, emfd
, &phdr
, sizeof(phdr
),
351 fmap
->u
.elf
.elfhdr
.e_phoff
+ i
* sizeof(phdr
)) &&
352 phdr
.p_type
== PT_LOAD
)
354 tmp
= (phdr
.p_vaddr
+ phdr
.p_memsz
+ page_mask
) & ~page_mask
;
355 if (fmap
->u
.elf
.elf_size
< tmp
) fmap
->u
.elf
.elf_size
= tmp
;
356 if (phdr
.p_vaddr
< fmap
->u
.elf
.elf_start
) fmap
->u
.elf
.elf_start
= phdr
.p_vaddr
;
359 /* if non relocatable ELF, then remove fixed address from computation
360 * otherwise, all addresses are zero based and start has no effect
362 fmap
->u
.elf
.elf_size
-= fmap
->u
.elf
.elf_start
;
366 case from_file
: break;
368 if (!(fmap
->u
.elf
.target_copy
= HeapAlloc(GetProcessHeap(), 0, fmap
->u
.elf
.elf_size
)))
370 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
373 if (!ReadProcessMemory(emfd
->u
.process
.handle
, emfd
->u
.process
.load_addr
, fmap
->u
.elf
.target_copy
,
374 fmap
->u
.elf
.elf_size
, NULL
))
376 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
377 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
384 HeapFree(GetProcessHeap(), 0, filename
);
388 /******************************************************************
391 * Unmaps an ELF file from memory (previously mapped with elf_map_file)
393 static void elf_unmap_file(struct image_file_map
* fmap
)
397 if (fmap
->u
.elf
.fd
!= -1)
399 struct image_section_map ism
;
401 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.elf
.elfhdr
.e_shnum
; ism
.sidx
++)
403 elf_unmap_section(&ism
);
405 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.sect
);
406 close(fmap
->u
.elf
.fd
);
408 HeapFree(GetProcessHeap(), 0, fmap
->u
.elf
.target_copy
);
409 fmap
= fmap
->u
.elf
.alternate
;
413 static void elf_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
415 elf_unmap_file(&modfmt
->u
.elf_info
->file_map
);
416 HeapFree(GetProcessHeap(), 0, modfmt
);
419 /******************************************************************
420 * elf_is_in_thunk_area
422 * Check whether an address lies within one of the thunk area we
425 int elf_is_in_thunk_area(unsigned long addr
,
426 const struct elf_thunk_area
* thunks
)
430 if (thunks
) for (i
= 0; thunks
[i
].symname
; i
++)
432 if (addr
>= thunks
[i
].rva_start
&& addr
< thunks
[i
].rva_end
)
438 /******************************************************************
441 * creating an internal hash table to ease use ELF symtab information lookup
443 static void elf_hash_symtab(struct module
* module
, struct pool
* pool
,
444 struct hash_table
* ht_symtab
, struct image_file_map
* fmap
,
445 struct elf_thunk_area
* thunks
)
450 struct symt_compiland
* compiland
= NULL
;
453 struct symtab_elt
* ste
;
454 struct image_section_map ism
, ism_str
;
456 if (!elf_find_section(fmap
, ".symtab", SHT_SYMTAB
, &ism
) &&
457 !elf_find_section(fmap
, ".dynsym", SHT_DYNSYM
, &ism
)) return;
458 if ((symp
= (const Elf_Sym
*)image_map_section(&ism
)) == IMAGE_NO_MAP
) return;
459 ism_str
.fmap
= ism
.fmap
;
460 ism_str
.sidx
= fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_link
;
461 if ((strp
= image_map_section(&ism_str
)) == IMAGE_NO_MAP
)
463 image_unmap_section(&ism
);
467 nsym
= image_get_map_size(&ism
) / sizeof(*symp
);
469 for (j
= 0; thunks
[j
].symname
; j
++)
470 thunks
[j
].rva_start
= thunks
[j
].rva_end
= 0;
472 for (i
= 0; i
< nsym
; i
++, symp
++)
474 /* Ignore certain types of entries which really aren't of that much
477 if ((ELF32_ST_TYPE(symp
->st_info
) != STT_NOTYPE
&&
478 ELF32_ST_TYPE(symp
->st_info
) != STT_FILE
&&
479 ELF32_ST_TYPE(symp
->st_info
) != STT_OBJECT
&&
480 ELF32_ST_TYPE(symp
->st_info
) != STT_FUNC
) ||
481 symp
->st_shndx
== SHN_UNDEF
)
486 symname
= strp
+ symp
->st_name
;
488 /* handle some specific symtab (that we'll throw away when done) */
489 switch (ELF32_ST_TYPE(symp
->st_info
))
493 compiland
= symt_new_compiland(module
, symp
->st_value
,
494 source_new(module
, NULL
, symname
));
499 /* we are only interested in wine markers inserted by winebuild */
500 for (j
= 0; thunks
[j
].symname
; j
++)
502 if (!strcmp(symname
, thunks
[j
].symname
))
504 thunks
[j
].rva_start
= symp
->st_value
;
505 thunks
[j
].rva_end
= symp
->st_value
+ symp
->st_size
;
512 /* FIXME: we don't need to handle them (GCC internals)
513 * Moreover, they screw up our symbol lookup :-/
515 if (symname
[0] == '.' && symname
[1] == 'L' && isdigit(symname
[2]))
518 ste
= pool_alloc(pool
, sizeof(*ste
));
519 ste
->ht_elt
.name
= symname
;
520 /* GCC emits, in some cases, a .<digit>+ suffix.
521 * This is used for static variable inside functions, so
522 * that we can have several such variables with same name in
523 * the same compilation unit
524 * We simply ignore that suffix when present (we also get rid
525 * of it in stabs parsing)
527 ptr
= symname
+ strlen(symname
) - 1;
530 while (isdigit(*ptr
) && ptr
>= symname
) ptr
--;
531 if (ptr
> symname
&& *ptr
== '.')
533 char* n
= pool_alloc(pool
, ptr
- symname
+ 1);
534 memcpy(n
, symname
, ptr
- symname
+ 1);
535 n
[ptr
- symname
] = '\0';
536 ste
->ht_elt
.name
= n
;
540 ste
->compiland
= compiland
;
542 hash_table_add(ht_symtab
, &ste
->ht_elt
);
544 /* as we added in the ht_symtab pointers to the symbols themselves,
545 * we cannot unmap yet the sections, it will be done when we're over
550 /******************************************************************
553 * lookup a symbol by name in our internal hash table for the symtab
555 static const Elf_Sym
* elf_lookup_symtab(const struct module
* module
,
556 const struct hash_table
* ht_symtab
,
557 const char* name
, const struct symt
* compiland
)
559 struct symtab_elt
* weak_result
= NULL
; /* without compiland name */
560 struct symtab_elt
* result
= NULL
;
561 struct hash_table_iter hti
;
562 struct symtab_elt
* ste
;
563 const char* compiland_name
;
564 const char* compiland_basename
;
567 /* we need weak match up (at least) when symbols of same name,
568 * defined several times in different compilation units,
569 * are merged in a single one (hence a different filename for c.u.)
573 compiland_name
= source_get(module
,
574 ((const struct symt_compiland
*)compiland
)->source
);
575 compiland_basename
= strrchr(compiland_name
, '/');
576 if (!compiland_basename
++) compiland_basename
= compiland_name
;
578 else compiland_name
= compiland_basename
= NULL
;
580 hash_table_iter_init(ht_symtab
, &hti
, name
);
581 while ((ste
= hash_table_iter_up(&hti
)))
583 if (ste
->used
|| strcmp(ste
->ht_elt
.name
, name
)) continue;
586 if ((ste
->compiland
&& !compiland_name
) || (!ste
->compiland
&& compiland_name
))
588 if (ste
->compiland
&& compiland_name
)
590 const char* filename
= source_get(module
, ste
->compiland
->source
);
591 if (strcmp(filename
, compiland_name
))
593 base
= strrchr(filename
, '/');
594 if (!base
++) base
= filename
;
595 if (strcmp(base
, compiland_basename
)) continue;
600 FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
601 name
, compiland_name
,
602 source_get(module
, result
->compiland
->source
), (unsigned int)result
->symp
->st_value
,
603 source_get(module
, ste
->compiland
->source
), (unsigned int)ste
->symp
->st_value
);
611 if (!result
&& !(result
= weak_result
))
613 FIXME("Couldn't find symbol %s!%s in symtab\n",
614 debugstr_w(module
->module
.ModuleName
), name
);
620 /******************************************************************
621 * elf_finish_stabs_info
623 * - get any relevant information (address & size) from the bits we got from the
624 * stabs debugging information
626 static void elf_finish_stabs_info(struct module
* module
, const struct hash_table
* symtab
)
628 struct hash_table_iter hti
;
632 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
634 hash_table_iter_init(&module
->ht_symbols
, &hti
, NULL
);
635 while ((ptr
= hash_table_iter_up(&hti
)))
637 sym
= GET_ENTRY(ptr
, struct symt_ht
, hash_elt
);
638 switch (sym
->symt
.tag
)
641 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
642 ((struct symt_function
*)sym
)->size
)
646 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
647 ((struct symt_function
*)sym
)->container
);
650 if (((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
&&
651 ((struct symt_function
*)sym
)->address
!= elf_info
->elf_addr
+ symp
->st_value
)
652 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
653 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
654 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
655 if (((struct symt_function
*)sym
)->size
&& ((struct symt_function
*)sym
)->size
!= symp
->st_size
)
656 FIXME("Changing size for %p/%s!%s from %08lx to %08x\n",
657 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
658 ((struct symt_function
*)sym
)->size
, (unsigned int)symp
->st_size
);
660 ((struct symt_function
*)sym
)->address
= elf_info
->elf_addr
+ symp
->st_value
;
661 ((struct symt_function
*)sym
)->size
= symp
->st_size
;
663 FIXME("Couldn't find %s!%s\n",
664 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
667 switch (((struct symt_data
*)sym
)->kind
)
670 case DataIsFileStatic
:
671 if (((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
)
673 symp
= elf_lookup_symtab(module
, symtab
, sym
->hash_elt
.name
,
674 ((struct symt_data
*)sym
)->container
);
677 if (((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
&&
678 ((struct symt_data
*)sym
)->u
.var
.offset
!= elf_info
->elf_addr
+ symp
->st_value
)
679 FIXME("Changing address for %p/%s!%s from %08lx to %08lx\n",
680 sym
, debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
,
681 ((struct symt_function
*)sym
)->address
, elf_info
->elf_addr
+ symp
->st_value
);
682 ((struct symt_data
*)sym
)->u
.var
.offset
= elf_info
->elf_addr
+ symp
->st_value
;
683 ((struct symt_data
*)sym
)->kind
= (ELF32_ST_BIND(symp
->st_info
) == STB_LOCAL
) ?
684 DataIsFileStatic
: DataIsGlobal
;
686 FIXME("Couldn't find %s!%s\n",
687 debugstr_w(module
->module
.ModuleName
), sym
->hash_elt
.name
);
693 FIXME("Unsupported tag %u\n", sym
->symt
.tag
);
697 /* since we may have changed some addresses & sizes, mark the module to be resorted */
698 module
->sortlist_valid
= FALSE
;
701 /******************************************************************
702 * elf_load_wine_thunks
704 * creating the thunk objects for a wine native DLL
706 static int elf_new_wine_thunks(struct module
* module
, const struct hash_table
* ht_symtab
,
707 const struct elf_thunk_area
* thunks
)
710 struct hash_table_iter hti
;
711 struct symtab_elt
* ste
;
713 struct symt_ht
* symt
;
715 hash_table_iter_init(ht_symtab
, &hti
, NULL
);
716 while ((ste
= hash_table_iter_up(&hti
)))
718 if (ste
->used
) continue;
720 addr
= module
->reloc_delta
+ ste
->symp
->st_value
;
722 j
= elf_is_in_thunk_area(ste
->symp
->st_value
, thunks
);
723 if (j
>= 0) /* thunk found */
725 symt_new_thunk(module
, ste
->compiland
, ste
->ht_elt
.name
, thunks
[j
].ordinal
,
726 addr
, ste
->symp
->st_size
);
732 symt
= symt_find_nearest(module
, addr
);
733 if (symt
&& !symt_get_info(module
, &symt
->symt
, TI_GET_ADDRESS
, &ref_addr
))
735 if (!symt
|| addr
!= ref_addr
)
737 /* creating public symbols for all the ELF symbols which haven't been
738 * used yet (ie we have no debug information on them)
739 * That's the case, for example, of the .spec.c files
741 switch (ELF32_ST_TYPE(ste
->symp
->st_info
))
744 symt_new_function(module
, ste
->compiland
, ste
->ht_elt
.name
,
745 addr
, ste
->symp
->st_size
, NULL
);
748 symt_new_global_variable(module
, ste
->compiland
, ste
->ht_elt
.name
,
749 ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
,
750 addr
, ste
->symp
->st_size
, NULL
);
753 FIXME("Shouldn't happen\n");
756 /* FIXME: this is a hack !!!
757 * we are adding new symbols, but as we're parsing a symbol table
758 * (hopefully without duplicate symbols) we delay rebuilding the sorted
759 * module table until we're done with the symbol table
760 * Otherwise, as we intertwine symbols's add and lookup, performance
763 module
->sortlist_valid
= TRUE
;
765 else if (strcmp(ste
->ht_elt
.name
, symt
->hash_elt
.name
))
767 ULONG64 xaddr
= 0, xsize
= 0;
770 symt_get_info(module
, &symt
->symt
, TI_GET_ADDRESS
, &xaddr
);
771 symt_get_info(module
, &symt
->symt
, TI_GET_LENGTH
, &xsize
);
772 symt_get_info(module
, &symt
->symt
, TI_GET_DATAKIND
, &kind
);
774 /* If none of symbols has a correct size, we consider they are both markers
775 * Hence, we can silence this warning
776 * Also, we check that we don't have two symbols, one local, the other
777 * global which is legal
779 if ((xsize
|| ste
->symp
->st_size
) &&
780 (kind
== (ELF32_ST_BIND(ste
->symp
->st_info
) == STB_LOCAL
) ? DataIsFileStatic
: DataIsGlobal
))
781 FIXME("Duplicate in %s: %s<%08lx-%08x> %s<%s-%s>\n",
782 debugstr_w(module
->module
.ModuleName
),
783 ste
->ht_elt
.name
, addr
, (unsigned int)ste
->symp
->st_size
,
785 wine_dbgstr_longlong(xaddr
), wine_dbgstr_longlong(xsize
));
789 /* see comment above */
790 module
->sortlist_valid
= FALSE
;
794 /******************************************************************
795 * elf_new_public_symbols
797 * Creates a set of public symbols from an ELF symtab
799 static int elf_new_public_symbols(struct module
* module
, const struct hash_table
* symtab
)
801 struct hash_table_iter hti
;
802 struct symtab_elt
* ste
;
804 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
806 /* FIXME: we're missing the ELF entry point here */
808 hash_table_iter_init(symtab
, &hti
, NULL
);
809 while ((ste
= hash_table_iter_up(&hti
)))
811 symt_new_public(module
, ste
->compiland
, ste
->ht_elt
.name
,
812 module
->reloc_delta
+ ste
->symp
->st_value
,
818 static BOOL
elf_check_debug_link(const WCHAR
* file
, struct image_file_map
* fmap
, DWORD crc
)
821 struct elf_map_file_data emfd
;
823 emfd
.kind
= from_file
;
824 emfd
.u
.file
.filename
= file
;
825 if (!elf_map_file(&emfd
, fmap
)) return FALSE
;
826 if (!(ret
= crc
== calc_crc32(fmap
->u
.elf
.fd
)))
828 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n",
829 debugstr_w(file
), calc_crc32(fmap
->u
.elf
.fd
), crc
);
830 elf_unmap_file(fmap
);
835 /******************************************************************
836 * elf_locate_debug_link
838 * Locate a filename from a .gnu_debuglink section, using the same
840 * "If the full name of the directory containing the executable is
841 * execdir, and the executable has a debug link that specifies the
842 * name debugfile, then GDB will automatically search for the
843 * debugging information file in three places:
844 * - the directory containing the executable file (that is, it
845 * will look for a file named `execdir/debugfile',
846 * - a subdirectory of that directory named `.debug' (that is, the
847 * file `execdir/.debug/debugfile', and
848 * - a subdirectory of the global debug file directory that includes
849 * the executable's full path, and the name from the link (that is,
850 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
851 * is the global debug file directory, and execdir has been turned
852 * into a relative path)." (from GDB manual)
854 static BOOL
elf_locate_debug_link(struct image_file_map
* fmap
, const char* filename
,
855 const WCHAR
* loaded_file
, DWORD crc
)
857 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
858 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
859 const size_t globalDebugDirLen
= sizeof(globalDebugDirW
) / sizeof(WCHAR
);
863 struct image_file_map
* fmap_link
= NULL
;
865 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
866 if (!fmap_link
) return FALSE
;
868 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
869 p
= HeapAlloc(GetProcessHeap(), 0,
870 (globalDebugDirLen
+ strlenW(loaded_file
) + 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
873 /* we prebuild the string with "execdir" */
874 strcpyW(p
, loaded_file
);
875 slash
= strrchrW(p
, '/');
876 if (slash
== NULL
) slash
= p
; else slash
++;
878 /* testing execdir/filename */
879 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
880 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
882 /* testing execdir/.debug/filename */
883 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
884 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ sizeof(dotDebugW
) / sizeof(WCHAR
), filename_len
);
885 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
887 /* testing globaldebugdir/execdir/filename */
888 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
889 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
890 slash
+= globalDebugDirLen
;
891 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
892 if (elf_check_debug_link(p
, fmap_link
, crc
)) goto found
;
894 /* finally testing filename */
895 if (elf_check_debug_link(slash
, fmap_link
, crc
)) goto found
;
898 WARN("Couldn't locate or map %s\n", filename
);
899 HeapFree(GetProcessHeap(), 0, p
);
900 HeapFree(GetProcessHeap(), 0, fmap_link
);
904 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
905 HeapFree(GetProcessHeap(), 0, p
);
906 fmap
->u
.elf
.alternate
= fmap_link
;
910 /******************************************************************
911 * elf_debuglink_parse
913 * Parses a .gnu_debuglink section and loads the debug info from
914 * the external file specified there.
916 static BOOL
elf_debuglink_parse(struct image_file_map
* fmap
, const struct module
* module
,
917 const BYTE
* debuglink
)
919 /* The content of a debug link section is:
920 * 1/ a NULL terminated string, containing the file name for the
922 * 2/ padding on 4 byte boundary
923 * 3/ CRC of the linked ELF file
925 const char* dbg_link
= (const char*)debuglink
;
928 crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
929 return elf_locate_debug_link(fmap
, dbg_link
, module
->module
.LoadedImageName
, crc
);
932 /******************************************************************
933 * elf_load_debug_info_from_map
935 * Loads the symbolic information from ELF module which mapping is described
937 * the module has been loaded at 'load_offset' address, so symbols' address
938 * relocation is performed.
939 * CRC is checked if fmap->with_crc is TRUE
941 * 0 if the file doesn't contain symbolic info (or this info cannot be
945 static BOOL
elf_load_debug_info_from_map(struct module
* module
,
946 struct image_file_map
* fmap
,
948 struct hash_table
* ht_symtab
)
950 BOOL ret
= FALSE
, lret
;
951 struct elf_thunk_area thunks
[] =
953 {"__wine_spec_import_thunks", THUNK_ORDINAL_NOTYPE
, 0, 0}, /* inter DLL calls */
954 {"__wine_spec_delayed_import_loaders", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
955 {"__wine_spec_delayed_import_thunks", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
956 {"__wine_delay_load", THUNK_ORDINAL_LOAD
, 0, 0}, /* delayed inter DLL calls */
957 {"__wine_spec_thunk_text_16", -16, 0, 0}, /* 16 => 32 thunks */
958 {"__wine_spec_thunk_text_32", -32, 0, 0}, /* 32 => 16 thunks */
962 module
->module
.SymType
= SymExport
;
964 /* create a hash table for the symtab */
965 elf_hash_symtab(module
, pool
, ht_symtab
, fmap
, thunks
);
967 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
969 struct image_section_map stab_sect
, stabstr_sect
;
970 struct image_section_map debuglink_sect
;
972 /* if present, add the .gnu_debuglink file as an alternate to current one */
973 if (elf_find_section(fmap
, ".gnu_debuglink", SHT_NULL
, &debuglink_sect
))
975 const BYTE
* dbg_link
;
977 dbg_link
= (const BYTE
*)image_map_section(&debuglink_sect
);
978 if (dbg_link
!= IMAGE_NO_MAP
)
980 lret
= elf_debuglink_parse(fmap
, module
, dbg_link
);
982 WARN("Couldn't load linked debug file for %s\n",
983 debugstr_w(module
->module
.ModuleName
));
986 image_unmap_section(&debuglink_sect
);
988 if (elf_find_section(fmap
, ".stab", SHT_NULL
, &stab_sect
) &&
989 elf_find_section(fmap
, ".stabstr", SHT_NULL
, &stabstr_sect
))
994 stab
= image_map_section(&stab_sect
);
995 stabstr
= image_map_section(&stabstr_sect
);
996 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
998 /* OK, now just parse all of the stabs. */
999 lret
= stabs_parse(module
, module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_addr
,
1000 stab
, image_get_map_size(&stab_sect
),
1001 stabstr
, image_get_map_size(&stabstr_sect
),
1004 /* and fill in the missing information for stabs */
1005 elf_finish_stabs_info(module
, ht_symtab
);
1007 WARN("Couldn't correctly read stabs\n");
1011 image_unmap_section(&stab_sect
);
1012 image_unmap_section(&stabstr_sect
);
1014 lret
= dwarf2_parse(module
, module
->reloc_delta
, thunks
, fmap
);
1017 if (strstrW(module
->module
.ModuleName
, S_ElfW
) ||
1018 !strcmpW(module
->module
.ModuleName
, S_WineLoaderW
))
1020 /* add the thunks for native libraries */
1021 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
1022 elf_new_wine_thunks(module
, ht_symtab
, thunks
);
1024 /* add all the public symbols from symtab */
1025 if (elf_new_public_symbols(module
, ht_symtab
) && !ret
) ret
= TRUE
;
1030 /******************************************************************
1031 * elf_load_debug_info
1033 * Loads ELF debugging information from the module image file.
1035 BOOL
elf_load_debug_info(struct module
* module
)
1039 struct hash_table ht_symtab
;
1040 struct module_format
* modfmt
;
1042 if (module
->type
!= DMT_ELF
|| !(modfmt
= module
->format_info
[DFI_ELF
]) || !modfmt
->u
.elf_info
)
1044 ERR("Bad elf module '%s'\n", debugstr_w(module
->module
.LoadedImageName
));
1048 pool_init(&pool
, 65536);
1049 hash_table_init(&pool
, &ht_symtab
, 256);
1051 ret
= elf_load_debug_info_from_map(module
, &modfmt
->u
.elf_info
->file_map
, &pool
, &ht_symtab
);
1053 pool_destroy(&pool
);
1057 /******************************************************************
1058 * elf_fetch_file_info
1060 * Gathers some more information for an ELF module from a given file
1062 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1063 DWORD
* size
, DWORD
* checksum
)
1065 struct image_file_map fmap
;
1067 struct elf_map_file_data emfd
;
1069 emfd
.kind
= from_file
;
1070 emfd
.u
.file
.filename
= name
;
1071 if (!elf_map_file(&emfd
, &fmap
)) return FALSE
;
1072 if (base
) *base
= fmap
.u
.elf
.elf_start
;
1073 *size
= fmap
.u
.elf
.elf_size
;
1074 *checksum
= calc_crc32(fmap
.u
.elf
.fd
);
1075 elf_unmap_file(&fmap
);
1079 static BOOL
elf_load_file_from_fmap(struct process
* pcs
, const WCHAR
* filename
,
1080 struct image_file_map
* fmap
, unsigned long load_offset
,
1081 unsigned long dyn_addr
, struct elf_info
* elf_info
)
1085 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
1087 struct image_section_map ism
;
1089 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1092 char* ptr
= (char*)fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1097 if (!ReadProcessMemory(pcs
->handle
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
1100 if (dyn
.d_tag
== DT_DEBUG
)
1102 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
1103 if (load_offset
== 0 && dyn_addr
== 0) /* likely the case */
1104 /* Assume this module (the Wine loader) has been loaded at its preferred address */
1105 dyn_addr
= ism
.fmap
->u
.elf
.sect
[ism
.sidx
].shdr
.sh_addr
;
1109 } while (dyn
.d_tag
!= DT_NULL
);
1110 if (dyn
.d_tag
== DT_NULL
) return ret
;
1115 if (elf_info
->flags
& ELF_INFO_MODULE
)
1117 struct elf_module_info
*elf_module_info
;
1118 struct module_format
* modfmt
;
1119 struct image_section_map ism
;
1120 unsigned long modbase
= load_offset
;
1122 if (elf_find_section(fmap
, ".dynamic", SHT_DYNAMIC
, &ism
))
1124 unsigned long rva_dyn
= elf_get_map_rva(&ism
);
1126 TRACE("For module %s, got ELF (start=%lx dyn=%lx), link_map (start=%lx dyn=%lx)\n",
1127 debugstr_w(filename
), (unsigned long)fmap
->u
.elf
.elf_start
, rva_dyn
,
1128 load_offset
, dyn_addr
);
1129 if (dyn_addr
&& load_offset
+ rva_dyn
!= dyn_addr
)
1131 WARN("\thave to relocate: %lx\n", dyn_addr
- rva_dyn
);
1132 modbase
= dyn_addr
- rva_dyn
;
1134 } else WARN("For module %s, no .dynamic section\n", debugstr_w(filename
));
1137 modfmt
= HeapAlloc(GetProcessHeap(), 0,
1138 sizeof(struct module_format
) + sizeof(struct elf_module_info
));
1139 if (!modfmt
) return FALSE
;
1140 elf_info
->module
= module_new(pcs
, filename
, DMT_ELF
, FALSE
, modbase
,
1141 fmap
->u
.elf
.elf_size
, 0, calc_crc32(fmap
->u
.elf
.fd
));
1142 if (!elf_info
->module
)
1144 HeapFree(GetProcessHeap(), 0, modfmt
);
1147 elf_info
->module
->reloc_delta
= elf_info
->module
->module
.BaseOfImage
- fmap
->u
.elf
.elf_start
;
1148 elf_module_info
= (void*)(modfmt
+ 1);
1149 elf_info
->module
->format_info
[DFI_ELF
] = modfmt
;
1150 modfmt
->module
= elf_info
->module
;
1151 modfmt
->remove
= elf_module_remove
;
1152 modfmt
->loc_compute
= NULL
;
1153 modfmt
->u
.elf_info
= elf_module_info
;
1155 elf_module_info
->elf_addr
= load_offset
;
1157 elf_module_info
->file_map
= *fmap
;
1158 elf_reset_file_map(fmap
);
1159 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
1161 elf_info
->module
->module
.SymType
= SymDeferred
;
1164 else ret
= elf_load_debug_info(elf_info
->module
);
1166 elf_module_info
->elf_mark
= 1;
1167 elf_module_info
->elf_loader
= 0;
1170 if (elf_info
->flags
& ELF_INFO_NAME
)
1173 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1) * sizeof(WCHAR
));
1176 strcpyW(ptr
, filename
);
1177 elf_info
->module_name
= ptr
;
1185 /******************************************************************
1188 * Loads the information for ELF module stored in 'filename'
1189 * the module has been loaded at 'load_offset' address
1191 * -1 if the file cannot be found/opened
1192 * 0 if the file doesn't contain symbolic info (or this info cannot be
1196 static BOOL
elf_load_file(struct process
* pcs
, const WCHAR
* filename
,
1197 unsigned long load_offset
, unsigned long dyn_addr
,
1198 struct elf_info
* elf_info
)
1201 struct image_file_map fmap
;
1202 struct elf_map_file_data emfd
;
1204 TRACE("Processing elf file '%s' at %08lx\n", debugstr_w(filename
), load_offset
);
1206 emfd
.kind
= from_file
;
1207 emfd
.u
.file
.filename
= filename
;
1208 if (!elf_map_file(&emfd
, &fmap
)) return ret
;
1210 /* Next, we need to find a few of the internal ELF headers within
1211 * this thing. We need the main executable header, and the section
1214 if (!fmap
.u
.elf
.elf_start
&& !load_offset
)
1215 ERR("Relocatable ELF %s, but no load address. Loading at 0x0000000\n",
1216 debugstr_w(filename
));
1218 ret
= elf_load_file_from_fmap(pcs
, filename
, &fmap
, load_offset
, dyn_addr
, elf_info
);
1220 elf_unmap_file(&fmap
);
1225 /******************************************************************
1226 * elf_load_file_from_path
1227 * tries to load an ELF file from a set of paths (separated by ':')
1229 static BOOL
elf_load_file_from_path(HANDLE hProcess
,
1230 const WCHAR
* filename
,
1231 unsigned long load_offset
,
1232 unsigned long dyn_addr
,
1234 struct elf_info
* elf_info
)
1238 WCHAR
* pathW
= NULL
;
1241 if (!path
) return FALSE
;
1243 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1244 pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1245 if (!pathW
) return FALSE
;
1246 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, pathW
, len
);
1248 for (s
= pathW
; s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
1250 t
= strchrW(s
, ':');
1252 fn
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename
) + 1 + lstrlenW(s
) + 1) * sizeof(WCHAR
));
1255 strcatW(fn
, S_SlashW
);
1256 strcatW(fn
, filename
);
1257 ret
= elf_load_file(hProcess
, fn
, load_offset
, dyn_addr
, elf_info
);
1258 HeapFree(GetProcessHeap(), 0, fn
);
1260 s
= (t
) ? (t
+1) : NULL
;
1263 HeapFree(GetProcessHeap(), 0, pathW
);
1267 /******************************************************************
1268 * elf_load_file_from_dll_path
1270 * Tries to load an ELF file from the dll path
1272 static BOOL
elf_load_file_from_dll_path(HANDLE hProcess
,
1273 const WCHAR
* filename
,
1274 unsigned long load_offset
,
1275 unsigned long dyn_addr
,
1276 struct elf_info
* elf_info
)
1279 unsigned int index
= 0;
1282 while (!ret
&& (path
= wine_dll_enum_load_path( index
++ )))
1287 len
= MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, NULL
, 0);
1289 name
= HeapAlloc( GetProcessHeap(), 0,
1290 (len
+ lstrlenW(filename
) + 2) * sizeof(WCHAR
) );
1293 MultiByteToWideChar(CP_UNIXCP
, 0, path
, -1, name
, len
);
1294 strcatW( name
, S_SlashW
);
1295 strcatW( name
, filename
);
1296 ret
= elf_load_file(hProcess
, name
, load_offset
, dyn_addr
, elf_info
);
1297 HeapFree( GetProcessHeap(), 0, name
);
1302 #ifdef AT_SYSINFO_EHDR
1303 /******************************************************************
1306 * locate some a value from the debuggee auxillary vector
1308 static BOOL
elf_search_auxv(const struct process
* pcs
, unsigned type
, unsigned long* val
)
1310 char buffer
[sizeof(SYMBOL_INFO
) + MAX_SYM_NAME
];
1311 SYMBOL_INFO
*si
= (SYMBOL_INFO
*)buffer
;
1317 si
->SizeOfStruct
= sizeof(*si
);
1318 si
->MaxNameLen
= MAX_SYM_NAME
;
1319 if (!SymFromName(pcs
->handle
, "libwine.so.1!__wine_main_environ", si
) ||
1320 !(addr
= (void*)(DWORD_PTR
)si
->Address
) ||
1321 !ReadProcessMemory(pcs
->handle
, addr
, &addr
, sizeof(addr
), NULL
) ||
1324 FIXME("can't find symbol in module\n");
1327 /* walk through envp[] */
1328 /* envp[] strings are located after the auxillary vector, so protect the walk */
1329 str_max
= (void*)(DWORD_PTR
)~0L;
1330 while (ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) &&
1331 (addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
))) != NULL
&& str
!= NULL
)
1332 str_max
= min(str_max
, str
);
1334 /* Walk through the end of envp[] array.
1335 * Actually, there can be several NULLs at the end of envp[]. This happens when an env variable is
1336 * deleted, the last entry is replaced by an extra NULL.
1338 while (addr
< str_max
&& ReadProcessMemory(pcs
->handle
, addr
, &str
, sizeof(str
), NULL
) && str
== NULL
)
1339 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(str
));
1341 while (ReadProcessMemory(pcs
->handle
, addr
, &auxv
, sizeof(auxv
), NULL
) && auxv
.a_type
!= AT_NULL
)
1343 if (auxv
.a_type
== type
)
1345 *val
= auxv
.a_un
.a_val
;
1348 addr
= (void*)((DWORD_PTR
)addr
+ sizeof(auxv
));
1355 /******************************************************************
1356 * elf_search_and_load_file
1358 * lookup a file in standard ELF locations, and if found, load it
1360 static BOOL
elf_search_and_load_file(struct process
* pcs
, const WCHAR
* filename
,
1361 unsigned long load_offset
, unsigned long dyn_addr
,
1362 struct elf_info
* elf_info
)
1365 struct module
* module
;
1366 static WCHAR S_libstdcPPW
[] = {'l','i','b','s','t','d','c','+','+','\0'};
1368 if (filename
== NULL
|| *filename
== '\0') return FALSE
;
1369 if ((module
= module_is_already_loaded(pcs
, filename
)))
1371 elf_info
->module
= module
;
1372 elf_info
->module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1373 return module
->module
.SymType
;
1376 if (strstrW(filename
, S_libstdcPPW
)) return FALSE
; /* We know we can't do it */
1377 ret
= elf_load_file(pcs
, filename
, load_offset
, dyn_addr
, elf_info
);
1378 /* if relative pathname, try some absolute base dirs */
1379 if (!ret
&& !strchrW(filename
, '/'))
1381 ret
= elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1382 getenv("PATH"), elf_info
) ||
1383 elf_load_file_from_path(pcs
, filename
, load_offset
, dyn_addr
,
1384 getenv("LD_LIBRARY_PATH"), elf_info
);
1385 if (!ret
) ret
= elf_load_file_from_dll_path(pcs
, filename
,
1386 load_offset
, dyn_addr
, elf_info
);
1392 typedef BOOL (*enum_elf_modules_cb
)(const WCHAR
*, unsigned long load_addr
,
1393 unsigned long dyn_addr
, BOOL is_system
, void* user
);
1395 /******************************************************************
1396 * elf_enum_modules_internal
1398 * Enumerate ELF modules from a running process
1400 static BOOL
elf_enum_modules_internal(const struct process
* pcs
,
1401 const WCHAR
* main_name
,
1402 enum_elf_modules_cb cb
, void* user
)
1404 struct r_debug dbg_hdr
;
1408 WCHAR bufstrW
[MAX_PATH
];
1410 if (!pcs
->dbg_hdr_addr
||
1411 !ReadProcessMemory(pcs
->handle
, (void*)pcs
->dbg_hdr_addr
,
1412 &dbg_hdr
, sizeof(dbg_hdr
), NULL
))
1415 /* Now walk the linked list. In all known ELF implementations,
1416 * the dynamic loader maintains this linked list for us. In some
1417 * cases the first entry doesn't appear with a name, in other cases it
1420 for (lm_addr
= (void*)dbg_hdr
.r_map
; lm_addr
; lm_addr
= (void*)lm
.l_next
)
1422 if (!ReadProcessMemory(pcs
->handle
, lm_addr
, &lm
, sizeof(lm
), NULL
))
1425 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
1426 lm
.l_name
!= NULL
&&
1427 ReadProcessMemory(pcs
->handle
, lm
.l_name
, bufstr
, sizeof(bufstr
), NULL
))
1429 bufstr
[sizeof(bufstr
) - 1] = '\0';
1430 MultiByteToWideChar(CP_UNIXCP
, 0, bufstr
, -1, bufstrW
, sizeof(bufstrW
) / sizeof(WCHAR
));
1431 if (main_name
&& !bufstrW
[0]) strcpyW(bufstrW
, main_name
);
1432 if (!cb(bufstrW
, (unsigned long)lm
.l_addr
, (unsigned long)lm
.l_ld
, FALSE
, user
)) break;
1436 #ifdef AT_SYSINFO_EHDR
1439 unsigned long ehdr_addr
;
1441 if (elf_search_auxv(pcs
, AT_SYSINFO_EHDR
, &ehdr_addr
))
1443 static const WCHAR vdsoW
[] = {'[','v','d','s','o',']','.','s','o',0};
1444 cb(vdsoW
, ehdr_addr
, 0, TRUE
, user
);
1451 /******************************************************************
1454 * Lookup in a running ELF process the loader, and sets its ELF link
1455 * address (for accessing the list of loaded .so libs) in pcs.
1456 * If flags is ELF_INFO_MODULE, the module for the loader is also
1457 * added as a module into pcs.
1459 static BOOL
elf_search_loader(struct process
* pcs
, struct elf_info
* elf_info
)
1464 /* All binaries are loaded with WINELOADER (if run from tree) or by the
1467 if ((ptr
= getenv("WINELOADER")))
1469 WCHAR tmp
[MAX_PATH
];
1470 MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, tmp
, sizeof(tmp
) / sizeof(WCHAR
));
1471 ret
= elf_search_and_load_file(pcs
, tmp
, 0, 0, elf_info
);
1475 ret
= elf_search_and_load_file(pcs
, S_WineW
, 0, 0, elf_info
);
1480 /******************************************************************
1481 * elf_read_wine_loader_dbg_info
1483 * Try to find a decent wine executable which could have loaded the debuggee
1485 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1487 struct elf_info elf_info
;
1489 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_MODULE
;
1490 if (!elf_search_loader(pcs
, &elf_info
)) return FALSE
;
1491 elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_loader
= 1;
1492 module_set_module(elf_info
.module
, S_WineLoaderW
);
1493 return (pcs
->dbg_hdr_addr
= elf_info
.dbg_hdr_addr
) != 0;
1496 struct elf_enum_user
1502 static BOOL
elf_enum_modules_translate(const WCHAR
* name
, unsigned long load_addr
,
1503 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1505 struct elf_enum_user
* eeu
= user
;
1506 return eeu
->cb(name
, load_addr
, eeu
->user
);
1509 /******************************************************************
1512 * Enumerates the ELF loaded modules from a running target (hProc)
1513 * This function doesn't require that someone has called SymInitialize
1514 * on this very process.
1516 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1519 struct elf_info elf_info
;
1521 struct elf_enum_user eeu
;
1523 memset(&pcs
, 0, sizeof(pcs
));
1525 elf_info
.flags
= ELF_INFO_DEBUG_HEADER
| ELF_INFO_NAME
;
1526 if (!elf_search_loader(&pcs
, &elf_info
)) return FALSE
;
1527 pcs
.dbg_hdr_addr
= elf_info
.dbg_hdr_addr
;
1530 ret
= elf_enum_modules_internal(&pcs
, elf_info
.module_name
, elf_enum_modules_translate
, &eeu
);
1531 HeapFree(GetProcessHeap(), 0, (char*)elf_info
.module_name
);
1537 struct process
* pcs
;
1538 struct elf_info elf_info
;
1543 /******************************************************************
1546 * Callback for elf_load_module, used to walk the list of loaded
1549 static BOOL
elf_load_cb(const WCHAR
* name
, unsigned long load_addr
,
1550 unsigned long dyn_addr
, BOOL is_system
, void* user
)
1552 struct elf_load
* el
= user
;
1556 if (is_system
) /* virtual ELF module, created by system. handle it from memory */
1558 struct module
* module
;
1559 struct elf_map_file_data emfd
;
1560 struct image_file_map fmap
;
1562 if ((module
= module_is_already_loaded(el
->pcs
, name
)))
1564 el
->elf_info
.module
= module
;
1565 el
->elf_info
.module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 1;
1566 return module
->module
.SymType
;
1569 emfd
.kind
= from_process
;
1570 emfd
.u
.process
.handle
= el
->pcs
->handle
;
1571 emfd
.u
.process
.load_addr
= (void*)load_addr
;
1573 if (elf_map_file(&emfd
, &fmap
))
1574 el
->ret
= elf_load_file_from_fmap(el
->pcs
, name
, &fmap
, load_addr
, 0, &el
->elf_info
);
1579 /* memcmp is needed for matches when bufstr contains also version information
1580 * el->name: libc.so, name: libc.so.6.0
1582 p
= strrchrW(name
, '/');
1586 if (!el
->name
|| !memcmp(p
, el
->name
, lstrlenW(el
->name
) * sizeof(WCHAR
)))
1588 el
->ret
= elf_search_and_load_file(el
->pcs
, name
, load_addr
, dyn_addr
, &el
->elf_info
);
1589 if (el
->name
) ret
= FALSE
;
1595 /******************************************************************
1598 * loads an ELF module and stores it in process' module list
1599 * Also, find module real name and load address from
1600 * the real loaded modules list in pcs address space
1602 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1606 TRACE("(%p %s %08lx)\n", pcs
, debugstr_w(name
), addr
);
1608 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1611 if (pcs
->dbg_hdr_addr
) /* we're debugging a life target */
1614 /* do only the lookup from the filename, not the path (as we lookup module
1615 * name in the process' loaded module list)
1617 el
.name
= strrchrW(name
, '/');
1618 if (!el
.name
++) el
.name
= name
;
1621 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1627 el
.ret
= elf_search_and_load_file(pcs
, el
.name
, addr
, 0, &el
.elf_info
);
1629 if (!el
.ret
) return NULL
;
1630 assert(el
.elf_info
.module
);
1631 return el
.elf_info
.module
;
1634 /******************************************************************
1635 * elf_synchronize_module_list
1637 * this functions rescans the debuggee module's list and synchronizes it with
1638 * the one from 'pcs', ie:
1639 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1640 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1642 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1644 struct module
* module
;
1647 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1649 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1650 module
->format_info
[DFI_ELF
]->u
.elf_info
->elf_mark
= 0;
1654 el
.elf_info
.flags
= ELF_INFO_MODULE
;
1656 el
.name
= NULL
; /* fetch all modules */
1658 if (!elf_enum_modules_internal(pcs
, NULL
, elf_load_cb
, &el
))
1661 module
= pcs
->lmodules
;
1664 if (module
->type
== DMT_ELF
&& !module
->is_virtual
)
1666 struct elf_module_info
* elf_info
= module
->format_info
[DFI_ELF
]->u
.elf_info
;
1668 if (!elf_info
->elf_mark
&& !elf_info
->elf_loader
)
1670 module_remove(pcs
, module
);
1671 /* restart all over */
1672 module
= pcs
->lmodules
;
1676 module
= module
->next
;
1681 #else /* !__ELF__ */
1683 BOOL
elf_find_section(struct image_file_map
* fmap
, const char* name
,
1684 unsigned sht
, struct image_section_map
* ism
)
1689 const char* elf_map_section(struct image_section_map
* ism
)
1694 void elf_unmap_section(struct image_section_map
* ism
)
1697 unsigned elf_get_map_size(const struct image_section_map
* ism
)
1702 DWORD_PTR
elf_get_map_rva(const struct image_section_map
* ism
)
1707 BOOL
elf_synchronize_module_list(struct process
* pcs
)
1712 BOOL
elf_fetch_file_info(const WCHAR
* name
, DWORD_PTR
* base
,
1713 DWORD
* size
, DWORD
* checksum
)
1718 BOOL
elf_read_wine_loader_dbg_info(struct process
* pcs
)
1723 BOOL
elf_enum_modules(HANDLE hProc
, enum_modules_cb cb
, void* user
)
1728 struct module
* elf_load_module(struct process
* pcs
, const WCHAR
* name
, unsigned long addr
)
1733 BOOL
elf_load_debug_info(struct module
* module
)
1738 int elf_is_in_thunk_area(unsigned long addr
,
1739 const struct elf_thunk_area
* thunks
)
1743 #endif /* __ELF__ */