Michael.Kang
2011-Mar-26 03:53 UTC
[LLVMdev] How to read memory data througn adress of unsigned long
I have a value of long type and like to get the data at the adress in the value: ################# unsigned long v = 0xc0008000 Value* addr = ConstantInt::get(getIntegerType(64), v) #################### Then I try to use LoadInstr to get a load instruction of ir as the following: ############################ Value* data = new LoadInst(addr, "", false, bb); encounter segmentation fault. ######################## I also try to use IntToPtrInstr to do some transform as the following: ################################ Type const *intptr_type cpu->dyncom_engine->exec_engine->getTargetData()->getIntPtrType(_CTX()); Value* ptr = new IntToPtrInst(v, intptr_type, "", bb); Value* data = new LoadInst(ptr, "", false, bb); ######################################## still encounter segmentation fault Any person can give me some hints for my case? Thanks in advance. I have tried a long time with different ways. Thanks MK -- www.skyeye.org
Reid Kleckner
2011-Mar-26 05:13 UTC
[LLVMdev] How to read memory data througn adress of unsigned long
On Fri, Mar 25, 2011 at 8:53 PM, Michael.Kang <blackfin.kang at gmail.com> wrote:> I have a value of long type and like to get the data at the adress in the value: > > ################# > unsigned long v = 0xc0008000 > Value* addr = ConstantInt::get(getIntegerType(64), v) > #################### > > Then I try to use LoadInstr to get a load instruction of ir as the following: > ############################ > Value* data = new LoadInst(addr, "", false, bb); > encounter segmentation fault. > ######################## >Perhaps you should use a debug build of LLVM so you get an assert message instead of a segfault. Here you're trying to load from an integer without first casting it to a pointer, which you do next.> I also try to use IntToPtrInstr to do some transform as the following: > ################################ > Type const *intptr_type > cpu->dyncom_engine->exec_engine->getTargetData()->getIntPtrType(_CTX()); > Value* ptr = new IntToPtrInst(v, intptr_type, "", bb); > Value* data = new LoadInst(ptr, "", false, bb); > ######################################## > still encounter segmentation fault > > Any person can give me some hints for my case? Thanks in advance. I > have tried a long time with different ways.Not sure why, but using a debug build would be helpful and using IRBuilder would help make your code less error-prone. Reid
Frits van Bommel
2011-Mar-26 07:52 UTC
[LLVMdev] How to read memory data througn adress of unsigned long
On Sat, Mar 26, 2011 at 6:13 AM, Reid Kleckner <reid.kleckner at gmail.com> wrote:> On Fri, Mar 25, 2011 at 8:53 PM, Michael.Kang <blackfin.kang at gmail.com> wrote: >> I also try to use IntToPtrInstr to do some transform as the following: >> ################################ >> Type const *intptr_type >> cpu->dyncom_engine->exec_engine->getTargetData()->getIntPtrType(_CTX()); >> Value* ptr = new IntToPtrInst(v, intptr_type, "", bb); >> Value* data = new LoadInst(ptr, "", false, bb); >> ######################################## >> still encounter segmentation fault >> >> Any person can give me some hints for my case? Thanks in advance. I >> have tried a long time with different ways. > > Not sure why, but using a debug build would be helpful and using > IRBuilder would help make your code less error-prone.getIntPtrType() doesn't return a pointer type, it returns an integer type that's at least as big as a pointer on the target in question. In other words: it returns a type that can be used as C's intptr_t. Micheal, you need to create an actual pointer type based on the type of value you want to load. For example, to load a 32-bit integer: Type* T = Type::getInt32Ty(_CTX())->getPointerTo(); Value* ptr = new IntToPtrInst(v, T, "", bb); Value* data = new LoadInst(ptr, "", false, bb); I agree that it's better to use IRBuilder though: it makes things easier (no need to keep passing 'bb', for example) and can do some automatic early constant folding for you (if you want). And it definitely helps to compile with assertions enabled so you get more meaningful errors when you make mistakes.
Apparently Analagous Threads
- [LLVMdev] How to read memory data througn adress of unsigned long
- [LLVMdev] How to load a data from the address of unsiged long type
- [LLVMdev] How to load a data from the address of unsiged long type
- runStaticConstructorsDestructors() causes crash on exit
- creating a callinst to an external function