1/* Definitions of target machine for GNU compiler, for HPs running
2   HP-UX using the 64bit runtime model.
3   Copyright (C) 1999-2020 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24<http://www.gnu.org/licenses/>.  */
25
26/* We use DTOR_LIST_BEGIN to carry a bunch of hacks to allow us to use
27   the init and fini array sections with both the HP and GNU linkers.
28   The linkers setup the required dynamic entries in the dynamic segment
29   and the dynamic linker does the calls.  This approach avoids using
30   collect2.
31
32   The first hack is to implement __do_global_ctors_aux in crtbegin as
33   it needs to be the first entry in the init array so that it is called
34   last.  HP got the order of the init array backwards.  The DT_INIT_ARRAY
35   is supposed to be executed in the same order as the addresses appear in
36   the array.  DT_FINI_ARRAY is supposed to be executed in the opposite
37   order.
38
39   The second hack is a set of plabels to implement the effect of
40   CRT_CALL_STATIC_FUNCTION.  HP-UX 11 only supports DI_INIT_ARRAY and
41   DT_FINI_ARRAY and they put the arrays in .init and .fini, rather than
42   in .init_array and .fini_array.  The standard defines for .init and
43   .fini have the execute flag set.  So, the assembler has to be hacked
44   to munge the standard flags for these sections to make them agree
45   with what the HP linker expects.  With the GNU linker, we need to
46   used the .init_array and .fini_array sections.  So, we set up for
47   both just in case.  Once we have built the table, the linker does
48   the rest of the work.
49   The order is significant.  Placing __do_global_ctors_aux first in
50   the list, results in it being called last.  User specified initializers,
51   either using the linker +init command or a plabel, run before the
52   initializers specified here.  */
53
54/* We need to add frame_dummy to the initializer list if EH_FRAME_SECTION_NAME
55   is defined.  */
56#if defined(__LIBGCC_EH_FRAME_SECTION_NAME__)
57#define PA_INIT_FRAME_DUMMY_ASM_OP ".dword P%frame_dummy"
58#else
59#define PA_INIT_FRAME_DUMMY_ASM_OP ""
60#endif
61
62/* The following hack sets up the .init, .init_array, .fini and
63   .fini_array sections.  */
64#define PA_CRTBEGIN_HACK \
65asm (TEXT_SECTION_ASM_OP);                                              \
66static void __attribute__((used))                                       \
67__do_global_ctors_aux (void)                                            \
68{                                                                       \
69  func_ptr *p = __CTOR_LIST__;                                          \
70  while (*(p + 1))                                                      \
71    p++;                                                                \
72  for (; *p != (func_ptr) -1; p--)                                      \
73    (*p) ();                                                            \
74}                                                                       \
75                                                                        \
76asm (HP_INIT_ARRAY_SECTION_ASM_OP);                                     \
77asm (".align 8");                                                       \
78asm (".dword P%__do_global_ctors_aux");                                 \
79asm (PA_INIT_FRAME_DUMMY_ASM_OP);                                       \
80asm (GNU_INIT_ARRAY_SECTION_ASM_OP);                                    \
81asm (".align 8");                                                       \
82asm (".dword P%__do_global_ctors_aux");                                 \
83asm (PA_INIT_FRAME_DUMMY_ASM_OP);                                       \
84asm (HP_FINI_ARRAY_SECTION_ASM_OP);                                     \
85asm (".align 8");                                                       \
86asm (".dword P%__do_global_dtors_aux");                                 \
87asm (GNU_FINI_ARRAY_SECTION_ASM_OP);                                    \
88asm (".align 8");                                                       \
89asm (".dword P%__do_global_dtors_aux")
90
91/* The following two variants of DTOR_LIST_BEGIN are identical to those
92   in crtstuff.c except for the addition of the above crtbegin hack.  */
93#ifdef __LIBGCC_DTORS_SECTION_ASM_OP__
94#define DTOR_LIST_BEGIN \
95asm (DTORS_SECTION_ASM_OP);                                             \
96STATIC func_ptr __DTOR_LIST__[1]                                        \
97  __attribute__ ((aligned(sizeof(func_ptr))))                           \
98  = { (func_ptr) (-1) };                                                \
99PA_CRTBEGIN_HACK
100#else
101#define DTOR_LIST_BEGIN \
102STATIC func_ptr __DTOR_LIST__[1]                                        \
103  __attribute__ ((section(".dtors"), aligned(sizeof(func_ptr))))        \
104  = { (func_ptr) (-1) };                                                \
105PA_CRTBEGIN_HACK
106#endif
107