[arm] Use .init_array for static constructors.

Paul Brook paul@codesourcery.com
Fri Sep 24 01:11:00 GMT 2004


The following patch makes arm targets use the .init_array section for static 
constructor lists. Non-eabi targets are unaffected. The pointers in this 
section are relocated by relocated by an R_ARM_TARGET1 relocation.
The eabi doesn't provide a mecahnism for supporting the init_priority 
attribute, so I've disables support for this on eabi based targets.

The eabi doesn't mention how static destructor lists should be handled, though 
it's implied that the atexit functionality makes these unnecessary.
I've left the handling of .dtors unchanged.

Tested with cross to arm-none-elf and arm-none-eabi.
Ok for csl-arm-branch? mainline?

Paul

2004-09-23  Paul Brook  <paul@codesourcery.com>

 * config/arm/arm.c (arm_elf_asm_constructor): New function.
 * config/arm/arm.h (CTORS_SECTION_ASM_OP): Define.
 (CTORS_SECTION_FOR_TARGET, CTOR_LIST_END, CTOR_LIST_BEGIN): Define.
 * config/arm/elf.h (TARGET_ASM_CONSTRUCTOR): Define.
 (SUPPORTS_INIT_PRIORITY): Evaluate to false for EABI based targets.

Index: gcc/config/arm/arm.c
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/gcc/config/arm/arm.c,v
retrieving revision 1.303.2.53
diff -u -p -r1.303.2.53 arm.c
--- gcc/config/arm/arm.c 22 Sep 2004 15:40:28 -0000 1.303.2.53
+++ gcc/config/arm/arm.c 23 Sep 2004 18:14:13 -0000
@@ -144,6 +144,9 @@ static rtx arm_expand_unop_builtin (enum
 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
 static void emit_constant_insn (rtx cond, rtx pattern);
 
+#ifdef OBJECT_FORMAT_ELF
+static void arm_elf_asm_constructor (rtx, int);
+#endif
 #ifndef ARM_PE
 static void arm_encode_section_info (tree, rtx, int);
 #endif
@@ -11036,6 +11039,26 @@ arm_assemble_integer (rtx x, unsigned in
 
   return default_assemble_integer (x, size, aligned_p);
 }
+
+
+/* Add a function to the list of static constructors.  */
+
+static void
+arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
+{
+  if (!TARGET_AAPCS_BASED)
+    {
+      default_named_section_asm_out_constructor (symbol, priority);
+      return;
+    }
+
+  /* Put these in the .init_array section, using a special relocation.  */
+  ctors_section ();
+  assemble_align (POINTER_SIZE);
+  fputs ("\t.word\t", asm_out_file);
+  output_addr_const (asm_out_file, symbol);
+  fputs ("(target1)\n", asm_out_file);
+}
 #endif
 
 /* A finite state machine takes care of noticing whether or not instructions
Index: gcc/config/arm/arm.h
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/gcc/config/arm/arm.h,v
retrieving revision 1.210.2.41
diff -u -p -r1.210.2.41 arm.h
--- gcc/config/arm/arm.h 22 Sep 2004 15:40:30 -0000 1.210.2.41
+++ gcc/config/arm/arm.h 23 Sep 2004 18:14:13 -0000
@@ -2112,6 +2112,38 @@ typedef struct
 #define ASM_OUTPUT_LABELREF(FILE, NAME)  \
    arm_asm_output_labelref (FILE, NAME)
 
+/* The EABI specifies that constructors should go in .init_array.
+   Other targets use .ctors for compatibility.  */
+#undef CTORS_SECTION_ASM_OP
+#define CTORS_SECTION_ASM_OP (TARGET_AAPCS_BASED \
+         ? "\t.section\t.init_array,\"aw\",%init_array" \
+         : "\t.section\t.ctors,\"aw\",%progbits")
+
+/* There macros are used in target files.  We need to define them here
+   because CTORS_SECTION_ASM_OP is not a constant.  The code is identical
+   to the code in crtstuff.c, but duplicated here to avoid depending on
+   TARGET_AAPCS_BASED.  */
+
+#ifdef __ARM_EABI__
+#define CTORS_SECTION_FOR_TARGET "\t.section\t.init_array,\"aw\",%init_array"
+#else
+#define CTORS_SECTION_FOR_TARGET "\t.section\t.ctors,\"aw\",%progbits"
+#endif
+
+#define CTOR_LIST_END \
+static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; \
+asm (CTORS_SECTION_FOR_TARGET); \
+STATIC func_ptr __CTOR_END__[1] \
+  __attribute__((aligned(sizeof(func_ptr)))) \
+  = { (func_ptr) 0 };
+#define CTOR_LIST_BEGIN \
+static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; \
+asm (CTORS_SECTION_FOR_TARGET); \
+STATIC func_ptr __CTOR_LIST__[1] \
+  __attribute__ ((__unused__, aligned(sizeof(func_ptr)))) \
+  = { (func_ptr) (-1) };
+
+
 #define ARM_DECLARE_FUNCTION_SIZE(STREAM, NAME, DECL) \
   arm_encode_call_attribute (DECL, SHORT_CALL_FLAG_CHAR)
 
Index: gcc/config/arm/elf.h
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/gcc/config/arm/elf.h,v
retrieving revision 1.44.8.5
diff -u -p -r1.44.8.5 elf.h
--- gcc/config/arm/elf.h 22 Sep 2004 15:40:30 -0000 1.44.8.5
+++ gcc/config/arm/elf.h 23 Sep 2004 18:14:13 -0000
@@ -124,6 +124,10 @@
 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
 
 
+/* Output an element in the static constructor array.  */
+#undef TARGET_ASM_CONSTRUCTOR
+#define TARGET_ASM_CONSTRUCTOR arm_elf_asm_constructor
+
 /* For PIC code we need to explicitly specify (PLT) and (GOT) relocs.  */
 #define NEED_PLT_RELOC flag_pic
 #define NEED_GOT_RELOC flag_pic
@@ -147,4 +151,5 @@
     }       \
   while (0)
 
-#define SUPPORTS_INIT_PRIORITY 1
+/* The EABI doesn't provide a way of implementing init_priority.  */
+#define SUPPORTS_INIT_PRIORITY (!TARGET_AAPCS_BASED)



More information about the Gcc-patches mailing list