[comp.lang.c] putting stuff in the text segment

m5@lynx.UUCP (Mike McNally) (05/26/88)

Does a widely-accepted syntax extension to put data structures in the
text segment exist?  We are thinking about adding something like this
to our compiler in order to reduce memory use by oft-forked large
applications.  I had in mind a couple of new storage classes, maybe 
"rom" and "staticrom".

If you want to complain about non-standard extensions to the language,
please change the subject line so that I can more easily kill the
articles.

-- 
Mike McNally of Lynx Real-Time Systems

uucp: lynx!m5 (maybe pyramid!voder!lynx!m5 if lynx is unknown)

guy@gorodish.Sun.COM (Guy Harris) (05/26/88)

> Does a widely-accepted syntax extension to put data structures in the
> text segment exist?  We are thinking about adding something like this
> to our compiler in order to reduce memory use by oft-forked large
> applications.  I had in mind a couple of new storage classes, maybe 
> "rom" and "staticrom".

Try "const"; it's in the current ANSI C draft (and will probably be in future
ones), and "stick this in ROM" is one valid interpretation of it, as is "stick
this into the text segment".  You needn't feel obliged to implement all the
ANSI C semantics (such as "assignments to 'const' objects are disallowed, as
are sneaky back-door ways of doing those assignments") unless and until you
have an ANSI C compiler (but be prepared for them).

Grab the ANSI C spec for a detailed description; it's not a simple storage
class.

jeff@unh.UUCP (Jeffrey E. F. Friedl) (05/26/88)

In article <3813@lynx.UUCP>, m5@lynx.UUCP (Mike McNally) writes:
> Does a widely-accepted syntax extension to put data structures in the
> text segment exist?  We are thinking about adding something like this
> to our compiler in order to reduce memory use by oft-forked large
> applications.  I had in mind a couple of new storage classes, maybe 
> "rom" and "staticrom".

Certainly a nice thing to be able to do. However, not all systems have
ideas about "segments" (under UNIX they usually do, of course, but C has
made its way to a lot of pretty strange places).

Also, using the word "rom" implies that you can't modify the data.
What should happen then if there is an attempt to do this?  Should the
language ensure that any attempt to modify the data will {succeed, crash}?
When you have text segments, are you allowed (by the operating system) to
modify its contents (a different issue from what the *language* specifies)?
And in what cases... All the time (BSD magic number 0407)? None of the time
(BSD magic number 0410,0413)? Some of the time (perhaps on some system where
it depends on the sticky bit or something like that), etc...

These aren't flames, just being a devil's advocate....

However, I get from your posting that you're just doing something that
you need at your site and just wanting to do it the way it's been done
before if it has been done before.

If you're using BSD at least, you can just compile the code where the
initialized data is with the -R flag, which is passed to the assembler
and indicates that all ".data" directives should be ".text".
This would *really* screw things up when done with normal code, but with
static data... just what you want.

If not using a system with a -R type flag and if you must hack the compiler,
perhaps at least ensure (mostly) that the programs can still work with a
"standard" compiler with something like

	#ifndef MY_HACKED_COMPILER
	#  define shared
	#endif

	shared struct foo [BIG] {this, that, the other};
	
Then, your comipler would understand "shared" (or whatever you decide
upon) and you would compile with -DMY_HACKED_COMPILER (or have a hacked
cpp do it for you).

	*jeff*

-------------------------------------------------------------------------------
Jeffrey Eric Francis Friedl, Box 2146 Babcock House, Durham New Hampshire 03824
..!{uunet,decvax}!unh!jeff   BITNET%"j_friedl@unhh"  ..!ucbvax!kentvax!jfriedl

I hope I'm not around Jan 18, 2038 at 10:14:08PM
(yes, friedl@vsi is my brother, and he's proud of it).

daveb@laidbak.UUCP (Dave Burton) (05/26/88)

In article <3813@lynx.UUCP> m5@lynx.UUCP (Mike McNally) writes:
|Does a widely-accepted syntax extension to put data structures in the
|text segment exist?  We are thinking about adding something like this
|to our compiler in order to reduce memory use by oft-forked large
|applications.  I had in mind a couple of new storage classes, maybe 
|"rom" and "staticrom".

I dunno of any language extensions, per se, to accomplish this.
The most widely used method to do this is to cc -S, sed 's/data/text/', as.
Sun (OS3.2) has embellished cc with a -R flag that accomplishes the same
thing with a whole lot less trouble.

I believe that using the -R approach is superior to changing the language.
(It is certainly more portable.)

m5@lynx.UUCP (Mike McNally) (05/26/88)

OK, OK.  I got a couple of letters stating that "const" can be used 
as a sort-of storage class modifier or whatever in ANSI C.  Great.
I'll go buy K&R2.

m5

-- 
Mike McNally of Lynx Real-Time Systems

uucp: lynx!m5 (maybe pyramid!voder!lynx!m5 if lynx is unknown)

friedl@vsi.UUCP (Stephen J. Friedl) (05/27/88)

In article <523@unh.UUCP>, jeff@unh.UUCP (Jeefrey E. F. Friedl) writes:
> In article <3813@lynx.UUCP>, m5@lynx.UUCP (Mike McNally) writes:
> > [How to put data in the text segment?]
> 
> [...]

> If you're using BSD at least, you can just compile the code where the
> initialized data is with the -R flag, which is passed to the assembler
> and indicates that all ".data" directives should be ".text".

Be careful with this: as I recall, *all* initialized statics go
into the .text and are readonly.  This means that:

        foo()
        {
        static int been_here = 0;

                if (! been_here)
                {
                        /* whatever */
                        been_here++;    /* <------ CORE DUMP HERE */
                }
        }

core dumps at the "been_here++"; a program can't modify its own
.text.  My solution was to declare:

        static int been_here;

and rely on the initialization to zero; an uninitialized static
always goes into the .bss.  This may have changed since 4.2.

-- 
Steve Friedl    V-Systems, Inc. (714) 545-6442    3B2-kind-of-guy
friedl@vsi.com    {backbones}!vsi.com!friedl   attmail!vsi!friedl

I'm jeff@unh's brother but I'm not proud of it.

jeff@unh.UUCP (Jeffrey E. F. Friedl) (05/28/88)

In article <688@vsi.UUCP>, friedl@vsi.UUCP (Stevie J. Friedl) writes:
> In article <523@unh.UUCP>, jeff@unh.UUCP (Jeffrey E. F. Friedl) writes:
> > In article <3813@lynx.UUCP>, m5@lynx.UUCP (Mike McNally) writes:
> > > [How to put data in the text segment?]
> > 
> > [...]
> 
> > If you're using BSD at least, you can just compile the code where the
> > initialized data is with the -R flag, which is passed to the assembler
> > and indicates that all ".data" directives should be ".text".
> 
> Be careful with this: as I recall, *all* initialized statics go
> into the .text and are readonly.  This means that:
> 
> [valid example deleted]

Exactly. That's why (if you continue reading my previous posting), I say
that when compiling normal code (as opposed to static initialized data)
you get lots of grief.  If those large tables are put into a separate
file (I usually have large static tables in separate files anyway), then
that file only could be compiled with the -R flag.  Other files, of course,
won't be affected.

-------------------------------------------------------------------------------
Jeffrey Eric Francis Friedl, Box 2146 Babcock House, Durham New Hampshire 03824
..!{uunet,decvax}!unh!jeff   BITNET%"j_friedl@unhh"  ..!ucbvax!kentvax!jfriedl

I hope I'm not around Jan 18, 2038 at 10:14:08PM

(friedl@vsi is my brother, and he's proud of it).

guy@gorodish.Sun.COM (Guy Harris) (05/29/88)

> I dunno of any language extensions, per se, to accomplish this.

I think Apollo has the keyword "readonly" to do this.

I suspect other vendors (DEC, in VMS C?) have added similar extensions.

I know ANSI C has "const" which can be used to do this (and was, in fact,
intended for this purpose, among others).

> The most widely used method to do this is to cc -S, sed 's/data/text/', as.
> Sun (OS3.2) has embellished cc with a -R flag that accomplishes the same
> thing with a whole lot less trouble.

1) Credit where credit is due: Sun didn't add "-R", Berkeley did.

2) It may be less trouble than running the "sed" script.  It is certainly *not*
   less trouble than having an explicit method in the language for indicating
   that *particular* objects should be stuck in read-only memory (or
   shared text, or whatever); "-R" is a very crude tool, and sticks *all*
   initialized data in the text segment.  Sometimes this can be a *real* pain.

mouse@mcgill-vision.UUCP (der Mouse) (06/12/88)

In article <1457@laidbak.UUCP>, daveb@laidbak.UUCP (Dave Burton) writes:
> In article <3813@lynx.UUCP> m5@lynx.UUCP (Mike McNally) writes:
>> Does a widely-accepted syntax extension to put data structures in
>> the text segment exist?
> I dunno of any language extensions, per se, to accomplish this.  [...]
> Sun (OS3.2) has embellished cc with a -R flag that accomplishes the
> same thing with a whole lot less trouble.

This is not Sun's innovation.  The -R flag exists in 4.3 and I believe
it existed in 4.2 as well.

> I believe that using the -R approach is superior to changing the
> language.  (It is certainly more portable.)

It is definitely more portable.  However, it has one disadvantage: it
applies to the entire file.  You can't have a mostly-normal file with
three strings going in the text segment, for example.

On the whole, I think changing the language is better *if* everyone
adopts the same way of doing so.  (This appears to be the intent of the
ANSI "const" keyword.)

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu

bill@proxftl.UUCP (T. William Wells) (06/13/88)

In article <1457@laidbak.UUCP>, daveb@laidbak.UUCP (Dave Burton) writes:
) In article <3813@lynx.UUCP> m5@lynx.UUCP (Mike McNally) writes:
) |Does a widely-accepted syntax extension to put data structures in the
) |text segment exist?  We are thinking about adding something like this
) |to our compiler in order to reduce memory use by oft-forked large
) |applications.  I had in mind a couple of new storage classes, maybe
) |"rom" and "staticrom".

The const keyword of Standard C, when applied to a statically
allocated variable, implies that one can place the variable in a
read-only segment; even if your text segments are read-only, any
variable marked const could be placed there.

Otherwise, you could define various qualifiers which can be used
like const but which specify a segment instead of an attribute.
Like defining storage new storage classes (actually new storage
class modifiers), this is relatively innocuous; porting to
another system just requires adding some #defines to get rid of
the new keywords.