Bug 596580: Fix mozJSSubScriptLoader's version finding. (r=brendan)
[mozilla-central.git] / tools / jprof / elf.cpp
blobf89e691035a07b6b30f14ab51af4e3c731048f23
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 1998
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s):
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK ***** */
36 #include "leaky.h"
38 #ifdef USE_ELF
40 #include "leaky.h"
41 #include <stdio.h>
42 #include <malloc.h>
43 #include <libelf/libelf.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <string.h>
48 void leaky::readSymbols(const char *fileName)
50 int fd = ::open(fileName, O_RDONLY);
51 if (fd < 0) {
52 fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName,
53 fileName);
54 exit(-1);
57 elf_version(EV_CURRENT);
58 Elf *elf = elf_begin(fd, ELF_C_READ, 0);
59 if (!elf) {
60 fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName,
61 fileName);
62 exit(-1);
65 long alloced = 10000;
66 Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000);
67 Symbol* sp = syms;
68 Symbol* last = syms + alloced;
70 // Get each of the relevant sections and add them to the list of
71 // symbols.
72 Elf32_Ehdr *ehdr = elf32_getehdr(elf);
73 if (!ehdr) {
74 fprintf(stderr, "%s: elf library lossage\n", applicationName);
75 exit(-1);
77 #if 0
78 Elf32_Half ndx = ehdr->e_shstrndx;
79 #endif
81 Elf_Scn *scn = 0;
82 int strtabndx = -1;
83 for (int i = 1; (scn = elf_nextscn(elf, scn)) != 0; i++) {
84 Elf32_Shdr *shdr = elf32_getshdr(scn);
85 #if 0
86 char *name = elf_strptr(elf, ndx, (size_t) shdr->sh_name);
87 printf("Section %s (%d 0x%x)\n", name ? name : "(null)",
88 shdr->sh_type, shdr->sh_type);
89 #endif
90 if (shdr->sh_type == SHT_STRTAB) {
91 /* We assume here that string tables preceed symbol tables... */
92 strtabndx = i;
93 continue;
95 #if 0
96 if (shdr->sh_type == SHT_DYNAMIC) {
97 /* Dynamic */
98 Elf_Data *data = elf_getdata(scn, 0);
99 if (!data || !data->d_size) {
100 printf("No data...");
101 continue;
104 Elf32_Dyn *dyn = (Elf32_Dyn*) data->d_buf;
105 Elf32_Dyn *lastdyn =
106 (Elf32_Dyn*) ((char*) data->d_buf + data->d_size);
107 for (; dyn < lastdyn; dyn++) {
108 printf("tag=%d value=0x%x\n", dyn->d_tag, dyn->d_un.d_val);
110 } else
111 #endif
112 if ((shdr->sh_type == SHT_SYMTAB) ||
113 (shdr->sh_type == SHT_DYNSYM)) {
114 /* Symbol table */
115 Elf_Data *data = elf_getdata(scn, 0);
116 if (!data || !data->d_size) {
117 printf("No data...");
118 continue;
121 /* In theory we now have the symbols... */
122 Elf32_Sym *esym = (Elf32_Sym*) data->d_buf;
123 Elf32_Sym *lastsym =
124 (Elf32_Sym*) ((char*) data->d_buf + data->d_size);
125 for (; esym < lastsym; esym++) {
126 #if 0
127 char *nm = elf_strptr(elf, strtabndx, (size_t)esym->st_name);
128 printf("%20s 0x%08x %02x %02x\n",
129 nm, esym->st_value, ELF32_ST_BIND(esym->st_info),
130 ELF32_ST_TYPE(esym->st_info));
131 #endif
132 if ((esym->st_value == 0) ||
133 (ELF32_ST_BIND(esym->st_info) == STB_WEAK) ||
134 (ELF32_ST_BIND(esym->st_info) == STB_NUM) ||
135 (ELF32_ST_TYPE(esym->st_info) != STT_FUNC)) {
136 continue;
138 #if 1
139 char *nm = elf_strptr(elf, strtabndx, (size_t)esym->st_name);
140 #endif
141 sp->name = nm ? strdup(nm) : "(no name)";
142 sp->address = esym->st_value;
143 sp++;
144 if (sp >= last) {
145 long n = alloced + 10000;
146 syms = (Symbol*)
147 realloc(syms, (size_t) (sizeof(Symbol) * n));
148 last = syms + n;
149 sp = syms + alloced;
150 alloced = n;
156 int interesting = sp - syms;
157 if (!quiet) {
158 printf("Total of %d symbols\n", interesting);
160 usefulSymbols = interesting;
161 externalSymbols = syms;
164 #endif /* USE_ELF */