]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
d87bef3a | 2 | Copyright (C) 1991-2023 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 | #include "ld.h" |
31 | #include "ldmain.h" | |
252b5132 RH |
32 | #include "ldexp.h" |
33 | #include "ldlang.h" | |
df2a7313 | 34 | #include <ldgram.h> |
252b5132 RH |
35 | #include "ldlex.h" |
36 | #include "ldmisc.h" | |
37 | #include "ldctor.h" | |
38 | #include "ldfile.h" | |
b71e2778 | 39 | #include "ldemul.h" |
252b5132 RH |
40 | #include "fnmatch.h" |
41 | #include "demangle.h" | |
108ba305 | 42 | #include "hashtab.h" |
0fef4b98 | 43 | #include "elf-bfd.h" |
2d5783fa NC |
44 | #include "bfdver.h" |
45 | ||
0381901e | 46 | #if BFD_SUPPORTS_PLUGINS |
5d3236ee | 47 | #include "plugin.h" |
2d5783fa | 48 | #endif |
252b5132 | 49 | |
7abb6dea | 50 | #ifndef offsetof |
cf888e70 | 51 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
52 | #endif |
53 | ||
fc90c280 AM |
54 | /* Convert between addresses in bytes and sizes in octets. |
55 | For currently supported targets, octets_per_byte is always a power | |
56 | of two, so we can use shifts. */ | |
57 | #define TO_ADDR(X) ((X) >> opb_shift) | |
58 | #define TO_SIZE(X) ((X) << opb_shift) | |
59 | ||
60 | /* Local variables. */ | |
252b5132 | 61 | static struct obstack stat_obstack; |
35835446 | 62 | static struct obstack map_obstack; |
b1eecf6f | 63 | static struct obstack pt_obstack; |
252b5132 RH |
64 | |
65 | #define obstack_chunk_alloc xmalloc | |
66 | #define obstack_chunk_free free | |
8be573a7 | 67 | static const char *entry_symbol_default = "start"; |
f38a2680 | 68 | static bool map_head_is_link_order = false; |
252b5132 | 69 | static lang_output_section_statement_type *default_common_section; |
f38a2680 | 70 | static bool map_option_f; |
252b5132 RH |
71 | static bfd_vma print_dot; |
72 | static lang_input_statement_type *first_file; | |
5f992e62 | 73 | static const char *current_target; |
bde18da4 AM |
74 | static lang_statement_list_type *stat_save[10]; |
75 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
c4b78195 | 76 | static struct unique_sections *unique_section_list; |
16e4ecc0 | 77 | static struct asneeded_minfo *asneeded_list_head; |
fc90c280 | 78 | static unsigned int opb_shift = 0; |
252b5132 | 79 | |
cf888e70 | 80 | /* Forward declarations. */ |
a431bc2e | 81 | static void exp_init_os (etree_type *); |
1579bae1 | 82 | static lang_input_statement_type *lookup_name (const char *); |
670c91c0 MM |
83 | static bool wont_add_section_p (asection *, |
84 | lang_output_section_statement_type *); | |
1579bae1 | 85 | static void insert_undefined (const char *); |
f38a2680 | 86 | static bool sort_def_symbol (struct bfd_link_hash_entry *, void *); |
b1eecf6f MM |
87 | static lang_statement_union_type *new_statement (enum statement_enum type, |
88 | size_t size, | |
89 | lang_statement_list_type *list); | |
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); | |
f38a2680 AM |
95 | static void print_input_section (asection *, bool); |
96 | static bool lang_one_common (struct bfd_link_hash_entry *, void *); | |
1579bae1 AM |
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 *); | |
f38a2680 | 101 | static void lang_do_memory_regions (bool); |
1579bae1 | 102 | |
cf888e70 | 103 | /* Exported variables. */ |
8be573a7 | 104 | const char *output_target; |
252b5132 | 105 | lang_output_section_statement_type *abs_output_section; |
b209b5a6 AM |
106 | /* Header for list of statements corresponding to any files involved in the |
107 | link, either specified from the command-line or added implicitely (eg. | |
108 | archive member used to resolved undefined symbol, wildcard statement from | |
109 | linker script, etc.). Next pointer is in next field of a | |
110 | lang_statement_header_type (reached via header field in a | |
111 | lang_statement_union). */ | |
112 | lang_statement_list_type statement_list; | |
5c1e6d53 | 113 | lang_statement_list_type lang_os_list; |
252b5132 | 114 | lang_statement_list_type *stat_ptr = &statement_list; |
e368bf56 TP |
115 | /* Header for list of statements corresponding to files used in the final |
116 | executable. This can be either object file specified on the command-line | |
117 | or library member resolving an undefined reference. Next pointer is in next | |
118 | field of a lang_input_statement_type (reached via input_statement field in a | |
119 | lang_statement_union). */ | |
87f2a346 | 120 | lang_statement_list_type file_chain = { NULL, NULL }; |
e368bf56 TP |
121 | /* Header for list of statements corresponding to files specified on the |
122 | command-line for linking. It thus contains real object files and archive | |
123 | but not archive members. Next pointer is in next_real_file field of a | |
124 | lang_input_statement_type statement (reached via input_statement field in a | |
125 | lang_statement_union). */ | |
dc27aea4 | 126 | lang_statement_list_type input_file_chain; |
16171946 | 127 | static const char *current_input_file; |
37a141bf | 128 | struct bfd_elf_dynamic_list **current_dynamic_list_p; |
e3e942e9 | 129 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 130 | const char *entry_section = ".text"; |
66be1055 | 131 | struct lang_input_statement_flags input_flags; |
f38a2680 AM |
132 | bool entry_from_cmdline; |
133 | bool lang_has_input_file = false; | |
134 | bool had_output_filename = false; | |
135 | bool lang_float_flag = false; | |
136 | bool delete_output_file_on_failure = false; | |
2d5783fa | 137 | bool enable_linker_version = false; |
f5ff60a6 | 138 | struct lang_phdr *lang_phdr_list; |
252b5132 | 139 | struct lang_nocrossrefs *nocrossref_list; |
16e4ecc0 | 140 | struct asneeded_minfo **asneeded_list_tail; |
094e34f2 | 141 | #ifdef ENABLE_LIBCTF |
139633c3 | 142 | static ctf_dict_t *ctf_output; |
094e34f2 | 143 | #endif |
e9ee469a | 144 | |
baf09cba AM |
145 | /* Functions that traverse the linker script and might evaluate |
146 | DEFINED() need to increment this at the start of the traversal. */ | |
420e579c | 147 | int lang_statement_iteration = 0; |
252b5132 | 148 | |
baf09cba AM |
149 | /* Count times through one_lang_size_sections_pass after mark phase. */ |
150 | static int lang_sizing_iteration = 0; | |
151 | ||
61f5d054 L |
152 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
153 | Although backslashes are treated specially if a pattern contains | |
154 | wildcards, we do not consider the mere presence of a backslash to | |
155 | be enough to cause the pattern to be treated as a wildcard. | |
156 | That lets us handle DOS filenames more naturally. */ | |
157 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
158 | ||
d1778b88 | 159 | #define new_stat(x, y) \ |
1579bae1 | 160 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 161 | |
d1778b88 AM |
162 | #define outside_section_address(q) \ |
163 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 164 | |
d1778b88 AM |
165 | #define outside_symbol_address(q) \ |
166 | ((q)->value + outside_section_address (q->section)) | |
252b5132 | 167 | |
1ff6de03 NA |
168 | /* CTF sections smaller than this are not compressed: compression of |
169 | dictionaries this small doesn't gain much, and this lets consumers mmap the | |
170 | sections directly out of the ELF file and use them with no decompression | |
171 | overhead if they want to. */ | |
172 | #define CTF_COMPRESSION_THRESHOLD 4096 | |
173 | ||
1579bae1 AM |
174 | void * |
175 | stat_alloc (size_t size) | |
252b5132 RH |
176 | { |
177 | return obstack_alloc (&stat_obstack, size); | |
178 | } | |
179 | ||
049522ca MM |
180 | /* Code for handling simple wildcards without going through fnmatch, |
181 | which can be expensive because of charset translations etc. */ | |
182 | ||
183 | /* A simple wild is a literal string followed by a single '*', | |
184 | where the literal part is at least 4 characters long. */ | |
185 | ||
186 | static bool | |
187 | is_simple_wild (const char *name) | |
188 | { | |
189 | size_t len = strcspn (name, "*?["); | |
190 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
191 | } | |
192 | ||
193 | static bool | |
194 | match_simple_wild (const char *pattern, const char *name) | |
195 | { | |
196 | /* The first four characters of the pattern are guaranteed valid | |
197 | non-wildcard characters. So we can go faster. */ | |
198 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
199 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
200 | return false; | |
201 | ||
202 | pattern += 4; | |
203 | name += 4; | |
204 | while (*pattern != '*') | |
205 | if (*name++ != *pattern++) | |
206 | return false; | |
207 | ||
208 | return true; | |
209 | } | |
210 | ||
97407faf AM |
211 | static int |
212 | name_match (const char *pattern, const char *name) | |
213 | { | |
049522ca MM |
214 | if (is_simple_wild (pattern)) |
215 | return !match_simple_wild (pattern, name); | |
97407faf AM |
216 | if (wildcardp (pattern)) |
217 | return fnmatch (pattern, name, 0); | |
218 | return strcmp (pattern, name); | |
219 | } | |
220 | ||
049522ca MM |
221 | /* Given an analyzed wildcard_spec SPEC, match it against NAME, |
222 | returns zero on a match, non-zero if there's no match. */ | |
223 | ||
224 | static int | |
225 | spec_match (const struct wildcard_spec *spec, const char *name) | |
226 | { | |
227 | size_t nl = spec->namelen; | |
228 | size_t pl = spec->prefixlen; | |
229 | size_t sl = spec->suffixlen; | |
3bf5bf54 | 230 | size_t inputlen = strlen (name); |
049522ca | 231 | int r; |
3bf5bf54 NC |
232 | |
233 | if (pl) | |
234 | { | |
235 | if (inputlen < pl) | |
236 | return 1; | |
237 | ||
238 | r = memcmp (spec->name, name, pl); | |
239 | if (r) | |
240 | return r; | |
241 | } | |
242 | ||
049522ca MM |
243 | if (sl) |
244 | { | |
049522ca MM |
245 | if (inputlen < sl) |
246 | return 1; | |
3bf5bf54 | 247 | |
049522ca MM |
248 | r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl); |
249 | if (r) | |
250 | return r; | |
251 | } | |
3bf5bf54 | 252 | |
049522ca MM |
253 | if (nl == pl + sl + 1 && spec->name[pl] == '*') |
254 | return 0; | |
3bf5bf54 NC |
255 | |
256 | if (nl > pl) | |
049522ca | 257 | return fnmatch (spec->name + pl, name + pl, 0); |
3bf5bf54 NC |
258 | |
259 | if (inputlen >= nl) | |
260 | return name[nl]; | |
261 | ||
262 | return 0; | |
049522ca MM |
263 | } |
264 | ||
16171946 FS |
265 | static char * |
266 | ldirname (const char *name) | |
267 | { | |
268 | const char *base = lbasename (name); | |
269 | char *dirname; | |
270 | ||
271 | while (base > name && IS_DIR_SEPARATOR (base[-1])) | |
272 | --base; | |
273 | if (base == name) | |
274 | return strdup ("."); | |
275 | dirname = strdup (name); | |
276 | dirname[base - name] = '\0'; | |
277 | return dirname; | |
278 | } | |
279 | ||
97407faf AM |
280 | /* If PATTERN is of the form archive:file, return a pointer to the |
281 | separator. If not, return NULL. */ | |
282 | ||
283 | static char * | |
284 | archive_path (const char *pattern) | |
285 | { | |
286 | char *p = NULL; | |
287 | ||
288 | if (link_info.path_separator == 0) | |
289 | return p; | |
290 | ||
291 | p = strchr (pattern, link_info.path_separator); | |
292 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
293 | if (p == NULL || link_info.path_separator != ':') | |
294 | return p; | |
295 | ||
296 | /* Assume a match on the second char is part of drive specifier, | |
297 | as in "c:\silly.dos". */ | |
967928e9 | 298 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
299 | p = strchr (p + 1, link_info.path_separator); |
300 | #endif | |
301 | return p; | |
302 | } | |
303 | ||
967928e9 AM |
304 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
305 | return whether F matches FILE_SPEC. */ | |
306 | ||
f38a2680 | 307 | static bool |
967928e9 AM |
308 | input_statement_is_archive_path (const char *file_spec, char *sep, |
309 | lang_input_statement_type *f) | |
310 | { | |
f38a2680 | 311 | bool match = false; |
967928e9 AM |
312 | |
313 | if ((*(sep + 1) == 0 | |
314 | || name_match (sep + 1, f->filename) == 0) | |
315 | && ((sep != file_spec) | |
316 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
317 | { | |
f38a2680 | 318 | match = true; |
967928e9 AM |
319 | |
320 | if (sep != file_spec) | |
321 | { | |
607b4833 | 322 | const char *aname = bfd_get_filename (f->the_bfd->my_archive); |
967928e9 AM |
323 | *sep = 0; |
324 | match = name_match (file_spec, aname) == 0; | |
325 | *sep = link_info.path_separator; | |
326 | } | |
327 | } | |
328 | return match; | |
329 | } | |
330 | ||
f38a2680 | 331 | static bool |
d0bf826b AM |
332 | unique_section_p (const asection *sec, |
333 | const lang_output_section_statement_type *os) | |
577a0623 AM |
334 | { |
335 | struct unique_sections *unam; | |
d0d6a25b | 336 | const char *secnam; |
577a0623 | 337 | |
7bdf4127 | 338 | if (!link_info.resolve_section_groups |
d0d6a25b AM |
339 | && sec->owner != NULL |
340 | && bfd_is_group_section (sec->owner, sec)) | |
d0bf826b AM |
341 | return !(os != NULL |
342 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
d0d6a25b AM |
343 | |
344 | secnam = sec->name; | |
577a0623 | 345 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf | 346 | if (name_match (unam->name, secnam) == 0) |
f38a2680 | 347 | return true; |
577a0623 | 348 | |
f38a2680 | 349 | return false; |
577a0623 AM |
350 | } |
351 | ||
08da4cac | 352 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 353 | |
8f1732fc AB |
354 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return |
355 | false. */ | |
72223188 | 356 | |
f38a2680 | 357 | static bool |
8f1732fc | 358 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, |
6c19b93b | 359 | lang_input_statement_type *file) |
4dec4d4e | 360 | { |
72223188 JJ |
361 | struct name_list *list_tmp; |
362 | ||
8f1732fc | 363 | for (list_tmp = exclude_list; |
72223188 JJ |
364 | list_tmp; |
365 | list_tmp = list_tmp->next) | |
366 | { | |
967928e9 | 367 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 368 | |
967928e9 | 369 | if (p != NULL) |
72223188 | 370 | { |
967928e9 | 371 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
f38a2680 | 372 | return true; |
72223188 JJ |
373 | } |
374 | ||
967928e9 | 375 | else if (name_match (list_tmp->name, file->filename) == 0) |
f38a2680 | 376 | return true; |
967928e9 AM |
377 | |
378 | /* FIXME: Perhaps remove the following at some stage? Matching | |
379 | unadorned archives like this was never documented and has | |
380 | been superceded by the archive:path syntax. */ | |
381 | else if (file->the_bfd != NULL | |
382 | && file->the_bfd->my_archive != NULL | |
383 | && name_match (list_tmp->name, | |
607b4833 | 384 | bfd_get_filename (file->the_bfd->my_archive)) == 0) |
f38a2680 | 385 | return true; |
72223188 JJ |
386 | } |
387 | ||
f38a2680 | 388 | return false; |
8f1732fc AB |
389 | } |
390 | ||
b1eecf6f MM |
391 | /* Add SECTION (from input FILE) to the list of matching sections |
392 | within PTR (the matching wildcard is SEC). */ | |
393 | ||
394 | static void | |
395 | add_matching_section (lang_wild_statement_type *ptr, | |
396 | struct wildcard_list *sec, | |
397 | asection *section, | |
398 | lang_input_statement_type *file) | |
399 | { | |
400 | lang_input_matcher_type *new_section; | |
401 | /* Add a section reference to the list. */ | |
402 | new_section = new_stat (lang_input_matcher, &ptr->matching_sections); | |
403 | new_section->section = section; | |
404 | new_section->pattern = sec; | |
405 | new_section->input_stmt = file; | |
406 | } | |
407 | ||
408 | /* Process section S (from input file FILE) in relation to wildcard | |
409 | statement PTR. We already know that a prefix of the name of S matches | |
410 | some wildcard in PTR's wildcard list. Here we check if the filename | |
411 | matches as well (if it's specified) and if any of the wildcards in fact | |
412 | does match. */ | |
413 | ||
414 | static void | |
415 | walk_wild_section_match (lang_wild_statement_type *ptr, | |
416 | lang_input_statement_type *file, | |
417 | asection *s) | |
418 | { | |
419 | struct wildcard_list *sec; | |
420 | const char *file_spec = ptr->filename; | |
421 | char *p; | |
422 | ||
423 | /* Check if filenames match. */ | |
424 | if (file_spec == NULL) | |
425 | ; | |
426 | else if ((p = archive_path (file_spec)) != NULL) | |
427 | { | |
428 | if (!input_statement_is_archive_path (file_spec, p, file)) | |
429 | return; | |
430 | } | |
431 | else if (wildcardp (file_spec)) | |
432 | { | |
433 | if (fnmatch (file_spec, file->filename, 0) != 0) | |
434 | return; | |
435 | } | |
436 | else | |
437 | { | |
8f5cd47b MM |
438 | /* XXX Matching against non-wildcard filename in wild statements |
439 | was done by going through lookup_name, which uses | |
440 | ->local_sym_name to compare against, not ->filename. We retain | |
441 | this behaviour even though the above code paths use filename. | |
442 | It would be more logical to use it here as well, in which | |
443 | case the above wildcard() arm could be folded into this by using | |
444 | name_match. This would also solve the worry of what to do | |
445 | about unset local_sym_name (in which case lookup_name simply adds | |
446 | the input file again). */ | |
447 | const char *filename = file->local_sym_name; | |
448 | if (filename == NULL | |
449 | || filename_cmp (filename, file_spec) != 0) | |
b1eecf6f MM |
450 | return; |
451 | } | |
452 | ||
885d8643 MM |
453 | /* If filename is excluded we're done. */ |
454 | if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file)) | |
455 | return; | |
456 | ||
b1eecf6f MM |
457 | /* Check section name against each wildcard spec. If there's no |
458 | wildcard all sections match. */ | |
459 | sec = ptr->section_list; | |
460 | if (sec == NULL) | |
461 | add_matching_section (ptr, sec, s, file); | |
462 | else | |
463 | { | |
464 | const char *sname = bfd_section_name (s); | |
465 | for (; sec != NULL; sec = sec->next) | |
466 | { | |
467 | if (sec->spec.name != NULL | |
468 | && spec_match (&sec->spec, sname) != 0) | |
469 | continue; | |
470 | ||
471 | /* Don't process sections from files which were excluded. */ | |
472 | if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, | |
473 | file)) | |
474 | add_matching_section (ptr, sec, s, file); | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
02ecc8e9 L |
479 | /* Return the numerical value of the init_priority attribute from |
480 | section name NAME. */ | |
481 | ||
9a24a276 AM |
482 | static int |
483 | get_init_priority (const asection *sec) | |
02ecc8e9 | 484 | { |
9a24a276 AM |
485 | const char *name = bfd_section_name (sec); |
486 | const char *dot; | |
02ecc8e9 L |
487 | |
488 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 489 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
490 | lower value means a higher priority. |
491 | ||
9a24a276 | 492 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
493 | decimal numerical value of the init_priority attribute. |
494 | The order of execution in .init_array is forward and | |
495 | .fini_array is backward. | |
9a24a276 | 496 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
497 | decimal numerical value of the init_priority attribute. |
498 | The order of execution in .ctors is backward and .dtors | |
499 | is forward. | |
9a24a276 AM |
500 | |
501 | .init_array.NNNNN sections would normally be placed in an output | |
502 | .init_array section, .fini_array.NNNNN in .fini_array, | |
503 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
504 | we should sort by increasing number (and could just use | |
505 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
506 | being placed in .init_array (which may also contain | |
507 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
508 | placed in .fini_array then we need to extract the init_priority | |
509 | attribute and sort on that. */ | |
510 | dot = strrchr (name, '.'); | |
511 | if (dot != NULL && ISDIGIT (dot[1])) | |
02ecc8e9 | 512 | { |
9a24a276 AM |
513 | char *end; |
514 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
515 | if (*end == 0) | |
516 | { | |
517 | if (dot == name + 6 | |
518 | && (strncmp (name, ".ctors", 6) == 0 | |
519 | || strncmp (name, ".dtors", 6) == 0)) | |
520 | init_priority = 65535 - init_priority; | |
521 | if (init_priority <= INT_MAX) | |
522 | return init_priority; | |
523 | } | |
02ecc8e9 | 524 | } |
9a24a276 | 525 | return -1; |
02ecc8e9 L |
526 | } |
527 | ||
50c77e5d NC |
528 | /* Compare sections ASEC and BSEC according to SORT. */ |
529 | ||
530 | static int | |
531 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
532 | { | |
533 | int ret; | |
9a24a276 | 534 | int a_priority, b_priority; |
50c77e5d NC |
535 | |
536 | switch (sort) | |
537 | { | |
538 | default: | |
539 | abort (); | |
540 | ||
02ecc8e9 | 541 | case by_init_priority: |
9a24a276 AM |
542 | a_priority = get_init_priority (asec); |
543 | b_priority = get_init_priority (bsec); | |
544 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 545 | goto sort_by_name; |
9a24a276 | 546 | ret = a_priority - b_priority; |
02ecc8e9 L |
547 | if (ret) |
548 | break; | |
549 | else | |
550 | goto sort_by_name; | |
551 | ||
50c77e5d | 552 | case by_alignment_name: |
fd361982 | 553 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
554 | if (ret) |
555 | break; | |
556 | /* Fall through. */ | |
557 | ||
558 | case by_name: | |
dc1e8a47 | 559 | sort_by_name: |
fd361982 | 560 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
561 | break; |
562 | ||
563 | case by_name_alignment: | |
fd361982 | 564 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
565 | if (ret) |
566 | break; | |
567 | /* Fall through. */ | |
568 | ||
569 | case by_alignment: | |
fd361982 | 570 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
571 | break; |
572 | } | |
573 | ||
574 | return ret; | |
575 | } | |
576 | ||
af31506c MM |
577 | /* PE puts the sort key in the input statement. */ |
578 | ||
579 | static const char * | |
580 | sort_filename (bfd *abfd) | |
581 | { | |
582 | lang_input_statement_type *is = bfd_usrdata (abfd); | |
583 | if (is->sort_key) | |
584 | return is->sort_key; | |
585 | return bfd_get_filename (abfd); | |
586 | } | |
587 | ||
588 | /* Handle wildcard sorting. This returns the place in a binary search tree | |
589 | where this FILE:SECTION should be inserted for wild statement WILD where | |
590 | the spec SEC was the matching one. The tree is later linearized. */ | |
50c77e5d NC |
591 | |
592 | static lang_section_bst_type ** | |
af31506c MM |
593 | wild_sort (lang_wild_statement_type *wild, |
594 | struct wildcard_list *sec, | |
595 | lang_input_statement_type *file, | |
596 | asection *section) | |
50c77e5d | 597 | { |
329c1c86 | 598 | lang_section_bst_type **tree; |
50c77e5d NC |
599 | |
600 | if (!wild->filenames_sorted | |
af31506c MM |
601 | && (sec == NULL || sec->spec.sorted == none |
602 | || sec->spec.sorted == by_none)) | |
50c77e5d | 603 | { |
af31506c MM |
604 | /* We might be called even if _this_ spec doesn't need sorting, |
605 | in which case we simply append at the right end of tree. */ | |
606 | return wild->rightmost; | |
50c77e5d NC |
607 | } |
608 | ||
af31506c | 609 | tree = &wild->tree; |
329c1c86 | 610 | while (*tree) |
50c77e5d | 611 | { |
af31506c MM |
612 | /* Sorting by filename takes precedence over sorting by section |
613 | name. */ | |
614 | ||
615 | if (wild->filenames_sorted) | |
616 | { | |
617 | const char *fn, *ln; | |
618 | bool fa, la; | |
619 | int i; | |
620 | asection *lsec = (*tree)->section; | |
621 | ||
622 | /* The PE support for the .idata section as generated by | |
623 | dlltool assumes that files will be sorted by the name of | |
624 | the archive and then the name of the file within the | |
625 | archive. */ | |
626 | ||
627 | fa = file->the_bfd->my_archive != NULL; | |
628 | if (fa) | |
629 | fn = sort_filename (file->the_bfd->my_archive); | |
630 | else | |
631 | fn = sort_filename (file->the_bfd); | |
632 | ||
633 | la = lsec->owner->my_archive != NULL; | |
634 | if (la) | |
635 | ln = sort_filename (lsec->owner->my_archive); | |
636 | else | |
637 | ln = sort_filename (lsec->owner); | |
638 | ||
639 | i = filename_cmp (fn, ln); | |
640 | if (i > 0) | |
641 | { tree = &((*tree)->right); continue; } | |
642 | else if (i < 0) | |
643 | { tree = &((*tree)->left); continue; } | |
644 | ||
645 | if (fa || la) | |
646 | { | |
647 | if (fa) | |
648 | fn = sort_filename (file->the_bfd); | |
649 | if (la) | |
650 | ln = sort_filename (lsec->owner); | |
651 | ||
652 | i = filename_cmp (fn, ln); | |
653 | if (i > 0) | |
654 | { tree = &((*tree)->right); continue; } | |
655 | else if (i < 0) | |
656 | { tree = &((*tree)->left); continue; } | |
657 | } | |
658 | } | |
659 | ||
660 | /* Here either the files are not sorted by name, or we are | |
661 | looking at the sections for this file. */ | |
662 | ||
50c77e5d | 663 | /* Find the correct node to append this section. */ |
b7eab2a9 MM |
664 | if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none |
665 | && compare_section (sec->spec.sorted, section, (*tree)->section) < 0) | |
329c1c86 AM |
666 | tree = &((*tree)->left); |
667 | else | |
668 | tree = &((*tree)->right); | |
50c77e5d NC |
669 | } |
670 | ||
671 | return tree; | |
672 | } | |
673 | ||
af31506c | 674 | /* Use wild_sort to build a BST to sort sections. */ |
50c77e5d NC |
675 | |
676 | static void | |
af31506c | 677 | output_section_callback_sort (lang_wild_statement_type *ptr, |
329c1c86 AM |
678 | struct wildcard_list *sec, |
679 | asection *section, | |
680 | lang_input_statement_type *file, | |
d0bf826b | 681 | void *output) |
50c77e5d NC |
682 | { |
683 | lang_section_bst_type *node; | |
684 | lang_section_bst_type **tree; | |
d0bf826b | 685 | lang_output_section_statement_type *os; |
50c77e5d | 686 | |
d0bf826b AM |
687 | os = (lang_output_section_statement_type *) output; |
688 | ||
689 | if (unique_section_p (section, os)) | |
50c77e5d NC |
690 | return; |
691 | ||
670c91c0 MM |
692 | /* Don't add sections to the tree when we already know that |
693 | lang_add_section won't do anything with it. */ | |
694 | if (wont_add_section_p (section, os)) | |
695 | return; | |
696 | ||
1e9cc1c2 | 697 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
50c77e5d NC |
698 | node->left = 0; |
699 | node->right = 0; | |
700 | node->section = section; | |
b209b5a6 | 701 | node->pattern = ptr->section_list; |
50c77e5d | 702 | |
af31506c | 703 | tree = wild_sort (ptr, sec, file, section); |
50c77e5d | 704 | if (tree != NULL) |
af31506c MM |
705 | { |
706 | *tree = node; | |
707 | if (tree == ptr->rightmost) | |
708 | ptr->rightmost = &node->right; | |
709 | } | |
50c77e5d NC |
710 | } |
711 | ||
712 | /* Convert a sorted sections' BST back to list form. */ | |
713 | ||
714 | static void | |
329c1c86 AM |
715 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
716 | lang_section_bst_type *tree, | |
717 | void *output) | |
50c77e5d NC |
718 | { |
719 | if (tree->left) | |
720 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
721 | ||
af31506c MM |
722 | lang_add_section (&ptr->children, tree->section, tree->pattern, |
723 | ptr->section_flag_list, | |
329c1c86 | 724 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
725 | |
726 | if (tree->right) | |
727 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
728 | ||
729 | free (tree); | |
730 | } | |
731 | ||
b1eecf6f MM |
732 | \f |
733 | /* Sections are matched against wildcard statements via a prefix tree. | |
734 | The prefix tree holds prefixes of all matching patterns (up to the first | |
735 | wildcard character), and the wild statement from which those patterns | |
736 | came. When matching a section name against the tree we're walking through | |
737 | the tree character by character. Each statement we hit is one that | |
738 | potentially matches. This is checked by actually going through the | |
739 | (glob) matching routines. | |
740 | ||
741 | When the section name turns out to actually match we record that section | |
742 | in the wild statements list of matching sections. */ | |
743 | ||
744 | /* A prefix can be matched by multiple statement, so we need a list of them. */ | |
745 | struct wild_stmt_list | |
746 | { | |
747 | lang_wild_statement_type *stmt; | |
748 | struct wild_stmt_list *next; | |
749 | }; | |
750 | ||
751 | /* The prefix tree itself. */ | |
752 | struct prefixtree | |
753 | { | |
754 | /* The list of all children (linked via .next). */ | |
755 | struct prefixtree *child; | |
756 | struct prefixtree *next; | |
757 | /* This tree node is responsible for the prefix of parent plus 'c'. */ | |
758 | char c; | |
759 | /* The statements that potentially can match this prefix. */ | |
760 | struct wild_stmt_list *stmt; | |
761 | }; | |
762 | ||
763 | /* We always have a root node in the prefix tree. It corresponds to the | |
764 | empty prefix. E.g. a glob like "*" would sit in this root. */ | |
765 | static struct prefixtree the_root, *ptroot = &the_root; | |
766 | ||
767 | /* Given a prefix tree in *TREE, corresponding to prefix P, find or | |
768 | INSERT the tree node corresponding to prefix P+C. */ | |
769 | ||
770 | static struct prefixtree * | |
771 | get_prefix_tree (struct prefixtree **tree, char c, bool insert) | |
772 | { | |
773 | struct prefixtree *t; | |
774 | for (t = *tree; t; t = t->next) | |
775 | if (t->c == c) | |
776 | return t; | |
777 | if (!insert) | |
778 | return NULL; | |
779 | t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); | |
780 | t->child = NULL; | |
781 | t->next = *tree; | |
782 | t->c = c; | |
783 | t->stmt = NULL; | |
784 | *tree = t; | |
785 | return t; | |
786 | } | |
787 | ||
788 | /* Add STMT to the set of statements that can be matched by the prefix | |
789 | corresponding to prefix tree T. */ | |
790 | ||
791 | static void | |
792 | pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) | |
793 | { | |
794 | struct wild_stmt_list *sl, **psl; | |
795 | sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); | |
796 | sl->stmt = stmt; | |
797 | sl->next = NULL; | |
798 | psl = &t->stmt; | |
799 | while (*psl) | |
800 | psl = &(*psl)->next; | |
801 | *psl = sl; | |
802 | } | |
803 | ||
804 | /* Insert STMT into the global prefix tree. */ | |
805 | ||
806 | static void | |
807 | insert_prefix_tree (lang_wild_statement_type *stmt) | |
808 | { | |
809 | struct wildcard_list *sec; | |
810 | struct prefixtree *t; | |
811 | ||
812 | if (!stmt->section_list) | |
813 | { | |
814 | /* If we have no section_list (no wildcards in the wild STMT), | |
815 | then every section name will match, so add this to the root. */ | |
816 | pt_add_stmt (ptroot, stmt); | |
817 | return; | |
818 | } | |
819 | ||
820 | for (sec = stmt->section_list; sec; sec = sec->next) | |
821 | { | |
822 | const char *name = sec->spec.name ? sec->spec.name : "*"; | |
823 | char c; | |
824 | t = ptroot; | |
825 | for (; (c = *name); name++) | |
826 | { | |
827 | if (c == '*' || c == '[' || c == '?') | |
828 | break; | |
829 | t = get_prefix_tree (&t->child, c, true); | |
830 | } | |
831 | /* If we hit a glob character, the matching prefix is what we saw | |
832 | until now. If we hit the end of pattern (hence it's no glob) then | |
833 | we can do better: we only need to record a match when a section name | |
834 | completely matches, not merely a prefix, so record the trailing 0 | |
835 | as well. */ | |
836 | if (!c) | |
837 | t = get_prefix_tree (&t->child, 0, true); | |
838 | pt_add_stmt (t, stmt); | |
839 | } | |
840 | } | |
841 | ||
842 | /* Dump T indented by INDENT spaces. */ | |
843 | ||
844 | static void | |
845 | debug_prefix_tree_rec (struct prefixtree *t, int indent) | |
846 | { | |
847 | for (; t; t = t->next) | |
848 | { | |
849 | struct wild_stmt_list *sl; | |
850 | printf ("%*s %c", indent, "", t->c); | |
851 | for (sl = t->stmt; sl; sl = sl->next) | |
852 | { | |
853 | struct wildcard_list *curr; | |
854 | printf (" %p ", sl->stmt); | |
855 | for (curr = sl->stmt->section_list; curr; curr = curr->next) | |
856 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
857 | } | |
858 | printf ("\n"); | |
859 | debug_prefix_tree_rec (t->child, indent + 2); | |
860 | } | |
861 | } | |
862 | ||
863 | /* Dump the global prefix tree. */ | |
864 | ||
865 | static void | |
866 | debug_prefix_tree (void) | |
867 | { | |
868 | debug_prefix_tree_rec (ptroot, 2); | |
869 | } | |
870 | ||
049522ca MM |
871 | /* Like strcspn() but start to look from the end to beginning of |
872 | S. Returns the length of the suffix of S consisting entirely | |
873 | of characters not in REJECT. */ | |
874 | ||
875 | static size_t | |
876 | rstrcspn (const char *s, const char *reject) | |
877 | { | |
878 | size_t len = strlen (s), sufflen = 0; | |
879 | while (len--) | |
880 | { | |
881 | char c = s[len]; | |
882 | if (strchr (reject, c) != 0) | |
883 | break; | |
884 | sufflen++; | |
885 | } | |
886 | return sufflen; | |
887 | } | |
888 | ||
b1eecf6f MM |
889 | /* Analyze the wildcards in wild statement PTR to setup various |
890 | things for quick matching. */ | |
72223188 JJ |
891 | |
892 | static void | |
893 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
894 | { | |
72223188 | 895 | struct wildcard_list *sec; |
d7791499 | 896 | |
e6f2cbf5 | 897 | ptr->tree = NULL; |
af31506c | 898 | ptr->rightmost = &ptr->tree; |
72223188 | 899 | |
049522ca MM |
900 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
901 | { | |
902 | if (sec->spec.name) | |
903 | { | |
904 | sec->spec.namelen = strlen (sec->spec.name); | |
905 | sec->spec.prefixlen = strcspn (sec->spec.name, "?*["); | |
906 | sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen, | |
907 | "?*]"); | |
908 | } | |
909 | else | |
910 | sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; | |
911 | } | |
912 | ||
b1eecf6f | 913 | insert_prefix_tree (ptr); |
4dec4d4e RH |
914 | } |
915 | ||
b1eecf6f MM |
916 | /* Match all sections from FILE against the global prefix tree, |
917 | and record them into each wild statement that has a match. */ | |
918 | ||
4dec4d4e | 919 | static void |
b1eecf6f | 920 | resolve_wild_sections (lang_input_statement_type *file) |
43ae96e9 | 921 | { |
b1eecf6f MM |
922 | asection *s; |
923 | ||
924 | if (file->flags.just_syms) | |
925 | return; | |
926 | ||
927 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
928 | { | |
929 | const char *sname = bfd_section_name (s); | |
930 | char c = 1; | |
931 | struct prefixtree *t = ptroot; | |
932 | //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename); | |
933 | do | |
934 | { | |
935 | if (t->stmt) | |
936 | { | |
937 | struct wild_stmt_list *sl; | |
938 | for (sl = t->stmt; sl; sl = sl->next) | |
939 | { | |
940 | walk_wild_section_match (sl->stmt, file, s); | |
941 | //printf (" ZZZ maybe place into %p\n", sl->stmt); | |
942 | } | |
943 | } | |
944 | if (!c) | |
945 | break; | |
946 | c = *sname++; | |
947 | t = get_prefix_tree (&t->child, c, false); | |
948 | } | |
949 | while (t); | |
950 | } | |
43ae96e9 MM |
951 | } |
952 | ||
b1eecf6f MM |
953 | /* Match all sections from all input files against the global prefix tree. */ |
954 | ||
955 | static void | |
956 | resolve_wilds (void) | |
957 | { | |
958 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
959 | { | |
960 | //printf("XXX %s\n", f->filename); | |
b1eecf6f MM |
961 | if (f->the_bfd == NULL |
962 | || !bfd_check_format (f->the_bfd, bfd_archive)) | |
963 | resolve_wild_sections (f); | |
964 | else | |
965 | { | |
966 | bfd *member; | |
967 | ||
968 | /* This is an archive file. We must map each member of the | |
969 | archive separately. */ | |
970 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); | |
971 | while (member != NULL) | |
972 | { | |
973 | /* When lookup_name is called, it will call the add_symbols | |
974 | entry point for the archive. For each element of the | |
975 | archive which is included, BFD will call ldlang_add_file, | |
976 | which will set the usrdata field of the member to the | |
977 | lang_input_statement. */ | |
978 | if (bfd_usrdata (member) != NULL) | |
979 | resolve_wild_sections (bfd_usrdata (member)); | |
980 | ||
981 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
982 | } | |
983 | } | |
984 | } | |
985 | } | |
986 | ||
b1eecf6f MM |
987 | /* For each input section that matches wild statement S calls |
988 | CALLBACK with DATA. */ | |
43ae96e9 MM |
989 | |
990 | static void | |
991 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) | |
992 | { | |
b1eecf6f | 993 | lang_statement_union_type *l; |
43ae96e9 | 994 | |
b1eecf6f | 995 | for (l = s->matching_sections.head; l; l = l->header.next) |
43ae96e9 | 996 | { |
b1eecf6f MM |
997 | (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, |
998 | l->input_matcher.input_stmt, data); | |
43ae96e9 | 999 | } |
5f992e62 AM |
1000 | } |
1001 | ||
08da4cac | 1002 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
1003 | function for each node, except those inside output section statements |
1004 | with constraint set to -1. */ | |
252b5132 | 1005 | |
8658f989 | 1006 | void |
1579bae1 AM |
1007 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
1008 | lang_statement_union_type *s) | |
252b5132 | 1009 | { |
1579bae1 | 1010 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
1011 | { |
1012 | func (s); | |
1013 | ||
1014 | switch (s->header.type) | |
1015 | { | |
1016 | case lang_constructors_statement_enum: | |
1017 | lang_for_each_statement_worker (func, constructor_list.head); | |
1018 | break; | |
1019 | case lang_output_section_statement_enum: | |
8658f989 AM |
1020 | if (s->output_section_statement.constraint != -1) |
1021 | lang_for_each_statement_worker | |
1022 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
1023 | break; |
1024 | case lang_wild_statement_enum: | |
6feb9908 AM |
1025 | lang_for_each_statement_worker (func, |
1026 | s->wild_statement.children.head); | |
252b5132 RH |
1027 | break; |
1028 | case lang_group_statement_enum: | |
1029 | lang_for_each_statement_worker (func, | |
1030 | s->group_statement.children.head); | |
1031 | break; | |
1032 | case lang_data_statement_enum: | |
1033 | case lang_reloc_statement_enum: | |
1034 | case lang_object_symbols_statement_enum: | |
1035 | case lang_output_statement_enum: | |
1036 | case lang_target_statement_enum: | |
1037 | case lang_input_section_enum: | |
1038 | case lang_input_statement_enum: | |
1039 | case lang_assignment_statement_enum: | |
1040 | case lang_padding_statement_enum: | |
1041 | case lang_address_statement_enum: | |
1042 | case lang_fill_statement_enum: | |
53d25da6 | 1043 | case lang_insert_statement_enum: |
252b5132 RH |
1044 | break; |
1045 | default: | |
1046 | FAIL (); | |
1047 | break; | |
1048 | } | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | void | |
1579bae1 | 1053 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1054 | { |
08da4cac | 1055 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1056 | } |
1057 | ||
1058 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1059 | |
252b5132 | 1060 | void |
1579bae1 | 1061 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1062 | { |
1579bae1 | 1063 | list->head = NULL; |
252b5132 RH |
1064 | list->tail = &list->head; |
1065 | } | |
1066 | ||
36983a93 AM |
1067 | static void |
1068 | lang_statement_append (lang_statement_list_type *list, | |
1069 | void *element, | |
1070 | void *field) | |
1071 | { | |
1072 | *(list->tail) = element; | |
1073 | list->tail = field; | |
1074 | } | |
1075 | ||
bde18da4 AM |
1076 | void |
1077 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1078 | { | |
1079 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1080 | abort (); | |
1081 | *stat_save_ptr++ = stat_ptr; | |
1082 | stat_ptr = new_ptr; | |
1083 | } | |
1084 | ||
1085 | void | |
1086 | pop_stat_ptr (void) | |
1087 | { | |
1088 | if (stat_save_ptr <= stat_save) | |
1089 | abort (); | |
1090 | stat_ptr = *--stat_save_ptr; | |
1091 | } | |
1092 | ||
08da4cac | 1093 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1094 | |
08da4cac | 1095 | static lang_statement_union_type * |
1579bae1 AM |
1096 | new_statement (enum statement_enum type, |
1097 | size_t size, | |
1098 | lang_statement_list_type *list) | |
252b5132 | 1099 | { |
d3ce72d0 | 1100 | lang_statement_union_type *new_stmt; |
252b5132 | 1101 | |
988de25b | 1102 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
1103 | new_stmt->header.type = type; |
1104 | new_stmt->header.next = NULL; | |
1105 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1106 | return new_stmt; | |
252b5132 RH |
1107 | } |
1108 | ||
08da4cac KH |
1109 | /* Build a new input file node for the language. There are several |
1110 | ways in which we treat an input file, eg, we only look at symbols, | |
1111 | or prefix it with a -l etc. | |
252b5132 | 1112 | |
08da4cac | 1113 | We can be supplied with requests for input files more than once; |
396a2467 | 1114 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1115 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1116 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1117 | |
252b5132 | 1118 | static lang_input_statement_type * |
1579bae1 AM |
1119 | new_afile (const char *name, |
1120 | lang_input_file_enum_type file_type, | |
16171946 FS |
1121 | const char *target, |
1122 | const char *from_filename) | |
252b5132 RH |
1123 | { |
1124 | lang_input_statement_type *p; | |
1125 | ||
f38a2680 | 1126 | lang_has_input_file = true; |
66be1055 | 1127 | |
fb221fba NC |
1128 | name = ldfile_possibly_remap_input (name); |
1129 | if (name == NULL) | |
1130 | return NULL; | |
1131 | ||
1f1f5b92 | 1132 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1133 | memset (&p->the_bfd, 0, |
1134 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
16171946 | 1135 | p->extra_search_path = NULL; |
252b5132 | 1136 | p->target = target; |
66be1055 AM |
1137 | p->flags.dynamic = input_flags.dynamic; |
1138 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1139 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1140 | p->flags.whole_archive = input_flags.whole_archive; | |
f4a23d42 | 1141 | p->flags.sysrooted = input_flags.sysrooted; |
a6ad7914 | 1142 | p->sort_key = NULL; |
bcb674cf | 1143 | |
252b5132 RH |
1144 | switch (file_type) |
1145 | { | |
1146 | case lang_input_file_is_symbols_only_enum: | |
1147 | p->filename = name; | |
252b5132 | 1148 | p->local_sym_name = name; |
f38a2680 AM |
1149 | p->flags.real = true; |
1150 | p->flags.just_syms = true; | |
252b5132 RH |
1151 | break; |
1152 | case lang_input_file_is_fake_enum: | |
1153 | p->filename = name; | |
252b5132 | 1154 | p->local_sym_name = name; |
252b5132 RH |
1155 | break; |
1156 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1157 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1158 | { |
1159 | p->filename = name + 1; | |
f38a2680 | 1160 | p->flags.full_name_provided = true; |
0aa7f586 | 1161 | } |
d4ae5fb0 | 1162 | else |
0aa7f586 | 1163 | p->filename = name; |
ff7a0acf | 1164 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
f38a2680 AM |
1165 | p->flags.maybe_archive = true; |
1166 | p->flags.real = true; | |
1167 | p->flags.search_dirs = true; | |
252b5132 RH |
1168 | break; |
1169 | case lang_input_file_is_marker_enum: | |
1170 | p->filename = name; | |
252b5132 | 1171 | p->local_sym_name = name; |
f38a2680 | 1172 | p->flags.search_dirs = true; |
252b5132 RH |
1173 | break; |
1174 | case lang_input_file_is_search_file_enum: | |
1175 | p->filename = name; | |
252b5132 | 1176 | p->local_sym_name = name; |
16171946 FS |
1177 | /* If name is a relative path, search the directory of the current linker |
1178 | script first. */ | |
1179 | if (from_filename && !IS_ABSOLUTE_PATH (name)) | |
1180 | p->extra_search_path = ldirname (from_filename); | |
f38a2680 AM |
1181 | p->flags.real = true; |
1182 | p->flags.search_dirs = true; | |
252b5132 RH |
1183 | break; |
1184 | case lang_input_file_is_file_enum: | |
1185 | p->filename = name; | |
252b5132 | 1186 | p->local_sym_name = name; |
f38a2680 | 1187 | p->flags.real = true; |
252b5132 RH |
1188 | break; |
1189 | default: | |
1190 | FAIL (); | |
1191 | } | |
c4b78195 | 1192 | |
36983a93 | 1193 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1194 | return p; |
1195 | } | |
1196 | ||
1197 | lang_input_statement_type * | |
1579bae1 AM |
1198 | lang_add_input_file (const char *name, |
1199 | lang_input_file_enum_type file_type, | |
1200 | const char *target) | |
252b5132 | 1201 | { |
3aa2d05a | 1202 | if (name != NULL |
08dedd66 | 1203 | && (*name == '=' || startswith (name, "$SYSROOT"))) |
bfa23434 HPN |
1204 | { |
1205 | lang_input_statement_type *ret; | |
1206 | char *sysrooted_name | |
3aa2d05a NC |
1207 | = concat (ld_sysroot, |
1208 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1209 | (const char *) NULL); | |
bfa23434 HPN |
1210 | |
1211 | /* We've now forcibly prepended the sysroot, making the input | |
1212 | file independent of the context. Therefore, temporarily | |
1213 | force a non-sysrooted context for this statement, so it won't | |
1214 | get the sysroot prepended again when opened. (N.B. if it's a | |
1215 | script, any child nodes with input files starting with "/" | |
1216 | will be handled as "sysrooted" as they'll be found to be | |
1217 | within the sysroot subdirectory.) */ | |
1218 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1219 | input_flags.sysrooted = 0; | |
16171946 | 1220 | ret = new_afile (sysrooted_name, file_type, target, NULL); |
bfa23434 HPN |
1221 | input_flags.sysrooted = outer_sysrooted; |
1222 | return ret; | |
1223 | } | |
1224 | ||
16171946 | 1225 | return new_afile (name, file_type, target, current_input_file); |
252b5132 RH |
1226 | } |
1227 | ||
409d7240 | 1228 | struct out_section_hash_entry |
750877ba L |
1229 | { |
1230 | struct bfd_hash_entry root; | |
409d7240 | 1231 | lang_statement_union_type s; |
750877ba L |
1232 | }; |
1233 | ||
1234 | /* The hash table. */ | |
1235 | ||
409d7240 | 1236 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1237 | |
384a9dda | 1238 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1239 | initialize the table, fill in an entry and remove the table. */ |
1240 | ||
1241 | static struct bfd_hash_entry * | |
329c1c86 | 1242 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1243 | struct bfd_hash_table *table, |
1244 | const char *string) | |
750877ba | 1245 | { |
384a9dda | 1246 | lang_output_section_statement_type **nextp; |
409d7240 | 1247 | struct out_section_hash_entry *ret; |
384a9dda AM |
1248 | |
1249 | if (entry == NULL) | |
1250 | { | |
1e9cc1c2 | 1251 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1252 | sizeof (*ret)); |
384a9dda AM |
1253 | if (entry == NULL) |
1254 | return entry; | |
1255 | } | |
1256 | ||
1257 | entry = bfd_hash_newfunc (entry, table, string); | |
1258 | if (entry == NULL) | |
1259 | return entry; | |
1260 | ||
409d7240 AM |
1261 | ret = (struct out_section_hash_entry *) entry; |
1262 | memset (&ret->s, 0, sizeof (ret->s)); | |
1263 | ret->s.header.type = lang_output_section_statement_enum; | |
3d9c8f6b AM |
1264 | ret->s.output_section_statement.subsection_alignment = NULL; |
1265 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1266 | ret->s.output_section_statement.block_value = 1; |
1267 | lang_list_init (&ret->s.output_section_statement.children); | |
1268 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1269 | |
218868ba | 1270 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1271 | first one, lang_os_list.tail points to the "next" |
218868ba | 1272 | field of the last element of the list. */ |
5c1e6d53 | 1273 | if (lang_os_list.head != NULL) |
409d7240 AM |
1274 | ret->s.output_section_statement.prev |
1275 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1276 | ((char *) lang_os_list.tail |
409d7240 | 1277 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1278 | |
384a9dda AM |
1279 | /* GCC's strict aliasing rules prevent us from just casting the |
1280 | address, so we store the pointer in a variable and cast that | |
1281 | instead. */ | |
409d7240 | 1282 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1283 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
384a9dda | 1284 | return &ret->root; |
750877ba L |
1285 | } |
1286 | ||
1287 | static void | |
409d7240 | 1288 | output_section_statement_table_init (void) |
750877ba | 1289 | { |
409d7240 AM |
1290 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1291 | output_section_statement_newfunc, | |
1292 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1293 | 61)) |
df5f2391 | 1294 | einfo (_("%F%P: can not create hash table: %E\n")); |
750877ba L |
1295 | } |
1296 | ||
1297 | static void | |
409d7240 | 1298 | output_section_statement_table_free (void) |
750877ba | 1299 | { |
409d7240 | 1300 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1301 | } |
1302 | ||
08da4cac KH |
1303 | /* Build enough state so that the parser can build its tree. */ |
1304 | ||
252b5132 | 1305 | void |
1579bae1 | 1306 | lang_init (void) |
252b5132 RH |
1307 | { |
1308 | obstack_begin (&stat_obstack, 1000); | |
b1eecf6f | 1309 | obstack_init (&pt_obstack); |
252b5132 RH |
1310 | |
1311 | stat_ptr = &statement_list; | |
1312 | ||
409d7240 | 1313 | output_section_statement_table_init (); |
750877ba | 1314 | |
252b5132 RH |
1315 | lang_list_init (stat_ptr); |
1316 | ||
1317 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1318 | lang_list_init (&lang_os_list); |
252b5132 | 1319 | lang_list_init (&file_chain); |
1579bae1 AM |
1320 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1321 | NULL); | |
08da4cac | 1322 | abs_output_section = |
21401fc7 | 1323 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); |
252b5132 RH |
1324 | |
1325 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1326 | |
16e4ecc0 AM |
1327 | asneeded_list_head = NULL; |
1328 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1329 | } |
1330 | ||
750877ba L |
1331 | void |
1332 | lang_finish (void) | |
1333 | { | |
409d7240 | 1334 | output_section_statement_table_free (); |
fb221fba | 1335 | ldfile_remap_input_free (); |
750877ba L |
1336 | } |
1337 | ||
252b5132 | 1338 | /*---------------------------------------------------------------------- |
08da4cac KH |
1339 | A region is an area of memory declared with the |
1340 | MEMORY { name:org=exp, len=exp ... } | |
1341 | syntax. | |
252b5132 | 1342 | |
08da4cac | 1343 | We maintain a list of all the regions here. |
252b5132 | 1344 | |
08da4cac | 1345 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1346 | which is created when looked up to be the entire data space. |
1347 | ||
1348 | If create is true we are creating a region inside a MEMORY block. | |
1349 | In this case it is probably an error to create a region that has | |
1350 | already been created. If we are not inside a MEMORY block it is | |
1351 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 | 1352 | and so we issue a warning. |
1e0061d2 | 1353 | |
4a93e180 NC |
1354 | Each region has at least one name. The first name is either |
1355 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1356 | alias names to an existing region within a script with | |
1357 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1358 | region. */ | |
252b5132 RH |
1359 | |
1360 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1361 | static lang_memory_region_type **lang_memory_region_list_tail |
1362 | = &lang_memory_region_list; | |
252b5132 RH |
1363 | |
1364 | lang_memory_region_type * | |
f38a2680 | 1365 | lang_memory_region_lookup (const char *const name, bool create) |
252b5132 | 1366 | { |
4a93e180 NC |
1367 | lang_memory_region_name *n; |
1368 | lang_memory_region_type *r; | |
d3ce72d0 | 1369 | lang_memory_region_type *new_region; |
252b5132 | 1370 | |
9f88b410 RS |
1371 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1372 | if (name == NULL) | |
1373 | return NULL; | |
1374 | ||
4a93e180 NC |
1375 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1376 | for (n = &r->name_list; n != NULL; n = n->next) | |
1377 | if (strcmp (n->name, name) == 0) | |
4724d37e RM |
1378 | { |
1379 | if (create) | |
c1c8c1ef | 1380 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1381 | NULL, name); |
1382 | return r; | |
1383 | } | |
252b5132 | 1384 | |
a747ee4d | 1385 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1386 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1387 | NULL, name); |
a747ee4d | 1388 | |
988de25b | 1389 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1390 | |
d3ce72d0 NC |
1391 | new_region->name_list.name = xstrdup (name); |
1392 | new_region->name_list.next = NULL; | |
1393 | new_region->next = NULL; | |
cc9ad334 | 1394 | new_region->origin_exp = NULL; |
d3ce72d0 | 1395 | new_region->origin = 0; |
cc9ad334 | 1396 | new_region->length_exp = NULL; |
d3ce72d0 NC |
1397 | new_region->length = ~(bfd_size_type) 0; |
1398 | new_region->current = 0; | |
1399 | new_region->last_os = NULL; | |
1400 | new_region->flags = 0; | |
1401 | new_region->not_flags = 0; | |
f38a2680 | 1402 | new_region->had_full_message = false; |
252b5132 | 1403 | |
d3ce72d0 NC |
1404 | *lang_memory_region_list_tail = new_region; |
1405 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1406 | |
d3ce72d0 | 1407 | return new_region; |
252b5132 RH |
1408 | } |
1409 | ||
4a93e180 | 1410 | void |
0aa7f586 | 1411 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1412 | { |
0aa7f586 AM |
1413 | lang_memory_region_name *n; |
1414 | lang_memory_region_type *r; | |
1415 | lang_memory_region_type *region; | |
4a93e180 NC |
1416 | |
1417 | /* The default region must be unique. This ensures that it is not necessary | |
1418 | to iterate through the name list if someone wants the check if a region is | |
1419 | the default memory region. */ | |
1420 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1421 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
c1c8c1ef | 1422 | einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
1423 | |
1424 | /* Look for the target region and check if the alias is not already | |
1425 | in use. */ | |
1426 | region = NULL; | |
1427 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1428 | for (n = &r->name_list; n != NULL; n = n->next) | |
1429 | { | |
4724d37e RM |
1430 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1431 | region = r; | |
1432 | if (strcmp (n->name, alias) == 0) | |
c1c8c1ef | 1433 | einfo (_("%F%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1434 | "alias `%s'\n"), |
1435 | NULL, alias); | |
4a93e180 NC |
1436 | } |
1437 | ||
1438 | /* Check if the target region exists. */ | |
1439 | if (region == NULL) | |
c1c8c1ef | 1440 | einfo (_("%F%P:%pS: error: memory region `%s' " |
4724d37e RM |
1441 | "for alias `%s' does not exist\n"), |
1442 | NULL, region_name, alias); | |
4a93e180 NC |
1443 | |
1444 | /* Add alias to region name list. */ | |
988de25b | 1445 | n = stat_alloc (sizeof (lang_memory_region_name)); |
4a93e180 NC |
1446 | n->name = xstrdup (alias); |
1447 | n->next = region->name_list.next; | |
1448 | region->name_list.next = n; | |
1449 | } | |
1450 | ||
5f992e62 | 1451 | static lang_memory_region_type * |
0aa7f586 | 1452 | lang_memory_default (asection *section) |
252b5132 RH |
1453 | { |
1454 | lang_memory_region_type *p; | |
1455 | ||
1456 | flagword sec_flags = section->flags; | |
1457 | ||
1458 | /* Override SEC_DATA to mean a writable section. */ | |
1459 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1460 | sec_flags |= SEC_DATA; | |
1461 | ||
1579bae1 | 1462 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1463 | { |
1464 | if ((p->flags & sec_flags) != 0 | |
1465 | && (p->not_flags & sec_flags) == 0) | |
1466 | { | |
1467 | return p; | |
1468 | } | |
1469 | } | |
f38a2680 | 1470 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
252b5132 RH |
1471 | } |
1472 | ||
24ef1aa7 GM |
1473 | /* Get the output section statement directly from the userdata. */ |
1474 | ||
1475 | lang_output_section_statement_type * | |
1476 | lang_output_section_get (const asection *output_section) | |
1477 | { | |
a48931cc | 1478 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1479 | } |
1480 | ||
d127ecce AM |
1481 | /* Find or create an output_section_statement with the given NAME. |
1482 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
21401fc7 AM |
1483 | match any non-negative constraint. If CREATE is 0 return NULL when |
1484 | no match exists. If CREATE is 1, create an output_section_statement | |
1485 | when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2, | |
1486 | always make a new output_section_statement. */ | |
d127ecce | 1487 | |
384a9dda | 1488 | lang_output_section_statement_type * |
d127ecce | 1489 | lang_output_section_statement_lookup (const char *name, |
66c103b7 | 1490 | int constraint, |
21401fc7 | 1491 | int create) |
252b5132 | 1492 | { |
409d7240 | 1493 | struct out_section_hash_entry *entry; |
252b5132 | 1494 | |
409d7240 AM |
1495 | entry = ((struct out_section_hash_entry *) |
1496 | bfd_hash_lookup (&output_section_statement_table, name, | |
f38a2680 | 1497 | create != 0, false)); |
384a9dda AM |
1498 | if (entry == NULL) |
1499 | { | |
66c103b7 | 1500 | if (create) |
df5f2391 | 1501 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1502 | return NULL; |
1503 | } | |
252b5132 | 1504 | |
409d7240 | 1505 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1506 | { |
384a9dda AM |
1507 | /* We have a section of this name, but it might not have the correct |
1508 | constraint. */ | |
66c103b7 | 1509 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1510 | |
d127ecce | 1511 | name = entry->s.output_section_statement.name; |
21401fc7 AM |
1512 | do |
1513 | { | |
1514 | if (create != 2 | |
1515 | && !(create && constraint == SPECIAL) | |
1516 | && (constraint == entry->s.output_section_statement.constraint | |
1517 | || (constraint == 0 | |
1518 | && entry->s.output_section_statement.constraint >= 0))) | |
1519 | return &entry->s.output_section_statement; | |
1520 | last_ent = entry; | |
1521 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1522 | } | |
1523 | while (entry != NULL | |
1524 | && name == entry->s.output_section_statement.name); | |
252b5132 | 1525 | |
66c103b7 AM |
1526 | if (!create) |
1527 | return NULL; | |
1528 | ||
409d7240 AM |
1529 | entry |
1530 | = ((struct out_section_hash_entry *) | |
1531 | output_section_statement_newfunc (NULL, | |
1532 | &output_section_statement_table, | |
1533 | name)); | |
750877ba | 1534 | if (entry == NULL) |
384a9dda | 1535 | { |
df5f2391 | 1536 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1537 | return NULL; |
1538 | } | |
1539 | entry->root = last_ent->root; | |
1540 | last_ent->root.next = &entry->root; | |
252b5132 | 1541 | } |
384a9dda | 1542 | |
409d7240 AM |
1543 | entry->s.output_section_statement.name = name; |
1544 | entry->s.output_section_statement.constraint = constraint; | |
de34d428 AM |
1545 | entry->s.output_section_statement.dup_output = (create == 2 |
1546 | || constraint == SPECIAL); | |
409d7240 | 1547 | return &entry->s.output_section_statement; |
252b5132 RH |
1548 | } |
1549 | ||
d127ecce AM |
1550 | /* Find the next output_section_statement with the same name as OS. |
1551 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1552 | match any non-negative constraint. */ | |
1553 | ||
1554 | lang_output_section_statement_type * | |
1555 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1556 | int constraint) | |
1557 | { | |
1558 | /* All output_section_statements are actually part of a | |
1559 | struct out_section_hash_entry. */ | |
1560 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1561 | ((char *) os | |
1562 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1563 | const char *name = os->name; | |
1564 | ||
1565 | ASSERT (name == entry->root.string); | |
1566 | do | |
1567 | { | |
1568 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1569 | if (entry == NULL | |
1570 | || name != entry->s.output_section_statement.name) | |
1571 | return NULL; | |
1572 | } | |
1573 | while (constraint != entry->s.output_section_statement.constraint | |
1574 | && (constraint != 0 | |
1575 | || entry->s.output_section_statement.constraint < 0)); | |
1576 | ||
1577 | return &entry->s.output_section_statement; | |
1578 | } | |
1579 | ||
afd7a018 AM |
1580 | /* A variant of lang_output_section_find used by place_orphan. |
1581 | Returns the output statement that should precede a new output | |
1582 | statement for SEC. If an exact match is found on certain flags, | |
1583 | sets *EXACT too. */ | |
1584 | ||
1585 | lang_output_section_statement_type * | |
1586 | lang_output_section_find_by_flags (const asection *sec, | |
93638471 | 1587 | flagword sec_flags, |
390fbbf1 AM |
1588 | lang_output_section_statement_type **exact, |
1589 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1590 | { |
1591 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1592 | flagword look_flags, differ; |
afd7a018 AM |
1593 | |
1594 | /* We know the first statement on this list is *ABS*. May as well | |
1595 | skip it. */ | |
8ce18f9c | 1596 | first = (void *) lang_os_list.head; |
afd7a018 AM |
1597 | first = first->next; |
1598 | ||
1599 | /* First try for an exact match. */ | |
1600 | found = NULL; | |
1601 | for (look = first; look; look = look->next) | |
1602 | { | |
d9d94ac8 | 1603 | look_flags = look->flags; |
afd7a018 | 1604 | if (look->bfd_section != NULL) |
ecca9871 | 1605 | { |
d9d94ac8 | 1606 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1607 | if (match_type && !match_type (link_info.output_bfd, |
1608 | look->bfd_section, | |
390fbbf1 | 1609 | sec->owner, sec)) |
ecca9871 L |
1610 | continue; |
1611 | } | |
d9d94ac8 AM |
1612 | differ = look_flags ^ sec_flags; |
1613 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1614 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1615 | found = look; |
1616 | } | |
1617 | if (found != NULL) | |
1618 | { | |
390fbbf1 AM |
1619 | if (exact != NULL) |
1620 | *exact = found; | |
afd7a018 AM |
1621 | return found; |
1622 | } | |
1623 | ||
d9d94ac8 AM |
1624 | if ((sec_flags & SEC_CODE) != 0 |
1625 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1626 | { |
1627 | /* Try for a rw code section. */ | |
1628 | for (look = first; look; look = look->next) | |
1629 | { | |
d9d94ac8 | 1630 | look_flags = look->flags; |
afd7a018 | 1631 | if (look->bfd_section != NULL) |
ecca9871 | 1632 | { |
d9d94ac8 | 1633 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1634 | if (match_type && !match_type (link_info.output_bfd, |
1635 | look->bfd_section, | |
390fbbf1 | 1636 | sec->owner, sec)) |
ecca9871 L |
1637 | continue; |
1638 | } | |
d9d94ac8 AM |
1639 | differ = look_flags ^ sec_flags; |
1640 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1641 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1642 | found = look; |
1643 | } | |
afd7a018 | 1644 | } |
d9d94ac8 AM |
1645 | else if ((sec_flags & SEC_READONLY) != 0 |
1646 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1647 | { |
1648 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1649 | for (look = first; look; look = look->next) | |
1650 | { | |
d9d94ac8 | 1651 | look_flags = look->flags; |
afd7a018 | 1652 | if (look->bfd_section != NULL) |
ecca9871 | 1653 | { |
d9d94ac8 | 1654 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1655 | if (match_type && !match_type (link_info.output_bfd, |
1656 | look->bfd_section, | |
390fbbf1 | 1657 | sec->owner, sec)) |
ecca9871 L |
1658 | continue; |
1659 | } | |
d9d94ac8 AM |
1660 | differ = look_flags ^ sec_flags; |
1661 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1662 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1663 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1664 | | SEC_READONLY)) | |
1665 | && !(look_flags & SEC_SMALL_DATA))) | |
afd7a018 AM |
1666 | found = look; |
1667 | } | |
afd7a018 | 1668 | } |
d9d94ac8 AM |
1669 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 |
1670 | && (sec_flags & SEC_ALLOC) != 0) | |
1671 | { | |
1672 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1673 | as if it were a loaded section, and don't use match_type. */ | |
f38a2680 | 1674 | bool seen_thread_local = false; |
d9d94ac8 AM |
1675 | |
1676 | match_type = NULL; | |
1677 | for (look = first; look; look = look->next) | |
1678 | { | |
1679 | look_flags = look->flags; | |
1680 | if (look->bfd_section != NULL) | |
1681 | look_flags = look->bfd_section->flags; | |
1682 | ||
1683 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1684 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1685 | { | |
1686 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1687 | if (!(look_flags & SEC_LOAD) | |
1688 | && (sec_flags & SEC_LOAD)) | |
1689 | /* ..so if we're at a .tbss section and we're placing | |
1690 | a .tdata section stop looking and return the | |
1691 | previous section. */ | |
1692 | break; | |
1693 | found = look; | |
f38a2680 | 1694 | seen_thread_local = true; |
d9d94ac8 AM |
1695 | } |
1696 | else if (seen_thread_local) | |
1697 | break; | |
1698 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1699 | found = look; | |
1700 | } | |
1701 | } | |
1702 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1703 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1704 | { |
1705 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1706 | for (look = first; look; look = look->next) | |
1707 | { | |
d9d94ac8 | 1708 | look_flags = look->flags; |
afd7a018 | 1709 | if (look->bfd_section != NULL) |
ecca9871 | 1710 | { |
d9d94ac8 | 1711 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1712 | if (match_type && !match_type (link_info.output_bfd, |
1713 | look->bfd_section, | |
390fbbf1 | 1714 | sec->owner, sec)) |
ecca9871 L |
1715 | continue; |
1716 | } | |
d9d94ac8 AM |
1717 | differ = look_flags ^ sec_flags; |
1718 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1719 | | SEC_THREAD_LOCAL)) | |
1720 | || ((look_flags & SEC_SMALL_DATA) | |
1721 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
afd7a018 AM |
1722 | found = look; |
1723 | } | |
afd7a018 | 1724 | } |
d9d94ac8 AM |
1725 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1726 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1727 | { |
1728 | /* .data goes after .rodata. */ | |
1729 | for (look = first; look; look = look->next) | |
1730 | { | |
d9d94ac8 | 1731 | look_flags = look->flags; |
afd7a018 | 1732 | if (look->bfd_section != NULL) |
ecca9871 | 1733 | { |
d9d94ac8 | 1734 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1735 | if (match_type && !match_type (link_info.output_bfd, |
1736 | look->bfd_section, | |
390fbbf1 | 1737 | sec->owner, sec)) |
ecca9871 L |
1738 | continue; |
1739 | } | |
d9d94ac8 AM |
1740 | differ = look_flags ^ sec_flags; |
1741 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1742 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1743 | found = look; |
1744 | } | |
afd7a018 | 1745 | } |
d9d94ac8 | 1746 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1747 | { |
07890c07 | 1748 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1749 | for (look = first; look; look = look->next) |
ecca9871 | 1750 | { |
d9d94ac8 | 1751 | look_flags = look->flags; |
390fbbf1 AM |
1752 | if (look->bfd_section != NULL) |
1753 | { | |
d9d94ac8 | 1754 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1755 | if (match_type && !match_type (link_info.output_bfd, |
1756 | look->bfd_section, | |
390fbbf1 AM |
1757 | sec->owner, sec)) |
1758 | continue; | |
1759 | } | |
d9d94ac8 AM |
1760 | differ = look_flags ^ sec_flags; |
1761 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1762 | found = look; |
ecca9871 | 1763 | } |
afd7a018 | 1764 | } |
07890c07 AM |
1765 | else |
1766 | { | |
1767 | /* non-alloc go last. */ | |
1768 | for (look = first; look; look = look->next) | |
1769 | { | |
d9d94ac8 | 1770 | look_flags = look->flags; |
07890c07 | 1771 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1772 | look_flags = look->bfd_section->flags; |
1773 | differ = look_flags ^ sec_flags; | |
1774 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1775 | found = look; |
1776 | } | |
1777 | return found; | |
1778 | } | |
afd7a018 | 1779 | |
390fbbf1 AM |
1780 | if (found || !match_type) |
1781 | return found; | |
1782 | ||
93638471 | 1783 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
1784 | } |
1785 | ||
1786 | /* Find the last output section before given output statement. | |
1787 | Used by place_orphan. */ | |
1788 | ||
1789 | static asection * | |
1790 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1791 | { | |
afd7a018 AM |
1792 | lang_output_section_statement_type *lookup; |
1793 | ||
218868ba | 1794 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1795 | { |
66c103b7 | 1796 | if (lookup->constraint < 0) |
afd7a018 | 1797 | continue; |
afd7a018 | 1798 | |
18e7a658 | 1799 | if (lookup->bfd_section != NULL) |
218868ba | 1800 | return lookup->bfd_section; |
afd7a018 AM |
1801 | } |
1802 | ||
1803 | return NULL; | |
1804 | } | |
1805 | ||
53d25da6 AM |
1806 | /* Look for a suitable place for a new output section statement. The |
1807 | idea is to skip over anything that might be inside a SECTIONS {} | |
1808 | statement in a script, before we find another output section | |
1809 | statement. Assignments to "dot" before an output section statement | |
0fa4d7cf AM |
1810 | are assumed to belong to it, except in two cases; The first |
1811 | assignment to dot, and assignments before non-alloc sections. | |
1812 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1813 | similar assignments that set the initial address, or we might | |
1814 | insert non-alloc note sections among assignments setting end of | |
1815 | image symbols. */ | |
53d25da6 AM |
1816 | |
1817 | static lang_statement_union_type ** | |
18e7a658 | 1818 | insert_os_after (lang_statement_union_type *after) |
53d25da6 AM |
1819 | { |
1820 | lang_statement_union_type **where; | |
1821 | lang_statement_union_type **assign = NULL; | |
f38a2680 | 1822 | bool ignore_first; |
53d25da6 | 1823 | |
18e7a658 | 1824 | ignore_first = after == lang_os_list.head; |
53d25da6 AM |
1825 | |
1826 | for (where = &after->header.next; | |
1827 | *where != NULL; | |
1828 | where = &(*where)->header.next) | |
1829 | { | |
1830 | switch ((*where)->header.type) | |
1831 | { | |
1832 | case lang_assignment_statement_enum: | |
1833 | if (assign == NULL) | |
1834 | { | |
1835 | lang_assignment_statement_type *ass; | |
1836 | ||
1837 | ass = &(*where)->assignment_statement; | |
1838 | if (ass->exp->type.node_class != etree_assert | |
1839 | && ass->exp->assign.dst[0] == '.' | |
73589426 AM |
1840 | && ass->exp->assign.dst[1] == 0) |
1841 | { | |
1842 | if (!ignore_first) | |
1843 | assign = where; | |
f38a2680 | 1844 | ignore_first = false; |
73589426 | 1845 | } |
53d25da6 | 1846 | } |
53d25da6 AM |
1847 | continue; |
1848 | case lang_wild_statement_enum: | |
1849 | case lang_input_section_enum: | |
1850 | case lang_object_symbols_statement_enum: | |
1851 | case lang_fill_statement_enum: | |
1852 | case lang_data_statement_enum: | |
1853 | case lang_reloc_statement_enum: | |
1854 | case lang_padding_statement_enum: | |
1855 | case lang_constructors_statement_enum: | |
1856 | assign = NULL; | |
f38a2680 | 1857 | ignore_first = false; |
53d25da6 AM |
1858 | continue; |
1859 | case lang_output_section_statement_enum: | |
1860 | if (assign != NULL) | |
0fa4d7cf AM |
1861 | { |
1862 | asection *s = (*where)->output_section_statement.bfd_section; | |
1863 | ||
249b2a84 L |
1864 | if (s == NULL |
1865 | || s->map_head.s == NULL | |
1866 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1867 | where = assign; |
1868 | } | |
53d25da6 AM |
1869 | break; |
1870 | case lang_input_statement_enum: | |
1871 | case lang_address_statement_enum: | |
1872 | case lang_target_statement_enum: | |
1873 | case lang_output_statement_enum: | |
1874 | case lang_group_statement_enum: | |
1875 | case lang_insert_statement_enum: | |
1876 | continue; | |
43ae96e9 MM |
1877 | case lang_input_matcher_enum: |
1878 | FAIL (); | |
53d25da6 AM |
1879 | } |
1880 | break; | |
1881 | } | |
1882 | ||
1883 | return where; | |
1884 | } | |
1885 | ||
afd7a018 | 1886 | lang_output_section_statement_type * |
7b986e99 | 1887 | lang_insert_orphan (asection *s, |
afd7a018 | 1888 | const char *secname, |
8a99a385 | 1889 | int constraint, |
afd7a018 AM |
1890 | lang_output_section_statement_type *after, |
1891 | struct orphan_save *place, | |
1892 | etree_type *address, | |
1893 | lang_statement_list_type *add_child) | |
1894 | { | |
afd7a018 | 1895 | lang_statement_list_type add; |
afd7a018 AM |
1896 | lang_output_section_statement_type *os; |
1897 | lang_output_section_statement_type **os_tail; | |
1898 | ||
afd7a018 AM |
1899 | /* If we have found an appropriate place for the output section |
1900 | statements for this orphan, add them to our own private list, | |
1901 | inserting them later into the global statement list. */ | |
1902 | if (after != NULL) | |
1903 | { | |
bde18da4 AM |
1904 | lang_list_init (&add); |
1905 | push_stat_ptr (&add); | |
afd7a018 AM |
1906 | } |
1907 | ||
0e1862bb L |
1908 | if (bfd_link_relocatable (&link_info) |
1909 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
1910 | address = exp_intop (0); |
1911 | ||
5c1e6d53 | 1912 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
c212f39d FS |
1913 | os = lang_enter_output_section_statement ( |
1914 | secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); | |
011aa75f | 1915 | |
afd7a018 AM |
1916 | if (add_child == NULL) |
1917 | add_child = &os->children; | |
b209b5a6 | 1918 | lang_add_section (add_child, s, NULL, NULL, os); |
afd7a018 | 1919 | |
f77c3948 DJ |
1920 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
1921 | { | |
1922 | const char *region = (after->region | |
1923 | ? after->region->name_list.name | |
1924 | : DEFAULT_MEMORY_REGION); | |
1925 | const char *lma_region = (after->lma_region | |
1926 | ? after->lma_region->name_list.name | |
1927 | : NULL); | |
1928 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
1929 | lma_region); | |
1930 | } | |
1931 | else | |
1932 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
1933 | NULL); | |
afd7a018 | 1934 | |
afd7a018 AM |
1935 | /* Restore the global list pointer. */ |
1936 | if (after != NULL) | |
bde18da4 | 1937 | pop_stat_ptr (); |
afd7a018 AM |
1938 | |
1939 | if (after != NULL && os->bfd_section != NULL) | |
1940 | { | |
5daa8fe7 | 1941 | asection *snew, *as; |
f38a2680 AM |
1942 | bool place_after = place->stmt == NULL; |
1943 | bool insert_after = true; | |
afd7a018 AM |
1944 | |
1945 | snew = os->bfd_section; | |
1946 | ||
1947 | /* Shuffle the bfd section list to make the output file look | |
1948 | neater. This is really only cosmetic. */ | |
1949 | if (place->section == NULL | |
8ce18f9c | 1950 | && after != (void *) lang_os_list.head) |
afd7a018 AM |
1951 | { |
1952 | asection *bfd_section = after->bfd_section; | |
1953 | ||
1954 | /* If the output statement hasn't been used to place any input | |
1955 | sections (and thus doesn't have an output bfd_section), | |
1956 | look for the closest prior output statement having an | |
1957 | output section. */ | |
1958 | if (bfd_section == NULL) | |
1959 | bfd_section = output_prev_sec_find (after); | |
1960 | ||
18e7a658 AM |
1961 | if (bfd_section != NULL |
1962 | && bfd_section->owner != NULL | |
1963 | && bfd_section != snew) | |
afd7a018 AM |
1964 | place->section = &bfd_section->next; |
1965 | } | |
1966 | ||
1967 | if (place->section == NULL) | |
f13a99db | 1968 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1969 | |
5daa8fe7 | 1970 | as = *place->section; |
5e542ba7 MS |
1971 | |
1972 | if (!as) | |
329c1c86 AM |
1973 | { |
1974 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1975 | |
1976 | /* Unlink the section. */ | |
f13a99db | 1977 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1978 | |
1979 | /* Now tack it back on in the right place. */ | |
f13a99db | 1980 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1981 | } |
1887ae73 L |
1982 | else if ((bfd_get_flavour (link_info.output_bfd) |
1983 | == bfd_target_elf_flavour) | |
1984 | && (bfd_get_flavour (s->owner) | |
1985 | == bfd_target_elf_flavour) | |
1986 | && ((elf_section_type (s) == SHT_NOTE | |
1987 | && (s->flags & SEC_LOAD) != 0) | |
1988 | || (elf_section_type (as) == SHT_NOTE | |
1989 | && (as->flags & SEC_LOAD) != 0))) | |
1990 | { | |
1991 | /* Make sure that output note sections are grouped and sorted | |
1992 | by alignments when inserting a note section or insert a | |
1993 | section after a note section, */ | |
1994 | asection *sec; | |
1995 | /* A specific section after which the output note section | |
1996 | should be placed. */ | |
1997 | asection *after_sec; | |
1998 | /* True if we need to insert the orphan section after a | |
1999 | specific section to maintain output note section order. */ | |
f38a2680 | 2000 | bool after_sec_note = false; |
f3e660db L |
2001 | |
2002 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
2003 | |
2004 | /* Group and sort output note section by alignments in | |
2005 | ascending order. */ | |
2006 | after_sec = NULL; | |
2007 | if (elf_section_type (s) == SHT_NOTE | |
2008 | && (s->flags & SEC_LOAD) != 0) | |
2009 | { | |
f3e660db L |
2010 | /* Search from the beginning for the last output note |
2011 | section with equal or larger alignments. NB: Don't | |
2012 | place orphan note section after non-note sections. */ | |
1887ae73 | 2013 | |
f3e660db L |
2014 | first_orphan_note = NULL; |
2015 | for (sec = link_info.output_bfd->sections; | |
1887ae73 L |
2016 | (sec != NULL |
2017 | && !bfd_is_abs_section (sec)); | |
2018 | sec = sec->next) | |
2019 | if (sec != snew | |
2020 | && elf_section_type (sec) == SHT_NOTE | |
2021 | && (sec->flags & SEC_LOAD) != 0) | |
2022 | { | |
f3e660db L |
2023 | if (!first_orphan_note) |
2024 | first_orphan_note = sec; | |
1887ae73 L |
2025 | if (sec->alignment_power >= s->alignment_power) |
2026 | after_sec = sec; | |
2027 | } | |
f3e660db L |
2028 | else if (first_orphan_note) |
2029 | { | |
2030 | /* Stop if there is non-note section after the first | |
2031 | orphan note section. */ | |
2032 | break; | |
2033 | } | |
1887ae73 | 2034 | |
f3e660db L |
2035 | /* If this will be the first orphan note section, it can |
2036 | be placed at the default location. */ | |
2037 | after_sec_note = first_orphan_note != NULL; | |
2038 | if (after_sec == NULL && after_sec_note) | |
1887ae73 | 2039 | { |
f3e660db L |
2040 | /* If all output note sections have smaller |
2041 | alignments, place the section before all | |
2042 | output orphan note sections. */ | |
2043 | after_sec = first_orphan_note; | |
f38a2680 | 2044 | insert_after = false; |
1887ae73 L |
2045 | } |
2046 | } | |
f3e660db | 2047 | else if (first_orphan_note) |
1887ae73 | 2048 | { |
f3e660db L |
2049 | /* Don't place non-note sections in the middle of orphan |
2050 | note sections. */ | |
f38a2680 | 2051 | after_sec_note = true; |
1887ae73 L |
2052 | after_sec = as; |
2053 | for (sec = as->next; | |
2054 | (sec != NULL | |
2055 | && !bfd_is_abs_section (sec)); | |
2056 | sec = sec->next) | |
2057 | if (elf_section_type (sec) == SHT_NOTE | |
2058 | && (sec->flags & SEC_LOAD) != 0) | |
2059 | after_sec = sec; | |
2060 | } | |
2061 | ||
2062 | if (after_sec_note) | |
2063 | { | |
2064 | if (after_sec) | |
2065 | { | |
f3e660db L |
2066 | /* Search forward to insert OS after AFTER_SEC output |
2067 | statement. */ | |
2068 | lang_output_section_statement_type *stmt, *next; | |
f38a2680 | 2069 | bool found = false; |
f3e660db L |
2070 | for (stmt = after; stmt != NULL; stmt = next) |
2071 | { | |
2072 | next = stmt->next; | |
2073 | if (insert_after) | |
2074 | { | |
2075 | if (stmt->bfd_section == after_sec) | |
2076 | { | |
f38a2680 AM |
2077 | place_after = true; |
2078 | found = true; | |
f3e660db L |
2079 | after = stmt; |
2080 | break; | |
2081 | } | |
2082 | } | |
2083 | else | |
2084 | { | |
2085 | /* If INSERT_AFTER is FALSE, place OS before | |
2086 | AFTER_SEC output statement. */ | |
2087 | if (next && next->bfd_section == after_sec) | |
2088 | { | |
f38a2680 AM |
2089 | place_after = true; |
2090 | found = true; | |
f3e660db L |
2091 | after = stmt; |
2092 | break; | |
2093 | } | |
2094 | } | |
2095 | } | |
2096 | ||
2097 | /* Search backward to insert OS after AFTER_SEC output | |
2098 | statement. */ | |
2099 | if (!found) | |
2100 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
1887ae73 | 2101 | { |
f3e660db L |
2102 | if (insert_after) |
2103 | { | |
2104 | if (stmt->bfd_section == after_sec) | |
2105 | { | |
f38a2680 | 2106 | place_after = true; |
f3e660db L |
2107 | after = stmt; |
2108 | break; | |
2109 | } | |
2110 | } | |
2111 | else | |
2112 | { | |
2113 | /* If INSERT_AFTER is FALSE, place OS before | |
2114 | AFTER_SEC output statement. */ | |
2115 | if (stmt->next->bfd_section == after_sec) | |
2116 | { | |
f38a2680 | 2117 | place_after = true; |
f3e660db L |
2118 | after = stmt; |
2119 | break; | |
2120 | } | |
2121 | } | |
1887ae73 L |
2122 | } |
2123 | } | |
2124 | ||
f3e660db L |
2125 | if (after_sec == NULL |
2126 | || (insert_after && after_sec->next != snew) | |
2127 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
2128 | { |
2129 | /* Unlink the section. */ | |
2130 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2131 | ||
2132 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2133 | prepend SNEW. */ | |
2134 | if (after_sec) | |
f3e660db L |
2135 | { |
2136 | if (insert_after) | |
2137 | bfd_section_list_insert_after (link_info.output_bfd, | |
2138 | after_sec, snew); | |
2139 | else | |
2140 | bfd_section_list_insert_before (link_info.output_bfd, | |
2141 | after_sec, snew); | |
2142 | } | |
1887ae73 L |
2143 | else |
2144 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2145 | } | |
2146 | } | |
2147 | else if (as != snew && as->prev != snew) | |
2148 | { | |
2149 | /* Unlink the section. */ | |
2150 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2151 | ||
2152 | /* Now tack it back on in the right place. */ | |
2153 | bfd_section_list_insert_before (link_info.output_bfd, | |
2154 | as, snew); | |
2155 | } | |
2156 | } | |
5e542ba7 | 2157 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2158 | { |
2159 | /* Unlink the section. */ | |
f13a99db | 2160 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2161 | |
5daa8fe7 | 2162 | /* Now tack it back on in the right place. */ |
f13a99db | 2163 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2164 | } |
afd7a018 AM |
2165 | |
2166 | /* Save the end of this list. Further ophans of this type will | |
2167 | follow the one we've just added. */ | |
2168 | place->section = &snew->next; | |
2169 | ||
2170 | /* The following is non-cosmetic. We try to put the output | |
2171 | statements in some sort of reasonable order here, because they | |
2172 | determine the final load addresses of the orphan sections. | |
2173 | In addition, placing output statements in the wrong order may | |
2174 | require extra segments. For instance, given a typical | |
2175 | situation of all read-only sections placed in one segment and | |
2176 | following that a segment containing all the read-write | |
2177 | sections, we wouldn't want to place an orphan read/write | |
2178 | section before or amongst the read-only ones. */ | |
2179 | if (add.head != NULL) | |
2180 | { | |
2181 | lang_output_section_statement_type *newly_added_os; | |
2182 | ||
1887ae73 L |
2183 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2184 | if (place_after) | |
afd7a018 | 2185 | { |
18e7a658 | 2186 | lang_statement_union_type **where; |
afd7a018 | 2187 | |
18e7a658 | 2188 | where = insert_os_after ((lang_statement_union_type *) after); |
afd7a018 AM |
2189 | *add.tail = *where; |
2190 | *where = add.head; | |
2191 | ||
2192 | place->os_tail = &after->next; | |
2193 | } | |
2194 | else | |
2195 | { | |
2196 | /* Put it after the last orphan statement we added. */ | |
2197 | *add.tail = *place->stmt; | |
2198 | *place->stmt = add.head; | |
2199 | } | |
2200 | ||
2201 | /* Fix the global list pointer if we happened to tack our | |
2202 | new list at the tail. */ | |
bde18da4 AM |
2203 | if (*stat_ptr->tail == add.head) |
2204 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
2205 | |
2206 | /* Save the end of this list. */ | |
2207 | place->stmt = add.tail; | |
2208 | ||
2209 | /* Do the same for the list of output section statements. */ | |
2210 | newly_added_os = *os_tail; | |
2211 | *os_tail = NULL; | |
218868ba AM |
2212 | newly_added_os->prev = (lang_output_section_statement_type *) |
2213 | ((char *) place->os_tail | |
2214 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2215 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2216 | if (newly_added_os->next != NULL) |
2217 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
2218 | *place->os_tail = newly_added_os; |
2219 | place->os_tail = &newly_added_os->next; | |
2220 | ||
2221 | /* Fixing the global list pointer here is a little different. | |
2222 | We added to the list in lang_enter_output_section_statement, | |
2223 | trimmed off the new output_section_statment above when | |
2224 | assigning *os_tail = NULL, but possibly added it back in | |
2225 | the same place when assigning *place->os_tail. */ | |
2226 | if (*os_tail == NULL) | |
5c1e6d53 | 2227 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2228 | } |
2229 | } | |
2230 | return os; | |
2231 | } | |
2232 | ||
16e4ecc0 AM |
2233 | static void |
2234 | lang_print_asneeded (void) | |
2235 | { | |
2236 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2237 | |
2238 | if (asneeded_list_head == NULL) | |
2239 | return; | |
2240 | ||
1273da04 | 2241 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2242 | |
2243 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2244 | { | |
6b9bd54c | 2245 | int len; |
16e4ecc0 AM |
2246 | |
2247 | minfo ("%s", m->soname); | |
2248 | len = strlen (m->soname); | |
2249 | ||
2250 | if (len >= 29) | |
2251 | { | |
2252 | print_nl (); | |
2253 | len = 0; | |
2254 | } | |
6b9bd54c | 2255 | print_spaces (30 - len); |
16e4ecc0 AM |
2256 | |
2257 | if (m->ref != NULL) | |
871b3ab2 | 2258 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2259 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2260 | } |
2261 | } | |
2262 | ||
252b5132 | 2263 | static void |
1579bae1 | 2264 | lang_map_flags (flagword flag) |
252b5132 RH |
2265 | { |
2266 | if (flag & SEC_ALLOC) | |
2267 | minfo ("a"); | |
2268 | ||
2269 | if (flag & SEC_CODE) | |
2270 | minfo ("x"); | |
2271 | ||
2272 | if (flag & SEC_READONLY) | |
2273 | minfo ("r"); | |
2274 | ||
2275 | if (flag & SEC_DATA) | |
2276 | minfo ("w"); | |
2277 | ||
2278 | if (flag & SEC_LOAD) | |
2279 | minfo ("l"); | |
2280 | } | |
2281 | ||
2282 | void | |
1579bae1 | 2283 | lang_map (void) |
252b5132 RH |
2284 | { |
2285 | lang_memory_region_type *m; | |
f38a2680 | 2286 | bool dis_header_printed = false; |
252b5132 | 2287 | |
fb221fba NC |
2288 | ldfile_print_input_remaps (); |
2289 | ||
4d4920ec EB |
2290 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2291 | { | |
2292 | asection *s; | |
2293 | ||
2294 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2295 | || file->flags.just_syms) |
4d4920ec EB |
2296 | continue; |
2297 | ||
035801ce FS |
2298 | if (config.print_map_discarded) |
2299 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2300 | if ((s->output_section == NULL | |
2301 | || s->output_section->owner != link_info.output_bfd) | |
2302 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2303 | { | |
2304 | if (! dis_header_printed) | |
2305 | { | |
496917ce | 2306 | minfo (_("\nDiscarded input sections\n\n")); |
f38a2680 | 2307 | dis_header_printed = true; |
035801ce | 2308 | } |
4d4920ec | 2309 | |
f38a2680 | 2310 | print_input_section (s, true); |
035801ce | 2311 | } |
4d4920ec | 2312 | } |
496917ce NC |
2313 | if (config.print_map_discarded && ! dis_header_printed) |
2314 | minfo (_("\nThere are no discarded input sections\n")); | |
4d4920ec | 2315 | |
252b5132 RH |
2316 | minfo (_("\nMemory Configuration\n\n")); |
2317 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2318 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2319 | ||
1579bae1 | 2320 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 | 2321 | { |
6b9bd54c | 2322 | fprintf (config.map_file, "%-16s", m->name_list.name); |
252b5132 | 2323 | |
6b9bd54c AM |
2324 | char buf[32]; |
2325 | bfd_sprintf_vma (link_info.output_bfd, buf, m->origin); | |
2326 | fprintf (config.map_file, " 0x%-16s", buf); | |
2327 | bfd_sprintf_vma (link_info.output_bfd, buf, m->length); | |
2328 | fprintf (config.map_file, | |
2329 | " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf); | |
2330 | if (m->flags) | |
2331 | lang_map_flags (m->flags); | |
252b5132 | 2332 | |
6b9bd54c AM |
2333 | if (m->not_flags) |
2334 | { | |
2335 | minfo ("!"); | |
2336 | lang_map_flags (m->not_flags); | |
252b5132 RH |
2337 | } |
2338 | ||
2339 | print_nl (); | |
2340 | } | |
2341 | ||
496917ce | 2342 | minfo (_("\nLinker script and memory map\n\n")); |
252b5132 | 2343 | |
0aa7f586 | 2344 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2345 | { |
2346 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2347 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2348 | } | |
6a846243 | 2349 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2350 | lang_statement_iteration++; |
252b5132 | 2351 | print_statements (); |
7a2f2d82 | 2352 | |
0aa7f586 AM |
2353 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2354 | config.map_file); | |
252b5132 RH |
2355 | } |
2356 | ||
f38a2680 | 2357 | static bool |
967928e9 AM |
2358 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2359 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2360 | { |
6fcc66ab AM |
2361 | if ((hash_entry->type == bfd_link_hash_defined |
2362 | || hash_entry->type == bfd_link_hash_defweak) | |
2363 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2364 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2365 | { |
6fcc66ab | 2366 | input_section_userdata_type *ud; |
35835446 JR |
2367 | struct map_symbol_def *def; |
2368 | ||
a48931cc | 2369 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2370 | if (!ud) |
35835446 | 2371 | { |
988de25b | 2372 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2373 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2374 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2375 | ud->map_symbol_def_count = 0; | |
35835446 | 2376 | } |
6fcc66ab | 2377 | else if (!ud->map_symbol_def_tail) |
35835446 | 2378 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2379 | |
1e9cc1c2 | 2380 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2381 | def->entry = hash_entry; |
76d7af2d | 2382 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2383 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2384 | ud->map_symbol_def_count++; |
35835446 | 2385 | } |
f38a2680 | 2386 | return true; |
35835446 JR |
2387 | } |
2388 | ||
252b5132 RH |
2389 | /* Initialize an output section. */ |
2390 | ||
2391 | static void | |
dfa7b0b8 | 2392 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2393 | { |
252b5132 | 2394 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
df5f2391 | 2395 | einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2396 | |
de34d428 | 2397 | if (!s->dup_output) |
8a99a385 | 2398 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); |
1579bae1 | 2399 | if (s->bfd_section == NULL) |
8a99a385 AM |
2400 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2401 | s->name, flags); | |
1579bae1 | 2402 | if (s->bfd_section == NULL) |
252b5132 | 2403 | { |
df5f2391 | 2404 | einfo (_("%F%P: output format %s cannot represent section" |
0aa7f586 | 2405 | " called %s: %E\n"), |
f13a99db | 2406 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2407 | } |
2408 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2409 | s->bfd_section->output_offset = 0; |
e0f6802f | 2410 | |
24ef1aa7 GM |
2411 | /* Set the userdata of the output section to the output section |
2412 | statement to avoid lookup. */ | |
a48931cc | 2413 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2414 | |
a431bc2e AM |
2415 | /* If there is a base address, make sure that any sections it might |
2416 | mention are initialized. */ | |
2417 | if (s->addr_tree != NULL) | |
2418 | exp_init_os (s->addr_tree); | |
2419 | ||
2420 | if (s->load_base != NULL) | |
2421 | exp_init_os (s->load_base); | |
2422 | ||
7270c5ed | 2423 | /* If supplied an alignment, set it. */ |
3d9c8f6b AM |
2424 | if (s->section_alignment != NULL) |
2425 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, | |
2426 | "section alignment"); | |
252b5132 RH |
2427 | } |
2428 | ||
2429 | /* Make sure that all output sections mentioned in an expression are | |
2430 | initialized. */ | |
2431 | ||
2432 | static void | |
1579bae1 | 2433 | exp_init_os (etree_type *exp) |
252b5132 RH |
2434 | { |
2435 | switch (exp->type.node_class) | |
2436 | { | |
2437 | case etree_assign: | |
9f4fb502 | 2438 | case etree_provide: |
eab62f2f | 2439 | case etree_provided: |
252b5132 RH |
2440 | exp_init_os (exp->assign.src); |
2441 | break; | |
2442 | ||
2443 | case etree_binary: | |
2444 | exp_init_os (exp->binary.lhs); | |
2445 | exp_init_os (exp->binary.rhs); | |
2446 | break; | |
2447 | ||
2448 | case etree_trinary: | |
2449 | exp_init_os (exp->trinary.cond); | |
2450 | exp_init_os (exp->trinary.lhs); | |
2451 | exp_init_os (exp->trinary.rhs); | |
2452 | break; | |
2453 | ||
b6ca8815 NS |
2454 | case etree_assert: |
2455 | exp_init_os (exp->assert_s.child); | |
2456 | break; | |
afd7a018 | 2457 | |
252b5132 RH |
2458 | case etree_unary: |
2459 | exp_init_os (exp->unary.child); | |
2460 | break; | |
2461 | ||
2462 | case etree_name: | |
2463 | switch (exp->type.node_code) | |
2464 | { | |
2465 | case ADDR: | |
2466 | case LOADADDR: | |
252b5132 RH |
2467 | { |
2468 | lang_output_section_statement_type *os; | |
2469 | ||
2470 | os = lang_output_section_find (exp->name.name); | |
2471 | if (os != NULL && os->bfd_section == NULL) | |
dfa7b0b8 | 2472 | init_os (os, 0); |
252b5132 RH |
2473 | } |
2474 | } | |
2475 | break; | |
2476 | ||
2477 | default: | |
2478 | break; | |
2479 | } | |
2480 | } | |
9503fd87 | 2481 | \f |
252b5132 | 2482 | static void |
1579bae1 | 2483 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2484 | { |
1e9cc1c2 | 2485 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2486 | |
2487 | /* If we are only reading symbols from this object, then we want to | |
2488 | discard all sections. */ | |
66be1055 | 2489 | if (entry->flags.just_syms) |
252b5132 | 2490 | { |
1449d79b | 2491 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2492 | return; |
2493 | } | |
2494 | ||
2e84f9c1 AM |
2495 | /* Deal with SHF_EXCLUDE ELF sections. */ |
2496 | if (!bfd_link_relocatable (&link_info) | |
2497 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2498 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2499 | sec->output_section = bfd_abs_section_ptr; | |
2500 | ||
ace66bb2 | 2501 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2502 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2503 | } |
2504 | \f | |
5b5f4e6f AB |
2505 | |
2506 | /* Returns true if SECTION is one we know will be discarded based on its | |
2507 | section flags, otherwise returns false. */ | |
2508 | ||
f38a2680 | 2509 | static bool |
5b5f4e6f AB |
2510 | lang_discard_section_p (asection *section) |
2511 | { | |
f38a2680 | 2512 | bool discard; |
5b5f4e6f AB |
2513 | flagword flags = section->flags; |
2514 | ||
2515 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2516 | discard = (flags & SEC_EXCLUDE) != 0; | |
2517 | ||
2518 | /* Discard the group descriptor sections when we're finally placing the | |
2519 | sections from within the group. */ | |
2520 | if ((flags & SEC_GROUP) != 0 | |
2521 | && link_info.resolve_section_groups) | |
f38a2680 | 2522 | discard = true; |
5b5f4e6f AB |
2523 | |
2524 | /* Discard debugging sections if we are stripping debugging | |
2525 | information. */ | |
2526 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2527 | && (flags & SEC_DEBUGGING) != 0) | |
f38a2680 | 2528 | discard = true; |
5b5f4e6f AB |
2529 | |
2530 | return discard; | |
2531 | } | |
2532 | ||
670c91c0 MM |
2533 | /* Return TRUE if SECTION is never going to be added to output statement |
2534 | OUTPUT. lang_add_section() definitely won't do anything with SECTION | |
2535 | if this returns TRUE. It may do something (or not) if this returns FALSE. | |
252b5132 | 2536 | |
670c91c0 MM |
2537 | Can be used as early-out to filter matches. This may set |
2538 | output_section of SECTION, if it was unset, to the abs section in case | |
2539 | we discover SECTION to be always discarded. This may also give | |
2540 | warning messages. */ | |
252b5132 | 2541 | |
670c91c0 MM |
2542 | static bool |
2543 | wont_add_section_p (asection *section, | |
2544 | lang_output_section_statement_type *output) | |
252b5132 | 2545 | { |
f38a2680 | 2546 | bool discard; |
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) | |
f38a2680 | 2554 | discard = true; |
252b5132 | 2555 | |
252b5132 RH |
2556 | if (discard) |
2557 | { | |
2558 | if (section->output_section == NULL) | |
2559 | { | |
670c91c0 MM |
2560 | /* This prevents future calls from assigning this section or |
2561 | warning about it again. */ | |
252b5132 RH |
2562 | section->output_section = bfd_abs_section_ptr; |
2563 | } | |
670c91c0 MM |
2564 | else if (bfd_is_abs_section (section->output_section)) |
2565 | ; | |
abf874aa CL |
2566 | else if (link_info.non_contiguous_regions_warnings) |
2567 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
c63d4862 | 2568 | "section `%pA' from `%pB' match /DISCARD/ clause.\n"), |
abf874aa CL |
2569 | NULL, section, section->owner); |
2570 | ||
670c91c0 | 2571 | return true; |
ae17ab41 CM |
2572 | } |
2573 | ||
dfa7b0b8 | 2574 | if (section->output_section != NULL) |
abf874aa CL |
2575 | { |
2576 | if (!link_info.non_contiguous_regions) | |
670c91c0 | 2577 | return true; |
abf874aa CL |
2578 | |
2579 | /* SECTION has already been handled in a special way | |
2580 | (eg. LINK_ONCE): skip it. */ | |
2581 | if (bfd_is_abs_section (section->output_section)) | |
670c91c0 | 2582 | return true; |
abf874aa CL |
2583 | |
2584 | /* Already assigned to the same output section, do not process | |
2585 | it again, to avoid creating loops between duplicate sections | |
2586 | later. */ | |
2587 | if (section->output_section == output->bfd_section) | |
670c91c0 | 2588 | return true; |
abf874aa CL |
2589 | |
2590 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2591 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
c63d4862 | 2592 | "change behaviour for section `%pA' from `%pB' (assigned to " |
abf874aa CL |
2593 | "%pA, but additional match: %pA)\n"), |
2594 | NULL, section, section->owner, section->output_section, | |
2595 | output->bfd_section); | |
2596 | ||
2597 | /* SECTION has already been assigned to an output section, but | |
2598 | the user allows it to be mapped to another one in case it | |
2599 | overflows. We'll later update the actual output section in | |
2600 | size_input_section as appropriate. */ | |
2601 | } | |
252b5132 | 2602 | |
670c91c0 MM |
2603 | return false; |
2604 | } | |
2605 | ||
2606 | /* The wild routines. | |
2607 | ||
2608 | These expand statements like *(.text) and foo.o to a list of | |
2609 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2610 | foo.o(.text, .data). */ | |
2611 | ||
2612 | /* Add SECTION to the output section OUTPUT. Do this by creating a | |
2613 | lang_input_section statement which is placed at PTR. */ | |
2614 | ||
2615 | void | |
2616 | lang_add_section (lang_statement_list_type *ptr, | |
2617 | asection *section, | |
2618 | struct wildcard_list *pattern, | |
2619 | struct flag_info *sflag_info, | |
2620 | lang_output_section_statement_type *output) | |
2621 | { | |
2622 | flagword flags = section->flags; | |
2623 | ||
2624 | lang_input_section_type *new_section; | |
2625 | bfd *abfd = link_info.output_bfd; | |
2626 | ||
2627 | if (wont_add_section_p (section, output)) | |
2628 | return; | |
2629 | ||
2630 | if (sflag_info) | |
2631 | { | |
2632 | bool keep; | |
2633 | ||
2634 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); | |
2635 | if (!keep) | |
2636 | return; | |
2637 | } | |
2638 | ||
dfa7b0b8 AM |
2639 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
2640 | to an output section, because we want to be able to include a | |
2641 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2642 | section (I don't know why we want to do this, but we do). | |
2643 | build_link_order in ldwrite.c handles this case by turning | |
2644 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2645 | flags &= ~ SEC_NEVER_LOAD; | |
8423293d | 2646 | |
dfa7b0b8 AM |
2647 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2648 | already been processed. One reason to do this is that on pe | |
2649 | format targets, .text$foo sections go into .text and it's odd | |
2650 | to see .text with SEC_LINK_ONCE set. */ | |
7bdf4127 AB |
2651 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2652 | { | |
2653 | if (link_info.resolve_section_groups) | |
6c19b93b | 2654 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2655 | else |
6c19b93b | 2656 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2657 | } |
2658 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2659 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2660 | |
dfa7b0b8 AM |
2661 | switch (output->sectype) |
2662 | { | |
2663 | case normal_section: | |
2664 | case overlay_section: | |
7b243801 | 2665 | case first_overlay_section: |
c212f39d | 2666 | case type_section: |
dfa7b0b8 AM |
2667 | break; |
2668 | case noalloc_section: | |
2669 | flags &= ~SEC_ALLOC; | |
2670 | break; | |
c212f39d | 2671 | case typed_readonly_section: |
6b86da53 LB |
2672 | case readonly_section: |
2673 | flags |= SEC_READONLY; | |
2674 | break; | |
dfa7b0b8 AM |
2675 | case noload_section: |
2676 | flags &= ~SEC_LOAD; | |
2677 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
2678 | /* Unfortunately GNU ld has managed to evolve two different |
2679 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2680 | alloc, no contents section. All others get a noload, noalloc | |
2681 | section. */ | |
2682 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
2e76e85a | 2683 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2684 | else |
2685 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2686 | break; |
2687 | } | |
252b5132 | 2688 | |
dfa7b0b8 AM |
2689 | if (output->bfd_section == NULL) |
2690 | init_os (output, flags); | |
252b5132 | 2691 | |
dfa7b0b8 AM |
2692 | /* If SEC_READONLY is not set in the input section, then clear |
2693 | it from the output section. */ | |
2694 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
252b5132 | 2695 | |
dfa7b0b8 AM |
2696 | if (output->bfd_section->linker_has_input) |
2697 | { | |
2698 | /* Only set SEC_READONLY flag on the first input section. */ | |
2699 | flags &= ~ SEC_READONLY; | |
252b5132 | 2700 | |
f5fa8ca2 | 2701 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
2702 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
2703 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2704 | || ((flags & SEC_MERGE) != 0 | |
2705 | && output->bfd_section->entsize != section->entsize)) | |
f5fa8ca2 | 2706 | { |
afd7a018 | 2707 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2708 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2709 | } | |
dfa7b0b8 AM |
2710 | } |
2711 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2712 | |
dfa7b0b8 AM |
2713 | if (!output->bfd_section->linker_has_input) |
2714 | { | |
2715 | output->bfd_section->linker_has_input = 1; | |
2716 | /* This must happen after flags have been updated. The output | |
2717 | section may have been created before we saw its first input | |
2718 | section, eg. for a data statement. */ | |
2719 | bfd_init_private_section_data (section->owner, section, | |
2720 | link_info.output_bfd, | |
2721 | output->bfd_section, | |
2722 | &link_info); | |
2723 | if ((flags & SEC_MERGE) != 0) | |
afd7a018 | 2724 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2725 | } |
f5fa8ca2 | 2726 | |
dfa7b0b8 AM |
2727 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
2728 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2729 | { | |
2730 | /* FIXME: This value should really be obtained from the bfd... */ | |
2731 | output->block_value = 128; | |
2732 | } | |
252b5132 | 2733 | |
b02db378 AM |
2734 | /* When a .ctors section is placed in .init_array it must be copied |
2735 | in reverse order. Similarly for .dtors. Set that up. */ | |
2736 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
2737 | && ((startswith (section->name, ".ctors") | |
2738 | && strcmp (output->bfd_section->name, ".init_array") == 0) | |
2739 | || (startswith (section->name, ".dtors") | |
2740 | && strcmp (output->bfd_section->name, ".fini_array") == 0)) | |
2741 | && (section->name[6] == 0 || section->name[6] == '.')) | |
2742 | section->flags |= SEC_ELF_REVERSE_COPY; | |
2743 | ||
dfa7b0b8 AM |
2744 | if (section->alignment_power > output->bfd_section->alignment_power) |
2745 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2746 | |
dfa7b0b8 | 2747 | section->output_section = output->bfd_section; |
252b5132 | 2748 | |
5b69e357 | 2749 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
2750 | { |
2751 | asection *s = output->bfd_section->map_tail.s; | |
2752 | output->bfd_section->map_tail.s = section; | |
2753 | section->map_head.s = NULL; | |
2754 | section->map_tail.s = s; | |
2755 | if (s != NULL) | |
2756 | s->map_head.s = section; | |
2757 | else | |
2758 | output->bfd_section->map_head.s = section; | |
252b5132 | 2759 | } |
dfa7b0b8 AM |
2760 | |
2761 | /* Add a section reference to the list. */ | |
2762 | new_section = new_stat (lang_input_section, ptr); | |
2763 | new_section->section = section; | |
b209b5a6 | 2764 | new_section->pattern = pattern; |
252b5132 RH |
2765 | } |
2766 | ||
252b5132 | 2767 | /* Expand a wild statement for a particular FILE. SECTION may be |
af31506c MM |
2768 | NULL, in which case it is a wild card. This assumes that the |
2769 | wild statement doesn't need any sorting (of filenames or sections). */ | |
252b5132 RH |
2770 | |
2771 | static void | |
af31506c MM |
2772 | output_section_callback_nosort (lang_wild_statement_type *ptr, |
2773 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2774 | asection *section, | |
2775 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
2776 | void *output) | |
4dec4d4e | 2777 | { |
d0bf826b AM |
2778 | lang_output_section_statement_type *os; |
2779 | ||
2780 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2781 | |
b6bf44ba | 2782 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2783 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2784 | return; |
2785 | ||
af31506c MM |
2786 | lang_add_section (&ptr->children, section, ptr->section_list, |
2787 | ptr->section_flag_list, os); | |
252b5132 RH |
2788 | } |
2789 | ||
0841712e JJ |
2790 | /* Check if all sections in a wild statement for a particular FILE |
2791 | are readonly. */ | |
2792 | ||
2793 | static void | |
2794 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2795 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2796 | asection *section, | |
2797 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
d0bf826b | 2798 | void *output) |
0841712e | 2799 | { |
d0bf826b AM |
2800 | lang_output_section_statement_type *os; |
2801 | ||
2802 | os = (lang_output_section_statement_type *) output; | |
2803 | ||
0841712e | 2804 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2805 | if (unique_section_p (section, os)) |
0841712e JJ |
2806 | return; |
2807 | ||
6feb9908 | 2808 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
f38a2680 | 2809 | os->all_input_readonly = false; |
0841712e JJ |
2810 | } |
2811 | ||
252b5132 RH |
2812 | /* This is passed a file name which must have been seen already and |
2813 | added to the statement tree. We will see if it has been opened | |
2814 | already and had its symbols read. If not then we'll read it. */ | |
2815 | ||
2816 | static lang_input_statement_type * | |
1579bae1 | 2817 | lookup_name (const char *name) |
252b5132 RH |
2818 | { |
2819 | lang_input_statement_type *search; | |
2820 | ||
8ce18f9c | 2821 | for (search = (void *) input_file_chain.head; |
1579bae1 | 2822 | search != NULL; |
36983a93 | 2823 | search = search->next_real_file) |
252b5132 | 2824 | { |
0013291d NC |
2825 | /* Use the local_sym_name as the name of the file that has |
2826 | already been loaded as filename might have been transformed | |
2827 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2828 | const char *filename = search->local_sym_name; |
0013291d | 2829 | |
0013291d | 2830 | if (filename != NULL |
42627821 | 2831 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2832 | break; |
2833 | } | |
2834 | ||
1579bae1 | 2835 | if (search == NULL) |
1f1f5b92 AM |
2836 | { |
2837 | /* Arrange to splice the input statement added by new_afile into | |
2838 | statement_list after the current input_file_chain tail. | |
2839 | We know input_file_chain is not an empty list, and that | |
2840 | lookup_name was called via open_input_bfds. Later calls to | |
2841 | lookup_name should always match an existing input_statement. */ | |
2842 | lang_statement_union_type **tail = stat_ptr->tail; | |
2843 | lang_statement_union_type **after | |
2844 | = (void *) ((char *) input_file_chain.tail | |
2845 | - offsetof (lang_input_statement_type, next_real_file) | |
2846 | + offsetof (lang_input_statement_type, header.next)); | |
2847 | lang_statement_union_type *rest = *after; | |
2848 | stat_ptr->tail = after; | |
2849 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
16171946 | 2850 | default_target, NULL); |
1f1f5b92 AM |
2851 | *stat_ptr->tail = rest; |
2852 | if (*tail == NULL) | |
2853 | stat_ptr->tail = tail; | |
2854 | } | |
252b5132 RH |
2855 | |
2856 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 2857 | don't add this file. */ |
66be1055 | 2858 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
2859 | return search; |
2860 | ||
0aa7f586 | 2861 | if (!load_symbols (search, NULL)) |
6770ec8c | 2862 | return NULL; |
252b5132 RH |
2863 | |
2864 | return search; | |
2865 | } | |
2866 | ||
b58f81ae DJ |
2867 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2868 | ||
2869 | struct excluded_lib | |
2870 | { | |
2871 | char *name; | |
2872 | struct excluded_lib *next; | |
2873 | }; | |
2874 | static struct excluded_lib *excluded_libs; | |
2875 | ||
2876 | void | |
2877 | add_excluded_libs (const char *list) | |
2878 | { | |
2879 | const char *p = list, *end; | |
2880 | ||
2881 | while (*p != '\0') | |
2882 | { | |
2883 | struct excluded_lib *entry; | |
2884 | end = strpbrk (p, ",:"); | |
2885 | if (end == NULL) | |
2886 | end = p + strlen (p); | |
1e9cc1c2 | 2887 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
b58f81ae | 2888 | entry->next = excluded_libs; |
1e9cc1c2 | 2889 | entry->name = (char *) xmalloc (end - p + 1); |
b58f81ae DJ |
2890 | memcpy (entry->name, p, end - p); |
2891 | entry->name[end - p] = '\0'; | |
2892 | excluded_libs = entry; | |
2893 | if (*end == '\0') | |
329c1c86 | 2894 | break; |
b58f81ae DJ |
2895 | p = end + 1; |
2896 | } | |
2897 | } | |
2898 | ||
2899 | static void | |
2900 | check_excluded_libs (bfd *abfd) | |
2901 | { | |
2902 | struct excluded_lib *lib = excluded_libs; | |
2903 | ||
2904 | while (lib) | |
2905 | { | |
2906 | int len = strlen (lib->name); | |
607b4833 | 2907 | const char *filename = lbasename (bfd_get_filename (abfd)); |
b58f81ae DJ |
2908 | |
2909 | if (strcmp (lib->name, "ALL") == 0) | |
2910 | { | |
f38a2680 | 2911 | abfd->no_export = true; |
b58f81ae DJ |
2912 | return; |
2913 | } | |
2914 | ||
42627821 | 2915 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
2916 | && (filename[len] == '\0' |
2917 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
2918 | && filename[len + 2] == '\0'))) | |
2919 | { | |
f38a2680 | 2920 | abfd->no_export = true; |
b58f81ae DJ |
2921 | return; |
2922 | } | |
2923 | ||
2924 | lib = lib->next; | |
2925 | } | |
2926 | } | |
2927 | ||
252b5132 RH |
2928 | /* Get the symbols for an input file. */ |
2929 | ||
f38a2680 | 2930 | bool |
1579bae1 AM |
2931 | load_symbols (lang_input_statement_type *entry, |
2932 | lang_statement_list_type *place) | |
252b5132 RH |
2933 | { |
2934 | char **matching; | |
2935 | ||
66be1055 | 2936 | if (entry->flags.loaded) |
f38a2680 | 2937 | return true; |
252b5132 RH |
2938 | |
2939 | ldfile_open_file (entry); | |
2940 | ||
c4b78195 | 2941 | /* Do not process further if the file was missing. */ |
66be1055 | 2942 | if (entry->flags.missing_file) |
f38a2680 | 2943 | return true; |
c4b78195 | 2944 | |
727a29ba AM |
2945 | if (trace_files || verbose) |
2946 | info_msg ("%pI\n", entry); | |
2947 | ||
0aa7f586 AM |
2948 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
2949 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
2950 | { |
2951 | bfd_error_type err; | |
66be1055 | 2952 | struct lang_input_statement_flags save_flags; |
47e2e729 | 2953 | extern FILE *yyin; |
b7a26f91 | 2954 | |
252b5132 | 2955 | err = bfd_get_error (); |
884fb58e NC |
2956 | |
2957 | /* See if the emulation has some special knowledge. */ | |
2958 | if (ldemul_unrecognized_file (entry)) | |
370426d0 AM |
2959 | { |
2960 | if (err == bfd_error_file_ambiguously_recognized) | |
2961 | free (matching); | |
2962 | return true; | |
2963 | } | |
884fb58e | 2964 | |
252b5132 RH |
2965 | if (err == bfd_error_file_ambiguously_recognized) |
2966 | { | |
2967 | char **p; | |
2968 | ||
df5f2391 AM |
2969 | einfo (_("%P: %pB: file not recognized: %E;" |
2970 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
2971 | for (p = matching; *p != NULL; p++) |
2972 | einfo (" %s", *p); | |
370426d0 | 2973 | free (matching); |
252b5132 RH |
2974 | einfo ("%F\n"); |
2975 | } | |
2976 | else if (err != bfd_error_file_not_recognized | |
2977 | || place == NULL) | |
df5f2391 | 2978 | einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 2979 | |
252b5132 RH |
2980 | bfd_close (entry->the_bfd); |
2981 | entry->the_bfd = NULL; | |
2982 | ||
252b5132 | 2983 | /* Try to interpret the file as a linker script. */ |
66be1055 | 2984 | save_flags = input_flags; |
252b5132 RH |
2985 | ldfile_open_command_file (entry->filename); |
2986 | ||
bde18da4 | 2987 | push_stat_ptr (place); |
66be1055 AM |
2988 | input_flags.add_DT_NEEDED_for_regular |
2989 | = entry->flags.add_DT_NEEDED_for_regular; | |
2990 | input_flags.add_DT_NEEDED_for_dynamic | |
2991 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
2992 | input_flags.whole_archive = entry->flags.whole_archive; | |
2993 | input_flags.dynamic = entry->flags.dynamic; | |
252b5132 | 2994 | |
f38a2680 | 2995 | ldfile_assumed_script = true; |
252b5132 | 2996 | parser_input = input_script; |
16171946 | 2997 | current_input_file = entry->filename; |
252b5132 | 2998 | yyparse (); |
16171946 | 2999 | current_input_file = NULL; |
f38a2680 | 3000 | ldfile_assumed_script = false; |
252b5132 | 3001 | |
f4a23d42 AM |
3002 | /* missing_file is sticky. sysrooted will already have been |
3003 | restored when seeing EOF in yyparse, but no harm to restore | |
3004 | again. */ | |
66be1055 AM |
3005 | save_flags.missing_file |= input_flags.missing_file; |
3006 | input_flags = save_flags; | |
bde18da4 | 3007 | pop_stat_ptr (); |
47e2e729 AM |
3008 | fclose (yyin); |
3009 | yyin = NULL; | |
f38a2680 | 3010 | entry->flags.loaded = true; |
252b5132 | 3011 | |
f38a2680 | 3012 | return true; |
252b5132 RH |
3013 | } |
3014 | ||
3015 | if (ldemul_recognized_file (entry)) | |
f38a2680 | 3016 | return true; |
252b5132 RH |
3017 | |
3018 | /* We don't call ldlang_add_file for an archive. Instead, the | |
3019 | add_symbols entry point will call ldlang_add_file, via the | |
3020 | add_archive_element callback, for each element of the archive | |
3021 | which is used. */ | |
3022 | switch (bfd_get_format (entry->the_bfd)) | |
3023 | { | |
3024 | default: | |
3025 | break; | |
3026 | ||
3027 | case bfd_object: | |
66be1055 | 3028 | if (!entry->flags.reload) |
15fc2e13 | 3029 | ldlang_add_file (entry); |
252b5132 RH |
3030 | break; |
3031 | ||
3032 | case bfd_archive: | |
b58f81ae DJ |
3033 | check_excluded_libs (entry->the_bfd); |
3034 | ||
00f93c44 | 3035 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 3036 | if (entry->flags.whole_archive) |
252b5132 | 3037 | { |
b7a26f91 | 3038 | bfd *member = NULL; |
f38a2680 | 3039 | bool loaded = true; |
6770ec8c NC |
3040 | |
3041 | for (;;) | |
252b5132 | 3042 | { |
5d3236ee | 3043 | bfd *subsbfd; |
6770ec8c NC |
3044 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
3045 | ||
3046 | if (member == NULL) | |
3047 | break; | |
b7a26f91 | 3048 | |
0aa7f586 | 3049 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 3050 | { |
df5f2391 | 3051 | einfo (_("%F%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 3052 | entry->the_bfd, member); |
f38a2680 | 3053 | loaded = false; |
6770ec8c NC |
3054 | } |
3055 | ||
db0ca79f | 3056 | subsbfd = member; |
46105645 AM |
3057 | if (!(*link_info.callbacks |
3058 | ->add_archive_element) (&link_info, member, | |
3059 | "--whole-archive", &subsbfd)) | |
252b5132 | 3060 | abort (); |
6770ec8c | 3061 | |
5d3236ee DK |
3062 | /* Potentially, the add_archive_element hook may have set a |
3063 | substitute BFD for us. */ | |
46105645 | 3064 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 3065 | { |
df5f2391 | 3066 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), member); |
f38a2680 | 3067 | loaded = false; |
6770ec8c | 3068 | } |
252b5132 RH |
3069 | } |
3070 | ||
66be1055 | 3071 | entry->flags.loaded = loaded; |
6770ec8c | 3072 | return loaded; |
252b5132 | 3073 | } |
6770ec8c | 3074 | break; |
252b5132 RH |
3075 | } |
3076 | ||
03bdc404 | 3077 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
f38a2680 | 3078 | entry->flags.loaded = true; |
6770ec8c | 3079 | else |
df5f2391 | 3080 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3081 | |
66be1055 | 3082 | return entry->flags.loaded; |
252b5132 RH |
3083 | } |
3084 | ||
b6bf44ba AM |
3085 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
3086 | may be NULL, indicating that it is a wildcard. Separate | |
3087 | lang_input_section statements are created for each part of the | |
3088 | expansion; they are added after the wild statement S. OUTPUT is | |
3089 | the output section. */ | |
252b5132 RH |
3090 | |
3091 | static void | |
1579bae1 AM |
3092 | wild (lang_wild_statement_type *s, |
3093 | const char *target ATTRIBUTE_UNUSED, | |
3094 | lang_output_section_statement_type *output) | |
252b5132 | 3095 | { |
b6bf44ba | 3096 | struct wildcard_list *sec; |
252b5132 | 3097 | |
af31506c | 3098 | if (s->filenames_sorted || s->any_specs_sorted) |
50c77e5d | 3099 | { |
329c1c86 AM |
3100 | lang_section_bst_type *tree; |
3101 | ||
af31506c | 3102 | walk_wild (s, output_section_callback_sort, output); |
50c77e5d | 3103 | |
e6f2cbf5 | 3104 | tree = s->tree; |
329c1c86 | 3105 | if (tree) |
e6f2cbf5 L |
3106 | { |
3107 | output_section_callback_tree_to_list (s, tree, output); | |
3108 | s->tree = NULL; | |
af31506c | 3109 | s->rightmost = &s->tree; |
e6f2cbf5 | 3110 | } |
50c77e5d | 3111 | } |
329c1c86 | 3112 | else |
af31506c | 3113 | walk_wild (s, output_section_callback_nosort, output); |
b6bf44ba | 3114 | |
abe6ac95 AM |
3115 | if (default_common_section == NULL) |
3116 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
3117 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
3118 | { | |
3119 | /* Remember the section that common is going to in case we | |
b7a26f91 | 3120 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3121 | default_common_section = output; |
abe6ac95 | 3122 | break; |
b6bf44ba | 3123 | } |
252b5132 RH |
3124 | } |
3125 | ||
b34976b6 | 3126 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3127 | |
e50d8076 | 3128 | static int |
1579bae1 | 3129 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3130 | { |
1e9cc1c2 | 3131 | const char *sought = (const char *) data; |
5f992e62 | 3132 | |
e50d8076 NC |
3133 | return strcmp (target->name, sought) == 0; |
3134 | } | |
3135 | ||
3136 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3137 | |
e50d8076 | 3138 | static void |
1d38e9d1 | 3139 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3140 | { |
3141 | char c; | |
5f992e62 | 3142 | |
08da4cac | 3143 | while ((c = *src++) != 0) |
3882b010 | 3144 | *dest++ = TOLOWER (c); |
e50d8076 | 3145 | |
08da4cac | 3146 | *dest = 0; |
e50d8076 NC |
3147 | } |
3148 | ||
396a2467 | 3149 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3150 | from haystack. */ |
08da4cac | 3151 | |
e50d8076 | 3152 | static void |
1d38e9d1 | 3153 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3154 | { |
3155 | haystack = strstr (haystack, needle); | |
5f992e62 | 3156 | |
e50d8076 NC |
3157 | if (haystack) |
3158 | { | |
08da4cac | 3159 | char *src; |
e50d8076 | 3160 | |
08da4cac KH |
3161 | for (src = haystack + strlen (needle); *src;) |
3162 | *haystack++ = *src++; | |
5f992e62 | 3163 | |
08da4cac | 3164 | *haystack = 0; |
e50d8076 NC |
3165 | } |
3166 | } | |
3167 | ||
3168 | /* Compare two target format name strings. | |
3169 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3170 | |
e50d8076 | 3171 | static int |
1d38e9d1 | 3172 | name_compare (const char *first, const char *second) |
e50d8076 | 3173 | { |
08da4cac KH |
3174 | char *copy1; |
3175 | char *copy2; | |
3176 | int result; | |
5f992e62 | 3177 | |
1e9cc1c2 NC |
3178 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3179 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3180 | |
3181 | /* Convert the names to lower case. */ | |
3182 | stricpy (copy1, first); | |
3183 | stricpy (copy2, second); | |
3184 | ||
1579bae1 | 3185 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
3186 | strcut (copy1, "big"); |
3187 | strcut (copy1, "little"); | |
3188 | strcut (copy2, "big"); | |
3189 | strcut (copy2, "little"); | |
3190 | ||
3191 | /* Return a value based on how many characters match, | |
3192 | starting from the beginning. If both strings are | |
3193 | the same then return 10 * their length. */ | |
08da4cac KH |
3194 | for (result = 0; copy1[result] == copy2[result]; result++) |
3195 | if (copy1[result] == 0) | |
e50d8076 NC |
3196 | { |
3197 | result *= 10; | |
3198 | break; | |
3199 | } | |
5f992e62 | 3200 | |
e50d8076 NC |
3201 | free (copy1); |
3202 | free (copy2); | |
3203 | ||
3204 | return result; | |
3205 | } | |
3206 | ||
3207 | /* Set by closest_target_match() below. */ | |
08da4cac | 3208 | static const bfd_target *winner; |
e50d8076 NC |
3209 | |
3210 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3211 | requirement that was specified on the command line, and is the nearest | |
3212 | match to the original output target. */ | |
08da4cac | 3213 | |
e50d8076 | 3214 | static int |
1579bae1 | 3215 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3216 | { |
1e9cc1c2 | 3217 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3218 | |
08da4cac KH |
3219 | if (command_line.endian == ENDIAN_BIG |
3220 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3221 | return 0; |
5f992e62 | 3222 | |
08da4cac KH |
3223 | if (command_line.endian == ENDIAN_LITTLE |
3224 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3225 | return 0; |
3226 | ||
3227 | /* Must be the same flavour. */ | |
3228 | if (target->flavour != original->flavour) | |
3229 | return 0; | |
3230 | ||
de7dd2bd | 3231 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3232 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
3233 | || strcmp (target->name, "elf64-big") == 0 |
3234 | || strcmp (target->name, "elf32-little") == 0 | |
3235 | || strcmp (target->name, "elf64-little") == 0) | |
3236 | return 0; | |
3237 | ||
e50d8076 NC |
3238 | /* If we have not found a potential winner yet, then record this one. */ |
3239 | if (winner == NULL) | |
3240 | { | |
3241 | winner = target; | |
3242 | return 0; | |
3243 | } | |
3244 | ||
3245 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 3246 | Compare their names and choose the better one. */ |
d1778b88 AM |
3247 | if (name_compare (target->name, original->name) |
3248 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
3249 | winner = target; |
3250 | ||
3251 | /* Keep on searching until wqe have checked them all. */ | |
3252 | return 0; | |
3253 | } | |
3254 | ||
3255 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 3256 | |
1d38e9d1 | 3257 | static const char * |
1579bae1 | 3258 | get_first_input_target (void) |
e50d8076 | 3259 | { |
1d38e9d1 | 3260 | const char *target = NULL; |
e50d8076 NC |
3261 | |
3262 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3263 | { | |
3264 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3265 | && s->flags.real) |
e50d8076 NC |
3266 | { |
3267 | ldfile_open_file (s); | |
5f992e62 | 3268 | |
e50d8076 NC |
3269 | if (s->the_bfd != NULL |
3270 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3271 | { | |
3272 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3273 | |
e50d8076 NC |
3274 | if (target != NULL) |
3275 | break; | |
3276 | } | |
3277 | } | |
3278 | } | |
5f992e62 | 3279 | |
e50d8076 NC |
3280 | return target; |
3281 | } | |
3282 | ||
599917b8 | 3283 | const char * |
1579bae1 | 3284 | lang_get_output_target (void) |
599917b8 JJ |
3285 | { |
3286 | const char *target; | |
3287 | ||
3288 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3289 | if (output_target != NULL) |
599917b8 JJ |
3290 | return output_target; |
3291 | ||
3292 | /* No - has the current target been set to something other than | |
3293 | the default? */ | |
30824704 | 3294 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
3295 | return current_target; |
3296 | ||
3297 | /* No - can we determine the format of the first input file? */ | |
3298 | target = get_first_input_target (); | |
3299 | if (target != NULL) | |
3300 | return target; | |
3301 | ||
3302 | /* Failed - use the default output target. */ | |
3303 | return default_target; | |
3304 | } | |
3305 | ||
252b5132 RH |
3306 | /* Open the output file. */ |
3307 | ||
f13a99db | 3308 | static void |
1579bae1 | 3309 | open_output (const char *name) |
252b5132 | 3310 | { |
43908c16 AM |
3311 | lang_input_statement_type *f; |
3312 | char *out = lrealpath (name); | |
3313 | ||
3314 | for (f = (void *) input_file_chain.head; | |
3315 | f != NULL; | |
3316 | f = f->next_real_file) | |
3317 | if (f->flags.real) | |
3318 | { | |
da1ecf89 | 3319 | char *in = lrealpath (f->local_sym_name); |
43908c16 AM |
3320 | if (filename_cmp (in, out) == 0) |
3321 | einfo (_("%F%P: input file '%s' is the same as output file\n"), | |
3322 | f->filename); | |
3323 | free (in); | |
3324 | } | |
3325 | free (out); | |
3326 | ||
599917b8 | 3327 | output_target = lang_get_output_target (); |
5f992e62 | 3328 | |
08da4cac KH |
3329 | /* Has the user requested a particular endianness on the command |
3330 | line? */ | |
e50d8076 NC |
3331 | if (command_line.endian != ENDIAN_UNSET) |
3332 | { | |
e50d8076 | 3333 | /* Get the chosen target. */ |
4212b42d AM |
3334 | const bfd_target *target |
3335 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3336 | |
c13b1b77 NC |
3337 | /* If the target is not supported, we cannot do anything. */ |
3338 | if (target != NULL) | |
e50d8076 | 3339 | { |
4212b42d AM |
3340 | enum bfd_endian desired_endian; |
3341 | ||
c13b1b77 NC |
3342 | if (command_line.endian == ENDIAN_BIG) |
3343 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3344 | else |
c13b1b77 | 3345 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
3346 | |
3347 | /* See if the target has the wrong endianness. This should | |
3348 | not happen if the linker script has provided big and | |
3349 | little endian alternatives, but some scrips don't do | |
3350 | this. */ | |
c13b1b77 | 3351 | if (target->byteorder != desired_endian) |
e50d8076 | 3352 | { |
c13b1b77 NC |
3353 | /* If it does, then see if the target provides |
3354 | an alternative with the correct endianness. */ | |
3355 | if (target->alternative_target != NULL | |
3356 | && (target->alternative_target->byteorder == desired_endian)) | |
3357 | output_target = target->alternative_target->name; | |
e50d8076 | 3358 | else |
c13b1b77 | 3359 | { |
5f992e62 AM |
3360 | /* Try to find a target as similar as possible to |
3361 | the default target, but which has the desired | |
3362 | endian characteristic. */ | |
4212b42d AM |
3363 | bfd_iterate_over_targets (closest_target_match, |
3364 | (void *) target); | |
5f992e62 AM |
3365 | |
3366 | /* Oh dear - we could not find any targets that | |
3367 | satisfy our requirements. */ | |
c13b1b77 | 3368 | if (winner == NULL) |
6feb9908 AM |
3369 | einfo (_("%P: warning: could not find any targets" |
3370 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3371 | else |
3372 | output_target = winner->name; | |
3373 | } | |
e50d8076 NC |
3374 | } |
3375 | } | |
252b5132 | 3376 | } |
5f992e62 | 3377 | |
f13a99db | 3378 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3379 | |
f13a99db | 3380 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3381 | { |
3382 | if (bfd_get_error () == bfd_error_invalid_target) | |
df5f2391 | 3383 | einfo (_("%F%P: target %s not found\n"), output_target); |
e50d8076 | 3384 | |
df5f2391 | 3385 | einfo (_("%F%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3386 | } |
3387 | ||
f38a2680 | 3388 | delete_output_file_on_failure = true; |
252b5132 | 3389 | |
0aa7f586 | 3390 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
df5f2391 | 3391 | einfo (_("%F%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3392 | if (!bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
3393 | ldfile_output_architecture, |
3394 | ldfile_output_machine)) | |
df5f2391 | 3395 | einfo (_("%F%P: %s: can not set architecture: %E\n"), name); |
252b5132 | 3396 | |
f13a99db | 3397 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3398 | if (link_info.hash == NULL) |
df5f2391 | 3399 | einfo (_("%F%P: can not create hash table: %E\n")); |
252b5132 | 3400 | |
f13a99db | 3401 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3402 | } |
3403 | ||
252b5132 | 3404 | static void |
1579bae1 | 3405 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3406 | { |
3407 | switch (statement->header.type) | |
3408 | { | |
3409 | case lang_output_statement_enum: | |
f13a99db AM |
3410 | ASSERT (link_info.output_bfd == NULL); |
3411 | open_output (statement->output_statement.name); | |
252b5132 | 3412 | ldemul_set_output_arch (); |
0e1862bb L |
3413 | if (config.magic_demand_paged |
3414 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3415 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3416 | else |
f13a99db | 3417 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3418 | if (config.text_read_only) |
f13a99db | 3419 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3420 | else |
f13a99db | 3421 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3422 | if (link_info.traditional_format) |
f13a99db | 3423 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3424 | else |
f13a99db | 3425 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
3426 | break; |
3427 | ||
3428 | case lang_target_statement_enum: | |
3429 | current_target = statement->target_statement.target; | |
3430 | break; | |
3431 | default: | |
3432 | break; | |
3433 | } | |
3434 | } | |
3435 | ||
e5caa5e0 | 3436 | static void |
61826503 | 3437 | init_opb (asection *s) |
e5caa5e0 | 3438 | { |
bb294208 | 3439 | unsigned int x; |
61826503 | 3440 | |
e5caa5e0 | 3441 | opb_shift = 0; |
bb294208 AM |
3442 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
3443 | && s != NULL | |
3444 | && (s->flags & SEC_ELF_OCTETS) != 0) | |
3445 | return; | |
3446 | ||
3447 | x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3448 | ldfile_output_machine); | |
e5caa5e0 AM |
3449 | if (x > 1) |
3450 | while ((x & 1) == 0) | |
3451 | { | |
3452 | x >>= 1; | |
3453 | ++opb_shift; | |
3454 | } | |
3455 | ASSERT (x == 1); | |
3456 | } | |
3457 | ||
252b5132 RH |
3458 | /* Open all the input files. */ |
3459 | ||
486329aa AM |
3460 | enum open_bfd_mode |
3461 | { | |
3462 | OPEN_BFD_NORMAL = 0, | |
3463 | OPEN_BFD_FORCE = 1, | |
3464 | OPEN_BFD_RESCAN = 2 | |
3465 | }; | |
0381901e | 3466 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 | 3467 | static lang_input_statement_type *plugin_insert = NULL; |
b02c4f16 | 3468 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3469 | #endif |
486329aa | 3470 | |
252b5132 | 3471 | static void |
486329aa | 3472 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
252b5132 | 3473 | { |
1579bae1 | 3474 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3475 | { |
3476 | switch (s->header.type) | |
3477 | { | |
3478 | case lang_constructors_statement_enum: | |
486329aa | 3479 | open_input_bfds (constructor_list.head, mode); |
252b5132 RH |
3480 | break; |
3481 | case lang_output_section_statement_enum: | |
486329aa | 3482 | open_input_bfds (s->output_section_statement.children.head, mode); |
252b5132 RH |
3483 | break; |
3484 | case lang_wild_statement_enum: | |
08da4cac | 3485 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3486 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3487 | && s->wild_statement.filename | |
97407faf AM |
3488 | && !wildcardp (s->wild_statement.filename) |
3489 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3490 | lookup_name (s->wild_statement.filename); |
486329aa | 3491 | open_input_bfds (s->wild_statement.children.head, mode); |
252b5132 RH |
3492 | break; |
3493 | case lang_group_statement_enum: | |
3494 | { | |
3495 | struct bfd_link_hash_entry *undefs; | |
0381901e | 3496 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3497 | lang_input_statement_type *plugin_insert_save; |
3498 | #endif | |
252b5132 RH |
3499 | |
3500 | /* We must continually search the entries in the group | |
08da4cac KH |
3501 | until no new symbols are added to the list of undefined |
3502 | symbols. */ | |
252b5132 RH |
3503 | |
3504 | do | |
3505 | { | |
0381901e | 3506 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3507 | plugin_insert_save = plugin_insert; |
3508 | #endif | |
252b5132 | 3509 | undefs = link_info.hash->undefs_tail; |
486329aa AM |
3510 | open_input_bfds (s->group_statement.children.head, |
3511 | mode | OPEN_BFD_FORCE); | |
252b5132 | 3512 | } |
b02c4f16 | 3513 | while (undefs != link_info.hash->undefs_tail |
0381901e | 3514 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3515 | /* Objects inserted by a plugin, which are loaded |
3516 | before we hit this loop, may have added new | |
3517 | undefs. */ | |
3518 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3519 | #endif | |
3520 | ); | |
252b5132 RH |
3521 | } |
3522 | break; | |
3523 | case lang_target_statement_enum: | |
3524 | current_target = s->target_statement.target; | |
3525 | break; | |
3526 | case lang_input_statement_enum: | |
66be1055 | 3527 | if (s->input_statement.flags.real) |
252b5132 | 3528 | { |
bde18da4 | 3529 | lang_statement_union_type **os_tail; |
252b5132 | 3530 | lang_statement_list_type add; |
d215621e | 3531 | bfd *abfd; |
252b5132 RH |
3532 | |
3533 | s->input_statement.target = current_target; | |
3534 | ||
3535 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3536 | is an archive which has already been searched, then |
3537 | force it to be researched unless the whole archive | |
d215621e AM |
3538 | has been loaded already. Do the same for a rescan. |
3539 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3540 | if (mode != OPEN_BFD_NORMAL |
0381901e | 3541 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3542 | && ((mode & OPEN_BFD_RESCAN) == 0 |
3543 | || plugin_insert == NULL) | |
3544 | #endif | |
66be1055 | 3545 | && s->input_statement.flags.loaded |
d215621e AM |
3546 | && (abfd = s->input_statement.the_bfd) != NULL |
3547 | && ((bfd_get_format (abfd) == bfd_archive | |
3548 | && !s->input_statement.flags.whole_archive) | |
3549 | || (bfd_get_format (abfd) == bfd_object | |
3550 | && ((abfd->flags) & DYNAMIC) != 0 | |
3551 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
e77620a5 | 3552 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3553 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3554 | { |
f38a2680 AM |
3555 | s->input_statement.flags.loaded = false; |
3556 | s->input_statement.flags.reload = true; | |
15fc2e13 | 3557 | } |
252b5132 | 3558 | |
5c1e6d53 | 3559 | os_tail = lang_os_list.tail; |
d1778b88 | 3560 | lang_list_init (&add); |
1276aefa | 3561 | |
0aa7f586 | 3562 | if (!load_symbols (&s->input_statement, &add)) |
f38a2680 | 3563 | config.make_executable = false; |
252b5132 RH |
3564 | |
3565 | if (add.head != NULL) | |
3566 | { | |
bde18da4 AM |
3567 | /* If this was a script with output sections then |
3568 | tack any added statements on to the end of the | |
3569 | list. This avoids having to reorder the output | |
3570 | section statement list. Very likely the user | |
3571 | forgot -T, and whatever we do here will not meet | |
3572 | naive user expectations. */ | |
5c1e6d53 | 3573 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
3574 | { |
3575 | einfo (_("%P: warning: %s contains output sections;" | |
3576 | " did you forget -T?\n"), | |
3577 | s->input_statement.filename); | |
3578 | *stat_ptr->tail = add.head; | |
3579 | stat_ptr->tail = add.tail; | |
3580 | } | |
3581 | else | |
3582 | { | |
3583 | *add.tail = s->header.next; | |
3584 | s->header.next = add.head; | |
3585 | } | |
252b5132 RH |
3586 | } |
3587 | } | |
0381901e | 3588 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3589 | /* If we have found the point at which a plugin added new |
3590 | files, clear plugin_insert to enable archive rescan. */ | |
3591 | if (&s->input_statement == plugin_insert) | |
3592 | plugin_insert = NULL; | |
3593 | #endif | |
252b5132 | 3594 | break; |
e759c116 | 3595 | case lang_assignment_statement_enum: |
165f707a | 3596 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
01554a74 | 3597 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
e759c116 | 3598 | break; |
252b5132 RH |
3599 | default: |
3600 | break; | |
3601 | } | |
3602 | } | |
c4b78195 NC |
3603 | |
3604 | /* Exit if any of the files were missing. */ | |
66be1055 | 3605 | if (input_flags.missing_file) |
c4b78195 | 3606 | einfo ("%F"); |
252b5132 RH |
3607 | } |
3608 | ||
094e34f2 | 3609 | #ifdef ENABLE_LIBCTF |
926c9e76 NA |
3610 | /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings |
3611 | that happened specifically at CTF open time. */ | |
3612 | static void | |
139633c3 | 3613 | lang_ctf_errs_warnings (ctf_dict_t *fp) |
926c9e76 NA |
3614 | { |
3615 | ctf_next_t *i = NULL; | |
3616 | char *text; | |
3617 | int is_warning; | |
3618 | int err; | |
3619 | ||
3620 | while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL) | |
3621 | { | |
5e9b84f7 | 3622 | einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), |
926c9e76 NA |
3623 | text); |
3624 | free (text); | |
3625 | } | |
3626 | if (err != ECTF_NEXT_END) | |
3627 | { | |
3628 | einfo (_("CTF error: cannot get CTF errors: `%s'\n"), | |
3629 | ctf_errmsg (err)); | |
3630 | } | |
3631 | ||
3632 | /* `err' returns errors from the error/warning iterator in particular. | |
3633 | These never assert. But if we have an fp, that could have recorded | |
3634 | an assertion failure: assert if it has done so. */ | |
3635 | ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL); | |
3636 | } | |
3637 | ||
1ff6de03 NA |
3638 | /* Open the CTF sections in the input files with libctf: if any were opened, |
3639 | create a fake input file that we'll write the merged CTF data to later | |
3640 | on. */ | |
3641 | ||
3642 | static void | |
3643 | ldlang_open_ctf (void) | |
3644 | { | |
3645 | int any_ctf = 0; | |
3646 | int err; | |
3647 | ||
3648 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3649 | { | |
3650 | asection *sect; | |
3651 | ||
139633c3 | 3652 | /* Incoming files from the compiler have a single ctf_dict_t in them |
1ff6de03 NA |
3653 | (which is presented to us by the libctf API in a ctf_archive_t |
3654 | wrapper): files derived from a previous relocatable link have a CTF | |
3655 | archive containing possibly many CTF files. */ | |
3656 | ||
3657 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3658 | { | |
3659 | if (err != ECTF_NOCTFDATA) | |
926c9e76 NA |
3660 | { |
3661 | lang_ctf_errs_warnings (NULL); | |
3662 | einfo (_("%P: warning: CTF section in %pB not loaded; " | |
5e9b84f7 | 3663 | "its types will be discarded: %s\n"), file->the_bfd, |
1ff6de03 | 3664 | ctf_errmsg (err)); |
926c9e76 | 3665 | } |
1ff6de03 NA |
3666 | continue; |
3667 | } | |
3668 | ||
3669 | /* Prevent the contents of this section from being written, while | |
fa03171f NA |
3670 | requiring the section itself to be duplicated in the output, but only |
3671 | once. */ | |
1ff6de03 NA |
3672 | /* This section must exist if ctf_bfdopen() succeeded. */ |
3673 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3674 | sect->size = 0; | |
3675 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3676 | ||
fa03171f NA |
3677 | if (any_ctf) |
3678 | sect->flags |= SEC_EXCLUDE; | |
1ff6de03 NA |
3679 | any_ctf = 1; |
3680 | } | |
3681 | ||
3682 | if (!any_ctf) | |
3683 | { | |
3684 | ctf_output = NULL; | |
3685 | return; | |
3686 | } | |
3687 | ||
3688 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3689 | return; | |
3690 | ||
c48acf6f | 3691 | einfo (_("%P: warning: CTF output not created: `%s'\n"), |
1ff6de03 NA |
3692 | ctf_errmsg (err)); |
3693 | ||
3694 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3695 | ctf_close (errfile->the_ctf); | |
3696 | } | |
3697 | ||
3698 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3699 | function and data object sections need adjustment. */ | |
3700 | ||
3701 | static void | |
3702 | lang_merge_ctf (void) | |
3703 | { | |
3704 | asection *output_sect; | |
5dba6f05 | 3705 | int flags = 0; |
1ff6de03 NA |
3706 | |
3707 | if (!ctf_output) | |
3708 | return; | |
3709 | ||
3710 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3711 | ||
3712 | /* If the section was discarded, don't waste time merging. */ | |
3713 | if (output_sect == NULL) | |
3714 | { | |
139633c3 | 3715 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
3716 | ctf_output = NULL; |
3717 | ||
3718 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3719 | { | |
3720 | ctf_close (file->the_ctf); | |
3721 | file->the_ctf = NULL; | |
3722 | } | |
3723 | return; | |
3724 | } | |
3725 | ||
3726 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3727 | { | |
3728 | if (!file->the_ctf) | |
3729 | continue; | |
3730 | ||
f320bba5 | 3731 | /* Takes ownership of file->the_ctf. */ |
1ff6de03 NA |
3732 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) |
3733 | { | |
f320bba5 EB |
3734 | einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), |
3735 | file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 NA |
3736 | ctf_close (file->the_ctf); |
3737 | file->the_ctf = NULL; | |
3738 | continue; | |
3739 | } | |
3740 | } | |
3741 | ||
5dba6f05 NA |
3742 | if (!config.ctf_share_duplicated) |
3743 | flags = CTF_LINK_SHARE_UNCONFLICTED; | |
3744 | else | |
3745 | flags = CTF_LINK_SHARE_DUPLICATED; | |
3746 | if (!config.ctf_variables) | |
3747 | flags |= CTF_LINK_OMIT_VARIABLES_SECTION; | |
35a01a04 NA |
3748 | if (bfd_link_relocatable (&link_info)) |
3749 | flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; | |
5dba6f05 NA |
3750 | |
3751 | if (ctf_link (ctf_output, flags) < 0) | |
1ff6de03 | 3752 | { |
926c9e76 | 3753 | lang_ctf_errs_warnings (ctf_output); |
f320bba5 | 3754 | einfo (_("%P: warning: CTF linking failed; " |
5e9b84f7 | 3755 | "output will have no CTF section: %s\n"), |
1ff6de03 NA |
3756 | ctf_errmsg (ctf_errno (ctf_output))); |
3757 | if (output_sect) | |
3758 | { | |
3759 | output_sect->size = 0; | |
3760 | output_sect->flags |= SEC_EXCLUDE; | |
3761 | } | |
3762 | } | |
926c9e76 | 3763 | /* Output any lingering errors that didn't come from ctf_link. */ |
8b37e7b6 | 3764 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3765 | } |
3766 | ||
3d16b64e NA |
3767 | /* Let the emulation acquire strings from the dynamic strtab to help it optimize |
3768 | the CTF, if supported. */ | |
1ff6de03 NA |
3769 | |
3770 | void | |
3d16b64e | 3771 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) |
1ff6de03 | 3772 | { |
3d16b64e NA |
3773 | ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); |
3774 | } | |
3775 | ||
3776 | /* Inform the emulation about the addition of a new dynamic symbol, in BFD | |
3777 | internal format. */ | |
3778 | void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym) | |
3779 | { | |
3780 | ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym); | |
1ff6de03 NA |
3781 | } |
3782 | ||
3783 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3784 | need to dedup against the strtab and symtab, then possibly called from the | |
3785 | target linker code if the dedup has happened. */ | |
3786 | static void | |
3787 | lang_write_ctf (int late) | |
3788 | { | |
3789 | size_t output_size; | |
3790 | asection *output_sect; | |
3791 | ||
3792 | if (!ctf_output) | |
3793 | return; | |
3794 | ||
3795 | if (late) | |
3796 | { | |
3797 | /* Emit CTF late if this emulation says it can do so. */ | |
3798 | if (ldemul_emit_ctf_early ()) | |
3799 | return; | |
3800 | } | |
3801 | else | |
3802 | { | |
3803 | if (!ldemul_emit_ctf_early ()) | |
3804 | return; | |
3805 | } | |
3806 | ||
3d16b64e NA |
3807 | /* Inform the emulation that all the symbols that will be received have |
3808 | been. */ | |
3809 | ||
3810 | ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL); | |
3811 | ||
1ff6de03 NA |
3812 | /* Emit CTF. */ |
3813 | ||
3814 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3815 | if (output_sect) | |
3816 | { | |
3817 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
3818 | CTF_COMPRESSION_THRESHOLD); | |
3819 | output_sect->size = output_size; | |
3820 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
3821 | ||
926c9e76 | 3822 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3823 | if (!output_sect->contents) |
3824 | { | |
f320bba5 | 3825 | einfo (_("%P: warning: CTF section emission failed; " |
5e9b84f7 | 3826 | "output will have no CTF section: %s\n"), |
f320bba5 | 3827 | ctf_errmsg (ctf_errno (ctf_output))); |
1ff6de03 NA |
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. */ | |
139633c3 | 3834 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
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 | } | |
094e34f2 NA |
3850 | #else |
3851 | static void | |
3852 | ldlang_open_ctf (void) | |
3853 | { | |
3854 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3855 | { | |
3856 | asection *sect; | |
3857 | ||
3858 | /* If built without CTF, warn and delete all CTF sections from the output. | |
3859 | (The alternative would be to simply concatenate them, which does not | |
3860 | yield a valid CTF section.) */ | |
3861 | ||
3862 | if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL) | |
3863 | { | |
f320bba5 EB |
3864 | einfo (_("%P: warning: CTF section in %pB not linkable: " |
3865 | "%P was built without support for CTF\n"), file->the_bfd); | |
094e34f2 NA |
3866 | sect->size = 0; |
3867 | sect->flags |= SEC_EXCLUDE; | |
3868 | } | |
3869 | } | |
3870 | } | |
3871 | ||
3872 | static void lang_merge_ctf (void) {} | |
3873 | void | |
3d16b64e NA |
3874 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab |
3875 | ATTRIBUTE_UNUSED) {} | |
3876 | void | |
3877 | ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED, | |
3878 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {} | |
094e34f2 NA |
3879 | static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} |
3880 | void ldlang_write_ctf_late (void) {} | |
3881 | #endif | |
1ff6de03 | 3882 | |
08da4cac | 3883 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3884 | This is a two step process as the symbol table doesn't even exist at |
3885 | the time the ld command line is processed. First we put the name | |
3886 | on a list, then, once the output file has been opened, transfer the | |
3887 | name to the symbol table. */ | |
3888 | ||
e3e942e9 | 3889 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3890 | |
e3e942e9 | 3891 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3892 | |
3893 | void | |
f38a2680 | 3894 | ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) |
252b5132 | 3895 | { |
24898b70 | 3896 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 3897 | |
988de25b | 3898 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
3899 | new_undef->next = ldlang_undef_chain_list_head; |
3900 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 3901 | |
d3ce72d0 | 3902 | new_undef->name = xstrdup (name); |
fcf0e35b | 3903 | |
f13a99db | 3904 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 3905 | insert_undefined (new_undef->name); |
fcf0e35b AM |
3906 | } |
3907 | ||
3908 | /* Insert NAME as undefined in the symbol table. */ | |
3909 | ||
3910 | static void | |
1579bae1 | 3911 | insert_undefined (const char *name) |
fcf0e35b AM |
3912 | { |
3913 | struct bfd_link_hash_entry *h; | |
3914 | ||
f38a2680 | 3915 | h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); |
1579bae1 | 3916 | if (h == NULL) |
df5f2391 | 3917 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
3918 | if (h->type == bfd_link_hash_new) |
3919 | { | |
3920 | h->type = bfd_link_hash_undefined; | |
3921 | h->u.undef.abfd = NULL; | |
f38a2680 | 3922 | h->non_ir_ref_regular = true; |
fcf0e35b AM |
3923 | bfd_link_add_undef (link_info.hash, h); |
3924 | } | |
252b5132 RH |
3925 | } |
3926 | ||
3927 | /* Run through the list of undefineds created above and place them | |
3928 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3929 | script file. */ |
3930 | ||
252b5132 | 3931 | static void |
1579bae1 | 3932 | lang_place_undefineds (void) |
252b5132 RH |
3933 | { |
3934 | ldlang_undef_chain_list_type *ptr; | |
3935 | ||
1579bae1 AM |
3936 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3937 | insert_undefined (ptr->name); | |
252b5132 RH |
3938 | } |
3939 | ||
5789f845 AM |
3940 | /* Mark -u symbols against garbage collection. */ |
3941 | ||
3942 | static void | |
3943 | lang_mark_undefineds (void) | |
3944 | { | |
3945 | ldlang_undef_chain_list_type *ptr; | |
3946 | ||
fc304b88 | 3947 | if (is_elf_hash_table (link_info.hash)) |
5789f845 AM |
3948 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3949 | { | |
3950 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
f38a2680 | 3951 | bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); |
5789f845 AM |
3952 | if (h != NULL) |
3953 | h->mark = 1; | |
3954 | } | |
3955 | } | |
3956 | ||
0a618243 AB |
3957 | /* Structure used to build the list of symbols that the user has required |
3958 | be defined. */ | |
3959 | ||
3960 | struct require_defined_symbol | |
3961 | { | |
3962 | const char *name; | |
3963 | struct require_defined_symbol *next; | |
3964 | }; | |
3965 | ||
3966 | /* The list of symbols that the user has required be defined. */ | |
3967 | ||
3968 | static struct require_defined_symbol *require_defined_symbol_list; | |
3969 | ||
3970 | /* Add a new symbol NAME to the list of symbols that are required to be | |
3971 | defined. */ | |
3972 | ||
3973 | void | |
0aa7f586 | 3974 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
3975 | { |
3976 | struct require_defined_symbol *ptr; | |
3977 | ||
f38a2680 | 3978 | ldlang_add_undef (name, true); |
988de25b | 3979 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 AB |
3980 | ptr->next = require_defined_symbol_list; |
3981 | ptr->name = strdup (name); | |
3982 | require_defined_symbol_list = ptr; | |
3983 | } | |
3984 | ||
3985 | /* Check that all symbols the user required to be defined, are defined, | |
3986 | raise an error if we find a symbol that is not defined. */ | |
3987 | ||
3988 | static void | |
3989 | ldlang_check_require_defined_symbols (void) | |
3990 | { | |
3991 | struct require_defined_symbol *ptr; | |
3992 | ||
3993 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
3994 | { | |
3995 | struct bfd_link_hash_entry *h; | |
3996 | ||
3997 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
f38a2680 | 3998 | false, false, true); |
0a618243 | 3999 | if (h == NULL |
0aa7f586 AM |
4000 | || (h->type != bfd_link_hash_defined |
4001 | && h->type != bfd_link_hash_defweak)) | |
df5f2391 | 4002 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
4003 | } |
4004 | } | |
4005 | ||
0841712e JJ |
4006 | /* Check for all readonly or some readwrite sections. */ |
4007 | ||
4008 | static void | |
6feb9908 AM |
4009 | check_input_sections |
4010 | (lang_statement_union_type *s, | |
4011 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 4012 | { |
988de25b | 4013 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
4014 | { |
4015 | switch (s->header.type) | |
967928e9 AM |
4016 | { |
4017 | case lang_wild_statement_enum: | |
4018 | walk_wild (&s->wild_statement, check_section_callback, | |
4019 | output_section_statement); | |
0aa7f586 | 4020 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4021 | return; |
4022 | break; | |
4023 | case lang_constructors_statement_enum: | |
4024 | check_input_sections (constructor_list.head, | |
4025 | output_section_statement); | |
0aa7f586 | 4026 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4027 | return; |
4028 | break; | |
4029 | case lang_group_statement_enum: | |
4030 | check_input_sections (s->group_statement.children.head, | |
4031 | output_section_statement); | |
0aa7f586 | 4032 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4033 | return; |
4034 | break; | |
4035 | default: | |
4036 | break; | |
4037 | } | |
0841712e JJ |
4038 | } |
4039 | } | |
4040 | ||
bcaa7b3e L |
4041 | /* Update wildcard statements if needed. */ |
4042 | ||
4043 | static void | |
4044 | update_wild_statements (lang_statement_union_type *s) | |
4045 | { | |
4046 | struct wildcard_list *sec; | |
4047 | ||
4048 | switch (sort_section) | |
4049 | { | |
4050 | default: | |
4051 | FAIL (); | |
4052 | ||
4053 | case none: | |
4054 | break; | |
4055 | ||
4056 | case by_name: | |
4057 | case by_alignment: | |
4058 | for (; s != NULL; s = s->header.next) | |
4059 | { | |
4060 | switch (s->header.type) | |
4061 | { | |
4062 | default: | |
4063 | break; | |
4064 | ||
4065 | case lang_wild_statement_enum: | |
0d0999db L |
4066 | for (sec = s->wild_statement.section_list; sec != NULL; |
4067 | sec = sec->next) | |
2d3181c7 AM |
4068 | /* Don't sort .init/.fini sections. */ |
4069 | if (strcmp (sec->spec.name, ".init") != 0 | |
4070 | && strcmp (sec->spec.name, ".fini") != 0) | |
af31506c MM |
4071 | { |
4072 | switch (sec->spec.sorted) | |
4073 | { | |
4074 | case none: | |
4075 | sec->spec.sorted = sort_section; | |
4076 | break; | |
4077 | case by_name: | |
4078 | if (sort_section == by_alignment) | |
4079 | sec->spec.sorted = by_name_alignment; | |
4080 | break; | |
4081 | case by_alignment: | |
4082 | if (sort_section == by_name) | |
4083 | sec->spec.sorted = by_alignment_name; | |
4084 | break; | |
4085 | default: | |
4086 | break; | |
4087 | } | |
4088 | s->wild_statement.any_specs_sorted = true; | |
4089 | } | |
bcaa7b3e L |
4090 | break; |
4091 | ||
4092 | case lang_constructors_statement_enum: | |
4093 | update_wild_statements (constructor_list.head); | |
4094 | break; | |
4095 | ||
4096 | case lang_output_section_statement_enum: | |
2d3181c7 AM |
4097 | update_wild_statements |
4098 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
4099 | break; |
4100 | ||
4101 | case lang_group_statement_enum: | |
4102 | update_wild_statements (s->group_statement.children.head); | |
4103 | break; | |
4104 | } | |
4105 | } | |
4106 | break; | |
4107 | } | |
4108 | } | |
4109 | ||
396a2467 | 4110 | /* Open input files and attach to output sections. */ |
08da4cac | 4111 | |
252b5132 | 4112 | static void |
1579bae1 AM |
4113 | map_input_to_output_sections |
4114 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4115 | lang_output_section_statement_type *os) |
252b5132 | 4116 | { |
1579bae1 | 4117 | for (; s != NULL; s = s->header.next) |
252b5132 | 4118 | { |
dfa7b0b8 AM |
4119 | lang_output_section_statement_type *tos; |
4120 | flagword flags; | |
c212f39d | 4121 | unsigned int type = 0; |
dfa7b0b8 | 4122 | |
252b5132 RH |
4123 | switch (s->header.type) |
4124 | { | |
252b5132 | 4125 | case lang_wild_statement_enum: |
afd7a018 | 4126 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4127 | break; |
252b5132 RH |
4128 | case lang_constructors_statement_enum: |
4129 | map_input_to_output_sections (constructor_list.head, | |
4130 | target, | |
afd7a018 | 4131 | os); |
252b5132 RH |
4132 | break; |
4133 | case lang_output_section_statement_enum: | |
dfa7b0b8 | 4134 | tos = &s->output_section_statement; |
21401fc7 AM |
4135 | if (tos->constraint == ONLY_IF_RW |
4136 | || tos->constraint == ONLY_IF_RO) | |
0841712e | 4137 | { |
f38a2680 | 4138 | tos->all_input_readonly = true; |
dfa7b0b8 AM |
4139 | check_input_sections (tos->children.head, tos); |
4140 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
21401fc7 | 4141 | tos->constraint = -1; |
0841712e | 4142 | } |
21401fc7 AM |
4143 | if (tos->constraint >= 0) |
4144 | map_input_to_output_sections (tos->children.head, | |
4145 | target, | |
4146 | tos); | |
a431bc2e AM |
4147 | break; |
4148 | case lang_output_statement_enum: | |
252b5132 RH |
4149 | break; |
4150 | case lang_target_statement_enum: | |
4151 | target = s->target_statement.target; | |
4152 | break; | |
4153 | case lang_group_statement_enum: | |
4154 | map_input_to_output_sections (s->group_statement.children.head, | |
4155 | target, | |
afd7a018 | 4156 | os); |
252b5132 | 4157 | break; |
384d938f NS |
4158 | case lang_data_statement_enum: |
4159 | /* Make sure that any sections mentioned in the expression | |
4160 | are initialized. */ | |
4161 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4162 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4163 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4164 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4165 | switch (os->sectype) | |
4166 | { | |
4167 | case normal_section: | |
4168 | case overlay_section: | |
7b243801 | 4169 | case first_overlay_section: |
dfa7b0b8 AM |
4170 | break; |
4171 | case noalloc_section: | |
4172 | flags = SEC_HAS_CONTENTS; | |
4173 | break; | |
6b86da53 LB |
4174 | case readonly_section: |
4175 | flags |= SEC_READONLY; | |
4176 | break; | |
c212f39d FS |
4177 | case typed_readonly_section: |
4178 | flags |= SEC_READONLY; | |
4179 | /* Fall through. */ | |
4180 | case type_section: | |
4181 | if (os->sectype_value->type.node_class == etree_name | |
4182 | && os->sectype_value->type.node_code == NAME) | |
4183 | { | |
4184 | const char *name = os->sectype_value->name.name; | |
4185 | if (strcmp (name, "SHT_PROGBITS") == 0) | |
4186 | type = SHT_PROGBITS; | |
4187 | else if (strcmp (name, "SHT_STRTAB") == 0) | |
4188 | type = SHT_STRTAB; | |
4189 | else if (strcmp (name, "SHT_NOTE") == 0) | |
4190 | type = SHT_NOTE; | |
4191 | else if (strcmp (name, "SHT_NOBITS") == 0) | |
4192 | type = SHT_NOBITS; | |
4193 | else if (strcmp (name, "SHT_INIT_ARRAY") == 0) | |
4194 | type = SHT_INIT_ARRAY; | |
4195 | else if (strcmp (name, "SHT_FINI_ARRAY") == 0) | |
4196 | type = SHT_FINI_ARRAY; | |
4197 | else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0) | |
4198 | type = SHT_PREINIT_ARRAY; | |
4199 | else | |
4200 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4201 | os->name); | |
4202 | } | |
4203 | else | |
4204 | { | |
4205 | exp_fold_tree_no_dot (os->sectype_value); | |
4206 | if (expld.result.valid_p) | |
4207 | type = expld.result.value; | |
4208 | else | |
4209 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4210 | os->name); | |
4211 | } | |
4212 | break; | |
dfa7b0b8 | 4213 | case noload_section: |
f4eaaf7f AM |
4214 | if (bfd_get_flavour (link_info.output_bfd) |
4215 | == bfd_target_elf_flavour) | |
4216 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4217 | else | |
4218 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
dfa7b0b8 AM |
4219 | break; |
4220 | } | |
a431bc2e | 4221 | if (os->bfd_section == NULL) |
326adec3 | 4222 | init_os (os, flags | SEC_READONLY); |
a431bc2e AM |
4223 | else |
4224 | os->bfd_section->flags |= flags; | |
c212f39d | 4225 | os->bfd_section->type = type; |
afd7a018 | 4226 | break; |
252b5132 | 4227 | case lang_input_section_enum: |
e0f6802f AM |
4228 | break; |
4229 | case lang_fill_statement_enum: | |
252b5132 | 4230 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4231 | case lang_reloc_statement_enum: |
4232 | case lang_padding_statement_enum: | |
4233 | case lang_input_statement_enum: | |
afd7a018 | 4234 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4235 | init_os (os, 0); |
252b5132 RH |
4236 | break; |
4237 | case lang_assignment_statement_enum: | |
afd7a018 | 4238 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4239 | init_os (os, 0); |
252b5132 RH |
4240 | |
4241 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4242 | are initialized. */ |
252b5132 RH |
4243 | exp_init_os (s->assignment_statement.exp); |
4244 | break; | |
252b5132 | 4245 | case lang_address_statement_enum: |
a431bc2e AM |
4246 | /* Mark the specified section with the supplied address. |
4247 | If this section was actually a segment marker, then the | |
4248 | directive is ignored if the linker script explicitly | |
4249 | processed the segment marker. Originally, the linker | |
4250 | treated segment directives (like -Ttext on the | |
4251 | command-line) as section directives. We honor the | |
370dfff4 | 4252 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4253 | linker scripts that do not specifically check for |
4254 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4255 | if (!s->address_statement.segment |
ba916c8a MM |
4256 | || !s->address_statement.segment->used) |
4257 | { | |
dfa7b0b8 AM |
4258 | const char *name = s->address_statement.section_name; |
4259 | ||
4260 | /* Create the output section statement here so that | |
4261 | orphans with a set address will be placed after other | |
4262 | script sections. If we let the orphan placement code | |
4263 | place them in amongst other sections then the address | |
4264 | will affect following script sections, which is | |
4265 | likely to surprise naive users. */ | |
21401fc7 | 4266 | tos = lang_output_section_statement_lookup (name, 0, 1); |
dfa7b0b8 AM |
4267 | tos->addr_tree = s->address_statement.address; |
4268 | if (tos->bfd_section == NULL) | |
4269 | init_os (tos, 0); | |
ba916c8a | 4270 | } |
252b5132 | 4271 | break; |
53d25da6 AM |
4272 | case lang_insert_statement_enum: |
4273 | break; | |
43ae96e9 MM |
4274 | case lang_input_matcher_enum: |
4275 | FAIL (); | |
53d25da6 AM |
4276 | } |
4277 | } | |
4278 | } | |
4279 | ||
4280 | /* An insert statement snips out all the linker statements from the | |
4281 | start of the list and places them after the output section | |
4282 | statement specified by the insert. This operation is complicated | |
4283 | by the fact that we keep a doubly linked list of output section | |
776ab89f AM |
4284 | statements as well as the singly linked list of all statements. |
4285 | FIXME someday: Twiddling with the list not only moves statements | |
4286 | from the user's script but also input and group statements that are | |
4287 | built from command line object files and --start-group. We only | |
4288 | get away with this because the list pointers used by file_chain | |
4289 | and input_file_chain are not reordered, and processing via | |
4290 | statement_list after this point mostly ignores input statements. | |
4291 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4292 | can end up looking odd. */ | |
53d25da6 AM |
4293 | |
4294 | static void | |
776ab89f | 4295 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
4296 | { |
4297 | lang_statement_union_type **s; | |
4298 | lang_output_section_statement_type *first_os = NULL; | |
4299 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 4300 | lang_output_section_statement_type *os; |
53d25da6 | 4301 | |
776ab89f AM |
4302 | s = start; |
4303 | while (*s != NULL) | |
53d25da6 AM |
4304 | { |
4305 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4306 | { | |
4307 | /* Keep pointers to the first and last output section | |
4308 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
4309 | os = &(*s)->output_section_statement; |
4310 | ||
4311 | ASSERT (last_os == NULL || last_os->next == os); | |
4312 | last_os = os; | |
66c103b7 AM |
4313 | |
4314 | /* Set constraint negative so that lang_output_section_find | |
4315 | won't match this output section statement. At this | |
4316 | stage in linking constraint has values in the range | |
4317 | [-1, ONLY_IN_RW]. */ | |
4318 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
4319 | if (first_os == NULL) |
4320 | first_os = last_os; | |
4321 | } | |
776ab89f AM |
4322 | else if ((*s)->header.type == lang_group_statement_enum) |
4323 | { | |
4324 | /* A user might put -T between --start-group and | |
4325 | --end-group. One way this odd construct might arise is | |
4326 | from a wrapper around ld to change library search | |
4327 | behaviour. For example: | |
4328 | #! /bin/sh | |
4329 | exec real_ld --start-group "$@" --end-group | |
4330 | This isn't completely unreasonable so go looking inside a | |
4331 | group statement for insert statements. */ | |
4332 | process_insert_statements (&(*s)->group_statement.children.head); | |
4333 | } | |
53d25da6 AM |
4334 | else if ((*s)->header.type == lang_insert_statement_enum) |
4335 | { | |
4336 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4337 | lang_output_section_statement_type *where; | |
53d25da6 AM |
4338 | lang_statement_union_type **ptr; |
4339 | lang_statement_union_type *first; | |
4340 | ||
abf874aa CL |
4341 | if (link_info.non_contiguous_regions) |
4342 | { | |
4343 | einfo (_("warning: INSERT statement in linker script is " | |
4344 | "incompatible with --enable-non-contiguous-regions.\n")); | |
4345 | } | |
4346 | ||
53d25da6 AM |
4347 | where = lang_output_section_find (i->where); |
4348 | if (where != NULL && i->is_before) | |
4349 | { | |
967928e9 | 4350 | do |
53d25da6 | 4351 | where = where->prev; |
66c103b7 | 4352 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4353 | } |
4354 | if (where == NULL) | |
4355 | { | |
66c103b7 AM |
4356 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
4357 | return; | |
53d25da6 AM |
4358 | } |
4359 | ||
4360 | /* Deal with reordering the output section statement list. */ | |
4361 | if (last_os != NULL) | |
4362 | { | |
4363 | asection *first_sec, *last_sec; | |
29183214 | 4364 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4365 | |
4366 | /* Snip out the output sections we are moving. */ | |
4367 | first_os->prev->next = last_os->next; | |
4368 | if (last_os->next == NULL) | |
29183214 L |
4369 | { |
4370 | next = &first_os->prev->next; | |
5c1e6d53 | 4371 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4372 | } |
53d25da6 AM |
4373 | else |
4374 | last_os->next->prev = first_os->prev; | |
4375 | /* Add them in at the new position. */ | |
4376 | last_os->next = where->next; | |
4377 | if (where->next == NULL) | |
29183214 L |
4378 | { |
4379 | next = &last_os->next; | |
5c1e6d53 | 4380 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4381 | } |
53d25da6 AM |
4382 | else |
4383 | where->next->prev = last_os; | |
4384 | first_os->prev = where; | |
4385 | where->next = first_os; | |
4386 | ||
4387 | /* Move the bfd sections in the same way. */ | |
4388 | first_sec = NULL; | |
4389 | last_sec = NULL; | |
4390 | for (os = first_os; os != NULL; os = os->next) | |
4391 | { | |
66c103b7 | 4392 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
4393 | if (os->bfd_section != NULL |
4394 | && os->bfd_section->owner != NULL) | |
4395 | { | |
4396 | last_sec = os->bfd_section; | |
4397 | if (first_sec == NULL) | |
4398 | first_sec = last_sec; | |
4399 | } | |
4400 | if (os == last_os) | |
4401 | break; | |
4402 | } | |
4403 | if (last_sec != NULL) | |
4404 | { | |
4405 | asection *sec = where->bfd_section; | |
4406 | if (sec == NULL) | |
4407 | sec = output_prev_sec_find (where); | |
4408 | ||
4409 | /* The place we want to insert must come after the | |
4410 | sections we are moving. So if we find no | |
4411 | section or if the section is the same as our | |
4412 | last section, then no move is needed. */ | |
4413 | if (sec != NULL && sec != last_sec) | |
4414 | { | |
4415 | /* Trim them off. */ | |
4416 | if (first_sec->prev != NULL) | |
4417 | first_sec->prev->next = last_sec->next; | |
4418 | else | |
f13a99db | 4419 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4420 | if (last_sec->next != NULL) |
4421 | last_sec->next->prev = first_sec->prev; | |
4422 | else | |
f13a99db | 4423 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 | 4424 | /* Add back. */ |
18e7a658 AM |
4425 | if (sec->owner == NULL) |
4426 | /* SEC is the absolute section, from the | |
4427 | first dummy output section statement. Add | |
4428 | back the sections we trimmed off to the | |
4429 | start of the bfd sections. */ | |
4430 | sec = NULL; | |
4431 | if (sec != NULL) | |
4432 | last_sec->next = sec->next; | |
4433 | else | |
4434 | last_sec->next = link_info.output_bfd->sections; | |
4435 | if (last_sec->next != NULL) | |
4436 | last_sec->next->prev = last_sec; | |
53d25da6 | 4437 | else |
f13a99db | 4438 | link_info.output_bfd->section_last = last_sec; |
53d25da6 | 4439 | first_sec->prev = sec; |
18e7a658 AM |
4440 | if (first_sec->prev != NULL) |
4441 | first_sec->prev->next = first_sec; | |
4442 | else | |
4443 | link_info.output_bfd->sections = first_sec; | |
53d25da6 AM |
4444 | } |
4445 | } | |
53d25da6 AM |
4446 | } |
4447 | ||
18e7a658 AM |
4448 | lang_statement_union_type *after = (void *) where; |
4449 | if (where == &lang_os_list.head->output_section_statement | |
4450 | && where->next == first_os) | |
4451 | { | |
4452 | /* PR30155. Handle a corner case where the statement | |
4453 | list is something like the following: | |
4454 | . LOAD t.o | |
4455 | . .data 0x0000000000000000 0x0 | |
4456 | . [0x0000000000000000] b = . | |
4457 | . *(.data) | |
4458 | . .data 0x0000000000000000 0x0 t.o | |
4459 | . 0x0000000000000000 0x4 LONG 0x0 | |
4460 | . INSERT BEFORE .text.start | |
4461 | . [0x0000000000000004] a = . | |
4462 | . .text.start 0x0000000000000000 0x0 | |
4463 | . [0x0000000000000000] c = . | |
4464 | . OUTPUT(a.out elf64-x86-64) | |
4465 | Here we do not want to allow insert_os_after to | |
4466 | choose a point inside the list we are moving. | |
4467 | That would lose the list. Instead, let | |
4468 | insert_os_after work from the INSERT, which in this | |
4469 | particular example will result in inserting after | |
4470 | the assignment "a = .". */ | |
4471 | after = *s; | |
4472 | } | |
4473 | ptr = insert_os_after (after); | |
776ab89f AM |
4474 | /* Snip everything from the start of the list, up to and |
4475 | including the insert statement we are currently processing. */ | |
4476 | first = *start; | |
4477 | *start = (*s)->header.next; | |
4478 | /* Add them back where they belong, minus the insert. */ | |
53d25da6 AM |
4479 | *s = *ptr; |
4480 | if (*s == NULL) | |
4481 | statement_list.tail = s; | |
4482 | *ptr = first; | |
776ab89f | 4483 | s = start; |
18e7a658 AM |
4484 | first_os = NULL; |
4485 | last_os = NULL; | |
776ab89f | 4486 | continue; |
252b5132 | 4487 | } |
776ab89f | 4488 | s = &(*s)->header.next; |
252b5132 | 4489 | } |
66c103b7 AM |
4490 | |
4491 | /* Undo constraint twiddling. */ | |
4492 | for (os = first_os; os != NULL; os = os->next) | |
4493 | { | |
4494 | os->constraint = -2 - os->constraint; | |
4495 | if (os == last_os) | |
4496 | break; | |
4497 | } | |
252b5132 RH |
4498 | } |
4499 | ||
4bd5a393 AM |
4500 | /* An output section might have been removed after its statement was |
4501 | added. For example, ldemul_before_allocation can remove dynamic | |
4502 | sections if they turn out to be not needed. Clean them up here. */ | |
4503 | ||
8423293d | 4504 | void |
1579bae1 | 4505 | strip_excluded_output_sections (void) |
4bd5a393 | 4506 | { |
afd7a018 | 4507 | lang_output_section_statement_type *os; |
4bd5a393 | 4508 | |
046183de | 4509 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4510 | if (expld.phase != lang_mark_phase_enum) |
4511 | { | |
4512 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4513 | expld.dataseg.phase = exp_seg_none; |
f38a2680 | 4514 | one_lang_size_sections_pass (NULL, false); |
e9ee469a AM |
4515 | lang_reset_memory_regions (); |
4516 | } | |
4517 | ||
8ce18f9c | 4518 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
4519 | os != NULL; |
4520 | os = os->next) | |
4bd5a393 | 4521 | { |
75ff4589 | 4522 | asection *output_section; |
f38a2680 | 4523 | bool exclude; |
4bd5a393 | 4524 | |
66c103b7 | 4525 | if (os->constraint < 0) |
0841712e | 4526 | continue; |
8423293d | 4527 | |
75ff4589 L |
4528 | output_section = os->bfd_section; |
4529 | if (output_section == NULL) | |
8423293d AM |
4530 | continue; |
4531 | ||
32124d5b AM |
4532 | exclude = (output_section->rawsize == 0 |
4533 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4534 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
4535 | output_section)); |
4536 | ||
4537 | /* Some sections have not yet been sized, notably .gnu.version, | |
4538 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4539 | input sections, so don't drop output sections that have such | |
4540 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4541 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
4542 | { |
4543 | asection *s; | |
8423293d | 4544 | |
32124d5b | 4545 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4546 | if ((s->flags & SEC_EXCLUDE) == 0 |
4547 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4548 | || link_info.emitrelocations)) | |
e9ee469a | 4549 | { |
f38a2680 | 4550 | exclude = false; |
e9ee469a AM |
4551 | break; |
4552 | } | |
75ff4589 | 4553 | } |
8423293d | 4554 | |
32124d5b | 4555 | if (exclude) |
4bd5a393 | 4556 | { |
e9ee469a AM |
4557 | /* We don't set bfd_section to NULL since bfd_section of the |
4558 | removed output section statement may still be used. */ | |
6d8bf25d | 4559 | if (!os->update_dot) |
f38a2680 | 4560 | os->ignored = true; |
e9ee469a | 4561 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4562 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4563 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4564 | } |
4565 | } | |
18cd5bce AM |
4566 | } |
4567 | ||
4568 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4569 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
4570 | |
4571 | void | |
4572 | lang_clear_os_map (void) | |
4573 | { | |
4574 | lang_output_section_statement_type *os; | |
4575 | ||
4576 | if (map_head_is_link_order) | |
4577 | return; | |
4578 | ||
8ce18f9c | 4579 | for (os = (void *) lang_os_list.head; |
18cd5bce AM |
4580 | os != NULL; |
4581 | os = os->next) | |
4582 | { | |
4583 | asection *output_section; | |
4584 | ||
4585 | if (os->constraint < 0) | |
4586 | continue; | |
4587 | ||
4588 | output_section = os->bfd_section; | |
4589 | if (output_section == NULL) | |
4590 | continue; | |
4591 | ||
4592 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4593 | output_section->map_head.link_order = NULL; | |
4594 | output_section->map_tail.link_order = NULL; | |
4595 | } | |
8423293d AM |
4596 | |
4597 | /* Stop future calls to lang_add_section from messing with map_head | |
4598 | and map_tail link_order fields. */ | |
f38a2680 | 4599 | map_head_is_link_order = true; |
4bd5a393 AM |
4600 | } |
4601 | ||
252b5132 | 4602 | static void |
1579bae1 AM |
4603 | print_output_section_statement |
4604 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
4605 | { |
4606 | asection *section = output_section_statement->bfd_section; | |
4607 | int len; | |
4608 | ||
4609 | if (output_section_statement != abs_output_section) | |
4610 | { | |
4611 | minfo ("\n%s", output_section_statement->name); | |
4612 | ||
4613 | if (section != NULL) | |
4614 | { | |
4615 | print_dot = section->vma; | |
4616 | ||
4617 | len = strlen (output_section_statement->name); | |
4618 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4619 | { | |
4620 | print_nl (); | |
4621 | len = 0; | |
4622 | } | |
6b9bd54c | 4623 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4624 | |
953dd97e | 4625 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4626 | |
67f744f3 AM |
4627 | if (section->vma != section->lma) |
4628 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4629 | |
4630 | if (output_section_statement->update_dot_tree != NULL) | |
4631 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4632 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
4633 | } |
4634 | ||
4635 | print_nl (); | |
4636 | } | |
4637 | ||
4638 | print_statement_list (output_section_statement->children.head, | |
4639 | output_section_statement); | |
4640 | } | |
4641 | ||
4642 | static void | |
1579bae1 AM |
4643 | print_assignment (lang_assignment_statement_type *assignment, |
4644 | lang_output_section_statement_type *output_section) | |
252b5132 | 4645 | { |
f38a2680 | 4646 | bool is_dot; |
afd7a018 | 4647 | etree_type *tree; |
8658f989 | 4648 | asection *osec; |
252b5132 | 4649 | |
6b9bd54c | 4650 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4651 | |
afd7a018 AM |
4652 | if (assignment->exp->type.node_class == etree_assert) |
4653 | { | |
f38a2680 | 4654 | is_dot = false; |
afd7a018 AM |
4655 | tree = assignment->exp->assert_s.child; |
4656 | } | |
4657 | else | |
4658 | { | |
4659 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4660 | |
4661 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4662 | tree = assignment->exp; |
afd7a018 AM |
4663 | } |
4664 | ||
8658f989 AM |
4665 | osec = output_section->bfd_section; |
4666 | if (osec == NULL) | |
4667 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4668 | |
4669 | if (assignment->exp->type.node_class != etree_provide) | |
4670 | exp_fold_tree (tree, osec, &print_dot); | |
4671 | else | |
f38a2680 | 4672 | expld.result.valid_p = false; |
c05b575a | 4673 | |
6b9bd54c AM |
4674 | char buf[32]; |
4675 | const char *str = buf; | |
e9ee469a | 4676 | if (expld.result.valid_p) |
7b17f854 | 4677 | { |
7b17f854 RS |
4678 | bfd_vma value; |
4679 | ||
4194268f AM |
4680 | if (assignment->exp->type.node_class == etree_assert |
4681 | || is_dot | |
4682 | || expld.assign_name != NULL) | |
3b83e13a | 4683 | { |
e9ee469a | 4684 | value = expld.result.value; |
10dbd1f3 | 4685 | |
7542af2a | 4686 | if (expld.result.section != NULL) |
e9ee469a | 4687 | value += expld.result.section->vma; |
7b17f854 | 4688 | |
6b9bd54c AM |
4689 | buf[0] = '0'; |
4690 | buf[1] = 'x'; | |
4691 | bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); | |
3b83e13a NC |
4692 | if (is_dot) |
4693 | print_dot = value; | |
4694 | } | |
4695 | else | |
4696 | { | |
4697 | struct bfd_link_hash_entry *h; | |
4698 | ||
4699 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
f38a2680 | 4700 | false, false, true); |
6a846243 AM |
4701 | if (h != NULL |
4702 | && (h->type == bfd_link_hash_defined | |
4703 | || h->type == bfd_link_hash_defweak)) | |
3b83e13a | 4704 | { |
10dbd1f3 | 4705 | value = h->u.def.value; |
f37a7048 NS |
4706 | value += h->u.def.section->output_section->vma; |
4707 | value += h->u.def.section->output_offset; | |
3b83e13a | 4708 | |
6b9bd54c AM |
4709 | buf[0] = '['; |
4710 | buf[1] = '0'; | |
4711 | buf[2] = 'x'; | |
4712 | bfd_sprintf_vma (link_info.output_bfd, buf + 3, value); | |
4713 | strcat (buf, "]"); | |
3b83e13a NC |
4714 | } |
4715 | else | |
6b9bd54c | 4716 | str = "[unresolved]"; |
3b83e13a | 4717 | } |
7b17f854 | 4718 | } |
252b5132 RH |
4719 | else |
4720 | { | |
c05b575a | 4721 | if (assignment->exp->type.node_class == etree_provide) |
6b9bd54c | 4722 | str = "[!provide]"; |
c05b575a | 4723 | else |
6b9bd54c | 4724 | str = "*undef*"; |
252b5132 | 4725 | } |
4194268f | 4726 | expld.assign_name = NULL; |
252b5132 | 4727 | |
6b9bd54c | 4728 | fprintf (config.map_file, "%-34s", str); |
252b5132 | 4729 | exp_print_tree (assignment->exp); |
252b5132 RH |
4730 | print_nl (); |
4731 | } | |
4732 | ||
4733 | static void | |
1579bae1 | 4734 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4735 | { |
0d41d9a2 | 4736 | if (statm->filename != NULL) |
906e58ca | 4737 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4738 | } |
4739 | ||
4740 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4741 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4742 | |
f38a2680 | 4743 | bool |
1579bae1 | 4744 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4745 | { |
1e9cc1c2 | 4746 | asection *sec = (asection *) ptr; |
252b5132 RH |
4747 | |
4748 | if ((hash_entry->type == bfd_link_hash_defined | |
4749 | || hash_entry->type == bfd_link_hash_defweak) | |
4750 | && sec == hash_entry->u.def.section) | |
4751 | { | |
6b9bd54c | 4752 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 RH |
4753 | minfo ("0x%V ", |
4754 | (hash_entry->u.def.value | |
4755 | + hash_entry->u.def.section->output_offset | |
4756 | + hash_entry->u.def.section->output_section->vma)); | |
4757 | ||
c1c8c1ef | 4758 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4759 | } |
4760 | ||
f38a2680 | 4761 | return true; |
252b5132 RH |
4762 | } |
4763 | ||
02688209 TG |
4764 | static int |
4765 | hash_entry_addr_cmp (const void *a, const void *b) | |
4766 | { | |
4767 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4768 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4769 | ||
4770 | if (l->u.def.value < r->u.def.value) | |
4771 | return -1; | |
4772 | else if (l->u.def.value > r->u.def.value) | |
4773 | return 1; | |
4774 | else | |
4775 | return 0; | |
4776 | } | |
4777 | ||
35835446 | 4778 | static void |
e0f6802f | 4779 | print_all_symbols (asection *sec) |
35835446 | 4780 | { |
a48931cc | 4781 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4782 | struct map_symbol_def *def; |
02688209 TG |
4783 | struct bfd_link_hash_entry **entries; |
4784 | unsigned int i; | |
35835446 | 4785 | |
afd7a018 AM |
4786 | if (!ud) |
4787 | return; | |
4788 | ||
35835446 | 4789 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4790 | |
02688209 | 4791 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4792 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4793 | obstack_alloc (&map_obstack, |
4794 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
4795 | |
4796 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4797 | entries[i] = def->entry; | |
4798 | ||
4799 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4724d37e | 4800 | hash_entry_addr_cmp); |
02688209 TG |
4801 | |
4802 | /* Print the symbols. */ | |
4803 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
3edf7b9f | 4804 | ldemul_print_symbol (entries[i], sec); |
02688209 TG |
4805 | |
4806 | obstack_free (&map_obstack, entries); | |
35835446 JR |
4807 | } |
4808 | ||
496917ce NC |
4809 | /* Returns TRUE if SYM is a symbol suitable for printing |
4810 | in a linker map as a local symbol. */ | |
4811 | ||
4812 | static bool | |
4813 | ld_is_local_symbol (asymbol * sym) | |
4814 | { | |
4815 | const char * name = bfd_asymbol_name (sym); | |
4816 | ||
4817 | if (name == NULL || *name == 0) | |
4818 | return false; | |
4819 | ||
4820 | if (strcmp (name, "(null)") == 0) | |
4821 | return false; | |
4822 | ||
4823 | /* Skip .Lxxx and such like. */ | |
4824 | if (bfd_is_local_label (link_info.output_bfd, sym)) | |
4825 | return false; | |
4826 | ||
4827 | /* FIXME: This is intended to skip ARM mapping symbols, | |
4828 | which for some reason are not excluded by bfd_is_local_label, | |
4829 | but maybe it is wrong for other architectures. | |
4830 | It would be better to fix bfd_is_local_label. */ | |
4831 | if (*name == '$') | |
4832 | return false; | |
4833 | ||
4834 | /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present | |
4835 | in the hash table, so do not print duplicates here. */ | |
4836 | struct bfd_link_hash_entry * h; | |
4837 | h = bfd_link_hash_lookup (link_info.hash, name, false /* create */, | |
4838 | false /* copy */, true /* follow */); | |
4839 | if (h == NULL) | |
4840 | return true; | |
4841 | ||
4842 | /* Symbols from the plugin owned BFD will not get their own | |
4843 | iteration of this function, but can be on the link_info | |
4844 | list. So include them here. */ | |
4845 | if (h->u.def.section->owner != NULL | |
4846 | && ((bfd_get_file_flags (h->u.def.section->owner) & (BFD_LINKER_CREATED | BFD_PLUGIN)) | |
4847 | == (BFD_LINKER_CREATED | BFD_PLUGIN))) | |
4848 | return true; | |
4849 | ||
4850 | return false; | |
4851 | } | |
4852 | ||
252b5132 RH |
4853 | /* Print information about an input section to the map file. */ |
4854 | ||
4855 | static void | |
f38a2680 | 4856 | print_input_section (asection *i, bool is_discarded) |
252b5132 | 4857 | { |
eea6121a | 4858 | bfd_size_type size = i->size; |
abe6ac95 AM |
4859 | int len; |
4860 | bfd_vma addr; | |
e5caa5e0 | 4861 | |
61826503 | 4862 | init_opb (i); |
4d4920ec | 4863 | |
6b9bd54c | 4864 | minfo (" %s", i->name); |
252b5132 | 4865 | |
abe6ac95 AM |
4866 | len = 1 + strlen (i->name); |
4867 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4868 | { | |
4869 | print_nl (); | |
4870 | len = 0; | |
4871 | } | |
6b9bd54c | 4872 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4873 | |
f13a99db AM |
4874 | if (i->output_section != NULL |
4875 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
4876 | addr = i->output_section->vma + i->output_offset; |
4877 | else | |
4878 | { | |
4879 | addr = print_dot; | |
d64703c6 TG |
4880 | if (!is_discarded) |
4881 | size = 0; | |
abe6ac95 | 4882 | } |
252b5132 | 4883 | |
6b9bd54c AM |
4884 | char buf[32]; |
4885 | bfd_sprintf_vma (link_info.output_bfd, buf, addr); | |
4886 | minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner); | |
252b5132 | 4887 | |
abe6ac95 AM |
4888 | if (size != i->rawsize && i->rawsize != 0) |
4889 | { | |
6b9bd54c AM |
4890 | len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); |
4891 | print_spaces (len); | |
b0a7971a | 4892 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 4893 | } |
252b5132 | 4894 | |
f13a99db AM |
4895 | if (i->output_section != NULL |
4896 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 4897 | { |
c0f00686 | 4898 | if (link_info.reduce_memory_overheads) |
3edf7b9f | 4899 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); |
abe6ac95 AM |
4900 | else |
4901 | print_all_symbols (i); | |
4902 | ||
5590fba9 NC |
4903 | /* Update print_dot, but make sure that we do not move it |
4904 | backwards - this could happen if we have overlays and a | |
4905 | later overlay is shorter than an earier one. */ | |
4906 | if (addr + TO_ADDR (size) > print_dot) | |
4907 | print_dot = addr + TO_ADDR (size); | |
496917ce NC |
4908 | |
4909 | if (config.print_map_locals) | |
4910 | { | |
4911 | long storage_needed; | |
4912 | ||
4913 | /* FIXME: It would be better to cache this table, rather | |
4914 | than recreating it for each output section. */ | |
4915 | /* FIXME: This call is not working for non-ELF based targets. | |
4916 | Find out why. */ | |
4917 | storage_needed = bfd_get_symtab_upper_bound (link_info.output_bfd); | |
4918 | if (storage_needed > 0) | |
4919 | { | |
4920 | asymbol ** symbol_table; | |
4921 | long number_of_symbols; | |
4922 | long j; | |
4923 | ||
4924 | symbol_table = xmalloc (storage_needed); | |
4925 | number_of_symbols = bfd_canonicalize_symtab (link_info.output_bfd, symbol_table); | |
4926 | ||
4927 | for (j = 0; j < number_of_symbols; j++) | |
4928 | { | |
4929 | asymbol * sym = symbol_table[j]; | |
4930 | bfd_vma sym_addr = sym->value + i->output_section->vma; | |
4931 | ||
4932 | if (sym->section == i->output_section | |
4933 | && (sym->flags & BSF_LOCAL) != 0 | |
4934 | && sym_addr >= addr | |
4935 | && sym_addr < print_dot | |
4936 | && ld_is_local_symbol (sym)) | |
4937 | { | |
4938 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
4939 | minfo ("0x%V (local) %s\n", sym_addr, bfd_asymbol_name (sym)); | |
4940 | } | |
4941 | } | |
4942 | ||
4943 | free (symbol_table); | |
4944 | } | |
4945 | } | |
252b5132 RH |
4946 | } |
4947 | } | |
4948 | ||
4949 | static void | |
1579bae1 | 4950 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 4951 | { |
2c382fb6 AM |
4952 | size_t size; |
4953 | unsigned char *p; | |
4954 | fputs (" FILL mask 0x", config.map_file); | |
4955 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4956 | fprintf (config.map_file, "%02x", *p); | |
4957 | fputs ("\n", config.map_file); | |
252b5132 RH |
4958 | } |
4959 | ||
4960 | static void | |
1579bae1 | 4961 | print_data_statement (lang_data_statement_type *data) |
252b5132 | 4962 | { |
252b5132 RH |
4963 | bfd_vma addr; |
4964 | bfd_size_type size; | |
4965 | const char *name; | |
4966 | ||
61826503 | 4967 | init_opb (data->output_section); |
6b9bd54c | 4968 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4969 | |
7fabd029 | 4970 | addr = data->output_offset; |
252b5132 RH |
4971 | if (data->output_section != NULL) |
4972 | addr += data->output_section->vma; | |
4973 | ||
4974 | switch (data->type) | |
4975 | { | |
4976 | default: | |
4977 | abort (); | |
4978 | case BYTE: | |
4979 | size = BYTE_SIZE; | |
4980 | name = "BYTE"; | |
4981 | break; | |
4982 | case SHORT: | |
4983 | size = SHORT_SIZE; | |
4984 | name = "SHORT"; | |
4985 | break; | |
4986 | case LONG: | |
4987 | size = LONG_SIZE; | |
4988 | name = "LONG"; | |
4989 | break; | |
4990 | case QUAD: | |
4991 | size = QUAD_SIZE; | |
4992 | name = "QUAD"; | |
4993 | break; | |
4994 | case SQUAD: | |
4995 | size = QUAD_SIZE; | |
4996 | name = "SQUAD"; | |
4997 | break; | |
4998 | } | |
4999 | ||
953dd97e DG |
5000 | if (size < TO_SIZE ((unsigned) 1)) |
5001 | size = TO_SIZE ((unsigned) 1); | |
5002 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
252b5132 RH |
5003 | |
5004 | if (data->exp->type.node_class != etree_value) | |
5005 | { | |
5006 | print_space (); | |
5007 | exp_print_tree (data->exp); | |
5008 | } | |
5009 | ||
5010 | print_nl (); | |
5011 | ||
e5caa5e0 | 5012 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
5013 | } |
5014 | ||
5015 | /* Print an address statement. These are generated by options like | |
5016 | -Ttext. */ | |
5017 | ||
5018 | static void | |
1579bae1 | 5019 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
5020 | { |
5021 | minfo (_("Address of section %s set to "), address->section_name); | |
5022 | exp_print_tree (address->address); | |
5023 | print_nl (); | |
5024 | } | |
5025 | ||
5026 | /* Print a reloc statement. */ | |
5027 | ||
5028 | static void | |
1579bae1 | 5029 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 | 5030 | { |
252b5132 RH |
5031 | bfd_vma addr; |
5032 | bfd_size_type size; | |
5033 | ||
61826503 | 5034 | init_opb (reloc->output_section); |
6b9bd54c | 5035 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 5036 | |
7fabd029 | 5037 | addr = reloc->output_offset; |
252b5132 RH |
5038 | if (reloc->output_section != NULL) |
5039 | addr += reloc->output_section->vma; | |
5040 | ||
5041 | size = bfd_get_reloc_size (reloc->howto); | |
5042 | ||
953dd97e | 5043 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
5044 | |
5045 | if (reloc->name != NULL) | |
5046 | minfo ("%s+", reloc->name); | |
5047 | else | |
5048 | minfo ("%s+", reloc->section->name); | |
5049 | ||
5050 | exp_print_tree (reloc->addend_exp); | |
5051 | ||
5052 | print_nl (); | |
5053 | ||
e5caa5e0 | 5054 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 5055 | } |
252b5132 RH |
5056 | |
5057 | static void | |
1579bae1 | 5058 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
5059 | { |
5060 | int len; | |
5061 | bfd_vma addr; | |
5062 | ||
61826503 | 5063 | init_opb (s->output_section); |
252b5132 RH |
5064 | minfo (" *fill*"); |
5065 | ||
5066 | len = sizeof " *fill*" - 1; | |
6b9bd54c | 5067 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 RH |
5068 | |
5069 | addr = s->output_offset; | |
5070 | if (s->output_section != NULL) | |
5071 | addr += s->output_section->vma; | |
953dd97e | 5072 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 5073 | |
2c382fb6 AM |
5074 | if (s->fill->size != 0) |
5075 | { | |
5076 | size_t size; | |
5077 | unsigned char *p; | |
5078 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
5079 | fprintf (config.map_file, "%02x", *p); | |
5080 | } | |
252b5132 RH |
5081 | |
5082 | print_nl (); | |
5083 | ||
e5caa5e0 | 5084 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
5085 | } |
5086 | ||
5087 | static void | |
1579bae1 AM |
5088 | print_wild_statement (lang_wild_statement_type *w, |
5089 | lang_output_section_statement_type *os) | |
252b5132 | 5090 | { |
b6bf44ba AM |
5091 | struct wildcard_list *sec; |
5092 | ||
252b5132 RH |
5093 | print_space (); |
5094 | ||
8f1732fc AB |
5095 | if (w->exclude_name_list) |
5096 | { | |
5097 | name_list *tmp; | |
5098 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
5099 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
6c19b93b | 5100 | minfo (" %s", tmp->name); |
8f1732fc AB |
5101 | minfo (") "); |
5102 | } | |
5103 | ||
252b5132 | 5104 | if (w->filenames_sorted) |
2b94abd4 | 5105 | minfo ("SORT_BY_NAME("); |
08da4cac | 5106 | if (w->filename != NULL) |
252b5132 RH |
5107 | minfo ("%s", w->filename); |
5108 | else | |
5109 | minfo ("*"); | |
5110 | if (w->filenames_sorted) | |
5111 | minfo (")"); | |
5112 | ||
5113 | minfo ("("); | |
b6bf44ba AM |
5114 | for (sec = w->section_list; sec; sec = sec->next) |
5115 | { | |
2b94abd4 AB |
5116 | int closing_paren = 0; |
5117 | ||
5118 | switch (sec->spec.sorted) | |
6c19b93b AM |
5119 | { |
5120 | case none: | |
5121 | break; | |
5122 | ||
5123 | case by_name: | |
5124 | minfo ("SORT_BY_NAME("); | |
5125 | closing_paren = 1; | |
5126 | break; | |
5127 | ||
5128 | case by_alignment: | |
5129 | minfo ("SORT_BY_ALIGNMENT("); | |
5130 | closing_paren = 1; | |
5131 | break; | |
5132 | ||
5133 | case by_name_alignment: | |
5134 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
5135 | closing_paren = 2; | |
5136 | break; | |
5137 | ||
5138 | case by_alignment_name: | |
5139 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
5140 | closing_paren = 2; | |
5141 | break; | |
5142 | ||
5143 | case by_none: | |
5144 | minfo ("SORT_NONE("); | |
5145 | closing_paren = 1; | |
5146 | break; | |
5147 | ||
5148 | case by_init_priority: | |
5149 | minfo ("SORT_BY_INIT_PRIORITY("); | |
5150 | closing_paren = 1; | |
5151 | break; | |
5152 | } | |
2b94abd4 | 5153 | |
b6bf44ba AM |
5154 | if (sec->spec.exclude_name_list != NULL) |
5155 | { | |
5156 | name_list *tmp; | |
34786259 | 5157 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 5158 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
5159 | minfo (" %s", tmp->name); |
5160 | minfo (") "); | |
b6bf44ba AM |
5161 | } |
5162 | if (sec->spec.name != NULL) | |
5163 | minfo ("%s", sec->spec.name); | |
5164 | else | |
5165 | minfo ("*"); | |
2b94abd4 | 5166 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 5167 | minfo (")"); |
34786259 AM |
5168 | if (sec->next) |
5169 | minfo (" "); | |
b6bf44ba | 5170 | } |
252b5132 RH |
5171 | minfo (")"); |
5172 | ||
5173 | print_nl (); | |
5174 | ||
5175 | print_statement_list (w->children.head, os); | |
5176 | } | |
5177 | ||
5178 | /* Print a group statement. */ | |
5179 | ||
5180 | static void | |
1579bae1 AM |
5181 | print_group (lang_group_statement_type *s, |
5182 | lang_output_section_statement_type *os) | |
252b5132 RH |
5183 | { |
5184 | fprintf (config.map_file, "START GROUP\n"); | |
5185 | print_statement_list (s->children.head, os); | |
5186 | fprintf (config.map_file, "END GROUP\n"); | |
5187 | } | |
5188 | ||
5189 | /* Print the list of statements in S. | |
5190 | This can be called for any statement type. */ | |
5191 | ||
5192 | static void | |
1579bae1 AM |
5193 | print_statement_list (lang_statement_union_type *s, |
5194 | lang_output_section_statement_type *os) | |
252b5132 RH |
5195 | { |
5196 | while (s != NULL) | |
5197 | { | |
5198 | print_statement (s, os); | |
bba1a0c0 | 5199 | s = s->header.next; |
252b5132 RH |
5200 | } |
5201 | } | |
5202 | ||
5203 | /* Print the first statement in statement list S. | |
5204 | This can be called for any statement type. */ | |
5205 | ||
5206 | static void | |
1579bae1 AM |
5207 | print_statement (lang_statement_union_type *s, |
5208 | lang_output_section_statement_type *os) | |
252b5132 RH |
5209 | { |
5210 | switch (s->header.type) | |
5211 | { | |
5212 | default: | |
5213 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
5214 | FAIL (); | |
5215 | break; | |
5216 | case lang_constructors_statement_enum: | |
5217 | if (constructor_list.head != NULL) | |
5218 | { | |
5219 | if (constructors_sorted) | |
5220 | minfo (" SORT (CONSTRUCTORS)\n"); | |
5221 | else | |
5222 | minfo (" CONSTRUCTORS\n"); | |
5223 | print_statement_list (constructor_list.head, os); | |
5224 | } | |
5225 | break; | |
5226 | case lang_wild_statement_enum: | |
5227 | print_wild_statement (&s->wild_statement, os); | |
5228 | break; | |
5229 | case lang_address_statement_enum: | |
5230 | print_address_statement (&s->address_statement); | |
5231 | break; | |
5232 | case lang_object_symbols_statement_enum: | |
5233 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
5234 | break; | |
5235 | case lang_fill_statement_enum: | |
5236 | print_fill_statement (&s->fill_statement); | |
5237 | break; | |
5238 | case lang_data_statement_enum: | |
5239 | print_data_statement (&s->data_statement); | |
5240 | break; | |
5241 | case lang_reloc_statement_enum: | |
5242 | print_reloc_statement (&s->reloc_statement); | |
5243 | break; | |
5244 | case lang_input_section_enum: | |
f38a2680 | 5245 | print_input_section (s->input_section.section, false); |
252b5132 RH |
5246 | break; |
5247 | case lang_padding_statement_enum: | |
5248 | print_padding_statement (&s->padding_statement); | |
5249 | break; | |
5250 | case lang_output_section_statement_enum: | |
5251 | print_output_section_statement (&s->output_section_statement); | |
5252 | break; | |
5253 | case lang_assignment_statement_enum: | |
5254 | print_assignment (&s->assignment_statement, os); | |
5255 | break; | |
5256 | case lang_target_statement_enum: | |
5257 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5258 | break; | |
5259 | case lang_output_statement_enum: | |
5260 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5261 | if (output_target != NULL) | |
5262 | minfo (" %s", output_target); | |
5263 | minfo (")\n"); | |
5264 | break; | |
5265 | case lang_input_statement_enum: | |
5266 | print_input_statement (&s->input_statement); | |
5267 | break; | |
5268 | case lang_group_statement_enum: | |
5269 | print_group (&s->group_statement, os); | |
5270 | break; | |
53d25da6 AM |
5271 | case lang_insert_statement_enum: |
5272 | minfo ("INSERT %s %s\n", | |
5273 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5274 | s->insert_statement.where); | |
5275 | break; | |
252b5132 RH |
5276 | } |
5277 | } | |
5278 | ||
5279 | static void | |
1579bae1 | 5280 | print_statements (void) |
252b5132 RH |
5281 | { |
5282 | print_statement_list (statement_list.head, abs_output_section); | |
5283 | } | |
5284 | ||
5285 | /* Print the first N statements in statement list S to STDERR. | |
5286 | If N == 0, nothing is printed. | |
5287 | If N < 0, the entire list is printed. | |
5288 | Intended to be called from GDB. */ | |
5289 | ||
5290 | void | |
1579bae1 | 5291 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
5292 | { |
5293 | FILE *map_save = config.map_file; | |
5294 | ||
5295 | config.map_file = stderr; | |
5296 | ||
5297 | if (n < 0) | |
5298 | print_statement_list (s, abs_output_section); | |
5299 | else | |
5300 | { | |
5301 | while (s && --n >= 0) | |
5302 | { | |
5303 | print_statement (s, abs_output_section); | |
bba1a0c0 | 5304 | s = s->header.next; |
252b5132 RH |
5305 | } |
5306 | } | |
5307 | ||
5308 | config.map_file = map_save; | |
5309 | } | |
5310 | ||
b3327aad | 5311 | static void |
1579bae1 AM |
5312 | insert_pad (lang_statement_union_type **ptr, |
5313 | fill_type *fill, | |
1a69ff95 | 5314 | bfd_size_type alignment_needed, |
1579bae1 AM |
5315 | asection *output_section, |
5316 | bfd_vma dot) | |
252b5132 | 5317 | { |
b7761f11 | 5318 | static fill_type zero_fill; |
e9ee469a | 5319 | lang_statement_union_type *pad = NULL; |
b3327aad | 5320 | |
e9ee469a AM |
5321 | if (ptr != &statement_list.head) |
5322 | pad = ((lang_statement_union_type *) | |
5323 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5324 | if (pad != NULL | |
5325 | && pad->header.type == lang_padding_statement_enum | |
5326 | && pad->padding_statement.output_section == output_section) | |
5327 | { | |
5328 | /* Use the existing pad statement. */ | |
5329 | } | |
5330 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
5331 | && pad->header.type == lang_padding_statement_enum |
5332 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5333 | { |
e9ee469a | 5334 | /* Use the existing pad statement. */ |
252b5132 | 5335 | } |
b3327aad | 5336 | else |
252b5132 | 5337 | { |
b3327aad | 5338 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5339 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5340 | pad->header.next = *ptr; |
5341 | *ptr = pad; | |
5342 | pad->header.type = lang_padding_statement_enum; | |
5343 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5344 | if (fill == NULL) |
2c382fb6 | 5345 | fill = &zero_fill; |
b3327aad | 5346 | pad->padding_statement.fill = fill; |
252b5132 | 5347 | } |
b3327aad AM |
5348 | pad->padding_statement.output_offset = dot - output_section->vma; |
5349 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5350 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5351 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5352 | - output_section->vma); | |
252b5132 RH |
5353 | } |
5354 | ||
08da4cac KH |
5355 | /* Work out how much this section will move the dot point. */ |
5356 | ||
252b5132 | 5357 | static bfd_vma |
6feb9908 AM |
5358 | size_input_section |
5359 | (lang_statement_union_type **this_ptr, | |
5360 | lang_output_section_statement_type *output_section_statement, | |
5361 | fill_type *fill, | |
f38a2680 | 5362 | bool *removed, |
6feb9908 | 5363 | bfd_vma dot) |
252b5132 RH |
5364 | { |
5365 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5366 | asection *i = is->section; | |
93d1b056 | 5367 | asection *o = output_section_statement->bfd_section; |
abf874aa CL |
5368 | *removed = 0; |
5369 | ||
5370 | if (link_info.non_contiguous_regions) | |
5371 | { | |
5372 | /* If the input section I has already been successfully assigned | |
5373 | to an output section other than O, don't bother with it and | |
5374 | let the caller remove it from the list. Keep processing in | |
5375 | case we have already handled O, because the repeated passes | |
5376 | have reinitialized its size. */ | |
5377 | if (i->already_assigned && i->already_assigned != o) | |
5378 | { | |
5379 | *removed = 1; | |
5380 | return dot; | |
5381 | } | |
5382 | } | |
252b5132 | 5383 | |
93d1b056 AM |
5384 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5385 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5386 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5387 | || output_section_statement->ignored) | |
93d1b056 AM |
5388 | i->output_offset = dot - o->vma; |
5389 | else | |
252b5132 | 5390 | { |
1a69ff95 | 5391 | bfd_size_type alignment_needed; |
b3327aad AM |
5392 | |
5393 | /* Align this section first to the input sections requirement, | |
5394 | then to the output section's requirement. If this alignment | |
5395 | is greater than any seen before, then record it too. Perform | |
5396 | the alignment by inserting a magic 'padding' statement. */ | |
5397 | ||
3d9c8f6b AM |
5398 | if (output_section_statement->subsection_alignment != NULL) |
5399 | i->alignment_power | |
5400 | = exp_get_power (output_section_statement->subsection_alignment, | |
5401 | "subsection alignment"); | |
b3327aad | 5402 | |
b3327aad AM |
5403 | if (o->alignment_power < i->alignment_power) |
5404 | o->alignment_power = i->alignment_power; | |
252b5132 | 5405 | |
b3327aad AM |
5406 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5407 | ||
5408 | if (alignment_needed != 0) | |
5409 | { | |
e5caa5e0 | 5410 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5411 | dot += alignment_needed; |
5412 | } | |
252b5132 | 5413 | |
abf874aa CL |
5414 | if (link_info.non_contiguous_regions) |
5415 | { | |
5416 | /* If I would overflow O, let the caller remove I from the | |
5417 | list. */ | |
5418 | if (output_section_statement->region) | |
5419 | { | |
5420 | bfd_vma end = output_section_statement->region->origin | |
5421 | + output_section_statement->region->length; | |
5422 | ||
5423 | if (dot + TO_ADDR (i->size) > end) | |
5424 | { | |
5425 | if (i->flags & SEC_LINKER_CREATED) | |
c63d4862 AM |
5426 | einfo (_("%F%P: Output section `%pA' not large enough for " |
5427 | "the linker-created stubs section `%pA'.\n"), | |
5428 | i->output_section, i); | |
abf874aa CL |
5429 | |
5430 | if (i->rawsize && i->rawsize != i->size) | |
53215f21 | 5431 | einfo (_("%F%P: Relaxation not supported with " |
c63d4862 AM |
5432 | "--enable-non-contiguous-regions (section `%pA' " |
5433 | "would overflow `%pA' after it changed size).\n"), | |
5434 | i, i->output_section); | |
abf874aa CL |
5435 | |
5436 | *removed = 1; | |
5437 | dot = end; | |
5438 | i->output_section = NULL; | |
5439 | return dot; | |
5440 | } | |
5441 | } | |
5442 | } | |
5443 | ||
08da4cac | 5444 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5445 | i->output_offset = dot - o->vma; |
252b5132 | 5446 | |
08da4cac | 5447 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5448 | dot += TO_ADDR (i->size); |
8772de11 MR |
5449 | if (!(o->flags & SEC_FIXED_SIZE)) |
5450 | o->size = TO_SIZE (dot - o->vma); | |
abf874aa CL |
5451 | |
5452 | if (link_info.non_contiguous_regions) | |
5453 | { | |
5454 | /* Record that I was successfully assigned to O, and update | |
5455 | its actual output section too. */ | |
5456 | i->already_assigned = o; | |
5457 | i->output_section = o; | |
5458 | } | |
252b5132 | 5459 | } |
252b5132 RH |
5460 | |
5461 | return dot; | |
5462 | } | |
5463 | ||
a87dd97a AM |
5464 | struct check_sec |
5465 | { | |
5466 | asection *sec; | |
f38a2680 | 5467 | bool warned; |
a87dd97a AM |
5468 | }; |
5469 | ||
5daa8fe7 L |
5470 | static int |
5471 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5472 | { | |
a87dd97a AM |
5473 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; |
5474 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5475 | ||
5476 | if (sec1->lma < sec2->lma) | |
5477 | return -1; | |
5478 | else if (sec1->lma > sec2->lma) | |
5479 | return 1; | |
5480 | else if (sec1->id < sec2->id) | |
5481 | return -1; | |
5482 | else if (sec1->id > sec2->id) | |
5483 | return 1; | |
5484 | ||
5485 | return 0; | |
5486 | } | |
5487 | ||
5488 | static int | |
5489 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5490 | { | |
5491 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5492 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5daa8fe7 | 5493 | |
a87dd97a | 5494 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5495 | return -1; |
a87dd97a | 5496 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5497 | return 1; |
7f6a71ff JM |
5498 | else if (sec1->id < sec2->id) |
5499 | return -1; | |
5500 | else if (sec1->id > sec2->id) | |
5501 | return 1; | |
5daa8fe7 L |
5502 | |
5503 | return 0; | |
5504 | } | |
5505 | ||
2e4a7aea AM |
5506 | #define IS_TBSS(s) \ |
5507 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5508 | ||
eea6121a | 5509 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5510 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5511 | |
252b5132 | 5512 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5513 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5514 | section addresses of the two sections. Also check whether any memory |
5515 | region has overflowed. */ | |
08da4cac | 5516 | |
252b5132 | 5517 | static void |
1579bae1 | 5518 | lang_check_section_addresses (void) |
252b5132 | 5519 | { |
f4427a75 | 5520 | asection *s, *p; |
a87dd97a AM |
5521 | struct check_sec *sections; |
5522 | size_t i, count; | |
d40e34db | 5523 | bfd_vma addr_mask; |
5daa8fe7 L |
5524 | bfd_vma s_start; |
5525 | bfd_vma s_end; | |
a87dd97a AM |
5526 | bfd_vma p_start = 0; |
5527 | bfd_vma p_end = 0; | |
20e56351 | 5528 | lang_memory_region_type *m; |
f38a2680 | 5529 | bool overlays; |
5daa8fe7 | 5530 | |
21701718 | 5531 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
5532 | addr_mask = ((bfd_vma) 1 << |
5533 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5534 | addr_mask = (addr_mask << 1) + 1; | |
5535 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
21701718 TG |
5536 | if ((s->flags & SEC_ALLOC) != 0) |
5537 | { | |
5538 | s_end = (s->vma + s->size) & addr_mask; | |
5539 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5540 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5541 | s->name); | |
5542 | else | |
5543 | { | |
5544 | s_end = (s->lma + s->size) & addr_mask; | |
5545 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5546 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5547 | s->name); | |
5548 | } | |
5549 | } | |
d40e34db | 5550 | |
f13a99db | 5551 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5552 | return; |
5553 | ||
a87dd97a AM |
5554 | count = bfd_count_sections (link_info.output_bfd); |
5555 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5556 | |
5557 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5558 | count = 0; |
f13a99db | 5559 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5560 | { |
a87dd97a | 5561 | if (IGNORE_SECTION (s) |
ec6d26be | 5562 | || s->size == 0) |
33275c22 | 5563 | continue; |
5f992e62 | 5564 | |
a87dd97a | 5565 | sections[count].sec = s; |
f38a2680 | 5566 | sections[count].warned = false; |
5daa8fe7 L |
5567 | count++; |
5568 | } | |
329c1c86 | 5569 | |
5daa8fe7 | 5570 | if (count <= 1) |
136a43b7 AM |
5571 | { |
5572 | free (sections); | |
5573 | return; | |
5574 | } | |
5f992e62 | 5575 | |
a87dd97a | 5576 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5577 | |
136a43b7 | 5578 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5579 | loadable sections, even with overlays. */ |
5580 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5581 | { |
a87dd97a | 5582 | s = sections[i].sec; |
61826503 | 5583 | init_opb (s); |
a87dd97a AM |
5584 | if ((s->flags & SEC_LOAD) != 0) |
5585 | { | |
5586 | s_start = s->lma; | |
5587 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5588 | ||
5589 | /* Look for an overlap. We have sorted sections by lma, so | |
5590 | we know that s_start >= p_start. Besides the obvious | |
5591 | case of overlap when the current section starts before | |
5592 | the previous one ends, we also must have overlap if the | |
5593 | previous section wraps around the address space. */ | |
5594 | if (p != NULL | |
5595 | && (s_start <= p_end | |
5596 | || p_end < p_start)) | |
5597 | { | |
5598 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5599 | " overlaps section %s LMA [%V,%V]\n"), | |
5600 | s->name, s_start, s_end, p->name, p_start, p_end); | |
f38a2680 | 5601 | sections[i].warned = true; |
a87dd97a AM |
5602 | } |
5603 | p = s; | |
5604 | p_start = s_start; | |
5605 | p_end = s_end; | |
5606 | } | |
5607 | } | |
5608 | ||
136a43b7 AM |
5609 | /* If any non-zero size allocated section (excluding tbss) starts at |
5610 | exactly the same VMA as another such section, then we have | |
5611 | overlays. Overlays generated by the OVERLAY keyword will have | |
5612 | this property. It is possible to intentionally generate overlays | |
5613 | that fail this test, but it would be unusual. */ | |
5614 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
f38a2680 | 5615 | overlays = false; |
136a43b7 AM |
5616 | p_start = sections[0].sec->vma; |
5617 | for (i = 1; i < count; i++) | |
a87dd97a | 5618 | { |
136a43b7 AM |
5619 | s_start = sections[i].sec->vma; |
5620 | if (p_start == s_start) | |
5621 | { | |
f38a2680 | 5622 | overlays = true; |
136a43b7 AM |
5623 | break; |
5624 | } | |
5625 | p_start = s_start; | |
5626 | } | |
a87dd97a | 5627 | |
136a43b7 AM |
5628 | /* Now check section VMAs if no overlays were detected. */ |
5629 | if (!overlays) | |
5630 | { | |
a87dd97a AM |
5631 | for (p = NULL, i = 0; i < count; i++) |
5632 | { | |
5633 | s = sections[i].sec; | |
61826503 | 5634 | init_opb (s); |
a87dd97a AM |
5635 | s_start = s->vma; |
5636 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5637 | ||
5638 | if (p != NULL | |
5639 | && !sections[i].warned | |
5640 | && (s_start <= p_end | |
5641 | || p_end < p_start)) | |
5642 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5643 | " overlaps section %s VMA [%V,%V]\n"), | |
5644 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5645 | p = s; | |
5646 | p_start = s_start; | |
5647 | p_end = s_end; | |
5648 | } | |
33275c22 | 5649 | } |
5daa8fe7 L |
5650 | |
5651 | free (sections); | |
20e56351 DJ |
5652 | |
5653 | /* If any memory region has overflowed, report by how much. | |
5654 | We do not issue this diagnostic for regions that had sections | |
5655 | explicitly placed outside their bounds; os_region_check's | |
5656 | diagnostics are adequate for that case. | |
5657 | ||
5658 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5659 | might overflow a 32-bit integer. There is, alas, no way to print | |
5660 | a bfd_vma quantity in decimal. */ | |
5661 | for (m = lang_memory_region_list; m; m = m->next) | |
5662 | if (m->had_full_message) | |
992a06ee AM |
5663 | { |
5664 | unsigned long over = m->current - (m->origin + m->length); | |
5665 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5666 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5667 | over), | |
5668 | m->name_list.name, over); | |
5669 | } | |
252b5132 RH |
5670 | } |
5671 | ||
562d3460 TW |
5672 | /* Make sure the new address is within the region. We explicitly permit the |
5673 | current address to be at the exact end of the region when the address is | |
5674 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 5675 | calculation wraps around. */ |
562d3460 TW |
5676 | |
5677 | static void | |
1579bae1 | 5678 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5679 | lang_memory_region_type *region, |
1579bae1 | 5680 | etree_type *tree, |
91d6fa6a | 5681 | bfd_vma rbase) |
562d3460 TW |
5682 | { |
5683 | if ((region->current < region->origin | |
5684 | || (region->current - region->origin > region->length)) | |
5685 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5686 | || rbase == 0)) |
562d3460 | 5687 | { |
1579bae1 | 5688 | if (tree != NULL) |
b7a26f91 | 5689 | { |
871b3ab2 | 5690 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5691 | " is not within region `%s'\n"), |
b7a26f91 KH |
5692 | region->current, |
5693 | os->bfd_section->owner, | |
5694 | os->bfd_section->name, | |
4a93e180 | 5695 | region->name_list.name); |
b7a26f91 | 5696 | } |
20e56351 | 5697 | else if (!region->had_full_message) |
b7a26f91 | 5698 | { |
f38a2680 | 5699 | region->had_full_message = true; |
20e56351 | 5700 | |
871b3ab2 | 5701 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5702 | os->bfd_section->owner, |
20e56351 | 5703 | os->bfd_section->name, |
4a93e180 | 5704 | region->name_list.name); |
b7a26f91 | 5705 | } |
562d3460 TW |
5706 | } |
5707 | } | |
5708 | ||
ed1794ee | 5709 | static void |
2085ee26 | 5710 | ldlang_check_relro_region (lang_statement_union_type *s) |
ed1794ee | 5711 | { |
2085ee26 AM |
5712 | seg_align_type *seg = &expld.dataseg; |
5713 | ||
ed1794ee L |
5714 | if (seg->relro == exp_seg_relro_start) |
5715 | { | |
5716 | if (!seg->relro_start_stat) | |
5717 | seg->relro_start_stat = s; | |
5718 | else | |
5719 | { | |
5720 | ASSERT (seg->relro_start_stat == s); | |
5721 | } | |
5722 | } | |
5723 | else if (seg->relro == exp_seg_relro_end) | |
5724 | { | |
5725 | if (!seg->relro_end_stat) | |
5726 | seg->relro_end_stat = s; | |
5727 | else | |
5728 | { | |
5729 | ASSERT (seg->relro_end_stat == s); | |
5730 | } | |
5731 | } | |
5732 | } | |
5733 | ||
252b5132 RH |
5734 | /* Set the sizes for all the output sections. */ |
5735 | ||
2d20f7bf | 5736 | static bfd_vma |
1579bae1 | 5737 | lang_size_sections_1 |
e535e147 | 5738 | (lang_statement_union_type **prev, |
1579bae1 | 5739 | lang_output_section_statement_type *output_section_statement, |
1579bae1 AM |
5740 | fill_type *fill, |
5741 | bfd_vma dot, | |
f38a2680 AM |
5742 | bool *relax, |
5743 | bool check_regions) | |
252b5132 | 5744 | { |
e535e147 | 5745 | lang_statement_union_type *s; |
abf874aa | 5746 | lang_statement_union_type *prev_s = NULL; |
f38a2680 | 5747 | bool removed_prev_s = false; |
e535e147 | 5748 | |
252b5132 | 5749 | /* Size up the sections from their constituent parts. */ |
abf874aa | 5750 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) |
252b5132 | 5751 | { |
f38a2680 | 5752 | bool removed = false; |
abf874aa | 5753 | |
252b5132 RH |
5754 | switch (s->header.type) |
5755 | { | |
5756 | case lang_output_section_statement_enum: | |
5757 | { | |
13075d04 | 5758 | bfd_vma newdot, after, dotdelta; |
d1778b88 | 5759 | lang_output_section_statement_type *os; |
cde9e0be | 5760 | lang_memory_region_type *r; |
c5b0a9ef | 5761 | int section_alignment = 0; |
252b5132 | 5762 | |
d1778b88 | 5763 | os = &s->output_section_statement; |
61826503 | 5764 | init_opb (os->bfd_section); |
8658f989 AM |
5765 | if (os->constraint == -1) |
5766 | break; | |
5767 | ||
fd68d03d AM |
5768 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
5769 | here, in lang_insert_orphan, or in the default linker scripts. | |
5770 | This is covering for coff backend linker bugs. See PR6945. */ | |
5771 | if (os->addr_tree == NULL | |
0e1862bb | 5772 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5773 | && (bfd_get_flavour (link_info.output_bfd) |
5774 | == bfd_target_coff_flavour)) | |
eceda120 | 5775 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5776 | if (os->addr_tree != NULL) |
5777 | { | |
a5df8c84 | 5778 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5779 | |
3bf9618b | 5780 | if (expld.result.valid_p) |
7542af2a AM |
5781 | { |
5782 | dot = expld.result.value; | |
5783 | if (expld.result.section != NULL) | |
5784 | dot += expld.result.section->vma; | |
5785 | } | |
3bf9618b | 5786 | else if (expld.phase != lang_mark_phase_enum) |
df5f2391 | 5787 | einfo (_("%F%P:%pS: non constant or forward reference" |
a5df8c84 | 5788 | " address expression for section %s\n"), |
dab69f68 | 5789 | os->addr_tree, os->name); |
a5df8c84 AM |
5790 | } |
5791 | ||
e9ee469a | 5792 | if (os->bfd_section == NULL) |
75ff4589 | 5793 | /* This section was removed or never actually created. */ |
252b5132 RH |
5794 | break; |
5795 | ||
5796 | /* If this is a COFF shared library section, use the size and | |
5797 | address from the input section. FIXME: This is COFF | |
5798 | specific; it would be cleaner if there were some other way | |
5799 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
5800 | if (((bfd_get_flavour (link_info.output_bfd) |
5801 | == bfd_target_ecoff_flavour) | |
5802 | || (bfd_get_flavour (link_info.output_bfd) | |
5803 | == bfd_target_coff_flavour)) | |
ebe372c1 | 5804 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 5805 | { |
08da4cac | 5806 | asection *input; |
252b5132 RH |
5807 | |
5808 | if (os->children.head == NULL | |
bba1a0c0 | 5809 | || os->children.head->header.next != NULL |
6feb9908 AM |
5810 | || (os->children.head->header.type |
5811 | != lang_input_section_enum)) | |
df5f2391 | 5812 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 5813 | " section %s\n"), os->name); |
252b5132 RH |
5814 | |
5815 | input = os->children.head->input_section.section; | |
fd361982 AM |
5816 | bfd_set_section_vma (os->bfd_section, |
5817 | bfd_section_vma (input)); | |
8772de11 MR |
5818 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5819 | os->bfd_section->size = input->size; | |
252b5132 RH |
5820 | break; |
5821 | } | |
5822 | ||
a5df8c84 | 5823 | newdot = dot; |
13075d04 | 5824 | dotdelta = 0; |
252b5132 RH |
5825 | if (bfd_is_abs_section (os->bfd_section)) |
5826 | { | |
5827 | /* No matter what happens, an abs section starts at zero. */ | |
5828 | ASSERT (os->bfd_section->vma == 0); | |
5829 | } | |
5830 | else | |
5831 | { | |
1579bae1 | 5832 | if (os->addr_tree == NULL) |
252b5132 RH |
5833 | { |
5834 | /* No address specified for this section, get one | |
5835 | from the region specification. */ | |
1579bae1 | 5836 | if (os->region == NULL |
6feb9908 | 5837 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
5838 | && os->region->name_list.name[0] == '*' |
5839 | && strcmp (os->region->name_list.name, | |
6feb9908 | 5840 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
5841 | { |
5842 | os->region = lang_memory_default (os->bfd_section); | |
5843 | } | |
5844 | ||
5845 | /* If a loadable section is using the default memory | |
5846 | region, and some non default memory regions were | |
66184979 | 5847 | defined, issue an error message. */ |
f0636a44 AM |
5848 | if (!os->ignored |
5849 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 5850 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 5851 | && check_regions |
4a93e180 | 5852 | && strcmp (os->region->name_list.name, |
6feb9908 | 5853 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 5854 | && lang_memory_region_list != NULL |
4a93e180 | 5855 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 5856 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a | 5857 | || lang_memory_region_list->next != NULL) |
baf09cba | 5858 | && lang_sizing_iteration == 1) |
66184979 NC |
5859 | { |
5860 | /* By default this is an error rather than just a | |
5861 | warning because if we allocate the section to the | |
5862 | default memory region we can end up creating an | |
07f3b6ad KH |
5863 | excessively large binary, or even seg faulting when |
5864 | attempting to perform a negative seek. See | |
6feb9908 | 5865 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
5866 | for an example of this. This behaviour can be |
5867 | overridden by the using the --no-check-sections | |
5868 | switch. */ | |
5869 | if (command_line.check_section_addresses) | |
df5f2391 | 5870 | einfo (_("%F%P: error: no memory region specified" |
6feb9908 | 5871 | " for loadable section `%s'\n"), |
fd361982 | 5872 | bfd_section_name (os->bfd_section)); |
66184979 | 5873 | else |
6feb9908 AM |
5874 | einfo (_("%P: warning: no memory region specified" |
5875 | " for loadable section `%s'\n"), | |
fd361982 | 5876 | bfd_section_name (os->bfd_section)); |
66184979 | 5877 | } |
252b5132 | 5878 | |
e9ee469a | 5879 | newdot = os->region->current; |
c5b0a9ef | 5880 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 5881 | } |
94b50910 | 5882 | else |
3d9c8f6b AM |
5883 | section_alignment = exp_get_power (os->section_alignment, |
5884 | "section alignment"); | |
5f992e62 | 5885 | |
7270c5ed | 5886 | /* Align to what the section needs. */ |
c5b0a9ef | 5887 | if (section_alignment > 0) |
94b50910 AM |
5888 | { |
5889 | bfd_vma savedot = newdot; | |
baf09cba | 5890 | bfd_vma diff = 0; |
252b5132 | 5891 | |
baf09cba | 5892 | newdot = align_power (newdot, section_alignment); |
13075d04 | 5893 | dotdelta = newdot - savedot; |
baf09cba AM |
5894 | |
5895 | if (lang_sizing_iteration == 1) | |
5896 | diff = dotdelta; | |
5897 | else if (lang_sizing_iteration > 1) | |
92d4b13b AM |
5898 | { |
5899 | /* Only report adjustments that would change | |
5900 | alignment from what we have already reported. */ | |
5901 | diff = newdot - os->bfd_section->vma; | |
5902 | if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) | |
5903 | diff = 0; | |
5904 | } | |
baf09cba | 5905 | if (diff != 0 |
94b50910 | 5906 | && (config.warn_section_align |
baf09cba AM |
5907 | || os->addr_tree != NULL)) |
5908 | einfo (_("%P: warning: " | |
5909 | "start of section %s changed by %ld\n"), | |
5910 | os->name, (long) diff); | |
94b50910 | 5911 | } |
252b5132 | 5912 | |
fd361982 | 5913 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 5914 | |
252b5132 RH |
5915 | os->bfd_section->output_offset = 0; |
5916 | } | |
5917 | ||
e535e147 | 5918 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
5919 | os->fill, newdot, relax, check_regions); |
5920 | ||
f38a2680 | 5921 | os->processed_vma = true; |
e9ee469a AM |
5922 | |
5923 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
5924 | /* Except for some special linker created sections, |
5925 | no output section should change from zero size | |
5926 | after strip_excluded_output_sections. A non-zero | |
5927 | size on an ignored section indicates that some | |
5928 | input section was not sized early enough. */ | |
5929 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 5930 | else |
e9ee469a | 5931 | { |
cde9e0be AM |
5932 | dot = os->bfd_section->vma; |
5933 | ||
5934 | /* Put the section within the requested block size, or | |
5935 | align at the block boundary. */ | |
5936 | after = ((dot | |
5937 | + TO_ADDR (os->bfd_section->size) | |
5938 | + os->block_value - 1) | |
5939 | & - (bfd_vma) os->block_value); | |
5940 | ||
8772de11 MR |
5941 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5942 | os->bfd_section->size = TO_SIZE (after | |
5943 | - os->bfd_section->vma); | |
cde9e0be AM |
5944 | } |
5945 | ||
5946 | /* Set section lma. */ | |
5947 | r = os->region; | |
5948 | if (r == NULL) | |
f38a2680 | 5949 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
cde9e0be AM |
5950 | |
5951 | if (os->load_base) | |
5952 | { | |
5953 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
5954 | os->bfd_section->lma = lma; | |
e9ee469a | 5955 | } |
07dfcf38 | 5956 | else if (os->lma_region != NULL) |
cde9e0be AM |
5957 | { |
5958 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 5959 | |
13075d04 SH |
5960 | if (os->align_lma_with_input) |
5961 | lma += dotdelta; | |
5962 | else | |
5963 | { | |
5964 | /* When LMA_REGION is the same as REGION, align the LMA | |
5965 | as we did for the VMA, possibly including alignment | |
5966 | from the bfd section. If a different region, then | |
5967 | only align according to the value in the output | |
5968 | statement. */ | |
5969 | if (os->lma_region != os->region) | |
3d9c8f6b AM |
5970 | section_alignment = exp_get_power (os->section_alignment, |
5971 | "section alignment"); | |
13075d04 SH |
5972 | if (section_alignment > 0) |
5973 | lma = align_power (lma, section_alignment); | |
5974 | } | |
cde9e0be AM |
5975 | os->bfd_section->lma = lma; |
5976 | } | |
dc0b6aa0 AM |
5977 | else if (r->last_os != NULL |
5978 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
5979 | { |
5980 | bfd_vma lma; | |
5981 | asection *last; | |
5982 | ||
5983 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
5984 | |
5985 | /* A backwards move of dot should be accompanied by | |
5986 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 5987 | os->load_base set) because backwards moves can |
dc0b6aa0 | 5988 | create overlapping LMAs. */ |
264b6205 | 5989 | if (dot < last->vma |
91eb6c46 | 5990 | && os->bfd_section->size != 0 |
ca62bc4a | 5991 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 5992 | { |
dc0b6aa0 AM |
5993 | /* If dot moved backwards then leave lma equal to |
5994 | vma. This is the old default lma, which might | |
5995 | just happen to work when the backwards move is | |
91eb6c46 AM |
5996 | sufficiently large. Nag if this changes anything, |
5997 | so people can fix their linker scripts. */ | |
5998 | ||
5999 | if (last->vma != last->lma) | |
0aa7f586 AM |
6000 | einfo (_("%P: warning: dot moved backwards " |
6001 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
6002 | } |
6003 | else | |
cde9e0be | 6004 | { |
152d792f AM |
6005 | /* If this is an overlay, set the current lma to that |
6006 | at the end of the previous section. */ | |
6007 | if (os->sectype == overlay_section) | |
fc90c280 | 6008 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
6009 | |
6010 | /* Otherwise, keep the same lma to vma relationship | |
6011 | as the previous section. */ | |
6012 | else | |
8610e0fd | 6013 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 6014 | |
c5b0a9ef AM |
6015 | if (section_alignment > 0) |
6016 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
6017 | os->bfd_section->lma = lma; |
6018 | } | |
6019 | } | |
f38a2680 | 6020 | os->processed_lma = true; |
5f992e62 | 6021 | |
cde9e0be AM |
6022 | /* Keep track of normal sections using the default |
6023 | lma region. We use this to set the lma for | |
6024 | following sections. Overlays or other linker | |
6025 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
6026 | default lma == vma is incorrect. |
6027 | To avoid warnings about dot moving backwards when using | |
6028 | -Ttext, don't start tracking sections until we find one | |
14ea2c1b AB |
6029 | of non-zero size or with lma set differently to vma. |
6030 | Do this tracking before we short-cut the loop so that we | |
6031 | track changes for the case where the section size is zero, | |
6032 | but the lma is set differently to the vma. This is | |
6033 | important, if an orphan section is placed after an | |
6034 | otherwise empty output section that has an explicit lma | |
6035 | set, we want that lma reflected in the orphans lma. */ | |
7b243801 AM |
6036 | if (((!IGNORE_SECTION (os->bfd_section) |
6037 | && (os->bfd_section->size != 0 | |
6038 | || (r->last_os == NULL | |
6039 | && os->bfd_section->vma != os->bfd_section->lma) | |
6040 | || (r->last_os != NULL | |
6041 | && dot >= (r->last_os->output_section_statement | |
6042 | .bfd_section->vma)))) | |
6043 | || os->sectype == first_overlay_section) | |
cde9e0be | 6044 | && os->lma_region == NULL |
0e1862bb | 6045 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 6046 | r->last_os = s; |
9f88b410 | 6047 | |
14ea2c1b AB |
6048 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
6049 | break; | |
6050 | ||
e5caec89 | 6051 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 6052 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 6053 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
6054 | dotdelta = TO_ADDR (os->bfd_section->size); |
6055 | else | |
6056 | dotdelta = 0; | |
6057 | dot += dotdelta; | |
e5caec89 | 6058 | |
9f88b410 | 6059 | if (os->update_dot_tree != 0) |
e9ee469a | 6060 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 6061 | |
252b5132 RH |
6062 | /* Update dot in the region ? |
6063 | We only do this if the section is going to be allocated, | |
6064 | since unallocated sections do not contribute to the region's | |
2e76e85a | 6065 | overall size in memory. */ |
1579bae1 | 6066 | if (os->region != NULL |
2e76e85a | 6067 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
6068 | { |
6069 | os->region->current = dot; | |
5f992e62 | 6070 | |
cf888e70 NC |
6071 | if (check_regions) |
6072 | /* Make sure the new address is within the region. */ | |
6073 | os_region_check (os, os->region, os->addr_tree, | |
6074 | os->bfd_section->vma); | |
08da4cac | 6075 | |
a2cab753 | 6076 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
6077 | && ((os->bfd_section->flags & SEC_LOAD) |
6078 | || os->align_lma_with_input)) | |
08da4cac | 6079 | { |
13075d04 | 6080 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 6081 | |
cf888e70 NC |
6082 | if (check_regions) |
6083 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 6084 | os->bfd_section->lma); |
08da4cac | 6085 | } |
252b5132 RH |
6086 | } |
6087 | } | |
6088 | break; | |
6089 | ||
6090 | case lang_constructors_statement_enum: | |
e535e147 | 6091 | dot = lang_size_sections_1 (&constructor_list.head, |
2d20f7bf | 6092 | output_section_statement, |
cf888e70 | 6093 | fill, dot, relax, check_regions); |
252b5132 RH |
6094 | break; |
6095 | ||
6096 | case lang_data_statement_enum: | |
6097 | { | |
6098 | unsigned int size = 0; | |
6099 | ||
7fabd029 | 6100 | s->data_statement.output_offset = |
08da4cac | 6101 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
6102 | s->data_statement.output_section = |
6103 | output_section_statement->bfd_section; | |
6104 | ||
1b493742 NS |
6105 | /* We might refer to provided symbols in the expression, and |
6106 | need to mark them as needed. */ | |
e9ee469a | 6107 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 6108 | |
252b5132 RH |
6109 | switch (s->data_statement.type) |
6110 | { | |
08da4cac KH |
6111 | default: |
6112 | abort (); | |
252b5132 RH |
6113 | case QUAD: |
6114 | case SQUAD: | |
6115 | size = QUAD_SIZE; | |
6116 | break; | |
6117 | case LONG: | |
6118 | size = LONG_SIZE; | |
6119 | break; | |
6120 | case SHORT: | |
6121 | size = SHORT_SIZE; | |
6122 | break; | |
6123 | case BYTE: | |
6124 | size = BYTE_SIZE; | |
6125 | break; | |
6126 | } | |
e5caa5e0 AM |
6127 | if (size < TO_SIZE ((unsigned) 1)) |
6128 | size = TO_SIZE ((unsigned) 1); | |
6129 | dot += TO_ADDR (size); | |
8772de11 MR |
6130 | if (!(output_section_statement->bfd_section->flags |
6131 | & SEC_FIXED_SIZE)) | |
6132 | output_section_statement->bfd_section->size | |
6133 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
aa4c3319 | 6134 | |
252b5132 RH |
6135 | } |
6136 | break; | |
6137 | ||
6138 | case lang_reloc_statement_enum: | |
6139 | { | |
6140 | int size; | |
6141 | ||
7fabd029 | 6142 | s->reloc_statement.output_offset = |
252b5132 RH |
6143 | dot - output_section_statement->bfd_section->vma; |
6144 | s->reloc_statement.output_section = | |
6145 | output_section_statement->bfd_section; | |
6146 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 6147 | dot += TO_ADDR (size); |
8772de11 MR |
6148 | if (!(output_section_statement->bfd_section->flags |
6149 | & SEC_FIXED_SIZE)) | |
6150 | output_section_statement->bfd_section->size | |
6151 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
252b5132 RH |
6152 | } |
6153 | break; | |
5f992e62 | 6154 | |
252b5132 | 6155 | case lang_wild_statement_enum: |
e535e147 | 6156 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
2d20f7bf | 6157 | output_section_statement, |
cf888e70 | 6158 | fill, dot, relax, check_regions); |
252b5132 RH |
6159 | break; |
6160 | ||
6161 | case lang_object_symbols_statement_enum: | |
3b6c1966 AM |
6162 | link_info.create_object_symbols_section |
6163 | = output_section_statement->bfd_section; | |
6164 | output_section_statement->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 6165 | break; |
e9ee469a | 6166 | |
252b5132 RH |
6167 | case lang_output_statement_enum: |
6168 | case lang_target_statement_enum: | |
6169 | break; | |
e9ee469a | 6170 | |
252b5132 RH |
6171 | case lang_input_section_enum: |
6172 | { | |
6173 | asection *i; | |
6174 | ||
e535e147 | 6175 | i = s->input_section.section; |
eea6121a | 6176 | if (relax) |
252b5132 | 6177 | { |
f38a2680 | 6178 | bool again; |
252b5132 | 6179 | |
0aa7f586 | 6180 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
df5f2391 | 6181 | einfo (_("%F%P: can't relax section: %E\n")); |
252b5132 | 6182 | if (again) |
f38a2680 | 6183 | *relax = true; |
252b5132 | 6184 | } |
b3327aad | 6185 | dot = size_input_section (prev, output_section_statement, |
abf874aa | 6186 | fill, &removed, dot); |
252b5132 RH |
6187 | } |
6188 | break; | |
e9ee469a | 6189 | |
252b5132 RH |
6190 | case lang_input_statement_enum: |
6191 | break; | |
e9ee469a | 6192 | |
252b5132 | 6193 | case lang_fill_statement_enum: |
08da4cac KH |
6194 | s->fill_statement.output_section = |
6195 | output_section_statement->bfd_section; | |
252b5132 RH |
6196 | |
6197 | fill = s->fill_statement.fill; | |
6198 | break; | |
e9ee469a | 6199 | |
252b5132 RH |
6200 | case lang_assignment_statement_enum: |
6201 | { | |
6202 | bfd_vma newdot = dot; | |
49c13adb | 6203 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 6204 | |
0f99513f | 6205 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6206 | |
49c13adb | 6207 | exp_fold_tree (tree, |
408082ec | 6208 | output_section_statement->bfd_section, |
252b5132 RH |
6209 | &newdot); |
6210 | ||
2085ee26 | 6211 | ldlang_check_relro_region (s); |
ed1794ee | 6212 | |
0f99513f | 6213 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6214 | |
d2667025 | 6215 | /* This symbol may be relative to this section. */ |
a14a5de3 | 6216 | if ((tree->type.node_class == etree_provided |
49c13adb L |
6217 | || tree->type.node_class == etree_assign) |
6218 | && (tree->assign.dst [0] != '.' | |
6219 | || tree->assign.dst [1] != '\0')) | |
6d8bf25d | 6220 | output_section_statement->update_dot = 1; |
49c13adb | 6221 | |
85852e36 | 6222 | if (!output_section_statement->ignored) |
252b5132 | 6223 | { |
252b5132 RH |
6224 | if (output_section_statement == abs_output_section) |
6225 | { | |
6226 | /* If we don't have an output section, then just adjust | |
6227 | the default memory address. */ | |
6feb9908 | 6228 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
f38a2680 | 6229 | false)->current = newdot; |
252b5132 | 6230 | } |
85852e36 | 6231 | else if (newdot != dot) |
252b5132 | 6232 | { |
b3327aad AM |
6233 | /* Insert a pad after this statement. We can't |
6234 | put the pad before when relaxing, in case the | |
6235 | assignment references dot. */ | |
e5caa5e0 | 6236 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
6237 | output_section_statement->bfd_section, dot); |
6238 | ||
6239 | /* Don't neuter the pad below when relaxing. */ | |
6240 | s = s->header.next; | |
9dfc8ab2 | 6241 | |
e9ee469a AM |
6242 | /* If dot is advanced, this implies that the section |
6243 | should have space allocated to it, unless the | |
6244 | user has explicitly stated that the section | |
2e76e85a | 6245 | should not be allocated. */ |
f4eaaf7f AM |
6246 | if (output_section_statement->sectype != noalloc_section |
6247 | && (output_section_statement->sectype != noload_section | |
6248 | || (bfd_get_flavour (link_info.output_bfd) | |
6249 | == bfd_target_elf_flavour))) | |
e9ee469a AM |
6250 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
6251 | } | |
252b5132 RH |
6252 | dot = newdot; |
6253 | } | |
6254 | } | |
6255 | break; | |
6256 | ||
6257 | case lang_padding_statement_enum: | |
c0c330a7 AM |
6258 | /* If this is the first time lang_size_sections is called, |
6259 | we won't have any padding statements. If this is the | |
6260 | second or later passes when relaxing, we should allow | |
6261 | padding to shrink. If padding is needed on this pass, it | |
6262 | will be added back in. */ | |
6263 | s->padding_statement.size = 0; | |
6e814ff8 AM |
6264 | |
6265 | /* Make sure output_offset is valid. If relaxation shrinks | |
6266 | the section and this pad isn't needed, it's possible to | |
6267 | have output_offset larger than the final size of the | |
6268 | section. bfd_set_section_contents will complain even for | |
6269 | a pad size of zero. */ | |
6270 | s->padding_statement.output_offset | |
6271 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
6272 | break; |
6273 | ||
6274 | case lang_group_statement_enum: | |
e535e147 | 6275 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
2d20f7bf | 6276 | output_section_statement, |
cf888e70 | 6277 | fill, dot, relax, check_regions); |
252b5132 RH |
6278 | break; |
6279 | ||
53d25da6 | 6280 | case lang_insert_statement_enum: |
252b5132 RH |
6281 | break; |
6282 | ||
c0c330a7 | 6283 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
6284 | case lang_address_statement_enum: |
6285 | break; | |
53d25da6 AM |
6286 | |
6287 | default: | |
6288 | FAIL (); | |
6289 | break; | |
252b5132 | 6290 | } |
abf874aa CL |
6291 | |
6292 | /* If an input section doesn't fit in the current output | |
6293 | section, remove it from the list. Handle the case where we | |
6294 | have to remove an input_section statement here: there is a | |
6295 | special case to remove the first element of the list. */ | |
6296 | if (link_info.non_contiguous_regions && removed) | |
6297 | { | |
6298 | /* If we removed the first element during the previous | |
6299 | iteration, override the loop assignment of prev_s. */ | |
6300 | if (removed_prev_s) | |
6301 | prev_s = NULL; | |
6302 | ||
6303 | if (prev_s) | |
6304 | { | |
6305 | /* If there was a real previous input section, just skip | |
6306 | the current one. */ | |
6307 | prev_s->header.next=s->header.next; | |
6308 | s = prev_s; | |
f38a2680 | 6309 | removed_prev_s = false; |
abf874aa CL |
6310 | } |
6311 | else | |
6312 | { | |
6313 | /* Remove the first input section of the list. */ | |
6314 | *prev = s->header.next; | |
f38a2680 | 6315 | removed_prev_s = true; |
abf874aa CL |
6316 | } |
6317 | ||
6318 | /* Move to next element, unless we removed the head of the | |
6319 | list. */ | |
6320 | if (!removed_prev_s) | |
6321 | prev = &s->header.next; | |
6322 | } | |
6323 | else | |
6324 | { | |
6325 | prev = &s->header.next; | |
f38a2680 | 6326 | removed_prev_s = false; |
abf874aa | 6327 | } |
252b5132 RH |
6328 | } |
6329 | return dot; | |
6330 | } | |
6331 | ||
591a748a NC |
6332 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
6333 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
6334 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
6335 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b | 6336 | |
f38a2680 | 6337 | bool |
0aa7f586 AM |
6338 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
6339 | bfd *abfd ATTRIBUTE_UNUSED, | |
6340 | asection *current_section, | |
6341 | asection *previous_section, | |
f38a2680 | 6342 | bool new_segment) |
2889e75b | 6343 | { |
0aa7f586 AM |
6344 | lang_output_section_statement_type *cur; |
6345 | lang_output_section_statement_type *prev; | |
591a748a NC |
6346 | |
6347 | /* The checks below are only necessary when the BFD library has decided | |
6348 | that the two sections ought to be placed into the same segment. */ | |
2889e75b | 6349 | if (new_segment) |
f38a2680 | 6350 | return true; |
2889e75b NC |
6351 | |
6352 | /* Paranoia checks. */ | |
6353 | if (current_section == NULL || previous_section == NULL) | |
6354 | return new_segment; | |
6355 | ||
4724d37e RM |
6356 | /* If this flag is set, the target never wants code and non-code |
6357 | sections comingled in the same segment. */ | |
6358 | if (config.separate_code | |
6359 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
f38a2680 | 6360 | return true; |
4724d37e | 6361 | |
2889e75b NC |
6362 | /* Find the memory regions associated with the two sections. |
6363 | We call lang_output_section_find() here rather than scanning the list | |
6364 | of output sections looking for a matching section pointer because if | |
591a748a | 6365 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6366 | cur = lang_output_section_find (current_section->name); |
6367 | prev = lang_output_section_find (previous_section->name); | |
6368 | ||
591a748a | 6369 | /* More paranoia. */ |
2889e75b NC |
6370 | if (cur == NULL || prev == NULL) |
6371 | return new_segment; | |
6372 | ||
6373 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6374 | different segments. See the email thread starting at the following |
6375 | URL for the reasons why this is necessary: | |
2889e75b NC |
6376 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6377 | return cur->region != prev->region; | |
6378 | } | |
6379 | ||
e9ee469a | 6380 | void |
f38a2680 | 6381 | one_lang_size_sections_pass (bool *relax, bool check_regions) |
2d20f7bf | 6382 | { |
420e579c | 6383 | lang_statement_iteration++; |
baf09cba AM |
6384 | if (expld.phase != lang_mark_phase_enum) |
6385 | lang_sizing_iteration++; | |
e535e147 AM |
6386 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6387 | 0, 0, relax, check_regions); | |
e9ee469a | 6388 | } |
420e579c | 6389 | |
f38a2680 | 6390 | static bool |
2085ee26 | 6391 | lang_size_segment (void) |
e9ee469a | 6392 | { |
cba6246d L |
6393 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6394 | a page could be saved in the data segment. */ | |
2085ee26 | 6395 | seg_align_type *seg = &expld.dataseg; |
cba6246d L |
6396 | bfd_vma first, last; |
6397 | ||
9833b775 AM |
6398 | first = -seg->base & (seg->commonpagesize - 1); |
6399 | last = seg->end & (seg->commonpagesize - 1); | |
cba6246d | 6400 | if (first && last |
9833b775 AM |
6401 | && ((seg->base & ~(seg->commonpagesize - 1)) |
6402 | != (seg->end & ~(seg->commonpagesize - 1))) | |
6403 | && first + last <= seg->commonpagesize) | |
8c37241b | 6404 | { |
cba6246d | 6405 | seg->phase = exp_seg_adjust; |
f38a2680 | 6406 | return true; |
cba6246d L |
6407 | } |
6408 | ||
6409 | seg->phase = exp_seg_done; | |
f38a2680 | 6410 | return false; |
cba6246d L |
6411 | } |
6412 | ||
6413 | static bfd_vma | |
2085ee26 | 6414 | lang_size_relro_segment_1 (void) |
cba6246d | 6415 | { |
2085ee26 | 6416 | seg_align_type *seg = &expld.dataseg; |
5197c88e | 6417 | bfd_vma relro_end, desired_end; |
6a9d0866 | 6418 | asection *sec; |
8c37241b | 6419 | |
cba6246d | 6420 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
9833b775 | 6421 | relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; |
0e5fabeb | 6422 | |
cba6246d | 6423 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
5197c88e | 6424 | desired_end = relro_end - seg->relro_offset; |
0e5fabeb | 6425 | |
5197c88e | 6426 | /* For sections in the relro segment.. */ |
cba6246d | 6427 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) |
6a9d0866 AM |
6428 | if ((sec->flags & SEC_ALLOC) != 0 |
6429 | && sec->vma >= seg->base | |
6430 | && sec->vma < seg->relro_end - seg->relro_offset) | |
cba6246d | 6431 | { |
6a9d0866 AM |
6432 | /* Where do we want to put this section so that it ends as |
6433 | desired? */ | |
6434 | bfd_vma start, end, bump; | |
6435 | ||
6436 | end = start = sec->vma; | |
6437 | if (!IS_TBSS (sec)) | |
6438 | end += TO_ADDR (sec->size); | |
6439 | bump = desired_end - end; | |
6440 | /* We'd like to increase START by BUMP, but we must heed | |
6441 | alignment so the increase might be less than optimum. */ | |
6442 | start += bump; | |
6443 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6444 | /* This is now the desired end for the previous section. */ | |
6445 | desired_end = start; | |
5197c88e AM |
6446 | } |
6447 | ||
6448 | seg->phase = exp_seg_relro_adjust; | |
6449 | ASSERT (desired_end >= seg->base); | |
5197c88e | 6450 | seg->base = desired_end; |
cba6246d L |
6451 | return relro_end; |
6452 | } | |
6453 | ||
f38a2680 AM |
6454 | static bool |
6455 | lang_size_relro_segment (bool *relax, bool check_regions) | |
cba6246d | 6456 | { |
f38a2680 | 6457 | bool do_reset = false; |
cba6246d L |
6458 | |
6459 | if (link_info.relro && expld.dataseg.relro_end) | |
6460 | { | |
e29de63f | 6461 | bfd_vma data_initial_base = expld.dataseg.base; |
2085ee26 | 6462 | bfd_vma data_relro_end = lang_size_relro_segment_1 (); |
0e5fabeb | 6463 | |
f86a8756 | 6464 | lang_reset_memory_regions (); |
e9ee469a | 6465 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6466 | |
cba6246d L |
6467 | /* Assignments to dot, or to output section address in a user |
6468 | script have increased padding over the original. Revert. */ | |
e29de63f | 6469 | if (expld.dataseg.relro_end > data_relro_end) |
a4f5ad88 | 6470 | { |
2085ee26 | 6471 | expld.dataseg.base = data_initial_base; |
f38a2680 | 6472 | do_reset = true; |
a4f5ad88 | 6473 | } |
8c37241b | 6474 | } |
2085ee26 | 6475 | else if (lang_size_segment ()) |
f38a2680 | 6476 | do_reset = true; |
cba6246d L |
6477 | |
6478 | return do_reset; | |
6479 | } | |
6480 | ||
6481 | void | |
f38a2680 | 6482 | lang_size_sections (bool *relax, bool check_regions) |
cba6246d L |
6483 | { |
6484 | expld.phase = lang_allocating_phase_enum; | |
6485 | expld.dataseg.phase = exp_seg_none; | |
6486 | ||
6487 | one_lang_size_sections_pass (relax, check_regions); | |
6488 | ||
6489 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6490 | expld.dataseg.phase = exp_seg_done; | |
6491 | ||
6492 | if (expld.dataseg.phase == exp_seg_end_seen) | |
2d20f7bf | 6493 | { |
f38a2680 | 6494 | bool do_reset |
cba6246d L |
6495 | = lang_size_relro_segment (relax, check_regions); |
6496 | ||
6497 | if (do_reset) | |
2d20f7bf | 6498 | { |
f86a8756 | 6499 | lang_reset_memory_regions (); |
e9ee469a | 6500 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6501 | } |
cba6246d L |
6502 | |
6503 | if (link_info.relro && expld.dataseg.relro_end) | |
6504 | { | |
6505 | link_info.relro_start = expld.dataseg.base; | |
6506 | link_info.relro_end = expld.dataseg.relro_end; | |
6507 | } | |
2d20f7bf | 6508 | } |
2d20f7bf JJ |
6509 | } |
6510 | ||
d2667025 AM |
6511 | static lang_output_section_statement_type *current_section; |
6512 | static lang_assignment_statement_type *current_assign; | |
f38a2680 | 6513 | static bool prefer_next_section; |
d2667025 | 6514 | |
420e579c HPN |
6515 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6516 | ||
6517 | static bfd_vma | |
66e28d60 AM |
6518 | lang_do_assignments_1 (lang_statement_union_type *s, |
6519 | lang_output_section_statement_type *current_os, | |
6520 | fill_type *fill, | |
d2667025 | 6521 | bfd_vma dot, |
f38a2680 | 6522 | bool *found_end) |
252b5132 | 6523 | { |
1579bae1 | 6524 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6525 | { |
6526 | switch (s->header.type) | |
6527 | { | |
6528 | case lang_constructors_statement_enum: | |
420e579c | 6529 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6530 | current_os, fill, dot, found_end); |
252b5132 RH |
6531 | break; |
6532 | ||
6533 | case lang_output_section_statement_enum: | |
6534 | { | |
d1778b88 | 6535 | lang_output_section_statement_type *os; |
f02cb058 | 6536 | bfd_vma newdot; |
252b5132 | 6537 | |
d1778b88 | 6538 | os = &(s->output_section_statement); |
d2667025 | 6539 | os->after_end = *found_end; |
61826503 | 6540 | init_opb (os->bfd_section); |
1cba418d AM |
6541 | newdot = dot; |
6542 | if (os->bfd_section != NULL) | |
252b5132 | 6543 | { |
1cba418d | 6544 | if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) |
d2667025 AM |
6545 | { |
6546 | current_section = os; | |
f38a2680 | 6547 | prefer_next_section = false; |
d2667025 | 6548 | } |
1cba418d | 6549 | newdot = os->bfd_section->vma; |
f02cb058 AM |
6550 | } |
6551 | newdot = lang_do_assignments_1 (os->children.head, | |
1cba418d | 6552 | os, os->fill, newdot, found_end); |
f02cb058 AM |
6553 | if (!os->ignored) |
6554 | { | |
6555 | if (os->bfd_section != NULL) | |
6556 | { | |
1cba418d AM |
6557 | newdot = os->bfd_section->vma; |
6558 | ||
f02cb058 AM |
6559 | /* .tbss sections effectively have zero size. */ |
6560 | if (!IS_TBSS (os->bfd_section) | |
6561 | || bfd_link_relocatable (&link_info)) | |
1cba418d | 6562 | newdot += TO_ADDR (os->bfd_section->size); |
f02cb058 AM |
6563 | |
6564 | if (os->update_dot_tree != NULL) | |
6565 | exp_fold_tree (os->update_dot_tree, | |
1cba418d | 6566 | bfd_abs_section_ptr, &newdot); |
f02cb058 | 6567 | } |
1cba418d | 6568 | dot = newdot; |
252b5132 RH |
6569 | } |
6570 | } | |
6571 | break; | |
e9ee469a | 6572 | |
252b5132 RH |
6573 | case lang_wild_statement_enum: |
6574 | ||
420e579c | 6575 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6576 | current_os, fill, dot, found_end); |
252b5132 RH |
6577 | break; |
6578 | ||
6579 | case lang_object_symbols_statement_enum: | |
6580 | case lang_output_statement_enum: | |
6581 | case lang_target_statement_enum: | |
252b5132 | 6582 | break; |
e9ee469a | 6583 | |
252b5132 | 6584 | case lang_data_statement_enum: |
e9ee469a AM |
6585 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
6586 | if (expld.result.valid_p) | |
7542af2a AM |
6587 | { |
6588 | s->data_statement.value = expld.result.value; | |
6589 | if (expld.result.section != NULL) | |
6590 | s->data_statement.value += expld.result.section->vma; | |
6591 | } | |
f02cb058 | 6592 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6593 | einfo (_("%F%P: invalid data statement\n")); |
b7a26f91 KH |
6594 | { |
6595 | unsigned int size; | |
08da4cac KH |
6596 | switch (s->data_statement.type) |
6597 | { | |
6598 | default: | |
6599 | abort (); | |
6600 | case QUAD: | |
6601 | case SQUAD: | |
6602 | size = QUAD_SIZE; | |
6603 | break; | |
6604 | case LONG: | |
6605 | size = LONG_SIZE; | |
6606 | break; | |
6607 | case SHORT: | |
6608 | size = SHORT_SIZE; | |
6609 | break; | |
6610 | case BYTE: | |
6611 | size = BYTE_SIZE; | |
6612 | break; | |
6613 | } | |
e5caa5e0 AM |
6614 | if (size < TO_SIZE ((unsigned) 1)) |
6615 | size = TO_SIZE ((unsigned) 1); | |
6616 | dot += TO_ADDR (size); | |
08da4cac | 6617 | } |
252b5132 RH |
6618 | break; |
6619 | ||
6620 | case lang_reloc_statement_enum: | |
e9ee469a AM |
6621 | exp_fold_tree (s->reloc_statement.addend_exp, |
6622 | bfd_abs_section_ptr, &dot); | |
6623 | if (expld.result.valid_p) | |
6624 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6625 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6626 | einfo (_("%F%P: invalid reloc statement\n")); |
e5caa5e0 | 6627 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6628 | break; |
6629 | ||
6630 | case lang_input_section_enum: | |
6631 | { | |
6632 | asection *in = s->input_section.section; | |
6633 | ||
57ceae94 | 6634 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6635 | dot += TO_ADDR (in->size); |
252b5132 RH |
6636 | } |
6637 | break; | |
6638 | ||
6639 | case lang_input_statement_enum: | |
6640 | break; | |
e9ee469a | 6641 | |
252b5132 RH |
6642 | case lang_fill_statement_enum: |
6643 | fill = s->fill_statement.fill; | |
6644 | break; | |
252b5132 | 6645 | |
e9ee469a | 6646 | case lang_assignment_statement_enum: |
d2667025 AM |
6647 | current_assign = &s->assignment_statement; |
6648 | if (current_assign->exp->type.node_class != etree_assert) | |
6649 | { | |
6650 | const char *p = current_assign->exp->assign.dst; | |
6651 | ||
6652 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
f38a2680 | 6653 | prefer_next_section = true; |
d2667025 AM |
6654 | |
6655 | while (*p == '_') | |
6656 | ++p; | |
6657 | if (strcmp (p, "end") == 0) | |
f38a2680 | 6658 | *found_end = true; |
d2667025 | 6659 | } |
e9ee469a | 6660 | exp_fold_tree (s->assignment_statement.exp, |
f02cb058 AM |
6661 | (current_os->bfd_section != NULL |
6662 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6663 | &dot); |
252b5132 | 6664 | break; |
e9ee469a | 6665 | |
252b5132 | 6666 | case lang_padding_statement_enum: |
e5caa5e0 | 6667 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6668 | break; |
6669 | ||
6670 | case lang_group_statement_enum: | |
420e579c | 6671 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6672 | current_os, fill, dot, found_end); |
252b5132 RH |
6673 | break; |
6674 | ||
53d25da6 | 6675 | case lang_insert_statement_enum: |
252b5132 | 6676 | break; |
e9ee469a | 6677 | |
252b5132 RH |
6678 | case lang_address_statement_enum: |
6679 | break; | |
53d25da6 AM |
6680 | |
6681 | default: | |
6682 | FAIL (); | |
6683 | break; | |
252b5132 | 6684 | } |
252b5132 RH |
6685 | } |
6686 | return dot; | |
6687 | } | |
6688 | ||
f2241121 | 6689 | void |
2f65ac72 | 6690 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6691 | { |
f38a2680 | 6692 | bool found_end = false; |
d2667025 AM |
6693 | |
6694 | current_section = NULL; | |
f38a2680 | 6695 | prefer_next_section = false; |
2f65ac72 | 6696 | expld.phase = phase; |
420e579c | 6697 | lang_statement_iteration++; |
d2667025 AM |
6698 | lang_do_assignments_1 (statement_list.head, |
6699 | abs_output_section, NULL, 0, &found_end); | |
6700 | } | |
6701 | ||
6702 | /* For an assignment statement outside of an output section statement, | |
6703 | choose the best of neighbouring output sections to use for values | |
6704 | of "dot". */ | |
6705 | ||
6706 | asection * | |
6707 | section_for_dot (void) | |
6708 | { | |
6709 | asection *s; | |
6710 | ||
6711 | /* Assignments belong to the previous output section, unless there | |
6712 | has been an assignment to "dot", in which case following | |
6713 | assignments belong to the next output section. (The assumption | |
6714 | is that an assignment to "dot" is setting up the address for the | |
6715 | next output section.) Except that past the assignment to "_end" | |
6716 | we always associate with the previous section. This exception is | |
6717 | for targets like SH that define an alloc .stack or other | |
6718 | weirdness after non-alloc sections. */ | |
6719 | if (current_section == NULL || prefer_next_section) | |
6720 | { | |
6721 | lang_statement_union_type *stmt; | |
6722 | lang_output_section_statement_type *os; | |
6723 | ||
6724 | for (stmt = (lang_statement_union_type *) current_assign; | |
6725 | stmt != NULL; | |
6726 | stmt = stmt->header.next) | |
6727 | if (stmt->header.type == lang_output_section_statement_enum) | |
6728 | break; | |
6729 | ||
10c00056 | 6730 | os = stmt ? &stmt->output_section_statement : NULL; |
d2667025 AM |
6731 | while (os != NULL |
6732 | && !os->after_end | |
6733 | && (os->bfd_section == NULL | |
6734 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6735 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6736 | os->bfd_section))) | |
6737 | os = os->next; | |
6738 | ||
6739 | if (current_section == NULL || os == NULL || !os->after_end) | |
6740 | { | |
6741 | if (os != NULL) | |
6742 | s = os->bfd_section; | |
6743 | else | |
6744 | s = link_info.output_bfd->section_last; | |
6745 | while (s != NULL | |
6746 | && ((s->flags & SEC_ALLOC) == 0 | |
6747 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6748 | s = s->prev; | |
6749 | if (s != NULL) | |
6750 | return s; | |
6751 | ||
6752 | return bfd_abs_section_ptr; | |
6753 | } | |
6754 | } | |
6755 | ||
6756 | s = current_section->bfd_section; | |
6757 | ||
6758 | /* The section may have been stripped. */ | |
6759 | while (s != NULL | |
6760 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6761 | || (s->flags & SEC_ALLOC) == 0 | |
6762 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6763 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6764 | s = s->prev; | |
6765 | if (s == NULL) | |
6766 | s = link_info.output_bfd->sections; | |
6767 | while (s != NULL | |
6768 | && ((s->flags & SEC_ALLOC) == 0 | |
6769 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6770 | s = s->next; | |
6771 | if (s != NULL) | |
6772 | return s; | |
6773 | ||
6774 | return bfd_abs_section_ptr; | |
420e579c HPN |
6775 | } |
6776 | ||
7dba9362 AM |
6777 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ |
6778 | ||
6779 | static struct bfd_link_hash_entry **start_stop_syms; | |
6780 | static size_t start_stop_count = 0; | |
6781 | static size_t start_stop_alloc = 0; | |
6782 | ||
6783 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6784 | to start_stop_syms. */ | |
6785 | ||
6786 | static void | |
6787 | lang_define_start_stop (const char *symbol, asection *sec) | |
6788 | { | |
6789 | struct bfd_link_hash_entry *h; | |
6790 | ||
6791 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6792 | if (h != NULL) | |
6793 | { | |
6794 | if (start_stop_count == start_stop_alloc) | |
6795 | { | |
6796 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6797 | start_stop_syms | |
6798 | = xrealloc (start_stop_syms, | |
6799 | start_stop_alloc * sizeof (*start_stop_syms)); | |
6800 | } | |
6801 | start_stop_syms[start_stop_count++] = h; | |
6802 | } | |
6803 | } | |
6804 | ||
6805 | /* Check for input sections whose names match references to | |
6806 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
6807 | preliminary definitions. */ | |
252b5132 RH |
6808 | |
6809 | static void | |
7dba9362 | 6810 | lang_init_start_stop (void) |
252b5132 | 6811 | { |
7dba9362 | 6812 | bfd *abfd; |
252b5132 | 6813 | asection *s; |
7dba9362 AM |
6814 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); |
6815 | ||
6816 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
6817 | for (s = abfd->sections; s != NULL; s = s->next) | |
6818 | { | |
6819 | const char *ps; | |
6820 | const char *secname = s->name; | |
6821 | ||
6822 | for (ps = secname; *ps != '\0'; ps++) | |
6823 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
6824 | break; | |
6825 | if (*ps == '\0') | |
6826 | { | |
6827 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
b27685f2 | 6828 | |
7dba9362 AM |
6829 | symbol[0] = leading_char; |
6830 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
6831 | lang_define_start_stop (symbol, s); | |
6832 | ||
6833 | symbol[1] = leading_char; | |
6834 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
6835 | lang_define_start_stop (symbol + 1, s); | |
6836 | ||
6837 | free (symbol); | |
6838 | } | |
6839 | } | |
6840 | } | |
6841 | ||
6842 | /* Iterate over start_stop_syms. */ | |
6843 | ||
6844 | static void | |
6845 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
6846 | { | |
6847 | size_t i; | |
6848 | ||
6849 | for (i = 0; i < start_stop_count; ++i) | |
6850 | func (start_stop_syms[i]); | |
6851 | } | |
6852 | ||
6853 | /* __start and __stop symbols are only supposed to be defined by the | |
6854 | linker for orphan sections, but we now extend that to sections that | |
6855 | map to an output section of the same name. The symbols were | |
6856 | defined early for --gc-sections, before we mapped input to output | |
6857 | sections, so undo those that don't satisfy this rule. */ | |
6858 | ||
6859 | static void | |
6860 | undef_start_stop (struct bfd_link_hash_entry *h) | |
6861 | { | |
6862 | if (h->ldscript_def) | |
b27685f2 L |
6863 | return; |
6864 | ||
7dba9362 AM |
6865 | if (h->u.def.section->output_section == NULL |
6866 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
6867 | || strcmp (h->u.def.section->name, | |
6868 | h->u.def.section->output_section->name) != 0) | |
6869 | { | |
4d1c6335 L |
6870 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, |
6871 | h->u.def.section->name); | |
6872 | if (sec != NULL) | |
6873 | { | |
6874 | /* When there are more than one input sections with the same | |
6875 | section name, SECNAME, linker picks the first one to define | |
6876 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
6877 | input section is removed by comdat group, we need to check | |
6878 | if there is still an output section with section name | |
6879 | SECNAME. */ | |
6880 | asection *i; | |
6881 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
6882 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
6883 | { | |
6884 | h->u.def.section = i; | |
6885 | return; | |
6886 | } | |
6887 | } | |
7dba9362 AM |
6888 | h->type = bfd_link_hash_undefined; |
6889 | h->u.undef.abfd = NULL; | |
fc304b88 | 6890 | if (is_elf_hash_table (link_info.hash)) |
7824c1d2 AM |
6891 | { |
6892 | const struct elf_backend_data *bed; | |
6893 | struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h; | |
6894 | unsigned int was_forced = eh->forced_local; | |
6895 | ||
6896 | bed = get_elf_backend_data (link_info.output_bfd); | |
f38a2680 | 6897 | (*bed->elf_backend_hide_symbol) (&link_info, eh, true); |
7824c1d2 AM |
6898 | if (!eh->ref_regular_nonweak) |
6899 | h->type = bfd_link_hash_undefweak; | |
6900 | eh->def_regular = 0; | |
6901 | eh->forced_local = was_forced; | |
6902 | } | |
7dba9362 AM |
6903 | } |
6904 | } | |
252b5132 | 6905 | |
7dba9362 AM |
6906 | static void |
6907 | lang_undef_start_stop (void) | |
6908 | { | |
6909 | foreach_start_stop (undef_start_stop); | |
6910 | } | |
6911 | ||
6912 | /* Check for output sections whose names match references to | |
6913 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
6914 | preliminary definitions. */ | |
6915 | ||
6916 | static void | |
6917 | lang_init_startof_sizeof (void) | |
6918 | { | |
6919 | asection *s; | |
252b5132 | 6920 | |
f13a99db | 6921 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6922 | { |
7dba9362 AM |
6923 | const char *secname = s->name; |
6924 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 6925 | |
7dba9362 AM |
6926 | sprintf (symbol, ".startof.%s", secname); |
6927 | lang_define_start_stop (symbol, s); | |
252b5132 | 6928 | |
7dba9362 AM |
6929 | memcpy (symbol + 1, ".size", 5); |
6930 | lang_define_start_stop (symbol + 1, s); | |
6931 | free (symbol); | |
6932 | } | |
6933 | } | |
cbd0eecf | 6934 | |
7dba9362 AM |
6935 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ |
6936 | ||
6937 | static void | |
6938 | set_start_stop (struct bfd_link_hash_entry *h) | |
6939 | { | |
6940 | if (h->ldscript_def | |
6941 | || h->type != bfd_link_hash_defined) | |
6942 | return; | |
cbd0eecf | 6943 | |
7dba9362 AM |
6944 | if (h->root.string[0] == '.') |
6945 | { | |
6946 | /* .startof. or .sizeof. symbol. | |
6947 | .startof. already has final value. */ | |
6948 | if (h->root.string[2] == 'i') | |
252b5132 | 6949 | { |
7dba9362 AM |
6950 | /* .sizeof. */ |
6951 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
6952 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 6953 | } |
7dba9362 AM |
6954 | } |
6955 | else | |
6956 | { | |
6957 | /* __start or __stop symbol. */ | |
6958 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 6959 | |
7dba9362 AM |
6960 | h->u.def.section = h->u.def.section->output_section; |
6961 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 6962 | { |
7dba9362 AM |
6963 | /* __stop_ */ |
6964 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 6965 | } |
252b5132 RH |
6966 | } |
6967 | } | |
6968 | ||
7dba9362 AM |
6969 | static void |
6970 | lang_finalize_start_stop (void) | |
6971 | { | |
6972 | foreach_start_stop (set_start_stop); | |
6973 | } | |
6974 | ||
97da0e26 AM |
6975 | static void |
6976 | lang_symbol_tweaks (void) | |
6977 | { | |
6978 | /* Give initial values for __start and __stop symbols, so that ELF | |
6979 | gc_sections will keep sections referenced by these symbols. Must | |
6980 | be done before lang_do_assignments. */ | |
6981 | if (config.build_constructors) | |
6982 | lang_init_start_stop (); | |
6983 | ||
6984 | /* Make __ehdr_start hidden, and set def_regular even though it is | |
6985 | likely undefined at this stage. For lang_check_relocs. */ | |
6986 | if (is_elf_hash_table (link_info.hash) | |
6987 | && !bfd_link_relocatable (&link_info)) | |
6988 | { | |
6989 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
6990 | bfd_link_hash_lookup (link_info.hash, "__ehdr_start", | |
6991 | false, false, true); | |
6992 | ||
6993 | /* Only adjust the export class if the symbol was referenced | |
6994 | and not defined, otherwise leave it alone. */ | |
6995 | if (h != NULL | |
6996 | && (h->root.type == bfd_link_hash_new | |
6997 | || h->root.type == bfd_link_hash_undefined | |
6998 | || h->root.type == bfd_link_hash_undefweak | |
6999 | || h->root.type == bfd_link_hash_common)) | |
7000 | { | |
7001 | const struct elf_backend_data *bed; | |
7002 | bed = get_elf_backend_data (link_info.output_bfd); | |
7003 | (*bed->elf_backend_hide_symbol) (&link_info, h, true); | |
7004 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) | |
7005 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
7006 | h->def_regular = 1; | |
7007 | h->root.linker_def = 1; | |
5617fae7 | 7008 | h->root.rel_from_abs = 1; |
97da0e26 AM |
7009 | } |
7010 | } | |
7011 | } | |
7012 | ||
252b5132 | 7013 | static void |
750877ba | 7014 | lang_end (void) |
252b5132 RH |
7015 | { |
7016 | struct bfd_link_hash_entry *h; | |
f38a2680 | 7017 | bool warn; |
252b5132 | 7018 | |
0e1862bb L |
7019 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
7020 | || bfd_link_dll (&link_info)) | |
71934f94 | 7021 | warn = entry_from_cmdline; |
252b5132 | 7022 | else |
f38a2680 | 7023 | warn = true; |
252b5132 | 7024 | |
ac69cbc6 | 7025 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
7026 | --gc-sections, unless --gc-keep-exported was also given. */ |
7027 | if (bfd_link_relocatable (&link_info) | |
7028 | && link_info.gc_sections | |
2f5541f3 AM |
7029 | && !link_info.gc_keep_exported) |
7030 | { | |
7031 | struct bfd_sym_chain *sym; | |
7032 | ||
7033 | for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next) | |
7034 | { | |
7035 | h = bfd_link_hash_lookup (link_info.hash, sym->name, | |
f38a2680 | 7036 | false, false, false); |
2f5541f3 AM |
7037 | if (h != NULL |
7038 | && (h->type == bfd_link_hash_defined | |
7039 | || h->type == bfd_link_hash_defweak) | |
7040 | && !bfd_is_const_section (h->u.def.section)) | |
7041 | break; | |
7042 | } | |
7043 | if (!sym) | |
7044 | einfo (_("%F%P: --gc-sections requires a defined symbol root " | |
7045 | "specified by -e or -u\n")); | |
7046 | } | |
ac69cbc6 | 7047 | |
1579bae1 | 7048 | if (entry_symbol.name == NULL) |
252b5132 | 7049 | { |
a359509e ZW |
7050 | /* No entry has been specified. Look for the default entry, but |
7051 | don't warn if we don't find it. */ | |
7052 | entry_symbol.name = entry_symbol_default; | |
f38a2680 | 7053 | warn = false; |
252b5132 RH |
7054 | } |
7055 | ||
e3e942e9 | 7056 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
f38a2680 | 7057 | false, false, true); |
1579bae1 | 7058 | if (h != NULL |
252b5132 RH |
7059 | && (h->type == bfd_link_hash_defined |
7060 | || h->type == bfd_link_hash_defweak) | |
7061 | && h->u.def.section->output_section != NULL) | |
7062 | { | |
7063 | bfd_vma val; | |
7064 | ||
7065 | val = (h->u.def.value | |
fd361982 | 7066 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 7067 | + h->u.def.section->output_offset); |
0aa7f586 | 7068 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 7069 | einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
7070 | } |
7071 | else | |
7072 | { | |
7073 | bfd_vma val; | |
5f992e62 | 7074 | const char *send; |
252b5132 RH |
7075 | |
7076 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 7077 | number. */ |
e3e942e9 | 7078 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
7079 | if (*send == '\0') |
7080 | { | |
0aa7f586 | 7081 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 7082 | einfo (_("%F%P: can't set start address\n")); |
252b5132 | 7083 | } |
5226a6a8 NC |
7084 | /* BZ 2004952: Only use the start of the entry section for executables. */ |
7085 | else if bfd_link_executable (&link_info) | |
252b5132 RH |
7086 | { |
7087 | asection *ts; | |
7088 | ||
7089 | /* Can't find the entry symbol, and it's not a number. Use | |
7090 | the first address in the text section. */ | |
f13a99db | 7091 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 7092 | if (ts != NULL) |
252b5132 RH |
7093 | { |
7094 | if (warn) | |
6feb9908 AM |
7095 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7096 | " defaulting to %V\n"), | |
e3e942e9 | 7097 | entry_symbol.name, |
fd361982 AM |
7098 | bfd_section_vma (ts)); |
7099 | if (!bfd_set_start_address (link_info.output_bfd, | |
7100 | bfd_section_vma (ts))) | |
df5f2391 | 7101 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
7102 | } |
7103 | else | |
7104 | { | |
7105 | if (warn) | |
6feb9908 AM |
7106 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7107 | " not setting start address\n"), | |
e3e942e9 | 7108 | entry_symbol.name); |
252b5132 RH |
7109 | } |
7110 | } | |
5226a6a8 NC |
7111 | else |
7112 | { | |
7113 | if (warn) | |
7114 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
7115 | " not setting start address\n"), | |
7116 | entry_symbol.name); | |
7117 | } | |
252b5132 RH |
7118 | } |
7119 | } | |
7120 | ||
7121 | /* This is a small function used when we want to ignore errors from | |
7122 | BFD. */ | |
7123 | ||
7124 | static void | |
52d45da3 AM |
7125 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
7126 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
7127 | { |
7128 | /* Don't do anything. */ | |
7129 | } | |
7130 | ||
7131 | /* Check that the architecture of all the input files is compatible | |
7132 | with the output file. Also call the backend to let it do any | |
7133 | other checking that is needed. */ | |
7134 | ||
7135 | static void | |
1579bae1 | 7136 | lang_check (void) |
252b5132 | 7137 | { |
36983a93 | 7138 | lang_input_statement_type *file; |
252b5132 | 7139 | bfd *input_bfd; |
5f992e62 | 7140 | const bfd_arch_info_type *compatible; |
252b5132 | 7141 | |
8ce18f9c | 7142 | for (file = (void *) file_chain.head; |
36983a93 AM |
7143 | file != NULL; |
7144 | file = file->next) | |
252b5132 | 7145 | { |
0381901e | 7146 | #if BFD_SUPPORTS_PLUGINS |
422b6f14 | 7147 | /* Don't check format of files claimed by plugin. */ |
36983a93 | 7148 | if (file->flags.claimed) |
422b6f14 | 7149 | continue; |
0381901e | 7150 | #endif /* BFD_SUPPORTS_PLUGINS */ |
36983a93 | 7151 | input_bfd = file->the_bfd; |
6feb9908 | 7152 | compatible |
f13a99db | 7153 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 7154 | command_line.accept_unknown_input_arch); |
30cba025 AM |
7155 | |
7156 | /* In general it is not possible to perform a relocatable | |
7157 | link between differing object formats when the input | |
7158 | file has relocations, because the relocations in the | |
7159 | input format may not have equivalent representations in | |
7160 | the output format (and besides BFD does not translate | |
7161 | relocs for other link purposes than a final link). */ | |
f437dadd AM |
7162 | if (!file->flags.just_syms |
7163 | && (bfd_link_relocatable (&link_info) | |
7164 | || link_info.emitrelocations) | |
30cba025 | 7165 | && (compatible == NULL |
f13a99db AM |
7166 | || (bfd_get_flavour (input_bfd) |
7167 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
7168 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
7169 | { | |
df5f2391 | 7170 | einfo (_("%F%P: relocatable linking with relocations from" |
871b3ab2 | 7171 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 7172 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 7173 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
7174 | /* einfo with %F exits. */ |
7175 | } | |
7176 | ||
252b5132 RH |
7177 | if (compatible == NULL) |
7178 | { | |
7179 | if (command_line.warn_mismatch) | |
df5f2391 | 7180 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 7181 | " is incompatible with %s output\n"), |
252b5132 | 7182 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 7183 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 7184 | } |
b9247304 | 7185 | |
a8acd6ee AM |
7186 | /* If the input bfd has no contents, it shouldn't set the |
7187 | private data of the output bfd. */ | |
f437dadd AM |
7188 | else if (!file->flags.just_syms |
7189 | && ((input_bfd->flags & DYNAMIC) != 0 | |
7190 | || bfd_count_sections (input_bfd) != 0)) | |
a8acd6ee | 7191 | { |
252b5132 RH |
7192 | bfd_error_handler_type pfn = NULL; |
7193 | ||
7194 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
7195 | files, temporarily set the BFD error handler to a |
7196 | function which will do nothing. We still want to call | |
7197 | bfd_merge_private_bfd_data, since it may set up | |
7198 | information which is needed in the output file. */ | |
0aa7f586 | 7199 | if (!command_line.warn_mismatch) |
252b5132 | 7200 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 7201 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
7202 | { |
7203 | if (command_line.warn_mismatch) | |
df5f2391 | 7204 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 7205 | " of file %pB\n"), input_bfd); |
252b5132 | 7206 | } |
0aa7f586 | 7207 | if (!command_line.warn_mismatch) |
252b5132 RH |
7208 | bfd_set_error_handler (pfn); |
7209 | } | |
7210 | } | |
7211 | } | |
7212 | ||
7213 | /* Look through all the global common symbols and attach them to the | |
7214 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 7215 | to roughly sort the entries by alignment. */ |
252b5132 RH |
7216 | |
7217 | static void | |
1579bae1 | 7218 | lang_common (void) |
252b5132 | 7219 | { |
a4819f54 | 7220 | if (link_info.inhibit_common_definition) |
4818e05f | 7221 | return; |
0e1862bb | 7222 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 7223 | && !command_line.force_common_definition) |
252b5132 RH |
7224 | return; |
7225 | ||
0aa7f586 | 7226 | if (!config.sort_common) |
1579bae1 | 7227 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
7228 | else |
7229 | { | |
de7dd2bd NC |
7230 | unsigned int power; |
7231 | ||
7232 | if (config.sort_common == sort_descending) | |
967928e9 AM |
7233 | { |
7234 | for (power = 4; power > 0; power--) | |
7235 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7236 | ||
7237 | power = 0; | |
7238 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7239 | } | |
de7dd2bd | 7240 | else |
967928e9 AM |
7241 | { |
7242 | for (power = 0; power <= 4; power++) | |
7243 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 7244 | |
2dc0e7b4 | 7245 | power = (unsigned int) -1; |
de7dd2bd NC |
7246 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
7247 | } | |
252b5132 RH |
7248 | } |
7249 | } | |
7250 | ||
7251 | /* Place one common symbol in the correct section. */ | |
7252 | ||
f38a2680 | 7253 | static bool |
1579bae1 | 7254 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
7255 | { |
7256 | unsigned int power_of_two; | |
7257 | bfd_vma size; | |
7258 | asection *section; | |
7259 | ||
7260 | if (h->type != bfd_link_hash_common) | |
f38a2680 | 7261 | return true; |
252b5132 RH |
7262 | |
7263 | size = h->u.c.size; | |
7264 | power_of_two = h->u.c.p->alignment_power; | |
7265 | ||
de7dd2bd NC |
7266 | if (config.sort_common == sort_descending |
7267 | && power_of_two < *(unsigned int *) info) | |
f38a2680 | 7268 | return true; |
de7dd2bd | 7269 | else if (config.sort_common == sort_ascending |
967928e9 | 7270 | && power_of_two > *(unsigned int *) info) |
f38a2680 | 7271 | return true; |
252b5132 RH |
7272 | |
7273 | section = h->u.c.p->section; | |
3023e3f6 | 7274 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
df5f2391 | 7275 | einfo (_("%F%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 7276 | h->root.string); |
252b5132 RH |
7277 | |
7278 | if (config.map_file != NULL) | |
7279 | { | |
f38a2680 | 7280 | static bool header_printed; |
252b5132 RH |
7281 | int len; |
7282 | char *name; | |
6b9bd54c | 7283 | char buf[32]; |
252b5132 | 7284 | |
0aa7f586 | 7285 | if (!header_printed) |
252b5132 RH |
7286 | { |
7287 | minfo (_("\nAllocating common symbols\n")); | |
7288 | minfo (_("Common symbol size file\n\n")); | |
f38a2680 | 7289 | header_printed = true; |
252b5132 RH |
7290 | } |
7291 | ||
f13a99db | 7292 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 7293 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
7294 | if (name == NULL) |
7295 | { | |
7296 | minfo ("%s", h->root.string); | |
7297 | len = strlen (h->root.string); | |
7298 | } | |
7299 | else | |
7300 | { | |
7301 | minfo ("%s", name); | |
7302 | len = strlen (name); | |
7303 | free (name); | |
7304 | } | |
252b5132 RH |
7305 | |
7306 | if (len >= 19) | |
7307 | { | |
7308 | print_nl (); | |
7309 | len = 0; | |
7310 | } | |
252b5132 | 7311 | |
f493c217 | 7312 | sprintf (buf, "%" PRIx64, (uint64_t) size); |
6b9bd54c | 7313 | fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); |
252b5132 | 7314 | |
871b3ab2 | 7315 | minfo ("%pB\n", section->owner); |
252b5132 RH |
7316 | } |
7317 | ||
f38a2680 | 7318 | return true; |
252b5132 RH |
7319 | } |
7320 | ||
c005eb9e AB |
7321 | /* Handle a single orphan section S, placing the orphan into an appropriate |
7322 | output section. The effects of the --orphan-handling command line | |
7323 | option are handled here. */ | |
7324 | ||
7325 | static void | |
7326 | ldlang_place_orphan (asection *s) | |
7327 | { | |
7328 | if (config.orphan_handling == orphan_handling_discard) | |
7329 | { | |
7330 | lang_output_section_statement_type *os; | |
21401fc7 | 7331 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); |
c005eb9e AB |
7332 | if (os->addr_tree == NULL |
7333 | && (bfd_link_relocatable (&link_info) | |
7334 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7335 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7336 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7337 | } |
7338 | else | |
7339 | { | |
7340 | lang_output_section_statement_type *os; | |
7341 | const char *name = s->name; | |
7342 | int constraint = 0; | |
7343 | ||
7344 | if (config.orphan_handling == orphan_handling_error) | |
df5f2391 | 7345 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
7346 | s, s->owner); |
7347 | ||
7348 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
7349 | constraint = SPECIAL; | |
7350 | ||
7351 | os = ldemul_place_orphan (s, name, constraint); | |
7352 | if (os == NULL) | |
7353 | { | |
21401fc7 | 7354 | os = lang_output_section_statement_lookup (name, constraint, 1); |
c005eb9e AB |
7355 | if (os->addr_tree == NULL |
7356 | && (bfd_link_relocatable (&link_info) | |
7357 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7358 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7359 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7360 | } |
7361 | ||
7362 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 7363 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 7364 | "placed in section `%s'\n"), |
c005eb9e AB |
7365 | s, s->owner, os->name); |
7366 | } | |
7367 | } | |
7368 | ||
08da4cac KH |
7369 | /* Run through the input files and ensure that every input section has |
7370 | somewhere to go. If one is found without a destination then create | |
7371 | an input request and place it into the statement tree. */ | |
252b5132 RH |
7372 | |
7373 | static void | |
1579bae1 | 7374 | lang_place_orphans (void) |
252b5132 | 7375 | { |
e50d8076 | 7376 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
7377 | { |
7378 | asection *s; | |
7379 | ||
1579bae1 | 7380 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7381 | { |
1579bae1 | 7382 | if (s->output_section == NULL) |
252b5132 | 7383 | { |
396a2467 | 7384 | /* This section of the file is not attached, root |
afd7a018 | 7385 | around for a sensible place for it to go. */ |
252b5132 | 7386 | |
66be1055 | 7387 | if (file->flags.just_syms) |
1449d79b | 7388 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 7389 | else if (lang_discard_section_p (s)) |
164e712d | 7390 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
7391 | else if (strcmp (s->name, "COMMON") == 0) |
7392 | { | |
7393 | /* This is a lonely common section which must have | |
7394 | come from an archive. We attach to the section | |
7395 | with the wildcard. */ | |
0e1862bb | 7396 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
7397 | || command_line.force_common_definition) |
7398 | { | |
7399 | if (default_common_section == NULL) | |
66c103b7 | 7400 | default_common_section |
21401fc7 | 7401 | = lang_output_section_statement_lookup (".bss", 0, 1); |
39dcfe18 | 7402 | lang_add_section (&default_common_section->children, s, |
b209b5a6 | 7403 | NULL, NULL, default_common_section); |
252b5132 RH |
7404 | } |
7405 | } | |
252b5132 | 7406 | else |
c005eb9e | 7407 | ldlang_place_orphan (s); |
252b5132 RH |
7408 | } |
7409 | } | |
7410 | } | |
7411 | } | |
7412 | ||
252b5132 | 7413 | void |
1579bae1 | 7414 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7415 | { |
aa8804e4 | 7416 | flagword *ptr_flags; |
252b5132 | 7417 | |
aa8804e4 | 7418 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7419 | |
252b5132 RH |
7420 | while (*flags) |
7421 | { | |
7422 | switch (*flags) | |
7423 | { | |
f0673d20 NC |
7424 | /* PR 17900: An exclamation mark in the attributes reverses |
7425 | the sense of any of the attributes that follow. */ | |
7426 | case '!': | |
0aa7f586 | 7427 | invert = !invert; |
f0673d20 NC |
7428 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7429 | break; | |
7430 | ||
252b5132 RH |
7431 | case 'A': case 'a': |
7432 | *ptr_flags |= SEC_ALLOC; | |
7433 | break; | |
7434 | ||
7435 | case 'R': case 'r': | |
7436 | *ptr_flags |= SEC_READONLY; | |
7437 | break; | |
7438 | ||
7439 | case 'W': case 'w': | |
7440 | *ptr_flags |= SEC_DATA; | |
7441 | break; | |
7442 | ||
7443 | case 'X': case 'x': | |
7444 | *ptr_flags |= SEC_CODE; | |
7445 | break; | |
7446 | ||
7447 | case 'L': case 'l': | |
7448 | case 'I': case 'i': | |
7449 | *ptr_flags |= SEC_LOAD; | |
7450 | break; | |
7451 | ||
7452 | default: | |
df5f2391 | 7453 | einfo (_("%F%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7454 | *flags, *flags); |
252b5132 RH |
7455 | break; |
7456 | } | |
7457 | flags++; | |
7458 | } | |
7459 | } | |
7460 | ||
d871d478 AM |
7461 | /* Call a function on each real input file. This function will be |
7462 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7463 | |
7464 | void | |
1579bae1 | 7465 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7466 | { |
7467 | lang_input_statement_type *f; | |
7468 | ||
8ce18f9c | 7469 | for (f = (void *) input_file_chain.head; |
252b5132 | 7470 | f != NULL; |
36983a93 | 7471 | f = f->next_real_file) |
d871d478 AM |
7472 | if (f->flags.real) |
7473 | func (f); | |
252b5132 RH |
7474 | } |
7475 | ||
d871d478 AM |
7476 | /* Call a function on each real file. The function will be called on |
7477 | all the elements of an archive which are included in the link, but | |
7478 | will not be called on the archive file itself. */ | |
252b5132 RH |
7479 | |
7480 | void | |
1579bae1 | 7481 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7482 | { |
e50d8076 | 7483 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7484 | { |
d871d478 AM |
7485 | if (f->flags.real) |
7486 | func (f); | |
252b5132 RH |
7487 | } |
7488 | } | |
7489 | ||
252b5132 | 7490 | void |
1579bae1 | 7491 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7492 | { |
36983a93 | 7493 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7494 | |
7495 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7496 | a link. */ | |
9221725d AM |
7497 | ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next |
7498 | && entry->the_bfd->link.next == NULL); | |
f13a99db | 7499 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7500 | |
7501 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7502 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7503 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
7504 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
7505 | ||
7506 | /* Look through the sections and check for any which should not be | |
7507 | included in the link. We need to do this now, so that we can | |
7508 | notice when the backend linker tries to report multiple | |
7509 | definition errors for symbols which are in sections we aren't | |
7510 | going to link. FIXME: It might be better to entirely ignore | |
7511 | symbols which are defined in sections which are going to be | |
7512 | discarded. This would require modifying the backend linker for | |
7513 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7514 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7515 | ||
1579bae1 | 7516 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7517 | } |
7518 | ||
7519 | void | |
1579bae1 | 7520 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7521 | { |
7522 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7523 | if (!had_output_filename || !from_script) |
252b5132 RH |
7524 | { |
7525 | output_filename = name; | |
f38a2680 | 7526 | had_output_filename = true; |
252b5132 RH |
7527 | } |
7528 | } | |
7529 | ||
aea4bd9d | 7530 | lang_output_section_statement_type * |
1579bae1 AM |
7531 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7532 | etree_type *address_exp, | |
7533 | enum section_type sectype, | |
c212f39d | 7534 | etree_type *sectype_value, |
1579bae1 AM |
7535 | etree_type *align, |
7536 | etree_type *subalign, | |
0841712e | 7537 | etree_type *ebase, |
1eec346e NC |
7538 | int constraint, |
7539 | int align_with_input) | |
252b5132 RH |
7540 | { |
7541 | lang_output_section_statement_type *os; | |
7542 | ||
66c103b7 | 7543 | os = lang_output_section_statement_lookup (output_section_statement_name, |
21401fc7 | 7544 | constraint, 2); |
967928e9 | 7545 | current_section = os; |
252b5132 | 7546 | |
1579bae1 | 7547 | if (os->addr_tree == NULL) |
08da4cac KH |
7548 | { |
7549 | os->addr_tree = address_exp; | |
7550 | } | |
252b5132 | 7551 | os->sectype = sectype; |
c212f39d FS |
7552 | if (sectype == type_section || sectype == typed_readonly_section) |
7553 | os->sectype_value = sectype_value; | |
7554 | else if (sectype == noload_section) | |
252b5132 | 7555 | os->flags = SEC_NEVER_LOAD; |
c212f39d FS |
7556 | else |
7557 | os->flags = SEC_NO_FLAGS; | |
e5caa5e0 | 7558 | os->block_value = 1; |
66c103b7 AM |
7559 | |
7560 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 7561 | push_stat_ptr (&os->children); |
252b5132 | 7562 | |
1eec346e NC |
7563 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7564 | if (os->align_lma_with_input && align != NULL) | |
c1c8c1ef | 7565 | einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7566 | NULL); |
1eec346e | 7567 | |
3d9c8f6b AM |
7568 | os->subsection_alignment = subalign; |
7569 | os->section_alignment = align; | |
252b5132 RH |
7570 | |
7571 | os->load_base = ebase; | |
aea4bd9d | 7572 | return os; |
252b5132 RH |
7573 | } |
7574 | ||
252b5132 | 7575 | void |
1579bae1 | 7576 | lang_final (void) |
252b5132 | 7577 | { |
d3ce72d0 NC |
7578 | lang_output_statement_type *new_stmt; |
7579 | ||
7580 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7581 | new_stmt->name = output_filename; | |
252b5132 RH |
7582 | } |
7583 | ||
08da4cac KH |
7584 | /* Reset the current counters in the regions. */ |
7585 | ||
e3dc8847 | 7586 | void |
1579bae1 | 7587 | lang_reset_memory_regions (void) |
252b5132 RH |
7588 | { |
7589 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7590 | asection *o; |
e9ee469a | 7591 | lang_output_section_statement_type *os; |
252b5132 | 7592 | |
1579bae1 | 7593 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7594 | { |
252b5132 | 7595 | p->current = p->origin; |
66e28d60 | 7596 | p->last_os = NULL; |
252b5132 | 7597 | } |
b3327aad | 7598 | |
8ce18f9c | 7599 | for (os = (void *) lang_os_list.head; |
e9ee469a AM |
7600 | os != NULL; |
7601 | os = os->next) | |
cde9e0be | 7602 | { |
f38a2680 AM |
7603 | os->processed_vma = false; |
7604 | os->processed_lma = false; | |
cde9e0be | 7605 | } |
e9ee469a | 7606 | |
f13a99db | 7607 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7608 | { |
7609 | /* Save the last size for possible use by bfd_relax_section. */ | |
7610 | o->rawsize = o->size; | |
8772de11 MR |
7611 | if (!(o->flags & SEC_FIXED_SIZE)) |
7612 | o->size = 0; | |
1a23a9e6 | 7613 | } |
252b5132 RH |
7614 | } |
7615 | ||
164e712d | 7616 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7617 | |
7618 | static void | |
1579bae1 | 7619 | gc_section_callback (lang_wild_statement_type *ptr, |
b209b5a6 | 7620 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
1579bae1 AM |
7621 | asection *section, |
7622 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7623 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7624 | { |
164e712d AM |
7625 | /* If the wild pattern was marked KEEP, the member sections |
7626 | should be as well. */ | |
4dec4d4e RH |
7627 | if (ptr->keep_sections) |
7628 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7629 | } |
7630 | ||
252b5132 RH |
7631 | /* Iterate over sections marking them against GC. */ |
7632 | ||
7633 | static void | |
1579bae1 | 7634 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7635 | { |
1579bae1 | 7636 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7637 | { |
7638 | switch (s->header.type) | |
7639 | { | |
7640 | case lang_wild_statement_enum: | |
164e712d | 7641 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7642 | break; |
252b5132 RH |
7643 | case lang_constructors_statement_enum: |
7644 | lang_gc_sections_1 (constructor_list.head); | |
7645 | break; | |
7646 | case lang_output_section_statement_enum: | |
7647 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7648 | break; | |
7649 | case lang_group_statement_enum: | |
7650 | lang_gc_sections_1 (s->group_statement.children.head); | |
7651 | break; | |
7652 | default: | |
7653 | break; | |
7654 | } | |
7655 | } | |
7656 | } | |
7657 | ||
7658 | static void | |
1579bae1 | 7659 | lang_gc_sections (void) |
252b5132 | 7660 | { |
252b5132 | 7661 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7662 | lang_gc_sections_1 (statement_list.head); |
7663 | ||
9ca57817 | 7664 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
15407e7e | 7665 | the special case of .stabstr debug info. (See bfd/stabs.c) |
9ca57817 | 7666 | Twiddle the flag here, to simplify later linker code. */ |
0e1862bb | 7667 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7668 | { |
7669 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7670 | { | |
7671 | asection *sec; | |
0381901e | 7672 | #if BFD_SUPPORTS_PLUGINS |
66be1055 | 7673 | if (f->flags.claimed) |
9e2278f5 AM |
7674 | continue; |
7675 | #endif | |
9ca57817 | 7676 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
15407e7e AM |
7677 | if ((sec->flags & SEC_DEBUGGING) == 0 |
7678 | || strcmp (sec->name, ".stabstr") != 0) | |
9ca57817 AM |
7679 | sec->flags &= ~SEC_EXCLUDE; |
7680 | } | |
7681 | } | |
7682 | ||
57316bff | 7683 | if (link_info.gc_sections) |
f13a99db | 7684 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7685 | } |
7686 | ||
b10a8ae0 L |
7687 | /* Worker for lang_find_relro_sections_1. */ |
7688 | ||
7689 | static void | |
7690 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7691 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7692 | asection *section, | |
7693 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7694 | void *data) | |
7695 | { | |
7696 | /* Discarded, excluded and ignored sections effectively have zero | |
7697 | size. */ | |
7698 | if (section->output_section != NULL | |
f13a99db | 7699 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7700 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7701 | && !IGNORE_SECTION (section) | |
7702 | && section->size != 0) | |
7703 | { | |
f38a2680 AM |
7704 | bool *has_relro_section = (bool *) data; |
7705 | *has_relro_section = true; | |
b10a8ae0 L |
7706 | } |
7707 | } | |
7708 | ||
7709 | /* Iterate over sections for relro sections. */ | |
7710 | ||
7711 | static void | |
7712 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
f38a2680 | 7713 | bool *has_relro_section) |
b10a8ae0 L |
7714 | { |
7715 | if (*has_relro_section) | |
7716 | return; | |
7717 | ||
7718 | for (; s != NULL; s = s->header.next) | |
7719 | { | |
2085ee26 | 7720 | if (s == expld.dataseg.relro_end_stat) |
b10a8ae0 L |
7721 | break; |
7722 | ||
7723 | switch (s->header.type) | |
7724 | { | |
7725 | case lang_wild_statement_enum: | |
7726 | walk_wild (&s->wild_statement, | |
7727 | find_relro_section_callback, | |
7728 | has_relro_section); | |
7729 | break; | |
7730 | case lang_constructors_statement_enum: | |
7731 | lang_find_relro_sections_1 (constructor_list.head, | |
2085ee26 | 7732 | has_relro_section); |
b10a8ae0 L |
7733 | break; |
7734 | case lang_output_section_statement_enum: | |
7735 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
2085ee26 | 7736 | has_relro_section); |
b10a8ae0 L |
7737 | break; |
7738 | case lang_group_statement_enum: | |
7739 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
2085ee26 | 7740 | has_relro_section); |
b10a8ae0 L |
7741 | break; |
7742 | default: | |
7743 | break; | |
7744 | } | |
7745 | } | |
7746 | } | |
7747 | ||
7748 | static void | |
7749 | lang_find_relro_sections (void) | |
7750 | { | |
f38a2680 | 7751 | bool has_relro_section = false; |
b10a8ae0 L |
7752 | |
7753 | /* Check all sections in the link script. */ | |
7754 | ||
7755 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
2085ee26 | 7756 | &has_relro_section); |
b10a8ae0 L |
7757 | |
7758 | if (!has_relro_section) | |
f38a2680 | 7759 | link_info.relro = false; |
b10a8ae0 L |
7760 | } |
7761 | ||
fbbc3759 L |
7762 | /* Relax all sections until bfd_relax_section gives up. */ |
7763 | ||
eaeb0a9d | 7764 | void |
f38a2680 | 7765 | lang_relax_sections (bool need_layout) |
fbbc3759 | 7766 | { |
6a91be86 L |
7767 | /* NB: Also enable relaxation to layout sections for DT_RELR. */ |
7768 | if (RELAXATION_ENABLED || link_info.enable_dt_relr) | |
fbbc3759 | 7769 | { |
eaeb0a9d AM |
7770 | /* We may need more than one relaxation pass. */ |
7771 | int i = link_info.relax_pass; | |
fbbc3759 | 7772 | |
eaeb0a9d AM |
7773 | /* The backend can use it to determine the current pass. */ |
7774 | link_info.relax_pass = 0; | |
fbbc3759 | 7775 | |
eaeb0a9d AM |
7776 | while (i--) |
7777 | { | |
7778 | /* Keep relaxing until bfd_relax_section gives up. */ | |
f38a2680 | 7779 | bool relax_again; |
fbbc3759 | 7780 | |
eaeb0a9d AM |
7781 | link_info.relax_trip = -1; |
7782 | do | |
7783 | { | |
7784 | link_info.relax_trip++; | |
7785 | ||
7786 | /* Note: pe-dll.c does something like this also. If you find | |
7787 | you need to change this code, you probably need to change | |
7788 | pe-dll.c also. DJ */ | |
7789 | ||
7790 | /* Do all the assignments with our current guesses as to | |
7791 | section sizes. */ | |
2f65ac72 | 7792 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7793 | |
7794 | /* We must do this after lang_do_assignments, because it uses | |
7795 | size. */ | |
7796 | lang_reset_memory_regions (); | |
7797 | ||
7798 | /* Perform another relax pass - this time we know where the | |
7799 | globals are, so can make a better guess. */ | |
f38a2680 AM |
7800 | relax_again = false; |
7801 | lang_size_sections (&relax_again, false); | |
eaeb0a9d AM |
7802 | } |
7803 | while (relax_again); | |
7804 | ||
7805 | link_info.relax_pass++; | |
7806 | } | |
f38a2680 | 7807 | need_layout = true; |
eaeb0a9d | 7808 | } |
fbbc3759 | 7809 | |
eaeb0a9d AM |
7810 | if (need_layout) |
7811 | { | |
7812 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 7813 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d | 7814 | lang_reset_memory_regions (); |
f38a2680 | 7815 | lang_size_sections (NULL, true); |
fbbc3759 | 7816 | } |
fbbc3759 L |
7817 | } |
7818 | ||
0381901e | 7819 | #if BFD_SUPPORTS_PLUGINS |
8543fde5 DK |
7820 | /* Find the insert point for the plugin's replacement files. We |
7821 | place them after the first claimed real object file, or if the | |
7822 | first claimed object is an archive member, after the last real | |
7823 | object file immediately preceding the archive. In the event | |
7824 | no objects have been claimed at all, we return the first dummy | |
7825 | object file on the list as the insert point; that works, but | |
7826 | the callee must be careful when relinking the file_chain as it | |
7827 | is not actually on that chain, only the statement_list and the | |
7828 | input_file list; in that case, the replacement files must be | |
7829 | inserted at the head of the file_chain. */ | |
7830 | ||
7831 | static lang_input_statement_type * | |
f38a2680 | 7832 | find_replacements_insert_point (bool *before) |
8543fde5 DK |
7833 | { |
7834 | lang_input_statement_type *claim1, *lastobject; | |
8ce18f9c AM |
7835 | lastobject = (void *) input_file_chain.head; |
7836 | for (claim1 = (void *) file_chain.head; | |
8543fde5 | 7837 | claim1 != NULL; |
36983a93 | 7838 | claim1 = claim1->next) |
8543fde5 | 7839 | { |
66be1055 | 7840 | if (claim1->flags.claimed) |
128bf1fe AM |
7841 | { |
7842 | *before = claim1->flags.claim_archive; | |
7843 | return claim1->flags.claim_archive ? lastobject : claim1; | |
7844 | } | |
8543fde5 | 7845 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 7846 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
7847 | lastobject = claim1; |
7848 | } | |
7849 | /* No files were claimed by the plugin. Choose the last object | |
7850 | file found on the list (maybe the first, dummy entry) as the | |
7851 | insert point. */ | |
f38a2680 | 7852 | *before = false; |
8543fde5 DK |
7853 | return lastobject; |
7854 | } | |
9e2278f5 | 7855 | |
1fa4ec6a AM |
7856 | /* Find where to insert ADD, an archive element or shared library |
7857 | added during a rescan. */ | |
7858 | ||
36983a93 | 7859 | static lang_input_statement_type ** |
1fa4ec6a AM |
7860 | find_rescan_insertion (lang_input_statement_type *add) |
7861 | { | |
7862 | bfd *add_bfd = add->the_bfd; | |
7863 | lang_input_statement_type *f; | |
7864 | lang_input_statement_type *last_loaded = NULL; | |
7865 | lang_input_statement_type *before = NULL; | |
36983a93 | 7866 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
7867 | |
7868 | if (add_bfd->my_archive != NULL) | |
7869 | add_bfd = add_bfd->my_archive; | |
7870 | ||
7871 | /* First look through the input file chain, to find an object file | |
7872 | before the one we've rescanned. Normal object files always | |
7873 | appear on both the input file chain and the file chain, so this | |
7874 | lets us get quickly to somewhere near the correct place on the | |
7875 | file chain if it is full of archive elements. Archives don't | |
7876 | appear on the file chain, but if an element has been extracted | |
7877 | then their input_statement->next points at it. */ | |
8ce18f9c | 7878 | for (f = (void *) input_file_chain.head; |
1fa4ec6a | 7879 | f != NULL; |
36983a93 | 7880 | f = f->next_real_file) |
1fa4ec6a AM |
7881 | { |
7882 | if (f->the_bfd == add_bfd) | |
7883 | { | |
7884 | before = last_loaded; | |
7885 | if (f->next != NULL) | |
36983a93 | 7886 | return &f->next->next; |
1fa4ec6a AM |
7887 | } |
7888 | if (f->the_bfd != NULL && f->next != NULL) | |
7889 | last_loaded = f; | |
7890 | } | |
7891 | ||
7892 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
7893 | *iter != NULL; | |
36983a93 AM |
7894 | iter = &(*iter)->next) |
7895 | if (!(*iter)->flags.claim_archive | |
7896 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
7897 | break; |
7898 | ||
7899 | return iter; | |
7900 | } | |
7901 | ||
9e2278f5 AM |
7902 | /* Insert SRCLIST into DESTLIST after given element by chaining |
7903 | on FIELD as the next-pointer. (Counterintuitively does not need | |
7904 | a pointer to the actual after-node itself, just its chain field.) */ | |
7905 | ||
7906 | static void | |
7907 | lang_list_insert_after (lang_statement_list_type *destlist, | |
7908 | lang_statement_list_type *srclist, | |
7909 | lang_statement_union_type **field) | |
7910 | { | |
7911 | *(srclist->tail) = *field; | |
7912 | *field = srclist->head; | |
7913 | if (destlist->tail == field) | |
7914 | destlist->tail = srclist->tail; | |
7915 | } | |
7916 | ||
7917 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
7918 | was taken as a copy of it and leave them in ORIGLIST. */ | |
7919 | ||
7920 | static void | |
7921 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
7922 | lang_statement_list_type *origlist) | |
7923 | { | |
7924 | union lang_statement_union **savetail; | |
7925 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
7926 | ASSERT (origlist->head == destlist->head); | |
7927 | savetail = origlist->tail; | |
7928 | origlist->head = *(savetail); | |
7929 | origlist->tail = destlist->tail; | |
7930 | destlist->tail = savetail; | |
7931 | *savetail = NULL; | |
7932 | } | |
128bf1fe AM |
7933 | |
7934 | static lang_statement_union_type ** | |
7935 | find_next_input_statement (lang_statement_union_type **s) | |
7936 | { | |
7937 | for ( ; *s; s = &(*s)->header.next) | |
7938 | { | |
7939 | lang_statement_union_type **t; | |
7940 | switch ((*s)->header.type) | |
7941 | { | |
7942 | case lang_input_statement_enum: | |
7943 | return s; | |
7944 | case lang_wild_statement_enum: | |
7945 | t = &(*s)->wild_statement.children.head; | |
7946 | break; | |
7947 | case lang_group_statement_enum: | |
7948 | t = &(*s)->group_statement.children.head; | |
7949 | break; | |
7950 | case lang_output_section_statement_enum: | |
7951 | t = &(*s)->output_section_statement.children.head; | |
7952 | break; | |
7953 | default: | |
7954 | continue; | |
7955 | } | |
7956 | t = find_next_input_statement (t); | |
7957 | if (*t) | |
7958 | return t; | |
7959 | } | |
7960 | return s; | |
7961 | } | |
0381901e | 7962 | #endif /* BFD_SUPPORTS_PLUGINS */ |
8543fde5 | 7963 | |
4153b6db NC |
7964 | /* Add NAME to the list of garbage collection entry points. */ |
7965 | ||
7966 | void | |
0aa7f586 | 7967 | lang_add_gc_name (const char *name) |
4153b6db NC |
7968 | { |
7969 | struct bfd_sym_chain *sym; | |
7970 | ||
7971 | if (name == NULL) | |
7972 | return; | |
7973 | ||
988de25b | 7974 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
7975 | |
7976 | sym->next = link_info.gc_sym_list; | |
7977 | sym->name = name; | |
7978 | link_info.gc_sym_list = sym; | |
7979 | } | |
7980 | ||
fbf05aa7 L |
7981 | /* Check relocations. */ |
7982 | ||
7983 | static void | |
7984 | lang_check_relocs (void) | |
7985 | { | |
4f3b23b3 | 7986 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
7987 | { |
7988 | bfd *abfd; | |
7989 | ||
7990 | for (abfd = link_info.input_bfds; | |
7991 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 7992 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 7993 | { |
4f3b23b3 | 7994 | /* No object output, fail return. */ |
f38a2680 | 7995 | config.make_executable = false; |
4f3b23b3 NC |
7996 | /* Note: we do not abort the loop, but rather |
7997 | continue the scan in case there are other | |
7998 | bad relocations to report. */ | |
fbf05aa7 L |
7999 | } |
8000 | } | |
8001 | } | |
8002 | ||
77f5e65e AB |
8003 | /* Look through all output sections looking for places where we can |
8004 | propagate forward the lma region. */ | |
8005 | ||
8006 | static void | |
8007 | lang_propagate_lma_regions (void) | |
8008 | { | |
8009 | lang_output_section_statement_type *os; | |
8010 | ||
8ce18f9c | 8011 | for (os = (void *) lang_os_list.head; |
77f5e65e AB |
8012 | os != NULL; |
8013 | os = os->next) | |
8014 | { | |
8015 | if (os->prev != NULL | |
8016 | && os->lma_region == NULL | |
8017 | && os->load_base == NULL | |
8018 | && os->addr_tree == NULL | |
8019 | && os->region == os->prev->region) | |
8020 | os->lma_region = os->prev->lma_region; | |
8021 | } | |
8022 | } | |
8023 | ||
c63d4862 AM |
8024 | static void |
8025 | warn_non_contiguous_discards (void) | |
8026 | { | |
8027 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
8028 | { | |
8029 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
8030 | || file->flags.just_syms) | |
8031 | continue; | |
8032 | ||
8033 | for (asection *s = file->the_bfd->sections; s != NULL; s = s->next) | |
8034 | if (s->output_section == NULL | |
8035 | && (s->flags & SEC_LINKER_CREATED) == 0) | |
8036 | einfo (_("%P: warning: --enable-non-contiguous-regions " | |
8037 | "discards section `%pA' from `%pB'\n"), | |
8038 | s, file->the_bfd); | |
8039 | } | |
8040 | } | |
8041 | ||
50be5d11 MM |
8042 | static void |
8043 | reset_one_wild (lang_statement_union_type *statement) | |
8044 | { | |
8045 | if (statement->header.type == lang_wild_statement_enum) | |
8046 | { | |
8047 | lang_wild_statement_type *stmt = &statement->wild_statement; | |
50be5d11 MM |
8048 | lang_list_init (&stmt->matching_sections); |
8049 | } | |
8050 | } | |
8051 | ||
8052 | static void | |
8053 | reset_resolved_wilds (void) | |
8054 | { | |
8055 | lang_for_each_statement (reset_one_wild); | |
8056 | } | |
8057 | ||
252b5132 | 8058 | void |
1579bae1 | 8059 | lang_process (void) |
252b5132 | 8060 | { |
55255dae | 8061 | /* Finalize dynamic list. */ |
40b36307 L |
8062 | if (link_info.dynamic_list) |
8063 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 8064 | |
252b5132 RH |
8065 | current_target = default_target; |
8066 | ||
08da4cac KH |
8067 | /* Open the output file. */ |
8068 | lang_for_each_statement (ldlang_open_output); | |
61826503 | 8069 | init_opb (NULL); |
252b5132 RH |
8070 | |
8071 | ldemul_create_output_section_statements (); | |
8072 | ||
08da4cac | 8073 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
8074 | lang_place_undefineds (); |
8075 | ||
082b7297 | 8076 | if (!bfd_section_already_linked_table_init ()) |
df5f2391 | 8077 | einfo (_("%F%P: can not create hash table: %E\n")); |
9503fd87 | 8078 | |
874ef038 AB |
8079 | /* A first pass through the memory regions ensures that if any region |
8080 | references a symbol for its origin or length then this symbol will be | |
8081 | added to the symbol table. Having these symbols in the symbol table | |
8082 | means that when we call open_input_bfds PROVIDE statements will | |
8083 | trigger to provide any needed symbols. The regions origins and | |
8084 | lengths are not assigned as a result of this call. */ | |
f38a2680 | 8085 | lang_do_memory_regions (false); |
874ef038 | 8086 | |
08da4cac | 8087 | /* Create a bfd for each input file. */ |
252b5132 | 8088 | current_target = default_target; |
165f707a | 8089 | lang_statement_iteration++; |
486329aa | 8090 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
874ef038 AB |
8091 | |
8092 | /* Now that open_input_bfds has processed assignments and provide | |
8093 | statements we can give values to symbolic origin/length now. */ | |
f38a2680 | 8094 | lang_do_memory_regions (true); |
252b5132 | 8095 | |
2605f35c AM |
8096 | ldemul_before_plugin_all_symbols_read (); |
8097 | ||
0381901e | 8098 | #if BFD_SUPPORTS_PLUGINS |
716db898 | 8099 | if (link_info.lto_plugin_active) |
f84854b6 | 8100 | { |
8543fde5 DK |
8101 | lang_statement_list_type added; |
8102 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 8103 | |
f84854b6 L |
8104 | /* Now all files are read, let the plugin(s) decide if there |
8105 | are any more to be added to the link before we call the | |
8543fde5 DK |
8106 | emulation's after_open hook. We create a private list of |
8107 | input statements for this purpose, which we will eventually | |
370dfff4 | 8108 | insert into the global statement list after the first claimed |
8543fde5 DK |
8109 | file. */ |
8110 | added = *stat_ptr; | |
8111 | /* We need to manipulate all three chains in synchrony. */ | |
8112 | files = file_chain; | |
8113 | inputfiles = input_file_chain; | |
f84854b6 | 8114 | if (plugin_call_all_symbols_read ()) |
df5f2391 | 8115 | einfo (_("%F%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 8116 | plugin_error_plugin ()); |
f38a2680 | 8117 | link_info.lto_all_symbols_read = true; |
8543fde5 | 8118 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 8119 | plugin_undefs = link_info.hash->undefs_tail; |
cdaa438c | 8120 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
b02c4f16 AM |
8121 | if (plugin_undefs == link_info.hash->undefs_tail) |
8122 | plugin_undefs = NULL; | |
8543fde5 DK |
8123 | /* Restore the global list pointer now they have all been added. */ |
8124 | lang_list_remove_tail (stat_ptr, &added); | |
8125 | /* And detach the fresh ends of the file lists. */ | |
8126 | lang_list_remove_tail (&file_chain, &files); | |
8127 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
8128 | /* Were any new files added? */ | |
8129 | if (added.head != NULL) | |
8130 | { | |
8131 | /* If so, we will insert them into the statement list immediately | |
128bf1fe AM |
8132 | after the first input file that was claimed by the plugin, |
8133 | unless that file was an archive in which case it is inserted | |
8134 | immediately before. */ | |
f38a2680 | 8135 | bool before; |
128bf1fe AM |
8136 | lang_statement_union_type **prev; |
8137 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
8138 | /* If a plugin adds input files without having claimed any, we |
8139 | don't really have a good idea where to place them. Just putting | |
8140 | them at the start or end of the list is liable to leave them | |
8141 | outside the crtbegin...crtend range. */ | |
9e2278f5 AM |
8142 | ASSERT (plugin_insert != NULL); |
8143 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
8144 | prev = &plugin_insert->header.next; |
8145 | if (before) | |
8146 | { | |
8147 | prev = find_next_input_statement (prev); | |
36983a93 | 8148 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 8149 | { |
a19826f4 | 8150 | /* We didn't find the expected input statement. |
1f1f5b92 | 8151 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
8152 | prev = &plugin_insert->header.next; |
8153 | } | |
8154 | } | |
8155 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
8156 | /* Likewise for the file chains. */ |
8157 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 8158 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
8159 | /* We must be careful when relinking file_chain; we may need to |
8160 | insert the new files at the head of the list if the insert | |
8161 | point chosen is the dummy first input file. */ | |
9e2278f5 | 8162 | if (plugin_insert->filename) |
36983a93 AM |
8163 | lang_list_insert_after (&file_chain, &files, |
8164 | (void *) &plugin_insert->next); | |
8543fde5 DK |
8165 | else |
8166 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
8167 | |
8168 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 8169 | files = file_chain; |
165f707a | 8170 | lang_statement_iteration++; |
9e2278f5 | 8171 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
8172 | lang_list_remove_tail (&file_chain, &files); |
8173 | while (files.head != NULL) | |
8174 | { | |
36983a93 AM |
8175 | lang_input_statement_type **insert; |
8176 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
8177 | bfd *my_arch; |
8178 | ||
8179 | insert = find_rescan_insertion (&files.head->input_statement); | |
8180 | /* All elements from an archive can be added at once. */ | |
8181 | iter = &files.head->input_statement.next; | |
8182 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
8183 | if (my_arch != NULL) | |
36983a93 AM |
8184 | for (; *iter != NULL; iter = &(*iter)->next) |
8185 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
8186 | break; |
8187 | temp = *insert; | |
36983a93 AM |
8188 | *insert = &files.head->input_statement; |
8189 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a | 8190 | *iter = temp; |
081609f5 AM |
8191 | if (file_chain.tail == (lang_statement_union_type **) insert) |
8192 | file_chain.tail = (lang_statement_union_type **) iter; | |
1fa4ec6a AM |
8193 | if (my_arch != NULL) |
8194 | { | |
00f93c44 | 8195 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 8196 | if (parent != NULL) |
36983a93 | 8197 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
8198 | ((char *) iter |
8199 | - offsetof (lang_input_statement_type, next)); | |
8200 | } | |
8201 | } | |
8543fde5 | 8202 | } |
f84854b6 | 8203 | } |
0381901e | 8204 | #endif /* BFD_SUPPORTS_PLUGINS */ |
5d3236ee | 8205 | |
2605f35c AM |
8206 | struct bfd_sym_chain **sym = &link_info.gc_sym_list; |
8207 | while (*sym) | |
8208 | sym = &(*sym)->next; | |
4153b6db | 8209 | |
2605f35c | 8210 | *sym = &entry_symbol; |
41f46ed9 | 8211 | |
e3e942e9 | 8212 | if (entry_symbol.name == NULL) |
41f46ed9 | 8213 | { |
2605f35c | 8214 | *sym = ldlang_undef_chain_list_head; |
41f46ed9 | 8215 | |
2605f35c | 8216 | /* entry_symbol is normally initialised by an ENTRY definition in the |
41f46ed9 SKS |
8217 | linker script or the -e command line option. But if neither of |
8218 | these have been used, the target specific backend may still have | |
4153b6db | 8219 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
8220 | Unfortunately this value will not be processed until lang_end() |
8221 | is called, long after this function has finished. So detect this | |
8222 | case here and add the target's entry symbol to the list of starting | |
8223 | points for garbage collection resolution. */ | |
4153b6db | 8224 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
8225 | } |
8226 | ||
4153b6db NC |
8227 | lang_add_gc_name (link_info.init_function); |
8228 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 8229 | |
252b5132 | 8230 | ldemul_after_open (); |
16e4ecc0 AM |
8231 | if (config.map_file != NULL) |
8232 | lang_print_asneeded (); | |
252b5132 | 8233 | |
1ff6de03 NA |
8234 | ldlang_open_ctf (); |
8235 | ||
082b7297 | 8236 | bfd_section_already_linked_table_free (); |
9503fd87 | 8237 | |
252b5132 RH |
8238 | /* Make sure that we're not mixing architectures. We call this |
8239 | after all the input files have been opened, but before we do any | |
8240 | other processing, so that any operations merge_private_bfd_data | |
8241 | does on the output file will be known during the rest of the | |
8242 | link. */ | |
8243 | lang_check (); | |
8244 | ||
8245 | /* Handle .exports instead of a version script if we're told to do so. */ | |
8246 | if (command_line.version_exports_section) | |
8247 | lang_do_version_exports_section (); | |
8248 | ||
8249 | /* Build all sets based on the information gathered from the input | |
8250 | files. */ | |
8251 | ldctor_build_sets (); | |
8252 | ||
97da0e26 | 8253 | lang_symbol_tweaks (); |
7dba9362 | 8254 | |
2aa9aad9 NC |
8255 | /* PR 13683: We must rerun the assignments prior to running garbage |
8256 | collection in order to make sure that all symbol aliases are resolved. */ | |
8257 | lang_do_assignments (lang_mark_phase_enum); | |
8258 | expld.phase = lang_first_phase_enum; | |
8259 | ||
08da4cac | 8260 | /* Size up the common data. */ |
252b5132 RH |
8261 | lang_common (); |
8262 | ||
b1eecf6f MM |
8263 | if (0) |
8264 | debug_prefix_tree (); | |
8265 | ||
8266 | resolve_wilds (); | |
8267 | ||
c4621b33 AM |
8268 | /* Remove unreferenced sections if asked to. */ |
8269 | lang_gc_sections (); | |
8270 | ||
5789f845 AM |
8271 | lang_mark_undefineds (); |
8272 | ||
fbf05aa7 L |
8273 | /* Check relocations. */ |
8274 | lang_check_relocs (); | |
8275 | ||
5c3261b0 L |
8276 | ldemul_after_check_relocs (); |
8277 | ||
50be5d11 MM |
8278 | /* There might have been new sections created (e.g. as result of |
8279 | checking relocs to need a .got, or suchlike), so to properly order | |
8280 | them into our lists of matching sections reset them here. */ | |
8281 | reset_resolved_wilds (); | |
b1eecf6f | 8282 | resolve_wilds (); |
50be5d11 | 8283 | |
af31506c MM |
8284 | /* Update wild statements in case the user gave --sort-section. |
8285 | Note how the option might have come after the linker script and | |
8286 | so couldn't have been set when the wild statements were created. */ | |
bcaa7b3e L |
8287 | update_wild_statements (statement_list.head); |
8288 | ||
252b5132 | 8289 | /* Run through the contours of the script and attach input sections |
08da4cac | 8290 | to the correct output sections. */ |
e759c116 | 8291 | lang_statement_iteration++; |
1579bae1 | 8292 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 8293 | |
776ab89f AM |
8294 | /* Start at the statement immediately after the special abs_section |
8295 | output statement, so that it isn't reordered. */ | |
8296 | process_insert_statements (&lang_os_list.head->header.next); | |
53d25da6 | 8297 | |
9b538ba7 L |
8298 | ldemul_before_place_orphans (); |
8299 | ||
08da4cac | 8300 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
8301 | lang_place_orphans (); |
8302 | ||
0e1862bb | 8303 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 8304 | { |
57ceae94 AM |
8305 | asection *found; |
8306 | ||
8307 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
8308 | sections, so that GCed sections are not merged, but before | |
8309 | assigning dynamic symbols, since removing whole input sections | |
8310 | is hard then. */ | |
f13a99db | 8311 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 8312 | |
862120bd | 8313 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 8314 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 8315 | |
1579bae1 | 8316 | if (found != NULL) |
862120bd AM |
8317 | { |
8318 | if (config.text_read_only) | |
8319 | found->flags |= SEC_READONLY; | |
8320 | else | |
8321 | found->flags &= ~SEC_READONLY; | |
8322 | } | |
8323 | } | |
8324 | ||
1ff6de03 NA |
8325 | /* Merge together CTF sections. After this, only the symtab-dependent |
8326 | function and data object sections need adjustment. */ | |
8327 | lang_merge_ctf (); | |
8328 | ||
8329 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
8330 | examining things laid out late, like the strtab. */ | |
8331 | lang_write_ctf (0); | |
8332 | ||
77f5e65e AB |
8333 | /* Copy forward lma regions for output sections in same lma region. */ |
8334 | lang_propagate_lma_regions (); | |
8335 | ||
7dba9362 AM |
8336 | /* Defining __start/__stop symbols early for --gc-sections to work |
8337 | around a glibc build problem can result in these symbols being | |
8338 | defined when they should not be. Fix them now. */ | |
8339 | if (config.build_constructors) | |
8340 | lang_undef_start_stop (); | |
8341 | ||
8342 | /* Define .startof./.sizeof. symbols with preliminary values before | |
8343 | dynamic symbols are created. */ | |
8344 | if (!bfd_link_relocatable (&link_info)) | |
8345 | lang_init_startof_sizeof (); | |
8346 | ||
862120bd AM |
8347 | /* Do anything special before sizing sections. This is where ELF |
8348 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
8349 | ldemul_before_allocation (); |
8350 | ||
8351 | /* We must record the program headers before we try to fix the | |
8352 | section positions, since they will affect SIZEOF_HEADERS. */ | |
8353 | lang_record_phdrs (); | |
8354 | ||
b10a8ae0 | 8355 | /* Check relro sections. */ |
0e1862bb | 8356 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
8357 | lang_find_relro_sections (); |
8358 | ||
b3327aad | 8359 | /* Size up the sections. */ |
0aa7f586 | 8360 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 8361 | |
252b5132 | 8362 | /* See if anything special should be done now we know how big |
eaeb0a9d | 8363 | everything is. This is where relaxation is done. */ |
252b5132 RH |
8364 | ldemul_after_allocation (); |
8365 | ||
7dba9362 AM |
8366 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
8367 | lang_finalize_start_stop (); | |
252b5132 | 8368 | |
7f0cfc6e AM |
8369 | /* Do all the assignments again, to report errors. Assignment |
8370 | statements are processed multiple times, updating symbols; In | |
8371 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
8372 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
8373 | also updated in ldemul_after_allocation. */ | |
2f65ac72 | 8374 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 8375 | |
8ded5a0f AM |
8376 | ldemul_finish (); |
8377 | ||
975f8a9e AM |
8378 | /* Convert absolute symbols to section relative. */ |
8379 | ldexp_finalize_syms (); | |
8380 | ||
252b5132 | 8381 | /* Make sure that the section addresses make sense. */ |
662ef24b | 8382 | if (command_line.check_section_addresses) |
252b5132 | 8383 | lang_check_section_addresses (); |
5f992e62 | 8384 | |
c63d4862 AM |
8385 | if (link_info.non_contiguous_regions |
8386 | && link_info.non_contiguous_regions_warnings) | |
8387 | warn_non_contiguous_discards (); | |
8388 | ||
0a618243 AB |
8389 | /* Check any required symbols are known. */ |
8390 | ldlang_check_require_defined_symbols (); | |
8391 | ||
750877ba | 8392 | lang_end (); |
252b5132 RH |
8393 | } |
8394 | ||
2d5783fa NC |
8395 | void |
8396 | lang_add_version_string (void) | |
8397 | { | |
8398 | if (! enable_linker_version) | |
8399 | return; | |
8400 | ||
8401 | const char * str = "GNU ld "; | |
8402 | int len = strlen (str); | |
8403 | int i; | |
8404 | ||
8405 | for (i = 0 ; i < len ; i++) | |
8406 | lang_add_data (BYTE, exp_intop (str[i])); | |
8407 | ||
8408 | str = BFD_VERSION_STRING; | |
8409 | len = strlen (str); | |
8410 | ||
8411 | for (i = 0 ; i < len ; i++) | |
8412 | lang_add_data (BYTE, exp_intop (str[i])); | |
8413 | ||
8414 | lang_add_data (BYTE, exp_intop ('\0')); | |
8415 | } | |
8416 | ||
252b5132 RH |
8417 | /* EXPORTED TO YACC */ |
8418 | ||
8419 | void | |
1579bae1 AM |
8420 | lang_add_wild (struct wildcard_spec *filespec, |
8421 | struct wildcard_list *section_list, | |
f38a2680 | 8422 | bool keep_sections) |
252b5132 | 8423 | { |
b6bf44ba | 8424 | struct wildcard_list *curr, *next; |
d3ce72d0 | 8425 | lang_wild_statement_type *new_stmt; |
af31506c | 8426 | bool any_specs_sorted = false; |
b6bf44ba AM |
8427 | |
8428 | /* Reverse the list as the parser puts it back to front. */ | |
8429 | for (curr = section_list, section_list = NULL; | |
8430 | curr != NULL; | |
8431 | section_list = curr, curr = next) | |
8432 | { | |
af31506c MM |
8433 | if (curr->spec.sorted != none && curr->spec.sorted != by_none) |
8434 | any_specs_sorted = true; | |
b6bf44ba AM |
8435 | next = curr->next; |
8436 | curr->next = section_list; | |
8437 | } | |
8438 | ||
8439 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 8440 | { |
b6bf44ba AM |
8441 | if (strcmp (filespec->name, "*") == 0) |
8442 | filespec->name = NULL; | |
0aa7f586 | 8443 | else if (!wildcardp (filespec->name)) |
f38a2680 | 8444 | lang_has_input_file = true; |
252b5132 | 8445 | } |
b6bf44ba | 8446 | |
d3ce72d0 NC |
8447 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
8448 | new_stmt->filename = NULL; | |
f38a2680 | 8449 | new_stmt->filenames_sorted = false; |
af31506c | 8450 | new_stmt->any_specs_sorted = any_specs_sorted; |
ae17ab41 | 8451 | new_stmt->section_flag_list = NULL; |
8f1732fc | 8452 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 8453 | if (filespec != NULL) |
252b5132 | 8454 | { |
d3ce72d0 NC |
8455 | new_stmt->filename = filespec->name; |
8456 | new_stmt->filenames_sorted = filespec->sorted == by_name; | |
ae17ab41 | 8457 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 8458 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 8459 | } |
d3ce72d0 NC |
8460 | new_stmt->section_list = section_list; |
8461 | new_stmt->keep_sections = keep_sections; | |
8462 | lang_list_init (&new_stmt->children); | |
43ae96e9 | 8463 | lang_list_init (&new_stmt->matching_sections); |
d3ce72d0 | 8464 | analyze_walk_wild_section_handler (new_stmt); |
b1eecf6f MM |
8465 | if (0) |
8466 | { | |
8467 | printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); | |
8468 | for (curr = new_stmt->section_list; curr; curr = curr->next) | |
8469 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
8470 | printf (")\n"); | |
8471 | } | |
252b5132 RH |
8472 | } |
8473 | ||
8474 | void | |
ba916c8a MM |
8475 | lang_section_start (const char *name, etree_type *address, |
8476 | const segment_type *segment) | |
252b5132 | 8477 | { |
d1778b88 | 8478 | lang_address_statement_type *ad; |
252b5132 | 8479 | |
d1778b88 | 8480 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
8481 | ad->section_name = name; |
8482 | ad->address = address; | |
ba916c8a | 8483 | ad->segment = segment; |
252b5132 RH |
8484 | } |
8485 | ||
8486 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
8487 | because of a -e argument on the command line, or zero if this is | |
8488 | called by ENTRY in a linker script. Command line arguments take | |
8489 | precedence. */ | |
8490 | ||
8491 | void | |
f38a2680 | 8492 | lang_add_entry (const char *name, bool cmdline) |
252b5132 | 8493 | { |
e3e942e9 | 8494 | if (entry_symbol.name == NULL |
252b5132 | 8495 | || cmdline |
0aa7f586 | 8496 | || !entry_from_cmdline) |
252b5132 | 8497 | { |
e3e942e9 | 8498 | entry_symbol.name = name; |
252b5132 RH |
8499 | entry_from_cmdline = cmdline; |
8500 | } | |
8501 | } | |
8502 | ||
a359509e ZW |
8503 | /* Set the default start symbol to NAME. .em files should use this, |
8504 | not lang_add_entry, to override the use of "start" if neither the | |
8505 | linker script nor the command line specifies an entry point. NAME | |
8506 | must be permanently allocated. */ | |
8507 | void | |
8508 | lang_default_entry (const char *name) | |
8509 | { | |
8510 | entry_symbol_default = name; | |
8511 | } | |
8512 | ||
252b5132 | 8513 | void |
1579bae1 | 8514 | lang_add_target (const char *name) |
252b5132 | 8515 | { |
d3ce72d0 | 8516 | lang_target_statement_type *new_stmt; |
252b5132 | 8517 | |
d3ce72d0 NC |
8518 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8519 | new_stmt->target = name; | |
252b5132 RH |
8520 | } |
8521 | ||
8522 | void | |
1579bae1 | 8523 | lang_add_map (const char *name) |
252b5132 RH |
8524 | { |
8525 | while (*name) | |
8526 | { | |
8527 | switch (*name) | |
8528 | { | |
08da4cac | 8529 | case 'F': |
f38a2680 | 8530 | map_option_f = true; |
252b5132 RH |
8531 | break; |
8532 | } | |
8533 | name++; | |
8534 | } | |
8535 | } | |
8536 | ||
8537 | void | |
1579bae1 | 8538 | lang_add_fill (fill_type *fill) |
252b5132 | 8539 | { |
d3ce72d0 | 8540 | lang_fill_statement_type *new_stmt; |
252b5132 | 8541 | |
d3ce72d0 NC |
8542 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8543 | new_stmt->fill = fill; | |
252b5132 RH |
8544 | } |
8545 | ||
8546 | void | |
1579bae1 | 8547 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8548 | { |
d3ce72d0 | 8549 | lang_data_statement_type *new_stmt; |
252b5132 | 8550 | |
d3ce72d0 NC |
8551 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8552 | new_stmt->exp = exp; | |
8553 | new_stmt->type = type; | |
252b5132 RH |
8554 | } |
8555 | ||
a4d5aec7 AM |
8556 | void |
8557 | lang_add_string (const char *s) | |
0d79a2a8 | 8558 | { |
a4d5aec7 AM |
8559 | bfd_vma len = strlen (s); |
8560 | bfd_vma i; | |
8561 | bool escape = false; | |
0d79a2a8 | 8562 | |
a4d5aec7 AM |
8563 | /* Add byte expressions until end of string. */ |
8564 | for (i = 0 ; i < len; i++) | |
0d79a2a8 US |
8565 | { |
8566 | char c = *s++; | |
8567 | ||
8568 | if (escape) | |
8569 | { | |
8570 | switch (c) | |
8571 | { | |
8572 | default: | |
8573 | /* Ignore the escape. */ | |
8574 | break; | |
8575 | ||
8576 | case 'n': c = '\n'; break; | |
8577 | case 'r': c = '\r'; break; | |
8578 | case 't': c = '\t'; break; | |
8579 | ||
8580 | case '0': | |
8581 | case '1': | |
8582 | case '2': | |
8583 | case '3': | |
8584 | case '4': | |
8585 | case '5': | |
8586 | case '6': | |
8587 | case '7': | |
8588 | /* We have an octal number. */ | |
8589 | { | |
8590 | unsigned int value = c - '0'; | |
8591 | ||
8592 | c = *s; | |
8593 | if ((c >= '0') && (c <= '7')) | |
8594 | { | |
8595 | value <<= 3; | |
8596 | value += (c - '0'); | |
8597 | i++; | |
8598 | s++; | |
a4d5aec7 | 8599 | |
0d79a2a8 US |
8600 | c = *s; |
8601 | if ((c >= '0') && (c <= '7')) | |
8602 | { | |
8603 | value <<= 3; | |
8604 | value += (c - '0'); | |
8605 | i++; | |
8606 | s++; | |
8607 | } | |
8608 | } | |
8609 | ||
8610 | if (value > 0xff) | |
8611 | { | |
8612 | /* octal: \777 is treated as '\077' + '7' */ | |
8613 | value >>= 3; | |
8614 | i--; | |
8615 | s--; | |
8616 | } | |
a4d5aec7 | 8617 | |
0d79a2a8 US |
8618 | c = value; |
8619 | } | |
8620 | break; | |
8621 | } | |
a4d5aec7 AM |
8622 | |
8623 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8624 | escape = false; |
8625 | } | |
8626 | else | |
8627 | { | |
8628 | if (c == '\\') | |
a4d5aec7 AM |
8629 | escape = true; |
8630 | else | |
8631 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8632 | } |
8633 | } | |
8634 | ||
a4d5aec7 AM |
8635 | /* Remeber to terminate the string. */ |
8636 | lang_add_data (BYTE, exp_intop (0)); | |
0d79a2a8 US |
8637 | } |
8638 | ||
252b5132 RH |
8639 | /* Create a new reloc statement. RELOC is the BFD relocation type to |
8640 | generate. HOWTO is the corresponding howto structure (we could | |
8641 | look this up, but the caller has already done so). SECTION is the | |
8642 | section to generate a reloc against, or NAME is the name of the | |
8643 | symbol to generate a reloc against. Exactly one of SECTION and | |
8644 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8645 | ||
8646 | void | |
1579bae1 AM |
8647 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
8648 | reloc_howto_type *howto, | |
8649 | asection *section, | |
8650 | const char *name, | |
8651 | union etree_union *addend) | |
252b5132 RH |
8652 | { |
8653 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8654 | |
252b5132 RH |
8655 | p->reloc = reloc; |
8656 | p->howto = howto; | |
8657 | p->section = section; | |
8658 | p->name = name; | |
8659 | p->addend_exp = addend; | |
8660 | ||
8661 | p->addend_value = 0; | |
8662 | p->output_section = NULL; | |
7fabd029 | 8663 | p->output_offset = 0; |
252b5132 RH |
8664 | } |
8665 | ||
8666 | lang_assignment_statement_type * | |
1579bae1 | 8667 | lang_add_assignment (etree_type *exp) |
252b5132 | 8668 | { |
d3ce72d0 | 8669 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8670 | |
d3ce72d0 NC |
8671 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8672 | new_stmt->exp = exp; | |
8673 | return new_stmt; | |
252b5132 RH |
8674 | } |
8675 | ||
8676 | void | |
1579bae1 | 8677 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8678 | { |
bd4d42c1 | 8679 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8680 | } |
8681 | ||
8682 | void | |
1579bae1 | 8683 | lang_startup (const char *name) |
252b5132 | 8684 | { |
1e915804 | 8685 | if (first_file->filename != NULL) |
252b5132 | 8686 | { |
df5f2391 | 8687 | einfo (_("%F%P: multiple STARTUP files\n")); |
252b5132 RH |
8688 | } |
8689 | first_file->filename = name; | |
8690 | first_file->local_sym_name = name; | |
f38a2680 | 8691 | first_file->flags.real = true; |
252b5132 RH |
8692 | } |
8693 | ||
8694 | void | |
f38a2680 | 8695 | lang_float (bool maybe) |
252b5132 RH |
8696 | { |
8697 | lang_float_flag = maybe; | |
8698 | } | |
8699 | ||
ee3cc2e2 RS |
8700 | |
8701 | /* Work out the load- and run-time regions from a script statement, and | |
8702 | store them in *LMA_REGION and *REGION respectively. | |
8703 | ||
a747ee4d NC |
8704 | MEMSPEC is the name of the run-time region, or the value of |
8705 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8706 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8707 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8708 | had an explicit load address. | |
ee3cc2e2 RS |
8709 | |
8710 | It is an error to specify both a load region and a load address. */ | |
8711 | ||
8712 | static void | |
6bdafbeb NC |
8713 | lang_get_regions (lang_memory_region_type **region, |
8714 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8715 | const char *memspec, |
8716 | const char *lma_memspec, | |
f38a2680 AM |
8717 | bool have_lma, |
8718 | bool have_vma) | |
ee3cc2e2 | 8719 | { |
f38a2680 | 8720 | *lma_region = lang_memory_region_lookup (lma_memspec, false); |
ee3cc2e2 | 8721 | |
6feb9908 AM |
8722 | /* If no runtime region or VMA has been specified, but the load region |
8723 | has been specified, then use the load region for the runtime region | |
8724 | as well. */ | |
6bdafbeb | 8725 | if (lma_memspec != NULL |
0aa7f586 | 8726 | && !have_vma |
6bdafbeb | 8727 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
8728 | *region = *lma_region; |
8729 | else | |
f38a2680 | 8730 | *region = lang_memory_region_lookup (memspec, false); |
ee3cc2e2 | 8731 | |
6bdafbeb | 8732 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 8733 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 8734 | NULL); |
ee3cc2e2 RS |
8735 | } |
8736 | ||
252b5132 | 8737 | void |
6bdafbeb NC |
8738 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
8739 | lang_output_section_phdr_list *phdrs, | |
8740 | const char *lma_memspec) | |
252b5132 | 8741 | { |
ee3cc2e2 RS |
8742 | lang_get_regions (¤t_section->region, |
8743 | ¤t_section->lma_region, | |
8744 | memspec, lma_memspec, | |
6bdafbeb NC |
8745 | current_section->load_base != NULL, |
8746 | current_section->addr_tree != NULL); | |
113e6845 | 8747 | |
252b5132 | 8748 | current_section->fill = fill; |
252b5132 | 8749 | current_section->phdrs = phdrs; |
bde18da4 | 8750 | pop_stat_ptr (); |
252b5132 RH |
8751 | } |
8752 | ||
252b5132 RH |
8753 | /* Set the output format type. -oformat overrides scripts. */ |
8754 | ||
8755 | void | |
1579bae1 AM |
8756 | lang_add_output_format (const char *format, |
8757 | const char *big, | |
8758 | const char *little, | |
8759 | int from_script) | |
252b5132 RH |
8760 | { |
8761 | if (output_target == NULL || !from_script) | |
8762 | { | |
8763 | if (command_line.endian == ENDIAN_BIG | |
8764 | && big != NULL) | |
8765 | format = big; | |
8766 | else if (command_line.endian == ENDIAN_LITTLE | |
8767 | && little != NULL) | |
8768 | format = little; | |
8769 | ||
8770 | output_target = format; | |
8771 | } | |
8772 | } | |
8773 | ||
53d25da6 AM |
8774 | void |
8775 | lang_add_insert (const char *where, int is_before) | |
8776 | { | |
d3ce72d0 | 8777 | lang_insert_statement_type *new_stmt; |
53d25da6 | 8778 | |
d3ce72d0 NC |
8779 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
8780 | new_stmt->where = where; | |
8781 | new_stmt->is_before = is_before; | |
53d25da6 AM |
8782 | saved_script_handle = previous_script_handle; |
8783 | } | |
8784 | ||
252b5132 RH |
8785 | /* Enter a group. This creates a new lang_group_statement, and sets |
8786 | stat_ptr to build new statements within the group. */ | |
8787 | ||
8788 | void | |
1579bae1 | 8789 | lang_enter_group (void) |
252b5132 RH |
8790 | { |
8791 | lang_group_statement_type *g; | |
8792 | ||
8793 | g = new_stat (lang_group_statement, stat_ptr); | |
8794 | lang_list_init (&g->children); | |
bde18da4 | 8795 | push_stat_ptr (&g->children); |
252b5132 RH |
8796 | } |
8797 | ||
8798 | /* Leave a group. This just resets stat_ptr to start writing to the | |
8799 | regular list of statements again. Note that this will not work if | |
8800 | groups can occur inside anything else which can adjust stat_ptr, | |
8801 | but currently they can't. */ | |
8802 | ||
8803 | void | |
1579bae1 | 8804 | lang_leave_group (void) |
252b5132 | 8805 | { |
bde18da4 | 8806 | pop_stat_ptr (); |
252b5132 RH |
8807 | } |
8808 | ||
8809 | /* Add a new program header. This is called for each entry in a PHDRS | |
8810 | command in a linker script. */ | |
8811 | ||
8812 | void | |
1579bae1 AM |
8813 | lang_new_phdr (const char *name, |
8814 | etree_type *type, | |
f38a2680 AM |
8815 | bool filehdr, |
8816 | bool phdrs, | |
1579bae1 AM |
8817 | etree_type *at, |
8818 | etree_type *flags) | |
252b5132 RH |
8819 | { |
8820 | struct lang_phdr *n, **pp; | |
f38a2680 | 8821 | bool hdrs; |
252b5132 | 8822 | |
988de25b | 8823 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
8824 | n->next = NULL; |
8825 | n->name = name; | |
3d9c8f6b | 8826 | n->type = exp_get_vma (type, 0, "program header type"); |
252b5132 RH |
8827 | n->filehdr = filehdr; |
8828 | n->phdrs = phdrs; | |
8829 | n->at = at; | |
8830 | n->flags = flags; | |
1e0061d2 | 8831 | |
5c1a3f0f | 8832 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
8833 | |
8834 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
8835 | if (hdrs |
8836 | && (*pp)->type == 1 | |
8837 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 8838 | { |
c1c8c1ef | 8839 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 8840 | " when prior PT_LOAD headers lack them\n"), NULL); |
f38a2680 | 8841 | hdrs = false; |
5c1a3f0f NS |
8842 | } |
8843 | ||
252b5132 RH |
8844 | *pp = n; |
8845 | } | |
8846 | ||
8847 | /* Record the program header information in the output BFD. FIXME: We | |
8848 | should not be calling an ELF specific function here. */ | |
8849 | ||
8850 | static void | |
1579bae1 | 8851 | lang_record_phdrs (void) |
252b5132 RH |
8852 | { |
8853 | unsigned int alc; | |
8854 | asection **secs; | |
6bdafbeb | 8855 | lang_output_section_phdr_list *last; |
252b5132 | 8856 | struct lang_phdr *l; |
afd7a018 | 8857 | lang_output_section_statement_type *os; |
252b5132 RH |
8858 | |
8859 | alc = 10; | |
1e9cc1c2 | 8860 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 8861 | last = NULL; |
591a748a | 8862 | |
252b5132 RH |
8863 | for (l = lang_phdr_list; l != NULL; l = l->next) |
8864 | { | |
8865 | unsigned int c; | |
8866 | flagword flags; | |
8867 | bfd_vma at; | |
8868 | ||
8869 | c = 0; | |
8ce18f9c | 8870 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8871 | os != NULL; |
8872 | os = os->next) | |
252b5132 | 8873 | { |
6bdafbeb | 8874 | lang_output_section_phdr_list *pl; |
252b5132 | 8875 | |
66c103b7 | 8876 | if (os->constraint < 0) |
0841712e | 8877 | continue; |
252b5132 RH |
8878 | |
8879 | pl = os->phdrs; | |
8880 | if (pl != NULL) | |
8881 | last = pl; | |
8882 | else | |
8883 | { | |
8884 | if (os->sectype == noload_section | |
8885 | || os->bfd_section == NULL | |
8886 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
8887 | continue; | |
591a748a | 8888 | |
7512c397 AM |
8889 | /* Don't add orphans to PT_INTERP header. */ |
8890 | if (l->type == 3) | |
8891 | continue; | |
8892 | ||
e9442572 | 8893 | if (last == NULL) |
591a748a | 8894 | { |
0aa7f586 | 8895 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
8896 | |
8897 | /* If we have not run across a section with a program | |
8898 | header assigned to it yet, then scan forwards to find | |
8899 | one. This prevents inconsistencies in the linker's | |
8900 | behaviour when a script has specified just a single | |
8901 | header and there are sections in that script which are | |
8902 | not assigned to it, and which occur before the first | |
8903 | use of that header. See here for more details: | |
8904 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
8905 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
8906 | if (tmp_os->phdrs) | |
e9442572 AM |
8907 | { |
8908 | last = tmp_os->phdrs; | |
8909 | break; | |
8910 | } | |
8911 | if (last == NULL) | |
8912 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 8913 | } |
e9442572 | 8914 | pl = last; |
252b5132 RH |
8915 | } |
8916 | ||
8917 | if (os->bfd_section == NULL) | |
8918 | continue; | |
8919 | ||
8920 | for (; pl != NULL; pl = pl->next) | |
8921 | { | |
8922 | if (strcmp (pl->name, l->name) == 0) | |
8923 | { | |
8924 | if (c >= alc) | |
8925 | { | |
8926 | alc *= 2; | |
1e9cc1c2 | 8927 | secs = (asection **) xrealloc (secs, |
4724d37e | 8928 | alc * sizeof (asection *)); |
252b5132 RH |
8929 | } |
8930 | secs[c] = os->bfd_section; | |
8931 | ++c; | |
f38a2680 | 8932 | pl->used = true; |
252b5132 RH |
8933 | } |
8934 | } | |
8935 | } | |
8936 | ||
8937 | if (l->flags == NULL) | |
8938 | flags = 0; | |
8939 | else | |
e9ee469a | 8940 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
8941 | |
8942 | if (l->at == NULL) | |
8943 | at = 0; | |
8944 | else | |
e9ee469a | 8945 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 8946 | |
0aa7f586 AM |
8947 | if (!bfd_record_phdr (link_info.output_bfd, l->type, |
8948 | l->flags != NULL, flags, l->at != NULL, | |
8949 | at, l->filehdr, l->phdrs, c, secs)) | |
252b5132 RH |
8950 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
8951 | } | |
8952 | ||
8953 | free (secs); | |
8954 | ||
8955 | /* Make sure all the phdr assignments succeeded. */ | |
8ce18f9c | 8956 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8957 | os != NULL; |
8958 | os = os->next) | |
252b5132 | 8959 | { |
6bdafbeb | 8960 | lang_output_section_phdr_list *pl; |
252b5132 | 8961 | |
66c103b7 | 8962 | if (os->constraint < 0 |
afd7a018 | 8963 | || os->bfd_section == NULL) |
252b5132 RH |
8964 | continue; |
8965 | ||
afd7a018 | 8966 | for (pl = os->phdrs; |
252b5132 RH |
8967 | pl != NULL; |
8968 | pl = pl->next) | |
0aa7f586 | 8969 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 8970 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 8971 | os->name, pl->name); |
252b5132 RH |
8972 | } |
8973 | } | |
8974 | ||
8975 | /* Record a list of sections which may not be cross referenced. */ | |
8976 | ||
8977 | void | |
6bdafbeb | 8978 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
8979 | { |
8980 | struct lang_nocrossrefs *n; | |
8981 | ||
1e9cc1c2 | 8982 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
252b5132 RH |
8983 | n->next = nocrossref_list; |
8984 | n->list = l; | |
f38a2680 | 8985 | n->onlyfirst = false; |
252b5132 RH |
8986 | nocrossref_list = n; |
8987 | ||
8988 | /* Set notice_all so that we get informed about all symbols. */ | |
f38a2680 | 8989 | link_info.notice_all = true; |
252b5132 | 8990 | } |
cdf96953 MF |
8991 | |
8992 | /* Record a section that cannot be referenced from a list of sections. */ | |
8993 | ||
8994 | void | |
8995 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
8996 | { | |
8997 | lang_add_nocrossref (l); | |
f38a2680 | 8998 | nocrossref_list->onlyfirst = true; |
cdf96953 | 8999 | } |
252b5132 RH |
9000 | \f |
9001 | /* Overlay handling. We handle overlays with some static variables. */ | |
9002 | ||
9003 | /* The overlay virtual address. */ | |
9004 | static etree_type *overlay_vma; | |
7e7d5768 AM |
9005 | /* And subsection alignment. */ |
9006 | static etree_type *overlay_subalign; | |
252b5132 | 9007 | |
252b5132 RH |
9008 | /* An expression for the maximum section size seen so far. */ |
9009 | static etree_type *overlay_max; | |
9010 | ||
9011 | /* A list of all the sections in this overlay. */ | |
9012 | ||
89cdebba | 9013 | struct overlay_list { |
252b5132 RH |
9014 | struct overlay_list *next; |
9015 | lang_output_section_statement_type *os; | |
9016 | }; | |
9017 | ||
9018 | static struct overlay_list *overlay_list; | |
9019 | ||
9020 | /* Start handling an overlay. */ | |
9021 | ||
9022 | void | |
7e7d5768 | 9023 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
9024 | { |
9025 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
9026 | ASSERT (overlay_vma == NULL |
9027 | && overlay_subalign == NULL | |
9028 | && overlay_max == NULL); | |
252b5132 RH |
9029 | |
9030 | overlay_vma = vma_expr; | |
7e7d5768 | 9031 | overlay_subalign = subalign; |
252b5132 RH |
9032 | } |
9033 | ||
9034 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
9035 | lang_enter_output_section_statement with the correct VMA. |
9036 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
9037 | |
9038 | void | |
1579bae1 | 9039 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
9040 | { |
9041 | struct overlay_list *n; | |
9042 | etree_type *size; | |
9043 | ||
152d792f | 9044 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
c212f39d | 9045 | 0, 0, overlay_subalign, 0, 0, 0); |
252b5132 | 9046 | |
9f88b410 | 9047 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
9048 | sections on this one. This will work correctly even if `.' is |
9049 | used in the addresses. */ | |
9050 | if (overlay_list == NULL) | |
9f88b410 | 9051 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
9052 | |
9053 | /* Remember the section. */ | |
1e9cc1c2 | 9054 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
9055 | n->os = current_section; |
9056 | n->next = overlay_list; | |
9057 | overlay_list = n; | |
9058 | ||
9059 | size = exp_nameop (SIZEOF, name); | |
9060 | ||
252b5132 RH |
9061 | /* Arrange to work out the maximum section end address. */ |
9062 | if (overlay_max == NULL) | |
9063 | overlay_max = size; | |
9064 | else | |
9065 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
9066 | } | |
9067 | ||
9068 | /* Finish a section in an overlay. There isn't any special to do | |
9069 | here. */ | |
9070 | ||
9071 | void | |
1579bae1 | 9072 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 9073 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
9074 | { |
9075 | const char *name; | |
9076 | char *clean, *s2; | |
9077 | const char *s1; | |
9078 | char *buf; | |
9079 | ||
9080 | name = current_section->name; | |
9081 | ||
a747ee4d NC |
9082 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
9083 | region and that no load-time region has been specified. It doesn't | |
9084 | really matter what we say here, since lang_leave_overlay will | |
9085 | override it. */ | |
9086 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
9087 | |
9088 | /* Define the magic symbols. */ | |
9089 | ||
1e9cc1c2 | 9090 | clean = (char *) xmalloc (strlen (name) + 1); |
252b5132 RH |
9091 | s2 = clean; |
9092 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 9093 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
9094 | *s2++ = *s1; |
9095 | *s2 = '\0'; | |
9096 | ||
1e9cc1c2 | 9097 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
252b5132 | 9098 | sprintf (buf, "__load_start_%s", clean); |
34711ca3 AM |
9099 | lang_add_assignment (exp_provide (buf, |
9100 | exp_nameop (LOADADDR, name), | |
f38a2680 | 9101 | false)); |
252b5132 | 9102 | |
1e9cc1c2 | 9103 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
252b5132 | 9104 | sprintf (buf, "__load_stop_%s", clean); |
34711ca3 AM |
9105 | lang_add_assignment (exp_provide (buf, |
9106 | exp_binop ('+', | |
9107 | exp_nameop (LOADADDR, name), | |
9108 | exp_nameop (SIZEOF, name)), | |
f38a2680 | 9109 | false)); |
252b5132 RH |
9110 | |
9111 | free (clean); | |
9112 | } | |
9113 | ||
9114 | /* Finish an overlay. If there are any overlay wide settings, this | |
9115 | looks through all the sections in the overlay and sets them. */ | |
9116 | ||
9117 | void | |
1579bae1 AM |
9118 | lang_leave_overlay (etree_type *lma_expr, |
9119 | int nocrossrefs, | |
9120 | fill_type *fill, | |
9121 | const char *memspec, | |
6bdafbeb | 9122 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 9123 | const char *lma_memspec) |
252b5132 RH |
9124 | { |
9125 | lang_memory_region_type *region; | |
562d3460 | 9126 | lang_memory_region_type *lma_region; |
252b5132 | 9127 | struct overlay_list *l; |
6bdafbeb | 9128 | lang_nocrossref_type *nocrossref; |
252b5132 | 9129 | |
ee3cc2e2 RS |
9130 | lang_get_regions (®ion, &lma_region, |
9131 | memspec, lma_memspec, | |
f38a2680 | 9132 | lma_expr != NULL, false); |
562d3460 | 9133 | |
252b5132 RH |
9134 | nocrossref = NULL; |
9135 | ||
9f88b410 RS |
9136 | /* After setting the size of the last section, set '.' to end of the |
9137 | overlay region. */ | |
9138 | if (overlay_list != NULL) | |
6d8bf25d AM |
9139 | { |
9140 | overlay_list->os->update_dot = 1; | |
9141 | overlay_list->os->update_dot_tree | |
f38a2680 | 9142 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); |
6d8bf25d | 9143 | } |
9f88b410 | 9144 | |
252b5132 RH |
9145 | l = overlay_list; |
9146 | while (l != NULL) | |
9147 | { | |
9148 | struct overlay_list *next; | |
9149 | ||
1579bae1 | 9150 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 9151 | l->os->fill = fill; |
1545243b | 9152 | |
9f88b410 RS |
9153 | l->os->region = region; |
9154 | l->os->lma_region = lma_region; | |
9155 | ||
9156 | /* The first section has the load address specified in the | |
9157 | OVERLAY statement. The rest are worked out from that. | |
9158 | The base address is not needed (and should be null) if | |
9159 | an LMA region was specified. */ | |
9160 | if (l->next == 0) | |
152d792f AM |
9161 | { |
9162 | l->os->load_base = lma_expr; | |
7b243801 | 9163 | l->os->sectype = first_overlay_section; |
152d792f | 9164 | } |
252b5132 RH |
9165 | if (phdrs != NULL && l->os->phdrs == NULL) |
9166 | l->os->phdrs = phdrs; | |
9167 | ||
9f88b410 | 9168 | if (nocrossrefs) |
252b5132 | 9169 | { |
6bdafbeb | 9170 | lang_nocrossref_type *nc; |
252b5132 | 9171 | |
1e9cc1c2 | 9172 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
252b5132 RH |
9173 | nc->name = l->os->name; |
9174 | nc->next = nocrossref; | |
9175 | nocrossref = nc; | |
9176 | } | |
9177 | ||
9178 | next = l->next; | |
9179 | free (l); | |
9180 | l = next; | |
9181 | } | |
9182 | ||
9183 | if (nocrossref != NULL) | |
9184 | lang_add_nocrossref (nocrossref); | |
9185 | ||
252b5132 | 9186 | overlay_vma = NULL; |
252b5132 RH |
9187 | overlay_list = NULL; |
9188 | overlay_max = NULL; | |
8cb1232a | 9189 | overlay_subalign = NULL; |
252b5132 RH |
9190 | } |
9191 | \f | |
9192 | /* Version handling. This is only useful for ELF. */ | |
9193 | ||
108ba305 JJ |
9194 | /* If PREV is NULL, return first version pattern matching particular symbol. |
9195 | If PREV is non-NULL, return first version pattern matching particular | |
9196 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 9197 | |
108ba305 JJ |
9198 | static struct bfd_elf_version_expr * |
9199 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
9200 | struct bfd_elf_version_expr *prev, | |
9201 | const char *sym) | |
252b5132 | 9202 | { |
93252b1c | 9203 | const char *c_sym; |
108ba305 JJ |
9204 | const char *cxx_sym = sym; |
9205 | const char *java_sym = sym; | |
9206 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
9207 | enum demangling_styles curr_style; |
9208 | ||
9209 | curr_style = CURRENT_DEMANGLING_STYLE; | |
9210 | cplus_demangle_set_style (no_demangling); | |
9211 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
9212 | if (!c_sym) | |
9213 | c_sym = sym; | |
9214 | cplus_demangle_set_style (curr_style); | |
252b5132 | 9215 | |
108ba305 | 9216 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 9217 | { |
93252b1c MF |
9218 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
9219 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
9220 | if (!cxx_sym) |
9221 | cxx_sym = sym; | |
252b5132 | 9222 | } |
df816215 | 9223 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 9224 | { |
93252b1c | 9225 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
9226 | if (!java_sym) |
9227 | java_sym = sym; | |
252b5132 RH |
9228 | } |
9229 | ||
ae5a3597 | 9230 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 9231 | { |
108ba305 JJ |
9232 | struct bfd_elf_version_expr e; |
9233 | ||
9234 | switch (prev ? prev->mask : 0) | |
9235 | { | |
967928e9 AM |
9236 | case 0: |
9237 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
9238 | { | |
93252b1c | 9239 | e.pattern = c_sym; |
1e9cc1c2 | 9240 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9241 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 9242 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
9243 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
9244 | goto out_ret; | |
9245 | else | |
9246 | expr = expr->next; | |
9247 | } | |
9248 | /* Fallthrough */ | |
9249 | case BFD_ELF_VERSION_C_TYPE: | |
9250 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
9251 | { | |
ae5a3597 | 9252 | e.pattern = cxx_sym; |
1e9cc1c2 | 9253 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9254 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9255 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
9256 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
9257 | goto out_ret; | |
9258 | else | |
9259 | expr = expr->next; | |
9260 | } | |
9261 | /* Fallthrough */ | |
9262 | case BFD_ELF_VERSION_CXX_TYPE: | |
9263 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
9264 | { | |
ae5a3597 | 9265 | e.pattern = java_sym; |
1e9cc1c2 | 9266 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9267 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9268 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
9269 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
9270 | goto out_ret; | |
9271 | else | |
9272 | expr = expr->next; | |
9273 | } | |
9274 | /* Fallthrough */ | |
9275 | default: | |
9276 | break; | |
108ba305 | 9277 | } |
252b5132 | 9278 | } |
108ba305 JJ |
9279 | |
9280 | /* Finally, try the wildcards. */ | |
ae5a3597 | 9281 | if (prev == NULL || prev->literal) |
108ba305 | 9282 | expr = head->remaining; |
252b5132 | 9283 | else |
108ba305 | 9284 | expr = prev->next; |
86043bbb | 9285 | for (; expr; expr = expr->next) |
252b5132 | 9286 | { |
108ba305 JJ |
9287 | const char *s; |
9288 | ||
86043bbb MM |
9289 | if (!expr->pattern) |
9290 | continue; | |
9291 | ||
108ba305 JJ |
9292 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
9293 | break; | |
9294 | ||
9295 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
9296 | s = java_sym; | |
9297 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
9298 | s = cxx_sym; | |
9299 | else | |
93252b1c | 9300 | s = c_sym; |
5e35cbc2 | 9301 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 9302 | break; |
252b5132 RH |
9303 | } |
9304 | ||
967928e9 | 9305 | out_ret: |
93252b1c MF |
9306 | if (c_sym != sym) |
9307 | free ((char *) c_sym); | |
108ba305 JJ |
9308 | if (cxx_sym != sym) |
9309 | free ((char *) cxx_sym); | |
9310 | if (java_sym != sym) | |
9311 | free ((char *) java_sym); | |
9312 | return expr; | |
252b5132 RH |
9313 | } |
9314 | ||
5e35cbc2 | 9315 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 9316 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
9317 | |
9318 | static const char * | |
9319 | realsymbol (const char *pattern) | |
9320 | { | |
9321 | const char *p; | |
f38a2680 | 9322 | bool changed = false, backslash = false; |
1e9cc1c2 | 9323 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
9324 | |
9325 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
9326 | { | |
9327 | /* It is a glob pattern only if there is no preceding | |
9328 | backslash. */ | |
5e35cbc2 L |
9329 | if (backslash) |
9330 | { | |
9331 | /* Remove the preceding backslash. */ | |
9332 | *(s - 1) = *p; | |
f38a2680 AM |
9333 | backslash = false; |
9334 | changed = true; | |
5e35cbc2 L |
9335 | } |
9336 | else | |
ae5a3597 AM |
9337 | { |
9338 | if (*p == '?' || *p == '*' || *p == '[') | |
9339 | { | |
9340 | free (symbol); | |
9341 | return NULL; | |
9342 | } | |
5e35cbc2 | 9343 | |
ae5a3597 AM |
9344 | *s++ = *p; |
9345 | backslash = *p == '\\'; | |
9346 | } | |
5e35cbc2 L |
9347 | } |
9348 | ||
9349 | if (changed) | |
9350 | { | |
9351 | *s = '\0'; | |
9352 | return symbol; | |
9353 | } | |
9354 | else | |
9355 | { | |
9356 | free (symbol); | |
9357 | return pattern; | |
9358 | } | |
9359 | } | |
9360 | ||
d3ce72d0 | 9361 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
9362 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
9363 | pattern to be matched against symbol names. */ | |
252b5132 RH |
9364 | |
9365 | struct bfd_elf_version_expr * | |
1579bae1 | 9366 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 9367 | const char *new_name, |
86043bbb | 9368 | const char *lang, |
f38a2680 | 9369 | bool literal_p) |
252b5132 RH |
9370 | { |
9371 | struct bfd_elf_version_expr *ret; | |
9372 | ||
d3ce72d0 | 9373 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
252b5132 | 9374 | ret->next = orig; |
31941635 L |
9375 | ret->symver = 0; |
9376 | ret->script = 0; | |
f38a2680 | 9377 | ret->literal = true; |
d3ce72d0 | 9378 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
9379 | if (ret->pattern == NULL) |
9380 | { | |
d3ce72d0 | 9381 | ret->pattern = new_name; |
f38a2680 | 9382 | ret->literal = false; |
ae5a3597 | 9383 | } |
252b5132 RH |
9384 | |
9385 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 9386 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 9387 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 9388 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 9389 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 9390 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
9391 | else |
9392 | { | |
9393 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
9394 | lang); | |
108ba305 | 9395 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
9396 | } |
9397 | ||
fac1652d | 9398 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
9399 | } |
9400 | ||
9401 | /* This is called for each set of variable names and match | |
9402 | expressions. */ | |
9403 | ||
9404 | struct bfd_elf_version_tree * | |
1579bae1 AM |
9405 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
9406 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
9407 | { |
9408 | struct bfd_elf_version_tree *ret; | |
9409 | ||
1e9cc1c2 | 9410 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
108ba305 JJ |
9411 | ret->globals.list = globals; |
9412 | ret->locals.list = locals; | |
9413 | ret->match = lang_vers_match; | |
252b5132 | 9414 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
9415 | return ret; |
9416 | } | |
9417 | ||
9418 | /* This static variable keeps track of version indices. */ | |
9419 | ||
9420 | static int version_index; | |
9421 | ||
108ba305 JJ |
9422 | static hashval_t |
9423 | version_expr_head_hash (const void *p) | |
9424 | { | |
1e9cc1c2 NC |
9425 | const struct bfd_elf_version_expr *e = |
9426 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 9427 | |
ae5a3597 | 9428 | return htab_hash_string (e->pattern); |
108ba305 JJ |
9429 | } |
9430 | ||
9431 | static int | |
9432 | version_expr_head_eq (const void *p1, const void *p2) | |
9433 | { | |
1e9cc1c2 NC |
9434 | const struct bfd_elf_version_expr *e1 = |
9435 | (const struct bfd_elf_version_expr *) p1; | |
9436 | const struct bfd_elf_version_expr *e2 = | |
9437 | (const struct bfd_elf_version_expr *) p2; | |
108ba305 | 9438 | |
ae5a3597 | 9439 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
9440 | } |
9441 | ||
9442 | static void | |
9443 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
9444 | { | |
9445 | size_t count = 0; | |
9446 | struct bfd_elf_version_expr *e, *next; | |
9447 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
9448 | ||
9449 | for (e = head->list; e; e = e->next) | |
9450 | { | |
ae5a3597 | 9451 | if (e->literal) |
108ba305 JJ |
9452 | count++; |
9453 | head->mask |= e->mask; | |
9454 | } | |
9455 | ||
9456 | if (count) | |
9457 | { | |
9458 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
9459 | version_expr_head_eq, NULL); | |
9460 | list_loc = &head->list; | |
9461 | remaining_loc = &head->remaining; | |
9462 | for (e = head->list; e; e = next) | |
9463 | { | |
9464 | next = e->next; | |
ae5a3597 | 9465 | if (!e->literal) |
108ba305 JJ |
9466 | { |
9467 | *remaining_loc = e; | |
9468 | remaining_loc = &e->next; | |
9469 | } | |
9470 | else | |
9471 | { | |
1e9cc1c2 | 9472 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
9473 | |
9474 | if (*loc) | |
9475 | { | |
9476 | struct bfd_elf_version_expr *e1, *last; | |
9477 | ||
1e9cc1c2 | 9478 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
9479 | last = NULL; |
9480 | do | |
9481 | { | |
9482 | if (e1->mask == e->mask) | |
9483 | { | |
9484 | last = NULL; | |
9485 | break; | |
9486 | } | |
9487 | last = e1; | |
9488 | e1 = e1->next; | |
9489 | } | |
ae5a3597 | 9490 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
9491 | |
9492 | if (last == NULL) | |
9493 | { | |
9494 | /* This is a duplicate. */ | |
9495 | /* FIXME: Memory leak. Sometimes pattern is not | |
9496 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 9497 | /* free (e->pattern); */ |
108ba305 JJ |
9498 | free (e); |
9499 | } | |
9500 | else | |
9501 | { | |
9502 | e->next = last->next; | |
9503 | last->next = e; | |
9504 | } | |
9505 | } | |
9506 | else | |
9507 | { | |
9508 | *loc = e; | |
9509 | *list_loc = e; | |
9510 | list_loc = &e->next; | |
9511 | } | |
9512 | } | |
9513 | } | |
9514 | *remaining_loc = NULL; | |
9515 | *list_loc = head->remaining; | |
9516 | } | |
9517 | else | |
9518 | head->remaining = head->list; | |
9519 | } | |
9520 | ||
252b5132 RH |
9521 | /* This is called when we know the name and dependencies of the |
9522 | version. */ | |
9523 | ||
9524 | void | |
1579bae1 AM |
9525 | lang_register_vers_node (const char *name, |
9526 | struct bfd_elf_version_tree *version, | |
9527 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
9528 | { |
9529 | struct bfd_elf_version_tree *t, **pp; | |
9530 | struct bfd_elf_version_expr *e1; | |
9531 | ||
6b9b879a JJ |
9532 | if (name == NULL) |
9533 | name = ""; | |
9534 | ||
fd91d419 L |
9535 | if (link_info.version_info != NULL |
9536 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 9537 | { |
6feb9908 AM |
9538 | einfo (_("%X%P: anonymous version tag cannot be combined" |
9539 | " with other version tags\n")); | |
5ed6aba4 | 9540 | free (version); |
6b9b879a JJ |
9541 | return; |
9542 | } | |
9543 | ||
252b5132 | 9544 | /* Make sure this node has a unique name. */ |
fd91d419 | 9545 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9546 | if (strcmp (t->name, name) == 0) |
9547 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9548 | ||
108ba305 JJ |
9549 | lang_finalize_version_expr_head (&version->globals); |
9550 | lang_finalize_version_expr_head (&version->locals); | |
9551 | ||
252b5132 RH |
9552 | /* Check the global and local match names, and make sure there |
9553 | aren't any duplicates. */ | |
9554 | ||
108ba305 | 9555 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9556 | { |
fd91d419 | 9557 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9558 | { |
9559 | struct bfd_elf_version_expr *e2; | |
9560 | ||
ae5a3597 | 9561 | if (t->locals.htab && e1->literal) |
108ba305 | 9562 | { |
1e9cc1c2 | 9563 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9564 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 9565 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9566 | { |
9567 | if (e1->mask == e2->mask) | |
6feb9908 | 9568 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 9569 | " in version information\n"), e1->pattern); |
108ba305 JJ |
9570 | e2 = e2->next; |
9571 | } | |
9572 | } | |
ae5a3597 | 9573 | else if (!e1->literal) |
108ba305 | 9574 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9575 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9576 | && e1->mask == e2->mask) | |
9577 | einfo (_("%X%P: duplicate expression `%s'" | |
9578 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9579 | } |
9580 | } | |
9581 | ||
108ba305 | 9582 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9583 | { |
fd91d419 | 9584 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9585 | { |
9586 | struct bfd_elf_version_expr *e2; | |
9587 | ||
ae5a3597 | 9588 | if (t->globals.htab && e1->literal) |
108ba305 | 9589 | { |
1e9cc1c2 | 9590 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9591 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9592 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9593 | { |
9594 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9595 | einfo (_("%X%P: duplicate expression `%s'" |
9596 | " in version information\n"), | |
ae5a3597 | 9597 | e1->pattern); |
108ba305 JJ |
9598 | e2 = e2->next; |
9599 | } | |
9600 | } | |
ae5a3597 | 9601 | else if (!e1->literal) |
108ba305 | 9602 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9603 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9604 | && e1->mask == e2->mask) | |
9605 | einfo (_("%X%P: duplicate expression `%s'" | |
9606 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9607 | } |
9608 | } | |
9609 | ||
9610 | version->deps = deps; | |
9611 | version->name = name; | |
6b9b879a JJ |
9612 | if (name[0] != '\0') |
9613 | { | |
9614 | ++version_index; | |
9615 | version->vernum = version_index; | |
9616 | } | |
9617 | else | |
9618 | version->vernum = 0; | |
252b5132 | 9619 | |
fd91d419 | 9620 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9621 | ; |
9622 | *pp = version; | |
9623 | } | |
9624 | ||
9625 | /* This is called when we see a version dependency. */ | |
9626 | ||
9627 | struct bfd_elf_version_deps * | |
1579bae1 | 9628 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9629 | { |
9630 | struct bfd_elf_version_deps *ret; | |
9631 | struct bfd_elf_version_tree *t; | |
9632 | ||
1e9cc1c2 | 9633 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
252b5132 RH |
9634 | ret->next = list; |
9635 | ||
fd91d419 | 9636 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9637 | { |
9638 | if (strcmp (t->name, name) == 0) | |
9639 | { | |
9640 | ret->version_needed = t; | |
9641 | return ret; | |
9642 | } | |
9643 | } | |
9644 | ||
9645 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9646 | ||
1e0061d2 | 9647 | ret->version_needed = NULL; |
252b5132 RH |
9648 | return ret; |
9649 | } | |
9650 | ||
9651 | static void | |
1579bae1 | 9652 | lang_do_version_exports_section (void) |
252b5132 RH |
9653 | { |
9654 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9655 | ||
9656 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9657 | { | |
9658 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9659 | char *contents, *p; | |
9660 | bfd_size_type len; | |
9661 | ||
9662 | if (sec == NULL) | |
b7a26f91 | 9663 | continue; |
252b5132 | 9664 | |
eea6121a | 9665 | len = sec->size; |
1e9cc1c2 | 9666 | contents = (char *) xmalloc (len); |
252b5132 | 9667 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9668 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9669 | |
9670 | p = contents; | |
89cdebba | 9671 | while (p < contents + len) |
252b5132 | 9672 | { |
f38a2680 | 9673 | greg = lang_new_vers_pattern (greg, p, NULL, false); |
252b5132 RH |
9674 | p = strchr (p, '\0') + 1; |
9675 | } | |
9676 | ||
9677 | /* Do not free the contents, as we used them creating the regex. */ | |
9678 | ||
9679 | /* Do not include this section in the link. */ | |
a14a5de3 | 9680 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9681 | } |
9682 | ||
f38a2680 | 9683 | lreg = lang_new_vers_pattern (NULL, "*", NULL, false); |
252b5132 RH |
9684 | lang_register_vers_node (command_line.version_exports_section, |
9685 | lang_new_vers_node (greg, lreg), NULL); | |
9686 | } | |
577a0623 | 9687 | |
874ef038 AB |
9688 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially |
9689 | called with UPDATE_REGIONS_P set to FALSE, in this case no errors are | |
9690 | thrown, however, references to symbols in the origin and length fields | |
9691 | will be pushed into the symbol table, this allows PROVIDE statements to | |
9692 | then provide these symbols. This function is called a second time with | |
9693 | UPDATE_REGIONS_P set to TRUE, this time the we update the actual region | |
9694 | data structures, and throw errors if missing symbols are encountered. */ | |
cc9ad334 SKS |
9695 | |
9696 | static void | |
f38a2680 | 9697 | lang_do_memory_regions (bool update_regions_p) |
cc9ad334 SKS |
9698 | { |
9699 | lang_memory_region_type *r = lang_memory_region_list; | |
9700 | ||
9701 | for (; r != NULL; r = r->next) | |
9702 | { | |
9703 | if (r->origin_exp) | |
0aa7f586 AM |
9704 | { |
9705 | exp_fold_tree_no_dot (r->origin_exp); | |
874ef038 AB |
9706 | if (update_regions_p) |
9707 | { | |
9708 | if (expld.result.valid_p) | |
9709 | { | |
9710 | r->origin = expld.result.value; | |
9711 | r->current = r->origin; | |
9712 | } | |
9713 | else | |
9714 | einfo (_("%P: invalid origin for memory region %s\n"), | |
9715 | r->name_list.name); | |
9716 | } | |
0aa7f586 | 9717 | } |
cc9ad334 | 9718 | if (r->length_exp) |
0aa7f586 AM |
9719 | { |
9720 | exp_fold_tree_no_dot (r->length_exp); | |
874ef038 AB |
9721 | if (update_regions_p) |
9722 | { | |
9723 | if (expld.result.valid_p) | |
9724 | r->length = expld.result.value; | |
9725 | else | |
9726 | einfo (_("%P: invalid length for memory region %s\n"), | |
9727 | r->name_list.name); | |
9728 | } | |
9729 | } | |
cc9ad334 SKS |
9730 | } |
9731 | } | |
9732 | ||
577a0623 | 9733 | void |
1579bae1 | 9734 | lang_add_unique (const char *name) |
577a0623 AM |
9735 | { |
9736 | struct unique_sections *ent; | |
9737 | ||
9738 | for (ent = unique_section_list; ent; ent = ent->next) | |
9739 | if (strcmp (ent->name, name) == 0) | |
9740 | return; | |
9741 | ||
1e9cc1c2 | 9742 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
577a0623 AM |
9743 | ent->name = xstrdup (name); |
9744 | ent->next = unique_section_list; | |
9745 | unique_section_list = ent; | |
9746 | } | |
55255dae L |
9747 | |
9748 | /* Append the list of dynamic symbols to the existing one. */ | |
9749 | ||
9750 | void | |
37a141bf FS |
9751 | lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, |
9752 | struct bfd_elf_version_expr *dynamic) | |
55255dae | 9753 | { |
37a141bf | 9754 | if (*list_p) |
55255dae | 9755 | { |
07806542 JJ |
9756 | struct bfd_elf_version_expr *tail; |
9757 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
9758 | ; | |
37a141bf FS |
9759 | tail->next = (*list_p)->head.list; |
9760 | (*list_p)->head.list = dynamic; | |
55255dae L |
9761 | } |
9762 | else | |
9763 | { | |
9764 | struct bfd_elf_dynamic_list *d; | |
9765 | ||
1e9cc1c2 | 9766 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
55255dae L |
9767 | d->head.list = dynamic; |
9768 | d->match = lang_vers_match; | |
37a141bf | 9769 | *list_p = d; |
55255dae L |
9770 | } |
9771 | } | |
9772 | ||
9773 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
9774 | one. */ | |
9775 | ||
9776 | void | |
9777 | lang_append_dynamic_list_cpp_typeinfo (void) | |
9778 | { | |
0aa7f586 | 9779 | const char *symbols[] = |
55255dae L |
9780 | { |
9781 | "typeinfo name for*", | |
9782 | "typeinfo for*" | |
9783 | }; | |
9784 | struct bfd_elf_version_expr *dynamic = NULL; | |
9785 | unsigned int i; | |
9786 | ||
9787 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9788 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9789 | false); |
55255dae | 9790 | |
37a141bf | 9791 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
55255dae | 9792 | } |
40b36307 L |
9793 | |
9794 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
9795 | existing one. */ | |
9796 | ||
9797 | void | |
9798 | lang_append_dynamic_list_cpp_new (void) | |
9799 | { | |
0aa7f586 | 9800 | const char *symbols[] = |
40b36307 L |
9801 | { |
9802 | "operator new*", | |
9803 | "operator delete*" | |
9804 | }; | |
9805 | struct bfd_elf_version_expr *dynamic = NULL; | |
9806 | unsigned int i; | |
9807 | ||
9808 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9809 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9810 | false); |
40b36307 | 9811 | |
37a141bf | 9812 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
40b36307 | 9813 | } |
01554a74 AM |
9814 | |
9815 | /* Scan a space and/or comma separated string of features. */ | |
9816 | ||
9817 | void | |
9818 | lang_ld_feature (char *str) | |
9819 | { | |
9820 | char *p, *q; | |
9821 | ||
9822 | p = str; | |
9823 | while (*p) | |
9824 | { | |
9825 | char sep; | |
9826 | while (*p == ',' || ISSPACE (*p)) | |
9827 | ++p; | |
9828 | if (!*p) | |
9829 | break; | |
9830 | q = p + 1; | |
9831 | while (*q && *q != ',' && !ISSPACE (*q)) | |
9832 | ++q; | |
9833 | sep = *q; | |
9834 | *q = 0; | |
9835 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
f38a2680 | 9836 | config.sane_expr = true; |
01554a74 AM |
9837 | else |
9838 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
9839 | *q = sep; | |
9840 | p = q; | |
9841 | } | |
9842 | } | |
3604cb1f TG |
9843 | |
9844 | /* Pretty print memory amount. */ | |
9845 | ||
9846 | static void | |
b8281767 | 9847 | lang_print_memory_size (uint64_t sz) |
3604cb1f TG |
9848 | { |
9849 | if ((sz & 0x3fffffff) == 0) | |
b8281767 | 9850 | printf ("%10" PRIu64 " GB", sz >> 30); |
3604cb1f | 9851 | else if ((sz & 0xfffff) == 0) |
b8281767 | 9852 | printf ("%10" PRIu64 " MB", sz >> 20); |
3604cb1f | 9853 | else if ((sz & 0x3ff) == 0) |
b8281767 | 9854 | printf ("%10" PRIu64 " KB", sz >> 10); |
3604cb1f | 9855 | else |
b8281767 | 9856 | printf (" %10" PRIu64 " B", sz); |
3604cb1f TG |
9857 | } |
9858 | ||
9859 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
9860 | ||
9861 | void | |
9862 | lang_print_memory_usage (void) | |
9863 | { | |
9864 | lang_memory_region_type *r; | |
9865 | ||
9866 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
9867 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
9868 | { | |
9869 | bfd_vma used_length = r->current - r->origin; | |
3604cb1f TG |
9870 | |
9871 | printf ("%16s: ",r->name_list.name); | |
9872 | lang_print_memory_size (used_length); | |
b8281767 | 9873 | lang_print_memory_size (r->length); |
3604cb1f | 9874 | |
1769380a AM |
9875 | if (r->length != 0) |
9876 | { | |
9877 | double percent = used_length * 100.0 / r->length; | |
2410edcd | 9878 | printf (" %6.2f%%", percent); |
1769380a | 9879 | } |
2410edcd | 9880 | printf ("\n"); |
3604cb1f TG |
9881 | } |
9882 | } |