How do you use read-only data sections?

Douglas B. Jones douglas at dekalb.UUCP
Sat May 26 01:04:16 AEST 1990


In article <9125 at bunny.GTE.COM> krs0 at bunny.GTE.COM (Rod Stephens) writes:
>I would like to place a string in a read-only data
>section of a program. I want the program to crash
>if it attempts to write into the string.
>Can anyone tell me how to do this? I cannot figure
>out how to get "cc" or "ld" to do it.
>Rod Stephens
>GTE Laboratories, Inc
>(617)466-4182


Here is a small example:
create the following two files:
file "init.c" -- one line
-------
char	*ro = "this is a read only string in init.c";
-------

file "t.c"
-------
extern	char	*ro;
main()
{
printf("before ro = (%s)\n",ro);
printf("Now try to change it to \"this is not a read only string\"\n");
ro = "this is not a read only string";
printf("after  ro = (%s)\n",ro);
exit(0);
}
-------
%cc -R -c init.c		-- look up cc(1)
%cc -c t.c
%cc init.o t.o

You should get a bus error when the assignment in t.c is attempted.
Of course, you can then set your program up to use signals to catch the error.

hope this helps,
Douglas

-- 
Doulas B. Jones					douglas at dekalb
Academic Computer Center		or	gatech!dekalb!douglas
DeKalb College
555 N. Indian Creek Drive/Clarkston, Ga. 30021



More information about the Comp.unix.ultrix mailing list