| 1 | /* ldctor.c -- constructor support routines |
| 2 | Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc. |
| 3 | By Steve Chamberlain <sac@cygnus.com> |
| 4 | |
| 5 | This file is part of GLD, the Gnu Linker. |
| 6 | |
| 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) |
| 10 | any later version. |
| 11 | |
| 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. |
| 16 | |
| 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. */ |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | #include "sysdep.h" |
| 23 | #include "bfdlink.h" |
| 24 | |
| 25 | #include "ld.h" |
| 26 | #include "ldexp.h" |
| 27 | #include "ldlang.h" |
| 28 | #include "ldmisc.h" |
| 29 | #include "ldgram.h" |
| 30 | #include "ldmain.h" |
| 31 | #include "ldctor.h" |
| 32 | |
| 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; |
| 36 | |
| 37 | /* We keep a list of these structures for each sets we build. */ |
| 38 | |
| 39 | struct set_info |
| 40 | { |
| 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. */ |
| 46 | }; |
| 47 | |
| 48 | struct set_element |
| 49 | { |
| 50 | struct set_element *next; /* Next element. */ |
| 51 | asection *section; /* Section of value. */ |
| 52 | bfd_vma value; /* Value. */ |
| 53 | }; |
| 54 | |
| 55 | /* The sets we have seen. */ |
| 56 | |
| 57 | static struct set_info *sets; |
| 58 | |
| 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. */ |
| 65 | |
| 66 | void |
| 67 | ldctor_add_set_entry (h, reloc, section, value) |
| 68 | struct bfd_link_hash_entry *h; |
| 69 | bfd_reloc_code_real_type reloc; |
| 70 | asection *section; |
| 71 | bfd_vma value; |
| 72 | { |
| 73 | struct set_info *p; |
| 74 | struct set_element *e; |
| 75 | struct set_element **epp; |
| 76 | |
| 77 | for (p = sets; p != (struct set_info *) NULL; p = p->next) |
| 78 | if (p->h == h) |
| 79 | break; |
| 80 | |
| 81 | if (p == (struct set_info *) NULL) |
| 82 | { |
| 83 | p = (struct set_info *) xmalloc (sizeof (struct set_info)); |
| 84 | p->next = sets; |
| 85 | sets = p; |
| 86 | p->h = h; |
| 87 | p->reloc = reloc; |
| 88 | p->count = 0; |
| 89 | p->elements = NULL; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | if (p->reloc != reloc) |
| 94 | { |
| 95 | einfo ("%P%X: Different relocs used in set %s\n", h->root.string); |
| 96 | return; |
| 97 | } |
| 98 | |
| 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) |
| 111 | { |
| 112 | einfo ("%P%X: Different object file formats composing set %s\n", |
| 113 | h->root.string); |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | e = (struct set_element *) xmalloc (sizeof (struct set_element)); |
| 119 | e->next = NULL; |
| 120 | e->section = section; |
| 121 | e->value = value; |
| 122 | |
| 123 | for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next) |
| 124 | ; |
| 125 | *epp = e; |
| 126 | |
| 127 | ++p->count; |
| 128 | } |
| 129 | |
| 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. */ |
| 134 | |
| 135 | void |
| 136 | ldctor_build_sets () |
| 137 | { |
| 138 | lang_statement_list_type *old; |
| 139 | struct set_info *p; |
| 140 | |
| 141 | old = stat_ptr; |
| 142 | stat_ptr = &constructor_list; |
| 143 | |
| 144 | lang_list_init (stat_ptr); |
| 145 | |
| 146 | for (p = sets; p != (struct set_info *) NULL; p = p->next) |
| 147 | { |
| 148 | struct set_element *e; |
| 149 | reloc_howto_type *howto; |
| 150 | int size; |
| 151 | |
| 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 |
| 154 | not do anything. */ |
| 155 | if (p->h->type == bfd_link_hash_defined) |
| 156 | continue; |
| 157 | |
| 158 | /* For each set we build: |
| 159 | set: |
| 160 | .long number_of_elements |
| 161 | .long element0 |
| 162 | ... |
| 163 | .long elementN |
| 164 | .long 0 |
| 165 | except that we use the right size instead of .long. When |
| 166 | generating relocateable output, we generate relocs instead of |
| 167 | addresses. */ |
| 168 | howto = bfd_reloc_type_lookup (output_bfd, p->reloc); |
| 169 | if (howto == (reloc_howto_type *) NULL) |
| 170 | { |
| 171 | if (link_info.relocateable) |
| 172 | { |
| 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), |
| 176 | p->h->root.string); |
| 177 | continue; |
| 178 | } |
| 179 | |
| 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, |
| 183 | p->reloc); |
| 184 | if (howto == NULL) |
| 185 | { |
| 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), |
| 189 | p->h->root.string); |
| 190 | continue; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | switch (bfd_get_reloc_size (howto)) |
| 195 | { |
| 196 | case 1: size = BYTE; break; |
| 197 | case 2: size = SHORT; break; |
| 198 | case 4: size = LONG; break; |
| 199 | case 8: size = QUAD; break; |
| 200 | default: |
| 201 | einfo ("%P%X: Unsupported size %d for set %s\n", |
| 202 | bfd_get_reloc_size (howto), p->h->root.string); |
| 203 | size = LONG; |
| 204 | break; |
| 205 | } |
| 206 | |
| 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)); |
| 210 | |
| 211 | for (e = p->elements; e != (struct set_element *) NULL; e = e->next) |
| 212 | { |
| 213 | if (link_info.relocateable) |
| 214 | lang_add_reloc (p->reloc, howto, e->section, |
| 215 | (const char *) NULL, exp_intop (e->value)); |
| 216 | else |
| 217 | lang_add_data (size, exp_relop (e->section, e->value)); |
| 218 | } |
| 219 | |
| 220 | lang_add_data (size, exp_intop (0)); |
| 221 | } |
| 222 | |
| 223 | stat_ptr = old; |
| 224 | } |