Skip to content

Commit

Permalink
-Add minimally functional encodings implementation.
Browse files Browse the repository at this point in the history
-TODO:Cleanup.
  • Loading branch information
lorenzo-gomez-windhover committed Sep 3, 2024
1 parent b1a9e37 commit 186880c
Show file tree
Hide file tree
Showing 9 changed files with 503 additions and 110 deletions.
23 changes: 23 additions & 0 deletions src/ElfFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,26 @@ std::vector<Elf32_Shdr> ElfFile::getElf32Headers() const { return elf32Head

void ElfFile::addElf32SymbolTableSymbol(Elf32Symbol newSymbol) { elf32SymbolTable.push_back(newSymbol); }
std::vector<Elf32Symbol> ElfFile::getElf32SymbolTable() const { return elf32SymbolTable; }

/**
* @brief ElfFile::getEncodings
* @return a list of encodings as per DWARF5 specification document section 5.1.1 titled "Base Type Encodings"
*/
std::vector<Encoding> ElfFile::getDWARFEncodings()
{
std::vector<Encoding> encodings{};

for (std::pair<int, Encoding> e : encodingsMap)
{
encodings.push_back(e.second);
}
return encodings;
}

/**
* @brief ElfFile::getDWARFEncoding
* @param encoding
* @todo add error-checking since we know the valid values
* @return
*/
Encoding& ElfFile::getDWARFEncoding(int encoding) { return encodingsMap.at(encoding); }
37 changes: 37 additions & 0 deletions src/ElfFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

#include "DefineMacro.h"
#include "Elf32Symbol.h"
#include "Encoding.h"
#include "Field.h"
#include "Juicer.h"
#include "Logger.h"
#include "Variable.h"
#include "dwarf.h"

class Symbol;
class Field;
Expand Down Expand Up @@ -80,6 +82,10 @@ class ElfFile
void addElf32SymbolTableSymbol(Elf32Symbol newSymbol);
std::vector<Elf32Symbol> getElf32SymbolTable() const;

std::vector<Encoding> getDWARFEncodings();

Encoding &getDWARFEncoding(int encoding);

private:
std::string md5;
/**
Expand Down Expand Up @@ -111,6 +117,37 @@ class ElfFile

std::vector<Elf32Symbol> elf32SymbolTable{};
std::vector<Elf32_Sym> elf32StringsTable{};

/**
* @note This list of encodings can be found in dwarf.h or
* in DWARF5 specification document section 5.1.1 titled "Base Type Encodings"
*/
/**
* @brief encodingMap maps the DWARF macros to strings.
*/
std::map<int, Encoding> encodingsMap = {
{DW_ATE_address, Encoding{"DW_ATE_address"}},
{DW_ATE_boolean, Encoding{"DW_ATE_boolean"}},
{DW_ATE_complex_float, Encoding{"DW_ATE_complex_float"}},
{DW_ATE_float, Encoding{"DW_ATE_float"}},
{DW_ATE_signed, Encoding{"DW_ATE_signed"}},
{DW_ATE_signed_char, Encoding{"DW_ATE_signed_char"}},
{DW_ATE_unsigned, Encoding{"DW_ATE_unsigned"}},
{DW_ATE_unsigned_char, Encoding{"DW_ATE_unsigned_char"}},
{DW_ATE_imaginary_float, Encoding{"DW_ATE_imaginary_float"}},
{DW_ATE_packed_decimal, Encoding{"DW_ATE_packed_decimal"}},
{DW_ATE_numeric_string, Encoding{"DW_ATE_numeric_string"}},
{DW_ATE_edited, Encoding{"DW_ATE_edited"}},
{DW_ATE_signed_fixed, Encoding{"DW_ATE_signed_fixed"}},
{DW_ATE_unsigned_fixed, Encoding{"DW_ATE_unsigned_fixed"}},
{DW_ATE_decimal_float, Encoding{"DW_ATE_decimal_float"}},
{DW_ATE_UTF, Encoding{"DW_ATE_UTF"}},
{DW_ATE_UCS, Encoding{"DW_ATE_UCS"}},
{DW_ATE_ASCII, Encoding{"DW_ATE_ASCII"}},

};

Encoding &getDWARFEncoding();
};

#endif /* ElfFile_H_ */
6 changes: 6 additions & 0 deletions src/Encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
Encoding::Encoding(std::string name) : name{name} {}

Encoding::Encoding() {}

std::string& Encoding::getName() { return name; }

void Encoding::setId(int newId) { id = newId; }

int Encoding::getId() const { return id; }
10 changes: 10 additions & 0 deletions src/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@

#include <string>

/**
* @brief The Encoding class
* Simple wrapper for DWARF5 encoding macros.
* Read DWARF5 specification document section 5.1.1 titled "Base Type Encodings"
* for more details.
*/

class Encoding
{
public:
Encoding(std::string name);
Encoding();
std::string& getName();
void setId(int);
int getId() const;

private:
std::string name;
Expand Down
29 changes: 25 additions & 4 deletions src/Juicer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3226,10 +3226,11 @@ Symbol *Juicer::process_DW_TAG_base_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Di
Dwarf_Attribute attr_struct;
Symbol *outSymbol = nullptr;
std::string cName;
Dwarf_Error error = 0;
Dwarf_Error error = 0;
Dwarf_Signed encodingValue = -1;

/* Get the name attribute of this Die. */
res = dwarf_attr(inDie, DW_AT_name, &attr_struct, &error);
res = dwarf_attr(inDie, DW_AT_name, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
Expand All @@ -3256,6 +3257,21 @@ Symbol *Juicer::process_DW_TAG_base_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Di
cName = dieName;
}

res = dwarf_attr(inDie, DW_AT_encoding, &attr_struct, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_attr(DW_AT_name). %u errno=%u %s", __LINE__, dwarf_errno(error), dwarf_errmsg(error));
}

if (res == DW_DLV_OK)
{
res = dwarf_formsdata(attr_struct, &encodingValue, &error);
if (res != DW_DLV_OK)
{
logger.logError("Error in dwarf_formstring. errno=%u %s", dwarf_errno(error), dwarf_errmsg(error));
}
}

outSymbol = elf.getSymbol(cName);

if (outSymbol == 0)
Expand Down Expand Up @@ -3342,6 +3358,7 @@ Symbol *Juicer::process_DW_TAG_base_type(ElfFile &elf, Dwarf_Debug dbg, Dwarf_Di
}
}
}
outSymbol->setEncoding(encodingValue);
}
}

Expand All @@ -3355,6 +3372,7 @@ void Juicer::process_DW_TAG_enumeration_type(ElfFile &elf, Symbol &symbol, Dwarf
Dwarf_Die enumeratorDie = 0;
Dwarf_Signed encodingValue;
Dwarf_Error error = 0;
std::string symbolEncoding{};

/* Get the fields by getting the first child. */
if (res == DW_DLV_OK)
Expand Down Expand Up @@ -3446,13 +3464,15 @@ void Juicer::process_DW_TAG_enumeration_type(ElfFile &elf, Symbol &symbol, Dwarf
{
case DW_ATE_signed:
{
res = dwarf_formsdata(attr_struct, &enumeratorValue, &error);
res = dwarf_formsdata(attr_struct, &enumeratorValue, &error);
symbolEncoding = "DW_ATE_signed";
break;
}

case DW_ATE_unsigned:
{
res = dwarf_formudata(attr_struct, (Dwarf_Unsigned *)&enumeratorValue, &error);
res = dwarf_formudata(attr_struct, (Dwarf_Unsigned *)&enumeratorValue, &error);
symbolEncoding = "DW_ATE_unsigned";
break;
}
default:
Expand All @@ -3474,6 +3494,7 @@ void Juicer::process_DW_TAG_enumeration_type(ElfFile &elf, Symbol &symbol, Dwarf
Dwarf_Die siblingDie = 0;

symbol.addEnumeration(sEnumeratorName, enumeratorValue);
symbol.setEncoding(encodingValue);

res = dwarf_siblingof(dbg, enumeratorDie, &siblingDie, &error);
if (res == DW_DLV_ERROR)
Expand Down
Loading

0 comments on commit 186880c

Please sign in to comment.