[comp.unix.ultrix] How do you use read-only data sections?

douglas@dekalb.UUCP (Douglas B. Jones) (05/25/90)

In article <9125@bunny.GTE.COM> krs0@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@dekalb
Academic Computer Center		or	gatech!dekalb!douglas
DeKalb College
555 N. Indian Creek Drive/Clarkston, Ga. 30021