Skip to content

Commit

Permalink
Fixes for #1 and #2, couple things more!
Browse files Browse the repository at this point in the history
  • Loading branch information
InusualZ committed Jan 21, 2018
1 parent 0b39562 commit 004f739
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 52 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.7)
project(SymbolExporter)
cmake_minimum_required(VERSION 3.5)
project(SymbolExporter CXX)

if (MSVC)
message(FATAL_ERROR "Sorry, Visual Studio is not supported. Only MinGW")
endif (MSVC)
endif ()

set(NAME "SymbolExporter")
set(CMAKE_CXX_STANDARD 11)
Expand All @@ -12,7 +12,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -Wall -static-libgcc -static-libs
set(SRC
src/Classyfier.cpp
src/Main.cpp
src/Object.cpp)
src/Object.cpp
src/Symbol32.cpp
src/VTable.cpp)

set(INCLUDES
src/elfio/elf_types.hpp
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ This software would let you export symbols and vtables from an ELF File to it's

# Options
* `-i` or `--input` - It's the ELF File to be parse. Obligatory
* `-o` or `--output` - Where you want to store the output. Default `output/`
* `-o` or `--output` - Where you want to store the output. Default `output/`
* `-m` or `--mangled` - Would write mangled symbol on comment form before the demangled symbol

# Compiling Requirement
* GCC v5.4 or MinGW (Might work but you may need to tweak some file first. If you do please make an PR, It would be greatly appreciated)
* CMake v3.5
* Git (Optional: You could download the source directly from the repo)

# Compiling
This software have to be compiled in a GCC Compiler. MingGW works fine.
Use the makefile
* `git clone https//github.com/InusualZ/SymbolExporter`
* `cd SymbolExporter`
* `mkdir build && cd build`
* `cmake ..`
* `make`
Yay! You have compiled the program! Enjoy!

# Disclaimer
```
This SymbolExporter is provided by InusualZ (Wesley Moret) "as is" and "with all faults."
InusualZ (Wesley Moret) makes no representations or warranties of any kind concerning the safety, suitability, lack of viruses, inaccuracies, typographical errors, or other harmful components of this SymbolExporter.
There are inherent dangers in the use of any software, and you are solely responsible for determining whether this SymbolExporter is compatible with your equipment and other software installed on your equipment.
You are also solely responsible for the protection of your equipment and backup of your data, and InusualZ (Wesley Moret) will not be liable for any damages you may suffer in connection with using, modifying, or distributing this SymbolExporter.
```
```
46 changes: 40 additions & 6 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ std::vector<Symbol32> getSymbols(const ELFIO::endianess_convertor& convertor, co
uint32_t valueOffset = convertor(pSym->st_value);
uint32_t size = convertor(pSym->st_size);
unsigned char bind = ELF_ST_BIND(pSym->st_info);
unsigned char type = static_cast<unsigned char>(ELF_ST_TYPE(pSym->st_info));
auto type = static_cast<unsigned char>(ELF_ST_TYPE(pSym->st_info));
uint16_t sectionIndex = convertor(pSym->st_shndx);
unsigned char other = pSym->st_other;

Expand All @@ -97,7 +97,7 @@ std::vector<Symbol32> getSymbols(const ELFIO::endianess_convertor& convertor, co
}

// Demangled
const char* demangled = demangle(name);
const char* demangled = Symbol32::demangle(name);

// Push Symbol
entries.emplace_back(i, demangled, name, nameOffset, valueOffset, size, bind, type, sectionIndex, other);
Expand Down Expand Up @@ -147,12 +147,15 @@ int main(int argc, const char** argv) {

Object::commentMangledSymbol = options.is_set("writeMangled");

auto startTime = std::chrono::high_resolution_clock::now();

ELFIO::elfio reader;
if (!reader.load(inputFile)) {
std::cerr << "FAILED TO LOAD BINARY: " << inputFile << std::endl;
return 1;
}

// TODO: Remove this restriction!
if (reader.get_class() != ELFCLASS32) {
std::cerr << "Only Elf 32bit is supported! Contact Developer!\n";
return 1;
Expand All @@ -179,15 +182,33 @@ int main(int argc, const char** argv) {
getVTablesContent(reader, vtables, symbols);
std::cout << "Done! Gathering their information\n";

// Creating output dir
int res = mkdir(outputFolder.c_str(), 777);

This comment has been minimized.

Copy link
@julianwi

julianwi Jan 30, 2018

the permission has to be written as octal number. You should change 777 to 0777.

if (res != 0 && errno != 17) {
std::cout << "Error, creating output folder. Make sure to give the program the appropiate permission.\n";
std::cout << "Errno(" << errno << "): " << std::strerror(errno);
return 1;
}

std::cout << "Writing Symbols to disk\n";
std::ofstream outputFile(outputFolder + "symbols.txt", std::ios_base::out | std::ios_base::trunc);
if (!outputFile.good()) {
std::cout << "Unable to write Symbol to disk.\n";
return 1;
}

for (const auto& symbol : symbols) {
outputFile << "[0x" << std::setfill('0') << std::setw(8) << std::hex << symbol.valueOffset - 1 << "] " << symbol.demangled << '\n';
}
outputFile.close();

std::cout << "Writing Virtual Tables to disk\n";
outputFile.open(outputFolder + "vtables.txt", std::ios_base::out | std::ios_base::trunc);
if (!outputFile.good()) {
std::cout << "Unable to write Virtual Tables to disk.\n";
return 1;
}

for (const auto& table : vtables) {
outputFile << '\n' << table.symbol->demangled << '\n';
for (const auto& it : table.content) {
Expand All @@ -214,21 +235,34 @@ int main(int argc, const char** argv) {
// Separate headers
outputFolder += "Headers/";

std::cout << "Writting to disk " << converter.getObjects().size() << " classes\n";

// Create folder
mkdir(outputFolder.c_str(), 0777);
res = mkdir(outputFolder.c_str(), 777);

This comment has been minimized.

Copy link
@julianwi

julianwi Jan 30, 2018

same as line 186

if (res != 0 && errno != 17) {
std::cout << "Error, creating header output folder. Make sure to give the program the appropriate permission.\n";
std::cout << "Errno(" << errno << "): " << std::strerror(errno);
return 1;
}

std::cout << "Writing to disk " << converter.getObjects().size() << " classes\n";

// Serialize objects to class
std::ofstream clazz;
for (const auto& object : converter.getObjects()) {
clazz.open(outputFolder + object->name + ".h", std::ios_base::out | std::ios_base::trunc);
if (!clazz.good()) {
std::cout << "Unable to write to: " << (outputFolder + object->name + ".h") << '\n';
clazz.close();
continue;
}

clazz << "#pragma once\n\n";
clazz << *object;
clazz.close();
}

std::cout << "DONE!\n";
auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startTime);

std::cout << "Done! It took " << endTime.count() << "ms\n";

return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Object.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Object.h"

bool Object::commentMangledSymbol = false;

Object::Object(Type objectType, std::string objectName, Symbol32* originalSymbol)
: type(objectType), name(std::move(objectName)), symbol(originalSymbol), vtable(nullptr) {}

Expand Down
2 changes: 1 addition & 1 deletion src/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "VTable.h"

struct Object {
static bool commentMangledSymbol = false; // The mangled symbol would be write before the demangled symbol in comment form
static bool commentMangledSymbol; // The mangled symbol would be write before the demangled symbol in comment form
enum Type {
NAMESPACE,
CLASS,
Expand Down
30 changes: 30 additions & 0 deletions src/Symbol32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "Symbol32.h"

#include <cstring>

#if defined(__GNUC__) || defined(__MINGW32__)
#include <cxxabi.h>
#else
#error "The program can only work on GCC Compilers"
#endif

Symbol32::Symbol32(uint32_t index_, const char* demangled_, const char* mangled_, uint32_t nameOffset_,
uint32_t valueOffset_, uint32_t size_, unsigned char bind_, unsigned char type_,
uint16_t sectionIndex_, unsigned char other_) :
index(index_), demangled(demangled_), mangled(mangled_), nameOffset(nameOffset_),
valueOffset(valueOffset_), size(size_), bind(bind_), type(type_),
sectionIndex(sectionIndex_), other(other_) {

isStatic = sectionIndex != 11 && bind == 1;
isDestructor = std::strstr(demangled, "::~") != nullptr;
}

const char* Symbol32::demangle(const char* mangled) {
int status;
char* demangled = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
if (status != 0) {
return "";
}

return demangled;
}
29 changes: 3 additions & 26 deletions src/Symbol32.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
#pragma once

#include <cstdint>
#include <cstring>

#if defined(__GNUC__) || defined(__MINGW32__)

#include <cxxabi.h>

#else
#error "The program can only work on GCC Compilers"
#endif

static const char* demangle(const char* mangled) {
int status;
char* demangled = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
if (status != 0) {
return "";
}

return demangled;
}

struct Symbol32 {
uint32_t index;
Expand All @@ -40,11 +21,7 @@ struct Symbol32 {
bool isStatic = false;

Symbol32(uint32_t index_, const char* demangled_, const char* mangled_, uint32_t nameOffset_, uint32_t valueOffset_,
uint32_t size_, unsigned char bind_, unsigned char type_, uint16_t sectionIndex_, unsigned char other_)
: index(index_), demangled(demangled_), mangled(mangled_), nameOffset(nameOffset_),
valueOffset(valueOffset_), size(size_), bind(bind_), type(type_),
sectionIndex(sectionIndex_), other(other_) {
isStatic = sectionIndex != 11 && bind == 1;
isDestructor = std::strstr(demangled, "::~") != nullptr;
}
uint32_t size_, unsigned char bind_, unsigned char type_, uint16_t sectionIndex_, unsigned char other_);

static const char* demangle(const char* mangled);
};
16 changes: 16 additions & 0 deletions src/VTable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "VTable.h"

#include <cstring>

#include "Symbol32.h"

VTable::VTable(Symbol32* symbol_) : symbol(symbol_) {
size = symbol_->size / 4 - 2;
baseOffset = symbol_->valueOffset + 8;
maxOffset = baseOffset + size * 4;

auto len = strlen(symbol->demangled);
for (size_t i = 11; i < len; ++i) {
name += symbol->demangled[i];
}
}
13 changes: 2 additions & 11 deletions src/VTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cstdint>
#include <map>

#include "Symbol32.h"
class Symbol32;

struct VTable {
std::string name;
Expand All @@ -13,14 +13,5 @@ struct VTable {
std::uint32_t maxOffset;
std::map<uint32_t, Symbol32*> content;

explicit VTable(Symbol32* symbol_) : symbol(symbol_) {
size = symbol_->size / 4 - 2;
baseOffset = symbol_->valueOffset + 8;
maxOffset = baseOffset + size * 4;

auto len = strlen(symbol->demangled);
for (size_t i = 11; i < len; ++i) {
name += symbol->demangled[i];
}
}
explicit VTable(Symbol32* symbol_);
};

0 comments on commit 004f739

Please sign in to comment.