]>
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; | |
edc1244e MM |
448 | lang_input_statement_type *arch_is; |
449 | if (filename && filename_cmp (filename, file_spec) == 0) | |
450 | ; | |
451 | /* FIXME: see also walk_wild_file_in_exclude_list for why we | |
452 | also check parents BFD (local_sym_)name to match input statements | |
453 | with unadorned archive names. */ | |
454 | else if (file->the_bfd | |
455 | && file->the_bfd->my_archive | |
456 | && (arch_is = bfd_usrdata (file->the_bfd->my_archive)) | |
457 | && arch_is->local_sym_name | |
458 | && filename_cmp (arch_is->local_sym_name, file_spec) == 0) | |
459 | ; | |
460 | else | |
b1eecf6f MM |
461 | return; |
462 | } | |
463 | ||
885d8643 MM |
464 | /* If filename is excluded we're done. */ |
465 | if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file)) | |
466 | return; | |
467 | ||
b1eecf6f MM |
468 | /* Check section name against each wildcard spec. If there's no |
469 | wildcard all sections match. */ | |
470 | sec = ptr->section_list; | |
471 | if (sec == NULL) | |
472 | add_matching_section (ptr, sec, s, file); | |
473 | else | |
474 | { | |
475 | const char *sname = bfd_section_name (s); | |
476 | for (; sec != NULL; sec = sec->next) | |
477 | { | |
478 | if (sec->spec.name != NULL | |
479 | && spec_match (&sec->spec, sname) != 0) | |
480 | continue; | |
481 | ||
482 | /* Don't process sections from files which were excluded. */ | |
483 | if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, | |
484 | file)) | |
485 | add_matching_section (ptr, sec, s, file); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
02ecc8e9 L |
490 | /* Return the numerical value of the init_priority attribute from |
491 | section name NAME. */ | |
492 | ||
9a24a276 AM |
493 | static int |
494 | get_init_priority (const asection *sec) | |
02ecc8e9 | 495 | { |
9a24a276 AM |
496 | const char *name = bfd_section_name (sec); |
497 | const char *dot; | |
02ecc8e9 L |
498 | |
499 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 500 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
501 | lower value means a higher priority. |
502 | ||
9a24a276 | 503 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
504 | decimal numerical value of the init_priority attribute. |
505 | The order of execution in .init_array is forward and | |
506 | .fini_array is backward. | |
9a24a276 | 507 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
508 | decimal numerical value of the init_priority attribute. |
509 | The order of execution in .ctors is backward and .dtors | |
510 | is forward. | |
9a24a276 AM |
511 | |
512 | .init_array.NNNNN sections would normally be placed in an output | |
513 | .init_array section, .fini_array.NNNNN in .fini_array, | |
514 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
515 | we should sort by increasing number (and could just use | |
516 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
517 | being placed in .init_array (which may also contain | |
518 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
519 | placed in .fini_array then we need to extract the init_priority | |
520 | attribute and sort on that. */ | |
521 | dot = strrchr (name, '.'); | |
522 | if (dot != NULL && ISDIGIT (dot[1])) | |
02ecc8e9 | 523 | { |
9a24a276 AM |
524 | char *end; |
525 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
526 | if (*end == 0) | |
527 | { | |
528 | if (dot == name + 6 | |
529 | && (strncmp (name, ".ctors", 6) == 0 | |
530 | || strncmp (name, ".dtors", 6) == 0)) | |
531 | init_priority = 65535 - init_priority; | |
532 | if (init_priority <= INT_MAX) | |
533 | return init_priority; | |
534 | } | |
02ecc8e9 | 535 | } |
9a24a276 | 536 | return -1; |
02ecc8e9 L |
537 | } |
538 | ||
50c77e5d NC |
539 | /* Compare sections ASEC and BSEC according to SORT. */ |
540 | ||
541 | static int | |
542 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
543 | { | |
544 | int ret; | |
9a24a276 | 545 | int a_priority, b_priority; |
50c77e5d NC |
546 | |
547 | switch (sort) | |
548 | { | |
549 | default: | |
550 | abort (); | |
551 | ||
02ecc8e9 | 552 | case by_init_priority: |
9a24a276 AM |
553 | a_priority = get_init_priority (asec); |
554 | b_priority = get_init_priority (bsec); | |
555 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 556 | goto sort_by_name; |
9a24a276 | 557 | ret = a_priority - b_priority; |
02ecc8e9 L |
558 | if (ret) |
559 | break; | |
560 | else | |
561 | goto sort_by_name; | |
562 | ||
50c77e5d | 563 | case by_alignment_name: |
fd361982 | 564 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
565 | if (ret) |
566 | break; | |
567 | /* Fall through. */ | |
568 | ||
569 | case by_name: | |
dc1e8a47 | 570 | sort_by_name: |
fd361982 | 571 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
572 | break; |
573 | ||
574 | case by_name_alignment: | |
fd361982 | 575 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
576 | if (ret) |
577 | break; | |
578 | /* Fall through. */ | |
579 | ||
580 | case by_alignment: | |
fd361982 | 581 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
582 | break; |
583 | } | |
584 | ||
585 | return ret; | |
586 | } | |
587 | ||
af31506c MM |
588 | /* PE puts the sort key in the input statement. */ |
589 | ||
590 | static const char * | |
591 | sort_filename (bfd *abfd) | |
592 | { | |
593 | lang_input_statement_type *is = bfd_usrdata (abfd); | |
594 | if (is->sort_key) | |
595 | return is->sort_key; | |
596 | return bfd_get_filename (abfd); | |
597 | } | |
598 | ||
599 | /* Handle wildcard sorting. This returns the place in a binary search tree | |
600 | where this FILE:SECTION should be inserted for wild statement WILD where | |
601 | the spec SEC was the matching one. The tree is later linearized. */ | |
50c77e5d NC |
602 | |
603 | static lang_section_bst_type ** | |
af31506c MM |
604 | wild_sort (lang_wild_statement_type *wild, |
605 | struct wildcard_list *sec, | |
606 | lang_input_statement_type *file, | |
607 | asection *section) | |
50c77e5d | 608 | { |
329c1c86 | 609 | lang_section_bst_type **tree; |
50c77e5d NC |
610 | |
611 | if (!wild->filenames_sorted | |
af31506c MM |
612 | && (sec == NULL || sec->spec.sorted == none |
613 | || sec->spec.sorted == by_none)) | |
50c77e5d | 614 | { |
af31506c MM |
615 | /* We might be called even if _this_ spec doesn't need sorting, |
616 | in which case we simply append at the right end of tree. */ | |
617 | return wild->rightmost; | |
50c77e5d NC |
618 | } |
619 | ||
af31506c | 620 | tree = &wild->tree; |
329c1c86 | 621 | while (*tree) |
50c77e5d | 622 | { |
af31506c MM |
623 | /* Sorting by filename takes precedence over sorting by section |
624 | name. */ | |
625 | ||
626 | if (wild->filenames_sorted) | |
627 | { | |
628 | const char *fn, *ln; | |
629 | bool fa, la; | |
630 | int i; | |
631 | asection *lsec = (*tree)->section; | |
632 | ||
633 | /* The PE support for the .idata section as generated by | |
634 | dlltool assumes that files will be sorted by the name of | |
635 | the archive and then the name of the file within the | |
636 | archive. */ | |
637 | ||
638 | fa = file->the_bfd->my_archive != NULL; | |
639 | if (fa) | |
640 | fn = sort_filename (file->the_bfd->my_archive); | |
641 | else | |
642 | fn = sort_filename (file->the_bfd); | |
643 | ||
644 | la = lsec->owner->my_archive != NULL; | |
645 | if (la) | |
646 | ln = sort_filename (lsec->owner->my_archive); | |
647 | else | |
648 | ln = sort_filename (lsec->owner); | |
649 | ||
650 | i = filename_cmp (fn, ln); | |
651 | if (i > 0) | |
652 | { tree = &((*tree)->right); continue; } | |
653 | else if (i < 0) | |
654 | { tree = &((*tree)->left); continue; } | |
655 | ||
656 | if (fa || la) | |
657 | { | |
658 | if (fa) | |
659 | fn = sort_filename (file->the_bfd); | |
660 | if (la) | |
661 | ln = sort_filename (lsec->owner); | |
662 | ||
663 | i = filename_cmp (fn, ln); | |
664 | if (i > 0) | |
665 | { tree = &((*tree)->right); continue; } | |
666 | else if (i < 0) | |
667 | { tree = &((*tree)->left); continue; } | |
668 | } | |
669 | } | |
670 | ||
671 | /* Here either the files are not sorted by name, or we are | |
672 | looking at the sections for this file. */ | |
673 | ||
50c77e5d | 674 | /* Find the correct node to append this section. */ |
b7eab2a9 MM |
675 | if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none |
676 | && compare_section (sec->spec.sorted, section, (*tree)->section) < 0) | |
329c1c86 AM |
677 | tree = &((*tree)->left); |
678 | else | |
679 | tree = &((*tree)->right); | |
50c77e5d NC |
680 | } |
681 | ||
682 | return tree; | |
683 | } | |
684 | ||
af31506c | 685 | /* Use wild_sort to build a BST to sort sections. */ |
50c77e5d NC |
686 | |
687 | static void | |
af31506c | 688 | output_section_callback_sort (lang_wild_statement_type *ptr, |
329c1c86 AM |
689 | struct wildcard_list *sec, |
690 | asection *section, | |
691 | lang_input_statement_type *file, | |
d0bf826b | 692 | void *output) |
50c77e5d NC |
693 | { |
694 | lang_section_bst_type *node; | |
695 | lang_section_bst_type **tree; | |
d0bf826b | 696 | lang_output_section_statement_type *os; |
50c77e5d | 697 | |
d0bf826b AM |
698 | os = (lang_output_section_statement_type *) output; |
699 | ||
700 | if (unique_section_p (section, os)) | |
50c77e5d NC |
701 | return; |
702 | ||
670c91c0 MM |
703 | /* Don't add sections to the tree when we already know that |
704 | lang_add_section won't do anything with it. */ | |
705 | if (wont_add_section_p (section, os)) | |
706 | return; | |
707 | ||
1e9cc1c2 | 708 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
50c77e5d NC |
709 | node->left = 0; |
710 | node->right = 0; | |
711 | node->section = section; | |
b209b5a6 | 712 | node->pattern = ptr->section_list; |
50c77e5d | 713 | |
af31506c | 714 | tree = wild_sort (ptr, sec, file, section); |
50c77e5d | 715 | if (tree != NULL) |
af31506c MM |
716 | { |
717 | *tree = node; | |
718 | if (tree == ptr->rightmost) | |
719 | ptr->rightmost = &node->right; | |
720 | } | |
50c77e5d NC |
721 | } |
722 | ||
723 | /* Convert a sorted sections' BST back to list form. */ | |
724 | ||
725 | static void | |
329c1c86 AM |
726 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
727 | lang_section_bst_type *tree, | |
728 | void *output) | |
50c77e5d NC |
729 | { |
730 | if (tree->left) | |
731 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
732 | ||
af31506c MM |
733 | lang_add_section (&ptr->children, tree->section, tree->pattern, |
734 | ptr->section_flag_list, | |
329c1c86 | 735 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
736 | |
737 | if (tree->right) | |
738 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
739 | ||
740 | free (tree); | |
741 | } | |
742 | ||
b1eecf6f MM |
743 | \f |
744 | /* Sections are matched against wildcard statements via a prefix tree. | |
745 | The prefix tree holds prefixes of all matching patterns (up to the first | |
746 | wildcard character), and the wild statement from which those patterns | |
747 | came. When matching a section name against the tree we're walking through | |
748 | the tree character by character. Each statement we hit is one that | |
749 | potentially matches. This is checked by actually going through the | |
750 | (glob) matching routines. | |
751 | ||
752 | When the section name turns out to actually match we record that section | |
753 | in the wild statements list of matching sections. */ | |
754 | ||
755 | /* A prefix can be matched by multiple statement, so we need a list of them. */ | |
756 | struct wild_stmt_list | |
757 | { | |
758 | lang_wild_statement_type *stmt; | |
759 | struct wild_stmt_list *next; | |
760 | }; | |
761 | ||
762 | /* The prefix tree itself. */ | |
763 | struct prefixtree | |
764 | { | |
765 | /* The list of all children (linked via .next). */ | |
766 | struct prefixtree *child; | |
767 | struct prefixtree *next; | |
768 | /* This tree node is responsible for the prefix of parent plus 'c'. */ | |
769 | char c; | |
770 | /* The statements that potentially can match this prefix. */ | |
771 | struct wild_stmt_list *stmt; | |
772 | }; | |
773 | ||
774 | /* We always have a root node in the prefix tree. It corresponds to the | |
775 | empty prefix. E.g. a glob like "*" would sit in this root. */ | |
776 | static struct prefixtree the_root, *ptroot = &the_root; | |
777 | ||
778 | /* Given a prefix tree in *TREE, corresponding to prefix P, find or | |
779 | INSERT the tree node corresponding to prefix P+C. */ | |
780 | ||
781 | static struct prefixtree * | |
782 | get_prefix_tree (struct prefixtree **tree, char c, bool insert) | |
783 | { | |
784 | struct prefixtree *t; | |
785 | for (t = *tree; t; t = t->next) | |
786 | if (t->c == c) | |
787 | return t; | |
788 | if (!insert) | |
789 | return NULL; | |
790 | t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); | |
791 | t->child = NULL; | |
792 | t->next = *tree; | |
793 | t->c = c; | |
794 | t->stmt = NULL; | |
795 | *tree = t; | |
796 | return t; | |
797 | } | |
798 | ||
799 | /* Add STMT to the set of statements that can be matched by the prefix | |
800 | corresponding to prefix tree T. */ | |
801 | ||
802 | static void | |
803 | pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) | |
804 | { | |
805 | struct wild_stmt_list *sl, **psl; | |
806 | sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); | |
807 | sl->stmt = stmt; | |
808 | sl->next = NULL; | |
809 | psl = &t->stmt; | |
810 | while (*psl) | |
811 | psl = &(*psl)->next; | |
812 | *psl = sl; | |
813 | } | |
814 | ||
815 | /* Insert STMT into the global prefix tree. */ | |
816 | ||
817 | static void | |
818 | insert_prefix_tree (lang_wild_statement_type *stmt) | |
819 | { | |
820 | struct wildcard_list *sec; | |
821 | struct prefixtree *t; | |
822 | ||
823 | if (!stmt->section_list) | |
824 | { | |
825 | /* If we have no section_list (no wildcards in the wild STMT), | |
826 | then every section name will match, so add this to the root. */ | |
827 | pt_add_stmt (ptroot, stmt); | |
828 | return; | |
829 | } | |
830 | ||
831 | for (sec = stmt->section_list; sec; sec = sec->next) | |
832 | { | |
833 | const char *name = sec->spec.name ? sec->spec.name : "*"; | |
834 | char c; | |
835 | t = ptroot; | |
836 | for (; (c = *name); name++) | |
837 | { | |
838 | if (c == '*' || c == '[' || c == '?') | |
839 | break; | |
840 | t = get_prefix_tree (&t->child, c, true); | |
841 | } | |
842 | /* If we hit a glob character, the matching prefix is what we saw | |
843 | until now. If we hit the end of pattern (hence it's no glob) then | |
844 | we can do better: we only need to record a match when a section name | |
845 | completely matches, not merely a prefix, so record the trailing 0 | |
846 | as well. */ | |
847 | if (!c) | |
848 | t = get_prefix_tree (&t->child, 0, true); | |
849 | pt_add_stmt (t, stmt); | |
850 | } | |
851 | } | |
852 | ||
853 | /* Dump T indented by INDENT spaces. */ | |
854 | ||
855 | static void | |
856 | debug_prefix_tree_rec (struct prefixtree *t, int indent) | |
857 | { | |
858 | for (; t; t = t->next) | |
859 | { | |
860 | struct wild_stmt_list *sl; | |
861 | printf ("%*s %c", indent, "", t->c); | |
862 | for (sl = t->stmt; sl; sl = sl->next) | |
863 | { | |
864 | struct wildcard_list *curr; | |
865 | printf (" %p ", sl->stmt); | |
866 | for (curr = sl->stmt->section_list; curr; curr = curr->next) | |
867 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
868 | } | |
869 | printf ("\n"); | |
870 | debug_prefix_tree_rec (t->child, indent + 2); | |
871 | } | |
872 | } | |
873 | ||
874 | /* Dump the global prefix tree. */ | |
875 | ||
876 | static void | |
877 | debug_prefix_tree (void) | |
878 | { | |
879 | debug_prefix_tree_rec (ptroot, 2); | |
880 | } | |
881 | ||
049522ca MM |
882 | /* Like strcspn() but start to look from the end to beginning of |
883 | S. Returns the length of the suffix of S consisting entirely | |
884 | of characters not in REJECT. */ | |
885 | ||
886 | static size_t | |
887 | rstrcspn (const char *s, const char *reject) | |
888 | { | |
889 | size_t len = strlen (s), sufflen = 0; | |
890 | while (len--) | |
891 | { | |
892 | char c = s[len]; | |
893 | if (strchr (reject, c) != 0) | |
894 | break; | |
895 | sufflen++; | |
896 | } | |
897 | return sufflen; | |
898 | } | |
899 | ||
b1eecf6f MM |
900 | /* Analyze the wildcards in wild statement PTR to setup various |
901 | things for quick matching. */ | |
72223188 JJ |
902 | |
903 | static void | |
904 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
905 | { | |
72223188 | 906 | struct wildcard_list *sec; |
d7791499 | 907 | |
e6f2cbf5 | 908 | ptr->tree = NULL; |
af31506c | 909 | ptr->rightmost = &ptr->tree; |
72223188 | 910 | |
049522ca MM |
911 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
912 | { | |
913 | if (sec->spec.name) | |
914 | { | |
915 | sec->spec.namelen = strlen (sec->spec.name); | |
916 | sec->spec.prefixlen = strcspn (sec->spec.name, "?*["); | |
917 | sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen, | |
918 | "?*]"); | |
919 | } | |
920 | else | |
921 | sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; | |
922 | } | |
923 | ||
b1eecf6f | 924 | insert_prefix_tree (ptr); |
4dec4d4e RH |
925 | } |
926 | ||
b1eecf6f MM |
927 | /* Match all sections from FILE against the global prefix tree, |
928 | and record them into each wild statement that has a match. */ | |
929 | ||
4dec4d4e | 930 | static void |
b1eecf6f | 931 | resolve_wild_sections (lang_input_statement_type *file) |
43ae96e9 | 932 | { |
b1eecf6f MM |
933 | asection *s; |
934 | ||
935 | if (file->flags.just_syms) | |
936 | return; | |
937 | ||
938 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
939 | { | |
940 | const char *sname = bfd_section_name (s); | |
941 | char c = 1; | |
942 | struct prefixtree *t = ptroot; | |
943 | //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename); | |
944 | do | |
945 | { | |
946 | if (t->stmt) | |
947 | { | |
948 | struct wild_stmt_list *sl; | |
949 | for (sl = t->stmt; sl; sl = sl->next) | |
950 | { | |
951 | walk_wild_section_match (sl->stmt, file, s); | |
952 | //printf (" ZZZ maybe place into %p\n", sl->stmt); | |
953 | } | |
954 | } | |
955 | if (!c) | |
956 | break; | |
957 | c = *sname++; | |
958 | t = get_prefix_tree (&t->child, c, false); | |
959 | } | |
960 | while (t); | |
961 | } | |
43ae96e9 MM |
962 | } |
963 | ||
b1eecf6f MM |
964 | /* Match all sections from all input files against the global prefix tree. */ |
965 | ||
966 | static void | |
967 | resolve_wilds (void) | |
968 | { | |
969 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
970 | { | |
971 | //printf("XXX %s\n", f->filename); | |
b1eecf6f MM |
972 | if (f->the_bfd == NULL |
973 | || !bfd_check_format (f->the_bfd, bfd_archive)) | |
974 | resolve_wild_sections (f); | |
975 | else | |
976 | { | |
977 | bfd *member; | |
978 | ||
979 | /* This is an archive file. We must map each member of the | |
980 | archive separately. */ | |
981 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); | |
982 | while (member != NULL) | |
983 | { | |
984 | /* When lookup_name is called, it will call the add_symbols | |
985 | entry point for the archive. For each element of the | |
986 | archive which is included, BFD will call ldlang_add_file, | |
987 | which will set the usrdata field of the member to the | |
988 | lang_input_statement. */ | |
989 | if (bfd_usrdata (member) != NULL) | |
990 | resolve_wild_sections (bfd_usrdata (member)); | |
991 | ||
992 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
993 | } | |
994 | } | |
995 | } | |
996 | } | |
997 | ||
b1eecf6f MM |
998 | /* For each input section that matches wild statement S calls |
999 | CALLBACK with DATA. */ | |
43ae96e9 MM |
1000 | |
1001 | static void | |
1002 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) | |
1003 | { | |
b1eecf6f | 1004 | lang_statement_union_type *l; |
43ae96e9 | 1005 | |
b1eecf6f | 1006 | for (l = s->matching_sections.head; l; l = l->header.next) |
43ae96e9 | 1007 | { |
b1eecf6f MM |
1008 | (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, |
1009 | l->input_matcher.input_stmt, data); | |
43ae96e9 | 1010 | } |
5f992e62 AM |
1011 | } |
1012 | ||
08da4cac | 1013 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
1014 | function for each node, except those inside output section statements |
1015 | with constraint set to -1. */ | |
252b5132 | 1016 | |
8658f989 | 1017 | void |
1579bae1 AM |
1018 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
1019 | lang_statement_union_type *s) | |
252b5132 | 1020 | { |
1579bae1 | 1021 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
1022 | { |
1023 | func (s); | |
1024 | ||
1025 | switch (s->header.type) | |
1026 | { | |
1027 | case lang_constructors_statement_enum: | |
1028 | lang_for_each_statement_worker (func, constructor_list.head); | |
1029 | break; | |
1030 | case lang_output_section_statement_enum: | |
8658f989 AM |
1031 | if (s->output_section_statement.constraint != -1) |
1032 | lang_for_each_statement_worker | |
1033 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
1034 | break; |
1035 | case lang_wild_statement_enum: | |
6feb9908 AM |
1036 | lang_for_each_statement_worker (func, |
1037 | s->wild_statement.children.head); | |
252b5132 RH |
1038 | break; |
1039 | case lang_group_statement_enum: | |
1040 | lang_for_each_statement_worker (func, | |
1041 | s->group_statement.children.head); | |
1042 | break; | |
1043 | case lang_data_statement_enum: | |
1044 | case lang_reloc_statement_enum: | |
1045 | case lang_object_symbols_statement_enum: | |
1046 | case lang_output_statement_enum: | |
1047 | case lang_target_statement_enum: | |
1048 | case lang_input_section_enum: | |
1049 | case lang_input_statement_enum: | |
1050 | case lang_assignment_statement_enum: | |
1051 | case lang_padding_statement_enum: | |
1052 | case lang_address_statement_enum: | |
1053 | case lang_fill_statement_enum: | |
53d25da6 | 1054 | case lang_insert_statement_enum: |
252b5132 RH |
1055 | break; |
1056 | default: | |
1057 | FAIL (); | |
1058 | break; | |
1059 | } | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | void | |
1579bae1 | 1064 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1065 | { |
08da4cac | 1066 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1067 | } |
1068 | ||
1069 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1070 | |
252b5132 | 1071 | void |
1579bae1 | 1072 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1073 | { |
1579bae1 | 1074 | list->head = NULL; |
252b5132 RH |
1075 | list->tail = &list->head; |
1076 | } | |
1077 | ||
36983a93 AM |
1078 | static void |
1079 | lang_statement_append (lang_statement_list_type *list, | |
1080 | void *element, | |
1081 | void *field) | |
1082 | { | |
1083 | *(list->tail) = element; | |
1084 | list->tail = field; | |
1085 | } | |
1086 | ||
bde18da4 AM |
1087 | void |
1088 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1089 | { | |
1090 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1091 | abort (); | |
1092 | *stat_save_ptr++ = stat_ptr; | |
1093 | stat_ptr = new_ptr; | |
1094 | } | |
1095 | ||
1096 | void | |
1097 | pop_stat_ptr (void) | |
1098 | { | |
1099 | if (stat_save_ptr <= stat_save) | |
1100 | abort (); | |
1101 | stat_ptr = *--stat_save_ptr; | |
1102 | } | |
1103 | ||
08da4cac | 1104 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1105 | |
08da4cac | 1106 | static lang_statement_union_type * |
1579bae1 AM |
1107 | new_statement (enum statement_enum type, |
1108 | size_t size, | |
1109 | lang_statement_list_type *list) | |
252b5132 | 1110 | { |
d3ce72d0 | 1111 | lang_statement_union_type *new_stmt; |
252b5132 | 1112 | |
988de25b | 1113 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
1114 | new_stmt->header.type = type; |
1115 | new_stmt->header.next = NULL; | |
1116 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1117 | return new_stmt; | |
252b5132 RH |
1118 | } |
1119 | ||
08da4cac KH |
1120 | /* Build a new input file node for the language. There are several |
1121 | ways in which we treat an input file, eg, we only look at symbols, | |
1122 | or prefix it with a -l etc. | |
252b5132 | 1123 | |
08da4cac | 1124 | We can be supplied with requests for input files more than once; |
396a2467 | 1125 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1126 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1127 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1128 | |
252b5132 | 1129 | static lang_input_statement_type * |
1579bae1 AM |
1130 | new_afile (const char *name, |
1131 | lang_input_file_enum_type file_type, | |
16171946 FS |
1132 | const char *target, |
1133 | const char *from_filename) | |
252b5132 RH |
1134 | { |
1135 | lang_input_statement_type *p; | |
1136 | ||
f38a2680 | 1137 | lang_has_input_file = true; |
66be1055 | 1138 | |
fb221fba NC |
1139 | name = ldfile_possibly_remap_input (name); |
1140 | if (name == NULL) | |
1141 | return NULL; | |
1142 | ||
1f1f5b92 | 1143 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1144 | memset (&p->the_bfd, 0, |
1145 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
16171946 | 1146 | p->extra_search_path = NULL; |
252b5132 | 1147 | p->target = target; |
66be1055 AM |
1148 | p->flags.dynamic = input_flags.dynamic; |
1149 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1150 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1151 | p->flags.whole_archive = input_flags.whole_archive; | |
f4a23d42 | 1152 | p->flags.sysrooted = input_flags.sysrooted; |
a6ad7914 | 1153 | p->sort_key = NULL; |
bcb674cf | 1154 | |
252b5132 RH |
1155 | switch (file_type) |
1156 | { | |
1157 | case lang_input_file_is_symbols_only_enum: | |
1158 | p->filename = name; | |
252b5132 | 1159 | p->local_sym_name = name; |
f38a2680 AM |
1160 | p->flags.real = true; |
1161 | p->flags.just_syms = true; | |
252b5132 RH |
1162 | break; |
1163 | case lang_input_file_is_fake_enum: | |
1164 | p->filename = name; | |
252b5132 | 1165 | p->local_sym_name = name; |
252b5132 RH |
1166 | break; |
1167 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1168 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1169 | { |
1170 | p->filename = name + 1; | |
f38a2680 | 1171 | p->flags.full_name_provided = true; |
0aa7f586 | 1172 | } |
d4ae5fb0 | 1173 | else |
0aa7f586 | 1174 | p->filename = name; |
ff7a0acf | 1175 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
f38a2680 AM |
1176 | p->flags.maybe_archive = true; |
1177 | p->flags.real = true; | |
1178 | p->flags.search_dirs = true; | |
252b5132 RH |
1179 | break; |
1180 | case lang_input_file_is_marker_enum: | |
1181 | p->filename = name; | |
252b5132 | 1182 | p->local_sym_name = name; |
f38a2680 | 1183 | p->flags.search_dirs = true; |
252b5132 RH |
1184 | break; |
1185 | case lang_input_file_is_search_file_enum: | |
1186 | p->filename = name; | |
252b5132 | 1187 | p->local_sym_name = name; |
16171946 FS |
1188 | /* If name is a relative path, search the directory of the current linker |
1189 | script first. */ | |
1190 | if (from_filename && !IS_ABSOLUTE_PATH (name)) | |
1191 | p->extra_search_path = ldirname (from_filename); | |
f38a2680 AM |
1192 | p->flags.real = true; |
1193 | p->flags.search_dirs = true; | |
252b5132 RH |
1194 | break; |
1195 | case lang_input_file_is_file_enum: | |
1196 | p->filename = name; | |
252b5132 | 1197 | p->local_sym_name = name; |
f38a2680 | 1198 | p->flags.real = true; |
252b5132 RH |
1199 | break; |
1200 | default: | |
1201 | FAIL (); | |
1202 | } | |
c4b78195 | 1203 | |
36983a93 | 1204 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1205 | return p; |
1206 | } | |
1207 | ||
1208 | lang_input_statement_type * | |
1579bae1 AM |
1209 | lang_add_input_file (const char *name, |
1210 | lang_input_file_enum_type file_type, | |
1211 | const char *target) | |
252b5132 | 1212 | { |
3aa2d05a | 1213 | if (name != NULL |
08dedd66 | 1214 | && (*name == '=' || startswith (name, "$SYSROOT"))) |
bfa23434 HPN |
1215 | { |
1216 | lang_input_statement_type *ret; | |
1217 | char *sysrooted_name | |
3aa2d05a NC |
1218 | = concat (ld_sysroot, |
1219 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1220 | (const char *) NULL); | |
bfa23434 HPN |
1221 | |
1222 | /* We've now forcibly prepended the sysroot, making the input | |
1223 | file independent of the context. Therefore, temporarily | |
1224 | force a non-sysrooted context for this statement, so it won't | |
1225 | get the sysroot prepended again when opened. (N.B. if it's a | |
1226 | script, any child nodes with input files starting with "/" | |
1227 | will be handled as "sysrooted" as they'll be found to be | |
1228 | within the sysroot subdirectory.) */ | |
1229 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1230 | input_flags.sysrooted = 0; | |
16171946 | 1231 | ret = new_afile (sysrooted_name, file_type, target, NULL); |
bfa23434 HPN |
1232 | input_flags.sysrooted = outer_sysrooted; |
1233 | return ret; | |
1234 | } | |
1235 | ||
16171946 | 1236 | return new_afile (name, file_type, target, current_input_file); |
252b5132 RH |
1237 | } |
1238 | ||
409d7240 | 1239 | struct out_section_hash_entry |
750877ba L |
1240 | { |
1241 | struct bfd_hash_entry root; | |
409d7240 | 1242 | lang_statement_union_type s; |
750877ba L |
1243 | }; |
1244 | ||
1245 | /* The hash table. */ | |
1246 | ||
409d7240 | 1247 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1248 | |
384a9dda | 1249 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1250 | initialize the table, fill in an entry and remove the table. */ |
1251 | ||
1252 | static struct bfd_hash_entry * | |
329c1c86 | 1253 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1254 | struct bfd_hash_table *table, |
1255 | const char *string) | |
750877ba | 1256 | { |
384a9dda | 1257 | lang_output_section_statement_type **nextp; |
409d7240 | 1258 | struct out_section_hash_entry *ret; |
384a9dda AM |
1259 | |
1260 | if (entry == NULL) | |
1261 | { | |
1e9cc1c2 | 1262 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1263 | sizeof (*ret)); |
384a9dda AM |
1264 | if (entry == NULL) |
1265 | return entry; | |
1266 | } | |
1267 | ||
1268 | entry = bfd_hash_newfunc (entry, table, string); | |
1269 | if (entry == NULL) | |
1270 | return entry; | |
1271 | ||
409d7240 AM |
1272 | ret = (struct out_section_hash_entry *) entry; |
1273 | memset (&ret->s, 0, sizeof (ret->s)); | |
1274 | ret->s.header.type = lang_output_section_statement_enum; | |
3d9c8f6b AM |
1275 | ret->s.output_section_statement.subsection_alignment = NULL; |
1276 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1277 | ret->s.output_section_statement.block_value = 1; |
1278 | lang_list_init (&ret->s.output_section_statement.children); | |
1279 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1280 | |
218868ba | 1281 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1282 | first one, lang_os_list.tail points to the "next" |
218868ba | 1283 | field of the last element of the list. */ |
5c1e6d53 | 1284 | if (lang_os_list.head != NULL) |
409d7240 AM |
1285 | ret->s.output_section_statement.prev |
1286 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1287 | ((char *) lang_os_list.tail |
409d7240 | 1288 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1289 | |
384a9dda AM |
1290 | /* GCC's strict aliasing rules prevent us from just casting the |
1291 | address, so we store the pointer in a variable and cast that | |
1292 | instead. */ | |
409d7240 | 1293 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1294 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
384a9dda | 1295 | return &ret->root; |
750877ba L |
1296 | } |
1297 | ||
1298 | static void | |
409d7240 | 1299 | output_section_statement_table_init (void) |
750877ba | 1300 | { |
409d7240 AM |
1301 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1302 | output_section_statement_newfunc, | |
1303 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1304 | 61)) |
df5f2391 | 1305 | einfo (_("%F%P: can not create hash table: %E\n")); |
750877ba L |
1306 | } |
1307 | ||
1308 | static void | |
409d7240 | 1309 | output_section_statement_table_free (void) |
750877ba | 1310 | { |
409d7240 | 1311 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1312 | } |
1313 | ||
08da4cac KH |
1314 | /* Build enough state so that the parser can build its tree. */ |
1315 | ||
252b5132 | 1316 | void |
1579bae1 | 1317 | lang_init (void) |
252b5132 RH |
1318 | { |
1319 | obstack_begin (&stat_obstack, 1000); | |
b1eecf6f | 1320 | obstack_init (&pt_obstack); |
252b5132 RH |
1321 | |
1322 | stat_ptr = &statement_list; | |
1323 | ||
409d7240 | 1324 | output_section_statement_table_init (); |
750877ba | 1325 | |
252b5132 RH |
1326 | lang_list_init (stat_ptr); |
1327 | ||
1328 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1329 | lang_list_init (&lang_os_list); |
252b5132 | 1330 | lang_list_init (&file_chain); |
1579bae1 AM |
1331 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1332 | NULL); | |
08da4cac | 1333 | abs_output_section = |
21401fc7 | 1334 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); |
252b5132 RH |
1335 | |
1336 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1337 | |
16e4ecc0 AM |
1338 | asneeded_list_head = NULL; |
1339 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1340 | } |
1341 | ||
750877ba L |
1342 | void |
1343 | lang_finish (void) | |
1344 | { | |
409d7240 | 1345 | output_section_statement_table_free (); |
fb221fba | 1346 | ldfile_remap_input_free (); |
750877ba L |
1347 | } |
1348 | ||
252b5132 | 1349 | /*---------------------------------------------------------------------- |
08da4cac KH |
1350 | A region is an area of memory declared with the |
1351 | MEMORY { name:org=exp, len=exp ... } | |
1352 | syntax. | |
252b5132 | 1353 | |
08da4cac | 1354 | We maintain a list of all the regions here. |
252b5132 | 1355 | |
08da4cac | 1356 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1357 | which is created when looked up to be the entire data space. |
1358 | ||
1359 | If create is true we are creating a region inside a MEMORY block. | |
1360 | In this case it is probably an error to create a region that has | |
1361 | already been created. If we are not inside a MEMORY block it is | |
1362 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 | 1363 | and so we issue a warning. |
1e0061d2 | 1364 | |
4a93e180 NC |
1365 | Each region has at least one name. The first name is either |
1366 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1367 | alias names to an existing region within a script with | |
1368 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1369 | region. */ | |
252b5132 RH |
1370 | |
1371 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1372 | static lang_memory_region_type **lang_memory_region_list_tail |
1373 | = &lang_memory_region_list; | |
252b5132 RH |
1374 | |
1375 | lang_memory_region_type * | |
f38a2680 | 1376 | lang_memory_region_lookup (const char *const name, bool create) |
252b5132 | 1377 | { |
4a93e180 NC |
1378 | lang_memory_region_name *n; |
1379 | lang_memory_region_type *r; | |
d3ce72d0 | 1380 | lang_memory_region_type *new_region; |
252b5132 | 1381 | |
9f88b410 RS |
1382 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1383 | if (name == NULL) | |
1384 | return NULL; | |
1385 | ||
4a93e180 NC |
1386 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1387 | for (n = &r->name_list; n != NULL; n = n->next) | |
1388 | if (strcmp (n->name, name) == 0) | |
4724d37e RM |
1389 | { |
1390 | if (create) | |
c1c8c1ef | 1391 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1392 | NULL, name); |
1393 | return r; | |
1394 | } | |
252b5132 | 1395 | |
a747ee4d | 1396 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1397 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1398 | NULL, name); |
a747ee4d | 1399 | |
988de25b | 1400 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1401 | |
d3ce72d0 NC |
1402 | new_region->name_list.name = xstrdup (name); |
1403 | new_region->name_list.next = NULL; | |
1404 | new_region->next = NULL; | |
cc9ad334 | 1405 | new_region->origin_exp = NULL; |
d3ce72d0 | 1406 | new_region->origin = 0; |
cc9ad334 | 1407 | new_region->length_exp = NULL; |
d3ce72d0 NC |
1408 | new_region->length = ~(bfd_size_type) 0; |
1409 | new_region->current = 0; | |
1410 | new_region->last_os = NULL; | |
1411 | new_region->flags = 0; | |
1412 | new_region->not_flags = 0; | |
f38a2680 | 1413 | new_region->had_full_message = false; |
252b5132 | 1414 | |
d3ce72d0 NC |
1415 | *lang_memory_region_list_tail = new_region; |
1416 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1417 | |
d3ce72d0 | 1418 | return new_region; |
252b5132 RH |
1419 | } |
1420 | ||
4a93e180 | 1421 | void |
0aa7f586 | 1422 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1423 | { |
0aa7f586 AM |
1424 | lang_memory_region_name *n; |
1425 | lang_memory_region_type *r; | |
1426 | lang_memory_region_type *region; | |
4a93e180 NC |
1427 | |
1428 | /* The default region must be unique. This ensures that it is not necessary | |
1429 | to iterate through the name list if someone wants the check if a region is | |
1430 | the default memory region. */ | |
1431 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1432 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
c1c8c1ef | 1433 | einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
1434 | |
1435 | /* Look for the target region and check if the alias is not already | |
1436 | in use. */ | |
1437 | region = NULL; | |
1438 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1439 | for (n = &r->name_list; n != NULL; n = n->next) | |
1440 | { | |
4724d37e RM |
1441 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1442 | region = r; | |
1443 | if (strcmp (n->name, alias) == 0) | |
c1c8c1ef | 1444 | einfo (_("%F%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1445 | "alias `%s'\n"), |
1446 | NULL, alias); | |
4a93e180 NC |
1447 | } |
1448 | ||
1449 | /* Check if the target region exists. */ | |
1450 | if (region == NULL) | |
c1c8c1ef | 1451 | einfo (_("%F%P:%pS: error: memory region `%s' " |
4724d37e RM |
1452 | "for alias `%s' does not exist\n"), |
1453 | NULL, region_name, alias); | |
4a93e180 NC |
1454 | |
1455 | /* Add alias to region name list. */ | |
988de25b | 1456 | n = stat_alloc (sizeof (lang_memory_region_name)); |
4a93e180 NC |
1457 | n->name = xstrdup (alias); |
1458 | n->next = region->name_list.next; | |
1459 | region->name_list.next = n; | |
1460 | } | |
1461 | ||
5f992e62 | 1462 | static lang_memory_region_type * |
0aa7f586 | 1463 | lang_memory_default (asection *section) |
252b5132 RH |
1464 | { |
1465 | lang_memory_region_type *p; | |
1466 | ||
1467 | flagword sec_flags = section->flags; | |
1468 | ||
1469 | /* Override SEC_DATA to mean a writable section. */ | |
1470 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1471 | sec_flags |= SEC_DATA; | |
1472 | ||
1579bae1 | 1473 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1474 | { |
1475 | if ((p->flags & sec_flags) != 0 | |
1476 | && (p->not_flags & sec_flags) == 0) | |
1477 | { | |
1478 | return p; | |
1479 | } | |
1480 | } | |
f38a2680 | 1481 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
252b5132 RH |
1482 | } |
1483 | ||
24ef1aa7 GM |
1484 | /* Get the output section statement directly from the userdata. */ |
1485 | ||
1486 | lang_output_section_statement_type * | |
1487 | lang_output_section_get (const asection *output_section) | |
1488 | { | |
a48931cc | 1489 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1490 | } |
1491 | ||
d127ecce AM |
1492 | /* Find or create an output_section_statement with the given NAME. |
1493 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
21401fc7 AM |
1494 | match any non-negative constraint. If CREATE is 0 return NULL when |
1495 | no match exists. If CREATE is 1, create an output_section_statement | |
1496 | when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2, | |
1497 | always make a new output_section_statement. */ | |
d127ecce | 1498 | |
384a9dda | 1499 | lang_output_section_statement_type * |
d127ecce | 1500 | lang_output_section_statement_lookup (const char *name, |
66c103b7 | 1501 | int constraint, |
21401fc7 | 1502 | int create) |
252b5132 | 1503 | { |
409d7240 | 1504 | struct out_section_hash_entry *entry; |
252b5132 | 1505 | |
409d7240 AM |
1506 | entry = ((struct out_section_hash_entry *) |
1507 | bfd_hash_lookup (&output_section_statement_table, name, | |
f38a2680 | 1508 | create != 0, false)); |
384a9dda AM |
1509 | if (entry == NULL) |
1510 | { | |
66c103b7 | 1511 | if (create) |
df5f2391 | 1512 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1513 | return NULL; |
1514 | } | |
252b5132 | 1515 | |
409d7240 | 1516 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1517 | { |
384a9dda AM |
1518 | /* We have a section of this name, but it might not have the correct |
1519 | constraint. */ | |
66c103b7 | 1520 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1521 | |
d127ecce | 1522 | name = entry->s.output_section_statement.name; |
21401fc7 AM |
1523 | do |
1524 | { | |
1525 | if (create != 2 | |
1526 | && !(create && constraint == SPECIAL) | |
1527 | && (constraint == entry->s.output_section_statement.constraint | |
1528 | || (constraint == 0 | |
1529 | && entry->s.output_section_statement.constraint >= 0))) | |
1530 | return &entry->s.output_section_statement; | |
1531 | last_ent = entry; | |
1532 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1533 | } | |
1534 | while (entry != NULL | |
1535 | && name == entry->s.output_section_statement.name); | |
252b5132 | 1536 | |
66c103b7 AM |
1537 | if (!create) |
1538 | return NULL; | |
1539 | ||
409d7240 AM |
1540 | entry |
1541 | = ((struct out_section_hash_entry *) | |
1542 | output_section_statement_newfunc (NULL, | |
1543 | &output_section_statement_table, | |
1544 | name)); | |
750877ba | 1545 | if (entry == NULL) |
384a9dda | 1546 | { |
df5f2391 | 1547 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1548 | return NULL; |
1549 | } | |
1550 | entry->root = last_ent->root; | |
1551 | last_ent->root.next = &entry->root; | |
252b5132 | 1552 | } |
384a9dda | 1553 | |
409d7240 AM |
1554 | entry->s.output_section_statement.name = name; |
1555 | entry->s.output_section_statement.constraint = constraint; | |
de34d428 AM |
1556 | entry->s.output_section_statement.dup_output = (create == 2 |
1557 | || constraint == SPECIAL); | |
409d7240 | 1558 | return &entry->s.output_section_statement; |
252b5132 RH |
1559 | } |
1560 | ||
d127ecce AM |
1561 | /* Find the next output_section_statement with the same name as OS. |
1562 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1563 | match any non-negative constraint. */ | |
1564 | ||
1565 | lang_output_section_statement_type * | |
1566 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1567 | int constraint) | |
1568 | { | |
1569 | /* All output_section_statements are actually part of a | |
1570 | struct out_section_hash_entry. */ | |
1571 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1572 | ((char *) os | |
1573 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1574 | const char *name = os->name; | |
1575 | ||
1576 | ASSERT (name == entry->root.string); | |
1577 | do | |
1578 | { | |
1579 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1580 | if (entry == NULL | |
1581 | || name != entry->s.output_section_statement.name) | |
1582 | return NULL; | |
1583 | } | |
1584 | while (constraint != entry->s.output_section_statement.constraint | |
1585 | && (constraint != 0 | |
1586 | || entry->s.output_section_statement.constraint < 0)); | |
1587 | ||
1588 | return &entry->s.output_section_statement; | |
1589 | } | |
1590 | ||
afd7a018 AM |
1591 | /* A variant of lang_output_section_find used by place_orphan. |
1592 | Returns the output statement that should precede a new output | |
1593 | statement for SEC. If an exact match is found on certain flags, | |
1594 | sets *EXACT too. */ | |
1595 | ||
1596 | lang_output_section_statement_type * | |
1597 | lang_output_section_find_by_flags (const asection *sec, | |
93638471 | 1598 | flagword sec_flags, |
390fbbf1 AM |
1599 | lang_output_section_statement_type **exact, |
1600 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1601 | { |
1602 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1603 | flagword look_flags, differ; |
afd7a018 AM |
1604 | |
1605 | /* We know the first statement on this list is *ABS*. May as well | |
1606 | skip it. */ | |
8ce18f9c | 1607 | first = (void *) lang_os_list.head; |
afd7a018 AM |
1608 | first = first->next; |
1609 | ||
1610 | /* First try for an exact match. */ | |
1611 | found = NULL; | |
1612 | for (look = first; look; look = look->next) | |
1613 | { | |
d9d94ac8 | 1614 | look_flags = look->flags; |
afd7a018 | 1615 | if (look->bfd_section != NULL) |
ecca9871 | 1616 | { |
d9d94ac8 | 1617 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1618 | if (match_type && !match_type (link_info.output_bfd, |
1619 | look->bfd_section, | |
390fbbf1 | 1620 | sec->owner, sec)) |
ecca9871 L |
1621 | continue; |
1622 | } | |
d9d94ac8 AM |
1623 | differ = look_flags ^ sec_flags; |
1624 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1625 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1626 | found = look; |
1627 | } | |
1628 | if (found != NULL) | |
1629 | { | |
390fbbf1 AM |
1630 | if (exact != NULL) |
1631 | *exact = found; | |
afd7a018 AM |
1632 | return found; |
1633 | } | |
1634 | ||
d9d94ac8 AM |
1635 | if ((sec_flags & SEC_CODE) != 0 |
1636 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1637 | { |
1638 | /* Try for a rw code section. */ | |
1639 | for (look = first; look; look = look->next) | |
1640 | { | |
d9d94ac8 | 1641 | look_flags = look->flags; |
afd7a018 | 1642 | if (look->bfd_section != NULL) |
ecca9871 | 1643 | { |
d9d94ac8 | 1644 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1645 | if (match_type && !match_type (link_info.output_bfd, |
1646 | look->bfd_section, | |
390fbbf1 | 1647 | sec->owner, sec)) |
ecca9871 L |
1648 | continue; |
1649 | } | |
d9d94ac8 AM |
1650 | differ = look_flags ^ sec_flags; |
1651 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1652 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1653 | found = look; |
1654 | } | |
afd7a018 | 1655 | } |
d9d94ac8 AM |
1656 | else if ((sec_flags & SEC_READONLY) != 0 |
1657 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1658 | { |
1659 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1660 | for (look = first; look; look = look->next) | |
1661 | { | |
d9d94ac8 | 1662 | look_flags = look->flags; |
afd7a018 | 1663 | if (look->bfd_section != NULL) |
ecca9871 | 1664 | { |
d9d94ac8 | 1665 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1666 | if (match_type && !match_type (link_info.output_bfd, |
1667 | look->bfd_section, | |
390fbbf1 | 1668 | sec->owner, sec)) |
ecca9871 L |
1669 | continue; |
1670 | } | |
d9d94ac8 AM |
1671 | differ = look_flags ^ sec_flags; |
1672 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1673 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1674 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1675 | | SEC_READONLY)) | |
1676 | && !(look_flags & SEC_SMALL_DATA))) | |
afd7a018 AM |
1677 | found = look; |
1678 | } | |
afd7a018 | 1679 | } |
d9d94ac8 AM |
1680 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 |
1681 | && (sec_flags & SEC_ALLOC) != 0) | |
1682 | { | |
1683 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1684 | as if it were a loaded section, and don't use match_type. */ | |
f38a2680 | 1685 | bool seen_thread_local = false; |
d9d94ac8 AM |
1686 | |
1687 | match_type = NULL; | |
1688 | for (look = first; look; look = look->next) | |
1689 | { | |
1690 | look_flags = look->flags; | |
1691 | if (look->bfd_section != NULL) | |
1692 | look_flags = look->bfd_section->flags; | |
1693 | ||
1694 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1695 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1696 | { | |
1697 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1698 | if (!(look_flags & SEC_LOAD) | |
1699 | && (sec_flags & SEC_LOAD)) | |
1700 | /* ..so if we're at a .tbss section and we're placing | |
1701 | a .tdata section stop looking and return the | |
1702 | previous section. */ | |
1703 | break; | |
1704 | found = look; | |
f38a2680 | 1705 | seen_thread_local = true; |
d9d94ac8 AM |
1706 | } |
1707 | else if (seen_thread_local) | |
1708 | break; | |
1709 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1710 | found = look; | |
1711 | } | |
1712 | } | |
1713 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1714 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1715 | { |
1716 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1717 | for (look = first; look; look = look->next) | |
1718 | { | |
d9d94ac8 | 1719 | look_flags = look->flags; |
afd7a018 | 1720 | if (look->bfd_section != NULL) |
ecca9871 | 1721 | { |
d9d94ac8 | 1722 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1723 | if (match_type && !match_type (link_info.output_bfd, |
1724 | look->bfd_section, | |
390fbbf1 | 1725 | sec->owner, sec)) |
ecca9871 L |
1726 | continue; |
1727 | } | |
d9d94ac8 AM |
1728 | differ = look_flags ^ sec_flags; |
1729 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1730 | | SEC_THREAD_LOCAL)) | |
1731 | || ((look_flags & SEC_SMALL_DATA) | |
1732 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
afd7a018 AM |
1733 | found = look; |
1734 | } | |
afd7a018 | 1735 | } |
d9d94ac8 AM |
1736 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1737 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1738 | { |
1739 | /* .data goes after .rodata. */ | |
1740 | for (look = first; look; look = look->next) | |
1741 | { | |
d9d94ac8 | 1742 | look_flags = look->flags; |
afd7a018 | 1743 | if (look->bfd_section != NULL) |
ecca9871 | 1744 | { |
d9d94ac8 | 1745 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1746 | if (match_type && !match_type (link_info.output_bfd, |
1747 | look->bfd_section, | |
390fbbf1 | 1748 | sec->owner, sec)) |
ecca9871 L |
1749 | continue; |
1750 | } | |
d9d94ac8 AM |
1751 | differ = look_flags ^ sec_flags; |
1752 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1753 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1754 | found = look; |
1755 | } | |
afd7a018 | 1756 | } |
d9d94ac8 | 1757 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1758 | { |
07890c07 | 1759 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1760 | for (look = first; look; look = look->next) |
ecca9871 | 1761 | { |
d9d94ac8 | 1762 | look_flags = look->flags; |
390fbbf1 AM |
1763 | if (look->bfd_section != NULL) |
1764 | { | |
d9d94ac8 | 1765 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1766 | if (match_type && !match_type (link_info.output_bfd, |
1767 | look->bfd_section, | |
390fbbf1 AM |
1768 | sec->owner, sec)) |
1769 | continue; | |
1770 | } | |
d9d94ac8 AM |
1771 | differ = look_flags ^ sec_flags; |
1772 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1773 | found = look; |
ecca9871 | 1774 | } |
afd7a018 | 1775 | } |
07890c07 AM |
1776 | else |
1777 | { | |
1778 | /* non-alloc go last. */ | |
1779 | for (look = first; look; look = look->next) | |
1780 | { | |
d9d94ac8 | 1781 | look_flags = look->flags; |
07890c07 | 1782 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1783 | look_flags = look->bfd_section->flags; |
1784 | differ = look_flags ^ sec_flags; | |
1785 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1786 | found = look; |
1787 | } | |
1788 | return found; | |
1789 | } | |
afd7a018 | 1790 | |
390fbbf1 AM |
1791 | if (found || !match_type) |
1792 | return found; | |
1793 | ||
93638471 | 1794 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
1795 | } |
1796 | ||
1797 | /* Find the last output section before given output statement. | |
1798 | Used by place_orphan. */ | |
1799 | ||
1800 | static asection * | |
1801 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1802 | { | |
afd7a018 AM |
1803 | lang_output_section_statement_type *lookup; |
1804 | ||
218868ba | 1805 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1806 | { |
66c103b7 | 1807 | if (lookup->constraint < 0) |
afd7a018 | 1808 | continue; |
afd7a018 | 1809 | |
18e7a658 | 1810 | if (lookup->bfd_section != NULL) |
218868ba | 1811 | return lookup->bfd_section; |
afd7a018 AM |
1812 | } |
1813 | ||
1814 | return NULL; | |
1815 | } | |
1816 | ||
53d25da6 AM |
1817 | /* Look for a suitable place for a new output section statement. The |
1818 | idea is to skip over anything that might be inside a SECTIONS {} | |
1819 | statement in a script, before we find another output section | |
1820 | statement. Assignments to "dot" before an output section statement | |
0fa4d7cf AM |
1821 | are assumed to belong to it, except in two cases; The first |
1822 | assignment to dot, and assignments before non-alloc sections. | |
1823 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1824 | similar assignments that set the initial address, or we might | |
1825 | insert non-alloc note sections among assignments setting end of | |
1826 | image symbols. */ | |
53d25da6 AM |
1827 | |
1828 | static lang_statement_union_type ** | |
18e7a658 | 1829 | insert_os_after (lang_statement_union_type *after) |
53d25da6 AM |
1830 | { |
1831 | lang_statement_union_type **where; | |
1832 | lang_statement_union_type **assign = NULL; | |
f38a2680 | 1833 | bool ignore_first; |
53d25da6 | 1834 | |
18e7a658 | 1835 | ignore_first = after == lang_os_list.head; |
53d25da6 AM |
1836 | |
1837 | for (where = &after->header.next; | |
1838 | *where != NULL; | |
1839 | where = &(*where)->header.next) | |
1840 | { | |
1841 | switch ((*where)->header.type) | |
1842 | { | |
1843 | case lang_assignment_statement_enum: | |
1844 | if (assign == NULL) | |
1845 | { | |
1846 | lang_assignment_statement_type *ass; | |
1847 | ||
1848 | ass = &(*where)->assignment_statement; | |
1849 | if (ass->exp->type.node_class != etree_assert | |
1850 | && ass->exp->assign.dst[0] == '.' | |
73589426 AM |
1851 | && ass->exp->assign.dst[1] == 0) |
1852 | { | |
1853 | if (!ignore_first) | |
1854 | assign = where; | |
f38a2680 | 1855 | ignore_first = false; |
73589426 | 1856 | } |
53d25da6 | 1857 | } |
53d25da6 AM |
1858 | continue; |
1859 | case lang_wild_statement_enum: | |
1860 | case lang_input_section_enum: | |
1861 | case lang_object_symbols_statement_enum: | |
1862 | case lang_fill_statement_enum: | |
1863 | case lang_data_statement_enum: | |
1864 | case lang_reloc_statement_enum: | |
1865 | case lang_padding_statement_enum: | |
1866 | case lang_constructors_statement_enum: | |
1867 | assign = NULL; | |
f38a2680 | 1868 | ignore_first = false; |
53d25da6 AM |
1869 | continue; |
1870 | case lang_output_section_statement_enum: | |
1871 | if (assign != NULL) | |
0fa4d7cf AM |
1872 | { |
1873 | asection *s = (*where)->output_section_statement.bfd_section; | |
1874 | ||
249b2a84 L |
1875 | if (s == NULL |
1876 | || s->map_head.s == NULL | |
1877 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1878 | where = assign; |
1879 | } | |
53d25da6 AM |
1880 | break; |
1881 | case lang_input_statement_enum: | |
1882 | case lang_address_statement_enum: | |
1883 | case lang_target_statement_enum: | |
1884 | case lang_output_statement_enum: | |
1885 | case lang_group_statement_enum: | |
1886 | case lang_insert_statement_enum: | |
1887 | continue; | |
43ae96e9 MM |
1888 | case lang_input_matcher_enum: |
1889 | FAIL (); | |
53d25da6 AM |
1890 | } |
1891 | break; | |
1892 | } | |
1893 | ||
1894 | return where; | |
1895 | } | |
1896 | ||
afd7a018 | 1897 | lang_output_section_statement_type * |
7b986e99 | 1898 | lang_insert_orphan (asection *s, |
afd7a018 | 1899 | const char *secname, |
8a99a385 | 1900 | int constraint, |
afd7a018 AM |
1901 | lang_output_section_statement_type *after, |
1902 | struct orphan_save *place, | |
1903 | etree_type *address, | |
1904 | lang_statement_list_type *add_child) | |
1905 | { | |
afd7a018 | 1906 | lang_statement_list_type add; |
afd7a018 AM |
1907 | lang_output_section_statement_type *os; |
1908 | lang_output_section_statement_type **os_tail; | |
1909 | ||
afd7a018 AM |
1910 | /* If we have found an appropriate place for the output section |
1911 | statements for this orphan, add them to our own private list, | |
1912 | inserting them later into the global statement list. */ | |
1913 | if (after != NULL) | |
1914 | { | |
bde18da4 AM |
1915 | lang_list_init (&add); |
1916 | push_stat_ptr (&add); | |
afd7a018 AM |
1917 | } |
1918 | ||
0e1862bb L |
1919 | if (bfd_link_relocatable (&link_info) |
1920 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
1921 | address = exp_intop (0); |
1922 | ||
5c1e6d53 | 1923 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
c212f39d FS |
1924 | os = lang_enter_output_section_statement ( |
1925 | secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); | |
011aa75f | 1926 | |
afd7a018 AM |
1927 | if (add_child == NULL) |
1928 | add_child = &os->children; | |
b209b5a6 | 1929 | lang_add_section (add_child, s, NULL, NULL, os); |
afd7a018 | 1930 | |
f77c3948 DJ |
1931 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
1932 | { | |
1933 | const char *region = (after->region | |
1934 | ? after->region->name_list.name | |
1935 | : DEFAULT_MEMORY_REGION); | |
1936 | const char *lma_region = (after->lma_region | |
1937 | ? after->lma_region->name_list.name | |
1938 | : NULL); | |
1939 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
1940 | lma_region); | |
1941 | } | |
1942 | else | |
1943 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
1944 | NULL); | |
afd7a018 | 1945 | |
afd7a018 AM |
1946 | /* Restore the global list pointer. */ |
1947 | if (after != NULL) | |
bde18da4 | 1948 | pop_stat_ptr (); |
afd7a018 AM |
1949 | |
1950 | if (after != NULL && os->bfd_section != NULL) | |
1951 | { | |
5daa8fe7 | 1952 | asection *snew, *as; |
f38a2680 AM |
1953 | bool place_after = place->stmt == NULL; |
1954 | bool insert_after = true; | |
afd7a018 AM |
1955 | |
1956 | snew = os->bfd_section; | |
1957 | ||
1958 | /* Shuffle the bfd section list to make the output file look | |
1959 | neater. This is really only cosmetic. */ | |
1960 | if (place->section == NULL | |
8ce18f9c | 1961 | && after != (void *) lang_os_list.head) |
afd7a018 AM |
1962 | { |
1963 | asection *bfd_section = after->bfd_section; | |
1964 | ||
1965 | /* If the output statement hasn't been used to place any input | |
1966 | sections (and thus doesn't have an output bfd_section), | |
1967 | look for the closest prior output statement having an | |
1968 | output section. */ | |
1969 | if (bfd_section == NULL) | |
1970 | bfd_section = output_prev_sec_find (after); | |
1971 | ||
18e7a658 AM |
1972 | if (bfd_section != NULL |
1973 | && bfd_section->owner != NULL | |
1974 | && bfd_section != snew) | |
afd7a018 AM |
1975 | place->section = &bfd_section->next; |
1976 | } | |
1977 | ||
1978 | if (place->section == NULL) | |
f13a99db | 1979 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1980 | |
5daa8fe7 | 1981 | as = *place->section; |
5e542ba7 MS |
1982 | |
1983 | if (!as) | |
329c1c86 AM |
1984 | { |
1985 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1986 | |
1987 | /* Unlink the section. */ | |
f13a99db | 1988 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1989 | |
1990 | /* Now tack it back on in the right place. */ | |
f13a99db | 1991 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1992 | } |
1887ae73 L |
1993 | else if ((bfd_get_flavour (link_info.output_bfd) |
1994 | == bfd_target_elf_flavour) | |
1995 | && (bfd_get_flavour (s->owner) | |
1996 | == bfd_target_elf_flavour) | |
1997 | && ((elf_section_type (s) == SHT_NOTE | |
1998 | && (s->flags & SEC_LOAD) != 0) | |
1999 | || (elf_section_type (as) == SHT_NOTE | |
2000 | && (as->flags & SEC_LOAD) != 0))) | |
2001 | { | |
2002 | /* Make sure that output note sections are grouped and sorted | |
2003 | by alignments when inserting a note section or insert a | |
2004 | section after a note section, */ | |
2005 | asection *sec; | |
2006 | /* A specific section after which the output note section | |
2007 | should be placed. */ | |
2008 | asection *after_sec; | |
2009 | /* True if we need to insert the orphan section after a | |
2010 | specific section to maintain output note section order. */ | |
f38a2680 | 2011 | bool after_sec_note = false; |
f3e660db L |
2012 | |
2013 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
2014 | |
2015 | /* Group and sort output note section by alignments in | |
2016 | ascending order. */ | |
2017 | after_sec = NULL; | |
2018 | if (elf_section_type (s) == SHT_NOTE | |
2019 | && (s->flags & SEC_LOAD) != 0) | |
2020 | { | |
f3e660db L |
2021 | /* Search from the beginning for the last output note |
2022 | section with equal or larger alignments. NB: Don't | |
2023 | place orphan note section after non-note sections. */ | |
1887ae73 | 2024 | |
f3e660db L |
2025 | first_orphan_note = NULL; |
2026 | for (sec = link_info.output_bfd->sections; | |
1887ae73 L |
2027 | (sec != NULL |
2028 | && !bfd_is_abs_section (sec)); | |
2029 | sec = sec->next) | |
2030 | if (sec != snew | |
2031 | && elf_section_type (sec) == SHT_NOTE | |
2032 | && (sec->flags & SEC_LOAD) != 0) | |
2033 | { | |
f3e660db L |
2034 | if (!first_orphan_note) |
2035 | first_orphan_note = sec; | |
1887ae73 L |
2036 | if (sec->alignment_power >= s->alignment_power) |
2037 | after_sec = sec; | |
2038 | } | |
f3e660db L |
2039 | else if (first_orphan_note) |
2040 | { | |
2041 | /* Stop if there is non-note section after the first | |
2042 | orphan note section. */ | |
2043 | break; | |
2044 | } | |
1887ae73 | 2045 | |
f3e660db L |
2046 | /* If this will be the first orphan note section, it can |
2047 | be placed at the default location. */ | |
2048 | after_sec_note = first_orphan_note != NULL; | |
2049 | if (after_sec == NULL && after_sec_note) | |
1887ae73 | 2050 | { |
f3e660db L |
2051 | /* If all output note sections have smaller |
2052 | alignments, place the section before all | |
2053 | output orphan note sections. */ | |
2054 | after_sec = first_orphan_note; | |
f38a2680 | 2055 | insert_after = false; |
1887ae73 L |
2056 | } |
2057 | } | |
f3e660db | 2058 | else if (first_orphan_note) |
1887ae73 | 2059 | { |
f3e660db L |
2060 | /* Don't place non-note sections in the middle of orphan |
2061 | note sections. */ | |
f38a2680 | 2062 | after_sec_note = true; |
1887ae73 L |
2063 | after_sec = as; |
2064 | for (sec = as->next; | |
2065 | (sec != NULL | |
2066 | && !bfd_is_abs_section (sec)); | |
2067 | sec = sec->next) | |
2068 | if (elf_section_type (sec) == SHT_NOTE | |
2069 | && (sec->flags & SEC_LOAD) != 0) | |
2070 | after_sec = sec; | |
2071 | } | |
2072 | ||
2073 | if (after_sec_note) | |
2074 | { | |
2075 | if (after_sec) | |
2076 | { | |
f3e660db L |
2077 | /* Search forward to insert OS after AFTER_SEC output |
2078 | statement. */ | |
2079 | lang_output_section_statement_type *stmt, *next; | |
f38a2680 | 2080 | bool found = false; |
f3e660db L |
2081 | for (stmt = after; stmt != NULL; stmt = next) |
2082 | { | |
2083 | next = stmt->next; | |
2084 | if (insert_after) | |
2085 | { | |
2086 | if (stmt->bfd_section == after_sec) | |
2087 | { | |
f38a2680 AM |
2088 | place_after = true; |
2089 | found = true; | |
f3e660db L |
2090 | after = stmt; |
2091 | break; | |
2092 | } | |
2093 | } | |
2094 | else | |
2095 | { | |
2096 | /* If INSERT_AFTER is FALSE, place OS before | |
2097 | AFTER_SEC output statement. */ | |
2098 | if (next && next->bfd_section == after_sec) | |
2099 | { | |
f38a2680 AM |
2100 | place_after = true; |
2101 | found = true; | |
f3e660db L |
2102 | after = stmt; |
2103 | break; | |
2104 | } | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | /* Search backward to insert OS after AFTER_SEC output | |
2109 | statement. */ | |
2110 | if (!found) | |
2111 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
1887ae73 | 2112 | { |
f3e660db L |
2113 | if (insert_after) |
2114 | { | |
2115 | if (stmt->bfd_section == after_sec) | |
2116 | { | |
f38a2680 | 2117 | place_after = true; |
f3e660db L |
2118 | after = stmt; |
2119 | break; | |
2120 | } | |
2121 | } | |
2122 | else | |
2123 | { | |
2124 | /* If INSERT_AFTER is FALSE, place OS before | |
2125 | AFTER_SEC output statement. */ | |
2126 | if (stmt->next->bfd_section == after_sec) | |
2127 | { | |
f38a2680 | 2128 | place_after = true; |
f3e660db L |
2129 | after = stmt; |
2130 | break; | |
2131 | } | |
2132 | } | |
1887ae73 L |
2133 | } |
2134 | } | |
2135 | ||
f3e660db L |
2136 | if (after_sec == NULL |
2137 | || (insert_after && after_sec->next != snew) | |
2138 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
2139 | { |
2140 | /* Unlink the section. */ | |
2141 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2142 | ||
2143 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2144 | prepend SNEW. */ | |
2145 | if (after_sec) | |
f3e660db L |
2146 | { |
2147 | if (insert_after) | |
2148 | bfd_section_list_insert_after (link_info.output_bfd, | |
2149 | after_sec, snew); | |
2150 | else | |
2151 | bfd_section_list_insert_before (link_info.output_bfd, | |
2152 | after_sec, snew); | |
2153 | } | |
1887ae73 L |
2154 | else |
2155 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2156 | } | |
2157 | } | |
2158 | else if (as != snew && as->prev != snew) | |
2159 | { | |
2160 | /* Unlink the section. */ | |
2161 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2162 | ||
2163 | /* Now tack it back on in the right place. */ | |
2164 | bfd_section_list_insert_before (link_info.output_bfd, | |
2165 | as, snew); | |
2166 | } | |
2167 | } | |
5e542ba7 | 2168 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2169 | { |
2170 | /* Unlink the section. */ | |
f13a99db | 2171 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2172 | |
5daa8fe7 | 2173 | /* Now tack it back on in the right place. */ |
f13a99db | 2174 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2175 | } |
afd7a018 AM |
2176 | |
2177 | /* Save the end of this list. Further ophans of this type will | |
2178 | follow the one we've just added. */ | |
2179 | place->section = &snew->next; | |
2180 | ||
2181 | /* The following is non-cosmetic. We try to put the output | |
2182 | statements in some sort of reasonable order here, because they | |
2183 | determine the final load addresses of the orphan sections. | |
2184 | In addition, placing output statements in the wrong order may | |
2185 | require extra segments. For instance, given a typical | |
2186 | situation of all read-only sections placed in one segment and | |
2187 | following that a segment containing all the read-write | |
2188 | sections, we wouldn't want to place an orphan read/write | |
2189 | section before or amongst the read-only ones. */ | |
2190 | if (add.head != NULL) | |
2191 | { | |
2192 | lang_output_section_statement_type *newly_added_os; | |
2193 | ||
1887ae73 L |
2194 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2195 | if (place_after) | |
afd7a018 | 2196 | { |
18e7a658 | 2197 | lang_statement_union_type **where; |
afd7a018 | 2198 | |
18e7a658 | 2199 | where = insert_os_after ((lang_statement_union_type *) after); |
afd7a018 AM |
2200 | *add.tail = *where; |
2201 | *where = add.head; | |
2202 | ||
2203 | place->os_tail = &after->next; | |
2204 | } | |
2205 | else | |
2206 | { | |
2207 | /* Put it after the last orphan statement we added. */ | |
2208 | *add.tail = *place->stmt; | |
2209 | *place->stmt = add.head; | |
2210 | } | |
2211 | ||
2212 | /* Fix the global list pointer if we happened to tack our | |
2213 | new list at the tail. */ | |
bde18da4 AM |
2214 | if (*stat_ptr->tail == add.head) |
2215 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
2216 | |
2217 | /* Save the end of this list. */ | |
2218 | place->stmt = add.tail; | |
2219 | ||
2220 | /* Do the same for the list of output section statements. */ | |
2221 | newly_added_os = *os_tail; | |
2222 | *os_tail = NULL; | |
218868ba AM |
2223 | newly_added_os->prev = (lang_output_section_statement_type *) |
2224 | ((char *) place->os_tail | |
2225 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2226 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2227 | if (newly_added_os->next != NULL) |
2228 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
2229 | *place->os_tail = newly_added_os; |
2230 | place->os_tail = &newly_added_os->next; | |
2231 | ||
2232 | /* Fixing the global list pointer here is a little different. | |
2233 | We added to the list in lang_enter_output_section_statement, | |
2234 | trimmed off the new output_section_statment above when | |
2235 | assigning *os_tail = NULL, but possibly added it back in | |
2236 | the same place when assigning *place->os_tail. */ | |
2237 | if (*os_tail == NULL) | |
5c1e6d53 | 2238 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2239 | } |
2240 | } | |
2241 | return os; | |
2242 | } | |
2243 | ||
16e4ecc0 AM |
2244 | static void |
2245 | lang_print_asneeded (void) | |
2246 | { | |
2247 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2248 | |
2249 | if (asneeded_list_head == NULL) | |
2250 | return; | |
2251 | ||
1273da04 | 2252 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2253 | |
2254 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2255 | { | |
6b9bd54c | 2256 | int len; |
16e4ecc0 AM |
2257 | |
2258 | minfo ("%s", m->soname); | |
2259 | len = strlen (m->soname); | |
2260 | ||
2261 | if (len >= 29) | |
2262 | { | |
2263 | print_nl (); | |
2264 | len = 0; | |
2265 | } | |
6b9bd54c | 2266 | print_spaces (30 - len); |
16e4ecc0 AM |
2267 | |
2268 | if (m->ref != NULL) | |
871b3ab2 | 2269 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2270 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2271 | } |
2272 | } | |
2273 | ||
252b5132 | 2274 | static void |
1579bae1 | 2275 | lang_map_flags (flagword flag) |
252b5132 RH |
2276 | { |
2277 | if (flag & SEC_ALLOC) | |
2278 | minfo ("a"); | |
2279 | ||
2280 | if (flag & SEC_CODE) | |
2281 | minfo ("x"); | |
2282 | ||
2283 | if (flag & SEC_READONLY) | |
2284 | minfo ("r"); | |
2285 | ||
2286 | if (flag & SEC_DATA) | |
2287 | minfo ("w"); | |
2288 | ||
2289 | if (flag & SEC_LOAD) | |
2290 | minfo ("l"); | |
2291 | } | |
2292 | ||
2293 | void | |
1579bae1 | 2294 | lang_map (void) |
252b5132 RH |
2295 | { |
2296 | lang_memory_region_type *m; | |
f38a2680 | 2297 | bool dis_header_printed = false; |
252b5132 | 2298 | |
fb221fba NC |
2299 | ldfile_print_input_remaps (); |
2300 | ||
4d4920ec EB |
2301 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2302 | { | |
2303 | asection *s; | |
2304 | ||
2305 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2306 | || file->flags.just_syms) |
4d4920ec EB |
2307 | continue; |
2308 | ||
035801ce FS |
2309 | if (config.print_map_discarded) |
2310 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2311 | if ((s->output_section == NULL | |
2312 | || s->output_section->owner != link_info.output_bfd) | |
2313 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2314 | { | |
2315 | if (! dis_header_printed) | |
2316 | { | |
496917ce | 2317 | minfo (_("\nDiscarded input sections\n\n")); |
f38a2680 | 2318 | dis_header_printed = true; |
035801ce | 2319 | } |
4d4920ec | 2320 | |
f38a2680 | 2321 | print_input_section (s, true); |
035801ce | 2322 | } |
4d4920ec | 2323 | } |
496917ce NC |
2324 | if (config.print_map_discarded && ! dis_header_printed) |
2325 | minfo (_("\nThere are no discarded input sections\n")); | |
4d4920ec | 2326 | |
252b5132 RH |
2327 | minfo (_("\nMemory Configuration\n\n")); |
2328 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2329 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2330 | ||
1579bae1 | 2331 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 | 2332 | { |
6b9bd54c | 2333 | fprintf (config.map_file, "%-16s", m->name_list.name); |
252b5132 | 2334 | |
6b9bd54c AM |
2335 | char buf[32]; |
2336 | bfd_sprintf_vma (link_info.output_bfd, buf, m->origin); | |
2337 | fprintf (config.map_file, " 0x%-16s", buf); | |
2338 | bfd_sprintf_vma (link_info.output_bfd, buf, m->length); | |
2339 | fprintf (config.map_file, | |
2340 | " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf); | |
2341 | if (m->flags) | |
2342 | lang_map_flags (m->flags); | |
252b5132 | 2343 | |
6b9bd54c AM |
2344 | if (m->not_flags) |
2345 | { | |
2346 | minfo ("!"); | |
2347 | lang_map_flags (m->not_flags); | |
252b5132 RH |
2348 | } |
2349 | ||
2350 | print_nl (); | |
2351 | } | |
2352 | ||
496917ce | 2353 | minfo (_("\nLinker script and memory map\n\n")); |
252b5132 | 2354 | |
0aa7f586 | 2355 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2356 | { |
2357 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2358 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2359 | } | |
6a846243 | 2360 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2361 | lang_statement_iteration++; |
252b5132 | 2362 | print_statements (); |
7a2f2d82 | 2363 | |
0aa7f586 AM |
2364 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2365 | config.map_file); | |
252b5132 RH |
2366 | } |
2367 | ||
f38a2680 | 2368 | static bool |
967928e9 AM |
2369 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2370 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2371 | { |
6fcc66ab AM |
2372 | if ((hash_entry->type == bfd_link_hash_defined |
2373 | || hash_entry->type == bfd_link_hash_defweak) | |
2374 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2375 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2376 | { |
6fcc66ab | 2377 | input_section_userdata_type *ud; |
35835446 JR |
2378 | struct map_symbol_def *def; |
2379 | ||
a48931cc | 2380 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2381 | if (!ud) |
35835446 | 2382 | { |
988de25b | 2383 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2384 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2385 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2386 | ud->map_symbol_def_count = 0; | |
35835446 | 2387 | } |
6fcc66ab | 2388 | else if (!ud->map_symbol_def_tail) |
35835446 | 2389 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2390 | |
1e9cc1c2 | 2391 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2392 | def->entry = hash_entry; |
76d7af2d | 2393 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2394 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2395 | ud->map_symbol_def_count++; |
35835446 | 2396 | } |
f38a2680 | 2397 | return true; |
35835446 JR |
2398 | } |
2399 | ||
252b5132 RH |
2400 | /* Initialize an output section. */ |
2401 | ||
2402 | static void | |
dfa7b0b8 | 2403 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2404 | { |
252b5132 | 2405 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
df5f2391 | 2406 | einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2407 | |
de34d428 | 2408 | if (!s->dup_output) |
8a99a385 | 2409 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); |
1579bae1 | 2410 | if (s->bfd_section == NULL) |
8a99a385 AM |
2411 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2412 | s->name, flags); | |
1579bae1 | 2413 | if (s->bfd_section == NULL) |
252b5132 | 2414 | { |
df5f2391 | 2415 | einfo (_("%F%P: output format %s cannot represent section" |
0aa7f586 | 2416 | " called %s: %E\n"), |
f13a99db | 2417 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2418 | } |
2419 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2420 | s->bfd_section->output_offset = 0; |
e0f6802f | 2421 | |
24ef1aa7 GM |
2422 | /* Set the userdata of the output section to the output section |
2423 | statement to avoid lookup. */ | |
a48931cc | 2424 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2425 | |
a431bc2e AM |
2426 | /* If there is a base address, make sure that any sections it might |
2427 | mention are initialized. */ | |
2428 | if (s->addr_tree != NULL) | |
2429 | exp_init_os (s->addr_tree); | |
2430 | ||
2431 | if (s->load_base != NULL) | |
2432 | exp_init_os (s->load_base); | |
2433 | ||
7270c5ed | 2434 | /* If supplied an alignment, set it. */ |
3d9c8f6b AM |
2435 | if (s->section_alignment != NULL) |
2436 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, | |
2437 | "section alignment"); | |
252b5132 RH |
2438 | } |
2439 | ||
2440 | /* Make sure that all output sections mentioned in an expression are | |
2441 | initialized. */ | |
2442 | ||
2443 | static void | |
1579bae1 | 2444 | exp_init_os (etree_type *exp) |
252b5132 RH |
2445 | { |
2446 | switch (exp->type.node_class) | |
2447 | { | |
2448 | case etree_assign: | |
9f4fb502 | 2449 | case etree_provide: |
eab62f2f | 2450 | case etree_provided: |
252b5132 RH |
2451 | exp_init_os (exp->assign.src); |
2452 | break; | |
2453 | ||
2454 | case etree_binary: | |
2455 | exp_init_os (exp->binary.lhs); | |
2456 | exp_init_os (exp->binary.rhs); | |
2457 | break; | |
2458 | ||
2459 | case etree_trinary: | |
2460 | exp_init_os (exp->trinary.cond); | |
2461 | exp_init_os (exp->trinary.lhs); | |
2462 | exp_init_os (exp->trinary.rhs); | |
2463 | break; | |
2464 | ||
b6ca8815 NS |
2465 | case etree_assert: |
2466 | exp_init_os (exp->assert_s.child); | |
2467 | break; | |
afd7a018 | 2468 | |
252b5132 RH |
2469 | case etree_unary: |
2470 | exp_init_os (exp->unary.child); | |
2471 | break; | |
2472 | ||
2473 | case etree_name: | |
2474 | switch (exp->type.node_code) | |
2475 | { | |
2476 | case ADDR: | |
2477 | case LOADADDR: | |
252b5132 RH |
2478 | { |
2479 | lang_output_section_statement_type *os; | |
2480 | ||
2481 | os = lang_output_section_find (exp->name.name); | |
2482 | if (os != NULL && os->bfd_section == NULL) | |
dfa7b0b8 | 2483 | init_os (os, 0); |
252b5132 RH |
2484 | } |
2485 | } | |
2486 | break; | |
2487 | ||
2488 | default: | |
2489 | break; | |
2490 | } | |
2491 | } | |
9503fd87 | 2492 | \f |
252b5132 | 2493 | static void |
1579bae1 | 2494 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2495 | { |
1e9cc1c2 | 2496 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2497 | |
2498 | /* If we are only reading symbols from this object, then we want to | |
2499 | discard all sections. */ | |
66be1055 | 2500 | if (entry->flags.just_syms) |
252b5132 | 2501 | { |
1449d79b | 2502 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2503 | return; |
2504 | } | |
2505 | ||
2e84f9c1 AM |
2506 | /* Deal with SHF_EXCLUDE ELF sections. */ |
2507 | if (!bfd_link_relocatable (&link_info) | |
2508 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2509 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2510 | sec->output_section = bfd_abs_section_ptr; | |
2511 | ||
ace66bb2 | 2512 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2513 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2514 | } |
2515 | \f | |
5b5f4e6f AB |
2516 | |
2517 | /* Returns true if SECTION is one we know will be discarded based on its | |
2518 | section flags, otherwise returns false. */ | |
2519 | ||
f38a2680 | 2520 | static bool |
5b5f4e6f AB |
2521 | lang_discard_section_p (asection *section) |
2522 | { | |
f38a2680 | 2523 | bool discard; |
5b5f4e6f AB |
2524 | flagword flags = section->flags; |
2525 | ||
2526 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2527 | discard = (flags & SEC_EXCLUDE) != 0; | |
2528 | ||
2529 | /* Discard the group descriptor sections when we're finally placing the | |
2530 | sections from within the group. */ | |
2531 | if ((flags & SEC_GROUP) != 0 | |
2532 | && link_info.resolve_section_groups) | |
f38a2680 | 2533 | discard = true; |
5b5f4e6f AB |
2534 | |
2535 | /* Discard debugging sections if we are stripping debugging | |
2536 | information. */ | |
2537 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2538 | && (flags & SEC_DEBUGGING) != 0) | |
f38a2680 | 2539 | discard = true; |
5b5f4e6f AB |
2540 | |
2541 | return discard; | |
2542 | } | |
2543 | ||
670c91c0 MM |
2544 | /* Return TRUE if SECTION is never going to be added to output statement |
2545 | OUTPUT. lang_add_section() definitely won't do anything with SECTION | |
2546 | if this returns TRUE. It may do something (or not) if this returns FALSE. | |
252b5132 | 2547 | |
670c91c0 MM |
2548 | Can be used as early-out to filter matches. This may set |
2549 | output_section of SECTION, if it was unset, to the abs section in case | |
2550 | we discover SECTION to be always discarded. This may also give | |
2551 | warning messages. */ | |
252b5132 | 2552 | |
670c91c0 MM |
2553 | static bool |
2554 | wont_add_section_p (asection *section, | |
2555 | lang_output_section_statement_type *output) | |
252b5132 | 2556 | { |
f38a2680 | 2557 | bool discard; |
252b5132 | 2558 | |
5b5f4e6f AB |
2559 | /* Is this section one we know should be discarded? */ |
2560 | discard = lang_discard_section_p (section); | |
252b5132 RH |
2561 | |
2562 | /* Discard input sections which are assigned to a section named | |
2563 | DISCARD_SECTION_NAME. */ | |
2564 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
f38a2680 | 2565 | discard = true; |
252b5132 | 2566 | |
252b5132 RH |
2567 | if (discard) |
2568 | { | |
2569 | if (section->output_section == NULL) | |
2570 | { | |
670c91c0 MM |
2571 | /* This prevents future calls from assigning this section or |
2572 | warning about it again. */ | |
252b5132 RH |
2573 | section->output_section = bfd_abs_section_ptr; |
2574 | } | |
670c91c0 MM |
2575 | else if (bfd_is_abs_section (section->output_section)) |
2576 | ; | |
abf874aa CL |
2577 | else if (link_info.non_contiguous_regions_warnings) |
2578 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
c63d4862 | 2579 | "section `%pA' from `%pB' match /DISCARD/ clause.\n"), |
abf874aa CL |
2580 | NULL, section, section->owner); |
2581 | ||
670c91c0 | 2582 | return true; |
ae17ab41 CM |
2583 | } |
2584 | ||
dfa7b0b8 | 2585 | if (section->output_section != NULL) |
abf874aa CL |
2586 | { |
2587 | if (!link_info.non_contiguous_regions) | |
670c91c0 | 2588 | return true; |
abf874aa CL |
2589 | |
2590 | /* SECTION has already been handled in a special way | |
2591 | (eg. LINK_ONCE): skip it. */ | |
2592 | if (bfd_is_abs_section (section->output_section)) | |
670c91c0 | 2593 | return true; |
abf874aa CL |
2594 | |
2595 | /* Already assigned to the same output section, do not process | |
2596 | it again, to avoid creating loops between duplicate sections | |
2597 | later. */ | |
2598 | if (section->output_section == output->bfd_section) | |
670c91c0 | 2599 | return true; |
abf874aa CL |
2600 | |
2601 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2602 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
c63d4862 | 2603 | "change behaviour for section `%pA' from `%pB' (assigned to " |
abf874aa CL |
2604 | "%pA, but additional match: %pA)\n"), |
2605 | NULL, section, section->owner, section->output_section, | |
2606 | output->bfd_section); | |
2607 | ||
2608 | /* SECTION has already been assigned to an output section, but | |
2609 | the user allows it to be mapped to another one in case it | |
2610 | overflows. We'll later update the actual output section in | |
2611 | size_input_section as appropriate. */ | |
2612 | } | |
252b5132 | 2613 | |
670c91c0 MM |
2614 | return false; |
2615 | } | |
2616 | ||
2617 | /* The wild routines. | |
2618 | ||
2619 | These expand statements like *(.text) and foo.o to a list of | |
2620 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2621 | foo.o(.text, .data). */ | |
2622 | ||
2623 | /* Add SECTION to the output section OUTPUT. Do this by creating a | |
2624 | lang_input_section statement which is placed at PTR. */ | |
2625 | ||
2626 | void | |
2627 | lang_add_section (lang_statement_list_type *ptr, | |
2628 | asection *section, | |
2629 | struct wildcard_list *pattern, | |
2630 | struct flag_info *sflag_info, | |
2631 | lang_output_section_statement_type *output) | |
2632 | { | |
2633 | flagword flags = section->flags; | |
2634 | ||
2635 | lang_input_section_type *new_section; | |
2636 | bfd *abfd = link_info.output_bfd; | |
2637 | ||
2638 | if (wont_add_section_p (section, output)) | |
2639 | return; | |
2640 | ||
2641 | if (sflag_info) | |
2642 | { | |
2643 | bool keep; | |
2644 | ||
2645 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); | |
2646 | if (!keep) | |
2647 | return; | |
2648 | } | |
2649 | ||
dfa7b0b8 AM |
2650 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
2651 | to an output section, because we want to be able to include a | |
2652 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2653 | section (I don't know why we want to do this, but we do). | |
2654 | build_link_order in ldwrite.c handles this case by turning | |
2655 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2656 | flags &= ~ SEC_NEVER_LOAD; | |
8423293d | 2657 | |
dfa7b0b8 AM |
2658 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2659 | already been processed. One reason to do this is that on pe | |
2660 | format targets, .text$foo sections go into .text and it's odd | |
2661 | to see .text with SEC_LINK_ONCE set. */ | |
7bdf4127 AB |
2662 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2663 | { | |
2664 | if (link_info.resolve_section_groups) | |
6c19b93b | 2665 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2666 | else |
6c19b93b | 2667 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2668 | } |
2669 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2670 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2671 | |
dfa7b0b8 AM |
2672 | switch (output->sectype) |
2673 | { | |
2674 | case normal_section: | |
2675 | case overlay_section: | |
7b243801 | 2676 | case first_overlay_section: |
c212f39d | 2677 | case type_section: |
dfa7b0b8 AM |
2678 | break; |
2679 | case noalloc_section: | |
2680 | flags &= ~SEC_ALLOC; | |
2681 | break; | |
c212f39d | 2682 | case typed_readonly_section: |
6b86da53 LB |
2683 | case readonly_section: |
2684 | flags |= SEC_READONLY; | |
2685 | break; | |
dfa7b0b8 AM |
2686 | case noload_section: |
2687 | flags &= ~SEC_LOAD; | |
2688 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
2689 | /* Unfortunately GNU ld has managed to evolve two different |
2690 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2691 | alloc, no contents section. All others get a noload, noalloc | |
2692 | section. */ | |
2693 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
2e76e85a | 2694 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2695 | else |
2696 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2697 | break; |
2698 | } | |
252b5132 | 2699 | |
dfa7b0b8 AM |
2700 | if (output->bfd_section == NULL) |
2701 | init_os (output, flags); | |
252b5132 | 2702 | |
dfa7b0b8 AM |
2703 | /* If SEC_READONLY is not set in the input section, then clear |
2704 | it from the output section. */ | |
2705 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
252b5132 | 2706 | |
dfa7b0b8 AM |
2707 | if (output->bfd_section->linker_has_input) |
2708 | { | |
2709 | /* Only set SEC_READONLY flag on the first input section. */ | |
2710 | flags &= ~ SEC_READONLY; | |
252b5132 | 2711 | |
f5fa8ca2 | 2712 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
2713 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
2714 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2715 | || ((flags & SEC_MERGE) != 0 | |
2716 | && output->bfd_section->entsize != section->entsize)) | |
f5fa8ca2 | 2717 | { |
afd7a018 | 2718 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2719 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2720 | } | |
dfa7b0b8 AM |
2721 | } |
2722 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2723 | |
dfa7b0b8 AM |
2724 | if (!output->bfd_section->linker_has_input) |
2725 | { | |
2726 | output->bfd_section->linker_has_input = 1; | |
2727 | /* This must happen after flags have been updated. The output | |
2728 | section may have been created before we saw its first input | |
2729 | section, eg. for a data statement. */ | |
2730 | bfd_init_private_section_data (section->owner, section, | |
2731 | link_info.output_bfd, | |
2732 | output->bfd_section, | |
2733 | &link_info); | |
2734 | if ((flags & SEC_MERGE) != 0) | |
afd7a018 | 2735 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2736 | } |
f5fa8ca2 | 2737 | |
dfa7b0b8 AM |
2738 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
2739 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2740 | { | |
2741 | /* FIXME: This value should really be obtained from the bfd... */ | |
2742 | output->block_value = 128; | |
2743 | } | |
252b5132 | 2744 | |
b02db378 AM |
2745 | /* When a .ctors section is placed in .init_array it must be copied |
2746 | in reverse order. Similarly for .dtors. Set that up. */ | |
2747 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
2748 | && ((startswith (section->name, ".ctors") | |
2749 | && strcmp (output->bfd_section->name, ".init_array") == 0) | |
2750 | || (startswith (section->name, ".dtors") | |
2751 | && strcmp (output->bfd_section->name, ".fini_array") == 0)) | |
2752 | && (section->name[6] == 0 || section->name[6] == '.')) | |
2753 | section->flags |= SEC_ELF_REVERSE_COPY; | |
2754 | ||
dfa7b0b8 AM |
2755 | if (section->alignment_power > output->bfd_section->alignment_power) |
2756 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2757 | |
dfa7b0b8 | 2758 | section->output_section = output->bfd_section; |
252b5132 | 2759 | |
5b69e357 | 2760 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
2761 | { |
2762 | asection *s = output->bfd_section->map_tail.s; | |
2763 | output->bfd_section->map_tail.s = section; | |
2764 | section->map_head.s = NULL; | |
2765 | section->map_tail.s = s; | |
2766 | if (s != NULL) | |
2767 | s->map_head.s = section; | |
2768 | else | |
2769 | output->bfd_section->map_head.s = section; | |
252b5132 | 2770 | } |
dfa7b0b8 AM |
2771 | |
2772 | /* Add a section reference to the list. */ | |
2773 | new_section = new_stat (lang_input_section, ptr); | |
2774 | new_section->section = section; | |
b209b5a6 | 2775 | new_section->pattern = pattern; |
252b5132 RH |
2776 | } |
2777 | ||
252b5132 | 2778 | /* Expand a wild statement for a particular FILE. SECTION may be |
af31506c MM |
2779 | NULL, in which case it is a wild card. This assumes that the |
2780 | wild statement doesn't need any sorting (of filenames or sections). */ | |
252b5132 RH |
2781 | |
2782 | static void | |
af31506c MM |
2783 | output_section_callback_nosort (lang_wild_statement_type *ptr, |
2784 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2785 | asection *section, | |
2786 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
2787 | void *output) | |
4dec4d4e | 2788 | { |
d0bf826b AM |
2789 | lang_output_section_statement_type *os; |
2790 | ||
2791 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2792 | |
b6bf44ba | 2793 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2794 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2795 | return; |
2796 | ||
af31506c MM |
2797 | lang_add_section (&ptr->children, section, ptr->section_list, |
2798 | ptr->section_flag_list, os); | |
252b5132 RH |
2799 | } |
2800 | ||
0841712e JJ |
2801 | /* Check if all sections in a wild statement for a particular FILE |
2802 | are readonly. */ | |
2803 | ||
2804 | static void | |
2805 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2806 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2807 | asection *section, | |
2808 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
d0bf826b | 2809 | void *output) |
0841712e | 2810 | { |
d0bf826b AM |
2811 | lang_output_section_statement_type *os; |
2812 | ||
2813 | os = (lang_output_section_statement_type *) output; | |
2814 | ||
0841712e | 2815 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2816 | if (unique_section_p (section, os)) |
0841712e JJ |
2817 | return; |
2818 | ||
6feb9908 | 2819 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
f38a2680 | 2820 | os->all_input_readonly = false; |
0841712e JJ |
2821 | } |
2822 | ||
252b5132 RH |
2823 | /* This is passed a file name which must have been seen already and |
2824 | added to the statement tree. We will see if it has been opened | |
2825 | already and had its symbols read. If not then we'll read it. */ | |
2826 | ||
2827 | static lang_input_statement_type * | |
1579bae1 | 2828 | lookup_name (const char *name) |
252b5132 RH |
2829 | { |
2830 | lang_input_statement_type *search; | |
2831 | ||
8ce18f9c | 2832 | for (search = (void *) input_file_chain.head; |
1579bae1 | 2833 | search != NULL; |
36983a93 | 2834 | search = search->next_real_file) |
252b5132 | 2835 | { |
0013291d NC |
2836 | /* Use the local_sym_name as the name of the file that has |
2837 | already been loaded as filename might have been transformed | |
2838 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2839 | const char *filename = search->local_sym_name; |
0013291d | 2840 | |
0013291d | 2841 | if (filename != NULL |
42627821 | 2842 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2843 | break; |
2844 | } | |
2845 | ||
1579bae1 | 2846 | if (search == NULL) |
1f1f5b92 AM |
2847 | { |
2848 | /* Arrange to splice the input statement added by new_afile into | |
2849 | statement_list after the current input_file_chain tail. | |
2850 | We know input_file_chain is not an empty list, and that | |
2851 | lookup_name was called via open_input_bfds. Later calls to | |
2852 | lookup_name should always match an existing input_statement. */ | |
2853 | lang_statement_union_type **tail = stat_ptr->tail; | |
2854 | lang_statement_union_type **after | |
2855 | = (void *) ((char *) input_file_chain.tail | |
2856 | - offsetof (lang_input_statement_type, next_real_file) | |
2857 | + offsetof (lang_input_statement_type, header.next)); | |
2858 | lang_statement_union_type *rest = *after; | |
2859 | stat_ptr->tail = after; | |
2860 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
16171946 | 2861 | default_target, NULL); |
1f1f5b92 AM |
2862 | *stat_ptr->tail = rest; |
2863 | if (*tail == NULL) | |
2864 | stat_ptr->tail = tail; | |
2865 | } | |
252b5132 RH |
2866 | |
2867 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 2868 | don't add this file. */ |
66be1055 | 2869 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
2870 | return search; |
2871 | ||
0aa7f586 | 2872 | if (!load_symbols (search, NULL)) |
6770ec8c | 2873 | return NULL; |
252b5132 RH |
2874 | |
2875 | return search; | |
2876 | } | |
2877 | ||
b58f81ae DJ |
2878 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2879 | ||
2880 | struct excluded_lib | |
2881 | { | |
2882 | char *name; | |
2883 | struct excluded_lib *next; | |
2884 | }; | |
2885 | static struct excluded_lib *excluded_libs; | |
2886 | ||
2887 | void | |
2888 | add_excluded_libs (const char *list) | |
2889 | { | |
2890 | const char *p = list, *end; | |
2891 | ||
2892 | while (*p != '\0') | |
2893 | { | |
2894 | struct excluded_lib *entry; | |
2895 | end = strpbrk (p, ",:"); | |
2896 | if (end == NULL) | |
2897 | end = p + strlen (p); | |
1e9cc1c2 | 2898 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
b58f81ae | 2899 | entry->next = excluded_libs; |
1e9cc1c2 | 2900 | entry->name = (char *) xmalloc (end - p + 1); |
b58f81ae DJ |
2901 | memcpy (entry->name, p, end - p); |
2902 | entry->name[end - p] = '\0'; | |
2903 | excluded_libs = entry; | |
2904 | if (*end == '\0') | |
329c1c86 | 2905 | break; |
b58f81ae DJ |
2906 | p = end + 1; |
2907 | } | |
2908 | } | |
2909 | ||
2910 | static void | |
2911 | check_excluded_libs (bfd *abfd) | |
2912 | { | |
2913 | struct excluded_lib *lib = excluded_libs; | |
2914 | ||
2915 | while (lib) | |
2916 | { | |
2917 | int len = strlen (lib->name); | |
607b4833 | 2918 | const char *filename = lbasename (bfd_get_filename (abfd)); |
b58f81ae DJ |
2919 | |
2920 | if (strcmp (lib->name, "ALL") == 0) | |
2921 | { | |
f38a2680 | 2922 | abfd->no_export = true; |
b58f81ae DJ |
2923 | return; |
2924 | } | |
2925 | ||
42627821 | 2926 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
2927 | && (filename[len] == '\0' |
2928 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
2929 | && filename[len + 2] == '\0'))) | |
2930 | { | |
f38a2680 | 2931 | abfd->no_export = true; |
b58f81ae DJ |
2932 | return; |
2933 | } | |
2934 | ||
2935 | lib = lib->next; | |
2936 | } | |
2937 | } | |
2938 | ||
252b5132 RH |
2939 | /* Get the symbols for an input file. */ |
2940 | ||
f38a2680 | 2941 | bool |
1579bae1 AM |
2942 | load_symbols (lang_input_statement_type *entry, |
2943 | lang_statement_list_type *place) | |
252b5132 RH |
2944 | { |
2945 | char **matching; | |
2946 | ||
66be1055 | 2947 | if (entry->flags.loaded) |
f38a2680 | 2948 | return true; |
252b5132 RH |
2949 | |
2950 | ldfile_open_file (entry); | |
2951 | ||
c4b78195 | 2952 | /* Do not process further if the file was missing. */ |
66be1055 | 2953 | if (entry->flags.missing_file) |
f38a2680 | 2954 | return true; |
c4b78195 | 2955 | |
727a29ba AM |
2956 | if (trace_files || verbose) |
2957 | info_msg ("%pI\n", entry); | |
2958 | ||
0aa7f586 AM |
2959 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
2960 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
2961 | { |
2962 | bfd_error_type err; | |
66be1055 | 2963 | struct lang_input_statement_flags save_flags; |
47e2e729 | 2964 | extern FILE *yyin; |
b7a26f91 | 2965 | |
252b5132 | 2966 | err = bfd_get_error (); |
884fb58e NC |
2967 | |
2968 | /* See if the emulation has some special knowledge. */ | |
2969 | if (ldemul_unrecognized_file (entry)) | |
370426d0 AM |
2970 | { |
2971 | if (err == bfd_error_file_ambiguously_recognized) | |
2972 | free (matching); | |
2973 | return true; | |
2974 | } | |
884fb58e | 2975 | |
252b5132 RH |
2976 | if (err == bfd_error_file_ambiguously_recognized) |
2977 | { | |
2978 | char **p; | |
2979 | ||
df5f2391 AM |
2980 | einfo (_("%P: %pB: file not recognized: %E;" |
2981 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
2982 | for (p = matching; *p != NULL; p++) |
2983 | einfo (" %s", *p); | |
370426d0 | 2984 | free (matching); |
252b5132 RH |
2985 | einfo ("%F\n"); |
2986 | } | |
2987 | else if (err != bfd_error_file_not_recognized | |
2988 | || place == NULL) | |
df5f2391 | 2989 | einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 2990 | |
252b5132 RH |
2991 | bfd_close (entry->the_bfd); |
2992 | entry->the_bfd = NULL; | |
2993 | ||
252b5132 | 2994 | /* Try to interpret the file as a linker script. */ |
66be1055 | 2995 | save_flags = input_flags; |
252b5132 RH |
2996 | ldfile_open_command_file (entry->filename); |
2997 | ||
bde18da4 | 2998 | push_stat_ptr (place); |
66be1055 AM |
2999 | input_flags.add_DT_NEEDED_for_regular |
3000 | = entry->flags.add_DT_NEEDED_for_regular; | |
3001 | input_flags.add_DT_NEEDED_for_dynamic | |
3002 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
3003 | input_flags.whole_archive = entry->flags.whole_archive; | |
3004 | input_flags.dynamic = entry->flags.dynamic; | |
252b5132 | 3005 | |
f38a2680 | 3006 | ldfile_assumed_script = true; |
252b5132 | 3007 | parser_input = input_script; |
16171946 | 3008 | current_input_file = entry->filename; |
252b5132 | 3009 | yyparse (); |
16171946 | 3010 | current_input_file = NULL; |
f38a2680 | 3011 | ldfile_assumed_script = false; |
252b5132 | 3012 | |
f4a23d42 AM |
3013 | /* missing_file is sticky. sysrooted will already have been |
3014 | restored when seeing EOF in yyparse, but no harm to restore | |
3015 | again. */ | |
66be1055 AM |
3016 | save_flags.missing_file |= input_flags.missing_file; |
3017 | input_flags = save_flags; | |
bde18da4 | 3018 | pop_stat_ptr (); |
47e2e729 AM |
3019 | fclose (yyin); |
3020 | yyin = NULL; | |
f38a2680 | 3021 | entry->flags.loaded = true; |
252b5132 | 3022 | |
f38a2680 | 3023 | return true; |
252b5132 RH |
3024 | } |
3025 | ||
3026 | if (ldemul_recognized_file (entry)) | |
f38a2680 | 3027 | return true; |
252b5132 RH |
3028 | |
3029 | /* We don't call ldlang_add_file for an archive. Instead, the | |
3030 | add_symbols entry point will call ldlang_add_file, via the | |
3031 | add_archive_element callback, for each element of the archive | |
3032 | which is used. */ | |
3033 | switch (bfd_get_format (entry->the_bfd)) | |
3034 | { | |
3035 | default: | |
3036 | break; | |
3037 | ||
3038 | case bfd_object: | |
66be1055 | 3039 | if (!entry->flags.reload) |
15fc2e13 | 3040 | ldlang_add_file (entry); |
252b5132 RH |
3041 | break; |
3042 | ||
3043 | case bfd_archive: | |
b58f81ae DJ |
3044 | check_excluded_libs (entry->the_bfd); |
3045 | ||
00f93c44 | 3046 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 3047 | if (entry->flags.whole_archive) |
252b5132 | 3048 | { |
b7a26f91 | 3049 | bfd *member = NULL; |
f38a2680 | 3050 | bool loaded = true; |
6770ec8c NC |
3051 | |
3052 | for (;;) | |
252b5132 | 3053 | { |
5d3236ee | 3054 | bfd *subsbfd; |
6770ec8c NC |
3055 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
3056 | ||
3057 | if (member == NULL) | |
3058 | break; | |
b7a26f91 | 3059 | |
0aa7f586 | 3060 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 3061 | { |
df5f2391 | 3062 | einfo (_("%F%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 3063 | entry->the_bfd, member); |
f38a2680 | 3064 | loaded = false; |
6770ec8c NC |
3065 | } |
3066 | ||
db0ca79f | 3067 | subsbfd = member; |
46105645 AM |
3068 | if (!(*link_info.callbacks |
3069 | ->add_archive_element) (&link_info, member, | |
3070 | "--whole-archive", &subsbfd)) | |
252b5132 | 3071 | abort (); |
6770ec8c | 3072 | |
5d3236ee DK |
3073 | /* Potentially, the add_archive_element hook may have set a |
3074 | substitute BFD for us. */ | |
46105645 | 3075 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 3076 | { |
df5f2391 | 3077 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), member); |
f38a2680 | 3078 | loaded = false; |
6770ec8c | 3079 | } |
252b5132 RH |
3080 | } |
3081 | ||
66be1055 | 3082 | entry->flags.loaded = loaded; |
6770ec8c | 3083 | return loaded; |
252b5132 | 3084 | } |
6770ec8c | 3085 | break; |
252b5132 RH |
3086 | } |
3087 | ||
03bdc404 | 3088 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
f38a2680 | 3089 | entry->flags.loaded = true; |
6770ec8c | 3090 | else |
df5f2391 | 3091 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3092 | |
66be1055 | 3093 | return entry->flags.loaded; |
252b5132 RH |
3094 | } |
3095 | ||
b6bf44ba AM |
3096 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
3097 | may be NULL, indicating that it is a wildcard. Separate | |
3098 | lang_input_section statements are created for each part of the | |
3099 | expansion; they are added after the wild statement S. OUTPUT is | |
3100 | the output section. */ | |
252b5132 RH |
3101 | |
3102 | static void | |
1579bae1 AM |
3103 | wild (lang_wild_statement_type *s, |
3104 | const char *target ATTRIBUTE_UNUSED, | |
3105 | lang_output_section_statement_type *output) | |
252b5132 | 3106 | { |
b6bf44ba | 3107 | struct wildcard_list *sec; |
252b5132 | 3108 | |
af31506c | 3109 | if (s->filenames_sorted || s->any_specs_sorted) |
50c77e5d | 3110 | { |
329c1c86 AM |
3111 | lang_section_bst_type *tree; |
3112 | ||
af31506c | 3113 | walk_wild (s, output_section_callback_sort, output); |
50c77e5d | 3114 | |
e6f2cbf5 | 3115 | tree = s->tree; |
329c1c86 | 3116 | if (tree) |
e6f2cbf5 L |
3117 | { |
3118 | output_section_callback_tree_to_list (s, tree, output); | |
3119 | s->tree = NULL; | |
af31506c | 3120 | s->rightmost = &s->tree; |
e6f2cbf5 | 3121 | } |
50c77e5d | 3122 | } |
329c1c86 | 3123 | else |
af31506c | 3124 | walk_wild (s, output_section_callback_nosort, output); |
b6bf44ba | 3125 | |
abe6ac95 AM |
3126 | if (default_common_section == NULL) |
3127 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
3128 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
3129 | { | |
3130 | /* Remember the section that common is going to in case we | |
b7a26f91 | 3131 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3132 | default_common_section = output; |
abe6ac95 | 3133 | break; |
b6bf44ba | 3134 | } |
252b5132 RH |
3135 | } |
3136 | ||
b34976b6 | 3137 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3138 | |
e50d8076 | 3139 | static int |
1579bae1 | 3140 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3141 | { |
1e9cc1c2 | 3142 | const char *sought = (const char *) data; |
5f992e62 | 3143 | |
e50d8076 NC |
3144 | return strcmp (target->name, sought) == 0; |
3145 | } | |
3146 | ||
3147 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3148 | |
e50d8076 | 3149 | static void |
1d38e9d1 | 3150 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3151 | { |
3152 | char c; | |
5f992e62 | 3153 | |
08da4cac | 3154 | while ((c = *src++) != 0) |
3882b010 | 3155 | *dest++ = TOLOWER (c); |
e50d8076 | 3156 | |
08da4cac | 3157 | *dest = 0; |
e50d8076 NC |
3158 | } |
3159 | ||
396a2467 | 3160 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3161 | from haystack. */ |
08da4cac | 3162 | |
e50d8076 | 3163 | static void |
1d38e9d1 | 3164 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3165 | { |
3166 | haystack = strstr (haystack, needle); | |
5f992e62 | 3167 | |
e50d8076 NC |
3168 | if (haystack) |
3169 | { | |
08da4cac | 3170 | char *src; |
e50d8076 | 3171 | |
08da4cac KH |
3172 | for (src = haystack + strlen (needle); *src;) |
3173 | *haystack++ = *src++; | |
5f992e62 | 3174 | |
08da4cac | 3175 | *haystack = 0; |
e50d8076 NC |
3176 | } |
3177 | } | |
3178 | ||
3179 | /* Compare two target format name strings. | |
3180 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3181 | |
e50d8076 | 3182 | static int |
1d38e9d1 | 3183 | name_compare (const char *first, const char *second) |
e50d8076 | 3184 | { |
08da4cac KH |
3185 | char *copy1; |
3186 | char *copy2; | |
3187 | int result; | |
5f992e62 | 3188 | |
1e9cc1c2 NC |
3189 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3190 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3191 | |
3192 | /* Convert the names to lower case. */ | |
3193 | stricpy (copy1, first); | |
3194 | stricpy (copy2, second); | |
3195 | ||
1579bae1 | 3196 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
3197 | strcut (copy1, "big"); |
3198 | strcut (copy1, "little"); | |
3199 | strcut (copy2, "big"); | |
3200 | strcut (copy2, "little"); | |
3201 | ||
3202 | /* Return a value based on how many characters match, | |
3203 | starting from the beginning. If both strings are | |
3204 | the same then return 10 * their length. */ | |
08da4cac KH |
3205 | for (result = 0; copy1[result] == copy2[result]; result++) |
3206 | if (copy1[result] == 0) | |
e50d8076 NC |
3207 | { |
3208 | result *= 10; | |
3209 | break; | |
3210 | } | |
5f992e62 | 3211 | |
e50d8076 NC |
3212 | free (copy1); |
3213 | free (copy2); | |
3214 | ||
3215 | return result; | |
3216 | } | |
3217 | ||
3218 | /* Set by closest_target_match() below. */ | |
08da4cac | 3219 | static const bfd_target *winner; |
e50d8076 NC |
3220 | |
3221 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3222 | requirement that was specified on the command line, and is the nearest | |
3223 | match to the original output target. */ | |
08da4cac | 3224 | |
e50d8076 | 3225 | static int |
1579bae1 | 3226 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3227 | { |
1e9cc1c2 | 3228 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3229 | |
08da4cac KH |
3230 | if (command_line.endian == ENDIAN_BIG |
3231 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3232 | return 0; |
5f992e62 | 3233 | |
08da4cac KH |
3234 | if (command_line.endian == ENDIAN_LITTLE |
3235 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3236 | return 0; |
3237 | ||
3238 | /* Must be the same flavour. */ | |
3239 | if (target->flavour != original->flavour) | |
3240 | return 0; | |
3241 | ||
de7dd2bd | 3242 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3243 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
3244 | || strcmp (target->name, "elf64-big") == 0 |
3245 | || strcmp (target->name, "elf32-little") == 0 | |
3246 | || strcmp (target->name, "elf64-little") == 0) | |
3247 | return 0; | |
3248 | ||
e50d8076 NC |
3249 | /* If we have not found a potential winner yet, then record this one. */ |
3250 | if (winner == NULL) | |
3251 | { | |
3252 | winner = target; | |
3253 | return 0; | |
3254 | } | |
3255 | ||
3256 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 3257 | Compare their names and choose the better one. */ |
d1778b88 AM |
3258 | if (name_compare (target->name, original->name) |
3259 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
3260 | winner = target; |
3261 | ||
3262 | /* Keep on searching until wqe have checked them all. */ | |
3263 | return 0; | |
3264 | } | |
3265 | ||
3266 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 3267 | |
1d38e9d1 | 3268 | static const char * |
1579bae1 | 3269 | get_first_input_target (void) |
e50d8076 | 3270 | { |
1d38e9d1 | 3271 | const char *target = NULL; |
e50d8076 NC |
3272 | |
3273 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3274 | { | |
3275 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3276 | && s->flags.real) |
e50d8076 NC |
3277 | { |
3278 | ldfile_open_file (s); | |
5f992e62 | 3279 | |
e50d8076 NC |
3280 | if (s->the_bfd != NULL |
3281 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3282 | { | |
3283 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3284 | |
e50d8076 NC |
3285 | if (target != NULL) |
3286 | break; | |
3287 | } | |
3288 | } | |
3289 | } | |
5f992e62 | 3290 | |
e50d8076 NC |
3291 | return target; |
3292 | } | |
3293 | ||
599917b8 | 3294 | const char * |
1579bae1 | 3295 | lang_get_output_target (void) |
599917b8 JJ |
3296 | { |
3297 | const char *target; | |
3298 | ||
3299 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3300 | if (output_target != NULL) |
599917b8 JJ |
3301 | return output_target; |
3302 | ||
3303 | /* No - has the current target been set to something other than | |
3304 | the default? */ | |
30824704 | 3305 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
3306 | return current_target; |
3307 | ||
3308 | /* No - can we determine the format of the first input file? */ | |
3309 | target = get_first_input_target (); | |
3310 | if (target != NULL) | |
3311 | return target; | |
3312 | ||
3313 | /* Failed - use the default output target. */ | |
3314 | return default_target; | |
3315 | } | |
3316 | ||
252b5132 RH |
3317 | /* Open the output file. */ |
3318 | ||
f13a99db | 3319 | static void |
1579bae1 | 3320 | open_output (const char *name) |
252b5132 | 3321 | { |
43908c16 AM |
3322 | lang_input_statement_type *f; |
3323 | char *out = lrealpath (name); | |
3324 | ||
3325 | for (f = (void *) input_file_chain.head; | |
3326 | f != NULL; | |
3327 | f = f->next_real_file) | |
3328 | if (f->flags.real) | |
3329 | { | |
da1ecf89 | 3330 | char *in = lrealpath (f->local_sym_name); |
43908c16 AM |
3331 | if (filename_cmp (in, out) == 0) |
3332 | einfo (_("%F%P: input file '%s' is the same as output file\n"), | |
3333 | f->filename); | |
3334 | free (in); | |
3335 | } | |
3336 | free (out); | |
3337 | ||
599917b8 | 3338 | output_target = lang_get_output_target (); |
5f992e62 | 3339 | |
08da4cac KH |
3340 | /* Has the user requested a particular endianness on the command |
3341 | line? */ | |
e50d8076 NC |
3342 | if (command_line.endian != ENDIAN_UNSET) |
3343 | { | |
e50d8076 | 3344 | /* Get the chosen target. */ |
4212b42d AM |
3345 | const bfd_target *target |
3346 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3347 | |
c13b1b77 NC |
3348 | /* If the target is not supported, we cannot do anything. */ |
3349 | if (target != NULL) | |
e50d8076 | 3350 | { |
4212b42d AM |
3351 | enum bfd_endian desired_endian; |
3352 | ||
c13b1b77 NC |
3353 | if (command_line.endian == ENDIAN_BIG) |
3354 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3355 | else |
c13b1b77 | 3356 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
3357 | |
3358 | /* See if the target has the wrong endianness. This should | |
3359 | not happen if the linker script has provided big and | |
3360 | little endian alternatives, but some scrips don't do | |
3361 | this. */ | |
c13b1b77 | 3362 | if (target->byteorder != desired_endian) |
e50d8076 | 3363 | { |
c13b1b77 NC |
3364 | /* If it does, then see if the target provides |
3365 | an alternative with the correct endianness. */ | |
3366 | if (target->alternative_target != NULL | |
3367 | && (target->alternative_target->byteorder == desired_endian)) | |
3368 | output_target = target->alternative_target->name; | |
e50d8076 | 3369 | else |
c13b1b77 | 3370 | { |
5f992e62 AM |
3371 | /* Try to find a target as similar as possible to |
3372 | the default target, but which has the desired | |
3373 | endian characteristic. */ | |
4212b42d AM |
3374 | bfd_iterate_over_targets (closest_target_match, |
3375 | (void *) target); | |
5f992e62 AM |
3376 | |
3377 | /* Oh dear - we could not find any targets that | |
3378 | satisfy our requirements. */ | |
c13b1b77 | 3379 | if (winner == NULL) |
6feb9908 AM |
3380 | einfo (_("%P: warning: could not find any targets" |
3381 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3382 | else |
3383 | output_target = winner->name; | |
3384 | } | |
e50d8076 NC |
3385 | } |
3386 | } | |
252b5132 | 3387 | } |
5f992e62 | 3388 | |
f13a99db | 3389 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3390 | |
f13a99db | 3391 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3392 | { |
3393 | if (bfd_get_error () == bfd_error_invalid_target) | |
df5f2391 | 3394 | einfo (_("%F%P: target %s not found\n"), output_target); |
e50d8076 | 3395 | |
df5f2391 | 3396 | einfo (_("%F%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3397 | } |
3398 | ||
f38a2680 | 3399 | delete_output_file_on_failure = true; |
252b5132 | 3400 | |
0aa7f586 | 3401 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
df5f2391 | 3402 | einfo (_("%F%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3403 | if (!bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
3404 | ldfile_output_architecture, |
3405 | ldfile_output_machine)) | |
df5f2391 | 3406 | einfo (_("%F%P: %s: can not set architecture: %E\n"), name); |
252b5132 | 3407 | |
f13a99db | 3408 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3409 | if (link_info.hash == NULL) |
df5f2391 | 3410 | einfo (_("%F%P: can not create hash table: %E\n")); |
252b5132 | 3411 | |
f13a99db | 3412 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3413 | } |
3414 | ||
252b5132 | 3415 | static void |
1579bae1 | 3416 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3417 | { |
3418 | switch (statement->header.type) | |
3419 | { | |
3420 | case lang_output_statement_enum: | |
f13a99db AM |
3421 | ASSERT (link_info.output_bfd == NULL); |
3422 | open_output (statement->output_statement.name); | |
252b5132 | 3423 | ldemul_set_output_arch (); |
0e1862bb L |
3424 | if (config.magic_demand_paged |
3425 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3426 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3427 | else |
f13a99db | 3428 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3429 | if (config.text_read_only) |
f13a99db | 3430 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3431 | else |
f13a99db | 3432 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3433 | if (link_info.traditional_format) |
f13a99db | 3434 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3435 | else |
f13a99db | 3436 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
3437 | break; |
3438 | ||
3439 | case lang_target_statement_enum: | |
3440 | current_target = statement->target_statement.target; | |
3441 | break; | |
3442 | default: | |
3443 | break; | |
3444 | } | |
3445 | } | |
3446 | ||
e5caa5e0 | 3447 | static void |
61826503 | 3448 | init_opb (asection *s) |
e5caa5e0 | 3449 | { |
bb294208 | 3450 | unsigned int x; |
61826503 | 3451 | |
e5caa5e0 | 3452 | opb_shift = 0; |
bb294208 AM |
3453 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
3454 | && s != NULL | |
3455 | && (s->flags & SEC_ELF_OCTETS) != 0) | |
3456 | return; | |
3457 | ||
3458 | x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3459 | ldfile_output_machine); | |
e5caa5e0 AM |
3460 | if (x > 1) |
3461 | while ((x & 1) == 0) | |
3462 | { | |
3463 | x >>= 1; | |
3464 | ++opb_shift; | |
3465 | } | |
3466 | ASSERT (x == 1); | |
3467 | } | |
3468 | ||
252b5132 RH |
3469 | /* Open all the input files. */ |
3470 | ||
486329aa AM |
3471 | enum open_bfd_mode |
3472 | { | |
3473 | OPEN_BFD_NORMAL = 0, | |
3474 | OPEN_BFD_FORCE = 1, | |
3475 | OPEN_BFD_RESCAN = 2 | |
3476 | }; | |
0381901e | 3477 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 | 3478 | static lang_input_statement_type *plugin_insert = NULL; |
b02c4f16 | 3479 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3480 | #endif |
486329aa | 3481 | |
252b5132 | 3482 | static void |
486329aa | 3483 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
252b5132 | 3484 | { |
1579bae1 | 3485 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3486 | { |
3487 | switch (s->header.type) | |
3488 | { | |
3489 | case lang_constructors_statement_enum: | |
486329aa | 3490 | open_input_bfds (constructor_list.head, mode); |
252b5132 RH |
3491 | break; |
3492 | case lang_output_section_statement_enum: | |
486329aa | 3493 | open_input_bfds (s->output_section_statement.children.head, mode); |
252b5132 RH |
3494 | break; |
3495 | case lang_wild_statement_enum: | |
08da4cac | 3496 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3497 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3498 | && s->wild_statement.filename | |
97407faf AM |
3499 | && !wildcardp (s->wild_statement.filename) |
3500 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3501 | lookup_name (s->wild_statement.filename); |
486329aa | 3502 | open_input_bfds (s->wild_statement.children.head, mode); |
252b5132 RH |
3503 | break; |
3504 | case lang_group_statement_enum: | |
3505 | { | |
3506 | struct bfd_link_hash_entry *undefs; | |
0381901e | 3507 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3508 | lang_input_statement_type *plugin_insert_save; |
3509 | #endif | |
252b5132 RH |
3510 | |
3511 | /* We must continually search the entries in the group | |
08da4cac KH |
3512 | until no new symbols are added to the list of undefined |
3513 | symbols. */ | |
252b5132 RH |
3514 | |
3515 | do | |
3516 | { | |
0381901e | 3517 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3518 | plugin_insert_save = plugin_insert; |
3519 | #endif | |
252b5132 | 3520 | undefs = link_info.hash->undefs_tail; |
486329aa AM |
3521 | open_input_bfds (s->group_statement.children.head, |
3522 | mode | OPEN_BFD_FORCE); | |
252b5132 | 3523 | } |
b02c4f16 | 3524 | while (undefs != link_info.hash->undefs_tail |
0381901e | 3525 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3526 | /* Objects inserted by a plugin, which are loaded |
3527 | before we hit this loop, may have added new | |
3528 | undefs. */ | |
3529 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3530 | #endif | |
3531 | ); | |
252b5132 RH |
3532 | } |
3533 | break; | |
3534 | case lang_target_statement_enum: | |
3535 | current_target = s->target_statement.target; | |
3536 | break; | |
3537 | case lang_input_statement_enum: | |
66be1055 | 3538 | if (s->input_statement.flags.real) |
252b5132 | 3539 | { |
bde18da4 | 3540 | lang_statement_union_type **os_tail; |
252b5132 | 3541 | lang_statement_list_type add; |
d215621e | 3542 | bfd *abfd; |
252b5132 RH |
3543 | |
3544 | s->input_statement.target = current_target; | |
3545 | ||
3546 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3547 | is an archive which has already been searched, then |
3548 | force it to be researched unless the whole archive | |
d215621e AM |
3549 | has been loaded already. Do the same for a rescan. |
3550 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3551 | if (mode != OPEN_BFD_NORMAL |
0381901e | 3552 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3553 | && ((mode & OPEN_BFD_RESCAN) == 0 |
3554 | || plugin_insert == NULL) | |
3555 | #endif | |
66be1055 | 3556 | && s->input_statement.flags.loaded |
d215621e AM |
3557 | && (abfd = s->input_statement.the_bfd) != NULL |
3558 | && ((bfd_get_format (abfd) == bfd_archive | |
3559 | && !s->input_statement.flags.whole_archive) | |
3560 | || (bfd_get_format (abfd) == bfd_object | |
3561 | && ((abfd->flags) & DYNAMIC) != 0 | |
3562 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
e77620a5 | 3563 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3564 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3565 | { |
f38a2680 AM |
3566 | s->input_statement.flags.loaded = false; |
3567 | s->input_statement.flags.reload = true; | |
15fc2e13 | 3568 | } |
252b5132 | 3569 | |
5c1e6d53 | 3570 | os_tail = lang_os_list.tail; |
d1778b88 | 3571 | lang_list_init (&add); |
1276aefa | 3572 | |
0aa7f586 | 3573 | if (!load_symbols (&s->input_statement, &add)) |
f38a2680 | 3574 | config.make_executable = false; |
252b5132 RH |
3575 | |
3576 | if (add.head != NULL) | |
3577 | { | |
bde18da4 AM |
3578 | /* If this was a script with output sections then |
3579 | tack any added statements on to the end of the | |
3580 | list. This avoids having to reorder the output | |
3581 | section statement list. Very likely the user | |
3582 | forgot -T, and whatever we do here will not meet | |
3583 | naive user expectations. */ | |
5c1e6d53 | 3584 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
3585 | { |
3586 | einfo (_("%P: warning: %s contains output sections;" | |
3587 | " did you forget -T?\n"), | |
3588 | s->input_statement.filename); | |
3589 | *stat_ptr->tail = add.head; | |
3590 | stat_ptr->tail = add.tail; | |
3591 | } | |
3592 | else | |
3593 | { | |
3594 | *add.tail = s->header.next; | |
3595 | s->header.next = add.head; | |
3596 | } | |
252b5132 RH |
3597 | } |
3598 | } | |
0381901e | 3599 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3600 | /* If we have found the point at which a plugin added new |
3601 | files, clear plugin_insert to enable archive rescan. */ | |
3602 | if (&s->input_statement == plugin_insert) | |
3603 | plugin_insert = NULL; | |
3604 | #endif | |
252b5132 | 3605 | break; |
e759c116 | 3606 | case lang_assignment_statement_enum: |
165f707a | 3607 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
01554a74 | 3608 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
e759c116 | 3609 | break; |
252b5132 RH |
3610 | default: |
3611 | break; | |
3612 | } | |
3613 | } | |
c4b78195 NC |
3614 | |
3615 | /* Exit if any of the files were missing. */ | |
66be1055 | 3616 | if (input_flags.missing_file) |
c4b78195 | 3617 | einfo ("%F"); |
252b5132 RH |
3618 | } |
3619 | ||
094e34f2 | 3620 | #ifdef ENABLE_LIBCTF |
926c9e76 NA |
3621 | /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings |
3622 | that happened specifically at CTF open time. */ | |
3623 | static void | |
139633c3 | 3624 | lang_ctf_errs_warnings (ctf_dict_t *fp) |
926c9e76 NA |
3625 | { |
3626 | ctf_next_t *i = NULL; | |
3627 | char *text; | |
3628 | int is_warning; | |
3629 | int err; | |
3630 | ||
3631 | while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL) | |
3632 | { | |
5e9b84f7 | 3633 | einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), |
926c9e76 NA |
3634 | text); |
3635 | free (text); | |
3636 | } | |
3637 | if (err != ECTF_NEXT_END) | |
3638 | { | |
3639 | einfo (_("CTF error: cannot get CTF errors: `%s'\n"), | |
3640 | ctf_errmsg (err)); | |
3641 | } | |
3642 | ||
3643 | /* `err' returns errors from the error/warning iterator in particular. | |
3644 | These never assert. But if we have an fp, that could have recorded | |
3645 | an assertion failure: assert if it has done so. */ | |
3646 | ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL); | |
3647 | } | |
3648 | ||
1ff6de03 NA |
3649 | /* Open the CTF sections in the input files with libctf: if any were opened, |
3650 | create a fake input file that we'll write the merged CTF data to later | |
3651 | on. */ | |
3652 | ||
3653 | static void | |
3654 | ldlang_open_ctf (void) | |
3655 | { | |
3656 | int any_ctf = 0; | |
3657 | int err; | |
3658 | ||
3659 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3660 | { | |
3661 | asection *sect; | |
3662 | ||
139633c3 | 3663 | /* Incoming files from the compiler have a single ctf_dict_t in them |
1ff6de03 NA |
3664 | (which is presented to us by the libctf API in a ctf_archive_t |
3665 | wrapper): files derived from a previous relocatable link have a CTF | |
3666 | archive containing possibly many CTF files. */ | |
3667 | ||
3668 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3669 | { | |
3670 | if (err != ECTF_NOCTFDATA) | |
926c9e76 NA |
3671 | { |
3672 | lang_ctf_errs_warnings (NULL); | |
3673 | einfo (_("%P: warning: CTF section in %pB not loaded; " | |
5e9b84f7 | 3674 | "its types will be discarded: %s\n"), file->the_bfd, |
1ff6de03 | 3675 | ctf_errmsg (err)); |
926c9e76 | 3676 | } |
1ff6de03 NA |
3677 | continue; |
3678 | } | |
3679 | ||
3680 | /* Prevent the contents of this section from being written, while | |
fa03171f NA |
3681 | requiring the section itself to be duplicated in the output, but only |
3682 | once. */ | |
1ff6de03 NA |
3683 | /* This section must exist if ctf_bfdopen() succeeded. */ |
3684 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3685 | sect->size = 0; | |
3686 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3687 | ||
fa03171f NA |
3688 | if (any_ctf) |
3689 | sect->flags |= SEC_EXCLUDE; | |
1ff6de03 NA |
3690 | any_ctf = 1; |
3691 | } | |
3692 | ||
3693 | if (!any_ctf) | |
3694 | { | |
3695 | ctf_output = NULL; | |
3696 | return; | |
3697 | } | |
3698 | ||
3699 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3700 | return; | |
3701 | ||
c48acf6f | 3702 | einfo (_("%P: warning: CTF output not created: `%s'\n"), |
1ff6de03 NA |
3703 | ctf_errmsg (err)); |
3704 | ||
3705 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3706 | ctf_close (errfile->the_ctf); | |
3707 | } | |
3708 | ||
3709 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3710 | function and data object sections need adjustment. */ | |
3711 | ||
3712 | static void | |
3713 | lang_merge_ctf (void) | |
3714 | { | |
3715 | asection *output_sect; | |
5dba6f05 | 3716 | int flags = 0; |
1ff6de03 NA |
3717 | |
3718 | if (!ctf_output) | |
3719 | return; | |
3720 | ||
3721 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3722 | ||
3723 | /* If the section was discarded, don't waste time merging. */ | |
3724 | if (output_sect == NULL) | |
3725 | { | |
139633c3 | 3726 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
3727 | ctf_output = NULL; |
3728 | ||
3729 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3730 | { | |
3731 | ctf_close (file->the_ctf); | |
3732 | file->the_ctf = NULL; | |
3733 | } | |
3734 | return; | |
3735 | } | |
3736 | ||
3737 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3738 | { | |
3739 | if (!file->the_ctf) | |
3740 | continue; | |
3741 | ||
f320bba5 | 3742 | /* Takes ownership of file->the_ctf. */ |
1ff6de03 NA |
3743 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) |
3744 | { | |
f320bba5 EB |
3745 | einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), |
3746 | file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 NA |
3747 | ctf_close (file->the_ctf); |
3748 | file->the_ctf = NULL; | |
3749 | continue; | |
3750 | } | |
3751 | } | |
3752 | ||
5dba6f05 NA |
3753 | if (!config.ctf_share_duplicated) |
3754 | flags = CTF_LINK_SHARE_UNCONFLICTED; | |
3755 | else | |
3756 | flags = CTF_LINK_SHARE_DUPLICATED; | |
3757 | if (!config.ctf_variables) | |
3758 | flags |= CTF_LINK_OMIT_VARIABLES_SECTION; | |
35a01a04 NA |
3759 | if (bfd_link_relocatable (&link_info)) |
3760 | flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; | |
5dba6f05 NA |
3761 | |
3762 | if (ctf_link (ctf_output, flags) < 0) | |
1ff6de03 | 3763 | { |
926c9e76 | 3764 | lang_ctf_errs_warnings (ctf_output); |
f320bba5 | 3765 | einfo (_("%P: warning: CTF linking failed; " |
5e9b84f7 | 3766 | "output will have no CTF section: %s\n"), |
1ff6de03 NA |
3767 | ctf_errmsg (ctf_errno (ctf_output))); |
3768 | if (output_sect) | |
3769 | { | |
3770 | output_sect->size = 0; | |
3771 | output_sect->flags |= SEC_EXCLUDE; | |
3772 | } | |
3773 | } | |
926c9e76 | 3774 | /* Output any lingering errors that didn't come from ctf_link. */ |
8b37e7b6 | 3775 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3776 | } |
3777 | ||
3d16b64e NA |
3778 | /* Let the emulation acquire strings from the dynamic strtab to help it optimize |
3779 | the CTF, if supported. */ | |
1ff6de03 NA |
3780 | |
3781 | void | |
3d16b64e | 3782 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) |
1ff6de03 | 3783 | { |
3d16b64e NA |
3784 | ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); |
3785 | } | |
3786 | ||
3787 | /* Inform the emulation about the addition of a new dynamic symbol, in BFD | |
3788 | internal format. */ | |
3789 | void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym) | |
3790 | { | |
3791 | ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym); | |
1ff6de03 NA |
3792 | } |
3793 | ||
3794 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3795 | need to dedup against the strtab and symtab, then possibly called from the | |
3796 | target linker code if the dedup has happened. */ | |
3797 | static void | |
3798 | lang_write_ctf (int late) | |
3799 | { | |
3800 | size_t output_size; | |
3801 | asection *output_sect; | |
3802 | ||
3803 | if (!ctf_output) | |
3804 | return; | |
3805 | ||
3806 | if (late) | |
3807 | { | |
3808 | /* Emit CTF late if this emulation says it can do so. */ | |
3809 | if (ldemul_emit_ctf_early ()) | |
3810 | return; | |
3811 | } | |
3812 | else | |
3813 | { | |
3814 | if (!ldemul_emit_ctf_early ()) | |
3815 | return; | |
3816 | } | |
3817 | ||
3d16b64e NA |
3818 | /* Inform the emulation that all the symbols that will be received have |
3819 | been. */ | |
3820 | ||
3821 | ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL); | |
3822 | ||
1ff6de03 NA |
3823 | /* Emit CTF. */ |
3824 | ||
3825 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3826 | if (output_sect) | |
3827 | { | |
3828 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
3829 | CTF_COMPRESSION_THRESHOLD); | |
3830 | output_sect->size = output_size; | |
3831 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
3832 | ||
926c9e76 | 3833 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3834 | if (!output_sect->contents) |
3835 | { | |
f320bba5 | 3836 | einfo (_("%P: warning: CTF section emission failed; " |
5e9b84f7 | 3837 | "output will have no CTF section: %s\n"), |
f320bba5 | 3838 | ctf_errmsg (ctf_errno (ctf_output))); |
1ff6de03 NA |
3839 | output_sect->size = 0; |
3840 | output_sect->flags |= SEC_EXCLUDE; | |
3841 | } | |
3842 | } | |
3843 | ||
3844 | /* This also closes every CTF input file used in the link. */ | |
139633c3 | 3845 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
3846 | ctf_output = NULL; |
3847 | ||
3848 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3849 | file->the_ctf = NULL; | |
3850 | } | |
3851 | ||
3852 | /* Write out the CTF section late, if the emulation needs that. */ | |
3853 | ||
3854 | void | |
3855 | ldlang_write_ctf_late (void) | |
3856 | { | |
3857 | /* Trigger a "late call", if the emulation needs one. */ | |
3858 | ||
3859 | lang_write_ctf (1); | |
3860 | } | |
094e34f2 NA |
3861 | #else |
3862 | static void | |
3863 | ldlang_open_ctf (void) | |
3864 | { | |
3865 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3866 | { | |
3867 | asection *sect; | |
3868 | ||
3869 | /* If built without CTF, warn and delete all CTF sections from the output. | |
3870 | (The alternative would be to simply concatenate them, which does not | |
3871 | yield a valid CTF section.) */ | |
3872 | ||
3873 | if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL) | |
3874 | { | |
f320bba5 EB |
3875 | einfo (_("%P: warning: CTF section in %pB not linkable: " |
3876 | "%P was built without support for CTF\n"), file->the_bfd); | |
094e34f2 NA |
3877 | sect->size = 0; |
3878 | sect->flags |= SEC_EXCLUDE; | |
3879 | } | |
3880 | } | |
3881 | } | |
3882 | ||
3883 | static void lang_merge_ctf (void) {} | |
3884 | void | |
3d16b64e NA |
3885 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab |
3886 | ATTRIBUTE_UNUSED) {} | |
3887 | void | |
3888 | ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED, | |
3889 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {} | |
094e34f2 NA |
3890 | static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} |
3891 | void ldlang_write_ctf_late (void) {} | |
3892 | #endif | |
1ff6de03 | 3893 | |
08da4cac | 3894 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3895 | This is a two step process as the symbol table doesn't even exist at |
3896 | the time the ld command line is processed. First we put the name | |
3897 | on a list, then, once the output file has been opened, transfer the | |
3898 | name to the symbol table. */ | |
3899 | ||
e3e942e9 | 3900 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3901 | |
e3e942e9 | 3902 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3903 | |
3904 | void | |
f38a2680 | 3905 | ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) |
252b5132 | 3906 | { |
24898b70 | 3907 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 3908 | |
988de25b | 3909 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
3910 | new_undef->next = ldlang_undef_chain_list_head; |
3911 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 3912 | |
d3ce72d0 | 3913 | new_undef->name = xstrdup (name); |
fcf0e35b | 3914 | |
f13a99db | 3915 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 3916 | insert_undefined (new_undef->name); |
fcf0e35b AM |
3917 | } |
3918 | ||
3919 | /* Insert NAME as undefined in the symbol table. */ | |
3920 | ||
3921 | static void | |
1579bae1 | 3922 | insert_undefined (const char *name) |
fcf0e35b AM |
3923 | { |
3924 | struct bfd_link_hash_entry *h; | |
3925 | ||
f38a2680 | 3926 | h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); |
1579bae1 | 3927 | if (h == NULL) |
df5f2391 | 3928 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
3929 | if (h->type == bfd_link_hash_new) |
3930 | { | |
3931 | h->type = bfd_link_hash_undefined; | |
3932 | h->u.undef.abfd = NULL; | |
f38a2680 | 3933 | h->non_ir_ref_regular = true; |
fcf0e35b AM |
3934 | bfd_link_add_undef (link_info.hash, h); |
3935 | } | |
252b5132 RH |
3936 | } |
3937 | ||
3938 | /* Run through the list of undefineds created above and place them | |
3939 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3940 | script file. */ |
3941 | ||
252b5132 | 3942 | static void |
1579bae1 | 3943 | lang_place_undefineds (void) |
252b5132 RH |
3944 | { |
3945 | ldlang_undef_chain_list_type *ptr; | |
3946 | ||
1579bae1 AM |
3947 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3948 | insert_undefined (ptr->name); | |
252b5132 RH |
3949 | } |
3950 | ||
5789f845 AM |
3951 | /* Mark -u symbols against garbage collection. */ |
3952 | ||
3953 | static void | |
3954 | lang_mark_undefineds (void) | |
3955 | { | |
3956 | ldlang_undef_chain_list_type *ptr; | |
3957 | ||
fc304b88 | 3958 | if (is_elf_hash_table (link_info.hash)) |
5789f845 AM |
3959 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3960 | { | |
3961 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
f38a2680 | 3962 | bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); |
5789f845 AM |
3963 | if (h != NULL) |
3964 | h->mark = 1; | |
3965 | } | |
3966 | } | |
3967 | ||
0a618243 AB |
3968 | /* Structure used to build the list of symbols that the user has required |
3969 | be defined. */ | |
3970 | ||
3971 | struct require_defined_symbol | |
3972 | { | |
3973 | const char *name; | |
3974 | struct require_defined_symbol *next; | |
3975 | }; | |
3976 | ||
3977 | /* The list of symbols that the user has required be defined. */ | |
3978 | ||
3979 | static struct require_defined_symbol *require_defined_symbol_list; | |
3980 | ||
3981 | /* Add a new symbol NAME to the list of symbols that are required to be | |
3982 | defined. */ | |
3983 | ||
3984 | void | |
0aa7f586 | 3985 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
3986 | { |
3987 | struct require_defined_symbol *ptr; | |
3988 | ||
f38a2680 | 3989 | ldlang_add_undef (name, true); |
988de25b | 3990 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 AB |
3991 | ptr->next = require_defined_symbol_list; |
3992 | ptr->name = strdup (name); | |
3993 | require_defined_symbol_list = ptr; | |
3994 | } | |
3995 | ||
3996 | /* Check that all symbols the user required to be defined, are defined, | |
3997 | raise an error if we find a symbol that is not defined. */ | |
3998 | ||
3999 | static void | |
4000 | ldlang_check_require_defined_symbols (void) | |
4001 | { | |
4002 | struct require_defined_symbol *ptr; | |
4003 | ||
4004 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
4005 | { | |
4006 | struct bfd_link_hash_entry *h; | |
4007 | ||
4008 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
f38a2680 | 4009 | false, false, true); |
0a618243 | 4010 | if (h == NULL |
0aa7f586 AM |
4011 | || (h->type != bfd_link_hash_defined |
4012 | && h->type != bfd_link_hash_defweak)) | |
df5f2391 | 4013 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
4014 | } |
4015 | } | |
4016 | ||
0841712e JJ |
4017 | /* Check for all readonly or some readwrite sections. */ |
4018 | ||
4019 | static void | |
6feb9908 AM |
4020 | check_input_sections |
4021 | (lang_statement_union_type *s, | |
4022 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 4023 | { |
988de25b | 4024 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
4025 | { |
4026 | switch (s->header.type) | |
967928e9 AM |
4027 | { |
4028 | case lang_wild_statement_enum: | |
4029 | walk_wild (&s->wild_statement, check_section_callback, | |
4030 | output_section_statement); | |
0aa7f586 | 4031 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4032 | return; |
4033 | break; | |
4034 | case lang_constructors_statement_enum: | |
4035 | check_input_sections (constructor_list.head, | |
4036 | output_section_statement); | |
0aa7f586 | 4037 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4038 | return; |
4039 | break; | |
4040 | case lang_group_statement_enum: | |
4041 | check_input_sections (s->group_statement.children.head, | |
4042 | output_section_statement); | |
0aa7f586 | 4043 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4044 | return; |
4045 | break; | |
4046 | default: | |
4047 | break; | |
4048 | } | |
0841712e JJ |
4049 | } |
4050 | } | |
4051 | ||
bcaa7b3e L |
4052 | /* Update wildcard statements if needed. */ |
4053 | ||
4054 | static void | |
4055 | update_wild_statements (lang_statement_union_type *s) | |
4056 | { | |
4057 | struct wildcard_list *sec; | |
4058 | ||
4059 | switch (sort_section) | |
4060 | { | |
4061 | default: | |
4062 | FAIL (); | |
4063 | ||
4064 | case none: | |
4065 | break; | |
4066 | ||
4067 | case by_name: | |
4068 | case by_alignment: | |
4069 | for (; s != NULL; s = s->header.next) | |
4070 | { | |
4071 | switch (s->header.type) | |
4072 | { | |
4073 | default: | |
4074 | break; | |
4075 | ||
4076 | case lang_wild_statement_enum: | |
0d0999db L |
4077 | for (sec = s->wild_statement.section_list; sec != NULL; |
4078 | sec = sec->next) | |
2d3181c7 AM |
4079 | /* Don't sort .init/.fini sections. */ |
4080 | if (strcmp (sec->spec.name, ".init") != 0 | |
4081 | && strcmp (sec->spec.name, ".fini") != 0) | |
af31506c MM |
4082 | { |
4083 | switch (sec->spec.sorted) | |
4084 | { | |
4085 | case none: | |
4086 | sec->spec.sorted = sort_section; | |
4087 | break; | |
4088 | case by_name: | |
4089 | if (sort_section == by_alignment) | |
4090 | sec->spec.sorted = by_name_alignment; | |
4091 | break; | |
4092 | case by_alignment: | |
4093 | if (sort_section == by_name) | |
4094 | sec->spec.sorted = by_alignment_name; | |
4095 | break; | |
4096 | default: | |
4097 | break; | |
4098 | } | |
4099 | s->wild_statement.any_specs_sorted = true; | |
4100 | } | |
bcaa7b3e L |
4101 | break; |
4102 | ||
4103 | case lang_constructors_statement_enum: | |
4104 | update_wild_statements (constructor_list.head); | |
4105 | break; | |
4106 | ||
4107 | case lang_output_section_statement_enum: | |
2d3181c7 AM |
4108 | update_wild_statements |
4109 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
4110 | break; |
4111 | ||
4112 | case lang_group_statement_enum: | |
4113 | update_wild_statements (s->group_statement.children.head); | |
4114 | break; | |
4115 | } | |
4116 | } | |
4117 | break; | |
4118 | } | |
4119 | } | |
4120 | ||
396a2467 | 4121 | /* Open input files and attach to output sections. */ |
08da4cac | 4122 | |
252b5132 | 4123 | static void |
1579bae1 AM |
4124 | map_input_to_output_sections |
4125 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4126 | lang_output_section_statement_type *os) |
252b5132 | 4127 | { |
1579bae1 | 4128 | for (; s != NULL; s = s->header.next) |
252b5132 | 4129 | { |
dfa7b0b8 AM |
4130 | lang_output_section_statement_type *tos; |
4131 | flagword flags; | |
c212f39d | 4132 | unsigned int type = 0; |
dfa7b0b8 | 4133 | |
252b5132 RH |
4134 | switch (s->header.type) |
4135 | { | |
252b5132 | 4136 | case lang_wild_statement_enum: |
afd7a018 | 4137 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4138 | break; |
252b5132 RH |
4139 | case lang_constructors_statement_enum: |
4140 | map_input_to_output_sections (constructor_list.head, | |
4141 | target, | |
afd7a018 | 4142 | os); |
252b5132 RH |
4143 | break; |
4144 | case lang_output_section_statement_enum: | |
dfa7b0b8 | 4145 | tos = &s->output_section_statement; |
21401fc7 AM |
4146 | if (tos->constraint == ONLY_IF_RW |
4147 | || tos->constraint == ONLY_IF_RO) | |
0841712e | 4148 | { |
f38a2680 | 4149 | tos->all_input_readonly = true; |
dfa7b0b8 AM |
4150 | check_input_sections (tos->children.head, tos); |
4151 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
21401fc7 | 4152 | tos->constraint = -1; |
0841712e | 4153 | } |
21401fc7 AM |
4154 | if (tos->constraint >= 0) |
4155 | map_input_to_output_sections (tos->children.head, | |
4156 | target, | |
4157 | tos); | |
a431bc2e AM |
4158 | break; |
4159 | case lang_output_statement_enum: | |
252b5132 RH |
4160 | break; |
4161 | case lang_target_statement_enum: | |
4162 | target = s->target_statement.target; | |
4163 | break; | |
4164 | case lang_group_statement_enum: | |
4165 | map_input_to_output_sections (s->group_statement.children.head, | |
4166 | target, | |
afd7a018 | 4167 | os); |
252b5132 | 4168 | break; |
384d938f NS |
4169 | case lang_data_statement_enum: |
4170 | /* Make sure that any sections mentioned in the expression | |
4171 | are initialized. */ | |
4172 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4173 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4174 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4175 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4176 | switch (os->sectype) | |
4177 | { | |
4178 | case normal_section: | |
4179 | case overlay_section: | |
7b243801 | 4180 | case first_overlay_section: |
dfa7b0b8 AM |
4181 | break; |
4182 | case noalloc_section: | |
4183 | flags = SEC_HAS_CONTENTS; | |
4184 | break; | |
6b86da53 LB |
4185 | case readonly_section: |
4186 | flags |= SEC_READONLY; | |
4187 | break; | |
c212f39d FS |
4188 | case typed_readonly_section: |
4189 | flags |= SEC_READONLY; | |
4190 | /* Fall through. */ | |
4191 | case type_section: | |
4192 | if (os->sectype_value->type.node_class == etree_name | |
4193 | && os->sectype_value->type.node_code == NAME) | |
4194 | { | |
4195 | const char *name = os->sectype_value->name.name; | |
4196 | if (strcmp (name, "SHT_PROGBITS") == 0) | |
4197 | type = SHT_PROGBITS; | |
4198 | else if (strcmp (name, "SHT_STRTAB") == 0) | |
4199 | type = SHT_STRTAB; | |
4200 | else if (strcmp (name, "SHT_NOTE") == 0) | |
4201 | type = SHT_NOTE; | |
4202 | else if (strcmp (name, "SHT_NOBITS") == 0) | |
4203 | type = SHT_NOBITS; | |
4204 | else if (strcmp (name, "SHT_INIT_ARRAY") == 0) | |
4205 | type = SHT_INIT_ARRAY; | |
4206 | else if (strcmp (name, "SHT_FINI_ARRAY") == 0) | |
4207 | type = SHT_FINI_ARRAY; | |
4208 | else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0) | |
4209 | type = SHT_PREINIT_ARRAY; | |
4210 | else | |
4211 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4212 | os->name); | |
4213 | } | |
4214 | else | |
4215 | { | |
4216 | exp_fold_tree_no_dot (os->sectype_value); | |
4217 | if (expld.result.valid_p) | |
4218 | type = expld.result.value; | |
4219 | else | |
4220 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4221 | os->name); | |
4222 | } | |
4223 | break; | |
dfa7b0b8 | 4224 | case noload_section: |
f4eaaf7f AM |
4225 | if (bfd_get_flavour (link_info.output_bfd) |
4226 | == bfd_target_elf_flavour) | |
4227 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4228 | else | |
4229 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
dfa7b0b8 AM |
4230 | break; |
4231 | } | |
a431bc2e | 4232 | if (os->bfd_section == NULL) |
326adec3 | 4233 | init_os (os, flags | SEC_READONLY); |
a431bc2e AM |
4234 | else |
4235 | os->bfd_section->flags |= flags; | |
c212f39d | 4236 | os->bfd_section->type = type; |
afd7a018 | 4237 | break; |
252b5132 | 4238 | case lang_input_section_enum: |
e0f6802f AM |
4239 | break; |
4240 | case lang_fill_statement_enum: | |
252b5132 | 4241 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4242 | case lang_reloc_statement_enum: |
4243 | case lang_padding_statement_enum: | |
4244 | case lang_input_statement_enum: | |
afd7a018 | 4245 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4246 | init_os (os, 0); |
252b5132 RH |
4247 | break; |
4248 | case lang_assignment_statement_enum: | |
afd7a018 | 4249 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4250 | init_os (os, 0); |
252b5132 RH |
4251 | |
4252 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4253 | are initialized. */ |
252b5132 RH |
4254 | exp_init_os (s->assignment_statement.exp); |
4255 | break; | |
252b5132 | 4256 | case lang_address_statement_enum: |
a431bc2e AM |
4257 | /* Mark the specified section with the supplied address. |
4258 | If this section was actually a segment marker, then the | |
4259 | directive is ignored if the linker script explicitly | |
4260 | processed the segment marker. Originally, the linker | |
4261 | treated segment directives (like -Ttext on the | |
4262 | command-line) as section directives. We honor the | |
370dfff4 | 4263 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4264 | linker scripts that do not specifically check for |
4265 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4266 | if (!s->address_statement.segment |
ba916c8a MM |
4267 | || !s->address_statement.segment->used) |
4268 | { | |
dfa7b0b8 AM |
4269 | const char *name = s->address_statement.section_name; |
4270 | ||
4271 | /* Create the output section statement here so that | |
4272 | orphans with a set address will be placed after other | |
4273 | script sections. If we let the orphan placement code | |
4274 | place them in amongst other sections then the address | |
4275 | will affect following script sections, which is | |
4276 | likely to surprise naive users. */ | |
21401fc7 | 4277 | tos = lang_output_section_statement_lookup (name, 0, 1); |
dfa7b0b8 AM |
4278 | tos->addr_tree = s->address_statement.address; |
4279 | if (tos->bfd_section == NULL) | |
4280 | init_os (tos, 0); | |
ba916c8a | 4281 | } |
252b5132 | 4282 | break; |
53d25da6 AM |
4283 | case lang_insert_statement_enum: |
4284 | break; | |
43ae96e9 MM |
4285 | case lang_input_matcher_enum: |
4286 | FAIL (); | |
53d25da6 AM |
4287 | } |
4288 | } | |
4289 | } | |
4290 | ||
4291 | /* An insert statement snips out all the linker statements from the | |
4292 | start of the list and places them after the output section | |
4293 | statement specified by the insert. This operation is complicated | |
4294 | by the fact that we keep a doubly linked list of output section | |
776ab89f AM |
4295 | statements as well as the singly linked list of all statements. |
4296 | FIXME someday: Twiddling with the list not only moves statements | |
4297 | from the user's script but also input and group statements that are | |
4298 | built from command line object files and --start-group. We only | |
4299 | get away with this because the list pointers used by file_chain | |
4300 | and input_file_chain are not reordered, and processing via | |
4301 | statement_list after this point mostly ignores input statements. | |
4302 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4303 | can end up looking odd. */ | |
53d25da6 AM |
4304 | |
4305 | static void | |
776ab89f | 4306 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
4307 | { |
4308 | lang_statement_union_type **s; | |
4309 | lang_output_section_statement_type *first_os = NULL; | |
4310 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 4311 | lang_output_section_statement_type *os; |
53d25da6 | 4312 | |
776ab89f AM |
4313 | s = start; |
4314 | while (*s != NULL) | |
53d25da6 AM |
4315 | { |
4316 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4317 | { | |
4318 | /* Keep pointers to the first and last output section | |
4319 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
4320 | os = &(*s)->output_section_statement; |
4321 | ||
4322 | ASSERT (last_os == NULL || last_os->next == os); | |
4323 | last_os = os; | |
66c103b7 AM |
4324 | |
4325 | /* Set constraint negative so that lang_output_section_find | |
4326 | won't match this output section statement. At this | |
4327 | stage in linking constraint has values in the range | |
4328 | [-1, ONLY_IN_RW]. */ | |
4329 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
4330 | if (first_os == NULL) |
4331 | first_os = last_os; | |
4332 | } | |
776ab89f AM |
4333 | else if ((*s)->header.type == lang_group_statement_enum) |
4334 | { | |
4335 | /* A user might put -T between --start-group and | |
4336 | --end-group. One way this odd construct might arise is | |
4337 | from a wrapper around ld to change library search | |
4338 | behaviour. For example: | |
4339 | #! /bin/sh | |
4340 | exec real_ld --start-group "$@" --end-group | |
4341 | This isn't completely unreasonable so go looking inside a | |
4342 | group statement for insert statements. */ | |
4343 | process_insert_statements (&(*s)->group_statement.children.head); | |
4344 | } | |
53d25da6 AM |
4345 | else if ((*s)->header.type == lang_insert_statement_enum) |
4346 | { | |
4347 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4348 | lang_output_section_statement_type *where; | |
53d25da6 AM |
4349 | lang_statement_union_type **ptr; |
4350 | lang_statement_union_type *first; | |
4351 | ||
abf874aa CL |
4352 | if (link_info.non_contiguous_regions) |
4353 | { | |
4354 | einfo (_("warning: INSERT statement in linker script is " | |
4355 | "incompatible with --enable-non-contiguous-regions.\n")); | |
4356 | } | |
4357 | ||
53d25da6 AM |
4358 | where = lang_output_section_find (i->where); |
4359 | if (where != NULL && i->is_before) | |
4360 | { | |
967928e9 | 4361 | do |
53d25da6 | 4362 | where = where->prev; |
66c103b7 | 4363 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4364 | } |
4365 | if (where == NULL) | |
4366 | { | |
66c103b7 AM |
4367 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
4368 | return; | |
53d25da6 AM |
4369 | } |
4370 | ||
4371 | /* Deal with reordering the output section statement list. */ | |
4372 | if (last_os != NULL) | |
4373 | { | |
4374 | asection *first_sec, *last_sec; | |
29183214 | 4375 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4376 | |
4377 | /* Snip out the output sections we are moving. */ | |
4378 | first_os->prev->next = last_os->next; | |
4379 | if (last_os->next == NULL) | |
29183214 L |
4380 | { |
4381 | next = &first_os->prev->next; | |
5c1e6d53 | 4382 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4383 | } |
53d25da6 AM |
4384 | else |
4385 | last_os->next->prev = first_os->prev; | |
4386 | /* Add them in at the new position. */ | |
4387 | last_os->next = where->next; | |
4388 | if (where->next == NULL) | |
29183214 L |
4389 | { |
4390 | next = &last_os->next; | |
5c1e6d53 | 4391 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4392 | } |
53d25da6 AM |
4393 | else |
4394 | where->next->prev = last_os; | |
4395 | first_os->prev = where; | |
4396 | where->next = first_os; | |
4397 | ||
4398 | /* Move the bfd sections in the same way. */ | |
4399 | first_sec = NULL; | |
4400 | last_sec = NULL; | |
4401 | for (os = first_os; os != NULL; os = os->next) | |
4402 | { | |
66c103b7 | 4403 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
4404 | if (os->bfd_section != NULL |
4405 | && os->bfd_section->owner != NULL) | |
4406 | { | |
4407 | last_sec = os->bfd_section; | |
4408 | if (first_sec == NULL) | |
4409 | first_sec = last_sec; | |
4410 | } | |
4411 | if (os == last_os) | |
4412 | break; | |
4413 | } | |
4414 | if (last_sec != NULL) | |
4415 | { | |
4416 | asection *sec = where->bfd_section; | |
4417 | if (sec == NULL) | |
4418 | sec = output_prev_sec_find (where); | |
4419 | ||
4420 | /* The place we want to insert must come after the | |
4421 | sections we are moving. So if we find no | |
4422 | section or if the section is the same as our | |
4423 | last section, then no move is needed. */ | |
4424 | if (sec != NULL && sec != last_sec) | |
4425 | { | |
4426 | /* Trim them off. */ | |
4427 | if (first_sec->prev != NULL) | |
4428 | first_sec->prev->next = last_sec->next; | |
4429 | else | |
f13a99db | 4430 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4431 | if (last_sec->next != NULL) |
4432 | last_sec->next->prev = first_sec->prev; | |
4433 | else | |
f13a99db | 4434 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 | 4435 | /* Add back. */ |
18e7a658 AM |
4436 | if (sec->owner == NULL) |
4437 | /* SEC is the absolute section, from the | |
4438 | first dummy output section statement. Add | |
4439 | back the sections we trimmed off to the | |
4440 | start of the bfd sections. */ | |
4441 | sec = NULL; | |
4442 | if (sec != NULL) | |
4443 | last_sec->next = sec->next; | |
4444 | else | |
4445 | last_sec->next = link_info.output_bfd->sections; | |
4446 | if (last_sec->next != NULL) | |
4447 | last_sec->next->prev = last_sec; | |
53d25da6 | 4448 | else |
f13a99db | 4449 | link_info.output_bfd->section_last = last_sec; |
53d25da6 | 4450 | first_sec->prev = sec; |
18e7a658 AM |
4451 | if (first_sec->prev != NULL) |
4452 | first_sec->prev->next = first_sec; | |
4453 | else | |
4454 | link_info.output_bfd->sections = first_sec; | |
53d25da6 AM |
4455 | } |
4456 | } | |
53d25da6 AM |
4457 | } |
4458 | ||
18e7a658 AM |
4459 | lang_statement_union_type *after = (void *) where; |
4460 | if (where == &lang_os_list.head->output_section_statement | |
4461 | && where->next == first_os) | |
4462 | { | |
4463 | /* PR30155. Handle a corner case where the statement | |
4464 | list is something like the following: | |
4465 | . LOAD t.o | |
4466 | . .data 0x0000000000000000 0x0 | |
4467 | . [0x0000000000000000] b = . | |
4468 | . *(.data) | |
4469 | . .data 0x0000000000000000 0x0 t.o | |
4470 | . 0x0000000000000000 0x4 LONG 0x0 | |
4471 | . INSERT BEFORE .text.start | |
4472 | . [0x0000000000000004] a = . | |
4473 | . .text.start 0x0000000000000000 0x0 | |
4474 | . [0x0000000000000000] c = . | |
4475 | . OUTPUT(a.out elf64-x86-64) | |
4476 | Here we do not want to allow insert_os_after to | |
4477 | choose a point inside the list we are moving. | |
4478 | That would lose the list. Instead, let | |
4479 | insert_os_after work from the INSERT, which in this | |
4480 | particular example will result in inserting after | |
4481 | the assignment "a = .". */ | |
4482 | after = *s; | |
4483 | } | |
4484 | ptr = insert_os_after (after); | |
776ab89f AM |
4485 | /* Snip everything from the start of the list, up to and |
4486 | including the insert statement we are currently processing. */ | |
4487 | first = *start; | |
4488 | *start = (*s)->header.next; | |
4489 | /* Add them back where they belong, minus the insert. */ | |
53d25da6 AM |
4490 | *s = *ptr; |
4491 | if (*s == NULL) | |
4492 | statement_list.tail = s; | |
4493 | *ptr = first; | |
776ab89f | 4494 | s = start; |
18e7a658 AM |
4495 | first_os = NULL; |
4496 | last_os = NULL; | |
776ab89f | 4497 | continue; |
252b5132 | 4498 | } |
776ab89f | 4499 | s = &(*s)->header.next; |
252b5132 | 4500 | } |
66c103b7 AM |
4501 | |
4502 | /* Undo constraint twiddling. */ | |
4503 | for (os = first_os; os != NULL; os = os->next) | |
4504 | { | |
4505 | os->constraint = -2 - os->constraint; | |
4506 | if (os == last_os) | |
4507 | break; | |
4508 | } | |
252b5132 RH |
4509 | } |
4510 | ||
4bd5a393 AM |
4511 | /* An output section might have been removed after its statement was |
4512 | added. For example, ldemul_before_allocation can remove dynamic | |
4513 | sections if they turn out to be not needed. Clean them up here. */ | |
4514 | ||
8423293d | 4515 | void |
1579bae1 | 4516 | strip_excluded_output_sections (void) |
4bd5a393 | 4517 | { |
afd7a018 | 4518 | lang_output_section_statement_type *os; |
4bd5a393 | 4519 | |
046183de | 4520 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4521 | if (expld.phase != lang_mark_phase_enum) |
4522 | { | |
4523 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4524 | expld.dataseg.phase = exp_seg_none; |
f38a2680 | 4525 | one_lang_size_sections_pass (NULL, false); |
e9ee469a AM |
4526 | lang_reset_memory_regions (); |
4527 | } | |
4528 | ||
8ce18f9c | 4529 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
4530 | os != NULL; |
4531 | os = os->next) | |
4bd5a393 | 4532 | { |
75ff4589 | 4533 | asection *output_section; |
f38a2680 | 4534 | bool exclude; |
4bd5a393 | 4535 | |
66c103b7 | 4536 | if (os->constraint < 0) |
0841712e | 4537 | continue; |
8423293d | 4538 | |
75ff4589 L |
4539 | output_section = os->bfd_section; |
4540 | if (output_section == NULL) | |
8423293d AM |
4541 | continue; |
4542 | ||
32124d5b AM |
4543 | exclude = (output_section->rawsize == 0 |
4544 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4545 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
4546 | output_section)); |
4547 | ||
4548 | /* Some sections have not yet been sized, notably .gnu.version, | |
4549 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4550 | input sections, so don't drop output sections that have such | |
4551 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4552 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
4553 | { |
4554 | asection *s; | |
8423293d | 4555 | |
32124d5b | 4556 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4557 | if ((s->flags & SEC_EXCLUDE) == 0 |
4558 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4559 | || link_info.emitrelocations)) | |
e9ee469a | 4560 | { |
f38a2680 | 4561 | exclude = false; |
e9ee469a AM |
4562 | break; |
4563 | } | |
75ff4589 | 4564 | } |
8423293d | 4565 | |
32124d5b | 4566 | if (exclude) |
4bd5a393 | 4567 | { |
e9ee469a AM |
4568 | /* We don't set bfd_section to NULL since bfd_section of the |
4569 | removed output section statement may still be used. */ | |
6d8bf25d | 4570 | if (!os->update_dot) |
f38a2680 | 4571 | os->ignored = true; |
e9ee469a | 4572 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4573 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4574 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4575 | } |
4576 | } | |
18cd5bce AM |
4577 | } |
4578 | ||
4579 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4580 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
4581 | |
4582 | void | |
4583 | lang_clear_os_map (void) | |
4584 | { | |
4585 | lang_output_section_statement_type *os; | |
4586 | ||
4587 | if (map_head_is_link_order) | |
4588 | return; | |
4589 | ||
8ce18f9c | 4590 | for (os = (void *) lang_os_list.head; |
18cd5bce AM |
4591 | os != NULL; |
4592 | os = os->next) | |
4593 | { | |
4594 | asection *output_section; | |
4595 | ||
4596 | if (os->constraint < 0) | |
4597 | continue; | |
4598 | ||
4599 | output_section = os->bfd_section; | |
4600 | if (output_section == NULL) | |
4601 | continue; | |
4602 | ||
4603 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4604 | output_section->map_head.link_order = NULL; | |
4605 | output_section->map_tail.link_order = NULL; | |
4606 | } | |
8423293d AM |
4607 | |
4608 | /* Stop future calls to lang_add_section from messing with map_head | |
4609 | and map_tail link_order fields. */ | |
f38a2680 | 4610 | map_head_is_link_order = true; |
4bd5a393 AM |
4611 | } |
4612 | ||
252b5132 | 4613 | static void |
1579bae1 AM |
4614 | print_output_section_statement |
4615 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
4616 | { |
4617 | asection *section = output_section_statement->bfd_section; | |
4618 | int len; | |
4619 | ||
4620 | if (output_section_statement != abs_output_section) | |
4621 | { | |
4622 | minfo ("\n%s", output_section_statement->name); | |
4623 | ||
4624 | if (section != NULL) | |
4625 | { | |
4626 | print_dot = section->vma; | |
4627 | ||
4628 | len = strlen (output_section_statement->name); | |
4629 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4630 | { | |
4631 | print_nl (); | |
4632 | len = 0; | |
4633 | } | |
6b9bd54c | 4634 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4635 | |
953dd97e | 4636 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4637 | |
67f744f3 AM |
4638 | if (section->vma != section->lma) |
4639 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4640 | |
4641 | if (output_section_statement->update_dot_tree != NULL) | |
4642 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4643 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
4644 | } |
4645 | ||
4646 | print_nl (); | |
4647 | } | |
4648 | ||
4649 | print_statement_list (output_section_statement->children.head, | |
4650 | output_section_statement); | |
4651 | } | |
4652 | ||
4653 | static void | |
1579bae1 AM |
4654 | print_assignment (lang_assignment_statement_type *assignment, |
4655 | lang_output_section_statement_type *output_section) | |
252b5132 | 4656 | { |
f38a2680 | 4657 | bool is_dot; |
afd7a018 | 4658 | etree_type *tree; |
8658f989 | 4659 | asection *osec; |
252b5132 | 4660 | |
6b9bd54c | 4661 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4662 | |
afd7a018 AM |
4663 | if (assignment->exp->type.node_class == etree_assert) |
4664 | { | |
f38a2680 | 4665 | is_dot = false; |
afd7a018 AM |
4666 | tree = assignment->exp->assert_s.child; |
4667 | } | |
4668 | else | |
4669 | { | |
4670 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4671 | |
4672 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4673 | tree = assignment->exp; |
afd7a018 AM |
4674 | } |
4675 | ||
8658f989 AM |
4676 | osec = output_section->bfd_section; |
4677 | if (osec == NULL) | |
4678 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4679 | |
4680 | if (assignment->exp->type.node_class != etree_provide) | |
4681 | exp_fold_tree (tree, osec, &print_dot); | |
4682 | else | |
f38a2680 | 4683 | expld.result.valid_p = false; |
c05b575a | 4684 | |
6b9bd54c AM |
4685 | char buf[32]; |
4686 | const char *str = buf; | |
e9ee469a | 4687 | if (expld.result.valid_p) |
7b17f854 | 4688 | { |
7b17f854 RS |
4689 | bfd_vma value; |
4690 | ||
4194268f AM |
4691 | if (assignment->exp->type.node_class == etree_assert |
4692 | || is_dot | |
4693 | || expld.assign_name != NULL) | |
3b83e13a | 4694 | { |
e9ee469a | 4695 | value = expld.result.value; |
10dbd1f3 | 4696 | |
7542af2a | 4697 | if (expld.result.section != NULL) |
e9ee469a | 4698 | value += expld.result.section->vma; |
7b17f854 | 4699 | |
6b9bd54c AM |
4700 | buf[0] = '0'; |
4701 | buf[1] = 'x'; | |
4702 | bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); | |
3b83e13a NC |
4703 | if (is_dot) |
4704 | print_dot = value; | |
4705 | } | |
4706 | else | |
4707 | { | |
4708 | struct bfd_link_hash_entry *h; | |
4709 | ||
4710 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
f38a2680 | 4711 | false, false, true); |
6a846243 AM |
4712 | if (h != NULL |
4713 | && (h->type == bfd_link_hash_defined | |
4714 | || h->type == bfd_link_hash_defweak)) | |
3b83e13a | 4715 | { |
10dbd1f3 | 4716 | value = h->u.def.value; |
f37a7048 NS |
4717 | value += h->u.def.section->output_section->vma; |
4718 | value += h->u.def.section->output_offset; | |
3b83e13a | 4719 | |
6b9bd54c AM |
4720 | buf[0] = '['; |
4721 | buf[1] = '0'; | |
4722 | buf[2] = 'x'; | |
4723 | bfd_sprintf_vma (link_info.output_bfd, buf + 3, value); | |
4724 | strcat (buf, "]"); | |
3b83e13a NC |
4725 | } |
4726 | else | |
6b9bd54c | 4727 | str = "[unresolved]"; |
3b83e13a | 4728 | } |
7b17f854 | 4729 | } |
252b5132 RH |
4730 | else |
4731 | { | |
c05b575a | 4732 | if (assignment->exp->type.node_class == etree_provide) |
6b9bd54c | 4733 | str = "[!provide]"; |
c05b575a | 4734 | else |
6b9bd54c | 4735 | str = "*undef*"; |
252b5132 | 4736 | } |
4194268f | 4737 | expld.assign_name = NULL; |
252b5132 | 4738 | |
6b9bd54c | 4739 | fprintf (config.map_file, "%-34s", str); |
252b5132 | 4740 | exp_print_tree (assignment->exp); |
252b5132 RH |
4741 | print_nl (); |
4742 | } | |
4743 | ||
4744 | static void | |
1579bae1 | 4745 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4746 | { |
0d41d9a2 | 4747 | if (statm->filename != NULL) |
906e58ca | 4748 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4749 | } |
4750 | ||
4751 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4752 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4753 | |
f38a2680 | 4754 | bool |
1579bae1 | 4755 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4756 | { |
1e9cc1c2 | 4757 | asection *sec = (asection *) ptr; |
252b5132 RH |
4758 | |
4759 | if ((hash_entry->type == bfd_link_hash_defined | |
4760 | || hash_entry->type == bfd_link_hash_defweak) | |
4761 | && sec == hash_entry->u.def.section) | |
4762 | { | |
6b9bd54c | 4763 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 RH |
4764 | minfo ("0x%V ", |
4765 | (hash_entry->u.def.value | |
4766 | + hash_entry->u.def.section->output_offset | |
4767 | + hash_entry->u.def.section->output_section->vma)); | |
4768 | ||
c1c8c1ef | 4769 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4770 | } |
4771 | ||
f38a2680 | 4772 | return true; |
252b5132 RH |
4773 | } |
4774 | ||
02688209 TG |
4775 | static int |
4776 | hash_entry_addr_cmp (const void *a, const void *b) | |
4777 | { | |
4778 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4779 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4780 | ||
4781 | if (l->u.def.value < r->u.def.value) | |
4782 | return -1; | |
4783 | else if (l->u.def.value > r->u.def.value) | |
4784 | return 1; | |
4785 | else | |
4786 | return 0; | |
4787 | } | |
4788 | ||
35835446 | 4789 | static void |
e0f6802f | 4790 | print_all_symbols (asection *sec) |
35835446 | 4791 | { |
a48931cc | 4792 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4793 | struct map_symbol_def *def; |
02688209 TG |
4794 | struct bfd_link_hash_entry **entries; |
4795 | unsigned int i; | |
35835446 | 4796 | |
afd7a018 AM |
4797 | if (!ud) |
4798 | return; | |
4799 | ||
35835446 | 4800 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4801 | |
02688209 | 4802 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4803 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4804 | obstack_alloc (&map_obstack, |
4805 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
4806 | |
4807 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4808 | entries[i] = def->entry; | |
4809 | ||
4810 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4724d37e | 4811 | hash_entry_addr_cmp); |
02688209 TG |
4812 | |
4813 | /* Print the symbols. */ | |
4814 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
3edf7b9f | 4815 | ldemul_print_symbol (entries[i], sec); |
02688209 TG |
4816 | |
4817 | obstack_free (&map_obstack, entries); | |
35835446 JR |
4818 | } |
4819 | ||
496917ce NC |
4820 | /* Returns TRUE if SYM is a symbol suitable for printing |
4821 | in a linker map as a local symbol. */ | |
4822 | ||
4823 | static bool | |
4824 | ld_is_local_symbol (asymbol * sym) | |
4825 | { | |
4826 | const char * name = bfd_asymbol_name (sym); | |
4827 | ||
4828 | if (name == NULL || *name == 0) | |
4829 | return false; | |
4830 | ||
4831 | if (strcmp (name, "(null)") == 0) | |
4832 | return false; | |
4833 | ||
4834 | /* Skip .Lxxx and such like. */ | |
4835 | if (bfd_is_local_label (link_info.output_bfd, sym)) | |
4836 | return false; | |
4837 | ||
4838 | /* FIXME: This is intended to skip ARM mapping symbols, | |
4839 | which for some reason are not excluded by bfd_is_local_label, | |
4840 | but maybe it is wrong for other architectures. | |
4841 | It would be better to fix bfd_is_local_label. */ | |
4842 | if (*name == '$') | |
4843 | return false; | |
4844 | ||
4845 | /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present | |
4846 | in the hash table, so do not print duplicates here. */ | |
4847 | struct bfd_link_hash_entry * h; | |
4848 | h = bfd_link_hash_lookup (link_info.hash, name, false /* create */, | |
4849 | false /* copy */, true /* follow */); | |
4850 | if (h == NULL) | |
4851 | return true; | |
4852 | ||
4853 | /* Symbols from the plugin owned BFD will not get their own | |
4854 | iteration of this function, but can be on the link_info | |
4855 | list. So include them here. */ | |
4856 | if (h->u.def.section->owner != NULL | |
4857 | && ((bfd_get_file_flags (h->u.def.section->owner) & (BFD_LINKER_CREATED | BFD_PLUGIN)) | |
4858 | == (BFD_LINKER_CREATED | BFD_PLUGIN))) | |
4859 | return true; | |
4860 | ||
4861 | return false; | |
4862 | } | |
4863 | ||
252b5132 RH |
4864 | /* Print information about an input section to the map file. */ |
4865 | ||
4866 | static void | |
f38a2680 | 4867 | print_input_section (asection *i, bool is_discarded) |
252b5132 | 4868 | { |
eea6121a | 4869 | bfd_size_type size = i->size; |
abe6ac95 AM |
4870 | int len; |
4871 | bfd_vma addr; | |
e5caa5e0 | 4872 | |
61826503 | 4873 | init_opb (i); |
4d4920ec | 4874 | |
6b9bd54c | 4875 | minfo (" %s", i->name); |
252b5132 | 4876 | |
abe6ac95 AM |
4877 | len = 1 + strlen (i->name); |
4878 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4879 | { | |
4880 | print_nl (); | |
4881 | len = 0; | |
4882 | } | |
6b9bd54c | 4883 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4884 | |
f13a99db AM |
4885 | if (i->output_section != NULL |
4886 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
4887 | addr = i->output_section->vma + i->output_offset; |
4888 | else | |
4889 | { | |
4890 | addr = print_dot; | |
d64703c6 TG |
4891 | if (!is_discarded) |
4892 | size = 0; | |
abe6ac95 | 4893 | } |
252b5132 | 4894 | |
6b9bd54c AM |
4895 | char buf[32]; |
4896 | bfd_sprintf_vma (link_info.output_bfd, buf, addr); | |
4897 | minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner); | |
252b5132 | 4898 | |
abe6ac95 AM |
4899 | if (size != i->rawsize && i->rawsize != 0) |
4900 | { | |
6b9bd54c AM |
4901 | len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); |
4902 | print_spaces (len); | |
b0a7971a | 4903 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 4904 | } |
252b5132 | 4905 | |
f13a99db AM |
4906 | if (i->output_section != NULL |
4907 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 4908 | { |
c0f00686 | 4909 | if (link_info.reduce_memory_overheads) |
3edf7b9f | 4910 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); |
abe6ac95 AM |
4911 | else |
4912 | print_all_symbols (i); | |
4913 | ||
5590fba9 NC |
4914 | /* Update print_dot, but make sure that we do not move it |
4915 | backwards - this could happen if we have overlays and a | |
4916 | later overlay is shorter than an earier one. */ | |
4917 | if (addr + TO_ADDR (size) > print_dot) | |
4918 | print_dot = addr + TO_ADDR (size); | |
496917ce NC |
4919 | |
4920 | if (config.print_map_locals) | |
4921 | { | |
4922 | long storage_needed; | |
4923 | ||
4924 | /* FIXME: It would be better to cache this table, rather | |
4925 | than recreating it for each output section. */ | |
4926 | /* FIXME: This call is not working for non-ELF based targets. | |
4927 | Find out why. */ | |
4928 | storage_needed = bfd_get_symtab_upper_bound (link_info.output_bfd); | |
4929 | if (storage_needed > 0) | |
4930 | { | |
4931 | asymbol ** symbol_table; | |
4932 | long number_of_symbols; | |
4933 | long j; | |
4934 | ||
4935 | symbol_table = xmalloc (storage_needed); | |
4936 | number_of_symbols = bfd_canonicalize_symtab (link_info.output_bfd, symbol_table); | |
4937 | ||
4938 | for (j = 0; j < number_of_symbols; j++) | |
4939 | { | |
4940 | asymbol * sym = symbol_table[j]; | |
4941 | bfd_vma sym_addr = sym->value + i->output_section->vma; | |
4942 | ||
4943 | if (sym->section == i->output_section | |
4944 | && (sym->flags & BSF_LOCAL) != 0 | |
4945 | && sym_addr >= addr | |
4946 | && sym_addr < print_dot | |
4947 | && ld_is_local_symbol (sym)) | |
4948 | { | |
4949 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
4950 | minfo ("0x%V (local) %s\n", sym_addr, bfd_asymbol_name (sym)); | |
4951 | } | |
4952 | } | |
4953 | ||
4954 | free (symbol_table); | |
4955 | } | |
4956 | } | |
252b5132 RH |
4957 | } |
4958 | } | |
4959 | ||
4960 | static void | |
1579bae1 | 4961 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 4962 | { |
2c382fb6 AM |
4963 | size_t size; |
4964 | unsigned char *p; | |
4965 | fputs (" FILL mask 0x", config.map_file); | |
4966 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4967 | fprintf (config.map_file, "%02x", *p); | |
4968 | fputs ("\n", config.map_file); | |
252b5132 RH |
4969 | } |
4970 | ||
4971 | static void | |
1579bae1 | 4972 | print_data_statement (lang_data_statement_type *data) |
252b5132 | 4973 | { |
252b5132 RH |
4974 | bfd_vma addr; |
4975 | bfd_size_type size; | |
4976 | const char *name; | |
4977 | ||
61826503 | 4978 | init_opb (data->output_section); |
6b9bd54c | 4979 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4980 | |
7fabd029 | 4981 | addr = data->output_offset; |
252b5132 RH |
4982 | if (data->output_section != NULL) |
4983 | addr += data->output_section->vma; | |
4984 | ||
4985 | switch (data->type) | |
4986 | { | |
4987 | default: | |
4988 | abort (); | |
4989 | case BYTE: | |
4990 | size = BYTE_SIZE; | |
4991 | name = "BYTE"; | |
4992 | break; | |
4993 | case SHORT: | |
4994 | size = SHORT_SIZE; | |
4995 | name = "SHORT"; | |
4996 | break; | |
4997 | case LONG: | |
4998 | size = LONG_SIZE; | |
4999 | name = "LONG"; | |
5000 | break; | |
5001 | case QUAD: | |
5002 | size = QUAD_SIZE; | |
5003 | name = "QUAD"; | |
5004 | break; | |
5005 | case SQUAD: | |
5006 | size = QUAD_SIZE; | |
5007 | name = "SQUAD"; | |
5008 | break; | |
5009 | } | |
5010 | ||
953dd97e DG |
5011 | if (size < TO_SIZE ((unsigned) 1)) |
5012 | size = TO_SIZE ((unsigned) 1); | |
5013 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
252b5132 RH |
5014 | |
5015 | if (data->exp->type.node_class != etree_value) | |
5016 | { | |
5017 | print_space (); | |
5018 | exp_print_tree (data->exp); | |
5019 | } | |
5020 | ||
5021 | print_nl (); | |
5022 | ||
e5caa5e0 | 5023 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
5024 | } |
5025 | ||
5026 | /* Print an address statement. These are generated by options like | |
5027 | -Ttext. */ | |
5028 | ||
5029 | static void | |
1579bae1 | 5030 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
5031 | { |
5032 | minfo (_("Address of section %s set to "), address->section_name); | |
5033 | exp_print_tree (address->address); | |
5034 | print_nl (); | |
5035 | } | |
5036 | ||
5037 | /* Print a reloc statement. */ | |
5038 | ||
5039 | static void | |
1579bae1 | 5040 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 | 5041 | { |
252b5132 RH |
5042 | bfd_vma addr; |
5043 | bfd_size_type size; | |
5044 | ||
61826503 | 5045 | init_opb (reloc->output_section); |
6b9bd54c | 5046 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 5047 | |
7fabd029 | 5048 | addr = reloc->output_offset; |
252b5132 RH |
5049 | if (reloc->output_section != NULL) |
5050 | addr += reloc->output_section->vma; | |
5051 | ||
5052 | size = bfd_get_reloc_size (reloc->howto); | |
5053 | ||
953dd97e | 5054 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
5055 | |
5056 | if (reloc->name != NULL) | |
5057 | minfo ("%s+", reloc->name); | |
5058 | else | |
5059 | minfo ("%s+", reloc->section->name); | |
5060 | ||
5061 | exp_print_tree (reloc->addend_exp); | |
5062 | ||
5063 | print_nl (); | |
5064 | ||
e5caa5e0 | 5065 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 5066 | } |
252b5132 RH |
5067 | |
5068 | static void | |
1579bae1 | 5069 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
5070 | { |
5071 | int len; | |
5072 | bfd_vma addr; | |
5073 | ||
61826503 | 5074 | init_opb (s->output_section); |
252b5132 RH |
5075 | minfo (" *fill*"); |
5076 | ||
5077 | len = sizeof " *fill*" - 1; | |
6b9bd54c | 5078 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 RH |
5079 | |
5080 | addr = s->output_offset; | |
5081 | if (s->output_section != NULL) | |
5082 | addr += s->output_section->vma; | |
953dd97e | 5083 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 5084 | |
2c382fb6 AM |
5085 | if (s->fill->size != 0) |
5086 | { | |
5087 | size_t size; | |
5088 | unsigned char *p; | |
5089 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
5090 | fprintf (config.map_file, "%02x", *p); | |
5091 | } | |
252b5132 RH |
5092 | |
5093 | print_nl (); | |
5094 | ||
e5caa5e0 | 5095 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
5096 | } |
5097 | ||
5098 | static void | |
1579bae1 AM |
5099 | print_wild_statement (lang_wild_statement_type *w, |
5100 | lang_output_section_statement_type *os) | |
252b5132 | 5101 | { |
b6bf44ba AM |
5102 | struct wildcard_list *sec; |
5103 | ||
252b5132 RH |
5104 | print_space (); |
5105 | ||
8f1732fc AB |
5106 | if (w->exclude_name_list) |
5107 | { | |
5108 | name_list *tmp; | |
5109 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
5110 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
6c19b93b | 5111 | minfo (" %s", tmp->name); |
8f1732fc AB |
5112 | minfo (") "); |
5113 | } | |
5114 | ||
252b5132 | 5115 | if (w->filenames_sorted) |
2b94abd4 | 5116 | minfo ("SORT_BY_NAME("); |
08da4cac | 5117 | if (w->filename != NULL) |
252b5132 RH |
5118 | minfo ("%s", w->filename); |
5119 | else | |
5120 | minfo ("*"); | |
5121 | if (w->filenames_sorted) | |
5122 | minfo (")"); | |
5123 | ||
5124 | minfo ("("); | |
b6bf44ba AM |
5125 | for (sec = w->section_list; sec; sec = sec->next) |
5126 | { | |
2b94abd4 AB |
5127 | int closing_paren = 0; |
5128 | ||
5129 | switch (sec->spec.sorted) | |
6c19b93b AM |
5130 | { |
5131 | case none: | |
5132 | break; | |
5133 | ||
5134 | case by_name: | |
5135 | minfo ("SORT_BY_NAME("); | |
5136 | closing_paren = 1; | |
5137 | break; | |
5138 | ||
5139 | case by_alignment: | |
5140 | minfo ("SORT_BY_ALIGNMENT("); | |
5141 | closing_paren = 1; | |
5142 | break; | |
5143 | ||
5144 | case by_name_alignment: | |
5145 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
5146 | closing_paren = 2; | |
5147 | break; | |
5148 | ||
5149 | case by_alignment_name: | |
5150 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
5151 | closing_paren = 2; | |
5152 | break; | |
5153 | ||
5154 | case by_none: | |
5155 | minfo ("SORT_NONE("); | |
5156 | closing_paren = 1; | |
5157 | break; | |
5158 | ||
5159 | case by_init_priority: | |
5160 | minfo ("SORT_BY_INIT_PRIORITY("); | |
5161 | closing_paren = 1; | |
5162 | break; | |
5163 | } | |
2b94abd4 | 5164 | |
b6bf44ba AM |
5165 | if (sec->spec.exclude_name_list != NULL) |
5166 | { | |
5167 | name_list *tmp; | |
34786259 | 5168 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 5169 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
5170 | minfo (" %s", tmp->name); |
5171 | minfo (") "); | |
b6bf44ba AM |
5172 | } |
5173 | if (sec->spec.name != NULL) | |
5174 | minfo ("%s", sec->spec.name); | |
5175 | else | |
5176 | minfo ("*"); | |
2b94abd4 | 5177 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 5178 | minfo (")"); |
34786259 AM |
5179 | if (sec->next) |
5180 | minfo (" "); | |
b6bf44ba | 5181 | } |
252b5132 RH |
5182 | minfo (")"); |
5183 | ||
5184 | print_nl (); | |
5185 | ||
5186 | print_statement_list (w->children.head, os); | |
5187 | } | |
5188 | ||
5189 | /* Print a group statement. */ | |
5190 | ||
5191 | static void | |
1579bae1 AM |
5192 | print_group (lang_group_statement_type *s, |
5193 | lang_output_section_statement_type *os) | |
252b5132 RH |
5194 | { |
5195 | fprintf (config.map_file, "START GROUP\n"); | |
5196 | print_statement_list (s->children.head, os); | |
5197 | fprintf (config.map_file, "END GROUP\n"); | |
5198 | } | |
5199 | ||
5200 | /* Print the list of statements in S. | |
5201 | This can be called for any statement type. */ | |
5202 | ||
5203 | static void | |
1579bae1 AM |
5204 | print_statement_list (lang_statement_union_type *s, |
5205 | lang_output_section_statement_type *os) | |
252b5132 RH |
5206 | { |
5207 | while (s != NULL) | |
5208 | { | |
5209 | print_statement (s, os); | |
bba1a0c0 | 5210 | s = s->header.next; |
252b5132 RH |
5211 | } |
5212 | } | |
5213 | ||
5214 | /* Print the first statement in statement list S. | |
5215 | This can be called for any statement type. */ | |
5216 | ||
5217 | static void | |
1579bae1 AM |
5218 | print_statement (lang_statement_union_type *s, |
5219 | lang_output_section_statement_type *os) | |
252b5132 RH |
5220 | { |
5221 | switch (s->header.type) | |
5222 | { | |
5223 | default: | |
5224 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
5225 | FAIL (); | |
5226 | break; | |
5227 | case lang_constructors_statement_enum: | |
5228 | if (constructor_list.head != NULL) | |
5229 | { | |
5230 | if (constructors_sorted) | |
5231 | minfo (" SORT (CONSTRUCTORS)\n"); | |
5232 | else | |
5233 | minfo (" CONSTRUCTORS\n"); | |
5234 | print_statement_list (constructor_list.head, os); | |
5235 | } | |
5236 | break; | |
5237 | case lang_wild_statement_enum: | |
5238 | print_wild_statement (&s->wild_statement, os); | |
5239 | break; | |
5240 | case lang_address_statement_enum: | |
5241 | print_address_statement (&s->address_statement); | |
5242 | break; | |
5243 | case lang_object_symbols_statement_enum: | |
5244 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
5245 | break; | |
5246 | case lang_fill_statement_enum: | |
5247 | print_fill_statement (&s->fill_statement); | |
5248 | break; | |
5249 | case lang_data_statement_enum: | |
5250 | print_data_statement (&s->data_statement); | |
5251 | break; | |
5252 | case lang_reloc_statement_enum: | |
5253 | print_reloc_statement (&s->reloc_statement); | |
5254 | break; | |
5255 | case lang_input_section_enum: | |
f38a2680 | 5256 | print_input_section (s->input_section.section, false); |
252b5132 RH |
5257 | break; |
5258 | case lang_padding_statement_enum: | |
5259 | print_padding_statement (&s->padding_statement); | |
5260 | break; | |
5261 | case lang_output_section_statement_enum: | |
5262 | print_output_section_statement (&s->output_section_statement); | |
5263 | break; | |
5264 | case lang_assignment_statement_enum: | |
5265 | print_assignment (&s->assignment_statement, os); | |
5266 | break; | |
5267 | case lang_target_statement_enum: | |
5268 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5269 | break; | |
5270 | case lang_output_statement_enum: | |
5271 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5272 | if (output_target != NULL) | |
5273 | minfo (" %s", output_target); | |
5274 | minfo (")\n"); | |
5275 | break; | |
5276 | case lang_input_statement_enum: | |
5277 | print_input_statement (&s->input_statement); | |
5278 | break; | |
5279 | case lang_group_statement_enum: | |
5280 | print_group (&s->group_statement, os); | |
5281 | break; | |
53d25da6 AM |
5282 | case lang_insert_statement_enum: |
5283 | minfo ("INSERT %s %s\n", | |
5284 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5285 | s->insert_statement.where); | |
5286 | break; | |
252b5132 RH |
5287 | } |
5288 | } | |
5289 | ||
5290 | static void | |
1579bae1 | 5291 | print_statements (void) |
252b5132 RH |
5292 | { |
5293 | print_statement_list (statement_list.head, abs_output_section); | |
5294 | } | |
5295 | ||
5296 | /* Print the first N statements in statement list S to STDERR. | |
5297 | If N == 0, nothing is printed. | |
5298 | If N < 0, the entire list is printed. | |
5299 | Intended to be called from GDB. */ | |
5300 | ||
5301 | void | |
1579bae1 | 5302 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
5303 | { |
5304 | FILE *map_save = config.map_file; | |
5305 | ||
5306 | config.map_file = stderr; | |
5307 | ||
5308 | if (n < 0) | |
5309 | print_statement_list (s, abs_output_section); | |
5310 | else | |
5311 | { | |
5312 | while (s && --n >= 0) | |
5313 | { | |
5314 | print_statement (s, abs_output_section); | |
bba1a0c0 | 5315 | s = s->header.next; |
252b5132 RH |
5316 | } |
5317 | } | |
5318 | ||
5319 | config.map_file = map_save; | |
5320 | } | |
5321 | ||
b3327aad | 5322 | static void |
1579bae1 AM |
5323 | insert_pad (lang_statement_union_type **ptr, |
5324 | fill_type *fill, | |
1a69ff95 | 5325 | bfd_size_type alignment_needed, |
1579bae1 AM |
5326 | asection *output_section, |
5327 | bfd_vma dot) | |
252b5132 | 5328 | { |
b7761f11 | 5329 | static fill_type zero_fill; |
e9ee469a | 5330 | lang_statement_union_type *pad = NULL; |
b3327aad | 5331 | |
e9ee469a AM |
5332 | if (ptr != &statement_list.head) |
5333 | pad = ((lang_statement_union_type *) | |
5334 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5335 | if (pad != NULL | |
5336 | && pad->header.type == lang_padding_statement_enum | |
5337 | && pad->padding_statement.output_section == output_section) | |
5338 | { | |
5339 | /* Use the existing pad statement. */ | |
5340 | } | |
5341 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
5342 | && pad->header.type == lang_padding_statement_enum |
5343 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5344 | { |
e9ee469a | 5345 | /* Use the existing pad statement. */ |
252b5132 | 5346 | } |
b3327aad | 5347 | else |
252b5132 | 5348 | { |
b3327aad | 5349 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5350 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5351 | pad->header.next = *ptr; |
5352 | *ptr = pad; | |
5353 | pad->header.type = lang_padding_statement_enum; | |
5354 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5355 | if (fill == NULL) |
2c382fb6 | 5356 | fill = &zero_fill; |
b3327aad | 5357 | pad->padding_statement.fill = fill; |
252b5132 | 5358 | } |
b3327aad AM |
5359 | pad->padding_statement.output_offset = dot - output_section->vma; |
5360 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5361 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5362 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5363 | - output_section->vma); | |
252b5132 RH |
5364 | } |
5365 | ||
08da4cac KH |
5366 | /* Work out how much this section will move the dot point. */ |
5367 | ||
252b5132 | 5368 | static bfd_vma |
6feb9908 AM |
5369 | size_input_section |
5370 | (lang_statement_union_type **this_ptr, | |
5371 | lang_output_section_statement_type *output_section_statement, | |
5372 | fill_type *fill, | |
f38a2680 | 5373 | bool *removed, |
6feb9908 | 5374 | bfd_vma dot) |
252b5132 RH |
5375 | { |
5376 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5377 | asection *i = is->section; | |
93d1b056 | 5378 | asection *o = output_section_statement->bfd_section; |
abf874aa CL |
5379 | *removed = 0; |
5380 | ||
5381 | if (link_info.non_contiguous_regions) | |
5382 | { | |
5383 | /* If the input section I has already been successfully assigned | |
5384 | to an output section other than O, don't bother with it and | |
5385 | let the caller remove it from the list. Keep processing in | |
5386 | case we have already handled O, because the repeated passes | |
5387 | have reinitialized its size. */ | |
5388 | if (i->already_assigned && i->already_assigned != o) | |
5389 | { | |
5390 | *removed = 1; | |
5391 | return dot; | |
5392 | } | |
5393 | } | |
252b5132 | 5394 | |
93d1b056 AM |
5395 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5396 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5397 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5398 | || output_section_statement->ignored) | |
93d1b056 AM |
5399 | i->output_offset = dot - o->vma; |
5400 | else | |
252b5132 | 5401 | { |
1a69ff95 | 5402 | bfd_size_type alignment_needed; |
b3327aad AM |
5403 | |
5404 | /* Align this section first to the input sections requirement, | |
5405 | then to the output section's requirement. If this alignment | |
5406 | is greater than any seen before, then record it too. Perform | |
5407 | the alignment by inserting a magic 'padding' statement. */ | |
5408 | ||
3d9c8f6b AM |
5409 | if (output_section_statement->subsection_alignment != NULL) |
5410 | i->alignment_power | |
5411 | = exp_get_power (output_section_statement->subsection_alignment, | |
5412 | "subsection alignment"); | |
b3327aad | 5413 | |
b3327aad AM |
5414 | if (o->alignment_power < i->alignment_power) |
5415 | o->alignment_power = i->alignment_power; | |
252b5132 | 5416 | |
b3327aad AM |
5417 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5418 | ||
5419 | if (alignment_needed != 0) | |
5420 | { | |
e5caa5e0 | 5421 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5422 | dot += alignment_needed; |
5423 | } | |
252b5132 | 5424 | |
abf874aa CL |
5425 | if (link_info.non_contiguous_regions) |
5426 | { | |
5427 | /* If I would overflow O, let the caller remove I from the | |
5428 | list. */ | |
5429 | if (output_section_statement->region) | |
5430 | { | |
5431 | bfd_vma end = output_section_statement->region->origin | |
5432 | + output_section_statement->region->length; | |
5433 | ||
5434 | if (dot + TO_ADDR (i->size) > end) | |
5435 | { | |
5436 | if (i->flags & SEC_LINKER_CREATED) | |
c63d4862 AM |
5437 | einfo (_("%F%P: Output section `%pA' not large enough for " |
5438 | "the linker-created stubs section `%pA'.\n"), | |
5439 | i->output_section, i); | |
abf874aa CL |
5440 | |
5441 | if (i->rawsize && i->rawsize != i->size) | |
53215f21 | 5442 | einfo (_("%F%P: Relaxation not supported with " |
c63d4862 AM |
5443 | "--enable-non-contiguous-regions (section `%pA' " |
5444 | "would overflow `%pA' after it changed size).\n"), | |
5445 | i, i->output_section); | |
abf874aa CL |
5446 | |
5447 | *removed = 1; | |
5448 | dot = end; | |
5449 | i->output_section = NULL; | |
5450 | return dot; | |
5451 | } | |
5452 | } | |
5453 | } | |
5454 | ||
08da4cac | 5455 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5456 | i->output_offset = dot - o->vma; |
252b5132 | 5457 | |
08da4cac | 5458 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5459 | dot += TO_ADDR (i->size); |
8772de11 MR |
5460 | if (!(o->flags & SEC_FIXED_SIZE)) |
5461 | o->size = TO_SIZE (dot - o->vma); | |
abf874aa CL |
5462 | |
5463 | if (link_info.non_contiguous_regions) | |
5464 | { | |
5465 | /* Record that I was successfully assigned to O, and update | |
5466 | its actual output section too. */ | |
5467 | i->already_assigned = o; | |
5468 | i->output_section = o; | |
5469 | } | |
252b5132 | 5470 | } |
252b5132 RH |
5471 | |
5472 | return dot; | |
5473 | } | |
5474 | ||
a87dd97a AM |
5475 | struct check_sec |
5476 | { | |
5477 | asection *sec; | |
f38a2680 | 5478 | bool warned; |
a87dd97a AM |
5479 | }; |
5480 | ||
5daa8fe7 L |
5481 | static int |
5482 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5483 | { | |
a87dd97a AM |
5484 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; |
5485 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5486 | ||
5487 | if (sec1->lma < sec2->lma) | |
5488 | return -1; | |
5489 | else if (sec1->lma > sec2->lma) | |
5490 | return 1; | |
5491 | else if (sec1->id < sec2->id) | |
5492 | return -1; | |
5493 | else if (sec1->id > sec2->id) | |
5494 | return 1; | |
5495 | ||
5496 | return 0; | |
5497 | } | |
5498 | ||
5499 | static int | |
5500 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5501 | { | |
5502 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5503 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5daa8fe7 | 5504 | |
a87dd97a | 5505 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5506 | return -1; |
a87dd97a | 5507 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5508 | return 1; |
7f6a71ff JM |
5509 | else if (sec1->id < sec2->id) |
5510 | return -1; | |
5511 | else if (sec1->id > sec2->id) | |
5512 | return 1; | |
5daa8fe7 L |
5513 | |
5514 | return 0; | |
5515 | } | |
5516 | ||
2e4a7aea AM |
5517 | #define IS_TBSS(s) \ |
5518 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5519 | ||
eea6121a | 5520 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5521 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5522 | |
252b5132 | 5523 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5524 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5525 | section addresses of the two sections. Also check whether any memory |
5526 | region has overflowed. */ | |
08da4cac | 5527 | |
252b5132 | 5528 | static void |
1579bae1 | 5529 | lang_check_section_addresses (void) |
252b5132 | 5530 | { |
f4427a75 | 5531 | asection *s, *p; |
a87dd97a AM |
5532 | struct check_sec *sections; |
5533 | size_t i, count; | |
d40e34db | 5534 | bfd_vma addr_mask; |
5daa8fe7 L |
5535 | bfd_vma s_start; |
5536 | bfd_vma s_end; | |
a87dd97a AM |
5537 | bfd_vma p_start = 0; |
5538 | bfd_vma p_end = 0; | |
20e56351 | 5539 | lang_memory_region_type *m; |
f38a2680 | 5540 | bool overlays; |
5daa8fe7 | 5541 | |
21701718 | 5542 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
5543 | addr_mask = ((bfd_vma) 1 << |
5544 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5545 | addr_mask = (addr_mask << 1) + 1; | |
5546 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
21701718 TG |
5547 | if ((s->flags & SEC_ALLOC) != 0) |
5548 | { | |
5549 | s_end = (s->vma + s->size) & addr_mask; | |
5550 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5551 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5552 | s->name); | |
5553 | else | |
5554 | { | |
5555 | s_end = (s->lma + s->size) & addr_mask; | |
5556 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5557 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5558 | s->name); | |
5559 | } | |
5560 | } | |
d40e34db | 5561 | |
f13a99db | 5562 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5563 | return; |
5564 | ||
a87dd97a AM |
5565 | count = bfd_count_sections (link_info.output_bfd); |
5566 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5567 | |
5568 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5569 | count = 0; |
f13a99db | 5570 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5571 | { |
a87dd97a | 5572 | if (IGNORE_SECTION (s) |
ec6d26be | 5573 | || s->size == 0) |
33275c22 | 5574 | continue; |
5f992e62 | 5575 | |
a87dd97a | 5576 | sections[count].sec = s; |
f38a2680 | 5577 | sections[count].warned = false; |
5daa8fe7 L |
5578 | count++; |
5579 | } | |
329c1c86 | 5580 | |
5daa8fe7 | 5581 | if (count <= 1) |
136a43b7 AM |
5582 | { |
5583 | free (sections); | |
5584 | return; | |
5585 | } | |
5f992e62 | 5586 | |
a87dd97a | 5587 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5588 | |
136a43b7 | 5589 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5590 | loadable sections, even with overlays. */ |
5591 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5592 | { |
a87dd97a | 5593 | s = sections[i].sec; |
61826503 | 5594 | init_opb (s); |
a87dd97a AM |
5595 | if ((s->flags & SEC_LOAD) != 0) |
5596 | { | |
5597 | s_start = s->lma; | |
5598 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5599 | ||
5600 | /* Look for an overlap. We have sorted sections by lma, so | |
5601 | we know that s_start >= p_start. Besides the obvious | |
5602 | case of overlap when the current section starts before | |
5603 | the previous one ends, we also must have overlap if the | |
5604 | previous section wraps around the address space. */ | |
5605 | if (p != NULL | |
5606 | && (s_start <= p_end | |
5607 | || p_end < p_start)) | |
5608 | { | |
5609 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5610 | " overlaps section %s LMA [%V,%V]\n"), | |
5611 | s->name, s_start, s_end, p->name, p_start, p_end); | |
f38a2680 | 5612 | sections[i].warned = true; |
a87dd97a AM |
5613 | } |
5614 | p = s; | |
5615 | p_start = s_start; | |
5616 | p_end = s_end; | |
5617 | } | |
5618 | } | |
5619 | ||
136a43b7 AM |
5620 | /* If any non-zero size allocated section (excluding tbss) starts at |
5621 | exactly the same VMA as another such section, then we have | |
5622 | overlays. Overlays generated by the OVERLAY keyword will have | |
5623 | this property. It is possible to intentionally generate overlays | |
5624 | that fail this test, but it would be unusual. */ | |
5625 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
f38a2680 | 5626 | overlays = false; |
136a43b7 AM |
5627 | p_start = sections[0].sec->vma; |
5628 | for (i = 1; i < count; i++) | |
a87dd97a | 5629 | { |
136a43b7 AM |
5630 | s_start = sections[i].sec->vma; |
5631 | if (p_start == s_start) | |
5632 | { | |
f38a2680 | 5633 | overlays = true; |
136a43b7 AM |
5634 | break; |
5635 | } | |
5636 | p_start = s_start; | |
5637 | } | |
a87dd97a | 5638 | |
136a43b7 AM |
5639 | /* Now check section VMAs if no overlays were detected. */ |
5640 | if (!overlays) | |
5641 | { | |
a87dd97a AM |
5642 | for (p = NULL, i = 0; i < count; i++) |
5643 | { | |
5644 | s = sections[i].sec; | |
61826503 | 5645 | init_opb (s); |
a87dd97a AM |
5646 | s_start = s->vma; |
5647 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5648 | ||
5649 | if (p != NULL | |
5650 | && !sections[i].warned | |
5651 | && (s_start <= p_end | |
5652 | || p_end < p_start)) | |
5653 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5654 | " overlaps section %s VMA [%V,%V]\n"), | |
5655 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5656 | p = s; | |
5657 | p_start = s_start; | |
5658 | p_end = s_end; | |
5659 | } | |
33275c22 | 5660 | } |
5daa8fe7 L |
5661 | |
5662 | free (sections); | |
20e56351 DJ |
5663 | |
5664 | /* If any memory region has overflowed, report by how much. | |
5665 | We do not issue this diagnostic for regions that had sections | |
5666 | explicitly placed outside their bounds; os_region_check's | |
5667 | diagnostics are adequate for that case. | |
5668 | ||
5669 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5670 | might overflow a 32-bit integer. There is, alas, no way to print | |
5671 | a bfd_vma quantity in decimal. */ | |
5672 | for (m = lang_memory_region_list; m; m = m->next) | |
5673 | if (m->had_full_message) | |
992a06ee AM |
5674 | { |
5675 | unsigned long over = m->current - (m->origin + m->length); | |
5676 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5677 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5678 | over), | |
5679 | m->name_list.name, over); | |
5680 | } | |
252b5132 RH |
5681 | } |
5682 | ||
562d3460 TW |
5683 | /* Make sure the new address is within the region. We explicitly permit the |
5684 | current address to be at the exact end of the region when the address is | |
5685 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 5686 | calculation wraps around. */ |
562d3460 TW |
5687 | |
5688 | static void | |
1579bae1 | 5689 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5690 | lang_memory_region_type *region, |
1579bae1 | 5691 | etree_type *tree, |
91d6fa6a | 5692 | bfd_vma rbase) |
562d3460 TW |
5693 | { |
5694 | if ((region->current < region->origin | |
5695 | || (region->current - region->origin > region->length)) | |
5696 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5697 | || rbase == 0)) |
562d3460 | 5698 | { |
1579bae1 | 5699 | if (tree != NULL) |
b7a26f91 | 5700 | { |
871b3ab2 | 5701 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5702 | " is not within region `%s'\n"), |
b7a26f91 KH |
5703 | region->current, |
5704 | os->bfd_section->owner, | |
5705 | os->bfd_section->name, | |
4a93e180 | 5706 | region->name_list.name); |
b7a26f91 | 5707 | } |
20e56351 | 5708 | else if (!region->had_full_message) |
b7a26f91 | 5709 | { |
f38a2680 | 5710 | region->had_full_message = true; |
20e56351 | 5711 | |
871b3ab2 | 5712 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5713 | os->bfd_section->owner, |
20e56351 | 5714 | os->bfd_section->name, |
4a93e180 | 5715 | region->name_list.name); |
b7a26f91 | 5716 | } |
562d3460 TW |
5717 | } |
5718 | } | |
5719 | ||
ed1794ee | 5720 | static void |
2085ee26 | 5721 | ldlang_check_relro_region (lang_statement_union_type *s) |
ed1794ee | 5722 | { |
2085ee26 AM |
5723 | seg_align_type *seg = &expld.dataseg; |
5724 | ||
ed1794ee L |
5725 | if (seg->relro == exp_seg_relro_start) |
5726 | { | |
5727 | if (!seg->relro_start_stat) | |
5728 | seg->relro_start_stat = s; | |
5729 | else | |
5730 | { | |
5731 | ASSERT (seg->relro_start_stat == s); | |
5732 | } | |
5733 | } | |
5734 | else if (seg->relro == exp_seg_relro_end) | |
5735 | { | |
5736 | if (!seg->relro_end_stat) | |
5737 | seg->relro_end_stat = s; | |
5738 | else | |
5739 | { | |
5740 | ASSERT (seg->relro_end_stat == s); | |
5741 | } | |
5742 | } | |
5743 | } | |
5744 | ||
252b5132 RH |
5745 | /* Set the sizes for all the output sections. */ |
5746 | ||
2d20f7bf | 5747 | static bfd_vma |
1579bae1 | 5748 | lang_size_sections_1 |
e535e147 | 5749 | (lang_statement_union_type **prev, |
1579bae1 | 5750 | lang_output_section_statement_type *output_section_statement, |
1579bae1 AM |
5751 | fill_type *fill, |
5752 | bfd_vma dot, | |
f38a2680 AM |
5753 | bool *relax, |
5754 | bool check_regions) | |
252b5132 | 5755 | { |
e535e147 | 5756 | lang_statement_union_type *s; |
abf874aa | 5757 | lang_statement_union_type *prev_s = NULL; |
f38a2680 | 5758 | bool removed_prev_s = false; |
e535e147 | 5759 | |
252b5132 | 5760 | /* Size up the sections from their constituent parts. */ |
abf874aa | 5761 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) |
252b5132 | 5762 | { |
f38a2680 | 5763 | bool removed = false; |
abf874aa | 5764 | |
252b5132 RH |
5765 | switch (s->header.type) |
5766 | { | |
5767 | case lang_output_section_statement_enum: | |
5768 | { | |
13075d04 | 5769 | bfd_vma newdot, after, dotdelta; |
d1778b88 | 5770 | lang_output_section_statement_type *os; |
cde9e0be | 5771 | lang_memory_region_type *r; |
c5b0a9ef | 5772 | int section_alignment = 0; |
252b5132 | 5773 | |
d1778b88 | 5774 | os = &s->output_section_statement; |
61826503 | 5775 | init_opb (os->bfd_section); |
8658f989 AM |
5776 | if (os->constraint == -1) |
5777 | break; | |
5778 | ||
fd68d03d AM |
5779 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
5780 | here, in lang_insert_orphan, or in the default linker scripts. | |
5781 | This is covering for coff backend linker bugs. See PR6945. */ | |
5782 | if (os->addr_tree == NULL | |
0e1862bb | 5783 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5784 | && (bfd_get_flavour (link_info.output_bfd) |
5785 | == bfd_target_coff_flavour)) | |
eceda120 | 5786 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5787 | if (os->addr_tree != NULL) |
5788 | { | |
a5df8c84 | 5789 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5790 | |
3bf9618b | 5791 | if (expld.result.valid_p) |
7542af2a AM |
5792 | { |
5793 | dot = expld.result.value; | |
5794 | if (expld.result.section != NULL) | |
5795 | dot += expld.result.section->vma; | |
5796 | } | |
3bf9618b | 5797 | else if (expld.phase != lang_mark_phase_enum) |
df5f2391 | 5798 | einfo (_("%F%P:%pS: non constant or forward reference" |
a5df8c84 | 5799 | " address expression for section %s\n"), |
dab69f68 | 5800 | os->addr_tree, os->name); |
a5df8c84 AM |
5801 | } |
5802 | ||
e9ee469a | 5803 | if (os->bfd_section == NULL) |
75ff4589 | 5804 | /* This section was removed or never actually created. */ |
252b5132 RH |
5805 | break; |
5806 | ||
5807 | /* If this is a COFF shared library section, use the size and | |
5808 | address from the input section. FIXME: This is COFF | |
5809 | specific; it would be cleaner if there were some other way | |
5810 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
5811 | if (((bfd_get_flavour (link_info.output_bfd) |
5812 | == bfd_target_ecoff_flavour) | |
5813 | || (bfd_get_flavour (link_info.output_bfd) | |
5814 | == bfd_target_coff_flavour)) | |
ebe372c1 | 5815 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 5816 | { |
08da4cac | 5817 | asection *input; |
252b5132 RH |
5818 | |
5819 | if (os->children.head == NULL | |
bba1a0c0 | 5820 | || os->children.head->header.next != NULL |
6feb9908 AM |
5821 | || (os->children.head->header.type |
5822 | != lang_input_section_enum)) | |
df5f2391 | 5823 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 5824 | " section %s\n"), os->name); |
252b5132 RH |
5825 | |
5826 | input = os->children.head->input_section.section; | |
fd361982 AM |
5827 | bfd_set_section_vma (os->bfd_section, |
5828 | bfd_section_vma (input)); | |
8772de11 MR |
5829 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5830 | os->bfd_section->size = input->size; | |
252b5132 RH |
5831 | break; |
5832 | } | |
5833 | ||
a5df8c84 | 5834 | newdot = dot; |
13075d04 | 5835 | dotdelta = 0; |
252b5132 RH |
5836 | if (bfd_is_abs_section (os->bfd_section)) |
5837 | { | |
5838 | /* No matter what happens, an abs section starts at zero. */ | |
5839 | ASSERT (os->bfd_section->vma == 0); | |
5840 | } | |
5841 | else | |
5842 | { | |
1579bae1 | 5843 | if (os->addr_tree == NULL) |
252b5132 RH |
5844 | { |
5845 | /* No address specified for this section, get one | |
5846 | from the region specification. */ | |
1579bae1 | 5847 | if (os->region == NULL |
6feb9908 | 5848 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
5849 | && os->region->name_list.name[0] == '*' |
5850 | && strcmp (os->region->name_list.name, | |
6feb9908 | 5851 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
5852 | { |
5853 | os->region = lang_memory_default (os->bfd_section); | |
5854 | } | |
5855 | ||
5856 | /* If a loadable section is using the default memory | |
5857 | region, and some non default memory regions were | |
66184979 | 5858 | defined, issue an error message. */ |
f0636a44 AM |
5859 | if (!os->ignored |
5860 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 5861 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 5862 | && check_regions |
4a93e180 | 5863 | && strcmp (os->region->name_list.name, |
6feb9908 | 5864 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 5865 | && lang_memory_region_list != NULL |
4a93e180 | 5866 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 5867 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a | 5868 | || lang_memory_region_list->next != NULL) |
baf09cba | 5869 | && lang_sizing_iteration == 1) |
66184979 NC |
5870 | { |
5871 | /* By default this is an error rather than just a | |
5872 | warning because if we allocate the section to the | |
5873 | default memory region we can end up creating an | |
07f3b6ad KH |
5874 | excessively large binary, or even seg faulting when |
5875 | attempting to perform a negative seek. See | |
6feb9908 | 5876 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
5877 | for an example of this. This behaviour can be |
5878 | overridden by the using the --no-check-sections | |
5879 | switch. */ | |
5880 | if (command_line.check_section_addresses) | |
df5f2391 | 5881 | einfo (_("%F%P: error: no memory region specified" |
6feb9908 | 5882 | " for loadable section `%s'\n"), |
fd361982 | 5883 | bfd_section_name (os->bfd_section)); |
66184979 | 5884 | else |
6feb9908 AM |
5885 | einfo (_("%P: warning: no memory region specified" |
5886 | " for loadable section `%s'\n"), | |
fd361982 | 5887 | bfd_section_name (os->bfd_section)); |
66184979 | 5888 | } |
252b5132 | 5889 | |
e9ee469a | 5890 | newdot = os->region->current; |
c5b0a9ef | 5891 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 5892 | } |
94b50910 | 5893 | else |
3d9c8f6b AM |
5894 | section_alignment = exp_get_power (os->section_alignment, |
5895 | "section alignment"); | |
5f992e62 | 5896 | |
7270c5ed | 5897 | /* Align to what the section needs. */ |
c5b0a9ef | 5898 | if (section_alignment > 0) |
94b50910 AM |
5899 | { |
5900 | bfd_vma savedot = newdot; | |
baf09cba | 5901 | bfd_vma diff = 0; |
252b5132 | 5902 | |
baf09cba | 5903 | newdot = align_power (newdot, section_alignment); |
13075d04 | 5904 | dotdelta = newdot - savedot; |
baf09cba AM |
5905 | |
5906 | if (lang_sizing_iteration == 1) | |
5907 | diff = dotdelta; | |
5908 | else if (lang_sizing_iteration > 1) | |
92d4b13b AM |
5909 | { |
5910 | /* Only report adjustments that would change | |
5911 | alignment from what we have already reported. */ | |
5912 | diff = newdot - os->bfd_section->vma; | |
5913 | if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) | |
5914 | diff = 0; | |
5915 | } | |
baf09cba | 5916 | if (diff != 0 |
94b50910 | 5917 | && (config.warn_section_align |
baf09cba AM |
5918 | || os->addr_tree != NULL)) |
5919 | einfo (_("%P: warning: " | |
5920 | "start of section %s changed by %ld\n"), | |
5921 | os->name, (long) diff); | |
94b50910 | 5922 | } |
252b5132 | 5923 | |
fd361982 | 5924 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 5925 | |
252b5132 RH |
5926 | os->bfd_section->output_offset = 0; |
5927 | } | |
5928 | ||
e535e147 | 5929 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
5930 | os->fill, newdot, relax, check_regions); |
5931 | ||
f38a2680 | 5932 | os->processed_vma = true; |
e9ee469a AM |
5933 | |
5934 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
5935 | /* Except for some special linker created sections, |
5936 | no output section should change from zero size | |
5937 | after strip_excluded_output_sections. A non-zero | |
5938 | size on an ignored section indicates that some | |
5939 | input section was not sized early enough. */ | |
5940 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 5941 | else |
e9ee469a | 5942 | { |
cde9e0be AM |
5943 | dot = os->bfd_section->vma; |
5944 | ||
5945 | /* Put the section within the requested block size, or | |
5946 | align at the block boundary. */ | |
5947 | after = ((dot | |
5948 | + TO_ADDR (os->bfd_section->size) | |
5949 | + os->block_value - 1) | |
5950 | & - (bfd_vma) os->block_value); | |
5951 | ||
8772de11 MR |
5952 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5953 | os->bfd_section->size = TO_SIZE (after | |
5954 | - os->bfd_section->vma); | |
cde9e0be AM |
5955 | } |
5956 | ||
5957 | /* Set section lma. */ | |
5958 | r = os->region; | |
5959 | if (r == NULL) | |
f38a2680 | 5960 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
cde9e0be AM |
5961 | |
5962 | if (os->load_base) | |
5963 | { | |
5964 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
5965 | os->bfd_section->lma = lma; | |
e9ee469a | 5966 | } |
07dfcf38 | 5967 | else if (os->lma_region != NULL) |
cde9e0be AM |
5968 | { |
5969 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 5970 | |
13075d04 SH |
5971 | if (os->align_lma_with_input) |
5972 | lma += dotdelta; | |
5973 | else | |
5974 | { | |
5975 | /* When LMA_REGION is the same as REGION, align the LMA | |
5976 | as we did for the VMA, possibly including alignment | |
5977 | from the bfd section. If a different region, then | |
5978 | only align according to the value in the output | |
5979 | statement. */ | |
5980 | if (os->lma_region != os->region) | |
3d9c8f6b AM |
5981 | section_alignment = exp_get_power (os->section_alignment, |
5982 | "section alignment"); | |
13075d04 SH |
5983 | if (section_alignment > 0) |
5984 | lma = align_power (lma, section_alignment); | |
5985 | } | |
cde9e0be AM |
5986 | os->bfd_section->lma = lma; |
5987 | } | |
dc0b6aa0 AM |
5988 | else if (r->last_os != NULL |
5989 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
5990 | { |
5991 | bfd_vma lma; | |
5992 | asection *last; | |
5993 | ||
5994 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
5995 | |
5996 | /* A backwards move of dot should be accompanied by | |
5997 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 5998 | os->load_base set) because backwards moves can |
dc0b6aa0 | 5999 | create overlapping LMAs. */ |
264b6205 | 6000 | if (dot < last->vma |
91eb6c46 | 6001 | && os->bfd_section->size != 0 |
ca62bc4a | 6002 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 6003 | { |
dc0b6aa0 AM |
6004 | /* If dot moved backwards then leave lma equal to |
6005 | vma. This is the old default lma, which might | |
6006 | just happen to work when the backwards move is | |
91eb6c46 AM |
6007 | sufficiently large. Nag if this changes anything, |
6008 | so people can fix their linker scripts. */ | |
6009 | ||
6010 | if (last->vma != last->lma) | |
0aa7f586 AM |
6011 | einfo (_("%P: warning: dot moved backwards " |
6012 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
6013 | } |
6014 | else | |
cde9e0be | 6015 | { |
152d792f AM |
6016 | /* If this is an overlay, set the current lma to that |
6017 | at the end of the previous section. */ | |
6018 | if (os->sectype == overlay_section) | |
fc90c280 | 6019 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
6020 | |
6021 | /* Otherwise, keep the same lma to vma relationship | |
6022 | as the previous section. */ | |
6023 | else | |
8610e0fd | 6024 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 6025 | |
c5b0a9ef AM |
6026 | if (section_alignment > 0) |
6027 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
6028 | os->bfd_section->lma = lma; |
6029 | } | |
6030 | } | |
f38a2680 | 6031 | os->processed_lma = true; |
5f992e62 | 6032 | |
cde9e0be AM |
6033 | /* Keep track of normal sections using the default |
6034 | lma region. We use this to set the lma for | |
6035 | following sections. Overlays or other linker | |
6036 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
6037 | default lma == vma is incorrect. |
6038 | To avoid warnings about dot moving backwards when using | |
6039 | -Ttext, don't start tracking sections until we find one | |
14ea2c1b AB |
6040 | of non-zero size or with lma set differently to vma. |
6041 | Do this tracking before we short-cut the loop so that we | |
6042 | track changes for the case where the section size is zero, | |
6043 | but the lma is set differently to the vma. This is | |
6044 | important, if an orphan section is placed after an | |
6045 | otherwise empty output section that has an explicit lma | |
6046 | set, we want that lma reflected in the orphans lma. */ | |
7b243801 AM |
6047 | if (((!IGNORE_SECTION (os->bfd_section) |
6048 | && (os->bfd_section->size != 0 | |
6049 | || (r->last_os == NULL | |
6050 | && os->bfd_section->vma != os->bfd_section->lma) | |
6051 | || (r->last_os != NULL | |
6052 | && dot >= (r->last_os->output_section_statement | |
6053 | .bfd_section->vma)))) | |
6054 | || os->sectype == first_overlay_section) | |
cde9e0be | 6055 | && os->lma_region == NULL |
0e1862bb | 6056 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 6057 | r->last_os = s; |
9f88b410 | 6058 | |
14ea2c1b AB |
6059 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
6060 | break; | |
6061 | ||
e5caec89 | 6062 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 6063 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 6064 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
6065 | dotdelta = TO_ADDR (os->bfd_section->size); |
6066 | else | |
6067 | dotdelta = 0; | |
6068 | dot += dotdelta; | |
e5caec89 | 6069 | |
9f88b410 | 6070 | if (os->update_dot_tree != 0) |
e9ee469a | 6071 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 6072 | |
252b5132 RH |
6073 | /* Update dot in the region ? |
6074 | We only do this if the section is going to be allocated, | |
6075 | since unallocated sections do not contribute to the region's | |
2e76e85a | 6076 | overall size in memory. */ |
1579bae1 | 6077 | if (os->region != NULL |
2e76e85a | 6078 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
6079 | { |
6080 | os->region->current = dot; | |
5f992e62 | 6081 | |
cf888e70 NC |
6082 | if (check_regions) |
6083 | /* Make sure the new address is within the region. */ | |
6084 | os_region_check (os, os->region, os->addr_tree, | |
6085 | os->bfd_section->vma); | |
08da4cac | 6086 | |
a2cab753 | 6087 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
6088 | && ((os->bfd_section->flags & SEC_LOAD) |
6089 | || os->align_lma_with_input)) | |
08da4cac | 6090 | { |
13075d04 | 6091 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 6092 | |
cf888e70 NC |
6093 | if (check_regions) |
6094 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 6095 | os->bfd_section->lma); |
08da4cac | 6096 | } |
252b5132 RH |
6097 | } |
6098 | } | |
6099 | break; | |
6100 | ||
6101 | case lang_constructors_statement_enum: | |
e535e147 | 6102 | dot = lang_size_sections_1 (&constructor_list.head, |
2d20f7bf | 6103 | output_section_statement, |
cf888e70 | 6104 | fill, dot, relax, check_regions); |
252b5132 RH |
6105 | break; |
6106 | ||
6107 | case lang_data_statement_enum: | |
6108 | { | |
6109 | unsigned int size = 0; | |
6110 | ||
7fabd029 | 6111 | s->data_statement.output_offset = |
08da4cac | 6112 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
6113 | s->data_statement.output_section = |
6114 | output_section_statement->bfd_section; | |
6115 | ||
1b493742 NS |
6116 | /* We might refer to provided symbols in the expression, and |
6117 | need to mark them as needed. */ | |
e9ee469a | 6118 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 6119 | |
252b5132 RH |
6120 | switch (s->data_statement.type) |
6121 | { | |
08da4cac KH |
6122 | default: |
6123 | abort (); | |
252b5132 RH |
6124 | case QUAD: |
6125 | case SQUAD: | |
6126 | size = QUAD_SIZE; | |
6127 | break; | |
6128 | case LONG: | |
6129 | size = LONG_SIZE; | |
6130 | break; | |
6131 | case SHORT: | |
6132 | size = SHORT_SIZE; | |
6133 | break; | |
6134 | case BYTE: | |
6135 | size = BYTE_SIZE; | |
6136 | break; | |
6137 | } | |
e5caa5e0 AM |
6138 | if (size < TO_SIZE ((unsigned) 1)) |
6139 | size = TO_SIZE ((unsigned) 1); | |
6140 | dot += TO_ADDR (size); | |
8772de11 MR |
6141 | if (!(output_section_statement->bfd_section->flags |
6142 | & SEC_FIXED_SIZE)) | |
6143 | output_section_statement->bfd_section->size | |
6144 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
aa4c3319 | 6145 | |
252b5132 RH |
6146 | } |
6147 | break; | |
6148 | ||
6149 | case lang_reloc_statement_enum: | |
6150 | { | |
6151 | int size; | |
6152 | ||
7fabd029 | 6153 | s->reloc_statement.output_offset = |
252b5132 RH |
6154 | dot - output_section_statement->bfd_section->vma; |
6155 | s->reloc_statement.output_section = | |
6156 | output_section_statement->bfd_section; | |
6157 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 6158 | dot += TO_ADDR (size); |
8772de11 MR |
6159 | if (!(output_section_statement->bfd_section->flags |
6160 | & SEC_FIXED_SIZE)) | |
6161 | output_section_statement->bfd_section->size | |
6162 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
252b5132 RH |
6163 | } |
6164 | break; | |
5f992e62 | 6165 | |
252b5132 | 6166 | case lang_wild_statement_enum: |
e535e147 | 6167 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
2d20f7bf | 6168 | output_section_statement, |
cf888e70 | 6169 | fill, dot, relax, check_regions); |
252b5132 RH |
6170 | break; |
6171 | ||
6172 | case lang_object_symbols_statement_enum: | |
3b6c1966 AM |
6173 | link_info.create_object_symbols_section |
6174 | = output_section_statement->bfd_section; | |
6175 | output_section_statement->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 6176 | break; |
e9ee469a | 6177 | |
252b5132 RH |
6178 | case lang_output_statement_enum: |
6179 | case lang_target_statement_enum: | |
6180 | break; | |
e9ee469a | 6181 | |
252b5132 RH |
6182 | case lang_input_section_enum: |
6183 | { | |
6184 | asection *i; | |
6185 | ||
e535e147 | 6186 | i = s->input_section.section; |
eea6121a | 6187 | if (relax) |
252b5132 | 6188 | { |
f38a2680 | 6189 | bool again; |
252b5132 | 6190 | |
0aa7f586 | 6191 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
df5f2391 | 6192 | einfo (_("%F%P: can't relax section: %E\n")); |
252b5132 | 6193 | if (again) |
f38a2680 | 6194 | *relax = true; |
252b5132 | 6195 | } |
b3327aad | 6196 | dot = size_input_section (prev, output_section_statement, |
abf874aa | 6197 | fill, &removed, dot); |
252b5132 RH |
6198 | } |
6199 | break; | |
e9ee469a | 6200 | |
252b5132 RH |
6201 | case lang_input_statement_enum: |
6202 | break; | |
e9ee469a | 6203 | |
252b5132 | 6204 | case lang_fill_statement_enum: |
08da4cac KH |
6205 | s->fill_statement.output_section = |
6206 | output_section_statement->bfd_section; | |
252b5132 RH |
6207 | |
6208 | fill = s->fill_statement.fill; | |
6209 | break; | |
e9ee469a | 6210 | |
252b5132 RH |
6211 | case lang_assignment_statement_enum: |
6212 | { | |
6213 | bfd_vma newdot = dot; | |
49c13adb | 6214 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 6215 | |
0f99513f | 6216 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6217 | |
49c13adb | 6218 | exp_fold_tree (tree, |
408082ec | 6219 | output_section_statement->bfd_section, |
252b5132 RH |
6220 | &newdot); |
6221 | ||
2085ee26 | 6222 | ldlang_check_relro_region (s); |
ed1794ee | 6223 | |
0f99513f | 6224 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6225 | |
d2667025 | 6226 | /* This symbol may be relative to this section. */ |
a14a5de3 | 6227 | if ((tree->type.node_class == etree_provided |
49c13adb L |
6228 | || tree->type.node_class == etree_assign) |
6229 | && (tree->assign.dst [0] != '.' | |
6230 | || tree->assign.dst [1] != '\0')) | |
6d8bf25d | 6231 | output_section_statement->update_dot = 1; |
49c13adb | 6232 | |
85852e36 | 6233 | if (!output_section_statement->ignored) |
252b5132 | 6234 | { |
252b5132 RH |
6235 | if (output_section_statement == abs_output_section) |
6236 | { | |
6237 | /* If we don't have an output section, then just adjust | |
6238 | the default memory address. */ | |
6feb9908 | 6239 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
f38a2680 | 6240 | false)->current = newdot; |
252b5132 | 6241 | } |
85852e36 | 6242 | else if (newdot != dot) |
252b5132 | 6243 | { |
b3327aad AM |
6244 | /* Insert a pad after this statement. We can't |
6245 | put the pad before when relaxing, in case the | |
6246 | assignment references dot. */ | |
e5caa5e0 | 6247 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
6248 | output_section_statement->bfd_section, dot); |
6249 | ||
6250 | /* Don't neuter the pad below when relaxing. */ | |
6251 | s = s->header.next; | |
9dfc8ab2 | 6252 | |
e9ee469a AM |
6253 | /* If dot is advanced, this implies that the section |
6254 | should have space allocated to it, unless the | |
6255 | user has explicitly stated that the section | |
2e76e85a | 6256 | should not be allocated. */ |
f4eaaf7f AM |
6257 | if (output_section_statement->sectype != noalloc_section |
6258 | && (output_section_statement->sectype != noload_section | |
6259 | || (bfd_get_flavour (link_info.output_bfd) | |
6260 | == bfd_target_elf_flavour))) | |
e9ee469a AM |
6261 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
6262 | } | |
252b5132 RH |
6263 | dot = newdot; |
6264 | } | |
6265 | } | |
6266 | break; | |
6267 | ||
6268 | case lang_padding_statement_enum: | |
c0c330a7 AM |
6269 | /* If this is the first time lang_size_sections is called, |
6270 | we won't have any padding statements. If this is the | |
6271 | second or later passes when relaxing, we should allow | |
6272 | padding to shrink. If padding is needed on this pass, it | |
6273 | will be added back in. */ | |
6274 | s->padding_statement.size = 0; | |
6e814ff8 AM |
6275 | |
6276 | /* Make sure output_offset is valid. If relaxation shrinks | |
6277 | the section and this pad isn't needed, it's possible to | |
6278 | have output_offset larger than the final size of the | |
6279 | section. bfd_set_section_contents will complain even for | |
6280 | a pad size of zero. */ | |
6281 | s->padding_statement.output_offset | |
6282 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
6283 | break; |
6284 | ||
6285 | case lang_group_statement_enum: | |
e535e147 | 6286 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
2d20f7bf | 6287 | output_section_statement, |
cf888e70 | 6288 | fill, dot, relax, check_regions); |
252b5132 RH |
6289 | break; |
6290 | ||
53d25da6 | 6291 | case lang_insert_statement_enum: |
252b5132 RH |
6292 | break; |
6293 | ||
c0c330a7 | 6294 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
6295 | case lang_address_statement_enum: |
6296 | break; | |
53d25da6 AM |
6297 | |
6298 | default: | |
6299 | FAIL (); | |
6300 | break; | |
252b5132 | 6301 | } |
abf874aa CL |
6302 | |
6303 | /* If an input section doesn't fit in the current output | |
6304 | section, remove it from the list. Handle the case where we | |
6305 | have to remove an input_section statement here: there is a | |
6306 | special case to remove the first element of the list. */ | |
6307 | if (link_info.non_contiguous_regions && removed) | |
6308 | { | |
6309 | /* If we removed the first element during the previous | |
6310 | iteration, override the loop assignment of prev_s. */ | |
6311 | if (removed_prev_s) | |
6312 | prev_s = NULL; | |
6313 | ||
6314 | if (prev_s) | |
6315 | { | |
6316 | /* If there was a real previous input section, just skip | |
6317 | the current one. */ | |
6318 | prev_s->header.next=s->header.next; | |
6319 | s = prev_s; | |
f38a2680 | 6320 | removed_prev_s = false; |
abf874aa CL |
6321 | } |
6322 | else | |
6323 | { | |
6324 | /* Remove the first input section of the list. */ | |
6325 | *prev = s->header.next; | |
f38a2680 | 6326 | removed_prev_s = true; |
abf874aa CL |
6327 | } |
6328 | ||
6329 | /* Move to next element, unless we removed the head of the | |
6330 | list. */ | |
6331 | if (!removed_prev_s) | |
6332 | prev = &s->header.next; | |
6333 | } | |
6334 | else | |
6335 | { | |
6336 | prev = &s->header.next; | |
f38a2680 | 6337 | removed_prev_s = false; |
abf874aa | 6338 | } |
252b5132 RH |
6339 | } |
6340 | return dot; | |
6341 | } | |
6342 | ||
591a748a NC |
6343 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
6344 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
6345 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
6346 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b | 6347 | |
f38a2680 | 6348 | bool |
0aa7f586 AM |
6349 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
6350 | bfd *abfd ATTRIBUTE_UNUSED, | |
6351 | asection *current_section, | |
6352 | asection *previous_section, | |
f38a2680 | 6353 | bool new_segment) |
2889e75b | 6354 | { |
0aa7f586 AM |
6355 | lang_output_section_statement_type *cur; |
6356 | lang_output_section_statement_type *prev; | |
591a748a NC |
6357 | |
6358 | /* The checks below are only necessary when the BFD library has decided | |
6359 | that the two sections ought to be placed into the same segment. */ | |
2889e75b | 6360 | if (new_segment) |
f38a2680 | 6361 | return true; |
2889e75b NC |
6362 | |
6363 | /* Paranoia checks. */ | |
6364 | if (current_section == NULL || previous_section == NULL) | |
6365 | return new_segment; | |
6366 | ||
4724d37e RM |
6367 | /* If this flag is set, the target never wants code and non-code |
6368 | sections comingled in the same segment. */ | |
6369 | if (config.separate_code | |
6370 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
f38a2680 | 6371 | return true; |
4724d37e | 6372 | |
2889e75b NC |
6373 | /* Find the memory regions associated with the two sections. |
6374 | We call lang_output_section_find() here rather than scanning the list | |
6375 | of output sections looking for a matching section pointer because if | |
591a748a | 6376 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6377 | cur = lang_output_section_find (current_section->name); |
6378 | prev = lang_output_section_find (previous_section->name); | |
6379 | ||
591a748a | 6380 | /* More paranoia. */ |
2889e75b NC |
6381 | if (cur == NULL || prev == NULL) |
6382 | return new_segment; | |
6383 | ||
6384 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6385 | different segments. See the email thread starting at the following |
6386 | URL for the reasons why this is necessary: | |
2889e75b NC |
6387 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6388 | return cur->region != prev->region; | |
6389 | } | |
6390 | ||
e9ee469a | 6391 | void |
f38a2680 | 6392 | one_lang_size_sections_pass (bool *relax, bool check_regions) |
2d20f7bf | 6393 | { |
420e579c | 6394 | lang_statement_iteration++; |
baf09cba AM |
6395 | if (expld.phase != lang_mark_phase_enum) |
6396 | lang_sizing_iteration++; | |
e535e147 AM |
6397 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6398 | 0, 0, relax, check_regions); | |
e9ee469a | 6399 | } |
420e579c | 6400 | |
f38a2680 | 6401 | static bool |
2085ee26 | 6402 | lang_size_segment (void) |
e9ee469a | 6403 | { |
cba6246d L |
6404 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6405 | a page could be saved in the data segment. */ | |
2085ee26 | 6406 | seg_align_type *seg = &expld.dataseg; |
cba6246d L |
6407 | bfd_vma first, last; |
6408 | ||
9833b775 AM |
6409 | first = -seg->base & (seg->commonpagesize - 1); |
6410 | last = seg->end & (seg->commonpagesize - 1); | |
cba6246d | 6411 | if (first && last |
9833b775 AM |
6412 | && ((seg->base & ~(seg->commonpagesize - 1)) |
6413 | != (seg->end & ~(seg->commonpagesize - 1))) | |
6414 | && first + last <= seg->commonpagesize) | |
8c37241b | 6415 | { |
cba6246d | 6416 | seg->phase = exp_seg_adjust; |
f38a2680 | 6417 | return true; |
cba6246d L |
6418 | } |
6419 | ||
6420 | seg->phase = exp_seg_done; | |
f38a2680 | 6421 | return false; |
cba6246d L |
6422 | } |
6423 | ||
6424 | static bfd_vma | |
2085ee26 | 6425 | lang_size_relro_segment_1 (void) |
cba6246d | 6426 | { |
2085ee26 | 6427 | seg_align_type *seg = &expld.dataseg; |
5197c88e | 6428 | bfd_vma relro_end, desired_end; |
6a9d0866 | 6429 | asection *sec; |
8c37241b | 6430 | |
cba6246d | 6431 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
9833b775 | 6432 | relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; |
0e5fabeb | 6433 | |
cba6246d | 6434 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
5197c88e | 6435 | desired_end = relro_end - seg->relro_offset; |
0e5fabeb | 6436 | |
5197c88e | 6437 | /* For sections in the relro segment.. */ |
cba6246d | 6438 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) |
6a9d0866 AM |
6439 | if ((sec->flags & SEC_ALLOC) != 0 |
6440 | && sec->vma >= seg->base | |
6441 | && sec->vma < seg->relro_end - seg->relro_offset) | |
cba6246d | 6442 | { |
6a9d0866 AM |
6443 | /* Where do we want to put this section so that it ends as |
6444 | desired? */ | |
6445 | bfd_vma start, end, bump; | |
6446 | ||
6447 | end = start = sec->vma; | |
6448 | if (!IS_TBSS (sec)) | |
6449 | end += TO_ADDR (sec->size); | |
6450 | bump = desired_end - end; | |
6451 | /* We'd like to increase START by BUMP, but we must heed | |
6452 | alignment so the increase might be less than optimum. */ | |
6453 | start += bump; | |
6454 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6455 | /* This is now the desired end for the previous section. */ | |
6456 | desired_end = start; | |
5197c88e AM |
6457 | } |
6458 | ||
6459 | seg->phase = exp_seg_relro_adjust; | |
6460 | ASSERT (desired_end >= seg->base); | |
5197c88e | 6461 | seg->base = desired_end; |
cba6246d L |
6462 | return relro_end; |
6463 | } | |
6464 | ||
f38a2680 AM |
6465 | static bool |
6466 | lang_size_relro_segment (bool *relax, bool check_regions) | |
cba6246d | 6467 | { |
f38a2680 | 6468 | bool do_reset = false; |
cba6246d L |
6469 | |
6470 | if (link_info.relro && expld.dataseg.relro_end) | |
6471 | { | |
e29de63f | 6472 | bfd_vma data_initial_base = expld.dataseg.base; |
2085ee26 | 6473 | bfd_vma data_relro_end = lang_size_relro_segment_1 (); |
0e5fabeb | 6474 | |
f86a8756 | 6475 | lang_reset_memory_regions (); |
e9ee469a | 6476 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6477 | |
cba6246d L |
6478 | /* Assignments to dot, or to output section address in a user |
6479 | script have increased padding over the original. Revert. */ | |
e29de63f | 6480 | if (expld.dataseg.relro_end > data_relro_end) |
a4f5ad88 | 6481 | { |
2085ee26 | 6482 | expld.dataseg.base = data_initial_base; |
f38a2680 | 6483 | do_reset = true; |
a4f5ad88 | 6484 | } |
8c37241b | 6485 | } |
2085ee26 | 6486 | else if (lang_size_segment ()) |
f38a2680 | 6487 | do_reset = true; |
cba6246d L |
6488 | |
6489 | return do_reset; | |
6490 | } | |
6491 | ||
6492 | void | |
f38a2680 | 6493 | lang_size_sections (bool *relax, bool check_regions) |
cba6246d L |
6494 | { |
6495 | expld.phase = lang_allocating_phase_enum; | |
6496 | expld.dataseg.phase = exp_seg_none; | |
6497 | ||
6498 | one_lang_size_sections_pass (relax, check_regions); | |
6499 | ||
6500 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6501 | expld.dataseg.phase = exp_seg_done; | |
6502 | ||
6503 | if (expld.dataseg.phase == exp_seg_end_seen) | |
2d20f7bf | 6504 | { |
f38a2680 | 6505 | bool do_reset |
cba6246d L |
6506 | = lang_size_relro_segment (relax, check_regions); |
6507 | ||
6508 | if (do_reset) | |
2d20f7bf | 6509 | { |
f86a8756 | 6510 | lang_reset_memory_regions (); |
e9ee469a | 6511 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6512 | } |
cba6246d L |
6513 | |
6514 | if (link_info.relro && expld.dataseg.relro_end) | |
6515 | { | |
6516 | link_info.relro_start = expld.dataseg.base; | |
6517 | link_info.relro_end = expld.dataseg.relro_end; | |
6518 | } | |
2d20f7bf | 6519 | } |
2d20f7bf JJ |
6520 | } |
6521 | ||
d2667025 AM |
6522 | static lang_output_section_statement_type *current_section; |
6523 | static lang_assignment_statement_type *current_assign; | |
f38a2680 | 6524 | static bool prefer_next_section; |
d2667025 | 6525 | |
420e579c HPN |
6526 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6527 | ||
6528 | static bfd_vma | |
66e28d60 AM |
6529 | lang_do_assignments_1 (lang_statement_union_type *s, |
6530 | lang_output_section_statement_type *current_os, | |
6531 | fill_type *fill, | |
d2667025 | 6532 | bfd_vma dot, |
f38a2680 | 6533 | bool *found_end) |
252b5132 | 6534 | { |
1579bae1 | 6535 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6536 | { |
6537 | switch (s->header.type) | |
6538 | { | |
6539 | case lang_constructors_statement_enum: | |
420e579c | 6540 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6541 | current_os, fill, dot, found_end); |
252b5132 RH |
6542 | break; |
6543 | ||
6544 | case lang_output_section_statement_enum: | |
6545 | { | |
d1778b88 | 6546 | lang_output_section_statement_type *os; |
f02cb058 | 6547 | bfd_vma newdot; |
252b5132 | 6548 | |
d1778b88 | 6549 | os = &(s->output_section_statement); |
d2667025 | 6550 | os->after_end = *found_end; |
61826503 | 6551 | init_opb (os->bfd_section); |
1cba418d AM |
6552 | newdot = dot; |
6553 | if (os->bfd_section != NULL) | |
252b5132 | 6554 | { |
1cba418d | 6555 | if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) |
d2667025 AM |
6556 | { |
6557 | current_section = os; | |
f38a2680 | 6558 | prefer_next_section = false; |
d2667025 | 6559 | } |
1cba418d | 6560 | newdot = os->bfd_section->vma; |
f02cb058 AM |
6561 | } |
6562 | newdot = lang_do_assignments_1 (os->children.head, | |
1cba418d | 6563 | os, os->fill, newdot, found_end); |
f02cb058 AM |
6564 | if (!os->ignored) |
6565 | { | |
6566 | if (os->bfd_section != NULL) | |
6567 | { | |
1cba418d AM |
6568 | newdot = os->bfd_section->vma; |
6569 | ||
f02cb058 AM |
6570 | /* .tbss sections effectively have zero size. */ |
6571 | if (!IS_TBSS (os->bfd_section) | |
6572 | || bfd_link_relocatable (&link_info)) | |
1cba418d | 6573 | newdot += TO_ADDR (os->bfd_section->size); |
f02cb058 AM |
6574 | |
6575 | if (os->update_dot_tree != NULL) | |
6576 | exp_fold_tree (os->update_dot_tree, | |
1cba418d | 6577 | bfd_abs_section_ptr, &newdot); |
f02cb058 | 6578 | } |
1cba418d | 6579 | dot = newdot; |
252b5132 RH |
6580 | } |
6581 | } | |
6582 | break; | |
e9ee469a | 6583 | |
252b5132 RH |
6584 | case lang_wild_statement_enum: |
6585 | ||
420e579c | 6586 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6587 | current_os, fill, dot, found_end); |
252b5132 RH |
6588 | break; |
6589 | ||
6590 | case lang_object_symbols_statement_enum: | |
6591 | case lang_output_statement_enum: | |
6592 | case lang_target_statement_enum: | |
252b5132 | 6593 | break; |
e9ee469a | 6594 | |
252b5132 | 6595 | case lang_data_statement_enum: |
e9ee469a AM |
6596 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
6597 | if (expld.result.valid_p) | |
7542af2a AM |
6598 | { |
6599 | s->data_statement.value = expld.result.value; | |
6600 | if (expld.result.section != NULL) | |
6601 | s->data_statement.value += expld.result.section->vma; | |
6602 | } | |
f02cb058 | 6603 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6604 | einfo (_("%F%P: invalid data statement\n")); |
b7a26f91 KH |
6605 | { |
6606 | unsigned int size; | |
08da4cac KH |
6607 | switch (s->data_statement.type) |
6608 | { | |
6609 | default: | |
6610 | abort (); | |
6611 | case QUAD: | |
6612 | case SQUAD: | |
6613 | size = QUAD_SIZE; | |
6614 | break; | |
6615 | case LONG: | |
6616 | size = LONG_SIZE; | |
6617 | break; | |
6618 | case SHORT: | |
6619 | size = SHORT_SIZE; | |
6620 | break; | |
6621 | case BYTE: | |
6622 | size = BYTE_SIZE; | |
6623 | break; | |
6624 | } | |
e5caa5e0 AM |
6625 | if (size < TO_SIZE ((unsigned) 1)) |
6626 | size = TO_SIZE ((unsigned) 1); | |
6627 | dot += TO_ADDR (size); | |
08da4cac | 6628 | } |
252b5132 RH |
6629 | break; |
6630 | ||
6631 | case lang_reloc_statement_enum: | |
e9ee469a AM |
6632 | exp_fold_tree (s->reloc_statement.addend_exp, |
6633 | bfd_abs_section_ptr, &dot); | |
6634 | if (expld.result.valid_p) | |
6635 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6636 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6637 | einfo (_("%F%P: invalid reloc statement\n")); |
e5caa5e0 | 6638 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6639 | break; |
6640 | ||
6641 | case lang_input_section_enum: | |
6642 | { | |
6643 | asection *in = s->input_section.section; | |
6644 | ||
57ceae94 | 6645 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6646 | dot += TO_ADDR (in->size); |
252b5132 RH |
6647 | } |
6648 | break; | |
6649 | ||
6650 | case lang_input_statement_enum: | |
6651 | break; | |
e9ee469a | 6652 | |
252b5132 RH |
6653 | case lang_fill_statement_enum: |
6654 | fill = s->fill_statement.fill; | |
6655 | break; | |
252b5132 | 6656 | |
e9ee469a | 6657 | case lang_assignment_statement_enum: |
d2667025 AM |
6658 | current_assign = &s->assignment_statement; |
6659 | if (current_assign->exp->type.node_class != etree_assert) | |
6660 | { | |
6661 | const char *p = current_assign->exp->assign.dst; | |
6662 | ||
6663 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
f38a2680 | 6664 | prefer_next_section = true; |
d2667025 AM |
6665 | |
6666 | while (*p == '_') | |
6667 | ++p; | |
6668 | if (strcmp (p, "end") == 0) | |
f38a2680 | 6669 | *found_end = true; |
d2667025 | 6670 | } |
e9ee469a | 6671 | exp_fold_tree (s->assignment_statement.exp, |
f02cb058 AM |
6672 | (current_os->bfd_section != NULL |
6673 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6674 | &dot); |
252b5132 | 6675 | break; |
e9ee469a | 6676 | |
252b5132 | 6677 | case lang_padding_statement_enum: |
e5caa5e0 | 6678 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6679 | break; |
6680 | ||
6681 | case lang_group_statement_enum: | |
420e579c | 6682 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6683 | current_os, fill, dot, found_end); |
252b5132 RH |
6684 | break; |
6685 | ||
53d25da6 | 6686 | case lang_insert_statement_enum: |
252b5132 | 6687 | break; |
e9ee469a | 6688 | |
252b5132 RH |
6689 | case lang_address_statement_enum: |
6690 | break; | |
53d25da6 AM |
6691 | |
6692 | default: | |
6693 | FAIL (); | |
6694 | break; | |
252b5132 | 6695 | } |
252b5132 RH |
6696 | } |
6697 | return dot; | |
6698 | } | |
6699 | ||
f2241121 | 6700 | void |
2f65ac72 | 6701 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6702 | { |
f38a2680 | 6703 | bool found_end = false; |
d2667025 AM |
6704 | |
6705 | current_section = NULL; | |
f38a2680 | 6706 | prefer_next_section = false; |
2f65ac72 | 6707 | expld.phase = phase; |
420e579c | 6708 | lang_statement_iteration++; |
d2667025 AM |
6709 | lang_do_assignments_1 (statement_list.head, |
6710 | abs_output_section, NULL, 0, &found_end); | |
6711 | } | |
6712 | ||
6713 | /* For an assignment statement outside of an output section statement, | |
6714 | choose the best of neighbouring output sections to use for values | |
6715 | of "dot". */ | |
6716 | ||
6717 | asection * | |
6718 | section_for_dot (void) | |
6719 | { | |
6720 | asection *s; | |
6721 | ||
6722 | /* Assignments belong to the previous output section, unless there | |
6723 | has been an assignment to "dot", in which case following | |
6724 | assignments belong to the next output section. (The assumption | |
6725 | is that an assignment to "dot" is setting up the address for the | |
6726 | next output section.) Except that past the assignment to "_end" | |
6727 | we always associate with the previous section. This exception is | |
6728 | for targets like SH that define an alloc .stack or other | |
6729 | weirdness after non-alloc sections. */ | |
6730 | if (current_section == NULL || prefer_next_section) | |
6731 | { | |
6732 | lang_statement_union_type *stmt; | |
6733 | lang_output_section_statement_type *os; | |
6734 | ||
6735 | for (stmt = (lang_statement_union_type *) current_assign; | |
6736 | stmt != NULL; | |
6737 | stmt = stmt->header.next) | |
6738 | if (stmt->header.type == lang_output_section_statement_enum) | |
6739 | break; | |
6740 | ||
10c00056 | 6741 | os = stmt ? &stmt->output_section_statement : NULL; |
d2667025 AM |
6742 | while (os != NULL |
6743 | && !os->after_end | |
6744 | && (os->bfd_section == NULL | |
6745 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6746 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6747 | os->bfd_section))) | |
6748 | os = os->next; | |
6749 | ||
6750 | if (current_section == NULL || os == NULL || !os->after_end) | |
6751 | { | |
6752 | if (os != NULL) | |
6753 | s = os->bfd_section; | |
6754 | else | |
6755 | s = link_info.output_bfd->section_last; | |
6756 | while (s != NULL | |
6757 | && ((s->flags & SEC_ALLOC) == 0 | |
6758 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6759 | s = s->prev; | |
6760 | if (s != NULL) | |
6761 | return s; | |
6762 | ||
6763 | return bfd_abs_section_ptr; | |
6764 | } | |
6765 | } | |
6766 | ||
6767 | s = current_section->bfd_section; | |
6768 | ||
6769 | /* The section may have been stripped. */ | |
6770 | while (s != NULL | |
6771 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6772 | || (s->flags & SEC_ALLOC) == 0 | |
6773 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6774 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6775 | s = s->prev; | |
6776 | if (s == NULL) | |
6777 | s = link_info.output_bfd->sections; | |
6778 | while (s != NULL | |
6779 | && ((s->flags & SEC_ALLOC) == 0 | |
6780 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6781 | s = s->next; | |
6782 | if (s != NULL) | |
6783 | return s; | |
6784 | ||
6785 | return bfd_abs_section_ptr; | |
420e579c HPN |
6786 | } |
6787 | ||
7dba9362 AM |
6788 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ |
6789 | ||
6790 | static struct bfd_link_hash_entry **start_stop_syms; | |
6791 | static size_t start_stop_count = 0; | |
6792 | static size_t start_stop_alloc = 0; | |
6793 | ||
6794 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6795 | to start_stop_syms. */ | |
6796 | ||
6797 | static void | |
6798 | lang_define_start_stop (const char *symbol, asection *sec) | |
6799 | { | |
6800 | struct bfd_link_hash_entry *h; | |
6801 | ||
6802 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6803 | if (h != NULL) | |
6804 | { | |
6805 | if (start_stop_count == start_stop_alloc) | |
6806 | { | |
6807 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6808 | start_stop_syms | |
6809 | = xrealloc (start_stop_syms, | |
6810 | start_stop_alloc * sizeof (*start_stop_syms)); | |
6811 | } | |
6812 | start_stop_syms[start_stop_count++] = h; | |
6813 | } | |
6814 | } | |
6815 | ||
6816 | /* Check for input sections whose names match references to | |
6817 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
6818 | preliminary definitions. */ | |
252b5132 RH |
6819 | |
6820 | static void | |
7dba9362 | 6821 | lang_init_start_stop (void) |
252b5132 | 6822 | { |
7dba9362 | 6823 | bfd *abfd; |
252b5132 | 6824 | asection *s; |
7dba9362 AM |
6825 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); |
6826 | ||
6827 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
6828 | for (s = abfd->sections; s != NULL; s = s->next) | |
6829 | { | |
6830 | const char *ps; | |
6831 | const char *secname = s->name; | |
6832 | ||
6833 | for (ps = secname; *ps != '\0'; ps++) | |
6834 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
6835 | break; | |
6836 | if (*ps == '\0') | |
6837 | { | |
6838 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
b27685f2 | 6839 | |
7dba9362 AM |
6840 | symbol[0] = leading_char; |
6841 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
6842 | lang_define_start_stop (symbol, s); | |
6843 | ||
6844 | symbol[1] = leading_char; | |
6845 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
6846 | lang_define_start_stop (symbol + 1, s); | |
6847 | ||
6848 | free (symbol); | |
6849 | } | |
6850 | } | |
6851 | } | |
6852 | ||
6853 | /* Iterate over start_stop_syms. */ | |
6854 | ||
6855 | static void | |
6856 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
6857 | { | |
6858 | size_t i; | |
6859 | ||
6860 | for (i = 0; i < start_stop_count; ++i) | |
6861 | func (start_stop_syms[i]); | |
6862 | } | |
6863 | ||
6864 | /* __start and __stop symbols are only supposed to be defined by the | |
6865 | linker for orphan sections, but we now extend that to sections that | |
6866 | map to an output section of the same name. The symbols were | |
6867 | defined early for --gc-sections, before we mapped input to output | |
6868 | sections, so undo those that don't satisfy this rule. */ | |
6869 | ||
6870 | static void | |
6871 | undef_start_stop (struct bfd_link_hash_entry *h) | |
6872 | { | |
6873 | if (h->ldscript_def) | |
b27685f2 L |
6874 | return; |
6875 | ||
7dba9362 AM |
6876 | if (h->u.def.section->output_section == NULL |
6877 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
6878 | || strcmp (h->u.def.section->name, | |
6879 | h->u.def.section->output_section->name) != 0) | |
6880 | { | |
4d1c6335 L |
6881 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, |
6882 | h->u.def.section->name); | |
6883 | if (sec != NULL) | |
6884 | { | |
6885 | /* When there are more than one input sections with the same | |
6886 | section name, SECNAME, linker picks the first one to define | |
6887 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
6888 | input section is removed by comdat group, we need to check | |
6889 | if there is still an output section with section name | |
6890 | SECNAME. */ | |
6891 | asection *i; | |
6892 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
6893 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
6894 | { | |
6895 | h->u.def.section = i; | |
6896 | return; | |
6897 | } | |
6898 | } | |
7dba9362 AM |
6899 | h->type = bfd_link_hash_undefined; |
6900 | h->u.undef.abfd = NULL; | |
fc304b88 | 6901 | if (is_elf_hash_table (link_info.hash)) |
7824c1d2 AM |
6902 | { |
6903 | const struct elf_backend_data *bed; | |
6904 | struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h; | |
6905 | unsigned int was_forced = eh->forced_local; | |
6906 | ||
6907 | bed = get_elf_backend_data (link_info.output_bfd); | |
f38a2680 | 6908 | (*bed->elf_backend_hide_symbol) (&link_info, eh, true); |
7824c1d2 AM |
6909 | if (!eh->ref_regular_nonweak) |
6910 | h->type = bfd_link_hash_undefweak; | |
6911 | eh->def_regular = 0; | |
6912 | eh->forced_local = was_forced; | |
6913 | } | |
7dba9362 AM |
6914 | } |
6915 | } | |
252b5132 | 6916 | |
7dba9362 AM |
6917 | static void |
6918 | lang_undef_start_stop (void) | |
6919 | { | |
6920 | foreach_start_stop (undef_start_stop); | |
6921 | } | |
6922 | ||
6923 | /* Check for output sections whose names match references to | |
6924 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
6925 | preliminary definitions. */ | |
6926 | ||
6927 | static void | |
6928 | lang_init_startof_sizeof (void) | |
6929 | { | |
6930 | asection *s; | |
252b5132 | 6931 | |
f13a99db | 6932 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6933 | { |
7dba9362 AM |
6934 | const char *secname = s->name; |
6935 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 6936 | |
7dba9362 AM |
6937 | sprintf (symbol, ".startof.%s", secname); |
6938 | lang_define_start_stop (symbol, s); | |
252b5132 | 6939 | |
7dba9362 AM |
6940 | memcpy (symbol + 1, ".size", 5); |
6941 | lang_define_start_stop (symbol + 1, s); | |
6942 | free (symbol); | |
6943 | } | |
6944 | } | |
cbd0eecf | 6945 | |
7dba9362 AM |
6946 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ |
6947 | ||
6948 | static void | |
6949 | set_start_stop (struct bfd_link_hash_entry *h) | |
6950 | { | |
6951 | if (h->ldscript_def | |
6952 | || h->type != bfd_link_hash_defined) | |
6953 | return; | |
cbd0eecf | 6954 | |
7dba9362 AM |
6955 | if (h->root.string[0] == '.') |
6956 | { | |
6957 | /* .startof. or .sizeof. symbol. | |
6958 | .startof. already has final value. */ | |
6959 | if (h->root.string[2] == 'i') | |
252b5132 | 6960 | { |
7dba9362 AM |
6961 | /* .sizeof. */ |
6962 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
6963 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 6964 | } |
7dba9362 AM |
6965 | } |
6966 | else | |
6967 | { | |
6968 | /* __start or __stop symbol. */ | |
6969 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 6970 | |
7dba9362 AM |
6971 | h->u.def.section = h->u.def.section->output_section; |
6972 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 6973 | { |
7dba9362 AM |
6974 | /* __stop_ */ |
6975 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 6976 | } |
252b5132 RH |
6977 | } |
6978 | } | |
6979 | ||
7dba9362 AM |
6980 | static void |
6981 | lang_finalize_start_stop (void) | |
6982 | { | |
6983 | foreach_start_stop (set_start_stop); | |
6984 | } | |
6985 | ||
97da0e26 AM |
6986 | static void |
6987 | lang_symbol_tweaks (void) | |
6988 | { | |
6989 | /* Give initial values for __start and __stop symbols, so that ELF | |
6990 | gc_sections will keep sections referenced by these symbols. Must | |
6991 | be done before lang_do_assignments. */ | |
6992 | if (config.build_constructors) | |
6993 | lang_init_start_stop (); | |
6994 | ||
6995 | /* Make __ehdr_start hidden, and set def_regular even though it is | |
6996 | likely undefined at this stage. For lang_check_relocs. */ | |
6997 | if (is_elf_hash_table (link_info.hash) | |
6998 | && !bfd_link_relocatable (&link_info)) | |
6999 | { | |
7000 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
7001 | bfd_link_hash_lookup (link_info.hash, "__ehdr_start", | |
7002 | false, false, true); | |
7003 | ||
7004 | /* Only adjust the export class if the symbol was referenced | |
7005 | and not defined, otherwise leave it alone. */ | |
7006 | if (h != NULL | |
7007 | && (h->root.type == bfd_link_hash_new | |
7008 | || h->root.type == bfd_link_hash_undefined | |
7009 | || h->root.type == bfd_link_hash_undefweak | |
7010 | || h->root.type == bfd_link_hash_common)) | |
7011 | { | |
7012 | const struct elf_backend_data *bed; | |
7013 | bed = get_elf_backend_data (link_info.output_bfd); | |
7014 | (*bed->elf_backend_hide_symbol) (&link_info, h, true); | |
7015 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) | |
7016 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
7017 | h->def_regular = 1; | |
7018 | h->root.linker_def = 1; | |
5617fae7 | 7019 | h->root.rel_from_abs = 1; |
97da0e26 AM |
7020 | } |
7021 | } | |
7022 | } | |
7023 | ||
252b5132 | 7024 | static void |
750877ba | 7025 | lang_end (void) |
252b5132 RH |
7026 | { |
7027 | struct bfd_link_hash_entry *h; | |
f38a2680 | 7028 | bool warn; |
252b5132 | 7029 | |
0e1862bb L |
7030 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
7031 | || bfd_link_dll (&link_info)) | |
71934f94 | 7032 | warn = entry_from_cmdline; |
252b5132 | 7033 | else |
f38a2680 | 7034 | warn = true; |
252b5132 | 7035 | |
ac69cbc6 | 7036 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
7037 | --gc-sections, unless --gc-keep-exported was also given. */ |
7038 | if (bfd_link_relocatable (&link_info) | |
7039 | && link_info.gc_sections | |
2f5541f3 AM |
7040 | && !link_info.gc_keep_exported) |
7041 | { | |
7042 | struct bfd_sym_chain *sym; | |
7043 | ||
7044 | for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next) | |
7045 | { | |
7046 | h = bfd_link_hash_lookup (link_info.hash, sym->name, | |
f38a2680 | 7047 | false, false, false); |
2f5541f3 AM |
7048 | if (h != NULL |
7049 | && (h->type == bfd_link_hash_defined | |
7050 | || h->type == bfd_link_hash_defweak) | |
7051 | && !bfd_is_const_section (h->u.def.section)) | |
7052 | break; | |
7053 | } | |
7054 | if (!sym) | |
7055 | einfo (_("%F%P: --gc-sections requires a defined symbol root " | |
7056 | "specified by -e or -u\n")); | |
7057 | } | |
ac69cbc6 | 7058 | |
1579bae1 | 7059 | if (entry_symbol.name == NULL) |
252b5132 | 7060 | { |
a359509e ZW |
7061 | /* No entry has been specified. Look for the default entry, but |
7062 | don't warn if we don't find it. */ | |
7063 | entry_symbol.name = entry_symbol_default; | |
f38a2680 | 7064 | warn = false; |
252b5132 RH |
7065 | } |
7066 | ||
e3e942e9 | 7067 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
f38a2680 | 7068 | false, false, true); |
1579bae1 | 7069 | if (h != NULL |
252b5132 RH |
7070 | && (h->type == bfd_link_hash_defined |
7071 | || h->type == bfd_link_hash_defweak) | |
7072 | && h->u.def.section->output_section != NULL) | |
7073 | { | |
7074 | bfd_vma val; | |
7075 | ||
7076 | val = (h->u.def.value | |
fd361982 | 7077 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 7078 | + h->u.def.section->output_offset); |
0aa7f586 | 7079 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 7080 | einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
7081 | } |
7082 | else | |
7083 | { | |
7084 | bfd_vma val; | |
5f992e62 | 7085 | const char *send; |
252b5132 RH |
7086 | |
7087 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 7088 | number. */ |
e3e942e9 | 7089 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
7090 | if (*send == '\0') |
7091 | { | |
0aa7f586 | 7092 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 7093 | einfo (_("%F%P: can't set start address\n")); |
252b5132 | 7094 | } |
5226a6a8 NC |
7095 | /* BZ 2004952: Only use the start of the entry section for executables. */ |
7096 | else if bfd_link_executable (&link_info) | |
252b5132 RH |
7097 | { |
7098 | asection *ts; | |
7099 | ||
7100 | /* Can't find the entry symbol, and it's not a number. Use | |
7101 | the first address in the text section. */ | |
f13a99db | 7102 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 7103 | if (ts != NULL) |
252b5132 RH |
7104 | { |
7105 | if (warn) | |
6feb9908 AM |
7106 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7107 | " defaulting to %V\n"), | |
e3e942e9 | 7108 | entry_symbol.name, |
fd361982 AM |
7109 | bfd_section_vma (ts)); |
7110 | if (!bfd_set_start_address (link_info.output_bfd, | |
7111 | bfd_section_vma (ts))) | |
df5f2391 | 7112 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
7113 | } |
7114 | else | |
7115 | { | |
7116 | if (warn) | |
6feb9908 AM |
7117 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7118 | " not setting start address\n"), | |
e3e942e9 | 7119 | entry_symbol.name); |
252b5132 RH |
7120 | } |
7121 | } | |
5226a6a8 NC |
7122 | else |
7123 | { | |
7124 | if (warn) | |
7125 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
7126 | " not setting start address\n"), | |
7127 | entry_symbol.name); | |
7128 | } | |
252b5132 RH |
7129 | } |
7130 | } | |
7131 | ||
7132 | /* This is a small function used when we want to ignore errors from | |
7133 | BFD. */ | |
7134 | ||
7135 | static void | |
52d45da3 AM |
7136 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
7137 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
7138 | { |
7139 | /* Don't do anything. */ | |
7140 | } | |
7141 | ||
7142 | /* Check that the architecture of all the input files is compatible | |
7143 | with the output file. Also call the backend to let it do any | |
7144 | other checking that is needed. */ | |
7145 | ||
7146 | static void | |
1579bae1 | 7147 | lang_check (void) |
252b5132 | 7148 | { |
36983a93 | 7149 | lang_input_statement_type *file; |
252b5132 | 7150 | bfd *input_bfd; |
5f992e62 | 7151 | const bfd_arch_info_type *compatible; |
252b5132 | 7152 | |
8ce18f9c | 7153 | for (file = (void *) file_chain.head; |
36983a93 AM |
7154 | file != NULL; |
7155 | file = file->next) | |
252b5132 | 7156 | { |
0381901e | 7157 | #if BFD_SUPPORTS_PLUGINS |
422b6f14 | 7158 | /* Don't check format of files claimed by plugin. */ |
36983a93 | 7159 | if (file->flags.claimed) |
422b6f14 | 7160 | continue; |
0381901e | 7161 | #endif /* BFD_SUPPORTS_PLUGINS */ |
36983a93 | 7162 | input_bfd = file->the_bfd; |
6feb9908 | 7163 | compatible |
f13a99db | 7164 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 7165 | command_line.accept_unknown_input_arch); |
30cba025 AM |
7166 | |
7167 | /* In general it is not possible to perform a relocatable | |
7168 | link between differing object formats when the input | |
7169 | file has relocations, because the relocations in the | |
7170 | input format may not have equivalent representations in | |
7171 | the output format (and besides BFD does not translate | |
7172 | relocs for other link purposes than a final link). */ | |
f437dadd AM |
7173 | if (!file->flags.just_syms |
7174 | && (bfd_link_relocatable (&link_info) | |
7175 | || link_info.emitrelocations) | |
30cba025 | 7176 | && (compatible == NULL |
f13a99db AM |
7177 | || (bfd_get_flavour (input_bfd) |
7178 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
7179 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
7180 | { | |
df5f2391 | 7181 | einfo (_("%F%P: relocatable linking with relocations from" |
871b3ab2 | 7182 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 7183 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 7184 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
7185 | /* einfo with %F exits. */ |
7186 | } | |
7187 | ||
252b5132 RH |
7188 | if (compatible == NULL) |
7189 | { | |
7190 | if (command_line.warn_mismatch) | |
df5f2391 | 7191 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 7192 | " is incompatible with %s output\n"), |
252b5132 | 7193 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 7194 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 7195 | } |
b9247304 | 7196 | |
a8acd6ee AM |
7197 | /* If the input bfd has no contents, it shouldn't set the |
7198 | private data of the output bfd. */ | |
f437dadd AM |
7199 | else if (!file->flags.just_syms |
7200 | && ((input_bfd->flags & DYNAMIC) != 0 | |
7201 | || bfd_count_sections (input_bfd) != 0)) | |
a8acd6ee | 7202 | { |
252b5132 RH |
7203 | bfd_error_handler_type pfn = NULL; |
7204 | ||
7205 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
7206 | files, temporarily set the BFD error handler to a |
7207 | function which will do nothing. We still want to call | |
7208 | bfd_merge_private_bfd_data, since it may set up | |
7209 | information which is needed in the output file. */ | |
0aa7f586 | 7210 | if (!command_line.warn_mismatch) |
252b5132 | 7211 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 7212 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
7213 | { |
7214 | if (command_line.warn_mismatch) | |
df5f2391 | 7215 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 7216 | " of file %pB\n"), input_bfd); |
252b5132 | 7217 | } |
0aa7f586 | 7218 | if (!command_line.warn_mismatch) |
252b5132 RH |
7219 | bfd_set_error_handler (pfn); |
7220 | } | |
7221 | } | |
7222 | } | |
7223 | ||
7224 | /* Look through all the global common symbols and attach them to the | |
7225 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 7226 | to roughly sort the entries by alignment. */ |
252b5132 RH |
7227 | |
7228 | static void | |
1579bae1 | 7229 | lang_common (void) |
252b5132 | 7230 | { |
a4819f54 | 7231 | if (link_info.inhibit_common_definition) |
4818e05f | 7232 | return; |
0e1862bb | 7233 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 7234 | && !command_line.force_common_definition) |
252b5132 RH |
7235 | return; |
7236 | ||
0aa7f586 | 7237 | if (!config.sort_common) |
1579bae1 | 7238 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
7239 | else |
7240 | { | |
de7dd2bd NC |
7241 | unsigned int power; |
7242 | ||
7243 | if (config.sort_common == sort_descending) | |
967928e9 AM |
7244 | { |
7245 | for (power = 4; power > 0; power--) | |
7246 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7247 | ||
7248 | power = 0; | |
7249 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7250 | } | |
de7dd2bd | 7251 | else |
967928e9 AM |
7252 | { |
7253 | for (power = 0; power <= 4; power++) | |
7254 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 7255 | |
2dc0e7b4 | 7256 | power = (unsigned int) -1; |
de7dd2bd NC |
7257 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
7258 | } | |
252b5132 RH |
7259 | } |
7260 | } | |
7261 | ||
7262 | /* Place one common symbol in the correct section. */ | |
7263 | ||
f38a2680 | 7264 | static bool |
1579bae1 | 7265 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
7266 | { |
7267 | unsigned int power_of_two; | |
7268 | bfd_vma size; | |
7269 | asection *section; | |
7270 | ||
7271 | if (h->type != bfd_link_hash_common) | |
f38a2680 | 7272 | return true; |
252b5132 RH |
7273 | |
7274 | size = h->u.c.size; | |
7275 | power_of_two = h->u.c.p->alignment_power; | |
7276 | ||
de7dd2bd NC |
7277 | if (config.sort_common == sort_descending |
7278 | && power_of_two < *(unsigned int *) info) | |
f38a2680 | 7279 | return true; |
de7dd2bd | 7280 | else if (config.sort_common == sort_ascending |
967928e9 | 7281 | && power_of_two > *(unsigned int *) info) |
f38a2680 | 7282 | return true; |
252b5132 RH |
7283 | |
7284 | section = h->u.c.p->section; | |
3023e3f6 | 7285 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
df5f2391 | 7286 | einfo (_("%F%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 7287 | h->root.string); |
252b5132 RH |
7288 | |
7289 | if (config.map_file != NULL) | |
7290 | { | |
f38a2680 | 7291 | static bool header_printed; |
252b5132 RH |
7292 | int len; |
7293 | char *name; | |
6b9bd54c | 7294 | char buf[32]; |
252b5132 | 7295 | |
0aa7f586 | 7296 | if (!header_printed) |
252b5132 RH |
7297 | { |
7298 | minfo (_("\nAllocating common symbols\n")); | |
7299 | minfo (_("Common symbol size file\n\n")); | |
f38a2680 | 7300 | header_printed = true; |
252b5132 RH |
7301 | } |
7302 | ||
f13a99db | 7303 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 7304 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
7305 | if (name == NULL) |
7306 | { | |
7307 | minfo ("%s", h->root.string); | |
7308 | len = strlen (h->root.string); | |
7309 | } | |
7310 | else | |
7311 | { | |
7312 | minfo ("%s", name); | |
7313 | len = strlen (name); | |
7314 | free (name); | |
7315 | } | |
252b5132 RH |
7316 | |
7317 | if (len >= 19) | |
7318 | { | |
7319 | print_nl (); | |
7320 | len = 0; | |
7321 | } | |
252b5132 | 7322 | |
f493c217 | 7323 | sprintf (buf, "%" PRIx64, (uint64_t) size); |
6b9bd54c | 7324 | fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); |
252b5132 | 7325 | |
871b3ab2 | 7326 | minfo ("%pB\n", section->owner); |
252b5132 RH |
7327 | } |
7328 | ||
f38a2680 | 7329 | return true; |
252b5132 RH |
7330 | } |
7331 | ||
c005eb9e AB |
7332 | /* Handle a single orphan section S, placing the orphan into an appropriate |
7333 | output section. The effects of the --orphan-handling command line | |
7334 | option are handled here. */ | |
7335 | ||
7336 | static void | |
7337 | ldlang_place_orphan (asection *s) | |
7338 | { | |
7339 | if (config.orphan_handling == orphan_handling_discard) | |
7340 | { | |
7341 | lang_output_section_statement_type *os; | |
21401fc7 | 7342 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); |
c005eb9e AB |
7343 | if (os->addr_tree == NULL |
7344 | && (bfd_link_relocatable (&link_info) | |
7345 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7346 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7347 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7348 | } |
7349 | else | |
7350 | { | |
7351 | lang_output_section_statement_type *os; | |
7352 | const char *name = s->name; | |
7353 | int constraint = 0; | |
7354 | ||
7355 | if (config.orphan_handling == orphan_handling_error) | |
df5f2391 | 7356 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
7357 | s, s->owner); |
7358 | ||
7359 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
7360 | constraint = SPECIAL; | |
7361 | ||
7362 | os = ldemul_place_orphan (s, name, constraint); | |
7363 | if (os == NULL) | |
7364 | { | |
21401fc7 | 7365 | os = lang_output_section_statement_lookup (name, constraint, 1); |
c005eb9e AB |
7366 | if (os->addr_tree == NULL |
7367 | && (bfd_link_relocatable (&link_info) | |
7368 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7369 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7370 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7371 | } |
7372 | ||
7373 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 7374 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 7375 | "placed in section `%s'\n"), |
c005eb9e AB |
7376 | s, s->owner, os->name); |
7377 | } | |
7378 | } | |
7379 | ||
08da4cac KH |
7380 | /* Run through the input files and ensure that every input section has |
7381 | somewhere to go. If one is found without a destination then create | |
7382 | an input request and place it into the statement tree. */ | |
252b5132 RH |
7383 | |
7384 | static void | |
1579bae1 | 7385 | lang_place_orphans (void) |
252b5132 | 7386 | { |
e50d8076 | 7387 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
7388 | { |
7389 | asection *s; | |
7390 | ||
1579bae1 | 7391 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7392 | { |
1579bae1 | 7393 | if (s->output_section == NULL) |
252b5132 | 7394 | { |
396a2467 | 7395 | /* This section of the file is not attached, root |
afd7a018 | 7396 | around for a sensible place for it to go. */ |
252b5132 | 7397 | |
66be1055 | 7398 | if (file->flags.just_syms) |
1449d79b | 7399 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 7400 | else if (lang_discard_section_p (s)) |
164e712d | 7401 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
7402 | else if (strcmp (s->name, "COMMON") == 0) |
7403 | { | |
7404 | /* This is a lonely common section which must have | |
7405 | come from an archive. We attach to the section | |
7406 | with the wildcard. */ | |
0e1862bb | 7407 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
7408 | || command_line.force_common_definition) |
7409 | { | |
7410 | if (default_common_section == NULL) | |
66c103b7 | 7411 | default_common_section |
21401fc7 | 7412 | = lang_output_section_statement_lookup (".bss", 0, 1); |
39dcfe18 | 7413 | lang_add_section (&default_common_section->children, s, |
b209b5a6 | 7414 | NULL, NULL, default_common_section); |
252b5132 RH |
7415 | } |
7416 | } | |
252b5132 | 7417 | else |
c005eb9e | 7418 | ldlang_place_orphan (s); |
252b5132 RH |
7419 | } |
7420 | } | |
7421 | } | |
7422 | } | |
7423 | ||
252b5132 | 7424 | void |
1579bae1 | 7425 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7426 | { |
aa8804e4 | 7427 | flagword *ptr_flags; |
252b5132 | 7428 | |
aa8804e4 | 7429 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7430 | |
252b5132 RH |
7431 | while (*flags) |
7432 | { | |
7433 | switch (*flags) | |
7434 | { | |
f0673d20 NC |
7435 | /* PR 17900: An exclamation mark in the attributes reverses |
7436 | the sense of any of the attributes that follow. */ | |
7437 | case '!': | |
0aa7f586 | 7438 | invert = !invert; |
f0673d20 NC |
7439 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7440 | break; | |
7441 | ||
252b5132 RH |
7442 | case 'A': case 'a': |
7443 | *ptr_flags |= SEC_ALLOC; | |
7444 | break; | |
7445 | ||
7446 | case 'R': case 'r': | |
7447 | *ptr_flags |= SEC_READONLY; | |
7448 | break; | |
7449 | ||
7450 | case 'W': case 'w': | |
7451 | *ptr_flags |= SEC_DATA; | |
7452 | break; | |
7453 | ||
7454 | case 'X': case 'x': | |
7455 | *ptr_flags |= SEC_CODE; | |
7456 | break; | |
7457 | ||
7458 | case 'L': case 'l': | |
7459 | case 'I': case 'i': | |
7460 | *ptr_flags |= SEC_LOAD; | |
7461 | break; | |
7462 | ||
7463 | default: | |
df5f2391 | 7464 | einfo (_("%F%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7465 | *flags, *flags); |
252b5132 RH |
7466 | break; |
7467 | } | |
7468 | flags++; | |
7469 | } | |
7470 | } | |
7471 | ||
d871d478 AM |
7472 | /* Call a function on each real input file. This function will be |
7473 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7474 | |
7475 | void | |
1579bae1 | 7476 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7477 | { |
7478 | lang_input_statement_type *f; | |
7479 | ||
8ce18f9c | 7480 | for (f = (void *) input_file_chain.head; |
252b5132 | 7481 | f != NULL; |
36983a93 | 7482 | f = f->next_real_file) |
d871d478 AM |
7483 | if (f->flags.real) |
7484 | func (f); | |
252b5132 RH |
7485 | } |
7486 | ||
d871d478 AM |
7487 | /* Call a function on each real file. The function will be called on |
7488 | all the elements of an archive which are included in the link, but | |
7489 | will not be called on the archive file itself. */ | |
252b5132 RH |
7490 | |
7491 | void | |
1579bae1 | 7492 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7493 | { |
e50d8076 | 7494 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7495 | { |
d871d478 AM |
7496 | if (f->flags.real) |
7497 | func (f); | |
252b5132 RH |
7498 | } |
7499 | } | |
7500 | ||
252b5132 | 7501 | void |
1579bae1 | 7502 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7503 | { |
36983a93 | 7504 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7505 | |
7506 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7507 | a link. */ | |
9221725d AM |
7508 | ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next |
7509 | && entry->the_bfd->link.next == NULL); | |
f13a99db | 7510 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7511 | |
7512 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7513 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7514 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
7515 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
7516 | ||
7517 | /* Look through the sections and check for any which should not be | |
7518 | included in the link. We need to do this now, so that we can | |
7519 | notice when the backend linker tries to report multiple | |
7520 | definition errors for symbols which are in sections we aren't | |
7521 | going to link. FIXME: It might be better to entirely ignore | |
7522 | symbols which are defined in sections which are going to be | |
7523 | discarded. This would require modifying the backend linker for | |
7524 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7525 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7526 | ||
1579bae1 | 7527 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7528 | } |
7529 | ||
7530 | void | |
1579bae1 | 7531 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7532 | { |
7533 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7534 | if (!had_output_filename || !from_script) |
252b5132 RH |
7535 | { |
7536 | output_filename = name; | |
f38a2680 | 7537 | had_output_filename = true; |
252b5132 RH |
7538 | } |
7539 | } | |
7540 | ||
aea4bd9d | 7541 | lang_output_section_statement_type * |
1579bae1 AM |
7542 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7543 | etree_type *address_exp, | |
7544 | enum section_type sectype, | |
c212f39d | 7545 | etree_type *sectype_value, |
1579bae1 AM |
7546 | etree_type *align, |
7547 | etree_type *subalign, | |
0841712e | 7548 | etree_type *ebase, |
1eec346e NC |
7549 | int constraint, |
7550 | int align_with_input) | |
252b5132 RH |
7551 | { |
7552 | lang_output_section_statement_type *os; | |
7553 | ||
66c103b7 | 7554 | os = lang_output_section_statement_lookup (output_section_statement_name, |
21401fc7 | 7555 | constraint, 2); |
967928e9 | 7556 | current_section = os; |
252b5132 | 7557 | |
1579bae1 | 7558 | if (os->addr_tree == NULL) |
08da4cac KH |
7559 | { |
7560 | os->addr_tree = address_exp; | |
7561 | } | |
252b5132 | 7562 | os->sectype = sectype; |
c212f39d FS |
7563 | if (sectype == type_section || sectype == typed_readonly_section) |
7564 | os->sectype_value = sectype_value; | |
7565 | else if (sectype == noload_section) | |
252b5132 | 7566 | os->flags = SEC_NEVER_LOAD; |
c212f39d FS |
7567 | else |
7568 | os->flags = SEC_NO_FLAGS; | |
e5caa5e0 | 7569 | os->block_value = 1; |
66c103b7 AM |
7570 | |
7571 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 7572 | push_stat_ptr (&os->children); |
252b5132 | 7573 | |
1eec346e NC |
7574 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7575 | if (os->align_lma_with_input && align != NULL) | |
c1c8c1ef | 7576 | einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7577 | NULL); |
1eec346e | 7578 | |
3d9c8f6b AM |
7579 | os->subsection_alignment = subalign; |
7580 | os->section_alignment = align; | |
252b5132 RH |
7581 | |
7582 | os->load_base = ebase; | |
aea4bd9d | 7583 | return os; |
252b5132 RH |
7584 | } |
7585 | ||
252b5132 | 7586 | void |
1579bae1 | 7587 | lang_final (void) |
252b5132 | 7588 | { |
d3ce72d0 NC |
7589 | lang_output_statement_type *new_stmt; |
7590 | ||
7591 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7592 | new_stmt->name = output_filename; | |
252b5132 RH |
7593 | } |
7594 | ||
08da4cac KH |
7595 | /* Reset the current counters in the regions. */ |
7596 | ||
e3dc8847 | 7597 | void |
1579bae1 | 7598 | lang_reset_memory_regions (void) |
252b5132 RH |
7599 | { |
7600 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7601 | asection *o; |
e9ee469a | 7602 | lang_output_section_statement_type *os; |
252b5132 | 7603 | |
1579bae1 | 7604 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7605 | { |
252b5132 | 7606 | p->current = p->origin; |
66e28d60 | 7607 | p->last_os = NULL; |
252b5132 | 7608 | } |
b3327aad | 7609 | |
8ce18f9c | 7610 | for (os = (void *) lang_os_list.head; |
e9ee469a AM |
7611 | os != NULL; |
7612 | os = os->next) | |
cde9e0be | 7613 | { |
f38a2680 AM |
7614 | os->processed_vma = false; |
7615 | os->processed_lma = false; | |
cde9e0be | 7616 | } |
e9ee469a | 7617 | |
f13a99db | 7618 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7619 | { |
7620 | /* Save the last size for possible use by bfd_relax_section. */ | |
7621 | o->rawsize = o->size; | |
8772de11 MR |
7622 | if (!(o->flags & SEC_FIXED_SIZE)) |
7623 | o->size = 0; | |
1a23a9e6 | 7624 | } |
252b5132 RH |
7625 | } |
7626 | ||
164e712d | 7627 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7628 | |
7629 | static void | |
1579bae1 | 7630 | gc_section_callback (lang_wild_statement_type *ptr, |
b209b5a6 | 7631 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
1579bae1 AM |
7632 | asection *section, |
7633 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7634 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7635 | { |
164e712d AM |
7636 | /* If the wild pattern was marked KEEP, the member sections |
7637 | should be as well. */ | |
4dec4d4e RH |
7638 | if (ptr->keep_sections) |
7639 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7640 | } |
7641 | ||
252b5132 RH |
7642 | /* Iterate over sections marking them against GC. */ |
7643 | ||
7644 | static void | |
1579bae1 | 7645 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7646 | { |
1579bae1 | 7647 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7648 | { |
7649 | switch (s->header.type) | |
7650 | { | |
7651 | case lang_wild_statement_enum: | |
164e712d | 7652 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7653 | break; |
252b5132 RH |
7654 | case lang_constructors_statement_enum: |
7655 | lang_gc_sections_1 (constructor_list.head); | |
7656 | break; | |
7657 | case lang_output_section_statement_enum: | |
7658 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7659 | break; | |
7660 | case lang_group_statement_enum: | |
7661 | lang_gc_sections_1 (s->group_statement.children.head); | |
7662 | break; | |
7663 | default: | |
7664 | break; | |
7665 | } | |
7666 | } | |
7667 | } | |
7668 | ||
7669 | static void | |
1579bae1 | 7670 | lang_gc_sections (void) |
252b5132 | 7671 | { |
252b5132 | 7672 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7673 | lang_gc_sections_1 (statement_list.head); |
7674 | ||
9ca57817 | 7675 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
15407e7e | 7676 | the special case of .stabstr debug info. (See bfd/stabs.c) |
9ca57817 | 7677 | Twiddle the flag here, to simplify later linker code. */ |
0e1862bb | 7678 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7679 | { |
7680 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7681 | { | |
7682 | asection *sec; | |
0381901e | 7683 | #if BFD_SUPPORTS_PLUGINS |
66be1055 | 7684 | if (f->flags.claimed) |
9e2278f5 AM |
7685 | continue; |
7686 | #endif | |
9ca57817 | 7687 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
15407e7e AM |
7688 | if ((sec->flags & SEC_DEBUGGING) == 0 |
7689 | || strcmp (sec->name, ".stabstr") != 0) | |
9ca57817 AM |
7690 | sec->flags &= ~SEC_EXCLUDE; |
7691 | } | |
7692 | } | |
7693 | ||
57316bff | 7694 | if (link_info.gc_sections) |
f13a99db | 7695 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7696 | } |
7697 | ||
b10a8ae0 L |
7698 | /* Worker for lang_find_relro_sections_1. */ |
7699 | ||
7700 | static void | |
7701 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7702 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7703 | asection *section, | |
7704 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7705 | void *data) | |
7706 | { | |
7707 | /* Discarded, excluded and ignored sections effectively have zero | |
7708 | size. */ | |
7709 | if (section->output_section != NULL | |
f13a99db | 7710 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7711 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7712 | && !IGNORE_SECTION (section) | |
7713 | && section->size != 0) | |
7714 | { | |
f38a2680 AM |
7715 | bool *has_relro_section = (bool *) data; |
7716 | *has_relro_section = true; | |
b10a8ae0 L |
7717 | } |
7718 | } | |
7719 | ||
7720 | /* Iterate over sections for relro sections. */ | |
7721 | ||
7722 | static void | |
7723 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
f38a2680 | 7724 | bool *has_relro_section) |
b10a8ae0 L |
7725 | { |
7726 | if (*has_relro_section) | |
7727 | return; | |
7728 | ||
7729 | for (; s != NULL; s = s->header.next) | |
7730 | { | |
2085ee26 | 7731 | if (s == expld.dataseg.relro_end_stat) |
b10a8ae0 L |
7732 | break; |
7733 | ||
7734 | switch (s->header.type) | |
7735 | { | |
7736 | case lang_wild_statement_enum: | |
7737 | walk_wild (&s->wild_statement, | |
7738 | find_relro_section_callback, | |
7739 | has_relro_section); | |
7740 | break; | |
7741 | case lang_constructors_statement_enum: | |
7742 | lang_find_relro_sections_1 (constructor_list.head, | |
2085ee26 | 7743 | has_relro_section); |
b10a8ae0 L |
7744 | break; |
7745 | case lang_output_section_statement_enum: | |
7746 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
2085ee26 | 7747 | has_relro_section); |
b10a8ae0 L |
7748 | break; |
7749 | case lang_group_statement_enum: | |
7750 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
2085ee26 | 7751 | has_relro_section); |
b10a8ae0 L |
7752 | break; |
7753 | default: | |
7754 | break; | |
7755 | } | |
7756 | } | |
7757 | } | |
7758 | ||
7759 | static void | |
7760 | lang_find_relro_sections (void) | |
7761 | { | |
f38a2680 | 7762 | bool has_relro_section = false; |
b10a8ae0 L |
7763 | |
7764 | /* Check all sections in the link script. */ | |
7765 | ||
7766 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
2085ee26 | 7767 | &has_relro_section); |
b10a8ae0 L |
7768 | |
7769 | if (!has_relro_section) | |
f38a2680 | 7770 | link_info.relro = false; |
b10a8ae0 L |
7771 | } |
7772 | ||
fbbc3759 L |
7773 | /* Relax all sections until bfd_relax_section gives up. */ |
7774 | ||
eaeb0a9d | 7775 | void |
f38a2680 | 7776 | lang_relax_sections (bool need_layout) |
fbbc3759 | 7777 | { |
6a91be86 L |
7778 | /* NB: Also enable relaxation to layout sections for DT_RELR. */ |
7779 | if (RELAXATION_ENABLED || link_info.enable_dt_relr) | |
fbbc3759 | 7780 | { |
eaeb0a9d AM |
7781 | /* We may need more than one relaxation pass. */ |
7782 | int i = link_info.relax_pass; | |
fbbc3759 | 7783 | |
eaeb0a9d AM |
7784 | /* The backend can use it to determine the current pass. */ |
7785 | link_info.relax_pass = 0; | |
fbbc3759 | 7786 | |
eaeb0a9d AM |
7787 | while (i--) |
7788 | { | |
7789 | /* Keep relaxing until bfd_relax_section gives up. */ | |
f38a2680 | 7790 | bool relax_again; |
fbbc3759 | 7791 | |
eaeb0a9d AM |
7792 | link_info.relax_trip = -1; |
7793 | do | |
7794 | { | |
7795 | link_info.relax_trip++; | |
7796 | ||
7797 | /* Note: pe-dll.c does something like this also. If you find | |
7798 | you need to change this code, you probably need to change | |
7799 | pe-dll.c also. DJ */ | |
7800 | ||
7801 | /* Do all the assignments with our current guesses as to | |
7802 | section sizes. */ | |
2f65ac72 | 7803 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7804 | |
7805 | /* We must do this after lang_do_assignments, because it uses | |
7806 | size. */ | |
7807 | lang_reset_memory_regions (); | |
7808 | ||
7809 | /* Perform another relax pass - this time we know where the | |
7810 | globals are, so can make a better guess. */ | |
f38a2680 AM |
7811 | relax_again = false; |
7812 | lang_size_sections (&relax_again, false); | |
eaeb0a9d AM |
7813 | } |
7814 | while (relax_again); | |
7815 | ||
7816 | link_info.relax_pass++; | |
7817 | } | |
f38a2680 | 7818 | need_layout = true; |
eaeb0a9d | 7819 | } |
fbbc3759 | 7820 | |
eaeb0a9d AM |
7821 | if (need_layout) |
7822 | { | |
7823 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 7824 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d | 7825 | lang_reset_memory_regions (); |
f38a2680 | 7826 | lang_size_sections (NULL, true); |
fbbc3759 | 7827 | } |
fbbc3759 L |
7828 | } |
7829 | ||
0381901e | 7830 | #if BFD_SUPPORTS_PLUGINS |
8543fde5 DK |
7831 | /* Find the insert point for the plugin's replacement files. We |
7832 | place them after the first claimed real object file, or if the | |
7833 | first claimed object is an archive member, after the last real | |
7834 | object file immediately preceding the archive. In the event | |
7835 | no objects have been claimed at all, we return the first dummy | |
7836 | object file on the list as the insert point; that works, but | |
7837 | the callee must be careful when relinking the file_chain as it | |
7838 | is not actually on that chain, only the statement_list and the | |
7839 | input_file list; in that case, the replacement files must be | |
7840 | inserted at the head of the file_chain. */ | |
7841 | ||
7842 | static lang_input_statement_type * | |
f38a2680 | 7843 | find_replacements_insert_point (bool *before) |
8543fde5 DK |
7844 | { |
7845 | lang_input_statement_type *claim1, *lastobject; | |
8ce18f9c AM |
7846 | lastobject = (void *) input_file_chain.head; |
7847 | for (claim1 = (void *) file_chain.head; | |
8543fde5 | 7848 | claim1 != NULL; |
36983a93 | 7849 | claim1 = claim1->next) |
8543fde5 | 7850 | { |
66be1055 | 7851 | if (claim1->flags.claimed) |
128bf1fe AM |
7852 | { |
7853 | *before = claim1->flags.claim_archive; | |
7854 | return claim1->flags.claim_archive ? lastobject : claim1; | |
7855 | } | |
8543fde5 | 7856 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 7857 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
7858 | lastobject = claim1; |
7859 | } | |
7860 | /* No files were claimed by the plugin. Choose the last object | |
7861 | file found on the list (maybe the first, dummy entry) as the | |
7862 | insert point. */ | |
f38a2680 | 7863 | *before = false; |
8543fde5 DK |
7864 | return lastobject; |
7865 | } | |
9e2278f5 | 7866 | |
1fa4ec6a AM |
7867 | /* Find where to insert ADD, an archive element or shared library |
7868 | added during a rescan. */ | |
7869 | ||
36983a93 | 7870 | static lang_input_statement_type ** |
1fa4ec6a AM |
7871 | find_rescan_insertion (lang_input_statement_type *add) |
7872 | { | |
7873 | bfd *add_bfd = add->the_bfd; | |
7874 | lang_input_statement_type *f; | |
7875 | lang_input_statement_type *last_loaded = NULL; | |
7876 | lang_input_statement_type *before = NULL; | |
36983a93 | 7877 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
7878 | |
7879 | if (add_bfd->my_archive != NULL) | |
7880 | add_bfd = add_bfd->my_archive; | |
7881 | ||
7882 | /* First look through the input file chain, to find an object file | |
7883 | before the one we've rescanned. Normal object files always | |
7884 | appear on both the input file chain and the file chain, so this | |
7885 | lets us get quickly to somewhere near the correct place on the | |
7886 | file chain if it is full of archive elements. Archives don't | |
7887 | appear on the file chain, but if an element has been extracted | |
7888 | then their input_statement->next points at it. */ | |
8ce18f9c | 7889 | for (f = (void *) input_file_chain.head; |
1fa4ec6a | 7890 | f != NULL; |
36983a93 | 7891 | f = f->next_real_file) |
1fa4ec6a AM |
7892 | { |
7893 | if (f->the_bfd == add_bfd) | |
7894 | { | |
7895 | before = last_loaded; | |
7896 | if (f->next != NULL) | |
36983a93 | 7897 | return &f->next->next; |
1fa4ec6a AM |
7898 | } |
7899 | if (f->the_bfd != NULL && f->next != NULL) | |
7900 | last_loaded = f; | |
7901 | } | |
7902 | ||
7903 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
7904 | *iter != NULL; | |
36983a93 AM |
7905 | iter = &(*iter)->next) |
7906 | if (!(*iter)->flags.claim_archive | |
7907 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
7908 | break; |
7909 | ||
7910 | return iter; | |
7911 | } | |
7912 | ||
9e2278f5 AM |
7913 | /* Insert SRCLIST into DESTLIST after given element by chaining |
7914 | on FIELD as the next-pointer. (Counterintuitively does not need | |
7915 | a pointer to the actual after-node itself, just its chain field.) */ | |
7916 | ||
7917 | static void | |
7918 | lang_list_insert_after (lang_statement_list_type *destlist, | |
7919 | lang_statement_list_type *srclist, | |
7920 | lang_statement_union_type **field) | |
7921 | { | |
7922 | *(srclist->tail) = *field; | |
7923 | *field = srclist->head; | |
7924 | if (destlist->tail == field) | |
7925 | destlist->tail = srclist->tail; | |
7926 | } | |
7927 | ||
7928 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
7929 | was taken as a copy of it and leave them in ORIGLIST. */ | |
7930 | ||
7931 | static void | |
7932 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
7933 | lang_statement_list_type *origlist) | |
7934 | { | |
7935 | union lang_statement_union **savetail; | |
7936 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
7937 | ASSERT (origlist->head == destlist->head); | |
7938 | savetail = origlist->tail; | |
7939 | origlist->head = *(savetail); | |
7940 | origlist->tail = destlist->tail; | |
7941 | destlist->tail = savetail; | |
7942 | *savetail = NULL; | |
7943 | } | |
128bf1fe AM |
7944 | |
7945 | static lang_statement_union_type ** | |
7946 | find_next_input_statement (lang_statement_union_type **s) | |
7947 | { | |
7948 | for ( ; *s; s = &(*s)->header.next) | |
7949 | { | |
7950 | lang_statement_union_type **t; | |
7951 | switch ((*s)->header.type) | |
7952 | { | |
7953 | case lang_input_statement_enum: | |
7954 | return s; | |
7955 | case lang_wild_statement_enum: | |
7956 | t = &(*s)->wild_statement.children.head; | |
7957 | break; | |
7958 | case lang_group_statement_enum: | |
7959 | t = &(*s)->group_statement.children.head; | |
7960 | break; | |
7961 | case lang_output_section_statement_enum: | |
7962 | t = &(*s)->output_section_statement.children.head; | |
7963 | break; | |
7964 | default: | |
7965 | continue; | |
7966 | } | |
7967 | t = find_next_input_statement (t); | |
7968 | if (*t) | |
7969 | return t; | |
7970 | } | |
7971 | return s; | |
7972 | } | |
0381901e | 7973 | #endif /* BFD_SUPPORTS_PLUGINS */ |
8543fde5 | 7974 | |
4153b6db NC |
7975 | /* Add NAME to the list of garbage collection entry points. */ |
7976 | ||
7977 | void | |
0aa7f586 | 7978 | lang_add_gc_name (const char *name) |
4153b6db NC |
7979 | { |
7980 | struct bfd_sym_chain *sym; | |
7981 | ||
7982 | if (name == NULL) | |
7983 | return; | |
7984 | ||
988de25b | 7985 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
7986 | |
7987 | sym->next = link_info.gc_sym_list; | |
7988 | sym->name = name; | |
7989 | link_info.gc_sym_list = sym; | |
7990 | } | |
7991 | ||
fbf05aa7 L |
7992 | /* Check relocations. */ |
7993 | ||
7994 | static void | |
7995 | lang_check_relocs (void) | |
7996 | { | |
4f3b23b3 | 7997 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
7998 | { |
7999 | bfd *abfd; | |
8000 | ||
8001 | for (abfd = link_info.input_bfds; | |
8002 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 8003 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 8004 | { |
4f3b23b3 | 8005 | /* No object output, fail return. */ |
f38a2680 | 8006 | config.make_executable = false; |
4f3b23b3 NC |
8007 | /* Note: we do not abort the loop, but rather |
8008 | continue the scan in case there are other | |
8009 | bad relocations to report. */ | |
fbf05aa7 L |
8010 | } |
8011 | } | |
8012 | } | |
8013 | ||
77f5e65e AB |
8014 | /* Look through all output sections looking for places where we can |
8015 | propagate forward the lma region. */ | |
8016 | ||
8017 | static void | |
8018 | lang_propagate_lma_regions (void) | |
8019 | { | |
8020 | lang_output_section_statement_type *os; | |
8021 | ||
8ce18f9c | 8022 | for (os = (void *) lang_os_list.head; |
77f5e65e AB |
8023 | os != NULL; |
8024 | os = os->next) | |
8025 | { | |
8026 | if (os->prev != NULL | |
8027 | && os->lma_region == NULL | |
8028 | && os->load_base == NULL | |
8029 | && os->addr_tree == NULL | |
8030 | && os->region == os->prev->region) | |
8031 | os->lma_region = os->prev->lma_region; | |
8032 | } | |
8033 | } | |
8034 | ||
c63d4862 AM |
8035 | static void |
8036 | warn_non_contiguous_discards (void) | |
8037 | { | |
8038 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
8039 | { | |
8040 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
8041 | || file->flags.just_syms) | |
8042 | continue; | |
8043 | ||
8044 | for (asection *s = file->the_bfd->sections; s != NULL; s = s->next) | |
8045 | if (s->output_section == NULL | |
8046 | && (s->flags & SEC_LINKER_CREATED) == 0) | |
8047 | einfo (_("%P: warning: --enable-non-contiguous-regions " | |
8048 | "discards section `%pA' from `%pB'\n"), | |
8049 | s, file->the_bfd); | |
8050 | } | |
8051 | } | |
8052 | ||
50be5d11 MM |
8053 | static void |
8054 | reset_one_wild (lang_statement_union_type *statement) | |
8055 | { | |
8056 | if (statement->header.type == lang_wild_statement_enum) | |
8057 | { | |
8058 | lang_wild_statement_type *stmt = &statement->wild_statement; | |
50be5d11 MM |
8059 | lang_list_init (&stmt->matching_sections); |
8060 | } | |
8061 | } | |
8062 | ||
8063 | static void | |
8064 | reset_resolved_wilds (void) | |
8065 | { | |
8066 | lang_for_each_statement (reset_one_wild); | |
8067 | } | |
8068 | ||
252b5132 | 8069 | void |
1579bae1 | 8070 | lang_process (void) |
252b5132 | 8071 | { |
55255dae | 8072 | /* Finalize dynamic list. */ |
40b36307 L |
8073 | if (link_info.dynamic_list) |
8074 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 8075 | |
252b5132 RH |
8076 | current_target = default_target; |
8077 | ||
08da4cac KH |
8078 | /* Open the output file. */ |
8079 | lang_for_each_statement (ldlang_open_output); | |
61826503 | 8080 | init_opb (NULL); |
252b5132 RH |
8081 | |
8082 | ldemul_create_output_section_statements (); | |
8083 | ||
08da4cac | 8084 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
8085 | lang_place_undefineds (); |
8086 | ||
082b7297 | 8087 | if (!bfd_section_already_linked_table_init ()) |
df5f2391 | 8088 | einfo (_("%F%P: can not create hash table: %E\n")); |
9503fd87 | 8089 | |
874ef038 AB |
8090 | /* A first pass through the memory regions ensures that if any region |
8091 | references a symbol for its origin or length then this symbol will be | |
8092 | added to the symbol table. Having these symbols in the symbol table | |
8093 | means that when we call open_input_bfds PROVIDE statements will | |
8094 | trigger to provide any needed symbols. The regions origins and | |
8095 | lengths are not assigned as a result of this call. */ | |
f38a2680 | 8096 | lang_do_memory_regions (false); |
874ef038 | 8097 | |
08da4cac | 8098 | /* Create a bfd for each input file. */ |
252b5132 | 8099 | current_target = default_target; |
165f707a | 8100 | lang_statement_iteration++; |
486329aa | 8101 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
874ef038 AB |
8102 | |
8103 | /* Now that open_input_bfds has processed assignments and provide | |
8104 | statements we can give values to symbolic origin/length now. */ | |
f38a2680 | 8105 | lang_do_memory_regions (true); |
252b5132 | 8106 | |
2605f35c AM |
8107 | ldemul_before_plugin_all_symbols_read (); |
8108 | ||
0381901e | 8109 | #if BFD_SUPPORTS_PLUGINS |
716db898 | 8110 | if (link_info.lto_plugin_active) |
f84854b6 | 8111 | { |
8543fde5 DK |
8112 | lang_statement_list_type added; |
8113 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 8114 | |
f84854b6 L |
8115 | /* Now all files are read, let the plugin(s) decide if there |
8116 | are any more to be added to the link before we call the | |
8543fde5 DK |
8117 | emulation's after_open hook. We create a private list of |
8118 | input statements for this purpose, which we will eventually | |
370dfff4 | 8119 | insert into the global statement list after the first claimed |
8543fde5 DK |
8120 | file. */ |
8121 | added = *stat_ptr; | |
8122 | /* We need to manipulate all three chains in synchrony. */ | |
8123 | files = file_chain; | |
8124 | inputfiles = input_file_chain; | |
f84854b6 | 8125 | if (plugin_call_all_symbols_read ()) |
df5f2391 | 8126 | einfo (_("%F%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 8127 | plugin_error_plugin ()); |
f38a2680 | 8128 | link_info.lto_all_symbols_read = true; |
8543fde5 | 8129 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 8130 | plugin_undefs = link_info.hash->undefs_tail; |
cdaa438c | 8131 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
b02c4f16 AM |
8132 | if (plugin_undefs == link_info.hash->undefs_tail) |
8133 | plugin_undefs = NULL; | |
8543fde5 DK |
8134 | /* Restore the global list pointer now they have all been added. */ |
8135 | lang_list_remove_tail (stat_ptr, &added); | |
8136 | /* And detach the fresh ends of the file lists. */ | |
8137 | lang_list_remove_tail (&file_chain, &files); | |
8138 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
8139 | /* Were any new files added? */ | |
8140 | if (added.head != NULL) | |
8141 | { | |
8142 | /* If so, we will insert them into the statement list immediately | |
128bf1fe AM |
8143 | after the first input file that was claimed by the plugin, |
8144 | unless that file was an archive in which case it is inserted | |
8145 | immediately before. */ | |
f38a2680 | 8146 | bool before; |
128bf1fe AM |
8147 | lang_statement_union_type **prev; |
8148 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
8149 | /* If a plugin adds input files without having claimed any, we |
8150 | don't really have a good idea where to place them. Just putting | |
8151 | them at the start or end of the list is liable to leave them | |
8152 | outside the crtbegin...crtend range. */ | |
9e2278f5 AM |
8153 | ASSERT (plugin_insert != NULL); |
8154 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
8155 | prev = &plugin_insert->header.next; |
8156 | if (before) | |
8157 | { | |
8158 | prev = find_next_input_statement (prev); | |
36983a93 | 8159 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 8160 | { |
a19826f4 | 8161 | /* We didn't find the expected input statement. |
1f1f5b92 | 8162 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
8163 | prev = &plugin_insert->header.next; |
8164 | } | |
8165 | } | |
8166 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
8167 | /* Likewise for the file chains. */ |
8168 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 8169 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
8170 | /* We must be careful when relinking file_chain; we may need to |
8171 | insert the new files at the head of the list if the insert | |
8172 | point chosen is the dummy first input file. */ | |
9e2278f5 | 8173 | if (plugin_insert->filename) |
36983a93 AM |
8174 | lang_list_insert_after (&file_chain, &files, |
8175 | (void *) &plugin_insert->next); | |
8543fde5 DK |
8176 | else |
8177 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
8178 | |
8179 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 8180 | files = file_chain; |
165f707a | 8181 | lang_statement_iteration++; |
9e2278f5 | 8182 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
8183 | lang_list_remove_tail (&file_chain, &files); |
8184 | while (files.head != NULL) | |
8185 | { | |
36983a93 AM |
8186 | lang_input_statement_type **insert; |
8187 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
8188 | bfd *my_arch; |
8189 | ||
8190 | insert = find_rescan_insertion (&files.head->input_statement); | |
8191 | /* All elements from an archive can be added at once. */ | |
8192 | iter = &files.head->input_statement.next; | |
8193 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
8194 | if (my_arch != NULL) | |
36983a93 AM |
8195 | for (; *iter != NULL; iter = &(*iter)->next) |
8196 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
8197 | break; |
8198 | temp = *insert; | |
36983a93 AM |
8199 | *insert = &files.head->input_statement; |
8200 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a | 8201 | *iter = temp; |
081609f5 AM |
8202 | if (file_chain.tail == (lang_statement_union_type **) insert) |
8203 | file_chain.tail = (lang_statement_union_type **) iter; | |
1fa4ec6a AM |
8204 | if (my_arch != NULL) |
8205 | { | |
00f93c44 | 8206 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 8207 | if (parent != NULL) |
36983a93 | 8208 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
8209 | ((char *) iter |
8210 | - offsetof (lang_input_statement_type, next)); | |
8211 | } | |
8212 | } | |
8543fde5 | 8213 | } |
f84854b6 | 8214 | } |
0381901e | 8215 | #endif /* BFD_SUPPORTS_PLUGINS */ |
5d3236ee | 8216 | |
2605f35c AM |
8217 | struct bfd_sym_chain **sym = &link_info.gc_sym_list; |
8218 | while (*sym) | |
8219 | sym = &(*sym)->next; | |
4153b6db | 8220 | |
2605f35c | 8221 | *sym = &entry_symbol; |
41f46ed9 | 8222 | |
e3e942e9 | 8223 | if (entry_symbol.name == NULL) |
41f46ed9 | 8224 | { |
2605f35c | 8225 | *sym = ldlang_undef_chain_list_head; |
41f46ed9 | 8226 | |
2605f35c | 8227 | /* entry_symbol is normally initialised by an ENTRY definition in the |
41f46ed9 SKS |
8228 | linker script or the -e command line option. But if neither of |
8229 | these have been used, the target specific backend may still have | |
4153b6db | 8230 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
8231 | Unfortunately this value will not be processed until lang_end() |
8232 | is called, long after this function has finished. So detect this | |
8233 | case here and add the target's entry symbol to the list of starting | |
8234 | points for garbage collection resolution. */ | |
4153b6db | 8235 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
8236 | } |
8237 | ||
4153b6db NC |
8238 | lang_add_gc_name (link_info.init_function); |
8239 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 8240 | |
252b5132 | 8241 | ldemul_after_open (); |
16e4ecc0 AM |
8242 | if (config.map_file != NULL) |
8243 | lang_print_asneeded (); | |
252b5132 | 8244 | |
1ff6de03 NA |
8245 | ldlang_open_ctf (); |
8246 | ||
082b7297 | 8247 | bfd_section_already_linked_table_free (); |
9503fd87 | 8248 | |
252b5132 RH |
8249 | /* Make sure that we're not mixing architectures. We call this |
8250 | after all the input files have been opened, but before we do any | |
8251 | other processing, so that any operations merge_private_bfd_data | |
8252 | does on the output file will be known during the rest of the | |
8253 | link. */ | |
8254 | lang_check (); | |
8255 | ||
8256 | /* Handle .exports instead of a version script if we're told to do so. */ | |
8257 | if (command_line.version_exports_section) | |
8258 | lang_do_version_exports_section (); | |
8259 | ||
8260 | /* Build all sets based on the information gathered from the input | |
8261 | files. */ | |
8262 | ldctor_build_sets (); | |
8263 | ||
97da0e26 | 8264 | lang_symbol_tweaks (); |
7dba9362 | 8265 | |
2aa9aad9 NC |
8266 | /* PR 13683: We must rerun the assignments prior to running garbage |
8267 | collection in order to make sure that all symbol aliases are resolved. */ | |
8268 | lang_do_assignments (lang_mark_phase_enum); | |
8269 | expld.phase = lang_first_phase_enum; | |
8270 | ||
08da4cac | 8271 | /* Size up the common data. */ |
252b5132 RH |
8272 | lang_common (); |
8273 | ||
b1eecf6f MM |
8274 | if (0) |
8275 | debug_prefix_tree (); | |
8276 | ||
8277 | resolve_wilds (); | |
8278 | ||
c4621b33 AM |
8279 | /* Remove unreferenced sections if asked to. */ |
8280 | lang_gc_sections (); | |
8281 | ||
5789f845 AM |
8282 | lang_mark_undefineds (); |
8283 | ||
fbf05aa7 L |
8284 | /* Check relocations. */ |
8285 | lang_check_relocs (); | |
8286 | ||
5c3261b0 L |
8287 | ldemul_after_check_relocs (); |
8288 | ||
50be5d11 MM |
8289 | /* There might have been new sections created (e.g. as result of |
8290 | checking relocs to need a .got, or suchlike), so to properly order | |
8291 | them into our lists of matching sections reset them here. */ | |
8292 | reset_resolved_wilds (); | |
b1eecf6f | 8293 | resolve_wilds (); |
50be5d11 | 8294 | |
af31506c MM |
8295 | /* Update wild statements in case the user gave --sort-section. |
8296 | Note how the option might have come after the linker script and | |
8297 | so couldn't have been set when the wild statements were created. */ | |
bcaa7b3e L |
8298 | update_wild_statements (statement_list.head); |
8299 | ||
252b5132 | 8300 | /* Run through the contours of the script and attach input sections |
08da4cac | 8301 | to the correct output sections. */ |
e759c116 | 8302 | lang_statement_iteration++; |
1579bae1 | 8303 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 8304 | |
776ab89f AM |
8305 | /* Start at the statement immediately after the special abs_section |
8306 | output statement, so that it isn't reordered. */ | |
8307 | process_insert_statements (&lang_os_list.head->header.next); | |
53d25da6 | 8308 | |
9b538ba7 L |
8309 | ldemul_before_place_orphans (); |
8310 | ||
08da4cac | 8311 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
8312 | lang_place_orphans (); |
8313 | ||
0e1862bb | 8314 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 8315 | { |
57ceae94 AM |
8316 | asection *found; |
8317 | ||
8318 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
8319 | sections, so that GCed sections are not merged, but before | |
8320 | assigning dynamic symbols, since removing whole input sections | |
8321 | is hard then. */ | |
f13a99db | 8322 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 8323 | |
862120bd | 8324 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 8325 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 8326 | |
1579bae1 | 8327 | if (found != NULL) |
862120bd AM |
8328 | { |
8329 | if (config.text_read_only) | |
8330 | found->flags |= SEC_READONLY; | |
8331 | else | |
8332 | found->flags &= ~SEC_READONLY; | |
8333 | } | |
8334 | } | |
8335 | ||
1ff6de03 NA |
8336 | /* Merge together CTF sections. After this, only the symtab-dependent |
8337 | function and data object sections need adjustment. */ | |
8338 | lang_merge_ctf (); | |
8339 | ||
8340 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
8341 | examining things laid out late, like the strtab. */ | |
8342 | lang_write_ctf (0); | |
8343 | ||
77f5e65e AB |
8344 | /* Copy forward lma regions for output sections in same lma region. */ |
8345 | lang_propagate_lma_regions (); | |
8346 | ||
7dba9362 AM |
8347 | /* Defining __start/__stop symbols early for --gc-sections to work |
8348 | around a glibc build problem can result in these symbols being | |
8349 | defined when they should not be. Fix them now. */ | |
8350 | if (config.build_constructors) | |
8351 | lang_undef_start_stop (); | |
8352 | ||
8353 | /* Define .startof./.sizeof. symbols with preliminary values before | |
8354 | dynamic symbols are created. */ | |
8355 | if (!bfd_link_relocatable (&link_info)) | |
8356 | lang_init_startof_sizeof (); | |
8357 | ||
862120bd AM |
8358 | /* Do anything special before sizing sections. This is where ELF |
8359 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
8360 | ldemul_before_allocation (); |
8361 | ||
8362 | /* We must record the program headers before we try to fix the | |
8363 | section positions, since they will affect SIZEOF_HEADERS. */ | |
8364 | lang_record_phdrs (); | |
8365 | ||
b10a8ae0 | 8366 | /* Check relro sections. */ |
0e1862bb | 8367 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
8368 | lang_find_relro_sections (); |
8369 | ||
b3327aad | 8370 | /* Size up the sections. */ |
0aa7f586 | 8371 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 8372 | |
252b5132 | 8373 | /* See if anything special should be done now we know how big |
eaeb0a9d | 8374 | everything is. This is where relaxation is done. */ |
252b5132 RH |
8375 | ldemul_after_allocation (); |
8376 | ||
7dba9362 AM |
8377 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
8378 | lang_finalize_start_stop (); | |
252b5132 | 8379 | |
7f0cfc6e AM |
8380 | /* Do all the assignments again, to report errors. Assignment |
8381 | statements are processed multiple times, updating symbols; In | |
8382 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
8383 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
8384 | also updated in ldemul_after_allocation. */ | |
2f65ac72 | 8385 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 8386 | |
8ded5a0f AM |
8387 | ldemul_finish (); |
8388 | ||
975f8a9e AM |
8389 | /* Convert absolute symbols to section relative. */ |
8390 | ldexp_finalize_syms (); | |
8391 | ||
252b5132 | 8392 | /* Make sure that the section addresses make sense. */ |
662ef24b | 8393 | if (command_line.check_section_addresses) |
252b5132 | 8394 | lang_check_section_addresses (); |
5f992e62 | 8395 | |
c63d4862 AM |
8396 | if (link_info.non_contiguous_regions |
8397 | && link_info.non_contiguous_regions_warnings) | |
8398 | warn_non_contiguous_discards (); | |
8399 | ||
0a618243 AB |
8400 | /* Check any required symbols are known. */ |
8401 | ldlang_check_require_defined_symbols (); | |
8402 | ||
750877ba | 8403 | lang_end (); |
252b5132 RH |
8404 | } |
8405 | ||
2d5783fa NC |
8406 | void |
8407 | lang_add_version_string (void) | |
8408 | { | |
8409 | if (! enable_linker_version) | |
8410 | return; | |
8411 | ||
8412 | const char * str = "GNU ld "; | |
8413 | int len = strlen (str); | |
8414 | int i; | |
8415 | ||
8416 | for (i = 0 ; i < len ; i++) | |
8417 | lang_add_data (BYTE, exp_intop (str[i])); | |
8418 | ||
8419 | str = BFD_VERSION_STRING; | |
8420 | len = strlen (str); | |
8421 | ||
8422 | for (i = 0 ; i < len ; i++) | |
8423 | lang_add_data (BYTE, exp_intop (str[i])); | |
8424 | ||
8425 | lang_add_data (BYTE, exp_intop ('\0')); | |
8426 | } | |
8427 | ||
252b5132 RH |
8428 | /* EXPORTED TO YACC */ |
8429 | ||
8430 | void | |
1579bae1 AM |
8431 | lang_add_wild (struct wildcard_spec *filespec, |
8432 | struct wildcard_list *section_list, | |
f38a2680 | 8433 | bool keep_sections) |
252b5132 | 8434 | { |
b6bf44ba | 8435 | struct wildcard_list *curr, *next; |
d3ce72d0 | 8436 | lang_wild_statement_type *new_stmt; |
af31506c | 8437 | bool any_specs_sorted = false; |
b6bf44ba AM |
8438 | |
8439 | /* Reverse the list as the parser puts it back to front. */ | |
8440 | for (curr = section_list, section_list = NULL; | |
8441 | curr != NULL; | |
8442 | section_list = curr, curr = next) | |
8443 | { | |
af31506c MM |
8444 | if (curr->spec.sorted != none && curr->spec.sorted != by_none) |
8445 | any_specs_sorted = true; | |
b6bf44ba AM |
8446 | next = curr->next; |
8447 | curr->next = section_list; | |
8448 | } | |
8449 | ||
8450 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 8451 | { |
b6bf44ba AM |
8452 | if (strcmp (filespec->name, "*") == 0) |
8453 | filespec->name = NULL; | |
0aa7f586 | 8454 | else if (!wildcardp (filespec->name)) |
f38a2680 | 8455 | lang_has_input_file = true; |
252b5132 | 8456 | } |
b6bf44ba | 8457 | |
d3ce72d0 NC |
8458 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
8459 | new_stmt->filename = NULL; | |
f38a2680 | 8460 | new_stmt->filenames_sorted = false; |
af31506c | 8461 | new_stmt->any_specs_sorted = any_specs_sorted; |
ae17ab41 | 8462 | new_stmt->section_flag_list = NULL; |
8f1732fc | 8463 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 8464 | if (filespec != NULL) |
252b5132 | 8465 | { |
d3ce72d0 NC |
8466 | new_stmt->filename = filespec->name; |
8467 | new_stmt->filenames_sorted = filespec->sorted == by_name; | |
ae17ab41 | 8468 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 8469 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 8470 | } |
d3ce72d0 NC |
8471 | new_stmt->section_list = section_list; |
8472 | new_stmt->keep_sections = keep_sections; | |
8473 | lang_list_init (&new_stmt->children); | |
43ae96e9 | 8474 | lang_list_init (&new_stmt->matching_sections); |
d3ce72d0 | 8475 | analyze_walk_wild_section_handler (new_stmt); |
b1eecf6f MM |
8476 | if (0) |
8477 | { | |
8478 | printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); | |
8479 | for (curr = new_stmt->section_list; curr; curr = curr->next) | |
8480 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
8481 | printf (")\n"); | |
8482 | } | |
252b5132 RH |
8483 | } |
8484 | ||
8485 | void | |
ba916c8a MM |
8486 | lang_section_start (const char *name, etree_type *address, |
8487 | const segment_type *segment) | |
252b5132 | 8488 | { |
d1778b88 | 8489 | lang_address_statement_type *ad; |
252b5132 | 8490 | |
d1778b88 | 8491 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
8492 | ad->section_name = name; |
8493 | ad->address = address; | |
ba916c8a | 8494 | ad->segment = segment; |
252b5132 RH |
8495 | } |
8496 | ||
8497 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
8498 | because of a -e argument on the command line, or zero if this is | |
8499 | called by ENTRY in a linker script. Command line arguments take | |
8500 | precedence. */ | |
8501 | ||
8502 | void | |
f38a2680 | 8503 | lang_add_entry (const char *name, bool cmdline) |
252b5132 | 8504 | { |
e3e942e9 | 8505 | if (entry_symbol.name == NULL |
252b5132 | 8506 | || cmdline |
0aa7f586 | 8507 | || !entry_from_cmdline) |
252b5132 | 8508 | { |
e3e942e9 | 8509 | entry_symbol.name = name; |
252b5132 RH |
8510 | entry_from_cmdline = cmdline; |
8511 | } | |
8512 | } | |
8513 | ||
a359509e ZW |
8514 | /* Set the default start symbol to NAME. .em files should use this, |
8515 | not lang_add_entry, to override the use of "start" if neither the | |
8516 | linker script nor the command line specifies an entry point. NAME | |
8517 | must be permanently allocated. */ | |
8518 | void | |
8519 | lang_default_entry (const char *name) | |
8520 | { | |
8521 | entry_symbol_default = name; | |
8522 | } | |
8523 | ||
252b5132 | 8524 | void |
1579bae1 | 8525 | lang_add_target (const char *name) |
252b5132 | 8526 | { |
d3ce72d0 | 8527 | lang_target_statement_type *new_stmt; |
252b5132 | 8528 | |
d3ce72d0 NC |
8529 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8530 | new_stmt->target = name; | |
252b5132 RH |
8531 | } |
8532 | ||
8533 | void | |
1579bae1 | 8534 | lang_add_map (const char *name) |
252b5132 RH |
8535 | { |
8536 | while (*name) | |
8537 | { | |
8538 | switch (*name) | |
8539 | { | |
08da4cac | 8540 | case 'F': |
f38a2680 | 8541 | map_option_f = true; |
252b5132 RH |
8542 | break; |
8543 | } | |
8544 | name++; | |
8545 | } | |
8546 | } | |
8547 | ||
8548 | void | |
1579bae1 | 8549 | lang_add_fill (fill_type *fill) |
252b5132 | 8550 | { |
d3ce72d0 | 8551 | lang_fill_statement_type *new_stmt; |
252b5132 | 8552 | |
d3ce72d0 NC |
8553 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8554 | new_stmt->fill = fill; | |
252b5132 RH |
8555 | } |
8556 | ||
8557 | void | |
1579bae1 | 8558 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8559 | { |
d3ce72d0 | 8560 | lang_data_statement_type *new_stmt; |
252b5132 | 8561 | |
d3ce72d0 NC |
8562 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8563 | new_stmt->exp = exp; | |
8564 | new_stmt->type = type; | |
252b5132 RH |
8565 | } |
8566 | ||
a4d5aec7 AM |
8567 | void |
8568 | lang_add_string (const char *s) | |
0d79a2a8 | 8569 | { |
a4d5aec7 AM |
8570 | bfd_vma len = strlen (s); |
8571 | bfd_vma i; | |
8572 | bool escape = false; | |
0d79a2a8 | 8573 | |
a4d5aec7 AM |
8574 | /* Add byte expressions until end of string. */ |
8575 | for (i = 0 ; i < len; i++) | |
0d79a2a8 US |
8576 | { |
8577 | char c = *s++; | |
8578 | ||
8579 | if (escape) | |
8580 | { | |
8581 | switch (c) | |
8582 | { | |
8583 | default: | |
8584 | /* Ignore the escape. */ | |
8585 | break; | |
8586 | ||
8587 | case 'n': c = '\n'; break; | |
8588 | case 'r': c = '\r'; break; | |
8589 | case 't': c = '\t'; break; | |
8590 | ||
8591 | case '0': | |
8592 | case '1': | |
8593 | case '2': | |
8594 | case '3': | |
8595 | case '4': | |
8596 | case '5': | |
8597 | case '6': | |
8598 | case '7': | |
8599 | /* We have an octal number. */ | |
8600 | { | |
8601 | unsigned int value = c - '0'; | |
8602 | ||
8603 | c = *s; | |
8604 | if ((c >= '0') && (c <= '7')) | |
8605 | { | |
8606 | value <<= 3; | |
8607 | value += (c - '0'); | |
8608 | i++; | |
8609 | s++; | |
a4d5aec7 | 8610 | |
0d79a2a8 US |
8611 | c = *s; |
8612 | if ((c >= '0') && (c <= '7')) | |
8613 | { | |
8614 | value <<= 3; | |
8615 | value += (c - '0'); | |
8616 | i++; | |
8617 | s++; | |
8618 | } | |
8619 | } | |
8620 | ||
8621 | if (value > 0xff) | |
8622 | { | |
8623 | /* octal: \777 is treated as '\077' + '7' */ | |
8624 | value >>= 3; | |
8625 | i--; | |
8626 | s--; | |
8627 | } | |
a4d5aec7 | 8628 | |
0d79a2a8 US |
8629 | c = value; |
8630 | } | |
8631 | break; | |
8632 | } | |
a4d5aec7 AM |
8633 | |
8634 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8635 | escape = false; |
8636 | } | |
8637 | else | |
8638 | { | |
8639 | if (c == '\\') | |
a4d5aec7 AM |
8640 | escape = true; |
8641 | else | |
8642 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8643 | } |
8644 | } | |
8645 | ||
a4d5aec7 AM |
8646 | /* Remeber to terminate the string. */ |
8647 | lang_add_data (BYTE, exp_intop (0)); | |
0d79a2a8 US |
8648 | } |
8649 | ||
252b5132 RH |
8650 | /* Create a new reloc statement. RELOC is the BFD relocation type to |
8651 | generate. HOWTO is the corresponding howto structure (we could | |
8652 | look this up, but the caller has already done so). SECTION is the | |
8653 | section to generate a reloc against, or NAME is the name of the | |
8654 | symbol to generate a reloc against. Exactly one of SECTION and | |
8655 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8656 | ||
8657 | void | |
1579bae1 AM |
8658 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
8659 | reloc_howto_type *howto, | |
8660 | asection *section, | |
8661 | const char *name, | |
8662 | union etree_union *addend) | |
252b5132 RH |
8663 | { |
8664 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8665 | |
252b5132 RH |
8666 | p->reloc = reloc; |
8667 | p->howto = howto; | |
8668 | p->section = section; | |
8669 | p->name = name; | |
8670 | p->addend_exp = addend; | |
8671 | ||
8672 | p->addend_value = 0; | |
8673 | p->output_section = NULL; | |
7fabd029 | 8674 | p->output_offset = 0; |
252b5132 RH |
8675 | } |
8676 | ||
8677 | lang_assignment_statement_type * | |
1579bae1 | 8678 | lang_add_assignment (etree_type *exp) |
252b5132 | 8679 | { |
d3ce72d0 | 8680 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8681 | |
d3ce72d0 NC |
8682 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8683 | new_stmt->exp = exp; | |
8684 | return new_stmt; | |
252b5132 RH |
8685 | } |
8686 | ||
8687 | void | |
1579bae1 | 8688 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8689 | { |
bd4d42c1 | 8690 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8691 | } |
8692 | ||
8693 | void | |
1579bae1 | 8694 | lang_startup (const char *name) |
252b5132 | 8695 | { |
1e915804 | 8696 | if (first_file->filename != NULL) |
252b5132 | 8697 | { |
df5f2391 | 8698 | einfo (_("%F%P: multiple STARTUP files\n")); |
252b5132 RH |
8699 | } |
8700 | first_file->filename = name; | |
8701 | first_file->local_sym_name = name; | |
f38a2680 | 8702 | first_file->flags.real = true; |
252b5132 RH |
8703 | } |
8704 | ||
8705 | void | |
f38a2680 | 8706 | lang_float (bool maybe) |
252b5132 RH |
8707 | { |
8708 | lang_float_flag = maybe; | |
8709 | } | |
8710 | ||
ee3cc2e2 RS |
8711 | |
8712 | /* Work out the load- and run-time regions from a script statement, and | |
8713 | store them in *LMA_REGION and *REGION respectively. | |
8714 | ||
a747ee4d NC |
8715 | MEMSPEC is the name of the run-time region, or the value of |
8716 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8717 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8718 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8719 | had an explicit load address. | |
ee3cc2e2 RS |
8720 | |
8721 | It is an error to specify both a load region and a load address. */ | |
8722 | ||
8723 | static void | |
6bdafbeb NC |
8724 | lang_get_regions (lang_memory_region_type **region, |
8725 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8726 | const char *memspec, |
8727 | const char *lma_memspec, | |
f38a2680 AM |
8728 | bool have_lma, |
8729 | bool have_vma) | |
ee3cc2e2 | 8730 | { |
f38a2680 | 8731 | *lma_region = lang_memory_region_lookup (lma_memspec, false); |
ee3cc2e2 | 8732 | |
6feb9908 AM |
8733 | /* If no runtime region or VMA has been specified, but the load region |
8734 | has been specified, then use the load region for the runtime region | |
8735 | as well. */ | |
6bdafbeb | 8736 | if (lma_memspec != NULL |
0aa7f586 | 8737 | && !have_vma |
6bdafbeb | 8738 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
8739 | *region = *lma_region; |
8740 | else | |
f38a2680 | 8741 | *region = lang_memory_region_lookup (memspec, false); |
ee3cc2e2 | 8742 | |
6bdafbeb | 8743 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 8744 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 8745 | NULL); |
ee3cc2e2 RS |
8746 | } |
8747 | ||
252b5132 | 8748 | void |
6bdafbeb NC |
8749 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
8750 | lang_output_section_phdr_list *phdrs, | |
8751 | const char *lma_memspec) | |
252b5132 | 8752 | { |
ee3cc2e2 RS |
8753 | lang_get_regions (¤t_section->region, |
8754 | ¤t_section->lma_region, | |
8755 | memspec, lma_memspec, | |
6bdafbeb NC |
8756 | current_section->load_base != NULL, |
8757 | current_section->addr_tree != NULL); | |
113e6845 | 8758 | |
252b5132 | 8759 | current_section->fill = fill; |
252b5132 | 8760 | current_section->phdrs = phdrs; |
bde18da4 | 8761 | pop_stat_ptr (); |
252b5132 RH |
8762 | } |
8763 | ||
252b5132 RH |
8764 | /* Set the output format type. -oformat overrides scripts. */ |
8765 | ||
8766 | void | |
1579bae1 AM |
8767 | lang_add_output_format (const char *format, |
8768 | const char *big, | |
8769 | const char *little, | |
8770 | int from_script) | |
252b5132 RH |
8771 | { |
8772 | if (output_target == NULL || !from_script) | |
8773 | { | |
8774 | if (command_line.endian == ENDIAN_BIG | |
8775 | && big != NULL) | |
8776 | format = big; | |
8777 | else if (command_line.endian == ENDIAN_LITTLE | |
8778 | && little != NULL) | |
8779 | format = little; | |
8780 | ||
8781 | output_target = format; | |
8782 | } | |
8783 | } | |
8784 | ||
53d25da6 AM |
8785 | void |
8786 | lang_add_insert (const char *where, int is_before) | |
8787 | { | |
d3ce72d0 | 8788 | lang_insert_statement_type *new_stmt; |
53d25da6 | 8789 | |
d3ce72d0 NC |
8790 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
8791 | new_stmt->where = where; | |
8792 | new_stmt->is_before = is_before; | |
53d25da6 AM |
8793 | saved_script_handle = previous_script_handle; |
8794 | } | |
8795 | ||
252b5132 RH |
8796 | /* Enter a group. This creates a new lang_group_statement, and sets |
8797 | stat_ptr to build new statements within the group. */ | |
8798 | ||
8799 | void | |
1579bae1 | 8800 | lang_enter_group (void) |
252b5132 RH |
8801 | { |
8802 | lang_group_statement_type *g; | |
8803 | ||
8804 | g = new_stat (lang_group_statement, stat_ptr); | |
8805 | lang_list_init (&g->children); | |
bde18da4 | 8806 | push_stat_ptr (&g->children); |
252b5132 RH |
8807 | } |
8808 | ||
8809 | /* Leave a group. This just resets stat_ptr to start writing to the | |
8810 | regular list of statements again. Note that this will not work if | |
8811 | groups can occur inside anything else which can adjust stat_ptr, | |
8812 | but currently they can't. */ | |
8813 | ||
8814 | void | |
1579bae1 | 8815 | lang_leave_group (void) |
252b5132 | 8816 | { |
bde18da4 | 8817 | pop_stat_ptr (); |
252b5132 RH |
8818 | } |
8819 | ||
8820 | /* Add a new program header. This is called for each entry in a PHDRS | |
8821 | command in a linker script. */ | |
8822 | ||
8823 | void | |
1579bae1 AM |
8824 | lang_new_phdr (const char *name, |
8825 | etree_type *type, | |
f38a2680 AM |
8826 | bool filehdr, |
8827 | bool phdrs, | |
1579bae1 AM |
8828 | etree_type *at, |
8829 | etree_type *flags) | |
252b5132 RH |
8830 | { |
8831 | struct lang_phdr *n, **pp; | |
f38a2680 | 8832 | bool hdrs; |
252b5132 | 8833 | |
988de25b | 8834 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
8835 | n->next = NULL; |
8836 | n->name = name; | |
3d9c8f6b | 8837 | n->type = exp_get_vma (type, 0, "program header type"); |
252b5132 RH |
8838 | n->filehdr = filehdr; |
8839 | n->phdrs = phdrs; | |
8840 | n->at = at; | |
8841 | n->flags = flags; | |
1e0061d2 | 8842 | |
5c1a3f0f | 8843 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
8844 | |
8845 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
8846 | if (hdrs |
8847 | && (*pp)->type == 1 | |
8848 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 8849 | { |
c1c8c1ef | 8850 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 8851 | " when prior PT_LOAD headers lack them\n"), NULL); |
f38a2680 | 8852 | hdrs = false; |
5c1a3f0f NS |
8853 | } |
8854 | ||
252b5132 RH |
8855 | *pp = n; |
8856 | } | |
8857 | ||
8858 | /* Record the program header information in the output BFD. FIXME: We | |
8859 | should not be calling an ELF specific function here. */ | |
8860 | ||
8861 | static void | |
1579bae1 | 8862 | lang_record_phdrs (void) |
252b5132 RH |
8863 | { |
8864 | unsigned int alc; | |
8865 | asection **secs; | |
6bdafbeb | 8866 | lang_output_section_phdr_list *last; |
252b5132 | 8867 | struct lang_phdr *l; |
afd7a018 | 8868 | lang_output_section_statement_type *os; |
252b5132 RH |
8869 | |
8870 | alc = 10; | |
1e9cc1c2 | 8871 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 8872 | last = NULL; |
591a748a | 8873 | |
252b5132 RH |
8874 | for (l = lang_phdr_list; l != NULL; l = l->next) |
8875 | { | |
8876 | unsigned int c; | |
8877 | flagword flags; | |
8878 | bfd_vma at; | |
8879 | ||
8880 | c = 0; | |
8ce18f9c | 8881 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8882 | os != NULL; |
8883 | os = os->next) | |
252b5132 | 8884 | { |
6bdafbeb | 8885 | lang_output_section_phdr_list *pl; |
252b5132 | 8886 | |
66c103b7 | 8887 | if (os->constraint < 0) |
0841712e | 8888 | continue; |
252b5132 RH |
8889 | |
8890 | pl = os->phdrs; | |
8891 | if (pl != NULL) | |
8892 | last = pl; | |
8893 | else | |
8894 | { | |
8895 | if (os->sectype == noload_section | |
8896 | || os->bfd_section == NULL | |
8897 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
8898 | continue; | |
591a748a | 8899 | |
7512c397 AM |
8900 | /* Don't add orphans to PT_INTERP header. */ |
8901 | if (l->type == 3) | |
8902 | continue; | |
8903 | ||
e9442572 | 8904 | if (last == NULL) |
591a748a | 8905 | { |
0aa7f586 | 8906 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
8907 | |
8908 | /* If we have not run across a section with a program | |
8909 | header assigned to it yet, then scan forwards to find | |
8910 | one. This prevents inconsistencies in the linker's | |
8911 | behaviour when a script has specified just a single | |
8912 | header and there are sections in that script which are | |
8913 | not assigned to it, and which occur before the first | |
8914 | use of that header. See here for more details: | |
8915 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
8916 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
8917 | if (tmp_os->phdrs) | |
e9442572 AM |
8918 | { |
8919 | last = tmp_os->phdrs; | |
8920 | break; | |
8921 | } | |
8922 | if (last == NULL) | |
8923 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 8924 | } |
e9442572 | 8925 | pl = last; |
252b5132 RH |
8926 | } |
8927 | ||
8928 | if (os->bfd_section == NULL) | |
8929 | continue; | |
8930 | ||
8931 | for (; pl != NULL; pl = pl->next) | |
8932 | { | |
8933 | if (strcmp (pl->name, l->name) == 0) | |
8934 | { | |
8935 | if (c >= alc) | |
8936 | { | |
8937 | alc *= 2; | |
1e9cc1c2 | 8938 | secs = (asection **) xrealloc (secs, |
4724d37e | 8939 | alc * sizeof (asection *)); |
252b5132 RH |
8940 | } |
8941 | secs[c] = os->bfd_section; | |
8942 | ++c; | |
f38a2680 | 8943 | pl->used = true; |
252b5132 RH |
8944 | } |
8945 | } | |
8946 | } | |
8947 | ||
8948 | if (l->flags == NULL) | |
8949 | flags = 0; | |
8950 | else | |
e9ee469a | 8951 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
8952 | |
8953 | if (l->at == NULL) | |
8954 | at = 0; | |
8955 | else | |
e9ee469a | 8956 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 8957 | |
0aa7f586 AM |
8958 | if (!bfd_record_phdr (link_info.output_bfd, l->type, |
8959 | l->flags != NULL, flags, l->at != NULL, | |
8960 | at, l->filehdr, l->phdrs, c, secs)) | |
252b5132 RH |
8961 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
8962 | } | |
8963 | ||
8964 | free (secs); | |
8965 | ||
8966 | /* Make sure all the phdr assignments succeeded. */ | |
8ce18f9c | 8967 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8968 | os != NULL; |
8969 | os = os->next) | |
252b5132 | 8970 | { |
6bdafbeb | 8971 | lang_output_section_phdr_list *pl; |
252b5132 | 8972 | |
66c103b7 | 8973 | if (os->constraint < 0 |
afd7a018 | 8974 | || os->bfd_section == NULL) |
252b5132 RH |
8975 | continue; |
8976 | ||
afd7a018 | 8977 | for (pl = os->phdrs; |
252b5132 RH |
8978 | pl != NULL; |
8979 | pl = pl->next) | |
0aa7f586 | 8980 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 8981 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 8982 | os->name, pl->name); |
252b5132 RH |
8983 | } |
8984 | } | |
8985 | ||
8986 | /* Record a list of sections which may not be cross referenced. */ | |
8987 | ||
8988 | void | |
6bdafbeb | 8989 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
8990 | { |
8991 | struct lang_nocrossrefs *n; | |
8992 | ||
1e9cc1c2 | 8993 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
252b5132 RH |
8994 | n->next = nocrossref_list; |
8995 | n->list = l; | |
f38a2680 | 8996 | n->onlyfirst = false; |
252b5132 RH |
8997 | nocrossref_list = n; |
8998 | ||
8999 | /* Set notice_all so that we get informed about all symbols. */ | |
f38a2680 | 9000 | link_info.notice_all = true; |
252b5132 | 9001 | } |
cdf96953 MF |
9002 | |
9003 | /* Record a section that cannot be referenced from a list of sections. */ | |
9004 | ||
9005 | void | |
9006 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
9007 | { | |
9008 | lang_add_nocrossref (l); | |
f38a2680 | 9009 | nocrossref_list->onlyfirst = true; |
cdf96953 | 9010 | } |
252b5132 RH |
9011 | \f |
9012 | /* Overlay handling. We handle overlays with some static variables. */ | |
9013 | ||
9014 | /* The overlay virtual address. */ | |
9015 | static etree_type *overlay_vma; | |
7e7d5768 AM |
9016 | /* And subsection alignment. */ |
9017 | static etree_type *overlay_subalign; | |
252b5132 | 9018 | |
252b5132 RH |
9019 | /* An expression for the maximum section size seen so far. */ |
9020 | static etree_type *overlay_max; | |
9021 | ||
9022 | /* A list of all the sections in this overlay. */ | |
9023 | ||
89cdebba | 9024 | struct overlay_list { |
252b5132 RH |
9025 | struct overlay_list *next; |
9026 | lang_output_section_statement_type *os; | |
9027 | }; | |
9028 | ||
9029 | static struct overlay_list *overlay_list; | |
9030 | ||
9031 | /* Start handling an overlay. */ | |
9032 | ||
9033 | void | |
7e7d5768 | 9034 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
9035 | { |
9036 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
9037 | ASSERT (overlay_vma == NULL |
9038 | && overlay_subalign == NULL | |
9039 | && overlay_max == NULL); | |
252b5132 RH |
9040 | |
9041 | overlay_vma = vma_expr; | |
7e7d5768 | 9042 | overlay_subalign = subalign; |
252b5132 RH |
9043 | } |
9044 | ||
9045 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
9046 | lang_enter_output_section_statement with the correct VMA. |
9047 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
9048 | |
9049 | void | |
1579bae1 | 9050 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
9051 | { |
9052 | struct overlay_list *n; | |
9053 | etree_type *size; | |
9054 | ||
152d792f | 9055 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
c212f39d | 9056 | 0, 0, overlay_subalign, 0, 0, 0); |
252b5132 | 9057 | |
9f88b410 | 9058 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
9059 | sections on this one. This will work correctly even if `.' is |
9060 | used in the addresses. */ | |
9061 | if (overlay_list == NULL) | |
9f88b410 | 9062 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
9063 | |
9064 | /* Remember the section. */ | |
1e9cc1c2 | 9065 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
9066 | n->os = current_section; |
9067 | n->next = overlay_list; | |
9068 | overlay_list = n; | |
9069 | ||
9070 | size = exp_nameop (SIZEOF, name); | |
9071 | ||
252b5132 RH |
9072 | /* Arrange to work out the maximum section end address. */ |
9073 | if (overlay_max == NULL) | |
9074 | overlay_max = size; | |
9075 | else | |
9076 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
9077 | } | |
9078 | ||
9079 | /* Finish a section in an overlay. There isn't any special to do | |
9080 | here. */ | |
9081 | ||
9082 | void | |
1579bae1 | 9083 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 9084 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
9085 | { |
9086 | const char *name; | |
9087 | char *clean, *s2; | |
9088 | const char *s1; | |
9089 | char *buf; | |
9090 | ||
9091 | name = current_section->name; | |
9092 | ||
a747ee4d NC |
9093 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
9094 | region and that no load-time region has been specified. It doesn't | |
9095 | really matter what we say here, since lang_leave_overlay will | |
9096 | override it. */ | |
9097 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
9098 | |
9099 | /* Define the magic symbols. */ | |
9100 | ||
1e9cc1c2 | 9101 | clean = (char *) xmalloc (strlen (name) + 1); |
252b5132 RH |
9102 | s2 = clean; |
9103 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 9104 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
9105 | *s2++ = *s1; |
9106 | *s2 = '\0'; | |
9107 | ||
1e9cc1c2 | 9108 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
252b5132 | 9109 | sprintf (buf, "__load_start_%s", clean); |
34711ca3 AM |
9110 | lang_add_assignment (exp_provide (buf, |
9111 | exp_nameop (LOADADDR, name), | |
f38a2680 | 9112 | false)); |
252b5132 | 9113 | |
1e9cc1c2 | 9114 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
252b5132 | 9115 | sprintf (buf, "__load_stop_%s", clean); |
34711ca3 AM |
9116 | lang_add_assignment (exp_provide (buf, |
9117 | exp_binop ('+', | |
9118 | exp_nameop (LOADADDR, name), | |
9119 | exp_nameop (SIZEOF, name)), | |
f38a2680 | 9120 | false)); |
252b5132 RH |
9121 | |
9122 | free (clean); | |
9123 | } | |
9124 | ||
9125 | /* Finish an overlay. If there are any overlay wide settings, this | |
9126 | looks through all the sections in the overlay and sets them. */ | |
9127 | ||
9128 | void | |
1579bae1 AM |
9129 | lang_leave_overlay (etree_type *lma_expr, |
9130 | int nocrossrefs, | |
9131 | fill_type *fill, | |
9132 | const char *memspec, | |
6bdafbeb | 9133 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 9134 | const char *lma_memspec) |
252b5132 RH |
9135 | { |
9136 | lang_memory_region_type *region; | |
562d3460 | 9137 | lang_memory_region_type *lma_region; |
252b5132 | 9138 | struct overlay_list *l; |
6bdafbeb | 9139 | lang_nocrossref_type *nocrossref; |
252b5132 | 9140 | |
ee3cc2e2 RS |
9141 | lang_get_regions (®ion, &lma_region, |
9142 | memspec, lma_memspec, | |
f38a2680 | 9143 | lma_expr != NULL, false); |
562d3460 | 9144 | |
252b5132 RH |
9145 | nocrossref = NULL; |
9146 | ||
9f88b410 RS |
9147 | /* After setting the size of the last section, set '.' to end of the |
9148 | overlay region. */ | |
9149 | if (overlay_list != NULL) | |
6d8bf25d AM |
9150 | { |
9151 | overlay_list->os->update_dot = 1; | |
9152 | overlay_list->os->update_dot_tree | |
f38a2680 | 9153 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); |
6d8bf25d | 9154 | } |
9f88b410 | 9155 | |
252b5132 RH |
9156 | l = overlay_list; |
9157 | while (l != NULL) | |
9158 | { | |
9159 | struct overlay_list *next; | |
9160 | ||
1579bae1 | 9161 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 9162 | l->os->fill = fill; |
1545243b | 9163 | |
9f88b410 RS |
9164 | l->os->region = region; |
9165 | l->os->lma_region = lma_region; | |
9166 | ||
9167 | /* The first section has the load address specified in the | |
9168 | OVERLAY statement. The rest are worked out from that. | |
9169 | The base address is not needed (and should be null) if | |
9170 | an LMA region was specified. */ | |
9171 | if (l->next == 0) | |
152d792f AM |
9172 | { |
9173 | l->os->load_base = lma_expr; | |
7b243801 | 9174 | l->os->sectype = first_overlay_section; |
152d792f | 9175 | } |
252b5132 RH |
9176 | if (phdrs != NULL && l->os->phdrs == NULL) |
9177 | l->os->phdrs = phdrs; | |
9178 | ||
9f88b410 | 9179 | if (nocrossrefs) |
252b5132 | 9180 | { |
6bdafbeb | 9181 | lang_nocrossref_type *nc; |
252b5132 | 9182 | |
1e9cc1c2 | 9183 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
252b5132 RH |
9184 | nc->name = l->os->name; |
9185 | nc->next = nocrossref; | |
9186 | nocrossref = nc; | |
9187 | } | |
9188 | ||
9189 | next = l->next; | |
9190 | free (l); | |
9191 | l = next; | |
9192 | } | |
9193 | ||
9194 | if (nocrossref != NULL) | |
9195 | lang_add_nocrossref (nocrossref); | |
9196 | ||
252b5132 | 9197 | overlay_vma = NULL; |
252b5132 RH |
9198 | overlay_list = NULL; |
9199 | overlay_max = NULL; | |
8cb1232a | 9200 | overlay_subalign = NULL; |
252b5132 RH |
9201 | } |
9202 | \f | |
9203 | /* Version handling. This is only useful for ELF. */ | |
9204 | ||
108ba305 JJ |
9205 | /* If PREV is NULL, return first version pattern matching particular symbol. |
9206 | If PREV is non-NULL, return first version pattern matching particular | |
9207 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 9208 | |
108ba305 JJ |
9209 | static struct bfd_elf_version_expr * |
9210 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
9211 | struct bfd_elf_version_expr *prev, | |
9212 | const char *sym) | |
252b5132 | 9213 | { |
93252b1c | 9214 | const char *c_sym; |
108ba305 JJ |
9215 | const char *cxx_sym = sym; |
9216 | const char *java_sym = sym; | |
9217 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
9218 | enum demangling_styles curr_style; |
9219 | ||
9220 | curr_style = CURRENT_DEMANGLING_STYLE; | |
9221 | cplus_demangle_set_style (no_demangling); | |
9222 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
9223 | if (!c_sym) | |
9224 | c_sym = sym; | |
9225 | cplus_demangle_set_style (curr_style); | |
252b5132 | 9226 | |
108ba305 | 9227 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 9228 | { |
93252b1c MF |
9229 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
9230 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
9231 | if (!cxx_sym) |
9232 | cxx_sym = sym; | |
252b5132 | 9233 | } |
df816215 | 9234 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 9235 | { |
93252b1c | 9236 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
9237 | if (!java_sym) |
9238 | java_sym = sym; | |
252b5132 RH |
9239 | } |
9240 | ||
ae5a3597 | 9241 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 9242 | { |
108ba305 JJ |
9243 | struct bfd_elf_version_expr e; |
9244 | ||
9245 | switch (prev ? prev->mask : 0) | |
9246 | { | |
967928e9 AM |
9247 | case 0: |
9248 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
9249 | { | |
93252b1c | 9250 | e.pattern = c_sym; |
1e9cc1c2 | 9251 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9252 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 9253 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
9254 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
9255 | goto out_ret; | |
9256 | else | |
9257 | expr = expr->next; | |
9258 | } | |
9259 | /* Fallthrough */ | |
9260 | case BFD_ELF_VERSION_C_TYPE: | |
9261 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
9262 | { | |
ae5a3597 | 9263 | e.pattern = cxx_sym; |
1e9cc1c2 | 9264 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9265 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9266 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
9267 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
9268 | goto out_ret; | |
9269 | else | |
9270 | expr = expr->next; | |
9271 | } | |
9272 | /* Fallthrough */ | |
9273 | case BFD_ELF_VERSION_CXX_TYPE: | |
9274 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
9275 | { | |
ae5a3597 | 9276 | e.pattern = java_sym; |
1e9cc1c2 | 9277 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9278 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9279 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
9280 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
9281 | goto out_ret; | |
9282 | else | |
9283 | expr = expr->next; | |
9284 | } | |
9285 | /* Fallthrough */ | |
9286 | default: | |
9287 | break; | |
108ba305 | 9288 | } |
252b5132 | 9289 | } |
108ba305 JJ |
9290 | |
9291 | /* Finally, try the wildcards. */ | |
ae5a3597 | 9292 | if (prev == NULL || prev->literal) |
108ba305 | 9293 | expr = head->remaining; |
252b5132 | 9294 | else |
108ba305 | 9295 | expr = prev->next; |
86043bbb | 9296 | for (; expr; expr = expr->next) |
252b5132 | 9297 | { |
108ba305 JJ |
9298 | const char *s; |
9299 | ||
86043bbb MM |
9300 | if (!expr->pattern) |
9301 | continue; | |
9302 | ||
108ba305 JJ |
9303 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
9304 | break; | |
9305 | ||
9306 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
9307 | s = java_sym; | |
9308 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
9309 | s = cxx_sym; | |
9310 | else | |
93252b1c | 9311 | s = c_sym; |
5e35cbc2 | 9312 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 9313 | break; |
252b5132 RH |
9314 | } |
9315 | ||
967928e9 | 9316 | out_ret: |
93252b1c MF |
9317 | if (c_sym != sym) |
9318 | free ((char *) c_sym); | |
108ba305 JJ |
9319 | if (cxx_sym != sym) |
9320 | free ((char *) cxx_sym); | |
9321 | if (java_sym != sym) | |
9322 | free ((char *) java_sym); | |
9323 | return expr; | |
252b5132 RH |
9324 | } |
9325 | ||
5e35cbc2 | 9326 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 9327 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
9328 | |
9329 | static const char * | |
9330 | realsymbol (const char *pattern) | |
9331 | { | |
9332 | const char *p; | |
f38a2680 | 9333 | bool changed = false, backslash = false; |
1e9cc1c2 | 9334 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
9335 | |
9336 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
9337 | { | |
9338 | /* It is a glob pattern only if there is no preceding | |
9339 | backslash. */ | |
5e35cbc2 L |
9340 | if (backslash) |
9341 | { | |
9342 | /* Remove the preceding backslash. */ | |
9343 | *(s - 1) = *p; | |
f38a2680 AM |
9344 | backslash = false; |
9345 | changed = true; | |
5e35cbc2 L |
9346 | } |
9347 | else | |
ae5a3597 AM |
9348 | { |
9349 | if (*p == '?' || *p == '*' || *p == '[') | |
9350 | { | |
9351 | free (symbol); | |
9352 | return NULL; | |
9353 | } | |
5e35cbc2 | 9354 | |
ae5a3597 AM |
9355 | *s++ = *p; |
9356 | backslash = *p == '\\'; | |
9357 | } | |
5e35cbc2 L |
9358 | } |
9359 | ||
9360 | if (changed) | |
9361 | { | |
9362 | *s = '\0'; | |
9363 | return symbol; | |
9364 | } | |
9365 | else | |
9366 | { | |
9367 | free (symbol); | |
9368 | return pattern; | |
9369 | } | |
9370 | } | |
9371 | ||
d3ce72d0 | 9372 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
9373 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
9374 | pattern to be matched against symbol names. */ | |
252b5132 RH |
9375 | |
9376 | struct bfd_elf_version_expr * | |
1579bae1 | 9377 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 9378 | const char *new_name, |
86043bbb | 9379 | const char *lang, |
f38a2680 | 9380 | bool literal_p) |
252b5132 RH |
9381 | { |
9382 | struct bfd_elf_version_expr *ret; | |
9383 | ||
d3ce72d0 | 9384 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
252b5132 | 9385 | ret->next = orig; |
31941635 L |
9386 | ret->symver = 0; |
9387 | ret->script = 0; | |
f38a2680 | 9388 | ret->literal = true; |
d3ce72d0 | 9389 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
9390 | if (ret->pattern == NULL) |
9391 | { | |
d3ce72d0 | 9392 | ret->pattern = new_name; |
f38a2680 | 9393 | ret->literal = false; |
ae5a3597 | 9394 | } |
252b5132 RH |
9395 | |
9396 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 9397 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 9398 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 9399 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 9400 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 9401 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
9402 | else |
9403 | { | |
9404 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
9405 | lang); | |
108ba305 | 9406 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
9407 | } |
9408 | ||
fac1652d | 9409 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
9410 | } |
9411 | ||
9412 | /* This is called for each set of variable names and match | |
9413 | expressions. */ | |
9414 | ||
9415 | struct bfd_elf_version_tree * | |
1579bae1 AM |
9416 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
9417 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
9418 | { |
9419 | struct bfd_elf_version_tree *ret; | |
9420 | ||
1e9cc1c2 | 9421 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
108ba305 JJ |
9422 | ret->globals.list = globals; |
9423 | ret->locals.list = locals; | |
9424 | ret->match = lang_vers_match; | |
252b5132 | 9425 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
9426 | return ret; |
9427 | } | |
9428 | ||
9429 | /* This static variable keeps track of version indices. */ | |
9430 | ||
9431 | static int version_index; | |
9432 | ||
108ba305 JJ |
9433 | static hashval_t |
9434 | version_expr_head_hash (const void *p) | |
9435 | { | |
1e9cc1c2 NC |
9436 | const struct bfd_elf_version_expr *e = |
9437 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 9438 | |
ae5a3597 | 9439 | return htab_hash_string (e->pattern); |
108ba305 JJ |
9440 | } |
9441 | ||
9442 | static int | |
9443 | version_expr_head_eq (const void *p1, const void *p2) | |
9444 | { | |
1e9cc1c2 NC |
9445 | const struct bfd_elf_version_expr *e1 = |
9446 | (const struct bfd_elf_version_expr *) p1; | |
9447 | const struct bfd_elf_version_expr *e2 = | |
9448 | (const struct bfd_elf_version_expr *) p2; | |
108ba305 | 9449 | |
ae5a3597 | 9450 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
9451 | } |
9452 | ||
9453 | static void | |
9454 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
9455 | { | |
9456 | size_t count = 0; | |
9457 | struct bfd_elf_version_expr *e, *next; | |
9458 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
9459 | ||
9460 | for (e = head->list; e; e = e->next) | |
9461 | { | |
ae5a3597 | 9462 | if (e->literal) |
108ba305 JJ |
9463 | count++; |
9464 | head->mask |= e->mask; | |
9465 | } | |
9466 | ||
9467 | if (count) | |
9468 | { | |
9469 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
9470 | version_expr_head_eq, NULL); | |
9471 | list_loc = &head->list; | |
9472 | remaining_loc = &head->remaining; | |
9473 | for (e = head->list; e; e = next) | |
9474 | { | |
9475 | next = e->next; | |
ae5a3597 | 9476 | if (!e->literal) |
108ba305 JJ |
9477 | { |
9478 | *remaining_loc = e; | |
9479 | remaining_loc = &e->next; | |
9480 | } | |
9481 | else | |
9482 | { | |
1e9cc1c2 | 9483 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
9484 | |
9485 | if (*loc) | |
9486 | { | |
9487 | struct bfd_elf_version_expr *e1, *last; | |
9488 | ||
1e9cc1c2 | 9489 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
9490 | last = NULL; |
9491 | do | |
9492 | { | |
9493 | if (e1->mask == e->mask) | |
9494 | { | |
9495 | last = NULL; | |
9496 | break; | |
9497 | } | |
9498 | last = e1; | |
9499 | e1 = e1->next; | |
9500 | } | |
ae5a3597 | 9501 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
9502 | |
9503 | if (last == NULL) | |
9504 | { | |
9505 | /* This is a duplicate. */ | |
9506 | /* FIXME: Memory leak. Sometimes pattern is not | |
9507 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 9508 | /* free (e->pattern); */ |
108ba305 JJ |
9509 | free (e); |
9510 | } | |
9511 | else | |
9512 | { | |
9513 | e->next = last->next; | |
9514 | last->next = e; | |
9515 | } | |
9516 | } | |
9517 | else | |
9518 | { | |
9519 | *loc = e; | |
9520 | *list_loc = e; | |
9521 | list_loc = &e->next; | |
9522 | } | |
9523 | } | |
9524 | } | |
9525 | *remaining_loc = NULL; | |
9526 | *list_loc = head->remaining; | |
9527 | } | |
9528 | else | |
9529 | head->remaining = head->list; | |
9530 | } | |
9531 | ||
252b5132 RH |
9532 | /* This is called when we know the name and dependencies of the |
9533 | version. */ | |
9534 | ||
9535 | void | |
1579bae1 AM |
9536 | lang_register_vers_node (const char *name, |
9537 | struct bfd_elf_version_tree *version, | |
9538 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
9539 | { |
9540 | struct bfd_elf_version_tree *t, **pp; | |
9541 | struct bfd_elf_version_expr *e1; | |
9542 | ||
6b9b879a JJ |
9543 | if (name == NULL) |
9544 | name = ""; | |
9545 | ||
fd91d419 L |
9546 | if (link_info.version_info != NULL |
9547 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 9548 | { |
6feb9908 AM |
9549 | einfo (_("%X%P: anonymous version tag cannot be combined" |
9550 | " with other version tags\n")); | |
5ed6aba4 | 9551 | free (version); |
6b9b879a JJ |
9552 | return; |
9553 | } | |
9554 | ||
252b5132 | 9555 | /* Make sure this node has a unique name. */ |
fd91d419 | 9556 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9557 | if (strcmp (t->name, name) == 0) |
9558 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9559 | ||
108ba305 JJ |
9560 | lang_finalize_version_expr_head (&version->globals); |
9561 | lang_finalize_version_expr_head (&version->locals); | |
9562 | ||
252b5132 RH |
9563 | /* Check the global and local match names, and make sure there |
9564 | aren't any duplicates. */ | |
9565 | ||
108ba305 | 9566 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9567 | { |
fd91d419 | 9568 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9569 | { |
9570 | struct bfd_elf_version_expr *e2; | |
9571 | ||
ae5a3597 | 9572 | if (t->locals.htab && e1->literal) |
108ba305 | 9573 | { |
1e9cc1c2 | 9574 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9575 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 9576 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9577 | { |
9578 | if (e1->mask == e2->mask) | |
6feb9908 | 9579 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 9580 | " in version information\n"), e1->pattern); |
108ba305 JJ |
9581 | e2 = e2->next; |
9582 | } | |
9583 | } | |
ae5a3597 | 9584 | else if (!e1->literal) |
108ba305 | 9585 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9586 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9587 | && e1->mask == e2->mask) | |
9588 | einfo (_("%X%P: duplicate expression `%s'" | |
9589 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9590 | } |
9591 | } | |
9592 | ||
108ba305 | 9593 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9594 | { |
fd91d419 | 9595 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9596 | { |
9597 | struct bfd_elf_version_expr *e2; | |
9598 | ||
ae5a3597 | 9599 | if (t->globals.htab && e1->literal) |
108ba305 | 9600 | { |
1e9cc1c2 | 9601 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9602 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9603 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9604 | { |
9605 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9606 | einfo (_("%X%P: duplicate expression `%s'" |
9607 | " in version information\n"), | |
ae5a3597 | 9608 | e1->pattern); |
108ba305 JJ |
9609 | e2 = e2->next; |
9610 | } | |
9611 | } | |
ae5a3597 | 9612 | else if (!e1->literal) |
108ba305 | 9613 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9614 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9615 | && e1->mask == e2->mask) | |
9616 | einfo (_("%X%P: duplicate expression `%s'" | |
9617 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9618 | } |
9619 | } | |
9620 | ||
9621 | version->deps = deps; | |
9622 | version->name = name; | |
6b9b879a JJ |
9623 | if (name[0] != '\0') |
9624 | { | |
9625 | ++version_index; | |
9626 | version->vernum = version_index; | |
9627 | } | |
9628 | else | |
9629 | version->vernum = 0; | |
252b5132 | 9630 | |
fd91d419 | 9631 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9632 | ; |
9633 | *pp = version; | |
9634 | } | |
9635 | ||
9636 | /* This is called when we see a version dependency. */ | |
9637 | ||
9638 | struct bfd_elf_version_deps * | |
1579bae1 | 9639 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9640 | { |
9641 | struct bfd_elf_version_deps *ret; | |
9642 | struct bfd_elf_version_tree *t; | |
9643 | ||
1e9cc1c2 | 9644 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
252b5132 RH |
9645 | ret->next = list; |
9646 | ||
fd91d419 | 9647 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9648 | { |
9649 | if (strcmp (t->name, name) == 0) | |
9650 | { | |
9651 | ret->version_needed = t; | |
9652 | return ret; | |
9653 | } | |
9654 | } | |
9655 | ||
9656 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9657 | ||
1e0061d2 | 9658 | ret->version_needed = NULL; |
252b5132 RH |
9659 | return ret; |
9660 | } | |
9661 | ||
9662 | static void | |
1579bae1 | 9663 | lang_do_version_exports_section (void) |
252b5132 RH |
9664 | { |
9665 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9666 | ||
9667 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9668 | { | |
9669 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9670 | char *contents, *p; | |
9671 | bfd_size_type len; | |
9672 | ||
9673 | if (sec == NULL) | |
b7a26f91 | 9674 | continue; |
252b5132 | 9675 | |
eea6121a | 9676 | len = sec->size; |
1e9cc1c2 | 9677 | contents = (char *) xmalloc (len); |
252b5132 | 9678 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9679 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9680 | |
9681 | p = contents; | |
89cdebba | 9682 | while (p < contents + len) |
252b5132 | 9683 | { |
f38a2680 | 9684 | greg = lang_new_vers_pattern (greg, p, NULL, false); |
252b5132 RH |
9685 | p = strchr (p, '\0') + 1; |
9686 | } | |
9687 | ||
9688 | /* Do not free the contents, as we used them creating the regex. */ | |
9689 | ||
9690 | /* Do not include this section in the link. */ | |
a14a5de3 | 9691 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9692 | } |
9693 | ||
f38a2680 | 9694 | lreg = lang_new_vers_pattern (NULL, "*", NULL, false); |
252b5132 RH |
9695 | lang_register_vers_node (command_line.version_exports_section, |
9696 | lang_new_vers_node (greg, lreg), NULL); | |
9697 | } | |
577a0623 | 9698 | |
874ef038 AB |
9699 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially |
9700 | called with UPDATE_REGIONS_P set to FALSE, in this case no errors are | |
9701 | thrown, however, references to symbols in the origin and length fields | |
9702 | will be pushed into the symbol table, this allows PROVIDE statements to | |
9703 | then provide these symbols. This function is called a second time with | |
9704 | UPDATE_REGIONS_P set to TRUE, this time the we update the actual region | |
9705 | data structures, and throw errors if missing symbols are encountered. */ | |
cc9ad334 SKS |
9706 | |
9707 | static void | |
f38a2680 | 9708 | lang_do_memory_regions (bool update_regions_p) |
cc9ad334 SKS |
9709 | { |
9710 | lang_memory_region_type *r = lang_memory_region_list; | |
9711 | ||
9712 | for (; r != NULL; r = r->next) | |
9713 | { | |
9714 | if (r->origin_exp) | |
0aa7f586 AM |
9715 | { |
9716 | exp_fold_tree_no_dot (r->origin_exp); | |
874ef038 AB |
9717 | if (update_regions_p) |
9718 | { | |
9719 | if (expld.result.valid_p) | |
9720 | { | |
9721 | r->origin = expld.result.value; | |
9722 | r->current = r->origin; | |
9723 | } | |
9724 | else | |
9725 | einfo (_("%P: invalid origin for memory region %s\n"), | |
9726 | r->name_list.name); | |
9727 | } | |
0aa7f586 | 9728 | } |
cc9ad334 | 9729 | if (r->length_exp) |
0aa7f586 AM |
9730 | { |
9731 | exp_fold_tree_no_dot (r->length_exp); | |
874ef038 AB |
9732 | if (update_regions_p) |
9733 | { | |
9734 | if (expld.result.valid_p) | |
9735 | r->length = expld.result.value; | |
9736 | else | |
9737 | einfo (_("%P: invalid length for memory region %s\n"), | |
9738 | r->name_list.name); | |
9739 | } | |
9740 | } | |
cc9ad334 SKS |
9741 | } |
9742 | } | |
9743 | ||
577a0623 | 9744 | void |
1579bae1 | 9745 | lang_add_unique (const char *name) |
577a0623 AM |
9746 | { |
9747 | struct unique_sections *ent; | |
9748 | ||
9749 | for (ent = unique_section_list; ent; ent = ent->next) | |
9750 | if (strcmp (ent->name, name) == 0) | |
9751 | return; | |
9752 | ||
1e9cc1c2 | 9753 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
577a0623 AM |
9754 | ent->name = xstrdup (name); |
9755 | ent->next = unique_section_list; | |
9756 | unique_section_list = ent; | |
9757 | } | |
55255dae L |
9758 | |
9759 | /* Append the list of dynamic symbols to the existing one. */ | |
9760 | ||
9761 | void | |
37a141bf FS |
9762 | lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, |
9763 | struct bfd_elf_version_expr *dynamic) | |
55255dae | 9764 | { |
37a141bf | 9765 | if (*list_p) |
55255dae | 9766 | { |
07806542 JJ |
9767 | struct bfd_elf_version_expr *tail; |
9768 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
9769 | ; | |
37a141bf FS |
9770 | tail->next = (*list_p)->head.list; |
9771 | (*list_p)->head.list = dynamic; | |
55255dae L |
9772 | } |
9773 | else | |
9774 | { | |
9775 | struct bfd_elf_dynamic_list *d; | |
9776 | ||
1e9cc1c2 | 9777 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
55255dae L |
9778 | d->head.list = dynamic; |
9779 | d->match = lang_vers_match; | |
37a141bf | 9780 | *list_p = d; |
55255dae L |
9781 | } |
9782 | } | |
9783 | ||
9784 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
9785 | one. */ | |
9786 | ||
9787 | void | |
9788 | lang_append_dynamic_list_cpp_typeinfo (void) | |
9789 | { | |
0aa7f586 | 9790 | const char *symbols[] = |
55255dae L |
9791 | { |
9792 | "typeinfo name for*", | |
9793 | "typeinfo for*" | |
9794 | }; | |
9795 | struct bfd_elf_version_expr *dynamic = NULL; | |
9796 | unsigned int i; | |
9797 | ||
9798 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9799 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9800 | false); |
55255dae | 9801 | |
37a141bf | 9802 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
55255dae | 9803 | } |
40b36307 L |
9804 | |
9805 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
9806 | existing one. */ | |
9807 | ||
9808 | void | |
9809 | lang_append_dynamic_list_cpp_new (void) | |
9810 | { | |
0aa7f586 | 9811 | const char *symbols[] = |
40b36307 L |
9812 | { |
9813 | "operator new*", | |
9814 | "operator delete*" | |
9815 | }; | |
9816 | struct bfd_elf_version_expr *dynamic = NULL; | |
9817 | unsigned int i; | |
9818 | ||
9819 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9820 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9821 | false); |
40b36307 | 9822 | |
37a141bf | 9823 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
40b36307 | 9824 | } |
01554a74 AM |
9825 | |
9826 | /* Scan a space and/or comma separated string of features. */ | |
9827 | ||
9828 | void | |
9829 | lang_ld_feature (char *str) | |
9830 | { | |
9831 | char *p, *q; | |
9832 | ||
9833 | p = str; | |
9834 | while (*p) | |
9835 | { | |
9836 | char sep; | |
9837 | while (*p == ',' || ISSPACE (*p)) | |
9838 | ++p; | |
9839 | if (!*p) | |
9840 | break; | |
9841 | q = p + 1; | |
9842 | while (*q && *q != ',' && !ISSPACE (*q)) | |
9843 | ++q; | |
9844 | sep = *q; | |
9845 | *q = 0; | |
9846 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
f38a2680 | 9847 | config.sane_expr = true; |
01554a74 AM |
9848 | else |
9849 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
9850 | *q = sep; | |
9851 | p = q; | |
9852 | } | |
9853 | } | |
3604cb1f TG |
9854 | |
9855 | /* Pretty print memory amount. */ | |
9856 | ||
9857 | static void | |
b8281767 | 9858 | lang_print_memory_size (uint64_t sz) |
3604cb1f TG |
9859 | { |
9860 | if ((sz & 0x3fffffff) == 0) | |
b8281767 | 9861 | printf ("%10" PRIu64 " GB", sz >> 30); |
3604cb1f | 9862 | else if ((sz & 0xfffff) == 0) |
b8281767 | 9863 | printf ("%10" PRIu64 " MB", sz >> 20); |
3604cb1f | 9864 | else if ((sz & 0x3ff) == 0) |
b8281767 | 9865 | printf ("%10" PRIu64 " KB", sz >> 10); |
3604cb1f | 9866 | else |
b8281767 | 9867 | printf (" %10" PRIu64 " B", sz); |
3604cb1f TG |
9868 | } |
9869 | ||
9870 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
9871 | ||
9872 | void | |
9873 | lang_print_memory_usage (void) | |
9874 | { | |
9875 | lang_memory_region_type *r; | |
9876 | ||
9877 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
9878 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
9879 | { | |
9880 | bfd_vma used_length = r->current - r->origin; | |
3604cb1f TG |
9881 | |
9882 | printf ("%16s: ",r->name_list.name); | |
9883 | lang_print_memory_size (used_length); | |
b8281767 | 9884 | lang_print_memory_size (r->length); |
3604cb1f | 9885 | |
1769380a AM |
9886 | if (r->length != 0) |
9887 | { | |
9888 | double percent = used_length * 100.0 / r->length; | |
2410edcd | 9889 | printf (" %6.2f%%", percent); |
1769380a | 9890 | } |
2410edcd | 9891 | printf ("\n"); |
3604cb1f TG |
9892 | } |
9893 | } |