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 Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 1998
18 * the Initial Developer. All Rights Reserved.
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 ***** */
43 #include <libelf/libelf.h>
48 void leaky::readSymbols(const char *fileName
)
50 int fd
= ::open(fileName
, O_RDONLY
);
52 fprintf(stderr
, "%s: unable to open \"%s\"\n", applicationName
,
57 elf_version(EV_CURRENT
);
58 Elf
*elf
= elf_begin(fd
, ELF_C_READ
, 0);
60 fprintf(stderr
, "%s: \"%s\": has no symbol table\n", applicationName
,
66 Symbol
* syms
= (Symbol
*) malloc(sizeof(Symbol
) * 10000);
68 Symbol
* last
= syms
+ alloced
;
70 // Get each of the relevant sections and add them to the list of
72 Elf32_Ehdr
*ehdr
= elf32_getehdr(elf
);
74 fprintf(stderr
, "%s: elf library lossage\n", applicationName
);
78 Elf32_Half ndx
= ehdr
->e_shstrndx
;
83 for (int i
= 1; (scn
= elf_nextscn(elf
, scn
)) != 0; i
++) {
84 Elf32_Shdr
*shdr
= elf32_getshdr(scn
);
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
);
90 if (shdr
->sh_type
== SHT_STRTAB
) {
91 /* We assume here that string tables preceed symbol tables... */
96 if (shdr
->sh_type
== SHT_DYNAMIC
) {
98 Elf_Data
*data
= elf_getdata(scn
, 0);
99 if (!data
|| !data
->d_size
) {
100 printf("No data...");
104 Elf32_Dyn
*dyn
= (Elf32_Dyn
*) data
->d_buf
;
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
);
112 if ((shdr
->sh_type
== SHT_SYMTAB
) ||
113 (shdr
->sh_type
== SHT_DYNSYM
)) {
115 Elf_Data
*data
= elf_getdata(scn
, 0);
116 if (!data
|| !data
->d_size
) {
117 printf("No data...");
121 /* In theory we now have the symbols... */
122 Elf32_Sym
*esym
= (Elf32_Sym
*) data
->d_buf
;
124 (Elf32_Sym
*) ((char*) data
->d_buf
+ data
->d_size
);
125 for (; esym
< lastsym
; esym
++) {
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
));
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
)) {
139 char *nm
= elf_strptr(elf
, strtabndx
, (size_t)esym
->st_name
);
141 sp
->name
= nm
? strdup(nm
) : "(no name)";
142 sp
->address
= esym
->st_value
;
145 long n
= alloced
+ 10000;
147 realloc(syms
, (size_t) (sizeof(Symbol
) * n
));
156 int interesting
= sp
- syms
;
158 printf("Total of %d symbols\n", interesting
);
160 usefulSymbols
= interesting
;
161 externalSymbols
= syms
;