This is the mail archive of the mailing list for the newlib project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFA: Add -ffixed-a2 to gcc command line when building crt1.i for mn10300

Hi Jeff,

  We have recently encountered a problem with C++ constructors for the
  MN10300 target when compiling with -fPIC.  The symptom was that the
  simulated program was running out of memory, but the culprit turned
  out to be the __main() function in libgloss/mn10300/crt1.c which was
  calling the same constructor multiple times.

  The cause of this behavior was that __main() had been compiled as
  an ordinary function (without -fPIC) as part of the libgloss library
  but the constructors had been compiled with -fPIC.  The problem is
  that the a2 register is normally call-saved, so gcc was using it in
  __main() to hold a pointer value across invocations of the
  constructors.  With -fPIC enabled however a2 becomes fixed, and in
  fact it gets initialized and clobbered inside any function which
  uses GOT based addressing.  So __main() calls a constructor thinking
  that a2 will be preserved and the constructor corrupts a2 thinking
  that it is fixed and that any function that wants to use it will
  initialize it first.

  The attached patch solves this problem by making the a2 register
  fixed when compiling crt1.c, even though it is not compiled with
  -fPIC enabled.  This is safe since gcc just uses a3 instead of a2.
  It is a bit of a workaround since -fPIC really ought to enable a
  full set of multilibs, and I am trying to put together a convincing
  test case to present to the mn10300 gcc maintainers, but in the
  meantime this patch works and it is simple and safe.

  So, may I apply this patch please ?


2008-10-09  Nick Clifton  <>

	* mn10300/ Add rule for building crt1.o which adds
	-ffixed-a2 rto the gcc command line.

Index: libgloss/mn10300/
RCS file: /cvs/src/src/libgloss/mn10300/,v
retrieving revision 1.4
diff -c -3 -p -r1.4
*** libgloss/mn10300/	17 Nov 2006 19:18:02 -0000	1.4
--- libgloss/mn10300/	9 Oct 2008 10:00:57 -0000
*************** Makefile: config.status @hos
*** 156,158 ****
--- 156,173 ----
  config.status: configure
  	$(SHELL) config.status --recheck
+ # Use -ffixed-a2 2when building crt1.c.  This is to support constructors that
+ # have been built with -fPIC.  The -fPIC option makes the a2 register fixed,
+ # which is at odds with the default behaviour of treating it as call-saved.
+ # Making a2 fixed even for non-PIC code is safe, since it just means that gcc
+ # will use a3 instead of a2 when it compiles __main() in crt1.c.
+ #
+ # Theoretically -fPIC ought to trigger a different set of multilibs, but this
+ # has not been done.  (Presumably to save space).  This lack would only cause
+ # problems if a non-PIC function calls a PIC function which handles pointers
+ # in some fashion.  Apart from this case of invoking constructors, the only
+ # other time this would happen is if a non-PIC compiled library function (eg:
+ # qsort) calls a PIC compiled function which needs to access global data.
+ crt1.o: crt1.c
+ 	$(CC) $(CFLAGS_FOR_TARGET) -O2 $(INCLUDES) -c $(CFLAGS) -ffixed-a2 $<


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]