Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
b3adc24a | 2 | Copyright (C) 1991-2020 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; |
16171946 | 120 | static const char *current_input_file; |
e3e942e9 | 121 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 122 | const char *entry_section = ".text"; |
66be1055 | 123 | struct lang_input_statement_flags input_flags; |
b34976b6 | 124 | bfd_boolean entry_from_cmdline; |
24898b70 | 125 | bfd_boolean undef_from_cmdline; |
b34976b6 AM |
126 | bfd_boolean lang_has_input_file = FALSE; |
127 | bfd_boolean had_output_filename = FALSE; | |
128 | bfd_boolean lang_float_flag = FALSE; | |
129 | bfd_boolean delete_output_file_on_failure = FALSE; | |
f5ff60a6 | 130 | struct lang_phdr *lang_phdr_list; |
252b5132 | 131 | struct lang_nocrossrefs *nocrossref_list; |
16e4ecc0 | 132 | struct asneeded_minfo **asneeded_list_tail; |
1ff6de03 | 133 | static ctf_file_t *ctf_output; |
e9ee469a | 134 | |
baf09cba AM |
135 | /* Functions that traverse the linker script and might evaluate |
136 | DEFINED() need to increment this at the start of the traversal. */ | |
420e579c | 137 | int lang_statement_iteration = 0; |
252b5132 | 138 | |
baf09cba AM |
139 | /* Count times through one_lang_size_sections_pass after mark phase. */ |
140 | static int lang_sizing_iteration = 0; | |
141 | ||
61f5d054 L |
142 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
143 | Although backslashes are treated specially if a pattern contains | |
144 | wildcards, we do not consider the mere presence of a backslash to | |
145 | be enough to cause the pattern to be treated as a wildcard. | |
146 | That lets us handle DOS filenames more naturally. */ | |
147 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
148 | ||
d1778b88 | 149 | #define new_stat(x, y) \ |
1579bae1 | 150 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 151 | |
d1778b88 AM |
152 | #define outside_section_address(q) \ |
153 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 154 | |
d1778b88 AM |
155 | #define outside_symbol_address(q) \ |
156 | ((q)->value + outside_section_address (q->section)) | |
252b5132 | 157 | |
1ff6de03 NA |
158 | /* CTF sections smaller than this are not compressed: compression of |
159 | dictionaries this small doesn't gain much, and this lets consumers mmap the | |
160 | sections directly out of the ELF file and use them with no decompression | |
161 | overhead if they want to. */ | |
162 | #define CTF_COMPRESSION_THRESHOLD 4096 | |
163 | ||
1579bae1 AM |
164 | void * |
165 | stat_alloc (size_t size) | |
252b5132 RH |
166 | { |
167 | return obstack_alloc (&stat_obstack, size); | |
168 | } | |
169 | ||
97407faf AM |
170 | static int |
171 | name_match (const char *pattern, const char *name) | |
172 | { | |
173 | if (wildcardp (pattern)) | |
174 | return fnmatch (pattern, name, 0); | |
175 | return strcmp (pattern, name); | |
176 | } | |
177 | ||
16171946 FS |
178 | static char * |
179 | ldirname (const char *name) | |
180 | { | |
181 | const char *base = lbasename (name); | |
182 | char *dirname; | |
183 | ||
184 | while (base > name && IS_DIR_SEPARATOR (base[-1])) | |
185 | --base; | |
186 | if (base == name) | |
187 | return strdup ("."); | |
188 | dirname = strdup (name); | |
189 | dirname[base - name] = '\0'; | |
190 | return dirname; | |
191 | } | |
192 | ||
97407faf AM |
193 | /* If PATTERN is of the form archive:file, return a pointer to the |
194 | separator. If not, return NULL. */ | |
195 | ||
196 | static char * | |
197 | archive_path (const char *pattern) | |
198 | { | |
199 | char *p = NULL; | |
200 | ||
201 | if (link_info.path_separator == 0) | |
202 | return p; | |
203 | ||
204 | p = strchr (pattern, link_info.path_separator); | |
205 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
206 | if (p == NULL || link_info.path_separator != ':') | |
207 | return p; | |
208 | ||
209 | /* Assume a match on the second char is part of drive specifier, | |
210 | as in "c:\silly.dos". */ | |
967928e9 | 211 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
212 | p = strchr (p + 1, link_info.path_separator); |
213 | #endif | |
214 | return p; | |
215 | } | |
216 | ||
967928e9 AM |
217 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
218 | return whether F matches FILE_SPEC. */ | |
219 | ||
220 | static bfd_boolean | |
221 | input_statement_is_archive_path (const char *file_spec, char *sep, | |
222 | lang_input_statement_type *f) | |
223 | { | |
224 | bfd_boolean match = FALSE; | |
225 | ||
226 | if ((*(sep + 1) == 0 | |
227 | || name_match (sep + 1, f->filename) == 0) | |
228 | && ((sep != file_spec) | |
229 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
230 | { | |
231 | match = TRUE; | |
232 | ||
233 | if (sep != file_spec) | |
234 | { | |
607b4833 | 235 | const char *aname = bfd_get_filename (f->the_bfd->my_archive); |
967928e9 AM |
236 | *sep = 0; |
237 | match = name_match (file_spec, aname) == 0; | |
238 | *sep = link_info.path_separator; | |
239 | } | |
240 | } | |
241 | return match; | |
242 | } | |
243 | ||
8a99a385 | 244 | static bfd_boolean |
d0bf826b AM |
245 | unique_section_p (const asection *sec, |
246 | const lang_output_section_statement_type *os) | |
577a0623 AM |
247 | { |
248 | struct unique_sections *unam; | |
d0d6a25b | 249 | const char *secnam; |
577a0623 | 250 | |
7bdf4127 | 251 | if (!link_info.resolve_section_groups |
d0d6a25b AM |
252 | && sec->owner != NULL |
253 | && bfd_is_group_section (sec->owner, sec)) | |
d0bf826b AM |
254 | return !(os != NULL |
255 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
d0d6a25b AM |
256 | |
257 | secnam = sec->name; | |
577a0623 | 258 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf AM |
259 | if (name_match (unam->name, secnam) == 0) |
260 | return TRUE; | |
577a0623 | 261 | |
b34976b6 | 262 | return FALSE; |
577a0623 AM |
263 | } |
264 | ||
08da4cac | 265 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 266 | |
8f1732fc AB |
267 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return |
268 | false. */ | |
72223188 | 269 | |
8f1732fc AB |
270 | static bfd_boolean |
271 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, | |
6c19b93b | 272 | lang_input_statement_type *file) |
4dec4d4e | 273 | { |
72223188 JJ |
274 | struct name_list *list_tmp; |
275 | ||
8f1732fc | 276 | for (list_tmp = exclude_list; |
72223188 JJ |
277 | list_tmp; |
278 | list_tmp = list_tmp->next) | |
279 | { | |
967928e9 | 280 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 281 | |
967928e9 | 282 | if (p != NULL) |
72223188 | 283 | { |
967928e9 | 284 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
8f1732fc | 285 | return TRUE; |
72223188 JJ |
286 | } |
287 | ||
967928e9 | 288 | else if (name_match (list_tmp->name, file->filename) == 0) |
8f1732fc | 289 | return TRUE; |
967928e9 AM |
290 | |
291 | /* FIXME: Perhaps remove the following at some stage? Matching | |
292 | unadorned archives like this was never documented and has | |
293 | been superceded by the archive:path syntax. */ | |
294 | else if (file->the_bfd != NULL | |
295 | && file->the_bfd->my_archive != NULL | |
296 | && name_match (list_tmp->name, | |
607b4833 | 297 | bfd_get_filename (file->the_bfd->my_archive)) == 0) |
8f1732fc | 298 | return TRUE; |
72223188 JJ |
299 | } |
300 | ||
8f1732fc AB |
301 | return FALSE; |
302 | } | |
303 | ||
304 | /* Try processing a section against a wildcard. This just calls | |
305 | the callback unless the filename exclusion list is present | |
306 | and excludes the file. It's hardly ever present so this | |
307 | function is very fast. */ | |
308 | ||
309 | static void | |
310 | walk_wild_consider_section (lang_wild_statement_type *ptr, | |
311 | lang_input_statement_type *file, | |
312 | asection *s, | |
313 | struct wildcard_list *sec, | |
314 | callback_t callback, | |
315 | void *data) | |
316 | { | |
317 | /* Don't process sections from files which were excluded. */ | |
318 | if (walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, file)) | |
319 | return; | |
320 | ||
b9c361e0 | 321 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
72223188 JJ |
322 | } |
323 | ||
324 | /* Lowest common denominator routine that can handle everything correctly, | |
325 | but slowly. */ | |
326 | ||
327 | static void | |
328 | walk_wild_section_general (lang_wild_statement_type *ptr, | |
329 | lang_input_statement_type *file, | |
330 | callback_t callback, | |
331 | void *data) | |
332 | { | |
333 | asection *s; | |
334 | struct wildcard_list *sec; | |
b6bf44ba AM |
335 | |
336 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
4dec4d4e | 337 | { |
b6bf44ba | 338 | sec = ptr->section_list; |
2181f54f | 339 | if (sec == NULL) |
b9c361e0 | 340 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
2181f54f AM |
341 | |
342 | while (sec != NULL) | |
08da4cac | 343 | { |
b34976b6 | 344 | bfd_boolean skip = FALSE; |
2181f54f | 345 | |
72223188 | 346 | if (sec->spec.name != NULL) |
b6bf44ba | 347 | { |
fd361982 | 348 | const char *sname = bfd_section_name (s); |
2181f54f | 349 | |
97407faf | 350 | skip = name_match (sec->spec.name, sname) != 0; |
b6bf44ba | 351 | } |
4dec4d4e | 352 | |
b6bf44ba | 353 | if (!skip) |
72223188 | 354 | walk_wild_consider_section (ptr, file, s, sec, callback, data); |
4dec4d4e | 355 | |
2181f54f | 356 | sec = sec->next; |
4dec4d4e RH |
357 | } |
358 | } | |
359 | } | |
360 | ||
72223188 JJ |
361 | /* Routines to find a single section given its name. If there's more |
362 | than one section with that name, we report that. */ | |
363 | ||
364 | typedef struct | |
365 | { | |
366 | asection *found_section; | |
367 | bfd_boolean multiple_sections_found; | |
368 | } section_iterator_callback_data; | |
369 | ||
370 | static bfd_boolean | |
91d6fa6a | 371 | section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data) |
72223188 | 372 | { |
1e9cc1c2 | 373 | section_iterator_callback_data *d = (section_iterator_callback_data *) data; |
72223188 JJ |
374 | |
375 | if (d->found_section != NULL) | |
376 | { | |
377 | d->multiple_sections_found = TRUE; | |
378 | return TRUE; | |
379 | } | |
380 | ||
381 | d->found_section = s; | |
382 | return FALSE; | |
383 | } | |
384 | ||
385 | static asection * | |
386 | find_section (lang_input_statement_type *file, | |
387 | struct wildcard_list *sec, | |
388 | bfd_boolean *multiple_sections_found) | |
389 | { | |
390 | section_iterator_callback_data cb_data = { NULL, FALSE }; | |
391 | ||
329c1c86 | 392 | bfd_get_section_by_name_if (file->the_bfd, sec->spec.name, |
72223188 JJ |
393 | section_iterator_callback, &cb_data); |
394 | *multiple_sections_found = cb_data.multiple_sections_found; | |
395 | return cb_data.found_section; | |
396 | } | |
397 | ||
398 | /* Code for handling simple wildcards without going through fnmatch, | |
399 | which can be expensive because of charset translations etc. */ | |
400 | ||
401 | /* A simple wild is a literal string followed by a single '*', | |
402 | where the literal part is at least 4 characters long. */ | |
403 | ||
404 | static bfd_boolean | |
405 | is_simple_wild (const char *name) | |
406 | { | |
407 | size_t len = strcspn (name, "*?["); | |
408 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
409 | } | |
410 | ||
411 | static bfd_boolean | |
412 | match_simple_wild (const char *pattern, const char *name) | |
413 | { | |
414 | /* The first four characters of the pattern are guaranteed valid | |
415 | non-wildcard characters. So we can go faster. */ | |
416 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
417 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
418 | return FALSE; | |
419 | ||
420 | pattern += 4; | |
421 | name += 4; | |
422 | while (*pattern != '*') | |
423 | if (*name++ != *pattern++) | |
424 | return FALSE; | |
425 | ||
426 | return TRUE; | |
427 | } | |
428 | ||
02ecc8e9 L |
429 | /* Return the numerical value of the init_priority attribute from |
430 | section name NAME. */ | |
431 | ||
9a24a276 AM |
432 | static int |
433 | get_init_priority (const asection *sec) | |
02ecc8e9 | 434 | { |
9a24a276 AM |
435 | const char *name = bfd_section_name (sec); |
436 | const char *dot; | |
02ecc8e9 L |
437 | |
438 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 439 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
440 | lower value means a higher priority. |
441 | ||
9a24a276 | 442 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
443 | decimal numerical value of the init_priority attribute. |
444 | The order of execution in .init_array is forward and | |
445 | .fini_array is backward. | |
9a24a276 | 446 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
447 | decimal numerical value of the init_priority attribute. |
448 | The order of execution in .ctors is backward and .dtors | |
449 | is forward. | |
9a24a276 AM |
450 | |
451 | .init_array.NNNNN sections would normally be placed in an output | |
452 | .init_array section, .fini_array.NNNNN in .fini_array, | |
453 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
454 | we should sort by increasing number (and could just use | |
455 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
456 | being placed in .init_array (which may also contain | |
457 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
458 | placed in .fini_array then we need to extract the init_priority | |
459 | attribute and sort on that. */ | |
460 | dot = strrchr (name, '.'); | |
461 | if (dot != NULL && ISDIGIT (dot[1])) | |
02ecc8e9 | 462 | { |
9a24a276 AM |
463 | char *end; |
464 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
465 | if (*end == 0) | |
466 | { | |
467 | if (dot == name + 6 | |
468 | && (strncmp (name, ".ctors", 6) == 0 | |
469 | || strncmp (name, ".dtors", 6) == 0)) | |
470 | init_priority = 65535 - init_priority; | |
471 | if (init_priority <= INT_MAX) | |
472 | return init_priority; | |
473 | } | |
02ecc8e9 | 474 | } |
9a24a276 | 475 | return -1; |
02ecc8e9 L |
476 | } |
477 | ||
50c77e5d NC |
478 | /* Compare sections ASEC and BSEC according to SORT. */ |
479 | ||
480 | static int | |
481 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
482 | { | |
483 | int ret; | |
9a24a276 | 484 | int a_priority, b_priority; |
50c77e5d NC |
485 | |
486 | switch (sort) | |
487 | { | |
488 | default: | |
489 | abort (); | |
490 | ||
02ecc8e9 | 491 | case by_init_priority: |
9a24a276 AM |
492 | a_priority = get_init_priority (asec); |
493 | b_priority = get_init_priority (bsec); | |
494 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 495 | goto sort_by_name; |
9a24a276 | 496 | ret = a_priority - b_priority; |
02ecc8e9 L |
497 | if (ret) |
498 | break; | |
499 | else | |
500 | goto sort_by_name; | |
501 | ||
50c77e5d | 502 | case by_alignment_name: |
fd361982 | 503 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
504 | if (ret) |
505 | break; | |
506 | /* Fall through. */ | |
507 | ||
508 | case by_name: | |
dc1e8a47 | 509 | sort_by_name: |
fd361982 | 510 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
511 | break; |
512 | ||
513 | case by_name_alignment: | |
fd361982 | 514 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
515 | if (ret) |
516 | break; | |
517 | /* Fall through. */ | |
518 | ||
519 | case by_alignment: | |
fd361982 | 520 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
521 | break; |
522 | } | |
523 | ||
524 | return ret; | |
525 | } | |
526 | ||
329c1c86 | 527 | /* Build a Binary Search Tree to sort sections, unlike insertion sort |
50c77e5d NC |
528 | used in wild_sort(). BST is considerably faster if the number of |
529 | of sections are large. */ | |
530 | ||
531 | static lang_section_bst_type ** | |
532 | wild_sort_fast (lang_wild_statement_type *wild, | |
329c1c86 AM |
533 | struct wildcard_list *sec, |
534 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
535 | asection *section) | |
50c77e5d | 536 | { |
329c1c86 | 537 | lang_section_bst_type **tree; |
50c77e5d | 538 | |
e6f2cbf5 | 539 | tree = &wild->tree; |
50c77e5d | 540 | if (!wild->filenames_sorted |
329c1c86 | 541 | && (sec == NULL || sec->spec.sorted == none)) |
50c77e5d NC |
542 | { |
543 | /* Append at the right end of tree. */ | |
544 | while (*tree) | |
329c1c86 | 545 | tree = &((*tree)->right); |
50c77e5d NC |
546 | return tree; |
547 | } | |
548 | ||
329c1c86 | 549 | while (*tree) |
50c77e5d NC |
550 | { |
551 | /* Find the correct node to append this section. */ | |
329c1c86 AM |
552 | if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0) |
553 | tree = &((*tree)->left); | |
554 | else | |
555 | tree = &((*tree)->right); | |
50c77e5d NC |
556 | } |
557 | ||
558 | return tree; | |
559 | } | |
560 | ||
561 | /* Use wild_sort_fast to build a BST to sort sections. */ | |
562 | ||
563 | static void | |
564 | output_section_callback_fast (lang_wild_statement_type *ptr, | |
329c1c86 AM |
565 | struct wildcard_list *sec, |
566 | asection *section, | |
b9c361e0 | 567 | struct flag_info *sflag_list ATTRIBUTE_UNUSED, |
329c1c86 | 568 | lang_input_statement_type *file, |
d0bf826b | 569 | void *output) |
50c77e5d NC |
570 | { |
571 | lang_section_bst_type *node; | |
572 | lang_section_bst_type **tree; | |
d0bf826b | 573 | lang_output_section_statement_type *os; |
50c77e5d | 574 | |
d0bf826b AM |
575 | os = (lang_output_section_statement_type *) output; |
576 | ||
577 | if (unique_section_p (section, os)) | |
50c77e5d NC |
578 | return; |
579 | ||
1e9cc1c2 | 580 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
50c77e5d NC |
581 | node->left = 0; |
582 | node->right = 0; | |
583 | node->section = section; | |
584 | ||
585 | tree = wild_sort_fast (ptr, sec, file, section); | |
586 | if (tree != NULL) | |
587 | *tree = node; | |
588 | } | |
589 | ||
590 | /* Convert a sorted sections' BST back to list form. */ | |
591 | ||
592 | static void | |
329c1c86 AM |
593 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
594 | lang_section_bst_type *tree, | |
595 | void *output) | |
50c77e5d NC |
596 | { |
597 | if (tree->left) | |
598 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
599 | ||
b9c361e0 | 600 | lang_add_section (&ptr->children, tree->section, NULL, |
329c1c86 | 601 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
602 | |
603 | if (tree->right) | |
604 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
605 | ||
606 | free (tree); | |
607 | } | |
608 | ||
72223188 JJ |
609 | /* Specialized, optimized routines for handling different kinds of |
610 | wildcards */ | |
611 | ||
612 | static void | |
613 | walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr, | |
614 | lang_input_statement_type *file, | |
615 | callback_t callback, | |
616 | void *data) | |
617 | { | |
618 | /* We can just do a hash lookup for the section with the right name. | |
619 | But if that lookup discovers more than one section with the name | |
620 | (should be rare), we fall back to the general algorithm because | |
621 | we would otherwise have to sort the sections to make sure they | |
622 | get processed in the bfd's order. */ | |
623 | bfd_boolean multiple_sections_found; | |
624 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
625 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
626 | ||
627 | if (multiple_sections_found) | |
628 | walk_wild_section_general (ptr, file, callback, data); | |
629 | else if (s0) | |
630 | walk_wild_consider_section (ptr, file, s0, sec0, callback, data); | |
631 | } | |
632 | ||
633 | static void | |
634 | walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr, | |
635 | lang_input_statement_type *file, | |
636 | callback_t callback, | |
637 | void *data) | |
638 | { | |
639 | asection *s; | |
640 | struct wildcard_list *wildsec0 = ptr->handler_data[0]; | |
641 | ||
642 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
643 | { | |
fd361982 | 644 | const char *sname = bfd_section_name (s); |
72223188 JJ |
645 | bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname); |
646 | ||
647 | if (!skip) | |
648 | walk_wild_consider_section (ptr, file, s, wildsec0, callback, data); | |
649 | } | |
650 | } | |
651 | ||
652 | static void | |
653 | walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr, | |
654 | lang_input_statement_type *file, | |
655 | callback_t callback, | |
656 | void *data) | |
657 | { | |
658 | asection *s; | |
659 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
660 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
661 | bfd_boolean multiple_sections_found; | |
662 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
663 | ||
664 | if (multiple_sections_found) | |
665 | { | |
666 | walk_wild_section_general (ptr, file, callback, data); | |
667 | return; | |
668 | } | |
669 | ||
670 | /* Note that if the section was not found, s0 is NULL and | |
671 | we'll simply never succeed the s == s0 test below. */ | |
672 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
673 | { | |
674 | /* Recall that in this code path, a section cannot satisfy more | |
675 | than one spec, so if s == s0 then it cannot match | |
676 | wildspec1. */ | |
677 | if (s == s0) | |
678 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
679 | else | |
680 | { | |
fd361982 | 681 | const char *sname = bfd_section_name (s); |
72223188 JJ |
682 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
683 | ||
684 | if (!skip) | |
685 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, | |
686 | data); | |
687 | } | |
688 | } | |
689 | } | |
690 | ||
691 | static void | |
692 | walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr, | |
693 | lang_input_statement_type *file, | |
694 | callback_t callback, | |
695 | void *data) | |
696 | { | |
697 | asection *s; | |
698 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
699 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; | |
700 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
701 | bfd_boolean multiple_sections_found; | |
702 | asection *s0 = find_section (file, sec0, &multiple_sections_found); | |
703 | ||
704 | if (multiple_sections_found) | |
705 | { | |
706 | walk_wild_section_general (ptr, file, callback, data); | |
707 | return; | |
708 | } | |
709 | ||
710 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
711 | { | |
712 | if (s == s0) | |
713 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
714 | else | |
715 | { | |
fd361982 | 716 | const char *sname = bfd_section_name (s); |
72223188 JJ |
717 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
718 | ||
719 | if (!skip) | |
720 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, data); | |
721 | else | |
722 | { | |
723 | skip = !match_simple_wild (wildsec2->spec.name, sname); | |
724 | if (!skip) | |
725 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
726 | data); | |
727 | } | |
728 | } | |
729 | } | |
730 | } | |
731 | ||
732 | static void | |
733 | walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr, | |
734 | lang_input_statement_type *file, | |
735 | callback_t callback, | |
736 | void *data) | |
737 | { | |
738 | asection *s; | |
739 | struct wildcard_list *sec0 = ptr->handler_data[0]; | |
740 | struct wildcard_list *sec1 = ptr->handler_data[1]; | |
741 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; | |
742 | struct wildcard_list *wildsec3 = ptr->handler_data[3]; | |
743 | bfd_boolean multiple_sections_found; | |
744 | asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1; | |
745 | ||
746 | if (multiple_sections_found) | |
747 | { | |
748 | walk_wild_section_general (ptr, file, callback, data); | |
749 | return; | |
750 | } | |
751 | ||
752 | s1 = find_section (file, sec1, &multiple_sections_found); | |
753 | if (multiple_sections_found) | |
754 | { | |
755 | walk_wild_section_general (ptr, file, callback, data); | |
756 | return; | |
757 | } | |
758 | ||
759 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
760 | { | |
761 | if (s == s0) | |
762 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); | |
763 | else | |
764 | if (s == s1) | |
765 | walk_wild_consider_section (ptr, file, s, sec1, callback, data); | |
766 | else | |
767 | { | |
fd361982 | 768 | const char *sname = bfd_section_name (s); |
72223188 JJ |
769 | bfd_boolean skip = !match_simple_wild (wildsec2->spec.name, |
770 | sname); | |
771 | ||
772 | if (!skip) | |
773 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, | |
774 | data); | |
775 | else | |
776 | { | |
777 | skip = !match_simple_wild (wildsec3->spec.name, sname); | |
778 | if (!skip) | |
779 | walk_wild_consider_section (ptr, file, s, wildsec3, | |
780 | callback, data); | |
781 | } | |
782 | } | |
783 | } | |
784 | } | |
785 | ||
786 | static void | |
787 | walk_wild_section (lang_wild_statement_type *ptr, | |
788 | lang_input_statement_type *file, | |
789 | callback_t callback, | |
790 | void *data) | |
791 | { | |
66be1055 | 792 | if (file->flags.just_syms) |
72223188 JJ |
793 | return; |
794 | ||
795 | (*ptr->walk_wild_section_handler) (ptr, file, callback, data); | |
796 | } | |
797 | ||
798 | /* Returns TRUE when name1 is a wildcard spec that might match | |
799 | something name2 can match. We're conservative: we return FALSE | |
800 | only if the prefixes of name1 and name2 are different up to the | |
801 | first wildcard character. */ | |
802 | ||
803 | static bfd_boolean | |
804 | wild_spec_can_overlap (const char *name1, const char *name2) | |
805 | { | |
806 | size_t prefix1_len = strcspn (name1, "?*["); | |
807 | size_t prefix2_len = strcspn (name2, "?*["); | |
808 | size_t min_prefix_len; | |
809 | ||
810 | /* Note that if there is no wildcard character, then we treat the | |
811 | terminating 0 as part of the prefix. Thus ".text" won't match | |
812 | ".text." or ".text.*", for example. */ | |
813 | if (name1[prefix1_len] == '\0') | |
814 | prefix1_len++; | |
815 | if (name2[prefix2_len] == '\0') | |
816 | prefix2_len++; | |
817 | ||
818 | min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len; | |
819 | ||
820 | return memcmp (name1, name2, min_prefix_len) == 0; | |
821 | } | |
822 | ||
823 | /* Select specialized code to handle various kinds of wildcard | |
824 | statements. */ | |
825 | ||
826 | static void | |
827 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
828 | { | |
829 | int sec_count = 0; | |
830 | int wild_name_count = 0; | |
831 | struct wildcard_list *sec; | |
832 | int signature; | |
833 | int data_counter; | |
834 | ||
835 | ptr->walk_wild_section_handler = walk_wild_section_general; | |
7544697a AM |
836 | ptr->handler_data[0] = NULL; |
837 | ptr->handler_data[1] = NULL; | |
838 | ptr->handler_data[2] = NULL; | |
839 | ptr->handler_data[3] = NULL; | |
e6f2cbf5 | 840 | ptr->tree = NULL; |
72223188 JJ |
841 | |
842 | /* Count how many wildcard_specs there are, and how many of those | |
843 | actually use wildcards in the name. Also, bail out if any of the | |
844 | wildcard names are NULL. (Can this actually happen? | |
845 | walk_wild_section used to test for it.) And bail out if any | |
846 | of the wildcards are more complex than a simple string | |
847 | ending in a single '*'. */ | |
848 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
849 | { | |
850 | ++sec_count; | |
851 | if (sec->spec.name == NULL) | |
852 | return; | |
853 | if (wildcardp (sec->spec.name)) | |
854 | { | |
855 | ++wild_name_count; | |
856 | if (!is_simple_wild (sec->spec.name)) | |
857 | return; | |
858 | } | |
859 | } | |
860 | ||
861 | /* The zero-spec case would be easy to optimize but it doesn't | |
862 | happen in practice. Likewise, more than 4 specs doesn't | |
863 | happen in practice. */ | |
864 | if (sec_count == 0 || sec_count > 4) | |
865 | return; | |
866 | ||
867 | /* Check that no two specs can match the same section. */ | |
868 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
869 | { | |
870 | struct wildcard_list *sec2; | |
871 | for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next) | |
872 | { | |
873 | if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name)) | |
874 | return; | |
875 | } | |
876 | } | |
877 | ||
878 | signature = (sec_count << 8) + wild_name_count; | |
879 | switch (signature) | |
880 | { | |
881 | case 0x0100: | |
882 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0; | |
883 | break; | |
884 | case 0x0101: | |
885 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1; | |
886 | break; | |
887 | case 0x0201: | |
888 | ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1; | |
889 | break; | |
890 | case 0x0302: | |
891 | ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2; | |
892 | break; | |
893 | case 0x0402: | |
894 | ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2; | |
895 | break; | |
896 | default: | |
897 | return; | |
898 | } | |
899 | ||
900 | /* Now fill the data array with pointers to the specs, first the | |
901 | specs with non-wildcard names, then the specs with wildcard | |
902 | names. It's OK to process the specs in different order from the | |
903 | given order, because we've already determined that no section | |
904 | will match more than one spec. */ | |
905 | data_counter = 0; | |
906 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
907 | if (!wildcardp (sec->spec.name)) | |
908 | ptr->handler_data[data_counter++] = sec; | |
909 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
910 | if (wildcardp (sec->spec.name)) | |
911 | ptr->handler_data[data_counter++] = sec; | |
912 | } | |
913 | ||
4dec4d4e RH |
914 | /* Handle a wild statement for a single file F. */ |
915 | ||
916 | static void | |
1579bae1 AM |
917 | walk_wild_file (lang_wild_statement_type *s, |
918 | lang_input_statement_type *f, | |
919 | callback_t callback, | |
920 | void *data) | |
4dec4d4e | 921 | { |
8f1732fc AB |
922 | if (walk_wild_file_in_exclude_list (s->exclude_name_list, f)) |
923 | return; | |
924 | ||
4dec4d4e | 925 | if (f->the_bfd == NULL |
0aa7f586 | 926 | || !bfd_check_format (f->the_bfd, bfd_archive)) |
b6bf44ba | 927 | walk_wild_section (s, f, callback, data); |
4dec4d4e RH |
928 | else |
929 | { | |
930 | bfd *member; | |
931 | ||
932 | /* This is an archive file. We must map each member of the | |
933 | archive separately. */ | |
1579bae1 | 934 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
4dec4d4e RH |
935 | while (member != NULL) |
936 | { | |
937 | /* When lookup_name is called, it will call the add_symbols | |
938 | entry point for the archive. For each element of the | |
939 | archive which is included, BFD will call ldlang_add_file, | |
940 | which will set the usrdata field of the member to the | |
941 | lang_input_statement. */ | |
00f93c44 AM |
942 | if (bfd_usrdata (member) != NULL) |
943 | walk_wild_section (s, bfd_usrdata (member), callback, data); | |
4dec4d4e RH |
944 | |
945 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | static void | |
1579bae1 | 951 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
4dec4d4e | 952 | { |
b6bf44ba | 953 | const char *file_spec = s->filename; |
97407faf | 954 | char *p; |
b6bf44ba AM |
955 | |
956 | if (file_spec == NULL) | |
4dec4d4e RH |
957 | { |
958 | /* Perform the iteration over all files in the list. */ | |
e50d8076 | 959 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 960 | { |
b6bf44ba | 961 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
962 | } |
963 | } | |
97407faf AM |
964 | else if ((p = archive_path (file_spec)) != NULL) |
965 | { | |
966 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
967 | { | |
967928e9 AM |
968 | if (input_statement_is_archive_path (file_spec, p, f)) |
969 | walk_wild_file (s, f, callback, data); | |
97407faf AM |
970 | } |
971 | } | |
b6bf44ba | 972 | else if (wildcardp (file_spec)) |
4dec4d4e | 973 | { |
e50d8076 | 974 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 975 | { |
68bbb9f7 | 976 | if (fnmatch (file_spec, f->filename, 0) == 0) |
b6bf44ba | 977 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
978 | } |
979 | } | |
980 | else | |
981 | { | |
e50d8076 NC |
982 | lang_input_statement_type *f; |
983 | ||
4dec4d4e | 984 | /* Perform the iteration over a single file. */ |
b6bf44ba | 985 | f = lookup_name (file_spec); |
6770ec8c | 986 | if (f) |
b6bf44ba | 987 | walk_wild_file (s, f, callback, data); |
4dec4d4e | 988 | } |
5f992e62 AM |
989 | } |
990 | ||
08da4cac | 991 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
992 | function for each node, except those inside output section statements |
993 | with constraint set to -1. */ | |
252b5132 | 994 | |
8658f989 | 995 | void |
1579bae1 AM |
996 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
997 | lang_statement_union_type *s) | |
252b5132 | 998 | { |
1579bae1 | 999 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
1000 | { |
1001 | func (s); | |
1002 | ||
1003 | switch (s->header.type) | |
1004 | { | |
1005 | case lang_constructors_statement_enum: | |
1006 | lang_for_each_statement_worker (func, constructor_list.head); | |
1007 | break; | |
1008 | case lang_output_section_statement_enum: | |
8658f989 AM |
1009 | if (s->output_section_statement.constraint != -1) |
1010 | lang_for_each_statement_worker | |
1011 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
1012 | break; |
1013 | case lang_wild_statement_enum: | |
6feb9908 AM |
1014 | lang_for_each_statement_worker (func, |
1015 | s->wild_statement.children.head); | |
252b5132 RH |
1016 | break; |
1017 | case lang_group_statement_enum: | |
1018 | lang_for_each_statement_worker (func, | |
1019 | s->group_statement.children.head); | |
1020 | break; | |
1021 | case lang_data_statement_enum: | |
1022 | case lang_reloc_statement_enum: | |
1023 | case lang_object_symbols_statement_enum: | |
1024 | case lang_output_statement_enum: | |
1025 | case lang_target_statement_enum: | |
1026 | case lang_input_section_enum: | |
1027 | case lang_input_statement_enum: | |
1028 | case lang_assignment_statement_enum: | |
1029 | case lang_padding_statement_enum: | |
1030 | case lang_address_statement_enum: | |
1031 | case lang_fill_statement_enum: | |
53d25da6 | 1032 | case lang_insert_statement_enum: |
252b5132 RH |
1033 | break; |
1034 | default: | |
1035 | FAIL (); | |
1036 | break; | |
1037 | } | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | void | |
1579bae1 | 1042 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1043 | { |
08da4cac | 1044 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1045 | } |
1046 | ||
1047 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1048 | |
252b5132 | 1049 | void |
1579bae1 | 1050 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1051 | { |
1579bae1 | 1052 | list->head = NULL; |
252b5132 RH |
1053 | list->tail = &list->head; |
1054 | } | |
1055 | ||
36983a93 AM |
1056 | static void |
1057 | lang_statement_append (lang_statement_list_type *list, | |
1058 | void *element, | |
1059 | void *field) | |
1060 | { | |
1061 | *(list->tail) = element; | |
1062 | list->tail = field; | |
1063 | } | |
1064 | ||
bde18da4 AM |
1065 | void |
1066 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1067 | { | |
1068 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1069 | abort (); | |
1070 | *stat_save_ptr++ = stat_ptr; | |
1071 | stat_ptr = new_ptr; | |
1072 | } | |
1073 | ||
1074 | void | |
1075 | pop_stat_ptr (void) | |
1076 | { | |
1077 | if (stat_save_ptr <= stat_save) | |
1078 | abort (); | |
1079 | stat_ptr = *--stat_save_ptr; | |
1080 | } | |
1081 | ||
08da4cac | 1082 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1083 | |
08da4cac | 1084 | static lang_statement_union_type * |
1579bae1 AM |
1085 | new_statement (enum statement_enum type, |
1086 | size_t size, | |
1087 | lang_statement_list_type *list) | |
252b5132 | 1088 | { |
d3ce72d0 | 1089 | lang_statement_union_type *new_stmt; |
252b5132 | 1090 | |
988de25b | 1091 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
1092 | new_stmt->header.type = type; |
1093 | new_stmt->header.next = NULL; | |
1094 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1095 | return new_stmt; | |
252b5132 RH |
1096 | } |
1097 | ||
08da4cac KH |
1098 | /* Build a new input file node for the language. There are several |
1099 | ways in which we treat an input file, eg, we only look at symbols, | |
1100 | or prefix it with a -l etc. | |
252b5132 | 1101 | |
08da4cac | 1102 | We can be supplied with requests for input files more than once; |
396a2467 | 1103 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1104 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1105 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1106 | |
252b5132 | 1107 | static lang_input_statement_type * |
1579bae1 AM |
1108 | new_afile (const char *name, |
1109 | lang_input_file_enum_type file_type, | |
16171946 FS |
1110 | const char *target, |
1111 | const char *from_filename) | |
252b5132 RH |
1112 | { |
1113 | lang_input_statement_type *p; | |
1114 | ||
66be1055 AM |
1115 | lang_has_input_file = TRUE; |
1116 | ||
1f1f5b92 | 1117 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1118 | memset (&p->the_bfd, 0, |
1119 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
16171946 | 1120 | p->extra_search_path = NULL; |
252b5132 | 1121 | p->target = target; |
66be1055 AM |
1122 | p->flags.dynamic = input_flags.dynamic; |
1123 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1124 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1125 | p->flags.whole_archive = input_flags.whole_archive; | |
f4a23d42 | 1126 | p->flags.sysrooted = input_flags.sysrooted; |
bcb674cf | 1127 | |
252b5132 RH |
1128 | switch (file_type) |
1129 | { | |
1130 | case lang_input_file_is_symbols_only_enum: | |
1131 | p->filename = name; | |
252b5132 | 1132 | p->local_sym_name = name; |
66be1055 AM |
1133 | p->flags.real = TRUE; |
1134 | p->flags.just_syms = TRUE; | |
252b5132 RH |
1135 | break; |
1136 | case lang_input_file_is_fake_enum: | |
1137 | p->filename = name; | |
252b5132 | 1138 | p->local_sym_name = name; |
252b5132 RH |
1139 | break; |
1140 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1141 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1142 | { |
1143 | p->filename = name + 1; | |
1144 | p->flags.full_name_provided = TRUE; | |
1145 | } | |
d4ae5fb0 | 1146 | else |
0aa7f586 | 1147 | p->filename = name; |
ff7a0acf | 1148 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
66be1055 AM |
1149 | p->flags.maybe_archive = TRUE; |
1150 | p->flags.real = TRUE; | |
1151 | p->flags.search_dirs = TRUE; | |
252b5132 RH |
1152 | break; |
1153 | case lang_input_file_is_marker_enum: | |
1154 | p->filename = name; | |
252b5132 | 1155 | p->local_sym_name = name; |
66be1055 | 1156 | p->flags.search_dirs = TRUE; |
252b5132 RH |
1157 | break; |
1158 | case lang_input_file_is_search_file_enum: | |
1159 | p->filename = name; | |
252b5132 | 1160 | p->local_sym_name = name; |
16171946 FS |
1161 | /* If name is a relative path, search the directory of the current linker |
1162 | script first. */ | |
1163 | if (from_filename && !IS_ABSOLUTE_PATH (name)) | |
1164 | p->extra_search_path = ldirname (from_filename); | |
66be1055 AM |
1165 | p->flags.real = TRUE; |
1166 | p->flags.search_dirs = TRUE; | |
252b5132 RH |
1167 | break; |
1168 | case lang_input_file_is_file_enum: | |
1169 | p->filename = name; | |
252b5132 | 1170 | p->local_sym_name = name; |
66be1055 | 1171 | p->flags.real = TRUE; |
252b5132 RH |
1172 | break; |
1173 | default: | |
1174 | FAIL (); | |
1175 | } | |
c4b78195 | 1176 | |
36983a93 | 1177 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1178 | return p; |
1179 | } | |
1180 | ||
1181 | lang_input_statement_type * | |
1579bae1 AM |
1182 | lang_add_input_file (const char *name, |
1183 | lang_input_file_enum_type file_type, | |
1184 | const char *target) | |
252b5132 | 1185 | { |
3aa2d05a NC |
1186 | if (name != NULL |
1187 | && (*name == '=' || CONST_STRNEQ (name, "$SYSROOT"))) | |
bfa23434 HPN |
1188 | { |
1189 | lang_input_statement_type *ret; | |
1190 | char *sysrooted_name | |
3aa2d05a NC |
1191 | = concat (ld_sysroot, |
1192 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1193 | (const char *) NULL); | |
bfa23434 HPN |
1194 | |
1195 | /* We've now forcibly prepended the sysroot, making the input | |
1196 | file independent of the context. Therefore, temporarily | |
1197 | force a non-sysrooted context for this statement, so it won't | |
1198 | get the sysroot prepended again when opened. (N.B. if it's a | |
1199 | script, any child nodes with input files starting with "/" | |
1200 | will be handled as "sysrooted" as they'll be found to be | |
1201 | within the sysroot subdirectory.) */ | |
1202 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1203 | input_flags.sysrooted = 0; | |
16171946 | 1204 | ret = new_afile (sysrooted_name, file_type, target, NULL); |
bfa23434 HPN |
1205 | input_flags.sysrooted = outer_sysrooted; |
1206 | return ret; | |
1207 | } | |
1208 | ||
16171946 | 1209 | return new_afile (name, file_type, target, current_input_file); |
252b5132 RH |
1210 | } |
1211 | ||
409d7240 | 1212 | struct out_section_hash_entry |
750877ba L |
1213 | { |
1214 | struct bfd_hash_entry root; | |
409d7240 | 1215 | lang_statement_union_type s; |
750877ba L |
1216 | }; |
1217 | ||
1218 | /* The hash table. */ | |
1219 | ||
409d7240 | 1220 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1221 | |
384a9dda | 1222 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1223 | initialize the table, fill in an entry and remove the table. */ |
1224 | ||
1225 | static struct bfd_hash_entry * | |
329c1c86 | 1226 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1227 | struct bfd_hash_table *table, |
1228 | const char *string) | |
750877ba | 1229 | { |
384a9dda | 1230 | lang_output_section_statement_type **nextp; |
409d7240 | 1231 | struct out_section_hash_entry *ret; |
384a9dda AM |
1232 | |
1233 | if (entry == NULL) | |
1234 | { | |
1e9cc1c2 | 1235 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1236 | sizeof (*ret)); |
384a9dda AM |
1237 | if (entry == NULL) |
1238 | return entry; | |
1239 | } | |
1240 | ||
1241 | entry = bfd_hash_newfunc (entry, table, string); | |
1242 | if (entry == NULL) | |
1243 | return entry; | |
1244 | ||
409d7240 AM |
1245 | ret = (struct out_section_hash_entry *) entry; |
1246 | memset (&ret->s, 0, sizeof (ret->s)); | |
1247 | ret->s.header.type = lang_output_section_statement_enum; | |
3d9c8f6b AM |
1248 | ret->s.output_section_statement.subsection_alignment = NULL; |
1249 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1250 | ret->s.output_section_statement.block_value = 1; |
1251 | lang_list_init (&ret->s.output_section_statement.children); | |
1252 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1253 | |
218868ba | 1254 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1255 | first one, lang_os_list.tail points to the "next" |
218868ba | 1256 | field of the last element of the list. */ |
5c1e6d53 | 1257 | if (lang_os_list.head != NULL) |
409d7240 AM |
1258 | ret->s.output_section_statement.prev |
1259 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1260 | ((char *) lang_os_list.tail |
409d7240 | 1261 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1262 | |
384a9dda AM |
1263 | /* GCC's strict aliasing rules prevent us from just casting the |
1264 | address, so we store the pointer in a variable and cast that | |
1265 | instead. */ | |
409d7240 | 1266 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1267 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
384a9dda | 1268 | return &ret->root; |
750877ba L |
1269 | } |
1270 | ||
1271 | static void | |
409d7240 | 1272 | output_section_statement_table_init (void) |
750877ba | 1273 | { |
409d7240 AM |
1274 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1275 | output_section_statement_newfunc, | |
1276 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1277 | 61)) |
df5f2391 | 1278 | einfo (_("%F%P: can not create hash table: %E\n")); |
750877ba L |
1279 | } |
1280 | ||
1281 | static void | |
409d7240 | 1282 | output_section_statement_table_free (void) |
750877ba | 1283 | { |
409d7240 | 1284 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1285 | } |
1286 | ||
08da4cac KH |
1287 | /* Build enough state so that the parser can build its tree. */ |
1288 | ||
252b5132 | 1289 | void |
1579bae1 | 1290 | lang_init (void) |
252b5132 RH |
1291 | { |
1292 | obstack_begin (&stat_obstack, 1000); | |
1293 | ||
1294 | stat_ptr = &statement_list; | |
1295 | ||
409d7240 | 1296 | output_section_statement_table_init (); |
750877ba | 1297 | |
252b5132 RH |
1298 | lang_list_init (stat_ptr); |
1299 | ||
1300 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1301 | lang_list_init (&lang_os_list); |
252b5132 | 1302 | lang_list_init (&file_chain); |
1579bae1 AM |
1303 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1304 | NULL); | |
08da4cac | 1305 | abs_output_section = |
66c103b7 | 1306 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE); |
252b5132 RH |
1307 | |
1308 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1309 | |
16e4ecc0 AM |
1310 | asneeded_list_head = NULL; |
1311 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1312 | } |
1313 | ||
750877ba L |
1314 | void |
1315 | lang_finish (void) | |
1316 | { | |
409d7240 | 1317 | output_section_statement_table_free (); |
750877ba L |
1318 | } |
1319 | ||
252b5132 | 1320 | /*---------------------------------------------------------------------- |
08da4cac KH |
1321 | A region is an area of memory declared with the |
1322 | MEMORY { name:org=exp, len=exp ... } | |
1323 | syntax. | |
252b5132 | 1324 | |
08da4cac | 1325 | We maintain a list of all the regions here. |
252b5132 | 1326 | |
08da4cac | 1327 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1328 | which is created when looked up to be the entire data space. |
1329 | ||
1330 | If create is true we are creating a region inside a MEMORY block. | |
1331 | In this case it is probably an error to create a region that has | |
1332 | already been created. If we are not inside a MEMORY block it is | |
1333 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 | 1334 | and so we issue a warning. |
1e0061d2 | 1335 | |
4a93e180 NC |
1336 | Each region has at least one name. The first name is either |
1337 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1338 | alias names to an existing region within a script with | |
1339 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1340 | region. */ | |
252b5132 RH |
1341 | |
1342 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1343 | static lang_memory_region_type **lang_memory_region_list_tail |
1344 | = &lang_memory_region_list; | |
252b5132 RH |
1345 | |
1346 | lang_memory_region_type * | |
a747ee4d | 1347 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
252b5132 | 1348 | { |
4a93e180 NC |
1349 | lang_memory_region_name *n; |
1350 | lang_memory_region_type *r; | |
d3ce72d0 | 1351 | lang_memory_region_type *new_region; |
252b5132 | 1352 | |
9f88b410 RS |
1353 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1354 | if (name == NULL) | |
1355 | return NULL; | |
1356 | ||
4a93e180 NC |
1357 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1358 | for (n = &r->name_list; n != NULL; n = n->next) | |
1359 | if (strcmp (n->name, name) == 0) | |
4724d37e RM |
1360 | { |
1361 | if (create) | |
c1c8c1ef | 1362 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1363 | NULL, name); |
1364 | return r; | |
1365 | } | |
252b5132 | 1366 | |
a747ee4d | 1367 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1368 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1369 | NULL, name); |
a747ee4d | 1370 | |
988de25b | 1371 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1372 | |
d3ce72d0 NC |
1373 | new_region->name_list.name = xstrdup (name); |
1374 | new_region->name_list.next = NULL; | |
1375 | new_region->next = NULL; | |
cc9ad334 | 1376 | new_region->origin_exp = NULL; |
d3ce72d0 | 1377 | new_region->origin = 0; |
cc9ad334 | 1378 | new_region->length_exp = NULL; |
d3ce72d0 NC |
1379 | new_region->length = ~(bfd_size_type) 0; |
1380 | new_region->current = 0; | |
1381 | new_region->last_os = NULL; | |
1382 | new_region->flags = 0; | |
1383 | new_region->not_flags = 0; | |
1384 | new_region->had_full_message = FALSE; | |
252b5132 | 1385 | |
d3ce72d0 NC |
1386 | *lang_memory_region_list_tail = new_region; |
1387 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1388 | |
d3ce72d0 | 1389 | return new_region; |
252b5132 RH |
1390 | } |
1391 | ||
4a93e180 | 1392 | void |
0aa7f586 | 1393 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1394 | { |
0aa7f586 AM |
1395 | lang_memory_region_name *n; |
1396 | lang_memory_region_type *r; | |
1397 | lang_memory_region_type *region; | |
4a93e180 NC |
1398 | |
1399 | /* The default region must be unique. This ensures that it is not necessary | |
1400 | to iterate through the name list if someone wants the check if a region is | |
1401 | the default memory region. */ | |
1402 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1403 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
c1c8c1ef | 1404 | einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
1405 | |
1406 | /* Look for the target region and check if the alias is not already | |
1407 | in use. */ | |
1408 | region = NULL; | |
1409 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1410 | for (n = &r->name_list; n != NULL; n = n->next) | |
1411 | { | |
4724d37e RM |
1412 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1413 | region = r; | |
1414 | if (strcmp (n->name, alias) == 0) | |
c1c8c1ef | 1415 | einfo (_("%F%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1416 | "alias `%s'\n"), |
1417 | NULL, alias); | |
4a93e180 NC |
1418 | } |
1419 | ||
1420 | /* Check if the target region exists. */ | |
1421 | if (region == NULL) | |
c1c8c1ef | 1422 | einfo (_("%F%P:%pS: error: memory region `%s' " |
4724d37e RM |
1423 | "for alias `%s' does not exist\n"), |
1424 | NULL, region_name, alias); | |
4a93e180 NC |
1425 | |
1426 | /* Add alias to region name list. */ | |
988de25b | 1427 | n = stat_alloc (sizeof (lang_memory_region_name)); |
4a93e180 NC |
1428 | n->name = xstrdup (alias); |
1429 | n->next = region->name_list.next; | |
1430 | region->name_list.next = n; | |
1431 | } | |
1432 | ||
5f992e62 | 1433 | static lang_memory_region_type * |
0aa7f586 | 1434 | lang_memory_default (asection *section) |
252b5132 RH |
1435 | { |
1436 | lang_memory_region_type *p; | |
1437 | ||
1438 | flagword sec_flags = section->flags; | |
1439 | ||
1440 | /* Override SEC_DATA to mean a writable section. */ | |
1441 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1442 | sec_flags |= SEC_DATA; | |
1443 | ||
1579bae1 | 1444 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1445 | { |
1446 | if ((p->flags & sec_flags) != 0 | |
1447 | && (p->not_flags & sec_flags) == 0) | |
1448 | { | |
1449 | return p; | |
1450 | } | |
1451 | } | |
a747ee4d | 1452 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
252b5132 RH |
1453 | } |
1454 | ||
24ef1aa7 GM |
1455 | /* Get the output section statement directly from the userdata. */ |
1456 | ||
1457 | lang_output_section_statement_type * | |
1458 | lang_output_section_get (const asection *output_section) | |
1459 | { | |
a48931cc | 1460 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1461 | } |
1462 | ||
d127ecce AM |
1463 | /* Find or create an output_section_statement with the given NAME. |
1464 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
1465 | match any non-negative constraint. If CREATE, always make a | |
1466 | new output_section_statement for SPECIAL CONSTRAINT. */ | |
1467 | ||
384a9dda | 1468 | lang_output_section_statement_type * |
d127ecce | 1469 | lang_output_section_statement_lookup (const char *name, |
66c103b7 AM |
1470 | int constraint, |
1471 | bfd_boolean create) | |
252b5132 | 1472 | { |
409d7240 | 1473 | struct out_section_hash_entry *entry; |
252b5132 | 1474 | |
409d7240 AM |
1475 | entry = ((struct out_section_hash_entry *) |
1476 | bfd_hash_lookup (&output_section_statement_table, name, | |
66c103b7 | 1477 | create, FALSE)); |
384a9dda AM |
1478 | if (entry == NULL) |
1479 | { | |
66c103b7 | 1480 | if (create) |
df5f2391 | 1481 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1482 | return NULL; |
1483 | } | |
252b5132 | 1484 | |
409d7240 | 1485 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1486 | { |
384a9dda AM |
1487 | /* We have a section of this name, but it might not have the correct |
1488 | constraint. */ | |
66c103b7 | 1489 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1490 | |
d127ecce | 1491 | name = entry->s.output_section_statement.name; |
8a99a385 AM |
1492 | if (create && constraint == SPECIAL) |
1493 | /* Not traversing to the end reverses the order of the second | |
1494 | and subsequent SPECIAL sections in the hash table chain, | |
1495 | but that shouldn't matter. */ | |
1496 | last_ent = entry; | |
1497 | else | |
1498 | do | |
1499 | { | |
d127ecce AM |
1500 | if (constraint == entry->s.output_section_statement.constraint |
1501 | || (constraint == 0 | |
1502 | && entry->s.output_section_statement.constraint >= 0)) | |
8a99a385 AM |
1503 | return &entry->s.output_section_statement; |
1504 | last_ent = entry; | |
1505 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1506 | } | |
1507 | while (entry != NULL | |
d127ecce | 1508 | && name == entry->s.output_section_statement.name); |
252b5132 | 1509 | |
66c103b7 AM |
1510 | if (!create) |
1511 | return NULL; | |
1512 | ||
409d7240 AM |
1513 | entry |
1514 | = ((struct out_section_hash_entry *) | |
1515 | output_section_statement_newfunc (NULL, | |
1516 | &output_section_statement_table, | |
1517 | name)); | |
750877ba | 1518 | if (entry == NULL) |
384a9dda | 1519 | { |
df5f2391 | 1520 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1521 | return NULL; |
1522 | } | |
1523 | entry->root = last_ent->root; | |
1524 | last_ent->root.next = &entry->root; | |
252b5132 | 1525 | } |
384a9dda | 1526 | |
409d7240 AM |
1527 | entry->s.output_section_statement.name = name; |
1528 | entry->s.output_section_statement.constraint = constraint; | |
1529 | return &entry->s.output_section_statement; | |
252b5132 RH |
1530 | } |
1531 | ||
d127ecce AM |
1532 | /* Find the next output_section_statement with the same name as OS. |
1533 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1534 | match any non-negative constraint. */ | |
1535 | ||
1536 | lang_output_section_statement_type * | |
1537 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1538 | int constraint) | |
1539 | { | |
1540 | /* All output_section_statements are actually part of a | |
1541 | struct out_section_hash_entry. */ | |
1542 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1543 | ((char *) os | |
1544 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1545 | const char *name = os->name; | |
1546 | ||
1547 | ASSERT (name == entry->root.string); | |
1548 | do | |
1549 | { | |
1550 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1551 | if (entry == NULL | |
1552 | || name != entry->s.output_section_statement.name) | |
1553 | return NULL; | |
1554 | } | |
1555 | while (constraint != entry->s.output_section_statement.constraint | |
1556 | && (constraint != 0 | |
1557 | || entry->s.output_section_statement.constraint < 0)); | |
1558 | ||
1559 | return &entry->s.output_section_statement; | |
1560 | } | |
1561 | ||
afd7a018 AM |
1562 | /* A variant of lang_output_section_find used by place_orphan. |
1563 | Returns the output statement that should precede a new output | |
1564 | statement for SEC. If an exact match is found on certain flags, | |
1565 | sets *EXACT too. */ | |
1566 | ||
1567 | lang_output_section_statement_type * | |
1568 | lang_output_section_find_by_flags (const asection *sec, | |
93638471 | 1569 | flagword sec_flags, |
390fbbf1 AM |
1570 | lang_output_section_statement_type **exact, |
1571 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1572 | { |
1573 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1574 | flagword look_flags, differ; |
afd7a018 AM |
1575 | |
1576 | /* We know the first statement on this list is *ABS*. May as well | |
1577 | skip it. */ | |
8ce18f9c | 1578 | first = (void *) lang_os_list.head; |
afd7a018 AM |
1579 | first = first->next; |
1580 | ||
1581 | /* First try for an exact match. */ | |
1582 | found = NULL; | |
1583 | for (look = first; look; look = look->next) | |
1584 | { | |
d9d94ac8 | 1585 | look_flags = look->flags; |
afd7a018 | 1586 | if (look->bfd_section != NULL) |
ecca9871 | 1587 | { |
d9d94ac8 | 1588 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1589 | if (match_type && !match_type (link_info.output_bfd, |
1590 | look->bfd_section, | |
390fbbf1 | 1591 | sec->owner, sec)) |
ecca9871 L |
1592 | continue; |
1593 | } | |
d9d94ac8 AM |
1594 | differ = look_flags ^ sec_flags; |
1595 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1596 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1597 | found = look; |
1598 | } | |
1599 | if (found != NULL) | |
1600 | { | |
390fbbf1 AM |
1601 | if (exact != NULL) |
1602 | *exact = found; | |
afd7a018 AM |
1603 | return found; |
1604 | } | |
1605 | ||
d9d94ac8 AM |
1606 | if ((sec_flags & SEC_CODE) != 0 |
1607 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1608 | { |
1609 | /* Try for a rw code section. */ | |
1610 | for (look = first; look; look = look->next) | |
1611 | { | |
d9d94ac8 | 1612 | look_flags = look->flags; |
afd7a018 | 1613 | if (look->bfd_section != NULL) |
ecca9871 | 1614 | { |
d9d94ac8 | 1615 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1616 | if (match_type && !match_type (link_info.output_bfd, |
1617 | look->bfd_section, | |
390fbbf1 | 1618 | sec->owner, sec)) |
ecca9871 L |
1619 | continue; |
1620 | } | |
d9d94ac8 AM |
1621 | differ = look_flags ^ sec_flags; |
1622 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1623 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1624 | found = look; |
1625 | } | |
afd7a018 | 1626 | } |
d9d94ac8 AM |
1627 | else if ((sec_flags & SEC_READONLY) != 0 |
1628 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1629 | { |
1630 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1631 | for (look = first; look; look = look->next) | |
1632 | { | |
d9d94ac8 | 1633 | look_flags = look->flags; |
afd7a018 | 1634 | if (look->bfd_section != NULL) |
ecca9871 | 1635 | { |
d9d94ac8 | 1636 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1637 | if (match_type && !match_type (link_info.output_bfd, |
1638 | look->bfd_section, | |
390fbbf1 | 1639 | sec->owner, sec)) |
ecca9871 L |
1640 | continue; |
1641 | } | |
d9d94ac8 AM |
1642 | differ = look_flags ^ sec_flags; |
1643 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1644 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1645 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1646 | | SEC_READONLY)) | |
1647 | && !(look_flags & SEC_SMALL_DATA))) | |
afd7a018 AM |
1648 | found = look; |
1649 | } | |
afd7a018 | 1650 | } |
d9d94ac8 AM |
1651 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 |
1652 | && (sec_flags & SEC_ALLOC) != 0) | |
1653 | { | |
1654 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1655 | as if it were a loaded section, and don't use match_type. */ | |
1656 | bfd_boolean seen_thread_local = FALSE; | |
1657 | ||
1658 | match_type = NULL; | |
1659 | for (look = first; look; look = look->next) | |
1660 | { | |
1661 | look_flags = look->flags; | |
1662 | if (look->bfd_section != NULL) | |
1663 | look_flags = look->bfd_section->flags; | |
1664 | ||
1665 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1666 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1667 | { | |
1668 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1669 | if (!(look_flags & SEC_LOAD) | |
1670 | && (sec_flags & SEC_LOAD)) | |
1671 | /* ..so if we're at a .tbss section and we're placing | |
1672 | a .tdata section stop looking and return the | |
1673 | previous section. */ | |
1674 | break; | |
1675 | found = look; | |
1676 | seen_thread_local = TRUE; | |
1677 | } | |
1678 | else if (seen_thread_local) | |
1679 | break; | |
1680 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1681 | found = look; | |
1682 | } | |
1683 | } | |
1684 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1685 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1686 | { |
1687 | /* .sdata goes after .data, .sbss after .sdata. */ | |
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_THREAD_LOCAL)) | |
1702 | || ((look_flags & SEC_SMALL_DATA) | |
1703 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
afd7a018 AM |
1704 | found = look; |
1705 | } | |
afd7a018 | 1706 | } |
d9d94ac8 AM |
1707 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1708 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1709 | { |
1710 | /* .data goes after .rodata. */ | |
1711 | for (look = first; look; look = look->next) | |
1712 | { | |
d9d94ac8 | 1713 | look_flags = look->flags; |
afd7a018 | 1714 | if (look->bfd_section != NULL) |
ecca9871 | 1715 | { |
d9d94ac8 | 1716 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1717 | if (match_type && !match_type (link_info.output_bfd, |
1718 | look->bfd_section, | |
390fbbf1 | 1719 | sec->owner, sec)) |
ecca9871 L |
1720 | continue; |
1721 | } | |
d9d94ac8 AM |
1722 | differ = look_flags ^ sec_flags; |
1723 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1724 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1725 | found = look; |
1726 | } | |
afd7a018 | 1727 | } |
d9d94ac8 | 1728 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1729 | { |
07890c07 | 1730 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1731 | for (look = first; look; look = look->next) |
ecca9871 | 1732 | { |
d9d94ac8 | 1733 | look_flags = look->flags; |
390fbbf1 AM |
1734 | if (look->bfd_section != NULL) |
1735 | { | |
d9d94ac8 | 1736 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1737 | if (match_type && !match_type (link_info.output_bfd, |
1738 | look->bfd_section, | |
390fbbf1 AM |
1739 | sec->owner, sec)) |
1740 | continue; | |
1741 | } | |
d9d94ac8 AM |
1742 | differ = look_flags ^ sec_flags; |
1743 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1744 | found = look; |
ecca9871 | 1745 | } |
afd7a018 | 1746 | } |
07890c07 AM |
1747 | else |
1748 | { | |
1749 | /* non-alloc go last. */ | |
1750 | for (look = first; look; look = look->next) | |
1751 | { | |
d9d94ac8 | 1752 | look_flags = look->flags; |
07890c07 | 1753 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1754 | look_flags = look->bfd_section->flags; |
1755 | differ = look_flags ^ sec_flags; | |
1756 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1757 | found = look; |
1758 | } | |
1759 | return found; | |
1760 | } | |
afd7a018 | 1761 | |
390fbbf1 AM |
1762 | if (found || !match_type) |
1763 | return found; | |
1764 | ||
93638471 | 1765 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
1766 | } |
1767 | ||
1768 | /* Find the last output section before given output statement. | |
1769 | Used by place_orphan. */ | |
1770 | ||
1771 | static asection * | |
1772 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1773 | { | |
afd7a018 AM |
1774 | lang_output_section_statement_type *lookup; |
1775 | ||
218868ba | 1776 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1777 | { |
66c103b7 | 1778 | if (lookup->constraint < 0) |
afd7a018 | 1779 | continue; |
afd7a018 AM |
1780 | |
1781 | if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) | |
218868ba | 1782 | return lookup->bfd_section; |
afd7a018 AM |
1783 | } |
1784 | ||
1785 | return NULL; | |
1786 | } | |
1787 | ||
53d25da6 AM |
1788 | /* Look for a suitable place for a new output section statement. The |
1789 | idea is to skip over anything that might be inside a SECTIONS {} | |
1790 | statement in a script, before we find another output section | |
1791 | statement. Assignments to "dot" before an output section statement | |
0fa4d7cf AM |
1792 | are assumed to belong to it, except in two cases; The first |
1793 | assignment to dot, and assignments before non-alloc sections. | |
1794 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1795 | similar assignments that set the initial address, or we might | |
1796 | insert non-alloc note sections among assignments setting end of | |
1797 | image symbols. */ | |
53d25da6 AM |
1798 | |
1799 | static lang_statement_union_type ** | |
1800 | insert_os_after (lang_output_section_statement_type *after) | |
1801 | { | |
1802 | lang_statement_union_type **where; | |
1803 | lang_statement_union_type **assign = NULL; | |
1804 | bfd_boolean ignore_first; | |
1805 | ||
8ce18f9c | 1806 | ignore_first = after == (void *) lang_os_list.head; |
53d25da6 AM |
1807 | |
1808 | for (where = &after->header.next; | |
1809 | *where != NULL; | |
1810 | where = &(*where)->header.next) | |
1811 | { | |
1812 | switch ((*where)->header.type) | |
1813 | { | |
1814 | case lang_assignment_statement_enum: | |
1815 | if (assign == NULL) | |
1816 | { | |
1817 | lang_assignment_statement_type *ass; | |
1818 | ||
1819 | ass = &(*where)->assignment_statement; | |
1820 | if (ass->exp->type.node_class != etree_assert | |
1821 | && ass->exp->assign.dst[0] == '.' | |
73589426 AM |
1822 | && ass->exp->assign.dst[1] == 0) |
1823 | { | |
1824 | if (!ignore_first) | |
1825 | assign = where; | |
1826 | ignore_first = FALSE; | |
1827 | } | |
53d25da6 | 1828 | } |
53d25da6 AM |
1829 | continue; |
1830 | case lang_wild_statement_enum: | |
1831 | case lang_input_section_enum: | |
1832 | case lang_object_symbols_statement_enum: | |
1833 | case lang_fill_statement_enum: | |
1834 | case lang_data_statement_enum: | |
1835 | case lang_reloc_statement_enum: | |
1836 | case lang_padding_statement_enum: | |
1837 | case lang_constructors_statement_enum: | |
1838 | assign = NULL; | |
73589426 | 1839 | ignore_first = FALSE; |
53d25da6 AM |
1840 | continue; |
1841 | case lang_output_section_statement_enum: | |
1842 | if (assign != NULL) | |
0fa4d7cf AM |
1843 | { |
1844 | asection *s = (*where)->output_section_statement.bfd_section; | |
1845 | ||
249b2a84 L |
1846 | if (s == NULL |
1847 | || s->map_head.s == NULL | |
1848 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1849 | where = assign; |
1850 | } | |
53d25da6 AM |
1851 | break; |
1852 | case lang_input_statement_enum: | |
1853 | case lang_address_statement_enum: | |
1854 | case lang_target_statement_enum: | |
1855 | case lang_output_statement_enum: | |
1856 | case lang_group_statement_enum: | |
1857 | case lang_insert_statement_enum: | |
1858 | continue; | |
1859 | } | |
1860 | break; | |
1861 | } | |
1862 | ||
1863 | return where; | |
1864 | } | |
1865 | ||
afd7a018 | 1866 | lang_output_section_statement_type * |
7b986e99 | 1867 | lang_insert_orphan (asection *s, |
afd7a018 | 1868 | const char *secname, |
8a99a385 | 1869 | int constraint, |
afd7a018 AM |
1870 | lang_output_section_statement_type *after, |
1871 | struct orphan_save *place, | |
1872 | etree_type *address, | |
1873 | lang_statement_list_type *add_child) | |
1874 | { | |
afd7a018 | 1875 | lang_statement_list_type add; |
afd7a018 AM |
1876 | lang_output_section_statement_type *os; |
1877 | lang_output_section_statement_type **os_tail; | |
1878 | ||
afd7a018 AM |
1879 | /* If we have found an appropriate place for the output section |
1880 | statements for this orphan, add them to our own private list, | |
1881 | inserting them later into the global statement list. */ | |
1882 | if (after != NULL) | |
1883 | { | |
bde18da4 AM |
1884 | lang_list_init (&add); |
1885 | push_stat_ptr (&add); | |
afd7a018 AM |
1886 | } |
1887 | ||
0e1862bb L |
1888 | if (bfd_link_relocatable (&link_info) |
1889 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
1890 | address = exp_intop (0); |
1891 | ||
5c1e6d53 | 1892 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
1e9cc1c2 | 1893 | os = lang_enter_output_section_statement (secname, address, normal_section, |
1eec346e | 1894 | NULL, NULL, NULL, constraint, 0); |
011aa75f | 1895 | |
afd7a018 AM |
1896 | if (add_child == NULL) |
1897 | add_child = &os->children; | |
b9c361e0 | 1898 | lang_add_section (add_child, s, NULL, os); |
afd7a018 | 1899 | |
f77c3948 DJ |
1900 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
1901 | { | |
1902 | const char *region = (after->region | |
1903 | ? after->region->name_list.name | |
1904 | : DEFAULT_MEMORY_REGION); | |
1905 | const char *lma_region = (after->lma_region | |
1906 | ? after->lma_region->name_list.name | |
1907 | : NULL); | |
1908 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
1909 | lma_region); | |
1910 | } | |
1911 | else | |
1912 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
1913 | NULL); | |
afd7a018 | 1914 | |
afd7a018 AM |
1915 | /* Restore the global list pointer. */ |
1916 | if (after != NULL) | |
bde18da4 | 1917 | pop_stat_ptr (); |
afd7a018 AM |
1918 | |
1919 | if (after != NULL && os->bfd_section != NULL) | |
1920 | { | |
5daa8fe7 | 1921 | asection *snew, *as; |
1887ae73 | 1922 | bfd_boolean place_after = place->stmt == NULL; |
f3e660db | 1923 | bfd_boolean insert_after = TRUE; |
afd7a018 AM |
1924 | |
1925 | snew = os->bfd_section; | |
1926 | ||
1927 | /* Shuffle the bfd section list to make the output file look | |
1928 | neater. This is really only cosmetic. */ | |
1929 | if (place->section == NULL | |
8ce18f9c | 1930 | && after != (void *) lang_os_list.head) |
afd7a018 AM |
1931 | { |
1932 | asection *bfd_section = after->bfd_section; | |
1933 | ||
1934 | /* If the output statement hasn't been used to place any input | |
1935 | sections (and thus doesn't have an output bfd_section), | |
1936 | look for the closest prior output statement having an | |
1937 | output section. */ | |
1938 | if (bfd_section == NULL) | |
1939 | bfd_section = output_prev_sec_find (after); | |
1940 | ||
1941 | if (bfd_section != NULL && bfd_section != snew) | |
1942 | place->section = &bfd_section->next; | |
1943 | } | |
1944 | ||
1945 | if (place->section == NULL) | |
f13a99db | 1946 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1947 | |
5daa8fe7 | 1948 | as = *place->section; |
5e542ba7 MS |
1949 | |
1950 | if (!as) | |
329c1c86 AM |
1951 | { |
1952 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1953 | |
1954 | /* Unlink the section. */ | |
f13a99db | 1955 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1956 | |
1957 | /* Now tack it back on in the right place. */ | |
f13a99db | 1958 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1959 | } |
1887ae73 L |
1960 | else if ((bfd_get_flavour (link_info.output_bfd) |
1961 | == bfd_target_elf_flavour) | |
1962 | && (bfd_get_flavour (s->owner) | |
1963 | == bfd_target_elf_flavour) | |
1964 | && ((elf_section_type (s) == SHT_NOTE | |
1965 | && (s->flags & SEC_LOAD) != 0) | |
1966 | || (elf_section_type (as) == SHT_NOTE | |
1967 | && (as->flags & SEC_LOAD) != 0))) | |
1968 | { | |
1969 | /* Make sure that output note sections are grouped and sorted | |
1970 | by alignments when inserting a note section or insert a | |
1971 | section after a note section, */ | |
1972 | asection *sec; | |
1973 | /* A specific section after which the output note section | |
1974 | should be placed. */ | |
1975 | asection *after_sec; | |
1976 | /* True if we need to insert the orphan section after a | |
1977 | specific section to maintain output note section order. */ | |
f3e660db L |
1978 | bfd_boolean after_sec_note = FALSE; |
1979 | ||
1980 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
1981 | |
1982 | /* Group and sort output note section by alignments in | |
1983 | ascending order. */ | |
1984 | after_sec = NULL; | |
1985 | if (elf_section_type (s) == SHT_NOTE | |
1986 | && (s->flags & SEC_LOAD) != 0) | |
1987 | { | |
f3e660db L |
1988 | /* Search from the beginning for the last output note |
1989 | section with equal or larger alignments. NB: Don't | |
1990 | place orphan note section after non-note sections. */ | |
1887ae73 | 1991 | |
f3e660db L |
1992 | first_orphan_note = NULL; |
1993 | for (sec = link_info.output_bfd->sections; | |
1887ae73 L |
1994 | (sec != NULL |
1995 | && !bfd_is_abs_section (sec)); | |
1996 | sec = sec->next) | |
1997 | if (sec != snew | |
1998 | && elf_section_type (sec) == SHT_NOTE | |
1999 | && (sec->flags & SEC_LOAD) != 0) | |
2000 | { | |
f3e660db L |
2001 | if (!first_orphan_note) |
2002 | first_orphan_note = sec; | |
1887ae73 L |
2003 | if (sec->alignment_power >= s->alignment_power) |
2004 | after_sec = sec; | |
2005 | } | |
f3e660db L |
2006 | else if (first_orphan_note) |
2007 | { | |
2008 | /* Stop if there is non-note section after the first | |
2009 | orphan note section. */ | |
2010 | break; | |
2011 | } | |
1887ae73 | 2012 | |
f3e660db L |
2013 | /* If this will be the first orphan note section, it can |
2014 | be placed at the default location. */ | |
2015 | after_sec_note = first_orphan_note != NULL; | |
2016 | if (after_sec == NULL && after_sec_note) | |
1887ae73 | 2017 | { |
f3e660db L |
2018 | /* If all output note sections have smaller |
2019 | alignments, place the section before all | |
2020 | output orphan note sections. */ | |
2021 | after_sec = first_orphan_note; | |
2022 | insert_after = FALSE; | |
1887ae73 L |
2023 | } |
2024 | } | |
f3e660db | 2025 | else if (first_orphan_note) |
1887ae73 | 2026 | { |
f3e660db L |
2027 | /* Don't place non-note sections in the middle of orphan |
2028 | note sections. */ | |
1887ae73 L |
2029 | after_sec_note = TRUE; |
2030 | after_sec = as; | |
2031 | for (sec = as->next; | |
2032 | (sec != NULL | |
2033 | && !bfd_is_abs_section (sec)); | |
2034 | sec = sec->next) | |
2035 | if (elf_section_type (sec) == SHT_NOTE | |
2036 | && (sec->flags & SEC_LOAD) != 0) | |
2037 | after_sec = sec; | |
2038 | } | |
2039 | ||
2040 | if (after_sec_note) | |
2041 | { | |
2042 | if (after_sec) | |
2043 | { | |
f3e660db L |
2044 | /* Search forward to insert OS after AFTER_SEC output |
2045 | statement. */ | |
2046 | lang_output_section_statement_type *stmt, *next; | |
2047 | bfd_boolean found = FALSE; | |
2048 | for (stmt = after; stmt != NULL; stmt = next) | |
2049 | { | |
2050 | next = stmt->next; | |
2051 | if (insert_after) | |
2052 | { | |
2053 | if (stmt->bfd_section == after_sec) | |
2054 | { | |
2055 | place_after = TRUE; | |
2056 | found = TRUE; | |
2057 | after = stmt; | |
2058 | break; | |
2059 | } | |
2060 | } | |
2061 | else | |
2062 | { | |
2063 | /* If INSERT_AFTER is FALSE, place OS before | |
2064 | AFTER_SEC output statement. */ | |
2065 | if (next && next->bfd_section == after_sec) | |
2066 | { | |
2067 | place_after = TRUE; | |
2068 | found = TRUE; | |
2069 | after = stmt; | |
2070 | break; | |
2071 | } | |
2072 | } | |
2073 | } | |
2074 | ||
2075 | /* Search backward to insert OS after AFTER_SEC output | |
2076 | statement. */ | |
2077 | if (!found) | |
2078 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
1887ae73 | 2079 | { |
f3e660db L |
2080 | if (insert_after) |
2081 | { | |
2082 | if (stmt->bfd_section == after_sec) | |
2083 | { | |
2084 | place_after = TRUE; | |
2085 | after = stmt; | |
2086 | break; | |
2087 | } | |
2088 | } | |
2089 | else | |
2090 | { | |
2091 | /* If INSERT_AFTER is FALSE, place OS before | |
2092 | AFTER_SEC output statement. */ | |
2093 | if (stmt->next->bfd_section == after_sec) | |
2094 | { | |
2095 | place_after = TRUE; | |
2096 | after = stmt; | |
2097 | break; | |
2098 | } | |
2099 | } | |
1887ae73 L |
2100 | } |
2101 | } | |
2102 | ||
f3e660db L |
2103 | if (after_sec == NULL |
2104 | || (insert_after && after_sec->next != snew) | |
2105 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
2106 | { |
2107 | /* Unlink the section. */ | |
2108 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2109 | ||
2110 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2111 | prepend SNEW. */ | |
2112 | if (after_sec) | |
f3e660db L |
2113 | { |
2114 | if (insert_after) | |
2115 | bfd_section_list_insert_after (link_info.output_bfd, | |
2116 | after_sec, snew); | |
2117 | else | |
2118 | bfd_section_list_insert_before (link_info.output_bfd, | |
2119 | after_sec, snew); | |
2120 | } | |
1887ae73 L |
2121 | else |
2122 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2123 | } | |
2124 | } | |
2125 | else if (as != snew && as->prev != snew) | |
2126 | { | |
2127 | /* Unlink the section. */ | |
2128 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2129 | ||
2130 | /* Now tack it back on in the right place. */ | |
2131 | bfd_section_list_insert_before (link_info.output_bfd, | |
2132 | as, snew); | |
2133 | } | |
2134 | } | |
5e542ba7 | 2135 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2136 | { |
2137 | /* Unlink the section. */ | |
f13a99db | 2138 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2139 | |
5daa8fe7 | 2140 | /* Now tack it back on in the right place. */ |
f13a99db | 2141 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2142 | } |
afd7a018 AM |
2143 | |
2144 | /* Save the end of this list. Further ophans of this type will | |
2145 | follow the one we've just added. */ | |
2146 | place->section = &snew->next; | |
2147 | ||
2148 | /* The following is non-cosmetic. We try to put the output | |
2149 | statements in some sort of reasonable order here, because they | |
2150 | determine the final load addresses of the orphan sections. | |
2151 | In addition, placing output statements in the wrong order may | |
2152 | require extra segments. For instance, given a typical | |
2153 | situation of all read-only sections placed in one segment and | |
2154 | following that a segment containing all the read-write | |
2155 | sections, we wouldn't want to place an orphan read/write | |
2156 | section before or amongst the read-only ones. */ | |
2157 | if (add.head != NULL) | |
2158 | { | |
2159 | lang_output_section_statement_type *newly_added_os; | |
2160 | ||
1887ae73 L |
2161 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2162 | if (place_after) | |
afd7a018 | 2163 | { |
53d25da6 | 2164 | lang_statement_union_type **where = insert_os_after (after); |
afd7a018 AM |
2165 | |
2166 | *add.tail = *where; | |
2167 | *where = add.head; | |
2168 | ||
2169 | place->os_tail = &after->next; | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | /* Put it after the last orphan statement we added. */ | |
2174 | *add.tail = *place->stmt; | |
2175 | *place->stmt = add.head; | |
2176 | } | |
2177 | ||
2178 | /* Fix the global list pointer if we happened to tack our | |
2179 | new list at the tail. */ | |
bde18da4 AM |
2180 | if (*stat_ptr->tail == add.head) |
2181 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
2182 | |
2183 | /* Save the end of this list. */ | |
2184 | place->stmt = add.tail; | |
2185 | ||
2186 | /* Do the same for the list of output section statements. */ | |
2187 | newly_added_os = *os_tail; | |
2188 | *os_tail = NULL; | |
218868ba AM |
2189 | newly_added_os->prev = (lang_output_section_statement_type *) |
2190 | ((char *) place->os_tail | |
2191 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2192 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2193 | if (newly_added_os->next != NULL) |
2194 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
2195 | *place->os_tail = newly_added_os; |
2196 | place->os_tail = &newly_added_os->next; | |
2197 | ||
2198 | /* Fixing the global list pointer here is a little different. | |
2199 | We added to the list in lang_enter_output_section_statement, | |
2200 | trimmed off the new output_section_statment above when | |
2201 | assigning *os_tail = NULL, but possibly added it back in | |
2202 | the same place when assigning *place->os_tail. */ | |
2203 | if (*os_tail == NULL) | |
5c1e6d53 | 2204 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2205 | } |
2206 | } | |
2207 | return os; | |
2208 | } | |
2209 | ||
16e4ecc0 AM |
2210 | static void |
2211 | lang_print_asneeded (void) | |
2212 | { | |
2213 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2214 | |
2215 | if (asneeded_list_head == NULL) | |
2216 | return; | |
2217 | ||
1273da04 | 2218 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2219 | |
2220 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2221 | { | |
2222 | size_t len; | |
2223 | ||
2224 | minfo ("%s", m->soname); | |
2225 | len = strlen (m->soname); | |
2226 | ||
2227 | if (len >= 29) | |
2228 | { | |
2229 | print_nl (); | |
2230 | len = 0; | |
2231 | } | |
2232 | while (len < 30) | |
2233 | { | |
2234 | print_space (); | |
2235 | ++len; | |
2236 | } | |
2237 | ||
2238 | if (m->ref != NULL) | |
871b3ab2 | 2239 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2240 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2241 | } |
2242 | } | |
2243 | ||
252b5132 | 2244 | static void |
1579bae1 | 2245 | lang_map_flags (flagword flag) |
252b5132 RH |
2246 | { |
2247 | if (flag & SEC_ALLOC) | |
2248 | minfo ("a"); | |
2249 | ||
2250 | if (flag & SEC_CODE) | |
2251 | minfo ("x"); | |
2252 | ||
2253 | if (flag & SEC_READONLY) | |
2254 | minfo ("r"); | |
2255 | ||
2256 | if (flag & SEC_DATA) | |
2257 | minfo ("w"); | |
2258 | ||
2259 | if (flag & SEC_LOAD) | |
2260 | minfo ("l"); | |
2261 | } | |
2262 | ||
2263 | void | |
1579bae1 | 2264 | lang_map (void) |
252b5132 RH |
2265 | { |
2266 | lang_memory_region_type *m; | |
4d4920ec | 2267 | bfd_boolean dis_header_printed = FALSE; |
252b5132 | 2268 | |
4d4920ec EB |
2269 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2270 | { | |
2271 | asection *s; | |
2272 | ||
2273 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2274 | || file->flags.just_syms) |
4d4920ec EB |
2275 | continue; |
2276 | ||
035801ce FS |
2277 | if (config.print_map_discarded) |
2278 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2279 | if ((s->output_section == NULL | |
2280 | || s->output_section->owner != link_info.output_bfd) | |
2281 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2282 | { | |
2283 | if (! dis_header_printed) | |
2284 | { | |
2285 | fprintf (config.map_file, _("\nDiscarded input sections\n\n")); | |
2286 | dis_header_printed = TRUE; | |
2287 | } | |
4d4920ec | 2288 | |
035801ce FS |
2289 | print_input_section (s, TRUE); |
2290 | } | |
4d4920ec EB |
2291 | } |
2292 | ||
252b5132 RH |
2293 | minfo (_("\nMemory Configuration\n\n")); |
2294 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2295 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2296 | ||
1579bae1 | 2297 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 RH |
2298 | { |
2299 | char buf[100]; | |
2300 | int len; | |
2301 | ||
4a93e180 | 2302 | fprintf (config.map_file, "%-16s ", m->name_list.name); |
252b5132 RH |
2303 | |
2304 | sprintf_vma (buf, m->origin); | |
2305 | minfo ("0x%s ", buf); | |
2306 | len = strlen (buf); | |
2307 | while (len < 16) | |
2308 | { | |
2309 | print_space (); | |
2310 | ++len; | |
2311 | } | |
2312 | ||
2313 | minfo ("0x%V", m->length); | |
2314 | if (m->flags || m->not_flags) | |
2315 | { | |
2316 | #ifndef BFD64 | |
2317 | minfo (" "); | |
2318 | #endif | |
2319 | if (m->flags) | |
2320 | { | |
2321 | print_space (); | |
2322 | lang_map_flags (m->flags); | |
2323 | } | |
2324 | ||
2325 | if (m->not_flags) | |
2326 | { | |
2327 | minfo (" !"); | |
2328 | lang_map_flags (m->not_flags); | |
2329 | } | |
2330 | } | |
2331 | ||
2332 | print_nl (); | |
2333 | } | |
2334 | ||
2335 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
2336 | ||
0aa7f586 | 2337 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2338 | { |
2339 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2340 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2341 | } | |
6a846243 | 2342 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2343 | lang_statement_iteration++; |
252b5132 | 2344 | print_statements (); |
7a2f2d82 | 2345 | |
0aa7f586 AM |
2346 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2347 | config.map_file); | |
252b5132 RH |
2348 | } |
2349 | ||
35835446 | 2350 | static bfd_boolean |
967928e9 AM |
2351 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2352 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2353 | { |
6fcc66ab AM |
2354 | if ((hash_entry->type == bfd_link_hash_defined |
2355 | || hash_entry->type == bfd_link_hash_defweak) | |
2356 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2357 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2358 | { |
6fcc66ab | 2359 | input_section_userdata_type *ud; |
35835446 JR |
2360 | struct map_symbol_def *def; |
2361 | ||
a48931cc | 2362 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2363 | if (!ud) |
35835446 | 2364 | { |
988de25b | 2365 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2366 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2367 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2368 | ud->map_symbol_def_count = 0; | |
35835446 | 2369 | } |
6fcc66ab | 2370 | else if (!ud->map_symbol_def_tail) |
35835446 | 2371 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2372 | |
1e9cc1c2 | 2373 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2374 | def->entry = hash_entry; |
76d7af2d | 2375 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2376 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2377 | ud->map_symbol_def_count++; |
35835446 JR |
2378 | } |
2379 | return TRUE; | |
2380 | } | |
2381 | ||
252b5132 RH |
2382 | /* Initialize an output section. */ |
2383 | ||
2384 | static void | |
dfa7b0b8 | 2385 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2386 | { |
252b5132 | 2387 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
df5f2391 | 2388 | einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2389 | |
8a99a385 AM |
2390 | if (s->constraint != SPECIAL) |
2391 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); | |
1579bae1 | 2392 | if (s->bfd_section == NULL) |
8a99a385 AM |
2393 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2394 | s->name, flags); | |
1579bae1 | 2395 | if (s->bfd_section == NULL) |
252b5132 | 2396 | { |
df5f2391 | 2397 | einfo (_("%F%P: output format %s cannot represent section" |
0aa7f586 | 2398 | " called %s: %E\n"), |
f13a99db | 2399 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2400 | } |
2401 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2402 | s->bfd_section->output_offset = 0; |
e0f6802f | 2403 | |
24ef1aa7 GM |
2404 | /* Set the userdata of the output section to the output section |
2405 | statement to avoid lookup. */ | |
a48931cc | 2406 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2407 | |
a431bc2e AM |
2408 | /* If there is a base address, make sure that any sections it might |
2409 | mention are initialized. */ | |
2410 | if (s->addr_tree != NULL) | |
2411 | exp_init_os (s->addr_tree); | |
2412 | ||
2413 | if (s->load_base != NULL) | |
2414 | exp_init_os (s->load_base); | |
2415 | ||
7270c5ed | 2416 | /* If supplied an alignment, set it. */ |
3d9c8f6b AM |
2417 | if (s->section_alignment != NULL) |
2418 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, | |
2419 | "section alignment"); | |
252b5132 RH |
2420 | } |
2421 | ||
2422 | /* Make sure that all output sections mentioned in an expression are | |
2423 | initialized. */ | |
2424 | ||
2425 | static void | |
1579bae1 | 2426 | exp_init_os (etree_type *exp) |
252b5132 RH |
2427 | { |
2428 | switch (exp->type.node_class) | |
2429 | { | |
2430 | case etree_assign: | |
9f4fb502 | 2431 | case etree_provide: |
eab62f2f | 2432 | case etree_provided: |
252b5132 RH |
2433 | exp_init_os (exp->assign.src); |
2434 | break; | |
2435 | ||
2436 | case etree_binary: | |
2437 | exp_init_os (exp->binary.lhs); | |
2438 | exp_init_os (exp->binary.rhs); | |
2439 | break; | |
2440 | ||
2441 | case etree_trinary: | |
2442 | exp_init_os (exp->trinary.cond); | |
2443 | exp_init_os (exp->trinary.lhs); | |
2444 | exp_init_os (exp->trinary.rhs); | |
2445 | break; | |
2446 | ||
b6ca8815 NS |
2447 | case etree_assert: |
2448 | exp_init_os (exp->assert_s.child); | |
2449 | break; | |
afd7a018 | 2450 | |
252b5132 RH |
2451 | case etree_unary: |
2452 | exp_init_os (exp->unary.child); | |
2453 | break; | |
2454 | ||
2455 | case etree_name: | |
2456 | switch (exp->type.node_code) | |
2457 | { | |
2458 | case ADDR: | |
2459 | case LOADADDR: | |
2460 | case SIZEOF: | |
2461 | { | |
2462 | lang_output_section_statement_type *os; | |
2463 | ||
2464 | os = lang_output_section_find (exp->name.name); | |
2465 | if (os != NULL && os->bfd_section == NULL) | |
dfa7b0b8 | 2466 | init_os (os, 0); |
252b5132 RH |
2467 | } |
2468 | } | |
2469 | break; | |
2470 | ||
2471 | default: | |
2472 | break; | |
2473 | } | |
2474 | } | |
9503fd87 | 2475 | \f |
252b5132 | 2476 | static void |
1579bae1 | 2477 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2478 | { |
1e9cc1c2 | 2479 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2480 | |
2481 | /* If we are only reading symbols from this object, then we want to | |
2482 | discard all sections. */ | |
66be1055 | 2483 | if (entry->flags.just_syms) |
252b5132 | 2484 | { |
1449d79b | 2485 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2486 | return; |
2487 | } | |
2488 | ||
2e84f9c1 AM |
2489 | /* Deal with SHF_EXCLUDE ELF sections. */ |
2490 | if (!bfd_link_relocatable (&link_info) | |
2491 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2492 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2493 | sec->output_section = bfd_abs_section_ptr; | |
2494 | ||
ace66bb2 | 2495 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2496 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2497 | } |
2498 | \f | |
5b5f4e6f AB |
2499 | |
2500 | /* Returns true if SECTION is one we know will be discarded based on its | |
2501 | section flags, otherwise returns false. */ | |
2502 | ||
2503 | static bfd_boolean | |
2504 | lang_discard_section_p (asection *section) | |
2505 | { | |
2506 | bfd_boolean discard; | |
2507 | flagword flags = section->flags; | |
2508 | ||
2509 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2510 | discard = (flags & SEC_EXCLUDE) != 0; | |
2511 | ||
2512 | /* Discard the group descriptor sections when we're finally placing the | |
2513 | sections from within the group. */ | |
2514 | if ((flags & SEC_GROUP) != 0 | |
2515 | && link_info.resolve_section_groups) | |
2516 | discard = TRUE; | |
2517 | ||
2518 | /* Discard debugging sections if we are stripping debugging | |
2519 | information. */ | |
2520 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2521 | && (flags & SEC_DEBUGGING) != 0) | |
2522 | discard = TRUE; | |
2523 | ||
2524 | return discard; | |
2525 | } | |
2526 | ||
252b5132 RH |
2527 | /* The wild routines. |
2528 | ||
2529 | These expand statements like *(.text) and foo.o to a list of | |
2530 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2531 | foo.o(.text, .data). */ | |
2532 | ||
252b5132 | 2533 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
b9c361e0 | 2534 | lang_input_section statement which is placed at PTR. */ |
252b5132 RH |
2535 | |
2536 | void | |
1579bae1 AM |
2537 | lang_add_section (lang_statement_list_type *ptr, |
2538 | asection *section, | |
b9c361e0 | 2539 | struct flag_info *sflag_info, |
7b986e99 | 2540 | lang_output_section_statement_type *output) |
252b5132 | 2541 | { |
164e712d | 2542 | flagword flags = section->flags; |
ae17ab41 | 2543 | |
b34976b6 | 2544 | bfd_boolean discard; |
dfa7b0b8 | 2545 | lang_input_section_type *new_section; |
ae17ab41 | 2546 | bfd *abfd = link_info.output_bfd; |
252b5132 | 2547 | |
5b5f4e6f AB |
2548 | /* Is this section one we know should be discarded? */ |
2549 | discard = lang_discard_section_p (section); | |
252b5132 RH |
2550 | |
2551 | /* Discard input sections which are assigned to a section named | |
2552 | DISCARD_SECTION_NAME. */ | |
2553 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
b34976b6 | 2554 | discard = TRUE; |
252b5132 | 2555 | |
252b5132 RH |
2556 | if (discard) |
2557 | { | |
2558 | if (section->output_section == NULL) | |
2559 | { | |
2560 | /* This prevents future calls from assigning this section. */ | |
2561 | section->output_section = bfd_abs_section_ptr; | |
2562 | } | |
abf874aa CL |
2563 | else if (link_info.non_contiguous_regions_warnings) |
2564 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
2565 | "section `%pA' from '%pB' match /DISCARD/ clause.\n"), | |
2566 | NULL, section, section->owner); | |
2567 | ||
252b5132 RH |
2568 | return; |
2569 | } | |
2570 | ||
ae17ab41 CM |
2571 | if (sflag_info) |
2572 | { | |
b9c361e0 | 2573 | bfd_boolean keep; |
ae17ab41 | 2574 | |
b9c361e0 JL |
2575 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); |
2576 | if (!keep) | |
4724d37e | 2577 | return; |
ae17ab41 CM |
2578 | } |
2579 | ||
dfa7b0b8 | 2580 | if (section->output_section != NULL) |
abf874aa CL |
2581 | { |
2582 | if (!link_info.non_contiguous_regions) | |
2583 | return; | |
2584 | ||
2585 | /* SECTION has already been handled in a special way | |
2586 | (eg. LINK_ONCE): skip it. */ | |
2587 | if (bfd_is_abs_section (section->output_section)) | |
2588 | return; | |
2589 | ||
2590 | /* Already assigned to the same output section, do not process | |
2591 | it again, to avoid creating loops between duplicate sections | |
2592 | later. */ | |
2593 | if (section->output_section == output->bfd_section) | |
2594 | return; | |
2595 | ||
2596 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2597 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
2598 | "change behaviour for section `%pA' from '%pB' (assigned to " | |
2599 | "%pA, but additional match: %pA)\n"), | |
2600 | NULL, section, section->owner, section->output_section, | |
2601 | output->bfd_section); | |
2602 | ||
2603 | /* SECTION has already been assigned to an output section, but | |
2604 | the user allows it to be mapped to another one in case it | |
2605 | overflows. We'll later update the actual output section in | |
2606 | size_input_section as appropriate. */ | |
2607 | } | |
252b5132 | 2608 | |
dfa7b0b8 AM |
2609 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
2610 | to an output section, because we want to be able to include a | |
2611 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2612 | section (I don't know why we want to do this, but we do). | |
2613 | build_link_order in ldwrite.c handles this case by turning | |
2614 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2615 | flags &= ~ SEC_NEVER_LOAD; | |
8423293d | 2616 | |
dfa7b0b8 AM |
2617 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2618 | already been processed. One reason to do this is that on pe | |
2619 | format targets, .text$foo sections go into .text and it's odd | |
2620 | to see .text with SEC_LINK_ONCE set. */ | |
7bdf4127 AB |
2621 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2622 | { | |
2623 | if (link_info.resolve_section_groups) | |
6c19b93b | 2624 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2625 | else |
6c19b93b | 2626 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2627 | } |
2628 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2629 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2630 | |
dfa7b0b8 AM |
2631 | switch (output->sectype) |
2632 | { | |
2633 | case normal_section: | |
2634 | case overlay_section: | |
7b243801 | 2635 | case first_overlay_section: |
dfa7b0b8 AM |
2636 | break; |
2637 | case noalloc_section: | |
2638 | flags &= ~SEC_ALLOC; | |
2639 | break; | |
2640 | case noload_section: | |
2641 | flags &= ~SEC_LOAD; | |
2642 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
2643 | /* Unfortunately GNU ld has managed to evolve two different |
2644 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2645 | alloc, no contents section. All others get a noload, noalloc | |
2646 | section. */ | |
2647 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
2e76e85a | 2648 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2649 | else |
2650 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2651 | break; |
2652 | } | |
252b5132 | 2653 | |
dfa7b0b8 AM |
2654 | if (output->bfd_section == NULL) |
2655 | init_os (output, flags); | |
252b5132 | 2656 | |
dfa7b0b8 AM |
2657 | /* If SEC_READONLY is not set in the input section, then clear |
2658 | it from the output section. */ | |
2659 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
252b5132 | 2660 | |
dfa7b0b8 AM |
2661 | if (output->bfd_section->linker_has_input) |
2662 | { | |
2663 | /* Only set SEC_READONLY flag on the first input section. */ | |
2664 | flags &= ~ SEC_READONLY; | |
252b5132 | 2665 | |
f5fa8ca2 | 2666 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
2667 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
2668 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2669 | || ((flags & SEC_MERGE) != 0 | |
2670 | && output->bfd_section->entsize != section->entsize)) | |
f5fa8ca2 | 2671 | { |
afd7a018 | 2672 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2673 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2674 | } | |
dfa7b0b8 AM |
2675 | } |
2676 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2677 | |
dfa7b0b8 AM |
2678 | if (!output->bfd_section->linker_has_input) |
2679 | { | |
2680 | output->bfd_section->linker_has_input = 1; | |
2681 | /* This must happen after flags have been updated. The output | |
2682 | section may have been created before we saw its first input | |
2683 | section, eg. for a data statement. */ | |
2684 | bfd_init_private_section_data (section->owner, section, | |
2685 | link_info.output_bfd, | |
2686 | output->bfd_section, | |
2687 | &link_info); | |
2688 | if ((flags & SEC_MERGE) != 0) | |
afd7a018 | 2689 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2690 | } |
f5fa8ca2 | 2691 | |
dfa7b0b8 AM |
2692 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
2693 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2694 | { | |
2695 | /* FIXME: This value should really be obtained from the bfd... */ | |
2696 | output->block_value = 128; | |
2697 | } | |
252b5132 | 2698 | |
dfa7b0b8 AM |
2699 | if (section->alignment_power > output->bfd_section->alignment_power) |
2700 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2701 | |
dfa7b0b8 | 2702 | section->output_section = output->bfd_section; |
252b5132 | 2703 | |
5b69e357 | 2704 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
2705 | { |
2706 | asection *s = output->bfd_section->map_tail.s; | |
2707 | output->bfd_section->map_tail.s = section; | |
2708 | section->map_head.s = NULL; | |
2709 | section->map_tail.s = s; | |
2710 | if (s != NULL) | |
2711 | s->map_head.s = section; | |
2712 | else | |
2713 | output->bfd_section->map_head.s = section; | |
252b5132 | 2714 | } |
dfa7b0b8 AM |
2715 | |
2716 | /* Add a section reference to the list. */ | |
2717 | new_section = new_stat (lang_input_section, ptr); | |
2718 | new_section->section = section; | |
252b5132 RH |
2719 | } |
2720 | ||
2721 | /* Handle wildcard sorting. This returns the lang_input_section which | |
2722 | should follow the one we are going to create for SECTION and FILE, | |
2723 | based on the sorting requirements of WILD. It returns NULL if the | |
2724 | new section should just go at the end of the current list. */ | |
2725 | ||
2726 | static lang_statement_union_type * | |
1579bae1 AM |
2727 | wild_sort (lang_wild_statement_type *wild, |
2728 | struct wildcard_list *sec, | |
2729 | lang_input_statement_type *file, | |
2730 | asection *section) | |
252b5132 | 2731 | { |
252b5132 RH |
2732 | lang_statement_union_type *l; |
2733 | ||
bcaa7b3e L |
2734 | if (!wild->filenames_sorted |
2735 | && (sec == NULL || sec->spec.sorted == none)) | |
252b5132 RH |
2736 | return NULL; |
2737 | ||
bba1a0c0 | 2738 | for (l = wild->children.head; l != NULL; l = l->header.next) |
252b5132 RH |
2739 | { |
2740 | lang_input_section_type *ls; | |
2741 | ||
2742 | if (l->header.type != lang_input_section_enum) | |
2743 | continue; | |
2744 | ls = &l->input_section; | |
2745 | ||
2746 | /* Sorting by filename takes precedence over sorting by section | |
afd7a018 | 2747 | name. */ |
252b5132 RH |
2748 | |
2749 | if (wild->filenames_sorted) | |
2750 | { | |
2751 | const char *fn, *ln; | |
b34976b6 | 2752 | bfd_boolean fa, la; |
252b5132 RH |
2753 | int i; |
2754 | ||
2755 | /* The PE support for the .idata section as generated by | |
afd7a018 AM |
2756 | dlltool assumes that files will be sorted by the name of |
2757 | the archive and then the name of the file within the | |
2758 | archive. */ | |
252b5132 RH |
2759 | |
2760 | if (file->the_bfd != NULL | |
3860d2b4 | 2761 | && file->the_bfd->my_archive != NULL) |
252b5132 | 2762 | { |
3860d2b4 | 2763 | fn = bfd_get_filename (file->the_bfd->my_archive); |
b34976b6 | 2764 | fa = TRUE; |
252b5132 RH |
2765 | } |
2766 | else | |
2767 | { | |
2768 | fn = file->filename; | |
b34976b6 | 2769 | fa = FALSE; |
252b5132 RH |
2770 | } |
2771 | ||
3860d2b4 | 2772 | if (ls->section->owner->my_archive != NULL) |
252b5132 | 2773 | { |
3860d2b4 | 2774 | ln = bfd_get_filename (ls->section->owner->my_archive); |
b34976b6 | 2775 | la = TRUE; |
252b5132 RH |
2776 | } |
2777 | else | |
2778 | { | |
607b4833 | 2779 | ln = bfd_get_filename (ls->section->owner); |
b34976b6 | 2780 | la = FALSE; |
252b5132 RH |
2781 | } |
2782 | ||
42627821 | 2783 | i = filename_cmp (fn, ln); |
252b5132 RH |
2784 | if (i > 0) |
2785 | continue; | |
2786 | else if (i < 0) | |
2787 | break; | |
2788 | ||
2789 | if (fa || la) | |
2790 | { | |
2791 | if (fa) | |
2792 | fn = file->filename; | |
2793 | if (la) | |
607b4833 | 2794 | ln = bfd_get_filename (ls->section->owner); |
252b5132 | 2795 | |
42627821 | 2796 | i = filename_cmp (fn, ln); |
252b5132 RH |
2797 | if (i > 0) |
2798 | continue; | |
2799 | else if (i < 0) | |
2800 | break; | |
2801 | } | |
2802 | } | |
2803 | ||
2804 | /* Here either the files are not sorted by name, or we are | |
afd7a018 | 2805 | looking at the sections for this file. */ |
252b5132 | 2806 | |
b2e4da5a L |
2807 | if (sec != NULL |
2808 | && sec->spec.sorted != none | |
2809 | && sec->spec.sorted != by_none) | |
32124d5b AM |
2810 | if (compare_section (sec->spec.sorted, section, ls->section) < 0) |
2811 | break; | |
252b5132 RH |
2812 | } |
2813 | ||
2814 | return l; | |
2815 | } | |
2816 | ||
2817 | /* Expand a wild statement for a particular FILE. SECTION may be | |
2818 | NULL, in which case it is a wild card. */ | |
2819 | ||
2820 | static void | |
1579bae1 AM |
2821 | output_section_callback (lang_wild_statement_type *ptr, |
2822 | struct wildcard_list *sec, | |
2823 | asection *section, | |
b9c361e0 | 2824 | struct flag_info *sflag_info, |
1579bae1 AM |
2825 | lang_input_statement_type *file, |
2826 | void *output) | |
4dec4d4e RH |
2827 | { |
2828 | lang_statement_union_type *before; | |
d0bf826b AM |
2829 | lang_output_section_statement_type *os; |
2830 | ||
2831 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2832 | |
b6bf44ba | 2833 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2834 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2835 | return; |
2836 | ||
b6bf44ba | 2837 | before = wild_sort (ptr, sec, file, section); |
5f992e62 | 2838 | |
4dec4d4e RH |
2839 | /* Here BEFORE points to the lang_input_section which |
2840 | should follow the one we are about to add. If BEFORE | |
2841 | is NULL, then the section should just go at the end | |
2842 | of the current list. */ | |
5f992e62 | 2843 | |
4dec4d4e | 2844 | if (before == NULL) |
b9c361e0 | 2845 | lang_add_section (&ptr->children, section, sflag_info, os); |
4dec4d4e | 2846 | else |
252b5132 | 2847 | { |
4dec4d4e RH |
2848 | lang_statement_list_type list; |
2849 | lang_statement_union_type **pp; | |
5f992e62 | 2850 | |
4dec4d4e | 2851 | lang_list_init (&list); |
b9c361e0 | 2852 | lang_add_section (&list, section, sflag_info, os); |
5f992e62 | 2853 | |
4dec4d4e RH |
2854 | /* If we are discarding the section, LIST.HEAD will |
2855 | be NULL. */ | |
2856 | if (list.head != NULL) | |
252b5132 | 2857 | { |
bba1a0c0 | 2858 | ASSERT (list.head->header.next == NULL); |
5f992e62 | 2859 | |
4dec4d4e RH |
2860 | for (pp = &ptr->children.head; |
2861 | *pp != before; | |
bba1a0c0 | 2862 | pp = &(*pp)->header.next) |
4dec4d4e | 2863 | ASSERT (*pp != NULL); |
5f992e62 | 2864 | |
bba1a0c0 | 2865 | list.head->header.next = *pp; |
4dec4d4e | 2866 | *pp = list.head; |
252b5132 RH |
2867 | } |
2868 | } | |
2869 | } | |
2870 | ||
0841712e JJ |
2871 | /* Check if all sections in a wild statement for a particular FILE |
2872 | are readonly. */ | |
2873 | ||
2874 | static void | |
2875 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2876 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2877 | asection *section, | |
b9c361e0 | 2878 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
0841712e | 2879 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
d0bf826b | 2880 | void *output) |
0841712e | 2881 | { |
d0bf826b AM |
2882 | lang_output_section_statement_type *os; |
2883 | ||
2884 | os = (lang_output_section_statement_type *) output; | |
2885 | ||
0841712e | 2886 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2887 | if (unique_section_p (section, os)) |
0841712e JJ |
2888 | return; |
2889 | ||
6feb9908 | 2890 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
d0bf826b | 2891 | os->all_input_readonly = FALSE; |
0841712e JJ |
2892 | } |
2893 | ||
252b5132 RH |
2894 | /* This is passed a file name which must have been seen already and |
2895 | added to the statement tree. We will see if it has been opened | |
2896 | already and had its symbols read. If not then we'll read it. */ | |
2897 | ||
2898 | static lang_input_statement_type * | |
1579bae1 | 2899 | lookup_name (const char *name) |
252b5132 RH |
2900 | { |
2901 | lang_input_statement_type *search; | |
2902 | ||
8ce18f9c | 2903 | for (search = (void *) input_file_chain.head; |
1579bae1 | 2904 | search != NULL; |
36983a93 | 2905 | search = search->next_real_file) |
252b5132 | 2906 | { |
0013291d NC |
2907 | /* Use the local_sym_name as the name of the file that has |
2908 | already been loaded as filename might have been transformed | |
2909 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2910 | const char *filename = search->local_sym_name; |
0013291d | 2911 | |
0013291d | 2912 | if (filename != NULL |
42627821 | 2913 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2914 | break; |
2915 | } | |
2916 | ||
1579bae1 | 2917 | if (search == NULL) |
1f1f5b92 AM |
2918 | { |
2919 | /* Arrange to splice the input statement added by new_afile into | |
2920 | statement_list after the current input_file_chain tail. | |
2921 | We know input_file_chain is not an empty list, and that | |
2922 | lookup_name was called via open_input_bfds. Later calls to | |
2923 | lookup_name should always match an existing input_statement. */ | |
2924 | lang_statement_union_type **tail = stat_ptr->tail; | |
2925 | lang_statement_union_type **after | |
2926 | = (void *) ((char *) input_file_chain.tail | |
2927 | - offsetof (lang_input_statement_type, next_real_file) | |
2928 | + offsetof (lang_input_statement_type, header.next)); | |
2929 | lang_statement_union_type *rest = *after; | |
2930 | stat_ptr->tail = after; | |
2931 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
16171946 | 2932 | default_target, NULL); |
1f1f5b92 AM |
2933 | *stat_ptr->tail = rest; |
2934 | if (*tail == NULL) | |
2935 | stat_ptr->tail = tail; | |
2936 | } | |
252b5132 RH |
2937 | |
2938 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 2939 | don't add this file. */ |
66be1055 | 2940 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
2941 | return search; |
2942 | ||
0aa7f586 | 2943 | if (!load_symbols (search, NULL)) |
6770ec8c | 2944 | return NULL; |
252b5132 RH |
2945 | |
2946 | return search; | |
2947 | } | |
2948 | ||
b58f81ae DJ |
2949 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2950 | ||
2951 | struct excluded_lib | |
2952 | { | |
2953 | char *name; | |
2954 | struct excluded_lib *next; | |
2955 | }; | |
2956 | static struct excluded_lib *excluded_libs; | |
2957 | ||
2958 | void | |
2959 | add_excluded_libs (const char *list) | |
2960 | { | |
2961 | const char *p = list, *end; | |
2962 | ||
2963 | while (*p != '\0') | |
2964 | { | |
2965 | struct excluded_lib *entry; | |
2966 | end = strpbrk (p, ",:"); | |
2967 | if (end == NULL) | |
2968 | end = p + strlen (p); | |
1e9cc1c2 | 2969 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
b58f81ae | 2970 | entry->next = excluded_libs; |
1e9cc1c2 | 2971 | entry->name = (char *) xmalloc (end - p + 1); |
b58f81ae DJ |
2972 | memcpy (entry->name, p, end - p); |
2973 | entry->name[end - p] = '\0'; | |
2974 | excluded_libs = entry; | |
2975 | if (*end == '\0') | |
329c1c86 | 2976 | break; |
b58f81ae DJ |
2977 | p = end + 1; |
2978 | } | |
2979 | } | |
2980 | ||
2981 | static void | |
2982 | check_excluded_libs (bfd *abfd) | |
2983 | { | |
2984 | struct excluded_lib *lib = excluded_libs; | |
2985 | ||
2986 | while (lib) | |
2987 | { | |
2988 | int len = strlen (lib->name); | |
607b4833 | 2989 | const char *filename = lbasename (bfd_get_filename (abfd)); |
b58f81ae DJ |
2990 | |
2991 | if (strcmp (lib->name, "ALL") == 0) | |
2992 | { | |
2993 | abfd->no_export = TRUE; | |
2994 | return; | |
2995 | } | |
2996 | ||
42627821 | 2997 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
2998 | && (filename[len] == '\0' |
2999 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
3000 | && filename[len + 2] == '\0'))) | |
3001 | { | |
3002 | abfd->no_export = TRUE; | |
3003 | return; | |
3004 | } | |
3005 | ||
3006 | lib = lib->next; | |
3007 | } | |
3008 | } | |
3009 | ||
252b5132 RH |
3010 | /* Get the symbols for an input file. */ |
3011 | ||
e9f53129 | 3012 | bfd_boolean |
1579bae1 AM |
3013 | load_symbols (lang_input_statement_type *entry, |
3014 | lang_statement_list_type *place) | |
252b5132 RH |
3015 | { |
3016 | char **matching; | |
3017 | ||
66be1055 | 3018 | if (entry->flags.loaded) |
b34976b6 | 3019 | return TRUE; |
252b5132 RH |
3020 | |
3021 | ldfile_open_file (entry); | |
3022 | ||
c4b78195 | 3023 | /* Do not process further if the file was missing. */ |
66be1055 | 3024 | if (entry->flags.missing_file) |
c4b78195 NC |
3025 | return TRUE; |
3026 | ||
727a29ba AM |
3027 | if (trace_files || verbose) |
3028 | info_msg ("%pI\n", entry); | |
3029 | ||
0aa7f586 AM |
3030 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
3031 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
3032 | { |
3033 | bfd_error_type err; | |
66be1055 | 3034 | struct lang_input_statement_flags save_flags; |
47e2e729 | 3035 | extern FILE *yyin; |
b7a26f91 | 3036 | |
252b5132 | 3037 | err = bfd_get_error (); |
884fb58e NC |
3038 | |
3039 | /* See if the emulation has some special knowledge. */ | |
3040 | if (ldemul_unrecognized_file (entry)) | |
b34976b6 | 3041 | return TRUE; |
884fb58e | 3042 | |
252b5132 RH |
3043 | if (err == bfd_error_file_ambiguously_recognized) |
3044 | { | |
3045 | char **p; | |
3046 | ||
df5f2391 AM |
3047 | einfo (_("%P: %pB: file not recognized: %E;" |
3048 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
3049 | for (p = matching; *p != NULL; p++) |
3050 | einfo (" %s", *p); | |
3051 | einfo ("%F\n"); | |
3052 | } | |
3053 | else if (err != bfd_error_file_not_recognized | |
3054 | || place == NULL) | |
df5f2391 | 3055 | einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 3056 | |
252b5132 RH |
3057 | bfd_close (entry->the_bfd); |
3058 | entry->the_bfd = NULL; | |
3059 | ||
252b5132 | 3060 | /* Try to interpret the file as a linker script. */ |
66be1055 | 3061 | save_flags = input_flags; |
252b5132 RH |
3062 | ldfile_open_command_file (entry->filename); |
3063 | ||
bde18da4 | 3064 | push_stat_ptr (place); |
66be1055 AM |
3065 | input_flags.add_DT_NEEDED_for_regular |
3066 | = entry->flags.add_DT_NEEDED_for_regular; | |
3067 | input_flags.add_DT_NEEDED_for_dynamic | |
3068 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
3069 | input_flags.whole_archive = entry->flags.whole_archive; | |
3070 | input_flags.dynamic = entry->flags.dynamic; | |
252b5132 | 3071 | |
b34976b6 | 3072 | ldfile_assumed_script = TRUE; |
252b5132 | 3073 | parser_input = input_script; |
16171946 | 3074 | current_input_file = entry->filename; |
252b5132 | 3075 | yyparse (); |
16171946 | 3076 | current_input_file = NULL; |
b34976b6 | 3077 | ldfile_assumed_script = FALSE; |
252b5132 | 3078 | |
f4a23d42 AM |
3079 | /* missing_file is sticky. sysrooted will already have been |
3080 | restored when seeing EOF in yyparse, but no harm to restore | |
3081 | again. */ | |
66be1055 AM |
3082 | save_flags.missing_file |= input_flags.missing_file; |
3083 | input_flags = save_flags; | |
bde18da4 | 3084 | pop_stat_ptr (); |
47e2e729 AM |
3085 | fclose (yyin); |
3086 | yyin = NULL; | |
3087 | entry->flags.loaded = TRUE; | |
252b5132 | 3088 | |
bde18da4 | 3089 | return TRUE; |
252b5132 RH |
3090 | } |
3091 | ||
3092 | if (ldemul_recognized_file (entry)) | |
b34976b6 | 3093 | return TRUE; |
252b5132 RH |
3094 | |
3095 | /* We don't call ldlang_add_file for an archive. Instead, the | |
3096 | add_symbols entry point will call ldlang_add_file, via the | |
3097 | add_archive_element callback, for each element of the archive | |
3098 | which is used. */ | |
3099 | switch (bfd_get_format (entry->the_bfd)) | |
3100 | { | |
3101 | default: | |
3102 | break; | |
3103 | ||
3104 | case bfd_object: | |
66be1055 | 3105 | if (!entry->flags.reload) |
15fc2e13 | 3106 | ldlang_add_file (entry); |
252b5132 RH |
3107 | break; |
3108 | ||
3109 | case bfd_archive: | |
b58f81ae DJ |
3110 | check_excluded_libs (entry->the_bfd); |
3111 | ||
00f93c44 | 3112 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 3113 | if (entry->flags.whole_archive) |
252b5132 | 3114 | { |
b7a26f91 | 3115 | bfd *member = NULL; |
b34976b6 | 3116 | bfd_boolean loaded = TRUE; |
6770ec8c NC |
3117 | |
3118 | for (;;) | |
252b5132 | 3119 | { |
5d3236ee | 3120 | bfd *subsbfd; |
6770ec8c NC |
3121 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
3122 | ||
3123 | if (member == NULL) | |
3124 | break; | |
b7a26f91 | 3125 | |
0aa7f586 | 3126 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 3127 | { |
df5f2391 | 3128 | einfo (_("%F%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 3129 | entry->the_bfd, member); |
b34976b6 | 3130 | loaded = FALSE; |
6770ec8c NC |
3131 | } |
3132 | ||
db0ca79f | 3133 | subsbfd = member; |
46105645 AM |
3134 | if (!(*link_info.callbacks |
3135 | ->add_archive_element) (&link_info, member, | |
3136 | "--whole-archive", &subsbfd)) | |
252b5132 | 3137 | abort (); |
6770ec8c | 3138 | |
5d3236ee DK |
3139 | /* Potentially, the add_archive_element hook may have set a |
3140 | substitute BFD for us. */ | |
46105645 | 3141 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 3142 | { |
df5f2391 | 3143 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), member); |
b34976b6 | 3144 | loaded = FALSE; |
6770ec8c | 3145 | } |
252b5132 RH |
3146 | } |
3147 | ||
66be1055 | 3148 | entry->flags.loaded = loaded; |
6770ec8c | 3149 | return loaded; |
252b5132 | 3150 | } |
6770ec8c | 3151 | break; |
252b5132 RH |
3152 | } |
3153 | ||
03bdc404 | 3154 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
66be1055 | 3155 | entry->flags.loaded = TRUE; |
6770ec8c | 3156 | else |
df5f2391 | 3157 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3158 | |
66be1055 | 3159 | return entry->flags.loaded; |
252b5132 RH |
3160 | } |
3161 | ||
b6bf44ba AM |
3162 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
3163 | may be NULL, indicating that it is a wildcard. Separate | |
3164 | lang_input_section statements are created for each part of the | |
3165 | expansion; they are added after the wild statement S. OUTPUT is | |
3166 | the output section. */ | |
252b5132 RH |
3167 | |
3168 | static void | |
1579bae1 AM |
3169 | wild (lang_wild_statement_type *s, |
3170 | const char *target ATTRIBUTE_UNUSED, | |
3171 | lang_output_section_statement_type *output) | |
252b5132 | 3172 | { |
b6bf44ba | 3173 | struct wildcard_list *sec; |
252b5132 | 3174 | |
50c77e5d | 3175 | if (s->handler_data[0] |
329c1c86 | 3176 | && s->handler_data[0]->spec.sorted == by_name |
50c77e5d NC |
3177 | && !s->filenames_sorted) |
3178 | { | |
329c1c86 AM |
3179 | lang_section_bst_type *tree; |
3180 | ||
50c77e5d NC |
3181 | walk_wild (s, output_section_callback_fast, output); |
3182 | ||
e6f2cbf5 | 3183 | tree = s->tree; |
329c1c86 | 3184 | if (tree) |
e6f2cbf5 L |
3185 | { |
3186 | output_section_callback_tree_to_list (s, tree, output); | |
3187 | s->tree = NULL; | |
3188 | } | |
50c77e5d | 3189 | } |
329c1c86 | 3190 | else |
50c77e5d | 3191 | walk_wild (s, output_section_callback, output); |
b6bf44ba | 3192 | |
abe6ac95 AM |
3193 | if (default_common_section == NULL) |
3194 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
3195 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
3196 | { | |
3197 | /* Remember the section that common is going to in case we | |
b7a26f91 | 3198 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3199 | default_common_section = output; |
abe6ac95 | 3200 | break; |
b6bf44ba | 3201 | } |
252b5132 RH |
3202 | } |
3203 | ||
b34976b6 | 3204 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3205 | |
e50d8076 | 3206 | static int |
1579bae1 | 3207 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3208 | { |
1e9cc1c2 | 3209 | const char *sought = (const char *) data; |
5f992e62 | 3210 | |
e50d8076 NC |
3211 | return strcmp (target->name, sought) == 0; |
3212 | } | |
3213 | ||
3214 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3215 | |
e50d8076 | 3216 | static void |
1d38e9d1 | 3217 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3218 | { |
3219 | char c; | |
5f992e62 | 3220 | |
08da4cac | 3221 | while ((c = *src++) != 0) |
3882b010 | 3222 | *dest++ = TOLOWER (c); |
e50d8076 | 3223 | |
08da4cac | 3224 | *dest = 0; |
e50d8076 NC |
3225 | } |
3226 | ||
396a2467 | 3227 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3228 | from haystack. */ |
08da4cac | 3229 | |
e50d8076 | 3230 | static void |
1d38e9d1 | 3231 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3232 | { |
3233 | haystack = strstr (haystack, needle); | |
5f992e62 | 3234 | |
e50d8076 NC |
3235 | if (haystack) |
3236 | { | |
08da4cac | 3237 | char *src; |
e50d8076 | 3238 | |
08da4cac KH |
3239 | for (src = haystack + strlen (needle); *src;) |
3240 | *haystack++ = *src++; | |
5f992e62 | 3241 | |
08da4cac | 3242 | *haystack = 0; |
e50d8076 NC |
3243 | } |
3244 | } | |
3245 | ||
3246 | /* Compare two target format name strings. | |
3247 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3248 | |
e50d8076 | 3249 | static int |
1d38e9d1 | 3250 | name_compare (const char *first, const char *second) |
e50d8076 | 3251 | { |
08da4cac KH |
3252 | char *copy1; |
3253 | char *copy2; | |
3254 | int result; | |
5f992e62 | 3255 | |
1e9cc1c2 NC |
3256 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3257 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3258 | |
3259 | /* Convert the names to lower case. */ | |
3260 | stricpy (copy1, first); | |
3261 | stricpy (copy2, second); | |
3262 | ||
1579bae1 | 3263 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
3264 | strcut (copy1, "big"); |
3265 | strcut (copy1, "little"); | |
3266 | strcut (copy2, "big"); | |
3267 | strcut (copy2, "little"); | |
3268 | ||
3269 | /* Return a value based on how many characters match, | |
3270 | starting from the beginning. If both strings are | |
3271 | the same then return 10 * their length. */ | |
08da4cac KH |
3272 | for (result = 0; copy1[result] == copy2[result]; result++) |
3273 | if (copy1[result] == 0) | |
e50d8076 NC |
3274 | { |
3275 | result *= 10; | |
3276 | break; | |
3277 | } | |
5f992e62 | 3278 | |
e50d8076 NC |
3279 | free (copy1); |
3280 | free (copy2); | |
3281 | ||
3282 | return result; | |
3283 | } | |
3284 | ||
3285 | /* Set by closest_target_match() below. */ | |
08da4cac | 3286 | static const bfd_target *winner; |
e50d8076 NC |
3287 | |
3288 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3289 | requirement that was specified on the command line, and is the nearest | |
3290 | match to the original output target. */ | |
08da4cac | 3291 | |
e50d8076 | 3292 | static int |
1579bae1 | 3293 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3294 | { |
1e9cc1c2 | 3295 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3296 | |
08da4cac KH |
3297 | if (command_line.endian == ENDIAN_BIG |
3298 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3299 | return 0; |
5f992e62 | 3300 | |
08da4cac KH |
3301 | if (command_line.endian == ENDIAN_LITTLE |
3302 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3303 | return 0; |
3304 | ||
3305 | /* Must be the same flavour. */ | |
3306 | if (target->flavour != original->flavour) | |
3307 | return 0; | |
3308 | ||
de7dd2bd | 3309 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3310 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
3311 | || strcmp (target->name, "elf64-big") == 0 |
3312 | || strcmp (target->name, "elf32-little") == 0 | |
3313 | || strcmp (target->name, "elf64-little") == 0) | |
3314 | return 0; | |
3315 | ||
e50d8076 NC |
3316 | /* If we have not found a potential winner yet, then record this one. */ |
3317 | if (winner == NULL) | |
3318 | { | |
3319 | winner = target; | |
3320 | return 0; | |
3321 | } | |
3322 | ||
3323 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 3324 | Compare their names and choose the better one. */ |
d1778b88 AM |
3325 | if (name_compare (target->name, original->name) |
3326 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
3327 | winner = target; |
3328 | ||
3329 | /* Keep on searching until wqe have checked them all. */ | |
3330 | return 0; | |
3331 | } | |
3332 | ||
3333 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 3334 | |
1d38e9d1 | 3335 | static const char * |
1579bae1 | 3336 | get_first_input_target (void) |
e50d8076 | 3337 | { |
1d38e9d1 | 3338 | const char *target = NULL; |
e50d8076 NC |
3339 | |
3340 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3341 | { | |
3342 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3343 | && s->flags.real) |
e50d8076 NC |
3344 | { |
3345 | ldfile_open_file (s); | |
5f992e62 | 3346 | |
e50d8076 NC |
3347 | if (s->the_bfd != NULL |
3348 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3349 | { | |
3350 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3351 | |
e50d8076 NC |
3352 | if (target != NULL) |
3353 | break; | |
3354 | } | |
3355 | } | |
3356 | } | |
5f992e62 | 3357 | |
e50d8076 NC |
3358 | return target; |
3359 | } | |
3360 | ||
599917b8 | 3361 | const char * |
1579bae1 | 3362 | lang_get_output_target (void) |
599917b8 JJ |
3363 | { |
3364 | const char *target; | |
3365 | ||
3366 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3367 | if (output_target != NULL) |
599917b8 JJ |
3368 | return output_target; |
3369 | ||
3370 | /* No - has the current target been set to something other than | |
3371 | the default? */ | |
30824704 | 3372 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
3373 | return current_target; |
3374 | ||
3375 | /* No - can we determine the format of the first input file? */ | |
3376 | target = get_first_input_target (); | |
3377 | if (target != NULL) | |
3378 | return target; | |
3379 | ||
3380 | /* Failed - use the default output target. */ | |
3381 | return default_target; | |
3382 | } | |
3383 | ||
252b5132 RH |
3384 | /* Open the output file. */ |
3385 | ||
f13a99db | 3386 | static void |
1579bae1 | 3387 | open_output (const char *name) |
252b5132 | 3388 | { |
599917b8 | 3389 | output_target = lang_get_output_target (); |
5f992e62 | 3390 | |
08da4cac KH |
3391 | /* Has the user requested a particular endianness on the command |
3392 | line? */ | |
e50d8076 NC |
3393 | if (command_line.endian != ENDIAN_UNSET) |
3394 | { | |
e50d8076 | 3395 | /* Get the chosen target. */ |
4212b42d AM |
3396 | const bfd_target *target |
3397 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3398 | |
c13b1b77 NC |
3399 | /* If the target is not supported, we cannot do anything. */ |
3400 | if (target != NULL) | |
e50d8076 | 3401 | { |
4212b42d AM |
3402 | enum bfd_endian desired_endian; |
3403 | ||
c13b1b77 NC |
3404 | if (command_line.endian == ENDIAN_BIG) |
3405 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3406 | else |
c13b1b77 | 3407 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
3408 | |
3409 | /* See if the target has the wrong endianness. This should | |
3410 | not happen if the linker script has provided big and | |
3411 | little endian alternatives, but some scrips don't do | |
3412 | this. */ | |
c13b1b77 | 3413 | if (target->byteorder != desired_endian) |
e50d8076 | 3414 | { |
c13b1b77 NC |
3415 | /* If it does, then see if the target provides |
3416 | an alternative with the correct endianness. */ | |
3417 | if (target->alternative_target != NULL | |
3418 | && (target->alternative_target->byteorder == desired_endian)) | |
3419 | output_target = target->alternative_target->name; | |
e50d8076 | 3420 | else |
c13b1b77 | 3421 | { |
5f992e62 AM |
3422 | /* Try to find a target as similar as possible to |
3423 | the default target, but which has the desired | |
3424 | endian characteristic. */ | |
4212b42d AM |
3425 | bfd_iterate_over_targets (closest_target_match, |
3426 | (void *) target); | |
5f992e62 AM |
3427 | |
3428 | /* Oh dear - we could not find any targets that | |
3429 | satisfy our requirements. */ | |
c13b1b77 | 3430 | if (winner == NULL) |
6feb9908 AM |
3431 | einfo (_("%P: warning: could not find any targets" |
3432 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3433 | else |
3434 | output_target = winner->name; | |
3435 | } | |
e50d8076 NC |
3436 | } |
3437 | } | |
252b5132 | 3438 | } |
5f992e62 | 3439 | |
f13a99db | 3440 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3441 | |
f13a99db | 3442 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3443 | { |
3444 | if (bfd_get_error () == bfd_error_invalid_target) | |
df5f2391 | 3445 | einfo (_("%F%P: target %s not found\n"), output_target); |
e50d8076 | 3446 | |
df5f2391 | 3447 | einfo (_("%F%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3448 | } |
3449 | ||
b34976b6 | 3450 | delete_output_file_on_failure = TRUE; |
252b5132 | 3451 | |
0aa7f586 | 3452 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
df5f2391 | 3453 | einfo (_("%F%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3454 | if (!bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
3455 | ldfile_output_architecture, |
3456 | ldfile_output_machine)) | |
df5f2391 | 3457 | einfo (_("%F%P: %s: can not set architecture: %E\n"), name); |
252b5132 | 3458 | |
f13a99db | 3459 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3460 | if (link_info.hash == NULL) |
df5f2391 | 3461 | einfo (_("%F%P: can not create hash table: %E\n")); |
252b5132 | 3462 | |
f13a99db | 3463 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3464 | } |
3465 | ||
252b5132 | 3466 | static void |
1579bae1 | 3467 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3468 | { |
3469 | switch (statement->header.type) | |
3470 | { | |
3471 | case lang_output_statement_enum: | |
f13a99db AM |
3472 | ASSERT (link_info.output_bfd == NULL); |
3473 | open_output (statement->output_statement.name); | |
252b5132 | 3474 | ldemul_set_output_arch (); |
0e1862bb L |
3475 | if (config.magic_demand_paged |
3476 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3477 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3478 | else |
f13a99db | 3479 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3480 | if (config.text_read_only) |
f13a99db | 3481 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3482 | else |
f13a99db | 3483 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3484 | if (link_info.traditional_format) |
f13a99db | 3485 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3486 | else |
f13a99db | 3487 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
3488 | break; |
3489 | ||
3490 | case lang_target_statement_enum: | |
3491 | current_target = statement->target_statement.target; | |
3492 | break; | |
3493 | default: | |
3494 | break; | |
3495 | } | |
3496 | } | |
3497 | ||
e5caa5e0 | 3498 | static void |
61826503 | 3499 | init_opb (asection *s) |
e5caa5e0 | 3500 | { |
bb294208 | 3501 | unsigned int x; |
61826503 | 3502 | |
e5caa5e0 | 3503 | opb_shift = 0; |
bb294208 AM |
3504 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
3505 | && s != NULL | |
3506 | && (s->flags & SEC_ELF_OCTETS) != 0) | |
3507 | return; | |
3508 | ||
3509 | x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3510 | ldfile_output_machine); | |
e5caa5e0 AM |
3511 | if (x > 1) |
3512 | while ((x & 1) == 0) | |
3513 | { | |
3514 | x >>= 1; | |
3515 | ++opb_shift; | |
3516 | } | |
3517 | ASSERT (x == 1); | |
3518 | } | |
3519 | ||
252b5132 RH |
3520 | /* Open all the input files. */ |
3521 | ||
486329aa AM |
3522 | enum open_bfd_mode |
3523 | { | |
3524 | OPEN_BFD_NORMAL = 0, | |
3525 | OPEN_BFD_FORCE = 1, | |
3526 | OPEN_BFD_RESCAN = 2 | |
3527 | }; | |
9e2278f5 AM |
3528 | #ifdef ENABLE_PLUGINS |
3529 | static lang_input_statement_type *plugin_insert = NULL; | |
b02c4f16 | 3530 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3531 | #endif |
486329aa | 3532 | |
252b5132 | 3533 | static void |
486329aa | 3534 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
252b5132 | 3535 | { |
1579bae1 | 3536 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3537 | { |
3538 | switch (s->header.type) | |
3539 | { | |
3540 | case lang_constructors_statement_enum: | |
486329aa | 3541 | open_input_bfds (constructor_list.head, mode); |
252b5132 RH |
3542 | break; |
3543 | case lang_output_section_statement_enum: | |
486329aa | 3544 | open_input_bfds (s->output_section_statement.children.head, mode); |
252b5132 RH |
3545 | break; |
3546 | case lang_wild_statement_enum: | |
08da4cac | 3547 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3548 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3549 | && s->wild_statement.filename | |
97407faf AM |
3550 | && !wildcardp (s->wild_statement.filename) |
3551 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3552 | lookup_name (s->wild_statement.filename); |
486329aa | 3553 | open_input_bfds (s->wild_statement.children.head, mode); |
252b5132 RH |
3554 | break; |
3555 | case lang_group_statement_enum: | |
3556 | { | |
3557 | struct bfd_link_hash_entry *undefs; | |
b02c4f16 AM |
3558 | #ifdef ENABLE_PLUGINS |
3559 | lang_input_statement_type *plugin_insert_save; | |
3560 | #endif | |
252b5132 RH |
3561 | |
3562 | /* We must continually search the entries in the group | |
08da4cac KH |
3563 | until no new symbols are added to the list of undefined |
3564 | symbols. */ | |
252b5132 RH |
3565 | |
3566 | do | |
3567 | { | |
b02c4f16 AM |
3568 | #ifdef ENABLE_PLUGINS |
3569 | plugin_insert_save = plugin_insert; | |
3570 | #endif | |
252b5132 | 3571 | undefs = link_info.hash->undefs_tail; |
486329aa AM |
3572 | open_input_bfds (s->group_statement.children.head, |
3573 | mode | OPEN_BFD_FORCE); | |
252b5132 | 3574 | } |
b02c4f16 AM |
3575 | while (undefs != link_info.hash->undefs_tail |
3576 | #ifdef ENABLE_PLUGINS | |
3577 | /* Objects inserted by a plugin, which are loaded | |
3578 | before we hit this loop, may have added new | |
3579 | undefs. */ | |
3580 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3581 | #endif | |
3582 | ); | |
252b5132 RH |
3583 | } |
3584 | break; | |
3585 | case lang_target_statement_enum: | |
3586 | current_target = s->target_statement.target; | |
3587 | break; | |
3588 | case lang_input_statement_enum: | |
66be1055 | 3589 | if (s->input_statement.flags.real) |
252b5132 | 3590 | { |
bde18da4 | 3591 | lang_statement_union_type **os_tail; |
252b5132 | 3592 | lang_statement_list_type add; |
d215621e | 3593 | bfd *abfd; |
252b5132 RH |
3594 | |
3595 | s->input_statement.target = current_target; | |
3596 | ||
3597 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3598 | is an archive which has already been searched, then |
3599 | force it to be researched unless the whole archive | |
d215621e AM |
3600 | has been loaded already. Do the same for a rescan. |
3601 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3602 | if (mode != OPEN_BFD_NORMAL |
9e2278f5 AM |
3603 | #ifdef ENABLE_PLUGINS |
3604 | && ((mode & OPEN_BFD_RESCAN) == 0 | |
3605 | || plugin_insert == NULL) | |
3606 | #endif | |
66be1055 | 3607 | && s->input_statement.flags.loaded |
d215621e AM |
3608 | && (abfd = s->input_statement.the_bfd) != NULL |
3609 | && ((bfd_get_format (abfd) == bfd_archive | |
3610 | && !s->input_statement.flags.whole_archive) | |
3611 | || (bfd_get_format (abfd) == bfd_object | |
3612 | && ((abfd->flags) & DYNAMIC) != 0 | |
3613 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
e77620a5 | 3614 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3615 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3616 | { |
66be1055 AM |
3617 | s->input_statement.flags.loaded = FALSE; |
3618 | s->input_statement.flags.reload = TRUE; | |
15fc2e13 | 3619 | } |
252b5132 | 3620 | |
5c1e6d53 | 3621 | os_tail = lang_os_list.tail; |
d1778b88 | 3622 | lang_list_init (&add); |
1276aefa | 3623 | |
0aa7f586 | 3624 | if (!load_symbols (&s->input_statement, &add)) |
b34976b6 | 3625 | config.make_executable = FALSE; |
252b5132 RH |
3626 | |
3627 | if (add.head != NULL) | |
3628 | { | |
bde18da4 AM |
3629 | /* If this was a script with output sections then |
3630 | tack any added statements on to the end of the | |
3631 | list. This avoids having to reorder the output | |
3632 | section statement list. Very likely the user | |
3633 | forgot -T, and whatever we do here will not meet | |
3634 | naive user expectations. */ | |
5c1e6d53 | 3635 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
3636 | { |
3637 | einfo (_("%P: warning: %s contains output sections;" | |
3638 | " did you forget -T?\n"), | |
3639 | s->input_statement.filename); | |
3640 | *stat_ptr->tail = add.head; | |
3641 | stat_ptr->tail = add.tail; | |
3642 | } | |
3643 | else | |
3644 | { | |
3645 | *add.tail = s->header.next; | |
3646 | s->header.next = add.head; | |
3647 | } | |
252b5132 RH |
3648 | } |
3649 | } | |
9e2278f5 AM |
3650 | #ifdef ENABLE_PLUGINS |
3651 | /* If we have found the point at which a plugin added new | |
3652 | files, clear plugin_insert to enable archive rescan. */ | |
3653 | if (&s->input_statement == plugin_insert) | |
3654 | plugin_insert = NULL; | |
3655 | #endif | |
252b5132 | 3656 | break; |
e759c116 | 3657 | case lang_assignment_statement_enum: |
165f707a | 3658 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
01554a74 | 3659 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
e759c116 | 3660 | break; |
252b5132 RH |
3661 | default: |
3662 | break; | |
3663 | } | |
3664 | } | |
c4b78195 NC |
3665 | |
3666 | /* Exit if any of the files were missing. */ | |
66be1055 | 3667 | if (input_flags.missing_file) |
c4b78195 | 3668 | einfo ("%F"); |
252b5132 RH |
3669 | } |
3670 | ||
1ff6de03 NA |
3671 | /* Open the CTF sections in the input files with libctf: if any were opened, |
3672 | create a fake input file that we'll write the merged CTF data to later | |
3673 | on. */ | |
3674 | ||
3675 | static void | |
3676 | ldlang_open_ctf (void) | |
3677 | { | |
3678 | int any_ctf = 0; | |
3679 | int err; | |
3680 | ||
3681 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3682 | { | |
3683 | asection *sect; | |
3684 | ||
3685 | /* Incoming files from the compiler have a single ctf_file_t in them | |
3686 | (which is presented to us by the libctf API in a ctf_archive_t | |
3687 | wrapper): files derived from a previous relocatable link have a CTF | |
3688 | archive containing possibly many CTF files. */ | |
3689 | ||
3690 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3691 | { | |
3692 | if (err != ECTF_NOCTFDATA) | |
3693 | einfo (_("%P: warning: CTF section in `%pI' not loaded: " | |
3694 | "its types will be discarded: `%s'\n"), file, | |
3695 | ctf_errmsg (err)); | |
3696 | continue; | |
3697 | } | |
3698 | ||
3699 | /* Prevent the contents of this section from being written, while | |
3700 | requiring the section itself to be duplicated in the output. */ | |
3701 | /* This section must exist if ctf_bfdopen() succeeded. */ | |
3702 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3703 | sect->size = 0; | |
3704 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3705 | ||
3706 | any_ctf = 1; | |
3707 | } | |
3708 | ||
3709 | if (!any_ctf) | |
3710 | { | |
3711 | ctf_output = NULL; | |
3712 | return; | |
3713 | } | |
3714 | ||
3715 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3716 | return; | |
3717 | ||
c48acf6f | 3718 | einfo (_("%P: warning: CTF output not created: `%s'\n"), |
1ff6de03 NA |
3719 | ctf_errmsg (err)); |
3720 | ||
3721 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3722 | ctf_close (errfile->the_ctf); | |
3723 | } | |
3724 | ||
3725 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3726 | function and data object sections need adjustment. */ | |
3727 | ||
3728 | static void | |
3729 | lang_merge_ctf (void) | |
3730 | { | |
3731 | asection *output_sect; | |
3732 | ||
3733 | if (!ctf_output) | |
3734 | return; | |
3735 | ||
3736 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3737 | ||
3738 | /* If the section was discarded, don't waste time merging. */ | |
3739 | if (output_sect == NULL) | |
3740 | { | |
3741 | ctf_file_close (ctf_output); | |
3742 | ctf_output = NULL; | |
3743 | ||
3744 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3745 | { | |
3746 | ctf_close (file->the_ctf); | |
3747 | file->the_ctf = NULL; | |
3748 | } | |
3749 | return; | |
3750 | } | |
3751 | ||
3752 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3753 | { | |
3754 | if (!file->the_ctf) | |
3755 | continue; | |
3756 | ||
3757 | /* Takes ownership of file->u.the_ctfa. */ | |
3758 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) | |
3759 | { | |
3760 | einfo (_("%F%P: cannot link with CTF in %pB: %s\n"), file->the_bfd, | |
3761 | ctf_errmsg (ctf_errno (ctf_output))); | |
3762 | ctf_close (file->the_ctf); | |
3763 | file->the_ctf = NULL; | |
3764 | continue; | |
3765 | } | |
3766 | } | |
3767 | ||
3768 | if (ctf_link (ctf_output, CTF_LINK_SHARE_UNCONFLICTED) < 0) | |
3769 | { | |
3770 | einfo (_("%F%P: CTF linking failed; output will have no CTF section: %s\n"), | |
3771 | ctf_errmsg (ctf_errno (ctf_output))); | |
3772 | if (output_sect) | |
3773 | { | |
3774 | output_sect->size = 0; | |
3775 | output_sect->flags |= SEC_EXCLUDE; | |
3776 | } | |
3777 | } | |
3778 | } | |
3779 | ||
3780 | /* Let the emulation examine the symbol table and strtab to help it optimize the | |
3781 | CTF, if supported. */ | |
3782 | ||
3783 | void | |
3784 | ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms, bfd_size_type symcount, | |
3785 | struct elf_strtab_hash *symstrtab) | |
3786 | { | |
3787 | ldemul_examine_strtab_for_ctf (ctf_output, syms, symcount, symstrtab); | |
3788 | } | |
3789 | ||
3790 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3791 | need to dedup against the strtab and symtab, then possibly called from the | |
3792 | target linker code if the dedup has happened. */ | |
3793 | static void | |
3794 | lang_write_ctf (int late) | |
3795 | { | |
3796 | size_t output_size; | |
3797 | asection *output_sect; | |
3798 | ||
3799 | if (!ctf_output) | |
3800 | return; | |
3801 | ||
3802 | if (late) | |
3803 | { | |
3804 | /* Emit CTF late if this emulation says it can do so. */ | |
3805 | if (ldemul_emit_ctf_early ()) | |
3806 | return; | |
3807 | } | |
3808 | else | |
3809 | { | |
3810 | if (!ldemul_emit_ctf_early ()) | |
3811 | return; | |
3812 | } | |
3813 | ||
3814 | /* Emit CTF. */ | |
3815 | ||
3816 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3817 | if (output_sect) | |
3818 | { | |
3819 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
3820 | CTF_COMPRESSION_THRESHOLD); | |
3821 | output_sect->size = output_size; | |
3822 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
3823 | ||
3824 | if (!output_sect->contents) | |
3825 | { | |
3826 | einfo (_("%F%P: CTF section emission failed; output will have no " | |
3827 | "CTF section: %s\n"), ctf_errmsg (ctf_errno (ctf_output))); | |
3828 | output_sect->size = 0; | |
3829 | output_sect->flags |= SEC_EXCLUDE; | |
3830 | } | |
3831 | } | |
3832 | ||
3833 | /* This also closes every CTF input file used in the link. */ | |
3834 | ctf_file_close (ctf_output); | |
3835 | ctf_output = NULL; | |
3836 | ||
3837 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3838 | file->the_ctf = NULL; | |
3839 | } | |
3840 | ||
3841 | /* Write out the CTF section late, if the emulation needs that. */ | |
3842 | ||
3843 | void | |
3844 | ldlang_write_ctf_late (void) | |
3845 | { | |
3846 | /* Trigger a "late call", if the emulation needs one. */ | |
3847 | ||
3848 | lang_write_ctf (1); | |
3849 | } | |
3850 | ||
08da4cac | 3851 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3852 | This is a two step process as the symbol table doesn't even exist at |
3853 | the time the ld command line is processed. First we put the name | |
3854 | on a list, then, once the output file has been opened, transfer the | |
3855 | name to the symbol table. */ | |
3856 | ||
e3e942e9 | 3857 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3858 | |
e3e942e9 | 3859 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3860 | |
3861 | void | |
24898b70 | 3862 | ldlang_add_undef (const char *const name, bfd_boolean cmdline) |
252b5132 | 3863 | { |
24898b70 | 3864 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 3865 | |
24898b70 | 3866 | undef_from_cmdline = undef_from_cmdline || cmdline; |
988de25b | 3867 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
3868 | new_undef->next = ldlang_undef_chain_list_head; |
3869 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 3870 | |
d3ce72d0 | 3871 | new_undef->name = xstrdup (name); |
fcf0e35b | 3872 | |
f13a99db | 3873 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 3874 | insert_undefined (new_undef->name); |
fcf0e35b AM |
3875 | } |
3876 | ||
3877 | /* Insert NAME as undefined in the symbol table. */ | |
3878 | ||
3879 | static void | |
1579bae1 | 3880 | insert_undefined (const char *name) |
fcf0e35b AM |
3881 | { |
3882 | struct bfd_link_hash_entry *h; | |
3883 | ||
b34976b6 | 3884 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
1579bae1 | 3885 | if (h == NULL) |
df5f2391 | 3886 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
3887 | if (h->type == bfd_link_hash_new) |
3888 | { | |
3889 | h->type = bfd_link_hash_undefined; | |
3890 | h->u.undef.abfd = NULL; | |
94d401b8 | 3891 | h->non_ir_ref_regular = TRUE; |
80070c0d MR |
3892 | if (is_elf_hash_table (link_info.hash)) |
3893 | ((struct elf_link_hash_entry *) h)->mark = 1; | |
fcf0e35b AM |
3894 | bfd_link_add_undef (link_info.hash, h); |
3895 | } | |
252b5132 RH |
3896 | } |
3897 | ||
3898 | /* Run through the list of undefineds created above and place them | |
3899 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3900 | script file. */ |
3901 | ||
252b5132 | 3902 | static void |
1579bae1 | 3903 | lang_place_undefineds (void) |
252b5132 RH |
3904 | { |
3905 | ldlang_undef_chain_list_type *ptr; | |
3906 | ||
1579bae1 AM |
3907 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3908 | insert_undefined (ptr->name); | |
252b5132 RH |
3909 | } |
3910 | ||
0a618243 AB |
3911 | /* Structure used to build the list of symbols that the user has required |
3912 | be defined. */ | |
3913 | ||
3914 | struct require_defined_symbol | |
3915 | { | |
3916 | const char *name; | |
3917 | struct require_defined_symbol *next; | |
3918 | }; | |
3919 | ||
3920 | /* The list of symbols that the user has required be defined. */ | |
3921 | ||
3922 | static struct require_defined_symbol *require_defined_symbol_list; | |
3923 | ||
3924 | /* Add a new symbol NAME to the list of symbols that are required to be | |
3925 | defined. */ | |
3926 | ||
3927 | void | |
0aa7f586 | 3928 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
3929 | { |
3930 | struct require_defined_symbol *ptr; | |
3931 | ||
3932 | ldlang_add_undef (name, TRUE); | |
988de25b | 3933 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 AB |
3934 | ptr->next = require_defined_symbol_list; |
3935 | ptr->name = strdup (name); | |
3936 | require_defined_symbol_list = ptr; | |
3937 | } | |
3938 | ||
3939 | /* Check that all symbols the user required to be defined, are defined, | |
3940 | raise an error if we find a symbol that is not defined. */ | |
3941 | ||
3942 | static void | |
3943 | ldlang_check_require_defined_symbols (void) | |
3944 | { | |
3945 | struct require_defined_symbol *ptr; | |
3946 | ||
3947 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
3948 | { | |
3949 | struct bfd_link_hash_entry *h; | |
3950 | ||
3951 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
0aa7f586 | 3952 | FALSE, FALSE, TRUE); |
0a618243 | 3953 | if (h == NULL |
0aa7f586 AM |
3954 | || (h->type != bfd_link_hash_defined |
3955 | && h->type != bfd_link_hash_defweak)) | |
df5f2391 | 3956 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
3957 | } |
3958 | } | |
3959 | ||
0841712e JJ |
3960 | /* Check for all readonly or some readwrite sections. */ |
3961 | ||
3962 | static void | |
6feb9908 AM |
3963 | check_input_sections |
3964 | (lang_statement_union_type *s, | |
3965 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 3966 | { |
988de25b | 3967 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
3968 | { |
3969 | switch (s->header.type) | |
967928e9 AM |
3970 | { |
3971 | case lang_wild_statement_enum: | |
3972 | walk_wild (&s->wild_statement, check_section_callback, | |
3973 | output_section_statement); | |
0aa7f586 | 3974 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3975 | return; |
3976 | break; | |
3977 | case lang_constructors_statement_enum: | |
3978 | check_input_sections (constructor_list.head, | |
3979 | output_section_statement); | |
0aa7f586 | 3980 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3981 | return; |
3982 | break; | |
3983 | case lang_group_statement_enum: | |
3984 | check_input_sections (s->group_statement.children.head, | |
3985 | output_section_statement); | |
0aa7f586 | 3986 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3987 | return; |
3988 | break; | |
3989 | default: | |
3990 | break; | |
3991 | } | |
0841712e JJ |
3992 | } |
3993 | } | |
3994 | ||
bcaa7b3e L |
3995 | /* Update wildcard statements if needed. */ |
3996 | ||
3997 | static void | |
3998 | update_wild_statements (lang_statement_union_type *s) | |
3999 | { | |
4000 | struct wildcard_list *sec; | |
4001 | ||
4002 | switch (sort_section) | |
4003 | { | |
4004 | default: | |
4005 | FAIL (); | |
4006 | ||
4007 | case none: | |
4008 | break; | |
4009 | ||
4010 | case by_name: | |
4011 | case by_alignment: | |
4012 | for (; s != NULL; s = s->header.next) | |
4013 | { | |
4014 | switch (s->header.type) | |
4015 | { | |
4016 | default: | |
4017 | break; | |
4018 | ||
4019 | case lang_wild_statement_enum: | |
0d0999db L |
4020 | for (sec = s->wild_statement.section_list; sec != NULL; |
4021 | sec = sec->next) | |
2d3181c7 AM |
4022 | /* Don't sort .init/.fini sections. */ |
4023 | if (strcmp (sec->spec.name, ".init") != 0 | |
4024 | && strcmp (sec->spec.name, ".fini") != 0) | |
bcaa7b3e L |
4025 | switch (sec->spec.sorted) |
4026 | { | |
4027 | case none: | |
4028 | sec->spec.sorted = sort_section; | |
4029 | break; | |
4030 | case by_name: | |
4031 | if (sort_section == by_alignment) | |
4032 | sec->spec.sorted = by_name_alignment; | |
4033 | break; | |
4034 | case by_alignment: | |
4035 | if (sort_section == by_name) | |
4036 | sec->spec.sorted = by_alignment_name; | |
4037 | break; | |
4038 | default: | |
4039 | break; | |
4040 | } | |
bcaa7b3e L |
4041 | break; |
4042 | ||
4043 | case lang_constructors_statement_enum: | |
4044 | update_wild_statements (constructor_list.head); | |
4045 | break; | |
4046 | ||
4047 | case lang_output_section_statement_enum: | |
2d3181c7 AM |
4048 | update_wild_statements |
4049 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
4050 | break; |
4051 | ||
4052 | case lang_group_statement_enum: | |
4053 | update_wild_statements (s->group_statement.children.head); | |
4054 | break; | |
4055 | } | |
4056 | } | |
4057 | break; | |
4058 | } | |
4059 | } | |
4060 | ||
396a2467 | 4061 | /* Open input files and attach to output sections. */ |
08da4cac | 4062 | |
252b5132 | 4063 | static void |
1579bae1 AM |
4064 | map_input_to_output_sections |
4065 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4066 | lang_output_section_statement_type *os) |
252b5132 | 4067 | { |
1579bae1 | 4068 | for (; s != NULL; s = s->header.next) |
252b5132 | 4069 | { |
dfa7b0b8 AM |
4070 | lang_output_section_statement_type *tos; |
4071 | flagword flags; | |
4072 | ||
252b5132 RH |
4073 | switch (s->header.type) |
4074 | { | |
252b5132 | 4075 | case lang_wild_statement_enum: |
afd7a018 | 4076 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4077 | break; |
252b5132 RH |
4078 | case lang_constructors_statement_enum: |
4079 | map_input_to_output_sections (constructor_list.head, | |
4080 | target, | |
afd7a018 | 4081 | os); |
252b5132 RH |
4082 | break; |
4083 | case lang_output_section_statement_enum: | |
dfa7b0b8 AM |
4084 | tos = &s->output_section_statement; |
4085 | if (tos->constraint != 0) | |
0841712e | 4086 | { |
dfa7b0b8 AM |
4087 | if (tos->constraint != ONLY_IF_RW |
4088 | && tos->constraint != ONLY_IF_RO) | |
0841712e | 4089 | break; |
dfa7b0b8 AM |
4090 | tos->all_input_readonly = TRUE; |
4091 | check_input_sections (tos->children.head, tos); | |
4092 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
0841712e | 4093 | { |
dfa7b0b8 | 4094 | tos->constraint = -1; |
0841712e JJ |
4095 | break; |
4096 | } | |
4097 | } | |
dfa7b0b8 | 4098 | map_input_to_output_sections (tos->children.head, |
252b5132 | 4099 | target, |
dfa7b0b8 | 4100 | tos); |
a431bc2e AM |
4101 | break; |
4102 | case lang_output_statement_enum: | |
252b5132 RH |
4103 | break; |
4104 | case lang_target_statement_enum: | |
4105 | target = s->target_statement.target; | |
4106 | break; | |
4107 | case lang_group_statement_enum: | |
4108 | map_input_to_output_sections (s->group_statement.children.head, | |
4109 | target, | |
afd7a018 | 4110 | os); |
252b5132 | 4111 | break; |
384d938f NS |
4112 | case lang_data_statement_enum: |
4113 | /* Make sure that any sections mentioned in the expression | |
4114 | are initialized. */ | |
4115 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4116 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4117 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4118 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4119 | switch (os->sectype) | |
4120 | { | |
4121 | case normal_section: | |
4122 | case overlay_section: | |
7b243801 | 4123 | case first_overlay_section: |
dfa7b0b8 AM |
4124 | break; |
4125 | case noalloc_section: | |
4126 | flags = SEC_HAS_CONTENTS; | |
4127 | break; | |
4128 | case noload_section: | |
f4eaaf7f AM |
4129 | if (bfd_get_flavour (link_info.output_bfd) |
4130 | == bfd_target_elf_flavour) | |
4131 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4132 | else | |
4133 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
dfa7b0b8 AM |
4134 | break; |
4135 | } | |
a431bc2e | 4136 | if (os->bfd_section == NULL) |
dfa7b0b8 | 4137 | init_os (os, flags); |
a431bc2e AM |
4138 | else |
4139 | os->bfd_section->flags |= flags; | |
afd7a018 | 4140 | break; |
252b5132 | 4141 | case lang_input_section_enum: |
e0f6802f AM |
4142 | break; |
4143 | case lang_fill_statement_enum: | |
252b5132 | 4144 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4145 | case lang_reloc_statement_enum: |
4146 | case lang_padding_statement_enum: | |
4147 | case lang_input_statement_enum: | |
afd7a018 | 4148 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4149 | init_os (os, 0); |
252b5132 RH |
4150 | break; |
4151 | case lang_assignment_statement_enum: | |
afd7a018 | 4152 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4153 | init_os (os, 0); |
252b5132 RH |
4154 | |
4155 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4156 | are initialized. */ |
252b5132 RH |
4157 | exp_init_os (s->assignment_statement.exp); |
4158 | break; | |
252b5132 | 4159 | case lang_address_statement_enum: |
a431bc2e AM |
4160 | /* Mark the specified section with the supplied address. |
4161 | If this section was actually a segment marker, then the | |
4162 | directive is ignored if the linker script explicitly | |
4163 | processed the segment marker. Originally, the linker | |
4164 | treated segment directives (like -Ttext on the | |
4165 | command-line) as section directives. We honor the | |
370dfff4 | 4166 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4167 | linker scripts that do not specifically check for |
4168 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4169 | if (!s->address_statement.segment |
ba916c8a MM |
4170 | || !s->address_statement.segment->used) |
4171 | { | |
dfa7b0b8 AM |
4172 | const char *name = s->address_statement.section_name; |
4173 | ||
4174 | /* Create the output section statement here so that | |
4175 | orphans with a set address will be placed after other | |
4176 | script sections. If we let the orphan placement code | |
4177 | place them in amongst other sections then the address | |
4178 | will affect following script sections, which is | |
4179 | likely to surprise naive users. */ | |
4180 | tos = lang_output_section_statement_lookup (name, 0, TRUE); | |
4181 | tos->addr_tree = s->address_statement.address; | |
4182 | if (tos->bfd_section == NULL) | |
4183 | init_os (tos, 0); | |
ba916c8a | 4184 | } |
252b5132 | 4185 | break; |
53d25da6 AM |
4186 | case lang_insert_statement_enum: |
4187 | break; | |
4188 | } | |
4189 | } | |
4190 | } | |
4191 | ||
4192 | /* An insert statement snips out all the linker statements from the | |
4193 | start of the list and places them after the output section | |
4194 | statement specified by the insert. This operation is complicated | |
4195 | by the fact that we keep a doubly linked list of output section | |
776ab89f AM |
4196 | statements as well as the singly linked list of all statements. |
4197 | FIXME someday: Twiddling with the list not only moves statements | |
4198 | from the user's script but also input and group statements that are | |
4199 | built from command line object files and --start-group. We only | |
4200 | get away with this because the list pointers used by file_chain | |
4201 | and input_file_chain are not reordered, and processing via | |
4202 | statement_list after this point mostly ignores input statements. | |
4203 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4204 | can end up looking odd. */ | |
53d25da6 AM |
4205 | |
4206 | static void | |
776ab89f | 4207 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
4208 | { |
4209 | lang_statement_union_type **s; | |
4210 | lang_output_section_statement_type *first_os = NULL; | |
4211 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 4212 | lang_output_section_statement_type *os; |
53d25da6 | 4213 | |
776ab89f AM |
4214 | s = start; |
4215 | while (*s != NULL) | |
53d25da6 AM |
4216 | { |
4217 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4218 | { | |
4219 | /* Keep pointers to the first and last output section | |
4220 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
4221 | os = &(*s)->output_section_statement; |
4222 | ||
4223 | ASSERT (last_os == NULL || last_os->next == os); | |
4224 | last_os = os; | |
66c103b7 AM |
4225 | |
4226 | /* Set constraint negative so that lang_output_section_find | |
4227 | won't match this output section statement. At this | |
4228 | stage in linking constraint has values in the range | |
4229 | [-1, ONLY_IN_RW]. */ | |
4230 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
4231 | if (first_os == NULL) |
4232 | first_os = last_os; | |
4233 | } | |
776ab89f AM |
4234 | else if ((*s)->header.type == lang_group_statement_enum) |
4235 | { | |
4236 | /* A user might put -T between --start-group and | |
4237 | --end-group. One way this odd construct might arise is | |
4238 | from a wrapper around ld to change library search | |
4239 | behaviour. For example: | |
4240 | #! /bin/sh | |
4241 | exec real_ld --start-group "$@" --end-group | |
4242 | This isn't completely unreasonable so go looking inside a | |
4243 | group statement for insert statements. */ | |
4244 | process_insert_statements (&(*s)->group_statement.children.head); | |
4245 | } | |
53d25da6 AM |
4246 | else if ((*s)->header.type == lang_insert_statement_enum) |
4247 | { | |
4248 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4249 | lang_output_section_statement_type *where; | |
53d25da6 AM |
4250 | lang_statement_union_type **ptr; |
4251 | lang_statement_union_type *first; | |
4252 | ||
abf874aa CL |
4253 | if (link_info.non_contiguous_regions) |
4254 | { | |
4255 | einfo (_("warning: INSERT statement in linker script is " | |
4256 | "incompatible with --enable-non-contiguous-regions.\n")); | |
4257 | } | |
4258 | ||
53d25da6 AM |
4259 | where = lang_output_section_find (i->where); |
4260 | if (where != NULL && i->is_before) | |
4261 | { | |
967928e9 | 4262 | do |
53d25da6 | 4263 | where = where->prev; |
66c103b7 | 4264 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4265 | } |
4266 | if (where == NULL) | |
4267 | { | |
66c103b7 AM |
4268 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
4269 | return; | |
53d25da6 AM |
4270 | } |
4271 | ||
4272 | /* Deal with reordering the output section statement list. */ | |
4273 | if (last_os != NULL) | |
4274 | { | |
4275 | asection *first_sec, *last_sec; | |
29183214 | 4276 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4277 | |
4278 | /* Snip out the output sections we are moving. */ | |
4279 | first_os->prev->next = last_os->next; | |
4280 | if (last_os->next == NULL) | |
29183214 L |
4281 | { |
4282 | next = &first_os->prev->next; | |
5c1e6d53 | 4283 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4284 | } |
53d25da6 AM |
4285 | else |
4286 | last_os->next->prev = first_os->prev; | |
4287 | /* Add them in at the new position. */ | |
4288 | last_os->next = where->next; | |
4289 | if (where->next == NULL) | |
29183214 L |
4290 | { |
4291 | next = &last_os->next; | |
5c1e6d53 | 4292 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4293 | } |
53d25da6 AM |
4294 | else |
4295 | where->next->prev = last_os; | |
4296 | first_os->prev = where; | |
4297 | where->next = first_os; | |
4298 | ||
4299 | /* Move the bfd sections in the same way. */ | |
4300 | first_sec = NULL; | |
4301 | last_sec = NULL; | |
4302 | for (os = first_os; os != NULL; os = os->next) | |
4303 | { | |
66c103b7 | 4304 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
4305 | if (os->bfd_section != NULL |
4306 | && os->bfd_section->owner != NULL) | |
4307 | { | |
4308 | last_sec = os->bfd_section; | |
4309 | if (first_sec == NULL) | |
4310 | first_sec = last_sec; | |
4311 | } | |
4312 | if (os == last_os) | |
4313 | break; | |
4314 | } | |
4315 | if (last_sec != NULL) | |
4316 | { | |
4317 | asection *sec = where->bfd_section; | |
4318 | if (sec == NULL) | |
4319 | sec = output_prev_sec_find (where); | |
4320 | ||
4321 | /* The place we want to insert must come after the | |
4322 | sections we are moving. So if we find no | |
4323 | section or if the section is the same as our | |
4324 | last section, then no move is needed. */ | |
4325 | if (sec != NULL && sec != last_sec) | |
4326 | { | |
4327 | /* Trim them off. */ | |
4328 | if (first_sec->prev != NULL) | |
4329 | first_sec->prev->next = last_sec->next; | |
4330 | else | |
f13a99db | 4331 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4332 | if (last_sec->next != NULL) |
4333 | last_sec->next->prev = first_sec->prev; | |
4334 | else | |
f13a99db | 4335 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 AM |
4336 | /* Add back. */ |
4337 | last_sec->next = sec->next; | |
4338 | if (sec->next != NULL) | |
4339 | sec->next->prev = last_sec; | |
4340 | else | |
f13a99db | 4341 | link_info.output_bfd->section_last = last_sec; |
53d25da6 AM |
4342 | first_sec->prev = sec; |
4343 | sec->next = first_sec; | |
4344 | } | |
4345 | } | |
4346 | ||
4347 | first_os = NULL; | |
4348 | last_os = NULL; | |
4349 | } | |
4350 | ||
4351 | ptr = insert_os_after (where); | |
776ab89f AM |
4352 | /* Snip everything from the start of the list, up to and |
4353 | including the insert statement we are currently processing. */ | |
4354 | first = *start; | |
4355 | *start = (*s)->header.next; | |
4356 | /* Add them back where they belong, minus the insert. */ | |
53d25da6 AM |
4357 | *s = *ptr; |
4358 | if (*s == NULL) | |
4359 | statement_list.tail = s; | |
4360 | *ptr = first; | |
776ab89f AM |
4361 | s = start; |
4362 | continue; | |
252b5132 | 4363 | } |
776ab89f | 4364 | s = &(*s)->header.next; |
252b5132 | 4365 | } |
66c103b7 AM |
4366 | |
4367 | /* Undo constraint twiddling. */ | |
4368 | for (os = first_os; os != NULL; os = os->next) | |
4369 | { | |
4370 | os->constraint = -2 - os->constraint; | |
4371 | if (os == last_os) | |
4372 | break; | |
4373 | } | |
252b5132 RH |
4374 | } |
4375 | ||
4bd5a393 AM |
4376 | /* An output section might have been removed after its statement was |
4377 | added. For example, ldemul_before_allocation can remove dynamic | |
4378 | sections if they turn out to be not needed. Clean them up here. */ | |
4379 | ||
8423293d | 4380 | void |
1579bae1 | 4381 | strip_excluded_output_sections (void) |
4bd5a393 | 4382 | { |
afd7a018 | 4383 | lang_output_section_statement_type *os; |
4bd5a393 | 4384 | |
046183de | 4385 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4386 | if (expld.phase != lang_mark_phase_enum) |
4387 | { | |
4388 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4389 | expld.dataseg.phase = exp_seg_none; |
e9ee469a AM |
4390 | one_lang_size_sections_pass (NULL, FALSE); |
4391 | lang_reset_memory_regions (); | |
4392 | } | |
4393 | ||
8ce18f9c | 4394 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
4395 | os != NULL; |
4396 | os = os->next) | |
4bd5a393 | 4397 | { |
75ff4589 L |
4398 | asection *output_section; |
4399 | bfd_boolean exclude; | |
4bd5a393 | 4400 | |
66c103b7 | 4401 | if (os->constraint < 0) |
0841712e | 4402 | continue; |
8423293d | 4403 | |
75ff4589 L |
4404 | output_section = os->bfd_section; |
4405 | if (output_section == NULL) | |
8423293d AM |
4406 | continue; |
4407 | ||
32124d5b AM |
4408 | exclude = (output_section->rawsize == 0 |
4409 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4410 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
4411 | output_section)); |
4412 | ||
4413 | /* Some sections have not yet been sized, notably .gnu.version, | |
4414 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4415 | input sections, so don't drop output sections that have such | |
4416 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4417 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
4418 | { |
4419 | asection *s; | |
8423293d | 4420 | |
32124d5b | 4421 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4422 | if ((s->flags & SEC_EXCLUDE) == 0 |
4423 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4424 | || link_info.emitrelocations)) | |
e9ee469a AM |
4425 | { |
4426 | exclude = FALSE; | |
4427 | break; | |
4428 | } | |
75ff4589 | 4429 | } |
8423293d | 4430 | |
32124d5b | 4431 | if (exclude) |
4bd5a393 | 4432 | { |
e9ee469a AM |
4433 | /* We don't set bfd_section to NULL since bfd_section of the |
4434 | removed output section statement may still be used. */ | |
6d8bf25d | 4435 | if (!os->update_dot) |
74541ad4 | 4436 | os->ignored = TRUE; |
e9ee469a | 4437 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4438 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4439 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4440 | } |
4441 | } | |
18cd5bce AM |
4442 | } |
4443 | ||
4444 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4445 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
4446 | |
4447 | void | |
4448 | lang_clear_os_map (void) | |
4449 | { | |
4450 | lang_output_section_statement_type *os; | |
4451 | ||
4452 | if (map_head_is_link_order) | |
4453 | return; | |
4454 | ||
8ce18f9c | 4455 | for (os = (void *) lang_os_list.head; |
18cd5bce AM |
4456 | os != NULL; |
4457 | os = os->next) | |
4458 | { | |
4459 | asection *output_section; | |
4460 | ||
4461 | if (os->constraint < 0) | |
4462 | continue; | |
4463 | ||
4464 | output_section = os->bfd_section; | |
4465 | if (output_section == NULL) | |
4466 | continue; | |
4467 | ||
4468 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4469 | output_section->map_head.link_order = NULL; | |
4470 | output_section->map_tail.link_order = NULL; | |
4471 | } | |
8423293d AM |
4472 | |
4473 | /* Stop future calls to lang_add_section from messing with map_head | |
4474 | and map_tail link_order fields. */ | |
18cd5bce | 4475 | map_head_is_link_order = TRUE; |
4bd5a393 AM |
4476 | } |
4477 | ||
252b5132 | 4478 | static void |
1579bae1 AM |
4479 | print_output_section_statement |
4480 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
4481 | { |
4482 | asection *section = output_section_statement->bfd_section; | |
4483 | int len; | |
4484 | ||
4485 | if (output_section_statement != abs_output_section) | |
4486 | { | |
4487 | minfo ("\n%s", output_section_statement->name); | |
4488 | ||
4489 | if (section != NULL) | |
4490 | { | |
4491 | print_dot = section->vma; | |
4492 | ||
4493 | len = strlen (output_section_statement->name); | |
4494 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4495 | { | |
4496 | print_nl (); | |
4497 | len = 0; | |
4498 | } | |
4499 | while (len < SECTION_NAME_MAP_LENGTH) | |
4500 | { | |
4501 | print_space (); | |
4502 | ++len; | |
4503 | } | |
4504 | ||
953dd97e | 4505 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4506 | |
67f744f3 AM |
4507 | if (section->vma != section->lma) |
4508 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4509 | |
4510 | if (output_section_statement->update_dot_tree != NULL) | |
4511 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4512 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
4513 | } |
4514 | ||
4515 | print_nl (); | |
4516 | } | |
4517 | ||
4518 | print_statement_list (output_section_statement->children.head, | |
4519 | output_section_statement); | |
4520 | } | |
4521 | ||
4522 | static void | |
1579bae1 AM |
4523 | print_assignment (lang_assignment_statement_type *assignment, |
4524 | lang_output_section_statement_type *output_section) | |
252b5132 | 4525 | { |
3b83e13a NC |
4526 | unsigned int i; |
4527 | bfd_boolean is_dot; | |
afd7a018 | 4528 | etree_type *tree; |
8658f989 | 4529 | asection *osec; |
252b5132 RH |
4530 | |
4531 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
4532 | print_space (); | |
4533 | ||
afd7a018 AM |
4534 | if (assignment->exp->type.node_class == etree_assert) |
4535 | { | |
3b83e13a | 4536 | is_dot = FALSE; |
afd7a018 AM |
4537 | tree = assignment->exp->assert_s.child; |
4538 | } | |
4539 | else | |
4540 | { | |
4541 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4542 | |
4543 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4544 | tree = assignment->exp; |
afd7a018 AM |
4545 | } |
4546 | ||
8658f989 AM |
4547 | osec = output_section->bfd_section; |
4548 | if (osec == NULL) | |
4549 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4550 | |
4551 | if (assignment->exp->type.node_class != etree_provide) | |
4552 | exp_fold_tree (tree, osec, &print_dot); | |
4553 | else | |
4554 | expld.result.valid_p = FALSE; | |
4555 | ||
e9ee469a | 4556 | if (expld.result.valid_p) |
7b17f854 | 4557 | { |
7b17f854 RS |
4558 | bfd_vma value; |
4559 | ||
4194268f AM |
4560 | if (assignment->exp->type.node_class == etree_assert |
4561 | || is_dot | |
4562 | || expld.assign_name != NULL) | |
3b83e13a | 4563 | { |
e9ee469a | 4564 | value = expld.result.value; |
10dbd1f3 | 4565 | |
7542af2a | 4566 | if (expld.result.section != NULL) |
e9ee469a | 4567 | value += expld.result.section->vma; |
7b17f854 | 4568 | |
3b83e13a NC |
4569 | minfo ("0x%V", value); |
4570 | if (is_dot) | |
4571 | print_dot = value; | |
4572 | } | |
4573 | else | |
4574 | { | |
4575 | struct bfd_link_hash_entry *h; | |
4576 | ||
4577 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
4578 | FALSE, FALSE, TRUE); | |
6a846243 AM |
4579 | if (h != NULL |
4580 | && (h->type == bfd_link_hash_defined | |
4581 | || h->type == bfd_link_hash_defweak)) | |
3b83e13a | 4582 | { |
10dbd1f3 | 4583 | value = h->u.def.value; |
f37a7048 NS |
4584 | value += h->u.def.section->output_section->vma; |
4585 | value += h->u.def.section->output_offset; | |
3b83e13a NC |
4586 | |
4587 | minfo ("[0x%V]", value); | |
4588 | } | |
4589 | else | |
4590 | minfo ("[unresolved]"); | |
4591 | } | |
7b17f854 | 4592 | } |
252b5132 RH |
4593 | else |
4594 | { | |
c05b575a | 4595 | if (assignment->exp->type.node_class == etree_provide) |
0aa7f586 | 4596 | minfo ("[!provide]"); |
c05b575a | 4597 | else |
0aa7f586 | 4598 | minfo ("*undef* "); |
252b5132 RH |
4599 | #ifdef BFD64 |
4600 | minfo (" "); | |
4601 | #endif | |
4602 | } | |
4194268f | 4603 | expld.assign_name = NULL; |
252b5132 RH |
4604 | |
4605 | minfo (" "); | |
252b5132 | 4606 | exp_print_tree (assignment->exp); |
252b5132 RH |
4607 | print_nl (); |
4608 | } | |
4609 | ||
4610 | static void | |
1579bae1 | 4611 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4612 | { |
0d41d9a2 | 4613 | if (statm->filename != NULL) |
906e58ca | 4614 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4615 | } |
4616 | ||
4617 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4618 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4619 | |
3edf7b9f | 4620 | bfd_boolean |
1579bae1 | 4621 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4622 | { |
1e9cc1c2 | 4623 | asection *sec = (asection *) ptr; |
252b5132 RH |
4624 | |
4625 | if ((hash_entry->type == bfd_link_hash_defined | |
4626 | || hash_entry->type == bfd_link_hash_defweak) | |
4627 | && sec == hash_entry->u.def.section) | |
4628 | { | |
4629 | int i; | |
4630 | ||
4631 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
4632 | print_space (); | |
4633 | minfo ("0x%V ", | |
4634 | (hash_entry->u.def.value | |
4635 | + hash_entry->u.def.section->output_offset | |
4636 | + hash_entry->u.def.section->output_section->vma)); | |
4637 | ||
c1c8c1ef | 4638 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4639 | } |
4640 | ||
b34976b6 | 4641 | return TRUE; |
252b5132 RH |
4642 | } |
4643 | ||
02688209 TG |
4644 | static int |
4645 | hash_entry_addr_cmp (const void *a, const void *b) | |
4646 | { | |
4647 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4648 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4649 | ||
4650 | if (l->u.def.value < r->u.def.value) | |
4651 | return -1; | |
4652 | else if (l->u.def.value > r->u.def.value) | |
4653 | return 1; | |
4654 | else | |
4655 | return 0; | |
4656 | } | |
4657 | ||
35835446 | 4658 | static void |
e0f6802f | 4659 | print_all_symbols (asection *sec) |
35835446 | 4660 | { |
a48931cc | 4661 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4662 | struct map_symbol_def *def; |
02688209 TG |
4663 | struct bfd_link_hash_entry **entries; |
4664 | unsigned int i; | |
35835446 | 4665 | |
afd7a018 AM |
4666 | if (!ud) |
4667 | return; | |
4668 | ||
35835446 | 4669 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4670 | |
02688209 | 4671 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4672 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4673 | obstack_alloc (&map_obstack, |
4674 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
4675 | |
4676 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4677 | entries[i] = def->entry; | |
4678 | ||
4679 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4724d37e | 4680 | hash_entry_addr_cmp); |
02688209 TG |
4681 | |
4682 | /* Print the symbols. */ | |
4683 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
3edf7b9f | 4684 | ldemul_print_symbol (entries[i], sec); |
02688209 TG |
4685 | |
4686 | obstack_free (&map_obstack, entries); | |
35835446 JR |
4687 | } |
4688 | ||
252b5132 RH |
4689 | /* Print information about an input section to the map file. */ |
4690 | ||
4691 | static void | |
d64703c6 | 4692 | print_input_section (asection *i, bfd_boolean is_discarded) |
252b5132 | 4693 | { |
eea6121a | 4694 | bfd_size_type size = i->size; |
abe6ac95 AM |
4695 | int len; |
4696 | bfd_vma addr; | |
e5caa5e0 | 4697 | |
61826503 | 4698 | init_opb (i); |
4d4920ec | 4699 | |
abe6ac95 AM |
4700 | print_space (); |
4701 | minfo ("%s", i->name); | |
252b5132 | 4702 | |
abe6ac95 AM |
4703 | len = 1 + strlen (i->name); |
4704 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4705 | { | |
4706 | print_nl (); | |
4707 | len = 0; | |
4708 | } | |
4709 | while (len < SECTION_NAME_MAP_LENGTH) | |
4710 | { | |
57ceae94 | 4711 | print_space (); |
abe6ac95 AM |
4712 | ++len; |
4713 | } | |
252b5132 | 4714 | |
f13a99db AM |
4715 | if (i->output_section != NULL |
4716 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
4717 | addr = i->output_section->vma + i->output_offset; |
4718 | else | |
4719 | { | |
4720 | addr = print_dot; | |
d64703c6 TG |
4721 | if (!is_discarded) |
4722 | size = 0; | |
abe6ac95 | 4723 | } |
252b5132 | 4724 | |
b0a7971a | 4725 | minfo ("0x%V %W %pB\n", addr, TO_ADDR (size), i->owner); |
252b5132 | 4726 | |
abe6ac95 AM |
4727 | if (size != i->rawsize && i->rawsize != 0) |
4728 | { | |
4729 | len = SECTION_NAME_MAP_LENGTH + 3; | |
252b5132 | 4730 | #ifdef BFD64 |
abe6ac95 | 4731 | len += 16; |
252b5132 | 4732 | #else |
abe6ac95 | 4733 | len += 8; |
252b5132 | 4734 | #endif |
abe6ac95 AM |
4735 | while (len > 0) |
4736 | { | |
4737 | print_space (); | |
4738 | --len; | |
57ceae94 AM |
4739 | } |
4740 | ||
b0a7971a | 4741 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 4742 | } |
252b5132 | 4743 | |
f13a99db AM |
4744 | if (i->output_section != NULL |
4745 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 4746 | { |
c0f00686 | 4747 | if (link_info.reduce_memory_overheads) |
3edf7b9f | 4748 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); |
abe6ac95 AM |
4749 | else |
4750 | print_all_symbols (i); | |
4751 | ||
5590fba9 NC |
4752 | /* Update print_dot, but make sure that we do not move it |
4753 | backwards - this could happen if we have overlays and a | |
4754 | later overlay is shorter than an earier one. */ | |
4755 | if (addr + TO_ADDR (size) > print_dot) | |
4756 | print_dot = addr + TO_ADDR (size); | |
252b5132 RH |
4757 | } |
4758 | } | |
4759 | ||
4760 | static void | |
1579bae1 | 4761 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 4762 | { |
2c382fb6 AM |
4763 | size_t size; |
4764 | unsigned char *p; | |
4765 | fputs (" FILL mask 0x", config.map_file); | |
4766 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4767 | fprintf (config.map_file, "%02x", *p); | |
4768 | fputs ("\n", config.map_file); | |
252b5132 RH |
4769 | } |
4770 | ||
4771 | static void | |
1579bae1 | 4772 | print_data_statement (lang_data_statement_type *data) |
252b5132 RH |
4773 | { |
4774 | int i; | |
4775 | bfd_vma addr; | |
4776 | bfd_size_type size; | |
4777 | const char *name; | |
4778 | ||
61826503 | 4779 | init_opb (data->output_section); |
252b5132 RH |
4780 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4781 | print_space (); | |
4782 | ||
7fabd029 | 4783 | addr = data->output_offset; |
252b5132 RH |
4784 | if (data->output_section != NULL) |
4785 | addr += data->output_section->vma; | |
4786 | ||
4787 | switch (data->type) | |
4788 | { | |
4789 | default: | |
4790 | abort (); | |
4791 | case BYTE: | |
4792 | size = BYTE_SIZE; | |
4793 | name = "BYTE"; | |
4794 | break; | |
4795 | case SHORT: | |
4796 | size = SHORT_SIZE; | |
4797 | name = "SHORT"; | |
4798 | break; | |
4799 | case LONG: | |
4800 | size = LONG_SIZE; | |
4801 | name = "LONG"; | |
4802 | break; | |
4803 | case QUAD: | |
4804 | size = QUAD_SIZE; | |
4805 | name = "QUAD"; | |
4806 | break; | |
4807 | case SQUAD: | |
4808 | size = QUAD_SIZE; | |
4809 | name = "SQUAD"; | |
4810 | break; | |
4811 | } | |
4812 | ||
953dd97e DG |
4813 | if (size < TO_SIZE ((unsigned) 1)) |
4814 | size = TO_SIZE ((unsigned) 1); | |
4815 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
252b5132 RH |
4816 | |
4817 | if (data->exp->type.node_class != etree_value) | |
4818 | { | |
4819 | print_space (); | |
4820 | exp_print_tree (data->exp); | |
4821 | } | |
4822 | ||
4823 | print_nl (); | |
4824 | ||
e5caa5e0 | 4825 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
4826 | } |
4827 | ||
4828 | /* Print an address statement. These are generated by options like | |
4829 | -Ttext. */ | |
4830 | ||
4831 | static void | |
1579bae1 | 4832 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
4833 | { |
4834 | minfo (_("Address of section %s set to "), address->section_name); | |
4835 | exp_print_tree (address->address); | |
4836 | print_nl (); | |
4837 | } | |
4838 | ||
4839 | /* Print a reloc statement. */ | |
4840 | ||
4841 | static void | |
1579bae1 | 4842 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 RH |
4843 | { |
4844 | int i; | |
4845 | bfd_vma addr; | |
4846 | bfd_size_type size; | |
4847 | ||
61826503 | 4848 | init_opb (reloc->output_section); |
252b5132 RH |
4849 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
4850 | print_space (); | |
4851 | ||
7fabd029 | 4852 | addr = reloc->output_offset; |
252b5132 RH |
4853 | if (reloc->output_section != NULL) |
4854 | addr += reloc->output_section->vma; | |
4855 | ||
4856 | size = bfd_get_reloc_size (reloc->howto); | |
4857 | ||
953dd97e | 4858 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
4859 | |
4860 | if (reloc->name != NULL) | |
4861 | minfo ("%s+", reloc->name); | |
4862 | else | |
4863 | minfo ("%s+", reloc->section->name); | |
4864 | ||
4865 | exp_print_tree (reloc->addend_exp); | |
4866 | ||
4867 | print_nl (); | |
4868 | ||
e5caa5e0 | 4869 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 4870 | } |
252b5132 RH |
4871 | |
4872 | static void | |
1579bae1 | 4873 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
4874 | { |
4875 | int len; | |
4876 | bfd_vma addr; | |
4877 | ||
61826503 | 4878 | init_opb (s->output_section); |
252b5132 RH |
4879 | minfo (" *fill*"); |
4880 | ||
4881 | len = sizeof " *fill*" - 1; | |
4882 | while (len < SECTION_NAME_MAP_LENGTH) | |
4883 | { | |
4884 | print_space (); | |
4885 | ++len; | |
4886 | } | |
4887 | ||
4888 | addr = s->output_offset; | |
4889 | if (s->output_section != NULL) | |
4890 | addr += s->output_section->vma; | |
953dd97e | 4891 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 4892 | |
2c382fb6 AM |
4893 | if (s->fill->size != 0) |
4894 | { | |
4895 | size_t size; | |
4896 | unsigned char *p; | |
4897 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
4898 | fprintf (config.map_file, "%02x", *p); | |
4899 | } | |
252b5132 RH |
4900 | |
4901 | print_nl (); | |
4902 | ||
e5caa5e0 | 4903 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
4904 | } |
4905 | ||
4906 | static void | |
1579bae1 AM |
4907 | print_wild_statement (lang_wild_statement_type *w, |
4908 | lang_output_section_statement_type *os) | |
252b5132 | 4909 | { |
b6bf44ba AM |
4910 | struct wildcard_list *sec; |
4911 | ||
252b5132 RH |
4912 | print_space (); |
4913 | ||
8f1732fc AB |
4914 | if (w->exclude_name_list) |
4915 | { | |
4916 | name_list *tmp; | |
4917 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
4918 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
6c19b93b | 4919 | minfo (" %s", tmp->name); |
8f1732fc AB |
4920 | minfo (") "); |
4921 | } | |
4922 | ||
252b5132 | 4923 | if (w->filenames_sorted) |
2b94abd4 | 4924 | minfo ("SORT_BY_NAME("); |
08da4cac | 4925 | if (w->filename != NULL) |
252b5132 RH |
4926 | minfo ("%s", w->filename); |
4927 | else | |
4928 | minfo ("*"); | |
4929 | if (w->filenames_sorted) | |
4930 | minfo (")"); | |
4931 | ||
4932 | minfo ("("); | |
b6bf44ba AM |
4933 | for (sec = w->section_list; sec; sec = sec->next) |
4934 | { | |
2b94abd4 AB |
4935 | int closing_paren = 0; |
4936 | ||
4937 | switch (sec->spec.sorted) | |
6c19b93b AM |
4938 | { |
4939 | case none: | |
4940 | break; | |
4941 | ||
4942 | case by_name: | |
4943 | minfo ("SORT_BY_NAME("); | |
4944 | closing_paren = 1; | |
4945 | break; | |
4946 | ||
4947 | case by_alignment: | |
4948 | minfo ("SORT_BY_ALIGNMENT("); | |
4949 | closing_paren = 1; | |
4950 | break; | |
4951 | ||
4952 | case by_name_alignment: | |
4953 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
4954 | closing_paren = 2; | |
4955 | break; | |
4956 | ||
4957 | case by_alignment_name: | |
4958 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
4959 | closing_paren = 2; | |
4960 | break; | |
4961 | ||
4962 | case by_none: | |
4963 | minfo ("SORT_NONE("); | |
4964 | closing_paren = 1; | |
4965 | break; | |
4966 | ||
4967 | case by_init_priority: | |
4968 | minfo ("SORT_BY_INIT_PRIORITY("); | |
4969 | closing_paren = 1; | |
4970 | break; | |
4971 | } | |
2b94abd4 | 4972 | |
b6bf44ba AM |
4973 | if (sec->spec.exclude_name_list != NULL) |
4974 | { | |
4975 | name_list *tmp; | |
34786259 | 4976 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 4977 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
4978 | minfo (" %s", tmp->name); |
4979 | minfo (") "); | |
b6bf44ba AM |
4980 | } |
4981 | if (sec->spec.name != NULL) | |
4982 | minfo ("%s", sec->spec.name); | |
4983 | else | |
4984 | minfo ("*"); | |
2b94abd4 | 4985 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 4986 | minfo (")"); |
34786259 AM |
4987 | if (sec->next) |
4988 | minfo (" "); | |
b6bf44ba | 4989 | } |
252b5132 RH |
4990 | minfo (")"); |
4991 | ||
4992 | print_nl (); | |
4993 | ||
4994 | print_statement_list (w->children.head, os); | |
4995 | } | |
4996 | ||
4997 | /* Print a group statement. */ | |
4998 | ||
4999 | static void | |
1579bae1 AM |
5000 | print_group (lang_group_statement_type *s, |
5001 | lang_output_section_statement_type *os) | |
252b5132 RH |
5002 | { |
5003 | fprintf (config.map_file, "START GROUP\n"); | |
5004 | print_statement_list (s->children.head, os); | |
5005 | fprintf (config.map_file, "END GROUP\n"); | |
5006 | } | |
5007 | ||
5008 | /* Print the list of statements in S. | |
5009 | This can be called for any statement type. */ | |
5010 | ||
5011 | static void | |
1579bae1 AM |
5012 | print_statement_list (lang_statement_union_type *s, |
5013 | lang_output_section_statement_type *os) | |
252b5132 RH |
5014 | { |
5015 | while (s != NULL) | |
5016 | { | |
5017 | print_statement (s, os); | |
bba1a0c0 | 5018 | s = s->header.next; |
252b5132 RH |
5019 | } |
5020 | } | |
5021 | ||
5022 | /* Print the first statement in statement list S. | |
5023 | This can be called for any statement type. */ | |
5024 | ||
5025 | static void | |
1579bae1 AM |
5026 | print_statement (lang_statement_union_type *s, |
5027 | lang_output_section_statement_type *os) | |
252b5132 RH |
5028 | { |
5029 | switch (s->header.type) | |
5030 | { | |
5031 | default: | |
5032 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
5033 | FAIL (); | |
5034 | break; | |
5035 | case lang_constructors_statement_enum: | |
5036 | if (constructor_list.head != NULL) | |
5037 | { | |
5038 | if (constructors_sorted) | |
5039 | minfo (" SORT (CONSTRUCTORS)\n"); | |
5040 | else | |
5041 | minfo (" CONSTRUCTORS\n"); | |
5042 | print_statement_list (constructor_list.head, os); | |
5043 | } | |
5044 | break; | |
5045 | case lang_wild_statement_enum: | |
5046 | print_wild_statement (&s->wild_statement, os); | |
5047 | break; | |
5048 | case lang_address_statement_enum: | |
5049 | print_address_statement (&s->address_statement); | |
5050 | break; | |
5051 | case lang_object_symbols_statement_enum: | |
5052 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
5053 | break; | |
5054 | case lang_fill_statement_enum: | |
5055 | print_fill_statement (&s->fill_statement); | |
5056 | break; | |
5057 | case lang_data_statement_enum: | |
5058 | print_data_statement (&s->data_statement); | |
5059 | break; | |
5060 | case lang_reloc_statement_enum: | |
5061 | print_reloc_statement (&s->reloc_statement); | |
5062 | break; | |
5063 | case lang_input_section_enum: | |
d64703c6 | 5064 | print_input_section (s->input_section.section, FALSE); |
252b5132 RH |
5065 | break; |
5066 | case lang_padding_statement_enum: | |
5067 | print_padding_statement (&s->padding_statement); | |
5068 | break; | |
5069 | case lang_output_section_statement_enum: | |
5070 | print_output_section_statement (&s->output_section_statement); | |
5071 | break; | |
5072 | case lang_assignment_statement_enum: | |
5073 | print_assignment (&s->assignment_statement, os); | |
5074 | break; | |
5075 | case lang_target_statement_enum: | |
5076 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5077 | break; | |
5078 | case lang_output_statement_enum: | |
5079 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5080 | if (output_target != NULL) | |
5081 | minfo (" %s", output_target); | |
5082 | minfo (")\n"); | |
5083 | break; | |
5084 | case lang_input_statement_enum: | |
5085 | print_input_statement (&s->input_statement); | |
5086 | break; | |
5087 | case lang_group_statement_enum: | |
5088 | print_group (&s->group_statement, os); | |
5089 | break; | |
53d25da6 AM |
5090 | case lang_insert_statement_enum: |
5091 | minfo ("INSERT %s %s\n", | |
5092 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5093 | s->insert_statement.where); | |
5094 | break; | |
252b5132 RH |
5095 | } |
5096 | } | |
5097 | ||
5098 | static void | |
1579bae1 | 5099 | print_statements (void) |
252b5132 RH |
5100 | { |
5101 | print_statement_list (statement_list.head, abs_output_section); | |
5102 | } | |
5103 | ||
5104 | /* Print the first N statements in statement list S to STDERR. | |
5105 | If N == 0, nothing is printed. | |
5106 | If N < 0, the entire list is printed. | |
5107 | Intended to be called from GDB. */ | |
5108 | ||
5109 | void | |
1579bae1 | 5110 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
5111 | { |
5112 | FILE *map_save = config.map_file; | |
5113 | ||
5114 | config.map_file = stderr; | |
5115 | ||
5116 | if (n < 0) | |
5117 | print_statement_list (s, abs_output_section); | |
5118 | else | |
5119 | { | |
5120 | while (s && --n >= 0) | |
5121 | { | |
5122 | print_statement (s, abs_output_section); | |
bba1a0c0 | 5123 | s = s->header.next; |
252b5132 RH |
5124 | } |
5125 | } | |
5126 | ||
5127 | config.map_file = map_save; | |
5128 | } | |
5129 | ||
b3327aad | 5130 | static void |
1579bae1 AM |
5131 | insert_pad (lang_statement_union_type **ptr, |
5132 | fill_type *fill, | |
1a69ff95 | 5133 | bfd_size_type alignment_needed, |
1579bae1 AM |
5134 | asection *output_section, |
5135 | bfd_vma dot) | |
252b5132 | 5136 | { |
b7761f11 | 5137 | static fill_type zero_fill; |
e9ee469a | 5138 | lang_statement_union_type *pad = NULL; |
b3327aad | 5139 | |
e9ee469a AM |
5140 | if (ptr != &statement_list.head) |
5141 | pad = ((lang_statement_union_type *) | |
5142 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5143 | if (pad != NULL | |
5144 | && pad->header.type == lang_padding_statement_enum | |
5145 | && pad->padding_statement.output_section == output_section) | |
5146 | { | |
5147 | /* Use the existing pad statement. */ | |
5148 | } | |
5149 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
5150 | && pad->header.type == lang_padding_statement_enum |
5151 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5152 | { |
e9ee469a | 5153 | /* Use the existing pad statement. */ |
252b5132 | 5154 | } |
b3327aad | 5155 | else |
252b5132 | 5156 | { |
b3327aad | 5157 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5158 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5159 | pad->header.next = *ptr; |
5160 | *ptr = pad; | |
5161 | pad->header.type = lang_padding_statement_enum; | |
5162 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5163 | if (fill == NULL) |
2c382fb6 | 5164 | fill = &zero_fill; |
b3327aad | 5165 | pad->padding_statement.fill = fill; |
252b5132 | 5166 | } |
b3327aad AM |
5167 | pad->padding_statement.output_offset = dot - output_section->vma; |
5168 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5169 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5170 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5171 | - output_section->vma); | |
252b5132 RH |
5172 | } |
5173 | ||
08da4cac KH |
5174 | /* Work out how much this section will move the dot point. */ |
5175 | ||
252b5132 | 5176 | static bfd_vma |
6feb9908 AM |
5177 | size_input_section |
5178 | (lang_statement_union_type **this_ptr, | |
5179 | lang_output_section_statement_type *output_section_statement, | |
5180 | fill_type *fill, | |
abf874aa | 5181 | bfd_boolean *removed, |
6feb9908 | 5182 | bfd_vma dot) |
252b5132 RH |
5183 | { |
5184 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5185 | asection *i = is->section; | |
93d1b056 | 5186 | asection *o = output_section_statement->bfd_section; |
abf874aa CL |
5187 | *removed = 0; |
5188 | ||
5189 | if (link_info.non_contiguous_regions) | |
5190 | { | |
5191 | /* If the input section I has already been successfully assigned | |
5192 | to an output section other than O, don't bother with it and | |
5193 | let the caller remove it from the list. Keep processing in | |
5194 | case we have already handled O, because the repeated passes | |
5195 | have reinitialized its size. */ | |
5196 | if (i->already_assigned && i->already_assigned != o) | |
5197 | { | |
5198 | *removed = 1; | |
5199 | return dot; | |
5200 | } | |
5201 | } | |
252b5132 | 5202 | |
93d1b056 AM |
5203 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5204 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5205 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5206 | || output_section_statement->ignored) | |
93d1b056 AM |
5207 | i->output_offset = dot - o->vma; |
5208 | else | |
252b5132 | 5209 | { |
1a69ff95 | 5210 | bfd_size_type alignment_needed; |
b3327aad AM |
5211 | |
5212 | /* Align this section first to the input sections requirement, | |
5213 | then to the output section's requirement. If this alignment | |
5214 | is greater than any seen before, then record it too. Perform | |
5215 | the alignment by inserting a magic 'padding' statement. */ | |
5216 | ||
3d9c8f6b AM |
5217 | if (output_section_statement->subsection_alignment != NULL) |
5218 | i->alignment_power | |
5219 | = exp_get_power (output_section_statement->subsection_alignment, | |
5220 | "subsection alignment"); | |
b3327aad | 5221 | |
b3327aad AM |
5222 | if (o->alignment_power < i->alignment_power) |
5223 | o->alignment_power = i->alignment_power; | |
252b5132 | 5224 | |
b3327aad AM |
5225 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5226 | ||
5227 | if (alignment_needed != 0) | |
5228 | { | |
e5caa5e0 | 5229 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5230 | dot += alignment_needed; |
5231 | } | |
252b5132 | 5232 | |
abf874aa CL |
5233 | if (link_info.non_contiguous_regions) |
5234 | { | |
5235 | /* If I would overflow O, let the caller remove I from the | |
5236 | list. */ | |
5237 | if (output_section_statement->region) | |
5238 | { | |
5239 | bfd_vma end = output_section_statement->region->origin | |
5240 | + output_section_statement->region->length; | |
5241 | ||
5242 | if (dot + TO_ADDR (i->size) > end) | |
5243 | { | |
5244 | if (i->flags & SEC_LINKER_CREATED) | |
53215f21 CL |
5245 | einfo (_("%F%P: Output section '%s' not large enough for the " |
5246 | "linker-created stubs section '%s'.\n"), | |
5247 | i->output_section->name, i->name); | |
abf874aa CL |
5248 | |
5249 | if (i->rawsize && i->rawsize != i->size) | |
53215f21 CL |
5250 | einfo (_("%F%P: Relaxation not supported with " |
5251 | "--enable-non-contiguous-regions (section '%s' " | |
5252 | "would overflow '%s' after it changed size).\n"), | |
5253 | i->name, i->output_section->name); | |
abf874aa CL |
5254 | |
5255 | *removed = 1; | |
5256 | dot = end; | |
5257 | i->output_section = NULL; | |
5258 | return dot; | |
5259 | } | |
5260 | } | |
5261 | } | |
5262 | ||
08da4cac | 5263 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5264 | i->output_offset = dot - o->vma; |
252b5132 | 5265 | |
08da4cac | 5266 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5267 | dot += TO_ADDR (i->size); |
8772de11 MR |
5268 | if (!(o->flags & SEC_FIXED_SIZE)) |
5269 | o->size = TO_SIZE (dot - o->vma); | |
abf874aa CL |
5270 | |
5271 | if (link_info.non_contiguous_regions) | |
5272 | { | |
5273 | /* Record that I was successfully assigned to O, and update | |
5274 | its actual output section too. */ | |
5275 | i->already_assigned = o; | |
5276 | i->output_section = o; | |
5277 | } | |
252b5132 | 5278 | } |
252b5132 RH |
5279 | |
5280 | return dot; | |
5281 | } | |
5282 | ||
a87dd97a AM |
5283 | struct check_sec |
5284 | { | |
5285 | asection *sec; | |
5286 | bfd_boolean warned; | |
5287 | }; | |
5288 | ||
5daa8fe7 L |
5289 | static int |
5290 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5291 | { | |
a87dd97a AM |
5292 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; |
5293 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5294 | ||
5295 | if (sec1->lma < sec2->lma) | |
5296 | return -1; | |
5297 | else if (sec1->lma > sec2->lma) | |
5298 | return 1; | |
5299 | else if (sec1->id < sec2->id) | |
5300 | return -1; | |
5301 | else if (sec1->id > sec2->id) | |
5302 | return 1; | |
5303 | ||
5304 | return 0; | |
5305 | } | |
5306 | ||
5307 | static int | |
5308 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5309 | { | |
5310 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5311 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5daa8fe7 | 5312 | |
a87dd97a | 5313 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5314 | return -1; |
a87dd97a | 5315 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5316 | return 1; |
7f6a71ff JM |
5317 | else if (sec1->id < sec2->id) |
5318 | return -1; | |
5319 | else if (sec1->id > sec2->id) | |
5320 | return 1; | |
5daa8fe7 L |
5321 | |
5322 | return 0; | |
5323 | } | |
5324 | ||
2e4a7aea AM |
5325 | #define IS_TBSS(s) \ |
5326 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5327 | ||
eea6121a | 5328 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5329 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5330 | |
252b5132 | 5331 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5332 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5333 | section addresses of the two sections. Also check whether any memory |
5334 | region has overflowed. */ | |
08da4cac | 5335 | |
252b5132 | 5336 | static void |
1579bae1 | 5337 | lang_check_section_addresses (void) |
252b5132 | 5338 | { |
f4427a75 | 5339 | asection *s, *p; |
a87dd97a AM |
5340 | struct check_sec *sections; |
5341 | size_t i, count; | |
d40e34db | 5342 | bfd_vma addr_mask; |
5daa8fe7 L |
5343 | bfd_vma s_start; |
5344 | bfd_vma s_end; | |
a87dd97a AM |
5345 | bfd_vma p_start = 0; |
5346 | bfd_vma p_end = 0; | |
20e56351 | 5347 | lang_memory_region_type *m; |
136a43b7 | 5348 | bfd_boolean overlays; |
5daa8fe7 | 5349 | |
21701718 | 5350 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
5351 | addr_mask = ((bfd_vma) 1 << |
5352 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5353 | addr_mask = (addr_mask << 1) + 1; | |
5354 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
21701718 TG |
5355 | if ((s->flags & SEC_ALLOC) != 0) |
5356 | { | |
5357 | s_end = (s->vma + s->size) & addr_mask; | |
5358 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5359 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5360 | s->name); | |
5361 | else | |
5362 | { | |
5363 | s_end = (s->lma + s->size) & addr_mask; | |
5364 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5365 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5366 | s->name); | |
5367 | } | |
5368 | } | |
d40e34db | 5369 | |
f13a99db | 5370 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5371 | return; |
5372 | ||
a87dd97a AM |
5373 | count = bfd_count_sections (link_info.output_bfd); |
5374 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5375 | |
5376 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5377 | count = 0; |
f13a99db | 5378 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5379 | { |
a87dd97a | 5380 | if (IGNORE_SECTION (s) |
ec6d26be | 5381 | || s->size == 0) |
33275c22 | 5382 | continue; |
5f992e62 | 5383 | |
a87dd97a AM |
5384 | sections[count].sec = s; |
5385 | sections[count].warned = FALSE; | |
5daa8fe7 L |
5386 | count++; |
5387 | } | |
329c1c86 | 5388 | |
5daa8fe7 | 5389 | if (count <= 1) |
136a43b7 AM |
5390 | { |
5391 | free (sections); | |
5392 | return; | |
5393 | } | |
5f992e62 | 5394 | |
a87dd97a | 5395 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5396 | |
136a43b7 | 5397 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5398 | loadable sections, even with overlays. */ |
5399 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5400 | { |
a87dd97a | 5401 | s = sections[i].sec; |
61826503 | 5402 | init_opb (s); |
a87dd97a AM |
5403 | if ((s->flags & SEC_LOAD) != 0) |
5404 | { | |
5405 | s_start = s->lma; | |
5406 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5407 | ||
5408 | /* Look for an overlap. We have sorted sections by lma, so | |
5409 | we know that s_start >= p_start. Besides the obvious | |
5410 | case of overlap when the current section starts before | |
5411 | the previous one ends, we also must have overlap if the | |
5412 | previous section wraps around the address space. */ | |
5413 | if (p != NULL | |
5414 | && (s_start <= p_end | |
5415 | || p_end < p_start)) | |
5416 | { | |
5417 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5418 | " overlaps section %s LMA [%V,%V]\n"), | |
5419 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5420 | sections[i].warned = TRUE; | |
5421 | } | |
5422 | p = s; | |
5423 | p_start = s_start; | |
5424 | p_end = s_end; | |
5425 | } | |
5426 | } | |
5427 | ||
136a43b7 AM |
5428 | /* If any non-zero size allocated section (excluding tbss) starts at |
5429 | exactly the same VMA as another such section, then we have | |
5430 | overlays. Overlays generated by the OVERLAY keyword will have | |
5431 | this property. It is possible to intentionally generate overlays | |
5432 | that fail this test, but it would be unusual. */ | |
5433 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
5434 | overlays = FALSE; | |
5435 | p_start = sections[0].sec->vma; | |
5436 | for (i = 1; i < count; i++) | |
a87dd97a | 5437 | { |
136a43b7 AM |
5438 | s_start = sections[i].sec->vma; |
5439 | if (p_start == s_start) | |
5440 | { | |
5441 | overlays = TRUE; | |
5442 | break; | |
5443 | } | |
5444 | p_start = s_start; | |
5445 | } | |
a87dd97a | 5446 | |
136a43b7 AM |
5447 | /* Now check section VMAs if no overlays were detected. */ |
5448 | if (!overlays) | |
5449 | { | |
a87dd97a AM |
5450 | for (p = NULL, i = 0; i < count; i++) |
5451 | { | |
5452 | s = sections[i].sec; | |
61826503 | 5453 | init_opb (s); |
a87dd97a AM |
5454 | s_start = s->vma; |
5455 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5456 | ||
5457 | if (p != NULL | |
5458 | && !sections[i].warned | |
5459 | && (s_start <= p_end | |
5460 | || p_end < p_start)) | |
5461 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5462 | " overlaps section %s VMA [%V,%V]\n"), | |
5463 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5464 | p = s; | |
5465 | p_start = s_start; | |
5466 | p_end = s_end; | |
5467 | } | |
33275c22 | 5468 | } |
5daa8fe7 L |
5469 | |
5470 | free (sections); | |
20e56351 DJ |
5471 | |
5472 | /* If any memory region has overflowed, report by how much. | |
5473 | We do not issue this diagnostic for regions that had sections | |
5474 | explicitly placed outside their bounds; os_region_check's | |
5475 | diagnostics are adequate for that case. | |
5476 | ||
5477 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5478 | might overflow a 32-bit integer. There is, alas, no way to print | |
5479 | a bfd_vma quantity in decimal. */ | |
5480 | for (m = lang_memory_region_list; m; m = m->next) | |
5481 | if (m->had_full_message) | |
992a06ee AM |
5482 | { |
5483 | unsigned long over = m->current - (m->origin + m->length); | |
5484 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5485 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5486 | over), | |
5487 | m->name_list.name, over); | |
5488 | } | |
252b5132 RH |
5489 | } |
5490 | ||
562d3460 TW |
5491 | /* Make sure the new address is within the region. We explicitly permit the |
5492 | current address to be at the exact end of the region when the address is | |
5493 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 5494 | calculation wraps around. */ |
562d3460 TW |
5495 | |
5496 | static void | |
1579bae1 | 5497 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5498 | lang_memory_region_type *region, |
1579bae1 | 5499 | etree_type *tree, |
91d6fa6a | 5500 | bfd_vma rbase) |
562d3460 TW |
5501 | { |
5502 | if ((region->current < region->origin | |
5503 | || (region->current - region->origin > region->length)) | |
5504 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5505 | || rbase == 0)) |
562d3460 | 5506 | { |
1579bae1 | 5507 | if (tree != NULL) |
b7a26f91 | 5508 | { |
871b3ab2 | 5509 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5510 | " is not within region `%s'\n"), |
b7a26f91 KH |
5511 | region->current, |
5512 | os->bfd_section->owner, | |
5513 | os->bfd_section->name, | |
4a93e180 | 5514 | region->name_list.name); |
b7a26f91 | 5515 | } |
20e56351 | 5516 | else if (!region->had_full_message) |
b7a26f91 | 5517 | { |
20e56351 DJ |
5518 | region->had_full_message = TRUE; |
5519 | ||
871b3ab2 | 5520 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5521 | os->bfd_section->owner, |
20e56351 | 5522 | os->bfd_section->name, |
4a93e180 | 5523 | region->name_list.name); |
b7a26f91 | 5524 | } |
562d3460 TW |
5525 | } |
5526 | } | |
5527 | ||
ed1794ee L |
5528 | static void |
5529 | ldlang_check_relro_region (lang_statement_union_type *s, | |
5530 | seg_align_type *seg) | |
5531 | { | |
5532 | if (seg->relro == exp_seg_relro_start) | |
5533 | { | |
5534 | if (!seg->relro_start_stat) | |
5535 | seg->relro_start_stat = s; | |
5536 | else | |
5537 | { | |
5538 | ASSERT (seg->relro_start_stat == s); | |
5539 | } | |
5540 | } | |
5541 | else if (seg->relro == exp_seg_relro_end) | |
5542 | { | |
5543 | if (!seg->relro_end_stat) | |
5544 | seg->relro_end_stat = s; | |
5545 | else | |
5546 | { | |
5547 | ASSERT (seg->relro_end_stat == s); | |
5548 | } | |
5549 | } | |
5550 | } | |
5551 | ||
252b5132 RH |
5552 | /* Set the sizes for all the output sections. */ |
5553 | ||
2d20f7bf | 5554 | static bfd_vma |
1579bae1 | 5555 | lang_size_sections_1 |
e535e147 | 5556 | (lang_statement_union_type **prev, |
1579bae1 | 5557 | lang_output_section_statement_type *output_section_statement, |
1579bae1 AM |
5558 | fill_type *fill, |
5559 | bfd_vma dot, | |
5560 | bfd_boolean *relax, | |
5561 | bfd_boolean check_regions) | |
252b5132 | 5562 | { |
e535e147 | 5563 | lang_statement_union_type *s; |
abf874aa CL |
5564 | lang_statement_union_type *prev_s = NULL; |
5565 | bfd_boolean removed_prev_s = FALSE; | |
e535e147 | 5566 | |
252b5132 | 5567 | /* Size up the sections from their constituent parts. */ |
abf874aa | 5568 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) |
252b5132 | 5569 | { |
abf874aa CL |
5570 | bfd_boolean removed=FALSE; |
5571 | ||
252b5132 RH |
5572 | switch (s->header.type) |
5573 | { | |
5574 | case lang_output_section_statement_enum: | |
5575 | { | |
13075d04 | 5576 | bfd_vma newdot, after, dotdelta; |
d1778b88 | 5577 | lang_output_section_statement_type *os; |
cde9e0be | 5578 | lang_memory_region_type *r; |
c5b0a9ef | 5579 | int section_alignment = 0; |
252b5132 | 5580 | |
d1778b88 | 5581 | os = &s->output_section_statement; |
61826503 | 5582 | init_opb (os->bfd_section); |
8658f989 AM |
5583 | if (os->constraint == -1) |
5584 | break; | |
5585 | ||
fd68d03d AM |
5586 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
5587 | here, in lang_insert_orphan, or in the default linker scripts. | |
5588 | This is covering for coff backend linker bugs. See PR6945. */ | |
5589 | if (os->addr_tree == NULL | |
0e1862bb | 5590 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5591 | && (bfd_get_flavour (link_info.output_bfd) |
5592 | == bfd_target_coff_flavour)) | |
eceda120 | 5593 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5594 | if (os->addr_tree != NULL) |
5595 | { | |
cde9e0be | 5596 | os->processed_vma = FALSE; |
a5df8c84 | 5597 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5598 | |
3bf9618b | 5599 | if (expld.result.valid_p) |
7542af2a AM |
5600 | { |
5601 | dot = expld.result.value; | |
5602 | if (expld.result.section != NULL) | |
5603 | dot += expld.result.section->vma; | |
5604 | } | |
3bf9618b | 5605 | else if (expld.phase != lang_mark_phase_enum) |
df5f2391 | 5606 | einfo (_("%F%P:%pS: non constant or forward reference" |
a5df8c84 | 5607 | " address expression for section %s\n"), |
dab69f68 | 5608 | os->addr_tree, os->name); |
a5df8c84 AM |
5609 | } |
5610 | ||
e9ee469a | 5611 | if (os->bfd_section == NULL) |
75ff4589 | 5612 | /* This section was removed or never actually created. */ |
252b5132 RH |
5613 | break; |
5614 | ||
5615 | /* If this is a COFF shared library section, use the size and | |
5616 | address from the input section. FIXME: This is COFF | |
5617 | specific; it would be cleaner if there were some other way | |
5618 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
5619 | if (((bfd_get_flavour (link_info.output_bfd) |
5620 | == bfd_target_ecoff_flavour) | |
5621 | || (bfd_get_flavour (link_info.output_bfd) | |
5622 | == bfd_target_coff_flavour)) | |
ebe372c1 | 5623 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 5624 | { |
08da4cac | 5625 | asection *input; |
252b5132 RH |
5626 | |
5627 | if (os->children.head == NULL | |
bba1a0c0 | 5628 | || os->children.head->header.next != NULL |
6feb9908 AM |
5629 | || (os->children.head->header.type |
5630 | != lang_input_section_enum)) | |
df5f2391 | 5631 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 5632 | " section %s\n"), os->name); |
252b5132 RH |
5633 | |
5634 | input = os->children.head->input_section.section; | |
fd361982 AM |
5635 | bfd_set_section_vma (os->bfd_section, |
5636 | bfd_section_vma (input)); | |
8772de11 MR |
5637 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5638 | os->bfd_section->size = input->size; | |
252b5132 RH |
5639 | break; |
5640 | } | |
5641 | ||
a5df8c84 | 5642 | newdot = dot; |
13075d04 | 5643 | dotdelta = 0; |
252b5132 RH |
5644 | if (bfd_is_abs_section (os->bfd_section)) |
5645 | { | |
5646 | /* No matter what happens, an abs section starts at zero. */ | |
5647 | ASSERT (os->bfd_section->vma == 0); | |
5648 | } | |
5649 | else | |
5650 | { | |
1579bae1 | 5651 | if (os->addr_tree == NULL) |
252b5132 RH |
5652 | { |
5653 | /* No address specified for this section, get one | |
5654 | from the region specification. */ | |
1579bae1 | 5655 | if (os->region == NULL |
6feb9908 | 5656 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
5657 | && os->region->name_list.name[0] == '*' |
5658 | && strcmp (os->region->name_list.name, | |
6feb9908 | 5659 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
5660 | { |
5661 | os->region = lang_memory_default (os->bfd_section); | |
5662 | } | |
5663 | ||
5664 | /* If a loadable section is using the default memory | |
5665 | region, and some non default memory regions were | |
66184979 | 5666 | defined, issue an error message. */ |
f0636a44 AM |
5667 | if (!os->ignored |
5668 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 5669 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 5670 | && check_regions |
4a93e180 | 5671 | && strcmp (os->region->name_list.name, |
6feb9908 | 5672 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 5673 | && lang_memory_region_list != NULL |
4a93e180 | 5674 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 5675 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a | 5676 | || lang_memory_region_list->next != NULL) |
baf09cba | 5677 | && lang_sizing_iteration == 1) |
66184979 NC |
5678 | { |
5679 | /* By default this is an error rather than just a | |
5680 | warning because if we allocate the section to the | |
5681 | default memory region we can end up creating an | |
07f3b6ad KH |
5682 | excessively large binary, or even seg faulting when |
5683 | attempting to perform a negative seek. See | |
6feb9908 | 5684 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
5685 | for an example of this. This behaviour can be |
5686 | overridden by the using the --no-check-sections | |
5687 | switch. */ | |
5688 | if (command_line.check_section_addresses) | |
df5f2391 | 5689 | einfo (_("%F%P: error: no memory region specified" |
6feb9908 | 5690 | " for loadable section `%s'\n"), |
fd361982 | 5691 | bfd_section_name (os->bfd_section)); |
66184979 | 5692 | else |
6feb9908 AM |
5693 | einfo (_("%P: warning: no memory region specified" |
5694 | " for loadable section `%s'\n"), | |
fd361982 | 5695 | bfd_section_name (os->bfd_section)); |
66184979 | 5696 | } |
252b5132 | 5697 | |
e9ee469a | 5698 | newdot = os->region->current; |
c5b0a9ef | 5699 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 5700 | } |
94b50910 | 5701 | else |
3d9c8f6b AM |
5702 | section_alignment = exp_get_power (os->section_alignment, |
5703 | "section alignment"); | |
5f992e62 | 5704 | |
7270c5ed | 5705 | /* Align to what the section needs. */ |
c5b0a9ef | 5706 | if (section_alignment > 0) |
94b50910 AM |
5707 | { |
5708 | bfd_vma savedot = newdot; | |
baf09cba | 5709 | bfd_vma diff = 0; |
252b5132 | 5710 | |
baf09cba | 5711 | newdot = align_power (newdot, section_alignment); |
13075d04 | 5712 | dotdelta = newdot - savedot; |
baf09cba AM |
5713 | |
5714 | if (lang_sizing_iteration == 1) | |
5715 | diff = dotdelta; | |
5716 | else if (lang_sizing_iteration > 1) | |
92d4b13b AM |
5717 | { |
5718 | /* Only report adjustments that would change | |
5719 | alignment from what we have already reported. */ | |
5720 | diff = newdot - os->bfd_section->vma; | |
5721 | if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) | |
5722 | diff = 0; | |
5723 | } | |
baf09cba | 5724 | if (diff != 0 |
94b50910 | 5725 | && (config.warn_section_align |
baf09cba AM |
5726 | || os->addr_tree != NULL)) |
5727 | einfo (_("%P: warning: " | |
5728 | "start of section %s changed by %ld\n"), | |
5729 | os->name, (long) diff); | |
94b50910 | 5730 | } |
252b5132 | 5731 | |
fd361982 | 5732 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 5733 | |
252b5132 RH |
5734 | os->bfd_section->output_offset = 0; |
5735 | } | |
5736 | ||
e535e147 | 5737 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
5738 | os->fill, newdot, relax, check_regions); |
5739 | ||
cde9e0be | 5740 | os->processed_vma = TRUE; |
e9ee469a AM |
5741 | |
5742 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
5743 | /* Except for some special linker created sections, |
5744 | no output section should change from zero size | |
5745 | after strip_excluded_output_sections. A non-zero | |
5746 | size on an ignored section indicates that some | |
5747 | input section was not sized early enough. */ | |
5748 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 5749 | else |
e9ee469a | 5750 | { |
cde9e0be AM |
5751 | dot = os->bfd_section->vma; |
5752 | ||
5753 | /* Put the section within the requested block size, or | |
5754 | align at the block boundary. */ | |
5755 | after = ((dot | |
5756 | + TO_ADDR (os->bfd_section->size) | |
5757 | + os->block_value - 1) | |
5758 | & - (bfd_vma) os->block_value); | |
5759 | ||
8772de11 MR |
5760 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5761 | os->bfd_section->size = TO_SIZE (after | |
5762 | - os->bfd_section->vma); | |
cde9e0be AM |
5763 | } |
5764 | ||
5765 | /* Set section lma. */ | |
5766 | r = os->region; | |
5767 | if (r == NULL) | |
5768 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); | |
5769 | ||
5770 | if (os->load_base) | |
5771 | { | |
5772 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
5773 | os->bfd_section->lma = lma; | |
e9ee469a | 5774 | } |
07dfcf38 | 5775 | else if (os->lma_region != NULL) |
cde9e0be AM |
5776 | { |
5777 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 5778 | |
13075d04 SH |
5779 | if (os->align_lma_with_input) |
5780 | lma += dotdelta; | |
5781 | else | |
5782 | { | |
5783 | /* When LMA_REGION is the same as REGION, align the LMA | |
5784 | as we did for the VMA, possibly including alignment | |
5785 | from the bfd section. If a different region, then | |
5786 | only align according to the value in the output | |
5787 | statement. */ | |
5788 | if (os->lma_region != os->region) | |
3d9c8f6b AM |
5789 | section_alignment = exp_get_power (os->section_alignment, |
5790 | "section alignment"); | |
13075d04 SH |
5791 | if (section_alignment > 0) |
5792 | lma = align_power (lma, section_alignment); | |
5793 | } | |
cde9e0be AM |
5794 | os->bfd_section->lma = lma; |
5795 | } | |
dc0b6aa0 AM |
5796 | else if (r->last_os != NULL |
5797 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
5798 | { |
5799 | bfd_vma lma; | |
5800 | asection *last; | |
5801 | ||
5802 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
5803 | |
5804 | /* A backwards move of dot should be accompanied by | |
5805 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 5806 | os->load_base set) because backwards moves can |
dc0b6aa0 | 5807 | create overlapping LMAs. */ |
264b6205 | 5808 | if (dot < last->vma |
91eb6c46 | 5809 | && os->bfd_section->size != 0 |
ca62bc4a | 5810 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 5811 | { |
dc0b6aa0 AM |
5812 | /* If dot moved backwards then leave lma equal to |
5813 | vma. This is the old default lma, which might | |
5814 | just happen to work when the backwards move is | |
91eb6c46 AM |
5815 | sufficiently large. Nag if this changes anything, |
5816 | so people can fix their linker scripts. */ | |
5817 | ||
5818 | if (last->vma != last->lma) | |
0aa7f586 AM |
5819 | einfo (_("%P: warning: dot moved backwards " |
5820 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
5821 | } |
5822 | else | |
cde9e0be | 5823 | { |
152d792f AM |
5824 | /* If this is an overlay, set the current lma to that |
5825 | at the end of the previous section. */ | |
5826 | if (os->sectype == overlay_section) | |
fc90c280 | 5827 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
5828 | |
5829 | /* Otherwise, keep the same lma to vma relationship | |
5830 | as the previous section. */ | |
5831 | else | |
8610e0fd | 5832 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 5833 | |
c5b0a9ef AM |
5834 | if (section_alignment > 0) |
5835 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
5836 | os->bfd_section->lma = lma; |
5837 | } | |
5838 | } | |
5839 | os->processed_lma = TRUE; | |
5f992e62 | 5840 | |
cde9e0be AM |
5841 | /* Keep track of normal sections using the default |
5842 | lma region. We use this to set the lma for | |
5843 | following sections. Overlays or other linker | |
5844 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
5845 | default lma == vma is incorrect. |
5846 | To avoid warnings about dot moving backwards when using | |
5847 | -Ttext, don't start tracking sections until we find one | |
14ea2c1b AB |
5848 | of non-zero size or with lma set differently to vma. |
5849 | Do this tracking before we short-cut the loop so that we | |
5850 | track changes for the case where the section size is zero, | |
5851 | but the lma is set differently to the vma. This is | |
5852 | important, if an orphan section is placed after an | |
5853 | otherwise empty output section that has an explicit lma | |
5854 | set, we want that lma reflected in the orphans lma. */ | |
7b243801 AM |
5855 | if (((!IGNORE_SECTION (os->bfd_section) |
5856 | && (os->bfd_section->size != 0 | |
5857 | || (r->last_os == NULL | |
5858 | && os->bfd_section->vma != os->bfd_section->lma) | |
5859 | || (r->last_os != NULL | |
5860 | && dot >= (r->last_os->output_section_statement | |
5861 | .bfd_section->vma)))) | |
5862 | || os->sectype == first_overlay_section) | |
cde9e0be | 5863 | && os->lma_region == NULL |
0e1862bb | 5864 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 5865 | r->last_os = s; |
9f88b410 | 5866 | |
14ea2c1b AB |
5867 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
5868 | break; | |
5869 | ||
e5caec89 | 5870 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 5871 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 5872 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
5873 | dotdelta = TO_ADDR (os->bfd_section->size); |
5874 | else | |
5875 | dotdelta = 0; | |
5876 | dot += dotdelta; | |
e5caec89 | 5877 | |
9f88b410 | 5878 | if (os->update_dot_tree != 0) |
e9ee469a | 5879 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 5880 | |
252b5132 RH |
5881 | /* Update dot in the region ? |
5882 | We only do this if the section is going to be allocated, | |
5883 | since unallocated sections do not contribute to the region's | |
2e76e85a | 5884 | overall size in memory. */ |
1579bae1 | 5885 | if (os->region != NULL |
2e76e85a | 5886 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
5887 | { |
5888 | os->region->current = dot; | |
5f992e62 | 5889 | |
cf888e70 NC |
5890 | if (check_regions) |
5891 | /* Make sure the new address is within the region. */ | |
5892 | os_region_check (os, os->region, os->addr_tree, | |
5893 | os->bfd_section->vma); | |
08da4cac | 5894 | |
a2cab753 | 5895 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
5896 | && ((os->bfd_section->flags & SEC_LOAD) |
5897 | || os->align_lma_with_input)) | |
08da4cac | 5898 | { |
13075d04 | 5899 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 5900 | |
cf888e70 NC |
5901 | if (check_regions) |
5902 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 5903 | os->bfd_section->lma); |
08da4cac | 5904 | } |
252b5132 RH |
5905 | } |
5906 | } | |
5907 | break; | |
5908 | ||
5909 | case lang_constructors_statement_enum: | |
e535e147 | 5910 | dot = lang_size_sections_1 (&constructor_list.head, |
2d20f7bf | 5911 | output_section_statement, |
cf888e70 | 5912 | fill, dot, relax, check_regions); |
252b5132 RH |
5913 | break; |
5914 | ||
5915 | case lang_data_statement_enum: | |
5916 | { | |
5917 | unsigned int size = 0; | |
5918 | ||
7fabd029 | 5919 | s->data_statement.output_offset = |
08da4cac | 5920 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
5921 | s->data_statement.output_section = |
5922 | output_section_statement->bfd_section; | |
5923 | ||
1b493742 NS |
5924 | /* We might refer to provided symbols in the expression, and |
5925 | need to mark them as needed. */ | |
e9ee469a | 5926 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 5927 | |
252b5132 RH |
5928 | switch (s->data_statement.type) |
5929 | { | |
08da4cac KH |
5930 | default: |
5931 | abort (); | |
252b5132 RH |
5932 | case QUAD: |
5933 | case SQUAD: | |
5934 | size = QUAD_SIZE; | |
5935 | break; | |
5936 | case LONG: | |
5937 | size = LONG_SIZE; | |
5938 | break; | |
5939 | case SHORT: | |
5940 | size = SHORT_SIZE; | |
5941 | break; | |
5942 | case BYTE: | |
5943 | size = BYTE_SIZE; | |
5944 | break; | |
5945 | } | |
e5caa5e0 AM |
5946 | if (size < TO_SIZE ((unsigned) 1)) |
5947 | size = TO_SIZE ((unsigned) 1); | |
5948 | dot += TO_ADDR (size); | |
8772de11 MR |
5949 | if (!(output_section_statement->bfd_section->flags |
5950 | & SEC_FIXED_SIZE)) | |
5951 | output_section_statement->bfd_section->size | |
5952 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
aa4c3319 | 5953 | |
252b5132 RH |
5954 | } |
5955 | break; | |
5956 | ||
5957 | case lang_reloc_statement_enum: | |
5958 | { | |
5959 | int size; | |
5960 | ||
7fabd029 | 5961 | s->reloc_statement.output_offset = |
252b5132 RH |
5962 | dot - output_section_statement->bfd_section->vma; |
5963 | s->reloc_statement.output_section = | |
5964 | output_section_statement->bfd_section; | |
5965 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 5966 | dot += TO_ADDR (size); |
8772de11 MR |
5967 | if (!(output_section_statement->bfd_section->flags |
5968 | & SEC_FIXED_SIZE)) | |
5969 | output_section_statement->bfd_section->size | |
5970 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
252b5132 RH |
5971 | } |
5972 | break; | |
5f992e62 | 5973 | |
252b5132 | 5974 | case lang_wild_statement_enum: |
e535e147 | 5975 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
2d20f7bf | 5976 | output_section_statement, |
cf888e70 | 5977 | fill, dot, relax, check_regions); |
252b5132 RH |
5978 | break; |
5979 | ||
5980 | case lang_object_symbols_statement_enum: | |
3b6c1966 AM |
5981 | link_info.create_object_symbols_section |
5982 | = output_section_statement->bfd_section; | |
5983 | output_section_statement->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 5984 | break; |
e9ee469a | 5985 | |
252b5132 RH |
5986 | case lang_output_statement_enum: |
5987 | case lang_target_statement_enum: | |
5988 | break; | |
e9ee469a | 5989 | |
252b5132 RH |
5990 | case lang_input_section_enum: |
5991 | { | |
5992 | asection *i; | |
5993 | ||
e535e147 | 5994 | i = s->input_section.section; |
eea6121a | 5995 | if (relax) |
252b5132 | 5996 | { |
b34976b6 | 5997 | bfd_boolean again; |
252b5132 | 5998 | |
0aa7f586 | 5999 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
df5f2391 | 6000 | einfo (_("%F%P: can't relax section: %E\n")); |
252b5132 | 6001 | if (again) |
b34976b6 | 6002 | *relax = TRUE; |
252b5132 | 6003 | } |
b3327aad | 6004 | dot = size_input_section (prev, output_section_statement, |
abf874aa | 6005 | fill, &removed, dot); |
252b5132 RH |
6006 | } |
6007 | break; | |
e9ee469a | 6008 | |
252b5132 RH |
6009 | case lang_input_statement_enum: |
6010 | break; | |
e9ee469a | 6011 | |
252b5132 | 6012 | case lang_fill_statement_enum: |
08da4cac KH |
6013 | s->fill_statement.output_section = |
6014 | output_section_statement->bfd_section; | |
252b5132 RH |
6015 | |
6016 | fill = s->fill_statement.fill; | |
6017 | break; | |
e9ee469a | 6018 | |
252b5132 RH |
6019 | case lang_assignment_statement_enum: |
6020 | { | |
6021 | bfd_vma newdot = dot; | |
49c13adb | 6022 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 6023 | |
0f99513f | 6024 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6025 | |
49c13adb | 6026 | exp_fold_tree (tree, |
408082ec | 6027 | output_section_statement->bfd_section, |
252b5132 RH |
6028 | &newdot); |
6029 | ||
ed1794ee L |
6030 | ldlang_check_relro_region (s, &expld.dataseg); |
6031 | ||
0f99513f | 6032 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6033 | |
d2667025 | 6034 | /* This symbol may be relative to this section. */ |
a14a5de3 | 6035 | if ((tree->type.node_class == etree_provided |
49c13adb L |
6036 | || tree->type.node_class == etree_assign) |
6037 | && (tree->assign.dst [0] != '.' | |
6038 | || tree->assign.dst [1] != '\0')) | |
6d8bf25d | 6039 | output_section_statement->update_dot = 1; |
49c13adb | 6040 | |
85852e36 | 6041 | if (!output_section_statement->ignored) |
252b5132 | 6042 | { |
252b5132 RH |
6043 | if (output_section_statement == abs_output_section) |
6044 | { | |
6045 | /* If we don't have an output section, then just adjust | |
6046 | the default memory address. */ | |
6feb9908 AM |
6047 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
6048 | FALSE)->current = newdot; | |
252b5132 | 6049 | } |
85852e36 | 6050 | else if (newdot != dot) |
252b5132 | 6051 | { |
b3327aad AM |
6052 | /* Insert a pad after this statement. We can't |
6053 | put the pad before when relaxing, in case the | |
6054 | assignment references dot. */ | |
e5caa5e0 | 6055 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
6056 | output_section_statement->bfd_section, dot); |
6057 | ||
6058 | /* Don't neuter the pad below when relaxing. */ | |
6059 | s = s->header.next; | |
9dfc8ab2 | 6060 | |
e9ee469a AM |
6061 | /* If dot is advanced, this implies that the section |
6062 | should have space allocated to it, unless the | |
6063 | user has explicitly stated that the section | |
2e76e85a | 6064 | should not be allocated. */ |
f4eaaf7f AM |
6065 | if (output_section_statement->sectype != noalloc_section |
6066 | && (output_section_statement->sectype != noload_section | |
6067 | || (bfd_get_flavour (link_info.output_bfd) | |
6068 | == bfd_target_elf_flavour))) | |
e9ee469a AM |
6069 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
6070 | } | |
252b5132 RH |
6071 | dot = newdot; |
6072 | } | |
6073 | } | |
6074 | break; | |
6075 | ||
6076 | case lang_padding_statement_enum: | |
c0c330a7 AM |
6077 | /* If this is the first time lang_size_sections is called, |
6078 | we won't have any padding statements. If this is the | |
6079 | second or later passes when relaxing, we should allow | |
6080 | padding to shrink. If padding is needed on this pass, it | |
6081 | will be added back in. */ | |
6082 | s->padding_statement.size = 0; | |
6e814ff8 AM |
6083 | |
6084 | /* Make sure output_offset is valid. If relaxation shrinks | |
6085 | the section and this pad isn't needed, it's possible to | |
6086 | have output_offset larger than the final size of the | |
6087 | section. bfd_set_section_contents will complain even for | |
6088 | a pad size of zero. */ | |
6089 | s->padding_statement.output_offset | |
6090 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
6091 | break; |
6092 | ||
6093 | case lang_group_statement_enum: | |
e535e147 | 6094 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
2d20f7bf | 6095 | output_section_statement, |
cf888e70 | 6096 | fill, dot, relax, check_regions); |
252b5132 RH |
6097 | break; |
6098 | ||
53d25da6 | 6099 | case lang_insert_statement_enum: |
252b5132 RH |
6100 | break; |
6101 | ||
c0c330a7 | 6102 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
6103 | case lang_address_statement_enum: |
6104 | break; | |
53d25da6 AM |
6105 | |
6106 | default: | |
6107 | FAIL (); | |
6108 | break; | |
252b5132 | 6109 | } |
abf874aa CL |
6110 | |
6111 | /* If an input section doesn't fit in the current output | |
6112 | section, remove it from the list. Handle the case where we | |
6113 | have to remove an input_section statement here: there is a | |
6114 | special case to remove the first element of the list. */ | |
6115 | if (link_info.non_contiguous_regions && removed) | |
6116 | { | |
6117 | /* If we removed the first element during the previous | |
6118 | iteration, override the loop assignment of prev_s. */ | |
6119 | if (removed_prev_s) | |
6120 | prev_s = NULL; | |
6121 | ||
6122 | if (prev_s) | |
6123 | { | |
6124 | /* If there was a real previous input section, just skip | |
6125 | the current one. */ | |
6126 | prev_s->header.next=s->header.next; | |
6127 | s = prev_s; | |
6128 | removed_prev_s = FALSE; | |
6129 | } | |
6130 | else | |
6131 | { | |
6132 | /* Remove the first input section of the list. */ | |
6133 | *prev = s->header.next; | |
6134 | removed_prev_s = TRUE; | |
6135 | } | |
6136 | ||
6137 | /* Move to next element, unless we removed the head of the | |
6138 | list. */ | |
6139 | if (!removed_prev_s) | |
6140 | prev = &s->header.next; | |
6141 | } | |
6142 | else | |
6143 | { | |
6144 | prev = &s->header.next; | |
6145 | removed_prev_s = FALSE; | |
6146 | } | |
252b5132 RH |
6147 | } |
6148 | return dot; | |
6149 | } | |
6150 | ||
591a748a NC |
6151 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
6152 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
6153 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
6154 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b NC |
6155 | |
6156 | bfd_boolean | |
0aa7f586 AM |
6157 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
6158 | bfd *abfd ATTRIBUTE_UNUSED, | |
6159 | asection *current_section, | |
6160 | asection *previous_section, | |
2889e75b NC |
6161 | bfd_boolean new_segment) |
6162 | { | |
0aa7f586 AM |
6163 | lang_output_section_statement_type *cur; |
6164 | lang_output_section_statement_type *prev; | |
591a748a NC |
6165 | |
6166 | /* The checks below are only necessary when the BFD library has decided | |
6167 | that the two sections ought to be placed into the same segment. */ | |
2889e75b NC |
6168 | if (new_segment) |
6169 | return TRUE; | |
6170 | ||
6171 | /* Paranoia checks. */ | |
6172 | if (current_section == NULL || previous_section == NULL) | |
6173 | return new_segment; | |
6174 | ||
4724d37e RM |
6175 | /* If this flag is set, the target never wants code and non-code |
6176 | sections comingled in the same segment. */ | |
6177 | if (config.separate_code | |
6178 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
6179 | return TRUE; | |
6180 | ||
2889e75b NC |
6181 | /* Find the memory regions associated with the two sections. |
6182 | We call lang_output_section_find() here rather than scanning the list | |
6183 | of output sections looking for a matching section pointer because if | |
591a748a | 6184 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6185 | cur = lang_output_section_find (current_section->name); |
6186 | prev = lang_output_section_find (previous_section->name); | |
6187 | ||
591a748a | 6188 | /* More paranoia. */ |
2889e75b NC |
6189 | if (cur == NULL || prev == NULL) |
6190 | return new_segment; | |
6191 | ||
6192 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6193 | different segments. See the email thread starting at the following |
6194 | URL for the reasons why this is necessary: | |
2889e75b NC |
6195 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6196 | return cur->region != prev->region; | |
6197 | } | |
6198 | ||
e9ee469a AM |
6199 | void |
6200 | one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions) | |
2d20f7bf | 6201 | { |
420e579c | 6202 | lang_statement_iteration++; |
baf09cba AM |
6203 | if (expld.phase != lang_mark_phase_enum) |
6204 | lang_sizing_iteration++; | |
e535e147 AM |
6205 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6206 | 0, 0, relax, check_regions); | |
e9ee469a | 6207 | } |
420e579c | 6208 | |
cba6246d L |
6209 | static bfd_boolean |
6210 | lang_size_segment (seg_align_type *seg) | |
e9ee469a | 6211 | { |
cba6246d L |
6212 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6213 | a page could be saved in the data segment. */ | |
6214 | bfd_vma first, last; | |
6215 | ||
6216 | first = -seg->base & (seg->pagesize - 1); | |
6217 | last = seg->end & (seg->pagesize - 1); | |
6218 | if (first && last | |
6219 | && ((seg->base & ~(seg->pagesize - 1)) | |
6220 | != (seg->end & ~(seg->pagesize - 1))) | |
6221 | && first + last <= seg->pagesize) | |
8c37241b | 6222 | { |
cba6246d L |
6223 | seg->phase = exp_seg_adjust; |
6224 | return TRUE; | |
6225 | } | |
6226 | ||
6227 | seg->phase = exp_seg_done; | |
6228 | return FALSE; | |
6229 | } | |
6230 | ||
6231 | static bfd_vma | |
6232 | lang_size_relro_segment_1 (seg_align_type *seg) | |
6233 | { | |
6234 | bfd_vma relro_end, desired_end; | |
6235 | asection *sec; | |
8c37241b | 6236 | |
cba6246d L |
6237 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
6238 | relro_end = ((seg->relro_end + seg->pagesize - 1) | |
6239 | & ~(seg->pagesize - 1)); | |
0e5fabeb | 6240 | |
cba6246d L |
6241 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
6242 | desired_end = relro_end - seg->relro_offset; | |
0e5fabeb | 6243 | |
cba6246d L |
6244 | /* For sections in the relro segment.. */ |
6245 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) | |
6246 | if ((sec->flags & SEC_ALLOC) != 0 | |
6247 | && sec->vma >= seg->base | |
6248 | && sec->vma < seg->relro_end - seg->relro_offset) | |
6249 | { | |
6250 | /* Where do we want to put this section so that it ends as | |
6251 | desired? */ | |
6252 | bfd_vma start, end, bump; | |
6253 | ||
6254 | end = start = sec->vma; | |
6255 | if (!IS_TBSS (sec)) | |
6256 | end += TO_ADDR (sec->size); | |
6257 | bump = desired_end - end; | |
6258 | /* We'd like to increase START by BUMP, but we must heed | |
6259 | alignment so the increase might be less than optimum. */ | |
6260 | start += bump; | |
6261 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6262 | /* This is now the desired end for the previous section. */ | |
6263 | desired_end = start; | |
6264 | } | |
6265 | ||
6266 | seg->phase = exp_seg_relro_adjust; | |
6267 | ASSERT (desired_end >= seg->base); | |
6268 | seg->base = desired_end; | |
6269 | return relro_end; | |
6270 | } | |
6271 | ||
6272 | static bfd_boolean | |
6273 | lang_size_relro_segment (bfd_boolean *relax, bfd_boolean check_regions) | |
6274 | { | |
6275 | bfd_boolean do_reset = FALSE; | |
6276 | bfd_boolean do_data_relro; | |
6277 | bfd_vma data_initial_base, data_relro_end; | |
6278 | ||
6279 | if (link_info.relro && expld.dataseg.relro_end) | |
6280 | { | |
6281 | do_data_relro = TRUE; | |
6282 | data_initial_base = expld.dataseg.base; | |
6283 | data_relro_end = lang_size_relro_segment_1 (&expld.dataseg); | |
6284 | } | |
6285 | else | |
6286 | { | |
6287 | do_data_relro = FALSE; | |
6288 | data_initial_base = data_relro_end = 0; | |
6289 | } | |
0e5fabeb | 6290 | |
cba6246d L |
6291 | if (do_data_relro) |
6292 | { | |
f86a8756 | 6293 | lang_reset_memory_regions (); |
e9ee469a | 6294 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6295 | |
cba6246d L |
6296 | /* Assignments to dot, or to output section address in a user |
6297 | script have increased padding over the original. Revert. */ | |
6298 | if (do_data_relro && expld.dataseg.relro_end > data_relro_end) | |
a4f5ad88 | 6299 | { |
cba6246d L |
6300 | expld.dataseg.base = data_initial_base;; |
6301 | do_reset = TRUE; | |
a4f5ad88 | 6302 | } |
8c37241b | 6303 | } |
cba6246d L |
6304 | |
6305 | if (!do_data_relro && lang_size_segment (&expld.dataseg)) | |
6306 | do_reset = TRUE; | |
6307 | ||
6308 | return do_reset; | |
6309 | } | |
6310 | ||
6311 | void | |
6312 | lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions) | |
6313 | { | |
6314 | expld.phase = lang_allocating_phase_enum; | |
6315 | expld.dataseg.phase = exp_seg_none; | |
6316 | ||
6317 | one_lang_size_sections_pass (relax, check_regions); | |
6318 | ||
6319 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6320 | expld.dataseg.phase = exp_seg_done; | |
6321 | ||
6322 | if (expld.dataseg.phase == exp_seg_end_seen) | |
2d20f7bf | 6323 | { |
cba6246d L |
6324 | bfd_boolean do_reset |
6325 | = lang_size_relro_segment (relax, check_regions); | |
6326 | ||
6327 | if (do_reset) | |
2d20f7bf | 6328 | { |
f86a8756 | 6329 | lang_reset_memory_regions (); |
e9ee469a | 6330 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6331 | } |
cba6246d L |
6332 | |
6333 | if (link_info.relro && expld.dataseg.relro_end) | |
6334 | { | |
6335 | link_info.relro_start = expld.dataseg.base; | |
6336 | link_info.relro_end = expld.dataseg.relro_end; | |
6337 | } | |
2d20f7bf | 6338 | } |
2d20f7bf JJ |
6339 | } |
6340 | ||
d2667025 AM |
6341 | static lang_output_section_statement_type *current_section; |
6342 | static lang_assignment_statement_type *current_assign; | |
6343 | static bfd_boolean prefer_next_section; | |
6344 | ||
420e579c HPN |
6345 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6346 | ||
6347 | static bfd_vma | |
66e28d60 AM |
6348 | lang_do_assignments_1 (lang_statement_union_type *s, |
6349 | lang_output_section_statement_type *current_os, | |
6350 | fill_type *fill, | |
d2667025 AM |
6351 | bfd_vma dot, |
6352 | bfd_boolean *found_end) | |
252b5132 | 6353 | { |
1579bae1 | 6354 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6355 | { |
6356 | switch (s->header.type) | |
6357 | { | |
6358 | case lang_constructors_statement_enum: | |
420e579c | 6359 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6360 | current_os, fill, dot, found_end); |
252b5132 RH |
6361 | break; |
6362 | ||
6363 | case lang_output_section_statement_enum: | |
6364 | { | |
d1778b88 | 6365 | lang_output_section_statement_type *os; |
f02cb058 | 6366 | bfd_vma newdot; |
252b5132 | 6367 | |
d1778b88 | 6368 | os = &(s->output_section_statement); |
d2667025 | 6369 | os->after_end = *found_end; |
61826503 | 6370 | init_opb (os->bfd_section); |
75ff4589 | 6371 | if (os->bfd_section != NULL && !os->ignored) |
252b5132 | 6372 | { |
d2667025 AM |
6373 | if ((os->bfd_section->flags & SEC_ALLOC) != 0) |
6374 | { | |
6375 | current_section = os; | |
6376 | prefer_next_section = FALSE; | |
6377 | } | |
252b5132 | 6378 | dot = os->bfd_section->vma; |
f02cb058 AM |
6379 | } |
6380 | newdot = lang_do_assignments_1 (os->children.head, | |
6381 | os, os->fill, dot, found_end); | |
6382 | if (!os->ignored) | |
6383 | { | |
6384 | if (os->bfd_section != NULL) | |
6385 | { | |
6386 | /* .tbss sections effectively have zero size. */ | |
6387 | if (!IS_TBSS (os->bfd_section) | |
6388 | || bfd_link_relocatable (&link_info)) | |
6389 | dot += TO_ADDR (os->bfd_section->size); | |
6390 | ||
6391 | if (os->update_dot_tree != NULL) | |
6392 | exp_fold_tree (os->update_dot_tree, | |
6393 | bfd_abs_section_ptr, &dot); | |
6394 | } | |
6395 | else | |
6396 | dot = newdot; | |
252b5132 RH |
6397 | } |
6398 | } | |
6399 | break; | |
e9ee469a | 6400 | |
252b5132 RH |
6401 | case lang_wild_statement_enum: |
6402 | ||
420e579c | 6403 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6404 | current_os, fill, dot, found_end); |
252b5132 RH |
6405 | break; |
6406 | ||
6407 | case lang_object_symbols_statement_enum: | |
6408 | case lang_output_statement_enum: | |
6409 | case lang_target_statement_enum: | |
252b5132 | 6410 | break; |
e9ee469a | 6411 | |
252b5132 | 6412 | case lang_data_statement_enum: |
e9ee469a AM |
6413 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
6414 | if (expld.result.valid_p) | |
7542af2a AM |
6415 | { |
6416 | s->data_statement.value = expld.result.value; | |
6417 | if (expld.result.section != NULL) | |
6418 | s->data_statement.value += expld.result.section->vma; | |
6419 | } | |
f02cb058 | 6420 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6421 | einfo (_("%F%P: invalid data statement\n")); |
b7a26f91 KH |
6422 | { |
6423 | unsigned int size; | |
08da4cac KH |
6424 | switch (s->data_statement.type) |
6425 | { | |
6426 | default: | |
6427 | abort (); | |
6428 | case QUAD: | |
6429 | case SQUAD: | |
6430 | size = QUAD_SIZE; | |
6431 | break; | |
6432 | case LONG: | |
6433 | size = LONG_SIZE; | |
6434 | break; | |
6435 | case SHORT: | |
6436 | size = SHORT_SIZE; | |
6437 | break; | |
6438 | case BYTE: | |
6439 | size = BYTE_SIZE; | |
6440 | break; | |
6441 | } | |
e5caa5e0 AM |
6442 | if (size < TO_SIZE ((unsigned) 1)) |
6443 | size = TO_SIZE ((unsigned) 1); | |
6444 | dot += TO_ADDR (size); | |
08da4cac | 6445 | } |
252b5132 RH |
6446 | break; |
6447 | ||
6448 | case lang_reloc_statement_enum: | |
e9ee469a AM |
6449 | exp_fold_tree (s->reloc_statement.addend_exp, |
6450 | bfd_abs_section_ptr, &dot); | |
6451 | if (expld.result.valid_p) | |
6452 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6453 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6454 | einfo (_("%F%P: invalid reloc statement\n")); |
e5caa5e0 | 6455 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6456 | break; |
6457 | ||
6458 | case lang_input_section_enum: | |
6459 | { | |
6460 | asection *in = s->input_section.section; | |
6461 | ||
57ceae94 | 6462 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6463 | dot += TO_ADDR (in->size); |
252b5132 RH |
6464 | } |
6465 | break; | |
6466 | ||
6467 | case lang_input_statement_enum: | |
6468 | break; | |
e9ee469a | 6469 | |
252b5132 RH |
6470 | case lang_fill_statement_enum: |
6471 | fill = s->fill_statement.fill; | |
6472 | break; | |
252b5132 | 6473 | |
e9ee469a | 6474 | case lang_assignment_statement_enum: |
d2667025 AM |
6475 | current_assign = &s->assignment_statement; |
6476 | if (current_assign->exp->type.node_class != etree_assert) | |
6477 | { | |
6478 | const char *p = current_assign->exp->assign.dst; | |
6479 | ||
6480 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
6481 | prefer_next_section = TRUE; | |
6482 | ||
6483 | while (*p == '_') | |
6484 | ++p; | |
6485 | if (strcmp (p, "end") == 0) | |
6486 | *found_end = TRUE; | |
6487 | } | |
e9ee469a | 6488 | exp_fold_tree (s->assignment_statement.exp, |
f02cb058 AM |
6489 | (current_os->bfd_section != NULL |
6490 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6491 | &dot); |
252b5132 | 6492 | break; |
e9ee469a | 6493 | |
252b5132 | 6494 | case lang_padding_statement_enum: |
e5caa5e0 | 6495 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6496 | break; |
6497 | ||
6498 | case lang_group_statement_enum: | |
420e579c | 6499 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6500 | current_os, fill, dot, found_end); |
252b5132 RH |
6501 | break; |
6502 | ||
53d25da6 | 6503 | case lang_insert_statement_enum: |
252b5132 | 6504 | break; |
e9ee469a | 6505 | |
252b5132 RH |
6506 | case lang_address_statement_enum: |
6507 | break; | |
53d25da6 AM |
6508 | |
6509 | default: | |
6510 | FAIL (); | |
6511 | break; | |
252b5132 | 6512 | } |
252b5132 RH |
6513 | } |
6514 | return dot; | |
6515 | } | |
6516 | ||
f2241121 | 6517 | void |
2f65ac72 | 6518 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6519 | { |
d2667025 AM |
6520 | bfd_boolean found_end = FALSE; |
6521 | ||
6522 | current_section = NULL; | |
6523 | prefer_next_section = FALSE; | |
2f65ac72 | 6524 | expld.phase = phase; |
420e579c | 6525 | lang_statement_iteration++; |
d2667025 AM |
6526 | lang_do_assignments_1 (statement_list.head, |
6527 | abs_output_section, NULL, 0, &found_end); | |
6528 | } | |
6529 | ||
6530 | /* For an assignment statement outside of an output section statement, | |
6531 | choose the best of neighbouring output sections to use for values | |
6532 | of "dot". */ | |
6533 | ||
6534 | asection * | |
6535 | section_for_dot (void) | |
6536 | { | |
6537 | asection *s; | |
6538 | ||
6539 | /* Assignments belong to the previous output section, unless there | |
6540 | has been an assignment to "dot", in which case following | |
6541 | assignments belong to the next output section. (The assumption | |
6542 | is that an assignment to "dot" is setting up the address for the | |
6543 | next output section.) Except that past the assignment to "_end" | |
6544 | we always associate with the previous section. This exception is | |
6545 | for targets like SH that define an alloc .stack or other | |
6546 | weirdness after non-alloc sections. */ | |
6547 | if (current_section == NULL || prefer_next_section) | |
6548 | { | |
6549 | lang_statement_union_type *stmt; | |
6550 | lang_output_section_statement_type *os; | |
6551 | ||
6552 | for (stmt = (lang_statement_union_type *) current_assign; | |
6553 | stmt != NULL; | |
6554 | stmt = stmt->header.next) | |
6555 | if (stmt->header.type == lang_output_section_statement_enum) | |
6556 | break; | |
6557 | ||
6558 | os = &stmt->output_section_statement; | |
6559 | while (os != NULL | |
6560 | && !os->after_end | |
6561 | && (os->bfd_section == NULL | |
6562 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6563 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6564 | os->bfd_section))) | |
6565 | os = os->next; | |
6566 | ||
6567 | if (current_section == NULL || os == NULL || !os->after_end) | |
6568 | { | |
6569 | if (os != NULL) | |
6570 | s = os->bfd_section; | |
6571 | else | |
6572 | s = link_info.output_bfd->section_last; | |
6573 | while (s != NULL | |
6574 | && ((s->flags & SEC_ALLOC) == 0 | |
6575 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6576 | s = s->prev; | |
6577 | if (s != NULL) | |
6578 | return s; | |
6579 | ||
6580 | return bfd_abs_section_ptr; | |
6581 | } | |
6582 | } | |
6583 | ||
6584 | s = current_section->bfd_section; | |
6585 | ||
6586 | /* The section may have been stripped. */ | |
6587 | while (s != NULL | |
6588 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6589 | || (s->flags & SEC_ALLOC) == 0 | |
6590 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6591 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6592 | s = s->prev; | |
6593 | if (s == NULL) | |
6594 | s = link_info.output_bfd->sections; | |
6595 | while (s != NULL | |
6596 | && ((s->flags & SEC_ALLOC) == 0 | |
6597 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6598 | s = s->next; | |
6599 | if (s != NULL) | |
6600 | return s; | |
6601 | ||
6602 | return bfd_abs_section_ptr; | |
420e579c HPN |
6603 | } |
6604 | ||
7dba9362 AM |
6605 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ |
6606 | ||
6607 | static struct bfd_link_hash_entry **start_stop_syms; | |
6608 | static size_t start_stop_count = 0; | |
6609 | static size_t start_stop_alloc = 0; | |
6610 | ||
6611 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6612 | to start_stop_syms. */ | |
6613 | ||
6614 | static void | |
6615 | lang_define_start_stop (const char *symbol, asection *sec) | |
6616 | { | |
6617 | struct bfd_link_hash_entry *h; | |
6618 | ||
6619 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6620 | if (h != NULL) | |
6621 | { | |
6622 | if (start_stop_count == start_stop_alloc) | |
6623 | { | |
6624 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6625 | start_stop_syms | |
6626 | = xrealloc (start_stop_syms, | |
6627 | start_stop_alloc * sizeof (*start_stop_syms)); | |
6628 | } | |
6629 | start_stop_syms[start_stop_count++] = h; | |
6630 | } | |
6631 | } | |
6632 | ||
6633 | /* Check for input sections whose names match references to | |
6634 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
6635 | preliminary definitions. */ | |
252b5132 RH |
6636 | |
6637 | static void | |
7dba9362 | 6638 | lang_init_start_stop (void) |
252b5132 | 6639 | { |
7dba9362 | 6640 | bfd *abfd; |
252b5132 | 6641 | asection *s; |
7dba9362 AM |
6642 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); |
6643 | ||
6644 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
6645 | for (s = abfd->sections; s != NULL; s = s->next) | |
6646 | { | |
6647 | const char *ps; | |
6648 | const char *secname = s->name; | |
6649 | ||
6650 | for (ps = secname; *ps != '\0'; ps++) | |
6651 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
6652 | break; | |
6653 | if (*ps == '\0') | |
6654 | { | |
6655 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
b27685f2 | 6656 | |
7dba9362 AM |
6657 | symbol[0] = leading_char; |
6658 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
6659 | lang_define_start_stop (symbol, s); | |
6660 | ||
6661 | symbol[1] = leading_char; | |
6662 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
6663 | lang_define_start_stop (symbol + 1, s); | |
6664 | ||
6665 | free (symbol); | |
6666 | } | |
6667 | } | |
6668 | } | |
6669 | ||
6670 | /* Iterate over start_stop_syms. */ | |
6671 | ||
6672 | static void | |
6673 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
6674 | { | |
6675 | size_t i; | |
6676 | ||
6677 | for (i = 0; i < start_stop_count; ++i) | |
6678 | func (start_stop_syms[i]); | |
6679 | } | |
6680 | ||
6681 | /* __start and __stop symbols are only supposed to be defined by the | |
6682 | linker for orphan sections, but we now extend that to sections that | |
6683 | map to an output section of the same name. The symbols were | |
6684 | defined early for --gc-sections, before we mapped input to output | |
6685 | sections, so undo those that don't satisfy this rule. */ | |
6686 | ||
6687 | static void | |
6688 | undef_start_stop (struct bfd_link_hash_entry *h) | |
6689 | { | |
6690 | if (h->ldscript_def) | |
b27685f2 L |
6691 | return; |
6692 | ||
7dba9362 AM |
6693 | if (h->u.def.section->output_section == NULL |
6694 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
6695 | || strcmp (h->u.def.section->name, | |
6696 | h->u.def.section->output_section->name) != 0) | |
6697 | { | |
4d1c6335 L |
6698 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, |
6699 | h->u.def.section->name); | |
6700 | if (sec != NULL) | |
6701 | { | |
6702 | /* When there are more than one input sections with the same | |
6703 | section name, SECNAME, linker picks the first one to define | |
6704 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
6705 | input section is removed by comdat group, we need to check | |
6706 | if there is still an output section with section name | |
6707 | SECNAME. */ | |
6708 | asection *i; | |
6709 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
6710 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
6711 | { | |
6712 | h->u.def.section = i; | |
6713 | return; | |
6714 | } | |
6715 | } | |
7dba9362 AM |
6716 | h->type = bfd_link_hash_undefined; |
6717 | h->u.undef.abfd = NULL; | |
6718 | } | |
6719 | } | |
252b5132 | 6720 | |
7dba9362 AM |
6721 | static void |
6722 | lang_undef_start_stop (void) | |
6723 | { | |
6724 | foreach_start_stop (undef_start_stop); | |
6725 | } | |
6726 | ||
6727 | /* Check for output sections whose names match references to | |
6728 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
6729 | preliminary definitions. */ | |
6730 | ||
6731 | static void | |
6732 | lang_init_startof_sizeof (void) | |
6733 | { | |
6734 | asection *s; | |
252b5132 | 6735 | |
f13a99db | 6736 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6737 | { |
7dba9362 AM |
6738 | const char *secname = s->name; |
6739 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 6740 | |
7dba9362 AM |
6741 | sprintf (symbol, ".startof.%s", secname); |
6742 | lang_define_start_stop (symbol, s); | |
252b5132 | 6743 | |
7dba9362 AM |
6744 | memcpy (symbol + 1, ".size", 5); |
6745 | lang_define_start_stop (symbol + 1, s); | |
6746 | free (symbol); | |
6747 | } | |
6748 | } | |
cbd0eecf | 6749 | |
7dba9362 AM |
6750 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ |
6751 | ||
6752 | static void | |
6753 | set_start_stop (struct bfd_link_hash_entry *h) | |
6754 | { | |
6755 | if (h->ldscript_def | |
6756 | || h->type != bfd_link_hash_defined) | |
6757 | return; | |
cbd0eecf | 6758 | |
7dba9362 AM |
6759 | if (h->root.string[0] == '.') |
6760 | { | |
6761 | /* .startof. or .sizeof. symbol. | |
6762 | .startof. already has final value. */ | |
6763 | if (h->root.string[2] == 'i') | |
252b5132 | 6764 | { |
7dba9362 AM |
6765 | /* .sizeof. */ |
6766 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
6767 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 6768 | } |
7dba9362 AM |
6769 | } |
6770 | else | |
6771 | { | |
6772 | /* __start or __stop symbol. */ | |
6773 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 6774 | |
7dba9362 AM |
6775 | h->u.def.section = h->u.def.section->output_section; |
6776 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 6777 | { |
7dba9362 AM |
6778 | /* __stop_ */ |
6779 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 6780 | } |
252b5132 RH |
6781 | } |
6782 | } | |
6783 | ||
7dba9362 AM |
6784 | static void |
6785 | lang_finalize_start_stop (void) | |
6786 | { | |
6787 | foreach_start_stop (set_start_stop); | |
6788 | } | |
6789 | ||
252b5132 | 6790 | static void |
750877ba | 6791 | lang_end (void) |
252b5132 RH |
6792 | { |
6793 | struct bfd_link_hash_entry *h; | |
b34976b6 | 6794 | bfd_boolean warn; |
252b5132 | 6795 | |
0e1862bb L |
6796 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
6797 | || bfd_link_dll (&link_info)) | |
71934f94 | 6798 | warn = entry_from_cmdline; |
252b5132 | 6799 | else |
b34976b6 | 6800 | warn = TRUE; |
252b5132 | 6801 | |
ac69cbc6 | 6802 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
6803 | --gc-sections, unless --gc-keep-exported was also given. */ |
6804 | if (bfd_link_relocatable (&link_info) | |
6805 | && link_info.gc_sections | |
6806 | && !link_info.gc_keep_exported | |
24898b70 | 6807 | && !(entry_from_cmdline || undef_from_cmdline)) |
df5f2391 | 6808 | einfo (_("%F%P: gc-sections requires either an entry or " |
ac69cbc6 TG |
6809 | "an undefined symbol\n")); |
6810 | ||
1579bae1 | 6811 | if (entry_symbol.name == NULL) |
252b5132 | 6812 | { |
a359509e ZW |
6813 | /* No entry has been specified. Look for the default entry, but |
6814 | don't warn if we don't find it. */ | |
6815 | entry_symbol.name = entry_symbol_default; | |
b34976b6 | 6816 | warn = FALSE; |
252b5132 RH |
6817 | } |
6818 | ||
e3e942e9 | 6819 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
b34976b6 | 6820 | FALSE, FALSE, TRUE); |
1579bae1 | 6821 | if (h != NULL |
252b5132 RH |
6822 | && (h->type == bfd_link_hash_defined |
6823 | || h->type == bfd_link_hash_defweak) | |
6824 | && h->u.def.section->output_section != NULL) | |
6825 | { | |
6826 | bfd_vma val; | |
6827 | ||
6828 | val = (h->u.def.value | |
fd361982 | 6829 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 6830 | + h->u.def.section->output_offset); |
0aa7f586 | 6831 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6832 | einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
6833 | } |
6834 | else | |
6835 | { | |
6836 | bfd_vma val; | |
5f992e62 | 6837 | const char *send; |
252b5132 RH |
6838 | |
6839 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 6840 | number. */ |
e3e942e9 | 6841 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
6842 | if (*send == '\0') |
6843 | { | |
0aa7f586 | 6844 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6845 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
6846 | } |
6847 | else | |
6848 | { | |
6849 | asection *ts; | |
6850 | ||
6851 | /* Can't find the entry symbol, and it's not a number. Use | |
6852 | the first address in the text section. */ | |
f13a99db | 6853 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 6854 | if (ts != NULL) |
252b5132 RH |
6855 | { |
6856 | if (warn) | |
6feb9908 AM |
6857 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6858 | " defaulting to %V\n"), | |
e3e942e9 | 6859 | entry_symbol.name, |
fd361982 AM |
6860 | bfd_section_vma (ts)); |
6861 | if (!bfd_set_start_address (link_info.output_bfd, | |
6862 | bfd_section_vma (ts))) | |
df5f2391 | 6863 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
6864 | } |
6865 | else | |
6866 | { | |
6867 | if (warn) | |
6feb9908 AM |
6868 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6869 | " not setting start address\n"), | |
e3e942e9 | 6870 | entry_symbol.name); |
252b5132 RH |
6871 | } |
6872 | } | |
6873 | } | |
6874 | } | |
6875 | ||
6876 | /* This is a small function used when we want to ignore errors from | |
6877 | BFD. */ | |
6878 | ||
6879 | static void | |
52d45da3 AM |
6880 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
6881 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
6882 | { |
6883 | /* Don't do anything. */ | |
6884 | } | |
6885 | ||
6886 | /* Check that the architecture of all the input files is compatible | |
6887 | with the output file. Also call the backend to let it do any | |
6888 | other checking that is needed. */ | |
6889 | ||
6890 | static void | |
1579bae1 | 6891 | lang_check (void) |
252b5132 | 6892 | { |
36983a93 | 6893 | lang_input_statement_type *file; |
252b5132 | 6894 | bfd *input_bfd; |
5f992e62 | 6895 | const bfd_arch_info_type *compatible; |
252b5132 | 6896 | |
8ce18f9c | 6897 | for (file = (void *) file_chain.head; |
36983a93 AM |
6898 | file != NULL; |
6899 | file = file->next) | |
252b5132 | 6900 | { |
422b6f14 DK |
6901 | #ifdef ENABLE_PLUGINS |
6902 | /* Don't check format of files claimed by plugin. */ | |
36983a93 | 6903 | if (file->flags.claimed) |
422b6f14 DK |
6904 | continue; |
6905 | #endif /* ENABLE_PLUGINS */ | |
36983a93 | 6906 | input_bfd = file->the_bfd; |
6feb9908 | 6907 | compatible |
f13a99db | 6908 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 6909 | command_line.accept_unknown_input_arch); |
30cba025 AM |
6910 | |
6911 | /* In general it is not possible to perform a relocatable | |
6912 | link between differing object formats when the input | |
6913 | file has relocations, because the relocations in the | |
6914 | input format may not have equivalent representations in | |
6915 | the output format (and besides BFD does not translate | |
6916 | relocs for other link purposes than a final link). */ | |
0e1862bb L |
6917 | if ((bfd_link_relocatable (&link_info) |
6918 | || link_info.emitrelocations) | |
30cba025 | 6919 | && (compatible == NULL |
f13a99db AM |
6920 | || (bfd_get_flavour (input_bfd) |
6921 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
6922 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
6923 | { | |
df5f2391 | 6924 | einfo (_("%F%P: relocatable linking with relocations from" |
871b3ab2 | 6925 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 6926 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 6927 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
6928 | /* einfo with %F exits. */ |
6929 | } | |
6930 | ||
252b5132 RH |
6931 | if (compatible == NULL) |
6932 | { | |
6933 | if (command_line.warn_mismatch) | |
df5f2391 | 6934 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 6935 | " is incompatible with %s output\n"), |
252b5132 | 6936 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 6937 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 6938 | } |
b9247304 | 6939 | |
a8acd6ee AM |
6940 | /* If the input bfd has no contents, it shouldn't set the |
6941 | private data of the output bfd. */ | |
6942 | else if ((input_bfd->flags & DYNAMIC) != 0 | |
6943 | || bfd_count_sections (input_bfd) != 0) | |
6944 | { | |
252b5132 RH |
6945 | bfd_error_handler_type pfn = NULL; |
6946 | ||
6947 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
6948 | files, temporarily set the BFD error handler to a |
6949 | function which will do nothing. We still want to call | |
6950 | bfd_merge_private_bfd_data, since it may set up | |
6951 | information which is needed in the output file. */ | |
0aa7f586 | 6952 | if (!command_line.warn_mismatch) |
252b5132 | 6953 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 6954 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
6955 | { |
6956 | if (command_line.warn_mismatch) | |
df5f2391 | 6957 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 6958 | " of file %pB\n"), input_bfd); |
252b5132 | 6959 | } |
0aa7f586 | 6960 | if (!command_line.warn_mismatch) |
252b5132 RH |
6961 | bfd_set_error_handler (pfn); |
6962 | } | |
6963 | } | |
6964 | } | |
6965 | ||
6966 | /* Look through all the global common symbols and attach them to the | |
6967 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 6968 | to roughly sort the entries by alignment. */ |
252b5132 RH |
6969 | |
6970 | static void | |
1579bae1 | 6971 | lang_common (void) |
252b5132 | 6972 | { |
a4819f54 | 6973 | if (link_info.inhibit_common_definition) |
4818e05f | 6974 | return; |
0e1862bb | 6975 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 6976 | && !command_line.force_common_definition) |
252b5132 RH |
6977 | return; |
6978 | ||
0aa7f586 | 6979 | if (!config.sort_common) |
1579bae1 | 6980 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
6981 | else |
6982 | { | |
de7dd2bd NC |
6983 | unsigned int power; |
6984 | ||
6985 | if (config.sort_common == sort_descending) | |
967928e9 AM |
6986 | { |
6987 | for (power = 4; power > 0; power--) | |
6988 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
6989 | ||
6990 | power = 0; | |
6991 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
6992 | } | |
de7dd2bd | 6993 | else |
967928e9 AM |
6994 | { |
6995 | for (power = 0; power <= 4; power++) | |
6996 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 6997 | |
2dc0e7b4 | 6998 | power = (unsigned int) -1; |
de7dd2bd NC |
6999 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
7000 | } | |
252b5132 RH |
7001 | } |
7002 | } | |
7003 | ||
7004 | /* Place one common symbol in the correct section. */ | |
7005 | ||
b34976b6 | 7006 | static bfd_boolean |
1579bae1 | 7007 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
7008 | { |
7009 | unsigned int power_of_two; | |
7010 | bfd_vma size; | |
7011 | asection *section; | |
7012 | ||
7013 | if (h->type != bfd_link_hash_common) | |
b34976b6 | 7014 | return TRUE; |
252b5132 RH |
7015 | |
7016 | size = h->u.c.size; | |
7017 | power_of_two = h->u.c.p->alignment_power; | |
7018 | ||
de7dd2bd NC |
7019 | if (config.sort_common == sort_descending |
7020 | && power_of_two < *(unsigned int *) info) | |
7021 | return TRUE; | |
7022 | else if (config.sort_common == sort_ascending | |
967928e9 | 7023 | && power_of_two > *(unsigned int *) info) |
b34976b6 | 7024 | return TRUE; |
252b5132 RH |
7025 | |
7026 | section = h->u.c.p->section; | |
3023e3f6 | 7027 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
df5f2391 | 7028 | einfo (_("%F%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 7029 | h->root.string); |
252b5132 RH |
7030 | |
7031 | if (config.map_file != NULL) | |
7032 | { | |
b34976b6 | 7033 | static bfd_boolean header_printed; |
252b5132 RH |
7034 | int len; |
7035 | char *name; | |
7036 | char buf[50]; | |
7037 | ||
0aa7f586 | 7038 | if (!header_printed) |
252b5132 RH |
7039 | { |
7040 | minfo (_("\nAllocating common symbols\n")); | |
7041 | minfo (_("Common symbol size file\n\n")); | |
b34976b6 | 7042 | header_printed = TRUE; |
252b5132 RH |
7043 | } |
7044 | ||
f13a99db | 7045 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 7046 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
7047 | if (name == NULL) |
7048 | { | |
7049 | minfo ("%s", h->root.string); | |
7050 | len = strlen (h->root.string); | |
7051 | } | |
7052 | else | |
7053 | { | |
7054 | minfo ("%s", name); | |
7055 | len = strlen (name); | |
7056 | free (name); | |
7057 | } | |
252b5132 RH |
7058 | |
7059 | if (len >= 19) | |
7060 | { | |
7061 | print_nl (); | |
7062 | len = 0; | |
7063 | } | |
7064 | while (len < 20) | |
7065 | { | |
7066 | print_space (); | |
7067 | ++len; | |
7068 | } | |
7069 | ||
7070 | minfo ("0x"); | |
7071 | if (size <= 0xffffffff) | |
7072 | sprintf (buf, "%lx", (unsigned long) size); | |
7073 | else | |
7074 | sprintf_vma (buf, size); | |
7075 | minfo ("%s", buf); | |
7076 | len = strlen (buf); | |
7077 | ||
7078 | while (len < 16) | |
7079 | { | |
7080 | print_space (); | |
7081 | ++len; | |
7082 | } | |
7083 | ||
871b3ab2 | 7084 | minfo ("%pB\n", section->owner); |
252b5132 RH |
7085 | } |
7086 | ||
b34976b6 | 7087 | return TRUE; |
252b5132 RH |
7088 | } |
7089 | ||
c005eb9e AB |
7090 | /* Handle a single orphan section S, placing the orphan into an appropriate |
7091 | output section. The effects of the --orphan-handling command line | |
7092 | option are handled here. */ | |
7093 | ||
7094 | static void | |
7095 | ldlang_place_orphan (asection *s) | |
7096 | { | |
7097 | if (config.orphan_handling == orphan_handling_discard) | |
7098 | { | |
7099 | lang_output_section_statement_type *os; | |
7100 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, | |
7101 | TRUE); | |
7102 | if (os->addr_tree == NULL | |
7103 | && (bfd_link_relocatable (&link_info) | |
7104 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7105 | os->addr_tree = exp_intop (0); | |
7106 | lang_add_section (&os->children, s, NULL, os); | |
7107 | } | |
7108 | else | |
7109 | { | |
7110 | lang_output_section_statement_type *os; | |
7111 | const char *name = s->name; | |
7112 | int constraint = 0; | |
7113 | ||
7114 | if (config.orphan_handling == orphan_handling_error) | |
df5f2391 | 7115 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
7116 | s, s->owner); |
7117 | ||
7118 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
7119 | constraint = SPECIAL; | |
7120 | ||
7121 | os = ldemul_place_orphan (s, name, constraint); | |
7122 | if (os == NULL) | |
7123 | { | |
7124 | os = lang_output_section_statement_lookup (name, constraint, TRUE); | |
7125 | if (os->addr_tree == NULL | |
7126 | && (bfd_link_relocatable (&link_info) | |
7127 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7128 | os->addr_tree = exp_intop (0); | |
7129 | lang_add_section (&os->children, s, NULL, os); | |
7130 | } | |
7131 | ||
7132 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 7133 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 7134 | "placed in section `%s'\n"), |
c005eb9e AB |
7135 | s, s->owner, os->name); |
7136 | } | |
7137 | } | |
7138 | ||
08da4cac KH |
7139 | /* Run through the input files and ensure that every input section has |
7140 | somewhere to go. If one is found without a destination then create | |
7141 | an input request and place it into the statement tree. */ | |
252b5132 RH |
7142 | |
7143 | static void | |
1579bae1 | 7144 | lang_place_orphans (void) |
252b5132 | 7145 | { |
e50d8076 | 7146 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
7147 | { |
7148 | asection *s; | |
7149 | ||
1579bae1 | 7150 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7151 | { |
1579bae1 | 7152 | if (s->output_section == NULL) |
252b5132 | 7153 | { |
396a2467 | 7154 | /* This section of the file is not attached, root |
afd7a018 | 7155 | around for a sensible place for it to go. */ |
252b5132 | 7156 | |
66be1055 | 7157 | if (file->flags.just_syms) |
1449d79b | 7158 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 7159 | else if (lang_discard_section_p (s)) |
164e712d | 7160 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
7161 | else if (strcmp (s->name, "COMMON") == 0) |
7162 | { | |
7163 | /* This is a lonely common section which must have | |
7164 | come from an archive. We attach to the section | |
7165 | with the wildcard. */ | |
0e1862bb | 7166 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
7167 | || command_line.force_common_definition) |
7168 | { | |
7169 | if (default_common_section == NULL) | |
66c103b7 AM |
7170 | default_common_section |
7171 | = lang_output_section_statement_lookup (".bss", 0, | |
7172 | TRUE); | |
39dcfe18 | 7173 | lang_add_section (&default_common_section->children, s, |
b9c361e0 | 7174 | NULL, default_common_section); |
252b5132 RH |
7175 | } |
7176 | } | |
252b5132 | 7177 | else |
c005eb9e | 7178 | ldlang_place_orphan (s); |
252b5132 RH |
7179 | } |
7180 | } | |
7181 | } | |
7182 | } | |
7183 | ||
252b5132 | 7184 | void |
1579bae1 | 7185 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7186 | { |
aa8804e4 | 7187 | flagword *ptr_flags; |
252b5132 | 7188 | |
aa8804e4 | 7189 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7190 | |
252b5132 RH |
7191 | while (*flags) |
7192 | { | |
7193 | switch (*flags) | |
7194 | { | |
f0673d20 NC |
7195 | /* PR 17900: An exclamation mark in the attributes reverses |
7196 | the sense of any of the attributes that follow. */ | |
7197 | case '!': | |
0aa7f586 | 7198 | invert = !invert; |
f0673d20 NC |
7199 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7200 | break; | |
7201 | ||
252b5132 RH |
7202 | case 'A': case 'a': |
7203 | *ptr_flags |= SEC_ALLOC; | |
7204 | break; | |
7205 | ||
7206 | case 'R': case 'r': | |
7207 | *ptr_flags |= SEC_READONLY; | |
7208 | break; | |
7209 | ||
7210 | case 'W': case 'w': | |
7211 | *ptr_flags |= SEC_DATA; | |
7212 | break; | |
7213 | ||
7214 | case 'X': case 'x': | |
7215 | *ptr_flags |= SEC_CODE; | |
7216 | break; | |
7217 | ||
7218 | case 'L': case 'l': | |
7219 | case 'I': case 'i': | |
7220 | *ptr_flags |= SEC_LOAD; | |
7221 | break; | |
7222 | ||
7223 | default: | |
df5f2391 | 7224 | einfo (_("%F%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7225 | *flags, *flags); |
252b5132 RH |
7226 | break; |
7227 | } | |
7228 | flags++; | |
7229 | } | |
7230 | } | |
7231 | ||
d871d478 AM |
7232 | /* Call a function on each real input file. This function will be |
7233 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7234 | |
7235 | void | |
1579bae1 | 7236 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7237 | { |
7238 | lang_input_statement_type *f; | |
7239 | ||
8ce18f9c | 7240 | for (f = (void *) input_file_chain.head; |
252b5132 | 7241 | f != NULL; |
36983a93 | 7242 | f = f->next_real_file) |
d871d478 AM |
7243 | if (f->flags.real) |
7244 | func (f); | |
252b5132 RH |
7245 | } |
7246 | ||
d871d478 AM |
7247 | /* Call a function on each real file. The function will be called on |
7248 | all the elements of an archive which are included in the link, but | |
7249 | will not be called on the archive file itself. */ | |
252b5132 RH |
7250 | |
7251 | void | |
1579bae1 | 7252 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7253 | { |
e50d8076 | 7254 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7255 | { |
d871d478 AM |
7256 | if (f->flags.real) |
7257 | func (f); | |
252b5132 RH |
7258 | } |
7259 | } | |
7260 | ||
252b5132 | 7261 | void |
1579bae1 | 7262 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7263 | { |
36983a93 | 7264 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7265 | |
7266 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7267 | a link. */ | |
c72f2fb2 | 7268 | ASSERT (entry->the_bfd->link.next == NULL); |
f13a99db | 7269 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7270 | |
7271 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7272 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7273 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
7274 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
7275 | ||
7276 | /* Look through the sections and check for any which should not be | |
7277 | included in the link. We need to do this now, so that we can | |
7278 | notice when the backend linker tries to report multiple | |
7279 | definition errors for symbols which are in sections we aren't | |
7280 | going to link. FIXME: It might be better to entirely ignore | |
7281 | symbols which are defined in sections which are going to be | |
7282 | discarded. This would require modifying the backend linker for | |
7283 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7284 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7285 | ||
1579bae1 | 7286 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7287 | } |
7288 | ||
7289 | void | |
1579bae1 | 7290 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7291 | { |
7292 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7293 | if (!had_output_filename || !from_script) |
252b5132 RH |
7294 | { |
7295 | output_filename = name; | |
b34976b6 | 7296 | had_output_filename = TRUE; |
252b5132 RH |
7297 | } |
7298 | } | |
7299 | ||
aea4bd9d | 7300 | lang_output_section_statement_type * |
1579bae1 AM |
7301 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7302 | etree_type *address_exp, | |
7303 | enum section_type sectype, | |
1579bae1 AM |
7304 | etree_type *align, |
7305 | etree_type *subalign, | |
0841712e | 7306 | etree_type *ebase, |
1eec346e NC |
7307 | int constraint, |
7308 | int align_with_input) | |
252b5132 RH |
7309 | { |
7310 | lang_output_section_statement_type *os; | |
7311 | ||
66c103b7 AM |
7312 | os = lang_output_section_statement_lookup (output_section_statement_name, |
7313 | constraint, TRUE); | |
967928e9 | 7314 | current_section = os; |
252b5132 | 7315 | |
1579bae1 | 7316 | if (os->addr_tree == NULL) |
08da4cac KH |
7317 | { |
7318 | os->addr_tree = address_exp; | |
7319 | } | |
252b5132 RH |
7320 | os->sectype = sectype; |
7321 | if (sectype != noload_section) | |
7322 | os->flags = SEC_NO_FLAGS; | |
7323 | else | |
7324 | os->flags = SEC_NEVER_LOAD; | |
e5caa5e0 | 7325 | os->block_value = 1; |
66c103b7 AM |
7326 | |
7327 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 7328 | push_stat_ptr (&os->children); |
252b5132 | 7329 | |
1eec346e NC |
7330 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7331 | if (os->align_lma_with_input && align != NULL) | |
c1c8c1ef | 7332 | einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7333 | NULL); |
1eec346e | 7334 | |
3d9c8f6b AM |
7335 | os->subsection_alignment = subalign; |
7336 | os->section_alignment = align; | |
252b5132 RH |
7337 | |
7338 | os->load_base = ebase; | |
aea4bd9d | 7339 | return os; |
252b5132 RH |
7340 | } |
7341 | ||
252b5132 | 7342 | void |
1579bae1 | 7343 | lang_final (void) |
252b5132 | 7344 | { |
d3ce72d0 NC |
7345 | lang_output_statement_type *new_stmt; |
7346 | ||
7347 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7348 | new_stmt->name = output_filename; | |
252b5132 RH |
7349 | } |
7350 | ||
08da4cac KH |
7351 | /* Reset the current counters in the regions. */ |
7352 | ||
e3dc8847 | 7353 | void |
1579bae1 | 7354 | lang_reset_memory_regions (void) |
252b5132 RH |
7355 | { |
7356 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7357 | asection *o; |
e9ee469a | 7358 | lang_output_section_statement_type *os; |
252b5132 | 7359 | |
1579bae1 | 7360 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7361 | { |
252b5132 | 7362 | p->current = p->origin; |
66e28d60 | 7363 | p->last_os = NULL; |
252b5132 | 7364 | } |
b3327aad | 7365 | |
8ce18f9c | 7366 | for (os = (void *) lang_os_list.head; |
e9ee469a AM |
7367 | os != NULL; |
7368 | os = os->next) | |
cde9e0be AM |
7369 | { |
7370 | os->processed_vma = FALSE; | |
7371 | os->processed_lma = FALSE; | |
7372 | } | |
e9ee469a | 7373 | |
f13a99db | 7374 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7375 | { |
7376 | /* Save the last size for possible use by bfd_relax_section. */ | |
7377 | o->rawsize = o->size; | |
8772de11 MR |
7378 | if (!(o->flags & SEC_FIXED_SIZE)) |
7379 | o->size = 0; | |
1a23a9e6 | 7380 | } |
252b5132 RH |
7381 | } |
7382 | ||
164e712d | 7383 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7384 | |
7385 | static void | |
1579bae1 AM |
7386 | gc_section_callback (lang_wild_statement_type *ptr, |
7387 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7388 | asection *section, | |
b9c361e0 | 7389 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
1579bae1 AM |
7390 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
7391 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7392 | { |
164e712d AM |
7393 | /* If the wild pattern was marked KEEP, the member sections |
7394 | should be as well. */ | |
4dec4d4e RH |
7395 | if (ptr->keep_sections) |
7396 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7397 | } |
7398 | ||
252b5132 RH |
7399 | /* Iterate over sections marking them against GC. */ |
7400 | ||
7401 | static void | |
1579bae1 | 7402 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7403 | { |
1579bae1 | 7404 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7405 | { |
7406 | switch (s->header.type) | |
7407 | { | |
7408 | case lang_wild_statement_enum: | |
164e712d | 7409 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7410 | break; |
252b5132 RH |
7411 | case lang_constructors_statement_enum: |
7412 | lang_gc_sections_1 (constructor_list.head); | |
7413 | break; | |
7414 | case lang_output_section_statement_enum: | |
7415 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7416 | break; | |
7417 | case lang_group_statement_enum: | |
7418 | lang_gc_sections_1 (s->group_statement.children.head); | |
7419 | break; | |
7420 | default: | |
7421 | break; | |
7422 | } | |
7423 | } | |
7424 | } | |
7425 | ||
7426 | static void | |
1579bae1 | 7427 | lang_gc_sections (void) |
252b5132 | 7428 | { |
252b5132 | 7429 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7430 | lang_gc_sections_1 (statement_list.head); |
7431 | ||
9ca57817 AM |
7432 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
7433 | the special case of debug info. (See bfd/stabs.c) | |
7434 | Twiddle the flag here, to simplify later linker code. */ | |
0e1862bb | 7435 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7436 | { |
7437 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7438 | { | |
7439 | asection *sec; | |
9e2278f5 | 7440 | #ifdef ENABLE_PLUGINS |
66be1055 | 7441 | if (f->flags.claimed) |
9e2278f5 AM |
7442 | continue; |
7443 | #endif | |
9ca57817 AM |
7444 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
7445 | if ((sec->flags & SEC_DEBUGGING) == 0) | |
7446 | sec->flags &= ~SEC_EXCLUDE; | |
7447 | } | |
7448 | } | |
7449 | ||
57316bff | 7450 | if (link_info.gc_sections) |
f13a99db | 7451 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7452 | } |
7453 | ||
b10a8ae0 L |
7454 | /* Worker for lang_find_relro_sections_1. */ |
7455 | ||
7456 | static void | |
7457 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7458 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7459 | asection *section, | |
b9c361e0 | 7460 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
b10a8ae0 L |
7461 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
7462 | void *data) | |
7463 | { | |
7464 | /* Discarded, excluded and ignored sections effectively have zero | |
7465 | size. */ | |
7466 | if (section->output_section != NULL | |
f13a99db | 7467 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7468 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7469 | && !IGNORE_SECTION (section) | |
7470 | && section->size != 0) | |
7471 | { | |
7472 | bfd_boolean *has_relro_section = (bfd_boolean *) data; | |
7473 | *has_relro_section = TRUE; | |
7474 | } | |
7475 | } | |
7476 | ||
7477 | /* Iterate over sections for relro sections. */ | |
7478 | ||
7479 | static void | |
7480 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
ed1794ee | 7481 | seg_align_type *seg, |
b10a8ae0 L |
7482 | bfd_boolean *has_relro_section) |
7483 | { | |
7484 | if (*has_relro_section) | |
7485 | return; | |
7486 | ||
7487 | for (; s != NULL; s = s->header.next) | |
7488 | { | |
ed1794ee | 7489 | if (s == seg->relro_end_stat) |
b10a8ae0 L |
7490 | break; |
7491 | ||
7492 | switch (s->header.type) | |
7493 | { | |
7494 | case lang_wild_statement_enum: | |
7495 | walk_wild (&s->wild_statement, | |
7496 | find_relro_section_callback, | |
7497 | has_relro_section); | |
7498 | break; | |
7499 | case lang_constructors_statement_enum: | |
7500 | lang_find_relro_sections_1 (constructor_list.head, | |
ed1794ee | 7501 | seg, has_relro_section); |
b10a8ae0 L |
7502 | break; |
7503 | case lang_output_section_statement_enum: | |
7504 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
ed1794ee | 7505 | seg, has_relro_section); |
b10a8ae0 L |
7506 | break; |
7507 | case lang_group_statement_enum: | |
7508 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
ed1794ee | 7509 | seg, has_relro_section); |
b10a8ae0 L |
7510 | break; |
7511 | default: | |
7512 | break; | |
7513 | } | |
7514 | } | |
7515 | } | |
7516 | ||
7517 | static void | |
7518 | lang_find_relro_sections (void) | |
7519 | { | |
7520 | bfd_boolean has_relro_section = FALSE; | |
7521 | ||
7522 | /* Check all sections in the link script. */ | |
7523 | ||
7524 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
ed1794ee | 7525 | &expld.dataseg, &has_relro_section); |
b10a8ae0 L |
7526 | |
7527 | if (!has_relro_section) | |
7528 | link_info.relro = FALSE; | |
7529 | } | |
7530 | ||
fbbc3759 L |
7531 | /* Relax all sections until bfd_relax_section gives up. */ |
7532 | ||
eaeb0a9d AM |
7533 | void |
7534 | lang_relax_sections (bfd_boolean need_layout) | |
fbbc3759 | 7535 | { |
28d5f677 | 7536 | if (RELAXATION_ENABLED) |
fbbc3759 | 7537 | { |
eaeb0a9d AM |
7538 | /* We may need more than one relaxation pass. */ |
7539 | int i = link_info.relax_pass; | |
fbbc3759 | 7540 | |
eaeb0a9d AM |
7541 | /* The backend can use it to determine the current pass. */ |
7542 | link_info.relax_pass = 0; | |
fbbc3759 | 7543 | |
eaeb0a9d AM |
7544 | while (i--) |
7545 | { | |
7546 | /* Keep relaxing until bfd_relax_section gives up. */ | |
7547 | bfd_boolean relax_again; | |
fbbc3759 | 7548 | |
eaeb0a9d AM |
7549 | link_info.relax_trip = -1; |
7550 | do | |
7551 | { | |
7552 | link_info.relax_trip++; | |
7553 | ||
7554 | /* Note: pe-dll.c does something like this also. If you find | |
7555 | you need to change this code, you probably need to change | |
7556 | pe-dll.c also. DJ */ | |
7557 | ||
7558 | /* Do all the assignments with our current guesses as to | |
7559 | section sizes. */ | |
2f65ac72 | 7560 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7561 | |
7562 | /* We must do this after lang_do_assignments, because it uses | |
7563 | size. */ | |
7564 | lang_reset_memory_regions (); | |
7565 | ||
7566 | /* Perform another relax pass - this time we know where the | |
7567 | globals are, so can make a better guess. */ | |
7568 | relax_again = FALSE; | |
7569 | lang_size_sections (&relax_again, FALSE); | |
7570 | } | |
7571 | while (relax_again); | |
7572 | ||
7573 | link_info.relax_pass++; | |
7574 | } | |
7575 | need_layout = TRUE; | |
7576 | } | |
fbbc3759 | 7577 | |
eaeb0a9d AM |
7578 | if (need_layout) |
7579 | { | |
7580 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 7581 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7582 | lang_reset_memory_regions (); |
7583 | lang_size_sections (NULL, TRUE); | |
fbbc3759 | 7584 | } |
fbbc3759 L |
7585 | } |
7586 | ||
8543fde5 DK |
7587 | #ifdef ENABLE_PLUGINS |
7588 | /* Find the insert point for the plugin's replacement files. We | |
7589 | place them after the first claimed real object file, or if the | |
7590 | first claimed object is an archive member, after the last real | |
7591 | object file immediately preceding the archive. In the event | |
7592 | no objects have been claimed at all, we return the first dummy | |
7593 | object file on the list as the insert point; that works, but | |
7594 | the callee must be careful when relinking the file_chain as it | |
7595 | is not actually on that chain, only the statement_list and the | |
7596 | input_file list; in that case, the replacement files must be | |
7597 | inserted at the head of the file_chain. */ | |
7598 | ||
7599 | static lang_input_statement_type * | |
128bf1fe | 7600 | find_replacements_insert_point (bfd_boolean *before) |
8543fde5 DK |
7601 | { |
7602 | lang_input_statement_type *claim1, *lastobject; | |
8ce18f9c AM |
7603 | lastobject = (void *) input_file_chain.head; |
7604 | for (claim1 = (void *) file_chain.head; | |
8543fde5 | 7605 | claim1 != NULL; |
36983a93 | 7606 | claim1 = claim1->next) |
8543fde5 | 7607 | { |
66be1055 | 7608 | if (claim1->flags.claimed) |
128bf1fe AM |
7609 | { |
7610 | *before = claim1->flags.claim_archive; | |
7611 | return claim1->flags.claim_archive ? lastobject : claim1; | |
7612 | } | |
8543fde5 | 7613 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 7614 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
7615 | lastobject = claim1; |
7616 | } | |
7617 | /* No files were claimed by the plugin. Choose the last object | |
7618 | file found on the list (maybe the first, dummy entry) as the | |
7619 | insert point. */ | |
128bf1fe | 7620 | *before = FALSE; |
8543fde5 DK |
7621 | return lastobject; |
7622 | } | |
9e2278f5 | 7623 | |
1fa4ec6a AM |
7624 | /* Find where to insert ADD, an archive element or shared library |
7625 | added during a rescan. */ | |
7626 | ||
36983a93 | 7627 | static lang_input_statement_type ** |
1fa4ec6a AM |
7628 | find_rescan_insertion (lang_input_statement_type *add) |
7629 | { | |
7630 | bfd *add_bfd = add->the_bfd; | |
7631 | lang_input_statement_type *f; | |
7632 | lang_input_statement_type *last_loaded = NULL; | |
7633 | lang_input_statement_type *before = NULL; | |
36983a93 | 7634 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
7635 | |
7636 | if (add_bfd->my_archive != NULL) | |
7637 | add_bfd = add_bfd->my_archive; | |
7638 | ||
7639 | /* First look through the input file chain, to find an object file | |
7640 | before the one we've rescanned. Normal object files always | |
7641 | appear on both the input file chain and the file chain, so this | |
7642 | lets us get quickly to somewhere near the correct place on the | |
7643 | file chain if it is full of archive elements. Archives don't | |
7644 | appear on the file chain, but if an element has been extracted | |
7645 | then their input_statement->next points at it. */ | |
8ce18f9c | 7646 | for (f = (void *) input_file_chain.head; |
1fa4ec6a | 7647 | f != NULL; |
36983a93 | 7648 | f = f->next_real_file) |
1fa4ec6a AM |
7649 | { |
7650 | if (f->the_bfd == add_bfd) | |
7651 | { | |
7652 | before = last_loaded; | |
7653 | if (f->next != NULL) | |
36983a93 | 7654 | return &f->next->next; |
1fa4ec6a AM |
7655 | } |
7656 | if (f->the_bfd != NULL && f->next != NULL) | |
7657 | last_loaded = f; | |
7658 | } | |
7659 | ||
7660 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
7661 | *iter != NULL; | |
36983a93 AM |
7662 | iter = &(*iter)->next) |
7663 | if (!(*iter)->flags.claim_archive | |
7664 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
7665 | break; |
7666 | ||
7667 | return iter; | |
7668 | } | |
7669 | ||
9e2278f5 AM |
7670 | /* Insert SRCLIST into DESTLIST after given element by chaining |
7671 | on FIELD as the next-pointer. (Counterintuitively does not need | |
7672 | a pointer to the actual after-node itself, just its chain field.) */ | |
7673 | ||
7674 | static void | |
7675 | lang_list_insert_after (lang_statement_list_type *destlist, | |
7676 | lang_statement_list_type *srclist, | |
7677 | lang_statement_union_type **field) | |
7678 | { | |
7679 | *(srclist->tail) = *field; | |
7680 | *field = srclist->head; | |
7681 | if (destlist->tail == field) | |
7682 | destlist->tail = srclist->tail; | |
7683 | } | |
7684 | ||
7685 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
7686 | was taken as a copy of it and leave them in ORIGLIST. */ | |
7687 | ||
7688 | static void | |
7689 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
7690 | lang_statement_list_type *origlist) | |
7691 | { | |
7692 | union lang_statement_union **savetail; | |
7693 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
7694 | ASSERT (origlist->head == destlist->head); | |
7695 | savetail = origlist->tail; | |
7696 | origlist->head = *(savetail); | |
7697 | origlist->tail = destlist->tail; | |
7698 | destlist->tail = savetail; | |
7699 | *savetail = NULL; | |
7700 | } | |
128bf1fe AM |
7701 | |
7702 | static lang_statement_union_type ** | |
7703 | find_next_input_statement (lang_statement_union_type **s) | |
7704 | { | |
7705 | for ( ; *s; s = &(*s)->header.next) | |
7706 | { | |
7707 | lang_statement_union_type **t; | |
7708 | switch ((*s)->header.type) | |
7709 | { | |
7710 | case lang_input_statement_enum: | |
7711 | return s; | |
7712 | case lang_wild_statement_enum: | |
7713 | t = &(*s)->wild_statement.children.head; | |
7714 | break; | |
7715 | case lang_group_statement_enum: | |
7716 | t = &(*s)->group_statement.children.head; | |
7717 | break; | |
7718 | case lang_output_section_statement_enum: | |
7719 | t = &(*s)->output_section_statement.children.head; | |
7720 | break; | |
7721 | default: | |
7722 | continue; | |
7723 | } | |
7724 | t = find_next_input_statement (t); | |
7725 | if (*t) | |
7726 | return t; | |
7727 | } | |
7728 | return s; | |
7729 | } | |
8543fde5 DK |
7730 | #endif /* ENABLE_PLUGINS */ |
7731 | ||
4153b6db NC |
7732 | /* Add NAME to the list of garbage collection entry points. */ |
7733 | ||
7734 | void | |
0aa7f586 | 7735 | lang_add_gc_name (const char *name) |
4153b6db NC |
7736 | { |
7737 | struct bfd_sym_chain *sym; | |
7738 | ||
7739 | if (name == NULL) | |
7740 | return; | |
7741 | ||
988de25b | 7742 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
7743 | |
7744 | sym->next = link_info.gc_sym_list; | |
7745 | sym->name = name; | |
7746 | link_info.gc_sym_list = sym; | |
7747 | } | |
7748 | ||
fbf05aa7 L |
7749 | /* Check relocations. */ |
7750 | ||
7751 | static void | |
7752 | lang_check_relocs (void) | |
7753 | { | |
4f3b23b3 | 7754 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
7755 | { |
7756 | bfd *abfd; | |
7757 | ||
7758 | for (abfd = link_info.input_bfds; | |
7759 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 7760 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 7761 | { |
4f3b23b3 | 7762 | /* No object output, fail return. */ |
fbf05aa7 | 7763 | config.make_executable = FALSE; |
4f3b23b3 NC |
7764 | /* Note: we do not abort the loop, but rather |
7765 | continue the scan in case there are other | |
7766 | bad relocations to report. */ | |
fbf05aa7 L |
7767 | } |
7768 | } | |
7769 | } | |
7770 | ||
77f5e65e AB |
7771 | /* Look through all output sections looking for places where we can |
7772 | propagate forward the lma region. */ | |
7773 | ||
7774 | static void | |
7775 | lang_propagate_lma_regions (void) | |
7776 | { | |
7777 | lang_output_section_statement_type *os; | |
7778 | ||
8ce18f9c | 7779 | for (os = (void *) lang_os_list.head; |
77f5e65e AB |
7780 | os != NULL; |
7781 | os = os->next) | |
7782 | { | |
7783 | if (os->prev != NULL | |
7784 | && os->lma_region == NULL | |
7785 | && os->load_base == NULL | |
7786 | && os->addr_tree == NULL | |
7787 | && os->region == os->prev->region) | |
7788 | os->lma_region = os->prev->lma_region; | |
7789 | } | |
7790 | } | |
7791 | ||
252b5132 | 7792 | void |
1579bae1 | 7793 | lang_process (void) |
252b5132 | 7794 | { |
55255dae | 7795 | /* Finalize dynamic list. */ |
40b36307 L |
7796 | if (link_info.dynamic_list) |
7797 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 7798 | |
252b5132 RH |
7799 | current_target = default_target; |
7800 | ||
08da4cac KH |
7801 | /* Open the output file. */ |
7802 | lang_for_each_statement (ldlang_open_output); | |
61826503 | 7803 | init_opb (NULL); |
252b5132 RH |
7804 | |
7805 | ldemul_create_output_section_statements (); | |
7806 | ||
08da4cac | 7807 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
7808 | lang_place_undefineds (); |
7809 | ||
082b7297 | 7810 | if (!bfd_section_already_linked_table_init ()) |
df5f2391 | 7811 | einfo (_("%F%P: can not create hash table: %E\n")); |
9503fd87 | 7812 | |
08da4cac | 7813 | /* Create a bfd for each input file. */ |
252b5132 | 7814 | current_target = default_target; |
165f707a | 7815 | lang_statement_iteration++; |
486329aa | 7816 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
7f0cfc6e AM |
7817 | /* open_input_bfds also handles assignments, so we can give values |
7818 | to symbolic origin/length now. */ | |
7819 | lang_do_memory_regions (); | |
252b5132 | 7820 | |
5d3236ee | 7821 | #ifdef ENABLE_PLUGINS |
716db898 | 7822 | if (link_info.lto_plugin_active) |
f84854b6 | 7823 | { |
8543fde5 DK |
7824 | lang_statement_list_type added; |
7825 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 7826 | |
f84854b6 L |
7827 | /* Now all files are read, let the plugin(s) decide if there |
7828 | are any more to be added to the link before we call the | |
8543fde5 DK |
7829 | emulation's after_open hook. We create a private list of |
7830 | input statements for this purpose, which we will eventually | |
370dfff4 | 7831 | insert into the global statement list after the first claimed |
8543fde5 DK |
7832 | file. */ |
7833 | added = *stat_ptr; | |
7834 | /* We need to manipulate all three chains in synchrony. */ | |
7835 | files = file_chain; | |
7836 | inputfiles = input_file_chain; | |
f84854b6 | 7837 | if (plugin_call_all_symbols_read ()) |
df5f2391 | 7838 | einfo (_("%F%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 7839 | plugin_error_plugin ()); |
8543fde5 | 7840 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 7841 | plugin_undefs = link_info.hash->undefs_tail; |
cdaa438c | 7842 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
b02c4f16 AM |
7843 | if (plugin_undefs == link_info.hash->undefs_tail) |
7844 | plugin_undefs = NULL; | |
8543fde5 DK |
7845 | /* Restore the global list pointer now they have all been added. */ |
7846 | lang_list_remove_tail (stat_ptr, &added); | |
7847 | /* And detach the fresh ends of the file lists. */ | |
7848 | lang_list_remove_tail (&file_chain, &files); | |
7849 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
7850 | /* Were any new files added? */ | |
7851 | if (added.head != NULL) | |
7852 | { | |
7853 | /* If so, we will insert them into the statement list immediately | |
128bf1fe AM |
7854 | after the first input file that was claimed by the plugin, |
7855 | unless that file was an archive in which case it is inserted | |
7856 | immediately before. */ | |
7857 | bfd_boolean before; | |
7858 | lang_statement_union_type **prev; | |
7859 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
7860 | /* If a plugin adds input files without having claimed any, we |
7861 | don't really have a good idea where to place them. Just putting | |
7862 | them at the start or end of the list is liable to leave them | |
7863 | outside the crtbegin...crtend range. */ | |
9e2278f5 AM |
7864 | ASSERT (plugin_insert != NULL); |
7865 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
7866 | prev = &plugin_insert->header.next; |
7867 | if (before) | |
7868 | { | |
7869 | prev = find_next_input_statement (prev); | |
36983a93 | 7870 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 7871 | { |
a19826f4 | 7872 | /* We didn't find the expected input statement. |
1f1f5b92 | 7873 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
7874 | prev = &plugin_insert->header.next; |
7875 | } | |
7876 | } | |
7877 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
7878 | /* Likewise for the file chains. */ |
7879 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 7880 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
7881 | /* We must be careful when relinking file_chain; we may need to |
7882 | insert the new files at the head of the list if the insert | |
7883 | point chosen is the dummy first input file. */ | |
9e2278f5 | 7884 | if (plugin_insert->filename) |
36983a93 AM |
7885 | lang_list_insert_after (&file_chain, &files, |
7886 | (void *) &plugin_insert->next); | |
8543fde5 DK |
7887 | else |
7888 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
7889 | |
7890 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 7891 | files = file_chain; |
165f707a | 7892 | lang_statement_iteration++; |
9e2278f5 | 7893 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
7894 | lang_list_remove_tail (&file_chain, &files); |
7895 | while (files.head != NULL) | |
7896 | { | |
36983a93 AM |
7897 | lang_input_statement_type **insert; |
7898 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
7899 | bfd *my_arch; |
7900 | ||
7901 | insert = find_rescan_insertion (&files.head->input_statement); | |
7902 | /* All elements from an archive can be added at once. */ | |
7903 | iter = &files.head->input_statement.next; | |
7904 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
7905 | if (my_arch != NULL) | |
36983a93 AM |
7906 | for (; *iter != NULL; iter = &(*iter)->next) |
7907 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
7908 | break; |
7909 | temp = *insert; | |
36983a93 AM |
7910 | *insert = &files.head->input_statement; |
7911 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a AM |
7912 | *iter = temp; |
7913 | if (my_arch != NULL) | |
7914 | { | |
00f93c44 | 7915 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 7916 | if (parent != NULL) |
36983a93 | 7917 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
7918 | ((char *) iter |
7919 | - offsetof (lang_input_statement_type, next)); | |
7920 | } | |
7921 | } | |
8543fde5 | 7922 | } |
f84854b6 | 7923 | } |
5d3236ee DK |
7924 | #endif /* ENABLE_PLUGINS */ |
7925 | ||
0aa7f586 AM |
7926 | /* Make sure that nobody has tried to add a symbol to this list |
7927 | before now. */ | |
4153b6db NC |
7928 | ASSERT (link_info.gc_sym_list == NULL); |
7929 | ||
e3e942e9 | 7930 | link_info.gc_sym_list = &entry_symbol; |
41f46ed9 | 7931 | |
e3e942e9 | 7932 | if (entry_symbol.name == NULL) |
41f46ed9 SKS |
7933 | { |
7934 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
7935 | ||
7936 | /* entry_symbol is normally initialied by a ENTRY definition in the | |
7937 | linker script or the -e command line option. But if neither of | |
7938 | these have been used, the target specific backend may still have | |
4153b6db | 7939 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
7940 | Unfortunately this value will not be processed until lang_end() |
7941 | is called, long after this function has finished. So detect this | |
7942 | case here and add the target's entry symbol to the list of starting | |
7943 | points for garbage collection resolution. */ | |
4153b6db | 7944 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
7945 | } |
7946 | ||
4153b6db NC |
7947 | lang_add_gc_name (link_info.init_function); |
7948 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 7949 | |
252b5132 | 7950 | ldemul_after_open (); |
16e4ecc0 AM |
7951 | if (config.map_file != NULL) |
7952 | lang_print_asneeded (); | |
252b5132 | 7953 | |
1ff6de03 NA |
7954 | ldlang_open_ctf (); |
7955 | ||
082b7297 | 7956 | bfd_section_already_linked_table_free (); |
9503fd87 | 7957 | |
252b5132 RH |
7958 | /* Make sure that we're not mixing architectures. We call this |
7959 | after all the input files have been opened, but before we do any | |
7960 | other processing, so that any operations merge_private_bfd_data | |
7961 | does on the output file will be known during the rest of the | |
7962 | link. */ | |
7963 | lang_check (); | |
7964 | ||
7965 | /* Handle .exports instead of a version script if we're told to do so. */ | |
7966 | if (command_line.version_exports_section) | |
7967 | lang_do_version_exports_section (); | |
7968 | ||
7969 | /* Build all sets based on the information gathered from the input | |
7970 | files. */ | |
7971 | ldctor_build_sets (); | |
7972 | ||
7dba9362 AM |
7973 | /* Give initial values for __start and __stop symbols, so that ELF |
7974 | gc_sections will keep sections referenced by these symbols. Must | |
7975 | be done before lang_do_assignments below. */ | |
7976 | if (config.build_constructors) | |
7977 | lang_init_start_stop (); | |
7978 | ||
2aa9aad9 NC |
7979 | /* PR 13683: We must rerun the assignments prior to running garbage |
7980 | collection in order to make sure that all symbol aliases are resolved. */ | |
7981 | lang_do_assignments (lang_mark_phase_enum); | |
7982 | expld.phase = lang_first_phase_enum; | |
7983 | ||
08da4cac | 7984 | /* Size up the common data. */ |
252b5132 RH |
7985 | lang_common (); |
7986 | ||
c4621b33 AM |
7987 | /* Remove unreferenced sections if asked to. */ |
7988 | lang_gc_sections (); | |
7989 | ||
fbf05aa7 L |
7990 | /* Check relocations. */ |
7991 | lang_check_relocs (); | |
7992 | ||
5c3261b0 L |
7993 | ldemul_after_check_relocs (); |
7994 | ||
bcaa7b3e L |
7995 | /* Update wild statements. */ |
7996 | update_wild_statements (statement_list.head); | |
7997 | ||
252b5132 | 7998 | /* Run through the contours of the script and attach input sections |
08da4cac | 7999 | to the correct output sections. */ |
e759c116 | 8000 | lang_statement_iteration++; |
1579bae1 | 8001 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 8002 | |
776ab89f AM |
8003 | /* Start at the statement immediately after the special abs_section |
8004 | output statement, so that it isn't reordered. */ | |
8005 | process_insert_statements (&lang_os_list.head->header.next); | |
53d25da6 | 8006 | |
9b538ba7 L |
8007 | ldemul_before_place_orphans (); |
8008 | ||
08da4cac | 8009 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
8010 | lang_place_orphans (); |
8011 | ||
0e1862bb | 8012 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 8013 | { |
57ceae94 AM |
8014 | asection *found; |
8015 | ||
8016 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
8017 | sections, so that GCed sections are not merged, but before | |
8018 | assigning dynamic symbols, since removing whole input sections | |
8019 | is hard then. */ | |
f13a99db | 8020 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 8021 | |
862120bd | 8022 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 8023 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 8024 | |
1579bae1 | 8025 | if (found != NULL) |
862120bd AM |
8026 | { |
8027 | if (config.text_read_only) | |
8028 | found->flags |= SEC_READONLY; | |
8029 | else | |
8030 | found->flags &= ~SEC_READONLY; | |
8031 | } | |
8032 | } | |
8033 | ||
1ff6de03 NA |
8034 | /* Merge together CTF sections. After this, only the symtab-dependent |
8035 | function and data object sections need adjustment. */ | |
8036 | lang_merge_ctf (); | |
8037 | ||
8038 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
8039 | examining things laid out late, like the strtab. */ | |
8040 | lang_write_ctf (0); | |
8041 | ||
77f5e65e AB |
8042 | /* Copy forward lma regions for output sections in same lma region. */ |
8043 | lang_propagate_lma_regions (); | |
8044 | ||
7dba9362 AM |
8045 | /* Defining __start/__stop symbols early for --gc-sections to work |
8046 | around a glibc build problem can result in these symbols being | |
8047 | defined when they should not be. Fix them now. */ | |
8048 | if (config.build_constructors) | |
8049 | lang_undef_start_stop (); | |
8050 | ||
8051 | /* Define .startof./.sizeof. symbols with preliminary values before | |
8052 | dynamic symbols are created. */ | |
8053 | if (!bfd_link_relocatable (&link_info)) | |
8054 | lang_init_startof_sizeof (); | |
8055 | ||
862120bd AM |
8056 | /* Do anything special before sizing sections. This is where ELF |
8057 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
8058 | ldemul_before_allocation (); |
8059 | ||
8060 | /* We must record the program headers before we try to fix the | |
8061 | section positions, since they will affect SIZEOF_HEADERS. */ | |
8062 | lang_record_phdrs (); | |
8063 | ||
b10a8ae0 | 8064 | /* Check relro sections. */ |
0e1862bb | 8065 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
8066 | lang_find_relro_sections (); |
8067 | ||
b3327aad | 8068 | /* Size up the sections. */ |
0aa7f586 | 8069 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 8070 | |
252b5132 | 8071 | /* See if anything special should be done now we know how big |
eaeb0a9d | 8072 | everything is. This is where relaxation is done. */ |
252b5132 RH |
8073 | ldemul_after_allocation (); |
8074 | ||
7dba9362 AM |
8075 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
8076 | lang_finalize_start_stop (); | |
252b5132 | 8077 | |
7f0cfc6e AM |
8078 | /* Do all the assignments again, to report errors. Assignment |
8079 | statements are processed multiple times, updating symbols; In | |
8080 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
8081 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
8082 | also updated in ldemul_after_allocation. */ | |
2f65ac72 | 8083 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 8084 | |
8ded5a0f AM |
8085 | ldemul_finish (); |
8086 | ||
975f8a9e AM |
8087 | /* Convert absolute symbols to section relative. */ |
8088 | ldexp_finalize_syms (); | |
8089 | ||
252b5132 | 8090 | /* Make sure that the section addresses make sense. */ |
662ef24b | 8091 | if (command_line.check_section_addresses) |
252b5132 | 8092 | lang_check_section_addresses (); |
5f992e62 | 8093 | |
0a618243 AB |
8094 | /* Check any required symbols are known. */ |
8095 | ldlang_check_require_defined_symbols (); | |
8096 | ||
750877ba | 8097 | lang_end (); |
252b5132 RH |
8098 | } |
8099 | ||
8100 | /* EXPORTED TO YACC */ | |
8101 | ||
8102 | void | |
1579bae1 AM |
8103 | lang_add_wild (struct wildcard_spec *filespec, |
8104 | struct wildcard_list *section_list, | |
8105 | bfd_boolean keep_sections) | |
252b5132 | 8106 | { |
b6bf44ba | 8107 | struct wildcard_list *curr, *next; |
d3ce72d0 | 8108 | lang_wild_statement_type *new_stmt; |
b6bf44ba AM |
8109 | |
8110 | /* Reverse the list as the parser puts it back to front. */ | |
8111 | for (curr = section_list, section_list = NULL; | |
8112 | curr != NULL; | |
8113 | section_list = curr, curr = next) | |
8114 | { | |
b6bf44ba AM |
8115 | next = curr->next; |
8116 | curr->next = section_list; | |
8117 | } | |
8118 | ||
8119 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 8120 | { |
b6bf44ba AM |
8121 | if (strcmp (filespec->name, "*") == 0) |
8122 | filespec->name = NULL; | |
0aa7f586 | 8123 | else if (!wildcardp (filespec->name)) |
b34976b6 | 8124 | lang_has_input_file = TRUE; |
252b5132 | 8125 | } |
b6bf44ba | 8126 | |
d3ce72d0 NC |
8127 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
8128 | new_stmt->filename = NULL; | |
8129 | new_stmt->filenames_sorted = FALSE; | |
ae17ab41 | 8130 | new_stmt->section_flag_list = NULL; |
8f1732fc | 8131 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 8132 | if (filespec != NULL) |
252b5132 | 8133 | { |
d3ce72d0 NC |
8134 | new_stmt->filename = filespec->name; |
8135 | new_stmt->filenames_sorted = filespec->sorted == by_name; | |
ae17ab41 | 8136 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 8137 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 8138 | } |
d3ce72d0 NC |
8139 | new_stmt->section_list = section_list; |
8140 | new_stmt->keep_sections = keep_sections; | |
8141 | lang_list_init (&new_stmt->children); | |
8142 | analyze_walk_wild_section_handler (new_stmt); | |
252b5132 RH |
8143 | } |
8144 | ||
8145 | void | |
ba916c8a MM |
8146 | lang_section_start (const char *name, etree_type *address, |
8147 | const segment_type *segment) | |
252b5132 | 8148 | { |
d1778b88 | 8149 | lang_address_statement_type *ad; |
252b5132 | 8150 | |
d1778b88 | 8151 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
8152 | ad->section_name = name; |
8153 | ad->address = address; | |
ba916c8a | 8154 | ad->segment = segment; |
252b5132 RH |
8155 | } |
8156 | ||
8157 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
8158 | because of a -e argument on the command line, or zero if this is | |
8159 | called by ENTRY in a linker script. Command line arguments take | |
8160 | precedence. */ | |
8161 | ||
8162 | void | |
1579bae1 | 8163 | lang_add_entry (const char *name, bfd_boolean cmdline) |
252b5132 | 8164 | { |
e3e942e9 | 8165 | if (entry_symbol.name == NULL |
252b5132 | 8166 | || cmdline |
0aa7f586 | 8167 | || !entry_from_cmdline) |
252b5132 | 8168 | { |
e3e942e9 | 8169 | entry_symbol.name = name; |
252b5132 RH |
8170 | entry_from_cmdline = cmdline; |
8171 | } | |
8172 | } | |
8173 | ||
a359509e ZW |
8174 | /* Set the default start symbol to NAME. .em files should use this, |
8175 | not lang_add_entry, to override the use of "start" if neither the | |
8176 | linker script nor the command line specifies an entry point. NAME | |
8177 | must be permanently allocated. */ | |
8178 | void | |
8179 | lang_default_entry (const char *name) | |
8180 | { | |
8181 | entry_symbol_default = name; | |
8182 | } | |
8183 | ||
252b5132 | 8184 | void |
1579bae1 | 8185 | lang_add_target (const char *name) |
252b5132 | 8186 | { |
d3ce72d0 | 8187 | lang_target_statement_type *new_stmt; |
252b5132 | 8188 | |
d3ce72d0 NC |
8189 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8190 | new_stmt->target = name; | |
252b5132 RH |
8191 | } |
8192 | ||
8193 | void | |
1579bae1 | 8194 | lang_add_map (const char *name) |
252b5132 RH |
8195 | { |
8196 | while (*name) | |
8197 | { | |
8198 | switch (*name) | |
8199 | { | |
08da4cac | 8200 | case 'F': |
b34976b6 | 8201 | map_option_f = TRUE; |
252b5132 RH |
8202 | break; |
8203 | } | |
8204 | name++; | |
8205 | } | |
8206 | } | |
8207 | ||
8208 | void | |
1579bae1 | 8209 | lang_add_fill (fill_type *fill) |
252b5132 | 8210 | { |
d3ce72d0 | 8211 | lang_fill_statement_type *new_stmt; |
252b5132 | 8212 | |
d3ce72d0 NC |
8213 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8214 | new_stmt->fill = fill; | |
252b5132 RH |
8215 | } |
8216 | ||
8217 | void | |
1579bae1 | 8218 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8219 | { |
d3ce72d0 | 8220 | lang_data_statement_type *new_stmt; |
252b5132 | 8221 | |
d3ce72d0 NC |
8222 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8223 | new_stmt->exp = exp; | |
8224 | new_stmt->type = type; | |
252b5132 RH |
8225 | } |
8226 | ||
8227 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
8228 | generate. HOWTO is the corresponding howto structure (we could | |
8229 | look this up, but the caller has already done so). SECTION is the | |
8230 | section to generate a reloc against, or NAME is the name of the | |
8231 | symbol to generate a reloc against. Exactly one of SECTION and | |
8232 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8233 | ||
8234 | void | |
1579bae1 AM |
8235 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
8236 | reloc_howto_type *howto, | |
8237 | asection *section, | |
8238 | const char *name, | |
8239 | union etree_union *addend) | |
252b5132 RH |
8240 | { |
8241 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8242 | |
252b5132 RH |
8243 | p->reloc = reloc; |
8244 | p->howto = howto; | |
8245 | p->section = section; | |
8246 | p->name = name; | |
8247 | p->addend_exp = addend; | |
8248 | ||
8249 | p->addend_value = 0; | |
8250 | p->output_section = NULL; | |
7fabd029 | 8251 | p->output_offset = 0; |
252b5132 RH |
8252 | } |
8253 | ||
8254 | lang_assignment_statement_type * | |
1579bae1 | 8255 | lang_add_assignment (etree_type *exp) |
252b5132 | 8256 | { |
d3ce72d0 | 8257 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8258 | |
d3ce72d0 NC |
8259 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8260 | new_stmt->exp = exp; | |
8261 | return new_stmt; | |
252b5132 RH |
8262 | } |
8263 | ||
8264 | void | |
1579bae1 | 8265 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8266 | { |
bd4d42c1 | 8267 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8268 | } |
8269 | ||
8270 | void | |
1579bae1 | 8271 | lang_startup (const char *name) |
252b5132 | 8272 | { |
1e915804 | 8273 | if (first_file->filename != NULL) |
252b5132 | 8274 | { |
df5f2391 | 8275 | einfo (_("%F%P: multiple STARTUP files\n")); |
252b5132 RH |
8276 | } |
8277 | first_file->filename = name; | |
8278 | first_file->local_sym_name = name; | |
66be1055 | 8279 | first_file->flags.real = TRUE; |
252b5132 RH |
8280 | } |
8281 | ||
8282 | void | |
1579bae1 | 8283 | lang_float (bfd_boolean maybe) |
252b5132 RH |
8284 | { |
8285 | lang_float_flag = maybe; | |
8286 | } | |
8287 | ||
ee3cc2e2 RS |
8288 | |
8289 | /* Work out the load- and run-time regions from a script statement, and | |
8290 | store them in *LMA_REGION and *REGION respectively. | |
8291 | ||
a747ee4d NC |
8292 | MEMSPEC is the name of the run-time region, or the value of |
8293 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8294 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8295 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8296 | had an explicit load address. | |
ee3cc2e2 RS |
8297 | |
8298 | It is an error to specify both a load region and a load address. */ | |
8299 | ||
8300 | static void | |
6bdafbeb NC |
8301 | lang_get_regions (lang_memory_region_type **region, |
8302 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8303 | const char *memspec, |
8304 | const char *lma_memspec, | |
6bdafbeb NC |
8305 | bfd_boolean have_lma, |
8306 | bfd_boolean have_vma) | |
ee3cc2e2 | 8307 | { |
a747ee4d | 8308 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
ee3cc2e2 | 8309 | |
6feb9908 AM |
8310 | /* If no runtime region or VMA has been specified, but the load region |
8311 | has been specified, then use the load region for the runtime region | |
8312 | as well. */ | |
6bdafbeb | 8313 | if (lma_memspec != NULL |
0aa7f586 | 8314 | && !have_vma |
6bdafbeb | 8315 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
8316 | *region = *lma_region; |
8317 | else | |
a747ee4d | 8318 | *region = lang_memory_region_lookup (memspec, FALSE); |
ee3cc2e2 | 8319 | |
6bdafbeb | 8320 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 8321 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 8322 | NULL); |
ee3cc2e2 RS |
8323 | } |
8324 | ||
252b5132 | 8325 | void |
6bdafbeb NC |
8326 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
8327 | lang_output_section_phdr_list *phdrs, | |
8328 | const char *lma_memspec) | |
252b5132 | 8329 | { |
ee3cc2e2 RS |
8330 | lang_get_regions (¤t_section->region, |
8331 | ¤t_section->lma_region, | |
8332 | memspec, lma_memspec, | |
6bdafbeb NC |
8333 | current_section->load_base != NULL, |
8334 | current_section->addr_tree != NULL); | |
113e6845 | 8335 | |
252b5132 | 8336 | current_section->fill = fill; |
252b5132 | 8337 | current_section->phdrs = phdrs; |
bde18da4 | 8338 | pop_stat_ptr (); |
252b5132 RH |
8339 | } |
8340 | ||
252b5132 RH |
8341 | /* Set the output format type. -oformat overrides scripts. */ |
8342 | ||
8343 | void | |
1579bae1 AM |
8344 | lang_add_output_format (const char *format, |
8345 | const char *big, | |
8346 | const char *little, | |
8347 | int from_script) | |
252b5132 RH |
8348 | { |
8349 | if (output_target == NULL || !from_script) | |
8350 | { | |
8351 | if (command_line.endian == ENDIAN_BIG | |
8352 | && big != NULL) | |
8353 | format = big; | |
8354 | else if (command_line.endian == ENDIAN_LITTLE | |
8355 | && little != NULL) | |
8356 | format = little; | |
8357 | ||
8358 | output_target = format; | |
8359 | } | |
8360 | } | |
8361 | ||
53d25da6 AM |
8362 | void |
8363 | lang_add_insert (const char *where, int is_before) | |
8364 | { | |
d3ce72d0 | 8365 | lang_insert_statement_type *new_stmt; |
53d25da6 | 8366 | |
d3ce72d0 NC |
8367 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
8368 | new_stmt->where = where; | |
8369 | new_stmt->is_before = is_before; | |
53d25da6 AM |
8370 | saved_script_handle = previous_script_handle; |
8371 | } | |
8372 | ||
252b5132 RH |
8373 | /* Enter a group. This creates a new lang_group_statement, and sets |
8374 | stat_ptr to build new statements within the group. */ | |
8375 | ||
8376 | void | |
1579bae1 | 8377 | lang_enter_group (void) |
252b5132 RH |
8378 | { |
8379 | lang_group_statement_type *g; | |
8380 | ||
8381 | g = new_stat (lang_group_statement, stat_ptr); | |
8382 | lang_list_init (&g->children); | |
bde18da4 | 8383 | push_stat_ptr (&g->children); |
252b5132 RH |
8384 | } |
8385 | ||
8386 | /* Leave a group. This just resets stat_ptr to start writing to the | |
8387 | regular list of statements again. Note that this will not work if | |
8388 | groups can occur inside anything else which can adjust stat_ptr, | |
8389 | but currently they can't. */ | |
8390 | ||
8391 | void | |
1579bae1 | 8392 | lang_leave_group (void) |
252b5132 | 8393 | { |
bde18da4 | 8394 | pop_stat_ptr (); |
252b5132 RH |
8395 | } |
8396 | ||
8397 | /* Add a new program header. This is called for each entry in a PHDRS | |
8398 | command in a linker script. */ | |
8399 | ||
8400 | void | |
1579bae1 AM |
8401 | lang_new_phdr (const char *name, |
8402 | etree_type *type, | |
8403 | bfd_boolean filehdr, | |
8404 | bfd_boolean phdrs, | |
8405 | etree_type *at, | |
8406 | etree_type *flags) | |
252b5132 RH |
8407 | { |
8408 | struct lang_phdr *n, **pp; | |
5c1a3f0f | 8409 | bfd_boolean hdrs; |
252b5132 | 8410 | |
988de25b | 8411 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
8412 | n->next = NULL; |
8413 | n->name = name; | |
3d9c8f6b | 8414 | n->type = exp_get_vma (type, 0, "program header type"); |
252b5132 RH |
8415 | n->filehdr = filehdr; |
8416 | n->phdrs = phdrs; | |
8417 | n->at = at; | |
8418 | n->flags = flags; | |
1e0061d2 | 8419 | |
5c1a3f0f | 8420 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
8421 | |
8422 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
8423 | if (hdrs |
8424 | && (*pp)->type == 1 | |
8425 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 8426 | { |
c1c8c1ef | 8427 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 8428 | " when prior PT_LOAD headers lack them\n"), NULL); |
5c1a3f0f NS |
8429 | hdrs = FALSE; |
8430 | } | |
8431 | ||
252b5132 RH |
8432 | *pp = n; |
8433 | } | |
8434 | ||
8435 | /* Record the program header information in the output BFD. FIXME: We | |
8436 | should not be calling an ELF specific function here. */ | |
8437 | ||
8438 | static void | |
1579bae1 | 8439 | lang_record_phdrs (void) |
252b5132 RH |
8440 | { |
8441 | unsigned int alc; | |
8442 | asection **secs; | |
6bdafbeb | 8443 | lang_output_section_phdr_list *last; |
252b5132 | 8444 | struct lang_phdr *l; |
afd7a018 | 8445 | lang_output_section_statement_type *os; |
252b5132 RH |
8446 | |
8447 | alc = 10; | |
1e9cc1c2 | 8448 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 8449 | last = NULL; |
591a748a | 8450 | |
252b5132 RH |
8451 | for (l = lang_phdr_list; l != NULL; l = l->next) |
8452 | { | |
8453 | unsigned int c; | |
8454 | flagword flags; | |
8455 | bfd_vma at; | |
8456 | ||
8457 | c = 0; | |
8ce18f9c | 8458 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8459 | os != NULL; |
8460 | os = os->next) | |
252b5132 | 8461 | { |
6bdafbeb | 8462 | lang_output_section_phdr_list *pl; |
252b5132 | 8463 | |
66c103b7 | 8464 | if (os->constraint < 0) |
0841712e | 8465 | continue; |
252b5132 RH |
8466 | |
8467 | pl = os->phdrs; | |
8468 | if (pl != NULL) | |
8469 | last = pl; | |
8470 | else | |
8471 | { | |
8472 | if (os->sectype == noload_section | |
8473 | || os->bfd_section == NULL | |
8474 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
8475 | continue; | |
591a748a | 8476 | |
7512c397 AM |
8477 | /* Don't add orphans to PT_INTERP header. */ |
8478 | if (l->type == 3) | |
8479 | continue; | |
8480 | ||
e9442572 | 8481 | if (last == NULL) |
591a748a | 8482 | { |
0aa7f586 | 8483 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
8484 | |
8485 | /* If we have not run across a section with a program | |
8486 | header assigned to it yet, then scan forwards to find | |
8487 | one. This prevents inconsistencies in the linker's | |
8488 | behaviour when a script has specified just a single | |
8489 | header and there are sections in that script which are | |
8490 | not assigned to it, and which occur before the first | |
8491 | use of that header. See here for more details: | |
8492 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
8493 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
8494 | if (tmp_os->phdrs) | |
e9442572 AM |
8495 | { |
8496 | last = tmp_os->phdrs; | |
8497 | break; | |
8498 | } | |
8499 | if (last == NULL) | |
8500 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 8501 | } |
e9442572 | 8502 | pl = last; |
252b5132 RH |
8503 | } |
8504 | ||
8505 | if (os->bfd_section == NULL) | |
8506 | continue; | |
8507 | ||
8508 | for (; pl != NULL; pl = pl->next) | |
8509 | { | |
8510 | if (strcmp (pl->name, l->name) == 0) | |
8511 | { | |
8512 | if (c >= alc) | |
8513 | { | |
8514 | alc *= 2; | |
1e9cc1c2 | 8515 | secs = (asection **) xrealloc (secs, |
4724d37e | 8516 | alc * sizeof (asection *)); |
252b5132 RH |
8517 | } |
8518 | secs[c] = os->bfd_section; | |
8519 | ++c; | |
b34976b6 | 8520 | pl->used = TRUE; |
252b5132 RH |
8521 | } |
8522 | } | |
8523 | } | |
8524 | ||
8525 | if (l->flags == NULL) | |
8526 | flags = 0; | |
8527 | else | |
e9ee469a | 8528 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
8529 | |
8530 | if (l->at == NULL) | |
8531 | at = 0; | |
8532 | else | |
e9ee469a | 8533 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 8534 | |
0aa7f586 AM |
8535 | if (!bfd_record_phdr (link_info.output_bfd, l->type, |
8536 | l->flags != NULL, flags, l->at != NULL, | |
8537 | at, l->filehdr, l->phdrs, c, secs)) | |
252b5132 RH |
8538 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
8539 | } | |
8540 | ||
8541 | free (secs); | |
8542 | ||
8543 | /* Make sure all the phdr assignments succeeded. */ | |
8ce18f9c | 8544 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8545 | os != NULL; |
8546 | os = os->next) | |
252b5132 | 8547 | { |
6bdafbeb | 8548 | lang_output_section_phdr_list *pl; |
252b5132 | 8549 | |
66c103b7 | 8550 | if (os->constraint < 0 |
afd7a018 | 8551 | || os->bfd_section == NULL) |
252b5132 RH |
8552 | continue; |
8553 | ||
afd7a018 | 8554 | for (pl = os->phdrs; |
252b5132 RH |
8555 | pl != NULL; |
8556 | pl = pl->next) | |
0aa7f586 | 8557 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 8558 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 8559 | os->name, pl->name); |
252b5132 RH |
8560 | } |
8561 | } | |
8562 | ||
8563 | /* Record a list of sections which may not be cross referenced. */ | |
8564 | ||
8565 | void | |
6bdafbeb | 8566 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
8567 | { |
8568 | struct lang_nocrossrefs *n; | |
8569 | ||
1e9cc1c2 | 8570 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
252b5132 RH |
8571 | n->next = nocrossref_list; |
8572 | n->list = l; | |
cdf96953 | 8573 | n->onlyfirst = FALSE; |
252b5132 RH |
8574 | nocrossref_list = n; |
8575 | ||
8576 | /* Set notice_all so that we get informed about all symbols. */ | |
b34976b6 | 8577 | link_info.notice_all = TRUE; |
252b5132 | 8578 | } |
cdf96953 MF |
8579 | |
8580 | /* Record a section that cannot be referenced from a list of sections. */ | |
8581 | ||
8582 | void | |
8583 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
8584 | { | |
8585 | lang_add_nocrossref (l); | |
8586 | nocrossref_list->onlyfirst = TRUE; | |
8587 | } | |
252b5132 RH |
8588 | \f |
8589 | /* Overlay handling. We handle overlays with some static variables. */ | |
8590 | ||
8591 | /* The overlay virtual address. */ | |
8592 | static etree_type *overlay_vma; | |
7e7d5768 AM |
8593 | /* And subsection alignment. */ |
8594 | static etree_type *overlay_subalign; | |
252b5132 | 8595 | |
252b5132 RH |
8596 | /* An expression for the maximum section size seen so far. */ |
8597 | static etree_type *overlay_max; | |
8598 | ||
8599 | /* A list of all the sections in this overlay. */ | |
8600 | ||
89cdebba | 8601 | struct overlay_list { |
252b5132 RH |
8602 | struct overlay_list *next; |
8603 | lang_output_section_statement_type *os; | |
8604 | }; | |
8605 | ||
8606 | static struct overlay_list *overlay_list; | |
8607 | ||
8608 | /* Start handling an overlay. */ | |
8609 | ||
8610 | void | |
7e7d5768 | 8611 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
8612 | { |
8613 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
8614 | ASSERT (overlay_vma == NULL |
8615 | && overlay_subalign == NULL | |
8616 | && overlay_max == NULL); | |
252b5132 RH |
8617 | |
8618 | overlay_vma = vma_expr; | |
7e7d5768 | 8619 | overlay_subalign = subalign; |
252b5132 RH |
8620 | } |
8621 | ||
8622 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
8623 | lang_enter_output_section_statement with the correct VMA. |
8624 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
8625 | |
8626 | void | |
1579bae1 | 8627 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
8628 | { |
8629 | struct overlay_list *n; | |
8630 | etree_type *size; | |
8631 | ||
152d792f | 8632 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
1eec346e | 8633 | 0, overlay_subalign, 0, 0, 0); |
252b5132 | 8634 | |
9f88b410 | 8635 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
8636 | sections on this one. This will work correctly even if `.' is |
8637 | used in the addresses. */ | |
8638 | if (overlay_list == NULL) | |
9f88b410 | 8639 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
8640 | |
8641 | /* Remember the section. */ | |
1e9cc1c2 | 8642 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
8643 | n->os = current_section; |
8644 | n->next = overlay_list; | |
8645 | overlay_list = n; | |
8646 | ||
8647 | size = exp_nameop (SIZEOF, name); | |
8648 | ||
252b5132 RH |
8649 | /* Arrange to work out the maximum section end address. */ |
8650 | if (overlay_max == NULL) | |
8651 | overlay_max = size; | |
8652 | else | |
8653 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
8654 | } | |
8655 | ||
8656 | /* Finish a section in an overlay. There isn't any special to do | |
8657 | here. */ | |
8658 | ||
8659 | void | |
1579bae1 | 8660 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 8661 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
8662 | { |
8663 | const char *name; | |
8664 | char *clean, *s2; | |
8665 | const char *s1; | |
8666 | char *buf; | |
8667 | ||
8668 | name = current_section->name; | |
8669 | ||
a747ee4d NC |
8670 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
8671 | region and that no load-time region has been specified. It doesn't | |
8672 | really matter what we say here, since lang_leave_overlay will | |
8673 | override it. */ | |
8674 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
8675 | |
8676 | /* Define the magic symbols. */ | |
8677 | ||
1e9cc1c2 | 8678 | clean = (char *) xmalloc (strlen (name) + 1); |
252b5132 RH |
8679 | s2 = clean; |
8680 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 8681 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
8682 | *s2++ = *s1; |
8683 | *s2 = '\0'; | |
8684 | ||
1e9cc1c2 | 8685 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
252b5132 | 8686 | sprintf (buf, "__load_start_%s", clean); |
34711ca3 AM |
8687 | lang_add_assignment (exp_provide (buf, |
8688 | exp_nameop (LOADADDR, name), | |
8689 | FALSE)); | |
252b5132 | 8690 | |
1e9cc1c2 | 8691 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
252b5132 | 8692 | sprintf (buf, "__load_stop_%s", clean); |
34711ca3 AM |
8693 | lang_add_assignment (exp_provide (buf, |
8694 | exp_binop ('+', | |
8695 | exp_nameop (LOADADDR, name), | |
8696 | exp_nameop (SIZEOF, name)), | |
8697 | FALSE)); | |
252b5132 RH |
8698 | |
8699 | free (clean); | |
8700 | } | |
8701 | ||
8702 | /* Finish an overlay. If there are any overlay wide settings, this | |
8703 | looks through all the sections in the overlay and sets them. */ | |
8704 | ||
8705 | void | |
1579bae1 AM |
8706 | lang_leave_overlay (etree_type *lma_expr, |
8707 | int nocrossrefs, | |
8708 | fill_type *fill, | |
8709 | const char *memspec, | |
6bdafbeb | 8710 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 8711 | const char *lma_memspec) |
252b5132 RH |
8712 | { |
8713 | lang_memory_region_type *region; | |
562d3460 | 8714 | lang_memory_region_type *lma_region; |
252b5132 | 8715 | struct overlay_list *l; |
6bdafbeb | 8716 | lang_nocrossref_type *nocrossref; |
252b5132 | 8717 | |
ee3cc2e2 RS |
8718 | lang_get_regions (®ion, &lma_region, |
8719 | memspec, lma_memspec, | |
6bdafbeb | 8720 | lma_expr != NULL, FALSE); |
562d3460 | 8721 | |
252b5132 RH |
8722 | nocrossref = NULL; |
8723 | ||
9f88b410 RS |
8724 | /* After setting the size of the last section, set '.' to end of the |
8725 | overlay region. */ | |
8726 | if (overlay_list != NULL) | |
6d8bf25d AM |
8727 | { |
8728 | overlay_list->os->update_dot = 1; | |
8729 | overlay_list->os->update_dot_tree | |
eb8476a6 | 8730 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), FALSE); |
6d8bf25d | 8731 | } |
9f88b410 | 8732 | |
252b5132 RH |
8733 | l = overlay_list; |
8734 | while (l != NULL) | |
8735 | { | |
8736 | struct overlay_list *next; | |
8737 | ||
1579bae1 | 8738 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 8739 | l->os->fill = fill; |
1545243b | 8740 | |
9f88b410 RS |
8741 | l->os->region = region; |
8742 | l->os->lma_region = lma_region; | |
8743 | ||
8744 | /* The first section has the load address specified in the | |
8745 | OVERLAY statement. The rest are worked out from that. | |
8746 | The base address is not needed (and should be null) if | |
8747 | an LMA region was specified. */ | |
8748 | if (l->next == 0) | |
152d792f AM |
8749 | { |
8750 | l->os->load_base = lma_expr; | |
7b243801 | 8751 | l->os->sectype = first_overlay_section; |
152d792f | 8752 | } |
252b5132 RH |
8753 | if (phdrs != NULL && l->os->phdrs == NULL) |
8754 | l->os->phdrs = phdrs; | |
8755 | ||
9f88b410 | 8756 | if (nocrossrefs) |
252b5132 | 8757 | { |
6bdafbeb | 8758 | lang_nocrossref_type *nc; |
252b5132 | 8759 | |
1e9cc1c2 | 8760 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
252b5132 RH |
8761 | nc->name = l->os->name; |
8762 | nc->next = nocrossref; | |
8763 | nocrossref = nc; | |
8764 | } | |
8765 | ||
8766 | next = l->next; | |
8767 | free (l); | |
8768 | l = next; | |
8769 | } | |
8770 | ||
8771 | if (nocrossref != NULL) | |
8772 | lang_add_nocrossref (nocrossref); | |
8773 | ||
252b5132 | 8774 | overlay_vma = NULL; |
252b5132 RH |
8775 | overlay_list = NULL; |
8776 | overlay_max = NULL; | |
8cb1232a | 8777 | overlay_subalign = NULL; |
252b5132 RH |
8778 | } |
8779 | \f | |
8780 | /* Version handling. This is only useful for ELF. */ | |
8781 | ||
108ba305 JJ |
8782 | /* If PREV is NULL, return first version pattern matching particular symbol. |
8783 | If PREV is non-NULL, return first version pattern matching particular | |
8784 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 8785 | |
108ba305 JJ |
8786 | static struct bfd_elf_version_expr * |
8787 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
8788 | struct bfd_elf_version_expr *prev, | |
8789 | const char *sym) | |
252b5132 | 8790 | { |
93252b1c | 8791 | const char *c_sym; |
108ba305 JJ |
8792 | const char *cxx_sym = sym; |
8793 | const char *java_sym = sym; | |
8794 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
8795 | enum demangling_styles curr_style; |
8796 | ||
8797 | curr_style = CURRENT_DEMANGLING_STYLE; | |
8798 | cplus_demangle_set_style (no_demangling); | |
8799 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
8800 | if (!c_sym) | |
8801 | c_sym = sym; | |
8802 | cplus_demangle_set_style (curr_style); | |
252b5132 | 8803 | |
108ba305 | 8804 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 8805 | { |
93252b1c MF |
8806 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
8807 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
8808 | if (!cxx_sym) |
8809 | cxx_sym = sym; | |
252b5132 | 8810 | } |
df816215 | 8811 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 8812 | { |
93252b1c | 8813 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
8814 | if (!java_sym) |
8815 | java_sym = sym; | |
252b5132 RH |
8816 | } |
8817 | ||
ae5a3597 | 8818 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 8819 | { |
108ba305 JJ |
8820 | struct bfd_elf_version_expr e; |
8821 | ||
8822 | switch (prev ? prev->mask : 0) | |
8823 | { | |
967928e9 AM |
8824 | case 0: |
8825 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
8826 | { | |
93252b1c | 8827 | e.pattern = c_sym; |
1e9cc1c2 | 8828 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8829 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 8830 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
8831 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
8832 | goto out_ret; | |
8833 | else | |
8834 | expr = expr->next; | |
8835 | } | |
8836 | /* Fallthrough */ | |
8837 | case BFD_ELF_VERSION_C_TYPE: | |
8838 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
8839 | { | |
ae5a3597 | 8840 | e.pattern = cxx_sym; |
1e9cc1c2 | 8841 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8842 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8843 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
8844 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
8845 | goto out_ret; | |
8846 | else | |
8847 | expr = expr->next; | |
8848 | } | |
8849 | /* Fallthrough */ | |
8850 | case BFD_ELF_VERSION_CXX_TYPE: | |
8851 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
8852 | { | |
ae5a3597 | 8853 | e.pattern = java_sym; |
1e9cc1c2 | 8854 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8855 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8856 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
8857 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
8858 | goto out_ret; | |
8859 | else | |
8860 | expr = expr->next; | |
8861 | } | |
8862 | /* Fallthrough */ | |
8863 | default: | |
8864 | break; | |
108ba305 | 8865 | } |
252b5132 | 8866 | } |
108ba305 JJ |
8867 | |
8868 | /* Finally, try the wildcards. */ | |
ae5a3597 | 8869 | if (prev == NULL || prev->literal) |
108ba305 | 8870 | expr = head->remaining; |
252b5132 | 8871 | else |
108ba305 | 8872 | expr = prev->next; |
86043bbb | 8873 | for (; expr; expr = expr->next) |
252b5132 | 8874 | { |
108ba305 JJ |
8875 | const char *s; |
8876 | ||
86043bbb MM |
8877 | if (!expr->pattern) |
8878 | continue; | |
8879 | ||
108ba305 JJ |
8880 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
8881 | break; | |
8882 | ||
8883 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
8884 | s = java_sym; | |
8885 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
8886 | s = cxx_sym; | |
8887 | else | |
93252b1c | 8888 | s = c_sym; |
5e35cbc2 | 8889 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 8890 | break; |
252b5132 RH |
8891 | } |
8892 | ||
967928e9 | 8893 | out_ret: |
93252b1c MF |
8894 | if (c_sym != sym) |
8895 | free ((char *) c_sym); | |
108ba305 JJ |
8896 | if (cxx_sym != sym) |
8897 | free ((char *) cxx_sym); | |
8898 | if (java_sym != sym) | |
8899 | free ((char *) java_sym); | |
8900 | return expr; | |
252b5132 RH |
8901 | } |
8902 | ||
5e35cbc2 | 8903 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 8904 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
8905 | |
8906 | static const char * | |
8907 | realsymbol (const char *pattern) | |
8908 | { | |
8909 | const char *p; | |
8910 | bfd_boolean changed = FALSE, backslash = FALSE; | |
1e9cc1c2 | 8911 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
8912 | |
8913 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
8914 | { | |
8915 | /* It is a glob pattern only if there is no preceding | |
8916 | backslash. */ | |
5e35cbc2 L |
8917 | if (backslash) |
8918 | { | |
8919 | /* Remove the preceding backslash. */ | |
8920 | *(s - 1) = *p; | |
ae5a3597 | 8921 | backslash = FALSE; |
5e35cbc2 L |
8922 | changed = TRUE; |
8923 | } | |
8924 | else | |
ae5a3597 AM |
8925 | { |
8926 | if (*p == '?' || *p == '*' || *p == '[') | |
8927 | { | |
8928 | free (symbol); | |
8929 | return NULL; | |
8930 | } | |
5e35cbc2 | 8931 | |
ae5a3597 AM |
8932 | *s++ = *p; |
8933 | backslash = *p == '\\'; | |
8934 | } | |
5e35cbc2 L |
8935 | } |
8936 | ||
8937 | if (changed) | |
8938 | { | |
8939 | *s = '\0'; | |
8940 | return symbol; | |
8941 | } | |
8942 | else | |
8943 | { | |
8944 | free (symbol); | |
8945 | return pattern; | |
8946 | } | |
8947 | } | |
8948 | ||
d3ce72d0 | 8949 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
8950 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
8951 | pattern to be matched against symbol names. */ | |
252b5132 RH |
8952 | |
8953 | struct bfd_elf_version_expr * | |
1579bae1 | 8954 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 8955 | const char *new_name, |
86043bbb MM |
8956 | const char *lang, |
8957 | bfd_boolean literal_p) | |
252b5132 RH |
8958 | { |
8959 | struct bfd_elf_version_expr *ret; | |
8960 | ||
d3ce72d0 | 8961 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
252b5132 | 8962 | ret->next = orig; |
31941635 L |
8963 | ret->symver = 0; |
8964 | ret->script = 0; | |
ae5a3597 | 8965 | ret->literal = TRUE; |
d3ce72d0 | 8966 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
8967 | if (ret->pattern == NULL) |
8968 | { | |
d3ce72d0 | 8969 | ret->pattern = new_name; |
ae5a3597 AM |
8970 | ret->literal = FALSE; |
8971 | } | |
252b5132 RH |
8972 | |
8973 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 8974 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 8975 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 8976 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 8977 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 8978 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
8979 | else |
8980 | { | |
8981 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
8982 | lang); | |
108ba305 | 8983 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
8984 | } |
8985 | ||
fac1652d | 8986 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
8987 | } |
8988 | ||
8989 | /* This is called for each set of variable names and match | |
8990 | expressions. */ | |
8991 | ||
8992 | struct bfd_elf_version_tree * | |
1579bae1 AM |
8993 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
8994 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
8995 | { |
8996 | struct bfd_elf_version_tree *ret; | |
8997 | ||
1e9cc1c2 | 8998 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
108ba305 JJ |
8999 | ret->globals.list = globals; |
9000 | ret->locals.list = locals; | |
9001 | ret->match = lang_vers_match; | |
252b5132 | 9002 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
9003 | return ret; |
9004 | } | |
9005 | ||
9006 | /* This static variable keeps track of version indices. */ | |
9007 | ||
9008 | static int version_index; | |
9009 | ||
108ba305 JJ |
9010 | static hashval_t |
9011 | version_expr_head_hash (const void *p) | |
9012 | { | |
1e9cc1c2 NC |
9013 | const struct bfd_elf_version_expr *e = |
9014 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 9015 | |
ae5a3597 | 9016 | return htab_hash_string (e->pattern); |
108ba305 JJ |
9017 | } |
9018 | ||
9019 | static int | |
9020 | version_expr_head_eq (const void *p1, const void *p2) | |
9021 | { | |
1e9cc1c2 NC |
9022 | const struct bfd_elf_version_expr *e1 = |
9023 | (const struct bfd_elf_version_expr *) p1; | |
9024 | const struct bfd_elf_version_expr *e2 = | |
9025 | (const struct bfd_elf_version_expr *) p2; | |
108ba305 | 9026 | |
ae5a3597 | 9027 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
9028 | } |
9029 | ||
9030 | static void | |
9031 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
9032 | { | |
9033 | size_t count = 0; | |
9034 | struct bfd_elf_version_expr *e, *next; | |
9035 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
9036 | ||
9037 | for (e = head->list; e; e = e->next) | |
9038 | { | |
ae5a3597 | 9039 | if (e->literal) |
108ba305 JJ |
9040 | count++; |
9041 | head->mask |= e->mask; | |
9042 | } | |
9043 | ||
9044 | if (count) | |
9045 | { | |
9046 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
9047 | version_expr_head_eq, NULL); | |
9048 | list_loc = &head->list; | |
9049 | remaining_loc = &head->remaining; | |
9050 | for (e = head->list; e; e = next) | |
9051 | { | |
9052 | next = e->next; | |
ae5a3597 | 9053 | if (!e->literal) |
108ba305 JJ |
9054 | { |
9055 | *remaining_loc = e; | |
9056 | remaining_loc = &e->next; | |
9057 | } | |
9058 | else | |
9059 | { | |
1e9cc1c2 | 9060 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
9061 | |
9062 | if (*loc) | |
9063 | { | |
9064 | struct bfd_elf_version_expr *e1, *last; | |
9065 | ||
1e9cc1c2 | 9066 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
9067 | last = NULL; |
9068 | do | |
9069 | { | |
9070 | if (e1->mask == e->mask) | |
9071 | { | |
9072 | last = NULL; | |
9073 | break; | |
9074 | } | |
9075 | last = e1; | |
9076 | e1 = e1->next; | |
9077 | } | |
ae5a3597 | 9078 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
9079 | |
9080 | if (last == NULL) | |
9081 | { | |
9082 | /* This is a duplicate. */ | |
9083 | /* FIXME: Memory leak. Sometimes pattern is not | |
9084 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 9085 | /* free (e->pattern); */ |
108ba305 JJ |
9086 | free (e); |
9087 | } | |
9088 | else | |
9089 | { | |
9090 | e->next = last->next; | |
9091 | last->next = e; | |
9092 | } | |
9093 | } | |
9094 | else | |
9095 | { | |
9096 | *loc = e; | |
9097 | *list_loc = e; | |
9098 | list_loc = &e->next; | |
9099 | } | |
9100 | } | |
9101 | } | |
9102 | *remaining_loc = NULL; | |
9103 | *list_loc = head->remaining; | |
9104 | } | |
9105 | else | |
9106 | head->remaining = head->list; | |
9107 | } | |
9108 | ||
252b5132 RH |
9109 | /* This is called when we know the name and dependencies of the |
9110 | version. */ | |
9111 | ||
9112 | void | |
1579bae1 AM |
9113 | lang_register_vers_node (const char *name, |
9114 | struct bfd_elf_version_tree *version, | |
9115 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
9116 | { |
9117 | struct bfd_elf_version_tree *t, **pp; | |
9118 | struct bfd_elf_version_expr *e1; | |
9119 | ||
6b9b879a JJ |
9120 | if (name == NULL) |
9121 | name = ""; | |
9122 | ||
fd91d419 L |
9123 | if (link_info.version_info != NULL |
9124 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 9125 | { |
6feb9908 AM |
9126 | einfo (_("%X%P: anonymous version tag cannot be combined" |
9127 | " with other version tags\n")); | |
5ed6aba4 | 9128 | free (version); |
6b9b879a JJ |
9129 | return; |
9130 | } | |
9131 | ||
252b5132 | 9132 | /* Make sure this node has a unique name. */ |
fd91d419 | 9133 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9134 | if (strcmp (t->name, name) == 0) |
9135 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9136 | ||
108ba305 JJ |
9137 | lang_finalize_version_expr_head (&version->globals); |
9138 | lang_finalize_version_expr_head (&version->locals); | |
9139 | ||
252b5132 RH |
9140 | /* Check the global and local match names, and make sure there |
9141 | aren't any duplicates. */ | |
9142 | ||
108ba305 | 9143 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9144 | { |
fd91d419 | 9145 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9146 | { |
9147 | struct bfd_elf_version_expr *e2; | |
9148 | ||
ae5a3597 | 9149 | if (t->locals.htab && e1->literal) |
108ba305 | 9150 | { |
1e9cc1c2 | 9151 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9152 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 9153 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9154 | { |
9155 | if (e1->mask == e2->mask) | |
6feb9908 | 9156 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 9157 | " in version information\n"), e1->pattern); |
108ba305 JJ |
9158 | e2 = e2->next; |
9159 | } | |
9160 | } | |
ae5a3597 | 9161 | else if (!e1->literal) |
108ba305 | 9162 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9163 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9164 | && e1->mask == e2->mask) | |
9165 | einfo (_("%X%P: duplicate expression `%s'" | |
9166 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9167 | } |
9168 | } | |
9169 | ||
108ba305 | 9170 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9171 | { |
fd91d419 | 9172 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9173 | { |
9174 | struct bfd_elf_version_expr *e2; | |
9175 | ||
ae5a3597 | 9176 | if (t->globals.htab && e1->literal) |
108ba305 | 9177 | { |
1e9cc1c2 | 9178 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9179 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9180 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9181 | { |
9182 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9183 | einfo (_("%X%P: duplicate expression `%s'" |
9184 | " in version information\n"), | |
ae5a3597 | 9185 | e1->pattern); |
108ba305 JJ |
9186 | e2 = e2->next; |
9187 | } | |
9188 | } | |
ae5a3597 | 9189 | else if (!e1->literal) |
108ba305 | 9190 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9191 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9192 | && e1->mask == e2->mask) | |
9193 | einfo (_("%X%P: duplicate expression `%s'" | |
9194 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9195 | } |
9196 | } | |
9197 | ||
9198 | version->deps = deps; | |
9199 | version->name = name; | |
6b9b879a JJ |
9200 | if (name[0] != '\0') |
9201 | { | |
9202 | ++version_index; | |
9203 | version->vernum = version_index; | |
9204 | } | |
9205 | else | |
9206 | version->vernum = 0; | |
252b5132 | 9207 | |
fd91d419 | 9208 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9209 | ; |
9210 | *pp = version; | |
9211 | } | |
9212 | ||
9213 | /* This is called when we see a version dependency. */ | |
9214 | ||
9215 | struct bfd_elf_version_deps * | |
1579bae1 | 9216 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9217 | { |
9218 | struct bfd_elf_version_deps *ret; | |
9219 | struct bfd_elf_version_tree *t; | |
9220 | ||
1e9cc1c2 | 9221 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
252b5132 RH |
9222 | ret->next = list; |
9223 | ||
fd91d419 | 9224 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9225 | { |
9226 | if (strcmp (t->name, name) == 0) | |
9227 | { | |
9228 | ret->version_needed = t; | |
9229 | return ret; | |
9230 | } | |
9231 | } | |
9232 | ||
9233 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9234 | ||
1e0061d2 | 9235 | ret->version_needed = NULL; |
252b5132 RH |
9236 | return ret; |
9237 | } | |
9238 | ||
9239 | static void | |
1579bae1 | 9240 | lang_do_version_exports_section (void) |
252b5132 RH |
9241 | { |
9242 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9243 | ||
9244 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9245 | { | |
9246 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9247 | char *contents, *p; | |
9248 | bfd_size_type len; | |
9249 | ||
9250 | if (sec == NULL) | |
b7a26f91 | 9251 | continue; |
252b5132 | 9252 | |
eea6121a | 9253 | len = sec->size; |
1e9cc1c2 | 9254 | contents = (char *) xmalloc (len); |
252b5132 | 9255 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9256 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9257 | |
9258 | p = contents; | |
89cdebba | 9259 | while (p < contents + len) |
252b5132 | 9260 | { |
86043bbb | 9261 | greg = lang_new_vers_pattern (greg, p, NULL, FALSE); |
252b5132 RH |
9262 | p = strchr (p, '\0') + 1; |
9263 | } | |
9264 | ||
9265 | /* Do not free the contents, as we used them creating the regex. */ | |
9266 | ||
9267 | /* Do not include this section in the link. */ | |
a14a5de3 | 9268 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9269 | } |
9270 | ||
86043bbb | 9271 | lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE); |
252b5132 RH |
9272 | lang_register_vers_node (command_line.version_exports_section, |
9273 | lang_new_vers_node (greg, lreg), NULL); | |
9274 | } | |
577a0623 | 9275 | |
cc9ad334 SKS |
9276 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */ |
9277 | ||
9278 | static void | |
9279 | lang_do_memory_regions (void) | |
9280 | { | |
9281 | lang_memory_region_type *r = lang_memory_region_list; | |
9282 | ||
9283 | for (; r != NULL; r = r->next) | |
9284 | { | |
9285 | if (r->origin_exp) | |
0aa7f586 AM |
9286 | { |
9287 | exp_fold_tree_no_dot (r->origin_exp); | |
9288 | if (expld.result.valid_p) | |
9289 | { | |
9290 | r->origin = expld.result.value; | |
9291 | r->current = r->origin; | |
9292 | } | |
9293 | else | |
9294 | einfo (_("%F%P: invalid origin for memory region %s\n"), | |
9295 | r->name_list.name); | |
9296 | } | |
cc9ad334 | 9297 | if (r->length_exp) |
0aa7f586 AM |
9298 | { |
9299 | exp_fold_tree_no_dot (r->length_exp); | |
9300 | if (expld.result.valid_p) | |
9301 | r->length = expld.result.value; | |
9302 | else | |
9303 | einfo (_("%F%P: invalid length for memory region %s\n"), | |
9304 | r->name_list.name); | |
9305 | } | |
cc9ad334 SKS |
9306 | } |
9307 | } | |
9308 | ||
577a0623 | 9309 | void |
1579bae1 | 9310 | lang_add_unique (const char *name) |
577a0623 AM |
9311 | { |
9312 | struct unique_sections *ent; | |
9313 | ||
9314 | for (ent = unique_section_list; ent; ent = ent->next) | |
9315 | if (strcmp (ent->name, name) == 0) | |
9316 | return; | |
9317 | ||
1e9cc1c2 | 9318 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
577a0623 AM |
9319 | ent->name = xstrdup (name); |
9320 | ent->next = unique_section_list; | |
9321 | unique_section_list = ent; | |
9322 | } | |
55255dae L |
9323 | |
9324 | /* Append the list of dynamic symbols to the existing one. */ | |
9325 | ||
9326 | void | |
9327 | lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic) | |
9328 | { | |
40b36307 | 9329 | if (link_info.dynamic_list) |
55255dae | 9330 | { |
07806542 JJ |
9331 | struct bfd_elf_version_expr *tail; |
9332 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
9333 | ; | |
40b36307 L |
9334 | tail->next = link_info.dynamic_list->head.list; |
9335 | link_info.dynamic_list->head.list = dynamic; | |
55255dae L |
9336 | } |
9337 | else | |
9338 | { | |
9339 | struct bfd_elf_dynamic_list *d; | |
9340 | ||
1e9cc1c2 | 9341 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
55255dae L |
9342 | d->head.list = dynamic; |
9343 | d->match = lang_vers_match; | |
40b36307 | 9344 | link_info.dynamic_list = d; |
55255dae L |
9345 | } |
9346 | } | |
9347 | ||
9348 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
9349 | one. */ | |
9350 | ||
9351 | void | |
9352 | lang_append_dynamic_list_cpp_typeinfo (void) | |
9353 | { | |
0aa7f586 | 9354 | const char *symbols[] = |
55255dae L |
9355 | { |
9356 | "typeinfo name for*", | |
9357 | "typeinfo for*" | |
9358 | }; | |
9359 | struct bfd_elf_version_expr *dynamic = NULL; | |
9360 | unsigned int i; | |
9361 | ||
9362 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9363 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
9364 | FALSE); | |
9365 | ||
9366 | lang_append_dynamic_list (dynamic); | |
9367 | } | |
40b36307 L |
9368 | |
9369 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
9370 | existing one. */ | |
9371 | ||
9372 | void | |
9373 | lang_append_dynamic_list_cpp_new (void) | |
9374 | { | |
0aa7f586 | 9375 | const char *symbols[] = |
40b36307 L |
9376 | { |
9377 | "operator new*", | |
9378 | "operator delete*" | |
9379 | }; | |
9380 | struct bfd_elf_version_expr *dynamic = NULL; | |
9381 | unsigned int i; | |
9382 | ||
9383 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9384 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
9385 | FALSE); | |
9386 | ||
9387 | lang_append_dynamic_list (dynamic); | |
9388 | } | |
01554a74 AM |
9389 | |
9390 | /* Scan a space and/or comma separated string of features. */ | |
9391 | ||
9392 | void | |
9393 | lang_ld_feature (char *str) | |
9394 | { | |
9395 | char *p, *q; | |
9396 | ||
9397 | p = str; | |
9398 | while (*p) | |
9399 | { | |
9400 | char sep; | |
9401 | while (*p == ',' || ISSPACE (*p)) | |
9402 | ++p; | |
9403 | if (!*p) | |
9404 | break; | |
9405 | q = p + 1; | |
9406 | while (*q && *q != ',' && !ISSPACE (*q)) | |
9407 | ++q; | |
9408 | sep = *q; | |
9409 | *q = 0; | |
9410 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
9411 | config.sane_expr = TRUE; | |
9412 | else | |
9413 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
9414 | *q = sep; | |
9415 | p = q; | |
9416 | } | |
9417 | } | |
3604cb1f TG |
9418 | |
9419 | /* Pretty print memory amount. */ | |
9420 | ||
9421 | static void | |
9422 | lang_print_memory_size (bfd_vma sz) | |
9423 | { | |
9424 | if ((sz & 0x3fffffff) == 0) | |
9425 | printf ("%10" BFD_VMA_FMT "u GB", sz >> 30); | |
9426 | else if ((sz & 0xfffff) == 0) | |
9427 | printf ("%10" BFD_VMA_FMT "u MB", sz >> 20); | |
9428 | else if ((sz & 0x3ff) == 0) | |
9429 | printf ("%10" BFD_VMA_FMT "u KB", sz >> 10); | |
9430 | else | |
9431 | printf (" %10" BFD_VMA_FMT "u B", sz); | |
9432 | } | |
9433 | ||
9434 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
9435 | ||
9436 | void | |
9437 | lang_print_memory_usage (void) | |
9438 | { | |
9439 | lang_memory_region_type *r; | |
9440 | ||
9441 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
9442 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
9443 | { | |
9444 | bfd_vma used_length = r->current - r->origin; | |
3604cb1f TG |
9445 | |
9446 | printf ("%16s: ",r->name_list.name); | |
9447 | lang_print_memory_size (used_length); | |
9448 | lang_print_memory_size ((bfd_vma) r->length); | |
9449 | ||
1769380a AM |
9450 | if (r->length != 0) |
9451 | { | |
9452 | double percent = used_length * 100.0 / r->length; | |
2410edcd | 9453 | printf (" %6.2f%%", percent); |
1769380a | 9454 | } |
2410edcd | 9455 | printf ("\n"); |
3604cb1f TG |
9456 | } |
9457 | } |