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
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 1999
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
44 #include <libelf/libelf.h>
49 void leaky::readSymbols(const char *fileName
)
51 int fd
= ::open(fileName
, O_RDONLY
);
53 fprintf(stderr
, "%s: unable to open \"%s\"\n", applicationName
,
58 elf_version(EV_CURRENT
);
59 Elf
*elf
= elf_begin(fd
, ELF_C_READ
, 0);
61 fprintf(stderr
, "%s: \"%s\": has no symbol table\n", applicationName
,
67 Symbol
* syms
= (Symbol
*) malloc(sizeof(Symbol
) * 10000);
69 Symbol
* last
= syms
+ alloced
;
71 // Get each of the relevant sections and add them to the list of
73 Elf32_Ehdr
*ehdr
= elf32_getehdr(elf
);
75 fprintf(stderr
, "%s: elf library lossage\n", applicationName
);
79 Elf32_Half ndx
= ehdr
->e_shstrndx
;
84 for (int i
= 1; (scn
= elf_nextscn(elf
, scn
)) != 0; i
++) {
85 Elf32_Shdr
*shdr
= elf32_getshdr(scn
);
87 char *name
= elf_strptr(elf
, ndx
, (size_t) shdr
->sh_name
);
88 printf("Section %s (%d 0x%x)\n", name
? name
: "(null)",
89 shdr
->sh_type
, shdr
->sh_type
);
91 if (shdr
->sh_type
== SHT_STRTAB
) {
92 /* We assume here that string tables preceed symbol tables... */
97 if (shdr
->sh_type
== SHT_DYNAMIC
) {
99 Elf_Data
*data
= elf_getdata(scn
, 0);
100 if (!data
|| !data
->d_size
) {
101 printf("No data...");
105 Elf32_Dyn
*dyn
= (Elf32_Dyn
*) data
->d_buf
;
107 (Elf32_Dyn
*) ((char*) data
->d_buf
+ data
->d_size
);
108 for (; dyn
< lastdyn
; dyn
++) {
109 printf("tag=%d value=0x%x\n", dyn
->d_tag
, dyn
->d_un
.d_val
);
113 if ((shdr
->sh_type
== SHT_SYMTAB
) ||
114 (shdr
->sh_type
== SHT_DYNSYM
)) {
116 Elf_Data
*data
= elf_getdata(scn
, 0);
117 if (!data
|| !data
->d_size
) {
118 printf("No data...");
122 /* In theory we now have the symbols... */
123 Elf32_Sym
*esym
= (Elf32_Sym
*) data
->d_buf
;
125 (Elf32_Sym
*) ((char*) data
->d_buf
+ data
->d_size
);
126 for (; esym
< lastsym
; esym
++) {
128 char *nm
= elf_strptr(elf
, strtabndx
, (size_t)esym
->st_name
);
129 printf("%20s 0x%08x %02x %02x\n",
130 nm
, esym
->st_value
, ELF32_ST_BIND(esym
->st_info
),
131 ELF32_ST_TYPE(esym
->st_info
));
133 if ((esym
->st_value
== 0) ||
134 (ELF32_ST_BIND(esym
->st_info
) == STB_WEAK
) ||
135 (ELF32_ST_BIND(esym
->st_info
) == STB_NUM
) ||
136 (ELF32_ST_TYPE(esym
->st_info
) != STT_FUNC
)) {
140 char *nm
= elf_strptr(elf
, strtabndx
, (size_t)esym
->st_name
);
142 sp
->name
= nm
? strdup(nm
) : "(no name)";
143 sp
->address
= esym
->st_value
;
146 long n
= alloced
+ 10000;
148 realloc(syms
, (size_t) (sizeof(Symbol
) * n
));
157 int interesting
= sp
- syms
;
159 printf("Total of %d symbols\n", interesting
);
161 usefulSymbols
= interesting
;
162 externalSymbols
= syms
;