This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Why is a const array pointer with -fPIC mapped in ".data"?
- From: Richard Earnshaw <Richard dot Earnshaw at arm dot com>
- To: Kazuomi Kato <kato dot kazuomi at jp dot panasonic dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 17 May 2004 12:02:47 +0100
- Subject: Re: Why is a const array pointer with -fPIC mapped in ".data"?
- References: <039c01c43bfb$82d7fe40$2eaab684@KAZUOMI2>
On Mon, 2004-05-17 at 11:39, Kazuomi Kato wrote:
> I'd like to put an array data in the section ".rodata".
> I wish the array data to be unmodified so I used const type for it.
> However, inspite of the data specified as const type, I found the case
> that the data specified was mapped in the section ".data".
> If a const pointer array is compiled with -fPIC option, const type
> to the pointer is ignored and the pointer exists in the ".data".
>
> Do you know why this is happening?
Yes.
> Does the -fPIC option have to do with above phenomenon?
Yes.
The problem is that when the code is loaded it has to be dynamically
relocated (the pointers in the data have to be adjusted based on the
load address). Because of this, it can't be made read-only (at least,
not without having the dynamic loader playing page protection dances in
the MMU).
So although your code is not logically modifiable (from the strict
source-code perspective), it does have to be physically modifiable from
the execution perspective.
If you really want your PIC data tables to be read-only, don't put
pointers in them.
R.
>
> I used the gcc v3.2.1 for ARM.
>
> I'd like to show an example bellow.
> ===
> "test.c"
>
> const char const_rodata0[] = "test0";
> const char const_rodata1[] = "test1";
> const char const_rodata2[] = "test2";
> const char const_rodata3[] = "test3";
> const char const_rodata4[] = "test4";
> const char const_rodata5[] = "test5";
>
> const char * const array_const[BUF][2] = {ã/* specified const to array_const */
> {const_rodata0, const_rodata1},
> {const_rodata2, const_rodata3},
> {const_rodata4, const_rodata5}
> };