michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "leaky.h" michael@0: michael@0: #ifdef USE_ELF michael@0: michael@0: #include "leaky.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: void leaky::readSymbols(const char *fileName) michael@0: { michael@0: int fd = ::open(fileName, O_RDONLY); michael@0: if (fd < 0) { michael@0: fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName, michael@0: fileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: elf_version(EV_CURRENT); michael@0: Elf *elf = elf_begin(fd, ELF_C_READ, 0); michael@0: if (!elf) { michael@0: fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName, michael@0: fileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: long alloced = 10000; michael@0: Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000); michael@0: Symbol* sp = syms; michael@0: Symbol* last = syms + alloced; michael@0: michael@0: // Get each of the relevant sections and add them to the list of michael@0: // symbols. michael@0: Elf32_Ehdr *ehdr = elf32_getehdr(elf); michael@0: if (!ehdr) { michael@0: fprintf(stderr, "%s: elf library lossage\n", applicationName); michael@0: exit(-1); michael@0: } michael@0: #if 0 michael@0: Elf32_Half ndx = ehdr->e_shstrndx; michael@0: #endif michael@0: michael@0: Elf_Scn *scn = 0; michael@0: int strtabndx = -1; michael@0: for (int i = 1; (scn = elf_nextscn(elf, scn)) != 0; i++) { michael@0: Elf32_Shdr *shdr = elf32_getshdr(scn); michael@0: #if 0 michael@0: char *name = elf_strptr(elf, ndx, (size_t) shdr->sh_name); michael@0: printf("Section %s (%d 0x%x)\n", name ? name : "(null)", michael@0: shdr->sh_type, shdr->sh_type); michael@0: #endif michael@0: if (shdr->sh_type == SHT_STRTAB) { michael@0: /* We assume here that string tables preceed symbol tables... */ michael@0: strtabndx = i; michael@0: continue; michael@0: } michael@0: #if 0 michael@0: if (shdr->sh_type == SHT_DYNAMIC) { michael@0: /* Dynamic */ michael@0: Elf_Data *data = elf_getdata(scn, 0); michael@0: if (!data || !data->d_size) { michael@0: printf("No data..."); michael@0: continue; michael@0: } michael@0: michael@0: Elf32_Dyn *dyn = (Elf32_Dyn*) data->d_buf; michael@0: Elf32_Dyn *lastdyn = michael@0: (Elf32_Dyn*) ((char*) data->d_buf + data->d_size); michael@0: for (; dyn < lastdyn; dyn++) { michael@0: printf("tag=%d value=0x%x\n", dyn->d_tag, dyn->d_un.d_val); michael@0: } michael@0: } else michael@0: #endif michael@0: if ((shdr->sh_type == SHT_SYMTAB) || michael@0: (shdr->sh_type == SHT_DYNSYM)) { michael@0: /* Symbol table */ michael@0: Elf_Data *data = elf_getdata(scn, 0); michael@0: if (!data || !data->d_size) { michael@0: printf("No data..."); michael@0: continue; michael@0: } michael@0: michael@0: /* In theory we now have the symbols... */ michael@0: Elf32_Sym *esym = (Elf32_Sym*) data->d_buf; michael@0: Elf32_Sym *lastsym = michael@0: (Elf32_Sym*) ((char*) data->d_buf + data->d_size); michael@0: for (; esym < lastsym; esym++) { michael@0: #if 0 michael@0: char *nm = elf_strptr(elf, strtabndx, (size_t)esym->st_name); michael@0: printf("%20s 0x%08x %02x %02x\n", michael@0: nm, esym->st_value, ELF32_ST_BIND(esym->st_info), michael@0: ELF32_ST_TYPE(esym->st_info)); michael@0: #endif michael@0: if ((esym->st_value == 0) || michael@0: (ELF32_ST_BIND(esym->st_info) == STB_WEAK) || michael@0: (ELF32_ST_BIND(esym->st_info) == STB_NUM) || michael@0: (ELF32_ST_TYPE(esym->st_info) != STT_FUNC)) { michael@0: continue; michael@0: } michael@0: #if 1 michael@0: char *nm = elf_strptr(elf, strtabndx, (size_t)esym->st_name); michael@0: #endif michael@0: sp->name = nm ? strdup(nm) : "(no name)"; michael@0: sp->address = esym->st_value; michael@0: sp++; michael@0: if (sp >= last) { michael@0: long n = alloced + 10000; michael@0: syms = (Symbol*) michael@0: realloc(syms, (size_t) (sizeof(Symbol) * n)); michael@0: last = syms + n; michael@0: sp = syms + alloced; michael@0: alloced = n; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: int interesting = sp - syms; michael@0: if (!quiet) { michael@0: printf("Total of %d symbols\n", interesting); michael@0: } michael@0: usefulSymbols = interesting; michael@0: externalSymbols = syms; michael@0: } michael@0: michael@0: #endif /* USE_ELF */