Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

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

$
0
0

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 .


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>