OpenCores
URL https://opencores.org/ocsvn/fwrisc/fwrisc/trunk

Subversion Repositories fwrisc

[/] [fwrisc/] [trunk/] [ve/] [fwrisc/] [tests/] [ElfSymtabReader.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mballance
/*
2
 * ElfSymtabReader.cpp
3
 *
4
 * Copyright 2018 Matthew Ballance
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in
8
 * compliance with the License.  You may obtain a copy of
9
 * the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in
14
 * writing, software distributed under the License is
15
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16
 * CONDITIONS OF ANY KIND, either express or implied.  See
17
 * the License for the specific language governing
18
 * permissions and limitations under the License.
19
 *
20
 *  Created on: Nov 17, 2018
21
 *      Author: ballance
22
 */
23
 
24
#include "ElfSymtabReader.h"
25
#include <stdio.h>
26
#include <string.h>
27
#include <algorithm>
28
 
29
ElfSymtabReader::ElfSymtabReader() {
30
        // TODO Auto-generated constructor stub
31
 
32
}
33
 
34
ElfSymtabReader::~ElfSymtabReader() {
35
        // TODO Auto-generated destructor stub
36
}
37
 
38
Elf32_Sym ElfSymtabReader::find_sym(const std::string &name) {
39
        Elf32_Sym ret;
40
        std::map<std::string, Elf32_Sym>::iterator it;
41
 
42
        memset(&ret, 0, sizeof(Elf32_Sym));
43
 
44
        if ((it=m_symtab.find(name)) != m_symtab.end()) {
45
                ret = it->second;
46
        }
47
 
48
        return ret;
49
}
50
 
51
bool ElfSymtabReader::find_sym(Elf32_Addr addr, std::string &name) {
52
        std::map<Elf32_Addr,uint32_t>::iterator it;
53
 
54
        if ((it=m_addrtab.find(addr)) != m_addrtab.end()) {
55
                name = m_symlist.at(it->second).second;
56
                return true;
57
        } else {
58
                return false;
59
        }
60
}
61
 
62
int32_t ElfSymtabReader::find_sym(Elf32_Addr addr) {
63
        std::map<Elf32_Addr,uint32_t>::iterator it;
64
 
65
        if ((it=m_addrtab.find(addr)) != m_addrtab.end()) {
66
                return it->second;
67
        } else {
68
                return -1;
69
        }
70
}
71
 
72
const Elf32_Sym &ElfSymtabReader::get_sym(int32_t idx) {
73
        return m_symlist.at(idx).first;
74
}
75
 
76
const std::string &ElfSymtabReader::get_sym_name(int32_t idx) {
77
        return m_symlist.at(idx).second;
78
}
79
 
80
 
81
struct comp_syms {
82
        inline bool operator () (
83
                        const std::pair<Elf32_Sym, std::string> &v1,
84
                        const std::pair<Elf32_Sym, std::string> &v2) {
85
                return (v1.first.st_value < v2.first.st_value);
86
        }
87
};
88
 
89
 
90
void ElfSymtabReader::visit_shdr(const Elf32_Shdr &shdr) {
91
        if (shdr.sh_type == SHT_SYMTAB) {
92
                Elf32_Shdr str_shdr;
93
                std::vector<std::pair<Elf32_Sym, std::string>> syms;
94
 
95
                read(hdr().e_shoff+hdr().e_shentsize*shdr.sh_link,
96
                                &str_shdr, sizeof(Elf32_Shdr));
97
 
98
                fprintf(stdout, "String table: %d\n", str_shdr.sh_size);
99
                char *str_tmp = new char[str_shdr.sh_size];
100
                read(str_shdr.sh_offset, str_tmp, str_shdr.sh_size);
101
 
102
                for (uint32_t i=0; i<shdr.sh_size; i+=sizeof(Elf32_Sym)) {
103
                        Elf32_Sym sym;
104
 
105
                        read(shdr.sh_offset+i, &sym, sizeof(Elf32_Sym));
106
 
107
                        syms.push_back(std::pair<Elf32_Sym,std::string>(
108
                                        sym, &str_tmp[sym.st_name]));
109
                }
110
                delete [] str_tmp;
111
 
112
                // Now, sort the symbols list
113
                std::sort(syms.begin(), syms.end(), comp_syms());
114
 
115
                for (std::vector<std::pair<Elf32_Sym,std::string>>::const_iterator it=syms.begin();
116
                                it!=syms.end(); it++) {
117
                        m_symlist.push_back(*it);
118
                        m_symtab[it->second] = it->first;
119
                        m_addrtab[it->first.st_value] = m_symlist.size()-1; // index of the symbol
120
                }
121
        }
122
}
123
 
124
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.