-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.cc
110 lines (85 loc) · 2.74 KB
/
copy.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
/*
* 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 <jroedel@suse.de>
*/
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include "assembly.h"
#include "copy.h"
static void copy_symbol(const std::string &symbol,
const assembly::asm_file &file,
std::ostream &os)
{
if (!file.has_symbol(symbol)) {
std::cerr << "Error: Symbol not found: " << symbol << std::endl;
return;
}
auto func = std::unique_ptr<assembly::asm_object>(nullptr);
auto sym = file.get_symbol(symbol);
if (file.has_function(symbol)) {
func = file.get_function(symbol, assembly::func_flags::STRIP_DEBUG);
} else if (file.has_object(symbol)) {
func = file.get_object(symbol, assembly::func_flags::STRIP_DEBUG);
} else {
std::cerr << "Symbol not found: " << symbol << std::endl;
return;
}
if (sym.m_section_idx)
os << '\t' << file.stmt(sym.m_section_idx).raw() << std::endl;
if (sym.m_align_idx)
os << '\t' << file.stmt(sym.m_align_idx).raw() << std::endl;
if (sym.m_type_idx)
os << '\t' << file.stmt(sym.m_type_idx).raw() << std::endl;
if (sym.m_type == assembly::symbol_type::OBJECT && sym.m_size_idx)
os << '\t' << file.stmt(sym.m_size_idx).raw() << std::endl;
os << symbol << ':' << std::endl;
func->for_each_statement([&os](assembly::asm_statement &stmt) {
std::string prefix(stmt.type() == assembly::stmt_type::LABEL ? "" : "\t");
os << prefix << stmt.raw() << std::endl;
});
if (sym.m_type == assembly::symbol_type::FUNCTION && sym.m_size_idx)
os << '\t' << file.stmt(sym.m_size_idx).raw() << std::endl;
}
void copy_functions(const std::string &filename,
const std::vector<std::string> &symbols,
std::ostream &os)
{
std::set<std::string> functions, objects;
assembly::asm_file file(filename);
file.load();
for (auto fn : symbols) {
if (!file.has_function(fn)) {
std::cerr << "Function not found: " << fn << std::endl;
continue;
}
auto func = file.get_function(fn, assembly::func_flags::STRIP_DEBUG);
std::vector<std::string> syms = func->get_symbols();
for (auto s : syms) {
if (!file.has_symbol(s))
continue;
auto sym = file.get_symbol(s);
if (sym.m_scope != assembly::symbol_scope::LOCAL)
continue;
if (sym.m_type == assembly::symbol_type::FUNCTION)
functions.emplace(s);
else if (sym.m_type == assembly::symbol_type::OBJECT)
objects.emplace(s);
}
copy_symbol(fn, file, os);
os << "\t.globl " << fn << std::endl;
}
for (auto fn : functions)
copy_symbol(fn, file, os);
for (auto obj : objects)
copy_symbol(obj, file, os);
}