]>
Commit | Line | Data |
---|---|---|
1 | /* Linker command language support. | |
2 | Copyright (C) 1991-2025 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of the GNU Binutils. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
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. */ | |
20 | ||
21 | #include "sysdep.h" | |
22 | #include <limits.h> | |
23 | #include "bfd.h" | |
24 | #include "libiberty.h" | |
25 | #include "filenames.h" | |
26 | #include "safe-ctype.h" | |
27 | #include "obstack.h" | |
28 | #include "bfdlink.h" | |
29 | #include "ctf-api.h" | |
30 | #include "ld.h" | |
31 | #include "ldmain.h" | |
32 | #include "ldexp.h" | |
33 | #include "ldlang.h" | |
34 | #include <ldgram.h> | |
35 | #include "ldlex.h" | |
36 | #include "ldmisc.h" | |
37 | #include "ldctor.h" | |
38 | #include "ldfile.h" | |
39 | #include "ldemul.h" | |
40 | #include "ldwrite.h" | |
41 | #include "fnmatch.h" | |
42 | #include "demangle.h" | |
43 | #include "hashtab.h" | |
44 | #include "elf-bfd.h" | |
45 | #include "bfdver.h" | |
46 | ||
47 | #if BFD_SUPPORTS_PLUGINS | |
48 | #include "plugin.h" | |
49 | #endif | |
50 | ||
51 | /* FIXME: Put it here to avoid NAME conflict from ldgram.h. */ | |
52 | #include "elf-bfd.h" | |
53 | ||
54 | #ifndef offsetof | |
55 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) | |
56 | #endif | |
57 | ||
58 | /* Convert between addresses in bytes and sizes in octets. | |
59 | For currently supported targets, octets_per_byte is always a power | |
60 | of two, so we can use shifts. */ | |
61 | #define TO_ADDR(X) ((X) >> opb_shift) | |
62 | #define TO_SIZE(X) ((X) << opb_shift) | |
63 | ||
64 | /* Local variables. */ | |
65 | static struct obstack stat_obstack; | |
66 | static struct obstack map_obstack; | |
67 | static struct obstack pt_obstack; | |
68 | ||
69 | #define obstack_chunk_alloc xmalloc | |
70 | #define obstack_chunk_free free | |
71 | static const char *entry_symbol_default = "start"; | |
72 | static bool map_head_is_link_order = false; | |
73 | static lang_output_section_statement_type *default_common_section; | |
74 | static bool map_option_f; | |
75 | static bfd_vma print_dot; | |
76 | static lang_input_statement_type *first_file; | |
77 | static const char *current_target; | |
78 | static lang_statement_list_type *stat_save[10]; | |
79 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
80 | static struct unique_sections *unique_section_list; | |
81 | static struct asneeded_minfo *asneeded_list_head; | |
82 | static unsigned int opb_shift = 0; | |
83 | static cmdline_list_type cmdline_object_only_file_list; | |
84 | static cmdline_list_type cmdline_object_only_archive_list; | |
85 | static cmdline_list_type cmdline_temp_object_only_list; | |
86 | ||
87 | /* Forward declarations. */ | |
88 | static void exp_init_os (etree_type *); | |
89 | static lang_input_statement_type *lookup_name (const char *); | |
90 | static bool wont_add_section_p (asection *, | |
91 | lang_output_section_statement_type *); | |
92 | static void insert_undefined (const char *); | |
93 | static bool sort_def_symbol (struct bfd_link_hash_entry *, void *); | |
94 | static lang_statement_union_type *new_statement (enum statement_enum type, | |
95 | size_t size, | |
96 | lang_statement_list_type *list); | |
97 | static void print_statement (lang_statement_union_type *, | |
98 | lang_output_section_statement_type *); | |
99 | static void print_statement_list (lang_statement_union_type *, | |
100 | lang_output_section_statement_type *); | |
101 | static void print_statements (void); | |
102 | static void print_input_section (asection *, bool); | |
103 | static bool lang_one_common (struct bfd_link_hash_entry *, void *); | |
104 | static void lang_record_phdrs (void); | |
105 | static void lang_do_version_exports_section (void); | |
106 | static void lang_finalize_version_expr_head | |
107 | (struct bfd_elf_version_expr_head *); | |
108 | static void lang_do_memory_regions (bool); | |
109 | static void cmdline_lists_init (void); | |
110 | static void cmdline_get_object_only_input_files (void); | |
111 | static void print_cmdline_list (cmdline_union_type *); | |
112 | static bool cmdline_on_object_only_archive_list_p (bfd *); | |
113 | ||
114 | /* Exported variables. */ | |
115 | const char *output_target; | |
116 | lang_output_section_statement_type *abs_output_section; | |
117 | /* Header for list of statements corresponding to any files involved in the | |
118 | link, either specified from the command-line or added implicitely (eg. | |
119 | archive member used to resolved undefined symbol, wildcard statement from | |
120 | linker script, etc.). Next pointer is in next field of a | |
121 | lang_statement_header_type (reached via header field in a | |
122 | lang_statement_union). */ | |
123 | lang_statement_list_type statement_list; | |
124 | lang_statement_list_type lang_os_list; | |
125 | lang_statement_list_type *stat_ptr = &statement_list; | |
126 | /* Header for list of statements corresponding to files used in the final | |
127 | executable. This can be either object file specified on the command-line | |
128 | or library member resolving an undefined reference. Next pointer is in next | |
129 | field of a lang_input_statement_type (reached via input_statement field in a | |
130 | lang_statement_union). */ | |
131 | lang_statement_list_type file_chain = { NULL, NULL }; | |
132 | /* Header for list of statements corresponding to files specified on the | |
133 | command-line for linking. It thus contains real object files and archive | |
134 | but not archive members. Next pointer is in next_real_file field of a | |
135 | lang_input_statement_type statement (reached via input_statement field in a | |
136 | lang_statement_union). */ | |
137 | lang_statement_list_type input_file_chain; | |
138 | static const char *current_input_file; | |
139 | struct bfd_elf_dynamic_list **current_dynamic_list_p; | |
140 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; | |
141 | const char *entry_section = ".text"; | |
142 | struct lang_input_statement_flags input_flags; | |
143 | bool entry_from_cmdline; | |
144 | bool lang_has_input_file = false; | |
145 | bool had_output_filename = false; | |
146 | bool lang_float_flag = false; | |
147 | bool delete_output_file_on_failure = false; | |
148 | bool enable_linker_version = false; | |
149 | struct lang_phdr *lang_phdr_list; | |
150 | struct lang_nocrossrefs *nocrossref_list; | |
151 | struct asneeded_minfo **asneeded_list_tail; | |
152 | #ifdef ENABLE_LIBCTF | |
153 | static ctf_dict_t *ctf_output; | |
154 | #endif | |
155 | ||
156 | /* Functions that traverse the linker script and might evaluate | |
157 | DEFINED() need to increment this at the start of the traversal. */ | |
158 | int lang_statement_iteration = 0; | |
159 | ||
160 | /* Count times through one_lang_size_sections_pass after mark phase. */ | |
161 | static int lang_sizing_iteration = 0; | |
162 | ||
163 | /* Return TRUE if the PATTERN argument is a wildcard pattern. | |
164 | Although backslashes are treated specially if a pattern contains | |
165 | wildcards, we do not consider the mere presence of a backslash to | |
166 | be enough to cause the pattern to be treated as a wildcard. | |
167 | That lets us handle DOS filenames more naturally. */ | |
168 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
169 | ||
170 | #define new_stat(x, y) \ | |
171 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) | |
172 | ||
173 | #define outside_section_address(q) \ | |
174 | ((q)->output_offset + (q)->output_section->vma) | |
175 | ||
176 | #define outside_symbol_address(q) \ | |
177 | ((q)->value + outside_section_address (q->section)) | |
178 | ||
179 | /* CTF sections smaller than this are not compressed: compression of | |
180 | dictionaries this small doesn't gain much, and this lets consumers mmap the | |
181 | sections directly out of the ELF file and use them with no decompression | |
182 | overhead if they want to. */ | |
183 | #define CTF_COMPRESSION_THRESHOLD 4096 | |
184 | ||
185 | void * | |
186 | stat_alloc (size_t size) | |
187 | { | |
188 | return obstack_alloc (&stat_obstack, size); | |
189 | } | |
190 | ||
191 | void | |
192 | stat_free (void *str) | |
193 | { | |
194 | obstack_free (&stat_obstack, str); | |
195 | } | |
196 | ||
197 | void * | |
198 | stat_memdup (const void *src, size_t copy_size, size_t alloc_size) | |
199 | { | |
200 | void *ret = obstack_alloc (&stat_obstack, alloc_size); | |
201 | memcpy (ret, src, copy_size); | |
202 | if (alloc_size > copy_size) | |
203 | memset ((char *) ret + copy_size, 0, alloc_size - copy_size); | |
204 | return ret; | |
205 | } | |
206 | ||
207 | char * | |
208 | stat_strdup (const char *str) | |
209 | { | |
210 | size_t len = strlen (str) + 1; | |
211 | return stat_memdup (str, len, len); | |
212 | } | |
213 | ||
214 | char * | |
215 | stat_concat (const char *first, ...) | |
216 | { | |
217 | va_list args; | |
218 | va_start (args, first); | |
219 | ||
220 | size_t length = 0; | |
221 | for (const char *arg = first; arg; arg = va_arg (args, const char *)) | |
222 | length += strlen (arg); | |
223 | va_end (args); | |
224 | char *new_str = stat_alloc (length + 1); | |
225 | ||
226 | va_start (args, first); | |
227 | char *end = new_str; | |
228 | for (const char *arg = first; arg; arg = va_arg (args, const char *)) | |
229 | { | |
230 | length = strlen (arg); | |
231 | memcpy (end, arg, length); | |
232 | end += length; | |
233 | } | |
234 | *end = 0; | |
235 | va_end (args); | |
236 | return new_str; | |
237 | } | |
238 | ||
239 | /* Code for handling simple wildcards without going through fnmatch, | |
240 | which can be expensive because of charset translations etc. */ | |
241 | ||
242 | /* A simple wild is a literal string followed by a single '*', | |
243 | where the literal part is at least 4 characters long. */ | |
244 | ||
245 | static bool | |
246 | is_simple_wild (const char *name) | |
247 | { | |
248 | size_t len = strcspn (name, "*?["); | |
249 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
250 | } | |
251 | ||
252 | static bool | |
253 | match_simple_wild (const char *pattern, const char *name) | |
254 | { | |
255 | /* The first four characters of the pattern are guaranteed valid | |
256 | non-wildcard characters. So we can go faster. */ | |
257 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
258 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
259 | return false; | |
260 | ||
261 | pattern += 4; | |
262 | name += 4; | |
263 | while (*pattern != '*') | |
264 | if (*name++ != *pattern++) | |
265 | return false; | |
266 | ||
267 | return true; | |
268 | } | |
269 | ||
270 | static int | |
271 | name_match (const char *pattern, const char *name) | |
272 | { | |
273 | if (is_simple_wild (pattern)) | |
274 | return !match_simple_wild (pattern, name); | |
275 | if (wildcardp (pattern)) | |
276 | return fnmatch (pattern, name, 0); | |
277 | return strcmp (pattern, name); | |
278 | } | |
279 | ||
280 | /* Given an analyzed wildcard_spec SPEC, match it against NAME, | |
281 | returns zero on a match, non-zero if there's no match. */ | |
282 | ||
283 | static int | |
284 | spec_match (const struct wildcard_spec *spec, const char *name) | |
285 | { | |
286 | size_t nl = spec->namelen; | |
287 | size_t pl = spec->prefixlen; | |
288 | size_t sl = spec->suffixlen; | |
289 | size_t inputlen = strlen (name); | |
290 | int r; | |
291 | ||
292 | if (pl) | |
293 | { | |
294 | if (inputlen < pl) | |
295 | return 1; | |
296 | ||
297 | r = memcmp (spec->name, name, pl); | |
298 | if (r) | |
299 | return r; | |
300 | } | |
301 | ||
302 | if (sl) | |
303 | { | |
304 | if (inputlen < sl) | |
305 | return 1; | |
306 | ||
307 | r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl); | |
308 | if (r) | |
309 | return r; | |
310 | } | |
311 | ||
312 | if (nl == pl + sl + 1 && spec->name[pl] == '*') | |
313 | return 0; | |
314 | ||
315 | if (nl > pl) | |
316 | return fnmatch (spec->name + pl, name + pl, 0); | |
317 | ||
318 | if (inputlen >= nl) | |
319 | return name[nl]; | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | static char * | |
325 | stat_ldirname (const char *name) | |
326 | { | |
327 | const char *base = lbasename (name); | |
328 | ||
329 | while (base > name && IS_DIR_SEPARATOR (base[-1])) | |
330 | --base; | |
331 | size_t len = base - name; | |
332 | if (len == 0) | |
333 | return "."; | |
334 | return stat_memdup (name, len, len + 1); | |
335 | } | |
336 | ||
337 | /* If PATTERN is of the form archive:file, return a pointer to the | |
338 | separator. If not, return NULL. */ | |
339 | ||
340 | static char * | |
341 | archive_path (const char *pattern) | |
342 | { | |
343 | char *p = NULL; | |
344 | ||
345 | if (link_info.path_separator == 0) | |
346 | return p; | |
347 | ||
348 | p = strchr (pattern, link_info.path_separator); | |
349 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
350 | if (p == NULL || link_info.path_separator != ':') | |
351 | return p; | |
352 | ||
353 | /* Assume a match on the second char is part of drive specifier, | |
354 | as in "c:\silly.dos". */ | |
355 | if (p == pattern + 1 && ISALPHA (*pattern)) | |
356 | p = strchr (p + 1, link_info.path_separator); | |
357 | #endif | |
358 | return p; | |
359 | } | |
360 | ||
361 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, | |
362 | return whether F matches FILE_SPEC. */ | |
363 | ||
364 | static bool | |
365 | input_statement_is_archive_path (const char *file_spec, char *sep, | |
366 | lang_input_statement_type *f) | |
367 | { | |
368 | bool match = false; | |
369 | ||
370 | if ((*(sep + 1) == 0 | |
371 | || name_match (sep + 1, f->filename) == 0) | |
372 | && ((sep != file_spec) | |
373 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
374 | { | |
375 | match = true; | |
376 | ||
377 | if (sep != file_spec) | |
378 | { | |
379 | const char *aname = bfd_get_filename (f->the_bfd->my_archive); | |
380 | *sep = 0; | |
381 | match = name_match (file_spec, aname) == 0; | |
382 | *sep = link_info.path_separator; | |
383 | } | |
384 | } | |
385 | return match; | |
386 | } | |
387 | ||
388 | static bool | |
389 | unique_section_p (const asection *sec, | |
390 | const lang_output_section_statement_type *os) | |
391 | { | |
392 | struct unique_sections *unam; | |
393 | const char *secnam; | |
394 | ||
395 | if (!link_info.resolve_section_groups | |
396 | && sec->owner != NULL | |
397 | && bfd_is_group_section (sec->owner, sec)) | |
398 | return !(os != NULL | |
399 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
400 | ||
401 | secnam = sec->name; | |
402 | for (unam = unique_section_list; unam; unam = unam->next) | |
403 | if (name_match (unam->name, secnam) == 0) | |
404 | return true; | |
405 | ||
406 | return false; | |
407 | } | |
408 | ||
409 | /* Generic traversal routines for finding matching sections. */ | |
410 | ||
411 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return | |
412 | false. */ | |
413 | ||
414 | static bool | |
415 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, | |
416 | lang_input_statement_type *file) | |
417 | { | |
418 | struct name_list *list_tmp; | |
419 | ||
420 | for (list_tmp = exclude_list; | |
421 | list_tmp; | |
422 | list_tmp = list_tmp->next) | |
423 | { | |
424 | char *p = archive_path (list_tmp->name); | |
425 | ||
426 | if (p != NULL) | |
427 | { | |
428 | if (input_statement_is_archive_path (list_tmp->name, p, file)) | |
429 | return true; | |
430 | } | |
431 | ||
432 | else if (name_match (list_tmp->name, file->filename) == 0) | |
433 | return true; | |
434 | ||
435 | /* FIXME: Perhaps remove the following at some stage? Matching | |
436 | unadorned archives like this was never documented and has | |
437 | been superceded by the archive:path syntax. */ | |
438 | else if (file->the_bfd != NULL | |
439 | && file->the_bfd->my_archive != NULL | |
440 | && name_match (list_tmp->name, | |
441 | bfd_get_filename (file->the_bfd->my_archive)) == 0) | |
442 | return true; | |
443 | } | |
444 | ||
445 | return false; | |
446 | } | |
447 | ||
448 | /* Add SECTION (from input FILE) to the list of matching sections | |
449 | within PTR (the matching wildcard is SEC). */ | |
450 | ||
451 | static void | |
452 | add_matching_section (lang_wild_statement_type *ptr, | |
453 | struct wildcard_list *sec, | |
454 | asection *section, | |
455 | lang_input_statement_type *file) | |
456 | { | |
457 | lang_input_matcher_type *new_section; | |
458 | /* Add a section reference to the list. */ | |
459 | new_section = new_stat (lang_input_matcher, &ptr->matching_sections); | |
460 | new_section->section = section; | |
461 | new_section->pattern = sec; | |
462 | new_section->input_stmt = file; | |
463 | } | |
464 | ||
465 | /* Process section S (from input file FILE) in relation to wildcard | |
466 | statement PTR. We already know that a prefix of the name of S matches | |
467 | some wildcard in PTR's wildcard list. Here we check if the filename | |
468 | matches as well (if it's specified) and if any of the wildcards in fact | |
469 | does match. */ | |
470 | ||
471 | static void | |
472 | walk_wild_section_match (lang_wild_statement_type *ptr, | |
473 | lang_input_statement_type *file, | |
474 | asection *s) | |
475 | { | |
476 | struct wildcard_list *sec; | |
477 | const char *file_spec = ptr->filename; | |
478 | char *p; | |
479 | ||
480 | /* Check if filenames match. */ | |
481 | if (file_spec == NULL) | |
482 | ; | |
483 | else if ((p = archive_path (file_spec)) != NULL) | |
484 | { | |
485 | if (!input_statement_is_archive_path (file_spec, p, file)) | |
486 | return; | |
487 | } | |
488 | else if (wildcardp (file_spec)) | |
489 | { | |
490 | if (fnmatch (file_spec, file->filename, 0) != 0) | |
491 | return; | |
492 | } | |
493 | else | |
494 | { | |
495 | /* XXX Matching against non-wildcard filename in wild statements | |
496 | was done by going through lookup_name, which uses | |
497 | ->local_sym_name to compare against, not ->filename. We retain | |
498 | this behaviour even though the above code paths use filename. | |
499 | It would be more logical to use it here as well, in which | |
500 | case the above wildcard() arm could be folded into this by using | |
501 | name_match. This would also solve the worry of what to do | |
502 | about unset local_sym_name (in which case lookup_name simply adds | |
503 | the input file again). */ | |
504 | const char *filename = file->local_sym_name; | |
505 | lang_input_statement_type *arch_is; | |
506 | if (filename && filename_cmp (filename, file_spec) == 0) | |
507 | ; | |
508 | /* FIXME: see also walk_wild_file_in_exclude_list for why we | |
509 | also check parents BFD (local_sym_)name to match input statements | |
510 | with unadorned archive names. */ | |
511 | else if (file->the_bfd | |
512 | && file->the_bfd->my_archive | |
513 | && (arch_is = bfd_usrdata (file->the_bfd->my_archive)) | |
514 | && arch_is->local_sym_name | |
515 | && filename_cmp (arch_is->local_sym_name, file_spec) == 0) | |
516 | ; | |
517 | else | |
518 | return; | |
519 | } | |
520 | ||
521 | /* If filename is excluded we're done. */ | |
522 | if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file)) | |
523 | return; | |
524 | ||
525 | /* Check section name against each wildcard spec. If there's no | |
526 | wildcard all sections match. */ | |
527 | sec = ptr->section_list; | |
528 | if (sec == NULL) | |
529 | add_matching_section (ptr, sec, s, file); | |
530 | else | |
531 | { | |
532 | const char *sname = bfd_section_name (s); | |
533 | for (; sec != NULL; sec = sec->next) | |
534 | { | |
535 | if (sec->spec.name != NULL | |
536 | && spec_match (&sec->spec, sname) != 0) | |
537 | continue; | |
538 | ||
539 | /* Don't process sections from files which were excluded. */ | |
540 | if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, | |
541 | file)) | |
542 | add_matching_section (ptr, sec, s, file); | |
543 | } | |
544 | } | |
545 | } | |
546 | ||
547 | /* Return the numerical value of the init_priority attribute from | |
548 | section name NAME. */ | |
549 | ||
550 | static int | |
551 | get_init_priority (const asection *sec) | |
552 | { | |
553 | const char *name = bfd_section_name (sec); | |
554 | const char *dot; | |
555 | ||
556 | /* GCC uses the following section names for the init_priority | |
557 | attribute with numerical values 101 to 65535 inclusive. A | |
558 | lower value means a higher priority. | |
559 | ||
560 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the | |
561 | decimal numerical value of the init_priority attribute. | |
562 | The order of execution in .init_array is forward and | |
563 | .fini_array is backward. | |
564 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the | |
565 | decimal numerical value of the init_priority attribute. | |
566 | The order of execution in .ctors is backward and .dtors | |
567 | is forward. | |
568 | ||
569 | .init_array.NNNNN sections would normally be placed in an output | |
570 | .init_array section, .fini_array.NNNNN in .fini_array, | |
571 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
572 | we should sort by increasing number (and could just use | |
573 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
574 | being placed in .init_array (which may also contain | |
575 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
576 | placed in .fini_array then we need to extract the init_priority | |
577 | attribute and sort on that. */ | |
578 | dot = strrchr (name, '.'); | |
579 | if (dot != NULL && ISDIGIT (dot[1])) | |
580 | { | |
581 | char *end; | |
582 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
583 | if (*end == 0) | |
584 | { | |
585 | if (dot == name + 6 | |
586 | && (strncmp (name, ".ctors", 6) == 0 | |
587 | || strncmp (name, ".dtors", 6) == 0)) | |
588 | init_priority = 65535 - init_priority; | |
589 | if (init_priority <= INT_MAX) | |
590 | return init_priority; | |
591 | } | |
592 | } | |
593 | return -1; | |
594 | } | |
595 | ||
596 | /* Compare sections ASEC and BSEC according to SORT. */ | |
597 | ||
598 | static int | |
599 | compare_section (sort_type sort, asection *asec, asection *bsec, bool reversed) | |
600 | { | |
601 | int ret; | |
602 | int a_priority, b_priority; | |
603 | ||
604 | switch (sort) | |
605 | { | |
606 | default: | |
607 | abort (); | |
608 | ||
609 | case by_init_priority: | |
610 | a_priority = get_init_priority (asec); | |
611 | b_priority = get_init_priority (bsec); | |
612 | if (a_priority < 0 || b_priority < 0) | |
613 | goto sort_by_name; | |
614 | if (reversed) | |
615 | ret = b_priority - a_priority; | |
616 | else | |
617 | ret = a_priority - b_priority; | |
618 | if (ret) | |
619 | break; | |
620 | else | |
621 | goto sort_by_name; | |
622 | ||
623 | case by_alignment_name: | |
624 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); | |
625 | if (ret) | |
626 | break; | |
627 | /* Fall through. */ | |
628 | ||
629 | case by_name: | |
630 | sort_by_name: | |
631 | if (reversed) | |
632 | ret = strcmp (bfd_section_name (bsec), bfd_section_name (asec)); | |
633 | else | |
634 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); | |
635 | break; | |
636 | ||
637 | case by_name_alignment: | |
638 | if (reversed) | |
639 | ret = strcmp (bfd_section_name (bsec), bfd_section_name (asec)); | |
640 | else | |
641 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); | |
642 | if (ret) | |
643 | break; | |
644 | /* Fall through. */ | |
645 | ||
646 | case by_alignment: | |
647 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); | |
648 | break; | |
649 | } | |
650 | ||
651 | return ret; | |
652 | } | |
653 | ||
654 | /* PE puts the sort key in the input statement. */ | |
655 | ||
656 | static const char * | |
657 | sort_filename (bfd *abfd) | |
658 | { | |
659 | lang_input_statement_type *is = bfd_usrdata (abfd); | |
660 | if (is->sort_key) | |
661 | return is->sort_key; | |
662 | return bfd_get_filename (abfd); | |
663 | } | |
664 | ||
665 | /* Handle wildcard sorting. This returns the place in a binary search tree | |
666 | where this FILE:SECTION should be inserted for wild statement WILD where | |
667 | the spec SEC was the matching one. The tree is later linearized. */ | |
668 | ||
669 | static lang_section_bst_type ** | |
670 | wild_sort (lang_wild_statement_type *wild, | |
671 | struct wildcard_list *sec, | |
672 | lang_input_statement_type *file, | |
673 | asection *section) | |
674 | { | |
675 | lang_section_bst_type **tree; | |
676 | ||
677 | if (!wild->filenames_sorted | |
678 | && (sec == NULL || sec->spec.sorted == none | |
679 | || sec->spec.sorted == by_none)) | |
680 | { | |
681 | /* We might be called even if _this_ spec doesn't need sorting, | |
682 | in which case we simply append at the right end of tree. */ | |
683 | return wild->rightmost; | |
684 | } | |
685 | ||
686 | tree = &wild->tree; | |
687 | while (*tree) | |
688 | { | |
689 | /* Sorting by filename takes precedence over sorting by section | |
690 | name. */ | |
691 | ||
692 | if (wild->filenames_sorted) | |
693 | { | |
694 | const char *fn, *ln; | |
695 | bool fa, la; | |
696 | int i; | |
697 | asection *lsec = (*tree)->section; | |
698 | ||
699 | /* The PE support for the .idata section as generated by | |
700 | dlltool assumes that files will be sorted by the name of | |
701 | the archive and then the name of the file within the | |
702 | archive. */ | |
703 | ||
704 | fa = file->the_bfd->my_archive != NULL; | |
705 | if (fa) | |
706 | fn = sort_filename (file->the_bfd->my_archive); | |
707 | else | |
708 | fn = sort_filename (file->the_bfd); | |
709 | ||
710 | la = lsec->owner->my_archive != NULL; | |
711 | if (la) | |
712 | ln = sort_filename (lsec->owner->my_archive); | |
713 | else | |
714 | ln = sort_filename (lsec->owner); | |
715 | ||
716 | if (wild->filenames_reversed) | |
717 | i = filename_cmp (ln, fn); | |
718 | else | |
719 | i = filename_cmp (fn, ln); | |
720 | ||
721 | if (i > 0) | |
722 | { tree = &((*tree)->right); continue; } | |
723 | else if (i < 0) | |
724 | { tree = &((*tree)->left); continue; } | |
725 | ||
726 | if (fa || la) | |
727 | { | |
728 | if (fa) | |
729 | fn = sort_filename (file->the_bfd); | |
730 | if (la) | |
731 | ln = sort_filename (lsec->owner); | |
732 | ||
733 | if (wild->filenames_reversed) | |
734 | i = filename_cmp (ln, fn); | |
735 | else | |
736 | i = filename_cmp (fn, ln); | |
737 | ||
738 | if (i > 0) | |
739 | { tree = &((*tree)->right); continue; } | |
740 | else if (i < 0) | |
741 | { tree = &((*tree)->left); continue; } | |
742 | } | |
743 | } | |
744 | ||
745 | /* Here either the files are not sorted by name, or we are | |
746 | looking at the sections for this file. */ | |
747 | ||
748 | /* Find the correct node to append this section. */ | |
749 | if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none | |
750 | && compare_section (sec->spec.sorted, section, (*tree)->section, sec->spec.reversed) < 0) | |
751 | tree = &((*tree)->left); | |
752 | else | |
753 | tree = &((*tree)->right); | |
754 | } | |
755 | ||
756 | return tree; | |
757 | } | |
758 | ||
759 | /* Use wild_sort to build a BST to sort sections. */ | |
760 | ||
761 | static void | |
762 | output_section_callback_sort (lang_wild_statement_type *ptr, | |
763 | struct wildcard_list *sec, | |
764 | asection *section, | |
765 | lang_input_statement_type *file, | |
766 | void *output) | |
767 | { | |
768 | lang_section_bst_type *node; | |
769 | lang_section_bst_type **tree; | |
770 | lang_output_section_statement_type *os; | |
771 | ||
772 | os = (lang_output_section_statement_type *) output; | |
773 | ||
774 | if (unique_section_p (section, os)) | |
775 | return; | |
776 | ||
777 | /* Don't add sections to the tree when we already know that | |
778 | lang_add_section won't do anything with it. */ | |
779 | if (wont_add_section_p (section, os)) | |
780 | return; | |
781 | ||
782 | node = stat_alloc (sizeof (*node)); | |
783 | node->left = 0; | |
784 | node->right = 0; | |
785 | node->section = section; | |
786 | node->pattern = ptr->section_list; | |
787 | ||
788 | tree = wild_sort (ptr, sec, file, section); | |
789 | if (tree != NULL) | |
790 | { | |
791 | *tree = node; | |
792 | if (tree == ptr->rightmost) | |
793 | ptr->rightmost = &node->right; | |
794 | } | |
795 | } | |
796 | ||
797 | /* Convert a sorted sections' BST back to list form. */ | |
798 | ||
799 | static void | |
800 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, | |
801 | lang_section_bst_type *tree, | |
802 | void *output) | |
803 | { | |
804 | if (tree->left) | |
805 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
806 | ||
807 | lang_add_section (&ptr->children, tree->section, tree->pattern, | |
808 | ptr->section_flag_list, | |
809 | (lang_output_section_statement_type *) output); | |
810 | ||
811 | if (tree->right) | |
812 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
813 | } | |
814 | ||
815 | \f | |
816 | /* Sections are matched against wildcard statements via a prefix tree. | |
817 | The prefix tree holds prefixes of all matching patterns (up to the first | |
818 | wildcard character), and the wild statement from which those patterns | |
819 | came. When matching a section name against the tree we're walking through | |
820 | the tree character by character. Each statement we hit is one that | |
821 | potentially matches. This is checked by actually going through the | |
822 | (glob) matching routines. | |
823 | ||
824 | When the section name turns out to actually match we record that section | |
825 | in the wild statements list of matching sections. */ | |
826 | ||
827 | /* A prefix can be matched by multiple statement, so we need a list of them. */ | |
828 | struct wild_stmt_list | |
829 | { | |
830 | lang_wild_statement_type *stmt; | |
831 | struct wild_stmt_list *next; | |
832 | }; | |
833 | ||
834 | /* The prefix tree itself. */ | |
835 | struct prefixtree | |
836 | { | |
837 | /* The list of all children (linked via .next). */ | |
838 | struct prefixtree *child; | |
839 | struct prefixtree *next; | |
840 | /* This tree node is responsible for the prefix of parent plus 'c'. */ | |
841 | char c; | |
842 | /* The statements that potentially can match this prefix. */ | |
843 | struct wild_stmt_list *stmt; | |
844 | }; | |
845 | ||
846 | /* We always have a root node in the prefix tree. It corresponds to the | |
847 | empty prefix. E.g. a glob like "*" would sit in this root. */ | |
848 | static struct prefixtree the_root, *ptroot = &the_root; | |
849 | ||
850 | /* Given a prefix tree in *TREE, corresponding to prefix P, find or | |
851 | INSERT the tree node corresponding to prefix P+C. */ | |
852 | ||
853 | static struct prefixtree * | |
854 | get_prefix_tree (struct prefixtree **tree, char c, bool insert) | |
855 | { | |
856 | struct prefixtree *t; | |
857 | for (t = *tree; t; t = t->next) | |
858 | if (t->c == c) | |
859 | return t; | |
860 | if (!insert) | |
861 | return NULL; | |
862 | t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); | |
863 | t->child = NULL; | |
864 | t->next = *tree; | |
865 | t->c = c; | |
866 | t->stmt = NULL; | |
867 | *tree = t; | |
868 | return t; | |
869 | } | |
870 | ||
871 | /* Add STMT to the set of statements that can be matched by the prefix | |
872 | corresponding to prefix tree T. */ | |
873 | ||
874 | static void | |
875 | pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) | |
876 | { | |
877 | struct wild_stmt_list *sl, **psl; | |
878 | sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); | |
879 | sl->stmt = stmt; | |
880 | sl->next = NULL; | |
881 | psl = &t->stmt; | |
882 | while (*psl) | |
883 | psl = &(*psl)->next; | |
884 | *psl = sl; | |
885 | } | |
886 | ||
887 | /* Insert STMT into the global prefix tree. */ | |
888 | ||
889 | static void | |
890 | insert_prefix_tree (lang_wild_statement_type *stmt) | |
891 | { | |
892 | struct wildcard_list *sec; | |
893 | struct prefixtree *t; | |
894 | ||
895 | if (!stmt->section_list) | |
896 | { | |
897 | /* If we have no section_list (no wildcards in the wild STMT), | |
898 | then every section name will match, so add this to the root. */ | |
899 | pt_add_stmt (ptroot, stmt); | |
900 | return; | |
901 | } | |
902 | ||
903 | for (sec = stmt->section_list; sec; sec = sec->next) | |
904 | { | |
905 | const char *name = sec->spec.name ? sec->spec.name : "*"; | |
906 | char c; | |
907 | t = ptroot; | |
908 | for (; (c = *name); name++) | |
909 | { | |
910 | if (c == '*' || c == '[' || c == '?') | |
911 | break; | |
912 | t = get_prefix_tree (&t->child, c, true); | |
913 | } | |
914 | /* If we hit a glob character, the matching prefix is what we saw | |
915 | until now. If we hit the end of pattern (hence it's no glob) then | |
916 | we can do better: we only need to record a match when a section name | |
917 | completely matches, not merely a prefix, so record the trailing 0 | |
918 | as well. */ | |
919 | if (!c) | |
920 | t = get_prefix_tree (&t->child, 0, true); | |
921 | pt_add_stmt (t, stmt); | |
922 | } | |
923 | } | |
924 | ||
925 | /* Dump T indented by INDENT spaces. */ | |
926 | ||
927 | static void | |
928 | debug_prefix_tree_rec (struct prefixtree *t, int indent) | |
929 | { | |
930 | for (; t; t = t->next) | |
931 | { | |
932 | struct wild_stmt_list *sl; | |
933 | printf ("%*s %c", indent, "", t->c); | |
934 | for (sl = t->stmt; sl; sl = sl->next) | |
935 | { | |
936 | struct wildcard_list *curr; | |
937 | printf (" %p ", sl->stmt); | |
938 | for (curr = sl->stmt->section_list; curr; curr = curr->next) | |
939 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
940 | } | |
941 | printf ("\n"); | |
942 | debug_prefix_tree_rec (t->child, indent + 2); | |
943 | } | |
944 | } | |
945 | ||
946 | /* Dump the global prefix tree. */ | |
947 | ||
948 | static void | |
949 | debug_prefix_tree (void) | |
950 | { | |
951 | debug_prefix_tree_rec (ptroot, 2); | |
952 | } | |
953 | ||
954 | /* Like strcspn() but start to look from the end to beginning of | |
955 | S. Returns the length of the suffix of S consisting entirely | |
956 | of characters not in REJECT. */ | |
957 | ||
958 | static size_t | |
959 | rstrcspn (const char *s, const char *reject) | |
960 | { | |
961 | size_t len = strlen (s), sufflen = 0; | |
962 | while (len--) | |
963 | { | |
964 | char c = s[len]; | |
965 | if (strchr (reject, c) != 0) | |
966 | break; | |
967 | sufflen++; | |
968 | } | |
969 | return sufflen; | |
970 | } | |
971 | ||
972 | /* Analyze the wildcards in wild statement PTR to setup various | |
973 | things for quick matching. */ | |
974 | ||
975 | static void | |
976 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
977 | { | |
978 | struct wildcard_list *sec; | |
979 | ||
980 | ptr->tree = NULL; | |
981 | ptr->rightmost = &ptr->tree; | |
982 | ||
983 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) | |
984 | { | |
985 | if (sec->spec.name) | |
986 | { | |
987 | sec->spec.namelen = strlen (sec->spec.name); | |
988 | sec->spec.prefixlen = strcspn (sec->spec.name, "?*["); | |
989 | sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen, | |
990 | "?*]"); | |
991 | } | |
992 | else | |
993 | sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; | |
994 | } | |
995 | ||
996 | insert_prefix_tree (ptr); | |
997 | } | |
998 | ||
999 | /* Match all sections from FILE against the global prefix tree, | |
1000 | and record them into each wild statement that has a match. */ | |
1001 | ||
1002 | static void | |
1003 | resolve_wild_sections (lang_input_statement_type *file) | |
1004 | { | |
1005 | asection *s; | |
1006 | ||
1007 | if (file->flags.just_syms) | |
1008 | return; | |
1009 | ||
1010 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
1011 | { | |
1012 | const char *sname = bfd_section_name (s); | |
1013 | char c = 1; | |
1014 | struct prefixtree *t = ptroot; | |
1015 | //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename); | |
1016 | do | |
1017 | { | |
1018 | if (t->stmt) | |
1019 | { | |
1020 | struct wild_stmt_list *sl; | |
1021 | for (sl = t->stmt; sl; sl = sl->next) | |
1022 | { | |
1023 | walk_wild_section_match (sl->stmt, file, s); | |
1024 | //printf (" ZZZ maybe place into %p\n", sl->stmt); | |
1025 | } | |
1026 | } | |
1027 | if (!c) | |
1028 | break; | |
1029 | c = *sname++; | |
1030 | t = get_prefix_tree (&t->child, c, false); | |
1031 | } | |
1032 | while (t); | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | /* Match all sections from all input files against the global prefix tree. */ | |
1037 | ||
1038 | static void | |
1039 | resolve_wilds (void) | |
1040 | { | |
1041 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
1042 | { | |
1043 | //printf("XXX %s\n", f->filename); | |
1044 | if (f->the_bfd == NULL | |
1045 | || !bfd_check_format (f->the_bfd, bfd_archive)) | |
1046 | resolve_wild_sections (f); | |
1047 | else | |
1048 | { | |
1049 | bfd *member; | |
1050 | ||
1051 | /* This is an archive file. We must map each member of the | |
1052 | archive separately. */ | |
1053 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); | |
1054 | while (member != NULL) | |
1055 | { | |
1056 | /* When lookup_name is called, it will call the add_symbols | |
1057 | entry point for the archive. For each element of the | |
1058 | archive which is included, BFD will call ldlang_add_file, | |
1059 | which will set the usrdata field of the member to the | |
1060 | lang_input_statement. */ | |
1061 | if (bfd_usrdata (member) != NULL) | |
1062 | resolve_wild_sections (bfd_usrdata (member)); | |
1063 | ||
1064 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | /* For each input section that matches wild statement S calls | |
1071 | CALLBACK with DATA. */ | |
1072 | ||
1073 | static void | |
1074 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) | |
1075 | { | |
1076 | lang_statement_union_type *l; | |
1077 | ||
1078 | for (l = s->matching_sections.head; l; l = l->header.next) | |
1079 | { | |
1080 | (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, | |
1081 | l->input_matcher.input_stmt, data); | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | /* lang_for_each_statement walks the parse tree and calls the provided | |
1086 | function for each node, except those inside output section statements | |
1087 | with constraint set to -1. */ | |
1088 | ||
1089 | void | |
1090 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), | |
1091 | lang_statement_union_type *s) | |
1092 | { | |
1093 | for (; s != NULL; s = s->header.next) | |
1094 | { | |
1095 | func (s); | |
1096 | ||
1097 | switch (s->header.type) | |
1098 | { | |
1099 | case lang_constructors_statement_enum: | |
1100 | lang_for_each_statement_worker (func, constructor_list.head); | |
1101 | break; | |
1102 | case lang_output_section_statement_enum: | |
1103 | if (s->output_section_statement.constraint != -1) | |
1104 | lang_for_each_statement_worker | |
1105 | (func, s->output_section_statement.children.head); | |
1106 | break; | |
1107 | case lang_wild_statement_enum: | |
1108 | lang_for_each_statement_worker (func, | |
1109 | s->wild_statement.children.head); | |
1110 | break; | |
1111 | case lang_group_statement_enum: | |
1112 | lang_for_each_statement_worker (func, | |
1113 | s->group_statement.children.head); | |
1114 | break; | |
1115 | case lang_data_statement_enum: | |
1116 | case lang_reloc_statement_enum: | |
1117 | case lang_object_symbols_statement_enum: | |
1118 | case lang_output_statement_enum: | |
1119 | case lang_target_statement_enum: | |
1120 | case lang_input_section_enum: | |
1121 | case lang_input_statement_enum: | |
1122 | case lang_assignment_statement_enum: | |
1123 | case lang_padding_statement_enum: | |
1124 | case lang_address_statement_enum: | |
1125 | case lang_fill_statement_enum: | |
1126 | case lang_insert_statement_enum: | |
1127 | break; | |
1128 | default: | |
1129 | FAIL (); | |
1130 | break; | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | void | |
1136 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) | |
1137 | { | |
1138 | lang_for_each_statement_worker (func, statement_list.head); | |
1139 | } | |
1140 | ||
1141 | /*----------------------------------------------------------------------*/ | |
1142 | ||
1143 | void | |
1144 | lang_list_init (lang_statement_list_type *list) | |
1145 | { | |
1146 | list->head = NULL; | |
1147 | list->tail = &list->head; | |
1148 | } | |
1149 | ||
1150 | static void | |
1151 | lang_statement_append (lang_statement_list_type *list, | |
1152 | void *element, | |
1153 | void *field) | |
1154 | { | |
1155 | *(list->tail) = element; | |
1156 | list->tail = field; | |
1157 | } | |
1158 | ||
1159 | void | |
1160 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1161 | { | |
1162 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1163 | abort (); | |
1164 | *stat_save_ptr++ = stat_ptr; | |
1165 | stat_ptr = new_ptr; | |
1166 | } | |
1167 | ||
1168 | void | |
1169 | pop_stat_ptr (void) | |
1170 | { | |
1171 | if (stat_save_ptr <= stat_save) | |
1172 | abort (); | |
1173 | stat_ptr = *--stat_save_ptr; | |
1174 | } | |
1175 | ||
1176 | /* Build a new statement node for the parse tree. */ | |
1177 | ||
1178 | static lang_statement_union_type * | |
1179 | new_statement (enum statement_enum type, | |
1180 | size_t size, | |
1181 | lang_statement_list_type *list) | |
1182 | { | |
1183 | lang_statement_union_type *new_stmt; | |
1184 | ||
1185 | new_stmt = stat_alloc (size); | |
1186 | new_stmt->header.type = type; | |
1187 | new_stmt->header.next = NULL; | |
1188 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1189 | return new_stmt; | |
1190 | } | |
1191 | ||
1192 | /* Build a new input file node for the language. There are several | |
1193 | ways in which we treat an input file, eg, we only look at symbols, | |
1194 | or prefix it with a -l etc. | |
1195 | ||
1196 | We can be supplied with requests for input files more than once; | |
1197 | they may, for example be split over several lines like foo.o(.text) | |
1198 | foo.o(.data) etc, so when asked for a file we check that we haven't | |
1199 | got it already so we don't duplicate the bfd. */ | |
1200 | ||
1201 | static lang_input_statement_type * | |
1202 | new_afile (const char *name, | |
1203 | lang_input_file_enum_type file_type, | |
1204 | const char *target, | |
1205 | const char *from_filename) | |
1206 | { | |
1207 | lang_input_statement_type *p; | |
1208 | ||
1209 | lang_has_input_file = true; | |
1210 | ||
1211 | /* PR 30632: It is OK for name to be NULL. For example | |
1212 | see the initialization of first_file in lang_init(). */ | |
1213 | if (name != NULL) | |
1214 | { | |
1215 | name = ldfile_possibly_remap_input (name); | |
1216 | /* But if a name is remapped to NULL, it should be ignored. */ | |
1217 | if (name == NULL) | |
1218 | return NULL; | |
1219 | } | |
1220 | ||
1221 | p = new_stat (lang_input_statement, stat_ptr); | |
1222 | memset (&p->the_bfd, 0, | |
1223 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
1224 | p->extra_search_path = NULL; | |
1225 | p->target = target; | |
1226 | p->flags.dynamic = input_flags.dynamic; | |
1227 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1228 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1229 | p->flags.whole_archive = input_flags.whole_archive; | |
1230 | p->flags.sysrooted = input_flags.sysrooted; | |
1231 | p->sort_key = NULL; | |
1232 | ||
1233 | switch (file_type) | |
1234 | { | |
1235 | case lang_input_file_is_symbols_only_enum: | |
1236 | p->filename = name; | |
1237 | p->local_sym_name = name; | |
1238 | p->flags.real = true; | |
1239 | p->flags.just_syms = true; | |
1240 | break; | |
1241 | case lang_input_file_is_fake_enum: | |
1242 | p->filename = name; | |
1243 | p->local_sym_name = name; | |
1244 | break; | |
1245 | case lang_input_file_is_l_enum: | |
1246 | if (name[0] == ':' && name[1] != '\0') | |
1247 | { | |
1248 | p->filename = name + 1; | |
1249 | p->flags.full_name_provided = true; | |
1250 | } | |
1251 | else | |
1252 | p->filename = name; | |
1253 | p->local_sym_name = concat ("-l", name, (const char *) NULL); | |
1254 | p->flags.maybe_archive = true; | |
1255 | p->flags.real = true; | |
1256 | p->flags.search_dirs = true; | |
1257 | break; | |
1258 | case lang_input_file_is_marker_enum: | |
1259 | p->filename = name; | |
1260 | p->local_sym_name = name; | |
1261 | p->flags.search_dirs = true; | |
1262 | break; | |
1263 | case lang_input_file_is_search_file_enum: | |
1264 | p->filename = name; | |
1265 | p->local_sym_name = name; | |
1266 | /* If name is a relative path, search the directory of the current linker | |
1267 | script first. */ | |
1268 | if (from_filename && !IS_ABSOLUTE_PATH (name)) | |
1269 | p->extra_search_path = stat_ldirname (from_filename); | |
1270 | p->flags.real = true; | |
1271 | p->flags.search_dirs = true; | |
1272 | break; | |
1273 | case lang_input_file_is_file_enum: | |
1274 | p->filename = name; | |
1275 | p->local_sym_name = name; | |
1276 | p->flags.real = true; | |
1277 | break; | |
1278 | default: | |
1279 | FAIL (); | |
1280 | } | |
1281 | ||
1282 | lang_statement_append (&input_file_chain, p, &p->next_real_file); | |
1283 | return p; | |
1284 | } | |
1285 | ||
1286 | lang_input_statement_type * | |
1287 | lang_add_input_file (const char *name, | |
1288 | lang_input_file_enum_type file_type, | |
1289 | const char *target) | |
1290 | { | |
1291 | if (name != NULL | |
1292 | && (*name == '=' || startswith (name, "$SYSROOT"))) | |
1293 | { | |
1294 | lang_input_statement_type *ret; | |
1295 | char *sysrooted_name | |
1296 | = concat (ld_sysroot, | |
1297 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1298 | (const char *) NULL); | |
1299 | ||
1300 | /* We've now forcibly prepended the sysroot, making the input | |
1301 | file independent of the context. Therefore, temporarily | |
1302 | force a non-sysrooted context for this statement, so it won't | |
1303 | get the sysroot prepended again when opened. (N.B. if it's a | |
1304 | script, any child nodes with input files starting with "/" | |
1305 | will be handled as "sysrooted" as they'll be found to be | |
1306 | within the sysroot subdirectory.) */ | |
1307 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1308 | input_flags.sysrooted = 0; | |
1309 | ret = new_afile (sysrooted_name, file_type, target, NULL); | |
1310 | input_flags.sysrooted = outer_sysrooted; | |
1311 | return ret; | |
1312 | } | |
1313 | ||
1314 | return new_afile (name, file_type, target, current_input_file); | |
1315 | } | |
1316 | ||
1317 | struct out_section_hash_entry | |
1318 | { | |
1319 | struct bfd_hash_entry root; | |
1320 | lang_statement_union_type s; | |
1321 | struct out_section_hash_entry *tail; | |
1322 | }; | |
1323 | ||
1324 | /* The hash table. */ | |
1325 | ||
1326 | static struct bfd_hash_table output_section_statement_table; | |
1327 | ||
1328 | /* Support routines for the hash table used by lang_output_section_find, | |
1329 | initialize the table, fill in an entry and remove the table. */ | |
1330 | ||
1331 | static struct out_section_hash_entry * | |
1332 | output_section_statement_newfunc_1 (struct bfd_hash_entry *entry, | |
1333 | struct bfd_hash_table *table, | |
1334 | const char *string) | |
1335 | { | |
1336 | lang_output_section_statement_type **nextp; | |
1337 | struct out_section_hash_entry *ret; | |
1338 | ||
1339 | if (entry == NULL) | |
1340 | { | |
1341 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, | |
1342 | sizeof (*ret)); | |
1343 | if (entry == NULL) | |
1344 | return NULL; | |
1345 | } | |
1346 | ||
1347 | entry = bfd_hash_newfunc (entry, table, string); | |
1348 | if (entry == NULL) | |
1349 | return NULL; | |
1350 | ||
1351 | ret = (struct out_section_hash_entry *) entry; | |
1352 | memset (&ret->s, 0, sizeof (ret->s)); | |
1353 | ret->s.header.type = lang_output_section_statement_enum; | |
1354 | ret->s.output_section_statement.subsection_alignment = NULL; | |
1355 | ret->s.output_section_statement.section_alignment = NULL; | |
1356 | ret->s.output_section_statement.block_value = 1; | |
1357 | lang_list_init (&ret->s.output_section_statement.children); | |
1358 | lang_list_init (&ret->s.output_section_statement.sort_children); | |
1359 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
1360 | ||
1361 | /* For every output section statement added to the list, except the | |
1362 | first one, lang_os_list.tail points to the "next" | |
1363 | field of the last element of the list. */ | |
1364 | if (lang_os_list.head != NULL) | |
1365 | ret->s.output_section_statement.prev | |
1366 | = ((lang_output_section_statement_type *) | |
1367 | ((char *) lang_os_list.tail | |
1368 | - offsetof (lang_output_section_statement_type, next))); | |
1369 | ||
1370 | /* GCC's strict aliasing rules prevent us from just casting the | |
1371 | address, so we store the pointer in a variable and cast that | |
1372 | instead. */ | |
1373 | nextp = &ret->s.output_section_statement.next; | |
1374 | lang_statement_append (&lang_os_list, &ret->s, nextp); | |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | static struct bfd_hash_entry * | |
1379 | output_section_statement_newfunc (struct bfd_hash_entry *entry, | |
1380 | struct bfd_hash_table *table, | |
1381 | const char *string) | |
1382 | { | |
1383 | struct out_section_hash_entry *ret; | |
1384 | ||
1385 | ret = output_section_statement_newfunc_1 (entry, table, string); | |
1386 | if (ret == NULL) | |
1387 | return NULL; | |
1388 | ret->tail = ret; | |
1389 | return &ret->root; | |
1390 | } | |
1391 | ||
1392 | static void | |
1393 | output_section_statement_table_init (void) | |
1394 | { | |
1395 | if (!bfd_hash_table_init_n (&output_section_statement_table, | |
1396 | output_section_statement_newfunc, | |
1397 | sizeof (struct out_section_hash_entry), | |
1398 | 61)) | |
1399 | fatal (_("%P: can not create hash table: %E\n")); | |
1400 | } | |
1401 | ||
1402 | static void | |
1403 | output_section_statement_table_free (void) | |
1404 | { | |
1405 | bfd_hash_table_free (&output_section_statement_table); | |
1406 | } | |
1407 | ||
1408 | /* Build enough state so that the parser can build its tree. */ | |
1409 | ||
1410 | void | |
1411 | lang_init (bool object_only) | |
1412 | { | |
1413 | if (!object_only) | |
1414 | { | |
1415 | obstack_begin (&stat_obstack, 1000); | |
1416 | obstack_init (&pt_obstack); | |
1417 | } | |
1418 | ||
1419 | stat_ptr = &statement_list; | |
1420 | ||
1421 | output_section_statement_table_init (); | |
1422 | ||
1423 | cmdline_lists_init (); | |
1424 | ||
1425 | lang_list_init (stat_ptr); | |
1426 | ||
1427 | lang_list_init (&input_file_chain); | |
1428 | lang_list_init (&lang_os_list); | |
1429 | lang_list_init (&file_chain); | |
1430 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, | |
1431 | NULL); | |
1432 | abs_output_section = | |
1433 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); | |
1434 | ||
1435 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
1436 | ||
1437 | asneeded_list_head = NULL; | |
1438 | asneeded_list_tail = &asneeded_list_head; | |
1439 | } | |
1440 | ||
1441 | void | |
1442 | lang_finish (void) | |
1443 | { | |
1444 | output_section_statement_table_free (); | |
1445 | ldfile_free (); | |
1446 | } | |
1447 | ||
1448 | /*---------------------------------------------------------------------- | |
1449 | A region is an area of memory declared with the | |
1450 | MEMORY { name:org=exp, len=exp ... } | |
1451 | syntax. | |
1452 | ||
1453 | We maintain a list of all the regions here. | |
1454 | ||
1455 | If no regions are specified in the script, then the default is used | |
1456 | which is created when looked up to be the entire data space. | |
1457 | ||
1458 | If create is true we are creating a region inside a MEMORY block. | |
1459 | In this case it is probably an error to create a region that has | |
1460 | already been created. If we are not inside a MEMORY block it is | |
1461 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
1462 | and so we issue a warning. | |
1463 | ||
1464 | Each region has at least one name. The first name is either | |
1465 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1466 | alias names to an existing region within a script with | |
1467 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1468 | region. */ | |
1469 | ||
1470 | static lang_memory_region_type *lang_memory_region_list; | |
1471 | static lang_memory_region_type **lang_memory_region_list_tail | |
1472 | = &lang_memory_region_list; | |
1473 | ||
1474 | lang_memory_region_type * | |
1475 | lang_memory_region_lookup (const char *const name, bool create) | |
1476 | { | |
1477 | lang_memory_region_name *n; | |
1478 | lang_memory_region_type *r; | |
1479 | lang_memory_region_type *new_region; | |
1480 | ||
1481 | /* NAME is NULL for LMA memspecs if no region was specified. */ | |
1482 | if (name == NULL) | |
1483 | return NULL; | |
1484 | ||
1485 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1486 | for (n = &r->name_list; n != NULL; n = n->next) | |
1487 | if (strcmp (n->name, name) == 0) | |
1488 | { | |
1489 | if (create) | |
1490 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), | |
1491 | NULL, name); | |
1492 | return r; | |
1493 | } | |
1494 | ||
1495 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) | |
1496 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), | |
1497 | NULL, name); | |
1498 | ||
1499 | new_region = stat_alloc (sizeof (lang_memory_region_type)); | |
1500 | ||
1501 | new_region->name_list.name = stat_strdup (name); | |
1502 | new_region->name_list.next = NULL; | |
1503 | new_region->next = NULL; | |
1504 | new_region->origin_exp = NULL; | |
1505 | new_region->origin = 0; | |
1506 | new_region->length_exp = NULL; | |
1507 | new_region->length = ~(bfd_size_type) 0; | |
1508 | new_region->current = 0; | |
1509 | new_region->last_os = NULL; | |
1510 | new_region->flags = 0; | |
1511 | new_region->not_flags = 0; | |
1512 | new_region->had_full_message = false; | |
1513 | ||
1514 | *lang_memory_region_list_tail = new_region; | |
1515 | lang_memory_region_list_tail = &new_region->next; | |
1516 | ||
1517 | return new_region; | |
1518 | } | |
1519 | ||
1520 | void | |
1521 | lang_memory_region_alias (const char *alias, const char *region_name) | |
1522 | { | |
1523 | lang_memory_region_name *n; | |
1524 | lang_memory_region_type *r; | |
1525 | lang_memory_region_type *region; | |
1526 | ||
1527 | /* The default region must be unique. This ensures that it is not necessary | |
1528 | to iterate through the name list if someone wants the check if a region is | |
1529 | the default memory region. */ | |
1530 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1531 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
1532 | fatal (_("%P:%pS: error: alias for default memory region\n"), NULL); | |
1533 | ||
1534 | /* Look for the target region and check if the alias is not already | |
1535 | in use. */ | |
1536 | region = NULL; | |
1537 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1538 | for (n = &r->name_list; n != NULL; n = n->next) | |
1539 | { | |
1540 | if (region == NULL && strcmp (n->name, region_name) == 0) | |
1541 | region = r; | |
1542 | if (strcmp (n->name, alias) == 0) | |
1543 | fatal (_("%P:%pS: error: redefinition of memory region " | |
1544 | "alias `%s'\n"), | |
1545 | NULL, alias); | |
1546 | } | |
1547 | ||
1548 | /* Check if the target region exists. */ | |
1549 | if (region == NULL) | |
1550 | fatal (_("%P:%pS: error: memory region `%s' " | |
1551 | "for alias `%s' does not exist\n"), | |
1552 | NULL, region_name, alias); | |
1553 | ||
1554 | /* Add alias to region name list. */ | |
1555 | n = stat_alloc (sizeof (lang_memory_region_name)); | |
1556 | n->name = stat_strdup (alias); | |
1557 | n->next = region->name_list.next; | |
1558 | region->name_list.next = n; | |
1559 | } | |
1560 | ||
1561 | static lang_memory_region_type * | |
1562 | lang_memory_default (asection *section) | |
1563 | { | |
1564 | lang_memory_region_type *p; | |
1565 | ||
1566 | flagword sec_flags = section->flags; | |
1567 | ||
1568 | /* Override SEC_DATA to mean a writable section. */ | |
1569 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1570 | sec_flags |= SEC_DATA; | |
1571 | ||
1572 | for (p = lang_memory_region_list; p != NULL; p = p->next) | |
1573 | { | |
1574 | if ((p->flags & sec_flags) != 0 | |
1575 | && (p->not_flags & sec_flags) == 0) | |
1576 | { | |
1577 | return p; | |
1578 | } | |
1579 | } | |
1580 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); | |
1581 | } | |
1582 | ||
1583 | /* Get the output section statement directly from the userdata. */ | |
1584 | ||
1585 | lang_output_section_statement_type * | |
1586 | lang_output_section_get (const asection *output_section) | |
1587 | { | |
1588 | return bfd_section_userdata (output_section); | |
1589 | } | |
1590 | ||
1591 | /* Find or create an output_section_statement with the given NAME. | |
1592 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
1593 | match any non-negative constraint. If CREATE is 0 return NULL when | |
1594 | no match exists. If CREATE is 1, create an output_section_statement | |
1595 | when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2, | |
1596 | always make a new output_section_statement. */ | |
1597 | ||
1598 | lang_output_section_statement_type * | |
1599 | lang_output_section_statement_lookup (const char *name, | |
1600 | int constraint, | |
1601 | int create) | |
1602 | { | |
1603 | struct out_section_hash_entry *entry; | |
1604 | ||
1605 | entry = ((struct out_section_hash_entry *) | |
1606 | bfd_hash_lookup (&output_section_statement_table, name, | |
1607 | create != 0, false)); | |
1608 | if (entry == NULL) | |
1609 | { | |
1610 | if (create) | |
1611 | fatal (_("%P: failed creating section `%s': %E\n"), name); | |
1612 | return NULL; | |
1613 | } | |
1614 | ||
1615 | if (entry->s.output_section_statement.name != NULL) | |
1616 | { | |
1617 | /* We have a section of this name, but it might not have the correct | |
1618 | constraint. */ | |
1619 | struct out_section_hash_entry *first_ent = entry; | |
1620 | struct out_section_hash_entry *last_ent; | |
1621 | ||
1622 | name = entry->s.output_section_statement.name; | |
1623 | if (create != 2 | |
1624 | && !(create && constraint == SPECIAL)) | |
1625 | { | |
1626 | do | |
1627 | { | |
1628 | if (constraint == entry->s.output_section_statement.constraint | |
1629 | || (constraint == 0 | |
1630 | && entry->s.output_section_statement.constraint >= 0)) | |
1631 | return &entry->s.output_section_statement; | |
1632 | last_ent = entry; | |
1633 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1634 | } | |
1635 | while (entry != NULL | |
1636 | && name == entry->s.output_section_statement.name); | |
1637 | } | |
1638 | else | |
1639 | last_ent = first_ent->tail; | |
1640 | ||
1641 | if (!create) | |
1642 | return NULL; | |
1643 | ||
1644 | /* Only the first entry needs the tail pointer. */ | |
1645 | entry = bfd_hash_allocate (&output_section_statement_table, | |
1646 | offsetof (struct out_section_hash_entry, tail)); | |
1647 | if (entry != NULL) | |
1648 | entry | |
1649 | = output_section_statement_newfunc_1 (&entry->root, | |
1650 | &output_section_statement_table, | |
1651 | name); | |
1652 | if (entry == NULL) | |
1653 | { | |
1654 | fatal (_("%P: failed creating section `%s': %E\n"), name); | |
1655 | return NULL; | |
1656 | } | |
1657 | entry->root = last_ent->root; | |
1658 | last_ent->root.next = &entry->root; | |
1659 | first_ent->tail = entry; | |
1660 | } | |
1661 | ||
1662 | entry->s.output_section_statement.name = name; | |
1663 | entry->s.output_section_statement.constraint = constraint; | |
1664 | entry->s.output_section_statement.dup_output = (create == 2 | |
1665 | || constraint == SPECIAL); | |
1666 | return &entry->s.output_section_statement; | |
1667 | } | |
1668 | ||
1669 | /* Find the next output_section_statement with the same name as OS. | |
1670 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1671 | match any non-negative constraint. */ | |
1672 | ||
1673 | lang_output_section_statement_type * | |
1674 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1675 | int constraint) | |
1676 | { | |
1677 | /* All output_section_statements are actually part of a | |
1678 | struct out_section_hash_entry. */ | |
1679 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1680 | ((char *) os | |
1681 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1682 | const char *name = os->name; | |
1683 | ||
1684 | ASSERT (name == entry->root.string); | |
1685 | do | |
1686 | { | |
1687 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1688 | if (entry == NULL | |
1689 | || name != entry->s.output_section_statement.name) | |
1690 | return NULL; | |
1691 | } | |
1692 | while (constraint != entry->s.output_section_statement.constraint | |
1693 | && (constraint != 0 | |
1694 | || entry->s.output_section_statement.constraint < 0)); | |
1695 | ||
1696 | return &entry->s.output_section_statement; | |
1697 | } | |
1698 | ||
1699 | /* A variant of lang_output_section_find used by place_orphan. | |
1700 | Returns the output statement that should precede a new output | |
1701 | statement for SEC. If an exact match is found on certain flags, | |
1702 | sets *EXACT too. */ | |
1703 | ||
1704 | lang_output_section_statement_type * | |
1705 | lang_output_section_find_by_flags (const asection *sec, | |
1706 | flagword sec_flags, | |
1707 | lang_output_section_statement_type **exact, | |
1708 | lang_match_sec_type_func match_type) | |
1709 | { | |
1710 | lang_output_section_statement_type *first, *look, *found; | |
1711 | flagword look_flags, differ; | |
1712 | ||
1713 | /* We know the first statement on this list is *ABS*. May as well | |
1714 | skip it. */ | |
1715 | first = (void *) lang_os_list.head; | |
1716 | first = first->next; | |
1717 | ||
1718 | /* First try for an exact match. */ | |
1719 | found = NULL; | |
1720 | for (look = first; look; look = look->next) | |
1721 | { | |
1722 | look_flags = look->flags; | |
1723 | if (look->bfd_section != NULL) | |
1724 | { | |
1725 | look_flags = look->bfd_section->flags; | |
1726 | if (match_type && !match_type (link_info.output_bfd, | |
1727 | look->bfd_section, | |
1728 | sec->owner, sec)) | |
1729 | continue; | |
1730 | } | |
1731 | differ = look_flags ^ sec_flags; | |
1732 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1733 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1734 | found = look; | |
1735 | } | |
1736 | if (found != NULL) | |
1737 | { | |
1738 | if (exact != NULL) | |
1739 | *exact = found; | |
1740 | return found; | |
1741 | } | |
1742 | ||
1743 | if ((sec_flags & SEC_CODE) != 0 | |
1744 | && (sec_flags & SEC_ALLOC) != 0) | |
1745 | { | |
1746 | /* Try for a rw code section. */ | |
1747 | for (look = first; look; look = look->next) | |
1748 | { | |
1749 | look_flags = look->flags; | |
1750 | if (look->bfd_section != NULL) | |
1751 | { | |
1752 | look_flags = look->bfd_section->flags; | |
1753 | if (match_type && !match_type (link_info.output_bfd, | |
1754 | look->bfd_section, | |
1755 | sec->owner, sec)) | |
1756 | continue; | |
1757 | } | |
1758 | differ = look_flags ^ sec_flags; | |
1759 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1760 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1761 | found = look; | |
1762 | } | |
1763 | } | |
1764 | else if ((sec_flags & SEC_READONLY) != 0 | |
1765 | && (sec_flags & SEC_ALLOC) != 0) | |
1766 | { | |
1767 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1768 | for (look = first; look; look = look->next) | |
1769 | { | |
1770 | look_flags = look->flags; | |
1771 | if (look->bfd_section != NULL) | |
1772 | { | |
1773 | look_flags = look->bfd_section->flags; | |
1774 | if (match_type && !match_type (link_info.output_bfd, | |
1775 | look->bfd_section, | |
1776 | sec->owner, sec)) | |
1777 | continue; | |
1778 | } | |
1779 | differ = look_flags ^ sec_flags; | |
1780 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1781 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1782 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1783 | | SEC_READONLY)) | |
1784 | && !(look_flags & SEC_SMALL_DATA))) | |
1785 | found = look; | |
1786 | } | |
1787 | } | |
1788 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 | |
1789 | && (sec_flags & SEC_ALLOC) != 0) | |
1790 | { | |
1791 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1792 | as if it were a loaded section, and don't use match_type. */ | |
1793 | bool seen_thread_local = false; | |
1794 | ||
1795 | match_type = NULL; | |
1796 | for (look = first; look; look = look->next) | |
1797 | { | |
1798 | look_flags = look->flags; | |
1799 | if (look->bfd_section != NULL) | |
1800 | look_flags = look->bfd_section->flags; | |
1801 | ||
1802 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1803 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1804 | { | |
1805 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1806 | if (!(look_flags & SEC_LOAD) | |
1807 | && (sec_flags & SEC_LOAD)) | |
1808 | /* ..so if we're at a .tbss section and we're placing | |
1809 | a .tdata section stop looking and return the | |
1810 | previous section. */ | |
1811 | break; | |
1812 | found = look; | |
1813 | seen_thread_local = true; | |
1814 | } | |
1815 | else if (seen_thread_local) | |
1816 | break; | |
1817 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1818 | found = look; | |
1819 | } | |
1820 | } | |
1821 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1822 | && (sec_flags & SEC_ALLOC) != 0) | |
1823 | { | |
1824 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1825 | for (look = first; look; look = look->next) | |
1826 | { | |
1827 | look_flags = look->flags; | |
1828 | if (look->bfd_section != NULL) | |
1829 | { | |
1830 | look_flags = look->bfd_section->flags; | |
1831 | if (match_type && !match_type (link_info.output_bfd, | |
1832 | look->bfd_section, | |
1833 | sec->owner, sec)) | |
1834 | continue; | |
1835 | } | |
1836 | differ = look_flags ^ sec_flags; | |
1837 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1838 | | SEC_THREAD_LOCAL)) | |
1839 | || ((look_flags & SEC_SMALL_DATA) | |
1840 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
1841 | found = look; | |
1842 | } | |
1843 | } | |
1844 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 | |
1845 | && (sec_flags & SEC_ALLOC) != 0) | |
1846 | { | |
1847 | /* .data goes after .rodata. */ | |
1848 | for (look = first; look; look = look->next) | |
1849 | { | |
1850 | look_flags = look->flags; | |
1851 | if (look->bfd_section != NULL) | |
1852 | { | |
1853 | look_flags = look->bfd_section->flags; | |
1854 | if (match_type && !match_type (link_info.output_bfd, | |
1855 | look->bfd_section, | |
1856 | sec->owner, sec)) | |
1857 | continue; | |
1858 | } | |
1859 | differ = look_flags ^ sec_flags; | |
1860 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1861 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
1862 | found = look; | |
1863 | } | |
1864 | } | |
1865 | else if ((sec_flags & SEC_ALLOC) != 0) | |
1866 | { | |
1867 | /* .bss goes after any other alloc section. */ | |
1868 | for (look = first; look; look = look->next) | |
1869 | { | |
1870 | look_flags = look->flags; | |
1871 | if (look->bfd_section != NULL) | |
1872 | { | |
1873 | look_flags = look->bfd_section->flags; | |
1874 | if (match_type && !match_type (link_info.output_bfd, | |
1875 | look->bfd_section, | |
1876 | sec->owner, sec)) | |
1877 | continue; | |
1878 | } | |
1879 | differ = look_flags ^ sec_flags; | |
1880 | if (!(differ & SEC_ALLOC)) | |
1881 | found = look; | |
1882 | } | |
1883 | } | |
1884 | else | |
1885 | { | |
1886 | /* non-alloc go last. */ | |
1887 | for (look = first; look; look = look->next) | |
1888 | { | |
1889 | look_flags = look->flags; | |
1890 | if (look->bfd_section != NULL) | |
1891 | look_flags = look->bfd_section->flags; | |
1892 | differ = look_flags ^ sec_flags; | |
1893 | if (!(differ & SEC_DEBUGGING)) | |
1894 | found = look; | |
1895 | } | |
1896 | return found; | |
1897 | } | |
1898 | ||
1899 | if (found || !match_type) | |
1900 | return found; | |
1901 | ||
1902 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); | |
1903 | } | |
1904 | ||
1905 | /* Find the last output section before given output statement. | |
1906 | Used by place_orphan. */ | |
1907 | ||
1908 | static asection * | |
1909 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1910 | { | |
1911 | lang_output_section_statement_type *lookup; | |
1912 | ||
1913 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) | |
1914 | { | |
1915 | if (lookup->constraint < 0) | |
1916 | continue; | |
1917 | ||
1918 | if (lookup->bfd_section != NULL) | |
1919 | return lookup->bfd_section; | |
1920 | } | |
1921 | ||
1922 | return NULL; | |
1923 | } | |
1924 | ||
1925 | /* Look for a suitable place for a new output section statement. The | |
1926 | idea is to skip over anything that might be inside a SECTIONS {} | |
1927 | statement in a script, before we find another output section | |
1928 | statement. Assignments to "dot" before an output section statement | |
1929 | are assumed to belong to it, except in two cases; The first | |
1930 | assignment to dot, and assignments before non-alloc sections. | |
1931 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1932 | similar assignments that set the initial address, or we might | |
1933 | insert non-alloc note sections among assignments setting end of | |
1934 | image symbols. */ | |
1935 | ||
1936 | static lang_statement_union_type ** | |
1937 | insert_os_after (lang_statement_union_type *after) | |
1938 | { | |
1939 | lang_statement_union_type **where; | |
1940 | lang_statement_union_type **assign = NULL; | |
1941 | bool ignore_first; | |
1942 | ||
1943 | ignore_first = after == lang_os_list.head; | |
1944 | ||
1945 | for (where = &after->header.next; | |
1946 | *where != NULL; | |
1947 | where = &(*where)->header.next) | |
1948 | { | |
1949 | switch ((*where)->header.type) | |
1950 | { | |
1951 | case lang_assignment_statement_enum: | |
1952 | if (assign == NULL) | |
1953 | { | |
1954 | lang_assignment_statement_type *ass; | |
1955 | ||
1956 | ass = &(*where)->assignment_statement; | |
1957 | if (ass->exp->type.node_class != etree_assert | |
1958 | && ass->exp->assign.dst[0] == '.' | |
1959 | && ass->exp->assign.dst[1] == 0) | |
1960 | { | |
1961 | if (!ignore_first) | |
1962 | assign = where; | |
1963 | ignore_first = false; | |
1964 | } | |
1965 | } | |
1966 | continue; | |
1967 | case lang_wild_statement_enum: | |
1968 | case lang_input_section_enum: | |
1969 | case lang_object_symbols_statement_enum: | |
1970 | case lang_fill_statement_enum: | |
1971 | case lang_data_statement_enum: | |
1972 | case lang_reloc_statement_enum: | |
1973 | case lang_padding_statement_enum: | |
1974 | case lang_constructors_statement_enum: | |
1975 | assign = NULL; | |
1976 | ignore_first = false; | |
1977 | continue; | |
1978 | case lang_output_section_statement_enum: | |
1979 | if (assign != NULL) | |
1980 | { | |
1981 | asection *s = (*where)->output_section_statement.bfd_section; | |
1982 | ||
1983 | if (s == NULL | |
1984 | || s->map_head.s == NULL | |
1985 | || (s->flags & SEC_ALLOC) != 0) | |
1986 | where = assign; | |
1987 | } | |
1988 | break; | |
1989 | case lang_input_statement_enum: | |
1990 | case lang_address_statement_enum: | |
1991 | case lang_target_statement_enum: | |
1992 | case lang_output_statement_enum: | |
1993 | case lang_group_statement_enum: | |
1994 | case lang_insert_statement_enum: | |
1995 | continue; | |
1996 | case lang_input_matcher_enum: | |
1997 | FAIL (); | |
1998 | } | |
1999 | break; | |
2000 | } | |
2001 | ||
2002 | return where; | |
2003 | } | |
2004 | ||
2005 | lang_output_section_statement_type * | |
2006 | lang_insert_orphan (asection *s, | |
2007 | const char *secname, | |
2008 | int constraint, | |
2009 | lang_output_section_statement_type *after, | |
2010 | struct orphan_save *place, | |
2011 | etree_type *address, | |
2012 | lang_statement_list_type *add_child) | |
2013 | { | |
2014 | lang_statement_list_type add; | |
2015 | lang_output_section_statement_type *os; | |
2016 | lang_output_section_statement_type **os_tail; | |
2017 | ||
2018 | /* If we have found an appropriate place for the output section | |
2019 | statements for this orphan, add them to our own private list, | |
2020 | inserting them later into the global statement list. */ | |
2021 | if (after != NULL) | |
2022 | { | |
2023 | lang_list_init (&add); | |
2024 | push_stat_ptr (&add); | |
2025 | } | |
2026 | ||
2027 | if (bfd_link_relocatable (&link_info) | |
2028 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
2029 | address = exp_intop (0); | |
2030 | ||
2031 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; | |
2032 | os = lang_enter_output_section_statement ( | |
2033 | secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); | |
2034 | ||
2035 | if (add_child == NULL) | |
2036 | add_child = &os->children; | |
2037 | lang_add_section (add_child, s, NULL, NULL, os); | |
2038 | ||
2039 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) | |
2040 | { | |
2041 | const char *region = (after->region | |
2042 | ? after->region->name_list.name | |
2043 | : DEFAULT_MEMORY_REGION); | |
2044 | const char *lma_region = (after->lma_region | |
2045 | ? after->lma_region->name_list.name | |
2046 | : NULL); | |
2047 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
2048 | lma_region); | |
2049 | } | |
2050 | else | |
2051 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
2052 | NULL); | |
2053 | ||
2054 | /* Restore the global list pointer. */ | |
2055 | if (after != NULL) | |
2056 | pop_stat_ptr (); | |
2057 | ||
2058 | if (after != NULL && os->bfd_section != NULL) | |
2059 | { | |
2060 | asection *snew, *as; | |
2061 | bool place_after = place->stmt == NULL; | |
2062 | bool insert_after = true; | |
2063 | ||
2064 | snew = os->bfd_section; | |
2065 | ||
2066 | /* Shuffle the bfd section list to make the output file look | |
2067 | neater. This is really only cosmetic. */ | |
2068 | if (place->section == NULL | |
2069 | && after != (void *) lang_os_list.head) | |
2070 | { | |
2071 | asection *bfd_section = after->bfd_section; | |
2072 | ||
2073 | /* If the output statement hasn't been used to place any input | |
2074 | sections (and thus doesn't have an output bfd_section), | |
2075 | look for the closest prior output statement having an | |
2076 | output section. */ | |
2077 | if (bfd_section == NULL) | |
2078 | bfd_section = output_prev_sec_find (after); | |
2079 | ||
2080 | if (bfd_section != NULL | |
2081 | && bfd_section->owner != NULL | |
2082 | && bfd_section != snew) | |
2083 | place->section = &bfd_section->next; | |
2084 | } | |
2085 | ||
2086 | if (place->section == NULL) | |
2087 | place->section = &link_info.output_bfd->sections; | |
2088 | ||
2089 | as = *place->section; | |
2090 | ||
2091 | if (!as) | |
2092 | { | |
2093 | /* Put the section at the end of the list. */ | |
2094 | ||
2095 | /* Unlink the section. */ | |
2096 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2097 | ||
2098 | /* Now tack it back on in the right place. */ | |
2099 | bfd_section_list_append (link_info.output_bfd, snew); | |
2100 | } | |
2101 | else if ((bfd_get_flavour (link_info.output_bfd) | |
2102 | == bfd_target_elf_flavour) | |
2103 | && (bfd_get_flavour (s->owner) | |
2104 | == bfd_target_elf_flavour) | |
2105 | && ((elf_section_type (s) == SHT_NOTE | |
2106 | && (s->flags & SEC_LOAD) != 0) | |
2107 | || (elf_section_type (as) == SHT_NOTE | |
2108 | && (as->flags & SEC_LOAD) != 0))) | |
2109 | { | |
2110 | /* Make sure that output note sections are grouped and sorted | |
2111 | by alignments when inserting a note section or insert a | |
2112 | section after a note section, */ | |
2113 | asection *sec; | |
2114 | /* A specific section after which the output note section | |
2115 | should be placed. */ | |
2116 | asection *after_sec; | |
2117 | /* True if we need to insert the orphan section after a | |
2118 | specific section to maintain output note section order. */ | |
2119 | bool after_sec_note = false; | |
2120 | ||
2121 | static asection *first_orphan_note = NULL; | |
2122 | ||
2123 | /* Group and sort output note section by alignments in | |
2124 | ascending order. */ | |
2125 | after_sec = NULL; | |
2126 | if (elf_section_type (s) == SHT_NOTE | |
2127 | && (s->flags & SEC_LOAD) != 0) | |
2128 | { | |
2129 | /* Search from the beginning for the last output note | |
2130 | section with equal or larger alignments. NB: Don't | |
2131 | place orphan note section after non-note sections. */ | |
2132 | ||
2133 | first_orphan_note = NULL; | |
2134 | ||
2135 | /* NB: When --rosegment is used, the .note.gnu.build-id | |
2136 | section is placed before text sections. Ignore the | |
2137 | .note.gnu.build-id section if -z separate-code and | |
2138 | --rosegment are used together to avoid putting any | |
2139 | note sections between the .note.gnu.build-id section | |
2140 | and text sections in the same PT_LOAD segment. */ | |
2141 | bool ignore_build_id = (link_info.separate_code | |
2142 | && link_info.one_rosegment); | |
2143 | ||
2144 | for (sec = link_info.output_bfd->sections; | |
2145 | (sec != NULL | |
2146 | && !bfd_is_abs_section (sec)); | |
2147 | sec = sec->next) | |
2148 | if (sec != snew | |
2149 | && elf_section_type (sec) == SHT_NOTE | |
2150 | && (sec->flags & SEC_LOAD) != 0 | |
2151 | && (!ignore_build_id | |
2152 | || strcmp (sec->name, ".note.gnu.build-id") != 0)) | |
2153 | { | |
2154 | if (!first_orphan_note) | |
2155 | first_orphan_note = sec; | |
2156 | if (sec->alignment_power >= s->alignment_power) | |
2157 | after_sec = sec; | |
2158 | } | |
2159 | else if (first_orphan_note) | |
2160 | { | |
2161 | /* Stop if there is non-note section after the first | |
2162 | orphan note section. */ | |
2163 | break; | |
2164 | } | |
2165 | ||
2166 | /* If this will be the first orphan note section, it can | |
2167 | be placed at the default location. */ | |
2168 | after_sec_note = first_orphan_note != NULL; | |
2169 | if (after_sec == NULL && after_sec_note) | |
2170 | { | |
2171 | /* If all output note sections have smaller | |
2172 | alignments, place the section before all | |
2173 | output orphan note sections. */ | |
2174 | after_sec = first_orphan_note; | |
2175 | insert_after = false; | |
2176 | } | |
2177 | } | |
2178 | else if (first_orphan_note) | |
2179 | { | |
2180 | /* Don't place non-note sections in the middle of orphan | |
2181 | note sections. */ | |
2182 | after_sec_note = true; | |
2183 | after_sec = as; | |
2184 | for (sec = as->next; | |
2185 | (sec != NULL | |
2186 | && !bfd_is_abs_section (sec)); | |
2187 | sec = sec->next) | |
2188 | if (elf_section_type (sec) == SHT_NOTE | |
2189 | && (sec->flags & SEC_LOAD) != 0) | |
2190 | after_sec = sec; | |
2191 | } | |
2192 | ||
2193 | if (after_sec_note) | |
2194 | { | |
2195 | if (after_sec) | |
2196 | { | |
2197 | /* Search forward to insert OS after AFTER_SEC output | |
2198 | statement. */ | |
2199 | lang_output_section_statement_type *stmt, *next; | |
2200 | bool found = false; | |
2201 | for (stmt = after; stmt != NULL; stmt = next) | |
2202 | { | |
2203 | next = stmt->next; | |
2204 | if (insert_after) | |
2205 | { | |
2206 | if (stmt->bfd_section == after_sec) | |
2207 | { | |
2208 | place_after = true; | |
2209 | found = true; | |
2210 | after = stmt; | |
2211 | break; | |
2212 | } | |
2213 | } | |
2214 | else | |
2215 | { | |
2216 | /* If INSERT_AFTER is FALSE, place OS before | |
2217 | AFTER_SEC output statement. */ | |
2218 | if (next && next->bfd_section == after_sec) | |
2219 | { | |
2220 | place_after = true; | |
2221 | found = true; | |
2222 | after = stmt; | |
2223 | break; | |
2224 | } | |
2225 | } | |
2226 | } | |
2227 | ||
2228 | /* Search backward to insert OS after AFTER_SEC output | |
2229 | statement. */ | |
2230 | if (!found) | |
2231 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
2232 | { | |
2233 | if (insert_after) | |
2234 | { | |
2235 | if (stmt->bfd_section == after_sec) | |
2236 | { | |
2237 | place_after = true; | |
2238 | after = stmt; | |
2239 | break; | |
2240 | } | |
2241 | } | |
2242 | else | |
2243 | { | |
2244 | /* If INSERT_AFTER is FALSE, place OS before | |
2245 | AFTER_SEC output statement. */ | |
2246 | if (stmt->next->bfd_section == after_sec) | |
2247 | { | |
2248 | place_after = true; | |
2249 | after = stmt; | |
2250 | break; | |
2251 | } | |
2252 | } | |
2253 | } | |
2254 | } | |
2255 | ||
2256 | if (after_sec == NULL | |
2257 | || (insert_after && after_sec->next != snew) | |
2258 | || (!insert_after && after_sec->prev != snew)) | |
2259 | { | |
2260 | /* Unlink the section. */ | |
2261 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2262 | ||
2263 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2264 | prepend SNEW. */ | |
2265 | if (after_sec) | |
2266 | { | |
2267 | if (insert_after) | |
2268 | bfd_section_list_insert_after (link_info.output_bfd, | |
2269 | after_sec, snew); | |
2270 | else | |
2271 | bfd_section_list_insert_before (link_info.output_bfd, | |
2272 | after_sec, snew); | |
2273 | } | |
2274 | else | |
2275 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2276 | } | |
2277 | } | |
2278 | else if (as != snew && as->prev != snew) | |
2279 | { | |
2280 | /* Unlink the section. */ | |
2281 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2282 | ||
2283 | /* Now tack it back on in the right place. */ | |
2284 | bfd_section_list_insert_before (link_info.output_bfd, | |
2285 | as, snew); | |
2286 | } | |
2287 | } | |
2288 | else if (as != snew && as->prev != snew) | |
2289 | { | |
2290 | /* Unlink the section. */ | |
2291 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2292 | ||
2293 | /* Now tack it back on in the right place. */ | |
2294 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); | |
2295 | } | |
2296 | ||
2297 | /* Save the end of this list. Further ophans of this type will | |
2298 | follow the one we've just added. */ | |
2299 | place->section = &snew->next; | |
2300 | ||
2301 | /* The following is non-cosmetic. We try to put the output | |
2302 | statements in some sort of reasonable order here, because they | |
2303 | determine the final load addresses of the orphan sections. | |
2304 | In addition, placing output statements in the wrong order may | |
2305 | require extra segments. For instance, given a typical | |
2306 | situation of all read-only sections placed in one segment and | |
2307 | following that a segment containing all the read-write | |
2308 | sections, we wouldn't want to place an orphan read/write | |
2309 | section before or amongst the read-only ones. */ | |
2310 | if (add.head != NULL) | |
2311 | { | |
2312 | lang_output_section_statement_type *newly_added_os; | |
2313 | ||
2314 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ | |
2315 | if (place_after) | |
2316 | { | |
2317 | lang_statement_union_type **where; | |
2318 | ||
2319 | where = insert_os_after ((lang_statement_union_type *) after); | |
2320 | *add.tail = *where; | |
2321 | *where = add.head; | |
2322 | ||
2323 | place->os_tail = &after->next; | |
2324 | } | |
2325 | else | |
2326 | { | |
2327 | /* Put it after the last orphan statement we added. */ | |
2328 | *add.tail = *place->stmt; | |
2329 | *place->stmt = add.head; | |
2330 | } | |
2331 | ||
2332 | /* Fix the global list pointer if we happened to tack our | |
2333 | new list at the tail. */ | |
2334 | if (*stat_ptr->tail == add.head) | |
2335 | stat_ptr->tail = add.tail; | |
2336 | ||
2337 | /* Save the end of this list. */ | |
2338 | place->stmt = add.tail; | |
2339 | ||
2340 | /* Do the same for the list of output section statements. */ | |
2341 | newly_added_os = *os_tail; | |
2342 | *os_tail = NULL; | |
2343 | newly_added_os->prev = (lang_output_section_statement_type *) | |
2344 | ((char *) place->os_tail | |
2345 | - offsetof (lang_output_section_statement_type, next)); | |
2346 | newly_added_os->next = *place->os_tail; | |
2347 | if (newly_added_os->next != NULL) | |
2348 | newly_added_os->next->prev = newly_added_os; | |
2349 | *place->os_tail = newly_added_os; | |
2350 | place->os_tail = &newly_added_os->next; | |
2351 | ||
2352 | /* Fixing the global list pointer here is a little different. | |
2353 | We added to the list in lang_enter_output_section_statement, | |
2354 | trimmed off the new output_section_statment above when | |
2355 | assigning *os_tail = NULL, but possibly added it back in | |
2356 | the same place when assigning *place->os_tail. */ | |
2357 | if (*os_tail == NULL) | |
2358 | lang_os_list.tail = (lang_statement_union_type **) os_tail; | |
2359 | } | |
2360 | } | |
2361 | return os; | |
2362 | } | |
2363 | ||
2364 | static void | |
2365 | lang_print_asneeded (void) | |
2366 | { | |
2367 | struct asneeded_minfo *m; | |
2368 | ||
2369 | if (asneeded_list_head == NULL) | |
2370 | return; | |
2371 | ||
2372 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); | |
2373 | ||
2374 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2375 | { | |
2376 | int len; | |
2377 | ||
2378 | minfo ("%s", m->soname); | |
2379 | len = strlen (m->soname); | |
2380 | ||
2381 | if (len >= 29) | |
2382 | { | |
2383 | print_nl (); | |
2384 | len = 0; | |
2385 | } | |
2386 | print_spaces (30 - len); | |
2387 | ||
2388 | if (m->ref != NULL) | |
2389 | minfo ("%pB ", m->ref); | |
2390 | minfo ("(%pT)\n", m->name); | |
2391 | } | |
2392 | } | |
2393 | ||
2394 | static void | |
2395 | lang_map_flags (flagword flag) | |
2396 | { | |
2397 | if (flag & SEC_ALLOC) | |
2398 | minfo ("a"); | |
2399 | ||
2400 | if (flag & SEC_CODE) | |
2401 | minfo ("x"); | |
2402 | ||
2403 | if (flag & SEC_READONLY) | |
2404 | minfo ("r"); | |
2405 | ||
2406 | if (flag & SEC_DATA) | |
2407 | minfo ("w"); | |
2408 | ||
2409 | if (flag & SEC_LOAD) | |
2410 | minfo ("l"); | |
2411 | } | |
2412 | ||
2413 | void | |
2414 | lang_map (void) | |
2415 | { | |
2416 | lang_memory_region_type *m; | |
2417 | bool dis_header_printed = false; | |
2418 | ||
2419 | ldfile_print_input_remaps (); | |
2420 | ||
2421 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
2422 | { | |
2423 | asection *s; | |
2424 | ||
2425 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
2426 | || file->flags.just_syms) | |
2427 | continue; | |
2428 | ||
2429 | if (config.print_map_discarded) | |
2430 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2431 | if ((s->output_section == NULL | |
2432 | || s->output_section->owner != link_info.output_bfd) | |
2433 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2434 | { | |
2435 | if (! dis_header_printed) | |
2436 | { | |
2437 | minfo (_("\nDiscarded input sections\n\n")); | |
2438 | dis_header_printed = true; | |
2439 | } | |
2440 | ||
2441 | print_input_section (s, true); | |
2442 | } | |
2443 | } | |
2444 | if (config.print_map_discarded && ! dis_header_printed) | |
2445 | minfo (_("\nThere are no discarded input sections\n")); | |
2446 | ||
2447 | minfo (_("\nMemory Configuration\n\n")); | |
2448 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2449 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2450 | ||
2451 | for (m = lang_memory_region_list; m != NULL; m = m->next) | |
2452 | { | |
2453 | fprintf (config.map_file, "%-16s", m->name_list.name); | |
2454 | ||
2455 | char buf[32]; | |
2456 | bfd_sprintf_vma (link_info.output_bfd, buf, m->origin); | |
2457 | fprintf (config.map_file, " 0x%-16s", buf); | |
2458 | bfd_sprintf_vma (link_info.output_bfd, buf, m->length); | |
2459 | fprintf (config.map_file, | |
2460 | " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf); | |
2461 | if (m->flags) | |
2462 | lang_map_flags (m->flags); | |
2463 | ||
2464 | if (m->not_flags) | |
2465 | { | |
2466 | minfo ("!"); | |
2467 | lang_map_flags (m->not_flags); | |
2468 | } | |
2469 | ||
2470 | print_nl (); | |
2471 | } | |
2472 | ||
2473 | minfo (_("\nLinker script and memory map\n\n")); | |
2474 | ||
2475 | if (!link_info.reduce_memory_overheads) | |
2476 | { | |
2477 | obstack_begin (&map_obstack, 1000); | |
2478 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); | |
2479 | } | |
2480 | expld.phase = lang_fixed_phase_enum; | |
2481 | lang_statement_iteration++; | |
2482 | print_statements (); | |
2483 | ||
2484 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, | |
2485 | config.map_file); | |
2486 | } | |
2487 | ||
2488 | static bool | |
2489 | is_defined (struct bfd_link_hash_entry *h) | |
2490 | { | |
2491 | return h != NULL | |
2492 | && (h->type == bfd_link_hash_defined | |
2493 | || h->type == bfd_link_hash_defweak); | |
2494 | } | |
2495 | ||
2496 | static bool | |
2497 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, | |
2498 | void *info ATTRIBUTE_UNUSED) | |
2499 | { | |
2500 | if (is_defined (hash_entry) | |
2501 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2502 | && hash_entry->u.def.section->owner != NULL) | |
2503 | { | |
2504 | input_section_userdata_type *ud; | |
2505 | struct map_symbol_def *def; | |
2506 | ||
2507 | ud = bfd_section_userdata (hash_entry->u.def.section); | |
2508 | if (!ud) | |
2509 | { | |
2510 | ud = stat_alloc (sizeof (*ud)); | |
2511 | bfd_set_section_userdata (hash_entry->u.def.section, ud); | |
2512 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; | |
2513 | ud->map_symbol_def_count = 0; | |
2514 | } | |
2515 | else if (!ud->map_symbol_def_tail) | |
2516 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; | |
2517 | ||
2518 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); | |
2519 | def->entry = hash_entry; | |
2520 | *(ud->map_symbol_def_tail) = def; | |
2521 | ud->map_symbol_def_tail = &def->next; | |
2522 | ud->map_symbol_def_count++; | |
2523 | } | |
2524 | return true; | |
2525 | } | |
2526 | ||
2527 | /* Initialize an output section. */ | |
2528 | ||
2529 | static void | |
2530 | init_os (lang_output_section_statement_type *s, flagword flags) | |
2531 | { | |
2532 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) | |
2533 | fatal (_("%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); | |
2534 | ||
2535 | if (!s->dup_output) | |
2536 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); | |
2537 | if (s->bfd_section == NULL) | |
2538 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, | |
2539 | s->name, flags); | |
2540 | if (s->bfd_section == NULL) | |
2541 | { | |
2542 | fatal (_("%P: output format %s cannot represent section" | |
2543 | " called %s: %E\n"), | |
2544 | link_info.output_bfd->xvec->name, s->name); | |
2545 | } | |
2546 | s->bfd_section->output_section = s->bfd_section; | |
2547 | s->bfd_section->output_offset = 0; | |
2548 | ||
2549 | /* Set the userdata of the output section to the output section | |
2550 | statement to avoid lookup. */ | |
2551 | bfd_set_section_userdata (s->bfd_section, s); | |
2552 | ||
2553 | /* If there is a base address, make sure that any sections it might | |
2554 | mention are initialized. */ | |
2555 | if (s->addr_tree != NULL) | |
2556 | exp_init_os (s->addr_tree); | |
2557 | ||
2558 | if (s->load_base != NULL) | |
2559 | exp_init_os (s->load_base); | |
2560 | ||
2561 | /* If supplied an alignment, set it. */ | |
2562 | if (s->section_alignment != NULL) | |
2563 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, s, | |
2564 | "section alignment"); | |
2565 | } | |
2566 | ||
2567 | static flagword | |
2568 | get_os_init_flag (lang_output_section_statement_type * os) | |
2569 | { | |
2570 | if (os != NULL) | |
2571 | switch (os->sectype) | |
2572 | { | |
2573 | case readonly_section: return SEC_READONLY; | |
2574 | case noload_section: return SEC_NEVER_LOAD; | |
2575 | default: break; | |
2576 | } | |
2577 | ||
2578 | return 0; | |
2579 | } | |
2580 | ||
2581 | /* Make sure that all output sections mentioned in an expression are | |
2582 | initialized. */ | |
2583 | ||
2584 | static void | |
2585 | exp_init_os (etree_type *exp) | |
2586 | { | |
2587 | switch (exp->type.node_class) | |
2588 | { | |
2589 | case etree_assign: | |
2590 | case etree_provide: | |
2591 | case etree_provided: | |
2592 | exp_init_os (exp->assign.src); | |
2593 | break; | |
2594 | ||
2595 | case etree_binary: | |
2596 | exp_init_os (exp->binary.lhs); | |
2597 | exp_init_os (exp->binary.rhs); | |
2598 | break; | |
2599 | ||
2600 | case etree_trinary: | |
2601 | exp_init_os (exp->trinary.cond); | |
2602 | exp_init_os (exp->trinary.lhs); | |
2603 | exp_init_os (exp->trinary.rhs); | |
2604 | break; | |
2605 | ||
2606 | case etree_assert: | |
2607 | exp_init_os (exp->assert_s.child); | |
2608 | break; | |
2609 | ||
2610 | case etree_unary: | |
2611 | exp_init_os (exp->unary.child); | |
2612 | break; | |
2613 | ||
2614 | case etree_name: | |
2615 | switch (exp->type.node_code) | |
2616 | { | |
2617 | case ADDR: | |
2618 | case LOADADDR: | |
2619 | { | |
2620 | lang_output_section_statement_type *os; | |
2621 | ||
2622 | os = lang_output_section_find (exp->name.name); | |
2623 | if (os != NULL && os->bfd_section == NULL) | |
2624 | init_os (os, get_os_init_flag (os)); | |
2625 | } | |
2626 | } | |
2627 | break; | |
2628 | ||
2629 | default: | |
2630 | break; | |
2631 | } | |
2632 | } | |
2633 | \f | |
2634 | static void | |
2635 | section_already_linked (bfd *abfd, asection *sec, void *data) | |
2636 | { | |
2637 | lang_input_statement_type *entry = (lang_input_statement_type *) data; | |
2638 | ||
2639 | /* If we are only reading symbols from this object, then we want to | |
2640 | discard all sections. */ | |
2641 | if (entry->flags.just_syms) | |
2642 | { | |
2643 | bfd_link_just_syms (abfd, sec, &link_info); | |
2644 | return; | |
2645 | } | |
2646 | ||
2647 | /* Deal with SHF_EXCLUDE ELF sections. */ | |
2648 | if (!bfd_link_relocatable (&link_info) | |
2649 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2650 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2651 | sec->output_section = bfd_abs_section_ptr; | |
2652 | ||
2653 | if (!(abfd->flags & DYNAMIC)) | |
2654 | bfd_section_already_linked (abfd, sec, &link_info); | |
2655 | } | |
2656 | \f | |
2657 | ||
2658 | /* Returns true if SECTION is one we know will be discarded based on its | |
2659 | section flags, otherwise returns false. */ | |
2660 | ||
2661 | static bool | |
2662 | lang_discard_section_p (asection *section) | |
2663 | { | |
2664 | bool discard; | |
2665 | flagword flags = section->flags; | |
2666 | ||
2667 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2668 | discard = (flags & SEC_EXCLUDE) != 0; | |
2669 | ||
2670 | /* Discard the group descriptor sections when we're finally placing the | |
2671 | sections from within the group. */ | |
2672 | if ((flags & SEC_GROUP) != 0 | |
2673 | && link_info.resolve_section_groups) | |
2674 | discard = true; | |
2675 | ||
2676 | /* Discard debugging sections if we are stripping debugging | |
2677 | information. */ | |
2678 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2679 | && (flags & SEC_DEBUGGING) != 0) | |
2680 | discard = true; | |
2681 | ||
2682 | /* Discard non-alloc sections if we are stripping section headers. */ | |
2683 | else if (config.no_section_header && (flags & SEC_ALLOC) == 0) | |
2684 | discard = true; | |
2685 | ||
2686 | return discard; | |
2687 | } | |
2688 | ||
2689 | /* Return TRUE if SECTION is never going to be added to output statement | |
2690 | OUTPUT. lang_add_section() definitely won't do anything with SECTION | |
2691 | if this returns TRUE. It may do something (or not) if this returns FALSE. | |
2692 | ||
2693 | Can be used as early-out to filter matches. This may set | |
2694 | output_section of SECTION, if it was unset, to the abs section in case | |
2695 | we discover SECTION to be always discarded. This may also give | |
2696 | warning messages. */ | |
2697 | ||
2698 | static bool | |
2699 | wont_add_section_p (asection *section, | |
2700 | lang_output_section_statement_type *output) | |
2701 | { | |
2702 | bool discard; | |
2703 | ||
2704 | /* Is this section one we know should be discarded? */ | |
2705 | discard = lang_discard_section_p (section); | |
2706 | ||
2707 | /* Discard input sections which are assigned to a section named | |
2708 | DISCARD_SECTION_NAME. */ | |
2709 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
2710 | discard = true; | |
2711 | ||
2712 | if (discard) | |
2713 | { | |
2714 | if (section->output_section == NULL) | |
2715 | { | |
2716 | /* This prevents future calls from assigning this section or | |
2717 | warning about it again. */ | |
2718 | section->output_section = bfd_abs_section_ptr; | |
2719 | } | |
2720 | else if (bfd_is_abs_section (section->output_section)) | |
2721 | ; | |
2722 | else if (link_info.non_contiguous_regions_warnings) | |
2723 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
2724 | "section `%pA' from `%pB' match /DISCARD/ clause.\n"), | |
2725 | NULL, section, section->owner); | |
2726 | ||
2727 | return true; | |
2728 | } | |
2729 | ||
2730 | if (section->output_section != NULL) | |
2731 | { | |
2732 | if (!link_info.non_contiguous_regions) | |
2733 | return true; | |
2734 | ||
2735 | /* SECTION has already been handled in a special way | |
2736 | (eg. LINK_ONCE): skip it. */ | |
2737 | if (bfd_is_abs_section (section->output_section)) | |
2738 | return true; | |
2739 | ||
2740 | /* Already assigned to the same output section, do not process | |
2741 | it again, to avoid creating loops between duplicate sections | |
2742 | later. */ | |
2743 | if (section->output_section == output->bfd_section) | |
2744 | return true; | |
2745 | ||
2746 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2747 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
2748 | "change behaviour for section `%pA' from `%pB' (assigned to " | |
2749 | "%pA, but additional match: %pA)\n"), | |
2750 | NULL, section, section->owner, section->output_section, | |
2751 | output->bfd_section); | |
2752 | ||
2753 | /* SECTION has already been assigned to an output section, but | |
2754 | the user allows it to be mapped to another one in case it | |
2755 | overflows. We'll later update the actual output section in | |
2756 | size_input_section as appropriate. */ | |
2757 | } | |
2758 | ||
2759 | return false; | |
2760 | } | |
2761 | ||
2762 | /* The wild routines. | |
2763 | ||
2764 | These expand statements like *(.text) and foo.o to a list of | |
2765 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2766 | foo.o(.text, .data). */ | |
2767 | ||
2768 | /* Add SECTION to the output section OUTPUT. Do this by creating a | |
2769 | lang_input_section statement which is placed at PTR. */ | |
2770 | ||
2771 | void | |
2772 | lang_add_section (lang_statement_list_type *ptr, | |
2773 | asection *section, | |
2774 | struct wildcard_list *pattern, | |
2775 | struct flag_info *sflag_info, | |
2776 | lang_output_section_statement_type *output) | |
2777 | { | |
2778 | flagword flags = section->flags; | |
2779 | ||
2780 | lang_input_section_type *new_section; | |
2781 | bfd *abfd = link_info.output_bfd; | |
2782 | ||
2783 | if (wont_add_section_p (section, output)) | |
2784 | return; | |
2785 | ||
2786 | if (sflag_info) | |
2787 | { | |
2788 | bool keep; | |
2789 | ||
2790 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); | |
2791 | if (!keep) | |
2792 | return; | |
2793 | } | |
2794 | ||
2795 | /* We don't copy the SEC_NEVER_LOAD flag from an input section | |
2796 | to an output section, because we want to be able to include a | |
2797 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2798 | section (I don't know why we want to do this, but we do). | |
2799 | build_link_order in ldwrite.c handles this case by turning | |
2800 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2801 | flags &= ~ SEC_NEVER_LOAD; | |
2802 | ||
2803 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've | |
2804 | already been processed. One reason to do this is that on pe | |
2805 | format targets, .text$foo sections go into .text and it's odd | |
2806 | to see .text with SEC_LINK_ONCE set. */ | |
2807 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) | |
2808 | { | |
2809 | if (link_info.resolve_section_groups) | |
2810 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); | |
2811 | else | |
2812 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); | |
2813 | } | |
2814 | else if (!bfd_link_relocatable (&link_info)) | |
2815 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); | |
2816 | ||
2817 | switch (output->sectype) | |
2818 | { | |
2819 | case normal_section: | |
2820 | case overlay_section: | |
2821 | case first_overlay_section: | |
2822 | case type_section: | |
2823 | break; | |
2824 | case noalloc_section: | |
2825 | flags &= ~SEC_ALLOC; | |
2826 | break; | |
2827 | case typed_readonly_section: | |
2828 | case readonly_section: | |
2829 | flags |= SEC_READONLY; | |
2830 | break; | |
2831 | case noload_section: | |
2832 | flags &= ~SEC_LOAD; | |
2833 | flags |= SEC_NEVER_LOAD; | |
2834 | /* Unfortunately GNU ld has managed to evolve two different | |
2835 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2836 | alloc, no contents section. All others get a noload, noalloc | |
2837 | section. Unlike a .bss style section, if a note section is | |
2838 | marked as NOLOAD, also clear SEC_ALLOC. */ | |
2839 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
2840 | && elf_section_type (section) != SHT_NOTE) | |
2841 | flags &= ~SEC_HAS_CONTENTS; | |
2842 | else | |
2843 | flags &= ~SEC_ALLOC; | |
2844 | break; | |
2845 | } | |
2846 | ||
2847 | if (output->bfd_section == NULL) | |
2848 | init_os (output, flags); | |
2849 | ||
2850 | /* If SEC_READONLY is not set in the input section, then clear | |
2851 | it from the output section. */ | |
2852 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
2853 | ||
2854 | if (output->bfd_section->linker_has_input) | |
2855 | { | |
2856 | /* Only set SEC_READONLY flag on the first input section. */ | |
2857 | flags &= ~ SEC_READONLY; | |
2858 | ||
2859 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ | |
2860 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) | |
2861 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2862 | || ((flags & SEC_MERGE) != 0 | |
2863 | && output->bfd_section->entsize != section->entsize)) | |
2864 | { | |
2865 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
2866 | flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
2867 | } | |
2868 | } | |
2869 | output->bfd_section->flags |= flags; | |
2870 | ||
2871 | if (!output->bfd_section->linker_has_input) | |
2872 | { | |
2873 | output->bfd_section->linker_has_input = 1; | |
2874 | /* This must happen after flags have been updated. The output | |
2875 | section may have been created before we saw its first input | |
2876 | section, eg. for a data statement. */ | |
2877 | bfd_init_private_section_data (section->owner, section, | |
2878 | link_info.output_bfd, | |
2879 | output->bfd_section, | |
2880 | &link_info); | |
2881 | if ((flags & SEC_MERGE) != 0) | |
2882 | output->bfd_section->entsize = section->entsize; | |
2883 | } | |
2884 | ||
2885 | if ((flags & SEC_TIC54X_BLOCK) != 0 | |
2886 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2887 | { | |
2888 | /* FIXME: This value should really be obtained from the bfd... */ | |
2889 | output->block_value = 128; | |
2890 | } | |
2891 | ||
2892 | /* When a .ctors section is placed in .init_array it must be copied | |
2893 | in reverse order. Similarly for .dtors. Set that up. */ | |
2894 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
2895 | && ((startswith (section->name, ".ctors") | |
2896 | && strcmp (output->bfd_section->name, ".init_array") == 0) | |
2897 | || (startswith (section->name, ".dtors") | |
2898 | && strcmp (output->bfd_section->name, ".fini_array") == 0)) | |
2899 | && (section->name[6] == 0 || section->name[6] == '.')) | |
2900 | section->flags |= SEC_ELF_REVERSE_COPY; | |
2901 | ||
2902 | if (section->alignment_power > output->bfd_section->alignment_power) | |
2903 | output->bfd_section->alignment_power = section->alignment_power; | |
2904 | ||
2905 | section->output_section = output->bfd_section; | |
2906 | ||
2907 | if (!map_head_is_link_order) | |
2908 | { | |
2909 | asection *s = output->bfd_section->map_tail.s; | |
2910 | output->bfd_section->map_tail.s = section; | |
2911 | section->map_head.s = NULL; | |
2912 | section->map_tail.s = s; | |
2913 | if (s != NULL) | |
2914 | s->map_head.s = section; | |
2915 | else | |
2916 | output->bfd_section->map_head.s = section; | |
2917 | } | |
2918 | ||
2919 | /* Add a section reference to the list. */ | |
2920 | new_section = new_stat (lang_input_section, ptr); | |
2921 | new_section->section = section; | |
2922 | new_section->pattern = pattern; | |
2923 | } | |
2924 | ||
2925 | /* Expand a wild statement for a particular FILE. SECTION may be | |
2926 | NULL, in which case it is a wild card. This assumes that the | |
2927 | wild statement doesn't need any sorting (of filenames or sections). */ | |
2928 | ||
2929 | static void | |
2930 | output_section_callback_nosort (lang_wild_statement_type *ptr, | |
2931 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2932 | asection *section, | |
2933 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
2934 | void *output) | |
2935 | { | |
2936 | lang_output_section_statement_type *os; | |
2937 | ||
2938 | os = (lang_output_section_statement_type *) output; | |
2939 | ||
2940 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ | |
2941 | if (unique_section_p (section, os)) | |
2942 | return; | |
2943 | ||
2944 | lang_add_section (&ptr->children, section, ptr->section_list, | |
2945 | ptr->section_flag_list, os); | |
2946 | } | |
2947 | ||
2948 | /* Check if all sections in a wild statement for a particular FILE | |
2949 | are readonly. */ | |
2950 | ||
2951 | static void | |
2952 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2953 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2954 | asection *section, | |
2955 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
2956 | void *output) | |
2957 | { | |
2958 | lang_output_section_statement_type *os; | |
2959 | ||
2960 | os = (lang_output_section_statement_type *) output; | |
2961 | ||
2962 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ | |
2963 | if (unique_section_p (section, os)) | |
2964 | return; | |
2965 | ||
2966 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) | |
2967 | os->all_input_readonly = false; | |
2968 | } | |
2969 | ||
2970 | /* This is passed a file name which must have been seen already and | |
2971 | added to the statement tree. We will see if it has been opened | |
2972 | already and had its symbols read. If not then we'll read it. */ | |
2973 | ||
2974 | static lang_input_statement_type * | |
2975 | lookup_name (const char *name) | |
2976 | { | |
2977 | lang_input_statement_type *search; | |
2978 | ||
2979 | for (search = (void *) input_file_chain.head; | |
2980 | search != NULL; | |
2981 | search = search->next_real_file) | |
2982 | { | |
2983 | /* Use the local_sym_name as the name of the file that has | |
2984 | already been loaded as filename might have been transformed | |
2985 | via the search directory lookup mechanism. */ | |
2986 | const char *filename = search->local_sym_name; | |
2987 | ||
2988 | if (filename != NULL | |
2989 | && filename_cmp (filename, name) == 0) | |
2990 | break; | |
2991 | } | |
2992 | ||
2993 | if (search == NULL) | |
2994 | { | |
2995 | /* Arrange to splice the input statement added by new_afile into | |
2996 | statement_list after the current input_file_chain tail. | |
2997 | We know input_file_chain is not an empty list, and that | |
2998 | lookup_name was called via open_input_bfds. Later calls to | |
2999 | lookup_name should always match an existing input_statement. */ | |
3000 | lang_statement_union_type **tail = stat_ptr->tail; | |
3001 | lang_statement_union_type **after | |
3002 | = (void *) ((char *) input_file_chain.tail | |
3003 | - offsetof (lang_input_statement_type, next_real_file) | |
3004 | + offsetof (lang_input_statement_type, header.next)); | |
3005 | lang_statement_union_type *rest = *after; | |
3006 | stat_ptr->tail = after; | |
3007 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
3008 | default_target, NULL); | |
3009 | *stat_ptr->tail = rest; | |
3010 | if (*tail == NULL) | |
3011 | stat_ptr->tail = tail; | |
3012 | } | |
3013 | ||
3014 | /* If we have already added this file, or this file is not real | |
3015 | don't add this file. */ | |
3016 | if (search->flags.loaded || !search->flags.real) | |
3017 | return search; | |
3018 | ||
3019 | if (!load_symbols (search, NULL)) | |
3020 | return NULL; | |
3021 | ||
3022 | return search; | |
3023 | } | |
3024 | ||
3025 | /* Save LIST as a list of libraries whose symbols should not be exported. */ | |
3026 | ||
3027 | struct excluded_lib | |
3028 | { | |
3029 | char *name; | |
3030 | struct excluded_lib *next; | |
3031 | }; | |
3032 | static struct excluded_lib *excluded_libs; | |
3033 | ||
3034 | void | |
3035 | add_excluded_libs (const char *list) | |
3036 | { | |
3037 | const char *p = list, *end; | |
3038 | ||
3039 | while (*p != '\0') | |
3040 | { | |
3041 | struct excluded_lib *entry; | |
3042 | end = strpbrk (p, ",:"); | |
3043 | if (end == NULL) | |
3044 | end = p + strlen (p); | |
3045 | entry = stat_alloc (sizeof (*entry)); | |
3046 | entry->next = excluded_libs; | |
3047 | entry->name = stat_memdup (p, end - p, end - p + 1); | |
3048 | excluded_libs = entry; | |
3049 | if (*end == '\0') | |
3050 | break; | |
3051 | p = end + 1; | |
3052 | } | |
3053 | } | |
3054 | ||
3055 | static void | |
3056 | check_excluded_libs (bfd *abfd) | |
3057 | { | |
3058 | struct excluded_lib *lib = excluded_libs; | |
3059 | ||
3060 | while (lib) | |
3061 | { | |
3062 | int len = strlen (lib->name); | |
3063 | const char *filename = lbasename (bfd_get_filename (abfd)); | |
3064 | ||
3065 | if (strcmp (lib->name, "ALL") == 0) | |
3066 | { | |
3067 | abfd->no_export = true; | |
3068 | return; | |
3069 | } | |
3070 | ||
3071 | if (filename_ncmp (lib->name, filename, len) == 0 | |
3072 | && (filename[len] == '\0' | |
3073 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
3074 | && filename[len + 2] == '\0'))) | |
3075 | { | |
3076 | abfd->no_export = true; | |
3077 | return; | |
3078 | } | |
3079 | ||
3080 | lib = lib->next; | |
3081 | } | |
3082 | } | |
3083 | ||
3084 | /* Get the symbols for an input file. */ | |
3085 | ||
3086 | bool | |
3087 | load_symbols (lang_input_statement_type *entry, | |
3088 | lang_statement_list_type *place) | |
3089 | { | |
3090 | char **matching; | |
3091 | ||
3092 | if (entry->flags.loaded) | |
3093 | return true; | |
3094 | ||
3095 | ldfile_open_file (entry); | |
3096 | ||
3097 | /* Do not process further if the file was missing. */ | |
3098 | if (entry->flags.missing_file) | |
3099 | return true; | |
3100 | ||
3101 | if (trace_files || verbose) | |
3102 | info_msg ("%pI\n", entry); | |
3103 | ||
3104 | if (!bfd_check_format (entry->the_bfd, bfd_archive) | |
3105 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
3106 | { | |
3107 | bfd_error_type err; | |
3108 | struct lang_input_statement_flags save_flags; | |
3109 | extern FILE *yyin; | |
3110 | ||
3111 | err = bfd_get_error (); | |
3112 | ||
3113 | /* See if the emulation has some special knowledge. */ | |
3114 | if (ldemul_unrecognized_file (entry)) | |
3115 | { | |
3116 | if (err == bfd_error_file_ambiguously_recognized) | |
3117 | free (matching); | |
3118 | return true; | |
3119 | } | |
3120 | ||
3121 | if (err == bfd_error_file_ambiguously_recognized) | |
3122 | { | |
3123 | char **p; | |
3124 | ||
3125 | einfo (_("%P: %pB: file not recognized: %E;" | |
3126 | " matching formats:"), entry->the_bfd); | |
3127 | for (p = matching; *p != NULL; p++) | |
3128 | einfo (" %s", *p); | |
3129 | free (matching); | |
3130 | fatal ("\n"); | |
3131 | } | |
3132 | else if (err != bfd_error_file_not_recognized | |
3133 | || place == NULL) | |
3134 | fatal (_("%P: %pB: file not recognized: %E\n"), entry->the_bfd); | |
3135 | ||
3136 | bfd_close (entry->the_bfd); | |
3137 | entry->the_bfd = NULL; | |
3138 | ||
3139 | /* Try to interpret the file as a linker script. */ | |
3140 | save_flags = input_flags; | |
3141 | ldfile_open_command_file (entry->filename); | |
3142 | ||
3143 | push_stat_ptr (place); | |
3144 | input_flags.add_DT_NEEDED_for_regular | |
3145 | = entry->flags.add_DT_NEEDED_for_regular; | |
3146 | input_flags.add_DT_NEEDED_for_dynamic | |
3147 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
3148 | input_flags.whole_archive = entry->flags.whole_archive; | |
3149 | input_flags.dynamic = entry->flags.dynamic; | |
3150 | ||
3151 | ldfile_assumed_script = true; | |
3152 | parser_input = input_script; | |
3153 | current_input_file = entry->filename; | |
3154 | yyparse (); | |
3155 | current_input_file = NULL; | |
3156 | ldfile_assumed_script = false; | |
3157 | ||
3158 | /* missing_file is sticky. sysrooted will already have been | |
3159 | restored when seeing EOF in yyparse, but no harm to restore | |
3160 | again. */ | |
3161 | save_flags.missing_file |= input_flags.missing_file; | |
3162 | input_flags = save_flags; | |
3163 | pop_stat_ptr (); | |
3164 | fclose (yyin); | |
3165 | yyin = NULL; | |
3166 | entry->flags.loaded = true; | |
3167 | ||
3168 | return true; | |
3169 | } | |
3170 | ||
3171 | if (ldemul_recognized_file (entry)) | |
3172 | return true; | |
3173 | ||
3174 | /* We don't call ldlang_add_file for an archive. Instead, the | |
3175 | add_symbols entry point will call ldlang_add_file, via the | |
3176 | add_archive_element callback, for each element of the archive | |
3177 | which is used. */ | |
3178 | switch (bfd_get_format (entry->the_bfd)) | |
3179 | { | |
3180 | default: | |
3181 | break; | |
3182 | ||
3183 | case bfd_object: | |
3184 | if (!entry->flags.reload) | |
3185 | ldlang_add_file (entry); | |
3186 | break; | |
3187 | ||
3188 | case bfd_archive: | |
3189 | check_excluded_libs (entry->the_bfd); | |
3190 | ||
3191 | bfd_set_usrdata (entry->the_bfd, entry); | |
3192 | if (entry->flags.whole_archive) | |
3193 | { | |
3194 | bfd *member = NULL; | |
3195 | bool loaded = true; | |
3196 | ||
3197 | for (;;) | |
3198 | { | |
3199 | bfd *subsbfd; | |
3200 | member = bfd_openr_next_archived_file (entry->the_bfd, member); | |
3201 | ||
3202 | if (member == NULL) | |
3203 | break; | |
3204 | ||
3205 | if (!bfd_check_format (member, bfd_object)) | |
3206 | { | |
3207 | fatal (_("%P: %pB: member %pB in archive is not an object\n"), | |
3208 | entry->the_bfd, member); | |
3209 | loaded = false; | |
3210 | } | |
3211 | ||
3212 | if (config.emitting_gnu_object_only) | |
3213 | { | |
3214 | if (!cmdline_on_object_only_archive_list_p (member)) | |
3215 | continue; | |
3216 | } | |
3217 | ||
3218 | subsbfd = member; | |
3219 | if (!(*link_info.callbacks | |
3220 | ->add_archive_element) (&link_info, member, | |
3221 | "--whole-archive", &subsbfd)) | |
3222 | abort (); | |
3223 | ||
3224 | /* Potentially, the add_archive_element hook may have set a | |
3225 | substitute BFD for us. */ | |
3226 | if (!bfd_link_add_symbols (subsbfd, &link_info)) | |
3227 | { | |
3228 | fatal (_("%P: %pB: error adding symbols: %E\n"), member); | |
3229 | loaded = false; | |
3230 | } | |
3231 | } | |
3232 | ||
3233 | entry->flags.loaded = loaded; | |
3234 | return loaded; | |
3235 | } | |
3236 | break; | |
3237 | } | |
3238 | ||
3239 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) | |
3240 | entry->flags.loaded = true; | |
3241 | else | |
3242 | fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); | |
3243 | ||
3244 | return entry->flags.loaded; | |
3245 | } | |
3246 | ||
3247 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both | |
3248 | may be NULL, indicating that it is a wildcard. Separate | |
3249 | lang_input_section statements are created for each part of the | |
3250 | expansion; they are added after the wild statement S. OUTPUT is | |
3251 | the output section. */ | |
3252 | ||
3253 | static void | |
3254 | wild (lang_wild_statement_type *s, | |
3255 | const char *target ATTRIBUTE_UNUSED, | |
3256 | lang_output_section_statement_type *output) | |
3257 | { | |
3258 | struct wildcard_list *sec; | |
3259 | ||
3260 | if (s->filenames_sorted || s->any_specs_sorted) | |
3261 | { | |
3262 | lang_section_bst_type *tree; | |
3263 | ||
3264 | walk_wild (s, output_section_callback_sort, output); | |
3265 | ||
3266 | tree = s->tree; | |
3267 | if (tree) | |
3268 | { | |
3269 | output_section_callback_tree_to_list (s, tree, output); | |
3270 | s->tree = NULL; | |
3271 | s->rightmost = &s->tree; | |
3272 | } | |
3273 | } | |
3274 | else | |
3275 | walk_wild (s, output_section_callback_nosort, output); | |
3276 | ||
3277 | if (default_common_section == NULL) | |
3278 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
3279 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) | |
3280 | { | |
3281 | /* Remember the section that common is going to in case we | |
3282 | later get something which doesn't know where to put it. */ | |
3283 | default_common_section = output; | |
3284 | break; | |
3285 | } | |
3286 | } | |
3287 | ||
3288 | /* Return TRUE iff target is the sought target. */ | |
3289 | ||
3290 | static int | |
3291 | get_target (const bfd_target *target, void *data) | |
3292 | { | |
3293 | const char *sought = (const char *) data; | |
3294 | ||
3295 | return strcmp (target->name, sought) == 0; | |
3296 | } | |
3297 | ||
3298 | /* Like strcpy() but convert to lower case as well. */ | |
3299 | ||
3300 | static void | |
3301 | stricpy (char *dest, const char *src) | |
3302 | { | |
3303 | char c; | |
3304 | ||
3305 | while ((c = *src++) != 0) | |
3306 | *dest++ = TOLOWER (c); | |
3307 | ||
3308 | *dest = 0; | |
3309 | } | |
3310 | ||
3311 | /* Remove the first occurrence of needle (if any) in haystack | |
3312 | from haystack. */ | |
3313 | ||
3314 | static void | |
3315 | strcut (char *haystack, const char *needle) | |
3316 | { | |
3317 | haystack = strstr (haystack, needle); | |
3318 | ||
3319 | if (haystack) | |
3320 | { | |
3321 | char *src; | |
3322 | ||
3323 | for (src = haystack + strlen (needle); *src;) | |
3324 | *haystack++ = *src++; | |
3325 | ||
3326 | *haystack = 0; | |
3327 | } | |
3328 | } | |
3329 | ||
3330 | /* Compare two target format name strings. | |
3331 | Return a value indicating how "similar" they are. */ | |
3332 | ||
3333 | static int | |
3334 | name_compare (const char *first, const char *second) | |
3335 | { | |
3336 | char *copy1; | |
3337 | char *copy2; | |
3338 | int result; | |
3339 | ||
3340 | copy1 = (char *) xmalloc (strlen (first) + 1); | |
3341 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
3342 | ||
3343 | /* Convert the names to lower case. */ | |
3344 | stricpy (copy1, first); | |
3345 | stricpy (copy2, second); | |
3346 | ||
3347 | /* Remove size and endian strings from the name. */ | |
3348 | strcut (copy1, "big"); | |
3349 | strcut (copy1, "little"); | |
3350 | strcut (copy2, "big"); | |
3351 | strcut (copy2, "little"); | |
3352 | ||
3353 | /* Return a value based on how many characters match, | |
3354 | starting from the beginning. If both strings are | |
3355 | the same then return 10 * their length. */ | |
3356 | for (result = 0; copy1[result] == copy2[result]; result++) | |
3357 | if (copy1[result] == 0) | |
3358 | { | |
3359 | result *= 10; | |
3360 | break; | |
3361 | } | |
3362 | ||
3363 | free (copy1); | |
3364 | free (copy2); | |
3365 | ||
3366 | return result; | |
3367 | } | |
3368 | ||
3369 | /* Set by closest_target_match() below. */ | |
3370 | static const bfd_target *winner; | |
3371 | ||
3372 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3373 | requirement that was specified on the command line, and is the nearest | |
3374 | match to the original output target. */ | |
3375 | ||
3376 | static int | |
3377 | closest_target_match (const bfd_target *target, void *data) | |
3378 | { | |
3379 | const bfd_target *original = (const bfd_target *) data; | |
3380 | ||
3381 | if (command_line.endian == ENDIAN_BIG | |
3382 | && target->byteorder != BFD_ENDIAN_BIG) | |
3383 | return 0; | |
3384 | ||
3385 | if (command_line.endian == ENDIAN_LITTLE | |
3386 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
3387 | return 0; | |
3388 | ||
3389 | /* Must be the same flavour. */ | |
3390 | if (target->flavour != original->flavour) | |
3391 | return 0; | |
3392 | ||
3393 | /* Ignore generic big and little endian elf vectors. */ | |
3394 | if (strcmp (target->name, "elf32-big") == 0 | |
3395 | || strcmp (target->name, "elf64-big") == 0 | |
3396 | || strcmp (target->name, "elf32-little") == 0 | |
3397 | || strcmp (target->name, "elf64-little") == 0) | |
3398 | return 0; | |
3399 | ||
3400 | /* If we have not found a potential winner yet, then record this one. */ | |
3401 | if (winner == NULL) | |
3402 | { | |
3403 | winner = target; | |
3404 | return 0; | |
3405 | } | |
3406 | ||
3407 | /* Oh dear, we now have two potential candidates for a successful match. | |
3408 | Compare their names and choose the better one. */ | |
3409 | if (name_compare (target->name, original->name) | |
3410 | > name_compare (winner->name, original->name)) | |
3411 | winner = target; | |
3412 | ||
3413 | /* Keep on searching until wqe have checked them all. */ | |
3414 | return 0; | |
3415 | } | |
3416 | ||
3417 | /* Return the BFD target format of the first input file. */ | |
3418 | ||
3419 | static const char * | |
3420 | get_first_input_target (void) | |
3421 | { | |
3422 | const char *target = NULL; | |
3423 | ||
3424 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3425 | { | |
3426 | if (s->header.type == lang_input_statement_enum | |
3427 | && s->flags.real) | |
3428 | { | |
3429 | ldfile_open_file (s); | |
3430 | ||
3431 | if (s->the_bfd != NULL | |
3432 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3433 | { | |
3434 | target = bfd_get_target (s->the_bfd); | |
3435 | ||
3436 | if (target != NULL) | |
3437 | break; | |
3438 | } | |
3439 | } | |
3440 | } | |
3441 | ||
3442 | return target; | |
3443 | } | |
3444 | ||
3445 | const char * | |
3446 | lang_get_output_target (void) | |
3447 | { | |
3448 | const char *target; | |
3449 | ||
3450 | /* Has the user told us which output format to use? */ | |
3451 | if (output_target != NULL) | |
3452 | return output_target; | |
3453 | ||
3454 | /* No - has the current target been set to something other than | |
3455 | the default? */ | |
3456 | if (current_target != default_target && current_target != NULL) | |
3457 | return current_target; | |
3458 | ||
3459 | /* No - can we determine the format of the first input file? */ | |
3460 | target = get_first_input_target (); | |
3461 | if (target != NULL) | |
3462 | return target; | |
3463 | ||
3464 | /* Failed - use the default output target. */ | |
3465 | return default_target; | |
3466 | } | |
3467 | ||
3468 | /* Open the output file. */ | |
3469 | ||
3470 | static void | |
3471 | open_output (const char *name) | |
3472 | { | |
3473 | lang_input_statement_type *f; | |
3474 | char *out = lrealpath (name); | |
3475 | ||
3476 | for (f = (void *) input_file_chain.head; | |
3477 | f != NULL; | |
3478 | f = f->next_real_file) | |
3479 | if (f->flags.real) | |
3480 | { | |
3481 | char *in = lrealpath (f->local_sym_name); | |
3482 | if (filename_cmp (in, out) == 0) | |
3483 | fatal (_("%P: input file '%s' is the same as output file\n"), | |
3484 | f->filename); | |
3485 | free (in); | |
3486 | } | |
3487 | free (out); | |
3488 | ||
3489 | output_target = lang_get_output_target (); | |
3490 | ||
3491 | /* Has the user requested a particular endianness on the command | |
3492 | line? */ | |
3493 | if (command_line.endian != ENDIAN_UNSET) | |
3494 | { | |
3495 | /* Get the chosen target. */ | |
3496 | const bfd_target *target | |
3497 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
3498 | ||
3499 | /* If the target is not supported, we cannot do anything. */ | |
3500 | if (target != NULL) | |
3501 | { | |
3502 | enum bfd_endian desired_endian; | |
3503 | ||
3504 | if (command_line.endian == ENDIAN_BIG) | |
3505 | desired_endian = BFD_ENDIAN_BIG; | |
3506 | else | |
3507 | desired_endian = BFD_ENDIAN_LITTLE; | |
3508 | ||
3509 | /* See if the target has the wrong endianness. This should | |
3510 | not happen if the linker script has provided big and | |
3511 | little endian alternatives, but some scrips don't do | |
3512 | this. */ | |
3513 | if (target->byteorder != desired_endian) | |
3514 | { | |
3515 | /* If it does, then see if the target provides | |
3516 | an alternative with the correct endianness. */ | |
3517 | if (target->alternative_target != NULL | |
3518 | && (target->alternative_target->byteorder == desired_endian)) | |
3519 | output_target = target->alternative_target->name; | |
3520 | else | |
3521 | { | |
3522 | /* Try to find a target as similar as possible to | |
3523 | the default target, but which has the desired | |
3524 | endian characteristic. */ | |
3525 | bfd_iterate_over_targets (closest_target_match, | |
3526 | (void *) target); | |
3527 | ||
3528 | /* Oh dear - we could not find any targets that | |
3529 | satisfy our requirements. */ | |
3530 | if (winner == NULL) | |
3531 | einfo (_("%P: warning: could not find any targets" | |
3532 | " that match endianness requirement\n")); | |
3533 | else | |
3534 | output_target = winner->name; | |
3535 | } | |
3536 | } | |
3537 | } | |
3538 | } | |
3539 | ||
3540 | link_info.output_bfd = bfd_openw (name, output_target); | |
3541 | ||
3542 | if (link_info.output_bfd == NULL) | |
3543 | { | |
3544 | if (bfd_get_error () == bfd_error_invalid_target) | |
3545 | fatal (_("%P: target %s not found\n"), output_target); | |
3546 | ||
3547 | fatal (_("%P: cannot open output file %s: %E\n"), name); | |
3548 | } | |
3549 | ||
3550 | delete_output_file_on_failure = true; | |
3551 | ||
3552 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) | |
3553 | fatal (_("%P: %s: can not make object file: %E\n"), name); | |
3554 | if (!bfd_set_arch_mach (link_info.output_bfd, | |
3555 | ldfile_output_architecture, | |
3556 | ldfile_output_machine)) | |
3557 | fatal (_("%P: %s: can not set architecture: %E\n"), name); | |
3558 | ||
3559 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); | |
3560 | if (link_info.hash == NULL) | |
3561 | fatal (_("%P: can not create hash table: %E\n")); | |
3562 | ||
3563 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); | |
3564 | } | |
3565 | ||
3566 | static void | |
3567 | ldlang_open_output (lang_statement_union_type *statement) | |
3568 | { | |
3569 | switch (statement->header.type) | |
3570 | { | |
3571 | case lang_output_statement_enum: | |
3572 | ASSERT (link_info.output_bfd == NULL); | |
3573 | open_output (statement->output_statement.name); | |
3574 | ldemul_set_output_arch (); | |
3575 | if (config.magic_demand_paged | |
3576 | && !bfd_link_relocatable (&link_info)) | |
3577 | link_info.output_bfd->flags |= D_PAGED; | |
3578 | else | |
3579 | link_info.output_bfd->flags &= ~D_PAGED; | |
3580 | if (config.text_read_only) | |
3581 | link_info.output_bfd->flags |= WP_TEXT; | |
3582 | else | |
3583 | link_info.output_bfd->flags &= ~WP_TEXT; | |
3584 | if (link_info.traditional_format) | |
3585 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; | |
3586 | else | |
3587 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; | |
3588 | if (config.no_section_header) | |
3589 | link_info.output_bfd->flags |= BFD_NO_SECTION_HEADER; | |
3590 | else | |
3591 | link_info.output_bfd->flags &= ~BFD_NO_SECTION_HEADER; | |
3592 | break; | |
3593 | ||
3594 | case lang_target_statement_enum: | |
3595 | current_target = statement->target_statement.target; | |
3596 | break; | |
3597 | default: | |
3598 | break; | |
3599 | } | |
3600 | } | |
3601 | ||
3602 | static void | |
3603 | init_opb (asection *s) | |
3604 | { | |
3605 | unsigned int x; | |
3606 | ||
3607 | opb_shift = 0; | |
3608 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
3609 | && s != NULL | |
3610 | && (s->flags & SEC_ELF_OCTETS) != 0) | |
3611 | return; | |
3612 | ||
3613 | x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3614 | ldfile_output_machine); | |
3615 | if (x > 1) | |
3616 | while ((x & 1) == 0) | |
3617 | { | |
3618 | x >>= 1; | |
3619 | ++opb_shift; | |
3620 | } | |
3621 | ASSERT (x == 1); | |
3622 | } | |
3623 | ||
3624 | /* Open all the input files. */ | |
3625 | ||
3626 | enum open_bfd_mode | |
3627 | { | |
3628 | OPEN_BFD_NORMAL = 0, | |
3629 | OPEN_BFD_FORCE = 1, | |
3630 | OPEN_BFD_RESCAN = 2 | |
3631 | }; | |
3632 | #if BFD_SUPPORTS_PLUGINS | |
3633 | static lang_input_statement_type *plugin_insert = NULL; | |
3634 | static struct bfd_link_hash_entry *plugin_undefs = NULL; | |
3635 | #endif | |
3636 | ||
3637 | static void | |
3638 | open_input_bfds (lang_statement_union_type *s, | |
3639 | lang_output_section_statement_type *os, | |
3640 | enum open_bfd_mode mode) | |
3641 | { | |
3642 | for (; s != NULL; s = s->header.next) | |
3643 | { | |
3644 | switch (s->header.type) | |
3645 | { | |
3646 | case lang_constructors_statement_enum: | |
3647 | open_input_bfds (constructor_list.head, os, mode); | |
3648 | break; | |
3649 | case lang_output_section_statement_enum: | |
3650 | os = &s->output_section_statement; | |
3651 | open_input_bfds (os->children.head, os, mode); | |
3652 | break; | |
3653 | case lang_wild_statement_enum: | |
3654 | /* Maybe we should load the file's symbols. */ | |
3655 | if ((mode & OPEN_BFD_RESCAN) == 0 | |
3656 | && s->wild_statement.filename | |
3657 | && !wildcardp (s->wild_statement.filename) | |
3658 | && !archive_path (s->wild_statement.filename)) | |
3659 | lookup_name (s->wild_statement.filename); | |
3660 | open_input_bfds (s->wild_statement.children.head, os, mode); | |
3661 | break; | |
3662 | case lang_group_statement_enum: | |
3663 | { | |
3664 | struct bfd_link_hash_entry *undefs; | |
3665 | #if BFD_SUPPORTS_PLUGINS | |
3666 | lang_input_statement_type *plugin_insert_save; | |
3667 | #endif | |
3668 | ||
3669 | /* We must continually search the entries in the group | |
3670 | until no new symbols are added to the list of undefined | |
3671 | symbols. */ | |
3672 | ||
3673 | do | |
3674 | { | |
3675 | #if BFD_SUPPORTS_PLUGINS | |
3676 | plugin_insert_save = plugin_insert; | |
3677 | #endif | |
3678 | undefs = link_info.hash->undefs_tail; | |
3679 | open_input_bfds (s->group_statement.children.head, os, | |
3680 | mode | OPEN_BFD_FORCE); | |
3681 | } | |
3682 | while (undefs != link_info.hash->undefs_tail | |
3683 | #if BFD_SUPPORTS_PLUGINS | |
3684 | /* Objects inserted by a plugin, which are loaded | |
3685 | before we hit this loop, may have added new | |
3686 | undefs. */ | |
3687 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3688 | #endif | |
3689 | ); | |
3690 | } | |
3691 | break; | |
3692 | case lang_target_statement_enum: | |
3693 | current_target = s->target_statement.target; | |
3694 | break; | |
3695 | case lang_input_statement_enum: | |
3696 | if (s->input_statement.flags.real) | |
3697 | { | |
3698 | lang_statement_union_type **os_tail; | |
3699 | lang_statement_list_type add; | |
3700 | bfd *abfd; | |
3701 | ||
3702 | s->input_statement.target = current_target; | |
3703 | ||
3704 | /* If we are being called from within a group, and this | |
3705 | is an archive which has already been searched, then | |
3706 | force it to be researched unless the whole archive | |
3707 | has been loaded already. Do the same for a rescan. | |
3708 | Likewise reload --as-needed shared libs. */ | |
3709 | if (mode != OPEN_BFD_NORMAL | |
3710 | #if BFD_SUPPORTS_PLUGINS | |
3711 | && ((mode & OPEN_BFD_RESCAN) == 0 | |
3712 | || plugin_insert == NULL) | |
3713 | #endif | |
3714 | && s->input_statement.flags.loaded | |
3715 | && (abfd = s->input_statement.the_bfd) != NULL | |
3716 | && ((bfd_get_format (abfd) == bfd_archive | |
3717 | && !s->input_statement.flags.whole_archive) | |
3718 | || (bfd_get_format (abfd) == bfd_object | |
3719 | && ((abfd->flags) & DYNAMIC) != 0 | |
3720 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
3721 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
3722 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) | |
3723 | { | |
3724 | s->input_statement.flags.loaded = false; | |
3725 | s->input_statement.flags.reload = true; | |
3726 | } | |
3727 | ||
3728 | os_tail = lang_os_list.tail; | |
3729 | lang_list_init (&add); | |
3730 | ||
3731 | if (!load_symbols (&s->input_statement, &add)) | |
3732 | config.make_executable = false; | |
3733 | ||
3734 | if (add.head != NULL) | |
3735 | { | |
3736 | /* If this was a script with output sections then | |
3737 | tack any added statements on to the end of the | |
3738 | list. This avoids having to reorder the output | |
3739 | section statement list. Very likely the user | |
3740 | forgot -T, and whatever we do here will not meet | |
3741 | naive user expectations. */ | |
3742 | if (os_tail != lang_os_list.tail) | |
3743 | { | |
3744 | einfo (_("%P: warning: %s contains output sections;" | |
3745 | " did you forget -T?\n"), | |
3746 | s->input_statement.filename); | |
3747 | *stat_ptr->tail = add.head; | |
3748 | stat_ptr->tail = add.tail; | |
3749 | } | |
3750 | else | |
3751 | { | |
3752 | *add.tail = s->header.next; | |
3753 | s->header.next = add.head; | |
3754 | } | |
3755 | } | |
3756 | } | |
3757 | #if BFD_SUPPORTS_PLUGINS | |
3758 | /* If we have found the point at which a plugin added new | |
3759 | files, clear plugin_insert to enable archive rescan. */ | |
3760 | if (&s->input_statement == plugin_insert) | |
3761 | plugin_insert = NULL; | |
3762 | #endif | |
3763 | break; | |
3764 | case lang_assignment_statement_enum: | |
3765 | if (s->assignment_statement.exp->type.node_class != etree_assert) | |
3766 | exp_fold_tree_no_dot (s->assignment_statement.exp, os); | |
3767 | break; | |
3768 | default: | |
3769 | break; | |
3770 | } | |
3771 | } | |
3772 | ||
3773 | /* Exit if any of the files were missing. */ | |
3774 | if (input_flags.missing_file) | |
3775 | fatal (""); | |
3776 | } | |
3777 | ||
3778 | #ifdef ENABLE_LIBCTF | |
3779 | /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings | |
3780 | that happened specifically at CTF open time. */ | |
3781 | static void | |
3782 | lang_ctf_errs_warnings (ctf_dict_t *fp) | |
3783 | { | |
3784 | ctf_next_t *i = NULL; | |
3785 | char *text; | |
3786 | int is_warning; | |
3787 | int err; | |
3788 | ||
3789 | while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL) | |
3790 | { | |
3791 | einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), | |
3792 | text); | |
3793 | free (text); | |
3794 | } | |
3795 | if (err != ECTF_NEXT_END) | |
3796 | { | |
3797 | einfo (_("CTF error: cannot get CTF errors: `%s'\n"), | |
3798 | ctf_errmsg (err)); | |
3799 | } | |
3800 | ||
3801 | /* `err' returns errors from the error/warning iterator in particular. | |
3802 | These never assert. But if we have an fp, that could have recorded | |
3803 | an assertion failure: assert if it has done so. */ | |
3804 | ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL); | |
3805 | } | |
3806 | ||
3807 | /* Open the CTF sections in the input files with libctf: if any were opened, | |
3808 | create a fake input file that we'll write the merged CTF data to later | |
3809 | on. */ | |
3810 | ||
3811 | static void | |
3812 | ldlang_open_ctf (void) | |
3813 | { | |
3814 | int any_ctf = 0; | |
3815 | int err; | |
3816 | ||
3817 | ld_start_phase (PHASE_CTF); | |
3818 | ||
3819 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3820 | { | |
3821 | asection *sect; | |
3822 | ||
3823 | /* Incoming files from the compiler have a single ctf_dict_t in them | |
3824 | (which is presented to us by the libctf API in a ctf_archive_t | |
3825 | wrapper): files derived from a previous relocatable link have a CTF | |
3826 | archive containing possibly many CTF files. */ | |
3827 | ||
3828 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3829 | { | |
3830 | if (err != ECTF_NOCTFDATA) | |
3831 | { | |
3832 | lang_ctf_errs_warnings (NULL); | |
3833 | einfo (_("%P: warning: CTF section in %pB not loaded; " | |
3834 | "its types will be discarded: %s\n"), file->the_bfd, | |
3835 | ctf_errmsg (err)); | |
3836 | } | |
3837 | continue; | |
3838 | } | |
3839 | ||
3840 | /* Prevent the contents of this section from being written, while | |
3841 | requiring the section itself to be duplicated in the output, but only | |
3842 | once. */ | |
3843 | /* This section must exist if ctf_bfdopen() succeeded. */ | |
3844 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3845 | sect->size = 0; | |
3846 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3847 | ||
3848 | if (any_ctf) | |
3849 | sect->flags |= SEC_EXCLUDE; | |
3850 | any_ctf = 1; | |
3851 | } | |
3852 | ||
3853 | if (!any_ctf) | |
3854 | { | |
3855 | ctf_output = NULL; | |
3856 | ld_stop_phase (PHASE_CTF); | |
3857 | return; | |
3858 | } | |
3859 | ||
3860 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3861 | { | |
3862 | ld_stop_phase (PHASE_CTF); | |
3863 | return; | |
3864 | } | |
3865 | ||
3866 | einfo (_("%P: warning: CTF output not created: `%s'\n"), | |
3867 | ctf_errmsg (err)); | |
3868 | ||
3869 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3870 | ctf_close (errfile->the_ctf); | |
3871 | ||
3872 | ld_stop_phase (PHASE_CTF); | |
3873 | } | |
3874 | ||
3875 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3876 | function and data object sections need adjustment. */ | |
3877 | ||
3878 | static void | |
3879 | lang_merge_ctf (void) | |
3880 | { | |
3881 | asection *output_sect; | |
3882 | int flags = 0; | |
3883 | ||
3884 | if (!ctf_output) | |
3885 | return; | |
3886 | ||
3887 | ld_start_phase (PHASE_CTF); | |
3888 | ||
3889 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3890 | ||
3891 | /* If the section was discarded, don't waste time merging. */ | |
3892 | if (output_sect == NULL) | |
3893 | { | |
3894 | ctf_dict_close (ctf_output); | |
3895 | ctf_output = NULL; | |
3896 | ||
3897 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3898 | { | |
3899 | ctf_close (file->the_ctf); | |
3900 | file->the_ctf = NULL; | |
3901 | } | |
3902 | ||
3903 | ld_stop_phase (PHASE_CTF); | |
3904 | return; | |
3905 | } | |
3906 | ||
3907 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3908 | { | |
3909 | if (!file->the_ctf) | |
3910 | continue; | |
3911 | ||
3912 | /* Takes ownership of file->the_ctf. */ | |
3913 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) | |
3914 | { | |
3915 | einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), | |
3916 | file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); | |
3917 | ctf_close (file->the_ctf); | |
3918 | file->the_ctf = NULL; | |
3919 | continue; | |
3920 | } | |
3921 | } | |
3922 | ||
3923 | if (!config.ctf_share_duplicated) | |
3924 | flags = CTF_LINK_SHARE_UNCONFLICTED; | |
3925 | else | |
3926 | flags = CTF_LINK_SHARE_DUPLICATED; | |
3927 | if (!config.ctf_variables) | |
3928 | flags |= CTF_LINK_OMIT_VARIABLES_SECTION; | |
3929 | if (bfd_link_relocatable (&link_info)) | |
3930 | flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; | |
3931 | ||
3932 | if (ctf_link (ctf_output, flags) < 0) | |
3933 | { | |
3934 | lang_ctf_errs_warnings (ctf_output); | |
3935 | einfo (_("%P: warning: CTF linking failed; " | |
3936 | "output will have no CTF section: %s\n"), | |
3937 | ctf_errmsg (ctf_errno (ctf_output))); | |
3938 | if (output_sect) | |
3939 | { | |
3940 | output_sect->size = 0; | |
3941 | output_sect->flags |= SEC_EXCLUDE; | |
3942 | } | |
3943 | } | |
3944 | /* Output any lingering errors that didn't come from ctf_link. */ | |
3945 | lang_ctf_errs_warnings (ctf_output); | |
3946 | ||
3947 | ld_stop_phase (PHASE_CTF); | |
3948 | } | |
3949 | ||
3950 | /* Let the emulation acquire strings from the dynamic strtab to help it optimize | |
3951 | the CTF, if supported. */ | |
3952 | ||
3953 | void | |
3954 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) | |
3955 | { | |
3956 | ld_start_phase (PHASE_CTF); | |
3957 | ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); | |
3958 | ld_stop_phase (PHASE_CTF); | |
3959 | } | |
3960 | ||
3961 | /* Inform the emulation about the addition of a new dynamic symbol, in BFD | |
3962 | internal format. */ | |
3963 | void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym) | |
3964 | { | |
3965 | ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym); | |
3966 | } | |
3967 | ||
3968 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3969 | need to dedup against the strtab and symtab, then possibly called from the | |
3970 | target linker code if the dedup has happened. */ | |
3971 | static void | |
3972 | lang_write_ctf (int late) | |
3973 | { | |
3974 | size_t output_size; | |
3975 | asection *output_sect; | |
3976 | ||
3977 | if (!ctf_output) | |
3978 | return; | |
3979 | ||
3980 | ld_start_phase (PHASE_CTF); | |
3981 | ||
3982 | if (late) | |
3983 | { | |
3984 | /* Emit CTF late if this emulation says it can do so. */ | |
3985 | if (ldemul_emit_ctf_early ()) | |
3986 | { | |
3987 | ld_stop_phase (PHASE_CTF); | |
3988 | return; | |
3989 | } | |
3990 | } | |
3991 | else | |
3992 | { | |
3993 | if (!ldemul_emit_ctf_early ()) | |
3994 | { | |
3995 | ld_stop_phase (PHASE_CTF); | |
3996 | return; | |
3997 | } | |
3998 | } | |
3999 | ||
4000 | /* Inform the emulation that all the symbols that will be received have | |
4001 | been. */ | |
4002 | ||
4003 | ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL); | |
4004 | ||
4005 | /* Emit CTF. */ | |
4006 | ||
4007 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
4008 | if (output_sect) | |
4009 | { | |
4010 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
4011 | CTF_COMPRESSION_THRESHOLD); | |
4012 | output_sect->size = output_size; | |
4013 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
4014 | ||
4015 | lang_ctf_errs_warnings (ctf_output); | |
4016 | if (!output_sect->contents) | |
4017 | { | |
4018 | einfo (_("%P: warning: CTF section emission failed; " | |
4019 | "output will have no CTF section: %s\n"), | |
4020 | ctf_errmsg (ctf_errno (ctf_output))); | |
4021 | output_sect->size = 0; | |
4022 | output_sect->flags |= SEC_EXCLUDE; | |
4023 | } | |
4024 | } | |
4025 | ||
4026 | /* This also closes every CTF input file used in the link. */ | |
4027 | ctf_dict_close (ctf_output); | |
4028 | ctf_output = NULL; | |
4029 | ||
4030 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
4031 | file->the_ctf = NULL; | |
4032 | ||
4033 | ld_stop_phase (PHASE_CTF); | |
4034 | } | |
4035 | ||
4036 | /* Write out the CTF section late, if the emulation needs that. */ | |
4037 | ||
4038 | void | |
4039 | ldlang_write_ctf_late (void) | |
4040 | { | |
4041 | /* Trigger a "late call", if the emulation needs one. */ | |
4042 | ||
4043 | lang_write_ctf (1); | |
4044 | } | |
4045 | #else | |
4046 | static void | |
4047 | ldlang_open_ctf (void) | |
4048 | { | |
4049 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
4050 | { | |
4051 | asection *sect; | |
4052 | ||
4053 | /* If built without CTF, warn and delete all CTF sections from the output. | |
4054 | (The alternative would be to simply concatenate them, which does not | |
4055 | yield a valid CTF section.) */ | |
4056 | ||
4057 | if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL) | |
4058 | { | |
4059 | einfo (_("%P: warning: CTF section in %pB not linkable: " | |
4060 | "%P was built without support for CTF\n"), file->the_bfd); | |
4061 | sect->size = 0; | |
4062 | sect->flags |= SEC_EXCLUDE; | |
4063 | } | |
4064 | } | |
4065 | } | |
4066 | ||
4067 | static void lang_merge_ctf (void) {} | |
4068 | void | |
4069 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab | |
4070 | ATTRIBUTE_UNUSED) {} | |
4071 | void | |
4072 | ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED, | |
4073 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {} | |
4074 | static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} | |
4075 | void ldlang_write_ctf_late (void) {} | |
4076 | #endif | |
4077 | ||
4078 | /* Add the supplied name to the symbol table as an undefined reference. | |
4079 | This is a two step process as the symbol table doesn't even exist at | |
4080 | the time the ld command line is processed. First we put the name | |
4081 | on a list, then, once the output file has been opened, transfer the | |
4082 | name to the symbol table. */ | |
4083 | ||
4084 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; | |
4085 | ||
4086 | #define ldlang_undef_chain_list_head entry_symbol.next | |
4087 | ||
4088 | void | |
4089 | ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) | |
4090 | { | |
4091 | ldlang_undef_chain_list_type *new_undef; | |
4092 | ||
4093 | new_undef = stat_alloc (sizeof (*new_undef)); | |
4094 | new_undef->next = ldlang_undef_chain_list_head; | |
4095 | ldlang_undef_chain_list_head = new_undef; | |
4096 | ||
4097 | new_undef->name = stat_strdup (name); | |
4098 | ||
4099 | if (link_info.output_bfd != NULL) | |
4100 | insert_undefined (new_undef->name); | |
4101 | } | |
4102 | ||
4103 | /* Insert NAME as undefined in the symbol table. */ | |
4104 | ||
4105 | static void | |
4106 | insert_undefined (const char *name) | |
4107 | { | |
4108 | struct bfd_link_hash_entry *h; | |
4109 | ||
4110 | h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); | |
4111 | if (h == NULL) | |
4112 | fatal (_("%P: bfd_link_hash_lookup failed: %E\n")); | |
4113 | if (h->type == bfd_link_hash_new) | |
4114 | { | |
4115 | h->type = bfd_link_hash_undefined; | |
4116 | h->u.undef.abfd = NULL; | |
4117 | h->non_ir_ref_regular = true; | |
4118 | bfd_link_add_undef (link_info.hash, h); | |
4119 | } | |
4120 | } | |
4121 | ||
4122 | /* Run through the list of undefineds created above and place them | |
4123 | into the linker hash table as undefined symbols belonging to the | |
4124 | script file. */ | |
4125 | ||
4126 | static void | |
4127 | lang_place_undefineds (void) | |
4128 | { | |
4129 | ldlang_undef_chain_list_type *ptr; | |
4130 | ||
4131 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) | |
4132 | insert_undefined (ptr->name); | |
4133 | } | |
4134 | ||
4135 | /* Mark -u symbols against garbage collection. */ | |
4136 | ||
4137 | static void | |
4138 | lang_mark_undefineds (void) | |
4139 | { | |
4140 | ldlang_undef_chain_list_type *ptr; | |
4141 | ||
4142 | if (is_elf_hash_table (link_info.hash)) | |
4143 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) | |
4144 | { | |
4145 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
4146 | bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); | |
4147 | if (h != NULL) | |
4148 | h->mark = 1; | |
4149 | } | |
4150 | } | |
4151 | ||
4152 | /* Structure used to build the list of symbols that the user has required | |
4153 | be defined. */ | |
4154 | ||
4155 | struct require_defined_symbol | |
4156 | { | |
4157 | const char *name; | |
4158 | struct require_defined_symbol *next; | |
4159 | }; | |
4160 | ||
4161 | /* The list of symbols that the user has required be defined. */ | |
4162 | ||
4163 | static struct require_defined_symbol *require_defined_symbol_list; | |
4164 | ||
4165 | /* Add a new symbol NAME to the list of symbols that are required to be | |
4166 | defined. */ | |
4167 | ||
4168 | void | |
4169 | ldlang_add_require_defined (const char *const name) | |
4170 | { | |
4171 | struct require_defined_symbol *ptr; | |
4172 | ||
4173 | ldlang_add_undef (name, true); | |
4174 | ptr = stat_alloc (sizeof (*ptr)); | |
4175 | ptr->next = require_defined_symbol_list; | |
4176 | ptr->name = stat_strdup (name); | |
4177 | require_defined_symbol_list = ptr; | |
4178 | } | |
4179 | ||
4180 | /* Check that all symbols the user required to be defined, are defined, | |
4181 | raise an error if we find a symbol that is not defined. */ | |
4182 | ||
4183 | static void | |
4184 | ldlang_check_require_defined_symbols (void) | |
4185 | { | |
4186 | struct require_defined_symbol *ptr; | |
4187 | ||
4188 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
4189 | { | |
4190 | struct bfd_link_hash_entry *h; | |
4191 | ||
4192 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
4193 | false, false, true); | |
4194 | if (! is_defined (h)) | |
4195 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); | |
4196 | } | |
4197 | } | |
4198 | ||
4199 | /* Check for all readonly or some readwrite sections. */ | |
4200 | ||
4201 | static void | |
4202 | check_input_sections | |
4203 | (lang_statement_union_type *s, | |
4204 | lang_output_section_statement_type *output_section_statement) | |
4205 | { | |
4206 | for (; s != NULL; s = s->header.next) | |
4207 | { | |
4208 | switch (s->header.type) | |
4209 | { | |
4210 | case lang_wild_statement_enum: | |
4211 | walk_wild (&s->wild_statement, check_section_callback, | |
4212 | output_section_statement); | |
4213 | if (!output_section_statement->all_input_readonly) | |
4214 | return; | |
4215 | break; | |
4216 | case lang_constructors_statement_enum: | |
4217 | check_input_sections (constructor_list.head, | |
4218 | output_section_statement); | |
4219 | if (!output_section_statement->all_input_readonly) | |
4220 | return; | |
4221 | break; | |
4222 | case lang_group_statement_enum: | |
4223 | check_input_sections (s->group_statement.children.head, | |
4224 | output_section_statement); | |
4225 | if (!output_section_statement->all_input_readonly) | |
4226 | return; | |
4227 | break; | |
4228 | default: | |
4229 | break; | |
4230 | } | |
4231 | } | |
4232 | } | |
4233 | ||
4234 | /* Update wildcard statements if needed. */ | |
4235 | ||
4236 | static void | |
4237 | update_wild_statements (lang_statement_union_type *s) | |
4238 | { | |
4239 | struct wildcard_list *sec; | |
4240 | ||
4241 | switch (sort_section) | |
4242 | { | |
4243 | default: | |
4244 | FAIL (); | |
4245 | ||
4246 | case none: | |
4247 | break; | |
4248 | ||
4249 | case by_name: | |
4250 | case by_alignment: | |
4251 | for (; s != NULL; s = s->header.next) | |
4252 | { | |
4253 | switch (s->header.type) | |
4254 | { | |
4255 | default: | |
4256 | break; | |
4257 | ||
4258 | case lang_wild_statement_enum: | |
4259 | for (sec = s->wild_statement.section_list; sec != NULL; | |
4260 | sec = sec->next) | |
4261 | /* Don't sort .init/.fini sections. */ | |
4262 | if (strcmp (sec->spec.name, ".init") != 0 | |
4263 | && strcmp (sec->spec.name, ".fini") != 0) | |
4264 | { | |
4265 | switch (sec->spec.sorted) | |
4266 | { | |
4267 | case none: | |
4268 | sec->spec.sorted = sort_section; | |
4269 | break; | |
4270 | case by_name: | |
4271 | if (sort_section == by_alignment) | |
4272 | sec->spec.sorted = by_name_alignment; | |
4273 | break; | |
4274 | case by_alignment: | |
4275 | if (sort_section == by_name) | |
4276 | sec->spec.sorted = by_alignment_name; | |
4277 | break; | |
4278 | default: | |
4279 | break; | |
4280 | } | |
4281 | s->wild_statement.any_specs_sorted = true; | |
4282 | } | |
4283 | break; | |
4284 | ||
4285 | case lang_constructors_statement_enum: | |
4286 | update_wild_statements (constructor_list.head); | |
4287 | break; | |
4288 | ||
4289 | case lang_output_section_statement_enum: | |
4290 | update_wild_statements | |
4291 | (s->output_section_statement.children.head); | |
4292 | break; | |
4293 | ||
4294 | case lang_group_statement_enum: | |
4295 | update_wild_statements (s->group_statement.children.head); | |
4296 | break; | |
4297 | } | |
4298 | } | |
4299 | break; | |
4300 | } | |
4301 | } | |
4302 | ||
4303 | /* Open input files and attach to output sections. */ | |
4304 | ||
4305 | static void | |
4306 | map_input_to_output_sections | |
4307 | (lang_statement_union_type *s, const char *target, | |
4308 | lang_output_section_statement_type *os) | |
4309 | { | |
4310 | for (; s != NULL; s = s->header.next) | |
4311 | { | |
4312 | lang_output_section_statement_type *tos; | |
4313 | flagword flags; | |
4314 | unsigned int type = 0; | |
4315 | ||
4316 | switch (s->header.type) | |
4317 | { | |
4318 | case lang_wild_statement_enum: | |
4319 | wild (&s->wild_statement, target, os); | |
4320 | break; | |
4321 | case lang_constructors_statement_enum: | |
4322 | map_input_to_output_sections (constructor_list.head, | |
4323 | target, | |
4324 | os); | |
4325 | break; | |
4326 | case lang_output_section_statement_enum: | |
4327 | tos = &s->output_section_statement; | |
4328 | if (tos->constraint == ONLY_IF_RW | |
4329 | || tos->constraint == ONLY_IF_RO) | |
4330 | { | |
4331 | tos->all_input_readonly = true; | |
4332 | check_input_sections (tos->children.head, tos); | |
4333 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
4334 | tos->constraint = -1; | |
4335 | } | |
4336 | if (tos->constraint >= 0) | |
4337 | map_input_to_output_sections (tos->children.head, | |
4338 | target, | |
4339 | tos); | |
4340 | break; | |
4341 | case lang_output_statement_enum: | |
4342 | break; | |
4343 | case lang_target_statement_enum: | |
4344 | target = s->target_statement.target; | |
4345 | break; | |
4346 | case lang_group_statement_enum: | |
4347 | map_input_to_output_sections (s->group_statement.children.head, | |
4348 | target, | |
4349 | os); | |
4350 | break; | |
4351 | case lang_data_statement_enum: | |
4352 | if (os == NULL) | |
4353 | /* This should never happen. */ | |
4354 | FAIL (); | |
4355 | /* Make sure that any sections mentioned in the expression | |
4356 | are initialized. */ | |
4357 | exp_init_os (s->data_statement.exp); | |
4358 | /* The output section gets CONTENTS, ALLOC and LOAD, but | |
4359 | these may be overridden by the script. */ | |
4360 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; | |
4361 | switch (os->sectype) | |
4362 | { | |
4363 | case normal_section: | |
4364 | case overlay_section: | |
4365 | case first_overlay_section: | |
4366 | break; | |
4367 | case noalloc_section: | |
4368 | flags = SEC_HAS_CONTENTS; | |
4369 | break; | |
4370 | case readonly_section: | |
4371 | flags |= SEC_READONLY; | |
4372 | break; | |
4373 | case typed_readonly_section: | |
4374 | flags |= SEC_READONLY; | |
4375 | /* Fall through. */ | |
4376 | case type_section: | |
4377 | if (os->sectype_value->type.node_class == etree_name | |
4378 | && os->sectype_value->type.node_code == NAME) | |
4379 | { | |
4380 | const char *name = os->sectype_value->name.name; | |
4381 | if (strcmp (name, "SHT_PROGBITS") == 0) | |
4382 | type = SHT_PROGBITS; | |
4383 | else if (strcmp (name, "SHT_STRTAB") == 0) | |
4384 | type = SHT_STRTAB; | |
4385 | else if (strcmp (name, "SHT_NOTE") == 0) | |
4386 | type = SHT_NOTE; | |
4387 | else if (strcmp (name, "SHT_NOBITS") == 0) | |
4388 | type = SHT_NOBITS; | |
4389 | else if (strcmp (name, "SHT_INIT_ARRAY") == 0) | |
4390 | type = SHT_INIT_ARRAY; | |
4391 | else if (strcmp (name, "SHT_FINI_ARRAY") == 0) | |
4392 | type = SHT_FINI_ARRAY; | |
4393 | else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0) | |
4394 | type = SHT_PREINIT_ARRAY; | |
4395 | else | |
4396 | fatal (_ ("%P: invalid type for output section `%s'\n"), | |
4397 | os->name); | |
4398 | } | |
4399 | else | |
4400 | { | |
4401 | exp_fold_tree_no_dot (os->sectype_value, os); | |
4402 | if (expld.result.valid_p) | |
4403 | type = expld.result.value; | |
4404 | else | |
4405 | fatal (_ ("%P: invalid type for output section `%s'\n"), | |
4406 | os->name); | |
4407 | } | |
4408 | break; | |
4409 | case noload_section: | |
4410 | if (bfd_get_flavour (link_info.output_bfd) | |
4411 | == bfd_target_elf_flavour) | |
4412 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4413 | else | |
4414 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
4415 | break; | |
4416 | } | |
4417 | if (os->bfd_section == NULL) | |
4418 | init_os (os, flags | SEC_READONLY); | |
4419 | else | |
4420 | os->bfd_section->flags |= flags; | |
4421 | os->bfd_section->type = type; | |
4422 | break; | |
4423 | case lang_input_section_enum: | |
4424 | break; | |
4425 | case lang_fill_statement_enum: | |
4426 | case lang_object_symbols_statement_enum: | |
4427 | case lang_reloc_statement_enum: | |
4428 | case lang_padding_statement_enum: | |
4429 | case lang_input_statement_enum: | |
4430 | if (os != NULL && os->bfd_section == NULL) | |
4431 | init_os (os, 0); | |
4432 | break; | |
4433 | ||
4434 | case lang_assignment_statement_enum: | |
4435 | if (os != NULL && os->bfd_section == NULL) | |
4436 | init_os (os, get_os_init_flag (os)); | |
4437 | ||
4438 | /* Make sure that any sections mentioned in the assignment | |
4439 | are initialized. */ | |
4440 | exp_init_os (s->assignment_statement.exp); | |
4441 | break; | |
4442 | ||
4443 | case lang_address_statement_enum: | |
4444 | /* Mark the specified section with the supplied address. | |
4445 | If this section was actually a segment marker, then the | |
4446 | directive is ignored if the linker script explicitly | |
4447 | processed the segment marker. Originally, the linker | |
4448 | treated segment directives (like -Ttext on the | |
4449 | command-line) as section directives. We honor the | |
4450 | section directive semantics for backwards compatibility; | |
4451 | linker scripts that do not specifically check for | |
4452 | SEGMENT_START automatically get the old semantics. */ | |
4453 | if (!s->address_statement.segment | |
4454 | || !s->address_statement.segment->used) | |
4455 | { | |
4456 | const char *name = s->address_statement.section_name; | |
4457 | ||
4458 | /* Create the output section statement here so that | |
4459 | orphans with a set address will be placed after other | |
4460 | script sections. If we let the orphan placement code | |
4461 | place them in amongst other sections then the address | |
4462 | will affect following script sections, which is | |
4463 | likely to surprise naive users. */ | |
4464 | tos = lang_output_section_statement_lookup (name, 0, 1); | |
4465 | tos->addr_tree = s->address_statement.address; | |
4466 | if (tos->bfd_section == NULL) | |
4467 | init_os (tos, 0); | |
4468 | } | |
4469 | break; | |
4470 | case lang_insert_statement_enum: | |
4471 | break; | |
4472 | case lang_input_matcher_enum: | |
4473 | FAIL (); | |
4474 | } | |
4475 | } | |
4476 | } | |
4477 | ||
4478 | /* An insert statement snips out all the linker statements from the | |
4479 | start of the list and places them after the output section | |
4480 | statement specified by the insert. This operation is complicated | |
4481 | by the fact that we keep a doubly linked list of output section | |
4482 | statements as well as the singly linked list of all statements. | |
4483 | FIXME someday: Twiddling with the list not only moves statements | |
4484 | from the user's script but also input and group statements that are | |
4485 | built from command line object files and --start-group. We only | |
4486 | get away with this because the list pointers used by file_chain | |
4487 | and input_file_chain are not reordered, and processing via | |
4488 | statement_list after this point mostly ignores input statements. | |
4489 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4490 | can end up looking odd. */ | |
4491 | ||
4492 | static void | |
4493 | process_insert_statements (lang_statement_union_type **start) | |
4494 | { | |
4495 | lang_statement_union_type **s; | |
4496 | lang_output_section_statement_type *first_os = NULL; | |
4497 | lang_output_section_statement_type *last_os = NULL; | |
4498 | lang_output_section_statement_type *os; | |
4499 | ||
4500 | s = start; | |
4501 | while (*s != NULL) | |
4502 | { | |
4503 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4504 | { | |
4505 | /* Keep pointers to the first and last output section | |
4506 | statement in the sequence we may be about to move. */ | |
4507 | os = &(*s)->output_section_statement; | |
4508 | ||
4509 | ASSERT (last_os == NULL || last_os->next == os); | |
4510 | last_os = os; | |
4511 | ||
4512 | /* Set constraint negative so that lang_output_section_find | |
4513 | won't match this output section statement. At this | |
4514 | stage in linking constraint has values in the range | |
4515 | [-1, ONLY_IN_RW]. */ | |
4516 | last_os->constraint = -2 - last_os->constraint; | |
4517 | if (first_os == NULL) | |
4518 | first_os = last_os; | |
4519 | } | |
4520 | else if ((*s)->header.type == lang_group_statement_enum) | |
4521 | { | |
4522 | /* A user might put -T between --start-group and | |
4523 | --end-group. One way this odd construct might arise is | |
4524 | from a wrapper around ld to change library search | |
4525 | behaviour. For example: | |
4526 | #! /bin/sh | |
4527 | exec real_ld --start-group "$@" --end-group | |
4528 | This isn't completely unreasonable so go looking inside a | |
4529 | group statement for insert statements. */ | |
4530 | process_insert_statements (&(*s)->group_statement.children.head); | |
4531 | } | |
4532 | else if ((*s)->header.type == lang_insert_statement_enum) | |
4533 | { | |
4534 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4535 | lang_output_section_statement_type *where; | |
4536 | lang_statement_union_type **ptr; | |
4537 | lang_statement_union_type *first; | |
4538 | ||
4539 | if (link_info.non_contiguous_regions) | |
4540 | { | |
4541 | einfo (_("warning: INSERT statement in linker script is " | |
4542 | "incompatible with --enable-non-contiguous-regions.\n")); | |
4543 | } | |
4544 | ||
4545 | where = lang_output_section_find (i->where); | |
4546 | if (where != NULL && i->is_before) | |
4547 | { | |
4548 | do | |
4549 | where = where->prev; | |
4550 | while (where != NULL && where->constraint < 0); | |
4551 | } | |
4552 | if (where == NULL) | |
4553 | { | |
4554 | fatal (_("%P: %s not found for insert\n"), i->where); | |
4555 | return; | |
4556 | } | |
4557 | ||
4558 | /* Deal with reordering the output section statement list. */ | |
4559 | if (last_os != NULL) | |
4560 | { | |
4561 | asection *first_sec, *last_sec; | |
4562 | struct lang_output_section_statement_struct **next; | |
4563 | ||
4564 | /* Snip out the output sections we are moving. */ | |
4565 | first_os->prev->next = last_os->next; | |
4566 | if (last_os->next == NULL) | |
4567 | { | |
4568 | next = &first_os->prev->next; | |
4569 | lang_os_list.tail = (lang_statement_union_type **) next; | |
4570 | } | |
4571 | else | |
4572 | last_os->next->prev = first_os->prev; | |
4573 | /* Add them in at the new position. */ | |
4574 | last_os->next = where->next; | |
4575 | if (where->next == NULL) | |
4576 | { | |
4577 | next = &last_os->next; | |
4578 | lang_os_list.tail = (lang_statement_union_type **) next; | |
4579 | } | |
4580 | else | |
4581 | where->next->prev = last_os; | |
4582 | first_os->prev = where; | |
4583 | where->next = first_os; | |
4584 | ||
4585 | /* Move the bfd sections in the same way. */ | |
4586 | first_sec = NULL; | |
4587 | last_sec = NULL; | |
4588 | for (os = first_os; os != NULL; os = os->next) | |
4589 | { | |
4590 | os->constraint = -2 - os->constraint; | |
4591 | if (os->bfd_section != NULL | |
4592 | && os->bfd_section->owner != NULL) | |
4593 | { | |
4594 | last_sec = os->bfd_section; | |
4595 | if (first_sec == NULL) | |
4596 | first_sec = last_sec; | |
4597 | } | |
4598 | if (os == last_os) | |
4599 | break; | |
4600 | } | |
4601 | if (last_sec != NULL) | |
4602 | { | |
4603 | asection *sec = where->bfd_section; | |
4604 | if (sec == NULL) | |
4605 | sec = output_prev_sec_find (where); | |
4606 | ||
4607 | /* The place we want to insert must come after the | |
4608 | sections we are moving. So if we find no | |
4609 | section or if the section is the same as our | |
4610 | last section, then no move is needed. */ | |
4611 | if (sec != NULL && sec != last_sec) | |
4612 | { | |
4613 | /* Trim them off. */ | |
4614 | if (first_sec->prev != NULL) | |
4615 | first_sec->prev->next = last_sec->next; | |
4616 | else | |
4617 | link_info.output_bfd->sections = last_sec->next; | |
4618 | if (last_sec->next != NULL) | |
4619 | last_sec->next->prev = first_sec->prev; | |
4620 | else | |
4621 | link_info.output_bfd->section_last = first_sec->prev; | |
4622 | /* Add back. */ | |
4623 | if (sec->owner == NULL) | |
4624 | /* SEC is the absolute section, from the | |
4625 | first dummy output section statement. Add | |
4626 | back the sections we trimmed off to the | |
4627 | start of the bfd sections. */ | |
4628 | sec = NULL; | |
4629 | if (sec != NULL) | |
4630 | last_sec->next = sec->next; | |
4631 | else | |
4632 | last_sec->next = link_info.output_bfd->sections; | |
4633 | if (last_sec->next != NULL) | |
4634 | last_sec->next->prev = last_sec; | |
4635 | else | |
4636 | link_info.output_bfd->section_last = last_sec; | |
4637 | first_sec->prev = sec; | |
4638 | if (first_sec->prev != NULL) | |
4639 | first_sec->prev->next = first_sec; | |
4640 | else | |
4641 | link_info.output_bfd->sections = first_sec; | |
4642 | } | |
4643 | } | |
4644 | } | |
4645 | ||
4646 | lang_statement_union_type *after = (void *) where; | |
4647 | if (where == &lang_os_list.head->output_section_statement | |
4648 | && where->next == first_os) | |
4649 | { | |
4650 | /* PR30155. Handle a corner case where the statement | |
4651 | list is something like the following: | |
4652 | . LOAD t.o | |
4653 | . .data 0x0000000000000000 0x0 | |
4654 | . [0x0000000000000000] b = . | |
4655 | . *(.data) | |
4656 | . .data 0x0000000000000000 0x0 t.o | |
4657 | . 0x0000000000000000 0x4 LONG 0x0 | |
4658 | . INSERT BEFORE .text.start | |
4659 | . [0x0000000000000004] a = . | |
4660 | . .text.start 0x0000000000000000 0x0 | |
4661 | . [0x0000000000000000] c = . | |
4662 | . OUTPUT(a.out elf64-x86-64) | |
4663 | Here we do not want to allow insert_os_after to | |
4664 | choose a point inside the list we are moving. | |
4665 | That would lose the list. Instead, let | |
4666 | insert_os_after work from the INSERT, which in this | |
4667 | particular example will result in inserting after | |
4668 | the assignment "a = .". */ | |
4669 | after = *s; | |
4670 | } | |
4671 | ptr = insert_os_after (after); | |
4672 | /* Snip everything from the start of the list, up to and | |
4673 | including the insert statement we are currently processing. */ | |
4674 | first = *start; | |
4675 | *start = (*s)->header.next; | |
4676 | /* Add them back where they belong, minus the insert. */ | |
4677 | *s = *ptr; | |
4678 | if (*s == NULL) | |
4679 | statement_list.tail = s; | |
4680 | *ptr = first; | |
4681 | s = start; | |
4682 | first_os = NULL; | |
4683 | last_os = NULL; | |
4684 | continue; | |
4685 | } | |
4686 | s = &(*s)->header.next; | |
4687 | } | |
4688 | ||
4689 | /* Undo constraint twiddling. */ | |
4690 | for (os = first_os; os != NULL; os = os->next) | |
4691 | { | |
4692 | os->constraint = -2 - os->constraint; | |
4693 | if (os == last_os) | |
4694 | break; | |
4695 | } | |
4696 | } | |
4697 | ||
4698 | /* An output section might have been removed after its statement was | |
4699 | added. For example, ldemul_before_allocation can remove dynamic | |
4700 | sections if they turn out to be not needed. Clean them up here. */ | |
4701 | ||
4702 | void | |
4703 | strip_excluded_output_sections (void) | |
4704 | { | |
4705 | lang_output_section_statement_type *os; | |
4706 | ||
4707 | /* Run lang_size_sections (if not already done). */ | |
4708 | if (expld.phase != lang_mark_phase_enum) | |
4709 | { | |
4710 | expld.phase = lang_mark_phase_enum; | |
4711 | expld.dataseg.phase = exp_seg_none; | |
4712 | one_lang_size_sections_pass (NULL, false); | |
4713 | lang_reset_memory_regions (); | |
4714 | } | |
4715 | ||
4716 | for (os = (void *) lang_os_list.head; | |
4717 | os != NULL; | |
4718 | os = os->next) | |
4719 | { | |
4720 | asection *output_section; | |
4721 | bool exclude; | |
4722 | ||
4723 | if (os->constraint < 0) | |
4724 | continue; | |
4725 | ||
4726 | output_section = os->bfd_section; | |
4727 | if (output_section == NULL) | |
4728 | continue; | |
4729 | ||
4730 | exclude = (output_section->rawsize == 0 | |
4731 | && (output_section->flags & SEC_KEEP) == 0 | |
4732 | && !bfd_section_removed_from_list (link_info.output_bfd, | |
4733 | output_section)); | |
4734 | ||
4735 | /* Some sections have not yet been sized, notably .gnu.version, | |
4736 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4737 | input sections, so don't drop output sections that have such | |
4738 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4739 | if (exclude && output_section->map_head.s != NULL) | |
4740 | { | |
4741 | asection *s; | |
4742 | ||
4743 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) | |
4744 | if ((s->flags & SEC_EXCLUDE) == 0 | |
4745 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4746 | || link_info.emitrelocations)) | |
4747 | { | |
4748 | exclude = false; | |
4749 | break; | |
4750 | } | |
4751 | } | |
4752 | ||
4753 | if (exclude) | |
4754 | { | |
4755 | /* We don't set bfd_section to NULL since bfd_section of the | |
4756 | removed output section statement may still be used. */ | |
4757 | if (!os->update_dot) | |
4758 | os->ignored = true; | |
4759 | output_section->flags |= SEC_EXCLUDE; | |
4760 | bfd_section_list_remove (link_info.output_bfd, output_section); | |
4761 | link_info.output_bfd->section_count--; | |
4762 | } | |
4763 | } | |
4764 | } | |
4765 | ||
4766 | /* Called from ldwrite to clear out asection.map_head and | |
4767 | asection.map_tail for use as link_orders in ldwrite. */ | |
4768 | ||
4769 | void | |
4770 | lang_clear_os_map (void) | |
4771 | { | |
4772 | lang_output_section_statement_type *os; | |
4773 | ||
4774 | if (map_head_is_link_order) | |
4775 | return; | |
4776 | ||
4777 | for (os = (void *) lang_os_list.head; | |
4778 | os != NULL; | |
4779 | os = os->next) | |
4780 | { | |
4781 | asection *output_section; | |
4782 | ||
4783 | if (os->constraint < 0) | |
4784 | continue; | |
4785 | ||
4786 | output_section = os->bfd_section; | |
4787 | if (output_section == NULL) | |
4788 | continue; | |
4789 | ||
4790 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4791 | output_section->map_head.link_order = NULL; | |
4792 | output_section->map_tail.link_order = NULL; | |
4793 | } | |
4794 | ||
4795 | /* Stop future calls to lang_add_section from messing with map_head | |
4796 | and map_tail link_order fields. */ | |
4797 | map_head_is_link_order = true; | |
4798 | } | |
4799 | ||
4800 | static void | |
4801 | print_output_section_statement | |
4802 | (lang_output_section_statement_type *output_section_statement) | |
4803 | { | |
4804 | asection *section = output_section_statement->bfd_section; | |
4805 | int len; | |
4806 | ||
4807 | if (output_section_statement != abs_output_section) | |
4808 | { | |
4809 | minfo ("\n%s", output_section_statement->name); | |
4810 | ||
4811 | if (section != NULL) | |
4812 | { | |
4813 | print_dot = section->vma; | |
4814 | ||
4815 | len = strlen (output_section_statement->name); | |
4816 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4817 | { | |
4818 | print_nl (); | |
4819 | len = 0; | |
4820 | } | |
4821 | print_spaces (SECTION_NAME_MAP_LENGTH - len); | |
4822 | ||
4823 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); | |
4824 | ||
4825 | if (section->vma != section->lma) | |
4826 | minfo (_(" load address 0x%V"), section->lma); | |
4827 | ||
4828 | if (output_section_statement->update_dot_tree != NULL) | |
4829 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4830 | output_section_statement, | |
4831 | bfd_abs_section_ptr, &print_dot); | |
4832 | } | |
4833 | ||
4834 | print_nl (); | |
4835 | } | |
4836 | ||
4837 | print_statement_list (output_section_statement->children.head, | |
4838 | output_section_statement); | |
4839 | } | |
4840 | ||
4841 | static void | |
4842 | print_assignment (lang_assignment_statement_type *assignment, | |
4843 | lang_output_section_statement_type *output_section) | |
4844 | { | |
4845 | bool is_dot; | |
4846 | etree_type *tree; | |
4847 | asection *osec; | |
4848 | ||
4849 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
4850 | ||
4851 | if (assignment->exp->type.node_class == etree_assert) | |
4852 | { | |
4853 | is_dot = false; | |
4854 | tree = assignment->exp->assert_s.child; | |
4855 | } | |
4856 | else | |
4857 | { | |
4858 | const char *dst = assignment->exp->assign.dst; | |
4859 | ||
4860 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
4861 | tree = assignment->exp; | |
4862 | } | |
4863 | ||
4864 | osec = output_section->bfd_section; | |
4865 | if (osec == NULL) | |
4866 | osec = bfd_abs_section_ptr; | |
4867 | ||
4868 | if (assignment->exp->type.node_class != etree_provide) | |
4869 | exp_fold_tree (tree, output_section, osec, &print_dot); | |
4870 | else | |
4871 | expld.result.valid_p = false; | |
4872 | ||
4873 | char buf[32]; | |
4874 | const char *str = buf; | |
4875 | if (expld.result.valid_p) | |
4876 | { | |
4877 | bfd_vma value; | |
4878 | ||
4879 | if (assignment->exp->type.node_class == etree_assert | |
4880 | || is_dot | |
4881 | || expld.assign_name != NULL) | |
4882 | { | |
4883 | value = expld.result.value; | |
4884 | ||
4885 | if (expld.result.section != NULL) | |
4886 | value += expld.result.section->vma; | |
4887 | ||
4888 | buf[0] = '0'; | |
4889 | buf[1] = 'x'; | |
4890 | bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); | |
4891 | if (is_dot) | |
4892 | print_dot = value; | |
4893 | } | |
4894 | else | |
4895 | { | |
4896 | struct bfd_link_hash_entry *h; | |
4897 | ||
4898 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
4899 | false, false, true); | |
4900 | if (is_defined (h)) | |
4901 | { | |
4902 | value = h->u.def.value; | |
4903 | value += h->u.def.section->output_section->vma; | |
4904 | value += h->u.def.section->output_offset; | |
4905 | ||
4906 | buf[0] = '['; | |
4907 | buf[1] = '0'; | |
4908 | buf[2] = 'x'; | |
4909 | bfd_sprintf_vma (link_info.output_bfd, buf + 3, value); | |
4910 | strcat (buf, "]"); | |
4911 | } | |
4912 | else | |
4913 | str = "[unresolved]"; | |
4914 | } | |
4915 | } | |
4916 | else | |
4917 | { | |
4918 | if (assignment->exp->type.node_class == etree_provide) | |
4919 | str = "[!provide]"; | |
4920 | else | |
4921 | str = "*undef*"; | |
4922 | } | |
4923 | expld.assign_name = NULL; | |
4924 | ||
4925 | fprintf (config.map_file, "%-34s", str); | |
4926 | exp_print_tree (assignment->exp); | |
4927 | print_nl (); | |
4928 | } | |
4929 | ||
4930 | static void | |
4931 | print_input_statement (lang_input_statement_type *statm) | |
4932 | { | |
4933 | if (statm->filename != NULL) | |
4934 | fprintf (config.map_file, "LOAD %s\n", statm->filename); | |
4935 | } | |
4936 | ||
4937 | /* Print all symbols defined in a particular section. This is called | |
4938 | via bfd_link_hash_traverse, or by print_all_symbols. */ | |
4939 | ||
4940 | bool | |
4941 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) | |
4942 | { | |
4943 | asection *sec = (asection *) ptr; | |
4944 | ||
4945 | if (is_defined (hash_entry) | |
4946 | && sec == hash_entry->u.def.section) | |
4947 | { | |
4948 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
4949 | minfo ("0x%V ", | |
4950 | (hash_entry->u.def.value | |
4951 | + hash_entry->u.def.section->output_offset | |
4952 | + hash_entry->u.def.section->output_section->vma)); | |
4953 | ||
4954 | minfo (" %pT\n", hash_entry->root.string); | |
4955 | } | |
4956 | ||
4957 | return true; | |
4958 | } | |
4959 | ||
4960 | static int | |
4961 | hash_entry_addr_cmp (const void *a, const void *b) | |
4962 | { | |
4963 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4964 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4965 | ||
4966 | if (l->u.def.value < r->u.def.value) | |
4967 | return -1; | |
4968 | else if (l->u.def.value > r->u.def.value) | |
4969 | return 1; | |
4970 | else | |
4971 | return 0; | |
4972 | } | |
4973 | ||
4974 | static void | |
4975 | print_all_symbols (asection *sec) | |
4976 | { | |
4977 | input_section_userdata_type *ud = bfd_section_userdata (sec); | |
4978 | struct map_symbol_def *def; | |
4979 | struct bfd_link_hash_entry **entries; | |
4980 | unsigned int i; | |
4981 | ||
4982 | if (!ud) | |
4983 | return; | |
4984 | ||
4985 | *ud->map_symbol_def_tail = 0; | |
4986 | ||
4987 | /* Sort the symbols by address. */ | |
4988 | entries = (struct bfd_link_hash_entry **) | |
4989 | obstack_alloc (&map_obstack, | |
4990 | ud->map_symbol_def_count * sizeof (*entries)); | |
4991 | ||
4992 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4993 | entries[i] = def->entry; | |
4994 | ||
4995 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4996 | hash_entry_addr_cmp); | |
4997 | ||
4998 | /* Print the symbols. */ | |
4999 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
5000 | ldemul_print_symbol (entries[i], sec); | |
5001 | ||
5002 | obstack_free (&map_obstack, entries); | |
5003 | } | |
5004 | ||
5005 | /* Returns TRUE if SYM is a symbol suitable for printing | |
5006 | in a linker map as a local symbol. */ | |
5007 | ||
5008 | static bool | |
5009 | ld_is_local_symbol (asymbol * sym) | |
5010 | { | |
5011 | const char * name = bfd_asymbol_name (sym); | |
5012 | ||
5013 | if (name == NULL || *name == 0) | |
5014 | return false; | |
5015 | ||
5016 | /* Skip .Lxxx and such like. */ | |
5017 | if (bfd_is_local_label (link_info.output_bfd, sym)) | |
5018 | return false; | |
5019 | ||
5020 | /* FIXME: This is intended to skip ARM mapping symbols, | |
5021 | which for some reason are not excluded by bfd_is_local_label, | |
5022 | but maybe it is wrong for other architectures. | |
5023 | It would be better to fix bfd_is_local_label. */ | |
5024 | if (*name == '$') | |
5025 | return false; | |
5026 | ||
5027 | /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present | |
5028 | in the hash table, so do not print duplicates here. */ | |
5029 | struct bfd_link_hash_entry * h; | |
5030 | h = bfd_link_hash_lookup (link_info.hash, name, false /* create */, | |
5031 | false /* copy */, true /* follow */); | |
5032 | if (h == NULL) | |
5033 | return true; | |
5034 | ||
5035 | /* Symbols from the plugin owned BFD will not get their own | |
5036 | iteration of this function, but can be on the link_info | |
5037 | list. So include them here. */ | |
5038 | if (h->u.def.section->owner != NULL | |
5039 | && ((bfd_get_file_flags (h->u.def.section->owner) & (BFD_LINKER_CREATED | BFD_PLUGIN)) | |
5040 | == (BFD_LINKER_CREATED | BFD_PLUGIN))) | |
5041 | return true; | |
5042 | ||
5043 | return false; | |
5044 | } | |
5045 | ||
5046 | /* Print information about an input section to the map file. */ | |
5047 | ||
5048 | static void | |
5049 | print_input_section (asection *i, bool is_discarded) | |
5050 | { | |
5051 | bfd_size_type size = i->size; | |
5052 | int len; | |
5053 | bfd_vma addr; | |
5054 | ||
5055 | init_opb (i); | |
5056 | ||
5057 | minfo (" %s", i->name); | |
5058 | ||
5059 | len = 1 + strlen (i->name); | |
5060 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
5061 | { | |
5062 | print_nl (); | |
5063 | len = 0; | |
5064 | } | |
5065 | print_spaces (SECTION_NAME_MAP_LENGTH - len); | |
5066 | ||
5067 | if ((i->flags & SEC_EXCLUDE) == 0 | |
5068 | && i->output_section != NULL | |
5069 | && i->output_section->owner == link_info.output_bfd) | |
5070 | addr = i->output_section->vma + i->output_offset; | |
5071 | else | |
5072 | { | |
5073 | addr = print_dot; | |
5074 | if (!is_discarded) | |
5075 | size = 0; | |
5076 | } | |
5077 | ||
5078 | char buf[32]; | |
5079 | bfd_sprintf_vma (link_info.output_bfd, buf, addr); | |
5080 | minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner); | |
5081 | ||
5082 | if (size != i->rawsize && i->rawsize != 0) | |
5083 | { | |
5084 | len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); | |
5085 | print_spaces (len); | |
5086 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); | |
5087 | } | |
5088 | ||
5089 | if (i->output_section != NULL | |
5090 | && i->output_section->owner == link_info.output_bfd) | |
5091 | { | |
5092 | if (link_info.reduce_memory_overheads) | |
5093 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); | |
5094 | else | |
5095 | print_all_symbols (i); | |
5096 | ||
5097 | /* Update print_dot, but make sure that we do not move it | |
5098 | backwards - this could happen if we have overlays and a | |
5099 | later overlay is shorter than an earier one. */ | |
5100 | if (addr + TO_ADDR (size) > print_dot) | |
5101 | print_dot = addr + TO_ADDR (size); | |
5102 | ||
5103 | if (config.print_map_locals) | |
5104 | { | |
5105 | long storage_needed; | |
5106 | ||
5107 | /* FIXME: It would be better to cache this table, rather | |
5108 | than recreating it for each output section. */ | |
5109 | /* FIXME: This call is not working for non-ELF based targets. | |
5110 | Find out why. */ | |
5111 | storage_needed = bfd_get_symtab_upper_bound (link_info.output_bfd); | |
5112 | if (storage_needed > 0) | |
5113 | { | |
5114 | asymbol ** symbol_table; | |
5115 | long number_of_symbols; | |
5116 | long j; | |
5117 | ||
5118 | symbol_table = xmalloc (storage_needed); | |
5119 | number_of_symbols = bfd_canonicalize_symtab (link_info.output_bfd, symbol_table); | |
5120 | ||
5121 | for (j = 0; j < number_of_symbols; j++) | |
5122 | { | |
5123 | asymbol * sym = symbol_table[j]; | |
5124 | bfd_vma sym_addr = sym->value + i->output_section->vma; | |
5125 | ||
5126 | if (sym->section == i->output_section | |
5127 | && (sym->flags & BSF_LOCAL) != 0 | |
5128 | && sym_addr >= addr | |
5129 | && sym_addr < print_dot | |
5130 | && ld_is_local_symbol (sym)) | |
5131 | { | |
5132 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
5133 | minfo ("0x%V (local) %s\n", sym_addr, bfd_asymbol_name (sym)); | |
5134 | } | |
5135 | } | |
5136 | ||
5137 | free (symbol_table); | |
5138 | } | |
5139 | } | |
5140 | } | |
5141 | } | |
5142 | ||
5143 | static void | |
5144 | print_fill_statement (lang_fill_statement_type *fill) | |
5145 | { | |
5146 | size_t size; | |
5147 | unsigned char *p; | |
5148 | fputs (" FILL mask 0x", config.map_file); | |
5149 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
5150 | fprintf (config.map_file, "%02x", *p); | |
5151 | fputs ("\n", config.map_file); | |
5152 | } | |
5153 | ||
5154 | static void | |
5155 | print_data_statement (lang_data_statement_type *data) | |
5156 | { | |
5157 | bfd_vma addr; | |
5158 | bfd_size_type size; | |
5159 | const char *name; | |
5160 | ||
5161 | init_opb (data->output_section); | |
5162 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
5163 | ||
5164 | addr = data->output_offset; | |
5165 | if (data->output_section != NULL) | |
5166 | addr += data->output_section->vma; | |
5167 | ||
5168 | switch (data->type) | |
5169 | { | |
5170 | default: | |
5171 | abort (); | |
5172 | case BYTE: | |
5173 | size = BYTE_SIZE; | |
5174 | name = "BYTE"; | |
5175 | break; | |
5176 | case SHORT: | |
5177 | size = SHORT_SIZE; | |
5178 | name = "SHORT"; | |
5179 | break; | |
5180 | case LONG: | |
5181 | size = LONG_SIZE; | |
5182 | name = "LONG"; | |
5183 | break; | |
5184 | case QUAD: | |
5185 | size = QUAD_SIZE; | |
5186 | name = "QUAD"; | |
5187 | break; | |
5188 | case SQUAD: | |
5189 | size = QUAD_SIZE; | |
5190 | name = "SQUAD"; | |
5191 | break; | |
5192 | } | |
5193 | ||
5194 | if (size < TO_SIZE ((unsigned) 1)) | |
5195 | size = TO_SIZE ((unsigned) 1); | |
5196 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
5197 | ||
5198 | if (data->exp->type.node_class != etree_value) | |
5199 | { | |
5200 | print_space (); | |
5201 | exp_print_tree (data->exp); | |
5202 | } | |
5203 | ||
5204 | print_nl (); | |
5205 | ||
5206 | print_dot = addr + TO_ADDR (size); | |
5207 | } | |
5208 | ||
5209 | /* Print an address statement. These are generated by options like | |
5210 | -Ttext. */ | |
5211 | ||
5212 | static void | |
5213 | print_address_statement (lang_address_statement_type *address) | |
5214 | { | |
5215 | minfo (_("Address of section %s set to "), address->section_name); | |
5216 | exp_print_tree (address->address); | |
5217 | print_nl (); | |
5218 | } | |
5219 | ||
5220 | /* Print a reloc statement. */ | |
5221 | ||
5222 | static void | |
5223 | print_reloc_statement (lang_reloc_statement_type *reloc) | |
5224 | { | |
5225 | bfd_vma addr; | |
5226 | bfd_size_type size; | |
5227 | ||
5228 | init_opb (reloc->output_section); | |
5229 | print_spaces (SECTION_NAME_MAP_LENGTH); | |
5230 | ||
5231 | addr = reloc->output_offset; | |
5232 | if (reloc->output_section != NULL) | |
5233 | addr += reloc->output_section->vma; | |
5234 | ||
5235 | size = bfd_get_reloc_size (reloc->howto); | |
5236 | ||
5237 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); | |
5238 | ||
5239 | if (reloc->name != NULL) | |
5240 | minfo ("%s+", reloc->name); | |
5241 | else | |
5242 | minfo ("%s+", reloc->section->name); | |
5243 | ||
5244 | exp_print_tree (reloc->addend_exp); | |
5245 | ||
5246 | print_nl (); | |
5247 | ||
5248 | print_dot = addr + TO_ADDR (size); | |
5249 | } | |
5250 | ||
5251 | static void | |
5252 | print_padding_statement (lang_padding_statement_type *s) | |
5253 | { | |
5254 | int len; | |
5255 | bfd_vma addr; | |
5256 | ||
5257 | init_opb (s->output_section); | |
5258 | minfo (" *fill*"); | |
5259 | ||
5260 | len = sizeof " *fill*" - 1; | |
5261 | print_spaces (SECTION_NAME_MAP_LENGTH - len); | |
5262 | ||
5263 | addr = s->output_offset; | |
5264 | if (s->output_section != NULL) | |
5265 | addr += s->output_section->vma; | |
5266 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); | |
5267 | ||
5268 | if (s->fill->size != 0) | |
5269 | { | |
5270 | size_t size; | |
5271 | unsigned char *p; | |
5272 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
5273 | fprintf (config.map_file, "%02x", *p); | |
5274 | } | |
5275 | ||
5276 | print_nl (); | |
5277 | ||
5278 | print_dot = addr + TO_ADDR (s->size); | |
5279 | } | |
5280 | ||
5281 | static void | |
5282 | print_wild_statement (lang_wild_statement_type *w, | |
5283 | lang_output_section_statement_type *os) | |
5284 | { | |
5285 | struct wildcard_list *sec; | |
5286 | ||
5287 | print_space (); | |
5288 | ||
5289 | if (w->exclude_name_list) | |
5290 | { | |
5291 | name_list *tmp; | |
5292 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
5293 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
5294 | minfo (" %s", tmp->name); | |
5295 | minfo (") "); | |
5296 | } | |
5297 | ||
5298 | if (w->filenames_sorted) | |
5299 | minfo ("SORT_BY_NAME("); | |
5300 | if (w->filenames_reversed) | |
5301 | minfo ("REVERSE("); | |
5302 | if (w->filename != NULL) | |
5303 | minfo ("%s", w->filename); | |
5304 | else | |
5305 | minfo ("*"); | |
5306 | if (w->filenames_reversed) | |
5307 | minfo (")"); | |
5308 | if (w->filenames_sorted) | |
5309 | minfo (")"); | |
5310 | ||
5311 | minfo ("("); | |
5312 | for (sec = w->section_list; sec; sec = sec->next) | |
5313 | { | |
5314 | int closing_paren = 0; | |
5315 | ||
5316 | switch (sec->spec.sorted) | |
5317 | { | |
5318 | case none: | |
5319 | break; | |
5320 | ||
5321 | case by_name: | |
5322 | minfo ("SORT_BY_NAME("); | |
5323 | closing_paren = 1; | |
5324 | break; | |
5325 | ||
5326 | case by_alignment: | |
5327 | minfo ("SORT_BY_ALIGNMENT("); | |
5328 | closing_paren = 1; | |
5329 | break; | |
5330 | ||
5331 | case by_name_alignment: | |
5332 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
5333 | closing_paren = 2; | |
5334 | break; | |
5335 | ||
5336 | case by_alignment_name: | |
5337 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
5338 | closing_paren = 2; | |
5339 | break; | |
5340 | ||
5341 | case by_none: | |
5342 | minfo ("SORT_NONE("); | |
5343 | closing_paren = 1; | |
5344 | break; | |
5345 | ||
5346 | case by_init_priority: | |
5347 | minfo ("SORT_BY_INIT_PRIORITY("); | |
5348 | closing_paren = 1; | |
5349 | break; | |
5350 | } | |
5351 | ||
5352 | if (sec->spec.reversed) | |
5353 | { | |
5354 | minfo ("REVERSE("); | |
5355 | closing_paren++; | |
5356 | } | |
5357 | ||
5358 | if (sec->spec.exclude_name_list != NULL) | |
5359 | { | |
5360 | name_list *tmp; | |
5361 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); | |
5362 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) | |
5363 | minfo (" %s", tmp->name); | |
5364 | minfo (") "); | |
5365 | } | |
5366 | if (sec->spec.name != NULL) | |
5367 | minfo ("%s", sec->spec.name); | |
5368 | else | |
5369 | minfo ("*"); | |
5370 | for (;closing_paren > 0; closing_paren--) | |
5371 | minfo (")"); | |
5372 | if (sec->next) | |
5373 | minfo (" "); | |
5374 | } | |
5375 | minfo (")"); | |
5376 | ||
5377 | print_nl (); | |
5378 | ||
5379 | print_statement_list (w->children.head, os); | |
5380 | } | |
5381 | ||
5382 | /* Print a group statement. */ | |
5383 | ||
5384 | static void | |
5385 | print_group (lang_group_statement_type *s, | |
5386 | lang_output_section_statement_type *os) | |
5387 | { | |
5388 | fprintf (config.map_file, "START GROUP\n"); | |
5389 | print_statement_list (s->children.head, os); | |
5390 | fprintf (config.map_file, "END GROUP\n"); | |
5391 | } | |
5392 | ||
5393 | /* Print the list of statements in S. | |
5394 | This can be called for any statement type. */ | |
5395 | ||
5396 | static void | |
5397 | print_statement_list (lang_statement_union_type *s, | |
5398 | lang_output_section_statement_type *os) | |
5399 | { | |
5400 | while (s != NULL) | |
5401 | { | |
5402 | print_statement (s, os); | |
5403 | s = s->header.next; | |
5404 | } | |
5405 | } | |
5406 | ||
5407 | /* Print the first statement in statement list S. | |
5408 | This can be called for any statement type. */ | |
5409 | ||
5410 | static void | |
5411 | print_statement (lang_statement_union_type *s, | |
5412 | lang_output_section_statement_type *os) | |
5413 | { | |
5414 | switch (s->header.type) | |
5415 | { | |
5416 | default: | |
5417 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
5418 | FAIL (); | |
5419 | break; | |
5420 | case lang_constructors_statement_enum: | |
5421 | if (constructor_list.head != NULL) | |
5422 | { | |
5423 | if (constructors_sorted) | |
5424 | minfo (" SORT (CONSTRUCTORS)\n"); | |
5425 | else | |
5426 | minfo (" CONSTRUCTORS\n"); | |
5427 | print_statement_list (constructor_list.head, os); | |
5428 | } | |
5429 | break; | |
5430 | case lang_wild_statement_enum: | |
5431 | print_wild_statement (&s->wild_statement, os); | |
5432 | break; | |
5433 | case lang_address_statement_enum: | |
5434 | print_address_statement (&s->address_statement); | |
5435 | break; | |
5436 | case lang_object_symbols_statement_enum: | |
5437 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
5438 | break; | |
5439 | case lang_fill_statement_enum: | |
5440 | print_fill_statement (&s->fill_statement); | |
5441 | break; | |
5442 | case lang_data_statement_enum: | |
5443 | print_data_statement (&s->data_statement); | |
5444 | break; | |
5445 | case lang_reloc_statement_enum: | |
5446 | print_reloc_statement (&s->reloc_statement); | |
5447 | break; | |
5448 | case lang_input_section_enum: | |
5449 | print_input_section (s->input_section.section, false); | |
5450 | break; | |
5451 | case lang_padding_statement_enum: | |
5452 | print_padding_statement (&s->padding_statement); | |
5453 | break; | |
5454 | case lang_output_section_statement_enum: | |
5455 | print_output_section_statement (&s->output_section_statement); | |
5456 | break; | |
5457 | case lang_assignment_statement_enum: | |
5458 | print_assignment (&s->assignment_statement, os); | |
5459 | break; | |
5460 | case lang_target_statement_enum: | |
5461 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5462 | break; | |
5463 | case lang_output_statement_enum: | |
5464 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5465 | if (output_target != NULL) | |
5466 | minfo (" %s", output_target); | |
5467 | minfo (")\n"); | |
5468 | break; | |
5469 | case lang_input_statement_enum: | |
5470 | print_input_statement (&s->input_statement); | |
5471 | break; | |
5472 | case lang_group_statement_enum: | |
5473 | print_group (&s->group_statement, os); | |
5474 | break; | |
5475 | case lang_insert_statement_enum: | |
5476 | minfo ("INSERT %s %s\n", | |
5477 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5478 | s->insert_statement.where); | |
5479 | break; | |
5480 | } | |
5481 | } | |
5482 | ||
5483 | static void | |
5484 | print_statements (void) | |
5485 | { | |
5486 | print_statement_list (statement_list.head, abs_output_section); | |
5487 | } | |
5488 | ||
5489 | /* Print the first N statements in statement list S to STDERR. | |
5490 | If N == 0, nothing is printed. | |
5491 | If N < 0, the entire list is printed. | |
5492 | Intended to be called from GDB. */ | |
5493 | ||
5494 | void | |
5495 | dprint_statement (lang_statement_union_type *s, int n) | |
5496 | { | |
5497 | FILE *map_save = config.map_file; | |
5498 | ||
5499 | config.map_file = stderr; | |
5500 | ||
5501 | if (n < 0) | |
5502 | print_statement_list (s, abs_output_section); | |
5503 | else | |
5504 | { | |
5505 | while (s && --n >= 0) | |
5506 | { | |
5507 | print_statement (s, abs_output_section); | |
5508 | s = s->header.next; | |
5509 | } | |
5510 | } | |
5511 | ||
5512 | config.map_file = map_save; | |
5513 | } | |
5514 | ||
5515 | static void | |
5516 | insert_pad (lang_statement_union_type **ptr, | |
5517 | fill_type *fill, | |
5518 | bfd_size_type alignment_needed, | |
5519 | asection *output_section, | |
5520 | bfd_vma dot) | |
5521 | { | |
5522 | static fill_type zero_fill; | |
5523 | lang_statement_union_type *pad = NULL; | |
5524 | ||
5525 | if (ptr != &statement_list.head) | |
5526 | pad = ((lang_statement_union_type *) | |
5527 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5528 | if (pad != NULL | |
5529 | && pad->header.type == lang_padding_statement_enum | |
5530 | && pad->padding_statement.output_section == output_section) | |
5531 | { | |
5532 | /* Use the existing pad statement. */ | |
5533 | } | |
5534 | else if ((pad = *ptr) != NULL | |
5535 | && pad->header.type == lang_padding_statement_enum | |
5536 | && pad->padding_statement.output_section == output_section) | |
5537 | { | |
5538 | /* Use the existing pad statement. */ | |
5539 | } | |
5540 | else | |
5541 | { | |
5542 | /* Make a new padding statement, linked into existing chain. */ | |
5543 | pad = stat_alloc (sizeof (lang_padding_statement_type)); | |
5544 | pad->header.next = *ptr; | |
5545 | *ptr = pad; | |
5546 | pad->header.type = lang_padding_statement_enum; | |
5547 | pad->padding_statement.output_section = output_section; | |
5548 | if (fill == NULL) | |
5549 | fill = &zero_fill; | |
5550 | pad->padding_statement.fill = fill; | |
5551 | } | |
5552 | pad->padding_statement.output_offset = dot - output_section->vma; | |
5553 | pad->padding_statement.size = alignment_needed; | |
5554 | if (!(output_section->flags & SEC_FIXED_SIZE)) | |
5555 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5556 | - output_section->vma); | |
5557 | } | |
5558 | ||
5559 | /* Work out how much this section will move the dot point. */ | |
5560 | ||
5561 | static bfd_vma | |
5562 | size_input_section | |
5563 | (lang_statement_union_type **this_ptr, | |
5564 | lang_output_section_statement_type *output_section_statement, | |
5565 | fill_type *fill, | |
5566 | bool *removed, | |
5567 | bfd_vma dot) | |
5568 | { | |
5569 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5570 | asection *i = is->section; | |
5571 | asection *o = output_section_statement->bfd_section; | |
5572 | *removed = 0; | |
5573 | ||
5574 | if (link_info.non_contiguous_regions) | |
5575 | { | |
5576 | /* If the input section I has already been successfully assigned | |
5577 | to an output section other than O, don't bother with it and | |
5578 | let the caller remove it from the list. Keep processing in | |
5579 | case we have already handled O, because the repeated passes | |
5580 | have reinitialized its size. */ | |
5581 | if (i->already_assigned && i->already_assigned != o) | |
5582 | { | |
5583 | *removed = 1; | |
5584 | return dot; | |
5585 | } | |
5586 | } | |
5587 | ||
5588 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
5589 | i->output_offset = i->vma - o->vma; | |
5590 | else if (((i->flags & SEC_EXCLUDE) != 0) | |
5591 | || output_section_statement->ignored) | |
5592 | i->output_offset = dot - o->vma; | |
5593 | else | |
5594 | { | |
5595 | bfd_size_type alignment_needed; | |
5596 | ||
5597 | /* Align this section first to the input sections requirement, | |
5598 | then to the output section's requirement. If this alignment | |
5599 | is greater than any seen before, then record it too. Perform | |
5600 | the alignment by inserting a magic 'padding' statement. | |
5601 | We can force input section alignment within an output section | |
5602 | by using SUBALIGN. The value specified overrides any alignment | |
5603 | given by input sections, whether larger or smaller. */ | |
5604 | ||
5605 | if (output_section_statement->subsection_alignment != NULL) | |
5606 | o->alignment_power = i->alignment_power = | |
5607 | exp_get_power (output_section_statement->subsection_alignment, | |
5608 | output_section_statement, | |
5609 | "subsection alignment"); | |
5610 | ||
5611 | if (o->alignment_power < i->alignment_power) | |
5612 | o->alignment_power = i->alignment_power; | |
5613 | ||
5614 | alignment_needed = align_power (dot, i->alignment_power) - dot; | |
5615 | ||
5616 | if (alignment_needed != 0) | |
5617 | { | |
5618 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); | |
5619 | dot += alignment_needed; | |
5620 | } | |
5621 | ||
5622 | if (link_info.non_contiguous_regions) | |
5623 | { | |
5624 | /* If I would overflow O, let the caller remove I from the | |
5625 | list. */ | |
5626 | if (output_section_statement->region) | |
5627 | { | |
5628 | bfd_vma end = output_section_statement->region->origin | |
5629 | + output_section_statement->region->length; | |
5630 | ||
5631 | if (dot + TO_ADDR (i->size) > end) | |
5632 | { | |
5633 | if (i->flags & SEC_LINKER_CREATED) | |
5634 | fatal (_("%P: Output section `%pA' not large enough for " | |
5635 | "the linker-created stubs section `%pA'.\n"), | |
5636 | i->output_section, i); | |
5637 | ||
5638 | if (i->rawsize && i->rawsize != i->size) | |
5639 | fatal (_("%P: Relaxation not supported with " | |
5640 | "--enable-non-contiguous-regions (section `%pA' " | |
5641 | "would overflow `%pA' after it changed size).\n"), | |
5642 | i, i->output_section); | |
5643 | ||
5644 | *removed = 1; | |
5645 | dot = end; | |
5646 | i->output_section = NULL; | |
5647 | return dot; | |
5648 | } | |
5649 | } | |
5650 | } | |
5651 | ||
5652 | /* Remember where in the output section this input section goes. */ | |
5653 | i->output_offset = dot - o->vma; | |
5654 | ||
5655 | /* Mark how big the output section must be to contain this now. */ | |
5656 | dot += TO_ADDR (i->size); | |
5657 | if (!(o->flags & SEC_FIXED_SIZE)) | |
5658 | o->size = TO_SIZE (dot - o->vma); | |
5659 | ||
5660 | if (link_info.non_contiguous_regions) | |
5661 | { | |
5662 | /* Record that I was successfully assigned to O, and update | |
5663 | its actual output section too. */ | |
5664 | i->already_assigned = o; | |
5665 | i->output_section = o; | |
5666 | } | |
5667 | } | |
5668 | ||
5669 | return dot; | |
5670 | } | |
5671 | ||
5672 | struct check_sec | |
5673 | { | |
5674 | asection *sec; | |
5675 | bool warned; | |
5676 | }; | |
5677 | ||
5678 | static int | |
5679 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5680 | { | |
5681 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5682 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5683 | ||
5684 | if (sec1->lma < sec2->lma) | |
5685 | return -1; | |
5686 | else if (sec1->lma > sec2->lma) | |
5687 | return 1; | |
5688 | else if (sec1->id < sec2->id) | |
5689 | return -1; | |
5690 | else if (sec1->id > sec2->id) | |
5691 | return 1; | |
5692 | ||
5693 | return 0; | |
5694 | } | |
5695 | ||
5696 | static int | |
5697 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5698 | { | |
5699 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5700 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5701 | ||
5702 | if (sec1->vma < sec2->vma) | |
5703 | return -1; | |
5704 | else if (sec1->vma > sec2->vma) | |
5705 | return 1; | |
5706 | else if (sec1->id < sec2->id) | |
5707 | return -1; | |
5708 | else if (sec1->id > sec2->id) | |
5709 | return 1; | |
5710 | ||
5711 | return 0; | |
5712 | } | |
5713 | ||
5714 | #define IS_TBSS(s) \ | |
5715 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5716 | ||
5717 | #define IGNORE_SECTION(s) \ | |
5718 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) | |
5719 | ||
5720 | /* Check to see if any allocated sections overlap with other allocated | |
5721 | sections. This can happen if a linker script specifies the output | |
5722 | section addresses of the two sections. Also check whether any memory | |
5723 | region has overflowed. */ | |
5724 | ||
5725 | static void | |
5726 | lang_check_section_addresses (void) | |
5727 | { | |
5728 | asection *s, *p; | |
5729 | struct check_sec *sections; | |
5730 | size_t i, count; | |
5731 | bfd_vma addr_mask; | |
5732 | bfd_vma s_start; | |
5733 | bfd_vma s_end; | |
5734 | bfd_vma p_start = 0; | |
5735 | bfd_vma p_end = 0; | |
5736 | lang_memory_region_type *m; | |
5737 | bool overlays; | |
5738 | ||
5739 | /* Detect address space overflow on allocated sections. */ | |
5740 | addr_mask = ((bfd_vma) 1 << | |
5741 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5742 | addr_mask = (addr_mask << 1) + 1; | |
5743 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
5744 | if ((s->flags & SEC_ALLOC) != 0) | |
5745 | { | |
5746 | s_end = (s->vma + s->size) & addr_mask; | |
5747 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5748 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5749 | s->name); | |
5750 | else | |
5751 | { | |
5752 | s_end = (s->lma + s->size) & addr_mask; | |
5753 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5754 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5755 | s->name); | |
5756 | } | |
5757 | } | |
5758 | ||
5759 | if (bfd_count_sections (link_info.output_bfd) <= 1) | |
5760 | return; | |
5761 | ||
5762 | count = bfd_count_sections (link_info.output_bfd); | |
5763 | sections = XNEWVEC (struct check_sec, count); | |
5764 | ||
5765 | /* Scan all sections in the output list. */ | |
5766 | count = 0; | |
5767 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
5768 | { | |
5769 | if (IGNORE_SECTION (s) | |
5770 | || s->size == 0) | |
5771 | continue; | |
5772 | ||
5773 | sections[count].sec = s; | |
5774 | sections[count].warned = false; | |
5775 | count++; | |
5776 | } | |
5777 | ||
5778 | if (count <= 1) | |
5779 | { | |
5780 | free (sections); | |
5781 | return; | |
5782 | } | |
5783 | ||
5784 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); | |
5785 | ||
5786 | /* First check section LMAs. There should be no overlap of LMAs on | |
5787 | loadable sections, even with overlays. */ | |
5788 | for (p = NULL, i = 0; i < count; i++) | |
5789 | { | |
5790 | s = sections[i].sec; | |
5791 | init_opb (s); | |
5792 | if ((s->flags & SEC_LOAD) != 0) | |
5793 | { | |
5794 | s_start = s->lma; | |
5795 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5796 | ||
5797 | /* Look for an overlap. We have sorted sections by lma, so | |
5798 | we know that s_start >= p_start. Besides the obvious | |
5799 | case of overlap when the current section starts before | |
5800 | the previous one ends, we also must have overlap if the | |
5801 | previous section wraps around the address space. */ | |
5802 | if (p != NULL | |
5803 | && (s_start <= p_end | |
5804 | || p_end < p_start)) | |
5805 | { | |
5806 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5807 | " overlaps section %s LMA [%V,%V]\n"), | |
5808 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5809 | sections[i].warned = true; | |
5810 | } | |
5811 | p = s; | |
5812 | p_start = s_start; | |
5813 | p_end = s_end; | |
5814 | } | |
5815 | } | |
5816 | ||
5817 | /* If any non-zero size allocated section (excluding tbss) starts at | |
5818 | exactly the same VMA as another such section, then we have | |
5819 | overlays. Overlays generated by the OVERLAY keyword will have | |
5820 | this property. It is possible to intentionally generate overlays | |
5821 | that fail this test, but it would be unusual. */ | |
5822 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
5823 | overlays = false; | |
5824 | p_start = sections[0].sec->vma; | |
5825 | for (i = 1; i < count; i++) | |
5826 | { | |
5827 | s_start = sections[i].sec->vma; | |
5828 | if (p_start == s_start) | |
5829 | { | |
5830 | overlays = true; | |
5831 | break; | |
5832 | } | |
5833 | p_start = s_start; | |
5834 | } | |
5835 | ||
5836 | /* Now check section VMAs if no overlays were detected. */ | |
5837 | if (!overlays) | |
5838 | { | |
5839 | for (p = NULL, i = 0; i < count; i++) | |
5840 | { | |
5841 | s = sections[i].sec; | |
5842 | init_opb (s); | |
5843 | s_start = s->vma; | |
5844 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5845 | ||
5846 | if (p != NULL | |
5847 | && !sections[i].warned | |
5848 | && (s_start <= p_end | |
5849 | || p_end < p_start)) | |
5850 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5851 | " overlaps section %s VMA [%V,%V]\n"), | |
5852 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5853 | p = s; | |
5854 | p_start = s_start; | |
5855 | p_end = s_end; | |
5856 | } | |
5857 | } | |
5858 | ||
5859 | free (sections); | |
5860 | ||
5861 | /* If any memory region has overflowed, report by how much. | |
5862 | We do not issue this diagnostic for regions that had sections | |
5863 | explicitly placed outside their bounds; os_region_check's | |
5864 | diagnostics are adequate for that case. | |
5865 | ||
5866 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5867 | might overflow a 32-bit integer. There is, alas, no way to print | |
5868 | a bfd_vma quantity in decimal. */ | |
5869 | for (m = lang_memory_region_list; m; m = m->next) | |
5870 | if (m->had_full_message) | |
5871 | { | |
5872 | unsigned long over = m->current - (m->origin + m->length); | |
5873 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5874 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5875 | over), | |
5876 | m->name_list.name, over); | |
5877 | } | |
5878 | } | |
5879 | ||
5880 | /* Make sure the new address is within the region. We explicitly permit the | |
5881 | current address to be at the exact end of the region when the address is | |
5882 | non-zero, in case the region is at the end of addressable memory and the | |
5883 | calculation wraps around. */ | |
5884 | ||
5885 | static void | |
5886 | os_region_check (lang_output_section_statement_type *os, | |
5887 | lang_memory_region_type *region, | |
5888 | etree_type *tree, | |
5889 | bfd_vma rbase) | |
5890 | { | |
5891 | if ((region->current < region->origin | |
5892 | || (region->current - region->origin > region->length)) | |
5893 | && ((region->current != region->origin + region->length) | |
5894 | || rbase == 0)) | |
5895 | { | |
5896 | if (tree != NULL) | |
5897 | { | |
5898 | einfo (_("%X%P: address 0x%v of %pB section `%s'" | |
5899 | " is not within region `%s'\n"), | |
5900 | region->current, | |
5901 | os->bfd_section->owner, | |
5902 | os->bfd_section->name, | |
5903 | region->name_list.name); | |
5904 | } | |
5905 | else if (!region->had_full_message) | |
5906 | { | |
5907 | region->had_full_message = true; | |
5908 | ||
5909 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), | |
5910 | os->bfd_section->owner, | |
5911 | os->bfd_section->name, | |
5912 | region->name_list.name); | |
5913 | } | |
5914 | } | |
5915 | } | |
5916 | ||
5917 | static void | |
5918 | ldlang_check_relro_region (lang_statement_union_type *s) | |
5919 | { | |
5920 | seg_align_type *seg = &expld.dataseg; | |
5921 | ||
5922 | if (seg->relro == exp_seg_relro_start) | |
5923 | { | |
5924 | if (!seg->relro_start_stat) | |
5925 | seg->relro_start_stat = s; | |
5926 | else | |
5927 | { | |
5928 | ASSERT (seg->relro_start_stat == s); | |
5929 | } | |
5930 | } | |
5931 | else if (seg->relro == exp_seg_relro_end) | |
5932 | { | |
5933 | if (!seg->relro_end_stat) | |
5934 | seg->relro_end_stat = s; | |
5935 | else | |
5936 | { | |
5937 | ASSERT (seg->relro_end_stat == s); | |
5938 | } | |
5939 | } | |
5940 | } | |
5941 | ||
5942 | /* Set the sizes for all the output sections. */ | |
5943 | ||
5944 | static bfd_vma | |
5945 | lang_size_sections_1 | |
5946 | (lang_statement_union_type **prev, | |
5947 | lang_output_section_statement_type *current_os, | |
5948 | fill_type *fill, | |
5949 | bfd_vma dot, | |
5950 | bool *relax, | |
5951 | bool check_regions) | |
5952 | { | |
5953 | lang_statement_union_type *s; | |
5954 | lang_statement_union_type *prev_s = NULL; | |
5955 | bool removed_prev_s = false; | |
5956 | lang_output_section_statement_type *os = current_os; | |
5957 | ||
5958 | /* Size up the sections from their constituent parts. */ | |
5959 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) | |
5960 | { | |
5961 | bool removed = false; | |
5962 | ||
5963 | switch (s->header.type) | |
5964 | { | |
5965 | case lang_output_section_statement_enum: | |
5966 | { | |
5967 | bfd_vma newdot, after, dotdelta; | |
5968 | lang_memory_region_type *r; | |
5969 | int section_alignment = 0; | |
5970 | ||
5971 | os = &s->output_section_statement; | |
5972 | init_opb (os->bfd_section); | |
5973 | if (os->constraint == -1) | |
5974 | break; | |
5975 | ||
5976 | /* FIXME: We shouldn't need to zero section vmas for ld -r | |
5977 | here, in lang_insert_orphan, or in the default linker scripts. | |
5978 | This is covering for coff backend linker bugs. See PR6945. */ | |
5979 | if (os->addr_tree == NULL | |
5980 | && bfd_link_relocatable (&link_info) | |
5981 | && (bfd_get_flavour (link_info.output_bfd) | |
5982 | == bfd_target_coff_flavour)) | |
5983 | os->addr_tree = exp_intop (0); | |
5984 | if (os->addr_tree != NULL) | |
5985 | { | |
5986 | exp_fold_tree (os->addr_tree, os, bfd_abs_section_ptr, &dot); | |
5987 | ||
5988 | if (expld.result.valid_p) | |
5989 | { | |
5990 | dot = expld.result.value; | |
5991 | if (expld.result.section != NULL) | |
5992 | dot += expld.result.section->vma; | |
5993 | } | |
5994 | else if (expld.phase != lang_mark_phase_enum) | |
5995 | fatal (_("%P:%pS: non constant or forward reference" | |
5996 | " address expression for section %s\n"), | |
5997 | os->addr_tree, os->name); | |
5998 | } | |
5999 | ||
6000 | if (os->bfd_section == NULL) | |
6001 | /* This section was removed or never actually created. */ | |
6002 | break; | |
6003 | ||
6004 | /* If this is a COFF shared library section, use the size and | |
6005 | address from the input section. FIXME: This is COFF | |
6006 | specific; it would be cleaner if there were some other way | |
6007 | to do this, but nothing simple comes to mind. */ | |
6008 | if (((bfd_get_flavour (link_info.output_bfd) | |
6009 | == bfd_target_ecoff_flavour) | |
6010 | || (bfd_get_flavour (link_info.output_bfd) | |
6011 | == bfd_target_coff_flavour)) | |
6012 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) | |
6013 | { | |
6014 | asection *input; | |
6015 | ||
6016 | if (os->children.head == NULL | |
6017 | || os->children.head->header.next != NULL | |
6018 | || (os->children.head->header.type | |
6019 | != lang_input_section_enum)) | |
6020 | einfo (_("%X%P: internal error on COFF shared library" | |
6021 | " section %s\n"), os->name); | |
6022 | ||
6023 | input = os->children.head->input_section.section; | |
6024 | bfd_set_section_vma (os->bfd_section, | |
6025 | bfd_section_vma (input)); | |
6026 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) | |
6027 | os->bfd_section->size = input->size; | |
6028 | break; | |
6029 | } | |
6030 | ||
6031 | newdot = dot; | |
6032 | dotdelta = 0; | |
6033 | if (bfd_is_abs_section (os->bfd_section)) | |
6034 | { | |
6035 | /* No matter what happens, an abs section starts at zero. */ | |
6036 | ASSERT (os->bfd_section->vma == 0); | |
6037 | } | |
6038 | else | |
6039 | { | |
6040 | if (os->addr_tree == NULL) | |
6041 | { | |
6042 | /* No address specified for this section, get one | |
6043 | from the region specification. */ | |
6044 | if (os->region == NULL | |
6045 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) | |
6046 | && os->region->name_list.name[0] == '*' | |
6047 | && strcmp (os->region->name_list.name, | |
6048 | DEFAULT_MEMORY_REGION) == 0)) | |
6049 | { | |
6050 | os->region = lang_memory_default (os->bfd_section); | |
6051 | } | |
6052 | ||
6053 | /* If a loadable section is using the default memory | |
6054 | region, and some non default memory regions were | |
6055 | defined, issue an error message. */ | |
6056 | if (!os->ignored | |
6057 | && !IGNORE_SECTION (os->bfd_section) | |
6058 | && !bfd_link_relocatable (&link_info) | |
6059 | && check_regions | |
6060 | && strcmp (os->region->name_list.name, | |
6061 | DEFAULT_MEMORY_REGION) == 0 | |
6062 | && lang_memory_region_list != NULL | |
6063 | && (strcmp (lang_memory_region_list->name_list.name, | |
6064 | DEFAULT_MEMORY_REGION) != 0 | |
6065 | || lang_memory_region_list->next != NULL) | |
6066 | && lang_sizing_iteration == 1) | |
6067 | { | |
6068 | /* By default this is an error rather than just a | |
6069 | warning because if we allocate the section to the | |
6070 | default memory region we can end up creating an | |
6071 | excessively large binary, or even seg faulting when | |
6072 | attempting to perform a negative seek. See | |
6073 | sources.redhat.com/ml/binutils/2003-04/msg00423.html | |
6074 | for an example of this. This behaviour can be | |
6075 | overridden by the using the --no-check-sections | |
6076 | switch. */ | |
6077 | if (command_line.check_section_addresses) | |
6078 | fatal (_("%P: error: no memory region specified" | |
6079 | " for loadable section `%s'\n"), | |
6080 | bfd_section_name (os->bfd_section)); | |
6081 | else | |
6082 | einfo (_("%P: warning: no memory region specified" | |
6083 | " for loadable section `%s'\n"), | |
6084 | bfd_section_name (os->bfd_section)); | |
6085 | } | |
6086 | ||
6087 | newdot = os->region->current; | |
6088 | section_alignment = os->bfd_section->alignment_power; | |
6089 | } | |
6090 | else | |
6091 | section_alignment = exp_get_power (os->section_alignment, os, | |
6092 | "section alignment"); | |
6093 | ||
6094 | /* Align to what the section needs. */ | |
6095 | if (section_alignment > 0) | |
6096 | { | |
6097 | bfd_vma savedot = newdot; | |
6098 | bfd_vma diff = 0; | |
6099 | ||
6100 | newdot = align_power (newdot, section_alignment); | |
6101 | dotdelta = newdot - savedot; | |
6102 | ||
6103 | if (lang_sizing_iteration == 1) | |
6104 | diff = dotdelta; | |
6105 | else if (lang_sizing_iteration > 1) | |
6106 | { | |
6107 | /* Only report adjustments that would change | |
6108 | alignment from what we have already reported. */ | |
6109 | diff = newdot - os->bfd_section->vma; | |
6110 | if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) | |
6111 | diff = 0; | |
6112 | } | |
6113 | if (diff != 0 | |
6114 | && (config.warn_section_align | |
6115 | || os->addr_tree != NULL)) | |
6116 | einfo (_("%P: warning: " | |
6117 | "start of section %s changed by %ld\n"), | |
6118 | os->name, (long) diff); | |
6119 | } | |
6120 | ||
6121 | bfd_set_section_vma (os->bfd_section, newdot); | |
6122 | ||
6123 | os->bfd_section->output_offset = 0; | |
6124 | } | |
6125 | ||
6126 | lang_size_sections_1 (&os->children.head, os, | |
6127 | os->fill, newdot, relax, check_regions); | |
6128 | ||
6129 | os->processed_vma = true; | |
6130 | ||
6131 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
6132 | /* Except for some special linker created sections, | |
6133 | no output section should change from zero size | |
6134 | after strip_excluded_output_sections. A non-zero | |
6135 | size on an ignored section indicates that some | |
6136 | input section was not sized early enough. */ | |
6137 | ASSERT (os->bfd_section->size == 0); | |
6138 | else | |
6139 | { | |
6140 | dot = os->bfd_section->vma; | |
6141 | ||
6142 | /* Put the section within the requested block size, or | |
6143 | align at the block boundary. */ | |
6144 | after = ((dot | |
6145 | + TO_ADDR (os->bfd_section->size) | |
6146 | + os->block_value - 1) | |
6147 | & - (bfd_vma) os->block_value); | |
6148 | ||
6149 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) | |
6150 | os->bfd_section->size = TO_SIZE (after | |
6151 | - os->bfd_section->vma); | |
6152 | } | |
6153 | ||
6154 | /* Set section lma. */ | |
6155 | r = os->region; | |
6156 | if (r == NULL) | |
6157 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); | |
6158 | ||
6159 | if (os->load_base) | |
6160 | { | |
6161 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
6162 | os->bfd_section->lma = lma; | |
6163 | } | |
6164 | else if (os->lma_region != NULL) | |
6165 | { | |
6166 | bfd_vma lma = os->lma_region->current; | |
6167 | ||
6168 | if (os->align_lma_with_input) | |
6169 | lma += dotdelta; | |
6170 | else | |
6171 | { | |
6172 | /* When LMA_REGION is the same as REGION, align the LMA | |
6173 | as we did for the VMA, possibly including alignment | |
6174 | from the bfd section. If a different region, then | |
6175 | only align according to the value in the output | |
6176 | statement. */ | |
6177 | if (os->lma_region != os->region) | |
6178 | section_alignment = exp_get_power (os->section_alignment, | |
6179 | os, | |
6180 | "section alignment"); | |
6181 | if (section_alignment > 0) | |
6182 | lma = align_power (lma, section_alignment); | |
6183 | } | |
6184 | os->bfd_section->lma = lma; | |
6185 | } | |
6186 | else if (r->last_os != NULL | |
6187 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
6188 | { | |
6189 | bfd_vma lma; | |
6190 | asection *last; | |
6191 | ||
6192 | last = r->last_os->output_section_statement.bfd_section; | |
6193 | ||
6194 | /* A backwards move of dot should be accompanied by | |
6195 | an explicit assignment to the section LMA (ie. | |
6196 | os->load_base set) because backwards moves can | |
6197 | create overlapping LMAs. */ | |
6198 | if (dot < last->vma | |
6199 | && os->bfd_section->size != 0 | |
6200 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) | |
6201 | { | |
6202 | /* If dot moved backwards then leave lma equal to | |
6203 | vma. This is the old default lma, which might | |
6204 | just happen to work when the backwards move is | |
6205 | sufficiently large. Nag if this changes anything, | |
6206 | so people can fix their linker scripts. */ | |
6207 | ||
6208 | if (last->vma != last->lma) | |
6209 | einfo (_("%P: warning: dot moved backwards " | |
6210 | "before `%s'\n"), os->name); | |
6211 | } | |
6212 | else | |
6213 | { | |
6214 | /* If this is an overlay, set the current lma to that | |
6215 | at the end of the previous section. */ | |
6216 | if (os->sectype == overlay_section) | |
6217 | lma = last->lma + TO_ADDR (last->size); | |
6218 | ||
6219 | /* Otherwise, keep the same lma to vma relationship | |
6220 | as the previous section. */ | |
6221 | else | |
6222 | lma = os->bfd_section->vma + last->lma - last->vma; | |
6223 | ||
6224 | if (section_alignment > 0) | |
6225 | lma = align_power (lma, section_alignment); | |
6226 | os->bfd_section->lma = lma; | |
6227 | } | |
6228 | } | |
6229 | os->processed_lma = true; | |
6230 | ||
6231 | /* Keep track of normal sections using the default | |
6232 | lma region. We use this to set the lma for | |
6233 | following sections. Overlays or other linker | |
6234 | script assignment to lma might mean that the | |
6235 | default lma == vma is incorrect. | |
6236 | To avoid warnings about dot moving backwards when using | |
6237 | -Ttext, don't start tracking sections until we find one | |
6238 | of non-zero size or with lma set differently to vma. | |
6239 | Do this tracking before we short-cut the loop so that we | |
6240 | track changes for the case where the section size is zero, | |
6241 | but the lma is set differently to the vma. This is | |
6242 | important, if an orphan section is placed after an | |
6243 | otherwise empty output section that has an explicit lma | |
6244 | set, we want that lma reflected in the orphans lma. */ | |
6245 | if (((!IGNORE_SECTION (os->bfd_section) | |
6246 | && (os->bfd_section->size != 0 | |
6247 | || (r->last_os == NULL | |
6248 | && os->bfd_section->vma != os->bfd_section->lma) | |
6249 | || (r->last_os != NULL | |
6250 | && dot >= (r->last_os->output_section_statement | |
6251 | .bfd_section->vma)))) | |
6252 | || os->sectype == first_overlay_section) | |
6253 | && os->lma_region == NULL | |
6254 | && !bfd_link_relocatable (&link_info)) | |
6255 | r->last_os = s; | |
6256 | ||
6257 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
6258 | break; | |
6259 | ||
6260 | /* .tbss sections effectively have zero size. */ | |
6261 | if (!IS_TBSS (os->bfd_section) | |
6262 | || bfd_link_relocatable (&link_info)) | |
6263 | dotdelta = TO_ADDR (os->bfd_section->size); | |
6264 | else | |
6265 | dotdelta = 0; | |
6266 | dot += dotdelta; | |
6267 | ||
6268 | if (os->update_dot_tree != 0) | |
6269 | exp_fold_tree (os->update_dot_tree, os, bfd_abs_section_ptr, &dot); | |
6270 | ||
6271 | /* Update dot in the region ? | |
6272 | We only do this if the section is going to be allocated, | |
6273 | since unallocated sections do not contribute to the region's | |
6274 | overall size in memory. */ | |
6275 | if (os->region != NULL | |
6276 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) | |
6277 | { | |
6278 | os->region->current = dot; | |
6279 | ||
6280 | if (check_regions) | |
6281 | /* Make sure the new address is within the region. */ | |
6282 | os_region_check (os, os->region, os->addr_tree, | |
6283 | os->bfd_section->vma); | |
6284 | ||
6285 | if (os->lma_region != NULL && os->lma_region != os->region | |
6286 | && ((os->bfd_section->flags & SEC_LOAD) | |
6287 | || os->align_lma_with_input)) | |
6288 | { | |
6289 | os->lma_region->current = os->bfd_section->lma + dotdelta; | |
6290 | ||
6291 | if (check_regions) | |
6292 | os_region_check (os, os->lma_region, NULL, | |
6293 | os->bfd_section->lma); | |
6294 | } | |
6295 | } | |
6296 | } | |
6297 | break; | |
6298 | ||
6299 | case lang_constructors_statement_enum: | |
6300 | dot = lang_size_sections_1 (&constructor_list.head, current_os, | |
6301 | fill, dot, relax, check_regions); | |
6302 | break; | |
6303 | ||
6304 | case lang_data_statement_enum: | |
6305 | { | |
6306 | unsigned int size = 0; | |
6307 | ||
6308 | s->data_statement.output_offset = dot - current_os->bfd_section->vma; | |
6309 | s->data_statement.output_section = current_os->bfd_section; | |
6310 | ||
6311 | /* We might refer to provided symbols in the expression, and | |
6312 | need to mark them as needed. */ | |
6313 | exp_fold_tree (s->data_statement.exp, os, | |
6314 | bfd_abs_section_ptr, &dot); | |
6315 | ||
6316 | switch (s->data_statement.type) | |
6317 | { | |
6318 | default: | |
6319 | abort (); | |
6320 | case QUAD: | |
6321 | case SQUAD: | |
6322 | size = QUAD_SIZE; | |
6323 | break; | |
6324 | case LONG: | |
6325 | size = LONG_SIZE; | |
6326 | break; | |
6327 | case SHORT: | |
6328 | size = SHORT_SIZE; | |
6329 | break; | |
6330 | case BYTE: | |
6331 | size = BYTE_SIZE; | |
6332 | break; | |
6333 | } | |
6334 | if (size < TO_SIZE ((unsigned) 1)) | |
6335 | size = TO_SIZE ((unsigned) 1); | |
6336 | dot += TO_ADDR (size); | |
6337 | if (!(current_os->bfd_section->flags & SEC_FIXED_SIZE)) | |
6338 | current_os->bfd_section->size | |
6339 | = TO_SIZE (dot - current_os->bfd_section->vma); | |
6340 | ||
6341 | } | |
6342 | break; | |
6343 | ||
6344 | case lang_reloc_statement_enum: | |
6345 | { | |
6346 | int size; | |
6347 | ||
6348 | s->reloc_statement.output_offset | |
6349 | = dot - current_os->bfd_section->vma; | |
6350 | s->reloc_statement.output_section | |
6351 | = current_os->bfd_section; | |
6352 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
6353 | dot += TO_ADDR (size); | |
6354 | if (!(current_os->bfd_section->flags & SEC_FIXED_SIZE)) | |
6355 | current_os->bfd_section->size | |
6356 | = TO_SIZE (dot - current_os->bfd_section->vma); | |
6357 | } | |
6358 | break; | |
6359 | ||
6360 | case lang_wild_statement_enum: | |
6361 | dot = lang_size_sections_1 (&s->wild_statement.children.head, | |
6362 | current_os, fill, dot, relax, | |
6363 | check_regions); | |
6364 | break; | |
6365 | ||
6366 | case lang_object_symbols_statement_enum: | |
6367 | link_info.create_object_symbols_section = current_os->bfd_section; | |
6368 | current_os->bfd_section->flags |= SEC_KEEP; | |
6369 | break; | |
6370 | ||
6371 | case lang_output_statement_enum: | |
6372 | case lang_target_statement_enum: | |
6373 | break; | |
6374 | ||
6375 | case lang_input_section_enum: | |
6376 | { | |
6377 | asection *i; | |
6378 | ||
6379 | i = s->input_section.section; | |
6380 | if (relax) | |
6381 | { | |
6382 | bool again; | |
6383 | ||
6384 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) | |
6385 | fatal (_("%P: can't relax section: %E\n")); | |
6386 | if (again) | |
6387 | *relax = true; | |
6388 | } | |
6389 | dot = size_input_section (prev, current_os, fill, &removed, dot); | |
6390 | } | |
6391 | break; | |
6392 | ||
6393 | case lang_input_statement_enum: | |
6394 | break; | |
6395 | ||
6396 | case lang_fill_statement_enum: | |
6397 | s->fill_statement.output_section = current_os->bfd_section; | |
6398 | ||
6399 | fill = s->fill_statement.fill; | |
6400 | break; | |
6401 | ||
6402 | case lang_assignment_statement_enum: | |
6403 | { | |
6404 | bfd_vma newdot = dot; | |
6405 | etree_type *tree = s->assignment_statement.exp; | |
6406 | ||
6407 | expld.dataseg.relro = exp_seg_relro_none; | |
6408 | ||
6409 | exp_fold_tree (tree, os, current_os->bfd_section, &newdot); | |
6410 | ||
6411 | ldlang_check_relro_region (s); | |
6412 | ||
6413 | expld.dataseg.relro = exp_seg_relro_none; | |
6414 | ||
6415 | /* This symbol may be relative to this section. */ | |
6416 | if ((tree->type.node_class == etree_provided | |
6417 | || tree->type.node_class == etree_assign) | |
6418 | && (tree->assign.dst [0] != '.' | |
6419 | || tree->assign.dst [1] != '\0')) | |
6420 | current_os->update_dot = 1; | |
6421 | ||
6422 | if (!current_os->ignored) | |
6423 | { | |
6424 | if (current_os == abs_output_section) | |
6425 | { | |
6426 | /* If we don't have an output section, then just adjust | |
6427 | the default memory address. */ | |
6428 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, | |
6429 | false)->current = newdot; | |
6430 | } | |
6431 | else if (newdot != dot) | |
6432 | { | |
6433 | /* Insert a pad after this statement. We can't | |
6434 | put the pad before when relaxing, in case the | |
6435 | assignment references dot. */ | |
6436 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), | |
6437 | current_os->bfd_section, dot); | |
6438 | ||
6439 | /* Don't neuter the pad below when relaxing. */ | |
6440 | s = s->header.next; | |
6441 | ||
6442 | /* If dot is advanced, this implies that the section | |
6443 | should have space allocated to it, unless the | |
6444 | user has explicitly stated that the section | |
6445 | should not be allocated. */ | |
6446 | if (current_os->sectype != noalloc_section | |
6447 | && (current_os->sectype != noload_section | |
6448 | || (bfd_get_flavour (link_info.output_bfd) | |
6449 | == bfd_target_elf_flavour))) | |
6450 | current_os->bfd_section->flags |= SEC_ALLOC; | |
6451 | } | |
6452 | dot = newdot; | |
6453 | } | |
6454 | } | |
6455 | break; | |
6456 | ||
6457 | case lang_padding_statement_enum: | |
6458 | /* If this is the first time lang_size_sections is called, | |
6459 | we won't have any padding statements. If this is the | |
6460 | second or later passes when relaxing, we should allow | |
6461 | padding to shrink. If padding is needed on this pass, it | |
6462 | will be added back in. */ | |
6463 | s->padding_statement.size = 0; | |
6464 | ||
6465 | /* Make sure output_offset is valid. If relaxation shrinks | |
6466 | the section and this pad isn't needed, it's possible to | |
6467 | have output_offset larger than the final size of the | |
6468 | section. bfd_set_section_contents will complain even for | |
6469 | a pad size of zero. */ | |
6470 | s->padding_statement.output_offset | |
6471 | = dot - current_os->bfd_section->vma; | |
6472 | break; | |
6473 | ||
6474 | case lang_group_statement_enum: | |
6475 | dot = lang_size_sections_1 (&s->group_statement.children.head, | |
6476 | current_os, fill, dot, relax, | |
6477 | check_regions); | |
6478 | break; | |
6479 | ||
6480 | case lang_insert_statement_enum: | |
6481 | break; | |
6482 | ||
6483 | /* We can only get here when relaxing is turned on. */ | |
6484 | case lang_address_statement_enum: | |
6485 | break; | |
6486 | ||
6487 | default: | |
6488 | FAIL (); | |
6489 | break; | |
6490 | } | |
6491 | ||
6492 | /* If an input section doesn't fit in the current output | |
6493 | section, remove it from the list. Handle the case where we | |
6494 | have to remove an input_section statement here: there is a | |
6495 | special case to remove the first element of the list. */ | |
6496 | if (link_info.non_contiguous_regions && removed) | |
6497 | { | |
6498 | /* If we removed the first element during the previous | |
6499 | iteration, override the loop assignment of prev_s. */ | |
6500 | if (removed_prev_s) | |
6501 | prev_s = NULL; | |
6502 | ||
6503 | if (prev_s) | |
6504 | { | |
6505 | /* If there was a real previous input section, just skip | |
6506 | the current one. */ | |
6507 | prev_s->header.next=s->header.next; | |
6508 | s = prev_s; | |
6509 | removed_prev_s = false; | |
6510 | } | |
6511 | else | |
6512 | { | |
6513 | /* Remove the first input section of the list. */ | |
6514 | *prev = s->header.next; | |
6515 | removed_prev_s = true; | |
6516 | } | |
6517 | ||
6518 | /* Move to next element, unless we removed the head of the | |
6519 | list. */ | |
6520 | if (!removed_prev_s) | |
6521 | prev = &s->header.next; | |
6522 | } | |
6523 | else | |
6524 | { | |
6525 | prev = &s->header.next; | |
6526 | removed_prev_s = false; | |
6527 | } | |
6528 | } | |
6529 | return dot; | |
6530 | } | |
6531 | ||
6532 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. | |
6533 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
6534 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
6535 | segments. We are allowed an opportunity to override this decision. */ | |
6536 | ||
6537 | bool | |
6538 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
6539 | bfd *abfd ATTRIBUTE_UNUSED, | |
6540 | asection *current_section, | |
6541 | asection *previous_section, | |
6542 | bool new_segment) | |
6543 | { | |
6544 | lang_output_section_statement_type *cur; | |
6545 | lang_output_section_statement_type *prev; | |
6546 | ||
6547 | /* The checks below are only necessary when the BFD library has decided | |
6548 | that the two sections ought to be placed into the same segment. */ | |
6549 | if (new_segment) | |
6550 | return true; | |
6551 | ||
6552 | /* Paranoia checks. */ | |
6553 | if (current_section == NULL || previous_section == NULL) | |
6554 | return new_segment; | |
6555 | ||
6556 | /* If this flag is set, the target never wants code and non-code | |
6557 | sections comingled in the same segment. */ | |
6558 | if (config.separate_code | |
6559 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
6560 | return true; | |
6561 | ||
6562 | /* Find the memory regions associated with the two sections. | |
6563 | We call lang_output_section_find() here rather than scanning the list | |
6564 | of output sections looking for a matching section pointer because if | |
6565 | we have a large number of sections then a hash lookup is faster. */ | |
6566 | cur = lang_output_section_find (current_section->name); | |
6567 | prev = lang_output_section_find (previous_section->name); | |
6568 | ||
6569 | /* More paranoia. */ | |
6570 | if (cur == NULL || prev == NULL) | |
6571 | return new_segment; | |
6572 | ||
6573 | /* If the regions are different then force the sections to live in | |
6574 | different segments. See the email thread starting at the following | |
6575 | URL for the reasons why this is necessary: | |
6576 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ | |
6577 | return cur->region != prev->region; | |
6578 | } | |
6579 | ||
6580 | void | |
6581 | one_lang_size_sections_pass (bool *relax, bool check_regions) | |
6582 | { | |
6583 | lang_statement_iteration++; | |
6584 | if (expld.phase != lang_mark_phase_enum) | |
6585 | lang_sizing_iteration++; | |
6586 | lang_size_sections_1 (&statement_list.head, abs_output_section, | |
6587 | 0, 0, relax, check_regions); | |
6588 | } | |
6589 | ||
6590 | static bool | |
6591 | lang_size_segment (void) | |
6592 | { | |
6593 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether | |
6594 | a page could be saved in the data segment. */ | |
6595 | seg_align_type *seg = &expld.dataseg; | |
6596 | bfd_vma first, last; | |
6597 | ||
6598 | first = -seg->base & (seg->commonpagesize - 1); | |
6599 | last = seg->end & (seg->commonpagesize - 1); | |
6600 | if (first && last | |
6601 | && ((seg->base & ~(seg->commonpagesize - 1)) | |
6602 | != (seg->end & ~(seg->commonpagesize - 1))) | |
6603 | && first + last <= seg->commonpagesize) | |
6604 | { | |
6605 | seg->phase = exp_seg_adjust; | |
6606 | return true; | |
6607 | } | |
6608 | ||
6609 | seg->phase = exp_seg_done; | |
6610 | return false; | |
6611 | } | |
6612 | ||
6613 | static bfd_vma | |
6614 | lang_size_relro_segment_1 (void) | |
6615 | { | |
6616 | seg_align_type *seg = &expld.dataseg; | |
6617 | bfd_vma relro_end, desired_end; | |
6618 | asection *sec; | |
6619 | ||
6620 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ | |
6621 | relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; | |
6622 | ||
6623 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ | |
6624 | desired_end = relro_end - seg->relro_offset; | |
6625 | ||
6626 | /* For sections in the relro segment.. */ | |
6627 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) | |
6628 | if ((sec->flags & SEC_ALLOC) != 0 | |
6629 | && sec->vma >= seg->base | |
6630 | && sec->vma < seg->relro_end - seg->relro_offset) | |
6631 | { | |
6632 | /* Where do we want to put this section so that it ends as | |
6633 | desired? */ | |
6634 | bfd_vma start, end, bump; | |
6635 | ||
6636 | end = start = sec->vma; | |
6637 | if (!IS_TBSS (sec)) | |
6638 | end += TO_ADDR (sec->size); | |
6639 | bump = desired_end - end; | |
6640 | /* We'd like to increase START by BUMP, but we must heed | |
6641 | alignment so the increase might be less than optimum. */ | |
6642 | start += bump; | |
6643 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6644 | /* This is now the desired end for the previous section. */ | |
6645 | desired_end = start; | |
6646 | } | |
6647 | ||
6648 | seg->phase = exp_seg_relro_adjust; | |
6649 | ASSERT (desired_end >= seg->base); | |
6650 | seg->base = desired_end; | |
6651 | return relro_end; | |
6652 | } | |
6653 | ||
6654 | static bool | |
6655 | lang_size_relro_segment (bool *relax, bool check_regions) | |
6656 | { | |
6657 | bool do_reset = false; | |
6658 | ||
6659 | if (link_info.relro && expld.dataseg.relro_end) | |
6660 | { | |
6661 | bfd_vma data_initial_base = expld.dataseg.base; | |
6662 | bfd_vma data_relro_end = lang_size_relro_segment_1 (); | |
6663 | ||
6664 | lang_reset_memory_regions (); | |
6665 | one_lang_size_sections_pass (relax, check_regions); | |
6666 | ||
6667 | /* Assignments to dot, or to output section address in a user | |
6668 | script have increased padding over the original. Revert. */ | |
6669 | if (expld.dataseg.relro_end > data_relro_end) | |
6670 | { | |
6671 | expld.dataseg.base = data_initial_base; | |
6672 | do_reset = true; | |
6673 | } | |
6674 | } | |
6675 | else if (lang_size_segment ()) | |
6676 | do_reset = true; | |
6677 | ||
6678 | return do_reset; | |
6679 | } | |
6680 | ||
6681 | void | |
6682 | lang_size_sections (bool *relax, bool check_regions) | |
6683 | { | |
6684 | expld.phase = lang_allocating_phase_enum; | |
6685 | expld.dataseg.phase = exp_seg_none; | |
6686 | ||
6687 | one_lang_size_sections_pass (relax, check_regions); | |
6688 | ||
6689 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6690 | expld.dataseg.phase = exp_seg_done; | |
6691 | ||
6692 | if (expld.dataseg.phase == exp_seg_end_seen) | |
6693 | { | |
6694 | bool do_reset | |
6695 | = lang_size_relro_segment (relax, check_regions); | |
6696 | ||
6697 | if (do_reset) | |
6698 | { | |
6699 | lang_reset_memory_regions (); | |
6700 | one_lang_size_sections_pass (relax, check_regions); | |
6701 | } | |
6702 | ||
6703 | if (link_info.relro && expld.dataseg.relro_end) | |
6704 | { | |
6705 | link_info.relro_start = expld.dataseg.base; | |
6706 | link_info.relro_end = expld.dataseg.relro_end; | |
6707 | } | |
6708 | } | |
6709 | } | |
6710 | ||
6711 | static lang_output_section_statement_type *current_section; | |
6712 | static lang_assignment_statement_type *current_assign; | |
6713 | static bool prefer_next_section; | |
6714 | ||
6715 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ | |
6716 | ||
6717 | static bfd_vma | |
6718 | lang_do_assignments_1 (lang_statement_union_type *s, | |
6719 | lang_output_section_statement_type *current_os, | |
6720 | fill_type *fill, | |
6721 | bfd_vma dot, | |
6722 | bool *found_end) | |
6723 | { | |
6724 | lang_output_section_statement_type *os = current_os; | |
6725 | ||
6726 | for (; s != NULL; s = s->header.next) | |
6727 | { | |
6728 | switch (s->header.type) | |
6729 | { | |
6730 | case lang_constructors_statement_enum: | |
6731 | dot = lang_do_assignments_1 (constructor_list.head, | |
6732 | current_os, fill, dot, found_end); | |
6733 | break; | |
6734 | ||
6735 | case lang_output_section_statement_enum: | |
6736 | { | |
6737 | bfd_vma newdot; | |
6738 | ||
6739 | os = &s->output_section_statement; | |
6740 | os->after_end = *found_end; | |
6741 | init_opb (os->bfd_section); | |
6742 | newdot = dot; | |
6743 | if (os->bfd_section != NULL) | |
6744 | { | |
6745 | if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
6746 | { | |
6747 | current_section = os; | |
6748 | prefer_next_section = false; | |
6749 | } | |
6750 | newdot = os->bfd_section->vma; | |
6751 | } | |
6752 | newdot = lang_do_assignments_1 (os->children.head, | |
6753 | os, os->fill, newdot, found_end); | |
6754 | if (!os->ignored) | |
6755 | { | |
6756 | if (os->bfd_section != NULL) | |
6757 | { | |
6758 | newdot = os->bfd_section->vma; | |
6759 | ||
6760 | /* .tbss sections effectively have zero size. */ | |
6761 | if (!IS_TBSS (os->bfd_section) | |
6762 | || bfd_link_relocatable (&link_info)) | |
6763 | newdot += TO_ADDR (os->bfd_section->size); | |
6764 | ||
6765 | if (os->update_dot_tree != NULL) | |
6766 | exp_fold_tree (os->update_dot_tree, os, | |
6767 | bfd_abs_section_ptr, &newdot); | |
6768 | } | |
6769 | dot = newdot; | |
6770 | } | |
6771 | } | |
6772 | break; | |
6773 | ||
6774 | case lang_wild_statement_enum: | |
6775 | ||
6776 | dot = lang_do_assignments_1 (s->wild_statement.children.head, | |
6777 | current_os, fill, dot, found_end); | |
6778 | break; | |
6779 | ||
6780 | case lang_object_symbols_statement_enum: | |
6781 | case lang_output_statement_enum: | |
6782 | case lang_target_statement_enum: | |
6783 | break; | |
6784 | ||
6785 | case lang_data_statement_enum: | |
6786 | exp_fold_tree (s->data_statement.exp, os, bfd_abs_section_ptr, &dot); | |
6787 | if (expld.result.valid_p) | |
6788 | { | |
6789 | s->data_statement.value = expld.result.value; | |
6790 | if (expld.result.section != NULL) | |
6791 | s->data_statement.value += expld.result.section->vma; | |
6792 | } | |
6793 | else if (expld.phase == lang_final_phase_enum) | |
6794 | fatal (_("%P: invalid data statement\n")); | |
6795 | { | |
6796 | unsigned int size; | |
6797 | switch (s->data_statement.type) | |
6798 | { | |
6799 | default: | |
6800 | abort (); | |
6801 | case QUAD: | |
6802 | case SQUAD: | |
6803 | size = QUAD_SIZE; | |
6804 | break; | |
6805 | case LONG: | |
6806 | size = LONG_SIZE; | |
6807 | break; | |
6808 | case SHORT: | |
6809 | size = SHORT_SIZE; | |
6810 | break; | |
6811 | case BYTE: | |
6812 | size = BYTE_SIZE; | |
6813 | break; | |
6814 | } | |
6815 | if (size < TO_SIZE ((unsigned) 1)) | |
6816 | size = TO_SIZE ((unsigned) 1); | |
6817 | dot += TO_ADDR (size); | |
6818 | } | |
6819 | break; | |
6820 | ||
6821 | case lang_reloc_statement_enum: | |
6822 | exp_fold_tree (s->reloc_statement.addend_exp, os, | |
6823 | bfd_abs_section_ptr, &dot); | |
6824 | if (expld.result.valid_p) | |
6825 | s->reloc_statement.addend_value = expld.result.value; | |
6826 | else if (expld.phase == lang_final_phase_enum) | |
6827 | fatal (_("%P: invalid reloc statement\n")); | |
6828 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); | |
6829 | break; | |
6830 | ||
6831 | case lang_input_section_enum: | |
6832 | { | |
6833 | asection *in = s->input_section.section; | |
6834 | ||
6835 | if ((in->flags & SEC_EXCLUDE) == 0) | |
6836 | dot += TO_ADDR (in->size); | |
6837 | } | |
6838 | break; | |
6839 | ||
6840 | case lang_input_statement_enum: | |
6841 | break; | |
6842 | ||
6843 | case lang_fill_statement_enum: | |
6844 | fill = s->fill_statement.fill; | |
6845 | break; | |
6846 | ||
6847 | case lang_assignment_statement_enum: | |
6848 | current_assign = &s->assignment_statement; | |
6849 | if (current_assign->exp->type.node_class != etree_assert) | |
6850 | { | |
6851 | const char *p = current_assign->exp->assign.dst; | |
6852 | ||
6853 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
6854 | prefer_next_section = true; | |
6855 | ||
6856 | while (*p == '_') | |
6857 | ++p; | |
6858 | if (strcmp (p, "end") == 0) | |
6859 | *found_end = true; | |
6860 | } | |
6861 | exp_fold_tree (s->assignment_statement.exp, os, | |
6862 | (current_os->bfd_section != NULL | |
6863 | ? current_os->bfd_section : bfd_und_section_ptr), | |
6864 | &dot); | |
6865 | break; | |
6866 | ||
6867 | case lang_padding_statement_enum: | |
6868 | dot += TO_ADDR (s->padding_statement.size); | |
6869 | break; | |
6870 | ||
6871 | case lang_group_statement_enum: | |
6872 | dot = lang_do_assignments_1 (s->group_statement.children.head, | |
6873 | current_os, fill, dot, found_end); | |
6874 | break; | |
6875 | ||
6876 | case lang_insert_statement_enum: | |
6877 | break; | |
6878 | ||
6879 | case lang_address_statement_enum: | |
6880 | break; | |
6881 | ||
6882 | default: | |
6883 | FAIL (); | |
6884 | break; | |
6885 | } | |
6886 | } | |
6887 | return dot; | |
6888 | } | |
6889 | ||
6890 | void | |
6891 | lang_do_assignments (lang_phase_type phase) | |
6892 | { | |
6893 | bool found_end = false; | |
6894 | ||
6895 | current_section = NULL; | |
6896 | prefer_next_section = false; | |
6897 | expld.phase = phase; | |
6898 | lang_statement_iteration++; | |
6899 | lang_do_assignments_1 (statement_list.head, | |
6900 | abs_output_section, NULL, 0, &found_end); | |
6901 | } | |
6902 | ||
6903 | /* For an assignment statement outside of an output section statement, | |
6904 | choose the best of neighbouring output sections to use for values | |
6905 | of "dot". */ | |
6906 | ||
6907 | asection * | |
6908 | section_for_dot (void) | |
6909 | { | |
6910 | asection *s; | |
6911 | ||
6912 | /* Assignments belong to the previous output section, unless there | |
6913 | has been an assignment to "dot", in which case following | |
6914 | assignments belong to the next output section. (The assumption | |
6915 | is that an assignment to "dot" is setting up the address for the | |
6916 | next output section.) Except that past the assignment to "_end" | |
6917 | we always associate with the previous section. This exception is | |
6918 | for targets like SH that define an alloc .stack or other | |
6919 | weirdness after non-alloc sections. */ | |
6920 | if (current_section == NULL || prefer_next_section) | |
6921 | { | |
6922 | lang_statement_union_type *stmt; | |
6923 | lang_output_section_statement_type *os; | |
6924 | ||
6925 | for (stmt = (lang_statement_union_type *) current_assign; | |
6926 | stmt != NULL; | |
6927 | stmt = stmt->header.next) | |
6928 | if (stmt->header.type == lang_output_section_statement_enum) | |
6929 | break; | |
6930 | ||
6931 | os = stmt ? &stmt->output_section_statement : NULL; | |
6932 | while (os != NULL | |
6933 | && !os->after_end | |
6934 | && (os->bfd_section == NULL | |
6935 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6936 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6937 | os->bfd_section))) | |
6938 | os = os->next; | |
6939 | ||
6940 | if (current_section == NULL || os == NULL || !os->after_end) | |
6941 | { | |
6942 | if (os != NULL) | |
6943 | s = os->bfd_section; | |
6944 | else | |
6945 | s = link_info.output_bfd->section_last; | |
6946 | while (s != NULL | |
6947 | && ((s->flags & SEC_ALLOC) == 0 | |
6948 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6949 | s = s->prev; | |
6950 | if (s != NULL) | |
6951 | return s; | |
6952 | ||
6953 | return bfd_abs_section_ptr; | |
6954 | } | |
6955 | } | |
6956 | ||
6957 | s = current_section->bfd_section; | |
6958 | ||
6959 | /* The section may have been stripped. */ | |
6960 | while (s != NULL | |
6961 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6962 | || (s->flags & SEC_ALLOC) == 0 | |
6963 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6964 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6965 | s = s->prev; | |
6966 | if (s == NULL) | |
6967 | s = link_info.output_bfd->sections; | |
6968 | while (s != NULL | |
6969 | && ((s->flags & SEC_ALLOC) == 0 | |
6970 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6971 | s = s->next; | |
6972 | if (s != NULL) | |
6973 | return s; | |
6974 | ||
6975 | return bfd_abs_section_ptr; | |
6976 | } | |
6977 | ||
6978 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ | |
6979 | ||
6980 | static struct bfd_link_hash_entry **start_stop_syms; | |
6981 | static size_t start_stop_count = 0; | |
6982 | static size_t start_stop_alloc = 0; | |
6983 | ||
6984 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6985 | to start_stop_syms. */ | |
6986 | ||
6987 | static void | |
6988 | lang_define_start_stop (const char *symbol, asection *sec) | |
6989 | { | |
6990 | struct bfd_link_hash_entry *h; | |
6991 | ||
6992 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6993 | if (h != NULL) | |
6994 | { | |
6995 | if (start_stop_count == start_stop_alloc) | |
6996 | { | |
6997 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6998 | start_stop_syms | |
6999 | = xrealloc (start_stop_syms, | |
7000 | start_stop_alloc * sizeof (*start_stop_syms)); | |
7001 | } | |
7002 | start_stop_syms[start_stop_count++] = h; | |
7003 | } | |
7004 | } | |
7005 | ||
7006 | /* Check for input sections whose names match references to | |
7007 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
7008 | preliminary definitions. */ | |
7009 | ||
7010 | static void | |
7011 | lang_init_start_stop (void) | |
7012 | { | |
7013 | bfd *abfd; | |
7014 | asection *s; | |
7015 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); | |
7016 | ||
7017 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
7018 | for (s = abfd->sections; s != NULL; s = s->next) | |
7019 | { | |
7020 | const char *ps; | |
7021 | const char *secname = s->name; | |
7022 | ||
7023 | for (ps = secname; *ps != '\0'; ps++) | |
7024 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
7025 | break; | |
7026 | if (*ps == '\0') | |
7027 | { | |
7028 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
7029 | ||
7030 | symbol[0] = leading_char; | |
7031 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
7032 | lang_define_start_stop (symbol, s); | |
7033 | ||
7034 | symbol[1] = leading_char; | |
7035 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
7036 | lang_define_start_stop (symbol + 1, s); | |
7037 | ||
7038 | free (symbol); | |
7039 | } | |
7040 | } | |
7041 | } | |
7042 | ||
7043 | /* Iterate over start_stop_syms. */ | |
7044 | ||
7045 | static void | |
7046 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
7047 | { | |
7048 | size_t i; | |
7049 | ||
7050 | for (i = 0; i < start_stop_count; ++i) | |
7051 | func (start_stop_syms[i]); | |
7052 | } | |
7053 | ||
7054 | /* __start and __stop symbols are only supposed to be defined by the | |
7055 | linker for orphan sections, but we now extend that to sections that | |
7056 | map to an output section of the same name. The symbols were | |
7057 | defined early for --gc-sections, before we mapped input to output | |
7058 | sections, so undo those that don't satisfy this rule. */ | |
7059 | ||
7060 | static void | |
7061 | undef_start_stop (struct bfd_link_hash_entry *h) | |
7062 | { | |
7063 | if (h->ldscript_def) | |
7064 | return; | |
7065 | ||
7066 | if (h->u.def.section->output_section == NULL | |
7067 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
7068 | || strcmp (h->u.def.section->name, | |
7069 | h->u.def.section->output_section->name) != 0) | |
7070 | { | |
7071 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, | |
7072 | h->u.def.section->name); | |
7073 | if (sec != NULL) | |
7074 | { | |
7075 | /* When there are more than one input sections with the same | |
7076 | section name, SECNAME, linker picks the first one to define | |
7077 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
7078 | input section is removed by comdat group, we need to check | |
7079 | if there is still an output section with section name | |
7080 | SECNAME. */ | |
7081 | asection *i; | |
7082 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
7083 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
7084 | { | |
7085 | h->u.def.section = i; | |
7086 | return; | |
7087 | } | |
7088 | } | |
7089 | h->type = bfd_link_hash_undefined; | |
7090 | h->u.undef.abfd = NULL; | |
7091 | if (is_elf_hash_table (link_info.hash)) | |
7092 | { | |
7093 | const struct elf_backend_data *bed; | |
7094 | struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h; | |
7095 | unsigned int was_forced = eh->forced_local; | |
7096 | ||
7097 | bed = get_elf_backend_data (link_info.output_bfd); | |
7098 | (*bed->elf_backend_hide_symbol) (&link_info, eh, true); | |
7099 | if (!eh->ref_regular_nonweak) | |
7100 | h->type = bfd_link_hash_undefweak; | |
7101 | eh->def_regular = 0; | |
7102 | eh->forced_local = was_forced; | |
7103 | } | |
7104 | } | |
7105 | } | |
7106 | ||
7107 | static void | |
7108 | lang_undef_start_stop (void) | |
7109 | { | |
7110 | foreach_start_stop (undef_start_stop); | |
7111 | } | |
7112 | ||
7113 | /* Check for output sections whose names match references to | |
7114 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
7115 | preliminary definitions. */ | |
7116 | ||
7117 | static void | |
7118 | lang_init_startof_sizeof (void) | |
7119 | { | |
7120 | asection *s; | |
7121 | ||
7122 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
7123 | { | |
7124 | const char *secname = s->name; | |
7125 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
7126 | ||
7127 | sprintf (symbol, ".startof.%s", secname); | |
7128 | lang_define_start_stop (symbol, s); | |
7129 | ||
7130 | memcpy (symbol + 1, ".size", 5); | |
7131 | lang_define_start_stop (symbol + 1, s); | |
7132 | free (symbol); | |
7133 | } | |
7134 | } | |
7135 | ||
7136 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ | |
7137 | ||
7138 | static void | |
7139 | set_start_stop (struct bfd_link_hash_entry *h) | |
7140 | { | |
7141 | if (h->ldscript_def | |
7142 | || h->type != bfd_link_hash_defined) | |
7143 | return; | |
7144 | ||
7145 | if (h->root.string[0] == '.') | |
7146 | { | |
7147 | /* .startof. or .sizeof. symbol. | |
7148 | .startof. already has final value. */ | |
7149 | if (h->root.string[2] == 'i') | |
7150 | { | |
7151 | /* .sizeof. */ | |
7152 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
7153 | h->u.def.section = bfd_abs_section_ptr; | |
7154 | } | |
7155 | } | |
7156 | else | |
7157 | { | |
7158 | /* __start or __stop symbol. */ | |
7159 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
7160 | ||
7161 | h->u.def.section = h->u.def.section->output_section; | |
7162 | if (h->root.string[4 + has_lead] == 'o') | |
7163 | { | |
7164 | /* __stop_ */ | |
7165 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
7166 | } | |
7167 | } | |
7168 | } | |
7169 | ||
7170 | static void | |
7171 | lang_finalize_start_stop (void) | |
7172 | { | |
7173 | foreach_start_stop (set_start_stop); | |
7174 | } | |
7175 | ||
7176 | static void | |
7177 | lang_symbol_tweaks (void) | |
7178 | { | |
7179 | /* Give initial values for __start and __stop symbols, so that ELF | |
7180 | gc_sections will keep sections referenced by these symbols. Must | |
7181 | be done before lang_do_assignments. */ | |
7182 | if (config.build_constructors) | |
7183 | lang_init_start_stop (); | |
7184 | ||
7185 | /* Make __ehdr_start hidden, and set def_regular even though it is | |
7186 | likely undefined at this stage. For lang_check_relocs. */ | |
7187 | if (is_elf_hash_table (link_info.hash) | |
7188 | && !bfd_link_relocatable (&link_info)) | |
7189 | { | |
7190 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
7191 | bfd_link_hash_lookup (link_info.hash, "__ehdr_start", | |
7192 | false, false, true); | |
7193 | ||
7194 | /* Only adjust the export class if the symbol was referenced | |
7195 | and not defined, otherwise leave it alone. */ | |
7196 | if (h != NULL | |
7197 | && (h->root.type == bfd_link_hash_new | |
7198 | || h->root.type == bfd_link_hash_undefined | |
7199 | || h->root.type == bfd_link_hash_undefweak | |
7200 | || h->root.type == bfd_link_hash_common)) | |
7201 | { | |
7202 | const struct elf_backend_data *bed; | |
7203 | bed = get_elf_backend_data (link_info.output_bfd); | |
7204 | (*bed->elf_backend_hide_symbol) (&link_info, h, true); | |
7205 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) | |
7206 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
7207 | h->def_regular = 1; | |
7208 | h->root.linker_def = 1; | |
7209 | h->root.rel_from_abs = 1; | |
7210 | elf_hash_table (&link_info)->hehdr_start = h; | |
7211 | } | |
7212 | } | |
7213 | } | |
7214 | ||
7215 | static void | |
7216 | lang_end (void) | |
7217 | { | |
7218 | struct bfd_link_hash_entry *h; | |
7219 | bool warn; | |
7220 | ||
7221 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) | |
7222 | || bfd_link_dll (&link_info)) | |
7223 | warn = entry_from_cmdline; | |
7224 | else | |
7225 | warn = true; | |
7226 | ||
7227 | /* Force the user to specify a root when generating a relocatable with | |
7228 | --gc-sections, unless --gc-keep-exported was also given. */ | |
7229 | if (bfd_link_relocatable (&link_info) | |
7230 | && link_info.gc_sections | |
7231 | && !link_info.gc_keep_exported) | |
7232 | { | |
7233 | struct bfd_sym_chain *sym; | |
7234 | ||
7235 | for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next) | |
7236 | { | |
7237 | h = bfd_link_hash_lookup (link_info.hash, sym->name, | |
7238 | false, false, false); | |
7239 | if (is_defined (h) | |
7240 | && !bfd_is_const_section (h->u.def.section)) | |
7241 | break; | |
7242 | } | |
7243 | if (!sym) | |
7244 | fatal (_("%P: --gc-sections requires a defined symbol root " | |
7245 | "specified by -e or -u\n")); | |
7246 | } | |
7247 | ||
7248 | if (entry_symbol.name == NULL) | |
7249 | { | |
7250 | /* No entry has been specified. Look for the default entry, but | |
7251 | don't warn if we don't find it. */ | |
7252 | entry_symbol.name = entry_symbol_default; | |
7253 | warn = false; | |
7254 | } | |
7255 | ||
7256 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, | |
7257 | false, false, true); | |
7258 | ||
7259 | if (! is_defined (h) || h->u.def.section->output_section == NULL) | |
7260 | h = ldemul_find_alt_start_symbol (&entry_symbol); | |
7261 | ||
7262 | if (is_defined (h) | |
7263 | && h->u.def.section->output_section != NULL) | |
7264 | { | |
7265 | bfd_vma val; | |
7266 | ||
7267 | val = (h->u.def.value | |
7268 | + bfd_section_vma (h->u.def.section->output_section) | |
7269 | + h->u.def.section->output_offset); | |
7270 | if (!bfd_set_start_address (link_info.output_bfd, val)) | |
7271 | fatal (_("%P: %s: can't set start address\n"), entry_symbol.name); | |
7272 | } | |
7273 | else | |
7274 | { | |
7275 | bfd_vma val; | |
7276 | const char *send; | |
7277 | ||
7278 | /* We couldn't find the entry symbol. Try parsing it as a | |
7279 | number. */ | |
7280 | val = bfd_scan_vma (entry_symbol.name, &send, 0); | |
7281 | if (*send == '\0') | |
7282 | { | |
7283 | if (!bfd_set_start_address (link_info.output_bfd, val)) | |
7284 | fatal (_("%P: can't set start address\n")); | |
7285 | } | |
7286 | /* BZ 2004952: Only use the start of the entry section for executables. */ | |
7287 | else if bfd_link_executable (&link_info) | |
7288 | { | |
7289 | asection *ts; | |
7290 | ||
7291 | /* Can't find the entry symbol, and it's not a number. Use | |
7292 | the first address in the text section. */ | |
7293 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); | |
7294 | if (ts != NULL) | |
7295 | { | |
7296 | if (warn) | |
7297 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
7298 | " defaulting to %V\n"), | |
7299 | entry_symbol.name, | |
7300 | bfd_section_vma (ts)); | |
7301 | if (!bfd_set_start_address (link_info.output_bfd, | |
7302 | bfd_section_vma (ts))) | |
7303 | fatal (_("%P: can't set start address\n")); | |
7304 | } | |
7305 | else | |
7306 | { | |
7307 | if (warn) | |
7308 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
7309 | " not setting start address\n"), | |
7310 | entry_symbol.name); | |
7311 | } | |
7312 | } | |
7313 | else | |
7314 | { | |
7315 | if (warn) | |
7316 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
7317 | " not setting start address\n"), | |
7318 | entry_symbol.name); | |
7319 | } | |
7320 | } | |
7321 | } | |
7322 | ||
7323 | /* This is a small function used when we want to ignore errors from | |
7324 | BFD. */ | |
7325 | ||
7326 | static void | |
7327 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, | |
7328 | va_list ap ATTRIBUTE_UNUSED) | |
7329 | { | |
7330 | /* Don't do anything. */ | |
7331 | } | |
7332 | ||
7333 | /* Check that the architecture of all the input files is compatible | |
7334 | with the output file. Also call the backend to let it do any | |
7335 | other checking that is needed. */ | |
7336 | ||
7337 | static void | |
7338 | lang_check (void) | |
7339 | { | |
7340 | lang_input_statement_type *file; | |
7341 | bfd *input_bfd; | |
7342 | const bfd_arch_info_type *compatible; | |
7343 | ||
7344 | for (file = (void *) file_chain.head; | |
7345 | file != NULL; | |
7346 | file = file->next) | |
7347 | { | |
7348 | #if BFD_SUPPORTS_PLUGINS | |
7349 | /* Don't check format of files claimed by plugin. */ | |
7350 | if (file->flags.claimed) | |
7351 | continue; | |
7352 | #endif /* BFD_SUPPORTS_PLUGINS */ | |
7353 | input_bfd = file->the_bfd; | |
7354 | compatible | |
7355 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, | |
7356 | command_line.accept_unknown_input_arch); | |
7357 | ||
7358 | /* In general it is not possible to perform a relocatable | |
7359 | link between differing object formats when the input | |
7360 | file has relocations, because the relocations in the | |
7361 | input format may not have equivalent representations in | |
7362 | the output format (and besides BFD does not translate | |
7363 | relocs for other link purposes than a final link). */ | |
7364 | if (!file->flags.just_syms | |
7365 | && (bfd_link_relocatable (&link_info) | |
7366 | || link_info.emitrelocations) | |
7367 | && (compatible == NULL | |
7368 | || (bfd_get_flavour (input_bfd) | |
7369 | != bfd_get_flavour (link_info.output_bfd))) | |
7370 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) | |
7371 | { | |
7372 | fatal (_("%P: relocatable linking with relocations from" | |
7373 | " format %s (%pB) to format %s (%pB) is not supported\n"), | |
7374 | bfd_get_target (input_bfd), input_bfd, | |
7375 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); | |
7376 | } | |
7377 | ||
7378 | if (compatible == NULL) | |
7379 | { | |
7380 | if (command_line.warn_mismatch) | |
7381 | einfo (_("%X%P: %s architecture of input file `%pB'" | |
7382 | " is incompatible with %s output\n"), | |
7383 | bfd_printable_name (input_bfd), input_bfd, | |
7384 | bfd_printable_name (link_info.output_bfd)); | |
7385 | } | |
7386 | ||
7387 | /* If the input bfd has no contents, it shouldn't set the | |
7388 | private data of the output bfd. */ | |
7389 | else if (!file->flags.just_syms | |
7390 | && ((input_bfd->flags & DYNAMIC) != 0 | |
7391 | || bfd_count_sections (input_bfd) != 0)) | |
7392 | { | |
7393 | bfd_error_handler_type pfn = NULL; | |
7394 | ||
7395 | /* If we aren't supposed to warn about mismatched input | |
7396 | files, temporarily set the BFD error handler to a | |
7397 | function which will do nothing. We still want to call | |
7398 | bfd_merge_private_bfd_data, since it may set up | |
7399 | information which is needed in the output file. */ | |
7400 | if (!command_line.warn_mismatch) | |
7401 | pfn = bfd_set_error_handler (ignore_bfd_errors); | |
7402 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) | |
7403 | { | |
7404 | if (command_line.warn_mismatch) | |
7405 | einfo (_("%X%P: failed to merge target specific data" | |
7406 | " of file %pB\n"), input_bfd); | |
7407 | } | |
7408 | if (!command_line.warn_mismatch) | |
7409 | bfd_set_error_handler (pfn); | |
7410 | } | |
7411 | } | |
7412 | } | |
7413 | ||
7414 | /* Look through all the global common symbols and attach them to the | |
7415 | correct section. The -sort-common command line switch may be used | |
7416 | to roughly sort the entries by alignment. */ | |
7417 | ||
7418 | static void | |
7419 | lang_common (void) | |
7420 | { | |
7421 | if (link_info.inhibit_common_definition) | |
7422 | return; | |
7423 | if (bfd_link_relocatable (&link_info) | |
7424 | && !command_line.force_common_definition) | |
7425 | return; | |
7426 | ||
7427 | if (!config.sort_common) | |
7428 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); | |
7429 | else | |
7430 | { | |
7431 | unsigned int power; | |
7432 | ||
7433 | if (config.sort_common == sort_descending) | |
7434 | { | |
7435 | for (power = 4; power > 0; power--) | |
7436 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7437 | ||
7438 | power = 0; | |
7439 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7440 | } | |
7441 | else | |
7442 | { | |
7443 | for (power = 0; power <= 4; power++) | |
7444 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7445 | ||
7446 | power = (unsigned int) -1; | |
7447 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7448 | } | |
7449 | } | |
7450 | } | |
7451 | ||
7452 | /* Place one common symbol in the correct section. */ | |
7453 | ||
7454 | static bool | |
7455 | lang_one_common (struct bfd_link_hash_entry *h, void *info) | |
7456 | { | |
7457 | unsigned int power_of_two; | |
7458 | bfd_vma size; | |
7459 | asection *section; | |
7460 | ||
7461 | if (h->type != bfd_link_hash_common) | |
7462 | return true; | |
7463 | ||
7464 | size = h->u.c.size; | |
7465 | power_of_two = h->u.c.p->alignment_power; | |
7466 | ||
7467 | if (config.sort_common == sort_descending | |
7468 | && power_of_two < *(unsigned int *) info) | |
7469 | return true; | |
7470 | else if (config.sort_common == sort_ascending | |
7471 | && power_of_two > *(unsigned int *) info) | |
7472 | return true; | |
7473 | ||
7474 | section = h->u.c.p->section; | |
7475 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) | |
7476 | fatal (_("%P: could not define common symbol `%pT': %E\n"), | |
7477 | h->root.string); | |
7478 | ||
7479 | if (config.map_file != NULL) | |
7480 | { | |
7481 | static bool header_printed; | |
7482 | int len; | |
7483 | char *name; | |
7484 | char buf[32]; | |
7485 | ||
7486 | if (!header_printed) | |
7487 | { | |
7488 | minfo (_("\nAllocating common symbols\n")); | |
7489 | minfo (_("Common symbol size file\n\n")); | |
7490 | header_printed = true; | |
7491 | } | |
7492 | ||
7493 | name = bfd_demangle (link_info.output_bfd, h->root.string, | |
7494 | DMGL_ANSI | DMGL_PARAMS); | |
7495 | if (name == NULL) | |
7496 | { | |
7497 | minfo ("%s", h->root.string); | |
7498 | len = strlen (h->root.string); | |
7499 | } | |
7500 | else | |
7501 | { | |
7502 | minfo ("%s", name); | |
7503 | len = strlen (name); | |
7504 | free (name); | |
7505 | } | |
7506 | ||
7507 | if (len >= 19) | |
7508 | { | |
7509 | print_nl (); | |
7510 | len = 0; | |
7511 | } | |
7512 | ||
7513 | sprintf (buf, "%" PRIx64, (uint64_t) size); | |
7514 | fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); | |
7515 | ||
7516 | minfo ("%pB\n", section->owner); | |
7517 | } | |
7518 | ||
7519 | return true; | |
7520 | } | |
7521 | ||
7522 | /* Handle a single orphan section S, placing the orphan into an appropriate | |
7523 | output section. The effects of the --orphan-handling command line | |
7524 | option are handled here. */ | |
7525 | ||
7526 | static void | |
7527 | ldlang_place_orphan (asection *s) | |
7528 | { | |
7529 | if (config.orphan_handling == orphan_handling_discard) | |
7530 | { | |
7531 | lang_output_section_statement_type *os; | |
7532 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); | |
7533 | if (os->addr_tree == NULL | |
7534 | && (bfd_link_relocatable (&link_info) | |
7535 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7536 | os->addr_tree = exp_intop (0); | |
7537 | lang_add_section (&os->children, s, NULL, NULL, os); | |
7538 | } | |
7539 | else | |
7540 | { | |
7541 | lang_output_section_statement_type *os; | |
7542 | const char *name = s->name; | |
7543 | int constraint = 0; | |
7544 | ||
7545 | if (config.orphan_handling == orphan_handling_error) | |
7546 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), | |
7547 | s, s->owner); | |
7548 | ||
7549 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
7550 | constraint = SPECIAL; | |
7551 | ||
7552 | os = ldemul_place_orphan (s, name, constraint); | |
7553 | if (os == NULL) | |
7554 | { | |
7555 | os = lang_output_section_statement_lookup (name, constraint, 1); | |
7556 | if (os->addr_tree == NULL | |
7557 | && (bfd_link_relocatable (&link_info) | |
7558 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7559 | os->addr_tree = exp_intop (0); | |
7560 | lang_add_section (&os->children, s, NULL, NULL, os); | |
7561 | } | |
7562 | ||
7563 | if (config.orphan_handling == orphan_handling_warn) | |
7564 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " | |
7565 | "placed in section `%s'\n"), | |
7566 | s, s->owner, os->name); | |
7567 | } | |
7568 | } | |
7569 | ||
7570 | /* Run through the input files and ensure that every input section has | |
7571 | somewhere to go. If one is found without a destination then create | |
7572 | an input request and place it into the statement tree. */ | |
7573 | ||
7574 | static void | |
7575 | lang_place_orphans (void) | |
7576 | { | |
7577 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
7578 | { | |
7579 | asection *s; | |
7580 | ||
7581 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
7582 | { | |
7583 | if (s->output_section == NULL) | |
7584 | { | |
7585 | /* This section of the file is not attached, root | |
7586 | around for a sensible place for it to go. */ | |
7587 | ||
7588 | if (file->flags.just_syms) | |
7589 | bfd_link_just_syms (file->the_bfd, s, &link_info); | |
7590 | else if (lang_discard_section_p (s)) | |
7591 | s->output_section = bfd_abs_section_ptr; | |
7592 | else if (strcmp (s->name, "COMMON") == 0) | |
7593 | { | |
7594 | /* This is a lonely common section which must have | |
7595 | come from an archive. We attach to the section | |
7596 | with the wildcard. */ | |
7597 | if (!bfd_link_relocatable (&link_info) | |
7598 | || command_line.force_common_definition) | |
7599 | { | |
7600 | if (default_common_section == NULL) | |
7601 | default_common_section | |
7602 | = lang_output_section_statement_lookup (".bss", 0, 1); | |
7603 | lang_add_section (&default_common_section->children, s, | |
7604 | NULL, NULL, default_common_section); | |
7605 | } | |
7606 | } | |
7607 | else | |
7608 | ldlang_place_orphan (s); | |
7609 | } | |
7610 | } | |
7611 | } | |
7612 | } | |
7613 | ||
7614 | void | |
7615 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) | |
7616 | { | |
7617 | flagword *ptr_flags; | |
7618 | ||
7619 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; | |
7620 | ||
7621 | while (*flags) | |
7622 | { | |
7623 | switch (*flags) | |
7624 | { | |
7625 | /* PR 17900: An exclamation mark in the attributes reverses | |
7626 | the sense of any of the attributes that follow. */ | |
7627 | case '!': | |
7628 | invert = !invert; | |
7629 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; | |
7630 | break; | |
7631 | ||
7632 | case 'A': case 'a': | |
7633 | *ptr_flags |= SEC_ALLOC; | |
7634 | break; | |
7635 | ||
7636 | case 'R': case 'r': | |
7637 | *ptr_flags |= SEC_READONLY; | |
7638 | break; | |
7639 | ||
7640 | case 'W': case 'w': | |
7641 | *ptr_flags |= SEC_DATA; | |
7642 | break; | |
7643 | ||
7644 | case 'X': case 'x': | |
7645 | *ptr_flags |= SEC_CODE; | |
7646 | break; | |
7647 | ||
7648 | case 'L': case 'l': | |
7649 | case 'I': case 'i': | |
7650 | *ptr_flags |= SEC_LOAD; | |
7651 | break; | |
7652 | ||
7653 | default: | |
7654 | fatal (_("%P: invalid character %c (%d) in flags\n"), | |
7655 | *flags, *flags); | |
7656 | break; | |
7657 | } | |
7658 | flags++; | |
7659 | } | |
7660 | } | |
7661 | ||
7662 | /* Call a function on each real input file. This function will be | |
7663 | called on an archive, but not on the elements. */ | |
7664 | ||
7665 | void | |
7666 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) | |
7667 | { | |
7668 | lang_input_statement_type *f; | |
7669 | ||
7670 | for (f = (void *) input_file_chain.head; | |
7671 | f != NULL; | |
7672 | f = f->next_real_file) | |
7673 | if (f->flags.real) | |
7674 | func (f); | |
7675 | } | |
7676 | ||
7677 | /* Call a function on each real file. The function will be called on | |
7678 | all the elements of an archive which are included in the link, but | |
7679 | will not be called on the archive file itself. */ | |
7680 | ||
7681 | void | |
7682 | lang_for_each_file (void (*func) (lang_input_statement_type *)) | |
7683 | { | |
7684 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7685 | { | |
7686 | if (f->flags.real) | |
7687 | func (f); | |
7688 | } | |
7689 | } | |
7690 | ||
7691 | void | |
7692 | ldlang_add_file (lang_input_statement_type *entry) | |
7693 | { | |
7694 | lang_statement_append (&file_chain, entry, &entry->next); | |
7695 | ||
7696 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7697 | a link. */ | |
7698 | ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next | |
7699 | && entry->the_bfd->link.next == NULL); | |
7700 | ASSERT (entry->the_bfd != link_info.output_bfd); | |
7701 | ||
7702 | *link_info.input_bfds_tail = entry->the_bfd; | |
7703 | link_info.input_bfds_tail = &entry->the_bfd->link.next; | |
7704 | bfd_set_usrdata (entry->the_bfd, entry); | |
7705 | bfd_set_gp_size (entry->the_bfd, g_switch_value); | |
7706 | ||
7707 | /* Look through the sections and check for any which should not be | |
7708 | included in the link. We need to do this now, so that we can | |
7709 | notice when the backend linker tries to report multiple | |
7710 | definition errors for symbols which are in sections we aren't | |
7711 | going to link. FIXME: It might be better to entirely ignore | |
7712 | symbols which are defined in sections which are going to be | |
7713 | discarded. This would require modifying the backend linker for | |
7714 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7715 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7716 | ||
7717 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); | |
7718 | } | |
7719 | ||
7720 | void | |
7721 | lang_add_output (const char *name, int from_script) | |
7722 | { | |
7723 | /* Make -o on command line override OUTPUT in script. */ | |
7724 | if (!had_output_filename || !from_script) | |
7725 | { | |
7726 | output_filename = name; | |
7727 | had_output_filename = true; | |
7728 | } | |
7729 | } | |
7730 | ||
7731 | lang_output_section_statement_type * | |
7732 | lang_enter_output_section_statement (const char *output_section_statement_name, | |
7733 | etree_type *address_exp, | |
7734 | enum section_type sectype, | |
7735 | etree_type *sectype_value, | |
7736 | etree_type *align, | |
7737 | etree_type *subalign, | |
7738 | etree_type *ebase, | |
7739 | int constraint, | |
7740 | int align_with_input) | |
7741 | { | |
7742 | lang_output_section_statement_type *os; | |
7743 | ||
7744 | os = lang_output_section_statement_lookup (output_section_statement_name, | |
7745 | constraint, | |
7746 | in_section_ordering ? 0 : 2); | |
7747 | if (os == NULL) /* && in_section_ordering */ | |
7748 | fatal (_("%P:%pS: error: output section '%s' must already exist\n"), | |
7749 | NULL, output_section_statement_name); | |
7750 | current_section = os; | |
7751 | ||
7752 | /* Make next things chain into subchain of this. */ | |
7753 | push_stat_ptr (in_section_ordering ? &os->sort_children : &os->children); | |
7754 | ||
7755 | if (in_section_ordering) | |
7756 | return os; | |
7757 | ||
7758 | if (os->addr_tree == NULL) | |
7759 | os->addr_tree = address_exp; | |
7760 | ||
7761 | os->sectype = sectype; | |
7762 | if (sectype == type_section || sectype == typed_readonly_section) | |
7763 | os->sectype_value = sectype_value; | |
7764 | else if (sectype == noload_section) | |
7765 | os->flags = SEC_NEVER_LOAD; | |
7766 | else | |
7767 | os->flags = SEC_NO_FLAGS; | |
7768 | os->block_value = 1; | |
7769 | ||
7770 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; | |
7771 | if (os->align_lma_with_input && align != NULL) | |
7772 | fatal (_("%P:%pS: error: align with input and explicit align specified\n"), | |
7773 | NULL); | |
7774 | ||
7775 | os->subsection_alignment = subalign; | |
7776 | os->section_alignment = align; | |
7777 | ||
7778 | os->load_base = ebase; | |
7779 | return os; | |
7780 | } | |
7781 | ||
7782 | void | |
7783 | lang_final (void) | |
7784 | { | |
7785 | lang_output_statement_type *new_stmt; | |
7786 | ||
7787 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7788 | new_stmt->name = output_filename; | |
7789 | } | |
7790 | ||
7791 | /* Reset the current counters in the regions. */ | |
7792 | ||
7793 | void | |
7794 | lang_reset_memory_regions (void) | |
7795 | { | |
7796 | lang_memory_region_type *p = lang_memory_region_list; | |
7797 | asection *o; | |
7798 | lang_output_section_statement_type *os; | |
7799 | ||
7800 | for (p = lang_memory_region_list; p != NULL; p = p->next) | |
7801 | { | |
7802 | p->current = p->origin; | |
7803 | p->last_os = NULL; | |
7804 | } | |
7805 | ||
7806 | for (os = (void *) lang_os_list.head; | |
7807 | os != NULL; | |
7808 | os = os->next) | |
7809 | { | |
7810 | os->processed_vma = false; | |
7811 | os->processed_lma = false; | |
7812 | } | |
7813 | ||
7814 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) | |
7815 | { | |
7816 | /* Save the last size for possible use by bfd_relax_section. */ | |
7817 | o->rawsize = o->size; | |
7818 | if (!(o->flags & SEC_FIXED_SIZE)) | |
7819 | o->size = 0; | |
7820 | } | |
7821 | } | |
7822 | ||
7823 | /* Worker for lang_gc_sections_1. */ | |
7824 | ||
7825 | static void | |
7826 | gc_section_callback (lang_wild_statement_type *ptr, | |
7827 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7828 | asection *section, | |
7829 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7830 | void *data ATTRIBUTE_UNUSED) | |
7831 | { | |
7832 | /* If the wild pattern was marked KEEP, the member sections | |
7833 | should be as well. */ | |
7834 | if (ptr->keep_sections) | |
7835 | section->flags |= SEC_KEEP; | |
7836 | } | |
7837 | ||
7838 | /* Iterate over sections marking them against GC. */ | |
7839 | ||
7840 | static void | |
7841 | lang_gc_sections_1 (lang_statement_union_type *s) | |
7842 | { | |
7843 | for (; s != NULL; s = s->header.next) | |
7844 | { | |
7845 | switch (s->header.type) | |
7846 | { | |
7847 | case lang_wild_statement_enum: | |
7848 | walk_wild (&s->wild_statement, gc_section_callback, NULL); | |
7849 | break; | |
7850 | case lang_constructors_statement_enum: | |
7851 | lang_gc_sections_1 (constructor_list.head); | |
7852 | break; | |
7853 | case lang_output_section_statement_enum: | |
7854 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7855 | break; | |
7856 | case lang_group_statement_enum: | |
7857 | lang_gc_sections_1 (s->group_statement.children.head); | |
7858 | break; | |
7859 | default: | |
7860 | break; | |
7861 | } | |
7862 | } | |
7863 | } | |
7864 | ||
7865 | static void | |
7866 | lang_gc_sections (void) | |
7867 | { | |
7868 | /* Keep all sections so marked in the link script. */ | |
7869 | lang_gc_sections_1 (statement_list.head); | |
7870 | ||
7871 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in | |
7872 | the special case of .stabstr debug info. (See bfd/stabs.c) | |
7873 | Twiddle the flag here, to simplify later linker code. */ | |
7874 | if (bfd_link_relocatable (&link_info)) | |
7875 | { | |
7876 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7877 | { | |
7878 | asection *sec; | |
7879 | #if BFD_SUPPORTS_PLUGINS | |
7880 | if (f->flags.claimed) | |
7881 | continue; | |
7882 | #endif | |
7883 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) | |
7884 | if ((sec->flags & SEC_DEBUGGING) == 0 | |
7885 | || strcmp (sec->name, ".stabstr") != 0) | |
7886 | sec->flags &= ~SEC_EXCLUDE; | |
7887 | } | |
7888 | } | |
7889 | ||
7890 | if (link_info.gc_sections) | |
7891 | bfd_gc_sections (link_info.output_bfd, &link_info); | |
7892 | } | |
7893 | ||
7894 | /* Worker for lang_find_relro_sections_1. */ | |
7895 | ||
7896 | static void | |
7897 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7898 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7899 | asection *section, | |
7900 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7901 | void *data) | |
7902 | { | |
7903 | /* Discarded, excluded and ignored sections effectively have zero | |
7904 | size. */ | |
7905 | if (section->output_section != NULL | |
7906 | && section->output_section->owner == link_info.output_bfd | |
7907 | && (section->output_section->flags & SEC_EXCLUDE) == 0 | |
7908 | && !IGNORE_SECTION (section) | |
7909 | && section->size != 0) | |
7910 | { | |
7911 | bool *has_relro_section = (bool *) data; | |
7912 | *has_relro_section = true; | |
7913 | } | |
7914 | } | |
7915 | ||
7916 | /* Iterate over sections for relro sections. */ | |
7917 | ||
7918 | static void | |
7919 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
7920 | bool *has_relro_section) | |
7921 | { | |
7922 | if (*has_relro_section) | |
7923 | return; | |
7924 | ||
7925 | for (; s != NULL; s = s->header.next) | |
7926 | { | |
7927 | if (s == expld.dataseg.relro_end_stat) | |
7928 | break; | |
7929 | ||
7930 | switch (s->header.type) | |
7931 | { | |
7932 | case lang_wild_statement_enum: | |
7933 | walk_wild (&s->wild_statement, | |
7934 | find_relro_section_callback, | |
7935 | has_relro_section); | |
7936 | break; | |
7937 | case lang_constructors_statement_enum: | |
7938 | lang_find_relro_sections_1 (constructor_list.head, | |
7939 | has_relro_section); | |
7940 | break; | |
7941 | case lang_output_section_statement_enum: | |
7942 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
7943 | has_relro_section); | |
7944 | break; | |
7945 | case lang_group_statement_enum: | |
7946 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
7947 | has_relro_section); | |
7948 | break; | |
7949 | default: | |
7950 | break; | |
7951 | } | |
7952 | } | |
7953 | } | |
7954 | ||
7955 | static void | |
7956 | lang_find_relro_sections (void) | |
7957 | { | |
7958 | bool has_relro_section = false; | |
7959 | ||
7960 | /* Check all sections in the link script. */ | |
7961 | ||
7962 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
7963 | &has_relro_section); | |
7964 | ||
7965 | if (!has_relro_section) | |
7966 | link_info.relro = false; | |
7967 | } | |
7968 | ||
7969 | /* Relax all sections until bfd_relax_section gives up. */ | |
7970 | ||
7971 | void | |
7972 | lang_relax_sections (bool need_layout) | |
7973 | { | |
7974 | /* NB: Also enable relaxation to layout sections for DT_RELR. */ | |
7975 | if (RELAXATION_ENABLED || link_info.enable_dt_relr) | |
7976 | { | |
7977 | /* We may need more than one relaxation pass. */ | |
7978 | int i = link_info.relax_pass; | |
7979 | ||
7980 | /* The backend can use it to determine the current pass. */ | |
7981 | link_info.relax_pass = 0; | |
7982 | ||
7983 | while (i--) | |
7984 | { | |
7985 | /* Keep relaxing until bfd_relax_section gives up. */ | |
7986 | bool relax_again; | |
7987 | ||
7988 | link_info.relax_trip = -1; | |
7989 | do | |
7990 | { | |
7991 | link_info.relax_trip++; | |
7992 | ||
7993 | /* Note: pe-dll.c does something like this also. If you find | |
7994 | you need to change this code, you probably need to change | |
7995 | pe-dll.c also. DJ */ | |
7996 | ||
7997 | /* Do all the assignments with our current guesses as to | |
7998 | section sizes. */ | |
7999 | lang_do_assignments (lang_assigning_phase_enum); | |
8000 | ||
8001 | /* We must do this after lang_do_assignments, because it uses | |
8002 | size. */ | |
8003 | lang_reset_memory_regions (); | |
8004 | ||
8005 | /* Perform another relax pass - this time we know where the | |
8006 | globals are, so can make a better guess. */ | |
8007 | relax_again = false; | |
8008 | lang_size_sections (&relax_again, false); | |
8009 | } | |
8010 | while (relax_again); | |
8011 | ||
8012 | link_info.relax_pass++; | |
8013 | } | |
8014 | need_layout = true; | |
8015 | } | |
8016 | ||
8017 | if (need_layout) | |
8018 | { | |
8019 | /* Final extra sizing to report errors. */ | |
8020 | lang_do_assignments (lang_assigning_phase_enum); | |
8021 | lang_reset_memory_regions (); | |
8022 | lang_size_sections (NULL, true); | |
8023 | } | |
8024 | } | |
8025 | ||
8026 | #if BFD_SUPPORTS_PLUGINS | |
8027 | /* Find the insert point for the plugin's replacement files. We | |
8028 | place them after the first claimed real object file, or if the | |
8029 | first claimed object is an archive member, after the last real | |
8030 | object file immediately preceding the archive. In the event | |
8031 | no objects have been claimed at all, we return the first dummy | |
8032 | object file on the list as the insert point; that works, but | |
8033 | the callee must be careful when relinking the file_chain as it | |
8034 | is not actually on that chain, only the statement_list and the | |
8035 | input_file list; in that case, the replacement files must be | |
8036 | inserted at the head of the file_chain. */ | |
8037 | ||
8038 | static lang_input_statement_type * | |
8039 | find_replacements_insert_point (bool *before) | |
8040 | { | |
8041 | lang_input_statement_type *claim1, *lastobject; | |
8042 | lastobject = (void *) input_file_chain.head; | |
8043 | for (claim1 = (void *) file_chain.head; | |
8044 | claim1 != NULL; | |
8045 | claim1 = claim1->next) | |
8046 | { | |
8047 | if (claim1->flags.claimed) | |
8048 | { | |
8049 | *before = claim1->flags.claim_archive; | |
8050 | return claim1->flags.claim_archive ? lastobject : claim1; | |
8051 | } | |
8052 | /* Update lastobject if this is a real object file. */ | |
8053 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) | |
8054 | lastobject = claim1; | |
8055 | } | |
8056 | /* No files were claimed by the plugin. Choose the last object | |
8057 | file found on the list (maybe the first, dummy entry) as the | |
8058 | insert point. */ | |
8059 | *before = false; | |
8060 | return lastobject; | |
8061 | } | |
8062 | ||
8063 | /* Find where to insert ADD, an archive element or shared library | |
8064 | added during a rescan. */ | |
8065 | ||
8066 | static lang_input_statement_type ** | |
8067 | find_rescan_insertion (lang_input_statement_type *add) | |
8068 | { | |
8069 | bfd *add_bfd = add->the_bfd; | |
8070 | lang_input_statement_type *f; | |
8071 | lang_input_statement_type *last_loaded = NULL; | |
8072 | lang_input_statement_type *before = NULL; | |
8073 | lang_input_statement_type **iter = NULL; | |
8074 | ||
8075 | if (add_bfd->my_archive != NULL) | |
8076 | add_bfd = add_bfd->my_archive; | |
8077 | ||
8078 | /* First look through the input file chain, to find an object file | |
8079 | before the one we've rescanned. Normal object files always | |
8080 | appear on both the input file chain and the file chain, so this | |
8081 | lets us get quickly to somewhere near the correct place on the | |
8082 | file chain if it is full of archive elements. Archives don't | |
8083 | appear on the file chain, but if an element has been extracted | |
8084 | then their input_statement->next points at it. */ | |
8085 | for (f = (void *) input_file_chain.head; | |
8086 | f != NULL; | |
8087 | f = f->next_real_file) | |
8088 | { | |
8089 | if (f->the_bfd == add_bfd) | |
8090 | { | |
8091 | before = last_loaded; | |
8092 | if (f->next != NULL) | |
8093 | return &f->next->next; | |
8094 | } | |
8095 | if (f->the_bfd != NULL && f->next != NULL) | |
8096 | last_loaded = f; | |
8097 | } | |
8098 | ||
8099 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
8100 | *iter != NULL; | |
8101 | iter = &(*iter)->next) | |
8102 | if (!(*iter)->flags.claim_archive | |
8103 | && (*iter)->the_bfd->my_archive == NULL) | |
8104 | break; | |
8105 | ||
8106 | return iter; | |
8107 | } | |
8108 | ||
8109 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
8110 | was taken as a copy of it and leave them in ORIGLIST. */ | |
8111 | ||
8112 | static void | |
8113 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
8114 | lang_statement_list_type *origlist) | |
8115 | { | |
8116 | union lang_statement_union **savetail; | |
8117 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
8118 | ASSERT (origlist->head == destlist->head); | |
8119 | savetail = origlist->tail; | |
8120 | origlist->head = *(savetail); | |
8121 | origlist->tail = destlist->tail; | |
8122 | destlist->tail = savetail; | |
8123 | *savetail = NULL; | |
8124 | } | |
8125 | ||
8126 | static lang_statement_union_type ** | |
8127 | find_next_input_statement (lang_statement_union_type **s) | |
8128 | { | |
8129 | for ( ; *s; s = &(*s)->header.next) | |
8130 | { | |
8131 | lang_statement_union_type **t; | |
8132 | switch ((*s)->header.type) | |
8133 | { | |
8134 | case lang_input_statement_enum: | |
8135 | return s; | |
8136 | case lang_wild_statement_enum: | |
8137 | t = &(*s)->wild_statement.children.head; | |
8138 | break; | |
8139 | case lang_group_statement_enum: | |
8140 | t = &(*s)->group_statement.children.head; | |
8141 | break; | |
8142 | case lang_output_section_statement_enum: | |
8143 | t = &(*s)->output_section_statement.children.head; | |
8144 | break; | |
8145 | default: | |
8146 | continue; | |
8147 | } | |
8148 | t = find_next_input_statement (t); | |
8149 | if (*t) | |
8150 | return t; | |
8151 | } | |
8152 | return s; | |
8153 | } | |
8154 | #endif /* BFD_SUPPORTS_PLUGINS */ | |
8155 | ||
8156 | /* Insert SRCLIST into DESTLIST after given element by chaining | |
8157 | on FIELD as the next-pointer. (Counterintuitively does not need | |
8158 | a pointer to the actual after-node itself, just its chain field.) */ | |
8159 | ||
8160 | static void | |
8161 | lang_list_insert_after (lang_statement_list_type *destlist, | |
8162 | lang_statement_list_type *srclist, | |
8163 | lang_statement_union_type **field) | |
8164 | { | |
8165 | *(srclist->tail) = *field; | |
8166 | *field = srclist->head; | |
8167 | if (destlist->tail == field) | |
8168 | destlist->tail = srclist->tail; | |
8169 | } | |
8170 | ||
8171 | /* Add NAME to the list of garbage collection entry points. */ | |
8172 | ||
8173 | void | |
8174 | lang_add_gc_name (const char *name) | |
8175 | { | |
8176 | struct bfd_sym_chain *sym; | |
8177 | ||
8178 | if (name == NULL) | |
8179 | return; | |
8180 | ||
8181 | sym = stat_alloc (sizeof (*sym)); | |
8182 | ||
8183 | sym->next = link_info.gc_sym_list; | |
8184 | sym->name = name; | |
8185 | link_info.gc_sym_list = sym; | |
8186 | } | |
8187 | ||
8188 | /* Check relocations. */ | |
8189 | ||
8190 | static void | |
8191 | lang_check_relocs (void) | |
8192 | { | |
8193 | if (link_info.check_relocs_after_open_input) | |
8194 | { | |
8195 | bfd *abfd; | |
8196 | ||
8197 | for (abfd = link_info.input_bfds; | |
8198 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
8199 | if (!bfd_link_check_relocs (abfd, &link_info)) | |
8200 | { | |
8201 | /* No object output, fail return. */ | |
8202 | config.make_executable = false; | |
8203 | /* Note: we do not abort the loop, but rather | |
8204 | continue the scan in case there are other | |
8205 | bad relocations to report. */ | |
8206 | } | |
8207 | } | |
8208 | } | |
8209 | ||
8210 | /* Look through all output sections looking for places where we can | |
8211 | propagate forward the lma region. */ | |
8212 | ||
8213 | static void | |
8214 | lang_propagate_lma_regions (void) | |
8215 | { | |
8216 | lang_output_section_statement_type *os; | |
8217 | ||
8218 | for (os = (void *) lang_os_list.head; | |
8219 | os != NULL; | |
8220 | os = os->next) | |
8221 | { | |
8222 | if (os->prev != NULL | |
8223 | && os->lma_region == NULL | |
8224 | && os->load_base == NULL | |
8225 | && os->addr_tree == NULL | |
8226 | && os->region == os->prev->region) | |
8227 | os->lma_region = os->prev->lma_region; | |
8228 | } | |
8229 | } | |
8230 | ||
8231 | static void | |
8232 | warn_non_contiguous_discards (void) | |
8233 | { | |
8234 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
8235 | { | |
8236 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
8237 | || file->flags.just_syms) | |
8238 | continue; | |
8239 | ||
8240 | for (asection *s = file->the_bfd->sections; s != NULL; s = s->next) | |
8241 | if (s->output_section == NULL | |
8242 | && (s->flags & SEC_LINKER_CREATED) == 0) | |
8243 | einfo (_("%P: warning: --enable-non-contiguous-regions " | |
8244 | "discards section `%pA' from `%pB'\n"), | |
8245 | s, file->the_bfd); | |
8246 | } | |
8247 | } | |
8248 | ||
8249 | static void | |
8250 | reset_one_wild (lang_statement_union_type *statement) | |
8251 | { | |
8252 | if (statement->header.type == lang_wild_statement_enum) | |
8253 | { | |
8254 | lang_wild_statement_type *stmt = &statement->wild_statement; | |
8255 | lang_list_init (&stmt->matching_sections); | |
8256 | } | |
8257 | } | |
8258 | ||
8259 | static void | |
8260 | reset_resolved_wilds (void) | |
8261 | { | |
8262 | lang_for_each_statement (reset_one_wild); | |
8263 | } | |
8264 | ||
8265 | /* For each output section statement, splice any entries on the | |
8266 | sort_children list before the first wild statement on the children | |
8267 | list. */ | |
8268 | ||
8269 | static void | |
8270 | lang_os_merge_sort_children (void) | |
8271 | { | |
8272 | lang_output_section_statement_type *os; | |
8273 | for (os = (void *) lang_os_list.head; os != NULL; os = os->next) | |
8274 | { | |
8275 | if (os->sort_children.head != NULL) | |
8276 | { | |
8277 | lang_statement_union_type **where; | |
8278 | for (where = &os->children.head; | |
8279 | *where != NULL; | |
8280 | where = &(*where)->header.next) | |
8281 | if ((*where)->header.type == lang_wild_statement_enum) | |
8282 | break; | |
8283 | lang_list_insert_after (&os->children, &os->sort_children, where); | |
8284 | } | |
8285 | } | |
8286 | } | |
8287 | ||
8288 | void | |
8289 | lang_process (void) | |
8290 | { | |
8291 | lang_os_merge_sort_children (); | |
8292 | ||
8293 | /* Finalize dynamic list. */ | |
8294 | if (link_info.dynamic_list) | |
8295 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
8296 | ||
8297 | current_target = default_target; | |
8298 | ||
8299 | /* Open the output file. */ | |
8300 | lang_for_each_statement (ldlang_open_output); | |
8301 | init_opb (NULL); | |
8302 | ||
8303 | ldemul_create_output_section_statements (); | |
8304 | ||
8305 | /* Add to the hash table all undefineds on the command line. */ | |
8306 | lang_place_undefineds (); | |
8307 | ||
8308 | if (!bfd_section_already_linked_table_init ()) | |
8309 | fatal (_("%P: can not create hash table: %E\n")); | |
8310 | ||
8311 | /* A first pass through the memory regions ensures that if any region | |
8312 | references a symbol for its origin or length then this symbol will be | |
8313 | added to the symbol table. Having these symbols in the symbol table | |
8314 | means that when we call open_input_bfds PROVIDE statements will | |
8315 | trigger to provide any needed symbols. The regions origins and | |
8316 | lengths are not assigned as a result of this call. */ | |
8317 | lang_do_memory_regions (false); | |
8318 | ||
8319 | /* Create a bfd for each input file. */ | |
8320 | current_target = default_target; | |
8321 | lang_statement_iteration++; | |
8322 | open_input_bfds (statement_list.head, NULL, OPEN_BFD_NORMAL); | |
8323 | ||
8324 | /* Now that open_input_bfds has processed assignments and provide | |
8325 | statements we can give values to symbolic origin/length now. */ | |
8326 | lang_do_memory_regions (true); | |
8327 | ||
8328 | ldemul_before_plugin_all_symbols_read (); | |
8329 | ||
8330 | #if BFD_SUPPORTS_PLUGINS | |
8331 | if (link_info.lto_plugin_active) | |
8332 | { | |
8333 | lang_statement_list_type added; | |
8334 | lang_statement_list_type files, inputfiles; | |
8335 | ||
8336 | /* Now all files are read, let the plugin(s) decide if there | |
8337 | are any more to be added to the link before we call the | |
8338 | emulation's after_open hook. We create a private list of | |
8339 | input statements for this purpose, which we will eventually | |
8340 | insert into the global statement list after the first claimed | |
8341 | file. */ | |
8342 | added = *stat_ptr; | |
8343 | /* We need to manipulate all three chains in synchrony. */ | |
8344 | files = file_chain; | |
8345 | inputfiles = input_file_chain; | |
8346 | if (plugin_call_all_symbols_read ()) | |
8347 | fatal (_("%P: %s: plugin reported error after all symbols read\n"), | |
8348 | plugin_error_plugin ()); | |
8349 | link_info.lto_all_symbols_read = true; | |
8350 | /* Open any newly added files, updating the file chains. */ | |
8351 | plugin_undefs = link_info.hash->undefs_tail; | |
8352 | lang_output_section_statement_type *last_os = NULL; | |
8353 | if (lang_os_list.head != NULL) | |
8354 | last_os = ((lang_output_section_statement_type *) | |
8355 | ((char *) lang_os_list.tail | |
8356 | - offsetof (lang_output_section_statement_type, next))); | |
8357 | open_input_bfds (*added.tail, last_os, OPEN_BFD_NORMAL); | |
8358 | if (plugin_undefs == link_info.hash->undefs_tail) | |
8359 | plugin_undefs = NULL; | |
8360 | /* Restore the global list pointer now they have all been added. */ | |
8361 | lang_list_remove_tail (stat_ptr, &added); | |
8362 | /* And detach the fresh ends of the file lists. */ | |
8363 | lang_list_remove_tail (&file_chain, &files); | |
8364 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
8365 | /* Were any new files added? */ | |
8366 | if (added.head != NULL) | |
8367 | { | |
8368 | /* If so, we will insert them into the statement list immediately | |
8369 | after the first input file that was claimed by the plugin, | |
8370 | unless that file was an archive in which case it is inserted | |
8371 | immediately before. */ | |
8372 | bool before; | |
8373 | lang_statement_union_type **prev; | |
8374 | plugin_insert = find_replacements_insert_point (&before); | |
8375 | /* If a plugin adds input files without having claimed any, we | |
8376 | don't really have a good idea where to place them. Just putting | |
8377 | them at the start or end of the list is liable to leave them | |
8378 | outside the crtbegin...crtend range. */ | |
8379 | ASSERT (plugin_insert != NULL); | |
8380 | /* Splice the new statement list into the old one. */ | |
8381 | prev = &plugin_insert->header.next; | |
8382 | if (before) | |
8383 | { | |
8384 | prev = find_next_input_statement (prev); | |
8385 | if (*prev != (void *) plugin_insert->next_real_file) | |
8386 | { | |
8387 | /* We didn't find the expected input statement. | |
8388 | Fall back to adding after plugin_insert. */ | |
8389 | prev = &plugin_insert->header.next; | |
8390 | } | |
8391 | } | |
8392 | lang_list_insert_after (stat_ptr, &added, prev); | |
8393 | /* Likewise for the file chains. */ | |
8394 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
8395 | (void *) &plugin_insert->next_real_file); | |
8396 | /* We must be careful when relinking file_chain; we may need to | |
8397 | insert the new files at the head of the list if the insert | |
8398 | point chosen is the dummy first input file. */ | |
8399 | if (plugin_insert->filename) | |
8400 | lang_list_insert_after (&file_chain, &files, | |
8401 | (void *) &plugin_insert->next); | |
8402 | else | |
8403 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
8404 | ||
8405 | /* Rescan archives in case new undefined symbols have appeared. */ | |
8406 | files = file_chain; | |
8407 | lang_statement_iteration++; | |
8408 | open_input_bfds (statement_list.head, NULL, OPEN_BFD_RESCAN); | |
8409 | lang_list_remove_tail (&file_chain, &files); | |
8410 | while (files.head != NULL) | |
8411 | { | |
8412 | lang_input_statement_type **insert; | |
8413 | lang_input_statement_type **iter, *temp; | |
8414 | bfd *my_arch; | |
8415 | ||
8416 | insert = find_rescan_insertion (&files.head->input_statement); | |
8417 | /* All elements from an archive can be added at once. */ | |
8418 | iter = &files.head->input_statement.next; | |
8419 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
8420 | if (my_arch != NULL) | |
8421 | for (; *iter != NULL; iter = &(*iter)->next) | |
8422 | if ((*iter)->the_bfd->my_archive != my_arch) | |
8423 | break; | |
8424 | temp = *insert; | |
8425 | *insert = &files.head->input_statement; | |
8426 | files.head = (lang_statement_union_type *) *iter; | |
8427 | *iter = temp; | |
8428 | if (file_chain.tail == (lang_statement_union_type **) insert) | |
8429 | file_chain.tail = (lang_statement_union_type **) iter; | |
8430 | if (my_arch != NULL) | |
8431 | { | |
8432 | lang_input_statement_type *parent = bfd_usrdata (my_arch); | |
8433 | if (parent != NULL) | |
8434 | parent->next = (lang_input_statement_type *) | |
8435 | ((char *) iter | |
8436 | - offsetof (lang_input_statement_type, next)); | |
8437 | } | |
8438 | } | |
8439 | } | |
8440 | } | |
8441 | else | |
8442 | #endif /* BFD_SUPPORTS_PLUGINS */ | |
8443 | if (bfd_link_relocatable (&link_info)) | |
8444 | { | |
8445 | /* Check if .gnu_object_only section should be created. */ | |
8446 | bfd *p; | |
8447 | int object_type; | |
8448 | ||
8449 | object_type = 0; | |
8450 | for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link.next) | |
8451 | { | |
8452 | enum bfd_lto_object_type lto_type = bfd_get_lto_type (p); | |
8453 | /* NB: Treat fat IR object as IR object here. */ | |
8454 | if (lto_type == lto_fat_ir_object) | |
8455 | lto_type = lto_slim_ir_object; | |
8456 | object_type |= 1 << lto_type; | |
8457 | if ((object_type & (1 << lto_mixed_object)) != 0 | |
8458 | || ((object_type | |
8459 | & (1 << lto_non_ir_object | |
8460 | | 1 << lto_slim_ir_object)) | |
8461 | == (1 << lto_non_ir_object | 1 << lto_slim_ir_object))) | |
8462 | { | |
8463 | config.emit_gnu_object_only = true; | |
8464 | break; | |
8465 | } | |
8466 | } | |
8467 | ||
8468 | if (verbose | |
8469 | && (cmdline_object_only_file_list.head | |
8470 | || cmdline_object_only_archive_list.head)) | |
8471 | { | |
8472 | info_msg (_("Object-only input files:\n ")); | |
8473 | print_cmdline_list (cmdline_object_only_file_list.head); | |
8474 | print_cmdline_list (cmdline_object_only_archive_list.head); | |
8475 | } | |
8476 | } | |
8477 | ||
8478 | struct bfd_sym_chain **sym = &link_info.gc_sym_list; | |
8479 | while (*sym) | |
8480 | sym = &(*sym)->next; | |
8481 | ||
8482 | *sym = &entry_symbol; | |
8483 | ||
8484 | if (entry_symbol.name == NULL) | |
8485 | { | |
8486 | *sym = ldlang_undef_chain_list_head; | |
8487 | ||
8488 | /* entry_symbol is normally initialised by an ENTRY definition in the | |
8489 | linker script or the -e command line option. But if neither of | |
8490 | these have been used, the target specific backend may still have | |
8491 | provided an entry symbol via a call to lang_default_entry(). | |
8492 | Unfortunately this value will not be processed until lang_end() | |
8493 | is called, long after this function has finished. So detect this | |
8494 | case here and add the target's entry symbol to the list of starting | |
8495 | points for garbage collection resolution. */ | |
8496 | lang_add_gc_name (entry_symbol_default); | |
8497 | } | |
8498 | ||
8499 | lang_add_gc_name (link_info.init_function); | |
8500 | lang_add_gc_name (link_info.fini_function); | |
8501 | ||
8502 | ldemul_after_open (); | |
8503 | if (config.map_file != NULL) | |
8504 | lang_print_asneeded (); | |
8505 | ||
8506 | ldlang_open_ctf (); | |
8507 | ||
8508 | bfd_section_already_linked_table_free (); | |
8509 | ||
8510 | /* Make sure that we're not mixing architectures. We call this | |
8511 | after all the input files have been opened, but before we do any | |
8512 | other processing, so that any operations merge_private_bfd_data | |
8513 | does on the output file will be known during the rest of the | |
8514 | link. */ | |
8515 | lang_check (); | |
8516 | ||
8517 | /* Handle .exports instead of a version script if we're told to do so. */ | |
8518 | if (command_line.version_exports_section) | |
8519 | lang_do_version_exports_section (); | |
8520 | ||
8521 | /* Build all sets based on the information gathered from the input | |
8522 | files. */ | |
8523 | ldctor_build_sets (); | |
8524 | ||
8525 | lang_symbol_tweaks (); | |
8526 | ||
8527 | /* PR 13683: We must rerun the assignments prior to running garbage | |
8528 | collection in order to make sure that all symbol aliases are resolved. */ | |
8529 | lang_do_assignments (lang_mark_phase_enum); | |
8530 | expld.phase = lang_first_phase_enum; | |
8531 | ||
8532 | /* Size up the common data. */ | |
8533 | lang_common (); | |
8534 | ||
8535 | if (0) | |
8536 | debug_prefix_tree (); | |
8537 | ||
8538 | resolve_wilds (); | |
8539 | ||
8540 | /* Remove unreferenced sections if asked to. */ | |
8541 | lang_gc_sections (); | |
8542 | ||
8543 | lang_mark_undefineds (); | |
8544 | ||
8545 | /* Check relocations. */ | |
8546 | lang_check_relocs (); | |
8547 | ||
8548 | ldemul_after_check_relocs (); | |
8549 | ||
8550 | /* There might have been new sections created (e.g. as result of | |
8551 | checking relocs to need a .got, or suchlike), so to properly order | |
8552 | them into our lists of matching sections reset them here. */ | |
8553 | reset_resolved_wilds (); | |
8554 | resolve_wilds (); | |
8555 | ||
8556 | /* Update wild statements in case the user gave --sort-section. | |
8557 | Note how the option might have come after the linker script and | |
8558 | so couldn't have been set when the wild statements were created. */ | |
8559 | update_wild_statements (statement_list.head); | |
8560 | ||
8561 | /* Run through the contours of the script and attach input sections | |
8562 | to the correct output sections. */ | |
8563 | lang_statement_iteration++; | |
8564 | map_input_to_output_sections (statement_list.head, NULL, NULL); | |
8565 | ||
8566 | /* Start at the statement immediately after the special abs_section | |
8567 | output statement, so that it isn't reordered. */ | |
8568 | process_insert_statements (&lang_os_list.head->header.next); | |
8569 | ||
8570 | ldemul_before_place_orphans (); | |
8571 | ||
8572 | /* Find any sections not attached explicitly and handle them. */ | |
8573 | lang_place_orphans (); | |
8574 | ||
8575 | if (!bfd_link_relocatable (&link_info)) | |
8576 | { | |
8577 | asection *found; | |
8578 | ||
8579 | ld_start_phase (PHASE_MERGE); | |
8580 | ||
8581 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
8582 | sections, so that GCed sections are not merged, but before | |
8583 | assigning dynamic symbols, since removing whole input sections | |
8584 | is hard then. */ | |
8585 | if (!bfd_merge_sections (link_info.output_bfd, &link_info)) | |
8586 | fatal (_("%P: bfd_merge_sections failed: %E\n")); | |
8587 | ||
8588 | ld_stop_phase (PHASE_MERGE); | |
8589 | ||
8590 | /* Look for a text section and set the readonly attribute in it. */ | |
8591 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); | |
8592 | ||
8593 | if (found != NULL) | |
8594 | { | |
8595 | if (config.text_read_only) | |
8596 | found->flags |= SEC_READONLY; | |
8597 | else | |
8598 | found->flags &= ~SEC_READONLY; | |
8599 | } | |
8600 | } | |
8601 | ||
8602 | /* Merge together CTF sections. After this, only the symtab-dependent | |
8603 | function and data object sections need adjustment. */ | |
8604 | lang_merge_ctf (); | |
8605 | ||
8606 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
8607 | examining things laid out late, like the strtab. */ | |
8608 | lang_write_ctf (0); | |
8609 | ||
8610 | /* Copy forward lma regions for output sections in same lma region. */ | |
8611 | lang_propagate_lma_regions (); | |
8612 | ||
8613 | /* Defining __start/__stop symbols early for --gc-sections to work | |
8614 | around a glibc build problem can result in these symbols being | |
8615 | defined when they should not be. Fix them now. */ | |
8616 | if (config.build_constructors) | |
8617 | lang_undef_start_stop (); | |
8618 | ||
8619 | /* Define .startof./.sizeof. symbols with preliminary values before | |
8620 | dynamic symbols are created. */ | |
8621 | if (!bfd_link_relocatable (&link_info)) | |
8622 | lang_init_startof_sizeof (); | |
8623 | ||
8624 | /* Do anything special before sizing sections. This is where ELF | |
8625 | and other back-ends size dynamic sections. */ | |
8626 | ldemul_before_allocation (); | |
8627 | ||
8628 | /* We must record the program headers before we try to fix the | |
8629 | section positions, since they will affect SIZEOF_HEADERS. */ | |
8630 | lang_record_phdrs (); | |
8631 | ||
8632 | /* Check relro sections. */ | |
8633 | if (link_info.relro && !bfd_link_relocatable (&link_info)) | |
8634 | lang_find_relro_sections (); | |
8635 | ||
8636 | /* Size up the sections. */ | |
8637 | lang_size_sections (NULL, !RELAXATION_ENABLED); | |
8638 | ||
8639 | /* See if anything special should be done now we know how big | |
8640 | everything is. This is where relaxation is done. */ | |
8641 | ldemul_after_allocation (); | |
8642 | ||
8643 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ | |
8644 | lang_finalize_start_stop (); | |
8645 | ||
8646 | /* Do all the assignments again, to report errors. Assignment | |
8647 | statements are processed multiple times, updating symbols; In | |
8648 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
8649 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
8650 | also updated in ldemul_after_allocation. */ | |
8651 | lang_do_assignments (lang_final_phase_enum); | |
8652 | ||
8653 | ldemul_finish (); | |
8654 | ||
8655 | /* Convert absolute symbols to section relative. */ | |
8656 | ldexp_finalize_syms (); | |
8657 | ||
8658 | /* Make sure that the section addresses make sense. */ | |
8659 | if (command_line.check_section_addresses) | |
8660 | lang_check_section_addresses (); | |
8661 | ||
8662 | if (link_info.non_contiguous_regions | |
8663 | && link_info.non_contiguous_regions_warnings) | |
8664 | warn_non_contiguous_discards (); | |
8665 | ||
8666 | /* Check any required symbols are known. */ | |
8667 | ldlang_check_require_defined_symbols (); | |
8668 | ||
8669 | lang_end (); | |
8670 | } | |
8671 | ||
8672 | void | |
8673 | lang_add_version_string (void) | |
8674 | { | |
8675 | if (! enable_linker_version) | |
8676 | return; | |
8677 | ||
8678 | const char * str = "GNU ld "; | |
8679 | int len = strlen (str); | |
8680 | int i; | |
8681 | ||
8682 | for (i = 0 ; i < len ; i++) | |
8683 | lang_add_data (BYTE, exp_intop (str[i])); | |
8684 | ||
8685 | str = BFD_VERSION_STRING; | |
8686 | len = strlen (str); | |
8687 | ||
8688 | for (i = 0 ; i < len ; i++) | |
8689 | lang_add_data (BYTE, exp_intop (str[i])); | |
8690 | ||
8691 | lang_add_data (BYTE, exp_intop ('\0')); | |
8692 | } | |
8693 | ||
8694 | /* EXPORTED TO YACC */ | |
8695 | ||
8696 | void | |
8697 | lang_add_wild (struct wildcard_spec *filespec, | |
8698 | struct wildcard_list *section_list, | |
8699 | bool keep_sections) | |
8700 | { | |
8701 | struct wildcard_list *curr, *next; | |
8702 | lang_wild_statement_type *new_stmt; | |
8703 | bool any_specs_sorted = false; | |
8704 | ||
8705 | /* Reverse the list as the parser puts it back to front. */ | |
8706 | for (curr = section_list, section_list = NULL; | |
8707 | curr != NULL; | |
8708 | section_list = curr, curr = next) | |
8709 | { | |
8710 | if (curr->spec.sorted != none && curr->spec.sorted != by_none) | |
8711 | any_specs_sorted = true; | |
8712 | next = curr->next; | |
8713 | curr->next = section_list; | |
8714 | } | |
8715 | ||
8716 | if (filespec != NULL && filespec->name != NULL) | |
8717 | { | |
8718 | if (strcmp (filespec->name, "*") == 0) | |
8719 | filespec->name = NULL; | |
8720 | else if (!wildcardp (filespec->name)) | |
8721 | lang_has_input_file = true; | |
8722 | } | |
8723 | ||
8724 | new_stmt = new_stat (lang_wild_statement, stat_ptr); | |
8725 | new_stmt->filename = NULL; | |
8726 | new_stmt->filenames_sorted = false; | |
8727 | new_stmt->filenames_reversed = false; | |
8728 | new_stmt->any_specs_sorted = any_specs_sorted; | |
8729 | new_stmt->section_flag_list = NULL; | |
8730 | new_stmt->exclude_name_list = NULL; | |
8731 | if (filespec != NULL) | |
8732 | { | |
8733 | new_stmt->filename = filespec->name; | |
8734 | new_stmt->filenames_sorted = (filespec->sorted == by_name || filespec->reversed); | |
8735 | new_stmt->filenames_reversed = filespec->reversed; | |
8736 | new_stmt->section_flag_list = filespec->section_flag_list; | |
8737 | new_stmt->exclude_name_list = filespec->exclude_name_list; | |
8738 | } | |
8739 | new_stmt->section_list = section_list; | |
8740 | new_stmt->keep_sections = keep_sections; | |
8741 | lang_list_init (&new_stmt->children); | |
8742 | lang_list_init (&new_stmt->matching_sections); | |
8743 | analyze_walk_wild_section_handler (new_stmt); | |
8744 | if (0) | |
8745 | { | |
8746 | printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); | |
8747 | for (curr = new_stmt->section_list; curr; curr = curr->next) | |
8748 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
8749 | printf (")\n"); | |
8750 | } | |
8751 | } | |
8752 | ||
8753 | void | |
8754 | lang_section_start (const char *name, etree_type *address, | |
8755 | const segment_type *segment) | |
8756 | { | |
8757 | lang_address_statement_type *ad; | |
8758 | ||
8759 | ad = new_stat (lang_address_statement, stat_ptr); | |
8760 | ad->section_name = name; | |
8761 | ad->address = address; | |
8762 | ad->segment = segment; | |
8763 | } | |
8764 | ||
8765 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
8766 | because of a -e argument on the command line, or zero if this is | |
8767 | called by ENTRY in a linker script. Command line arguments take | |
8768 | precedence. */ | |
8769 | ||
8770 | void | |
8771 | lang_add_entry (const char *name, bool cmdline) | |
8772 | { | |
8773 | if (entry_symbol.name == NULL | |
8774 | || cmdline | |
8775 | || !entry_from_cmdline) | |
8776 | { | |
8777 | entry_symbol.name = name; | |
8778 | entry_from_cmdline = cmdline; | |
8779 | } | |
8780 | } | |
8781 | ||
8782 | /* Set the default start symbol to NAME. .em files should use this, | |
8783 | not lang_add_entry, to override the use of "start" if neither the | |
8784 | linker script nor the command line specifies an entry point. NAME | |
8785 | must be permanently allocated. */ | |
8786 | void | |
8787 | lang_default_entry (const char *name) | |
8788 | { | |
8789 | entry_symbol_default = name; | |
8790 | } | |
8791 | ||
8792 | void | |
8793 | lang_add_target (const char *name) | |
8794 | { | |
8795 | lang_target_statement_type *new_stmt; | |
8796 | ||
8797 | new_stmt = new_stat (lang_target_statement, stat_ptr); | |
8798 | new_stmt->target = name; | |
8799 | } | |
8800 | ||
8801 | void | |
8802 | lang_add_map (const char *name) | |
8803 | { | |
8804 | while (*name) | |
8805 | { | |
8806 | switch (*name) | |
8807 | { | |
8808 | case 'F': | |
8809 | map_option_f = true; | |
8810 | break; | |
8811 | } | |
8812 | name++; | |
8813 | } | |
8814 | } | |
8815 | ||
8816 | void | |
8817 | lang_add_fill (fill_type *fill) | |
8818 | { | |
8819 | lang_fill_statement_type *new_stmt; | |
8820 | ||
8821 | new_stmt = new_stat (lang_fill_statement, stat_ptr); | |
8822 | new_stmt->fill = fill; | |
8823 | } | |
8824 | ||
8825 | void | |
8826 | lang_add_data (int type, union etree_union *exp) | |
8827 | { | |
8828 | lang_data_statement_type *new_stmt; | |
8829 | ||
8830 | new_stmt = new_stat (lang_data_statement, stat_ptr); | |
8831 | new_stmt->exp = exp; | |
8832 | new_stmt->type = type; | |
8833 | } | |
8834 | ||
8835 | void | |
8836 | lang_add_string (const char *s) | |
8837 | { | |
8838 | bfd_vma len = strlen (s); | |
8839 | bfd_vma i; | |
8840 | bool escape = false; | |
8841 | ||
8842 | /* Add byte expressions until end of string. */ | |
8843 | for (i = 0 ; i < len; i++) | |
8844 | { | |
8845 | char c = *s++; | |
8846 | ||
8847 | if (escape) | |
8848 | { | |
8849 | switch (c) | |
8850 | { | |
8851 | default: | |
8852 | /* Ignore the escape. */ | |
8853 | break; | |
8854 | ||
8855 | case 'n': c = '\n'; break; | |
8856 | case 'r': c = '\r'; break; | |
8857 | case 't': c = '\t'; break; | |
8858 | ||
8859 | case '0': | |
8860 | case '1': | |
8861 | case '2': | |
8862 | case '3': | |
8863 | case '4': | |
8864 | case '5': | |
8865 | case '6': | |
8866 | case '7': | |
8867 | /* We have an octal number. */ | |
8868 | { | |
8869 | unsigned int value = c - '0'; | |
8870 | ||
8871 | c = *s; | |
8872 | if ((c >= '0') && (c <= '7')) | |
8873 | { | |
8874 | value <<= 3; | |
8875 | value += (c - '0'); | |
8876 | i++; | |
8877 | s++; | |
8878 | ||
8879 | c = *s; | |
8880 | if ((c >= '0') && (c <= '7')) | |
8881 | { | |
8882 | value <<= 3; | |
8883 | value += (c - '0'); | |
8884 | i++; | |
8885 | s++; | |
8886 | } | |
8887 | } | |
8888 | ||
8889 | if (value > 0xff) | |
8890 | { | |
8891 | /* octal: \777 is treated as '\077' + '7' */ | |
8892 | value >>= 3; | |
8893 | i--; | |
8894 | s--; | |
8895 | } | |
8896 | ||
8897 | c = value; | |
8898 | } | |
8899 | break; | |
8900 | } | |
8901 | ||
8902 | lang_add_data (BYTE, exp_intop (c)); | |
8903 | escape = false; | |
8904 | } | |
8905 | else | |
8906 | { | |
8907 | if (c == '\\') | |
8908 | escape = true; | |
8909 | else | |
8910 | lang_add_data (BYTE, exp_intop (c)); | |
8911 | } | |
8912 | } | |
8913 | ||
8914 | /* Remeber to terminate the string. */ | |
8915 | lang_add_data (BYTE, exp_intop (0)); | |
8916 | } | |
8917 | ||
8918 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
8919 | generate. HOWTO is the corresponding howto structure (we could | |
8920 | look this up, but the caller has already done so). SECTION is the | |
8921 | section to generate a reloc against, or NAME is the name of the | |
8922 | symbol to generate a reloc against. Exactly one of SECTION and | |
8923 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8924 | ||
8925 | void | |
8926 | lang_add_reloc (bfd_reloc_code_real_type reloc, | |
8927 | reloc_howto_type *howto, | |
8928 | asection *section, | |
8929 | const char *name, | |
8930 | union etree_union *addend) | |
8931 | { | |
8932 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
8933 | ||
8934 | p->reloc = reloc; | |
8935 | p->howto = howto; | |
8936 | p->section = section; | |
8937 | p->name = name; | |
8938 | p->addend_exp = addend; | |
8939 | ||
8940 | p->addend_value = 0; | |
8941 | p->output_section = NULL; | |
8942 | p->output_offset = 0; | |
8943 | } | |
8944 | ||
8945 | lang_assignment_statement_type * | |
8946 | lang_add_assignment (etree_type *exp) | |
8947 | { | |
8948 | lang_assignment_statement_type *new_stmt; | |
8949 | ||
8950 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); | |
8951 | new_stmt->exp = exp; | |
8952 | return new_stmt; | |
8953 | } | |
8954 | ||
8955 | void | |
8956 | lang_add_attribute (enum statement_enum attribute) | |
8957 | { | |
8958 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); | |
8959 | } | |
8960 | ||
8961 | void | |
8962 | lang_startup (const char *name) | |
8963 | { | |
8964 | if (first_file->filename != NULL) | |
8965 | fatal (_("%P: multiple STARTUP files\n")); | |
8966 | first_file->filename = name; | |
8967 | first_file->local_sym_name = name; | |
8968 | first_file->flags.real = true; | |
8969 | } | |
8970 | ||
8971 | void | |
8972 | lang_float (bool maybe) | |
8973 | { | |
8974 | lang_float_flag = maybe; | |
8975 | } | |
8976 | ||
8977 | ||
8978 | /* Work out the load- and run-time regions from a script statement, and | |
8979 | store them in *LMA_REGION and *REGION respectively. | |
8980 | ||
8981 | MEMSPEC is the name of the run-time region, or the value of | |
8982 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8983 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8984 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8985 | had an explicit load address. | |
8986 | ||
8987 | It is an error to specify both a load region and a load address. */ | |
8988 | ||
8989 | static void | |
8990 | lang_get_regions (lang_memory_region_type **region, | |
8991 | lang_memory_region_type **lma_region, | |
8992 | const char *memspec, | |
8993 | const char *lma_memspec, | |
8994 | bool have_lma, | |
8995 | bool have_vma) | |
8996 | { | |
8997 | *lma_region = lang_memory_region_lookup (lma_memspec, false); | |
8998 | ||
8999 | /* If no runtime region or VMA has been specified, but the load region | |
9000 | has been specified, then use the load region for the runtime region | |
9001 | as well. */ | |
9002 | if (lma_memspec != NULL | |
9003 | && !have_vma | |
9004 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) | |
9005 | *region = *lma_region; | |
9006 | else | |
9007 | *region = lang_memory_region_lookup (memspec, false); | |
9008 | ||
9009 | if (have_lma && lma_memspec != 0) | |
9010 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), | |
9011 | NULL); | |
9012 | } | |
9013 | ||
9014 | void | |
9015 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, | |
9016 | lang_output_section_phdr_list *phdrs, | |
9017 | const char *lma_memspec) | |
9018 | { | |
9019 | pop_stat_ptr (); | |
9020 | if (in_section_ordering) | |
9021 | return; | |
9022 | ||
9023 | lang_get_regions (¤t_section->region, | |
9024 | ¤t_section->lma_region, | |
9025 | memspec, lma_memspec, | |
9026 | current_section->load_base != NULL, | |
9027 | current_section->addr_tree != NULL); | |
9028 | ||
9029 | current_section->fill = fill; | |
9030 | current_section->phdrs = phdrs; | |
9031 | } | |
9032 | ||
9033 | /* Set the output format type. -oformat overrides scripts. */ | |
9034 | ||
9035 | void | |
9036 | lang_add_output_format (const char *format, | |
9037 | const char *big, | |
9038 | const char *little, | |
9039 | int from_script) | |
9040 | { | |
9041 | if (output_target == NULL || !from_script) | |
9042 | { | |
9043 | if (command_line.endian == ENDIAN_BIG | |
9044 | && big != NULL) | |
9045 | format = big; | |
9046 | else if (command_line.endian == ENDIAN_LITTLE | |
9047 | && little != NULL) | |
9048 | format = little; | |
9049 | ||
9050 | output_target = format; | |
9051 | } | |
9052 | } | |
9053 | ||
9054 | void | |
9055 | lang_add_insert (const char *where, int is_before) | |
9056 | { | |
9057 | lang_insert_statement_type *new_stmt; | |
9058 | ||
9059 | new_stmt = new_stat (lang_insert_statement, stat_ptr); | |
9060 | new_stmt->where = where; | |
9061 | new_stmt->is_before = is_before; | |
9062 | saved_script_handle = previous_script_handle; | |
9063 | } | |
9064 | ||
9065 | /* Enter a group. This creates a new lang_group_statement, and sets | |
9066 | stat_ptr to build new statements within the group. */ | |
9067 | ||
9068 | void | |
9069 | lang_enter_group (void) | |
9070 | { | |
9071 | lang_group_statement_type *g; | |
9072 | ||
9073 | g = new_stat (lang_group_statement, stat_ptr); | |
9074 | lang_list_init (&g->children); | |
9075 | push_stat_ptr (&g->children); | |
9076 | } | |
9077 | ||
9078 | /* Leave a group. This just resets stat_ptr to start writing to the | |
9079 | regular list of statements again. Note that this will not work if | |
9080 | groups can occur inside anything else which can adjust stat_ptr, | |
9081 | but currently they can't. */ | |
9082 | ||
9083 | void | |
9084 | lang_leave_group (void) | |
9085 | { | |
9086 | pop_stat_ptr (); | |
9087 | } | |
9088 | ||
9089 | /* Add a new program header. This is called for each entry in a PHDRS | |
9090 | command in a linker script. */ | |
9091 | ||
9092 | void | |
9093 | lang_new_phdr (const char *name, | |
9094 | etree_type *type, | |
9095 | bool filehdr, | |
9096 | bool phdrs, | |
9097 | etree_type *at, | |
9098 | etree_type *flags) | |
9099 | { | |
9100 | struct lang_phdr *n, **pp; | |
9101 | bool hdrs; | |
9102 | ||
9103 | n = stat_alloc (sizeof (struct lang_phdr)); | |
9104 | n->next = NULL; | |
9105 | n->name = name; | |
9106 | n->type = exp_get_vma (type, NULL, 0, "program header type"); | |
9107 | n->filehdr = filehdr; | |
9108 | n->phdrs = phdrs; | |
9109 | n->at = at; | |
9110 | n->flags = flags; | |
9111 | ||
9112 | hdrs = n->type == 1 && (phdrs || filehdr); | |
9113 | ||
9114 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
9115 | if (hdrs | |
9116 | && (*pp)->type == 1 | |
9117 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
9118 | { | |
9119 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" | |
9120 | " when prior PT_LOAD headers lack them\n"), NULL); | |
9121 | hdrs = false; | |
9122 | } | |
9123 | ||
9124 | *pp = n; | |
9125 | } | |
9126 | ||
9127 | /* Record the program header information in the output BFD. FIXME: We | |
9128 | should not be calling an ELF specific function here. */ | |
9129 | ||
9130 | static void | |
9131 | lang_record_phdrs (void) | |
9132 | { | |
9133 | unsigned int alc; | |
9134 | asection **secs; | |
9135 | lang_output_section_phdr_list *last; | |
9136 | struct lang_phdr *l; | |
9137 | lang_output_section_statement_type *os; | |
9138 | ||
9139 | alc = 10; | |
9140 | secs = (asection **) xmalloc (alc * sizeof (asection *)); | |
9141 | last = NULL; | |
9142 | ||
9143 | for (l = lang_phdr_list; l != NULL; l = l->next) | |
9144 | { | |
9145 | unsigned int c; | |
9146 | flagword flags; | |
9147 | bfd_vma at; | |
9148 | ||
9149 | c = 0; | |
9150 | for (os = (void *) lang_os_list.head; | |
9151 | os != NULL; | |
9152 | os = os->next) | |
9153 | { | |
9154 | lang_output_section_phdr_list *pl; | |
9155 | ||
9156 | if (os->constraint < 0) | |
9157 | continue; | |
9158 | ||
9159 | pl = os->phdrs; | |
9160 | if (pl != NULL) | |
9161 | last = pl; | |
9162 | else | |
9163 | { | |
9164 | if (os->sectype == noload_section | |
9165 | || os->bfd_section == NULL | |
9166 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
9167 | continue; | |
9168 | ||
9169 | /* Don't add orphans to PT_INTERP header. */ | |
9170 | if (l->type == PT_INTERP) | |
9171 | continue; | |
9172 | ||
9173 | if (last == NULL) | |
9174 | { | |
9175 | lang_output_section_statement_type *tmp_os; | |
9176 | ||
9177 | /* If we have not run across a section with a program | |
9178 | header assigned to it yet, then scan forwards to find | |
9179 | one. This prevents inconsistencies in the linker's | |
9180 | behaviour when a script has specified just a single | |
9181 | header and there are sections in that script which are | |
9182 | not assigned to it, and which occur before the first | |
9183 | use of that header. See here for more details: | |
9184 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
9185 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
9186 | if (tmp_os->phdrs) | |
9187 | { | |
9188 | last = tmp_os->phdrs; | |
9189 | break; | |
9190 | } | |
9191 | if (last == NULL) | |
9192 | fatal (_("%P: no sections assigned to phdrs\n")); | |
9193 | } | |
9194 | pl = last; | |
9195 | } | |
9196 | ||
9197 | if (os->bfd_section == NULL) | |
9198 | continue; | |
9199 | ||
9200 | for (; pl != NULL; pl = pl->next) | |
9201 | { | |
9202 | if (strcmp (pl->name, l->name) == 0) | |
9203 | { | |
9204 | if (c >= alc) | |
9205 | { | |
9206 | alc *= 2; | |
9207 | secs = (asection **) xrealloc (secs, | |
9208 | alc * sizeof (asection *)); | |
9209 | } | |
9210 | secs[c] = os->bfd_section; | |
9211 | ++c; | |
9212 | pl->used = true; | |
9213 | } | |
9214 | } | |
9215 | } | |
9216 | ||
9217 | if (l->flags == NULL) | |
9218 | flags = 0; | |
9219 | else | |
9220 | flags = exp_get_vma (l->flags, NULL, 0, "phdr flags"); | |
9221 | ||
9222 | if (l->at == NULL) | |
9223 | at = 0; | |
9224 | else | |
9225 | at = exp_get_vma (l->at, NULL, 0, "phdr load address"); | |
9226 | ||
9227 | if (!bfd_record_phdr (link_info.output_bfd, l->type, | |
9228 | l->flags != NULL, flags, l->at != NULL, | |
9229 | at, l->filehdr, l->phdrs, c, secs)) | |
9230 | fatal (_("%P: bfd_record_phdr failed: %E\n")); | |
9231 | } | |
9232 | ||
9233 | free (secs); | |
9234 | ||
9235 | /* Make sure all the phdr assignments succeeded. */ | |
9236 | for (os = (void *) lang_os_list.head; | |
9237 | os != NULL; | |
9238 | os = os->next) | |
9239 | { | |
9240 | lang_output_section_phdr_list *pl; | |
9241 | ||
9242 | if (os->constraint < 0 | |
9243 | || os->bfd_section == NULL) | |
9244 | continue; | |
9245 | ||
9246 | for (pl = os->phdrs; | |
9247 | pl != NULL; | |
9248 | pl = pl->next) | |
9249 | if (!pl->used && strcmp (pl->name, "NONE") != 0) | |
9250 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), | |
9251 | os->name, pl->name); | |
9252 | } | |
9253 | } | |
9254 | ||
9255 | /* Record a list of sections which may not be cross referenced. */ | |
9256 | ||
9257 | void | |
9258 | lang_add_nocrossref (lang_nocrossref_type *l) | |
9259 | { | |
9260 | struct lang_nocrossrefs *n; | |
9261 | ||
9262 | n = stat_alloc (sizeof *n); | |
9263 | n->next = nocrossref_list; | |
9264 | n->list = l; | |
9265 | n->onlyfirst = false; | |
9266 | nocrossref_list = n; | |
9267 | ||
9268 | /* Set notice_all so that we get informed about all symbols. */ | |
9269 | link_info.notice_all = true; | |
9270 | } | |
9271 | ||
9272 | /* Record a section that cannot be referenced from a list of sections. */ | |
9273 | ||
9274 | void | |
9275 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
9276 | { | |
9277 | lang_add_nocrossref (l); | |
9278 | nocrossref_list->onlyfirst = true; | |
9279 | } | |
9280 | \f | |
9281 | /* Overlay handling. We handle overlays with some static variables. */ | |
9282 | ||
9283 | /* The overlay virtual address. */ | |
9284 | static etree_type *overlay_vma; | |
9285 | /* And subsection alignment. */ | |
9286 | static etree_type *overlay_subalign; | |
9287 | ||
9288 | /* An expression for the maximum section size seen so far. */ | |
9289 | static etree_type *overlay_max; | |
9290 | ||
9291 | /* A list of all the sections in this overlay. */ | |
9292 | ||
9293 | struct overlay_list { | |
9294 | struct overlay_list *next; | |
9295 | lang_output_section_statement_type *os; | |
9296 | }; | |
9297 | ||
9298 | static struct overlay_list *overlay_list; | |
9299 | ||
9300 | /* Start handling an overlay. */ | |
9301 | ||
9302 | void | |
9303 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) | |
9304 | { | |
9305 | /* The grammar should prevent nested overlays from occurring. */ | |
9306 | ASSERT (overlay_vma == NULL | |
9307 | && overlay_subalign == NULL | |
9308 | && overlay_max == NULL); | |
9309 | ||
9310 | overlay_vma = vma_expr; | |
9311 | overlay_subalign = subalign; | |
9312 | } | |
9313 | ||
9314 | /* Start a section in an overlay. We handle this by calling | |
9315 | lang_enter_output_section_statement with the correct VMA. | |
9316 | lang_leave_overlay sets up the LMA and memory regions. */ | |
9317 | ||
9318 | void | |
9319 | lang_enter_overlay_section (const char *name) | |
9320 | { | |
9321 | struct overlay_list *n; | |
9322 | etree_type *size; | |
9323 | ||
9324 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, | |
9325 | 0, 0, overlay_subalign, 0, 0, 0); | |
9326 | ||
9327 | /* If this is the first section, then base the VMA of future | |
9328 | sections on this one. This will work correctly even if `.' is | |
9329 | used in the addresses. */ | |
9330 | if (overlay_list == NULL) | |
9331 | overlay_vma = exp_nameop (ADDR, name); | |
9332 | ||
9333 | /* Remember the section. */ | |
9334 | n = (struct overlay_list *) xmalloc (sizeof *n); | |
9335 | n->os = current_section; | |
9336 | n->next = overlay_list; | |
9337 | overlay_list = n; | |
9338 | ||
9339 | size = exp_nameop (SIZEOF, name); | |
9340 | ||
9341 | /* Arrange to work out the maximum section end address. */ | |
9342 | if (overlay_max == NULL) | |
9343 | overlay_max = size; | |
9344 | else | |
9345 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
9346 | } | |
9347 | ||
9348 | /* Finish a section in an overlay. There isn't any special to do | |
9349 | here. */ | |
9350 | ||
9351 | void | |
9352 | lang_leave_overlay_section (fill_type *fill, | |
9353 | lang_output_section_phdr_list *phdrs) | |
9354 | { | |
9355 | const char *name = current_section->name;; | |
9356 | ||
9357 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory | |
9358 | region and that no load-time region has been specified. It doesn't | |
9359 | really matter what we say here, since lang_leave_overlay will | |
9360 | override it. */ | |
9361 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
9362 | ||
9363 | /* Define the magic symbols. */ | |
9364 | ||
9365 | char *clean = xmalloc (strlen (name) + 1); | |
9366 | char *s2 = clean; | |
9367 | for (const char *s1 = name; *s1 != '\0'; s1++) | |
9368 | if (ISALNUM (*s1) || *s1 == '_') | |
9369 | *s2++ = *s1; | |
9370 | *s2 = '\0'; | |
9371 | ||
9372 | char *buf = xasprintf ("__load_start_%s", clean); | |
9373 | lang_add_assignment (exp_provide (buf, | |
9374 | exp_nameop (LOADADDR, name), | |
9375 | false)); | |
9376 | ||
9377 | buf = xasprintf ("__load_stop_%s", clean); | |
9378 | lang_add_assignment (exp_provide (buf, | |
9379 | exp_binop ('+', | |
9380 | exp_nameop (LOADADDR, name), | |
9381 | exp_nameop (SIZEOF, name)), | |
9382 | false)); | |
9383 | ||
9384 | free (clean); | |
9385 | } | |
9386 | ||
9387 | /* Finish an overlay. If there are any overlay wide settings, this | |
9388 | looks through all the sections in the overlay and sets them. */ | |
9389 | ||
9390 | void | |
9391 | lang_leave_overlay (etree_type *lma_expr, | |
9392 | int nocrossrefs, | |
9393 | fill_type *fill, | |
9394 | const char *memspec, | |
9395 | lang_output_section_phdr_list *phdrs, | |
9396 | const char *lma_memspec) | |
9397 | { | |
9398 | lang_memory_region_type *region; | |
9399 | lang_memory_region_type *lma_region; | |
9400 | struct overlay_list *l; | |
9401 | lang_nocrossref_type *nocrossref; | |
9402 | ||
9403 | lang_get_regions (®ion, &lma_region, | |
9404 | memspec, lma_memspec, | |
9405 | lma_expr != NULL, false); | |
9406 | ||
9407 | nocrossref = NULL; | |
9408 | ||
9409 | /* After setting the size of the last section, set '.' to end of the | |
9410 | overlay region. */ | |
9411 | if (overlay_list != NULL) | |
9412 | { | |
9413 | overlay_list->os->update_dot = 1; | |
9414 | overlay_list->os->update_dot_tree | |
9415 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); | |
9416 | } | |
9417 | ||
9418 | l = overlay_list; | |
9419 | while (l != NULL) | |
9420 | { | |
9421 | struct overlay_list *next; | |
9422 | ||
9423 | if (fill != NULL && l->os->fill == NULL) | |
9424 | l->os->fill = fill; | |
9425 | ||
9426 | l->os->region = region; | |
9427 | l->os->lma_region = lma_region; | |
9428 | ||
9429 | /* The first section has the load address specified in the | |
9430 | OVERLAY statement. The rest are worked out from that. | |
9431 | The base address is not needed (and should be null) if | |
9432 | an LMA region was specified. */ | |
9433 | if (l->next == 0) | |
9434 | { | |
9435 | l->os->load_base = lma_expr; | |
9436 | l->os->sectype = first_overlay_section; | |
9437 | } | |
9438 | if (phdrs != NULL && l->os->phdrs == NULL) | |
9439 | l->os->phdrs = phdrs; | |
9440 | ||
9441 | if (nocrossrefs) | |
9442 | { | |
9443 | lang_nocrossref_type *nc; | |
9444 | ||
9445 | nc = stat_alloc (sizeof *nc); | |
9446 | nc->name = l->os->name; | |
9447 | nc->next = nocrossref; | |
9448 | nocrossref = nc; | |
9449 | } | |
9450 | ||
9451 | next = l->next; | |
9452 | free (l); | |
9453 | l = next; | |
9454 | } | |
9455 | ||
9456 | if (nocrossref != NULL) | |
9457 | lang_add_nocrossref (nocrossref); | |
9458 | ||
9459 | overlay_vma = NULL; | |
9460 | overlay_list = NULL; | |
9461 | overlay_max = NULL; | |
9462 | overlay_subalign = NULL; | |
9463 | } | |
9464 | \f | |
9465 | /* Version handling. This is only useful for ELF. */ | |
9466 | ||
9467 | /* If PREV is NULL, return first version pattern matching particular symbol. | |
9468 | If PREV is non-NULL, return first version pattern matching particular | |
9469 | symbol after PREV (previously returned by lang_vers_match). */ | |
9470 | ||
9471 | static struct bfd_elf_version_expr * | |
9472 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
9473 | struct bfd_elf_version_expr *prev, | |
9474 | const char *sym) | |
9475 | { | |
9476 | const char *c_sym; | |
9477 | const char *cxx_sym = sym; | |
9478 | const char *java_sym = sym; | |
9479 | struct bfd_elf_version_expr *expr = NULL; | |
9480 | enum demangling_styles curr_style; | |
9481 | ||
9482 | curr_style = CURRENT_DEMANGLING_STYLE; | |
9483 | cplus_demangle_set_style (no_demangling); | |
9484 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
9485 | if (!c_sym) | |
9486 | c_sym = sym; | |
9487 | cplus_demangle_set_style (curr_style); | |
9488 | ||
9489 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
9490 | { | |
9491 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, | |
9492 | DMGL_PARAMS | DMGL_ANSI); | |
9493 | if (!cxx_sym) | |
9494 | cxx_sym = sym; | |
9495 | } | |
9496 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
9497 | { | |
9498 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); | |
9499 | if (!java_sym) | |
9500 | java_sym = sym; | |
9501 | } | |
9502 | ||
9503 | if (head->htab && (prev == NULL || prev->literal)) | |
9504 | { | |
9505 | struct bfd_elf_version_expr e; | |
9506 | ||
9507 | switch (prev ? prev->mask : 0) | |
9508 | { | |
9509 | case 0: | |
9510 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
9511 | { | |
9512 | e.pattern = c_sym; | |
9513 | expr = (struct bfd_elf_version_expr *) | |
9514 | htab_find ((htab_t) head->htab, &e); | |
9515 | while (expr && strcmp (expr->pattern, c_sym) == 0) | |
9516 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) | |
9517 | goto out_ret; | |
9518 | else | |
9519 | expr = expr->next; | |
9520 | } | |
9521 | /* Fallthrough */ | |
9522 | case BFD_ELF_VERSION_C_TYPE: | |
9523 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
9524 | { | |
9525 | e.pattern = cxx_sym; | |
9526 | expr = (struct bfd_elf_version_expr *) | |
9527 | htab_find ((htab_t) head->htab, &e); | |
9528 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) | |
9529 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
9530 | goto out_ret; | |
9531 | else | |
9532 | expr = expr->next; | |
9533 | } | |
9534 | /* Fallthrough */ | |
9535 | case BFD_ELF_VERSION_CXX_TYPE: | |
9536 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
9537 | { | |
9538 | e.pattern = java_sym; | |
9539 | expr = (struct bfd_elf_version_expr *) | |
9540 | htab_find ((htab_t) head->htab, &e); | |
9541 | while (expr && strcmp (expr->pattern, java_sym) == 0) | |
9542 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
9543 | goto out_ret; | |
9544 | else | |
9545 | expr = expr->next; | |
9546 | } | |
9547 | /* Fallthrough */ | |
9548 | default: | |
9549 | break; | |
9550 | } | |
9551 | } | |
9552 | ||
9553 | /* Finally, try the wildcards. */ | |
9554 | if (prev == NULL || prev->literal) | |
9555 | expr = head->remaining; | |
9556 | else | |
9557 | expr = prev->next; | |
9558 | for (; expr; expr = expr->next) | |
9559 | { | |
9560 | const char *s; | |
9561 | ||
9562 | if (!expr->pattern) | |
9563 | continue; | |
9564 | ||
9565 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') | |
9566 | break; | |
9567 | ||
9568 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
9569 | s = java_sym; | |
9570 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
9571 | s = cxx_sym; | |
9572 | else | |
9573 | s = c_sym; | |
9574 | if (fnmatch (expr->pattern, s, 0) == 0) | |
9575 | break; | |
9576 | } | |
9577 | ||
9578 | out_ret: | |
9579 | if (c_sym != sym) | |
9580 | free ((char *) c_sym); | |
9581 | if (cxx_sym != sym) | |
9582 | free ((char *) cxx_sym); | |
9583 | if (java_sym != sym) | |
9584 | free ((char *) java_sym); | |
9585 | return expr; | |
9586 | } | |
9587 | ||
9588 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, | |
9589 | return a pointer to the symbol name with any backslash quotes removed. */ | |
9590 | ||
9591 | static const char * | |
9592 | realsymbol (const char *pattern) | |
9593 | { | |
9594 | const char *p; | |
9595 | bool changed = false, backslash = false; | |
9596 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); | |
9597 | ||
9598 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
9599 | { | |
9600 | /* It is a glob pattern only if there is no preceding | |
9601 | backslash. */ | |
9602 | if (backslash) | |
9603 | { | |
9604 | /* Remove the preceding backslash. */ | |
9605 | *(s - 1) = *p; | |
9606 | backslash = false; | |
9607 | changed = true; | |
9608 | } | |
9609 | else | |
9610 | { | |
9611 | if (*p == '?' || *p == '*' || *p == '[') | |
9612 | { | |
9613 | free (symbol); | |
9614 | return NULL; | |
9615 | } | |
9616 | ||
9617 | *s++ = *p; | |
9618 | backslash = *p == '\\'; | |
9619 | } | |
9620 | } | |
9621 | ||
9622 | if (changed) | |
9623 | { | |
9624 | *s = '\0'; | |
9625 | pattern = stat_strdup (symbol); | |
9626 | } | |
9627 | free (symbol); | |
9628 | return pattern; | |
9629 | } | |
9630 | ||
9631 | /* This is called for each variable name or match expression. NEW_NAME is | |
9632 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob | |
9633 | pattern to be matched against symbol names. */ | |
9634 | ||
9635 | struct bfd_elf_version_expr * | |
9636 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, | |
9637 | const char *new_name, | |
9638 | const char *lang, | |
9639 | bool literal_p) | |
9640 | { | |
9641 | struct bfd_elf_version_expr *ret; | |
9642 | ||
9643 | ret = stat_alloc (sizeof *ret); | |
9644 | ret->next = orig; | |
9645 | ret->symver = 0; | |
9646 | ret->script = 0; | |
9647 | ret->literal = true; | |
9648 | ret->pattern = literal_p ? new_name : realsymbol (new_name); | |
9649 | if (ret->pattern == NULL) | |
9650 | { | |
9651 | ret->pattern = new_name; | |
9652 | ret->literal = false; | |
9653 | } | |
9654 | ||
9655 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
9656 | ret->mask = BFD_ELF_VERSION_C_TYPE; | |
9657 | else if (strcasecmp (lang, "C++") == 0) | |
9658 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; | |
9659 | else if (strcasecmp (lang, "Java") == 0) | |
9660 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; | |
9661 | else | |
9662 | { | |
9663 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
9664 | lang); | |
9665 | ret->mask = BFD_ELF_VERSION_C_TYPE; | |
9666 | } | |
9667 | ||
9668 | return ldemul_new_vers_pattern (ret); | |
9669 | } | |
9670 | ||
9671 | /* This is called for each set of variable names and match | |
9672 | expressions. */ | |
9673 | ||
9674 | struct bfd_elf_version_tree * | |
9675 | lang_new_vers_node (struct bfd_elf_version_expr *globals, | |
9676 | struct bfd_elf_version_expr *locals) | |
9677 | { | |
9678 | struct bfd_elf_version_tree *ret; | |
9679 | ||
9680 | ret = stat_alloc (sizeof (*ret)); | |
9681 | memset (ret, 0, sizeof (*ret)); | |
9682 | ret->globals.list = globals; | |
9683 | ret->locals.list = locals; | |
9684 | ret->match = lang_vers_match; | |
9685 | ret->name_indx = (unsigned int) -1; | |
9686 | return ret; | |
9687 | } | |
9688 | ||
9689 | /* This static variable keeps track of version indices. */ | |
9690 | ||
9691 | static int version_index; | |
9692 | ||
9693 | static hashval_t | |
9694 | version_expr_head_hash (const void *p) | |
9695 | { | |
9696 | const struct bfd_elf_version_expr *e = | |
9697 | (const struct bfd_elf_version_expr *) p; | |
9698 | ||
9699 | return htab_hash_string (e->pattern); | |
9700 | } | |
9701 | ||
9702 | static int | |
9703 | version_expr_head_eq (const void *p1, const void *p2) | |
9704 | { | |
9705 | const struct bfd_elf_version_expr *e1 = | |
9706 | (const struct bfd_elf_version_expr *) p1; | |
9707 | const struct bfd_elf_version_expr *e2 = | |
9708 | (const struct bfd_elf_version_expr *) p2; | |
9709 | ||
9710 | return strcmp (e1->pattern, e2->pattern) == 0; | |
9711 | } | |
9712 | ||
9713 | static void | |
9714 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
9715 | { | |
9716 | size_t count = 0; | |
9717 | struct bfd_elf_version_expr *e, *next; | |
9718 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
9719 | ||
9720 | for (e = head->list; e; e = e->next) | |
9721 | { | |
9722 | if (e->literal) | |
9723 | count++; | |
9724 | head->mask |= e->mask; | |
9725 | } | |
9726 | ||
9727 | if (count) | |
9728 | { | |
9729 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
9730 | version_expr_head_eq, NULL); | |
9731 | list_loc = &head->list; | |
9732 | remaining_loc = &head->remaining; | |
9733 | for (e = head->list; e; e = next) | |
9734 | { | |
9735 | next = e->next; | |
9736 | if (!e->literal) | |
9737 | { | |
9738 | *remaining_loc = e; | |
9739 | remaining_loc = &e->next; | |
9740 | } | |
9741 | else | |
9742 | { | |
9743 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); | |
9744 | ||
9745 | if (*loc) | |
9746 | { | |
9747 | struct bfd_elf_version_expr *e1, *last; | |
9748 | ||
9749 | e1 = (struct bfd_elf_version_expr *) *loc; | |
9750 | last = NULL; | |
9751 | do | |
9752 | { | |
9753 | if (e1->mask == e->mask) | |
9754 | { | |
9755 | last = NULL; | |
9756 | break; | |
9757 | } | |
9758 | last = e1; | |
9759 | e1 = e1->next; | |
9760 | } | |
9761 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); | |
9762 | ||
9763 | if (last != NULL) | |
9764 | { | |
9765 | e->next = last->next; | |
9766 | last->next = e; | |
9767 | } | |
9768 | } | |
9769 | else | |
9770 | { | |
9771 | *loc = e; | |
9772 | *list_loc = e; | |
9773 | list_loc = &e->next; | |
9774 | } | |
9775 | } | |
9776 | } | |
9777 | *remaining_loc = NULL; | |
9778 | *list_loc = head->remaining; | |
9779 | } | |
9780 | else | |
9781 | head->remaining = head->list; | |
9782 | } | |
9783 | ||
9784 | /* This is called when we know the name and dependencies of the | |
9785 | version. */ | |
9786 | ||
9787 | void | |
9788 | lang_register_vers_node (const char *name, | |
9789 | struct bfd_elf_version_tree *version, | |
9790 | struct bfd_elf_version_deps *deps) | |
9791 | { | |
9792 | struct bfd_elf_version_tree *t, **pp; | |
9793 | struct bfd_elf_version_expr *e1; | |
9794 | ||
9795 | if (name == NULL) | |
9796 | name = ""; | |
9797 | ||
9798 | if (link_info.version_info != NULL | |
9799 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
9800 | { | |
9801 | einfo (_("%X%P: anonymous version tag cannot be combined" | |
9802 | " with other version tags\n")); | |
9803 | return; | |
9804 | } | |
9805 | ||
9806 | /* Make sure this node has a unique name. */ | |
9807 | for (t = link_info.version_info; t != NULL; t = t->next) | |
9808 | if (strcmp (t->name, name) == 0) | |
9809 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9810 | ||
9811 | lang_finalize_version_expr_head (&version->globals); | |
9812 | lang_finalize_version_expr_head (&version->locals); | |
9813 | ||
9814 | /* Check the global and local match names, and make sure there | |
9815 | aren't any duplicates. */ | |
9816 | ||
9817 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) | |
9818 | { | |
9819 | for (t = link_info.version_info; t != NULL; t = t->next) | |
9820 | { | |
9821 | struct bfd_elf_version_expr *e2; | |
9822 | ||
9823 | if (t->locals.htab && e1->literal) | |
9824 | { | |
9825 | e2 = (struct bfd_elf_version_expr *) | |
9826 | htab_find ((htab_t) t->locals.htab, e1); | |
9827 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) | |
9828 | { | |
9829 | if (e1->mask == e2->mask) | |
9830 | einfo (_("%X%P: duplicate expression `%s'" | |
9831 | " in version information\n"), e1->pattern); | |
9832 | e2 = e2->next; | |
9833 | } | |
9834 | } | |
9835 | else if (!e1->literal) | |
9836 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) | |
9837 | if (strcmp (e1->pattern, e2->pattern) == 0 | |
9838 | && e1->mask == e2->mask) | |
9839 | einfo (_("%X%P: duplicate expression `%s'" | |
9840 | " in version information\n"), e1->pattern); | |
9841 | } | |
9842 | } | |
9843 | ||
9844 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) | |
9845 | { | |
9846 | for (t = link_info.version_info; t != NULL; t = t->next) | |
9847 | { | |
9848 | struct bfd_elf_version_expr *e2; | |
9849 | ||
9850 | if (t->globals.htab && e1->literal) | |
9851 | { | |
9852 | e2 = (struct bfd_elf_version_expr *) | |
9853 | htab_find ((htab_t) t->globals.htab, e1); | |
9854 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) | |
9855 | { | |
9856 | if (e1->mask == e2->mask) | |
9857 | einfo (_("%X%P: duplicate expression `%s'" | |
9858 | " in version information\n"), | |
9859 | e1->pattern); | |
9860 | e2 = e2->next; | |
9861 | } | |
9862 | } | |
9863 | else if (!e1->literal) | |
9864 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) | |
9865 | if (strcmp (e1->pattern, e2->pattern) == 0 | |
9866 | && e1->mask == e2->mask) | |
9867 | einfo (_("%X%P: duplicate expression `%s'" | |
9868 | " in version information\n"), e1->pattern); | |
9869 | } | |
9870 | } | |
9871 | ||
9872 | version->deps = deps; | |
9873 | version->name = name; | |
9874 | if (name[0] != '\0') | |
9875 | { | |
9876 | ++version_index; | |
9877 | version->vernum = version_index; | |
9878 | } | |
9879 | else | |
9880 | version->vernum = 0; | |
9881 | ||
9882 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) | |
9883 | ; | |
9884 | *pp = version; | |
9885 | } | |
9886 | ||
9887 | /* This is called when we see a version dependency. */ | |
9888 | ||
9889 | struct bfd_elf_version_deps * | |
9890 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) | |
9891 | { | |
9892 | struct bfd_elf_version_deps *ret; | |
9893 | struct bfd_elf_version_tree *t; | |
9894 | ||
9895 | ret = stat_alloc (sizeof *ret); | |
9896 | ret->next = list; | |
9897 | ||
9898 | for (t = link_info.version_info; t != NULL; t = t->next) | |
9899 | { | |
9900 | if (strcmp (t->name, name) == 0) | |
9901 | { | |
9902 | ret->version_needed = t; | |
9903 | return ret; | |
9904 | } | |
9905 | } | |
9906 | ||
9907 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9908 | ||
9909 | ret->version_needed = NULL; | |
9910 | return ret; | |
9911 | } | |
9912 | ||
9913 | static void | |
9914 | lang_do_version_exports_section (void) | |
9915 | { | |
9916 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9917 | ||
9918 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9919 | { | |
9920 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9921 | char *contents, *p; | |
9922 | bfd_size_type len; | |
9923 | ||
9924 | if (sec == NULL) | |
9925 | continue; | |
9926 | ||
9927 | len = sec->size; | |
9928 | contents = stat_alloc (len); | |
9929 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) | |
9930 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); | |
9931 | ||
9932 | p = contents; | |
9933 | while (p < contents + len) | |
9934 | { | |
9935 | greg = lang_new_vers_pattern (greg, p, NULL, false); | |
9936 | p = strchr (p, '\0') + 1; | |
9937 | } | |
9938 | ||
9939 | /* Do not include this section in the link. */ | |
9940 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; | |
9941 | } | |
9942 | ||
9943 | lreg = lang_new_vers_pattern (NULL, "*", NULL, false); | |
9944 | lang_register_vers_node (command_line.version_exports_section, | |
9945 | lang_new_vers_node (greg, lreg), NULL); | |
9946 | } | |
9947 | ||
9948 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially | |
9949 | called with UPDATE_REGIONS_P set to FALSE, in this case no errors are | |
9950 | thrown, however, references to symbols in the origin and length fields | |
9951 | will be pushed into the symbol table, this allows PROVIDE statements to | |
9952 | then provide these symbols. This function is called a second time with | |
9953 | UPDATE_REGIONS_P set to TRUE, this time the we update the actual region | |
9954 | data structures, and throw errors if missing symbols are encountered. */ | |
9955 | ||
9956 | static void | |
9957 | lang_do_memory_regions (bool update_regions_p) | |
9958 | { | |
9959 | lang_memory_region_type *r = lang_memory_region_list; | |
9960 | ||
9961 | for (; r != NULL; r = r->next) | |
9962 | { | |
9963 | if (r->origin_exp) | |
9964 | { | |
9965 | exp_fold_tree_no_dot (r->origin_exp, NULL); | |
9966 | if (update_regions_p) | |
9967 | { | |
9968 | if (expld.result.valid_p) | |
9969 | { | |
9970 | r->origin = expld.result.value; | |
9971 | r->current = r->origin; | |
9972 | } | |
9973 | else | |
9974 | einfo (_("%P: invalid origin for memory region %s\n"), | |
9975 | r->name_list.name); | |
9976 | } | |
9977 | } | |
9978 | if (r->length_exp) | |
9979 | { | |
9980 | exp_fold_tree_no_dot (r->length_exp, NULL); | |
9981 | if (update_regions_p) | |
9982 | { | |
9983 | if (expld.result.valid_p) | |
9984 | r->length = expld.result.value; | |
9985 | else | |
9986 | einfo (_("%P: invalid length for memory region %s\n"), | |
9987 | r->name_list.name); | |
9988 | } | |
9989 | } | |
9990 | } | |
9991 | } | |
9992 | ||
9993 | void | |
9994 | lang_add_unique (const char *name) | |
9995 | { | |
9996 | struct unique_sections *ent; | |
9997 | ||
9998 | for (ent = unique_section_list; ent; ent = ent->next) | |
9999 | if (strcmp (ent->name, name) == 0) | |
10000 | return; | |
10001 | ||
10002 | ent = stat_alloc (sizeof *ent); | |
10003 | ent->name = stat_strdup (name); | |
10004 | ent->next = unique_section_list; | |
10005 | unique_section_list = ent; | |
10006 | } | |
10007 | ||
10008 | /* Append the list of dynamic symbols to the existing one. */ | |
10009 | ||
10010 | void | |
10011 | lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, | |
10012 | struct bfd_elf_version_expr *dynamic) | |
10013 | { | |
10014 | if (*list_p) | |
10015 | { | |
10016 | struct bfd_elf_version_expr *tail; | |
10017 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
10018 | ; | |
10019 | tail->next = (*list_p)->head.list; | |
10020 | (*list_p)->head.list = dynamic; | |
10021 | } | |
10022 | else | |
10023 | { | |
10024 | struct bfd_elf_dynamic_list *d; | |
10025 | ||
10026 | d = stat_alloc (sizeof (*d)); | |
10027 | memset (d, 0, sizeof (*d)); | |
10028 | d->head.list = dynamic; | |
10029 | d->match = lang_vers_match; | |
10030 | *list_p = d; | |
10031 | } | |
10032 | } | |
10033 | ||
10034 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
10035 | one. */ | |
10036 | ||
10037 | void | |
10038 | lang_append_dynamic_list_cpp_typeinfo (void) | |
10039 | { | |
10040 | const char *symbols[] = | |
10041 | { | |
10042 | "typeinfo name for*", | |
10043 | "typeinfo for*" | |
10044 | }; | |
10045 | struct bfd_elf_version_expr *dynamic = NULL; | |
10046 | unsigned int i; | |
10047 | ||
10048 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
10049 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
10050 | false); | |
10051 | ||
10052 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); | |
10053 | } | |
10054 | ||
10055 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
10056 | existing one. */ | |
10057 | ||
10058 | void | |
10059 | lang_append_dynamic_list_cpp_new (void) | |
10060 | { | |
10061 | const char *symbols[] = | |
10062 | { | |
10063 | "operator new*", | |
10064 | "operator delete*" | |
10065 | }; | |
10066 | struct bfd_elf_version_expr *dynamic = NULL; | |
10067 | unsigned int i; | |
10068 | ||
10069 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
10070 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
10071 | false); | |
10072 | ||
10073 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); | |
10074 | } | |
10075 | ||
10076 | /* Scan a space and/or comma separated string of features. */ | |
10077 | ||
10078 | void | |
10079 | lang_ld_feature (char *str) | |
10080 | { | |
10081 | char *p, *q; | |
10082 | ||
10083 | p = str; | |
10084 | while (*p) | |
10085 | { | |
10086 | char sep; | |
10087 | while (*p == ',' || ISSPACE (*p)) | |
10088 | ++p; | |
10089 | if (!*p) | |
10090 | break; | |
10091 | q = p + 1; | |
10092 | while (*q && *q != ',' && !ISSPACE (*q)) | |
10093 | ++q; | |
10094 | sep = *q; | |
10095 | *q = 0; | |
10096 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
10097 | config.sane_expr = true; | |
10098 | else | |
10099 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
10100 | *q = sep; | |
10101 | p = q; | |
10102 | } | |
10103 | } | |
10104 | ||
10105 | /* Pretty print memory amount. */ | |
10106 | ||
10107 | static void | |
10108 | lang_print_memory_size (uint64_t sz) | |
10109 | { | |
10110 | if (sz == 0) | |
10111 | printf (" %10" PRIu64 " B", sz); | |
10112 | else if ((sz & 0x3fffffff) == 0) | |
10113 | printf ("%10" PRIu64 " GB", sz >> 30); | |
10114 | else if ((sz & 0xfffff) == 0) | |
10115 | printf ("%10" PRIu64 " MB", sz >> 20); | |
10116 | else if ((sz & 0x3ff) == 0) | |
10117 | printf ("%10" PRIu64 " KB", sz >> 10); | |
10118 | else | |
10119 | printf (" %10" PRIu64 " B", sz); | |
10120 | } | |
10121 | ||
10122 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
10123 | ||
10124 | void | |
10125 | lang_print_memory_usage (void) | |
10126 | { | |
10127 | lang_memory_region_type *r; | |
10128 | ||
10129 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
10130 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
10131 | { | |
10132 | bfd_vma used_length = r->current - r->origin; | |
10133 | ||
10134 | printf ("%16s: ",r->name_list.name); | |
10135 | lang_print_memory_size (used_length); | |
10136 | lang_print_memory_size (r->length); | |
10137 | ||
10138 | if (r->length != 0) | |
10139 | { | |
10140 | double percent = used_length * 100.0 / r->length; | |
10141 | printf (" %6.2f%%", percent); | |
10142 | } | |
10143 | printf ("\n"); | |
10144 | } | |
10145 | } | |
10146 | ||
10147 | static void | |
10148 | cmdline_lists_init (void) | |
10149 | { | |
10150 | cmdline_object_only_file_list.tail | |
10151 | = &cmdline_object_only_file_list.head; | |
10152 | cmdline_object_only_archive_list.tail | |
10153 | = &cmdline_object_only_archive_list.head; | |
10154 | cmdline_temp_object_only_list.tail | |
10155 | = &cmdline_temp_object_only_list.head; | |
10156 | } | |
10157 | ||
10158 | /* Allocate an item with TYPE and DATA. */ | |
10159 | ||
10160 | static cmdline_union_type * | |
10161 | cmdline_list_new (cmdline_enum_type type, void *data) | |
10162 | { | |
10163 | cmdline_union_type *new_opt; | |
10164 | ||
10165 | new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt)); | |
10166 | new_opt->header.type = type; | |
10167 | switch (type) | |
10168 | { | |
10169 | default: | |
10170 | break; | |
10171 | case cmdline_is_file_enum: | |
10172 | new_opt->file.filename = (const char *) data; | |
10173 | break; | |
10174 | case cmdline_is_bfd_enum: | |
10175 | new_opt->abfd.abfd = (bfd *) data; | |
10176 | break; | |
10177 | } | |
10178 | return new_opt; | |
10179 | } | |
10180 | ||
10181 | /* Append an item with TYPE and DATA to LIST. */ | |
10182 | ||
10183 | static void | |
10184 | cmdline_list_append (cmdline_list_type *list, cmdline_enum_type type, | |
10185 | void *data) | |
10186 | { | |
10187 | cmdline_union_type *new_opt = cmdline_list_new (type, data); | |
10188 | new_opt->header.next = NULL; | |
10189 | *list->tail = new_opt; | |
10190 | list->tail = &new_opt->header.next; | |
10191 | } | |
10192 | ||
10193 | static void | |
10194 | print_cmdline_list (cmdline_union_type *c) | |
10195 | { | |
10196 | for (; c != NULL; c = c->header.next) | |
10197 | switch (c->header.type) | |
10198 | { | |
10199 | default: | |
10200 | abort (); | |
10201 | case cmdline_is_file_enum: | |
10202 | info_msg (" %s", c->file.filename); | |
10203 | break; | |
10204 | case cmdline_is_bfd_enum: | |
10205 | info_msg (" [%B]", c->abfd.abfd); | |
10206 | break; | |
10207 | } | |
10208 | ||
10209 | info_msg ("\n"); | |
10210 | } | |
10211 | ||
10212 | /* Return TRUE if ABFD is on cmdline_object_only_archive_list. */ | |
10213 | ||
10214 | static bool | |
10215 | cmdline_on_object_only_archive_list_p (bfd *abfd) | |
10216 | { | |
10217 | cmdline_union_type *c, *next; | |
10218 | bfd *archive, *obfd, *oarchive; | |
10219 | ufile_ptr origin = abfd->origin; | |
10220 | ||
10221 | archive = abfd->my_archive; | |
10222 | for (c = cmdline_object_only_archive_list.head; c != NULL; c = next) | |
10223 | { | |
10224 | if (c->header.type != cmdline_is_bfd_enum) | |
10225 | abort (); | |
10226 | ||
10227 | next = c->header.next; | |
10228 | obfd = c->abfd.abfd; | |
10229 | oarchive = obfd->my_archive; | |
10230 | ||
10231 | /* The list is grouped by archive file name and sorted by member | |
10232 | origin. */ | |
10233 | if (strcmp (archive->filename, oarchive->filename) != 0) | |
10234 | continue; | |
10235 | ||
10236 | if (origin == obfd->origin) | |
10237 | return true; | |
10238 | else if (origin < obfd->origin) | |
10239 | return false; | |
10240 | } | |
10241 | ||
10242 | return false; | |
10243 | } | |
10244 | ||
10245 | /* Append an item with TYPE and DATA to cmdline_object_only_file_list | |
10246 | or cmdline_object_only_archive_list if needed. */ | |
10247 | ||
10248 | static void | |
10249 | cmdline_object_only_list_append (cmdline_enum_type type, void *data) | |
10250 | { | |
10251 | cmdline_union_type *c; | |
10252 | cmdline_union_type *new_opt, *next, **prev; | |
10253 | bfd *abfd, *archive; | |
10254 | bfd *obfd, *oarchive; | |
10255 | bfd *nbfd, *narchive; | |
10256 | ufile_ptr origin, norigin; | |
10257 | ||
10258 | /* Put it on cmdline_object_only_file_list if it isn't an archive | |
10259 | member. */ | |
10260 | switch (type) | |
10261 | { | |
10262 | default: | |
10263 | abort (); | |
10264 | case cmdline_is_bfd_enum: | |
10265 | abfd = (bfd *) data; | |
10266 | archive = abfd->my_archive; | |
10267 | if (archive) | |
10268 | break; | |
10269 | /* Fallthru */ | |
10270 | case cmdline_is_file_enum: | |
10271 | cmdline_list_append (&cmdline_object_only_file_list, type, data); | |
10272 | return; | |
10273 | } | |
10274 | ||
10275 | /* Put archive member on cmdline_object_only_archive_list and sort | |
10276 | the list by archive name and archive member origin. */ | |
10277 | new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt)); | |
10278 | new_opt->header.type = cmdline_is_bfd_enum; | |
10279 | new_opt->header.next = NULL; | |
10280 | new_opt->abfd.abfd = (bfd *) data; | |
10281 | ||
10282 | c = cmdline_object_only_archive_list.head; | |
10283 | if (c == NULL) | |
10284 | { | |
10285 | cmdline_object_only_archive_list.head = new_opt; | |
10286 | cmdline_object_only_archive_list.tail = &new_opt->header.next; | |
10287 | return; | |
10288 | } | |
10289 | ||
10290 | prev = NULL; | |
10291 | origin = abfd->origin; | |
10292 | for (; c != NULL; c = next) | |
10293 | { | |
10294 | if (c->header.type != cmdline_is_bfd_enum) | |
10295 | abort (); | |
10296 | ||
10297 | next = c->header.next; | |
10298 | ||
10299 | obfd = c->abfd.abfd; | |
10300 | oarchive = obfd->my_archive; | |
10301 | ||
10302 | if (strcmp (archive->filename, oarchive->filename) == 0) | |
10303 | { | |
10304 | bool after; | |
10305 | ||
10306 | if (origin < obfd->origin) | |
10307 | { | |
10308 | /* Insert it before the current. */ | |
10309 | new_opt->header.next = c; | |
10310 | if (prev) | |
10311 | *prev = new_opt; | |
10312 | else | |
10313 | cmdline_object_only_archive_list.head = new_opt; | |
10314 | return; | |
10315 | } | |
10316 | ||
10317 | after = true; | |
10318 | ||
10319 | /* Check origin. */ | |
10320 | while (next) | |
10321 | { | |
10322 | if (next->header.type != cmdline_is_bfd_enum) | |
10323 | abort (); | |
10324 | ||
10325 | nbfd = next->abfd.abfd; | |
10326 | norigin = nbfd->origin; | |
10327 | if (origin > norigin) | |
10328 | { | |
10329 | /* Insert it after NEXT. */ | |
10330 | break; | |
10331 | } | |
10332 | ||
10333 | narchive = nbfd->my_archive; | |
10334 | if (strcmp (archive->filename, narchive->filename) != 0) | |
10335 | { | |
10336 | /* Insert it befor NEXT. */ | |
10337 | after = false; | |
10338 | break; | |
10339 | } | |
10340 | ||
10341 | c = next; | |
10342 | next = next->header.next; | |
10343 | } | |
10344 | ||
10345 | if (after && next) | |
10346 | { | |
10347 | c = next; | |
10348 | next = next->header.next; | |
10349 | } | |
10350 | ||
10351 | if (*cmdline_object_only_archive_list.tail == c->header.next) | |
10352 | cmdline_object_only_archive_list.tail | |
10353 | = &new_opt->header.next; | |
10354 | ||
10355 | prev = &c->header.next; | |
10356 | new_opt->header.next = next; | |
10357 | *prev = new_opt; | |
10358 | return; | |
10359 | } | |
10360 | ||
10361 | prev = &c->header.next; | |
10362 | } | |
10363 | ||
10364 | *cmdline_object_only_archive_list.tail = new_opt; | |
10365 | cmdline_object_only_archive_list.tail = &new_opt->header.next; | |
10366 | } | |
10367 | ||
10368 | /* Get object-only input files. */ | |
10369 | ||
10370 | static void | |
10371 | cmdline_get_object_only_input_files (void) | |
10372 | { | |
10373 | cmdline_union_type *c, *next; | |
10374 | bfd *abfd, *archive; | |
10375 | bfd *nbfd, *narchive; | |
10376 | ||
10377 | /* Add files first. */ | |
10378 | for (c = cmdline_object_only_file_list.head; | |
10379 | c != NULL; c = c->header.next) | |
10380 | switch (c->header.type) | |
10381 | { | |
10382 | default: | |
10383 | abort (); | |
10384 | case cmdline_is_file_enum: | |
10385 | lang_add_input_file (c->file.filename, | |
10386 | lang_input_file_is_file_enum, NULL); | |
10387 | break; | |
10388 | case cmdline_is_bfd_enum: | |
10389 | abfd = c->abfd.abfd; | |
10390 | if (abfd->my_archive) | |
10391 | abort (); | |
10392 | lang_add_input_file (abfd->filename, | |
10393 | lang_input_file_is_file_enum, NULL); | |
10394 | break; | |
10395 | } | |
10396 | ||
10397 | /* Add archive members next. */ | |
10398 | for (c = cmdline_object_only_archive_list.head; c != NULL; c = next) | |
10399 | { | |
10400 | if (c->header.type != cmdline_is_bfd_enum) | |
10401 | abort (); | |
10402 | ||
10403 | next = c->header.next; | |
10404 | ||
10405 | abfd = c->abfd.abfd; | |
10406 | archive = abfd->my_archive; | |
10407 | ||
10408 | /* Add the first archive of the archive member group. */ | |
10409 | lang_add_input_file (archive->filename, | |
10410 | lang_input_file_is_file_enum, NULL); | |
10411 | ||
10412 | /* Skip the rest members in the archive member group. */ | |
10413 | do | |
10414 | { | |
10415 | if (!next) | |
10416 | break; | |
10417 | ||
10418 | if (next->header.type != cmdline_is_bfd_enum) | |
10419 | abort (); | |
10420 | ||
10421 | next = next->header.next; | |
10422 | if (!next) | |
10423 | break; | |
10424 | nbfd = next->abfd.abfd; | |
10425 | narchive = nbfd->my_archive; | |
10426 | } | |
10427 | while (strcmp (archive->filename, narchive->filename) == 0); | |
10428 | } | |
10429 | } | |
10430 | ||
10431 | struct cmdline_arg | |
10432 | { | |
10433 | bfd *obfd; | |
10434 | asymbol **isympp; | |
10435 | int status; | |
10436 | }; | |
10437 | ||
10438 | /* Create a section in OBFD with the same | |
10439 | name and attributes as ISECTION in IBFD. */ | |
10440 | ||
10441 | static void | |
10442 | setup_section (bfd *ibfd, sec_ptr isection, void *p) | |
10443 | { | |
10444 | struct cmdline_arg *arg = (struct cmdline_arg *) p; | |
10445 | bfd *obfd = arg->obfd; | |
10446 | asymbol **isympp = arg->isympp; | |
10447 | const char *name = isection->name; | |
10448 | sec_ptr osection; | |
10449 | const char *err; | |
10450 | ||
10451 | /* Skip the object-only section. */ | |
10452 | if (ibfd->object_only_section == isection) | |
10453 | return; | |
10454 | ||
10455 | /* If we have already failed earlier on, do not keep on generating | |
10456 | complaints now. */ | |
10457 | if (arg->status) | |
10458 | return; | |
10459 | ||
10460 | osection = bfd_make_section_anyway_with_flags (obfd, name, | |
10461 | isection->flags); | |
10462 | ||
10463 | if (osection == NULL) | |
10464 | { | |
10465 | err = _("failed to create output section"); | |
10466 | goto loser; | |
10467 | } | |
10468 | ||
10469 | osection->size = isection->size; | |
10470 | osection->vma = isection->vma; | |
10471 | osection->lma = isection->lma; | |
10472 | osection->alignment_power = isection->alignment_power; | |
10473 | ||
10474 | /* Copy merge entity size. */ | |
10475 | osection->entsize = isection->entsize; | |
10476 | ||
10477 | /* This used to be mangle_section; we do here to avoid using | |
10478 | bfd_get_section_by_name since some formats allow multiple | |
10479 | sections with the same name. */ | |
10480 | isection->output_section = osection; | |
10481 | isection->output_offset = 0; | |
10482 | ||
10483 | if ((isection->flags & SEC_GROUP) != 0) | |
10484 | { | |
10485 | asymbol *gsym = bfd_group_signature (isection, isympp); | |
10486 | ||
10487 | if (gsym != NULL) | |
10488 | { | |
10489 | gsym->flags |= BSF_KEEP; | |
10490 | if (ibfd->xvec->flavour == bfd_target_elf_flavour) | |
10491 | elf_group_id (isection) = gsym; | |
10492 | } | |
10493 | } | |
10494 | ||
10495 | /* Allow the BFD backend to copy any private data it understands | |
10496 | from the input section to the output section. */ | |
10497 | if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection)) | |
10498 | { | |
10499 | err = _("failed to copy private data"); | |
10500 | goto loser; | |
10501 | } | |
10502 | ||
10503 | /* All went well. */ | |
10504 | return; | |
10505 | ||
10506 | loser: | |
10507 | arg->status = 1; | |
10508 | fatal (_("%P: setup_section: %s: %s\n"), err, name); | |
10509 | } | |
10510 | ||
10511 | /* Copy the data of input section ISECTION of IBFD | |
10512 | to an output section with the same name in OBFD. | |
10513 | If stripping then don't copy any relocation info. */ | |
10514 | ||
10515 | static void | |
10516 | copy_section (bfd *ibfd, sec_ptr isection, void *p) | |
10517 | { | |
10518 | struct cmdline_arg *arg = (struct cmdline_arg *) p; | |
10519 | bfd *obfd = arg->obfd; | |
10520 | asymbol **isympp = arg->isympp; | |
10521 | arelent **relpp; | |
10522 | long relcount; | |
10523 | sec_ptr osection; | |
10524 | bfd_size_type size; | |
10525 | long relsize; | |
10526 | flagword flags; | |
10527 | const char *err; | |
10528 | ||
10529 | /* Skip the object-only section. */ | |
10530 | if (ibfd->object_only_section == isection) | |
10531 | return; | |
10532 | ||
10533 | /* If we have already failed earlier on, do not keep on generating | |
10534 | complaints now. */ | |
10535 | if (arg->status) | |
10536 | return; | |
10537 | ||
10538 | flags = bfd_section_flags (isection); | |
10539 | if ((flags & SEC_GROUP) != 0) | |
10540 | return; | |
10541 | ||
10542 | osection = isection->output_section; | |
10543 | size = bfd_section_size (isection); | |
10544 | ||
10545 | if (size == 0 || osection == 0) | |
10546 | return; | |
10547 | ||
10548 | relsize = bfd_get_reloc_upper_bound (ibfd, isection); | |
10549 | ||
10550 | if (relsize < 0) | |
10551 | { | |
10552 | /* Do not complain if the target does not support relocations. */ | |
10553 | if (relsize == -1 | |
10554 | && bfd_get_error () == bfd_error_invalid_operation) | |
10555 | relsize = 0; | |
10556 | else | |
10557 | { | |
10558 | err = bfd_errmsg (bfd_get_error ()); | |
10559 | goto loser; | |
10560 | } | |
10561 | } | |
10562 | ||
10563 | if (relsize == 0) | |
10564 | bfd_set_reloc (obfd, osection, NULL, 0); | |
10565 | else | |
10566 | { | |
10567 | relpp = (arelent **) xmalloc (relsize); | |
10568 | relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); | |
10569 | if (relcount < 0) | |
10570 | { | |
10571 | err = _("relocation count is negative"); | |
10572 | goto loser; | |
10573 | } | |
10574 | ||
10575 | bfd_set_reloc (obfd, osection, | |
10576 | relcount == 0 ? NULL : relpp, relcount); | |
10577 | if (relcount == 0) | |
10578 | free (relpp); | |
10579 | } | |
10580 | ||
10581 | if (bfd_section_flags (isection) & SEC_HAS_CONTENTS) | |
10582 | { | |
10583 | bfd_byte *memhunk = NULL; | |
10584 | ||
10585 | if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)) | |
10586 | { | |
10587 | err = bfd_errmsg (bfd_get_error ()); | |
10588 | goto loser; | |
10589 | } | |
10590 | ||
10591 | if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size)) | |
10592 | { | |
10593 | err = bfd_errmsg (bfd_get_error ()); | |
10594 | goto loser; | |
10595 | } | |
10596 | free (memhunk); | |
10597 | } | |
10598 | ||
10599 | /* All went well. */ | |
10600 | return; | |
10601 | ||
10602 | loser: | |
10603 | fatal (_("%P: copy_section: %s: %s\n"), err, isection->name); | |
10604 | } | |
10605 | /* Open the temporary bfd created in the same directory as PATH. */ | |
10606 | ||
10607 | static bfd * | |
10608 | cmdline_fopen_temp (const char *path, const char *target, | |
10609 | const char *mode) | |
10610 | { | |
10611 | #define template "ldXXXXXX" | |
10612 | const char *slash = strrchr (path, '/'); | |
10613 | char *tmpname; | |
10614 | size_t len; | |
10615 | int fd; | |
10616 | ||
10617 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
10618 | { | |
10619 | /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ | |
10620 | char *bslash = strrchr (path, '\\'); | |
10621 | ||
10622 | if (slash == NULL || (bslash != NULL && bslash > slash)) | |
10623 | slash = bslash; | |
10624 | if (slash == NULL && path[0] != '\0' && path[1] == ':') | |
10625 | slash = path + 1; | |
10626 | } | |
10627 | #endif | |
10628 | ||
10629 | if (slash != (char *) NULL) | |
10630 | { | |
10631 | len = slash - path; | |
10632 | tmpname = (char *) xmalloc (len + sizeof (template) + 2); | |
10633 | memcpy (tmpname, path, len); | |
10634 | ||
10635 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
10636 | /* If tmpname is "X:", appending a slash will make it a root | |
10637 | directory on drive X, which is NOT the same as the current | |
10638 | directory on drive X. */ | |
10639 | if (len == 2 && tmpname[1] == ':') | |
10640 | tmpname[len++] = '.'; | |
10641 | #endif | |
10642 | tmpname[len++] = '/'; | |
10643 | } | |
10644 | else | |
10645 | { | |
10646 | tmpname = (char *) xmalloc (sizeof (template)); | |
10647 | len = 0; | |
10648 | } | |
10649 | ||
10650 | memcpy (tmpname + len, template, sizeof (template)); | |
10651 | #undef template | |
10652 | ||
10653 | #ifdef HAVE_MKSTEMP | |
10654 | fd = mkstemp (tmpname); | |
10655 | #else | |
10656 | tmpname = mktemp (tmpname); | |
10657 | if (tmpname == NULL) | |
10658 | return NULL; | |
10659 | fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600); | |
10660 | #endif | |
10661 | if (fd == -1) | |
10662 | return NULL; | |
10663 | return bfd_fopen (tmpname, target, mode, fd); | |
10664 | } | |
10665 | ||
10666 | /* Add the object-only section. */ | |
10667 | ||
10668 | static void | |
10669 | cmdline_add_object_only_section (bfd_byte *contents, size_t size) | |
10670 | { | |
10671 | bfd_vma start; | |
10672 | flagword flags; | |
10673 | enum bfd_architecture iarch; | |
10674 | unsigned int imach; | |
10675 | long symcount; | |
10676 | long symsize; | |
10677 | asymbol **isympp = NULL; | |
10678 | asymbol **osympp = NULL; | |
10679 | bfd *obfd = NULL, *ibfd; | |
10680 | const char *err; | |
10681 | struct arg | |
10682 | { | |
10683 | bfd *obfd; | |
10684 | asymbol **isympp; | |
10685 | int status; | |
10686 | } arg; | |
10687 | char **matching; | |
10688 | char *ofilename = NULL; | |
10689 | asection *sec; | |
10690 | ||
10691 | ibfd = bfd_openr (output_filename, output_target); | |
10692 | if (!ibfd) | |
10693 | { | |
10694 | err = bfd_errmsg (bfd_get_error ()); | |
10695 | goto loser; | |
10696 | } | |
10697 | ||
10698 | if (!bfd_check_format_matches (ibfd, bfd_object, &matching)) | |
10699 | { | |
10700 | err = bfd_errmsg (bfd_get_error ()); | |
10701 | goto loser; | |
10702 | } | |
10703 | ||
10704 | obfd = cmdline_fopen_temp (output_filename, output_target, FOPEN_WB); | |
10705 | if (!obfd) | |
10706 | { | |
10707 | err = bfd_errmsg (bfd_get_error ()); | |
10708 | goto loser; | |
10709 | } | |
10710 | /* To be used after bfd_close (). */ | |
10711 | ofilename = xstrdup (bfd_get_filename (obfd)); | |
10712 | ||
10713 | if (!bfd_set_format (obfd, bfd_object)) | |
10714 | { | |
10715 | err = bfd_errmsg (bfd_get_error ()); | |
10716 | goto loser; | |
10717 | } | |
10718 | ||
10719 | /* Copy the start address, flags and architecture of input file to | |
10720 | output file. */ | |
10721 | flags = bfd_get_file_flags (ibfd); | |
10722 | start = bfd_get_start_address (ibfd); | |
10723 | iarch = bfd_get_arch (ibfd); | |
10724 | imach = bfd_get_mach (ibfd); | |
10725 | if (!bfd_set_start_address (obfd, start) | |
10726 | || !bfd_set_file_flags (obfd, flags) | |
10727 | || !bfd_set_arch_mach (obfd, iarch, imach)) | |
10728 | { | |
10729 | err = bfd_errmsg (bfd_get_error ()); | |
10730 | goto loser; | |
10731 | } | |
10732 | ||
10733 | symsize = bfd_get_symtab_upper_bound (ibfd); | |
10734 | if (symsize < 0) | |
10735 | { | |
10736 | err = bfd_errmsg (bfd_get_error ()); | |
10737 | goto loser; | |
10738 | } | |
10739 | ||
10740 | isympp = (asymbol **) xmalloc (symsize); | |
10741 | symcount = bfd_canonicalize_symtab (ibfd, isympp); | |
10742 | if (symcount < 0) | |
10743 | { | |
10744 | err = bfd_errmsg (bfd_get_error ()); | |
10745 | goto loser; | |
10746 | } | |
10747 | ||
10748 | arg.obfd = obfd; | |
10749 | arg.isympp = isympp; | |
10750 | arg.status = 0; | |
10751 | ||
10752 | /* BFD mandates that all output sections be created and sizes set before | |
10753 | any output is done. Thus, we traverse all sections multiple times. */ | |
10754 | bfd_map_over_sections (ibfd, setup_section, &arg); | |
10755 | ||
10756 | if (arg.status) | |
10757 | { | |
10758 | err = _("error setting up sections"); | |
10759 | goto loser; | |
10760 | } | |
10761 | ||
10762 | /* Allow the BFD backend to copy any private data it understands | |
10763 | from the input section to the output section. */ | |
10764 | if (! bfd_copy_private_header_data (ibfd, obfd)) | |
10765 | { | |
10766 | err = _("error copying private header data"); | |
10767 | goto loser; | |
10768 | } | |
10769 | ||
10770 | /* Create the object-only section. */ | |
10771 | sec = bfd_make_section_with_flags (obfd, | |
10772 | GNU_OBJECT_ONLY_SECTION_NAME, | |
10773 | (SEC_HAS_CONTENTS | |
10774 | | SEC_READONLY | |
10775 | | SEC_DATA | |
10776 | | SEC_LINKER_CREATED)); | |
10777 | if (sec == NULL) | |
10778 | { | |
10779 | err = _("can't create object-only section"); | |
10780 | goto loser; | |
10781 | } | |
10782 | ||
10783 | if (! bfd_set_section_size (sec, size)) | |
10784 | { | |
10785 | err = _("can't set object-only section size"); | |
10786 | goto loser; | |
10787 | } | |
10788 | ||
10789 | if (ibfd->object_only_section) | |
10790 | { | |
10791 | /* Filter out the object-only section symbol. */ | |
10792 | long src_count = 0, dst_count = 0; | |
10793 | asymbol **from, **to; | |
10794 | ||
10795 | osympp = xmalloc ((symcount + 1) * sizeof (asymbol *)); | |
10796 | from = isympp; | |
10797 | to = osympp; | |
10798 | for (; src_count < symcount; src_count++) | |
10799 | { | |
10800 | asymbol *sym = from[src_count]; | |
10801 | if (bfd_asymbol_section (sym) != ibfd->object_only_section) | |
10802 | to[dst_count++] = sym; | |
10803 | } | |
10804 | to[dst_count] = NULL; | |
10805 | symcount = dst_count; | |
10806 | bfd_set_symtab (obfd, osympp, symcount); | |
10807 | } | |
10808 | else | |
10809 | bfd_set_symtab (obfd, isympp, symcount); | |
10810 | ||
10811 | /* This has to happen after the symbol table has been set. */ | |
10812 | bfd_map_over_sections (ibfd, copy_section, &arg); | |
10813 | ||
10814 | if (arg.status) | |
10815 | { | |
10816 | err = _("error copying sections"); | |
10817 | goto loser; | |
10818 | } | |
10819 | ||
10820 | /* Copy the object-only section to the output. */ | |
10821 | if (! bfd_set_section_contents (obfd, sec, contents, 0, size)) | |
10822 | { | |
10823 | err = _("error adding object-only section"); | |
10824 | goto loser; | |
10825 | } | |
10826 | ||
10827 | /* Allow the BFD backend to copy any private data it understands | |
10828 | from the input BFD to the output BFD. This is done last to | |
10829 | permit the routine to look at the filtered symbol table, which is | |
10830 | important for the ECOFF code at least. */ | |
10831 | if (! bfd_copy_private_bfd_data (ibfd, obfd)) | |
10832 | { | |
10833 | err = _("error copying private BFD data"); | |
10834 | goto loser; | |
10835 | } | |
10836 | ||
10837 | if (!bfd_close (obfd)) | |
10838 | { | |
10839 | unlink (ofilename); | |
10840 | fatal (_("%P: failed to finish output with object-only section\n")); | |
10841 | } | |
10842 | ||
10843 | /* Must be freed after bfd_close (). */ | |
10844 | free (isympp); | |
10845 | free (osympp); | |
10846 | ||
10847 | if (rename (ofilename, output_filename)) | |
10848 | { | |
10849 | unlink (ofilename); | |
10850 | fatal (_("%P: failed to rename output with object-only section\n")); | |
10851 | } | |
10852 | ||
10853 | free (ofilename); | |
10854 | return; | |
10855 | ||
10856 | loser: | |
10857 | free (isympp); | |
10858 | free (osympp); | |
10859 | if (obfd) | |
10860 | bfd_close (obfd); | |
10861 | if (ofilename) | |
10862 | { | |
10863 | unlink (ofilename); | |
10864 | free (ofilename); | |
10865 | } | |
10866 | fatal (_("%P: failed to add object-only section: %s\n"), err); | |
10867 | } | |
10868 | ||
10869 | /* Emit the final output with object-only section. */ | |
10870 | ||
10871 | void | |
10872 | cmdline_emit_object_only_section (void) | |
10873 | { | |
10874 | const char *saved_output_filename = output_filename; | |
10875 | int fd; | |
10876 | size_t size, off; | |
10877 | bfd_byte *contents; | |
10878 | struct stat st; | |
10879 | ||
10880 | /* Get a temporary object-only file. */ | |
10881 | output_filename = make_temp_file (".obj-only.o"); | |
10882 | ||
10883 | had_output_filename = false; | |
10884 | link_info.input_bfds = NULL; | |
10885 | link_info.input_bfds_tail = &link_info.input_bfds; | |
10886 | ||
10887 | lang_init (true); | |
10888 | ldexp_init (true); | |
10889 | ||
10890 | /* Set up the object-only output. */ | |
10891 | lang_final (); | |
10892 | ||
10893 | /* Open the object-only file for output. */ | |
10894 | lang_for_each_statement (ldlang_open_output); | |
10895 | ||
10896 | ldemul_create_output_section_statements (); | |
10897 | ||
10898 | if (!bfd_section_already_linked_table_init ()) | |
10899 | fatal (_("%P: Failed to create hash table\n")); | |
10900 | ||
10901 | /* Call cmdline_on_object_only_archive_list_p to check which member | |
10902 | should be loaded. */ | |
10903 | input_flags.whole_archive = true; | |
10904 | ||
10905 | /* Set it to avoid adding more to cmdline lists. */ | |
10906 | config.emitting_gnu_object_only = true; | |
10907 | ||
10908 | /* Get object-only input files. */ | |
10909 | cmdline_get_object_only_input_files (); | |
10910 | ||
10911 | /* Open object-only input files. */ | |
10912 | open_input_bfds (statement_list.head, NULL, OPEN_BFD_NORMAL); | |
10913 | ||
10914 | ldemul_after_open (); | |
10915 | ||
10916 | bfd_section_already_linked_table_free (); | |
10917 | ||
10918 | /* Make sure that we're not mixing architectures. We call this | |
10919 | after all the input files have been opened, but before we do any | |
10920 | other processing, so that any operations merge_private_bfd_data | |
10921 | does on the output file will be known during the rest of the | |
10922 | link. */ | |
10923 | lang_check (); | |
10924 | ||
10925 | /* Size up the common data. */ | |
10926 | lang_common (); | |
10927 | ||
10928 | /* Update wild statements. */ | |
10929 | update_wild_statements (statement_list.head); | |
10930 | ||
10931 | /* Run through the contours of the script and attach input sections | |
10932 | to the correct output sections. */ | |
10933 | map_input_to_output_sections (statement_list.head, NULL, NULL); | |
10934 | ||
10935 | /* Find any sections not attached explicitly and handle them. */ | |
10936 | lang_place_orphans (); | |
10937 | ||
10938 | /* Do anything special before sizing sections. This is where ELF | |
10939 | and other back-ends size dynamic sections. */ | |
10940 | ldemul_before_allocation (); | |
10941 | ||
10942 | /* Size up the sections. */ | |
10943 | lang_size_sections (NULL, ! RELAXATION_ENABLED); | |
10944 | ||
10945 | /* See if anything special should be done now we know how big | |
10946 | everything is. This is where relaxation is done. */ | |
10947 | ldemul_after_allocation (); | |
10948 | ||
10949 | ldemul_finish (); | |
10950 | ||
10951 | /* Make sure that the section addresses make sense. */ | |
10952 | if (command_line.check_section_addresses) | |
10953 | lang_check_section_addresses (); | |
10954 | ||
10955 | lang_end (); | |
10956 | ||
10957 | ldwrite (); | |
10958 | ||
10959 | ldexp_finish (true); | |
10960 | lang_finish (); | |
10961 | ||
10962 | if (! bfd_close (link_info.output_bfd)) | |
10963 | fatal (_("%P:%s: final close failed on object-only output: %E\n"), | |
10964 | output_filename); | |
10965 | ||
10966 | link_info.output_bfd = NULL; | |
10967 | ||
10968 | /* Read in the object-only file. */ | |
10969 | fd = open (output_filename, O_RDONLY | O_BINARY); | |
10970 | if (fd < 0) | |
10971 | { | |
10972 | bfd_set_error (bfd_error_system_call); | |
10973 | fatal (_("%P:%s: cannot open object-only output: %E\n"), | |
10974 | output_filename); | |
10975 | } | |
10976 | ||
10977 | /* Get the object-only file size. */ | |
10978 | if (fstat (fd, &st) != 0) | |
10979 | { | |
10980 | bfd_set_error (bfd_error_system_call); | |
10981 | fatal (_("%P:%s: cannot stat object-only output: %E\n"), | |
10982 | output_filename); | |
10983 | } | |
10984 | ||
10985 | size = st.st_size; | |
10986 | off = 0; | |
10987 | contents = (bfd_byte *) xmalloc (size); | |
10988 | while (off != size) | |
10989 | { | |
10990 | ssize_t got; | |
10991 | ||
10992 | got = read (fd, contents + off, size - off); | |
10993 | if (got < 0) | |
10994 | { | |
10995 | bfd_set_error (bfd_error_system_call); | |
10996 | fatal (_("%P:%s: read failed on object-only output: %E\n"), | |
10997 | output_filename); | |
10998 | } | |
10999 | ||
11000 | off += got; | |
11001 | } | |
11002 | ||
11003 | close (fd); | |
11004 | ||
11005 | /* Remove the temporary object-only file. */ | |
11006 | unlink (output_filename); | |
11007 | ||
11008 | output_filename = saved_output_filename; | |
11009 | ||
11010 | cmdline_add_object_only_section (contents, size); | |
11011 | ||
11012 | free (contents); | |
11013 | } | |
11014 | ||
11015 | /* Extract the object-only section. */ | |
11016 | ||
11017 | static const char * | |
11018 | cmdline_extract_object_only_section (bfd *abfd) | |
11019 | { | |
11020 | const char *name = bfd_extract_object_only_section (abfd); | |
11021 | ||
11022 | if (name == NULL) | |
11023 | fatal (_("%P: cannot extract object-only section from %B: %E\n"), abfd); | |
11024 | ||
11025 | /* It should be removed after it is done. */ | |
11026 | cmdline_list_append (&cmdline_temp_object_only_list, | |
11027 | cmdline_is_file_enum, (void *) name); | |
11028 | ||
11029 | return name; | |
11030 | } | |
11031 | ||
11032 | /* Load the object-only section. */ | |
11033 | ||
11034 | static void | |
11035 | cmdline_load_object_only_section (const char *name) | |
11036 | { | |
11037 | lang_input_statement_type *entry | |
11038 | = new_afile (name, lang_input_file_is_file_enum, NULL, NULL); | |
11039 | ||
11040 | if (!entry) | |
11041 | abort (); | |
11042 | ||
11043 | ldfile_open_file (entry); | |
11044 | ||
11045 | if (trace_files || verbose) | |
11046 | info_msg ("%pI\n", entry); | |
11047 | ||
11048 | if (entry->flags.missing_file | |
11049 | || bfd_get_format (entry->the_bfd) != bfd_object) | |
11050 | abort (); | |
11051 | ||
11052 | ldlang_add_file (entry); | |
11053 | ||
11054 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) | |
11055 | entry->flags.loaded = true; | |
11056 | else | |
11057 | fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); | |
11058 | } | |
11059 | ||
11060 | /* Check and handle the object-only section. */ | |
11061 | ||
11062 | void | |
11063 | cmdline_check_object_only_section (bfd *abfd, bool lto) | |
11064 | { | |
11065 | const char *filename; | |
11066 | ||
11067 | if (config.emitting_gnu_object_only || abfd->format != bfd_object) | |
11068 | return; | |
11069 | ||
11070 | if (lto) | |
11071 | { | |
11072 | /* For LTO link, we only need to extract object-only section | |
11073 | from the mixed object, add it to input, and put it on LTO | |
11074 | claimed output. */ | |
11075 | switch (bfd_get_lto_type (abfd)) | |
11076 | { | |
11077 | default: | |
11078 | abort (); | |
11079 | case lto_mixed_object: | |
11080 | filename = cmdline_extract_object_only_section (abfd); | |
11081 | cmdline_load_object_only_section (filename); | |
11082 | break; | |
11083 | case lto_non_ir_object: | |
11084 | case lto_slim_ir_object: | |
11085 | case lto_fat_ir_object: | |
11086 | break; | |
11087 | } | |
11088 | } | |
11089 | else if (bfd_link_relocatable (&link_info)) | |
11090 | { | |
11091 | /* For non-LTO relocatable link, we need to append non-IR object | |
11092 | file and the object file in object-only section to the object | |
11093 | only list. */ | |
11094 | switch (bfd_get_lto_type (abfd)) | |
11095 | { | |
11096 | default: | |
11097 | abort (); | |
11098 | case lto_mixed_object: | |
11099 | filename = cmdline_extract_object_only_section (abfd); | |
11100 | cmdline_object_only_list_append (cmdline_is_file_enum, | |
11101 | (void *) filename); | |
11102 | break; | |
11103 | case lto_non_ir_object: | |
11104 | cmdline_object_only_list_append (cmdline_is_bfd_enum, abfd); | |
11105 | break; | |
11106 | case lto_slim_ir_object: | |
11107 | case lto_fat_ir_object: | |
11108 | break; | |
11109 | } | |
11110 | } | |
11111 | } | |
11112 | ||
11113 | /* Remove temporary object-only files. */ | |
11114 | ||
11115 | void | |
11116 | cmdline_remove_object_only_files (void) | |
11117 | { | |
11118 | cmdline_union_type *c; | |
11119 | ||
11120 | if (config.plugin_save_temps) | |
11121 | return; | |
11122 | ||
11123 | c = cmdline_temp_object_only_list.head; | |
11124 | for (; c != NULL; c = c->header.next) | |
11125 | switch (c->header.type) | |
11126 | { | |
11127 | default: | |
11128 | abort (); | |
11129 | case cmdline_is_file_enum: | |
11130 | unlink (c->file.filename); | |
11131 | break; | |
11132 | } | |
11133 | } |