Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / tools / reorder / elf_symbol_table.cpp
blob4c0a23bbf3df8e7d360311ba7cfc10d0c859f31f
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 ``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.
20 * Contributor(s):
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 ***** */
37 #include <string>
38 #include <fstream>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <sys/stat.h>
46 #include "elf_symbol_table.h"
47 #include "elf_utils.h"
49 int
50 elf_symbol_table::init(const char *name)
52 // Open the file readonly.
53 m_fd = open(name, O_RDONLY);
54 if (m_fd < 0) {
55 perror(name);
56 return m_fd;
59 // Get its size.
60 struct stat statbuf;
61 if (fstat(m_fd, &statbuf) < 0) {
62 perror(name);
63 return -1;
66 m_size = statbuf.st_size;
68 // Memory map it.
69 m_mapping = mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
70 if (m_mapping == MAP_FAILED) {
71 perror(name);
72 return -1;
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)
78 return -1;
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
91 int shndx = 0;
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)) {
111 #ifdef DEBUG
112 hex(cout);
113 cout << sym->st_value << endl;
114 #endif
115 m_rsymtab.put(sym->st_value, sym->st_value + sym->st_size, sym);
119 return 0;
123 elf_symbol_table::finish()
125 if (m_mapping != MAP_FAILED) {
126 munmap(m_mapping, m_size);
127 m_mapping = MAP_FAILED;
130 if (m_fd >= 0) {
131 close(m_fd);
132 m_fd = -1;
135 return 0;
138 const Elf32_Sym *
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;
145 const char *
146 elf_symbol_table::get_symbol_name(const Elf32_Sym *sym) const
148 return m_strtab + sym->st_name;