This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]

Re: HP-UX 11.x patch to call C++ static constructors at dlopen()


> This patch fixes PR 8360
> 
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&p
> r=8360

I have installed the enclosed patch on the main and 3.3 branch.  It
incorporates your patch to use LD_INIT_SWITCH and LD_FINI_SWITCH 
for running initializers and finalizers under 32-bit hpux11 with a
much more extensive patch for 64-bit hpux11.  The 64-bit patch doesn't
use LD_INIT_SWITCH and LD_FINI_SWITCH as there are no switches which
work with GNU ld.  Instead, we use array init and fini sections.

Testing exposed a lot of non conformat behavior.  On the 32-bit port,
the LD_INIT_SWITCH and LD_FINI_SWITCH switches are added by collect2
to the end of the link command.  This results in the initializers
running after any user specified initializers which is the desired
behavior.  However, the order of execution is opposite to that indicated
in the man page for ld.

Similar non conformant behavior was observed for the 64-bit port.
Command line initializers with HP ld run in the opposite order to that
of the 32-bit port (ie., as specified in the man page), however, the
order of execution for the addresses listed in the init array section
is opposite to that specified in the System V ABI.

Further, HP sticks the init and fini arrays in .init and .fini, respectively.
As a result, they have to use non standard attributes for these sections.
This required an assembler hack and hppa64 gas users will need to update gas
to the cvs head as 01/01/2002 or later.  

We also had to add hackery to accomodate HP's non conformant implementation
of weak symbols.

The modifications needed to use array initializers and finalizers with
crtstuff.c are definitely a hack and the integration could be improved.
However, I think that should wait until we see if HP changes their ELF
implementation.  The current implementation hides everthing in the
backend.

The patch has been tested on hppa64-hp-hpux11.X, hppa2.0w-hp-hpux11.X
and hppa-unknown-linux-gnu, 3.4 and 3.3, GNU and HP as.

Having working initializers on hppa64-hpux11, fixes a number of g++,
v3 and objc testsuite failures.  It should now be possible to use a
shared libgcc on hppa64-hpux11.

Dave
-- 
J. David Anglin                                  dave.anglin@nrc.ca
National Research Council of Canada              (613) 990-0752 (FAX: 952-6605)

2002-01-04  John David Anglin  <dave.anglin@nrc.ca>

	* config.gcc (hppa*64*-*-hpux11*): Define extra_parts.  Don't use
	collect2.
	* pa-hpux11.h (LDD_SUFFIX, PARSE_LDD_OUTPUT): Undefine.
	(HAS_INIT_SECTION, LD_INIT_SWITCH, LD_FINI_SWITCH): Define.
	* pa64-hpux.h (HP_INIT_ARRAY_SECTION_ASM_OP,
	GNU_INIT_ARRAY_SECTION_ASM_OP, HP_FINI_ARRAY_SECTION_ASM_OP,
	GNU_FINI_ARRAY_SECTION_ASM_OP): Define.
	(CTORS_SECTION_ASM_OP, DTORS_SECTION_ASM_OP): Define when not using
	elfos.h.
	(EH_FRAME_IN_DATA_SECTION): Delete define.
	(HAS_INIT_SECTION, LD_INIT_SWITCH, LD_FINI_SWITCH): Undefine.
	(STARTFILE_SPEC): Use crtbegin.o.
	(ENDFILE_SPEC): Use crtend.o.
	(INIT_SECTION_ASM_OP, FINI_SECTION_ASM_OP, CRT_CALL_STATIC_FUNCTION,
	SUPPORTS_INIT_PRIORITY, PA_CXA_FINALIZE_STUB, PA_INIT_FINI_HACK,
	PA_INIT_FRAME_DUMMY_ASM_OP, PA_JV_REGISTERCLASSES_STUB,
	DTOR_LIST_BEGIN): Define.
	* pa.c (TARGET_ASM_CONSTRUCTOR): Define.
	(pa_asm_out_constructor, pa_asm_out_destructor): New functions.
	* som.h (SUPPORTS_INIT_PRIORITY): Delete define.

Index: config.gcc
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/config.gcc,v
retrieving revision 1.270
diff -u -3 -p -r1.270 config.gcc
--- config.gcc	26 Dec 2002 18:01:22 -0000	1.270
+++ config.gcc	3 Jan 2003 19:57:04 -0000
@@ -921,7 +921,7 @@ hppa*64*-*-hpux11*)
 	tmake_file="pa/t-pa64 pa/t-pa-hpux"
 	xmake_file="pa/x-ada"
 	target_cpu_default="(MASK_PA_11|MASK_PA_20|MASK_GAS)"
-
+	extra_parts="crtbegin.o crtend.o crtbeginS.o crtendS.o crtbeginT.o"
 	if test x$gnu_ld = xyes
 	then
 		target_cpu_default="${target_cpu_default}|MASK_GNU_LD"
@@ -934,7 +934,6 @@ hppa*64*-*-hpux11*)
 #		tmake_file="${tmake_file} pa/t-dce-thr"
 #	fi
 	install_headers_dir=install-headers-cpio
-	use_collect2=yes
 	;;
 hppa1.1-*-hpux11* | hppa2*-*-hpux11*)
 	target_cpu_default="MASK_PA_11"
Index: config/pa/pa-hpux11.h
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/config/pa/pa-hpux11.h,v
retrieving revision 1.5
diff -u -3 -p -r1.5 pa-hpux11.h
--- config/pa/pa-hpux11.h	22 Sep 2002 19:23:19 -0000	1.5
+++ config/pa/pa-hpux11.h	3 Jan 2003 19:57:04 -0000
@@ -117,3 +117,16 @@ Boston, MA 02111-1307, USA.  */
 
 #define SIZE_TYPE "long unsigned int"
 #define PTRDIFF_TYPE "long int"
+
+/* HP-UX 11.0 and above provides initialization and finalization function
+   support from linker command line.  We don't need to invoke __main to run
+   constructors.  We also don't need chatr to determine the dependencies of
+   dynamically linked executables and shared libraries.  */
+#undef LDD_SUFFIX
+#undef PARSE_LDD_OUTPUT
+#undef HAS_INIT_SECTION
+#define HAS_INIT_SECTION 1
+#undef LD_INIT_SWITCH
+#define LD_INIT_SWITCH "+init"
+#undef LD_FINI_SWITCH
+#define LD_FINI_SWITCH "+fini"
Index: config/pa/pa64-hpux.h
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/config/pa/pa64-hpux.h,v
retrieving revision 1.20
diff -u -3 -p -r1.20 pa64-hpux.h
--- config/pa/pa64-hpux.h	16 Dec 2002 18:21:43 -0000	1.20
+++ config/pa/pa64-hpux.h	3 Jan 2003 19:57:04 -0000
@@ -112,6 +112,11 @@ do {								\
 #define DATA_SECTION_ASM_OP	"\t.data"
 #define BSS_SECTION_ASM_OP	"\t.section\t.bss"
 
+#define HP_INIT_ARRAY_SECTION_ASM_OP	"\t.section\t.init"
+#define GNU_INIT_ARRAY_SECTION_ASM_OP	"\t.section\t.init_array"
+#define HP_FINI_ARRAY_SECTION_ASM_OP	"\t.section\t.fini"
+#define GNU_FINI_ARRAY_SECTION_ASM_OP	"\t.section\t.fini_array"
+
 #undef ASM_OUTPUT_ALIGNED_COMMON
 #define ASM_OUTPUT_ALIGNED_COMMON(FILE, NAME, SIZE, ALIGN)		\
 do {									\
@@ -220,55 +225,175 @@ do {								\
 } while (0)
 
 #undef TEXT_SECTION_ASM_OP
-#define TEXT_SECTION_ASM_OP "\t.SUBSPA $CODE$\n"
+#define TEXT_SECTION_ASM_OP		"\t.SUBSPA $CODE$\n"
 #undef READONLY_DATA_SECTION_ASM_OP
-#define READONLY_DATA_SECTION_ASM_OP "\t.SUBSPA $LIT$\n"
+#define READONLY_DATA_SECTION_ASM_OP	"\t.SUBSPA $LIT$\n"
 #undef DATA_SECTION_ASM_OP
-#define DATA_SECTION_ASM_OP "\t.SUBSPA $DATA$\n"
+#define DATA_SECTION_ASM_OP		"\t.SUBSPA $DATA$\n"
 #undef BSS_SECTION_ASM_OP
-#define BSS_SECTION_ASM_OP "\t.SUBSPA $BSS$\n"
+#define BSS_SECTION_ASM_OP		"\t.SUBSPA $BSS$\n"
 
-#endif /* USING_ELFOS_H */
+/* We provide explicit defines for CTORS_SECTION_ASM_OP and
+   DTORS_SECTION_ASM_OP since we don't yet have support for
+   named sections with the HP assembler.  */
+#undef CTORS_SECTION_ASM_OP
+#define CTORS_SECTION_ASM_OP "\t.SUBSPA \\.ctors,QUAD=1,ALIGN=8,ACCESS=31"
+#undef DTORS_SECTION_ASM_OP
+#define DTORS_SECTION_ASM_OP "\t.SUBSPA \\.dtors,QUAD=1,ALIGN=8,ACCESS=31"
+
+#define HP_INIT_ARRAY_SECTION_ASM_OP \
+  "\t.SUBSPA \\.init,QUAD=1,ALIGN=8,ACCESS=31"
+#define GNU_INIT_ARRAY_SECTION_ASM_OP \
+  "\t.SUBSPA \\.init_array,QUAD=1,ALIGN=8,ACCESS=31"
+#define HP_FINI_ARRAY_SECTION_ASM_OP \
+  "\t.SUBSPA \\.fini,QUAD=1,ALIGN=8,ACCESS=31"
+#define GNU_FINI_ARRAY_SECTION_ASM_OP \
+  "\t.SUBSPA \\.fini_array,QUAD=1,ALIGN=8,ACCESS=31"
 
-/* For the time being, we aren't using init sections.  `P' relocations
-   are currently used for function references.  However, P relocations are
-   treated as data references and data references are bound by dld.sl
-   immediately at program startup.  This causes an abort due to undefined
-   weak symbols in crtbegin.o (e.g., __register_frame_info).  Possibly
-   Q relocations might avoid this problem but the GNU assembler doesn't
-   support them.  */
-#undef INIT_SECTION_ASM_OP
-#undef FINI_SECTION_ASM_OP
+#endif /* USING_ELFOS_H */
 
-#define EH_FRAME_IN_DATA_SECTION 1
+/* The following defines, used to run constructors and destructors with
+   the SOM linker under HP-UX 11, are not needed.  */
+#undef HAS_INIT_SECTION
+#undef LD_INIT_SWITCH
+#undef LD_FINI_SWITCH
 
+/* The following STARTFILE_SPEC and ENDFILE_SPEC defines provide the
+   magic needed to run initializers and finalizers.  */
+#undef STARTFILE_SPEC
+#define STARTFILE_SPEC \
+  "%{!shared: %{!symbolic: crt0.o%s}} %{static:crtbeginT.o%s} \
+   %{!static:%{!shared:crtbegin.o%s} %{shared:crtbeginS.o%s}}"
 #undef ENDFILE_SPEC
-#define ENDFILE_SPEC ""
+#define ENDFILE_SPEC "%{!shared:crtend.o%s} %{shared:crtendS.o%s}"
 
-#undef STARTFILE_SPEC
-#define STARTFILE_SPEC "%{!shared: %{!symbolic: crt0.o%s}}"
+/* Since HP uses the .init and .fini sections for array initializers
+   and finalizers, we need different defines for INIT_SECTION_ASM_OP
+   and FINI_SECTION_ASM_OP.  With the implementation adopted below,
+   the sections are not actually used.  However, we still must provide
+   defines to select the proper code path.  */
+#undef INIT_SECTION_ASM_OP
+#define INIT_SECTION_ASM_OP
+#undef FINI_SECTION_ASM_OP
+#define FINI_SECTION_ASM_OP
 
-/* Since we are not yet using .init and .fini sections, we need to
-   explicitly arrange to run the global constructors and destructors.
-   We could use ldd for this but it depends on LD_LIBRARY_PATH being
-   correctly set.  So, we use the ld init and fini switches. However,
-   we need to support different switches for the GNU and HP linkers.
-   We can't check TARGET_GNU_LD in collect2, so we need a different
-   test.  The +Accept switch is always the first switch when we are
-   using the HP linker (see define for LINK_SPEC).  Checking for it
-   is a somewhat fragile as it depends on internal details of the
-   collect2 program but it is better than testing ld_file_name.
-
-   FIXME: The GNU linker is broken.  The -init/-fini switches don't
-   work and ldd can't determine the dynamic dependences of executables
-   linked with GNU ld.  The init and fini routines are not executed
-   although DT_INIT and DT_FINI appear ok.  As a result, defining
-   LD_INIT_SWITCH and LD_FINI_SWITCH causes more harm than good when
-   using GNU ld.  However, the definitions appear to work fine with
-   the HP linker.  */
-#if 0
-#define LD_INIT_SWITCH (strcmp ("+Accept", ld2_argv[1]) ? "-init" : "+init")
-#define LD_FINI_SWITCH (strcmp ("+Accept", ld2_argv[1]) ? "-fini" : "+fini")
+/* We are using array initializers and don't want calls in the INIT
+   and FINI sections.  */
+#undef CRT_CALL_STATIC_FUNCTION
+#define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC)
+
+/* The init_priority attribute is not supported with HP ld.  This could be
+   supported if collect2 was used with LD_INIT_SWITCH.  Unfortunately, this
+   approach doesn't work with GNU ld since HP-UX doesn't support DT_INIT,
+   and therefore the -init and -fini GNU ld switches.  */
+#undef SUPPORTS_INIT_PRIORITY
+#define SUPPORTS_INIT_PRIORITY (TARGET_GNU_LD ? 1 : 0)
+
+/* We use DTOR_LIST_BEGIN to carry a bunch of hacks to allow us to use
+   the init and fini array sections with both the HP and GNU linkers.
+   The linkers setup the required dynamic entries in the dynamic segment
+   and the dynamic linker does the calls.  This approach avoids using
+   collect2.
+
+   The first hack is to implement __do_global_ctors_aux in crtbegin as
+   it needs to be the first entry in the init array so that it is called
+   last.  HP got the order of the init array backwards.  The DT_INIT_ARRAY
+   is supposed to be executed in the same order as the addresses appear in
+   the array.  DT_FINI_ARRAY is supposed to be executed in the opposite
+   order.
+
+   The second hack is stubs for __cxa_finalize and _Jv_RegisterClasses.
+   The HP implementation of undefined weak symbols is broken.  The linker
+   and dynamic loader both search for undefined weak symbols contrary the
+   generic System V ABI.  An undefined weak symbol should resolve to a
+   value of 0 rather than causing an error.  The prototypes for
+   __cxa_finalize and _Jv_RegisterClasses in crtstuff.c are weak when
+   weak is supported (GNU as), so in theory a strong define should override
+   the stub functions provided here.
+
+   The final hack is a set of plabels to implement the effect of
+   CRT_CALL_STATIC_FUNCTION.  HP-UX 11 only supports DI_INIT_ARRAY and
+   DT_FINI_ARRAY and they put the arrays in .init and .fini, rather than
+   in .init_array and .fini_array.  The standard defines for .init and
+   .fini have the execute flag set.  So, the assembler has to be hacked
+   to munge the standard flags for these sections to make them agree
+   with what the HP linker expects.  With the GNU linker, we need to
+   used the .init_array and .fini_array sections.  So, we set up for
+   both just in case.  Once we have built the table, the linker does
+   the rest of the work.
+
+   The order is significant.  Placing __do_global_ctors_aux first in
+   the list, results in it being called last.  User specified initializers,
+   either using the linker +init command or a plabel, run before the
+   initializers specified here.  */
+
+/* We need a __cxa_finalize stub if CRTSTUFFS_O is defined.  */
+#ifdef CRTSTUFFS_O
+#define PA_CXA_FINALIZE_STUB \
+extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;		\
+void									\
+__cxa_finalize (void *p __attribute__((unused))) {}
+#else
+#define PA_CXA_FINALIZE_STUB
+#endif
+
+/* We need a _Jv_RegisterClasses stub if JCR_SECTION_NAME is defined.  */
+#ifdef JCR_SECTION_NAME
+#define PA_JV_REGISTERCLASSES_STUB \
+void									\
+_Jv_RegisterClasses (void *p __attribute__((unused))) {}
+#else
+#define PA_JV_REGISTERCLASSES_STUB
+#endif
+
+/* We need to add frame_dummy to the initializer list if USE_EH_FRAME_REGISTRY
+   or JCR_SECTION_NAME is defined.  */
+#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
+#define PA_INIT_FRAME_DUMMY_ASM_OP ".dword P%frame_dummy"
+#else
+#define PA_INIT_FRAME_DUMMY_ASM_OP ""
+#endif
+
+#define PA_INIT_FINI_HACK \
+static void __attribute__((used))					\
+__do_global_ctors_aux (void)						\
+{									\
+  func_ptr *p = __CTOR_LIST__;						\
+  while (*(p + 1))							\
+    p++;								\
+  for (; *p != (func_ptr) -1; p--)					\
+    (*p) ();								\
+}									\
+									\
+PA_CXA_FINALIZE_STUB							\
+PA_JV_REGISTERCLASSES_STUB						\
+									\
+asm (HP_INIT_ARRAY_SECTION_ASM_OP);					\
+asm (".dword P%__do_global_ctors_aux");					\
+asm (PA_INIT_FRAME_DUMMY_ASM_OP);					\
+asm (GNU_INIT_ARRAY_SECTION_ASM_OP);					\
+asm (".dword P%__do_global_ctors_aux");					\
+asm (PA_INIT_FRAME_DUMMY_ASM_OP);					\
+asm (HP_FINI_ARRAY_SECTION_ASM_OP);					\
+asm (".dword P%__do_global_dtors_aux");					\
+asm (GNU_FINI_ARRAY_SECTION_ASM_OP);					\
+asm (".dword P%__do_global_dtors_aux")
+
+/* The following two variants of DTOR_LIST_BEGIN are identical to those
+   in crtstuff.c except for the addition of the above init-fini hack.  */
+#ifdef DTORS_SECTION_ASM_OP
+#define DTOR_LIST_BEGIN \
+asm (DTORS_SECTION_ASM_OP);						\
+STATIC func_ptr __DTOR_LIST__[1]					\
+  __attribute__ ((aligned(sizeof(func_ptr))))				\
+  = { (func_ptr) (-1) };						\
+PA_INIT_FINI_HACK
+#else
+#define DTOR_LIST_BEGIN \
+STATIC func_ptr __DTOR_LIST__[1]					\
+  __attribute__ ((section(".dtors"), aligned(sizeof(func_ptr))))	\
+  = { (func_ptr) (-1) };						\
+PA_INIT_FINI_HACK
 #endif
 
 /* If using HP ld do not call pxdb.  Use size as a program that does nothing
Index: config/pa/pa.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/config/pa/pa.c,v
retrieving revision 1.192
diff -u -3 -p -r1.192 pa.c
--- config/pa/pa.c	20 Dec 2002 23:24:39 -0000	1.192
+++ config/pa/pa.c	3 Jan 2003 19:57:05 -0000
@@ -124,6 +124,10 @@ static void pa_globalize_label PARAMS ((
      ATTRIBUTE_UNUSED;
 static void pa_asm_output_mi_thunk PARAMS ((FILE *, tree, HOST_WIDE_INT,
 					    HOST_WIDE_INT, tree));
+#if !defined(USE_COLLECT2)
+static void pa_asm_out_constructor PARAMS ((rtx, int));
+static void pa_asm_out_destructor PARAMS ((rtx, int));
+#endif
 static void copy_fp_args PARAMS ((rtx)) ATTRIBUTE_UNUSED;
 static int length_fp_args PARAMS ((rtx)) ATTRIBUTE_UNUSED;
 static struct deferred_plabel *get_plabel PARAMS ((const char *))
@@ -209,6 +213,13 @@ static size_t n_deferred_plabels = 0;
 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
 
+#if !defined(USE_COLLECT2)
+#undef TARGET_ASM_CONSTRUCTOR
+#define TARGET_ASM_CONSTRUCTOR pa_asm_out_constructor
+#undef TARGET_ASM_DESTRUCTOR
+#define TARGET_ASM_DESTRUCTOR pa_asm_out_destructor
+#endif
+
 struct gcc_target targetm = TARGET_INITIALIZER;
 
 void
@@ -6958,6 +6969,46 @@ fmpyaddoperands (operands)
   /* Passed.  Operands are suitable for fmpyadd.  */
   return 1;
 }
+
+#if !defined(USE_COLLECT2)
+static void
+pa_asm_out_constructor (symbol, priority)
+     rtx symbol;
+     int priority;
+{
+  if (!function_label_operand (symbol, VOIDmode))
+    hppa_encode_label (symbol);
+
+#ifdef CTORS_SECTION_ASM_OP
+  default_ctor_section_asm_out_constructor (symbol, priority);
+#else
+# ifdef TARGET_ASM_NAMED_SECTION
+  default_named_section_asm_out_constructor (symbol, priority);
+# else
+  default_stabs_asm_out_constructor (symbol, priority);
+# endif
+#endif
+}
+
+static void
+pa_asm_out_destructor (symbol, priority)
+     rtx symbol;
+     int priority;
+{
+  if (!function_label_operand (symbol, VOIDmode))
+    hppa_encode_label (symbol);
+
+#ifdef DTORS_SECTION_ASM_OP
+  default_dtor_section_asm_out_destructor (symbol, priority);
+#else
+# ifdef TARGET_ASM_NAMED_SECTION
+  default_named_section_asm_out_destructor (symbol, priority);
+# else
+  default_stabs_asm_out_destructor (symbol, priority);
+# endif
+#endif
+}
+#endif
 
 /* Returns 1 if the 6 operands specified in OPERANDS are suitable for
    use in fmpysub instructions.  */
Index: config/pa/som.h
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/config/pa/som.h,v
retrieving revision 1.39
diff -u -3 -p -r1.39 som.h
--- config/pa/som.h	31 Oct 2002 03:13:43 -0000	1.39
+++ config/pa/som.h	3 Jan 2003 19:57:06 -0000
@@ -362,10 +362,6 @@ do {						\
 /* The .align directive in the HP assembler allows up to a 32 alignment.  */
 #define MAX_OFILE_ALIGNMENT 32768
 
-/* SOM does not support the init_priority C++ attribute.  */
-#undef SUPPORTS_INIT_PRIORITY
-#define SUPPORTS_INIT_PRIORITY 0
-
 /* The SOM linker hardcodes paths into binaries.  As a result, dotdots
    must be removed from library prefixes to prevent binaries from depending
    on the location of the GCC tool directory.  The downside is GCC


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