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