Theodosius  v3.0
Jit linker, mapper, obfuscator, and mutator
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 
33 namespace theo::decomp {
34 symbol_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 
52 std::string symbol_t::name() const {
53  return m_name;
54 }
55 
56 std::uintptr_t symbol_t::offset() const {
57  return m_offset;
58 }
59 
60 std::uintptr_t symbol_t::allocated_at() const {
61  return m_allocated_at;
62 }
63 
64 coff::section_header_t* symbol_t::scn() const {
65  return m_scn;
66 }
67 
68 coff::image_t* symbol_t::img() const {
69  return m_img;
70 }
71 
72 std::uint32_t symbol_t::size() const {
73  return m_data.size();
74 }
75 
76 std::vector<std::uint8_t>& symbol_t::data() {
77  return m_data;
78 }
79 
81  return m_sym_type;
82 }
83 
84 void symbol_t::allocated_at(std::uintptr_t allocated_at) {
85  m_allocated_at = allocated_at;
86 }
87 
88 std::size_t symbol_t::hash() {
89  return hash(m_name);
90 }
91 
92 coff::symbol_t* symbol_t::sym() const {
93  return m_sym;
94 }
95 
96 std::vector<recomp::reloc_t>& symbol_t::relocs() {
97  return m_relocs;
98 }
99 
100 std::size_t symbol_t::hash(const std::string& sym) {
101  return std::hash<std::string>{}(sym);
102 }
103 
104 std::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
coff::symbol_t * sym() const
returns a pointer to the coff symbol object.
Definition: symbol.cpp:92
sym_type_t type() const
returns the type of the symbol.
Definition: symbol.cpp:80
coff::section_header_t * scn() const
gets the section header of the section in which the symbol is contained.
Definition: symbol.cpp:64
std::uintptr_t offset() const
gets the offset into the section where the symbol is located.
Definition: symbol.cpp:56
std::string name() const
gets the name of the symbol.
Definition: symbol.cpp:52
std::size_t hash()
gets the hash of the symbol name.
Definition: symbol.cpp:88
std::vector< std::uint8_t > & data()
returns a vector by reference of bytes containing the data of the symbol.
Definition: symbol.cpp:76
symbol_t(coff::image_t *img, std::string name, std::uintptr_t offset, std::vector< std::uint8_t > data, coff::section_header_t *scn={}, coff::symbol_t *sym={}, std::vector< recomp::reloc_t > relocs={}, sym_type_t dcmp_type={})
the explicit constructor of this symbol.
Definition: symbol.cpp:34
std::vector< recomp::reloc_t > & relocs()
returns a vector of relocations.
Definition: symbol.cpp:96
coff::image_t * img() const
gets the imagine in which the symbol is located inside of.
Definition: symbol.cpp:68
std::uint32_t size() const
returns the size of the symbol.
Definition: symbol.cpp:72
std::uintptr_t allocated_at() const
returns the address where the symbol is allocated.
Definition: symbol.cpp:60
the namespace that contains all of the decomposition related code.
Definition: decomp.hpp:49
sym_type_t
meta symbol type. this is an abstraction upon the coff symbol storage/class type.
Definition: symbol.hpp:43