本文整理汇总了C++中ConstantStruct类的典型用法代码示例。如果您正苦于以下问题:C++ ConstantStruct类的具体用法?C++ ConstantStruct怎么用?C++ ConstantStruct使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConstantStruct类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addObjCClass
/// addObjCClass - Parse i386/ppc ObjC class data structure.
void LTOModule::addObjCClass(GlobalVariable *clgv) {
ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
if (!c) return;
// second slot in __OBJC,__class is pointer to superclass name
std::string superclassName;
if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
NameAndAttributes info;
StringMap<NameAndAttributes>::value_type &entry =
_undefines.GetOrCreateValue(superclassName);
if (!entry.getValue().name) {
const char *symbolName = entry.getKey().data();
info.name = symbolName;
info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
info.isFunction = false;
info.symbol = clgv;
entry.setValue(info);
}
}
// third slot in __OBJC,__class is pointer to class name
std::string className;
if (objcClassNameFromExpression(c->getOperand(2), className)) {
StringSet::value_type &entry = _defines.GetOrCreateValue(className);
entry.setValue(1);
NameAndAttributes info;
info.name = entry.getKey().data();
info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
info.isFunction = false;
info.symbol = clgv;
_symbols.push_back(info);
}
}
示例2: readFuncList
static void readFuncList(GlobalVariable *Array, std::vector<Constant*> *Funcs) {
if (!Array->hasInitializer())
return;
Constant *Init = Array->getInitializer();
ArrayType *Ty = dyn_cast<ArrayType>(Init->getType());
if (!Ty) {
errs() << "Initializer: " << *Array->getInitializer() << "\n";
report_fatal_error("ExpandCtors: Initializer is not of array type");
}
if (Ty->getNumElements() == 0)
return;
ConstantArray *InitList = dyn_cast<ConstantArray>(Init);
if (!InitList) {
errs() << "Initializer: " << *Array->getInitializer() << "\n";
report_fatal_error("ExpandCtors: Unexpected initializer ConstantExpr");
}
std::vector<FuncArrayEntry> FuncsToSort;
for (unsigned Index = 0; Index < InitList->getNumOperands(); ++Index) {
ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(Index));
FuncArrayEntry Entry;
Entry.priority = cast<ConstantInt>(CS->getOperand(0))->getZExtValue();
Entry.func = CS->getOperand(1);
FuncsToSort.push_back(Entry);
}
std::sort(FuncsToSort.begin(), FuncsToSort.end(), compareEntries);
for (std::vector<FuncArrayEntry>::iterator Iter = FuncsToSort.begin();
Iter != FuncsToSort.end();
++Iter) {
Funcs->push_back(Iter->func);
}
}
示例3: Element
CtorDtorIterator::Element CtorDtorIterator::operator*() const {
ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors");
Constant *FuncC = CS->getOperand(1);
Function *Func = nullptr;
// Extract function pointer, pulling off any casts.
while (FuncC) {
if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
Func = F;
break;
} else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
if (CE->isCast())
FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
else
break;
} else {
// This isn't anything we recognize. Bail out with Func left set to null.
break;
}
}
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
if (Data && !isa<GlobalValue>(Data))
Data = nullptr;
return Element(Priority->getZExtValue(), Func, Data);
}
示例4:
/// Find the llvm.global_ctors list, verifying that all initializers have an
/// init priority of 65535.
static GlobalVariable *findGlobalCtors(Module &M) {
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
if (!GV)
return nullptr;
// Verify that the initializer is simple enough for us to handle. We are
// only allowed to optimize the initializer if it is unique.
if (!GV->hasUniqueInitializer())
return nullptr;
if (isa<ConstantAggregateZero>(GV->getInitializer()))
return GV;
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
for (auto &V : CA->operands()) {
if (isa<ConstantAggregateZero>(V))
continue;
ConstantStruct *CS = cast<ConstantStruct>(V);
if (isa<ConstantPointerNull>(CS->getOperand(1)))
continue;
// Must have a function or null ptr.
if (!isa<Function>(CS->getOperand(1)))
return nullptr;
// Init priority must be standard.
ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
if (CI->getZExtValue() != 65535)
return nullptr;
}
return GV;
}
示例5: SDEBUG
void ClassHierarchyUtils::findClassHierarchy(Module& M) {
// Extract class hierarchy using std::type_info structures rather than debug
// info. The former works even for when there are anonymous namespaces.
// (for more info, see http://mentorembedded.github.io/cxx-abi/abi.html)
//
// Class names are usually global, except in the case of anonymous namespaces, in which case
// they may be renamed during linking. This renaming is not consistent across type_info and
// vtables. For example, the following are for the same class:
// _ZTIN7content12_GLOBAL__N_115HeaderFlattenerE60908 and
// _ZTVN7content12_GLOBAL__N_115HeaderFlattenerE60906.
//
// (renamed from
// _ZTVN7content12_GLOBAL__N_115HeaderFlattenerE and
// _ZTIN7content12_GLOBAL__N_115HeaderFlattenerE).
//
// VTables give us the corresponding type_info, so we process them first and store
// the VT <-> TI mappings, as this will be useful later.
for (Module::global_iterator I=M.global_begin(), E=M.global_end(); I != E; I++) {
GlobalVariable* VT = &*I;
if (VT->getName().startswith("_ZTV")) {
SDEBUG("soaap.util.classhierarchy", 3, dbgs() << "Found vtable: " << VT->getName() << "\n");
if (VT->hasInitializer()) {
ConstantStruct* VTinit = cast<ConstantStruct>(VT->getInitializer());
// all occurrences of a type_info object refer to this class's type_info, so we
// can pick the first array
ConstantArray* VTinitElem = cast<ConstantArray>(VTinit->getOperand(0));
for (int i=0; i<VTinitElem->getNumOperands(); i++) {
// type_info will be the first global variable in the array.
// It's not always at a fixed index so we have to search for it...
// The first one corresponds to the primary vtable, all others
// correspond to secondary vtables. All type_info occurrences will
// be the same.
if (GlobalVariable* TI = dyn_cast<GlobalVariable>(VTinitElem->getOperand(i)->stripPointerCasts())) {
if (typeInfoToVTable.find(TI) != typeInfoToVTable.end()) {
report_fatal_error(TI->getName() + " <-> " + VT->getName() + " mapping already exists!");
}
// Record TI <-> VT mappings, as they will be useful later
typeInfoToVTable[TI] = VT;
vTableToTypeInfo[VT] = TI;
break; // we only need to process the first one
}
}
}
else {
SDEBUG("soaap.util.classhierarchy", 1, dbgs() << "WARNING: VTable " << VT->getName() << " does not have initializer\n");
}
}
}
// Process type_info structures, recording class-hierarchy information and base offsets
for (Module::global_iterator I=M.global_begin(), E=M.global_end(); I != E; I++) {
GlobalVariable* G = &*I;
if (G->getName().startswith("_ZTI")) {
SDEBUG("soaap.util.classhierarchy", 3, dbgs() << "Found type_info: " << G->getName() << "\n");
processTypeInfo(G, M);
}
}
SDEBUG("soaap.util.classhierarchy", 3, ppClassHierarchy(classToSubclasses));
}
示例6: errs
// add to analyze function pointer
bool PathList::extend_calledFunctionMap(Instruction *tmpfi, User *tmpuser, GlobalVariable *func_table, Function *ff)
{
// the first operand of GetElementPtrInst is user of the table
if (tmpfi->getOperand(0) == tmpuser) {
errs()<<"=====find function which calls command_table\n";
// coutn++;
// if the index of the table is a constant int
if (ConstantInt *cint = dyn_cast<ConstantInt>(tmpfi->getOperand(2))) {
// todo: deal with the constantint index
} else {// if the index is a variavle, then add all the funcitons of command_table into the CallGraph
// get the initialization value of the global table
if (Constant *C = func_table->getInitializer()) {
llvm::ConstantArray *ca = dyn_cast<llvm::ConstantArray>(&*C);
if (ca) {
errs()<<"Get ConstantArray ca success.\n";
errs()<<ca->getNumOperands()<<'\n';
// get the element of the global table
for (int cai = 0; cai < ca->getNumOperands(); cai++) {
Constant *tmpca = ca->getOperand(cai);
//tmpca->dump();
ConstantStruct *cs = dyn_cast<ConstantStruct>(&*tmpca);
if (cs) {
//errs()<<"get ConstantStruct cs success.\n";
//errs()<<cs->getNumOperands()<<'\n';
Constant *tmpcs = cs->getOperand(1);
//tmpcs->dump();
//errs() << tmpcs->getNumOperands()<<'\n';
//tmpcs->getType()->dump();
//tmpcs->getOperand(0)->dump();
// get the funciton pointer of the element
if (Function *csfunc = dyn_cast<Function>(&*(tmpcs->getOperand(0)))) {
//errs() << "get function tmpcs success.\n";
// add the current funciton ff to the calledFunctionMap
// as the funciton which calls function csfunc
FunctionMapTy::iterator it2 = calledFunctionMap.find(csfunc);
if(it2==calledFunctionMap.end()) //not find
{
calledFunctionMap[csfunc].push_back(std::make_pair(ff, tmpfi));
}
else {
it2->second.push_back(std::make_pair(ff, tmpfi));
}
}
}
}
} else
return false;
} else {
dbgs() << "Get the initialization value of the global table failed!\n";
return false;
}
}
} else
return false;
return true;
}
示例7: assert
Value *DbgStopPointInst::getDirectory() const {
// Once the operand indices are verified, update this assert
assert(LLVMDebugVersion == (7 << 16) && "Verify operand indices");
GlobalVariable *GV = cast<GlobalVariable>(getContext());
if (!GV->hasInitializer()) return NULL;
ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
return CS->getOperand(4);
}
示例8: report_fatal_error
void ControlFlowIntegrity::PatchDtorFunctions(void) {
GlobalVariable *global_dtors = _M.getNamedGlobal("llvm.global_dtors");
// check for dtor section
if (!global_dtors || !global_dtors->getOperand(0)) {
return;
}
Constant *c = global_dtors->getInitializer();
if (!c)
report_fatal_error("llvm.global_dtors without initializer!", false);
ConstantArray *CA = dyn_cast<ConstantArray>(c);
if (!CA)
report_fatal_error("Cast to ConstantArray failed", true);
for (Value *Op : ValueOpRange(*CA)) {
ConstantStruct *CS = dyn_cast<ConstantStruct>(Op);
if (!CS)
report_fatal_error("Cast to ConstantStruct failed", true);
Constant *FP = CS->getOperand(1);
if (FP->isNullValue())
break; // found a NULL termintator, stop here
Function *F = dyn_cast_or_null<Function>(FP);
if (F == NULL) {
// Strip off constant expression cast
ConstantExpr *CE = dyn_cast<ConstantExpr>(FP);
if (!CE)
report_fatal_error("Cast to ConstantExpr failed", true);
if (CE->isCast()) {
FP = CE->getOperand(0);
}
F = dyn_cast_or_null<Function>(FP);
}
if (!F)
report_fatal_error("Cast to Function failed", true);
// set visibility to hidden (do not export), unless it is already
// local (for ex. static), in which case we have to make it non-static
if (F->hasLocalLinkage()) {
F->setLinkage(llvm::GlobalValue::ExternalLinkage);
}
F->setVisibility(GlobalValue::HiddenVisibility);
_FiniFunctions.insert(F->getName());
}
if (global_dtors->getNumUses() > 0)
report_fatal_error("llvm.global_dtors uses count is > 0!", false);
global_dtors->removeFromParent();
}
示例9: parseGlobalCtors
static std::vector<Function*> parseGlobalCtors(GlobalVariable *GV) {
if (GV->getInitializer()->isNullValue())
return std::vector<Function *>();
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
std::vector<Function *> Result;
Result.reserve(CA->getNumOperands());
for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
ConstantStruct *CS = cast<ConstantStruct>(*i);
Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
}
return Result;
}
示例10: parseGlobalCtors
/// Given a llvm.global_ctors list that we can understand,
/// return a list of the functions and null terminator as a vector.
static std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
if (GV->getInitializer()->isNullValue())
return std::vector<Function *>();
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
std::vector<Function *> Result;
Result.reserve(CA->getNumOperands());
for (auto &V : CA->operands()) {
ConstantStruct *CS = cast<ConstantStruct>(V);
Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
}
return Result;
}
示例11: assert
// what a hack
static Function *getStubFunctionForCtorList(Module *m,
GlobalVariable *gv,
std::string name) {
assert(!gv->isDeclaration() && !gv->hasInternalLinkage() &&
"do not support old LLVM style constructor/destructor lists");
std::vector<Type *> nullary;
Function *fn = Function::Create(FunctionType::get(Type::getVoidTy(m->getContext()),
nullary, false),
GlobalVariable::InternalLinkage,
name,
m);
BasicBlock *bb = BasicBlock::Create(m->getContext(), "entry", fn);
// From lli:
// Should be an array of '{ int, void ()* }' structs. The first value is
// the init priority, which we ignore.
ConstantArray *arr = dyn_cast<ConstantArray>(gv->getInitializer());
if (arr) {
for (unsigned i=0; i<arr->getNumOperands(); i++) {
ConstantStruct *cs = cast<ConstantStruct>(arr->getOperand(i));
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 5)
// There is a third *optional* element in global_ctor elements (``i8
// @data``).
assert((cs->getNumOperands() == 2 || cs->getNumOperands() == 3) &&
"unexpected element in ctor initializer list");
#else
assert(cs->getNumOperands()==2 && "unexpected element in ctor initializer list");
#endif
Constant *fp = cs->getOperand(1);
if (!fp->isNullValue()) {
if (llvm::ConstantExpr *ce = dyn_cast<llvm::ConstantExpr>(fp))
fp = ce->getOperand(0);
if (Function *f = dyn_cast<Function>(fp)) {
CallInst::Create(f, "", bb);
} else {
assert(0 && "unable to get function pointer from ctor initializer list");
}
}
}
}
ReturnInst::Create(m->getContext(), bb);
return fn;
}
示例12: addObjCCategory
// Parse i386/ppc ObjC category data structure.
void LTOModule::addObjCCategory(GlobalVariable *clgv) {
ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
if (!c) return;
// second slot in __OBJC,__category is pointer to target class name
std::string targetclassName;
if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
return;
NameAndAttributes info;
StringMap<NameAndAttributes>::value_type &entry =
_undefines.GetOrCreateValue(targetclassName);
if (entry.getValue().name)
return;
const char *symbolName = entry.getKey().data();
info.name = symbolName;
info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
entry.setValue(info);
}
示例13: assert
bool CheckInserter::doFinalization(Module &M) {
// We couldn't directly add an element to a constant array, because doing so
// changes the type of the constant array.
// element type of llvm.global_ctors
StructType *ST = StructType::get(IntType,
PointerType::getUnqual(InitFiniType),
NULL); // end with null
// Move all existing elements of <GlobalName> to <Constants>.
vector<Constant *> Constants;
if (GlobalVariable *GlobalCtors = M.getNamedGlobal("llvm.global_ctors")) {
ConstantArray *CA = cast<ConstantArray>(GlobalCtors->getInitializer());
for (unsigned j = 0; j < CA->getNumOperands(); ++j) {
ConstantStruct *CS = cast<ConstantStruct>(CA->getOperand(j));
assert(CS->getType() == ST);
// Assume nobody is using the highest priority, so that <F> will be the
// first to run as a ctor.
assert(!cast<ConstantInt>(CS->getOperand(0))->isMinValue(true));
Constants.push_back(CS);
}
GlobalCtors->eraseFromParent();
}
// Add <F> with the highest priority.
Constants.push_back(ConstantStruct::get(ST,
ConstantInt::get(IntType, INT_MIN),
EnterProcess,
NULL));
// Create the new llvm.global_ctors.
ArrayType *ArrType = ArrayType::get(ST, Constants.size());
new GlobalVariable(M,
ArrType,
true,
GlobalValue::AppendingLinkage,
ConstantArray::get(ArrType, Constants),
"llvm.global_ctors");
return true;
}
示例14: processTypeInfo
void ClassHierarchyUtils::processTypeInfo(GlobalVariable* TI) {
if (find(classes.begin(), classes.end(), TI) == classes.end()) {
classes.push_back(TI);
// extract direct base classes from type_info
if (TI->hasInitializer()) {
ConstantStruct* TIinit = cast<ConstantStruct>(TI->getInitializer());
int TInumOperands = TIinit->getNumOperands();
if (TInumOperands > 2) {
// we have >= 1 base class(es).
if (TInumOperands == 3) {
// abi::__si_class_type_info
GlobalVariable* baseTI = cast<GlobalVariable>(TIinit->getOperand(2)->stripPointerCasts());
classToSubclasses[baseTI].push_back(TI);
//dbgs() << " " << *baseTI << "\n";
classToBaseOffset[TI][baseTI] = 0;
processTypeInfo(baseTI);
}
else {
// abi::__vmi_class_type_info
// (skip over first two additional fields, which are "flags" and "base_count")
for (int i=4; i<TInumOperands; i+=2) {
GlobalVariable* baseTI = cast<GlobalVariable>(TIinit->getOperand(i)->stripPointerCasts());
classToSubclasses[baseTI].push_back(TI);
int offset_flags = cast<ConstantInt>(TIinit->getOperand(i+1))->getSExtValue();
SDEBUG("soaap.util.classhierarchy", 3, dbgs() << TI->getName() << " -> " << baseTI->getName() << "\n");
SDEBUG("soaap.util.classhierarchy", 3, dbgs() << " offset_flags = " << offset_flags << "\n");
int offset = offset_flags >> 8; // TODO: is this value defined as a constant somewhere?
SDEBUG("soaap.util.classhierarchy", 3, dbgs() << " offset = " << offset << "\n");
classToBaseOffset[TI][baseTI] = offset;
//dbgs() << " " << *baseTI << "\n";
processTypeInfo(baseTI);
}
}
}
}
示例15: assert
void MemoryInstrumenter::lowerGlobalCtors(Module &M) {
// Find llvm.global_ctors.
GlobalVariable *GV = M.getNamedGlobal("llvm.global_ctors");
if (!GV)
return;
assert(!GV->isDeclaration() && !GV->hasLocalLinkage());
// Should be an array of '{ int, void ()* }' structs. The first value is
// the init priority, which must be 65535 if the bitcode is generated using
// clang.
if (ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer())) {
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
ConstantStruct *CS =
dyn_cast<ConstantStruct>(InitList->getOperand(i));
assert(CS);
assert(CS->getNumOperands() == 2);
// Get the priority.
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
assert(Priority);
// TODO: For now, we assume all priorities must be 65535.
assert(Priority->equalsInt(65535));
// Get the constructor function.
Constant *FP = CS->getOperand(1);
if (FP->isNullValue())
break; // Found a null terminator, exit.
// Explicitly call the constructor at the main entry.
CallInst::Create(FP, "", Main->begin()->getFirstNonPHI());
}
}
// Clear the global_ctors array.
// Use eraseFromParent() instead of removeFromParent().
GV->eraseFromParent();
}