// Copyright (c) 2022, _xeroxz // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // #pragma once #include #include #include #include #include namespace theo::decomp { /// /// meta symbol type. this is an abstraction upon the coff symbol storage/class /// type. /// enum sym_type_t { function, instruction, data, section }; /// /// symbol_t is an abstraction upon the coff symbol. this allows for easier /// manipulation of the symbol. symbols can be different things, sections, /// functions, and even instructions (when functions are broken down). /// /// this class is used throughout theodosius and is a keystone of the project. /// ensure you understand how this class works and what it contains. /// class symbol_t { public: /// /// the explicit constructor of this symbol. /// /// the image in which the symbol is located in. /// the name of the symbol. /// offset into the section where this symbol is /// located. /// the data of the symbol. there can be /// no data. /// the section header describing the /// section which contains the symbol. /// the coff symbol itself. /// a vector of relocations this symbol has (if /// any). /// the type of symbol explicit symbol_t(coff::image_t* img, std::string name, std::uintptr_t offset, std::vector data, coff::section_header_t* scn = {}, coff::symbol_t* sym = {}, std::vector relocs = {}, sym_type_t dcmp_type = {}); /// /// gets the name of the symbol. /// /// the name of the symbol. std::string name() const; /// /// gets the offset into the section where the symbol is located. /// /// the offset into the section where the symbol is /// located. std::uintptr_t offset() const; /// /// returns the address where the symbol is allocated. /// /// the address where the symbol is allocated. std::uintptr_t allocated_at() const; /// /// returns the size of the symbol. /// /// the size of the symbol. std::uint32_t size() const; /// /// gets the section header of the section in which the symbol is contained. /// /// the section header of the section in which the symbol is /// contained. coff::section_header_t* scn() const; /// /// gets the imagine in which the symbol is located inside of. /// /// the imagine in which the symbol is located inside of. coff::image_t* img() const; /// /// returns a vector by reference of bytes containing the data of the symbol. /// /// a vector by reference of bytes containing the data of the /// symbol. std::vector& data(); /// /// returns a pointer to the coff symbol object. /// /// a pointer to the coff symbol object. coff::symbol_t* sym() const; /// /// returns the type of the symbol. /// /// the type of the symbol. sym_type_t type() const; /// /// returns a vector of relocations. /// /// a vector of relocations. std::vector& relocs(); /// /// set the address where the symbol is allocated at. /// /// where the symbol is allocated at. void allocated_at(std::uintptr_t allocated_at); /// /// gets the hash of the symbol name. /// /// the hash of the symbol name. std::size_t hash(); /// /// generate a hash given the name of the symbol. /// /// the symbol name to create a hash from. /// the symbol name hash static std::size_t hash(const std::string& sym); /// /// get the name of a symbol. this function will create a symbol name if the /// symbol is opaquely named. /// /// for example in c++ if you define something like this: /// /// some_struct_t val = { value_one, value_two }; /// /// "val" will be stored in the .data section of the coff file. however the /// symbol name will be opaque (the name of the symbol will be ".data"). this /// causes issues with theo since each symbol needs its own unqiue name to /// generate a unique symbol name hash. for symbols like this, theo will /// create a name for it with the following format: /// /// .data#section_index!coff_file_timestamp+offset_into_section /// /// /// the coff file containing the symbol. /// the coff symbol itself. /// the name of the symbol, or a created one. static std::string name(const coff::image_t* img, coff::symbol_t* sym); private: std::string m_name; std::uintptr_t m_offset, m_allocated_at; std::vector m_data; coff::section_header_t* m_scn; std::vector m_relocs; sym_type_t m_sym_type; coff::symbol_t* m_sym; coff::image_t* m_img; }; } // namespace theo::decomp