[LLVMdev] Re: Hide visible string in variable

Zhang Qiuyu qiuyu at ucla.edu
Sat Oct 9 20:31:36 PDT 2004


Hi,

> On Mon, 27 Sep 2004, Zhang Qiuyu wrote:
> 
> > Is there a way to modify the string such as char a or char b?  Could I
> > use the way like "Replace an instruction with another Value" in Programm
> > Manual? In fact, what I am interested in is string with visible
> > expression, not all string, and I am trying to hide the orignal string
> > by using simple way like XOR..
> 
> Yes, there is.  At the C level, what transformation do you want to do?
> The LLVM code is a pretty straight-forward translation from the C code in
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> this case.

sorry, I still don't understand the way you mentioned. Would you mind giving me a example? 
What I have tried is that  I used the following code

for(Module::ginterator gI=M->gbegin(), gE=M->gbegin; gI!=gE;++gI){
    std::cout<<  *gI<< gI->getName()<<gI->getInitializer();
}

With *gI, I could get all string
With gI->getName, I could get the global variable name.
With gI->getInitializer(), I could get the init value for the GV.

Next, what I want to do is to modify the value of the GV, and I tried to use setInitializer(), but I don't know to do it. setInitializer() need contant parameter. I tried several ways, but I failed.

As you said, I can do it like in C level. For C, if I can get the address of the GV, I can read the value and do XOR and then write it back. But for this case, I don't know how to do that. Maybe I am not familar with STL. I really need your help. Sorry again.

for C level,

char a[]="global string test";
for(i=0;i<strlen(a);i++){
   a[i]=  a[i]^RANDMON;
}

For llvm, 
%a = internal global [20 x sbyte] c"global string test\0A\00"  
Would you mind telling me how to do it.

Qiuyu

> 
> > Is there a way to reorder the basic blocks?
> 
> Yes, for an example pass that does this, take a look at
> lib/Transforms/Scalar/BasicBlockPlacement.cpp
> 
> -Chris
> 
> > C Source code:
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > char a[20]="global string test\n";
> >
> > int main( int argc , char *argv[]){
> >
> >   char b[20]= "Internal string test";
> >
> >   printf("Global %s \n",a);
> >   printf("Internal %s\n",b);
> >
> > }
> >
> >
> > IR code :
> >
> > target endian = little
> > target pointersize = 32
> >  %struct..TorRec = type { int, void ()* }
> >  %struct.TorRec = type { int, void ()* }
> > %a = internal global [20 x sbyte] c"global string test\0A\00"  ; <[20 x sbyte]*> [#uses=1]
> > %.ctor_1 = internal global [20 x sbyte] c"Internal string test"  ; <[20 x sbyte]*> [#uses=1]
> > %.str_1 = internal constant [12 x sbyte] c"Global %s \0A\00"  ; <[12 x sbyte]*> [#uses=1]
> > %.str_2 = internal constant [13 x sbyte] c"Internal %s\0A\00"  ; <[13 x sbyte]*> [#uses=1]
> > %Initialized.0__ = internal global bool false  ; <bool*> [#uses=2]
> >
> > implementation   ; Functions:
> >
> > declare int %printf(sbyte*, ...)
> >
> > int %main(int %argc, sbyte** %argv) {
> > entry:
> >  %b = alloca [20 x sbyte]  ; <[20 x sbyte]*> [#uses=1]
> >  %tmp.1.i = load bool* %Initialized.0__  ; <bool> [#uses=1]
> >  br bool %tmp.1.i, label %__main.entry, label %endif.0.i
> >
> > endif.0.i:  ; preds = %entry
> >  store bool true, bool* %Initialized.0__
> >  br label %__main.entry
> >
> > __main.entry:  ; preds = %entry, %endif.0.i
> >  %tmp.0 = getelementptr [20 x sbyte]* %b, uint 0, uint 0  ; <sbyte*> [#uses=2]
> >  call void %llvm.memcpy( sbyte* %tmp.0, sbyte* getelementptr ([20 x sbyte]*  %.ctor_1, long 0, long 0), uint 20, uint 1 )
> >  %tmp.3 = call int (sbyte*, ...)* %printf( sbyte* getelementptr ([12 x sbyte]*  %.str_1, long 0, long 0), sbyte* getelementptr ([20 x sbyte]*  %a, long 0, long 0) )  ; <int> [#uses=0]
> >  %tmp.6 = call int (sbyte*, ...)* %printf( sbyte* getelementptr ([13 x sbyte]*  %.str_2, long 0, long 0), sbyte* %tmp.0 )  ; <int> [#uses=0]
> >  ret int 0
> > }
> >
> > declare void %llvm.memcpy(sbyte*, sbyte*, uint, uint)
> >
> >
> 
> -Chris
> 






More information about the llvm-dev mailing list