mutable in static const object -> segfault

Nathan Sidwell nathan@cs.bris.ac.uk
Mon Jul 13 09:02:00 GMT 1998


Hi,
egcs places static const objects in the read only section. Trouble is,
if that object contains a mutable member, you can't fiddle with it.

Here's an example.
--begin foo.ii
struct A 
{
  mutable int i;
};
  
static const A a = {0};

int main()
{
  a.i = 5;
  return 0;
}
--end foo.ii

Here's the session log for the 07/07 snapshot under solaris 2.5.1
--begin log
nathan@laie:10729>egcs-0707-g++ -o foo foo.ii -g -v
Reading specs from
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/specs
gcc version egcs-2.91.47 19980707 (gcc2 ss-980609 experimental)

/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/cc1plus
foo.ii -quiet -g -version -o /var/tmp/ccqtPHk2.s
GNU C++ version egcs-2.91.47 19980707 (gcc2 ss-980609 experimental)
(sparc-sun-solaris2.5.1) compiled by GNU C version 2.8.1.
 /usr/ccs/bin/as -V -Qy -s -o /var/tmp/ccEx7gw2%O /var/tmp/ccqtPHk2.s
/usr/ccs/bin/as: SC4.2 dev 30 Nov 1995

/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/collect2
-V -Y P,/usr/ccs/lib:/usr/lib -Qy -o foo
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/crt1.o
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/crti.o
/usr/ccs/lib/values-Xa.o
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/crtbegin.o
-L/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47
-L/home/staff/nathan/solaris/local/SunOS_5/sparc-sun-solaris2.5.1/lib
-L/usr/ccs/bin -L/usr/ccs/lib
-L/home/staff/nathan/solaris/local/SunOS_5/lib /var/tmp/ccEx7gw2%O
-lstdc++ -lm -lgcc -lc -lgcc
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/crtend.o
/home/staff/nathan/solaris/local/SunOS_5/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.47/crtn.o
ld: Software Generation Utilities (SGU) SunOS/ELF (LK-2.0 (S/I) -
versioning)

nathan@laie:10730>./foo
zsh: 18539 segmentation fault (core dumped)  ./foo

--end log
Under the debugger, foo has indeed blown up at the a.i = 5 line. and a
has been assigned to the read only section. Here's the relevant bit of
the assembly file,
.section        ".rodata"
.stabs "a:S(0,24)",38,0,6,a
        .align 4
        .type    a,#object
        .size    a,4
a:
        .uaword 0

I believe the above code is ok. egcs must check const static objects to
see if they have mutable members before committing them to rodata.

nathan 
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk



More information about the Gcc-bugs mailing list