Make it possible to add auxiliary values to the existing array; this
[wine.git] / programs / winedbg / elf.c
blobfa29a537fde92a0314256c1db6c998618ad1515f
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 HAVE_ELF_H
45 # include <elf.h>
46 #endif
47 #ifdef HAVE_SYS_ELF32_H
48 # include <sys/elf32.h>
49 #endif
50 #ifdef HAVE_SYS_EXEC_ELF_H
51 # include <sys/exec_elf.h>
52 #endif
53 #if !defined(DT_NUM)
54 # if defined(DT_COUNT)
55 # define DT_NUM DT_COUNT
56 # else
57 /* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
58 # define DT_NUM 24
59 # endif
60 #endif
61 #ifdef HAVE_LINK_H
62 # include <link.h>
63 #endif
64 #ifdef HAVE_SYS_LINK_H
65 # include <sys/link.h>
66 #endif
68 #include "wine/debug.h"
70 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
72 typedef struct tagELF_DBG_INFO
74 void *elf_addr;
75 } ELF_DBG_INFO;
77 #ifdef __ELF__
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;
94 int flags;
95 int i;
96 DBG_VALUE new_value;
97 int nsym;
98 const char* strp;
99 const char* symname;
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
110 * interest.
112 if (ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
113 symp->st_shndx == SHN_UNDEF)
115 continue;
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)
126 curfile = symname;
127 continue;
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);
138 else
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);
149 return TRUE;
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
156 * returns
157 * -1 if the file cannot be found/opened
158 * 0 if the file doesn't contain symbolic info (or this info cannot be
159 * read or parsed)
160 * 1 on success
162 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
164 enum DbgInfoLoad dil = DIL_ERROR;
165 char* addr = (char*)0xffffffff;
166 int fd = -1;
167 struct stat statbuf;
168 const Elf32_Ehdr* ehptr;
169 const Elf32_Shdr* spnt;
170 const char* shstrtab;
171 int i;
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);
177 return DIL_ERROR;
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;
189 dil = DIL_NOINFO;
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
199 * table.
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)
210 stabsect = i;
211 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
212 stabstrsect = i;
213 if (strcmp(shstrtab + spnt[i].sh_name, ".debug_info") == 0)
214 debugsect = i;
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))
229 dil = DIL_LOADED;
231 else
233 dil = DIL_ERROR;
234 WINE_WARN("Couldn't read correctly read stabs\n");
235 goto leave;
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);
257 leave:
258 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
259 if (fd != -1) close(fd);
261 return dil;
264 static unsigned is_dt_flag_valid(unsigned d_tag)
266 #ifndef DT_PROCNUM
267 #define DT_PROCNUM 0
268 #endif
269 #ifndef DT_EXTRANUM
270 #define DT_EXTRANUM 0
271 #endif
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)
275 #endif
276 #if defined(DT_LOPROC) && defined(DT_HIPROC)
277 || (d_tag >= DT_LOPROC && d_tag < DT_HIPROC)
278 #endif
283 * Loads the information for ELF module stored in 'filename'
284 * the module has been loaded at 'load_offset' address
285 * returns
286 * -1 if the file cannot be found/opened
287 * 0 if the file doesn't contain symbolic info (or this info cannot be
288 * read or parsed)
289 * 1 on success
291 static enum DbgInfoLoad DEBUG_ProcessElfFile(HANDLE hProcess,
292 const char* filename,
293 void *load_offset,
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;
299 int fd = -1;
300 struct stat statbuf;
301 const Elf32_Ehdr* ehptr;
302 const Elf32_Shdr* spnt;
303 const Elf32_Phdr* ppnt;
304 const char* shstrtab;
305 int i;
306 DWORD delta;
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;
324 dil = DIL_NOINFO;
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
329 * table.
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);
344 elf_info->size = 0;
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)
359 Elf32_Dyn dyn;
360 char* ptr = (char*)spnt[i].sh_addr;
361 unsigned long len;
365 if (!ReadProcessMemory(hProcess, ptr, &dyn, sizeof(dyn), &len) ||
366 len != sizeof(dyn) || !is_dt_flag_valid(dyn.d_tag))
367 dyn.d_tag = DT_NULL;
368 ptr += sizeof(dyn);
369 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
370 if (dyn.d_tag == DT_NULL)
372 dil = DIL_ERROR;
373 goto leave;
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)
391 DBG_MODULE* module;
393 module = DEBUG_AddModule(filename, DMT_ELF, elf_info->load_addr, elf_info->size, 0);
394 if (module)
396 if ((module->elf_dbg_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL)
398 WINE_ERR("OOM\n");
399 exit(0);
401 module->elf_dbg_info->elf_addr = load_offset;
402 module->dil = dil = DEBUG_LoadElfStabs(module);
404 else dil = DIL_ERROR;
407 leave:
408 if (addr != (char*)0xffffffff) munmap((void*)addr, statbuf.st_size);
409 if (fd != -1) close(fd);
411 return dil;
414 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(HANDLE hProcess,
415 const char * filename,
416 void *load_offset,
417 const char* path,
418 struct elf_info* elf_info)
420 enum DbgInfoLoad dil = DIL_ERROR;
421 char *s, *t, *fn;
422 char* paths = NULL;
424 if (!path) return dil;
426 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL)
428 t = strchr(s, ':');
429 if (t) *t = '\0';
430 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
431 if (!fn) break;
432 strcpy(fn, s );
433 strcat(fn, "/");
434 strcat(fn, filename);
435 dil = DEBUG_ProcessElfFile(hProcess, fn, load_offset, elf_info);
436 DBG_free(fn);
437 if (dil != DIL_ERROR) break;
438 s = (t) ? (t+1) : NULL;
441 DBG_free(paths);
442 return dil;
445 static enum DbgInfoLoad DEBUG_ProcessElfObject(HANDLE hProcess,
446 const char* filename,
447 void *load_offset,
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));
456 return DIL_LOADED;
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);
476 return dil;
479 static BOOL DEBUG_WalkList(const struct r_debug* dbg_hdr)
481 void* lm_addr;
482 struct link_map lm;
483 char bufstr[256];
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
491 * does.
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)))
496 return FALSE;
498 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
499 lm.l_name != NULL &&
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);
508 return TRUE;
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)
520 case RT_CONSISTENT:
521 DEBUG_WalkList(&dbg_hdr);
522 DEBUG_CheckDelayedBP();
523 break;
524 case RT_ADD:
525 break;
526 case RT_DELETE:
527 /* FIXME: this is not currently handled */
528 break;
531 return FALSE;
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)
541 const char* ptr;
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);
552 else
554 if ((dil = DEBUG_ProcessElfObject(hProcess, "wine-kthread", 0, elf_info)) == DIL_ERROR)
555 dil = DEBUG_ProcessElfObject(hProcess, "wine-pthread", 0, elf_info);
557 return dil;
560 /******************************************************************
561 * DEBUG_SetElfSoLoadBreakpoint
563 * Sets a breakpoint to handle .so loading events, so we can add debug info
564 * on the fly
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)))
574 return FALSE;
576 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
577 DEBUG_CurrProcess->dbg_hdr_addr = elf_info->dbg_hdr_addr;
579 if (dbg_hdr.r_brk)
581 DBG_VALUE value;
583 WINE_TRACE("Setting up a breakpoint on r_brk(%lx)\n",
584 (unsigned long)dbg_hdr.r_brk);
586 DEBUG_SetBreakpoints(FALSE);
587 value.type = NULL;
588 value.cookie = DV_TARGET;
589 value.addr.seg = 0;
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);
598 #else /* !__ELF__ */
600 enum DbgInfoLoad DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess, struct elf_info* elf_info)
602 return DIL_ERROR;
605 BOOL DEBUG_SetElfSoLoadBreakpoint(const struct elf_info* elf_info)
607 return FALSE;
610 #endif /* __ELF__ */