]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
e8e7cf2a | 2 | Copyright (C) 1991-2025 Free Software Foundation, Inc. |
252b5132 | 3 | |
f96b4a7b | 4 | This file is part of the GNU Binutils. |
252b5132 | 5 | |
f96b4a7b | 6 | This program is free software; you can redistribute it and/or modify |
53b2a62f | 7 | it under the terms of the GNU General Public License as published by |
f96b4a7b NC |
8 | the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. | |
252b5132 | 10 | |
f96b4a7b | 11 | This program is distributed in the hope that it will be useful, |
53b2a62f NC |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
53b2a62f | 16 | You should have received a copy of the GNU General Public License |
f96b4a7b NC |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
9a24a276 | 22 | #include <limits.h> |
3db64b00 | 23 | #include "bfd.h" |
252b5132 | 24 | #include "libiberty.h" |
42627821 | 25 | #include "filenames.h" |
3882b010 | 26 | #include "safe-ctype.h" |
252b5132 RH |
27 | #include "obstack.h" |
28 | #include "bfdlink.h" | |
1ff6de03 | 29 | #include "ctf-api.h" |
252b5132 RH |
30 | #include "ld.h" |
31 | #include "ldmain.h" | |
252b5132 RH |
32 | #include "ldexp.h" |
33 | #include "ldlang.h" | |
df2a7313 | 34 | #include <ldgram.h> |
252b5132 RH |
35 | #include "ldlex.h" |
36 | #include "ldmisc.h" | |
37 | #include "ldctor.h" | |
38 | #include "ldfile.h" | |
b71e2778 | 39 | #include "ldemul.h" |
9b854f16 | 40 | #include "ldwrite.h" |
252b5132 RH |
41 | #include "fnmatch.h" |
42 | #include "demangle.h" | |
108ba305 | 43 | #include "hashtab.h" |
0fef4b98 | 44 | #include "elf-bfd.h" |
2d5783fa NC |
45 | #include "bfdver.h" |
46 | ||
0381901e | 47 | #if BFD_SUPPORTS_PLUGINS |
5d3236ee | 48 | #include "plugin.h" |
2d5783fa | 49 | #endif |
252b5132 | 50 | |
9b854f16 L |
51 | /* FIXME: Put it here to avoid NAME conflict from ldgram.h. */ |
52 | #include "elf-bfd.h" | |
53 | ||
7abb6dea | 54 | #ifndef offsetof |
cf888e70 | 55 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
56 | #endif |
57 | ||
fc90c280 AM |
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. */ | |
252b5132 | 65 | static struct obstack stat_obstack; |
35835446 | 66 | static struct obstack map_obstack; |
b1eecf6f | 67 | static struct obstack pt_obstack; |
252b5132 RH |
68 | |
69 | #define obstack_chunk_alloc xmalloc | |
70 | #define obstack_chunk_free free | |
8be573a7 | 71 | static const char *entry_symbol_default = "start"; |
f38a2680 | 72 | static bool map_head_is_link_order = false; |
252b5132 | 73 | static lang_output_section_statement_type *default_common_section; |
f38a2680 | 74 | static bool map_option_f; |
252b5132 RH |
75 | static bfd_vma print_dot; |
76 | static lang_input_statement_type *first_file; | |
5f992e62 | 77 | static const char *current_target; |
bde18da4 AM |
78 | static lang_statement_list_type *stat_save[10]; |
79 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
c4b78195 | 80 | static struct unique_sections *unique_section_list; |
16e4ecc0 | 81 | static struct asneeded_minfo *asneeded_list_head; |
fc90c280 | 82 | static unsigned int opb_shift = 0; |
9b854f16 L |
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; | |
252b5132 | 86 | |
cf888e70 | 87 | /* Forward declarations. */ |
a431bc2e | 88 | static void exp_init_os (etree_type *); |
1579bae1 | 89 | static lang_input_statement_type *lookup_name (const char *); |
670c91c0 MM |
90 | static bool wont_add_section_p (asection *, |
91 | lang_output_section_statement_type *); | |
1579bae1 | 92 | static void insert_undefined (const char *); |
f38a2680 | 93 | static bool sort_def_symbol (struct bfd_link_hash_entry *, void *); |
b1eecf6f MM |
94 | static lang_statement_union_type *new_statement (enum statement_enum type, |
95 | size_t size, | |
96 | lang_statement_list_type *list); | |
1579bae1 AM |
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); | |
f38a2680 AM |
102 | static void print_input_section (asection *, bool); |
103 | static bool lang_one_common (struct bfd_link_hash_entry *, void *); | |
1579bae1 AM |
104 | static void lang_record_phdrs (void); |
105 | static void lang_do_version_exports_section (void); | |
55255dae L |
106 | static void lang_finalize_version_expr_head |
107 | (struct bfd_elf_version_expr_head *); | |
f38a2680 | 108 | static void lang_do_memory_regions (bool); |
9b854f16 L |
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 *); | |
1579bae1 | 113 | |
cf888e70 | 114 | /* Exported variables. */ |
8be573a7 | 115 | const char *output_target; |
252b5132 | 116 | lang_output_section_statement_type *abs_output_section; |
b209b5a6 AM |
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; | |
5c1e6d53 | 124 | lang_statement_list_type lang_os_list; |
252b5132 | 125 | lang_statement_list_type *stat_ptr = &statement_list; |
e368bf56 TP |
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). */ | |
87f2a346 | 131 | lang_statement_list_type file_chain = { NULL, NULL }; |
e368bf56 TP |
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). */ | |
dc27aea4 | 137 | lang_statement_list_type input_file_chain; |
16171946 | 138 | static const char *current_input_file; |
37a141bf | 139 | struct bfd_elf_dynamic_list **current_dynamic_list_p; |
e3e942e9 | 140 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 141 | const char *entry_section = ".text"; |
66be1055 | 142 | struct lang_input_statement_flags input_flags; |
f38a2680 AM |
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; | |
2d5783fa | 148 | bool enable_linker_version = false; |
f5ff60a6 | 149 | struct lang_phdr *lang_phdr_list; |
252b5132 | 150 | struct lang_nocrossrefs *nocrossref_list; |
16e4ecc0 | 151 | struct asneeded_minfo **asneeded_list_tail; |
094e34f2 | 152 | #ifdef ENABLE_LIBCTF |
139633c3 | 153 | static ctf_dict_t *ctf_output; |
094e34f2 | 154 | #endif |
e9ee469a | 155 | |
baf09cba AM |
156 | /* Functions that traverse the linker script and might evaluate |
157 | DEFINED() need to increment this at the start of the traversal. */ | |
420e579c | 158 | int lang_statement_iteration = 0; |
252b5132 | 159 | |
baf09cba AM |
160 | /* Count times through one_lang_size_sections_pass after mark phase. */ |
161 | static int lang_sizing_iteration = 0; | |
162 | ||
61f5d054 L |
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 | ||
d1778b88 | 170 | #define new_stat(x, y) \ |
1579bae1 | 171 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 172 | |
d1778b88 AM |
173 | #define outside_section_address(q) \ |
174 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 175 | |
d1778b88 AM |
176 | #define outside_symbol_address(q) \ |
177 | ((q)->value + outside_section_address (q->section)) | |
252b5132 | 178 | |
1ff6de03 NA |
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 | ||
1579bae1 AM |
185 | void * |
186 | stat_alloc (size_t size) | |
252b5132 RH |
187 | { |
188 | return obstack_alloc (&stat_obstack, size); | |
189 | } | |
190 | ||
90bea493 AM |
191 | void |
192 | stat_free (void *str) | |
193 | { | |
194 | obstack_free (&stat_obstack, str); | |
195 | } | |
196 | ||
d4115c2c AM |
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 | ||
90bea493 AM |
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 | ||
049522ca MM |
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 | ||
97407faf AM |
270 | static int |
271 | name_match (const char *pattern, const char *name) | |
272 | { | |
049522ca MM |
273 | if (is_simple_wild (pattern)) |
274 | return !match_simple_wild (pattern, name); | |
97407faf AM |
275 | if (wildcardp (pattern)) |
276 | return fnmatch (pattern, name, 0); | |
277 | return strcmp (pattern, name); | |
278 | } | |
279 | ||
049522ca MM |
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; | |
3bf5bf54 | 289 | size_t inputlen = strlen (name); |
049522ca | 290 | int r; |
3bf5bf54 NC |
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 | ||
049522ca MM |
302 | if (sl) |
303 | { | |
049522ca MM |
304 | if (inputlen < sl) |
305 | return 1; | |
3bf5bf54 | 306 | |
049522ca MM |
307 | r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl); |
308 | if (r) | |
309 | return r; | |
310 | } | |
3bf5bf54 | 311 | |
049522ca MM |
312 | if (nl == pl + sl + 1 && spec->name[pl] == '*') |
313 | return 0; | |
3bf5bf54 NC |
314 | |
315 | if (nl > pl) | |
049522ca | 316 | return fnmatch (spec->name + pl, name + pl, 0); |
3bf5bf54 NC |
317 | |
318 | if (inputlen >= nl) | |
319 | return name[nl]; | |
320 | ||
321 | return 0; | |
049522ca MM |
322 | } |
323 | ||
16171946 | 324 | static char * |
27ba92a5 | 325 | stat_ldirname (const char *name) |
16171946 FS |
326 | { |
327 | const char *base = lbasename (name); | |
16171946 FS |
328 | |
329 | while (base > name && IS_DIR_SEPARATOR (base[-1])) | |
330 | --base; | |
d4115c2c AM |
331 | size_t len = base - name; |
332 | if (len == 0) | |
333 | return "."; | |
334 | return stat_memdup (name, len, len + 1); | |
16171946 FS |
335 | } |
336 | ||
97407faf AM |
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". */ | |
967928e9 | 355 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
356 | p = strchr (p + 1, link_info.path_separator); |
357 | #endif | |
358 | return p; | |
359 | } | |
360 | ||
967928e9 AM |
361 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
362 | return whether F matches FILE_SPEC. */ | |
363 | ||
f38a2680 | 364 | static bool |
967928e9 AM |
365 | input_statement_is_archive_path (const char *file_spec, char *sep, |
366 | lang_input_statement_type *f) | |
367 | { | |
f38a2680 | 368 | bool match = false; |
967928e9 AM |
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 | { | |
f38a2680 | 375 | match = true; |
967928e9 AM |
376 | |
377 | if (sep != file_spec) | |
378 | { | |
607b4833 | 379 | const char *aname = bfd_get_filename (f->the_bfd->my_archive); |
967928e9 AM |
380 | *sep = 0; |
381 | match = name_match (file_spec, aname) == 0; | |
382 | *sep = link_info.path_separator; | |
383 | } | |
384 | } | |
385 | return match; | |
386 | } | |
387 | ||
f38a2680 | 388 | static bool |
d0bf826b AM |
389 | unique_section_p (const asection *sec, |
390 | const lang_output_section_statement_type *os) | |
577a0623 AM |
391 | { |
392 | struct unique_sections *unam; | |
d0d6a25b | 393 | const char *secnam; |
577a0623 | 394 | |
7bdf4127 | 395 | if (!link_info.resolve_section_groups |
d0d6a25b AM |
396 | && sec->owner != NULL |
397 | && bfd_is_group_section (sec->owner, sec)) | |
d0bf826b AM |
398 | return !(os != NULL |
399 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
d0d6a25b AM |
400 | |
401 | secnam = sec->name; | |
577a0623 | 402 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf | 403 | if (name_match (unam->name, secnam) == 0) |
f38a2680 | 404 | return true; |
577a0623 | 405 | |
f38a2680 | 406 | return false; |
577a0623 AM |
407 | } |
408 | ||
08da4cac | 409 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 410 | |
8f1732fc AB |
411 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return |
412 | false. */ | |
72223188 | 413 | |
f38a2680 | 414 | static bool |
8f1732fc | 415 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, |
6c19b93b | 416 | lang_input_statement_type *file) |
4dec4d4e | 417 | { |
72223188 JJ |
418 | struct name_list *list_tmp; |
419 | ||
8f1732fc | 420 | for (list_tmp = exclude_list; |
72223188 JJ |
421 | list_tmp; |
422 | list_tmp = list_tmp->next) | |
423 | { | |
967928e9 | 424 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 425 | |
967928e9 | 426 | if (p != NULL) |
72223188 | 427 | { |
967928e9 | 428 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
f38a2680 | 429 | return true; |
72223188 JJ |
430 | } |
431 | ||
967928e9 | 432 | else if (name_match (list_tmp->name, file->filename) == 0) |
f38a2680 | 433 | return true; |
967928e9 AM |
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, | |
607b4833 | 441 | bfd_get_filename (file->the_bfd->my_archive)) == 0) |
f38a2680 | 442 | return true; |
72223188 JJ |
443 | } |
444 | ||
f38a2680 | 445 | return false; |
8f1732fc AB |
446 | } |
447 | ||
b1eecf6f MM |
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 | { | |
8f5cd47b MM |
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; | |
edc1244e MM |
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 | |
b1eecf6f MM |
518 | return; |
519 | } | |
520 | ||
885d8643 MM |
521 | /* If filename is excluded we're done. */ |
522 | if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file)) | |
523 | return; | |
524 | ||
b1eecf6f MM |
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 | ||
02ecc8e9 L |
547 | /* Return the numerical value of the init_priority attribute from |
548 | section name NAME. */ | |
549 | ||
9a24a276 AM |
550 | static int |
551 | get_init_priority (const asection *sec) | |
02ecc8e9 | 552 | { |
9a24a276 AM |
553 | const char *name = bfd_section_name (sec); |
554 | const char *dot; | |
02ecc8e9 L |
555 | |
556 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 557 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
558 | lower value means a higher priority. |
559 | ||
9a24a276 | 560 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
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. | |
9a24a276 | 564 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
565 | decimal numerical value of the init_priority attribute. |
566 | The order of execution in .ctors is backward and .dtors | |
567 | is forward. | |
9a24a276 AM |
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])) | |
02ecc8e9 | 580 | { |
9a24a276 AM |
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 | } | |
02ecc8e9 | 592 | } |
9a24a276 | 593 | return -1; |
02ecc8e9 L |
594 | } |
595 | ||
50c77e5d NC |
596 | /* Compare sections ASEC and BSEC according to SORT. */ |
597 | ||
598 | static int | |
85921e9a | 599 | compare_section (sort_type sort, asection *asec, asection *bsec, bool reversed) |
50c77e5d NC |
600 | { |
601 | int ret; | |
9a24a276 | 602 | int a_priority, b_priority; |
50c77e5d NC |
603 | |
604 | switch (sort) | |
605 | { | |
606 | default: | |
607 | abort (); | |
608 | ||
02ecc8e9 | 609 | case by_init_priority: |
9a24a276 AM |
610 | a_priority = get_init_priority (asec); |
611 | b_priority = get_init_priority (bsec); | |
612 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 613 | goto sort_by_name; |
85921e9a NC |
614 | if (reversed) |
615 | ret = b_priority - a_priority; | |
616 | else | |
617 | ret = a_priority - b_priority; | |
02ecc8e9 L |
618 | if (ret) |
619 | break; | |
620 | else | |
621 | goto sort_by_name; | |
622 | ||
50c77e5d | 623 | case by_alignment_name: |
fd361982 | 624 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
625 | if (ret) |
626 | break; | |
627 | /* Fall through. */ | |
628 | ||
629 | case by_name: | |
dc1e8a47 | 630 | sort_by_name: |
85921e9a NC |
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)); | |
50c77e5d NC |
635 | break; |
636 | ||
637 | case by_name_alignment: | |
85921e9a NC |
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)); | |
50c77e5d NC |
642 | if (ret) |
643 | break; | |
644 | /* Fall through. */ | |
645 | ||
646 | case by_alignment: | |
fd361982 | 647 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
648 | break; |
649 | } | |
650 | ||
651 | return ret; | |
652 | } | |
653 | ||
af31506c MM |
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. */ | |
50c77e5d NC |
668 | |
669 | static lang_section_bst_type ** | |
af31506c MM |
670 | wild_sort (lang_wild_statement_type *wild, |
671 | struct wildcard_list *sec, | |
672 | lang_input_statement_type *file, | |
673 | asection *section) | |
50c77e5d | 674 | { |
329c1c86 | 675 | lang_section_bst_type **tree; |
50c77e5d NC |
676 | |
677 | if (!wild->filenames_sorted | |
af31506c MM |
678 | && (sec == NULL || sec->spec.sorted == none |
679 | || sec->spec.sorted == by_none)) | |
50c77e5d | 680 | { |
af31506c | 681 | /* We might be called even if _this_ spec doesn't need sorting, |
90bea493 | 682 | in which case we simply append at the right end of tree. */ |
af31506c | 683 | return wild->rightmost; |
50c77e5d NC |
684 | } |
685 | ||
af31506c | 686 | tree = &wild->tree; |
329c1c86 | 687 | while (*tree) |
50c77e5d | 688 | { |
af31506c MM |
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 | ||
85921e9a NC |
716 | if (wild->filenames_reversed) |
717 | i = filename_cmp (ln, fn); | |
718 | else | |
719 | i = filename_cmp (fn, ln); | |
720 | ||
af31506c MM |
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 | ||
85921e9a NC |
733 | if (wild->filenames_reversed) |
734 | i = filename_cmp (ln, fn); | |
735 | else | |
736 | i = filename_cmp (fn, ln); | |
90bea493 | 737 | |
af31506c MM |
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 | ||
50c77e5d | 748 | /* Find the correct node to append this section. */ |
b7eab2a9 | 749 | if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none |
85921e9a | 750 | && compare_section (sec->spec.sorted, section, (*tree)->section, sec->spec.reversed) < 0) |
329c1c86 AM |
751 | tree = &((*tree)->left); |
752 | else | |
753 | tree = &((*tree)->right); | |
50c77e5d NC |
754 | } |
755 | ||
756 | return tree; | |
757 | } | |
758 | ||
af31506c | 759 | /* Use wild_sort to build a BST to sort sections. */ |
50c77e5d NC |
760 | |
761 | static void | |
af31506c | 762 | output_section_callback_sort (lang_wild_statement_type *ptr, |
329c1c86 AM |
763 | struct wildcard_list *sec, |
764 | asection *section, | |
765 | lang_input_statement_type *file, | |
d0bf826b | 766 | void *output) |
50c77e5d NC |
767 | { |
768 | lang_section_bst_type *node; | |
769 | lang_section_bst_type **tree; | |
d0bf826b | 770 | lang_output_section_statement_type *os; |
50c77e5d | 771 | |
d0bf826b AM |
772 | os = (lang_output_section_statement_type *) output; |
773 | ||
774 | if (unique_section_p (section, os)) | |
50c77e5d NC |
775 | return; |
776 | ||
670c91c0 MM |
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 | ||
d4115c2c | 782 | node = stat_alloc (sizeof (*node)); |
50c77e5d NC |
783 | node->left = 0; |
784 | node->right = 0; | |
785 | node->section = section; | |
b209b5a6 | 786 | node->pattern = ptr->section_list; |
50c77e5d | 787 | |
af31506c | 788 | tree = wild_sort (ptr, sec, file, section); |
50c77e5d | 789 | if (tree != NULL) |
af31506c MM |
790 | { |
791 | *tree = node; | |
792 | if (tree == ptr->rightmost) | |
793 | ptr->rightmost = &node->right; | |
794 | } | |
50c77e5d NC |
795 | } |
796 | ||
797 | /* Convert a sorted sections' BST back to list form. */ | |
798 | ||
799 | static void | |
329c1c86 AM |
800 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
801 | lang_section_bst_type *tree, | |
802 | void *output) | |
50c77e5d NC |
803 | { |
804 | if (tree->left) | |
805 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
806 | ||
af31506c MM |
807 | lang_add_section (&ptr->children, tree->section, tree->pattern, |
808 | ptr->section_flag_list, | |
329c1c86 | 809 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
810 | |
811 | if (tree->right) | |
812 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
50c77e5d NC |
813 | } |
814 | ||
b1eecf6f MM |
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 | ||
049522ca MM |
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 | ||
b1eecf6f MM |
972 | /* Analyze the wildcards in wild statement PTR to setup various |
973 | things for quick matching. */ | |
72223188 JJ |
974 | |
975 | static void | |
976 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
977 | { | |
72223188 | 978 | struct wildcard_list *sec; |
d7791499 | 979 | |
e6f2cbf5 | 980 | ptr->tree = NULL; |
af31506c | 981 | ptr->rightmost = &ptr->tree; |
72223188 | 982 | |
049522ca MM |
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 | ||
b1eecf6f | 996 | insert_prefix_tree (ptr); |
4dec4d4e RH |
997 | } |
998 | ||
b1eecf6f MM |
999 | /* Match all sections from FILE against the global prefix tree, |
1000 | and record them into each wild statement that has a match. */ | |
1001 | ||
4dec4d4e | 1002 | static void |
b1eecf6f | 1003 | resolve_wild_sections (lang_input_statement_type *file) |
43ae96e9 | 1004 | { |
b1eecf6f MM |
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 | } | |
43ae96e9 MM |
1034 | } |
1035 | ||
b1eecf6f MM |
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); | |
b1eecf6f MM |
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 | ||
b1eecf6f MM |
1070 | /* For each input section that matches wild statement S calls |
1071 | CALLBACK with DATA. */ | |
43ae96e9 MM |
1072 | |
1073 | static void | |
1074 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) | |
1075 | { | |
b1eecf6f | 1076 | lang_statement_union_type *l; |
43ae96e9 | 1077 | |
b1eecf6f | 1078 | for (l = s->matching_sections.head; l; l = l->header.next) |
43ae96e9 | 1079 | { |
b1eecf6f MM |
1080 | (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, |
1081 | l->input_matcher.input_stmt, data); | |
43ae96e9 | 1082 | } |
5f992e62 AM |
1083 | } |
1084 | ||
08da4cac | 1085 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
1086 | function for each node, except those inside output section statements |
1087 | with constraint set to -1. */ | |
252b5132 | 1088 | |
8658f989 | 1089 | void |
1579bae1 AM |
1090 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
1091 | lang_statement_union_type *s) | |
252b5132 | 1092 | { |
1579bae1 | 1093 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
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: | |
8658f989 AM |
1103 | if (s->output_section_statement.constraint != -1) |
1104 | lang_for_each_statement_worker | |
1105 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
1106 | break; |
1107 | case lang_wild_statement_enum: | |
6feb9908 AM |
1108 | lang_for_each_statement_worker (func, |
1109 | s->wild_statement.children.head); | |
252b5132 RH |
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: | |
53d25da6 | 1126 | case lang_insert_statement_enum: |
252b5132 RH |
1127 | break; |
1128 | default: | |
1129 | FAIL (); | |
1130 | break; | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | void | |
1579bae1 | 1136 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1137 | { |
08da4cac | 1138 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1139 | } |
1140 | ||
1141 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1142 | |
252b5132 | 1143 | void |
1579bae1 | 1144 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1145 | { |
1579bae1 | 1146 | list->head = NULL; |
252b5132 RH |
1147 | list->tail = &list->head; |
1148 | } | |
1149 | ||
36983a93 AM |
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 | ||
bde18da4 AM |
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 | ||
08da4cac | 1176 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1177 | |
08da4cac | 1178 | static lang_statement_union_type * |
1579bae1 AM |
1179 | new_statement (enum statement_enum type, |
1180 | size_t size, | |
1181 | lang_statement_list_type *list) | |
252b5132 | 1182 | { |
d3ce72d0 | 1183 | lang_statement_union_type *new_stmt; |
252b5132 | 1184 | |
988de25b | 1185 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
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; | |
252b5132 RH |
1190 | } |
1191 | ||
08da4cac KH |
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. | |
252b5132 | 1195 | |
08da4cac | 1196 | We can be supplied with requests for input files more than once; |
396a2467 | 1197 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1198 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1199 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1200 | |
252b5132 | 1201 | static lang_input_statement_type * |
1579bae1 AM |
1202 | new_afile (const char *name, |
1203 | lang_input_file_enum_type file_type, | |
16171946 FS |
1204 | const char *target, |
1205 | const char *from_filename) | |
252b5132 RH |
1206 | { |
1207 | lang_input_statement_type *p; | |
1208 | ||
f38a2680 | 1209 | lang_has_input_file = true; |
66be1055 | 1210 | |
9cc5af6a PC |
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 | } | |
fb221fba | 1220 | |
1f1f5b92 | 1221 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1222 | memset (&p->the_bfd, 0, |
1223 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
16171946 | 1224 | p->extra_search_path = NULL; |
252b5132 | 1225 | p->target = target; |
66be1055 AM |
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; | |
f4a23d42 | 1230 | p->flags.sysrooted = input_flags.sysrooted; |
a6ad7914 | 1231 | p->sort_key = NULL; |
bcb674cf | 1232 | |
252b5132 RH |
1233 | switch (file_type) |
1234 | { | |
1235 | case lang_input_file_is_symbols_only_enum: | |
1236 | p->filename = name; | |
252b5132 | 1237 | p->local_sym_name = name; |
f38a2680 AM |
1238 | p->flags.real = true; |
1239 | p->flags.just_syms = true; | |
252b5132 RH |
1240 | break; |
1241 | case lang_input_file_is_fake_enum: | |
1242 | p->filename = name; | |
252b5132 | 1243 | p->local_sym_name = name; |
252b5132 RH |
1244 | break; |
1245 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1246 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1247 | { |
1248 | p->filename = name + 1; | |
f38a2680 | 1249 | p->flags.full_name_provided = true; |
0aa7f586 | 1250 | } |
d4ae5fb0 | 1251 | else |
0aa7f586 | 1252 | p->filename = name; |
ff7a0acf | 1253 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
f38a2680 AM |
1254 | p->flags.maybe_archive = true; |
1255 | p->flags.real = true; | |
1256 | p->flags.search_dirs = true; | |
252b5132 RH |
1257 | break; |
1258 | case lang_input_file_is_marker_enum: | |
1259 | p->filename = name; | |
252b5132 | 1260 | p->local_sym_name = name; |
f38a2680 | 1261 | p->flags.search_dirs = true; |
252b5132 RH |
1262 | break; |
1263 | case lang_input_file_is_search_file_enum: | |
1264 | p->filename = name; | |
252b5132 | 1265 | p->local_sym_name = name; |
16171946 | 1266 | /* If name is a relative path, search the directory of the current linker |
90bea493 | 1267 | script first. */ |
16171946 | 1268 | if (from_filename && !IS_ABSOLUTE_PATH (name)) |
27ba92a5 | 1269 | p->extra_search_path = stat_ldirname (from_filename); |
f38a2680 AM |
1270 | p->flags.real = true; |
1271 | p->flags.search_dirs = true; | |
252b5132 RH |
1272 | break; |
1273 | case lang_input_file_is_file_enum: | |
1274 | p->filename = name; | |
252b5132 | 1275 | p->local_sym_name = name; |
f38a2680 | 1276 | p->flags.real = true; |
252b5132 RH |
1277 | break; |
1278 | default: | |
1279 | FAIL (); | |
1280 | } | |
c4b78195 | 1281 | |
36983a93 | 1282 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1283 | return p; |
1284 | } | |
1285 | ||
1286 | lang_input_statement_type * | |
1579bae1 AM |
1287 | lang_add_input_file (const char *name, |
1288 | lang_input_file_enum_type file_type, | |
1289 | const char *target) | |
252b5132 | 1290 | { |
3aa2d05a | 1291 | if (name != NULL |
08dedd66 | 1292 | && (*name == '=' || startswith (name, "$SYSROOT"))) |
bfa23434 HPN |
1293 | { |
1294 | lang_input_statement_type *ret; | |
1295 | char *sysrooted_name | |
3aa2d05a NC |
1296 | = concat (ld_sysroot, |
1297 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1298 | (const char *) NULL); | |
bfa23434 HPN |
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; | |
16171946 | 1309 | ret = new_afile (sysrooted_name, file_type, target, NULL); |
bfa23434 HPN |
1310 | input_flags.sysrooted = outer_sysrooted; |
1311 | return ret; | |
1312 | } | |
1313 | ||
16171946 | 1314 | return new_afile (name, file_type, target, current_input_file); |
252b5132 RH |
1315 | } |
1316 | ||
409d7240 | 1317 | struct out_section_hash_entry |
750877ba L |
1318 | { |
1319 | struct bfd_hash_entry root; | |
409d7240 | 1320 | lang_statement_union_type s; |
a55875bd | 1321 | struct out_section_hash_entry *tail; |
750877ba L |
1322 | }; |
1323 | ||
1324 | /* The hash table. */ | |
1325 | ||
409d7240 | 1326 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1327 | |
384a9dda | 1328 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1329 | initialize the table, fill in an entry and remove the table. */ |
1330 | ||
a55875bd AM |
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) | |
750877ba | 1335 | { |
384a9dda | 1336 | lang_output_section_statement_type **nextp; |
409d7240 | 1337 | struct out_section_hash_entry *ret; |
384a9dda AM |
1338 | |
1339 | if (entry == NULL) | |
1340 | { | |
1e9cc1c2 | 1341 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1342 | sizeof (*ret)); |
384a9dda | 1343 | if (entry == NULL) |
a55875bd | 1344 | return NULL; |
384a9dda AM |
1345 | } |
1346 | ||
1347 | entry = bfd_hash_newfunc (entry, table, string); | |
1348 | if (entry == NULL) | |
a55875bd | 1349 | return NULL; |
384a9dda | 1350 | |
409d7240 AM |
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; | |
3d9c8f6b AM |
1354 | ret->s.output_section_statement.subsection_alignment = NULL; |
1355 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1356 | ret->s.output_section_statement.block_value = 1; |
1357 | lang_list_init (&ret->s.output_section_statement.children); | |
6e8376fa | 1358 | lang_list_init (&ret->s.output_section_statement.sort_children); |
409d7240 | 1359 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); |
384a9dda | 1360 | |
218868ba | 1361 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1362 | first one, lang_os_list.tail points to the "next" |
218868ba | 1363 | field of the last element of the list. */ |
5c1e6d53 | 1364 | if (lang_os_list.head != NULL) |
409d7240 AM |
1365 | ret->s.output_section_statement.prev |
1366 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1367 | ((char *) lang_os_list.tail |
409d7240 | 1368 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1369 | |
384a9dda AM |
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. */ | |
409d7240 | 1373 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1374 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
a55875bd AM |
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; | |
384a9dda | 1389 | return &ret->root; |
750877ba L |
1390 | } |
1391 | ||
1392 | static void | |
409d7240 | 1393 | output_section_statement_table_init (void) |
750877ba | 1394 | { |
409d7240 AM |
1395 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1396 | output_section_statement_newfunc, | |
1397 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1398 | 61)) |
8d97c1a5 | 1399 | fatal (_("%P: can not create hash table: %E\n")); |
750877ba L |
1400 | } |
1401 | ||
1402 | static void | |
409d7240 | 1403 | output_section_statement_table_free (void) |
750877ba | 1404 | { |
409d7240 | 1405 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1406 | } |
1407 | ||
08da4cac KH |
1408 | /* Build enough state so that the parser can build its tree. */ |
1409 | ||
252b5132 | 1410 | void |
9b854f16 | 1411 | lang_init (bool object_only) |
252b5132 | 1412 | { |
9b854f16 L |
1413 | if (!object_only) |
1414 | { | |
1415 | obstack_begin (&stat_obstack, 1000); | |
1416 | obstack_init (&pt_obstack); | |
1417 | } | |
252b5132 RH |
1418 | |
1419 | stat_ptr = &statement_list; | |
1420 | ||
409d7240 | 1421 | output_section_statement_table_init (); |
750877ba | 1422 | |
9b854f16 L |
1423 | cmdline_lists_init (); |
1424 | ||
252b5132 RH |
1425 | lang_list_init (stat_ptr); |
1426 | ||
1427 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1428 | lang_list_init (&lang_os_list); |
252b5132 | 1429 | lang_list_init (&file_chain); |
1579bae1 AM |
1430 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1431 | NULL); | |
08da4cac | 1432 | abs_output_section = |
21401fc7 | 1433 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); |
252b5132 RH |
1434 | |
1435 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1436 | |
16e4ecc0 AM |
1437 | asneeded_list_head = NULL; |
1438 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1439 | } |
1440 | ||
750877ba L |
1441 | void |
1442 | lang_finish (void) | |
1443 | { | |
409d7240 | 1444 | output_section_statement_table_free (); |
df151485 | 1445 | ldfile_free (); |
750877ba L |
1446 | } |
1447 | ||
252b5132 | 1448 | /*---------------------------------------------------------------------- |
08da4cac KH |
1449 | A region is an area of memory declared with the |
1450 | MEMORY { name:org=exp, len=exp ... } | |
1451 | syntax. | |
252b5132 | 1452 | |
08da4cac | 1453 | We maintain a list of all the regions here. |
252b5132 | 1454 | |
08da4cac | 1455 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
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) | |
4a93e180 | 1462 | and so we issue a warning. |
1e0061d2 | 1463 | |
4a93e180 NC |
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. */ | |
252b5132 RH |
1469 | |
1470 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1471 | static lang_memory_region_type **lang_memory_region_list_tail |
1472 | = &lang_memory_region_list; | |
252b5132 RH |
1473 | |
1474 | lang_memory_region_type * | |
f38a2680 | 1475 | lang_memory_region_lookup (const char *const name, bool create) |
252b5132 | 1476 | { |
4a93e180 NC |
1477 | lang_memory_region_name *n; |
1478 | lang_memory_region_type *r; | |
d3ce72d0 | 1479 | lang_memory_region_type *new_region; |
252b5132 | 1480 | |
9f88b410 RS |
1481 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1482 | if (name == NULL) | |
1483 | return NULL; | |
1484 | ||
4a93e180 NC |
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) | |
4724d37e RM |
1488 | { |
1489 | if (create) | |
c1c8c1ef | 1490 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1491 | NULL, name); |
1492 | return r; | |
1493 | } | |
252b5132 | 1494 | |
a747ee4d | 1495 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1496 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1497 | NULL, name); |
a747ee4d | 1498 | |
988de25b | 1499 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1500 | |
d4115c2c | 1501 | new_region->name_list.name = stat_strdup (name); |
d3ce72d0 NC |
1502 | new_region->name_list.next = NULL; |
1503 | new_region->next = NULL; | |
cc9ad334 | 1504 | new_region->origin_exp = NULL; |
d3ce72d0 | 1505 | new_region->origin = 0; |
cc9ad334 | 1506 | new_region->length_exp = NULL; |
d3ce72d0 NC |
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; | |
f38a2680 | 1512 | new_region->had_full_message = false; |
252b5132 | 1513 | |
d3ce72d0 NC |
1514 | *lang_memory_region_list_tail = new_region; |
1515 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1516 | |
d3ce72d0 | 1517 | return new_region; |
252b5132 RH |
1518 | } |
1519 | ||
4a93e180 | 1520 | void |
0aa7f586 | 1521 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1522 | { |
0aa7f586 AM |
1523 | lang_memory_region_name *n; |
1524 | lang_memory_region_type *r; | |
1525 | lang_memory_region_type *region; | |
4a93e180 NC |
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) | |
8d97c1a5 | 1532 | fatal (_("%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
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 | { | |
4724d37e RM |
1540 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1541 | region = r; | |
1542 | if (strcmp (n->name, alias) == 0) | |
8d97c1a5 | 1543 | fatal (_("%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1544 | "alias `%s'\n"), |
1545 | NULL, alias); | |
4a93e180 NC |
1546 | } |
1547 | ||
1548 | /* Check if the target region exists. */ | |
1549 | if (region == NULL) | |
8d97c1a5 | 1550 | fatal (_("%P:%pS: error: memory region `%s' " |
4724d37e RM |
1551 | "for alias `%s' does not exist\n"), |
1552 | NULL, region_name, alias); | |
4a93e180 NC |
1553 | |
1554 | /* Add alias to region name list. */ | |
988de25b | 1555 | n = stat_alloc (sizeof (lang_memory_region_name)); |
d4115c2c | 1556 | n->name = stat_strdup (alias); |
4a93e180 NC |
1557 | n->next = region->name_list.next; |
1558 | region->name_list.next = n; | |
1559 | } | |
1560 | ||
5f992e62 | 1561 | static lang_memory_region_type * |
0aa7f586 | 1562 | lang_memory_default (asection *section) |
252b5132 RH |
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 | ||
1579bae1 | 1572 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1573 | { |
1574 | if ((p->flags & sec_flags) != 0 | |
1575 | && (p->not_flags & sec_flags) == 0) | |
1576 | { | |
1577 | return p; | |
1578 | } | |
1579 | } | |
f38a2680 | 1580 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
252b5132 RH |
1581 | } |
1582 | ||
24ef1aa7 GM |
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 | { | |
a48931cc | 1588 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1589 | } |
1590 | ||
d127ecce AM |
1591 | /* Find or create an output_section_statement with the given NAME. |
1592 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
21401fc7 AM |
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. */ | |
d127ecce | 1597 | |
384a9dda | 1598 | lang_output_section_statement_type * |
d127ecce | 1599 | lang_output_section_statement_lookup (const char *name, |
66c103b7 | 1600 | int constraint, |
21401fc7 | 1601 | int create) |
252b5132 | 1602 | { |
409d7240 | 1603 | struct out_section_hash_entry *entry; |
252b5132 | 1604 | |
409d7240 AM |
1605 | entry = ((struct out_section_hash_entry *) |
1606 | bfd_hash_lookup (&output_section_statement_table, name, | |
f38a2680 | 1607 | create != 0, false)); |
384a9dda AM |
1608 | if (entry == NULL) |
1609 | { | |
66c103b7 | 1610 | if (create) |
8d97c1a5 | 1611 | fatal (_("%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1612 | return NULL; |
1613 | } | |
252b5132 | 1614 | |
409d7240 | 1615 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1616 | { |
384a9dda AM |
1617 | /* We have a section of this name, but it might not have the correct |
1618 | constraint. */ | |
a55875bd | 1619 | struct out_section_hash_entry *first_ent = entry; |
66c103b7 | 1620 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1621 | |
d127ecce | 1622 | name = entry->s.output_section_statement.name; |
a55875bd AM |
1623 | if (create != 2 |
1624 | && !(create && constraint == SPECIAL)) | |
21401fc7 | 1625 | { |
a55875bd AM |
1626 | do |
1627 | { | |
1628 | if (constraint == entry->s.output_section_statement.constraint | |
21401fc7 | 1629 | || (constraint == 0 |
a55875bd AM |
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); | |
21401fc7 | 1637 | } |
a55875bd AM |
1638 | else |
1639 | last_ent = first_ent->tail; | |
252b5132 | 1640 | |
66c103b7 AM |
1641 | if (!create) |
1642 | return NULL; | |
1643 | ||
a55875bd AM |
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); | |
750877ba | 1652 | if (entry == NULL) |
384a9dda | 1653 | { |
8d97c1a5 | 1654 | fatal (_("%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1655 | return NULL; |
1656 | } | |
1657 | entry->root = last_ent->root; | |
1658 | last_ent->root.next = &entry->root; | |
a55875bd | 1659 | first_ent->tail = entry; |
252b5132 | 1660 | } |
384a9dda | 1661 | |
409d7240 AM |
1662 | entry->s.output_section_statement.name = name; |
1663 | entry->s.output_section_statement.constraint = constraint; | |
de34d428 AM |
1664 | entry->s.output_section_statement.dup_output = (create == 2 |
1665 | || constraint == SPECIAL); | |
409d7240 | 1666 | return &entry->s.output_section_statement; |
252b5132 RH |
1667 | } |
1668 | ||
d127ecce AM |
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 | ||
afd7a018 AM |
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, | |
93638471 | 1706 | flagword sec_flags, |
390fbbf1 AM |
1707 | lang_output_section_statement_type **exact, |
1708 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1709 | { |
1710 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1711 | flagword look_flags, differ; |
afd7a018 AM |
1712 | |
1713 | /* We know the first statement on this list is *ABS*. May as well | |
1714 | skip it. */ | |
8ce18f9c | 1715 | first = (void *) lang_os_list.head; |
afd7a018 AM |
1716 | first = first->next; |
1717 | ||
1718 | /* First try for an exact match. */ | |
1719 | found = NULL; | |
1720 | for (look = first; look; look = look->next) | |
1721 | { | |
d9d94ac8 | 1722 | look_flags = look->flags; |
afd7a018 | 1723 | if (look->bfd_section != NULL) |
ecca9871 | 1724 | { |
d9d94ac8 | 1725 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1726 | if (match_type && !match_type (link_info.output_bfd, |
1727 | look->bfd_section, | |
390fbbf1 | 1728 | sec->owner, sec)) |
ecca9871 L |
1729 | continue; |
1730 | } | |
d9d94ac8 AM |
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))) | |
afd7a018 AM |
1734 | found = look; |
1735 | } | |
1736 | if (found != NULL) | |
1737 | { | |
390fbbf1 AM |
1738 | if (exact != NULL) |
1739 | *exact = found; | |
afd7a018 AM |
1740 | return found; |
1741 | } | |
1742 | ||
d9d94ac8 AM |
1743 | if ((sec_flags & SEC_CODE) != 0 |
1744 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1745 | { |
1746 | /* Try for a rw code section. */ | |
1747 | for (look = first; look; look = look->next) | |
1748 | { | |
d9d94ac8 | 1749 | look_flags = look->flags; |
afd7a018 | 1750 | if (look->bfd_section != NULL) |
ecca9871 | 1751 | { |
d9d94ac8 | 1752 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1753 | if (match_type && !match_type (link_info.output_bfd, |
1754 | look->bfd_section, | |
390fbbf1 | 1755 | sec->owner, sec)) |
ecca9871 L |
1756 | continue; |
1757 | } | |
d9d94ac8 AM |
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))) | |
afd7a018 AM |
1761 | found = look; |
1762 | } | |
afd7a018 | 1763 | } |
d9d94ac8 AM |
1764 | else if ((sec_flags & SEC_READONLY) != 0 |
1765 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1766 | { |
1767 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1768 | for (look = first; look; look = look->next) | |
1769 | { | |
d9d94ac8 | 1770 | look_flags = look->flags; |
afd7a018 | 1771 | if (look->bfd_section != NULL) |
ecca9871 | 1772 | { |
d9d94ac8 | 1773 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1774 | if (match_type && !match_type (link_info.output_bfd, |
1775 | look->bfd_section, | |
390fbbf1 | 1776 | sec->owner, sec)) |
ecca9871 L |
1777 | continue; |
1778 | } | |
d9d94ac8 AM |
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))) | |
afd7a018 AM |
1785 | found = look; |
1786 | } | |
afd7a018 | 1787 | } |
d9d94ac8 AM |
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. */ | |
f38a2680 | 1793 | bool seen_thread_local = false; |
d9d94ac8 AM |
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; | |
f38a2680 | 1813 | seen_thread_local = true; |
d9d94ac8 AM |
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) | |
afd7a018 AM |
1823 | { |
1824 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1825 | for (look = first; look; look = look->next) | |
1826 | { | |
d9d94ac8 | 1827 | look_flags = look->flags; |
afd7a018 | 1828 | if (look->bfd_section != NULL) |
ecca9871 | 1829 | { |
d9d94ac8 | 1830 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1831 | if (match_type && !match_type (link_info.output_bfd, |
1832 | look->bfd_section, | |
390fbbf1 | 1833 | sec->owner, sec)) |
ecca9871 L |
1834 | continue; |
1835 | } | |
d9d94ac8 AM |
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))) | |
afd7a018 AM |
1841 | found = look; |
1842 | } | |
afd7a018 | 1843 | } |
d9d94ac8 AM |
1844 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1845 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1846 | { |
1847 | /* .data goes after .rodata. */ | |
1848 | for (look = first; look; look = look->next) | |
1849 | { | |
d9d94ac8 | 1850 | look_flags = look->flags; |
afd7a018 | 1851 | if (look->bfd_section != NULL) |
ecca9871 | 1852 | { |
d9d94ac8 | 1853 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1854 | if (match_type && !match_type (link_info.output_bfd, |
1855 | look->bfd_section, | |
390fbbf1 | 1856 | sec->owner, sec)) |
ecca9871 L |
1857 | continue; |
1858 | } | |
d9d94ac8 AM |
1859 | differ = look_flags ^ sec_flags; |
1860 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1861 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1862 | found = look; |
1863 | } | |
afd7a018 | 1864 | } |
d9d94ac8 | 1865 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1866 | { |
07890c07 | 1867 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1868 | for (look = first; look; look = look->next) |
ecca9871 | 1869 | { |
d9d94ac8 | 1870 | look_flags = look->flags; |
390fbbf1 AM |
1871 | if (look->bfd_section != NULL) |
1872 | { | |
d9d94ac8 | 1873 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1874 | if (match_type && !match_type (link_info.output_bfd, |
1875 | look->bfd_section, | |
390fbbf1 AM |
1876 | sec->owner, sec)) |
1877 | continue; | |
1878 | } | |
d9d94ac8 AM |
1879 | differ = look_flags ^ sec_flags; |
1880 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1881 | found = look; |
ecca9871 | 1882 | } |
afd7a018 | 1883 | } |
07890c07 AM |
1884 | else |
1885 | { | |
1886 | /* non-alloc go last. */ | |
1887 | for (look = first; look; look = look->next) | |
1888 | { | |
d9d94ac8 | 1889 | look_flags = look->flags; |
07890c07 | 1890 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1891 | look_flags = look->bfd_section->flags; |
1892 | differ = look_flags ^ sec_flags; | |
1893 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1894 | found = look; |
1895 | } | |
1896 | return found; | |
1897 | } | |
afd7a018 | 1898 | |
390fbbf1 AM |
1899 | if (found || !match_type) |
1900 | return found; | |
1901 | ||
93638471 | 1902 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
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 | { | |
afd7a018 AM |
1911 | lang_output_section_statement_type *lookup; |
1912 | ||
218868ba | 1913 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1914 | { |
66c103b7 | 1915 | if (lookup->constraint < 0) |
afd7a018 | 1916 | continue; |
afd7a018 | 1917 | |
18e7a658 | 1918 | if (lookup->bfd_section != NULL) |
218868ba | 1919 | return lookup->bfd_section; |
afd7a018 AM |
1920 | } |
1921 | ||
1922 | return NULL; | |
1923 | } | |
1924 | ||
53d25da6 AM |
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 | |
0fa4d7cf AM |
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. */ | |
53d25da6 AM |
1935 | |
1936 | static lang_statement_union_type ** | |
18e7a658 | 1937 | insert_os_after (lang_statement_union_type *after) |
53d25da6 AM |
1938 | { |
1939 | lang_statement_union_type **where; | |
1940 | lang_statement_union_type **assign = NULL; | |
f38a2680 | 1941 | bool ignore_first; |
53d25da6 | 1942 | |
18e7a658 | 1943 | ignore_first = after == lang_os_list.head; |
53d25da6 AM |
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] == '.' | |
73589426 AM |
1959 | && ass->exp->assign.dst[1] == 0) |
1960 | { | |
1961 | if (!ignore_first) | |
1962 | assign = where; | |
f38a2680 | 1963 | ignore_first = false; |
73589426 | 1964 | } |
53d25da6 | 1965 | } |
53d25da6 AM |
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; | |
f38a2680 | 1976 | ignore_first = false; |
53d25da6 AM |
1977 | continue; |
1978 | case lang_output_section_statement_enum: | |
1979 | if (assign != NULL) | |
0fa4d7cf AM |
1980 | { |
1981 | asection *s = (*where)->output_section_statement.bfd_section; | |
1982 | ||
249b2a84 L |
1983 | if (s == NULL |
1984 | || s->map_head.s == NULL | |
1985 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1986 | where = assign; |
1987 | } | |
53d25da6 AM |
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; | |
43ae96e9 MM |
1996 | case lang_input_matcher_enum: |
1997 | FAIL (); | |
53d25da6 AM |
1998 | } |
1999 | break; | |
2000 | } | |
2001 | ||
2002 | return where; | |
2003 | } | |
2004 | ||
afd7a018 | 2005 | lang_output_section_statement_type * |
7b986e99 | 2006 | lang_insert_orphan (asection *s, |
afd7a018 | 2007 | const char *secname, |
8a99a385 | 2008 | int constraint, |
afd7a018 AM |
2009 | lang_output_section_statement_type *after, |
2010 | struct orphan_save *place, | |
2011 | etree_type *address, | |
2012 | lang_statement_list_type *add_child) | |
2013 | { | |
afd7a018 | 2014 | lang_statement_list_type add; |
afd7a018 AM |
2015 | lang_output_section_statement_type *os; |
2016 | lang_output_section_statement_type **os_tail; | |
2017 | ||
afd7a018 AM |
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 | { | |
bde18da4 AM |
2023 | lang_list_init (&add); |
2024 | push_stat_ptr (&add); | |
afd7a018 AM |
2025 | } |
2026 | ||
0e1862bb L |
2027 | if (bfd_link_relocatable (&link_info) |
2028 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
2029 | address = exp_intop (0); |
2030 | ||
5c1e6d53 | 2031 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
c212f39d FS |
2032 | os = lang_enter_output_section_statement ( |
2033 | secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); | |
011aa75f | 2034 | |
afd7a018 AM |
2035 | if (add_child == NULL) |
2036 | add_child = &os->children; | |
b209b5a6 | 2037 | lang_add_section (add_child, s, NULL, NULL, os); |
afd7a018 | 2038 | |
f77c3948 DJ |
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); | |
afd7a018 | 2053 | |
afd7a018 AM |
2054 | /* Restore the global list pointer. */ |
2055 | if (after != NULL) | |
bde18da4 | 2056 | pop_stat_ptr (); |
afd7a018 AM |
2057 | |
2058 | if (after != NULL && os->bfd_section != NULL) | |
2059 | { | |
5daa8fe7 | 2060 | asection *snew, *as; |
f38a2680 AM |
2061 | bool place_after = place->stmt == NULL; |
2062 | bool insert_after = true; | |
afd7a018 AM |
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 | |
8ce18f9c | 2069 | && after != (void *) lang_os_list.head) |
afd7a018 AM |
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 | ||
18e7a658 AM |
2080 | if (bfd_section != NULL |
2081 | && bfd_section->owner != NULL | |
2082 | && bfd_section != snew) | |
afd7a018 AM |
2083 | place->section = &bfd_section->next; |
2084 | } | |
2085 | ||
2086 | if (place->section == NULL) | |
f13a99db | 2087 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 2088 | |
5daa8fe7 | 2089 | as = *place->section; |
5e542ba7 MS |
2090 | |
2091 | if (!as) | |
329c1c86 AM |
2092 | { |
2093 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
2094 | |
2095 | /* Unlink the section. */ | |
f13a99db | 2096 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
2097 | |
2098 | /* Now tack it back on in the right place. */ | |
f13a99db | 2099 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 2100 | } |
1887ae73 L |
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. */ | |
f38a2680 | 2119 | bool after_sec_note = false; |
f3e660db L |
2120 | |
2121 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
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 | { | |
f3e660db L |
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. */ | |
1887ae73 | 2132 | |
f3e660db | 2133 | first_orphan_note = NULL; |
052940eb L |
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 | ||
f3e660db | 2144 | for (sec = link_info.output_bfd->sections; |
1887ae73 L |
2145 | (sec != NULL |
2146 | && !bfd_is_abs_section (sec)); | |
2147 | sec = sec->next) | |
2148 | if (sec != snew | |
2149 | && elf_section_type (sec) == SHT_NOTE | |
052940eb L |
2150 | && (sec->flags & SEC_LOAD) != 0 |
2151 | && (!ignore_build_id | |
2152 | || strcmp (sec->name, ".note.gnu.build-id") != 0)) | |
1887ae73 | 2153 | { |
f3e660db L |
2154 | if (!first_orphan_note) |
2155 | first_orphan_note = sec; | |
1887ae73 L |
2156 | if (sec->alignment_power >= s->alignment_power) |
2157 | after_sec = sec; | |
2158 | } | |
f3e660db L |
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 | } | |
1887ae73 | 2165 | |
f3e660db L |
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) | |
1887ae73 | 2170 | { |
f3e660db L |
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; | |
f38a2680 | 2175 | insert_after = false; |
1887ae73 L |
2176 | } |
2177 | } | |
f3e660db | 2178 | else if (first_orphan_note) |
1887ae73 | 2179 | { |
f3e660db | 2180 | /* Don't place non-note sections in the middle of orphan |
90bea493 | 2181 | note sections. */ |
f38a2680 | 2182 | after_sec_note = true; |
1887ae73 L |
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 | { | |
f3e660db L |
2197 | /* Search forward to insert OS after AFTER_SEC output |
2198 | statement. */ | |
2199 | lang_output_section_statement_type *stmt, *next; | |
f38a2680 | 2200 | bool found = false; |
f3e660db L |
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 | { | |
f38a2680 AM |
2208 | place_after = true; |
2209 | found = true; | |
f3e660db L |
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 | { | |
f38a2680 AM |
2220 | place_after = true; |
2221 | found = true; | |
f3e660db L |
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) | |
1887ae73 | 2232 | { |
f3e660db L |
2233 | if (insert_after) |
2234 | { | |
2235 | if (stmt->bfd_section == after_sec) | |
2236 | { | |
f38a2680 | 2237 | place_after = true; |
f3e660db L |
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 | { | |
f38a2680 | 2248 | place_after = true; |
f3e660db L |
2249 | after = stmt; |
2250 | break; | |
2251 | } | |
2252 | } | |
1887ae73 L |
2253 | } |
2254 | } | |
2255 | ||
f3e660db L |
2256 | if (after_sec == NULL |
2257 | || (insert_after && after_sec->next != snew) | |
2258 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
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) | |
f3e660db L |
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 | } | |
1887ae73 L |
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 | } | |
5e542ba7 | 2288 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2289 | { |
2290 | /* Unlink the section. */ | |
f13a99db | 2291 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2292 | |
5daa8fe7 | 2293 | /* Now tack it back on in the right place. */ |
f13a99db | 2294 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2295 | } |
afd7a018 AM |
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 | ||
1887ae73 L |
2314 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2315 | if (place_after) | |
afd7a018 | 2316 | { |
18e7a658 | 2317 | lang_statement_union_type **where; |
afd7a018 | 2318 | |
18e7a658 | 2319 | where = insert_os_after ((lang_statement_union_type *) after); |
afd7a018 AM |
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. */ | |
bde18da4 AM |
2334 | if (*stat_ptr->tail == add.head) |
2335 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
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; | |
218868ba AM |
2343 | newly_added_os->prev = (lang_output_section_statement_type *) |
2344 | ((char *) place->os_tail | |
2345 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2346 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2347 | if (newly_added_os->next != NULL) |
2348 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
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) | |
5c1e6d53 | 2358 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2359 | } |
2360 | } | |
2361 | return os; | |
2362 | } | |
2363 | ||
16e4ecc0 AM |
2364 | static void |
2365 | lang_print_asneeded (void) | |
2366 | { | |
2367 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2368 | |
2369 | if (asneeded_list_head == NULL) | |
2370 | return; | |
2371 | ||
1273da04 | 2372 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2373 | |
2374 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2375 | { | |
6b9bd54c | 2376 | int len; |
16e4ecc0 AM |
2377 | |
2378 | minfo ("%s", m->soname); | |
2379 | len = strlen (m->soname); | |
2380 | ||
2381 | if (len >= 29) | |
2382 | { | |
2383 | print_nl (); | |
2384 | len = 0; | |
2385 | } | |
6b9bd54c | 2386 | print_spaces (30 - len); |
16e4ecc0 AM |
2387 | |
2388 | if (m->ref != NULL) | |
871b3ab2 | 2389 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2390 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2391 | } |
2392 | } | |
2393 | ||
252b5132 | 2394 | static void |
1579bae1 | 2395 | lang_map_flags (flagword flag) |
252b5132 RH |
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 | |
1579bae1 | 2414 | lang_map (void) |
252b5132 RH |
2415 | { |
2416 | lang_memory_region_type *m; | |
f38a2680 | 2417 | bool dis_header_printed = false; |
252b5132 | 2418 | |
fb221fba NC |
2419 | ldfile_print_input_remaps (); |
2420 | ||
4d4920ec EB |
2421 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2422 | { | |
2423 | asection *s; | |
2424 | ||
2425 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2426 | || file->flags.just_syms) |
4d4920ec EB |
2427 | continue; |
2428 | ||
035801ce FS |
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 | { | |
496917ce | 2437 | minfo (_("\nDiscarded input sections\n\n")); |
f38a2680 | 2438 | dis_header_printed = true; |
035801ce | 2439 | } |
4d4920ec | 2440 | |
f38a2680 | 2441 | print_input_section (s, true); |
035801ce | 2442 | } |
4d4920ec | 2443 | } |
496917ce NC |
2444 | if (config.print_map_discarded && ! dis_header_printed) |
2445 | minfo (_("\nThere are no discarded input sections\n")); | |
4d4920ec | 2446 | |
252b5132 RH |
2447 | minfo (_("\nMemory Configuration\n\n")); |
2448 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2449 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2450 | ||
1579bae1 | 2451 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 | 2452 | { |
6b9bd54c | 2453 | fprintf (config.map_file, "%-16s", m->name_list.name); |
252b5132 | 2454 | |
6b9bd54c AM |
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); | |
252b5132 | 2463 | |
6b9bd54c AM |
2464 | if (m->not_flags) |
2465 | { | |
2466 | minfo ("!"); | |
2467 | lang_map_flags (m->not_flags); | |
252b5132 RH |
2468 | } |
2469 | ||
2470 | print_nl (); | |
2471 | } | |
2472 | ||
496917ce | 2473 | minfo (_("\nLinker script and memory map\n\n")); |
252b5132 | 2474 | |
0aa7f586 | 2475 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2476 | { |
2477 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2478 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2479 | } | |
6a846243 | 2480 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2481 | lang_statement_iteration++; |
252b5132 | 2482 | print_statements (); |
7a2f2d82 | 2483 | |
0aa7f586 AM |
2484 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2485 | config.map_file); | |
252b5132 RH |
2486 | } |
2487 | ||
a965df05 PR |
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 | ||
f38a2680 | 2496 | static bool |
967928e9 AM |
2497 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2498 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2499 | { |
a965df05 | 2500 | if (is_defined (hash_entry) |
6fcc66ab AM |
2501 | && hash_entry->u.def.section->owner != link_info.output_bfd |
2502 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2503 | { |
6fcc66ab | 2504 | input_section_userdata_type *ud; |
35835446 JR |
2505 | struct map_symbol_def *def; |
2506 | ||
a48931cc | 2507 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2508 | if (!ud) |
35835446 | 2509 | { |
988de25b | 2510 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2511 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2512 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2513 | ud->map_symbol_def_count = 0; | |
35835446 | 2514 | } |
6fcc66ab | 2515 | else if (!ud->map_symbol_def_tail) |
35835446 | 2516 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2517 | |
1e9cc1c2 | 2518 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2519 | def->entry = hash_entry; |
76d7af2d | 2520 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2521 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2522 | ud->map_symbol_def_count++; |
35835446 | 2523 | } |
f38a2680 | 2524 | return true; |
35835446 JR |
2525 | } |
2526 | ||
252b5132 RH |
2527 | /* Initialize an output section. */ |
2528 | ||
2529 | static void | |
dfa7b0b8 | 2530 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2531 | { |
252b5132 | 2532 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
8d97c1a5 | 2533 | fatal (_("%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2534 | |
de34d428 | 2535 | if (!s->dup_output) |
8a99a385 | 2536 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); |
1579bae1 | 2537 | if (s->bfd_section == NULL) |
8a99a385 AM |
2538 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2539 | s->name, flags); | |
1579bae1 | 2540 | if (s->bfd_section == NULL) |
252b5132 | 2541 | { |
8d97c1a5 | 2542 | fatal (_("%P: output format %s cannot represent section" |
0aa7f586 | 2543 | " called %s: %E\n"), |
f13a99db | 2544 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2545 | } |
2546 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2547 | s->bfd_section->output_offset = 0; |
e0f6802f | 2548 | |
24ef1aa7 GM |
2549 | /* Set the userdata of the output section to the output section |
2550 | statement to avoid lookup. */ | |
a48931cc | 2551 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2552 | |
a431bc2e AM |
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 | ||
7270c5ed | 2561 | /* If supplied an alignment, set it. */ |
3d9c8f6b | 2562 | if (s->section_alignment != NULL) |
b5c37946 | 2563 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, s, |
3d9c8f6b | 2564 | "section alignment"); |
252b5132 RH |
2565 | } |
2566 | ||
193327db HX |
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 | ||
252b5132 RH |
2581 | /* Make sure that all output sections mentioned in an expression are |
2582 | initialized. */ | |
2583 | ||
2584 | static void | |
1579bae1 | 2585 | exp_init_os (etree_type *exp) |
252b5132 RH |
2586 | { |
2587 | switch (exp->type.node_class) | |
2588 | { | |
2589 | case etree_assign: | |
9f4fb502 | 2590 | case etree_provide: |
eab62f2f | 2591 | case etree_provided: |
252b5132 RH |
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 | ||
b6ca8815 NS |
2606 | case etree_assert: |
2607 | exp_init_os (exp->assert_s.child); | |
2608 | break; | |
afd7a018 | 2609 | |
252b5132 RH |
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: | |
252b5132 RH |
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) | |
193327db | 2624 | init_os (os, get_os_init_flag (os)); |
252b5132 RH |
2625 | } |
2626 | } | |
2627 | break; | |
2628 | ||
2629 | default: | |
2630 | break; | |
2631 | } | |
2632 | } | |
9503fd87 | 2633 | \f |
252b5132 | 2634 | static void |
1579bae1 | 2635 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2636 | { |
1e9cc1c2 | 2637 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2638 | |
2639 | /* If we are only reading symbols from this object, then we want to | |
2640 | discard all sections. */ | |
66be1055 | 2641 | if (entry->flags.just_syms) |
252b5132 | 2642 | { |
1449d79b | 2643 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2644 | return; |
2645 | } | |
2646 | ||
2e84f9c1 AM |
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 | ||
ace66bb2 | 2653 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2654 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2655 | } |
2656 | \f | |
5b5f4e6f AB |
2657 | |
2658 | /* Returns true if SECTION is one we know will be discarded based on its | |
2659 | section flags, otherwise returns false. */ | |
2660 | ||
f38a2680 | 2661 | static bool |
5b5f4e6f AB |
2662 | lang_discard_section_p (asection *section) |
2663 | { | |
f38a2680 | 2664 | bool discard; |
5b5f4e6f AB |
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) | |
f38a2680 | 2674 | discard = true; |
5b5f4e6f AB |
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) | |
f38a2680 | 2680 | discard = true; |
5b5f4e6f | 2681 | |
02c1ba6c L |
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 | ||
5b5f4e6f AB |
2686 | return discard; |
2687 | } | |
2688 | ||
670c91c0 MM |
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. | |
252b5132 | 2692 | |
670c91c0 MM |
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. */ | |
252b5132 | 2697 | |
670c91c0 MM |
2698 | static bool |
2699 | wont_add_section_p (asection *section, | |
2700 | lang_output_section_statement_type *output) | |
252b5132 | 2701 | { |
f38a2680 | 2702 | bool discard; |
252b5132 | 2703 | |
5b5f4e6f AB |
2704 | /* Is this section one we know should be discarded? */ |
2705 | discard = lang_discard_section_p (section); | |
252b5132 RH |
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) | |
f38a2680 | 2710 | discard = true; |
252b5132 | 2711 | |
252b5132 RH |
2712 | if (discard) |
2713 | { | |
2714 | if (section->output_section == NULL) | |
2715 | { | |
670c91c0 MM |
2716 | /* This prevents future calls from assigning this section or |
2717 | warning about it again. */ | |
252b5132 RH |
2718 | section->output_section = bfd_abs_section_ptr; |
2719 | } | |
670c91c0 MM |
2720 | else if (bfd_is_abs_section (section->output_section)) |
2721 | ; | |
abf874aa CL |
2722 | else if (link_info.non_contiguous_regions_warnings) |
2723 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
c63d4862 | 2724 | "section `%pA' from `%pB' match /DISCARD/ clause.\n"), |
abf874aa CL |
2725 | NULL, section, section->owner); |
2726 | ||
670c91c0 | 2727 | return true; |
ae17ab41 CM |
2728 | } |
2729 | ||
dfa7b0b8 | 2730 | if (section->output_section != NULL) |
abf874aa CL |
2731 | { |
2732 | if (!link_info.non_contiguous_regions) | |
670c91c0 | 2733 | return true; |
abf874aa CL |
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)) | |
670c91c0 | 2738 | return true; |
abf874aa CL |
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) | |
670c91c0 | 2744 | return true; |
abf874aa CL |
2745 | |
2746 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2747 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
c63d4862 | 2748 | "change behaviour for section `%pA' from `%pB' (assigned to " |
abf874aa CL |
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 | } | |
252b5132 | 2758 | |
670c91c0 MM |
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 | ||
dfa7b0b8 AM |
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; | |
8423293d | 2802 | |
dfa7b0b8 AM |
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. */ | |
7bdf4127 AB |
2807 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2808 | { | |
2809 | if (link_info.resolve_section_groups) | |
6c19b93b | 2810 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2811 | else |
6c19b93b | 2812 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2813 | } |
2814 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2815 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2816 | |
dfa7b0b8 AM |
2817 | switch (output->sectype) |
2818 | { | |
2819 | case normal_section: | |
2820 | case overlay_section: | |
7b243801 | 2821 | case first_overlay_section: |
c212f39d | 2822 | case type_section: |
dfa7b0b8 AM |
2823 | break; |
2824 | case noalloc_section: | |
2825 | flags &= ~SEC_ALLOC; | |
2826 | break; | |
c212f39d | 2827 | case typed_readonly_section: |
6b86da53 LB |
2828 | case readonly_section: |
2829 | flags |= SEC_READONLY; | |
2830 | break; | |
dfa7b0b8 AM |
2831 | case noload_section: |
2832 | flags &= ~SEC_LOAD; | |
2833 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
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 | |
d87be451 L |
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) | |
2e76e85a | 2841 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2842 | else |
2843 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2844 | break; |
2845 | } | |
252b5132 | 2846 | |
dfa7b0b8 AM |
2847 | if (output->bfd_section == NULL) |
2848 | init_os (output, flags); | |
252b5132 | 2849 | |
dfa7b0b8 AM |
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; | |
252b5132 | 2853 | |
dfa7b0b8 AM |
2854 | if (output->bfd_section->linker_has_input) |
2855 | { | |
2856 | /* Only set SEC_READONLY flag on the first input section. */ | |
2857 | flags &= ~ SEC_READONLY; | |
252b5132 | 2858 | |
f5fa8ca2 | 2859 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
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)) | |
f5fa8ca2 | 2864 | { |
afd7a018 | 2865 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2866 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2867 | } | |
dfa7b0b8 AM |
2868 | } |
2869 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2870 | |
dfa7b0b8 AM |
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) | |
afd7a018 | 2882 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2883 | } |
f5fa8ca2 | 2884 | |
dfa7b0b8 AM |
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 | } | |
252b5132 | 2891 | |
b02db378 AM |
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 | ||
dfa7b0b8 AM |
2902 | if (section->alignment_power > output->bfd_section->alignment_power) |
2903 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2904 | |
dfa7b0b8 | 2905 | section->output_section = output->bfd_section; |
252b5132 | 2906 | |
5b69e357 | 2907 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
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; | |
252b5132 | 2917 | } |
dfa7b0b8 AM |
2918 | |
2919 | /* Add a section reference to the list. */ | |
2920 | new_section = new_stat (lang_input_section, ptr); | |
2921 | new_section->section = section; | |
b209b5a6 | 2922 | new_section->pattern = pattern; |
252b5132 RH |
2923 | } |
2924 | ||
252b5132 | 2925 | /* Expand a wild statement for a particular FILE. SECTION may be |
af31506c MM |
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). */ | |
252b5132 RH |
2928 | |
2929 | static void | |
af31506c MM |
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) | |
4dec4d4e | 2935 | { |
d0bf826b AM |
2936 | lang_output_section_statement_type *os; |
2937 | ||
2938 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2939 | |
b6bf44ba | 2940 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2941 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2942 | return; |
2943 | ||
af31506c MM |
2944 | lang_add_section (&ptr->children, section, ptr->section_list, |
2945 | ptr->section_flag_list, os); | |
252b5132 RH |
2946 | } |
2947 | ||
0841712e JJ |
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, | |
d0bf826b | 2956 | void *output) |
0841712e | 2957 | { |
d0bf826b AM |
2958 | lang_output_section_statement_type *os; |
2959 | ||
2960 | os = (lang_output_section_statement_type *) output; | |
2961 | ||
0841712e | 2962 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2963 | if (unique_section_p (section, os)) |
0841712e JJ |
2964 | return; |
2965 | ||
6feb9908 | 2966 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
f38a2680 | 2967 | os->all_input_readonly = false; |
0841712e JJ |
2968 | } |
2969 | ||
252b5132 RH |
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 * | |
1579bae1 | 2975 | lookup_name (const char *name) |
252b5132 RH |
2976 | { |
2977 | lang_input_statement_type *search; | |
2978 | ||
8ce18f9c | 2979 | for (search = (void *) input_file_chain.head; |
1579bae1 | 2980 | search != NULL; |
36983a93 | 2981 | search = search->next_real_file) |
252b5132 | 2982 | { |
0013291d NC |
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. */ | |
87aa7f19 | 2986 | const char *filename = search->local_sym_name; |
0013291d | 2987 | |
0013291d | 2988 | if (filename != NULL |
42627821 | 2989 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2990 | break; |
2991 | } | |
2992 | ||
1579bae1 | 2993 | if (search == NULL) |
1f1f5b92 AM |
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, | |
16171946 | 3008 | default_target, NULL); |
1f1f5b92 AM |
3009 | *stat_ptr->tail = rest; |
3010 | if (*tail == NULL) | |
3011 | stat_ptr->tail = tail; | |
3012 | } | |
252b5132 RH |
3013 | |
3014 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 3015 | don't add this file. */ |
66be1055 | 3016 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
3017 | return search; |
3018 | ||
0aa7f586 | 3019 | if (!load_symbols (search, NULL)) |
6770ec8c | 3020 | return NULL; |
252b5132 RH |
3021 | |
3022 | return search; | |
3023 | } | |
3024 | ||
b58f81ae DJ |
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); | |
d4115c2c | 3045 | entry = stat_alloc (sizeof (*entry)); |
b58f81ae | 3046 | entry->next = excluded_libs; |
d4115c2c | 3047 | entry->name = stat_memdup (p, end - p, end - p + 1); |
b58f81ae DJ |
3048 | excluded_libs = entry; |
3049 | if (*end == '\0') | |
329c1c86 | 3050 | break; |
b58f81ae DJ |
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); | |
607b4833 | 3063 | const char *filename = lbasename (bfd_get_filename (abfd)); |
b58f81ae DJ |
3064 | |
3065 | if (strcmp (lib->name, "ALL") == 0) | |
3066 | { | |
f38a2680 | 3067 | abfd->no_export = true; |
b58f81ae DJ |
3068 | return; |
3069 | } | |
3070 | ||
42627821 | 3071 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
3072 | && (filename[len] == '\0' |
3073 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
3074 | && filename[len + 2] == '\0'))) | |
3075 | { | |
f38a2680 | 3076 | abfd->no_export = true; |
b58f81ae DJ |
3077 | return; |
3078 | } | |
3079 | ||
3080 | lib = lib->next; | |
3081 | } | |
3082 | } | |
3083 | ||
252b5132 RH |
3084 | /* Get the symbols for an input file. */ |
3085 | ||
f38a2680 | 3086 | bool |
1579bae1 AM |
3087 | load_symbols (lang_input_statement_type *entry, |
3088 | lang_statement_list_type *place) | |
252b5132 RH |
3089 | { |
3090 | char **matching; | |
3091 | ||
66be1055 | 3092 | if (entry->flags.loaded) |
f38a2680 | 3093 | return true; |
252b5132 RH |
3094 | |
3095 | ldfile_open_file (entry); | |
3096 | ||
c4b78195 | 3097 | /* Do not process further if the file was missing. */ |
66be1055 | 3098 | if (entry->flags.missing_file) |
f38a2680 | 3099 | return true; |
c4b78195 | 3100 | |
727a29ba AM |
3101 | if (trace_files || verbose) |
3102 | info_msg ("%pI\n", entry); | |
3103 | ||
0aa7f586 AM |
3104 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
3105 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
3106 | { |
3107 | bfd_error_type err; | |
66be1055 | 3108 | struct lang_input_statement_flags save_flags; |
47e2e729 | 3109 | extern FILE *yyin; |
b7a26f91 | 3110 | |
252b5132 | 3111 | err = bfd_get_error (); |
884fb58e NC |
3112 | |
3113 | /* See if the emulation has some special knowledge. */ | |
3114 | if (ldemul_unrecognized_file (entry)) | |
370426d0 AM |
3115 | { |
3116 | if (err == bfd_error_file_ambiguously_recognized) | |
3117 | free (matching); | |
3118 | return true; | |
3119 | } | |
884fb58e | 3120 | |
252b5132 RH |
3121 | if (err == bfd_error_file_ambiguously_recognized) |
3122 | { | |
3123 | char **p; | |
3124 | ||
df5f2391 AM |
3125 | einfo (_("%P: %pB: file not recognized: %E;" |
3126 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
3127 | for (p = matching; *p != NULL; p++) |
3128 | einfo (" %s", *p); | |
370426d0 | 3129 | free (matching); |
8d97c1a5 | 3130 | fatal ("\n"); |
252b5132 RH |
3131 | } |
3132 | else if (err != bfd_error_file_not_recognized | |
3133 | || place == NULL) | |
8d97c1a5 | 3134 | fatal (_("%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 3135 | |
252b5132 RH |
3136 | bfd_close (entry->the_bfd); |
3137 | entry->the_bfd = NULL; | |
3138 | ||
252b5132 | 3139 | /* Try to interpret the file as a linker script. */ |
66be1055 | 3140 | save_flags = input_flags; |
252b5132 RH |
3141 | ldfile_open_command_file (entry->filename); |
3142 | ||
bde18da4 | 3143 | push_stat_ptr (place); |
66be1055 AM |
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; | |
252b5132 | 3150 | |
f38a2680 | 3151 | ldfile_assumed_script = true; |
252b5132 | 3152 | parser_input = input_script; |
16171946 | 3153 | current_input_file = entry->filename; |
252b5132 | 3154 | yyparse (); |
16171946 | 3155 | current_input_file = NULL; |
f38a2680 | 3156 | ldfile_assumed_script = false; |
252b5132 | 3157 | |
f4a23d42 AM |
3158 | /* missing_file is sticky. sysrooted will already have been |
3159 | restored when seeing EOF in yyparse, but no harm to restore | |
3160 | again. */ | |
66be1055 AM |
3161 | save_flags.missing_file |= input_flags.missing_file; |
3162 | input_flags = save_flags; | |
bde18da4 | 3163 | pop_stat_ptr (); |
47e2e729 AM |
3164 | fclose (yyin); |
3165 | yyin = NULL; | |
f38a2680 | 3166 | entry->flags.loaded = true; |
252b5132 | 3167 | |
f38a2680 | 3168 | return true; |
252b5132 RH |
3169 | } |
3170 | ||
3171 | if (ldemul_recognized_file (entry)) | |
f38a2680 | 3172 | return true; |
252b5132 RH |
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: | |
66be1055 | 3184 | if (!entry->flags.reload) |
15fc2e13 | 3185 | ldlang_add_file (entry); |
252b5132 RH |
3186 | break; |
3187 | ||
3188 | case bfd_archive: | |
b58f81ae DJ |
3189 | check_excluded_libs (entry->the_bfd); |
3190 | ||
00f93c44 | 3191 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 3192 | if (entry->flags.whole_archive) |
252b5132 | 3193 | { |
b7a26f91 | 3194 | bfd *member = NULL; |
f38a2680 | 3195 | bool loaded = true; |
6770ec8c NC |
3196 | |
3197 | for (;;) | |
252b5132 | 3198 | { |
5d3236ee | 3199 | bfd *subsbfd; |
6770ec8c NC |
3200 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
3201 | ||
3202 | if (member == NULL) | |
3203 | break; | |
b7a26f91 | 3204 | |
0aa7f586 | 3205 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 3206 | { |
8d97c1a5 | 3207 | fatal (_("%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 3208 | entry->the_bfd, member); |
f38a2680 | 3209 | loaded = false; |
6770ec8c NC |
3210 | } |
3211 | ||
9b854f16 L |
3212 | if (config.emitting_gnu_object_only) |
3213 | { | |
3214 | if (!cmdline_on_object_only_archive_list_p (member)) | |
3215 | continue; | |
3216 | } | |
3217 | ||
db0ca79f | 3218 | subsbfd = member; |
46105645 AM |
3219 | if (!(*link_info.callbacks |
3220 | ->add_archive_element) (&link_info, member, | |
3221 | "--whole-archive", &subsbfd)) | |
252b5132 | 3222 | abort (); |
6770ec8c | 3223 | |
5d3236ee DK |
3224 | /* Potentially, the add_archive_element hook may have set a |
3225 | substitute BFD for us. */ | |
46105645 | 3226 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 3227 | { |
8d97c1a5 | 3228 | fatal (_("%P: %pB: error adding symbols: %E\n"), member); |
f38a2680 | 3229 | loaded = false; |
6770ec8c | 3230 | } |
252b5132 RH |
3231 | } |
3232 | ||
66be1055 | 3233 | entry->flags.loaded = loaded; |
6770ec8c | 3234 | return loaded; |
252b5132 | 3235 | } |
6770ec8c | 3236 | break; |
252b5132 RH |
3237 | } |
3238 | ||
03bdc404 | 3239 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
f38a2680 | 3240 | entry->flags.loaded = true; |
6770ec8c | 3241 | else |
8d97c1a5 | 3242 | fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3243 | |
66be1055 | 3244 | return entry->flags.loaded; |
252b5132 RH |
3245 | } |
3246 | ||
b6bf44ba AM |
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. */ | |
252b5132 RH |
3252 | |
3253 | static void | |
1579bae1 AM |
3254 | wild (lang_wild_statement_type *s, |
3255 | const char *target ATTRIBUTE_UNUSED, | |
3256 | lang_output_section_statement_type *output) | |
252b5132 | 3257 | { |
b6bf44ba | 3258 | struct wildcard_list *sec; |
252b5132 | 3259 | |
af31506c | 3260 | if (s->filenames_sorted || s->any_specs_sorted) |
50c77e5d | 3261 | { |
329c1c86 AM |
3262 | lang_section_bst_type *tree; |
3263 | ||
af31506c | 3264 | walk_wild (s, output_section_callback_sort, output); |
50c77e5d | 3265 | |
e6f2cbf5 | 3266 | tree = s->tree; |
329c1c86 | 3267 | if (tree) |
e6f2cbf5 L |
3268 | { |
3269 | output_section_callback_tree_to_list (s, tree, output); | |
3270 | s->tree = NULL; | |
af31506c | 3271 | s->rightmost = &s->tree; |
e6f2cbf5 | 3272 | } |
50c77e5d | 3273 | } |
329c1c86 | 3274 | else |
af31506c | 3275 | walk_wild (s, output_section_callback_nosort, output); |
b6bf44ba | 3276 | |
abe6ac95 AM |
3277 | if (default_common_section == NULL) |
3278 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
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 | |
b7a26f91 | 3282 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3283 | default_common_section = output; |
abe6ac95 | 3284 | break; |
b6bf44ba | 3285 | } |
252b5132 RH |
3286 | } |
3287 | ||
b34976b6 | 3288 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3289 | |
e50d8076 | 3290 | static int |
1579bae1 | 3291 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3292 | { |
1e9cc1c2 | 3293 | const char *sought = (const char *) data; |
5f992e62 | 3294 | |
e50d8076 NC |
3295 | return strcmp (target->name, sought) == 0; |
3296 | } | |
3297 | ||
3298 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3299 | |
e50d8076 | 3300 | static void |
1d38e9d1 | 3301 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3302 | { |
3303 | char c; | |
5f992e62 | 3304 | |
08da4cac | 3305 | while ((c = *src++) != 0) |
3882b010 | 3306 | *dest++ = TOLOWER (c); |
e50d8076 | 3307 | |
08da4cac | 3308 | *dest = 0; |
e50d8076 NC |
3309 | } |
3310 | ||
396a2467 | 3311 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3312 | from haystack. */ |
08da4cac | 3313 | |
e50d8076 | 3314 | static void |
1d38e9d1 | 3315 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3316 | { |
3317 | haystack = strstr (haystack, needle); | |
5f992e62 | 3318 | |
e50d8076 NC |
3319 | if (haystack) |
3320 | { | |
08da4cac | 3321 | char *src; |
e50d8076 | 3322 | |
08da4cac KH |
3323 | for (src = haystack + strlen (needle); *src;) |
3324 | *haystack++ = *src++; | |
5f992e62 | 3325 | |
08da4cac | 3326 | *haystack = 0; |
e50d8076 NC |
3327 | } |
3328 | } | |
3329 | ||
3330 | /* Compare two target format name strings. | |
3331 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3332 | |
e50d8076 | 3333 | static int |
1d38e9d1 | 3334 | name_compare (const char *first, const char *second) |
e50d8076 | 3335 | { |
08da4cac KH |
3336 | char *copy1; |
3337 | char *copy2; | |
3338 | int result; | |
5f992e62 | 3339 | |
1e9cc1c2 NC |
3340 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3341 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3342 | |
3343 | /* Convert the names to lower case. */ | |
3344 | stricpy (copy1, first); | |
3345 | stricpy (copy2, second); | |
3346 | ||
1579bae1 | 3347 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
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. */ | |
08da4cac KH |
3356 | for (result = 0; copy1[result] == copy2[result]; result++) |
3357 | if (copy1[result] == 0) | |
e50d8076 NC |
3358 | { |
3359 | result *= 10; | |
3360 | break; | |
3361 | } | |
5f992e62 | 3362 | |
e50d8076 NC |
3363 | free (copy1); |
3364 | free (copy2); | |
3365 | ||
3366 | return result; | |
3367 | } | |
3368 | ||
3369 | /* Set by closest_target_match() below. */ | |
08da4cac | 3370 | static const bfd_target *winner; |
e50d8076 NC |
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. */ | |
08da4cac | 3375 | |
e50d8076 | 3376 | static int |
1579bae1 | 3377 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3378 | { |
1e9cc1c2 | 3379 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3380 | |
08da4cac KH |
3381 | if (command_line.endian == ENDIAN_BIG |
3382 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3383 | return 0; |
5f992e62 | 3384 | |
08da4cac KH |
3385 | if (command_line.endian == ENDIAN_LITTLE |
3386 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3387 | return 0; |
3388 | ||
3389 | /* Must be the same flavour. */ | |
3390 | if (target->flavour != original->flavour) | |
3391 | return 0; | |
3392 | ||
de7dd2bd | 3393 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3394 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
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 | ||
e50d8076 NC |
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. | |
4de2d33d | 3408 | Compare their names and choose the better one. */ |
d1778b88 AM |
3409 | if (name_compare (target->name, original->name) |
3410 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
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. */ | |
08da4cac | 3418 | |
1d38e9d1 | 3419 | static const char * |
1579bae1 | 3420 | get_first_input_target (void) |
e50d8076 | 3421 | { |
1d38e9d1 | 3422 | const char *target = NULL; |
e50d8076 NC |
3423 | |
3424 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3425 | { | |
3426 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3427 | && s->flags.real) |
e50d8076 NC |
3428 | { |
3429 | ldfile_open_file (s); | |
5f992e62 | 3430 | |
e50d8076 NC |
3431 | if (s->the_bfd != NULL |
3432 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3433 | { | |
3434 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3435 | |
e50d8076 NC |
3436 | if (target != NULL) |
3437 | break; | |
3438 | } | |
3439 | } | |
3440 | } | |
5f992e62 | 3441 | |
e50d8076 NC |
3442 | return target; |
3443 | } | |
3444 | ||
599917b8 | 3445 | const char * |
1579bae1 | 3446 | lang_get_output_target (void) |
599917b8 JJ |
3447 | { |
3448 | const char *target; | |
3449 | ||
3450 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3451 | if (output_target != NULL) |
599917b8 JJ |
3452 | return output_target; |
3453 | ||
3454 | /* No - has the current target been set to something other than | |
3455 | the default? */ | |
30824704 | 3456 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
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 | ||
252b5132 RH |
3468 | /* Open the output file. */ |
3469 | ||
f13a99db | 3470 | static void |
1579bae1 | 3471 | open_output (const char *name) |
252b5132 | 3472 | { |
43908c16 AM |
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 | { | |
da1ecf89 | 3481 | char *in = lrealpath (f->local_sym_name); |
43908c16 | 3482 | if (filename_cmp (in, out) == 0) |
8d97c1a5 | 3483 | fatal (_("%P: input file '%s' is the same as output file\n"), |
43908c16 AM |
3484 | f->filename); |
3485 | free (in); | |
3486 | } | |
3487 | free (out); | |
3488 | ||
599917b8 | 3489 | output_target = lang_get_output_target (); |
5f992e62 | 3490 | |
08da4cac KH |
3491 | /* Has the user requested a particular endianness on the command |
3492 | line? */ | |
e50d8076 NC |
3493 | if (command_line.endian != ENDIAN_UNSET) |
3494 | { | |
e50d8076 | 3495 | /* Get the chosen target. */ |
4212b42d AM |
3496 | const bfd_target *target |
3497 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3498 | |
c13b1b77 NC |
3499 | /* If the target is not supported, we cannot do anything. */ |
3500 | if (target != NULL) | |
e50d8076 | 3501 | { |
4212b42d AM |
3502 | enum bfd_endian desired_endian; |
3503 | ||
c13b1b77 NC |
3504 | if (command_line.endian == ENDIAN_BIG) |
3505 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3506 | else |
c13b1b77 | 3507 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
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. */ | |
c13b1b77 | 3513 | if (target->byteorder != desired_endian) |
e50d8076 | 3514 | { |
c13b1b77 NC |
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; | |
e50d8076 | 3520 | else |
c13b1b77 | 3521 | { |
5f992e62 AM |
3522 | /* Try to find a target as similar as possible to |
3523 | the default target, but which has the desired | |
3524 | endian characteristic. */ | |
4212b42d AM |
3525 | bfd_iterate_over_targets (closest_target_match, |
3526 | (void *) target); | |
5f992e62 AM |
3527 | |
3528 | /* Oh dear - we could not find any targets that | |
3529 | satisfy our requirements. */ | |
c13b1b77 | 3530 | if (winner == NULL) |
6feb9908 AM |
3531 | einfo (_("%P: warning: could not find any targets" |
3532 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3533 | else |
3534 | output_target = winner->name; | |
3535 | } | |
e50d8076 NC |
3536 | } |
3537 | } | |
252b5132 | 3538 | } |
5f992e62 | 3539 | |
f13a99db | 3540 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3541 | |
f13a99db | 3542 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3543 | { |
3544 | if (bfd_get_error () == bfd_error_invalid_target) | |
8d97c1a5 | 3545 | fatal (_("%P: target %s not found\n"), output_target); |
e50d8076 | 3546 | |
8d97c1a5 | 3547 | fatal (_("%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3548 | } |
3549 | ||
f38a2680 | 3550 | delete_output_file_on_failure = true; |
252b5132 | 3551 | |
0aa7f586 | 3552 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
8d97c1a5 | 3553 | fatal (_("%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3554 | if (!bfd_set_arch_mach (link_info.output_bfd, |
8d97c1a5 AM |
3555 | ldfile_output_architecture, |
3556 | ldfile_output_machine)) | |
3557 | fatal (_("%P: %s: can not set architecture: %E\n"), name); | |
252b5132 | 3558 | |
f13a99db | 3559 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3560 | if (link_info.hash == NULL) |
8d97c1a5 | 3561 | fatal (_("%P: can not create hash table: %E\n")); |
252b5132 | 3562 | |
f13a99db | 3563 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3564 | } |
3565 | ||
252b5132 | 3566 | static void |
1579bae1 | 3567 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3568 | { |
3569 | switch (statement->header.type) | |
3570 | { | |
3571 | case lang_output_statement_enum: | |
f13a99db AM |
3572 | ASSERT (link_info.output_bfd == NULL); |
3573 | open_output (statement->output_statement.name); | |
252b5132 | 3574 | ldemul_set_output_arch (); |
0e1862bb L |
3575 | if (config.magic_demand_paged |
3576 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3577 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3578 | else |
f13a99db | 3579 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3580 | if (config.text_read_only) |
f13a99db | 3581 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3582 | else |
f13a99db | 3583 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3584 | if (link_info.traditional_format) |
f13a99db | 3585 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3586 | else |
f13a99db | 3587 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
96cc7918 KB |
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; | |
252b5132 RH |
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 | ||
e5caa5e0 | 3602 | static void |
61826503 | 3603 | init_opb (asection *s) |
e5caa5e0 | 3604 | { |
bb294208 | 3605 | unsigned int x; |
61826503 | 3606 | |
e5caa5e0 | 3607 | opb_shift = 0; |
bb294208 AM |
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); | |
e5caa5e0 AM |
3615 | if (x > 1) |
3616 | while ((x & 1) == 0) | |
3617 | { | |
3618 | x >>= 1; | |
3619 | ++opb_shift; | |
3620 | } | |
3621 | ASSERT (x == 1); | |
3622 | } | |
3623 | ||
252b5132 RH |
3624 | /* Open all the input files. */ |
3625 | ||
486329aa AM |
3626 | enum open_bfd_mode |
3627 | { | |
3628 | OPEN_BFD_NORMAL = 0, | |
3629 | OPEN_BFD_FORCE = 1, | |
3630 | OPEN_BFD_RESCAN = 2 | |
3631 | }; | |
0381901e | 3632 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 | 3633 | static lang_input_statement_type *plugin_insert = NULL; |
b02c4f16 | 3634 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3635 | #endif |
486329aa | 3636 | |
252b5132 | 3637 | static void |
b5c37946 SJ |
3638 | open_input_bfds (lang_statement_union_type *s, |
3639 | lang_output_section_statement_type *os, | |
3640 | enum open_bfd_mode mode) | |
252b5132 | 3641 | { |
1579bae1 | 3642 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3643 | { |
3644 | switch (s->header.type) | |
3645 | { | |
3646 | case lang_constructors_statement_enum: | |
b5c37946 | 3647 | open_input_bfds (constructor_list.head, os, mode); |
252b5132 RH |
3648 | break; |
3649 | case lang_output_section_statement_enum: | |
b5c37946 SJ |
3650 | os = &s->output_section_statement; |
3651 | open_input_bfds (os->children.head, os, mode); | |
252b5132 RH |
3652 | break; |
3653 | case lang_wild_statement_enum: | |
08da4cac | 3654 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3655 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3656 | && s->wild_statement.filename | |
97407faf AM |
3657 | && !wildcardp (s->wild_statement.filename) |
3658 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3659 | lookup_name (s->wild_statement.filename); |
b5c37946 | 3660 | open_input_bfds (s->wild_statement.children.head, os, mode); |
252b5132 RH |
3661 | break; |
3662 | case lang_group_statement_enum: | |
3663 | { | |
3664 | struct bfd_link_hash_entry *undefs; | |
0381901e | 3665 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3666 | lang_input_statement_type *plugin_insert_save; |
3667 | #endif | |
252b5132 RH |
3668 | |
3669 | /* We must continually search the entries in the group | |
08da4cac KH |
3670 | until no new symbols are added to the list of undefined |
3671 | symbols. */ | |
252b5132 RH |
3672 | |
3673 | do | |
3674 | { | |
0381901e | 3675 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3676 | plugin_insert_save = plugin_insert; |
3677 | #endif | |
252b5132 | 3678 | undefs = link_info.hash->undefs_tail; |
b5c37946 | 3679 | open_input_bfds (s->group_statement.children.head, os, |
486329aa | 3680 | mode | OPEN_BFD_FORCE); |
252b5132 | 3681 | } |
b02c4f16 | 3682 | while (undefs != link_info.hash->undefs_tail |
0381901e | 3683 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
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 | ); | |
252b5132 RH |
3690 | } |
3691 | break; | |
3692 | case lang_target_statement_enum: | |
3693 | current_target = s->target_statement.target; | |
3694 | break; | |
3695 | case lang_input_statement_enum: | |
66be1055 | 3696 | if (s->input_statement.flags.real) |
252b5132 | 3697 | { |
bde18da4 | 3698 | lang_statement_union_type **os_tail; |
252b5132 | 3699 | lang_statement_list_type add; |
d215621e | 3700 | bfd *abfd; |
252b5132 RH |
3701 | |
3702 | s->input_statement.target = current_target; | |
3703 | ||
3704 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3705 | is an archive which has already been searched, then |
3706 | force it to be researched unless the whole archive | |
d215621e AM |
3707 | has been loaded already. Do the same for a rescan. |
3708 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3709 | if (mode != OPEN_BFD_NORMAL |
0381901e | 3710 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3711 | && ((mode & OPEN_BFD_RESCAN) == 0 |
3712 | || plugin_insert == NULL) | |
3713 | #endif | |
66be1055 | 3714 | && s->input_statement.flags.loaded |
d215621e AM |
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 | |
e77620a5 | 3721 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3722 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3723 | { |
f38a2680 AM |
3724 | s->input_statement.flags.loaded = false; |
3725 | s->input_statement.flags.reload = true; | |
15fc2e13 | 3726 | } |
252b5132 | 3727 | |
5c1e6d53 | 3728 | os_tail = lang_os_list.tail; |
d1778b88 | 3729 | lang_list_init (&add); |
1276aefa | 3730 | |
0aa7f586 | 3731 | if (!load_symbols (&s->input_statement, &add)) |
f38a2680 | 3732 | config.make_executable = false; |
252b5132 RH |
3733 | |
3734 | if (add.head != NULL) | |
3735 | { | |
bde18da4 AM |
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. */ | |
5c1e6d53 | 3742 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
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 | } | |
252b5132 RH |
3755 | } |
3756 | } | |
0381901e | 3757 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
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 | |
252b5132 | 3763 | break; |
e759c116 | 3764 | case lang_assignment_statement_enum: |
165f707a | 3765 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
b5c37946 | 3766 | exp_fold_tree_no_dot (s->assignment_statement.exp, os); |
e759c116 | 3767 | break; |
252b5132 RH |
3768 | default: |
3769 | break; | |
3770 | } | |
3771 | } | |
c4b78195 NC |
3772 | |
3773 | /* Exit if any of the files were missing. */ | |
66be1055 | 3774 | if (input_flags.missing_file) |
8d97c1a5 | 3775 | fatal (""); |
252b5132 RH |
3776 | } |
3777 | ||
094e34f2 | 3778 | #ifdef ENABLE_LIBCTF |
926c9e76 NA |
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 | |
139633c3 | 3782 | lang_ctf_errs_warnings (ctf_dict_t *fp) |
926c9e76 NA |
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 | { | |
5e9b84f7 | 3791 | einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), |
926c9e76 NA |
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 | ||
1ff6de03 NA |
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 | ||
c4fce3ef NC |
3817 | ld_start_phase (PHASE_CTF); |
3818 | ||
1ff6de03 NA |
3819 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
3820 | { | |
3821 | asection *sect; | |
3822 | ||
139633c3 | 3823 | /* Incoming files from the compiler have a single ctf_dict_t in them |
1ff6de03 NA |
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) | |
926c9e76 NA |
3831 | { |
3832 | lang_ctf_errs_warnings (NULL); | |
3833 | einfo (_("%P: warning: CTF section in %pB not loaded; " | |
5e9b84f7 | 3834 | "its types will be discarded: %s\n"), file->the_bfd, |
1ff6de03 | 3835 | ctf_errmsg (err)); |
926c9e76 | 3836 | } |
1ff6de03 NA |
3837 | continue; |
3838 | } | |
3839 | ||
3840 | /* Prevent the contents of this section from being written, while | |
fa03171f NA |
3841 | requiring the section itself to be duplicated in the output, but only |
3842 | once. */ | |
1ff6de03 NA |
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 | ||
fa03171f NA |
3848 | if (any_ctf) |
3849 | sect->flags |= SEC_EXCLUDE; | |
1ff6de03 NA |
3850 | any_ctf = 1; |
3851 | } | |
3852 | ||
3853 | if (!any_ctf) | |
3854 | { | |
3855 | ctf_output = NULL; | |
c4fce3ef | 3856 | ld_stop_phase (PHASE_CTF); |
1ff6de03 NA |
3857 | return; |
3858 | } | |
3859 | ||
3860 | if ((ctf_output = ctf_create (&err)) != NULL) | |
c4fce3ef NC |
3861 | { |
3862 | ld_stop_phase (PHASE_CTF); | |
3863 | return; | |
3864 | } | |
1ff6de03 | 3865 | |
c48acf6f | 3866 | einfo (_("%P: warning: CTF output not created: `%s'\n"), |
1ff6de03 NA |
3867 | ctf_errmsg (err)); |
3868 | ||
3869 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3870 | ctf_close (errfile->the_ctf); | |
c4fce3ef NC |
3871 | |
3872 | ld_stop_phase (PHASE_CTF); | |
1ff6de03 NA |
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; | |
5dba6f05 | 3882 | int flags = 0; |
1ff6de03 NA |
3883 | |
3884 | if (!ctf_output) | |
3885 | return; | |
3886 | ||
c4fce3ef NC |
3887 | ld_start_phase (PHASE_CTF); |
3888 | ||
1ff6de03 NA |
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 | { | |
139633c3 | 3894 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
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 | } | |
c4fce3ef NC |
3902 | |
3903 | ld_stop_phase (PHASE_CTF); | |
1ff6de03 NA |
3904 | return; |
3905 | } | |
3906 | ||
3907 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3908 | { | |
3909 | if (!file->the_ctf) | |
3910 | continue; | |
3911 | ||
f320bba5 | 3912 | /* Takes ownership of file->the_ctf. */ |
1ff6de03 NA |
3913 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) |
3914 | { | |
f320bba5 EB |
3915 | einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), |
3916 | file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 NA |
3917 | ctf_close (file->the_ctf); |
3918 | file->the_ctf = NULL; | |
3919 | continue; | |
3920 | } | |
3921 | } | |
3922 | ||
5dba6f05 NA |
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; | |
35a01a04 NA |
3929 | if (bfd_link_relocatable (&link_info)) |
3930 | flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; | |
5dba6f05 NA |
3931 | |
3932 | if (ctf_link (ctf_output, flags) < 0) | |
1ff6de03 | 3933 | { |
926c9e76 | 3934 | lang_ctf_errs_warnings (ctf_output); |
f320bba5 | 3935 | einfo (_("%P: warning: CTF linking failed; " |
5e9b84f7 | 3936 | "output will have no CTF section: %s\n"), |
1ff6de03 NA |
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 | } | |
926c9e76 | 3944 | /* Output any lingering errors that didn't come from ctf_link. */ |
8b37e7b6 | 3945 | lang_ctf_errs_warnings (ctf_output); |
c4fce3ef NC |
3946 | |
3947 | ld_stop_phase (PHASE_CTF); | |
1ff6de03 NA |
3948 | } |
3949 | ||
3d16b64e NA |
3950 | /* Let the emulation acquire strings from the dynamic strtab to help it optimize |
3951 | the CTF, if supported. */ | |
1ff6de03 NA |
3952 | |
3953 | void | |
3d16b64e | 3954 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) |
1ff6de03 | 3955 | { |
c4fce3ef | 3956 | ld_start_phase (PHASE_CTF); |
3d16b64e | 3957 | ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); |
c4fce3ef | 3958 | ld_stop_phase (PHASE_CTF); |
3d16b64e NA |
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); | |
1ff6de03 NA |
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 | ||
c4fce3ef NC |
3980 | ld_start_phase (PHASE_CTF); |
3981 | ||
1ff6de03 NA |
3982 | if (late) |
3983 | { | |
3984 | /* Emit CTF late if this emulation says it can do so. */ | |
3985 | if (ldemul_emit_ctf_early ()) | |
c4fce3ef NC |
3986 | { |
3987 | ld_stop_phase (PHASE_CTF); | |
3988 | return; | |
3989 | } | |
1ff6de03 NA |
3990 | } |
3991 | else | |
3992 | { | |
3993 | if (!ldemul_emit_ctf_early ()) | |
c4fce3ef NC |
3994 | { |
3995 | ld_stop_phase (PHASE_CTF); | |
3996 | return; | |
3997 | } | |
1ff6de03 NA |
3998 | } |
3999 | ||
3d16b64e NA |
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 | ||
1ff6de03 NA |
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 | ||
926c9e76 | 4015 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
4016 | if (!output_sect->contents) |
4017 | { | |
f320bba5 | 4018 | einfo (_("%P: warning: CTF section emission failed; " |
5e9b84f7 | 4019 | "output will have no CTF section: %s\n"), |
f320bba5 | 4020 | ctf_errmsg (ctf_errno (ctf_output))); |
1ff6de03 NA |
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. */ | |
139633c3 | 4027 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
4028 | ctf_output = NULL; |
4029 | ||
4030 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
4031 | file->the_ctf = NULL; | |
c4fce3ef NC |
4032 | |
4033 | ld_stop_phase (PHASE_CTF); | |
1ff6de03 NA |
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 | } | |
094e34f2 NA |
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 | { | |
f320bba5 EB |
4059 | einfo (_("%P: warning: CTF section in %pB not linkable: " |
4060 | "%P was built without support for CTF\n"), file->the_bfd); | |
094e34f2 NA |
4061 | sect->size = 0; |
4062 | sect->flags |= SEC_EXCLUDE; | |
4063 | } | |
4064 | } | |
4065 | } | |
4066 | ||
4067 | static void lang_merge_ctf (void) {} | |
4068 | void | |
3d16b64e NA |
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) {} | |
094e34f2 NA |
4074 | static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} |
4075 | void ldlang_write_ctf_late (void) {} | |
4076 | #endif | |
1ff6de03 | 4077 | |
08da4cac | 4078 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
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 | ||
e3e942e9 | 4084 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 4085 | |
e3e942e9 | 4086 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
4087 | |
4088 | void | |
f38a2680 | 4089 | ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) |
252b5132 | 4090 | { |
24898b70 | 4091 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 4092 | |
988de25b | 4093 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
4094 | new_undef->next = ldlang_undef_chain_list_head; |
4095 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 4096 | |
d4115c2c | 4097 | new_undef->name = stat_strdup (name); |
fcf0e35b | 4098 | |
f13a99db | 4099 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 4100 | insert_undefined (new_undef->name); |
fcf0e35b AM |
4101 | } |
4102 | ||
4103 | /* Insert NAME as undefined in the symbol table. */ | |
4104 | ||
4105 | static void | |
1579bae1 | 4106 | insert_undefined (const char *name) |
fcf0e35b AM |
4107 | { |
4108 | struct bfd_link_hash_entry *h; | |
4109 | ||
f38a2680 | 4110 | h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); |
1579bae1 | 4111 | if (h == NULL) |
8d97c1a5 | 4112 | fatal (_("%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
4113 | if (h->type == bfd_link_hash_new) |
4114 | { | |
4115 | h->type = bfd_link_hash_undefined; | |
4116 | h->u.undef.abfd = NULL; | |
f38a2680 | 4117 | h->non_ir_ref_regular = true; |
fcf0e35b AM |
4118 | bfd_link_add_undef (link_info.hash, h); |
4119 | } | |
252b5132 RH |
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 | |
08da4cac KH |
4124 | script file. */ |
4125 | ||
252b5132 | 4126 | static void |
1579bae1 | 4127 | lang_place_undefineds (void) |
252b5132 RH |
4128 | { |
4129 | ldlang_undef_chain_list_type *ptr; | |
4130 | ||
1579bae1 AM |
4131 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
4132 | insert_undefined (ptr->name); | |
252b5132 RH |
4133 | } |
4134 | ||
5789f845 AM |
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 | ||
fc304b88 | 4142 | if (is_elf_hash_table (link_info.hash)) |
5789f845 AM |
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 *) | |
f38a2680 | 4146 | bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); |
5789f845 AM |
4147 | if (h != NULL) |
4148 | h->mark = 1; | |
4149 | } | |
4150 | } | |
4151 | ||
0a618243 AB |
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 | |
0aa7f586 | 4169 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
4170 | { |
4171 | struct require_defined_symbol *ptr; | |
4172 | ||
f38a2680 | 4173 | ldlang_add_undef (name, true); |
988de25b | 4174 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 | 4175 | ptr->next = require_defined_symbol_list; |
d4115c2c | 4176 | ptr->name = stat_strdup (name); |
0a618243 AB |
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, | |
f38a2680 | 4193 | false, false, true); |
a965df05 | 4194 | if (! is_defined (h)) |
df5f2391 | 4195 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
4196 | } |
4197 | } | |
4198 | ||
0841712e JJ |
4199 | /* Check for all readonly or some readwrite sections. */ |
4200 | ||
4201 | static void | |
6feb9908 AM |
4202 | check_input_sections |
4203 | (lang_statement_union_type *s, | |
4204 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 4205 | { |
988de25b | 4206 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
4207 | { |
4208 | switch (s->header.type) | |
967928e9 AM |
4209 | { |
4210 | case lang_wild_statement_enum: | |
4211 | walk_wild (&s->wild_statement, check_section_callback, | |
4212 | output_section_statement); | |
0aa7f586 | 4213 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4214 | return; |
4215 | break; | |
4216 | case lang_constructors_statement_enum: | |
4217 | check_input_sections (constructor_list.head, | |
4218 | output_section_statement); | |
0aa7f586 | 4219 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4220 | return; |
4221 | break; | |
4222 | case lang_group_statement_enum: | |
4223 | check_input_sections (s->group_statement.children.head, | |
4224 | output_section_statement); | |
0aa7f586 | 4225 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
4226 | return; |
4227 | break; | |
4228 | default: | |
4229 | break; | |
4230 | } | |
0841712e JJ |
4231 | } |
4232 | } | |
4233 | ||
bcaa7b3e L |
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: | |
0d0999db L |
4259 | for (sec = s->wild_statement.section_list; sec != NULL; |
4260 | sec = sec->next) | |
2d3181c7 AM |
4261 | /* Don't sort .init/.fini sections. */ |
4262 | if (strcmp (sec->spec.name, ".init") != 0 | |
4263 | && strcmp (sec->spec.name, ".fini") != 0) | |
af31506c MM |
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 | } | |
bcaa7b3e L |
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: | |
2d3181c7 AM |
4290 | update_wild_statements |
4291 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
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 | ||
396a2467 | 4303 | /* Open input files and attach to output sections. */ |
08da4cac | 4304 | |
252b5132 | 4305 | static void |
1579bae1 AM |
4306 | map_input_to_output_sections |
4307 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4308 | lang_output_section_statement_type *os) |
252b5132 | 4309 | { |
1579bae1 | 4310 | for (; s != NULL; s = s->header.next) |
252b5132 | 4311 | { |
dfa7b0b8 AM |
4312 | lang_output_section_statement_type *tos; |
4313 | flagword flags; | |
c212f39d | 4314 | unsigned int type = 0; |
dfa7b0b8 | 4315 | |
252b5132 RH |
4316 | switch (s->header.type) |
4317 | { | |
252b5132 | 4318 | case lang_wild_statement_enum: |
afd7a018 | 4319 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4320 | break; |
252b5132 RH |
4321 | case lang_constructors_statement_enum: |
4322 | map_input_to_output_sections (constructor_list.head, | |
4323 | target, | |
afd7a018 | 4324 | os); |
252b5132 RH |
4325 | break; |
4326 | case lang_output_section_statement_enum: | |
dfa7b0b8 | 4327 | tos = &s->output_section_statement; |
21401fc7 AM |
4328 | if (tos->constraint == ONLY_IF_RW |
4329 | || tos->constraint == ONLY_IF_RO) | |
0841712e | 4330 | { |
f38a2680 | 4331 | tos->all_input_readonly = true; |
dfa7b0b8 AM |
4332 | check_input_sections (tos->children.head, tos); |
4333 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
21401fc7 | 4334 | tos->constraint = -1; |
0841712e | 4335 | } |
21401fc7 AM |
4336 | if (tos->constraint >= 0) |
4337 | map_input_to_output_sections (tos->children.head, | |
4338 | target, | |
4339 | tos); | |
a431bc2e AM |
4340 | break; |
4341 | case lang_output_statement_enum: | |
252b5132 RH |
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, | |
afd7a018 | 4349 | os); |
252b5132 | 4350 | break; |
384d938f | 4351 | case lang_data_statement_enum: |
ee1cb49e NC |
4352 | if (os == NULL) |
4353 | /* This should never happen. */ | |
4354 | FAIL (); | |
384d938f NS |
4355 | /* Make sure that any sections mentioned in the expression |
4356 | are initialized. */ | |
4357 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4358 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4359 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4360 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4361 | switch (os->sectype) | |
4362 | { | |
4363 | case normal_section: | |
4364 | case overlay_section: | |
7b243801 | 4365 | case first_overlay_section: |
dfa7b0b8 AM |
4366 | break; |
4367 | case noalloc_section: | |
4368 | flags = SEC_HAS_CONTENTS; | |
4369 | break; | |
6b86da53 LB |
4370 | case readonly_section: |
4371 | flags |= SEC_READONLY; | |
4372 | break; | |
c212f39d FS |
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 | |
8d97c1a5 | 4396 | fatal (_ ("%P: invalid type for output section `%s'\n"), |
c212f39d FS |
4397 | os->name); |
4398 | } | |
4399 | else | |
4400 | { | |
b5c37946 | 4401 | exp_fold_tree_no_dot (os->sectype_value, os); |
c212f39d FS |
4402 | if (expld.result.valid_p) |
4403 | type = expld.result.value; | |
4404 | else | |
8d97c1a5 | 4405 | fatal (_ ("%P: invalid type for output section `%s'\n"), |
c212f39d FS |
4406 | os->name); |
4407 | } | |
4408 | break; | |
dfa7b0b8 | 4409 | case noload_section: |
f4eaaf7f AM |
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; | |
dfa7b0b8 AM |
4415 | break; |
4416 | } | |
a431bc2e | 4417 | if (os->bfd_section == NULL) |
326adec3 | 4418 | init_os (os, flags | SEC_READONLY); |
a431bc2e AM |
4419 | else |
4420 | os->bfd_section->flags |= flags; | |
c212f39d | 4421 | os->bfd_section->type = type; |
afd7a018 | 4422 | break; |
252b5132 | 4423 | case lang_input_section_enum: |
e0f6802f AM |
4424 | break; |
4425 | case lang_fill_statement_enum: | |
252b5132 | 4426 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4427 | case lang_reloc_statement_enum: |
4428 | case lang_padding_statement_enum: | |
4429 | case lang_input_statement_enum: | |
afd7a018 | 4430 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4431 | init_os (os, 0); |
252b5132 | 4432 | break; |
193327db | 4433 | |
252b5132 | 4434 | case lang_assignment_statement_enum: |
afd7a018 | 4435 | if (os != NULL && os->bfd_section == NULL) |
193327db | 4436 | init_os (os, get_os_init_flag (os)); |
252b5132 RH |
4437 | |
4438 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4439 | are initialized. */ |
252b5132 RH |
4440 | exp_init_os (s->assignment_statement.exp); |
4441 | break; | |
193327db | 4442 | |
252b5132 | 4443 | case lang_address_statement_enum: |
a431bc2e AM |
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 | |
370dfff4 | 4450 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4451 | linker scripts that do not specifically check for |
4452 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4453 | if (!s->address_statement.segment |
ba916c8a MM |
4454 | || !s->address_statement.segment->used) |
4455 | { | |
dfa7b0b8 AM |
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. */ | |
21401fc7 | 4464 | tos = lang_output_section_statement_lookup (name, 0, 1); |
dfa7b0b8 AM |
4465 | tos->addr_tree = s->address_statement.address; |
4466 | if (tos->bfd_section == NULL) | |
4467 | init_os (tos, 0); | |
ba916c8a | 4468 | } |
252b5132 | 4469 | break; |
53d25da6 AM |
4470 | case lang_insert_statement_enum: |
4471 | break; | |
43ae96e9 MM |
4472 | case lang_input_matcher_enum: |
4473 | FAIL (); | |
53d25da6 AM |
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 | |
776ab89f AM |
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. */ | |
53d25da6 AM |
4491 | |
4492 | static void | |
776ab89f | 4493 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
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; | |
66c103b7 | 4498 | lang_output_section_statement_type *os; |
53d25da6 | 4499 | |
776ab89f AM |
4500 | s = start; |
4501 | while (*s != NULL) | |
53d25da6 AM |
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. */ | |
d2ae7be0 AM |
4507 | os = &(*s)->output_section_statement; |
4508 | ||
4509 | ASSERT (last_os == NULL || last_os->next == os); | |
4510 | last_os = os; | |
66c103b7 AM |
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; | |
53d25da6 AM |
4517 | if (first_os == NULL) |
4518 | first_os = last_os; | |
4519 | } | |
776ab89f AM |
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 | } | |
53d25da6 AM |
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; | |
53d25da6 AM |
4536 | lang_statement_union_type **ptr; |
4537 | lang_statement_union_type *first; | |
4538 | ||
abf874aa CL |
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 | ||
53d25da6 AM |
4545 | where = lang_output_section_find (i->where); |
4546 | if (where != NULL && i->is_before) | |
4547 | { | |
967928e9 | 4548 | do |
53d25da6 | 4549 | where = where->prev; |
66c103b7 | 4550 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4551 | } |
4552 | if (where == NULL) | |
4553 | { | |
8d97c1a5 | 4554 | fatal (_("%P: %s not found for insert\n"), i->where); |
66c103b7 | 4555 | return; |
53d25da6 AM |
4556 | } |
4557 | ||
4558 | /* Deal with reordering the output section statement list. */ | |
4559 | if (last_os != NULL) | |
4560 | { | |
4561 | asection *first_sec, *last_sec; | |
29183214 | 4562 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4563 | |
4564 | /* Snip out the output sections we are moving. */ | |
4565 | first_os->prev->next = last_os->next; | |
4566 | if (last_os->next == NULL) | |
29183214 L |
4567 | { |
4568 | next = &first_os->prev->next; | |
5c1e6d53 | 4569 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4570 | } |
53d25da6 AM |
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) | |
29183214 L |
4576 | { |
4577 | next = &last_os->next; | |
5c1e6d53 | 4578 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4579 | } |
53d25da6 AM |
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 | { | |
66c103b7 | 4590 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
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 | |
f13a99db | 4617 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4618 | if (last_sec->next != NULL) |
4619 | last_sec->next->prev = first_sec->prev; | |
4620 | else | |
f13a99db | 4621 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 | 4622 | /* Add back. */ |
18e7a658 AM |
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; | |
53d25da6 | 4635 | else |
f13a99db | 4636 | link_info.output_bfd->section_last = last_sec; |
53d25da6 | 4637 | first_sec->prev = sec; |
18e7a658 AM |
4638 | if (first_sec->prev != NULL) |
4639 | first_sec->prev->next = first_sec; | |
4640 | else | |
4641 | link_info.output_bfd->sections = first_sec; | |
53d25da6 AM |
4642 | } |
4643 | } | |
53d25da6 AM |
4644 | } |
4645 | ||
18e7a658 AM |
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); | |
776ab89f AM |
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. */ | |
53d25da6 AM |
4677 | *s = *ptr; |
4678 | if (*s == NULL) | |
4679 | statement_list.tail = s; | |
4680 | *ptr = first; | |
776ab89f | 4681 | s = start; |
18e7a658 AM |
4682 | first_os = NULL; |
4683 | last_os = NULL; | |
776ab89f | 4684 | continue; |
252b5132 | 4685 | } |
776ab89f | 4686 | s = &(*s)->header.next; |
252b5132 | 4687 | } |
66c103b7 AM |
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 | } | |
252b5132 RH |
4696 | } |
4697 | ||
4bd5a393 AM |
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 | ||
8423293d | 4702 | void |
1579bae1 | 4703 | strip_excluded_output_sections (void) |
4bd5a393 | 4704 | { |
afd7a018 | 4705 | lang_output_section_statement_type *os; |
4bd5a393 | 4706 | |
046183de | 4707 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4708 | if (expld.phase != lang_mark_phase_enum) |
4709 | { | |
4710 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4711 | expld.dataseg.phase = exp_seg_none; |
f38a2680 | 4712 | one_lang_size_sections_pass (NULL, false); |
e9ee469a AM |
4713 | lang_reset_memory_regions (); |
4714 | } | |
4715 | ||
8ce18f9c | 4716 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
4717 | os != NULL; |
4718 | os = os->next) | |
4bd5a393 | 4719 | { |
75ff4589 | 4720 | asection *output_section; |
f38a2680 | 4721 | bool exclude; |
4bd5a393 | 4722 | |
66c103b7 | 4723 | if (os->constraint < 0) |
0841712e | 4724 | continue; |
8423293d | 4725 | |
75ff4589 L |
4726 | output_section = os->bfd_section; |
4727 | if (output_section == NULL) | |
8423293d AM |
4728 | continue; |
4729 | ||
32124d5b AM |
4730 | exclude = (output_section->rawsize == 0 |
4731 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4732 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
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) | |
75ff4589 L |
4740 | { |
4741 | asection *s; | |
8423293d | 4742 | |
32124d5b | 4743 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4744 | if ((s->flags & SEC_EXCLUDE) == 0 |
4745 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4746 | || link_info.emitrelocations)) | |
e9ee469a | 4747 | { |
f38a2680 | 4748 | exclude = false; |
e9ee469a AM |
4749 | break; |
4750 | } | |
75ff4589 | 4751 | } |
8423293d | 4752 | |
32124d5b | 4753 | if (exclude) |
4bd5a393 | 4754 | { |
e9ee469a AM |
4755 | /* We don't set bfd_section to NULL since bfd_section of the |
4756 | removed output section statement may still be used. */ | |
6d8bf25d | 4757 | if (!os->update_dot) |
f38a2680 | 4758 | os->ignored = true; |
e9ee469a | 4759 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4760 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4761 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4762 | } |
4763 | } | |
18cd5bce AM |
4764 | } |
4765 | ||
4766 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4767 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
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 | ||
8ce18f9c | 4777 | for (os = (void *) lang_os_list.head; |
18cd5bce AM |
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 | } | |
8423293d AM |
4794 | |
4795 | /* Stop future calls to lang_add_section from messing with map_head | |
4796 | and map_tail link_order fields. */ | |
f38a2680 | 4797 | map_head_is_link_order = true; |
4bd5a393 AM |
4798 | } |
4799 | ||
252b5132 | 4800 | static void |
1579bae1 AM |
4801 | print_output_section_statement |
4802 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
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 | } | |
6b9bd54c | 4821 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4822 | |
953dd97e | 4823 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4824 | |
67f744f3 AM |
4825 | if (section->vma != section->lma) |
4826 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4827 | |
4828 | if (output_section_statement->update_dot_tree != NULL) | |
4829 | exp_fold_tree (output_section_statement->update_dot_tree, | |
b5c37946 | 4830 | output_section_statement, |
5590fba9 | 4831 | bfd_abs_section_ptr, &print_dot); |
252b5132 RH |
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 | |
1579bae1 AM |
4842 | print_assignment (lang_assignment_statement_type *assignment, |
4843 | lang_output_section_statement_type *output_section) | |
252b5132 | 4844 | { |
f38a2680 | 4845 | bool is_dot; |
afd7a018 | 4846 | etree_type *tree; |
8658f989 | 4847 | asection *osec; |
252b5132 | 4848 | |
6b9bd54c | 4849 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4850 | |
afd7a018 AM |
4851 | if (assignment->exp->type.node_class == etree_assert) |
4852 | { | |
f38a2680 | 4853 | is_dot = false; |
afd7a018 AM |
4854 | tree = assignment->exp->assert_s.child; |
4855 | } | |
4856 | else | |
4857 | { | |
4858 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4859 | |
4860 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4861 | tree = assignment->exp; |
afd7a018 AM |
4862 | } |
4863 | ||
8658f989 AM |
4864 | osec = output_section->bfd_section; |
4865 | if (osec == NULL) | |
4866 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4867 | |
4868 | if (assignment->exp->type.node_class != etree_provide) | |
b5c37946 | 4869 | exp_fold_tree (tree, output_section, osec, &print_dot); |
c05b575a | 4870 | else |
f38a2680 | 4871 | expld.result.valid_p = false; |
c05b575a | 4872 | |
6b9bd54c AM |
4873 | char buf[32]; |
4874 | const char *str = buf; | |
e9ee469a | 4875 | if (expld.result.valid_p) |
7b17f854 | 4876 | { |
7b17f854 RS |
4877 | bfd_vma value; |
4878 | ||
4194268f AM |
4879 | if (assignment->exp->type.node_class == etree_assert |
4880 | || is_dot | |
4881 | || expld.assign_name != NULL) | |
3b83e13a | 4882 | { |
e9ee469a | 4883 | value = expld.result.value; |
10dbd1f3 | 4884 | |
7542af2a | 4885 | if (expld.result.section != NULL) |
e9ee469a | 4886 | value += expld.result.section->vma; |
7b17f854 | 4887 | |
6b9bd54c AM |
4888 | buf[0] = '0'; |
4889 | buf[1] = 'x'; | |
4890 | bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); | |
3b83e13a NC |
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, | |
f38a2680 | 4899 | false, false, true); |
a965df05 | 4900 | if (is_defined (h)) |
3b83e13a | 4901 | { |
10dbd1f3 | 4902 | value = h->u.def.value; |
f37a7048 NS |
4903 | value += h->u.def.section->output_section->vma; |
4904 | value += h->u.def.section->output_offset; | |
3b83e13a | 4905 | |
6b9bd54c AM |
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, "]"); | |
3b83e13a NC |
4911 | } |
4912 | else | |
6b9bd54c | 4913 | str = "[unresolved]"; |
3b83e13a | 4914 | } |
7b17f854 | 4915 | } |
252b5132 RH |
4916 | else |
4917 | { | |
c05b575a | 4918 | if (assignment->exp->type.node_class == etree_provide) |
6b9bd54c | 4919 | str = "[!provide]"; |
c05b575a | 4920 | else |
6b9bd54c | 4921 | str = "*undef*"; |
252b5132 | 4922 | } |
4194268f | 4923 | expld.assign_name = NULL; |
252b5132 | 4924 | |
6b9bd54c | 4925 | fprintf (config.map_file, "%-34s", str); |
252b5132 | 4926 | exp_print_tree (assignment->exp); |
252b5132 RH |
4927 | print_nl (); |
4928 | } | |
4929 | ||
4930 | static void | |
1579bae1 | 4931 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4932 | { |
0d41d9a2 | 4933 | if (statm->filename != NULL) |
906e58ca | 4934 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4935 | } |
4936 | ||
4937 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4938 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4939 | |
f38a2680 | 4940 | bool |
1579bae1 | 4941 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4942 | { |
1e9cc1c2 | 4943 | asection *sec = (asection *) ptr; |
252b5132 | 4944 | |
a965df05 | 4945 | if (is_defined (hash_entry) |
252b5132 RH |
4946 | && sec == hash_entry->u.def.section) |
4947 | { | |
6b9bd54c | 4948 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 RH |
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 | ||
c1c8c1ef | 4954 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4955 | } |
4956 | ||
f38a2680 | 4957 | return true; |
252b5132 RH |
4958 | } |
4959 | ||
02688209 TG |
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 | ||
35835446 | 4974 | static void |
e0f6802f | 4975 | print_all_symbols (asection *sec) |
35835446 | 4976 | { |
a48931cc | 4977 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4978 | struct map_symbol_def *def; |
02688209 TG |
4979 | struct bfd_link_hash_entry **entries; |
4980 | unsigned int i; | |
35835446 | 4981 | |
afd7a018 AM |
4982 | if (!ud) |
4983 | return; | |
4984 | ||
35835446 | 4985 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4986 | |
02688209 | 4987 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4988 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4989 | obstack_alloc (&map_obstack, |
4990 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
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), | |
4724d37e | 4996 | hash_entry_addr_cmp); |
02688209 TG |
4997 | |
4998 | /* Print the symbols. */ | |
4999 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
3edf7b9f | 5000 | ldemul_print_symbol (entries[i], sec); |
02688209 TG |
5001 | |
5002 | obstack_free (&map_obstack, entries); | |
35835446 JR |
5003 | } |
5004 | ||
496917ce NC |
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 | ||
496917ce NC |
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. | |
90bea493 | 5023 | It would be better to fix bfd_is_local_label. */ |
496917ce NC |
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; | |
90bea493 | 5030 | h = bfd_link_hash_lookup (link_info.hash, name, false /* create */, |
496917ce NC |
5031 | false /* copy */, true /* follow */); |
5032 | if (h == NULL) | |
5033 | return true; | |
90bea493 | 5034 | |
496917ce NC |
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 | ||
252b5132 RH |
5046 | /* Print information about an input section to the map file. */ |
5047 | ||
5048 | static void | |
f38a2680 | 5049 | print_input_section (asection *i, bool is_discarded) |
252b5132 | 5050 | { |
eea6121a | 5051 | bfd_size_type size = i->size; |
abe6ac95 AM |
5052 | int len; |
5053 | bfd_vma addr; | |
e5caa5e0 | 5054 | |
61826503 | 5055 | init_opb (i); |
4d4920ec | 5056 | |
6b9bd54c | 5057 | minfo (" %s", i->name); |
252b5132 | 5058 | |
abe6ac95 AM |
5059 | len = 1 + strlen (i->name); |
5060 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
5061 | { | |
5062 | print_nl (); | |
5063 | len = 0; | |
5064 | } | |
6b9bd54c | 5065 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 5066 | |
9dd98830 AM |
5067 | if ((i->flags & SEC_EXCLUDE) == 0 |
5068 | && i->output_section != NULL | |
f13a99db | 5069 | && i->output_section->owner == link_info.output_bfd) |
abe6ac95 AM |
5070 | addr = i->output_section->vma + i->output_offset; |
5071 | else | |
5072 | { | |
5073 | addr = print_dot; | |
d64703c6 TG |
5074 | if (!is_discarded) |
5075 | size = 0; | |
abe6ac95 | 5076 | } |
252b5132 | 5077 | |
6b9bd54c AM |
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); | |
252b5132 | 5081 | |
abe6ac95 AM |
5082 | if (size != i->rawsize && i->rawsize != 0) |
5083 | { | |
6b9bd54c AM |
5084 | len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); |
5085 | print_spaces (len); | |
b0a7971a | 5086 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 5087 | } |
252b5132 | 5088 | |
f13a99db AM |
5089 | if (i->output_section != NULL |
5090 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 5091 | { |
c0f00686 | 5092 | if (link_info.reduce_memory_overheads) |
3edf7b9f | 5093 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); |
abe6ac95 AM |
5094 | else |
5095 | print_all_symbols (i); | |
5096 | ||
5590fba9 NC |
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); | |
496917ce NC |
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; | |
90bea493 | 5125 | |
496917ce NC |
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 | } | |
252b5132 RH |
5140 | } |
5141 | } | |
5142 | ||
5143 | static void | |
1579bae1 | 5144 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 5145 | { |
2c382fb6 AM |
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); | |
252b5132 RH |
5152 | } |
5153 | ||
5154 | static void | |
1579bae1 | 5155 | print_data_statement (lang_data_statement_type *data) |
252b5132 | 5156 | { |
252b5132 RH |
5157 | bfd_vma addr; |
5158 | bfd_size_type size; | |
5159 | const char *name; | |
5160 | ||
61826503 | 5161 | init_opb (data->output_section); |
6b9bd54c | 5162 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 5163 | |
7fabd029 | 5164 | addr = data->output_offset; |
252b5132 RH |
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 | ||
953dd97e DG |
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); | |
252b5132 RH |
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 | ||
e5caa5e0 | 5206 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
5207 | } |
5208 | ||
5209 | /* Print an address statement. These are generated by options like | |
5210 | -Ttext. */ | |
5211 | ||
5212 | static void | |
1579bae1 | 5213 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
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 | |
1579bae1 | 5223 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 | 5224 | { |
252b5132 RH |
5225 | bfd_vma addr; |
5226 | bfd_size_type size; | |
5227 | ||
61826503 | 5228 | init_opb (reloc->output_section); |
6b9bd54c | 5229 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 5230 | |
7fabd029 | 5231 | addr = reloc->output_offset; |
252b5132 RH |
5232 | if (reloc->output_section != NULL) |
5233 | addr += reloc->output_section->vma; | |
5234 | ||
5235 | size = bfd_get_reloc_size (reloc->howto); | |
5236 | ||
953dd97e | 5237 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
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 | ||
e5caa5e0 | 5248 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 5249 | } |
252b5132 RH |
5250 | |
5251 | static void | |
1579bae1 | 5252 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
5253 | { |
5254 | int len; | |
5255 | bfd_vma addr; | |
5256 | ||
61826503 | 5257 | init_opb (s->output_section); |
252b5132 RH |
5258 | minfo (" *fill*"); |
5259 | ||
5260 | len = sizeof " *fill*" - 1; | |
6b9bd54c | 5261 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 RH |
5262 | |
5263 | addr = s->output_offset; | |
5264 | if (s->output_section != NULL) | |
5265 | addr += s->output_section->vma; | |
953dd97e | 5266 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 5267 | |
2c382fb6 AM |
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 | } | |
252b5132 RH |
5275 | |
5276 | print_nl (); | |
5277 | ||
e5caa5e0 | 5278 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
5279 | } |
5280 | ||
5281 | static void | |
1579bae1 AM |
5282 | print_wild_statement (lang_wild_statement_type *w, |
5283 | lang_output_section_statement_type *os) | |
252b5132 | 5284 | { |
b6bf44ba AM |
5285 | struct wildcard_list *sec; |
5286 | ||
252b5132 RH |
5287 | print_space (); |
5288 | ||
8f1732fc AB |
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) | |
6c19b93b | 5294 | minfo (" %s", tmp->name); |
8f1732fc AB |
5295 | minfo (") "); |
5296 | } | |
5297 | ||
252b5132 | 5298 | if (w->filenames_sorted) |
2b94abd4 | 5299 | minfo ("SORT_BY_NAME("); |
85921e9a NC |
5300 | if (w->filenames_reversed) |
5301 | minfo ("REVERSE("); | |
08da4cac | 5302 | if (w->filename != NULL) |
252b5132 RH |
5303 | minfo ("%s", w->filename); |
5304 | else | |
5305 | minfo ("*"); | |
85921e9a NC |
5306 | if (w->filenames_reversed) |
5307 | minfo (")"); | |
252b5132 RH |
5308 | if (w->filenames_sorted) |
5309 | minfo (")"); | |
5310 | ||
5311 | minfo ("("); | |
b6bf44ba AM |
5312 | for (sec = w->section_list; sec; sec = sec->next) |
5313 | { | |
2b94abd4 AB |
5314 | int closing_paren = 0; |
5315 | ||
5316 | switch (sec->spec.sorted) | |
6c19b93b AM |
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 | } | |
2b94abd4 | 5351 | |
85921e9a NC |
5352 | if (sec->spec.reversed) |
5353 | { | |
5354 | minfo ("REVERSE("); | |
5355 | closing_paren++; | |
5356 | } | |
5357 | ||
b6bf44ba AM |
5358 | if (sec->spec.exclude_name_list != NULL) |
5359 | { | |
5360 | name_list *tmp; | |
34786259 | 5361 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 5362 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
5363 | minfo (" %s", tmp->name); |
5364 | minfo (") "); | |
b6bf44ba AM |
5365 | } |
5366 | if (sec->spec.name != NULL) | |
5367 | minfo ("%s", sec->spec.name); | |
5368 | else | |
5369 | minfo ("*"); | |
2b94abd4 | 5370 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 5371 | minfo (")"); |
34786259 AM |
5372 | if (sec->next) |
5373 | minfo (" "); | |
b6bf44ba | 5374 | } |
252b5132 RH |
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 | |
1579bae1 AM |
5385 | print_group (lang_group_statement_type *s, |
5386 | lang_output_section_statement_type *os) | |
252b5132 RH |
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 | |
1579bae1 AM |
5397 | print_statement_list (lang_statement_union_type *s, |
5398 | lang_output_section_statement_type *os) | |
252b5132 RH |
5399 | { |
5400 | while (s != NULL) | |
5401 | { | |
5402 | print_statement (s, os); | |
bba1a0c0 | 5403 | s = s->header.next; |
252b5132 RH |
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 | |
1579bae1 AM |
5411 | print_statement (lang_statement_union_type *s, |
5412 | lang_output_section_statement_type *os) | |
252b5132 RH |
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: | |
f38a2680 | 5449 | print_input_section (s->input_section.section, false); |
252b5132 RH |
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; | |
53d25da6 AM |
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; | |
252b5132 RH |
5480 | } |
5481 | } | |
5482 | ||
5483 | static void | |
1579bae1 | 5484 | print_statements (void) |
252b5132 RH |
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 | |
1579bae1 | 5495 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
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); | |
bba1a0c0 | 5508 | s = s->header.next; |
252b5132 RH |
5509 | } |
5510 | } | |
5511 | ||
5512 | config.map_file = map_save; | |
5513 | } | |
5514 | ||
b3327aad | 5515 | static void |
1579bae1 AM |
5516 | insert_pad (lang_statement_union_type **ptr, |
5517 | fill_type *fill, | |
1a69ff95 | 5518 | bfd_size_type alignment_needed, |
1579bae1 AM |
5519 | asection *output_section, |
5520 | bfd_vma dot) | |
252b5132 | 5521 | { |
b7761f11 | 5522 | static fill_type zero_fill; |
e9ee469a | 5523 | lang_statement_union_type *pad = NULL; |
b3327aad | 5524 | |
e9ee469a AM |
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 | |
906e58ca NC |
5535 | && pad->header.type == lang_padding_statement_enum |
5536 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5537 | { |
e9ee469a | 5538 | /* Use the existing pad statement. */ |
252b5132 | 5539 | } |
b3327aad | 5540 | else |
252b5132 | 5541 | { |
b3327aad | 5542 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5543 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5544 | pad->header.next = *ptr; |
5545 | *ptr = pad; | |
5546 | pad->header.type = lang_padding_statement_enum; | |
5547 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5548 | if (fill == NULL) |
2c382fb6 | 5549 | fill = &zero_fill; |
b3327aad | 5550 | pad->padding_statement.fill = fill; |
252b5132 | 5551 | } |
b3327aad AM |
5552 | pad->padding_statement.output_offset = dot - output_section->vma; |
5553 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5554 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5555 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5556 | - output_section->vma); | |
252b5132 RH |
5557 | } |
5558 | ||
08da4cac KH |
5559 | /* Work out how much this section will move the dot point. */ |
5560 | ||
252b5132 | 5561 | static bfd_vma |
6feb9908 AM |
5562 | size_input_section |
5563 | (lang_statement_union_type **this_ptr, | |
5564 | lang_output_section_statement_type *output_section_statement, | |
5565 | fill_type *fill, | |
f38a2680 | 5566 | bool *removed, |
6feb9908 | 5567 | bfd_vma dot) |
252b5132 RH |
5568 | { |
5569 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5570 | asection *i = is->section; | |
93d1b056 | 5571 | asection *o = output_section_statement->bfd_section; |
abf874aa CL |
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 | } | |
252b5132 | 5587 | |
93d1b056 AM |
5588 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5589 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5590 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5591 | || output_section_statement->ignored) | |
93d1b056 AM |
5592 | i->output_offset = dot - o->vma; |
5593 | else | |
252b5132 | 5594 | { |
1a69ff95 | 5595 | bfd_size_type alignment_needed; |
b3327aad AM |
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 | |
60856b5c | 5600 | the alignment by inserting a magic 'padding' statement. |
90bea493 AM |
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. */ | |
b3327aad | 5604 | |
3d9c8f6b | 5605 | if (output_section_statement->subsection_alignment != NULL) |
60856b5c VB |
5606 | o->alignment_power = i->alignment_power = |
5607 | exp_get_power (output_section_statement->subsection_alignment, | |
5608 | output_section_statement, | |
5609 | "subsection alignment"); | |
b3327aad | 5610 | |
b3327aad AM |
5611 | if (o->alignment_power < i->alignment_power) |
5612 | o->alignment_power = i->alignment_power; | |
252b5132 | 5613 | |
b3327aad AM |
5614 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5615 | ||
5616 | if (alignment_needed != 0) | |
5617 | { | |
e5caa5e0 | 5618 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5619 | dot += alignment_needed; |
5620 | } | |
252b5132 | 5621 | |
abf874aa CL |
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) | |
8d97c1a5 | 5634 | fatal (_("%P: Output section `%pA' not large enough for " |
c63d4862 AM |
5635 | "the linker-created stubs section `%pA'.\n"), |
5636 | i->output_section, i); | |
abf874aa CL |
5637 | |
5638 | if (i->rawsize && i->rawsize != i->size) | |
8d97c1a5 | 5639 | fatal (_("%P: Relaxation not supported with " |
c63d4862 AM |
5640 | "--enable-non-contiguous-regions (section `%pA' " |
5641 | "would overflow `%pA' after it changed size).\n"), | |
5642 | i, i->output_section); | |
abf874aa CL |
5643 | |
5644 | *removed = 1; | |
5645 | dot = end; | |
5646 | i->output_section = NULL; | |
5647 | return dot; | |
5648 | } | |
5649 | } | |
5650 | } | |
5651 | ||
08da4cac | 5652 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5653 | i->output_offset = dot - o->vma; |
252b5132 | 5654 | |
08da4cac | 5655 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5656 | dot += TO_ADDR (i->size); |
8772de11 MR |
5657 | if (!(o->flags & SEC_FIXED_SIZE)) |
5658 | o->size = TO_SIZE (dot - o->vma); | |
abf874aa CL |
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 | } | |
252b5132 | 5667 | } |
252b5132 RH |
5668 | |
5669 | return dot; | |
5670 | } | |
5671 | ||
a87dd97a AM |
5672 | struct check_sec |
5673 | { | |
5674 | asection *sec; | |
f38a2680 | 5675 | bool warned; |
a87dd97a AM |
5676 | }; |
5677 | ||
5daa8fe7 L |
5678 | static int |
5679 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5680 | { | |
a87dd97a AM |
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; | |
5daa8fe7 | 5701 | |
a87dd97a | 5702 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5703 | return -1; |
a87dd97a | 5704 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5705 | return 1; |
7f6a71ff JM |
5706 | else if (sec1->id < sec2->id) |
5707 | return -1; | |
5708 | else if (sec1->id > sec2->id) | |
5709 | return 1; | |
5daa8fe7 L |
5710 | |
5711 | return 0; | |
5712 | } | |
5713 | ||
2e4a7aea AM |
5714 | #define IS_TBSS(s) \ |
5715 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5716 | ||
eea6121a | 5717 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5718 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5719 | |
252b5132 | 5720 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5721 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5722 | section addresses of the two sections. Also check whether any memory |
5723 | region has overflowed. */ | |
08da4cac | 5724 | |
252b5132 | 5725 | static void |
1579bae1 | 5726 | lang_check_section_addresses (void) |
252b5132 | 5727 | { |
f4427a75 | 5728 | asection *s, *p; |
a87dd97a AM |
5729 | struct check_sec *sections; |
5730 | size_t i, count; | |
d40e34db | 5731 | bfd_vma addr_mask; |
5daa8fe7 L |
5732 | bfd_vma s_start; |
5733 | bfd_vma s_end; | |
a87dd97a AM |
5734 | bfd_vma p_start = 0; |
5735 | bfd_vma p_end = 0; | |
20e56351 | 5736 | lang_memory_region_type *m; |
f38a2680 | 5737 | bool overlays; |
5daa8fe7 | 5738 | |
21701718 | 5739 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
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) | |
21701718 TG |
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 | } | |
d40e34db | 5758 | |
f13a99db | 5759 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5760 | return; |
5761 | ||
a87dd97a AM |
5762 | count = bfd_count_sections (link_info.output_bfd); |
5763 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5764 | |
5765 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5766 | count = 0; |
f13a99db | 5767 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5768 | { |
a87dd97a | 5769 | if (IGNORE_SECTION (s) |
ec6d26be | 5770 | || s->size == 0) |
33275c22 | 5771 | continue; |
5f992e62 | 5772 | |
a87dd97a | 5773 | sections[count].sec = s; |
f38a2680 | 5774 | sections[count].warned = false; |
5daa8fe7 L |
5775 | count++; |
5776 | } | |
329c1c86 | 5777 | |
5daa8fe7 | 5778 | if (count <= 1) |
136a43b7 AM |
5779 | { |
5780 | free (sections); | |
5781 | return; | |
5782 | } | |
5f992e62 | 5783 | |
a87dd97a | 5784 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5785 | |
136a43b7 | 5786 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5787 | loadable sections, even with overlays. */ |
5788 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5789 | { |
a87dd97a | 5790 | s = sections[i].sec; |
61826503 | 5791 | init_opb (s); |
a87dd97a AM |
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); | |
f38a2680 | 5809 | sections[i].warned = true; |
a87dd97a AM |
5810 | } |
5811 | p = s; | |
5812 | p_start = s_start; | |
5813 | p_end = s_end; | |
5814 | } | |
5815 | } | |
5816 | ||
136a43b7 AM |
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); | |
f38a2680 | 5823 | overlays = false; |
136a43b7 AM |
5824 | p_start = sections[0].sec->vma; |
5825 | for (i = 1; i < count; i++) | |
a87dd97a | 5826 | { |
136a43b7 AM |
5827 | s_start = sections[i].sec->vma; |
5828 | if (p_start == s_start) | |
5829 | { | |
f38a2680 | 5830 | overlays = true; |
136a43b7 AM |
5831 | break; |
5832 | } | |
5833 | p_start = s_start; | |
5834 | } | |
a87dd97a | 5835 | |
136a43b7 AM |
5836 | /* Now check section VMAs if no overlays were detected. */ |
5837 | if (!overlays) | |
5838 | { | |
a87dd97a AM |
5839 | for (p = NULL, i = 0; i < count; i++) |
5840 | { | |
5841 | s = sections[i].sec; | |
61826503 | 5842 | init_opb (s); |
a87dd97a AM |
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 | } | |
33275c22 | 5857 | } |
5daa8fe7 L |
5858 | |
5859 | free (sections); | |
20e56351 DJ |
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) | |
992a06ee AM |
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 | } | |
252b5132 RH |
5878 | } |
5879 | ||
562d3460 TW |
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 | |
5f992e62 | 5883 | calculation wraps around. */ |
562d3460 TW |
5884 | |
5885 | static void | |
1579bae1 | 5886 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5887 | lang_memory_region_type *region, |
1579bae1 | 5888 | etree_type *tree, |
91d6fa6a | 5889 | bfd_vma rbase) |
562d3460 TW |
5890 | { |
5891 | if ((region->current < region->origin | |
5892 | || (region->current - region->origin > region->length)) | |
5893 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5894 | || rbase == 0)) |
562d3460 | 5895 | { |
1579bae1 | 5896 | if (tree != NULL) |
b7a26f91 | 5897 | { |
871b3ab2 | 5898 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5899 | " is not within region `%s'\n"), |
b7a26f91 KH |
5900 | region->current, |
5901 | os->bfd_section->owner, | |
5902 | os->bfd_section->name, | |
4a93e180 | 5903 | region->name_list.name); |
b7a26f91 | 5904 | } |
20e56351 | 5905 | else if (!region->had_full_message) |
b7a26f91 | 5906 | { |
f38a2680 | 5907 | region->had_full_message = true; |
20e56351 | 5908 | |
871b3ab2 | 5909 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5910 | os->bfd_section->owner, |
20e56351 | 5911 | os->bfd_section->name, |
4a93e180 | 5912 | region->name_list.name); |
b7a26f91 | 5913 | } |
562d3460 TW |
5914 | } |
5915 | } | |
5916 | ||
ed1794ee | 5917 | static void |
2085ee26 | 5918 | ldlang_check_relro_region (lang_statement_union_type *s) |
ed1794ee | 5919 | { |
2085ee26 AM |
5920 | seg_align_type *seg = &expld.dataseg; |
5921 | ||
ed1794ee L |
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 | ||
252b5132 RH |
5942 | /* Set the sizes for all the output sections. */ |
5943 | ||
2d20f7bf | 5944 | static bfd_vma |
1579bae1 | 5945 | lang_size_sections_1 |
e535e147 | 5946 | (lang_statement_union_type **prev, |
b5c37946 | 5947 | lang_output_section_statement_type *current_os, |
1579bae1 AM |
5948 | fill_type *fill, |
5949 | bfd_vma dot, | |
f38a2680 AM |
5950 | bool *relax, |
5951 | bool check_regions) | |
252b5132 | 5952 | { |
e535e147 | 5953 | lang_statement_union_type *s; |
abf874aa | 5954 | lang_statement_union_type *prev_s = NULL; |
f38a2680 | 5955 | bool removed_prev_s = false; |
b5c37946 | 5956 | lang_output_section_statement_type *os = current_os; |
e535e147 | 5957 | |
252b5132 | 5958 | /* Size up the sections from their constituent parts. */ |
abf874aa | 5959 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) |
252b5132 | 5960 | { |
f38a2680 | 5961 | bool removed = false; |
abf874aa | 5962 | |
252b5132 RH |
5963 | switch (s->header.type) |
5964 | { | |
5965 | case lang_output_section_statement_enum: | |
5966 | { | |
13075d04 | 5967 | bfd_vma newdot, after, dotdelta; |
cde9e0be | 5968 | lang_memory_region_type *r; |
c5b0a9ef | 5969 | int section_alignment = 0; |
252b5132 | 5970 | |
d1778b88 | 5971 | os = &s->output_section_statement; |
61826503 | 5972 | init_opb (os->bfd_section); |
8658f989 AM |
5973 | if (os->constraint == -1) |
5974 | break; | |
5975 | ||
fd68d03d AM |
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 | |
0e1862bb | 5980 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5981 | && (bfd_get_flavour (link_info.output_bfd) |
5982 | == bfd_target_coff_flavour)) | |
eceda120 | 5983 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5984 | if (os->addr_tree != NULL) |
5985 | { | |
b5c37946 | 5986 | exp_fold_tree (os->addr_tree, os, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5987 | |
3bf9618b | 5988 | if (expld.result.valid_p) |
7542af2a AM |
5989 | { |
5990 | dot = expld.result.value; | |
5991 | if (expld.result.section != NULL) | |
5992 | dot += expld.result.section->vma; | |
5993 | } | |
3bf9618b | 5994 | else if (expld.phase != lang_mark_phase_enum) |
8d97c1a5 | 5995 | fatal (_("%P:%pS: non constant or forward reference" |
a5df8c84 | 5996 | " address expression for section %s\n"), |
dab69f68 | 5997 | os->addr_tree, os->name); |
a5df8c84 AM |
5998 | } |
5999 | ||
e9ee469a | 6000 | if (os->bfd_section == NULL) |
75ff4589 | 6001 | /* This section was removed or never actually created. */ |
252b5132 RH |
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. */ | |
f13a99db AM |
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)) | |
ebe372c1 | 6012 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 6013 | { |
08da4cac | 6014 | asection *input; |
252b5132 RH |
6015 | |
6016 | if (os->children.head == NULL | |
bba1a0c0 | 6017 | || os->children.head->header.next != NULL |
6feb9908 AM |
6018 | || (os->children.head->header.type |
6019 | != lang_input_section_enum)) | |
df5f2391 | 6020 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 6021 | " section %s\n"), os->name); |
252b5132 RH |
6022 | |
6023 | input = os->children.head->input_section.section; | |
fd361982 AM |
6024 | bfd_set_section_vma (os->bfd_section, |
6025 | bfd_section_vma (input)); | |
8772de11 MR |
6026 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
6027 | os->bfd_section->size = input->size; | |
252b5132 RH |
6028 | break; |
6029 | } | |
6030 | ||
a5df8c84 | 6031 | newdot = dot; |
13075d04 | 6032 | dotdelta = 0; |
252b5132 RH |
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 | { | |
1579bae1 | 6040 | if (os->addr_tree == NULL) |
252b5132 RH |
6041 | { |
6042 | /* No address specified for this section, get one | |
6043 | from the region specification. */ | |
1579bae1 | 6044 | if (os->region == NULL |
6feb9908 | 6045 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
6046 | && os->region->name_list.name[0] == '*' |
6047 | && strcmp (os->region->name_list.name, | |
6feb9908 | 6048 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
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 | |
66184979 | 6055 | defined, issue an error message. */ |
f0636a44 AM |
6056 | if (!os->ignored |
6057 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 6058 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 6059 | && check_regions |
4a93e180 | 6060 | && strcmp (os->region->name_list.name, |
6feb9908 | 6061 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 6062 | && lang_memory_region_list != NULL |
4a93e180 | 6063 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 6064 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a | 6065 | || lang_memory_region_list->next != NULL) |
baf09cba | 6066 | && lang_sizing_iteration == 1) |
66184979 NC |
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 | |
07f3b6ad KH |
6071 | excessively large binary, or even seg faulting when |
6072 | attempting to perform a negative seek. See | |
6feb9908 | 6073 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
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) | |
8d97c1a5 | 6078 | fatal (_("%P: error: no memory region specified" |
6feb9908 | 6079 | " for loadable section `%s'\n"), |
fd361982 | 6080 | bfd_section_name (os->bfd_section)); |
66184979 | 6081 | else |
6feb9908 AM |
6082 | einfo (_("%P: warning: no memory region specified" |
6083 | " for loadable section `%s'\n"), | |
fd361982 | 6084 | bfd_section_name (os->bfd_section)); |
66184979 | 6085 | } |
252b5132 | 6086 | |
e9ee469a | 6087 | newdot = os->region->current; |
c5b0a9ef | 6088 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 6089 | } |
94b50910 | 6090 | else |
b5c37946 | 6091 | section_alignment = exp_get_power (os->section_alignment, os, |
3d9c8f6b | 6092 | "section alignment"); |
5f992e62 | 6093 | |
7270c5ed | 6094 | /* Align to what the section needs. */ |
c5b0a9ef | 6095 | if (section_alignment > 0) |
94b50910 AM |
6096 | { |
6097 | bfd_vma savedot = newdot; | |
baf09cba | 6098 | bfd_vma diff = 0; |
252b5132 | 6099 | |
baf09cba | 6100 | newdot = align_power (newdot, section_alignment); |
13075d04 | 6101 | dotdelta = newdot - savedot; |
baf09cba AM |
6102 | |
6103 | if (lang_sizing_iteration == 1) | |
6104 | diff = dotdelta; | |
6105 | else if (lang_sizing_iteration > 1) | |
92d4b13b AM |
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 | } | |
baf09cba | 6113 | if (diff != 0 |
94b50910 | 6114 | && (config.warn_section_align |
baf09cba AM |
6115 | || os->addr_tree != NULL)) |
6116 | einfo (_("%P: warning: " | |
6117 | "start of section %s changed by %ld\n"), | |
6118 | os->name, (long) diff); | |
94b50910 | 6119 | } |
252b5132 | 6120 | |
fd361982 | 6121 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 6122 | |
252b5132 RH |
6123 | os->bfd_section->output_offset = 0; |
6124 | } | |
6125 | ||
e535e147 | 6126 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
6127 | os->fill, newdot, relax, check_regions); |
6128 | ||
f38a2680 | 6129 | os->processed_vma = true; |
e9ee469a AM |
6130 | |
6131 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
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); | |
cde9e0be | 6138 | else |
e9ee469a | 6139 | { |
cde9e0be AM |
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 | ||
8772de11 MR |
6149 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
6150 | os->bfd_section->size = TO_SIZE (after | |
6151 | - os->bfd_section->vma); | |
cde9e0be AM |
6152 | } |
6153 | ||
6154 | /* Set section lma. */ | |
6155 | r = os->region; | |
6156 | if (r == NULL) | |
f38a2680 | 6157 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
cde9e0be AM |
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; | |
e9ee469a | 6163 | } |
07dfcf38 | 6164 | else if (os->lma_region != NULL) |
cde9e0be AM |
6165 | { |
6166 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 6167 | |
13075d04 SH |
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) | |
3d9c8f6b | 6178 | section_alignment = exp_get_power (os->section_alignment, |
b5c37946 | 6179 | os, |
3d9c8f6b | 6180 | "section alignment"); |
13075d04 SH |
6181 | if (section_alignment > 0) |
6182 | lma = align_power (lma, section_alignment); | |
6183 | } | |
cde9e0be AM |
6184 | os->bfd_section->lma = lma; |
6185 | } | |
dc0b6aa0 AM |
6186 | else if (r->last_os != NULL |
6187 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
6188 | { |
6189 | bfd_vma lma; | |
6190 | asection *last; | |
6191 | ||
6192 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
6193 | |
6194 | /* A backwards move of dot should be accompanied by | |
6195 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 6196 | os->load_base set) because backwards moves can |
dc0b6aa0 | 6197 | create overlapping LMAs. */ |
264b6205 | 6198 | if (dot < last->vma |
91eb6c46 | 6199 | && os->bfd_section->size != 0 |
ca62bc4a | 6200 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 6201 | { |
dc0b6aa0 AM |
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 | |
91eb6c46 AM |
6205 | sufficiently large. Nag if this changes anything, |
6206 | so people can fix their linker scripts. */ | |
6207 | ||
6208 | if (last->vma != last->lma) | |
0aa7f586 AM |
6209 | einfo (_("%P: warning: dot moved backwards " |
6210 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
6211 | } |
6212 | else | |
cde9e0be | 6213 | { |
152d792f AM |
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) | |
fc90c280 | 6217 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
6218 | |
6219 | /* Otherwise, keep the same lma to vma relationship | |
6220 | as the previous section. */ | |
6221 | else | |
8610e0fd | 6222 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 6223 | |
c5b0a9ef AM |
6224 | if (section_alignment > 0) |
6225 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
6226 | os->bfd_section->lma = lma; |
6227 | } | |
6228 | } | |
f38a2680 | 6229 | os->processed_lma = true; |
5f992e62 | 6230 | |
cde9e0be AM |
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 | |
dc0b6aa0 AM |
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 | |
14ea2c1b AB |
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. */ | |
7b243801 AM |
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) | |
cde9e0be | 6253 | && os->lma_region == NULL |
0e1862bb | 6254 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 6255 | r->last_os = s; |
9f88b410 | 6256 | |
14ea2c1b AB |
6257 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
6258 | break; | |
6259 | ||
e5caec89 | 6260 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 6261 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 6262 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
6263 | dotdelta = TO_ADDR (os->bfd_section->size); |
6264 | else | |
6265 | dotdelta = 0; | |
6266 | dot += dotdelta; | |
e5caec89 | 6267 | |
9f88b410 | 6268 | if (os->update_dot_tree != 0) |
b5c37946 | 6269 | exp_fold_tree (os->update_dot_tree, os, bfd_abs_section_ptr, &dot); |
9f88b410 | 6270 | |
252b5132 RH |
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 | |
2e76e85a | 6274 | overall size in memory. */ |
1579bae1 | 6275 | if (os->region != NULL |
2e76e85a | 6276 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
6277 | { |
6278 | os->region->current = dot; | |
5f992e62 | 6279 | |
cf888e70 NC |
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); | |
08da4cac | 6284 | |
a2cab753 | 6285 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
6286 | && ((os->bfd_section->flags & SEC_LOAD) |
6287 | || os->align_lma_with_input)) | |
08da4cac | 6288 | { |
13075d04 | 6289 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 6290 | |
cf888e70 NC |
6291 | if (check_regions) |
6292 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 6293 | os->bfd_section->lma); |
08da4cac | 6294 | } |
252b5132 RH |
6295 | } |
6296 | } | |
6297 | break; | |
6298 | ||
6299 | case lang_constructors_statement_enum: | |
b5c37946 | 6300 | dot = lang_size_sections_1 (&constructor_list.head, current_os, |
cf888e70 | 6301 | fill, dot, relax, check_regions); |
252b5132 RH |
6302 | break; |
6303 | ||
6304 | case lang_data_statement_enum: | |
6305 | { | |
6306 | unsigned int size = 0; | |
6307 | ||
b5c37946 SJ |
6308 | s->data_statement.output_offset = dot - current_os->bfd_section->vma; |
6309 | s->data_statement.output_section = current_os->bfd_section; | |
252b5132 | 6310 | |
1b493742 NS |
6311 | /* We might refer to provided symbols in the expression, and |
6312 | need to mark them as needed. */ | |
b5c37946 SJ |
6313 | exp_fold_tree (s->data_statement.exp, os, |
6314 | bfd_abs_section_ptr, &dot); | |
1b493742 | 6315 | |
252b5132 RH |
6316 | switch (s->data_statement.type) |
6317 | { | |
08da4cac KH |
6318 | default: |
6319 | abort (); | |
252b5132 RH |
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 | } | |
e5caa5e0 AM |
6334 | if (size < TO_SIZE ((unsigned) 1)) |
6335 | size = TO_SIZE ((unsigned) 1); | |
6336 | dot += TO_ADDR (size); | |
b5c37946 SJ |
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); | |
aa4c3319 | 6340 | |
252b5132 RH |
6341 | } |
6342 | break; | |
6343 | ||
6344 | case lang_reloc_statement_enum: | |
6345 | { | |
6346 | int size; | |
6347 | ||
b5c37946 SJ |
6348 | s->reloc_statement.output_offset |
6349 | = dot - current_os->bfd_section->vma; | |
6350 | s->reloc_statement.output_section | |
6351 | = current_os->bfd_section; | |
252b5132 | 6352 | size = bfd_get_reloc_size (s->reloc_statement.howto); |
e5caa5e0 | 6353 | dot += TO_ADDR (size); |
b5c37946 SJ |
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); | |
252b5132 RH |
6357 | } |
6358 | break; | |
5f992e62 | 6359 | |
252b5132 | 6360 | case lang_wild_statement_enum: |
e535e147 | 6361 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
b5c37946 SJ |
6362 | current_os, fill, dot, relax, |
6363 | check_regions); | |
252b5132 RH |
6364 | break; |
6365 | ||
6366 | case lang_object_symbols_statement_enum: | |
b5c37946 SJ |
6367 | link_info.create_object_symbols_section = current_os->bfd_section; |
6368 | current_os->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 6369 | break; |
e9ee469a | 6370 | |
252b5132 RH |
6371 | case lang_output_statement_enum: |
6372 | case lang_target_statement_enum: | |
6373 | break; | |
e9ee469a | 6374 | |
252b5132 RH |
6375 | case lang_input_section_enum: |
6376 | { | |
6377 | asection *i; | |
6378 | ||
e535e147 | 6379 | i = s->input_section.section; |
eea6121a | 6380 | if (relax) |
252b5132 | 6381 | { |
f38a2680 | 6382 | bool again; |
252b5132 | 6383 | |
0aa7f586 | 6384 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
8d97c1a5 | 6385 | fatal (_("%P: can't relax section: %E\n")); |
252b5132 | 6386 | if (again) |
f38a2680 | 6387 | *relax = true; |
252b5132 | 6388 | } |
b5c37946 | 6389 | dot = size_input_section (prev, current_os, fill, &removed, dot); |
252b5132 RH |
6390 | } |
6391 | break; | |
e9ee469a | 6392 | |
252b5132 RH |
6393 | case lang_input_statement_enum: |
6394 | break; | |
e9ee469a | 6395 | |
252b5132 | 6396 | case lang_fill_statement_enum: |
b5c37946 | 6397 | s->fill_statement.output_section = current_os->bfd_section; |
252b5132 RH |
6398 | |
6399 | fill = s->fill_statement.fill; | |
6400 | break; | |
e9ee469a | 6401 | |
252b5132 RH |
6402 | case lang_assignment_statement_enum: |
6403 | { | |
6404 | bfd_vma newdot = dot; | |
49c13adb | 6405 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 6406 | |
0f99513f | 6407 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6408 | |
b5c37946 | 6409 | exp_fold_tree (tree, os, current_os->bfd_section, &newdot); |
252b5132 | 6410 | |
2085ee26 | 6411 | ldlang_check_relro_region (s); |
ed1794ee | 6412 | |
0f99513f | 6413 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6414 | |
d2667025 | 6415 | /* This symbol may be relative to this section. */ |
a14a5de3 | 6416 | if ((tree->type.node_class == etree_provided |
49c13adb L |
6417 | || tree->type.node_class == etree_assign) |
6418 | && (tree->assign.dst [0] != '.' | |
6419 | || tree->assign.dst [1] != '\0')) | |
b5c37946 | 6420 | current_os->update_dot = 1; |
49c13adb | 6421 | |
b5c37946 | 6422 | if (!current_os->ignored) |
252b5132 | 6423 | { |
b5c37946 | 6424 | if (current_os == abs_output_section) |
252b5132 RH |
6425 | { |
6426 | /* If we don't have an output section, then just adjust | |
6427 | the default memory address. */ | |
6feb9908 | 6428 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
f38a2680 | 6429 | false)->current = newdot; |
252b5132 | 6430 | } |
85852e36 | 6431 | else if (newdot != dot) |
252b5132 | 6432 | { |
b3327aad AM |
6433 | /* Insert a pad after this statement. We can't |
6434 | put the pad before when relaxing, in case the | |
6435 | assignment references dot. */ | |
e5caa5e0 | 6436 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b5c37946 | 6437 | current_os->bfd_section, dot); |
b3327aad AM |
6438 | |
6439 | /* Don't neuter the pad below when relaxing. */ | |
6440 | s = s->header.next; | |
9dfc8ab2 | 6441 | |
e9ee469a AM |
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 | |
2e76e85a | 6445 | should not be allocated. */ |
b5c37946 SJ |
6446 | if (current_os->sectype != noalloc_section |
6447 | && (current_os->sectype != noload_section | |
f4eaaf7f AM |
6448 | || (bfd_get_flavour (link_info.output_bfd) |
6449 | == bfd_target_elf_flavour))) | |
b5c37946 | 6450 | current_os->bfd_section->flags |= SEC_ALLOC; |
e9ee469a | 6451 | } |
252b5132 RH |
6452 | dot = newdot; |
6453 | } | |
6454 | } | |
6455 | break; | |
6456 | ||
6457 | case lang_padding_statement_enum: | |
c0c330a7 AM |
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; | |
6e814ff8 AM |
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 | |
b5c37946 | 6471 | = dot - current_os->bfd_section->vma; |
252b5132 RH |
6472 | break; |
6473 | ||
6474 | case lang_group_statement_enum: | |
e535e147 | 6475 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
b5c37946 SJ |
6476 | current_os, fill, dot, relax, |
6477 | check_regions); | |
252b5132 RH |
6478 | break; |
6479 | ||
53d25da6 | 6480 | case lang_insert_statement_enum: |
252b5132 RH |
6481 | break; |
6482 | ||
c0c330a7 | 6483 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
6484 | case lang_address_statement_enum: |
6485 | break; | |
53d25da6 AM |
6486 | |
6487 | default: | |
6488 | FAIL (); | |
6489 | break; | |
252b5132 | 6490 | } |
abf874aa CL |
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; | |
f38a2680 | 6509 | removed_prev_s = false; |
abf874aa CL |
6510 | } |
6511 | else | |
6512 | { | |
6513 | /* Remove the first input section of the list. */ | |
6514 | *prev = s->header.next; | |
f38a2680 | 6515 | removed_prev_s = true; |
abf874aa CL |
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; | |
f38a2680 | 6526 | removed_prev_s = false; |
abf874aa | 6527 | } |
252b5132 RH |
6528 | } |
6529 | return dot; | |
6530 | } | |
6531 | ||
591a748a NC |
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. */ | |
2889e75b | 6536 | |
f38a2680 | 6537 | bool |
0aa7f586 AM |
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, | |
f38a2680 | 6542 | bool new_segment) |
2889e75b | 6543 | { |
0aa7f586 AM |
6544 | lang_output_section_statement_type *cur; |
6545 | lang_output_section_statement_type *prev; | |
591a748a NC |
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. */ | |
2889e75b | 6549 | if (new_segment) |
f38a2680 | 6550 | return true; |
2889e75b NC |
6551 | |
6552 | /* Paranoia checks. */ | |
6553 | if (current_section == NULL || previous_section == NULL) | |
6554 | return new_segment; | |
6555 | ||
4724d37e RM |
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)) | |
f38a2680 | 6560 | return true; |
4724d37e | 6561 | |
2889e75b NC |
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 | |
591a748a | 6565 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6566 | cur = lang_output_section_find (current_section->name); |
6567 | prev = lang_output_section_find (previous_section->name); | |
6568 | ||
591a748a | 6569 | /* More paranoia. */ |
2889e75b NC |
6570 | if (cur == NULL || prev == NULL) |
6571 | return new_segment; | |
6572 | ||
6573 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6574 | different segments. See the email thread starting at the following |
6575 | URL for the reasons why this is necessary: | |
2889e75b NC |
6576 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6577 | return cur->region != prev->region; | |
6578 | } | |
6579 | ||
e9ee469a | 6580 | void |
f38a2680 | 6581 | one_lang_size_sections_pass (bool *relax, bool check_regions) |
2d20f7bf | 6582 | { |
420e579c | 6583 | lang_statement_iteration++; |
baf09cba AM |
6584 | if (expld.phase != lang_mark_phase_enum) |
6585 | lang_sizing_iteration++; | |
e535e147 AM |
6586 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6587 | 0, 0, relax, check_regions); | |
e9ee469a | 6588 | } |
420e579c | 6589 | |
f38a2680 | 6590 | static bool |
2085ee26 | 6591 | lang_size_segment (void) |
e9ee469a | 6592 | { |
cba6246d L |
6593 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6594 | a page could be saved in the data segment. */ | |
2085ee26 | 6595 | seg_align_type *seg = &expld.dataseg; |
cba6246d L |
6596 | bfd_vma first, last; |
6597 | ||
9833b775 AM |
6598 | first = -seg->base & (seg->commonpagesize - 1); |
6599 | last = seg->end & (seg->commonpagesize - 1); | |
cba6246d | 6600 | if (first && last |
9833b775 AM |
6601 | && ((seg->base & ~(seg->commonpagesize - 1)) |
6602 | != (seg->end & ~(seg->commonpagesize - 1))) | |
6603 | && first + last <= seg->commonpagesize) | |
8c37241b | 6604 | { |
cba6246d | 6605 | seg->phase = exp_seg_adjust; |
f38a2680 | 6606 | return true; |
cba6246d L |
6607 | } |
6608 | ||
6609 | seg->phase = exp_seg_done; | |
f38a2680 | 6610 | return false; |
cba6246d L |
6611 | } |
6612 | ||
6613 | static bfd_vma | |
2085ee26 | 6614 | lang_size_relro_segment_1 (void) |
cba6246d | 6615 | { |
2085ee26 | 6616 | seg_align_type *seg = &expld.dataseg; |
5197c88e | 6617 | bfd_vma relro_end, desired_end; |
6a9d0866 | 6618 | asection *sec; |
8c37241b | 6619 | |
cba6246d | 6620 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
9833b775 | 6621 | relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; |
0e5fabeb | 6622 | |
cba6246d | 6623 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
5197c88e | 6624 | desired_end = relro_end - seg->relro_offset; |
0e5fabeb | 6625 | |
5197c88e | 6626 | /* For sections in the relro segment.. */ |
cba6246d | 6627 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) |
6a9d0866 AM |
6628 | if ((sec->flags & SEC_ALLOC) != 0 |
6629 | && sec->vma >= seg->base | |
6630 | && sec->vma < seg->relro_end - seg->relro_offset) | |
cba6246d | 6631 | { |
6a9d0866 AM |
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; | |
5197c88e AM |
6646 | } |
6647 | ||
6648 | seg->phase = exp_seg_relro_adjust; | |
6649 | ASSERT (desired_end >= seg->base); | |
5197c88e | 6650 | seg->base = desired_end; |
cba6246d L |
6651 | return relro_end; |
6652 | } | |
6653 | ||
f38a2680 AM |
6654 | static bool |
6655 | lang_size_relro_segment (bool *relax, bool check_regions) | |
cba6246d | 6656 | { |
f38a2680 | 6657 | bool do_reset = false; |
cba6246d L |
6658 | |
6659 | if (link_info.relro && expld.dataseg.relro_end) | |
6660 | { | |
e29de63f | 6661 | bfd_vma data_initial_base = expld.dataseg.base; |
2085ee26 | 6662 | bfd_vma data_relro_end = lang_size_relro_segment_1 (); |
0e5fabeb | 6663 | |
f86a8756 | 6664 | lang_reset_memory_regions (); |
e9ee469a | 6665 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6666 | |
cba6246d L |
6667 | /* Assignments to dot, or to output section address in a user |
6668 | script have increased padding over the original. Revert. */ | |
e29de63f | 6669 | if (expld.dataseg.relro_end > data_relro_end) |
a4f5ad88 | 6670 | { |
2085ee26 | 6671 | expld.dataseg.base = data_initial_base; |
f38a2680 | 6672 | do_reset = true; |
a4f5ad88 | 6673 | } |
8c37241b | 6674 | } |
2085ee26 | 6675 | else if (lang_size_segment ()) |
f38a2680 | 6676 | do_reset = true; |
cba6246d L |
6677 | |
6678 | return do_reset; | |
6679 | } | |
6680 | ||
6681 | void | |
f38a2680 | 6682 | lang_size_sections (bool *relax, bool check_regions) |
cba6246d L |
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) | |
2d20f7bf | 6693 | { |
f38a2680 | 6694 | bool do_reset |
cba6246d L |
6695 | = lang_size_relro_segment (relax, check_regions); |
6696 | ||
6697 | if (do_reset) | |
2d20f7bf | 6698 | { |
f86a8756 | 6699 | lang_reset_memory_regions (); |
e9ee469a | 6700 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6701 | } |
cba6246d L |
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 | } | |
2d20f7bf | 6708 | } |
2d20f7bf JJ |
6709 | } |
6710 | ||
d2667025 AM |
6711 | static lang_output_section_statement_type *current_section; |
6712 | static lang_assignment_statement_type *current_assign; | |
f38a2680 | 6713 | static bool prefer_next_section; |
d2667025 | 6714 | |
420e579c HPN |
6715 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6716 | ||
6717 | static bfd_vma | |
66e28d60 AM |
6718 | lang_do_assignments_1 (lang_statement_union_type *s, |
6719 | lang_output_section_statement_type *current_os, | |
6720 | fill_type *fill, | |
d2667025 | 6721 | bfd_vma dot, |
f38a2680 | 6722 | bool *found_end) |
252b5132 | 6723 | { |
b5c37946 SJ |
6724 | lang_output_section_statement_type *os = current_os; |
6725 | ||
1579bae1 | 6726 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6727 | { |
6728 | switch (s->header.type) | |
6729 | { | |
6730 | case lang_constructors_statement_enum: | |
420e579c | 6731 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6732 | current_os, fill, dot, found_end); |
252b5132 RH |
6733 | break; |
6734 | ||
6735 | case lang_output_section_statement_enum: | |
6736 | { | |
f02cb058 | 6737 | bfd_vma newdot; |
252b5132 | 6738 | |
b5c37946 | 6739 | os = &s->output_section_statement; |
d2667025 | 6740 | os->after_end = *found_end; |
61826503 | 6741 | init_opb (os->bfd_section); |
1cba418d AM |
6742 | newdot = dot; |
6743 | if (os->bfd_section != NULL) | |
252b5132 | 6744 | { |
1cba418d | 6745 | if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) |
d2667025 AM |
6746 | { |
6747 | current_section = os; | |
f38a2680 | 6748 | prefer_next_section = false; |
d2667025 | 6749 | } |
1cba418d | 6750 | newdot = os->bfd_section->vma; |
f02cb058 AM |
6751 | } |
6752 | newdot = lang_do_assignments_1 (os->children.head, | |
1cba418d | 6753 | os, os->fill, newdot, found_end); |
f02cb058 AM |
6754 | if (!os->ignored) |
6755 | { | |
6756 | if (os->bfd_section != NULL) | |
6757 | { | |
1cba418d AM |
6758 | newdot = os->bfd_section->vma; |
6759 | ||
f02cb058 AM |
6760 | /* .tbss sections effectively have zero size. */ |
6761 | if (!IS_TBSS (os->bfd_section) | |
6762 | || bfd_link_relocatable (&link_info)) | |
1cba418d | 6763 | newdot += TO_ADDR (os->bfd_section->size); |
f02cb058 AM |
6764 | |
6765 | if (os->update_dot_tree != NULL) | |
b5c37946 | 6766 | exp_fold_tree (os->update_dot_tree, os, |
1cba418d | 6767 | bfd_abs_section_ptr, &newdot); |
f02cb058 | 6768 | } |
1cba418d | 6769 | dot = newdot; |
252b5132 RH |
6770 | } |
6771 | } | |
6772 | break; | |
e9ee469a | 6773 | |
252b5132 RH |
6774 | case lang_wild_statement_enum: |
6775 | ||
420e579c | 6776 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6777 | current_os, fill, dot, found_end); |
252b5132 RH |
6778 | break; |
6779 | ||
6780 | case lang_object_symbols_statement_enum: | |
6781 | case lang_output_statement_enum: | |
6782 | case lang_target_statement_enum: | |
252b5132 | 6783 | break; |
e9ee469a | 6784 | |
252b5132 | 6785 | case lang_data_statement_enum: |
b5c37946 | 6786 | exp_fold_tree (s->data_statement.exp, os, bfd_abs_section_ptr, &dot); |
e9ee469a | 6787 | if (expld.result.valid_p) |
7542af2a AM |
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 | } | |
f02cb058 | 6793 | else if (expld.phase == lang_final_phase_enum) |
8d97c1a5 | 6794 | fatal (_("%P: invalid data statement\n")); |
b7a26f91 KH |
6795 | { |
6796 | unsigned int size; | |
08da4cac KH |
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 | } | |
e5caa5e0 AM |
6815 | if (size < TO_SIZE ((unsigned) 1)) |
6816 | size = TO_SIZE ((unsigned) 1); | |
6817 | dot += TO_ADDR (size); | |
08da4cac | 6818 | } |
252b5132 RH |
6819 | break; |
6820 | ||
6821 | case lang_reloc_statement_enum: | |
b5c37946 | 6822 | exp_fold_tree (s->reloc_statement.addend_exp, os, |
e9ee469a AM |
6823 | bfd_abs_section_ptr, &dot); |
6824 | if (expld.result.valid_p) | |
6825 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6826 | else if (expld.phase == lang_final_phase_enum) |
8d97c1a5 | 6827 | fatal (_("%P: invalid reloc statement\n")); |
e5caa5e0 | 6828 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6829 | break; |
6830 | ||
6831 | case lang_input_section_enum: | |
6832 | { | |
6833 | asection *in = s->input_section.section; | |
6834 | ||
57ceae94 | 6835 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6836 | dot += TO_ADDR (in->size); |
252b5132 RH |
6837 | } |
6838 | break; | |
6839 | ||
6840 | case lang_input_statement_enum: | |
6841 | break; | |
e9ee469a | 6842 | |
252b5132 RH |
6843 | case lang_fill_statement_enum: |
6844 | fill = s->fill_statement.fill; | |
6845 | break; | |
252b5132 | 6846 | |
e9ee469a | 6847 | case lang_assignment_statement_enum: |
d2667025 AM |
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) | |
f38a2680 | 6854 | prefer_next_section = true; |
d2667025 AM |
6855 | |
6856 | while (*p == '_') | |
6857 | ++p; | |
6858 | if (strcmp (p, "end") == 0) | |
f38a2680 | 6859 | *found_end = true; |
d2667025 | 6860 | } |
b5c37946 | 6861 | exp_fold_tree (s->assignment_statement.exp, os, |
f02cb058 AM |
6862 | (current_os->bfd_section != NULL |
6863 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6864 | &dot); |
252b5132 | 6865 | break; |
e9ee469a | 6866 | |
252b5132 | 6867 | case lang_padding_statement_enum: |
e5caa5e0 | 6868 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6869 | break; |
6870 | ||
6871 | case lang_group_statement_enum: | |
420e579c | 6872 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6873 | current_os, fill, dot, found_end); |
252b5132 RH |
6874 | break; |
6875 | ||
53d25da6 | 6876 | case lang_insert_statement_enum: |
252b5132 | 6877 | break; |
e9ee469a | 6878 | |
252b5132 RH |
6879 | case lang_address_statement_enum: |
6880 | break; | |
53d25da6 AM |
6881 | |
6882 | default: | |
6883 | FAIL (); | |
6884 | break; | |
252b5132 | 6885 | } |
252b5132 RH |
6886 | } |
6887 | return dot; | |
6888 | } | |
6889 | ||
f2241121 | 6890 | void |
2f65ac72 | 6891 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6892 | { |
f38a2680 | 6893 | bool found_end = false; |
d2667025 AM |
6894 | |
6895 | current_section = NULL; | |
f38a2680 | 6896 | prefer_next_section = false; |
2f65ac72 | 6897 | expld.phase = phase; |
420e579c | 6898 | lang_statement_iteration++; |
d2667025 AM |
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 | ||
10c00056 | 6931 | os = stmt ? &stmt->output_section_statement : NULL; |
d2667025 AM |
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; | |
420e579c HPN |
6976 | } |
6977 | ||
7dba9362 AM |
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. */ | |
252b5132 RH |
7009 | |
7010 | static void | |
7dba9362 | 7011 | lang_init_start_stop (void) |
252b5132 | 7012 | { |
7dba9362 | 7013 | bfd *abfd; |
252b5132 | 7014 | asection *s; |
7dba9362 AM |
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)); | |
b27685f2 | 7029 | |
7dba9362 AM |
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) | |
b27685f2 L |
7064 | return; |
7065 | ||
7dba9362 AM |
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 | { | |
4d1c6335 L |
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 | } | |
7dba9362 AM |
7089 | h->type = bfd_link_hash_undefined; |
7090 | h->u.undef.abfd = NULL; | |
fc304b88 | 7091 | if (is_elf_hash_table (link_info.hash)) |
7824c1d2 AM |
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); | |
f38a2680 | 7098 | (*bed->elf_backend_hide_symbol) (&link_info, eh, true); |
7824c1d2 AM |
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 | } | |
7dba9362 AM |
7104 | } |
7105 | } | |
252b5132 | 7106 | |
7dba9362 AM |
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; | |
252b5132 | 7121 | |
f13a99db | 7122 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7123 | { |
7dba9362 AM |
7124 | const char *secname = s->name; |
7125 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 7126 | |
7dba9362 AM |
7127 | sprintf (symbol, ".startof.%s", secname); |
7128 | lang_define_start_stop (symbol, s); | |
252b5132 | 7129 | |
7dba9362 AM |
7130 | memcpy (symbol + 1, ".size", 5); |
7131 | lang_define_start_stop (symbol + 1, s); | |
7132 | free (symbol); | |
7133 | } | |
7134 | } | |
cbd0eecf | 7135 | |
7dba9362 AM |
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; | |
cbd0eecf | 7144 | |
7dba9362 AM |
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') | |
252b5132 | 7150 | { |
7dba9362 AM |
7151 | /* .sizeof. */ |
7152 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
7153 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 7154 | } |
7dba9362 AM |
7155 | } |
7156 | else | |
7157 | { | |
7158 | /* __start or __stop symbol. */ | |
7159 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 7160 | |
7dba9362 AM |
7161 | h->u.def.section = h->u.def.section->output_section; |
7162 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 7163 | { |
7dba9362 AM |
7164 | /* __stop_ */ |
7165 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 7166 | } |
252b5132 RH |
7167 | } |
7168 | } | |
7169 | ||
7dba9362 AM |
7170 | static void |
7171 | lang_finalize_start_stop (void) | |
7172 | { | |
7173 | foreach_start_stop (set_start_stop); | |
7174 | } | |
7175 | ||
97da0e26 AM |
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; | |
5617fae7 | 7209 | h->root.rel_from_abs = 1; |
bc61d591 | 7210 | elf_hash_table (&link_info)->hehdr_start = h; |
97da0e26 AM |
7211 | } |
7212 | } | |
7213 | } | |
7214 | ||
252b5132 | 7215 | static void |
750877ba | 7216 | lang_end (void) |
252b5132 RH |
7217 | { |
7218 | struct bfd_link_hash_entry *h; | |
f38a2680 | 7219 | bool warn; |
252b5132 | 7220 | |
0e1862bb L |
7221 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
7222 | || bfd_link_dll (&link_info)) | |
71934f94 | 7223 | warn = entry_from_cmdline; |
252b5132 | 7224 | else |
f38a2680 | 7225 | warn = true; |
252b5132 | 7226 | |
ac69cbc6 | 7227 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
7228 | --gc-sections, unless --gc-keep-exported was also given. */ |
7229 | if (bfd_link_relocatable (&link_info) | |
7230 | && link_info.gc_sections | |
2f5541f3 AM |
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, | |
f38a2680 | 7238 | false, false, false); |
a965df05 | 7239 | if (is_defined (h) |
2f5541f3 AM |
7240 | && !bfd_is_const_section (h->u.def.section)) |
7241 | break; | |
7242 | } | |
7243 | if (!sym) | |
8d97c1a5 | 7244 | fatal (_("%P: --gc-sections requires a defined symbol root " |
2f5541f3 AM |
7245 | "specified by -e or -u\n")); |
7246 | } | |
ac69cbc6 | 7247 | |
1579bae1 | 7248 | if (entry_symbol.name == NULL) |
252b5132 | 7249 | { |
a359509e ZW |
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; | |
f38a2680 | 7253 | warn = false; |
252b5132 RH |
7254 | } |
7255 | ||
e3e942e9 | 7256 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
f38a2680 | 7257 | false, false, true); |
a965df05 PR |
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) | |
252b5132 RH |
7263 | && h->u.def.section->output_section != NULL) |
7264 | { | |
7265 | bfd_vma val; | |
7266 | ||
7267 | val = (h->u.def.value | |
fd361982 | 7268 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 7269 | + h->u.def.section->output_offset); |
0aa7f586 | 7270 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
8d97c1a5 | 7271 | fatal (_("%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
7272 | } |
7273 | else | |
7274 | { | |
7275 | bfd_vma val; | |
5f992e62 | 7276 | const char *send; |
252b5132 RH |
7277 | |
7278 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 7279 | number. */ |
e3e942e9 | 7280 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
7281 | if (*send == '\0') |
7282 | { | |
0aa7f586 | 7283 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
8d97c1a5 | 7284 | fatal (_("%P: can't set start address\n")); |
252b5132 | 7285 | } |
5226a6a8 NC |
7286 | /* BZ 2004952: Only use the start of the entry section for executables. */ |
7287 | else if bfd_link_executable (&link_info) | |
252b5132 RH |
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. */ | |
f13a99db | 7293 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 7294 | if (ts != NULL) |
252b5132 RH |
7295 | { |
7296 | if (warn) | |
6feb9908 AM |
7297 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7298 | " defaulting to %V\n"), | |
e3e942e9 | 7299 | entry_symbol.name, |
fd361982 AM |
7300 | bfd_section_vma (ts)); |
7301 | if (!bfd_set_start_address (link_info.output_bfd, | |
7302 | bfd_section_vma (ts))) | |
8d97c1a5 | 7303 | fatal (_("%P: can't set start address\n")); |
252b5132 RH |
7304 | } |
7305 | else | |
7306 | { | |
7307 | if (warn) | |
6feb9908 AM |
7308 | einfo (_("%P: warning: cannot find entry symbol %s;" |
7309 | " not setting start address\n"), | |
e3e942e9 | 7310 | entry_symbol.name); |
252b5132 RH |
7311 | } |
7312 | } | |
5226a6a8 NC |
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 | } | |
252b5132 RH |
7320 | } |
7321 | } | |
7322 | ||
7323 | /* This is a small function used when we want to ignore errors from | |
7324 | BFD. */ | |
7325 | ||
7326 | static void | |
52d45da3 AM |
7327 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
7328 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
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 | |
1579bae1 | 7338 | lang_check (void) |
252b5132 | 7339 | { |
36983a93 | 7340 | lang_input_statement_type *file; |
252b5132 | 7341 | bfd *input_bfd; |
5f992e62 | 7342 | const bfd_arch_info_type *compatible; |
252b5132 | 7343 | |
8ce18f9c | 7344 | for (file = (void *) file_chain.head; |
36983a93 AM |
7345 | file != NULL; |
7346 | file = file->next) | |
252b5132 | 7347 | { |
0381901e | 7348 | #if BFD_SUPPORTS_PLUGINS |
422b6f14 | 7349 | /* Don't check format of files claimed by plugin. */ |
36983a93 | 7350 | if (file->flags.claimed) |
422b6f14 | 7351 | continue; |
0381901e | 7352 | #endif /* BFD_SUPPORTS_PLUGINS */ |
36983a93 | 7353 | input_bfd = file->the_bfd; |
6feb9908 | 7354 | compatible |
f13a99db | 7355 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 7356 | command_line.accept_unknown_input_arch); |
30cba025 AM |
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). */ | |
f437dadd AM |
7364 | if (!file->flags.just_syms |
7365 | && (bfd_link_relocatable (&link_info) | |
7366 | || link_info.emitrelocations) | |
30cba025 | 7367 | && (compatible == NULL |
f13a99db AM |
7368 | || (bfd_get_flavour (input_bfd) |
7369 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
7370 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
7371 | { | |
8d97c1a5 | 7372 | fatal (_("%P: relocatable linking with relocations from" |
871b3ab2 | 7373 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 7374 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 7375 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
7376 | } |
7377 | ||
252b5132 RH |
7378 | if (compatible == NULL) |
7379 | { | |
7380 | if (command_line.warn_mismatch) | |
df5f2391 | 7381 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 7382 | " is incompatible with %s output\n"), |
252b5132 | 7383 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 7384 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 7385 | } |
b9247304 | 7386 | |
a8acd6ee AM |
7387 | /* If the input bfd has no contents, it shouldn't set the |
7388 | private data of the output bfd. */ | |
f437dadd AM |
7389 | else if (!file->flags.just_syms |
7390 | && ((input_bfd->flags & DYNAMIC) != 0 | |
7391 | || bfd_count_sections (input_bfd) != 0)) | |
a8acd6ee | 7392 | { |
252b5132 RH |
7393 | bfd_error_handler_type pfn = NULL; |
7394 | ||
7395 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
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. */ | |
0aa7f586 | 7400 | if (!command_line.warn_mismatch) |
252b5132 | 7401 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 7402 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
7403 | { |
7404 | if (command_line.warn_mismatch) | |
df5f2391 | 7405 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 7406 | " of file %pB\n"), input_bfd); |
252b5132 | 7407 | } |
0aa7f586 | 7408 | if (!command_line.warn_mismatch) |
252b5132 RH |
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 | |
de7dd2bd | 7416 | to roughly sort the entries by alignment. */ |
252b5132 RH |
7417 | |
7418 | static void | |
1579bae1 | 7419 | lang_common (void) |
252b5132 | 7420 | { |
a4819f54 | 7421 | if (link_info.inhibit_common_definition) |
4818e05f | 7422 | return; |
0e1862bb | 7423 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 7424 | && !command_line.force_common_definition) |
252b5132 RH |
7425 | return; |
7426 | ||
0aa7f586 | 7427 | if (!config.sort_common) |
1579bae1 | 7428 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
7429 | else |
7430 | { | |
de7dd2bd NC |
7431 | unsigned int power; |
7432 | ||
7433 | if (config.sort_common == sort_descending) | |
967928e9 AM |
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 | } | |
de7dd2bd | 7441 | else |
967928e9 AM |
7442 | { |
7443 | for (power = 0; power <= 4; power++) | |
7444 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 7445 | |
2dc0e7b4 | 7446 | power = (unsigned int) -1; |
de7dd2bd NC |
7447 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
7448 | } | |
252b5132 RH |
7449 | } |
7450 | } | |
7451 | ||
7452 | /* Place one common symbol in the correct section. */ | |
7453 | ||
f38a2680 | 7454 | static bool |
1579bae1 | 7455 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
7456 | { |
7457 | unsigned int power_of_two; | |
7458 | bfd_vma size; | |
7459 | asection *section; | |
7460 | ||
7461 | if (h->type != bfd_link_hash_common) | |
f38a2680 | 7462 | return true; |
252b5132 RH |
7463 | |
7464 | size = h->u.c.size; | |
7465 | power_of_two = h->u.c.p->alignment_power; | |
7466 | ||
de7dd2bd NC |
7467 | if (config.sort_common == sort_descending |
7468 | && power_of_two < *(unsigned int *) info) | |
f38a2680 | 7469 | return true; |
de7dd2bd | 7470 | else if (config.sort_common == sort_ascending |
967928e9 | 7471 | && power_of_two > *(unsigned int *) info) |
f38a2680 | 7472 | return true; |
252b5132 RH |
7473 | |
7474 | section = h->u.c.p->section; | |
3023e3f6 | 7475 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
8d97c1a5 | 7476 | fatal (_("%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 7477 | h->root.string); |
252b5132 RH |
7478 | |
7479 | if (config.map_file != NULL) | |
7480 | { | |
f38a2680 | 7481 | static bool header_printed; |
252b5132 RH |
7482 | int len; |
7483 | char *name; | |
6b9bd54c | 7484 | char buf[32]; |
252b5132 | 7485 | |
0aa7f586 | 7486 | if (!header_printed) |
252b5132 RH |
7487 | { |
7488 | minfo (_("\nAllocating common symbols\n")); | |
7489 | minfo (_("Common symbol size file\n\n")); | |
f38a2680 | 7490 | header_printed = true; |
252b5132 RH |
7491 | } |
7492 | ||
f13a99db | 7493 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 7494 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
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 | } | |
252b5132 RH |
7506 | |
7507 | if (len >= 19) | |
7508 | { | |
7509 | print_nl (); | |
7510 | len = 0; | |
7511 | } | |
252b5132 | 7512 | |
f493c217 | 7513 | sprintf (buf, "%" PRIx64, (uint64_t) size); |
6b9bd54c | 7514 | fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); |
252b5132 | 7515 | |
871b3ab2 | 7516 | minfo ("%pB\n", section->owner); |
252b5132 RH |
7517 | } |
7518 | ||
f38a2680 | 7519 | return true; |
252b5132 RH |
7520 | } |
7521 | ||
c005eb9e AB |
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; | |
21401fc7 | 7532 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); |
c005eb9e AB |
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); | |
b209b5a6 | 7537 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
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) | |
df5f2391 | 7546 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
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 | { | |
21401fc7 | 7555 | os = lang_output_section_statement_lookup (name, constraint, 1); |
c005eb9e AB |
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); | |
b209b5a6 | 7560 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7561 | } |
7562 | ||
7563 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 7564 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 7565 | "placed in section `%s'\n"), |
c005eb9e AB |
7566 | s, s->owner, os->name); |
7567 | } | |
7568 | } | |
7569 | ||
08da4cac KH |
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. */ | |
252b5132 RH |
7573 | |
7574 | static void | |
1579bae1 | 7575 | lang_place_orphans (void) |
252b5132 | 7576 | { |
e50d8076 | 7577 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
7578 | { |
7579 | asection *s; | |
7580 | ||
1579bae1 | 7581 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7582 | { |
1579bae1 | 7583 | if (s->output_section == NULL) |
252b5132 | 7584 | { |
396a2467 | 7585 | /* This section of the file is not attached, root |
afd7a018 | 7586 | around for a sensible place for it to go. */ |
252b5132 | 7587 | |
66be1055 | 7588 | if (file->flags.just_syms) |
1449d79b | 7589 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 7590 | else if (lang_discard_section_p (s)) |
164e712d | 7591 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
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. */ | |
0e1862bb | 7597 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
7598 | || command_line.force_common_definition) |
7599 | { | |
7600 | if (default_common_section == NULL) | |
66c103b7 | 7601 | default_common_section |
21401fc7 | 7602 | = lang_output_section_statement_lookup (".bss", 0, 1); |
39dcfe18 | 7603 | lang_add_section (&default_common_section->children, s, |
b209b5a6 | 7604 | NULL, NULL, default_common_section); |
252b5132 RH |
7605 | } |
7606 | } | |
252b5132 | 7607 | else |
c005eb9e | 7608 | ldlang_place_orphan (s); |
252b5132 RH |
7609 | } |
7610 | } | |
7611 | } | |
7612 | } | |
7613 | ||
252b5132 | 7614 | void |
1579bae1 | 7615 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7616 | { |
aa8804e4 | 7617 | flagword *ptr_flags; |
252b5132 | 7618 | |
aa8804e4 | 7619 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7620 | |
252b5132 RH |
7621 | while (*flags) |
7622 | { | |
7623 | switch (*flags) | |
7624 | { | |
f0673d20 NC |
7625 | /* PR 17900: An exclamation mark in the attributes reverses |
7626 | the sense of any of the attributes that follow. */ | |
7627 | case '!': | |
0aa7f586 | 7628 | invert = !invert; |
f0673d20 NC |
7629 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7630 | break; | |
7631 | ||
252b5132 RH |
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: | |
8d97c1a5 | 7654 | fatal (_("%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7655 | *flags, *flags); |
252b5132 RH |
7656 | break; |
7657 | } | |
7658 | flags++; | |
7659 | } | |
7660 | } | |
7661 | ||
d871d478 AM |
7662 | /* Call a function on each real input file. This function will be |
7663 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7664 | |
7665 | void | |
1579bae1 | 7666 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7667 | { |
7668 | lang_input_statement_type *f; | |
7669 | ||
8ce18f9c | 7670 | for (f = (void *) input_file_chain.head; |
252b5132 | 7671 | f != NULL; |
36983a93 | 7672 | f = f->next_real_file) |
d871d478 AM |
7673 | if (f->flags.real) |
7674 | func (f); | |
252b5132 RH |
7675 | } |
7676 | ||
d871d478 AM |
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. */ | |
252b5132 RH |
7680 | |
7681 | void | |
1579bae1 | 7682 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7683 | { |
e50d8076 | 7684 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7685 | { |
d871d478 AM |
7686 | if (f->flags.real) |
7687 | func (f); | |
252b5132 RH |
7688 | } |
7689 | } | |
7690 | ||
252b5132 | 7691 | void |
1579bae1 | 7692 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7693 | { |
36983a93 | 7694 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7695 | |
7696 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7697 | a link. */ | |
9221725d AM |
7698 | ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next |
7699 | && entry->the_bfd->link.next == NULL); | |
f13a99db | 7700 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7701 | |
7702 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7703 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7704 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
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 | ||
1579bae1 | 7717 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7718 | } |
7719 | ||
7720 | void | |
1579bae1 | 7721 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7722 | { |
7723 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7724 | if (!had_output_filename || !from_script) |
252b5132 RH |
7725 | { |
7726 | output_filename = name; | |
f38a2680 | 7727 | had_output_filename = true; |
252b5132 RH |
7728 | } |
7729 | } | |
7730 | ||
aea4bd9d | 7731 | lang_output_section_statement_type * |
1579bae1 AM |
7732 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7733 | etree_type *address_exp, | |
7734 | enum section_type sectype, | |
c212f39d | 7735 | etree_type *sectype_value, |
1579bae1 AM |
7736 | etree_type *align, |
7737 | etree_type *subalign, | |
0841712e | 7738 | etree_type *ebase, |
1eec346e NC |
7739 | int constraint, |
7740 | int align_with_input) | |
252b5132 RH |
7741 | { |
7742 | lang_output_section_statement_type *os; | |
7743 | ||
66c103b7 | 7744 | os = lang_output_section_statement_lookup (output_section_statement_name, |
6e8376fa NC |
7745 | constraint, |
7746 | in_section_ordering ? 0 : 2); | |
7747 | if (os == NULL) /* && in_section_ordering */ | |
8d97c1a5 | 7748 | fatal (_("%P:%pS: error: output section '%s' must already exist\n"), |
6e8376fa | 7749 | NULL, output_section_statement_name); |
967928e9 | 7750 | current_section = os; |
252b5132 | 7751 | |
6e8376fa NC |
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 | ||
1579bae1 | 7758 | if (os->addr_tree == NULL) |
6e8376fa NC |
7759 | os->addr_tree = address_exp; |
7760 | ||
252b5132 | 7761 | os->sectype = sectype; |
c212f39d FS |
7762 | if (sectype == type_section || sectype == typed_readonly_section) |
7763 | os->sectype_value = sectype_value; | |
7764 | else if (sectype == noload_section) | |
252b5132 | 7765 | os->flags = SEC_NEVER_LOAD; |
c212f39d FS |
7766 | else |
7767 | os->flags = SEC_NO_FLAGS; | |
e5caa5e0 | 7768 | os->block_value = 1; |
66c103b7 | 7769 | |
1eec346e NC |
7770 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7771 | if (os->align_lma_with_input && align != NULL) | |
8d97c1a5 | 7772 | fatal (_("%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7773 | NULL); |
1eec346e | 7774 | |
3d9c8f6b AM |
7775 | os->subsection_alignment = subalign; |
7776 | os->section_alignment = align; | |
252b5132 RH |
7777 | |
7778 | os->load_base = ebase; | |
aea4bd9d | 7779 | return os; |
252b5132 RH |
7780 | } |
7781 | ||
252b5132 | 7782 | void |
1579bae1 | 7783 | lang_final (void) |
252b5132 | 7784 | { |
d3ce72d0 NC |
7785 | lang_output_statement_type *new_stmt; |
7786 | ||
7787 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7788 | new_stmt->name = output_filename; | |
252b5132 RH |
7789 | } |
7790 | ||
08da4cac KH |
7791 | /* Reset the current counters in the regions. */ |
7792 | ||
e3dc8847 | 7793 | void |
1579bae1 | 7794 | lang_reset_memory_regions (void) |
252b5132 RH |
7795 | { |
7796 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7797 | asection *o; |
e9ee469a | 7798 | lang_output_section_statement_type *os; |
252b5132 | 7799 | |
1579bae1 | 7800 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7801 | { |
252b5132 | 7802 | p->current = p->origin; |
66e28d60 | 7803 | p->last_os = NULL; |
252b5132 | 7804 | } |
b3327aad | 7805 | |
8ce18f9c | 7806 | for (os = (void *) lang_os_list.head; |
e9ee469a AM |
7807 | os != NULL; |
7808 | os = os->next) | |
cde9e0be | 7809 | { |
f38a2680 AM |
7810 | os->processed_vma = false; |
7811 | os->processed_lma = false; | |
cde9e0be | 7812 | } |
e9ee469a | 7813 | |
f13a99db | 7814 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7815 | { |
7816 | /* Save the last size for possible use by bfd_relax_section. */ | |
7817 | o->rawsize = o->size; | |
8772de11 MR |
7818 | if (!(o->flags & SEC_FIXED_SIZE)) |
7819 | o->size = 0; | |
1a23a9e6 | 7820 | } |
252b5132 RH |
7821 | } |
7822 | ||
164e712d | 7823 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7824 | |
7825 | static void | |
1579bae1 | 7826 | gc_section_callback (lang_wild_statement_type *ptr, |
b209b5a6 | 7827 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
1579bae1 AM |
7828 | asection *section, |
7829 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7830 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7831 | { |
164e712d AM |
7832 | /* If the wild pattern was marked KEEP, the member sections |
7833 | should be as well. */ | |
4dec4d4e RH |
7834 | if (ptr->keep_sections) |
7835 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7836 | } |
7837 | ||
252b5132 RH |
7838 | /* Iterate over sections marking them against GC. */ |
7839 | ||
7840 | static void | |
1579bae1 | 7841 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7842 | { |
1579bae1 | 7843 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7844 | { |
7845 | switch (s->header.type) | |
7846 | { | |
7847 | case lang_wild_statement_enum: | |
164e712d | 7848 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7849 | break; |
252b5132 RH |
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 | |
1579bae1 | 7866 | lang_gc_sections (void) |
252b5132 | 7867 | { |
252b5132 | 7868 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7869 | lang_gc_sections_1 (statement_list.head); |
7870 | ||
9ca57817 | 7871 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
15407e7e | 7872 | the special case of .stabstr debug info. (See bfd/stabs.c) |
9ca57817 | 7873 | Twiddle the flag here, to simplify later linker code. */ |
0e1862bb | 7874 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7875 | { |
7876 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7877 | { | |
7878 | asection *sec; | |
0381901e | 7879 | #if BFD_SUPPORTS_PLUGINS |
66be1055 | 7880 | if (f->flags.claimed) |
9e2278f5 AM |
7881 | continue; |
7882 | #endif | |
9ca57817 | 7883 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
15407e7e AM |
7884 | if ((sec->flags & SEC_DEBUGGING) == 0 |
7885 | || strcmp (sec->name, ".stabstr") != 0) | |
9ca57817 AM |
7886 | sec->flags &= ~SEC_EXCLUDE; |
7887 | } | |
7888 | } | |
7889 | ||
57316bff | 7890 | if (link_info.gc_sections) |
f13a99db | 7891 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7892 | } |
7893 | ||
b10a8ae0 L |
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 | |
f13a99db | 7906 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7907 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7908 | && !IGNORE_SECTION (section) | |
7909 | && section->size != 0) | |
7910 | { | |
f38a2680 AM |
7911 | bool *has_relro_section = (bool *) data; |
7912 | *has_relro_section = true; | |
b10a8ae0 L |
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, | |
f38a2680 | 7920 | bool *has_relro_section) |
b10a8ae0 L |
7921 | { |
7922 | if (*has_relro_section) | |
7923 | return; | |
7924 | ||
7925 | for (; s != NULL; s = s->header.next) | |
7926 | { | |
2085ee26 | 7927 | if (s == expld.dataseg.relro_end_stat) |
b10a8ae0 L |
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, | |
2085ee26 | 7939 | has_relro_section); |
b10a8ae0 L |
7940 | break; |
7941 | case lang_output_section_statement_enum: | |
7942 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
2085ee26 | 7943 | has_relro_section); |
b10a8ae0 L |
7944 | break; |
7945 | case lang_group_statement_enum: | |
7946 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
2085ee26 | 7947 | has_relro_section); |
b10a8ae0 L |
7948 | break; |
7949 | default: | |
7950 | break; | |
7951 | } | |
7952 | } | |
7953 | } | |
7954 | ||
7955 | static void | |
7956 | lang_find_relro_sections (void) | |
7957 | { | |
f38a2680 | 7958 | bool has_relro_section = false; |
b10a8ae0 L |
7959 | |
7960 | /* Check all sections in the link script. */ | |
7961 | ||
7962 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
2085ee26 | 7963 | &has_relro_section); |
b10a8ae0 L |
7964 | |
7965 | if (!has_relro_section) | |
f38a2680 | 7966 | link_info.relro = false; |
b10a8ae0 L |
7967 | } |
7968 | ||
fbbc3759 L |
7969 | /* Relax all sections until bfd_relax_section gives up. */ |
7970 | ||
eaeb0a9d | 7971 | void |
f38a2680 | 7972 | lang_relax_sections (bool need_layout) |
fbbc3759 | 7973 | { |
6a91be86 L |
7974 | /* NB: Also enable relaxation to layout sections for DT_RELR. */ |
7975 | if (RELAXATION_ENABLED || link_info.enable_dt_relr) | |
fbbc3759 | 7976 | { |
eaeb0a9d AM |
7977 | /* We may need more than one relaxation pass. */ |
7978 | int i = link_info.relax_pass; | |
fbbc3759 | 7979 | |
eaeb0a9d AM |
7980 | /* The backend can use it to determine the current pass. */ |
7981 | link_info.relax_pass = 0; | |
fbbc3759 | 7982 | |
eaeb0a9d AM |
7983 | while (i--) |
7984 | { | |
7985 | /* Keep relaxing until bfd_relax_section gives up. */ | |
f38a2680 | 7986 | bool relax_again; |
fbbc3759 | 7987 | |
eaeb0a9d AM |
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. */ | |
2f65ac72 | 7999 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
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. */ | |
f38a2680 AM |
8007 | relax_again = false; |
8008 | lang_size_sections (&relax_again, false); | |
eaeb0a9d AM |
8009 | } |
8010 | while (relax_again); | |
8011 | ||
8012 | link_info.relax_pass++; | |
8013 | } | |
f38a2680 | 8014 | need_layout = true; |
eaeb0a9d | 8015 | } |
fbbc3759 | 8016 | |
eaeb0a9d AM |
8017 | if (need_layout) |
8018 | { | |
8019 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 8020 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d | 8021 | lang_reset_memory_regions (); |
f38a2680 | 8022 | lang_size_sections (NULL, true); |
fbbc3759 | 8023 | } |
fbbc3759 L |
8024 | } |
8025 | ||
0381901e | 8026 | #if BFD_SUPPORTS_PLUGINS |
8543fde5 DK |
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 * | |
f38a2680 | 8039 | find_replacements_insert_point (bool *before) |
8543fde5 DK |
8040 | { |
8041 | lang_input_statement_type *claim1, *lastobject; | |
8ce18f9c AM |
8042 | lastobject = (void *) input_file_chain.head; |
8043 | for (claim1 = (void *) file_chain.head; | |
8543fde5 | 8044 | claim1 != NULL; |
36983a93 | 8045 | claim1 = claim1->next) |
8543fde5 | 8046 | { |
66be1055 | 8047 | if (claim1->flags.claimed) |
128bf1fe AM |
8048 | { |
8049 | *before = claim1->flags.claim_archive; | |
8050 | return claim1->flags.claim_archive ? lastobject : claim1; | |
8051 | } | |
8543fde5 | 8052 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 8053 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
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. */ | |
f38a2680 | 8059 | *before = false; |
8543fde5 DK |
8060 | return lastobject; |
8061 | } | |
9e2278f5 | 8062 | |
1fa4ec6a AM |
8063 | /* Find where to insert ADD, an archive element or shared library |
8064 | added during a rescan. */ | |
8065 | ||
36983a93 | 8066 | static lang_input_statement_type ** |
1fa4ec6a AM |
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; | |
36983a93 | 8073 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
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. */ | |
8ce18f9c | 8085 | for (f = (void *) input_file_chain.head; |
1fa4ec6a | 8086 | f != NULL; |
36983a93 | 8087 | f = f->next_real_file) |
1fa4ec6a AM |
8088 | { |
8089 | if (f->the_bfd == add_bfd) | |
8090 | { | |
8091 | before = last_loaded; | |
8092 | if (f->next != NULL) | |
36983a93 | 8093 | return &f->next->next; |
1fa4ec6a AM |
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; | |
36983a93 AM |
8101 | iter = &(*iter)->next) |
8102 | if (!(*iter)->flags.claim_archive | |
8103 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
8104 | break; |
8105 | ||
8106 | return iter; | |
8107 | } | |
8108 | ||
9e2278f5 AM |
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 | } | |
128bf1fe AM |
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 | } | |
0381901e | 8154 | #endif /* BFD_SUPPORTS_PLUGINS */ |
8543fde5 | 8155 | |
6e8376fa NC |
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 | ||
4153b6db NC |
8171 | /* Add NAME to the list of garbage collection entry points. */ |
8172 | ||
8173 | void | |
0aa7f586 | 8174 | lang_add_gc_name (const char *name) |
4153b6db NC |
8175 | { |
8176 | struct bfd_sym_chain *sym; | |
8177 | ||
8178 | if (name == NULL) | |
8179 | return; | |
8180 | ||
988de25b | 8181 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
8182 | |
8183 | sym->next = link_info.gc_sym_list; | |
8184 | sym->name = name; | |
8185 | link_info.gc_sym_list = sym; | |
8186 | } | |
8187 | ||
fbf05aa7 L |
8188 | /* Check relocations. */ |
8189 | ||
8190 | static void | |
8191 | lang_check_relocs (void) | |
8192 | { | |
4f3b23b3 | 8193 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
8194 | { |
8195 | bfd *abfd; | |
8196 | ||
8197 | for (abfd = link_info.input_bfds; | |
8198 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 8199 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 8200 | { |
4f3b23b3 | 8201 | /* No object output, fail return. */ |
f38a2680 | 8202 | config.make_executable = false; |
4f3b23b3 NC |
8203 | /* Note: we do not abort the loop, but rather |
8204 | continue the scan in case there are other | |
8205 | bad relocations to report. */ | |
fbf05aa7 L |
8206 | } |
8207 | } | |
8208 | } | |
8209 | ||
77f5e65e AB |
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 | ||
8ce18f9c | 8218 | for (os = (void *) lang_os_list.head; |
77f5e65e AB |
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 | ||
c63d4862 AM |
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 | ||
50be5d11 MM |
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; | |
50be5d11 MM |
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 | ||
6e8376fa NC |
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 | ||
252b5132 | 8288 | void |
1579bae1 | 8289 | lang_process (void) |
252b5132 | 8290 | { |
6e8376fa NC |
8291 | lang_os_merge_sort_children (); |
8292 | ||
55255dae | 8293 | /* Finalize dynamic list. */ |
40b36307 L |
8294 | if (link_info.dynamic_list) |
8295 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 8296 | |
252b5132 RH |
8297 | current_target = default_target; |
8298 | ||
08da4cac KH |
8299 | /* Open the output file. */ |
8300 | lang_for_each_statement (ldlang_open_output); | |
61826503 | 8301 | init_opb (NULL); |
252b5132 RH |
8302 | |
8303 | ldemul_create_output_section_statements (); | |
8304 | ||
08da4cac | 8305 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
8306 | lang_place_undefineds (); |
8307 | ||
082b7297 | 8308 | if (!bfd_section_already_linked_table_init ()) |
8d97c1a5 | 8309 | fatal (_("%P: can not create hash table: %E\n")); |
9503fd87 | 8310 | |
874ef038 AB |
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. */ | |
f38a2680 | 8317 | lang_do_memory_regions (false); |
874ef038 | 8318 | |
08da4cac | 8319 | /* Create a bfd for each input file. */ |
252b5132 | 8320 | current_target = default_target; |
165f707a | 8321 | lang_statement_iteration++; |
b5c37946 | 8322 | open_input_bfds (statement_list.head, NULL, OPEN_BFD_NORMAL); |
874ef038 AB |
8323 | |
8324 | /* Now that open_input_bfds has processed assignments and provide | |
8325 | statements we can give values to symbolic origin/length now. */ | |
f38a2680 | 8326 | lang_do_memory_regions (true); |
252b5132 | 8327 | |
2605f35c AM |
8328 | ldemul_before_plugin_all_symbols_read (); |
8329 | ||
0381901e | 8330 | #if BFD_SUPPORTS_PLUGINS |
716db898 | 8331 | if (link_info.lto_plugin_active) |
f84854b6 | 8332 | { |
8543fde5 DK |
8333 | lang_statement_list_type added; |
8334 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 8335 | |
f84854b6 L |
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 | |
8543fde5 DK |
8338 | emulation's after_open hook. We create a private list of |
8339 | input statements for this purpose, which we will eventually | |
370dfff4 | 8340 | insert into the global statement list after the first claimed |
8543fde5 DK |
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; | |
f84854b6 | 8346 | if (plugin_call_all_symbols_read ()) |
8d97c1a5 | 8347 | fatal (_("%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 8348 | plugin_error_plugin ()); |
f38a2680 | 8349 | link_info.lto_all_symbols_read = true; |
8543fde5 | 8350 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 8351 | plugin_undefs = link_info.hash->undefs_tail; |
b5c37946 SJ |
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); | |
b02c4f16 AM |
8358 | if (plugin_undefs == link_info.hash->undefs_tail) |
8359 | plugin_undefs = NULL; | |
8543fde5 DK |
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 | |
128bf1fe AM |
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. */ | |
f38a2680 | 8372 | bool before; |
128bf1fe AM |
8373 | lang_statement_union_type **prev; |
8374 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
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. */ | |
9e2278f5 AM |
8379 | ASSERT (plugin_insert != NULL); |
8380 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
8381 | prev = &plugin_insert->header.next; |
8382 | if (before) | |
8383 | { | |
8384 | prev = find_next_input_statement (prev); | |
36983a93 | 8385 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 8386 | { |
a19826f4 | 8387 | /* We didn't find the expected input statement. |
1f1f5b92 | 8388 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
8389 | prev = &plugin_insert->header.next; |
8390 | } | |
8391 | } | |
8392 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
8393 | /* Likewise for the file chains. */ |
8394 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 8395 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
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. */ | |
9e2278f5 | 8399 | if (plugin_insert->filename) |
36983a93 AM |
8400 | lang_list_insert_after (&file_chain, &files, |
8401 | (void *) &plugin_insert->next); | |
8543fde5 DK |
8402 | else |
8403 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
8404 | |
8405 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 8406 | files = file_chain; |
165f707a | 8407 | lang_statement_iteration++; |
b5c37946 | 8408 | open_input_bfds (statement_list.head, NULL, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
8409 | lang_list_remove_tail (&file_chain, &files); |
8410 | while (files.head != NULL) | |
8411 | { | |
36983a93 AM |
8412 | lang_input_statement_type **insert; |
8413 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
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) | |
36983a93 AM |
8421 | for (; *iter != NULL; iter = &(*iter)->next) |
8422 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
8423 | break; |
8424 | temp = *insert; | |
36983a93 AM |
8425 | *insert = &files.head->input_statement; |
8426 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a | 8427 | *iter = temp; |
081609f5 AM |
8428 | if (file_chain.tail == (lang_statement_union_type **) insert) |
8429 | file_chain.tail = (lang_statement_union_type **) iter; | |
1fa4ec6a AM |
8430 | if (my_arch != NULL) |
8431 | { | |
00f93c44 | 8432 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 8433 | if (parent != NULL) |
36983a93 | 8434 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
8435 | ((char *) iter |
8436 | - offsetof (lang_input_statement_type, next)); | |
8437 | } | |
8438 | } | |
8543fde5 | 8439 | } |
f84854b6 | 8440 | } |
9b854f16 | 8441 | else |
0381901e | 8442 | #endif /* BFD_SUPPORTS_PLUGINS */ |
9b854f16 L |
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 | } | |
5d3236ee | 8477 | |
2605f35c AM |
8478 | struct bfd_sym_chain **sym = &link_info.gc_sym_list; |
8479 | while (*sym) | |
8480 | sym = &(*sym)->next; | |
4153b6db | 8481 | |
2605f35c | 8482 | *sym = &entry_symbol; |
41f46ed9 | 8483 | |
e3e942e9 | 8484 | if (entry_symbol.name == NULL) |
41f46ed9 | 8485 | { |
2605f35c | 8486 | *sym = ldlang_undef_chain_list_head; |
41f46ed9 | 8487 | |
2605f35c | 8488 | /* entry_symbol is normally initialised by an ENTRY definition in the |
41f46ed9 SKS |
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 | |
4153b6db | 8491 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
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. */ | |
4153b6db | 8496 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
8497 | } |
8498 | ||
4153b6db NC |
8499 | lang_add_gc_name (link_info.init_function); |
8500 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 8501 | |
252b5132 | 8502 | ldemul_after_open (); |
16e4ecc0 AM |
8503 | if (config.map_file != NULL) |
8504 | lang_print_asneeded (); | |
252b5132 | 8505 | |
1ff6de03 NA |
8506 | ldlang_open_ctf (); |
8507 | ||
082b7297 | 8508 | bfd_section_already_linked_table_free (); |
9503fd87 | 8509 | |
252b5132 RH |
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 | ||
97da0e26 | 8525 | lang_symbol_tweaks (); |
7dba9362 | 8526 | |
2aa9aad9 NC |
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 | ||
08da4cac | 8532 | /* Size up the common data. */ |
252b5132 RH |
8533 | lang_common (); |
8534 | ||
b1eecf6f MM |
8535 | if (0) |
8536 | debug_prefix_tree (); | |
8537 | ||
8538 | resolve_wilds (); | |
8539 | ||
c4621b33 AM |
8540 | /* Remove unreferenced sections if asked to. */ |
8541 | lang_gc_sections (); | |
8542 | ||
5789f845 AM |
8543 | lang_mark_undefineds (); |
8544 | ||
fbf05aa7 L |
8545 | /* Check relocations. */ |
8546 | lang_check_relocs (); | |
8547 | ||
5c3261b0 L |
8548 | ldemul_after_check_relocs (); |
8549 | ||
50be5d11 MM |
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 (); | |
b1eecf6f | 8554 | resolve_wilds (); |
50be5d11 | 8555 | |
af31506c MM |
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. */ | |
bcaa7b3e L |
8559 | update_wild_statements (statement_list.head); |
8560 | ||
252b5132 | 8561 | /* Run through the contours of the script and attach input sections |
08da4cac | 8562 | to the correct output sections. */ |
e759c116 | 8563 | lang_statement_iteration++; |
1579bae1 | 8564 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 8565 | |
776ab89f AM |
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); | |
53d25da6 | 8569 | |
9b538ba7 L |
8570 | ldemul_before_place_orphans (); |
8571 | ||
08da4cac | 8572 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
8573 | lang_place_orphans (); |
8574 | ||
0e1862bb | 8575 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 8576 | { |
57ceae94 AM |
8577 | asection *found; |
8578 | ||
c4fce3ef NC |
8579 | ld_start_phase (PHASE_MERGE); |
8580 | ||
57ceae94 AM |
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. */ | |
32dc3672 | 8585 | if (!bfd_merge_sections (link_info.output_bfd, &link_info)) |
8d97c1a5 | 8586 | fatal (_("%P: bfd_merge_sections failed: %E\n")); |
57ceae94 | 8587 | |
c4fce3ef NC |
8588 | ld_stop_phase (PHASE_MERGE); |
8589 | ||
862120bd | 8590 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 8591 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 8592 | |
1579bae1 | 8593 | if (found != NULL) |
862120bd AM |
8594 | { |
8595 | if (config.text_read_only) | |
8596 | found->flags |= SEC_READONLY; | |
8597 | else | |
8598 | found->flags &= ~SEC_READONLY; | |
8599 | } | |
8600 | } | |
8601 | ||
1ff6de03 NA |
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 | ||
77f5e65e AB |
8610 | /* Copy forward lma regions for output sections in same lma region. */ |
8611 | lang_propagate_lma_regions (); | |
8612 | ||
7dba9362 AM |
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 | ||
862120bd AM |
8624 | /* Do anything special before sizing sections. This is where ELF |
8625 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
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 | ||
b10a8ae0 | 8632 | /* Check relro sections. */ |
0e1862bb | 8633 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
8634 | lang_find_relro_sections (); |
8635 | ||
b3327aad | 8636 | /* Size up the sections. */ |
0aa7f586 | 8637 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 8638 | |
252b5132 | 8639 | /* See if anything special should be done now we know how big |
eaeb0a9d | 8640 | everything is. This is where relaxation is done. */ |
252b5132 RH |
8641 | ldemul_after_allocation (); |
8642 | ||
7dba9362 AM |
8643 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
8644 | lang_finalize_start_stop (); | |
252b5132 | 8645 | |
7f0cfc6e AM |
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. */ | |
2f65ac72 | 8651 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 8652 | |
8ded5a0f AM |
8653 | ldemul_finish (); |
8654 | ||
975f8a9e AM |
8655 | /* Convert absolute symbols to section relative. */ |
8656 | ldexp_finalize_syms (); | |
8657 | ||
252b5132 | 8658 | /* Make sure that the section addresses make sense. */ |
662ef24b | 8659 | if (command_line.check_section_addresses) |
252b5132 | 8660 | lang_check_section_addresses (); |
5f992e62 | 8661 | |
c63d4862 AM |
8662 | if (link_info.non_contiguous_regions |
8663 | && link_info.non_contiguous_regions_warnings) | |
8664 | warn_non_contiguous_discards (); | |
8665 | ||
0a618243 AB |
8666 | /* Check any required symbols are known. */ |
8667 | ldlang_check_require_defined_symbols (); | |
8668 | ||
750877ba | 8669 | lang_end (); |
252b5132 RH |
8670 | } |
8671 | ||
2d5783fa NC |
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 | ||
252b5132 RH |
8694 | /* EXPORTED TO YACC */ |
8695 | ||
8696 | void | |
1579bae1 AM |
8697 | lang_add_wild (struct wildcard_spec *filespec, |
8698 | struct wildcard_list *section_list, | |
f38a2680 | 8699 | bool keep_sections) |
252b5132 | 8700 | { |
b6bf44ba | 8701 | struct wildcard_list *curr, *next; |
d3ce72d0 | 8702 | lang_wild_statement_type *new_stmt; |
af31506c | 8703 | bool any_specs_sorted = false; |
b6bf44ba AM |
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 | { | |
af31506c MM |
8710 | if (curr->spec.sorted != none && curr->spec.sorted != by_none) |
8711 | any_specs_sorted = true; | |
b6bf44ba AM |
8712 | next = curr->next; |
8713 | curr->next = section_list; | |
8714 | } | |
8715 | ||
8716 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 8717 | { |
b6bf44ba AM |
8718 | if (strcmp (filespec->name, "*") == 0) |
8719 | filespec->name = NULL; | |
0aa7f586 | 8720 | else if (!wildcardp (filespec->name)) |
f38a2680 | 8721 | lang_has_input_file = true; |
252b5132 | 8722 | } |
b6bf44ba | 8723 | |
d3ce72d0 NC |
8724 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
8725 | new_stmt->filename = NULL; | |
f38a2680 | 8726 | new_stmt->filenames_sorted = false; |
a0213824 | 8727 | new_stmt->filenames_reversed = false; |
af31506c | 8728 | new_stmt->any_specs_sorted = any_specs_sorted; |
ae17ab41 | 8729 | new_stmt->section_flag_list = NULL; |
8f1732fc | 8730 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 8731 | if (filespec != NULL) |
252b5132 | 8732 | { |
d3ce72d0 | 8733 | new_stmt->filename = filespec->name; |
85921e9a | 8734 | new_stmt->filenames_sorted = (filespec->sorted == by_name || filespec->reversed); |
a0213824 | 8735 | new_stmt->filenames_reversed = filespec->reversed; |
ae17ab41 | 8736 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 8737 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 8738 | } |
d3ce72d0 NC |
8739 | new_stmt->section_list = section_list; |
8740 | new_stmt->keep_sections = keep_sections; | |
8741 | lang_list_init (&new_stmt->children); | |
43ae96e9 | 8742 | lang_list_init (&new_stmt->matching_sections); |
d3ce72d0 | 8743 | analyze_walk_wild_section_handler (new_stmt); |
b1eecf6f MM |
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 | } | |
252b5132 RH |
8751 | } |
8752 | ||
8753 | void | |
ba916c8a MM |
8754 | lang_section_start (const char *name, etree_type *address, |
8755 | const segment_type *segment) | |
252b5132 | 8756 | { |
d1778b88 | 8757 | lang_address_statement_type *ad; |
252b5132 | 8758 | |
d1778b88 | 8759 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
8760 | ad->section_name = name; |
8761 | ad->address = address; | |
ba916c8a | 8762 | ad->segment = segment; |
252b5132 RH |
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 | |
f38a2680 | 8771 | lang_add_entry (const char *name, bool cmdline) |
252b5132 | 8772 | { |
e3e942e9 | 8773 | if (entry_symbol.name == NULL |
252b5132 | 8774 | || cmdline |
0aa7f586 | 8775 | || !entry_from_cmdline) |
252b5132 | 8776 | { |
e3e942e9 | 8777 | entry_symbol.name = name; |
252b5132 RH |
8778 | entry_from_cmdline = cmdline; |
8779 | } | |
8780 | } | |
8781 | ||
a359509e ZW |
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 | ||
252b5132 | 8792 | void |
1579bae1 | 8793 | lang_add_target (const char *name) |
252b5132 | 8794 | { |
d3ce72d0 | 8795 | lang_target_statement_type *new_stmt; |
252b5132 | 8796 | |
d3ce72d0 NC |
8797 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8798 | new_stmt->target = name; | |
252b5132 RH |
8799 | } |
8800 | ||
8801 | void | |
1579bae1 | 8802 | lang_add_map (const char *name) |
252b5132 RH |
8803 | { |
8804 | while (*name) | |
8805 | { | |
8806 | switch (*name) | |
8807 | { | |
08da4cac | 8808 | case 'F': |
f38a2680 | 8809 | map_option_f = true; |
252b5132 RH |
8810 | break; |
8811 | } | |
8812 | name++; | |
8813 | } | |
8814 | } | |
8815 | ||
8816 | void | |
1579bae1 | 8817 | lang_add_fill (fill_type *fill) |
252b5132 | 8818 | { |
d3ce72d0 | 8819 | lang_fill_statement_type *new_stmt; |
252b5132 | 8820 | |
d3ce72d0 NC |
8821 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8822 | new_stmt->fill = fill; | |
252b5132 RH |
8823 | } |
8824 | ||
8825 | void | |
1579bae1 | 8826 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8827 | { |
d3ce72d0 | 8828 | lang_data_statement_type *new_stmt; |
252b5132 | 8829 | |
d3ce72d0 NC |
8830 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8831 | new_stmt->exp = exp; | |
8832 | new_stmt->type = type; | |
252b5132 RH |
8833 | } |
8834 | ||
a4d5aec7 AM |
8835 | void |
8836 | lang_add_string (const char *s) | |
0d79a2a8 | 8837 | { |
a4d5aec7 AM |
8838 | bfd_vma len = strlen (s); |
8839 | bfd_vma i; | |
8840 | bool escape = false; | |
0d79a2a8 | 8841 | |
a4d5aec7 AM |
8842 | /* Add byte expressions until end of string. */ |
8843 | for (i = 0 ; i < len; i++) | |
0d79a2a8 US |
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; | |
90bea493 | 8858 | |
0d79a2a8 US |
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++; | |
a4d5aec7 | 8878 | |
0d79a2a8 US |
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 | } | |
a4d5aec7 | 8896 | |
0d79a2a8 US |
8897 | c = value; |
8898 | } | |
8899 | break; | |
8900 | } | |
a4d5aec7 AM |
8901 | |
8902 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8903 | escape = false; |
8904 | } | |
8905 | else | |
8906 | { | |
8907 | if (c == '\\') | |
a4d5aec7 AM |
8908 | escape = true; |
8909 | else | |
8910 | lang_add_data (BYTE, exp_intop (c)); | |
0d79a2a8 US |
8911 | } |
8912 | } | |
8913 | ||
a4d5aec7 AM |
8914 | /* Remeber to terminate the string. */ |
8915 | lang_add_data (BYTE, exp_intop (0)); | |
0d79a2a8 US |
8916 | } |
8917 | ||
252b5132 RH |
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 | |
1579bae1 AM |
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) | |
252b5132 RH |
8931 | { |
8932 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8933 | |
252b5132 RH |
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; | |
7fabd029 | 8942 | p->output_offset = 0; |
252b5132 RH |
8943 | } |
8944 | ||
8945 | lang_assignment_statement_type * | |
1579bae1 | 8946 | lang_add_assignment (etree_type *exp) |
252b5132 | 8947 | { |
d3ce72d0 | 8948 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8949 | |
d3ce72d0 NC |
8950 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8951 | new_stmt->exp = exp; | |
8952 | return new_stmt; | |
252b5132 RH |
8953 | } |
8954 | ||
8955 | void | |
1579bae1 | 8956 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8957 | { |
bd4d42c1 | 8958 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8959 | } |
8960 | ||
8961 | void | |
1579bae1 | 8962 | lang_startup (const char *name) |
252b5132 | 8963 | { |
1e915804 | 8964 | if (first_file->filename != NULL) |
8d97c1a5 | 8965 | fatal (_("%P: multiple STARTUP files\n")); |
252b5132 RH |
8966 | first_file->filename = name; |
8967 | first_file->local_sym_name = name; | |
f38a2680 | 8968 | first_file->flags.real = true; |
252b5132 RH |
8969 | } |
8970 | ||
8971 | void | |
f38a2680 | 8972 | lang_float (bool maybe) |
252b5132 RH |
8973 | { |
8974 | lang_float_flag = maybe; | |
8975 | } | |
8976 | ||
ee3cc2e2 RS |
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 | ||
a747ee4d NC |
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. | |
ee3cc2e2 RS |
8986 | |
8987 | It is an error to specify both a load region and a load address. */ | |
8988 | ||
8989 | static void | |
6bdafbeb NC |
8990 | lang_get_regions (lang_memory_region_type **region, |
8991 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8992 | const char *memspec, |
8993 | const char *lma_memspec, | |
f38a2680 AM |
8994 | bool have_lma, |
8995 | bool have_vma) | |
ee3cc2e2 | 8996 | { |
f38a2680 | 8997 | *lma_region = lang_memory_region_lookup (lma_memspec, false); |
ee3cc2e2 | 8998 | |
6feb9908 AM |
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. */ | |
6bdafbeb | 9002 | if (lma_memspec != NULL |
0aa7f586 | 9003 | && !have_vma |
6bdafbeb | 9004 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
9005 | *region = *lma_region; |
9006 | else | |
f38a2680 | 9007 | *region = lang_memory_region_lookup (memspec, false); |
ee3cc2e2 | 9008 | |
6bdafbeb | 9009 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 9010 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 9011 | NULL); |
ee3cc2e2 RS |
9012 | } |
9013 | ||
252b5132 | 9014 | void |
6bdafbeb NC |
9015 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
9016 | lang_output_section_phdr_list *phdrs, | |
9017 | const char *lma_memspec) | |
252b5132 | 9018 | { |
6e8376fa NC |
9019 | pop_stat_ptr (); |
9020 | if (in_section_ordering) | |
9021 | return; | |
9022 | ||
ee3cc2e2 RS |
9023 | lang_get_regions (¤t_section->region, |
9024 | ¤t_section->lma_region, | |
9025 | memspec, lma_memspec, | |
6bdafbeb NC |
9026 | current_section->load_base != NULL, |
9027 | current_section->addr_tree != NULL); | |
113e6845 | 9028 | |
252b5132 | 9029 | current_section->fill = fill; |
252b5132 | 9030 | current_section->phdrs = phdrs; |
252b5132 RH |
9031 | } |
9032 | ||
252b5132 RH |
9033 | /* Set the output format type. -oformat overrides scripts. */ |
9034 | ||
9035 | void | |
1579bae1 AM |
9036 | lang_add_output_format (const char *format, |
9037 | const char *big, | |
9038 | const char *little, | |
9039 | int from_script) | |
252b5132 RH |
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 | ||
53d25da6 AM |
9054 | void |
9055 | lang_add_insert (const char *where, int is_before) | |
9056 | { | |
d3ce72d0 | 9057 | lang_insert_statement_type *new_stmt; |
53d25da6 | 9058 | |
d3ce72d0 NC |
9059 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
9060 | new_stmt->where = where; | |
9061 | new_stmt->is_before = is_before; | |
53d25da6 AM |
9062 | saved_script_handle = previous_script_handle; |
9063 | } | |
9064 | ||
252b5132 RH |
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 | |
1579bae1 | 9069 | lang_enter_group (void) |
252b5132 RH |
9070 | { |
9071 | lang_group_statement_type *g; | |
9072 | ||
9073 | g = new_stat (lang_group_statement, stat_ptr); | |
9074 | lang_list_init (&g->children); | |
bde18da4 | 9075 | push_stat_ptr (&g->children); |
252b5132 RH |
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 | |
1579bae1 | 9084 | lang_leave_group (void) |
252b5132 | 9085 | { |
bde18da4 | 9086 | pop_stat_ptr (); |
252b5132 RH |
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 | |
1579bae1 AM |
9093 | lang_new_phdr (const char *name, |
9094 | etree_type *type, | |
f38a2680 AM |
9095 | bool filehdr, |
9096 | bool phdrs, | |
1579bae1 AM |
9097 | etree_type *at, |
9098 | etree_type *flags) | |
252b5132 RH |
9099 | { |
9100 | struct lang_phdr *n, **pp; | |
f38a2680 | 9101 | bool hdrs; |
252b5132 | 9102 | |
988de25b | 9103 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
9104 | n->next = NULL; |
9105 | n->name = name; | |
b5c37946 | 9106 | n->type = exp_get_vma (type, NULL, 0, "program header type"); |
252b5132 RH |
9107 | n->filehdr = filehdr; |
9108 | n->phdrs = phdrs; | |
9109 | n->at = at; | |
9110 | n->flags = flags; | |
1e0061d2 | 9111 | |
5c1a3f0f | 9112 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
9113 | |
9114 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
9115 | if (hdrs |
9116 | && (*pp)->type == 1 | |
9117 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 9118 | { |
c1c8c1ef | 9119 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 9120 | " when prior PT_LOAD headers lack them\n"), NULL); |
f38a2680 | 9121 | hdrs = false; |
5c1a3f0f NS |
9122 | } |
9123 | ||
252b5132 RH |
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 | |
1579bae1 | 9131 | lang_record_phdrs (void) |
252b5132 RH |
9132 | { |
9133 | unsigned int alc; | |
9134 | asection **secs; | |
6bdafbeb | 9135 | lang_output_section_phdr_list *last; |
252b5132 | 9136 | struct lang_phdr *l; |
afd7a018 | 9137 | lang_output_section_statement_type *os; |
252b5132 RH |
9138 | |
9139 | alc = 10; | |
1e9cc1c2 | 9140 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 9141 | last = NULL; |
591a748a | 9142 | |
252b5132 RH |
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; | |
8ce18f9c | 9150 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
9151 | os != NULL; |
9152 | os = os->next) | |
252b5132 | 9153 | { |
6bdafbeb | 9154 | lang_output_section_phdr_list *pl; |
252b5132 | 9155 | |
66c103b7 | 9156 | if (os->constraint < 0) |
0841712e | 9157 | continue; |
252b5132 RH |
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; | |
591a748a | 9168 | |
7512c397 | 9169 | /* Don't add orphans to PT_INTERP header. */ |
7ecf0250 | 9170 | if (l->type == PT_INTERP) |
7512c397 AM |
9171 | continue; |
9172 | ||
e9442572 | 9173 | if (last == NULL) |
591a748a | 9174 | { |
0aa7f586 | 9175 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
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) | |
e9442572 AM |
9187 | { |
9188 | last = tmp_os->phdrs; | |
9189 | break; | |
9190 | } | |
9191 | if (last == NULL) | |
8d97c1a5 | 9192 | fatal (_("%P: no sections assigned to phdrs\n")); |
591a748a | 9193 | } |
e9442572 | 9194 | pl = last; |
252b5132 RH |
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; | |
1e9cc1c2 | 9207 | secs = (asection **) xrealloc (secs, |
4724d37e | 9208 | alc * sizeof (asection *)); |
252b5132 RH |
9209 | } |
9210 | secs[c] = os->bfd_section; | |
9211 | ++c; | |
f38a2680 | 9212 | pl->used = true; |
252b5132 RH |
9213 | } |
9214 | } | |
9215 | } | |
9216 | ||
9217 | if (l->flags == NULL) | |
9218 | flags = 0; | |
9219 | else | |
b5c37946 | 9220 | flags = exp_get_vma (l->flags, NULL, 0, "phdr flags"); |
252b5132 RH |
9221 | |
9222 | if (l->at == NULL) | |
9223 | at = 0; | |
9224 | else | |
b5c37946 | 9225 | at = exp_get_vma (l->at, NULL, 0, "phdr load address"); |
252b5132 | 9226 | |
0aa7f586 AM |
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)) | |
8d97c1a5 | 9230 | fatal (_("%P: bfd_record_phdr failed: %E\n")); |
252b5132 RH |
9231 | } |
9232 | ||
9233 | free (secs); | |
9234 | ||
9235 | /* Make sure all the phdr assignments succeeded. */ | |
8ce18f9c | 9236 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
9237 | os != NULL; |
9238 | os = os->next) | |
252b5132 | 9239 | { |
6bdafbeb | 9240 | lang_output_section_phdr_list *pl; |
252b5132 | 9241 | |
66c103b7 | 9242 | if (os->constraint < 0 |
afd7a018 | 9243 | || os->bfd_section == NULL) |
252b5132 RH |
9244 | continue; |
9245 | ||
afd7a018 | 9246 | for (pl = os->phdrs; |
252b5132 RH |
9247 | pl != NULL; |
9248 | pl = pl->next) | |
0aa7f586 | 9249 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 9250 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 9251 | os->name, pl->name); |
252b5132 RH |
9252 | } |
9253 | } | |
9254 | ||
9255 | /* Record a list of sections which may not be cross referenced. */ | |
9256 | ||
9257 | void | |
6bdafbeb | 9258 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
9259 | { |
9260 | struct lang_nocrossrefs *n; | |
9261 | ||
d4115c2c | 9262 | n = stat_alloc (sizeof *n); |
252b5132 RH |
9263 | n->next = nocrossref_list; |
9264 | n->list = l; | |
f38a2680 | 9265 | n->onlyfirst = false; |
252b5132 RH |
9266 | nocrossref_list = n; |
9267 | ||
9268 | /* Set notice_all so that we get informed about all symbols. */ | |
f38a2680 | 9269 | link_info.notice_all = true; |
252b5132 | 9270 | } |
cdf96953 MF |
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); | |
f38a2680 | 9278 | nocrossref_list->onlyfirst = true; |
cdf96953 | 9279 | } |
252b5132 RH |
9280 | \f |
9281 | /* Overlay handling. We handle overlays with some static variables. */ | |
9282 | ||
9283 | /* The overlay virtual address. */ | |
9284 | static etree_type *overlay_vma; | |
7e7d5768 AM |
9285 | /* And subsection alignment. */ |
9286 | static etree_type *overlay_subalign; | |
252b5132 | 9287 | |
252b5132 RH |
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 | ||
89cdebba | 9293 | struct overlay_list { |
252b5132 RH |
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 | |
7e7d5768 | 9303 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
9304 | { |
9305 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
9306 | ASSERT (overlay_vma == NULL |
9307 | && overlay_subalign == NULL | |
9308 | && overlay_max == NULL); | |
252b5132 RH |
9309 | |
9310 | overlay_vma = vma_expr; | |
7e7d5768 | 9311 | overlay_subalign = subalign; |
252b5132 RH |
9312 | } |
9313 | ||
9314 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
9315 | lang_enter_output_section_statement with the correct VMA. |
9316 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
9317 | |
9318 | void | |
1579bae1 | 9319 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
9320 | { |
9321 | struct overlay_list *n; | |
9322 | etree_type *size; | |
9323 | ||
152d792f | 9324 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
c212f39d | 9325 | 0, 0, overlay_subalign, 0, 0, 0); |
252b5132 | 9326 | |
9f88b410 | 9327 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
9328 | sections on this one. This will work correctly even if `.' is |
9329 | used in the addresses. */ | |
9330 | if (overlay_list == NULL) | |
9f88b410 | 9331 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
9332 | |
9333 | /* Remember the section. */ | |
1e9cc1c2 | 9334 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
9335 | n->os = current_section; |
9336 | n->next = overlay_list; | |
9337 | overlay_list = n; | |
9338 | ||
9339 | size = exp_nameop (SIZEOF, name); | |
9340 | ||
252b5132 RH |
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 | |
1579bae1 | 9352 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 9353 | lang_output_section_phdr_list *phdrs) |
252b5132 | 9354 | { |
86b26b45 | 9355 | const char *name = current_section->name;; |
252b5132 | 9356 | |
a747ee4d NC |
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); | |
252b5132 RH |
9362 | |
9363 | /* Define the magic symbols. */ | |
9364 | ||
86b26b45 AM |
9365 | char *clean = xmalloc (strlen (name) + 1); |
9366 | char *s2 = clean; | |
9367 | for (const char *s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 9368 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
9369 | *s2++ = *s1; |
9370 | *s2 = '\0'; | |
9371 | ||
86b26b45 | 9372 | char *buf = xasprintf ("__load_start_%s", clean); |
34711ca3 AM |
9373 | lang_add_assignment (exp_provide (buf, |
9374 | exp_nameop (LOADADDR, name), | |
f38a2680 | 9375 | false)); |
252b5132 | 9376 | |
86b26b45 | 9377 | buf = xasprintf ("__load_stop_%s", clean); |
34711ca3 AM |
9378 | lang_add_assignment (exp_provide (buf, |
9379 | exp_binop ('+', | |
9380 | exp_nameop (LOADADDR, name), | |
9381 | exp_nameop (SIZEOF, name)), | |
f38a2680 | 9382 | false)); |
252b5132 RH |
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 | |
1579bae1 AM |
9391 | lang_leave_overlay (etree_type *lma_expr, |
9392 | int nocrossrefs, | |
9393 | fill_type *fill, | |
9394 | const char *memspec, | |
6bdafbeb | 9395 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 9396 | const char *lma_memspec) |
252b5132 RH |
9397 | { |
9398 | lang_memory_region_type *region; | |
562d3460 | 9399 | lang_memory_region_type *lma_region; |
252b5132 | 9400 | struct overlay_list *l; |
6bdafbeb | 9401 | lang_nocrossref_type *nocrossref; |
252b5132 | 9402 | |
ee3cc2e2 RS |
9403 | lang_get_regions (®ion, &lma_region, |
9404 | memspec, lma_memspec, | |
f38a2680 | 9405 | lma_expr != NULL, false); |
562d3460 | 9406 | |
252b5132 RH |
9407 | nocrossref = NULL; |
9408 | ||
9f88b410 RS |
9409 | /* After setting the size of the last section, set '.' to end of the |
9410 | overlay region. */ | |
9411 | if (overlay_list != NULL) | |
6d8bf25d AM |
9412 | { |
9413 | overlay_list->os->update_dot = 1; | |
9414 | overlay_list->os->update_dot_tree | |
f38a2680 | 9415 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); |
6d8bf25d | 9416 | } |
9f88b410 | 9417 | |
252b5132 RH |
9418 | l = overlay_list; |
9419 | while (l != NULL) | |
9420 | { | |
9421 | struct overlay_list *next; | |
9422 | ||
1579bae1 | 9423 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 9424 | l->os->fill = fill; |
1545243b | 9425 | |
9f88b410 RS |
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) | |
152d792f AM |
9434 | { |
9435 | l->os->load_base = lma_expr; | |
7b243801 | 9436 | l->os->sectype = first_overlay_section; |
152d792f | 9437 | } |
252b5132 RH |
9438 | if (phdrs != NULL && l->os->phdrs == NULL) |
9439 | l->os->phdrs = phdrs; | |
9440 | ||
9f88b410 | 9441 | if (nocrossrefs) |
252b5132 | 9442 | { |
6bdafbeb | 9443 | lang_nocrossref_type *nc; |
252b5132 | 9444 | |
d4115c2c | 9445 | nc = stat_alloc (sizeof *nc); |
252b5132 RH |
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 | ||
252b5132 | 9459 | overlay_vma = NULL; |
252b5132 RH |
9460 | overlay_list = NULL; |
9461 | overlay_max = NULL; | |
8cb1232a | 9462 | overlay_subalign = NULL; |
252b5132 RH |
9463 | } |
9464 | \f | |
9465 | /* Version handling. This is only useful for ELF. */ | |
9466 | ||
108ba305 JJ |
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). */ | |
252b5132 | 9470 | |
108ba305 JJ |
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) | |
252b5132 | 9475 | { |
93252b1c | 9476 | const char *c_sym; |
108ba305 JJ |
9477 | const char *cxx_sym = sym; |
9478 | const char *java_sym = sym; | |
9479 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
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); | |
252b5132 | 9488 | |
108ba305 | 9489 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 9490 | { |
93252b1c MF |
9491 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
9492 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
9493 | if (!cxx_sym) |
9494 | cxx_sym = sym; | |
252b5132 | 9495 | } |
df816215 | 9496 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 9497 | { |
93252b1c | 9498 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
9499 | if (!java_sym) |
9500 | java_sym = sym; | |
252b5132 RH |
9501 | } |
9502 | ||
ae5a3597 | 9503 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 9504 | { |
108ba305 JJ |
9505 | struct bfd_elf_version_expr e; |
9506 | ||
9507 | switch (prev ? prev->mask : 0) | |
9508 | { | |
967928e9 AM |
9509 | case 0: |
9510 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
9511 | { | |
93252b1c | 9512 | e.pattern = c_sym; |
1e9cc1c2 | 9513 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9514 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 9515 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
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 | { | |
ae5a3597 | 9525 | e.pattern = cxx_sym; |
1e9cc1c2 | 9526 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9527 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9528 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
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 | { | |
ae5a3597 | 9538 | e.pattern = java_sym; |
1e9cc1c2 | 9539 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 9540 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 9541 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
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; | |
108ba305 | 9550 | } |
252b5132 | 9551 | } |
108ba305 JJ |
9552 | |
9553 | /* Finally, try the wildcards. */ | |
ae5a3597 | 9554 | if (prev == NULL || prev->literal) |
108ba305 | 9555 | expr = head->remaining; |
252b5132 | 9556 | else |
108ba305 | 9557 | expr = prev->next; |
86043bbb | 9558 | for (; expr; expr = expr->next) |
252b5132 | 9559 | { |
108ba305 JJ |
9560 | const char *s; |
9561 | ||
86043bbb MM |
9562 | if (!expr->pattern) |
9563 | continue; | |
9564 | ||
108ba305 JJ |
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 | |
93252b1c | 9573 | s = c_sym; |
5e35cbc2 | 9574 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 9575 | break; |
252b5132 RH |
9576 | } |
9577 | ||
967928e9 | 9578 | out_ret: |
93252b1c MF |
9579 | if (c_sym != sym) |
9580 | free ((char *) c_sym); | |
108ba305 JJ |
9581 | if (cxx_sym != sym) |
9582 | free ((char *) cxx_sym); | |
9583 | if (java_sym != sym) | |
9584 | free ((char *) java_sym); | |
9585 | return expr; | |
252b5132 RH |
9586 | } |
9587 | ||
5e35cbc2 | 9588 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 9589 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
9590 | |
9591 | static const char * | |
9592 | realsymbol (const char *pattern) | |
9593 | { | |
9594 | const char *p; | |
f38a2680 | 9595 | bool changed = false, backslash = false; |
1e9cc1c2 | 9596 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
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. */ | |
5e35cbc2 L |
9602 | if (backslash) |
9603 | { | |
9604 | /* Remove the preceding backslash. */ | |
9605 | *(s - 1) = *p; | |
f38a2680 AM |
9606 | backslash = false; |
9607 | changed = true; | |
5e35cbc2 L |
9608 | } |
9609 | else | |
ae5a3597 AM |
9610 | { |
9611 | if (*p == '?' || *p == '*' || *p == '[') | |
9612 | { | |
9613 | free (symbol); | |
9614 | return NULL; | |
9615 | } | |
5e35cbc2 | 9616 | |
ae5a3597 AM |
9617 | *s++ = *p; |
9618 | backslash = *p == '\\'; | |
9619 | } | |
5e35cbc2 L |
9620 | } |
9621 | ||
9622 | if (changed) | |
9623 | { | |
9624 | *s = '\0'; | |
d4115c2c | 9625 | pattern = stat_strdup (symbol); |
5e35cbc2 | 9626 | } |
d4115c2c AM |
9627 | free (symbol); |
9628 | return pattern; | |
5e35cbc2 L |
9629 | } |
9630 | ||
d3ce72d0 | 9631 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
9632 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
9633 | pattern to be matched against symbol names. */ | |
252b5132 RH |
9634 | |
9635 | struct bfd_elf_version_expr * | |
1579bae1 | 9636 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 9637 | const char *new_name, |
86043bbb | 9638 | const char *lang, |
f38a2680 | 9639 | bool literal_p) |
252b5132 RH |
9640 | { |
9641 | struct bfd_elf_version_expr *ret; | |
9642 | ||
d4115c2c | 9643 | ret = stat_alloc (sizeof *ret); |
252b5132 | 9644 | ret->next = orig; |
31941635 L |
9645 | ret->symver = 0; |
9646 | ret->script = 0; | |
f38a2680 | 9647 | ret->literal = true; |
d3ce72d0 | 9648 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
9649 | if (ret->pattern == NULL) |
9650 | { | |
d3ce72d0 | 9651 | ret->pattern = new_name; |
f38a2680 | 9652 | ret->literal = false; |
ae5a3597 | 9653 | } |
252b5132 RH |
9654 | |
9655 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 9656 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 9657 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 9658 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 9659 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 9660 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
9661 | else |
9662 | { | |
9663 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
9664 | lang); | |
108ba305 | 9665 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
9666 | } |
9667 | ||
fac1652d | 9668 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
9669 | } |
9670 | ||
9671 | /* This is called for each set of variable names and match | |
9672 | expressions. */ | |
9673 | ||
9674 | struct bfd_elf_version_tree * | |
1579bae1 AM |
9675 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
9676 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
9677 | { |
9678 | struct bfd_elf_version_tree *ret; | |
9679 | ||
d4115c2c AM |
9680 | ret = stat_alloc (sizeof (*ret)); |
9681 | memset (ret, 0, sizeof (*ret)); | |
108ba305 JJ |
9682 | ret->globals.list = globals; |
9683 | ret->locals.list = locals; | |
9684 | ret->match = lang_vers_match; | |
252b5132 | 9685 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
9686 | return ret; |
9687 | } | |
9688 | ||
9689 | /* This static variable keeps track of version indices. */ | |
9690 | ||
9691 | static int version_index; | |
9692 | ||
108ba305 JJ |
9693 | static hashval_t |
9694 | version_expr_head_hash (const void *p) | |
9695 | { | |
1e9cc1c2 NC |
9696 | const struct bfd_elf_version_expr *e = |
9697 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 9698 | |
ae5a3597 | 9699 | return htab_hash_string (e->pattern); |
108ba305 JJ |
9700 | } |
9701 | ||
9702 | static int | |
9703 | version_expr_head_eq (const void *p1, const void *p2) | |
9704 | { | |
1e9cc1c2 NC |
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; | |
108ba305 | 9709 | |
ae5a3597 | 9710 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
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 | { | |
ae5a3597 | 9722 | if (e->literal) |
108ba305 JJ |
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; | |
ae5a3597 | 9736 | if (!e->literal) |
108ba305 JJ |
9737 | { |
9738 | *remaining_loc = e; | |
9739 | remaining_loc = &e->next; | |
9740 | } | |
9741 | else | |
9742 | { | |
1e9cc1c2 | 9743 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
9744 | |
9745 | if (*loc) | |
9746 | { | |
9747 | struct bfd_elf_version_expr *e1, *last; | |
9748 | ||
1e9cc1c2 | 9749 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
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 | } | |
ae5a3597 | 9761 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 | 9762 | |
d4115c2c | 9763 | if (last != NULL) |
108ba305 JJ |
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 | ||
252b5132 RH |
9784 | /* This is called when we know the name and dependencies of the |
9785 | version. */ | |
9786 | ||
9787 | void | |
1579bae1 AM |
9788 | lang_register_vers_node (const char *name, |
9789 | struct bfd_elf_version_tree *version, | |
9790 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
9791 | { |
9792 | struct bfd_elf_version_tree *t, **pp; | |
9793 | struct bfd_elf_version_expr *e1; | |
9794 | ||
6b9b879a JJ |
9795 | if (name == NULL) |
9796 | name = ""; | |
9797 | ||
fd91d419 L |
9798 | if (link_info.version_info != NULL |
9799 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 9800 | { |
6feb9908 AM |
9801 | einfo (_("%X%P: anonymous version tag cannot be combined" |
9802 | " with other version tags\n")); | |
6b9b879a JJ |
9803 | return; |
9804 | } | |
9805 | ||
252b5132 | 9806 | /* Make sure this node has a unique name. */ |
fd91d419 | 9807 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9808 | if (strcmp (t->name, name) == 0) |
9809 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9810 | ||
108ba305 JJ |
9811 | lang_finalize_version_expr_head (&version->globals); |
9812 | lang_finalize_version_expr_head (&version->locals); | |
9813 | ||
252b5132 RH |
9814 | /* Check the global and local match names, and make sure there |
9815 | aren't any duplicates. */ | |
9816 | ||
108ba305 | 9817 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9818 | { |
fd91d419 | 9819 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9820 | { |
9821 | struct bfd_elf_version_expr *e2; | |
9822 | ||
ae5a3597 | 9823 | if (t->locals.htab && e1->literal) |
108ba305 | 9824 | { |
1e9cc1c2 | 9825 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9826 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 9827 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9828 | { |
9829 | if (e1->mask == e2->mask) | |
6feb9908 | 9830 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 9831 | " in version information\n"), e1->pattern); |
108ba305 JJ |
9832 | e2 = e2->next; |
9833 | } | |
9834 | } | |
ae5a3597 | 9835 | else if (!e1->literal) |
108ba305 | 9836 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
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); | |
252b5132 RH |
9841 | } |
9842 | } | |
9843 | ||
108ba305 | 9844 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9845 | { |
fd91d419 | 9846 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9847 | { |
9848 | struct bfd_elf_version_expr *e2; | |
9849 | ||
ae5a3597 | 9850 | if (t->globals.htab && e1->literal) |
108ba305 | 9851 | { |
1e9cc1c2 | 9852 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9853 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9854 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9855 | { |
9856 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9857 | einfo (_("%X%P: duplicate expression `%s'" |
9858 | " in version information\n"), | |
ae5a3597 | 9859 | e1->pattern); |
108ba305 JJ |
9860 | e2 = e2->next; |
9861 | } | |
9862 | } | |
ae5a3597 | 9863 | else if (!e1->literal) |
108ba305 | 9864 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
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); | |
252b5132 RH |
9869 | } |
9870 | } | |
9871 | ||
9872 | version->deps = deps; | |
9873 | version->name = name; | |
6b9b879a JJ |
9874 | if (name[0] != '\0') |
9875 | { | |
9876 | ++version_index; | |
9877 | version->vernum = version_index; | |
9878 | } | |
9879 | else | |
9880 | version->vernum = 0; | |
252b5132 | 9881 | |
fd91d419 | 9882 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9883 | ; |
9884 | *pp = version; | |
9885 | } | |
9886 | ||
9887 | /* This is called when we see a version dependency. */ | |
9888 | ||
9889 | struct bfd_elf_version_deps * | |
1579bae1 | 9890 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9891 | { |
9892 | struct bfd_elf_version_deps *ret; | |
9893 | struct bfd_elf_version_tree *t; | |
9894 | ||
d4115c2c | 9895 | ret = stat_alloc (sizeof *ret); |
252b5132 RH |
9896 | ret->next = list; |
9897 | ||
fd91d419 | 9898 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
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 | ||
1e0061d2 | 9909 | ret->version_needed = NULL; |
252b5132 RH |
9910 | return ret; |
9911 | } | |
9912 | ||
9913 | static void | |
1579bae1 | 9914 | lang_do_version_exports_section (void) |
252b5132 RH |
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) | |
b7a26f91 | 9925 | continue; |
252b5132 | 9926 | |
eea6121a | 9927 | len = sec->size; |
d4115c2c | 9928 | contents = stat_alloc (len); |
252b5132 | 9929 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9930 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9931 | |
9932 | p = contents; | |
89cdebba | 9933 | while (p < contents + len) |
252b5132 | 9934 | { |
f38a2680 | 9935 | greg = lang_new_vers_pattern (greg, p, NULL, false); |
252b5132 RH |
9936 | p = strchr (p, '\0') + 1; |
9937 | } | |
9938 | ||
252b5132 | 9939 | /* Do not include this section in the link. */ |
a14a5de3 | 9940 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9941 | } |
9942 | ||
f38a2680 | 9943 | lreg = lang_new_vers_pattern (NULL, "*", NULL, false); |
252b5132 RH |
9944 | lang_register_vers_node (command_line.version_exports_section, |
9945 | lang_new_vers_node (greg, lreg), NULL); | |
9946 | } | |
577a0623 | 9947 | |
874ef038 AB |
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. */ | |
cc9ad334 SKS |
9955 | |
9956 | static void | |
f38a2680 | 9957 | lang_do_memory_regions (bool update_regions_p) |
cc9ad334 SKS |
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) | |
0aa7f586 | 9964 | { |
b5c37946 | 9965 | exp_fold_tree_no_dot (r->origin_exp, NULL); |
90bea493 AM |
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 | } | |
0aa7f586 | 9977 | } |
cc9ad334 | 9978 | if (r->length_exp) |
0aa7f586 | 9979 | { |
b5c37946 | 9980 | exp_fold_tree_no_dot (r->length_exp, NULL); |
90bea493 AM |
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 | } | |
cc9ad334 SKS |
9990 | } |
9991 | } | |
9992 | ||
577a0623 | 9993 | void |
1579bae1 | 9994 | lang_add_unique (const char *name) |
577a0623 AM |
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 | ||
d4115c2c AM |
10002 | ent = stat_alloc (sizeof *ent); |
10003 | ent->name = stat_strdup (name); | |
577a0623 AM |
10004 | ent->next = unique_section_list; |
10005 | unique_section_list = ent; | |
10006 | } | |
55255dae L |
10007 | |
10008 | /* Append the list of dynamic symbols to the existing one. */ | |
10009 | ||
10010 | void | |
37a141bf FS |
10011 | lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, |
10012 | struct bfd_elf_version_expr *dynamic) | |
55255dae | 10013 | { |
37a141bf | 10014 | if (*list_p) |
55255dae | 10015 | { |
07806542 JJ |
10016 | struct bfd_elf_version_expr *tail; |
10017 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
10018 | ; | |
37a141bf FS |
10019 | tail->next = (*list_p)->head.list; |
10020 | (*list_p)->head.list = dynamic; | |
55255dae L |
10021 | } |
10022 | else | |
10023 | { | |
10024 | struct bfd_elf_dynamic_list *d; | |
10025 | ||
d4115c2c AM |
10026 | d = stat_alloc (sizeof (*d)); |
10027 | memset (d, 0, sizeof (*d)); | |
55255dae L |
10028 | d->head.list = dynamic; |
10029 | d->match = lang_vers_match; | |
37a141bf | 10030 | *list_p = d; |
55255dae L |
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 | { | |
0aa7f586 | 10040 | const char *symbols[] = |
55255dae L |
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++", | |
f38a2680 | 10050 | false); |
55255dae | 10051 | |
37a141bf | 10052 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
55255dae | 10053 | } |
40b36307 L |
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 | { | |
0aa7f586 | 10061 | const char *symbols[] = |
40b36307 L |
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++", | |
f38a2680 | 10071 | false); |
40b36307 | 10072 | |
37a141bf | 10073 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
40b36307 | 10074 | } |
01554a74 AM |
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) | |
f38a2680 | 10097 | config.sane_expr = true; |
01554a74 AM |
10098 | else |
10099 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
10100 | *q = sep; | |
10101 | p = q; | |
10102 | } | |
10103 | } | |
3604cb1f TG |
10104 | |
10105 | /* Pretty print memory amount. */ | |
10106 | ||
10107 | static void | |
b8281767 | 10108 | lang_print_memory_size (uint64_t sz) |
3604cb1f | 10109 | { |
8ea1e363 TS |
10110 | if (sz == 0) |
10111 | printf (" %10" PRIu64 " B", sz); | |
10112 | else if ((sz & 0x3fffffff) == 0) | |
b8281767 | 10113 | printf ("%10" PRIu64 " GB", sz >> 30); |
3604cb1f | 10114 | else if ((sz & 0xfffff) == 0) |
b8281767 | 10115 | printf ("%10" PRIu64 " MB", sz >> 20); |
3604cb1f | 10116 | else if ((sz & 0x3ff) == 0) |
b8281767 | 10117 | printf ("%10" PRIu64 " KB", sz >> 10); |
3604cb1f | 10118 | else |
b8281767 | 10119 | printf (" %10" PRIu64 " B", sz); |
3604cb1f TG |
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; | |
3604cb1f TG |
10133 | |
10134 | printf ("%16s: ",r->name_list.name); | |
10135 | lang_print_memory_size (used_length); | |
b8281767 | 10136 | lang_print_memory_size (r->length); |
3604cb1f | 10137 | |
1769380a AM |
10138 | if (r->length != 0) |
10139 | { | |
10140 | double percent = used_length * 100.0 / r->length; | |
2410edcd | 10141 | printf (" %6.2f%%", percent); |
1769380a | 10142 | } |
2410edcd | 10143 | printf ("\n"); |
3604cb1f TG |
10144 | } |
10145 | } | |
9b854f16 L |
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; | |
8d97c1a5 | 10508 | fatal (_("%P: setup_section: %s: %s\n"), err, name); |
9b854f16 L |
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: | |
8d97c1a5 | 10603 | fatal (_("%P: copy_section: %s: %s\n"), err, isection->name); |
9b854f16 L |
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 | ||
e2b02acd | 10795 | osympp = xmalloc ((symcount + 1) * sizeof (asymbol *)); |
9b854f16 L |
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); | |
8d97c1a5 | 10840 | fatal (_("%P: failed to finish output with object-only section\n")); |
9b854f16 L |
10841 | } |
10842 | ||
10843 | /* Must be freed after bfd_close (). */ | |
10844 | free (isympp); | |
0e4207c5 | 10845 | free (osympp); |
9b854f16 L |
10846 | |
10847 | if (rename (ofilename, output_filename)) | |
10848 | { | |
10849 | unlink (ofilename); | |
8d97c1a5 | 10850 | fatal (_("%P: failed to rename output with object-only section\n")); |
9b854f16 L |
10851 | } |
10852 | ||
10853 | free (ofilename); | |
10854 | return; | |
10855 | ||
10856 | loser: | |
0e4207c5 AM |
10857 | free (isympp); |
10858 | free (osympp); | |
9b854f16 L |
10859 | if (obfd) |
10860 | bfd_close (obfd); | |
10861 | if (ofilename) | |
0e4207c5 AM |
10862 | { |
10863 | unlink (ofilename); | |
10864 | free (ofilename); | |
10865 | } | |
8d97c1a5 | 10866 | fatal (_("%P: failed to add object-only section: %s\n"), err); |
9b854f16 L |
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 | ||
9b854f16 L |
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 ()) | |
8d97c1a5 | 10899 | fatal (_("%P: Failed to create hash table\n")); |
9b854f16 L |
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)) | |
8d97c1a5 AM |
10963 | fatal (_("%P:%s: final close failed on object-only output: %E\n"), |
10964 | output_filename); | |
9b854f16 L |
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); | |
8d97c1a5 | 10973 | fatal (_("%P:%s: cannot open object-only output: %E\n"), |
9b854f16 L |
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); | |
8d97c1a5 | 10981 | fatal (_("%P:%s: cannot stat object-only output: %E\n"), |
9b854f16 L |
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); | |
8d97c1a5 | 10996 | fatal (_("%P:%s: read failed on object-only output: %E\n"), |
9b854f16 L |
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) | |
8d97c1a5 | 11023 | fatal (_("%P: cannot extract object-only section from %B: %E\n"), abfd); |
9b854f16 L |
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 | ||
d7a3c9e6 L |
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 | |
8d97c1a5 | 11057 | fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
d7a3c9e6 L |
11058 | } |
11059 | ||
9b854f16 L |
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); | |
d7a3c9e6 | 11081 | cmdline_load_object_only_section (filename); |
9b854f16 L |
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 | } |