2 * File elf.c - processing of ELF files
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifdef HAVE_SYS_MMAN_H
35 #define PATH_MAX MAX_PATH
40 #if defined(__svr4__) || defined(__sun)
47 #ifdef HAVE_SYS_ELF32_H
48 # include <sys/elf32.h>
50 #ifdef HAVE_SYS_EXEC_ELF_H
51 # include <sys/exec_elf.h>
54 # if defined(DT_COUNT)
55 # define DT_NUM DT_COUNT
57 /* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
64 #ifdef HAVE_SYS_LINK_H
65 # include <sys/link.h>
68 #include "wine/debug.h"
70 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
72 typedef struct tagELF_DBG_INFO
80 * Walk through the entire symbol table and add any symbols we find there.
81 * This can be used in cases where we have stripped ELF shared libraries,
82 * or it can be used in cases where we have data symbols for which the address
83 * isn't encoded in the stabs.
85 * This is all really quite easy, since we don't have to worry about line
86 * numbers or local data variables.
88 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, const char* addr
,
89 void *load_addr
, const Elf32_Shdr
* symtab
,
90 const Elf32_Shdr
* strtab
)
92 const char* curfile
= NULL
;
93 struct name_hash
* curr_sym
= NULL
;
100 const Elf32_Sym
* symp
;
102 symp
= (Elf32_Sym
*)(addr
+ symtab
->sh_offset
);
103 nsym
= symtab
->sh_size
/ sizeof(*symp
);
104 strp
= (char *)(addr
+ strtab
->sh_offset
);
106 for (i
= 0; i
< nsym
; i
++, symp
++)
109 * Ignore certain types of entries which really aren't of that much
112 if (ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
113 symp
->st_shndx
== SHN_UNDEF
)
118 symname
= strp
+ symp
->st_name
;
121 * Save the name of the current file, so we have a way of tracking
122 * static functions/data.
124 if (ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
130 new_value
.type
= NULL
;
131 new_value
.addr
.seg
= 0;
132 new_value
.addr
.off
= (unsigned long)load_addr
+ symp
->st_value
;
133 new_value
.cookie
= DV_TARGET
;
134 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
135 ? SYM_FUNC
: SYM_DATA
);
136 if (ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
137 curr_sym
= DEBUG_AddSymbol(symname
, &new_value
, NULL
, flags
);
139 curr_sym
= DEBUG_AddSymbol(symname
, &new_value
, curfile
, flags
);
142 * Record the size of the symbol. This can come in handy in
143 * some cases. Not really used yet, however.
145 if (symp
->st_size
!= 0)
146 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
153 * Loads the symbolic information from ELF module stored in 'filename'
154 * the module has been loaded at 'load_offset' address, so symbols' address
155 * relocation is performed
157 * -1 if the file cannot be found/opened
158 * 0 if the file doesn't contain symbolic info (or this info cannot be
162 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
164 enum DbgInfoLoad dil
= DIL_ERROR
;
165 char* addr
= (char*)0xffffffff;
168 const Elf32_Ehdr
* ehptr
;
169 const Elf32_Shdr
* spnt
;
170 const char* shstrtab
;
172 int stabsect
, stabstrsect
, debugsect
;
174 if (module
->type
!= DMT_ELF
|| !module
->elf_dbg_info
)
176 WINE_ERR("Bad elf module '%s'\n", module
->module_name
);
180 /* check that the file exists, and that the module hasn't been loaded yet */
181 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
182 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
185 * Now open the file, so that we can mmap() it.
187 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
191 * Now mmap() the file.
193 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
194 if (addr
== (char*)0xffffffff) goto leave
;
197 * Next, we need to find a few of the internal ELF headers within
198 * this thing. We need the main executable header, and the section
201 ehptr
= (Elf32_Ehdr
*)addr
;
202 spnt
= (Elf32_Shdr
*)(addr
+ ehptr
->e_shoff
);
203 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
205 stabsect
= stabstrsect
= debugsect
= -1;
207 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
209 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
211 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
213 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".debug_info") == 0)
217 if (stabsect
!= -1 && stabstrsect
!= -1)
220 * OK, now just parse all of the stabs.
222 if (DEBUG_ParseStabs(addr
,
223 module
->elf_dbg_info
->elf_addr
,
224 spnt
[stabsect
].sh_offset
,
225 spnt
[stabsect
].sh_size
,
226 spnt
[stabstrsect
].sh_offset
,
227 spnt
[stabstrsect
].sh_size
))
234 WINE_WARN("Couldn't read correctly read stabs\n");
238 else if (debugsect
!= -1)
240 /* Dwarf 2 debug information */
241 dil
= DIL_NOT_SUPPORTED
;
243 /* now load dynamic symbol info */
244 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
246 if ((strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0) &&
247 (spnt
[i
].sh_type
== SHT_SYMTAB
))
248 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_dbg_info
->elf_addr
,
249 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
251 if ((strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0) &&
252 (spnt
[i
].sh_type
== SHT_DYNSYM
))
253 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_dbg_info
->elf_addr
,
254 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
258 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
259 if (fd
!= -1) close(fd
);
264 static unsigned is_dt_flag_valid(unsigned d_tag
)
270 #define DT_EXTRANUM 0
272 return (d_tag
>= 0 && d_tag
< DT_NUM
+ DT_PROCNUM
+ DT_EXTRANUM
)
273 #if defined(DT_LOOS) && defined(DT_HIOS)
274 || (d_tag
>= DT_LOOS
&& d_tag
< DT_HIOS
)
276 #if defined(DT_LOPROC) && defined(DT_HIPROC)
277 || (d_tag
>= DT_LOPROC
&& d_tag
< DT_HIPROC
)
283 * Loads the information for ELF module stored in 'filename'
284 * the module has been loaded at 'load_offset' address
286 * -1 if the file cannot be found/opened
287 * 0 if the file doesn't contain symbolic info (or this info cannot be
291 static enum DbgInfoLoad
DEBUG_ProcessElfFile(HANDLE hProcess
,
292 const char* filename
,
294 struct elf_info
* elf_info
)
296 static const unsigned char elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
297 enum DbgInfoLoad dil
= DIL_ERROR
;
298 const char* addr
= (char*)0xffffffff;
301 const Elf32_Ehdr
* ehptr
;
302 const Elf32_Shdr
* spnt
;
303 const Elf32_Phdr
* ppnt
;
304 const char* shstrtab
;
308 WINE_TRACE("Processing elf file '%s' at %p\n", filename
, load_offset
);
310 /* check that the file exists, and that the module hasn't been loaded yet */
311 if (stat(filename
, &statbuf
) == -1) goto leave
;
314 * Now open the file, so that we can mmap() it.
316 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
319 * Now mmap() the file.
321 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
322 if (addr
== (char*)-1) goto leave
;
327 * Next, we need to find a few of the internal ELF headers within
328 * this thing. We need the main executable header, and the section
331 ehptr
= (Elf32_Ehdr
*)addr
;
332 if (memcmp(ehptr
->e_ident
, elf_signature
, sizeof(elf_signature
))) goto leave
;
334 spnt
= (Elf32_Shdr
*)(addr
+ ehptr
->e_shoff
);
335 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
337 /* if non relocatable ELF, then remove fixed address from computation
338 * otherwise, all addresses are zero based
340 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
342 /* grab size of module once loaded in memory */
343 ppnt
= (Elf32_Phdr
*)(addr
+ ehptr
->e_phoff
);
345 for (i
= 0; i
< ehptr
->e_phnum
; i
++)
347 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
348 elf_info
->size
+= (ppnt
[i
].p_align
<= 1) ? ppnt
[i
].p_memsz
:
349 (ppnt
[i
].p_memsz
+ ppnt
[i
].p_align
- 1) & ~(ppnt
[i
].p_align
- 1);
352 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
354 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
355 spnt
[i
].sh_type
== SHT_DYNAMIC
)
357 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
360 char* ptr
= (char*)spnt
[i
].sh_addr
;
365 if (!ReadProcessMemory(hProcess
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
366 len
!= sizeof(dyn
) || !is_dt_flag_valid(dyn
.d_tag
))
369 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
370 if (dyn
.d_tag
== DT_NULL
)
375 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
380 elf_info
->segments
[0] = elf_info
->segments
[1] = elf_info
->segments
[2] = 0;
381 if (elf_info
->flags
& ELF_INFO_PATH
)
383 strncpy(elf_info
->elf_path
, filename
, elf_info
->elf_path_len
);
384 elf_info
->elf_path
[elf_info
->elf_path_len
- 1] = '\0';
387 elf_info
->load_addr
= (load_offset
== 0) ? (void *)ehptr
->e_entry
: load_offset
;
389 if (elf_info
->flags
& ELF_INFO_MODULE
)
393 module
= DEBUG_AddModule(filename
, DMT_ELF
, elf_info
->load_addr
, elf_info
->size
, 0);
396 if ((module
->elf_dbg_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
)
401 module
->elf_dbg_info
->elf_addr
= load_offset
;
402 module
->dil
= dil
= DEBUG_LoadElfStabs(module
);
404 else dil
= DIL_ERROR
;
408 if (addr
!= (char*)0xffffffff) munmap((void*)addr
, statbuf
.st_size
);
409 if (fd
!= -1) close(fd
);
414 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(HANDLE hProcess
,
415 const char * filename
,
418 struct elf_info
* elf_info
)
420 enum DbgInfoLoad dil
= DIL_ERROR
;
424 if (!path
) return dil
;
426 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
430 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
434 strcat(fn
, filename
);
435 dil
= DEBUG_ProcessElfFile(hProcess
, fn
, load_offset
, elf_info
);
437 if (dil
!= DIL_ERROR
) break;
438 s
= (t
) ? (t
+1) : NULL
;
445 static enum DbgInfoLoad
DEBUG_ProcessElfObject(HANDLE hProcess
,
446 const char* filename
,
448 struct elf_info
* elf_info
)
450 enum DbgInfoLoad dil
= DIL_ERROR
;
452 if (filename
== NULL
|| *filename
== '\0') return DIL_ERROR
;
453 if (DEBUG_FindModuleByName(filename
, DMT_ELF
))
455 assert(!(elf_info
->flags
& ELF_INFO_PATH
));
459 if (strstr(filename
, "libstdc++")) return DIL_ERROR
; /* We know we can't do it */
460 dil
= DEBUG_ProcessElfFile(hProcess
, filename
, load_offset
, elf_info
);
461 /* if relative pathname, try some absolute base dirs */
462 if (dil
== DIL_ERROR
&& !strchr(filename
, '/'))
464 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
465 getenv("PATH"), elf_info
);
466 if (dil
== DIL_ERROR
)
467 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
468 getenv("LD_LIBRARY_PATH"), elf_info
);
469 if (dil
== DIL_ERROR
)
470 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
471 getenv("WINEDLLPATH"), elf_info
);
474 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
479 static BOOL
DEBUG_WalkList(const struct r_debug
* dbg_hdr
)
484 struct elf_info elf_info
;
486 elf_info
.flags
= ELF_INFO_MODULE
;
488 * Now walk the linked list. In all known ELF implementations,
489 * the dynamic loader maintains this linked list for us. In some
490 * cases the first entry doesn't appear with a name, in other cases it
493 for (lm_addr
= (void *)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (void *)lm
.l_next
)
495 if (!DEBUG_READ_MEM_VERBOSE(lm_addr
, &lm
, sizeof(lm
)))
498 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
500 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
)))
502 bufstr
[sizeof(bufstr
) - 1] = '\0';
503 DEBUG_ProcessElfObject(DEBUG_CurrProcess
->handle
, bufstr
,
504 (void *)lm
.l_addr
, &elf_info
);
511 static BOOL
DEBUG_RescanElf(void)
513 struct r_debug dbg_hdr
;
515 if (DEBUG_CurrProcess
&&
516 DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
518 switch (dbg_hdr
.r_state
)
521 DEBUG_WalkList(&dbg_hdr
);
522 DEBUG_CheckDelayedBP();
527 /* FIXME: this is not currently handled */
534 /******************************************************************
535 * DEBUG_ReadWineLoaderDbgInfo
537 * Try to find a decent wine executable which could have loader the debuggee
539 enum DbgInfoLoad
DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess
, struct elf_info
* elf_info
)
542 enum DbgInfoLoad dil
;
544 /* All binaries are loaded with WINELOADER (if run from tree) or by the
545 * main executable (either wine-kthread or wine-pthread)
546 * Note: the heuristic used to know whether we need to load wine-pthread or
547 * wine-kthread is not 100% safe
549 elf_info
->flags
|= ELF_INFO_DEBUG_HEADER
;
550 if ((ptr
= getenv("WINELOADER")))
551 dil
= DEBUG_ProcessElfObject(hProcess
, ptr
, 0, elf_info
);
554 if ((dil
= DEBUG_ProcessElfObject(hProcess
, "wine-kthread", 0, elf_info
)) == DIL_ERROR
)
555 dil
= DEBUG_ProcessElfObject(hProcess
, "wine-pthread", 0, elf_info
);
560 /******************************************************************
561 * DEBUG_SetElfSoLoadBreakpoint
563 * Sets a breakpoint to handle .so loading events, so we can add debug info
566 BOOL
DEBUG_SetElfSoLoadBreakpoint(const struct elf_info
* elf_info
)
568 struct r_debug dbg_hdr
;
571 * OK, now dig into the actual tables themselves.
573 if (!DEBUG_READ_MEM_VERBOSE((void*)elf_info
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
576 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
577 DEBUG_CurrProcess
->dbg_hdr_addr
= elf_info
->dbg_hdr_addr
;
583 WINE_TRACE("Setting up a breakpoint on r_brk(%lx)\n",
584 (unsigned long)dbg_hdr
.r_brk
);
586 DEBUG_SetBreakpoints(FALSE
);
588 value
.cookie
= DV_TARGET
;
590 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
591 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
, TRUE
);
592 DEBUG_SetBreakpoints(TRUE
);
595 return DEBUG_WalkList(&dbg_hdr
);
600 enum DbgInfoLoad
DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess
, struct elf_info
* elf_info
)
605 BOOL
DEBUG_SetElfSoLoadBreakpoint(const struct elf_info
* elf_info
)