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