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