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" |
9a24a276 | 22 | #include <limits.h> |
3db64b00 | 23 | #include "bfd.h" |
252b5132 | 24 | #include "libiberty.h" |
42627821 | 25 | #include "filenames.h" |
3882b010 | 26 | #include "safe-ctype.h" |
252b5132 RH |
27 | #include "obstack.h" |
28 | #include "bfdlink.h" | |
1ff6de03 | 29 | #include "ctf-api.h" |
252b5132 RH |
30 | |
31 | #include "ld.h" | |
32 | #include "ldmain.h" | |
252b5132 RH |
33 | #include "ldexp.h" |
34 | #include "ldlang.h" | |
df2a7313 | 35 | #include <ldgram.h> |
252b5132 RH |
36 | #include "ldlex.h" |
37 | #include "ldmisc.h" | |
38 | #include "ldctor.h" | |
39 | #include "ldfile.h" | |
b71e2778 | 40 | #include "ldemul.h" |
252b5132 RH |
41 | #include "fnmatch.h" |
42 | #include "demangle.h" | |
108ba305 | 43 | #include "hashtab.h" |
0fef4b98 | 44 | #include "elf-bfd.h" |
5d3236ee DK |
45 | #ifdef ENABLE_PLUGINS |
46 | #include "plugin.h" | |
47 | #endif /* ENABLE_PLUGINS */ | |
252b5132 | 48 | |
7abb6dea | 49 | #ifndef offsetof |
cf888e70 | 50 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
51 | #endif |
52 | ||
fc90c280 AM |
53 | /* Convert between addresses in bytes and sizes in octets. |
54 | For currently supported targets, octets_per_byte is always a power | |
55 | of two, so we can use shifts. */ | |
56 | #define TO_ADDR(X) ((X) >> opb_shift) | |
57 | #define TO_SIZE(X) ((X) << opb_shift) | |
58 | ||
59 | /* Local variables. */ | |
252b5132 | 60 | static struct obstack stat_obstack; |
35835446 | 61 | static struct obstack map_obstack; |
252b5132 RH |
62 | |
63 | #define obstack_chunk_alloc xmalloc | |
64 | #define obstack_chunk_free free | |
8be573a7 | 65 | static const char *entry_symbol_default = "start"; |
18cd5bce | 66 | static bfd_boolean map_head_is_link_order = FALSE; |
252b5132 | 67 | static lang_output_section_statement_type *default_common_section; |
b34976b6 | 68 | static bfd_boolean map_option_f; |
252b5132 RH |
69 | static bfd_vma print_dot; |
70 | static lang_input_statement_type *first_file; | |
5f992e62 | 71 | static const char *current_target; |
e368bf56 TP |
72 | /* Header for list of statements corresponding to any files involved in the |
73 | link, either specified from the command-line or added implicitely (eg. | |
74 | archive member used to resolved undefined symbol, wildcard statement from | |
75 | linker script, etc.). Next pointer is in next field of a | |
76 | lang_statement_header_type (reached via header field in a | |
77 | lang_statement_union). */ | |
252b5132 | 78 | static lang_statement_list_type statement_list; |
bde18da4 AM |
79 | static lang_statement_list_type *stat_save[10]; |
80 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
c4b78195 | 81 | static struct unique_sections *unique_section_list; |
16e4ecc0 | 82 | static struct asneeded_minfo *asneeded_list_head; |
fc90c280 | 83 | static unsigned int opb_shift = 0; |
252b5132 | 84 | |
cf888e70 | 85 | /* Forward declarations. */ |
a431bc2e | 86 | static void exp_init_os (etree_type *); |
1579bae1 | 87 | static lang_input_statement_type *lookup_name (const char *); |
1579bae1 | 88 | static void insert_undefined (const char *); |
35835446 | 89 | static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *); |
1579bae1 AM |
90 | static void print_statement (lang_statement_union_type *, |
91 | lang_output_section_statement_type *); | |
92 | static void print_statement_list (lang_statement_union_type *, | |
93 | lang_output_section_statement_type *); | |
94 | static void print_statements (void); | |
d64703c6 | 95 | static void print_input_section (asection *, bfd_boolean); |
1579bae1 AM |
96 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); |
97 | static void lang_record_phdrs (void); | |
98 | static void lang_do_version_exports_section (void); | |
55255dae L |
99 | static void lang_finalize_version_expr_head |
100 | (struct bfd_elf_version_expr_head *); | |
cc9ad334 | 101 | static void lang_do_memory_regions (void); |
1579bae1 | 102 | |
cf888e70 | 103 | /* Exported variables. */ |
8be573a7 | 104 | const char *output_target; |
252b5132 | 105 | lang_output_section_statement_type *abs_output_section; |
5c1e6d53 | 106 | lang_statement_list_type lang_os_list; |
252b5132 | 107 | lang_statement_list_type *stat_ptr = &statement_list; |
e368bf56 TP |
108 | /* Header for list of statements corresponding to files used in the final |
109 | executable. This can be either object file specified on the command-line | |
110 | or library member resolving an undefined reference. Next pointer is in next | |
111 | field of a lang_input_statement_type (reached via input_statement field in a | |
112 | lang_statement_union). */ | |
87f2a346 | 113 | lang_statement_list_type file_chain = { NULL, NULL }; |
e368bf56 TP |
114 | /* Header for list of statements corresponding to files specified on the |
115 | command-line for linking. It thus contains real object files and archive | |
116 | but not archive members. Next pointer is in next_real_file field of a | |
117 | lang_input_statement_type statement (reached via input_statement field in a | |
118 | lang_statement_union). */ | |
dc27aea4 | 119 | lang_statement_list_type input_file_chain; |
e3e942e9 | 120 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 121 | const char *entry_section = ".text"; |
66be1055 | 122 | struct lang_input_statement_flags input_flags; |
b34976b6 | 123 | bfd_boolean entry_from_cmdline; |
24898b70 | 124 | bfd_boolean undef_from_cmdline; |
b34976b6 AM |
125 | bfd_boolean lang_has_input_file = FALSE; |
126 | bfd_boolean had_output_filename = FALSE; | |
127 | bfd_boolean lang_float_flag = FALSE; | |
128 | bfd_boolean delete_output_file_on_failure = FALSE; | |
f5ff60a6 | 129 | struct lang_phdr *lang_phdr_list; |
252b5132 | 130 | struct lang_nocrossrefs *nocrossref_list; |
16e4ecc0 | 131 | struct asneeded_minfo **asneeded_list_tail; |
1ff6de03 | 132 | static ctf_file_t *ctf_output; |
e9ee469a AM |
133 | |
134 | /* Functions that traverse the linker script and might evaluate | |
fa72205c | 135 | DEFINED() need to increment this at the start of the traversal. */ |
420e579c | 136 | int lang_statement_iteration = 0; |
252b5132 | 137 | |
61f5d054 L |
138 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
139 | Although backslashes are treated specially if a pattern contains | |
140 | wildcards, we do not consider the mere presence of a backslash to | |
141 | be enough to cause the pattern to be treated as a wildcard. | |
142 | That lets us handle DOS filenames more naturally. */ | |
143 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
144 | ||
d1778b88 | 145 | #define new_stat(x, y) \ |
1579bae1 | 146 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 147 | |
d1778b88 AM |
148 | #define outside_section_address(q) \ |
149 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 150 | |
d1778b88 AM |
151 | #define outside_symbol_address(q) \ |
152 | ((q)->value + outside_section_address (q->section)) | |
252b5132 RH |
153 | |
154 | #define SECTION_NAME_MAP_LENGTH (16) | |
155 | ||
1ff6de03 NA |
156 | /* CTF sections smaller than this are not compressed: compression of |
157 | dictionaries this small doesn't gain much, and this lets consumers mmap the | |
158 | sections directly out of the ELF file and use them with no decompression | |
159 | overhead if they want to. */ | |
160 | #define CTF_COMPRESSION_THRESHOLD 4096 | |
161 | ||
1579bae1 AM |
162 | void * |
163 | stat_alloc (size_t size) | |
252b5132 RH |
164 | { |
165 | return obstack_alloc (&stat_obstack, size); | |
166 | } | |
167 | ||
97407faf AM |
168 | static int |
169 | name_match (const char *pattern, const char *name) | |
170 | { | |
171 | if (wildcardp (pattern)) | |
172 | return fnmatch (pattern, name, 0); | |
173 | return strcmp (pattern, name); | |
174 | } | |
175 | ||
176 | /* If PATTERN is of the form archive:file, return a pointer to the | |
177 | separator. If not, return NULL. */ | |
178 | ||
179 | static char * | |
180 | archive_path (const char *pattern) | |
181 | { | |
182 | char *p = NULL; | |
183 | ||
184 | if (link_info.path_separator == 0) | |
185 | return p; | |
186 | ||
187 | p = strchr (pattern, link_info.path_separator); | |
188 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
189 | if (p == NULL || link_info.path_separator != ':') | |
190 | return p; | |
191 | ||
192 | /* Assume a match on the second char is part of drive specifier, | |
193 | as in "c:\silly.dos". */ | |
967928e9 | 194 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
195 | p = strchr (p + 1, link_info.path_separator); |
196 | #endif | |
197 | return p; | |
198 | } | |
199 | ||
967928e9 AM |
200 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
201 | return whether F matches FILE_SPEC. */ | |
202 | ||
203 | static bfd_boolean | |
204 | input_statement_is_archive_path (const char *file_spec, char *sep, | |
205 | lang_input_statement_type *f) | |
206 | { | |
207 | bfd_boolean match = FALSE; | |
208 | ||
209 | if ((*(sep + 1) == 0 | |
210 | || name_match (sep + 1, f->filename) == 0) | |
211 | && ((sep != file_spec) | |
212 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
213 | { | |
214 | match = TRUE; | |
215 | ||
216 | if (sep != file_spec) | |
217 | { | |
218 | const char *aname = f->the_bfd->my_archive->filename; | |
219 | *sep = 0; | |
220 | match = name_match (file_spec, aname) == 0; | |
221 | *sep = link_info.path_separator; | |
222 | } | |
223 | } | |
224 | return match; | |
225 | } | |
226 | ||
8a99a385 | 227 | static bfd_boolean |
d0bf826b AM |
228 | unique_section_p (const asection *sec, |
229 | const lang_output_section_statement_type *os) | |
577a0623 AM |
230 | { |
231 | struct unique_sections *unam; | |
d0d6a25b | 232 | const char *secnam; |
577a0623 | 233 | |
7bdf4127 | 234 | if (!link_info.resolve_section_groups |
d0d6a25b AM |
235 | && sec->owner != NULL |
236 | && bfd_is_group_section (sec->owner, sec)) | |
d0bf826b AM |
237 | return !(os != NULL |
238 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
d0d6a25b AM |
239 | |
240 | secnam = sec->name; | |
577a0623 | 241 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf AM |
242 | if (name_match (unam->name, secnam) == 0) |
243 | return TRUE; | |
577a0623 | 244 | |
b34976b6 | 245 | return FALSE; |
577a0623 AM |
246 | } |
247 | ||
08da4cac | 248 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 249 | |
8f1732fc AB |
250 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return |
251 | false. */ | |
72223188 | 252 | |
8f1732fc AB |
253 | static bfd_boolean |
254 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, | |
6c19b93b | 255 | lang_input_statement_type *file) |
4dec4d4e | 256 | { |
72223188 JJ |
257 | struct name_list *list_tmp; |
258 | ||
8f1732fc | 259 | for (list_tmp = exclude_list; |
72223188 JJ |
260 | list_tmp; |
261 | list_tmp = list_tmp->next) | |
262 | { | |
967928e9 | 263 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 264 | |
967928e9 | 265 | if (p != NULL) |
72223188 | 266 | { |
967928e9 | 267 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
8f1732fc | 268 | return TRUE; |
72223188 JJ |
269 | } |
270 | ||
967928e9 | 271 | else if (name_match (list_tmp->name, file->filename) == 0) |
8f1732fc | 272 | return TRUE; |
967928e9 AM |
273 | |
274 | /* FIXME: Perhaps remove the following at some stage? Matching | |
275 | unadorned archives like this was never documented and has | |
276 | been superceded by the archive:path syntax. */ | |
277 | else if (file->the_bfd != NULL | |
278 | && file->the_bfd->my_archive != NULL | |
279 | && name_match (list_tmp->name, | |
280 | file->the_bfd->my_archive->filename) == 0) | |
8f1732fc | 281 | return TRUE; |
72223188 JJ |
282 | } |
283 | ||
8f1732fc AB |
284 | return FALSE; |
285 | } | |
286 | ||
287 | /* Try processing a section against a wildcard. This just calls | |
288 | the callback unless the filename exclusion list is present | |
289 | and excludes the file. It's hardly ever present so this | |
290 | function is very fast. */ | |
291 | ||
292 | static void | |
293 | walk_wild_consider_section (lang_wild_statement_type *ptr, | |
294 | lang_input_statement_type *file, | |
295 | asection *s, | |
296 | struct wildcard_list *sec, | |
297 | callback_t callback, | |
298 | void *data) | |
299 | { | |
300 | /* Don't process sections from files which were excluded. */ | |
301 | if (walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, file)) | |
302 | return; | |
303 | ||
b9c361e0 | 304 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
72223188 JJ |
305 | } |
306 | ||
307 | /* Lowest common denominator routine that can handle everything correctly, | |
308 | but slowly. */ | |
309 | ||
310 | static void | |
311 | walk_wild_section_general (lang_wild_statement_type *ptr, | |
312 | lang_input_statement_type *file, | |
313 | callback_t callback, | |
314 | void *data) | |
315 | { | |
316 | asection *s; | |
317 | struct wildcard_list *sec; | |
b6bf44ba AM |
318 | |
319 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
4dec4d4e | 320 | { |
b6bf44ba | 321 | sec = ptr->section_list; |
2181f54f | 322 | if (sec == NULL) |
b9c361e0 | 323 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
2181f54f AM |
324 | |
325 | while (sec != NULL) | |
08da4cac | 326 | { |
b34976b6 | 327 | bfd_boolean skip = FALSE; |
2181f54f | 328 | |
72223188 | 329 | if (sec->spec.name != NULL) |
b6bf44ba | 330 | { |
fd361982 | 331 | const char *sname = bfd_section_name (s); |
2181f54f | 332 | |
97407faf | 333 | skip = name_match (sec->spec.name, sname) != 0; |
b6bf44ba | 334 | } |
4dec4d4e | 335 | |
b6bf44ba | 336 | if (!skip) |
72223188 | 337 | walk_wild_consider_section (ptr, file, s, sec, callback, data); |
4dec4d4e | 338 | |
2181f54f | 339 | sec = sec->next; |
4dec4d4e RH |
340 | } |
341 | } | |
342 | } | |
343 | ||
72223188 JJ |
344 | /* Routines to find a single section given its name. If there's more |
345 | than one section with that name, we report that. */ | |
346 | ||
347 | typedef struct | |
348 | { | |
349 | asection *found_section; | |
350 | bfd_boolean multiple_sections_found; | |
351 | } section_iterator_callback_data; | |
352 | ||
353 | static bfd_boolean | |
91d6fa6a | 354 | section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data) |
72223188 | 355 | { |
1e9cc1c2 | 356 | section_iterator_callback_data *d = (section_iterator_callback_data *) data; |
72223188 JJ |
357 | |
358 | if (d->found_section != NULL) | |
359 | { | |
360 | d->multiple_sections_found = TRUE; | |
361 | return TRUE; | |
362 | } | |
363 | ||
364 | d->found_section = s; | |
365 | return FALSE; | |
366 | } | |
367 | ||
368 | static asection * | |
369 | find_section (lang_input_statement_type *file, | |
370 | struct wildcard_list *sec, | |
371 | bfd_boolean *multiple_sections_found) | |
372 | { | |
373 | section_iterator_callback_data cb_data = { NULL, FALSE }; | |
374 | ||
329c1c86 | 375 | bfd_get_section_by_name_if (file->the_bfd, sec->spec.name, |
72223188 JJ |
376 | section_iterator_callback, &cb_data); |
377 | *multiple_sections_found = cb_data.multiple_sections_found; | |
378 | return cb_data.found_section; | |
379 | } | |
380 | ||
381 | /* Code for handling simple wildcards without going through fnmatch, | |
382 | which can be expensive because of charset translations etc. */ | |
383 | ||
384 | /* A simple wild is a literal string followed by a single '*', | |
385 | where the literal part is at least 4 characters long. */ | |
386 | ||
387 | static bfd_boolean | |
388 | is_simple_wild (const char *name) | |
389 | { | |
390 | size_t len = strcspn (name, "*?["); | |
391 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
392 | } | |
393 | ||
394 | static bfd_boolean | |
395 | match_simple_wild (const char *pattern, const char *name) | |
396 | { | |
397 | /* The first four characters of the pattern are guaranteed valid | |
398 | non-wildcard characters. So we can go faster. */ | |
399 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
400 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
401 | return FALSE; | |
402 | ||
403 | pattern += 4; | |
404 | name += 4; | |
405 | while (*pattern != '*') | |
406 | if (*name++ != *pattern++) | |
407 | return FALSE; | |
408 | ||
409 | return TRUE; | |
410 | } | |
411 | ||
02ecc8e9 L |
412 | /* Return the numerical value of the init_priority attribute from |
413 | section name NAME. */ | |
414 | ||
9a24a276 AM |
415 | static int |
416 | get_init_priority (const asection *sec) | |
02ecc8e9 | 417 | { |
9a24a276 AM |
418 | const char *name = bfd_section_name (sec); |
419 | const char *dot; | |
02ecc8e9 L |
420 | |
421 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 422 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
423 | lower value means a higher priority. |
424 | ||
9a24a276 | 425 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
426 | decimal numerical value of the init_priority attribute. |
427 | The order of execution in .init_array is forward and | |
428 | .fini_array is backward. | |
9a24a276 | 429 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
430 | decimal numerical value of the init_priority attribute. |
431 | The order of execution in .ctors is backward and .dtors | |
432 | is forward. | |
9a24a276 AM |
433 | |
434 | .init_array.NNNNN sections would normally be placed in an output | |
435 | .init_array section, .fini_array.NNNNN in .fini_array, | |
436 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
437 | we should sort by increasing number (and could just use | |
438 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
439 | being placed in .init_array (which may also contain | |
440 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
441 | placed in .fini_array then we need to extract the init_priority | |
442 | attribute and sort on that. */ | |
443 | dot = strrchr (name, '.'); | |
444 | if (dot != NULL && ISDIGIT (dot[1])) | |
02ecc8e9 | 445 | { |
9a24a276 AM |
446 | char *end; |
447 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
448 | if (*end == 0) | |
449 | { | |
450 | if (dot == name + 6 | |
451 | && (strncmp (name, ".ctors", 6) == 0 | |
452 | || strncmp (name, ".dtors", 6) == 0)) | |
453 | init_priority = 65535 - init_priority; | |
454 | if (init_priority <= INT_MAX) | |
455 | return init_priority; | |
456 | } | |
02ecc8e9 | 457 | } |
9a24a276 | 458 | return -1; |
02ecc8e9 L |
459 | } |
460 | ||
50c77e5d NC |
461 | /* Compare sections ASEC and BSEC according to SORT. */ |
462 | ||
463 | static int | |
464 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
465 | { | |
466 | int ret; | |
9a24a276 | 467 | int a_priority, b_priority; |
50c77e5d NC |
468 | |
469 | switch (sort) | |
470 | { | |
471 | default: | |
472 | abort (); | |
473 | ||
02ecc8e9 | 474 | case by_init_priority: |
9a24a276 AM |
475 | a_priority = get_init_priority (asec); |
476 | b_priority = get_init_priority (bsec); | |
477 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 478 | goto sort_by_name; |
9a24a276 | 479 | ret = a_priority - b_priority; |
02ecc8e9 L |
480 | if (ret) |
481 | break; | |
482 | else | |
483 | goto sort_by_name; | |
484 | ||
50c77e5d | 485 | case by_alignment_name: |
fd361982 | 486 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
487 | if (ret) |
488 | break; | |
489 | /* Fall through. */ | |
490 | ||
491 | case by_name: | |
02ecc8e9 | 492 | sort_by_name: |
fd361982 | 493 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
494 | break; |
495 | ||
496 | case by_name_alignment: | |
fd361982 | 497 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
498 | if (ret) |
499 | break; | |
500 | /* Fall through. */ | |
501 | ||
502 | case by_alignment: | |
fd361982 | 503 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
504 | break; |
505 | } | |
506 | ||
507 | return ret; | |
508 | } | |
509 | ||
329c1c86 | 510 | /* Build a Binary Search Tree to sort sections, unlike insertion sort |
50c77e5d NC |
511 | used in wild_sort(). BST is considerably faster if the number of |
512 | of sections are large. */ | |
513 | ||
514 | static lang_section_bst_type ** | |
515 | wild_sort_fast (lang_wild_statement_type *wild, | |
329c1c86 AM |
516 | struct wildcard_list *sec, |
517 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
518 | asection *section) | |
50c77e5d | 519 | { |
329c1c86 | 520 | lang_section_bst_type **tree; |
50c77e5d | 521 | |
e6f2cbf5 | 522 | tree = &wild->tree; |
50c77e5d | 523 | if (!wild->filenames_sorted |
329c1c86 | 524 | && (sec == NULL || sec->spec.sorted == none)) |
50c77e5d NC |
525 | { |
526 | /* Append at the right end of tree. */ | |
527 | while (*tree) | |
329c1c86 | 528 | tree = &((*tree)->right); |
50c77e5d NC |
529 | return tree; |
530 | } | |
531 | ||
329c1c86 | 532 | while (*tree) |
50c77e5d NC |
533 | { |
534 | /* Find the correct node to append this section. */ | |
329c1c86 AM |
535 | if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0) |
536 | tree = &((*tree)->left); | |
537 | else | |
538 | tree = &((*tree)->right); | |
50c77e5d NC |
539 | } |
540 | ||
541 | return tree; | |
542 | } | |
543 | ||
544 | /* Use wild_sort_fast to build a BST to sort sections. */ | |
545 | ||
546 | static void | |
547 | output_section_callback_fast (lang_wild_statement_type *ptr, | |
329c1c86 AM |
548 | struct wildcard_list *sec, |
549 | asection *section, | |
b9c361e0 | 550 | struct flag_info *sflag_list ATTRIBUTE_UNUSED, |
329c1c86 | 551 | lang_input_statement_type *file, |
d0bf826b | 552 | void *output) |
50c77e5d NC |
553 | { |
554 | lang_section_bst_type *node; | |
555 | lang_section_bst_type **tree; | |
d0bf826b | 556 | lang_output_section_statement_type *os; |
50c77e5d | 557 | |
d0bf826b AM |
558 | os = (lang_output_section_statement_type *) output; |
559 | ||
560 | if (unique_section_p (section, os)) | |
50c77e5d NC |
561 | return; |
562 | ||
1e9cc1c2 | 563 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
50c77e5d NC |
564 | node->left = 0; |
565 | node->right = 0; | |
566 | node->section = section; | |
567 | ||
568 | tree = wild_sort_fast (ptr, sec, file, section); | |
569 | if (tree != NULL) | |
570 | *tree = node; | |
571 | } | |
572 | ||
573 | /* Convert a sorted sections' BST back to list form. */ | |
574 | ||
575 | static void | |
329c1c86 AM |
576 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
577 | lang_section_bst_type *tree, | |
578 | void *output) | |
50c77e5d NC |
579 | { |
580 | if (tree->left) | |
581 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
582 | ||
b9c361e0 | 583 | lang_add_section (&ptr->children, tree->section, NULL, |
329c1c86 | 584 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
585 | |
586 | if (tree->right) | |
587 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
588 | ||
589 | free (tree); | |
590 | } | |
591 | ||
72223188 JJ |
592 | /* Specialized, optimized routines for handling different kinds of |
593 | wildcards */ | |
594 | ||
595 | static void | |
596 | walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr, | |
597 | lang_input_statement_type *file, | |
598 | callback_t callback, | |
599 | void *data) | |
600 | { | |
601 | /* We can just do a hash lookup for the section with the right name. | |
602 | But if that lookup discovers more than one section with the name | |
603 | (should be rare), we fall back to the general algorithm because | |
604 | we would otherwise have to sort the sections to make sure they | |
605 | get processed in the bfd's order. */ | |
606 | bfd_boolean multiple_sections_found; | |
607 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
608 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
609 | ||
610 | if (multiple_sections_found) | |
611 | walk_wild_section_general (ptr, file, callback, data); | |
612 | else if (s0) | |
613 | walk_wild_consider_section (ptr, file, s0, sec0, callback, data); | |
614 | } | |
615 | ||
616 | static void | |
617 | walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr, | |
618 | lang_input_statement_type *file, | |
619 | callback_t callback, | |
620 | void *data) | |
621 | { | |
622 | asection *s; | |
623 | struct wildcard_list *wildsec0 = ptr->handler_data[0]; | |
624 | ||
625 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
626 | { | |
fd361982 | 627 | const char *sname = bfd_section_name (s); |
72223188 JJ |
628 | bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname); |
629 | ||
630 | if (!skip) | |
631 | walk_wild_consider_section (ptr, file, s, wildsec0, callback, data); | |
632 | } | |
633 | } | |
634 | ||
635 | static void | |
636 | walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr, | |
637 | lang_input_statement_type *file, | |
638 | callback_t callback, | |
639 | void *data) | |
640 | { | |
641 | asection *s; | |
642 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
643 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
644 | bfd_boolean multiple_sections_found; | |
645 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
646 | ||
647 | if (multiple_sections_found) | |
648 | { | |
649 | walk_wild_section_general (ptr, file, callback, data); | |
650 | return; | |
651 | } | |
652 | ||
653 | /* Note that if the section was not found, s0 is NULL and | |
654 | we'll simply never succeed the s == s0 test below. */ | |
655 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
656 | { | |
657 | /* Recall that in this code path, a section cannot satisfy more | |
658 | than one spec, so if s == s0 then it cannot match | |
659 | wildspec1. */ | |
660 | if (s == s0) | |
661 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
662 | else | |
663 | { | |
fd361982 | 664 | const char *sname = bfd_section_name (s); |
72223188 JJ |
665 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
666 | ||
667 | if (!skip) | |
668 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, | |
669 | data); | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
674 | static void | |
675 | walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr, | |
676 | lang_input_statement_type *file, | |
677 | callback_t callback, | |
678 | void *data) | |
679 | { | |
680 | asection *s; | |
681 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
682 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
683 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
684 | bfd_boolean multiple_sections_found; | |
685 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
686 | ||
687 | if (multiple_sections_found) | |
688 | { | |
689 | walk_wild_section_general (ptr, file, callback, data); | |
690 | return; | |
691 | } | |
692 | ||
693 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
694 | { | |
695 | if (s == s0) | |
696 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
697 | else | |
698 | { | |
fd361982 | 699 | const char *sname = bfd_section_name (s); |
72223188 JJ |
700 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
701 | ||
702 | if (!skip) | |
703 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, data); | |
704 | else | |
705 | { | |
706 | skip = !match_simple_wild (wildsec2->spec.name, sname); | |
707 | if (!skip) | |
708 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
709 | data); | |
710 | } | |
711 | } | |
712 | } | |
713 | } | |
714 | ||
715 | static void | |
716 | walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr, | |
717 | lang_input_statement_type *file, | |
718 | callback_t callback, | |
719 | void *data) | |
720 | { | |
721 | asection *s; | |
722 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
723 | struct wildcard_list *sec1 = ptr->handler_data[1]; | |
724 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
725 | struct wildcard_list *wildsec3 = ptr->handler_data[3]; | |
726 | bfd_boolean multiple_sections_found; | |
727 | asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1; | |
728 | ||
729 | if (multiple_sections_found) | |
730 | { | |
731 | walk_wild_section_general (ptr, file, callback, data); | |
732 | return; | |
733 | } | |
734 | ||
735 | s1 = find_section (file, sec1, &multiple_sections_found); | |
736 | if (multiple_sections_found) | |
737 | { | |
738 | walk_wild_section_general (ptr, file, callback, data); | |
739 | return; | |
740 | } | |
741 | ||
742 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
743 | { | |
744 | if (s == s0) | |
745 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
746 | else | |
747 | if (s == s1) | |
748 | walk_wild_consider_section (ptr, file, s, sec1, callback, data); | |
749 | else | |
750 | { | |
fd361982 | 751 | const char *sname = bfd_section_name (s); |
72223188 JJ |
752 | bfd_boolean skip = !match_simple_wild (wildsec2->spec.name, |
753 | sname); | |
754 | ||
755 | if (!skip) | |
756 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
757 | data); | |
758 | else | |
759 | { | |
760 | skip = !match_simple_wild (wildsec3->spec.name, sname); | |
761 | if (!skip) | |
762 | walk_wild_consider_section (ptr, file, s, wildsec3, | |
763 | callback, data); | |
764 | } | |
765 | } | |
766 | } | |
767 | } | |
768 | ||
769 | static void | |
770 | walk_wild_section (lang_wild_statement_type *ptr, | |
771 | lang_input_statement_type *file, | |
772 | callback_t callback, | |
773 | void *data) | |
774 | { | |
66be1055 | 775 | if (file->flags.just_syms) |
72223188 JJ |
776 | return; |
777 | ||
778 | (*ptr->walk_wild_section_handler) (ptr, file, callback, data); | |
779 | } | |
780 | ||
781 | /* Returns TRUE when name1 is a wildcard spec that might match | |
782 | something name2 can match. We're conservative: we return FALSE | |
783 | only if the prefixes of name1 and name2 are different up to the | |
784 | first wildcard character. */ | |
785 | ||
786 | static bfd_boolean | |
787 | wild_spec_can_overlap (const char *name1, const char *name2) | |
788 | { | |
789 | size_t prefix1_len = strcspn (name1, "?*["); | |
790 | size_t prefix2_len = strcspn (name2, "?*["); | |
791 | size_t min_prefix_len; | |
792 | ||
793 | /* Note that if there is no wildcard character, then we treat the | |
794 | terminating 0 as part of the prefix. Thus ".text" won't match | |
795 | ".text." or ".text.*", for example. */ | |
796 | if (name1[prefix1_len] == '\0') | |
797 | prefix1_len++; | |
798 | if (name2[prefix2_len] == '\0') | |
799 | prefix2_len++; | |
800 | ||
801 | min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len; | |
802 | ||
803 | return memcmp (name1, name2, min_prefix_len) == 0; | |
804 | } | |
805 | ||
806 | /* Select specialized code to handle various kinds of wildcard | |
807 | statements. */ | |
808 | ||
809 | static void | |
810 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
811 | { | |
812 | int sec_count = 0; | |
813 | int wild_name_count = 0; | |
814 | struct wildcard_list *sec; | |
815 | int signature; | |
816 | int data_counter; | |
817 | ||
818 | ptr->walk_wild_section_handler = walk_wild_section_general; | |
7544697a AM |
819 | ptr->handler_data[0] = NULL; |
820 | ptr->handler_data[1] = NULL; | |
821 | ptr->handler_data[2] = NULL; | |
822 | ptr->handler_data[3] = NULL; | |
e6f2cbf5 | 823 | ptr->tree = NULL; |
72223188 JJ |
824 | |
825 | /* Count how many wildcard_specs there are, and how many of those | |
826 | actually use wildcards in the name. Also, bail out if any of the | |
827 | wildcard names are NULL. (Can this actually happen? | |
828 | walk_wild_section used to test for it.) And bail out if any | |
829 | of the wildcards are more complex than a simple string | |
830 | ending in a single '*'. */ | |
831 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
832 | { | |
833 | ++sec_count; | |
834 | if (sec->spec.name == NULL) | |
835 | return; | |
836 | if (wildcardp (sec->spec.name)) | |
837 | { | |
838 | ++wild_name_count; | |
839 | if (!is_simple_wild (sec->spec.name)) | |
840 | return; | |
841 | } | |
842 | } | |
843 | ||
844 | /* The zero-spec case would be easy to optimize but it doesn't | |
845 | happen in practice. Likewise, more than 4 specs doesn't | |
846 | happen in practice. */ | |
847 | if (sec_count == 0 || sec_count > 4) | |
848 | return; | |
849 | ||
850 | /* Check that no two specs can match the same section. */ | |
851 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
852 | { | |
853 | struct wildcard_list *sec2; | |
854 | for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next) | |
855 | { | |
856 | if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name)) | |
857 | return; | |
858 | } | |
859 | } | |
860 | ||
861 | signature = (sec_count << 8) + wild_name_count; | |
862 | switch (signature) | |
863 | { | |
864 | case 0x0100: | |
865 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0; | |
866 | break; | |
867 | case 0x0101: | |
868 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1; | |
869 | break; | |
870 | case 0x0201: | |
871 | ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1; | |
872 | break; | |
873 | case 0x0302: | |
874 | ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2; | |
875 | break; | |
876 | case 0x0402: | |
877 | ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2; | |
878 | break; | |
879 | default: | |
880 | return; | |
881 | } | |
882 | ||
883 | /* Now fill the data array with pointers to the specs, first the | |
884 | specs with non-wildcard names, then the specs with wildcard | |
885 | names. It's OK to process the specs in different order from the | |
886 | given order, because we've already determined that no section | |
887 | will match more than one spec. */ | |
888 | data_counter = 0; | |
889 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
890 | if (!wildcardp (sec->spec.name)) | |
891 | ptr->handler_data[data_counter++] = sec; | |
892 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
893 | if (wildcardp (sec->spec.name)) | |
894 | ptr->handler_data[data_counter++] = sec; | |
895 | } | |
896 | ||
4dec4d4e RH |
897 | /* Handle a wild statement for a single file F. */ |
898 | ||
899 | static void | |
1579bae1 AM |
900 | walk_wild_file (lang_wild_statement_type *s, |
901 | lang_input_statement_type *f, | |
902 | callback_t callback, | |
903 | void *data) | |
4dec4d4e | 904 | { |
8f1732fc AB |
905 | if (walk_wild_file_in_exclude_list (s->exclude_name_list, f)) |
906 | return; | |
907 | ||
4dec4d4e | 908 | if (f->the_bfd == NULL |
0aa7f586 | 909 | || !bfd_check_format (f->the_bfd, bfd_archive)) |
b6bf44ba | 910 | walk_wild_section (s, f, callback, data); |
4dec4d4e RH |
911 | else |
912 | { | |
913 | bfd *member; | |
914 | ||
915 | /* This is an archive file. We must map each member of the | |
916 | archive separately. */ | |
1579bae1 | 917 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
4dec4d4e RH |
918 | while (member != NULL) |
919 | { | |
920 | /* When lookup_name is called, it will call the add_symbols | |
921 | entry point for the archive. For each element of the | |
922 | archive which is included, BFD will call ldlang_add_file, | |
923 | which will set the usrdata field of the member to the | |
924 | lang_input_statement. */ | |
00f93c44 AM |
925 | if (bfd_usrdata (member) != NULL) |
926 | walk_wild_section (s, bfd_usrdata (member), callback, data); | |
4dec4d4e RH |
927 | |
928 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
929 | } | |
930 | } | |
931 | } | |
932 | ||
933 | static void | |
1579bae1 | 934 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
4dec4d4e | 935 | { |
b6bf44ba | 936 | const char *file_spec = s->filename; |
97407faf | 937 | char *p; |
b6bf44ba AM |
938 | |
939 | if (file_spec == NULL) | |
4dec4d4e RH |
940 | { |
941 | /* Perform the iteration over all files in the list. */ | |
e50d8076 | 942 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 943 | { |
b6bf44ba | 944 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
945 | } |
946 | } | |
97407faf AM |
947 | else if ((p = archive_path (file_spec)) != NULL) |
948 | { | |
949 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
950 | { | |
967928e9 AM |
951 | if (input_statement_is_archive_path (file_spec, p, f)) |
952 | walk_wild_file (s, f, callback, data); | |
97407faf AM |
953 | } |
954 | } | |
b6bf44ba | 955 | else if (wildcardp (file_spec)) |
4dec4d4e | 956 | { |
e50d8076 | 957 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 958 | { |
68bbb9f7 | 959 | if (fnmatch (file_spec, f->filename, 0) == 0) |
b6bf44ba | 960 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
961 | } |
962 | } | |
963 | else | |
964 | { | |
e50d8076 NC |
965 | lang_input_statement_type *f; |
966 | ||
4dec4d4e | 967 | /* Perform the iteration over a single file. */ |
b6bf44ba | 968 | f = lookup_name (file_spec); |
6770ec8c | 969 | if (f) |
b6bf44ba | 970 | walk_wild_file (s, f, callback, data); |
4dec4d4e | 971 | } |
5f992e62 AM |
972 | } |
973 | ||
08da4cac | 974 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
975 | function for each node, except those inside output section statements |
976 | with constraint set to -1. */ | |
252b5132 | 977 | |
8658f989 | 978 | void |
1579bae1 AM |
979 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
980 | lang_statement_union_type *s) | |
252b5132 | 981 | { |
1579bae1 | 982 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
983 | { |
984 | func (s); | |
985 | ||
986 | switch (s->header.type) | |
987 | { | |
988 | case lang_constructors_statement_enum: | |
989 | lang_for_each_statement_worker (func, constructor_list.head); | |
990 | break; | |
991 | case lang_output_section_statement_enum: | |
8658f989 AM |
992 | if (s->output_section_statement.constraint != -1) |
993 | lang_for_each_statement_worker | |
994 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
995 | break; |
996 | case lang_wild_statement_enum: | |
6feb9908 AM |
997 | lang_for_each_statement_worker (func, |
998 | s->wild_statement.children.head); | |
252b5132 RH |
999 | break; |
1000 | case lang_group_statement_enum: | |
1001 | lang_for_each_statement_worker (func, | |
1002 | s->group_statement.children.head); | |
1003 | break; | |
1004 | case lang_data_statement_enum: | |
1005 | case lang_reloc_statement_enum: | |
1006 | case lang_object_symbols_statement_enum: | |
1007 | case lang_output_statement_enum: | |
1008 | case lang_target_statement_enum: | |
1009 | case lang_input_section_enum: | |
1010 | case lang_input_statement_enum: | |
1011 | case lang_assignment_statement_enum: | |
1012 | case lang_padding_statement_enum: | |
1013 | case lang_address_statement_enum: | |
1014 | case lang_fill_statement_enum: | |
53d25da6 | 1015 | case lang_insert_statement_enum: |
252b5132 RH |
1016 | break; |
1017 | default: | |
1018 | FAIL (); | |
1019 | break; | |
1020 | } | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | void | |
1579bae1 | 1025 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1026 | { |
08da4cac | 1027 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1028 | } |
1029 | ||
1030 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1031 | |
252b5132 | 1032 | void |
1579bae1 | 1033 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1034 | { |
1579bae1 | 1035 | list->head = NULL; |
252b5132 RH |
1036 | list->tail = &list->head; |
1037 | } | |
1038 | ||
36983a93 AM |
1039 | static void |
1040 | lang_statement_append (lang_statement_list_type *list, | |
1041 | void *element, | |
1042 | void *field) | |
1043 | { | |
1044 | *(list->tail) = element; | |
1045 | list->tail = field; | |
1046 | } | |
1047 | ||
bde18da4 AM |
1048 | void |
1049 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1050 | { | |
1051 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1052 | abort (); | |
1053 | *stat_save_ptr++ = stat_ptr; | |
1054 | stat_ptr = new_ptr; | |
1055 | } | |
1056 | ||
1057 | void | |
1058 | pop_stat_ptr (void) | |
1059 | { | |
1060 | if (stat_save_ptr <= stat_save) | |
1061 | abort (); | |
1062 | stat_ptr = *--stat_save_ptr; | |
1063 | } | |
1064 | ||
08da4cac | 1065 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1066 | |
08da4cac | 1067 | static lang_statement_union_type * |
1579bae1 AM |
1068 | new_statement (enum statement_enum type, |
1069 | size_t size, | |
1070 | lang_statement_list_type *list) | |
252b5132 | 1071 | { |
d3ce72d0 | 1072 | lang_statement_union_type *new_stmt; |
252b5132 | 1073 | |
988de25b | 1074 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
1075 | new_stmt->header.type = type; |
1076 | new_stmt->header.next = NULL; | |
1077 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1078 | return new_stmt; | |
252b5132 RH |
1079 | } |
1080 | ||
08da4cac KH |
1081 | /* Build a new input file node for the language. There are several |
1082 | ways in which we treat an input file, eg, we only look at symbols, | |
1083 | or prefix it with a -l etc. | |
252b5132 | 1084 | |
08da4cac | 1085 | We can be supplied with requests for input files more than once; |
396a2467 | 1086 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1087 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1088 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1089 | |
252b5132 | 1090 | static lang_input_statement_type * |
1579bae1 AM |
1091 | new_afile (const char *name, |
1092 | lang_input_file_enum_type file_type, | |
1f1f5b92 | 1093 | const char *target) |
252b5132 RH |
1094 | { |
1095 | lang_input_statement_type *p; | |
1096 | ||
66be1055 AM |
1097 | lang_has_input_file = TRUE; |
1098 | ||
1f1f5b92 | 1099 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1100 | memset (&p->the_bfd, 0, |
1101 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
252b5132 | 1102 | p->target = target; |
66be1055 AM |
1103 | p->flags.dynamic = input_flags.dynamic; |
1104 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1105 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1106 | p->flags.whole_archive = input_flags.whole_archive; | |
f4a23d42 | 1107 | p->flags.sysrooted = input_flags.sysrooted; |
bcb674cf | 1108 | |
252b5132 RH |
1109 | switch (file_type) |
1110 | { | |
1111 | case lang_input_file_is_symbols_only_enum: | |
1112 | p->filename = name; | |
252b5132 | 1113 | p->local_sym_name = name; |
66be1055 AM |
1114 | p->flags.real = TRUE; |
1115 | p->flags.just_syms = TRUE; | |
252b5132 RH |
1116 | break; |
1117 | case lang_input_file_is_fake_enum: | |
1118 | p->filename = name; | |
252b5132 | 1119 | p->local_sym_name = name; |
252b5132 RH |
1120 | break; |
1121 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1122 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1123 | { |
1124 | p->filename = name + 1; | |
1125 | p->flags.full_name_provided = TRUE; | |
1126 | } | |
d4ae5fb0 | 1127 | else |
0aa7f586 | 1128 | p->filename = name; |
ff7a0acf | 1129 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
66be1055 AM |
1130 | p->flags.maybe_archive = TRUE; |
1131 | p->flags.real = TRUE; | |
1132 | p->flags.search_dirs = TRUE; | |
252b5132 RH |
1133 | break; |
1134 | case lang_input_file_is_marker_enum: | |
1135 | p->filename = name; | |
252b5132 | 1136 | p->local_sym_name = name; |
66be1055 | 1137 | p->flags.search_dirs = TRUE; |
252b5132 RH |
1138 | break; |
1139 | case lang_input_file_is_search_file_enum: | |
1140 | p->filename = name; | |
252b5132 | 1141 | p->local_sym_name = name; |
66be1055 AM |
1142 | p->flags.real = TRUE; |
1143 | p->flags.search_dirs = TRUE; | |
252b5132 RH |
1144 | break; |
1145 | case lang_input_file_is_file_enum: | |
1146 | p->filename = name; | |
252b5132 | 1147 | p->local_sym_name = name; |
66be1055 | 1148 | p->flags.real = TRUE; |
252b5132 RH |
1149 | break; |
1150 | default: | |
1151 | FAIL (); | |
1152 | } | |
c4b78195 | 1153 | |
36983a93 | 1154 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1155 | return p; |
1156 | } | |
1157 | ||
1158 | lang_input_statement_type * | |
1579bae1 AM |
1159 | lang_add_input_file (const char *name, |
1160 | lang_input_file_enum_type file_type, | |
1161 | const char *target) | |
252b5132 | 1162 | { |
3aa2d05a NC |
1163 | if (name != NULL |
1164 | && (*name == '=' || CONST_STRNEQ (name, "$SYSROOT"))) | |
bfa23434 HPN |
1165 | { |
1166 | lang_input_statement_type *ret; | |
1167 | char *sysrooted_name | |
3aa2d05a NC |
1168 | = concat (ld_sysroot, |
1169 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1170 | (const char *) NULL); | |
bfa23434 HPN |
1171 | |
1172 | /* We've now forcibly prepended the sysroot, making the input | |
1173 | file independent of the context. Therefore, temporarily | |
1174 | force a non-sysrooted context for this statement, so it won't | |
1175 | get the sysroot prepended again when opened. (N.B. if it's a | |
1176 | script, any child nodes with input files starting with "/" | |
1177 | will be handled as "sysrooted" as they'll be found to be | |
1178 | within the sysroot subdirectory.) */ | |
1179 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1180 | input_flags.sysrooted = 0; | |
1f1f5b92 | 1181 | ret = new_afile (sysrooted_name, file_type, target); |
bfa23434 HPN |
1182 | input_flags.sysrooted = outer_sysrooted; |
1183 | return ret; | |
1184 | } | |
1185 | ||
1f1f5b92 | 1186 | return new_afile (name, file_type, target); |
252b5132 RH |
1187 | } |
1188 | ||
409d7240 | 1189 | struct out_section_hash_entry |
750877ba L |
1190 | { |
1191 | struct bfd_hash_entry root; | |
409d7240 | 1192 | lang_statement_union_type s; |
750877ba L |
1193 | }; |
1194 | ||
1195 | /* The hash table. */ | |
1196 | ||
409d7240 | 1197 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1198 | |
384a9dda | 1199 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1200 | initialize the table, fill in an entry and remove the table. */ |
1201 | ||
1202 | static struct bfd_hash_entry * | |
329c1c86 | 1203 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1204 | struct bfd_hash_table *table, |
1205 | const char *string) | |
750877ba | 1206 | { |
384a9dda | 1207 | lang_output_section_statement_type **nextp; |
409d7240 | 1208 | struct out_section_hash_entry *ret; |
384a9dda AM |
1209 | |
1210 | if (entry == NULL) | |
1211 | { | |
1e9cc1c2 | 1212 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1213 | sizeof (*ret)); |
384a9dda AM |
1214 | if (entry == NULL) |
1215 | return entry; | |
1216 | } | |
1217 | ||
1218 | entry = bfd_hash_newfunc (entry, table, string); | |
1219 | if (entry == NULL) | |
1220 | return entry; | |
1221 | ||
409d7240 AM |
1222 | ret = (struct out_section_hash_entry *) entry; |
1223 | memset (&ret->s, 0, sizeof (ret->s)); | |
1224 | ret->s.header.type = lang_output_section_statement_enum; | |
3d9c8f6b AM |
1225 | ret->s.output_section_statement.subsection_alignment = NULL; |
1226 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1227 | ret->s.output_section_statement.block_value = 1; |
1228 | lang_list_init (&ret->s.output_section_statement.children); | |
1229 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1230 | |
218868ba | 1231 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1232 | first one, lang_os_list.tail points to the "next" |
218868ba | 1233 | field of the last element of the list. */ |
5c1e6d53 | 1234 | if (lang_os_list.head != NULL) |
409d7240 AM |
1235 | ret->s.output_section_statement.prev |
1236 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1237 | ((char *) lang_os_list.tail |
409d7240 | 1238 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1239 | |
384a9dda AM |
1240 | /* GCC's strict aliasing rules prevent us from just casting the |
1241 | address, so we store the pointer in a variable and cast that | |
1242 | instead. */ | |
409d7240 | 1243 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1244 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
384a9dda | 1245 | return &ret->root; |
750877ba L |
1246 | } |
1247 | ||
1248 | static void | |
409d7240 | 1249 | output_section_statement_table_init (void) |
750877ba | 1250 | { |
409d7240 AM |
1251 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1252 | output_section_statement_newfunc, | |
1253 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1254 | 61)) |
df5f2391 | 1255 | einfo (_("%F%P: can not create hash table: %E\n")); |
750877ba L |
1256 | } |
1257 | ||
1258 | static void | |
409d7240 | 1259 | output_section_statement_table_free (void) |
750877ba | 1260 | { |
409d7240 | 1261 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1262 | } |
1263 | ||
08da4cac KH |
1264 | /* Build enough state so that the parser can build its tree. */ |
1265 | ||
252b5132 | 1266 | void |
1579bae1 | 1267 | lang_init (void) |
252b5132 RH |
1268 | { |
1269 | obstack_begin (&stat_obstack, 1000); | |
1270 | ||
1271 | stat_ptr = &statement_list; | |
1272 | ||
409d7240 | 1273 | output_section_statement_table_init (); |
750877ba | 1274 | |
252b5132 RH |
1275 | lang_list_init (stat_ptr); |
1276 | ||
1277 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1278 | lang_list_init (&lang_os_list); |
252b5132 | 1279 | lang_list_init (&file_chain); |
1579bae1 AM |
1280 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1281 | NULL); | |
08da4cac | 1282 | abs_output_section = |
66c103b7 | 1283 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE); |
252b5132 RH |
1284 | |
1285 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1286 | |
16e4ecc0 AM |
1287 | asneeded_list_head = NULL; |
1288 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1289 | } |
1290 | ||
750877ba L |
1291 | void |
1292 | lang_finish (void) | |
1293 | { | |
409d7240 | 1294 | output_section_statement_table_free (); |
750877ba L |
1295 | } |
1296 | ||
252b5132 | 1297 | /*---------------------------------------------------------------------- |
08da4cac KH |
1298 | A region is an area of memory declared with the |
1299 | MEMORY { name:org=exp, len=exp ... } | |
1300 | syntax. | |
252b5132 | 1301 | |
08da4cac | 1302 | We maintain a list of all the regions here. |
252b5132 | 1303 | |
08da4cac | 1304 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1305 | which is created when looked up to be the entire data space. |
1306 | ||
1307 | If create is true we are creating a region inside a MEMORY block. | |
1308 | In this case it is probably an error to create a region that has | |
1309 | already been created. If we are not inside a MEMORY block it is | |
1310 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 | 1311 | and so we issue a warning. |
1e0061d2 | 1312 | |
4a93e180 NC |
1313 | Each region has at least one name. The first name is either |
1314 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1315 | alias names to an existing region within a script with | |
1316 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1317 | region. */ | |
252b5132 RH |
1318 | |
1319 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1320 | static lang_memory_region_type **lang_memory_region_list_tail |
1321 | = &lang_memory_region_list; | |
252b5132 RH |
1322 | |
1323 | lang_memory_region_type * | |
a747ee4d | 1324 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
252b5132 | 1325 | { |
4a93e180 NC |
1326 | lang_memory_region_name *n; |
1327 | lang_memory_region_type *r; | |
d3ce72d0 | 1328 | lang_memory_region_type *new_region; |
252b5132 | 1329 | |
9f88b410 RS |
1330 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1331 | if (name == NULL) | |
1332 | return NULL; | |
1333 | ||
4a93e180 NC |
1334 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1335 | for (n = &r->name_list; n != NULL; n = n->next) | |
1336 | if (strcmp (n->name, name) == 0) | |
4724d37e RM |
1337 | { |
1338 | if (create) | |
c1c8c1ef | 1339 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1340 | NULL, name); |
1341 | return r; | |
1342 | } | |
252b5132 | 1343 | |
a747ee4d | 1344 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1345 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1346 | NULL, name); |
a747ee4d | 1347 | |
988de25b | 1348 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1349 | |
d3ce72d0 NC |
1350 | new_region->name_list.name = xstrdup (name); |
1351 | new_region->name_list.next = NULL; | |
1352 | new_region->next = NULL; | |
cc9ad334 | 1353 | new_region->origin_exp = NULL; |
d3ce72d0 | 1354 | new_region->origin = 0; |
cc9ad334 | 1355 | new_region->length_exp = NULL; |
d3ce72d0 NC |
1356 | new_region->length = ~(bfd_size_type) 0; |
1357 | new_region->current = 0; | |
1358 | new_region->last_os = NULL; | |
1359 | new_region->flags = 0; | |
1360 | new_region->not_flags = 0; | |
1361 | new_region->had_full_message = FALSE; | |
252b5132 | 1362 | |
d3ce72d0 NC |
1363 | *lang_memory_region_list_tail = new_region; |
1364 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1365 | |
d3ce72d0 | 1366 | return new_region; |
252b5132 RH |
1367 | } |
1368 | ||
4a93e180 | 1369 | void |
0aa7f586 | 1370 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1371 | { |
0aa7f586 AM |
1372 | lang_memory_region_name *n; |
1373 | lang_memory_region_type *r; | |
1374 | lang_memory_region_type *region; | |
4a93e180 NC |
1375 | |
1376 | /* The default region must be unique. This ensures that it is not necessary | |
1377 | to iterate through the name list if someone wants the check if a region is | |
1378 | the default memory region. */ | |
1379 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1380 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
c1c8c1ef | 1381 | einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
1382 | |
1383 | /* Look for the target region and check if the alias is not already | |
1384 | in use. */ | |
1385 | region = NULL; | |
1386 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1387 | for (n = &r->name_list; n != NULL; n = n->next) | |
1388 | { | |
4724d37e RM |
1389 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1390 | region = r; | |
1391 | if (strcmp (n->name, alias) == 0) | |
c1c8c1ef | 1392 | einfo (_("%F%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1393 | "alias `%s'\n"), |
1394 | NULL, alias); | |
4a93e180 NC |
1395 | } |
1396 | ||
1397 | /* Check if the target region exists. */ | |
1398 | if (region == NULL) | |
c1c8c1ef | 1399 | einfo (_("%F%P:%pS: error: memory region `%s' " |
4724d37e RM |
1400 | "for alias `%s' does not exist\n"), |
1401 | NULL, region_name, alias); | |
4a93e180 NC |
1402 | |
1403 | /* Add alias to region name list. */ | |
988de25b | 1404 | n = stat_alloc (sizeof (lang_memory_region_name)); |
4a93e180 NC |
1405 | n->name = xstrdup (alias); |
1406 | n->next = region->name_list.next; | |
1407 | region->name_list.next = n; | |
1408 | } | |
1409 | ||
5f992e62 | 1410 | static lang_memory_region_type * |
0aa7f586 | 1411 | lang_memory_default (asection *section) |
252b5132 RH |
1412 | { |
1413 | lang_memory_region_type *p; | |
1414 | ||
1415 | flagword sec_flags = section->flags; | |
1416 | ||
1417 | /* Override SEC_DATA to mean a writable section. */ | |
1418 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1419 | sec_flags |= SEC_DATA; | |
1420 | ||
1579bae1 | 1421 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1422 | { |
1423 | if ((p->flags & sec_flags) != 0 | |
1424 | && (p->not_flags & sec_flags) == 0) | |
1425 | { | |
1426 | return p; | |
1427 | } | |
1428 | } | |
a747ee4d | 1429 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
252b5132 RH |
1430 | } |
1431 | ||
24ef1aa7 GM |
1432 | /* Get the output section statement directly from the userdata. */ |
1433 | ||
1434 | lang_output_section_statement_type * | |
1435 | lang_output_section_get (const asection *output_section) | |
1436 | { | |
a48931cc | 1437 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1438 | } |
1439 | ||
d127ecce AM |
1440 | /* Find or create an output_section_statement with the given NAME. |
1441 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
1442 | match any non-negative constraint. If CREATE, always make a | |
1443 | new output_section_statement for SPECIAL CONSTRAINT. */ | |
1444 | ||
384a9dda | 1445 | lang_output_section_statement_type * |
d127ecce | 1446 | lang_output_section_statement_lookup (const char *name, |
66c103b7 AM |
1447 | int constraint, |
1448 | bfd_boolean create) | |
252b5132 | 1449 | { |
409d7240 | 1450 | struct out_section_hash_entry *entry; |
252b5132 | 1451 | |
409d7240 AM |
1452 | entry = ((struct out_section_hash_entry *) |
1453 | bfd_hash_lookup (&output_section_statement_table, name, | |
66c103b7 | 1454 | create, FALSE)); |
384a9dda AM |
1455 | if (entry == NULL) |
1456 | { | |
66c103b7 | 1457 | if (create) |
df5f2391 | 1458 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1459 | return NULL; |
1460 | } | |
252b5132 | 1461 | |
409d7240 | 1462 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1463 | { |
384a9dda AM |
1464 | /* We have a section of this name, but it might not have the correct |
1465 | constraint. */ | |
66c103b7 | 1466 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1467 | |
d127ecce | 1468 | name = entry->s.output_section_statement.name; |
8a99a385 AM |
1469 | if (create && constraint == SPECIAL) |
1470 | /* Not traversing to the end reverses the order of the second | |
1471 | and subsequent SPECIAL sections in the hash table chain, | |
1472 | but that shouldn't matter. */ | |
1473 | last_ent = entry; | |
1474 | else | |
1475 | do | |
1476 | { | |
d127ecce AM |
1477 | if (constraint == entry->s.output_section_statement.constraint |
1478 | || (constraint == 0 | |
1479 | && entry->s.output_section_statement.constraint >= 0)) | |
8a99a385 AM |
1480 | return &entry->s.output_section_statement; |
1481 | last_ent = entry; | |
1482 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1483 | } | |
1484 | while (entry != NULL | |
d127ecce | 1485 | && name == entry->s.output_section_statement.name); |
252b5132 | 1486 | |
66c103b7 AM |
1487 | if (!create) |
1488 | return NULL; | |
1489 | ||
409d7240 AM |
1490 | entry |
1491 | = ((struct out_section_hash_entry *) | |
1492 | output_section_statement_newfunc (NULL, | |
1493 | &output_section_statement_table, | |
1494 | name)); | |
750877ba | 1495 | if (entry == NULL) |
384a9dda | 1496 | { |
df5f2391 | 1497 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1498 | return NULL; |
1499 | } | |
1500 | entry->root = last_ent->root; | |
1501 | last_ent->root.next = &entry->root; | |
252b5132 | 1502 | } |
384a9dda | 1503 | |
409d7240 AM |
1504 | entry->s.output_section_statement.name = name; |
1505 | entry->s.output_section_statement.constraint = constraint; | |
1506 | return &entry->s.output_section_statement; | |
252b5132 RH |
1507 | } |
1508 | ||
d127ecce AM |
1509 | /* Find the next output_section_statement with the same name as OS. |
1510 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1511 | match any non-negative constraint. */ | |
1512 | ||
1513 | lang_output_section_statement_type * | |
1514 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1515 | int constraint) | |
1516 | { | |
1517 | /* All output_section_statements are actually part of a | |
1518 | struct out_section_hash_entry. */ | |
1519 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1520 | ((char *) os | |
1521 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1522 | const char *name = os->name; | |
1523 | ||
1524 | ASSERT (name == entry->root.string); | |
1525 | do | |
1526 | { | |
1527 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1528 | if (entry == NULL | |
1529 | || name != entry->s.output_section_statement.name) | |
1530 | return NULL; | |
1531 | } | |
1532 | while (constraint != entry->s.output_section_statement.constraint | |
1533 | && (constraint != 0 | |
1534 | || entry->s.output_section_statement.constraint < 0)); | |
1535 | ||
1536 | return &entry->s.output_section_statement; | |
1537 | } | |
1538 | ||
afd7a018 AM |
1539 | /* A variant of lang_output_section_find used by place_orphan. |
1540 | Returns the output statement that should precede a new output | |
1541 | statement for SEC. If an exact match is found on certain flags, | |
1542 | sets *EXACT too. */ | |
1543 | ||
1544 | lang_output_section_statement_type * | |
1545 | lang_output_section_find_by_flags (const asection *sec, | |
93638471 | 1546 | flagword sec_flags, |
390fbbf1 AM |
1547 | lang_output_section_statement_type **exact, |
1548 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1549 | { |
1550 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1551 | flagword look_flags, differ; |
afd7a018 AM |
1552 | |
1553 | /* We know the first statement on this list is *ABS*. May as well | |
1554 | skip it. */ | |
5c1e6d53 | 1555 | first = &lang_os_list.head->output_section_statement; |
afd7a018 AM |
1556 | first = first->next; |
1557 | ||
1558 | /* First try for an exact match. */ | |
1559 | found = NULL; | |
1560 | for (look = first; look; look = look->next) | |
1561 | { | |
d9d94ac8 | 1562 | look_flags = look->flags; |
afd7a018 | 1563 | if (look->bfd_section != NULL) |
ecca9871 | 1564 | { |
d9d94ac8 | 1565 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1566 | if (match_type && !match_type (link_info.output_bfd, |
1567 | look->bfd_section, | |
390fbbf1 | 1568 | sec->owner, sec)) |
ecca9871 L |
1569 | continue; |
1570 | } | |
d9d94ac8 AM |
1571 | differ = look_flags ^ sec_flags; |
1572 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1573 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1574 | found = look; |
1575 | } | |
1576 | if (found != NULL) | |
1577 | { | |
390fbbf1 AM |
1578 | if (exact != NULL) |
1579 | *exact = found; | |
afd7a018 AM |
1580 | return found; |
1581 | } | |
1582 | ||
d9d94ac8 AM |
1583 | if ((sec_flags & SEC_CODE) != 0 |
1584 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1585 | { |
1586 | /* Try for a rw code section. */ | |
1587 | for (look = first; look; look = look->next) | |
1588 | { | |
d9d94ac8 | 1589 | look_flags = look->flags; |
afd7a018 | 1590 | if (look->bfd_section != NULL) |
ecca9871 | 1591 | { |
d9d94ac8 | 1592 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1593 | if (match_type && !match_type (link_info.output_bfd, |
1594 | look->bfd_section, | |
390fbbf1 | 1595 | sec->owner, sec)) |
ecca9871 L |
1596 | continue; |
1597 | } | |
d9d94ac8 AM |
1598 | differ = look_flags ^ sec_flags; |
1599 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1600 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1601 | found = look; |
1602 | } | |
afd7a018 | 1603 | } |
d9d94ac8 AM |
1604 | else if ((sec_flags & SEC_READONLY) != 0 |
1605 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1606 | { |
1607 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1608 | for (look = first; look; look = look->next) | |
1609 | { | |
d9d94ac8 | 1610 | look_flags = look->flags; |
afd7a018 | 1611 | if (look->bfd_section != NULL) |
ecca9871 | 1612 | { |
d9d94ac8 | 1613 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1614 | if (match_type && !match_type (link_info.output_bfd, |
1615 | look->bfd_section, | |
390fbbf1 | 1616 | sec->owner, sec)) |
ecca9871 L |
1617 | continue; |
1618 | } | |
d9d94ac8 AM |
1619 | differ = look_flags ^ sec_flags; |
1620 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1621 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1622 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1623 | | SEC_READONLY)) | |
1624 | && !(look_flags & SEC_SMALL_DATA))) | |
afd7a018 AM |
1625 | found = look; |
1626 | } | |
afd7a018 | 1627 | } |
d9d94ac8 AM |
1628 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 |
1629 | && (sec_flags & SEC_ALLOC) != 0) | |
1630 | { | |
1631 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1632 | as if it were a loaded section, and don't use match_type. */ | |
1633 | bfd_boolean seen_thread_local = FALSE; | |
1634 | ||
1635 | match_type = NULL; | |
1636 | for (look = first; look; look = look->next) | |
1637 | { | |
1638 | look_flags = look->flags; | |
1639 | if (look->bfd_section != NULL) | |
1640 | look_flags = look->bfd_section->flags; | |
1641 | ||
1642 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1643 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1644 | { | |
1645 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1646 | if (!(look_flags & SEC_LOAD) | |
1647 | && (sec_flags & SEC_LOAD)) | |
1648 | /* ..so if we're at a .tbss section and we're placing | |
1649 | a .tdata section stop looking and return the | |
1650 | previous section. */ | |
1651 | break; | |
1652 | found = look; | |
1653 | seen_thread_local = TRUE; | |
1654 | } | |
1655 | else if (seen_thread_local) | |
1656 | break; | |
1657 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1658 | found = look; | |
1659 | } | |
1660 | } | |
1661 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1662 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1663 | { |
1664 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1665 | for (look = first; look; look = look->next) | |
1666 | { | |
d9d94ac8 | 1667 | look_flags = look->flags; |
afd7a018 | 1668 | if (look->bfd_section != NULL) |
ecca9871 | 1669 | { |
d9d94ac8 | 1670 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1671 | if (match_type && !match_type (link_info.output_bfd, |
1672 | look->bfd_section, | |
390fbbf1 | 1673 | sec->owner, sec)) |
ecca9871 L |
1674 | continue; |
1675 | } | |
d9d94ac8 AM |
1676 | differ = look_flags ^ sec_flags; |
1677 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1678 | | SEC_THREAD_LOCAL)) | |
1679 | || ((look_flags & SEC_SMALL_DATA) | |
1680 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
afd7a018 AM |
1681 | found = look; |
1682 | } | |
afd7a018 | 1683 | } |
d9d94ac8 AM |
1684 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1685 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1686 | { |
1687 | /* .data goes after .rodata. */ | |
1688 | for (look = first; look; look = look->next) | |
1689 | { | |
d9d94ac8 | 1690 | look_flags = look->flags; |
afd7a018 | 1691 | if (look->bfd_section != NULL) |
ecca9871 | 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 | 1696 | sec->owner, sec)) |
ecca9871 L |
1697 | continue; |
1698 | } | |
d9d94ac8 AM |
1699 | differ = look_flags ^ sec_flags; |
1700 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1701 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1702 | found = look; |
1703 | } | |
afd7a018 | 1704 | } |
d9d94ac8 | 1705 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1706 | { |
07890c07 | 1707 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1708 | for (look = first; look; look = look->next) |
ecca9871 | 1709 | { |
d9d94ac8 | 1710 | look_flags = look->flags; |
390fbbf1 AM |
1711 | if (look->bfd_section != NULL) |
1712 | { | |
d9d94ac8 | 1713 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1714 | if (match_type && !match_type (link_info.output_bfd, |
1715 | look->bfd_section, | |
390fbbf1 AM |
1716 | sec->owner, sec)) |
1717 | continue; | |
1718 | } | |
d9d94ac8 AM |
1719 | differ = look_flags ^ sec_flags; |
1720 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1721 | found = look; |
ecca9871 | 1722 | } |
afd7a018 | 1723 | } |
07890c07 AM |
1724 | else |
1725 | { | |
1726 | /* non-alloc go last. */ | |
1727 | for (look = first; look; look = look->next) | |
1728 | { | |
d9d94ac8 | 1729 | look_flags = look->flags; |
07890c07 | 1730 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1731 | look_flags = look->bfd_section->flags; |
1732 | differ = look_flags ^ sec_flags; | |
1733 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1734 | found = look; |
1735 | } | |
1736 | return found; | |
1737 | } | |
afd7a018 | 1738 | |
390fbbf1 AM |
1739 | if (found || !match_type) |
1740 | return found; | |
1741 | ||
93638471 | 1742 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
1743 | } |
1744 | ||
1745 | /* Find the last output section before given output statement. | |
1746 | Used by place_orphan. */ | |
1747 | ||
1748 | static asection * | |
1749 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1750 | { | |
afd7a018 AM |
1751 | lang_output_section_statement_type *lookup; |
1752 | ||
218868ba | 1753 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1754 | { |
66c103b7 | 1755 | if (lookup->constraint < 0) |
afd7a018 | 1756 | continue; |
afd7a018 AM |
1757 | |
1758 | if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) | |
218868ba | 1759 | return lookup->bfd_section; |
afd7a018 AM |
1760 | } |
1761 | ||
1762 | return NULL; | |
1763 | } | |
1764 | ||
53d25da6 AM |
1765 | /* Look for a suitable place for a new output section statement. The |
1766 | idea is to skip over anything that might be inside a SECTIONS {} | |
1767 | statement in a script, before we find another output section | |
1768 | statement. Assignments to "dot" before an output section statement | |
0fa4d7cf AM |
1769 | are assumed to belong to it, except in two cases; The first |
1770 | assignment to dot, and assignments before non-alloc sections. | |
1771 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1772 | similar assignments that set the initial address, or we might | |
1773 | insert non-alloc note sections among assignments setting end of | |
1774 | image symbols. */ | |
53d25da6 AM |
1775 | |
1776 | static lang_statement_union_type ** | |
1777 | insert_os_after (lang_output_section_statement_type *after) | |
1778 | { | |
1779 | lang_statement_union_type **where; | |
1780 | lang_statement_union_type **assign = NULL; | |
1781 | bfd_boolean ignore_first; | |
1782 | ||
5c1e6d53 | 1783 | ignore_first = after == &lang_os_list.head->output_section_statement; |
53d25da6 AM |
1784 | |
1785 | for (where = &after->header.next; | |
1786 | *where != NULL; | |
1787 | where = &(*where)->header.next) | |
1788 | { | |
1789 | switch ((*where)->header.type) | |
1790 | { | |
1791 | case lang_assignment_statement_enum: | |
1792 | if (assign == NULL) | |
1793 | { | |
1794 | lang_assignment_statement_type *ass; | |
1795 | ||
1796 | ass = &(*where)->assignment_statement; | |
1797 | if (ass->exp->type.node_class != etree_assert | |
1798 | && ass->exp->assign.dst[0] == '.' | |
73589426 AM |
1799 | && ass->exp->assign.dst[1] == 0) |
1800 | { | |
1801 | if (!ignore_first) | |
1802 | assign = where; | |
1803 | ignore_first = FALSE; | |
1804 | } | |
53d25da6 | 1805 | } |
53d25da6 AM |
1806 | continue; |
1807 | case lang_wild_statement_enum: | |
1808 | case lang_input_section_enum: | |
1809 | case lang_object_symbols_statement_enum: | |
1810 | case lang_fill_statement_enum: | |
1811 | case lang_data_statement_enum: | |
1812 | case lang_reloc_statement_enum: | |
1813 | case lang_padding_statement_enum: | |
1814 | case lang_constructors_statement_enum: | |
1815 | assign = NULL; | |
73589426 | 1816 | ignore_first = FALSE; |
53d25da6 AM |
1817 | continue; |
1818 | case lang_output_section_statement_enum: | |
1819 | if (assign != NULL) | |
0fa4d7cf AM |
1820 | { |
1821 | asection *s = (*where)->output_section_statement.bfd_section; | |
1822 | ||
249b2a84 L |
1823 | if (s == NULL |
1824 | || s->map_head.s == NULL | |
1825 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1826 | where = assign; |
1827 | } | |
53d25da6 AM |
1828 | break; |
1829 | case lang_input_statement_enum: | |
1830 | case lang_address_statement_enum: | |
1831 | case lang_target_statement_enum: | |
1832 | case lang_output_statement_enum: | |
1833 | case lang_group_statement_enum: | |
1834 | case lang_insert_statement_enum: | |
1835 | continue; | |
1836 | } | |
1837 | break; | |
1838 | } | |
1839 | ||
1840 | return where; | |
1841 | } | |
1842 | ||
afd7a018 | 1843 | lang_output_section_statement_type * |
7b986e99 | 1844 | lang_insert_orphan (asection *s, |
afd7a018 | 1845 | const char *secname, |
8a99a385 | 1846 | int constraint, |
afd7a018 AM |
1847 | lang_output_section_statement_type *after, |
1848 | struct orphan_save *place, | |
1849 | etree_type *address, | |
1850 | lang_statement_list_type *add_child) | |
1851 | { | |
afd7a018 | 1852 | lang_statement_list_type add; |
afd7a018 AM |
1853 | lang_output_section_statement_type *os; |
1854 | lang_output_section_statement_type **os_tail; | |
1855 | ||
afd7a018 AM |
1856 | /* If we have found an appropriate place for the output section |
1857 | statements for this orphan, add them to our own private list, | |
1858 | inserting them later into the global statement list. */ | |
1859 | if (after != NULL) | |
1860 | { | |
bde18da4 AM |
1861 | lang_list_init (&add); |
1862 | push_stat_ptr (&add); | |
afd7a018 AM |
1863 | } |
1864 | ||
0e1862bb L |
1865 | if (bfd_link_relocatable (&link_info) |
1866 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
1867 | address = exp_intop (0); |
1868 | ||
5c1e6d53 | 1869 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
1e9cc1c2 | 1870 | os = lang_enter_output_section_statement (secname, address, normal_section, |
1eec346e | 1871 | NULL, NULL, NULL, constraint, 0); |
011aa75f | 1872 | |
afd7a018 AM |
1873 | if (add_child == NULL) |
1874 | add_child = &os->children; | |
b9c361e0 | 1875 | lang_add_section (add_child, s, NULL, os); |
afd7a018 | 1876 | |
f77c3948 DJ |
1877 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
1878 | { | |
1879 | const char *region = (after->region | |
1880 | ? after->region->name_list.name | |
1881 | : DEFAULT_MEMORY_REGION); | |
1882 | const char *lma_region = (after->lma_region | |
1883 | ? after->lma_region->name_list.name | |
1884 | : NULL); | |
1885 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
1886 | lma_region); | |
1887 | } | |
1888 | else | |
1889 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
1890 | NULL); | |
afd7a018 | 1891 | |
afd7a018 AM |
1892 | /* Restore the global list pointer. */ |
1893 | if (after != NULL) | |
bde18da4 | 1894 | pop_stat_ptr (); |
afd7a018 AM |
1895 | |
1896 | if (after != NULL && os->bfd_section != NULL) | |
1897 | { | |
5daa8fe7 | 1898 | asection *snew, *as; |
1887ae73 | 1899 | bfd_boolean place_after = place->stmt == NULL; |
f3e660db | 1900 | bfd_boolean insert_after = TRUE; |
afd7a018 AM |
1901 | |
1902 | snew = os->bfd_section; | |
1903 | ||
1904 | /* Shuffle the bfd section list to make the output file look | |
1905 | neater. This is really only cosmetic. */ | |
1906 | if (place->section == NULL | |
5c1e6d53 | 1907 | && after != &lang_os_list.head->output_section_statement) |
afd7a018 AM |
1908 | { |
1909 | asection *bfd_section = after->bfd_section; | |
1910 | ||
1911 | /* If the output statement hasn't been used to place any input | |
1912 | sections (and thus doesn't have an output bfd_section), | |
1913 | look for the closest prior output statement having an | |
1914 | output section. */ | |
1915 | if (bfd_section == NULL) | |
1916 | bfd_section = output_prev_sec_find (after); | |
1917 | ||
1918 | if (bfd_section != NULL && bfd_section != snew) | |
1919 | place->section = &bfd_section->next; | |
1920 | } | |
1921 | ||
1922 | if (place->section == NULL) | |
f13a99db | 1923 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1924 | |
5daa8fe7 | 1925 | as = *place->section; |
5e542ba7 MS |
1926 | |
1927 | if (!as) | |
329c1c86 AM |
1928 | { |
1929 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1930 | |
1931 | /* Unlink the section. */ | |
f13a99db | 1932 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1933 | |
1934 | /* Now tack it back on in the right place. */ | |
f13a99db | 1935 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1936 | } |
1887ae73 L |
1937 | else if ((bfd_get_flavour (link_info.output_bfd) |
1938 | == bfd_target_elf_flavour) | |
1939 | && (bfd_get_flavour (s->owner) | |
1940 | == bfd_target_elf_flavour) | |
1941 | && ((elf_section_type (s) == SHT_NOTE | |
1942 | && (s->flags & SEC_LOAD) != 0) | |
1943 | || (elf_section_type (as) == SHT_NOTE | |
1944 | && (as->flags & SEC_LOAD) != 0))) | |
1945 | { | |
1946 | /* Make sure that output note sections are grouped and sorted | |
1947 | by alignments when inserting a note section or insert a | |
1948 | section after a note section, */ | |
1949 | asection *sec; | |
1950 | /* A specific section after which the output note section | |
1951 | should be placed. */ | |
1952 | asection *after_sec; | |
1953 | /* True if we need to insert the orphan section after a | |
1954 | specific section to maintain output note section order. */ | |
f3e660db L |
1955 | bfd_boolean after_sec_note = FALSE; |
1956 | ||
1957 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
1958 | |
1959 | /* Group and sort output note section by alignments in | |
1960 | ascending order. */ | |
1961 | after_sec = NULL; | |
1962 | if (elf_section_type (s) == SHT_NOTE | |
1963 | && (s->flags & SEC_LOAD) != 0) | |
1964 | { | |
f3e660db L |
1965 | /* Search from the beginning for the last output note |
1966 | section with equal or larger alignments. NB: Don't | |
1967 | place orphan note section after non-note sections. */ | |
1887ae73 | 1968 | |
f3e660db L |
1969 | first_orphan_note = NULL; |
1970 | for (sec = link_info.output_bfd->sections; | |
1887ae73 L |
1971 | (sec != NULL |
1972 | && !bfd_is_abs_section (sec)); | |
1973 | sec = sec->next) | |
1974 | if (sec != snew | |
1975 | && elf_section_type (sec) == SHT_NOTE | |
1976 | && (sec->flags & SEC_LOAD) != 0) | |
1977 | { | |
f3e660db L |
1978 | if (!first_orphan_note) |
1979 | first_orphan_note = sec; | |
1887ae73 L |
1980 | if (sec->alignment_power >= s->alignment_power) |
1981 | after_sec = sec; | |
1982 | } | |
f3e660db L |
1983 | else if (first_orphan_note) |
1984 | { | |
1985 | /* Stop if there is non-note section after the first | |
1986 | orphan note section. */ | |
1987 | break; | |
1988 | } | |
1887ae73 | 1989 | |
f3e660db L |
1990 | /* If this will be the first orphan note section, it can |
1991 | be placed at the default location. */ | |
1992 | after_sec_note = first_orphan_note != NULL; | |
1993 | if (after_sec == NULL && after_sec_note) | |
1887ae73 | 1994 | { |
f3e660db L |
1995 | /* If all output note sections have smaller |
1996 | alignments, place the section before all | |
1997 | output orphan note sections. */ | |
1998 | after_sec = first_orphan_note; | |
1999 | insert_after = FALSE; | |
1887ae73 L |
2000 | } |
2001 | } | |
f3e660db | 2002 | else if (first_orphan_note) |
1887ae73 | 2003 | { |
f3e660db L |
2004 | /* Don't place non-note sections in the middle of orphan |
2005 | note sections. */ | |
1887ae73 L |
2006 | after_sec_note = TRUE; |
2007 | after_sec = as; | |
2008 | for (sec = as->next; | |
2009 | (sec != NULL | |
2010 | && !bfd_is_abs_section (sec)); | |
2011 | sec = sec->next) | |
2012 | if (elf_section_type (sec) == SHT_NOTE | |
2013 | && (sec->flags & SEC_LOAD) != 0) | |
2014 | after_sec = sec; | |
2015 | } | |
2016 | ||
2017 | if (after_sec_note) | |
2018 | { | |
2019 | if (after_sec) | |
2020 | { | |
f3e660db L |
2021 | /* Search forward to insert OS after AFTER_SEC output |
2022 | statement. */ | |
2023 | lang_output_section_statement_type *stmt, *next; | |
2024 | bfd_boolean found = FALSE; | |
2025 | for (stmt = after; stmt != NULL; stmt = next) | |
2026 | { | |
2027 | next = stmt->next; | |
2028 | if (insert_after) | |
2029 | { | |
2030 | if (stmt->bfd_section == after_sec) | |
2031 | { | |
2032 | place_after = TRUE; | |
2033 | found = TRUE; | |
2034 | after = stmt; | |
2035 | break; | |
2036 | } | |
2037 | } | |
2038 | else | |
2039 | { | |
2040 | /* If INSERT_AFTER is FALSE, place OS before | |
2041 | AFTER_SEC output statement. */ | |
2042 | if (next && next->bfd_section == after_sec) | |
2043 | { | |
2044 | place_after = TRUE; | |
2045 | found = TRUE; | |
2046 | after = stmt; | |
2047 | break; | |
2048 | } | |
2049 | } | |
2050 | } | |
2051 | ||
2052 | /* Search backward to insert OS after AFTER_SEC output | |
2053 | statement. */ | |
2054 | if (!found) | |
2055 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
1887ae73 | 2056 | { |
f3e660db L |
2057 | if (insert_after) |
2058 | { | |
2059 | if (stmt->bfd_section == after_sec) | |
2060 | { | |
2061 | place_after = TRUE; | |
2062 | after = stmt; | |
2063 | break; | |
2064 | } | |
2065 | } | |
2066 | else | |
2067 | { | |
2068 | /* If INSERT_AFTER is FALSE, place OS before | |
2069 | AFTER_SEC output statement. */ | |
2070 | if (stmt->next->bfd_section == after_sec) | |
2071 | { | |
2072 | place_after = TRUE; | |
2073 | after = stmt; | |
2074 | break; | |
2075 | } | |
2076 | } | |
1887ae73 L |
2077 | } |
2078 | } | |
2079 | ||
f3e660db L |
2080 | if (after_sec == NULL |
2081 | || (insert_after && after_sec->next != snew) | |
2082 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
2083 | { |
2084 | /* Unlink the section. */ | |
2085 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2086 | ||
2087 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2088 | prepend SNEW. */ | |
2089 | if (after_sec) | |
f3e660db L |
2090 | { |
2091 | if (insert_after) | |
2092 | bfd_section_list_insert_after (link_info.output_bfd, | |
2093 | after_sec, snew); | |
2094 | else | |
2095 | bfd_section_list_insert_before (link_info.output_bfd, | |
2096 | after_sec, snew); | |
2097 | } | |
1887ae73 L |
2098 | else |
2099 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2100 | } | |
2101 | } | |
2102 | else if (as != snew && as->prev != snew) | |
2103 | { | |
2104 | /* Unlink the section. */ | |
2105 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2106 | ||
2107 | /* Now tack it back on in the right place. */ | |
2108 | bfd_section_list_insert_before (link_info.output_bfd, | |
2109 | as, snew); | |
2110 | } | |
2111 | } | |
5e542ba7 | 2112 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2113 | { |
2114 | /* Unlink the section. */ | |
f13a99db | 2115 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2116 | |
5daa8fe7 | 2117 | /* Now tack it back on in the right place. */ |
f13a99db | 2118 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2119 | } |
afd7a018 AM |
2120 | |
2121 | /* Save the end of this list. Further ophans of this type will | |
2122 | follow the one we've just added. */ | |
2123 | place->section = &snew->next; | |
2124 | ||
2125 | /* The following is non-cosmetic. We try to put the output | |
2126 | statements in some sort of reasonable order here, because they | |
2127 | determine the final load addresses of the orphan sections. | |
2128 | In addition, placing output statements in the wrong order may | |
2129 | require extra segments. For instance, given a typical | |
2130 | situation of all read-only sections placed in one segment and | |
2131 | following that a segment containing all the read-write | |
2132 | sections, we wouldn't want to place an orphan read/write | |
2133 | section before or amongst the read-only ones. */ | |
2134 | if (add.head != NULL) | |
2135 | { | |
2136 | lang_output_section_statement_type *newly_added_os; | |
2137 | ||
1887ae73 L |
2138 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2139 | if (place_after) | |
afd7a018 | 2140 | { |
53d25da6 | 2141 | lang_statement_union_type **where = insert_os_after (after); |
afd7a018 AM |
2142 | |
2143 | *add.tail = *where; | |
2144 | *where = add.head; | |
2145 | ||
2146 | place->os_tail = &after->next; | |
2147 | } | |
2148 | else | |
2149 | { | |
2150 | /* Put it after the last orphan statement we added. */ | |
2151 | *add.tail = *place->stmt; | |
2152 | *place->stmt = add.head; | |
2153 | } | |
2154 | ||
2155 | /* Fix the global list pointer if we happened to tack our | |
2156 | new list at the tail. */ | |
bde18da4 AM |
2157 | if (*stat_ptr->tail == add.head) |
2158 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
2159 | |
2160 | /* Save the end of this list. */ | |
2161 | place->stmt = add.tail; | |
2162 | ||
2163 | /* Do the same for the list of output section statements. */ | |
2164 | newly_added_os = *os_tail; | |
2165 | *os_tail = NULL; | |
218868ba AM |
2166 | newly_added_os->prev = (lang_output_section_statement_type *) |
2167 | ((char *) place->os_tail | |
2168 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2169 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2170 | if (newly_added_os->next != NULL) |
2171 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
2172 | *place->os_tail = newly_added_os; |
2173 | place->os_tail = &newly_added_os->next; | |
2174 | ||
2175 | /* Fixing the global list pointer here is a little different. | |
2176 | We added to the list in lang_enter_output_section_statement, | |
2177 | trimmed off the new output_section_statment above when | |
2178 | assigning *os_tail = NULL, but possibly added it back in | |
2179 | the same place when assigning *place->os_tail. */ | |
2180 | if (*os_tail == NULL) | |
5c1e6d53 | 2181 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2182 | } |
2183 | } | |
2184 | return os; | |
2185 | } | |
2186 | ||
16e4ecc0 AM |
2187 | static void |
2188 | lang_print_asneeded (void) | |
2189 | { | |
2190 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2191 | |
2192 | if (asneeded_list_head == NULL) | |
2193 | return; | |
2194 | ||
1273da04 | 2195 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2196 | |
2197 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2198 | { | |
2199 | size_t len; | |
2200 | ||
2201 | minfo ("%s", m->soname); | |
2202 | len = strlen (m->soname); | |
2203 | ||
2204 | if (len >= 29) | |
2205 | { | |
2206 | print_nl (); | |
2207 | len = 0; | |
2208 | } | |
2209 | while (len < 30) | |
2210 | { | |
2211 | print_space (); | |
2212 | ++len; | |
2213 | } | |
2214 | ||
2215 | if (m->ref != NULL) | |
871b3ab2 | 2216 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2217 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2218 | } |
2219 | } | |
2220 | ||
252b5132 | 2221 | static void |
1579bae1 | 2222 | lang_map_flags (flagword flag) |
252b5132 RH |
2223 | { |
2224 | if (flag & SEC_ALLOC) | |
2225 | minfo ("a"); | |
2226 | ||
2227 | if (flag & SEC_CODE) | |
2228 | minfo ("x"); | |
2229 | ||
2230 | if (flag & SEC_READONLY) | |
2231 | minfo ("r"); | |
2232 | ||
2233 | if (flag & SEC_DATA) | |
2234 | minfo ("w"); | |
2235 | ||
2236 | if (flag & SEC_LOAD) | |
2237 | minfo ("l"); | |
2238 | } | |
2239 | ||
2240 | void | |
1579bae1 | 2241 | lang_map (void) |
252b5132 RH |
2242 | { |
2243 | lang_memory_region_type *m; | |
4d4920ec | 2244 | bfd_boolean dis_header_printed = FALSE; |
252b5132 | 2245 | |
4d4920ec EB |
2246 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2247 | { | |
2248 | asection *s; | |
2249 | ||
2250 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2251 | || file->flags.just_syms) |
4d4920ec EB |
2252 | continue; |
2253 | ||
035801ce FS |
2254 | if (config.print_map_discarded) |
2255 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2256 | if ((s->output_section == NULL | |
2257 | || s->output_section->owner != link_info.output_bfd) | |
2258 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2259 | { | |
2260 | if (! dis_header_printed) | |
2261 | { | |
2262 | fprintf (config.map_file, _("\nDiscarded input sections\n\n")); | |
2263 | dis_header_printed = TRUE; | |
2264 | } | |
4d4920ec | 2265 | |
035801ce FS |
2266 | print_input_section (s, TRUE); |
2267 | } | |
4d4920ec EB |
2268 | } |
2269 | ||
252b5132 RH |
2270 | minfo (_("\nMemory Configuration\n\n")); |
2271 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2272 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2273 | ||
1579bae1 | 2274 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 RH |
2275 | { |
2276 | char buf[100]; | |
2277 | int len; | |
2278 | ||
4a93e180 | 2279 | fprintf (config.map_file, "%-16s ", m->name_list.name); |
252b5132 RH |
2280 | |
2281 | sprintf_vma (buf, m->origin); | |
2282 | minfo ("0x%s ", buf); | |
2283 | len = strlen (buf); | |
2284 | while (len < 16) | |
2285 | { | |
2286 | print_space (); | |
2287 | ++len; | |
2288 | } | |
2289 | ||
2290 | minfo ("0x%V", m->length); | |
2291 | if (m->flags || m->not_flags) | |
2292 | { | |
2293 | #ifndef BFD64 | |
2294 | minfo (" "); | |
2295 | #endif | |
2296 | if (m->flags) | |
2297 | { | |
2298 | print_space (); | |
2299 | lang_map_flags (m->flags); | |
2300 | } | |
2301 | ||
2302 | if (m->not_flags) | |
2303 | { | |
2304 | minfo (" !"); | |
2305 | lang_map_flags (m->not_flags); | |
2306 | } | |
2307 | } | |
2308 | ||
2309 | print_nl (); | |
2310 | } | |
2311 | ||
2312 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
2313 | ||
0aa7f586 | 2314 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2315 | { |
2316 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2317 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2318 | } | |
6a846243 | 2319 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2320 | lang_statement_iteration++; |
252b5132 | 2321 | print_statements (); |
7a2f2d82 | 2322 | |
0aa7f586 AM |
2323 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2324 | config.map_file); | |
252b5132 RH |
2325 | } |
2326 | ||
35835446 | 2327 | static bfd_boolean |
967928e9 AM |
2328 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2329 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2330 | { |
6fcc66ab AM |
2331 | if ((hash_entry->type == bfd_link_hash_defined |
2332 | || hash_entry->type == bfd_link_hash_defweak) | |
2333 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2334 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2335 | { |
6fcc66ab | 2336 | input_section_userdata_type *ud; |
35835446 JR |
2337 | struct map_symbol_def *def; |
2338 | ||
a48931cc | 2339 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2340 | if (!ud) |
35835446 | 2341 | { |
988de25b | 2342 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2343 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2344 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2345 | ud->map_symbol_def_count = 0; | |
35835446 | 2346 | } |
6fcc66ab | 2347 | else if (!ud->map_symbol_def_tail) |
35835446 | 2348 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2349 | |
1e9cc1c2 | 2350 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2351 | def->entry = hash_entry; |
76d7af2d | 2352 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2353 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2354 | ud->map_symbol_def_count++; |
35835446 JR |
2355 | } |
2356 | return TRUE; | |
2357 | } | |
2358 | ||
252b5132 RH |
2359 | /* Initialize an output section. */ |
2360 | ||
2361 | static void | |
dfa7b0b8 | 2362 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2363 | { |
252b5132 | 2364 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
df5f2391 | 2365 | einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2366 | |
8a99a385 AM |
2367 | if (s->constraint != SPECIAL) |
2368 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); | |
1579bae1 | 2369 | if (s->bfd_section == NULL) |
8a99a385 AM |
2370 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2371 | s->name, flags); | |
1579bae1 | 2372 | if (s->bfd_section == NULL) |
252b5132 | 2373 | { |
df5f2391 | 2374 | einfo (_("%F%P: output format %s cannot represent section" |
0aa7f586 | 2375 | " called %s: %E\n"), |
f13a99db | 2376 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2377 | } |
2378 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2379 | s->bfd_section->output_offset = 0; |
e0f6802f | 2380 | |
24ef1aa7 GM |
2381 | /* Set the userdata of the output section to the output section |
2382 | statement to avoid lookup. */ | |
a48931cc | 2383 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2384 | |
a431bc2e AM |
2385 | /* If there is a base address, make sure that any sections it might |
2386 | mention are initialized. */ | |
2387 | if (s->addr_tree != NULL) | |
2388 | exp_init_os (s->addr_tree); | |
2389 | ||
2390 | if (s->load_base != NULL) | |
2391 | exp_init_os (s->load_base); | |
2392 | ||
7270c5ed | 2393 | /* If supplied an alignment, set it. */ |
3d9c8f6b AM |
2394 | if (s->section_alignment != NULL) |
2395 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, | |
2396 | "section alignment"); | |
252b5132 RH |
2397 | } |
2398 | ||
2399 | /* Make sure that all output sections mentioned in an expression are | |
2400 | initialized. */ | |
2401 | ||
2402 | static void | |
1579bae1 | 2403 | exp_init_os (etree_type *exp) |
252b5132 RH |
2404 | { |
2405 | switch (exp->type.node_class) | |
2406 | { | |
2407 | case etree_assign: | |
9f4fb502 | 2408 | case etree_provide: |
eab62f2f | 2409 | case etree_provided: |
252b5132 RH |
2410 | exp_init_os (exp->assign.src); |
2411 | break; | |
2412 | ||
2413 | case etree_binary: | |
2414 | exp_init_os (exp->binary.lhs); | |
2415 | exp_init_os (exp->binary.rhs); | |
2416 | break; | |
2417 | ||
2418 | case etree_trinary: | |
2419 | exp_init_os (exp->trinary.cond); | |
2420 | exp_init_os (exp->trinary.lhs); | |
2421 | exp_init_os (exp->trinary.rhs); | |
2422 | break; | |
2423 | ||
b6ca8815 NS |
2424 | case etree_assert: |
2425 | exp_init_os (exp->assert_s.child); | |
2426 | break; | |
afd7a018 | 2427 | |
252b5132 RH |
2428 | case etree_unary: |
2429 | exp_init_os (exp->unary.child); | |
2430 | break; | |
2431 | ||
2432 | case etree_name: | |
2433 | switch (exp->type.node_code) | |
2434 | { | |
2435 | case ADDR: | |
2436 | case LOADADDR: | |
2437 | case SIZEOF: | |
2438 | { | |
2439 | lang_output_section_statement_type *os; | |
2440 | ||
2441 | os = lang_output_section_find (exp->name.name); | |
2442 | if (os != NULL && os->bfd_section == NULL) | |
dfa7b0b8 | 2443 | init_os (os, 0); |
252b5132 RH |
2444 | } |
2445 | } | |
2446 | break; | |
2447 | ||
2448 | default: | |
2449 | break; | |
2450 | } | |
2451 | } | |
9503fd87 | 2452 | \f |
252b5132 | 2453 | static void |
1579bae1 | 2454 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2455 | { |
1e9cc1c2 | 2456 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2457 | |
2458 | /* If we are only reading symbols from this object, then we want to | |
2459 | discard all sections. */ | |
66be1055 | 2460 | if (entry->flags.just_syms) |
252b5132 | 2461 | { |
1449d79b | 2462 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2463 | return; |
2464 | } | |
2465 | ||
2e84f9c1 AM |
2466 | /* Deal with SHF_EXCLUDE ELF sections. */ |
2467 | if (!bfd_link_relocatable (&link_info) | |
2468 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2469 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2470 | sec->output_section = bfd_abs_section_ptr; | |
2471 | ||
ace66bb2 | 2472 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2473 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2474 | } |
2475 | \f | |
5b5f4e6f AB |
2476 | |
2477 | /* Returns true if SECTION is one we know will be discarded based on its | |
2478 | section flags, otherwise returns false. */ | |
2479 | ||
2480 | static bfd_boolean | |
2481 | lang_discard_section_p (asection *section) | |
2482 | { | |
2483 | bfd_boolean discard; | |
2484 | flagword flags = section->flags; | |
2485 | ||
2486 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2487 | discard = (flags & SEC_EXCLUDE) != 0; | |
2488 | ||
2489 | /* Discard the group descriptor sections when we're finally placing the | |
2490 | sections from within the group. */ | |
2491 | if ((flags & SEC_GROUP) != 0 | |
2492 | && link_info.resolve_section_groups) | |
2493 | discard = TRUE; | |
2494 | ||
2495 | /* Discard debugging sections if we are stripping debugging | |
2496 | information. */ | |
2497 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2498 | && (flags & SEC_DEBUGGING) != 0) | |
2499 | discard = TRUE; | |
2500 | ||
2501 | return discard; | |
2502 | } | |
2503 | ||
252b5132 RH |
2504 | /* The wild routines. |
2505 | ||
2506 | These expand statements like *(.text) and foo.o to a list of | |
2507 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2508 | foo.o(.text, .data). */ | |
2509 | ||
252b5132 | 2510 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
b9c361e0 | 2511 | lang_input_section statement which is placed at PTR. */ |
252b5132 RH |
2512 | |
2513 | void | |
1579bae1 AM |
2514 | lang_add_section (lang_statement_list_type *ptr, |
2515 | asection *section, | |
b9c361e0 | 2516 | struct flag_info *sflag_info, |
7b986e99 | 2517 | lang_output_section_statement_type *output) |
252b5132 | 2518 | { |
164e712d | 2519 | flagword flags = section->flags; |
ae17ab41 | 2520 | |
b34976b6 | 2521 | bfd_boolean discard; |
dfa7b0b8 | 2522 | lang_input_section_type *new_section; |
ae17ab41 | 2523 | bfd *abfd = link_info.output_bfd; |
252b5132 | 2524 | |
5b5f4e6f AB |
2525 | /* Is this section one we know should be discarded? */ |
2526 | discard = lang_discard_section_p (section); | |
252b5132 RH |
2527 | |
2528 | /* Discard input sections which are assigned to a section named | |
2529 | DISCARD_SECTION_NAME. */ | |
2530 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
b34976b6 | 2531 | discard = TRUE; |
252b5132 | 2532 | |
252b5132 RH |
2533 | if (discard) |
2534 | { | |
2535 | if (section->output_section == NULL) | |
2536 | { | |
2537 | /* This prevents future calls from assigning this section. */ | |
2538 | section->output_section = bfd_abs_section_ptr; | |
2539 | } | |
2540 | return; | |
2541 | } | |
2542 | ||
ae17ab41 CM |
2543 | if (sflag_info) |
2544 | { | |
b9c361e0 | 2545 | bfd_boolean keep; |
ae17ab41 | 2546 | |
b9c361e0 JL |
2547 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); |
2548 | if (!keep) | |
4724d37e | 2549 | return; |
ae17ab41 CM |
2550 | } |
2551 | ||
dfa7b0b8 AM |
2552 | if (section->output_section != NULL) |
2553 | return; | |
252b5132 | 2554 | |
dfa7b0b8 AM |
2555 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
2556 | to an output section, because we want to be able to include a | |
2557 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2558 | section (I don't know why we want to do this, but we do). | |
2559 | build_link_order in ldwrite.c handles this case by turning | |
2560 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2561 | flags &= ~ SEC_NEVER_LOAD; | |
8423293d | 2562 | |
dfa7b0b8 AM |
2563 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2564 | already been processed. One reason to do this is that on pe | |
2565 | format targets, .text$foo sections go into .text and it's odd | |
2566 | to see .text with SEC_LINK_ONCE set. */ | |
7bdf4127 AB |
2567 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2568 | { | |
2569 | if (link_info.resolve_section_groups) | |
6c19b93b | 2570 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2571 | else |
6c19b93b | 2572 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2573 | } |
2574 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2575 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2576 | |
dfa7b0b8 AM |
2577 | switch (output->sectype) |
2578 | { | |
2579 | case normal_section: | |
2580 | case overlay_section: | |
7b243801 | 2581 | case first_overlay_section: |
dfa7b0b8 AM |
2582 | break; |
2583 | case noalloc_section: | |
2584 | flags &= ~SEC_ALLOC; | |
2585 | break; | |
2586 | case noload_section: | |
2587 | flags &= ~SEC_LOAD; | |
2588 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
2589 | /* Unfortunately GNU ld has managed to evolve two different |
2590 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2591 | alloc, no contents section. All others get a noload, noalloc | |
2592 | section. */ | |
2593 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
2e76e85a | 2594 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2595 | else |
2596 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2597 | break; |
2598 | } | |
252b5132 | 2599 | |
dfa7b0b8 AM |
2600 | if (output->bfd_section == NULL) |
2601 | init_os (output, flags); | |
252b5132 | 2602 | |
dfa7b0b8 AM |
2603 | /* If SEC_READONLY is not set in the input section, then clear |
2604 | it from the output section. */ | |
2605 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
252b5132 | 2606 | |
dfa7b0b8 AM |
2607 | if (output->bfd_section->linker_has_input) |
2608 | { | |
2609 | /* Only set SEC_READONLY flag on the first input section. */ | |
2610 | flags &= ~ SEC_READONLY; | |
252b5132 | 2611 | |
f5fa8ca2 | 2612 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
2613 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
2614 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2615 | || ((flags & SEC_MERGE) != 0 | |
2616 | && output->bfd_section->entsize != section->entsize)) | |
f5fa8ca2 | 2617 | { |
afd7a018 | 2618 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2619 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2620 | } | |
dfa7b0b8 AM |
2621 | } |
2622 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2623 | |
dfa7b0b8 AM |
2624 | if (!output->bfd_section->linker_has_input) |
2625 | { | |
2626 | output->bfd_section->linker_has_input = 1; | |
2627 | /* This must happen after flags have been updated. The output | |
2628 | section may have been created before we saw its first input | |
2629 | section, eg. for a data statement. */ | |
2630 | bfd_init_private_section_data (section->owner, section, | |
2631 | link_info.output_bfd, | |
2632 | output->bfd_section, | |
2633 | &link_info); | |
2634 | if ((flags & SEC_MERGE) != 0) | |
afd7a018 | 2635 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2636 | } |
f5fa8ca2 | 2637 | |
dfa7b0b8 AM |
2638 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
2639 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2640 | { | |
2641 | /* FIXME: This value should really be obtained from the bfd... */ | |
2642 | output->block_value = 128; | |
2643 | } | |
252b5132 | 2644 | |
dfa7b0b8 AM |
2645 | if (section->alignment_power > output->bfd_section->alignment_power) |
2646 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2647 | |
dfa7b0b8 | 2648 | section->output_section = output->bfd_section; |
252b5132 | 2649 | |
5b69e357 | 2650 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
2651 | { |
2652 | asection *s = output->bfd_section->map_tail.s; | |
2653 | output->bfd_section->map_tail.s = section; | |
2654 | section->map_head.s = NULL; | |
2655 | section->map_tail.s = s; | |
2656 | if (s != NULL) | |
2657 | s->map_head.s = section; | |
2658 | else | |
2659 | output->bfd_section->map_head.s = section; | |
252b5132 | 2660 | } |
dfa7b0b8 AM |
2661 | |
2662 | /* Add a section reference to the list. */ | |
2663 | new_section = new_stat (lang_input_section, ptr); | |
2664 | new_section->section = section; | |
252b5132 RH |
2665 | } |
2666 | ||
2667 | /* Handle wildcard sorting. This returns the lang_input_section which | |
2668 | should follow the one we are going to create for SECTION and FILE, | |
2669 | based on the sorting requirements of WILD. It returns NULL if the | |
2670 | new section should just go at the end of the current list. */ | |
2671 | ||
2672 | static lang_statement_union_type * | |
1579bae1 AM |
2673 | wild_sort (lang_wild_statement_type *wild, |
2674 | struct wildcard_list *sec, | |
2675 | lang_input_statement_type *file, | |
2676 | asection *section) | |
252b5132 | 2677 | { |
252b5132 RH |
2678 | lang_statement_union_type *l; |
2679 | ||
bcaa7b3e L |
2680 | if (!wild->filenames_sorted |
2681 | && (sec == NULL || sec->spec.sorted == none)) | |
252b5132 RH |
2682 | return NULL; |
2683 | ||
bba1a0c0 | 2684 | for (l = wild->children.head; l != NULL; l = l->header.next) |
252b5132 RH |
2685 | { |
2686 | lang_input_section_type *ls; | |
2687 | ||
2688 | if (l->header.type != lang_input_section_enum) | |
2689 | continue; | |
2690 | ls = &l->input_section; | |
2691 | ||
2692 | /* Sorting by filename takes precedence over sorting by section | |
afd7a018 | 2693 | name. */ |
252b5132 RH |
2694 | |
2695 | if (wild->filenames_sorted) | |
2696 | { | |
2697 | const char *fn, *ln; | |
b34976b6 | 2698 | bfd_boolean fa, la; |
252b5132 RH |
2699 | int i; |
2700 | ||
2701 | /* The PE support for the .idata section as generated by | |
afd7a018 AM |
2702 | dlltool assumes that files will be sorted by the name of |
2703 | the archive and then the name of the file within the | |
2704 | archive. */ | |
252b5132 RH |
2705 | |
2706 | if (file->the_bfd != NULL | |
3860d2b4 | 2707 | && file->the_bfd->my_archive != NULL) |
252b5132 | 2708 | { |
3860d2b4 | 2709 | fn = bfd_get_filename (file->the_bfd->my_archive); |
b34976b6 | 2710 | fa = TRUE; |
252b5132 RH |
2711 | } |
2712 | else | |
2713 | { | |
2714 | fn = file->filename; | |
b34976b6 | 2715 | fa = FALSE; |
252b5132 RH |
2716 | } |
2717 | ||
3860d2b4 | 2718 | if (ls->section->owner->my_archive != NULL) |
252b5132 | 2719 | { |
3860d2b4 | 2720 | ln = bfd_get_filename (ls->section->owner->my_archive); |
b34976b6 | 2721 | la = TRUE; |
252b5132 RH |
2722 | } |
2723 | else | |
2724 | { | |
7b986e99 | 2725 | ln = ls->section->owner->filename; |
b34976b6 | 2726 | la = FALSE; |
252b5132 RH |
2727 | } |
2728 | ||
42627821 | 2729 | i = filename_cmp (fn, ln); |
252b5132 RH |
2730 | if (i > 0) |
2731 | continue; | |
2732 | else if (i < 0) | |
2733 | break; | |
2734 | ||
2735 | if (fa || la) | |
2736 | { | |
2737 | if (fa) | |
2738 | fn = file->filename; | |
2739 | if (la) | |
7b986e99 | 2740 | ln = ls->section->owner->filename; |
252b5132 | 2741 | |
42627821 | 2742 | i = filename_cmp (fn, ln); |
252b5132 RH |
2743 | if (i > 0) |
2744 | continue; | |
2745 | else if (i < 0) | |
2746 | break; | |
2747 | } | |
2748 | } | |
2749 | ||
2750 | /* Here either the files are not sorted by name, or we are | |
afd7a018 | 2751 | looking at the sections for this file. */ |
252b5132 | 2752 | |
b2e4da5a L |
2753 | if (sec != NULL |
2754 | && sec->spec.sorted != none | |
2755 | && sec->spec.sorted != by_none) | |
32124d5b AM |
2756 | if (compare_section (sec->spec.sorted, section, ls->section) < 0) |
2757 | break; | |
252b5132 RH |
2758 | } |
2759 | ||
2760 | return l; | |
2761 | } | |
2762 | ||
2763 | /* Expand a wild statement for a particular FILE. SECTION may be | |
2764 | NULL, in which case it is a wild card. */ | |
2765 | ||
2766 | static void | |
1579bae1 AM |
2767 | output_section_callback (lang_wild_statement_type *ptr, |
2768 | struct wildcard_list *sec, | |
2769 | asection *section, | |
b9c361e0 | 2770 | struct flag_info *sflag_info, |
1579bae1 AM |
2771 | lang_input_statement_type *file, |
2772 | void *output) | |
4dec4d4e RH |
2773 | { |
2774 | lang_statement_union_type *before; | |
d0bf826b AM |
2775 | lang_output_section_statement_type *os; |
2776 | ||
2777 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2778 | |
b6bf44ba | 2779 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2780 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2781 | return; |
2782 | ||
b6bf44ba | 2783 | before = wild_sort (ptr, sec, file, section); |
5f992e62 | 2784 | |
4dec4d4e RH |
2785 | /* Here BEFORE points to the lang_input_section which |
2786 | should follow the one we are about to add. If BEFORE | |
2787 | is NULL, then the section should just go at the end | |
2788 | of the current list. */ | |
5f992e62 | 2789 | |
4dec4d4e | 2790 | if (before == NULL) |
b9c361e0 | 2791 | lang_add_section (&ptr->children, section, sflag_info, os); |
4dec4d4e | 2792 | else |
252b5132 | 2793 | { |
4dec4d4e RH |
2794 | lang_statement_list_type list; |
2795 | lang_statement_union_type **pp; | |
5f992e62 | 2796 | |
4dec4d4e | 2797 | lang_list_init (&list); |
b9c361e0 | 2798 | lang_add_section (&list, section, sflag_info, os); |
5f992e62 | 2799 | |
4dec4d4e RH |
2800 | /* If we are discarding the section, LIST.HEAD will |
2801 | be NULL. */ | |
2802 | if (list.head != NULL) | |
252b5132 | 2803 | { |
bba1a0c0 | 2804 | ASSERT (list.head->header.next == NULL); |
5f992e62 | 2805 | |
4dec4d4e RH |
2806 | for (pp = &ptr->children.head; |
2807 | *pp != before; | |
bba1a0c0 | 2808 | pp = &(*pp)->header.next) |
4dec4d4e | 2809 | ASSERT (*pp != NULL); |
5f992e62 | 2810 | |
bba1a0c0 | 2811 | list.head->header.next = *pp; |
4dec4d4e | 2812 | *pp = list.head; |
252b5132 RH |
2813 | } |
2814 | } | |
2815 | } | |
2816 | ||
0841712e JJ |
2817 | /* Check if all sections in a wild statement for a particular FILE |
2818 | are readonly. */ | |
2819 | ||
2820 | static void | |
2821 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2822 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2823 | asection *section, | |
b9c361e0 | 2824 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
0841712e | 2825 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
d0bf826b | 2826 | void *output) |
0841712e | 2827 | { |
d0bf826b AM |
2828 | lang_output_section_statement_type *os; |
2829 | ||
2830 | os = (lang_output_section_statement_type *) output; | |
2831 | ||
0841712e | 2832 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2833 | if (unique_section_p (section, os)) |
0841712e JJ |
2834 | return; |
2835 | ||
6feb9908 | 2836 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
d0bf826b | 2837 | os->all_input_readonly = FALSE; |
0841712e JJ |
2838 | } |
2839 | ||
252b5132 RH |
2840 | /* This is passed a file name which must have been seen already and |
2841 | added to the statement tree. We will see if it has been opened | |
2842 | already and had its symbols read. If not then we'll read it. */ | |
2843 | ||
2844 | static lang_input_statement_type * | |
1579bae1 | 2845 | lookup_name (const char *name) |
252b5132 RH |
2846 | { |
2847 | lang_input_statement_type *search; | |
2848 | ||
36983a93 | 2849 | for (search = &input_file_chain.head->input_statement; |
1579bae1 | 2850 | search != NULL; |
36983a93 | 2851 | search = search->next_real_file) |
252b5132 | 2852 | { |
0013291d NC |
2853 | /* Use the local_sym_name as the name of the file that has |
2854 | already been loaded as filename might have been transformed | |
2855 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2856 | const char *filename = search->local_sym_name; |
0013291d | 2857 | |
0013291d | 2858 | if (filename != NULL |
42627821 | 2859 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2860 | break; |
2861 | } | |
2862 | ||
1579bae1 | 2863 | if (search == NULL) |
1f1f5b92 AM |
2864 | { |
2865 | /* Arrange to splice the input statement added by new_afile into | |
2866 | statement_list after the current input_file_chain tail. | |
2867 | We know input_file_chain is not an empty list, and that | |
2868 | lookup_name was called via open_input_bfds. Later calls to | |
2869 | lookup_name should always match an existing input_statement. */ | |
2870 | lang_statement_union_type **tail = stat_ptr->tail; | |
2871 | lang_statement_union_type **after | |
2872 | = (void *) ((char *) input_file_chain.tail | |
2873 | - offsetof (lang_input_statement_type, next_real_file) | |
2874 | + offsetof (lang_input_statement_type, header.next)); | |
2875 | lang_statement_union_type *rest = *after; | |
2876 | stat_ptr->tail = after; | |
2877 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
2878 | default_target); | |
2879 | *stat_ptr->tail = rest; | |
2880 | if (*tail == NULL) | |
2881 | stat_ptr->tail = tail; | |
2882 | } | |
252b5132 RH |
2883 | |
2884 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 2885 | don't add this file. */ |
66be1055 | 2886 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
2887 | return search; |
2888 | ||
0aa7f586 | 2889 | if (!load_symbols (search, NULL)) |
6770ec8c | 2890 | return NULL; |
252b5132 RH |
2891 | |
2892 | return search; | |
2893 | } | |
2894 | ||
b58f81ae DJ |
2895 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2896 | ||
2897 | struct excluded_lib | |
2898 | { | |
2899 | char *name; | |
2900 | struct excluded_lib *next; | |
2901 | }; | |
2902 | static struct excluded_lib *excluded_libs; | |
2903 | ||
2904 | void | |
2905 | add_excluded_libs (const char *list) | |
2906 | { | |
2907 | const char *p = list, *end; | |
2908 | ||
2909 | while (*p != '\0') | |
2910 | { | |
2911 | struct excluded_lib *entry; | |
2912 | end = strpbrk (p, ",:"); | |
2913 | if (end == NULL) | |
2914 | end = p + strlen (p); | |
1e9cc1c2 | 2915 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
b58f81ae | 2916 | entry->next = excluded_libs; |
1e9cc1c2 | 2917 | entry->name = (char *) xmalloc (end - p + 1); |
b58f81ae DJ |
2918 | memcpy (entry->name, p, end - p); |
2919 | entry->name[end - p] = '\0'; | |
2920 | excluded_libs = entry; | |
2921 | if (*end == '\0') | |
329c1c86 | 2922 | break; |
b58f81ae DJ |
2923 | p = end + 1; |
2924 | } | |
2925 | } | |
2926 | ||
2927 | static void | |
2928 | check_excluded_libs (bfd *abfd) | |
2929 | { | |
2930 | struct excluded_lib *lib = excluded_libs; | |
2931 | ||
2932 | while (lib) | |
2933 | { | |
2934 | int len = strlen (lib->name); | |
2935 | const char *filename = lbasename (abfd->filename); | |
2936 | ||
2937 | if (strcmp (lib->name, "ALL") == 0) | |
2938 | { | |
2939 | abfd->no_export = TRUE; | |
2940 | return; | |
2941 | } | |
2942 | ||
42627821 | 2943 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
2944 | && (filename[len] == '\0' |
2945 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
2946 | && filename[len + 2] == '\0'))) | |
2947 | { | |
2948 | abfd->no_export = TRUE; | |
2949 | return; | |
2950 | } | |
2951 | ||
2952 | lib = lib->next; | |
2953 | } | |
2954 | } | |
2955 | ||
252b5132 RH |
2956 | /* Get the symbols for an input file. */ |
2957 | ||
e9f53129 | 2958 | bfd_boolean |
1579bae1 AM |
2959 | load_symbols (lang_input_statement_type *entry, |
2960 | lang_statement_list_type *place) | |
252b5132 RH |
2961 | { |
2962 | char **matching; | |
2963 | ||
66be1055 | 2964 | if (entry->flags.loaded) |
b34976b6 | 2965 | return TRUE; |
252b5132 RH |
2966 | |
2967 | ldfile_open_file (entry); | |
2968 | ||
c4b78195 | 2969 | /* Do not process further if the file was missing. */ |
66be1055 | 2970 | if (entry->flags.missing_file) |
c4b78195 NC |
2971 | return TRUE; |
2972 | ||
727a29ba AM |
2973 | if (trace_files || verbose) |
2974 | info_msg ("%pI\n", entry); | |
2975 | ||
0aa7f586 AM |
2976 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
2977 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
2978 | { |
2979 | bfd_error_type err; | |
66be1055 | 2980 | struct lang_input_statement_flags save_flags; |
47e2e729 | 2981 | extern FILE *yyin; |
b7a26f91 | 2982 | |
252b5132 | 2983 | err = bfd_get_error (); |
884fb58e NC |
2984 | |
2985 | /* See if the emulation has some special knowledge. */ | |
2986 | if (ldemul_unrecognized_file (entry)) | |
b34976b6 | 2987 | return TRUE; |
884fb58e | 2988 | |
252b5132 RH |
2989 | if (err == bfd_error_file_ambiguously_recognized) |
2990 | { | |
2991 | char **p; | |
2992 | ||
df5f2391 AM |
2993 | einfo (_("%P: %pB: file not recognized: %E;" |
2994 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
2995 | for (p = matching; *p != NULL; p++) |
2996 | einfo (" %s", *p); | |
2997 | einfo ("%F\n"); | |
2998 | } | |
2999 | else if (err != bfd_error_file_not_recognized | |
3000 | || place == NULL) | |
df5f2391 | 3001 | einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 3002 | |
252b5132 RH |
3003 | bfd_close (entry->the_bfd); |
3004 | entry->the_bfd = NULL; | |
3005 | ||
252b5132 | 3006 | /* Try to interpret the file as a linker script. */ |
66be1055 | 3007 | save_flags = input_flags; |
252b5132 RH |
3008 | ldfile_open_command_file (entry->filename); |
3009 | ||
bde18da4 | 3010 | push_stat_ptr (place); |
66be1055 AM |
3011 | input_flags.add_DT_NEEDED_for_regular |
3012 | = entry->flags.add_DT_NEEDED_for_regular; | |
3013 | input_flags.add_DT_NEEDED_for_dynamic | |
3014 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
3015 | input_flags.whole_archive = entry->flags.whole_archive; | |
3016 | input_flags.dynamic = entry->flags.dynamic; | |
252b5132 | 3017 | |
b34976b6 | 3018 | ldfile_assumed_script = TRUE; |
252b5132 RH |
3019 | parser_input = input_script; |
3020 | yyparse (); | |
b34976b6 | 3021 | ldfile_assumed_script = FALSE; |
252b5132 | 3022 | |
f4a23d42 AM |
3023 | /* missing_file is sticky. sysrooted will already have been |
3024 | restored when seeing EOF in yyparse, but no harm to restore | |
3025 | again. */ | |
66be1055 AM |
3026 | save_flags.missing_file |= input_flags.missing_file; |
3027 | input_flags = save_flags; | |
bde18da4 | 3028 | pop_stat_ptr (); |
47e2e729 AM |
3029 | fclose (yyin); |
3030 | yyin = NULL; | |
3031 | entry->flags.loaded = TRUE; | |
252b5132 | 3032 | |
bde18da4 | 3033 | return TRUE; |
252b5132 RH |
3034 | } |
3035 | ||
3036 | if (ldemul_recognized_file (entry)) | |
b34976b6 | 3037 | return TRUE; |
252b5132 RH |
3038 | |
3039 | /* We don't call ldlang_add_file for an archive. Instead, the | |
3040 | add_symbols entry point will call ldlang_add_file, via the | |
3041 | add_archive_element callback, for each element of the archive | |
3042 | which is used. */ | |
3043 | switch (bfd_get_format (entry->the_bfd)) | |
3044 | { | |
3045 | default: | |
3046 | break; | |
3047 | ||
3048 | case bfd_object: | |
66be1055 | 3049 | if (!entry->flags.reload) |
15fc2e13 | 3050 | ldlang_add_file (entry); |
252b5132 RH |
3051 | break; |
3052 | ||
3053 | case bfd_archive: | |
b58f81ae DJ |
3054 | check_excluded_libs (entry->the_bfd); |
3055 | ||
00f93c44 | 3056 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 3057 | if (entry->flags.whole_archive) |
252b5132 | 3058 | { |
b7a26f91 | 3059 | bfd *member = NULL; |
b34976b6 | 3060 | bfd_boolean loaded = TRUE; |
6770ec8c NC |
3061 | |
3062 | for (;;) | |
252b5132 | 3063 | { |
5d3236ee | 3064 | bfd *subsbfd; |
6770ec8c NC |
3065 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
3066 | ||
3067 | if (member == NULL) | |
3068 | break; | |
b7a26f91 | 3069 | |
0aa7f586 | 3070 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 3071 | { |
df5f2391 | 3072 | einfo (_("%F%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 3073 | entry->the_bfd, member); |
b34976b6 | 3074 | loaded = FALSE; |
6770ec8c NC |
3075 | } |
3076 | ||
db0ca79f | 3077 | subsbfd = member; |
46105645 AM |
3078 | if (!(*link_info.callbacks |
3079 | ->add_archive_element) (&link_info, member, | |
3080 | "--whole-archive", &subsbfd)) | |
252b5132 | 3081 | abort (); |
6770ec8c | 3082 | |
5d3236ee DK |
3083 | /* Potentially, the add_archive_element hook may have set a |
3084 | substitute BFD for us. */ | |
46105645 | 3085 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 3086 | { |
df5f2391 | 3087 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), member); |
b34976b6 | 3088 | loaded = FALSE; |
6770ec8c | 3089 | } |
252b5132 RH |
3090 | } |
3091 | ||
66be1055 | 3092 | entry->flags.loaded = loaded; |
6770ec8c | 3093 | return loaded; |
252b5132 | 3094 | } |
6770ec8c | 3095 | break; |
252b5132 RH |
3096 | } |
3097 | ||
03bdc404 | 3098 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
66be1055 | 3099 | entry->flags.loaded = TRUE; |
6770ec8c | 3100 | else |
df5f2391 | 3101 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3102 | |
66be1055 | 3103 | return entry->flags.loaded; |
252b5132 RH |
3104 | } |
3105 | ||
b6bf44ba AM |
3106 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
3107 | may be NULL, indicating that it is a wildcard. Separate | |
3108 | lang_input_section statements are created for each part of the | |
3109 | expansion; they are added after the wild statement S. OUTPUT is | |
3110 | the output section. */ | |
252b5132 RH |
3111 | |
3112 | static void | |
1579bae1 AM |
3113 | wild (lang_wild_statement_type *s, |
3114 | const char *target ATTRIBUTE_UNUSED, | |
3115 | lang_output_section_statement_type *output) | |
252b5132 | 3116 | { |
b6bf44ba | 3117 | struct wildcard_list *sec; |
252b5132 | 3118 | |
50c77e5d | 3119 | if (s->handler_data[0] |
329c1c86 | 3120 | && s->handler_data[0]->spec.sorted == by_name |
50c77e5d NC |
3121 | && !s->filenames_sorted) |
3122 | { | |
329c1c86 AM |
3123 | lang_section_bst_type *tree; |
3124 | ||
50c77e5d NC |
3125 | walk_wild (s, output_section_callback_fast, output); |
3126 | ||
e6f2cbf5 | 3127 | tree = s->tree; |
329c1c86 | 3128 | if (tree) |
e6f2cbf5 L |
3129 | { |
3130 | output_section_callback_tree_to_list (s, tree, output); | |
3131 | s->tree = NULL; | |
3132 | } | |
50c77e5d | 3133 | } |
329c1c86 | 3134 | else |
50c77e5d | 3135 | walk_wild (s, output_section_callback, output); |
b6bf44ba | 3136 | |
abe6ac95 AM |
3137 | if (default_common_section == NULL) |
3138 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
3139 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
3140 | { | |
3141 | /* Remember the section that common is going to in case we | |
b7a26f91 | 3142 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3143 | default_common_section = output; |
abe6ac95 | 3144 | break; |
b6bf44ba | 3145 | } |
252b5132 RH |
3146 | } |
3147 | ||
b34976b6 | 3148 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3149 | |
e50d8076 | 3150 | static int |
1579bae1 | 3151 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3152 | { |
1e9cc1c2 | 3153 | const char *sought = (const char *) data; |
5f992e62 | 3154 | |
e50d8076 NC |
3155 | return strcmp (target->name, sought) == 0; |
3156 | } | |
3157 | ||
3158 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3159 | |
e50d8076 | 3160 | static void |
1d38e9d1 | 3161 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3162 | { |
3163 | char c; | |
5f992e62 | 3164 | |
08da4cac | 3165 | while ((c = *src++) != 0) |
3882b010 | 3166 | *dest++ = TOLOWER (c); |
e50d8076 | 3167 | |
08da4cac | 3168 | *dest = 0; |
e50d8076 NC |
3169 | } |
3170 | ||
396a2467 | 3171 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3172 | from haystack. */ |
08da4cac | 3173 | |
e50d8076 | 3174 | static void |
1d38e9d1 | 3175 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3176 | { |
3177 | haystack = strstr (haystack, needle); | |
5f992e62 | 3178 | |
e50d8076 NC |
3179 | if (haystack) |
3180 | { | |
08da4cac | 3181 | char *src; |
e50d8076 | 3182 | |
08da4cac KH |
3183 | for (src = haystack + strlen (needle); *src;) |
3184 | *haystack++ = *src++; | |
5f992e62 | 3185 | |
08da4cac | 3186 | *haystack = 0; |
e50d8076 NC |
3187 | } |
3188 | } | |
3189 | ||
3190 | /* Compare two target format name strings. | |
3191 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3192 | |
e50d8076 | 3193 | static int |
1d38e9d1 | 3194 | name_compare (const char *first, const char *second) |
e50d8076 | 3195 | { |
08da4cac KH |
3196 | char *copy1; |
3197 | char *copy2; | |
3198 | int result; | |
5f992e62 | 3199 | |
1e9cc1c2 NC |
3200 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3201 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3202 | |
3203 | /* Convert the names to lower case. */ | |
3204 | stricpy (copy1, first); | |
3205 | stricpy (copy2, second); | |
3206 | ||
1579bae1 | 3207 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
3208 | strcut (copy1, "big"); |
3209 | strcut (copy1, "little"); | |
3210 | strcut (copy2, "big"); | |
3211 | strcut (copy2, "little"); | |
3212 | ||
3213 | /* Return a value based on how many characters match, | |
3214 | starting from the beginning. If both strings are | |
3215 | the same then return 10 * their length. */ | |
08da4cac KH |
3216 | for (result = 0; copy1[result] == copy2[result]; result++) |
3217 | if (copy1[result] == 0) | |
e50d8076 NC |
3218 | { |
3219 | result *= 10; | |
3220 | break; | |
3221 | } | |
5f992e62 | 3222 | |
e50d8076 NC |
3223 | free (copy1); |
3224 | free (copy2); | |
3225 | ||
3226 | return result; | |
3227 | } | |
3228 | ||
3229 | /* Set by closest_target_match() below. */ | |
08da4cac | 3230 | static const bfd_target *winner; |
e50d8076 NC |
3231 | |
3232 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3233 | requirement that was specified on the command line, and is the nearest | |
3234 | match to the original output target. */ | |
08da4cac | 3235 | |
e50d8076 | 3236 | static int |
1579bae1 | 3237 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3238 | { |
1e9cc1c2 | 3239 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3240 | |
08da4cac KH |
3241 | if (command_line.endian == ENDIAN_BIG |
3242 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3243 | return 0; |
5f992e62 | 3244 | |
08da4cac KH |
3245 | if (command_line.endian == ENDIAN_LITTLE |
3246 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3247 | return 0; |
3248 | ||
3249 | /* Must be the same flavour. */ | |
3250 | if (target->flavour != original->flavour) | |
3251 | return 0; | |
3252 | ||
de7dd2bd | 3253 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3254 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
3255 | || strcmp (target->name, "elf64-big") == 0 |
3256 | || strcmp (target->name, "elf32-little") == 0 | |
3257 | || strcmp (target->name, "elf64-little") == 0) | |
3258 | return 0; | |
3259 | ||
e50d8076 NC |
3260 | /* If we have not found a potential winner yet, then record this one. */ |
3261 | if (winner == NULL) | |
3262 | { | |
3263 | winner = target; | |
3264 | return 0; | |
3265 | } | |
3266 | ||
3267 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 3268 | Compare their names and choose the better one. */ |
d1778b88 AM |
3269 | if (name_compare (target->name, original->name) |
3270 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
3271 | winner = target; |
3272 | ||
3273 | /* Keep on searching until wqe have checked them all. */ | |
3274 | return 0; | |
3275 | } | |
3276 | ||
3277 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 3278 | |
1d38e9d1 | 3279 | static const char * |
1579bae1 | 3280 | get_first_input_target (void) |
e50d8076 | 3281 | { |
1d38e9d1 | 3282 | const char *target = NULL; |
e50d8076 NC |
3283 | |
3284 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3285 | { | |
3286 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3287 | && s->flags.real) |
e50d8076 NC |
3288 | { |
3289 | ldfile_open_file (s); | |
5f992e62 | 3290 | |
e50d8076 NC |
3291 | if (s->the_bfd != NULL |
3292 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3293 | { | |
3294 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3295 | |
e50d8076 NC |
3296 | if (target != NULL) |
3297 | break; | |
3298 | } | |
3299 | } | |
3300 | } | |
5f992e62 | 3301 | |
e50d8076 NC |
3302 | return target; |
3303 | } | |
3304 | ||
599917b8 | 3305 | const char * |
1579bae1 | 3306 | lang_get_output_target (void) |
599917b8 JJ |
3307 | { |
3308 | const char *target; | |
3309 | ||
3310 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3311 | if (output_target != NULL) |
599917b8 JJ |
3312 | return output_target; |
3313 | ||
3314 | /* No - has the current target been set to something other than | |
3315 | the default? */ | |
30824704 | 3316 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
3317 | return current_target; |
3318 | ||
3319 | /* No - can we determine the format of the first input file? */ | |
3320 | target = get_first_input_target (); | |
3321 | if (target != NULL) | |
3322 | return target; | |
3323 | ||
3324 | /* Failed - use the default output target. */ | |
3325 | return default_target; | |
3326 | } | |
3327 | ||
252b5132 RH |
3328 | /* Open the output file. */ |
3329 | ||
f13a99db | 3330 | static void |
1579bae1 | 3331 | open_output (const char *name) |
252b5132 | 3332 | { |
599917b8 | 3333 | output_target = lang_get_output_target (); |
5f992e62 | 3334 | |
08da4cac KH |
3335 | /* Has the user requested a particular endianness on the command |
3336 | line? */ | |
e50d8076 NC |
3337 | if (command_line.endian != ENDIAN_UNSET) |
3338 | { | |
e50d8076 | 3339 | /* Get the chosen target. */ |
4212b42d AM |
3340 | const bfd_target *target |
3341 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3342 | |
c13b1b77 NC |
3343 | /* If the target is not supported, we cannot do anything. */ |
3344 | if (target != NULL) | |
e50d8076 | 3345 | { |
4212b42d AM |
3346 | enum bfd_endian desired_endian; |
3347 | ||
c13b1b77 NC |
3348 | if (command_line.endian == ENDIAN_BIG) |
3349 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3350 | else |
c13b1b77 | 3351 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
3352 | |
3353 | /* See if the target has the wrong endianness. This should | |
3354 | not happen if the linker script has provided big and | |
3355 | little endian alternatives, but some scrips don't do | |
3356 | this. */ | |
c13b1b77 | 3357 | if (target->byteorder != desired_endian) |
e50d8076 | 3358 | { |
c13b1b77 NC |
3359 | /* If it does, then see if the target provides |
3360 | an alternative with the correct endianness. */ | |
3361 | if (target->alternative_target != NULL | |
3362 | && (target->alternative_target->byteorder == desired_endian)) | |
3363 | output_target = target->alternative_target->name; | |
e50d8076 | 3364 | else |
c13b1b77 | 3365 | { |
5f992e62 AM |
3366 | /* Try to find a target as similar as possible to |
3367 | the default target, but which has the desired | |
3368 | endian characteristic. */ | |
4212b42d AM |
3369 | bfd_iterate_over_targets (closest_target_match, |
3370 | (void *) target); | |
5f992e62 AM |
3371 | |
3372 | /* Oh dear - we could not find any targets that | |
3373 | satisfy our requirements. */ | |
c13b1b77 | 3374 | if (winner == NULL) |
6feb9908 AM |
3375 | einfo (_("%P: warning: could not find any targets" |
3376 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3377 | else |
3378 | output_target = winner->name; | |
3379 | } | |
e50d8076 NC |
3380 | } |
3381 | } | |
252b5132 | 3382 | } |
5f992e62 | 3383 | |
f13a99db | 3384 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3385 | |
f13a99db | 3386 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3387 | { |
3388 | if (bfd_get_error () == bfd_error_invalid_target) | |
df5f2391 | 3389 | einfo (_("%F%P: target %s not found\n"), output_target); |
e50d8076 | 3390 | |
df5f2391 | 3391 | einfo (_("%F%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3392 | } |
3393 | ||
b34976b6 | 3394 | delete_output_file_on_failure = TRUE; |
252b5132 | 3395 | |
0aa7f586 | 3396 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
df5f2391 | 3397 | einfo (_("%F%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3398 | if (!bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
3399 | ldfile_output_architecture, |
3400 | ldfile_output_machine)) | |
df5f2391 | 3401 | einfo (_("%F%P: %s: can not set architecture: %E\n"), name); |
252b5132 | 3402 | |
f13a99db | 3403 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3404 | if (link_info.hash == NULL) |
df5f2391 | 3405 | einfo (_("%F%P: can not create hash table: %E\n")); |
252b5132 | 3406 | |
f13a99db | 3407 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3408 | } |
3409 | ||
252b5132 | 3410 | static void |
1579bae1 | 3411 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3412 | { |
3413 | switch (statement->header.type) | |
3414 | { | |
3415 | case lang_output_statement_enum: | |
f13a99db AM |
3416 | ASSERT (link_info.output_bfd == NULL); |
3417 | open_output (statement->output_statement.name); | |
252b5132 | 3418 | ldemul_set_output_arch (); |
0e1862bb L |
3419 | if (config.magic_demand_paged |
3420 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3421 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3422 | else |
f13a99db | 3423 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3424 | if (config.text_read_only) |
f13a99db | 3425 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3426 | else |
f13a99db | 3427 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3428 | if (link_info.traditional_format) |
f13a99db | 3429 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3430 | else |
f13a99db | 3431 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
3432 | break; |
3433 | ||
3434 | case lang_target_statement_enum: | |
3435 | current_target = statement->target_statement.target; | |
3436 | break; | |
3437 | default: | |
3438 | break; | |
3439 | } | |
3440 | } | |
3441 | ||
e5caa5e0 AM |
3442 | static void |
3443 | init_opb (void) | |
3444 | { | |
3445 | unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3446 | ldfile_output_machine); | |
3447 | opb_shift = 0; | |
3448 | if (x > 1) | |
3449 | while ((x & 1) == 0) | |
3450 | { | |
3451 | x >>= 1; | |
3452 | ++opb_shift; | |
3453 | } | |
3454 | ASSERT (x == 1); | |
3455 | } | |
3456 | ||
252b5132 RH |
3457 | /* Open all the input files. */ |
3458 | ||
486329aa AM |
3459 | enum open_bfd_mode |
3460 | { | |
3461 | OPEN_BFD_NORMAL = 0, | |
3462 | OPEN_BFD_FORCE = 1, | |
3463 | OPEN_BFD_RESCAN = 2 | |
3464 | }; | |
9e2278f5 AM |
3465 | #ifdef ENABLE_PLUGINS |
3466 | static lang_input_statement_type *plugin_insert = NULL; | |
b02c4f16 | 3467 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3468 | #endif |
486329aa | 3469 | |
252b5132 | 3470 | static void |
486329aa | 3471 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
252b5132 | 3472 | { |
1579bae1 | 3473 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3474 | { |
3475 | switch (s->header.type) | |
3476 | { | |
3477 | case lang_constructors_statement_enum: | |
486329aa | 3478 | open_input_bfds (constructor_list.head, mode); |
252b5132 RH |
3479 | break; |
3480 | case lang_output_section_statement_enum: | |
486329aa | 3481 | open_input_bfds (s->output_section_statement.children.head, mode); |
252b5132 RH |
3482 | break; |
3483 | case lang_wild_statement_enum: | |
08da4cac | 3484 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3485 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3486 | && s->wild_statement.filename | |
97407faf AM |
3487 | && !wildcardp (s->wild_statement.filename) |
3488 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3489 | lookup_name (s->wild_statement.filename); |
486329aa | 3490 | open_input_bfds (s->wild_statement.children.head, mode); |
252b5132 RH |
3491 | break; |
3492 | case lang_group_statement_enum: | |
3493 | { | |
3494 | struct bfd_link_hash_entry *undefs; | |
b02c4f16 AM |
3495 | #ifdef ENABLE_PLUGINS |
3496 | lang_input_statement_type *plugin_insert_save; | |
3497 | #endif | |
252b5132 RH |
3498 | |
3499 | /* We must continually search the entries in the group | |
08da4cac KH |
3500 | until no new symbols are added to the list of undefined |
3501 | symbols. */ | |
252b5132 RH |
3502 | |
3503 | do | |
3504 | { | |
b02c4f16 AM |
3505 | #ifdef ENABLE_PLUGINS |
3506 | plugin_insert_save = plugin_insert; | |
3507 | #endif | |
252b5132 | 3508 | undefs = link_info.hash->undefs_tail; |
486329aa AM |
3509 | open_input_bfds (s->group_statement.children.head, |
3510 | mode | OPEN_BFD_FORCE); | |
252b5132 | 3511 | } |
b02c4f16 AM |
3512 | while (undefs != link_info.hash->undefs_tail |
3513 | #ifdef ENABLE_PLUGINS | |
3514 | /* Objects inserted by a plugin, which are loaded | |
3515 | before we hit this loop, may have added new | |
3516 | undefs. */ | |
3517 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3518 | #endif | |
3519 | ); | |
252b5132 RH |
3520 | } |
3521 | break; | |
3522 | case lang_target_statement_enum: | |
3523 | current_target = s->target_statement.target; | |
3524 | break; | |
3525 | case lang_input_statement_enum: | |
66be1055 | 3526 | if (s->input_statement.flags.real) |
252b5132 | 3527 | { |
bde18da4 | 3528 | lang_statement_union_type **os_tail; |
252b5132 | 3529 | lang_statement_list_type add; |
d215621e | 3530 | bfd *abfd; |
252b5132 RH |
3531 | |
3532 | s->input_statement.target = current_target; | |
3533 | ||
3534 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3535 | is an archive which has already been searched, then |
3536 | force it to be researched unless the whole archive | |
d215621e AM |
3537 | has been loaded already. Do the same for a rescan. |
3538 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3539 | if (mode != OPEN_BFD_NORMAL |
9e2278f5 AM |
3540 | #ifdef ENABLE_PLUGINS |
3541 | && ((mode & OPEN_BFD_RESCAN) == 0 | |
3542 | || plugin_insert == NULL) | |
3543 | #endif | |
66be1055 | 3544 | && s->input_statement.flags.loaded |
d215621e AM |
3545 | && (abfd = s->input_statement.the_bfd) != NULL |
3546 | && ((bfd_get_format (abfd) == bfd_archive | |
3547 | && !s->input_statement.flags.whole_archive) | |
3548 | || (bfd_get_format (abfd) == bfd_object | |
3549 | && ((abfd->flags) & DYNAMIC) != 0 | |
3550 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
e77620a5 | 3551 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3552 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3553 | { |
66be1055 AM |
3554 | s->input_statement.flags.loaded = FALSE; |
3555 | s->input_statement.flags.reload = TRUE; | |
15fc2e13 | 3556 | } |
252b5132 | 3557 | |
5c1e6d53 | 3558 | os_tail = lang_os_list.tail; |
d1778b88 | 3559 | lang_list_init (&add); |
1276aefa | 3560 | |
0aa7f586 | 3561 | if (!load_symbols (&s->input_statement, &add)) |
b34976b6 | 3562 | config.make_executable = FALSE; |
252b5132 RH |
3563 | |
3564 | if (add.head != NULL) | |
3565 | { | |
bde18da4 AM |
3566 | /* If this was a script with output sections then |
3567 | tack any added statements on to the end of the | |
3568 | list. This avoids having to reorder the output | |
3569 | section statement list. Very likely the user | |
3570 | forgot -T, and whatever we do here will not meet | |
3571 | naive user expectations. */ | |
5c1e6d53 | 3572 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
3573 | { |
3574 | einfo (_("%P: warning: %s contains output sections;" | |
3575 | " did you forget -T?\n"), | |
3576 | s->input_statement.filename); | |
3577 | *stat_ptr->tail = add.head; | |
3578 | stat_ptr->tail = add.tail; | |
3579 | } | |
3580 | else | |
3581 | { | |
3582 | *add.tail = s->header.next; | |
3583 | s->header.next = add.head; | |
3584 | } | |
252b5132 RH |
3585 | } |
3586 | } | |
9e2278f5 AM |
3587 | #ifdef ENABLE_PLUGINS |
3588 | /* If we have found the point at which a plugin added new | |
3589 | files, clear plugin_insert to enable archive rescan. */ | |
3590 | if (&s->input_statement == plugin_insert) | |
3591 | plugin_insert = NULL; | |
3592 | #endif | |
252b5132 | 3593 | break; |
e759c116 | 3594 | case lang_assignment_statement_enum: |
165f707a | 3595 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
01554a74 | 3596 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
e759c116 | 3597 | break; |
252b5132 RH |
3598 | default: |
3599 | break; | |
3600 | } | |
3601 | } | |
c4b78195 NC |
3602 | |
3603 | /* Exit if any of the files were missing. */ | |
66be1055 | 3604 | if (input_flags.missing_file) |
c4b78195 | 3605 | einfo ("%F"); |
252b5132 RH |
3606 | } |
3607 | ||
1ff6de03 NA |
3608 | /* Open the CTF sections in the input files with libctf: if any were opened, |
3609 | create a fake input file that we'll write the merged CTF data to later | |
3610 | on. */ | |
3611 | ||
3612 | static void | |
3613 | ldlang_open_ctf (void) | |
3614 | { | |
3615 | int any_ctf = 0; | |
3616 | int err; | |
3617 | ||
3618 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3619 | { | |
3620 | asection *sect; | |
3621 | ||
3622 | /* Incoming files from the compiler have a single ctf_file_t in them | |
3623 | (which is presented to us by the libctf API in a ctf_archive_t | |
3624 | wrapper): files derived from a previous relocatable link have a CTF | |
3625 | archive containing possibly many CTF files. */ | |
3626 | ||
3627 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3628 | { | |
3629 | if (err != ECTF_NOCTFDATA) | |
3630 | einfo (_("%P: warning: CTF section in `%pI' not loaded: " | |
3631 | "its types will be discarded: `%s'\n"), file, | |
3632 | ctf_errmsg (err)); | |
3633 | continue; | |
3634 | } | |
3635 | ||
3636 | /* Prevent the contents of this section from being written, while | |
3637 | requiring the section itself to be duplicated in the output. */ | |
3638 | /* This section must exist if ctf_bfdopen() succeeded. */ | |
3639 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3640 | sect->size = 0; | |
3641 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3642 | ||
3643 | any_ctf = 1; | |
3644 | } | |
3645 | ||
3646 | if (!any_ctf) | |
3647 | { | |
3648 | ctf_output = NULL; | |
3649 | return; | |
3650 | } | |
3651 | ||
3652 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3653 | return; | |
3654 | ||
3655 | einfo (_("%P: warning: CTF output not created: `s'\n"), | |
3656 | ctf_errmsg (err)); | |
3657 | ||
3658 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3659 | ctf_close (errfile->the_ctf); | |
3660 | } | |
3661 | ||
3662 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3663 | function and data object sections need adjustment. */ | |
3664 | ||
3665 | static void | |
3666 | lang_merge_ctf (void) | |
3667 | { | |
3668 | asection *output_sect; | |
3669 | ||
3670 | if (!ctf_output) | |
3671 | return; | |
3672 | ||
3673 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3674 | ||
3675 | /* If the section was discarded, don't waste time merging. */ | |
3676 | if (output_sect == NULL) | |
3677 | { | |
3678 | ctf_file_close (ctf_output); | |
3679 | ctf_output = NULL; | |
3680 | ||
3681 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3682 | { | |
3683 | ctf_close (file->the_ctf); | |
3684 | file->the_ctf = NULL; | |
3685 | } | |
3686 | return; | |
3687 | } | |
3688 | ||
3689 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3690 | { | |
3691 | if (!file->the_ctf) | |
3692 | continue; | |
3693 | ||
3694 | /* Takes ownership of file->u.the_ctfa. */ | |
3695 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) | |
3696 | { | |
3697 | einfo (_("%F%P: cannot link with CTF in %pB: %s\n"), file->the_bfd, | |
3698 | ctf_errmsg (ctf_errno (ctf_output))); | |
3699 | ctf_close (file->the_ctf); | |
3700 | file->the_ctf = NULL; | |
3701 | continue; | |
3702 | } | |
3703 | } | |
3704 | ||
3705 | if (ctf_link (ctf_output, CTF_LINK_SHARE_UNCONFLICTED) < 0) | |
3706 | { | |
3707 | einfo (_("%F%P: CTF linking failed; output will have no CTF section: %s\n"), | |
3708 | ctf_errmsg (ctf_errno (ctf_output))); | |
3709 | if (output_sect) | |
3710 | { | |
3711 | output_sect->size = 0; | |
3712 | output_sect->flags |= SEC_EXCLUDE; | |
3713 | } | |
3714 | } | |
3715 | } | |
3716 | ||
3717 | /* Let the emulation examine the symbol table and strtab to help it optimize the | |
3718 | CTF, if supported. */ | |
3719 | ||
3720 | void | |
3721 | ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms, bfd_size_type symcount, | |
3722 | struct elf_strtab_hash *symstrtab) | |
3723 | { | |
3724 | ldemul_examine_strtab_for_ctf (ctf_output, syms, symcount, symstrtab); | |
3725 | } | |
3726 | ||
3727 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3728 | need to dedup against the strtab and symtab, then possibly called from the | |
3729 | target linker code if the dedup has happened. */ | |
3730 | static void | |
3731 | lang_write_ctf (int late) | |
3732 | { | |
3733 | size_t output_size; | |
3734 | asection *output_sect; | |
3735 | ||
3736 | if (!ctf_output) | |
3737 | return; | |
3738 | ||
3739 | if (late) | |
3740 | { | |
3741 | /* Emit CTF late if this emulation says it can do so. */ | |
3742 | if (ldemul_emit_ctf_early ()) | |
3743 | return; | |
3744 | } | |
3745 | else | |
3746 | { | |
3747 | if (!ldemul_emit_ctf_early ()) | |
3748 | return; | |
3749 | } | |
3750 | ||
3751 | /* Emit CTF. */ | |
3752 | ||
3753 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3754 | if (output_sect) | |
3755 | { | |
3756 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
3757 | CTF_COMPRESSION_THRESHOLD); | |
3758 | output_sect->size = output_size; | |
3759 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
3760 | ||
3761 | if (!output_sect->contents) | |
3762 | { | |
3763 | einfo (_("%F%P: CTF section emission failed; output will have no " | |
3764 | "CTF section: %s\n"), ctf_errmsg (ctf_errno (ctf_output))); | |
3765 | output_sect->size = 0; | |
3766 | output_sect->flags |= SEC_EXCLUDE; | |
3767 | } | |
3768 | } | |
3769 | ||
3770 | /* This also closes every CTF input file used in the link. */ | |
3771 | ctf_file_close (ctf_output); | |
3772 | ctf_output = NULL; | |
3773 | ||
3774 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3775 | file->the_ctf = NULL; | |
3776 | } | |
3777 | ||
3778 | /* Write out the CTF section late, if the emulation needs that. */ | |
3779 | ||
3780 | void | |
3781 | ldlang_write_ctf_late (void) | |
3782 | { | |
3783 | /* Trigger a "late call", if the emulation needs one. */ | |
3784 | ||
3785 | lang_write_ctf (1); | |
3786 | } | |
3787 | ||
08da4cac | 3788 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3789 | This is a two step process as the symbol table doesn't even exist at |
3790 | the time the ld command line is processed. First we put the name | |
3791 | on a list, then, once the output file has been opened, transfer the | |
3792 | name to the symbol table. */ | |
3793 | ||
e3e942e9 | 3794 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3795 | |
e3e942e9 | 3796 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3797 | |
3798 | void | |
24898b70 | 3799 | ldlang_add_undef (const char *const name, bfd_boolean cmdline) |
252b5132 | 3800 | { |
24898b70 | 3801 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 3802 | |
24898b70 | 3803 | undef_from_cmdline = undef_from_cmdline || cmdline; |
988de25b | 3804 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
3805 | new_undef->next = ldlang_undef_chain_list_head; |
3806 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 3807 | |
d3ce72d0 | 3808 | new_undef->name = xstrdup (name); |
fcf0e35b | 3809 | |
f13a99db | 3810 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 3811 | insert_undefined (new_undef->name); |
fcf0e35b AM |
3812 | } |
3813 | ||
3814 | /* Insert NAME as undefined in the symbol table. */ | |
3815 | ||
3816 | static void | |
1579bae1 | 3817 | insert_undefined (const char *name) |
fcf0e35b AM |
3818 | { |
3819 | struct bfd_link_hash_entry *h; | |
3820 | ||
b34976b6 | 3821 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
1579bae1 | 3822 | if (h == NULL) |
df5f2391 | 3823 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
3824 | if (h->type == bfd_link_hash_new) |
3825 | { | |
3826 | h->type = bfd_link_hash_undefined; | |
3827 | h->u.undef.abfd = NULL; | |
94d401b8 | 3828 | h->non_ir_ref_regular = TRUE; |
80070c0d MR |
3829 | if (is_elf_hash_table (link_info.hash)) |
3830 | ((struct elf_link_hash_entry *) h)->mark = 1; | |
fcf0e35b AM |
3831 | bfd_link_add_undef (link_info.hash, h); |
3832 | } | |
252b5132 RH |
3833 | } |
3834 | ||
3835 | /* Run through the list of undefineds created above and place them | |
3836 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3837 | script file. */ |
3838 | ||
252b5132 | 3839 | static void |
1579bae1 | 3840 | lang_place_undefineds (void) |
252b5132 RH |
3841 | { |
3842 | ldlang_undef_chain_list_type *ptr; | |
3843 | ||
1579bae1 AM |
3844 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3845 | insert_undefined (ptr->name); | |
252b5132 RH |
3846 | } |
3847 | ||
0a618243 AB |
3848 | /* Structure used to build the list of symbols that the user has required |
3849 | be defined. */ | |
3850 | ||
3851 | struct require_defined_symbol | |
3852 | { | |
3853 | const char *name; | |
3854 | struct require_defined_symbol *next; | |
3855 | }; | |
3856 | ||
3857 | /* The list of symbols that the user has required be defined. */ | |
3858 | ||
3859 | static struct require_defined_symbol *require_defined_symbol_list; | |
3860 | ||
3861 | /* Add a new symbol NAME to the list of symbols that are required to be | |
3862 | defined. */ | |
3863 | ||
3864 | void | |
0aa7f586 | 3865 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
3866 | { |
3867 | struct require_defined_symbol *ptr; | |
3868 | ||
3869 | ldlang_add_undef (name, TRUE); | |
988de25b | 3870 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 AB |
3871 | ptr->next = require_defined_symbol_list; |
3872 | ptr->name = strdup (name); | |
3873 | require_defined_symbol_list = ptr; | |
3874 | } | |
3875 | ||
3876 | /* Check that all symbols the user required to be defined, are defined, | |
3877 | raise an error if we find a symbol that is not defined. */ | |
3878 | ||
3879 | static void | |
3880 | ldlang_check_require_defined_symbols (void) | |
3881 | { | |
3882 | struct require_defined_symbol *ptr; | |
3883 | ||
3884 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
3885 | { | |
3886 | struct bfd_link_hash_entry *h; | |
3887 | ||
3888 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
0aa7f586 | 3889 | FALSE, FALSE, TRUE); |
0a618243 | 3890 | if (h == NULL |
0aa7f586 AM |
3891 | || (h->type != bfd_link_hash_defined |
3892 | && h->type != bfd_link_hash_defweak)) | |
df5f2391 | 3893 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
3894 | } |
3895 | } | |
3896 | ||
0841712e JJ |
3897 | /* Check for all readonly or some readwrite sections. */ |
3898 | ||
3899 | static void | |
6feb9908 AM |
3900 | check_input_sections |
3901 | (lang_statement_union_type *s, | |
3902 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 3903 | { |
988de25b | 3904 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
3905 | { |
3906 | switch (s->header.type) | |
967928e9 AM |
3907 | { |
3908 | case lang_wild_statement_enum: | |
3909 | walk_wild (&s->wild_statement, check_section_callback, | |
3910 | output_section_statement); | |
0aa7f586 | 3911 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3912 | return; |
3913 | break; | |
3914 | case lang_constructors_statement_enum: | |
3915 | check_input_sections (constructor_list.head, | |
3916 | output_section_statement); | |
0aa7f586 | 3917 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3918 | return; |
3919 | break; | |
3920 | case lang_group_statement_enum: | |
3921 | check_input_sections (s->group_statement.children.head, | |
3922 | output_section_statement); | |
0aa7f586 | 3923 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3924 | return; |
3925 | break; | |
3926 | default: | |
3927 | break; | |
3928 | } | |
0841712e JJ |
3929 | } |
3930 | } | |
3931 | ||
bcaa7b3e L |
3932 | /* Update wildcard statements if needed. */ |
3933 | ||
3934 | static void | |
3935 | update_wild_statements (lang_statement_union_type *s) | |
3936 | { | |
3937 | struct wildcard_list *sec; | |
3938 | ||
3939 | switch (sort_section) | |
3940 | { | |
3941 | default: | |
3942 | FAIL (); | |
3943 | ||
3944 | case none: | |
3945 | break; | |
3946 | ||
3947 | case by_name: | |
3948 | case by_alignment: | |
3949 | for (; s != NULL; s = s->header.next) | |
3950 | { | |
3951 | switch (s->header.type) | |
3952 | { | |
3953 | default: | |
3954 | break; | |
3955 | ||
3956 | case lang_wild_statement_enum: | |
0d0999db L |
3957 | for (sec = s->wild_statement.section_list; sec != NULL; |
3958 | sec = sec->next) | |
2d3181c7 AM |
3959 | /* Don't sort .init/.fini sections. */ |
3960 | if (strcmp (sec->spec.name, ".init") != 0 | |
3961 | && strcmp (sec->spec.name, ".fini") != 0) | |
bcaa7b3e L |
3962 | switch (sec->spec.sorted) |
3963 | { | |
3964 | case none: | |
3965 | sec->spec.sorted = sort_section; | |
3966 | break; | |
3967 | case by_name: | |
3968 | if (sort_section == by_alignment) | |
3969 | sec->spec.sorted = by_name_alignment; | |
3970 | break; | |
3971 | case by_alignment: | |
3972 | if (sort_section == by_name) | |
3973 | sec->spec.sorted = by_alignment_name; | |
3974 | break; | |
3975 | default: | |
3976 | break; | |
3977 | } | |
bcaa7b3e L |
3978 | break; |
3979 | ||
3980 | case lang_constructors_statement_enum: | |
3981 | update_wild_statements (constructor_list.head); | |
3982 | break; | |
3983 | ||
3984 | case lang_output_section_statement_enum: | |
2d3181c7 AM |
3985 | update_wild_statements |
3986 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
3987 | break; |
3988 | ||
3989 | case lang_group_statement_enum: | |
3990 | update_wild_statements (s->group_statement.children.head); | |
3991 | break; | |
3992 | } | |
3993 | } | |
3994 | break; | |
3995 | } | |
3996 | } | |
3997 | ||
396a2467 | 3998 | /* Open input files and attach to output sections. */ |
08da4cac | 3999 | |
252b5132 | 4000 | static void |
1579bae1 AM |
4001 | map_input_to_output_sections |
4002 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4003 | lang_output_section_statement_type *os) |
252b5132 | 4004 | { |
1579bae1 | 4005 | for (; s != NULL; s = s->header.next) |
252b5132 | 4006 | { |
dfa7b0b8 AM |
4007 | lang_output_section_statement_type *tos; |
4008 | flagword flags; | |
4009 | ||
252b5132 RH |
4010 | switch (s->header.type) |
4011 | { | |
252b5132 | 4012 | case lang_wild_statement_enum: |
afd7a018 | 4013 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4014 | break; |
252b5132 RH |
4015 | case lang_constructors_statement_enum: |
4016 | map_input_to_output_sections (constructor_list.head, | |
4017 | target, | |
afd7a018 | 4018 | os); |
252b5132 RH |
4019 | break; |
4020 | case lang_output_section_statement_enum: | |
dfa7b0b8 AM |
4021 | tos = &s->output_section_statement; |
4022 | if (tos->constraint != 0) | |
0841712e | 4023 | { |
dfa7b0b8 AM |
4024 | if (tos->constraint != ONLY_IF_RW |
4025 | && tos->constraint != ONLY_IF_RO) | |
0841712e | 4026 | break; |
dfa7b0b8 AM |
4027 | tos->all_input_readonly = TRUE; |
4028 | check_input_sections (tos->children.head, tos); | |
4029 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
0841712e | 4030 | { |
dfa7b0b8 | 4031 | tos->constraint = -1; |
0841712e JJ |
4032 | break; |
4033 | } | |
4034 | } | |
dfa7b0b8 | 4035 | map_input_to_output_sections (tos->children.head, |
252b5132 | 4036 | target, |
dfa7b0b8 | 4037 | tos); |
a431bc2e AM |
4038 | break; |
4039 | case lang_output_statement_enum: | |
252b5132 RH |
4040 | break; |
4041 | case lang_target_statement_enum: | |
4042 | target = s->target_statement.target; | |
4043 | break; | |
4044 | case lang_group_statement_enum: | |
4045 | map_input_to_output_sections (s->group_statement.children.head, | |
4046 | target, | |
afd7a018 | 4047 | os); |
252b5132 | 4048 | break; |
384d938f NS |
4049 | case lang_data_statement_enum: |
4050 | /* Make sure that any sections mentioned in the expression | |
4051 | are initialized. */ | |
4052 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4053 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4054 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4055 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4056 | switch (os->sectype) | |
4057 | { | |
4058 | case normal_section: | |
4059 | case overlay_section: | |
7b243801 | 4060 | case first_overlay_section: |
dfa7b0b8 AM |
4061 | break; |
4062 | case noalloc_section: | |
4063 | flags = SEC_HAS_CONTENTS; | |
4064 | break; | |
4065 | case noload_section: | |
f4eaaf7f AM |
4066 | if (bfd_get_flavour (link_info.output_bfd) |
4067 | == bfd_target_elf_flavour) | |
4068 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4069 | else | |
4070 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
dfa7b0b8 AM |
4071 | break; |
4072 | } | |
a431bc2e | 4073 | if (os->bfd_section == NULL) |
dfa7b0b8 | 4074 | init_os (os, flags); |
a431bc2e AM |
4075 | else |
4076 | os->bfd_section->flags |= flags; | |
afd7a018 | 4077 | break; |
252b5132 | 4078 | case lang_input_section_enum: |
e0f6802f AM |
4079 | break; |
4080 | case lang_fill_statement_enum: | |
252b5132 | 4081 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4082 | case lang_reloc_statement_enum: |
4083 | case lang_padding_statement_enum: | |
4084 | case lang_input_statement_enum: | |
afd7a018 | 4085 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4086 | init_os (os, 0); |
252b5132 RH |
4087 | break; |
4088 | case lang_assignment_statement_enum: | |
afd7a018 | 4089 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4090 | init_os (os, 0); |
252b5132 RH |
4091 | |
4092 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4093 | are initialized. */ |
252b5132 RH |
4094 | exp_init_os (s->assignment_statement.exp); |
4095 | break; | |
252b5132 | 4096 | case lang_address_statement_enum: |
a431bc2e AM |
4097 | /* Mark the specified section with the supplied address. |
4098 | If this section was actually a segment marker, then the | |
4099 | directive is ignored if the linker script explicitly | |
4100 | processed the segment marker. Originally, the linker | |
4101 | treated segment directives (like -Ttext on the | |
4102 | command-line) as section directives. We honor the | |
370dfff4 | 4103 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4104 | linker scripts that do not specifically check for |
4105 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4106 | if (!s->address_statement.segment |
ba916c8a MM |
4107 | || !s->address_statement.segment->used) |
4108 | { | |
dfa7b0b8 AM |
4109 | const char *name = s->address_statement.section_name; |
4110 | ||
4111 | /* Create the output section statement here so that | |
4112 | orphans with a set address will be placed after other | |
4113 | script sections. If we let the orphan placement code | |
4114 | place them in amongst other sections then the address | |
4115 | will affect following script sections, which is | |
4116 | likely to surprise naive users. */ | |
4117 | tos = lang_output_section_statement_lookup (name, 0, TRUE); | |
4118 | tos->addr_tree = s->address_statement.address; | |
4119 | if (tos->bfd_section == NULL) | |
4120 | init_os (tos, 0); | |
ba916c8a | 4121 | } |
252b5132 | 4122 | break; |
53d25da6 AM |
4123 | case lang_insert_statement_enum: |
4124 | break; | |
4125 | } | |
4126 | } | |
4127 | } | |
4128 | ||
4129 | /* An insert statement snips out all the linker statements from the | |
4130 | start of the list and places them after the output section | |
4131 | statement specified by the insert. This operation is complicated | |
4132 | by the fact that we keep a doubly linked list of output section | |
776ab89f AM |
4133 | statements as well as the singly linked list of all statements. |
4134 | FIXME someday: Twiddling with the list not only moves statements | |
4135 | from the user's script but also input and group statements that are | |
4136 | built from command line object files and --start-group. We only | |
4137 | get away with this because the list pointers used by file_chain | |
4138 | and input_file_chain are not reordered, and processing via | |
4139 | statement_list after this point mostly ignores input statements. | |
4140 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4141 | can end up looking odd. */ | |
53d25da6 AM |
4142 | |
4143 | static void | |
776ab89f | 4144 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
4145 | { |
4146 | lang_statement_union_type **s; | |
4147 | lang_output_section_statement_type *first_os = NULL; | |
4148 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 4149 | lang_output_section_statement_type *os; |
53d25da6 | 4150 | |
776ab89f AM |
4151 | s = start; |
4152 | while (*s != NULL) | |
53d25da6 AM |
4153 | { |
4154 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4155 | { | |
4156 | /* Keep pointers to the first and last output section | |
4157 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
4158 | os = &(*s)->output_section_statement; |
4159 | ||
4160 | ASSERT (last_os == NULL || last_os->next == os); | |
4161 | last_os = os; | |
66c103b7 AM |
4162 | |
4163 | /* Set constraint negative so that lang_output_section_find | |
4164 | won't match this output section statement. At this | |
4165 | stage in linking constraint has values in the range | |
4166 | [-1, ONLY_IN_RW]. */ | |
4167 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
4168 | if (first_os == NULL) |
4169 | first_os = last_os; | |
4170 | } | |
776ab89f AM |
4171 | else if ((*s)->header.type == lang_group_statement_enum) |
4172 | { | |
4173 | /* A user might put -T between --start-group and | |
4174 | --end-group. One way this odd construct might arise is | |
4175 | from a wrapper around ld to change library search | |
4176 | behaviour. For example: | |
4177 | #! /bin/sh | |
4178 | exec real_ld --start-group "$@" --end-group | |
4179 | This isn't completely unreasonable so go looking inside a | |
4180 | group statement for insert statements. */ | |
4181 | process_insert_statements (&(*s)->group_statement.children.head); | |
4182 | } | |
53d25da6 AM |
4183 | else if ((*s)->header.type == lang_insert_statement_enum) |
4184 | { | |
4185 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4186 | lang_output_section_statement_type *where; | |
53d25da6 AM |
4187 | lang_statement_union_type **ptr; |
4188 | lang_statement_union_type *first; | |
4189 | ||
4190 | where = lang_output_section_find (i->where); | |
4191 | if (where != NULL && i->is_before) | |
4192 | { | |
967928e9 | 4193 | do |
53d25da6 | 4194 | where = where->prev; |
66c103b7 | 4195 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4196 | } |
4197 | if (where == NULL) | |
4198 | { | |
66c103b7 AM |
4199 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
4200 | return; | |
53d25da6 AM |
4201 | } |
4202 | ||
4203 | /* Deal with reordering the output section statement list. */ | |
4204 | if (last_os != NULL) | |
4205 | { | |
4206 | asection *first_sec, *last_sec; | |
29183214 | 4207 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4208 | |
4209 | /* Snip out the output sections we are moving. */ | |
4210 | first_os->prev->next = last_os->next; | |
4211 | if (last_os->next == NULL) | |
29183214 L |
4212 | { |
4213 | next = &first_os->prev->next; | |
5c1e6d53 | 4214 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4215 | } |
53d25da6 AM |
4216 | else |
4217 | last_os->next->prev = first_os->prev; | |
4218 | /* Add them in at the new position. */ | |
4219 | last_os->next = where->next; | |
4220 | if (where->next == NULL) | |
29183214 L |
4221 | { |
4222 | next = &last_os->next; | |
5c1e6d53 | 4223 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4224 | } |
53d25da6 AM |
4225 | else |
4226 | where->next->prev = last_os; | |
4227 | first_os->prev = where; | |
4228 | where->next = first_os; | |
4229 | ||
4230 | /* Move the bfd sections in the same way. */ | |
4231 | first_sec = NULL; | |
4232 | last_sec = NULL; | |
4233 | for (os = first_os; os != NULL; os = os->next) | |
4234 | { | |
66c103b7 | 4235 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
4236 | if (os->bfd_section != NULL |
4237 | && os->bfd_section->owner != NULL) | |
4238 | { | |
4239 | last_sec = os->bfd_section; | |
4240 | if (first_sec == NULL) | |
4241 | first_sec = last_sec; | |
4242 | } | |
4243 | if (os == last_os) | |
4244 | break; | |
4245 | } | |
4246 | if (last_sec != NULL) | |
4247 | { | |
4248 | asection *sec = where->bfd_section; | |
4249 | if (sec == NULL) | |
4250 | sec = output_prev_sec_find (where); | |
4251 | ||
4252 | /* The place we want to insert must come after the | |
4253 | sections we are moving. So if we find no | |
4254 | section or if the section is the same as our | |
4255 | last section, then no move is needed. */ | |
4256 | if (sec != NULL && sec != last_sec) | |
4257 | { | |
4258 | /* Trim them off. */ | |
4259 | if (first_sec->prev != NULL) | |
4260 | first_sec->prev->next = last_sec->next; | |
4261 | else | |
f13a99db | 4262 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4263 | if (last_sec->next != NULL) |
4264 | last_sec->next->prev = first_sec->prev; | |
4265 | else | |
f13a99db | 4266 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 AM |
4267 | /* Add back. */ |
4268 | last_sec->next = sec->next; | |
4269 | if (sec->next != NULL) | |
4270 | sec->next->prev = last_sec; | |
4271 | else | |
f13a99db | 4272 | link_info.output_bfd->section_last = last_sec; |
53d25da6 AM |
4273 | first_sec->prev = sec; |
4274 | sec->next = first_sec; | |
4275 | } | |
4276 | } | |
4277 | ||
4278 | first_os = NULL; | |
4279 | last_os = NULL; | |
4280 | } | |
4281 | ||
4282 | ptr = insert_os_after (where); | |
776ab89f AM |
4283 | /* Snip everything from the start of the list, up to and |
4284 | including the insert statement we are currently processing. */ | |
4285 | first = *start; | |
4286 | *start = (*s)->header.next; | |
4287 | /* Add them back where they belong, minus the insert. */ | |
53d25da6 AM |
4288 | *s = *ptr; |
4289 | if (*s == NULL) | |
4290 | statement_list.tail = s; | |
4291 | *ptr = first; | |
776ab89f AM |
4292 | s = start; |
4293 | continue; | |
252b5132 | 4294 | } |
776ab89f | 4295 | s = &(*s)->header.next; |
252b5132 | 4296 | } |
66c103b7 AM |
4297 | |
4298 | /* Undo constraint twiddling. */ | |
4299 | for (os = first_os; os != NULL; os = os->next) | |
4300 | { | |
4301 | os->constraint = -2 - os->constraint; | |
4302 | if (os == last_os) | |
4303 | break; | |
4304 | } | |
252b5132 RH |
4305 | } |
4306 | ||
4bd5a393 AM |
4307 | /* An output section might have been removed after its statement was |
4308 | added. For example, ldemul_before_allocation can remove dynamic | |
4309 | sections if they turn out to be not needed. Clean them up here. */ | |
4310 | ||
8423293d | 4311 | void |
1579bae1 | 4312 | strip_excluded_output_sections (void) |
4bd5a393 | 4313 | { |
afd7a018 | 4314 | lang_output_section_statement_type *os; |
4bd5a393 | 4315 | |
046183de | 4316 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4317 | if (expld.phase != lang_mark_phase_enum) |
4318 | { | |
4319 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4320 | expld.dataseg.phase = exp_seg_none; |
e9ee469a AM |
4321 | one_lang_size_sections_pass (NULL, FALSE); |
4322 | lang_reset_memory_regions (); | |
4323 | } | |
4324 | ||
5c1e6d53 | 4325 | for (os = &lang_os_list.head->output_section_statement; |
afd7a018 AM |
4326 | os != NULL; |
4327 | os = os->next) | |
4bd5a393 | 4328 | { |
75ff4589 L |
4329 | asection *output_section; |
4330 | bfd_boolean exclude; | |
4bd5a393 | 4331 | |
66c103b7 | 4332 | if (os->constraint < 0) |
0841712e | 4333 | continue; |
8423293d | 4334 | |
75ff4589 L |
4335 | output_section = os->bfd_section; |
4336 | if (output_section == NULL) | |
8423293d AM |
4337 | continue; |
4338 | ||
32124d5b AM |
4339 | exclude = (output_section->rawsize == 0 |
4340 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4341 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
4342 | output_section)); |
4343 | ||
4344 | /* Some sections have not yet been sized, notably .gnu.version, | |
4345 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4346 | input sections, so don't drop output sections that have such | |
4347 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4348 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
4349 | { |
4350 | asection *s; | |
8423293d | 4351 | |
32124d5b | 4352 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4353 | if ((s->flags & SEC_EXCLUDE) == 0 |
4354 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4355 | || link_info.emitrelocations)) | |
e9ee469a AM |
4356 | { |
4357 | exclude = FALSE; | |
4358 | break; | |
4359 | } | |
75ff4589 | 4360 | } |
8423293d | 4361 | |
32124d5b | 4362 | if (exclude) |
4bd5a393 | 4363 | { |
e9ee469a AM |
4364 | /* We don't set bfd_section to NULL since bfd_section of the |
4365 | removed output section statement may still be used. */ | |
6d8bf25d | 4366 | if (!os->update_dot) |
74541ad4 | 4367 | os->ignored = TRUE; |
e9ee469a | 4368 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4369 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4370 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4371 | } |
4372 | } | |
18cd5bce AM |
4373 | } |
4374 | ||
4375 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4376 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
4377 | |
4378 | void | |
4379 | lang_clear_os_map (void) | |
4380 | { | |
4381 | lang_output_section_statement_type *os; | |
4382 | ||
4383 | if (map_head_is_link_order) | |
4384 | return; | |
4385 | ||
5c1e6d53 | 4386 | for (os = &lang_os_list.head->output_section_statement; |
18cd5bce AM |
4387 | os != NULL; |
4388 | os = os->next) | |
4389 | { | |
4390 | asection *output_section; | |
4391 | ||
4392 | if (os->constraint < 0) | |
4393 | continue; | |
4394 | ||
4395 | output_section = os->bfd_section; | |
4396 | if (output_section == NULL) | |
4397 | continue; | |
4398 | ||
4399 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4400 | output_section->map_head.link_order = NULL; | |
4401 | output_section->map_tail.link_order = NULL; | |
4402 | } | |
8423293d AM |
4403 | |
4404 | /* Stop future calls to lang_add_section from messing with map_head | |
4405 | and map_tail link_order fields. */ | |
18cd5bce | 4406 | map_head_is_link_order = TRUE; |
4bd5a393 AM |
4407 | } |
4408 | ||
252b5132 | 4409 | static void |
1579bae1 AM |
4410 | print_output_section_statement |
4411 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
4412 | { |
4413 | asection *section = output_section_statement->bfd_section; | |
4414 | int len; | |
4415 | ||
4416 | if (output_section_statement != abs_output_section) | |
4417 | { | |
4418 | minfo ("\n%s", output_section_statement->name); | |
4419 | ||
4420 | if (section != NULL) | |
4421 | { | |
4422 | print_dot = section->vma; | |
4423 | ||
4424 | len = strlen (output_section_statement->name); | |
4425 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4426 | { | |
4427 | print_nl (); | |
4428 | len = 0; | |
4429 | } | |
4430 | while (len < SECTION_NAME_MAP_LENGTH) | |
4431 | { | |
4432 | print_space (); | |
4433 | ++len; | |
4434 | } | |
4435 | ||
953dd97e | 4436 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4437 | |
67f744f3 AM |
4438 | if (section->vma != section->lma) |
4439 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4440 | |
4441 | if (output_section_statement->update_dot_tree != NULL) | |
4442 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4443 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
4444 | } |
4445 | ||
4446 | print_nl (); | |
4447 | } | |
4448 | ||
4449 | print_statement_list (output_section_statement->children.head, | |
4450 | output_section_statement); | |
4451 | } | |
4452 | ||
4453 | static void | |
1579bae1 AM |
4454 | print_assignment (lang_assignment_statement_type *assignment, |
4455 | lang_output_section_statement_type *output_section) | |
252b5132 | 4456 | { |
3b83e13a NC |
4457 | unsigned int i; |
4458 | bfd_boolean is_dot; | |
afd7a018 | 4459 | etree_type *tree; |
8658f989 | 4460 | asection *osec; |
252b5132 RH |
4461 | |
4462 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
4463 | print_space (); | |
4464 | ||
afd7a018 AM |
4465 | if (assignment->exp->type.node_class == etree_assert) |
4466 | { | |
3b83e13a | 4467 | is_dot = FALSE; |
afd7a018 AM |
4468 | tree = assignment->exp->assert_s.child; |
4469 | } | |
4470 | else | |
4471 | { | |
4472 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4473 | |
4474 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4475 | tree = assignment->exp; |
afd7a018 AM |
4476 | } |
4477 | ||
8658f989 AM |
4478 | osec = output_section->bfd_section; |
4479 | if (osec == NULL) | |
4480 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4481 | |
4482 | if (assignment->exp->type.node_class != etree_provide) | |
4483 | exp_fold_tree (tree, osec, &print_dot); | |
4484 | else | |
4485 | expld.result.valid_p = FALSE; | |
4486 | ||
e9ee469a | 4487 | if (expld.result.valid_p) |
7b17f854 | 4488 | { |
7b17f854 RS |
4489 | bfd_vma value; |
4490 | ||
4194268f AM |
4491 | if (assignment->exp->type.node_class == etree_assert |
4492 | || is_dot | |
4493 | || expld.assign_name != NULL) | |
3b83e13a | 4494 | { |
e9ee469a | 4495 | value = expld.result.value; |
10dbd1f3 | 4496 | |
7542af2a | 4497 | if (expld.result.section != NULL) |
e9ee469a | 4498 | value += expld.result.section->vma; |
7b17f854 | 4499 | |
3b83e13a NC |
4500 | minfo ("0x%V", value); |
4501 | if (is_dot) | |
4502 | print_dot = value; | |
4503 | } | |
4504 | else | |
4505 | { | |
4506 | struct bfd_link_hash_entry *h; | |
4507 | ||
4508 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
4509 | FALSE, FALSE, TRUE); | |
6a846243 AM |
4510 | if (h != NULL |
4511 | && (h->type == bfd_link_hash_defined | |
4512 | || h->type == bfd_link_hash_defweak)) | |
3b83e13a | 4513 | { |
10dbd1f3 | 4514 | value = h->u.def.value; |
f37a7048 NS |
4515 | value += h->u.def.section->output_section->vma; |
4516 | value += h->u.def.section->output_offset; | |
3b83e13a NC |
4517 | |
4518 | minfo ("[0x%V]", value); | |
4519 | } | |
4520 | else | |
4521 | minfo ("[unresolved]"); | |
4522 | } | |
7b17f854 | 4523 | } |
252b5132 RH |
4524 | else |
4525 | { | |
c05b575a | 4526 | if (assignment->exp->type.node_class == etree_provide) |
0aa7f586 | 4527 | minfo ("[!provide]"); |
c05b575a | 4528 | else |
0aa7f586 | 4529 | minfo ("*undef* "); |
252b5132 RH |
4530 | #ifdef BFD64 |
4531 | minfo (" "); | |
4532 | #endif | |
4533 | } | |
4194268f | 4534 | expld.assign_name = NULL; |
252b5132 RH |
4535 | |
4536 | minfo (" "); | |
252b5132 | 4537 | exp_print_tree (assignment->exp); |
252b5132 RH |
4538 | print_nl (); |
4539 | } | |
4540 | ||
4541 | static void | |
1579bae1 | 4542 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4543 | { |
0d41d9a2 | 4544 | if (statm->filename != NULL) |
906e58ca | 4545 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4546 | } |
4547 | ||
4548 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4549 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4550 | |
b34976b6 | 4551 | static bfd_boolean |
1579bae1 | 4552 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4553 | { |
1e9cc1c2 | 4554 | asection *sec = (asection *) ptr; |
252b5132 RH |
4555 | |
4556 | if ((hash_entry->type == bfd_link_hash_defined | |
4557 | || hash_entry->type == bfd_link_hash_defweak) | |
4558 | && sec == hash_entry->u.def.section) | |
4559 | { | |
4560 | int i; | |
4561 | ||
4562 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
4563 | print_space (); | |
4564 | minfo ("0x%V ", | |
4565 | (hash_entry->u.def.value | |
4566 | + hash_entry->u.def.section->output_offset | |
4567 | + hash_entry->u.def.section->output_section->vma)); | |
4568 | ||
c1c8c1ef | 4569 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4570 | } |
4571 | ||
b34976b6 | 4572 | return TRUE; |
252b5132 RH |
4573 | } |
4574 | ||
02688209 TG |
4575 | static int |
4576 | hash_entry_addr_cmp (const void *a, const void *b) | |
4577 | { | |
4578 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4579 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4580 | ||
4581 | if (l->u.def.value < r->u.def.value) | |
4582 | return -1; | |
4583 | else if (l->u.def.value > r->u.def.value) | |
4584 | return 1; | |
4585 | else | |
4586 | return 0; | |
4587 | } | |
4588 | ||
35835446 | 4589 | static void |
e0f6802f | 4590 | print_all_symbols (asection *sec) |
35835446 | 4591 | { |
a48931cc | 4592 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4593 | struct map_symbol_def *def; |
02688209 TG |
4594 | struct bfd_link_hash_entry **entries; |
4595 | unsigned int i; | |
35835446 | 4596 | |
afd7a018 AM |
4597 | if (!ud) |
4598 | return; | |
4599 | ||
35835446 | 4600 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4601 | |
02688209 | 4602 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4603 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4604 | obstack_alloc (&map_obstack, |
4605 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
4606 | |
4607 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4608 | entries[i] = def->entry; | |
4609 | ||
4610 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4724d37e | 4611 | hash_entry_addr_cmp); |
02688209 TG |
4612 | |
4613 | /* Print the symbols. */ | |
4614 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
4615 | print_one_symbol (entries[i], sec); | |
4616 | ||
4617 | obstack_free (&map_obstack, entries); | |
35835446 JR |
4618 | } |
4619 | ||
252b5132 RH |
4620 | /* Print information about an input section to the map file. */ |
4621 | ||
4622 | static void | |
d64703c6 | 4623 | print_input_section (asection *i, bfd_boolean is_discarded) |
252b5132 | 4624 | { |
eea6121a | 4625 | bfd_size_type size = i->size; |
abe6ac95 AM |
4626 | int len; |
4627 | bfd_vma addr; | |
e5caa5e0 AM |
4628 | |
4629 | init_opb (); | |
4d4920ec | 4630 | |
abe6ac95 AM |
4631 | print_space (); |
4632 | minfo ("%s", i->name); | |
252b5132 | 4633 | |
abe6ac95 AM |
4634 | len = 1 + strlen (i->name); |
4635 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4636 | { | |
4637 | print_nl (); | |
4638 | len = 0; | |
4639 | } | |
4640 | while (len < SECTION_NAME_MAP_LENGTH) | |
4641 | { | |
57ceae94 | 4642 | print_space (); |
abe6ac95 AM |
4643 | ++len; |
4644 | } | |
252b5132 | 4645 | |
f13a99db AM |
4646 | if (i->output_section != NULL |
4647 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
4648 | addr = i->output_section->vma + i->output_offset; |
4649 | else | |
4650 | { | |
4651 | addr = print_dot; | |
d64703c6 TG |
4652 | if (!is_discarded) |
4653 | size = 0; | |
abe6ac95 | 4654 | } |
252b5132 | 4655 | |
b0a7971a | 4656 | minfo ("0x%V %W %pB\n", addr, TO_ADDR (size), i->owner); |
252b5132 | 4657 | |
abe6ac95 AM |
4658 | if (size != i->rawsize && i->rawsize != 0) |
4659 | { | |
4660 | len = SECTION_NAME_MAP_LENGTH + 3; | |
252b5132 | 4661 | #ifdef BFD64 |
abe6ac95 | 4662 | len += 16; |
252b5132 | 4663 | #else |
abe6ac95 | 4664 | len += 8; |
252b5132 | 4665 | #endif |
abe6ac95 AM |
4666 | while (len > 0) |
4667 | { | |
4668 | print_space (); | |
4669 | --len; | |
57ceae94 AM |
4670 | } |
4671 | ||
b0a7971a | 4672 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 4673 | } |
252b5132 | 4674 | |
f13a99db AM |
4675 | if (i->output_section != NULL |
4676 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 4677 | { |
c0f00686 | 4678 | if (link_info.reduce_memory_overheads) |
abe6ac95 AM |
4679 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); |
4680 | else | |
4681 | print_all_symbols (i); | |
4682 | ||
5590fba9 NC |
4683 | /* Update print_dot, but make sure that we do not move it |
4684 | backwards - this could happen if we have overlays and a | |
4685 | later overlay is shorter than an earier one. */ | |
4686 | if (addr + TO_ADDR (size) > print_dot) | |
4687 | print_dot = addr + TO_ADDR (size); | |
252b5132 RH |
4688 | } |
4689 | } | |
4690 | ||
4691 | static void | |
1579bae1 | 4692 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 4693 | { |
2c382fb6 AM |
4694 | size_t size; |
4695 | unsigned char *p; | |
4696 | fputs (" FILL mask 0x", config.map_file); | |
4697 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4698 | fprintf (config.map_file, "%02x", *p); | |
4699 | fputs ("\n", config.map_file); | |
252b5132 RH |
4700 | } |
4701 | ||
4702 | static void | |
1579bae1 | 4703 | print_data_statement (lang_data_statement_type *data) |
252b5132 RH |
4704 | { |
4705 | int i; | |
4706 | bfd_vma addr; | |
4707 | bfd_size_type size; | |
4708 | const char *name; | |
4709 | ||
e5caa5e0 | 4710 | init_opb (); |
252b5132 RH |
4711 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4712 | print_space (); | |
4713 | ||
7fabd029 | 4714 | addr = data->output_offset; |
252b5132 RH |
4715 | if (data->output_section != NULL) |
4716 | addr += data->output_section->vma; | |
4717 | ||
4718 | switch (data->type) | |
4719 | { | |
4720 | default: | |
4721 | abort (); | |
4722 | case BYTE: | |
4723 | size = BYTE_SIZE; | |
4724 | name = "BYTE"; | |
4725 | break; | |
4726 | case SHORT: | |
4727 | size = SHORT_SIZE; | |
4728 | name = "SHORT"; | |
4729 | break; | |
4730 | case LONG: | |
4731 | size = LONG_SIZE; | |
4732 | name = "LONG"; | |
4733 | break; | |
4734 | case QUAD: | |
4735 | size = QUAD_SIZE; | |
4736 | name = "QUAD"; | |
4737 | break; | |
4738 | case SQUAD: | |
4739 | size = QUAD_SIZE; | |
4740 | name = "SQUAD"; | |
4741 | break; | |
4742 | } | |
4743 | ||
953dd97e DG |
4744 | if (size < TO_SIZE ((unsigned) 1)) |
4745 | size = TO_SIZE ((unsigned) 1); | |
4746 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
252b5132 RH |
4747 | |
4748 | if (data->exp->type.node_class != etree_value) | |
4749 | { | |
4750 | print_space (); | |
4751 | exp_print_tree (data->exp); | |
4752 | } | |
4753 | ||
4754 | print_nl (); | |
4755 | ||
e5caa5e0 | 4756 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
4757 | } |
4758 | ||
4759 | /* Print an address statement. These are generated by options like | |
4760 | -Ttext. */ | |
4761 | ||
4762 | static void | |
1579bae1 | 4763 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
4764 | { |
4765 | minfo (_("Address of section %s set to "), address->section_name); | |
4766 | exp_print_tree (address->address); | |
4767 | print_nl (); | |
4768 | } | |
4769 | ||
4770 | /* Print a reloc statement. */ | |
4771 | ||
4772 | static void | |
1579bae1 | 4773 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 RH |
4774 | { |
4775 | int i; | |
4776 | bfd_vma addr; | |
4777 | bfd_size_type size; | |
4778 | ||
e5caa5e0 | 4779 | init_opb (); |
252b5132 RH |
4780 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4781 | print_space (); | |
4782 | ||
7fabd029 | 4783 | addr = reloc->output_offset; |
252b5132 RH |
4784 | if (reloc->output_section != NULL) |
4785 | addr += reloc->output_section->vma; | |
4786 | ||
4787 | size = bfd_get_reloc_size (reloc->howto); | |
4788 | ||
953dd97e | 4789 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
4790 | |
4791 | if (reloc->name != NULL) | |
4792 | minfo ("%s+", reloc->name); | |
4793 | else | |
4794 | minfo ("%s+", reloc->section->name); | |
4795 | ||
4796 | exp_print_tree (reloc->addend_exp); | |
4797 | ||
4798 | print_nl (); | |
4799 | ||
e5caa5e0 | 4800 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 4801 | } |
252b5132 RH |
4802 | |
4803 | static void | |
1579bae1 | 4804 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
4805 | { |
4806 | int len; | |
4807 | bfd_vma addr; | |
4808 | ||
e5caa5e0 | 4809 | init_opb (); |
252b5132 RH |
4810 | minfo (" *fill*"); |
4811 | ||
4812 | len = sizeof " *fill*" - 1; | |
4813 | while (len < SECTION_NAME_MAP_LENGTH) | |
4814 | { | |
4815 | print_space (); | |
4816 | ++len; | |
4817 | } | |
4818 | ||
4819 | addr = s->output_offset; | |
4820 | if (s->output_section != NULL) | |
4821 | addr += s->output_section->vma; | |
953dd97e | 4822 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 4823 | |
2c382fb6 AM |
4824 | if (s->fill->size != 0) |
4825 | { | |
4826 | size_t size; | |
4827 | unsigned char *p; | |
4828 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
4829 | fprintf (config.map_file, "%02x", *p); | |
4830 | } | |
252b5132 RH |
4831 | |
4832 | print_nl (); | |
4833 | ||
e5caa5e0 | 4834 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
4835 | } |
4836 | ||
4837 | static void | |
1579bae1 AM |
4838 | print_wild_statement (lang_wild_statement_type *w, |
4839 | lang_output_section_statement_type *os) | |
252b5132 | 4840 | { |
b6bf44ba AM |
4841 | struct wildcard_list *sec; |
4842 | ||
252b5132 RH |
4843 | print_space (); |
4844 | ||
8f1732fc AB |
4845 | if (w->exclude_name_list) |
4846 | { | |
4847 | name_list *tmp; | |
4848 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
4849 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
6c19b93b | 4850 | minfo (" %s", tmp->name); |
8f1732fc AB |
4851 | minfo (") "); |
4852 | } | |
4853 | ||
252b5132 | 4854 | if (w->filenames_sorted) |
2b94abd4 | 4855 | minfo ("SORT_BY_NAME("); |
08da4cac | 4856 | if (w->filename != NULL) |
252b5132 RH |
4857 | minfo ("%s", w->filename); |
4858 | else | |
4859 | minfo ("*"); | |
4860 | if (w->filenames_sorted) | |
4861 | minfo (")"); | |
4862 | ||
4863 | minfo ("("); | |
b6bf44ba AM |
4864 | for (sec = w->section_list; sec; sec = sec->next) |
4865 | { | |
2b94abd4 AB |
4866 | int closing_paren = 0; |
4867 | ||
4868 | switch (sec->spec.sorted) | |
6c19b93b AM |
4869 | { |
4870 | case none: | |
4871 | break; | |
4872 | ||
4873 | case by_name: | |
4874 | minfo ("SORT_BY_NAME("); | |
4875 | closing_paren = 1; | |
4876 | break; | |
4877 | ||
4878 | case by_alignment: | |
4879 | minfo ("SORT_BY_ALIGNMENT("); | |
4880 | closing_paren = 1; | |
4881 | break; | |
4882 | ||
4883 | case by_name_alignment: | |
4884 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
4885 | closing_paren = 2; | |
4886 | break; | |
4887 | ||
4888 | case by_alignment_name: | |
4889 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
4890 | closing_paren = 2; | |
4891 | break; | |
4892 | ||
4893 | case by_none: | |
4894 | minfo ("SORT_NONE("); | |
4895 | closing_paren = 1; | |
4896 | break; | |
4897 | ||
4898 | case by_init_priority: | |
4899 | minfo ("SORT_BY_INIT_PRIORITY("); | |
4900 | closing_paren = 1; | |
4901 | break; | |
4902 | } | |
2b94abd4 | 4903 | |
b6bf44ba AM |
4904 | if (sec->spec.exclude_name_list != NULL) |
4905 | { | |
4906 | name_list *tmp; | |
34786259 | 4907 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 4908 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
4909 | minfo (" %s", tmp->name); |
4910 | minfo (") "); | |
b6bf44ba AM |
4911 | } |
4912 | if (sec->spec.name != NULL) | |
4913 | minfo ("%s", sec->spec.name); | |
4914 | else | |
4915 | minfo ("*"); | |
2b94abd4 | 4916 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 4917 | minfo (")"); |
34786259 AM |
4918 | if (sec->next) |
4919 | minfo (" "); | |
b6bf44ba | 4920 | } |
252b5132 RH |
4921 | minfo (")"); |
4922 | ||
4923 | print_nl (); | |
4924 | ||
4925 | print_statement_list (w->children.head, os); | |
4926 | } | |
4927 | ||
4928 | /* Print a group statement. */ | |
4929 | ||
4930 | static void | |
1579bae1 AM |
4931 | print_group (lang_group_statement_type *s, |
4932 | lang_output_section_statement_type *os) | |
252b5132 RH |
4933 | { |
4934 | fprintf (config.map_file, "START GROUP\n"); | |
4935 | print_statement_list (s->children.head, os); | |
4936 | fprintf (config.map_file, "END GROUP\n"); | |
4937 | } | |
4938 | ||
4939 | /* Print the list of statements in S. | |
4940 | This can be called for any statement type. */ | |
4941 | ||
4942 | static void | |
1579bae1 AM |
4943 | print_statement_list (lang_statement_union_type *s, |
4944 | lang_output_section_statement_type *os) | |
252b5132 RH |
4945 | { |
4946 | while (s != NULL) | |
4947 | { | |
4948 | print_statement (s, os); | |
bba1a0c0 | 4949 | s = s->header.next; |
252b5132 RH |
4950 | } |
4951 | } | |
4952 | ||
4953 | /* Print the first statement in statement list S. | |
4954 | This can be called for any statement type. */ | |
4955 | ||
4956 | static void | |
1579bae1 AM |
4957 | print_statement (lang_statement_union_type *s, |
4958 | lang_output_section_statement_type *os) | |
252b5132 RH |
4959 | { |
4960 | switch (s->header.type) | |
4961 | { | |
4962 | default: | |
4963 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
4964 | FAIL (); | |
4965 | break; | |
4966 | case lang_constructors_statement_enum: | |
4967 | if (constructor_list.head != NULL) | |
4968 | { | |
4969 | if (constructors_sorted) | |
4970 | minfo (" SORT (CONSTRUCTORS)\n"); | |
4971 | else | |
4972 | minfo (" CONSTRUCTORS\n"); | |
4973 | print_statement_list (constructor_list.head, os); | |
4974 | } | |
4975 | break; | |
4976 | case lang_wild_statement_enum: | |
4977 | print_wild_statement (&s->wild_statement, os); | |
4978 | break; | |
4979 | case lang_address_statement_enum: | |
4980 | print_address_statement (&s->address_statement); | |
4981 | break; | |
4982 | case lang_object_symbols_statement_enum: | |
4983 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
4984 | break; | |
4985 | case lang_fill_statement_enum: | |
4986 | print_fill_statement (&s->fill_statement); | |
4987 | break; | |
4988 | case lang_data_statement_enum: | |
4989 | print_data_statement (&s->data_statement); | |
4990 | break; | |
4991 | case lang_reloc_statement_enum: | |
4992 | print_reloc_statement (&s->reloc_statement); | |
4993 | break; | |
4994 | case lang_input_section_enum: | |
d64703c6 | 4995 | print_input_section (s->input_section.section, FALSE); |
252b5132 RH |
4996 | break; |
4997 | case lang_padding_statement_enum: | |
4998 | print_padding_statement (&s->padding_statement); | |
4999 | break; | |
5000 | case lang_output_section_statement_enum: | |
5001 | print_output_section_statement (&s->output_section_statement); | |
5002 | break; | |
5003 | case lang_assignment_statement_enum: | |
5004 | print_assignment (&s->assignment_statement, os); | |
5005 | break; | |
5006 | case lang_target_statement_enum: | |
5007 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5008 | break; | |
5009 | case lang_output_statement_enum: | |
5010 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5011 | if (output_target != NULL) | |
5012 | minfo (" %s", output_target); | |
5013 | minfo (")\n"); | |
5014 | break; | |
5015 | case lang_input_statement_enum: | |
5016 | print_input_statement (&s->input_statement); | |
5017 | break; | |
5018 | case lang_group_statement_enum: | |
5019 | print_group (&s->group_statement, os); | |
5020 | break; | |
53d25da6 AM |
5021 | case lang_insert_statement_enum: |
5022 | minfo ("INSERT %s %s\n", | |
5023 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5024 | s->insert_statement.where); | |
5025 | break; | |
252b5132 RH |
5026 | } |
5027 | } | |
5028 | ||
5029 | static void | |
1579bae1 | 5030 | print_statements (void) |
252b5132 RH |
5031 | { |
5032 | print_statement_list (statement_list.head, abs_output_section); | |
5033 | } | |
5034 | ||
5035 | /* Print the first N statements in statement list S to STDERR. | |
5036 | If N == 0, nothing is printed. | |
5037 | If N < 0, the entire list is printed. | |
5038 | Intended to be called from GDB. */ | |
5039 | ||
5040 | void | |
1579bae1 | 5041 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
5042 | { |
5043 | FILE *map_save = config.map_file; | |
5044 | ||
5045 | config.map_file = stderr; | |
5046 | ||
5047 | if (n < 0) | |
5048 | print_statement_list (s, abs_output_section); | |
5049 | else | |
5050 | { | |
5051 | while (s && --n >= 0) | |
5052 | { | |
5053 | print_statement (s, abs_output_section); | |
bba1a0c0 | 5054 | s = s->header.next; |
252b5132 RH |
5055 | } |
5056 | } | |
5057 | ||
5058 | config.map_file = map_save; | |
5059 | } | |
5060 | ||
b3327aad | 5061 | static void |
1579bae1 AM |
5062 | insert_pad (lang_statement_union_type **ptr, |
5063 | fill_type *fill, | |
1a69ff95 | 5064 | bfd_size_type alignment_needed, |
1579bae1 AM |
5065 | asection *output_section, |
5066 | bfd_vma dot) | |
252b5132 | 5067 | { |
b7761f11 | 5068 | static fill_type zero_fill; |
e9ee469a | 5069 | lang_statement_union_type *pad = NULL; |
b3327aad | 5070 | |
e9ee469a AM |
5071 | if (ptr != &statement_list.head) |
5072 | pad = ((lang_statement_union_type *) | |
5073 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5074 | if (pad != NULL | |
5075 | && pad->header.type == lang_padding_statement_enum | |
5076 | && pad->padding_statement.output_section == output_section) | |
5077 | { | |
5078 | /* Use the existing pad statement. */ | |
5079 | } | |
5080 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
5081 | && pad->header.type == lang_padding_statement_enum |
5082 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5083 | { |
e9ee469a | 5084 | /* Use the existing pad statement. */ |
252b5132 | 5085 | } |
b3327aad | 5086 | else |
252b5132 | 5087 | { |
b3327aad | 5088 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5089 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5090 | pad->header.next = *ptr; |
5091 | *ptr = pad; | |
5092 | pad->header.type = lang_padding_statement_enum; | |
5093 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5094 | if (fill == NULL) |
2c382fb6 | 5095 | fill = &zero_fill; |
b3327aad | 5096 | pad->padding_statement.fill = fill; |
252b5132 | 5097 | } |
b3327aad AM |
5098 | pad->padding_statement.output_offset = dot - output_section->vma; |
5099 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5100 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5101 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5102 | - output_section->vma); | |
252b5132 RH |
5103 | } |
5104 | ||
08da4cac KH |
5105 | /* Work out how much this section will move the dot point. */ |
5106 | ||
252b5132 | 5107 | static bfd_vma |
6feb9908 AM |
5108 | size_input_section |
5109 | (lang_statement_union_type **this_ptr, | |
5110 | lang_output_section_statement_type *output_section_statement, | |
5111 | fill_type *fill, | |
5112 | bfd_vma dot) | |
252b5132 RH |
5113 | { |
5114 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5115 | asection *i = is->section; | |
93d1b056 | 5116 | asection *o = output_section_statement->bfd_section; |
252b5132 | 5117 | |
93d1b056 AM |
5118 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5119 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5120 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5121 | || output_section_statement->ignored) | |
93d1b056 AM |
5122 | i->output_offset = dot - o->vma; |
5123 | else | |
252b5132 | 5124 | { |
1a69ff95 | 5125 | bfd_size_type alignment_needed; |
b3327aad AM |
5126 | |
5127 | /* Align this section first to the input sections requirement, | |
5128 | then to the output section's requirement. If this alignment | |
5129 | is greater than any seen before, then record it too. Perform | |
5130 | the alignment by inserting a magic 'padding' statement. */ | |
5131 | ||
3d9c8f6b AM |
5132 | if (output_section_statement->subsection_alignment != NULL) |
5133 | i->alignment_power | |
5134 | = exp_get_power (output_section_statement->subsection_alignment, | |
5135 | "subsection alignment"); | |
b3327aad | 5136 | |
b3327aad AM |
5137 | if (o->alignment_power < i->alignment_power) |
5138 | o->alignment_power = i->alignment_power; | |
252b5132 | 5139 | |
b3327aad AM |
5140 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5141 | ||
5142 | if (alignment_needed != 0) | |
5143 | { | |
e5caa5e0 | 5144 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5145 | dot += alignment_needed; |
5146 | } | |
252b5132 | 5147 | |
08da4cac | 5148 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5149 | i->output_offset = dot - o->vma; |
252b5132 | 5150 | |
08da4cac | 5151 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5152 | dot += TO_ADDR (i->size); |
8772de11 MR |
5153 | if (!(o->flags & SEC_FIXED_SIZE)) |
5154 | o->size = TO_SIZE (dot - o->vma); | |
252b5132 | 5155 | } |
252b5132 RH |
5156 | |
5157 | return dot; | |
5158 | } | |
5159 | ||
a87dd97a AM |
5160 | struct check_sec |
5161 | { | |
5162 | asection *sec; | |
5163 | bfd_boolean warned; | |
5164 | }; | |
5165 | ||
5daa8fe7 L |
5166 | static int |
5167 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5168 | { | |
a87dd97a AM |
5169 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; |
5170 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5171 | ||
5172 | if (sec1->lma < sec2->lma) | |
5173 | return -1; | |
5174 | else if (sec1->lma > sec2->lma) | |
5175 | return 1; | |
5176 | else if (sec1->id < sec2->id) | |
5177 | return -1; | |
5178 | else if (sec1->id > sec2->id) | |
5179 | return 1; | |
5180 | ||
5181 | return 0; | |
5182 | } | |
5183 | ||
5184 | static int | |
5185 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5186 | { | |
5187 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5188 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5daa8fe7 | 5189 | |
a87dd97a | 5190 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5191 | return -1; |
a87dd97a | 5192 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5193 | return 1; |
7f6a71ff JM |
5194 | else if (sec1->id < sec2->id) |
5195 | return -1; | |
5196 | else if (sec1->id > sec2->id) | |
5197 | return 1; | |
5daa8fe7 L |
5198 | |
5199 | return 0; | |
5200 | } | |
5201 | ||
2e4a7aea AM |
5202 | #define IS_TBSS(s) \ |
5203 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5204 | ||
eea6121a | 5205 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5206 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5207 | |
252b5132 | 5208 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5209 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5210 | section addresses of the two sections. Also check whether any memory |
5211 | region has overflowed. */ | |
08da4cac | 5212 | |
252b5132 | 5213 | static void |
1579bae1 | 5214 | lang_check_section_addresses (void) |
252b5132 | 5215 | { |
f4427a75 | 5216 | asection *s, *p; |
a87dd97a AM |
5217 | struct check_sec *sections; |
5218 | size_t i, count; | |
d40e34db | 5219 | bfd_vma addr_mask; |
5daa8fe7 L |
5220 | bfd_vma s_start; |
5221 | bfd_vma s_end; | |
a87dd97a AM |
5222 | bfd_vma p_start = 0; |
5223 | bfd_vma p_end = 0; | |
20e56351 | 5224 | lang_memory_region_type *m; |
136a43b7 | 5225 | bfd_boolean overlays; |
5daa8fe7 | 5226 | |
21701718 | 5227 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
5228 | addr_mask = ((bfd_vma) 1 << |
5229 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5230 | addr_mask = (addr_mask << 1) + 1; | |
5231 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
21701718 TG |
5232 | if ((s->flags & SEC_ALLOC) != 0) |
5233 | { | |
5234 | s_end = (s->vma + s->size) & addr_mask; | |
5235 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5236 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5237 | s->name); | |
5238 | else | |
5239 | { | |
5240 | s_end = (s->lma + s->size) & addr_mask; | |
5241 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5242 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5243 | s->name); | |
5244 | } | |
5245 | } | |
d40e34db | 5246 | |
f13a99db | 5247 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5248 | return; |
5249 | ||
a87dd97a AM |
5250 | count = bfd_count_sections (link_info.output_bfd); |
5251 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5252 | |
5253 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5254 | count = 0; |
f13a99db | 5255 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5256 | { |
a87dd97a | 5257 | if (IGNORE_SECTION (s) |
ec6d26be | 5258 | || s->size == 0) |
33275c22 | 5259 | continue; |
5f992e62 | 5260 | |
a87dd97a AM |
5261 | sections[count].sec = s; |
5262 | sections[count].warned = FALSE; | |
5daa8fe7 L |
5263 | count++; |
5264 | } | |
329c1c86 | 5265 | |
5daa8fe7 | 5266 | if (count <= 1) |
136a43b7 AM |
5267 | { |
5268 | free (sections); | |
5269 | return; | |
5270 | } | |
5f992e62 | 5271 | |
a87dd97a | 5272 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5273 | |
136a43b7 | 5274 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5275 | loadable sections, even with overlays. */ |
5276 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5277 | { |
a87dd97a AM |
5278 | s = sections[i].sec; |
5279 | if ((s->flags & SEC_LOAD) != 0) | |
5280 | { | |
5281 | s_start = s->lma; | |
5282 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5283 | ||
5284 | /* Look for an overlap. We have sorted sections by lma, so | |
5285 | we know that s_start >= p_start. Besides the obvious | |
5286 | case of overlap when the current section starts before | |
5287 | the previous one ends, we also must have overlap if the | |
5288 | previous section wraps around the address space. */ | |
5289 | if (p != NULL | |
5290 | && (s_start <= p_end | |
5291 | || p_end < p_start)) | |
5292 | { | |
5293 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5294 | " overlaps section %s LMA [%V,%V]\n"), | |
5295 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5296 | sections[i].warned = TRUE; | |
5297 | } | |
5298 | p = s; | |
5299 | p_start = s_start; | |
5300 | p_end = s_end; | |
5301 | } | |
5302 | } | |
5303 | ||
136a43b7 AM |
5304 | /* If any non-zero size allocated section (excluding tbss) starts at |
5305 | exactly the same VMA as another such section, then we have | |
5306 | overlays. Overlays generated by the OVERLAY keyword will have | |
5307 | this property. It is possible to intentionally generate overlays | |
5308 | that fail this test, but it would be unusual. */ | |
5309 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
5310 | overlays = FALSE; | |
5311 | p_start = sections[0].sec->vma; | |
5312 | for (i = 1; i < count; i++) | |
a87dd97a | 5313 | { |
136a43b7 AM |
5314 | s_start = sections[i].sec->vma; |
5315 | if (p_start == s_start) | |
5316 | { | |
5317 | overlays = TRUE; | |
5318 | break; | |
5319 | } | |
5320 | p_start = s_start; | |
5321 | } | |
a87dd97a | 5322 | |
136a43b7 AM |
5323 | /* Now check section VMAs if no overlays were detected. */ |
5324 | if (!overlays) | |
5325 | { | |
a87dd97a AM |
5326 | for (p = NULL, i = 0; i < count; i++) |
5327 | { | |
5328 | s = sections[i].sec; | |
5329 | s_start = s->vma; | |
5330 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5331 | ||
5332 | if (p != NULL | |
5333 | && !sections[i].warned | |
5334 | && (s_start <= p_end | |
5335 | || p_end < p_start)) | |
5336 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5337 | " overlaps section %s VMA [%V,%V]\n"), | |
5338 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5339 | p = s; | |
5340 | p_start = s_start; | |
5341 | p_end = s_end; | |
5342 | } | |
33275c22 | 5343 | } |
5daa8fe7 L |
5344 | |
5345 | free (sections); | |
20e56351 DJ |
5346 | |
5347 | /* If any memory region has overflowed, report by how much. | |
5348 | We do not issue this diagnostic for regions that had sections | |
5349 | explicitly placed outside their bounds; os_region_check's | |
5350 | diagnostics are adequate for that case. | |
5351 | ||
5352 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5353 | might overflow a 32-bit integer. There is, alas, no way to print | |
5354 | a bfd_vma quantity in decimal. */ | |
5355 | for (m = lang_memory_region_list; m; m = m->next) | |
5356 | if (m->had_full_message) | |
992a06ee AM |
5357 | { |
5358 | unsigned long over = m->current - (m->origin + m->length); | |
5359 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5360 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5361 | over), | |
5362 | m->name_list.name, over); | |
5363 | } | |
252b5132 RH |
5364 | } |
5365 | ||
562d3460 TW |
5366 | /* Make sure the new address is within the region. We explicitly permit the |
5367 | current address to be at the exact end of the region when the address is | |
5368 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 5369 | calculation wraps around. */ |
562d3460 TW |
5370 | |
5371 | static void | |
1579bae1 | 5372 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5373 | lang_memory_region_type *region, |
1579bae1 | 5374 | etree_type *tree, |
91d6fa6a | 5375 | bfd_vma rbase) |
562d3460 TW |
5376 | { |
5377 | if ((region->current < region->origin | |
5378 | || (region->current - region->origin > region->length)) | |
5379 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5380 | || rbase == 0)) |
562d3460 | 5381 | { |
1579bae1 | 5382 | if (tree != NULL) |
b7a26f91 | 5383 | { |
871b3ab2 | 5384 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5385 | " is not within region `%s'\n"), |
b7a26f91 KH |
5386 | region->current, |
5387 | os->bfd_section->owner, | |
5388 | os->bfd_section->name, | |
4a93e180 | 5389 | region->name_list.name); |
b7a26f91 | 5390 | } |
20e56351 | 5391 | else if (!region->had_full_message) |
b7a26f91 | 5392 | { |
20e56351 DJ |
5393 | region->had_full_message = TRUE; |
5394 | ||
871b3ab2 | 5395 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5396 | os->bfd_section->owner, |
20e56351 | 5397 | os->bfd_section->name, |
4a93e180 | 5398 | region->name_list.name); |
b7a26f91 | 5399 | } |
562d3460 TW |
5400 | } |
5401 | } | |
5402 | ||
ed1794ee L |
5403 | static void |
5404 | ldlang_check_relro_region (lang_statement_union_type *s, | |
5405 | seg_align_type *seg) | |
5406 | { | |
5407 | if (seg->relro == exp_seg_relro_start) | |
5408 | { | |
5409 | if (!seg->relro_start_stat) | |
5410 | seg->relro_start_stat = s; | |
5411 | else | |
5412 | { | |
5413 | ASSERT (seg->relro_start_stat == s); | |
5414 | } | |
5415 | } | |
5416 | else if (seg->relro == exp_seg_relro_end) | |
5417 | { | |
5418 | if (!seg->relro_end_stat) | |
5419 | seg->relro_end_stat = s; | |
5420 | else | |
5421 | { | |
5422 | ASSERT (seg->relro_end_stat == s); | |
5423 | } | |
5424 | } | |
5425 | } | |
5426 | ||
252b5132 RH |
5427 | /* Set the sizes for all the output sections. */ |
5428 | ||
2d20f7bf | 5429 | static bfd_vma |
1579bae1 | 5430 | lang_size_sections_1 |
e535e147 | 5431 | (lang_statement_union_type **prev, |
1579bae1 | 5432 | lang_output_section_statement_type *output_section_statement, |
1579bae1 AM |
5433 | fill_type *fill, |
5434 | bfd_vma dot, | |
5435 | bfd_boolean *relax, | |
5436 | bfd_boolean check_regions) | |
252b5132 | 5437 | { |
e535e147 AM |
5438 | lang_statement_union_type *s; |
5439 | ||
252b5132 | 5440 | /* Size up the sections from their constituent parts. */ |
e535e147 | 5441 | for (s = *prev; s != NULL; s = s->header.next) |
252b5132 RH |
5442 | { |
5443 | switch (s->header.type) | |
5444 | { | |
5445 | case lang_output_section_statement_enum: | |
5446 | { | |
13075d04 | 5447 | bfd_vma newdot, after, dotdelta; |
d1778b88 | 5448 | lang_output_section_statement_type *os; |
cde9e0be | 5449 | lang_memory_region_type *r; |
c5b0a9ef | 5450 | int section_alignment = 0; |
252b5132 | 5451 | |
d1778b88 | 5452 | os = &s->output_section_statement; |
8658f989 AM |
5453 | if (os->constraint == -1) |
5454 | break; | |
5455 | ||
fd68d03d AM |
5456 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
5457 | here, in lang_insert_orphan, or in the default linker scripts. | |
5458 | This is covering for coff backend linker bugs. See PR6945. */ | |
5459 | if (os->addr_tree == NULL | |
0e1862bb | 5460 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5461 | && (bfd_get_flavour (link_info.output_bfd) |
5462 | == bfd_target_coff_flavour)) | |
eceda120 | 5463 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5464 | if (os->addr_tree != NULL) |
5465 | { | |
cde9e0be | 5466 | os->processed_vma = FALSE; |
a5df8c84 | 5467 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5468 | |
3bf9618b | 5469 | if (expld.result.valid_p) |
7542af2a AM |
5470 | { |
5471 | dot = expld.result.value; | |
5472 | if (expld.result.section != NULL) | |
5473 | dot += expld.result.section->vma; | |
5474 | } | |
3bf9618b | 5475 | else if (expld.phase != lang_mark_phase_enum) |
df5f2391 | 5476 | einfo (_("%F%P:%pS: non constant or forward reference" |
a5df8c84 | 5477 | " address expression for section %s\n"), |
dab69f68 | 5478 | os->addr_tree, os->name); |
a5df8c84 AM |
5479 | } |
5480 | ||
e9ee469a | 5481 | if (os->bfd_section == NULL) |
75ff4589 | 5482 | /* This section was removed or never actually created. */ |
252b5132 RH |
5483 | break; |
5484 | ||
5485 | /* If this is a COFF shared library section, use the size and | |
5486 | address from the input section. FIXME: This is COFF | |
5487 | specific; it would be cleaner if there were some other way | |
5488 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
5489 | if (((bfd_get_flavour (link_info.output_bfd) |
5490 | == bfd_target_ecoff_flavour) | |
5491 | || (bfd_get_flavour (link_info.output_bfd) | |
5492 | == bfd_target_coff_flavour)) | |
ebe372c1 | 5493 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 5494 | { |
08da4cac | 5495 | asection *input; |
252b5132 RH |
5496 | |
5497 | if (os->children.head == NULL | |
bba1a0c0 | 5498 | || os->children.head->header.next != NULL |
6feb9908 AM |
5499 | || (os->children.head->header.type |
5500 | != lang_input_section_enum)) | |
df5f2391 | 5501 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 5502 | " section %s\n"), os->name); |
252b5132 RH |
5503 | |
5504 | input = os->children.head->input_section.section; | |
fd361982 AM |
5505 | bfd_set_section_vma (os->bfd_section, |
5506 | bfd_section_vma (input)); | |
8772de11 MR |
5507 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5508 | os->bfd_section->size = input->size; | |
252b5132 RH |
5509 | break; |
5510 | } | |
5511 | ||
a5df8c84 | 5512 | newdot = dot; |
13075d04 | 5513 | dotdelta = 0; |
252b5132 RH |
5514 | if (bfd_is_abs_section (os->bfd_section)) |
5515 | { | |
5516 | /* No matter what happens, an abs section starts at zero. */ | |
5517 | ASSERT (os->bfd_section->vma == 0); | |
5518 | } | |
5519 | else | |
5520 | { | |
1579bae1 | 5521 | if (os->addr_tree == NULL) |
252b5132 RH |
5522 | { |
5523 | /* No address specified for this section, get one | |
5524 | from the region specification. */ | |
1579bae1 | 5525 | if (os->region == NULL |
6feb9908 | 5526 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
5527 | && os->region->name_list.name[0] == '*' |
5528 | && strcmp (os->region->name_list.name, | |
6feb9908 | 5529 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
5530 | { |
5531 | os->region = lang_memory_default (os->bfd_section); | |
5532 | } | |
5533 | ||
5534 | /* If a loadable section is using the default memory | |
5535 | region, and some non default memory regions were | |
66184979 | 5536 | defined, issue an error message. */ |
f0636a44 AM |
5537 | if (!os->ignored |
5538 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 5539 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 5540 | && check_regions |
4a93e180 | 5541 | && strcmp (os->region->name_list.name, |
6feb9908 | 5542 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 5543 | && lang_memory_region_list != NULL |
4a93e180 | 5544 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 5545 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a AM |
5546 | || lang_memory_region_list->next != NULL) |
5547 | && expld.phase != lang_mark_phase_enum) | |
66184979 NC |
5548 | { |
5549 | /* By default this is an error rather than just a | |
5550 | warning because if we allocate the section to the | |
5551 | default memory region we can end up creating an | |
07f3b6ad KH |
5552 | excessively large binary, or even seg faulting when |
5553 | attempting to perform a negative seek. See | |
6feb9908 | 5554 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
5555 | for an example of this. This behaviour can be |
5556 | overridden by the using the --no-check-sections | |
5557 | switch. */ | |
5558 | if (command_line.check_section_addresses) | |
df5f2391 | 5559 | einfo (_("%F%P: error: no memory region specified" |
6feb9908 | 5560 | " for loadable section `%s'\n"), |
fd361982 | 5561 | bfd_section_name (os->bfd_section)); |
66184979 | 5562 | else |
6feb9908 AM |
5563 | einfo (_("%P: warning: no memory region specified" |
5564 | " for loadable section `%s'\n"), | |
fd361982 | 5565 | bfd_section_name (os->bfd_section)); |
66184979 | 5566 | } |
252b5132 | 5567 | |
e9ee469a | 5568 | newdot = os->region->current; |
c5b0a9ef | 5569 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 5570 | } |
94b50910 | 5571 | else |
3d9c8f6b AM |
5572 | section_alignment = exp_get_power (os->section_alignment, |
5573 | "section alignment"); | |
5f992e62 | 5574 | |
7270c5ed | 5575 | /* Align to what the section needs. */ |
c5b0a9ef | 5576 | if (section_alignment > 0) |
94b50910 AM |
5577 | { |
5578 | bfd_vma savedot = newdot; | |
c5b0a9ef | 5579 | newdot = align_power (newdot, section_alignment); |
252b5132 | 5580 | |
13075d04 SH |
5581 | dotdelta = newdot - savedot; |
5582 | if (dotdelta != 0 | |
94b50910 AM |
5583 | && (config.warn_section_align |
5584 | || os->addr_tree != NULL) | |
5585 | && expld.phase != lang_mark_phase_enum) | |
992a06ee AM |
5586 | einfo (ngettext ("%P: warning: changing start of " |
5587 | "section %s by %lu byte\n", | |
5588 | "%P: warning: changing start of " | |
5589 | "section %s by %lu bytes\n", | |
5590 | (unsigned long) dotdelta), | |
13075d04 | 5591 | os->name, (unsigned long) dotdelta); |
94b50910 | 5592 | } |
252b5132 | 5593 | |
fd361982 | 5594 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 5595 | |
252b5132 RH |
5596 | os->bfd_section->output_offset = 0; |
5597 | } | |
5598 | ||
e535e147 | 5599 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
5600 | os->fill, newdot, relax, check_regions); |
5601 | ||
cde9e0be | 5602 | os->processed_vma = TRUE; |
e9ee469a AM |
5603 | |
5604 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
5605 | /* Except for some special linker created sections, |
5606 | no output section should change from zero size | |
5607 | after strip_excluded_output_sections. A non-zero | |
5608 | size on an ignored section indicates that some | |
5609 | input section was not sized early enough. */ | |
5610 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 5611 | else |
e9ee469a | 5612 | { |
cde9e0be AM |
5613 | dot = os->bfd_section->vma; |
5614 | ||
5615 | /* Put the section within the requested block size, or | |
5616 | align at the block boundary. */ | |
5617 | after = ((dot | |
5618 | + TO_ADDR (os->bfd_section->size) | |
5619 | + os->block_value - 1) | |
5620 | & - (bfd_vma) os->block_value); | |
5621 | ||
8772de11 MR |
5622 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5623 | os->bfd_section->size = TO_SIZE (after | |
5624 | - os->bfd_section->vma); | |
cde9e0be AM |
5625 | } |
5626 | ||
5627 | /* Set section lma. */ | |
5628 | r = os->region; | |
5629 | if (r == NULL) | |
5630 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); | |
5631 | ||
5632 | if (os->load_base) | |
5633 | { | |
5634 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
5635 | os->bfd_section->lma = lma; | |
e9ee469a | 5636 | } |
07dfcf38 | 5637 | else if (os->lma_region != NULL) |
cde9e0be AM |
5638 | { |
5639 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 5640 | |
13075d04 SH |
5641 | if (os->align_lma_with_input) |
5642 | lma += dotdelta; | |
5643 | else | |
5644 | { | |
5645 | /* When LMA_REGION is the same as REGION, align the LMA | |
5646 | as we did for the VMA, possibly including alignment | |
5647 | from the bfd section. If a different region, then | |
5648 | only align according to the value in the output | |
5649 | statement. */ | |
5650 | if (os->lma_region != os->region) | |
3d9c8f6b AM |
5651 | section_alignment = exp_get_power (os->section_alignment, |
5652 | "section alignment"); | |
13075d04 SH |
5653 | if (section_alignment > 0) |
5654 | lma = align_power (lma, section_alignment); | |
5655 | } | |
cde9e0be AM |
5656 | os->bfd_section->lma = lma; |
5657 | } | |
dc0b6aa0 AM |
5658 | else if (r->last_os != NULL |
5659 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
5660 | { |
5661 | bfd_vma lma; | |
5662 | asection *last; | |
5663 | ||
5664 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
5665 | |
5666 | /* A backwards move of dot should be accompanied by | |
5667 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 5668 | os->load_base set) because backwards moves can |
dc0b6aa0 | 5669 | create overlapping LMAs. */ |
264b6205 | 5670 | if (dot < last->vma |
91eb6c46 | 5671 | && os->bfd_section->size != 0 |
ca62bc4a | 5672 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 5673 | { |
dc0b6aa0 AM |
5674 | /* If dot moved backwards then leave lma equal to |
5675 | vma. This is the old default lma, which might | |
5676 | just happen to work when the backwards move is | |
91eb6c46 AM |
5677 | sufficiently large. Nag if this changes anything, |
5678 | so people can fix their linker scripts. */ | |
5679 | ||
5680 | if (last->vma != last->lma) | |
0aa7f586 AM |
5681 | einfo (_("%P: warning: dot moved backwards " |
5682 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
5683 | } |
5684 | else | |
cde9e0be | 5685 | { |
152d792f AM |
5686 | /* If this is an overlay, set the current lma to that |
5687 | at the end of the previous section. */ | |
5688 | if (os->sectype == overlay_section) | |
fc90c280 | 5689 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
5690 | |
5691 | /* Otherwise, keep the same lma to vma relationship | |
5692 | as the previous section. */ | |
5693 | else | |
8610e0fd | 5694 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 5695 | |
c5b0a9ef AM |
5696 | if (section_alignment > 0) |
5697 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
5698 | os->bfd_section->lma = lma; |
5699 | } | |
5700 | } | |
5701 | os->processed_lma = TRUE; | |
5f992e62 | 5702 | |
cde9e0be AM |
5703 | /* Keep track of normal sections using the default |
5704 | lma region. We use this to set the lma for | |
5705 | following sections. Overlays or other linker | |
5706 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
5707 | default lma == vma is incorrect. |
5708 | To avoid warnings about dot moving backwards when using | |
5709 | -Ttext, don't start tracking sections until we find one | |
14ea2c1b AB |
5710 | of non-zero size or with lma set differently to vma. |
5711 | Do this tracking before we short-cut the loop so that we | |
5712 | track changes for the case where the section size is zero, | |
5713 | but the lma is set differently to the vma. This is | |
5714 | important, if an orphan section is placed after an | |
5715 | otherwise empty output section that has an explicit lma | |
5716 | set, we want that lma reflected in the orphans lma. */ | |
7b243801 AM |
5717 | if (((!IGNORE_SECTION (os->bfd_section) |
5718 | && (os->bfd_section->size != 0 | |
5719 | || (r->last_os == NULL | |
5720 | && os->bfd_section->vma != os->bfd_section->lma) | |
5721 | || (r->last_os != NULL | |
5722 | && dot >= (r->last_os->output_section_statement | |
5723 | .bfd_section->vma)))) | |
5724 | || os->sectype == first_overlay_section) | |
cde9e0be | 5725 | && os->lma_region == NULL |
0e1862bb | 5726 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 5727 | r->last_os = s; |
9f88b410 | 5728 | |
14ea2c1b AB |
5729 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
5730 | break; | |
5731 | ||
e5caec89 | 5732 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 5733 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 5734 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
5735 | dotdelta = TO_ADDR (os->bfd_section->size); |
5736 | else | |
5737 | dotdelta = 0; | |
5738 | dot += dotdelta; | |
e5caec89 | 5739 | |
9f88b410 | 5740 | if (os->update_dot_tree != 0) |
e9ee469a | 5741 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 5742 | |
252b5132 RH |
5743 | /* Update dot in the region ? |
5744 | We only do this if the section is going to be allocated, | |
5745 | since unallocated sections do not contribute to the region's | |
2e76e85a | 5746 | overall size in memory. */ |
1579bae1 | 5747 | if (os->region != NULL |
2e76e85a | 5748 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
5749 | { |
5750 | os->region->current = dot; | |
5f992e62 | 5751 | |
cf888e70 NC |
5752 | if (check_regions) |
5753 | /* Make sure the new address is within the region. */ | |
5754 | os_region_check (os, os->region, os->addr_tree, | |
5755 | os->bfd_section->vma); | |
08da4cac | 5756 | |
a2cab753 | 5757 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
5758 | && ((os->bfd_section->flags & SEC_LOAD) |
5759 | || os->align_lma_with_input)) | |
08da4cac | 5760 | { |
13075d04 | 5761 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 5762 | |
cf888e70 NC |
5763 | if (check_regions) |
5764 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 5765 | os->bfd_section->lma); |
08da4cac | 5766 | } |
252b5132 RH |
5767 | } |
5768 | } | |
5769 | break; | |
5770 | ||
5771 | case lang_constructors_statement_enum: | |
e535e147 | 5772 | dot = lang_size_sections_1 (&constructor_list.head, |
2d20f7bf | 5773 | output_section_statement, |
cf888e70 | 5774 | fill, dot, relax, check_regions); |
252b5132 RH |
5775 | break; |
5776 | ||
5777 | case lang_data_statement_enum: | |
5778 | { | |
5779 | unsigned int size = 0; | |
5780 | ||
7fabd029 | 5781 | s->data_statement.output_offset = |
08da4cac | 5782 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
5783 | s->data_statement.output_section = |
5784 | output_section_statement->bfd_section; | |
5785 | ||
1b493742 NS |
5786 | /* We might refer to provided symbols in the expression, and |
5787 | need to mark them as needed. */ | |
e9ee469a | 5788 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 5789 | |
252b5132 RH |
5790 | switch (s->data_statement.type) |
5791 | { | |
08da4cac KH |
5792 | default: |
5793 | abort (); | |
252b5132 RH |
5794 | case QUAD: |
5795 | case SQUAD: | |
5796 | size = QUAD_SIZE; | |
5797 | break; | |
5798 | case LONG: | |
5799 | size = LONG_SIZE; | |
5800 | break; | |
5801 | case SHORT: | |
5802 | size = SHORT_SIZE; | |
5803 | break; | |
5804 | case BYTE: | |
5805 | size = BYTE_SIZE; | |
5806 | break; | |
5807 | } | |
e5caa5e0 AM |
5808 | if (size < TO_SIZE ((unsigned) 1)) |
5809 | size = TO_SIZE ((unsigned) 1); | |
5810 | dot += TO_ADDR (size); | |
8772de11 MR |
5811 | if (!(output_section_statement->bfd_section->flags |
5812 | & SEC_FIXED_SIZE)) | |
5813 | output_section_statement->bfd_section->size | |
5814 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
aa4c3319 | 5815 | |
252b5132 RH |
5816 | } |
5817 | break; | |
5818 | ||
5819 | case lang_reloc_statement_enum: | |
5820 | { | |
5821 | int size; | |
5822 | ||
7fabd029 | 5823 | s->reloc_statement.output_offset = |
252b5132 RH |
5824 | dot - output_section_statement->bfd_section->vma; |
5825 | s->reloc_statement.output_section = | |
5826 | output_section_statement->bfd_section; | |
5827 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 5828 | dot += TO_ADDR (size); |
8772de11 MR |
5829 | if (!(output_section_statement->bfd_section->flags |
5830 | & SEC_FIXED_SIZE)) | |
5831 | output_section_statement->bfd_section->size | |
5832 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
252b5132 RH |
5833 | } |
5834 | break; | |
5f992e62 | 5835 | |
252b5132 | 5836 | case lang_wild_statement_enum: |
e535e147 | 5837 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
2d20f7bf | 5838 | output_section_statement, |
cf888e70 | 5839 | fill, dot, relax, check_regions); |
252b5132 RH |
5840 | break; |
5841 | ||
5842 | case lang_object_symbols_statement_enum: | |
3b6c1966 AM |
5843 | link_info.create_object_symbols_section |
5844 | = output_section_statement->bfd_section; | |
5845 | output_section_statement->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 5846 | break; |
e9ee469a | 5847 | |
252b5132 RH |
5848 | case lang_output_statement_enum: |
5849 | case lang_target_statement_enum: | |
5850 | break; | |
e9ee469a | 5851 | |
252b5132 RH |
5852 | case lang_input_section_enum: |
5853 | { | |
5854 | asection *i; | |
5855 | ||
e535e147 | 5856 | i = s->input_section.section; |
eea6121a | 5857 | if (relax) |
252b5132 | 5858 | { |
b34976b6 | 5859 | bfd_boolean again; |
252b5132 | 5860 | |
0aa7f586 | 5861 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
df5f2391 | 5862 | einfo (_("%F%P: can't relax section: %E\n")); |
252b5132 | 5863 | if (again) |
b34976b6 | 5864 | *relax = TRUE; |
252b5132 | 5865 | } |
b3327aad | 5866 | dot = size_input_section (prev, output_section_statement, |
ffe54b37 | 5867 | fill, dot); |
252b5132 RH |
5868 | } |
5869 | break; | |
e9ee469a | 5870 | |
252b5132 RH |
5871 | case lang_input_statement_enum: |
5872 | break; | |
e9ee469a | 5873 | |
252b5132 | 5874 | case lang_fill_statement_enum: |
08da4cac KH |
5875 | s->fill_statement.output_section = |
5876 | output_section_statement->bfd_section; | |
252b5132 RH |
5877 | |
5878 | fill = s->fill_statement.fill; | |
5879 | break; | |
e9ee469a | 5880 | |
252b5132 RH |
5881 | case lang_assignment_statement_enum: |
5882 | { | |
5883 | bfd_vma newdot = dot; | |
49c13adb | 5884 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 5885 | |
0f99513f | 5886 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 5887 | |
49c13adb | 5888 | exp_fold_tree (tree, |
408082ec | 5889 | output_section_statement->bfd_section, |
252b5132 RH |
5890 | &newdot); |
5891 | ||
ed1794ee L |
5892 | ldlang_check_relro_region (s, &expld.dataseg); |
5893 | ||
0f99513f | 5894 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 5895 | |
d2667025 | 5896 | /* This symbol may be relative to this section. */ |
a14a5de3 | 5897 | if ((tree->type.node_class == etree_provided |
49c13adb L |
5898 | || tree->type.node_class == etree_assign) |
5899 | && (tree->assign.dst [0] != '.' | |
5900 | || tree->assign.dst [1] != '\0')) | |
6d8bf25d | 5901 | output_section_statement->update_dot = 1; |
49c13adb | 5902 | |
85852e36 | 5903 | if (!output_section_statement->ignored) |
252b5132 | 5904 | { |
252b5132 RH |
5905 | if (output_section_statement == abs_output_section) |
5906 | { | |
5907 | /* If we don't have an output section, then just adjust | |
5908 | the default memory address. */ | |
6feb9908 AM |
5909 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
5910 | FALSE)->current = newdot; | |
252b5132 | 5911 | } |
85852e36 | 5912 | else if (newdot != dot) |
252b5132 | 5913 | { |
b3327aad AM |
5914 | /* Insert a pad after this statement. We can't |
5915 | put the pad before when relaxing, in case the | |
5916 | assignment references dot. */ | |
e5caa5e0 | 5917 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
5918 | output_section_statement->bfd_section, dot); |
5919 | ||
5920 | /* Don't neuter the pad below when relaxing. */ | |
5921 | s = s->header.next; | |
9dfc8ab2 | 5922 | |
e9ee469a AM |
5923 | /* If dot is advanced, this implies that the section |
5924 | should have space allocated to it, unless the | |
5925 | user has explicitly stated that the section | |
2e76e85a | 5926 | should not be allocated. */ |
f4eaaf7f AM |
5927 | if (output_section_statement->sectype != noalloc_section |
5928 | && (output_section_statement->sectype != noload_section | |
5929 | || (bfd_get_flavour (link_info.output_bfd) | |
5930 | == bfd_target_elf_flavour))) | |
e9ee469a AM |
5931 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
5932 | } | |
252b5132 RH |
5933 | dot = newdot; |
5934 | } | |
5935 | } | |
5936 | break; | |
5937 | ||
5938 | case lang_padding_statement_enum: | |
c0c330a7 AM |
5939 | /* If this is the first time lang_size_sections is called, |
5940 | we won't have any padding statements. If this is the | |
5941 | second or later passes when relaxing, we should allow | |
5942 | padding to shrink. If padding is needed on this pass, it | |
5943 | will be added back in. */ | |
5944 | s->padding_statement.size = 0; | |
6e814ff8 AM |
5945 | |
5946 | /* Make sure output_offset is valid. If relaxation shrinks | |
5947 | the section and this pad isn't needed, it's possible to | |
5948 | have output_offset larger than the final size of the | |
5949 | section. bfd_set_section_contents will complain even for | |
5950 | a pad size of zero. */ | |
5951 | s->padding_statement.output_offset | |
5952 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
5953 | break; |
5954 | ||
5955 | case lang_group_statement_enum: | |
e535e147 | 5956 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
2d20f7bf | 5957 | output_section_statement, |
cf888e70 | 5958 | fill, dot, relax, check_regions); |
252b5132 RH |
5959 | break; |
5960 | ||
53d25da6 | 5961 | case lang_insert_statement_enum: |
252b5132 RH |
5962 | break; |
5963 | ||
c0c330a7 | 5964 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
5965 | case lang_address_statement_enum: |
5966 | break; | |
53d25da6 AM |
5967 | |
5968 | default: | |
5969 | FAIL (); | |
5970 | break; | |
252b5132 RH |
5971 | } |
5972 | prev = &s->header.next; | |
5973 | } | |
5974 | return dot; | |
5975 | } | |
5976 | ||
591a748a NC |
5977 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
5978 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
5979 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
5980 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b NC |
5981 | |
5982 | bfd_boolean | |
0aa7f586 AM |
5983 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
5984 | bfd *abfd ATTRIBUTE_UNUSED, | |
5985 | asection *current_section, | |
5986 | asection *previous_section, | |
2889e75b NC |
5987 | bfd_boolean new_segment) |
5988 | { | |
0aa7f586 AM |
5989 | lang_output_section_statement_type *cur; |
5990 | lang_output_section_statement_type *prev; | |
591a748a NC |
5991 | |
5992 | /* The checks below are only necessary when the BFD library has decided | |
5993 | that the two sections ought to be placed into the same segment. */ | |
2889e75b NC |
5994 | if (new_segment) |
5995 | return TRUE; | |
5996 | ||
5997 | /* Paranoia checks. */ | |
5998 | if (current_section == NULL || previous_section == NULL) | |
5999 | return new_segment; | |
6000 | ||
4724d37e RM |
6001 | /* If this flag is set, the target never wants code and non-code |
6002 | sections comingled in the same segment. */ | |
6003 | if (config.separate_code | |
6004 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
6005 | return TRUE; | |
6006 | ||
2889e75b NC |
6007 | /* Find the memory regions associated with the two sections. |
6008 | We call lang_output_section_find() here rather than scanning the list | |
6009 | of output sections looking for a matching section pointer because if | |
591a748a | 6010 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6011 | cur = lang_output_section_find (current_section->name); |
6012 | prev = lang_output_section_find (previous_section->name); | |
6013 | ||
591a748a | 6014 | /* More paranoia. */ |
2889e75b NC |
6015 | if (cur == NULL || prev == NULL) |
6016 | return new_segment; | |
6017 | ||
6018 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6019 | different segments. See the email thread starting at the following |
6020 | URL for the reasons why this is necessary: | |
2889e75b NC |
6021 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6022 | return cur->region != prev->region; | |
6023 | } | |
6024 | ||
e9ee469a AM |
6025 | void |
6026 | one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions) | |
2d20f7bf | 6027 | { |
420e579c | 6028 | lang_statement_iteration++; |
e535e147 AM |
6029 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6030 | 0, 0, relax, check_regions); | |
e9ee469a | 6031 | } |
420e579c | 6032 | |
cba6246d L |
6033 | static bfd_boolean |
6034 | lang_size_segment (seg_align_type *seg) | |
e9ee469a | 6035 | { |
cba6246d L |
6036 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6037 | a page could be saved in the data segment. */ | |
6038 | bfd_vma first, last; | |
6039 | ||
6040 | first = -seg->base & (seg->pagesize - 1); | |
6041 | last = seg->end & (seg->pagesize - 1); | |
6042 | if (first && last | |
6043 | && ((seg->base & ~(seg->pagesize - 1)) | |
6044 | != (seg->end & ~(seg->pagesize - 1))) | |
6045 | && first + last <= seg->pagesize) | |
8c37241b | 6046 | { |
cba6246d L |
6047 | seg->phase = exp_seg_adjust; |
6048 | return TRUE; | |
6049 | } | |
6050 | ||
6051 | seg->phase = exp_seg_done; | |
6052 | return FALSE; | |
6053 | } | |
6054 | ||
6055 | static bfd_vma | |
6056 | lang_size_relro_segment_1 (seg_align_type *seg) | |
6057 | { | |
6058 | bfd_vma relro_end, desired_end; | |
6059 | asection *sec; | |
8c37241b | 6060 | |
cba6246d L |
6061 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
6062 | relro_end = ((seg->relro_end + seg->pagesize - 1) | |
6063 | & ~(seg->pagesize - 1)); | |
0e5fabeb | 6064 | |
cba6246d L |
6065 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
6066 | desired_end = relro_end - seg->relro_offset; | |
0e5fabeb | 6067 | |
cba6246d L |
6068 | /* For sections in the relro segment.. */ |
6069 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) | |
6070 | if ((sec->flags & SEC_ALLOC) != 0 | |
6071 | && sec->vma >= seg->base | |
6072 | && sec->vma < seg->relro_end - seg->relro_offset) | |
6073 | { | |
6074 | /* Where do we want to put this section so that it ends as | |
6075 | desired? */ | |
6076 | bfd_vma start, end, bump; | |
6077 | ||
6078 | end = start = sec->vma; | |
6079 | if (!IS_TBSS (sec)) | |
6080 | end += TO_ADDR (sec->size); | |
6081 | bump = desired_end - end; | |
6082 | /* We'd like to increase START by BUMP, but we must heed | |
6083 | alignment so the increase might be less than optimum. */ | |
6084 | start += bump; | |
6085 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6086 | /* This is now the desired end for the previous section. */ | |
6087 | desired_end = start; | |
6088 | } | |
6089 | ||
6090 | seg->phase = exp_seg_relro_adjust; | |
6091 | ASSERT (desired_end >= seg->base); | |
6092 | seg->base = desired_end; | |
6093 | return relro_end; | |
6094 | } | |
6095 | ||
6096 | static bfd_boolean | |
6097 | lang_size_relro_segment (bfd_boolean *relax, bfd_boolean check_regions) | |
6098 | { | |
6099 | bfd_boolean do_reset = FALSE; | |
6100 | bfd_boolean do_data_relro; | |
6101 | bfd_vma data_initial_base, data_relro_end; | |
6102 | ||
6103 | if (link_info.relro && expld.dataseg.relro_end) | |
6104 | { | |
6105 | do_data_relro = TRUE; | |
6106 | data_initial_base = expld.dataseg.base; | |
6107 | data_relro_end = lang_size_relro_segment_1 (&expld.dataseg); | |
6108 | } | |
6109 | else | |
6110 | { | |
6111 | do_data_relro = FALSE; | |
6112 | data_initial_base = data_relro_end = 0; | |
6113 | } | |
0e5fabeb | 6114 | |
cba6246d L |
6115 | if (do_data_relro) |
6116 | { | |
f86a8756 | 6117 | lang_reset_memory_regions (); |
e9ee469a | 6118 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6119 | |
cba6246d L |
6120 | /* Assignments to dot, or to output section address in a user |
6121 | script have increased padding over the original. Revert. */ | |
6122 | if (do_data_relro && expld.dataseg.relro_end > data_relro_end) | |
a4f5ad88 | 6123 | { |
cba6246d L |
6124 | expld.dataseg.base = data_initial_base;; |
6125 | do_reset = TRUE; | |
a4f5ad88 | 6126 | } |
8c37241b | 6127 | } |
cba6246d L |
6128 | |
6129 | if (!do_data_relro && lang_size_segment (&expld.dataseg)) | |
6130 | do_reset = TRUE; | |
6131 | ||
6132 | return do_reset; | |
6133 | } | |
6134 | ||
6135 | void | |
6136 | lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions) | |
6137 | { | |
6138 | expld.phase = lang_allocating_phase_enum; | |
6139 | expld.dataseg.phase = exp_seg_none; | |
6140 | ||
6141 | one_lang_size_sections_pass (relax, check_regions); | |
6142 | ||
6143 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6144 | expld.dataseg.phase = exp_seg_done; | |
6145 | ||
6146 | if (expld.dataseg.phase == exp_seg_end_seen) | |
2d20f7bf | 6147 | { |
cba6246d L |
6148 | bfd_boolean do_reset |
6149 | = lang_size_relro_segment (relax, check_regions); | |
6150 | ||
6151 | if (do_reset) | |
2d20f7bf | 6152 | { |
f86a8756 | 6153 | lang_reset_memory_regions (); |
e9ee469a | 6154 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6155 | } |
cba6246d L |
6156 | |
6157 | if (link_info.relro && expld.dataseg.relro_end) | |
6158 | { | |
6159 | link_info.relro_start = expld.dataseg.base; | |
6160 | link_info.relro_end = expld.dataseg.relro_end; | |
6161 | } | |
2d20f7bf | 6162 | } |
2d20f7bf JJ |
6163 | } |
6164 | ||
d2667025 AM |
6165 | static lang_output_section_statement_type *current_section; |
6166 | static lang_assignment_statement_type *current_assign; | |
6167 | static bfd_boolean prefer_next_section; | |
6168 | ||
420e579c HPN |
6169 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6170 | ||
6171 | static bfd_vma | |
66e28d60 AM |
6172 | lang_do_assignments_1 (lang_statement_union_type *s, |
6173 | lang_output_section_statement_type *current_os, | |
6174 | fill_type *fill, | |
d2667025 AM |
6175 | bfd_vma dot, |
6176 | bfd_boolean *found_end) | |
252b5132 | 6177 | { |
1579bae1 | 6178 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6179 | { |
6180 | switch (s->header.type) | |
6181 | { | |
6182 | case lang_constructors_statement_enum: | |
420e579c | 6183 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6184 | current_os, fill, dot, found_end); |
252b5132 RH |
6185 | break; |
6186 | ||
6187 | case lang_output_section_statement_enum: | |
6188 | { | |
d1778b88 | 6189 | lang_output_section_statement_type *os; |
f02cb058 | 6190 | bfd_vma newdot; |
252b5132 | 6191 | |
d1778b88 | 6192 | os = &(s->output_section_statement); |
d2667025 | 6193 | os->after_end = *found_end; |
75ff4589 | 6194 | if (os->bfd_section != NULL && !os->ignored) |
252b5132 | 6195 | { |
d2667025 AM |
6196 | if ((os->bfd_section->flags & SEC_ALLOC) != 0) |
6197 | { | |
6198 | current_section = os; | |
6199 | prefer_next_section = FALSE; | |
6200 | } | |
252b5132 | 6201 | dot = os->bfd_section->vma; |
f02cb058 AM |
6202 | } |
6203 | newdot = lang_do_assignments_1 (os->children.head, | |
6204 | os, os->fill, dot, found_end); | |
6205 | if (!os->ignored) | |
6206 | { | |
6207 | if (os->bfd_section != NULL) | |
6208 | { | |
6209 | /* .tbss sections effectively have zero size. */ | |
6210 | if (!IS_TBSS (os->bfd_section) | |
6211 | || bfd_link_relocatable (&link_info)) | |
6212 | dot += TO_ADDR (os->bfd_section->size); | |
6213 | ||
6214 | if (os->update_dot_tree != NULL) | |
6215 | exp_fold_tree (os->update_dot_tree, | |
6216 | bfd_abs_section_ptr, &dot); | |
6217 | } | |
6218 | else | |
6219 | dot = newdot; | |
252b5132 RH |
6220 | } |
6221 | } | |
6222 | break; | |
e9ee469a | 6223 | |
252b5132 RH |
6224 | case lang_wild_statement_enum: |
6225 | ||
420e579c | 6226 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6227 | current_os, fill, dot, found_end); |
252b5132 RH |
6228 | break; |
6229 | ||
6230 | case lang_object_symbols_statement_enum: | |
6231 | case lang_output_statement_enum: | |
6232 | case lang_target_statement_enum: | |
252b5132 | 6233 | break; |
e9ee469a | 6234 | |
252b5132 | 6235 | case lang_data_statement_enum: |
e9ee469a AM |
6236 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
6237 | if (expld.result.valid_p) | |
7542af2a AM |
6238 | { |
6239 | s->data_statement.value = expld.result.value; | |
6240 | if (expld.result.section != NULL) | |
6241 | s->data_statement.value += expld.result.section->vma; | |
6242 | } | |
f02cb058 | 6243 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6244 | einfo (_("%F%P: invalid data statement\n")); |
b7a26f91 KH |
6245 | { |
6246 | unsigned int size; | |
08da4cac KH |
6247 | switch (s->data_statement.type) |
6248 | { | |
6249 | default: | |
6250 | abort (); | |
6251 | case QUAD: | |
6252 | case SQUAD: | |
6253 | size = QUAD_SIZE; | |
6254 | break; | |
6255 | case LONG: | |
6256 | size = LONG_SIZE; | |
6257 | break; | |
6258 | case SHORT: | |
6259 | size = SHORT_SIZE; | |
6260 | break; | |
6261 | case BYTE: | |
6262 | size = BYTE_SIZE; | |
6263 | break; | |
6264 | } | |
e5caa5e0 AM |
6265 | if (size < TO_SIZE ((unsigned) 1)) |
6266 | size = TO_SIZE ((unsigned) 1); | |
6267 | dot += TO_ADDR (size); | |
08da4cac | 6268 | } |
252b5132 RH |
6269 | break; |
6270 | ||
6271 | case lang_reloc_statement_enum: | |
e9ee469a AM |
6272 | exp_fold_tree (s->reloc_statement.addend_exp, |
6273 | bfd_abs_section_ptr, &dot); | |
6274 | if (expld.result.valid_p) | |
6275 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6276 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6277 | einfo (_("%F%P: invalid reloc statement\n")); |
e5caa5e0 | 6278 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6279 | break; |
6280 | ||
6281 | case lang_input_section_enum: | |
6282 | { | |
6283 | asection *in = s->input_section.section; | |
6284 | ||
57ceae94 | 6285 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6286 | dot += TO_ADDR (in->size); |
252b5132 RH |
6287 | } |
6288 | break; | |
6289 | ||
6290 | case lang_input_statement_enum: | |
6291 | break; | |
e9ee469a | 6292 | |
252b5132 RH |
6293 | case lang_fill_statement_enum: |
6294 | fill = s->fill_statement.fill; | |
6295 | break; | |
252b5132 | 6296 | |
e9ee469a | 6297 | case lang_assignment_statement_enum: |
d2667025 AM |
6298 | current_assign = &s->assignment_statement; |
6299 | if (current_assign->exp->type.node_class != etree_assert) | |
6300 | { | |
6301 | const char *p = current_assign->exp->assign.dst; | |
6302 | ||
6303 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
6304 | prefer_next_section = TRUE; | |
6305 | ||
6306 | while (*p == '_') | |
6307 | ++p; | |
6308 | if (strcmp (p, "end") == 0) | |
6309 | *found_end = TRUE; | |
6310 | } | |
e9ee469a | 6311 | exp_fold_tree (s->assignment_statement.exp, |
f02cb058 AM |
6312 | (current_os->bfd_section != NULL |
6313 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6314 | &dot); |
252b5132 | 6315 | break; |
e9ee469a | 6316 | |
252b5132 | 6317 | case lang_padding_statement_enum: |
e5caa5e0 | 6318 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6319 | break; |
6320 | ||
6321 | case lang_group_statement_enum: | |
420e579c | 6322 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6323 | current_os, fill, dot, found_end); |
252b5132 RH |
6324 | break; |
6325 | ||
53d25da6 | 6326 | case lang_insert_statement_enum: |
252b5132 | 6327 | break; |
e9ee469a | 6328 | |
252b5132 RH |
6329 | case lang_address_statement_enum: |
6330 | break; | |
53d25da6 AM |
6331 | |
6332 | default: | |
6333 | FAIL (); | |
6334 | break; | |
252b5132 | 6335 | } |
252b5132 RH |
6336 | } |
6337 | return dot; | |
6338 | } | |
6339 | ||
f2241121 | 6340 | void |
2f65ac72 | 6341 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6342 | { |
d2667025 AM |
6343 | bfd_boolean found_end = FALSE; |
6344 | ||
6345 | current_section = NULL; | |
6346 | prefer_next_section = FALSE; | |
2f65ac72 | 6347 | expld.phase = phase; |
420e579c | 6348 | lang_statement_iteration++; |
d2667025 AM |
6349 | lang_do_assignments_1 (statement_list.head, |
6350 | abs_output_section, NULL, 0, &found_end); | |
6351 | } | |
6352 | ||
6353 | /* For an assignment statement outside of an output section statement, | |
6354 | choose the best of neighbouring output sections to use for values | |
6355 | of "dot". */ | |
6356 | ||
6357 | asection * | |
6358 | section_for_dot (void) | |
6359 | { | |
6360 | asection *s; | |
6361 | ||
6362 | /* Assignments belong to the previous output section, unless there | |
6363 | has been an assignment to "dot", in which case following | |
6364 | assignments belong to the next output section. (The assumption | |
6365 | is that an assignment to "dot" is setting up the address for the | |
6366 | next output section.) Except that past the assignment to "_end" | |
6367 | we always associate with the previous section. This exception is | |
6368 | for targets like SH that define an alloc .stack or other | |
6369 | weirdness after non-alloc sections. */ | |
6370 | if (current_section == NULL || prefer_next_section) | |
6371 | { | |
6372 | lang_statement_union_type *stmt; | |
6373 | lang_output_section_statement_type *os; | |
6374 | ||
6375 | for (stmt = (lang_statement_union_type *) current_assign; | |
6376 | stmt != NULL; | |
6377 | stmt = stmt->header.next) | |
6378 | if (stmt->header.type == lang_output_section_statement_enum) | |
6379 | break; | |
6380 | ||
6381 | os = &stmt->output_section_statement; | |
6382 | while (os != NULL | |
6383 | && !os->after_end | |
6384 | && (os->bfd_section == NULL | |
6385 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6386 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6387 | os->bfd_section))) | |
6388 | os = os->next; | |
6389 | ||
6390 | if (current_section == NULL || os == NULL || !os->after_end) | |
6391 | { | |
6392 | if (os != NULL) | |
6393 | s = os->bfd_section; | |
6394 | else | |
6395 | s = link_info.output_bfd->section_last; | |
6396 | while (s != NULL | |
6397 | && ((s->flags & SEC_ALLOC) == 0 | |
6398 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6399 | s = s->prev; | |
6400 | if (s != NULL) | |
6401 | return s; | |
6402 | ||
6403 | return bfd_abs_section_ptr; | |
6404 | } | |
6405 | } | |
6406 | ||
6407 | s = current_section->bfd_section; | |
6408 | ||
6409 | /* The section may have been stripped. */ | |
6410 | while (s != NULL | |
6411 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6412 | || (s->flags & SEC_ALLOC) == 0 | |
6413 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6414 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6415 | s = s->prev; | |
6416 | if (s == NULL) | |
6417 | s = link_info.output_bfd->sections; | |
6418 | while (s != NULL | |
6419 | && ((s->flags & SEC_ALLOC) == 0 | |
6420 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6421 | s = s->next; | |
6422 | if (s != NULL) | |
6423 | return s; | |
6424 | ||
6425 | return bfd_abs_section_ptr; | |
420e579c HPN |
6426 | } |
6427 | ||
7dba9362 AM |
6428 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ |
6429 | ||
6430 | static struct bfd_link_hash_entry **start_stop_syms; | |
6431 | static size_t start_stop_count = 0; | |
6432 | static size_t start_stop_alloc = 0; | |
6433 | ||
6434 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6435 | to start_stop_syms. */ | |
6436 | ||
6437 | static void | |
6438 | lang_define_start_stop (const char *symbol, asection *sec) | |
6439 | { | |
6440 | struct bfd_link_hash_entry *h; | |
6441 | ||
6442 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6443 | if (h != NULL) | |
6444 | { | |
6445 | if (start_stop_count == start_stop_alloc) | |
6446 | { | |
6447 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6448 | start_stop_syms | |
6449 | = xrealloc (start_stop_syms, | |
6450 | start_stop_alloc * sizeof (*start_stop_syms)); | |
6451 | } | |
6452 | start_stop_syms[start_stop_count++] = h; | |
6453 | } | |
6454 | } | |
6455 | ||
6456 | /* Check for input sections whose names match references to | |
6457 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
6458 | preliminary definitions. */ | |
252b5132 RH |
6459 | |
6460 | static void | |
7dba9362 | 6461 | lang_init_start_stop (void) |
252b5132 | 6462 | { |
7dba9362 | 6463 | bfd *abfd; |
252b5132 | 6464 | asection *s; |
7dba9362 AM |
6465 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); |
6466 | ||
6467 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
6468 | for (s = abfd->sections; s != NULL; s = s->next) | |
6469 | { | |
6470 | const char *ps; | |
6471 | const char *secname = s->name; | |
6472 | ||
6473 | for (ps = secname; *ps != '\0'; ps++) | |
6474 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
6475 | break; | |
6476 | if (*ps == '\0') | |
6477 | { | |
6478 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
b27685f2 | 6479 | |
7dba9362 AM |
6480 | symbol[0] = leading_char; |
6481 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
6482 | lang_define_start_stop (symbol, s); | |
6483 | ||
6484 | symbol[1] = leading_char; | |
6485 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
6486 | lang_define_start_stop (symbol + 1, s); | |
6487 | ||
6488 | free (symbol); | |
6489 | } | |
6490 | } | |
6491 | } | |
6492 | ||
6493 | /* Iterate over start_stop_syms. */ | |
6494 | ||
6495 | static void | |
6496 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
6497 | { | |
6498 | size_t i; | |
6499 | ||
6500 | for (i = 0; i < start_stop_count; ++i) | |
6501 | func (start_stop_syms[i]); | |
6502 | } | |
6503 | ||
6504 | /* __start and __stop symbols are only supposed to be defined by the | |
6505 | linker for orphan sections, but we now extend that to sections that | |
6506 | map to an output section of the same name. The symbols were | |
6507 | defined early for --gc-sections, before we mapped input to output | |
6508 | sections, so undo those that don't satisfy this rule. */ | |
6509 | ||
6510 | static void | |
6511 | undef_start_stop (struct bfd_link_hash_entry *h) | |
6512 | { | |
6513 | if (h->ldscript_def) | |
b27685f2 L |
6514 | return; |
6515 | ||
7dba9362 AM |
6516 | if (h->u.def.section->output_section == NULL |
6517 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
6518 | || strcmp (h->u.def.section->name, | |
6519 | h->u.def.section->output_section->name) != 0) | |
6520 | { | |
4d1c6335 L |
6521 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, |
6522 | h->u.def.section->name); | |
6523 | if (sec != NULL) | |
6524 | { | |
6525 | /* When there are more than one input sections with the same | |
6526 | section name, SECNAME, linker picks the first one to define | |
6527 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
6528 | input section is removed by comdat group, we need to check | |
6529 | if there is still an output section with section name | |
6530 | SECNAME. */ | |
6531 | asection *i; | |
6532 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
6533 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
6534 | { | |
6535 | h->u.def.section = i; | |
6536 | return; | |
6537 | } | |
6538 | } | |
7dba9362 AM |
6539 | h->type = bfd_link_hash_undefined; |
6540 | h->u.undef.abfd = NULL; | |
6541 | } | |
6542 | } | |
252b5132 | 6543 | |
7dba9362 AM |
6544 | static void |
6545 | lang_undef_start_stop (void) | |
6546 | { | |
6547 | foreach_start_stop (undef_start_stop); | |
6548 | } | |
6549 | ||
6550 | /* Check for output sections whose names match references to | |
6551 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
6552 | preliminary definitions. */ | |
6553 | ||
6554 | static void | |
6555 | lang_init_startof_sizeof (void) | |
6556 | { | |
6557 | asection *s; | |
252b5132 | 6558 | |
f13a99db | 6559 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6560 | { |
7dba9362 AM |
6561 | const char *secname = s->name; |
6562 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 6563 | |
7dba9362 AM |
6564 | sprintf (symbol, ".startof.%s", secname); |
6565 | lang_define_start_stop (symbol, s); | |
252b5132 | 6566 | |
7dba9362 AM |
6567 | memcpy (symbol + 1, ".size", 5); |
6568 | lang_define_start_stop (symbol + 1, s); | |
6569 | free (symbol); | |
6570 | } | |
6571 | } | |
cbd0eecf | 6572 | |
7dba9362 AM |
6573 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ |
6574 | ||
6575 | static void | |
6576 | set_start_stop (struct bfd_link_hash_entry *h) | |
6577 | { | |
6578 | if (h->ldscript_def | |
6579 | || h->type != bfd_link_hash_defined) | |
6580 | return; | |
cbd0eecf | 6581 | |
7dba9362 AM |
6582 | if (h->root.string[0] == '.') |
6583 | { | |
6584 | /* .startof. or .sizeof. symbol. | |
6585 | .startof. already has final value. */ | |
6586 | if (h->root.string[2] == 'i') | |
252b5132 | 6587 | { |
7dba9362 AM |
6588 | /* .sizeof. */ |
6589 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
6590 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 6591 | } |
7dba9362 AM |
6592 | } |
6593 | else | |
6594 | { | |
6595 | /* __start or __stop symbol. */ | |
6596 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 6597 | |
7dba9362 AM |
6598 | h->u.def.section = h->u.def.section->output_section; |
6599 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 6600 | { |
7dba9362 AM |
6601 | /* __stop_ */ |
6602 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 6603 | } |
252b5132 RH |
6604 | } |
6605 | } | |
6606 | ||
7dba9362 AM |
6607 | static void |
6608 | lang_finalize_start_stop (void) | |
6609 | { | |
6610 | foreach_start_stop (set_start_stop); | |
6611 | } | |
6612 | ||
252b5132 | 6613 | static void |
750877ba | 6614 | lang_end (void) |
252b5132 RH |
6615 | { |
6616 | struct bfd_link_hash_entry *h; | |
b34976b6 | 6617 | bfd_boolean warn; |
252b5132 | 6618 | |
0e1862bb L |
6619 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
6620 | || bfd_link_dll (&link_info)) | |
71934f94 | 6621 | warn = entry_from_cmdline; |
252b5132 | 6622 | else |
b34976b6 | 6623 | warn = TRUE; |
252b5132 | 6624 | |
ac69cbc6 | 6625 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
6626 | --gc-sections, unless --gc-keep-exported was also given. */ |
6627 | if (bfd_link_relocatable (&link_info) | |
6628 | && link_info.gc_sections | |
6629 | && !link_info.gc_keep_exported | |
24898b70 | 6630 | && !(entry_from_cmdline || undef_from_cmdline)) |
df5f2391 | 6631 | einfo (_("%F%P: gc-sections requires either an entry or " |
ac69cbc6 TG |
6632 | "an undefined symbol\n")); |
6633 | ||
1579bae1 | 6634 | if (entry_symbol.name == NULL) |
252b5132 | 6635 | { |
a359509e ZW |
6636 | /* No entry has been specified. Look for the default entry, but |
6637 | don't warn if we don't find it. */ | |
6638 | entry_symbol.name = entry_symbol_default; | |
b34976b6 | 6639 | warn = FALSE; |
252b5132 RH |
6640 | } |
6641 | ||
e3e942e9 | 6642 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
b34976b6 | 6643 | FALSE, FALSE, TRUE); |
1579bae1 | 6644 | if (h != NULL |
252b5132 RH |
6645 | && (h->type == bfd_link_hash_defined |
6646 | || h->type == bfd_link_hash_defweak) | |
6647 | && h->u.def.section->output_section != NULL) | |
6648 | { | |
6649 | bfd_vma val; | |
6650 | ||
6651 | val = (h->u.def.value | |
fd361982 | 6652 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 6653 | + h->u.def.section->output_offset); |
0aa7f586 | 6654 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6655 | einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
6656 | } |
6657 | else | |
6658 | { | |
6659 | bfd_vma val; | |
5f992e62 | 6660 | const char *send; |
252b5132 RH |
6661 | |
6662 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 6663 | number. */ |
e3e942e9 | 6664 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
6665 | if (*send == '\0') |
6666 | { | |
0aa7f586 | 6667 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6668 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
6669 | } |
6670 | else | |
6671 | { | |
6672 | asection *ts; | |
6673 | ||
6674 | /* Can't find the entry symbol, and it's not a number. Use | |
6675 | the first address in the text section. */ | |
f13a99db | 6676 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 6677 | if (ts != NULL) |
252b5132 RH |
6678 | { |
6679 | if (warn) | |
6feb9908 AM |
6680 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6681 | " defaulting to %V\n"), | |
e3e942e9 | 6682 | entry_symbol.name, |
fd361982 AM |
6683 | bfd_section_vma (ts)); |
6684 | if (!bfd_set_start_address (link_info.output_bfd, | |
6685 | bfd_section_vma (ts))) | |
df5f2391 | 6686 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
6687 | } |
6688 | else | |
6689 | { | |
6690 | if (warn) | |
6feb9908 AM |
6691 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6692 | " not setting start address\n"), | |
e3e942e9 | 6693 | entry_symbol.name); |
252b5132 RH |
6694 | } |
6695 | } | |
6696 | } | |
6697 | } | |
6698 | ||
6699 | /* This is a small function used when we want to ignore errors from | |
6700 | BFD. */ | |
6701 | ||
6702 | static void | |
52d45da3 AM |
6703 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
6704 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
6705 | { |
6706 | /* Don't do anything. */ | |
6707 | } | |
6708 | ||
6709 | /* Check that the architecture of all the input files is compatible | |
6710 | with the output file. Also call the backend to let it do any | |
6711 | other checking that is needed. */ | |
6712 | ||
6713 | static void | |
1579bae1 | 6714 | lang_check (void) |
252b5132 | 6715 | { |
36983a93 | 6716 | lang_input_statement_type *file; |
252b5132 | 6717 | bfd *input_bfd; |
5f992e62 | 6718 | const bfd_arch_info_type *compatible; |
252b5132 | 6719 | |
36983a93 AM |
6720 | for (file = &file_chain.head->input_statement; |
6721 | file != NULL; | |
6722 | file = file->next) | |
252b5132 | 6723 | { |
422b6f14 DK |
6724 | #ifdef ENABLE_PLUGINS |
6725 | /* Don't check format of files claimed by plugin. */ | |
36983a93 | 6726 | if (file->flags.claimed) |
422b6f14 DK |
6727 | continue; |
6728 | #endif /* ENABLE_PLUGINS */ | |
36983a93 | 6729 | input_bfd = file->the_bfd; |
6feb9908 | 6730 | compatible |
f13a99db | 6731 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 6732 | command_line.accept_unknown_input_arch); |
30cba025 AM |
6733 | |
6734 | /* In general it is not possible to perform a relocatable | |
6735 | link between differing object formats when the input | |
6736 | file has relocations, because the relocations in the | |
6737 | input format may not have equivalent representations in | |
6738 | the output format (and besides BFD does not translate | |
6739 | relocs for other link purposes than a final link). */ | |
0e1862bb L |
6740 | if ((bfd_link_relocatable (&link_info) |
6741 | || link_info.emitrelocations) | |
30cba025 | 6742 | && (compatible == NULL |
f13a99db AM |
6743 | || (bfd_get_flavour (input_bfd) |
6744 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
6745 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
6746 | { | |
df5f2391 | 6747 | einfo (_("%F%P: relocatable linking with relocations from" |
871b3ab2 | 6748 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 6749 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 6750 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
6751 | /* einfo with %F exits. */ |
6752 | } | |
6753 | ||
252b5132 RH |
6754 | if (compatible == NULL) |
6755 | { | |
6756 | if (command_line.warn_mismatch) | |
df5f2391 | 6757 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 6758 | " is incompatible with %s output\n"), |
252b5132 | 6759 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 6760 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 6761 | } |
b9247304 | 6762 | else if (bfd_count_sections (input_bfd)) |
252b5132 | 6763 | { |
b9247304 | 6764 | /* If the input bfd has no contents, it shouldn't set the |
b7a26f91 | 6765 | private data of the output bfd. */ |
b9247304 | 6766 | |
252b5132 RH |
6767 | bfd_error_handler_type pfn = NULL; |
6768 | ||
6769 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
6770 | files, temporarily set the BFD error handler to a |
6771 | function which will do nothing. We still want to call | |
6772 | bfd_merge_private_bfd_data, since it may set up | |
6773 | information which is needed in the output file. */ | |
0aa7f586 | 6774 | if (!command_line.warn_mismatch) |
252b5132 | 6775 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 6776 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
6777 | { |
6778 | if (command_line.warn_mismatch) | |
df5f2391 | 6779 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 6780 | " of file %pB\n"), input_bfd); |
252b5132 | 6781 | } |
0aa7f586 | 6782 | if (!command_line.warn_mismatch) |
252b5132 RH |
6783 | bfd_set_error_handler (pfn); |
6784 | } | |
6785 | } | |
6786 | } | |
6787 | ||
6788 | /* Look through all the global common symbols and attach them to the | |
6789 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 6790 | to roughly sort the entries by alignment. */ |
252b5132 RH |
6791 | |
6792 | static void | |
1579bae1 | 6793 | lang_common (void) |
252b5132 | 6794 | { |
a4819f54 | 6795 | if (link_info.inhibit_common_definition) |
4818e05f | 6796 | return; |
0e1862bb | 6797 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 6798 | && !command_line.force_common_definition) |
252b5132 RH |
6799 | return; |
6800 | ||
0aa7f586 | 6801 | if (!config.sort_common) |
1579bae1 | 6802 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
6803 | else |
6804 | { | |
de7dd2bd NC |
6805 | unsigned int power; |
6806 | ||
6807 | if (config.sort_common == sort_descending) | |
967928e9 AM |
6808 | { |
6809 | for (power = 4; power > 0; power--) | |
6810 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
6811 | ||
6812 | power = 0; | |
6813 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
6814 | } | |
de7dd2bd | 6815 | else |
967928e9 AM |
6816 | { |
6817 | for (power = 0; power <= 4; power++) | |
6818 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 6819 | |
2dc0e7b4 | 6820 | power = (unsigned int) -1; |
de7dd2bd NC |
6821 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
6822 | } | |
252b5132 RH |
6823 | } |
6824 | } | |
6825 | ||
6826 | /* Place one common symbol in the correct section. */ | |
6827 | ||
b34976b6 | 6828 | static bfd_boolean |
1579bae1 | 6829 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
6830 | { |
6831 | unsigned int power_of_two; | |
6832 | bfd_vma size; | |
6833 | asection *section; | |
6834 | ||
6835 | if (h->type != bfd_link_hash_common) | |
b34976b6 | 6836 | return TRUE; |
252b5132 RH |
6837 | |
6838 | size = h->u.c.size; | |
6839 | power_of_two = h->u.c.p->alignment_power; | |
6840 | ||
de7dd2bd NC |
6841 | if (config.sort_common == sort_descending |
6842 | && power_of_two < *(unsigned int *) info) | |
6843 | return TRUE; | |
6844 | else if (config.sort_common == sort_ascending | |
967928e9 | 6845 | && power_of_two > *(unsigned int *) info) |
b34976b6 | 6846 | return TRUE; |
252b5132 RH |
6847 | |
6848 | section = h->u.c.p->section; | |
3023e3f6 | 6849 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
df5f2391 | 6850 | einfo (_("%F%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 6851 | h->root.string); |
252b5132 RH |
6852 | |
6853 | if (config.map_file != NULL) | |
6854 | { | |
b34976b6 | 6855 | static bfd_boolean header_printed; |
252b5132 RH |
6856 | int len; |
6857 | char *name; | |
6858 | char buf[50]; | |
6859 | ||
0aa7f586 | 6860 | if (!header_printed) |
252b5132 RH |
6861 | { |
6862 | minfo (_("\nAllocating common symbols\n")); | |
6863 | minfo (_("Common symbol size file\n\n")); | |
b34976b6 | 6864 | header_printed = TRUE; |
252b5132 RH |
6865 | } |
6866 | ||
f13a99db | 6867 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 6868 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
6869 | if (name == NULL) |
6870 | { | |
6871 | minfo ("%s", h->root.string); | |
6872 | len = strlen (h->root.string); | |
6873 | } | |
6874 | else | |
6875 | { | |
6876 | minfo ("%s", name); | |
6877 | len = strlen (name); | |
6878 | free (name); | |
6879 | } | |
252b5132 RH |
6880 | |
6881 | if (len >= 19) | |
6882 | { | |
6883 | print_nl (); | |
6884 | len = 0; | |
6885 | } | |
6886 | while (len < 20) | |
6887 | { | |
6888 | print_space (); | |
6889 | ++len; | |
6890 | } | |
6891 | ||
6892 | minfo ("0x"); | |
6893 | if (size <= 0xffffffff) | |
6894 | sprintf (buf, "%lx", (unsigned long) size); | |
6895 | else | |
6896 | sprintf_vma (buf, size); | |
6897 | minfo ("%s", buf); | |
6898 | len = strlen (buf); | |
6899 | ||
6900 | while (len < 16) | |
6901 | { | |
6902 | print_space (); | |
6903 | ++len; | |
6904 | } | |
6905 | ||
871b3ab2 | 6906 | minfo ("%pB\n", section->owner); |
252b5132 RH |
6907 | } |
6908 | ||
b34976b6 | 6909 | return TRUE; |
252b5132 RH |
6910 | } |
6911 | ||
c005eb9e AB |
6912 | /* Handle a single orphan section S, placing the orphan into an appropriate |
6913 | output section. The effects of the --orphan-handling command line | |
6914 | option are handled here. */ | |
6915 | ||
6916 | static void | |
6917 | ldlang_place_orphan (asection *s) | |
6918 | { | |
6919 | if (config.orphan_handling == orphan_handling_discard) | |
6920 | { | |
6921 | lang_output_section_statement_type *os; | |
6922 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, | |
6923 | TRUE); | |
6924 | if (os->addr_tree == NULL | |
6925 | && (bfd_link_relocatable (&link_info) | |
6926 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
6927 | os->addr_tree = exp_intop (0); | |
6928 | lang_add_section (&os->children, s, NULL, os); | |
6929 | } | |
6930 | else | |
6931 | { | |
6932 | lang_output_section_statement_type *os; | |
6933 | const char *name = s->name; | |
6934 | int constraint = 0; | |
6935 | ||
6936 | if (config.orphan_handling == orphan_handling_error) | |
df5f2391 | 6937 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
6938 | s, s->owner); |
6939 | ||
6940 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
6941 | constraint = SPECIAL; | |
6942 | ||
6943 | os = ldemul_place_orphan (s, name, constraint); | |
6944 | if (os == NULL) | |
6945 | { | |
6946 | os = lang_output_section_statement_lookup (name, constraint, TRUE); | |
6947 | if (os->addr_tree == NULL | |
6948 | && (bfd_link_relocatable (&link_info) | |
6949 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
6950 | os->addr_tree = exp_intop (0); | |
6951 | lang_add_section (&os->children, s, NULL, os); | |
6952 | } | |
6953 | ||
6954 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 6955 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 6956 | "placed in section `%s'\n"), |
c005eb9e AB |
6957 | s, s->owner, os->name); |
6958 | } | |
6959 | } | |
6960 | ||
08da4cac KH |
6961 | /* Run through the input files and ensure that every input section has |
6962 | somewhere to go. If one is found without a destination then create | |
6963 | an input request and place it into the statement tree. */ | |
252b5132 RH |
6964 | |
6965 | static void | |
1579bae1 | 6966 | lang_place_orphans (void) |
252b5132 | 6967 | { |
e50d8076 | 6968 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
6969 | { |
6970 | asection *s; | |
6971 | ||
1579bae1 | 6972 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6973 | { |
1579bae1 | 6974 | if (s->output_section == NULL) |
252b5132 | 6975 | { |
396a2467 | 6976 | /* This section of the file is not attached, root |
afd7a018 | 6977 | around for a sensible place for it to go. */ |
252b5132 | 6978 | |
66be1055 | 6979 | if (file->flags.just_syms) |
1449d79b | 6980 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 6981 | else if (lang_discard_section_p (s)) |
164e712d | 6982 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
6983 | else if (strcmp (s->name, "COMMON") == 0) |
6984 | { | |
6985 | /* This is a lonely common section which must have | |
6986 | come from an archive. We attach to the section | |
6987 | with the wildcard. */ | |
0e1862bb | 6988 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
6989 | || command_line.force_common_definition) |
6990 | { | |
6991 | if (default_common_section == NULL) | |
66c103b7 AM |
6992 | default_common_section |
6993 | = lang_output_section_statement_lookup (".bss", 0, | |
6994 | TRUE); | |
39dcfe18 | 6995 | lang_add_section (&default_common_section->children, s, |
b9c361e0 | 6996 | NULL, default_common_section); |
252b5132 RH |
6997 | } |
6998 | } | |
252b5132 | 6999 | else |
c005eb9e | 7000 | ldlang_place_orphan (s); |
252b5132 RH |
7001 | } |
7002 | } | |
7003 | } | |
7004 | } | |
7005 | ||
252b5132 | 7006 | void |
1579bae1 | 7007 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7008 | { |
aa8804e4 | 7009 | flagword *ptr_flags; |
252b5132 | 7010 | |
aa8804e4 | 7011 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7012 | |
252b5132 RH |
7013 | while (*flags) |
7014 | { | |
7015 | switch (*flags) | |
7016 | { | |
f0673d20 NC |
7017 | /* PR 17900: An exclamation mark in the attributes reverses |
7018 | the sense of any of the attributes that follow. */ | |
7019 | case '!': | |
0aa7f586 | 7020 | invert = !invert; |
f0673d20 NC |
7021 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7022 | break; | |
7023 | ||
252b5132 RH |
7024 | case 'A': case 'a': |
7025 | *ptr_flags |= SEC_ALLOC; | |
7026 | break; | |
7027 | ||
7028 | case 'R': case 'r': | |
7029 | *ptr_flags |= SEC_READONLY; | |
7030 | break; | |
7031 | ||
7032 | case 'W': case 'w': | |
7033 | *ptr_flags |= SEC_DATA; | |
7034 | break; | |
7035 | ||
7036 | case 'X': case 'x': | |
7037 | *ptr_flags |= SEC_CODE; | |
7038 | break; | |
7039 | ||
7040 | case 'L': case 'l': | |
7041 | case 'I': case 'i': | |
7042 | *ptr_flags |= SEC_LOAD; | |
7043 | break; | |
7044 | ||
7045 | default: | |
df5f2391 | 7046 | einfo (_("%F%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7047 | *flags, *flags); |
252b5132 RH |
7048 | break; |
7049 | } | |
7050 | flags++; | |
7051 | } | |
7052 | } | |
7053 | ||
d871d478 AM |
7054 | /* Call a function on each real input file. This function will be |
7055 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7056 | |
7057 | void | |
1579bae1 | 7058 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7059 | { |
7060 | lang_input_statement_type *f; | |
7061 | ||
1fa4ec6a | 7062 | for (f = &input_file_chain.head->input_statement; |
252b5132 | 7063 | f != NULL; |
36983a93 | 7064 | f = f->next_real_file) |
d871d478 AM |
7065 | if (f->flags.real) |
7066 | func (f); | |
252b5132 RH |
7067 | } |
7068 | ||
d871d478 AM |
7069 | /* Call a function on each real file. The function will be called on |
7070 | all the elements of an archive which are included in the link, but | |
7071 | will not be called on the archive file itself. */ | |
252b5132 RH |
7072 | |
7073 | void | |
1579bae1 | 7074 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7075 | { |
e50d8076 | 7076 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7077 | { |
d871d478 AM |
7078 | if (f->flags.real) |
7079 | func (f); | |
252b5132 RH |
7080 | } |
7081 | } | |
7082 | ||
252b5132 | 7083 | void |
1579bae1 | 7084 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7085 | { |
36983a93 | 7086 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7087 | |
7088 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7089 | a link. */ | |
c72f2fb2 | 7090 | ASSERT (entry->the_bfd->link.next == NULL); |
f13a99db | 7091 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7092 | |
7093 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7094 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7095 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
7096 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
7097 | ||
7098 | /* Look through the sections and check for any which should not be | |
7099 | included in the link. We need to do this now, so that we can | |
7100 | notice when the backend linker tries to report multiple | |
7101 | definition errors for symbols which are in sections we aren't | |
7102 | going to link. FIXME: It might be better to entirely ignore | |
7103 | symbols which are defined in sections which are going to be | |
7104 | discarded. This would require modifying the backend linker for | |
7105 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7106 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7107 | ||
1579bae1 | 7108 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7109 | } |
7110 | ||
7111 | void | |
1579bae1 | 7112 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7113 | { |
7114 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7115 | if (!had_output_filename || !from_script) |
252b5132 RH |
7116 | { |
7117 | output_filename = name; | |
b34976b6 | 7118 | had_output_filename = TRUE; |
252b5132 RH |
7119 | } |
7120 | } | |
7121 | ||
aea4bd9d | 7122 | lang_output_section_statement_type * |
1579bae1 AM |
7123 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7124 | etree_type *address_exp, | |
7125 | enum section_type sectype, | |
1579bae1 AM |
7126 | etree_type *align, |
7127 | etree_type *subalign, | |
0841712e | 7128 | etree_type *ebase, |
1eec346e NC |
7129 | int constraint, |
7130 | int align_with_input) | |
252b5132 RH |
7131 | { |
7132 | lang_output_section_statement_type *os; | |
7133 | ||
66c103b7 AM |
7134 | os = lang_output_section_statement_lookup (output_section_statement_name, |
7135 | constraint, TRUE); | |
967928e9 | 7136 | current_section = os; |
252b5132 | 7137 | |
1579bae1 | 7138 | if (os->addr_tree == NULL) |
08da4cac KH |
7139 | { |
7140 | os->addr_tree = address_exp; | |
7141 | } | |
252b5132 RH |
7142 | os->sectype = sectype; |
7143 | if (sectype != noload_section) | |
7144 | os->flags = SEC_NO_FLAGS; | |
7145 | else | |
7146 | os->flags = SEC_NEVER_LOAD; | |
e5caa5e0 | 7147 | os->block_value = 1; |
66c103b7 AM |
7148 | |
7149 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 7150 | push_stat_ptr (&os->children); |
252b5132 | 7151 | |
1eec346e NC |
7152 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7153 | if (os->align_lma_with_input && align != NULL) | |
c1c8c1ef | 7154 | einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7155 | NULL); |
1eec346e | 7156 | |
3d9c8f6b AM |
7157 | os->subsection_alignment = subalign; |
7158 | os->section_alignment = align; | |
252b5132 RH |
7159 | |
7160 | os->load_base = ebase; | |
aea4bd9d | 7161 | return os; |
252b5132 RH |
7162 | } |
7163 | ||
252b5132 | 7164 | void |
1579bae1 | 7165 | lang_final (void) |
252b5132 | 7166 | { |
d3ce72d0 NC |
7167 | lang_output_statement_type *new_stmt; |
7168 | ||
7169 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7170 | new_stmt->name = output_filename; | |
252b5132 RH |
7171 | } |
7172 | ||
08da4cac KH |
7173 | /* Reset the current counters in the regions. */ |
7174 | ||
e3dc8847 | 7175 | void |
1579bae1 | 7176 | lang_reset_memory_regions (void) |
252b5132 RH |
7177 | { |
7178 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7179 | asection *o; |
e9ee469a | 7180 | lang_output_section_statement_type *os; |
252b5132 | 7181 | |
1579bae1 | 7182 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7183 | { |
252b5132 | 7184 | p->current = p->origin; |
66e28d60 | 7185 | p->last_os = NULL; |
252b5132 | 7186 | } |
b3327aad | 7187 | |
5c1e6d53 | 7188 | for (os = &lang_os_list.head->output_section_statement; |
e9ee469a AM |
7189 | os != NULL; |
7190 | os = os->next) | |
cde9e0be AM |
7191 | { |
7192 | os->processed_vma = FALSE; | |
7193 | os->processed_lma = FALSE; | |
7194 | } | |
e9ee469a | 7195 | |
f13a99db | 7196 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7197 | { |
7198 | /* Save the last size for possible use by bfd_relax_section. */ | |
7199 | o->rawsize = o->size; | |
8772de11 MR |
7200 | if (!(o->flags & SEC_FIXED_SIZE)) |
7201 | o->size = 0; | |
1a23a9e6 | 7202 | } |
252b5132 RH |
7203 | } |
7204 | ||
164e712d | 7205 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7206 | |
7207 | static void | |
1579bae1 AM |
7208 | gc_section_callback (lang_wild_statement_type *ptr, |
7209 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7210 | asection *section, | |
b9c361e0 | 7211 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
1579bae1 AM |
7212 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
7213 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7214 | { |
164e712d AM |
7215 | /* If the wild pattern was marked KEEP, the member sections |
7216 | should be as well. */ | |
4dec4d4e RH |
7217 | if (ptr->keep_sections) |
7218 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7219 | } |
7220 | ||
252b5132 RH |
7221 | /* Iterate over sections marking them against GC. */ |
7222 | ||
7223 | static void | |
1579bae1 | 7224 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7225 | { |
1579bae1 | 7226 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7227 | { |
7228 | switch (s->header.type) | |
7229 | { | |
7230 | case lang_wild_statement_enum: | |
164e712d | 7231 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7232 | break; |
252b5132 RH |
7233 | case lang_constructors_statement_enum: |
7234 | lang_gc_sections_1 (constructor_list.head); | |
7235 | break; | |
7236 | case lang_output_section_statement_enum: | |
7237 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7238 | break; | |
7239 | case lang_group_statement_enum: | |
7240 | lang_gc_sections_1 (s->group_statement.children.head); | |
7241 | break; | |
7242 | default: | |
7243 | break; | |
7244 | } | |
7245 | } | |
7246 | } | |
7247 | ||
7248 | static void | |
1579bae1 | 7249 | lang_gc_sections (void) |
252b5132 | 7250 | { |
252b5132 | 7251 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7252 | lang_gc_sections_1 (statement_list.head); |
7253 | ||
9ca57817 AM |
7254 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
7255 | the special case of debug info. (See bfd/stabs.c) | |
7256 | Twiddle the flag here, to simplify later linker code. */ | |
0e1862bb | 7257 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7258 | { |
7259 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7260 | { | |
7261 | asection *sec; | |
9e2278f5 | 7262 | #ifdef ENABLE_PLUGINS |
66be1055 | 7263 | if (f->flags.claimed) |
9e2278f5 AM |
7264 | continue; |
7265 | #endif | |
9ca57817 AM |
7266 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
7267 | if ((sec->flags & SEC_DEBUGGING) == 0) | |
7268 | sec->flags &= ~SEC_EXCLUDE; | |
7269 | } | |
7270 | } | |
7271 | ||
57316bff | 7272 | if (link_info.gc_sections) |
f13a99db | 7273 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7274 | } |
7275 | ||
b10a8ae0 L |
7276 | /* Worker for lang_find_relro_sections_1. */ |
7277 | ||
7278 | static void | |
7279 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7280 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7281 | asection *section, | |
b9c361e0 | 7282 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
b10a8ae0 L |
7283 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
7284 | void *data) | |
7285 | { | |
7286 | /* Discarded, excluded and ignored sections effectively have zero | |
7287 | size. */ | |
7288 | if (section->output_section != NULL | |
f13a99db | 7289 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7290 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7291 | && !IGNORE_SECTION (section) | |
7292 | && section->size != 0) | |
7293 | { | |
7294 | bfd_boolean *has_relro_section = (bfd_boolean *) data; | |
7295 | *has_relro_section = TRUE; | |
7296 | } | |
7297 | } | |
7298 | ||
7299 | /* Iterate over sections for relro sections. */ | |
7300 | ||
7301 | static void | |
7302 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
ed1794ee | 7303 | seg_align_type *seg, |
b10a8ae0 L |
7304 | bfd_boolean *has_relro_section) |
7305 | { | |
7306 | if (*has_relro_section) | |
7307 | return; | |
7308 | ||
7309 | for (; s != NULL; s = s->header.next) | |
7310 | { | |
ed1794ee | 7311 | if (s == seg->relro_end_stat) |
b10a8ae0 L |
7312 | break; |
7313 | ||
7314 | switch (s->header.type) | |
7315 | { | |
7316 | case lang_wild_statement_enum: | |
7317 | walk_wild (&s->wild_statement, | |
7318 | find_relro_section_callback, | |
7319 | has_relro_section); | |
7320 | break; | |
7321 | case lang_constructors_statement_enum: | |
7322 | lang_find_relro_sections_1 (constructor_list.head, | |
ed1794ee | 7323 | seg, has_relro_section); |
b10a8ae0 L |
7324 | break; |
7325 | case lang_output_section_statement_enum: | |
7326 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
ed1794ee | 7327 | seg, has_relro_section); |
b10a8ae0 L |
7328 | break; |
7329 | case lang_group_statement_enum: | |
7330 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
ed1794ee | 7331 | seg, has_relro_section); |
b10a8ae0 L |
7332 | break; |
7333 | default: | |
7334 | break; | |
7335 | } | |
7336 | } | |
7337 | } | |
7338 | ||
7339 | static void | |
7340 | lang_find_relro_sections (void) | |
7341 | { | |
7342 | bfd_boolean has_relro_section = FALSE; | |
7343 | ||
7344 | /* Check all sections in the link script. */ | |
7345 | ||
7346 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
ed1794ee | 7347 | &expld.dataseg, &has_relro_section); |
b10a8ae0 L |
7348 | |
7349 | if (!has_relro_section) | |
7350 | link_info.relro = FALSE; | |
7351 | } | |
7352 | ||
fbbc3759 L |
7353 | /* Relax all sections until bfd_relax_section gives up. */ |
7354 | ||
eaeb0a9d AM |
7355 | void |
7356 | lang_relax_sections (bfd_boolean need_layout) | |
fbbc3759 | 7357 | { |
28d5f677 | 7358 | if (RELAXATION_ENABLED) |
fbbc3759 | 7359 | { |
eaeb0a9d AM |
7360 | /* We may need more than one relaxation pass. */ |
7361 | int i = link_info.relax_pass; | |
fbbc3759 | 7362 | |
eaeb0a9d AM |
7363 | /* The backend can use it to determine the current pass. */ |
7364 | link_info.relax_pass = 0; | |
fbbc3759 | 7365 | |
eaeb0a9d AM |
7366 | while (i--) |
7367 | { | |
7368 | /* Keep relaxing until bfd_relax_section gives up. */ | |
7369 | bfd_boolean relax_again; | |
fbbc3759 | 7370 | |
eaeb0a9d AM |
7371 | link_info.relax_trip = -1; |
7372 | do | |
7373 | { | |
7374 | link_info.relax_trip++; | |
7375 | ||
7376 | /* Note: pe-dll.c does something like this also. If you find | |
7377 | you need to change this code, you probably need to change | |
7378 | pe-dll.c also. DJ */ | |
7379 | ||
7380 | /* Do all the assignments with our current guesses as to | |
7381 | section sizes. */ | |
2f65ac72 | 7382 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7383 | |
7384 | /* We must do this after lang_do_assignments, because it uses | |
7385 | size. */ | |
7386 | lang_reset_memory_regions (); | |
7387 | ||
7388 | /* Perform another relax pass - this time we know where the | |
7389 | globals are, so can make a better guess. */ | |
7390 | relax_again = FALSE; | |
7391 | lang_size_sections (&relax_again, FALSE); | |
7392 | } | |
7393 | while (relax_again); | |
7394 | ||
7395 | link_info.relax_pass++; | |
7396 | } | |
7397 | need_layout = TRUE; | |
7398 | } | |
fbbc3759 | 7399 | |
eaeb0a9d AM |
7400 | if (need_layout) |
7401 | { | |
7402 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 7403 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7404 | lang_reset_memory_regions (); |
7405 | lang_size_sections (NULL, TRUE); | |
fbbc3759 | 7406 | } |
fbbc3759 L |
7407 | } |
7408 | ||
8543fde5 DK |
7409 | #ifdef ENABLE_PLUGINS |
7410 | /* Find the insert point for the plugin's replacement files. We | |
7411 | place them after the first claimed real object file, or if the | |
7412 | first claimed object is an archive member, after the last real | |
7413 | object file immediately preceding the archive. In the event | |
7414 | no objects have been claimed at all, we return the first dummy | |
7415 | object file on the list as the insert point; that works, but | |
7416 | the callee must be careful when relinking the file_chain as it | |
7417 | is not actually on that chain, only the statement_list and the | |
7418 | input_file list; in that case, the replacement files must be | |
7419 | inserted at the head of the file_chain. */ | |
7420 | ||
7421 | static lang_input_statement_type * | |
128bf1fe | 7422 | find_replacements_insert_point (bfd_boolean *before) |
8543fde5 DK |
7423 | { |
7424 | lang_input_statement_type *claim1, *lastobject; | |
7425 | lastobject = &input_file_chain.head->input_statement; | |
7426 | for (claim1 = &file_chain.head->input_statement; | |
7427 | claim1 != NULL; | |
36983a93 | 7428 | claim1 = claim1->next) |
8543fde5 | 7429 | { |
66be1055 | 7430 | if (claim1->flags.claimed) |
128bf1fe AM |
7431 | { |
7432 | *before = claim1->flags.claim_archive; | |
7433 | return claim1->flags.claim_archive ? lastobject : claim1; | |
7434 | } | |
8543fde5 | 7435 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 7436 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
7437 | lastobject = claim1; |
7438 | } | |
7439 | /* No files were claimed by the plugin. Choose the last object | |
7440 | file found on the list (maybe the first, dummy entry) as the | |
7441 | insert point. */ | |
128bf1fe | 7442 | *before = FALSE; |
8543fde5 DK |
7443 | return lastobject; |
7444 | } | |
9e2278f5 | 7445 | |
1fa4ec6a AM |
7446 | /* Find where to insert ADD, an archive element or shared library |
7447 | added during a rescan. */ | |
7448 | ||
36983a93 | 7449 | static lang_input_statement_type ** |
1fa4ec6a AM |
7450 | find_rescan_insertion (lang_input_statement_type *add) |
7451 | { | |
7452 | bfd *add_bfd = add->the_bfd; | |
7453 | lang_input_statement_type *f; | |
7454 | lang_input_statement_type *last_loaded = NULL; | |
7455 | lang_input_statement_type *before = NULL; | |
36983a93 | 7456 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
7457 | |
7458 | if (add_bfd->my_archive != NULL) | |
7459 | add_bfd = add_bfd->my_archive; | |
7460 | ||
7461 | /* First look through the input file chain, to find an object file | |
7462 | before the one we've rescanned. Normal object files always | |
7463 | appear on both the input file chain and the file chain, so this | |
7464 | lets us get quickly to somewhere near the correct place on the | |
7465 | file chain if it is full of archive elements. Archives don't | |
7466 | appear on the file chain, but if an element has been extracted | |
7467 | then their input_statement->next points at it. */ | |
7468 | for (f = &input_file_chain.head->input_statement; | |
7469 | f != NULL; | |
36983a93 | 7470 | f = f->next_real_file) |
1fa4ec6a AM |
7471 | { |
7472 | if (f->the_bfd == add_bfd) | |
7473 | { | |
7474 | before = last_loaded; | |
7475 | if (f->next != NULL) | |
36983a93 | 7476 | return &f->next->next; |
1fa4ec6a AM |
7477 | } |
7478 | if (f->the_bfd != NULL && f->next != NULL) | |
7479 | last_loaded = f; | |
7480 | } | |
7481 | ||
7482 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
7483 | *iter != NULL; | |
36983a93 AM |
7484 | iter = &(*iter)->next) |
7485 | if (!(*iter)->flags.claim_archive | |
7486 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
7487 | break; |
7488 | ||
7489 | return iter; | |
7490 | } | |
7491 | ||
9e2278f5 AM |
7492 | /* Insert SRCLIST into DESTLIST after given element by chaining |
7493 | on FIELD as the next-pointer. (Counterintuitively does not need | |
7494 | a pointer to the actual after-node itself, just its chain field.) */ | |
7495 | ||
7496 | static void | |
7497 | lang_list_insert_after (lang_statement_list_type *destlist, | |
7498 | lang_statement_list_type *srclist, | |
7499 | lang_statement_union_type **field) | |
7500 | { | |
7501 | *(srclist->tail) = *field; | |
7502 | *field = srclist->head; | |
7503 | if (destlist->tail == field) | |
7504 | destlist->tail = srclist->tail; | |
7505 | } | |
7506 | ||
7507 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
7508 | was taken as a copy of it and leave them in ORIGLIST. */ | |
7509 | ||
7510 | static void | |
7511 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
7512 | lang_statement_list_type *origlist) | |
7513 | { | |
7514 | union lang_statement_union **savetail; | |
7515 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
7516 | ASSERT (origlist->head == destlist->head); | |
7517 | savetail = origlist->tail; | |
7518 | origlist->head = *(savetail); | |
7519 | origlist->tail = destlist->tail; | |
7520 | destlist->tail = savetail; | |
7521 | *savetail = NULL; | |
7522 | } | |
128bf1fe AM |
7523 | |
7524 | static lang_statement_union_type ** | |
7525 | find_next_input_statement (lang_statement_union_type **s) | |
7526 | { | |
7527 | for ( ; *s; s = &(*s)->header.next) | |
7528 | { | |
7529 | lang_statement_union_type **t; | |
7530 | switch ((*s)->header.type) | |
7531 | { | |
7532 | case lang_input_statement_enum: | |
7533 | return s; | |
7534 | case lang_wild_statement_enum: | |
7535 | t = &(*s)->wild_statement.children.head; | |
7536 | break; | |
7537 | case lang_group_statement_enum: | |
7538 | t = &(*s)->group_statement.children.head; | |
7539 | break; | |
7540 | case lang_output_section_statement_enum: | |
7541 | t = &(*s)->output_section_statement.children.head; | |
7542 | break; | |
7543 | default: | |
7544 | continue; | |
7545 | } | |
7546 | t = find_next_input_statement (t); | |
7547 | if (*t) | |
7548 | return t; | |
7549 | } | |
7550 | return s; | |
7551 | } | |
8543fde5 DK |
7552 | #endif /* ENABLE_PLUGINS */ |
7553 | ||
4153b6db NC |
7554 | /* Add NAME to the list of garbage collection entry points. */ |
7555 | ||
7556 | void | |
0aa7f586 | 7557 | lang_add_gc_name (const char *name) |
4153b6db NC |
7558 | { |
7559 | struct bfd_sym_chain *sym; | |
7560 | ||
7561 | if (name == NULL) | |
7562 | return; | |
7563 | ||
988de25b | 7564 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
7565 | |
7566 | sym->next = link_info.gc_sym_list; | |
7567 | sym->name = name; | |
7568 | link_info.gc_sym_list = sym; | |
7569 | } | |
7570 | ||
fbf05aa7 L |
7571 | /* Check relocations. */ |
7572 | ||
7573 | static void | |
7574 | lang_check_relocs (void) | |
7575 | { | |
4f3b23b3 | 7576 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
7577 | { |
7578 | bfd *abfd; | |
7579 | ||
7580 | for (abfd = link_info.input_bfds; | |
7581 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 7582 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 7583 | { |
4f3b23b3 | 7584 | /* No object output, fail return. */ |
fbf05aa7 | 7585 | config.make_executable = FALSE; |
4f3b23b3 NC |
7586 | /* Note: we do not abort the loop, but rather |
7587 | continue the scan in case there are other | |
7588 | bad relocations to report. */ | |
fbf05aa7 L |
7589 | } |
7590 | } | |
7591 | } | |
7592 | ||
77f5e65e AB |
7593 | /* Look through all output sections looking for places where we can |
7594 | propagate forward the lma region. */ | |
7595 | ||
7596 | static void | |
7597 | lang_propagate_lma_regions (void) | |
7598 | { | |
7599 | lang_output_section_statement_type *os; | |
7600 | ||
5c1e6d53 | 7601 | for (os = &lang_os_list.head->output_section_statement; |
77f5e65e AB |
7602 | os != NULL; |
7603 | os = os->next) | |
7604 | { | |
7605 | if (os->prev != NULL | |
7606 | && os->lma_region == NULL | |
7607 | && os->load_base == NULL | |
7608 | && os->addr_tree == NULL | |
7609 | && os->region == os->prev->region) | |
7610 | os->lma_region = os->prev->lma_region; | |
7611 | } | |
7612 | } | |
7613 | ||
252b5132 | 7614 | void |
1579bae1 | 7615 | lang_process (void) |
252b5132 | 7616 | { |
55255dae | 7617 | /* Finalize dynamic list. */ |
40b36307 L |
7618 | if (link_info.dynamic_list) |
7619 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 7620 | |
252b5132 RH |
7621 | current_target = default_target; |
7622 | ||
08da4cac KH |
7623 | /* Open the output file. */ |
7624 | lang_for_each_statement (ldlang_open_output); | |
e5caa5e0 | 7625 | init_opb (); |
252b5132 RH |
7626 | |
7627 | ldemul_create_output_section_statements (); | |
7628 | ||
08da4cac | 7629 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
7630 | lang_place_undefineds (); |
7631 | ||
082b7297 | 7632 | if (!bfd_section_already_linked_table_init ()) |
df5f2391 | 7633 | einfo (_("%F%P: can not create hash table: %E\n")); |
9503fd87 | 7634 | |
08da4cac | 7635 | /* Create a bfd for each input file. */ |
252b5132 | 7636 | current_target = default_target; |
165f707a | 7637 | lang_statement_iteration++; |
486329aa | 7638 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
7f0cfc6e AM |
7639 | /* open_input_bfds also handles assignments, so we can give values |
7640 | to symbolic origin/length now. */ | |
7641 | lang_do_memory_regions (); | |
252b5132 | 7642 | |
5d3236ee | 7643 | #ifdef ENABLE_PLUGINS |
716db898 | 7644 | if (link_info.lto_plugin_active) |
f84854b6 | 7645 | { |
8543fde5 DK |
7646 | lang_statement_list_type added; |
7647 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 7648 | |
f84854b6 L |
7649 | /* Now all files are read, let the plugin(s) decide if there |
7650 | are any more to be added to the link before we call the | |
8543fde5 DK |
7651 | emulation's after_open hook. We create a private list of |
7652 | input statements for this purpose, which we will eventually | |
370dfff4 | 7653 | insert into the global statement list after the first claimed |
8543fde5 DK |
7654 | file. */ |
7655 | added = *stat_ptr; | |
7656 | /* We need to manipulate all three chains in synchrony. */ | |
7657 | files = file_chain; | |
7658 | inputfiles = input_file_chain; | |
f84854b6 | 7659 | if (plugin_call_all_symbols_read ()) |
df5f2391 | 7660 | einfo (_("%F%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 7661 | plugin_error_plugin ()); |
8543fde5 | 7662 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 7663 | plugin_undefs = link_info.hash->undefs_tail; |
cdaa438c | 7664 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
b02c4f16 AM |
7665 | if (plugin_undefs == link_info.hash->undefs_tail) |
7666 | plugin_undefs = NULL; | |
8543fde5 DK |
7667 | /* Restore the global list pointer now they have all been added. */ |
7668 | lang_list_remove_tail (stat_ptr, &added); | |
7669 | /* And detach the fresh ends of the file lists. */ | |
7670 | lang_list_remove_tail (&file_chain, &files); | |
7671 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
7672 | /* Were any new files added? */ | |
7673 | if (added.head != NULL) | |
7674 | { | |
7675 | /* If so, we will insert them into the statement list immediately | |
128bf1fe AM |
7676 | after the first input file that was claimed by the plugin, |
7677 | unless that file was an archive in which case it is inserted | |
7678 | immediately before. */ | |
7679 | bfd_boolean before; | |
7680 | lang_statement_union_type **prev; | |
7681 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
7682 | /* If a plugin adds input files without having claimed any, we |
7683 | don't really have a good idea where to place them. Just putting | |
7684 | them at the start or end of the list is liable to leave them | |
7685 | outside the crtbegin...crtend range. */ | |
9e2278f5 AM |
7686 | ASSERT (plugin_insert != NULL); |
7687 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
7688 | prev = &plugin_insert->header.next; |
7689 | if (before) | |
7690 | { | |
7691 | prev = find_next_input_statement (prev); | |
36983a93 | 7692 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 7693 | { |
a19826f4 | 7694 | /* We didn't find the expected input statement. |
1f1f5b92 | 7695 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
7696 | prev = &plugin_insert->header.next; |
7697 | } | |
7698 | } | |
7699 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
7700 | /* Likewise for the file chains. */ |
7701 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 7702 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
7703 | /* We must be careful when relinking file_chain; we may need to |
7704 | insert the new files at the head of the list if the insert | |
7705 | point chosen is the dummy first input file. */ | |
9e2278f5 | 7706 | if (plugin_insert->filename) |
36983a93 AM |
7707 | lang_list_insert_after (&file_chain, &files, |
7708 | (void *) &plugin_insert->next); | |
8543fde5 DK |
7709 | else |
7710 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
7711 | |
7712 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 7713 | files = file_chain; |
165f707a | 7714 | lang_statement_iteration++; |
9e2278f5 | 7715 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
7716 | lang_list_remove_tail (&file_chain, &files); |
7717 | while (files.head != NULL) | |
7718 | { | |
36983a93 AM |
7719 | lang_input_statement_type **insert; |
7720 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
7721 | bfd *my_arch; |
7722 | ||
7723 | insert = find_rescan_insertion (&files.head->input_statement); | |
7724 | /* All elements from an archive can be added at once. */ | |
7725 | iter = &files.head->input_statement.next; | |
7726 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
7727 | if (my_arch != NULL) | |
36983a93 AM |
7728 | for (; *iter != NULL; iter = &(*iter)->next) |
7729 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
7730 | break; |
7731 | temp = *insert; | |
36983a93 AM |
7732 | *insert = &files.head->input_statement; |
7733 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a AM |
7734 | *iter = temp; |
7735 | if (my_arch != NULL) | |
7736 | { | |
00f93c44 | 7737 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 7738 | if (parent != NULL) |
36983a93 | 7739 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
7740 | ((char *) iter |
7741 | - offsetof (lang_input_statement_type, next)); | |
7742 | } | |
7743 | } | |
8543fde5 | 7744 | } |
f84854b6 | 7745 | } |
5d3236ee DK |
7746 | #endif /* ENABLE_PLUGINS */ |
7747 | ||
0aa7f586 AM |
7748 | /* Make sure that nobody has tried to add a symbol to this list |
7749 | before now. */ | |
4153b6db NC |
7750 | ASSERT (link_info.gc_sym_list == NULL); |
7751 | ||
e3e942e9 | 7752 | link_info.gc_sym_list = &entry_symbol; |
41f46ed9 | 7753 | |
e3e942e9 | 7754 | if (entry_symbol.name == NULL) |
41f46ed9 SKS |
7755 | { |
7756 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
7757 | ||
7758 | /* entry_symbol is normally initialied by a ENTRY definition in the | |
7759 | linker script or the -e command line option. But if neither of | |
7760 | these have been used, the target specific backend may still have | |
4153b6db | 7761 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
7762 | Unfortunately this value will not be processed until lang_end() |
7763 | is called, long after this function has finished. So detect this | |
7764 | case here and add the target's entry symbol to the list of starting | |
7765 | points for garbage collection resolution. */ | |
4153b6db | 7766 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
7767 | } |
7768 | ||
4153b6db NC |
7769 | lang_add_gc_name (link_info.init_function); |
7770 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 7771 | |
252b5132 | 7772 | ldemul_after_open (); |
16e4ecc0 AM |
7773 | if (config.map_file != NULL) |
7774 | lang_print_asneeded (); | |
252b5132 | 7775 | |
1ff6de03 NA |
7776 | ldlang_open_ctf (); |
7777 | ||
082b7297 | 7778 | bfd_section_already_linked_table_free (); |
9503fd87 | 7779 | |
252b5132 RH |
7780 | /* Make sure that we're not mixing architectures. We call this |
7781 | after all the input files have been opened, but before we do any | |
7782 | other processing, so that any operations merge_private_bfd_data | |
7783 | does on the output file will be known during the rest of the | |
7784 | link. */ | |
7785 | lang_check (); | |
7786 | ||
7787 | /* Handle .exports instead of a version script if we're told to do so. */ | |
7788 | if (command_line.version_exports_section) | |
7789 | lang_do_version_exports_section (); | |
7790 | ||
7791 | /* Build all sets based on the information gathered from the input | |
7792 | files. */ | |
7793 | ldctor_build_sets (); | |
7794 | ||
7dba9362 AM |
7795 | /* Give initial values for __start and __stop symbols, so that ELF |
7796 | gc_sections will keep sections referenced by these symbols. Must | |
7797 | be done before lang_do_assignments below. */ | |
7798 | if (config.build_constructors) | |
7799 | lang_init_start_stop (); | |
7800 | ||
2aa9aad9 NC |
7801 | /* PR 13683: We must rerun the assignments prior to running garbage |
7802 | collection in order to make sure that all symbol aliases are resolved. */ | |
7803 | lang_do_assignments (lang_mark_phase_enum); | |
7804 | expld.phase = lang_first_phase_enum; | |
7805 | ||
08da4cac | 7806 | /* Size up the common data. */ |
252b5132 RH |
7807 | lang_common (); |
7808 | ||
c4621b33 AM |
7809 | /* Remove unreferenced sections if asked to. */ |
7810 | lang_gc_sections (); | |
7811 | ||
fbf05aa7 L |
7812 | /* Check relocations. */ |
7813 | lang_check_relocs (); | |
7814 | ||
5c3261b0 L |
7815 | ldemul_after_check_relocs (); |
7816 | ||
bcaa7b3e L |
7817 | /* Update wild statements. */ |
7818 | update_wild_statements (statement_list.head); | |
7819 | ||
252b5132 | 7820 | /* Run through the contours of the script and attach input sections |
08da4cac | 7821 | to the correct output sections. */ |
e759c116 | 7822 | lang_statement_iteration++; |
1579bae1 | 7823 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 7824 | |
776ab89f AM |
7825 | /* Start at the statement immediately after the special abs_section |
7826 | output statement, so that it isn't reordered. */ | |
7827 | process_insert_statements (&lang_os_list.head->header.next); | |
53d25da6 | 7828 | |
08da4cac | 7829 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
7830 | lang_place_orphans (); |
7831 | ||
0e1862bb | 7832 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 7833 | { |
57ceae94 AM |
7834 | asection *found; |
7835 | ||
7836 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
7837 | sections, so that GCed sections are not merged, but before | |
7838 | assigning dynamic symbols, since removing whole input sections | |
7839 | is hard then. */ | |
f13a99db | 7840 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 7841 | |
862120bd | 7842 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 7843 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 7844 | |
1579bae1 | 7845 | if (found != NULL) |
862120bd AM |
7846 | { |
7847 | if (config.text_read_only) | |
7848 | found->flags |= SEC_READONLY; | |
7849 | else | |
7850 | found->flags &= ~SEC_READONLY; | |
7851 | } | |
7852 | } | |
7853 | ||
1ff6de03 NA |
7854 | /* Merge together CTF sections. After this, only the symtab-dependent |
7855 | function and data object sections need adjustment. */ | |
7856 | lang_merge_ctf (); | |
7857 | ||
7858 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
7859 | examining things laid out late, like the strtab. */ | |
7860 | lang_write_ctf (0); | |
7861 | ||
77f5e65e AB |
7862 | /* Copy forward lma regions for output sections in same lma region. */ |
7863 | lang_propagate_lma_regions (); | |
7864 | ||
7dba9362 AM |
7865 | /* Defining __start/__stop symbols early for --gc-sections to work |
7866 | around a glibc build problem can result in these symbols being | |
7867 | defined when they should not be. Fix them now. */ | |
7868 | if (config.build_constructors) | |
7869 | lang_undef_start_stop (); | |
7870 | ||
7871 | /* Define .startof./.sizeof. symbols with preliminary values before | |
7872 | dynamic symbols are created. */ | |
7873 | if (!bfd_link_relocatable (&link_info)) | |
7874 | lang_init_startof_sizeof (); | |
7875 | ||
862120bd AM |
7876 | /* Do anything special before sizing sections. This is where ELF |
7877 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
7878 | ldemul_before_allocation (); |
7879 | ||
7880 | /* We must record the program headers before we try to fix the | |
7881 | section positions, since they will affect SIZEOF_HEADERS. */ | |
7882 | lang_record_phdrs (); | |
7883 | ||
b10a8ae0 | 7884 | /* Check relro sections. */ |
0e1862bb | 7885 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
7886 | lang_find_relro_sections (); |
7887 | ||
b3327aad | 7888 | /* Size up the sections. */ |
0aa7f586 | 7889 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 7890 | |
252b5132 | 7891 | /* See if anything special should be done now we know how big |
eaeb0a9d | 7892 | everything is. This is where relaxation is done. */ |
252b5132 RH |
7893 | ldemul_after_allocation (); |
7894 | ||
7dba9362 AM |
7895 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
7896 | lang_finalize_start_stop (); | |
252b5132 | 7897 | |
7f0cfc6e AM |
7898 | /* Do all the assignments again, to report errors. Assignment |
7899 | statements are processed multiple times, updating symbols; In | |
7900 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
7901 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
7902 | also updated in ldemul_after_allocation. */ | |
2f65ac72 | 7903 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 7904 | |
8ded5a0f AM |
7905 | ldemul_finish (); |
7906 | ||
975f8a9e AM |
7907 | /* Convert absolute symbols to section relative. */ |
7908 | ldexp_finalize_syms (); | |
7909 | ||
252b5132 | 7910 | /* Make sure that the section addresses make sense. */ |
662ef24b | 7911 | if (command_line.check_section_addresses) |
252b5132 | 7912 | lang_check_section_addresses (); |
5f992e62 | 7913 | |
0a618243 AB |
7914 | /* Check any required symbols are known. */ |
7915 | ldlang_check_require_defined_symbols (); | |
7916 | ||
750877ba | 7917 | lang_end (); |
252b5132 RH |
7918 | } |
7919 | ||
7920 | /* EXPORTED TO YACC */ | |
7921 | ||
7922 | void | |
1579bae1 AM |
7923 | lang_add_wild (struct wildcard_spec *filespec, |
7924 | struct wildcard_list *section_list, | |
7925 | bfd_boolean keep_sections) | |
252b5132 | 7926 | { |
b6bf44ba | 7927 | struct wildcard_list *curr, *next; |
d3ce72d0 | 7928 | lang_wild_statement_type *new_stmt; |
b6bf44ba AM |
7929 | |
7930 | /* Reverse the list as the parser puts it back to front. */ | |
7931 | for (curr = section_list, section_list = NULL; | |
7932 | curr != NULL; | |
7933 | section_list = curr, curr = next) | |
7934 | { | |
b6bf44ba AM |
7935 | next = curr->next; |
7936 | curr->next = section_list; | |
7937 | } | |
7938 | ||
7939 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 7940 | { |
b6bf44ba AM |
7941 | if (strcmp (filespec->name, "*") == 0) |
7942 | filespec->name = NULL; | |
0aa7f586 | 7943 | else if (!wildcardp (filespec->name)) |
b34976b6 | 7944 | lang_has_input_file = TRUE; |
252b5132 | 7945 | } |
b6bf44ba | 7946 | |
d3ce72d0 NC |
7947 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
7948 | new_stmt->filename = NULL; | |
7949 | new_stmt->filenames_sorted = FALSE; | |
ae17ab41 | 7950 | new_stmt->section_flag_list = NULL; |
8f1732fc | 7951 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 7952 | if (filespec != NULL) |
252b5132 | 7953 | { |
d3ce72d0 NC |
7954 | new_stmt->filename = filespec->name; |
7955 | new_stmt->filenames_sorted = filespec->sorted == by_name; | |
ae17ab41 | 7956 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 7957 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 7958 | } |
d3ce72d0 NC |
7959 | new_stmt->section_list = section_list; |
7960 | new_stmt->keep_sections = keep_sections; | |
7961 | lang_list_init (&new_stmt->children); | |
7962 | analyze_walk_wild_section_handler (new_stmt); | |
252b5132 RH |
7963 | } |
7964 | ||
7965 | void | |
ba916c8a MM |
7966 | lang_section_start (const char *name, etree_type *address, |
7967 | const segment_type *segment) | |
252b5132 | 7968 | { |
d1778b88 | 7969 | lang_address_statement_type *ad; |
252b5132 | 7970 | |
d1778b88 | 7971 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
7972 | ad->section_name = name; |
7973 | ad->address = address; | |
ba916c8a | 7974 | ad->segment = segment; |
252b5132 RH |
7975 | } |
7976 | ||
7977 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
7978 | because of a -e argument on the command line, or zero if this is | |
7979 | called by ENTRY in a linker script. Command line arguments take | |
7980 | precedence. */ | |
7981 | ||
7982 | void | |
1579bae1 | 7983 | lang_add_entry (const char *name, bfd_boolean cmdline) |
252b5132 | 7984 | { |
e3e942e9 | 7985 | if (entry_symbol.name == NULL |
252b5132 | 7986 | || cmdline |
0aa7f586 | 7987 | || !entry_from_cmdline) |
252b5132 | 7988 | { |
e3e942e9 | 7989 | entry_symbol.name = name; |
252b5132 RH |
7990 | entry_from_cmdline = cmdline; |
7991 | } | |
7992 | } | |
7993 | ||
a359509e ZW |
7994 | /* Set the default start symbol to NAME. .em files should use this, |
7995 | not lang_add_entry, to override the use of "start" if neither the | |
7996 | linker script nor the command line specifies an entry point. NAME | |
7997 | must be permanently allocated. */ | |
7998 | void | |
7999 | lang_default_entry (const char *name) | |
8000 | { | |
8001 | entry_symbol_default = name; | |
8002 | } | |
8003 | ||
252b5132 | 8004 | void |
1579bae1 | 8005 | lang_add_target (const char *name) |
252b5132 | 8006 | { |
d3ce72d0 | 8007 | lang_target_statement_type *new_stmt; |
252b5132 | 8008 | |
d3ce72d0 NC |
8009 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8010 | new_stmt->target = name; | |
252b5132 RH |
8011 | } |
8012 | ||
8013 | void | |
1579bae1 | 8014 | lang_add_map (const char *name) |
252b5132 RH |
8015 | { |
8016 | while (*name) | |
8017 | { | |
8018 | switch (*name) | |
8019 | { | |
08da4cac | 8020 | case 'F': |
b34976b6 | 8021 | map_option_f = TRUE; |
252b5132 RH |
8022 | break; |
8023 | } | |
8024 | name++; | |
8025 | } | |
8026 | } | |
8027 | ||
8028 | void | |
1579bae1 | 8029 | lang_add_fill (fill_type *fill) |
252b5132 | 8030 | { |
d3ce72d0 | 8031 | lang_fill_statement_type *new_stmt; |
252b5132 | 8032 | |
d3ce72d0 NC |
8033 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8034 | new_stmt->fill = fill; | |
252b5132 RH |
8035 | } |
8036 | ||
8037 | void | |
1579bae1 | 8038 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8039 | { |
d3ce72d0 | 8040 | lang_data_statement_type *new_stmt; |
252b5132 | 8041 | |
d3ce72d0 NC |
8042 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8043 | new_stmt->exp = exp; | |
8044 | new_stmt->type = type; | |
252b5132 RH |
8045 | } |
8046 | ||
8047 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
8048 | generate. HOWTO is the corresponding howto structure (we could | |
8049 | look this up, but the caller has already done so). SECTION is the | |
8050 | section to generate a reloc against, or NAME is the name of the | |
8051 | symbol to generate a reloc against. Exactly one of SECTION and | |
8052 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8053 | ||
8054 | void | |
1579bae1 AM |
8055 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
8056 | reloc_howto_type *howto, | |
8057 | asection *section, | |
8058 | const char *name, | |
8059 | union etree_union *addend) | |
252b5132 RH |
8060 | { |
8061 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8062 | |
252b5132 RH |
8063 | p->reloc = reloc; |
8064 | p->howto = howto; | |
8065 | p->section = section; | |
8066 | p->name = name; | |
8067 | p->addend_exp = addend; | |
8068 | ||
8069 | p->addend_value = 0; | |
8070 | p->output_section = NULL; | |
7fabd029 | 8071 | p->output_offset = 0; |
252b5132 RH |
8072 | } |
8073 | ||
8074 | lang_assignment_statement_type * | |
1579bae1 | 8075 | lang_add_assignment (etree_type *exp) |
252b5132 | 8076 | { |
d3ce72d0 | 8077 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8078 | |
d3ce72d0 NC |
8079 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8080 | new_stmt->exp = exp; | |
8081 | return new_stmt; | |
252b5132 RH |
8082 | } |
8083 | ||
8084 | void | |
1579bae1 | 8085 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8086 | { |
bd4d42c1 | 8087 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8088 | } |
8089 | ||
8090 | void | |
1579bae1 | 8091 | lang_startup (const char *name) |
252b5132 | 8092 | { |
1e915804 | 8093 | if (first_file->filename != NULL) |
252b5132 | 8094 | { |
df5f2391 | 8095 | einfo (_("%F%P: multiple STARTUP files\n")); |
252b5132 RH |
8096 | } |
8097 | first_file->filename = name; | |
8098 | first_file->local_sym_name = name; | |
66be1055 | 8099 | first_file->flags.real = TRUE; |
252b5132 RH |
8100 | } |
8101 | ||
8102 | void | |
1579bae1 | 8103 | lang_float (bfd_boolean maybe) |
252b5132 RH |
8104 | { |
8105 | lang_float_flag = maybe; | |
8106 | } | |
8107 | ||
ee3cc2e2 RS |
8108 | |
8109 | /* Work out the load- and run-time regions from a script statement, and | |
8110 | store them in *LMA_REGION and *REGION respectively. | |
8111 | ||
a747ee4d NC |
8112 | MEMSPEC is the name of the run-time region, or the value of |
8113 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8114 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8115 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8116 | had an explicit load address. | |
ee3cc2e2 RS |
8117 | |
8118 | It is an error to specify both a load region and a load address. */ | |
8119 | ||
8120 | static void | |
6bdafbeb NC |
8121 | lang_get_regions (lang_memory_region_type **region, |
8122 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8123 | const char *memspec, |
8124 | const char *lma_memspec, | |
6bdafbeb NC |
8125 | bfd_boolean have_lma, |
8126 | bfd_boolean have_vma) | |
ee3cc2e2 | 8127 | { |
a747ee4d | 8128 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
ee3cc2e2 | 8129 | |
6feb9908 AM |
8130 | /* If no runtime region or VMA has been specified, but the load region |
8131 | has been specified, then use the load region for the runtime region | |
8132 | as well. */ | |
6bdafbeb | 8133 | if (lma_memspec != NULL |
0aa7f586 | 8134 | && !have_vma |
6bdafbeb | 8135 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
8136 | *region = *lma_region; |
8137 | else | |
a747ee4d | 8138 | *region = lang_memory_region_lookup (memspec, FALSE); |
ee3cc2e2 | 8139 | |
6bdafbeb | 8140 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 8141 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 8142 | NULL); |
ee3cc2e2 RS |
8143 | } |
8144 | ||
252b5132 | 8145 | void |
6bdafbeb NC |
8146 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
8147 | lang_output_section_phdr_list *phdrs, | |
8148 | const char *lma_memspec) | |
252b5132 | 8149 | { |
ee3cc2e2 RS |
8150 | lang_get_regions (¤t_section->region, |
8151 | ¤t_section->lma_region, | |
8152 | memspec, lma_memspec, | |
6bdafbeb NC |
8153 | current_section->load_base != NULL, |
8154 | current_section->addr_tree != NULL); | |
113e6845 | 8155 | |
252b5132 | 8156 | current_section->fill = fill; |
252b5132 | 8157 | current_section->phdrs = phdrs; |
bde18da4 | 8158 | pop_stat_ptr (); |
252b5132 RH |
8159 | } |
8160 | ||
252b5132 RH |
8161 | /* Set the output format type. -oformat overrides scripts. */ |
8162 | ||
8163 | void | |
1579bae1 AM |
8164 | lang_add_output_format (const char *format, |
8165 | const char *big, | |
8166 | const char *little, | |
8167 | int from_script) | |
252b5132 RH |
8168 | { |
8169 | if (output_target == NULL || !from_script) | |
8170 | { | |
8171 | if (command_line.endian == ENDIAN_BIG | |
8172 | && big != NULL) | |
8173 | format = big; | |
8174 | else if (command_line.endian == ENDIAN_LITTLE | |
8175 | && little != NULL) | |
8176 | format = little; | |
8177 | ||
8178 | output_target = format; | |
8179 | } | |
8180 | } | |
8181 | ||
53d25da6 AM |
8182 | void |
8183 | lang_add_insert (const char *where, int is_before) | |
8184 | { | |
d3ce72d0 | 8185 | lang_insert_statement_type *new_stmt; |
53d25da6 | 8186 | |
d3ce72d0 NC |
8187 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
8188 | new_stmt->where = where; | |
8189 | new_stmt->is_before = is_before; | |
53d25da6 AM |
8190 | saved_script_handle = previous_script_handle; |
8191 | } | |
8192 | ||
252b5132 RH |
8193 | /* Enter a group. This creates a new lang_group_statement, and sets |
8194 | stat_ptr to build new statements within the group. */ | |
8195 | ||
8196 | void | |
1579bae1 | 8197 | lang_enter_group (void) |
252b5132 RH |
8198 | { |
8199 | lang_group_statement_type *g; | |
8200 | ||
8201 | g = new_stat (lang_group_statement, stat_ptr); | |
8202 | lang_list_init (&g->children); | |
bde18da4 | 8203 | push_stat_ptr (&g->children); |
252b5132 RH |
8204 | } |
8205 | ||
8206 | /* Leave a group. This just resets stat_ptr to start writing to the | |
8207 | regular list of statements again. Note that this will not work if | |
8208 | groups can occur inside anything else which can adjust stat_ptr, | |
8209 | but currently they can't. */ | |
8210 | ||
8211 | void | |
1579bae1 | 8212 | lang_leave_group (void) |
252b5132 | 8213 | { |
bde18da4 | 8214 | pop_stat_ptr (); |
252b5132 RH |
8215 | } |
8216 | ||
8217 | /* Add a new program header. This is called for each entry in a PHDRS | |
8218 | command in a linker script. */ | |
8219 | ||
8220 | void | |
1579bae1 AM |
8221 | lang_new_phdr (const char *name, |
8222 | etree_type *type, | |
8223 | bfd_boolean filehdr, | |
8224 | bfd_boolean phdrs, | |
8225 | etree_type *at, | |
8226 | etree_type *flags) | |
252b5132 RH |
8227 | { |
8228 | struct lang_phdr *n, **pp; | |
5c1a3f0f | 8229 | bfd_boolean hdrs; |
252b5132 | 8230 | |
988de25b | 8231 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
8232 | n->next = NULL; |
8233 | n->name = name; | |
3d9c8f6b | 8234 | n->type = exp_get_vma (type, 0, "program header type"); |
252b5132 RH |
8235 | n->filehdr = filehdr; |
8236 | n->phdrs = phdrs; | |
8237 | n->at = at; | |
8238 | n->flags = flags; | |
1e0061d2 | 8239 | |
5c1a3f0f | 8240 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
8241 | |
8242 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
8243 | if (hdrs |
8244 | && (*pp)->type == 1 | |
8245 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 8246 | { |
c1c8c1ef | 8247 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 8248 | " when prior PT_LOAD headers lack them\n"), NULL); |
5c1a3f0f NS |
8249 | hdrs = FALSE; |
8250 | } | |
8251 | ||
252b5132 RH |
8252 | *pp = n; |
8253 | } | |
8254 | ||
8255 | /* Record the program header information in the output BFD. FIXME: We | |
8256 | should not be calling an ELF specific function here. */ | |
8257 | ||
8258 | static void | |
1579bae1 | 8259 | lang_record_phdrs (void) |
252b5132 RH |
8260 | { |
8261 | unsigned int alc; | |
8262 | asection **secs; | |
6bdafbeb | 8263 | lang_output_section_phdr_list *last; |
252b5132 | 8264 | struct lang_phdr *l; |
afd7a018 | 8265 | lang_output_section_statement_type *os; |
252b5132 RH |
8266 | |
8267 | alc = 10; | |
1e9cc1c2 | 8268 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 8269 | last = NULL; |
591a748a | 8270 | |
252b5132 RH |
8271 | for (l = lang_phdr_list; l != NULL; l = l->next) |
8272 | { | |
8273 | unsigned int c; | |
8274 | flagword flags; | |
8275 | bfd_vma at; | |
8276 | ||
8277 | c = 0; | |
5c1e6d53 | 8278 | for (os = &lang_os_list.head->output_section_statement; |
afd7a018 AM |
8279 | os != NULL; |
8280 | os = os->next) | |
252b5132 | 8281 | { |
6bdafbeb | 8282 | lang_output_section_phdr_list *pl; |
252b5132 | 8283 | |
66c103b7 | 8284 | if (os->constraint < 0) |
0841712e | 8285 | continue; |
252b5132 RH |
8286 | |
8287 | pl = os->phdrs; | |
8288 | if (pl != NULL) | |
8289 | last = pl; | |
8290 | else | |
8291 | { | |
8292 | if (os->sectype == noload_section | |
8293 | || os->bfd_section == NULL | |
8294 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
8295 | continue; | |
591a748a | 8296 | |
7512c397 AM |
8297 | /* Don't add orphans to PT_INTERP header. */ |
8298 | if (l->type == 3) | |
8299 | continue; | |
8300 | ||
e9442572 | 8301 | if (last == NULL) |
591a748a | 8302 | { |
0aa7f586 | 8303 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
8304 | |
8305 | /* If we have not run across a section with a program | |
8306 | header assigned to it yet, then scan forwards to find | |
8307 | one. This prevents inconsistencies in the linker's | |
8308 | behaviour when a script has specified just a single | |
8309 | header and there are sections in that script which are | |
8310 | not assigned to it, and which occur before the first | |
8311 | use of that header. See here for more details: | |
8312 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
8313 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
8314 | if (tmp_os->phdrs) | |
e9442572 AM |
8315 | { |
8316 | last = tmp_os->phdrs; | |
8317 | break; | |
8318 | } | |
8319 | if (last == NULL) | |
8320 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 8321 | } |
e9442572 | 8322 | pl = last; |
252b5132 RH |
8323 | } |
8324 | ||
8325 | if (os->bfd_section == NULL) | |
8326 | continue; | |
8327 | ||
8328 | for (; pl != NULL; pl = pl->next) | |
8329 | { | |
8330 | if (strcmp (pl->name, l->name) == 0) | |
8331 | { | |
8332 | if (c >= alc) | |
8333 | { | |
8334 | alc *= 2; | |
1e9cc1c2 | 8335 | secs = (asection **) xrealloc (secs, |
4724d37e | 8336 | alc * sizeof (asection *)); |
252b5132 RH |
8337 | } |
8338 | secs[c] = os->bfd_section; | |
8339 | ++c; | |
b34976b6 | 8340 | pl->used = TRUE; |
252b5132 RH |
8341 | } |
8342 | } | |
8343 | } | |
8344 | ||
8345 | if (l->flags == NULL) | |
8346 | flags = 0; | |
8347 | else | |
e9ee469a | 8348 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
8349 | |
8350 | if (l->at == NULL) | |
8351 | at = 0; | |
8352 | else | |
e9ee469a | 8353 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 8354 | |
0aa7f586 AM |
8355 | if (!bfd_record_phdr (link_info.output_bfd, l->type, |
8356 | l->flags != NULL, flags, l->at != NULL, | |
8357 | at, l->filehdr, l->phdrs, c, secs)) | |
252b5132 RH |
8358 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
8359 | } | |
8360 | ||
8361 | free (secs); | |
8362 | ||
8363 | /* Make sure all the phdr assignments succeeded. */ | |
5c1e6d53 | 8364 | for (os = &lang_os_list.head->output_section_statement; |
afd7a018 AM |
8365 | os != NULL; |
8366 | os = os->next) | |
252b5132 | 8367 | { |
6bdafbeb | 8368 | lang_output_section_phdr_list *pl; |
252b5132 | 8369 | |
66c103b7 | 8370 | if (os->constraint < 0 |
afd7a018 | 8371 | || os->bfd_section == NULL) |
252b5132 RH |
8372 | continue; |
8373 | ||
afd7a018 | 8374 | for (pl = os->phdrs; |
252b5132 RH |
8375 | pl != NULL; |
8376 | pl = pl->next) | |
0aa7f586 | 8377 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 8378 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 8379 | os->name, pl->name); |
252b5132 RH |
8380 | } |
8381 | } | |
8382 | ||
8383 | /* Record a list of sections which may not be cross referenced. */ | |
8384 | ||
8385 | void | |
6bdafbeb | 8386 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
8387 | { |
8388 | struct lang_nocrossrefs *n; | |
8389 | ||
1e9cc1c2 | 8390 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
252b5132 RH |
8391 | n->next = nocrossref_list; |
8392 | n->list = l; | |
cdf96953 | 8393 | n->onlyfirst = FALSE; |
252b5132 RH |
8394 | nocrossref_list = n; |
8395 | ||
8396 | /* Set notice_all so that we get informed about all symbols. */ | |
b34976b6 | 8397 | link_info.notice_all = TRUE; |
252b5132 | 8398 | } |
cdf96953 MF |
8399 | |
8400 | /* Record a section that cannot be referenced from a list of sections. */ | |
8401 | ||
8402 | void | |
8403 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
8404 | { | |
8405 | lang_add_nocrossref (l); | |
8406 | nocrossref_list->onlyfirst = TRUE; | |
8407 | } | |
252b5132 RH |
8408 | \f |
8409 | /* Overlay handling. We handle overlays with some static variables. */ | |
8410 | ||
8411 | /* The overlay virtual address. */ | |
8412 | static etree_type *overlay_vma; | |
7e7d5768 AM |
8413 | /* And subsection alignment. */ |
8414 | static etree_type *overlay_subalign; | |
252b5132 | 8415 | |
252b5132 RH |
8416 | /* An expression for the maximum section size seen so far. */ |
8417 | static etree_type *overlay_max; | |
8418 | ||
8419 | /* A list of all the sections in this overlay. */ | |
8420 | ||
89cdebba | 8421 | struct overlay_list { |
252b5132 RH |
8422 | struct overlay_list *next; |
8423 | lang_output_section_statement_type *os; | |
8424 | }; | |
8425 | ||
8426 | static struct overlay_list *overlay_list; | |
8427 | ||
8428 | /* Start handling an overlay. */ | |
8429 | ||
8430 | void | |
7e7d5768 | 8431 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
8432 | { |
8433 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
8434 | ASSERT (overlay_vma == NULL |
8435 | && overlay_subalign == NULL | |
8436 | && overlay_max == NULL); | |
252b5132 RH |
8437 | |
8438 | overlay_vma = vma_expr; | |
7e7d5768 | 8439 | overlay_subalign = subalign; |
252b5132 RH |
8440 | } |
8441 | ||
8442 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
8443 | lang_enter_output_section_statement with the correct VMA. |
8444 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
8445 | |
8446 | void | |
1579bae1 | 8447 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
8448 | { |
8449 | struct overlay_list *n; | |
8450 | etree_type *size; | |
8451 | ||
152d792f | 8452 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
1eec346e | 8453 | 0, overlay_subalign, 0, 0, 0); |
252b5132 | 8454 | |
9f88b410 | 8455 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
8456 | sections on this one. This will work correctly even if `.' is |
8457 | used in the addresses. */ | |
8458 | if (overlay_list == NULL) | |
9f88b410 | 8459 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
8460 | |
8461 | /* Remember the section. */ | |
1e9cc1c2 | 8462 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
8463 | n->os = current_section; |
8464 | n->next = overlay_list; | |
8465 | overlay_list = n; | |
8466 | ||
8467 | size = exp_nameop (SIZEOF, name); | |
8468 | ||
252b5132 RH |
8469 | /* Arrange to work out the maximum section end address. */ |
8470 | if (overlay_max == NULL) | |
8471 | overlay_max = size; | |
8472 | else | |
8473 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
8474 | } | |
8475 | ||
8476 | /* Finish a section in an overlay. There isn't any special to do | |
8477 | here. */ | |
8478 | ||
8479 | void | |
1579bae1 | 8480 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 8481 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
8482 | { |
8483 | const char *name; | |
8484 | char *clean, *s2; | |
8485 | const char *s1; | |
8486 | char *buf; | |
8487 | ||
8488 | name = current_section->name; | |
8489 | ||
a747ee4d NC |
8490 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
8491 | region and that no load-time region has been specified. It doesn't | |
8492 | really matter what we say here, since lang_leave_overlay will | |
8493 | override it. */ | |
8494 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
8495 | |
8496 | /* Define the magic symbols. */ | |
8497 | ||
1e9cc1c2 | 8498 | clean = (char *) xmalloc (strlen (name) + 1); |
252b5132 RH |
8499 | s2 = clean; |
8500 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 8501 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
8502 | *s2++ = *s1; |
8503 | *s2 = '\0'; | |
8504 | ||
1e9cc1c2 | 8505 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
252b5132 | 8506 | sprintf (buf, "__load_start_%s", clean); |
34711ca3 AM |
8507 | lang_add_assignment (exp_provide (buf, |
8508 | exp_nameop (LOADADDR, name), | |
8509 | FALSE)); | |
252b5132 | 8510 | |
1e9cc1c2 | 8511 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
252b5132 | 8512 | sprintf (buf, "__load_stop_%s", clean); |
34711ca3 AM |
8513 | lang_add_assignment (exp_provide (buf, |
8514 | exp_binop ('+', | |
8515 | exp_nameop (LOADADDR, name), | |
8516 | exp_nameop (SIZEOF, name)), | |
8517 | FALSE)); | |
252b5132 RH |
8518 | |
8519 | free (clean); | |
8520 | } | |
8521 | ||
8522 | /* Finish an overlay. If there are any overlay wide settings, this | |
8523 | looks through all the sections in the overlay and sets them. */ | |
8524 | ||
8525 | void | |
1579bae1 AM |
8526 | lang_leave_overlay (etree_type *lma_expr, |
8527 | int nocrossrefs, | |
8528 | fill_type *fill, | |
8529 | const char *memspec, | |
6bdafbeb | 8530 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 8531 | const char *lma_memspec) |
252b5132 RH |
8532 | { |
8533 | lang_memory_region_type *region; | |
562d3460 | 8534 | lang_memory_region_type *lma_region; |
252b5132 | 8535 | struct overlay_list *l; |
6bdafbeb | 8536 | lang_nocrossref_type *nocrossref; |
252b5132 | 8537 | |
ee3cc2e2 RS |
8538 | lang_get_regions (®ion, &lma_region, |
8539 | memspec, lma_memspec, | |
6bdafbeb | 8540 | lma_expr != NULL, FALSE); |
562d3460 | 8541 | |
252b5132 RH |
8542 | nocrossref = NULL; |
8543 | ||
9f88b410 RS |
8544 | /* After setting the size of the last section, set '.' to end of the |
8545 | overlay region. */ | |
8546 | if (overlay_list != NULL) | |
6d8bf25d AM |
8547 | { |
8548 | overlay_list->os->update_dot = 1; | |
8549 | overlay_list->os->update_dot_tree | |
eb8476a6 | 8550 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), FALSE); |
6d8bf25d | 8551 | } |
9f88b410 | 8552 | |
252b5132 RH |
8553 | l = overlay_list; |
8554 | while (l != NULL) | |
8555 | { | |
8556 | struct overlay_list *next; | |
8557 | ||
1579bae1 | 8558 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 8559 | l->os->fill = fill; |
1545243b | 8560 | |
9f88b410 RS |
8561 | l->os->region = region; |
8562 | l->os->lma_region = lma_region; | |
8563 | ||
8564 | /* The first section has the load address specified in the | |
8565 | OVERLAY statement. The rest are worked out from that. | |
8566 | The base address is not needed (and should be null) if | |
8567 | an LMA region was specified. */ | |
8568 | if (l->next == 0) | |
152d792f AM |
8569 | { |
8570 | l->os->load_base = lma_expr; | |
7b243801 | 8571 | l->os->sectype = first_overlay_section; |
152d792f | 8572 | } |
252b5132 RH |
8573 | if (phdrs != NULL && l->os->phdrs == NULL) |
8574 | l->os->phdrs = phdrs; | |
8575 | ||
9f88b410 | 8576 | if (nocrossrefs) |
252b5132 | 8577 | { |
6bdafbeb | 8578 | lang_nocrossref_type *nc; |
252b5132 | 8579 | |
1e9cc1c2 | 8580 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
252b5132 RH |
8581 | nc->name = l->os->name; |
8582 | nc->next = nocrossref; | |
8583 | nocrossref = nc; | |
8584 | } | |
8585 | ||
8586 | next = l->next; | |
8587 | free (l); | |
8588 | l = next; | |
8589 | } | |
8590 | ||
8591 | if (nocrossref != NULL) | |
8592 | lang_add_nocrossref (nocrossref); | |
8593 | ||
252b5132 | 8594 | overlay_vma = NULL; |
252b5132 RH |
8595 | overlay_list = NULL; |
8596 | overlay_max = NULL; | |
8cb1232a | 8597 | overlay_subalign = NULL; |
252b5132 RH |
8598 | } |
8599 | \f | |
8600 | /* Version handling. This is only useful for ELF. */ | |
8601 | ||
108ba305 JJ |
8602 | /* If PREV is NULL, return first version pattern matching particular symbol. |
8603 | If PREV is non-NULL, return first version pattern matching particular | |
8604 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 8605 | |
108ba305 JJ |
8606 | static struct bfd_elf_version_expr * |
8607 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
8608 | struct bfd_elf_version_expr *prev, | |
8609 | const char *sym) | |
252b5132 | 8610 | { |
93252b1c | 8611 | const char *c_sym; |
108ba305 JJ |
8612 | const char *cxx_sym = sym; |
8613 | const char *java_sym = sym; | |
8614 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
8615 | enum demangling_styles curr_style; |
8616 | ||
8617 | curr_style = CURRENT_DEMANGLING_STYLE; | |
8618 | cplus_demangle_set_style (no_demangling); | |
8619 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
8620 | if (!c_sym) | |
8621 | c_sym = sym; | |
8622 | cplus_demangle_set_style (curr_style); | |
252b5132 | 8623 | |
108ba305 | 8624 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 8625 | { |
93252b1c MF |
8626 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
8627 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
8628 | if (!cxx_sym) |
8629 | cxx_sym = sym; | |
252b5132 | 8630 | } |
df816215 | 8631 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 8632 | { |
93252b1c | 8633 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
8634 | if (!java_sym) |
8635 | java_sym = sym; | |
252b5132 RH |
8636 | } |
8637 | ||
ae5a3597 | 8638 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 8639 | { |
108ba305 JJ |
8640 | struct bfd_elf_version_expr e; |
8641 | ||
8642 | switch (prev ? prev->mask : 0) | |
8643 | { | |
967928e9 AM |
8644 | case 0: |
8645 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
8646 | { | |
93252b1c | 8647 | e.pattern = c_sym; |
1e9cc1c2 | 8648 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8649 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 8650 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
8651 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
8652 | goto out_ret; | |
8653 | else | |
8654 | expr = expr->next; | |
8655 | } | |
8656 | /* Fallthrough */ | |
8657 | case BFD_ELF_VERSION_C_TYPE: | |
8658 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
8659 | { | |
ae5a3597 | 8660 | e.pattern = cxx_sym; |
1e9cc1c2 | 8661 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8662 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8663 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
8664 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
8665 | goto out_ret; | |
8666 | else | |
8667 | expr = expr->next; | |
8668 | } | |
8669 | /* Fallthrough */ | |
8670 | case BFD_ELF_VERSION_CXX_TYPE: | |
8671 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
8672 | { | |
ae5a3597 | 8673 | e.pattern = java_sym; |
1e9cc1c2 | 8674 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8675 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8676 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
8677 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
8678 | goto out_ret; | |
8679 | else | |
8680 | expr = expr->next; | |
8681 | } | |
8682 | /* Fallthrough */ | |
8683 | default: | |
8684 | break; | |
108ba305 | 8685 | } |
252b5132 | 8686 | } |
108ba305 JJ |
8687 | |
8688 | /* Finally, try the wildcards. */ | |
ae5a3597 | 8689 | if (prev == NULL || prev->literal) |
108ba305 | 8690 | expr = head->remaining; |
252b5132 | 8691 | else |
108ba305 | 8692 | expr = prev->next; |
86043bbb | 8693 | for (; expr; expr = expr->next) |
252b5132 | 8694 | { |
108ba305 JJ |
8695 | const char *s; |
8696 | ||
86043bbb MM |
8697 | if (!expr->pattern) |
8698 | continue; | |
8699 | ||
108ba305 JJ |
8700 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
8701 | break; | |
8702 | ||
8703 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
8704 | s = java_sym; | |
8705 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
8706 | s = cxx_sym; | |
8707 | else | |
93252b1c | 8708 | s = c_sym; |
5e35cbc2 | 8709 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 8710 | break; |
252b5132 RH |
8711 | } |
8712 | ||
967928e9 | 8713 | out_ret: |
93252b1c MF |
8714 | if (c_sym != sym) |
8715 | free ((char *) c_sym); | |
108ba305 JJ |
8716 | if (cxx_sym != sym) |
8717 | free ((char *) cxx_sym); | |
8718 | if (java_sym != sym) | |
8719 | free ((char *) java_sym); | |
8720 | return expr; | |
252b5132 RH |
8721 | } |
8722 | ||
5e35cbc2 | 8723 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 8724 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
8725 | |
8726 | static const char * | |
8727 | realsymbol (const char *pattern) | |
8728 | { | |
8729 | const char *p; | |
8730 | bfd_boolean changed = FALSE, backslash = FALSE; | |
1e9cc1c2 | 8731 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
8732 | |
8733 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
8734 | { | |
8735 | /* It is a glob pattern only if there is no preceding | |
8736 | backslash. */ | |
5e35cbc2 L |
8737 | if (backslash) |
8738 | { | |
8739 | /* Remove the preceding backslash. */ | |
8740 | *(s - 1) = *p; | |
ae5a3597 | 8741 | backslash = FALSE; |
5e35cbc2 L |
8742 | changed = TRUE; |
8743 | } | |
8744 | else | |
ae5a3597 AM |
8745 | { |
8746 | if (*p == '?' || *p == '*' || *p == '[') | |
8747 | { | |
8748 | free (symbol); | |
8749 | return NULL; | |
8750 | } | |
5e35cbc2 | 8751 | |
ae5a3597 AM |
8752 | *s++ = *p; |
8753 | backslash = *p == '\\'; | |
8754 | } | |
5e35cbc2 L |
8755 | } |
8756 | ||
8757 | if (changed) | |
8758 | { | |
8759 | *s = '\0'; | |
8760 | return symbol; | |
8761 | } | |
8762 | else | |
8763 | { | |
8764 | free (symbol); | |
8765 | return pattern; | |
8766 | } | |
8767 | } | |
8768 | ||
d3ce72d0 | 8769 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
8770 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
8771 | pattern to be matched against symbol names. */ | |
252b5132 RH |
8772 | |
8773 | struct bfd_elf_version_expr * | |
1579bae1 | 8774 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 8775 | const char *new_name, |
86043bbb MM |
8776 | const char *lang, |
8777 | bfd_boolean literal_p) | |
252b5132 RH |
8778 | { |
8779 | struct bfd_elf_version_expr *ret; | |
8780 | ||
d3ce72d0 | 8781 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
252b5132 | 8782 | ret->next = orig; |
31941635 L |
8783 | ret->symver = 0; |
8784 | ret->script = 0; | |
ae5a3597 | 8785 | ret->literal = TRUE; |
d3ce72d0 | 8786 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
8787 | if (ret->pattern == NULL) |
8788 | { | |
d3ce72d0 | 8789 | ret->pattern = new_name; |
ae5a3597 AM |
8790 | ret->literal = FALSE; |
8791 | } | |
252b5132 RH |
8792 | |
8793 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 8794 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 8795 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 8796 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 8797 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 8798 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
8799 | else |
8800 | { | |
8801 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
8802 | lang); | |
108ba305 | 8803 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
8804 | } |
8805 | ||
fac1652d | 8806 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
8807 | } |
8808 | ||
8809 | /* This is called for each set of variable names and match | |
8810 | expressions. */ | |
8811 | ||
8812 | struct bfd_elf_version_tree * | |
1579bae1 AM |
8813 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
8814 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
8815 | { |
8816 | struct bfd_elf_version_tree *ret; | |
8817 | ||
1e9cc1c2 | 8818 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
108ba305 JJ |
8819 | ret->globals.list = globals; |
8820 | ret->locals.list = locals; | |
8821 | ret->match = lang_vers_match; | |
252b5132 | 8822 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
8823 | return ret; |
8824 | } | |
8825 | ||
8826 | /* This static variable keeps track of version indices. */ | |
8827 | ||
8828 | static int version_index; | |
8829 | ||
108ba305 JJ |
8830 | static hashval_t |
8831 | version_expr_head_hash (const void *p) | |
8832 | { | |
1e9cc1c2 NC |
8833 | const struct bfd_elf_version_expr *e = |
8834 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 8835 | |
ae5a3597 | 8836 | return htab_hash_string (e->pattern); |
108ba305 JJ |
8837 | } |
8838 | ||
8839 | static int | |
8840 | version_expr_head_eq (const void *p1, const void *p2) | |
8841 | { | |
1e9cc1c2 NC |
8842 | const struct bfd_elf_version_expr *e1 = |
8843 | (const struct bfd_elf_version_expr *) p1; | |
8844 | const struct bfd_elf_version_expr *e2 = | |
8845 | (const struct bfd_elf_version_expr *) p2; | |
108ba305 | 8846 | |
ae5a3597 | 8847 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
8848 | } |
8849 | ||
8850 | static void | |
8851 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
8852 | { | |
8853 | size_t count = 0; | |
8854 | struct bfd_elf_version_expr *e, *next; | |
8855 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
8856 | ||
8857 | for (e = head->list; e; e = e->next) | |
8858 | { | |
ae5a3597 | 8859 | if (e->literal) |
108ba305 JJ |
8860 | count++; |
8861 | head->mask |= e->mask; | |
8862 | } | |
8863 | ||
8864 | if (count) | |
8865 | { | |
8866 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
8867 | version_expr_head_eq, NULL); | |
8868 | list_loc = &head->list; | |
8869 | remaining_loc = &head->remaining; | |
8870 | for (e = head->list; e; e = next) | |
8871 | { | |
8872 | next = e->next; | |
ae5a3597 | 8873 | if (!e->literal) |
108ba305 JJ |
8874 | { |
8875 | *remaining_loc = e; | |
8876 | remaining_loc = &e->next; | |
8877 | } | |
8878 | else | |
8879 | { | |
1e9cc1c2 | 8880 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
8881 | |
8882 | if (*loc) | |
8883 | { | |
8884 | struct bfd_elf_version_expr *e1, *last; | |
8885 | ||
1e9cc1c2 | 8886 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
8887 | last = NULL; |
8888 | do | |
8889 | { | |
8890 | if (e1->mask == e->mask) | |
8891 | { | |
8892 | last = NULL; | |
8893 | break; | |
8894 | } | |
8895 | last = e1; | |
8896 | e1 = e1->next; | |
8897 | } | |
ae5a3597 | 8898 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
8899 | |
8900 | if (last == NULL) | |
8901 | { | |
8902 | /* This is a duplicate. */ | |
8903 | /* FIXME: Memory leak. Sometimes pattern is not | |
8904 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 8905 | /* free (e->pattern); */ |
108ba305 JJ |
8906 | free (e); |
8907 | } | |
8908 | else | |
8909 | { | |
8910 | e->next = last->next; | |
8911 | last->next = e; | |
8912 | } | |
8913 | } | |
8914 | else | |
8915 | { | |
8916 | *loc = e; | |
8917 | *list_loc = e; | |
8918 | list_loc = &e->next; | |
8919 | } | |
8920 | } | |
8921 | } | |
8922 | *remaining_loc = NULL; | |
8923 | *list_loc = head->remaining; | |
8924 | } | |
8925 | else | |
8926 | head->remaining = head->list; | |
8927 | } | |
8928 | ||
252b5132 RH |
8929 | /* This is called when we know the name and dependencies of the |
8930 | version. */ | |
8931 | ||
8932 | void | |
1579bae1 AM |
8933 | lang_register_vers_node (const char *name, |
8934 | struct bfd_elf_version_tree *version, | |
8935 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
8936 | { |
8937 | struct bfd_elf_version_tree *t, **pp; | |
8938 | struct bfd_elf_version_expr *e1; | |
8939 | ||
6b9b879a JJ |
8940 | if (name == NULL) |
8941 | name = ""; | |
8942 | ||
fd91d419 L |
8943 | if (link_info.version_info != NULL |
8944 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 8945 | { |
6feb9908 AM |
8946 | einfo (_("%X%P: anonymous version tag cannot be combined" |
8947 | " with other version tags\n")); | |
5ed6aba4 | 8948 | free (version); |
6b9b879a JJ |
8949 | return; |
8950 | } | |
8951 | ||
252b5132 | 8952 | /* Make sure this node has a unique name. */ |
fd91d419 | 8953 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
8954 | if (strcmp (t->name, name) == 0) |
8955 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
8956 | ||
108ba305 JJ |
8957 | lang_finalize_version_expr_head (&version->globals); |
8958 | lang_finalize_version_expr_head (&version->locals); | |
8959 | ||
252b5132 RH |
8960 | /* Check the global and local match names, and make sure there |
8961 | aren't any duplicates. */ | |
8962 | ||
108ba305 | 8963 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 8964 | { |
fd91d419 | 8965 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
8966 | { |
8967 | struct bfd_elf_version_expr *e2; | |
8968 | ||
ae5a3597 | 8969 | if (t->locals.htab && e1->literal) |
108ba305 | 8970 | { |
1e9cc1c2 | 8971 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 8972 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 8973 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
8974 | { |
8975 | if (e1->mask == e2->mask) | |
6feb9908 | 8976 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 8977 | " in version information\n"), e1->pattern); |
108ba305 JJ |
8978 | e2 = e2->next; |
8979 | } | |
8980 | } | |
ae5a3597 | 8981 | else if (!e1->literal) |
108ba305 | 8982 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
8983 | if (strcmp (e1->pattern, e2->pattern) == 0 |
8984 | && e1->mask == e2->mask) | |
8985 | einfo (_("%X%P: duplicate expression `%s'" | |
8986 | " in version information\n"), e1->pattern); | |
252b5132 RH |
8987 | } |
8988 | } | |
8989 | ||
108ba305 | 8990 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 8991 | { |
fd91d419 | 8992 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
8993 | { |
8994 | struct bfd_elf_version_expr *e2; | |
8995 | ||
ae5a3597 | 8996 | if (t->globals.htab && e1->literal) |
108ba305 | 8997 | { |
1e9cc1c2 | 8998 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 8999 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9000 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9001 | { |
9002 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9003 | einfo (_("%X%P: duplicate expression `%s'" |
9004 | " in version information\n"), | |
ae5a3597 | 9005 | e1->pattern); |
108ba305 JJ |
9006 | e2 = e2->next; |
9007 | } | |
9008 | } | |
ae5a3597 | 9009 | else if (!e1->literal) |
108ba305 | 9010 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9011 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9012 | && e1->mask == e2->mask) | |
9013 | einfo (_("%X%P: duplicate expression `%s'" | |
9014 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9015 | } |
9016 | } | |
9017 | ||
9018 | version->deps = deps; | |
9019 | version->name = name; | |
6b9b879a JJ |
9020 | if (name[0] != '\0') |
9021 | { | |
9022 | ++version_index; | |
9023 | version->vernum = version_index; | |
9024 | } | |
9025 | else | |
9026 | version->vernum = 0; | |
252b5132 | 9027 | |
fd91d419 | 9028 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9029 | ; |
9030 | *pp = version; | |
9031 | } | |
9032 | ||
9033 | /* This is called when we see a version dependency. */ | |
9034 | ||
9035 | struct bfd_elf_version_deps * | |
1579bae1 | 9036 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9037 | { |
9038 | struct bfd_elf_version_deps *ret; | |
9039 | struct bfd_elf_version_tree *t; | |
9040 | ||
1e9cc1c2 | 9041 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
252b5132 RH |
9042 | ret->next = list; |
9043 | ||
fd91d419 | 9044 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9045 | { |
9046 | if (strcmp (t->name, name) == 0) | |
9047 | { | |
9048 | ret->version_needed = t; | |
9049 | return ret; | |
9050 | } | |
9051 | } | |
9052 | ||
9053 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9054 | ||
1e0061d2 | 9055 | ret->version_needed = NULL; |
252b5132 RH |
9056 | return ret; |
9057 | } | |
9058 | ||
9059 | static void | |
1579bae1 | 9060 | lang_do_version_exports_section (void) |
252b5132 RH |
9061 | { |
9062 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9063 | ||
9064 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9065 | { | |
9066 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9067 | char *contents, *p; | |
9068 | bfd_size_type len; | |
9069 | ||
9070 | if (sec == NULL) | |
b7a26f91 | 9071 | continue; |
252b5132 | 9072 | |
eea6121a | 9073 | len = sec->size; |
1e9cc1c2 | 9074 | contents = (char *) xmalloc (len); |
252b5132 | 9075 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9076 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9077 | |
9078 | p = contents; | |
89cdebba | 9079 | while (p < contents + len) |
252b5132 | 9080 | { |
86043bbb | 9081 | greg = lang_new_vers_pattern (greg, p, NULL, FALSE); |
252b5132 RH |
9082 | p = strchr (p, '\0') + 1; |
9083 | } | |
9084 | ||
9085 | /* Do not free the contents, as we used them creating the regex. */ | |
9086 | ||
9087 | /* Do not include this section in the link. */ | |
a14a5de3 | 9088 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9089 | } |
9090 | ||
86043bbb | 9091 | lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE); |
252b5132 RH |
9092 | lang_register_vers_node (command_line.version_exports_section, |
9093 | lang_new_vers_node (greg, lreg), NULL); | |
9094 | } | |
577a0623 | 9095 | |
cc9ad334 SKS |
9096 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */ |
9097 | ||
9098 | static void | |
9099 | lang_do_memory_regions (void) | |
9100 | { | |
9101 | lang_memory_region_type *r = lang_memory_region_list; | |
9102 | ||
9103 | for (; r != NULL; r = r->next) | |
9104 | { | |
9105 | if (r->origin_exp) | |
0aa7f586 AM |
9106 | { |
9107 | exp_fold_tree_no_dot (r->origin_exp); | |
9108 | if (expld.result.valid_p) | |
9109 | { | |
9110 | r->origin = expld.result.value; | |
9111 | r->current = r->origin; | |
9112 | } | |
9113 | else | |
9114 | einfo (_("%F%P: invalid origin for memory region %s\n"), | |
9115 | r->name_list.name); | |
9116 | } | |
cc9ad334 | 9117 | if (r->length_exp) |
0aa7f586 AM |
9118 | { |
9119 | exp_fold_tree_no_dot (r->length_exp); | |
9120 | if (expld.result.valid_p) | |
9121 | r->length = expld.result.value; | |
9122 | else | |
9123 | einfo (_("%F%P: invalid length for memory region %s\n"), | |
9124 | r->name_list.name); | |
9125 | } | |
cc9ad334 SKS |
9126 | } |
9127 | } | |
9128 | ||
577a0623 | 9129 | void |
1579bae1 | 9130 | lang_add_unique (const char *name) |
577a0623 AM |
9131 | { |
9132 | struct unique_sections *ent; | |
9133 | ||
9134 | for (ent = unique_section_list; ent; ent = ent->next) | |
9135 | if (strcmp (ent->name, name) == 0) | |
9136 | return; | |
9137 | ||
1e9cc1c2 | 9138 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
577a0623 AM |
9139 | ent->name = xstrdup (name); |
9140 | ent->next = unique_section_list; | |
9141 | unique_section_list = ent; | |
9142 | } | |
55255dae L |
9143 | |
9144 | /* Append the list of dynamic symbols to the existing one. */ | |
9145 | ||
9146 | void | |
9147 | lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic) | |
9148 | { | |
40b36307 | 9149 | if (link_info.dynamic_list) |
55255dae | 9150 | { |
07806542 JJ |
9151 | struct bfd_elf_version_expr *tail; |
9152 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
9153 | ; | |
40b36307 L |
9154 | tail->next = link_info.dynamic_list->head.list; |
9155 | link_info.dynamic_list->head.list = dynamic; | |
55255dae L |
9156 | } |
9157 | else | |
9158 | { | |
9159 | struct bfd_elf_dynamic_list *d; | |
9160 | ||
1e9cc1c2 | 9161 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
55255dae L |
9162 | d->head.list = dynamic; |
9163 | d->match = lang_vers_match; | |
40b36307 | 9164 | link_info.dynamic_list = d; |
55255dae L |
9165 | } |
9166 | } | |
9167 | ||
9168 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
9169 | one. */ | |
9170 | ||
9171 | void | |
9172 | lang_append_dynamic_list_cpp_typeinfo (void) | |
9173 | { | |
0aa7f586 | 9174 | const char *symbols[] = |
55255dae L |
9175 | { |
9176 | "typeinfo name for*", | |
9177 | "typeinfo for*" | |
9178 | }; | |
9179 | struct bfd_elf_version_expr *dynamic = NULL; | |
9180 | unsigned int i; | |
9181 | ||
9182 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9183 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
9184 | FALSE); | |
9185 | ||
9186 | lang_append_dynamic_list (dynamic); | |
9187 | } | |
40b36307 L |
9188 | |
9189 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
9190 | existing one. */ | |
9191 | ||
9192 | void | |
9193 | lang_append_dynamic_list_cpp_new (void) | |
9194 | { | |
0aa7f586 | 9195 | const char *symbols[] = |
40b36307 L |
9196 | { |
9197 | "operator new*", | |
9198 | "operator delete*" | |
9199 | }; | |
9200 | struct bfd_elf_version_expr *dynamic = NULL; | |
9201 | unsigned int i; | |
9202 | ||
9203 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9204 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
9205 | FALSE); | |
9206 | ||
9207 | lang_append_dynamic_list (dynamic); | |
9208 | } | |
01554a74 AM |
9209 | |
9210 | /* Scan a space and/or comma separated string of features. */ | |
9211 | ||
9212 | void | |
9213 | lang_ld_feature (char *str) | |
9214 | { | |
9215 | char *p, *q; | |
9216 | ||
9217 | p = str; | |
9218 | while (*p) | |
9219 | { | |
9220 | char sep; | |
9221 | while (*p == ',' || ISSPACE (*p)) | |
9222 | ++p; | |
9223 | if (!*p) | |
9224 | break; | |
9225 | q = p + 1; | |
9226 | while (*q && *q != ',' && !ISSPACE (*q)) | |
9227 | ++q; | |
9228 | sep = *q; | |
9229 | *q = 0; | |
9230 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
9231 | config.sane_expr = TRUE; | |
9232 | else | |
9233 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
9234 | *q = sep; | |
9235 | p = q; | |
9236 | } | |
9237 | } | |
3604cb1f TG |
9238 | |
9239 | /* Pretty print memory amount. */ | |
9240 | ||
9241 | static void | |
9242 | lang_print_memory_size (bfd_vma sz) | |
9243 | { | |
9244 | if ((sz & 0x3fffffff) == 0) | |
9245 | printf ("%10" BFD_VMA_FMT "u GB", sz >> 30); | |
9246 | else if ((sz & 0xfffff) == 0) | |
9247 | printf ("%10" BFD_VMA_FMT "u MB", sz >> 20); | |
9248 | else if ((sz & 0x3ff) == 0) | |
9249 | printf ("%10" BFD_VMA_FMT "u KB", sz >> 10); | |
9250 | else | |
9251 | printf (" %10" BFD_VMA_FMT "u B", sz); | |
9252 | } | |
9253 | ||
9254 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
9255 | ||
9256 | void | |
9257 | lang_print_memory_usage (void) | |
9258 | { | |
9259 | lang_memory_region_type *r; | |
9260 | ||
9261 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
9262 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
9263 | { | |
9264 | bfd_vma used_length = r->current - r->origin; | |
9265 | double percent; | |
9266 | ||
9267 | printf ("%16s: ",r->name_list.name); | |
9268 | lang_print_memory_size (used_length); | |
9269 | lang_print_memory_size ((bfd_vma) r->length); | |
9270 | ||
9271 | percent = used_length * 100.0 / r->length; | |
9272 | ||
9273 | printf (" %6.2f%%\n", percent); | |
9274 | } | |
9275 | } |