Twinkle HanA
Twinkle HanA

Reputation: 93

How to convert a Constant* to Value* in LLVM?

I just want to obfuscate selector name, but I don't know How to convert a Constant* to Value* :

void symObfs(Module &M) {
            errs()<<"Start Obfuscating...\n";
            srand (time(NULL));
            //Objective-C Method ClassName Stuff
            for(auto G=M.getGlobalList().begin();G!=M.getGlobalList().end();G++){
                GlobalVariable &GL=*G;
                if (GL.getName().str().find("OBJC_METH_VAR_NAME_")==0||GL.getName().str().find("OBJC_CLASS_NAME_")==0){
                    ConstantDataArray* Initializer=dyn_cast<ConstantDataArray>(GL.getInitializer ());
                    // string newName = fakeNameGenerator();
                    string newName = randomString(16);
                    Constant *Value =ConstantDataArray::getString(Initializer->getContext(),newName,true);
                    //GL.setInitializer(Value);
                    errs()<<"Selector Name: "<<Initializer->getAsCString ()<<" Obfuscated to: "<<newName<<"\n";
                    Initializer->Value::replaceAllUsesWith(Value);
                }
            }
}

Thanks!

Upvotes: 2

Views: 2081

Answers (1)

Ajay Brahmakshatriya
Ajay Brahmakshatriya

Reputation: 9203

The Constant Class inherits from the Value Class.

So you do not need to do anything to convert from Constant* to Value*. You can directly assign a Constant* to a Value* since in C++ pointers to children class objects can be assigned to parent class pointers variables.

On the other hand if you want to assign from Value* to Constant* you will have to use dyn_cast as

Constant * C = dyn_cast<Constant*>(V);

This will set C to NULL if the object in V is not indeed a Constant.

Upvotes: 2

Related Questions