Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
a2b64bed | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
bde18da4 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
f96b4a7b | 6 | This file is part of the GNU Binutils. |
252b5132 | 7 | |
f96b4a7b | 8 | This program is free software; you can redistribute it and/or modify |
53b2a62f | 9 | it under the terms of the GNU General Public License as published by |
f96b4a7b NC |
10 | the Free Software Foundation; either version 3 of the License, or |
11 | (at your option) any later version. | |
252b5132 | 12 | |
f96b4a7b | 13 | This program is distributed in the hope that it will be useful, |
53b2a62f NC |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
53b2a62f | 18 | You should have received a copy of the GNU General Public License |
f96b4a7b NC |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | MA 02110-1301, USA. */ | |
252b5132 | 22 | |
de7dd2bd NC |
23 | #include <limits.h> |
24 | ||
252b5132 | 25 | #include "sysdep.h" |
3db64b00 | 26 | #include "bfd.h" |
252b5132 | 27 | #include "libiberty.h" |
3882b010 | 28 | #include "safe-ctype.h" |
252b5132 RH |
29 | #include "obstack.h" |
30 | #include "bfdlink.h" | |
31 | ||
32 | #include "ld.h" | |
33 | #include "ldmain.h" | |
252b5132 RH |
34 | #include "ldexp.h" |
35 | #include "ldlang.h" | |
df2a7313 | 36 | #include <ldgram.h> |
252b5132 RH |
37 | #include "ldlex.h" |
38 | #include "ldmisc.h" | |
39 | #include "ldctor.h" | |
40 | #include "ldfile.h" | |
b71e2778 | 41 | #include "ldemul.h" |
252b5132 RH |
42 | #include "fnmatch.h" |
43 | #include "demangle.h" | |
108ba305 | 44 | #include "hashtab.h" |
252b5132 | 45 | |
7abb6dea | 46 | #ifndef offsetof |
cf888e70 | 47 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
48 | #endif |
49 | ||
cf888e70 | 50 | /* Locals variables. */ |
252b5132 | 51 | static struct obstack stat_obstack; |
35835446 | 52 | static struct obstack map_obstack; |
252b5132 RH |
53 | |
54 | #define obstack_chunk_alloc xmalloc | |
55 | #define obstack_chunk_free free | |
5f992e62 | 56 | static const char *startup_file; |
b34976b6 | 57 | static bfd_boolean placed_commons = FALSE; |
8423293d | 58 | static bfd_boolean stripped_excluded_sections = FALSE; |
252b5132 | 59 | static lang_output_section_statement_type *default_common_section; |
b34976b6 | 60 | static bfd_boolean map_option_f; |
252b5132 RH |
61 | static bfd_vma print_dot; |
62 | static lang_input_statement_type *first_file; | |
5f992e62 AM |
63 | static const char *current_target; |
64 | static const char *output_target; | |
252b5132 | 65 | static lang_statement_list_type statement_list; |
420e579c | 66 | static struct bfd_hash_table lang_definedness_table; |
bde18da4 AM |
67 | static lang_statement_list_type *stat_save[10]; |
68 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
252b5132 | 69 | |
cf888e70 | 70 | /* Forward declarations. */ |
1579bae1 | 71 | static void exp_init_os (etree_type *); |
35835446 | 72 | static void init_map_userdata (bfd *, asection *, void *); |
1579bae1 | 73 | static lang_input_statement_type *lookup_name (const char *); |
420e579c HPN |
74 | static struct bfd_hash_entry *lang_definedness_newfunc |
75 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); | |
1579bae1 | 76 | static void insert_undefined (const char *); |
35835446 | 77 | static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *); |
1579bae1 AM |
78 | static void print_statement (lang_statement_union_type *, |
79 | lang_output_section_statement_type *); | |
80 | static void print_statement_list (lang_statement_union_type *, | |
81 | lang_output_section_statement_type *); | |
82 | static void print_statements (void); | |
4d4920ec | 83 | static void print_input_section (asection *); |
1579bae1 AM |
84 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); |
85 | static void lang_record_phdrs (void); | |
86 | static void lang_do_version_exports_section (void); | |
55255dae L |
87 | static void lang_finalize_version_expr_head |
88 | (struct bfd_elf_version_expr_head *); | |
1579bae1 | 89 | |
cf888e70 | 90 | /* Exported variables. */ |
252b5132 | 91 | lang_output_section_statement_type *abs_output_section; |
aea4bd9d | 92 | lang_statement_list_type lang_output_section_statement; |
252b5132 | 93 | lang_statement_list_type *stat_ptr = &statement_list; |
87f2a346 | 94 | lang_statement_list_type file_chain = { NULL, NULL }; |
dc27aea4 | 95 | lang_statement_list_type input_file_chain; |
e3e942e9 | 96 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
a359509e | 97 | static const char *entry_symbol_default = "start"; |
1e281515 | 98 | const char *entry_section = ".text"; |
b34976b6 AM |
99 | bfd_boolean entry_from_cmdline; |
100 | bfd_boolean lang_has_input_file = FALSE; | |
101 | bfd_boolean had_output_filename = FALSE; | |
102 | bfd_boolean lang_float_flag = FALSE; | |
103 | bfd_boolean delete_output_file_on_failure = FALSE; | |
f5ff60a6 | 104 | struct lang_phdr *lang_phdr_list; |
252b5132 | 105 | struct lang_nocrossrefs *nocrossref_list; |
279e75dc | 106 | static struct unique_sections *unique_section_list; |
e3f2db7f | 107 | static bfd_boolean ldlang_sysrooted_script = FALSE; |
e9ee469a AM |
108 | |
109 | /* Functions that traverse the linker script and might evaluate | |
110 | DEFINED() need to increment this. */ | |
420e579c | 111 | int lang_statement_iteration = 0; |
252b5132 RH |
112 | |
113 | etree_type *base; /* Relocation base - or null */ | |
114 | ||
61f5d054 L |
115 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
116 | Although backslashes are treated specially if a pattern contains | |
117 | wildcards, we do not consider the mere presence of a backslash to | |
118 | be enough to cause the pattern to be treated as a wildcard. | |
119 | That lets us handle DOS filenames more naturally. */ | |
120 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
121 | ||
d1778b88 | 122 | #define new_stat(x, y) \ |
1579bae1 | 123 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 124 | |
d1778b88 AM |
125 | #define outside_section_address(q) \ |
126 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 127 | |
d1778b88 AM |
128 | #define outside_symbol_address(q) \ |
129 | ((q)->value + outside_section_address (q->section)) | |
252b5132 RH |
130 | |
131 | #define SECTION_NAME_MAP_LENGTH (16) | |
132 | ||
1579bae1 AM |
133 | void * |
134 | stat_alloc (size_t size) | |
252b5132 RH |
135 | { |
136 | return obstack_alloc (&stat_obstack, size); | |
137 | } | |
138 | ||
97407faf AM |
139 | static int |
140 | name_match (const char *pattern, const char *name) | |
141 | { | |
142 | if (wildcardp (pattern)) | |
143 | return fnmatch (pattern, name, 0); | |
144 | return strcmp (pattern, name); | |
145 | } | |
146 | ||
147 | /* If PATTERN is of the form archive:file, return a pointer to the | |
148 | separator. If not, return NULL. */ | |
149 | ||
150 | static char * | |
151 | archive_path (const char *pattern) | |
152 | { | |
153 | char *p = NULL; | |
154 | ||
155 | if (link_info.path_separator == 0) | |
156 | return p; | |
157 | ||
158 | p = strchr (pattern, link_info.path_separator); | |
159 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
160 | if (p == NULL || link_info.path_separator != ':') | |
161 | return p; | |
162 | ||
163 | /* Assume a match on the second char is part of drive specifier, | |
164 | as in "c:\silly.dos". */ | |
967928e9 | 165 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
166 | p = strchr (p + 1, link_info.path_separator); |
167 | #endif | |
168 | return p; | |
169 | } | |
170 | ||
967928e9 AM |
171 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
172 | return whether F matches FILE_SPEC. */ | |
173 | ||
174 | static bfd_boolean | |
175 | input_statement_is_archive_path (const char *file_spec, char *sep, | |
176 | lang_input_statement_type *f) | |
177 | { | |
178 | bfd_boolean match = FALSE; | |
179 | ||
180 | if ((*(sep + 1) == 0 | |
181 | || name_match (sep + 1, f->filename) == 0) | |
182 | && ((sep != file_spec) | |
183 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
184 | { | |
185 | match = TRUE; | |
186 | ||
187 | if (sep != file_spec) | |
188 | { | |
189 | const char *aname = f->the_bfd->my_archive->filename; | |
190 | *sep = 0; | |
191 | match = name_match (file_spec, aname) == 0; | |
192 | *sep = link_info.path_separator; | |
193 | } | |
194 | } | |
195 | return match; | |
196 | } | |
197 | ||
8a99a385 | 198 | static bfd_boolean |
d0d6a25b | 199 | unique_section_p (const asection *sec) |
577a0623 AM |
200 | { |
201 | struct unique_sections *unam; | |
d0d6a25b | 202 | const char *secnam; |
577a0623 | 203 | |
d0d6a25b AM |
204 | if (link_info.relocatable |
205 | && sec->owner != NULL | |
206 | && bfd_is_group_section (sec->owner, sec)) | |
207 | return TRUE; | |
208 | ||
209 | secnam = sec->name; | |
577a0623 | 210 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf AM |
211 | if (name_match (unam->name, secnam) == 0) |
212 | return TRUE; | |
577a0623 | 213 | |
b34976b6 | 214 | return FALSE; |
577a0623 AM |
215 | } |
216 | ||
08da4cac | 217 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 218 | |
72223188 JJ |
219 | /* Try processing a section against a wildcard. This just calls |
220 | the callback unless the filename exclusion list is present | |
221 | and excludes the file. It's hardly ever present so this | |
222 | function is very fast. */ | |
223 | ||
4dec4d4e | 224 | static void |
72223188 JJ |
225 | walk_wild_consider_section (lang_wild_statement_type *ptr, |
226 | lang_input_statement_type *file, | |
227 | asection *s, | |
228 | struct wildcard_list *sec, | |
229 | callback_t callback, | |
230 | void *data) | |
4dec4d4e | 231 | { |
72223188 JJ |
232 | struct name_list *list_tmp; |
233 | ||
97407faf | 234 | /* Don't process sections from files which were excluded. */ |
72223188 JJ |
235 | for (list_tmp = sec->spec.exclude_name_list; |
236 | list_tmp; | |
237 | list_tmp = list_tmp->next) | |
238 | { | |
967928e9 | 239 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 240 | |
967928e9 | 241 | if (p != NULL) |
72223188 | 242 | { |
967928e9 AM |
243 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
244 | return; | |
72223188 JJ |
245 | } |
246 | ||
967928e9 AM |
247 | else if (name_match (list_tmp->name, file->filename) == 0) |
248 | return; | |
249 | ||
250 | /* FIXME: Perhaps remove the following at some stage? Matching | |
251 | unadorned archives like this was never documented and has | |
252 | been superceded by the archive:path syntax. */ | |
253 | else if (file->the_bfd != NULL | |
254 | && file->the_bfd->my_archive != NULL | |
255 | && name_match (list_tmp->name, | |
256 | file->the_bfd->my_archive->filename) == 0) | |
257 | return; | |
72223188 JJ |
258 | } |
259 | ||
967928e9 | 260 | (*callback) (ptr, sec, s, file, data); |
72223188 JJ |
261 | } |
262 | ||
263 | /* Lowest common denominator routine that can handle everything correctly, | |
264 | but slowly. */ | |
265 | ||
266 | static void | |
267 | walk_wild_section_general (lang_wild_statement_type *ptr, | |
268 | lang_input_statement_type *file, | |
269 | callback_t callback, | |
270 | void *data) | |
271 | { | |
272 | asection *s; | |
273 | struct wildcard_list *sec; | |
b6bf44ba AM |
274 | |
275 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
4dec4d4e | 276 | { |
b6bf44ba | 277 | sec = ptr->section_list; |
2181f54f AM |
278 | if (sec == NULL) |
279 | (*callback) (ptr, sec, s, file, data); | |
280 | ||
281 | while (sec != NULL) | |
08da4cac | 282 | { |
b34976b6 | 283 | bfd_boolean skip = FALSE; |
2181f54f | 284 | |
72223188 | 285 | if (sec->spec.name != NULL) |
b6bf44ba | 286 | { |
2181f54f AM |
287 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
288 | ||
97407faf | 289 | skip = name_match (sec->spec.name, sname) != 0; |
b6bf44ba | 290 | } |
4dec4d4e | 291 | |
b6bf44ba | 292 | if (!skip) |
72223188 | 293 | walk_wild_consider_section (ptr, file, s, sec, callback, data); |
4dec4d4e | 294 | |
2181f54f | 295 | sec = sec->next; |
4dec4d4e RH |
296 | } |
297 | } | |
298 | } | |
299 | ||
72223188 JJ |
300 | /* Routines to find a single section given its name. If there's more |
301 | than one section with that name, we report that. */ | |
302 | ||
303 | typedef struct | |
304 | { | |
305 | asection *found_section; | |
306 | bfd_boolean multiple_sections_found; | |
307 | } section_iterator_callback_data; | |
308 | ||
309 | static bfd_boolean | |
310 | section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data) | |
311 | { | |
312 | section_iterator_callback_data *d = data; | |
313 | ||
314 | if (d->found_section != NULL) | |
315 | { | |
316 | d->multiple_sections_found = TRUE; | |
317 | return TRUE; | |
318 | } | |
319 | ||
320 | d->found_section = s; | |
321 | return FALSE; | |
322 | } | |
323 | ||
324 | static asection * | |
325 | find_section (lang_input_statement_type *file, | |
326 | struct wildcard_list *sec, | |
327 | bfd_boolean *multiple_sections_found) | |
328 | { | |
329 | section_iterator_callback_data cb_data = { NULL, FALSE }; | |
330 | ||
329c1c86 | 331 | bfd_get_section_by_name_if (file->the_bfd, sec->spec.name, |
72223188 JJ |
332 | section_iterator_callback, &cb_data); |
333 | *multiple_sections_found = cb_data.multiple_sections_found; | |
334 | return cb_data.found_section; | |
335 | } | |
336 | ||
337 | /* Code for handling simple wildcards without going through fnmatch, | |
338 | which can be expensive because of charset translations etc. */ | |
339 | ||
340 | /* A simple wild is a literal string followed by a single '*', | |
341 | where the literal part is at least 4 characters long. */ | |
342 | ||
343 | static bfd_boolean | |
344 | is_simple_wild (const char *name) | |
345 | { | |
346 | size_t len = strcspn (name, "*?["); | |
347 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
348 | } | |
349 | ||
350 | static bfd_boolean | |
351 | match_simple_wild (const char *pattern, const char *name) | |
352 | { | |
353 | /* The first four characters of the pattern are guaranteed valid | |
354 | non-wildcard characters. So we can go faster. */ | |
355 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
356 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
357 | return FALSE; | |
358 | ||
359 | pattern += 4; | |
360 | name += 4; | |
361 | while (*pattern != '*') | |
362 | if (*name++ != *pattern++) | |
363 | return FALSE; | |
364 | ||
365 | return TRUE; | |
366 | } | |
367 | ||
50c77e5d NC |
368 | /* Compare sections ASEC and BSEC according to SORT. */ |
369 | ||
370 | static int | |
371 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
372 | { | |
373 | int ret; | |
374 | ||
375 | switch (sort) | |
376 | { | |
377 | default: | |
378 | abort (); | |
379 | ||
380 | case by_alignment_name: | |
381 | ret = (bfd_section_alignment (bsec->owner, bsec) | |
382 | - bfd_section_alignment (asec->owner, asec)); | |
383 | if (ret) | |
384 | break; | |
385 | /* Fall through. */ | |
386 | ||
387 | case by_name: | |
388 | ret = strcmp (bfd_get_section_name (asec->owner, asec), | |
389 | bfd_get_section_name (bsec->owner, bsec)); | |
390 | break; | |
391 | ||
392 | case by_name_alignment: | |
393 | ret = strcmp (bfd_get_section_name (asec->owner, asec), | |
394 | bfd_get_section_name (bsec->owner, bsec)); | |
395 | if (ret) | |
396 | break; | |
397 | /* Fall through. */ | |
398 | ||
399 | case by_alignment: | |
400 | ret = (bfd_section_alignment (bsec->owner, bsec) | |
401 | - bfd_section_alignment (asec->owner, asec)); | |
402 | break; | |
403 | } | |
404 | ||
405 | return ret; | |
406 | } | |
407 | ||
329c1c86 | 408 | /* Build a Binary Search Tree to sort sections, unlike insertion sort |
50c77e5d NC |
409 | used in wild_sort(). BST is considerably faster if the number of |
410 | of sections are large. */ | |
411 | ||
412 | static lang_section_bst_type ** | |
413 | wild_sort_fast (lang_wild_statement_type *wild, | |
329c1c86 AM |
414 | struct wildcard_list *sec, |
415 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
416 | asection *section) | |
50c77e5d | 417 | { |
329c1c86 | 418 | lang_section_bst_type **tree; |
50c77e5d | 419 | |
e6f2cbf5 | 420 | tree = &wild->tree; |
50c77e5d | 421 | if (!wild->filenames_sorted |
329c1c86 | 422 | && (sec == NULL || sec->spec.sorted == none)) |
50c77e5d NC |
423 | { |
424 | /* Append at the right end of tree. */ | |
425 | while (*tree) | |
329c1c86 | 426 | tree = &((*tree)->right); |
50c77e5d NC |
427 | return tree; |
428 | } | |
429 | ||
329c1c86 | 430 | while (*tree) |
50c77e5d NC |
431 | { |
432 | /* Find the correct node to append this section. */ | |
329c1c86 AM |
433 | if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0) |
434 | tree = &((*tree)->left); | |
435 | else | |
436 | tree = &((*tree)->right); | |
50c77e5d NC |
437 | } |
438 | ||
439 | return tree; | |
440 | } | |
441 | ||
442 | /* Use wild_sort_fast to build a BST to sort sections. */ | |
443 | ||
444 | static void | |
445 | output_section_callback_fast (lang_wild_statement_type *ptr, | |
329c1c86 AM |
446 | struct wildcard_list *sec, |
447 | asection *section, | |
448 | lang_input_statement_type *file, | |
449 | void *output ATTRIBUTE_UNUSED) | |
50c77e5d NC |
450 | { |
451 | lang_section_bst_type *node; | |
452 | lang_section_bst_type **tree; | |
453 | ||
454 | if (unique_section_p (section)) | |
455 | return; | |
456 | ||
457 | node = xmalloc (sizeof (lang_section_bst_type)); | |
458 | node->left = 0; | |
459 | node->right = 0; | |
460 | node->section = section; | |
461 | ||
462 | tree = wild_sort_fast (ptr, sec, file, section); | |
463 | if (tree != NULL) | |
464 | *tree = node; | |
465 | } | |
466 | ||
467 | /* Convert a sorted sections' BST back to list form. */ | |
468 | ||
469 | static void | |
329c1c86 AM |
470 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
471 | lang_section_bst_type *tree, | |
472 | void *output) | |
50c77e5d NC |
473 | { |
474 | if (tree->left) | |
475 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
476 | ||
329c1c86 AM |
477 | lang_add_section (&ptr->children, tree->section, |
478 | (lang_output_section_statement_type *) output); | |
50c77e5d NC |
479 | |
480 | if (tree->right) | |
481 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
482 | ||
483 | free (tree); | |
484 | } | |
485 | ||
72223188 JJ |
486 | /* Specialized, optimized routines for handling different kinds of |
487 | wildcards */ | |
488 | ||
489 | static void | |
490 | walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr, | |
491 | lang_input_statement_type *file, | |
492 | callback_t callback, | |
493 | void *data) | |
494 | { | |
495 | /* We can just do a hash lookup for the section with the right name. | |
496 | But if that lookup discovers more than one section with the name | |
497 | (should be rare), we fall back to the general algorithm because | |
498 | we would otherwise have to sort the sections to make sure they | |
499 | get processed in the bfd's order. */ | |
500 | bfd_boolean multiple_sections_found; | |
501 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
502 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
503 | ||
504 | if (multiple_sections_found) | |
505 | walk_wild_section_general (ptr, file, callback, data); | |
506 | else if (s0) | |
507 | walk_wild_consider_section (ptr, file, s0, sec0, callback, data); | |
508 | } | |
509 | ||
510 | static void | |
511 | walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr, | |
512 | lang_input_statement_type *file, | |
513 | callback_t callback, | |
514 | void *data) | |
515 | { | |
516 | asection *s; | |
517 | struct wildcard_list *wildsec0 = ptr->handler_data[0]; | |
518 | ||
519 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
520 | { | |
521 | const char *sname = bfd_get_section_name (file->the_bfd, s); | |
522 | bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname); | |
523 | ||
524 | if (!skip) | |
525 | walk_wild_consider_section (ptr, file, s, wildsec0, callback, data); | |
526 | } | |
527 | } | |
528 | ||
529 | static void | |
530 | walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr, | |
531 | lang_input_statement_type *file, | |
532 | callback_t callback, | |
533 | void *data) | |
534 | { | |
535 | asection *s; | |
536 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
537 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
538 | bfd_boolean multiple_sections_found; | |
539 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
540 | ||
541 | if (multiple_sections_found) | |
542 | { | |
543 | walk_wild_section_general (ptr, file, callback, data); | |
544 | return; | |
545 | } | |
546 | ||
547 | /* Note that if the section was not found, s0 is NULL and | |
548 | we'll simply never succeed the s == s0 test below. */ | |
549 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
550 | { | |
551 | /* Recall that in this code path, a section cannot satisfy more | |
552 | than one spec, so if s == s0 then it cannot match | |
553 | wildspec1. */ | |
554 | if (s == s0) | |
555 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
556 | else | |
557 | { | |
558 | const char *sname = bfd_get_section_name (file->the_bfd, s); | |
559 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); | |
560 | ||
561 | if (!skip) | |
562 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, | |
563 | data); | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
568 | static void | |
569 | walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr, | |
570 | lang_input_statement_type *file, | |
571 | callback_t callback, | |
572 | void *data) | |
573 | { | |
574 | asection *s; | |
575 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
576 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
577 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
578 | bfd_boolean multiple_sections_found; | |
579 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
580 | ||
581 | if (multiple_sections_found) | |
582 | { | |
583 | walk_wild_section_general (ptr, file, callback, data); | |
584 | return; | |
585 | } | |
586 | ||
587 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
588 | { | |
589 | if (s == s0) | |
590 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
591 | else | |
592 | { | |
593 | const char *sname = bfd_get_section_name (file->the_bfd, s); | |
594 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); | |
595 | ||
596 | if (!skip) | |
597 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, data); | |
598 | else | |
599 | { | |
600 | skip = !match_simple_wild (wildsec2->spec.name, sname); | |
601 | if (!skip) | |
602 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
603 | data); | |
604 | } | |
605 | } | |
606 | } | |
607 | } | |
608 | ||
609 | static void | |
610 | walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr, | |
611 | lang_input_statement_type *file, | |
612 | callback_t callback, | |
613 | void *data) | |
614 | { | |
615 | asection *s; | |
616 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
617 | struct wildcard_list *sec1 = ptr->handler_data[1]; | |
618 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
619 | struct wildcard_list *wildsec3 = ptr->handler_data[3]; | |
620 | bfd_boolean multiple_sections_found; | |
621 | asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1; | |
622 | ||
623 | if (multiple_sections_found) | |
624 | { | |
625 | walk_wild_section_general (ptr, file, callback, data); | |
626 | return; | |
627 | } | |
628 | ||
629 | s1 = find_section (file, sec1, &multiple_sections_found); | |
630 | if (multiple_sections_found) | |
631 | { | |
632 | walk_wild_section_general (ptr, file, callback, data); | |
633 | return; | |
634 | } | |
635 | ||
636 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
637 | { | |
638 | if (s == s0) | |
639 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
640 | else | |
641 | if (s == s1) | |
642 | walk_wild_consider_section (ptr, file, s, sec1, callback, data); | |
643 | else | |
644 | { | |
645 | const char *sname = bfd_get_section_name (file->the_bfd, s); | |
646 | bfd_boolean skip = !match_simple_wild (wildsec2->spec.name, | |
647 | sname); | |
648 | ||
649 | if (!skip) | |
650 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
651 | data); | |
652 | else | |
653 | { | |
654 | skip = !match_simple_wild (wildsec3->spec.name, sname); | |
655 | if (!skip) | |
656 | walk_wild_consider_section (ptr, file, s, wildsec3, | |
657 | callback, data); | |
658 | } | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
663 | static void | |
664 | walk_wild_section (lang_wild_statement_type *ptr, | |
665 | lang_input_statement_type *file, | |
666 | callback_t callback, | |
667 | void *data) | |
668 | { | |
669 | if (file->just_syms_flag) | |
670 | return; | |
671 | ||
672 | (*ptr->walk_wild_section_handler) (ptr, file, callback, data); | |
673 | } | |
674 | ||
675 | /* Returns TRUE when name1 is a wildcard spec that might match | |
676 | something name2 can match. We're conservative: we return FALSE | |
677 | only if the prefixes of name1 and name2 are different up to the | |
678 | first wildcard character. */ | |
679 | ||
680 | static bfd_boolean | |
681 | wild_spec_can_overlap (const char *name1, const char *name2) | |
682 | { | |
683 | size_t prefix1_len = strcspn (name1, "?*["); | |
684 | size_t prefix2_len = strcspn (name2, "?*["); | |
685 | size_t min_prefix_len; | |
686 | ||
687 | /* Note that if there is no wildcard character, then we treat the | |
688 | terminating 0 as part of the prefix. Thus ".text" won't match | |
689 | ".text." or ".text.*", for example. */ | |
690 | if (name1[prefix1_len] == '\0') | |
691 | prefix1_len++; | |
692 | if (name2[prefix2_len] == '\0') | |
693 | prefix2_len++; | |
694 | ||
695 | min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len; | |
696 | ||
697 | return memcmp (name1, name2, min_prefix_len) == 0; | |
698 | } | |
699 | ||
700 | /* Select specialized code to handle various kinds of wildcard | |
701 | statements. */ | |
702 | ||
703 | static void | |
704 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
705 | { | |
706 | int sec_count = 0; | |
707 | int wild_name_count = 0; | |
708 | struct wildcard_list *sec; | |
709 | int signature; | |
710 | int data_counter; | |
711 | ||
712 | ptr->walk_wild_section_handler = walk_wild_section_general; | |
7544697a AM |
713 | ptr->handler_data[0] = NULL; |
714 | ptr->handler_data[1] = NULL; | |
715 | ptr->handler_data[2] = NULL; | |
716 | ptr->handler_data[3] = NULL; | |
e6f2cbf5 | 717 | ptr->tree = NULL; |
72223188 JJ |
718 | |
719 | /* Count how many wildcard_specs there are, and how many of those | |
720 | actually use wildcards in the name. Also, bail out if any of the | |
721 | wildcard names are NULL. (Can this actually happen? | |
722 | walk_wild_section used to test for it.) And bail out if any | |
723 | of the wildcards are more complex than a simple string | |
724 | ending in a single '*'. */ | |
725 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
726 | { | |
727 | ++sec_count; | |
728 | if (sec->spec.name == NULL) | |
729 | return; | |
730 | if (wildcardp (sec->spec.name)) | |
731 | { | |
732 | ++wild_name_count; | |
733 | if (!is_simple_wild (sec->spec.name)) | |
734 | return; | |
735 | } | |
736 | } | |
737 | ||
738 | /* The zero-spec case would be easy to optimize but it doesn't | |
739 | happen in practice. Likewise, more than 4 specs doesn't | |
740 | happen in practice. */ | |
741 | if (sec_count == 0 || sec_count > 4) | |
742 | return; | |
743 | ||
744 | /* Check that no two specs can match the same section. */ | |
745 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
746 | { | |
747 | struct wildcard_list *sec2; | |
748 | for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next) | |
749 | { | |
750 | if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name)) | |
751 | return; | |
752 | } | |
753 | } | |
754 | ||
755 | signature = (sec_count << 8) + wild_name_count; | |
756 | switch (signature) | |
757 | { | |
758 | case 0x0100: | |
759 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0; | |
760 | break; | |
761 | case 0x0101: | |
762 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1; | |
763 | break; | |
764 | case 0x0201: | |
765 | ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1; | |
766 | break; | |
767 | case 0x0302: | |
768 | ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2; | |
769 | break; | |
770 | case 0x0402: | |
771 | ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2; | |
772 | break; | |
773 | default: | |
774 | return; | |
775 | } | |
776 | ||
777 | /* Now fill the data array with pointers to the specs, first the | |
778 | specs with non-wildcard names, then the specs with wildcard | |
779 | names. It's OK to process the specs in different order from the | |
780 | given order, because we've already determined that no section | |
781 | will match more than one spec. */ | |
782 | data_counter = 0; | |
783 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
784 | if (!wildcardp (sec->spec.name)) | |
785 | ptr->handler_data[data_counter++] = sec; | |
786 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
787 | if (wildcardp (sec->spec.name)) | |
788 | ptr->handler_data[data_counter++] = sec; | |
789 | } | |
790 | ||
4dec4d4e RH |
791 | /* Handle a wild statement for a single file F. */ |
792 | ||
793 | static void | |
1579bae1 AM |
794 | walk_wild_file (lang_wild_statement_type *s, |
795 | lang_input_statement_type *f, | |
796 | callback_t callback, | |
797 | void *data) | |
4dec4d4e RH |
798 | { |
799 | if (f->the_bfd == NULL | |
800 | || ! bfd_check_format (f->the_bfd, bfd_archive)) | |
b6bf44ba | 801 | walk_wild_section (s, f, callback, data); |
4dec4d4e RH |
802 | else |
803 | { | |
804 | bfd *member; | |
805 | ||
806 | /* This is an archive file. We must map each member of the | |
807 | archive separately. */ | |
1579bae1 | 808 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
4dec4d4e RH |
809 | while (member != NULL) |
810 | { | |
811 | /* When lookup_name is called, it will call the add_symbols | |
812 | entry point for the archive. For each element of the | |
813 | archive which is included, BFD will call ldlang_add_file, | |
814 | which will set the usrdata field of the member to the | |
815 | lang_input_statement. */ | |
816 | if (member->usrdata != NULL) | |
817 | { | |
1579bae1 | 818 | walk_wild_section (s, member->usrdata, callback, data); |
4dec4d4e RH |
819 | } |
820 | ||
821 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
826 | static void | |
1579bae1 | 827 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
4dec4d4e | 828 | { |
b6bf44ba | 829 | const char *file_spec = s->filename; |
97407faf | 830 | char *p; |
b6bf44ba AM |
831 | |
832 | if (file_spec == NULL) | |
4dec4d4e RH |
833 | { |
834 | /* Perform the iteration over all files in the list. */ | |
e50d8076 | 835 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 836 | { |
b6bf44ba | 837 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
838 | } |
839 | } | |
97407faf AM |
840 | else if ((p = archive_path (file_spec)) != NULL) |
841 | { | |
842 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
843 | { | |
967928e9 AM |
844 | if (input_statement_is_archive_path (file_spec, p, f)) |
845 | walk_wild_file (s, f, callback, data); | |
97407faf AM |
846 | } |
847 | } | |
b6bf44ba | 848 | else if (wildcardp (file_spec)) |
4dec4d4e | 849 | { |
e50d8076 | 850 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 851 | { |
68bbb9f7 | 852 | if (fnmatch (file_spec, f->filename, 0) == 0) |
b6bf44ba | 853 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
854 | } |
855 | } | |
856 | else | |
857 | { | |
e50d8076 NC |
858 | lang_input_statement_type *f; |
859 | ||
4dec4d4e | 860 | /* Perform the iteration over a single file. */ |
b6bf44ba | 861 | f = lookup_name (file_spec); |
6770ec8c | 862 | if (f) |
b6bf44ba | 863 | walk_wild_file (s, f, callback, data); |
4dec4d4e | 864 | } |
5f992e62 AM |
865 | } |
866 | ||
08da4cac KH |
867 | /* lang_for_each_statement walks the parse tree and calls the provided |
868 | function for each node. */ | |
252b5132 RH |
869 | |
870 | static void | |
1579bae1 AM |
871 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
872 | lang_statement_union_type *s) | |
252b5132 | 873 | { |
1579bae1 | 874 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
875 | { |
876 | func (s); | |
877 | ||
878 | switch (s->header.type) | |
879 | { | |
880 | case lang_constructors_statement_enum: | |
881 | lang_for_each_statement_worker (func, constructor_list.head); | |
882 | break; | |
883 | case lang_output_section_statement_enum: | |
884 | lang_for_each_statement_worker | |
6feb9908 | 885 | (func, s->output_section_statement.children.head); |
252b5132 RH |
886 | break; |
887 | case lang_wild_statement_enum: | |
6feb9908 AM |
888 | lang_for_each_statement_worker (func, |
889 | s->wild_statement.children.head); | |
252b5132 RH |
890 | break; |
891 | case lang_group_statement_enum: | |
892 | lang_for_each_statement_worker (func, | |
893 | s->group_statement.children.head); | |
894 | break; | |
895 | case lang_data_statement_enum: | |
896 | case lang_reloc_statement_enum: | |
897 | case lang_object_symbols_statement_enum: | |
898 | case lang_output_statement_enum: | |
899 | case lang_target_statement_enum: | |
900 | case lang_input_section_enum: | |
901 | case lang_input_statement_enum: | |
902 | case lang_assignment_statement_enum: | |
903 | case lang_padding_statement_enum: | |
904 | case lang_address_statement_enum: | |
905 | case lang_fill_statement_enum: | |
53d25da6 | 906 | case lang_insert_statement_enum: |
252b5132 RH |
907 | break; |
908 | default: | |
909 | FAIL (); | |
910 | break; | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | void | |
1579bae1 | 916 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 917 | { |
08da4cac | 918 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
919 | } |
920 | ||
921 | /*----------------------------------------------------------------------*/ | |
08da4cac | 922 | |
252b5132 | 923 | void |
1579bae1 | 924 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 925 | { |
1579bae1 | 926 | list->head = NULL; |
252b5132 RH |
927 | list->tail = &list->head; |
928 | } | |
929 | ||
bde18da4 AM |
930 | void |
931 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
932 | { | |
933 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
934 | abort (); | |
935 | *stat_save_ptr++ = stat_ptr; | |
936 | stat_ptr = new_ptr; | |
937 | } | |
938 | ||
939 | void | |
940 | pop_stat_ptr (void) | |
941 | { | |
942 | if (stat_save_ptr <= stat_save) | |
943 | abort (); | |
944 | stat_ptr = *--stat_save_ptr; | |
945 | } | |
946 | ||
08da4cac | 947 | /* Build a new statement node for the parse tree. */ |
252b5132 | 948 | |
08da4cac | 949 | static lang_statement_union_type * |
1579bae1 AM |
950 | new_statement (enum statement_enum type, |
951 | size_t size, | |
952 | lang_statement_list_type *list) | |
252b5132 | 953 | { |
1579bae1 | 954 | lang_statement_union_type *new; |
252b5132 | 955 | |
1579bae1 | 956 | new = stat_alloc (size); |
252b5132 | 957 | new->header.type = type; |
1579bae1 | 958 | new->header.next = NULL; |
252b5132 RH |
959 | lang_statement_append (list, new, &new->header.next); |
960 | return new; | |
961 | } | |
962 | ||
08da4cac KH |
963 | /* Build a new input file node for the language. There are several |
964 | ways in which we treat an input file, eg, we only look at symbols, | |
965 | or prefix it with a -l etc. | |
252b5132 | 966 | |
08da4cac | 967 | We can be supplied with requests for input files more than once; |
396a2467 | 968 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 969 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 970 | got it already so we don't duplicate the bfd. */ |
252b5132 | 971 | |
252b5132 | 972 | static lang_input_statement_type * |
1579bae1 AM |
973 | new_afile (const char *name, |
974 | lang_input_file_enum_type file_type, | |
975 | const char *target, | |
976 | bfd_boolean add_to_list) | |
252b5132 RH |
977 | { |
978 | lang_input_statement_type *p; | |
979 | ||
980 | if (add_to_list) | |
981 | p = new_stat (lang_input_statement, stat_ptr); | |
982 | else | |
983 | { | |
1579bae1 | 984 | p = stat_alloc (sizeof (lang_input_statement_type)); |
bd4d42c1 | 985 | p->header.type = lang_input_statement_enum; |
252b5132 RH |
986 | p->header.next = NULL; |
987 | } | |
988 | ||
b34976b6 | 989 | lang_has_input_file = TRUE; |
252b5132 | 990 | p->target = target; |
e3f2db7f | 991 | p->sysrooted = FALSE; |
bcb674cf RS |
992 | |
993 | if (file_type == lang_input_file_is_l_enum | |
994 | && name[0] == ':' && name[1] != '\0') | |
995 | { | |
996 | file_type = lang_input_file_is_search_file_enum; | |
997 | name = name + 1; | |
998 | } | |
999 | ||
252b5132 RH |
1000 | switch (file_type) |
1001 | { | |
1002 | case lang_input_file_is_symbols_only_enum: | |
1003 | p->filename = name; | |
b34976b6 AM |
1004 | p->is_archive = FALSE; |
1005 | p->real = TRUE; | |
252b5132 | 1006 | p->local_sym_name = name; |
b34976b6 AM |
1007 | p->just_syms_flag = TRUE; |
1008 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
1009 | break; |
1010 | case lang_input_file_is_fake_enum: | |
1011 | p->filename = name; | |
b34976b6 AM |
1012 | p->is_archive = FALSE; |
1013 | p->real = FALSE; | |
252b5132 | 1014 | p->local_sym_name = name; |
b34976b6 AM |
1015 | p->just_syms_flag = FALSE; |
1016 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
1017 | break; |
1018 | case lang_input_file_is_l_enum: | |
b34976b6 | 1019 | p->is_archive = TRUE; |
252b5132 | 1020 | p->filename = name; |
b34976b6 | 1021 | p->real = TRUE; |
ff7a0acf | 1022 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
b34976b6 AM |
1023 | p->just_syms_flag = FALSE; |
1024 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
1025 | break; |
1026 | case lang_input_file_is_marker_enum: | |
1027 | p->filename = name; | |
b34976b6 AM |
1028 | p->is_archive = FALSE; |
1029 | p->real = FALSE; | |
252b5132 | 1030 | p->local_sym_name = name; |
b34976b6 AM |
1031 | p->just_syms_flag = FALSE; |
1032 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
1033 | break; |
1034 | case lang_input_file_is_search_file_enum: | |
e3f2db7f | 1035 | p->sysrooted = ldlang_sysrooted_script; |
252b5132 | 1036 | p->filename = name; |
b34976b6 AM |
1037 | p->is_archive = FALSE; |
1038 | p->real = TRUE; | |
252b5132 | 1039 | p->local_sym_name = name; |
b34976b6 AM |
1040 | p->just_syms_flag = FALSE; |
1041 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
1042 | break; |
1043 | case lang_input_file_is_file_enum: | |
1044 | p->filename = name; | |
b34976b6 AM |
1045 | p->is_archive = FALSE; |
1046 | p->real = TRUE; | |
252b5132 | 1047 | p->local_sym_name = name; |
b34976b6 AM |
1048 | p->just_syms_flag = FALSE; |
1049 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
1050 | break; |
1051 | default: | |
1052 | FAIL (); | |
1053 | } | |
1579bae1 | 1054 | p->the_bfd = NULL; |
1579bae1 AM |
1055 | p->next_real_file = NULL; |
1056 | p->next = NULL; | |
252b5132 | 1057 | p->dynamic = config.dynamic_link; |
e56f61be | 1058 | p->add_needed = add_needed; |
4a43e768 | 1059 | p->as_needed = as_needed; |
252b5132 | 1060 | p->whole_archive = whole_archive; |
b34976b6 | 1061 | p->loaded = FALSE; |
252b5132 RH |
1062 | lang_statement_append (&input_file_chain, |
1063 | (lang_statement_union_type *) p, | |
1064 | &p->next_real_file); | |
1065 | return p; | |
1066 | } | |
1067 | ||
1068 | lang_input_statement_type * | |
1579bae1 AM |
1069 | lang_add_input_file (const char *name, |
1070 | lang_input_file_enum_type file_type, | |
1071 | const char *target) | |
252b5132 | 1072 | { |
b34976b6 | 1073 | return new_afile (name, file_type, target, TRUE); |
252b5132 RH |
1074 | } |
1075 | ||
409d7240 | 1076 | struct out_section_hash_entry |
750877ba L |
1077 | { |
1078 | struct bfd_hash_entry root; | |
409d7240 | 1079 | lang_statement_union_type s; |
750877ba L |
1080 | }; |
1081 | ||
1082 | /* The hash table. */ | |
1083 | ||
409d7240 | 1084 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1085 | |
384a9dda | 1086 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1087 | initialize the table, fill in an entry and remove the table. */ |
1088 | ||
1089 | static struct bfd_hash_entry * | |
329c1c86 | 1090 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1091 | struct bfd_hash_table *table, |
1092 | const char *string) | |
750877ba | 1093 | { |
384a9dda | 1094 | lang_output_section_statement_type **nextp; |
409d7240 | 1095 | struct out_section_hash_entry *ret; |
384a9dda AM |
1096 | |
1097 | if (entry == NULL) | |
1098 | { | |
1099 | entry = bfd_hash_allocate (table, sizeof (*ret)); | |
1100 | if (entry == NULL) | |
1101 | return entry; | |
1102 | } | |
1103 | ||
1104 | entry = bfd_hash_newfunc (entry, table, string); | |
1105 | if (entry == NULL) | |
1106 | return entry; | |
1107 | ||
409d7240 AM |
1108 | ret = (struct out_section_hash_entry *) entry; |
1109 | memset (&ret->s, 0, sizeof (ret->s)); | |
1110 | ret->s.header.type = lang_output_section_statement_enum; | |
1111 | ret->s.output_section_statement.subsection_alignment = -1; | |
1112 | ret->s.output_section_statement.section_alignment = -1; | |
1113 | ret->s.output_section_statement.block_value = 1; | |
1114 | lang_list_init (&ret->s.output_section_statement.children); | |
1115 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1116 | |
218868ba AM |
1117 | /* For every output section statement added to the list, except the |
1118 | first one, lang_output_section_statement.tail points to the "next" | |
1119 | field of the last element of the list. */ | |
1120 | if (lang_output_section_statement.head != NULL) | |
409d7240 AM |
1121 | ret->s.output_section_statement.prev |
1122 | = ((lang_output_section_statement_type *) | |
1123 | ((char *) lang_output_section_statement.tail | |
1124 | - offsetof (lang_output_section_statement_type, next))); | |
218868ba | 1125 | |
384a9dda AM |
1126 | /* GCC's strict aliasing rules prevent us from just casting the |
1127 | address, so we store the pointer in a variable and cast that | |
1128 | instead. */ | |
409d7240 | 1129 | nextp = &ret->s.output_section_statement.next; |
384a9dda | 1130 | lang_statement_append (&lang_output_section_statement, |
409d7240 | 1131 | &ret->s, |
384a9dda AM |
1132 | (lang_statement_union_type **) nextp); |
1133 | return &ret->root; | |
750877ba L |
1134 | } |
1135 | ||
1136 | static void | |
409d7240 | 1137 | output_section_statement_table_init (void) |
750877ba | 1138 | { |
409d7240 AM |
1139 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1140 | output_section_statement_newfunc, | |
1141 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1142 | 61)) |
384a9dda | 1143 | einfo (_("%P%F: can not create hash table: %E\n")); |
750877ba L |
1144 | } |
1145 | ||
1146 | static void | |
409d7240 | 1147 | output_section_statement_table_free (void) |
750877ba | 1148 | { |
409d7240 | 1149 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1150 | } |
1151 | ||
08da4cac KH |
1152 | /* Build enough state so that the parser can build its tree. */ |
1153 | ||
252b5132 | 1154 | void |
1579bae1 | 1155 | lang_init (void) |
252b5132 RH |
1156 | { |
1157 | obstack_begin (&stat_obstack, 1000); | |
1158 | ||
1159 | stat_ptr = &statement_list; | |
1160 | ||
409d7240 | 1161 | output_section_statement_table_init (); |
750877ba | 1162 | |
252b5132 RH |
1163 | lang_list_init (stat_ptr); |
1164 | ||
1165 | lang_list_init (&input_file_chain); | |
1166 | lang_list_init (&lang_output_section_statement); | |
1167 | lang_list_init (&file_chain); | |
1579bae1 AM |
1168 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1169 | NULL); | |
08da4cac | 1170 | abs_output_section = |
66c103b7 | 1171 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE); |
252b5132 RH |
1172 | |
1173 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c HPN |
1174 | |
1175 | /* The value "3" is ad-hoc, somewhat related to the expected number of | |
1176 | DEFINED expressions in a linker script. For most default linker | |
1177 | scripts, there are none. Why a hash table then? Well, it's somewhat | |
1178 | simpler to re-use working machinery than using a linked list in terms | |
1179 | of code-complexity here in ld, besides the initialization which just | |
1180 | looks like other code here. */ | |
688c58f3 | 1181 | if (!bfd_hash_table_init_n (&lang_definedness_table, |
66eb6687 AM |
1182 | lang_definedness_newfunc, |
1183 | sizeof (struct lang_definedness_hash_entry), | |
1184 | 3)) | |
384a9dda | 1185 | einfo (_("%P%F: can not create hash table: %E\n")); |
252b5132 RH |
1186 | } |
1187 | ||
750877ba L |
1188 | void |
1189 | lang_finish (void) | |
1190 | { | |
409d7240 | 1191 | output_section_statement_table_free (); |
750877ba L |
1192 | } |
1193 | ||
252b5132 | 1194 | /*---------------------------------------------------------------------- |
08da4cac KH |
1195 | A region is an area of memory declared with the |
1196 | MEMORY { name:org=exp, len=exp ... } | |
1197 | syntax. | |
252b5132 | 1198 | |
08da4cac | 1199 | We maintain a list of all the regions here. |
252b5132 | 1200 | |
08da4cac | 1201 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1202 | which is created when looked up to be the entire data space. |
1203 | ||
1204 | If create is true we are creating a region inside a MEMORY block. | |
1205 | In this case it is probably an error to create a region that has | |
1206 | already been created. If we are not inside a MEMORY block it is | |
1207 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 NC |
1208 | and so we issue a warning. |
1209 | ||
1210 | Each region has at least one name. The first name is either | |
1211 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1212 | alias names to an existing region within a script with | |
1213 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1214 | region. */ | |
252b5132 RH |
1215 | |
1216 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1217 | static lang_memory_region_type **lang_memory_region_list_tail |
1218 | = &lang_memory_region_list; | |
252b5132 RH |
1219 | |
1220 | lang_memory_region_type * | |
a747ee4d | 1221 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
252b5132 | 1222 | { |
4a93e180 NC |
1223 | lang_memory_region_name *n; |
1224 | lang_memory_region_type *r; | |
1579bae1 | 1225 | lang_memory_region_type *new; |
252b5132 | 1226 | |
9f88b410 RS |
1227 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1228 | if (name == NULL) | |
1229 | return NULL; | |
1230 | ||
4a93e180 NC |
1231 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1232 | for (n = &r->name_list; n != NULL; n = n->next) | |
1233 | if (strcmp (n->name, name) == 0) | |
1234 | { | |
1235 | if (create) | |
1236 | einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"), | |
1237 | name); | |
1238 | return r; | |
1239 | } | |
252b5132 | 1240 | |
a747ee4d | 1241 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
4a93e180 | 1242 | einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name); |
a747ee4d | 1243 | |
1579bae1 | 1244 | new = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1245 | |
4a93e180 NC |
1246 | new->name_list.name = xstrdup (name); |
1247 | new->name_list.next = NULL; | |
1579bae1 | 1248 | new->next = NULL; |
1579bae1 | 1249 | new->origin = 0; |
1579bae1 AM |
1250 | new->length = ~(bfd_size_type) 0; |
1251 | new->current = 0; | |
66e28d60 AM |
1252 | new->last_os = NULL; |
1253 | new->flags = 0; | |
1254 | new->not_flags = 0; | |
1579bae1 | 1255 | new->had_full_message = FALSE; |
252b5132 | 1256 | |
66e28d60 AM |
1257 | *lang_memory_region_list_tail = new; |
1258 | lang_memory_region_list_tail = &new->next; | |
1259 | ||
1579bae1 | 1260 | return new; |
252b5132 RH |
1261 | } |
1262 | ||
4a93e180 NC |
1263 | void |
1264 | lang_memory_region_alias (const char * alias, const char * region_name) | |
1265 | { | |
1266 | lang_memory_region_name * n; | |
1267 | lang_memory_region_type * r; | |
1268 | lang_memory_region_type * region; | |
1269 | ||
1270 | /* The default region must be unique. This ensures that it is not necessary | |
1271 | to iterate through the name list if someone wants the check if a region is | |
1272 | the default memory region. */ | |
1273 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1274 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
1275 | einfo (_("%F%P:%S: error: alias for default memory region\n")); | |
1276 | ||
1277 | /* Look for the target region and check if the alias is not already | |
1278 | in use. */ | |
1279 | region = NULL; | |
1280 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1281 | for (n = &r->name_list; n != NULL; n = n->next) | |
1282 | { | |
1283 | if (region == NULL && strcmp (n->name, region_name) == 0) | |
1284 | region = r; | |
1285 | if (strcmp (n->name, alias) == 0) | |
1286 | einfo (_("%F%P:%S: error: redefinition of memory region " | |
1287 | "alias `%s'\n"), | |
1288 | alias); | |
1289 | } | |
1290 | ||
1291 | /* Check if the target region exists. */ | |
1292 | if (region == NULL) | |
1293 | einfo (_("%F%P:%S: error: memory region `%s' " | |
1294 | "for alias `%s' does not exist\n"), | |
1295 | region_name, | |
1296 | alias); | |
1297 | ||
1298 | /* Add alias to region name list. */ | |
1299 | n = stat_alloc (sizeof (lang_memory_region_name)); | |
1300 | n->name = xstrdup (alias); | |
1301 | n->next = region->name_list.next; | |
1302 | region->name_list.next = n; | |
1303 | } | |
1304 | ||
5f992e62 | 1305 | static lang_memory_region_type * |
4a93e180 | 1306 | lang_memory_default (asection * section) |
252b5132 RH |
1307 | { |
1308 | lang_memory_region_type *p; | |
1309 | ||
1310 | flagword sec_flags = section->flags; | |
1311 | ||
1312 | /* Override SEC_DATA to mean a writable section. */ | |
1313 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1314 | sec_flags |= SEC_DATA; | |
1315 | ||
1579bae1 | 1316 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1317 | { |
1318 | if ((p->flags & sec_flags) != 0 | |
1319 | && (p->not_flags & sec_flags) == 0) | |
1320 | { | |
1321 | return p; | |
1322 | } | |
1323 | } | |
a747ee4d | 1324 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
252b5132 RH |
1325 | } |
1326 | ||
384a9dda | 1327 | lang_output_section_statement_type * |
66c103b7 AM |
1328 | lang_output_section_statement_lookup (const char *const name, |
1329 | int constraint, | |
1330 | bfd_boolean create) | |
252b5132 | 1331 | { |
409d7240 | 1332 | struct out_section_hash_entry *entry; |
252b5132 | 1333 | |
409d7240 AM |
1334 | entry = ((struct out_section_hash_entry *) |
1335 | bfd_hash_lookup (&output_section_statement_table, name, | |
66c103b7 | 1336 | create, FALSE)); |
384a9dda AM |
1337 | if (entry == NULL) |
1338 | { | |
66c103b7 AM |
1339 | if (create) |
1340 | einfo (_("%P%F: failed creating section `%s': %E\n"), name); | |
384a9dda AM |
1341 | return NULL; |
1342 | } | |
252b5132 | 1343 | |
409d7240 | 1344 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1345 | { |
384a9dda AM |
1346 | /* We have a section of this name, but it might not have the correct |
1347 | constraint. */ | |
66c103b7 AM |
1348 | struct out_section_hash_entry *last_ent; |
1349 | unsigned long hash = entry->root.hash; | |
1350 | ||
8a99a385 AM |
1351 | if (create && constraint == SPECIAL) |
1352 | /* Not traversing to the end reverses the order of the second | |
1353 | and subsequent SPECIAL sections in the hash table chain, | |
1354 | but that shouldn't matter. */ | |
1355 | last_ent = entry; | |
1356 | else | |
1357 | do | |
1358 | { | |
1359 | if (entry->s.output_section_statement.constraint >= 0 | |
1360 | && (constraint == 0 | |
1361 | || (constraint | |
1362 | == entry->s.output_section_statement.constraint))) | |
1363 | return &entry->s.output_section_statement; | |
1364 | last_ent = entry; | |
1365 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1366 | } | |
1367 | while (entry != NULL | |
1368 | && entry->root.hash == hash | |
1369 | && strcmp (name, entry->s.output_section_statement.name) == 0); | |
252b5132 | 1370 | |
66c103b7 AM |
1371 | if (!create) |
1372 | return NULL; | |
1373 | ||
409d7240 AM |
1374 | entry |
1375 | = ((struct out_section_hash_entry *) | |
1376 | output_section_statement_newfunc (NULL, | |
1377 | &output_section_statement_table, | |
1378 | name)); | |
750877ba | 1379 | if (entry == NULL) |
384a9dda AM |
1380 | { |
1381 | einfo (_("%P%F: failed creating section `%s': %E\n"), name); | |
1382 | return NULL; | |
1383 | } | |
1384 | entry->root = last_ent->root; | |
1385 | last_ent->root.next = &entry->root; | |
252b5132 | 1386 | } |
384a9dda | 1387 | |
409d7240 AM |
1388 | entry->s.output_section_statement.name = name; |
1389 | entry->s.output_section_statement.constraint = constraint; | |
1390 | return &entry->s.output_section_statement; | |
252b5132 RH |
1391 | } |
1392 | ||
afd7a018 AM |
1393 | /* A variant of lang_output_section_find used by place_orphan. |
1394 | Returns the output statement that should precede a new output | |
1395 | statement for SEC. If an exact match is found on certain flags, | |
1396 | sets *EXACT too. */ | |
1397 | ||
1398 | lang_output_section_statement_type * | |
1399 | lang_output_section_find_by_flags (const asection *sec, | |
390fbbf1 AM |
1400 | lang_output_section_statement_type **exact, |
1401 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1402 | { |
1403 | lang_output_section_statement_type *first, *look, *found; | |
1404 | flagword flags; | |
1405 | ||
1406 | /* We know the first statement on this list is *ABS*. May as well | |
1407 | skip it. */ | |
1408 | first = &lang_output_section_statement.head->output_section_statement; | |
1409 | first = first->next; | |
1410 | ||
1411 | /* First try for an exact match. */ | |
1412 | found = NULL; | |
1413 | for (look = first; look; look = look->next) | |
1414 | { | |
1415 | flags = look->flags; | |
1416 | if (look->bfd_section != NULL) | |
ecca9871 L |
1417 | { |
1418 | flags = look->bfd_section->flags; | |
f13a99db AM |
1419 | if (match_type && !match_type (link_info.output_bfd, |
1420 | look->bfd_section, | |
390fbbf1 | 1421 | sec->owner, sec)) |
ecca9871 L |
1422 | continue; |
1423 | } | |
afd7a018 AM |
1424 | flags ^= sec->flags; |
1425 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1426 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1427 | found = look; | |
1428 | } | |
1429 | if (found != NULL) | |
1430 | { | |
390fbbf1 AM |
1431 | if (exact != NULL) |
1432 | *exact = found; | |
afd7a018 AM |
1433 | return found; |
1434 | } | |
1435 | ||
07890c07 AM |
1436 | if ((sec->flags & SEC_CODE) != 0 |
1437 | && (sec->flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1438 | { |
1439 | /* Try for a rw code section. */ | |
1440 | for (look = first; look; look = look->next) | |
1441 | { | |
1442 | flags = look->flags; | |
1443 | if (look->bfd_section != NULL) | |
ecca9871 L |
1444 | { |
1445 | flags = look->bfd_section->flags; | |
f13a99db AM |
1446 | if (match_type && !match_type (link_info.output_bfd, |
1447 | look->bfd_section, | |
390fbbf1 | 1448 | sec->owner, sec)) |
ecca9871 L |
1449 | continue; |
1450 | } | |
afd7a018 AM |
1451 | flags ^= sec->flags; |
1452 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1453 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1454 | found = look; | |
1455 | } | |
afd7a018 | 1456 | } |
07890c07 AM |
1457 | else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0 |
1458 | && (sec->flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1459 | { |
1460 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1461 | for (look = first; look; look = look->next) | |
1462 | { | |
1463 | flags = look->flags; | |
1464 | if (look->bfd_section != NULL) | |
ecca9871 L |
1465 | { |
1466 | flags = look->bfd_section->flags; | |
f13a99db AM |
1467 | if (match_type && !match_type (link_info.output_bfd, |
1468 | look->bfd_section, | |
390fbbf1 | 1469 | sec->owner, sec)) |
ecca9871 L |
1470 | continue; |
1471 | } | |
afd7a018 AM |
1472 | flags ^= sec->flags; |
1473 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1474 | | SEC_READONLY)) | |
1475 | && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1476 | found = look; | |
1477 | } | |
afd7a018 | 1478 | } |
07890c07 AM |
1479 | else if ((sec->flags & SEC_SMALL_DATA) != 0 |
1480 | && (sec->flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1481 | { |
1482 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1483 | for (look = first; look; look = look->next) | |
1484 | { | |
1485 | flags = look->flags; | |
1486 | if (look->bfd_section != NULL) | |
ecca9871 L |
1487 | { |
1488 | flags = look->bfd_section->flags; | |
f13a99db AM |
1489 | if (match_type && !match_type (link_info.output_bfd, |
1490 | look->bfd_section, | |
390fbbf1 | 1491 | sec->owner, sec)) |
ecca9871 L |
1492 | continue; |
1493 | } | |
afd7a018 AM |
1494 | flags ^= sec->flags; |
1495 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1496 | | SEC_THREAD_LOCAL)) | |
1497 | || ((look->flags & SEC_SMALL_DATA) | |
1498 | && !(sec->flags & SEC_HAS_CONTENTS))) | |
1499 | found = look; | |
1500 | } | |
afd7a018 | 1501 | } |
07890c07 AM |
1502 | else if ((sec->flags & SEC_HAS_CONTENTS) != 0 |
1503 | && (sec->flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1504 | { |
1505 | /* .data goes after .rodata. */ | |
1506 | for (look = first; look; look = look->next) | |
1507 | { | |
1508 | flags = look->flags; | |
1509 | if (look->bfd_section != NULL) | |
ecca9871 L |
1510 | { |
1511 | flags = look->bfd_section->flags; | |
f13a99db AM |
1512 | if (match_type && !match_type (link_info.output_bfd, |
1513 | look->bfd_section, | |
390fbbf1 | 1514 | sec->owner, sec)) |
ecca9871 L |
1515 | continue; |
1516 | } | |
afd7a018 AM |
1517 | flags ^= sec->flags; |
1518 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1519 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1520 | found = look; | |
1521 | } | |
afd7a018 | 1522 | } |
07890c07 | 1523 | else if ((sec->flags & SEC_ALLOC) != 0) |
afd7a018 | 1524 | { |
07890c07 | 1525 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1526 | for (look = first; look; look = look->next) |
ecca9871 | 1527 | { |
390fbbf1 AM |
1528 | flags = look->flags; |
1529 | if (look->bfd_section != NULL) | |
1530 | { | |
1531 | flags = look->bfd_section->flags; | |
f13a99db AM |
1532 | if (match_type && !match_type (link_info.output_bfd, |
1533 | look->bfd_section, | |
390fbbf1 AM |
1534 | sec->owner, sec)) |
1535 | continue; | |
1536 | } | |
1537 | flags ^= sec->flags; | |
1538 | if (!(flags & SEC_ALLOC)) | |
1539 | found = look; | |
ecca9871 | 1540 | } |
afd7a018 | 1541 | } |
07890c07 AM |
1542 | else |
1543 | { | |
1544 | /* non-alloc go last. */ | |
1545 | for (look = first; look; look = look->next) | |
1546 | { | |
1547 | flags = look->flags; | |
1548 | if (look->bfd_section != NULL) | |
1549 | flags = look->bfd_section->flags; | |
1550 | flags ^= sec->flags; | |
1551 | if (!(flags & SEC_DEBUGGING)) | |
1552 | found = look; | |
1553 | } | |
1554 | return found; | |
1555 | } | |
afd7a018 | 1556 | |
390fbbf1 AM |
1557 | if (found || !match_type) |
1558 | return found; | |
1559 | ||
1560 | return lang_output_section_find_by_flags (sec, NULL, NULL); | |
afd7a018 AM |
1561 | } |
1562 | ||
1563 | /* Find the last output section before given output statement. | |
1564 | Used by place_orphan. */ | |
1565 | ||
1566 | static asection * | |
1567 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1568 | { | |
afd7a018 AM |
1569 | lang_output_section_statement_type *lookup; |
1570 | ||
218868ba | 1571 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1572 | { |
66c103b7 | 1573 | if (lookup->constraint < 0) |
afd7a018 | 1574 | continue; |
afd7a018 AM |
1575 | |
1576 | if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) | |
218868ba | 1577 | return lookup->bfd_section; |
afd7a018 AM |
1578 | } |
1579 | ||
1580 | return NULL; | |
1581 | } | |
1582 | ||
53d25da6 AM |
1583 | /* Look for a suitable place for a new output section statement. The |
1584 | idea is to skip over anything that might be inside a SECTIONS {} | |
1585 | statement in a script, before we find another output section | |
1586 | statement. Assignments to "dot" before an output section statement | |
1587 | are assumed to belong to it. An exception to this rule is made for | |
1588 | the first assignment to dot, otherwise we might put an orphan | |
1589 | before . = . + SIZEOF_HEADERS or similar assignments that set the | |
1590 | initial address. */ | |
1591 | ||
1592 | static lang_statement_union_type ** | |
1593 | insert_os_after (lang_output_section_statement_type *after) | |
1594 | { | |
1595 | lang_statement_union_type **where; | |
1596 | lang_statement_union_type **assign = NULL; | |
1597 | bfd_boolean ignore_first; | |
1598 | ||
1599 | ignore_first | |
1600 | = after == &lang_output_section_statement.head->output_section_statement; | |
1601 | ||
1602 | for (where = &after->header.next; | |
1603 | *where != NULL; | |
1604 | where = &(*where)->header.next) | |
1605 | { | |
1606 | switch ((*where)->header.type) | |
1607 | { | |
1608 | case lang_assignment_statement_enum: | |
1609 | if (assign == NULL) | |
1610 | { | |
1611 | lang_assignment_statement_type *ass; | |
1612 | ||
1613 | ass = &(*where)->assignment_statement; | |
1614 | if (ass->exp->type.node_class != etree_assert | |
1615 | && ass->exp->assign.dst[0] == '.' | |
1616 | && ass->exp->assign.dst[1] == 0 | |
1617 | && !ignore_first) | |
1618 | assign = where; | |
1619 | } | |
1620 | ignore_first = FALSE; | |
1621 | continue; | |
1622 | case lang_wild_statement_enum: | |
1623 | case lang_input_section_enum: | |
1624 | case lang_object_symbols_statement_enum: | |
1625 | case lang_fill_statement_enum: | |
1626 | case lang_data_statement_enum: | |
1627 | case lang_reloc_statement_enum: | |
1628 | case lang_padding_statement_enum: | |
1629 | case lang_constructors_statement_enum: | |
1630 | assign = NULL; | |
1631 | continue; | |
1632 | case lang_output_section_statement_enum: | |
1633 | if (assign != NULL) | |
1634 | where = assign; | |
1635 | break; | |
1636 | case lang_input_statement_enum: | |
1637 | case lang_address_statement_enum: | |
1638 | case lang_target_statement_enum: | |
1639 | case lang_output_statement_enum: | |
1640 | case lang_group_statement_enum: | |
1641 | case lang_insert_statement_enum: | |
1642 | continue; | |
1643 | } | |
1644 | break; | |
1645 | } | |
1646 | ||
1647 | return where; | |
1648 | } | |
1649 | ||
afd7a018 | 1650 | lang_output_section_statement_type * |
7b986e99 | 1651 | lang_insert_orphan (asection *s, |
afd7a018 | 1652 | const char *secname, |
8a99a385 | 1653 | int constraint, |
afd7a018 AM |
1654 | lang_output_section_statement_type *after, |
1655 | struct orphan_save *place, | |
1656 | etree_type *address, | |
1657 | lang_statement_list_type *add_child) | |
1658 | { | |
afd7a018 AM |
1659 | lang_statement_list_type add; |
1660 | const char *ps; | |
afd7a018 AM |
1661 | lang_output_section_statement_type *os; |
1662 | lang_output_section_statement_type **os_tail; | |
1663 | ||
afd7a018 AM |
1664 | /* If we have found an appropriate place for the output section |
1665 | statements for this orphan, add them to our own private list, | |
1666 | inserting them later into the global statement list. */ | |
1667 | if (after != NULL) | |
1668 | { | |
bde18da4 AM |
1669 | lang_list_init (&add); |
1670 | push_stat_ptr (&add); | |
afd7a018 AM |
1671 | } |
1672 | ||
1673 | ps = NULL; | |
1674 | if (config.build_constructors) | |
1675 | { | |
1676 | /* If the name of the section is representable in C, then create | |
1677 | symbols to mark the start and the end of the section. */ | |
1678 | for (ps = secname; *ps != '\0'; ps++) | |
1679 | if (! ISALNUM ((unsigned char) *ps) && *ps != '_') | |
1680 | break; | |
1681 | if (*ps == '\0') | |
1682 | { | |
1683 | char *symname; | |
1684 | etree_type *e_align; | |
1685 | ||
1686 | symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1); | |
f13a99db | 1687 | symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd); |
afd7a018 AM |
1688 | sprintf (symname + (symname[0] != 0), "__start_%s", secname); |
1689 | e_align = exp_unop (ALIGN_K, | |
1690 | exp_intop ((bfd_vma) 1 << s->alignment_power)); | |
1691 | lang_add_assignment (exp_assop ('=', ".", e_align)); | |
34711ca3 AM |
1692 | lang_add_assignment (exp_provide (symname, |
1693 | exp_nameop (NAME, "."), | |
1694 | FALSE)); | |
afd7a018 AM |
1695 | } |
1696 | } | |
1697 | ||
1698 | if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
1699 | address = exp_intop (0); | |
1700 | ||
afd7a018 AM |
1701 | os_tail = ((lang_output_section_statement_type **) |
1702 | lang_output_section_statement.tail); | |
1703 | os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL, | |
8a99a385 | 1704 | NULL, constraint); |
afd7a018 AM |
1705 | |
1706 | if (add_child == NULL) | |
1707 | add_child = &os->children; | |
7b986e99 | 1708 | lang_add_section (add_child, s, os); |
afd7a018 AM |
1709 | |
1710 | lang_leave_output_section_statement (0, "*default*", NULL, NULL); | |
1711 | ||
1712 | if (config.build_constructors && *ps == '\0') | |
1713 | { | |
1714 | char *symname; | |
1715 | ||
afd7a018 | 1716 | symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1); |
f13a99db | 1717 | symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd); |
afd7a018 | 1718 | sprintf (symname + (symname[0] != 0), "__stop_%s", secname); |
34711ca3 AM |
1719 | lang_add_assignment (exp_provide (symname, |
1720 | exp_nameop (NAME, "."), | |
1721 | FALSE)); | |
afd7a018 AM |
1722 | } |
1723 | ||
1724 | /* Restore the global list pointer. */ | |
1725 | if (after != NULL) | |
bde18da4 | 1726 | pop_stat_ptr (); |
afd7a018 AM |
1727 | |
1728 | if (after != NULL && os->bfd_section != NULL) | |
1729 | { | |
5daa8fe7 | 1730 | asection *snew, *as; |
afd7a018 AM |
1731 | |
1732 | snew = os->bfd_section; | |
1733 | ||
1734 | /* Shuffle the bfd section list to make the output file look | |
1735 | neater. This is really only cosmetic. */ | |
1736 | if (place->section == NULL | |
1737 | && after != (&lang_output_section_statement.head | |
1738 | ->output_section_statement)) | |
1739 | { | |
1740 | asection *bfd_section = after->bfd_section; | |
1741 | ||
1742 | /* If the output statement hasn't been used to place any input | |
1743 | sections (and thus doesn't have an output bfd_section), | |
1744 | look for the closest prior output statement having an | |
1745 | output section. */ | |
1746 | if (bfd_section == NULL) | |
1747 | bfd_section = output_prev_sec_find (after); | |
1748 | ||
1749 | if (bfd_section != NULL && bfd_section != snew) | |
1750 | place->section = &bfd_section->next; | |
1751 | } | |
1752 | ||
1753 | if (place->section == NULL) | |
f13a99db | 1754 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1755 | |
5daa8fe7 | 1756 | as = *place->section; |
5e542ba7 MS |
1757 | |
1758 | if (!as) | |
329c1c86 AM |
1759 | { |
1760 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1761 | |
1762 | /* Unlink the section. */ | |
f13a99db | 1763 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1764 | |
1765 | /* Now tack it back on in the right place. */ | |
f13a99db | 1766 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1767 | } |
5e542ba7 | 1768 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
1769 | { |
1770 | /* Unlink the section. */ | |
f13a99db | 1771 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 1772 | |
5daa8fe7 | 1773 | /* Now tack it back on in the right place. */ |
f13a99db | 1774 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 1775 | } |
afd7a018 AM |
1776 | |
1777 | /* Save the end of this list. Further ophans of this type will | |
1778 | follow the one we've just added. */ | |
1779 | place->section = &snew->next; | |
1780 | ||
1781 | /* The following is non-cosmetic. We try to put the output | |
1782 | statements in some sort of reasonable order here, because they | |
1783 | determine the final load addresses of the orphan sections. | |
1784 | In addition, placing output statements in the wrong order may | |
1785 | require extra segments. For instance, given a typical | |
1786 | situation of all read-only sections placed in one segment and | |
1787 | following that a segment containing all the read-write | |
1788 | sections, we wouldn't want to place an orphan read/write | |
1789 | section before or amongst the read-only ones. */ | |
1790 | if (add.head != NULL) | |
1791 | { | |
1792 | lang_output_section_statement_type *newly_added_os; | |
1793 | ||
1794 | if (place->stmt == NULL) | |
1795 | { | |
53d25da6 | 1796 | lang_statement_union_type **where = insert_os_after (after); |
afd7a018 AM |
1797 | |
1798 | *add.tail = *where; | |
1799 | *where = add.head; | |
1800 | ||
1801 | place->os_tail = &after->next; | |
1802 | } | |
1803 | else | |
1804 | { | |
1805 | /* Put it after the last orphan statement we added. */ | |
1806 | *add.tail = *place->stmt; | |
1807 | *place->stmt = add.head; | |
1808 | } | |
1809 | ||
1810 | /* Fix the global list pointer if we happened to tack our | |
1811 | new list at the tail. */ | |
bde18da4 AM |
1812 | if (*stat_ptr->tail == add.head) |
1813 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
1814 | |
1815 | /* Save the end of this list. */ | |
1816 | place->stmt = add.tail; | |
1817 | ||
1818 | /* Do the same for the list of output section statements. */ | |
1819 | newly_added_os = *os_tail; | |
1820 | *os_tail = NULL; | |
218868ba AM |
1821 | newly_added_os->prev = (lang_output_section_statement_type *) |
1822 | ((char *) place->os_tail | |
1823 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 1824 | newly_added_os->next = *place->os_tail; |
218868ba AM |
1825 | if (newly_added_os->next != NULL) |
1826 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
1827 | *place->os_tail = newly_added_os; |
1828 | place->os_tail = &newly_added_os->next; | |
1829 | ||
1830 | /* Fixing the global list pointer here is a little different. | |
1831 | We added to the list in lang_enter_output_section_statement, | |
1832 | trimmed off the new output_section_statment above when | |
1833 | assigning *os_tail = NULL, but possibly added it back in | |
1834 | the same place when assigning *place->os_tail. */ | |
1835 | if (*os_tail == NULL) | |
1836 | lang_output_section_statement.tail | |
1837 | = (lang_statement_union_type **) os_tail; | |
1838 | } | |
1839 | } | |
1840 | return os; | |
1841 | } | |
1842 | ||
252b5132 | 1843 | static void |
1579bae1 | 1844 | lang_map_flags (flagword flag) |
252b5132 RH |
1845 | { |
1846 | if (flag & SEC_ALLOC) | |
1847 | minfo ("a"); | |
1848 | ||
1849 | if (flag & SEC_CODE) | |
1850 | minfo ("x"); | |
1851 | ||
1852 | if (flag & SEC_READONLY) | |
1853 | minfo ("r"); | |
1854 | ||
1855 | if (flag & SEC_DATA) | |
1856 | minfo ("w"); | |
1857 | ||
1858 | if (flag & SEC_LOAD) | |
1859 | minfo ("l"); | |
1860 | } | |
1861 | ||
1862 | void | |
1579bae1 | 1863 | lang_map (void) |
252b5132 RH |
1864 | { |
1865 | lang_memory_region_type *m; | |
4d4920ec | 1866 | bfd_boolean dis_header_printed = FALSE; |
35835446 | 1867 | bfd *p; |
252b5132 | 1868 | |
4d4920ec EB |
1869 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
1870 | { | |
1871 | asection *s; | |
1872 | ||
1873 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
1874 | || file->just_syms_flag) | |
1875 | continue; | |
1876 | ||
1877 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
a14a5de3 | 1878 | if ((s->output_section == NULL |
f13a99db | 1879 | || s->output_section->owner != link_info.output_bfd) |
a14a5de3 | 1880 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) |
4d4920ec EB |
1881 | { |
1882 | if (! dis_header_printed) | |
1883 | { | |
1884 | fprintf (config.map_file, _("\nDiscarded input sections\n\n")); | |
1885 | dis_header_printed = TRUE; | |
1886 | } | |
1887 | ||
1888 | print_input_section (s); | |
1889 | } | |
1890 | } | |
1891 | ||
252b5132 RH |
1892 | minfo (_("\nMemory Configuration\n\n")); |
1893 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
1894 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
1895 | ||
1579bae1 | 1896 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 RH |
1897 | { |
1898 | char buf[100]; | |
1899 | int len; | |
1900 | ||
4a93e180 | 1901 | fprintf (config.map_file, "%-16s ", m->name_list.name); |
252b5132 RH |
1902 | |
1903 | sprintf_vma (buf, m->origin); | |
1904 | minfo ("0x%s ", buf); | |
1905 | len = strlen (buf); | |
1906 | while (len < 16) | |
1907 | { | |
1908 | print_space (); | |
1909 | ++len; | |
1910 | } | |
1911 | ||
1912 | minfo ("0x%V", m->length); | |
1913 | if (m->flags || m->not_flags) | |
1914 | { | |
1915 | #ifndef BFD64 | |
1916 | minfo (" "); | |
1917 | #endif | |
1918 | if (m->flags) | |
1919 | { | |
1920 | print_space (); | |
1921 | lang_map_flags (m->flags); | |
1922 | } | |
1923 | ||
1924 | if (m->not_flags) | |
1925 | { | |
1926 | minfo (" !"); | |
1927 | lang_map_flags (m->not_flags); | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | print_nl (); | |
1932 | } | |
1933 | ||
1934 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
1935 | ||
c0f00686 | 1936 | if (! link_info.reduce_memory_overheads) |
35835446 JR |
1937 | { |
1938 | obstack_begin (&map_obstack, 1000); | |
1939 | for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next) | |
1940 | bfd_map_over_sections (p, init_map_userdata, 0); | |
1941 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); | |
1942 | } | |
a1438fce | 1943 | lang_statement_iteration ++; |
252b5132 RH |
1944 | print_statements (); |
1945 | } | |
1946 | ||
35835446 | 1947 | static void |
967928e9 AM |
1948 | init_map_userdata (bfd *abfd ATTRIBUTE_UNUSED, |
1949 | asection *sec, | |
1950 | void *data ATTRIBUTE_UNUSED) | |
35835446 JR |
1951 | { |
1952 | fat_section_userdata_type *new_data | |
1953 | = ((fat_section_userdata_type *) (stat_alloc | |
1954 | (sizeof (fat_section_userdata_type)))); | |
1955 | ||
1956 | ASSERT (get_userdata (sec) == NULL); | |
1957 | get_userdata (sec) = new_data; | |
1958 | new_data->map_symbol_def_tail = &new_data->map_symbol_def_head; | |
1959 | } | |
1960 | ||
1961 | static bfd_boolean | |
967928e9 AM |
1962 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
1963 | void *info ATTRIBUTE_UNUSED) | |
35835446 JR |
1964 | { |
1965 | if (hash_entry->type == bfd_link_hash_defined | |
1966 | || hash_entry->type == bfd_link_hash_defweak) | |
1967 | { | |
1968 | struct fat_user_section_struct *ud; | |
1969 | struct map_symbol_def *def; | |
1970 | ||
1971 | ud = get_userdata (hash_entry->u.def.section); | |
1972 | if (! ud) | |
1973 | { | |
1974 | /* ??? What do we have to do to initialize this beforehand? */ | |
1975 | /* The first time we get here is bfd_abs_section... */ | |
1976 | init_map_userdata (0, hash_entry->u.def.section, 0); | |
1977 | ud = get_userdata (hash_entry->u.def.section); | |
1978 | } | |
1979 | else if (!ud->map_symbol_def_tail) | |
1980 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; | |
afd7a018 | 1981 | |
6feb9908 | 1982 | def = obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 1983 | def->entry = hash_entry; |
76d7af2d | 1984 | *(ud->map_symbol_def_tail) = def; |
35835446 JR |
1985 | ud->map_symbol_def_tail = &def->next; |
1986 | } | |
1987 | return TRUE; | |
1988 | } | |
1989 | ||
252b5132 RH |
1990 | /* Initialize an output section. */ |
1991 | ||
1992 | static void | |
12d814e1 L |
1993 | init_os (lang_output_section_statement_type *s, asection *isec, |
1994 | flagword flags) | |
252b5132 | 1995 | { |
252b5132 RH |
1996 | if (s->bfd_section != NULL) |
1997 | return; | |
1998 | ||
1999 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) | |
6f9efd97 | 2000 | einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2001 | |
8a99a385 AM |
2002 | if (s->constraint != SPECIAL) |
2003 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); | |
1579bae1 | 2004 | if (s->bfd_section == NULL) |
8a99a385 AM |
2005 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2006 | s->name, flags); | |
1579bae1 | 2007 | if (s->bfd_section == NULL) |
252b5132 RH |
2008 | { |
2009 | einfo (_("%P%F: output format %s cannot represent section called %s\n"), | |
f13a99db | 2010 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2011 | } |
2012 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2013 | s->bfd_section->output_offset = 0; |
e0f6802f | 2014 | |
c0f00686 | 2015 | if (!link_info.reduce_memory_overheads) |
72223188 JJ |
2016 | { |
2017 | fat_section_userdata_type *new | |
2018 | = stat_alloc (sizeof (fat_section_userdata_type)); | |
2019 | memset (new, 0, sizeof (fat_section_userdata_type)); | |
2020 | get_userdata (s->bfd_section) = new; | |
2021 | } | |
2022 | ||
252b5132 RH |
2023 | /* If there is a base address, make sure that any sections it might |
2024 | mention are initialized. */ | |
2025 | if (s->addr_tree != NULL) | |
2026 | exp_init_os (s->addr_tree); | |
18794b0c AM |
2027 | |
2028 | if (s->load_base != NULL) | |
2029 | exp_init_os (s->load_base); | |
ccd2ec6a | 2030 | |
7270c5ed AM |
2031 | /* If supplied an alignment, set it. */ |
2032 | if (s->section_alignment != -1) | |
2033 | s->bfd_section->alignment_power = s->section_alignment; | |
2034 | ||
ccd2ec6a L |
2035 | if (isec) |
2036 | bfd_init_private_section_data (isec->owner, isec, | |
f13a99db | 2037 | link_info.output_bfd, s->bfd_section, |
ccd2ec6a | 2038 | &link_info); |
252b5132 RH |
2039 | } |
2040 | ||
2041 | /* Make sure that all output sections mentioned in an expression are | |
2042 | initialized. */ | |
2043 | ||
2044 | static void | |
1579bae1 | 2045 | exp_init_os (etree_type *exp) |
252b5132 RH |
2046 | { |
2047 | switch (exp->type.node_class) | |
2048 | { | |
2049 | case etree_assign: | |
9f4fb502 | 2050 | case etree_provide: |
252b5132 RH |
2051 | exp_init_os (exp->assign.src); |
2052 | break; | |
2053 | ||
2054 | case etree_binary: | |
2055 | exp_init_os (exp->binary.lhs); | |
2056 | exp_init_os (exp->binary.rhs); | |
2057 | break; | |
2058 | ||
2059 | case etree_trinary: | |
2060 | exp_init_os (exp->trinary.cond); | |
2061 | exp_init_os (exp->trinary.lhs); | |
2062 | exp_init_os (exp->trinary.rhs); | |
2063 | break; | |
2064 | ||
b6ca8815 NS |
2065 | case etree_assert: |
2066 | exp_init_os (exp->assert_s.child); | |
2067 | break; | |
afd7a018 | 2068 | |
252b5132 RH |
2069 | case etree_unary: |
2070 | exp_init_os (exp->unary.child); | |
2071 | break; | |
2072 | ||
2073 | case etree_name: | |
2074 | switch (exp->type.node_code) | |
2075 | { | |
2076 | case ADDR: | |
2077 | case LOADADDR: | |
2078 | case SIZEOF: | |
2079 | { | |
2080 | lang_output_section_statement_type *os; | |
2081 | ||
2082 | os = lang_output_section_find (exp->name.name); | |
2083 | if (os != NULL && os->bfd_section == NULL) | |
12d814e1 | 2084 | init_os (os, NULL, 0); |
252b5132 RH |
2085 | } |
2086 | } | |
2087 | break; | |
2088 | ||
2089 | default: | |
2090 | break; | |
2091 | } | |
2092 | } | |
9503fd87 | 2093 | \f |
252b5132 | 2094 | static void |
1579bae1 | 2095 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2096 | { |
1579bae1 | 2097 | lang_input_statement_type *entry = data; |
252b5132 RH |
2098 | |
2099 | /* If we are only reading symbols from this object, then we want to | |
2100 | discard all sections. */ | |
2101 | if (entry->just_syms_flag) | |
2102 | { | |
1449d79b | 2103 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2104 | return; |
2105 | } | |
2106 | ||
ace66bb2 | 2107 | if (!(abfd->flags & DYNAMIC)) |
c0f00686 | 2108 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2109 | } |
2110 | \f | |
2111 | /* The wild routines. | |
2112 | ||
2113 | These expand statements like *(.text) and foo.o to a list of | |
2114 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2115 | foo.o(.text, .data). */ | |
2116 | ||
252b5132 RH |
2117 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
2118 | lang_input_section statement which is placed at PTR. FILE is the | |
2119 | input file which holds SECTION. */ | |
2120 | ||
2121 | void | |
1579bae1 AM |
2122 | lang_add_section (lang_statement_list_type *ptr, |
2123 | asection *section, | |
7b986e99 | 2124 | lang_output_section_statement_type *output) |
252b5132 | 2125 | { |
164e712d | 2126 | flagword flags = section->flags; |
b34976b6 | 2127 | bfd_boolean discard; |
252b5132 | 2128 | |
e49f5022 | 2129 | /* Discard sections marked with SEC_EXCLUDE. */ |
b3096250 | 2130 | discard = (flags & SEC_EXCLUDE) != 0; |
252b5132 RH |
2131 | |
2132 | /* Discard input sections which are assigned to a section named | |
2133 | DISCARD_SECTION_NAME. */ | |
2134 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
b34976b6 | 2135 | discard = TRUE; |
252b5132 RH |
2136 | |
2137 | /* Discard debugging sections if we are stripping debugging | |
2138 | information. */ | |
2139 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2140 | && (flags & SEC_DEBUGGING) != 0) | |
b34976b6 | 2141 | discard = TRUE; |
252b5132 RH |
2142 | |
2143 | if (discard) | |
2144 | { | |
2145 | if (section->output_section == NULL) | |
2146 | { | |
2147 | /* This prevents future calls from assigning this section. */ | |
2148 | section->output_section = bfd_abs_section_ptr; | |
2149 | } | |
2150 | return; | |
2151 | } | |
2152 | ||
2153 | if (section->output_section == NULL) | |
2154 | { | |
b34976b6 | 2155 | bfd_boolean first; |
252b5132 RH |
2156 | lang_input_section_type *new; |
2157 | flagword flags; | |
2158 | ||
12d814e1 L |
2159 | flags = section->flags; |
2160 | ||
2161 | /* We don't copy the SEC_NEVER_LOAD flag from an input section | |
2162 | to an output section, because we want to be able to include a | |
2163 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2164 | section (I don't know why we want to do this, but we do). | |
2165 | build_link_order in ldwrite.c handles this case by turning | |
2166 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2167 | ||
2168 | flags &= ~ SEC_NEVER_LOAD; | |
2169 | ||
2170 | switch (output->sectype) | |
2171 | { | |
2172 | case normal_section: | |
152d792f | 2173 | case overlay_section: |
12d814e1 L |
2174 | break; |
2175 | case noalloc_section: | |
2176 | flags &= ~SEC_ALLOC; | |
2177 | break; | |
2178 | case noload_section: | |
2179 | flags &= ~SEC_LOAD; | |
2180 | flags |= SEC_NEVER_LOAD; | |
2181 | break; | |
2182 | } | |
2183 | ||
252b5132 | 2184 | if (output->bfd_section == NULL) |
12d814e1 | 2185 | init_os (output, section, flags); |
d1778b88 AM |
2186 | |
2187 | first = ! output->bfd_section->linker_has_input; | |
2188 | output->bfd_section->linker_has_input = 1; | |
252b5132 | 2189 | |
8423293d AM |
2190 | if (!link_info.relocatable |
2191 | && !stripped_excluded_sections) | |
2192 | { | |
2193 | asection *s = output->bfd_section->map_tail.s; | |
2194 | output->bfd_section->map_tail.s = section; | |
2195 | section->map_head.s = NULL; | |
2196 | section->map_tail.s = s; | |
2197 | if (s != NULL) | |
2198 | s->map_head.s = section; | |
2199 | else | |
2200 | output->bfd_section->map_head.s = section; | |
2201 | } | |
2202 | ||
08da4cac | 2203 | /* Add a section reference to the list. */ |
252b5132 RH |
2204 | new = new_stat (lang_input_section, ptr); |
2205 | ||
2206 | new->section = section; | |
252b5132 RH |
2207 | section->output_section = output->bfd_section; |
2208 | ||
252b5132 RH |
2209 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2210 | already been processed. One reason to do this is that on pe | |
2211 | format targets, .text$foo sections go into .text and it's odd | |
2212 | to see .text with SEC_LINK_ONCE set. */ | |
2213 | ||
1049f94e | 2214 | if (! link_info.relocatable) |
252b5132 RH |
2215 | flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES); |
2216 | ||
2217 | /* If this is not the first input section, and the SEC_READONLY | |
afd7a018 AM |
2218 | flag is not currently set, then don't set it just because the |
2219 | input section has it set. */ | |
252b5132 | 2220 | |
afd7a018 | 2221 | if (! first && (output->bfd_section->flags & SEC_READONLY) == 0) |
252b5132 RH |
2222 | flags &= ~ SEC_READONLY; |
2223 | ||
f5fa8ca2 JJ |
2224 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
2225 | if (! first | |
afd7a018 | 2226 | && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
f5fa8ca2 JJ |
2227 | != (flags & (SEC_MERGE | SEC_STRINGS)) |
2228 | || ((flags & SEC_MERGE) | |
afd7a018 | 2229 | && output->bfd_section->entsize != section->entsize))) |
f5fa8ca2 | 2230 | { |
afd7a018 | 2231 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2232 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2233 | } | |
2234 | ||
afd7a018 | 2235 | output->bfd_section->flags |= flags; |
252b5132 | 2236 | |
f5fa8ca2 | 2237 | if (flags & SEC_MERGE) |
afd7a018 | 2238 | output->bfd_section->entsize = section->entsize; |
f5fa8ca2 | 2239 | |
252b5132 | 2240 | /* If SEC_READONLY is not set in the input section, then clear |
afd7a018 | 2241 | it from the output section. */ |
252b5132 | 2242 | if ((section->flags & SEC_READONLY) == 0) |
afd7a018 | 2243 | output->bfd_section->flags &= ~SEC_READONLY; |
252b5132 | 2244 | |
667f5177 ILT |
2245 | /* Copy over SEC_SMALL_DATA. */ |
2246 | if (section->flags & SEC_SMALL_DATA) | |
afd7a018 | 2247 | output->bfd_section->flags |= SEC_SMALL_DATA; |
9e41f973 | 2248 | |
252b5132 RH |
2249 | if (section->alignment_power > output->bfd_section->alignment_power) |
2250 | output->bfd_section->alignment_power = section->alignment_power; | |
2251 | ||
ebe372c1 L |
2252 | if (bfd_get_arch (section->owner) == bfd_arch_tic54x |
2253 | && (section->flags & SEC_TIC54X_BLOCK) != 0) | |
08da4cac | 2254 | { |
ebe372c1 | 2255 | output->bfd_section->flags |= SEC_TIC54X_BLOCK; |
08da4cac KH |
2256 | /* FIXME: This value should really be obtained from the bfd... */ |
2257 | output->block_value = 128; | |
2258 | } | |
252b5132 RH |
2259 | } |
2260 | } | |
2261 | ||
2262 | /* Handle wildcard sorting. This returns the lang_input_section which | |
2263 | should follow the one we are going to create for SECTION and FILE, | |
2264 | based on the sorting requirements of WILD. It returns NULL if the | |
2265 | new section should just go at the end of the current list. */ | |
2266 | ||
2267 | static lang_statement_union_type * | |
1579bae1 AM |
2268 | wild_sort (lang_wild_statement_type *wild, |
2269 | struct wildcard_list *sec, | |
2270 | lang_input_statement_type *file, | |
2271 | asection *section) | |
252b5132 RH |
2272 | { |
2273 | const char *section_name; | |
2274 | lang_statement_union_type *l; | |
2275 | ||
bcaa7b3e L |
2276 | if (!wild->filenames_sorted |
2277 | && (sec == NULL || sec->spec.sorted == none)) | |
252b5132 RH |
2278 | return NULL; |
2279 | ||
2280 | section_name = bfd_get_section_name (file->the_bfd, section); | |
bba1a0c0 | 2281 | for (l = wild->children.head; l != NULL; l = l->header.next) |
252b5132 RH |
2282 | { |
2283 | lang_input_section_type *ls; | |
2284 | ||
2285 | if (l->header.type != lang_input_section_enum) | |
2286 | continue; | |
2287 | ls = &l->input_section; | |
2288 | ||
2289 | /* Sorting by filename takes precedence over sorting by section | |
afd7a018 | 2290 | name. */ |
252b5132 RH |
2291 | |
2292 | if (wild->filenames_sorted) | |
2293 | { | |
2294 | const char *fn, *ln; | |
b34976b6 | 2295 | bfd_boolean fa, la; |
252b5132 RH |
2296 | int i; |
2297 | ||
2298 | /* The PE support for the .idata section as generated by | |
afd7a018 AM |
2299 | dlltool assumes that files will be sorted by the name of |
2300 | the archive and then the name of the file within the | |
2301 | archive. */ | |
252b5132 RH |
2302 | |
2303 | if (file->the_bfd != NULL | |
2304 | && bfd_my_archive (file->the_bfd) != NULL) | |
2305 | { | |
2306 | fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); | |
b34976b6 | 2307 | fa = TRUE; |
252b5132 RH |
2308 | } |
2309 | else | |
2310 | { | |
2311 | fn = file->filename; | |
b34976b6 | 2312 | fa = FALSE; |
252b5132 RH |
2313 | } |
2314 | ||
7b986e99 | 2315 | if (bfd_my_archive (ls->section->owner) != NULL) |
252b5132 | 2316 | { |
7b986e99 | 2317 | ln = bfd_get_filename (bfd_my_archive (ls->section->owner)); |
b34976b6 | 2318 | la = TRUE; |
252b5132 RH |
2319 | } |
2320 | else | |
2321 | { | |
7b986e99 | 2322 | ln = ls->section->owner->filename; |
b34976b6 | 2323 | la = FALSE; |
252b5132 RH |
2324 | } |
2325 | ||
2326 | i = strcmp (fn, ln); | |
2327 | if (i > 0) | |
2328 | continue; | |
2329 | else if (i < 0) | |
2330 | break; | |
2331 | ||
2332 | if (fa || la) | |
2333 | { | |
2334 | if (fa) | |
2335 | fn = file->filename; | |
2336 | if (la) | |
7b986e99 | 2337 | ln = ls->section->owner->filename; |
252b5132 RH |
2338 | |
2339 | i = strcmp (fn, ln); | |
2340 | if (i > 0) | |
2341 | continue; | |
2342 | else if (i < 0) | |
2343 | break; | |
2344 | } | |
2345 | } | |
2346 | ||
2347 | /* Here either the files are not sorted by name, or we are | |
afd7a018 | 2348 | looking at the sections for this file. */ |
252b5132 | 2349 | |
bcaa7b3e | 2350 | if (sec != NULL && sec->spec.sorted != none) |
32124d5b AM |
2351 | if (compare_section (sec->spec.sorted, section, ls->section) < 0) |
2352 | break; | |
252b5132 RH |
2353 | } |
2354 | ||
2355 | return l; | |
2356 | } | |
2357 | ||
2358 | /* Expand a wild statement for a particular FILE. SECTION may be | |
2359 | NULL, in which case it is a wild card. */ | |
2360 | ||
2361 | static void | |
1579bae1 AM |
2362 | output_section_callback (lang_wild_statement_type *ptr, |
2363 | struct wildcard_list *sec, | |
2364 | asection *section, | |
2365 | lang_input_statement_type *file, | |
2366 | void *output) | |
4dec4d4e RH |
2367 | { |
2368 | lang_statement_union_type *before; | |
5f992e62 | 2369 | |
b6bf44ba | 2370 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0d6a25b | 2371 | if (unique_section_p (section)) |
b6bf44ba AM |
2372 | return; |
2373 | ||
b6bf44ba | 2374 | before = wild_sort (ptr, sec, file, section); |
5f992e62 | 2375 | |
4dec4d4e RH |
2376 | /* Here BEFORE points to the lang_input_section which |
2377 | should follow the one we are about to add. If BEFORE | |
2378 | is NULL, then the section should just go at the end | |
2379 | of the current list. */ | |
5f992e62 | 2380 | |
4dec4d4e | 2381 | if (before == NULL) |
39dcfe18 | 2382 | lang_add_section (&ptr->children, section, |
7b986e99 | 2383 | (lang_output_section_statement_type *) output); |
4dec4d4e | 2384 | else |
252b5132 | 2385 | { |
4dec4d4e RH |
2386 | lang_statement_list_type list; |
2387 | lang_statement_union_type **pp; | |
5f992e62 | 2388 | |
4dec4d4e | 2389 | lang_list_init (&list); |
39dcfe18 | 2390 | lang_add_section (&list, section, |
7b986e99 | 2391 | (lang_output_section_statement_type *) output); |
5f992e62 | 2392 | |
4dec4d4e RH |
2393 | /* If we are discarding the section, LIST.HEAD will |
2394 | be NULL. */ | |
2395 | if (list.head != NULL) | |
252b5132 | 2396 | { |
bba1a0c0 | 2397 | ASSERT (list.head->header.next == NULL); |
5f992e62 | 2398 | |
4dec4d4e RH |
2399 | for (pp = &ptr->children.head; |
2400 | *pp != before; | |
bba1a0c0 | 2401 | pp = &(*pp)->header.next) |
4dec4d4e | 2402 | ASSERT (*pp != NULL); |
5f992e62 | 2403 | |
bba1a0c0 | 2404 | list.head->header.next = *pp; |
4dec4d4e | 2405 | *pp = list.head; |
252b5132 RH |
2406 | } |
2407 | } | |
2408 | } | |
2409 | ||
0841712e JJ |
2410 | /* Check if all sections in a wild statement for a particular FILE |
2411 | are readonly. */ | |
2412 | ||
2413 | static void | |
2414 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2415 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2416 | asection *section, | |
2417 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
6feb9908 | 2418 | void *data) |
0841712e JJ |
2419 | { |
2420 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ | |
2421 | if (unique_section_p (section)) | |
2422 | return; | |
2423 | ||
6feb9908 AM |
2424 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
2425 | ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE; | |
0841712e JJ |
2426 | } |
2427 | ||
252b5132 RH |
2428 | /* This is passed a file name which must have been seen already and |
2429 | added to the statement tree. We will see if it has been opened | |
2430 | already and had its symbols read. If not then we'll read it. */ | |
2431 | ||
2432 | static lang_input_statement_type * | |
1579bae1 | 2433 | lookup_name (const char *name) |
252b5132 RH |
2434 | { |
2435 | lang_input_statement_type *search; | |
2436 | ||
2437 | for (search = (lang_input_statement_type *) input_file_chain.head; | |
1579bae1 | 2438 | search != NULL; |
252b5132 RH |
2439 | search = (lang_input_statement_type *) search->next_real_file) |
2440 | { | |
0013291d NC |
2441 | /* Use the local_sym_name as the name of the file that has |
2442 | already been loaded as filename might have been transformed | |
2443 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2444 | const char *filename = search->local_sym_name; |
0013291d | 2445 | |
0013291d | 2446 | if (filename != NULL |
0013291d | 2447 | && strcmp (filename, name) == 0) |
252b5132 RH |
2448 | break; |
2449 | } | |
2450 | ||
1579bae1 | 2451 | if (search == NULL) |
6feb9908 AM |
2452 | search = new_afile (name, lang_input_file_is_search_file_enum, |
2453 | default_target, FALSE); | |
252b5132 RH |
2454 | |
2455 | /* If we have already added this file, or this file is not real | |
87aa7f19 AM |
2456 | don't add this file. */ |
2457 | if (search->loaded || !search->real) | |
252b5132 RH |
2458 | return search; |
2459 | ||
1579bae1 | 2460 | if (! load_symbols (search, NULL)) |
6770ec8c | 2461 | return NULL; |
252b5132 RH |
2462 | |
2463 | return search; | |
2464 | } | |
2465 | ||
b58f81ae DJ |
2466 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2467 | ||
2468 | struct excluded_lib | |
2469 | { | |
2470 | char *name; | |
2471 | struct excluded_lib *next; | |
2472 | }; | |
2473 | static struct excluded_lib *excluded_libs; | |
2474 | ||
2475 | void | |
2476 | add_excluded_libs (const char *list) | |
2477 | { | |
2478 | const char *p = list, *end; | |
2479 | ||
2480 | while (*p != '\0') | |
2481 | { | |
2482 | struct excluded_lib *entry; | |
2483 | end = strpbrk (p, ",:"); | |
2484 | if (end == NULL) | |
2485 | end = p + strlen (p); | |
2486 | entry = xmalloc (sizeof (*entry)); | |
2487 | entry->next = excluded_libs; | |
2488 | entry->name = xmalloc (end - p + 1); | |
2489 | memcpy (entry->name, p, end - p); | |
2490 | entry->name[end - p] = '\0'; | |
2491 | excluded_libs = entry; | |
2492 | if (*end == '\0') | |
329c1c86 | 2493 | break; |
b58f81ae DJ |
2494 | p = end + 1; |
2495 | } | |
2496 | } | |
2497 | ||
2498 | static void | |
2499 | check_excluded_libs (bfd *abfd) | |
2500 | { | |
2501 | struct excluded_lib *lib = excluded_libs; | |
2502 | ||
2503 | while (lib) | |
2504 | { | |
2505 | int len = strlen (lib->name); | |
2506 | const char *filename = lbasename (abfd->filename); | |
2507 | ||
2508 | if (strcmp (lib->name, "ALL") == 0) | |
2509 | { | |
2510 | abfd->no_export = TRUE; | |
2511 | return; | |
2512 | } | |
2513 | ||
2514 | if (strncmp (lib->name, filename, len) == 0 | |
2515 | && (filename[len] == '\0' | |
2516 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
2517 | && filename[len + 2] == '\0'))) | |
2518 | { | |
2519 | abfd->no_export = TRUE; | |
2520 | return; | |
2521 | } | |
2522 | ||
2523 | lib = lib->next; | |
2524 | } | |
2525 | } | |
2526 | ||
252b5132 RH |
2527 | /* Get the symbols for an input file. */ |
2528 | ||
e9f53129 | 2529 | bfd_boolean |
1579bae1 AM |
2530 | load_symbols (lang_input_statement_type *entry, |
2531 | lang_statement_list_type *place) | |
252b5132 RH |
2532 | { |
2533 | char **matching; | |
2534 | ||
2535 | if (entry->loaded) | |
b34976b6 | 2536 | return TRUE; |
252b5132 RH |
2537 | |
2538 | ldfile_open_file (entry); | |
2539 | ||
2540 | if (! bfd_check_format (entry->the_bfd, bfd_archive) | |
2541 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
2542 | { | |
2543 | bfd_error_type err; | |
e3f2db7f | 2544 | bfd_boolean save_ldlang_sysrooted_script; |
f2e349f9 | 2545 | bfd_boolean save_as_needed, save_add_needed; |
b7a26f91 | 2546 | |
252b5132 | 2547 | err = bfd_get_error (); |
884fb58e NC |
2548 | |
2549 | /* See if the emulation has some special knowledge. */ | |
2550 | if (ldemul_unrecognized_file (entry)) | |
b34976b6 | 2551 | return TRUE; |
884fb58e | 2552 | |
252b5132 RH |
2553 | if (err == bfd_error_file_ambiguously_recognized) |
2554 | { | |
2555 | char **p; | |
2556 | ||
2557 | einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); | |
2558 | einfo (_("%B: matching formats:"), entry->the_bfd); | |
2559 | for (p = matching; *p != NULL; p++) | |
2560 | einfo (" %s", *p); | |
2561 | einfo ("%F\n"); | |
2562 | } | |
2563 | else if (err != bfd_error_file_not_recognized | |
2564 | || place == NULL) | |
967928e9 | 2565 | einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 2566 | |
252b5132 RH |
2567 | bfd_close (entry->the_bfd); |
2568 | entry->the_bfd = NULL; | |
2569 | ||
252b5132 | 2570 | /* Try to interpret the file as a linker script. */ |
252b5132 RH |
2571 | ldfile_open_command_file (entry->filename); |
2572 | ||
bde18da4 | 2573 | push_stat_ptr (place); |
e3f2db7f AO |
2574 | save_ldlang_sysrooted_script = ldlang_sysrooted_script; |
2575 | ldlang_sysrooted_script = entry->sysrooted; | |
f2e349f9 KK |
2576 | save_as_needed = as_needed; |
2577 | as_needed = entry->as_needed; | |
2578 | save_add_needed = add_needed; | |
2579 | add_needed = entry->add_needed; | |
252b5132 | 2580 | |
b34976b6 | 2581 | ldfile_assumed_script = TRUE; |
252b5132 | 2582 | parser_input = input_script; |
532345f2 L |
2583 | /* We want to use the same -Bdynamic/-Bstatic as the one for |
2584 | ENTRY. */ | |
2585 | config.dynamic_link = entry->dynamic; | |
252b5132 | 2586 | yyparse (); |
b34976b6 | 2587 | ldfile_assumed_script = FALSE; |
252b5132 | 2588 | |
e3f2db7f | 2589 | ldlang_sysrooted_script = save_ldlang_sysrooted_script; |
f2e349f9 KK |
2590 | as_needed = save_as_needed; |
2591 | add_needed = save_add_needed; | |
bde18da4 | 2592 | pop_stat_ptr (); |
252b5132 | 2593 | |
bde18da4 | 2594 | return TRUE; |
252b5132 RH |
2595 | } |
2596 | ||
2597 | if (ldemul_recognized_file (entry)) | |
b34976b6 | 2598 | return TRUE; |
252b5132 RH |
2599 | |
2600 | /* We don't call ldlang_add_file for an archive. Instead, the | |
2601 | add_symbols entry point will call ldlang_add_file, via the | |
2602 | add_archive_element callback, for each element of the archive | |
2603 | which is used. */ | |
2604 | switch (bfd_get_format (entry->the_bfd)) | |
2605 | { | |
2606 | default: | |
2607 | break; | |
2608 | ||
2609 | case bfd_object: | |
2610 | ldlang_add_file (entry); | |
2611 | if (trace_files || trace_file_tries) | |
2612 | info_msg ("%I\n", entry); | |
2613 | break; | |
2614 | ||
2615 | case bfd_archive: | |
b58f81ae DJ |
2616 | check_excluded_libs (entry->the_bfd); |
2617 | ||
252b5132 RH |
2618 | if (entry->whole_archive) |
2619 | { | |
b7a26f91 | 2620 | bfd *member = NULL; |
b34976b6 | 2621 | bfd_boolean loaded = TRUE; |
6770ec8c NC |
2622 | |
2623 | for (;;) | |
252b5132 | 2624 | { |
6770ec8c NC |
2625 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
2626 | ||
2627 | if (member == NULL) | |
2628 | break; | |
b7a26f91 | 2629 | |
252b5132 | 2630 | if (! bfd_check_format (member, bfd_object)) |
6770ec8c NC |
2631 | { |
2632 | einfo (_("%F%B: member %B in archive is not an object\n"), | |
2633 | entry->the_bfd, member); | |
b34976b6 | 2634 | loaded = FALSE; |
6770ec8c NC |
2635 | } |
2636 | ||
252b5132 RH |
2637 | if (! ((*link_info.callbacks->add_archive_element) |
2638 | (&link_info, member, "--whole-archive"))) | |
2639 | abort (); | |
6770ec8c | 2640 | |
252b5132 | 2641 | if (! bfd_link_add_symbols (member, &link_info)) |
6770ec8c NC |
2642 | { |
2643 | einfo (_("%F%B: could not read symbols: %E\n"), member); | |
b34976b6 | 2644 | loaded = FALSE; |
6770ec8c | 2645 | } |
252b5132 RH |
2646 | } |
2647 | ||
6770ec8c NC |
2648 | entry->loaded = loaded; |
2649 | return loaded; | |
252b5132 | 2650 | } |
6770ec8c | 2651 | break; |
252b5132 RH |
2652 | } |
2653 | ||
03bdc404 | 2654 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
b34976b6 | 2655 | entry->loaded = TRUE; |
6770ec8c | 2656 | else |
252b5132 RH |
2657 | einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd); |
2658 | ||
6770ec8c | 2659 | return entry->loaded; |
252b5132 RH |
2660 | } |
2661 | ||
b6bf44ba AM |
2662 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
2663 | may be NULL, indicating that it is a wildcard. Separate | |
2664 | lang_input_section statements are created for each part of the | |
2665 | expansion; they are added after the wild statement S. OUTPUT is | |
2666 | the output section. */ | |
252b5132 RH |
2667 | |
2668 | static void | |
1579bae1 AM |
2669 | wild (lang_wild_statement_type *s, |
2670 | const char *target ATTRIBUTE_UNUSED, | |
2671 | lang_output_section_statement_type *output) | |
252b5132 | 2672 | { |
b6bf44ba | 2673 | struct wildcard_list *sec; |
252b5132 | 2674 | |
50c77e5d | 2675 | if (s->handler_data[0] |
329c1c86 | 2676 | && s->handler_data[0]->spec.sorted == by_name |
50c77e5d NC |
2677 | && !s->filenames_sorted) |
2678 | { | |
329c1c86 AM |
2679 | lang_section_bst_type *tree; |
2680 | ||
50c77e5d NC |
2681 | walk_wild (s, output_section_callback_fast, output); |
2682 | ||
e6f2cbf5 | 2683 | tree = s->tree; |
329c1c86 | 2684 | if (tree) |
e6f2cbf5 L |
2685 | { |
2686 | output_section_callback_tree_to_list (s, tree, output); | |
2687 | s->tree = NULL; | |
2688 | } | |
50c77e5d | 2689 | } |
329c1c86 | 2690 | else |
50c77e5d | 2691 | walk_wild (s, output_section_callback, output); |
b6bf44ba | 2692 | |
abe6ac95 AM |
2693 | if (default_common_section == NULL) |
2694 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
2695 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
2696 | { | |
2697 | /* Remember the section that common is going to in case we | |
b7a26f91 | 2698 | later get something which doesn't know where to put it. */ |
b6bf44ba | 2699 | default_common_section = output; |
abe6ac95 | 2700 | break; |
b6bf44ba | 2701 | } |
252b5132 RH |
2702 | } |
2703 | ||
b34976b6 | 2704 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 2705 | |
e50d8076 | 2706 | static int |
1579bae1 | 2707 | get_target (const bfd_target *target, void *data) |
e50d8076 | 2708 | { |
1579bae1 | 2709 | const char *sought = data; |
5f992e62 | 2710 | |
e50d8076 NC |
2711 | return strcmp (target->name, sought) == 0; |
2712 | } | |
2713 | ||
2714 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 2715 | |
e50d8076 | 2716 | static void |
1579bae1 | 2717 | stricpy (char *dest, char *src) |
e50d8076 NC |
2718 | { |
2719 | char c; | |
5f992e62 | 2720 | |
08da4cac | 2721 | while ((c = *src++) != 0) |
3882b010 | 2722 | *dest++ = TOLOWER (c); |
e50d8076 | 2723 | |
08da4cac | 2724 | *dest = 0; |
e50d8076 NC |
2725 | } |
2726 | ||
396a2467 | 2727 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 2728 | from haystack. */ |
08da4cac | 2729 | |
e50d8076 | 2730 | static void |
1579bae1 | 2731 | strcut (char *haystack, char *needle) |
e50d8076 NC |
2732 | { |
2733 | haystack = strstr (haystack, needle); | |
5f992e62 | 2734 | |
e50d8076 NC |
2735 | if (haystack) |
2736 | { | |
08da4cac | 2737 | char *src; |
e50d8076 | 2738 | |
08da4cac KH |
2739 | for (src = haystack + strlen (needle); *src;) |
2740 | *haystack++ = *src++; | |
5f992e62 | 2741 | |
08da4cac | 2742 | *haystack = 0; |
e50d8076 NC |
2743 | } |
2744 | } | |
2745 | ||
2746 | /* Compare two target format name strings. | |
2747 | Return a value indicating how "similar" they are. */ | |
08da4cac | 2748 | |
e50d8076 | 2749 | static int |
1579bae1 | 2750 | name_compare (char *first, char *second) |
e50d8076 | 2751 | { |
08da4cac KH |
2752 | char *copy1; |
2753 | char *copy2; | |
2754 | int result; | |
5f992e62 | 2755 | |
e50d8076 NC |
2756 | copy1 = xmalloc (strlen (first) + 1); |
2757 | copy2 = xmalloc (strlen (second) + 1); | |
2758 | ||
2759 | /* Convert the names to lower case. */ | |
2760 | stricpy (copy1, first); | |
2761 | stricpy (copy2, second); | |
2762 | ||
1579bae1 | 2763 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
2764 | strcut (copy1, "big"); |
2765 | strcut (copy1, "little"); | |
2766 | strcut (copy2, "big"); | |
2767 | strcut (copy2, "little"); | |
2768 | ||
2769 | /* Return a value based on how many characters match, | |
2770 | starting from the beginning. If both strings are | |
2771 | the same then return 10 * their length. */ | |
08da4cac KH |
2772 | for (result = 0; copy1[result] == copy2[result]; result++) |
2773 | if (copy1[result] == 0) | |
e50d8076 NC |
2774 | { |
2775 | result *= 10; | |
2776 | break; | |
2777 | } | |
5f992e62 | 2778 | |
e50d8076 NC |
2779 | free (copy1); |
2780 | free (copy2); | |
2781 | ||
2782 | return result; | |
2783 | } | |
2784 | ||
2785 | /* Set by closest_target_match() below. */ | |
08da4cac | 2786 | static const bfd_target *winner; |
e50d8076 NC |
2787 | |
2788 | /* Scan all the valid bfd targets looking for one that has the endianness | |
2789 | requirement that was specified on the command line, and is the nearest | |
2790 | match to the original output target. */ | |
08da4cac | 2791 | |
e50d8076 | 2792 | static int |
1579bae1 | 2793 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 2794 | { |
1579bae1 | 2795 | const bfd_target *original = data; |
5f992e62 | 2796 | |
08da4cac KH |
2797 | if (command_line.endian == ENDIAN_BIG |
2798 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 2799 | return 0; |
5f992e62 | 2800 | |
08da4cac KH |
2801 | if (command_line.endian == ENDIAN_LITTLE |
2802 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
2803 | return 0; |
2804 | ||
2805 | /* Must be the same flavour. */ | |
2806 | if (target->flavour != original->flavour) | |
2807 | return 0; | |
2808 | ||
de7dd2bd | 2809 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 2810 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
2811 | || strcmp (target->name, "elf64-big") == 0 |
2812 | || strcmp (target->name, "elf32-little") == 0 | |
2813 | || strcmp (target->name, "elf64-little") == 0) | |
2814 | return 0; | |
2815 | ||
e50d8076 NC |
2816 | /* If we have not found a potential winner yet, then record this one. */ |
2817 | if (winner == NULL) | |
2818 | { | |
2819 | winner = target; | |
2820 | return 0; | |
2821 | } | |
2822 | ||
2823 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 2824 | Compare their names and choose the better one. */ |
d1778b88 AM |
2825 | if (name_compare (target->name, original->name) |
2826 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
2827 | winner = target; |
2828 | ||
2829 | /* Keep on searching until wqe have checked them all. */ | |
2830 | return 0; | |
2831 | } | |
2832 | ||
2833 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 2834 | |
e50d8076 | 2835 | static char * |
1579bae1 | 2836 | get_first_input_target (void) |
e50d8076 | 2837 | { |
08da4cac | 2838 | char *target = NULL; |
e50d8076 NC |
2839 | |
2840 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
2841 | { | |
2842 | if (s->header.type == lang_input_statement_enum | |
2843 | && s->real) | |
2844 | { | |
2845 | ldfile_open_file (s); | |
5f992e62 | 2846 | |
e50d8076 NC |
2847 | if (s->the_bfd != NULL |
2848 | && bfd_check_format (s->the_bfd, bfd_object)) | |
2849 | { | |
2850 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 2851 | |
e50d8076 NC |
2852 | if (target != NULL) |
2853 | break; | |
2854 | } | |
2855 | } | |
2856 | } | |
5f992e62 | 2857 | |
e50d8076 NC |
2858 | return target; |
2859 | } | |
2860 | ||
599917b8 | 2861 | const char * |
1579bae1 | 2862 | lang_get_output_target (void) |
599917b8 JJ |
2863 | { |
2864 | const char *target; | |
2865 | ||
2866 | /* Has the user told us which output format to use? */ | |
1579bae1 | 2867 | if (output_target != NULL) |
599917b8 JJ |
2868 | return output_target; |
2869 | ||
2870 | /* No - has the current target been set to something other than | |
2871 | the default? */ | |
2872 | if (current_target != default_target) | |
2873 | return current_target; | |
2874 | ||
2875 | /* No - can we determine the format of the first input file? */ | |
2876 | target = get_first_input_target (); | |
2877 | if (target != NULL) | |
2878 | return target; | |
2879 | ||
2880 | /* Failed - use the default output target. */ | |
2881 | return default_target; | |
2882 | } | |
2883 | ||
252b5132 RH |
2884 | /* Open the output file. */ |
2885 | ||
f13a99db | 2886 | static void |
1579bae1 | 2887 | open_output (const char *name) |
252b5132 | 2888 | { |
599917b8 | 2889 | output_target = lang_get_output_target (); |
5f992e62 | 2890 | |
08da4cac KH |
2891 | /* Has the user requested a particular endianness on the command |
2892 | line? */ | |
e50d8076 NC |
2893 | if (command_line.endian != ENDIAN_UNSET) |
2894 | { | |
08da4cac | 2895 | const bfd_target *target; |
1b69a0bf | 2896 | enum bfd_endian desired_endian; |
e50d8076 NC |
2897 | |
2898 | /* Get the chosen target. */ | |
1579bae1 | 2899 | target = bfd_search_for_target (get_target, (void *) output_target); |
e50d8076 | 2900 | |
c13b1b77 NC |
2901 | /* If the target is not supported, we cannot do anything. */ |
2902 | if (target != NULL) | |
e50d8076 | 2903 | { |
c13b1b77 NC |
2904 | if (command_line.endian == ENDIAN_BIG) |
2905 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 2906 | else |
c13b1b77 | 2907 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
2908 | |
2909 | /* See if the target has the wrong endianness. This should | |
2910 | not happen if the linker script has provided big and | |
2911 | little endian alternatives, but some scrips don't do | |
2912 | this. */ | |
c13b1b77 | 2913 | if (target->byteorder != desired_endian) |
e50d8076 | 2914 | { |
c13b1b77 NC |
2915 | /* If it does, then see if the target provides |
2916 | an alternative with the correct endianness. */ | |
2917 | if (target->alternative_target != NULL | |
2918 | && (target->alternative_target->byteorder == desired_endian)) | |
2919 | output_target = target->alternative_target->name; | |
e50d8076 | 2920 | else |
c13b1b77 | 2921 | { |
5f992e62 AM |
2922 | /* Try to find a target as similar as possible to |
2923 | the default target, but which has the desired | |
2924 | endian characteristic. */ | |
1579bae1 AM |
2925 | bfd_search_for_target (closest_target_match, |
2926 | (void *) target); | |
5f992e62 AM |
2927 | |
2928 | /* Oh dear - we could not find any targets that | |
2929 | satisfy our requirements. */ | |
c13b1b77 | 2930 | if (winner == NULL) |
6feb9908 AM |
2931 | einfo (_("%P: warning: could not find any targets" |
2932 | " that match endianness requirement\n")); | |
c13b1b77 NC |
2933 | else |
2934 | output_target = winner->name; | |
2935 | } | |
e50d8076 NC |
2936 | } |
2937 | } | |
252b5132 | 2938 | } |
5f992e62 | 2939 | |
f13a99db | 2940 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 2941 | |
f13a99db | 2942 | if (link_info.output_bfd == NULL) |
252b5132 RH |
2943 | { |
2944 | if (bfd_get_error () == bfd_error_invalid_target) | |
e50d8076 NC |
2945 | einfo (_("%P%F: target %s not found\n"), output_target); |
2946 | ||
252b5132 RH |
2947 | einfo (_("%P%F: cannot open output file %s: %E\n"), name); |
2948 | } | |
2949 | ||
b34976b6 | 2950 | delete_output_file_on_failure = TRUE; |
252b5132 | 2951 | |
f13a99db | 2952 | if (! bfd_set_format (link_info.output_bfd, bfd_object)) |
252b5132 | 2953 | einfo (_("%P%F:%s: can not make object file: %E\n"), name); |
f13a99db | 2954 | if (! bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
2955 | ldfile_output_architecture, |
2956 | ldfile_output_machine)) | |
2957 | einfo (_("%P%F:%s: can not set architecture: %E\n"), name); | |
2958 | ||
f13a99db | 2959 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 2960 | if (link_info.hash == NULL) |
384a9dda | 2961 | einfo (_("%P%F: can not create hash table: %E\n")); |
252b5132 | 2962 | |
f13a99db | 2963 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
2964 | } |
2965 | ||
252b5132 | 2966 | static void |
1579bae1 | 2967 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
2968 | { |
2969 | switch (statement->header.type) | |
2970 | { | |
2971 | case lang_output_statement_enum: | |
f13a99db AM |
2972 | ASSERT (link_info.output_bfd == NULL); |
2973 | open_output (statement->output_statement.name); | |
252b5132 | 2974 | ldemul_set_output_arch (); |
1049f94e | 2975 | if (config.magic_demand_paged && !link_info.relocatable) |
f13a99db | 2976 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 2977 | else |
f13a99db | 2978 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 2979 | if (config.text_read_only) |
f13a99db | 2980 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 2981 | else |
f13a99db | 2982 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 2983 | if (link_info.traditional_format) |
f13a99db | 2984 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 2985 | else |
f13a99db | 2986 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
2987 | break; |
2988 | ||
2989 | case lang_target_statement_enum: | |
2990 | current_target = statement->target_statement.target; | |
2991 | break; | |
2992 | default: | |
2993 | break; | |
2994 | } | |
2995 | } | |
2996 | ||
e5caa5e0 AM |
2997 | /* Convert between addresses in bytes and sizes in octets. |
2998 | For currently supported targets, octets_per_byte is always a power | |
2999 | of two, so we can use shifts. */ | |
3000 | #define TO_ADDR(X) ((X) >> opb_shift) | |
3001 | #define TO_SIZE(X) ((X) << opb_shift) | |
3002 | ||
3003 | /* Support the above. */ | |
3004 | static unsigned int opb_shift = 0; | |
3005 | ||
3006 | static void | |
3007 | init_opb (void) | |
3008 | { | |
3009 | unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3010 | ldfile_output_machine); | |
3011 | opb_shift = 0; | |
3012 | if (x > 1) | |
3013 | while ((x & 1) == 0) | |
3014 | { | |
3015 | x >>= 1; | |
3016 | ++opb_shift; | |
3017 | } | |
3018 | ASSERT (x == 1); | |
3019 | } | |
3020 | ||
252b5132 RH |
3021 | /* Open all the input files. */ |
3022 | ||
3023 | static void | |
1579bae1 | 3024 | open_input_bfds (lang_statement_union_type *s, bfd_boolean force) |
252b5132 | 3025 | { |
1579bae1 | 3026 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3027 | { |
3028 | switch (s->header.type) | |
3029 | { | |
3030 | case lang_constructors_statement_enum: | |
3031 | open_input_bfds (constructor_list.head, force); | |
3032 | break; | |
3033 | case lang_output_section_statement_enum: | |
3034 | open_input_bfds (s->output_section_statement.children.head, force); | |
3035 | break; | |
3036 | case lang_wild_statement_enum: | |
08da4cac | 3037 | /* Maybe we should load the file's symbols. */ |
252b5132 | 3038 | if (s->wild_statement.filename |
97407faf AM |
3039 | && !wildcardp (s->wild_statement.filename) |
3040 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3041 | lookup_name (s->wild_statement.filename); |
252b5132 RH |
3042 | open_input_bfds (s->wild_statement.children.head, force); |
3043 | break; | |
3044 | case lang_group_statement_enum: | |
3045 | { | |
3046 | struct bfd_link_hash_entry *undefs; | |
3047 | ||
3048 | /* We must continually search the entries in the group | |
08da4cac KH |
3049 | until no new symbols are added to the list of undefined |
3050 | symbols. */ | |
252b5132 RH |
3051 | |
3052 | do | |
3053 | { | |
3054 | undefs = link_info.hash->undefs_tail; | |
b34976b6 | 3055 | open_input_bfds (s->group_statement.children.head, TRUE); |
252b5132 RH |
3056 | } |
3057 | while (undefs != link_info.hash->undefs_tail); | |
3058 | } | |
3059 | break; | |
3060 | case lang_target_statement_enum: | |
3061 | current_target = s->target_statement.target; | |
3062 | break; | |
3063 | case lang_input_statement_enum: | |
e50d8076 | 3064 | if (s->input_statement.real) |
252b5132 | 3065 | { |
bde18da4 | 3066 | lang_statement_union_type **os_tail; |
252b5132 RH |
3067 | lang_statement_list_type add; |
3068 | ||
3069 | s->input_statement.target = current_target; | |
3070 | ||
3071 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3072 | is an archive which has already been searched, then |
3073 | force it to be researched unless the whole archive | |
cd4c806a | 3074 | has been loaded already. */ |
252b5132 | 3075 | if (force |
cd4c806a | 3076 | && !s->input_statement.whole_archive |
252b5132 RH |
3077 | && s->input_statement.loaded |
3078 | && bfd_check_format (s->input_statement.the_bfd, | |
3079 | bfd_archive)) | |
b34976b6 | 3080 | s->input_statement.loaded = FALSE; |
252b5132 | 3081 | |
bde18da4 | 3082 | os_tail = lang_output_section_statement.tail; |
d1778b88 | 3083 | lang_list_init (&add); |
1276aefa | 3084 | |
6770ec8c | 3085 | if (! load_symbols (&s->input_statement, &add)) |
b34976b6 | 3086 | config.make_executable = FALSE; |
252b5132 RH |
3087 | |
3088 | if (add.head != NULL) | |
3089 | { | |
bde18da4 AM |
3090 | /* If this was a script with output sections then |
3091 | tack any added statements on to the end of the | |
3092 | list. This avoids having to reorder the output | |
3093 | section statement list. Very likely the user | |
3094 | forgot -T, and whatever we do here will not meet | |
3095 | naive user expectations. */ | |
3096 | if (os_tail != lang_output_section_statement.tail) | |
3097 | { | |
3098 | einfo (_("%P: warning: %s contains output sections;" | |
3099 | " did you forget -T?\n"), | |
3100 | s->input_statement.filename); | |
3101 | *stat_ptr->tail = add.head; | |
3102 | stat_ptr->tail = add.tail; | |
3103 | } | |
3104 | else | |
3105 | { | |
3106 | *add.tail = s->header.next; | |
3107 | s->header.next = add.head; | |
3108 | } | |
252b5132 RH |
3109 | } |
3110 | } | |
3111 | break; | |
3112 | default: | |
3113 | break; | |
3114 | } | |
3115 | } | |
3116 | } | |
3117 | ||
420e579c HPN |
3118 | /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */ |
3119 | ||
3120 | void | |
3121 | lang_track_definedness (const char *name) | |
3122 | { | |
3123 | if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL) | |
3124 | einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name); | |
3125 | } | |
3126 | ||
3127 | /* New-function for the definedness hash table. */ | |
3128 | ||
3129 | static struct bfd_hash_entry * | |
3130 | lang_definedness_newfunc (struct bfd_hash_entry *entry, | |
3131 | struct bfd_hash_table *table ATTRIBUTE_UNUSED, | |
3132 | const char *name ATTRIBUTE_UNUSED) | |
3133 | { | |
3134 | struct lang_definedness_hash_entry *ret | |
3135 | = (struct lang_definedness_hash_entry *) entry; | |
3136 | ||
3137 | if (ret == NULL) | |
3138 | ret = (struct lang_definedness_hash_entry *) | |
3139 | bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry)); | |
3140 | ||
3141 | if (ret == NULL) | |
3142 | einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name); | |
3143 | ||
3144 | ret->iteration = -1; | |
3145 | return &ret->root; | |
3146 | } | |
3147 | ||
3148 | /* Return the iteration when the definition of NAME was last updated. A | |
3149 | value of -1 means that the symbol is not defined in the linker script | |
3150 | or the command line, but may be defined in the linker symbol table. */ | |
3151 | ||
3152 | int | |
3153 | lang_symbol_definition_iteration (const char *name) | |
3154 | { | |
3155 | struct lang_definedness_hash_entry *defentry | |
3156 | = (struct lang_definedness_hash_entry *) | |
3157 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
3158 | ||
3159 | /* We've already created this one on the presence of DEFINED in the | |
3160 | script, so it can't be NULL unless something is borked elsewhere in | |
3161 | the code. */ | |
3162 | if (defentry == NULL) | |
3163 | FAIL (); | |
3164 | ||
3165 | return defentry->iteration; | |
3166 | } | |
3167 | ||
3168 | /* Update the definedness state of NAME. */ | |
3169 | ||
3170 | void | |
3171 | lang_update_definedness (const char *name, struct bfd_link_hash_entry *h) | |
3172 | { | |
3173 | struct lang_definedness_hash_entry *defentry | |
3174 | = (struct lang_definedness_hash_entry *) | |
3175 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
3176 | ||
3177 | /* We don't keep track of symbols not tested with DEFINED. */ | |
3178 | if (defentry == NULL) | |
3179 | return; | |
3180 | ||
3181 | /* If the symbol was already defined, and not from an earlier statement | |
3182 | iteration, don't update the definedness iteration, because that'd | |
3183 | make the symbol seem defined in the linker script at this point, and | |
3184 | it wasn't; it was defined in some object. If we do anyway, DEFINED | |
3185 | would start to yield false before this point and the construct "sym = | |
3186 | DEFINED (sym) ? sym : X;" would change sym to X despite being defined | |
3187 | in an object. */ | |
3188 | if (h->type != bfd_link_hash_undefined | |
3189 | && h->type != bfd_link_hash_common | |
3190 | && h->type != bfd_link_hash_new | |
3191 | && defentry->iteration == -1) | |
3192 | return; | |
3193 | ||
3194 | defentry->iteration = lang_statement_iteration; | |
3195 | } | |
3196 | ||
08da4cac | 3197 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3198 | This is a two step process as the symbol table doesn't even exist at |
3199 | the time the ld command line is processed. First we put the name | |
3200 | on a list, then, once the output file has been opened, transfer the | |
3201 | name to the symbol table. */ | |
3202 | ||
e3e942e9 | 3203 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3204 | |
e3e942e9 | 3205 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3206 | |
3207 | void | |
1579bae1 | 3208 | ldlang_add_undef (const char *const name) |
252b5132 RH |
3209 | { |
3210 | ldlang_undef_chain_list_type *new = | |
1579bae1 | 3211 | stat_alloc (sizeof (ldlang_undef_chain_list_type)); |
252b5132 RH |
3212 | |
3213 | new->next = ldlang_undef_chain_list_head; | |
3214 | ldlang_undef_chain_list_head = new; | |
3215 | ||
d1b2b2dc | 3216 | new->name = xstrdup (name); |
fcf0e35b | 3217 | |
f13a99db | 3218 | if (link_info.output_bfd != NULL) |
fcf0e35b AM |
3219 | insert_undefined (new->name); |
3220 | } | |
3221 | ||
3222 | /* Insert NAME as undefined in the symbol table. */ | |
3223 | ||
3224 | static void | |
1579bae1 | 3225 | insert_undefined (const char *name) |
fcf0e35b AM |
3226 | { |
3227 | struct bfd_link_hash_entry *h; | |
3228 | ||
b34976b6 | 3229 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
1579bae1 | 3230 | if (h == NULL) |
fcf0e35b AM |
3231 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
3232 | if (h->type == bfd_link_hash_new) | |
3233 | { | |
3234 | h->type = bfd_link_hash_undefined; | |
3235 | h->u.undef.abfd = NULL; | |
3236 | bfd_link_add_undef (link_info.hash, h); | |
3237 | } | |
252b5132 RH |
3238 | } |
3239 | ||
3240 | /* Run through the list of undefineds created above and place them | |
3241 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3242 | script file. */ |
3243 | ||
252b5132 | 3244 | static void |
1579bae1 | 3245 | lang_place_undefineds (void) |
252b5132 RH |
3246 | { |
3247 | ldlang_undef_chain_list_type *ptr; | |
3248 | ||
1579bae1 AM |
3249 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3250 | insert_undefined (ptr->name); | |
252b5132 RH |
3251 | } |
3252 | ||
0841712e JJ |
3253 | /* Check for all readonly or some readwrite sections. */ |
3254 | ||
3255 | static void | |
6feb9908 AM |
3256 | check_input_sections |
3257 | (lang_statement_union_type *s, | |
3258 | lang_output_section_statement_type *output_section_statement) | |
0841712e JJ |
3259 | { |
3260 | for (; s != (lang_statement_union_type *) NULL; s = s->header.next) | |
3261 | { | |
3262 | switch (s->header.type) | |
967928e9 AM |
3263 | { |
3264 | case lang_wild_statement_enum: | |
3265 | walk_wild (&s->wild_statement, check_section_callback, | |
3266 | output_section_statement); | |
3267 | if (! output_section_statement->all_input_readonly) | |
3268 | return; | |
3269 | break; | |
3270 | case lang_constructors_statement_enum: | |
3271 | check_input_sections (constructor_list.head, | |
3272 | output_section_statement); | |
3273 | if (! output_section_statement->all_input_readonly) | |
3274 | return; | |
3275 | break; | |
3276 | case lang_group_statement_enum: | |
3277 | check_input_sections (s->group_statement.children.head, | |
3278 | output_section_statement); | |
3279 | if (! output_section_statement->all_input_readonly) | |
3280 | return; | |
3281 | break; | |
3282 | default: | |
3283 | break; | |
3284 | } | |
0841712e JJ |
3285 | } |
3286 | } | |
3287 | ||
bcaa7b3e L |
3288 | /* Update wildcard statements if needed. */ |
3289 | ||
3290 | static void | |
3291 | update_wild_statements (lang_statement_union_type *s) | |
3292 | { | |
3293 | struct wildcard_list *sec; | |
3294 | ||
3295 | switch (sort_section) | |
3296 | { | |
3297 | default: | |
3298 | FAIL (); | |
3299 | ||
3300 | case none: | |
3301 | break; | |
3302 | ||
3303 | case by_name: | |
3304 | case by_alignment: | |
3305 | for (; s != NULL; s = s->header.next) | |
3306 | { | |
3307 | switch (s->header.type) | |
3308 | { | |
3309 | default: | |
3310 | break; | |
3311 | ||
3312 | case lang_wild_statement_enum: | |
3313 | sec = s->wild_statement.section_list; | |
0d0999db L |
3314 | for (sec = s->wild_statement.section_list; sec != NULL; |
3315 | sec = sec->next) | |
bcaa7b3e L |
3316 | { |
3317 | switch (sec->spec.sorted) | |
3318 | { | |
3319 | case none: | |
3320 | sec->spec.sorted = sort_section; | |
3321 | break; | |
3322 | case by_name: | |
3323 | if (sort_section == by_alignment) | |
3324 | sec->spec.sorted = by_name_alignment; | |
3325 | break; | |
3326 | case by_alignment: | |
3327 | if (sort_section == by_name) | |
3328 | sec->spec.sorted = by_alignment_name; | |
3329 | break; | |
3330 | default: | |
3331 | break; | |
3332 | } | |
3333 | } | |
3334 | break; | |
3335 | ||
3336 | case lang_constructors_statement_enum: | |
3337 | update_wild_statements (constructor_list.head); | |
3338 | break; | |
3339 | ||
3340 | case lang_output_section_statement_enum: | |
3341 | update_wild_statements | |
3342 | (s->output_section_statement.children.head); | |
3343 | break; | |
3344 | ||
3345 | case lang_group_statement_enum: | |
3346 | update_wild_statements (s->group_statement.children.head); | |
3347 | break; | |
3348 | } | |
3349 | } | |
3350 | break; | |
3351 | } | |
3352 | } | |
3353 | ||
396a2467 | 3354 | /* Open input files and attach to output sections. */ |
08da4cac | 3355 | |
252b5132 | 3356 | static void |
1579bae1 AM |
3357 | map_input_to_output_sections |
3358 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 3359 | lang_output_section_statement_type *os) |
252b5132 | 3360 | { |
12d814e1 L |
3361 | flagword flags; |
3362 | ||
1579bae1 | 3363 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3364 | { |
3365 | switch (s->header.type) | |
3366 | { | |
252b5132 | 3367 | case lang_wild_statement_enum: |
afd7a018 | 3368 | wild (&s->wild_statement, target, os); |
abc6ab0a | 3369 | break; |
252b5132 RH |
3370 | case lang_constructors_statement_enum: |
3371 | map_input_to_output_sections (constructor_list.head, | |
3372 | target, | |
afd7a018 | 3373 | os); |
252b5132 RH |
3374 | break; |
3375 | case lang_output_section_statement_enum: | |
0841712e JJ |
3376 | if (s->output_section_statement.constraint) |
3377 | { | |
0cf7d72c AM |
3378 | if (s->output_section_statement.constraint != ONLY_IF_RW |
3379 | && s->output_section_statement.constraint != ONLY_IF_RO) | |
0841712e JJ |
3380 | break; |
3381 | s->output_section_statement.all_input_readonly = TRUE; | |
3382 | check_input_sections (s->output_section_statement.children.head, | |
3383 | &s->output_section_statement); | |
3384 | if ((s->output_section_statement.all_input_readonly | |
3385 | && s->output_section_statement.constraint == ONLY_IF_RW) | |
3386 | || (!s->output_section_statement.all_input_readonly | |
3387 | && s->output_section_statement.constraint == ONLY_IF_RO)) | |
3388 | { | |
3389 | s->output_section_statement.constraint = -1; | |
3390 | break; | |
3391 | } | |
3392 | } | |
3393 | ||
252b5132 RH |
3394 | map_input_to_output_sections (s->output_section_statement.children.head, |
3395 | target, | |
3396 | &s->output_section_statement); | |
3397 | break; | |
3398 | case lang_output_statement_enum: | |
3399 | break; | |
3400 | case lang_target_statement_enum: | |
3401 | target = s->target_statement.target; | |
3402 | break; | |
3403 | case lang_group_statement_enum: | |
3404 | map_input_to_output_sections (s->group_statement.children.head, | |
3405 | target, | |
afd7a018 | 3406 | os); |
252b5132 | 3407 | break; |
384d938f NS |
3408 | case lang_data_statement_enum: |
3409 | /* Make sure that any sections mentioned in the expression | |
3410 | are initialized. */ | |
3411 | exp_init_os (s->data_statement.exp); | |
12d814e1 | 3412 | flags = SEC_HAS_CONTENTS; |
afd7a018 AM |
3413 | /* The output section gets contents, and then we inspect for |
3414 | any flags set in the input script which override any ALLOC. */ | |
afd7a018 | 3415 | if (!(os->flags & SEC_NEVER_LOAD)) |
12d814e1 L |
3416 | flags |= SEC_ALLOC | SEC_LOAD; |
3417 | if (os->bfd_section == NULL) | |
3418 | init_os (os, NULL, flags); | |
3419 | else | |
3420 | os->bfd_section->flags |= flags; | |
afd7a018 | 3421 | break; |
252b5132 | 3422 | case lang_input_section_enum: |
e0f6802f AM |
3423 | break; |
3424 | case lang_fill_statement_enum: | |
252b5132 | 3425 | case lang_object_symbols_statement_enum: |
252b5132 RH |
3426 | case lang_reloc_statement_enum: |
3427 | case lang_padding_statement_enum: | |
3428 | case lang_input_statement_enum: | |
afd7a018 | 3429 | if (os != NULL && os->bfd_section == NULL) |
12d814e1 | 3430 | init_os (os, NULL, 0); |
252b5132 RH |
3431 | break; |
3432 | case lang_assignment_statement_enum: | |
afd7a018 | 3433 | if (os != NULL && os->bfd_section == NULL) |
12d814e1 | 3434 | init_os (os, NULL, 0); |
252b5132 RH |
3435 | |
3436 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 3437 | are initialized. */ |
252b5132 RH |
3438 | exp_init_os (s->assignment_statement.exp); |
3439 | break; | |
252b5132 | 3440 | case lang_address_statement_enum: |
329c1c86 | 3441 | /* Mark the specified section with the supplied address. |
ba916c8a MM |
3442 | If this section was actually a segment marker, then the |
3443 | directive is ignored if the linker script explicitly | |
3444 | processed the segment marker. Originally, the linker | |
3445 | treated segment directives (like -Ttext on the | |
3446 | command-line) as section directives. We honor the | |
3447 | section directive semantics for backwards compatibilty; | |
3448 | linker scripts that do not specifically check for | |
3449 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 3450 | if (!s->address_statement.segment |
ba916c8a MM |
3451 | || !s->address_statement.segment->used) |
3452 | { | |
3453 | lang_output_section_statement_type *aos | |
3454 | = (lang_output_section_statement_lookup | |
66c103b7 | 3455 | (s->address_statement.section_name, 0, TRUE)); |
329c1c86 | 3456 | |
ba916c8a | 3457 | if (aos->bfd_section == NULL) |
12d814e1 | 3458 | init_os (aos, NULL, 0); |
ba916c8a MM |
3459 | aos->addr_tree = s->address_statement.address; |
3460 | } | |
252b5132 | 3461 | break; |
53d25da6 AM |
3462 | case lang_insert_statement_enum: |
3463 | break; | |
3464 | } | |
3465 | } | |
3466 | } | |
3467 | ||
3468 | /* An insert statement snips out all the linker statements from the | |
3469 | start of the list and places them after the output section | |
3470 | statement specified by the insert. This operation is complicated | |
3471 | by the fact that we keep a doubly linked list of output section | |
3472 | statements as well as the singly linked list of all statements. */ | |
3473 | ||
3474 | static void | |
3475 | process_insert_statements (void) | |
3476 | { | |
3477 | lang_statement_union_type **s; | |
3478 | lang_output_section_statement_type *first_os = NULL; | |
3479 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 3480 | lang_output_section_statement_type *os; |
53d25da6 AM |
3481 | |
3482 | /* "start of list" is actually the statement immediately after | |
3483 | the special abs_section output statement, so that it isn't | |
3484 | reordered. */ | |
3485 | s = &lang_output_section_statement.head; | |
3486 | while (*(s = &(*s)->header.next) != NULL) | |
3487 | { | |
3488 | if ((*s)->header.type == lang_output_section_statement_enum) | |
3489 | { | |
3490 | /* Keep pointers to the first and last output section | |
3491 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
3492 | os = &(*s)->output_section_statement; |
3493 | ||
3494 | ASSERT (last_os == NULL || last_os->next == os); | |
3495 | last_os = os; | |
66c103b7 AM |
3496 | |
3497 | /* Set constraint negative so that lang_output_section_find | |
3498 | won't match this output section statement. At this | |
3499 | stage in linking constraint has values in the range | |
3500 | [-1, ONLY_IN_RW]. */ | |
3501 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
3502 | if (first_os == NULL) |
3503 | first_os = last_os; | |
3504 | } | |
3505 | else if ((*s)->header.type == lang_insert_statement_enum) | |
3506 | { | |
3507 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
3508 | lang_output_section_statement_type *where; | |
53d25da6 AM |
3509 | lang_statement_union_type **ptr; |
3510 | lang_statement_union_type *first; | |
3511 | ||
3512 | where = lang_output_section_find (i->where); | |
3513 | if (where != NULL && i->is_before) | |
3514 | { | |
967928e9 | 3515 | do |
53d25da6 | 3516 | where = where->prev; |
66c103b7 | 3517 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
3518 | } |
3519 | if (where == NULL) | |
3520 | { | |
66c103b7 AM |
3521 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
3522 | return; | |
53d25da6 AM |
3523 | } |
3524 | ||
3525 | /* Deal with reordering the output section statement list. */ | |
3526 | if (last_os != NULL) | |
3527 | { | |
3528 | asection *first_sec, *last_sec; | |
29183214 | 3529 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
3530 | |
3531 | /* Snip out the output sections we are moving. */ | |
3532 | first_os->prev->next = last_os->next; | |
3533 | if (last_os->next == NULL) | |
29183214 L |
3534 | { |
3535 | next = &first_os->prev->next; | |
3536 | lang_output_section_statement.tail | |
3537 | = (lang_statement_union_type **) next; | |
3538 | } | |
53d25da6 AM |
3539 | else |
3540 | last_os->next->prev = first_os->prev; | |
3541 | /* Add them in at the new position. */ | |
3542 | last_os->next = where->next; | |
3543 | if (where->next == NULL) | |
29183214 L |
3544 | { |
3545 | next = &last_os->next; | |
3546 | lang_output_section_statement.tail | |
3547 | = (lang_statement_union_type **) next; | |
3548 | } | |
53d25da6 AM |
3549 | else |
3550 | where->next->prev = last_os; | |
3551 | first_os->prev = where; | |
3552 | where->next = first_os; | |
3553 | ||
3554 | /* Move the bfd sections in the same way. */ | |
3555 | first_sec = NULL; | |
3556 | last_sec = NULL; | |
3557 | for (os = first_os; os != NULL; os = os->next) | |
3558 | { | |
66c103b7 | 3559 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
3560 | if (os->bfd_section != NULL |
3561 | && os->bfd_section->owner != NULL) | |
3562 | { | |
3563 | last_sec = os->bfd_section; | |
3564 | if (first_sec == NULL) | |
3565 | first_sec = last_sec; | |
3566 | } | |
3567 | if (os == last_os) | |
3568 | break; | |
3569 | } | |
3570 | if (last_sec != NULL) | |
3571 | { | |
3572 | asection *sec = where->bfd_section; | |
3573 | if (sec == NULL) | |
3574 | sec = output_prev_sec_find (where); | |
3575 | ||
3576 | /* The place we want to insert must come after the | |
3577 | sections we are moving. So if we find no | |
3578 | section or if the section is the same as our | |
3579 | last section, then no move is needed. */ | |
3580 | if (sec != NULL && sec != last_sec) | |
3581 | { | |
3582 | /* Trim them off. */ | |
3583 | if (first_sec->prev != NULL) | |
3584 | first_sec->prev->next = last_sec->next; | |
3585 | else | |
f13a99db | 3586 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
3587 | if (last_sec->next != NULL) |
3588 | last_sec->next->prev = first_sec->prev; | |
3589 | else | |
f13a99db | 3590 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 AM |
3591 | /* Add back. */ |
3592 | last_sec->next = sec->next; | |
3593 | if (sec->next != NULL) | |
3594 | sec->next->prev = last_sec; | |
3595 | else | |
f13a99db | 3596 | link_info.output_bfd->section_last = last_sec; |
53d25da6 AM |
3597 | first_sec->prev = sec; |
3598 | sec->next = first_sec; | |
3599 | } | |
3600 | } | |
3601 | ||
3602 | first_os = NULL; | |
3603 | last_os = NULL; | |
3604 | } | |
3605 | ||
3606 | ptr = insert_os_after (where); | |
3607 | /* Snip everything after the abs_section output statement we | |
3608 | know is at the start of the list, up to and including | |
3609 | the insert statement we are currently processing. */ | |
3610 | first = lang_output_section_statement.head->header.next; | |
3611 | lang_output_section_statement.head->header.next = (*s)->header.next; | |
3612 | /* Add them back where they belong. */ | |
3613 | *s = *ptr; | |
3614 | if (*s == NULL) | |
3615 | statement_list.tail = s; | |
3616 | *ptr = first; | |
3617 | s = &lang_output_section_statement.head; | |
252b5132 RH |
3618 | } |
3619 | } | |
66c103b7 AM |
3620 | |
3621 | /* Undo constraint twiddling. */ | |
3622 | for (os = first_os; os != NULL; os = os->next) | |
3623 | { | |
3624 | os->constraint = -2 - os->constraint; | |
3625 | if (os == last_os) | |
3626 | break; | |
3627 | } | |
252b5132 RH |
3628 | } |
3629 | ||
4bd5a393 AM |
3630 | /* An output section might have been removed after its statement was |
3631 | added. For example, ldemul_before_allocation can remove dynamic | |
3632 | sections if they turn out to be not needed. Clean them up here. */ | |
3633 | ||
8423293d | 3634 | void |
1579bae1 | 3635 | strip_excluded_output_sections (void) |
4bd5a393 | 3636 | { |
afd7a018 | 3637 | lang_output_section_statement_type *os; |
4bd5a393 | 3638 | |
046183de | 3639 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
3640 | if (expld.phase != lang_mark_phase_enum) |
3641 | { | |
3642 | expld.phase = lang_mark_phase_enum; | |
3643 | expld.dataseg.phase = exp_dataseg_none; | |
3644 | one_lang_size_sections_pass (NULL, FALSE); | |
3645 | lang_reset_memory_regions (); | |
3646 | } | |
3647 | ||
afd7a018 AM |
3648 | for (os = &lang_output_section_statement.head->output_section_statement; |
3649 | os != NULL; | |
3650 | os = os->next) | |
4bd5a393 | 3651 | { |
75ff4589 L |
3652 | asection *output_section; |
3653 | bfd_boolean exclude; | |
4bd5a393 | 3654 | |
66c103b7 | 3655 | if (os->constraint < 0) |
0841712e | 3656 | continue; |
8423293d | 3657 | |
75ff4589 L |
3658 | output_section = os->bfd_section; |
3659 | if (output_section == NULL) | |
8423293d AM |
3660 | continue; |
3661 | ||
32124d5b AM |
3662 | exclude = (output_section->rawsize == 0 |
3663 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 3664 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
3665 | output_section)); |
3666 | ||
3667 | /* Some sections have not yet been sized, notably .gnu.version, | |
3668 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
3669 | input sections, so don't drop output sections that have such | |
3670 | input sections unless they are also marked SEC_EXCLUDE. */ | |
3671 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
3672 | { |
3673 | asection *s; | |
8423293d | 3674 | |
32124d5b AM |
3675 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
3676 | if ((s->flags & SEC_LINKER_CREATED) != 0 | |
3677 | && (s->flags & SEC_EXCLUDE) == 0) | |
e9ee469a AM |
3678 | { |
3679 | exclude = FALSE; | |
3680 | break; | |
3681 | } | |
75ff4589 | 3682 | } |
8423293d | 3683 | |
32124d5b AM |
3684 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ |
3685 | output_section->map_head.link_order = NULL; | |
3686 | output_section->map_tail.link_order = NULL; | |
3687 | ||
3688 | if (exclude) | |
4bd5a393 | 3689 | { |
e9ee469a AM |
3690 | /* We don't set bfd_section to NULL since bfd_section of the |
3691 | removed output section statement may still be used. */ | |
3d263d86 AM |
3692 | if (!os->section_relative_symbol |
3693 | && !os->update_dot_tree) | |
74541ad4 | 3694 | os->ignored = TRUE; |
e9ee469a | 3695 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
3696 | bfd_section_list_remove (link_info.output_bfd, output_section); |
3697 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
3698 | } |
3699 | } | |
8423293d AM |
3700 | |
3701 | /* Stop future calls to lang_add_section from messing with map_head | |
3702 | and map_tail link_order fields. */ | |
3703 | stripped_excluded_sections = TRUE; | |
4bd5a393 AM |
3704 | } |
3705 | ||
252b5132 | 3706 | static void |
1579bae1 AM |
3707 | print_output_section_statement |
3708 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
3709 | { |
3710 | asection *section = output_section_statement->bfd_section; | |
3711 | int len; | |
3712 | ||
3713 | if (output_section_statement != abs_output_section) | |
3714 | { | |
3715 | minfo ("\n%s", output_section_statement->name); | |
3716 | ||
3717 | if (section != NULL) | |
3718 | { | |
3719 | print_dot = section->vma; | |
3720 | ||
3721 | len = strlen (output_section_statement->name); | |
3722 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
3723 | { | |
3724 | print_nl (); | |
3725 | len = 0; | |
3726 | } | |
3727 | while (len < SECTION_NAME_MAP_LENGTH) | |
3728 | { | |
3729 | print_space (); | |
3730 | ++len; | |
3731 | } | |
3732 | ||
eea6121a | 3733 | minfo ("0x%V %W", section->vma, section->size); |
252b5132 | 3734 | |
67f744f3 AM |
3735 | if (section->vma != section->lma) |
3736 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
3737 | |
3738 | if (output_section_statement->update_dot_tree != NULL) | |
3739 | exp_fold_tree (output_section_statement->update_dot_tree, | |
3740 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
3741 | } |
3742 | ||
3743 | print_nl (); | |
3744 | } | |
3745 | ||
3746 | print_statement_list (output_section_statement->children.head, | |
3747 | output_section_statement); | |
3748 | } | |
3749 | ||
3b83e13a NC |
3750 | /* Scan for the use of the destination in the right hand side |
3751 | of an expression. In such cases we will not compute the | |
3752 | correct expression, since the value of DST that is used on | |
3753 | the right hand side will be its final value, not its value | |
3754 | just before this expression is evaluated. */ | |
329c1c86 | 3755 | |
3b83e13a NC |
3756 | static bfd_boolean |
3757 | scan_for_self_assignment (const char * dst, etree_type * rhs) | |
3758 | { | |
3759 | if (rhs == NULL || dst == NULL) | |
3760 | return FALSE; | |
3761 | ||
3762 | switch (rhs->type.node_class) | |
3763 | { | |
3764 | case etree_binary: | |
3765 | return scan_for_self_assignment (dst, rhs->binary.lhs) | |
3766 | || scan_for_self_assignment (dst, rhs->binary.rhs); | |
3767 | ||
3768 | case etree_trinary: | |
3769 | return scan_for_self_assignment (dst, rhs->trinary.lhs) | |
3770 | || scan_for_self_assignment (dst, rhs->trinary.rhs); | |
3771 | ||
3772 | case etree_assign: | |
3773 | case etree_provided: | |
3774 | case etree_provide: | |
3775 | if (strcmp (dst, rhs->assign.dst) == 0) | |
3776 | return TRUE; | |
3777 | return scan_for_self_assignment (dst, rhs->assign.src); | |
3778 | ||
3779 | case etree_unary: | |
3780 | return scan_for_self_assignment (dst, rhs->unary.child); | |
3781 | ||
3782 | case etree_value: | |
3783 | if (rhs->value.str) | |
3784 | return strcmp (dst, rhs->value.str) == 0; | |
3785 | return FALSE; | |
3786 | ||
3787 | case etree_name: | |
3788 | if (rhs->name.name) | |
3789 | return strcmp (dst, rhs->name.name) == 0; | |
3790 | return FALSE; | |
3791 | ||
3792 | default: | |
3793 | break; | |
3794 | } | |
3795 | ||
3796 | return FALSE; | |
3797 | } | |
3798 | ||
3799 | ||
252b5132 | 3800 | static void |
1579bae1 AM |
3801 | print_assignment (lang_assignment_statement_type *assignment, |
3802 | lang_output_section_statement_type *output_section) | |
252b5132 | 3803 | { |
3b83e13a NC |
3804 | unsigned int i; |
3805 | bfd_boolean is_dot; | |
3806 | bfd_boolean computation_is_valid = TRUE; | |
afd7a018 | 3807 | etree_type *tree; |
252b5132 RH |
3808 | |
3809 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
3810 | print_space (); | |
3811 | ||
afd7a018 AM |
3812 | if (assignment->exp->type.node_class == etree_assert) |
3813 | { | |
3b83e13a | 3814 | is_dot = FALSE; |
afd7a018 | 3815 | tree = assignment->exp->assert_s.child; |
3b83e13a | 3816 | computation_is_valid = TRUE; |
afd7a018 AM |
3817 | } |
3818 | else | |
3819 | { | |
3820 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
3821 | |
3822 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
afd7a018 | 3823 | tree = assignment->exp->assign.src; |
3b83e13a | 3824 | computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE); |
afd7a018 AM |
3825 | } |
3826 | ||
e9ee469a AM |
3827 | exp_fold_tree (tree, output_section->bfd_section, &print_dot); |
3828 | if (expld.result.valid_p) | |
7b17f854 | 3829 | { |
7b17f854 RS |
3830 | bfd_vma value; |
3831 | ||
3b83e13a NC |
3832 | if (computation_is_valid) |
3833 | { | |
e9ee469a | 3834 | value = expld.result.value; |
10dbd1f3 | 3835 | |
e9ee469a AM |
3836 | if (expld.result.section) |
3837 | value += expld.result.section->vma; | |
7b17f854 | 3838 | |
3b83e13a NC |
3839 | minfo ("0x%V", value); |
3840 | if (is_dot) | |
3841 | print_dot = value; | |
3842 | } | |
3843 | else | |
3844 | { | |
3845 | struct bfd_link_hash_entry *h; | |
3846 | ||
3847 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
3848 | FALSE, FALSE, TRUE); | |
3849 | if (h) | |
3850 | { | |
10dbd1f3 NC |
3851 | value = h->u.def.value; |
3852 | ||
e9ee469a | 3853 | if (expld.result.section) |
a1438fce | 3854 | value += expld.result.section->vma; |
3b83e13a NC |
3855 | |
3856 | minfo ("[0x%V]", value); | |
3857 | } | |
3858 | else | |
3859 | minfo ("[unresolved]"); | |
3860 | } | |
7b17f854 | 3861 | } |
252b5132 RH |
3862 | else |
3863 | { | |
3864 | minfo ("*undef* "); | |
3865 | #ifdef BFD64 | |
3866 | minfo (" "); | |
3867 | #endif | |
3868 | } | |
3869 | ||
3870 | minfo (" "); | |
252b5132 | 3871 | exp_print_tree (assignment->exp); |
252b5132 RH |
3872 | print_nl (); |
3873 | } | |
3874 | ||
3875 | static void | |
1579bae1 | 3876 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 3877 | { |
967928e9 AM |
3878 | if (statm->filename != NULL |
3879 | && (statm->the_bfd == NULL | |
3880 | || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0)) | |
906e58ca | 3881 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
3882 | } |
3883 | ||
3884 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 3885 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 3886 | |
b34976b6 | 3887 | static bfd_boolean |
1579bae1 | 3888 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 3889 | { |
1579bae1 | 3890 | asection *sec = ptr; |
252b5132 RH |
3891 | |
3892 | if ((hash_entry->type == bfd_link_hash_defined | |
3893 | || hash_entry->type == bfd_link_hash_defweak) | |
3894 | && sec == hash_entry->u.def.section) | |
3895 | { | |
3896 | int i; | |
3897 | ||
3898 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
3899 | print_space (); | |
3900 | minfo ("0x%V ", | |
3901 | (hash_entry->u.def.value | |
3902 | + hash_entry->u.def.section->output_offset | |
3903 | + hash_entry->u.def.section->output_section->vma)); | |
3904 | ||
3905 | minfo (" %T\n", hash_entry->root.string); | |
3906 | } | |
3907 | ||
b34976b6 | 3908 | return TRUE; |
252b5132 RH |
3909 | } |
3910 | ||
35835446 | 3911 | static void |
e0f6802f | 3912 | print_all_symbols (asection *sec) |
35835446 JR |
3913 | { |
3914 | struct fat_user_section_struct *ud = get_userdata (sec); | |
3915 | struct map_symbol_def *def; | |
3916 | ||
afd7a018 AM |
3917 | if (!ud) |
3918 | return; | |
3919 | ||
35835446 JR |
3920 | *ud->map_symbol_def_tail = 0; |
3921 | for (def = ud->map_symbol_def_head; def; def = def->next) | |
3922 | print_one_symbol (def->entry, sec); | |
3923 | } | |
3924 | ||
252b5132 RH |
3925 | /* Print information about an input section to the map file. */ |
3926 | ||
3927 | static void | |
4d4920ec | 3928 | print_input_section (asection *i) |
252b5132 | 3929 | { |
eea6121a | 3930 | bfd_size_type size = i->size; |
abe6ac95 AM |
3931 | int len; |
3932 | bfd_vma addr; | |
e5caa5e0 AM |
3933 | |
3934 | init_opb (); | |
4d4920ec | 3935 | |
abe6ac95 AM |
3936 | print_space (); |
3937 | minfo ("%s", i->name); | |
252b5132 | 3938 | |
abe6ac95 AM |
3939 | len = 1 + strlen (i->name); |
3940 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
3941 | { | |
3942 | print_nl (); | |
3943 | len = 0; | |
3944 | } | |
3945 | while (len < SECTION_NAME_MAP_LENGTH) | |
3946 | { | |
57ceae94 | 3947 | print_space (); |
abe6ac95 AM |
3948 | ++len; |
3949 | } | |
252b5132 | 3950 | |
f13a99db AM |
3951 | if (i->output_section != NULL |
3952 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
3953 | addr = i->output_section->vma + i->output_offset; |
3954 | else | |
3955 | { | |
3956 | addr = print_dot; | |
3957 | size = 0; | |
3958 | } | |
252b5132 | 3959 | |
abe6ac95 | 3960 | minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner); |
252b5132 | 3961 | |
abe6ac95 AM |
3962 | if (size != i->rawsize && i->rawsize != 0) |
3963 | { | |
3964 | len = SECTION_NAME_MAP_LENGTH + 3; | |
252b5132 | 3965 | #ifdef BFD64 |
abe6ac95 | 3966 | len += 16; |
252b5132 | 3967 | #else |
abe6ac95 | 3968 | len += 8; |
252b5132 | 3969 | #endif |
abe6ac95 AM |
3970 | while (len > 0) |
3971 | { | |
3972 | print_space (); | |
3973 | --len; | |
57ceae94 AM |
3974 | } |
3975 | ||
abe6ac95 AM |
3976 | minfo (_("%W (size before relaxing)\n"), i->rawsize); |
3977 | } | |
252b5132 | 3978 | |
f13a99db AM |
3979 | if (i->output_section != NULL |
3980 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 3981 | { |
c0f00686 | 3982 | if (link_info.reduce_memory_overheads) |
abe6ac95 AM |
3983 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); |
3984 | else | |
3985 | print_all_symbols (i); | |
3986 | ||
5590fba9 NC |
3987 | /* Update print_dot, but make sure that we do not move it |
3988 | backwards - this could happen if we have overlays and a | |
3989 | later overlay is shorter than an earier one. */ | |
3990 | if (addr + TO_ADDR (size) > print_dot) | |
3991 | print_dot = addr + TO_ADDR (size); | |
252b5132 RH |
3992 | } |
3993 | } | |
3994 | ||
3995 | static void | |
1579bae1 | 3996 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 3997 | { |
2c382fb6 AM |
3998 | size_t size; |
3999 | unsigned char *p; | |
4000 | fputs (" FILL mask 0x", config.map_file); | |
4001 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4002 | fprintf (config.map_file, "%02x", *p); | |
4003 | fputs ("\n", config.map_file); | |
252b5132 RH |
4004 | } |
4005 | ||
4006 | static void | |
1579bae1 | 4007 | print_data_statement (lang_data_statement_type *data) |
252b5132 RH |
4008 | { |
4009 | int i; | |
4010 | bfd_vma addr; | |
4011 | bfd_size_type size; | |
4012 | const char *name; | |
4013 | ||
e5caa5e0 | 4014 | init_opb (); |
252b5132 RH |
4015 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4016 | print_space (); | |
4017 | ||
7fabd029 | 4018 | addr = data->output_offset; |
252b5132 RH |
4019 | if (data->output_section != NULL) |
4020 | addr += data->output_section->vma; | |
4021 | ||
4022 | switch (data->type) | |
4023 | { | |
4024 | default: | |
4025 | abort (); | |
4026 | case BYTE: | |
4027 | size = BYTE_SIZE; | |
4028 | name = "BYTE"; | |
4029 | break; | |
4030 | case SHORT: | |
4031 | size = SHORT_SIZE; | |
4032 | name = "SHORT"; | |
4033 | break; | |
4034 | case LONG: | |
4035 | size = LONG_SIZE; | |
4036 | name = "LONG"; | |
4037 | break; | |
4038 | case QUAD: | |
4039 | size = QUAD_SIZE; | |
4040 | name = "QUAD"; | |
4041 | break; | |
4042 | case SQUAD: | |
4043 | size = QUAD_SIZE; | |
4044 | name = "SQUAD"; | |
4045 | break; | |
4046 | } | |
4047 | ||
4048 | minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); | |
4049 | ||
4050 | if (data->exp->type.node_class != etree_value) | |
4051 | { | |
4052 | print_space (); | |
4053 | exp_print_tree (data->exp); | |
4054 | } | |
4055 | ||
4056 | print_nl (); | |
4057 | ||
e5caa5e0 | 4058 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
4059 | } |
4060 | ||
4061 | /* Print an address statement. These are generated by options like | |
4062 | -Ttext. */ | |
4063 | ||
4064 | static void | |
1579bae1 | 4065 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
4066 | { |
4067 | minfo (_("Address of section %s set to "), address->section_name); | |
4068 | exp_print_tree (address->address); | |
4069 | print_nl (); | |
4070 | } | |
4071 | ||
4072 | /* Print a reloc statement. */ | |
4073 | ||
4074 | static void | |
1579bae1 | 4075 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 RH |
4076 | { |
4077 | int i; | |
4078 | bfd_vma addr; | |
4079 | bfd_size_type size; | |
4080 | ||
e5caa5e0 | 4081 | init_opb (); |
252b5132 RH |
4082 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4083 | print_space (); | |
4084 | ||
7fabd029 | 4085 | addr = reloc->output_offset; |
252b5132 RH |
4086 | if (reloc->output_section != NULL) |
4087 | addr += reloc->output_section->vma; | |
4088 | ||
4089 | size = bfd_get_reloc_size (reloc->howto); | |
4090 | ||
4091 | minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); | |
4092 | ||
4093 | if (reloc->name != NULL) | |
4094 | minfo ("%s+", reloc->name); | |
4095 | else | |
4096 | minfo ("%s+", reloc->section->name); | |
4097 | ||
4098 | exp_print_tree (reloc->addend_exp); | |
4099 | ||
4100 | print_nl (); | |
4101 | ||
e5caa5e0 | 4102 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 4103 | } |
252b5132 RH |
4104 | |
4105 | static void | |
1579bae1 | 4106 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
4107 | { |
4108 | int len; | |
4109 | bfd_vma addr; | |
4110 | ||
e5caa5e0 | 4111 | init_opb (); |
252b5132 RH |
4112 | minfo (" *fill*"); |
4113 | ||
4114 | len = sizeof " *fill*" - 1; | |
4115 | while (len < SECTION_NAME_MAP_LENGTH) | |
4116 | { | |
4117 | print_space (); | |
4118 | ++len; | |
4119 | } | |
4120 | ||
4121 | addr = s->output_offset; | |
4122 | if (s->output_section != NULL) | |
4123 | addr += s->output_section->vma; | |
5f9b8920 | 4124 | minfo ("0x%V %W ", addr, (bfd_vma) s->size); |
252b5132 | 4125 | |
2c382fb6 AM |
4126 | if (s->fill->size != 0) |
4127 | { | |
4128 | size_t size; | |
4129 | unsigned char *p; | |
4130 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
4131 | fprintf (config.map_file, "%02x", *p); | |
4132 | } | |
252b5132 RH |
4133 | |
4134 | print_nl (); | |
4135 | ||
e5caa5e0 | 4136 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
4137 | } |
4138 | ||
4139 | static void | |
1579bae1 AM |
4140 | print_wild_statement (lang_wild_statement_type *w, |
4141 | lang_output_section_statement_type *os) | |
252b5132 | 4142 | { |
b6bf44ba AM |
4143 | struct wildcard_list *sec; |
4144 | ||
252b5132 RH |
4145 | print_space (); |
4146 | ||
4147 | if (w->filenames_sorted) | |
4148 | minfo ("SORT("); | |
08da4cac | 4149 | if (w->filename != NULL) |
252b5132 RH |
4150 | minfo ("%s", w->filename); |
4151 | else | |
4152 | minfo ("*"); | |
4153 | if (w->filenames_sorted) | |
4154 | minfo (")"); | |
4155 | ||
4156 | minfo ("("); | |
b6bf44ba AM |
4157 | for (sec = w->section_list; sec; sec = sec->next) |
4158 | { | |
4159 | if (sec->spec.sorted) | |
4160 | minfo ("SORT("); | |
4161 | if (sec->spec.exclude_name_list != NULL) | |
4162 | { | |
4163 | name_list *tmp; | |
34786259 | 4164 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 4165 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
4166 | minfo (" %s", tmp->name); |
4167 | minfo (") "); | |
b6bf44ba AM |
4168 | } |
4169 | if (sec->spec.name != NULL) | |
4170 | minfo ("%s", sec->spec.name); | |
4171 | else | |
4172 | minfo ("*"); | |
4173 | if (sec->spec.sorted) | |
4174 | minfo (")"); | |
34786259 AM |
4175 | if (sec->next) |
4176 | minfo (" "); | |
b6bf44ba | 4177 | } |
252b5132 RH |
4178 | minfo (")"); |
4179 | ||
4180 | print_nl (); | |
4181 | ||
4182 | print_statement_list (w->children.head, os); | |
4183 | } | |
4184 | ||
4185 | /* Print a group statement. */ | |
4186 | ||
4187 | static void | |
1579bae1 AM |
4188 | print_group (lang_group_statement_type *s, |
4189 | lang_output_section_statement_type *os) | |
252b5132 RH |
4190 | { |
4191 | fprintf (config.map_file, "START GROUP\n"); | |
4192 | print_statement_list (s->children.head, os); | |
4193 | fprintf (config.map_file, "END GROUP\n"); | |
4194 | } | |
4195 | ||
4196 | /* Print the list of statements in S. | |
4197 | This can be called for any statement type. */ | |
4198 | ||
4199 | static void | |
1579bae1 AM |
4200 | print_statement_list (lang_statement_union_type *s, |
4201 | lang_output_section_statement_type *os) | |
252b5132 RH |
4202 | { |
4203 | while (s != NULL) | |
4204 | { | |
4205 | print_statement (s, os); | |
bba1a0c0 | 4206 | s = s->header.next; |
252b5132 RH |
4207 | } |
4208 | } | |
4209 | ||
4210 | /* Print the first statement in statement list S. | |
4211 | This can be called for any statement type. */ | |
4212 | ||
4213 | static void | |
1579bae1 AM |
4214 | print_statement (lang_statement_union_type *s, |
4215 | lang_output_section_statement_type *os) | |
252b5132 RH |
4216 | { |
4217 | switch (s->header.type) | |
4218 | { | |
4219 | default: | |
4220 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
4221 | FAIL (); | |
4222 | break; | |
4223 | case lang_constructors_statement_enum: | |
4224 | if (constructor_list.head != NULL) | |
4225 | { | |
4226 | if (constructors_sorted) | |
4227 | minfo (" SORT (CONSTRUCTORS)\n"); | |
4228 | else | |
4229 | minfo (" CONSTRUCTORS\n"); | |
4230 | print_statement_list (constructor_list.head, os); | |
4231 | } | |
4232 | break; | |
4233 | case lang_wild_statement_enum: | |
4234 | print_wild_statement (&s->wild_statement, os); | |
4235 | break; | |
4236 | case lang_address_statement_enum: | |
4237 | print_address_statement (&s->address_statement); | |
4238 | break; | |
4239 | case lang_object_symbols_statement_enum: | |
4240 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
4241 | break; | |
4242 | case lang_fill_statement_enum: | |
4243 | print_fill_statement (&s->fill_statement); | |
4244 | break; | |
4245 | case lang_data_statement_enum: | |
4246 | print_data_statement (&s->data_statement); | |
4247 | break; | |
4248 | case lang_reloc_statement_enum: | |
4249 | print_reloc_statement (&s->reloc_statement); | |
4250 | break; | |
4251 | case lang_input_section_enum: | |
4d4920ec | 4252 | print_input_section (s->input_section.section); |
252b5132 RH |
4253 | break; |
4254 | case lang_padding_statement_enum: | |
4255 | print_padding_statement (&s->padding_statement); | |
4256 | break; | |
4257 | case lang_output_section_statement_enum: | |
4258 | print_output_section_statement (&s->output_section_statement); | |
4259 | break; | |
4260 | case lang_assignment_statement_enum: | |
4261 | print_assignment (&s->assignment_statement, os); | |
4262 | break; | |
4263 | case lang_target_statement_enum: | |
4264 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
4265 | break; | |
4266 | case lang_output_statement_enum: | |
4267 | minfo ("OUTPUT(%s", s->output_statement.name); | |
4268 | if (output_target != NULL) | |
4269 | minfo (" %s", output_target); | |
4270 | minfo (")\n"); | |
4271 | break; | |
4272 | case lang_input_statement_enum: | |
4273 | print_input_statement (&s->input_statement); | |
4274 | break; | |
4275 | case lang_group_statement_enum: | |
4276 | print_group (&s->group_statement, os); | |
4277 | break; | |
53d25da6 AM |
4278 | case lang_insert_statement_enum: |
4279 | minfo ("INSERT %s %s\n", | |
4280 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
4281 | s->insert_statement.where); | |
4282 | break; | |
252b5132 RH |
4283 | } |
4284 | } | |
4285 | ||
4286 | static void | |
1579bae1 | 4287 | print_statements (void) |
252b5132 RH |
4288 | { |
4289 | print_statement_list (statement_list.head, abs_output_section); | |
4290 | } | |
4291 | ||
4292 | /* Print the first N statements in statement list S to STDERR. | |
4293 | If N == 0, nothing is printed. | |
4294 | If N < 0, the entire list is printed. | |
4295 | Intended to be called from GDB. */ | |
4296 | ||
4297 | void | |
1579bae1 | 4298 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
4299 | { |
4300 | FILE *map_save = config.map_file; | |
4301 | ||
4302 | config.map_file = stderr; | |
4303 | ||
4304 | if (n < 0) | |
4305 | print_statement_list (s, abs_output_section); | |
4306 | else | |
4307 | { | |
4308 | while (s && --n >= 0) | |
4309 | { | |
4310 | print_statement (s, abs_output_section); | |
bba1a0c0 | 4311 | s = s->header.next; |
252b5132 RH |
4312 | } |
4313 | } | |
4314 | ||
4315 | config.map_file = map_save; | |
4316 | } | |
4317 | ||
b3327aad | 4318 | static void |
1579bae1 AM |
4319 | insert_pad (lang_statement_union_type **ptr, |
4320 | fill_type *fill, | |
4321 | unsigned int alignment_needed, | |
4322 | asection *output_section, | |
4323 | bfd_vma dot) | |
252b5132 | 4324 | { |
2c382fb6 | 4325 | static fill_type zero_fill = { 1, { 0 } }; |
e9ee469a | 4326 | lang_statement_union_type *pad = NULL; |
b3327aad | 4327 | |
e9ee469a AM |
4328 | if (ptr != &statement_list.head) |
4329 | pad = ((lang_statement_union_type *) | |
4330 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
4331 | if (pad != NULL | |
4332 | && pad->header.type == lang_padding_statement_enum | |
4333 | && pad->padding_statement.output_section == output_section) | |
4334 | { | |
4335 | /* Use the existing pad statement. */ | |
4336 | } | |
4337 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
4338 | && pad->header.type == lang_padding_statement_enum |
4339 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 4340 | { |
e9ee469a | 4341 | /* Use the existing pad statement. */ |
252b5132 | 4342 | } |
b3327aad | 4343 | else |
252b5132 | 4344 | { |
b3327aad | 4345 | /* Make a new padding statement, linked into existing chain. */ |
1579bae1 | 4346 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
4347 | pad->header.next = *ptr; |
4348 | *ptr = pad; | |
4349 | pad->header.type = lang_padding_statement_enum; | |
4350 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 4351 | if (fill == NULL) |
2c382fb6 | 4352 | fill = &zero_fill; |
b3327aad | 4353 | pad->padding_statement.fill = fill; |
252b5132 | 4354 | } |
b3327aad AM |
4355 | pad->padding_statement.output_offset = dot - output_section->vma; |
4356 | pad->padding_statement.size = alignment_needed; | |
eea6121a | 4357 | output_section->size += alignment_needed; |
252b5132 RH |
4358 | } |
4359 | ||
08da4cac KH |
4360 | /* Work out how much this section will move the dot point. */ |
4361 | ||
252b5132 | 4362 | static bfd_vma |
6feb9908 AM |
4363 | size_input_section |
4364 | (lang_statement_union_type **this_ptr, | |
4365 | lang_output_section_statement_type *output_section_statement, | |
4366 | fill_type *fill, | |
4367 | bfd_vma dot) | |
252b5132 RH |
4368 | { |
4369 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
4370 | asection *i = is->section; | |
4371 | ||
7b986e99 AM |
4372 | if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag |
4373 | && (i->flags & SEC_EXCLUDE) == 0) | |
252b5132 | 4374 | { |
b3327aad AM |
4375 | unsigned int alignment_needed; |
4376 | asection *o; | |
4377 | ||
4378 | /* Align this section first to the input sections requirement, | |
4379 | then to the output section's requirement. If this alignment | |
4380 | is greater than any seen before, then record it too. Perform | |
4381 | the alignment by inserting a magic 'padding' statement. */ | |
4382 | ||
252b5132 | 4383 | if (output_section_statement->subsection_alignment != -1) |
b3327aad AM |
4384 | i->alignment_power = output_section_statement->subsection_alignment; |
4385 | ||
4386 | o = output_section_statement->bfd_section; | |
4387 | if (o->alignment_power < i->alignment_power) | |
4388 | o->alignment_power = i->alignment_power; | |
252b5132 | 4389 | |
b3327aad AM |
4390 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
4391 | ||
4392 | if (alignment_needed != 0) | |
4393 | { | |
e5caa5e0 | 4394 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
4395 | dot += alignment_needed; |
4396 | } | |
252b5132 | 4397 | |
08da4cac | 4398 | /* Remember where in the output section this input section goes. */ |
252b5132 | 4399 | |
b3327aad | 4400 | i->output_offset = dot - o->vma; |
252b5132 | 4401 | |
08da4cac | 4402 | /* Mark how big the output section must be to contain this now. */ |
eea6121a AM |
4403 | dot += TO_ADDR (i->size); |
4404 | o->size = TO_SIZE (dot - o->vma); | |
252b5132 RH |
4405 | } |
4406 | else | |
4407 | { | |
4408 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
4409 | } | |
4410 | ||
4411 | return dot; | |
4412 | } | |
4413 | ||
5daa8fe7 L |
4414 | static int |
4415 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
4416 | { | |
4417 | const asection *sec1 = *(const asection **) arg1; | |
4418 | const asection *sec2 = *(const asection **) arg2; | |
4419 | ||
4420 | if (bfd_section_lma (sec1->owner, sec1) | |
4421 | < bfd_section_lma (sec2->owner, sec2)) | |
4422 | return -1; | |
4423 | else if (bfd_section_lma (sec1->owner, sec1) | |
4424 | > bfd_section_lma (sec2->owner, sec2)) | |
4425 | return 1; | |
7f6a71ff JM |
4426 | else if (sec1->id < sec2->id) |
4427 | return -1; | |
4428 | else if (sec1->id > sec2->id) | |
4429 | return 1; | |
5daa8fe7 L |
4430 | |
4431 | return 0; | |
4432 | } | |
4433 | ||
eea6121a | 4434 | #define IGNORE_SECTION(s) \ |
afd7a018 AM |
4435 | ((s->flags & SEC_NEVER_LOAD) != 0 \ |
4436 | || (s->flags & SEC_ALLOC) == 0 \ | |
4437 | || ((s->flags & SEC_THREAD_LOCAL) != 0 \ | |
de7f8cc8 | 4438 | && (s->flags & SEC_LOAD) == 0)) |
d1778b88 | 4439 | |
252b5132 | 4440 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 4441 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
4442 | section addresses of the two sections. Also check whether any memory |
4443 | region has overflowed. */ | |
08da4cac | 4444 | |
252b5132 | 4445 | static void |
1579bae1 | 4446 | lang_check_section_addresses (void) |
252b5132 | 4447 | { |
5daa8fe7 L |
4448 | asection *s, *os; |
4449 | asection **sections, **spp; | |
4450 | unsigned int count; | |
4451 | bfd_vma s_start; | |
4452 | bfd_vma s_end; | |
4453 | bfd_vma os_start; | |
4454 | bfd_vma os_end; | |
4455 | bfd_size_type amt; | |
20e56351 | 4456 | lang_memory_region_type *m; |
5daa8fe7 | 4457 | |
f13a99db | 4458 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
4459 | return; |
4460 | ||
f13a99db | 4461 | amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *); |
5daa8fe7 | 4462 | sections = xmalloc (amt); |
252b5132 RH |
4463 | |
4464 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 4465 | count = 0; |
f13a99db | 4466 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 4467 | { |
5daa8fe7 | 4468 | /* Only consider loadable sections with real contents. */ |
de7f8cc8 | 4469 | if (IGNORE_SECTION (s) || s->size == 0) |
33275c22 | 4470 | continue; |
5f992e62 | 4471 | |
5daa8fe7 L |
4472 | sections[count] = s; |
4473 | count++; | |
4474 | } | |
329c1c86 | 4475 | |
5daa8fe7 L |
4476 | if (count <= 1) |
4477 | return; | |
5f992e62 | 4478 | |
5daa8fe7 L |
4479 | qsort (sections, (size_t) count, sizeof (asection *), |
4480 | sort_sections_by_lma); | |
5f992e62 | 4481 | |
5daa8fe7 L |
4482 | spp = sections; |
4483 | s = *spp++; | |
f13a99db | 4484 | s_start = bfd_section_lma (link_info.output_bfd, s); |
5daa8fe7 L |
4485 | s_end = s_start + TO_ADDR (s->size) - 1; |
4486 | for (count--; count; count--) | |
4487 | { | |
4488 | /* We must check the sections' LMA addresses not their VMA | |
4489 | addresses because overlay sections can have overlapping VMAs | |
4490 | but they must have distinct LMAs. */ | |
4491 | os = s; | |
329c1c86 | 4492 | os_start = s_start; |
5daa8fe7 L |
4493 | os_end = s_end; |
4494 | s = *spp++; | |
f13a99db | 4495 | s_start = bfd_section_lma (link_info.output_bfd, s); |
5daa8fe7 | 4496 | s_end = s_start + TO_ADDR (s->size) - 1; |
5f992e62 | 4497 | |
5daa8fe7 L |
4498 | /* Look for an overlap. */ |
4499 | if (s_end >= os_start && s_start <= os_end) | |
4500 | einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"), | |
4501 | s->name, s_start, s_end, os->name, os_start, os_end); | |
33275c22 | 4502 | } |
5daa8fe7 L |
4503 | |
4504 | free (sections); | |
20e56351 DJ |
4505 | |
4506 | /* If any memory region has overflowed, report by how much. | |
4507 | We do not issue this diagnostic for regions that had sections | |
4508 | explicitly placed outside their bounds; os_region_check's | |
4509 | diagnostics are adequate for that case. | |
4510 | ||
4511 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
4512 | might overflow a 32-bit integer. There is, alas, no way to print | |
4513 | a bfd_vma quantity in decimal. */ | |
4514 | for (m = lang_memory_region_list; m; m = m->next) | |
4515 | if (m->had_full_message) | |
4a93e180 NC |
4516 | einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"), |
4517 | m->name_list.name, (long)(m->current - (m->origin + m->length))); | |
20e56351 | 4518 | |
252b5132 RH |
4519 | } |
4520 | ||
562d3460 TW |
4521 | /* Make sure the new address is within the region. We explicitly permit the |
4522 | current address to be at the exact end of the region when the address is | |
4523 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 4524 | calculation wraps around. */ |
562d3460 TW |
4525 | |
4526 | static void | |
1579bae1 | 4527 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 4528 | lang_memory_region_type *region, |
1579bae1 AM |
4529 | etree_type *tree, |
4530 | bfd_vma base) | |
562d3460 TW |
4531 | { |
4532 | if ((region->current < region->origin | |
4533 | || (region->current - region->origin > region->length)) | |
4534 | && ((region->current != region->origin + region->length) | |
b7a26f91 | 4535 | || base == 0)) |
562d3460 | 4536 | { |
1579bae1 | 4537 | if (tree != NULL) |
b7a26f91 | 4538 | { |
4a93e180 NC |
4539 | einfo (_("%X%P: address 0x%v of %B section `%s'" |
4540 | " is not within region `%s'\n"), | |
b7a26f91 KH |
4541 | region->current, |
4542 | os->bfd_section->owner, | |
4543 | os->bfd_section->name, | |
4a93e180 | 4544 | region->name_list.name); |
b7a26f91 | 4545 | } |
20e56351 | 4546 | else if (!region->had_full_message) |
b7a26f91 | 4547 | { |
20e56351 DJ |
4548 | region->had_full_message = TRUE; |
4549 | ||
4a93e180 | 4550 | einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 4551 | os->bfd_section->owner, |
20e56351 | 4552 | os->bfd_section->name, |
4a93e180 | 4553 | region->name_list.name); |
b7a26f91 | 4554 | } |
562d3460 TW |
4555 | } |
4556 | } | |
4557 | ||
252b5132 RH |
4558 | /* Set the sizes for all the output sections. */ |
4559 | ||
2d20f7bf | 4560 | static bfd_vma |
1579bae1 AM |
4561 | lang_size_sections_1 |
4562 | (lang_statement_union_type *s, | |
4563 | lang_output_section_statement_type *output_section_statement, | |
4564 | lang_statement_union_type **prev, | |
4565 | fill_type *fill, | |
4566 | bfd_vma dot, | |
4567 | bfd_boolean *relax, | |
4568 | bfd_boolean check_regions) | |
252b5132 RH |
4569 | { |
4570 | /* Size up the sections from their constituent parts. */ | |
1579bae1 | 4571 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
4572 | { |
4573 | switch (s->header.type) | |
4574 | { | |
4575 | case lang_output_section_statement_enum: | |
4576 | { | |
e9ee469a | 4577 | bfd_vma newdot, after; |
d1778b88 | 4578 | lang_output_section_statement_type *os; |
cde9e0be | 4579 | lang_memory_region_type *r; |
252b5132 | 4580 | |
d1778b88 | 4581 | os = &s->output_section_statement; |
a5df8c84 AM |
4582 | if (os->addr_tree != NULL) |
4583 | { | |
cde9e0be | 4584 | os->processed_vma = FALSE; |
a5df8c84 | 4585 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 4586 | |
3bf9618b AM |
4587 | if (expld.result.valid_p) |
4588 | dot = expld.result.value + expld.result.section->vma; | |
4589 | else if (expld.phase != lang_mark_phase_enum) | |
a5df8c84 AM |
4590 | einfo (_("%F%S: non constant or forward reference" |
4591 | " address expression for section %s\n"), | |
4592 | os->name); | |
a5df8c84 AM |
4593 | } |
4594 | ||
e9ee469a | 4595 | if (os->bfd_section == NULL) |
75ff4589 | 4596 | /* This section was removed or never actually created. */ |
252b5132 RH |
4597 | break; |
4598 | ||
4599 | /* If this is a COFF shared library section, use the size and | |
4600 | address from the input section. FIXME: This is COFF | |
4601 | specific; it would be cleaner if there were some other way | |
4602 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
4603 | if (((bfd_get_flavour (link_info.output_bfd) |
4604 | == bfd_target_ecoff_flavour) | |
4605 | || (bfd_get_flavour (link_info.output_bfd) | |
4606 | == bfd_target_coff_flavour)) | |
ebe372c1 | 4607 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 4608 | { |
08da4cac | 4609 | asection *input; |
252b5132 RH |
4610 | |
4611 | if (os->children.head == NULL | |
bba1a0c0 | 4612 | || os->children.head->header.next != NULL |
6feb9908 AM |
4613 | || (os->children.head->header.type |
4614 | != lang_input_section_enum)) | |
4615 | einfo (_("%P%X: Internal error on COFF shared library" | |
4616 | " section %s\n"), os->name); | |
252b5132 RH |
4617 | |
4618 | input = os->children.head->input_section.section; | |
4619 | bfd_set_section_vma (os->bfd_section->owner, | |
4620 | os->bfd_section, | |
4621 | bfd_section_vma (input->owner, input)); | |
eea6121a | 4622 | os->bfd_section->size = input->size; |
252b5132 RH |
4623 | break; |
4624 | } | |
4625 | ||
a5df8c84 | 4626 | newdot = dot; |
252b5132 RH |
4627 | if (bfd_is_abs_section (os->bfd_section)) |
4628 | { | |
4629 | /* No matter what happens, an abs section starts at zero. */ | |
4630 | ASSERT (os->bfd_section->vma == 0); | |
4631 | } | |
4632 | else | |
4633 | { | |
94b50910 | 4634 | int align; |
7270c5ed | 4635 | |
1579bae1 | 4636 | if (os->addr_tree == NULL) |
252b5132 RH |
4637 | { |
4638 | /* No address specified for this section, get one | |
4639 | from the region specification. */ | |
1579bae1 | 4640 | if (os->region == NULL |
6feb9908 | 4641 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
4642 | && os->region->name_list.name[0] == '*' |
4643 | && strcmp (os->region->name_list.name, | |
6feb9908 | 4644 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
4645 | { |
4646 | os->region = lang_memory_default (os->bfd_section); | |
4647 | } | |
4648 | ||
4649 | /* If a loadable section is using the default memory | |
4650 | region, and some non default memory regions were | |
66184979 | 4651 | defined, issue an error message. */ |
f0636a44 AM |
4652 | if (!os->ignored |
4653 | && !IGNORE_SECTION (os->bfd_section) | |
1049f94e | 4654 | && ! link_info.relocatable |
cf888e70 | 4655 | && check_regions |
4a93e180 | 4656 | && strcmp (os->region->name_list.name, |
6feb9908 | 4657 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 4658 | && lang_memory_region_list != NULL |
4a93e180 | 4659 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 4660 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a AM |
4661 | || lang_memory_region_list->next != NULL) |
4662 | && expld.phase != lang_mark_phase_enum) | |
66184979 NC |
4663 | { |
4664 | /* By default this is an error rather than just a | |
4665 | warning because if we allocate the section to the | |
4666 | default memory region we can end up creating an | |
07f3b6ad KH |
4667 | excessively large binary, or even seg faulting when |
4668 | attempting to perform a negative seek. See | |
6feb9908 | 4669 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
4670 | for an example of this. This behaviour can be |
4671 | overridden by the using the --no-check-sections | |
4672 | switch. */ | |
4673 | if (command_line.check_section_addresses) | |
6feb9908 AM |
4674 | einfo (_("%P%F: error: no memory region specified" |
4675 | " for loadable section `%s'\n"), | |
f13a99db | 4676 | bfd_get_section_name (link_info.output_bfd, |
66184979 NC |
4677 | os->bfd_section)); |
4678 | else | |
6feb9908 AM |
4679 | einfo (_("%P: warning: no memory region specified" |
4680 | " for loadable section `%s'\n"), | |
f13a99db | 4681 | bfd_get_section_name (link_info.output_bfd, |
66184979 NC |
4682 | os->bfd_section)); |
4683 | } | |
252b5132 | 4684 | |
e9ee469a | 4685 | newdot = os->region->current; |
94b50910 | 4686 | align = os->bfd_section->alignment_power; |
252b5132 | 4687 | } |
94b50910 AM |
4688 | else |
4689 | align = os->section_alignment; | |
5f992e62 | 4690 | |
7270c5ed | 4691 | /* Align to what the section needs. */ |
94b50910 AM |
4692 | if (align > 0) |
4693 | { | |
4694 | bfd_vma savedot = newdot; | |
4695 | newdot = align_power (newdot, align); | |
252b5132 | 4696 | |
94b50910 AM |
4697 | if (newdot != savedot |
4698 | && (config.warn_section_align | |
4699 | || os->addr_tree != NULL) | |
4700 | && expld.phase != lang_mark_phase_enum) | |
4701 | einfo (_("%P: warning: changing start of section" | |
4702 | " %s by %lu bytes\n"), | |
4703 | os->name, (unsigned long) (newdot - savedot)); | |
4704 | } | |
252b5132 | 4705 | |
fb6cee96 NC |
4706 | /* PR 6945: Do not update the vma's of output sections |
4707 | when performing a relocatable link on COFF objects. */ | |
4708 | if (! link_info.relocatable | |
4709 | || (bfd_get_flavour (link_info.output_bfd) | |
4710 | != bfd_target_coff_flavour)) | |
4711 | bfd_set_section_vma (0, os->bfd_section, newdot); | |
5f992e62 | 4712 | |
252b5132 RH |
4713 | os->bfd_section->output_offset = 0; |
4714 | } | |
4715 | ||
2d20f7bf | 4716 | lang_size_sections_1 (os->children.head, os, &os->children.head, |
e9ee469a AM |
4717 | os->fill, newdot, relax, check_regions); |
4718 | ||
cde9e0be | 4719 | os->processed_vma = TRUE; |
e9ee469a AM |
4720 | |
4721 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
4722 | /* Except for some special linker created sections, |
4723 | no output section should change from zero size | |
4724 | after strip_excluded_output_sections. A non-zero | |
4725 | size on an ignored section indicates that some | |
4726 | input section was not sized early enough. */ | |
4727 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 4728 | else |
e9ee469a | 4729 | { |
cde9e0be AM |
4730 | dot = os->bfd_section->vma; |
4731 | ||
4732 | /* Put the section within the requested block size, or | |
4733 | align at the block boundary. */ | |
4734 | after = ((dot | |
4735 | + TO_ADDR (os->bfd_section->size) | |
4736 | + os->block_value - 1) | |
4737 | & - (bfd_vma) os->block_value); | |
4738 | ||
4739 | os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma); | |
4740 | } | |
4741 | ||
4742 | /* Set section lma. */ | |
4743 | r = os->region; | |
4744 | if (r == NULL) | |
4745 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); | |
4746 | ||
4747 | if (os->load_base) | |
4748 | { | |
4749 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
4750 | os->bfd_section->lma = lma; | |
e9ee469a | 4751 | } |
07dfcf38 | 4752 | else if (os->lma_region != NULL) |
cde9e0be AM |
4753 | { |
4754 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 4755 | |
cde9e0be AM |
4756 | if (os->section_alignment != -1) |
4757 | lma = align_power (lma, os->section_alignment); | |
4758 | os->bfd_section->lma = lma; | |
4759 | } | |
dc0b6aa0 AM |
4760 | else if (r->last_os != NULL |
4761 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
4762 | { |
4763 | bfd_vma lma; | |
4764 | asection *last; | |
4765 | ||
4766 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
4767 | |
4768 | /* A backwards move of dot should be accompanied by | |
4769 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 4770 | os->load_base set) because backwards moves can |
dc0b6aa0 | 4771 | create overlapping LMAs. */ |
264b6205 | 4772 | if (dot < last->vma |
91eb6c46 AM |
4773 | && os->bfd_section->size != 0 |
4774 | && dot + os->bfd_section->size <= last->vma) | |
dc0b6aa0 | 4775 | { |
dc0b6aa0 AM |
4776 | /* If dot moved backwards then leave lma equal to |
4777 | vma. This is the old default lma, which might | |
4778 | just happen to work when the backwards move is | |
91eb6c46 AM |
4779 | sufficiently large. Nag if this changes anything, |
4780 | so people can fix their linker scripts. */ | |
4781 | ||
4782 | if (last->vma != last->lma) | |
4783 | einfo (_("%P: warning: dot moved backwards before `%s'\n"), | |
4784 | os->name); | |
dc0b6aa0 AM |
4785 | } |
4786 | else | |
cde9e0be | 4787 | { |
152d792f AM |
4788 | /* If this is an overlay, set the current lma to that |
4789 | at the end of the previous section. */ | |
4790 | if (os->sectype == overlay_section) | |
cde9e0be AM |
4791 | lma = last->lma + last->size; |
4792 | ||
4793 | /* Otherwise, keep the same lma to vma relationship | |
4794 | as the previous section. */ | |
4795 | else | |
4796 | lma = dot + last->lma - last->vma; | |
4797 | ||
4798 | if (os->section_alignment != -1) | |
4799 | lma = align_power (lma, os->section_alignment); | |
4800 | os->bfd_section->lma = lma; | |
4801 | } | |
4802 | } | |
4803 | os->processed_lma = TRUE; | |
5f992e62 | 4804 | |
cde9e0be AM |
4805 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
4806 | break; | |
252b5132 | 4807 | |
cde9e0be AM |
4808 | /* Keep track of normal sections using the default |
4809 | lma region. We use this to set the lma for | |
4810 | following sections. Overlays or other linker | |
4811 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
4812 | default lma == vma is incorrect. |
4813 | To avoid warnings about dot moving backwards when using | |
4814 | -Ttext, don't start tracking sections until we find one | |
4815 | of non-zero size or with lma set differently to vma. */ | |
cde9e0be AM |
4816 | if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 |
4817 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0) | |
dc0b6aa0 AM |
4818 | && (os->bfd_section->flags & SEC_ALLOC) != 0 |
4819 | && (os->bfd_section->size != 0 | |
264b6205 AM |
4820 | || (r->last_os == NULL |
4821 | && os->bfd_section->vma != os->bfd_section->lma) | |
4822 | || (r->last_os != NULL | |
4823 | && dot >= (r->last_os->output_section_statement | |
ea0c3cd5 | 4824 | .bfd_section->vma))) |
cde9e0be AM |
4825 | && os->lma_region == NULL |
4826 | && !link_info.relocatable) | |
4827 | r->last_os = s; | |
9f88b410 | 4828 | |
e5caec89 NS |
4829 | /* .tbss sections effectively have zero size. */ |
4830 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
4831 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
4832 | || link_info.relocatable) | |
eea6121a | 4833 | dot += TO_ADDR (os->bfd_section->size); |
e5caec89 | 4834 | |
9f88b410 | 4835 | if (os->update_dot_tree != 0) |
e9ee469a | 4836 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 4837 | |
252b5132 RH |
4838 | /* Update dot in the region ? |
4839 | We only do this if the section is going to be allocated, | |
4840 | since unallocated sections do not contribute to the region's | |
13392b77 | 4841 | overall size in memory. |
5f992e62 | 4842 | |
cce4c4c5 NC |
4843 | If the SEC_NEVER_LOAD bit is not set, it will affect the |
4844 | addresses of sections after it. We have to update | |
4845 | dot. */ | |
1579bae1 | 4846 | if (os->region != NULL |
6feb9908 AM |
4847 | && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0 |
4848 | || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))) | |
252b5132 RH |
4849 | { |
4850 | os->region->current = dot; | |
5f992e62 | 4851 | |
cf888e70 NC |
4852 | if (check_regions) |
4853 | /* Make sure the new address is within the region. */ | |
4854 | os_region_check (os, os->region, os->addr_tree, | |
4855 | os->bfd_section->vma); | |
08da4cac | 4856 | |
a2cab753 NS |
4857 | if (os->lma_region != NULL && os->lma_region != os->region |
4858 | && (os->bfd_section->flags & SEC_LOAD)) | |
08da4cac | 4859 | { |
cde9e0be AM |
4860 | os->lma_region->current |
4861 | = os->bfd_section->lma + TO_ADDR (os->bfd_section->size); | |
66e28d60 | 4862 | |
cf888e70 NC |
4863 | if (check_regions) |
4864 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 4865 | os->bfd_section->lma); |
08da4cac | 4866 | } |
252b5132 RH |
4867 | } |
4868 | } | |
4869 | break; | |
4870 | ||
4871 | case lang_constructors_statement_enum: | |
2d20f7bf JJ |
4872 | dot = lang_size_sections_1 (constructor_list.head, |
4873 | output_section_statement, | |
4874 | &s->wild_statement.children.head, | |
cf888e70 | 4875 | fill, dot, relax, check_regions); |
252b5132 RH |
4876 | break; |
4877 | ||
4878 | case lang_data_statement_enum: | |
4879 | { | |
4880 | unsigned int size = 0; | |
4881 | ||
7fabd029 | 4882 | s->data_statement.output_offset = |
08da4cac | 4883 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
4884 | s->data_statement.output_section = |
4885 | output_section_statement->bfd_section; | |
4886 | ||
1b493742 NS |
4887 | /* We might refer to provided symbols in the expression, and |
4888 | need to mark them as needed. */ | |
e9ee469a | 4889 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 4890 | |
252b5132 RH |
4891 | switch (s->data_statement.type) |
4892 | { | |
08da4cac KH |
4893 | default: |
4894 | abort (); | |
252b5132 RH |
4895 | case QUAD: |
4896 | case SQUAD: | |
4897 | size = QUAD_SIZE; | |
4898 | break; | |
4899 | case LONG: | |
4900 | size = LONG_SIZE; | |
4901 | break; | |
4902 | case SHORT: | |
4903 | size = SHORT_SIZE; | |
4904 | break; | |
4905 | case BYTE: | |
4906 | size = BYTE_SIZE; | |
4907 | break; | |
4908 | } | |
e5caa5e0 AM |
4909 | if (size < TO_SIZE ((unsigned) 1)) |
4910 | size = TO_SIZE ((unsigned) 1); | |
4911 | dot += TO_ADDR (size); | |
eea6121a | 4912 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
4913 | } |
4914 | break; | |
4915 | ||
4916 | case lang_reloc_statement_enum: | |
4917 | { | |
4918 | int size; | |
4919 | ||
7fabd029 | 4920 | s->reloc_statement.output_offset = |
252b5132 RH |
4921 | dot - output_section_statement->bfd_section->vma; |
4922 | s->reloc_statement.output_section = | |
4923 | output_section_statement->bfd_section; | |
4924 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 4925 | dot += TO_ADDR (size); |
eea6121a | 4926 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
4927 | } |
4928 | break; | |
5f992e62 | 4929 | |
252b5132 | 4930 | case lang_wild_statement_enum: |
2d20f7bf JJ |
4931 | dot = lang_size_sections_1 (s->wild_statement.children.head, |
4932 | output_section_statement, | |
4933 | &s->wild_statement.children.head, | |
cf888e70 | 4934 | fill, dot, relax, check_regions); |
252b5132 RH |
4935 | break; |
4936 | ||
4937 | case lang_object_symbols_statement_enum: | |
4938 | link_info.create_object_symbols_section = | |
4939 | output_section_statement->bfd_section; | |
4940 | break; | |
e9ee469a | 4941 | |
252b5132 RH |
4942 | case lang_output_statement_enum: |
4943 | case lang_target_statement_enum: | |
4944 | break; | |
e9ee469a | 4945 | |
252b5132 RH |
4946 | case lang_input_section_enum: |
4947 | { | |
4948 | asection *i; | |
4949 | ||
4950 | i = (*prev)->input_section.section; | |
eea6121a | 4951 | if (relax) |
252b5132 | 4952 | { |
b34976b6 | 4953 | bfd_boolean again; |
252b5132 RH |
4954 | |
4955 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) | |
4956 | einfo (_("%P%F: can't relax section: %E\n")); | |
4957 | if (again) | |
b34976b6 | 4958 | *relax = TRUE; |
252b5132 | 4959 | } |
b3327aad AM |
4960 | dot = size_input_section (prev, output_section_statement, |
4961 | output_section_statement->fill, dot); | |
252b5132 RH |
4962 | } |
4963 | break; | |
e9ee469a | 4964 | |
252b5132 RH |
4965 | case lang_input_statement_enum: |
4966 | break; | |
e9ee469a | 4967 | |
252b5132 | 4968 | case lang_fill_statement_enum: |
08da4cac KH |
4969 | s->fill_statement.output_section = |
4970 | output_section_statement->bfd_section; | |
252b5132 RH |
4971 | |
4972 | fill = s->fill_statement.fill; | |
4973 | break; | |
e9ee469a | 4974 | |
252b5132 RH |
4975 | case lang_assignment_statement_enum: |
4976 | { | |
4977 | bfd_vma newdot = dot; | |
49c13adb | 4978 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 4979 | |
b10a8ae0 L |
4980 | expld.dataseg.relro = exp_dataseg_relro_none; |
4981 | ||
49c13adb | 4982 | exp_fold_tree (tree, |
408082ec | 4983 | output_section_statement->bfd_section, |
252b5132 RH |
4984 | &newdot); |
4985 | ||
b10a8ae0 L |
4986 | if (expld.dataseg.relro == exp_dataseg_relro_start) |
4987 | { | |
4988 | if (!expld.dataseg.relro_start_stat) | |
4989 | expld.dataseg.relro_start_stat = s; | |
4990 | else | |
4991 | { | |
4992 | ASSERT (expld.dataseg.relro_start_stat == s); | |
4993 | } | |
4994 | } | |
4995 | else if (expld.dataseg.relro == exp_dataseg_relro_end) | |
4996 | { | |
4997 | if (!expld.dataseg.relro_end_stat) | |
4998 | expld.dataseg.relro_end_stat = s; | |
4999 | else | |
5000 | { | |
5001 | ASSERT (expld.dataseg.relro_end_stat == s); | |
5002 | } | |
5003 | } | |
5004 | expld.dataseg.relro = exp_dataseg_relro_none; | |
5005 | ||
49c13adb | 5006 | /* This symbol is relative to this section. */ |
a14a5de3 | 5007 | if ((tree->type.node_class == etree_provided |
49c13adb L |
5008 | || tree->type.node_class == etree_assign) |
5009 | && (tree->assign.dst [0] != '.' | |
5010 | || tree->assign.dst [1] != '\0')) | |
5011 | output_section_statement->section_relative_symbol = 1; | |
5012 | ||
85852e36 | 5013 | if (!output_section_statement->ignored) |
252b5132 | 5014 | { |
252b5132 RH |
5015 | if (output_section_statement == abs_output_section) |
5016 | { | |
5017 | /* If we don't have an output section, then just adjust | |
5018 | the default memory address. */ | |
6feb9908 AM |
5019 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
5020 | FALSE)->current = newdot; | |
252b5132 | 5021 | } |
85852e36 | 5022 | else if (newdot != dot) |
252b5132 | 5023 | { |
b3327aad AM |
5024 | /* Insert a pad after this statement. We can't |
5025 | put the pad before when relaxing, in case the | |
5026 | assignment references dot. */ | |
e5caa5e0 | 5027 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
5028 | output_section_statement->bfd_section, dot); |
5029 | ||
5030 | /* Don't neuter the pad below when relaxing. */ | |
5031 | s = s->header.next; | |
9dfc8ab2 | 5032 | |
e9ee469a AM |
5033 | /* If dot is advanced, this implies that the section |
5034 | should have space allocated to it, unless the | |
5035 | user has explicitly stated that the section | |
5036 | should never be loaded. */ | |
14ac84b4 | 5037 | if (!(output_section_statement->flags & SEC_NEVER_LOAD)) |
e9ee469a AM |
5038 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
5039 | } | |
252b5132 RH |
5040 | dot = newdot; |
5041 | } | |
5042 | } | |
5043 | break; | |
5044 | ||
5045 | case lang_padding_statement_enum: | |
c0c330a7 AM |
5046 | /* If this is the first time lang_size_sections is called, |
5047 | we won't have any padding statements. If this is the | |
5048 | second or later passes when relaxing, we should allow | |
5049 | padding to shrink. If padding is needed on this pass, it | |
5050 | will be added back in. */ | |
5051 | s->padding_statement.size = 0; | |
6e814ff8 AM |
5052 | |
5053 | /* Make sure output_offset is valid. If relaxation shrinks | |
5054 | the section and this pad isn't needed, it's possible to | |
5055 | have output_offset larger than the final size of the | |
5056 | section. bfd_set_section_contents will complain even for | |
5057 | a pad size of zero. */ | |
5058 | s->padding_statement.output_offset | |
5059 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
5060 | break; |
5061 | ||
5062 | case lang_group_statement_enum: | |
2d20f7bf JJ |
5063 | dot = lang_size_sections_1 (s->group_statement.children.head, |
5064 | output_section_statement, | |
5065 | &s->group_statement.children.head, | |
cf888e70 | 5066 | fill, dot, relax, check_regions); |
252b5132 RH |
5067 | break; |
5068 | ||
53d25da6 | 5069 | case lang_insert_statement_enum: |
252b5132 RH |
5070 | break; |
5071 | ||
c0c330a7 | 5072 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
5073 | case lang_address_statement_enum: |
5074 | break; | |
53d25da6 AM |
5075 | |
5076 | default: | |
5077 | FAIL (); | |
5078 | break; | |
252b5132 RH |
5079 | } |
5080 | prev = &s->header.next; | |
5081 | } | |
5082 | return dot; | |
5083 | } | |
5084 | ||
591a748a NC |
5085 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
5086 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
5087 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
5088 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b NC |
5089 | |
5090 | bfd_boolean | |
5091 | ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED, | |
5092 | bfd * abfd ATTRIBUTE_UNUSED, | |
5093 | asection * current_section, | |
5094 | asection * previous_section, | |
5095 | bfd_boolean new_segment) | |
5096 | { | |
5097 | lang_output_section_statement_type * cur; | |
5098 | lang_output_section_statement_type * prev; | |
591a748a NC |
5099 | |
5100 | /* The checks below are only necessary when the BFD library has decided | |
5101 | that the two sections ought to be placed into the same segment. */ | |
2889e75b NC |
5102 | if (new_segment) |
5103 | return TRUE; | |
5104 | ||
5105 | /* Paranoia checks. */ | |
5106 | if (current_section == NULL || previous_section == NULL) | |
5107 | return new_segment; | |
5108 | ||
5109 | /* Find the memory regions associated with the two sections. | |
5110 | We call lang_output_section_find() here rather than scanning the list | |
5111 | of output sections looking for a matching section pointer because if | |
591a748a | 5112 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
5113 | cur = lang_output_section_find (current_section->name); |
5114 | prev = lang_output_section_find (previous_section->name); | |
5115 | ||
591a748a | 5116 | /* More paranoia. */ |
2889e75b NC |
5117 | if (cur == NULL || prev == NULL) |
5118 | return new_segment; | |
5119 | ||
5120 | /* If the regions are different then force the sections to live in | |
591a748a NC |
5121 | different segments. See the email thread starting at the following |
5122 | URL for the reasons why this is necessary: | |
2889e75b NC |
5123 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
5124 | return cur->region != prev->region; | |
5125 | } | |
5126 | ||
e9ee469a AM |
5127 | void |
5128 | one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions) | |
2d20f7bf | 5129 | { |
420e579c | 5130 | lang_statement_iteration++; |
e9ee469a AM |
5131 | lang_size_sections_1 (statement_list.head, abs_output_section, |
5132 | &statement_list.head, 0, 0, relax, check_regions); | |
5133 | } | |
420e579c | 5134 | |
e9ee469a AM |
5135 | void |
5136 | lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions) | |
5137 | { | |
5138 | expld.phase = lang_allocating_phase_enum; | |
5139 | expld.dataseg.phase = exp_dataseg_none; | |
5140 | ||
5141 | one_lang_size_sections_pass (relax, check_regions); | |
5142 | if (expld.dataseg.phase == exp_dataseg_end_seen | |
5143 | && link_info.relro && expld.dataseg.relro_end) | |
8c37241b JJ |
5144 | { |
5145 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try | |
e9ee469a | 5146 | to put expld.dataseg.relro on a (common) page boundary. */ |
2b60860a | 5147 | bfd_vma min_base, old_base, relro_end, maxpage; |
8c37241b | 5148 | |
e9ee469a | 5149 | expld.dataseg.phase = exp_dataseg_relro_adjust; |
e9ee469a | 5150 | maxpage = expld.dataseg.maxpagesize; |
2b60860a JK |
5151 | /* MIN_BASE is the absolute minimum address we are allowed to start the |
5152 | read-write segment (byte before will be mapped read-only). */ | |
5153 | min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1); | |
5154 | /* OLD_BASE is the address for a feasible minimum address which will | |
5155 | still not cause a data overlap inside MAXPAGE causing file offset skip | |
5156 | by MAXPAGE. */ | |
5157 | old_base = expld.dataseg.base; | |
e9ee469a AM |
5158 | expld.dataseg.base += (-expld.dataseg.relro_end |
5159 | & (expld.dataseg.pagesize - 1)); | |
a4f5ad88 | 5160 | /* Compute the expected PT_GNU_RELRO segment end. */ |
967928e9 AM |
5161 | relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1) |
5162 | & ~(expld.dataseg.pagesize - 1)); | |
2b60860a | 5163 | if (min_base + maxpage < expld.dataseg.base) |
e3070fef | 5164 | { |
e9ee469a | 5165 | expld.dataseg.base -= maxpage; |
e3070fef JJ |
5166 | relro_end -= maxpage; |
5167 | } | |
f86a8756 | 5168 | lang_reset_memory_regions (); |
e9ee469a AM |
5169 | one_lang_size_sections_pass (relax, check_regions); |
5170 | if (expld.dataseg.relro_end > relro_end) | |
a4f5ad88 JJ |
5171 | { |
5172 | /* The alignment of sections between DATA_SEGMENT_ALIGN | |
5173 | and DATA_SEGMENT_RELRO_END caused huge padding to be | |
2b60860a JK |
5174 | inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so |
5175 | that the section alignments will fit in. */ | |
a4f5ad88 JJ |
5176 | asection *sec; |
5177 | unsigned int max_alignment_power = 0; | |
5178 | ||
5179 | /* Find maximum alignment power of sections between | |
5180 | DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */ | |
f13a99db | 5181 | for (sec = link_info.output_bfd->sections; sec; sec = sec->next) |
e9ee469a AM |
5182 | if (sec->vma >= expld.dataseg.base |
5183 | && sec->vma < expld.dataseg.relro_end | |
a4f5ad88 JJ |
5184 | && sec->alignment_power > max_alignment_power) |
5185 | max_alignment_power = sec->alignment_power; | |
5186 | ||
e9ee469a | 5187 | if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize) |
a4f5ad88 | 5188 | { |
2b60860a | 5189 | if (expld.dataseg.base - (1 << max_alignment_power) < old_base) |
e9ee469a AM |
5190 | expld.dataseg.base += expld.dataseg.pagesize; |
5191 | expld.dataseg.base -= (1 << max_alignment_power); | |
f86a8756 | 5192 | lang_reset_memory_regions (); |
e9ee469a | 5193 | one_lang_size_sections_pass (relax, check_regions); |
a4f5ad88 JJ |
5194 | } |
5195 | } | |
e9ee469a AM |
5196 | link_info.relro_start = expld.dataseg.base; |
5197 | link_info.relro_end = expld.dataseg.relro_end; | |
8c37241b | 5198 | } |
e9ee469a | 5199 | else if (expld.dataseg.phase == exp_dataseg_end_seen) |
2d20f7bf JJ |
5200 | { |
5201 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether | |
5202 | a page could be saved in the data segment. */ | |
5203 | bfd_vma first, last; | |
5204 | ||
e9ee469a AM |
5205 | first = -expld.dataseg.base & (expld.dataseg.pagesize - 1); |
5206 | last = expld.dataseg.end & (expld.dataseg.pagesize - 1); | |
2d20f7bf | 5207 | if (first && last |
e9ee469a AM |
5208 | && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1)) |
5209 | != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1))) | |
5210 | && first + last <= expld.dataseg.pagesize) | |
2d20f7bf | 5211 | { |
e9ee469a | 5212 | expld.dataseg.phase = exp_dataseg_adjust; |
f86a8756 | 5213 | lang_reset_memory_regions (); |
e9ee469a | 5214 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf JJ |
5215 | } |
5216 | } | |
5217 | ||
e9ee469a | 5218 | expld.phase = lang_final_phase_enum; |
2d20f7bf JJ |
5219 | } |
5220 | ||
420e579c HPN |
5221 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
5222 | ||
5223 | static bfd_vma | |
66e28d60 AM |
5224 | lang_do_assignments_1 (lang_statement_union_type *s, |
5225 | lang_output_section_statement_type *current_os, | |
5226 | fill_type *fill, | |
5227 | bfd_vma dot) | |
252b5132 | 5228 | { |
1579bae1 | 5229 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
5230 | { |
5231 | switch (s->header.type) | |
5232 | { | |
5233 | case lang_constructors_statement_enum: | |
420e579c | 5234 | dot = lang_do_assignments_1 (constructor_list.head, |
66e28d60 | 5235 | current_os, fill, dot); |
252b5132 RH |
5236 | break; |
5237 | ||
5238 | case lang_output_section_statement_enum: | |
5239 | { | |
d1778b88 | 5240 | lang_output_section_statement_type *os; |
252b5132 | 5241 | |
d1778b88 | 5242 | os = &(s->output_section_statement); |
75ff4589 | 5243 | if (os->bfd_section != NULL && !os->ignored) |
252b5132 RH |
5244 | { |
5245 | dot = os->bfd_section->vma; | |
66e28d60 | 5246 | |
cde9e0be | 5247 | lang_do_assignments_1 (os->children.head, os, os->fill, dot); |
66e28d60 | 5248 | |
3737f867 JJ |
5249 | /* .tbss sections effectively have zero size. */ |
5250 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
5251 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
5252 | || link_info.relocatable) | |
cde9e0be | 5253 | dot += TO_ADDR (os->bfd_section->size); |
5590fba9 NC |
5254 | |
5255 | if (os->update_dot_tree != NULL) | |
5256 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); | |
252b5132 RH |
5257 | } |
5258 | } | |
5259 | break; | |
e9ee469a | 5260 | |
252b5132 RH |
5261 | case lang_wild_statement_enum: |
5262 | ||
420e579c | 5263 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
66e28d60 | 5264 | current_os, fill, dot); |
252b5132 RH |
5265 | break; |
5266 | ||
5267 | case lang_object_symbols_statement_enum: | |
5268 | case lang_output_statement_enum: | |
5269 | case lang_target_statement_enum: | |
252b5132 | 5270 | break; |
e9ee469a | 5271 | |
252b5132 | 5272 | case lang_data_statement_enum: |
e9ee469a AM |
5273 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
5274 | if (expld.result.valid_p) | |
5275 | s->data_statement.value = (expld.result.value | |
5276 | + expld.result.section->vma); | |
5277 | else | |
5278 | einfo (_("%F%P: invalid data statement\n")); | |
b7a26f91 KH |
5279 | { |
5280 | unsigned int size; | |
08da4cac KH |
5281 | switch (s->data_statement.type) |
5282 | { | |
5283 | default: | |
5284 | abort (); | |
5285 | case QUAD: | |
5286 | case SQUAD: | |
5287 | size = QUAD_SIZE; | |
5288 | break; | |
5289 | case LONG: | |
5290 | size = LONG_SIZE; | |
5291 | break; | |
5292 | case SHORT: | |
5293 | size = SHORT_SIZE; | |
5294 | break; | |
5295 | case BYTE: | |
5296 | size = BYTE_SIZE; | |
5297 | break; | |
5298 | } | |
e5caa5e0 AM |
5299 | if (size < TO_SIZE ((unsigned) 1)) |
5300 | size = TO_SIZE ((unsigned) 1); | |
5301 | dot += TO_ADDR (size); | |
08da4cac | 5302 | } |
252b5132 RH |
5303 | break; |
5304 | ||
5305 | case lang_reloc_statement_enum: | |
e9ee469a AM |
5306 | exp_fold_tree (s->reloc_statement.addend_exp, |
5307 | bfd_abs_section_ptr, &dot); | |
5308 | if (expld.result.valid_p) | |
5309 | s->reloc_statement.addend_value = expld.result.value; | |
5310 | else | |
5311 | einfo (_("%F%P: invalid reloc statement\n")); | |
e5caa5e0 | 5312 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
5313 | break; |
5314 | ||
5315 | case lang_input_section_enum: | |
5316 | { | |
5317 | asection *in = s->input_section.section; | |
5318 | ||
57ceae94 | 5319 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 5320 | dot += TO_ADDR (in->size); |
252b5132 RH |
5321 | } |
5322 | break; | |
5323 | ||
5324 | case lang_input_statement_enum: | |
5325 | break; | |
e9ee469a | 5326 | |
252b5132 RH |
5327 | case lang_fill_statement_enum: |
5328 | fill = s->fill_statement.fill; | |
5329 | break; | |
252b5132 | 5330 | |
e9ee469a AM |
5331 | case lang_assignment_statement_enum: |
5332 | exp_fold_tree (s->assignment_statement.exp, | |
66e28d60 | 5333 | current_os->bfd_section, |
e9ee469a | 5334 | &dot); |
252b5132 | 5335 | break; |
e9ee469a | 5336 | |
252b5132 | 5337 | case lang_padding_statement_enum: |
e5caa5e0 | 5338 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
5339 | break; |
5340 | ||
5341 | case lang_group_statement_enum: | |
420e579c | 5342 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
66e28d60 | 5343 | current_os, fill, dot); |
252b5132 RH |
5344 | break; |
5345 | ||
53d25da6 | 5346 | case lang_insert_statement_enum: |
252b5132 | 5347 | break; |
e9ee469a | 5348 | |
252b5132 RH |
5349 | case lang_address_statement_enum: |
5350 | break; | |
53d25da6 AM |
5351 | |
5352 | default: | |
5353 | FAIL (); | |
5354 | break; | |
252b5132 | 5355 | } |
252b5132 RH |
5356 | } |
5357 | return dot; | |
5358 | } | |
5359 | ||
f2241121 | 5360 | void |
e9ee469a | 5361 | lang_do_assignments (void) |
420e579c | 5362 | { |
420e579c | 5363 | lang_statement_iteration++; |
e9ee469a | 5364 | lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0); |
420e579c HPN |
5365 | } |
5366 | ||
252b5132 RH |
5367 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
5368 | operator .startof. (section_name), it produces an undefined symbol | |
5369 | .startof.section_name. Similarly, when it sees | |
5370 | .sizeof. (section_name), it produces an undefined symbol | |
5371 | .sizeof.section_name. For all the output sections, we look for | |
5372 | such symbols, and set them to the correct value. */ | |
5373 | ||
5374 | static void | |
1579bae1 | 5375 | lang_set_startof (void) |
252b5132 RH |
5376 | { |
5377 | asection *s; | |
5378 | ||
1049f94e | 5379 | if (link_info.relocatable) |
252b5132 RH |
5380 | return; |
5381 | ||
f13a99db | 5382 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 RH |
5383 | { |
5384 | const char *secname; | |
5385 | char *buf; | |
5386 | struct bfd_link_hash_entry *h; | |
5387 | ||
f13a99db | 5388 | secname = bfd_get_section_name (link_info.output_bfd, s); |
252b5132 RH |
5389 | buf = xmalloc (10 + strlen (secname)); |
5390 | ||
5391 | sprintf (buf, ".startof.%s", secname); | |
b34976b6 | 5392 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
5393 | if (h != NULL && h->type == bfd_link_hash_undefined) |
5394 | { | |
5395 | h->type = bfd_link_hash_defined; | |
f13a99db | 5396 | h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s); |
252b5132 RH |
5397 | h->u.def.section = bfd_abs_section_ptr; |
5398 | } | |
5399 | ||
5400 | sprintf (buf, ".sizeof.%s", secname); | |
b34976b6 | 5401 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
5402 | if (h != NULL && h->type == bfd_link_hash_undefined) |
5403 | { | |
5404 | h->type = bfd_link_hash_defined; | |
eea6121a | 5405 | h->u.def.value = TO_ADDR (s->size); |
252b5132 RH |
5406 | h->u.def.section = bfd_abs_section_ptr; |
5407 | } | |
5408 | ||
5409 | free (buf); | |
5410 | } | |
5411 | } | |
5412 | ||
5413 | static void | |
750877ba | 5414 | lang_end (void) |
252b5132 RH |
5415 | { |
5416 | struct bfd_link_hash_entry *h; | |
b34976b6 | 5417 | bfd_boolean warn; |
252b5132 | 5418 | |
ac69cbc6 TG |
5419 | if ((link_info.relocatable && !link_info.gc_sections) |
5420 | || link_info.shared) | |
71934f94 | 5421 | warn = entry_from_cmdline; |
252b5132 | 5422 | else |
b34976b6 | 5423 | warn = TRUE; |
252b5132 | 5424 | |
ac69cbc6 TG |
5425 | /* Force the user to specify a root when generating a relocatable with |
5426 | --gc-sections. */ | |
5427 | if (link_info.gc_sections && link_info.relocatable | |
5428 | && (entry_symbol.name == NULL | |
5429 | && ldlang_undef_chain_list_head == NULL)) | |
5430 | einfo (_("%P%F: gc-sections requires either an entry or " | |
5431 | "an undefined symbol\n")); | |
5432 | ||
1579bae1 | 5433 | if (entry_symbol.name == NULL) |
252b5132 | 5434 | { |
a359509e ZW |
5435 | /* No entry has been specified. Look for the default entry, but |
5436 | don't warn if we don't find it. */ | |
5437 | entry_symbol.name = entry_symbol_default; | |
b34976b6 | 5438 | warn = FALSE; |
252b5132 RH |
5439 | } |
5440 | ||
e3e942e9 | 5441 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
b34976b6 | 5442 | FALSE, FALSE, TRUE); |
1579bae1 | 5443 | if (h != NULL |
252b5132 RH |
5444 | && (h->type == bfd_link_hash_defined |
5445 | || h->type == bfd_link_hash_defweak) | |
5446 | && h->u.def.section->output_section != NULL) | |
5447 | { | |
5448 | bfd_vma val; | |
5449 | ||
5450 | val = (h->u.def.value | |
f13a99db | 5451 | + bfd_get_section_vma (link_info.output_bfd, |
252b5132 RH |
5452 | h->u.def.section->output_section) |
5453 | + h->u.def.section->output_offset); | |
f13a99db | 5454 | if (! bfd_set_start_address (link_info.output_bfd, val)) |
e3e942e9 | 5455 | einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
5456 | } |
5457 | else | |
5458 | { | |
5459 | bfd_vma val; | |
5f992e62 | 5460 | const char *send; |
252b5132 RH |
5461 | |
5462 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 5463 | number. */ |
e3e942e9 | 5464 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
5465 | if (*send == '\0') |
5466 | { | |
f13a99db | 5467 | if (! bfd_set_start_address (link_info.output_bfd, val)) |
252b5132 RH |
5468 | einfo (_("%P%F: can't set start address\n")); |
5469 | } | |
5470 | else | |
5471 | { | |
5472 | asection *ts; | |
5473 | ||
5474 | /* Can't find the entry symbol, and it's not a number. Use | |
5475 | the first address in the text section. */ | |
f13a99db | 5476 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 5477 | if (ts != NULL) |
252b5132 RH |
5478 | { |
5479 | if (warn) | |
6feb9908 AM |
5480 | einfo (_("%P: warning: cannot find entry symbol %s;" |
5481 | " defaulting to %V\n"), | |
e3e942e9 | 5482 | entry_symbol.name, |
f13a99db AM |
5483 | bfd_get_section_vma (link_info.output_bfd, ts)); |
5484 | if (!(bfd_set_start_address | |
5485 | (link_info.output_bfd, | |
5486 | bfd_get_section_vma (link_info.output_bfd, ts)))) | |
252b5132 RH |
5487 | einfo (_("%P%F: can't set start address\n")); |
5488 | } | |
5489 | else | |
5490 | { | |
5491 | if (warn) | |
6feb9908 AM |
5492 | einfo (_("%P: warning: cannot find entry symbol %s;" |
5493 | " not setting start address\n"), | |
e3e942e9 | 5494 | entry_symbol.name); |
252b5132 RH |
5495 | } |
5496 | } | |
5497 | } | |
420e579c | 5498 | |
7115639b AM |
5499 | /* Don't bfd_hash_table_free (&lang_definedness_table); |
5500 | map file output may result in a call of lang_track_definedness. */ | |
252b5132 RH |
5501 | } |
5502 | ||
5503 | /* This is a small function used when we want to ignore errors from | |
5504 | BFD. */ | |
5505 | ||
5506 | static void | |
87f2a346 | 5507 | ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) |
252b5132 RH |
5508 | { |
5509 | /* Don't do anything. */ | |
5510 | } | |
5511 | ||
5512 | /* Check that the architecture of all the input files is compatible | |
5513 | with the output file. Also call the backend to let it do any | |
5514 | other checking that is needed. */ | |
5515 | ||
5516 | static void | |
1579bae1 | 5517 | lang_check (void) |
252b5132 RH |
5518 | { |
5519 | lang_statement_union_type *file; | |
5520 | bfd *input_bfd; | |
5f992e62 | 5521 | const bfd_arch_info_type *compatible; |
252b5132 | 5522 | |
1579bae1 | 5523 | for (file = file_chain.head; file != NULL; file = file->input_statement.next) |
252b5132 RH |
5524 | { |
5525 | input_bfd = file->input_statement.the_bfd; | |
6feb9908 | 5526 | compatible |
f13a99db | 5527 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 5528 | command_line.accept_unknown_input_arch); |
30cba025 AM |
5529 | |
5530 | /* In general it is not possible to perform a relocatable | |
5531 | link between differing object formats when the input | |
5532 | file has relocations, because the relocations in the | |
5533 | input format may not have equivalent representations in | |
5534 | the output format (and besides BFD does not translate | |
5535 | relocs for other link purposes than a final link). */ | |
1049f94e | 5536 | if ((link_info.relocatable || link_info.emitrelocations) |
30cba025 | 5537 | && (compatible == NULL |
f13a99db AM |
5538 | || (bfd_get_flavour (input_bfd) |
5539 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
5540 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
5541 | { | |
6feb9908 AM |
5542 | einfo (_("%P%F: Relocatable linking with relocations from" |
5543 | " format %s (%B) to format %s (%B) is not supported\n"), | |
30cba025 | 5544 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 5545 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
5546 | /* einfo with %F exits. */ |
5547 | } | |
5548 | ||
252b5132 RH |
5549 | if (compatible == NULL) |
5550 | { | |
5551 | if (command_line.warn_mismatch) | |
9e7750f2 | 5552 | einfo (_("%P%X: %s architecture of input file `%B'" |
6feb9908 | 5553 | " is incompatible with %s output\n"), |
252b5132 | 5554 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 5555 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 5556 | } |
b9247304 | 5557 | else if (bfd_count_sections (input_bfd)) |
252b5132 | 5558 | { |
b9247304 | 5559 | /* If the input bfd has no contents, it shouldn't set the |
b7a26f91 | 5560 | private data of the output bfd. */ |
b9247304 | 5561 | |
252b5132 RH |
5562 | bfd_error_handler_type pfn = NULL; |
5563 | ||
5564 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
5565 | files, temporarily set the BFD error handler to a |
5566 | function which will do nothing. We still want to call | |
5567 | bfd_merge_private_bfd_data, since it may set up | |
5568 | information which is needed in the output file. */ | |
252b5132 RH |
5569 | if (! command_line.warn_mismatch) |
5570 | pfn = bfd_set_error_handler (ignore_bfd_errors); | |
f13a99db | 5571 | if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd)) |
252b5132 RH |
5572 | { |
5573 | if (command_line.warn_mismatch) | |
6feb9908 AM |
5574 | einfo (_("%P%X: failed to merge target specific data" |
5575 | " of file %B\n"), input_bfd); | |
252b5132 RH |
5576 | } |
5577 | if (! command_line.warn_mismatch) | |
5578 | bfd_set_error_handler (pfn); | |
5579 | } | |
5580 | } | |
5581 | } | |
5582 | ||
5583 | /* Look through all the global common symbols and attach them to the | |
5584 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 5585 | to roughly sort the entries by alignment. */ |
252b5132 RH |
5586 | |
5587 | static void | |
1579bae1 | 5588 | lang_common (void) |
252b5132 | 5589 | { |
4818e05f AM |
5590 | if (command_line.inhibit_common_definition) |
5591 | return; | |
1049f94e | 5592 | if (link_info.relocatable |
252b5132 RH |
5593 | && ! command_line.force_common_definition) |
5594 | return; | |
5595 | ||
5596 | if (! config.sort_common) | |
1579bae1 | 5597 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
5598 | else |
5599 | { | |
de7dd2bd NC |
5600 | unsigned int power; |
5601 | ||
5602 | if (config.sort_common == sort_descending) | |
967928e9 AM |
5603 | { |
5604 | for (power = 4; power > 0; power--) | |
5605 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
5606 | ||
5607 | power = 0; | |
5608 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
5609 | } | |
de7dd2bd | 5610 | else |
967928e9 AM |
5611 | { |
5612 | for (power = 0; power <= 4; power++) | |
5613 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 5614 | |
967928e9 | 5615 | power = UINT_MAX; |
de7dd2bd NC |
5616 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
5617 | } | |
252b5132 RH |
5618 | } |
5619 | } | |
5620 | ||
5621 | /* Place one common symbol in the correct section. */ | |
5622 | ||
b34976b6 | 5623 | static bfd_boolean |
1579bae1 | 5624 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
5625 | { |
5626 | unsigned int power_of_two; | |
5627 | bfd_vma size; | |
5628 | asection *section; | |
5629 | ||
5630 | if (h->type != bfd_link_hash_common) | |
b34976b6 | 5631 | return TRUE; |
252b5132 RH |
5632 | |
5633 | size = h->u.c.size; | |
5634 | power_of_two = h->u.c.p->alignment_power; | |
5635 | ||
de7dd2bd NC |
5636 | if (config.sort_common == sort_descending |
5637 | && power_of_two < *(unsigned int *) info) | |
5638 | return TRUE; | |
5639 | else if (config.sort_common == sort_ascending | |
967928e9 | 5640 | && power_of_two > *(unsigned int *) info) |
b34976b6 | 5641 | return TRUE; |
252b5132 RH |
5642 | |
5643 | section = h->u.c.p->section; | |
5644 | ||
e5caa5e0 | 5645 | /* Increase the size of the section to align the common sym. */ |
eea6121a AM |
5646 | section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1; |
5647 | section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift)); | |
252b5132 RH |
5648 | |
5649 | /* Adjust the alignment if necessary. */ | |
5650 | if (power_of_two > section->alignment_power) | |
5651 | section->alignment_power = power_of_two; | |
5652 | ||
5653 | /* Change the symbol from common to defined. */ | |
5654 | h->type = bfd_link_hash_defined; | |
5655 | h->u.def.section = section; | |
eea6121a | 5656 | h->u.def.value = section->size; |
252b5132 RH |
5657 | |
5658 | /* Increase the size of the section. */ | |
eea6121a | 5659 | section->size += size; |
252b5132 RH |
5660 | |
5661 | /* Make sure the section is allocated in memory, and make sure that | |
5662 | it is no longer a common section. */ | |
5663 | section->flags |= SEC_ALLOC; | |
08da4cac | 5664 | section->flags &= ~SEC_IS_COMMON; |
252b5132 RH |
5665 | |
5666 | if (config.map_file != NULL) | |
5667 | { | |
b34976b6 | 5668 | static bfd_boolean header_printed; |
252b5132 RH |
5669 | int len; |
5670 | char *name; | |
5671 | char buf[50]; | |
5672 | ||
5673 | if (! header_printed) | |
5674 | { | |
5675 | minfo (_("\nAllocating common symbols\n")); | |
5676 | minfo (_("Common symbol size file\n\n")); | |
b34976b6 | 5677 | header_printed = TRUE; |
252b5132 RH |
5678 | } |
5679 | ||
f13a99db | 5680 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 5681 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
5682 | if (name == NULL) |
5683 | { | |
5684 | minfo ("%s", h->root.string); | |
5685 | len = strlen (h->root.string); | |
5686 | } | |
5687 | else | |
5688 | { | |
5689 | minfo ("%s", name); | |
5690 | len = strlen (name); | |
5691 | free (name); | |
5692 | } | |
252b5132 RH |
5693 | |
5694 | if (len >= 19) | |
5695 | { | |
5696 | print_nl (); | |
5697 | len = 0; | |
5698 | } | |
5699 | while (len < 20) | |
5700 | { | |
5701 | print_space (); | |
5702 | ++len; | |
5703 | } | |
5704 | ||
5705 | minfo ("0x"); | |
5706 | if (size <= 0xffffffff) | |
5707 | sprintf (buf, "%lx", (unsigned long) size); | |
5708 | else | |
5709 | sprintf_vma (buf, size); | |
5710 | minfo ("%s", buf); | |
5711 | len = strlen (buf); | |
5712 | ||
5713 | while (len < 16) | |
5714 | { | |
5715 | print_space (); | |
5716 | ++len; | |
5717 | } | |
5718 | ||
5719 | minfo ("%B\n", section->owner); | |
5720 | } | |
5721 | ||
b34976b6 | 5722 | return TRUE; |
252b5132 RH |
5723 | } |
5724 | ||
08da4cac KH |
5725 | /* Run through the input files and ensure that every input section has |
5726 | somewhere to go. If one is found without a destination then create | |
5727 | an input request and place it into the statement tree. */ | |
252b5132 RH |
5728 | |
5729 | static void | |
1579bae1 | 5730 | lang_place_orphans (void) |
252b5132 | 5731 | { |
e50d8076 | 5732 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
5733 | { |
5734 | asection *s; | |
5735 | ||
1579bae1 | 5736 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 5737 | { |
1579bae1 | 5738 | if (s->output_section == NULL) |
252b5132 | 5739 | { |
396a2467 | 5740 | /* This section of the file is not attached, root |
afd7a018 | 5741 | around for a sensible place for it to go. */ |
252b5132 RH |
5742 | |
5743 | if (file->just_syms_flag) | |
1449d79b AM |
5744 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5745 | else if ((s->flags & SEC_EXCLUDE) != 0) | |
164e712d | 5746 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
5747 | else if (strcmp (s->name, "COMMON") == 0) |
5748 | { | |
5749 | /* This is a lonely common section which must have | |
5750 | come from an archive. We attach to the section | |
5751 | with the wildcard. */ | |
1049f94e | 5752 | if (! link_info.relocatable |
252b5132 RH |
5753 | || command_line.force_common_definition) |
5754 | { | |
5755 | if (default_common_section == NULL) | |
66c103b7 AM |
5756 | default_common_section |
5757 | = lang_output_section_statement_lookup (".bss", 0, | |
5758 | TRUE); | |
39dcfe18 | 5759 | lang_add_section (&default_common_section->children, s, |
7b986e99 | 5760 | default_common_section); |
252b5132 RH |
5761 | } |
5762 | } | |
252b5132 RH |
5763 | else |
5764 | { | |
bcacc0f5 | 5765 | const char *name = s->name; |
8a99a385 | 5766 | int constraint = 0; |
252b5132 | 5767 | |
8a99a385 AM |
5768 | if (config.unique_orphan_sections || unique_section_p (s)) |
5769 | constraint = SPECIAL; | |
bcacc0f5 | 5770 | |
8a99a385 | 5771 | if (!ldemul_place_orphan (s, name, constraint)) |
bcacc0f5 AM |
5772 | { |
5773 | lang_output_section_statement_type *os; | |
8a99a385 AM |
5774 | os = lang_output_section_statement_lookup (name, |
5775 | constraint, | |
bcacc0f5 AM |
5776 | TRUE); |
5777 | lang_add_section (&os->children, s, os); | |
5778 | } | |
252b5132 RH |
5779 | } |
5780 | } | |
5781 | } | |
5782 | } | |
5783 | } | |
5784 | ||
252b5132 | 5785 | void |
1579bae1 | 5786 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 5787 | { |
aa8804e4 | 5788 | flagword *ptr_flags; |
252b5132 | 5789 | |
aa8804e4 | 5790 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
252b5132 RH |
5791 | while (*flags) |
5792 | { | |
5793 | switch (*flags) | |
5794 | { | |
252b5132 RH |
5795 | case 'A': case 'a': |
5796 | *ptr_flags |= SEC_ALLOC; | |
5797 | break; | |
5798 | ||
5799 | case 'R': case 'r': | |
5800 | *ptr_flags |= SEC_READONLY; | |
5801 | break; | |
5802 | ||
5803 | case 'W': case 'w': | |
5804 | *ptr_flags |= SEC_DATA; | |
5805 | break; | |
5806 | ||
5807 | case 'X': case 'x': | |
5808 | *ptr_flags |= SEC_CODE; | |
5809 | break; | |
5810 | ||
5811 | case 'L': case 'l': | |
5812 | case 'I': case 'i': | |
5813 | *ptr_flags |= SEC_LOAD; | |
5814 | break; | |
5815 | ||
5816 | default: | |
5817 | einfo (_("%P%F: invalid syntax in flags\n")); | |
5818 | break; | |
5819 | } | |
5820 | flags++; | |
5821 | } | |
5822 | } | |
5823 | ||
5824 | /* Call a function on each input file. This function will be called | |
5825 | on an archive, but not on the elements. */ | |
5826 | ||
5827 | void | |
1579bae1 | 5828 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
5829 | { |
5830 | lang_input_statement_type *f; | |
5831 | ||
5832 | for (f = (lang_input_statement_type *) input_file_chain.head; | |
5833 | f != NULL; | |
5834 | f = (lang_input_statement_type *) f->next_real_file) | |
5835 | func (f); | |
5836 | } | |
5837 | ||
5838 | /* Call a function on each file. The function will be called on all | |
5839 | the elements of an archive which are included in the link, but will | |
5840 | not be called on the archive file itself. */ | |
5841 | ||
5842 | void | |
1579bae1 | 5843 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 5844 | { |
e50d8076 | 5845 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 RH |
5846 | { |
5847 | func (f); | |
5848 | } | |
5849 | } | |
5850 | ||
252b5132 | 5851 | void |
1579bae1 | 5852 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 5853 | { |
252b5132 RH |
5854 | lang_statement_append (&file_chain, |
5855 | (lang_statement_union_type *) entry, | |
5856 | &entry->next); | |
5857 | ||
5858 | /* The BFD linker needs to have a list of all input BFDs involved in | |
5859 | a link. */ | |
1579bae1 | 5860 | ASSERT (entry->the_bfd->link_next == NULL); |
f13a99db | 5861 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
5862 | |
5863 | *link_info.input_bfds_tail = entry->the_bfd; | |
5864 | link_info.input_bfds_tail = &entry->the_bfd->link_next; | |
1579bae1 | 5865 | entry->the_bfd->usrdata = entry; |
252b5132 RH |
5866 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
5867 | ||
5868 | /* Look through the sections and check for any which should not be | |
5869 | included in the link. We need to do this now, so that we can | |
5870 | notice when the backend linker tries to report multiple | |
5871 | definition errors for symbols which are in sections we aren't | |
5872 | going to link. FIXME: It might be better to entirely ignore | |
5873 | symbols which are defined in sections which are going to be | |
5874 | discarded. This would require modifying the backend linker for | |
5875 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
5876 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
5877 | ||
1579bae1 | 5878 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
5879 | } |
5880 | ||
5881 | void | |
1579bae1 | 5882 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
5883 | { |
5884 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 5885 | if (!had_output_filename || !from_script) |
252b5132 RH |
5886 | { |
5887 | output_filename = name; | |
b34976b6 | 5888 | had_output_filename = TRUE; |
252b5132 RH |
5889 | } |
5890 | } | |
5891 | ||
252b5132 RH |
5892 | static lang_output_section_statement_type *current_section; |
5893 | ||
5894 | static int | |
1579bae1 | 5895 | topower (int x) |
252b5132 RH |
5896 | { |
5897 | unsigned int i = 1; | |
5898 | int l; | |
5899 | ||
5900 | if (x < 0) | |
5901 | return -1; | |
5902 | ||
5f992e62 | 5903 | for (l = 0; l < 32; l++) |
252b5132 RH |
5904 | { |
5905 | if (i >= (unsigned int) x) | |
5906 | return l; | |
5907 | i <<= 1; | |
5908 | } | |
5909 | ||
5910 | return 0; | |
5911 | } | |
5912 | ||
aea4bd9d | 5913 | lang_output_section_statement_type * |
1579bae1 AM |
5914 | lang_enter_output_section_statement (const char *output_section_statement_name, |
5915 | etree_type *address_exp, | |
5916 | enum section_type sectype, | |
1579bae1 AM |
5917 | etree_type *align, |
5918 | etree_type *subalign, | |
0841712e JJ |
5919 | etree_type *ebase, |
5920 | int constraint) | |
252b5132 RH |
5921 | { |
5922 | lang_output_section_statement_type *os; | |
5923 | ||
66c103b7 AM |
5924 | os = lang_output_section_statement_lookup (output_section_statement_name, |
5925 | constraint, TRUE); | |
967928e9 | 5926 | current_section = os; |
252b5132 | 5927 | |
1579bae1 | 5928 | if (os->addr_tree == NULL) |
08da4cac KH |
5929 | { |
5930 | os->addr_tree = address_exp; | |
5931 | } | |
252b5132 RH |
5932 | os->sectype = sectype; |
5933 | if (sectype != noload_section) | |
5934 | os->flags = SEC_NO_FLAGS; | |
5935 | else | |
5936 | os->flags = SEC_NEVER_LOAD; | |
e5caa5e0 | 5937 | os->block_value = 1; |
66c103b7 AM |
5938 | |
5939 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 5940 | push_stat_ptr (&os->children); |
252b5132 | 5941 | |
08da4cac | 5942 | os->subsection_alignment = |
e9ee469a | 5943 | topower (exp_get_value_int (subalign, -1, "subsection alignment")); |
08da4cac | 5944 | os->section_alignment = |
e9ee469a | 5945 | topower (exp_get_value_int (align, -1, "section alignment")); |
252b5132 RH |
5946 | |
5947 | os->load_base = ebase; | |
aea4bd9d | 5948 | return os; |
252b5132 RH |
5949 | } |
5950 | ||
252b5132 | 5951 | void |
1579bae1 | 5952 | lang_final (void) |
252b5132 | 5953 | { |
bd4d42c1 | 5954 | lang_output_statement_type *new; |
252b5132 | 5955 | |
bd4d42c1 | 5956 | new = new_stat (lang_output_statement, stat_ptr); |
252b5132 RH |
5957 | new->name = output_filename; |
5958 | } | |
5959 | ||
08da4cac KH |
5960 | /* Reset the current counters in the regions. */ |
5961 | ||
e3dc8847 | 5962 | void |
1579bae1 | 5963 | lang_reset_memory_regions (void) |
252b5132 RH |
5964 | { |
5965 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 5966 | asection *o; |
e9ee469a | 5967 | lang_output_section_statement_type *os; |
252b5132 | 5968 | |
1579bae1 | 5969 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 5970 | { |
252b5132 | 5971 | p->current = p->origin; |
66e28d60 | 5972 | p->last_os = NULL; |
252b5132 | 5973 | } |
b3327aad | 5974 | |
e9ee469a AM |
5975 | for (os = &lang_output_section_statement.head->output_section_statement; |
5976 | os != NULL; | |
5977 | os = os->next) | |
cde9e0be AM |
5978 | { |
5979 | os->processed_vma = FALSE; | |
5980 | os->processed_lma = FALSE; | |
5981 | } | |
e9ee469a | 5982 | |
f13a99db | 5983 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
5984 | { |
5985 | /* Save the last size for possible use by bfd_relax_section. */ | |
5986 | o->rawsize = o->size; | |
5987 | o->size = 0; | |
5988 | } | |
252b5132 RH |
5989 | } |
5990 | ||
164e712d | 5991 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
5992 | |
5993 | static void | |
1579bae1 AM |
5994 | gc_section_callback (lang_wild_statement_type *ptr, |
5995 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
5996 | asection *section, | |
5997 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
5998 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 5999 | { |
164e712d AM |
6000 | /* If the wild pattern was marked KEEP, the member sections |
6001 | should be as well. */ | |
4dec4d4e RH |
6002 | if (ptr->keep_sections) |
6003 | section->flags |= SEC_KEEP; | |
252b5132 RH |
6004 | } |
6005 | ||
252b5132 RH |
6006 | /* Iterate over sections marking them against GC. */ |
6007 | ||
6008 | static void | |
1579bae1 | 6009 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 6010 | { |
1579bae1 | 6011 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6012 | { |
6013 | switch (s->header.type) | |
6014 | { | |
6015 | case lang_wild_statement_enum: | |
164e712d | 6016 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 6017 | break; |
252b5132 RH |
6018 | case lang_constructors_statement_enum: |
6019 | lang_gc_sections_1 (constructor_list.head); | |
6020 | break; | |
6021 | case lang_output_section_statement_enum: | |
6022 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
6023 | break; | |
6024 | case lang_group_statement_enum: | |
6025 | lang_gc_sections_1 (s->group_statement.children.head); | |
6026 | break; | |
6027 | default: | |
6028 | break; | |
6029 | } | |
6030 | } | |
6031 | } | |
6032 | ||
6033 | static void | |
1579bae1 | 6034 | lang_gc_sections (void) |
252b5132 | 6035 | { |
252b5132 RH |
6036 | /* Keep all sections so marked in the link script. */ |
6037 | ||
6038 | lang_gc_sections_1 (statement_list.head); | |
6039 | ||
9ca57817 AM |
6040 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
6041 | the special case of debug info. (See bfd/stabs.c) | |
6042 | Twiddle the flag here, to simplify later linker code. */ | |
6043 | if (link_info.relocatable) | |
6044 | { | |
6045 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
6046 | { | |
6047 | asection *sec; | |
6048 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) | |
6049 | if ((sec->flags & SEC_DEBUGGING) == 0) | |
6050 | sec->flags &= ~SEC_EXCLUDE; | |
6051 | } | |
6052 | } | |
6053 | ||
57316bff | 6054 | if (link_info.gc_sections) |
f13a99db | 6055 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
6056 | } |
6057 | ||
b10a8ae0 L |
6058 | /* Worker for lang_find_relro_sections_1. */ |
6059 | ||
6060 | static void | |
6061 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
6062 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
6063 | asection *section, | |
6064 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
6065 | void *data) | |
6066 | { | |
6067 | /* Discarded, excluded and ignored sections effectively have zero | |
6068 | size. */ | |
6069 | if (section->output_section != NULL | |
f13a99db | 6070 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
6071 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
6072 | && !IGNORE_SECTION (section) | |
6073 | && section->size != 0) | |
6074 | { | |
6075 | bfd_boolean *has_relro_section = (bfd_boolean *) data; | |
6076 | *has_relro_section = TRUE; | |
6077 | } | |
6078 | } | |
6079 | ||
6080 | /* Iterate over sections for relro sections. */ | |
6081 | ||
6082 | static void | |
6083 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
6084 | bfd_boolean *has_relro_section) | |
6085 | { | |
6086 | if (*has_relro_section) | |
6087 | return; | |
6088 | ||
6089 | for (; s != NULL; s = s->header.next) | |
6090 | { | |
6091 | if (s == expld.dataseg.relro_end_stat) | |
6092 | break; | |
6093 | ||
6094 | switch (s->header.type) | |
6095 | { | |
6096 | case lang_wild_statement_enum: | |
6097 | walk_wild (&s->wild_statement, | |
6098 | find_relro_section_callback, | |
6099 | has_relro_section); | |
6100 | break; | |
6101 | case lang_constructors_statement_enum: | |
6102 | lang_find_relro_sections_1 (constructor_list.head, | |
6103 | has_relro_section); | |
6104 | break; | |
6105 | case lang_output_section_statement_enum: | |
6106 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
6107 | has_relro_section); | |
6108 | break; | |
6109 | case lang_group_statement_enum: | |
6110 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
6111 | has_relro_section); | |
6112 | break; | |
6113 | default: | |
6114 | break; | |
6115 | } | |
6116 | } | |
6117 | } | |
6118 | ||
6119 | static void | |
6120 | lang_find_relro_sections (void) | |
6121 | { | |
6122 | bfd_boolean has_relro_section = FALSE; | |
6123 | ||
6124 | /* Check all sections in the link script. */ | |
6125 | ||
6126 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
6127 | &has_relro_section); | |
6128 | ||
6129 | if (!has_relro_section) | |
6130 | link_info.relro = FALSE; | |
6131 | } | |
6132 | ||
fbbc3759 L |
6133 | /* Relax all sections until bfd_relax_section gives up. */ |
6134 | ||
6135 | static void | |
6136 | relax_sections (void) | |
6137 | { | |
6138 | /* Keep relaxing until bfd_relax_section gives up. */ | |
6139 | bfd_boolean relax_again; | |
6140 | ||
4a828aab | 6141 | link_info.relax_trip = -1; |
fbbc3759 L |
6142 | do |
6143 | { | |
329c1c86 | 6144 | relax_again = FALSE; |
4a828aab | 6145 | link_info.relax_trip++; |
fbbc3759 L |
6146 | |
6147 | /* Note: pe-dll.c does something like this also. If you find | |
6148 | you need to change this code, you probably need to change | |
6149 | pe-dll.c also. DJ */ | |
6150 | ||
6151 | /* Do all the assignments with our current guesses as to | |
6152 | section sizes. */ | |
6153 | lang_do_assignments (); | |
6154 | ||
6155 | /* We must do this after lang_do_assignments, because it uses | |
6156 | size. */ | |
6157 | lang_reset_memory_regions (); | |
6158 | ||
6159 | /* Perform another relax pass - this time we know where the | |
6160 | globals are, so can make a better guess. */ | |
6161 | lang_size_sections (&relax_again, FALSE); | |
6162 | } | |
6163 | while (relax_again); | |
6164 | } | |
6165 | ||
252b5132 | 6166 | void |
1579bae1 | 6167 | lang_process (void) |
252b5132 | 6168 | { |
55255dae | 6169 | /* Finalize dynamic list. */ |
40b36307 L |
6170 | if (link_info.dynamic_list) |
6171 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 6172 | |
252b5132 RH |
6173 | current_target = default_target; |
6174 | ||
08da4cac KH |
6175 | /* Open the output file. */ |
6176 | lang_for_each_statement (ldlang_open_output); | |
e5caa5e0 | 6177 | init_opb (); |
252b5132 RH |
6178 | |
6179 | ldemul_create_output_section_statements (); | |
6180 | ||
08da4cac | 6181 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
6182 | lang_place_undefineds (); |
6183 | ||
082b7297 L |
6184 | if (!bfd_section_already_linked_table_init ()) |
6185 | einfo (_("%P%F: Failed to create hash table\n")); | |
9503fd87 | 6186 | |
08da4cac | 6187 | /* Create a bfd for each input file. */ |
252b5132 | 6188 | current_target = default_target; |
b34976b6 | 6189 | open_input_bfds (statement_list.head, FALSE); |
252b5132 | 6190 | |
e3e942e9 AM |
6191 | link_info.gc_sym_list = &entry_symbol; |
6192 | if (entry_symbol.name == NULL) | |
6193 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
6194 | ||
252b5132 RH |
6195 | ldemul_after_open (); |
6196 | ||
082b7297 | 6197 | bfd_section_already_linked_table_free (); |
9503fd87 | 6198 | |
252b5132 RH |
6199 | /* Make sure that we're not mixing architectures. We call this |
6200 | after all the input files have been opened, but before we do any | |
6201 | other processing, so that any operations merge_private_bfd_data | |
6202 | does on the output file will be known during the rest of the | |
6203 | link. */ | |
6204 | lang_check (); | |
6205 | ||
6206 | /* Handle .exports instead of a version script if we're told to do so. */ | |
6207 | if (command_line.version_exports_section) | |
6208 | lang_do_version_exports_section (); | |
6209 | ||
6210 | /* Build all sets based on the information gathered from the input | |
6211 | files. */ | |
6212 | ldctor_build_sets (); | |
6213 | ||
6214 | /* Remove unreferenced sections if asked to. */ | |
164e712d | 6215 | lang_gc_sections (); |
252b5132 | 6216 | |
08da4cac | 6217 | /* Size up the common data. */ |
252b5132 RH |
6218 | lang_common (); |
6219 | ||
bcaa7b3e L |
6220 | /* Update wild statements. */ |
6221 | update_wild_statements (statement_list.head); | |
6222 | ||
252b5132 | 6223 | /* Run through the contours of the script and attach input sections |
08da4cac | 6224 | to the correct output sections. */ |
1579bae1 | 6225 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 6226 | |
53d25da6 AM |
6227 | process_insert_statements (); |
6228 | ||
08da4cac | 6229 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
6230 | lang_place_orphans (); |
6231 | ||
1049f94e | 6232 | if (! link_info.relocatable) |
862120bd | 6233 | { |
57ceae94 AM |
6234 | asection *found; |
6235 | ||
6236 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
6237 | sections, so that GCed sections are not merged, but before | |
6238 | assigning dynamic symbols, since removing whole input sections | |
6239 | is hard then. */ | |
f13a99db | 6240 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 6241 | |
862120bd | 6242 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 6243 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 6244 | |
1579bae1 | 6245 | if (found != NULL) |
862120bd AM |
6246 | { |
6247 | if (config.text_read_only) | |
6248 | found->flags |= SEC_READONLY; | |
6249 | else | |
6250 | found->flags &= ~SEC_READONLY; | |
6251 | } | |
6252 | } | |
6253 | ||
6254 | /* Do anything special before sizing sections. This is where ELF | |
6255 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
6256 | ldemul_before_allocation (); |
6257 | ||
6258 | /* We must record the program headers before we try to fix the | |
6259 | section positions, since they will affect SIZEOF_HEADERS. */ | |
6260 | lang_record_phdrs (); | |
6261 | ||
b10a8ae0 L |
6262 | /* Check relro sections. */ |
6263 | if (link_info.relro && ! link_info.relocatable) | |
6264 | lang_find_relro_sections (); | |
6265 | ||
b3327aad | 6266 | /* Size up the sections. */ |
e9ee469a | 6267 | lang_size_sections (NULL, !command_line.relax); |
b3327aad | 6268 | |
08da4cac | 6269 | /* Now run around and relax if we can. */ |
252b5132 RH |
6270 | if (command_line.relax) |
6271 | { | |
fbbc3759 L |
6272 | /* We may need more than one relaxation pass. */ |
6273 | int i = link_info.relax_pass; | |
252b5132 | 6274 | |
fbbc3759 L |
6275 | /* The backend can use it to determine the current pass. */ |
6276 | link_info.relax_pass = 0; | |
252b5132 | 6277 | |
fbbc3759 L |
6278 | while (i--) |
6279 | { | |
6280 | relax_sections (); | |
6281 | link_info.relax_pass++; | |
252b5132 | 6282 | } |
cf888e70 NC |
6283 | |
6284 | /* Final extra sizing to report errors. */ | |
e9ee469a | 6285 | lang_do_assignments (); |
5a46fe39 | 6286 | lang_reset_memory_regions (); |
e9ee469a | 6287 | lang_size_sections (NULL, TRUE); |
252b5132 | 6288 | } |
252b5132 RH |
6289 | |
6290 | /* See if anything special should be done now we know how big | |
6291 | everything is. */ | |
6292 | ldemul_after_allocation (); | |
6293 | ||
6294 | /* Fix any .startof. or .sizeof. symbols. */ | |
6295 | lang_set_startof (); | |
6296 | ||
08da4cac KH |
6297 | /* Do all the assignments, now that we know the final resting places |
6298 | of all the symbols. */ | |
252b5132 | 6299 | |
e9ee469a | 6300 | lang_do_assignments (); |
252b5132 | 6301 | |
8ded5a0f AM |
6302 | ldemul_finish (); |
6303 | ||
252b5132 | 6304 | /* Make sure that the section addresses make sense. */ |
662ef24b | 6305 | if (command_line.check_section_addresses) |
252b5132 | 6306 | lang_check_section_addresses (); |
5f992e62 | 6307 | |
750877ba | 6308 | lang_end (); |
252b5132 RH |
6309 | } |
6310 | ||
6311 | /* EXPORTED TO YACC */ | |
6312 | ||
6313 | void | |
1579bae1 AM |
6314 | lang_add_wild (struct wildcard_spec *filespec, |
6315 | struct wildcard_list *section_list, | |
6316 | bfd_boolean keep_sections) | |
252b5132 | 6317 | { |
b6bf44ba AM |
6318 | struct wildcard_list *curr, *next; |
6319 | lang_wild_statement_type *new; | |
6320 | ||
6321 | /* Reverse the list as the parser puts it back to front. */ | |
6322 | for (curr = section_list, section_list = NULL; | |
6323 | curr != NULL; | |
6324 | section_list = curr, curr = next) | |
6325 | { | |
6326 | if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0) | |
b34976b6 | 6327 | placed_commons = TRUE; |
252b5132 | 6328 | |
b6bf44ba AM |
6329 | next = curr->next; |
6330 | curr->next = section_list; | |
6331 | } | |
6332 | ||
6333 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 6334 | { |
b6bf44ba AM |
6335 | if (strcmp (filespec->name, "*") == 0) |
6336 | filespec->name = NULL; | |
6337 | else if (! wildcardp (filespec->name)) | |
b34976b6 | 6338 | lang_has_input_file = TRUE; |
252b5132 | 6339 | } |
b6bf44ba AM |
6340 | |
6341 | new = new_stat (lang_wild_statement, stat_ptr); | |
6342 | new->filename = NULL; | |
b34976b6 | 6343 | new->filenames_sorted = FALSE; |
b6bf44ba | 6344 | if (filespec != NULL) |
252b5132 | 6345 | { |
b6bf44ba | 6346 | new->filename = filespec->name; |
bcaa7b3e | 6347 | new->filenames_sorted = filespec->sorted == by_name; |
252b5132 | 6348 | } |
b6bf44ba | 6349 | new->section_list = section_list; |
252b5132 | 6350 | new->keep_sections = keep_sections; |
252b5132 | 6351 | lang_list_init (&new->children); |
72223188 | 6352 | analyze_walk_wild_section_handler (new); |
252b5132 RH |
6353 | } |
6354 | ||
6355 | void | |
ba916c8a MM |
6356 | lang_section_start (const char *name, etree_type *address, |
6357 | const segment_type *segment) | |
252b5132 | 6358 | { |
d1778b88 | 6359 | lang_address_statement_type *ad; |
252b5132 | 6360 | |
d1778b88 | 6361 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
6362 | ad->section_name = name; |
6363 | ad->address = address; | |
ba916c8a | 6364 | ad->segment = segment; |
252b5132 RH |
6365 | } |
6366 | ||
6367 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
6368 | because of a -e argument on the command line, or zero if this is | |
6369 | called by ENTRY in a linker script. Command line arguments take | |
6370 | precedence. */ | |
6371 | ||
6372 | void | |
1579bae1 | 6373 | lang_add_entry (const char *name, bfd_boolean cmdline) |
252b5132 | 6374 | { |
e3e942e9 | 6375 | if (entry_symbol.name == NULL |
252b5132 RH |
6376 | || cmdline |
6377 | || ! entry_from_cmdline) | |
6378 | { | |
e3e942e9 | 6379 | entry_symbol.name = name; |
252b5132 RH |
6380 | entry_from_cmdline = cmdline; |
6381 | } | |
6382 | } | |
6383 | ||
a359509e ZW |
6384 | /* Set the default start symbol to NAME. .em files should use this, |
6385 | not lang_add_entry, to override the use of "start" if neither the | |
6386 | linker script nor the command line specifies an entry point. NAME | |
6387 | must be permanently allocated. */ | |
6388 | void | |
6389 | lang_default_entry (const char *name) | |
6390 | { | |
6391 | entry_symbol_default = name; | |
6392 | } | |
6393 | ||
252b5132 | 6394 | void |
1579bae1 | 6395 | lang_add_target (const char *name) |
252b5132 | 6396 | { |
bd4d42c1 | 6397 | lang_target_statement_type *new; |
252b5132 | 6398 | |
bd4d42c1 | 6399 | new = new_stat (lang_target_statement, stat_ptr); |
252b5132 | 6400 | new->target = name; |
252b5132 RH |
6401 | } |
6402 | ||
6403 | void | |
1579bae1 | 6404 | lang_add_map (const char *name) |
252b5132 RH |
6405 | { |
6406 | while (*name) | |
6407 | { | |
6408 | switch (*name) | |
6409 | { | |
08da4cac | 6410 | case 'F': |
b34976b6 | 6411 | map_option_f = TRUE; |
252b5132 RH |
6412 | break; |
6413 | } | |
6414 | name++; | |
6415 | } | |
6416 | } | |
6417 | ||
6418 | void | |
1579bae1 | 6419 | lang_add_fill (fill_type *fill) |
252b5132 | 6420 | { |
bd4d42c1 | 6421 | lang_fill_statement_type *new; |
252b5132 | 6422 | |
bd4d42c1 | 6423 | new = new_stat (lang_fill_statement, stat_ptr); |
2c382fb6 | 6424 | new->fill = fill; |
252b5132 RH |
6425 | } |
6426 | ||
6427 | void | |
1579bae1 | 6428 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 6429 | { |
bd4d42c1 | 6430 | lang_data_statement_type *new; |
252b5132 | 6431 | |
bd4d42c1 | 6432 | new = new_stat (lang_data_statement, stat_ptr); |
252b5132 RH |
6433 | new->exp = exp; |
6434 | new->type = type; | |
252b5132 RH |
6435 | } |
6436 | ||
6437 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
6438 | generate. HOWTO is the corresponding howto structure (we could | |
6439 | look this up, but the caller has already done so). SECTION is the | |
6440 | section to generate a reloc against, or NAME is the name of the | |
6441 | symbol to generate a reloc against. Exactly one of SECTION and | |
6442 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
6443 | ||
6444 | void | |
1579bae1 AM |
6445 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
6446 | reloc_howto_type *howto, | |
6447 | asection *section, | |
6448 | const char *name, | |
6449 | union etree_union *addend) | |
252b5132 RH |
6450 | { |
6451 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 6452 | |
252b5132 RH |
6453 | p->reloc = reloc; |
6454 | p->howto = howto; | |
6455 | p->section = section; | |
6456 | p->name = name; | |
6457 | p->addend_exp = addend; | |
6458 | ||
6459 | p->addend_value = 0; | |
6460 | p->output_section = NULL; | |
7fabd029 | 6461 | p->output_offset = 0; |
252b5132 RH |
6462 | } |
6463 | ||
6464 | lang_assignment_statement_type * | |
1579bae1 | 6465 | lang_add_assignment (etree_type *exp) |
252b5132 | 6466 | { |
bd4d42c1 | 6467 | lang_assignment_statement_type *new; |
252b5132 | 6468 | |
bd4d42c1 | 6469 | new = new_stat (lang_assignment_statement, stat_ptr); |
252b5132 RH |
6470 | new->exp = exp; |
6471 | return new; | |
6472 | } | |
6473 | ||
6474 | void | |
1579bae1 | 6475 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 6476 | { |
bd4d42c1 | 6477 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
6478 | } |
6479 | ||
6480 | void | |
1579bae1 | 6481 | lang_startup (const char *name) |
252b5132 | 6482 | { |
1579bae1 | 6483 | if (startup_file != NULL) |
252b5132 | 6484 | { |
4520f868 | 6485 | einfo (_("%P%F: multiple STARTUP files\n")); |
252b5132 RH |
6486 | } |
6487 | first_file->filename = name; | |
6488 | first_file->local_sym_name = name; | |
b34976b6 | 6489 | first_file->real = TRUE; |
252b5132 RH |
6490 | |
6491 | startup_file = name; | |
6492 | } | |
6493 | ||
6494 | void | |
1579bae1 | 6495 | lang_float (bfd_boolean maybe) |
252b5132 RH |
6496 | { |
6497 | lang_float_flag = maybe; | |
6498 | } | |
6499 | ||
ee3cc2e2 RS |
6500 | |
6501 | /* Work out the load- and run-time regions from a script statement, and | |
6502 | store them in *LMA_REGION and *REGION respectively. | |
6503 | ||
a747ee4d NC |
6504 | MEMSPEC is the name of the run-time region, or the value of |
6505 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
6506 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
6507 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
6508 | had an explicit load address. | |
ee3cc2e2 RS |
6509 | |
6510 | It is an error to specify both a load region and a load address. */ | |
6511 | ||
6512 | static void | |
6bdafbeb NC |
6513 | lang_get_regions (lang_memory_region_type **region, |
6514 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
6515 | const char *memspec, |
6516 | const char *lma_memspec, | |
6bdafbeb NC |
6517 | bfd_boolean have_lma, |
6518 | bfd_boolean have_vma) | |
ee3cc2e2 | 6519 | { |
a747ee4d | 6520 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
ee3cc2e2 | 6521 | |
6feb9908 AM |
6522 | /* If no runtime region or VMA has been specified, but the load region |
6523 | has been specified, then use the load region for the runtime region | |
6524 | as well. */ | |
6bdafbeb NC |
6525 | if (lma_memspec != NULL |
6526 | && ! have_vma | |
6527 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) | |
ee3cc2e2 RS |
6528 | *region = *lma_region; |
6529 | else | |
a747ee4d | 6530 | *region = lang_memory_region_lookup (memspec, FALSE); |
ee3cc2e2 | 6531 | |
6bdafbeb | 6532 | if (have_lma && lma_memspec != 0) |
ee3cc2e2 RS |
6533 | einfo (_("%X%P:%S: section has both a load address and a load region\n")); |
6534 | } | |
6535 | ||
252b5132 | 6536 | void |
6bdafbeb NC |
6537 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
6538 | lang_output_section_phdr_list *phdrs, | |
6539 | const char *lma_memspec) | |
252b5132 | 6540 | { |
ee3cc2e2 RS |
6541 | lang_get_regions (¤t_section->region, |
6542 | ¤t_section->lma_region, | |
6543 | memspec, lma_memspec, | |
6bdafbeb NC |
6544 | current_section->load_base != NULL, |
6545 | current_section->addr_tree != NULL); | |
252b5132 | 6546 | current_section->fill = fill; |
252b5132 | 6547 | current_section->phdrs = phdrs; |
bde18da4 | 6548 | pop_stat_ptr (); |
252b5132 RH |
6549 | } |
6550 | ||
08da4cac KH |
6551 | /* Create an absolute symbol with the given name with the value of the |
6552 | address of first byte of the section named. | |
6553 | ||
6554 | If the symbol already exists, then do nothing. */ | |
252b5132 | 6555 | |
252b5132 | 6556 | void |
1579bae1 | 6557 | lang_abs_symbol_at_beginning_of (const char *secname, const char *name) |
252b5132 RH |
6558 | { |
6559 | struct bfd_link_hash_entry *h; | |
6560 | ||
b34976b6 | 6561 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 6562 | if (h == NULL) |
252b5132 RH |
6563 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
6564 | ||
6565 | if (h->type == bfd_link_hash_new | |
6566 | || h->type == bfd_link_hash_undefined) | |
6567 | { | |
6568 | asection *sec; | |
6569 | ||
6570 | h->type = bfd_link_hash_defined; | |
6571 | ||
f13a99db | 6572 | sec = bfd_get_section_by_name (link_info.output_bfd, secname); |
1579bae1 | 6573 | if (sec == NULL) |
252b5132 RH |
6574 | h->u.def.value = 0; |
6575 | else | |
f13a99db | 6576 | h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec); |
252b5132 RH |
6577 | |
6578 | h->u.def.section = bfd_abs_section_ptr; | |
6579 | } | |
6580 | } | |
6581 | ||
08da4cac KH |
6582 | /* Create an absolute symbol with the given name with the value of the |
6583 | address of the first byte after the end of the section named. | |
6584 | ||
6585 | If the symbol already exists, then do nothing. */ | |
252b5132 | 6586 | |
252b5132 | 6587 | void |
1579bae1 | 6588 | lang_abs_symbol_at_end_of (const char *secname, const char *name) |
252b5132 RH |
6589 | { |
6590 | struct bfd_link_hash_entry *h; | |
6591 | ||
b34976b6 | 6592 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 6593 | if (h == NULL) |
252b5132 RH |
6594 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
6595 | ||
6596 | if (h->type == bfd_link_hash_new | |
6597 | || h->type == bfd_link_hash_undefined) | |
6598 | { | |
6599 | asection *sec; | |
6600 | ||
6601 | h->type = bfd_link_hash_defined; | |
6602 | ||
f13a99db | 6603 | sec = bfd_get_section_by_name (link_info.output_bfd, secname); |
1579bae1 | 6604 | if (sec == NULL) |
252b5132 RH |
6605 | h->u.def.value = 0; |
6606 | else | |
f13a99db | 6607 | h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec) |
eea6121a | 6608 | + TO_ADDR (sec->size)); |
252b5132 RH |
6609 | |
6610 | h->u.def.section = bfd_abs_section_ptr; | |
6611 | } | |
6612 | } | |
6613 | ||
6614 | void | |
1579bae1 AM |
6615 | lang_statement_append (lang_statement_list_type *list, |
6616 | lang_statement_union_type *element, | |
6617 | lang_statement_union_type **field) | |
252b5132 RH |
6618 | { |
6619 | *(list->tail) = element; | |
6620 | list->tail = field; | |
6621 | } | |
6622 | ||
6623 | /* Set the output format type. -oformat overrides scripts. */ | |
6624 | ||
6625 | void | |
1579bae1 AM |
6626 | lang_add_output_format (const char *format, |
6627 | const char *big, | |
6628 | const char *little, | |
6629 | int from_script) | |
252b5132 RH |
6630 | { |
6631 | if (output_target == NULL || !from_script) | |
6632 | { | |
6633 | if (command_line.endian == ENDIAN_BIG | |
6634 | && big != NULL) | |
6635 | format = big; | |
6636 | else if (command_line.endian == ENDIAN_LITTLE | |
6637 | && little != NULL) | |
6638 | format = little; | |
6639 | ||
6640 | output_target = format; | |
6641 | } | |
6642 | } | |
6643 | ||
53d25da6 AM |
6644 | void |
6645 | lang_add_insert (const char *where, int is_before) | |
6646 | { | |
6647 | lang_insert_statement_type *new; | |
6648 | ||
6649 | new = new_stat (lang_insert_statement, stat_ptr); | |
6650 | new->where = where; | |
6651 | new->is_before = is_before; | |
6652 | saved_script_handle = previous_script_handle; | |
6653 | } | |
6654 | ||
252b5132 RH |
6655 | /* Enter a group. This creates a new lang_group_statement, and sets |
6656 | stat_ptr to build new statements within the group. */ | |
6657 | ||
6658 | void | |
1579bae1 | 6659 | lang_enter_group (void) |
252b5132 RH |
6660 | { |
6661 | lang_group_statement_type *g; | |
6662 | ||
6663 | g = new_stat (lang_group_statement, stat_ptr); | |
6664 | lang_list_init (&g->children); | |
bde18da4 | 6665 | push_stat_ptr (&g->children); |
252b5132 RH |
6666 | } |
6667 | ||
6668 | /* Leave a group. This just resets stat_ptr to start writing to the | |
6669 | regular list of statements again. Note that this will not work if | |
6670 | groups can occur inside anything else which can adjust stat_ptr, | |
6671 | but currently they can't. */ | |
6672 | ||
6673 | void | |
1579bae1 | 6674 | lang_leave_group (void) |
252b5132 | 6675 | { |
bde18da4 | 6676 | pop_stat_ptr (); |
252b5132 RH |
6677 | } |
6678 | ||
6679 | /* Add a new program header. This is called for each entry in a PHDRS | |
6680 | command in a linker script. */ | |
6681 | ||
6682 | void | |
1579bae1 AM |
6683 | lang_new_phdr (const char *name, |
6684 | etree_type *type, | |
6685 | bfd_boolean filehdr, | |
6686 | bfd_boolean phdrs, | |
6687 | etree_type *at, | |
6688 | etree_type *flags) | |
252b5132 RH |
6689 | { |
6690 | struct lang_phdr *n, **pp; | |
6691 | ||
1579bae1 | 6692 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
6693 | n->next = NULL; |
6694 | n->name = name; | |
e9ee469a | 6695 | n->type = exp_get_value_int (type, 0, "program header type"); |
252b5132 RH |
6696 | n->filehdr = filehdr; |
6697 | n->phdrs = phdrs; | |
6698 | n->at = at; | |
6699 | n->flags = flags; | |
6700 | ||
6701 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
6702 | ; | |
6703 | *pp = n; | |
6704 | } | |
6705 | ||
6706 | /* Record the program header information in the output BFD. FIXME: We | |
6707 | should not be calling an ELF specific function here. */ | |
6708 | ||
6709 | static void | |
1579bae1 | 6710 | lang_record_phdrs (void) |
252b5132 RH |
6711 | { |
6712 | unsigned int alc; | |
6713 | asection **secs; | |
6bdafbeb | 6714 | lang_output_section_phdr_list *last; |
252b5132 | 6715 | struct lang_phdr *l; |
afd7a018 | 6716 | lang_output_section_statement_type *os; |
252b5132 RH |
6717 | |
6718 | alc = 10; | |
1579bae1 | 6719 | secs = xmalloc (alc * sizeof (asection *)); |
252b5132 | 6720 | last = NULL; |
591a748a | 6721 | |
252b5132 RH |
6722 | for (l = lang_phdr_list; l != NULL; l = l->next) |
6723 | { | |
6724 | unsigned int c; | |
6725 | flagword flags; | |
6726 | bfd_vma at; | |
6727 | ||
6728 | c = 0; | |
afd7a018 AM |
6729 | for (os = &lang_output_section_statement.head->output_section_statement; |
6730 | os != NULL; | |
6731 | os = os->next) | |
252b5132 | 6732 | { |
6bdafbeb | 6733 | lang_output_section_phdr_list *pl; |
252b5132 | 6734 | |
66c103b7 | 6735 | if (os->constraint < 0) |
0841712e | 6736 | continue; |
252b5132 RH |
6737 | |
6738 | pl = os->phdrs; | |
6739 | if (pl != NULL) | |
6740 | last = pl; | |
6741 | else | |
6742 | { | |
6743 | if (os->sectype == noload_section | |
6744 | || os->bfd_section == NULL | |
6745 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
6746 | continue; | |
591a748a | 6747 | |
7512c397 AM |
6748 | /* Don't add orphans to PT_INTERP header. */ |
6749 | if (l->type == 3) | |
6750 | continue; | |
6751 | ||
e9442572 | 6752 | if (last == NULL) |
591a748a NC |
6753 | { |
6754 | lang_output_section_statement_type * tmp_os; | |
6755 | ||
6756 | /* If we have not run across a section with a program | |
6757 | header assigned to it yet, then scan forwards to find | |
6758 | one. This prevents inconsistencies in the linker's | |
6759 | behaviour when a script has specified just a single | |
6760 | header and there are sections in that script which are | |
6761 | not assigned to it, and which occur before the first | |
6762 | use of that header. See here for more details: | |
6763 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
6764 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
6765 | if (tmp_os->phdrs) | |
e9442572 AM |
6766 | { |
6767 | last = tmp_os->phdrs; | |
6768 | break; | |
6769 | } | |
6770 | if (last == NULL) | |
6771 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 6772 | } |
e9442572 | 6773 | pl = last; |
252b5132 RH |
6774 | } |
6775 | ||
6776 | if (os->bfd_section == NULL) | |
6777 | continue; | |
6778 | ||
6779 | for (; pl != NULL; pl = pl->next) | |
6780 | { | |
6781 | if (strcmp (pl->name, l->name) == 0) | |
6782 | { | |
6783 | if (c >= alc) | |
6784 | { | |
6785 | alc *= 2; | |
1579bae1 | 6786 | secs = xrealloc (secs, alc * sizeof (asection *)); |
252b5132 RH |
6787 | } |
6788 | secs[c] = os->bfd_section; | |
6789 | ++c; | |
b34976b6 | 6790 | pl->used = TRUE; |
252b5132 RH |
6791 | } |
6792 | } | |
6793 | } | |
6794 | ||
6795 | if (l->flags == NULL) | |
6796 | flags = 0; | |
6797 | else | |
e9ee469a | 6798 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
6799 | |
6800 | if (l->at == NULL) | |
6801 | at = 0; | |
6802 | else | |
e9ee469a | 6803 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 6804 | |
f13a99db | 6805 | if (! bfd_record_phdr (link_info.output_bfd, l->type, |
d1778b88 | 6806 | l->flags != NULL, flags, l->at != NULL, |
252b5132 RH |
6807 | at, l->filehdr, l->phdrs, c, secs)) |
6808 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); | |
6809 | } | |
6810 | ||
6811 | free (secs); | |
6812 | ||
6813 | /* Make sure all the phdr assignments succeeded. */ | |
afd7a018 AM |
6814 | for (os = &lang_output_section_statement.head->output_section_statement; |
6815 | os != NULL; | |
6816 | os = os->next) | |
252b5132 | 6817 | { |
6bdafbeb | 6818 | lang_output_section_phdr_list *pl; |
252b5132 | 6819 | |
66c103b7 | 6820 | if (os->constraint < 0 |
afd7a018 | 6821 | || os->bfd_section == NULL) |
252b5132 RH |
6822 | continue; |
6823 | ||
afd7a018 | 6824 | for (pl = os->phdrs; |
252b5132 RH |
6825 | pl != NULL; |
6826 | pl = pl->next) | |
6827 | if (! pl->used && strcmp (pl->name, "NONE") != 0) | |
6828 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), | |
afd7a018 | 6829 | os->name, pl->name); |
252b5132 RH |
6830 | } |
6831 | } | |
6832 | ||
6833 | /* Record a list of sections which may not be cross referenced. */ | |
6834 | ||
6835 | void | |
6bdafbeb | 6836 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
6837 | { |
6838 | struct lang_nocrossrefs *n; | |
6839 | ||
1579bae1 | 6840 | n = xmalloc (sizeof *n); |
252b5132 RH |
6841 | n->next = nocrossref_list; |
6842 | n->list = l; | |
6843 | nocrossref_list = n; | |
6844 | ||
6845 | /* Set notice_all so that we get informed about all symbols. */ | |
b34976b6 | 6846 | link_info.notice_all = TRUE; |
252b5132 RH |
6847 | } |
6848 | \f | |
6849 | /* Overlay handling. We handle overlays with some static variables. */ | |
6850 | ||
6851 | /* The overlay virtual address. */ | |
6852 | static etree_type *overlay_vma; | |
7e7d5768 AM |
6853 | /* And subsection alignment. */ |
6854 | static etree_type *overlay_subalign; | |
252b5132 | 6855 | |
252b5132 RH |
6856 | /* An expression for the maximum section size seen so far. */ |
6857 | static etree_type *overlay_max; | |
6858 | ||
6859 | /* A list of all the sections in this overlay. */ | |
6860 | ||
89cdebba | 6861 | struct overlay_list { |
252b5132 RH |
6862 | struct overlay_list *next; |
6863 | lang_output_section_statement_type *os; | |
6864 | }; | |
6865 | ||
6866 | static struct overlay_list *overlay_list; | |
6867 | ||
6868 | /* Start handling an overlay. */ | |
6869 | ||
6870 | void | |
7e7d5768 | 6871 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
6872 | { |
6873 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
6874 | ASSERT (overlay_vma == NULL |
6875 | && overlay_subalign == NULL | |
6876 | && overlay_max == NULL); | |
252b5132 RH |
6877 | |
6878 | overlay_vma = vma_expr; | |
7e7d5768 | 6879 | overlay_subalign = subalign; |
252b5132 RH |
6880 | } |
6881 | ||
6882 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
6883 | lang_enter_output_section_statement with the correct VMA. |
6884 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
6885 | |
6886 | void | |
1579bae1 | 6887 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
6888 | { |
6889 | struct overlay_list *n; | |
6890 | etree_type *size; | |
6891 | ||
152d792f | 6892 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
0841712e | 6893 | 0, overlay_subalign, 0, 0); |
252b5132 | 6894 | |
9f88b410 | 6895 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
6896 | sections on this one. This will work correctly even if `.' is |
6897 | used in the addresses. */ | |
6898 | if (overlay_list == NULL) | |
9f88b410 | 6899 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
6900 | |
6901 | /* Remember the section. */ | |
1579bae1 | 6902 | n = xmalloc (sizeof *n); |
252b5132 RH |
6903 | n->os = current_section; |
6904 | n->next = overlay_list; | |
6905 | overlay_list = n; | |
6906 | ||
6907 | size = exp_nameop (SIZEOF, name); | |
6908 | ||
252b5132 RH |
6909 | /* Arrange to work out the maximum section end address. */ |
6910 | if (overlay_max == NULL) | |
6911 | overlay_max = size; | |
6912 | else | |
6913 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
6914 | } | |
6915 | ||
6916 | /* Finish a section in an overlay. There isn't any special to do | |
6917 | here. */ | |
6918 | ||
6919 | void | |
1579bae1 | 6920 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 6921 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
6922 | { |
6923 | const char *name; | |
6924 | char *clean, *s2; | |
6925 | const char *s1; | |
6926 | char *buf; | |
6927 | ||
6928 | name = current_section->name; | |
6929 | ||
a747ee4d NC |
6930 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
6931 | region and that no load-time region has been specified. It doesn't | |
6932 | really matter what we say here, since lang_leave_overlay will | |
6933 | override it. */ | |
6934 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
6935 | |
6936 | /* Define the magic symbols. */ | |
6937 | ||
6938 | clean = xmalloc (strlen (name) + 1); | |
6939 | s2 = clean; | |
6940 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 6941 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
6942 | *s2++ = *s1; |
6943 | *s2 = '\0'; | |
6944 | ||
6945 | buf = xmalloc (strlen (clean) + sizeof "__load_start_"); | |
6946 | sprintf (buf, "__load_start_%s", clean); | |
34711ca3 AM |
6947 | lang_add_assignment (exp_provide (buf, |
6948 | exp_nameop (LOADADDR, name), | |
6949 | FALSE)); | |
252b5132 RH |
6950 | |
6951 | buf = xmalloc (strlen (clean) + sizeof "__load_stop_"); | |
6952 | sprintf (buf, "__load_stop_%s", clean); | |
34711ca3 AM |
6953 | lang_add_assignment (exp_provide (buf, |
6954 | exp_binop ('+', | |
6955 | exp_nameop (LOADADDR, name), | |
6956 | exp_nameop (SIZEOF, name)), | |
6957 | FALSE)); | |
252b5132 RH |
6958 | |
6959 | free (clean); | |
6960 | } | |
6961 | ||
6962 | /* Finish an overlay. If there are any overlay wide settings, this | |
6963 | looks through all the sections in the overlay and sets them. */ | |
6964 | ||
6965 | void | |
1579bae1 AM |
6966 | lang_leave_overlay (etree_type *lma_expr, |
6967 | int nocrossrefs, | |
6968 | fill_type *fill, | |
6969 | const char *memspec, | |
6bdafbeb | 6970 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 6971 | const char *lma_memspec) |
252b5132 RH |
6972 | { |
6973 | lang_memory_region_type *region; | |
562d3460 | 6974 | lang_memory_region_type *lma_region; |
252b5132 | 6975 | struct overlay_list *l; |
6bdafbeb | 6976 | lang_nocrossref_type *nocrossref; |
252b5132 | 6977 | |
ee3cc2e2 RS |
6978 | lang_get_regions (®ion, &lma_region, |
6979 | memspec, lma_memspec, | |
6bdafbeb | 6980 | lma_expr != NULL, FALSE); |
562d3460 | 6981 | |
252b5132 RH |
6982 | nocrossref = NULL; |
6983 | ||
9f88b410 RS |
6984 | /* After setting the size of the last section, set '.' to end of the |
6985 | overlay region. */ | |
6986 | if (overlay_list != NULL) | |
6987 | overlay_list->os->update_dot_tree | |
6988 | = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max)); | |
6989 | ||
252b5132 RH |
6990 | l = overlay_list; |
6991 | while (l != NULL) | |
6992 | { | |
6993 | struct overlay_list *next; | |
6994 | ||
1579bae1 | 6995 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 6996 | l->os->fill = fill; |
1545243b | 6997 | |
9f88b410 RS |
6998 | l->os->region = region; |
6999 | l->os->lma_region = lma_region; | |
7000 | ||
7001 | /* The first section has the load address specified in the | |
7002 | OVERLAY statement. The rest are worked out from that. | |
7003 | The base address is not needed (and should be null) if | |
7004 | an LMA region was specified. */ | |
7005 | if (l->next == 0) | |
152d792f AM |
7006 | { |
7007 | l->os->load_base = lma_expr; | |
7008 | l->os->sectype = normal_section; | |
7009 | } | |
252b5132 RH |
7010 | if (phdrs != NULL && l->os->phdrs == NULL) |
7011 | l->os->phdrs = phdrs; | |
7012 | ||
9f88b410 | 7013 | if (nocrossrefs) |
252b5132 | 7014 | { |
6bdafbeb | 7015 | lang_nocrossref_type *nc; |
252b5132 | 7016 | |
1579bae1 | 7017 | nc = xmalloc (sizeof *nc); |
252b5132 RH |
7018 | nc->name = l->os->name; |
7019 | nc->next = nocrossref; | |
7020 | nocrossref = nc; | |
7021 | } | |
7022 | ||
7023 | next = l->next; | |
7024 | free (l); | |
7025 | l = next; | |
7026 | } | |
7027 | ||
7028 | if (nocrossref != NULL) | |
7029 | lang_add_nocrossref (nocrossref); | |
7030 | ||
252b5132 | 7031 | overlay_vma = NULL; |
252b5132 RH |
7032 | overlay_list = NULL; |
7033 | overlay_max = NULL; | |
7034 | } | |
7035 | \f | |
7036 | /* Version handling. This is only useful for ELF. */ | |
7037 | ||
7038 | /* This global variable holds the version tree that we build. */ | |
7039 | ||
7040 | struct bfd_elf_version_tree *lang_elf_version_info; | |
7041 | ||
108ba305 JJ |
7042 | /* If PREV is NULL, return first version pattern matching particular symbol. |
7043 | If PREV is non-NULL, return first version pattern matching particular | |
7044 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 7045 | |
108ba305 JJ |
7046 | static struct bfd_elf_version_expr * |
7047 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
7048 | struct bfd_elf_version_expr *prev, | |
7049 | const char *sym) | |
252b5132 | 7050 | { |
108ba305 JJ |
7051 | const char *cxx_sym = sym; |
7052 | const char *java_sym = sym; | |
7053 | struct bfd_elf_version_expr *expr = NULL; | |
252b5132 | 7054 | |
108ba305 | 7055 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 7056 | { |
2f97444a | 7057 | cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI); |
108ba305 JJ |
7058 | if (!cxx_sym) |
7059 | cxx_sym = sym; | |
252b5132 | 7060 | } |
df816215 | 7061 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 7062 | { |
108ba305 JJ |
7063 | java_sym = cplus_demangle (sym, DMGL_JAVA); |
7064 | if (!java_sym) | |
7065 | java_sym = sym; | |
252b5132 RH |
7066 | } |
7067 | ||
ae5a3597 | 7068 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 7069 | { |
108ba305 JJ |
7070 | struct bfd_elf_version_expr e; |
7071 | ||
7072 | switch (prev ? prev->mask : 0) | |
7073 | { | |
967928e9 AM |
7074 | case 0: |
7075 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
7076 | { | |
ae5a3597 | 7077 | e.pattern = sym; |
967928e9 | 7078 | expr = htab_find (head->htab, &e); |
ae5a3597 | 7079 | while (expr && strcmp (expr->pattern, sym) == 0) |
967928e9 AM |
7080 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
7081 | goto out_ret; | |
7082 | else | |
7083 | expr = expr->next; | |
7084 | } | |
7085 | /* Fallthrough */ | |
7086 | case BFD_ELF_VERSION_C_TYPE: | |
7087 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
7088 | { | |
ae5a3597 | 7089 | e.pattern = cxx_sym; |
967928e9 | 7090 | expr = htab_find (head->htab, &e); |
ae5a3597 | 7091 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
7092 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
7093 | goto out_ret; | |
7094 | else | |
7095 | expr = expr->next; | |
7096 | } | |
7097 | /* Fallthrough */ | |
7098 | case BFD_ELF_VERSION_CXX_TYPE: | |
7099 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
7100 | { | |
ae5a3597 | 7101 | e.pattern = java_sym; |
967928e9 | 7102 | expr = htab_find (head->htab, &e); |
ae5a3597 | 7103 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
7104 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
7105 | goto out_ret; | |
7106 | else | |
7107 | expr = expr->next; | |
7108 | } | |
7109 | /* Fallthrough */ | |
7110 | default: | |
7111 | break; | |
108ba305 | 7112 | } |
252b5132 | 7113 | } |
108ba305 JJ |
7114 | |
7115 | /* Finally, try the wildcards. */ | |
ae5a3597 | 7116 | if (prev == NULL || prev->literal) |
108ba305 | 7117 | expr = head->remaining; |
252b5132 | 7118 | else |
108ba305 | 7119 | expr = prev->next; |
86043bbb | 7120 | for (; expr; expr = expr->next) |
252b5132 | 7121 | { |
108ba305 JJ |
7122 | const char *s; |
7123 | ||
86043bbb MM |
7124 | if (!expr->pattern) |
7125 | continue; | |
7126 | ||
108ba305 JJ |
7127 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
7128 | break; | |
7129 | ||
7130 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
7131 | s = java_sym; | |
7132 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
7133 | s = cxx_sym; | |
7134 | else | |
7135 | s = sym; | |
5e35cbc2 | 7136 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 7137 | break; |
252b5132 RH |
7138 | } |
7139 | ||
967928e9 | 7140 | out_ret: |
108ba305 JJ |
7141 | if (cxx_sym != sym) |
7142 | free ((char *) cxx_sym); | |
7143 | if (java_sym != sym) | |
7144 | free ((char *) java_sym); | |
7145 | return expr; | |
252b5132 RH |
7146 | } |
7147 | ||
5e35cbc2 | 7148 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 7149 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
7150 | |
7151 | static const char * | |
7152 | realsymbol (const char *pattern) | |
7153 | { | |
7154 | const char *p; | |
7155 | bfd_boolean changed = FALSE, backslash = FALSE; | |
7156 | char *s, *symbol = xmalloc (strlen (pattern) + 1); | |
7157 | ||
7158 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
7159 | { | |
7160 | /* It is a glob pattern only if there is no preceding | |
7161 | backslash. */ | |
5e35cbc2 L |
7162 | if (backslash) |
7163 | { | |
7164 | /* Remove the preceding backslash. */ | |
7165 | *(s - 1) = *p; | |
ae5a3597 | 7166 | backslash = FALSE; |
5e35cbc2 L |
7167 | changed = TRUE; |
7168 | } | |
7169 | else | |
ae5a3597 AM |
7170 | { |
7171 | if (*p == '?' || *p == '*' || *p == '[') | |
7172 | { | |
7173 | free (symbol); | |
7174 | return NULL; | |
7175 | } | |
5e35cbc2 | 7176 | |
ae5a3597 AM |
7177 | *s++ = *p; |
7178 | backslash = *p == '\\'; | |
7179 | } | |
5e35cbc2 L |
7180 | } |
7181 | ||
7182 | if (changed) | |
7183 | { | |
7184 | *s = '\0'; | |
7185 | return symbol; | |
7186 | } | |
7187 | else | |
7188 | { | |
7189 | free (symbol); | |
7190 | return pattern; | |
7191 | } | |
7192 | } | |
7193 | ||
86043bbb MM |
7194 | /* This is called for each variable name or match expression. NEW is |
7195 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob | |
7196 | pattern to be matched against symbol names. */ | |
252b5132 RH |
7197 | |
7198 | struct bfd_elf_version_expr * | |
1579bae1 AM |
7199 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
7200 | const char *new, | |
86043bbb MM |
7201 | const char *lang, |
7202 | bfd_boolean literal_p) | |
252b5132 RH |
7203 | { |
7204 | struct bfd_elf_version_expr *ret; | |
7205 | ||
1579bae1 | 7206 | ret = xmalloc (sizeof *ret); |
252b5132 | 7207 | ret->next = orig; |
31941635 L |
7208 | ret->symver = 0; |
7209 | ret->script = 0; | |
ae5a3597 AM |
7210 | ret->literal = TRUE; |
7211 | ret->pattern = literal_p ? new : realsymbol (new); | |
7212 | if (ret->pattern == NULL) | |
7213 | { | |
7214 | ret->pattern = new; | |
7215 | ret->literal = FALSE; | |
7216 | } | |
252b5132 RH |
7217 | |
7218 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 7219 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 7220 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 7221 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 7222 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 7223 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
7224 | else |
7225 | { | |
7226 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
7227 | lang); | |
108ba305 | 7228 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
7229 | } |
7230 | ||
fac1652d | 7231 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
7232 | } |
7233 | ||
7234 | /* This is called for each set of variable names and match | |
7235 | expressions. */ | |
7236 | ||
7237 | struct bfd_elf_version_tree * | |
1579bae1 AM |
7238 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
7239 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
7240 | { |
7241 | struct bfd_elf_version_tree *ret; | |
7242 | ||
108ba305 JJ |
7243 | ret = xcalloc (1, sizeof *ret); |
7244 | ret->globals.list = globals; | |
7245 | ret->locals.list = locals; | |
7246 | ret->match = lang_vers_match; | |
252b5132 | 7247 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
7248 | return ret; |
7249 | } | |
7250 | ||
7251 | /* This static variable keeps track of version indices. */ | |
7252 | ||
7253 | static int version_index; | |
7254 | ||
108ba305 JJ |
7255 | static hashval_t |
7256 | version_expr_head_hash (const void *p) | |
7257 | { | |
7258 | const struct bfd_elf_version_expr *e = p; | |
7259 | ||
ae5a3597 | 7260 | return htab_hash_string (e->pattern); |
108ba305 JJ |
7261 | } |
7262 | ||
7263 | static int | |
7264 | version_expr_head_eq (const void *p1, const void *p2) | |
7265 | { | |
7266 | const struct bfd_elf_version_expr *e1 = p1; | |
7267 | const struct bfd_elf_version_expr *e2 = p2; | |
7268 | ||
ae5a3597 | 7269 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
7270 | } |
7271 | ||
7272 | static void | |
7273 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
7274 | { | |
7275 | size_t count = 0; | |
7276 | struct bfd_elf_version_expr *e, *next; | |
7277 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
7278 | ||
7279 | for (e = head->list; e; e = e->next) | |
7280 | { | |
ae5a3597 | 7281 | if (e->literal) |
108ba305 JJ |
7282 | count++; |
7283 | head->mask |= e->mask; | |
7284 | } | |
7285 | ||
7286 | if (count) | |
7287 | { | |
7288 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
7289 | version_expr_head_eq, NULL); | |
7290 | list_loc = &head->list; | |
7291 | remaining_loc = &head->remaining; | |
7292 | for (e = head->list; e; e = next) | |
7293 | { | |
7294 | next = e->next; | |
ae5a3597 | 7295 | if (!e->literal) |
108ba305 JJ |
7296 | { |
7297 | *remaining_loc = e; | |
7298 | remaining_loc = &e->next; | |
7299 | } | |
7300 | else | |
7301 | { | |
7302 | void **loc = htab_find_slot (head->htab, e, INSERT); | |
7303 | ||
7304 | if (*loc) | |
7305 | { | |
7306 | struct bfd_elf_version_expr *e1, *last; | |
7307 | ||
7308 | e1 = *loc; | |
7309 | last = NULL; | |
7310 | do | |
7311 | { | |
7312 | if (e1->mask == e->mask) | |
7313 | { | |
7314 | last = NULL; | |
7315 | break; | |
7316 | } | |
7317 | last = e1; | |
7318 | e1 = e1->next; | |
7319 | } | |
ae5a3597 | 7320 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
7321 | |
7322 | if (last == NULL) | |
7323 | { | |
7324 | /* This is a duplicate. */ | |
7325 | /* FIXME: Memory leak. Sometimes pattern is not | |
7326 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 7327 | /* free (e->pattern); */ |
108ba305 JJ |
7328 | free (e); |
7329 | } | |
7330 | else | |
7331 | { | |
7332 | e->next = last->next; | |
7333 | last->next = e; | |
7334 | } | |
7335 | } | |
7336 | else | |
7337 | { | |
7338 | *loc = e; | |
7339 | *list_loc = e; | |
7340 | list_loc = &e->next; | |
7341 | } | |
7342 | } | |
7343 | } | |
7344 | *remaining_loc = NULL; | |
7345 | *list_loc = head->remaining; | |
7346 | } | |
7347 | else | |
7348 | head->remaining = head->list; | |
7349 | } | |
7350 | ||
252b5132 RH |
7351 | /* This is called when we know the name and dependencies of the |
7352 | version. */ | |
7353 | ||
7354 | void | |
1579bae1 AM |
7355 | lang_register_vers_node (const char *name, |
7356 | struct bfd_elf_version_tree *version, | |
7357 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
7358 | { |
7359 | struct bfd_elf_version_tree *t, **pp; | |
7360 | struct bfd_elf_version_expr *e1; | |
7361 | ||
6b9b879a JJ |
7362 | if (name == NULL) |
7363 | name = ""; | |
7364 | ||
7365 | if ((name[0] == '\0' && lang_elf_version_info != NULL) | |
7366 | || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0')) | |
7367 | { | |
6feb9908 AM |
7368 | einfo (_("%X%P: anonymous version tag cannot be combined" |
7369 | " with other version tags\n")); | |
5ed6aba4 | 7370 | free (version); |
6b9b879a JJ |
7371 | return; |
7372 | } | |
7373 | ||
252b5132 RH |
7374 | /* Make sure this node has a unique name. */ |
7375 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
7376 | if (strcmp (t->name, name) == 0) | |
7377 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
7378 | ||
108ba305 JJ |
7379 | lang_finalize_version_expr_head (&version->globals); |
7380 | lang_finalize_version_expr_head (&version->locals); | |
7381 | ||
252b5132 RH |
7382 | /* Check the global and local match names, and make sure there |
7383 | aren't any duplicates. */ | |
7384 | ||
108ba305 | 7385 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
7386 | { |
7387 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
7388 | { | |
7389 | struct bfd_elf_version_expr *e2; | |
7390 | ||
ae5a3597 | 7391 | if (t->locals.htab && e1->literal) |
108ba305 JJ |
7392 | { |
7393 | e2 = htab_find (t->locals.htab, e1); | |
ae5a3597 | 7394 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
7395 | { |
7396 | if (e1->mask == e2->mask) | |
6feb9908 | 7397 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 7398 | " in version information\n"), e1->pattern); |
108ba305 JJ |
7399 | e2 = e2->next; |
7400 | } | |
7401 | } | |
ae5a3597 | 7402 | else if (!e1->literal) |
108ba305 | 7403 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
7404 | if (strcmp (e1->pattern, e2->pattern) == 0 |
7405 | && e1->mask == e2->mask) | |
7406 | einfo (_("%X%P: duplicate expression `%s'" | |
7407 | " in version information\n"), e1->pattern); | |
252b5132 RH |
7408 | } |
7409 | } | |
7410 | ||
108ba305 | 7411 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
7412 | { |
7413 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
7414 | { | |
7415 | struct bfd_elf_version_expr *e2; | |
7416 | ||
ae5a3597 | 7417 | if (t->globals.htab && e1->literal) |
108ba305 JJ |
7418 | { |
7419 | e2 = htab_find (t->globals.htab, e1); | |
ae5a3597 | 7420 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
7421 | { |
7422 | if (e1->mask == e2->mask) | |
6feb9908 AM |
7423 | einfo (_("%X%P: duplicate expression `%s'" |
7424 | " in version information\n"), | |
ae5a3597 | 7425 | e1->pattern); |
108ba305 JJ |
7426 | e2 = e2->next; |
7427 | } | |
7428 | } | |
ae5a3597 | 7429 | else if (!e1->literal) |
108ba305 | 7430 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
7431 | if (strcmp (e1->pattern, e2->pattern) == 0 |
7432 | && e1->mask == e2->mask) | |
7433 | einfo (_("%X%P: duplicate expression `%s'" | |
7434 | " in version information\n"), e1->pattern); | |
252b5132 RH |
7435 | } |
7436 | } | |
7437 | ||
7438 | version->deps = deps; | |
7439 | version->name = name; | |
6b9b879a JJ |
7440 | if (name[0] != '\0') |
7441 | { | |
7442 | ++version_index; | |
7443 | version->vernum = version_index; | |
7444 | } | |
7445 | else | |
7446 | version->vernum = 0; | |
252b5132 RH |
7447 | |
7448 | for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next) | |
7449 | ; | |
7450 | *pp = version; | |
7451 | } | |
7452 | ||
7453 | /* This is called when we see a version dependency. */ | |
7454 | ||
7455 | struct bfd_elf_version_deps * | |
1579bae1 | 7456 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
7457 | { |
7458 | struct bfd_elf_version_deps *ret; | |
7459 | struct bfd_elf_version_tree *t; | |
7460 | ||
1579bae1 | 7461 | ret = xmalloc (sizeof *ret); |
252b5132 RH |
7462 | ret->next = list; |
7463 | ||
7464 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
7465 | { | |
7466 | if (strcmp (t->name, name) == 0) | |
7467 | { | |
7468 | ret->version_needed = t; | |
7469 | return ret; | |
7470 | } | |
7471 | } | |
7472 | ||
7473 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
7474 | ||
7475 | return ret; | |
7476 | } | |
7477 | ||
7478 | static void | |
1579bae1 | 7479 | lang_do_version_exports_section (void) |
252b5132 RH |
7480 | { |
7481 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
7482 | ||
7483 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
7484 | { | |
7485 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
7486 | char *contents, *p; | |
7487 | bfd_size_type len; | |
7488 | ||
7489 | if (sec == NULL) | |
b7a26f91 | 7490 | continue; |
252b5132 | 7491 | |
eea6121a | 7492 | len = sec->size; |
252b5132 RH |
7493 | contents = xmalloc (len); |
7494 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) | |
6f9efd97 | 7495 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
7496 | |
7497 | p = contents; | |
89cdebba | 7498 | while (p < contents + len) |
252b5132 | 7499 | { |
86043bbb | 7500 | greg = lang_new_vers_pattern (greg, p, NULL, FALSE); |
252b5132 RH |
7501 | p = strchr (p, '\0') + 1; |
7502 | } | |
7503 | ||
7504 | /* Do not free the contents, as we used them creating the regex. */ | |
7505 | ||
7506 | /* Do not include this section in the link. */ | |
a14a5de3 | 7507 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
7508 | } |
7509 | ||
86043bbb | 7510 | lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE); |
252b5132 RH |
7511 | lang_register_vers_node (command_line.version_exports_section, |
7512 | lang_new_vers_node (greg, lreg), NULL); | |
7513 | } | |
577a0623 AM |
7514 | |
7515 | void | |
1579bae1 | 7516 | lang_add_unique (const char *name) |
577a0623 AM |
7517 | { |
7518 | struct unique_sections *ent; | |
7519 | ||
7520 | for (ent = unique_section_list; ent; ent = ent->next) | |
7521 | if (strcmp (ent->name, name) == 0) | |
7522 | return; | |
7523 | ||
1579bae1 | 7524 | ent = xmalloc (sizeof *ent); |
577a0623 AM |
7525 | ent->name = xstrdup (name); |
7526 | ent->next = unique_section_list; | |
7527 | unique_section_list = ent; | |
7528 | } | |
55255dae L |
7529 | |
7530 | /* Append the list of dynamic symbols to the existing one. */ | |
7531 | ||
7532 | void | |
7533 | lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic) | |
7534 | { | |
40b36307 | 7535 | if (link_info.dynamic_list) |
55255dae | 7536 | { |
07806542 JJ |
7537 | struct bfd_elf_version_expr *tail; |
7538 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
7539 | ; | |
40b36307 L |
7540 | tail->next = link_info.dynamic_list->head.list; |
7541 | link_info.dynamic_list->head.list = dynamic; | |
55255dae L |
7542 | } |
7543 | else | |
7544 | { | |
7545 | struct bfd_elf_dynamic_list *d; | |
7546 | ||
7547 | d = xcalloc (1, sizeof *d); | |
7548 | d->head.list = dynamic; | |
7549 | d->match = lang_vers_match; | |
40b36307 | 7550 | link_info.dynamic_list = d; |
55255dae L |
7551 | } |
7552 | } | |
7553 | ||
7554 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
7555 | one. */ | |
7556 | ||
7557 | void | |
7558 | lang_append_dynamic_list_cpp_typeinfo (void) | |
7559 | { | |
7560 | const char * symbols [] = | |
7561 | { | |
7562 | "typeinfo name for*", | |
7563 | "typeinfo for*" | |
7564 | }; | |
7565 | struct bfd_elf_version_expr *dynamic = NULL; | |
7566 | unsigned int i; | |
7567 | ||
7568 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
7569 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
7570 | FALSE); | |
7571 | ||
7572 | lang_append_dynamic_list (dynamic); | |
7573 | } | |
40b36307 L |
7574 | |
7575 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
7576 | existing one. */ | |
7577 | ||
7578 | void | |
7579 | lang_append_dynamic_list_cpp_new (void) | |
7580 | { | |
7581 | const char * symbols [] = | |
7582 | { | |
7583 | "operator new*", | |
7584 | "operator delete*" | |
7585 | }; | |
7586 | struct bfd_elf_version_expr *dynamic = NULL; | |
7587 | unsigned int i; | |
7588 | ||
7589 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
7590 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
7591 | FALSE); | |
7592 | ||
7593 | lang_append_dynamic_list (dynamic); | |
7594 | } |