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
29 #ifdef HAVE_SYS_MMAN_H
36 #define PATH_MAX MAX_PATH
39 #include "dbghelp_private.h"
41 #if defined(__svr4__) || defined(__sun)
48 #ifdef HAVE_SYS_ELF32_H
49 # include <sys/elf32.h>
51 #ifdef HAVE_SYS_EXEC_ELF_H
52 # include <sys/exec_elf.h>
55 # if defined(DT_COUNT)
56 # define DT_NUM DT_COUNT
58 /* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
65 #ifdef HAVE_SYS_LINK_H
66 # include <sys/link.h>
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
73 typedef struct tagELF_DBG_INFO
75 unsigned long elf_addr;
80 #define ELF_INFO_DEBUG_HEADER 0x0001
81 #define ELF_INFO_MODULE 0x0002
85 unsigned flags; /* IN one (or several) of the ELF_INFO constants */
86 unsigned long dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
87 struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
90 /******************************************************************
93 * Walk through the entire symbol table and add any symbols we find there.
94 * This can be used in cases where we have stripped ELF shared libraries,
95 * or it can be used in cases where we have data symbols for which the address
96 * isn't encoded in the stabs.
98 * This is all really quite easy, since we don't have to worry about line
99 * numbers or local data variables.
101 static int elf_load_symtab(struct module* module, const char* addr,
102 unsigned long load_addr, const Elf32_Shdr* symtab,
103 const Elf32_Shdr* strtab)
108 const Elf32_Sym* symp;
109 struct symt_compiland* compiland = NULL;
111 symp = (Elf32_Sym*)(addr + symtab->sh_offset);
112 nsym = symtab->sh_size / sizeof(*symp);
113 strp = (char*)(addr + strtab->sh_offset);
115 for (i = 0; i < nsym; i++, symp++)
117 /* Ignore certain types of entries which really aren't of that much
120 if (ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
121 symp->st_shndx == SHN_UNDEF)
126 symname = strp + symp->st_name;
128 if (ELF32_ST_TYPE(symp->st_info) == STT_FILE)
130 compiland = symt_new_compiland(module, symname);
133 symt_new_public(module, compiland, symname,
134 load_addr + symp->st_value, symp->st_size,
135 TRUE /* FIXME */, ELF32_ST_TYPE(symp->st_info) == STT_FUNC);
141 /******************************************************************
142 * elf_load_debug_info
144 * Loads the symbolic information from ELF module stored in 'filename'
145 * the module has been loaded at 'load_offset' address, so symbols' address
146 * relocation is performed
148 * -1 if the file cannot be found/opened
149 * 0 if the file doesn't contain symbolic info (or this info cannot be
153 SYM_TYPE elf_load_debug_info(struct module* module)
155 SYM_TYPE sym_type = -1;
156 char* addr = (char*)0xffffffff;
159 const Elf32_Ehdr* ehptr;
160 const Elf32_Shdr* spnt;
161 const char* shstrtab;
163 int stabsect, stabstrsect, debugsect;
164 int symsect, dynsect;
166 if (module->type != DMT_ELF || !module->elf_dbg_info)
168 ERR("Bad elf module '%s'\n", module->module.LoadedImageName);
172 TRACE("%s\n", module->module.LoadedImageName);
173 /* check that the file exists, and that the module hasn't been loaded yet */
174 if (stat(module->module.LoadedImageName, &statbuf) == -1) goto leave;
175 if (S_ISDIR(statbuf.st_mode)) goto leave;
178 * Now open the file, so that we can mmap() it.
180 if ((fd = open(module->module.LoadedImageName, O_RDONLY)) == -1) goto leave;
183 * Now mmap() the file.
185 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
186 if (addr == (char*)0xffffffff) goto leave;
190 * Next, we need to find a few of the internal ELF headers within
191 * this thing. We need the main executable header, and the section
194 ehptr = (Elf32_Ehdr*)addr;
195 spnt = (Elf32_Shdr*)(addr + ehptr->e_shoff);
196 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
198 symsect = dynsect = stabsect = stabstrsect = debugsect = -1;
200 for (i = 0; i < ehptr->e_shnum; i++)
202 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
204 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
206 if (strcmp(shstrtab + spnt[i].sh_name, ".debug_info") == 0)
208 if ((strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0) &&
209 (spnt[i].sh_type == SHT_SYMTAB))
211 if ((strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0) &&
212 (spnt[i].sh_type == SHT_DYNSYM))
215 /* start loading dynamic symbol info (so that we can get the correct address) */
217 elf_load_symtab(module, addr, module->elf_dbg_info->elf_addr,
218 spnt + symsect, spnt + spnt[symsect].sh_link);
219 else if (dynsect != -1)
220 elf_load_symtab(module, addr, module->elf_dbg_info->elf_addr,
221 spnt + dynsect, spnt + spnt[dynsect].sh_link);
222 sym_type = SymExport;
224 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
226 if (stabsect != -1 && stabstrsect != -1)
228 /* OK, now just parse all of the stabs. */
229 sym_type = stabs_parse(module, addr, module->elf_dbg_info->elf_addr,
230 spnt[stabsect].sh_offset, spnt[stabsect].sh_size,
231 spnt[stabstrsect].sh_offset,
232 spnt[stabstrsect].sh_size);
235 WARN("Couldn't read correctly read stabs\n");
239 else if (debugsect != -1)
241 /* Dwarf 2 debug information */
242 FIXME("Unsupported Dwarf2 information\n");
248 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
249 if (fd != -1) close(fd);
251 return module->module.SymType = sym_type;
254 /******************************************************************
256 * returns true iff the section tag is valid
258 static unsigned is_dt_flag_valid(unsigned d_tag)
264 #define DT_EXTRANUM 0
266 return (d_tag >= 0 && d_tag < DT_NUM + DT_PROCNUM + DT_EXTRANUM)
267 #if defined(DT_LOOS) && defined(DT_HIOS)
268 || (d_tag >= DT_LOOS && d_tag < DT_HIOS)
270 #if defined(DT_LOPROC) && defined(DT_HIPROC)
271 || (d_tag >= DT_LOPROC && d_tag < DT_HIPROC)
276 /******************************************************************
279 * Loads the information for ELF module stored in 'filename'
280 * the module has been loaded at 'load_offset' address
282 * -1 if the file cannot be found/opened
283 * 0 if the file doesn't contain symbolic info (or this info cannot be
287 static SYM_TYPE elf_load_file(struct process* pcs, const char* filename,
288 unsigned long load_offset, struct elf_info* elf_info)
290 static const BYTE elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
291 SYM_TYPE sym_type = -1;
292 const char* addr = (char*)0xffffffff;
295 const Elf32_Ehdr* ehptr;
296 const Elf32_Shdr* spnt;
297 const Elf32_Phdr* ppnt;
298 const char* shstrtab;
302 TRACE("Processing elf file '%s' at %08lx\n", filename, load_offset);
304 /* check that the file exists, and that the module hasn't been loaded yet */
305 if (stat(filename, &statbuf) == -1) goto leave;
307 /* Now open the file, so that we can mmap() it. */
308 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
310 /* Now mmap() the file. */
311 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
312 if (addr == (char*)-1) goto leave;
316 /* Next, we need to find a few of the internal ELF headers within
317 * this thing. We need the main executable header, and the section
320 ehptr = (Elf32_Ehdr*)addr;
321 if (memcmp(ehptr->e_ident, elf_signature, sizeof(elf_signature))) goto leave;
323 spnt = (Elf32_Shdr*)(addr + ehptr->e_shoff);
324 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
326 /* if non relocatable ELF, then remove fixed address from computation
327 * otherwise, all addresses are zero based
329 delta = (load_offset == 0) ? ehptr->e_entry : 0;
331 /* grab size of module once loaded in memory */
332 ppnt = (Elf32_Phdr*)(addr + ehptr->e_phoff);
334 for (i = 0; i < ehptr->e_phnum; i++)
336 if (ppnt[i].p_type == PT_LOAD)
338 size += (ppnt[i].p_align <= 1) ? ppnt[i].p_memsz :
339 (ppnt[i].p_memsz + ppnt[i].p_align - 1) & ~(ppnt[i].p_align - 1);
343 if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
345 for (i = 0; i < ehptr->e_shnum; i++)
347 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
348 spnt[i].sh_type == SHT_DYNAMIC)
351 char* ptr = (char*)spnt[i].sh_addr;
356 if (!ReadProcessMemory(pcs->handle, ptr, &dyn, sizeof(dyn), &len) ||
357 len != sizeof(dyn) || !is_dt_flag_valid(dyn.d_tag))
360 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
361 if (dyn.d_tag == DT_NULL)
366 elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
371 if (elf_info->flags & ELF_INFO_MODULE)
373 elf_info->module = module_new(pcs, filename, DMT_ELF,
374 (load_offset) ? load_offset : ehptr->e_entry,
376 if (elf_info->module)
378 if ((elf_info->module->elf_dbg_info = HeapAlloc(GetProcessHeap(), 0, sizeof(ELF_DBG_INFO))) == NULL)
383 elf_info->module->elf_dbg_info->elf_addr = load_offset;
384 elf_info->module->module.SymType = sym_type =
385 (dbghelp_options & SYMOPT_DEFERRED_LOADS) ? SymDeferred :
386 elf_load_debug_info(elf_info->module);
387 elf_info->module->elf_mark = 1;
393 if (addr != (char*)0xffffffff) munmap((void*)addr, statbuf.st_size);
394 if (fd != -1) close(fd);
399 /******************************************************************
400 * elf_load_file_from_path
401 * tries to load an ELF file from a set of paths (separated by ':')
403 static SYM_TYPE elf_load_file_from_path(HANDLE hProcess,
404 const char* filename,
405 unsigned long load_offset,
407 struct elf_info* elf_info)
409 SYM_TYPE sym_type = -1;
413 if (!path) return sym_type;
415 paths = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(path) + 1), path);
416 for (s = paths; s && *s; s = (t) ? (t+1) : NULL)
420 fn = HeapAlloc(GetProcessHeap(), 0, strlen(filename) + 1 + strlen(s) + 1);
424 strcat(fn, filename);
425 sym_type = elf_load_file(hProcess, fn, load_offset, elf_info);
426 HeapFree(GetProcessHeap(), 0, fn);
427 if (sym_type != -1) break;
428 s = (t) ? (t+1) : NULL;
431 HeapFree(GetProcessHeap(), 0, paths);
435 /******************************************************************
436 * elf_search_and_load_file
438 * lookup a file in standard ELF locations, and if found, load it
440 static SYM_TYPE elf_search_and_load_file(struct process* pcs, const char* filename,
441 unsigned long load_offset, struct elf_info* elf_info)
443 SYM_TYPE sym_type = -1;
444 struct module* module;
446 if (filename == NULL || *filename == '\0') return sym_type;
447 if ((module = module_find_by_name(pcs, filename, DMT_ELF)))
449 elf_info->module = module;
450 module->elf_mark = 1;
451 return module->module.SymType;
454 if (strstr(filename, "libstdc++")) return -1; /* We know we can't do it */
455 sym_type = elf_load_file(pcs, filename, load_offset, elf_info);
456 /* if relative pathname, try some absolute base dirs */
457 if (sym_type == -1 && !strchr(filename, '/'))
459 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
460 getenv("PATH"), elf_info);
462 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
463 getenv("LD_LIBRARY_PATH"), elf_info);
465 sym_type = elf_load_file_from_path(pcs, filename, load_offset,
466 getenv("WINEDLLPATH"), elf_info);
472 /******************************************************************
473 * elf_synchronize_module_list
475 * this functions rescans the debuggee module's list and synchronizes it with
476 * the one from 'pcs', ie:
477 * - if a module is in debuggee and not in pcs, it's loaded into pcs
478 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
480 BOOL elf_synchronize_module_list(struct process* pcs)
482 struct r_debug dbg_hdr;
486 struct elf_info elf_info;
487 struct module* module;
489 if (!pcs->dbg_hdr_addr) return FALSE;
490 if (!read_mem(pcs->handle, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)) ||
491 dbg_hdr.r_state != RT_CONSISTENT)
494 for (module = pcs->lmodules; module; module = module->next)
496 if (module->type == DMT_ELF) module->elf_mark = 0;
499 elf_info.flags = ELF_INFO_MODULE;
500 /* Now walk the linked list. In all known ELF implementations,
501 * the dynamic loader maintains this linked list for us. In some
502 * cases the first entry doesn't appear with a name, in other cases it
505 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
507 if (!read_mem(pcs->handle, (ULONG)lm_addr, &lm, sizeof(lm)))
510 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
512 read_mem(pcs->handle, (ULONG)lm.l_name, bufstr, sizeof(bufstr)))
514 bufstr[sizeof(bufstr) - 1] = '\0';
515 elf_search_and_load_file(pcs, bufstr, (unsigned long)lm.l_addr,
520 for (module = pcs->lmodules; module; module = module->next)
522 if (module->type == DMT_ELF && !module->elf_mark)
524 module_remove(pcs, module);
525 /* restart all over */
526 module = pcs->lmodules;
532 /******************************************************************
533 * elf_read_wine_loader_dbg_info
535 * Try to find a decent wine executable which could have loader the debuggee
537 unsigned long elf_read_wine_loader_dbg_info(struct process* pcs)
541 struct elf_info elf_info;
543 elf_info.flags = ELF_INFO_DEBUG_HEADER;
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 use to know whether we need to load wine-pthread or
547 * wine-kthread is not 100% safe
549 if ((ptr = getenv("WINELOADER")))
550 sym_type = elf_search_and_load_file(pcs, ptr, 0, &elf_info);
553 if ((sym_type = elf_search_and_load_file(pcs, "wine-kthread", 0, &elf_info)) == -1)
554 sym_type = elf_search_and_load_file(pcs, "wine-pthread", 0, &elf_info);
556 return (sym_type < 0) ? 0 : elf_info.dbg_hdr_addr;
559 /******************************************************************
562 * loads an ELF module and stores it in process' module list
563 * if 'sync' is TRUE, let's find module real name and load address from
564 * the real loaded modules list in pcs address space
566 struct module* elf_load_module(struct process* pcs, const char* name)
568 struct elf_info elf_info;
569 SYM_TYPE sym_type = -1;
572 struct r_debug dbg_hdr;
577 TRACE("(%p %s)\n", pcs, name);
579 elf_info.flags = ELF_INFO_MODULE;
581 /* do only the lookup from the filename, not the path (as we lookup module name
582 * in the process' loaded module list)
584 xname = strrchr(name, '/');
585 if (!xname++) xname = name;
587 if (!read_mem(pcs->handle, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)) ||
588 dbg_hdr.r_state != RT_CONSISTENT)
591 for (lm_addr = (void*)dbg_hdr.r_map; lm_addr; lm_addr = (void*)lm.l_next)
593 if (!read_mem(pcs->handle, (ULONG)lm_addr, &lm, sizeof(lm)))
596 if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
598 read_mem(pcs->handle, (ULONG)lm.l_name, bufstr, sizeof(bufstr)))
600 bufstr[sizeof(bufstr) - 1] = '\0';
601 /* memcmp is needed for matches when bufstr contains also version information
602 * name: libc.so, bufstr: libc.so.6.0
604 p = strrchr(bufstr, '/');
605 if (!p++) p = bufstr;
606 if (!memcmp(p, xname, strlen(xname)))
608 sym_type = elf_search_and_load_file(pcs, bufstr,
609 (unsigned long)lm.l_addr, &elf_info);
614 if (!lm_addr || sym_type == -1) return NULL;
615 assert(elf_info.module);
616 return elf_info.module;
621 BOOL elf_synchronize_module_list(struct process* pcs)
626 unsigned long elf_read_wine_loader_dbg_info(struct process* pcs)
631 struct module* elf_load_module(struct process* pcs, const char* name)
636 SYM_TYPE elf_load_debug_info(struct module* module)