Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC] Rewrite FunctionFilter #186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
7 changes: 1 addition & 6 deletions ARM/ARMMachineInstructionRaiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ARMMachineInstructionRaiser::ARMMachineInstructionRaiser(
MachineFunction &MF, const ModuleRaiser *MR, MCInstRaiser *MCIR)
: MachineInstructionRaiser(MF, MR, MCIR), MachineRegInfo(MF.getRegInfo()) {}

bool ARMMachineInstructionRaiser::raiseMachineFunction() {
bool ARMMachineInstructionRaiser::raise() {
const ARMModuleRaiser *ConstAMR = dyn_cast<ARMModuleRaiser>(MR);
assert(ConstAMR != nullptr && "The ARM module raiser is not initialized!");
ARMModuleRaiser &AMR = const_cast<ARMModuleRaiser &>(*ConstAMR);
Expand Down Expand Up @@ -60,11 +60,6 @@ bool ARMMachineInstructionRaiser::raiseMachineFunction() {
return true;
}

bool ARMMachineInstructionRaiser::raise() {
return raiseMachineFunction();
// return true;
}

int ARMMachineInstructionRaiser::getArgumentNumber(unsigned PReg) {
// NYI
assert(false &&
Expand Down
1 change: 0 additions & 1 deletion ARM/ARMMachineInstructionRaiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ARMMachineInstructionRaiser : public MachineInstructionRaiser {
std::vector<JumpTableInfo> JTList;

private:
bool raiseMachineFunction();
// Commonly used LLVM data structures during this phase
MachineRegisterInfo &MachineRegInfo;
};
Expand Down
4 changes: 4 additions & 0 deletions ARM/ARMModuleRaiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ void ARMModuleRaiser::addRODataValueAt(Value *V, uint64_t Offset) const {
GlobalRODataValues.emplace(Offset, V);
}

bool ARMModuleRaiser::addPasses(PassManagerBase &PM) {
return true;
}

void registerARMModuleRaiser() {
registerModuleRaiser(new ARMModuleRaiser());
}
29 changes: 15 additions & 14 deletions ARM/ARMModuleRaiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_MCTOLL_ARM_ARMMODULERAISER_H
#define LLVM_TOOLS_LLVM_MCTOLL_ARM_ARMMODULERAISER_H
#ifndef LLVM_TOOLS_LLVM_MCTOLL_ARM_ARM_MODULE_RAISER_H
#define LLVM_TOOLS_LLVM_MCTOLL_ARM_ARM_MODULE_RAISER_H

#include "Raiser/ModuleRaiser.h"

Expand All @@ -30,33 +30,34 @@ class ARMModuleRaiser : public ModuleRaiser {
// Create a new MachineFunctionRaiser object and add it to the list of
// MachineFunction raiser objects of this module.
MachineFunctionRaiser *
CreateAndAddMachineFunctionRaiser(Function *f, const ModuleRaiser *mr,
uint64_t start, uint64_t end) override;
CreateAndAddMachineFunctionRaiser(Function *Fn, const ModuleRaiser *MR,
uint64_t Start, uint64_t End) override;
bool collectDynamicRelocations() override;
bool addPasses(PassManagerBase &PM) override;

void collectRodataInstAddr(uint64_t instAddr) {
InstArgCollect.push_back(instAddr);
void collectRodataInstAddr(uint64_t InstAddr) {
InstArgCollect.push_back(InstAddr);
}

void fillInstArgMap(uint64_t rodataAddr, uint64_t argNum) {
InstArgNumMap[rodataAddr] = argNum;
void fillInstArgMap(uint64_t RodataAddr, uint64_t ArgNum) {
InstArgNumMap[RodataAddr] = ArgNum;
}

void fillInstAddrFuncMap(uint64_t callAddr, Function *func) {
InstAddrFuncMap[callAddr] = func;
void fillInstAddrFuncMap(uint64_t CallAddr, Function *Fn) {
InstAddrFuncMap[CallAddr] = Fn;
}

Function *getCallFunc(uint64_t callAddr) { return InstAddrFuncMap[callAddr]; }
Function *getCallFunc(uint64_t CallAddr) { return InstAddrFuncMap[CallAddr]; }

// Get function arg number.
uint64_t getFunctionArgNum(uint64_t);

// Accoring call instruction to get the rodata instruction addr.
uint64_t getArgNumInstrAddr(uint64_t);
// Method to map syscall.
void setSyscallMapping(uint64_t idx, Function *fn) { SyscallMap[idx] = fn; }
void setSyscallMapping(uint64_t Idx, Function *Fn) { SyscallMap[Idx] = Fn; }

Function *getSyscallFunc(uint64_t idx) { return SyscallMap[idx]; }
Function *getSyscallFunc(uint64_t Idx) { return SyscallMap[Idx]; }

const Value *getRODataValueAt(uint64_t Offset) const;

Expand Down Expand Up @@ -86,4 +87,4 @@ class ARMModuleRaiser : public ModuleRaiser {

extern "C" void registerARMModuleRaiser();

#endif // LLVM_TOOLS_LLVM_MCTOLL_ARM_ARMMODULERAISER_H
#endif // LLVM_TOOLS_LLVM_MCTOLL_ARM_ARM_MODULE_RAISER_H
77 changes: 76 additions & 1 deletion Raiser/FunctionFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void FunctionFilter::eraseFunctionBySymbol(StringRef &Sym,

/// Read the function symbol set from the configuration file of filter
/// functions.
bool FunctionFilter::readFilterFunctionConfigFile(
bool FunctionFilter::readFunctionFilterConfigFile(
std::string &FunctionFilterFilename) {

if (FunctionFilterFilename.size() == 0)
Expand Down Expand Up @@ -362,3 +362,78 @@ void FunctionFilter::dump(FilterType FT) {
});
}
}

/// Check if function is needs raising
bool FunctionFilter::checkFunctionFilter(StringRef &PrototypeStr, uint64_t Start) {
bool RaiseFunc = true;
// Check the symbol name whether it should be excluded or not.
// Check in a non-empty exclude list
if (!isFilterSetEmpty(FunctionFilter::FILTER_EXCLUDE)) {
FunctionFilter::FuncInfo *FI = findFuncInfoBySymbol(
PrototypeStr, FunctionFilter::FILTER_EXCLUDE);
if (FI != nullptr) {
// Record the function start index.
FI->StartIdx = Start;
// Skip raising this function symbol
RaiseFunc = false;
}
}

if (!isFilterSetEmpty(FunctionFilter::FILTER_INCLUDE)) {
// Include list specified. Unless the current function symbol is
// specified in the include list, skip raising it.
RaiseFunc = false;
// Check the symbol name whether it should be included or not.
if (findFuncInfoBySymbol(
PrototypeStr, FunctionFilter::FILTER_INCLUDE) != nullptr)
RaiseFunc = true;
}
return RaiseFunc;
}

/*
A list of symbol entries corresponding to CRT functions added by
the linker while creating an ELF executable. It is not necessary to
disassemble and translate these functions.
*/

static std::set<StringRef> ELFCRTSymbols = {
"call_weak_fn",
"deregister_tm_clones",
"__do_global_dtors_aux",
"__do_global_dtors_aux_fini_array_entry",
"_fini",
"frame_dummy",
"__frame_dummy_init_array_entry",
"_init",
"__init_array_end",
"__init_array_start",
"__libc_csu_fini",
"__libc_csu_init",
"register_tm_clones",
"_start",
"_dl_relocate_static_pie"};

/*
A list of symbol entries corresponding to CRT functions added by
the linker while creating an MachO executable. It is not necessary
to disassemble and translate these functions.
*/

static std::set<StringRef> MachOCRTSymbols = {"__mh_execute_header",
"dyld_stub_binder", "__text",
"__stubs", "__stub_helper"};

/// Check if function is CRT function.
bool FunctionFilter::isCRTFunction(const ObjectFile *Obj, StringRef &Sym) {
if (Obj->isELF()) {
return (ELFCRTSymbols.find(Sym) != ELFCRTSymbols.end());
}
if (Obj->isMachO()) {
// If Symbol is not in the MachOCRTSymbol list return true indicating that
// this is a symbol of a function we are interested in disassembling and
// raising.
return (MachOCRTSymbols.find(Sym) != MachOCRTSymbols.end());
}
return false;
}
10 changes: 9 additions & 1 deletion Raiser/FunctionFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"

namespace llvm {
namespace mctoll {

using namespace object;

/// Class encapsulating lists of function specifications to be included and
/// excluded along with methods to maintain and query the lists.
class FunctionFilter {
Expand Down Expand Up @@ -80,9 +84,13 @@ class FunctionFilter {
/// Get the data type corresponding to type string.
Type *getPrimitiveDataType(const StringRef &TypeStr);
/// Read user-specified include and exclude functions from file
bool readFilterFunctionConfigFile(std::string &FunctionFilterFilename);
bool readFunctionFilterConfigFile(std::string &FunctionFilterFilename);
/// Test if the list of specified list is empty.
bool isFilterSetEmpty(FilterType);
/// Check if function is needs raising.
bool checkFunctionFilter(StringRef &PrototypeStr, uint64_t Start);
/// Check if function is CRT function.
bool isCRTFunction(const ObjectFile *Obj, StringRef &Sym);
/// Dump the list of specified list; dump both include and exclude lists if no
/// argument is specified.
void dump(FilterType FT = FILTER_NONE);
Expand Down
Loading