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 ``elf_symbol_table''
16 * The Initial Developer of the Original Code is Netscape
17 * Communications Corp. Portions created by the Initial Developer are
18 * Copyright (C) 2001 the Initial Developer. All Rights Reserved.
21 * Chris Waterson <waterson@netscape.com>
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 ***** */
46 #include "elf_symbol_table.h"
47 #include "elf_utils.h"
50 elf_symbol_table::init(const char *name
)
52 // Open the file readonly.
53 m_fd
= open(name
, O_RDONLY
);
61 if (fstat(m_fd
, &statbuf
) < 0) {
66 m_size
= statbuf
.st_size
;
69 m_mapping
= mmap(0, m_size
, PROT_READ
, MAP_SHARED
, m_fd
, 0);
70 if (m_mapping
== MAP_FAILED
) {
75 // Make sure it's an ELF header.
76 const Elf32_Ehdr
*ehdr
= reinterpret_cast<const Elf32_Ehdr
*>(m_mapping
);
77 if (elf_verify_header(ehdr
) < 0)
80 const char *mapping
= reinterpret_cast<const char *>(m_mapping
);
82 // Find the section headers
83 const Elf32_Shdr
*shdrs
84 = reinterpret_cast<const Elf32_Shdr
*>(mapping
+ ehdr
->e_shoff
);
86 // find the section header string table, .shstrtab
87 const Elf32_Shdr
*shstrtabsh
= shdrs
+ ehdr
->e_shstrndx
;
88 const char *shstrtab
= mapping
+ shstrtabsh
->sh_offset
;
90 // parse the sections we care about
92 const Elf32_Shdr
*shlimit
= shdrs
+ ehdr
->e_shnum
;
93 for (const Elf32_Shdr
*shdr
= shdrs
; shdr
< shlimit
; ++shdr
, ++shndx
) {
94 basic_string
<char> name(shstrtab
+ shdr
->sh_name
);
95 if (name
== ".symtab") {
96 m_symbols
= reinterpret_cast<const Elf32_Sym
*>(mapping
+ shdr
->sh_offset
);
97 m_nsymbols
= shdr
->sh_size
/ sizeof(Elf32_Sym
);
99 else if (name
== ".strtab") {
100 m_strtab
= mapping
+ shdr
->sh_offset
;
102 else if (name
== ".text") {
103 m_text_shndx
= shndx
;
107 // Parse the symbol table
108 const Elf32_Sym
*limit
= m_symbols
+ m_nsymbols
;
109 for (const Elf32_Sym
*sym
= m_symbols
; sym
< limit
; ++sym
) {
110 if (is_function(sym
)) {
113 cout
<< sym
->st_value
<< endl
;
115 m_rsymtab
.put(sym
->st_value
, sym
->st_value
+ sym
->st_size
, sym
);
123 elf_symbol_table::finish()
125 if (m_mapping
!= MAP_FAILED
) {
126 munmap(m_mapping
, m_size
);
127 m_mapping
= MAP_FAILED
;
139 elf_symbol_table::lookup(unsigned int addr
) const
141 rsymtab_t::const_iterator result
= m_rsymtab
.get(addr
);
142 return result
!= m_rsymtab
.end() ? reinterpret_cast<const Elf32_Sym
*>(*result
) : 0;
146 elf_symbol_table::get_symbol_name(const Elf32_Sym
*sym
) const
148 return m_strtab
+ sym
->st_name
;