]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
a2c58332 | 2 | Copyright (C) 1991-2022 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 | |
31 | #include "ld.h" | |
32 | #include "ldmain.h" | |
252b5132 RH |
33 | #include "ldexp.h" |
34 | #include "ldlang.h" | |
df2a7313 | 35 | #include <ldgram.h> |
252b5132 RH |
36 | #include "ldlex.h" |
37 | #include "ldmisc.h" | |
38 | #include "ldctor.h" | |
39 | #include "ldfile.h" | |
b71e2778 | 40 | #include "ldemul.h" |
252b5132 RH |
41 | #include "fnmatch.h" |
42 | #include "demangle.h" | |
108ba305 | 43 | #include "hashtab.h" |
0fef4b98 | 44 | #include "elf-bfd.h" |
0381901e | 45 | #if BFD_SUPPORTS_PLUGINS |
5d3236ee | 46 | #include "plugin.h" |
0381901e | 47 | #endif /* BFD_SUPPORTS_PLUGINS */ |
252b5132 | 48 | |
7abb6dea | 49 | #ifndef offsetof |
cf888e70 | 50 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
51 | #endif |
52 | ||
fc90c280 AM |
53 | /* Convert between addresses in bytes and sizes in octets. |
54 | For currently supported targets, octets_per_byte is always a power | |
55 | of two, so we can use shifts. */ | |
56 | #define TO_ADDR(X) ((X) >> opb_shift) | |
57 | #define TO_SIZE(X) ((X) << opb_shift) | |
58 | ||
59 | /* Local variables. */ | |
252b5132 | 60 | static struct obstack stat_obstack; |
35835446 | 61 | static struct obstack map_obstack; |
b1eecf6f | 62 | static struct obstack pt_obstack; |
252b5132 RH |
63 | |
64 | #define obstack_chunk_alloc xmalloc | |
65 | #define obstack_chunk_free free | |
8be573a7 | 66 | static const char *entry_symbol_default = "start"; |
f38a2680 | 67 | static bool map_head_is_link_order = false; |
252b5132 | 68 | static lang_output_section_statement_type *default_common_section; |
f38a2680 | 69 | static bool map_option_f; |
252b5132 RH |
70 | static bfd_vma print_dot; |
71 | static lang_input_statement_type *first_file; | |
5f992e62 | 72 | static const char *current_target; |
bde18da4 AM |
73 | static lang_statement_list_type *stat_save[10]; |
74 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; | |
c4b78195 | 75 | static struct unique_sections *unique_section_list; |
16e4ecc0 | 76 | static struct asneeded_minfo *asneeded_list_head; |
fc90c280 | 77 | static unsigned int opb_shift = 0; |
252b5132 | 78 | |
cf888e70 | 79 | /* Forward declarations. */ |
a431bc2e | 80 | static void exp_init_os (etree_type *); |
1579bae1 | 81 | static lang_input_statement_type *lookup_name (const char *); |
1579bae1 | 82 | static void insert_undefined (const char *); |
f38a2680 | 83 | static bool sort_def_symbol (struct bfd_link_hash_entry *, void *); |
b1eecf6f MM |
84 | static lang_statement_union_type *new_statement (enum statement_enum type, |
85 | size_t size, | |
86 | lang_statement_list_type *list); | |
1579bae1 AM |
87 | static void print_statement (lang_statement_union_type *, |
88 | lang_output_section_statement_type *); | |
89 | static void print_statement_list (lang_statement_union_type *, | |
90 | lang_output_section_statement_type *); | |
91 | static void print_statements (void); | |
f38a2680 AM |
92 | static void print_input_section (asection *, bool); |
93 | static bool lang_one_common (struct bfd_link_hash_entry *, void *); | |
1579bae1 AM |
94 | static void lang_record_phdrs (void); |
95 | static void lang_do_version_exports_section (void); | |
55255dae L |
96 | static void lang_finalize_version_expr_head |
97 | (struct bfd_elf_version_expr_head *); | |
f38a2680 | 98 | static void lang_do_memory_regions (bool); |
1579bae1 | 99 | |
cf888e70 | 100 | /* Exported variables. */ |
8be573a7 | 101 | const char *output_target; |
252b5132 | 102 | lang_output_section_statement_type *abs_output_section; |
b209b5a6 AM |
103 | /* Header for list of statements corresponding to any files involved in the |
104 | link, either specified from the command-line or added implicitely (eg. | |
105 | archive member used to resolved undefined symbol, wildcard statement from | |
106 | linker script, etc.). Next pointer is in next field of a | |
107 | lang_statement_header_type (reached via header field in a | |
108 | lang_statement_union). */ | |
109 | lang_statement_list_type statement_list; | |
5c1e6d53 | 110 | lang_statement_list_type lang_os_list; |
252b5132 | 111 | lang_statement_list_type *stat_ptr = &statement_list; |
e368bf56 TP |
112 | /* Header for list of statements corresponding to files used in the final |
113 | executable. This can be either object file specified on the command-line | |
114 | or library member resolving an undefined reference. Next pointer is in next | |
115 | field of a lang_input_statement_type (reached via input_statement field in a | |
116 | lang_statement_union). */ | |
87f2a346 | 117 | lang_statement_list_type file_chain = { NULL, NULL }; |
e368bf56 TP |
118 | /* Header for list of statements corresponding to files specified on the |
119 | command-line for linking. It thus contains real object files and archive | |
120 | but not archive members. Next pointer is in next_real_file field of a | |
121 | lang_input_statement_type statement (reached via input_statement field in a | |
122 | lang_statement_union). */ | |
dc27aea4 | 123 | lang_statement_list_type input_file_chain; |
16171946 | 124 | static const char *current_input_file; |
37a141bf | 125 | struct bfd_elf_dynamic_list **current_dynamic_list_p; |
e3e942e9 | 126 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 127 | const char *entry_section = ".text"; |
66be1055 | 128 | struct lang_input_statement_flags input_flags; |
f38a2680 AM |
129 | bool entry_from_cmdline; |
130 | bool lang_has_input_file = false; | |
131 | bool had_output_filename = false; | |
132 | bool lang_float_flag = false; | |
133 | bool delete_output_file_on_failure = false; | |
f5ff60a6 | 134 | struct lang_phdr *lang_phdr_list; |
252b5132 | 135 | struct lang_nocrossrefs *nocrossref_list; |
16e4ecc0 | 136 | struct asneeded_minfo **asneeded_list_tail; |
094e34f2 | 137 | #ifdef ENABLE_LIBCTF |
139633c3 | 138 | static ctf_dict_t *ctf_output; |
094e34f2 | 139 | #endif |
e9ee469a | 140 | |
baf09cba AM |
141 | /* Functions that traverse the linker script and might evaluate |
142 | DEFINED() need to increment this at the start of the traversal. */ | |
420e579c | 143 | int lang_statement_iteration = 0; |
252b5132 | 144 | |
baf09cba AM |
145 | /* Count times through one_lang_size_sections_pass after mark phase. */ |
146 | static int lang_sizing_iteration = 0; | |
147 | ||
61f5d054 L |
148 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
149 | Although backslashes are treated specially if a pattern contains | |
150 | wildcards, we do not consider the mere presence of a backslash to | |
151 | be enough to cause the pattern to be treated as a wildcard. | |
152 | That lets us handle DOS filenames more naturally. */ | |
153 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
154 | ||
d1778b88 | 155 | #define new_stat(x, y) \ |
1579bae1 | 156 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 157 | |
d1778b88 AM |
158 | #define outside_section_address(q) \ |
159 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 160 | |
d1778b88 AM |
161 | #define outside_symbol_address(q) \ |
162 | ((q)->value + outside_section_address (q->section)) | |
252b5132 | 163 | |
1ff6de03 NA |
164 | /* CTF sections smaller than this are not compressed: compression of |
165 | dictionaries this small doesn't gain much, and this lets consumers mmap the | |
166 | sections directly out of the ELF file and use them with no decompression | |
167 | overhead if they want to. */ | |
168 | #define CTF_COMPRESSION_THRESHOLD 4096 | |
169 | ||
1579bae1 AM |
170 | void * |
171 | stat_alloc (size_t size) | |
252b5132 RH |
172 | { |
173 | return obstack_alloc (&stat_obstack, size); | |
174 | } | |
175 | ||
049522ca MM |
176 | /* Code for handling simple wildcards without going through fnmatch, |
177 | which can be expensive because of charset translations etc. */ | |
178 | ||
179 | /* A simple wild is a literal string followed by a single '*', | |
180 | where the literal part is at least 4 characters long. */ | |
181 | ||
182 | static bool | |
183 | is_simple_wild (const char *name) | |
184 | { | |
185 | size_t len = strcspn (name, "*?["); | |
186 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; | |
187 | } | |
188 | ||
189 | static bool | |
190 | match_simple_wild (const char *pattern, const char *name) | |
191 | { | |
192 | /* The first four characters of the pattern are guaranteed valid | |
193 | non-wildcard characters. So we can go faster. */ | |
194 | if (pattern[0] != name[0] || pattern[1] != name[1] | |
195 | || pattern[2] != name[2] || pattern[3] != name[3]) | |
196 | return false; | |
197 | ||
198 | pattern += 4; | |
199 | name += 4; | |
200 | while (*pattern != '*') | |
201 | if (*name++ != *pattern++) | |
202 | return false; | |
203 | ||
204 | return true; | |
205 | } | |
206 | ||
97407faf AM |
207 | static int |
208 | name_match (const char *pattern, const char *name) | |
209 | { | |
049522ca MM |
210 | if (is_simple_wild (pattern)) |
211 | return !match_simple_wild (pattern, name); | |
97407faf AM |
212 | if (wildcardp (pattern)) |
213 | return fnmatch (pattern, name, 0); | |
214 | return strcmp (pattern, name); | |
215 | } | |
216 | ||
049522ca MM |
217 | /* Given an analyzed wildcard_spec SPEC, match it against NAME, |
218 | returns zero on a match, non-zero if there's no match. */ | |
219 | ||
220 | static int | |
221 | spec_match (const struct wildcard_spec *spec, const char *name) | |
222 | { | |
223 | size_t nl = spec->namelen; | |
224 | size_t pl = spec->prefixlen; | |
225 | size_t sl = spec->suffixlen; | |
226 | int r; | |
227 | if (pl && (r = memcmp (spec->name, name, pl))) | |
228 | return r; | |
229 | if (sl) | |
230 | { | |
231 | size_t inputlen = strlen (name); | |
232 | if (inputlen < sl) | |
233 | return 1; | |
234 | r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl); | |
235 | if (r) | |
236 | return r; | |
237 | } | |
238 | if (nl == pl + sl + 1 && spec->name[pl] == '*') | |
239 | return 0; | |
240 | else if (nl > pl) | |
241 | return fnmatch (spec->name + pl, name + pl, 0); | |
242 | return name[nl]; | |
243 | } | |
244 | ||
16171946 FS |
245 | static char * |
246 | ldirname (const char *name) | |
247 | { | |
248 | const char *base = lbasename (name); | |
249 | char *dirname; | |
250 | ||
251 | while (base > name && IS_DIR_SEPARATOR (base[-1])) | |
252 | --base; | |
253 | if (base == name) | |
254 | return strdup ("."); | |
255 | dirname = strdup (name); | |
256 | dirname[base - name] = '\0'; | |
257 | return dirname; | |
258 | } | |
259 | ||
97407faf AM |
260 | /* If PATTERN is of the form archive:file, return a pointer to the |
261 | separator. If not, return NULL. */ | |
262 | ||
263 | static char * | |
264 | archive_path (const char *pattern) | |
265 | { | |
266 | char *p = NULL; | |
267 | ||
268 | if (link_info.path_separator == 0) | |
269 | return p; | |
270 | ||
271 | p = strchr (pattern, link_info.path_separator); | |
272 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
273 | if (p == NULL || link_info.path_separator != ':') | |
274 | return p; | |
275 | ||
276 | /* Assume a match on the second char is part of drive specifier, | |
277 | as in "c:\silly.dos". */ | |
967928e9 | 278 | if (p == pattern + 1 && ISALPHA (*pattern)) |
97407faf AM |
279 | p = strchr (p + 1, link_info.path_separator); |
280 | #endif | |
281 | return p; | |
282 | } | |
283 | ||
967928e9 AM |
284 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
285 | return whether F matches FILE_SPEC. */ | |
286 | ||
f38a2680 | 287 | static bool |
967928e9 AM |
288 | input_statement_is_archive_path (const char *file_spec, char *sep, |
289 | lang_input_statement_type *f) | |
290 | { | |
f38a2680 | 291 | bool match = false; |
967928e9 AM |
292 | |
293 | if ((*(sep + 1) == 0 | |
294 | || name_match (sep + 1, f->filename) == 0) | |
295 | && ((sep != file_spec) | |
296 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) | |
297 | { | |
f38a2680 | 298 | match = true; |
967928e9 AM |
299 | |
300 | if (sep != file_spec) | |
301 | { | |
607b4833 | 302 | const char *aname = bfd_get_filename (f->the_bfd->my_archive); |
967928e9 AM |
303 | *sep = 0; |
304 | match = name_match (file_spec, aname) == 0; | |
305 | *sep = link_info.path_separator; | |
306 | } | |
307 | } | |
308 | return match; | |
309 | } | |
310 | ||
f38a2680 | 311 | static bool |
d0bf826b AM |
312 | unique_section_p (const asection *sec, |
313 | const lang_output_section_statement_type *os) | |
577a0623 AM |
314 | { |
315 | struct unique_sections *unam; | |
d0d6a25b | 316 | const char *secnam; |
577a0623 | 317 | |
7bdf4127 | 318 | if (!link_info.resolve_section_groups |
d0d6a25b AM |
319 | && sec->owner != NULL |
320 | && bfd_is_group_section (sec->owner, sec)) | |
d0bf826b AM |
321 | return !(os != NULL |
322 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); | |
d0d6a25b AM |
323 | |
324 | secnam = sec->name; | |
577a0623 | 325 | for (unam = unique_section_list; unam; unam = unam->next) |
97407faf | 326 | if (name_match (unam->name, secnam) == 0) |
f38a2680 | 327 | return true; |
577a0623 | 328 | |
f38a2680 | 329 | return false; |
577a0623 AM |
330 | } |
331 | ||
08da4cac | 332 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e | 333 | |
8f1732fc AB |
334 | /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return |
335 | false. */ | |
72223188 | 336 | |
f38a2680 | 337 | static bool |
8f1732fc | 338 | walk_wild_file_in_exclude_list (struct name_list *exclude_list, |
6c19b93b | 339 | lang_input_statement_type *file) |
4dec4d4e | 340 | { |
72223188 JJ |
341 | struct name_list *list_tmp; |
342 | ||
8f1732fc | 343 | for (list_tmp = exclude_list; |
72223188 JJ |
344 | list_tmp; |
345 | list_tmp = list_tmp->next) | |
346 | { | |
967928e9 | 347 | char *p = archive_path (list_tmp->name); |
b6bf44ba | 348 | |
967928e9 | 349 | if (p != NULL) |
72223188 | 350 | { |
967928e9 | 351 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
f38a2680 | 352 | return true; |
72223188 JJ |
353 | } |
354 | ||
967928e9 | 355 | else if (name_match (list_tmp->name, file->filename) == 0) |
f38a2680 | 356 | return true; |
967928e9 AM |
357 | |
358 | /* FIXME: Perhaps remove the following at some stage? Matching | |
359 | unadorned archives like this was never documented and has | |
360 | been superceded by the archive:path syntax. */ | |
361 | else if (file->the_bfd != NULL | |
362 | && file->the_bfd->my_archive != NULL | |
363 | && name_match (list_tmp->name, | |
607b4833 | 364 | bfd_get_filename (file->the_bfd->my_archive)) == 0) |
f38a2680 | 365 | return true; |
72223188 JJ |
366 | } |
367 | ||
f38a2680 | 368 | return false; |
8f1732fc AB |
369 | } |
370 | ||
b1eecf6f MM |
371 | /* Add SECTION (from input FILE) to the list of matching sections |
372 | within PTR (the matching wildcard is SEC). */ | |
373 | ||
374 | static void | |
375 | add_matching_section (lang_wild_statement_type *ptr, | |
376 | struct wildcard_list *sec, | |
377 | asection *section, | |
378 | lang_input_statement_type *file) | |
379 | { | |
380 | lang_input_matcher_type *new_section; | |
381 | /* Add a section reference to the list. */ | |
382 | new_section = new_stat (lang_input_matcher, &ptr->matching_sections); | |
383 | new_section->section = section; | |
384 | new_section->pattern = sec; | |
385 | new_section->input_stmt = file; | |
386 | } | |
387 | ||
388 | /* Process section S (from input file FILE) in relation to wildcard | |
389 | statement PTR. We already know that a prefix of the name of S matches | |
390 | some wildcard in PTR's wildcard list. Here we check if the filename | |
391 | matches as well (if it's specified) and if any of the wildcards in fact | |
392 | does match. */ | |
393 | ||
394 | static void | |
395 | walk_wild_section_match (lang_wild_statement_type *ptr, | |
396 | lang_input_statement_type *file, | |
397 | asection *s) | |
398 | { | |
399 | struct wildcard_list *sec; | |
400 | const char *file_spec = ptr->filename; | |
401 | char *p; | |
402 | ||
403 | /* Check if filenames match. */ | |
404 | if (file_spec == NULL) | |
405 | ; | |
406 | else if ((p = archive_path (file_spec)) != NULL) | |
407 | { | |
408 | if (!input_statement_is_archive_path (file_spec, p, file)) | |
409 | return; | |
410 | } | |
411 | else if (wildcardp (file_spec)) | |
412 | { | |
413 | if (fnmatch (file_spec, file->filename, 0) != 0) | |
414 | return; | |
415 | } | |
416 | else | |
417 | { | |
418 | lang_input_statement_type *f; | |
419 | /* Perform the iteration over a single file. */ | |
420 | f = lookup_name (file_spec); | |
421 | if (f != file) | |
422 | return; | |
423 | } | |
424 | ||
425 | /* Check section name against each wildcard spec. If there's no | |
426 | wildcard all sections match. */ | |
427 | sec = ptr->section_list; | |
428 | if (sec == NULL) | |
429 | add_matching_section (ptr, sec, s, file); | |
430 | else | |
431 | { | |
432 | const char *sname = bfd_section_name (s); | |
433 | for (; sec != NULL; sec = sec->next) | |
434 | { | |
435 | if (sec->spec.name != NULL | |
436 | && spec_match (&sec->spec, sname) != 0) | |
437 | continue; | |
438 | ||
439 | /* Don't process sections from files which were excluded. */ | |
440 | if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, | |
441 | file)) | |
442 | add_matching_section (ptr, sec, s, file); | |
443 | } | |
444 | } | |
445 | } | |
446 | ||
02ecc8e9 L |
447 | /* Return the numerical value of the init_priority attribute from |
448 | section name NAME. */ | |
449 | ||
9a24a276 AM |
450 | static int |
451 | get_init_priority (const asection *sec) | |
02ecc8e9 | 452 | { |
9a24a276 AM |
453 | const char *name = bfd_section_name (sec); |
454 | const char *dot; | |
02ecc8e9 L |
455 | |
456 | /* GCC uses the following section names for the init_priority | |
9a24a276 | 457 | attribute with numerical values 101 to 65535 inclusive. A |
02ecc8e9 L |
458 | lower value means a higher priority. |
459 | ||
9a24a276 | 460 | 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the |
02ecc8e9 L |
461 | decimal numerical value of the init_priority attribute. |
462 | The order of execution in .init_array is forward and | |
463 | .fini_array is backward. | |
9a24a276 | 464 | 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the |
02ecc8e9 L |
465 | decimal numerical value of the init_priority attribute. |
466 | The order of execution in .ctors is backward and .dtors | |
467 | is forward. | |
9a24a276 AM |
468 | |
469 | .init_array.NNNNN sections would normally be placed in an output | |
470 | .init_array section, .fini_array.NNNNN in .fini_array, | |
471 | .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means | |
472 | we should sort by increasing number (and could just use | |
473 | SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are | |
474 | being placed in .init_array (which may also contain | |
475 | .init_array.NNNNN sections) or .dtors.NNNNN sections are being | |
476 | placed in .fini_array then we need to extract the init_priority | |
477 | attribute and sort on that. */ | |
478 | dot = strrchr (name, '.'); | |
479 | if (dot != NULL && ISDIGIT (dot[1])) | |
02ecc8e9 | 480 | { |
9a24a276 AM |
481 | char *end; |
482 | unsigned long init_priority = strtoul (dot + 1, &end, 10); | |
483 | if (*end == 0) | |
484 | { | |
485 | if (dot == name + 6 | |
486 | && (strncmp (name, ".ctors", 6) == 0 | |
487 | || strncmp (name, ".dtors", 6) == 0)) | |
488 | init_priority = 65535 - init_priority; | |
489 | if (init_priority <= INT_MAX) | |
490 | return init_priority; | |
491 | } | |
02ecc8e9 | 492 | } |
9a24a276 | 493 | return -1; |
02ecc8e9 L |
494 | } |
495 | ||
50c77e5d NC |
496 | /* Compare sections ASEC and BSEC according to SORT. */ |
497 | ||
498 | static int | |
499 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
500 | { | |
501 | int ret; | |
9a24a276 | 502 | int a_priority, b_priority; |
50c77e5d NC |
503 | |
504 | switch (sort) | |
505 | { | |
506 | default: | |
507 | abort (); | |
508 | ||
02ecc8e9 | 509 | case by_init_priority: |
9a24a276 AM |
510 | a_priority = get_init_priority (asec); |
511 | b_priority = get_init_priority (bsec); | |
512 | if (a_priority < 0 || b_priority < 0) | |
02ecc8e9 | 513 | goto sort_by_name; |
9a24a276 | 514 | ret = a_priority - b_priority; |
02ecc8e9 L |
515 | if (ret) |
516 | break; | |
517 | else | |
518 | goto sort_by_name; | |
519 | ||
50c77e5d | 520 | case by_alignment_name: |
fd361982 | 521 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
522 | if (ret) |
523 | break; | |
524 | /* Fall through. */ | |
525 | ||
526 | case by_name: | |
dc1e8a47 | 527 | sort_by_name: |
fd361982 | 528 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
529 | break; |
530 | ||
531 | case by_name_alignment: | |
fd361982 | 532 | ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); |
50c77e5d NC |
533 | if (ret) |
534 | break; | |
535 | /* Fall through. */ | |
536 | ||
537 | case by_alignment: | |
fd361982 | 538 | ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); |
50c77e5d NC |
539 | break; |
540 | } | |
541 | ||
542 | return ret; | |
543 | } | |
544 | ||
af31506c MM |
545 | /* PE puts the sort key in the input statement. */ |
546 | ||
547 | static const char * | |
548 | sort_filename (bfd *abfd) | |
549 | { | |
550 | lang_input_statement_type *is = bfd_usrdata (abfd); | |
551 | if (is->sort_key) | |
552 | return is->sort_key; | |
553 | return bfd_get_filename (abfd); | |
554 | } | |
555 | ||
556 | /* Handle wildcard sorting. This returns the place in a binary search tree | |
557 | where this FILE:SECTION should be inserted for wild statement WILD where | |
558 | the spec SEC was the matching one. The tree is later linearized. */ | |
50c77e5d NC |
559 | |
560 | static lang_section_bst_type ** | |
af31506c MM |
561 | wild_sort (lang_wild_statement_type *wild, |
562 | struct wildcard_list *sec, | |
563 | lang_input_statement_type *file, | |
564 | asection *section) | |
50c77e5d | 565 | { |
329c1c86 | 566 | lang_section_bst_type **tree; |
50c77e5d NC |
567 | |
568 | if (!wild->filenames_sorted | |
af31506c MM |
569 | && (sec == NULL || sec->spec.sorted == none |
570 | || sec->spec.sorted == by_none)) | |
50c77e5d | 571 | { |
af31506c MM |
572 | /* We might be called even if _this_ spec doesn't need sorting, |
573 | in which case we simply append at the right end of tree. */ | |
574 | return wild->rightmost; | |
50c77e5d NC |
575 | } |
576 | ||
af31506c | 577 | tree = &wild->tree; |
329c1c86 | 578 | while (*tree) |
50c77e5d | 579 | { |
af31506c MM |
580 | /* Sorting by filename takes precedence over sorting by section |
581 | name. */ | |
582 | ||
583 | if (wild->filenames_sorted) | |
584 | { | |
585 | const char *fn, *ln; | |
586 | bool fa, la; | |
587 | int i; | |
588 | asection *lsec = (*tree)->section; | |
589 | ||
590 | /* The PE support for the .idata section as generated by | |
591 | dlltool assumes that files will be sorted by the name of | |
592 | the archive and then the name of the file within the | |
593 | archive. */ | |
594 | ||
595 | fa = file->the_bfd->my_archive != NULL; | |
596 | if (fa) | |
597 | fn = sort_filename (file->the_bfd->my_archive); | |
598 | else | |
599 | fn = sort_filename (file->the_bfd); | |
600 | ||
601 | la = lsec->owner->my_archive != NULL; | |
602 | if (la) | |
603 | ln = sort_filename (lsec->owner->my_archive); | |
604 | else | |
605 | ln = sort_filename (lsec->owner); | |
606 | ||
607 | i = filename_cmp (fn, ln); | |
608 | if (i > 0) | |
609 | { tree = &((*tree)->right); continue; } | |
610 | else if (i < 0) | |
611 | { tree = &((*tree)->left); continue; } | |
612 | ||
613 | if (fa || la) | |
614 | { | |
615 | if (fa) | |
616 | fn = sort_filename (file->the_bfd); | |
617 | if (la) | |
618 | ln = sort_filename (lsec->owner); | |
619 | ||
620 | i = filename_cmp (fn, ln); | |
621 | if (i > 0) | |
622 | { tree = &((*tree)->right); continue; } | |
623 | else if (i < 0) | |
624 | { tree = &((*tree)->left); continue; } | |
625 | } | |
626 | } | |
627 | ||
628 | /* Here either the files are not sorted by name, or we are | |
629 | looking at the sections for this file. */ | |
630 | ||
50c77e5d | 631 | /* Find the correct node to append this section. */ |
329c1c86 AM |
632 | if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0) |
633 | tree = &((*tree)->left); | |
634 | else | |
635 | tree = &((*tree)->right); | |
50c77e5d NC |
636 | } |
637 | ||
638 | return tree; | |
639 | } | |
640 | ||
af31506c | 641 | /* Use wild_sort to build a BST to sort sections. */ |
50c77e5d NC |
642 | |
643 | static void | |
af31506c | 644 | output_section_callback_sort (lang_wild_statement_type *ptr, |
329c1c86 AM |
645 | struct wildcard_list *sec, |
646 | asection *section, | |
647 | lang_input_statement_type *file, | |
d0bf826b | 648 | void *output) |
50c77e5d NC |
649 | { |
650 | lang_section_bst_type *node; | |
651 | lang_section_bst_type **tree; | |
d0bf826b | 652 | lang_output_section_statement_type *os; |
50c77e5d | 653 | |
d0bf826b AM |
654 | os = (lang_output_section_statement_type *) output; |
655 | ||
656 | if (unique_section_p (section, os)) | |
50c77e5d NC |
657 | return; |
658 | ||
1e9cc1c2 | 659 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
50c77e5d NC |
660 | node->left = 0; |
661 | node->right = 0; | |
662 | node->section = section; | |
b209b5a6 | 663 | node->pattern = ptr->section_list; |
50c77e5d | 664 | |
af31506c | 665 | tree = wild_sort (ptr, sec, file, section); |
50c77e5d | 666 | if (tree != NULL) |
af31506c MM |
667 | { |
668 | *tree = node; | |
669 | if (tree == ptr->rightmost) | |
670 | ptr->rightmost = &node->right; | |
671 | } | |
50c77e5d NC |
672 | } |
673 | ||
674 | /* Convert a sorted sections' BST back to list form. */ | |
675 | ||
676 | static void | |
329c1c86 AM |
677 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
678 | lang_section_bst_type *tree, | |
679 | void *output) | |
50c77e5d NC |
680 | { |
681 | if (tree->left) | |
682 | output_section_callback_tree_to_list (ptr, tree->left, output); | |
683 | ||
af31506c MM |
684 | lang_add_section (&ptr->children, tree->section, tree->pattern, |
685 | ptr->section_flag_list, | |
329c1c86 | 686 | (lang_output_section_statement_type *) output); |
50c77e5d NC |
687 | |
688 | if (tree->right) | |
689 | output_section_callback_tree_to_list (ptr, tree->right, output); | |
690 | ||
691 | free (tree); | |
692 | } | |
693 | ||
b1eecf6f MM |
694 | \f |
695 | /* Sections are matched against wildcard statements via a prefix tree. | |
696 | The prefix tree holds prefixes of all matching patterns (up to the first | |
697 | wildcard character), and the wild statement from which those patterns | |
698 | came. When matching a section name against the tree we're walking through | |
699 | the tree character by character. Each statement we hit is one that | |
700 | potentially matches. This is checked by actually going through the | |
701 | (glob) matching routines. | |
702 | ||
703 | When the section name turns out to actually match we record that section | |
704 | in the wild statements list of matching sections. */ | |
705 | ||
706 | /* A prefix can be matched by multiple statement, so we need a list of them. */ | |
707 | struct wild_stmt_list | |
708 | { | |
709 | lang_wild_statement_type *stmt; | |
710 | struct wild_stmt_list *next; | |
711 | }; | |
712 | ||
713 | /* The prefix tree itself. */ | |
714 | struct prefixtree | |
715 | { | |
716 | /* The list of all children (linked via .next). */ | |
717 | struct prefixtree *child; | |
718 | struct prefixtree *next; | |
719 | /* This tree node is responsible for the prefix of parent plus 'c'. */ | |
720 | char c; | |
721 | /* The statements that potentially can match this prefix. */ | |
722 | struct wild_stmt_list *stmt; | |
723 | }; | |
724 | ||
725 | /* We always have a root node in the prefix tree. It corresponds to the | |
726 | empty prefix. E.g. a glob like "*" would sit in this root. */ | |
727 | static struct prefixtree the_root, *ptroot = &the_root; | |
728 | ||
729 | /* Given a prefix tree in *TREE, corresponding to prefix P, find or | |
730 | INSERT the tree node corresponding to prefix P+C. */ | |
731 | ||
732 | static struct prefixtree * | |
733 | get_prefix_tree (struct prefixtree **tree, char c, bool insert) | |
734 | { | |
735 | struct prefixtree *t; | |
736 | for (t = *tree; t; t = t->next) | |
737 | if (t->c == c) | |
738 | return t; | |
739 | if (!insert) | |
740 | return NULL; | |
741 | t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); | |
742 | t->child = NULL; | |
743 | t->next = *tree; | |
744 | t->c = c; | |
745 | t->stmt = NULL; | |
746 | *tree = t; | |
747 | return t; | |
748 | } | |
749 | ||
750 | /* Add STMT to the set of statements that can be matched by the prefix | |
751 | corresponding to prefix tree T. */ | |
752 | ||
753 | static void | |
754 | pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) | |
755 | { | |
756 | struct wild_stmt_list *sl, **psl; | |
757 | sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); | |
758 | sl->stmt = stmt; | |
759 | sl->next = NULL; | |
760 | psl = &t->stmt; | |
761 | while (*psl) | |
762 | psl = &(*psl)->next; | |
763 | *psl = sl; | |
764 | } | |
765 | ||
766 | /* Insert STMT into the global prefix tree. */ | |
767 | ||
768 | static void | |
769 | insert_prefix_tree (lang_wild_statement_type *stmt) | |
770 | { | |
771 | struct wildcard_list *sec; | |
772 | struct prefixtree *t; | |
773 | ||
774 | if (!stmt->section_list) | |
775 | { | |
776 | /* If we have no section_list (no wildcards in the wild STMT), | |
777 | then every section name will match, so add this to the root. */ | |
778 | pt_add_stmt (ptroot, stmt); | |
779 | return; | |
780 | } | |
781 | ||
782 | for (sec = stmt->section_list; sec; sec = sec->next) | |
783 | { | |
784 | const char *name = sec->spec.name ? sec->spec.name : "*"; | |
785 | char c; | |
786 | t = ptroot; | |
787 | for (; (c = *name); name++) | |
788 | { | |
789 | if (c == '*' || c == '[' || c == '?') | |
790 | break; | |
791 | t = get_prefix_tree (&t->child, c, true); | |
792 | } | |
793 | /* If we hit a glob character, the matching prefix is what we saw | |
794 | until now. If we hit the end of pattern (hence it's no glob) then | |
795 | we can do better: we only need to record a match when a section name | |
796 | completely matches, not merely a prefix, so record the trailing 0 | |
797 | as well. */ | |
798 | if (!c) | |
799 | t = get_prefix_tree (&t->child, 0, true); | |
800 | pt_add_stmt (t, stmt); | |
801 | } | |
802 | } | |
803 | ||
804 | /* Dump T indented by INDENT spaces. */ | |
805 | ||
806 | static void | |
807 | debug_prefix_tree_rec (struct prefixtree *t, int indent) | |
808 | { | |
809 | for (; t; t = t->next) | |
810 | { | |
811 | struct wild_stmt_list *sl; | |
812 | printf ("%*s %c", indent, "", t->c); | |
813 | for (sl = t->stmt; sl; sl = sl->next) | |
814 | { | |
815 | struct wildcard_list *curr; | |
816 | printf (" %p ", sl->stmt); | |
817 | for (curr = sl->stmt->section_list; curr; curr = curr->next) | |
818 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
819 | } | |
820 | printf ("\n"); | |
821 | debug_prefix_tree_rec (t->child, indent + 2); | |
822 | } | |
823 | } | |
824 | ||
825 | /* Dump the global prefix tree. */ | |
826 | ||
827 | static void | |
828 | debug_prefix_tree (void) | |
829 | { | |
830 | debug_prefix_tree_rec (ptroot, 2); | |
831 | } | |
832 | ||
049522ca MM |
833 | /* Like strcspn() but start to look from the end to beginning of |
834 | S. Returns the length of the suffix of S consisting entirely | |
835 | of characters not in REJECT. */ | |
836 | ||
837 | static size_t | |
838 | rstrcspn (const char *s, const char *reject) | |
839 | { | |
840 | size_t len = strlen (s), sufflen = 0; | |
841 | while (len--) | |
842 | { | |
843 | char c = s[len]; | |
844 | if (strchr (reject, c) != 0) | |
845 | break; | |
846 | sufflen++; | |
847 | } | |
848 | return sufflen; | |
849 | } | |
850 | ||
b1eecf6f MM |
851 | /* Analyze the wildcards in wild statement PTR to setup various |
852 | things for quick matching. */ | |
72223188 JJ |
853 | |
854 | static void | |
855 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) | |
856 | { | |
72223188 | 857 | struct wildcard_list *sec; |
d7791499 | 858 | |
e6f2cbf5 | 859 | ptr->tree = NULL; |
af31506c | 860 | ptr->rightmost = &ptr->tree; |
72223188 | 861 | |
049522ca MM |
862 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
863 | { | |
864 | if (sec->spec.name) | |
865 | { | |
866 | sec->spec.namelen = strlen (sec->spec.name); | |
867 | sec->spec.prefixlen = strcspn (sec->spec.name, "?*["); | |
868 | sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen, | |
869 | "?*]"); | |
870 | } | |
871 | else | |
872 | sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; | |
873 | } | |
874 | ||
b1eecf6f | 875 | insert_prefix_tree (ptr); |
4dec4d4e RH |
876 | } |
877 | ||
b1eecf6f MM |
878 | /* Match all sections from FILE against the global prefix tree, |
879 | and record them into each wild statement that has a match. */ | |
880 | ||
4dec4d4e | 881 | static void |
b1eecf6f | 882 | resolve_wild_sections (lang_input_statement_type *file) |
43ae96e9 | 883 | { |
b1eecf6f MM |
884 | asection *s; |
885 | ||
886 | if (file->flags.just_syms) | |
887 | return; | |
888 | ||
889 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
890 | { | |
891 | const char *sname = bfd_section_name (s); | |
892 | char c = 1; | |
893 | struct prefixtree *t = ptroot; | |
894 | //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename); | |
895 | do | |
896 | { | |
897 | if (t->stmt) | |
898 | { | |
899 | struct wild_stmt_list *sl; | |
900 | for (sl = t->stmt; sl; sl = sl->next) | |
901 | { | |
902 | walk_wild_section_match (sl->stmt, file, s); | |
903 | //printf (" ZZZ maybe place into %p\n", sl->stmt); | |
904 | } | |
905 | } | |
906 | if (!c) | |
907 | break; | |
908 | c = *sname++; | |
909 | t = get_prefix_tree (&t->child, c, false); | |
910 | } | |
911 | while (t); | |
912 | } | |
43ae96e9 MM |
913 | } |
914 | ||
b1eecf6f MM |
915 | /* Match all sections from all input files against the global prefix tree. */ |
916 | ||
917 | static void | |
918 | resolve_wilds (void) | |
919 | { | |
920 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
921 | { | |
922 | //printf("XXX %s\n", f->filename); | |
923 | /* XXX if (walk_wild_file_in_exclude_list (s->exclude_name_list, f)) | |
924 | return;*/ | |
925 | ||
926 | if (f->the_bfd == NULL | |
927 | || !bfd_check_format (f->the_bfd, bfd_archive)) | |
928 | resolve_wild_sections (f); | |
929 | else | |
930 | { | |
931 | bfd *member; | |
932 | ||
933 | /* This is an archive file. We must map each member of the | |
934 | archive separately. */ | |
935 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); | |
936 | while (member != NULL) | |
937 | { | |
938 | /* When lookup_name is called, it will call the add_symbols | |
939 | entry point for the archive. For each element of the | |
940 | archive which is included, BFD will call ldlang_add_file, | |
941 | which will set the usrdata field of the member to the | |
942 | lang_input_statement. */ | |
943 | if (bfd_usrdata (member) != NULL) | |
944 | resolve_wild_sections (bfd_usrdata (member)); | |
945 | ||
946 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
947 | } | |
948 | } | |
949 | } | |
950 | } | |
951 | ||
b1eecf6f MM |
952 | /* For each input section that matches wild statement S calls |
953 | CALLBACK with DATA. */ | |
43ae96e9 MM |
954 | |
955 | static void | |
956 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) | |
957 | { | |
b1eecf6f | 958 | lang_statement_union_type *l; |
43ae96e9 | 959 | |
b1eecf6f | 960 | for (l = s->matching_sections.head; l; l = l->header.next) |
43ae96e9 | 961 | { |
b1eecf6f MM |
962 | (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, |
963 | l->input_matcher.input_stmt, data); | |
43ae96e9 | 964 | } |
5f992e62 AM |
965 | } |
966 | ||
08da4cac | 967 | /* lang_for_each_statement walks the parse tree and calls the provided |
8658f989 AM |
968 | function for each node, except those inside output section statements |
969 | with constraint set to -1. */ | |
252b5132 | 970 | |
8658f989 | 971 | void |
1579bae1 AM |
972 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
973 | lang_statement_union_type *s) | |
252b5132 | 974 | { |
1579bae1 | 975 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
976 | { |
977 | func (s); | |
978 | ||
979 | switch (s->header.type) | |
980 | { | |
981 | case lang_constructors_statement_enum: | |
982 | lang_for_each_statement_worker (func, constructor_list.head); | |
983 | break; | |
984 | case lang_output_section_statement_enum: | |
8658f989 AM |
985 | if (s->output_section_statement.constraint != -1) |
986 | lang_for_each_statement_worker | |
987 | (func, s->output_section_statement.children.head); | |
252b5132 RH |
988 | break; |
989 | case lang_wild_statement_enum: | |
6feb9908 AM |
990 | lang_for_each_statement_worker (func, |
991 | s->wild_statement.children.head); | |
252b5132 RH |
992 | break; |
993 | case lang_group_statement_enum: | |
994 | lang_for_each_statement_worker (func, | |
995 | s->group_statement.children.head); | |
996 | break; | |
997 | case lang_data_statement_enum: | |
998 | case lang_reloc_statement_enum: | |
999 | case lang_object_symbols_statement_enum: | |
1000 | case lang_output_statement_enum: | |
1001 | case lang_target_statement_enum: | |
1002 | case lang_input_section_enum: | |
1003 | case lang_input_statement_enum: | |
1004 | case lang_assignment_statement_enum: | |
1005 | case lang_padding_statement_enum: | |
1006 | case lang_address_statement_enum: | |
1007 | case lang_fill_statement_enum: | |
53d25da6 | 1008 | case lang_insert_statement_enum: |
252b5132 RH |
1009 | break; |
1010 | default: | |
1011 | FAIL (); | |
1012 | break; | |
1013 | } | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | void | |
1579bae1 | 1018 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 1019 | { |
08da4cac | 1020 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
1021 | } |
1022 | ||
1023 | /*----------------------------------------------------------------------*/ | |
08da4cac | 1024 | |
252b5132 | 1025 | void |
1579bae1 | 1026 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 1027 | { |
1579bae1 | 1028 | list->head = NULL; |
252b5132 RH |
1029 | list->tail = &list->head; |
1030 | } | |
1031 | ||
36983a93 AM |
1032 | static void |
1033 | lang_statement_append (lang_statement_list_type *list, | |
1034 | void *element, | |
1035 | void *field) | |
1036 | { | |
1037 | *(list->tail) = element; | |
1038 | list->tail = field; | |
1039 | } | |
1040 | ||
bde18da4 AM |
1041 | void |
1042 | push_stat_ptr (lang_statement_list_type *new_ptr) | |
1043 | { | |
1044 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) | |
1045 | abort (); | |
1046 | *stat_save_ptr++ = stat_ptr; | |
1047 | stat_ptr = new_ptr; | |
1048 | } | |
1049 | ||
1050 | void | |
1051 | pop_stat_ptr (void) | |
1052 | { | |
1053 | if (stat_save_ptr <= stat_save) | |
1054 | abort (); | |
1055 | stat_ptr = *--stat_save_ptr; | |
1056 | } | |
1057 | ||
08da4cac | 1058 | /* Build a new statement node for the parse tree. */ |
252b5132 | 1059 | |
08da4cac | 1060 | static lang_statement_union_type * |
1579bae1 AM |
1061 | new_statement (enum statement_enum type, |
1062 | size_t size, | |
1063 | lang_statement_list_type *list) | |
252b5132 | 1064 | { |
d3ce72d0 | 1065 | lang_statement_union_type *new_stmt; |
252b5132 | 1066 | |
988de25b | 1067 | new_stmt = stat_alloc (size); |
d3ce72d0 NC |
1068 | new_stmt->header.type = type; |
1069 | new_stmt->header.next = NULL; | |
1070 | lang_statement_append (list, new_stmt, &new_stmt->header.next); | |
1071 | return new_stmt; | |
252b5132 RH |
1072 | } |
1073 | ||
08da4cac KH |
1074 | /* Build a new input file node for the language. There are several |
1075 | ways in which we treat an input file, eg, we only look at symbols, | |
1076 | or prefix it with a -l etc. | |
252b5132 | 1077 | |
08da4cac | 1078 | We can be supplied with requests for input files more than once; |
396a2467 | 1079 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 1080 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 1081 | got it already so we don't duplicate the bfd. */ |
252b5132 | 1082 | |
252b5132 | 1083 | static lang_input_statement_type * |
1579bae1 AM |
1084 | new_afile (const char *name, |
1085 | lang_input_file_enum_type file_type, | |
16171946 FS |
1086 | const char *target, |
1087 | const char *from_filename) | |
252b5132 RH |
1088 | { |
1089 | lang_input_statement_type *p; | |
1090 | ||
f38a2680 | 1091 | lang_has_input_file = true; |
66be1055 | 1092 | |
1f1f5b92 | 1093 | p = new_stat (lang_input_statement, stat_ptr); |
66be1055 AM |
1094 | memset (&p->the_bfd, 0, |
1095 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); | |
16171946 | 1096 | p->extra_search_path = NULL; |
252b5132 | 1097 | p->target = target; |
66be1055 AM |
1098 | p->flags.dynamic = input_flags.dynamic; |
1099 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; | |
1100 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; | |
1101 | p->flags.whole_archive = input_flags.whole_archive; | |
f4a23d42 | 1102 | p->flags.sysrooted = input_flags.sysrooted; |
a6ad7914 | 1103 | p->sort_key = NULL; |
bcb674cf | 1104 | |
252b5132 RH |
1105 | switch (file_type) |
1106 | { | |
1107 | case lang_input_file_is_symbols_only_enum: | |
1108 | p->filename = name; | |
252b5132 | 1109 | p->local_sym_name = name; |
f38a2680 AM |
1110 | p->flags.real = true; |
1111 | p->flags.just_syms = true; | |
252b5132 RH |
1112 | break; |
1113 | case lang_input_file_is_fake_enum: | |
1114 | p->filename = name; | |
252b5132 | 1115 | p->local_sym_name = name; |
252b5132 RH |
1116 | break; |
1117 | case lang_input_file_is_l_enum: | |
d4ae5fb0 | 1118 | if (name[0] == ':' && name[1] != '\0') |
0aa7f586 AM |
1119 | { |
1120 | p->filename = name + 1; | |
f38a2680 | 1121 | p->flags.full_name_provided = true; |
0aa7f586 | 1122 | } |
d4ae5fb0 | 1123 | else |
0aa7f586 | 1124 | p->filename = name; |
ff7a0acf | 1125 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
f38a2680 AM |
1126 | p->flags.maybe_archive = true; |
1127 | p->flags.real = true; | |
1128 | p->flags.search_dirs = true; | |
252b5132 RH |
1129 | break; |
1130 | case lang_input_file_is_marker_enum: | |
1131 | p->filename = name; | |
252b5132 | 1132 | p->local_sym_name = name; |
f38a2680 | 1133 | p->flags.search_dirs = true; |
252b5132 RH |
1134 | break; |
1135 | case lang_input_file_is_search_file_enum: | |
1136 | p->filename = name; | |
252b5132 | 1137 | p->local_sym_name = name; |
16171946 FS |
1138 | /* If name is a relative path, search the directory of the current linker |
1139 | script first. */ | |
1140 | if (from_filename && !IS_ABSOLUTE_PATH (name)) | |
1141 | p->extra_search_path = ldirname (from_filename); | |
f38a2680 AM |
1142 | p->flags.real = true; |
1143 | p->flags.search_dirs = true; | |
252b5132 RH |
1144 | break; |
1145 | case lang_input_file_is_file_enum: | |
1146 | p->filename = name; | |
252b5132 | 1147 | p->local_sym_name = name; |
f38a2680 | 1148 | p->flags.real = true; |
252b5132 RH |
1149 | break; |
1150 | default: | |
1151 | FAIL (); | |
1152 | } | |
c4b78195 | 1153 | |
36983a93 | 1154 | lang_statement_append (&input_file_chain, p, &p->next_real_file); |
252b5132 RH |
1155 | return p; |
1156 | } | |
1157 | ||
1158 | lang_input_statement_type * | |
1579bae1 AM |
1159 | lang_add_input_file (const char *name, |
1160 | lang_input_file_enum_type file_type, | |
1161 | const char *target) | |
252b5132 | 1162 | { |
3aa2d05a | 1163 | if (name != NULL |
08dedd66 | 1164 | && (*name == '=' || startswith (name, "$SYSROOT"))) |
bfa23434 HPN |
1165 | { |
1166 | lang_input_statement_type *ret; | |
1167 | char *sysrooted_name | |
3aa2d05a NC |
1168 | = concat (ld_sysroot, |
1169 | name + (*name == '=' ? 1 : strlen ("$SYSROOT")), | |
1170 | (const char *) NULL); | |
bfa23434 HPN |
1171 | |
1172 | /* We've now forcibly prepended the sysroot, making the input | |
1173 | file independent of the context. Therefore, temporarily | |
1174 | force a non-sysrooted context for this statement, so it won't | |
1175 | get the sysroot prepended again when opened. (N.B. if it's a | |
1176 | script, any child nodes with input files starting with "/" | |
1177 | will be handled as "sysrooted" as they'll be found to be | |
1178 | within the sysroot subdirectory.) */ | |
1179 | unsigned int outer_sysrooted = input_flags.sysrooted; | |
1180 | input_flags.sysrooted = 0; | |
16171946 | 1181 | ret = new_afile (sysrooted_name, file_type, target, NULL); |
bfa23434 HPN |
1182 | input_flags.sysrooted = outer_sysrooted; |
1183 | return ret; | |
1184 | } | |
1185 | ||
16171946 | 1186 | return new_afile (name, file_type, target, current_input_file); |
252b5132 RH |
1187 | } |
1188 | ||
409d7240 | 1189 | struct out_section_hash_entry |
750877ba L |
1190 | { |
1191 | struct bfd_hash_entry root; | |
409d7240 | 1192 | lang_statement_union_type s; |
750877ba L |
1193 | }; |
1194 | ||
1195 | /* The hash table. */ | |
1196 | ||
409d7240 | 1197 | static struct bfd_hash_table output_section_statement_table; |
750877ba | 1198 | |
384a9dda | 1199 | /* Support routines for the hash table used by lang_output_section_find, |
750877ba L |
1200 | initialize the table, fill in an entry and remove the table. */ |
1201 | ||
1202 | static struct bfd_hash_entry * | |
329c1c86 | 1203 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
409d7240 AM |
1204 | struct bfd_hash_table *table, |
1205 | const char *string) | |
750877ba | 1206 | { |
384a9dda | 1207 | lang_output_section_statement_type **nextp; |
409d7240 | 1208 | struct out_section_hash_entry *ret; |
384a9dda AM |
1209 | |
1210 | if (entry == NULL) | |
1211 | { | |
1e9cc1c2 | 1212 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
4724d37e | 1213 | sizeof (*ret)); |
384a9dda AM |
1214 | if (entry == NULL) |
1215 | return entry; | |
1216 | } | |
1217 | ||
1218 | entry = bfd_hash_newfunc (entry, table, string); | |
1219 | if (entry == NULL) | |
1220 | return entry; | |
1221 | ||
409d7240 AM |
1222 | ret = (struct out_section_hash_entry *) entry; |
1223 | memset (&ret->s, 0, sizeof (ret->s)); | |
1224 | ret->s.header.type = lang_output_section_statement_enum; | |
3d9c8f6b AM |
1225 | ret->s.output_section_statement.subsection_alignment = NULL; |
1226 | ret->s.output_section_statement.section_alignment = NULL; | |
409d7240 AM |
1227 | ret->s.output_section_statement.block_value = 1; |
1228 | lang_list_init (&ret->s.output_section_statement.children); | |
1229 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); | |
384a9dda | 1230 | |
218868ba | 1231 | /* For every output section statement added to the list, except the |
5c1e6d53 | 1232 | first one, lang_os_list.tail points to the "next" |
218868ba | 1233 | field of the last element of the list. */ |
5c1e6d53 | 1234 | if (lang_os_list.head != NULL) |
409d7240 AM |
1235 | ret->s.output_section_statement.prev |
1236 | = ((lang_output_section_statement_type *) | |
5c1e6d53 | 1237 | ((char *) lang_os_list.tail |
409d7240 | 1238 | - offsetof (lang_output_section_statement_type, next))); |
218868ba | 1239 | |
384a9dda AM |
1240 | /* GCC's strict aliasing rules prevent us from just casting the |
1241 | address, so we store the pointer in a variable and cast that | |
1242 | instead. */ | |
409d7240 | 1243 | nextp = &ret->s.output_section_statement.next; |
36983a93 | 1244 | lang_statement_append (&lang_os_list, &ret->s, nextp); |
384a9dda | 1245 | return &ret->root; |
750877ba L |
1246 | } |
1247 | ||
1248 | static void | |
409d7240 | 1249 | output_section_statement_table_init (void) |
750877ba | 1250 | { |
409d7240 AM |
1251 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
1252 | output_section_statement_newfunc, | |
1253 | sizeof (struct out_section_hash_entry), | |
66eb6687 | 1254 | 61)) |
df5f2391 | 1255 | einfo (_("%F%P: can not create hash table: %E\n")); |
750877ba L |
1256 | } |
1257 | ||
1258 | static void | |
409d7240 | 1259 | output_section_statement_table_free (void) |
750877ba | 1260 | { |
409d7240 | 1261 | bfd_hash_table_free (&output_section_statement_table); |
750877ba L |
1262 | } |
1263 | ||
08da4cac KH |
1264 | /* Build enough state so that the parser can build its tree. */ |
1265 | ||
252b5132 | 1266 | void |
1579bae1 | 1267 | lang_init (void) |
252b5132 RH |
1268 | { |
1269 | obstack_begin (&stat_obstack, 1000); | |
b1eecf6f | 1270 | obstack_init (&pt_obstack); |
252b5132 RH |
1271 | |
1272 | stat_ptr = &statement_list; | |
1273 | ||
409d7240 | 1274 | output_section_statement_table_init (); |
750877ba | 1275 | |
252b5132 RH |
1276 | lang_list_init (stat_ptr); |
1277 | ||
1278 | lang_list_init (&input_file_chain); | |
5c1e6d53 | 1279 | lang_list_init (&lang_os_list); |
252b5132 | 1280 | lang_list_init (&file_chain); |
1579bae1 AM |
1281 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
1282 | NULL); | |
08da4cac | 1283 | abs_output_section = |
21401fc7 | 1284 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); |
252b5132 RH |
1285 | |
1286 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c | 1287 | |
16e4ecc0 AM |
1288 | asneeded_list_head = NULL; |
1289 | asneeded_list_tail = &asneeded_list_head; | |
252b5132 RH |
1290 | } |
1291 | ||
750877ba L |
1292 | void |
1293 | lang_finish (void) | |
1294 | { | |
409d7240 | 1295 | output_section_statement_table_free (); |
750877ba L |
1296 | } |
1297 | ||
252b5132 | 1298 | /*---------------------------------------------------------------------- |
08da4cac KH |
1299 | A region is an area of memory declared with the |
1300 | MEMORY { name:org=exp, len=exp ... } | |
1301 | syntax. | |
252b5132 | 1302 | |
08da4cac | 1303 | We maintain a list of all the regions here. |
252b5132 | 1304 | |
08da4cac | 1305 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
1306 | which is created when looked up to be the entire data space. |
1307 | ||
1308 | If create is true we are creating a region inside a MEMORY block. | |
1309 | In this case it is probably an error to create a region that has | |
1310 | already been created. If we are not inside a MEMORY block it is | |
1311 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
4a93e180 | 1312 | and so we issue a warning. |
1e0061d2 | 1313 | |
4a93e180 NC |
1314 | Each region has at least one name. The first name is either |
1315 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add | |
1316 | alias names to an existing region within a script with | |
1317 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one | |
1318 | region. */ | |
252b5132 RH |
1319 | |
1320 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
1321 | static lang_memory_region_type **lang_memory_region_list_tail |
1322 | = &lang_memory_region_list; | |
252b5132 RH |
1323 | |
1324 | lang_memory_region_type * | |
f38a2680 | 1325 | lang_memory_region_lookup (const char *const name, bool create) |
252b5132 | 1326 | { |
4a93e180 NC |
1327 | lang_memory_region_name *n; |
1328 | lang_memory_region_type *r; | |
d3ce72d0 | 1329 | lang_memory_region_type *new_region; |
252b5132 | 1330 | |
9f88b410 RS |
1331 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
1332 | if (name == NULL) | |
1333 | return NULL; | |
1334 | ||
4a93e180 NC |
1335 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
1336 | for (n = &r->name_list; n != NULL; n = n->next) | |
1337 | if (strcmp (n->name, name) == 0) | |
4724d37e RM |
1338 | { |
1339 | if (create) | |
c1c8c1ef | 1340 | einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), |
4724d37e RM |
1341 | NULL, name); |
1342 | return r; | |
1343 | } | |
252b5132 | 1344 | |
a747ee4d | 1345 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
c1c8c1ef | 1346 | einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), |
dab69f68 | 1347 | NULL, name); |
a747ee4d | 1348 | |
988de25b | 1349 | new_region = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 1350 | |
d3ce72d0 NC |
1351 | new_region->name_list.name = xstrdup (name); |
1352 | new_region->name_list.next = NULL; | |
1353 | new_region->next = NULL; | |
cc9ad334 | 1354 | new_region->origin_exp = NULL; |
d3ce72d0 | 1355 | new_region->origin = 0; |
cc9ad334 | 1356 | new_region->length_exp = NULL; |
d3ce72d0 NC |
1357 | new_region->length = ~(bfd_size_type) 0; |
1358 | new_region->current = 0; | |
1359 | new_region->last_os = NULL; | |
1360 | new_region->flags = 0; | |
1361 | new_region->not_flags = 0; | |
f38a2680 | 1362 | new_region->had_full_message = false; |
252b5132 | 1363 | |
d3ce72d0 NC |
1364 | *lang_memory_region_list_tail = new_region; |
1365 | lang_memory_region_list_tail = &new_region->next; | |
66e28d60 | 1366 | |
d3ce72d0 | 1367 | return new_region; |
252b5132 RH |
1368 | } |
1369 | ||
4a93e180 | 1370 | void |
0aa7f586 | 1371 | lang_memory_region_alias (const char *alias, const char *region_name) |
4a93e180 | 1372 | { |
0aa7f586 AM |
1373 | lang_memory_region_name *n; |
1374 | lang_memory_region_type *r; | |
1375 | lang_memory_region_type *region; | |
4a93e180 NC |
1376 | |
1377 | /* The default region must be unique. This ensures that it is not necessary | |
1378 | to iterate through the name list if someone wants the check if a region is | |
1379 | the default memory region. */ | |
1380 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 | |
1381 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) | |
c1c8c1ef | 1382 | einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL); |
4a93e180 NC |
1383 | |
1384 | /* Look for the target region and check if the alias is not already | |
1385 | in use. */ | |
1386 | region = NULL; | |
1387 | for (r = lang_memory_region_list; r != NULL; r = r->next) | |
1388 | for (n = &r->name_list; n != NULL; n = n->next) | |
1389 | { | |
4724d37e RM |
1390 | if (region == NULL && strcmp (n->name, region_name) == 0) |
1391 | region = r; | |
1392 | if (strcmp (n->name, alias) == 0) | |
c1c8c1ef | 1393 | einfo (_("%F%P:%pS: error: redefinition of memory region " |
4724d37e RM |
1394 | "alias `%s'\n"), |
1395 | NULL, alias); | |
4a93e180 NC |
1396 | } |
1397 | ||
1398 | /* Check if the target region exists. */ | |
1399 | if (region == NULL) | |
c1c8c1ef | 1400 | einfo (_("%F%P:%pS: error: memory region `%s' " |
4724d37e RM |
1401 | "for alias `%s' does not exist\n"), |
1402 | NULL, region_name, alias); | |
4a93e180 NC |
1403 | |
1404 | /* Add alias to region name list. */ | |
988de25b | 1405 | n = stat_alloc (sizeof (lang_memory_region_name)); |
4a93e180 NC |
1406 | n->name = xstrdup (alias); |
1407 | n->next = region->name_list.next; | |
1408 | region->name_list.next = n; | |
1409 | } | |
1410 | ||
5f992e62 | 1411 | static lang_memory_region_type * |
0aa7f586 | 1412 | lang_memory_default (asection *section) |
252b5132 RH |
1413 | { |
1414 | lang_memory_region_type *p; | |
1415 | ||
1416 | flagword sec_flags = section->flags; | |
1417 | ||
1418 | /* Override SEC_DATA to mean a writable section. */ | |
1419 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
1420 | sec_flags |= SEC_DATA; | |
1421 | ||
1579bae1 | 1422 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
1423 | { |
1424 | if ((p->flags & sec_flags) != 0 | |
1425 | && (p->not_flags & sec_flags) == 0) | |
1426 | { | |
1427 | return p; | |
1428 | } | |
1429 | } | |
f38a2680 | 1430 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
252b5132 RH |
1431 | } |
1432 | ||
24ef1aa7 GM |
1433 | /* Get the output section statement directly from the userdata. */ |
1434 | ||
1435 | lang_output_section_statement_type * | |
1436 | lang_output_section_get (const asection *output_section) | |
1437 | { | |
a48931cc | 1438 | return bfd_section_userdata (output_section); |
24ef1aa7 GM |
1439 | } |
1440 | ||
d127ecce AM |
1441 | /* Find or create an output_section_statement with the given NAME. |
1442 | If CONSTRAINT is non-zero match one with that constraint, otherwise | |
21401fc7 AM |
1443 | match any non-negative constraint. If CREATE is 0 return NULL when |
1444 | no match exists. If CREATE is 1, create an output_section_statement | |
1445 | when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2, | |
1446 | always make a new output_section_statement. */ | |
d127ecce | 1447 | |
384a9dda | 1448 | lang_output_section_statement_type * |
d127ecce | 1449 | lang_output_section_statement_lookup (const char *name, |
66c103b7 | 1450 | int constraint, |
21401fc7 | 1451 | int create) |
252b5132 | 1452 | { |
409d7240 | 1453 | struct out_section_hash_entry *entry; |
252b5132 | 1454 | |
409d7240 AM |
1455 | entry = ((struct out_section_hash_entry *) |
1456 | bfd_hash_lookup (&output_section_statement_table, name, | |
f38a2680 | 1457 | create != 0, false)); |
384a9dda AM |
1458 | if (entry == NULL) |
1459 | { | |
66c103b7 | 1460 | if (create) |
df5f2391 | 1461 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1462 | return NULL; |
1463 | } | |
252b5132 | 1464 | |
409d7240 | 1465 | if (entry->s.output_section_statement.name != NULL) |
252b5132 | 1466 | { |
384a9dda AM |
1467 | /* We have a section of this name, but it might not have the correct |
1468 | constraint. */ | |
66c103b7 | 1469 | struct out_section_hash_entry *last_ent; |
66c103b7 | 1470 | |
d127ecce | 1471 | name = entry->s.output_section_statement.name; |
21401fc7 AM |
1472 | do |
1473 | { | |
1474 | if (create != 2 | |
1475 | && !(create && constraint == SPECIAL) | |
1476 | && (constraint == entry->s.output_section_statement.constraint | |
1477 | || (constraint == 0 | |
1478 | && entry->s.output_section_statement.constraint >= 0))) | |
1479 | return &entry->s.output_section_statement; | |
1480 | last_ent = entry; | |
1481 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1482 | } | |
1483 | while (entry != NULL | |
1484 | && name == entry->s.output_section_statement.name); | |
252b5132 | 1485 | |
66c103b7 AM |
1486 | if (!create) |
1487 | return NULL; | |
1488 | ||
409d7240 AM |
1489 | entry |
1490 | = ((struct out_section_hash_entry *) | |
1491 | output_section_statement_newfunc (NULL, | |
1492 | &output_section_statement_table, | |
1493 | name)); | |
750877ba | 1494 | if (entry == NULL) |
384a9dda | 1495 | { |
df5f2391 | 1496 | einfo (_("%F%P: failed creating section `%s': %E\n"), name); |
384a9dda AM |
1497 | return NULL; |
1498 | } | |
1499 | entry->root = last_ent->root; | |
1500 | last_ent->root.next = &entry->root; | |
252b5132 | 1501 | } |
384a9dda | 1502 | |
409d7240 AM |
1503 | entry->s.output_section_statement.name = name; |
1504 | entry->s.output_section_statement.constraint = constraint; | |
de34d428 AM |
1505 | entry->s.output_section_statement.dup_output = (create == 2 |
1506 | || constraint == SPECIAL); | |
409d7240 | 1507 | return &entry->s.output_section_statement; |
252b5132 RH |
1508 | } |
1509 | ||
d127ecce AM |
1510 | /* Find the next output_section_statement with the same name as OS. |
1511 | If CONSTRAINT is non-zero, find one with that constraint otherwise | |
1512 | match any non-negative constraint. */ | |
1513 | ||
1514 | lang_output_section_statement_type * | |
1515 | next_matching_output_section_statement (lang_output_section_statement_type *os, | |
1516 | int constraint) | |
1517 | { | |
1518 | /* All output_section_statements are actually part of a | |
1519 | struct out_section_hash_entry. */ | |
1520 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) | |
1521 | ((char *) os | |
1522 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); | |
1523 | const char *name = os->name; | |
1524 | ||
1525 | ASSERT (name == entry->root.string); | |
1526 | do | |
1527 | { | |
1528 | entry = (struct out_section_hash_entry *) entry->root.next; | |
1529 | if (entry == NULL | |
1530 | || name != entry->s.output_section_statement.name) | |
1531 | return NULL; | |
1532 | } | |
1533 | while (constraint != entry->s.output_section_statement.constraint | |
1534 | && (constraint != 0 | |
1535 | || entry->s.output_section_statement.constraint < 0)); | |
1536 | ||
1537 | return &entry->s.output_section_statement; | |
1538 | } | |
1539 | ||
afd7a018 AM |
1540 | /* A variant of lang_output_section_find used by place_orphan. |
1541 | Returns the output statement that should precede a new output | |
1542 | statement for SEC. If an exact match is found on certain flags, | |
1543 | sets *EXACT too. */ | |
1544 | ||
1545 | lang_output_section_statement_type * | |
1546 | lang_output_section_find_by_flags (const asection *sec, | |
93638471 | 1547 | flagword sec_flags, |
390fbbf1 AM |
1548 | lang_output_section_statement_type **exact, |
1549 | lang_match_sec_type_func match_type) | |
afd7a018 AM |
1550 | { |
1551 | lang_output_section_statement_type *first, *look, *found; | |
93638471 | 1552 | flagword look_flags, differ; |
afd7a018 AM |
1553 | |
1554 | /* We know the first statement on this list is *ABS*. May as well | |
1555 | skip it. */ | |
8ce18f9c | 1556 | first = (void *) lang_os_list.head; |
afd7a018 AM |
1557 | first = first->next; |
1558 | ||
1559 | /* First try for an exact match. */ | |
1560 | found = NULL; | |
1561 | for (look = first; look; look = look->next) | |
1562 | { | |
d9d94ac8 | 1563 | look_flags = look->flags; |
afd7a018 | 1564 | if (look->bfd_section != NULL) |
ecca9871 | 1565 | { |
d9d94ac8 | 1566 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1567 | if (match_type && !match_type (link_info.output_bfd, |
1568 | look->bfd_section, | |
390fbbf1 | 1569 | sec->owner, sec)) |
ecca9871 L |
1570 | continue; |
1571 | } | |
d9d94ac8 AM |
1572 | differ = look_flags ^ sec_flags; |
1573 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
1574 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1575 | found = look; |
1576 | } | |
1577 | if (found != NULL) | |
1578 | { | |
390fbbf1 AM |
1579 | if (exact != NULL) |
1580 | *exact = found; | |
afd7a018 AM |
1581 | return found; |
1582 | } | |
1583 | ||
d9d94ac8 AM |
1584 | if ((sec_flags & SEC_CODE) != 0 |
1585 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1586 | { |
1587 | /* Try for a rw code section. */ | |
1588 | for (look = first; look; look = look->next) | |
1589 | { | |
d9d94ac8 | 1590 | look_flags = look->flags; |
afd7a018 | 1591 | if (look->bfd_section != NULL) |
ecca9871 | 1592 | { |
d9d94ac8 | 1593 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1594 | if (match_type && !match_type (link_info.output_bfd, |
1595 | look->bfd_section, | |
390fbbf1 | 1596 | sec->owner, sec)) |
ecca9871 L |
1597 | continue; |
1598 | } | |
d9d94ac8 AM |
1599 | differ = look_flags ^ sec_flags; |
1600 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1601 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1602 | found = look; |
1603 | } | |
afd7a018 | 1604 | } |
d9d94ac8 AM |
1605 | else if ((sec_flags & SEC_READONLY) != 0 |
1606 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1607 | { |
1608 | /* .rodata can go after .text, .sdata2 after .rodata. */ | |
1609 | for (look = first; look; look = look->next) | |
1610 | { | |
d9d94ac8 | 1611 | look_flags = look->flags; |
afd7a018 | 1612 | if (look->bfd_section != NULL) |
ecca9871 | 1613 | { |
d9d94ac8 | 1614 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1615 | if (match_type && !match_type (link_info.output_bfd, |
1616 | look->bfd_section, | |
390fbbf1 | 1617 | sec->owner, sec)) |
ecca9871 L |
1618 | continue; |
1619 | } | |
d9d94ac8 AM |
1620 | differ = look_flags ^ sec_flags; |
1621 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1622 | | SEC_READONLY | SEC_SMALL_DATA)) | |
1623 | || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1624 | | SEC_READONLY)) | |
1625 | && !(look_flags & SEC_SMALL_DATA))) | |
afd7a018 AM |
1626 | found = look; |
1627 | } | |
afd7a018 | 1628 | } |
d9d94ac8 AM |
1629 | else if ((sec_flags & SEC_THREAD_LOCAL) != 0 |
1630 | && (sec_flags & SEC_ALLOC) != 0) | |
1631 | { | |
1632 | /* .tdata can go after .data, .tbss after .tdata. Treat .tbss | |
1633 | as if it were a loaded section, and don't use match_type. */ | |
f38a2680 | 1634 | bool seen_thread_local = false; |
d9d94ac8 AM |
1635 | |
1636 | match_type = NULL; | |
1637 | for (look = first; look; look = look->next) | |
1638 | { | |
1639 | look_flags = look->flags; | |
1640 | if (look->bfd_section != NULL) | |
1641 | look_flags = look->bfd_section->flags; | |
1642 | ||
1643 | differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); | |
1644 | if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) | |
1645 | { | |
1646 | /* .tdata and .tbss must be adjacent and in that order. */ | |
1647 | if (!(look_flags & SEC_LOAD) | |
1648 | && (sec_flags & SEC_LOAD)) | |
1649 | /* ..so if we're at a .tbss section and we're placing | |
1650 | a .tdata section stop looking and return the | |
1651 | previous section. */ | |
1652 | break; | |
1653 | found = look; | |
f38a2680 | 1654 | seen_thread_local = true; |
d9d94ac8 AM |
1655 | } |
1656 | else if (seen_thread_local) | |
1657 | break; | |
1658 | else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) | |
1659 | found = look; | |
1660 | } | |
1661 | } | |
1662 | else if ((sec_flags & SEC_SMALL_DATA) != 0 | |
1663 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1664 | { |
1665 | /* .sdata goes after .data, .sbss after .sdata. */ | |
1666 | for (look = first; look; look = look->next) | |
1667 | { | |
d9d94ac8 | 1668 | look_flags = look->flags; |
afd7a018 | 1669 | if (look->bfd_section != NULL) |
ecca9871 | 1670 | { |
d9d94ac8 | 1671 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1672 | if (match_type && !match_type (link_info.output_bfd, |
1673 | look->bfd_section, | |
390fbbf1 | 1674 | sec->owner, sec)) |
ecca9871 L |
1675 | continue; |
1676 | } | |
d9d94ac8 AM |
1677 | differ = look_flags ^ sec_flags; |
1678 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1679 | | SEC_THREAD_LOCAL)) | |
1680 | || ((look_flags & SEC_SMALL_DATA) | |
1681 | && !(sec_flags & SEC_HAS_CONTENTS))) | |
afd7a018 AM |
1682 | found = look; |
1683 | } | |
afd7a018 | 1684 | } |
d9d94ac8 AM |
1685 | else if ((sec_flags & SEC_HAS_CONTENTS) != 0 |
1686 | && (sec_flags & SEC_ALLOC) != 0) | |
afd7a018 AM |
1687 | { |
1688 | /* .data goes after .rodata. */ | |
1689 | for (look = first; look; look = look->next) | |
1690 | { | |
d9d94ac8 | 1691 | look_flags = look->flags; |
afd7a018 | 1692 | if (look->bfd_section != NULL) |
ecca9871 | 1693 | { |
d9d94ac8 | 1694 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1695 | if (match_type && !match_type (link_info.output_bfd, |
1696 | look->bfd_section, | |
390fbbf1 | 1697 | sec->owner, sec)) |
ecca9871 L |
1698 | continue; |
1699 | } | |
d9d94ac8 AM |
1700 | differ = look_flags ^ sec_flags; |
1701 | if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | |
1702 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) | |
afd7a018 AM |
1703 | found = look; |
1704 | } | |
afd7a018 | 1705 | } |
d9d94ac8 | 1706 | else if ((sec_flags & SEC_ALLOC) != 0) |
afd7a018 | 1707 | { |
07890c07 | 1708 | /* .bss goes after any other alloc section. */ |
390fbbf1 | 1709 | for (look = first; look; look = look->next) |
ecca9871 | 1710 | { |
d9d94ac8 | 1711 | look_flags = look->flags; |
390fbbf1 AM |
1712 | if (look->bfd_section != NULL) |
1713 | { | |
d9d94ac8 | 1714 | look_flags = look->bfd_section->flags; |
f13a99db AM |
1715 | if (match_type && !match_type (link_info.output_bfd, |
1716 | look->bfd_section, | |
390fbbf1 AM |
1717 | sec->owner, sec)) |
1718 | continue; | |
1719 | } | |
d9d94ac8 AM |
1720 | differ = look_flags ^ sec_flags; |
1721 | if (!(differ & SEC_ALLOC)) | |
390fbbf1 | 1722 | found = look; |
ecca9871 | 1723 | } |
afd7a018 | 1724 | } |
07890c07 AM |
1725 | else |
1726 | { | |
1727 | /* non-alloc go last. */ | |
1728 | for (look = first; look; look = look->next) | |
1729 | { | |
d9d94ac8 | 1730 | look_flags = look->flags; |
07890c07 | 1731 | if (look->bfd_section != NULL) |
d9d94ac8 AM |
1732 | look_flags = look->bfd_section->flags; |
1733 | differ = look_flags ^ sec_flags; | |
1734 | if (!(differ & SEC_DEBUGGING)) | |
07890c07 AM |
1735 | found = look; |
1736 | } | |
1737 | return found; | |
1738 | } | |
afd7a018 | 1739 | |
390fbbf1 AM |
1740 | if (found || !match_type) |
1741 | return found; | |
1742 | ||
93638471 | 1743 | return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); |
afd7a018 AM |
1744 | } |
1745 | ||
1746 | /* Find the last output section before given output statement. | |
1747 | Used by place_orphan. */ | |
1748 | ||
1749 | static asection * | |
1750 | output_prev_sec_find (lang_output_section_statement_type *os) | |
1751 | { | |
afd7a018 AM |
1752 | lang_output_section_statement_type *lookup; |
1753 | ||
218868ba | 1754 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
afd7a018 | 1755 | { |
66c103b7 | 1756 | if (lookup->constraint < 0) |
afd7a018 | 1757 | continue; |
afd7a018 AM |
1758 | |
1759 | if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) | |
218868ba | 1760 | return lookup->bfd_section; |
afd7a018 AM |
1761 | } |
1762 | ||
1763 | return NULL; | |
1764 | } | |
1765 | ||
53d25da6 AM |
1766 | /* Look for a suitable place for a new output section statement. The |
1767 | idea is to skip over anything that might be inside a SECTIONS {} | |
1768 | statement in a script, before we find another output section | |
1769 | statement. Assignments to "dot" before an output section statement | |
0fa4d7cf AM |
1770 | are assumed to belong to it, except in two cases; The first |
1771 | assignment to dot, and assignments before non-alloc sections. | |
1772 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or | |
1773 | similar assignments that set the initial address, or we might | |
1774 | insert non-alloc note sections among assignments setting end of | |
1775 | image symbols. */ | |
53d25da6 AM |
1776 | |
1777 | static lang_statement_union_type ** | |
1778 | insert_os_after (lang_output_section_statement_type *after) | |
1779 | { | |
1780 | lang_statement_union_type **where; | |
1781 | lang_statement_union_type **assign = NULL; | |
f38a2680 | 1782 | bool ignore_first; |
53d25da6 | 1783 | |
8ce18f9c | 1784 | ignore_first = after == (void *) lang_os_list.head; |
53d25da6 AM |
1785 | |
1786 | for (where = &after->header.next; | |
1787 | *where != NULL; | |
1788 | where = &(*where)->header.next) | |
1789 | { | |
1790 | switch ((*where)->header.type) | |
1791 | { | |
1792 | case lang_assignment_statement_enum: | |
1793 | if (assign == NULL) | |
1794 | { | |
1795 | lang_assignment_statement_type *ass; | |
1796 | ||
1797 | ass = &(*where)->assignment_statement; | |
1798 | if (ass->exp->type.node_class != etree_assert | |
1799 | && ass->exp->assign.dst[0] == '.' | |
73589426 AM |
1800 | && ass->exp->assign.dst[1] == 0) |
1801 | { | |
1802 | if (!ignore_first) | |
1803 | assign = where; | |
f38a2680 | 1804 | ignore_first = false; |
73589426 | 1805 | } |
53d25da6 | 1806 | } |
53d25da6 AM |
1807 | continue; |
1808 | case lang_wild_statement_enum: | |
1809 | case lang_input_section_enum: | |
1810 | case lang_object_symbols_statement_enum: | |
1811 | case lang_fill_statement_enum: | |
1812 | case lang_data_statement_enum: | |
1813 | case lang_reloc_statement_enum: | |
1814 | case lang_padding_statement_enum: | |
1815 | case lang_constructors_statement_enum: | |
1816 | assign = NULL; | |
f38a2680 | 1817 | ignore_first = false; |
53d25da6 AM |
1818 | continue; |
1819 | case lang_output_section_statement_enum: | |
1820 | if (assign != NULL) | |
0fa4d7cf AM |
1821 | { |
1822 | asection *s = (*where)->output_section_statement.bfd_section; | |
1823 | ||
249b2a84 L |
1824 | if (s == NULL |
1825 | || s->map_head.s == NULL | |
1826 | || (s->flags & SEC_ALLOC) != 0) | |
0fa4d7cf AM |
1827 | where = assign; |
1828 | } | |
53d25da6 AM |
1829 | break; |
1830 | case lang_input_statement_enum: | |
1831 | case lang_address_statement_enum: | |
1832 | case lang_target_statement_enum: | |
1833 | case lang_output_statement_enum: | |
1834 | case lang_group_statement_enum: | |
1835 | case lang_insert_statement_enum: | |
1836 | continue; | |
43ae96e9 MM |
1837 | case lang_input_matcher_enum: |
1838 | FAIL (); | |
53d25da6 AM |
1839 | } |
1840 | break; | |
1841 | } | |
1842 | ||
1843 | return where; | |
1844 | } | |
1845 | ||
afd7a018 | 1846 | lang_output_section_statement_type * |
7b986e99 | 1847 | lang_insert_orphan (asection *s, |
afd7a018 | 1848 | const char *secname, |
8a99a385 | 1849 | int constraint, |
afd7a018 AM |
1850 | lang_output_section_statement_type *after, |
1851 | struct orphan_save *place, | |
1852 | etree_type *address, | |
1853 | lang_statement_list_type *add_child) | |
1854 | { | |
afd7a018 | 1855 | lang_statement_list_type add; |
afd7a018 AM |
1856 | lang_output_section_statement_type *os; |
1857 | lang_output_section_statement_type **os_tail; | |
1858 | ||
afd7a018 AM |
1859 | /* If we have found an appropriate place for the output section |
1860 | statements for this orphan, add them to our own private list, | |
1861 | inserting them later into the global statement list. */ | |
1862 | if (after != NULL) | |
1863 | { | |
bde18da4 AM |
1864 | lang_list_init (&add); |
1865 | push_stat_ptr (&add); | |
afd7a018 AM |
1866 | } |
1867 | ||
0e1862bb L |
1868 | if (bfd_link_relocatable (&link_info) |
1869 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) | |
011aa75f AM |
1870 | address = exp_intop (0); |
1871 | ||
5c1e6d53 | 1872 | os_tail = (lang_output_section_statement_type **) lang_os_list.tail; |
c212f39d FS |
1873 | os = lang_enter_output_section_statement ( |
1874 | secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); | |
011aa75f | 1875 | |
afd7a018 AM |
1876 | if (add_child == NULL) |
1877 | add_child = &os->children; | |
b209b5a6 | 1878 | lang_add_section (add_child, s, NULL, NULL, os); |
afd7a018 | 1879 | |
f77c3948 DJ |
1880 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
1881 | { | |
1882 | const char *region = (after->region | |
1883 | ? after->region->name_list.name | |
1884 | : DEFAULT_MEMORY_REGION); | |
1885 | const char *lma_region = (after->lma_region | |
1886 | ? after->lma_region->name_list.name | |
1887 | : NULL); | |
1888 | lang_leave_output_section_statement (NULL, region, after->phdrs, | |
1889 | lma_region); | |
1890 | } | |
1891 | else | |
1892 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, | |
1893 | NULL); | |
afd7a018 | 1894 | |
afd7a018 AM |
1895 | /* Restore the global list pointer. */ |
1896 | if (after != NULL) | |
bde18da4 | 1897 | pop_stat_ptr (); |
afd7a018 AM |
1898 | |
1899 | if (after != NULL && os->bfd_section != NULL) | |
1900 | { | |
5daa8fe7 | 1901 | asection *snew, *as; |
f38a2680 AM |
1902 | bool place_after = place->stmt == NULL; |
1903 | bool insert_after = true; | |
afd7a018 AM |
1904 | |
1905 | snew = os->bfd_section; | |
1906 | ||
1907 | /* Shuffle the bfd section list to make the output file look | |
1908 | neater. This is really only cosmetic. */ | |
1909 | if (place->section == NULL | |
8ce18f9c | 1910 | && after != (void *) lang_os_list.head) |
afd7a018 AM |
1911 | { |
1912 | asection *bfd_section = after->bfd_section; | |
1913 | ||
1914 | /* If the output statement hasn't been used to place any input | |
1915 | sections (and thus doesn't have an output bfd_section), | |
1916 | look for the closest prior output statement having an | |
1917 | output section. */ | |
1918 | if (bfd_section == NULL) | |
1919 | bfd_section = output_prev_sec_find (after); | |
1920 | ||
1921 | if (bfd_section != NULL && bfd_section != snew) | |
1922 | place->section = &bfd_section->next; | |
1923 | } | |
1924 | ||
1925 | if (place->section == NULL) | |
f13a99db | 1926 | place->section = &link_info.output_bfd->sections; |
afd7a018 | 1927 | |
5daa8fe7 | 1928 | as = *place->section; |
5e542ba7 MS |
1929 | |
1930 | if (!as) | |
329c1c86 AM |
1931 | { |
1932 | /* Put the section at the end of the list. */ | |
5e542ba7 MS |
1933 | |
1934 | /* Unlink the section. */ | |
f13a99db | 1935 | bfd_section_list_remove (link_info.output_bfd, snew); |
5e542ba7 MS |
1936 | |
1937 | /* Now tack it back on in the right place. */ | |
f13a99db | 1938 | bfd_section_list_append (link_info.output_bfd, snew); |
329c1c86 | 1939 | } |
1887ae73 L |
1940 | else if ((bfd_get_flavour (link_info.output_bfd) |
1941 | == bfd_target_elf_flavour) | |
1942 | && (bfd_get_flavour (s->owner) | |
1943 | == bfd_target_elf_flavour) | |
1944 | && ((elf_section_type (s) == SHT_NOTE | |
1945 | && (s->flags & SEC_LOAD) != 0) | |
1946 | || (elf_section_type (as) == SHT_NOTE | |
1947 | && (as->flags & SEC_LOAD) != 0))) | |
1948 | { | |
1949 | /* Make sure that output note sections are grouped and sorted | |
1950 | by alignments when inserting a note section or insert a | |
1951 | section after a note section, */ | |
1952 | asection *sec; | |
1953 | /* A specific section after which the output note section | |
1954 | should be placed. */ | |
1955 | asection *after_sec; | |
1956 | /* True if we need to insert the orphan section after a | |
1957 | specific section to maintain output note section order. */ | |
f38a2680 | 1958 | bool after_sec_note = false; |
f3e660db L |
1959 | |
1960 | static asection *first_orphan_note = NULL; | |
1887ae73 L |
1961 | |
1962 | /* Group and sort output note section by alignments in | |
1963 | ascending order. */ | |
1964 | after_sec = NULL; | |
1965 | if (elf_section_type (s) == SHT_NOTE | |
1966 | && (s->flags & SEC_LOAD) != 0) | |
1967 | { | |
f3e660db L |
1968 | /* Search from the beginning for the last output note |
1969 | section with equal or larger alignments. NB: Don't | |
1970 | place orphan note section after non-note sections. */ | |
1887ae73 | 1971 | |
f3e660db L |
1972 | first_orphan_note = NULL; |
1973 | for (sec = link_info.output_bfd->sections; | |
1887ae73 L |
1974 | (sec != NULL |
1975 | && !bfd_is_abs_section (sec)); | |
1976 | sec = sec->next) | |
1977 | if (sec != snew | |
1978 | && elf_section_type (sec) == SHT_NOTE | |
1979 | && (sec->flags & SEC_LOAD) != 0) | |
1980 | { | |
f3e660db L |
1981 | if (!first_orphan_note) |
1982 | first_orphan_note = sec; | |
1887ae73 L |
1983 | if (sec->alignment_power >= s->alignment_power) |
1984 | after_sec = sec; | |
1985 | } | |
f3e660db L |
1986 | else if (first_orphan_note) |
1987 | { | |
1988 | /* Stop if there is non-note section after the first | |
1989 | orphan note section. */ | |
1990 | break; | |
1991 | } | |
1887ae73 | 1992 | |
f3e660db L |
1993 | /* If this will be the first orphan note section, it can |
1994 | be placed at the default location. */ | |
1995 | after_sec_note = first_orphan_note != NULL; | |
1996 | if (after_sec == NULL && after_sec_note) | |
1887ae73 | 1997 | { |
f3e660db L |
1998 | /* If all output note sections have smaller |
1999 | alignments, place the section before all | |
2000 | output orphan note sections. */ | |
2001 | after_sec = first_orphan_note; | |
f38a2680 | 2002 | insert_after = false; |
1887ae73 L |
2003 | } |
2004 | } | |
f3e660db | 2005 | else if (first_orphan_note) |
1887ae73 | 2006 | { |
f3e660db L |
2007 | /* Don't place non-note sections in the middle of orphan |
2008 | note sections. */ | |
f38a2680 | 2009 | after_sec_note = true; |
1887ae73 L |
2010 | after_sec = as; |
2011 | for (sec = as->next; | |
2012 | (sec != NULL | |
2013 | && !bfd_is_abs_section (sec)); | |
2014 | sec = sec->next) | |
2015 | if (elf_section_type (sec) == SHT_NOTE | |
2016 | && (sec->flags & SEC_LOAD) != 0) | |
2017 | after_sec = sec; | |
2018 | } | |
2019 | ||
2020 | if (after_sec_note) | |
2021 | { | |
2022 | if (after_sec) | |
2023 | { | |
f3e660db L |
2024 | /* Search forward to insert OS after AFTER_SEC output |
2025 | statement. */ | |
2026 | lang_output_section_statement_type *stmt, *next; | |
f38a2680 | 2027 | bool found = false; |
f3e660db L |
2028 | for (stmt = after; stmt != NULL; stmt = next) |
2029 | { | |
2030 | next = stmt->next; | |
2031 | if (insert_after) | |
2032 | { | |
2033 | if (stmt->bfd_section == after_sec) | |
2034 | { | |
f38a2680 AM |
2035 | place_after = true; |
2036 | found = true; | |
f3e660db L |
2037 | after = stmt; |
2038 | break; | |
2039 | } | |
2040 | } | |
2041 | else | |
2042 | { | |
2043 | /* If INSERT_AFTER is FALSE, place OS before | |
2044 | AFTER_SEC output statement. */ | |
2045 | if (next && next->bfd_section == after_sec) | |
2046 | { | |
f38a2680 AM |
2047 | place_after = true; |
2048 | found = true; | |
f3e660db L |
2049 | after = stmt; |
2050 | break; | |
2051 | } | |
2052 | } | |
2053 | } | |
2054 | ||
2055 | /* Search backward to insert OS after AFTER_SEC output | |
2056 | statement. */ | |
2057 | if (!found) | |
2058 | for (stmt = after; stmt != NULL; stmt = stmt->prev) | |
1887ae73 | 2059 | { |
f3e660db L |
2060 | if (insert_after) |
2061 | { | |
2062 | if (stmt->bfd_section == after_sec) | |
2063 | { | |
f38a2680 | 2064 | place_after = true; |
f3e660db L |
2065 | after = stmt; |
2066 | break; | |
2067 | } | |
2068 | } | |
2069 | else | |
2070 | { | |
2071 | /* If INSERT_AFTER is FALSE, place OS before | |
2072 | AFTER_SEC output statement. */ | |
2073 | if (stmt->next->bfd_section == after_sec) | |
2074 | { | |
f38a2680 | 2075 | place_after = true; |
f3e660db L |
2076 | after = stmt; |
2077 | break; | |
2078 | } | |
2079 | } | |
1887ae73 L |
2080 | } |
2081 | } | |
2082 | ||
f3e660db L |
2083 | if (after_sec == NULL |
2084 | || (insert_after && after_sec->next != snew) | |
2085 | || (!insert_after && after_sec->prev != snew)) | |
1887ae73 L |
2086 | { |
2087 | /* Unlink the section. */ | |
2088 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2089 | ||
2090 | /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, | |
2091 | prepend SNEW. */ | |
2092 | if (after_sec) | |
f3e660db L |
2093 | { |
2094 | if (insert_after) | |
2095 | bfd_section_list_insert_after (link_info.output_bfd, | |
2096 | after_sec, snew); | |
2097 | else | |
2098 | bfd_section_list_insert_before (link_info.output_bfd, | |
2099 | after_sec, snew); | |
2100 | } | |
1887ae73 L |
2101 | else |
2102 | bfd_section_list_prepend (link_info.output_bfd, snew); | |
2103 | } | |
2104 | } | |
2105 | else if (as != snew && as->prev != snew) | |
2106 | { | |
2107 | /* Unlink the section. */ | |
2108 | bfd_section_list_remove (link_info.output_bfd, snew); | |
2109 | ||
2110 | /* Now tack it back on in the right place. */ | |
2111 | bfd_section_list_insert_before (link_info.output_bfd, | |
2112 | as, snew); | |
2113 | } | |
2114 | } | |
5e542ba7 | 2115 | else if (as != snew && as->prev != snew) |
5daa8fe7 L |
2116 | { |
2117 | /* Unlink the section. */ | |
f13a99db | 2118 | bfd_section_list_remove (link_info.output_bfd, snew); |
afd7a018 | 2119 | |
5daa8fe7 | 2120 | /* Now tack it back on in the right place. */ |
f13a99db | 2121 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
5daa8fe7 | 2122 | } |
afd7a018 AM |
2123 | |
2124 | /* Save the end of this list. Further ophans of this type will | |
2125 | follow the one we've just added. */ | |
2126 | place->section = &snew->next; | |
2127 | ||
2128 | /* The following is non-cosmetic. We try to put the output | |
2129 | statements in some sort of reasonable order here, because they | |
2130 | determine the final load addresses of the orphan sections. | |
2131 | In addition, placing output statements in the wrong order may | |
2132 | require extra segments. For instance, given a typical | |
2133 | situation of all read-only sections placed in one segment and | |
2134 | following that a segment containing all the read-write | |
2135 | sections, we wouldn't want to place an orphan read/write | |
2136 | section before or amongst the read-only ones. */ | |
2137 | if (add.head != NULL) | |
2138 | { | |
2139 | lang_output_section_statement_type *newly_added_os; | |
2140 | ||
1887ae73 L |
2141 | /* Place OS after AFTER if AFTER_NOTE is TRUE. */ |
2142 | if (place_after) | |
afd7a018 | 2143 | { |
53d25da6 | 2144 | lang_statement_union_type **where = insert_os_after (after); |
afd7a018 AM |
2145 | |
2146 | *add.tail = *where; | |
2147 | *where = add.head; | |
2148 | ||
2149 | place->os_tail = &after->next; | |
2150 | } | |
2151 | else | |
2152 | { | |
2153 | /* Put it after the last orphan statement we added. */ | |
2154 | *add.tail = *place->stmt; | |
2155 | *place->stmt = add.head; | |
2156 | } | |
2157 | ||
2158 | /* Fix the global list pointer if we happened to tack our | |
2159 | new list at the tail. */ | |
bde18da4 AM |
2160 | if (*stat_ptr->tail == add.head) |
2161 | stat_ptr->tail = add.tail; | |
afd7a018 AM |
2162 | |
2163 | /* Save the end of this list. */ | |
2164 | place->stmt = add.tail; | |
2165 | ||
2166 | /* Do the same for the list of output section statements. */ | |
2167 | newly_added_os = *os_tail; | |
2168 | *os_tail = NULL; | |
218868ba AM |
2169 | newly_added_os->prev = (lang_output_section_statement_type *) |
2170 | ((char *) place->os_tail | |
2171 | - offsetof (lang_output_section_statement_type, next)); | |
afd7a018 | 2172 | newly_added_os->next = *place->os_tail; |
218868ba AM |
2173 | if (newly_added_os->next != NULL) |
2174 | newly_added_os->next->prev = newly_added_os; | |
afd7a018 AM |
2175 | *place->os_tail = newly_added_os; |
2176 | place->os_tail = &newly_added_os->next; | |
2177 | ||
2178 | /* Fixing the global list pointer here is a little different. | |
2179 | We added to the list in lang_enter_output_section_statement, | |
2180 | trimmed off the new output_section_statment above when | |
2181 | assigning *os_tail = NULL, but possibly added it back in | |
2182 | the same place when assigning *place->os_tail. */ | |
2183 | if (*os_tail == NULL) | |
5c1e6d53 | 2184 | lang_os_list.tail = (lang_statement_union_type **) os_tail; |
afd7a018 AM |
2185 | } |
2186 | } | |
2187 | return os; | |
2188 | } | |
2189 | ||
16e4ecc0 AM |
2190 | static void |
2191 | lang_print_asneeded (void) | |
2192 | { | |
2193 | struct asneeded_minfo *m; | |
16e4ecc0 AM |
2194 | |
2195 | if (asneeded_list_head == NULL) | |
2196 | return; | |
2197 | ||
1273da04 | 2198 | minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); |
16e4ecc0 AM |
2199 | |
2200 | for (m = asneeded_list_head; m != NULL; m = m->next) | |
2201 | { | |
6b9bd54c | 2202 | int len; |
16e4ecc0 AM |
2203 | |
2204 | minfo ("%s", m->soname); | |
2205 | len = strlen (m->soname); | |
2206 | ||
2207 | if (len >= 29) | |
2208 | { | |
2209 | print_nl (); | |
2210 | len = 0; | |
2211 | } | |
6b9bd54c | 2212 | print_spaces (30 - len); |
16e4ecc0 AM |
2213 | |
2214 | if (m->ref != NULL) | |
871b3ab2 | 2215 | minfo ("%pB ", m->ref); |
c1c8c1ef | 2216 | minfo ("(%pT)\n", m->name); |
16e4ecc0 AM |
2217 | } |
2218 | } | |
2219 | ||
252b5132 | 2220 | static void |
1579bae1 | 2221 | lang_map_flags (flagword flag) |
252b5132 RH |
2222 | { |
2223 | if (flag & SEC_ALLOC) | |
2224 | minfo ("a"); | |
2225 | ||
2226 | if (flag & SEC_CODE) | |
2227 | minfo ("x"); | |
2228 | ||
2229 | if (flag & SEC_READONLY) | |
2230 | minfo ("r"); | |
2231 | ||
2232 | if (flag & SEC_DATA) | |
2233 | minfo ("w"); | |
2234 | ||
2235 | if (flag & SEC_LOAD) | |
2236 | minfo ("l"); | |
2237 | } | |
2238 | ||
2239 | void | |
1579bae1 | 2240 | lang_map (void) |
252b5132 RH |
2241 | { |
2242 | lang_memory_region_type *m; | |
f38a2680 | 2243 | bool dis_header_printed = false; |
252b5132 | 2244 | |
4d4920ec EB |
2245 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
2246 | { | |
2247 | asection *s; | |
2248 | ||
2249 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 | |
66be1055 | 2250 | || file->flags.just_syms) |
4d4920ec EB |
2251 | continue; |
2252 | ||
035801ce FS |
2253 | if (config.print_map_discarded) |
2254 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
2255 | if ((s->output_section == NULL | |
2256 | || s->output_section->owner != link_info.output_bfd) | |
2257 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) | |
2258 | { | |
2259 | if (! dis_header_printed) | |
2260 | { | |
2261 | fprintf (config.map_file, _("\nDiscarded input sections\n\n")); | |
f38a2680 | 2262 | dis_header_printed = true; |
035801ce | 2263 | } |
4d4920ec | 2264 | |
f38a2680 | 2265 | print_input_section (s, true); |
035801ce | 2266 | } |
4d4920ec EB |
2267 | } |
2268 | ||
252b5132 RH |
2269 | minfo (_("\nMemory Configuration\n\n")); |
2270 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
2271 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
2272 | ||
1579bae1 | 2273 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 | 2274 | { |
6b9bd54c | 2275 | fprintf (config.map_file, "%-16s", m->name_list.name); |
252b5132 | 2276 | |
6b9bd54c AM |
2277 | char buf[32]; |
2278 | bfd_sprintf_vma (link_info.output_bfd, buf, m->origin); | |
2279 | fprintf (config.map_file, " 0x%-16s", buf); | |
2280 | bfd_sprintf_vma (link_info.output_bfd, buf, m->length); | |
2281 | fprintf (config.map_file, | |
2282 | " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf); | |
2283 | if (m->flags) | |
2284 | lang_map_flags (m->flags); | |
252b5132 | 2285 | |
6b9bd54c AM |
2286 | if (m->not_flags) |
2287 | { | |
2288 | minfo ("!"); | |
2289 | lang_map_flags (m->not_flags); | |
252b5132 RH |
2290 | } |
2291 | ||
2292 | print_nl (); | |
2293 | } | |
2294 | ||
2295 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
2296 | ||
0aa7f586 | 2297 | if (!link_info.reduce_memory_overheads) |
35835446 JR |
2298 | { |
2299 | obstack_begin (&map_obstack, 1000); | |
35835446 JR |
2300 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
2301 | } | |
6a846243 | 2302 | expld.phase = lang_fixed_phase_enum; |
fa72205c | 2303 | lang_statement_iteration++; |
252b5132 | 2304 | print_statements (); |
7a2f2d82 | 2305 | |
0aa7f586 AM |
2306 | ldemul_extra_map_file_text (link_info.output_bfd, &link_info, |
2307 | config.map_file); | |
252b5132 RH |
2308 | } |
2309 | ||
f38a2680 | 2310 | static bool |
967928e9 AM |
2311 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
2312 | void *info ATTRIBUTE_UNUSED) | |
35835446 | 2313 | { |
6fcc66ab AM |
2314 | if ((hash_entry->type == bfd_link_hash_defined |
2315 | || hash_entry->type == bfd_link_hash_defweak) | |
2316 | && hash_entry->u.def.section->owner != link_info.output_bfd | |
2317 | && hash_entry->u.def.section->owner != NULL) | |
35835446 | 2318 | { |
6fcc66ab | 2319 | input_section_userdata_type *ud; |
35835446 JR |
2320 | struct map_symbol_def *def; |
2321 | ||
a48931cc | 2322 | ud = bfd_section_userdata (hash_entry->u.def.section); |
6fcc66ab | 2323 | if (!ud) |
35835446 | 2324 | { |
988de25b | 2325 | ud = stat_alloc (sizeof (*ud)); |
a48931cc | 2326 | bfd_set_section_userdata (hash_entry->u.def.section, ud); |
6fcc66ab AM |
2327 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
2328 | ud->map_symbol_def_count = 0; | |
35835446 | 2329 | } |
6fcc66ab | 2330 | else if (!ud->map_symbol_def_tail) |
35835446 | 2331 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
afd7a018 | 2332 | |
1e9cc1c2 | 2333 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 2334 | def->entry = hash_entry; |
76d7af2d | 2335 | *(ud->map_symbol_def_tail) = def; |
35835446 | 2336 | ud->map_symbol_def_tail = &def->next; |
02688209 | 2337 | ud->map_symbol_def_count++; |
35835446 | 2338 | } |
f38a2680 | 2339 | return true; |
35835446 JR |
2340 | } |
2341 | ||
252b5132 RH |
2342 | /* Initialize an output section. */ |
2343 | ||
2344 | static void | |
dfa7b0b8 | 2345 | init_os (lang_output_section_statement_type *s, flagword flags) |
252b5132 | 2346 | { |
252b5132 | 2347 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
df5f2391 | 2348 | einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 2349 | |
de34d428 | 2350 | if (!s->dup_output) |
8a99a385 | 2351 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); |
1579bae1 | 2352 | if (s->bfd_section == NULL) |
8a99a385 AM |
2353 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
2354 | s->name, flags); | |
1579bae1 | 2355 | if (s->bfd_section == NULL) |
252b5132 | 2356 | { |
df5f2391 | 2357 | einfo (_("%F%P: output format %s cannot represent section" |
0aa7f586 | 2358 | " called %s: %E\n"), |
f13a99db | 2359 | link_info.output_bfd->xvec->name, s->name); |
252b5132 RH |
2360 | } |
2361 | s->bfd_section->output_section = s->bfd_section; | |
252b5132 | 2362 | s->bfd_section->output_offset = 0; |
e0f6802f | 2363 | |
24ef1aa7 GM |
2364 | /* Set the userdata of the output section to the output section |
2365 | statement to avoid lookup. */ | |
a48931cc | 2366 | bfd_set_section_userdata (s->bfd_section, s); |
24ef1aa7 | 2367 | |
a431bc2e AM |
2368 | /* If there is a base address, make sure that any sections it might |
2369 | mention are initialized. */ | |
2370 | if (s->addr_tree != NULL) | |
2371 | exp_init_os (s->addr_tree); | |
2372 | ||
2373 | if (s->load_base != NULL) | |
2374 | exp_init_os (s->load_base); | |
2375 | ||
7270c5ed | 2376 | /* If supplied an alignment, set it. */ |
3d9c8f6b AM |
2377 | if (s->section_alignment != NULL) |
2378 | s->bfd_section->alignment_power = exp_get_power (s->section_alignment, | |
2379 | "section alignment"); | |
252b5132 RH |
2380 | } |
2381 | ||
2382 | /* Make sure that all output sections mentioned in an expression are | |
2383 | initialized. */ | |
2384 | ||
2385 | static void | |
1579bae1 | 2386 | exp_init_os (etree_type *exp) |
252b5132 RH |
2387 | { |
2388 | switch (exp->type.node_class) | |
2389 | { | |
2390 | case etree_assign: | |
9f4fb502 | 2391 | case etree_provide: |
eab62f2f | 2392 | case etree_provided: |
252b5132 RH |
2393 | exp_init_os (exp->assign.src); |
2394 | break; | |
2395 | ||
2396 | case etree_binary: | |
2397 | exp_init_os (exp->binary.lhs); | |
2398 | exp_init_os (exp->binary.rhs); | |
2399 | break; | |
2400 | ||
2401 | case etree_trinary: | |
2402 | exp_init_os (exp->trinary.cond); | |
2403 | exp_init_os (exp->trinary.lhs); | |
2404 | exp_init_os (exp->trinary.rhs); | |
2405 | break; | |
2406 | ||
b6ca8815 NS |
2407 | case etree_assert: |
2408 | exp_init_os (exp->assert_s.child); | |
2409 | break; | |
afd7a018 | 2410 | |
252b5132 RH |
2411 | case etree_unary: |
2412 | exp_init_os (exp->unary.child); | |
2413 | break; | |
2414 | ||
2415 | case etree_name: | |
2416 | switch (exp->type.node_code) | |
2417 | { | |
2418 | case ADDR: | |
2419 | case LOADADDR: | |
252b5132 RH |
2420 | { |
2421 | lang_output_section_statement_type *os; | |
2422 | ||
2423 | os = lang_output_section_find (exp->name.name); | |
2424 | if (os != NULL && os->bfd_section == NULL) | |
dfa7b0b8 | 2425 | init_os (os, 0); |
252b5132 RH |
2426 | } |
2427 | } | |
2428 | break; | |
2429 | ||
2430 | default: | |
2431 | break; | |
2432 | } | |
2433 | } | |
9503fd87 | 2434 | \f |
252b5132 | 2435 | static void |
1579bae1 | 2436 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 2437 | { |
1e9cc1c2 | 2438 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
252b5132 RH |
2439 | |
2440 | /* If we are only reading symbols from this object, then we want to | |
2441 | discard all sections. */ | |
66be1055 | 2442 | if (entry->flags.just_syms) |
252b5132 | 2443 | { |
1449d79b | 2444 | bfd_link_just_syms (abfd, sec, &link_info); |
252b5132 RH |
2445 | return; |
2446 | } | |
2447 | ||
2e84f9c1 AM |
2448 | /* Deal with SHF_EXCLUDE ELF sections. */ |
2449 | if (!bfd_link_relocatable (&link_info) | |
2450 | && (abfd->flags & BFD_PLUGIN) == 0 | |
2451 | && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2452 | sec->output_section = bfd_abs_section_ptr; | |
2453 | ||
ace66bb2 | 2454 | if (!(abfd->flags & DYNAMIC)) |
c77ec726 | 2455 | bfd_section_already_linked (abfd, sec, &link_info); |
252b5132 RH |
2456 | } |
2457 | \f | |
5b5f4e6f AB |
2458 | |
2459 | /* Returns true if SECTION is one we know will be discarded based on its | |
2460 | section flags, otherwise returns false. */ | |
2461 | ||
f38a2680 | 2462 | static bool |
5b5f4e6f AB |
2463 | lang_discard_section_p (asection *section) |
2464 | { | |
f38a2680 | 2465 | bool discard; |
5b5f4e6f AB |
2466 | flagword flags = section->flags; |
2467 | ||
2468 | /* Discard sections marked with SEC_EXCLUDE. */ | |
2469 | discard = (flags & SEC_EXCLUDE) != 0; | |
2470 | ||
2471 | /* Discard the group descriptor sections when we're finally placing the | |
2472 | sections from within the group. */ | |
2473 | if ((flags & SEC_GROUP) != 0 | |
2474 | && link_info.resolve_section_groups) | |
f38a2680 | 2475 | discard = true; |
5b5f4e6f AB |
2476 | |
2477 | /* Discard debugging sections if we are stripping debugging | |
2478 | information. */ | |
2479 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
2480 | && (flags & SEC_DEBUGGING) != 0) | |
f38a2680 | 2481 | discard = true; |
5b5f4e6f AB |
2482 | |
2483 | return discard; | |
2484 | } | |
2485 | ||
252b5132 RH |
2486 | /* The wild routines. |
2487 | ||
2488 | These expand statements like *(.text) and foo.o to a list of | |
2489 | explicit actions, like foo.o(.text), bar.o(.text) and | |
2490 | foo.o(.text, .data). */ | |
2491 | ||
252b5132 | 2492 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
b9c361e0 | 2493 | lang_input_section statement which is placed at PTR. */ |
252b5132 RH |
2494 | |
2495 | void | |
1579bae1 AM |
2496 | lang_add_section (lang_statement_list_type *ptr, |
2497 | asection *section, | |
b209b5a6 | 2498 | struct wildcard_list *pattern, |
b9c361e0 | 2499 | struct flag_info *sflag_info, |
7b986e99 | 2500 | lang_output_section_statement_type *output) |
252b5132 | 2501 | { |
164e712d | 2502 | flagword flags = section->flags; |
ae17ab41 | 2503 | |
f38a2680 | 2504 | bool discard; |
dfa7b0b8 | 2505 | lang_input_section_type *new_section; |
ae17ab41 | 2506 | bfd *abfd = link_info.output_bfd; |
252b5132 | 2507 | |
5b5f4e6f AB |
2508 | /* Is this section one we know should be discarded? */ |
2509 | discard = lang_discard_section_p (section); | |
252b5132 RH |
2510 | |
2511 | /* Discard input sections which are assigned to a section named | |
2512 | DISCARD_SECTION_NAME. */ | |
2513 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
f38a2680 | 2514 | discard = true; |
252b5132 | 2515 | |
252b5132 RH |
2516 | if (discard) |
2517 | { | |
2518 | if (section->output_section == NULL) | |
2519 | { | |
2520 | /* This prevents future calls from assigning this section. */ | |
2521 | section->output_section = bfd_abs_section_ptr; | |
2522 | } | |
abf874aa CL |
2523 | else if (link_info.non_contiguous_regions_warnings) |
2524 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " | |
2525 | "section `%pA' from '%pB' match /DISCARD/ clause.\n"), | |
2526 | NULL, section, section->owner); | |
2527 | ||
252b5132 RH |
2528 | return; |
2529 | } | |
2530 | ||
ae17ab41 CM |
2531 | if (sflag_info) |
2532 | { | |
f38a2680 | 2533 | bool keep; |
ae17ab41 | 2534 | |
b9c361e0 JL |
2535 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); |
2536 | if (!keep) | |
4724d37e | 2537 | return; |
ae17ab41 CM |
2538 | } |
2539 | ||
dfa7b0b8 | 2540 | if (section->output_section != NULL) |
abf874aa CL |
2541 | { |
2542 | if (!link_info.non_contiguous_regions) | |
2543 | return; | |
2544 | ||
2545 | /* SECTION has already been handled in a special way | |
2546 | (eg. LINK_ONCE): skip it. */ | |
2547 | if (bfd_is_abs_section (section->output_section)) | |
2548 | return; | |
2549 | ||
2550 | /* Already assigned to the same output section, do not process | |
2551 | it again, to avoid creating loops between duplicate sections | |
2552 | later. */ | |
2553 | if (section->output_section == output->bfd_section) | |
2554 | return; | |
2555 | ||
2556 | if (link_info.non_contiguous_regions_warnings && output->bfd_section) | |
2557 | einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " | |
2558 | "change behaviour for section `%pA' from '%pB' (assigned to " | |
2559 | "%pA, but additional match: %pA)\n"), | |
2560 | NULL, section, section->owner, section->output_section, | |
2561 | output->bfd_section); | |
2562 | ||
2563 | /* SECTION has already been assigned to an output section, but | |
2564 | the user allows it to be mapped to another one in case it | |
2565 | overflows. We'll later update the actual output section in | |
2566 | size_input_section as appropriate. */ | |
2567 | } | |
252b5132 | 2568 | |
dfa7b0b8 AM |
2569 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
2570 | to an output section, because we want to be able to include a | |
2571 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
2572 | section (I don't know why we want to do this, but we do). | |
2573 | build_link_order in ldwrite.c handles this case by turning | |
2574 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
2575 | flags &= ~ SEC_NEVER_LOAD; | |
8423293d | 2576 | |
dfa7b0b8 AM |
2577 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
2578 | already been processed. One reason to do this is that on pe | |
2579 | format targets, .text$foo sections go into .text and it's odd | |
2580 | to see .text with SEC_LINK_ONCE set. */ | |
7bdf4127 AB |
2581 | if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) |
2582 | { | |
2583 | if (link_info.resolve_section_groups) | |
6c19b93b | 2584 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 | 2585 | else |
6c19b93b | 2586 | flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); |
7bdf4127 AB |
2587 | } |
2588 | else if (!bfd_link_relocatable (&link_info)) | |
0814be7d | 2589 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
252b5132 | 2590 | |
dfa7b0b8 AM |
2591 | switch (output->sectype) |
2592 | { | |
2593 | case normal_section: | |
2594 | case overlay_section: | |
7b243801 | 2595 | case first_overlay_section: |
c212f39d | 2596 | case type_section: |
dfa7b0b8 AM |
2597 | break; |
2598 | case noalloc_section: | |
2599 | flags &= ~SEC_ALLOC; | |
2600 | break; | |
c212f39d | 2601 | case typed_readonly_section: |
6b86da53 LB |
2602 | case readonly_section: |
2603 | flags |= SEC_READONLY; | |
2604 | break; | |
dfa7b0b8 AM |
2605 | case noload_section: |
2606 | flags &= ~SEC_LOAD; | |
2607 | flags |= SEC_NEVER_LOAD; | |
f4eaaf7f AM |
2608 | /* Unfortunately GNU ld has managed to evolve two different |
2609 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, | |
2610 | alloc, no contents section. All others get a noload, noalloc | |
2611 | section. */ | |
2612 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
2e76e85a | 2613 | flags &= ~SEC_HAS_CONTENTS; |
f4eaaf7f AM |
2614 | else |
2615 | flags &= ~SEC_ALLOC; | |
dfa7b0b8 AM |
2616 | break; |
2617 | } | |
252b5132 | 2618 | |
dfa7b0b8 AM |
2619 | if (output->bfd_section == NULL) |
2620 | init_os (output, flags); | |
252b5132 | 2621 | |
dfa7b0b8 AM |
2622 | /* If SEC_READONLY is not set in the input section, then clear |
2623 | it from the output section. */ | |
2624 | output->bfd_section->flags &= flags | ~SEC_READONLY; | |
252b5132 | 2625 | |
dfa7b0b8 AM |
2626 | if (output->bfd_section->linker_has_input) |
2627 | { | |
2628 | /* Only set SEC_READONLY flag on the first input section. */ | |
2629 | flags &= ~ SEC_READONLY; | |
252b5132 | 2630 | |
f5fa8ca2 | 2631 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
dfa7b0b8 AM |
2632 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
2633 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
2634 | || ((flags & SEC_MERGE) != 0 | |
2635 | && output->bfd_section->entsize != section->entsize)) | |
f5fa8ca2 | 2636 | { |
afd7a018 | 2637 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
f5fa8ca2 JJ |
2638 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
2639 | } | |
dfa7b0b8 AM |
2640 | } |
2641 | output->bfd_section->flags |= flags; | |
f5fa8ca2 | 2642 | |
dfa7b0b8 AM |
2643 | if (!output->bfd_section->linker_has_input) |
2644 | { | |
2645 | output->bfd_section->linker_has_input = 1; | |
2646 | /* This must happen after flags have been updated. The output | |
2647 | section may have been created before we saw its first input | |
2648 | section, eg. for a data statement. */ | |
2649 | bfd_init_private_section_data (section->owner, section, | |
2650 | link_info.output_bfd, | |
2651 | output->bfd_section, | |
2652 | &link_info); | |
2653 | if ((flags & SEC_MERGE) != 0) | |
afd7a018 | 2654 | output->bfd_section->entsize = section->entsize; |
dfa7b0b8 | 2655 | } |
f5fa8ca2 | 2656 | |
dfa7b0b8 AM |
2657 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
2658 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) | |
2659 | { | |
2660 | /* FIXME: This value should really be obtained from the bfd... */ | |
2661 | output->block_value = 128; | |
2662 | } | |
252b5132 | 2663 | |
b02db378 AM |
2664 | /* When a .ctors section is placed in .init_array it must be copied |
2665 | in reverse order. Similarly for .dtors. Set that up. */ | |
2666 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour | |
2667 | && ((startswith (section->name, ".ctors") | |
2668 | && strcmp (output->bfd_section->name, ".init_array") == 0) | |
2669 | || (startswith (section->name, ".dtors") | |
2670 | && strcmp (output->bfd_section->name, ".fini_array") == 0)) | |
2671 | && (section->name[6] == 0 || section->name[6] == '.')) | |
2672 | section->flags |= SEC_ELF_REVERSE_COPY; | |
2673 | ||
dfa7b0b8 AM |
2674 | if (section->alignment_power > output->bfd_section->alignment_power) |
2675 | output->bfd_section->alignment_power = section->alignment_power; | |
9e41f973 | 2676 | |
dfa7b0b8 | 2677 | section->output_section = output->bfd_section; |
252b5132 | 2678 | |
5b69e357 | 2679 | if (!map_head_is_link_order) |
dfa7b0b8 AM |
2680 | { |
2681 | asection *s = output->bfd_section->map_tail.s; | |
2682 | output->bfd_section->map_tail.s = section; | |
2683 | section->map_head.s = NULL; | |
2684 | section->map_tail.s = s; | |
2685 | if (s != NULL) | |
2686 | s->map_head.s = section; | |
2687 | else | |
2688 | output->bfd_section->map_head.s = section; | |
252b5132 | 2689 | } |
dfa7b0b8 AM |
2690 | |
2691 | /* Add a section reference to the list. */ | |
2692 | new_section = new_stat (lang_input_section, ptr); | |
2693 | new_section->section = section; | |
b209b5a6 | 2694 | new_section->pattern = pattern; |
252b5132 RH |
2695 | } |
2696 | ||
252b5132 | 2697 | /* Expand a wild statement for a particular FILE. SECTION may be |
af31506c MM |
2698 | NULL, in which case it is a wild card. This assumes that the |
2699 | wild statement doesn't need any sorting (of filenames or sections). */ | |
252b5132 RH |
2700 | |
2701 | static void | |
af31506c MM |
2702 | output_section_callback_nosort (lang_wild_statement_type *ptr, |
2703 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2704 | asection *section, | |
2705 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
2706 | void *output) | |
4dec4d4e | 2707 | { |
d0bf826b AM |
2708 | lang_output_section_statement_type *os; |
2709 | ||
2710 | os = (lang_output_section_statement_type *) output; | |
5f992e62 | 2711 | |
b6bf44ba | 2712 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2713 | if (unique_section_p (section, os)) |
b6bf44ba AM |
2714 | return; |
2715 | ||
af31506c MM |
2716 | lang_add_section (&ptr->children, section, ptr->section_list, |
2717 | ptr->section_flag_list, os); | |
252b5132 RH |
2718 | } |
2719 | ||
0841712e JJ |
2720 | /* Check if all sections in a wild statement for a particular FILE |
2721 | are readonly. */ | |
2722 | ||
2723 | static void | |
2724 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
2725 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
2726 | asection *section, | |
2727 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
d0bf826b | 2728 | void *output) |
0841712e | 2729 | { |
d0bf826b AM |
2730 | lang_output_section_statement_type *os; |
2731 | ||
2732 | os = (lang_output_section_statement_type *) output; | |
2733 | ||
0841712e | 2734 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0bf826b | 2735 | if (unique_section_p (section, os)) |
0841712e JJ |
2736 | return; |
2737 | ||
6feb9908 | 2738 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
f38a2680 | 2739 | os->all_input_readonly = false; |
0841712e JJ |
2740 | } |
2741 | ||
252b5132 RH |
2742 | /* This is passed a file name which must have been seen already and |
2743 | added to the statement tree. We will see if it has been opened | |
2744 | already and had its symbols read. If not then we'll read it. */ | |
2745 | ||
2746 | static lang_input_statement_type * | |
1579bae1 | 2747 | lookup_name (const char *name) |
252b5132 RH |
2748 | { |
2749 | lang_input_statement_type *search; | |
2750 | ||
8ce18f9c | 2751 | for (search = (void *) input_file_chain.head; |
1579bae1 | 2752 | search != NULL; |
36983a93 | 2753 | search = search->next_real_file) |
252b5132 | 2754 | { |
0013291d NC |
2755 | /* Use the local_sym_name as the name of the file that has |
2756 | already been loaded as filename might have been transformed | |
2757 | via the search directory lookup mechanism. */ | |
87aa7f19 | 2758 | const char *filename = search->local_sym_name; |
0013291d | 2759 | |
0013291d | 2760 | if (filename != NULL |
42627821 | 2761 | && filename_cmp (filename, name) == 0) |
252b5132 RH |
2762 | break; |
2763 | } | |
2764 | ||
1579bae1 | 2765 | if (search == NULL) |
1f1f5b92 AM |
2766 | { |
2767 | /* Arrange to splice the input statement added by new_afile into | |
2768 | statement_list after the current input_file_chain tail. | |
2769 | We know input_file_chain is not an empty list, and that | |
2770 | lookup_name was called via open_input_bfds. Later calls to | |
2771 | lookup_name should always match an existing input_statement. */ | |
2772 | lang_statement_union_type **tail = stat_ptr->tail; | |
2773 | lang_statement_union_type **after | |
2774 | = (void *) ((char *) input_file_chain.tail | |
2775 | - offsetof (lang_input_statement_type, next_real_file) | |
2776 | + offsetof (lang_input_statement_type, header.next)); | |
2777 | lang_statement_union_type *rest = *after; | |
2778 | stat_ptr->tail = after; | |
2779 | search = new_afile (name, lang_input_file_is_search_file_enum, | |
16171946 | 2780 | default_target, NULL); |
1f1f5b92 AM |
2781 | *stat_ptr->tail = rest; |
2782 | if (*tail == NULL) | |
2783 | stat_ptr->tail = tail; | |
2784 | } | |
252b5132 RH |
2785 | |
2786 | /* If we have already added this file, or this file is not real | |
87aa7f19 | 2787 | don't add this file. */ |
66be1055 | 2788 | if (search->flags.loaded || !search->flags.real) |
252b5132 RH |
2789 | return search; |
2790 | ||
0aa7f586 | 2791 | if (!load_symbols (search, NULL)) |
6770ec8c | 2792 | return NULL; |
252b5132 RH |
2793 | |
2794 | return search; | |
2795 | } | |
2796 | ||
b58f81ae DJ |
2797 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
2798 | ||
2799 | struct excluded_lib | |
2800 | { | |
2801 | char *name; | |
2802 | struct excluded_lib *next; | |
2803 | }; | |
2804 | static struct excluded_lib *excluded_libs; | |
2805 | ||
2806 | void | |
2807 | add_excluded_libs (const char *list) | |
2808 | { | |
2809 | const char *p = list, *end; | |
2810 | ||
2811 | while (*p != '\0') | |
2812 | { | |
2813 | struct excluded_lib *entry; | |
2814 | end = strpbrk (p, ",:"); | |
2815 | if (end == NULL) | |
2816 | end = p + strlen (p); | |
1e9cc1c2 | 2817 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
b58f81ae | 2818 | entry->next = excluded_libs; |
1e9cc1c2 | 2819 | entry->name = (char *) xmalloc (end - p + 1); |
b58f81ae DJ |
2820 | memcpy (entry->name, p, end - p); |
2821 | entry->name[end - p] = '\0'; | |
2822 | excluded_libs = entry; | |
2823 | if (*end == '\0') | |
329c1c86 | 2824 | break; |
b58f81ae DJ |
2825 | p = end + 1; |
2826 | } | |
2827 | } | |
2828 | ||
2829 | static void | |
2830 | check_excluded_libs (bfd *abfd) | |
2831 | { | |
2832 | struct excluded_lib *lib = excluded_libs; | |
2833 | ||
2834 | while (lib) | |
2835 | { | |
2836 | int len = strlen (lib->name); | |
607b4833 | 2837 | const char *filename = lbasename (bfd_get_filename (abfd)); |
b58f81ae DJ |
2838 | |
2839 | if (strcmp (lib->name, "ALL") == 0) | |
2840 | { | |
f38a2680 | 2841 | abfd->no_export = true; |
b58f81ae DJ |
2842 | return; |
2843 | } | |
2844 | ||
42627821 | 2845 | if (filename_ncmp (lib->name, filename, len) == 0 |
b58f81ae DJ |
2846 | && (filename[len] == '\0' |
2847 | || (filename[len] == '.' && filename[len + 1] == 'a' | |
2848 | && filename[len + 2] == '\0'))) | |
2849 | { | |
f38a2680 | 2850 | abfd->no_export = true; |
b58f81ae DJ |
2851 | return; |
2852 | } | |
2853 | ||
2854 | lib = lib->next; | |
2855 | } | |
2856 | } | |
2857 | ||
252b5132 RH |
2858 | /* Get the symbols for an input file. */ |
2859 | ||
f38a2680 | 2860 | bool |
1579bae1 AM |
2861 | load_symbols (lang_input_statement_type *entry, |
2862 | lang_statement_list_type *place) | |
252b5132 RH |
2863 | { |
2864 | char **matching; | |
2865 | ||
66be1055 | 2866 | if (entry->flags.loaded) |
f38a2680 | 2867 | return true; |
252b5132 RH |
2868 | |
2869 | ldfile_open_file (entry); | |
2870 | ||
c4b78195 | 2871 | /* Do not process further if the file was missing. */ |
66be1055 | 2872 | if (entry->flags.missing_file) |
f38a2680 | 2873 | return true; |
c4b78195 | 2874 | |
727a29ba AM |
2875 | if (trace_files || verbose) |
2876 | info_msg ("%pI\n", entry); | |
2877 | ||
0aa7f586 AM |
2878 | if (!bfd_check_format (entry->the_bfd, bfd_archive) |
2879 | && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
252b5132 RH |
2880 | { |
2881 | bfd_error_type err; | |
66be1055 | 2882 | struct lang_input_statement_flags save_flags; |
47e2e729 | 2883 | extern FILE *yyin; |
b7a26f91 | 2884 | |
252b5132 | 2885 | err = bfd_get_error (); |
884fb58e NC |
2886 | |
2887 | /* See if the emulation has some special knowledge. */ | |
2888 | if (ldemul_unrecognized_file (entry)) | |
370426d0 AM |
2889 | { |
2890 | if (err == bfd_error_file_ambiguously_recognized) | |
2891 | free (matching); | |
2892 | return true; | |
2893 | } | |
884fb58e | 2894 | |
252b5132 RH |
2895 | if (err == bfd_error_file_ambiguously_recognized) |
2896 | { | |
2897 | char **p; | |
2898 | ||
df5f2391 AM |
2899 | einfo (_("%P: %pB: file not recognized: %E;" |
2900 | " matching formats:"), entry->the_bfd); | |
252b5132 RH |
2901 | for (p = matching; *p != NULL; p++) |
2902 | einfo (" %s", *p); | |
370426d0 | 2903 | free (matching); |
252b5132 RH |
2904 | einfo ("%F\n"); |
2905 | } | |
2906 | else if (err != bfd_error_file_not_recognized | |
2907 | || place == NULL) | |
df5f2391 | 2908 | einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd); |
b7a26f91 | 2909 | |
252b5132 RH |
2910 | bfd_close (entry->the_bfd); |
2911 | entry->the_bfd = NULL; | |
2912 | ||
252b5132 | 2913 | /* Try to interpret the file as a linker script. */ |
66be1055 | 2914 | save_flags = input_flags; |
252b5132 RH |
2915 | ldfile_open_command_file (entry->filename); |
2916 | ||
bde18da4 | 2917 | push_stat_ptr (place); |
66be1055 AM |
2918 | input_flags.add_DT_NEEDED_for_regular |
2919 | = entry->flags.add_DT_NEEDED_for_regular; | |
2920 | input_flags.add_DT_NEEDED_for_dynamic | |
2921 | = entry->flags.add_DT_NEEDED_for_dynamic; | |
2922 | input_flags.whole_archive = entry->flags.whole_archive; | |
2923 | input_flags.dynamic = entry->flags.dynamic; | |
252b5132 | 2924 | |
f38a2680 | 2925 | ldfile_assumed_script = true; |
252b5132 | 2926 | parser_input = input_script; |
16171946 | 2927 | current_input_file = entry->filename; |
252b5132 | 2928 | yyparse (); |
16171946 | 2929 | current_input_file = NULL; |
f38a2680 | 2930 | ldfile_assumed_script = false; |
252b5132 | 2931 | |
f4a23d42 AM |
2932 | /* missing_file is sticky. sysrooted will already have been |
2933 | restored when seeing EOF in yyparse, but no harm to restore | |
2934 | again. */ | |
66be1055 AM |
2935 | save_flags.missing_file |= input_flags.missing_file; |
2936 | input_flags = save_flags; | |
bde18da4 | 2937 | pop_stat_ptr (); |
47e2e729 AM |
2938 | fclose (yyin); |
2939 | yyin = NULL; | |
f38a2680 | 2940 | entry->flags.loaded = true; |
252b5132 | 2941 | |
f38a2680 | 2942 | return true; |
252b5132 RH |
2943 | } |
2944 | ||
2945 | if (ldemul_recognized_file (entry)) | |
f38a2680 | 2946 | return true; |
252b5132 RH |
2947 | |
2948 | /* We don't call ldlang_add_file for an archive. Instead, the | |
2949 | add_symbols entry point will call ldlang_add_file, via the | |
2950 | add_archive_element callback, for each element of the archive | |
2951 | which is used. */ | |
2952 | switch (bfd_get_format (entry->the_bfd)) | |
2953 | { | |
2954 | default: | |
2955 | break; | |
2956 | ||
2957 | case bfd_object: | |
66be1055 | 2958 | if (!entry->flags.reload) |
15fc2e13 | 2959 | ldlang_add_file (entry); |
252b5132 RH |
2960 | break; |
2961 | ||
2962 | case bfd_archive: | |
b58f81ae DJ |
2963 | check_excluded_libs (entry->the_bfd); |
2964 | ||
00f93c44 | 2965 | bfd_set_usrdata (entry->the_bfd, entry); |
66be1055 | 2966 | if (entry->flags.whole_archive) |
252b5132 | 2967 | { |
b7a26f91 | 2968 | bfd *member = NULL; |
f38a2680 | 2969 | bool loaded = true; |
6770ec8c NC |
2970 | |
2971 | for (;;) | |
252b5132 | 2972 | { |
5d3236ee | 2973 | bfd *subsbfd; |
6770ec8c NC |
2974 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
2975 | ||
2976 | if (member == NULL) | |
2977 | break; | |
b7a26f91 | 2978 | |
0aa7f586 | 2979 | if (!bfd_check_format (member, bfd_object)) |
6770ec8c | 2980 | { |
df5f2391 | 2981 | einfo (_("%F%P: %pB: member %pB in archive is not an object\n"), |
6770ec8c | 2982 | entry->the_bfd, member); |
f38a2680 | 2983 | loaded = false; |
6770ec8c NC |
2984 | } |
2985 | ||
db0ca79f | 2986 | subsbfd = member; |
46105645 AM |
2987 | if (!(*link_info.callbacks |
2988 | ->add_archive_element) (&link_info, member, | |
2989 | "--whole-archive", &subsbfd)) | |
252b5132 | 2990 | abort (); |
6770ec8c | 2991 | |
5d3236ee DK |
2992 | /* Potentially, the add_archive_element hook may have set a |
2993 | substitute BFD for us. */ | |
46105645 | 2994 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
6770ec8c | 2995 | { |
df5f2391 | 2996 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), member); |
f38a2680 | 2997 | loaded = false; |
6770ec8c | 2998 | } |
252b5132 RH |
2999 | } |
3000 | ||
66be1055 | 3001 | entry->flags.loaded = loaded; |
6770ec8c | 3002 | return loaded; |
252b5132 | 3003 | } |
6770ec8c | 3004 | break; |
252b5132 RH |
3005 | } |
3006 | ||
03bdc404 | 3007 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
f38a2680 | 3008 | entry->flags.loaded = true; |
6770ec8c | 3009 | else |
df5f2391 | 3010 | einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd); |
252b5132 | 3011 | |
66be1055 | 3012 | return entry->flags.loaded; |
252b5132 RH |
3013 | } |
3014 | ||
b6bf44ba AM |
3015 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
3016 | may be NULL, indicating that it is a wildcard. Separate | |
3017 | lang_input_section statements are created for each part of the | |
3018 | expansion; they are added after the wild statement S. OUTPUT is | |
3019 | the output section. */ | |
252b5132 RH |
3020 | |
3021 | static void | |
1579bae1 AM |
3022 | wild (lang_wild_statement_type *s, |
3023 | const char *target ATTRIBUTE_UNUSED, | |
3024 | lang_output_section_statement_type *output) | |
252b5132 | 3025 | { |
b6bf44ba | 3026 | struct wildcard_list *sec; |
252b5132 | 3027 | |
af31506c | 3028 | if (s->filenames_sorted || s->any_specs_sorted) |
50c77e5d | 3029 | { |
329c1c86 AM |
3030 | lang_section_bst_type *tree; |
3031 | ||
af31506c | 3032 | walk_wild (s, output_section_callback_sort, output); |
50c77e5d | 3033 | |
e6f2cbf5 | 3034 | tree = s->tree; |
329c1c86 | 3035 | if (tree) |
e6f2cbf5 L |
3036 | { |
3037 | output_section_callback_tree_to_list (s, tree, output); | |
3038 | s->tree = NULL; | |
af31506c | 3039 | s->rightmost = &s->tree; |
e6f2cbf5 | 3040 | } |
50c77e5d | 3041 | } |
329c1c86 | 3042 | else |
af31506c | 3043 | walk_wild (s, output_section_callback_nosort, output); |
b6bf44ba | 3044 | |
abe6ac95 AM |
3045 | if (default_common_section == NULL) |
3046 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
b6bf44ba AM |
3047 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
3048 | { | |
3049 | /* Remember the section that common is going to in case we | |
b7a26f91 | 3050 | later get something which doesn't know where to put it. */ |
b6bf44ba | 3051 | default_common_section = output; |
abe6ac95 | 3052 | break; |
b6bf44ba | 3053 | } |
252b5132 RH |
3054 | } |
3055 | ||
b34976b6 | 3056 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 3057 | |
e50d8076 | 3058 | static int |
1579bae1 | 3059 | get_target (const bfd_target *target, void *data) |
e50d8076 | 3060 | { |
1e9cc1c2 | 3061 | const char *sought = (const char *) data; |
5f992e62 | 3062 | |
e50d8076 NC |
3063 | return strcmp (target->name, sought) == 0; |
3064 | } | |
3065 | ||
3066 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 3067 | |
e50d8076 | 3068 | static void |
1d38e9d1 | 3069 | stricpy (char *dest, const char *src) |
e50d8076 NC |
3070 | { |
3071 | char c; | |
5f992e62 | 3072 | |
08da4cac | 3073 | while ((c = *src++) != 0) |
3882b010 | 3074 | *dest++ = TOLOWER (c); |
e50d8076 | 3075 | |
08da4cac | 3076 | *dest = 0; |
e50d8076 NC |
3077 | } |
3078 | ||
396a2467 | 3079 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 3080 | from haystack. */ |
08da4cac | 3081 | |
e50d8076 | 3082 | static void |
1d38e9d1 | 3083 | strcut (char *haystack, const char *needle) |
e50d8076 NC |
3084 | { |
3085 | haystack = strstr (haystack, needle); | |
5f992e62 | 3086 | |
e50d8076 NC |
3087 | if (haystack) |
3088 | { | |
08da4cac | 3089 | char *src; |
e50d8076 | 3090 | |
08da4cac KH |
3091 | for (src = haystack + strlen (needle); *src;) |
3092 | *haystack++ = *src++; | |
5f992e62 | 3093 | |
08da4cac | 3094 | *haystack = 0; |
e50d8076 NC |
3095 | } |
3096 | } | |
3097 | ||
3098 | /* Compare two target format name strings. | |
3099 | Return a value indicating how "similar" they are. */ | |
08da4cac | 3100 | |
e50d8076 | 3101 | static int |
1d38e9d1 | 3102 | name_compare (const char *first, const char *second) |
e50d8076 | 3103 | { |
08da4cac KH |
3104 | char *copy1; |
3105 | char *copy2; | |
3106 | int result; | |
5f992e62 | 3107 | |
1e9cc1c2 NC |
3108 | copy1 = (char *) xmalloc (strlen (first) + 1); |
3109 | copy2 = (char *) xmalloc (strlen (second) + 1); | |
e50d8076 NC |
3110 | |
3111 | /* Convert the names to lower case. */ | |
3112 | stricpy (copy1, first); | |
3113 | stricpy (copy2, second); | |
3114 | ||
1579bae1 | 3115 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
3116 | strcut (copy1, "big"); |
3117 | strcut (copy1, "little"); | |
3118 | strcut (copy2, "big"); | |
3119 | strcut (copy2, "little"); | |
3120 | ||
3121 | /* Return a value based on how many characters match, | |
3122 | starting from the beginning. If both strings are | |
3123 | the same then return 10 * their length. */ | |
08da4cac KH |
3124 | for (result = 0; copy1[result] == copy2[result]; result++) |
3125 | if (copy1[result] == 0) | |
e50d8076 NC |
3126 | { |
3127 | result *= 10; | |
3128 | break; | |
3129 | } | |
5f992e62 | 3130 | |
e50d8076 NC |
3131 | free (copy1); |
3132 | free (copy2); | |
3133 | ||
3134 | return result; | |
3135 | } | |
3136 | ||
3137 | /* Set by closest_target_match() below. */ | |
08da4cac | 3138 | static const bfd_target *winner; |
e50d8076 NC |
3139 | |
3140 | /* Scan all the valid bfd targets looking for one that has the endianness | |
3141 | requirement that was specified on the command line, and is the nearest | |
3142 | match to the original output target. */ | |
08da4cac | 3143 | |
e50d8076 | 3144 | static int |
1579bae1 | 3145 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 3146 | { |
1e9cc1c2 | 3147 | const bfd_target *original = (const bfd_target *) data; |
5f992e62 | 3148 | |
08da4cac KH |
3149 | if (command_line.endian == ENDIAN_BIG |
3150 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 3151 | return 0; |
5f992e62 | 3152 | |
08da4cac KH |
3153 | if (command_line.endian == ENDIAN_LITTLE |
3154 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
3155 | return 0; |
3156 | ||
3157 | /* Must be the same flavour. */ | |
3158 | if (target->flavour != original->flavour) | |
3159 | return 0; | |
3160 | ||
de7dd2bd | 3161 | /* Ignore generic big and little endian elf vectors. */ |
967928e9 | 3162 | if (strcmp (target->name, "elf32-big") == 0 |
de7dd2bd NC |
3163 | || strcmp (target->name, "elf64-big") == 0 |
3164 | || strcmp (target->name, "elf32-little") == 0 | |
3165 | || strcmp (target->name, "elf64-little") == 0) | |
3166 | return 0; | |
3167 | ||
e50d8076 NC |
3168 | /* If we have not found a potential winner yet, then record this one. */ |
3169 | if (winner == NULL) | |
3170 | { | |
3171 | winner = target; | |
3172 | return 0; | |
3173 | } | |
3174 | ||
3175 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 3176 | Compare their names and choose the better one. */ |
d1778b88 AM |
3177 | if (name_compare (target->name, original->name) |
3178 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
3179 | winner = target; |
3180 | ||
3181 | /* Keep on searching until wqe have checked them all. */ | |
3182 | return 0; | |
3183 | } | |
3184 | ||
3185 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 3186 | |
1d38e9d1 | 3187 | static const char * |
1579bae1 | 3188 | get_first_input_target (void) |
e50d8076 | 3189 | { |
1d38e9d1 | 3190 | const char *target = NULL; |
e50d8076 NC |
3191 | |
3192 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
3193 | { | |
3194 | if (s->header.type == lang_input_statement_enum | |
66be1055 | 3195 | && s->flags.real) |
e50d8076 NC |
3196 | { |
3197 | ldfile_open_file (s); | |
5f992e62 | 3198 | |
e50d8076 NC |
3199 | if (s->the_bfd != NULL |
3200 | && bfd_check_format (s->the_bfd, bfd_object)) | |
3201 | { | |
3202 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 3203 | |
e50d8076 NC |
3204 | if (target != NULL) |
3205 | break; | |
3206 | } | |
3207 | } | |
3208 | } | |
5f992e62 | 3209 | |
e50d8076 NC |
3210 | return target; |
3211 | } | |
3212 | ||
599917b8 | 3213 | const char * |
1579bae1 | 3214 | lang_get_output_target (void) |
599917b8 JJ |
3215 | { |
3216 | const char *target; | |
3217 | ||
3218 | /* Has the user told us which output format to use? */ | |
1579bae1 | 3219 | if (output_target != NULL) |
599917b8 JJ |
3220 | return output_target; |
3221 | ||
3222 | /* No - has the current target been set to something other than | |
3223 | the default? */ | |
30824704 | 3224 | if (current_target != default_target && current_target != NULL) |
599917b8 JJ |
3225 | return current_target; |
3226 | ||
3227 | /* No - can we determine the format of the first input file? */ | |
3228 | target = get_first_input_target (); | |
3229 | if (target != NULL) | |
3230 | return target; | |
3231 | ||
3232 | /* Failed - use the default output target. */ | |
3233 | return default_target; | |
3234 | } | |
3235 | ||
252b5132 RH |
3236 | /* Open the output file. */ |
3237 | ||
f13a99db | 3238 | static void |
1579bae1 | 3239 | open_output (const char *name) |
252b5132 | 3240 | { |
43908c16 AM |
3241 | lang_input_statement_type *f; |
3242 | char *out = lrealpath (name); | |
3243 | ||
3244 | for (f = (void *) input_file_chain.head; | |
3245 | f != NULL; | |
3246 | f = f->next_real_file) | |
3247 | if (f->flags.real) | |
3248 | { | |
da1ecf89 | 3249 | char *in = lrealpath (f->local_sym_name); |
43908c16 AM |
3250 | if (filename_cmp (in, out) == 0) |
3251 | einfo (_("%F%P: input file '%s' is the same as output file\n"), | |
3252 | f->filename); | |
3253 | free (in); | |
3254 | } | |
3255 | free (out); | |
3256 | ||
599917b8 | 3257 | output_target = lang_get_output_target (); |
5f992e62 | 3258 | |
08da4cac KH |
3259 | /* Has the user requested a particular endianness on the command |
3260 | line? */ | |
e50d8076 NC |
3261 | if (command_line.endian != ENDIAN_UNSET) |
3262 | { | |
e50d8076 | 3263 | /* Get the chosen target. */ |
4212b42d AM |
3264 | const bfd_target *target |
3265 | = bfd_iterate_over_targets (get_target, (void *) output_target); | |
e50d8076 | 3266 | |
c13b1b77 NC |
3267 | /* If the target is not supported, we cannot do anything. */ |
3268 | if (target != NULL) | |
e50d8076 | 3269 | { |
4212b42d AM |
3270 | enum bfd_endian desired_endian; |
3271 | ||
c13b1b77 NC |
3272 | if (command_line.endian == ENDIAN_BIG) |
3273 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 3274 | else |
c13b1b77 | 3275 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
3276 | |
3277 | /* See if the target has the wrong endianness. This should | |
3278 | not happen if the linker script has provided big and | |
3279 | little endian alternatives, but some scrips don't do | |
3280 | this. */ | |
c13b1b77 | 3281 | if (target->byteorder != desired_endian) |
e50d8076 | 3282 | { |
c13b1b77 NC |
3283 | /* If it does, then see if the target provides |
3284 | an alternative with the correct endianness. */ | |
3285 | if (target->alternative_target != NULL | |
3286 | && (target->alternative_target->byteorder == desired_endian)) | |
3287 | output_target = target->alternative_target->name; | |
e50d8076 | 3288 | else |
c13b1b77 | 3289 | { |
5f992e62 AM |
3290 | /* Try to find a target as similar as possible to |
3291 | the default target, but which has the desired | |
3292 | endian characteristic. */ | |
4212b42d AM |
3293 | bfd_iterate_over_targets (closest_target_match, |
3294 | (void *) target); | |
5f992e62 AM |
3295 | |
3296 | /* Oh dear - we could not find any targets that | |
3297 | satisfy our requirements. */ | |
c13b1b77 | 3298 | if (winner == NULL) |
6feb9908 AM |
3299 | einfo (_("%P: warning: could not find any targets" |
3300 | " that match endianness requirement\n")); | |
c13b1b77 NC |
3301 | else |
3302 | output_target = winner->name; | |
3303 | } | |
e50d8076 NC |
3304 | } |
3305 | } | |
252b5132 | 3306 | } |
5f992e62 | 3307 | |
f13a99db | 3308 | link_info.output_bfd = bfd_openw (name, output_target); |
252b5132 | 3309 | |
f13a99db | 3310 | if (link_info.output_bfd == NULL) |
252b5132 RH |
3311 | { |
3312 | if (bfd_get_error () == bfd_error_invalid_target) | |
df5f2391 | 3313 | einfo (_("%F%P: target %s not found\n"), output_target); |
e50d8076 | 3314 | |
df5f2391 | 3315 | einfo (_("%F%P: cannot open output file %s: %E\n"), name); |
252b5132 RH |
3316 | } |
3317 | ||
f38a2680 | 3318 | delete_output_file_on_failure = true; |
252b5132 | 3319 | |
0aa7f586 | 3320 | if (!bfd_set_format (link_info.output_bfd, bfd_object)) |
df5f2391 | 3321 | einfo (_("%F%P: %s: can not make object file: %E\n"), name); |
0aa7f586 | 3322 | if (!bfd_set_arch_mach (link_info.output_bfd, |
252b5132 RH |
3323 | ldfile_output_architecture, |
3324 | ldfile_output_machine)) | |
df5f2391 | 3325 | einfo (_("%F%P: %s: can not set architecture: %E\n"), name); |
252b5132 | 3326 | |
f13a99db | 3327 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
1579bae1 | 3328 | if (link_info.hash == NULL) |
df5f2391 | 3329 | einfo (_("%F%P: can not create hash table: %E\n")); |
252b5132 | 3330 | |
f13a99db | 3331 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
252b5132 RH |
3332 | } |
3333 | ||
252b5132 | 3334 | static void |
1579bae1 | 3335 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
3336 | { |
3337 | switch (statement->header.type) | |
3338 | { | |
3339 | case lang_output_statement_enum: | |
f13a99db AM |
3340 | ASSERT (link_info.output_bfd == NULL); |
3341 | open_output (statement->output_statement.name); | |
252b5132 | 3342 | ldemul_set_output_arch (); |
0e1862bb L |
3343 | if (config.magic_demand_paged |
3344 | && !bfd_link_relocatable (&link_info)) | |
f13a99db | 3345 | link_info.output_bfd->flags |= D_PAGED; |
252b5132 | 3346 | else |
f13a99db | 3347 | link_info.output_bfd->flags &= ~D_PAGED; |
252b5132 | 3348 | if (config.text_read_only) |
f13a99db | 3349 | link_info.output_bfd->flags |= WP_TEXT; |
252b5132 | 3350 | else |
f13a99db | 3351 | link_info.output_bfd->flags &= ~WP_TEXT; |
252b5132 | 3352 | if (link_info.traditional_format) |
f13a99db | 3353 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
252b5132 | 3354 | else |
f13a99db | 3355 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
252b5132 RH |
3356 | break; |
3357 | ||
3358 | case lang_target_statement_enum: | |
3359 | current_target = statement->target_statement.target; | |
3360 | break; | |
3361 | default: | |
3362 | break; | |
3363 | } | |
3364 | } | |
3365 | ||
e5caa5e0 | 3366 | static void |
61826503 | 3367 | init_opb (asection *s) |
e5caa5e0 | 3368 | { |
bb294208 | 3369 | unsigned int x; |
61826503 | 3370 | |
e5caa5e0 | 3371 | opb_shift = 0; |
bb294208 AM |
3372 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
3373 | && s != NULL | |
3374 | && (s->flags & SEC_ELF_OCTETS) != 0) | |
3375 | return; | |
3376 | ||
3377 | x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
3378 | ldfile_output_machine); | |
e5caa5e0 AM |
3379 | if (x > 1) |
3380 | while ((x & 1) == 0) | |
3381 | { | |
3382 | x >>= 1; | |
3383 | ++opb_shift; | |
3384 | } | |
3385 | ASSERT (x == 1); | |
3386 | } | |
3387 | ||
252b5132 RH |
3388 | /* Open all the input files. */ |
3389 | ||
486329aa AM |
3390 | enum open_bfd_mode |
3391 | { | |
3392 | OPEN_BFD_NORMAL = 0, | |
3393 | OPEN_BFD_FORCE = 1, | |
3394 | OPEN_BFD_RESCAN = 2 | |
3395 | }; | |
0381901e | 3396 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 | 3397 | static lang_input_statement_type *plugin_insert = NULL; |
b02c4f16 | 3398 | static struct bfd_link_hash_entry *plugin_undefs = NULL; |
9e2278f5 | 3399 | #endif |
486329aa | 3400 | |
252b5132 | 3401 | static void |
486329aa | 3402 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
252b5132 | 3403 | { |
1579bae1 | 3404 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3405 | { |
3406 | switch (s->header.type) | |
3407 | { | |
3408 | case lang_constructors_statement_enum: | |
486329aa | 3409 | open_input_bfds (constructor_list.head, mode); |
252b5132 RH |
3410 | break; |
3411 | case lang_output_section_statement_enum: | |
486329aa | 3412 | open_input_bfds (s->output_section_statement.children.head, mode); |
252b5132 RH |
3413 | break; |
3414 | case lang_wild_statement_enum: | |
08da4cac | 3415 | /* Maybe we should load the file's symbols. */ |
486329aa AM |
3416 | if ((mode & OPEN_BFD_RESCAN) == 0 |
3417 | && s->wild_statement.filename | |
97407faf AM |
3418 | && !wildcardp (s->wild_statement.filename) |
3419 | && !archive_path (s->wild_statement.filename)) | |
4a43e768 | 3420 | lookup_name (s->wild_statement.filename); |
486329aa | 3421 | open_input_bfds (s->wild_statement.children.head, mode); |
252b5132 RH |
3422 | break; |
3423 | case lang_group_statement_enum: | |
3424 | { | |
3425 | struct bfd_link_hash_entry *undefs; | |
0381901e | 3426 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3427 | lang_input_statement_type *plugin_insert_save; |
3428 | #endif | |
252b5132 RH |
3429 | |
3430 | /* We must continually search the entries in the group | |
08da4cac KH |
3431 | until no new symbols are added to the list of undefined |
3432 | symbols. */ | |
252b5132 RH |
3433 | |
3434 | do | |
3435 | { | |
0381901e | 3436 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3437 | plugin_insert_save = plugin_insert; |
3438 | #endif | |
252b5132 | 3439 | undefs = link_info.hash->undefs_tail; |
486329aa AM |
3440 | open_input_bfds (s->group_statement.children.head, |
3441 | mode | OPEN_BFD_FORCE); | |
252b5132 | 3442 | } |
b02c4f16 | 3443 | while (undefs != link_info.hash->undefs_tail |
0381901e | 3444 | #if BFD_SUPPORTS_PLUGINS |
b02c4f16 AM |
3445 | /* Objects inserted by a plugin, which are loaded |
3446 | before we hit this loop, may have added new | |
3447 | undefs. */ | |
3448 | || (plugin_insert != plugin_insert_save && plugin_undefs) | |
3449 | #endif | |
3450 | ); | |
252b5132 RH |
3451 | } |
3452 | break; | |
3453 | case lang_target_statement_enum: | |
3454 | current_target = s->target_statement.target; | |
3455 | break; | |
3456 | case lang_input_statement_enum: | |
66be1055 | 3457 | if (s->input_statement.flags.real) |
252b5132 | 3458 | { |
bde18da4 | 3459 | lang_statement_union_type **os_tail; |
252b5132 | 3460 | lang_statement_list_type add; |
d215621e | 3461 | bfd *abfd; |
252b5132 RH |
3462 | |
3463 | s->input_statement.target = current_target; | |
3464 | ||
3465 | /* If we are being called from within a group, and this | |
afd7a018 AM |
3466 | is an archive which has already been searched, then |
3467 | force it to be researched unless the whole archive | |
d215621e AM |
3468 | has been loaded already. Do the same for a rescan. |
3469 | Likewise reload --as-needed shared libs. */ | |
486329aa | 3470 | if (mode != OPEN_BFD_NORMAL |
0381901e | 3471 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3472 | && ((mode & OPEN_BFD_RESCAN) == 0 |
3473 | || plugin_insert == NULL) | |
3474 | #endif | |
66be1055 | 3475 | && s->input_statement.flags.loaded |
d215621e AM |
3476 | && (abfd = s->input_statement.the_bfd) != NULL |
3477 | && ((bfd_get_format (abfd) == bfd_archive | |
3478 | && !s->input_statement.flags.whole_archive) | |
3479 | || (bfd_get_format (abfd) == bfd_object | |
3480 | && ((abfd->flags) & DYNAMIC) != 0 | |
3481 | && s->input_statement.flags.add_DT_NEEDED_for_regular | |
e77620a5 | 3482 | && bfd_get_flavour (abfd) == bfd_target_elf_flavour |
0fef4b98 | 3483 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) |
15fc2e13 | 3484 | { |
f38a2680 AM |
3485 | s->input_statement.flags.loaded = false; |
3486 | s->input_statement.flags.reload = true; | |
15fc2e13 | 3487 | } |
252b5132 | 3488 | |
5c1e6d53 | 3489 | os_tail = lang_os_list.tail; |
d1778b88 | 3490 | lang_list_init (&add); |
1276aefa | 3491 | |
0aa7f586 | 3492 | if (!load_symbols (&s->input_statement, &add)) |
f38a2680 | 3493 | config.make_executable = false; |
252b5132 RH |
3494 | |
3495 | if (add.head != NULL) | |
3496 | { | |
bde18da4 AM |
3497 | /* If this was a script with output sections then |
3498 | tack any added statements on to the end of the | |
3499 | list. This avoids having to reorder the output | |
3500 | section statement list. Very likely the user | |
3501 | forgot -T, and whatever we do here will not meet | |
3502 | naive user expectations. */ | |
5c1e6d53 | 3503 | if (os_tail != lang_os_list.tail) |
bde18da4 AM |
3504 | { |
3505 | einfo (_("%P: warning: %s contains output sections;" | |
3506 | " did you forget -T?\n"), | |
3507 | s->input_statement.filename); | |
3508 | *stat_ptr->tail = add.head; | |
3509 | stat_ptr->tail = add.tail; | |
3510 | } | |
3511 | else | |
3512 | { | |
3513 | *add.tail = s->header.next; | |
3514 | s->header.next = add.head; | |
3515 | } | |
252b5132 RH |
3516 | } |
3517 | } | |
0381901e | 3518 | #if BFD_SUPPORTS_PLUGINS |
9e2278f5 AM |
3519 | /* If we have found the point at which a plugin added new |
3520 | files, clear plugin_insert to enable archive rescan. */ | |
3521 | if (&s->input_statement == plugin_insert) | |
3522 | plugin_insert = NULL; | |
3523 | #endif | |
252b5132 | 3524 | break; |
e759c116 | 3525 | case lang_assignment_statement_enum: |
165f707a | 3526 | if (s->assignment_statement.exp->type.node_class != etree_assert) |
01554a74 | 3527 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
e759c116 | 3528 | break; |
252b5132 RH |
3529 | default: |
3530 | break; | |
3531 | } | |
3532 | } | |
c4b78195 NC |
3533 | |
3534 | /* Exit if any of the files were missing. */ | |
66be1055 | 3535 | if (input_flags.missing_file) |
c4b78195 | 3536 | einfo ("%F"); |
252b5132 RH |
3537 | } |
3538 | ||
094e34f2 | 3539 | #ifdef ENABLE_LIBCTF |
926c9e76 NA |
3540 | /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings |
3541 | that happened specifically at CTF open time. */ | |
3542 | static void | |
139633c3 | 3543 | lang_ctf_errs_warnings (ctf_dict_t *fp) |
926c9e76 NA |
3544 | { |
3545 | ctf_next_t *i = NULL; | |
3546 | char *text; | |
3547 | int is_warning; | |
3548 | int err; | |
3549 | ||
3550 | while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL) | |
3551 | { | |
5e9b84f7 | 3552 | einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), |
926c9e76 NA |
3553 | text); |
3554 | free (text); | |
3555 | } | |
3556 | if (err != ECTF_NEXT_END) | |
3557 | { | |
3558 | einfo (_("CTF error: cannot get CTF errors: `%s'\n"), | |
3559 | ctf_errmsg (err)); | |
3560 | } | |
3561 | ||
3562 | /* `err' returns errors from the error/warning iterator in particular. | |
3563 | These never assert. But if we have an fp, that could have recorded | |
3564 | an assertion failure: assert if it has done so. */ | |
3565 | ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL); | |
3566 | } | |
3567 | ||
1ff6de03 NA |
3568 | /* Open the CTF sections in the input files with libctf: if any were opened, |
3569 | create a fake input file that we'll write the merged CTF data to later | |
3570 | on. */ | |
3571 | ||
3572 | static void | |
3573 | ldlang_open_ctf (void) | |
3574 | { | |
3575 | int any_ctf = 0; | |
3576 | int err; | |
3577 | ||
3578 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3579 | { | |
3580 | asection *sect; | |
3581 | ||
139633c3 | 3582 | /* Incoming files from the compiler have a single ctf_dict_t in them |
1ff6de03 NA |
3583 | (which is presented to us by the libctf API in a ctf_archive_t |
3584 | wrapper): files derived from a previous relocatable link have a CTF | |
3585 | archive containing possibly many CTF files. */ | |
3586 | ||
3587 | if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) | |
3588 | { | |
3589 | if (err != ECTF_NOCTFDATA) | |
926c9e76 NA |
3590 | { |
3591 | lang_ctf_errs_warnings (NULL); | |
3592 | einfo (_("%P: warning: CTF section in %pB not loaded; " | |
5e9b84f7 | 3593 | "its types will be discarded: %s\n"), file->the_bfd, |
1ff6de03 | 3594 | ctf_errmsg (err)); |
926c9e76 | 3595 | } |
1ff6de03 NA |
3596 | continue; |
3597 | } | |
3598 | ||
3599 | /* Prevent the contents of this section from being written, while | |
fa03171f NA |
3600 | requiring the section itself to be duplicated in the output, but only |
3601 | once. */ | |
1ff6de03 NA |
3602 | /* This section must exist if ctf_bfdopen() succeeded. */ |
3603 | sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); | |
3604 | sect->size = 0; | |
3605 | sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; | |
3606 | ||
fa03171f NA |
3607 | if (any_ctf) |
3608 | sect->flags |= SEC_EXCLUDE; | |
1ff6de03 NA |
3609 | any_ctf = 1; |
3610 | } | |
3611 | ||
3612 | if (!any_ctf) | |
3613 | { | |
3614 | ctf_output = NULL; | |
3615 | return; | |
3616 | } | |
3617 | ||
3618 | if ((ctf_output = ctf_create (&err)) != NULL) | |
3619 | return; | |
3620 | ||
c48acf6f | 3621 | einfo (_("%P: warning: CTF output not created: `%s'\n"), |
1ff6de03 NA |
3622 | ctf_errmsg (err)); |
3623 | ||
3624 | LANG_FOR_EACH_INPUT_STATEMENT (errfile) | |
3625 | ctf_close (errfile->the_ctf); | |
3626 | } | |
3627 | ||
3628 | /* Merge together CTF sections. After this, only the symtab-dependent | |
3629 | function and data object sections need adjustment. */ | |
3630 | ||
3631 | static void | |
3632 | lang_merge_ctf (void) | |
3633 | { | |
3634 | asection *output_sect; | |
5dba6f05 | 3635 | int flags = 0; |
1ff6de03 NA |
3636 | |
3637 | if (!ctf_output) | |
3638 | return; | |
3639 | ||
3640 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3641 | ||
3642 | /* If the section was discarded, don't waste time merging. */ | |
3643 | if (output_sect == NULL) | |
3644 | { | |
139633c3 | 3645 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
3646 | ctf_output = NULL; |
3647 | ||
3648 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3649 | { | |
3650 | ctf_close (file->the_ctf); | |
3651 | file->the_ctf = NULL; | |
3652 | } | |
3653 | return; | |
3654 | } | |
3655 | ||
3656 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3657 | { | |
3658 | if (!file->the_ctf) | |
3659 | continue; | |
3660 | ||
f320bba5 | 3661 | /* Takes ownership of file->the_ctf. */ |
1ff6de03 NA |
3662 | if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) |
3663 | { | |
f320bba5 EB |
3664 | einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), |
3665 | file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 NA |
3666 | ctf_close (file->the_ctf); |
3667 | file->the_ctf = NULL; | |
3668 | continue; | |
3669 | } | |
3670 | } | |
3671 | ||
5dba6f05 NA |
3672 | if (!config.ctf_share_duplicated) |
3673 | flags = CTF_LINK_SHARE_UNCONFLICTED; | |
3674 | else | |
3675 | flags = CTF_LINK_SHARE_DUPLICATED; | |
3676 | if (!config.ctf_variables) | |
3677 | flags |= CTF_LINK_OMIT_VARIABLES_SECTION; | |
35a01a04 NA |
3678 | if (bfd_link_relocatable (&link_info)) |
3679 | flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; | |
5dba6f05 NA |
3680 | |
3681 | if (ctf_link (ctf_output, flags) < 0) | |
1ff6de03 | 3682 | { |
926c9e76 | 3683 | lang_ctf_errs_warnings (ctf_output); |
f320bba5 | 3684 | einfo (_("%P: warning: CTF linking failed; " |
5e9b84f7 | 3685 | "output will have no CTF section: %s\n"), |
1ff6de03 NA |
3686 | ctf_errmsg (ctf_errno (ctf_output))); |
3687 | if (output_sect) | |
3688 | { | |
3689 | output_sect->size = 0; | |
3690 | output_sect->flags |= SEC_EXCLUDE; | |
3691 | } | |
3692 | } | |
926c9e76 | 3693 | /* Output any lingering errors that didn't come from ctf_link. */ |
8b37e7b6 | 3694 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3695 | } |
3696 | ||
3d16b64e NA |
3697 | /* Let the emulation acquire strings from the dynamic strtab to help it optimize |
3698 | the CTF, if supported. */ | |
1ff6de03 NA |
3699 | |
3700 | void | |
3d16b64e | 3701 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) |
1ff6de03 | 3702 | { |
3d16b64e NA |
3703 | ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); |
3704 | } | |
3705 | ||
3706 | /* Inform the emulation about the addition of a new dynamic symbol, in BFD | |
3707 | internal format. */ | |
3708 | void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym) | |
3709 | { | |
3710 | ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym); | |
1ff6de03 NA |
3711 | } |
3712 | ||
3713 | /* Write out the CTF section. Called early, if the emulation isn't going to | |
3714 | need to dedup against the strtab and symtab, then possibly called from the | |
3715 | target linker code if the dedup has happened. */ | |
3716 | static void | |
3717 | lang_write_ctf (int late) | |
3718 | { | |
3719 | size_t output_size; | |
3720 | asection *output_sect; | |
3721 | ||
3722 | if (!ctf_output) | |
3723 | return; | |
3724 | ||
3725 | if (late) | |
3726 | { | |
3727 | /* Emit CTF late if this emulation says it can do so. */ | |
3728 | if (ldemul_emit_ctf_early ()) | |
3729 | return; | |
3730 | } | |
3731 | else | |
3732 | { | |
3733 | if (!ldemul_emit_ctf_early ()) | |
3734 | return; | |
3735 | } | |
3736 | ||
3d16b64e NA |
3737 | /* Inform the emulation that all the symbols that will be received have |
3738 | been. */ | |
3739 | ||
3740 | ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL); | |
3741 | ||
1ff6de03 NA |
3742 | /* Emit CTF. */ |
3743 | ||
3744 | output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); | |
3745 | if (output_sect) | |
3746 | { | |
3747 | output_sect->contents = ctf_link_write (ctf_output, &output_size, | |
3748 | CTF_COMPRESSION_THRESHOLD); | |
3749 | output_sect->size = output_size; | |
3750 | output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; | |
3751 | ||
926c9e76 | 3752 | lang_ctf_errs_warnings (ctf_output); |
1ff6de03 NA |
3753 | if (!output_sect->contents) |
3754 | { | |
f320bba5 | 3755 | einfo (_("%P: warning: CTF section emission failed; " |
5e9b84f7 | 3756 | "output will have no CTF section: %s\n"), |
f320bba5 | 3757 | ctf_errmsg (ctf_errno (ctf_output))); |
1ff6de03 NA |
3758 | output_sect->size = 0; |
3759 | output_sect->flags |= SEC_EXCLUDE; | |
3760 | } | |
3761 | } | |
3762 | ||
3763 | /* This also closes every CTF input file used in the link. */ | |
139633c3 | 3764 | ctf_dict_close (ctf_output); |
1ff6de03 NA |
3765 | ctf_output = NULL; |
3766 | ||
3767 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3768 | file->the_ctf = NULL; | |
3769 | } | |
3770 | ||
3771 | /* Write out the CTF section late, if the emulation needs that. */ | |
3772 | ||
3773 | void | |
3774 | ldlang_write_ctf_late (void) | |
3775 | { | |
3776 | /* Trigger a "late call", if the emulation needs one. */ | |
3777 | ||
3778 | lang_write_ctf (1); | |
3779 | } | |
094e34f2 NA |
3780 | #else |
3781 | static void | |
3782 | ldlang_open_ctf (void) | |
3783 | { | |
3784 | LANG_FOR_EACH_INPUT_STATEMENT (file) | |
3785 | { | |
3786 | asection *sect; | |
3787 | ||
3788 | /* If built without CTF, warn and delete all CTF sections from the output. | |
3789 | (The alternative would be to simply concatenate them, which does not | |
3790 | yield a valid CTF section.) */ | |
3791 | ||
3792 | if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL) | |
3793 | { | |
f320bba5 EB |
3794 | einfo (_("%P: warning: CTF section in %pB not linkable: " |
3795 | "%P was built without support for CTF\n"), file->the_bfd); | |
094e34f2 NA |
3796 | sect->size = 0; |
3797 | sect->flags |= SEC_EXCLUDE; | |
3798 | } | |
3799 | } | |
3800 | } | |
3801 | ||
3802 | static void lang_merge_ctf (void) {} | |
3803 | void | |
3d16b64e NA |
3804 | ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab |
3805 | ATTRIBUTE_UNUSED) {} | |
3806 | void | |
3807 | ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED, | |
3808 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {} | |
094e34f2 NA |
3809 | static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} |
3810 | void ldlang_write_ctf_late (void) {} | |
3811 | #endif | |
1ff6de03 | 3812 | |
08da4cac | 3813 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
3814 | This is a two step process as the symbol table doesn't even exist at |
3815 | the time the ld command line is processed. First we put the name | |
3816 | on a list, then, once the output file has been opened, transfer the | |
3817 | name to the symbol table. */ | |
3818 | ||
e3e942e9 | 3819 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 3820 | |
e3e942e9 | 3821 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
3822 | |
3823 | void | |
f38a2680 | 3824 | ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) |
252b5132 | 3825 | { |
24898b70 | 3826 | ldlang_undef_chain_list_type *new_undef; |
252b5132 | 3827 | |
988de25b | 3828 | new_undef = stat_alloc (sizeof (*new_undef)); |
d3ce72d0 NC |
3829 | new_undef->next = ldlang_undef_chain_list_head; |
3830 | ldlang_undef_chain_list_head = new_undef; | |
252b5132 | 3831 | |
d3ce72d0 | 3832 | new_undef->name = xstrdup (name); |
fcf0e35b | 3833 | |
f13a99db | 3834 | if (link_info.output_bfd != NULL) |
d3ce72d0 | 3835 | insert_undefined (new_undef->name); |
fcf0e35b AM |
3836 | } |
3837 | ||
3838 | /* Insert NAME as undefined in the symbol table. */ | |
3839 | ||
3840 | static void | |
1579bae1 | 3841 | insert_undefined (const char *name) |
fcf0e35b AM |
3842 | { |
3843 | struct bfd_link_hash_entry *h; | |
3844 | ||
f38a2680 | 3845 | h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); |
1579bae1 | 3846 | if (h == NULL) |
df5f2391 | 3847 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
fcf0e35b AM |
3848 | if (h->type == bfd_link_hash_new) |
3849 | { | |
3850 | h->type = bfd_link_hash_undefined; | |
3851 | h->u.undef.abfd = NULL; | |
f38a2680 | 3852 | h->non_ir_ref_regular = true; |
fcf0e35b AM |
3853 | bfd_link_add_undef (link_info.hash, h); |
3854 | } | |
252b5132 RH |
3855 | } |
3856 | ||
3857 | /* Run through the list of undefineds created above and place them | |
3858 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
3859 | script file. */ |
3860 | ||
252b5132 | 3861 | static void |
1579bae1 | 3862 | lang_place_undefineds (void) |
252b5132 RH |
3863 | { |
3864 | ldlang_undef_chain_list_type *ptr; | |
3865 | ||
1579bae1 AM |
3866 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3867 | insert_undefined (ptr->name); | |
252b5132 RH |
3868 | } |
3869 | ||
5789f845 AM |
3870 | /* Mark -u symbols against garbage collection. */ |
3871 | ||
3872 | static void | |
3873 | lang_mark_undefineds (void) | |
3874 | { | |
3875 | ldlang_undef_chain_list_type *ptr; | |
3876 | ||
fc304b88 | 3877 | if (is_elf_hash_table (link_info.hash)) |
5789f845 AM |
3878 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
3879 | { | |
3880 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
f38a2680 | 3881 | bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); |
5789f845 AM |
3882 | if (h != NULL) |
3883 | h->mark = 1; | |
3884 | } | |
3885 | } | |
3886 | ||
0a618243 AB |
3887 | /* Structure used to build the list of symbols that the user has required |
3888 | be defined. */ | |
3889 | ||
3890 | struct require_defined_symbol | |
3891 | { | |
3892 | const char *name; | |
3893 | struct require_defined_symbol *next; | |
3894 | }; | |
3895 | ||
3896 | /* The list of symbols that the user has required be defined. */ | |
3897 | ||
3898 | static struct require_defined_symbol *require_defined_symbol_list; | |
3899 | ||
3900 | /* Add a new symbol NAME to the list of symbols that are required to be | |
3901 | defined. */ | |
3902 | ||
3903 | void | |
0aa7f586 | 3904 | ldlang_add_require_defined (const char *const name) |
0a618243 AB |
3905 | { |
3906 | struct require_defined_symbol *ptr; | |
3907 | ||
f38a2680 | 3908 | ldlang_add_undef (name, true); |
988de25b | 3909 | ptr = stat_alloc (sizeof (*ptr)); |
0a618243 AB |
3910 | ptr->next = require_defined_symbol_list; |
3911 | ptr->name = strdup (name); | |
3912 | require_defined_symbol_list = ptr; | |
3913 | } | |
3914 | ||
3915 | /* Check that all symbols the user required to be defined, are defined, | |
3916 | raise an error if we find a symbol that is not defined. */ | |
3917 | ||
3918 | static void | |
3919 | ldlang_check_require_defined_symbols (void) | |
3920 | { | |
3921 | struct require_defined_symbol *ptr; | |
3922 | ||
3923 | for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) | |
3924 | { | |
3925 | struct bfd_link_hash_entry *h; | |
3926 | ||
3927 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, | |
f38a2680 | 3928 | false, false, true); |
0a618243 | 3929 | if (h == NULL |
0aa7f586 AM |
3930 | || (h->type != bfd_link_hash_defined |
3931 | && h->type != bfd_link_hash_defweak)) | |
df5f2391 | 3932 | einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); |
0a618243 AB |
3933 | } |
3934 | } | |
3935 | ||
0841712e JJ |
3936 | /* Check for all readonly or some readwrite sections. */ |
3937 | ||
3938 | static void | |
6feb9908 AM |
3939 | check_input_sections |
3940 | (lang_statement_union_type *s, | |
3941 | lang_output_section_statement_type *output_section_statement) | |
0841712e | 3942 | { |
988de25b | 3943 | for (; s != NULL; s = s->header.next) |
0841712e JJ |
3944 | { |
3945 | switch (s->header.type) | |
967928e9 AM |
3946 | { |
3947 | case lang_wild_statement_enum: | |
3948 | walk_wild (&s->wild_statement, check_section_callback, | |
3949 | output_section_statement); | |
0aa7f586 | 3950 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3951 | return; |
3952 | break; | |
3953 | case lang_constructors_statement_enum: | |
3954 | check_input_sections (constructor_list.head, | |
3955 | output_section_statement); | |
0aa7f586 | 3956 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3957 | return; |
3958 | break; | |
3959 | case lang_group_statement_enum: | |
3960 | check_input_sections (s->group_statement.children.head, | |
3961 | output_section_statement); | |
0aa7f586 | 3962 | if (!output_section_statement->all_input_readonly) |
967928e9 AM |
3963 | return; |
3964 | break; | |
3965 | default: | |
3966 | break; | |
3967 | } | |
0841712e JJ |
3968 | } |
3969 | } | |
3970 | ||
bcaa7b3e L |
3971 | /* Update wildcard statements if needed. */ |
3972 | ||
3973 | static void | |
3974 | update_wild_statements (lang_statement_union_type *s) | |
3975 | { | |
3976 | struct wildcard_list *sec; | |
3977 | ||
3978 | switch (sort_section) | |
3979 | { | |
3980 | default: | |
3981 | FAIL (); | |
3982 | ||
3983 | case none: | |
3984 | break; | |
3985 | ||
3986 | case by_name: | |
3987 | case by_alignment: | |
3988 | for (; s != NULL; s = s->header.next) | |
3989 | { | |
3990 | switch (s->header.type) | |
3991 | { | |
3992 | default: | |
3993 | break; | |
3994 | ||
3995 | case lang_wild_statement_enum: | |
0d0999db L |
3996 | for (sec = s->wild_statement.section_list; sec != NULL; |
3997 | sec = sec->next) | |
2d3181c7 AM |
3998 | /* Don't sort .init/.fini sections. */ |
3999 | if (strcmp (sec->spec.name, ".init") != 0 | |
4000 | && strcmp (sec->spec.name, ".fini") != 0) | |
af31506c MM |
4001 | { |
4002 | switch (sec->spec.sorted) | |
4003 | { | |
4004 | case none: | |
4005 | sec->spec.sorted = sort_section; | |
4006 | break; | |
4007 | case by_name: | |
4008 | if (sort_section == by_alignment) | |
4009 | sec->spec.sorted = by_name_alignment; | |
4010 | break; | |
4011 | case by_alignment: | |
4012 | if (sort_section == by_name) | |
4013 | sec->spec.sorted = by_alignment_name; | |
4014 | break; | |
4015 | default: | |
4016 | break; | |
4017 | } | |
4018 | s->wild_statement.any_specs_sorted = true; | |
4019 | } | |
bcaa7b3e L |
4020 | break; |
4021 | ||
4022 | case lang_constructors_statement_enum: | |
4023 | update_wild_statements (constructor_list.head); | |
4024 | break; | |
4025 | ||
4026 | case lang_output_section_statement_enum: | |
2d3181c7 AM |
4027 | update_wild_statements |
4028 | (s->output_section_statement.children.head); | |
bcaa7b3e L |
4029 | break; |
4030 | ||
4031 | case lang_group_statement_enum: | |
4032 | update_wild_statements (s->group_statement.children.head); | |
4033 | break; | |
4034 | } | |
4035 | } | |
4036 | break; | |
4037 | } | |
4038 | } | |
4039 | ||
396a2467 | 4040 | /* Open input files and attach to output sections. */ |
08da4cac | 4041 | |
252b5132 | 4042 | static void |
1579bae1 AM |
4043 | map_input_to_output_sections |
4044 | (lang_statement_union_type *s, const char *target, | |
afd7a018 | 4045 | lang_output_section_statement_type *os) |
252b5132 | 4046 | { |
1579bae1 | 4047 | for (; s != NULL; s = s->header.next) |
252b5132 | 4048 | { |
dfa7b0b8 AM |
4049 | lang_output_section_statement_type *tos; |
4050 | flagword flags; | |
c212f39d | 4051 | unsigned int type = 0; |
dfa7b0b8 | 4052 | |
252b5132 RH |
4053 | switch (s->header.type) |
4054 | { | |
252b5132 | 4055 | case lang_wild_statement_enum: |
afd7a018 | 4056 | wild (&s->wild_statement, target, os); |
abc6ab0a | 4057 | break; |
252b5132 RH |
4058 | case lang_constructors_statement_enum: |
4059 | map_input_to_output_sections (constructor_list.head, | |
4060 | target, | |
afd7a018 | 4061 | os); |
252b5132 RH |
4062 | break; |
4063 | case lang_output_section_statement_enum: | |
dfa7b0b8 | 4064 | tos = &s->output_section_statement; |
21401fc7 AM |
4065 | if (tos->constraint == ONLY_IF_RW |
4066 | || tos->constraint == ONLY_IF_RO) | |
0841712e | 4067 | { |
f38a2680 | 4068 | tos->all_input_readonly = true; |
dfa7b0b8 AM |
4069 | check_input_sections (tos->children.head, tos); |
4070 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) | |
21401fc7 | 4071 | tos->constraint = -1; |
0841712e | 4072 | } |
21401fc7 AM |
4073 | if (tos->constraint >= 0) |
4074 | map_input_to_output_sections (tos->children.head, | |
4075 | target, | |
4076 | tos); | |
a431bc2e AM |
4077 | break; |
4078 | case lang_output_statement_enum: | |
252b5132 RH |
4079 | break; |
4080 | case lang_target_statement_enum: | |
4081 | target = s->target_statement.target; | |
4082 | break; | |
4083 | case lang_group_statement_enum: | |
4084 | map_input_to_output_sections (s->group_statement.children.head, | |
4085 | target, | |
afd7a018 | 4086 | os); |
252b5132 | 4087 | break; |
384d938f NS |
4088 | case lang_data_statement_enum: |
4089 | /* Make sure that any sections mentioned in the expression | |
4090 | are initialized. */ | |
4091 | exp_init_os (s->data_statement.exp); | |
2e76e85a AM |
4092 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
4093 | these may be overridden by the script. */ | |
dfa7b0b8 AM |
4094 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
4095 | switch (os->sectype) | |
4096 | { | |
4097 | case normal_section: | |
4098 | case overlay_section: | |
7b243801 | 4099 | case first_overlay_section: |
dfa7b0b8 AM |
4100 | break; |
4101 | case noalloc_section: | |
4102 | flags = SEC_HAS_CONTENTS; | |
4103 | break; | |
6b86da53 LB |
4104 | case readonly_section: |
4105 | flags |= SEC_READONLY; | |
4106 | break; | |
c212f39d FS |
4107 | case typed_readonly_section: |
4108 | flags |= SEC_READONLY; | |
4109 | /* Fall through. */ | |
4110 | case type_section: | |
4111 | if (os->sectype_value->type.node_class == etree_name | |
4112 | && os->sectype_value->type.node_code == NAME) | |
4113 | { | |
4114 | const char *name = os->sectype_value->name.name; | |
4115 | if (strcmp (name, "SHT_PROGBITS") == 0) | |
4116 | type = SHT_PROGBITS; | |
4117 | else if (strcmp (name, "SHT_STRTAB") == 0) | |
4118 | type = SHT_STRTAB; | |
4119 | else if (strcmp (name, "SHT_NOTE") == 0) | |
4120 | type = SHT_NOTE; | |
4121 | else if (strcmp (name, "SHT_NOBITS") == 0) | |
4122 | type = SHT_NOBITS; | |
4123 | else if (strcmp (name, "SHT_INIT_ARRAY") == 0) | |
4124 | type = SHT_INIT_ARRAY; | |
4125 | else if (strcmp (name, "SHT_FINI_ARRAY") == 0) | |
4126 | type = SHT_FINI_ARRAY; | |
4127 | else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0) | |
4128 | type = SHT_PREINIT_ARRAY; | |
4129 | else | |
4130 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4131 | os->name); | |
4132 | } | |
4133 | else | |
4134 | { | |
4135 | exp_fold_tree_no_dot (os->sectype_value); | |
4136 | if (expld.result.valid_p) | |
4137 | type = expld.result.value; | |
4138 | else | |
4139 | einfo (_ ("%F%P: invalid type for output section `%s'\n"), | |
4140 | os->name); | |
4141 | } | |
4142 | break; | |
dfa7b0b8 | 4143 | case noload_section: |
f4eaaf7f AM |
4144 | if (bfd_get_flavour (link_info.output_bfd) |
4145 | == bfd_target_elf_flavour) | |
4146 | flags = SEC_NEVER_LOAD | SEC_ALLOC; | |
4147 | else | |
4148 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; | |
dfa7b0b8 AM |
4149 | break; |
4150 | } | |
a431bc2e | 4151 | if (os->bfd_section == NULL) |
326adec3 | 4152 | init_os (os, flags | SEC_READONLY); |
a431bc2e AM |
4153 | else |
4154 | os->bfd_section->flags |= flags; | |
c212f39d | 4155 | os->bfd_section->type = type; |
afd7a018 | 4156 | break; |
252b5132 | 4157 | case lang_input_section_enum: |
e0f6802f AM |
4158 | break; |
4159 | case lang_fill_statement_enum: | |
252b5132 | 4160 | case lang_object_symbols_statement_enum: |
252b5132 RH |
4161 | case lang_reloc_statement_enum: |
4162 | case lang_padding_statement_enum: | |
4163 | case lang_input_statement_enum: | |
afd7a018 | 4164 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4165 | init_os (os, 0); |
252b5132 RH |
4166 | break; |
4167 | case lang_assignment_statement_enum: | |
afd7a018 | 4168 | if (os != NULL && os->bfd_section == NULL) |
dfa7b0b8 | 4169 | init_os (os, 0); |
252b5132 RH |
4170 | |
4171 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 4172 | are initialized. */ |
252b5132 RH |
4173 | exp_init_os (s->assignment_statement.exp); |
4174 | break; | |
252b5132 | 4175 | case lang_address_statement_enum: |
a431bc2e AM |
4176 | /* Mark the specified section with the supplied address. |
4177 | If this section was actually a segment marker, then the | |
4178 | directive is ignored if the linker script explicitly | |
4179 | processed the segment marker. Originally, the linker | |
4180 | treated segment directives (like -Ttext on the | |
4181 | command-line) as section directives. We honor the | |
370dfff4 | 4182 | section directive semantics for backwards compatibility; |
a431bc2e AM |
4183 | linker scripts that do not specifically check for |
4184 | SEGMENT_START automatically get the old semantics. */ | |
329c1c86 | 4185 | if (!s->address_statement.segment |
ba916c8a MM |
4186 | || !s->address_statement.segment->used) |
4187 | { | |
dfa7b0b8 AM |
4188 | const char *name = s->address_statement.section_name; |
4189 | ||
4190 | /* Create the output section statement here so that | |
4191 | orphans with a set address will be placed after other | |
4192 | script sections. If we let the orphan placement code | |
4193 | place them in amongst other sections then the address | |
4194 | will affect following script sections, which is | |
4195 | likely to surprise naive users. */ | |
21401fc7 | 4196 | tos = lang_output_section_statement_lookup (name, 0, 1); |
dfa7b0b8 AM |
4197 | tos->addr_tree = s->address_statement.address; |
4198 | if (tos->bfd_section == NULL) | |
4199 | init_os (tos, 0); | |
ba916c8a | 4200 | } |
252b5132 | 4201 | break; |
53d25da6 AM |
4202 | case lang_insert_statement_enum: |
4203 | break; | |
43ae96e9 MM |
4204 | case lang_input_matcher_enum: |
4205 | FAIL (); | |
53d25da6 AM |
4206 | } |
4207 | } | |
4208 | } | |
4209 | ||
4210 | /* An insert statement snips out all the linker statements from the | |
4211 | start of the list and places them after the output section | |
4212 | statement specified by the insert. This operation is complicated | |
4213 | by the fact that we keep a doubly linked list of output section | |
776ab89f AM |
4214 | statements as well as the singly linked list of all statements. |
4215 | FIXME someday: Twiddling with the list not only moves statements | |
4216 | from the user's script but also input and group statements that are | |
4217 | built from command line object files and --start-group. We only | |
4218 | get away with this because the list pointers used by file_chain | |
4219 | and input_file_chain are not reordered, and processing via | |
4220 | statement_list after this point mostly ignores input statements. | |
4221 | One exception is the map file, where LOAD and START GROUP/END GROUP | |
4222 | can end up looking odd. */ | |
53d25da6 AM |
4223 | |
4224 | static void | |
776ab89f | 4225 | process_insert_statements (lang_statement_union_type **start) |
53d25da6 AM |
4226 | { |
4227 | lang_statement_union_type **s; | |
4228 | lang_output_section_statement_type *first_os = NULL; | |
4229 | lang_output_section_statement_type *last_os = NULL; | |
66c103b7 | 4230 | lang_output_section_statement_type *os; |
53d25da6 | 4231 | |
776ab89f AM |
4232 | s = start; |
4233 | while (*s != NULL) | |
53d25da6 AM |
4234 | { |
4235 | if ((*s)->header.type == lang_output_section_statement_enum) | |
4236 | { | |
4237 | /* Keep pointers to the first and last output section | |
4238 | statement in the sequence we may be about to move. */ | |
d2ae7be0 AM |
4239 | os = &(*s)->output_section_statement; |
4240 | ||
4241 | ASSERT (last_os == NULL || last_os->next == os); | |
4242 | last_os = os; | |
66c103b7 AM |
4243 | |
4244 | /* Set constraint negative so that lang_output_section_find | |
4245 | won't match this output section statement. At this | |
4246 | stage in linking constraint has values in the range | |
4247 | [-1, ONLY_IN_RW]. */ | |
4248 | last_os->constraint = -2 - last_os->constraint; | |
53d25da6 AM |
4249 | if (first_os == NULL) |
4250 | first_os = last_os; | |
4251 | } | |
776ab89f AM |
4252 | else if ((*s)->header.type == lang_group_statement_enum) |
4253 | { | |
4254 | /* A user might put -T between --start-group and | |
4255 | --end-group. One way this odd construct might arise is | |
4256 | from a wrapper around ld to change library search | |
4257 | behaviour. For example: | |
4258 | #! /bin/sh | |
4259 | exec real_ld --start-group "$@" --end-group | |
4260 | This isn't completely unreasonable so go looking inside a | |
4261 | group statement for insert statements. */ | |
4262 | process_insert_statements (&(*s)->group_statement.children.head); | |
4263 | } | |
53d25da6 AM |
4264 | else if ((*s)->header.type == lang_insert_statement_enum) |
4265 | { | |
4266 | lang_insert_statement_type *i = &(*s)->insert_statement; | |
4267 | lang_output_section_statement_type *where; | |
53d25da6 AM |
4268 | lang_statement_union_type **ptr; |
4269 | lang_statement_union_type *first; | |
4270 | ||
abf874aa CL |
4271 | if (link_info.non_contiguous_regions) |
4272 | { | |
4273 | einfo (_("warning: INSERT statement in linker script is " | |
4274 | "incompatible with --enable-non-contiguous-regions.\n")); | |
4275 | } | |
4276 | ||
53d25da6 AM |
4277 | where = lang_output_section_find (i->where); |
4278 | if (where != NULL && i->is_before) | |
4279 | { | |
967928e9 | 4280 | do |
53d25da6 | 4281 | where = where->prev; |
66c103b7 | 4282 | while (where != NULL && where->constraint < 0); |
53d25da6 AM |
4283 | } |
4284 | if (where == NULL) | |
4285 | { | |
66c103b7 AM |
4286 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
4287 | return; | |
53d25da6 AM |
4288 | } |
4289 | ||
4290 | /* Deal with reordering the output section statement list. */ | |
4291 | if (last_os != NULL) | |
4292 | { | |
4293 | asection *first_sec, *last_sec; | |
29183214 | 4294 | struct lang_output_section_statement_struct **next; |
53d25da6 AM |
4295 | |
4296 | /* Snip out the output sections we are moving. */ | |
4297 | first_os->prev->next = last_os->next; | |
4298 | if (last_os->next == NULL) | |
29183214 L |
4299 | { |
4300 | next = &first_os->prev->next; | |
5c1e6d53 | 4301 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4302 | } |
53d25da6 AM |
4303 | else |
4304 | last_os->next->prev = first_os->prev; | |
4305 | /* Add them in at the new position. */ | |
4306 | last_os->next = where->next; | |
4307 | if (where->next == NULL) | |
29183214 L |
4308 | { |
4309 | next = &last_os->next; | |
5c1e6d53 | 4310 | lang_os_list.tail = (lang_statement_union_type **) next; |
29183214 | 4311 | } |
53d25da6 AM |
4312 | else |
4313 | where->next->prev = last_os; | |
4314 | first_os->prev = where; | |
4315 | where->next = first_os; | |
4316 | ||
4317 | /* Move the bfd sections in the same way. */ | |
4318 | first_sec = NULL; | |
4319 | last_sec = NULL; | |
4320 | for (os = first_os; os != NULL; os = os->next) | |
4321 | { | |
66c103b7 | 4322 | os->constraint = -2 - os->constraint; |
53d25da6 AM |
4323 | if (os->bfd_section != NULL |
4324 | && os->bfd_section->owner != NULL) | |
4325 | { | |
4326 | last_sec = os->bfd_section; | |
4327 | if (first_sec == NULL) | |
4328 | first_sec = last_sec; | |
4329 | } | |
4330 | if (os == last_os) | |
4331 | break; | |
4332 | } | |
4333 | if (last_sec != NULL) | |
4334 | { | |
4335 | asection *sec = where->bfd_section; | |
4336 | if (sec == NULL) | |
4337 | sec = output_prev_sec_find (where); | |
4338 | ||
4339 | /* The place we want to insert must come after the | |
4340 | sections we are moving. So if we find no | |
4341 | section or if the section is the same as our | |
4342 | last section, then no move is needed. */ | |
4343 | if (sec != NULL && sec != last_sec) | |
4344 | { | |
4345 | /* Trim them off. */ | |
4346 | if (first_sec->prev != NULL) | |
4347 | first_sec->prev->next = last_sec->next; | |
4348 | else | |
f13a99db | 4349 | link_info.output_bfd->sections = last_sec->next; |
53d25da6 AM |
4350 | if (last_sec->next != NULL) |
4351 | last_sec->next->prev = first_sec->prev; | |
4352 | else | |
f13a99db | 4353 | link_info.output_bfd->section_last = first_sec->prev; |
53d25da6 AM |
4354 | /* Add back. */ |
4355 | last_sec->next = sec->next; | |
4356 | if (sec->next != NULL) | |
4357 | sec->next->prev = last_sec; | |
4358 | else | |
f13a99db | 4359 | link_info.output_bfd->section_last = last_sec; |
53d25da6 AM |
4360 | first_sec->prev = sec; |
4361 | sec->next = first_sec; | |
4362 | } | |
4363 | } | |
4364 | ||
4365 | first_os = NULL; | |
4366 | last_os = NULL; | |
4367 | } | |
4368 | ||
4369 | ptr = insert_os_after (where); | |
776ab89f AM |
4370 | /* Snip everything from the start of the list, up to and |
4371 | including the insert statement we are currently processing. */ | |
4372 | first = *start; | |
4373 | *start = (*s)->header.next; | |
4374 | /* Add them back where they belong, minus the insert. */ | |
53d25da6 AM |
4375 | *s = *ptr; |
4376 | if (*s == NULL) | |
4377 | statement_list.tail = s; | |
4378 | *ptr = first; | |
776ab89f AM |
4379 | s = start; |
4380 | continue; | |
252b5132 | 4381 | } |
776ab89f | 4382 | s = &(*s)->header.next; |
252b5132 | 4383 | } |
66c103b7 AM |
4384 | |
4385 | /* Undo constraint twiddling. */ | |
4386 | for (os = first_os; os != NULL; os = os->next) | |
4387 | { | |
4388 | os->constraint = -2 - os->constraint; | |
4389 | if (os == last_os) | |
4390 | break; | |
4391 | } | |
252b5132 RH |
4392 | } |
4393 | ||
4bd5a393 AM |
4394 | /* An output section might have been removed after its statement was |
4395 | added. For example, ldemul_before_allocation can remove dynamic | |
4396 | sections if they turn out to be not needed. Clean them up here. */ | |
4397 | ||
8423293d | 4398 | void |
1579bae1 | 4399 | strip_excluded_output_sections (void) |
4bd5a393 | 4400 | { |
afd7a018 | 4401 | lang_output_section_statement_type *os; |
4bd5a393 | 4402 | |
046183de | 4403 | /* Run lang_size_sections (if not already done). */ |
e9ee469a AM |
4404 | if (expld.phase != lang_mark_phase_enum) |
4405 | { | |
4406 | expld.phase = lang_mark_phase_enum; | |
0f99513f | 4407 | expld.dataseg.phase = exp_seg_none; |
f38a2680 | 4408 | one_lang_size_sections_pass (NULL, false); |
e9ee469a AM |
4409 | lang_reset_memory_regions (); |
4410 | } | |
4411 | ||
8ce18f9c | 4412 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
4413 | os != NULL; |
4414 | os = os->next) | |
4bd5a393 | 4415 | { |
75ff4589 | 4416 | asection *output_section; |
f38a2680 | 4417 | bool exclude; |
4bd5a393 | 4418 | |
66c103b7 | 4419 | if (os->constraint < 0) |
0841712e | 4420 | continue; |
8423293d | 4421 | |
75ff4589 L |
4422 | output_section = os->bfd_section; |
4423 | if (output_section == NULL) | |
8423293d AM |
4424 | continue; |
4425 | ||
32124d5b AM |
4426 | exclude = (output_section->rawsize == 0 |
4427 | && (output_section->flags & SEC_KEEP) == 0 | |
f13a99db | 4428 | && !bfd_section_removed_from_list (link_info.output_bfd, |
32124d5b AM |
4429 | output_section)); |
4430 | ||
4431 | /* Some sections have not yet been sized, notably .gnu.version, | |
4432 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED | |
4433 | input sections, so don't drop output sections that have such | |
4434 | input sections unless they are also marked SEC_EXCLUDE. */ | |
4435 | if (exclude && output_section->map_head.s != NULL) | |
75ff4589 L |
4436 | { |
4437 | asection *s; | |
8423293d | 4438 | |
32124d5b | 4439 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
b514e6a5 AM |
4440 | if ((s->flags & SEC_EXCLUDE) == 0 |
4441 | && ((s->flags & SEC_LINKER_CREATED) != 0 | |
4442 | || link_info.emitrelocations)) | |
e9ee469a | 4443 | { |
f38a2680 | 4444 | exclude = false; |
e9ee469a AM |
4445 | break; |
4446 | } | |
75ff4589 | 4447 | } |
8423293d | 4448 | |
32124d5b | 4449 | if (exclude) |
4bd5a393 | 4450 | { |
e9ee469a AM |
4451 | /* We don't set bfd_section to NULL since bfd_section of the |
4452 | removed output section statement may still be used. */ | |
6d8bf25d | 4453 | if (!os->update_dot) |
f38a2680 | 4454 | os->ignored = true; |
e9ee469a | 4455 | output_section->flags |= SEC_EXCLUDE; |
f13a99db AM |
4456 | bfd_section_list_remove (link_info.output_bfd, output_section); |
4457 | link_info.output_bfd->section_count--; | |
4bd5a393 AM |
4458 | } |
4459 | } | |
18cd5bce AM |
4460 | } |
4461 | ||
4462 | /* Called from ldwrite to clear out asection.map_head and | |
211dc24b | 4463 | asection.map_tail for use as link_orders in ldwrite. */ |
18cd5bce AM |
4464 | |
4465 | void | |
4466 | lang_clear_os_map (void) | |
4467 | { | |
4468 | lang_output_section_statement_type *os; | |
4469 | ||
4470 | if (map_head_is_link_order) | |
4471 | return; | |
4472 | ||
8ce18f9c | 4473 | for (os = (void *) lang_os_list.head; |
18cd5bce AM |
4474 | os != NULL; |
4475 | os = os->next) | |
4476 | { | |
4477 | asection *output_section; | |
4478 | ||
4479 | if (os->constraint < 0) | |
4480 | continue; | |
4481 | ||
4482 | output_section = os->bfd_section; | |
4483 | if (output_section == NULL) | |
4484 | continue; | |
4485 | ||
4486 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ | |
4487 | output_section->map_head.link_order = NULL; | |
4488 | output_section->map_tail.link_order = NULL; | |
4489 | } | |
8423293d AM |
4490 | |
4491 | /* Stop future calls to lang_add_section from messing with map_head | |
4492 | and map_tail link_order fields. */ | |
f38a2680 | 4493 | map_head_is_link_order = true; |
4bd5a393 AM |
4494 | } |
4495 | ||
252b5132 | 4496 | static void |
1579bae1 AM |
4497 | print_output_section_statement |
4498 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
4499 | { |
4500 | asection *section = output_section_statement->bfd_section; | |
4501 | int len; | |
4502 | ||
4503 | if (output_section_statement != abs_output_section) | |
4504 | { | |
4505 | minfo ("\n%s", output_section_statement->name); | |
4506 | ||
4507 | if (section != NULL) | |
4508 | { | |
4509 | print_dot = section->vma; | |
4510 | ||
4511 | len = strlen (output_section_statement->name); | |
4512 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4513 | { | |
4514 | print_nl (); | |
4515 | len = 0; | |
4516 | } | |
6b9bd54c | 4517 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4518 | |
953dd97e | 4519 | minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); |
252b5132 | 4520 | |
67f744f3 AM |
4521 | if (section->vma != section->lma) |
4522 | minfo (_(" load address 0x%V"), section->lma); | |
5590fba9 NC |
4523 | |
4524 | if (output_section_statement->update_dot_tree != NULL) | |
4525 | exp_fold_tree (output_section_statement->update_dot_tree, | |
4526 | bfd_abs_section_ptr, &print_dot); | |
252b5132 RH |
4527 | } |
4528 | ||
4529 | print_nl (); | |
4530 | } | |
4531 | ||
4532 | print_statement_list (output_section_statement->children.head, | |
4533 | output_section_statement); | |
4534 | } | |
4535 | ||
4536 | static void | |
1579bae1 AM |
4537 | print_assignment (lang_assignment_statement_type *assignment, |
4538 | lang_output_section_statement_type *output_section) | |
252b5132 | 4539 | { |
f38a2680 | 4540 | bool is_dot; |
afd7a018 | 4541 | etree_type *tree; |
8658f989 | 4542 | asection *osec; |
252b5132 | 4543 | |
6b9bd54c | 4544 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4545 | |
afd7a018 AM |
4546 | if (assignment->exp->type.node_class == etree_assert) |
4547 | { | |
f38a2680 | 4548 | is_dot = false; |
afd7a018 AM |
4549 | tree = assignment->exp->assert_s.child; |
4550 | } | |
4551 | else | |
4552 | { | |
4553 | const char *dst = assignment->exp->assign.dst; | |
3b83e13a NC |
4554 | |
4555 | is_dot = (dst[0] == '.' && dst[1] == 0); | |
6a846243 | 4556 | tree = assignment->exp; |
afd7a018 AM |
4557 | } |
4558 | ||
8658f989 AM |
4559 | osec = output_section->bfd_section; |
4560 | if (osec == NULL) | |
4561 | osec = bfd_abs_section_ptr; | |
c05b575a AB |
4562 | |
4563 | if (assignment->exp->type.node_class != etree_provide) | |
4564 | exp_fold_tree (tree, osec, &print_dot); | |
4565 | else | |
f38a2680 | 4566 | expld.result.valid_p = false; |
c05b575a | 4567 | |
6b9bd54c AM |
4568 | char buf[32]; |
4569 | const char *str = buf; | |
e9ee469a | 4570 | if (expld.result.valid_p) |
7b17f854 | 4571 | { |
7b17f854 RS |
4572 | bfd_vma value; |
4573 | ||
4194268f AM |
4574 | if (assignment->exp->type.node_class == etree_assert |
4575 | || is_dot | |
4576 | || expld.assign_name != NULL) | |
3b83e13a | 4577 | { |
e9ee469a | 4578 | value = expld.result.value; |
10dbd1f3 | 4579 | |
7542af2a | 4580 | if (expld.result.section != NULL) |
e9ee469a | 4581 | value += expld.result.section->vma; |
7b17f854 | 4582 | |
6b9bd54c AM |
4583 | buf[0] = '0'; |
4584 | buf[1] = 'x'; | |
4585 | bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); | |
3b83e13a NC |
4586 | if (is_dot) |
4587 | print_dot = value; | |
4588 | } | |
4589 | else | |
4590 | { | |
4591 | struct bfd_link_hash_entry *h; | |
4592 | ||
4593 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, | |
f38a2680 | 4594 | false, false, true); |
6a846243 AM |
4595 | if (h != NULL |
4596 | && (h->type == bfd_link_hash_defined | |
4597 | || h->type == bfd_link_hash_defweak)) | |
3b83e13a | 4598 | { |
10dbd1f3 | 4599 | value = h->u.def.value; |
f37a7048 NS |
4600 | value += h->u.def.section->output_section->vma; |
4601 | value += h->u.def.section->output_offset; | |
3b83e13a | 4602 | |
6b9bd54c AM |
4603 | buf[0] = '['; |
4604 | buf[1] = '0'; | |
4605 | buf[2] = 'x'; | |
4606 | bfd_sprintf_vma (link_info.output_bfd, buf + 3, value); | |
4607 | strcat (buf, "]"); | |
3b83e13a NC |
4608 | } |
4609 | else | |
6b9bd54c | 4610 | str = "[unresolved]"; |
3b83e13a | 4611 | } |
7b17f854 | 4612 | } |
252b5132 RH |
4613 | else |
4614 | { | |
c05b575a | 4615 | if (assignment->exp->type.node_class == etree_provide) |
6b9bd54c | 4616 | str = "[!provide]"; |
c05b575a | 4617 | else |
6b9bd54c | 4618 | str = "*undef*"; |
252b5132 | 4619 | } |
4194268f | 4620 | expld.assign_name = NULL; |
252b5132 | 4621 | |
6b9bd54c | 4622 | fprintf (config.map_file, "%-34s", str); |
252b5132 | 4623 | exp_print_tree (assignment->exp); |
252b5132 RH |
4624 | print_nl (); |
4625 | } | |
4626 | ||
4627 | static void | |
1579bae1 | 4628 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 4629 | { |
0d41d9a2 | 4630 | if (statm->filename != NULL) |
906e58ca | 4631 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
252b5132 RH |
4632 | } |
4633 | ||
4634 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 4635 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 4636 | |
f38a2680 | 4637 | bool |
1579bae1 | 4638 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 4639 | { |
1e9cc1c2 | 4640 | asection *sec = (asection *) ptr; |
252b5132 RH |
4641 | |
4642 | if ((hash_entry->type == bfd_link_hash_defined | |
4643 | || hash_entry->type == bfd_link_hash_defweak) | |
4644 | && sec == hash_entry->u.def.section) | |
4645 | { | |
6b9bd54c | 4646 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 RH |
4647 | minfo ("0x%V ", |
4648 | (hash_entry->u.def.value | |
4649 | + hash_entry->u.def.section->output_offset | |
4650 | + hash_entry->u.def.section->output_section->vma)); | |
4651 | ||
c1c8c1ef | 4652 | minfo (" %pT\n", hash_entry->root.string); |
252b5132 RH |
4653 | } |
4654 | ||
f38a2680 | 4655 | return true; |
252b5132 RH |
4656 | } |
4657 | ||
02688209 TG |
4658 | static int |
4659 | hash_entry_addr_cmp (const void *a, const void *b) | |
4660 | { | |
4661 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; | |
4662 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; | |
4663 | ||
4664 | if (l->u.def.value < r->u.def.value) | |
4665 | return -1; | |
4666 | else if (l->u.def.value > r->u.def.value) | |
4667 | return 1; | |
4668 | else | |
4669 | return 0; | |
4670 | } | |
4671 | ||
35835446 | 4672 | static void |
e0f6802f | 4673 | print_all_symbols (asection *sec) |
35835446 | 4674 | { |
a48931cc | 4675 | input_section_userdata_type *ud = bfd_section_userdata (sec); |
35835446 | 4676 | struct map_symbol_def *def; |
02688209 TG |
4677 | struct bfd_link_hash_entry **entries; |
4678 | unsigned int i; | |
35835446 | 4679 | |
afd7a018 AM |
4680 | if (!ud) |
4681 | return; | |
4682 | ||
35835446 | 4683 | *ud->map_symbol_def_tail = 0; |
1e0061d2 | 4684 | |
02688209 | 4685 | /* Sort the symbols by address. */ |
1e9cc1c2 | 4686 | entries = (struct bfd_link_hash_entry **) |
0aa7f586 AM |
4687 | obstack_alloc (&map_obstack, |
4688 | ud->map_symbol_def_count * sizeof (*entries)); | |
02688209 TG |
4689 | |
4690 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) | |
4691 | entries[i] = def->entry; | |
4692 | ||
4693 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), | |
4724d37e | 4694 | hash_entry_addr_cmp); |
02688209 TG |
4695 | |
4696 | /* Print the symbols. */ | |
4697 | for (i = 0; i < ud->map_symbol_def_count; i++) | |
3edf7b9f | 4698 | ldemul_print_symbol (entries[i], sec); |
02688209 TG |
4699 | |
4700 | obstack_free (&map_obstack, entries); | |
35835446 JR |
4701 | } |
4702 | ||
252b5132 RH |
4703 | /* Print information about an input section to the map file. */ |
4704 | ||
4705 | static void | |
f38a2680 | 4706 | print_input_section (asection *i, bool is_discarded) |
252b5132 | 4707 | { |
eea6121a | 4708 | bfd_size_type size = i->size; |
abe6ac95 AM |
4709 | int len; |
4710 | bfd_vma addr; | |
e5caa5e0 | 4711 | |
61826503 | 4712 | init_opb (i); |
4d4920ec | 4713 | |
6b9bd54c | 4714 | minfo (" %s", i->name); |
252b5132 | 4715 | |
abe6ac95 AM |
4716 | len = 1 + strlen (i->name); |
4717 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
4718 | { | |
4719 | print_nl (); | |
4720 | len = 0; | |
4721 | } | |
6b9bd54c | 4722 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 | 4723 | |
f13a99db AM |
4724 | if (i->output_section != NULL |
4725 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 AM |
4726 | addr = i->output_section->vma + i->output_offset; |
4727 | else | |
4728 | { | |
4729 | addr = print_dot; | |
d64703c6 TG |
4730 | if (!is_discarded) |
4731 | size = 0; | |
abe6ac95 | 4732 | } |
252b5132 | 4733 | |
6b9bd54c AM |
4734 | char buf[32]; |
4735 | bfd_sprintf_vma (link_info.output_bfd, buf, addr); | |
4736 | minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner); | |
252b5132 | 4737 | |
abe6ac95 AM |
4738 | if (size != i->rawsize && i->rawsize != 0) |
4739 | { | |
6b9bd54c AM |
4740 | len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); |
4741 | print_spaces (len); | |
b0a7971a | 4742 | minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); |
abe6ac95 | 4743 | } |
252b5132 | 4744 | |
f13a99db AM |
4745 | if (i->output_section != NULL |
4746 | && i->output_section->owner == link_info.output_bfd) | |
abe6ac95 | 4747 | { |
c0f00686 | 4748 | if (link_info.reduce_memory_overheads) |
3edf7b9f | 4749 | bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); |
abe6ac95 AM |
4750 | else |
4751 | print_all_symbols (i); | |
4752 | ||
5590fba9 NC |
4753 | /* Update print_dot, but make sure that we do not move it |
4754 | backwards - this could happen if we have overlays and a | |
4755 | later overlay is shorter than an earier one. */ | |
4756 | if (addr + TO_ADDR (size) > print_dot) | |
4757 | print_dot = addr + TO_ADDR (size); | |
252b5132 RH |
4758 | } |
4759 | } | |
4760 | ||
4761 | static void | |
1579bae1 | 4762 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 4763 | { |
2c382fb6 AM |
4764 | size_t size; |
4765 | unsigned char *p; | |
4766 | fputs (" FILL mask 0x", config.map_file); | |
4767 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
4768 | fprintf (config.map_file, "%02x", *p); | |
4769 | fputs ("\n", config.map_file); | |
252b5132 RH |
4770 | } |
4771 | ||
4772 | static void | |
1579bae1 | 4773 | print_data_statement (lang_data_statement_type *data) |
252b5132 | 4774 | { |
252b5132 RH |
4775 | bfd_vma addr; |
4776 | bfd_size_type size; | |
4777 | const char *name; | |
4778 | ||
61826503 | 4779 | init_opb (data->output_section); |
6b9bd54c | 4780 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4781 | |
7fabd029 | 4782 | addr = data->output_offset; |
252b5132 RH |
4783 | if (data->output_section != NULL) |
4784 | addr += data->output_section->vma; | |
4785 | ||
4786 | switch (data->type) | |
4787 | { | |
4788 | default: | |
4789 | abort (); | |
4790 | case BYTE: | |
4791 | size = BYTE_SIZE; | |
4792 | name = "BYTE"; | |
4793 | break; | |
4794 | case SHORT: | |
4795 | size = SHORT_SIZE; | |
4796 | name = "SHORT"; | |
4797 | break; | |
4798 | case LONG: | |
4799 | size = LONG_SIZE; | |
4800 | name = "LONG"; | |
4801 | break; | |
4802 | case QUAD: | |
4803 | size = QUAD_SIZE; | |
4804 | name = "QUAD"; | |
4805 | break; | |
4806 | case SQUAD: | |
4807 | size = QUAD_SIZE; | |
4808 | name = "SQUAD"; | |
4809 | break; | |
4810 | } | |
4811 | ||
953dd97e DG |
4812 | if (size < TO_SIZE ((unsigned) 1)) |
4813 | size = TO_SIZE ((unsigned) 1); | |
4814 | minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); | |
252b5132 RH |
4815 | |
4816 | if (data->exp->type.node_class != etree_value) | |
4817 | { | |
4818 | print_space (); | |
4819 | exp_print_tree (data->exp); | |
4820 | } | |
4821 | ||
4822 | print_nl (); | |
4823 | ||
e5caa5e0 | 4824 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
4825 | } |
4826 | ||
4827 | /* Print an address statement. These are generated by options like | |
4828 | -Ttext. */ | |
4829 | ||
4830 | static void | |
1579bae1 | 4831 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
4832 | { |
4833 | minfo (_("Address of section %s set to "), address->section_name); | |
4834 | exp_print_tree (address->address); | |
4835 | print_nl (); | |
4836 | } | |
4837 | ||
4838 | /* Print a reloc statement. */ | |
4839 | ||
4840 | static void | |
1579bae1 | 4841 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 | 4842 | { |
252b5132 RH |
4843 | bfd_vma addr; |
4844 | bfd_size_type size; | |
4845 | ||
61826503 | 4846 | init_opb (reloc->output_section); |
6b9bd54c | 4847 | print_spaces (SECTION_NAME_MAP_LENGTH); |
252b5132 | 4848 | |
7fabd029 | 4849 | addr = reloc->output_offset; |
252b5132 RH |
4850 | if (reloc->output_section != NULL) |
4851 | addr += reloc->output_section->vma; | |
4852 | ||
4853 | size = bfd_get_reloc_size (reloc->howto); | |
4854 | ||
953dd97e | 4855 | minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); |
252b5132 RH |
4856 | |
4857 | if (reloc->name != NULL) | |
4858 | minfo ("%s+", reloc->name); | |
4859 | else | |
4860 | minfo ("%s+", reloc->section->name); | |
4861 | ||
4862 | exp_print_tree (reloc->addend_exp); | |
4863 | ||
4864 | print_nl (); | |
4865 | ||
e5caa5e0 | 4866 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 4867 | } |
252b5132 RH |
4868 | |
4869 | static void | |
1579bae1 | 4870 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
4871 | { |
4872 | int len; | |
4873 | bfd_vma addr; | |
4874 | ||
61826503 | 4875 | init_opb (s->output_section); |
252b5132 RH |
4876 | minfo (" *fill*"); |
4877 | ||
4878 | len = sizeof " *fill*" - 1; | |
6b9bd54c | 4879 | print_spaces (SECTION_NAME_MAP_LENGTH - len); |
252b5132 RH |
4880 | |
4881 | addr = s->output_offset; | |
4882 | if (s->output_section != NULL) | |
4883 | addr += s->output_section->vma; | |
953dd97e | 4884 | minfo ("0x%V %W ", addr, TO_ADDR (s->size)); |
252b5132 | 4885 | |
2c382fb6 AM |
4886 | if (s->fill->size != 0) |
4887 | { | |
4888 | size_t size; | |
4889 | unsigned char *p; | |
4890 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
4891 | fprintf (config.map_file, "%02x", *p); | |
4892 | } | |
252b5132 RH |
4893 | |
4894 | print_nl (); | |
4895 | ||
e5caa5e0 | 4896 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
4897 | } |
4898 | ||
4899 | static void | |
1579bae1 AM |
4900 | print_wild_statement (lang_wild_statement_type *w, |
4901 | lang_output_section_statement_type *os) | |
252b5132 | 4902 | { |
b6bf44ba AM |
4903 | struct wildcard_list *sec; |
4904 | ||
252b5132 RH |
4905 | print_space (); |
4906 | ||
8f1732fc AB |
4907 | if (w->exclude_name_list) |
4908 | { | |
4909 | name_list *tmp; | |
4910 | minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); | |
4911 | for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) | |
6c19b93b | 4912 | minfo (" %s", tmp->name); |
8f1732fc AB |
4913 | minfo (") "); |
4914 | } | |
4915 | ||
252b5132 | 4916 | if (w->filenames_sorted) |
2b94abd4 | 4917 | minfo ("SORT_BY_NAME("); |
08da4cac | 4918 | if (w->filename != NULL) |
252b5132 RH |
4919 | minfo ("%s", w->filename); |
4920 | else | |
4921 | minfo ("*"); | |
4922 | if (w->filenames_sorted) | |
4923 | minfo (")"); | |
4924 | ||
4925 | minfo ("("); | |
b6bf44ba AM |
4926 | for (sec = w->section_list; sec; sec = sec->next) |
4927 | { | |
2b94abd4 AB |
4928 | int closing_paren = 0; |
4929 | ||
4930 | switch (sec->spec.sorted) | |
6c19b93b AM |
4931 | { |
4932 | case none: | |
4933 | break; | |
4934 | ||
4935 | case by_name: | |
4936 | minfo ("SORT_BY_NAME("); | |
4937 | closing_paren = 1; | |
4938 | break; | |
4939 | ||
4940 | case by_alignment: | |
4941 | minfo ("SORT_BY_ALIGNMENT("); | |
4942 | closing_paren = 1; | |
4943 | break; | |
4944 | ||
4945 | case by_name_alignment: | |
4946 | minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); | |
4947 | closing_paren = 2; | |
4948 | break; | |
4949 | ||
4950 | case by_alignment_name: | |
4951 | minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); | |
4952 | closing_paren = 2; | |
4953 | break; | |
4954 | ||
4955 | case by_none: | |
4956 | minfo ("SORT_NONE("); | |
4957 | closing_paren = 1; | |
4958 | break; | |
4959 | ||
4960 | case by_init_priority: | |
4961 | minfo ("SORT_BY_INIT_PRIORITY("); | |
4962 | closing_paren = 1; | |
4963 | break; | |
4964 | } | |
2b94abd4 | 4965 | |
b6bf44ba AM |
4966 | if (sec->spec.exclude_name_list != NULL) |
4967 | { | |
4968 | name_list *tmp; | |
34786259 | 4969 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 4970 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
4971 | minfo (" %s", tmp->name); |
4972 | minfo (") "); | |
b6bf44ba AM |
4973 | } |
4974 | if (sec->spec.name != NULL) | |
4975 | minfo ("%s", sec->spec.name); | |
4976 | else | |
4977 | minfo ("*"); | |
2b94abd4 | 4978 | for (;closing_paren > 0; closing_paren--) |
6c19b93b | 4979 | minfo (")"); |
34786259 AM |
4980 | if (sec->next) |
4981 | minfo (" "); | |
b6bf44ba | 4982 | } |
252b5132 RH |
4983 | minfo (")"); |
4984 | ||
4985 | print_nl (); | |
4986 | ||
4987 | print_statement_list (w->children.head, os); | |
4988 | } | |
4989 | ||
4990 | /* Print a group statement. */ | |
4991 | ||
4992 | static void | |
1579bae1 AM |
4993 | print_group (lang_group_statement_type *s, |
4994 | lang_output_section_statement_type *os) | |
252b5132 RH |
4995 | { |
4996 | fprintf (config.map_file, "START GROUP\n"); | |
4997 | print_statement_list (s->children.head, os); | |
4998 | fprintf (config.map_file, "END GROUP\n"); | |
4999 | } | |
5000 | ||
5001 | /* Print the list of statements in S. | |
5002 | This can be called for any statement type. */ | |
5003 | ||
5004 | static void | |
1579bae1 AM |
5005 | print_statement_list (lang_statement_union_type *s, |
5006 | lang_output_section_statement_type *os) | |
252b5132 RH |
5007 | { |
5008 | while (s != NULL) | |
5009 | { | |
5010 | print_statement (s, os); | |
bba1a0c0 | 5011 | s = s->header.next; |
252b5132 RH |
5012 | } |
5013 | } | |
5014 | ||
5015 | /* Print the first statement in statement list S. | |
5016 | This can be called for any statement type. */ | |
5017 | ||
5018 | static void | |
1579bae1 AM |
5019 | print_statement (lang_statement_union_type *s, |
5020 | lang_output_section_statement_type *os) | |
252b5132 RH |
5021 | { |
5022 | switch (s->header.type) | |
5023 | { | |
5024 | default: | |
5025 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
5026 | FAIL (); | |
5027 | break; | |
5028 | case lang_constructors_statement_enum: | |
5029 | if (constructor_list.head != NULL) | |
5030 | { | |
5031 | if (constructors_sorted) | |
5032 | minfo (" SORT (CONSTRUCTORS)\n"); | |
5033 | else | |
5034 | minfo (" CONSTRUCTORS\n"); | |
5035 | print_statement_list (constructor_list.head, os); | |
5036 | } | |
5037 | break; | |
5038 | case lang_wild_statement_enum: | |
5039 | print_wild_statement (&s->wild_statement, os); | |
5040 | break; | |
5041 | case lang_address_statement_enum: | |
5042 | print_address_statement (&s->address_statement); | |
5043 | break; | |
5044 | case lang_object_symbols_statement_enum: | |
5045 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
5046 | break; | |
5047 | case lang_fill_statement_enum: | |
5048 | print_fill_statement (&s->fill_statement); | |
5049 | break; | |
5050 | case lang_data_statement_enum: | |
5051 | print_data_statement (&s->data_statement); | |
5052 | break; | |
5053 | case lang_reloc_statement_enum: | |
5054 | print_reloc_statement (&s->reloc_statement); | |
5055 | break; | |
5056 | case lang_input_section_enum: | |
f38a2680 | 5057 | print_input_section (s->input_section.section, false); |
252b5132 RH |
5058 | break; |
5059 | case lang_padding_statement_enum: | |
5060 | print_padding_statement (&s->padding_statement); | |
5061 | break; | |
5062 | case lang_output_section_statement_enum: | |
5063 | print_output_section_statement (&s->output_section_statement); | |
5064 | break; | |
5065 | case lang_assignment_statement_enum: | |
5066 | print_assignment (&s->assignment_statement, os); | |
5067 | break; | |
5068 | case lang_target_statement_enum: | |
5069 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
5070 | break; | |
5071 | case lang_output_statement_enum: | |
5072 | minfo ("OUTPUT(%s", s->output_statement.name); | |
5073 | if (output_target != NULL) | |
5074 | minfo (" %s", output_target); | |
5075 | minfo (")\n"); | |
5076 | break; | |
5077 | case lang_input_statement_enum: | |
5078 | print_input_statement (&s->input_statement); | |
5079 | break; | |
5080 | case lang_group_statement_enum: | |
5081 | print_group (&s->group_statement, os); | |
5082 | break; | |
53d25da6 AM |
5083 | case lang_insert_statement_enum: |
5084 | minfo ("INSERT %s %s\n", | |
5085 | s->insert_statement.is_before ? "BEFORE" : "AFTER", | |
5086 | s->insert_statement.where); | |
5087 | break; | |
252b5132 RH |
5088 | } |
5089 | } | |
5090 | ||
5091 | static void | |
1579bae1 | 5092 | print_statements (void) |
252b5132 RH |
5093 | { |
5094 | print_statement_list (statement_list.head, abs_output_section); | |
5095 | } | |
5096 | ||
5097 | /* Print the first N statements in statement list S to STDERR. | |
5098 | If N == 0, nothing is printed. | |
5099 | If N < 0, the entire list is printed. | |
5100 | Intended to be called from GDB. */ | |
5101 | ||
5102 | void | |
1579bae1 | 5103 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
5104 | { |
5105 | FILE *map_save = config.map_file; | |
5106 | ||
5107 | config.map_file = stderr; | |
5108 | ||
5109 | if (n < 0) | |
5110 | print_statement_list (s, abs_output_section); | |
5111 | else | |
5112 | { | |
5113 | while (s && --n >= 0) | |
5114 | { | |
5115 | print_statement (s, abs_output_section); | |
bba1a0c0 | 5116 | s = s->header.next; |
252b5132 RH |
5117 | } |
5118 | } | |
5119 | ||
5120 | config.map_file = map_save; | |
5121 | } | |
5122 | ||
b3327aad | 5123 | static void |
1579bae1 AM |
5124 | insert_pad (lang_statement_union_type **ptr, |
5125 | fill_type *fill, | |
1a69ff95 | 5126 | bfd_size_type alignment_needed, |
1579bae1 AM |
5127 | asection *output_section, |
5128 | bfd_vma dot) | |
252b5132 | 5129 | { |
b7761f11 | 5130 | static fill_type zero_fill; |
e9ee469a | 5131 | lang_statement_union_type *pad = NULL; |
b3327aad | 5132 | |
e9ee469a AM |
5133 | if (ptr != &statement_list.head) |
5134 | pad = ((lang_statement_union_type *) | |
5135 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); | |
5136 | if (pad != NULL | |
5137 | && pad->header.type == lang_padding_statement_enum | |
5138 | && pad->padding_statement.output_section == output_section) | |
5139 | { | |
5140 | /* Use the existing pad statement. */ | |
5141 | } | |
5142 | else if ((pad = *ptr) != NULL | |
906e58ca NC |
5143 | && pad->header.type == lang_padding_statement_enum |
5144 | && pad->padding_statement.output_section == output_section) | |
252b5132 | 5145 | { |
e9ee469a | 5146 | /* Use the existing pad statement. */ |
252b5132 | 5147 | } |
b3327aad | 5148 | else |
252b5132 | 5149 | { |
b3327aad | 5150 | /* Make a new padding statement, linked into existing chain. */ |
988de25b | 5151 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
5152 | pad->header.next = *ptr; |
5153 | *ptr = pad; | |
5154 | pad->header.type = lang_padding_statement_enum; | |
5155 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 5156 | if (fill == NULL) |
2c382fb6 | 5157 | fill = &zero_fill; |
b3327aad | 5158 | pad->padding_statement.fill = fill; |
252b5132 | 5159 | } |
b3327aad AM |
5160 | pad->padding_statement.output_offset = dot - output_section->vma; |
5161 | pad->padding_statement.size = alignment_needed; | |
8772de11 MR |
5162 | if (!(output_section->flags & SEC_FIXED_SIZE)) |
5163 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) | |
5164 | - output_section->vma); | |
252b5132 RH |
5165 | } |
5166 | ||
08da4cac KH |
5167 | /* Work out how much this section will move the dot point. */ |
5168 | ||
252b5132 | 5169 | static bfd_vma |
6feb9908 AM |
5170 | size_input_section |
5171 | (lang_statement_union_type **this_ptr, | |
5172 | lang_output_section_statement_type *output_section_statement, | |
5173 | fill_type *fill, | |
f38a2680 | 5174 | bool *removed, |
6feb9908 | 5175 | bfd_vma dot) |
252b5132 RH |
5176 | { |
5177 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
5178 | asection *i = is->section; | |
93d1b056 | 5179 | asection *o = output_section_statement->bfd_section; |
abf874aa CL |
5180 | *removed = 0; |
5181 | ||
5182 | if (link_info.non_contiguous_regions) | |
5183 | { | |
5184 | /* If the input section I has already been successfully assigned | |
5185 | to an output section other than O, don't bother with it and | |
5186 | let the caller remove it from the list. Keep processing in | |
5187 | case we have already handled O, because the repeated passes | |
5188 | have reinitialized its size. */ | |
5189 | if (i->already_assigned && i->already_assigned != o) | |
5190 | { | |
5191 | *removed = 1; | |
5192 | return dot; | |
5193 | } | |
5194 | } | |
252b5132 | 5195 | |
93d1b056 AM |
5196 | if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) |
5197 | i->output_offset = i->vma - o->vma; | |
9ef7906f IK |
5198 | else if (((i->flags & SEC_EXCLUDE) != 0) |
5199 | || output_section_statement->ignored) | |
93d1b056 AM |
5200 | i->output_offset = dot - o->vma; |
5201 | else | |
252b5132 | 5202 | { |
1a69ff95 | 5203 | bfd_size_type alignment_needed; |
b3327aad AM |
5204 | |
5205 | /* Align this section first to the input sections requirement, | |
5206 | then to the output section's requirement. If this alignment | |
5207 | is greater than any seen before, then record it too. Perform | |
5208 | the alignment by inserting a magic 'padding' statement. */ | |
5209 | ||
3d9c8f6b AM |
5210 | if (output_section_statement->subsection_alignment != NULL) |
5211 | i->alignment_power | |
5212 | = exp_get_power (output_section_statement->subsection_alignment, | |
5213 | "subsection alignment"); | |
b3327aad | 5214 | |
b3327aad AM |
5215 | if (o->alignment_power < i->alignment_power) |
5216 | o->alignment_power = i->alignment_power; | |
252b5132 | 5217 | |
b3327aad AM |
5218 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
5219 | ||
5220 | if (alignment_needed != 0) | |
5221 | { | |
e5caa5e0 | 5222 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
5223 | dot += alignment_needed; |
5224 | } | |
252b5132 | 5225 | |
abf874aa CL |
5226 | if (link_info.non_contiguous_regions) |
5227 | { | |
5228 | /* If I would overflow O, let the caller remove I from the | |
5229 | list. */ | |
5230 | if (output_section_statement->region) | |
5231 | { | |
5232 | bfd_vma end = output_section_statement->region->origin | |
5233 | + output_section_statement->region->length; | |
5234 | ||
5235 | if (dot + TO_ADDR (i->size) > end) | |
5236 | { | |
5237 | if (i->flags & SEC_LINKER_CREATED) | |
53215f21 CL |
5238 | einfo (_("%F%P: Output section '%s' not large enough for the " |
5239 | "linker-created stubs section '%s'.\n"), | |
5240 | i->output_section->name, i->name); | |
abf874aa CL |
5241 | |
5242 | if (i->rawsize && i->rawsize != i->size) | |
53215f21 CL |
5243 | einfo (_("%F%P: Relaxation not supported with " |
5244 | "--enable-non-contiguous-regions (section '%s' " | |
5245 | "would overflow '%s' after it changed size).\n"), | |
5246 | i->name, i->output_section->name); | |
abf874aa CL |
5247 | |
5248 | *removed = 1; | |
5249 | dot = end; | |
5250 | i->output_section = NULL; | |
5251 | return dot; | |
5252 | } | |
5253 | } | |
5254 | } | |
5255 | ||
08da4cac | 5256 | /* Remember where in the output section this input section goes. */ |
b3327aad | 5257 | i->output_offset = dot - o->vma; |
252b5132 | 5258 | |
08da4cac | 5259 | /* Mark how big the output section must be to contain this now. */ |
eea6121a | 5260 | dot += TO_ADDR (i->size); |
8772de11 MR |
5261 | if (!(o->flags & SEC_FIXED_SIZE)) |
5262 | o->size = TO_SIZE (dot - o->vma); | |
abf874aa CL |
5263 | |
5264 | if (link_info.non_contiguous_regions) | |
5265 | { | |
5266 | /* Record that I was successfully assigned to O, and update | |
5267 | its actual output section too. */ | |
5268 | i->already_assigned = o; | |
5269 | i->output_section = o; | |
5270 | } | |
252b5132 | 5271 | } |
252b5132 RH |
5272 | |
5273 | return dot; | |
5274 | } | |
5275 | ||
a87dd97a AM |
5276 | struct check_sec |
5277 | { | |
5278 | asection *sec; | |
f38a2680 | 5279 | bool warned; |
a87dd97a AM |
5280 | }; |
5281 | ||
5daa8fe7 L |
5282 | static int |
5283 | sort_sections_by_lma (const void *arg1, const void *arg2) | |
5284 | { | |
a87dd97a AM |
5285 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; |
5286 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5287 | ||
5288 | if (sec1->lma < sec2->lma) | |
5289 | return -1; | |
5290 | else if (sec1->lma > sec2->lma) | |
5291 | return 1; | |
5292 | else if (sec1->id < sec2->id) | |
5293 | return -1; | |
5294 | else if (sec1->id > sec2->id) | |
5295 | return 1; | |
5296 | ||
5297 | return 0; | |
5298 | } | |
5299 | ||
5300 | static int | |
5301 | sort_sections_by_vma (const void *arg1, const void *arg2) | |
5302 | { | |
5303 | const asection *sec1 = ((const struct check_sec *) arg1)->sec; | |
5304 | const asection *sec2 = ((const struct check_sec *) arg2)->sec; | |
5daa8fe7 | 5305 | |
a87dd97a | 5306 | if (sec1->vma < sec2->vma) |
5daa8fe7 | 5307 | return -1; |
a87dd97a | 5308 | else if (sec1->vma > sec2->vma) |
5daa8fe7 | 5309 | return 1; |
7f6a71ff JM |
5310 | else if (sec1->id < sec2->id) |
5311 | return -1; | |
5312 | else if (sec1->id > sec2->id) | |
5313 | return 1; | |
5daa8fe7 L |
5314 | |
5315 | return 0; | |
5316 | } | |
5317 | ||
2e4a7aea AM |
5318 | #define IS_TBSS(s) \ |
5319 | ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) | |
5320 | ||
eea6121a | 5321 | #define IGNORE_SECTION(s) \ |
2e4a7aea | 5322 | ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) |
d1778b88 | 5323 | |
252b5132 | 5324 | /* Check to see if any allocated sections overlap with other allocated |
afd7a018 | 5325 | sections. This can happen if a linker script specifies the output |
20e56351 DJ |
5326 | section addresses of the two sections. Also check whether any memory |
5327 | region has overflowed. */ | |
08da4cac | 5328 | |
252b5132 | 5329 | static void |
1579bae1 | 5330 | lang_check_section_addresses (void) |
252b5132 | 5331 | { |
f4427a75 | 5332 | asection *s, *p; |
a87dd97a AM |
5333 | struct check_sec *sections; |
5334 | size_t i, count; | |
d40e34db | 5335 | bfd_vma addr_mask; |
5daa8fe7 L |
5336 | bfd_vma s_start; |
5337 | bfd_vma s_end; | |
a87dd97a AM |
5338 | bfd_vma p_start = 0; |
5339 | bfd_vma p_end = 0; | |
20e56351 | 5340 | lang_memory_region_type *m; |
f38a2680 | 5341 | bool overlays; |
5daa8fe7 | 5342 | |
21701718 | 5343 | /* Detect address space overflow on allocated sections. */ |
d40e34db TG |
5344 | addr_mask = ((bfd_vma) 1 << |
5345 | (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; | |
5346 | addr_mask = (addr_mask << 1) + 1; | |
5347 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) | |
21701718 TG |
5348 | if ((s->flags & SEC_ALLOC) != 0) |
5349 | { | |
5350 | s_end = (s->vma + s->size) & addr_mask; | |
5351 | if (s_end != 0 && s_end < (s->vma & addr_mask)) | |
5352 | einfo (_("%X%P: section %s VMA wraps around address space\n"), | |
5353 | s->name); | |
5354 | else | |
5355 | { | |
5356 | s_end = (s->lma + s->size) & addr_mask; | |
5357 | if (s_end != 0 && s_end < (s->lma & addr_mask)) | |
5358 | einfo (_("%X%P: section %s LMA wraps around address space\n"), | |
5359 | s->name); | |
5360 | } | |
5361 | } | |
d40e34db | 5362 | |
f13a99db | 5363 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
5daa8fe7 L |
5364 | return; |
5365 | ||
a87dd97a AM |
5366 | count = bfd_count_sections (link_info.output_bfd); |
5367 | sections = XNEWVEC (struct check_sec, count); | |
252b5132 RH |
5368 | |
5369 | /* Scan all sections in the output list. */ | |
5daa8fe7 | 5370 | count = 0; |
f13a99db | 5371 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
33275c22 | 5372 | { |
a87dd97a | 5373 | if (IGNORE_SECTION (s) |
ec6d26be | 5374 | || s->size == 0) |
33275c22 | 5375 | continue; |
5f992e62 | 5376 | |
a87dd97a | 5377 | sections[count].sec = s; |
f38a2680 | 5378 | sections[count].warned = false; |
5daa8fe7 L |
5379 | count++; |
5380 | } | |
329c1c86 | 5381 | |
5daa8fe7 | 5382 | if (count <= 1) |
136a43b7 AM |
5383 | { |
5384 | free (sections); | |
5385 | return; | |
5386 | } | |
5f992e62 | 5387 | |
a87dd97a | 5388 | qsort (sections, count, sizeof (*sections), sort_sections_by_lma); |
5f992e62 | 5389 | |
136a43b7 | 5390 | /* First check section LMAs. There should be no overlap of LMAs on |
a87dd97a AM |
5391 | loadable sections, even with overlays. */ |
5392 | for (p = NULL, i = 0; i < count; i++) | |
5daa8fe7 | 5393 | { |
a87dd97a | 5394 | s = sections[i].sec; |
61826503 | 5395 | init_opb (s); |
a87dd97a AM |
5396 | if ((s->flags & SEC_LOAD) != 0) |
5397 | { | |
5398 | s_start = s->lma; | |
5399 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5400 | ||
5401 | /* Look for an overlap. We have sorted sections by lma, so | |
5402 | we know that s_start >= p_start. Besides the obvious | |
5403 | case of overlap when the current section starts before | |
5404 | the previous one ends, we also must have overlap if the | |
5405 | previous section wraps around the address space. */ | |
5406 | if (p != NULL | |
5407 | && (s_start <= p_end | |
5408 | || p_end < p_start)) | |
5409 | { | |
5410 | einfo (_("%X%P: section %s LMA [%V,%V]" | |
5411 | " overlaps section %s LMA [%V,%V]\n"), | |
5412 | s->name, s_start, s_end, p->name, p_start, p_end); | |
f38a2680 | 5413 | sections[i].warned = true; |
a87dd97a AM |
5414 | } |
5415 | p = s; | |
5416 | p_start = s_start; | |
5417 | p_end = s_end; | |
5418 | } | |
5419 | } | |
5420 | ||
136a43b7 AM |
5421 | /* If any non-zero size allocated section (excluding tbss) starts at |
5422 | exactly the same VMA as another such section, then we have | |
5423 | overlays. Overlays generated by the OVERLAY keyword will have | |
5424 | this property. It is possible to intentionally generate overlays | |
5425 | that fail this test, but it would be unusual. */ | |
5426 | qsort (sections, count, sizeof (*sections), sort_sections_by_vma); | |
f38a2680 | 5427 | overlays = false; |
136a43b7 AM |
5428 | p_start = sections[0].sec->vma; |
5429 | for (i = 1; i < count; i++) | |
a87dd97a | 5430 | { |
136a43b7 AM |
5431 | s_start = sections[i].sec->vma; |
5432 | if (p_start == s_start) | |
5433 | { | |
f38a2680 | 5434 | overlays = true; |
136a43b7 AM |
5435 | break; |
5436 | } | |
5437 | p_start = s_start; | |
5438 | } | |
a87dd97a | 5439 | |
136a43b7 AM |
5440 | /* Now check section VMAs if no overlays were detected. */ |
5441 | if (!overlays) | |
5442 | { | |
a87dd97a AM |
5443 | for (p = NULL, i = 0; i < count; i++) |
5444 | { | |
5445 | s = sections[i].sec; | |
61826503 | 5446 | init_opb (s); |
a87dd97a AM |
5447 | s_start = s->vma; |
5448 | s_end = s_start + TO_ADDR (s->size) - 1; | |
5449 | ||
5450 | if (p != NULL | |
5451 | && !sections[i].warned | |
5452 | && (s_start <= p_end | |
5453 | || p_end < p_start)) | |
5454 | einfo (_("%X%P: section %s VMA [%V,%V]" | |
5455 | " overlaps section %s VMA [%V,%V]\n"), | |
5456 | s->name, s_start, s_end, p->name, p_start, p_end); | |
5457 | p = s; | |
5458 | p_start = s_start; | |
5459 | p_end = s_end; | |
5460 | } | |
33275c22 | 5461 | } |
5daa8fe7 L |
5462 | |
5463 | free (sections); | |
20e56351 DJ |
5464 | |
5465 | /* If any memory region has overflowed, report by how much. | |
5466 | We do not issue this diagnostic for regions that had sections | |
5467 | explicitly placed outside their bounds; os_region_check's | |
5468 | diagnostics are adequate for that case. | |
5469 | ||
5470 | FIXME: It is conceivable that m->current - (m->origin + m->length) | |
5471 | might overflow a 32-bit integer. There is, alas, no way to print | |
5472 | a bfd_vma quantity in decimal. */ | |
5473 | for (m = lang_memory_region_list; m; m = m->next) | |
5474 | if (m->had_full_message) | |
992a06ee AM |
5475 | { |
5476 | unsigned long over = m->current - (m->origin + m->length); | |
5477 | einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", | |
5478 | "%X%P: region `%s' overflowed by %lu bytes\n", | |
5479 | over), | |
5480 | m->name_list.name, over); | |
5481 | } | |
252b5132 RH |
5482 | } |
5483 | ||
562d3460 TW |
5484 | /* Make sure the new address is within the region. We explicitly permit the |
5485 | current address to be at the exact end of the region when the address is | |
5486 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 5487 | calculation wraps around. */ |
562d3460 TW |
5488 | |
5489 | static void | |
1579bae1 | 5490 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 5491 | lang_memory_region_type *region, |
1579bae1 | 5492 | etree_type *tree, |
91d6fa6a | 5493 | bfd_vma rbase) |
562d3460 TW |
5494 | { |
5495 | if ((region->current < region->origin | |
5496 | || (region->current - region->origin > region->length)) | |
5497 | && ((region->current != region->origin + region->length) | |
91d6fa6a | 5498 | || rbase == 0)) |
562d3460 | 5499 | { |
1579bae1 | 5500 | if (tree != NULL) |
b7a26f91 | 5501 | { |
871b3ab2 | 5502 | einfo (_("%X%P: address 0x%v of %pB section `%s'" |
4a93e180 | 5503 | " is not within region `%s'\n"), |
b7a26f91 KH |
5504 | region->current, |
5505 | os->bfd_section->owner, | |
5506 | os->bfd_section->name, | |
4a93e180 | 5507 | region->name_list.name); |
b7a26f91 | 5508 | } |
20e56351 | 5509 | else if (!region->had_full_message) |
b7a26f91 | 5510 | { |
f38a2680 | 5511 | region->had_full_message = true; |
20e56351 | 5512 | |
871b3ab2 | 5513 | einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), |
b7a26f91 | 5514 | os->bfd_section->owner, |
20e56351 | 5515 | os->bfd_section->name, |
4a93e180 | 5516 | region->name_list.name); |
b7a26f91 | 5517 | } |
562d3460 TW |
5518 | } |
5519 | } | |
5520 | ||
ed1794ee | 5521 | static void |
2085ee26 | 5522 | ldlang_check_relro_region (lang_statement_union_type *s) |
ed1794ee | 5523 | { |
2085ee26 AM |
5524 | seg_align_type *seg = &expld.dataseg; |
5525 | ||
ed1794ee L |
5526 | if (seg->relro == exp_seg_relro_start) |
5527 | { | |
5528 | if (!seg->relro_start_stat) | |
5529 | seg->relro_start_stat = s; | |
5530 | else | |
5531 | { | |
5532 | ASSERT (seg->relro_start_stat == s); | |
5533 | } | |
5534 | } | |
5535 | else if (seg->relro == exp_seg_relro_end) | |
5536 | { | |
5537 | if (!seg->relro_end_stat) | |
5538 | seg->relro_end_stat = s; | |
5539 | else | |
5540 | { | |
5541 | ASSERT (seg->relro_end_stat == s); | |
5542 | } | |
5543 | } | |
5544 | } | |
5545 | ||
252b5132 RH |
5546 | /* Set the sizes for all the output sections. */ |
5547 | ||
2d20f7bf | 5548 | static bfd_vma |
1579bae1 | 5549 | lang_size_sections_1 |
e535e147 | 5550 | (lang_statement_union_type **prev, |
1579bae1 | 5551 | lang_output_section_statement_type *output_section_statement, |
1579bae1 AM |
5552 | fill_type *fill, |
5553 | bfd_vma dot, | |
f38a2680 AM |
5554 | bool *relax, |
5555 | bool check_regions) | |
252b5132 | 5556 | { |
e535e147 | 5557 | lang_statement_union_type *s; |
abf874aa | 5558 | lang_statement_union_type *prev_s = NULL; |
f38a2680 | 5559 | bool removed_prev_s = false; |
e535e147 | 5560 | |
252b5132 | 5561 | /* Size up the sections from their constituent parts. */ |
abf874aa | 5562 | for (s = *prev; s != NULL; prev_s = s, s = s->header.next) |
252b5132 | 5563 | { |
f38a2680 | 5564 | bool removed = false; |
abf874aa | 5565 | |
252b5132 RH |
5566 | switch (s->header.type) |
5567 | { | |
5568 | case lang_output_section_statement_enum: | |
5569 | { | |
13075d04 | 5570 | bfd_vma newdot, after, dotdelta; |
d1778b88 | 5571 | lang_output_section_statement_type *os; |
cde9e0be | 5572 | lang_memory_region_type *r; |
c5b0a9ef | 5573 | int section_alignment = 0; |
252b5132 | 5574 | |
d1778b88 | 5575 | os = &s->output_section_statement; |
61826503 | 5576 | init_opb (os->bfd_section); |
8658f989 AM |
5577 | if (os->constraint == -1) |
5578 | break; | |
5579 | ||
fd68d03d AM |
5580 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
5581 | here, in lang_insert_orphan, or in the default linker scripts. | |
5582 | This is covering for coff backend linker bugs. See PR6945. */ | |
5583 | if (os->addr_tree == NULL | |
0e1862bb | 5584 | && bfd_link_relocatable (&link_info) |
fd68d03d AM |
5585 | && (bfd_get_flavour (link_info.output_bfd) |
5586 | == bfd_target_coff_flavour)) | |
eceda120 | 5587 | os->addr_tree = exp_intop (0); |
a5df8c84 AM |
5588 | if (os->addr_tree != NULL) |
5589 | { | |
a5df8c84 | 5590 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
a5df8c84 | 5591 | |
3bf9618b | 5592 | if (expld.result.valid_p) |
7542af2a AM |
5593 | { |
5594 | dot = expld.result.value; | |
5595 | if (expld.result.section != NULL) | |
5596 | dot += expld.result.section->vma; | |
5597 | } | |
3bf9618b | 5598 | else if (expld.phase != lang_mark_phase_enum) |
df5f2391 | 5599 | einfo (_("%F%P:%pS: non constant or forward reference" |
a5df8c84 | 5600 | " address expression for section %s\n"), |
dab69f68 | 5601 | os->addr_tree, os->name); |
a5df8c84 AM |
5602 | } |
5603 | ||
e9ee469a | 5604 | if (os->bfd_section == NULL) |
75ff4589 | 5605 | /* This section was removed or never actually created. */ |
252b5132 RH |
5606 | break; |
5607 | ||
5608 | /* If this is a COFF shared library section, use the size and | |
5609 | address from the input section. FIXME: This is COFF | |
5610 | specific; it would be cleaner if there were some other way | |
5611 | to do this, but nothing simple comes to mind. */ | |
f13a99db AM |
5612 | if (((bfd_get_flavour (link_info.output_bfd) |
5613 | == bfd_target_ecoff_flavour) | |
5614 | || (bfd_get_flavour (link_info.output_bfd) | |
5615 | == bfd_target_coff_flavour)) | |
ebe372c1 | 5616 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
252b5132 | 5617 | { |
08da4cac | 5618 | asection *input; |
252b5132 RH |
5619 | |
5620 | if (os->children.head == NULL | |
bba1a0c0 | 5621 | || os->children.head->header.next != NULL |
6feb9908 AM |
5622 | || (os->children.head->header.type |
5623 | != lang_input_section_enum)) | |
df5f2391 | 5624 | einfo (_("%X%P: internal error on COFF shared library" |
6feb9908 | 5625 | " section %s\n"), os->name); |
252b5132 RH |
5626 | |
5627 | input = os->children.head->input_section.section; | |
fd361982 AM |
5628 | bfd_set_section_vma (os->bfd_section, |
5629 | bfd_section_vma (input)); | |
8772de11 MR |
5630 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5631 | os->bfd_section->size = input->size; | |
252b5132 RH |
5632 | break; |
5633 | } | |
5634 | ||
a5df8c84 | 5635 | newdot = dot; |
13075d04 | 5636 | dotdelta = 0; |
252b5132 RH |
5637 | if (bfd_is_abs_section (os->bfd_section)) |
5638 | { | |
5639 | /* No matter what happens, an abs section starts at zero. */ | |
5640 | ASSERT (os->bfd_section->vma == 0); | |
5641 | } | |
5642 | else | |
5643 | { | |
1579bae1 | 5644 | if (os->addr_tree == NULL) |
252b5132 RH |
5645 | { |
5646 | /* No address specified for this section, get one | |
5647 | from the region specification. */ | |
1579bae1 | 5648 | if (os->region == NULL |
6feb9908 | 5649 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
4a93e180 NC |
5650 | && os->region->name_list.name[0] == '*' |
5651 | && strcmp (os->region->name_list.name, | |
6feb9908 | 5652 | DEFAULT_MEMORY_REGION) == 0)) |
252b5132 RH |
5653 | { |
5654 | os->region = lang_memory_default (os->bfd_section); | |
5655 | } | |
5656 | ||
5657 | /* If a loadable section is using the default memory | |
5658 | region, and some non default memory regions were | |
66184979 | 5659 | defined, issue an error message. */ |
f0636a44 AM |
5660 | if (!os->ignored |
5661 | && !IGNORE_SECTION (os->bfd_section) | |
0e1862bb | 5662 | && !bfd_link_relocatable (&link_info) |
cf888e70 | 5663 | && check_regions |
4a93e180 | 5664 | && strcmp (os->region->name_list.name, |
6feb9908 | 5665 | DEFAULT_MEMORY_REGION) == 0 |
252b5132 | 5666 | && lang_memory_region_list != NULL |
4a93e180 | 5667 | && (strcmp (lang_memory_region_list->name_list.name, |
a747ee4d | 5668 | DEFAULT_MEMORY_REGION) != 0 |
e9ee469a | 5669 | || lang_memory_region_list->next != NULL) |
baf09cba | 5670 | && lang_sizing_iteration == 1) |
66184979 NC |
5671 | { |
5672 | /* By default this is an error rather than just a | |
5673 | warning because if we allocate the section to the | |
5674 | default memory region we can end up creating an | |
07f3b6ad KH |
5675 | excessively large binary, or even seg faulting when |
5676 | attempting to perform a negative seek. See | |
6feb9908 | 5677 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
5678 | for an example of this. This behaviour can be |
5679 | overridden by the using the --no-check-sections | |
5680 | switch. */ | |
5681 | if (command_line.check_section_addresses) | |
df5f2391 | 5682 | einfo (_("%F%P: error: no memory region specified" |
6feb9908 | 5683 | " for loadable section `%s'\n"), |
fd361982 | 5684 | bfd_section_name (os->bfd_section)); |
66184979 | 5685 | else |
6feb9908 AM |
5686 | einfo (_("%P: warning: no memory region specified" |
5687 | " for loadable section `%s'\n"), | |
fd361982 | 5688 | bfd_section_name (os->bfd_section)); |
66184979 | 5689 | } |
252b5132 | 5690 | |
e9ee469a | 5691 | newdot = os->region->current; |
c5b0a9ef | 5692 | section_alignment = os->bfd_section->alignment_power; |
252b5132 | 5693 | } |
94b50910 | 5694 | else |
3d9c8f6b AM |
5695 | section_alignment = exp_get_power (os->section_alignment, |
5696 | "section alignment"); | |
5f992e62 | 5697 | |
7270c5ed | 5698 | /* Align to what the section needs. */ |
c5b0a9ef | 5699 | if (section_alignment > 0) |
94b50910 AM |
5700 | { |
5701 | bfd_vma savedot = newdot; | |
baf09cba | 5702 | bfd_vma diff = 0; |
252b5132 | 5703 | |
baf09cba | 5704 | newdot = align_power (newdot, section_alignment); |
13075d04 | 5705 | dotdelta = newdot - savedot; |
baf09cba AM |
5706 | |
5707 | if (lang_sizing_iteration == 1) | |
5708 | diff = dotdelta; | |
5709 | else if (lang_sizing_iteration > 1) | |
92d4b13b AM |
5710 | { |
5711 | /* Only report adjustments that would change | |
5712 | alignment from what we have already reported. */ | |
5713 | diff = newdot - os->bfd_section->vma; | |
5714 | if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) | |
5715 | diff = 0; | |
5716 | } | |
baf09cba | 5717 | if (diff != 0 |
94b50910 | 5718 | && (config.warn_section_align |
baf09cba AM |
5719 | || os->addr_tree != NULL)) |
5720 | einfo (_("%P: warning: " | |
5721 | "start of section %s changed by %ld\n"), | |
5722 | os->name, (long) diff); | |
94b50910 | 5723 | } |
252b5132 | 5724 | |
fd361982 | 5725 | bfd_set_section_vma (os->bfd_section, newdot); |
5f992e62 | 5726 | |
252b5132 RH |
5727 | os->bfd_section->output_offset = 0; |
5728 | } | |
5729 | ||
e535e147 | 5730 | lang_size_sections_1 (&os->children.head, os, |
e9ee469a AM |
5731 | os->fill, newdot, relax, check_regions); |
5732 | ||
f38a2680 | 5733 | os->processed_vma = true; |
e9ee469a AM |
5734 | |
5735 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) | |
2f475487 AM |
5736 | /* Except for some special linker created sections, |
5737 | no output section should change from zero size | |
5738 | after strip_excluded_output_sections. A non-zero | |
5739 | size on an ignored section indicates that some | |
5740 | input section was not sized early enough. */ | |
5741 | ASSERT (os->bfd_section->size == 0); | |
cde9e0be | 5742 | else |
e9ee469a | 5743 | { |
cde9e0be AM |
5744 | dot = os->bfd_section->vma; |
5745 | ||
5746 | /* Put the section within the requested block size, or | |
5747 | align at the block boundary. */ | |
5748 | after = ((dot | |
5749 | + TO_ADDR (os->bfd_section->size) | |
5750 | + os->block_value - 1) | |
5751 | & - (bfd_vma) os->block_value); | |
5752 | ||
8772de11 MR |
5753 | if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) |
5754 | os->bfd_section->size = TO_SIZE (after | |
5755 | - os->bfd_section->vma); | |
cde9e0be AM |
5756 | } |
5757 | ||
5758 | /* Set section lma. */ | |
5759 | r = os->region; | |
5760 | if (r == NULL) | |
f38a2680 | 5761 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); |
cde9e0be AM |
5762 | |
5763 | if (os->load_base) | |
5764 | { | |
5765 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); | |
5766 | os->bfd_section->lma = lma; | |
e9ee469a | 5767 | } |
07dfcf38 | 5768 | else if (os->lma_region != NULL) |
cde9e0be AM |
5769 | { |
5770 | bfd_vma lma = os->lma_region->current; | |
e9ee469a | 5771 | |
13075d04 SH |
5772 | if (os->align_lma_with_input) |
5773 | lma += dotdelta; | |
5774 | else | |
5775 | { | |
5776 | /* When LMA_REGION is the same as REGION, align the LMA | |
5777 | as we did for the VMA, possibly including alignment | |
5778 | from the bfd section. If a different region, then | |
5779 | only align according to the value in the output | |
5780 | statement. */ | |
5781 | if (os->lma_region != os->region) | |
3d9c8f6b AM |
5782 | section_alignment = exp_get_power (os->section_alignment, |
5783 | "section alignment"); | |
13075d04 SH |
5784 | if (section_alignment > 0) |
5785 | lma = align_power (lma, section_alignment); | |
5786 | } | |
cde9e0be AM |
5787 | os->bfd_section->lma = lma; |
5788 | } | |
dc0b6aa0 AM |
5789 | else if (r->last_os != NULL |
5790 | && (os->bfd_section->flags & SEC_ALLOC) != 0) | |
cde9e0be AM |
5791 | { |
5792 | bfd_vma lma; | |
5793 | asection *last; | |
5794 | ||
5795 | last = r->last_os->output_section_statement.bfd_section; | |
dc0b6aa0 AM |
5796 | |
5797 | /* A backwards move of dot should be accompanied by | |
5798 | an explicit assignment to the section LMA (ie. | |
91eb6c46 | 5799 | os->load_base set) because backwards moves can |
dc0b6aa0 | 5800 | create overlapping LMAs. */ |
264b6205 | 5801 | if (dot < last->vma |
91eb6c46 | 5802 | && os->bfd_section->size != 0 |
ca62bc4a | 5803 | && dot + TO_ADDR (os->bfd_section->size) <= last->vma) |
dc0b6aa0 | 5804 | { |
dc0b6aa0 AM |
5805 | /* If dot moved backwards then leave lma equal to |
5806 | vma. This is the old default lma, which might | |
5807 | just happen to work when the backwards move is | |
91eb6c46 AM |
5808 | sufficiently large. Nag if this changes anything, |
5809 | so people can fix their linker scripts. */ | |
5810 | ||
5811 | if (last->vma != last->lma) | |
0aa7f586 AM |
5812 | einfo (_("%P: warning: dot moved backwards " |
5813 | "before `%s'\n"), os->name); | |
dc0b6aa0 AM |
5814 | } |
5815 | else | |
cde9e0be | 5816 | { |
152d792f AM |
5817 | /* If this is an overlay, set the current lma to that |
5818 | at the end of the previous section. */ | |
5819 | if (os->sectype == overlay_section) | |
fc90c280 | 5820 | lma = last->lma + TO_ADDR (last->size); |
cde9e0be AM |
5821 | |
5822 | /* Otherwise, keep the same lma to vma relationship | |
5823 | as the previous section. */ | |
5824 | else | |
8610e0fd | 5825 | lma = os->bfd_section->vma + last->lma - last->vma; |
cde9e0be | 5826 | |
c5b0a9ef AM |
5827 | if (section_alignment > 0) |
5828 | lma = align_power (lma, section_alignment); | |
cde9e0be AM |
5829 | os->bfd_section->lma = lma; |
5830 | } | |
5831 | } | |
f38a2680 | 5832 | os->processed_lma = true; |
5f992e62 | 5833 | |
cde9e0be AM |
5834 | /* Keep track of normal sections using the default |
5835 | lma region. We use this to set the lma for | |
5836 | following sections. Overlays or other linker | |
5837 | script assignment to lma might mean that the | |
dc0b6aa0 AM |
5838 | default lma == vma is incorrect. |
5839 | To avoid warnings about dot moving backwards when using | |
5840 | -Ttext, don't start tracking sections until we find one | |
14ea2c1b AB |
5841 | of non-zero size or with lma set differently to vma. |
5842 | Do this tracking before we short-cut the loop so that we | |
5843 | track changes for the case where the section size is zero, | |
5844 | but the lma is set differently to the vma. This is | |
5845 | important, if an orphan section is placed after an | |
5846 | otherwise empty output section that has an explicit lma | |
5847 | set, we want that lma reflected in the orphans lma. */ | |
7b243801 AM |
5848 | if (((!IGNORE_SECTION (os->bfd_section) |
5849 | && (os->bfd_section->size != 0 | |
5850 | || (r->last_os == NULL | |
5851 | && os->bfd_section->vma != os->bfd_section->lma) | |
5852 | || (r->last_os != NULL | |
5853 | && dot >= (r->last_os->output_section_statement | |
5854 | .bfd_section->vma)))) | |
5855 | || os->sectype == first_overlay_section) | |
cde9e0be | 5856 | && os->lma_region == NULL |
0e1862bb | 5857 | && !bfd_link_relocatable (&link_info)) |
cde9e0be | 5858 | r->last_os = s; |
9f88b410 | 5859 | |
14ea2c1b AB |
5860 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
5861 | break; | |
5862 | ||
e5caec89 | 5863 | /* .tbss sections effectively have zero size. */ |
2e4a7aea | 5864 | if (!IS_TBSS (os->bfd_section) |
0e1862bb | 5865 | || bfd_link_relocatable (&link_info)) |
13075d04 SH |
5866 | dotdelta = TO_ADDR (os->bfd_section->size); |
5867 | else | |
5868 | dotdelta = 0; | |
5869 | dot += dotdelta; | |
e5caec89 | 5870 | |
9f88b410 | 5871 | if (os->update_dot_tree != 0) |
e9ee469a | 5872 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
9f88b410 | 5873 | |
252b5132 RH |
5874 | /* Update dot in the region ? |
5875 | We only do this if the section is going to be allocated, | |
5876 | since unallocated sections do not contribute to the region's | |
2e76e85a | 5877 | overall size in memory. */ |
1579bae1 | 5878 | if (os->region != NULL |
2e76e85a | 5879 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
252b5132 RH |
5880 | { |
5881 | os->region->current = dot; | |
5f992e62 | 5882 | |
cf888e70 NC |
5883 | if (check_regions) |
5884 | /* Make sure the new address is within the region. */ | |
5885 | os_region_check (os, os->region, os->addr_tree, | |
5886 | os->bfd_section->vma); | |
08da4cac | 5887 | |
a2cab753 | 5888 | if (os->lma_region != NULL && os->lma_region != os->region |
13075d04 SH |
5889 | && ((os->bfd_section->flags & SEC_LOAD) |
5890 | || os->align_lma_with_input)) | |
08da4cac | 5891 | { |
13075d04 | 5892 | os->lma_region->current = os->bfd_section->lma + dotdelta; |
66e28d60 | 5893 | |
cf888e70 NC |
5894 | if (check_regions) |
5895 | os_region_check (os, os->lma_region, NULL, | |
cde9e0be | 5896 | os->bfd_section->lma); |
08da4cac | 5897 | } |
252b5132 RH |
5898 | } |
5899 | } | |
5900 | break; | |
5901 | ||
5902 | case lang_constructors_statement_enum: | |
e535e147 | 5903 | dot = lang_size_sections_1 (&constructor_list.head, |
2d20f7bf | 5904 | output_section_statement, |
cf888e70 | 5905 | fill, dot, relax, check_regions); |
252b5132 RH |
5906 | break; |
5907 | ||
5908 | case lang_data_statement_enum: | |
5909 | { | |
5910 | unsigned int size = 0; | |
5911 | ||
7fabd029 | 5912 | s->data_statement.output_offset = |
08da4cac | 5913 | dot - output_section_statement->bfd_section->vma; |
252b5132 RH |
5914 | s->data_statement.output_section = |
5915 | output_section_statement->bfd_section; | |
5916 | ||
1b493742 NS |
5917 | /* We might refer to provided symbols in the expression, and |
5918 | need to mark them as needed. */ | |
e9ee469a | 5919 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
1b493742 | 5920 | |
252b5132 RH |
5921 | switch (s->data_statement.type) |
5922 | { | |
08da4cac KH |
5923 | default: |
5924 | abort (); | |
252b5132 RH |
5925 | case QUAD: |
5926 | case SQUAD: | |
5927 | size = QUAD_SIZE; | |
5928 | break; | |
5929 | case LONG: | |
5930 | size = LONG_SIZE; | |
5931 | break; | |
5932 | case SHORT: | |
5933 | size = SHORT_SIZE; | |
5934 | break; | |
5935 | case BYTE: | |
5936 | size = BYTE_SIZE; | |
5937 | break; | |
5938 | } | |
e5caa5e0 AM |
5939 | if (size < TO_SIZE ((unsigned) 1)) |
5940 | size = TO_SIZE ((unsigned) 1); | |
5941 | dot += TO_ADDR (size); | |
8772de11 MR |
5942 | if (!(output_section_statement->bfd_section->flags |
5943 | & SEC_FIXED_SIZE)) | |
5944 | output_section_statement->bfd_section->size | |
5945 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
aa4c3319 | 5946 | |
252b5132 RH |
5947 | } |
5948 | break; | |
5949 | ||
5950 | case lang_reloc_statement_enum: | |
5951 | { | |
5952 | int size; | |
5953 | ||
7fabd029 | 5954 | s->reloc_statement.output_offset = |
252b5132 RH |
5955 | dot - output_section_statement->bfd_section->vma; |
5956 | s->reloc_statement.output_section = | |
5957 | output_section_statement->bfd_section; | |
5958 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 5959 | dot += TO_ADDR (size); |
8772de11 MR |
5960 | if (!(output_section_statement->bfd_section->flags |
5961 | & SEC_FIXED_SIZE)) | |
5962 | output_section_statement->bfd_section->size | |
5963 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); | |
252b5132 RH |
5964 | } |
5965 | break; | |
5f992e62 | 5966 | |
252b5132 | 5967 | case lang_wild_statement_enum: |
e535e147 | 5968 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
2d20f7bf | 5969 | output_section_statement, |
cf888e70 | 5970 | fill, dot, relax, check_regions); |
252b5132 RH |
5971 | break; |
5972 | ||
5973 | case lang_object_symbols_statement_enum: | |
3b6c1966 AM |
5974 | link_info.create_object_symbols_section |
5975 | = output_section_statement->bfd_section; | |
5976 | output_section_statement->bfd_section->flags |= SEC_KEEP; | |
252b5132 | 5977 | break; |
e9ee469a | 5978 | |
252b5132 RH |
5979 | case lang_output_statement_enum: |
5980 | case lang_target_statement_enum: | |
5981 | break; | |
e9ee469a | 5982 | |
252b5132 RH |
5983 | case lang_input_section_enum: |
5984 | { | |
5985 | asection *i; | |
5986 | ||
e535e147 | 5987 | i = s->input_section.section; |
eea6121a | 5988 | if (relax) |
252b5132 | 5989 | { |
f38a2680 | 5990 | bool again; |
252b5132 | 5991 | |
0aa7f586 | 5992 | if (!bfd_relax_section (i->owner, i, &link_info, &again)) |
df5f2391 | 5993 | einfo (_("%F%P: can't relax section: %E\n")); |
252b5132 | 5994 | if (again) |
f38a2680 | 5995 | *relax = true; |
252b5132 | 5996 | } |
b3327aad | 5997 | dot = size_input_section (prev, output_section_statement, |
abf874aa | 5998 | fill, &removed, dot); |
252b5132 RH |
5999 | } |
6000 | break; | |
e9ee469a | 6001 | |
252b5132 RH |
6002 | case lang_input_statement_enum: |
6003 | break; | |
e9ee469a | 6004 | |
252b5132 | 6005 | case lang_fill_statement_enum: |
08da4cac KH |
6006 | s->fill_statement.output_section = |
6007 | output_section_statement->bfd_section; | |
252b5132 RH |
6008 | |
6009 | fill = s->fill_statement.fill; | |
6010 | break; | |
e9ee469a | 6011 | |
252b5132 RH |
6012 | case lang_assignment_statement_enum: |
6013 | { | |
6014 | bfd_vma newdot = dot; | |
49c13adb | 6015 | etree_type *tree = s->assignment_statement.exp; |
252b5132 | 6016 | |
0f99513f | 6017 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6018 | |
49c13adb | 6019 | exp_fold_tree (tree, |
408082ec | 6020 | output_section_statement->bfd_section, |
252b5132 RH |
6021 | &newdot); |
6022 | ||
2085ee26 | 6023 | ldlang_check_relro_region (s); |
ed1794ee | 6024 | |
0f99513f | 6025 | expld.dataseg.relro = exp_seg_relro_none; |
b10a8ae0 | 6026 | |
d2667025 | 6027 | /* This symbol may be relative to this section. */ |
a14a5de3 | 6028 | if ((tree->type.node_class == etree_provided |
49c13adb L |
6029 | || tree->type.node_class == etree_assign) |
6030 | && (tree->assign.dst [0] != '.' | |
6031 | || tree->assign.dst [1] != '\0')) | |
6d8bf25d | 6032 | output_section_statement->update_dot = 1; |
49c13adb | 6033 | |
85852e36 | 6034 | if (!output_section_statement->ignored) |
252b5132 | 6035 | { |
252b5132 RH |
6036 | if (output_section_statement == abs_output_section) |
6037 | { | |
6038 | /* If we don't have an output section, then just adjust | |
6039 | the default memory address. */ | |
6feb9908 | 6040 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
f38a2680 | 6041 | false)->current = newdot; |
252b5132 | 6042 | } |
85852e36 | 6043 | else if (newdot != dot) |
252b5132 | 6044 | { |
b3327aad AM |
6045 | /* Insert a pad after this statement. We can't |
6046 | put the pad before when relaxing, in case the | |
6047 | assignment references dot. */ | |
e5caa5e0 | 6048 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
6049 | output_section_statement->bfd_section, dot); |
6050 | ||
6051 | /* Don't neuter the pad below when relaxing. */ | |
6052 | s = s->header.next; | |
9dfc8ab2 | 6053 | |
e9ee469a AM |
6054 | /* If dot is advanced, this implies that the section |
6055 | should have space allocated to it, unless the | |
6056 | user has explicitly stated that the section | |
2e76e85a | 6057 | should not be allocated. */ |
f4eaaf7f AM |
6058 | if (output_section_statement->sectype != noalloc_section |
6059 | && (output_section_statement->sectype != noload_section | |
6060 | || (bfd_get_flavour (link_info.output_bfd) | |
6061 | == bfd_target_elf_flavour))) | |
e9ee469a AM |
6062 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
6063 | } | |
252b5132 RH |
6064 | dot = newdot; |
6065 | } | |
6066 | } | |
6067 | break; | |
6068 | ||
6069 | case lang_padding_statement_enum: | |
c0c330a7 AM |
6070 | /* If this is the first time lang_size_sections is called, |
6071 | we won't have any padding statements. If this is the | |
6072 | second or later passes when relaxing, we should allow | |
6073 | padding to shrink. If padding is needed on this pass, it | |
6074 | will be added back in. */ | |
6075 | s->padding_statement.size = 0; | |
6e814ff8 AM |
6076 | |
6077 | /* Make sure output_offset is valid. If relaxation shrinks | |
6078 | the section and this pad isn't needed, it's possible to | |
6079 | have output_offset larger than the final size of the | |
6080 | section. bfd_set_section_contents will complain even for | |
6081 | a pad size of zero. */ | |
6082 | s->padding_statement.output_offset | |
6083 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
6084 | break; |
6085 | ||
6086 | case lang_group_statement_enum: | |
e535e147 | 6087 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
2d20f7bf | 6088 | output_section_statement, |
cf888e70 | 6089 | fill, dot, relax, check_regions); |
252b5132 RH |
6090 | break; |
6091 | ||
53d25da6 | 6092 | case lang_insert_statement_enum: |
252b5132 RH |
6093 | break; |
6094 | ||
c0c330a7 | 6095 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
6096 | case lang_address_statement_enum: |
6097 | break; | |
53d25da6 AM |
6098 | |
6099 | default: | |
6100 | FAIL (); | |
6101 | break; | |
252b5132 | 6102 | } |
abf874aa CL |
6103 | |
6104 | /* If an input section doesn't fit in the current output | |
6105 | section, remove it from the list. Handle the case where we | |
6106 | have to remove an input_section statement here: there is a | |
6107 | special case to remove the first element of the list. */ | |
6108 | if (link_info.non_contiguous_regions && removed) | |
6109 | { | |
6110 | /* If we removed the first element during the previous | |
6111 | iteration, override the loop assignment of prev_s. */ | |
6112 | if (removed_prev_s) | |
6113 | prev_s = NULL; | |
6114 | ||
6115 | if (prev_s) | |
6116 | { | |
6117 | /* If there was a real previous input section, just skip | |
6118 | the current one. */ | |
6119 | prev_s->header.next=s->header.next; | |
6120 | s = prev_s; | |
f38a2680 | 6121 | removed_prev_s = false; |
abf874aa CL |
6122 | } |
6123 | else | |
6124 | { | |
6125 | /* Remove the first input section of the list. */ | |
6126 | *prev = s->header.next; | |
f38a2680 | 6127 | removed_prev_s = true; |
abf874aa CL |
6128 | } |
6129 | ||
6130 | /* Move to next element, unless we removed the head of the | |
6131 | list. */ | |
6132 | if (!removed_prev_s) | |
6133 | prev = &s->header.next; | |
6134 | } | |
6135 | else | |
6136 | { | |
6137 | prev = &s->header.next; | |
f38a2680 | 6138 | removed_prev_s = false; |
abf874aa | 6139 | } |
252b5132 RH |
6140 | } |
6141 | return dot; | |
6142 | } | |
6143 | ||
591a748a NC |
6144 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
6145 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that | |
6146 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different | |
6147 | segments. We are allowed an opportunity to override this decision. */ | |
2889e75b | 6148 | |
f38a2680 | 6149 | bool |
0aa7f586 AM |
6150 | ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
6151 | bfd *abfd ATTRIBUTE_UNUSED, | |
6152 | asection *current_section, | |
6153 | asection *previous_section, | |
f38a2680 | 6154 | bool new_segment) |
2889e75b | 6155 | { |
0aa7f586 AM |
6156 | lang_output_section_statement_type *cur; |
6157 | lang_output_section_statement_type *prev; | |
591a748a NC |
6158 | |
6159 | /* The checks below are only necessary when the BFD library has decided | |
6160 | that the two sections ought to be placed into the same segment. */ | |
2889e75b | 6161 | if (new_segment) |
f38a2680 | 6162 | return true; |
2889e75b NC |
6163 | |
6164 | /* Paranoia checks. */ | |
6165 | if (current_section == NULL || previous_section == NULL) | |
6166 | return new_segment; | |
6167 | ||
4724d37e RM |
6168 | /* If this flag is set, the target never wants code and non-code |
6169 | sections comingled in the same segment. */ | |
6170 | if (config.separate_code | |
6171 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) | |
f38a2680 | 6172 | return true; |
4724d37e | 6173 | |
2889e75b NC |
6174 | /* Find the memory regions associated with the two sections. |
6175 | We call lang_output_section_find() here rather than scanning the list | |
6176 | of output sections looking for a matching section pointer because if | |
591a748a | 6177 | we have a large number of sections then a hash lookup is faster. */ |
2889e75b NC |
6178 | cur = lang_output_section_find (current_section->name); |
6179 | prev = lang_output_section_find (previous_section->name); | |
6180 | ||
591a748a | 6181 | /* More paranoia. */ |
2889e75b NC |
6182 | if (cur == NULL || prev == NULL) |
6183 | return new_segment; | |
6184 | ||
6185 | /* If the regions are different then force the sections to live in | |
591a748a NC |
6186 | different segments. See the email thread starting at the following |
6187 | URL for the reasons why this is necessary: | |
2889e75b NC |
6188 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
6189 | return cur->region != prev->region; | |
6190 | } | |
6191 | ||
e9ee469a | 6192 | void |
f38a2680 | 6193 | one_lang_size_sections_pass (bool *relax, bool check_regions) |
2d20f7bf | 6194 | { |
420e579c | 6195 | lang_statement_iteration++; |
baf09cba AM |
6196 | if (expld.phase != lang_mark_phase_enum) |
6197 | lang_sizing_iteration++; | |
e535e147 AM |
6198 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
6199 | 0, 0, relax, check_regions); | |
e9ee469a | 6200 | } |
420e579c | 6201 | |
f38a2680 | 6202 | static bool |
2085ee26 | 6203 | lang_size_segment (void) |
e9ee469a | 6204 | { |
cba6246d L |
6205 | /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether |
6206 | a page could be saved in the data segment. */ | |
2085ee26 | 6207 | seg_align_type *seg = &expld.dataseg; |
cba6246d L |
6208 | bfd_vma first, last; |
6209 | ||
9833b775 AM |
6210 | first = -seg->base & (seg->commonpagesize - 1); |
6211 | last = seg->end & (seg->commonpagesize - 1); | |
cba6246d | 6212 | if (first && last |
9833b775 AM |
6213 | && ((seg->base & ~(seg->commonpagesize - 1)) |
6214 | != (seg->end & ~(seg->commonpagesize - 1))) | |
6215 | && first + last <= seg->commonpagesize) | |
8c37241b | 6216 | { |
cba6246d | 6217 | seg->phase = exp_seg_adjust; |
f38a2680 | 6218 | return true; |
cba6246d L |
6219 | } |
6220 | ||
6221 | seg->phase = exp_seg_done; | |
f38a2680 | 6222 | return false; |
cba6246d L |
6223 | } |
6224 | ||
6225 | static bfd_vma | |
2085ee26 | 6226 | lang_size_relro_segment_1 (void) |
cba6246d | 6227 | { |
2085ee26 | 6228 | seg_align_type *seg = &expld.dataseg; |
5197c88e | 6229 | bfd_vma relro_end, desired_end; |
6a9d0866 | 6230 | asection *sec; |
8c37241b | 6231 | |
cba6246d | 6232 | /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ |
9833b775 | 6233 | relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; |
0e5fabeb | 6234 | |
cba6246d | 6235 | /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ |
5197c88e | 6236 | desired_end = relro_end - seg->relro_offset; |
0e5fabeb | 6237 | |
5197c88e | 6238 | /* For sections in the relro segment.. */ |
cba6246d | 6239 | for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) |
6a9d0866 AM |
6240 | if ((sec->flags & SEC_ALLOC) != 0 |
6241 | && sec->vma >= seg->base | |
6242 | && sec->vma < seg->relro_end - seg->relro_offset) | |
cba6246d | 6243 | { |
6a9d0866 AM |
6244 | /* Where do we want to put this section so that it ends as |
6245 | desired? */ | |
6246 | bfd_vma start, end, bump; | |
6247 | ||
6248 | end = start = sec->vma; | |
6249 | if (!IS_TBSS (sec)) | |
6250 | end += TO_ADDR (sec->size); | |
6251 | bump = desired_end - end; | |
6252 | /* We'd like to increase START by BUMP, but we must heed | |
6253 | alignment so the increase might be less than optimum. */ | |
6254 | start += bump; | |
6255 | start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); | |
6256 | /* This is now the desired end for the previous section. */ | |
6257 | desired_end = start; | |
5197c88e AM |
6258 | } |
6259 | ||
6260 | seg->phase = exp_seg_relro_adjust; | |
6261 | ASSERT (desired_end >= seg->base); | |
5197c88e | 6262 | seg->base = desired_end; |
cba6246d L |
6263 | return relro_end; |
6264 | } | |
6265 | ||
f38a2680 AM |
6266 | static bool |
6267 | lang_size_relro_segment (bool *relax, bool check_regions) | |
cba6246d | 6268 | { |
f38a2680 | 6269 | bool do_reset = false; |
cba6246d L |
6270 | |
6271 | if (link_info.relro && expld.dataseg.relro_end) | |
6272 | { | |
e29de63f | 6273 | bfd_vma data_initial_base = expld.dataseg.base; |
2085ee26 | 6274 | bfd_vma data_relro_end = lang_size_relro_segment_1 (); |
0e5fabeb | 6275 | |
f86a8756 | 6276 | lang_reset_memory_regions (); |
e9ee469a | 6277 | one_lang_size_sections_pass (relax, check_regions); |
0e5fabeb | 6278 | |
cba6246d L |
6279 | /* Assignments to dot, or to output section address in a user |
6280 | script have increased padding over the original. Revert. */ | |
e29de63f | 6281 | if (expld.dataseg.relro_end > data_relro_end) |
a4f5ad88 | 6282 | { |
2085ee26 | 6283 | expld.dataseg.base = data_initial_base; |
f38a2680 | 6284 | do_reset = true; |
a4f5ad88 | 6285 | } |
8c37241b | 6286 | } |
2085ee26 | 6287 | else if (lang_size_segment ()) |
f38a2680 | 6288 | do_reset = true; |
cba6246d L |
6289 | |
6290 | return do_reset; | |
6291 | } | |
6292 | ||
6293 | void | |
f38a2680 | 6294 | lang_size_sections (bool *relax, bool check_regions) |
cba6246d L |
6295 | { |
6296 | expld.phase = lang_allocating_phase_enum; | |
6297 | expld.dataseg.phase = exp_seg_none; | |
6298 | ||
6299 | one_lang_size_sections_pass (relax, check_regions); | |
6300 | ||
6301 | if (expld.dataseg.phase != exp_seg_end_seen) | |
6302 | expld.dataseg.phase = exp_seg_done; | |
6303 | ||
6304 | if (expld.dataseg.phase == exp_seg_end_seen) | |
2d20f7bf | 6305 | { |
f38a2680 | 6306 | bool do_reset |
cba6246d L |
6307 | = lang_size_relro_segment (relax, check_regions); |
6308 | ||
6309 | if (do_reset) | |
2d20f7bf | 6310 | { |
f86a8756 | 6311 | lang_reset_memory_regions (); |
e9ee469a | 6312 | one_lang_size_sections_pass (relax, check_regions); |
2d20f7bf | 6313 | } |
cba6246d L |
6314 | |
6315 | if (link_info.relro && expld.dataseg.relro_end) | |
6316 | { | |
6317 | link_info.relro_start = expld.dataseg.base; | |
6318 | link_info.relro_end = expld.dataseg.relro_end; | |
6319 | } | |
2d20f7bf | 6320 | } |
2d20f7bf JJ |
6321 | } |
6322 | ||
d2667025 AM |
6323 | static lang_output_section_statement_type *current_section; |
6324 | static lang_assignment_statement_type *current_assign; | |
f38a2680 | 6325 | static bool prefer_next_section; |
d2667025 | 6326 | |
420e579c HPN |
6327 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
6328 | ||
6329 | static bfd_vma | |
66e28d60 AM |
6330 | lang_do_assignments_1 (lang_statement_union_type *s, |
6331 | lang_output_section_statement_type *current_os, | |
6332 | fill_type *fill, | |
d2667025 | 6333 | bfd_vma dot, |
f38a2680 | 6334 | bool *found_end) |
252b5132 | 6335 | { |
1579bae1 | 6336 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
6337 | { |
6338 | switch (s->header.type) | |
6339 | { | |
6340 | case lang_constructors_statement_enum: | |
420e579c | 6341 | dot = lang_do_assignments_1 (constructor_list.head, |
d2667025 | 6342 | current_os, fill, dot, found_end); |
252b5132 RH |
6343 | break; |
6344 | ||
6345 | case lang_output_section_statement_enum: | |
6346 | { | |
d1778b88 | 6347 | lang_output_section_statement_type *os; |
f02cb058 | 6348 | bfd_vma newdot; |
252b5132 | 6349 | |
d1778b88 | 6350 | os = &(s->output_section_statement); |
d2667025 | 6351 | os->after_end = *found_end; |
61826503 | 6352 | init_opb (os->bfd_section); |
1cba418d AM |
6353 | newdot = dot; |
6354 | if (os->bfd_section != NULL) | |
252b5132 | 6355 | { |
1cba418d | 6356 | if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) |
d2667025 AM |
6357 | { |
6358 | current_section = os; | |
f38a2680 | 6359 | prefer_next_section = false; |
d2667025 | 6360 | } |
1cba418d | 6361 | newdot = os->bfd_section->vma; |
f02cb058 AM |
6362 | } |
6363 | newdot = lang_do_assignments_1 (os->children.head, | |
1cba418d | 6364 | os, os->fill, newdot, found_end); |
f02cb058 AM |
6365 | if (!os->ignored) |
6366 | { | |
6367 | if (os->bfd_section != NULL) | |
6368 | { | |
1cba418d AM |
6369 | newdot = os->bfd_section->vma; |
6370 | ||
f02cb058 AM |
6371 | /* .tbss sections effectively have zero size. */ |
6372 | if (!IS_TBSS (os->bfd_section) | |
6373 | || bfd_link_relocatable (&link_info)) | |
1cba418d | 6374 | newdot += TO_ADDR (os->bfd_section->size); |
f02cb058 AM |
6375 | |
6376 | if (os->update_dot_tree != NULL) | |
6377 | exp_fold_tree (os->update_dot_tree, | |
1cba418d | 6378 | bfd_abs_section_ptr, &newdot); |
f02cb058 | 6379 | } |
1cba418d | 6380 | dot = newdot; |
252b5132 RH |
6381 | } |
6382 | } | |
6383 | break; | |
e9ee469a | 6384 | |
252b5132 RH |
6385 | case lang_wild_statement_enum: |
6386 | ||
420e579c | 6387 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
d2667025 | 6388 | current_os, fill, dot, found_end); |
252b5132 RH |
6389 | break; |
6390 | ||
6391 | case lang_object_symbols_statement_enum: | |
6392 | case lang_output_statement_enum: | |
6393 | case lang_target_statement_enum: | |
252b5132 | 6394 | break; |
e9ee469a | 6395 | |
252b5132 | 6396 | case lang_data_statement_enum: |
e9ee469a AM |
6397 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
6398 | if (expld.result.valid_p) | |
7542af2a AM |
6399 | { |
6400 | s->data_statement.value = expld.result.value; | |
6401 | if (expld.result.section != NULL) | |
6402 | s->data_statement.value += expld.result.section->vma; | |
6403 | } | |
f02cb058 | 6404 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6405 | einfo (_("%F%P: invalid data statement\n")); |
b7a26f91 KH |
6406 | { |
6407 | unsigned int size; | |
08da4cac KH |
6408 | switch (s->data_statement.type) |
6409 | { | |
6410 | default: | |
6411 | abort (); | |
6412 | case QUAD: | |
6413 | case SQUAD: | |
6414 | size = QUAD_SIZE; | |
6415 | break; | |
6416 | case LONG: | |
6417 | size = LONG_SIZE; | |
6418 | break; | |
6419 | case SHORT: | |
6420 | size = SHORT_SIZE; | |
6421 | break; | |
6422 | case BYTE: | |
6423 | size = BYTE_SIZE; | |
6424 | break; | |
6425 | } | |
e5caa5e0 AM |
6426 | if (size < TO_SIZE ((unsigned) 1)) |
6427 | size = TO_SIZE ((unsigned) 1); | |
6428 | dot += TO_ADDR (size); | |
08da4cac | 6429 | } |
252b5132 RH |
6430 | break; |
6431 | ||
6432 | case lang_reloc_statement_enum: | |
e9ee469a AM |
6433 | exp_fold_tree (s->reloc_statement.addend_exp, |
6434 | bfd_abs_section_ptr, &dot); | |
6435 | if (expld.result.valid_p) | |
6436 | s->reloc_statement.addend_value = expld.result.value; | |
f02cb058 | 6437 | else if (expld.phase == lang_final_phase_enum) |
e9ee469a | 6438 | einfo (_("%F%P: invalid reloc statement\n")); |
e5caa5e0 | 6439 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
6440 | break; |
6441 | ||
6442 | case lang_input_section_enum: | |
6443 | { | |
6444 | asection *in = s->input_section.section; | |
6445 | ||
57ceae94 | 6446 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 6447 | dot += TO_ADDR (in->size); |
252b5132 RH |
6448 | } |
6449 | break; | |
6450 | ||
6451 | case lang_input_statement_enum: | |
6452 | break; | |
e9ee469a | 6453 | |
252b5132 RH |
6454 | case lang_fill_statement_enum: |
6455 | fill = s->fill_statement.fill; | |
6456 | break; | |
252b5132 | 6457 | |
e9ee469a | 6458 | case lang_assignment_statement_enum: |
d2667025 AM |
6459 | current_assign = &s->assignment_statement; |
6460 | if (current_assign->exp->type.node_class != etree_assert) | |
6461 | { | |
6462 | const char *p = current_assign->exp->assign.dst; | |
6463 | ||
6464 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) | |
f38a2680 | 6465 | prefer_next_section = true; |
d2667025 AM |
6466 | |
6467 | while (*p == '_') | |
6468 | ++p; | |
6469 | if (strcmp (p, "end") == 0) | |
f38a2680 | 6470 | *found_end = true; |
d2667025 | 6471 | } |
e9ee469a | 6472 | exp_fold_tree (s->assignment_statement.exp, |
f02cb058 AM |
6473 | (current_os->bfd_section != NULL |
6474 | ? current_os->bfd_section : bfd_und_section_ptr), | |
e9ee469a | 6475 | &dot); |
252b5132 | 6476 | break; |
e9ee469a | 6477 | |
252b5132 | 6478 | case lang_padding_statement_enum: |
e5caa5e0 | 6479 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
6480 | break; |
6481 | ||
6482 | case lang_group_statement_enum: | |
420e579c | 6483 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
d2667025 | 6484 | current_os, fill, dot, found_end); |
252b5132 RH |
6485 | break; |
6486 | ||
53d25da6 | 6487 | case lang_insert_statement_enum: |
252b5132 | 6488 | break; |
e9ee469a | 6489 | |
252b5132 RH |
6490 | case lang_address_statement_enum: |
6491 | break; | |
53d25da6 AM |
6492 | |
6493 | default: | |
6494 | FAIL (); | |
6495 | break; | |
252b5132 | 6496 | } |
252b5132 RH |
6497 | } |
6498 | return dot; | |
6499 | } | |
6500 | ||
f2241121 | 6501 | void |
2f65ac72 | 6502 | lang_do_assignments (lang_phase_type phase) |
420e579c | 6503 | { |
f38a2680 | 6504 | bool found_end = false; |
d2667025 AM |
6505 | |
6506 | current_section = NULL; | |
f38a2680 | 6507 | prefer_next_section = false; |
2f65ac72 | 6508 | expld.phase = phase; |
420e579c | 6509 | lang_statement_iteration++; |
d2667025 AM |
6510 | lang_do_assignments_1 (statement_list.head, |
6511 | abs_output_section, NULL, 0, &found_end); | |
6512 | } | |
6513 | ||
6514 | /* For an assignment statement outside of an output section statement, | |
6515 | choose the best of neighbouring output sections to use for values | |
6516 | of "dot". */ | |
6517 | ||
6518 | asection * | |
6519 | section_for_dot (void) | |
6520 | { | |
6521 | asection *s; | |
6522 | ||
6523 | /* Assignments belong to the previous output section, unless there | |
6524 | has been an assignment to "dot", in which case following | |
6525 | assignments belong to the next output section. (The assumption | |
6526 | is that an assignment to "dot" is setting up the address for the | |
6527 | next output section.) Except that past the assignment to "_end" | |
6528 | we always associate with the previous section. This exception is | |
6529 | for targets like SH that define an alloc .stack or other | |
6530 | weirdness after non-alloc sections. */ | |
6531 | if (current_section == NULL || prefer_next_section) | |
6532 | { | |
6533 | lang_statement_union_type *stmt; | |
6534 | lang_output_section_statement_type *os; | |
6535 | ||
6536 | for (stmt = (lang_statement_union_type *) current_assign; | |
6537 | stmt != NULL; | |
6538 | stmt = stmt->header.next) | |
6539 | if (stmt->header.type == lang_output_section_statement_enum) | |
6540 | break; | |
6541 | ||
10c00056 | 6542 | os = stmt ? &stmt->output_section_statement : NULL; |
d2667025 AM |
6543 | while (os != NULL |
6544 | && !os->after_end | |
6545 | && (os->bfd_section == NULL | |
6546 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 | |
6547 | || bfd_section_removed_from_list (link_info.output_bfd, | |
6548 | os->bfd_section))) | |
6549 | os = os->next; | |
6550 | ||
6551 | if (current_section == NULL || os == NULL || !os->after_end) | |
6552 | { | |
6553 | if (os != NULL) | |
6554 | s = os->bfd_section; | |
6555 | else | |
6556 | s = link_info.output_bfd->section_last; | |
6557 | while (s != NULL | |
6558 | && ((s->flags & SEC_ALLOC) == 0 | |
6559 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6560 | s = s->prev; | |
6561 | if (s != NULL) | |
6562 | return s; | |
6563 | ||
6564 | return bfd_abs_section_ptr; | |
6565 | } | |
6566 | } | |
6567 | ||
6568 | s = current_section->bfd_section; | |
6569 | ||
6570 | /* The section may have been stripped. */ | |
6571 | while (s != NULL | |
6572 | && ((s->flags & SEC_EXCLUDE) != 0 | |
6573 | || (s->flags & SEC_ALLOC) == 0 | |
6574 | || (s->flags & SEC_THREAD_LOCAL) != 0 | |
6575 | || bfd_section_removed_from_list (link_info.output_bfd, s))) | |
6576 | s = s->prev; | |
6577 | if (s == NULL) | |
6578 | s = link_info.output_bfd->sections; | |
6579 | while (s != NULL | |
6580 | && ((s->flags & SEC_ALLOC) == 0 | |
6581 | || (s->flags & SEC_THREAD_LOCAL) != 0)) | |
6582 | s = s->next; | |
6583 | if (s != NULL) | |
6584 | return s; | |
6585 | ||
6586 | return bfd_abs_section_ptr; | |
420e579c HPN |
6587 | } |
6588 | ||
7dba9362 AM |
6589 | /* Array of __start/__stop/.startof./.sizeof/ symbols. */ |
6590 | ||
6591 | static struct bfd_link_hash_entry **start_stop_syms; | |
6592 | static size_t start_stop_count = 0; | |
6593 | static size_t start_stop_alloc = 0; | |
6594 | ||
6595 | /* Give start/stop SYMBOL for SEC a preliminary definition, and add it | |
6596 | to start_stop_syms. */ | |
6597 | ||
6598 | static void | |
6599 | lang_define_start_stop (const char *symbol, asection *sec) | |
6600 | { | |
6601 | struct bfd_link_hash_entry *h; | |
6602 | ||
6603 | h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); | |
6604 | if (h != NULL) | |
6605 | { | |
6606 | if (start_stop_count == start_stop_alloc) | |
6607 | { | |
6608 | start_stop_alloc = 2 * start_stop_alloc + 10; | |
6609 | start_stop_syms | |
6610 | = xrealloc (start_stop_syms, | |
6611 | start_stop_alloc * sizeof (*start_stop_syms)); | |
6612 | } | |
6613 | start_stop_syms[start_stop_count++] = h; | |
6614 | } | |
6615 | } | |
6616 | ||
6617 | /* Check for input sections whose names match references to | |
6618 | __start_SECNAME or __stop_SECNAME symbols. Give the symbols | |
6619 | preliminary definitions. */ | |
252b5132 RH |
6620 | |
6621 | static void | |
7dba9362 | 6622 | lang_init_start_stop (void) |
252b5132 | 6623 | { |
7dba9362 | 6624 | bfd *abfd; |
252b5132 | 6625 | asection *s; |
7dba9362 AM |
6626 | char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); |
6627 | ||
6628 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) | |
6629 | for (s = abfd->sections; s != NULL; s = s->next) | |
6630 | { | |
6631 | const char *ps; | |
6632 | const char *secname = s->name; | |
6633 | ||
6634 | for (ps = secname; *ps != '\0'; ps++) | |
6635 | if (!ISALNUM ((unsigned char) *ps) && *ps != '_') | |
6636 | break; | |
6637 | if (*ps == '\0') | |
6638 | { | |
6639 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
b27685f2 | 6640 | |
7dba9362 AM |
6641 | symbol[0] = leading_char; |
6642 | sprintf (symbol + (leading_char != 0), "__start_%s", secname); | |
6643 | lang_define_start_stop (symbol, s); | |
6644 | ||
6645 | symbol[1] = leading_char; | |
6646 | memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); | |
6647 | lang_define_start_stop (symbol + 1, s); | |
6648 | ||
6649 | free (symbol); | |
6650 | } | |
6651 | } | |
6652 | } | |
6653 | ||
6654 | /* Iterate over start_stop_syms. */ | |
6655 | ||
6656 | static void | |
6657 | foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) | |
6658 | { | |
6659 | size_t i; | |
6660 | ||
6661 | for (i = 0; i < start_stop_count; ++i) | |
6662 | func (start_stop_syms[i]); | |
6663 | } | |
6664 | ||
6665 | /* __start and __stop symbols are only supposed to be defined by the | |
6666 | linker for orphan sections, but we now extend that to sections that | |
6667 | map to an output section of the same name. The symbols were | |
6668 | defined early for --gc-sections, before we mapped input to output | |
6669 | sections, so undo those that don't satisfy this rule. */ | |
6670 | ||
6671 | static void | |
6672 | undef_start_stop (struct bfd_link_hash_entry *h) | |
6673 | { | |
6674 | if (h->ldscript_def) | |
b27685f2 L |
6675 | return; |
6676 | ||
7dba9362 AM |
6677 | if (h->u.def.section->output_section == NULL |
6678 | || h->u.def.section->output_section->owner != link_info.output_bfd | |
6679 | || strcmp (h->u.def.section->name, | |
6680 | h->u.def.section->output_section->name) != 0) | |
6681 | { | |
4d1c6335 L |
6682 | asection *sec = bfd_get_section_by_name (link_info.output_bfd, |
6683 | h->u.def.section->name); | |
6684 | if (sec != NULL) | |
6685 | { | |
6686 | /* When there are more than one input sections with the same | |
6687 | section name, SECNAME, linker picks the first one to define | |
6688 | __start_SECNAME and __stop_SECNAME symbols. When the first | |
6689 | input section is removed by comdat group, we need to check | |
6690 | if there is still an output section with section name | |
6691 | SECNAME. */ | |
6692 | asection *i; | |
6693 | for (i = sec->map_head.s; i != NULL; i = i->map_head.s) | |
6694 | if (strcmp (h->u.def.section->name, i->name) == 0) | |
6695 | { | |
6696 | h->u.def.section = i; | |
6697 | return; | |
6698 | } | |
6699 | } | |
7dba9362 AM |
6700 | h->type = bfd_link_hash_undefined; |
6701 | h->u.undef.abfd = NULL; | |
fc304b88 | 6702 | if (is_elf_hash_table (link_info.hash)) |
7824c1d2 AM |
6703 | { |
6704 | const struct elf_backend_data *bed; | |
6705 | struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h; | |
6706 | unsigned int was_forced = eh->forced_local; | |
6707 | ||
6708 | bed = get_elf_backend_data (link_info.output_bfd); | |
f38a2680 | 6709 | (*bed->elf_backend_hide_symbol) (&link_info, eh, true); |
7824c1d2 AM |
6710 | if (!eh->ref_regular_nonweak) |
6711 | h->type = bfd_link_hash_undefweak; | |
6712 | eh->def_regular = 0; | |
6713 | eh->forced_local = was_forced; | |
6714 | } | |
7dba9362 AM |
6715 | } |
6716 | } | |
252b5132 | 6717 | |
7dba9362 AM |
6718 | static void |
6719 | lang_undef_start_stop (void) | |
6720 | { | |
6721 | foreach_start_stop (undef_start_stop); | |
6722 | } | |
6723 | ||
6724 | /* Check for output sections whose names match references to | |
6725 | .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols | |
6726 | preliminary definitions. */ | |
6727 | ||
6728 | static void | |
6729 | lang_init_startof_sizeof (void) | |
6730 | { | |
6731 | asection *s; | |
252b5132 | 6732 | |
f13a99db | 6733 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
252b5132 | 6734 | { |
7dba9362 AM |
6735 | const char *secname = s->name; |
6736 | char *symbol = (char *) xmalloc (10 + strlen (secname)); | |
252b5132 | 6737 | |
7dba9362 AM |
6738 | sprintf (symbol, ".startof.%s", secname); |
6739 | lang_define_start_stop (symbol, s); | |
252b5132 | 6740 | |
7dba9362 AM |
6741 | memcpy (symbol + 1, ".size", 5); |
6742 | lang_define_start_stop (symbol + 1, s); | |
6743 | free (symbol); | |
6744 | } | |
6745 | } | |
cbd0eecf | 6746 | |
7dba9362 AM |
6747 | /* Set .startof., .sizeof., __start and __stop symbols final values. */ |
6748 | ||
6749 | static void | |
6750 | set_start_stop (struct bfd_link_hash_entry *h) | |
6751 | { | |
6752 | if (h->ldscript_def | |
6753 | || h->type != bfd_link_hash_defined) | |
6754 | return; | |
cbd0eecf | 6755 | |
7dba9362 AM |
6756 | if (h->root.string[0] == '.') |
6757 | { | |
6758 | /* .startof. or .sizeof. symbol. | |
6759 | .startof. already has final value. */ | |
6760 | if (h->root.string[2] == 'i') | |
252b5132 | 6761 | { |
7dba9362 AM |
6762 | /* .sizeof. */ |
6763 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
6764 | h->u.def.section = bfd_abs_section_ptr; | |
252b5132 | 6765 | } |
7dba9362 AM |
6766 | } |
6767 | else | |
6768 | { | |
6769 | /* __start or __stop symbol. */ | |
6770 | int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; | |
252b5132 | 6771 | |
7dba9362 AM |
6772 | h->u.def.section = h->u.def.section->output_section; |
6773 | if (h->root.string[4 + has_lead] == 'o') | |
252b5132 | 6774 | { |
7dba9362 AM |
6775 | /* __stop_ */ |
6776 | h->u.def.value = TO_ADDR (h->u.def.section->size); | |
252b5132 | 6777 | } |
252b5132 RH |
6778 | } |
6779 | } | |
6780 | ||
7dba9362 AM |
6781 | static void |
6782 | lang_finalize_start_stop (void) | |
6783 | { | |
6784 | foreach_start_stop (set_start_stop); | |
6785 | } | |
6786 | ||
97da0e26 AM |
6787 | static void |
6788 | lang_symbol_tweaks (void) | |
6789 | { | |
6790 | /* Give initial values for __start and __stop symbols, so that ELF | |
6791 | gc_sections will keep sections referenced by these symbols. Must | |
6792 | be done before lang_do_assignments. */ | |
6793 | if (config.build_constructors) | |
6794 | lang_init_start_stop (); | |
6795 | ||
6796 | /* Make __ehdr_start hidden, and set def_regular even though it is | |
6797 | likely undefined at this stage. For lang_check_relocs. */ | |
6798 | if (is_elf_hash_table (link_info.hash) | |
6799 | && !bfd_link_relocatable (&link_info)) | |
6800 | { | |
6801 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) | |
6802 | bfd_link_hash_lookup (link_info.hash, "__ehdr_start", | |
6803 | false, false, true); | |
6804 | ||
6805 | /* Only adjust the export class if the symbol was referenced | |
6806 | and not defined, otherwise leave it alone. */ | |
6807 | if (h != NULL | |
6808 | && (h->root.type == bfd_link_hash_new | |
6809 | || h->root.type == bfd_link_hash_undefined | |
6810 | || h->root.type == bfd_link_hash_undefweak | |
6811 | || h->root.type == bfd_link_hash_common)) | |
6812 | { | |
6813 | const struct elf_backend_data *bed; | |
6814 | bed = get_elf_backend_data (link_info.output_bfd); | |
6815 | (*bed->elf_backend_hide_symbol) (&link_info, h, true); | |
6816 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) | |
6817 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
6818 | h->def_regular = 1; | |
6819 | h->root.linker_def = 1; | |
5617fae7 | 6820 | h->root.rel_from_abs = 1; |
97da0e26 AM |
6821 | } |
6822 | } | |
6823 | } | |
6824 | ||
252b5132 | 6825 | static void |
750877ba | 6826 | lang_end (void) |
252b5132 RH |
6827 | { |
6828 | struct bfd_link_hash_entry *h; | |
f38a2680 | 6829 | bool warn; |
252b5132 | 6830 | |
0e1862bb L |
6831 | if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) |
6832 | || bfd_link_dll (&link_info)) | |
71934f94 | 6833 | warn = entry_from_cmdline; |
252b5132 | 6834 | else |
f38a2680 | 6835 | warn = true; |
252b5132 | 6836 | |
ac69cbc6 | 6837 | /* Force the user to specify a root when generating a relocatable with |
91ae256e AM |
6838 | --gc-sections, unless --gc-keep-exported was also given. */ |
6839 | if (bfd_link_relocatable (&link_info) | |
6840 | && link_info.gc_sections | |
2f5541f3 AM |
6841 | && !link_info.gc_keep_exported) |
6842 | { | |
6843 | struct bfd_sym_chain *sym; | |
6844 | ||
6845 | for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next) | |
6846 | { | |
6847 | h = bfd_link_hash_lookup (link_info.hash, sym->name, | |
f38a2680 | 6848 | false, false, false); |
2f5541f3 AM |
6849 | if (h != NULL |
6850 | && (h->type == bfd_link_hash_defined | |
6851 | || h->type == bfd_link_hash_defweak) | |
6852 | && !bfd_is_const_section (h->u.def.section)) | |
6853 | break; | |
6854 | } | |
6855 | if (!sym) | |
6856 | einfo (_("%F%P: --gc-sections requires a defined symbol root " | |
6857 | "specified by -e or -u\n")); | |
6858 | } | |
ac69cbc6 | 6859 | |
1579bae1 | 6860 | if (entry_symbol.name == NULL) |
252b5132 | 6861 | { |
a359509e ZW |
6862 | /* No entry has been specified. Look for the default entry, but |
6863 | don't warn if we don't find it. */ | |
6864 | entry_symbol.name = entry_symbol_default; | |
f38a2680 | 6865 | warn = false; |
252b5132 RH |
6866 | } |
6867 | ||
e3e942e9 | 6868 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
f38a2680 | 6869 | false, false, true); |
1579bae1 | 6870 | if (h != NULL |
252b5132 RH |
6871 | && (h->type == bfd_link_hash_defined |
6872 | || h->type == bfd_link_hash_defweak) | |
6873 | && h->u.def.section->output_section != NULL) | |
6874 | { | |
6875 | bfd_vma val; | |
6876 | ||
6877 | val = (h->u.def.value | |
fd361982 | 6878 | + bfd_section_vma (h->u.def.section->output_section) |
252b5132 | 6879 | + h->u.def.section->output_offset); |
0aa7f586 | 6880 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6881 | einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
6882 | } |
6883 | else | |
6884 | { | |
6885 | bfd_vma val; | |
5f992e62 | 6886 | const char *send; |
252b5132 RH |
6887 | |
6888 | /* We couldn't find the entry symbol. Try parsing it as a | |
afd7a018 | 6889 | number. */ |
e3e942e9 | 6890 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
6891 | if (*send == '\0') |
6892 | { | |
0aa7f586 | 6893 | if (!bfd_set_start_address (link_info.output_bfd, val)) |
df5f2391 | 6894 | einfo (_("%F%P: can't set start address\n")); |
252b5132 | 6895 | } |
5226a6a8 NC |
6896 | /* BZ 2004952: Only use the start of the entry section for executables. */ |
6897 | else if bfd_link_executable (&link_info) | |
252b5132 RH |
6898 | { |
6899 | asection *ts; | |
6900 | ||
6901 | /* Can't find the entry symbol, and it's not a number. Use | |
6902 | the first address in the text section. */ | |
f13a99db | 6903 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
1579bae1 | 6904 | if (ts != NULL) |
252b5132 RH |
6905 | { |
6906 | if (warn) | |
6feb9908 AM |
6907 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6908 | " defaulting to %V\n"), | |
e3e942e9 | 6909 | entry_symbol.name, |
fd361982 AM |
6910 | bfd_section_vma (ts)); |
6911 | if (!bfd_set_start_address (link_info.output_bfd, | |
6912 | bfd_section_vma (ts))) | |
df5f2391 | 6913 | einfo (_("%F%P: can't set start address\n")); |
252b5132 RH |
6914 | } |
6915 | else | |
6916 | { | |
6917 | if (warn) | |
6feb9908 AM |
6918 | einfo (_("%P: warning: cannot find entry symbol %s;" |
6919 | " not setting start address\n"), | |
e3e942e9 | 6920 | entry_symbol.name); |
252b5132 RH |
6921 | } |
6922 | } | |
5226a6a8 NC |
6923 | else |
6924 | { | |
6925 | if (warn) | |
6926 | einfo (_("%P: warning: cannot find entry symbol %s;" | |
6927 | " not setting start address\n"), | |
6928 | entry_symbol.name); | |
6929 | } | |
252b5132 RH |
6930 | } |
6931 | } | |
6932 | ||
6933 | /* This is a small function used when we want to ignore errors from | |
6934 | BFD. */ | |
6935 | ||
6936 | static void | |
52d45da3 AM |
6937 | ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, |
6938 | va_list ap ATTRIBUTE_UNUSED) | |
252b5132 RH |
6939 | { |
6940 | /* Don't do anything. */ | |
6941 | } | |
6942 | ||
6943 | /* Check that the architecture of all the input files is compatible | |
6944 | with the output file. Also call the backend to let it do any | |
6945 | other checking that is needed. */ | |
6946 | ||
6947 | static void | |
1579bae1 | 6948 | lang_check (void) |
252b5132 | 6949 | { |
36983a93 | 6950 | lang_input_statement_type *file; |
252b5132 | 6951 | bfd *input_bfd; |
5f992e62 | 6952 | const bfd_arch_info_type *compatible; |
252b5132 | 6953 | |
8ce18f9c | 6954 | for (file = (void *) file_chain.head; |
36983a93 AM |
6955 | file != NULL; |
6956 | file = file->next) | |
252b5132 | 6957 | { |
0381901e | 6958 | #if BFD_SUPPORTS_PLUGINS |
422b6f14 | 6959 | /* Don't check format of files claimed by plugin. */ |
36983a93 | 6960 | if (file->flags.claimed) |
422b6f14 | 6961 | continue; |
0381901e | 6962 | #endif /* BFD_SUPPORTS_PLUGINS */ |
36983a93 | 6963 | input_bfd = file->the_bfd; |
6feb9908 | 6964 | compatible |
f13a99db | 6965 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
6feb9908 | 6966 | command_line.accept_unknown_input_arch); |
30cba025 AM |
6967 | |
6968 | /* In general it is not possible to perform a relocatable | |
6969 | link between differing object formats when the input | |
6970 | file has relocations, because the relocations in the | |
6971 | input format may not have equivalent representations in | |
6972 | the output format (and besides BFD does not translate | |
6973 | relocs for other link purposes than a final link). */ | |
f437dadd AM |
6974 | if (!file->flags.just_syms |
6975 | && (bfd_link_relocatable (&link_info) | |
6976 | || link_info.emitrelocations) | |
30cba025 | 6977 | && (compatible == NULL |
f13a99db AM |
6978 | || (bfd_get_flavour (input_bfd) |
6979 | != bfd_get_flavour (link_info.output_bfd))) | |
30cba025 AM |
6980 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
6981 | { | |
df5f2391 | 6982 | einfo (_("%F%P: relocatable linking with relocations from" |
871b3ab2 | 6983 | " format %s (%pB) to format %s (%pB) is not supported\n"), |
30cba025 | 6984 | bfd_get_target (input_bfd), input_bfd, |
f13a99db | 6985 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
30cba025 AM |
6986 | /* einfo with %F exits. */ |
6987 | } | |
6988 | ||
252b5132 RH |
6989 | if (compatible == NULL) |
6990 | { | |
6991 | if (command_line.warn_mismatch) | |
df5f2391 | 6992 | einfo (_("%X%P: %s architecture of input file `%pB'" |
6feb9908 | 6993 | " is incompatible with %s output\n"), |
252b5132 | 6994 | bfd_printable_name (input_bfd), input_bfd, |
f13a99db | 6995 | bfd_printable_name (link_info.output_bfd)); |
252b5132 | 6996 | } |
b9247304 | 6997 | |
a8acd6ee AM |
6998 | /* If the input bfd has no contents, it shouldn't set the |
6999 | private data of the output bfd. */ | |
f437dadd AM |
7000 | else if (!file->flags.just_syms |
7001 | && ((input_bfd->flags & DYNAMIC) != 0 | |
7002 | || bfd_count_sections (input_bfd) != 0)) | |
a8acd6ee | 7003 | { |
252b5132 RH |
7004 | bfd_error_handler_type pfn = NULL; |
7005 | ||
7006 | /* If we aren't supposed to warn about mismatched input | |
afd7a018 AM |
7007 | files, temporarily set the BFD error handler to a |
7008 | function which will do nothing. We still want to call | |
7009 | bfd_merge_private_bfd_data, since it may set up | |
7010 | information which is needed in the output file. */ | |
0aa7f586 | 7011 | if (!command_line.warn_mismatch) |
252b5132 | 7012 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
50e03d47 | 7013 | if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) |
252b5132 RH |
7014 | { |
7015 | if (command_line.warn_mismatch) | |
df5f2391 | 7016 | einfo (_("%X%P: failed to merge target specific data" |
871b3ab2 | 7017 | " of file %pB\n"), input_bfd); |
252b5132 | 7018 | } |
0aa7f586 | 7019 | if (!command_line.warn_mismatch) |
252b5132 RH |
7020 | bfd_set_error_handler (pfn); |
7021 | } | |
7022 | } | |
7023 | } | |
7024 | ||
7025 | /* Look through all the global common symbols and attach them to the | |
7026 | correct section. The -sort-common command line switch may be used | |
de7dd2bd | 7027 | to roughly sort the entries by alignment. */ |
252b5132 RH |
7028 | |
7029 | static void | |
1579bae1 | 7030 | lang_common (void) |
252b5132 | 7031 | { |
a4819f54 | 7032 | if (link_info.inhibit_common_definition) |
4818e05f | 7033 | return; |
0e1862bb | 7034 | if (bfd_link_relocatable (&link_info) |
0aa7f586 | 7035 | && !command_line.force_common_definition) |
252b5132 RH |
7036 | return; |
7037 | ||
0aa7f586 | 7038 | if (!config.sort_common) |
1579bae1 | 7039 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
7040 | else |
7041 | { | |
de7dd2bd NC |
7042 | unsigned int power; |
7043 | ||
7044 | if (config.sort_common == sort_descending) | |
967928e9 AM |
7045 | { |
7046 | for (power = 4; power > 0; power--) | |
7047 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7048 | ||
7049 | power = 0; | |
7050 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
7051 | } | |
de7dd2bd | 7052 | else |
967928e9 AM |
7053 | { |
7054 | for (power = 0; power <= 4; power++) | |
7055 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); | |
252b5132 | 7056 | |
2dc0e7b4 | 7057 | power = (unsigned int) -1; |
de7dd2bd NC |
7058 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
7059 | } | |
252b5132 RH |
7060 | } |
7061 | } | |
7062 | ||
7063 | /* Place one common symbol in the correct section. */ | |
7064 | ||
f38a2680 | 7065 | static bool |
1579bae1 | 7066 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
7067 | { |
7068 | unsigned int power_of_two; | |
7069 | bfd_vma size; | |
7070 | asection *section; | |
7071 | ||
7072 | if (h->type != bfd_link_hash_common) | |
f38a2680 | 7073 | return true; |
252b5132 RH |
7074 | |
7075 | size = h->u.c.size; | |
7076 | power_of_two = h->u.c.p->alignment_power; | |
7077 | ||
de7dd2bd NC |
7078 | if (config.sort_common == sort_descending |
7079 | && power_of_two < *(unsigned int *) info) | |
f38a2680 | 7080 | return true; |
de7dd2bd | 7081 | else if (config.sort_common == sort_ascending |
967928e9 | 7082 | && power_of_two > *(unsigned int *) info) |
f38a2680 | 7083 | return true; |
252b5132 RH |
7084 | |
7085 | section = h->u.c.p->section; | |
3023e3f6 | 7086 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
df5f2391 | 7087 | einfo (_("%F%P: could not define common symbol `%pT': %E\n"), |
3023e3f6 | 7088 | h->root.string); |
252b5132 RH |
7089 | |
7090 | if (config.map_file != NULL) | |
7091 | { | |
f38a2680 | 7092 | static bool header_printed; |
252b5132 RH |
7093 | int len; |
7094 | char *name; | |
6b9bd54c | 7095 | char buf[32]; |
252b5132 | 7096 | |
0aa7f586 | 7097 | if (!header_printed) |
252b5132 RH |
7098 | { |
7099 | minfo (_("\nAllocating common symbols\n")); | |
7100 | minfo (_("Common symbol size file\n\n")); | |
f38a2680 | 7101 | header_printed = true; |
252b5132 RH |
7102 | } |
7103 | ||
f13a99db | 7104 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
73705ac3 | 7105 | DMGL_ANSI | DMGL_PARAMS); |
d7d4c8de AM |
7106 | if (name == NULL) |
7107 | { | |
7108 | minfo ("%s", h->root.string); | |
7109 | len = strlen (h->root.string); | |
7110 | } | |
7111 | else | |
7112 | { | |
7113 | minfo ("%s", name); | |
7114 | len = strlen (name); | |
7115 | free (name); | |
7116 | } | |
252b5132 RH |
7117 | |
7118 | if (len >= 19) | |
7119 | { | |
7120 | print_nl (); | |
7121 | len = 0; | |
7122 | } | |
252b5132 | 7123 | |
f493c217 | 7124 | sprintf (buf, "%" PRIx64, (uint64_t) size); |
6b9bd54c | 7125 | fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); |
252b5132 | 7126 | |
871b3ab2 | 7127 | minfo ("%pB\n", section->owner); |
252b5132 RH |
7128 | } |
7129 | ||
f38a2680 | 7130 | return true; |
252b5132 RH |
7131 | } |
7132 | ||
c005eb9e AB |
7133 | /* Handle a single orphan section S, placing the orphan into an appropriate |
7134 | output section. The effects of the --orphan-handling command line | |
7135 | option are handled here. */ | |
7136 | ||
7137 | static void | |
7138 | ldlang_place_orphan (asection *s) | |
7139 | { | |
7140 | if (config.orphan_handling == orphan_handling_discard) | |
7141 | { | |
7142 | lang_output_section_statement_type *os; | |
21401fc7 | 7143 | os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); |
c005eb9e AB |
7144 | if (os->addr_tree == NULL |
7145 | && (bfd_link_relocatable (&link_info) | |
7146 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7147 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7148 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7149 | } |
7150 | else | |
7151 | { | |
7152 | lang_output_section_statement_type *os; | |
7153 | const char *name = s->name; | |
7154 | int constraint = 0; | |
7155 | ||
7156 | if (config.orphan_handling == orphan_handling_error) | |
df5f2391 | 7157 | einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), |
c005eb9e AB |
7158 | s, s->owner); |
7159 | ||
7160 | if (config.unique_orphan_sections || unique_section_p (s, NULL)) | |
7161 | constraint = SPECIAL; | |
7162 | ||
7163 | os = ldemul_place_orphan (s, name, constraint); | |
7164 | if (os == NULL) | |
7165 | { | |
21401fc7 | 7166 | os = lang_output_section_statement_lookup (name, constraint, 1); |
c005eb9e AB |
7167 | if (os->addr_tree == NULL |
7168 | && (bfd_link_relocatable (&link_info) | |
7169 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
7170 | os->addr_tree = exp_intop (0); | |
b209b5a6 | 7171 | lang_add_section (&os->children, s, NULL, NULL, os); |
c005eb9e AB |
7172 | } |
7173 | ||
7174 | if (config.orphan_handling == orphan_handling_warn) | |
871b3ab2 | 7175 | einfo (_("%P: warning: orphan section `%pA' from `%pB' being " |
df5f2391 | 7176 | "placed in section `%s'\n"), |
c005eb9e AB |
7177 | s, s->owner, os->name); |
7178 | } | |
7179 | } | |
7180 | ||
08da4cac KH |
7181 | /* Run through the input files and ensure that every input section has |
7182 | somewhere to go. If one is found without a destination then create | |
7183 | an input request and place it into the statement tree. */ | |
252b5132 RH |
7184 | |
7185 | static void | |
1579bae1 | 7186 | lang_place_orphans (void) |
252b5132 | 7187 | { |
e50d8076 | 7188 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
7189 | { |
7190 | asection *s; | |
7191 | ||
1579bae1 | 7192 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 7193 | { |
1579bae1 | 7194 | if (s->output_section == NULL) |
252b5132 | 7195 | { |
396a2467 | 7196 | /* This section of the file is not attached, root |
afd7a018 | 7197 | around for a sensible place for it to go. */ |
252b5132 | 7198 | |
66be1055 | 7199 | if (file->flags.just_syms) |
1449d79b | 7200 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
5b5f4e6f | 7201 | else if (lang_discard_section_p (s)) |
164e712d | 7202 | s->output_section = bfd_abs_section_ptr; |
252b5132 RH |
7203 | else if (strcmp (s->name, "COMMON") == 0) |
7204 | { | |
7205 | /* This is a lonely common section which must have | |
7206 | come from an archive. We attach to the section | |
7207 | with the wildcard. */ | |
0e1862bb | 7208 | if (!bfd_link_relocatable (&link_info) |
252b5132 RH |
7209 | || command_line.force_common_definition) |
7210 | { | |
7211 | if (default_common_section == NULL) | |
66c103b7 | 7212 | default_common_section |
21401fc7 | 7213 | = lang_output_section_statement_lookup (".bss", 0, 1); |
39dcfe18 | 7214 | lang_add_section (&default_common_section->children, s, |
b209b5a6 | 7215 | NULL, NULL, default_common_section); |
252b5132 RH |
7216 | } |
7217 | } | |
252b5132 | 7218 | else |
c005eb9e | 7219 | ldlang_place_orphan (s); |
252b5132 RH |
7220 | } |
7221 | } | |
7222 | } | |
7223 | } | |
7224 | ||
252b5132 | 7225 | void |
1579bae1 | 7226 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 7227 | { |
aa8804e4 | 7228 | flagword *ptr_flags; |
252b5132 | 7229 | |
aa8804e4 | 7230 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
f0673d20 | 7231 | |
252b5132 RH |
7232 | while (*flags) |
7233 | { | |
7234 | switch (*flags) | |
7235 | { | |
f0673d20 NC |
7236 | /* PR 17900: An exclamation mark in the attributes reverses |
7237 | the sense of any of the attributes that follow. */ | |
7238 | case '!': | |
0aa7f586 | 7239 | invert = !invert; |
f0673d20 NC |
7240 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
7241 | break; | |
7242 | ||
252b5132 RH |
7243 | case 'A': case 'a': |
7244 | *ptr_flags |= SEC_ALLOC; | |
7245 | break; | |
7246 | ||
7247 | case 'R': case 'r': | |
7248 | *ptr_flags |= SEC_READONLY; | |
7249 | break; | |
7250 | ||
7251 | case 'W': case 'w': | |
7252 | *ptr_flags |= SEC_DATA; | |
7253 | break; | |
7254 | ||
7255 | case 'X': case 'x': | |
7256 | *ptr_flags |= SEC_CODE; | |
7257 | break; | |
7258 | ||
7259 | case 'L': case 'l': | |
7260 | case 'I': case 'i': | |
7261 | *ptr_flags |= SEC_LOAD; | |
7262 | break; | |
7263 | ||
7264 | default: | |
df5f2391 | 7265 | einfo (_("%F%P: invalid character %c (%d) in flags\n"), |
0aa7f586 | 7266 | *flags, *flags); |
252b5132 RH |
7267 | break; |
7268 | } | |
7269 | flags++; | |
7270 | } | |
7271 | } | |
7272 | ||
d871d478 AM |
7273 | /* Call a function on each real input file. This function will be |
7274 | called on an archive, but not on the elements. */ | |
252b5132 RH |
7275 | |
7276 | void | |
1579bae1 | 7277 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
7278 | { |
7279 | lang_input_statement_type *f; | |
7280 | ||
8ce18f9c | 7281 | for (f = (void *) input_file_chain.head; |
252b5132 | 7282 | f != NULL; |
36983a93 | 7283 | f = f->next_real_file) |
d871d478 AM |
7284 | if (f->flags.real) |
7285 | func (f); | |
252b5132 RH |
7286 | } |
7287 | ||
d871d478 AM |
7288 | /* Call a function on each real file. The function will be called on |
7289 | all the elements of an archive which are included in the link, but | |
7290 | will not be called on the archive file itself. */ | |
252b5132 RH |
7291 | |
7292 | void | |
1579bae1 | 7293 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 7294 | { |
e50d8076 | 7295 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 | 7296 | { |
d871d478 AM |
7297 | if (f->flags.real) |
7298 | func (f); | |
252b5132 RH |
7299 | } |
7300 | } | |
7301 | ||
252b5132 | 7302 | void |
1579bae1 | 7303 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 | 7304 | { |
36983a93 | 7305 | lang_statement_append (&file_chain, entry, &entry->next); |
252b5132 RH |
7306 | |
7307 | /* The BFD linker needs to have a list of all input BFDs involved in | |
7308 | a link. */ | |
9221725d AM |
7309 | ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next |
7310 | && entry->the_bfd->link.next == NULL); | |
f13a99db | 7311 | ASSERT (entry->the_bfd != link_info.output_bfd); |
a9a4c53e AM |
7312 | |
7313 | *link_info.input_bfds_tail = entry->the_bfd; | |
c72f2fb2 | 7314 | link_info.input_bfds_tail = &entry->the_bfd->link.next; |
00f93c44 | 7315 | bfd_set_usrdata (entry->the_bfd, entry); |
252b5132 RH |
7316 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
7317 | ||
7318 | /* Look through the sections and check for any which should not be | |
7319 | included in the link. We need to do this now, so that we can | |
7320 | notice when the backend linker tries to report multiple | |
7321 | definition errors for symbols which are in sections we aren't | |
7322 | going to link. FIXME: It might be better to entirely ignore | |
7323 | symbols which are defined in sections which are going to be | |
7324 | discarded. This would require modifying the backend linker for | |
7325 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
7326 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
7327 | ||
1579bae1 | 7328 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
7329 | } |
7330 | ||
7331 | void | |
1579bae1 | 7332 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
7333 | { |
7334 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 7335 | if (!had_output_filename || !from_script) |
252b5132 RH |
7336 | { |
7337 | output_filename = name; | |
f38a2680 | 7338 | had_output_filename = true; |
252b5132 RH |
7339 | } |
7340 | } | |
7341 | ||
aea4bd9d | 7342 | lang_output_section_statement_type * |
1579bae1 AM |
7343 | lang_enter_output_section_statement (const char *output_section_statement_name, |
7344 | etree_type *address_exp, | |
7345 | enum section_type sectype, | |
c212f39d | 7346 | etree_type *sectype_value, |
1579bae1 AM |
7347 | etree_type *align, |
7348 | etree_type *subalign, | |
0841712e | 7349 | etree_type *ebase, |
1eec346e NC |
7350 | int constraint, |
7351 | int align_with_input) | |
252b5132 RH |
7352 | { |
7353 | lang_output_section_statement_type *os; | |
7354 | ||
66c103b7 | 7355 | os = lang_output_section_statement_lookup (output_section_statement_name, |
21401fc7 | 7356 | constraint, 2); |
967928e9 | 7357 | current_section = os; |
252b5132 | 7358 | |
1579bae1 | 7359 | if (os->addr_tree == NULL) |
08da4cac KH |
7360 | { |
7361 | os->addr_tree = address_exp; | |
7362 | } | |
252b5132 | 7363 | os->sectype = sectype; |
c212f39d FS |
7364 | if (sectype == type_section || sectype == typed_readonly_section) |
7365 | os->sectype_value = sectype_value; | |
7366 | else if (sectype == noload_section) | |
252b5132 | 7367 | os->flags = SEC_NEVER_LOAD; |
c212f39d FS |
7368 | else |
7369 | os->flags = SEC_NO_FLAGS; | |
e5caa5e0 | 7370 | os->block_value = 1; |
66c103b7 AM |
7371 | |
7372 | /* Make next things chain into subchain of this. */ | |
bde18da4 | 7373 | push_stat_ptr (&os->children); |
252b5132 | 7374 | |
1eec346e NC |
7375 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
7376 | if (os->align_lma_with_input && align != NULL) | |
c1c8c1ef | 7377 | einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), |
0aa7f586 | 7378 | NULL); |
1eec346e | 7379 | |
3d9c8f6b AM |
7380 | os->subsection_alignment = subalign; |
7381 | os->section_alignment = align; | |
252b5132 RH |
7382 | |
7383 | os->load_base = ebase; | |
aea4bd9d | 7384 | return os; |
252b5132 RH |
7385 | } |
7386 | ||
252b5132 | 7387 | void |
1579bae1 | 7388 | lang_final (void) |
252b5132 | 7389 | { |
d3ce72d0 NC |
7390 | lang_output_statement_type *new_stmt; |
7391 | ||
7392 | new_stmt = new_stat (lang_output_statement, stat_ptr); | |
7393 | new_stmt->name = output_filename; | |
252b5132 RH |
7394 | } |
7395 | ||
08da4cac KH |
7396 | /* Reset the current counters in the regions. */ |
7397 | ||
e3dc8847 | 7398 | void |
1579bae1 | 7399 | lang_reset_memory_regions (void) |
252b5132 RH |
7400 | { |
7401 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 7402 | asection *o; |
e9ee469a | 7403 | lang_output_section_statement_type *os; |
252b5132 | 7404 | |
1579bae1 | 7405 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 | 7406 | { |
252b5132 | 7407 | p->current = p->origin; |
66e28d60 | 7408 | p->last_os = NULL; |
252b5132 | 7409 | } |
b3327aad | 7410 | |
8ce18f9c | 7411 | for (os = (void *) lang_os_list.head; |
e9ee469a AM |
7412 | os != NULL; |
7413 | os = os->next) | |
cde9e0be | 7414 | { |
f38a2680 AM |
7415 | os->processed_vma = false; |
7416 | os->processed_lma = false; | |
cde9e0be | 7417 | } |
e9ee469a | 7418 | |
f13a99db | 7419 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
1a23a9e6 AM |
7420 | { |
7421 | /* Save the last size for possible use by bfd_relax_section. */ | |
7422 | o->rawsize = o->size; | |
8772de11 MR |
7423 | if (!(o->flags & SEC_FIXED_SIZE)) |
7424 | o->size = 0; | |
1a23a9e6 | 7425 | } |
252b5132 RH |
7426 | } |
7427 | ||
164e712d | 7428 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
7429 | |
7430 | static void | |
1579bae1 | 7431 | gc_section_callback (lang_wild_statement_type *ptr, |
b209b5a6 | 7432 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
1579bae1 AM |
7433 | asection *section, |
7434 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7435 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 7436 | { |
164e712d AM |
7437 | /* If the wild pattern was marked KEEP, the member sections |
7438 | should be as well. */ | |
4dec4d4e RH |
7439 | if (ptr->keep_sections) |
7440 | section->flags |= SEC_KEEP; | |
252b5132 RH |
7441 | } |
7442 | ||
252b5132 RH |
7443 | /* Iterate over sections marking them against GC. */ |
7444 | ||
7445 | static void | |
1579bae1 | 7446 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 7447 | { |
1579bae1 | 7448 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
7449 | { |
7450 | switch (s->header.type) | |
7451 | { | |
7452 | case lang_wild_statement_enum: | |
164e712d | 7453 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 7454 | break; |
252b5132 RH |
7455 | case lang_constructors_statement_enum: |
7456 | lang_gc_sections_1 (constructor_list.head); | |
7457 | break; | |
7458 | case lang_output_section_statement_enum: | |
7459 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
7460 | break; | |
7461 | case lang_group_statement_enum: | |
7462 | lang_gc_sections_1 (s->group_statement.children.head); | |
7463 | break; | |
7464 | default: | |
7465 | break; | |
7466 | } | |
7467 | } | |
7468 | } | |
7469 | ||
7470 | static void | |
1579bae1 | 7471 | lang_gc_sections (void) |
252b5132 | 7472 | { |
252b5132 | 7473 | /* Keep all sections so marked in the link script. */ |
252b5132 RH |
7474 | lang_gc_sections_1 (statement_list.head); |
7475 | ||
9ca57817 | 7476 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
15407e7e | 7477 | the special case of .stabstr debug info. (See bfd/stabs.c) |
9ca57817 | 7478 | Twiddle the flag here, to simplify later linker code. */ |
0e1862bb | 7479 | if (bfd_link_relocatable (&link_info)) |
9ca57817 AM |
7480 | { |
7481 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
7482 | { | |
7483 | asection *sec; | |
0381901e | 7484 | #if BFD_SUPPORTS_PLUGINS |
66be1055 | 7485 | if (f->flags.claimed) |
9e2278f5 AM |
7486 | continue; |
7487 | #endif | |
9ca57817 | 7488 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
15407e7e AM |
7489 | if ((sec->flags & SEC_DEBUGGING) == 0 |
7490 | || strcmp (sec->name, ".stabstr") != 0) | |
9ca57817 AM |
7491 | sec->flags &= ~SEC_EXCLUDE; |
7492 | } | |
7493 | } | |
7494 | ||
57316bff | 7495 | if (link_info.gc_sections) |
f13a99db | 7496 | bfd_gc_sections (link_info.output_bfd, &link_info); |
252b5132 RH |
7497 | } |
7498 | ||
b10a8ae0 L |
7499 | /* Worker for lang_find_relro_sections_1. */ |
7500 | ||
7501 | static void | |
7502 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
7503 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
7504 | asection *section, | |
7505 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
7506 | void *data) | |
7507 | { | |
7508 | /* Discarded, excluded and ignored sections effectively have zero | |
7509 | size. */ | |
7510 | if (section->output_section != NULL | |
f13a99db | 7511 | && section->output_section->owner == link_info.output_bfd |
b10a8ae0 L |
7512 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
7513 | && !IGNORE_SECTION (section) | |
7514 | && section->size != 0) | |
7515 | { | |
f38a2680 AM |
7516 | bool *has_relro_section = (bool *) data; |
7517 | *has_relro_section = true; | |
b10a8ae0 L |
7518 | } |
7519 | } | |
7520 | ||
7521 | /* Iterate over sections for relro sections. */ | |
7522 | ||
7523 | static void | |
7524 | lang_find_relro_sections_1 (lang_statement_union_type *s, | |
f38a2680 | 7525 | bool *has_relro_section) |
b10a8ae0 L |
7526 | { |
7527 | if (*has_relro_section) | |
7528 | return; | |
7529 | ||
7530 | for (; s != NULL; s = s->header.next) | |
7531 | { | |
2085ee26 | 7532 | if (s == expld.dataseg.relro_end_stat) |
b10a8ae0 L |
7533 | break; |
7534 | ||
7535 | switch (s->header.type) | |
7536 | { | |
7537 | case lang_wild_statement_enum: | |
7538 | walk_wild (&s->wild_statement, | |
7539 | find_relro_section_callback, | |
7540 | has_relro_section); | |
7541 | break; | |
7542 | case lang_constructors_statement_enum: | |
7543 | lang_find_relro_sections_1 (constructor_list.head, | |
2085ee26 | 7544 | has_relro_section); |
b10a8ae0 L |
7545 | break; |
7546 | case lang_output_section_statement_enum: | |
7547 | lang_find_relro_sections_1 (s->output_section_statement.children.head, | |
2085ee26 | 7548 | has_relro_section); |
b10a8ae0 L |
7549 | break; |
7550 | case lang_group_statement_enum: | |
7551 | lang_find_relro_sections_1 (s->group_statement.children.head, | |
2085ee26 | 7552 | has_relro_section); |
b10a8ae0 L |
7553 | break; |
7554 | default: | |
7555 | break; | |
7556 | } | |
7557 | } | |
7558 | } | |
7559 | ||
7560 | static void | |
7561 | lang_find_relro_sections (void) | |
7562 | { | |
f38a2680 | 7563 | bool has_relro_section = false; |
b10a8ae0 L |
7564 | |
7565 | /* Check all sections in the link script. */ | |
7566 | ||
7567 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, | |
2085ee26 | 7568 | &has_relro_section); |
b10a8ae0 L |
7569 | |
7570 | if (!has_relro_section) | |
f38a2680 | 7571 | link_info.relro = false; |
b10a8ae0 L |
7572 | } |
7573 | ||
fbbc3759 L |
7574 | /* Relax all sections until bfd_relax_section gives up. */ |
7575 | ||
eaeb0a9d | 7576 | void |
f38a2680 | 7577 | lang_relax_sections (bool need_layout) |
fbbc3759 | 7578 | { |
6a91be86 L |
7579 | /* NB: Also enable relaxation to layout sections for DT_RELR. */ |
7580 | if (RELAXATION_ENABLED || link_info.enable_dt_relr) | |
fbbc3759 | 7581 | { |
eaeb0a9d AM |
7582 | /* We may need more than one relaxation pass. */ |
7583 | int i = link_info.relax_pass; | |
fbbc3759 | 7584 | |
eaeb0a9d AM |
7585 | /* The backend can use it to determine the current pass. */ |
7586 | link_info.relax_pass = 0; | |
fbbc3759 | 7587 | |
eaeb0a9d AM |
7588 | while (i--) |
7589 | { | |
7590 | /* Keep relaxing until bfd_relax_section gives up. */ | |
f38a2680 | 7591 | bool relax_again; |
fbbc3759 | 7592 | |
eaeb0a9d AM |
7593 | link_info.relax_trip = -1; |
7594 | do | |
7595 | { | |
7596 | link_info.relax_trip++; | |
7597 | ||
7598 | /* Note: pe-dll.c does something like this also. If you find | |
7599 | you need to change this code, you probably need to change | |
7600 | pe-dll.c also. DJ */ | |
7601 | ||
7602 | /* Do all the assignments with our current guesses as to | |
7603 | section sizes. */ | |
2f65ac72 | 7604 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d AM |
7605 | |
7606 | /* We must do this after lang_do_assignments, because it uses | |
7607 | size. */ | |
7608 | lang_reset_memory_regions (); | |
7609 | ||
7610 | /* Perform another relax pass - this time we know where the | |
7611 | globals are, so can make a better guess. */ | |
f38a2680 AM |
7612 | relax_again = false; |
7613 | lang_size_sections (&relax_again, false); | |
eaeb0a9d AM |
7614 | } |
7615 | while (relax_again); | |
7616 | ||
7617 | link_info.relax_pass++; | |
7618 | } | |
f38a2680 | 7619 | need_layout = true; |
eaeb0a9d | 7620 | } |
fbbc3759 | 7621 | |
eaeb0a9d AM |
7622 | if (need_layout) |
7623 | { | |
7624 | /* Final extra sizing to report errors. */ | |
2f65ac72 | 7625 | lang_do_assignments (lang_assigning_phase_enum); |
eaeb0a9d | 7626 | lang_reset_memory_regions (); |
f38a2680 | 7627 | lang_size_sections (NULL, true); |
fbbc3759 | 7628 | } |
fbbc3759 L |
7629 | } |
7630 | ||
0381901e | 7631 | #if BFD_SUPPORTS_PLUGINS |
8543fde5 DK |
7632 | /* Find the insert point for the plugin's replacement files. We |
7633 | place them after the first claimed real object file, or if the | |
7634 | first claimed object is an archive member, after the last real | |
7635 | object file immediately preceding the archive. In the event | |
7636 | no objects have been claimed at all, we return the first dummy | |
7637 | object file on the list as the insert point; that works, but | |
7638 | the callee must be careful when relinking the file_chain as it | |
7639 | is not actually on that chain, only the statement_list and the | |
7640 | input_file list; in that case, the replacement files must be | |
7641 | inserted at the head of the file_chain. */ | |
7642 | ||
7643 | static lang_input_statement_type * | |
f38a2680 | 7644 | find_replacements_insert_point (bool *before) |
8543fde5 DK |
7645 | { |
7646 | lang_input_statement_type *claim1, *lastobject; | |
8ce18f9c AM |
7647 | lastobject = (void *) input_file_chain.head; |
7648 | for (claim1 = (void *) file_chain.head; | |
8543fde5 | 7649 | claim1 != NULL; |
36983a93 | 7650 | claim1 = claim1->next) |
8543fde5 | 7651 | { |
66be1055 | 7652 | if (claim1->flags.claimed) |
128bf1fe AM |
7653 | { |
7654 | *before = claim1->flags.claim_archive; | |
7655 | return claim1->flags.claim_archive ? lastobject : claim1; | |
7656 | } | |
8543fde5 | 7657 | /* Update lastobject if this is a real object file. */ |
0aa7f586 | 7658 | if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) |
8543fde5 DK |
7659 | lastobject = claim1; |
7660 | } | |
7661 | /* No files were claimed by the plugin. Choose the last object | |
7662 | file found on the list (maybe the first, dummy entry) as the | |
7663 | insert point. */ | |
f38a2680 | 7664 | *before = false; |
8543fde5 DK |
7665 | return lastobject; |
7666 | } | |
9e2278f5 | 7667 | |
1fa4ec6a AM |
7668 | /* Find where to insert ADD, an archive element or shared library |
7669 | added during a rescan. */ | |
7670 | ||
36983a93 | 7671 | static lang_input_statement_type ** |
1fa4ec6a AM |
7672 | find_rescan_insertion (lang_input_statement_type *add) |
7673 | { | |
7674 | bfd *add_bfd = add->the_bfd; | |
7675 | lang_input_statement_type *f; | |
7676 | lang_input_statement_type *last_loaded = NULL; | |
7677 | lang_input_statement_type *before = NULL; | |
36983a93 | 7678 | lang_input_statement_type **iter = NULL; |
1fa4ec6a AM |
7679 | |
7680 | if (add_bfd->my_archive != NULL) | |
7681 | add_bfd = add_bfd->my_archive; | |
7682 | ||
7683 | /* First look through the input file chain, to find an object file | |
7684 | before the one we've rescanned. Normal object files always | |
7685 | appear on both the input file chain and the file chain, so this | |
7686 | lets us get quickly to somewhere near the correct place on the | |
7687 | file chain if it is full of archive elements. Archives don't | |
7688 | appear on the file chain, but if an element has been extracted | |
7689 | then their input_statement->next points at it. */ | |
8ce18f9c | 7690 | for (f = (void *) input_file_chain.head; |
1fa4ec6a | 7691 | f != NULL; |
36983a93 | 7692 | f = f->next_real_file) |
1fa4ec6a AM |
7693 | { |
7694 | if (f->the_bfd == add_bfd) | |
7695 | { | |
7696 | before = last_loaded; | |
7697 | if (f->next != NULL) | |
36983a93 | 7698 | return &f->next->next; |
1fa4ec6a AM |
7699 | } |
7700 | if (f->the_bfd != NULL && f->next != NULL) | |
7701 | last_loaded = f; | |
7702 | } | |
7703 | ||
7704 | for (iter = before ? &before->next : &file_chain.head->input_statement.next; | |
7705 | *iter != NULL; | |
36983a93 AM |
7706 | iter = &(*iter)->next) |
7707 | if (!(*iter)->flags.claim_archive | |
7708 | && (*iter)->the_bfd->my_archive == NULL) | |
1fa4ec6a AM |
7709 | break; |
7710 | ||
7711 | return iter; | |
7712 | } | |
7713 | ||
9e2278f5 AM |
7714 | /* Insert SRCLIST into DESTLIST after given element by chaining |
7715 | on FIELD as the next-pointer. (Counterintuitively does not need | |
7716 | a pointer to the actual after-node itself, just its chain field.) */ | |
7717 | ||
7718 | static void | |
7719 | lang_list_insert_after (lang_statement_list_type *destlist, | |
7720 | lang_statement_list_type *srclist, | |
7721 | lang_statement_union_type **field) | |
7722 | { | |
7723 | *(srclist->tail) = *field; | |
7724 | *field = srclist->head; | |
7725 | if (destlist->tail == field) | |
7726 | destlist->tail = srclist->tail; | |
7727 | } | |
7728 | ||
7729 | /* Detach new nodes added to DESTLIST since the time ORIGLIST | |
7730 | was taken as a copy of it and leave them in ORIGLIST. */ | |
7731 | ||
7732 | static void | |
7733 | lang_list_remove_tail (lang_statement_list_type *destlist, | |
7734 | lang_statement_list_type *origlist) | |
7735 | { | |
7736 | union lang_statement_union **savetail; | |
7737 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ | |
7738 | ASSERT (origlist->head == destlist->head); | |
7739 | savetail = origlist->tail; | |
7740 | origlist->head = *(savetail); | |
7741 | origlist->tail = destlist->tail; | |
7742 | destlist->tail = savetail; | |
7743 | *savetail = NULL; | |
7744 | } | |
128bf1fe AM |
7745 | |
7746 | static lang_statement_union_type ** | |
7747 | find_next_input_statement (lang_statement_union_type **s) | |
7748 | { | |
7749 | for ( ; *s; s = &(*s)->header.next) | |
7750 | { | |
7751 | lang_statement_union_type **t; | |
7752 | switch ((*s)->header.type) | |
7753 | { | |
7754 | case lang_input_statement_enum: | |
7755 | return s; | |
7756 | case lang_wild_statement_enum: | |
7757 | t = &(*s)->wild_statement.children.head; | |
7758 | break; | |
7759 | case lang_group_statement_enum: | |
7760 | t = &(*s)->group_statement.children.head; | |
7761 | break; | |
7762 | case lang_output_section_statement_enum: | |
7763 | t = &(*s)->output_section_statement.children.head; | |
7764 | break; | |
7765 | default: | |
7766 | continue; | |
7767 | } | |
7768 | t = find_next_input_statement (t); | |
7769 | if (*t) | |
7770 | return t; | |
7771 | } | |
7772 | return s; | |
7773 | } | |
0381901e | 7774 | #endif /* BFD_SUPPORTS_PLUGINS */ |
8543fde5 | 7775 | |
4153b6db NC |
7776 | /* Add NAME to the list of garbage collection entry points. */ |
7777 | ||
7778 | void | |
0aa7f586 | 7779 | lang_add_gc_name (const char *name) |
4153b6db NC |
7780 | { |
7781 | struct bfd_sym_chain *sym; | |
7782 | ||
7783 | if (name == NULL) | |
7784 | return; | |
7785 | ||
988de25b | 7786 | sym = stat_alloc (sizeof (*sym)); |
4153b6db NC |
7787 | |
7788 | sym->next = link_info.gc_sym_list; | |
7789 | sym->name = name; | |
7790 | link_info.gc_sym_list = sym; | |
7791 | } | |
7792 | ||
fbf05aa7 L |
7793 | /* Check relocations. */ |
7794 | ||
7795 | static void | |
7796 | lang_check_relocs (void) | |
7797 | { | |
4f3b23b3 | 7798 | if (link_info.check_relocs_after_open_input) |
fbf05aa7 L |
7799 | { |
7800 | bfd *abfd; | |
7801 | ||
7802 | for (abfd = link_info.input_bfds; | |
7803 | abfd != (bfd *) NULL; abfd = abfd->link.next) | |
4f3b23b3 | 7804 | if (!bfd_link_check_relocs (abfd, &link_info)) |
fbf05aa7 | 7805 | { |
4f3b23b3 | 7806 | /* No object output, fail return. */ |
f38a2680 | 7807 | config.make_executable = false; |
4f3b23b3 NC |
7808 | /* Note: we do not abort the loop, but rather |
7809 | continue the scan in case there are other | |
7810 | bad relocations to report. */ | |
fbf05aa7 L |
7811 | } |
7812 | } | |
7813 | } | |
7814 | ||
77f5e65e AB |
7815 | /* Look through all output sections looking for places where we can |
7816 | propagate forward the lma region. */ | |
7817 | ||
7818 | static void | |
7819 | lang_propagate_lma_regions (void) | |
7820 | { | |
7821 | lang_output_section_statement_type *os; | |
7822 | ||
8ce18f9c | 7823 | for (os = (void *) lang_os_list.head; |
77f5e65e AB |
7824 | os != NULL; |
7825 | os = os->next) | |
7826 | { | |
7827 | if (os->prev != NULL | |
7828 | && os->lma_region == NULL | |
7829 | && os->load_base == NULL | |
7830 | && os->addr_tree == NULL | |
7831 | && os->region == os->prev->region) | |
7832 | os->lma_region = os->prev->lma_region; | |
7833 | } | |
7834 | } | |
7835 | ||
50be5d11 MM |
7836 | static void |
7837 | reset_one_wild (lang_statement_union_type *statement) | |
7838 | { | |
7839 | if (statement->header.type == lang_wild_statement_enum) | |
7840 | { | |
7841 | lang_wild_statement_type *stmt = &statement->wild_statement; | |
50be5d11 MM |
7842 | lang_list_init (&stmt->matching_sections); |
7843 | } | |
7844 | } | |
7845 | ||
7846 | static void | |
7847 | reset_resolved_wilds (void) | |
7848 | { | |
7849 | lang_for_each_statement (reset_one_wild); | |
7850 | } | |
7851 | ||
252b5132 | 7852 | void |
1579bae1 | 7853 | lang_process (void) |
252b5132 | 7854 | { |
55255dae | 7855 | /* Finalize dynamic list. */ |
40b36307 L |
7856 | if (link_info.dynamic_list) |
7857 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); | |
55255dae | 7858 | |
252b5132 RH |
7859 | current_target = default_target; |
7860 | ||
08da4cac KH |
7861 | /* Open the output file. */ |
7862 | lang_for_each_statement (ldlang_open_output); | |
61826503 | 7863 | init_opb (NULL); |
252b5132 RH |
7864 | |
7865 | ldemul_create_output_section_statements (); | |
7866 | ||
08da4cac | 7867 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
7868 | lang_place_undefineds (); |
7869 | ||
082b7297 | 7870 | if (!bfd_section_already_linked_table_init ()) |
df5f2391 | 7871 | einfo (_("%F%P: can not create hash table: %E\n")); |
9503fd87 | 7872 | |
874ef038 AB |
7873 | /* A first pass through the memory regions ensures that if any region |
7874 | references a symbol for its origin or length then this symbol will be | |
7875 | added to the symbol table. Having these symbols in the symbol table | |
7876 | means that when we call open_input_bfds PROVIDE statements will | |
7877 | trigger to provide any needed symbols. The regions origins and | |
7878 | lengths are not assigned as a result of this call. */ | |
f38a2680 | 7879 | lang_do_memory_regions (false); |
874ef038 | 7880 | |
08da4cac | 7881 | /* Create a bfd for each input file. */ |
252b5132 | 7882 | current_target = default_target; |
165f707a | 7883 | lang_statement_iteration++; |
486329aa | 7884 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
874ef038 AB |
7885 | |
7886 | /* Now that open_input_bfds has processed assignments and provide | |
7887 | statements we can give values to symbolic origin/length now. */ | |
f38a2680 | 7888 | lang_do_memory_regions (true); |
252b5132 | 7889 | |
0381901e | 7890 | #if BFD_SUPPORTS_PLUGINS |
716db898 | 7891 | if (link_info.lto_plugin_active) |
f84854b6 | 7892 | { |
8543fde5 DK |
7893 | lang_statement_list_type added; |
7894 | lang_statement_list_type files, inputfiles; | |
9e2278f5 | 7895 | |
320fdefe L |
7896 | ldemul_before_plugin_all_symbols_read (); |
7897 | ||
f84854b6 L |
7898 | /* Now all files are read, let the plugin(s) decide if there |
7899 | are any more to be added to the link before we call the | |
8543fde5 DK |
7900 | emulation's after_open hook. We create a private list of |
7901 | input statements for this purpose, which we will eventually | |
370dfff4 | 7902 | insert into the global statement list after the first claimed |
8543fde5 DK |
7903 | file. */ |
7904 | added = *stat_ptr; | |
7905 | /* We need to manipulate all three chains in synchrony. */ | |
7906 | files = file_chain; | |
7907 | inputfiles = input_file_chain; | |
f84854b6 | 7908 | if (plugin_call_all_symbols_read ()) |
df5f2391 | 7909 | einfo (_("%F%P: %s: plugin reported error after all symbols read\n"), |
f84854b6 | 7910 | plugin_error_plugin ()); |
f38a2680 | 7911 | link_info.lto_all_symbols_read = true; |
8543fde5 | 7912 | /* Open any newly added files, updating the file chains. */ |
b02c4f16 | 7913 | plugin_undefs = link_info.hash->undefs_tail; |
cdaa438c | 7914 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
b02c4f16 AM |
7915 | if (plugin_undefs == link_info.hash->undefs_tail) |
7916 | plugin_undefs = NULL; | |
8543fde5 DK |
7917 | /* Restore the global list pointer now they have all been added. */ |
7918 | lang_list_remove_tail (stat_ptr, &added); | |
7919 | /* And detach the fresh ends of the file lists. */ | |
7920 | lang_list_remove_tail (&file_chain, &files); | |
7921 | lang_list_remove_tail (&input_file_chain, &inputfiles); | |
7922 | /* Were any new files added? */ | |
7923 | if (added.head != NULL) | |
7924 | { | |
7925 | /* If so, we will insert them into the statement list immediately | |
128bf1fe AM |
7926 | after the first input file that was claimed by the plugin, |
7927 | unless that file was an archive in which case it is inserted | |
7928 | immediately before. */ | |
f38a2680 | 7929 | bool before; |
128bf1fe AM |
7930 | lang_statement_union_type **prev; |
7931 | plugin_insert = find_replacements_insert_point (&before); | |
8543fde5 DK |
7932 | /* If a plugin adds input files without having claimed any, we |
7933 | don't really have a good idea where to place them. Just putting | |
7934 | them at the start or end of the list is liable to leave them | |
7935 | outside the crtbegin...crtend range. */ | |
9e2278f5 AM |
7936 | ASSERT (plugin_insert != NULL); |
7937 | /* Splice the new statement list into the old one. */ | |
128bf1fe AM |
7938 | prev = &plugin_insert->header.next; |
7939 | if (before) | |
7940 | { | |
7941 | prev = find_next_input_statement (prev); | |
36983a93 | 7942 | if (*prev != (void *) plugin_insert->next_real_file) |
128bf1fe | 7943 | { |
a19826f4 | 7944 | /* We didn't find the expected input statement. |
1f1f5b92 | 7945 | Fall back to adding after plugin_insert. */ |
128bf1fe AM |
7946 | prev = &plugin_insert->header.next; |
7947 | } | |
7948 | } | |
7949 | lang_list_insert_after (stat_ptr, &added, prev); | |
8543fde5 DK |
7950 | /* Likewise for the file chains. */ |
7951 | lang_list_insert_after (&input_file_chain, &inputfiles, | |
36983a93 | 7952 | (void *) &plugin_insert->next_real_file); |
8543fde5 DK |
7953 | /* We must be careful when relinking file_chain; we may need to |
7954 | insert the new files at the head of the list if the insert | |
7955 | point chosen is the dummy first input file. */ | |
9e2278f5 | 7956 | if (plugin_insert->filename) |
36983a93 AM |
7957 | lang_list_insert_after (&file_chain, &files, |
7958 | (void *) &plugin_insert->next); | |
8543fde5 DK |
7959 | else |
7960 | lang_list_insert_after (&file_chain, &files, &file_chain.head); | |
9e2278f5 AM |
7961 | |
7962 | /* Rescan archives in case new undefined symbols have appeared. */ | |
1fa4ec6a | 7963 | files = file_chain; |
165f707a | 7964 | lang_statement_iteration++; |
9e2278f5 | 7965 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
1fa4ec6a AM |
7966 | lang_list_remove_tail (&file_chain, &files); |
7967 | while (files.head != NULL) | |
7968 | { | |
36983a93 AM |
7969 | lang_input_statement_type **insert; |
7970 | lang_input_statement_type **iter, *temp; | |
1fa4ec6a AM |
7971 | bfd *my_arch; |
7972 | ||
7973 | insert = find_rescan_insertion (&files.head->input_statement); | |
7974 | /* All elements from an archive can be added at once. */ | |
7975 | iter = &files.head->input_statement.next; | |
7976 | my_arch = files.head->input_statement.the_bfd->my_archive; | |
7977 | if (my_arch != NULL) | |
36983a93 AM |
7978 | for (; *iter != NULL; iter = &(*iter)->next) |
7979 | if ((*iter)->the_bfd->my_archive != my_arch) | |
1fa4ec6a AM |
7980 | break; |
7981 | temp = *insert; | |
36983a93 AM |
7982 | *insert = &files.head->input_statement; |
7983 | files.head = (lang_statement_union_type *) *iter; | |
1fa4ec6a AM |
7984 | *iter = temp; |
7985 | if (my_arch != NULL) | |
7986 | { | |
00f93c44 | 7987 | lang_input_statement_type *parent = bfd_usrdata (my_arch); |
1fa4ec6a | 7988 | if (parent != NULL) |
36983a93 | 7989 | parent->next = (lang_input_statement_type *) |
1fa4ec6a AM |
7990 | ((char *) iter |
7991 | - offsetof (lang_input_statement_type, next)); | |
7992 | } | |
7993 | } | |
8543fde5 | 7994 | } |
f84854b6 | 7995 | } |
0381901e | 7996 | #endif /* BFD_SUPPORTS_PLUGINS */ |
5d3236ee | 7997 | |
0aa7f586 AM |
7998 | /* Make sure that nobody has tried to add a symbol to this list |
7999 | before now. */ | |
4153b6db NC |
8000 | ASSERT (link_info.gc_sym_list == NULL); |
8001 | ||
e3e942e9 | 8002 | link_info.gc_sym_list = &entry_symbol; |
41f46ed9 | 8003 | |
e3e942e9 | 8004 | if (entry_symbol.name == NULL) |
41f46ed9 SKS |
8005 | { |
8006 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
8007 | ||
8008 | /* entry_symbol is normally initialied by a ENTRY definition in the | |
8009 | linker script or the -e command line option. But if neither of | |
8010 | these have been used, the target specific backend may still have | |
4153b6db | 8011 | provided an entry symbol via a call to lang_default_entry(). |
41f46ed9 SKS |
8012 | Unfortunately this value will not be processed until lang_end() |
8013 | is called, long after this function has finished. So detect this | |
8014 | case here and add the target's entry symbol to the list of starting | |
8015 | points for garbage collection resolution. */ | |
4153b6db | 8016 | lang_add_gc_name (entry_symbol_default); |
41f46ed9 SKS |
8017 | } |
8018 | ||
4153b6db NC |
8019 | lang_add_gc_name (link_info.init_function); |
8020 | lang_add_gc_name (link_info.fini_function); | |
e3e942e9 | 8021 | |
252b5132 | 8022 | ldemul_after_open (); |
16e4ecc0 AM |
8023 | if (config.map_file != NULL) |
8024 | lang_print_asneeded (); | |
252b5132 | 8025 | |
1ff6de03 NA |
8026 | ldlang_open_ctf (); |
8027 | ||
082b7297 | 8028 | bfd_section_already_linked_table_free (); |
9503fd87 | 8029 | |
252b5132 RH |
8030 | /* Make sure that we're not mixing architectures. We call this |
8031 | after all the input files have been opened, but before we do any | |
8032 | other processing, so that any operations merge_private_bfd_data | |
8033 | does on the output file will be known during the rest of the | |
8034 | link. */ | |
8035 | lang_check (); | |
8036 | ||
8037 | /* Handle .exports instead of a version script if we're told to do so. */ | |
8038 | if (command_line.version_exports_section) | |
8039 | lang_do_version_exports_section (); | |
8040 | ||
8041 | /* Build all sets based on the information gathered from the input | |
8042 | files. */ | |
8043 | ldctor_build_sets (); | |
8044 | ||
97da0e26 | 8045 | lang_symbol_tweaks (); |
7dba9362 | 8046 | |
2aa9aad9 NC |
8047 | /* PR 13683: We must rerun the assignments prior to running garbage |
8048 | collection in order to make sure that all symbol aliases are resolved. */ | |
8049 | lang_do_assignments (lang_mark_phase_enum); | |
8050 | expld.phase = lang_first_phase_enum; | |
8051 | ||
08da4cac | 8052 | /* Size up the common data. */ |
252b5132 RH |
8053 | lang_common (); |
8054 | ||
b1eecf6f MM |
8055 | if (0) |
8056 | debug_prefix_tree (); | |
8057 | ||
8058 | resolve_wilds (); | |
8059 | ||
c4621b33 AM |
8060 | /* Remove unreferenced sections if asked to. */ |
8061 | lang_gc_sections (); | |
8062 | ||
5789f845 AM |
8063 | lang_mark_undefineds (); |
8064 | ||
fbf05aa7 L |
8065 | /* Check relocations. */ |
8066 | lang_check_relocs (); | |
8067 | ||
5c3261b0 L |
8068 | ldemul_after_check_relocs (); |
8069 | ||
50be5d11 MM |
8070 | /* There might have been new sections created (e.g. as result of |
8071 | checking relocs to need a .got, or suchlike), so to properly order | |
8072 | them into our lists of matching sections reset them here. */ | |
8073 | reset_resolved_wilds (); | |
b1eecf6f | 8074 | resolve_wilds (); |
50be5d11 | 8075 | |
af31506c MM |
8076 | /* Update wild statements in case the user gave --sort-section. |
8077 | Note how the option might have come after the linker script and | |
8078 | so couldn't have been set when the wild statements were created. */ | |
bcaa7b3e L |
8079 | update_wild_statements (statement_list.head); |
8080 | ||
252b5132 | 8081 | /* Run through the contours of the script and attach input sections |
08da4cac | 8082 | to the correct output sections. */ |
e759c116 | 8083 | lang_statement_iteration++; |
1579bae1 | 8084 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 8085 | |
776ab89f AM |
8086 | /* Start at the statement immediately after the special abs_section |
8087 | output statement, so that it isn't reordered. */ | |
8088 | process_insert_statements (&lang_os_list.head->header.next); | |
53d25da6 | 8089 | |
9b538ba7 L |
8090 | ldemul_before_place_orphans (); |
8091 | ||
08da4cac | 8092 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
8093 | lang_place_orphans (); |
8094 | ||
0e1862bb | 8095 | if (!bfd_link_relocatable (&link_info)) |
862120bd | 8096 | { |
57ceae94 AM |
8097 | asection *found; |
8098 | ||
8099 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
8100 | sections, so that GCed sections are not merged, but before | |
8101 | assigning dynamic symbols, since removing whole input sections | |
8102 | is hard then. */ | |
f13a99db | 8103 | bfd_merge_sections (link_info.output_bfd, &link_info); |
57ceae94 | 8104 | |
862120bd | 8105 | /* Look for a text section and set the readonly attribute in it. */ |
f13a99db | 8106 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
862120bd | 8107 | |
1579bae1 | 8108 | if (found != NULL) |
862120bd AM |
8109 | { |
8110 | if (config.text_read_only) | |
8111 | found->flags |= SEC_READONLY; | |
8112 | else | |
8113 | found->flags &= ~SEC_READONLY; | |
8114 | } | |
8115 | } | |
8116 | ||
1ff6de03 NA |
8117 | /* Merge together CTF sections. After this, only the symtab-dependent |
8118 | function and data object sections need adjustment. */ | |
8119 | lang_merge_ctf (); | |
8120 | ||
8121 | /* Emit the CTF, iff the emulation doesn't need to do late emission after | |
8122 | examining things laid out late, like the strtab. */ | |
8123 | lang_write_ctf (0); | |
8124 | ||
77f5e65e AB |
8125 | /* Copy forward lma regions for output sections in same lma region. */ |
8126 | lang_propagate_lma_regions (); | |
8127 | ||
7dba9362 AM |
8128 | /* Defining __start/__stop symbols early for --gc-sections to work |
8129 | around a glibc build problem can result in these symbols being | |
8130 | defined when they should not be. Fix them now. */ | |
8131 | if (config.build_constructors) | |
8132 | lang_undef_start_stop (); | |
8133 | ||
8134 | /* Define .startof./.sizeof. symbols with preliminary values before | |
8135 | dynamic symbols are created. */ | |
8136 | if (!bfd_link_relocatable (&link_info)) | |
8137 | lang_init_startof_sizeof (); | |
8138 | ||
862120bd AM |
8139 | /* Do anything special before sizing sections. This is where ELF |
8140 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
8141 | ldemul_before_allocation (); |
8142 | ||
8143 | /* We must record the program headers before we try to fix the | |
8144 | section positions, since they will affect SIZEOF_HEADERS. */ | |
8145 | lang_record_phdrs (); | |
8146 | ||
b10a8ae0 | 8147 | /* Check relro sections. */ |
0e1862bb | 8148 | if (link_info.relro && !bfd_link_relocatable (&link_info)) |
b10a8ae0 L |
8149 | lang_find_relro_sections (); |
8150 | ||
b3327aad | 8151 | /* Size up the sections. */ |
0aa7f586 | 8152 | lang_size_sections (NULL, !RELAXATION_ENABLED); |
b3327aad | 8153 | |
252b5132 | 8154 | /* See if anything special should be done now we know how big |
eaeb0a9d | 8155 | everything is. This is where relaxation is done. */ |
252b5132 RH |
8156 | ldemul_after_allocation (); |
8157 | ||
7dba9362 AM |
8158 | /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ |
8159 | lang_finalize_start_stop (); | |
252b5132 | 8160 | |
7f0cfc6e AM |
8161 | /* Do all the assignments again, to report errors. Assignment |
8162 | statements are processed multiple times, updating symbols; In | |
8163 | open_input_bfds, lang_do_assignments, and lang_size_sections. | |
8164 | Since lang_relax_sections calls lang_do_assignments, symbols are | |
8165 | also updated in ldemul_after_allocation. */ | |
2f65ac72 | 8166 | lang_do_assignments (lang_final_phase_enum); |
252b5132 | 8167 | |
8ded5a0f AM |
8168 | ldemul_finish (); |
8169 | ||
975f8a9e AM |
8170 | /* Convert absolute symbols to section relative. */ |
8171 | ldexp_finalize_syms (); | |
8172 | ||
252b5132 | 8173 | /* Make sure that the section addresses make sense. */ |
662ef24b | 8174 | if (command_line.check_section_addresses) |
252b5132 | 8175 | lang_check_section_addresses (); |
5f992e62 | 8176 | |
0a618243 AB |
8177 | /* Check any required symbols are known. */ |
8178 | ldlang_check_require_defined_symbols (); | |
8179 | ||
750877ba | 8180 | lang_end (); |
252b5132 RH |
8181 | } |
8182 | ||
8183 | /* EXPORTED TO YACC */ | |
8184 | ||
8185 | void | |
1579bae1 AM |
8186 | lang_add_wild (struct wildcard_spec *filespec, |
8187 | struct wildcard_list *section_list, | |
f38a2680 | 8188 | bool keep_sections) |
252b5132 | 8189 | { |
b6bf44ba | 8190 | struct wildcard_list *curr, *next; |
d3ce72d0 | 8191 | lang_wild_statement_type *new_stmt; |
af31506c | 8192 | bool any_specs_sorted = false; |
b6bf44ba AM |
8193 | |
8194 | /* Reverse the list as the parser puts it back to front. */ | |
8195 | for (curr = section_list, section_list = NULL; | |
8196 | curr != NULL; | |
8197 | section_list = curr, curr = next) | |
8198 | { | |
af31506c MM |
8199 | if (curr->spec.sorted != none && curr->spec.sorted != by_none) |
8200 | any_specs_sorted = true; | |
b6bf44ba AM |
8201 | next = curr->next; |
8202 | curr->next = section_list; | |
8203 | } | |
8204 | ||
8205 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 8206 | { |
b6bf44ba AM |
8207 | if (strcmp (filespec->name, "*") == 0) |
8208 | filespec->name = NULL; | |
0aa7f586 | 8209 | else if (!wildcardp (filespec->name)) |
f38a2680 | 8210 | lang_has_input_file = true; |
252b5132 | 8211 | } |
b6bf44ba | 8212 | |
d3ce72d0 NC |
8213 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
8214 | new_stmt->filename = NULL; | |
f38a2680 | 8215 | new_stmt->filenames_sorted = false; |
af31506c | 8216 | new_stmt->any_specs_sorted = any_specs_sorted; |
ae17ab41 | 8217 | new_stmt->section_flag_list = NULL; |
8f1732fc | 8218 | new_stmt->exclude_name_list = NULL; |
b6bf44ba | 8219 | if (filespec != NULL) |
252b5132 | 8220 | { |
d3ce72d0 NC |
8221 | new_stmt->filename = filespec->name; |
8222 | new_stmt->filenames_sorted = filespec->sorted == by_name; | |
ae17ab41 | 8223 | new_stmt->section_flag_list = filespec->section_flag_list; |
8f1732fc | 8224 | new_stmt->exclude_name_list = filespec->exclude_name_list; |
252b5132 | 8225 | } |
d3ce72d0 NC |
8226 | new_stmt->section_list = section_list; |
8227 | new_stmt->keep_sections = keep_sections; | |
8228 | lang_list_init (&new_stmt->children); | |
43ae96e9 | 8229 | lang_list_init (&new_stmt->matching_sections); |
d3ce72d0 | 8230 | analyze_walk_wild_section_handler (new_stmt); |
b1eecf6f MM |
8231 | if (0) |
8232 | { | |
8233 | printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); | |
8234 | for (curr = new_stmt->section_list; curr; curr = curr->next) | |
8235 | printf ("%s ", curr->spec.name ? curr->spec.name : "*"); | |
8236 | printf (")\n"); | |
8237 | } | |
252b5132 RH |
8238 | } |
8239 | ||
8240 | void | |
ba916c8a MM |
8241 | lang_section_start (const char *name, etree_type *address, |
8242 | const segment_type *segment) | |
252b5132 | 8243 | { |
d1778b88 | 8244 | lang_address_statement_type *ad; |
252b5132 | 8245 | |
d1778b88 | 8246 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
8247 | ad->section_name = name; |
8248 | ad->address = address; | |
ba916c8a | 8249 | ad->segment = segment; |
252b5132 RH |
8250 | } |
8251 | ||
8252 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
8253 | because of a -e argument on the command line, or zero if this is | |
8254 | called by ENTRY in a linker script. Command line arguments take | |
8255 | precedence. */ | |
8256 | ||
8257 | void | |
f38a2680 | 8258 | lang_add_entry (const char *name, bool cmdline) |
252b5132 | 8259 | { |
e3e942e9 | 8260 | if (entry_symbol.name == NULL |
252b5132 | 8261 | || cmdline |
0aa7f586 | 8262 | || !entry_from_cmdline) |
252b5132 | 8263 | { |
e3e942e9 | 8264 | entry_symbol.name = name; |
252b5132 RH |
8265 | entry_from_cmdline = cmdline; |
8266 | } | |
8267 | } | |
8268 | ||
a359509e ZW |
8269 | /* Set the default start symbol to NAME. .em files should use this, |
8270 | not lang_add_entry, to override the use of "start" if neither the | |
8271 | linker script nor the command line specifies an entry point. NAME | |
8272 | must be permanently allocated. */ | |
8273 | void | |
8274 | lang_default_entry (const char *name) | |
8275 | { | |
8276 | entry_symbol_default = name; | |
8277 | } | |
8278 | ||
252b5132 | 8279 | void |
1579bae1 | 8280 | lang_add_target (const char *name) |
252b5132 | 8281 | { |
d3ce72d0 | 8282 | lang_target_statement_type *new_stmt; |
252b5132 | 8283 | |
d3ce72d0 NC |
8284 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
8285 | new_stmt->target = name; | |
252b5132 RH |
8286 | } |
8287 | ||
8288 | void | |
1579bae1 | 8289 | lang_add_map (const char *name) |
252b5132 RH |
8290 | { |
8291 | while (*name) | |
8292 | { | |
8293 | switch (*name) | |
8294 | { | |
08da4cac | 8295 | case 'F': |
f38a2680 | 8296 | map_option_f = true; |
252b5132 RH |
8297 | break; |
8298 | } | |
8299 | name++; | |
8300 | } | |
8301 | } | |
8302 | ||
8303 | void | |
1579bae1 | 8304 | lang_add_fill (fill_type *fill) |
252b5132 | 8305 | { |
d3ce72d0 | 8306 | lang_fill_statement_type *new_stmt; |
252b5132 | 8307 | |
d3ce72d0 NC |
8308 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
8309 | new_stmt->fill = fill; | |
252b5132 RH |
8310 | } |
8311 | ||
8312 | void | |
1579bae1 | 8313 | lang_add_data (int type, union etree_union *exp) |
252b5132 | 8314 | { |
d3ce72d0 | 8315 | lang_data_statement_type *new_stmt; |
252b5132 | 8316 | |
d3ce72d0 NC |
8317 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
8318 | new_stmt->exp = exp; | |
8319 | new_stmt->type = type; | |
252b5132 RH |
8320 | } |
8321 | ||
8322 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
8323 | generate. HOWTO is the corresponding howto structure (we could | |
8324 | look this up, but the caller has already done so). SECTION is the | |
8325 | section to generate a reloc against, or NAME is the name of the | |
8326 | symbol to generate a reloc against. Exactly one of SECTION and | |
8327 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
8328 | ||
8329 | void | |
1579bae1 AM |
8330 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
8331 | reloc_howto_type *howto, | |
8332 | asection *section, | |
8333 | const char *name, | |
8334 | union etree_union *addend) | |
252b5132 RH |
8335 | { |
8336 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 8337 | |
252b5132 RH |
8338 | p->reloc = reloc; |
8339 | p->howto = howto; | |
8340 | p->section = section; | |
8341 | p->name = name; | |
8342 | p->addend_exp = addend; | |
8343 | ||
8344 | p->addend_value = 0; | |
8345 | p->output_section = NULL; | |
7fabd029 | 8346 | p->output_offset = 0; |
252b5132 RH |
8347 | } |
8348 | ||
8349 | lang_assignment_statement_type * | |
1579bae1 | 8350 | lang_add_assignment (etree_type *exp) |
252b5132 | 8351 | { |
d3ce72d0 | 8352 | lang_assignment_statement_type *new_stmt; |
252b5132 | 8353 | |
d3ce72d0 NC |
8354 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
8355 | new_stmt->exp = exp; | |
8356 | return new_stmt; | |
252b5132 RH |
8357 | } |
8358 | ||
8359 | void | |
1579bae1 | 8360 | lang_add_attribute (enum statement_enum attribute) |
252b5132 | 8361 | { |
bd4d42c1 | 8362 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
252b5132 RH |
8363 | } |
8364 | ||
8365 | void | |
1579bae1 | 8366 | lang_startup (const char *name) |
252b5132 | 8367 | { |
1e915804 | 8368 | if (first_file->filename != NULL) |
252b5132 | 8369 | { |
df5f2391 | 8370 | einfo (_("%F%P: multiple STARTUP files\n")); |
252b5132 RH |
8371 | } |
8372 | first_file->filename = name; | |
8373 | first_file->local_sym_name = name; | |
f38a2680 | 8374 | first_file->flags.real = true; |
252b5132 RH |
8375 | } |
8376 | ||
8377 | void | |
f38a2680 | 8378 | lang_float (bool maybe) |
252b5132 RH |
8379 | { |
8380 | lang_float_flag = maybe; | |
8381 | } | |
8382 | ||
ee3cc2e2 RS |
8383 | |
8384 | /* Work out the load- and run-time regions from a script statement, and | |
8385 | store them in *LMA_REGION and *REGION respectively. | |
8386 | ||
a747ee4d NC |
8387 | MEMSPEC is the name of the run-time region, or the value of |
8388 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
8389 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
8390 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
8391 | had an explicit load address. | |
ee3cc2e2 RS |
8392 | |
8393 | It is an error to specify both a load region and a load address. */ | |
8394 | ||
8395 | static void | |
6bdafbeb NC |
8396 | lang_get_regions (lang_memory_region_type **region, |
8397 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
8398 | const char *memspec, |
8399 | const char *lma_memspec, | |
f38a2680 AM |
8400 | bool have_lma, |
8401 | bool have_vma) | |
ee3cc2e2 | 8402 | { |
f38a2680 | 8403 | *lma_region = lang_memory_region_lookup (lma_memspec, false); |
ee3cc2e2 | 8404 | |
6feb9908 AM |
8405 | /* If no runtime region or VMA has been specified, but the load region |
8406 | has been specified, then use the load region for the runtime region | |
8407 | as well. */ | |
6bdafbeb | 8408 | if (lma_memspec != NULL |
0aa7f586 | 8409 | && !have_vma |
6bdafbeb | 8410 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
ee3cc2e2 RS |
8411 | *region = *lma_region; |
8412 | else | |
f38a2680 | 8413 | *region = lang_memory_region_lookup (memspec, false); |
ee3cc2e2 | 8414 | |
6bdafbeb | 8415 | if (have_lma && lma_memspec != 0) |
c1c8c1ef | 8416 | einfo (_("%X%P:%pS: section has both a load address and a load region\n"), |
dab69f68 | 8417 | NULL); |
ee3cc2e2 RS |
8418 | } |
8419 | ||
252b5132 | 8420 | void |
6bdafbeb NC |
8421 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
8422 | lang_output_section_phdr_list *phdrs, | |
8423 | const char *lma_memspec) | |
252b5132 | 8424 | { |
ee3cc2e2 RS |
8425 | lang_get_regions (¤t_section->region, |
8426 | ¤t_section->lma_region, | |
8427 | memspec, lma_memspec, | |
6bdafbeb NC |
8428 | current_section->load_base != NULL, |
8429 | current_section->addr_tree != NULL); | |
113e6845 | 8430 | |
252b5132 | 8431 | current_section->fill = fill; |
252b5132 | 8432 | current_section->phdrs = phdrs; |
bde18da4 | 8433 | pop_stat_ptr (); |
252b5132 RH |
8434 | } |
8435 | ||
252b5132 RH |
8436 | /* Set the output format type. -oformat overrides scripts. */ |
8437 | ||
8438 | void | |
1579bae1 AM |
8439 | lang_add_output_format (const char *format, |
8440 | const char *big, | |
8441 | const char *little, | |
8442 | int from_script) | |
252b5132 RH |
8443 | { |
8444 | if (output_target == NULL || !from_script) | |
8445 | { | |
8446 | if (command_line.endian == ENDIAN_BIG | |
8447 | && big != NULL) | |
8448 | format = big; | |
8449 | else if (command_line.endian == ENDIAN_LITTLE | |
8450 | && little != NULL) | |
8451 | format = little; | |
8452 | ||
8453 | output_target = format; | |
8454 | } | |
8455 | } | |
8456 | ||
53d25da6 AM |
8457 | void |
8458 | lang_add_insert (const char *where, int is_before) | |
8459 | { | |
d3ce72d0 | 8460 | lang_insert_statement_type *new_stmt; |
53d25da6 | 8461 | |
d3ce72d0 NC |
8462 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
8463 | new_stmt->where = where; | |
8464 | new_stmt->is_before = is_before; | |
53d25da6 AM |
8465 | saved_script_handle = previous_script_handle; |
8466 | } | |
8467 | ||
252b5132 RH |
8468 | /* Enter a group. This creates a new lang_group_statement, and sets |
8469 | stat_ptr to build new statements within the group. */ | |
8470 | ||
8471 | void | |
1579bae1 | 8472 | lang_enter_group (void) |
252b5132 RH |
8473 | { |
8474 | lang_group_statement_type *g; | |
8475 | ||
8476 | g = new_stat (lang_group_statement, stat_ptr); | |
8477 | lang_list_init (&g->children); | |
bde18da4 | 8478 | push_stat_ptr (&g->children); |
252b5132 RH |
8479 | } |
8480 | ||
8481 | /* Leave a group. This just resets stat_ptr to start writing to the | |
8482 | regular list of statements again. Note that this will not work if | |
8483 | groups can occur inside anything else which can adjust stat_ptr, | |
8484 | but currently they can't. */ | |
8485 | ||
8486 | void | |
1579bae1 | 8487 | lang_leave_group (void) |
252b5132 | 8488 | { |
bde18da4 | 8489 | pop_stat_ptr (); |
252b5132 RH |
8490 | } |
8491 | ||
8492 | /* Add a new program header. This is called for each entry in a PHDRS | |
8493 | command in a linker script. */ | |
8494 | ||
8495 | void | |
1579bae1 AM |
8496 | lang_new_phdr (const char *name, |
8497 | etree_type *type, | |
f38a2680 AM |
8498 | bool filehdr, |
8499 | bool phdrs, | |
1579bae1 AM |
8500 | etree_type *at, |
8501 | etree_type *flags) | |
252b5132 RH |
8502 | { |
8503 | struct lang_phdr *n, **pp; | |
f38a2680 | 8504 | bool hdrs; |
252b5132 | 8505 | |
988de25b | 8506 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
8507 | n->next = NULL; |
8508 | n->name = name; | |
3d9c8f6b | 8509 | n->type = exp_get_vma (type, 0, "program header type"); |
252b5132 RH |
8510 | n->filehdr = filehdr; |
8511 | n->phdrs = phdrs; | |
8512 | n->at = at; | |
8513 | n->flags = flags; | |
1e0061d2 | 8514 | |
5c1a3f0f | 8515 | hdrs = n->type == 1 && (phdrs || filehdr); |
252b5132 RH |
8516 | |
8517 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4100cea3 AM |
8518 | if (hdrs |
8519 | && (*pp)->type == 1 | |
8520 | && !((*pp)->filehdr || (*pp)->phdrs)) | |
5c1a3f0f | 8521 | { |
c1c8c1ef | 8522 | einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" |
dab69f68 | 8523 | " when prior PT_LOAD headers lack them\n"), NULL); |
f38a2680 | 8524 | hdrs = false; |
5c1a3f0f NS |
8525 | } |
8526 | ||
252b5132 RH |
8527 | *pp = n; |
8528 | } | |
8529 | ||
8530 | /* Record the program header information in the output BFD. FIXME: We | |
8531 | should not be calling an ELF specific function here. */ | |
8532 | ||
8533 | static void | |
1579bae1 | 8534 | lang_record_phdrs (void) |
252b5132 RH |
8535 | { |
8536 | unsigned int alc; | |
8537 | asection **secs; | |
6bdafbeb | 8538 | lang_output_section_phdr_list *last; |
252b5132 | 8539 | struct lang_phdr *l; |
afd7a018 | 8540 | lang_output_section_statement_type *os; |
252b5132 RH |
8541 | |
8542 | alc = 10; | |
1e9cc1c2 | 8543 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
252b5132 | 8544 | last = NULL; |
591a748a | 8545 | |
252b5132 RH |
8546 | for (l = lang_phdr_list; l != NULL; l = l->next) |
8547 | { | |
8548 | unsigned int c; | |
8549 | flagword flags; | |
8550 | bfd_vma at; | |
8551 | ||
8552 | c = 0; | |
8ce18f9c | 8553 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8554 | os != NULL; |
8555 | os = os->next) | |
252b5132 | 8556 | { |
6bdafbeb | 8557 | lang_output_section_phdr_list *pl; |
252b5132 | 8558 | |
66c103b7 | 8559 | if (os->constraint < 0) |
0841712e | 8560 | continue; |
252b5132 RH |
8561 | |
8562 | pl = os->phdrs; | |
8563 | if (pl != NULL) | |
8564 | last = pl; | |
8565 | else | |
8566 | { | |
8567 | if (os->sectype == noload_section | |
8568 | || os->bfd_section == NULL | |
8569 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
8570 | continue; | |
591a748a | 8571 | |
7512c397 AM |
8572 | /* Don't add orphans to PT_INTERP header. */ |
8573 | if (l->type == 3) | |
8574 | continue; | |
8575 | ||
e9442572 | 8576 | if (last == NULL) |
591a748a | 8577 | { |
0aa7f586 | 8578 | lang_output_section_statement_type *tmp_os; |
591a748a NC |
8579 | |
8580 | /* If we have not run across a section with a program | |
8581 | header assigned to it yet, then scan forwards to find | |
8582 | one. This prevents inconsistencies in the linker's | |
8583 | behaviour when a script has specified just a single | |
8584 | header and there are sections in that script which are | |
8585 | not assigned to it, and which occur before the first | |
8586 | use of that header. See here for more details: | |
8587 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ | |
8588 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) | |
8589 | if (tmp_os->phdrs) | |
e9442572 AM |
8590 | { |
8591 | last = tmp_os->phdrs; | |
8592 | break; | |
8593 | } | |
8594 | if (last == NULL) | |
8595 | einfo (_("%F%P: no sections assigned to phdrs\n")); | |
591a748a | 8596 | } |
e9442572 | 8597 | pl = last; |
252b5132 RH |
8598 | } |
8599 | ||
8600 | if (os->bfd_section == NULL) | |
8601 | continue; | |
8602 | ||
8603 | for (; pl != NULL; pl = pl->next) | |
8604 | { | |
8605 | if (strcmp (pl->name, l->name) == 0) | |
8606 | { | |
8607 | if (c >= alc) | |
8608 | { | |
8609 | alc *= 2; | |
1e9cc1c2 | 8610 | secs = (asection **) xrealloc (secs, |
4724d37e | 8611 | alc * sizeof (asection *)); |
252b5132 RH |
8612 | } |
8613 | secs[c] = os->bfd_section; | |
8614 | ++c; | |
f38a2680 | 8615 | pl->used = true; |
252b5132 RH |
8616 | } |
8617 | } | |
8618 | } | |
8619 | ||
8620 | if (l->flags == NULL) | |
8621 | flags = 0; | |
8622 | else | |
e9ee469a | 8623 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
252b5132 RH |
8624 | |
8625 | if (l->at == NULL) | |
8626 | at = 0; | |
8627 | else | |
e9ee469a | 8628 | at = exp_get_vma (l->at, 0, "phdr load address"); |
252b5132 | 8629 | |
0aa7f586 AM |
8630 | if (!bfd_record_phdr (link_info.output_bfd, l->type, |
8631 | l->flags != NULL, flags, l->at != NULL, | |
8632 | at, l->filehdr, l->phdrs, c, secs)) | |
252b5132 RH |
8633 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
8634 | } | |
8635 | ||
8636 | free (secs); | |
8637 | ||
8638 | /* Make sure all the phdr assignments succeeded. */ | |
8ce18f9c | 8639 | for (os = (void *) lang_os_list.head; |
afd7a018 AM |
8640 | os != NULL; |
8641 | os = os->next) | |
252b5132 | 8642 | { |
6bdafbeb | 8643 | lang_output_section_phdr_list *pl; |
252b5132 | 8644 | |
66c103b7 | 8645 | if (os->constraint < 0 |
afd7a018 | 8646 | || os->bfd_section == NULL) |
252b5132 RH |
8647 | continue; |
8648 | ||
afd7a018 | 8649 | for (pl = os->phdrs; |
252b5132 RH |
8650 | pl != NULL; |
8651 | pl = pl->next) | |
0aa7f586 | 8652 | if (!pl->used && strcmp (pl->name, "NONE") != 0) |
252b5132 | 8653 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
afd7a018 | 8654 | os->name, pl->name); |
252b5132 RH |
8655 | } |
8656 | } | |
8657 | ||
8658 | /* Record a list of sections which may not be cross referenced. */ | |
8659 | ||
8660 | void | |
6bdafbeb | 8661 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
8662 | { |
8663 | struct lang_nocrossrefs *n; | |
8664 | ||
1e9cc1c2 | 8665 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
252b5132 RH |
8666 | n->next = nocrossref_list; |
8667 | n->list = l; | |
f38a2680 | 8668 | n->onlyfirst = false; |
252b5132 RH |
8669 | nocrossref_list = n; |
8670 | ||
8671 | /* Set notice_all so that we get informed about all symbols. */ | |
f38a2680 | 8672 | link_info.notice_all = true; |
252b5132 | 8673 | } |
cdf96953 MF |
8674 | |
8675 | /* Record a section that cannot be referenced from a list of sections. */ | |
8676 | ||
8677 | void | |
8678 | lang_add_nocrossref_to (lang_nocrossref_type *l) | |
8679 | { | |
8680 | lang_add_nocrossref (l); | |
f38a2680 | 8681 | nocrossref_list->onlyfirst = true; |
cdf96953 | 8682 | } |
252b5132 RH |
8683 | \f |
8684 | /* Overlay handling. We handle overlays with some static variables. */ | |
8685 | ||
8686 | /* The overlay virtual address. */ | |
8687 | static etree_type *overlay_vma; | |
7e7d5768 AM |
8688 | /* And subsection alignment. */ |
8689 | static etree_type *overlay_subalign; | |
252b5132 | 8690 | |
252b5132 RH |
8691 | /* An expression for the maximum section size seen so far. */ |
8692 | static etree_type *overlay_max; | |
8693 | ||
8694 | /* A list of all the sections in this overlay. */ | |
8695 | ||
89cdebba | 8696 | struct overlay_list { |
252b5132 RH |
8697 | struct overlay_list *next; |
8698 | lang_output_section_statement_type *os; | |
8699 | }; | |
8700 | ||
8701 | static struct overlay_list *overlay_list; | |
8702 | ||
8703 | /* Start handling an overlay. */ | |
8704 | ||
8705 | void | |
7e7d5768 | 8706 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
8707 | { |
8708 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
8709 | ASSERT (overlay_vma == NULL |
8710 | && overlay_subalign == NULL | |
8711 | && overlay_max == NULL); | |
252b5132 RH |
8712 | |
8713 | overlay_vma = vma_expr; | |
7e7d5768 | 8714 | overlay_subalign = subalign; |
252b5132 RH |
8715 | } |
8716 | ||
8717 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
8718 | lang_enter_output_section_statement with the correct VMA. |
8719 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
8720 | |
8721 | void | |
1579bae1 | 8722 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
8723 | { |
8724 | struct overlay_list *n; | |
8725 | etree_type *size; | |
8726 | ||
152d792f | 8727 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
c212f39d | 8728 | 0, 0, overlay_subalign, 0, 0, 0); |
252b5132 | 8729 | |
9f88b410 | 8730 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
8731 | sections on this one. This will work correctly even if `.' is |
8732 | used in the addresses. */ | |
8733 | if (overlay_list == NULL) | |
9f88b410 | 8734 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
8735 | |
8736 | /* Remember the section. */ | |
1e9cc1c2 | 8737 | n = (struct overlay_list *) xmalloc (sizeof *n); |
252b5132 RH |
8738 | n->os = current_section; |
8739 | n->next = overlay_list; | |
8740 | overlay_list = n; | |
8741 | ||
8742 | size = exp_nameop (SIZEOF, name); | |
8743 | ||
252b5132 RH |
8744 | /* Arrange to work out the maximum section end address. */ |
8745 | if (overlay_max == NULL) | |
8746 | overlay_max = size; | |
8747 | else | |
8748 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
8749 | } | |
8750 | ||
8751 | /* Finish a section in an overlay. There isn't any special to do | |
8752 | here. */ | |
8753 | ||
8754 | void | |
1579bae1 | 8755 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 8756 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
8757 | { |
8758 | const char *name; | |
8759 | char *clean, *s2; | |
8760 | const char *s1; | |
8761 | char *buf; | |
8762 | ||
8763 | name = current_section->name; | |
8764 | ||
a747ee4d NC |
8765 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
8766 | region and that no load-time region has been specified. It doesn't | |
8767 | really matter what we say here, since lang_leave_overlay will | |
8768 | override it. */ | |
8769 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
8770 | |
8771 | /* Define the magic symbols. */ | |
8772 | ||
1e9cc1c2 | 8773 | clean = (char *) xmalloc (strlen (name) + 1); |
252b5132 RH |
8774 | s2 = clean; |
8775 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 8776 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
8777 | *s2++ = *s1; |
8778 | *s2 = '\0'; | |
8779 | ||
1e9cc1c2 | 8780 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
252b5132 | 8781 | sprintf (buf, "__load_start_%s", clean); |
34711ca3 AM |
8782 | lang_add_assignment (exp_provide (buf, |
8783 | exp_nameop (LOADADDR, name), | |
f38a2680 | 8784 | false)); |
252b5132 | 8785 | |
1e9cc1c2 | 8786 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
252b5132 | 8787 | sprintf (buf, "__load_stop_%s", clean); |
34711ca3 AM |
8788 | lang_add_assignment (exp_provide (buf, |
8789 | exp_binop ('+', | |
8790 | exp_nameop (LOADADDR, name), | |
8791 | exp_nameop (SIZEOF, name)), | |
f38a2680 | 8792 | false)); |
252b5132 RH |
8793 | |
8794 | free (clean); | |
8795 | } | |
8796 | ||
8797 | /* Finish an overlay. If there are any overlay wide settings, this | |
8798 | looks through all the sections in the overlay and sets them. */ | |
8799 | ||
8800 | void | |
1579bae1 AM |
8801 | lang_leave_overlay (etree_type *lma_expr, |
8802 | int nocrossrefs, | |
8803 | fill_type *fill, | |
8804 | const char *memspec, | |
6bdafbeb | 8805 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 8806 | const char *lma_memspec) |
252b5132 RH |
8807 | { |
8808 | lang_memory_region_type *region; | |
562d3460 | 8809 | lang_memory_region_type *lma_region; |
252b5132 | 8810 | struct overlay_list *l; |
6bdafbeb | 8811 | lang_nocrossref_type *nocrossref; |
252b5132 | 8812 | |
ee3cc2e2 RS |
8813 | lang_get_regions (®ion, &lma_region, |
8814 | memspec, lma_memspec, | |
f38a2680 | 8815 | lma_expr != NULL, false); |
562d3460 | 8816 | |
252b5132 RH |
8817 | nocrossref = NULL; |
8818 | ||
9f88b410 RS |
8819 | /* After setting the size of the last section, set '.' to end of the |
8820 | overlay region. */ | |
8821 | if (overlay_list != NULL) | |
6d8bf25d AM |
8822 | { |
8823 | overlay_list->os->update_dot = 1; | |
8824 | overlay_list->os->update_dot_tree | |
f38a2680 | 8825 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); |
6d8bf25d | 8826 | } |
9f88b410 | 8827 | |
252b5132 RH |
8828 | l = overlay_list; |
8829 | while (l != NULL) | |
8830 | { | |
8831 | struct overlay_list *next; | |
8832 | ||
1579bae1 | 8833 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 8834 | l->os->fill = fill; |
1545243b | 8835 | |
9f88b410 RS |
8836 | l->os->region = region; |
8837 | l->os->lma_region = lma_region; | |
8838 | ||
8839 | /* The first section has the load address specified in the | |
8840 | OVERLAY statement. The rest are worked out from that. | |
8841 | The base address is not needed (and should be null) if | |
8842 | an LMA region was specified. */ | |
8843 | if (l->next == 0) | |
152d792f AM |
8844 | { |
8845 | l->os->load_base = lma_expr; | |
7b243801 | 8846 | l->os->sectype = first_overlay_section; |
152d792f | 8847 | } |
252b5132 RH |
8848 | if (phdrs != NULL && l->os->phdrs == NULL) |
8849 | l->os->phdrs = phdrs; | |
8850 | ||
9f88b410 | 8851 | if (nocrossrefs) |
252b5132 | 8852 | { |
6bdafbeb | 8853 | lang_nocrossref_type *nc; |
252b5132 | 8854 | |
1e9cc1c2 | 8855 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
252b5132 RH |
8856 | nc->name = l->os->name; |
8857 | nc->next = nocrossref; | |
8858 | nocrossref = nc; | |
8859 | } | |
8860 | ||
8861 | next = l->next; | |
8862 | free (l); | |
8863 | l = next; | |
8864 | } | |
8865 | ||
8866 | if (nocrossref != NULL) | |
8867 | lang_add_nocrossref (nocrossref); | |
8868 | ||
252b5132 | 8869 | overlay_vma = NULL; |
252b5132 RH |
8870 | overlay_list = NULL; |
8871 | overlay_max = NULL; | |
8cb1232a | 8872 | overlay_subalign = NULL; |
252b5132 RH |
8873 | } |
8874 | \f | |
8875 | /* Version handling. This is only useful for ELF. */ | |
8876 | ||
108ba305 JJ |
8877 | /* If PREV is NULL, return first version pattern matching particular symbol. |
8878 | If PREV is non-NULL, return first version pattern matching particular | |
8879 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 8880 | |
108ba305 JJ |
8881 | static struct bfd_elf_version_expr * |
8882 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
8883 | struct bfd_elf_version_expr *prev, | |
8884 | const char *sym) | |
252b5132 | 8885 | { |
93252b1c | 8886 | const char *c_sym; |
108ba305 JJ |
8887 | const char *cxx_sym = sym; |
8888 | const char *java_sym = sym; | |
8889 | struct bfd_elf_version_expr *expr = NULL; | |
93252b1c MF |
8890 | enum demangling_styles curr_style; |
8891 | ||
8892 | curr_style = CURRENT_DEMANGLING_STYLE; | |
8893 | cplus_demangle_set_style (no_demangling); | |
8894 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); | |
8895 | if (!c_sym) | |
8896 | c_sym = sym; | |
8897 | cplus_demangle_set_style (curr_style); | |
252b5132 | 8898 | |
108ba305 | 8899 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 8900 | { |
93252b1c MF |
8901 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
8902 | DMGL_PARAMS | DMGL_ANSI); | |
108ba305 JJ |
8903 | if (!cxx_sym) |
8904 | cxx_sym = sym; | |
252b5132 | 8905 | } |
df816215 | 8906 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 8907 | { |
93252b1c | 8908 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
108ba305 JJ |
8909 | if (!java_sym) |
8910 | java_sym = sym; | |
252b5132 RH |
8911 | } |
8912 | ||
ae5a3597 | 8913 | if (head->htab && (prev == NULL || prev->literal)) |
252b5132 | 8914 | { |
108ba305 JJ |
8915 | struct bfd_elf_version_expr e; |
8916 | ||
8917 | switch (prev ? prev->mask : 0) | |
8918 | { | |
967928e9 AM |
8919 | case 0: |
8920 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
8921 | { | |
93252b1c | 8922 | e.pattern = c_sym; |
1e9cc1c2 | 8923 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8924 | htab_find ((htab_t) head->htab, &e); |
93252b1c | 8925 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
967928e9 AM |
8926 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
8927 | goto out_ret; | |
8928 | else | |
8929 | expr = expr->next; | |
8930 | } | |
8931 | /* Fallthrough */ | |
8932 | case BFD_ELF_VERSION_C_TYPE: | |
8933 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
8934 | { | |
ae5a3597 | 8935 | e.pattern = cxx_sym; |
1e9cc1c2 | 8936 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8937 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8938 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
967928e9 AM |
8939 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
8940 | goto out_ret; | |
8941 | else | |
8942 | expr = expr->next; | |
8943 | } | |
8944 | /* Fallthrough */ | |
8945 | case BFD_ELF_VERSION_CXX_TYPE: | |
8946 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
8947 | { | |
ae5a3597 | 8948 | e.pattern = java_sym; |
1e9cc1c2 | 8949 | expr = (struct bfd_elf_version_expr *) |
4724d37e | 8950 | htab_find ((htab_t) head->htab, &e); |
ae5a3597 | 8951 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
967928e9 AM |
8952 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
8953 | goto out_ret; | |
8954 | else | |
8955 | expr = expr->next; | |
8956 | } | |
8957 | /* Fallthrough */ | |
8958 | default: | |
8959 | break; | |
108ba305 | 8960 | } |
252b5132 | 8961 | } |
108ba305 JJ |
8962 | |
8963 | /* Finally, try the wildcards. */ | |
ae5a3597 | 8964 | if (prev == NULL || prev->literal) |
108ba305 | 8965 | expr = head->remaining; |
252b5132 | 8966 | else |
108ba305 | 8967 | expr = prev->next; |
86043bbb | 8968 | for (; expr; expr = expr->next) |
252b5132 | 8969 | { |
108ba305 JJ |
8970 | const char *s; |
8971 | ||
86043bbb MM |
8972 | if (!expr->pattern) |
8973 | continue; | |
8974 | ||
108ba305 JJ |
8975 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
8976 | break; | |
8977 | ||
8978 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
8979 | s = java_sym; | |
8980 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
8981 | s = cxx_sym; | |
8982 | else | |
93252b1c | 8983 | s = c_sym; |
5e35cbc2 | 8984 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 | 8985 | break; |
252b5132 RH |
8986 | } |
8987 | ||
967928e9 | 8988 | out_ret: |
93252b1c MF |
8989 | if (c_sym != sym) |
8990 | free ((char *) c_sym); | |
108ba305 JJ |
8991 | if (cxx_sym != sym) |
8992 | free ((char *) cxx_sym); | |
8993 | if (java_sym != sym) | |
8994 | free ((char *) java_sym); | |
8995 | return expr; | |
252b5132 RH |
8996 | } |
8997 | ||
5e35cbc2 | 8998 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
ae5a3597 | 8999 | return a pointer to the symbol name with any backslash quotes removed. */ |
5e35cbc2 L |
9000 | |
9001 | static const char * | |
9002 | realsymbol (const char *pattern) | |
9003 | { | |
9004 | const char *p; | |
f38a2680 | 9005 | bool changed = false, backslash = false; |
1e9cc1c2 | 9006 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
5e35cbc2 L |
9007 | |
9008 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
9009 | { | |
9010 | /* It is a glob pattern only if there is no preceding | |
9011 | backslash. */ | |
5e35cbc2 L |
9012 | if (backslash) |
9013 | { | |
9014 | /* Remove the preceding backslash. */ | |
9015 | *(s - 1) = *p; | |
f38a2680 AM |
9016 | backslash = false; |
9017 | changed = true; | |
5e35cbc2 L |
9018 | } |
9019 | else | |
ae5a3597 AM |
9020 | { |
9021 | if (*p == '?' || *p == '*' || *p == '[') | |
9022 | { | |
9023 | free (symbol); | |
9024 | return NULL; | |
9025 | } | |
5e35cbc2 | 9026 | |
ae5a3597 AM |
9027 | *s++ = *p; |
9028 | backslash = *p == '\\'; | |
9029 | } | |
5e35cbc2 L |
9030 | } |
9031 | ||
9032 | if (changed) | |
9033 | { | |
9034 | *s = '\0'; | |
9035 | return symbol; | |
9036 | } | |
9037 | else | |
9038 | { | |
9039 | free (symbol); | |
9040 | return pattern; | |
9041 | } | |
9042 | } | |
9043 | ||
d3ce72d0 | 9044 | /* This is called for each variable name or match expression. NEW_NAME is |
86043bbb MM |
9045 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
9046 | pattern to be matched against symbol names. */ | |
252b5132 RH |
9047 | |
9048 | struct bfd_elf_version_expr * | |
1579bae1 | 9049 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
d3ce72d0 | 9050 | const char *new_name, |
86043bbb | 9051 | const char *lang, |
f38a2680 | 9052 | bool literal_p) |
252b5132 RH |
9053 | { |
9054 | struct bfd_elf_version_expr *ret; | |
9055 | ||
d3ce72d0 | 9056 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
252b5132 | 9057 | ret->next = orig; |
31941635 L |
9058 | ret->symver = 0; |
9059 | ret->script = 0; | |
f38a2680 | 9060 | ret->literal = true; |
d3ce72d0 | 9061 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
ae5a3597 AM |
9062 | if (ret->pattern == NULL) |
9063 | { | |
d3ce72d0 | 9064 | ret->pattern = new_name; |
f38a2680 | 9065 | ret->literal = false; |
ae5a3597 | 9066 | } |
252b5132 RH |
9067 | |
9068 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 9069 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 9070 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 9071 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 9072 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 9073 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
9074 | else |
9075 | { | |
9076 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
9077 | lang); | |
108ba305 | 9078 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
9079 | } |
9080 | ||
fac1652d | 9081 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
9082 | } |
9083 | ||
9084 | /* This is called for each set of variable names and match | |
9085 | expressions. */ | |
9086 | ||
9087 | struct bfd_elf_version_tree * | |
1579bae1 AM |
9088 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
9089 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
9090 | { |
9091 | struct bfd_elf_version_tree *ret; | |
9092 | ||
1e9cc1c2 | 9093 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
108ba305 JJ |
9094 | ret->globals.list = globals; |
9095 | ret->locals.list = locals; | |
9096 | ret->match = lang_vers_match; | |
252b5132 | 9097 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
9098 | return ret; |
9099 | } | |
9100 | ||
9101 | /* This static variable keeps track of version indices. */ | |
9102 | ||
9103 | static int version_index; | |
9104 | ||
108ba305 JJ |
9105 | static hashval_t |
9106 | version_expr_head_hash (const void *p) | |
9107 | { | |
1e9cc1c2 NC |
9108 | const struct bfd_elf_version_expr *e = |
9109 | (const struct bfd_elf_version_expr *) p; | |
108ba305 | 9110 | |
ae5a3597 | 9111 | return htab_hash_string (e->pattern); |
108ba305 JJ |
9112 | } |
9113 | ||
9114 | static int | |
9115 | version_expr_head_eq (const void *p1, const void *p2) | |
9116 | { | |
1e9cc1c2 NC |
9117 | const struct bfd_elf_version_expr *e1 = |
9118 | (const struct bfd_elf_version_expr *) p1; | |
9119 | const struct bfd_elf_version_expr *e2 = | |
9120 | (const struct bfd_elf_version_expr *) p2; | |
108ba305 | 9121 | |
ae5a3597 | 9122 | return strcmp (e1->pattern, e2->pattern) == 0; |
108ba305 JJ |
9123 | } |
9124 | ||
9125 | static void | |
9126 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
9127 | { | |
9128 | size_t count = 0; | |
9129 | struct bfd_elf_version_expr *e, *next; | |
9130 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
9131 | ||
9132 | for (e = head->list; e; e = e->next) | |
9133 | { | |
ae5a3597 | 9134 | if (e->literal) |
108ba305 JJ |
9135 | count++; |
9136 | head->mask |= e->mask; | |
9137 | } | |
9138 | ||
9139 | if (count) | |
9140 | { | |
9141 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
9142 | version_expr_head_eq, NULL); | |
9143 | list_loc = &head->list; | |
9144 | remaining_loc = &head->remaining; | |
9145 | for (e = head->list; e; e = next) | |
9146 | { | |
9147 | next = e->next; | |
ae5a3597 | 9148 | if (!e->literal) |
108ba305 JJ |
9149 | { |
9150 | *remaining_loc = e; | |
9151 | remaining_loc = &e->next; | |
9152 | } | |
9153 | else | |
9154 | { | |
1e9cc1c2 | 9155 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
108ba305 JJ |
9156 | |
9157 | if (*loc) | |
9158 | { | |
9159 | struct bfd_elf_version_expr *e1, *last; | |
9160 | ||
1e9cc1c2 | 9161 | e1 = (struct bfd_elf_version_expr *) *loc; |
108ba305 JJ |
9162 | last = NULL; |
9163 | do | |
9164 | { | |
9165 | if (e1->mask == e->mask) | |
9166 | { | |
9167 | last = NULL; | |
9168 | break; | |
9169 | } | |
9170 | last = e1; | |
9171 | e1 = e1->next; | |
9172 | } | |
ae5a3597 | 9173 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
108ba305 JJ |
9174 | |
9175 | if (last == NULL) | |
9176 | { | |
9177 | /* This is a duplicate. */ | |
9178 | /* FIXME: Memory leak. Sometimes pattern is not | |
9179 | xmalloced alone, but in larger chunk of memory. */ | |
ae5a3597 | 9180 | /* free (e->pattern); */ |
108ba305 JJ |
9181 | free (e); |
9182 | } | |
9183 | else | |
9184 | { | |
9185 | e->next = last->next; | |
9186 | last->next = e; | |
9187 | } | |
9188 | } | |
9189 | else | |
9190 | { | |
9191 | *loc = e; | |
9192 | *list_loc = e; | |
9193 | list_loc = &e->next; | |
9194 | } | |
9195 | } | |
9196 | } | |
9197 | *remaining_loc = NULL; | |
9198 | *list_loc = head->remaining; | |
9199 | } | |
9200 | else | |
9201 | head->remaining = head->list; | |
9202 | } | |
9203 | ||
252b5132 RH |
9204 | /* This is called when we know the name and dependencies of the |
9205 | version. */ | |
9206 | ||
9207 | void | |
1579bae1 AM |
9208 | lang_register_vers_node (const char *name, |
9209 | struct bfd_elf_version_tree *version, | |
9210 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
9211 | { |
9212 | struct bfd_elf_version_tree *t, **pp; | |
9213 | struct bfd_elf_version_expr *e1; | |
9214 | ||
6b9b879a JJ |
9215 | if (name == NULL) |
9216 | name = ""; | |
9217 | ||
fd91d419 L |
9218 | if (link_info.version_info != NULL |
9219 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) | |
6b9b879a | 9220 | { |
6feb9908 AM |
9221 | einfo (_("%X%P: anonymous version tag cannot be combined" |
9222 | " with other version tags\n")); | |
5ed6aba4 | 9223 | free (version); |
6b9b879a JJ |
9224 | return; |
9225 | } | |
9226 | ||
252b5132 | 9227 | /* Make sure this node has a unique name. */ |
fd91d419 | 9228 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9229 | if (strcmp (t->name, name) == 0) |
9230 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
9231 | ||
108ba305 JJ |
9232 | lang_finalize_version_expr_head (&version->globals); |
9233 | lang_finalize_version_expr_head (&version->locals); | |
9234 | ||
252b5132 RH |
9235 | /* Check the global and local match names, and make sure there |
9236 | aren't any duplicates. */ | |
9237 | ||
108ba305 | 9238 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9239 | { |
fd91d419 | 9240 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9241 | { |
9242 | struct bfd_elf_version_expr *e2; | |
9243 | ||
ae5a3597 | 9244 | if (t->locals.htab && e1->literal) |
108ba305 | 9245 | { |
1e9cc1c2 | 9246 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9247 | htab_find ((htab_t) t->locals.htab, e1); |
ae5a3597 | 9248 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9249 | { |
9250 | if (e1->mask == e2->mask) | |
6feb9908 | 9251 | einfo (_("%X%P: duplicate expression `%s'" |
ae5a3597 | 9252 | " in version information\n"), e1->pattern); |
108ba305 JJ |
9253 | e2 = e2->next; |
9254 | } | |
9255 | } | |
ae5a3597 | 9256 | else if (!e1->literal) |
108ba305 | 9257 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9258 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9259 | && e1->mask == e2->mask) | |
9260 | einfo (_("%X%P: duplicate expression `%s'" | |
9261 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9262 | } |
9263 | } | |
9264 | ||
108ba305 | 9265 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 | 9266 | { |
fd91d419 | 9267 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9268 | { |
9269 | struct bfd_elf_version_expr *e2; | |
9270 | ||
ae5a3597 | 9271 | if (t->globals.htab && e1->literal) |
108ba305 | 9272 | { |
1e9cc1c2 | 9273 | e2 = (struct bfd_elf_version_expr *) |
4724d37e | 9274 | htab_find ((htab_t) t->globals.htab, e1); |
ae5a3597 | 9275 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
108ba305 JJ |
9276 | { |
9277 | if (e1->mask == e2->mask) | |
6feb9908 AM |
9278 | einfo (_("%X%P: duplicate expression `%s'" |
9279 | " in version information\n"), | |
ae5a3597 | 9280 | e1->pattern); |
108ba305 JJ |
9281 | e2 = e2->next; |
9282 | } | |
9283 | } | |
ae5a3597 | 9284 | else if (!e1->literal) |
108ba305 | 9285 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
9286 | if (strcmp (e1->pattern, e2->pattern) == 0 |
9287 | && e1->mask == e2->mask) | |
9288 | einfo (_("%X%P: duplicate expression `%s'" | |
9289 | " in version information\n"), e1->pattern); | |
252b5132 RH |
9290 | } |
9291 | } | |
9292 | ||
9293 | version->deps = deps; | |
9294 | version->name = name; | |
6b9b879a JJ |
9295 | if (name[0] != '\0') |
9296 | { | |
9297 | ++version_index; | |
9298 | version->vernum = version_index; | |
9299 | } | |
9300 | else | |
9301 | version->vernum = 0; | |
252b5132 | 9302 | |
fd91d419 | 9303 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
252b5132 RH |
9304 | ; |
9305 | *pp = version; | |
9306 | } | |
9307 | ||
9308 | /* This is called when we see a version dependency. */ | |
9309 | ||
9310 | struct bfd_elf_version_deps * | |
1579bae1 | 9311 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
9312 | { |
9313 | struct bfd_elf_version_deps *ret; | |
9314 | struct bfd_elf_version_tree *t; | |
9315 | ||
1e9cc1c2 | 9316 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
252b5132 RH |
9317 | ret->next = list; |
9318 | ||
fd91d419 | 9319 | for (t = link_info.version_info; t != NULL; t = t->next) |
252b5132 RH |
9320 | { |
9321 | if (strcmp (t->name, name) == 0) | |
9322 | { | |
9323 | ret->version_needed = t; | |
9324 | return ret; | |
9325 | } | |
9326 | } | |
9327 | ||
9328 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
9329 | ||
1e0061d2 | 9330 | ret->version_needed = NULL; |
252b5132 RH |
9331 | return ret; |
9332 | } | |
9333 | ||
9334 | static void | |
1579bae1 | 9335 | lang_do_version_exports_section (void) |
252b5132 RH |
9336 | { |
9337 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
9338 | ||
9339 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
9340 | { | |
9341 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
9342 | char *contents, *p; | |
9343 | bfd_size_type len; | |
9344 | ||
9345 | if (sec == NULL) | |
b7a26f91 | 9346 | continue; |
252b5132 | 9347 | |
eea6121a | 9348 | len = sec->size; |
1e9cc1c2 | 9349 | contents = (char *) xmalloc (len); |
252b5132 | 9350 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
6f9efd97 | 9351 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
9352 | |
9353 | p = contents; | |
89cdebba | 9354 | while (p < contents + len) |
252b5132 | 9355 | { |
f38a2680 | 9356 | greg = lang_new_vers_pattern (greg, p, NULL, false); |
252b5132 RH |
9357 | p = strchr (p, '\0') + 1; |
9358 | } | |
9359 | ||
9360 | /* Do not free the contents, as we used them creating the regex. */ | |
9361 | ||
9362 | /* Do not include this section in the link. */ | |
a14a5de3 | 9363 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
9364 | } |
9365 | ||
f38a2680 | 9366 | lreg = lang_new_vers_pattern (NULL, "*", NULL, false); |
252b5132 RH |
9367 | lang_register_vers_node (command_line.version_exports_section, |
9368 | lang_new_vers_node (greg, lreg), NULL); | |
9369 | } | |
577a0623 | 9370 | |
874ef038 AB |
9371 | /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially |
9372 | called with UPDATE_REGIONS_P set to FALSE, in this case no errors are | |
9373 | thrown, however, references to symbols in the origin and length fields | |
9374 | will be pushed into the symbol table, this allows PROVIDE statements to | |
9375 | then provide these symbols. This function is called a second time with | |
9376 | UPDATE_REGIONS_P set to TRUE, this time the we update the actual region | |
9377 | data structures, and throw errors if missing symbols are encountered. */ | |
cc9ad334 SKS |
9378 | |
9379 | static void | |
f38a2680 | 9380 | lang_do_memory_regions (bool update_regions_p) |
cc9ad334 SKS |
9381 | { |
9382 | lang_memory_region_type *r = lang_memory_region_list; | |
9383 | ||
9384 | for (; r != NULL; r = r->next) | |
9385 | { | |
9386 | if (r->origin_exp) | |
0aa7f586 AM |
9387 | { |
9388 | exp_fold_tree_no_dot (r->origin_exp); | |
874ef038 AB |
9389 | if (update_regions_p) |
9390 | { | |
9391 | if (expld.result.valid_p) | |
9392 | { | |
9393 | r->origin = expld.result.value; | |
9394 | r->current = r->origin; | |
9395 | } | |
9396 | else | |
9397 | einfo (_("%P: invalid origin for memory region %s\n"), | |
9398 | r->name_list.name); | |
9399 | } | |
0aa7f586 | 9400 | } |
cc9ad334 | 9401 | if (r->length_exp) |
0aa7f586 AM |
9402 | { |
9403 | exp_fold_tree_no_dot (r->length_exp); | |
874ef038 AB |
9404 | if (update_regions_p) |
9405 | { | |
9406 | if (expld.result.valid_p) | |
9407 | r->length = expld.result.value; | |
9408 | else | |
9409 | einfo (_("%P: invalid length for memory region %s\n"), | |
9410 | r->name_list.name); | |
9411 | } | |
9412 | } | |
cc9ad334 SKS |
9413 | } |
9414 | } | |
9415 | ||
577a0623 | 9416 | void |
1579bae1 | 9417 | lang_add_unique (const char *name) |
577a0623 AM |
9418 | { |
9419 | struct unique_sections *ent; | |
9420 | ||
9421 | for (ent = unique_section_list; ent; ent = ent->next) | |
9422 | if (strcmp (ent->name, name) == 0) | |
9423 | return; | |
9424 | ||
1e9cc1c2 | 9425 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
577a0623 AM |
9426 | ent->name = xstrdup (name); |
9427 | ent->next = unique_section_list; | |
9428 | unique_section_list = ent; | |
9429 | } | |
55255dae L |
9430 | |
9431 | /* Append the list of dynamic symbols to the existing one. */ | |
9432 | ||
9433 | void | |
37a141bf FS |
9434 | lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, |
9435 | struct bfd_elf_version_expr *dynamic) | |
55255dae | 9436 | { |
37a141bf | 9437 | if (*list_p) |
55255dae | 9438 | { |
07806542 JJ |
9439 | struct bfd_elf_version_expr *tail; |
9440 | for (tail = dynamic; tail->next != NULL; tail = tail->next) | |
9441 | ; | |
37a141bf FS |
9442 | tail->next = (*list_p)->head.list; |
9443 | (*list_p)->head.list = dynamic; | |
55255dae L |
9444 | } |
9445 | else | |
9446 | { | |
9447 | struct bfd_elf_dynamic_list *d; | |
9448 | ||
1e9cc1c2 | 9449 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
55255dae L |
9450 | d->head.list = dynamic; |
9451 | d->match = lang_vers_match; | |
37a141bf | 9452 | *list_p = d; |
55255dae L |
9453 | } |
9454 | } | |
9455 | ||
9456 | /* Append the list of C++ typeinfo dynamic symbols to the existing | |
9457 | one. */ | |
9458 | ||
9459 | void | |
9460 | lang_append_dynamic_list_cpp_typeinfo (void) | |
9461 | { | |
0aa7f586 | 9462 | const char *symbols[] = |
55255dae L |
9463 | { |
9464 | "typeinfo name for*", | |
9465 | "typeinfo for*" | |
9466 | }; | |
9467 | struct bfd_elf_version_expr *dynamic = NULL; | |
9468 | unsigned int i; | |
9469 | ||
9470 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9471 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9472 | false); |
55255dae | 9473 | |
37a141bf | 9474 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
55255dae | 9475 | } |
40b36307 L |
9476 | |
9477 | /* Append the list of C++ operator new and delete dynamic symbols to the | |
9478 | existing one. */ | |
9479 | ||
9480 | void | |
9481 | lang_append_dynamic_list_cpp_new (void) | |
9482 | { | |
0aa7f586 | 9483 | const char *symbols[] = |
40b36307 L |
9484 | { |
9485 | "operator new*", | |
9486 | "operator delete*" | |
9487 | }; | |
9488 | struct bfd_elf_version_expr *dynamic = NULL; | |
9489 | unsigned int i; | |
9490 | ||
9491 | for (i = 0; i < ARRAY_SIZE (symbols); i++) | |
9492 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", | |
f38a2680 | 9493 | false); |
40b36307 | 9494 | |
37a141bf | 9495 | lang_append_dynamic_list (&link_info.dynamic_list, dynamic); |
40b36307 | 9496 | } |
01554a74 AM |
9497 | |
9498 | /* Scan a space and/or comma separated string of features. */ | |
9499 | ||
9500 | void | |
9501 | lang_ld_feature (char *str) | |
9502 | { | |
9503 | char *p, *q; | |
9504 | ||
9505 | p = str; | |
9506 | while (*p) | |
9507 | { | |
9508 | char sep; | |
9509 | while (*p == ',' || ISSPACE (*p)) | |
9510 | ++p; | |
9511 | if (!*p) | |
9512 | break; | |
9513 | q = p + 1; | |
9514 | while (*q && *q != ',' && !ISSPACE (*q)) | |
9515 | ++q; | |
9516 | sep = *q; | |
9517 | *q = 0; | |
9518 | if (strcasecmp (p, "SANE_EXPR") == 0) | |
f38a2680 | 9519 | config.sane_expr = true; |
01554a74 AM |
9520 | else |
9521 | einfo (_("%X%P: unknown feature `%s'\n"), p); | |
9522 | *q = sep; | |
9523 | p = q; | |
9524 | } | |
9525 | } | |
3604cb1f TG |
9526 | |
9527 | /* Pretty print memory amount. */ | |
9528 | ||
9529 | static void | |
b8281767 | 9530 | lang_print_memory_size (uint64_t sz) |
3604cb1f TG |
9531 | { |
9532 | if ((sz & 0x3fffffff) == 0) | |
b8281767 | 9533 | printf ("%10" PRIu64 " GB", sz >> 30); |
3604cb1f | 9534 | else if ((sz & 0xfffff) == 0) |
b8281767 | 9535 | printf ("%10" PRIu64 " MB", sz >> 20); |
3604cb1f | 9536 | else if ((sz & 0x3ff) == 0) |
b8281767 | 9537 | printf ("%10" PRIu64 " KB", sz >> 10); |
3604cb1f | 9538 | else |
b8281767 | 9539 | printf (" %10" PRIu64 " B", sz); |
3604cb1f TG |
9540 | } |
9541 | ||
9542 | /* Implement --print-memory-usage: disply per region memory usage. */ | |
9543 | ||
9544 | void | |
9545 | lang_print_memory_usage (void) | |
9546 | { | |
9547 | lang_memory_region_type *r; | |
9548 | ||
9549 | printf ("Memory region Used Size Region Size %%age Used\n"); | |
9550 | for (r = lang_memory_region_list; r->next != NULL; r = r->next) | |
9551 | { | |
9552 | bfd_vma used_length = r->current - r->origin; | |
3604cb1f TG |
9553 | |
9554 | printf ("%16s: ",r->name_list.name); | |
9555 | lang_print_memory_size (used_length); | |
b8281767 | 9556 | lang_print_memory_size (r->length); |
3604cb1f | 9557 | |
1769380a AM |
9558 | if (r->length != 0) |
9559 | { | |
9560 | double percent = used_length * 100.0 / r->length; | |
2410edcd | 9561 | printf (" %6.2f%%", percent); |
1769380a | 9562 | } |
2410edcd | 9563 | printf ("\n"); |
3604cb1f TG |
9564 | } |
9565 | } |