@ -1,3 +1,33 @@
// 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
# pragma once
# include <coff/image.hpp>
# include <coff/image.hpp>
# include <cstdint>
# include <cstdint>
@ -18,9 +48,25 @@ enum sym_type_t { function, instruction, data, section };
/// functions, and even instructions (when functions are broken down).
/// functions, and even instructions (when functions are broken down).
///
///
/// this class is used throughout theodosius and is a keystone of the project.
/// this class is used throughout theodosius and is a keystone of the project.
/// ensure you understand how this class works and what it contains.
/// </summary>
/// </summary>
class symbol_t {
class symbol_t {
public :
public :
/// <summary>
/// the explicit constructor of this symbol.
/// </summary>
/// <param name="img">the image in which the symbol is located in.</param>
/// <param name="name">the name of the symbol.</param>
/// <param name="offset">offset into the section where this symbol is
/// located.</param>
/// <param name="data">the data of the symbol. there can be
/// no data.</param>
/// <param name="scn">the section header describing the
/// section which contains the symbol.</param>
/// <param name="sym">the coff symbol itself.</param>
/// <param name="relocs">a vector of relocations this symbol has (if
/// any).</param>
/// <param name="dcmp_type">the type of symbol</param>
explicit symbol_t ( coff : : image_t * img ,
explicit symbol_t ( coff : : image_t * img ,
std : : string name ,
std : : string name ,
std : : uintptr_t offset ,
std : : uintptr_t offset ,
@ -30,20 +76,108 @@ class symbol_t {
std : : vector < recomp : : reloc_t > relocs = { } ,
std : : vector < recomp : : reloc_t > relocs = { } ,
sym_type_t dcmp_type = { } ) ;
sym_type_t dcmp_type = { } ) ;
/// <summary>
/// gets the name of the symbol.
/// </summary>
/// <returns>the name of the symbol.</returns>
std : : string name ( ) const ;
std : : string name ( ) const ;
/// <summary>
/// gets the offset into the section where the symbol is located.
/// </summary>
/// <returns>the offset into the section where the symbol is
/// located.</returns>
std : : uintptr_t offset ( ) const ;
std : : uintptr_t offset ( ) const ;
/// <summary>
/// returns the address where the symbol is allocated.
/// </summary>
/// <returns>the address where the symbol is allocated.</returns>
std : : uintptr_t allocated_at ( ) const ;
std : : uintptr_t allocated_at ( ) const ;
/// <summary>
/// returns the size of the symbol.
/// </summary>
/// <returns>the size of the symbol.</returns>
std : : uint32_t size ( ) const ;
std : : uint32_t size ( ) const ;
/// <summary>
/// gets the section header of the section in which the symbol is contained.
/// </summary>
/// <returns>the section header of the section in which the symbol is
/// contained.</returns>
coff : : section_header_t * scn ( ) const ;
coff : : section_header_t * scn ( ) const ;
/// <summary>
/// gets the imagine in which the symbol is located inside of.
/// </summary>
/// <returns>the imagine in which the symbol is located inside of.</returns>
coff : : image_t * img ( ) const ;
coff : : image_t * img ( ) const ;
/// <summary>
/// returns a vector by reference of bytes containing the data of the symbol.
/// </summary>
/// <returns>a vector by reference of bytes containing the data of the
/// symbol.</returns>
std : : vector < std : : uint8_t > & data ( ) ;
std : : vector < std : : uint8_t > & data ( ) ;
/// <summary>
/// returns a pointer to the coff symbol object.
/// </summary>
/// <returns>a pointer to the coff symbol object.</returns>
coff : : symbol_t * sym ( ) const ;
coff : : symbol_t * sym ( ) const ;
/// <summary>
/// returns the type of the symbol.
/// </summary>
/// <returns>the type of the symbol.</returns>
sym_type_t type ( ) const ;
sym_type_t type ( ) const ;
/// <summary>
/// returns a vector of relocations.
/// </summary>
/// <returns>a vector of relocations.</returns>
std : : vector < recomp : : reloc_t > & relocs ( ) ;
std : : vector < recomp : : reloc_t > & relocs ( ) ;
/// <summary>
/// set the address where the symbol is allocated at.
/// </summary>
/// <param name="allocated_at">where the symbol is allocated at.</param>
void allocated_at ( std : : uintptr_t allocated_at ) ;
void allocated_at ( std : : uintptr_t allocated_at ) ;
/// <summary>
/// gets the hash of the symbol name.
/// </summary>
/// <returns>the hash of the symbol name.</returns>
std : : size_t hash ( ) ;
std : : size_t hash ( ) ;
/// <summary>
/// generate a hash given the name of the symbol.
/// </summary>
/// <param name="sym">the symbol name to create a hash from.</param>
/// <returns>the symbol name hash</returns>
static std : : size_t hash ( const std : : string & sym ) ;
static std : : size_t hash ( const std : : string & sym ) ;
/// <summary>
/// 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
///
/// </summary>
/// <param name="img">the coff file containing the symbol.</param>
/// <param name="sym">the coff symbol itself.</param>
/// <returns>the name of the symbol, or a created one.</returns>
static std : : string name ( const coff : : image_t * img , coff : : symbol_t * sym ) ;
static std : : string name ( const coff : : image_t * img , coff : : symbol_t * sym ) ;
private :
private :