Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


CXX ?= g++
CXXFLAGS=-m32 -fPIC -shared -Wall -Wextra -std=c++11
LDFLAGS+=-static-libgcc -static-libstdc++
Expand All @@ -15,14 +13,14 @@ bin/dwarfexport.plx: $(DWARFEXPORT_SRC)
-L. \
-L$(IDA_PATH) \
$(INCLUDES) \
-D__LINUX__ $(LIBS) -o bin/dwarfexport.plx
-D__LINUX__ -D__X64__ $(LIBS) -o bin/dwarfexport.plx

bin/dwarfexport.plx64: $(DWARFEXPORT_SRC)
$(CXX) $(LDFLAGS) $(DWARFEXPORT_SRC) $(CXXFLAGS) \
-L. \
-L$(IDA_PATH) \
$(INCLUDES) \
-D__LINUX__ -D__EA64__ $(LIBS) -o bin/dwarfexport.plx64
-D__LINUX__ -D__X64 -D__EA64__ $(LIBS) -o bin/dwarfexport.plx64

clean:
rm -f bin/dwarfexport.plx bin/dwarfexport.plx64
28 changes: 14 additions & 14 deletions Makefile.osx
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
CXX ?= clang++
MACSDK=$(shell xcrun --show-sdk-path --sdk macosx)
CXXFLAGS=-m32 -fPIC -shared -Wall -Wextra -std=c++14 -isysroot $(MACSDK) -stdlib=libc++ -DUSE_DANGEROUS_FUNCTIONS=1 -DUSE_STANDARD_FILE_FUNCTIONS=1
LDFLAGS=-Lthirdparty/libelf-0.8.13/lib -Lthirdparty/libdwarf/libdwarf
CXXFLAGS=-fPIC -shared -Wall -Wextra -std=c++11 -isysroot $(MACSDK) -stdlib=libc++ -DUSE_DANGEROUS_FUNCTIONS=1 -DUSE_STANDARD_FILE_FUNCTIONS=1
LDFLAGS=-L/opt/local/lib/
LIBS=-lelf -ldwarf -lz
INCLUDES=-I$(IDASDK_PATH)/include -I$(IDA_PATH)/plugins/hexrays_sdk/include -Ithirdparty/libdwarf -I/usr/local/include
INCLUDES=-I$(IDASDK_PATH)/include -I$(IDA_PATH)/plugins/hexrays_sdk/include -I/opt/local/include/libelf -I/opt/local/include

DWARFEXPORT_SRC = $(wildcard src/*.cpp)

all: check-env bin/dwarfexport.pmc bin/dwarfexport.pmc64
all: check-env bin/dwarfexport.dylib bin/dwarfexport64.dylib

bin/dwarfexport.pmc: $(DWARFEXPORT_SRC)
bin/dwarfexport.dylib: $(DWARFEXPORT_SRC)
$(CXX) $(LDFLAGS) $(DWARFEXPORT_SRC) $(CXXFLAGS) \
-L. \
-L$(IDA_PATH) \
-L$(IDA_PATH) -L$(IDASDK_PATH)/lib/x64_mac_gcc_32/ \
$(INCLUDES) \
-D__MAC__ $(LIBS) -lida -o bin/dwarfexport.pmc
-D__MAC__ -D__X64__ $(LIBS) -lida -o bin/dwarfexport.dylib

bin/dwarfexport.pmc64: $(DWARFEXPORT_SRC)
bin/dwarfexport64.dylib: $(DWARFEXPORT_SRC)
$(CXX) $(LDFLAGS) $(DWARFEXPORT_SRC) $(CXXFLAGS) \
-L. \
-L$(IDA_PATH) \
-L$(IDA_PATH) -L$(IDASDK_PATH)/lib/x64_mac_gcc_64/ \
$(INCLUDES) \
-D__MAC__ -D__EA64__ $(LIBS) -lida64 -o bin/dwarfexport.pmc64
-D__MAC__ -D__X64__ -D__EA64__ $(LIBS) -lida64 -o bin/dwarfexport64.dylib

clean:
rm -f bin/dwarfexport.pmc bin/dwarfexport.pmc64
rm -f bin/dwarfexport.dylib bin/dwarfexport64.dylib

install:
cp -f bin/dwarfexport.pmc $(IDA_PATH)/plugins/
cp -f bin/dwarfexport.pmc64 $(IDA_PATH)/plugins/
cp -f bin/dwarfexport.dylib $(IDA_PATH)/plugins/
cp -f bin/dwarfexport64.dylib $(IDA_PATH)/plugins/

check-env:
ifndef IDASDK_PATH
Expand All @@ -38,4 +38,4 @@ ifndef IDA_PATH
$(error IDA_PATH is undefined)
endif

.PHONY: check-env
.PHONY: check-env
Binary file removed bin/dwarfexport.p64
Binary file not shown.
Binary file removed bin/dwarfexport.plw
Binary file not shown.
Binary file removed bin/dwarfexport.plx
Binary file not shown.
Binary file removed bin/dwarfexport.plx64
Binary file not shown.
Binary file removed bin/dwarfexport.pmc
Binary file not shown.
Binary file removed bin/dwarfexport.pmc64
Binary file not shown.
80 changes: 43 additions & 37 deletions src/dwarfexport.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

#include <cstdio>
#include <cstdlib>
#include <frame.hpp>
#include <fstream>
#include <ida.hpp>
//#include <hexrays.hpp>
#include <idp.hpp>
#include <kernwin.hpp>
#include <loader.hpp>
#include <name.hpp>
#include <range.hpp>
#include <segment.hpp>
#include <string>
#include <struct.hpp>

Expand Down Expand Up @@ -73,7 +75,7 @@ static Dwarf_P_Die add_struct_type(Dwarf_P_Debug dbg, Dwarf_P_Die cu,
for (int i = 0; i < member_count; ++i) {
udt_member_t member;
member.offset = i;
type.find_udt_member(STRMEM_INDEX, &member);
type.find_udt_member(&member, STRMEM_INDEX);
auto member_type = member.type;
auto member_die =
dwarf_new_die(dbg, DW_TAG_member, die, NULL, NULL, NULL, &err);
Expand Down Expand Up @@ -149,7 +151,7 @@ static Dwarf_P_Die add_array_type(Dwarf_P_Debug dbg, Dwarf_P_Die cu,
qstring name;

// Try to get size_t and use it for the index type
if (parse_decl2(idati, "size_t x;", &name, &size_type, PT_SIL)) {
if (parse_decl(&size_type, &name, NULL, "size_t x;", PT_SIL)) {
auto index_die = get_or_add_type(dbg, cu, size_type, record);
if (dwarf_add_AT_reference(dbg, subrange, DW_AT_type, index_die,
&err) == nullptr) {
Expand Down Expand Up @@ -361,7 +363,7 @@ static void add_disassembler_func_info(std::shared_ptr<DwarfGenInfo> info,
}

for (std::size_t i = 0; i < frame->memqty; ++i) {
auto name = get_member_name2(frame->members[i].id);
auto name = get_member_name(frame->members[i].id);

// Ignore these special 'variables'
if (name == " s" || name == " r") {
Expand All @@ -387,7 +389,7 @@ static void add_disassembler_func_info(std::shared_ptr<DwarfGenInfo> info,
auto member_struct = get_sptr(&frame->members[i]);
if (member_struct) {
tinfo_t type;
if (guess_tinfo2(member_struct->id, &type) == GUESS_FUNC_OK) {
if (guess_tinfo(&type, member_struct->id) == GUESS_FUNC_OK) {
auto var_type_die = get_or_add_type(dbg, cu, type, record);
if (dwarf_add_AT_reference(dbg, die, DW_AT_type, var_type_die, &err) ==
nullptr) {
Expand Down Expand Up @@ -432,7 +434,7 @@ static void add_decompiler_func_info(std::shared_ptr<DwarfGenInfo> info,
cfuncptr_t cfunc = decompile(func, &hf);

if (cfunc == nullptr) {
dwarfexport_log("Failed to decompile function at ", func->startEA);
dwarfexport_log("Failed to decompile function at ", func->start_ea);
return;
}

Expand All @@ -450,11 +452,11 @@ static void add_decompiler_func_info(std::shared_ptr<DwarfGenInfo> info,
const auto &eamap = cfunc->get_eamap();
ea_t previous_line_addr = 0;
for (std::size_t i = 0; i < sv.size(); ++i, ++linecount) {
char buf[MAXSTR];
const char *line = sv[i].line.c_str();
tag_remove(line, buf, MAXSTR);
qstring buf;
qstring line = sv[i].line;
tag_remove(&buf, line);

auto stripped_buf = std::string(buf);
auto stripped_buf = std::string(buf.c_str());
file << stripped_buf + "\n";

dwarfexport_log("Processing line: ", stripped_buf);
Expand All @@ -470,7 +472,7 @@ static void add_decompiler_func_info(std::shared_ptr<DwarfGenInfo> info,
// for that.
ea_t lowest_line_addr = 0, highest_line_addr = 0;
for (; index < stripped_buf.size(); ++index) {
if (!cfunc->get_line_item(line, index, true, nullptr, &item, nullptr)) {
if (!cfunc->get_line_item(line.c_str(), index, true, nullptr, &item, nullptr)) {
continue;
}

Expand All @@ -484,7 +486,7 @@ static void add_decompiler_func_info(std::shared_ptr<DwarfGenInfo> info,

// The address for this expression is outside of this function,
// so something strange is happening. Just ignore it.
if (addr == (ea_t)-1 || addr < func->startEA || addr > func->endEA) {
if (addr == (ea_t)-1 || addr < func->start_ea || addr > func->end_ea) {
continue;
}

Expand All @@ -495,8 +497,8 @@ static void add_decompiler_func_info(std::shared_ptr<DwarfGenInfo> info,
const auto &expr_areaset = bounds.at(eamap.at(addr).at(0));

// TODO: the area set may not be sorted this way
expr_lowest_addr = expr_areaset.getarea(0).startEA;
expr_highest_addr = expr_areaset.lastarea().endEA - 1;
expr_lowest_addr = expr_areaset.getrange(0).start_ea;
expr_highest_addr = expr_areaset.lastrange().end_ea - 1;
}

// In some situations, there are multiple lines that have the same
Expand Down Expand Up @@ -560,14 +562,14 @@ static Dwarf_P_Die add_function(std::shared_ptr<DwarfGenInfo> info,
}

// Add function name
auto name = get_long_name(func->startEA);
auto name = get_long_name(func->start_ea);
char *c_name = &*name.begin();

if (dwarf_add_AT_name(die, c_name, &err) == nullptr) {
dwarfexport_error("dwarf_add_AT_name failed: ", dwarf_errmsg(err));
}

auto mangled_name = get_true_name(func->startEA);
auto mangled_name = get_name(func->start_ea);
if (dwarf_add_AT_string(dbg, die, DW_AT_linkage_name, &mangled_name[0],
&err) == nullptr) {
dwarfexport_error("dwarf_add_AT_string failed: ", dwarf_errmsg(err));
Expand All @@ -577,7 +579,7 @@ static Dwarf_P_Die add_function(std::shared_ptr<DwarfGenInfo> info,

// Add ret type
tinfo_t func_type_info;
if (get_tinfo2(func->startEA, &func_type_info)) {
if (get_tinfo(&func_type_info, func->start_ea)) {
auto rettype = func_type_info.get_rettype();
auto rettype_die = get_or_add_type(dbg, cu, rettype, record);
if (dwarf_add_AT_reference(dbg, die, DW_AT_type, rettype_die, &err) ==
Expand All @@ -587,10 +589,10 @@ static Dwarf_P_Die add_function(std::shared_ptr<DwarfGenInfo> info,
}

// Add function bounds
dwarf_add_AT_targ_address(dbg, die, DW_AT_low_pc, func->startEA, 0, &err);
dwarf_add_AT_targ_address(dbg, die, DW_AT_high_pc, func->endEA - 1, 0, &err);
dwarf_add_AT_targ_address(dbg, die, DW_AT_low_pc, func->start_ea, 0, &err);
dwarf_add_AT_targ_address(dbg, die, DW_AT_high_pc, func->end_ea - 1, 0, &err);

auto is_named = has_name(getFlags(func->startEA));
auto is_named = has_name(get_flags(func->start_ea));
if (has_decompiler && options.use_decompiler() &&
(!options.only_decompile_named_funcs() ||
(options.only_decompile_named_funcs() && is_named))) {
Expand All @@ -600,7 +602,7 @@ static Dwarf_P_Die add_function(std::shared_ptr<DwarfGenInfo> info,
dwarf_add_AT_unsigned_const(dbg, die, DW_AT_decl_line, linecount, &err);

// The start of every function should have a line entry
dwarf_add_line_entry(dbg, file_index, func->startEA, linecount, 0, true,
dwarf_add_line_entry(dbg, file_index, func->start_ea, linecount, 0, true,
false, &err);

add_decompiler_func_info(info, cu, die, func, file, linecount, file_index,
Expand All @@ -624,7 +626,7 @@ void add_structures(Dwarf_P_Debug dbg, Dwarf_P_Die cu, type_record_t &record) {
auto tid = get_struc_by_idx(idx);
tinfo_t type;

if (guess_tinfo2(tid, &type) == GUESS_FUNC_OK) {
if (guess_tinfo(&type, tid) == GUESS_FUNC_OK) {
get_or_add_type(dbg, cu, type, record);
}
}
Expand All @@ -646,26 +648,29 @@ void add_global_variables(Dwarf_P_Debug dbg, Dwarf_P_Die cu,
continue;
}

for (auto addr = seg->startEA; addr < seg->endEA; ++addr) {
char name[MAXSTR];
if (!get_name(BADADDR, addr, name, MAXSTR)) {
for (auto addr = seg->start_ea; addr < seg->end_ea; ++addr) {
qstring name;
if (!get_name(&name, addr)) {
continue;
}

tinfo_t type;
if (guess_tinfo2(addr, &type) != GUESS_FUNC_OK) {
if (guess_tinfo(&type, addr) != GUESS_FUNC_OK) {
continue;
}

dwarfexport_log("Adding global variable");
dwarfexport_log(" name = ", name);
std::string lname(name.c_str());
dwarfexport_log(" name = ", lname);

dwarfexport_log(" location = ", addr);

auto die =
dwarf_new_die(dbg, DW_TAG_variable, cu, NULL, NULL, NULL, &err);
auto var_type_die = get_or_add_type(dbg, cu, type, record);

if (dwarf_add_AT_name(die, name, &err) == NULL) {

if (dwarf_add_AT_name(die, const_cast<char *>(name.c_str()), &err) == NULL) {
dwarfexport_error("dwarf_add_AT_name failed: ", dwarf_errmsg(err));
}

Expand Down Expand Up @@ -729,23 +734,24 @@ void add_debug_info(std::shared_ptr<DwarfGenInfo> info,
continue;
}

char segname[MAXSTR];
get_true_segm_name(seg, segname, sizeof(segname));
dwarfexport_log("Adding functions from: ", segname);
qstring segname;
get_segm_name(&segname, seg);
std::string lsegname(segname.c_str());
dwarfexport_log("Adding functions from: ", lsegname);

func_t *f = get_func(seg->startEA);
func_t *f = get_func(seg->start_ea);
if (f == nullptr) {
// In some cases, the start of the section may not actually be a function,
// so get the first available function.
f = get_next_func(seg->startEA);
f = get_next_func(seg->start_ea);

if (f == nullptr) {
dwarfexport_error("get_next_func() failed");
}
}

for (; f != nullptr; f = get_next_func(f->startEA)) {
if (f->startEA > seg->endEA) {
for (; f != nullptr; f = get_next_func(f->start_ea)) {
if (f->start_ea > seg->end_ea) {
break;
}

Expand Down Expand Up @@ -775,7 +781,7 @@ int idaapi init(void) {
return PLUGIN_OK;
}

void idaapi run(int) {
bool idaapi run(size_t) {
try {
auto default_options =
(has_decompiler) ? Options::ATTACH_DEBUG_INFO | Options::USE_DECOMPILER
Expand All @@ -799,7 +805,7 @@ void idaapi run(int) {
"<Attach Debug Info:C>\n"
"<Verbose:C>>\n";

if (AskUsingForm_c(dialog, options.filepath, &options.export_options) ==
if (ask_form(dialog, options.filepath, &options.export_options) ==
1) {

if (options.verbose()) {
Expand Down
2 changes: 1 addition & 1 deletion src/dwarfexport.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <fstream>
#include <hexrays.hpp>
#include <iostream>
#include <libdwarf/libdwarf.h>
#include <libdwarf.h>
#include <memory>
#include <sstream>
#include <stdexcept>
Expand Down
2 changes: 1 addition & 1 deletion src/dwarfgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ std::shared_ptr<DwarfGenInfo> generate_dwarf_object(const Options &options) {
const char *isa_name = (info->mode == Mode::BIT32) ? "x86" : "x86_64";

const char *dwarf_version = "V2";
int endian = (inf.mf) ? DW_DLC_TARGET_BIGENDIAN : DW_DLC_TARGET_LITTLEENDIAN;
int endian = (inf.is_be()) ? DW_DLC_TARGET_BIGENDIAN : DW_DLC_TARGET_LITTLEENDIAN;
Dwarf_Ptr errarg = 0;

decltype(&attached_info_callback) callback;
Expand Down