-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.cc
152 lines (121 loc) · 3.69 KB
/
info.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2015-2016 SUSE Linux GmbH
*
* Licensed under the GNU General Public License Version 2
* as published by the Free Software Foundation.
*
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* for details.
*
* Author: Joerg Roedel <[email protected]>
*/
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include "assembly.h"
#include "info.h"
static void print_one_symbol(assembly::asm_file &file,
std::string &sym, assembly::asm_symbol &info,
bool verbose)
{
std::string scope;
std::string type;
switch (info.m_scope) {
case assembly::symbol_scope::LOCAL:
scope = "Local";
break;
case assembly::symbol_scope::GLOBAL:
scope = "Global";
break;
default:
scope = "Unknown";
break;
}
switch (info.m_type) {
case assembly::symbol_type::FUNCTION:
type = "Function:";
break;
case assembly::symbol_type::OBJECT:
type = "Object:";
break;
default:
type = "Unknown:";
break;
}
std::cout << std::left;
std::cout << std::setw(10) << type << std::setw(48) << sym;
std::cout << " Scope: " << std::setw(10) << scope << std::endl;
if (!verbose)
return;
std::unique_ptr<assembly::asm_object> obj(nullptr);
if (info.m_type == assembly::symbol_type::FUNCTION)
obj = file.get_function(sym, assembly::func_flags::STRIP_DEBUG);
else
obj = file.get_object(sym, assembly::func_flags::STRIP_DEBUG);
if (obj == nullptr)
return;
auto symbols = obj->get_symbols();
if (symbols.size() == 0)
return;
std::cout << " Referenced symbols " << std::endl;
std::cout << " (F - Function, O - Object, G - Global, L - Local, E - External):" << std::endl;
for (auto &s : symbols) {
char type = '-', scope = 'E';
if (file.has_symbol(s)) {
auto sym = file.get_symbol(s);
if (sym.m_type == assembly::symbol_type::FUNCTION)
type = 'F';
else if (sym.m_type == assembly::symbol_type::OBJECT)
type = 'O';
if (sym.m_scope == assembly::symbol_scope::GLOBAL)
scope = 'G';
else if (sym.m_scope == assembly::symbol_scope::LOCAL)
scope = 'L';
}
std::cout << " " << s << " (" << type << scope << ')' << std::endl;
}
}
static void print_symbols(assembly::asm_file &file, struct info_options &opts,
std::function<bool(assembly::asm_symbol&)> filter)
{
file.for_each_symbol([&file, &opts, &filter](std::string sym,
assembly::asm_symbol info) {
if (!filter(info))
return;
print_one_symbol(file, sym, info, opts.verbose);
});
}
void print_symbol_info(const char *filename, struct info_options opts)
{
assembly::asm_file file(filename);
file.load();
if (opts.functions && opts.global)
print_symbols(file, opts, [](assembly::asm_symbol &s)
{ return s.m_type == assembly::symbol_type::FUNCTION &&
s.m_scope == assembly::symbol_scope::GLOBAL; });
if (opts.functions && opts.local)
print_symbols(file, opts, [](assembly::asm_symbol &s)
{ return s.m_type == assembly::symbol_type::FUNCTION &&
s.m_scope == assembly::symbol_scope::LOCAL; });
if (opts.objects && opts.global)
print_symbols(file, opts, [](assembly::asm_symbol &s)
{ return s.m_type == assembly::symbol_type::OBJECT &&
s.m_scope == assembly::symbol_scope::GLOBAL; });
if (opts.objects && opts.local)
print_symbols(file, opts, [](assembly::asm_symbol &s)
{ return s.m_type == assembly::symbol_type::OBJECT &&
s.m_scope == assembly::symbol_scope::LOCAL; });
}
void print_one_symbol_info(const char *filename, std::string fn_name)
{
assembly::asm_file file(filename);
file.load();
if (!file.has_function(fn_name)) {
std::cerr << "No such function: " << fn_name << std::endl;
return;
}
auto info = file.get_symbol(fn_name);
print_one_symbol(file, fn_name, info, true);
}