Glenn Fowler wrote:
> I've been at this so long you think I should know better
>
> I've been under the apparently mistaken assumption that
> if I dilligently pepper struct definitions and subsequent
> declarations and initializations with "const" that the compiler
> will attempt to place that data in readonly text
>
> I've done this with the libast tables in conftab.c lc.c
> but nm and size show the data going to the .data section
>   

Use elfdump, as nm doesn't tell the whole story, what with its a.out 
history.
In addition to .text and .data, there is .rodata. There are others, as well.

Using the following snippet, and fiddling with the const keyword:
#include <stdio.h>

char *d = "diggy";
const int c = 5;
static int e();
int main()
{
printf("%d\n", c);
e(d);
// d[0] = 'b';
}

static int e(char *p) {

p[0] = 'a';
}

And compiling alternately with Sun Studio 11 cc and /usr/sfw/bin/gcc, viz:
cc -o main.cc main.c; gcc -o main.gcc main.c; elfdump main*cc | egrep 
'\|[cde] '

You can see just where the variables go.

Interestingly, with the code as above, gcc produces code that segfaults, 
and cc produces code that runs. So you may have trouble finding an 
elegant, portable makefile without resorting to linker edit scripts. And 
I believe that creates a fundamental contradiction.

Elegant, portable, linker edit script.

Just keep repeating that to yourself.

Cheers!
-sam

> am I expecting too much from cc to do this without prodding?
> is there a set of options that at least works for gcc on
> multiple architectures? or at least for solaris and the ksh93
> integration?
>
> thanks
>
> -- Glenn Fowler -- AT&T Research, Florham Park NJ --
>
> _______________________________________________
> ast-users mailing list
> ast-users at research.att.com
> https://mailman.research.att.com/mailman/listinfo/ast-users
>
>   


Reply via email to