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