Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
symbol.cpp
Go to the documentation of this file.
1// Copyright (c) 2022, _xeroxz
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9//
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// 3. Neither the name of the copyright holder nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28// POSSIBILITY OF SUCH DAMAGE.
29//
30
31#include <decomp/symbol.hpp>
32
33namespace theo::decomp {
34symbol_t::symbol_t(coff::image_t* img,
35 std::string name,
36 std::uintptr_t offset,
37 std::vector<std::uint8_t> data,
38 coff::section_header_t* scn,
39 coff::symbol_t* sym,
40 std::vector<recomp::reloc_t> relocs,
41 sym_type_t dcmp_type)
42 : m_name(name),
43 m_offset(offset),
44 m_data(data),
45 m_scn(scn),
46 m_relocs(relocs),
47 m_sym_type(dcmp_type),
48 m_sym(sym),
49 m_img(img),
50 m_allocated_at(0) {}
51
52std::string symbol_t::name() const {
53 return m_name;
54}
55
56std::uintptr_t symbol_t::offset() const {
57 return m_offset;
58}
59
60std::uintptr_t symbol_t::allocated_at() const {
61 return m_allocated_at;
62}
63
64coff::section_header_t* symbol_t::scn() const {
65 return m_scn;
66}
67
68coff::image_t* symbol_t::img() const {
69 return m_img;
70}
71
72std::uint32_t symbol_t::size() const {
73 return m_data.size();
74}
75
76std::vector<std::uint8_t>& symbol_t::data() {
77 return m_data;
78}
79
81 return m_sym_type;
82}
83
84void symbol_t::allocated_at(std::uintptr_t allocated_at) {
85 m_allocated_at = allocated_at;
86}
87
88std::size_t symbol_t::hash() {
89 return hash(m_name);
90}
91
92coff::symbol_t* symbol_t::sym() const {
93 return m_sym;
94}
95
96std::vector<recomp::reloc_t>& symbol_t::relocs() {
97 return m_relocs;
98}
99
100std::size_t symbol_t::hash(const std::string& sym) {
101 return std::hash<std::string>{}(sym);
102}
103
104std::string symbol_t::name(const coff::image_t* img, coff::symbol_t* sym) {
105 if (sym->has_section() &&
106 sym->storage_class == coff::storage_class_id::private_symbol &&
107 sym->derived_type == coff::derived_type_id::none) {
108 auto scn = img->get_section(sym->section_index - 1);
109 auto res = std::string(scn->name.to_string(img->get_strings()).data())
110 .append("#")
111 .append(std::to_string(sym->section_index))
112 .append("!")
113 .append(std::to_string(img->file_header.timedate_stamp))
114 .append("+")
115 .append(std::to_string(sym->value));
116
117 return res;
118 }
119 return std::string(sym->name.to_string(img->get_strings()));
120}
121} // namespace theo::decomp