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