Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Neeharica_M_
Beginner
41 Views

Const data Globally Declared in 'c' is getting allocated in DATA REGION but not in the RODATA region. Any Solutions?

Hi Team,

I have written two test cases, example1.c and example2.c

*****************************************************************************************

example1.c::::

#include<stdio.h>

const char buf[11]="HelloWorld";

int foo(int value){
     return (value + 1);
}

int main(void){
        int val = 8;
        int (*funct_buf)(int) = (int (*)(int)) buf;
        memcpy(funct_buf,foo,sizeof(buf));
        printf("\n Rodata is writable... test failed\n");
        val = (*funct_buf)(val);
        printf("\n Rodata is executable..... test failed\n");

        return 0;
}

Compilation:: icc.12.0.022b.i686-linux example1.c

output:: ./a.out

Segmentation fault (core dumped)

*******************************************************************************************

example2.c:::::

#include<stdio.h>

int foo(int value){
     return (value +1);
}

int main(void){
        int val = 8;
        const char buf[11]="HelloWorld";

        int (*funct_buf)(int) = (int (*)(int)) buf;

        memcpy(funct_buf,foo,sizeof(buf));
        printf("\n Rodata is writable... test failed\n");
        val = (*funct_buf)(val);
        printf("\n Rodata is executable..... test failed\n");

        return 0;
}

Compilation:: icc.12.0.022b.i686-linux example1.c

output:: ./a.out
 Rodata is writable... test failed
Segmentation fault (core dumped)

********************************************************************************************************

From above test cases we can observe that when "const char buf[]" written globally then rodata section writable is not happening ,
 But when "const char buf[]" written inside of main function then rodata section writable is happening when I do memcpy.
 

But in the real scenario where I am using the "const char buf[]" which is written globally , the buf is getting allocated in the data region but not in the rodata region. And the the buf is being writable . Could you suggest any compiler options for making the buf to get into rodata region and make it non-writable . And I am using icc.12.0.022b.i686-linux .

0 Kudos
2 Replies
Kittur_G_Intel
Employee
41 Views

Hi, this looks like an issue with that very old version which was fixed in later versions of the compiler (which I verified). Either you can upgrade to the current version (which is 15.X) and 16.0 will be released soon as well.  Or, you can go to login to the Intel registration center and on the right side of the page you'll find a button (hover on it will show "click for older versions") and you can download older version of the subscribed product and check it out as well.  

_Kittur 

Kittur_G_Intel
Employee
41 Views

@Neeharica:  I assume that the issue is resolved per my previous communication else let me know if there are any further issues, thx.