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