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)
51 #ifdef HAVE_SYS_LINK_H
52 # include <sys/link.h>
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
60 typedef struct tagELF_DBG_INFO
68 * Walk through the entire symbol table and add any symbols we find there.
69 * This can be used in cases where we have stripped ELF shared libraries,
70 * or it can be used in cases where we have data symbols for which the address
71 * isn't encoded in the stabs.
73 * This is all really quite easy, since we don't have to worry about line
74 * numbers or local data variables.
76 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, const char* addr
,
77 void *load_addr
, const Elf32_Shdr
* symtab
,
78 const Elf32_Shdr
* strtab
)
80 const char* curfile
= NULL
;
81 struct name_hash
* curr_sym
= NULL
;
88 const Elf32_Sym
* symp
;
90 symp
= (Elf32_Sym
*)(addr
+ symtab
->sh_offset
);
91 nsym
= symtab
->sh_size
/ sizeof(*symp
);
92 strp
= (char *)(addr
+ strtab
->sh_offset
);
94 for (i
= 0; i
< nsym
; i
++, symp
++)
97 * Ignore certain types of entries which really aren't of that much
100 if (ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
101 symp
->st_shndx
== STN_UNDEF
)
106 symname
= strp
+ symp
->st_name
;
109 * Save the name of the current file, so we have a way of tracking
110 * static functions/data.
112 if (ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
118 new_value
.type
= NULL
;
119 new_value
.addr
.seg
= 0;
120 new_value
.addr
.off
= (unsigned long)load_addr
+ symp
->st_value
;
121 new_value
.cookie
= DV_TARGET
;
122 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
123 ? SYM_FUNC
: SYM_DATA
);
124 if (ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
125 curr_sym
= DEBUG_AddSymbol(symname
, &new_value
, NULL
, flags
);
127 curr_sym
= DEBUG_AddSymbol(symname
, &new_value
, curfile
, flags
);
130 * Record the size of the symbol. This can come in handy in
131 * some cases. Not really used yet, however.
133 if (symp
->st_size
!= 0)
134 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
141 * Loads the symbolic information from ELF module stored in 'filename'
142 * the module has been loaded at 'load_offset' address, so symbols' address
143 * relocation is performed
145 * -1 if the file cannot be found/opened
146 * 0 if the file doesn't contain symbolic info (or this info cannot be
150 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
152 enum DbgInfoLoad dil
= DIL_ERROR
;
153 char* addr
= (char*)0xffffffff;
156 const Elf32_Ehdr
* ehptr
;
157 const Elf32_Shdr
* spnt
;
158 const char* shstrtab
;
160 int stabsect
, stabstrsect
, debugsect
;
162 if (module
->type
!= DMT_ELF
|| !module
->elf_dbg_info
)
164 WINE_ERR("Bad elf module '%s'\n", module
->module_name
);
168 /* check that the file exists, and that the module hasn't been loaded yet */
169 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
170 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
173 * Now open the file, so that we can mmap() it.
175 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
179 * Now mmap() the file.
181 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
182 if (addr
== (char*)0xffffffff) goto leave
;
185 * Next, we need to find a few of the internal ELF headers within
186 * this thing. We need the main executable header, and the section
189 ehptr
= (Elf32_Ehdr
*)addr
;
190 spnt
= (Elf32_Shdr
*)(addr
+ ehptr
->e_shoff
);
191 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
193 stabsect
= stabstrsect
= debugsect
= -1;
195 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
197 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
199 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
201 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".debug_info") == 0)
205 if (stabsect
!= -1 && stabstrsect
!= -1)
208 * OK, now just parse all of the stabs.
210 if (DEBUG_ParseStabs(addr
,
211 module
->elf_dbg_info
->elf_addr
,
212 spnt
[stabsect
].sh_offset
,
213 spnt
[stabsect
].sh_size
,
214 spnt
[stabstrsect
].sh_offset
,
215 spnt
[stabstrsect
].sh_size
))
222 WINE_WARN("Couldn't read correctly read stabs\n");
226 else if (debugsect
!= -1)
228 /* Dwarf 2 debug information */
229 dil
= DIL_NOT_SUPPORTED
;
231 /* now load dynamic symbol info */
232 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
234 if ((strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0) &&
235 (spnt
[i
].sh_type
== SHT_SYMTAB
))
236 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_dbg_info
->elf_addr
,
237 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
239 if ((strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0) &&
240 (spnt
[i
].sh_type
== SHT_DYNSYM
))
241 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_dbg_info
->elf_addr
,
242 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
246 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
247 if (fd
!= -1) close(fd
);
253 * Loads the information for ELF module stored in 'filename'
254 * the module has been loaded at 'load_offset' address
256 * -1 if the file cannot be found/opened
257 * 0 if the file doesn't contain symbolic info (or this info cannot be
261 static enum DbgInfoLoad
DEBUG_ProcessElfFile(HANDLE hProcess
,
262 const char* filename
,
264 struct elf_info
* elf_info
)
266 static const unsigned char elf_signature
[4] = { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
};
267 enum DbgInfoLoad dil
= DIL_ERROR
;
268 const char* addr
= (char*)0xffffffff;
271 const Elf32_Ehdr
* ehptr
;
272 const Elf32_Shdr
* spnt
;
273 const Elf32_Phdr
* ppnt
;
274 const char* shstrtab
;
278 WINE_TRACE("Processing elf file '%s' at %p\n", filename
, load_offset
);
280 /* check that the file exists, and that the module hasn't been loaded yet */
281 if (stat(filename
, &statbuf
) == -1) goto leave
;
284 * Now open the file, so that we can mmap() it.
286 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
289 * Now mmap() the file.
291 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
292 if (addr
== (char*)-1) goto leave
;
297 * Next, we need to find a few of the internal ELF headers within
298 * this thing. We need the main executable header, and the section
301 ehptr
= (Elf32_Ehdr
*)addr
;
302 if (memcmp(ehptr
->e_ident
, elf_signature
, sizeof(elf_signature
))) goto leave
;
304 spnt
= (Elf32_Shdr
*)(addr
+ ehptr
->e_shoff
);
305 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
307 /* if non relocatable ELF, then remove fixed address from computation
308 * otherwise, all addresses are zero based
310 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
312 /* grab size of module once loaded in memory */
313 ppnt
= (Elf32_Phdr
*)(addr
+ ehptr
->e_phoff
);
315 for (i
= 0; i
< ehptr
->e_phnum
; i
++)
317 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
318 if (elf_info
->size
< ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
)
319 elf_info
->size
= ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
;
322 for (i
= 0; i
< ehptr
->e_shnum
; i
++)
324 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".bss") == 0 &&
325 spnt
[i
].sh_type
== SHT_NOBITS
)
327 if (elf_info
->size
< spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
)
328 elf_info
->size
= spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
;
330 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
331 spnt
[i
].sh_type
== SHT_DYNAMIC
)
333 if (elf_info
->flags
& ELF_INFO_DEBUG_HEADER
)
336 char* ptr
= (char*)spnt
[i
].sh_addr
;
341 if (!ReadProcessMemory(hProcess
, ptr
, &dyn
, sizeof(dyn
), &len
) ||
342 len
!= sizeof(dyn
) ||
344 dyn
.d_tag
< DT_NUM
+DT_PROCNUM
+DT_EXTRANUM
) ||
345 (dyn
.d_tag
>= DT_LOOS
&& dyn
.d_tag
< DT_HIOS
) ||
346 (dyn
.d_tag
>= DT_LOPROC
&& dyn
.d_tag
< DT_HIPROC
))
350 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
351 if (dyn
.d_tag
== DT_NULL
)
356 elf_info
->dbg_hdr_addr
= dyn
.d_un
.d_ptr
;
361 elf_info
->segments
[0] = elf_info
->segments
[1] = elf_info
->segments
[2] = 0;
362 if (elf_info
->flags
& ELF_INFO_PATH
)
364 strncpy(elf_info
->elf_path
, filename
, elf_info
->elf_path_len
);
365 elf_info
->elf_path
[elf_info
->elf_path_len
- 1] = '\0';
368 elf_info
->load_addr
= (load_offset
== 0) ? (void *)ehptr
->e_entry
: load_offset
;
370 if (elf_info
->flags
& ELF_INFO_MODULE
)
374 module
= DEBUG_AddModule(filename
, DMT_ELF
, elf_info
->load_addr
, elf_info
->size
, 0);
377 if ((module
->elf_dbg_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
)
382 module
->elf_dbg_info
->elf_addr
= load_offset
;
383 module
->dil
= dil
= DEBUG_LoadElfStabs(module
);
385 else dil
= DIL_ERROR
;
389 if (addr
!= (char*)0xffffffff) munmap((void*)addr
, statbuf
.st_size
);
390 if (fd
!= -1) close(fd
);
395 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(HANDLE hProcess
,
396 const char * filename
,
399 struct elf_info
* elf_info
)
401 enum DbgInfoLoad dil
= DIL_ERROR
;
405 if (!path
) return -1;
407 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
)
411 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
415 strcat(fn
, filename
);
416 dil
= DEBUG_ProcessElfFile(hProcess
, fn
, load_offset
, elf_info
);
418 if (dil
!= DIL_ERROR
) break;
419 s
= (t
) ? (t
+1) : NULL
;
426 static enum DbgInfoLoad
DEBUG_ProcessElfObject(HANDLE hProcess
,
427 const char* filename
,
429 struct elf_info
* elf_info
)
431 enum DbgInfoLoad dil
= DIL_ERROR
;
433 if (filename
== NULL
) return DIL_ERROR
;
434 if (DEBUG_FindModuleByName(filename
, DMT_ELF
))
436 assert(!(elf_info
->flags
& ELF_INFO_PATH
));
440 if (strstr(filename
, "libstdc++")) return DIL_ERROR
; /* We know we can't do it */
441 dil
= DEBUG_ProcessElfFile(hProcess
, filename
, load_offset
, elf_info
);
442 /* if relative pathname, try some absolute base dirs */
443 if (dil
== DIL_ERROR
&& !strchr(filename
, '/'))
445 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
446 getenv("PATH"), elf_info
);
447 if (dil
== DIL_ERROR
)
448 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
449 getenv("LD_LIBRARY_PATH"), elf_info
);
450 if (dil
== DIL_ERROR
)
451 dil
= DEBUG_ProcessElfFileFromPath(hProcess
, filename
, load_offset
,
452 getenv("WINEDLLPATH"), elf_info
);
455 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
460 static BOOL
DEBUG_WalkList(const struct r_debug
* dbg_hdr
)
465 struct elf_info elf_info
;
467 elf_info
.flags
= ELF_INFO_MODULE
;
469 * Now walk the linked list. In all known ELF implementations,
470 * the dynamic loader maintains this linked list for us. In some
471 * cases the first entry doesn't appear with a name, in other cases it
474 for (lm_addr
= (void *)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (void *)lm
.l_next
)
476 if (!DEBUG_READ_MEM_VERBOSE(lm_addr
, &lm
, sizeof(lm
)))
479 if (lm
.l_prev
!= NULL
&& /* skip first entry, normally debuggee itself */
481 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
)))
483 bufstr
[sizeof(bufstr
) - 1] = '\0';
484 DEBUG_ProcessElfObject(DEBUG_CurrProcess
->handle
, bufstr
,
485 (void *)lm
.l_addr
, &elf_info
);
492 static BOOL
DEBUG_RescanElf(void)
494 struct r_debug dbg_hdr
;
496 if (DEBUG_CurrProcess
&&
497 DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
499 switch (dbg_hdr
.r_state
)
502 DEBUG_WalkList(&dbg_hdr
);
503 DEBUG_CheckDelayedBP();
508 /* FIXME: this is not currently handled */
515 /******************************************************************
516 * DEBUG_ReadWineLoaderDbgInfo
518 * Try to find a decent wine executable which could have loader the debuggee
520 enum DbgInfoLoad
DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess
, struct elf_info
* elf_info
)
523 enum DbgInfoLoad dil
;
525 /* All binaries are loaded with WINELOADER (if run from tree) or by the
526 * main executable (either wine-kthread or wine-pthread)
527 * Note: the heuristic use to know wether we need to load wine-pthread or
528 * wine-kthread is not 100% safe
530 elf_info
->flags
|= ELF_INFO_DEBUG_HEADER
;
531 if ((ptr
= getenv("WINELOADER")))
532 dil
= DEBUG_ProcessElfObject(hProcess
, ptr
, 0, elf_info
);
535 if ((dil
= DEBUG_ProcessElfObject(hProcess
, "wine-kthread", 0, elf_info
)) == DIL_ERROR
)
536 dil
= DEBUG_ProcessElfObject(hProcess
, "wine-pthread", 0, elf_info
);
541 /******************************************************************
542 * DEBUG_SetElfSoLoadBreakpoint
544 * Sets a breakpoint to handle .so loading events, so we can add debug info
547 BOOL
DEBUG_SetElfSoLoadBreakpoint(const struct elf_info
* elf_info
)
549 struct r_debug dbg_hdr
;
552 * OK, now dig into the actual tables themselves.
554 if (!DEBUG_READ_MEM_VERBOSE((void*)elf_info
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
557 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
558 DEBUG_CurrProcess
->dbg_hdr_addr
= elf_info
->dbg_hdr_addr
;
564 WINE_TRACE("Setting up a breakpoint on r_brk(%lx)\n",
565 (unsigned long)dbg_hdr
.r_brk
);
567 DEBUG_SetBreakpoints(FALSE
);
569 value
.cookie
= DV_TARGET
;
571 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
572 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
, TRUE
);
573 DEBUG_SetBreakpoints(TRUE
);
576 return DEBUG_WalkList(&dbg_hdr
);
581 enum DbgInfoLoad
DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess
, struct elf_info
* elf_info
)
586 BOOL
DEBUG_SetElfSoLoadBreakpoint(const struct elf_info
* elf_info
)