// 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 #include #include #include #include #include #include /// /// the namespace that contains all of the decomposition related code. /// namespace theo::decomp { /// /// meta symbol data. consists of the coff image which contains the coff symbol, /// the coff symbol itself, and the size (if any) of the symbol. /// using sym_data_t = std::tuple; /// /// the main decomposition class which is responsible for breaking down lib file /// into coff files, and extracted used symbols from the coff files. /// class decomp_t { public: /// /// the explicit constructor for decomp_t /// /// vector of bytes containing the lib file. /// symbol table that gets populated and managed by this /// class. explicit decomp_t(std::vector& lib, recomp::symbol_table_t* syms); /// /// gets all of the routine objects. /// /// vector of routine objects. std::vector rtns(); /// /// gets a vector of bytes consisting of the lib file. /// /// a vector of bytes consisting of the lib file. std::vector lib(); /// /// gets all the obj files as a vector of a vector of bytes. /// /// all the obj files as a vector of a vector of bytes. std::vector> objs(); /// /// gets the symbol table. /// /// the symbol table. recomp::symbol_table_t* syms(); /// /// gets the section hash table section header --> hash of the section header /// ptr. /// /// the section hash table section header --> hash of the section /// header ptr. std::map& scn_hash_tbl(); /// /// decomposes (extracts) the symbols used. this function determines all used /// symbols given the entry point. /// /// the entry point symbol name. /// returns an optional pointer to the symbol table. no value in the /// optional object on failure. std::optional decompose(std::string& entry_sym); private: /// /// extracts used symbols from coff files. /// /// the entry point symbol name /// number of symbols used std::uint32_t ext_used_syms(const std::string&& entry_sym); /// /// get symbol meta data by name. /// /// symbol name /// optional symbol meta data if it exists. std::optional get_symbol(const std::string_view& name); /// /// the next symbol in the section. /// /// coff image that contains the symbol. /// coff section header of the section that contains the /// symbol. /// symbol in which to get the next one of. /// offset into the section where the next symbol is at. std::uint32_t next_sym(coff::image_t* img, coff::section_header_t* hdr, coff::symbol_t* s); const std::vector m_lib; std::vector> m_objs; std::vector m_rtns; std::set m_used_syms; std::set m_processed_objs; std::map m_scn_hash_tbl; std::map> m_lookup_tbl; recomp::symbol_table_t* m_syms; }; } // namespace theo::decomp