1 /* ldctor.c -- constructor support routines
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3 By Steve Chamberlain <sac@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
33 /* The list of statements needed to handle constructors. These are
34 invoked by the command CONSTRUCTORS in the linker script. */
35 lang_statement_list_type constructor_list
;
37 /* We keep a list of these structures for each sets we build. */
41 struct set_info
*next
; /* Next set. */
42 struct bfd_link_hash_entry
*h
; /* Hash table entry. */
43 bfd_reloc_code_real_type reloc
; /* Reloc to use for an entry. */
44 size_t count
; /* Number of elements. */
45 struct set_element
*elements
; /* Elements in set. */
50 struct set_element
*next
; /* Next element. */
51 asection
*section
; /* Section of value. */
52 bfd_vma value
; /* Value. */
55 /* The sets we have seen. */
57 static struct set_info
*sets
;
59 /* Add an entry to a set. H is the entry in the linker hash table.
60 RELOC is the relocation to use for an entry in the set. SECTION
61 and VALUE are the value to add. This is called during the first
62 phase of the link, when we are still gathering symbols together.
63 We just record the information now. The ldctor_find_constructors
64 function will construct the sets. */
67 ldctor_add_set_entry (h
, reloc
, section
, value
)
68 struct bfd_link_hash_entry
*h
;
69 bfd_reloc_code_real_type reloc
;
74 struct set_element
*e
;
75 struct set_element
**epp
;
77 for (p
= sets
; p
!= (struct set_info
*) NULL
; p
= p
->next
)
81 if (p
== (struct set_info
*) NULL
)
83 p
= (struct set_info
*) xmalloc (sizeof (struct set_info
));
93 if (p
->reloc
!= reloc
)
95 einfo ("%P%X: Different relocs used in set %s\n", h
->root
.string
);
99 /* Don't permit a set to be constructed from different object
100 file formats. The same reloc may have different results. We
101 actually could sometimes handle this, but the case is
102 unlikely to ever arise. Sometimes constructor symbols are in
103 unusual sections, such as the absolute section--this appears
104 to be the case in Linux a.out--and in such cases we just
105 assume everything is OK. */
106 if (p
->elements
!= NULL
107 && section
->owner
!= NULL
108 && p
->elements
->section
->owner
!= NULL
109 && strcmp (bfd_get_target (section
->owner
),
110 bfd_get_target (p
->elements
->section
->owner
)) != 0)
112 einfo ("%P%X: Different object file formats composing set %s\n",
118 e
= (struct set_element
*) xmalloc (sizeof (struct set_element
));
120 e
->section
= section
;
123 for (epp
= &p
->elements
; *epp
!= NULL
; epp
= &(*epp
)->next
)
130 /* This function is called after the first phase of the link and
131 before the second phase. At this point all set information has
132 been gathered. We now put the statements to build the sets
133 themselves into constructor_list. */
138 lang_statement_list_type
*old
;
142 stat_ptr
= &constructor_list
;
144 lang_list_init (stat_ptr
);
146 for (p
= sets
; p
!= (struct set_info
*) NULL
; p
= p
->next
)
148 struct set_element
*e
;
149 reloc_howto_type
*howto
;
152 /* If the symbol is defined, we may have been invoked from
153 collect, and the sets may already have been built, so we do
155 if (p
->h
->type
== bfd_link_hash_defined
)
158 /* For each set we build:
160 .long number_of_elements
165 except that we use the right size instead of .long. When
166 generating relocateable output, we generate relocs instead of
168 howto
= bfd_reloc_type_lookup (output_bfd
, p
->reloc
);
169 if (howto
== (reloc_howto_type
*) NULL
)
171 if (link_info
.relocateable
)
173 einfo ("%P%X: %s does not support reloc %s for set %s\n",
174 bfd_get_target (output_bfd
),
175 bfd_get_reloc_code_name (p
->reloc
),
180 /* If this is not a relocateable link, all we need is the
181 size, which we can get from the input BFD. */
182 howto
= bfd_reloc_type_lookup (p
->elements
->section
->owner
,
186 einfo ("%P%X: %s does not support reloc %s for set %s\n",
187 bfd_get_target (p
->elements
->section
->owner
),
188 bfd_get_reloc_code_name (p
->reloc
),
194 switch (bfd_get_reloc_size (howto
))
196 case 1: size
= BYTE
; break;
197 case 2: size
= SHORT
; break;
198 case 4: size
= LONG
; break;
199 case 8: size
= QUAD
; break;
201 einfo ("%P%X: Unsupported size %d for set %s\n",
202 bfd_get_reloc_size (howto
), p
->h
->root
.string
);
207 lang_add_assignment (exp_assop ('=', p
->h
->root
.string
,
208 exp_nameop (NAME
, ".")));
209 lang_add_data (size
, exp_intop ((bfd_vma
) p
->count
));
211 for (e
= p
->elements
; e
!= (struct set_element
*) NULL
; e
= e
->next
)
213 if (link_info
.relocateable
)
214 lang_add_reloc (p
->reloc
, howto
, e
->section
,
215 (const char *) NULL
, exp_intop (e
->value
));
217 lang_add_data (size
, exp_relop (e
->section
, e
->value
));
220 lang_add_data (size
, exp_intop (0));