- now detecting Dwarf debug information in ELF modules (but don't load
[wine.git] / programs / winedbg / elf.c
blob005e7c81477795b325009f9b1dddeff425b533dd
1 /*
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
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #ifndef PATH_MAX
35 #define PATH_MAX MAX_PATH
36 #endif
38 #include "debugger.h"
40 #if defined(__svr4__) || defined(__sun)
41 #define __ELF__
42 #endif
44 #ifdef __ELF__
45 #ifdef HAVE_ELF_H
46 # include <elf.h>
47 #endif
48 #ifdef HAVE_LINK_H
49 # include <link.h>
50 #endif
51 #ifdef HAVE_SYS_LINK_H
52 # include <sys/link.h>
53 #endif
54 #endif
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
60 typedef struct tagELF_DBG_INFO
62 void *elf_addr;
63 } ELF_DBG_INFO;
65 #ifdef __ELF__
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;
82 int flags;
83 int i;
84 DBG_VALUE new_value;
85 int nsym;
86 const char* strp;
87 const char* symname;
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
98 * interest.
100 if (ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
101 symp->st_shndx == STN_UNDEF)
103 continue;
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)
114 curfile = symname;
115 continue;
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);
126 else
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);
137 return TRUE;
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
144 * returns
145 * -1 if the file cannot be found/opened
146 * 0 if the file doesn't contain symbolic info (or this info cannot be
147 * read or parsed)
148 * 1 on success
150 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
152 enum DbgInfoLoad dil = DIL_ERROR;
153 char* addr = (char*)0xffffffff;
154 int fd = -1;
155 struct stat statbuf;
156 const Elf32_Ehdr* ehptr;
157 const Elf32_Shdr* spnt;
158 const char* shstrtab;
159 int i;
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);
165 return DIL_ERROR;
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;
177 dil = DIL_NOINFO;
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
187 * table.
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)
198 stabsect = i;
199 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
200 stabstrsect = i;
201 if (strcmp(shstrtab + spnt[i].sh_name, ".debug_info") == 0)
202 debugsect = i;
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))
217 dil = DIL_LOADED;
219 else
221 dil = DIL_ERROR;
222 WINE_WARN("Couldn't read correctly read stabs\n");
223 goto leave;
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);
245 leave:
246 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
247 if (fd != -1) close(fd);
249 return dil;
253 * Loads the information for ELF module stored in 'filename'
254 * the module has been loaded at 'load_offset' address
255 * returns
256 * -1 if the file cannot be found/opened
257 * 0 if the file doesn't contain symbolic info (or this info cannot be
258 * read or parsed)
259 * 1 on success
261 static enum DbgInfoLoad DEBUG_ProcessElfFile(HANDLE hProcess,
262 const char* filename,
263 void *load_offset,
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;
269 int fd = -1;
270 struct stat statbuf;
271 const Elf32_Ehdr* ehptr;
272 const Elf32_Shdr* spnt;
273 const Elf32_Phdr* ppnt;
274 const char* shstrtab;
275 int i;
276 DWORD delta;
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;
294 dil = DIL_NOINFO;
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
299 * table.
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);
314 elf_info->size = 0;
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)
335 Elf32_Dyn dyn;
336 char* ptr = (char*)spnt[i].sh_addr;
337 unsigned long len;
341 if (!ReadProcessMemory(hProcess, ptr, &dyn, sizeof(dyn), &len) ||
342 len != sizeof(dyn) ||
343 !((dyn.d_tag >= 0 &&
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))
348 dyn.d_tag = DT_NULL;
349 ptr += sizeof(dyn);
350 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
351 if (dyn.d_tag == DT_NULL)
353 dil = DIL_ERROR;
354 goto leave;
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)
372 DBG_MODULE* module;
374 module = DEBUG_AddModule(filename, DMT_ELF, elf_info->load_addr, elf_info->size, 0);
375 if (module)
377 if ((module->elf_dbg_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL)
379 WINE_ERR("OOM\n");
380 exit(0);
382 module->elf_dbg_info->elf_addr = load_offset;
383 module->dil = dil = DEBUG_LoadElfStabs(module);
385 else dil = DIL_ERROR;
388 leave:
389 if (addr != (char*)0xffffffff) munmap((void*)addr, statbuf.st_size);
390 if (fd != -1) close(fd);
392 return dil;
395 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(HANDLE hProcess,
396 const char * filename,
397 void *load_offset,
398 const char* path,
399 struct elf_info* elf_info)
401 enum DbgInfoLoad dil = DIL_ERROR;
402 char *s, *t, *fn;
403 char* paths = NULL;
405 if (!path) return -1;
407 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL)
409 t = strchr(s, ':');
410 if (t) *t = '\0';
411 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
412 if (!fn) break;
413 strcpy(fn, s );
414 strcat(fn, "/");
415 strcat(fn, filename);
416 dil = DEBUG_ProcessElfFile(hProcess, fn, load_offset, elf_info);
417 DBG_free(fn);
418 if (dil != DIL_ERROR) break;
419 s = (t) ? (t+1) : NULL;
422 DBG_free(paths);
423 return dil;
426 static enum DbgInfoLoad DEBUG_ProcessElfObject(HANDLE hProcess,
427 const char* filename,
428 void *load_offset,
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));
437 return DIL_LOADED;
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);
457 return dil;
460 static BOOL DEBUG_WalkList(const struct r_debug* dbg_hdr)
462 void* lm_addr;
463 struct link_map lm;
464 char bufstr[256];
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
472 * does.
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)))
477 return FALSE;
479 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
480 lm.l_name != NULL &&
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);
489 return TRUE;
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)
501 case RT_CONSISTENT:
502 DEBUG_WalkList(&dbg_hdr);
503 DEBUG_CheckDelayedBP();
504 break;
505 case RT_ADD:
506 break;
507 case RT_DELETE:
508 /* FIXME: this is not currently handled */
509 break;
512 return FALSE;
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)
522 const char* ptr;
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);
533 else
535 if ((dil = DEBUG_ProcessElfObject(hProcess, "wine-kthread", 0, elf_info)) == DIL_ERROR)
536 dil = DEBUG_ProcessElfObject(hProcess, "wine-pthread", 0, elf_info);
538 return dil;
541 /******************************************************************
542 * DEBUG_SetElfSoLoadBreakpoint
544 * Sets a breakpoint to handle .so loading events, so we can add debug info
545 * on the fly
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)))
555 return FALSE;
557 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
558 DEBUG_CurrProcess->dbg_hdr_addr = elf_info->dbg_hdr_addr;
560 if (dbg_hdr.r_brk)
562 DBG_VALUE value;
564 WINE_TRACE("Setting up a breakpoint on r_brk(%lx)\n",
565 (unsigned long)dbg_hdr.r_brk);
567 DEBUG_SetBreakpoints(FALSE);
568 value.type = NULL;
569 value.cookie = DV_TARGET;
570 value.addr.seg = 0;
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);
579 #else /* !__ELF__ */
581 enum DbgInfoLoad DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess, struct elf_info* elf_info)
583 return DIL_ERROR;
586 BOOL DEBUG_SetElfSoLoadBreakpoint(const struct elf_info* elf_info)
588 return FALSE;
591 #endif /* __ELF__ */