Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
symbol.hpp
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#pragma once
32#include <coff/image.hpp>
33#include <cstdint>
34#include <recomp/reloc.hpp>
35#include <string>
36#include <vector>
37
38namespace theo::decomp {
39/// <summary>
40/// meta symbol type. this is an abstraction upon the coff symbol storage/class
41/// type.
42/// </summary>
44 function = 0b00000001,
45 instruction = 0b00000010,
46 data = 0b00000100,
47 section = 0b00001000,
49};
50
51/// <summary>
52/// symbol_t is an abstraction upon the coff symbol. this allows for easier
53/// manipulation of the symbol. symbols can be different things, sections,
54/// functions, and even instructions (when functions are broken down).
55///
56/// this class is used throughout theodosius and is a keystone of the project.
57/// ensure you understand how this class works and what it contains.
58/// </summary>
59class symbol_t {
60 public:
61 /// <summary>
62 /// the explicit constructor of this symbol.
63 /// </summary>
64 /// <param name="img">the image in which the symbol is located in.</param>
65 /// <param name="name">the name of the symbol.</param>
66 /// <param name="offset">offset into the section where this symbol is
67 /// located.</param>
68 /// <param name="data">the data of the symbol. there can be
69 /// no data.</param>
70 /// <param name="scn">the section header describing the
71 /// section which contains the symbol.</param>
72 /// <param name="sym">the coff symbol itself.</param>
73 /// <param name="relocs">a vector of relocations this symbol has (if
74 /// any).</param>
75 /// <param name="dcmp_type">the type of symbol</param>
76 explicit symbol_t(coff::image_t* img,
77 std::string name,
78 std::uintptr_t offset,
79 std::vector<std::uint8_t> data,
80 coff::section_header_t* scn = {},
81 coff::symbol_t* sym = {},
82 std::vector<recomp::reloc_t> relocs = {},
83 sym_type_t dcmp_type = {});
84
85 /// <summary>
86 /// gets the name of the symbol.
87 /// </summary>
88 /// <returns>the name of the symbol.</returns>
89 std::string name() const;
90
91 /// <summary>
92 /// gets the offset into the section where the symbol is located.
93 /// </summary>
94 /// <returns>the offset into the section where the symbol is
95 /// located.</returns>
96 std::uintptr_t offset() const;
97
98 /// <summary>
99 /// returns the address where the symbol is allocated.
100 /// </summary>
101 /// <returns>the address where the symbol is allocated.</returns>
102 std::uintptr_t allocated_at() const;
103
104 /// <summary>
105 /// returns the size of the symbol.
106 /// </summary>
107 /// <returns>the size of the symbol.</returns>
108 std::uint32_t size() const;
109
110 /// <summary>
111 /// gets the section header of the section in which the symbol is contained.
112 /// </summary>
113 /// <returns>the section header of the section in which the symbol is
114 /// contained.</returns>
115 coff::section_header_t* scn() const;
116
117 /// <summary>
118 /// gets the imagine in which the symbol is located inside of.
119 /// </summary>
120 /// <returns>the imagine in which the symbol is located inside of.</returns>
121 coff::image_t* img() const;
122
123 /// <summary>
124 /// returns a vector by reference of bytes containing the data of the symbol.
125 /// </summary>
126 /// <returns>a vector by reference of bytes containing the data of the
127 /// symbol.</returns>
128 std::vector<std::uint8_t>& data();
129
130 /// <summary>
131 /// returns a pointer to the coff symbol object.
132 /// </summary>
133 /// <returns>a pointer to the coff symbol object.</returns>
134 coff::symbol_t* sym() const;
135
136 /// <summary>
137 /// returns the type of the symbol.
138 /// </summary>
139 /// <returns>the type of the symbol.</returns>
140 sym_type_t type() const;
141
142 /// <summary>
143 /// returns a vector of relocations.
144 /// </summary>
145 /// <returns>a vector of relocations.</returns>
146 std::vector<recomp::reloc_t>& relocs();
147
148 /// <summary>
149 /// set the address where the symbol is allocated at.
150 /// </summary>
151 /// <param name="allocated_at">where the symbol is allocated at.</param>
152 void allocated_at(std::uintptr_t allocated_at);
153
154 /// <summary>
155 /// gets the hash of the symbol name.
156 /// </summary>
157 /// <returns>the hash of the symbol name.</returns>
158 std::size_t hash();
159
160 /// <summary>
161 /// generate a hash given the name of the symbol.
162 /// </summary>
163 /// <param name="sym">the symbol name to create a hash from.</param>
164 /// <returns>the symbol name hash</returns>
165 static std::size_t hash(const std::string& sym);
166
167 /// <summary>
168 /// get the name of a symbol. this function will create a symbol name if the
169 /// symbol is opaquely named.
170 ///
171 /// for example in c++ if you define something like this:
172 ///
173 /// some_struct_t val = { value_one, value_two };
174 ///
175 /// "val" will be stored in the .data section of the coff file. however the
176 /// symbol name will be opaque (the name of the symbol will be ".data"). this
177 /// causes issues with theo since each symbol needs its own unqiue name to
178 /// generate a unique symbol name hash. for symbols like this, theo will
179 /// create a name for it with the following format:
180 ///
181 /// .data#section_index!coff_file_timestamp+offset_into_section
182 ///
183 /// </summary>
184 /// <param name="img">the coff file containing the symbol.</param>
185 /// <param name="sym">the coff symbol itself.</param>
186 /// <returns>the name of the symbol, or a created one.</returns>
187 static std::string name(const coff::image_t* img, coff::symbol_t* sym);
188
189 private:
190 std::string m_name;
191 std::uintptr_t m_offset, m_allocated_at;
192 std::vector<std::uint8_t> m_data;
193 coff::section_header_t* m_scn;
194 std::vector<recomp::reloc_t> m_relocs;
195 sym_type_t m_sym_type;
196 coff::symbol_t* m_sym;
197 coff::image_t* m_img;
198};
199} // namespace theo::decomp