From: Ted Bullock Subject: Re: const-ify tables To: Christian Weisgerber Cc: gameoftrees@openbsd.org Date: Tue, 15 Feb 2022 14:29:17 -0700 On 2022-02-15 2:12 p.m., Christian Weisgerber wrote: > Ted Bullock: > >> Interesting question to me, if you force writing to one of these const >> variables (ignoring compiler warnings) what happens to the program? Are >> there any security benefits to moving data to supposedly read only memory? > > They are actually placed in a memory section that is mapped read-only. > > $ cat t.c > const int i = 1; > > int > main(void) > { > int *j; > > j = &i; > *j = 2; > > return 0; > } > $ cc -o t t.c > t.c:8:4: warning: assigning to 'int *' from 'const int *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers] > j = &i; > ^ ~~ > 1 warning generated. > $ objdump -t t > ... > 00000000000004c8 g O .rodata 0000000000000004 i > ... > $ ./t > Segmentation fault (core dumped) Definitely strong improvement then, I like that. > > The tables in got.c etc. contain pointers, so they are placed in > the .data.rel.ro section, where the ld.so(1) dynamic linker first > updates the pointers to their final values and then maps the region > read-only before calling the main program. > -- Ted Bullock