Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
a2b64bed | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
e5caa5e0 | 3 | 2001, 2002, 2003, 2004 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
53b2a62f | 6 | This file is part of GLD, the Gnu Linker. |
252b5132 | 7 | |
53b2a62f NC |
8 | GLD is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
252b5132 | 12 | |
53b2a62f NC |
13 | GLD is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
53b2a62f NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with GLD; see the file COPYING. If not, write to the Free | |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-1307, USA. */ | |
252b5132 RH |
22 | |
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "libiberty.h" | |
3882b010 | 26 | #include "safe-ctype.h" |
252b5132 RH |
27 | #include "obstack.h" |
28 | #include "bfdlink.h" | |
29 | ||
30 | #include "ld.h" | |
31 | #include "ldmain.h" | |
252b5132 RH |
32 | #include "ldexp.h" |
33 | #include "ldlang.h" | |
df2a7313 | 34 | #include <ldgram.h> |
252b5132 RH |
35 | #include "ldlex.h" |
36 | #include "ldmisc.h" | |
37 | #include "ldctor.h" | |
38 | #include "ldfile.h" | |
b71e2778 | 39 | #include "ldemul.h" |
252b5132 RH |
40 | #include "fnmatch.h" |
41 | #include "demangle.h" | |
108ba305 | 42 | #include "hashtab.h" |
252b5132 | 43 | |
7abb6dea | 44 | #ifndef offsetof |
cf888e70 | 45 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
46 | #endif |
47 | ||
cf888e70 | 48 | /* Locals variables. */ |
252b5132 | 49 | static struct obstack stat_obstack; |
35835446 | 50 | static struct obstack map_obstack; |
252b5132 RH |
51 | |
52 | #define obstack_chunk_alloc xmalloc | |
53 | #define obstack_chunk_free free | |
5f992e62 | 54 | static const char *startup_file; |
252b5132 | 55 | static lang_statement_list_type input_file_chain; |
b34976b6 | 56 | static bfd_boolean placed_commons = FALSE; |
252b5132 | 57 | static lang_output_section_statement_type *default_common_section; |
b34976b6 | 58 | static bfd_boolean map_option_f; |
252b5132 RH |
59 | static bfd_vma print_dot; |
60 | static lang_input_statement_type *first_file; | |
5f992e62 AM |
61 | static const char *current_target; |
62 | static const char *output_target; | |
252b5132 RH |
63 | static lang_statement_list_type statement_list; |
64 | static struct lang_phdr *lang_phdr_list; | |
420e579c | 65 | static struct bfd_hash_table lang_definedness_table; |
252b5132 | 66 | |
cf888e70 | 67 | /* Forward declarations. */ |
1579bae1 | 68 | static void exp_init_os (etree_type *); |
35835446 | 69 | static void init_map_userdata (bfd *, asection *, void *); |
1579bae1 AM |
70 | static lang_input_statement_type *lookup_name (const char *); |
71 | static bfd_boolean load_symbols (lang_input_statement_type *, | |
72 | lang_statement_list_type *); | |
420e579c HPN |
73 | static struct bfd_hash_entry *lang_definedness_newfunc |
74 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); | |
1579bae1 | 75 | static void insert_undefined (const char *); |
35835446 JR |
76 | static void print_all_symbols (asection *); |
77 | static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *); | |
1579bae1 AM |
78 | static void print_statement (lang_statement_union_type *, |
79 | lang_output_section_statement_type *); | |
80 | static void print_statement_list (lang_statement_union_type *, | |
81 | lang_output_section_statement_type *); | |
82 | static void print_statements (void); | |
83 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); | |
84 | static void lang_record_phdrs (void); | |
85 | static void lang_do_version_exports_section (void); | |
86 | ||
87 | typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *, | |
88 | asection *, lang_input_statement_type *, void *); | |
5f992e62 | 89 | |
cf888e70 | 90 | /* Exported variables. */ |
252b5132 | 91 | lang_output_section_statement_type *abs_output_section; |
aea4bd9d | 92 | lang_statement_list_type lang_output_section_statement; |
252b5132 | 93 | lang_statement_list_type *stat_ptr = &statement_list; |
87f2a346 | 94 | lang_statement_list_type file_chain = { NULL, NULL }; |
e3e942e9 | 95 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 96 | const char *entry_section = ".text"; |
b34976b6 AM |
97 | bfd_boolean entry_from_cmdline; |
98 | bfd_boolean lang_has_input_file = FALSE; | |
99 | bfd_boolean had_output_filename = FALSE; | |
100 | bfd_boolean lang_float_flag = FALSE; | |
101 | bfd_boolean delete_output_file_on_failure = FALSE; | |
252b5132 | 102 | struct lang_nocrossrefs *nocrossref_list; |
577a0623 | 103 | struct unique_sections *unique_section_list; |
e3f2db7f | 104 | static bfd_boolean ldlang_sysrooted_script = FALSE; |
420e579c | 105 | int lang_statement_iteration = 0; |
252b5132 RH |
106 | |
107 | etree_type *base; /* Relocation base - or null */ | |
108 | ||
61f5d054 L |
109 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
110 | Although backslashes are treated specially if a pattern contains | |
111 | wildcards, we do not consider the mere presence of a backslash to | |
112 | be enough to cause the pattern to be treated as a wildcard. | |
113 | That lets us handle DOS filenames more naturally. */ | |
114 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
115 | ||
d1778b88 | 116 | #define new_stat(x, y) \ |
1579bae1 | 117 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 118 | |
d1778b88 AM |
119 | #define outside_section_address(q) \ |
120 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 121 | |
d1778b88 AM |
122 | #define outside_symbol_address(q) \ |
123 | ((q)->value + outside_section_address (q->section)) | |
252b5132 RH |
124 | |
125 | #define SECTION_NAME_MAP_LENGTH (16) | |
126 | ||
1579bae1 AM |
127 | void * |
128 | stat_alloc (size_t size) | |
252b5132 RH |
129 | { |
130 | return obstack_alloc (&stat_obstack, size); | |
131 | } | |
132 | ||
b34976b6 | 133 | bfd_boolean |
d0d6a25b | 134 | unique_section_p (const asection *sec) |
577a0623 AM |
135 | { |
136 | struct unique_sections *unam; | |
d0d6a25b | 137 | const char *secnam; |
577a0623 | 138 | |
d0d6a25b AM |
139 | if (link_info.relocatable |
140 | && sec->owner != NULL | |
141 | && bfd_is_group_section (sec->owner, sec)) | |
142 | return TRUE; | |
143 | ||
144 | secnam = sec->name; | |
577a0623 AM |
145 | for (unam = unique_section_list; unam; unam = unam->next) |
146 | if (wildcardp (unam->name) | |
147 | ? fnmatch (unam->name, secnam, 0) == 0 | |
148 | : strcmp (unam->name, secnam) == 0) | |
149 | { | |
b34976b6 | 150 | return TRUE; |
577a0623 AM |
151 | } |
152 | ||
b34976b6 | 153 | return FALSE; |
577a0623 AM |
154 | } |
155 | ||
08da4cac | 156 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e RH |
157 | |
158 | static void | |
1579bae1 AM |
159 | walk_wild_section (lang_wild_statement_type *ptr, |
160 | lang_input_statement_type *file, | |
161 | callback_t callback, | |
162 | void *data) | |
4dec4d4e | 163 | { |
b6bf44ba AM |
164 | asection *s; |
165 | ||
166 | if (file->just_syms_flag) | |
167 | return; | |
168 | ||
169 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
4dec4d4e | 170 | { |
b6bf44ba AM |
171 | struct wildcard_list *sec; |
172 | ||
173 | sec = ptr->section_list; | |
2181f54f AM |
174 | if (sec == NULL) |
175 | (*callback) (ptr, sec, s, file, data); | |
176 | ||
177 | while (sec != NULL) | |
08da4cac | 178 | { |
b34976b6 | 179 | bfd_boolean skip = FALSE; |
2181f54f AM |
180 | struct name_list *list_tmp; |
181 | ||
182 | /* Don't process sections from files which were | |
183 | excluded. */ | |
184 | for (list_tmp = sec->spec.exclude_name_list; | |
185 | list_tmp; | |
186 | list_tmp = list_tmp->next) | |
187 | { | |
188 | if (wildcardp (list_tmp->name)) | |
189 | skip = fnmatch (list_tmp->name, file->filename, 0) == 0; | |
190 | else | |
191 | skip = strcmp (list_tmp->name, file->filename) == 0; | |
4dec4d4e | 192 | |
34efb449 AO |
193 | /* If this file is part of an archive, and the archive is |
194 | excluded, exclude this file. */ | |
195 | if (! skip && file->the_bfd != NULL | |
196 | && file->the_bfd->my_archive != NULL | |
197 | && file->the_bfd->my_archive->filename != NULL) | |
198 | { | |
199 | if (wildcardp (list_tmp->name)) | |
200 | skip = fnmatch (list_tmp->name, | |
201 | file->the_bfd->my_archive->filename, | |
202 | 0) == 0; | |
203 | else | |
204 | skip = strcmp (list_tmp->name, | |
205 | file->the_bfd->my_archive->filename) == 0; | |
206 | } | |
207 | ||
2181f54f AM |
208 | if (skip) |
209 | break; | |
210 | } | |
211 | ||
212 | if (!skip && sec->spec.name != NULL) | |
b6bf44ba | 213 | { |
2181f54f AM |
214 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
215 | ||
216 | if (wildcardp (sec->spec.name)) | |
217 | skip = fnmatch (sec->spec.name, sname, 0) != 0; | |
218 | else | |
219 | skip = strcmp (sec->spec.name, sname) != 0; | |
b6bf44ba | 220 | } |
4dec4d4e | 221 | |
b6bf44ba AM |
222 | if (!skip) |
223 | (*callback) (ptr, sec, s, file, data); | |
4dec4d4e | 224 | |
2181f54f | 225 | sec = sec->next; |
4dec4d4e RH |
226 | } |
227 | } | |
228 | } | |
229 | ||
230 | /* Handle a wild statement for a single file F. */ | |
231 | ||
232 | static void | |
1579bae1 AM |
233 | walk_wild_file (lang_wild_statement_type *s, |
234 | lang_input_statement_type *f, | |
235 | callback_t callback, | |
236 | void *data) | |
4dec4d4e RH |
237 | { |
238 | if (f->the_bfd == NULL | |
239 | || ! bfd_check_format (f->the_bfd, bfd_archive)) | |
b6bf44ba | 240 | walk_wild_section (s, f, callback, data); |
4dec4d4e RH |
241 | else |
242 | { | |
243 | bfd *member; | |
244 | ||
245 | /* This is an archive file. We must map each member of the | |
246 | archive separately. */ | |
1579bae1 | 247 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
4dec4d4e RH |
248 | while (member != NULL) |
249 | { | |
250 | /* When lookup_name is called, it will call the add_symbols | |
251 | entry point for the archive. For each element of the | |
252 | archive which is included, BFD will call ldlang_add_file, | |
253 | which will set the usrdata field of the member to the | |
254 | lang_input_statement. */ | |
255 | if (member->usrdata != NULL) | |
256 | { | |
1579bae1 | 257 | walk_wild_section (s, member->usrdata, callback, data); |
4dec4d4e RH |
258 | } |
259 | ||
260 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | static void | |
1579bae1 | 266 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
4dec4d4e | 267 | { |
b6bf44ba AM |
268 | const char *file_spec = s->filename; |
269 | ||
270 | if (file_spec == NULL) | |
4dec4d4e RH |
271 | { |
272 | /* Perform the iteration over all files in the list. */ | |
e50d8076 | 273 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 274 | { |
b6bf44ba | 275 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
276 | } |
277 | } | |
b6bf44ba | 278 | else if (wildcardp (file_spec)) |
4dec4d4e | 279 | { |
e50d8076 | 280 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 281 | { |
b6bf44ba AM |
282 | if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0) |
283 | walk_wild_file (s, f, callback, data); | |
4dec4d4e RH |
284 | } |
285 | } | |
286 | else | |
287 | { | |
e50d8076 NC |
288 | lang_input_statement_type *f; |
289 | ||
4dec4d4e | 290 | /* Perform the iteration over a single file. */ |
b6bf44ba | 291 | f = lookup_name (file_spec); |
6770ec8c | 292 | if (f) |
b6bf44ba | 293 | walk_wild_file (s, f, callback, data); |
4dec4d4e | 294 | } |
5f992e62 AM |
295 | } |
296 | ||
08da4cac KH |
297 | /* lang_for_each_statement walks the parse tree and calls the provided |
298 | function for each node. */ | |
252b5132 RH |
299 | |
300 | static void | |
1579bae1 AM |
301 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
302 | lang_statement_union_type *s) | |
252b5132 | 303 | { |
1579bae1 | 304 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
305 | { |
306 | func (s); | |
307 | ||
308 | switch (s->header.type) | |
309 | { | |
310 | case lang_constructors_statement_enum: | |
311 | lang_for_each_statement_worker (func, constructor_list.head); | |
312 | break; | |
313 | case lang_output_section_statement_enum: | |
314 | lang_for_each_statement_worker | |
6feb9908 | 315 | (func, s->output_section_statement.children.head); |
252b5132 RH |
316 | break; |
317 | case lang_wild_statement_enum: | |
6feb9908 AM |
318 | lang_for_each_statement_worker (func, |
319 | s->wild_statement.children.head); | |
252b5132 RH |
320 | break; |
321 | case lang_group_statement_enum: | |
322 | lang_for_each_statement_worker (func, | |
323 | s->group_statement.children.head); | |
324 | break; | |
325 | case lang_data_statement_enum: | |
326 | case lang_reloc_statement_enum: | |
327 | case lang_object_symbols_statement_enum: | |
328 | case lang_output_statement_enum: | |
329 | case lang_target_statement_enum: | |
330 | case lang_input_section_enum: | |
331 | case lang_input_statement_enum: | |
332 | case lang_assignment_statement_enum: | |
333 | case lang_padding_statement_enum: | |
334 | case lang_address_statement_enum: | |
335 | case lang_fill_statement_enum: | |
336 | break; | |
337 | default: | |
338 | FAIL (); | |
339 | break; | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
344 | void | |
1579bae1 | 345 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 346 | { |
08da4cac | 347 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
348 | } |
349 | ||
350 | /*----------------------------------------------------------------------*/ | |
08da4cac | 351 | |
252b5132 | 352 | void |
1579bae1 | 353 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 354 | { |
1579bae1 | 355 | list->head = NULL; |
252b5132 RH |
356 | list->tail = &list->head; |
357 | } | |
358 | ||
08da4cac | 359 | /* Build a new statement node for the parse tree. */ |
252b5132 | 360 | |
08da4cac | 361 | static lang_statement_union_type * |
1579bae1 AM |
362 | new_statement (enum statement_enum type, |
363 | size_t size, | |
364 | lang_statement_list_type *list) | |
252b5132 | 365 | { |
1579bae1 | 366 | lang_statement_union_type *new; |
252b5132 | 367 | |
1579bae1 | 368 | new = stat_alloc (size); |
252b5132 | 369 | new->header.type = type; |
1579bae1 | 370 | new->header.next = NULL; |
252b5132 RH |
371 | lang_statement_append (list, new, &new->header.next); |
372 | return new; | |
373 | } | |
374 | ||
08da4cac KH |
375 | /* Build a new input file node for the language. There are several |
376 | ways in which we treat an input file, eg, we only look at symbols, | |
377 | or prefix it with a -l etc. | |
252b5132 | 378 | |
08da4cac | 379 | We can be supplied with requests for input files more than once; |
396a2467 | 380 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 381 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 382 | got it already so we don't duplicate the bfd. */ |
252b5132 | 383 | |
252b5132 | 384 | static lang_input_statement_type * |
1579bae1 AM |
385 | new_afile (const char *name, |
386 | lang_input_file_enum_type file_type, | |
387 | const char *target, | |
388 | bfd_boolean add_to_list) | |
252b5132 RH |
389 | { |
390 | lang_input_statement_type *p; | |
391 | ||
392 | if (add_to_list) | |
393 | p = new_stat (lang_input_statement, stat_ptr); | |
394 | else | |
395 | { | |
1579bae1 | 396 | p = stat_alloc (sizeof (lang_input_statement_type)); |
252b5132 RH |
397 | p->header.next = NULL; |
398 | } | |
399 | ||
b34976b6 | 400 | lang_has_input_file = TRUE; |
252b5132 | 401 | p->target = target; |
e3f2db7f | 402 | p->sysrooted = FALSE; |
252b5132 RH |
403 | switch (file_type) |
404 | { | |
405 | case lang_input_file_is_symbols_only_enum: | |
406 | p->filename = name; | |
b34976b6 AM |
407 | p->is_archive = FALSE; |
408 | p->real = TRUE; | |
252b5132 | 409 | p->local_sym_name = name; |
b34976b6 AM |
410 | p->just_syms_flag = TRUE; |
411 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
412 | break; |
413 | case lang_input_file_is_fake_enum: | |
414 | p->filename = name; | |
b34976b6 AM |
415 | p->is_archive = FALSE; |
416 | p->real = FALSE; | |
252b5132 | 417 | p->local_sym_name = name; |
b34976b6 AM |
418 | p->just_syms_flag = FALSE; |
419 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
420 | break; |
421 | case lang_input_file_is_l_enum: | |
b34976b6 | 422 | p->is_archive = TRUE; |
252b5132 | 423 | p->filename = name; |
b34976b6 | 424 | p->real = TRUE; |
1579bae1 | 425 | p->local_sym_name = concat ("-l", name, NULL); |
b34976b6 AM |
426 | p->just_syms_flag = FALSE; |
427 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
428 | break; |
429 | case lang_input_file_is_marker_enum: | |
430 | p->filename = name; | |
b34976b6 AM |
431 | p->is_archive = FALSE; |
432 | p->real = FALSE; | |
252b5132 | 433 | p->local_sym_name = name; |
b34976b6 AM |
434 | p->just_syms_flag = FALSE; |
435 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
436 | break; |
437 | case lang_input_file_is_search_file_enum: | |
e3f2db7f | 438 | p->sysrooted = ldlang_sysrooted_script; |
252b5132 | 439 | p->filename = name; |
b34976b6 AM |
440 | p->is_archive = FALSE; |
441 | p->real = TRUE; | |
252b5132 | 442 | p->local_sym_name = name; |
b34976b6 AM |
443 | p->just_syms_flag = FALSE; |
444 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
445 | break; |
446 | case lang_input_file_is_file_enum: | |
447 | p->filename = name; | |
b34976b6 AM |
448 | p->is_archive = FALSE; |
449 | p->real = TRUE; | |
252b5132 | 450 | p->local_sym_name = name; |
b34976b6 AM |
451 | p->just_syms_flag = FALSE; |
452 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
453 | break; |
454 | default: | |
455 | FAIL (); | |
456 | } | |
1579bae1 AM |
457 | p->the_bfd = NULL; |
458 | p->asymbols = NULL; | |
459 | p->next_real_file = NULL; | |
460 | p->next = NULL; | |
252b5132 RH |
461 | p->symbol_count = 0; |
462 | p->dynamic = config.dynamic_link; | |
e56f61be | 463 | p->add_needed = add_needed; |
4a43e768 | 464 | p->as_needed = as_needed; |
252b5132 | 465 | p->whole_archive = whole_archive; |
b34976b6 | 466 | p->loaded = FALSE; |
252b5132 RH |
467 | lang_statement_append (&input_file_chain, |
468 | (lang_statement_union_type *) p, | |
469 | &p->next_real_file); | |
470 | return p; | |
471 | } | |
472 | ||
473 | lang_input_statement_type * | |
1579bae1 AM |
474 | lang_add_input_file (const char *name, |
475 | lang_input_file_enum_type file_type, | |
476 | const char *target) | |
252b5132 | 477 | { |
b34976b6 AM |
478 | lang_has_input_file = TRUE; |
479 | return new_afile (name, file_type, target, TRUE); | |
252b5132 RH |
480 | } |
481 | ||
08da4cac KH |
482 | /* Build enough state so that the parser can build its tree. */ |
483 | ||
252b5132 | 484 | void |
1579bae1 | 485 | lang_init (void) |
252b5132 RH |
486 | { |
487 | obstack_begin (&stat_obstack, 1000); | |
488 | ||
489 | stat_ptr = &statement_list; | |
490 | ||
491 | lang_list_init (stat_ptr); | |
492 | ||
493 | lang_list_init (&input_file_chain); | |
494 | lang_list_init (&lang_output_section_statement); | |
495 | lang_list_init (&file_chain); | |
1579bae1 AM |
496 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
497 | NULL); | |
08da4cac KH |
498 | abs_output_section = |
499 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); | |
252b5132 RH |
500 | |
501 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c HPN |
502 | |
503 | /* The value "3" is ad-hoc, somewhat related to the expected number of | |
504 | DEFINED expressions in a linker script. For most default linker | |
505 | scripts, there are none. Why a hash table then? Well, it's somewhat | |
506 | simpler to re-use working machinery than using a linked list in terms | |
507 | of code-complexity here in ld, besides the initialization which just | |
508 | looks like other code here. */ | |
688c58f3 AM |
509 | if (!bfd_hash_table_init_n (&lang_definedness_table, |
510 | lang_definedness_newfunc, 3)) | |
420e579c HPN |
511 | einfo (_("%P%F: out of memory during initialization")); |
512 | ||
513 | /* Callers of exp_fold_tree need to increment this. */ | |
514 | lang_statement_iteration = 0; | |
252b5132 RH |
515 | } |
516 | ||
517 | /*---------------------------------------------------------------------- | |
08da4cac KH |
518 | A region is an area of memory declared with the |
519 | MEMORY { name:org=exp, len=exp ... } | |
520 | syntax. | |
252b5132 | 521 | |
08da4cac | 522 | We maintain a list of all the regions here. |
252b5132 | 523 | |
08da4cac | 524 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
525 | which is created when looked up to be the entire data space. |
526 | ||
527 | If create is true we are creating a region inside a MEMORY block. | |
528 | In this case it is probably an error to create a region that has | |
529 | already been created. If we are not inside a MEMORY block it is | |
530 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
531 | and so we issue a warning. */ | |
252b5132 RH |
532 | |
533 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
534 | static lang_memory_region_type **lang_memory_region_list_tail |
535 | = &lang_memory_region_list; | |
252b5132 RH |
536 | |
537 | lang_memory_region_type * | |
a747ee4d | 538 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
252b5132 RH |
539 | { |
540 | lang_memory_region_type *p; | |
1579bae1 | 541 | lang_memory_region_type *new; |
252b5132 | 542 | |
9f88b410 RS |
543 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
544 | if (name == NULL) | |
545 | return NULL; | |
546 | ||
1579bae1 | 547 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
a747ee4d NC |
548 | if (strcmp (p->name, name) == 0) |
549 | { | |
550 | if (create) | |
6feb9908 AM |
551 | einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), |
552 | name); | |
1579bae1 | 553 | return p; |
a747ee4d | 554 | } |
252b5132 RH |
555 | |
556 | #if 0 | |
557 | /* This code used to always use the first region in the list as the | |
558 | default region. I changed it to instead use a region | |
559 | encompassing all of memory as the default region. This permits | |
560 | NOLOAD sections to work reasonably without requiring a region. | |
561 | People should specify what region they mean, if they really want | |
562 | a region. */ | |
a747ee4d | 563 | if (strcmp (name, DEFAULT_MEMORY_REGION) == 0) |
252b5132 | 564 | { |
1579bae1 AM |
565 | if (lang_memory_region_list != NULL) |
566 | return lang_memory_region_list; | |
252b5132 RH |
567 | } |
568 | #endif | |
569 | ||
a747ee4d NC |
570 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
571 | einfo (_("%P:%S: warning: memory region %s not declared\n"), name); | |
572 | ||
1579bae1 | 573 | new = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 574 | |
1579bae1 AM |
575 | new->name = xstrdup (name); |
576 | new->next = NULL; | |
252b5132 | 577 | |
1579bae1 AM |
578 | *lang_memory_region_list_tail = new; |
579 | lang_memory_region_list_tail = &new->next; | |
580 | new->origin = 0; | |
581 | new->flags = 0; | |
582 | new->not_flags = 0; | |
583 | new->length = ~(bfd_size_type) 0; | |
584 | new->current = 0; | |
585 | new->had_full_message = FALSE; | |
252b5132 | 586 | |
1579bae1 | 587 | return new; |
252b5132 RH |
588 | } |
589 | ||
5f992e62 | 590 | static lang_memory_region_type * |
1579bae1 | 591 | lang_memory_default (asection *section) |
252b5132 RH |
592 | { |
593 | lang_memory_region_type *p; | |
594 | ||
595 | flagword sec_flags = section->flags; | |
596 | ||
597 | /* Override SEC_DATA to mean a writable section. */ | |
598 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
599 | sec_flags |= SEC_DATA; | |
600 | ||
1579bae1 | 601 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
602 | { |
603 | if ((p->flags & sec_flags) != 0 | |
604 | && (p->not_flags & sec_flags) == 0) | |
605 | { | |
606 | return p; | |
607 | } | |
608 | } | |
a747ee4d | 609 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
252b5132 RH |
610 | } |
611 | ||
0841712e JJ |
612 | static lang_output_section_statement_type * |
613 | lang_output_section_find_1 (const char *const name, int constraint) | |
252b5132 RH |
614 | { |
615 | lang_statement_union_type *u; | |
616 | lang_output_section_statement_type *lookup; | |
617 | ||
1579bae1 | 618 | for (u = lang_output_section_statement.head; u != NULL; u = lookup->next) |
252b5132 RH |
619 | { |
620 | lookup = &u->output_section_statement; | |
9f61903d | 621 | |
0841712e JJ |
622 | if (strcmp (name, lookup->name) == 0 |
623 | && lookup->constraint != -1 | |
624 | && (constraint == 0 || constraint == lookup->constraint)) | |
1579bae1 | 625 | return lookup; |
252b5132 | 626 | } |
1579bae1 | 627 | return NULL; |
252b5132 RH |
628 | } |
629 | ||
630 | lang_output_section_statement_type * | |
0841712e JJ |
631 | lang_output_section_find (const char *const name) |
632 | { | |
633 | return lang_output_section_find_1 (name, 0); | |
634 | } | |
635 | ||
636 | static lang_output_section_statement_type * | |
637 | lang_output_section_statement_lookup_1 (const char *const name, int constraint) | |
252b5132 RH |
638 | { |
639 | lang_output_section_statement_type *lookup; | |
640 | ||
0841712e | 641 | lookup = lang_output_section_find_1 (name, constraint); |
1579bae1 | 642 | if (lookup == NULL) |
252b5132 | 643 | { |
1579bae1 AM |
644 | lookup = new_stat (lang_output_section_statement, stat_ptr); |
645 | lookup->region = NULL; | |
646 | lookup->lma_region = NULL; | |
647 | lookup->fill = 0; | |
252b5132 RH |
648 | lookup->block_value = 1; |
649 | lookup->name = name; | |
650 | ||
1579bae1 AM |
651 | lookup->next = NULL; |
652 | lookup->bfd_section = NULL; | |
1b493742 | 653 | lookup->processed = 0; |
0841712e | 654 | lookup->constraint = constraint; |
252b5132 | 655 | lookup->sectype = normal_section; |
1579bae1 | 656 | lookup->addr_tree = NULL; |
252b5132 RH |
657 | lang_list_init (&lookup->children); |
658 | ||
1579bae1 | 659 | lookup->memspec = NULL; |
252b5132 RH |
660 | lookup->flags = 0; |
661 | lookup->subsection_alignment = -1; | |
662 | lookup->section_alignment = -1; | |
1579bae1 | 663 | lookup->load_base = NULL; |
9f88b410 | 664 | lookup->update_dot_tree = NULL; |
252b5132 RH |
665 | lookup->phdrs = NULL; |
666 | ||
667 | lang_statement_append (&lang_output_section_statement, | |
668 | (lang_statement_union_type *) lookup, | |
669 | &lookup->next); | |
670 | } | |
671 | return lookup; | |
672 | } | |
673 | ||
0841712e JJ |
674 | lang_output_section_statement_type * |
675 | lang_output_section_statement_lookup (const char *const name) | |
676 | { | |
677 | return lang_output_section_statement_lookup_1 (name, 0); | |
678 | } | |
679 | ||
252b5132 | 680 | static void |
1579bae1 | 681 | lang_map_flags (flagword flag) |
252b5132 RH |
682 | { |
683 | if (flag & SEC_ALLOC) | |
684 | minfo ("a"); | |
685 | ||
686 | if (flag & SEC_CODE) | |
687 | minfo ("x"); | |
688 | ||
689 | if (flag & SEC_READONLY) | |
690 | minfo ("r"); | |
691 | ||
692 | if (flag & SEC_DATA) | |
693 | minfo ("w"); | |
694 | ||
695 | if (flag & SEC_LOAD) | |
696 | minfo ("l"); | |
697 | } | |
698 | ||
699 | void | |
1579bae1 | 700 | lang_map (void) |
252b5132 RH |
701 | { |
702 | lang_memory_region_type *m; | |
35835446 | 703 | bfd *p; |
252b5132 RH |
704 | |
705 | minfo (_("\nMemory Configuration\n\n")); | |
706 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
707 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
708 | ||
1579bae1 | 709 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 RH |
710 | { |
711 | char buf[100]; | |
712 | int len; | |
713 | ||
714 | fprintf (config.map_file, "%-16s ", m->name); | |
715 | ||
716 | sprintf_vma (buf, m->origin); | |
717 | minfo ("0x%s ", buf); | |
718 | len = strlen (buf); | |
719 | while (len < 16) | |
720 | { | |
721 | print_space (); | |
722 | ++len; | |
723 | } | |
724 | ||
725 | minfo ("0x%V", m->length); | |
726 | if (m->flags || m->not_flags) | |
727 | { | |
728 | #ifndef BFD64 | |
729 | minfo (" "); | |
730 | #endif | |
731 | if (m->flags) | |
732 | { | |
733 | print_space (); | |
734 | lang_map_flags (m->flags); | |
735 | } | |
736 | ||
737 | if (m->not_flags) | |
738 | { | |
739 | minfo (" !"); | |
740 | lang_map_flags (m->not_flags); | |
741 | } | |
742 | } | |
743 | ||
744 | print_nl (); | |
745 | } | |
746 | ||
747 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
748 | ||
35835446 JR |
749 | if (! command_line.reduce_memory_overheads) |
750 | { | |
751 | obstack_begin (&map_obstack, 1000); | |
752 | for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next) | |
753 | bfd_map_over_sections (p, init_map_userdata, 0); | |
754 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); | |
755 | } | |
252b5132 RH |
756 | print_statements (); |
757 | } | |
758 | ||
35835446 JR |
759 | static void |
760 | init_map_userdata (abfd, sec, data) | |
761 | bfd *abfd ATTRIBUTE_UNUSED; | |
762 | asection *sec; | |
763 | void *data ATTRIBUTE_UNUSED; | |
764 | { | |
765 | fat_section_userdata_type *new_data | |
766 | = ((fat_section_userdata_type *) (stat_alloc | |
767 | (sizeof (fat_section_userdata_type)))); | |
768 | ||
769 | ASSERT (get_userdata (sec) == NULL); | |
770 | get_userdata (sec) = new_data; | |
771 | new_data->map_symbol_def_tail = &new_data->map_symbol_def_head; | |
772 | } | |
773 | ||
774 | static bfd_boolean | |
775 | sort_def_symbol (hash_entry, info) | |
776 | struct bfd_link_hash_entry *hash_entry; | |
777 | void *info ATTRIBUTE_UNUSED; | |
778 | { | |
779 | if (hash_entry->type == bfd_link_hash_defined | |
780 | || hash_entry->type == bfd_link_hash_defweak) | |
781 | { | |
782 | struct fat_user_section_struct *ud; | |
783 | struct map_symbol_def *def; | |
784 | ||
785 | ud = get_userdata (hash_entry->u.def.section); | |
786 | if (! ud) | |
787 | { | |
788 | /* ??? What do we have to do to initialize this beforehand? */ | |
789 | /* The first time we get here is bfd_abs_section... */ | |
790 | init_map_userdata (0, hash_entry->u.def.section, 0); | |
791 | ud = get_userdata (hash_entry->u.def.section); | |
792 | } | |
793 | else if (!ud->map_symbol_def_tail) | |
794 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; | |
76d7af2d | 795 | |
6feb9908 | 796 | def = obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 797 | def->entry = hash_entry; |
76d7af2d | 798 | *(ud->map_symbol_def_tail) = def; |
35835446 JR |
799 | ud->map_symbol_def_tail = &def->next; |
800 | } | |
801 | return TRUE; | |
802 | } | |
803 | ||
252b5132 RH |
804 | /* Initialize an output section. */ |
805 | ||
806 | static void | |
1579bae1 | 807 | init_os (lang_output_section_statement_type *s) |
252b5132 | 808 | { |
35835446 | 809 | lean_section_userdata_type *new; |
252b5132 RH |
810 | |
811 | if (s->bfd_section != NULL) | |
812 | return; | |
813 | ||
814 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) | |
6f9efd97 | 815 | einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 816 | |
35835446 | 817 | new = stat_alloc (SECTION_USERDATA_SIZE); |
76d7af2d | 818 | memset (new, 0, SECTION_USERDATA_SIZE); |
252b5132 RH |
819 | |
820 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); | |
1579bae1 | 821 | if (s->bfd_section == NULL) |
252b5132 | 822 | s->bfd_section = bfd_make_section (output_bfd, s->name); |
1579bae1 | 823 | if (s->bfd_section == NULL) |
252b5132 RH |
824 | { |
825 | einfo (_("%P%F: output format %s cannot represent section called %s\n"), | |
826 | output_bfd->xvec->name, s->name); | |
827 | } | |
828 | s->bfd_section->output_section = s->bfd_section; | |
829 | ||
08da4cac KH |
830 | /* We initialize an output sections output offset to minus its own |
831 | vma to allow us to output a section through itself. */ | |
252b5132 | 832 | s->bfd_section->output_offset = 0; |
1579bae1 | 833 | get_userdata (s->bfd_section) = new; |
252b5132 RH |
834 | |
835 | /* If there is a base address, make sure that any sections it might | |
836 | mention are initialized. */ | |
837 | if (s->addr_tree != NULL) | |
838 | exp_init_os (s->addr_tree); | |
18794b0c AM |
839 | |
840 | if (s->load_base != NULL) | |
841 | exp_init_os (s->load_base); | |
252b5132 RH |
842 | } |
843 | ||
844 | /* Make sure that all output sections mentioned in an expression are | |
845 | initialized. */ | |
846 | ||
847 | static void | |
1579bae1 | 848 | exp_init_os (etree_type *exp) |
252b5132 RH |
849 | { |
850 | switch (exp->type.node_class) | |
851 | { | |
852 | case etree_assign: | |
853 | exp_init_os (exp->assign.src); | |
854 | break; | |
855 | ||
856 | case etree_binary: | |
857 | exp_init_os (exp->binary.lhs); | |
858 | exp_init_os (exp->binary.rhs); | |
859 | break; | |
860 | ||
861 | case etree_trinary: | |
862 | exp_init_os (exp->trinary.cond); | |
863 | exp_init_os (exp->trinary.lhs); | |
864 | exp_init_os (exp->trinary.rhs); | |
865 | break; | |
866 | ||
b6ca8815 NS |
867 | case etree_assert: |
868 | exp_init_os (exp->assert_s.child); | |
869 | break; | |
870 | ||
252b5132 RH |
871 | case etree_unary: |
872 | exp_init_os (exp->unary.child); | |
873 | break; | |
874 | ||
875 | case etree_name: | |
876 | switch (exp->type.node_code) | |
877 | { | |
878 | case ADDR: | |
879 | case LOADADDR: | |
880 | case SIZEOF: | |
881 | { | |
882 | lang_output_section_statement_type *os; | |
883 | ||
884 | os = lang_output_section_find (exp->name.name); | |
885 | if (os != NULL && os->bfd_section == NULL) | |
886 | init_os (os); | |
887 | } | |
888 | } | |
889 | break; | |
890 | ||
891 | default: | |
892 | break; | |
893 | } | |
894 | } | |
9503fd87 | 895 | \f |
252b5132 | 896 | static void |
1579bae1 | 897 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 898 | { |
1579bae1 | 899 | lang_input_statement_type *entry = data; |
252b5132 RH |
900 | |
901 | /* If we are only reading symbols from this object, then we want to | |
902 | discard all sections. */ | |
903 | if (entry->just_syms_flag) | |
904 | { | |
c2c01aa7 | 905 | bfd_link_just_syms (sec, &link_info); |
252b5132 RH |
906 | return; |
907 | } | |
908 | ||
082b7297 | 909 | bfd_section_already_linked (abfd, sec); |
252b5132 RH |
910 | } |
911 | \f | |
912 | /* The wild routines. | |
913 | ||
914 | These expand statements like *(.text) and foo.o to a list of | |
915 | explicit actions, like foo.o(.text), bar.o(.text) and | |
916 | foo.o(.text, .data). */ | |
917 | ||
252b5132 RH |
918 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
919 | lang_input_section statement which is placed at PTR. FILE is the | |
920 | input file which holds SECTION. */ | |
921 | ||
922 | void | |
1579bae1 AM |
923 | lang_add_section (lang_statement_list_type *ptr, |
924 | asection *section, | |
925 | lang_output_section_statement_type *output, | |
926 | lang_input_statement_type *file) | |
252b5132 | 927 | { |
164e712d | 928 | flagword flags = section->flags; |
b34976b6 | 929 | bfd_boolean discard; |
252b5132 | 930 | |
e49f5022 | 931 | /* Discard sections marked with SEC_EXCLUDE. */ |
b3096250 | 932 | discard = (flags & SEC_EXCLUDE) != 0; |
252b5132 RH |
933 | |
934 | /* Discard input sections which are assigned to a section named | |
935 | DISCARD_SECTION_NAME. */ | |
936 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
b34976b6 | 937 | discard = TRUE; |
252b5132 RH |
938 | |
939 | /* Discard debugging sections if we are stripping debugging | |
940 | information. */ | |
941 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
942 | && (flags & SEC_DEBUGGING) != 0) | |
b34976b6 | 943 | discard = TRUE; |
252b5132 RH |
944 | |
945 | if (discard) | |
946 | { | |
947 | if (section->output_section == NULL) | |
948 | { | |
949 | /* This prevents future calls from assigning this section. */ | |
950 | section->output_section = bfd_abs_section_ptr; | |
951 | } | |
952 | return; | |
953 | } | |
954 | ||
955 | if (section->output_section == NULL) | |
956 | { | |
b34976b6 | 957 | bfd_boolean first; |
252b5132 RH |
958 | lang_input_section_type *new; |
959 | flagword flags; | |
960 | ||
961 | if (output->bfd_section == NULL) | |
d1778b88 AM |
962 | init_os (output); |
963 | ||
964 | first = ! output->bfd_section->linker_has_input; | |
965 | output->bfd_section->linker_has_input = 1; | |
252b5132 | 966 | |
08da4cac | 967 | /* Add a section reference to the list. */ |
252b5132 RH |
968 | new = new_stat (lang_input_section, ptr); |
969 | ||
970 | new->section = section; | |
971 | new->ifile = file; | |
972 | section->output_section = output->bfd_section; | |
973 | ||
974 | flags = section->flags; | |
975 | ||
976 | /* We don't copy the SEC_NEVER_LOAD flag from an input section | |
977 | to an output section, because we want to be able to include a | |
978 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
979 | section (I don't know why we want to do this, but we do). | |
980 | build_link_order in ldwrite.c handles this case by turning | |
981 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
982 | ||
983 | flags &= ~ SEC_NEVER_LOAD; | |
984 | ||
985 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've | |
986 | already been processed. One reason to do this is that on pe | |
987 | format targets, .text$foo sections go into .text and it's odd | |
988 | to see .text with SEC_LINK_ONCE set. */ | |
989 | ||
1049f94e | 990 | if (! link_info.relocatable) |
252b5132 RH |
991 | flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES); |
992 | ||
993 | /* If this is not the first input section, and the SEC_READONLY | |
994 | flag is not currently set, then don't set it just because the | |
995 | input section has it set. */ | |
996 | ||
997 | if (! first && (section->output_section->flags & SEC_READONLY) == 0) | |
998 | flags &= ~ SEC_READONLY; | |
999 | ||
f5fa8ca2 JJ |
1000 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
1001 | if (! first | |
1002 | && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS)) | |
1003 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
1004 | || ((flags & SEC_MERGE) | |
1005 | && section->output_section->entsize != section->entsize))) | |
1006 | { | |
1007 | section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
1008 | flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
1009 | } | |
1010 | ||
252b5132 RH |
1011 | section->output_section->flags |= flags; |
1012 | ||
f5fa8ca2 JJ |
1013 | if (flags & SEC_MERGE) |
1014 | section->output_section->entsize = section->entsize; | |
1015 | ||
252b5132 RH |
1016 | /* If SEC_READONLY is not set in the input section, then clear |
1017 | it from the output section. */ | |
1018 | if ((section->flags & SEC_READONLY) == 0) | |
1019 | section->output_section->flags &= ~SEC_READONLY; | |
1020 | ||
1021 | switch (output->sectype) | |
1022 | { | |
1023 | case normal_section: | |
1024 | break; | |
1025 | case dsect_section: | |
1026 | case copy_section: | |
1027 | case info_section: | |
1028 | case overlay_section: | |
1029 | output->bfd_section->flags &= ~SEC_ALLOC; | |
1030 | break; | |
1031 | case noload_section: | |
1032 | output->bfd_section->flags &= ~SEC_LOAD; | |
1033 | output->bfd_section->flags |= SEC_NEVER_LOAD; | |
1034 | break; | |
1035 | } | |
1036 | ||
667f5177 ILT |
1037 | /* Copy over SEC_SMALL_DATA. */ |
1038 | if (section->flags & SEC_SMALL_DATA) | |
1039 | section->output_section->flags |= SEC_SMALL_DATA; | |
9e41f973 | 1040 | |
252b5132 RH |
1041 | if (section->alignment_power > output->bfd_section->alignment_power) |
1042 | output->bfd_section->alignment_power = section->alignment_power; | |
1043 | ||
396a2467 | 1044 | /* If supplied an alignment, then force it. */ |
252b5132 RH |
1045 | if (output->section_alignment != -1) |
1046 | output->bfd_section->alignment_power = output->section_alignment; | |
74459f0e TW |
1047 | |
1048 | if (section->flags & SEC_BLOCK) | |
08da4cac KH |
1049 | { |
1050 | section->output_section->flags |= SEC_BLOCK; | |
1051 | /* FIXME: This value should really be obtained from the bfd... */ | |
1052 | output->block_value = 128; | |
1053 | } | |
252b5132 RH |
1054 | } |
1055 | } | |
1056 | ||
bcaa7b3e L |
1057 | /* Compare sections ASEC and BSEC according to SORT. */ |
1058 | ||
1059 | static int | |
1060 | compare_section (sort_type sort, asection *asec, asection *bsec) | |
1061 | { | |
1062 | int ret; | |
1063 | ||
1064 | switch (sort) | |
1065 | { | |
1066 | default: | |
1067 | abort (); | |
1068 | ||
1069 | case by_alignment_name: | |
1070 | ret = (bfd_section_alignment (bsec->owner, bsec) | |
1071 | - bfd_section_alignment (asec->owner, asec)); | |
1072 | if (ret) | |
1073 | break; | |
1074 | /* Fall through. */ | |
1075 | ||
1076 | case by_name: | |
1077 | ret = strcmp (bfd_get_section_name (asec->owner, asec), | |
1078 | bfd_get_section_name (bsec->owner, bsec)); | |
1079 | break; | |
1080 | ||
1081 | case by_name_alignment: | |
1082 | ret = strcmp (bfd_get_section_name (asec->owner, asec), | |
1083 | bfd_get_section_name (bsec->owner, bsec)); | |
1084 | if (ret) | |
1085 | break; | |
1086 | /* Fall through. */ | |
1087 | ||
1088 | case by_alignment: | |
1089 | ret = (bfd_section_alignment (bsec->owner, bsec) | |
1090 | - bfd_section_alignment (asec->owner, asec)); | |
1091 | break; | |
1092 | } | |
1093 | ||
1094 | return ret; | |
1095 | } | |
1096 | ||
252b5132 RH |
1097 | /* Handle wildcard sorting. This returns the lang_input_section which |
1098 | should follow the one we are going to create for SECTION and FILE, | |
1099 | based on the sorting requirements of WILD. It returns NULL if the | |
1100 | new section should just go at the end of the current list. */ | |
1101 | ||
1102 | static lang_statement_union_type * | |
1579bae1 AM |
1103 | wild_sort (lang_wild_statement_type *wild, |
1104 | struct wildcard_list *sec, | |
1105 | lang_input_statement_type *file, | |
1106 | asection *section) | |
252b5132 RH |
1107 | { |
1108 | const char *section_name; | |
1109 | lang_statement_union_type *l; | |
1110 | ||
bcaa7b3e L |
1111 | if (!wild->filenames_sorted |
1112 | && (sec == NULL || sec->spec.sorted == none)) | |
252b5132 RH |
1113 | return NULL; |
1114 | ||
1115 | section_name = bfd_get_section_name (file->the_bfd, section); | |
bba1a0c0 | 1116 | for (l = wild->children.head; l != NULL; l = l->header.next) |
252b5132 RH |
1117 | { |
1118 | lang_input_section_type *ls; | |
1119 | ||
1120 | if (l->header.type != lang_input_section_enum) | |
1121 | continue; | |
1122 | ls = &l->input_section; | |
1123 | ||
1124 | /* Sorting by filename takes precedence over sorting by section | |
1125 | name. */ | |
1126 | ||
1127 | if (wild->filenames_sorted) | |
1128 | { | |
1129 | const char *fn, *ln; | |
b34976b6 | 1130 | bfd_boolean fa, la; |
252b5132 RH |
1131 | int i; |
1132 | ||
1133 | /* The PE support for the .idata section as generated by | |
1134 | dlltool assumes that files will be sorted by the name of | |
1135 | the archive and then the name of the file within the | |
1136 | archive. */ | |
1137 | ||
1138 | if (file->the_bfd != NULL | |
1139 | && bfd_my_archive (file->the_bfd) != NULL) | |
1140 | { | |
1141 | fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); | |
b34976b6 | 1142 | fa = TRUE; |
252b5132 RH |
1143 | } |
1144 | else | |
1145 | { | |
1146 | fn = file->filename; | |
b34976b6 | 1147 | fa = FALSE; |
252b5132 RH |
1148 | } |
1149 | ||
1150 | if (ls->ifile->the_bfd != NULL | |
1151 | && bfd_my_archive (ls->ifile->the_bfd) != NULL) | |
1152 | { | |
1153 | ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd)); | |
b34976b6 | 1154 | la = TRUE; |
252b5132 RH |
1155 | } |
1156 | else | |
1157 | { | |
1158 | ln = ls->ifile->filename; | |
b34976b6 | 1159 | la = FALSE; |
252b5132 RH |
1160 | } |
1161 | ||
1162 | i = strcmp (fn, ln); | |
1163 | if (i > 0) | |
1164 | continue; | |
1165 | else if (i < 0) | |
1166 | break; | |
1167 | ||
1168 | if (fa || la) | |
1169 | { | |
1170 | if (fa) | |
1171 | fn = file->filename; | |
1172 | if (la) | |
1173 | ln = ls->ifile->filename; | |
1174 | ||
1175 | i = strcmp (fn, ln); | |
1176 | if (i > 0) | |
1177 | continue; | |
1178 | else if (i < 0) | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | /* Here either the files are not sorted by name, or we are | |
1184 | looking at the sections for this file. */ | |
1185 | ||
bcaa7b3e | 1186 | if (sec != NULL && sec->spec.sorted != none) |
252b5132 | 1187 | { |
bcaa7b3e L |
1188 | if (compare_section (sec->spec.sorted, section, |
1189 | ls->section) < 0) | |
252b5132 RH |
1190 | break; |
1191 | } | |
1192 | } | |
1193 | ||
1194 | return l; | |
1195 | } | |
1196 | ||
1197 | /* Expand a wild statement for a particular FILE. SECTION may be | |
1198 | NULL, in which case it is a wild card. */ | |
1199 | ||
1200 | static void | |
1579bae1 AM |
1201 | output_section_callback (lang_wild_statement_type *ptr, |
1202 | struct wildcard_list *sec, | |
1203 | asection *section, | |
1204 | lang_input_statement_type *file, | |
1205 | void *output) | |
4dec4d4e RH |
1206 | { |
1207 | lang_statement_union_type *before; | |
5f992e62 | 1208 | |
b6bf44ba | 1209 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0d6a25b | 1210 | if (unique_section_p (section)) |
b6bf44ba AM |
1211 | return; |
1212 | ||
b6bf44ba | 1213 | before = wild_sort (ptr, sec, file, section); |
5f992e62 | 1214 | |
4dec4d4e RH |
1215 | /* Here BEFORE points to the lang_input_section which |
1216 | should follow the one we are about to add. If BEFORE | |
1217 | is NULL, then the section should just go at the end | |
1218 | of the current list. */ | |
5f992e62 | 1219 | |
4dec4d4e | 1220 | if (before == NULL) |
39dcfe18 AM |
1221 | lang_add_section (&ptr->children, section, |
1222 | (lang_output_section_statement_type *) output, | |
1223 | file); | |
4dec4d4e | 1224 | else |
252b5132 | 1225 | { |
4dec4d4e RH |
1226 | lang_statement_list_type list; |
1227 | lang_statement_union_type **pp; | |
5f992e62 | 1228 | |
4dec4d4e | 1229 | lang_list_init (&list); |
39dcfe18 AM |
1230 | lang_add_section (&list, section, |
1231 | (lang_output_section_statement_type *) output, | |
1232 | file); | |
5f992e62 | 1233 | |
4dec4d4e RH |
1234 | /* If we are discarding the section, LIST.HEAD will |
1235 | be NULL. */ | |
1236 | if (list.head != NULL) | |
252b5132 | 1237 | { |
bba1a0c0 | 1238 | ASSERT (list.head->header.next == NULL); |
5f992e62 | 1239 | |
4dec4d4e RH |
1240 | for (pp = &ptr->children.head; |
1241 | *pp != before; | |
bba1a0c0 | 1242 | pp = &(*pp)->header.next) |
4dec4d4e | 1243 | ASSERT (*pp != NULL); |
5f992e62 | 1244 | |
bba1a0c0 | 1245 | list.head->header.next = *pp; |
4dec4d4e | 1246 | *pp = list.head; |
252b5132 RH |
1247 | } |
1248 | } | |
1249 | } | |
1250 | ||
0841712e JJ |
1251 | /* Check if all sections in a wild statement for a particular FILE |
1252 | are readonly. */ | |
1253 | ||
1254 | static void | |
1255 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
1256 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
1257 | asection *section, | |
1258 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
6feb9908 | 1259 | void *data) |
0841712e JJ |
1260 | { |
1261 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ | |
1262 | if (unique_section_p (section)) | |
1263 | return; | |
1264 | ||
6feb9908 AM |
1265 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
1266 | ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE; | |
0841712e JJ |
1267 | } |
1268 | ||
252b5132 RH |
1269 | /* This is passed a file name which must have been seen already and |
1270 | added to the statement tree. We will see if it has been opened | |
1271 | already and had its symbols read. If not then we'll read it. */ | |
1272 | ||
1273 | static lang_input_statement_type * | |
1579bae1 | 1274 | lookup_name (const char *name) |
252b5132 RH |
1275 | { |
1276 | lang_input_statement_type *search; | |
1277 | ||
1278 | for (search = (lang_input_statement_type *) input_file_chain.head; | |
1579bae1 | 1279 | search != NULL; |
252b5132 RH |
1280 | search = (lang_input_statement_type *) search->next_real_file) |
1281 | { | |
0013291d NC |
1282 | /* Use the local_sym_name as the name of the file that has |
1283 | already been loaded as filename might have been transformed | |
1284 | via the search directory lookup mechanism. */ | |
1285 | const char * filename = search->local_sym_name; | |
1286 | ||
1287 | if (filename == NULL && name == NULL) | |
252b5132 | 1288 | return search; |
0013291d | 1289 | if (filename != NULL |
1579bae1 | 1290 | && name != NULL |
0013291d | 1291 | && strcmp (filename, name) == 0) |
252b5132 RH |
1292 | break; |
1293 | } | |
1294 | ||
1579bae1 | 1295 | if (search == NULL) |
6feb9908 AM |
1296 | search = new_afile (name, lang_input_file_is_search_file_enum, |
1297 | default_target, FALSE); | |
252b5132 RH |
1298 | |
1299 | /* If we have already added this file, or this file is not real | |
1300 | (FIXME: can that ever actually happen?) or the name is NULL | |
1301 | (FIXME: can that ever actually happen?) don't add this file. */ | |
1302 | if (search->loaded | |
1303 | || ! search->real | |
1579bae1 | 1304 | || search->filename == NULL) |
252b5132 RH |
1305 | return search; |
1306 | ||
1579bae1 | 1307 | if (! load_symbols (search, NULL)) |
6770ec8c | 1308 | return NULL; |
252b5132 RH |
1309 | |
1310 | return search; | |
1311 | } | |
1312 | ||
1313 | /* Get the symbols for an input file. */ | |
1314 | ||
b34976b6 | 1315 | static bfd_boolean |
1579bae1 AM |
1316 | load_symbols (lang_input_statement_type *entry, |
1317 | lang_statement_list_type *place) | |
252b5132 RH |
1318 | { |
1319 | char **matching; | |
1320 | ||
1321 | if (entry->loaded) | |
b34976b6 | 1322 | return TRUE; |
252b5132 RH |
1323 | |
1324 | ldfile_open_file (entry); | |
1325 | ||
1326 | if (! bfd_check_format (entry->the_bfd, bfd_archive) | |
1327 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
1328 | { | |
1329 | bfd_error_type err; | |
1330 | lang_statement_list_type *hold; | |
b34976b6 | 1331 | bfd_boolean bad_load = TRUE; |
e3f2db7f | 1332 | bfd_boolean save_ldlang_sysrooted_script; |
b7a26f91 | 1333 | |
252b5132 | 1334 | err = bfd_get_error (); |
884fb58e NC |
1335 | |
1336 | /* See if the emulation has some special knowledge. */ | |
1337 | if (ldemul_unrecognized_file (entry)) | |
b34976b6 | 1338 | return TRUE; |
884fb58e | 1339 | |
252b5132 RH |
1340 | if (err == bfd_error_file_ambiguously_recognized) |
1341 | { | |
1342 | char **p; | |
1343 | ||
1344 | einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); | |
1345 | einfo (_("%B: matching formats:"), entry->the_bfd); | |
1346 | for (p = matching; *p != NULL; p++) | |
1347 | einfo (" %s", *p); | |
1348 | einfo ("%F\n"); | |
1349 | } | |
1350 | else if (err != bfd_error_file_not_recognized | |
1351 | || place == NULL) | |
6770ec8c NC |
1352 | einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); |
1353 | else | |
b34976b6 | 1354 | bad_load = FALSE; |
b7a26f91 | 1355 | |
252b5132 RH |
1356 | bfd_close (entry->the_bfd); |
1357 | entry->the_bfd = NULL; | |
1358 | ||
252b5132 | 1359 | /* Try to interpret the file as a linker script. */ |
252b5132 RH |
1360 | ldfile_open_command_file (entry->filename); |
1361 | ||
1362 | hold = stat_ptr; | |
1363 | stat_ptr = place; | |
e3f2db7f AO |
1364 | save_ldlang_sysrooted_script = ldlang_sysrooted_script; |
1365 | ldlang_sysrooted_script = entry->sysrooted; | |
252b5132 | 1366 | |
b34976b6 | 1367 | ldfile_assumed_script = TRUE; |
252b5132 RH |
1368 | parser_input = input_script; |
1369 | yyparse (); | |
b34976b6 | 1370 | ldfile_assumed_script = FALSE; |
252b5132 | 1371 | |
e3f2db7f | 1372 | ldlang_sysrooted_script = save_ldlang_sysrooted_script; |
252b5132 RH |
1373 | stat_ptr = hold; |
1374 | ||
6770ec8c | 1375 | return ! bad_load; |
252b5132 RH |
1376 | } |
1377 | ||
1378 | if (ldemul_recognized_file (entry)) | |
b34976b6 | 1379 | return TRUE; |
252b5132 RH |
1380 | |
1381 | /* We don't call ldlang_add_file for an archive. Instead, the | |
1382 | add_symbols entry point will call ldlang_add_file, via the | |
1383 | add_archive_element callback, for each element of the archive | |
1384 | which is used. */ | |
1385 | switch (bfd_get_format (entry->the_bfd)) | |
1386 | { | |
1387 | default: | |
1388 | break; | |
1389 | ||
1390 | case bfd_object: | |
1391 | ldlang_add_file (entry); | |
1392 | if (trace_files || trace_file_tries) | |
1393 | info_msg ("%I\n", entry); | |
1394 | break; | |
1395 | ||
1396 | case bfd_archive: | |
1397 | if (entry->whole_archive) | |
1398 | { | |
b7a26f91 | 1399 | bfd *member = NULL; |
b34976b6 | 1400 | bfd_boolean loaded = TRUE; |
6770ec8c NC |
1401 | |
1402 | for (;;) | |
252b5132 | 1403 | { |
6770ec8c NC |
1404 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
1405 | ||
1406 | if (member == NULL) | |
1407 | break; | |
b7a26f91 | 1408 | |
252b5132 | 1409 | if (! bfd_check_format (member, bfd_object)) |
6770ec8c NC |
1410 | { |
1411 | einfo (_("%F%B: member %B in archive is not an object\n"), | |
1412 | entry->the_bfd, member); | |
b34976b6 | 1413 | loaded = FALSE; |
6770ec8c NC |
1414 | } |
1415 | ||
252b5132 RH |
1416 | if (! ((*link_info.callbacks->add_archive_element) |
1417 | (&link_info, member, "--whole-archive"))) | |
1418 | abort (); | |
6770ec8c | 1419 | |
252b5132 | 1420 | if (! bfd_link_add_symbols (member, &link_info)) |
6770ec8c NC |
1421 | { |
1422 | einfo (_("%F%B: could not read symbols: %E\n"), member); | |
b34976b6 | 1423 | loaded = FALSE; |
6770ec8c | 1424 | } |
252b5132 RH |
1425 | } |
1426 | ||
6770ec8c NC |
1427 | entry->loaded = loaded; |
1428 | return loaded; | |
252b5132 | 1429 | } |
6770ec8c | 1430 | break; |
252b5132 RH |
1431 | } |
1432 | ||
03bdc404 | 1433 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
b34976b6 | 1434 | entry->loaded = TRUE; |
6770ec8c | 1435 | else |
252b5132 RH |
1436 | einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd); |
1437 | ||
6770ec8c | 1438 | return entry->loaded; |
252b5132 RH |
1439 | } |
1440 | ||
b6bf44ba AM |
1441 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
1442 | may be NULL, indicating that it is a wildcard. Separate | |
1443 | lang_input_section statements are created for each part of the | |
1444 | expansion; they are added after the wild statement S. OUTPUT is | |
1445 | the output section. */ | |
252b5132 RH |
1446 | |
1447 | static void | |
1579bae1 AM |
1448 | wild (lang_wild_statement_type *s, |
1449 | const char *target ATTRIBUTE_UNUSED, | |
1450 | lang_output_section_statement_type *output) | |
252b5132 | 1451 | { |
b6bf44ba | 1452 | struct wildcard_list *sec; |
252b5132 | 1453 | |
1579bae1 | 1454 | walk_wild (s, output_section_callback, output); |
b6bf44ba AM |
1455 | |
1456 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
252b5132 | 1457 | { |
b6bf44ba AM |
1458 | if (default_common_section != NULL) |
1459 | break; | |
1460 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) | |
1461 | { | |
1462 | /* Remember the section that common is going to in case we | |
b7a26f91 | 1463 | later get something which doesn't know where to put it. */ |
b6bf44ba AM |
1464 | default_common_section = output; |
1465 | } | |
252b5132 RH |
1466 | } |
1467 | } | |
1468 | ||
b34976b6 | 1469 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 1470 | |
e50d8076 | 1471 | static int |
1579bae1 | 1472 | get_target (const bfd_target *target, void *data) |
e50d8076 | 1473 | { |
1579bae1 | 1474 | const char *sought = data; |
5f992e62 | 1475 | |
e50d8076 NC |
1476 | return strcmp (target->name, sought) == 0; |
1477 | } | |
1478 | ||
1479 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 1480 | |
e50d8076 | 1481 | static void |
1579bae1 | 1482 | stricpy (char *dest, char *src) |
e50d8076 NC |
1483 | { |
1484 | char c; | |
5f992e62 | 1485 | |
08da4cac | 1486 | while ((c = *src++) != 0) |
3882b010 | 1487 | *dest++ = TOLOWER (c); |
e50d8076 | 1488 | |
08da4cac | 1489 | *dest = 0; |
e50d8076 NC |
1490 | } |
1491 | ||
396a2467 | 1492 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 1493 | from haystack. */ |
08da4cac | 1494 | |
e50d8076 | 1495 | static void |
1579bae1 | 1496 | strcut (char *haystack, char *needle) |
e50d8076 NC |
1497 | { |
1498 | haystack = strstr (haystack, needle); | |
5f992e62 | 1499 | |
e50d8076 NC |
1500 | if (haystack) |
1501 | { | |
08da4cac | 1502 | char *src; |
e50d8076 | 1503 | |
08da4cac KH |
1504 | for (src = haystack + strlen (needle); *src;) |
1505 | *haystack++ = *src++; | |
5f992e62 | 1506 | |
08da4cac | 1507 | *haystack = 0; |
e50d8076 NC |
1508 | } |
1509 | } | |
1510 | ||
1511 | /* Compare two target format name strings. | |
1512 | Return a value indicating how "similar" they are. */ | |
08da4cac | 1513 | |
e50d8076 | 1514 | static int |
1579bae1 | 1515 | name_compare (char *first, char *second) |
e50d8076 | 1516 | { |
08da4cac KH |
1517 | char *copy1; |
1518 | char *copy2; | |
1519 | int result; | |
5f992e62 | 1520 | |
e50d8076 NC |
1521 | copy1 = xmalloc (strlen (first) + 1); |
1522 | copy2 = xmalloc (strlen (second) + 1); | |
1523 | ||
1524 | /* Convert the names to lower case. */ | |
1525 | stricpy (copy1, first); | |
1526 | stricpy (copy2, second); | |
1527 | ||
1579bae1 | 1528 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
1529 | strcut (copy1, "big"); |
1530 | strcut (copy1, "little"); | |
1531 | strcut (copy2, "big"); | |
1532 | strcut (copy2, "little"); | |
1533 | ||
1534 | /* Return a value based on how many characters match, | |
1535 | starting from the beginning. If both strings are | |
1536 | the same then return 10 * their length. */ | |
08da4cac KH |
1537 | for (result = 0; copy1[result] == copy2[result]; result++) |
1538 | if (copy1[result] == 0) | |
e50d8076 NC |
1539 | { |
1540 | result *= 10; | |
1541 | break; | |
1542 | } | |
5f992e62 | 1543 | |
e50d8076 NC |
1544 | free (copy1); |
1545 | free (copy2); | |
1546 | ||
1547 | return result; | |
1548 | } | |
1549 | ||
1550 | /* Set by closest_target_match() below. */ | |
08da4cac | 1551 | static const bfd_target *winner; |
e50d8076 NC |
1552 | |
1553 | /* Scan all the valid bfd targets looking for one that has the endianness | |
1554 | requirement that was specified on the command line, and is the nearest | |
1555 | match to the original output target. */ | |
08da4cac | 1556 | |
e50d8076 | 1557 | static int |
1579bae1 | 1558 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 1559 | { |
1579bae1 | 1560 | const bfd_target *original = data; |
5f992e62 | 1561 | |
08da4cac KH |
1562 | if (command_line.endian == ENDIAN_BIG |
1563 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 1564 | return 0; |
5f992e62 | 1565 | |
08da4cac KH |
1566 | if (command_line.endian == ENDIAN_LITTLE |
1567 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
1568 | return 0; |
1569 | ||
1570 | /* Must be the same flavour. */ | |
1571 | if (target->flavour != original->flavour) | |
1572 | return 0; | |
1573 | ||
1574 | /* If we have not found a potential winner yet, then record this one. */ | |
1575 | if (winner == NULL) | |
1576 | { | |
1577 | winner = target; | |
1578 | return 0; | |
1579 | } | |
1580 | ||
1581 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 1582 | Compare their names and choose the better one. */ |
d1778b88 AM |
1583 | if (name_compare (target->name, original->name) |
1584 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
1585 | winner = target; |
1586 | ||
1587 | /* Keep on searching until wqe have checked them all. */ | |
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 1592 | |
e50d8076 | 1593 | static char * |
1579bae1 | 1594 | get_first_input_target (void) |
e50d8076 | 1595 | { |
08da4cac | 1596 | char *target = NULL; |
e50d8076 NC |
1597 | |
1598 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
1599 | { | |
1600 | if (s->header.type == lang_input_statement_enum | |
1601 | && s->real) | |
1602 | { | |
1603 | ldfile_open_file (s); | |
5f992e62 | 1604 | |
e50d8076 NC |
1605 | if (s->the_bfd != NULL |
1606 | && bfd_check_format (s->the_bfd, bfd_object)) | |
1607 | { | |
1608 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 1609 | |
e50d8076 NC |
1610 | if (target != NULL) |
1611 | break; | |
1612 | } | |
1613 | } | |
1614 | } | |
5f992e62 | 1615 | |
e50d8076 NC |
1616 | return target; |
1617 | } | |
1618 | ||
599917b8 | 1619 | const char * |
1579bae1 | 1620 | lang_get_output_target (void) |
599917b8 JJ |
1621 | { |
1622 | const char *target; | |
1623 | ||
1624 | /* Has the user told us which output format to use? */ | |
1579bae1 | 1625 | if (output_target != NULL) |
599917b8 JJ |
1626 | return output_target; |
1627 | ||
1628 | /* No - has the current target been set to something other than | |
1629 | the default? */ | |
1630 | if (current_target != default_target) | |
1631 | return current_target; | |
1632 | ||
1633 | /* No - can we determine the format of the first input file? */ | |
1634 | target = get_first_input_target (); | |
1635 | if (target != NULL) | |
1636 | return target; | |
1637 | ||
1638 | /* Failed - use the default output target. */ | |
1639 | return default_target; | |
1640 | } | |
1641 | ||
252b5132 RH |
1642 | /* Open the output file. */ |
1643 | ||
1644 | static bfd * | |
1579bae1 | 1645 | open_output (const char *name) |
252b5132 | 1646 | { |
08da4cac | 1647 | bfd *output; |
252b5132 | 1648 | |
599917b8 | 1649 | output_target = lang_get_output_target (); |
5f992e62 | 1650 | |
08da4cac KH |
1651 | /* Has the user requested a particular endianness on the command |
1652 | line? */ | |
e50d8076 NC |
1653 | if (command_line.endian != ENDIAN_UNSET) |
1654 | { | |
08da4cac | 1655 | const bfd_target *target; |
1b69a0bf | 1656 | enum bfd_endian desired_endian; |
e50d8076 NC |
1657 | |
1658 | /* Get the chosen target. */ | |
1579bae1 | 1659 | target = bfd_search_for_target (get_target, (void *) output_target); |
e50d8076 | 1660 | |
c13b1b77 NC |
1661 | /* If the target is not supported, we cannot do anything. */ |
1662 | if (target != NULL) | |
e50d8076 | 1663 | { |
c13b1b77 NC |
1664 | if (command_line.endian == ENDIAN_BIG) |
1665 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 1666 | else |
c13b1b77 | 1667 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
1668 | |
1669 | /* See if the target has the wrong endianness. This should | |
1670 | not happen if the linker script has provided big and | |
1671 | little endian alternatives, but some scrips don't do | |
1672 | this. */ | |
c13b1b77 | 1673 | if (target->byteorder != desired_endian) |
e50d8076 | 1674 | { |
c13b1b77 NC |
1675 | /* If it does, then see if the target provides |
1676 | an alternative with the correct endianness. */ | |
1677 | if (target->alternative_target != NULL | |
1678 | && (target->alternative_target->byteorder == desired_endian)) | |
1679 | output_target = target->alternative_target->name; | |
e50d8076 | 1680 | else |
c13b1b77 | 1681 | { |
5f992e62 AM |
1682 | /* Try to find a target as similar as possible to |
1683 | the default target, but which has the desired | |
1684 | endian characteristic. */ | |
1579bae1 AM |
1685 | bfd_search_for_target (closest_target_match, |
1686 | (void *) target); | |
5f992e62 AM |
1687 | |
1688 | /* Oh dear - we could not find any targets that | |
1689 | satisfy our requirements. */ | |
c13b1b77 | 1690 | if (winner == NULL) |
6feb9908 AM |
1691 | einfo (_("%P: warning: could not find any targets" |
1692 | " that match endianness requirement\n")); | |
c13b1b77 NC |
1693 | else |
1694 | output_target = winner->name; | |
1695 | } | |
e50d8076 NC |
1696 | } |
1697 | } | |
252b5132 | 1698 | } |
5f992e62 | 1699 | |
252b5132 RH |
1700 | output = bfd_openw (name, output_target); |
1701 | ||
1579bae1 | 1702 | if (output == NULL) |
252b5132 RH |
1703 | { |
1704 | if (bfd_get_error () == bfd_error_invalid_target) | |
e50d8076 NC |
1705 | einfo (_("%P%F: target %s not found\n"), output_target); |
1706 | ||
252b5132 RH |
1707 | einfo (_("%P%F: cannot open output file %s: %E\n"), name); |
1708 | } | |
1709 | ||
b34976b6 | 1710 | delete_output_file_on_failure = TRUE; |
252b5132 | 1711 | |
08da4cac KH |
1712 | #if 0 |
1713 | output->flags |= D_PAGED; | |
1714 | #endif | |
252b5132 RH |
1715 | |
1716 | if (! bfd_set_format (output, bfd_object)) | |
1717 | einfo (_("%P%F:%s: can not make object file: %E\n"), name); | |
1718 | if (! bfd_set_arch_mach (output, | |
1719 | ldfile_output_architecture, | |
1720 | ldfile_output_machine)) | |
1721 | einfo (_("%P%F:%s: can not set architecture: %E\n"), name); | |
1722 | ||
1723 | link_info.hash = bfd_link_hash_table_create (output); | |
1579bae1 | 1724 | if (link_info.hash == NULL) |
252b5132 RH |
1725 | einfo (_("%P%F: can not create link hash table: %E\n")); |
1726 | ||
1727 | bfd_set_gp_size (output, g_switch_value); | |
1728 | return output; | |
1729 | } | |
1730 | ||
252b5132 | 1731 | static void |
1579bae1 | 1732 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
1733 | { |
1734 | switch (statement->header.type) | |
1735 | { | |
1736 | case lang_output_statement_enum: | |
1579bae1 | 1737 | ASSERT (output_bfd == NULL); |
252b5132 RH |
1738 | output_bfd = open_output (statement->output_statement.name); |
1739 | ldemul_set_output_arch (); | |
1049f94e | 1740 | if (config.magic_demand_paged && !link_info.relocatable) |
252b5132 RH |
1741 | output_bfd->flags |= D_PAGED; |
1742 | else | |
1743 | output_bfd->flags &= ~D_PAGED; | |
1744 | if (config.text_read_only) | |
1745 | output_bfd->flags |= WP_TEXT; | |
1746 | else | |
1747 | output_bfd->flags &= ~WP_TEXT; | |
1748 | if (link_info.traditional_format) | |
1749 | output_bfd->flags |= BFD_TRADITIONAL_FORMAT; | |
1750 | else | |
1751 | output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; | |
1752 | break; | |
1753 | ||
1754 | case lang_target_statement_enum: | |
1755 | current_target = statement->target_statement.target; | |
1756 | break; | |
1757 | default: | |
1758 | break; | |
1759 | } | |
1760 | } | |
1761 | ||
e5caa5e0 AM |
1762 | /* Convert between addresses in bytes and sizes in octets. |
1763 | For currently supported targets, octets_per_byte is always a power | |
1764 | of two, so we can use shifts. */ | |
1765 | #define TO_ADDR(X) ((X) >> opb_shift) | |
1766 | #define TO_SIZE(X) ((X) << opb_shift) | |
1767 | ||
1768 | /* Support the above. */ | |
1769 | static unsigned int opb_shift = 0; | |
1770 | ||
1771 | static void | |
1772 | init_opb (void) | |
1773 | { | |
1774 | unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
1775 | ldfile_output_machine); | |
1776 | opb_shift = 0; | |
1777 | if (x > 1) | |
1778 | while ((x & 1) == 0) | |
1779 | { | |
1780 | x >>= 1; | |
1781 | ++opb_shift; | |
1782 | } | |
1783 | ASSERT (x == 1); | |
1784 | } | |
1785 | ||
252b5132 RH |
1786 | /* Open all the input files. */ |
1787 | ||
1788 | static void | |
1579bae1 | 1789 | open_input_bfds (lang_statement_union_type *s, bfd_boolean force) |
252b5132 | 1790 | { |
1579bae1 | 1791 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
1792 | { |
1793 | switch (s->header.type) | |
1794 | { | |
1795 | case lang_constructors_statement_enum: | |
1796 | open_input_bfds (constructor_list.head, force); | |
1797 | break; | |
1798 | case lang_output_section_statement_enum: | |
1799 | open_input_bfds (s->output_section_statement.children.head, force); | |
1800 | break; | |
1801 | case lang_wild_statement_enum: | |
08da4cac | 1802 | /* Maybe we should load the file's symbols. */ |
252b5132 RH |
1803 | if (s->wild_statement.filename |
1804 | && ! wildcardp (s->wild_statement.filename)) | |
4a43e768 | 1805 | lookup_name (s->wild_statement.filename); |
252b5132 RH |
1806 | open_input_bfds (s->wild_statement.children.head, force); |
1807 | break; | |
1808 | case lang_group_statement_enum: | |
1809 | { | |
1810 | struct bfd_link_hash_entry *undefs; | |
1811 | ||
1812 | /* We must continually search the entries in the group | |
08da4cac KH |
1813 | until no new symbols are added to the list of undefined |
1814 | symbols. */ | |
252b5132 RH |
1815 | |
1816 | do | |
1817 | { | |
1818 | undefs = link_info.hash->undefs_tail; | |
b34976b6 | 1819 | open_input_bfds (s->group_statement.children.head, TRUE); |
252b5132 RH |
1820 | } |
1821 | while (undefs != link_info.hash->undefs_tail); | |
1822 | } | |
1823 | break; | |
1824 | case lang_target_statement_enum: | |
1825 | current_target = s->target_statement.target; | |
1826 | break; | |
1827 | case lang_input_statement_enum: | |
e50d8076 | 1828 | if (s->input_statement.real) |
252b5132 RH |
1829 | { |
1830 | lang_statement_list_type add; | |
1831 | ||
1832 | s->input_statement.target = current_target; | |
1833 | ||
1834 | /* If we are being called from within a group, and this | |
1835 | is an archive which has already been searched, then | |
cd4c806a L |
1836 | force it to be researched unless the whole archive |
1837 | has been loaded already. */ | |
252b5132 | 1838 | if (force |
cd4c806a | 1839 | && !s->input_statement.whole_archive |
252b5132 RH |
1840 | && s->input_statement.loaded |
1841 | && bfd_check_format (s->input_statement.the_bfd, | |
1842 | bfd_archive)) | |
b34976b6 | 1843 | s->input_statement.loaded = FALSE; |
252b5132 | 1844 | |
d1778b88 | 1845 | lang_list_init (&add); |
1276aefa | 1846 | |
6770ec8c | 1847 | if (! load_symbols (&s->input_statement, &add)) |
b34976b6 | 1848 | config.make_executable = FALSE; |
252b5132 RH |
1849 | |
1850 | if (add.head != NULL) | |
1851 | { | |
bba1a0c0 AM |
1852 | *add.tail = s->header.next; |
1853 | s->header.next = add.head; | |
252b5132 RH |
1854 | } |
1855 | } | |
1856 | break; | |
1857 | default: | |
1858 | break; | |
1859 | } | |
1860 | } | |
1861 | } | |
1862 | ||
08da4cac KH |
1863 | /* If there are [COMMONS] statements, put a wild one into the bss |
1864 | section. */ | |
252b5132 RH |
1865 | |
1866 | static void | |
1579bae1 | 1867 | lang_reasonable_defaults (void) |
252b5132 RH |
1868 | { |
1869 | #if 0 | |
1870 | lang_output_section_statement_lookup (".text"); | |
1871 | lang_output_section_statement_lookup (".data"); | |
1872 | ||
08da4cac | 1873 | default_common_section = lang_output_section_statement_lookup (".bss"); |
252b5132 | 1874 | |
7c519c12 | 1875 | if (!placed_commons) |
252b5132 RH |
1876 | { |
1877 | lang_wild_statement_type *new = | |
1878 | new_stat (lang_wild_statement, | |
1879 | &default_common_section->children); | |
1880 | ||
1881 | new->section_name = "COMMON"; | |
1579bae1 | 1882 | new->filename = NULL; |
252b5132 RH |
1883 | lang_list_init (&new->children); |
1884 | } | |
1885 | #endif | |
252b5132 RH |
1886 | } |
1887 | ||
420e579c HPN |
1888 | /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */ |
1889 | ||
1890 | void | |
1891 | lang_track_definedness (const char *name) | |
1892 | { | |
1893 | if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL) | |
1894 | einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name); | |
1895 | } | |
1896 | ||
1897 | /* New-function for the definedness hash table. */ | |
1898 | ||
1899 | static struct bfd_hash_entry * | |
1900 | lang_definedness_newfunc (struct bfd_hash_entry *entry, | |
1901 | struct bfd_hash_table *table ATTRIBUTE_UNUSED, | |
1902 | const char *name ATTRIBUTE_UNUSED) | |
1903 | { | |
1904 | struct lang_definedness_hash_entry *ret | |
1905 | = (struct lang_definedness_hash_entry *) entry; | |
1906 | ||
1907 | if (ret == NULL) | |
1908 | ret = (struct lang_definedness_hash_entry *) | |
1909 | bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry)); | |
1910 | ||
1911 | if (ret == NULL) | |
1912 | einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name); | |
1913 | ||
1914 | ret->iteration = -1; | |
1915 | return &ret->root; | |
1916 | } | |
1917 | ||
1918 | /* Return the iteration when the definition of NAME was last updated. A | |
1919 | value of -1 means that the symbol is not defined in the linker script | |
1920 | or the command line, but may be defined in the linker symbol table. */ | |
1921 | ||
1922 | int | |
1923 | lang_symbol_definition_iteration (const char *name) | |
1924 | { | |
1925 | struct lang_definedness_hash_entry *defentry | |
1926 | = (struct lang_definedness_hash_entry *) | |
1927 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
1928 | ||
1929 | /* We've already created this one on the presence of DEFINED in the | |
1930 | script, so it can't be NULL unless something is borked elsewhere in | |
1931 | the code. */ | |
1932 | if (defentry == NULL) | |
1933 | FAIL (); | |
1934 | ||
1935 | return defentry->iteration; | |
1936 | } | |
1937 | ||
1938 | /* Update the definedness state of NAME. */ | |
1939 | ||
1940 | void | |
1941 | lang_update_definedness (const char *name, struct bfd_link_hash_entry *h) | |
1942 | { | |
1943 | struct lang_definedness_hash_entry *defentry | |
1944 | = (struct lang_definedness_hash_entry *) | |
1945 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
1946 | ||
1947 | /* We don't keep track of symbols not tested with DEFINED. */ | |
1948 | if (defentry == NULL) | |
1949 | return; | |
1950 | ||
1951 | /* If the symbol was already defined, and not from an earlier statement | |
1952 | iteration, don't update the definedness iteration, because that'd | |
1953 | make the symbol seem defined in the linker script at this point, and | |
1954 | it wasn't; it was defined in some object. If we do anyway, DEFINED | |
1955 | would start to yield false before this point and the construct "sym = | |
1956 | DEFINED (sym) ? sym : X;" would change sym to X despite being defined | |
1957 | in an object. */ | |
1958 | if (h->type != bfd_link_hash_undefined | |
1959 | && h->type != bfd_link_hash_common | |
1960 | && h->type != bfd_link_hash_new | |
1961 | && defentry->iteration == -1) | |
1962 | return; | |
1963 | ||
1964 | defentry->iteration = lang_statement_iteration; | |
1965 | } | |
1966 | ||
08da4cac | 1967 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
1968 | This is a two step process as the symbol table doesn't even exist at |
1969 | the time the ld command line is processed. First we put the name | |
1970 | on a list, then, once the output file has been opened, transfer the | |
1971 | name to the symbol table. */ | |
1972 | ||
e3e942e9 | 1973 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 1974 | |
e3e942e9 | 1975 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
1976 | |
1977 | void | |
1579bae1 | 1978 | ldlang_add_undef (const char *const name) |
252b5132 RH |
1979 | { |
1980 | ldlang_undef_chain_list_type *new = | |
1579bae1 | 1981 | stat_alloc (sizeof (ldlang_undef_chain_list_type)); |
252b5132 RH |
1982 | |
1983 | new->next = ldlang_undef_chain_list_head; | |
1984 | ldlang_undef_chain_list_head = new; | |
1985 | ||
d1b2b2dc | 1986 | new->name = xstrdup (name); |
fcf0e35b AM |
1987 | |
1988 | if (output_bfd != NULL) | |
1989 | insert_undefined (new->name); | |
1990 | } | |
1991 | ||
1992 | /* Insert NAME as undefined in the symbol table. */ | |
1993 | ||
1994 | static void | |
1579bae1 | 1995 | insert_undefined (const char *name) |
fcf0e35b AM |
1996 | { |
1997 | struct bfd_link_hash_entry *h; | |
1998 | ||
b34976b6 | 1999 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
1579bae1 | 2000 | if (h == NULL) |
fcf0e35b AM |
2001 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
2002 | if (h->type == bfd_link_hash_new) | |
2003 | { | |
2004 | h->type = bfd_link_hash_undefined; | |
2005 | h->u.undef.abfd = NULL; | |
2006 | bfd_link_add_undef (link_info.hash, h); | |
2007 | } | |
252b5132 RH |
2008 | } |
2009 | ||
2010 | /* Run through the list of undefineds created above and place them | |
2011 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
2012 | script file. */ |
2013 | ||
252b5132 | 2014 | static void |
1579bae1 | 2015 | lang_place_undefineds (void) |
252b5132 RH |
2016 | { |
2017 | ldlang_undef_chain_list_type *ptr; | |
2018 | ||
1579bae1 AM |
2019 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
2020 | insert_undefined (ptr->name); | |
252b5132 RH |
2021 | } |
2022 | ||
0841712e JJ |
2023 | /* Check for all readonly or some readwrite sections. */ |
2024 | ||
2025 | static void | |
6feb9908 AM |
2026 | check_input_sections |
2027 | (lang_statement_union_type *s, | |
2028 | lang_output_section_statement_type *output_section_statement) | |
0841712e JJ |
2029 | { |
2030 | for (; s != (lang_statement_union_type *) NULL; s = s->header.next) | |
2031 | { | |
2032 | switch (s->header.type) | |
2033 | { | |
2034 | case lang_wild_statement_enum: | |
2035 | walk_wild (&s->wild_statement, check_section_callback, | |
2036 | output_section_statement); | |
2037 | if (! output_section_statement->all_input_readonly) | |
2038 | return; | |
2039 | break; | |
2040 | case lang_constructors_statement_enum: | |
2041 | check_input_sections (constructor_list.head, | |
2042 | output_section_statement); | |
2043 | if (! output_section_statement->all_input_readonly) | |
2044 | return; | |
2045 | break; | |
2046 | case lang_group_statement_enum: | |
2047 | check_input_sections (s->group_statement.children.head, | |
2048 | output_section_statement); | |
2049 | if (! output_section_statement->all_input_readonly) | |
2050 | return; | |
2051 | break; | |
2052 | default: | |
2053 | break; | |
2054 | } | |
2055 | } | |
2056 | } | |
2057 | ||
bcaa7b3e L |
2058 | /* Update wildcard statements if needed. */ |
2059 | ||
2060 | static void | |
2061 | update_wild_statements (lang_statement_union_type *s) | |
2062 | { | |
2063 | struct wildcard_list *sec; | |
2064 | ||
2065 | switch (sort_section) | |
2066 | { | |
2067 | default: | |
2068 | FAIL (); | |
2069 | ||
2070 | case none: | |
2071 | break; | |
2072 | ||
2073 | case by_name: | |
2074 | case by_alignment: | |
2075 | for (; s != NULL; s = s->header.next) | |
2076 | { | |
2077 | switch (s->header.type) | |
2078 | { | |
2079 | default: | |
2080 | break; | |
2081 | ||
2082 | case lang_wild_statement_enum: | |
2083 | sec = s->wild_statement.section_list; | |
2084 | if (sec != NULL) | |
2085 | { | |
2086 | switch (sec->spec.sorted) | |
2087 | { | |
2088 | case none: | |
2089 | sec->spec.sorted = sort_section; | |
2090 | break; | |
2091 | case by_name: | |
2092 | if (sort_section == by_alignment) | |
2093 | sec->spec.sorted = by_name_alignment; | |
2094 | break; | |
2095 | case by_alignment: | |
2096 | if (sort_section == by_name) | |
2097 | sec->spec.sorted = by_alignment_name; | |
2098 | break; | |
2099 | default: | |
2100 | break; | |
2101 | } | |
2102 | } | |
2103 | break; | |
2104 | ||
2105 | case lang_constructors_statement_enum: | |
2106 | update_wild_statements (constructor_list.head); | |
2107 | break; | |
2108 | ||
2109 | case lang_output_section_statement_enum: | |
2110 | update_wild_statements | |
2111 | (s->output_section_statement.children.head); | |
2112 | break; | |
2113 | ||
2114 | case lang_group_statement_enum: | |
2115 | update_wild_statements (s->group_statement.children.head); | |
2116 | break; | |
2117 | } | |
2118 | } | |
2119 | break; | |
2120 | } | |
2121 | } | |
2122 | ||
396a2467 | 2123 | /* Open input files and attach to output sections. */ |
08da4cac | 2124 | |
252b5132 | 2125 | static void |
1579bae1 AM |
2126 | map_input_to_output_sections |
2127 | (lang_statement_union_type *s, const char *target, | |
2128 | lang_output_section_statement_type *output_section_statement) | |
252b5132 | 2129 | { |
1579bae1 | 2130 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
2131 | { |
2132 | switch (s->header.type) | |
2133 | { | |
252b5132 | 2134 | case lang_wild_statement_enum: |
b6bf44ba | 2135 | wild (&s->wild_statement, target, output_section_statement); |
abc6ab0a | 2136 | break; |
252b5132 RH |
2137 | case lang_constructors_statement_enum: |
2138 | map_input_to_output_sections (constructor_list.head, | |
2139 | target, | |
2140 | output_section_statement); | |
2141 | break; | |
2142 | case lang_output_section_statement_enum: | |
0841712e JJ |
2143 | if (s->output_section_statement.constraint) |
2144 | { | |
2145 | if (s->output_section_statement.constraint == -1) | |
2146 | break; | |
2147 | s->output_section_statement.all_input_readonly = TRUE; | |
2148 | check_input_sections (s->output_section_statement.children.head, | |
2149 | &s->output_section_statement); | |
2150 | if ((s->output_section_statement.all_input_readonly | |
2151 | && s->output_section_statement.constraint == ONLY_IF_RW) | |
2152 | || (!s->output_section_statement.all_input_readonly | |
2153 | && s->output_section_statement.constraint == ONLY_IF_RO)) | |
2154 | { | |
2155 | s->output_section_statement.constraint = -1; | |
2156 | break; | |
2157 | } | |
2158 | } | |
2159 | ||
252b5132 RH |
2160 | map_input_to_output_sections (s->output_section_statement.children.head, |
2161 | target, | |
2162 | &s->output_section_statement); | |
2163 | break; | |
2164 | case lang_output_statement_enum: | |
2165 | break; | |
2166 | case lang_target_statement_enum: | |
2167 | target = s->target_statement.target; | |
2168 | break; | |
2169 | case lang_group_statement_enum: | |
2170 | map_input_to_output_sections (s->group_statement.children.head, | |
2171 | target, | |
2172 | output_section_statement); | |
2173 | break; | |
384d938f NS |
2174 | case lang_data_statement_enum: |
2175 | /* Make sure that any sections mentioned in the expression | |
2176 | are initialized. */ | |
2177 | exp_init_os (s->data_statement.exp); | |
2178 | /* FALLTHROUGH */ | |
252b5132 RH |
2179 | case lang_fill_statement_enum: |
2180 | case lang_input_section_enum: | |
2181 | case lang_object_symbols_statement_enum: | |
252b5132 RH |
2182 | case lang_reloc_statement_enum: |
2183 | case lang_padding_statement_enum: | |
2184 | case lang_input_statement_enum: | |
2185 | if (output_section_statement != NULL | |
2186 | && output_section_statement->bfd_section == NULL) | |
2187 | init_os (output_section_statement); | |
2188 | break; | |
2189 | case lang_assignment_statement_enum: | |
2190 | if (output_section_statement != NULL | |
2191 | && output_section_statement->bfd_section == NULL) | |
2192 | init_os (output_section_statement); | |
2193 | ||
2194 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 2195 | are initialized. */ |
252b5132 RH |
2196 | exp_init_os (s->assignment_statement.exp); |
2197 | break; | |
2198 | case lang_afile_asection_pair_statement_enum: | |
2199 | FAIL (); | |
2200 | break; | |
2201 | case lang_address_statement_enum: | |
08da4cac | 2202 | /* Mark the specified section with the supplied address. */ |
252b5132 RH |
2203 | { |
2204 | lang_output_section_statement_type *os = | |
2205 | lang_output_section_statement_lookup | |
2206 | (s->address_statement.section_name); | |
2207 | ||
2208 | if (os->bfd_section == NULL) | |
2209 | init_os (os); | |
2210 | os->addr_tree = s->address_statement.address; | |
2211 | } | |
2212 | break; | |
2213 | } | |
2214 | } | |
2215 | } | |
2216 | ||
4bd5a393 AM |
2217 | /* An output section might have been removed after its statement was |
2218 | added. For example, ldemul_before_allocation can remove dynamic | |
2219 | sections if they turn out to be not needed. Clean them up here. */ | |
2220 | ||
2221 | static void | |
1579bae1 | 2222 | strip_excluded_output_sections (void) |
4bd5a393 AM |
2223 | { |
2224 | lang_statement_union_type *u; | |
2225 | ||
2226 | for (u = lang_output_section_statement.head; | |
2227 | u != NULL; | |
2228 | u = u->output_section_statement.next) | |
2229 | { | |
2230 | lang_output_section_statement_type *os; | |
2231 | asection *s; | |
2232 | ||
2233 | os = &u->output_section_statement; | |
0841712e JJ |
2234 | if (os->constraint == -1) |
2235 | continue; | |
4bd5a393 AM |
2236 | s = os->bfd_section; |
2237 | if (s != NULL && (s->flags & SEC_EXCLUDE) != 0) | |
2238 | { | |
2239 | asection **p; | |
2240 | ||
2241 | os->bfd_section = NULL; | |
2242 | ||
2243 | for (p = &output_bfd->sections; *p; p = &(*p)->next) | |
2244 | if (*p == s) | |
2245 | { | |
2246 | bfd_section_list_remove (output_bfd, p); | |
2247 | output_bfd->section_count--; | |
2248 | break; | |
2249 | } | |
2250 | } | |
2251 | } | |
2252 | } | |
2253 | ||
252b5132 | 2254 | static void |
1579bae1 AM |
2255 | print_output_section_statement |
2256 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
2257 | { |
2258 | asection *section = output_section_statement->bfd_section; | |
2259 | int len; | |
2260 | ||
2261 | if (output_section_statement != abs_output_section) | |
2262 | { | |
2263 | minfo ("\n%s", output_section_statement->name); | |
2264 | ||
2265 | if (section != NULL) | |
2266 | { | |
2267 | print_dot = section->vma; | |
2268 | ||
2269 | len = strlen (output_section_statement->name); | |
2270 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
2271 | { | |
2272 | print_nl (); | |
2273 | len = 0; | |
2274 | } | |
2275 | while (len < SECTION_NAME_MAP_LENGTH) | |
2276 | { | |
2277 | print_space (); | |
2278 | ++len; | |
2279 | } | |
2280 | ||
eea6121a | 2281 | minfo ("0x%V %W", section->vma, section->size); |
252b5132 RH |
2282 | |
2283 | if (output_section_statement->load_base != NULL) | |
2284 | { | |
2285 | bfd_vma addr; | |
2286 | ||
2287 | addr = exp_get_abs_int (output_section_statement->load_base, 0, | |
2288 | "load base", lang_final_phase_enum); | |
2289 | minfo (_(" load address 0x%V"), addr); | |
2290 | } | |
2291 | } | |
2292 | ||
2293 | print_nl (); | |
2294 | } | |
2295 | ||
2296 | print_statement_list (output_section_statement->children.head, | |
2297 | output_section_statement); | |
2298 | } | |
2299 | ||
2300 | static void | |
1579bae1 AM |
2301 | print_assignment (lang_assignment_statement_type *assignment, |
2302 | lang_output_section_statement_type *output_section) | |
252b5132 RH |
2303 | { |
2304 | int i; | |
2305 | etree_value_type result; | |
2306 | ||
2307 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
2308 | print_space (); | |
2309 | ||
2310 | result = exp_fold_tree (assignment->exp->assign.src, output_section, | |
2311 | lang_final_phase_enum, print_dot, &print_dot); | |
2312 | if (result.valid_p) | |
7b17f854 RS |
2313 | { |
2314 | const char *dst; | |
2315 | bfd_vma value; | |
2316 | ||
2317 | value = result.value + result.section->bfd_section->vma; | |
2318 | dst = assignment->exp->assign.dst; | |
2319 | ||
2320 | minfo ("0x%V", value); | |
2321 | if (dst[0] == '.' && dst[1] == 0) | |
2322 | print_dot = value; | |
2323 | } | |
252b5132 RH |
2324 | else |
2325 | { | |
2326 | minfo ("*undef* "); | |
2327 | #ifdef BFD64 | |
2328 | minfo (" "); | |
2329 | #endif | |
2330 | } | |
2331 | ||
2332 | minfo (" "); | |
2333 | ||
2334 | exp_print_tree (assignment->exp); | |
2335 | ||
2336 | print_nl (); | |
2337 | } | |
2338 | ||
2339 | static void | |
1579bae1 | 2340 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 2341 | { |
1579bae1 | 2342 | if (statm->filename != NULL) |
252b5132 RH |
2343 | { |
2344 | fprintf (config.map_file, "LOAD %s\n", statm->filename); | |
2345 | } | |
2346 | } | |
2347 | ||
2348 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 2349 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 2350 | |
b34976b6 | 2351 | static bfd_boolean |
1579bae1 | 2352 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 2353 | { |
1579bae1 | 2354 | asection *sec = ptr; |
252b5132 RH |
2355 | |
2356 | if ((hash_entry->type == bfd_link_hash_defined | |
2357 | || hash_entry->type == bfd_link_hash_defweak) | |
2358 | && sec == hash_entry->u.def.section) | |
2359 | { | |
2360 | int i; | |
2361 | ||
2362 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
2363 | print_space (); | |
2364 | minfo ("0x%V ", | |
2365 | (hash_entry->u.def.value | |
2366 | + hash_entry->u.def.section->output_offset | |
2367 | + hash_entry->u.def.section->output_section->vma)); | |
2368 | ||
2369 | minfo (" %T\n", hash_entry->root.string); | |
2370 | } | |
2371 | ||
b34976b6 | 2372 | return TRUE; |
252b5132 RH |
2373 | } |
2374 | ||
35835446 JR |
2375 | static void |
2376 | print_all_symbols (sec) | |
2377 | asection *sec; | |
2378 | { | |
2379 | struct fat_user_section_struct *ud = get_userdata (sec); | |
2380 | struct map_symbol_def *def; | |
2381 | ||
2382 | *ud->map_symbol_def_tail = 0; | |
2383 | for (def = ud->map_symbol_def_head; def; def = def->next) | |
2384 | print_one_symbol (def->entry, sec); | |
2385 | } | |
2386 | ||
252b5132 RH |
2387 | /* Print information about an input section to the map file. */ |
2388 | ||
2389 | static void | |
1579bae1 | 2390 | print_input_section (lang_input_section_type *in) |
252b5132 RH |
2391 | { |
2392 | asection *i = in->section; | |
eea6121a | 2393 | bfd_size_type size = i->size; |
e5caa5e0 AM |
2394 | |
2395 | init_opb (); | |
252b5132 RH |
2396 | if (size != 0) |
2397 | { | |
57ceae94 AM |
2398 | int len; |
2399 | bfd_vma addr; | |
252b5132 | 2400 | |
57ceae94 | 2401 | print_space (); |
252b5132 RH |
2402 | minfo ("%s", i->name); |
2403 | ||
57ceae94 AM |
2404 | len = 1 + strlen (i->name); |
2405 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
2406 | { | |
2407 | print_nl (); | |
2408 | len = 0; | |
2409 | } | |
2410 | while (len < SECTION_NAME_MAP_LENGTH) | |
252b5132 | 2411 | { |
57ceae94 AM |
2412 | print_space (); |
2413 | ++len; | |
2414 | } | |
252b5132 | 2415 | |
57ceae94 AM |
2416 | if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0) |
2417 | addr = i->output_section->vma + i->output_offset; | |
2418 | else | |
2419 | { | |
2420 | addr = print_dot; | |
2421 | size = 0; | |
2422 | } | |
252b5132 | 2423 | |
57ceae94 | 2424 | minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner); |
252b5132 | 2425 | |
eea6121a | 2426 | if (size != i->rawsize && i->rawsize != 0) |
57ceae94 AM |
2427 | { |
2428 | len = SECTION_NAME_MAP_LENGTH + 3; | |
252b5132 | 2429 | #ifdef BFD64 |
57ceae94 | 2430 | len += 16; |
252b5132 | 2431 | #else |
57ceae94 | 2432 | len += 8; |
252b5132 | 2433 | #endif |
57ceae94 AM |
2434 | while (len > 0) |
2435 | { | |
2436 | print_space (); | |
2437 | --len; | |
252b5132 RH |
2438 | } |
2439 | ||
eea6121a | 2440 | minfo (_("%W (size before relaxing)\n"), i->rawsize); |
57ceae94 AM |
2441 | } |
2442 | ||
2443 | if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0) | |
2444 | { | |
35835446 JR |
2445 | if (command_line.reduce_memory_overheads) |
2446 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); | |
2447 | else | |
2448 | print_all_symbols (i); | |
252b5132 | 2449 | |
57ceae94 | 2450 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
2451 | } |
2452 | } | |
2453 | } | |
2454 | ||
2455 | static void | |
1579bae1 | 2456 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 2457 | { |
2c382fb6 AM |
2458 | size_t size; |
2459 | unsigned char *p; | |
2460 | fputs (" FILL mask 0x", config.map_file); | |
2461 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
2462 | fprintf (config.map_file, "%02x", *p); | |
2463 | fputs ("\n", config.map_file); | |
252b5132 RH |
2464 | } |
2465 | ||
2466 | static void | |
1579bae1 | 2467 | print_data_statement (lang_data_statement_type *data) |
252b5132 RH |
2468 | { |
2469 | int i; | |
2470 | bfd_vma addr; | |
2471 | bfd_size_type size; | |
2472 | const char *name; | |
2473 | ||
e5caa5e0 | 2474 | init_opb (); |
252b5132 RH |
2475 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
2476 | print_space (); | |
2477 | ||
2478 | addr = data->output_vma; | |
2479 | if (data->output_section != NULL) | |
2480 | addr += data->output_section->vma; | |
2481 | ||
2482 | switch (data->type) | |
2483 | { | |
2484 | default: | |
2485 | abort (); | |
2486 | case BYTE: | |
2487 | size = BYTE_SIZE; | |
2488 | name = "BYTE"; | |
2489 | break; | |
2490 | case SHORT: | |
2491 | size = SHORT_SIZE; | |
2492 | name = "SHORT"; | |
2493 | break; | |
2494 | case LONG: | |
2495 | size = LONG_SIZE; | |
2496 | name = "LONG"; | |
2497 | break; | |
2498 | case QUAD: | |
2499 | size = QUAD_SIZE; | |
2500 | name = "QUAD"; | |
2501 | break; | |
2502 | case SQUAD: | |
2503 | size = QUAD_SIZE; | |
2504 | name = "SQUAD"; | |
2505 | break; | |
2506 | } | |
2507 | ||
2508 | minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); | |
2509 | ||
2510 | if (data->exp->type.node_class != etree_value) | |
2511 | { | |
2512 | print_space (); | |
2513 | exp_print_tree (data->exp); | |
2514 | } | |
2515 | ||
2516 | print_nl (); | |
2517 | ||
e5caa5e0 | 2518 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
2519 | } |
2520 | ||
2521 | /* Print an address statement. These are generated by options like | |
2522 | -Ttext. */ | |
2523 | ||
2524 | static void | |
1579bae1 | 2525 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
2526 | { |
2527 | minfo (_("Address of section %s set to "), address->section_name); | |
2528 | exp_print_tree (address->address); | |
2529 | print_nl (); | |
2530 | } | |
2531 | ||
2532 | /* Print a reloc statement. */ | |
2533 | ||
2534 | static void | |
1579bae1 | 2535 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 RH |
2536 | { |
2537 | int i; | |
2538 | bfd_vma addr; | |
2539 | bfd_size_type size; | |
2540 | ||
e5caa5e0 | 2541 | init_opb (); |
252b5132 RH |
2542 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
2543 | print_space (); | |
2544 | ||
2545 | addr = reloc->output_vma; | |
2546 | if (reloc->output_section != NULL) | |
2547 | addr += reloc->output_section->vma; | |
2548 | ||
2549 | size = bfd_get_reloc_size (reloc->howto); | |
2550 | ||
2551 | minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); | |
2552 | ||
2553 | if (reloc->name != NULL) | |
2554 | minfo ("%s+", reloc->name); | |
2555 | else | |
2556 | minfo ("%s+", reloc->section->name); | |
2557 | ||
2558 | exp_print_tree (reloc->addend_exp); | |
2559 | ||
2560 | print_nl (); | |
2561 | ||
e5caa5e0 | 2562 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 2563 | } |
252b5132 RH |
2564 | |
2565 | static void | |
1579bae1 | 2566 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
2567 | { |
2568 | int len; | |
2569 | bfd_vma addr; | |
2570 | ||
e5caa5e0 | 2571 | init_opb (); |
252b5132 RH |
2572 | minfo (" *fill*"); |
2573 | ||
2574 | len = sizeof " *fill*" - 1; | |
2575 | while (len < SECTION_NAME_MAP_LENGTH) | |
2576 | { | |
2577 | print_space (); | |
2578 | ++len; | |
2579 | } | |
2580 | ||
2581 | addr = s->output_offset; | |
2582 | if (s->output_section != NULL) | |
2583 | addr += s->output_section->vma; | |
5f9b8920 | 2584 | minfo ("0x%V %W ", addr, (bfd_vma) s->size); |
252b5132 | 2585 | |
2c382fb6 AM |
2586 | if (s->fill->size != 0) |
2587 | { | |
2588 | size_t size; | |
2589 | unsigned char *p; | |
2590 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
2591 | fprintf (config.map_file, "%02x", *p); | |
2592 | } | |
252b5132 RH |
2593 | |
2594 | print_nl (); | |
2595 | ||
e5caa5e0 | 2596 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
2597 | } |
2598 | ||
2599 | static void | |
1579bae1 AM |
2600 | print_wild_statement (lang_wild_statement_type *w, |
2601 | lang_output_section_statement_type *os) | |
252b5132 | 2602 | { |
b6bf44ba AM |
2603 | struct wildcard_list *sec; |
2604 | ||
252b5132 RH |
2605 | print_space (); |
2606 | ||
2607 | if (w->filenames_sorted) | |
2608 | minfo ("SORT("); | |
08da4cac | 2609 | if (w->filename != NULL) |
252b5132 RH |
2610 | minfo ("%s", w->filename); |
2611 | else | |
2612 | minfo ("*"); | |
2613 | if (w->filenames_sorted) | |
2614 | minfo (")"); | |
2615 | ||
2616 | minfo ("("); | |
b6bf44ba AM |
2617 | for (sec = w->section_list; sec; sec = sec->next) |
2618 | { | |
2619 | if (sec->spec.sorted) | |
2620 | minfo ("SORT("); | |
2621 | if (sec->spec.exclude_name_list != NULL) | |
2622 | { | |
2623 | name_list *tmp; | |
34786259 | 2624 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 2625 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
2626 | minfo (" %s", tmp->name); |
2627 | minfo (") "); | |
b6bf44ba AM |
2628 | } |
2629 | if (sec->spec.name != NULL) | |
2630 | minfo ("%s", sec->spec.name); | |
2631 | else | |
2632 | minfo ("*"); | |
2633 | if (sec->spec.sorted) | |
2634 | minfo (")"); | |
34786259 AM |
2635 | if (sec->next) |
2636 | minfo (" "); | |
b6bf44ba | 2637 | } |
252b5132 RH |
2638 | minfo (")"); |
2639 | ||
2640 | print_nl (); | |
2641 | ||
2642 | print_statement_list (w->children.head, os); | |
2643 | } | |
2644 | ||
2645 | /* Print a group statement. */ | |
2646 | ||
2647 | static void | |
1579bae1 AM |
2648 | print_group (lang_group_statement_type *s, |
2649 | lang_output_section_statement_type *os) | |
252b5132 RH |
2650 | { |
2651 | fprintf (config.map_file, "START GROUP\n"); | |
2652 | print_statement_list (s->children.head, os); | |
2653 | fprintf (config.map_file, "END GROUP\n"); | |
2654 | } | |
2655 | ||
2656 | /* Print the list of statements in S. | |
2657 | This can be called for any statement type. */ | |
2658 | ||
2659 | static void | |
1579bae1 AM |
2660 | print_statement_list (lang_statement_union_type *s, |
2661 | lang_output_section_statement_type *os) | |
252b5132 RH |
2662 | { |
2663 | while (s != NULL) | |
2664 | { | |
2665 | print_statement (s, os); | |
bba1a0c0 | 2666 | s = s->header.next; |
252b5132 RH |
2667 | } |
2668 | } | |
2669 | ||
2670 | /* Print the first statement in statement list S. | |
2671 | This can be called for any statement type. */ | |
2672 | ||
2673 | static void | |
1579bae1 AM |
2674 | print_statement (lang_statement_union_type *s, |
2675 | lang_output_section_statement_type *os) | |
252b5132 RH |
2676 | { |
2677 | switch (s->header.type) | |
2678 | { | |
2679 | default: | |
2680 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
2681 | FAIL (); | |
2682 | break; | |
2683 | case lang_constructors_statement_enum: | |
2684 | if (constructor_list.head != NULL) | |
2685 | { | |
2686 | if (constructors_sorted) | |
2687 | minfo (" SORT (CONSTRUCTORS)\n"); | |
2688 | else | |
2689 | minfo (" CONSTRUCTORS\n"); | |
2690 | print_statement_list (constructor_list.head, os); | |
2691 | } | |
2692 | break; | |
2693 | case lang_wild_statement_enum: | |
2694 | print_wild_statement (&s->wild_statement, os); | |
2695 | break; | |
2696 | case lang_address_statement_enum: | |
2697 | print_address_statement (&s->address_statement); | |
2698 | break; | |
2699 | case lang_object_symbols_statement_enum: | |
2700 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
2701 | break; | |
2702 | case lang_fill_statement_enum: | |
2703 | print_fill_statement (&s->fill_statement); | |
2704 | break; | |
2705 | case lang_data_statement_enum: | |
2706 | print_data_statement (&s->data_statement); | |
2707 | break; | |
2708 | case lang_reloc_statement_enum: | |
2709 | print_reloc_statement (&s->reloc_statement); | |
2710 | break; | |
2711 | case lang_input_section_enum: | |
2712 | print_input_section (&s->input_section); | |
2713 | break; | |
2714 | case lang_padding_statement_enum: | |
2715 | print_padding_statement (&s->padding_statement); | |
2716 | break; | |
2717 | case lang_output_section_statement_enum: | |
2718 | print_output_section_statement (&s->output_section_statement); | |
2719 | break; | |
2720 | case lang_assignment_statement_enum: | |
2721 | print_assignment (&s->assignment_statement, os); | |
2722 | break; | |
2723 | case lang_target_statement_enum: | |
2724 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
2725 | break; | |
2726 | case lang_output_statement_enum: | |
2727 | minfo ("OUTPUT(%s", s->output_statement.name); | |
2728 | if (output_target != NULL) | |
2729 | minfo (" %s", output_target); | |
2730 | minfo (")\n"); | |
2731 | break; | |
2732 | case lang_input_statement_enum: | |
2733 | print_input_statement (&s->input_statement); | |
2734 | break; | |
2735 | case lang_group_statement_enum: | |
2736 | print_group (&s->group_statement, os); | |
2737 | break; | |
2738 | case lang_afile_asection_pair_statement_enum: | |
2739 | FAIL (); | |
2740 | break; | |
2741 | } | |
2742 | } | |
2743 | ||
2744 | static void | |
1579bae1 | 2745 | print_statements (void) |
252b5132 RH |
2746 | { |
2747 | print_statement_list (statement_list.head, abs_output_section); | |
2748 | } | |
2749 | ||
2750 | /* Print the first N statements in statement list S to STDERR. | |
2751 | If N == 0, nothing is printed. | |
2752 | If N < 0, the entire list is printed. | |
2753 | Intended to be called from GDB. */ | |
2754 | ||
2755 | void | |
1579bae1 | 2756 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
2757 | { |
2758 | FILE *map_save = config.map_file; | |
2759 | ||
2760 | config.map_file = stderr; | |
2761 | ||
2762 | if (n < 0) | |
2763 | print_statement_list (s, abs_output_section); | |
2764 | else | |
2765 | { | |
2766 | while (s && --n >= 0) | |
2767 | { | |
2768 | print_statement (s, abs_output_section); | |
bba1a0c0 | 2769 | s = s->header.next; |
252b5132 RH |
2770 | } |
2771 | } | |
2772 | ||
2773 | config.map_file = map_save; | |
2774 | } | |
2775 | ||
b3327aad | 2776 | static void |
1579bae1 AM |
2777 | insert_pad (lang_statement_union_type **ptr, |
2778 | fill_type *fill, | |
2779 | unsigned int alignment_needed, | |
2780 | asection *output_section, | |
2781 | bfd_vma dot) | |
252b5132 | 2782 | { |
2c382fb6 | 2783 | static fill_type zero_fill = { 1, { 0 } }; |
b3327aad | 2784 | lang_statement_union_type *pad; |
b3327aad | 2785 | |
c0c330a7 | 2786 | pad = ((lang_statement_union_type *) |
2af02257 | 2787 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); |
b3327aad | 2788 | if (ptr != &statement_list.head |
2af02257 | 2789 | && pad->header.type == lang_padding_statement_enum |
b3327aad | 2790 | && pad->padding_statement.output_section == output_section) |
252b5132 | 2791 | { |
b3327aad AM |
2792 | /* Use the existing pad statement. The above test on output |
2793 | section is probably redundant, but it doesn't hurt to check. */ | |
252b5132 | 2794 | } |
b3327aad | 2795 | else |
252b5132 | 2796 | { |
b3327aad | 2797 | /* Make a new padding statement, linked into existing chain. */ |
1579bae1 | 2798 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
2799 | pad->header.next = *ptr; |
2800 | *ptr = pad; | |
2801 | pad->header.type = lang_padding_statement_enum; | |
2802 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 2803 | if (fill == NULL) |
2c382fb6 | 2804 | fill = &zero_fill; |
b3327aad | 2805 | pad->padding_statement.fill = fill; |
252b5132 | 2806 | } |
b3327aad AM |
2807 | pad->padding_statement.output_offset = dot - output_section->vma; |
2808 | pad->padding_statement.size = alignment_needed; | |
eea6121a | 2809 | output_section->size += alignment_needed; |
252b5132 RH |
2810 | } |
2811 | ||
08da4cac KH |
2812 | /* Work out how much this section will move the dot point. */ |
2813 | ||
252b5132 | 2814 | static bfd_vma |
6feb9908 AM |
2815 | size_input_section |
2816 | (lang_statement_union_type **this_ptr, | |
2817 | lang_output_section_statement_type *output_section_statement, | |
2818 | fill_type *fill, | |
2819 | bfd_vma dot) | |
252b5132 RH |
2820 | { |
2821 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
2822 | asection *i = is->section; | |
2823 | ||
57ceae94 | 2824 | if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0) |
252b5132 | 2825 | { |
b3327aad AM |
2826 | unsigned int alignment_needed; |
2827 | asection *o; | |
2828 | ||
2829 | /* Align this section first to the input sections requirement, | |
2830 | then to the output section's requirement. If this alignment | |
2831 | is greater than any seen before, then record it too. Perform | |
2832 | the alignment by inserting a magic 'padding' statement. */ | |
2833 | ||
252b5132 | 2834 | if (output_section_statement->subsection_alignment != -1) |
b3327aad AM |
2835 | i->alignment_power = output_section_statement->subsection_alignment; |
2836 | ||
2837 | o = output_section_statement->bfd_section; | |
2838 | if (o->alignment_power < i->alignment_power) | |
2839 | o->alignment_power = i->alignment_power; | |
252b5132 | 2840 | |
b3327aad AM |
2841 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
2842 | ||
2843 | if (alignment_needed != 0) | |
2844 | { | |
e5caa5e0 | 2845 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
2846 | dot += alignment_needed; |
2847 | } | |
252b5132 | 2848 | |
08da4cac | 2849 | /* Remember where in the output section this input section goes. */ |
252b5132 | 2850 | |
b3327aad | 2851 | i->output_offset = dot - o->vma; |
252b5132 | 2852 | |
08da4cac | 2853 | /* Mark how big the output section must be to contain this now. */ |
eea6121a AM |
2854 | dot += TO_ADDR (i->size); |
2855 | o->size = TO_SIZE (dot - o->vma); | |
252b5132 RH |
2856 | } |
2857 | else | |
2858 | { | |
2859 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
2860 | } | |
2861 | ||
2862 | return dot; | |
2863 | } | |
2864 | ||
eea6121a | 2865 | #define IGNORE_SECTION(s) \ |
6feb9908 AM |
2866 | (((s->flags & SEC_THREAD_LOCAL) != 0 \ |
2867 | ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \ | |
2868 | : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \ | |
eea6121a | 2869 | || s->size == 0) |
d1778b88 | 2870 | |
252b5132 RH |
2871 | /* Check to see if any allocated sections overlap with other allocated |
2872 | sections. This can happen when the linker script specifically specifies | |
2873 | the output section addresses of the two sections. */ | |
08da4cac | 2874 | |
252b5132 | 2875 | static void |
1579bae1 | 2876 | lang_check_section_addresses (void) |
252b5132 | 2877 | { |
08da4cac | 2878 | asection *s; |
252b5132 RH |
2879 | |
2880 | /* Scan all sections in the output list. */ | |
2881 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
33275c22 | 2882 | { |
08da4cac | 2883 | asection *os; |
5f992e62 | 2884 | |
33275c22 | 2885 | /* Ignore sections which are not loaded or which have no contents. */ |
eea6121a | 2886 | if (IGNORE_SECTION (s)) |
33275c22 | 2887 | continue; |
5f992e62 | 2888 | |
33275c22 NC |
2889 | /* Once we reach section 's' stop our seach. This prevents two |
2890 | warning messages from being produced, one for 'section A overlaps | |
2891 | section B' and one for 'section B overlaps section A'. */ | |
2892 | for (os = output_bfd->sections; os != s; os = os->next) | |
2893 | { | |
2894 | bfd_vma s_start; | |
2895 | bfd_vma s_end; | |
2896 | bfd_vma os_start; | |
2897 | bfd_vma os_end; | |
5f992e62 | 2898 | |
33275c22 | 2899 | /* Only consider loadable sections with real contents. */ |
eea6121a | 2900 | if (IGNORE_SECTION (os)) |
33275c22 | 2901 | continue; |
252b5132 | 2902 | |
33275c22 NC |
2903 | /* We must check the sections' LMA addresses not their |
2904 | VMA addresses because overlay sections can have | |
2905 | overlapping VMAs but they must have distinct LMAs. */ | |
e5caa5e0 | 2906 | s_start = bfd_section_lma (output_bfd, s); |
33275c22 | 2907 | os_start = bfd_section_lma (output_bfd, os); |
eea6121a AM |
2908 | s_end = s_start + TO_ADDR (s->size) - 1; |
2909 | os_end = os_start + TO_ADDR (os->size) - 1; | |
5f992e62 | 2910 | |
33275c22 NC |
2911 | /* Look for an overlap. */ |
2912 | if ((s_end < os_start) || (s_start > os_end)) | |
2913 | continue; | |
5f992e62 | 2914 | |
33275c22 | 2915 | einfo ( |
252b5132 | 2916 | _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"), |
33275c22 | 2917 | s->name, s_start, s_end, os->name, os_start, os_end); |
5f992e62 | 2918 | |
33275c22 NC |
2919 | /* Once we have found one overlap for this section, |
2920 | stop looking for others. */ | |
2921 | break; | |
2922 | } | |
2923 | } | |
252b5132 RH |
2924 | } |
2925 | ||
562d3460 TW |
2926 | /* Make sure the new address is within the region. We explicitly permit the |
2927 | current address to be at the exact end of the region when the address is | |
2928 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 2929 | calculation wraps around. */ |
562d3460 TW |
2930 | |
2931 | static void | |
1579bae1 | 2932 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 2933 | lang_memory_region_type *region, |
1579bae1 AM |
2934 | etree_type *tree, |
2935 | bfd_vma base) | |
562d3460 TW |
2936 | { |
2937 | if ((region->current < region->origin | |
2938 | || (region->current - region->origin > region->length)) | |
2939 | && ((region->current != region->origin + region->length) | |
b7a26f91 | 2940 | || base == 0)) |
562d3460 | 2941 | { |
1579bae1 | 2942 | if (tree != NULL) |
b7a26f91 | 2943 | { |
6feb9908 AM |
2944 | einfo (_("%X%P: address 0x%v of %B section %s" |
2945 | " is not within region %s\n"), | |
b7a26f91 KH |
2946 | region->current, |
2947 | os->bfd_section->owner, | |
2948 | os->bfd_section->name, | |
2949 | region->name); | |
2950 | } | |
562d3460 | 2951 | else |
b7a26f91 KH |
2952 | { |
2953 | einfo (_("%X%P: region %s is full (%B section %s)\n"), | |
2954 | region->name, | |
2955 | os->bfd_section->owner, | |
2956 | os->bfd_section->name); | |
2957 | } | |
562d3460 TW |
2958 | /* Reset the region pointer. */ |
2959 | region->current = region->origin; | |
2960 | } | |
2961 | } | |
2962 | ||
252b5132 RH |
2963 | /* Set the sizes for all the output sections. */ |
2964 | ||
2d20f7bf | 2965 | static bfd_vma |
1579bae1 AM |
2966 | lang_size_sections_1 |
2967 | (lang_statement_union_type *s, | |
2968 | lang_output_section_statement_type *output_section_statement, | |
2969 | lang_statement_union_type **prev, | |
2970 | fill_type *fill, | |
2971 | bfd_vma dot, | |
2972 | bfd_boolean *relax, | |
2973 | bfd_boolean check_regions) | |
252b5132 RH |
2974 | { |
2975 | /* Size up the sections from their constituent parts. */ | |
1579bae1 | 2976 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
2977 | { |
2978 | switch (s->header.type) | |
2979 | { | |
2980 | case lang_output_section_statement_enum: | |
2981 | { | |
2982 | bfd_vma after; | |
d1778b88 | 2983 | lang_output_section_statement_type *os; |
252b5132 | 2984 | |
d1778b88 | 2985 | os = &s->output_section_statement; |
252b5132 RH |
2986 | if (os->bfd_section == NULL) |
2987 | /* This section was never actually created. */ | |
2988 | break; | |
2989 | ||
2990 | /* If this is a COFF shared library section, use the size and | |
2991 | address from the input section. FIXME: This is COFF | |
2992 | specific; it would be cleaner if there were some other way | |
2993 | to do this, but nothing simple comes to mind. */ | |
2994 | if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) | |
2995 | { | |
08da4cac | 2996 | asection *input; |
252b5132 RH |
2997 | |
2998 | if (os->children.head == NULL | |
bba1a0c0 | 2999 | || os->children.head->header.next != NULL |
6feb9908 AM |
3000 | || (os->children.head->header.type |
3001 | != lang_input_section_enum)) | |
3002 | einfo (_("%P%X: Internal error on COFF shared library" | |
3003 | " section %s\n"), os->name); | |
252b5132 RH |
3004 | |
3005 | input = os->children.head->input_section.section; | |
3006 | bfd_set_section_vma (os->bfd_section->owner, | |
3007 | os->bfd_section, | |
3008 | bfd_section_vma (input->owner, input)); | |
eea6121a | 3009 | os->bfd_section->size = input->size; |
252b5132 RH |
3010 | break; |
3011 | } | |
3012 | ||
3013 | if (bfd_is_abs_section (os->bfd_section)) | |
3014 | { | |
3015 | /* No matter what happens, an abs section starts at zero. */ | |
3016 | ASSERT (os->bfd_section->vma == 0); | |
3017 | } | |
3018 | else | |
3019 | { | |
1579bae1 | 3020 | if (os->addr_tree == NULL) |
252b5132 RH |
3021 | { |
3022 | /* No address specified for this section, get one | |
3023 | from the region specification. */ | |
1579bae1 | 3024 | if (os->region == NULL |
6feb9908 | 3025 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
252b5132 | 3026 | && os->region->name[0] == '*' |
6feb9908 AM |
3027 | && strcmp (os->region->name, |
3028 | DEFAULT_MEMORY_REGION) == 0)) | |
252b5132 RH |
3029 | { |
3030 | os->region = lang_memory_default (os->bfd_section); | |
3031 | } | |
3032 | ||
3033 | /* If a loadable section is using the default memory | |
3034 | region, and some non default memory regions were | |
66184979 | 3035 | defined, issue an error message. */ |
eea6121a | 3036 | if (!IGNORE_SECTION (os->bfd_section) |
1049f94e | 3037 | && ! link_info.relocatable |
cf888e70 | 3038 | && check_regions |
6feb9908 AM |
3039 | && strcmp (os->region->name, |
3040 | DEFAULT_MEMORY_REGION) == 0 | |
252b5132 | 3041 | && lang_memory_region_list != NULL |
d1778b88 | 3042 | && (strcmp (lang_memory_region_list->name, |
a747ee4d | 3043 | DEFAULT_MEMORY_REGION) != 0 |
252b5132 | 3044 | || lang_memory_region_list->next != NULL)) |
66184979 NC |
3045 | { |
3046 | /* By default this is an error rather than just a | |
3047 | warning because if we allocate the section to the | |
3048 | default memory region we can end up creating an | |
07f3b6ad KH |
3049 | excessively large binary, or even seg faulting when |
3050 | attempting to perform a negative seek. See | |
6feb9908 | 3051 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
3052 | for an example of this. This behaviour can be |
3053 | overridden by the using the --no-check-sections | |
3054 | switch. */ | |
3055 | if (command_line.check_section_addresses) | |
6feb9908 AM |
3056 | einfo (_("%P%F: error: no memory region specified" |
3057 | " for loadable section `%s'\n"), | |
66184979 NC |
3058 | bfd_get_section_name (output_bfd, |
3059 | os->bfd_section)); | |
3060 | else | |
6feb9908 AM |
3061 | einfo (_("%P: warning: no memory region specified" |
3062 | " for loadable section `%s'\n"), | |
66184979 NC |
3063 | bfd_get_section_name (output_bfd, |
3064 | os->bfd_section)); | |
3065 | } | |
252b5132 RH |
3066 | |
3067 | dot = os->region->current; | |
5f992e62 | 3068 | |
252b5132 RH |
3069 | if (os->section_alignment == -1) |
3070 | { | |
3071 | bfd_vma olddot; | |
3072 | ||
3073 | olddot = dot; | |
d1778b88 AM |
3074 | dot = align_power (dot, |
3075 | os->bfd_section->alignment_power); | |
252b5132 RH |
3076 | |
3077 | if (dot != olddot && config.warn_section_align) | |
6feb9908 AM |
3078 | einfo (_("%P: warning: changing start of section" |
3079 | " %s by %u bytes\n"), | |
252b5132 RH |
3080 | os->name, (unsigned int) (dot - olddot)); |
3081 | } | |
3082 | } | |
3083 | else | |
3084 | { | |
3085 | etree_value_type r; | |
3086 | ||
1b493742 | 3087 | os->processed = -1; |
252b5132 RH |
3088 | r = exp_fold_tree (os->addr_tree, |
3089 | abs_output_section, | |
3090 | lang_allocating_phase_enum, | |
3091 | dot, &dot); | |
1b493742 NS |
3092 | os->processed = 0; |
3093 | ||
7c519c12 | 3094 | if (!r.valid_p) |
6feb9908 AM |
3095 | einfo (_("%F%S: non constant or forward reference" |
3096 | " address expression for section %s\n"), | |
7c519c12 AM |
3097 | os->name); |
3098 | ||
252b5132 RH |
3099 | dot = r.value + r.section->bfd_section->vma; |
3100 | } | |
5f992e62 | 3101 | |
252b5132 RH |
3102 | /* The section starts here. |
3103 | First, align to what the section needs. */ | |
3104 | ||
3105 | if (os->section_alignment != -1) | |
3106 | dot = align_power (dot, os->section_alignment); | |
3107 | ||
3108 | bfd_set_section_vma (0, os->bfd_section, dot); | |
5f992e62 | 3109 | |
252b5132 RH |
3110 | os->bfd_section->output_offset = 0; |
3111 | } | |
3112 | ||
2d20f7bf | 3113 | lang_size_sections_1 (os->children.head, os, &os->children.head, |
cf888e70 | 3114 | os->fill, dot, relax, check_regions); |
5f992e62 | 3115 | |
08da4cac KH |
3116 | /* Put the section within the requested block size, or |
3117 | align at the block boundary. */ | |
e5caa5e0 | 3118 | after = ((os->bfd_section->vma |
eea6121a | 3119 | + TO_ADDR (os->bfd_section->size) |
e5caa5e0 AM |
3120 | + os->block_value - 1) |
3121 | & - (bfd_vma) os->block_value); | |
252b5132 RH |
3122 | |
3123 | if (bfd_is_abs_section (os->bfd_section)) | |
3124 | ASSERT (after == os->bfd_section->vma); | |
3125 | else | |
eea6121a | 3126 | os->bfd_section->size |
e5caa5e0 | 3127 | = TO_SIZE (after - os->bfd_section->vma); |
9f88b410 | 3128 | |
e5caec89 NS |
3129 | dot = os->bfd_section->vma; |
3130 | /* .tbss sections effectively have zero size. */ | |
3131 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
3132 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
3133 | || link_info.relocatable) | |
eea6121a | 3134 | dot += TO_ADDR (os->bfd_section->size); |
e5caec89 | 3135 | |
1b493742 | 3136 | os->processed = 1; |
252b5132 | 3137 | |
9f88b410 RS |
3138 | if (os->update_dot_tree != 0) |
3139 | exp_fold_tree (os->update_dot_tree, abs_output_section, | |
3140 | lang_allocating_phase_enum, dot, &dot); | |
3141 | ||
252b5132 RH |
3142 | /* Update dot in the region ? |
3143 | We only do this if the section is going to be allocated, | |
3144 | since unallocated sections do not contribute to the region's | |
13392b77 | 3145 | overall size in memory. |
5f992e62 | 3146 | |
cce4c4c5 NC |
3147 | If the SEC_NEVER_LOAD bit is not set, it will affect the |
3148 | addresses of sections after it. We have to update | |
3149 | dot. */ | |
1579bae1 | 3150 | if (os->region != NULL |
6feb9908 AM |
3151 | && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0 |
3152 | || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))) | |
252b5132 RH |
3153 | { |
3154 | os->region->current = dot; | |
5f992e62 | 3155 | |
cf888e70 NC |
3156 | if (check_regions) |
3157 | /* Make sure the new address is within the region. */ | |
3158 | os_region_check (os, os->region, os->addr_tree, | |
3159 | os->bfd_section->vma); | |
08da4cac KH |
3160 | |
3161 | /* If there's no load address specified, use the run | |
3162 | region as the load region. */ | |
3163 | if (os->lma_region == NULL && os->load_base == NULL) | |
3164 | os->lma_region = os->region; | |
3165 | ||
ee3cc2e2 | 3166 | if (os->lma_region != NULL && os->lma_region != os->region) |
08da4cac | 3167 | { |
ee3cc2e2 RS |
3168 | /* Set load_base, which will be handled later. */ |
3169 | os->load_base = exp_intop (os->lma_region->current); | |
3170 | os->lma_region->current += | |
eea6121a | 3171 | TO_ADDR (os->bfd_section->size); |
cf888e70 NC |
3172 | if (check_regions) |
3173 | os_region_check (os, os->lma_region, NULL, | |
3174 | os->bfd_section->lma); | |
08da4cac | 3175 | } |
252b5132 RH |
3176 | } |
3177 | } | |
3178 | break; | |
3179 | ||
3180 | case lang_constructors_statement_enum: | |
2d20f7bf JJ |
3181 | dot = lang_size_sections_1 (constructor_list.head, |
3182 | output_section_statement, | |
3183 | &s->wild_statement.children.head, | |
cf888e70 | 3184 | fill, dot, relax, check_regions); |
252b5132 RH |
3185 | break; |
3186 | ||
3187 | case lang_data_statement_enum: | |
3188 | { | |
3189 | unsigned int size = 0; | |
3190 | ||
08da4cac KH |
3191 | s->data_statement.output_vma = |
3192 | dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
3193 | s->data_statement.output_section = |
3194 | output_section_statement->bfd_section; | |
3195 | ||
1b493742 NS |
3196 | /* We might refer to provided symbols in the expression, and |
3197 | need to mark them as needed. */ | |
3198 | exp_fold_tree (s->data_statement.exp, abs_output_section, | |
3199 | lang_allocating_phase_enum, dot, &dot); | |
3200 | ||
252b5132 RH |
3201 | switch (s->data_statement.type) |
3202 | { | |
08da4cac KH |
3203 | default: |
3204 | abort (); | |
252b5132 RH |
3205 | case QUAD: |
3206 | case SQUAD: | |
3207 | size = QUAD_SIZE; | |
3208 | break; | |
3209 | case LONG: | |
3210 | size = LONG_SIZE; | |
3211 | break; | |
3212 | case SHORT: | |
3213 | size = SHORT_SIZE; | |
3214 | break; | |
3215 | case BYTE: | |
3216 | size = BYTE_SIZE; | |
3217 | break; | |
3218 | } | |
e5caa5e0 AM |
3219 | if (size < TO_SIZE ((unsigned) 1)) |
3220 | size = TO_SIZE ((unsigned) 1); | |
3221 | dot += TO_ADDR (size); | |
eea6121a | 3222 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
3223 | /* The output section gets contents, and then we inspect for |
3224 | any flags set in the input script which override any ALLOC. */ | |
3225 | output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS; | |
08da4cac KH |
3226 | if (!(output_section_statement->flags & SEC_NEVER_LOAD)) |
3227 | { | |
3228 | output_section_statement->bfd_section->flags |= | |
3229 | SEC_ALLOC | SEC_LOAD; | |
3230 | } | |
252b5132 RH |
3231 | } |
3232 | break; | |
3233 | ||
3234 | case lang_reloc_statement_enum: | |
3235 | { | |
3236 | int size; | |
3237 | ||
3238 | s->reloc_statement.output_vma = | |
3239 | dot - output_section_statement->bfd_section->vma; | |
3240 | s->reloc_statement.output_section = | |
3241 | output_section_statement->bfd_section; | |
3242 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 3243 | dot += TO_ADDR (size); |
eea6121a | 3244 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
3245 | } |
3246 | break; | |
5f992e62 | 3247 | |
252b5132 RH |
3248 | case lang_wild_statement_enum: |
3249 | ||
2d20f7bf JJ |
3250 | dot = lang_size_sections_1 (s->wild_statement.children.head, |
3251 | output_section_statement, | |
3252 | &s->wild_statement.children.head, | |
cf888e70 | 3253 | fill, dot, relax, check_regions); |
252b5132 RH |
3254 | |
3255 | break; | |
3256 | ||
3257 | case lang_object_symbols_statement_enum: | |
3258 | link_info.create_object_symbols_section = | |
3259 | output_section_statement->bfd_section; | |
3260 | break; | |
3261 | case lang_output_statement_enum: | |
3262 | case lang_target_statement_enum: | |
3263 | break; | |
3264 | case lang_input_section_enum: | |
3265 | { | |
3266 | asection *i; | |
3267 | ||
3268 | i = (*prev)->input_section.section; | |
eea6121a | 3269 | if (relax) |
252b5132 | 3270 | { |
b34976b6 | 3271 | bfd_boolean again; |
252b5132 RH |
3272 | |
3273 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) | |
3274 | einfo (_("%P%F: can't relax section: %E\n")); | |
3275 | if (again) | |
b34976b6 | 3276 | *relax = TRUE; |
252b5132 | 3277 | } |
b3327aad AM |
3278 | dot = size_input_section (prev, output_section_statement, |
3279 | output_section_statement->fill, dot); | |
252b5132 RH |
3280 | } |
3281 | break; | |
3282 | case lang_input_statement_enum: | |
3283 | break; | |
3284 | case lang_fill_statement_enum: | |
08da4cac KH |
3285 | s->fill_statement.output_section = |
3286 | output_section_statement->bfd_section; | |
252b5132 RH |
3287 | |
3288 | fill = s->fill_statement.fill; | |
3289 | break; | |
3290 | case lang_assignment_statement_enum: | |
3291 | { | |
3292 | bfd_vma newdot = dot; | |
3293 | ||
3294 | exp_fold_tree (s->assignment_statement.exp, | |
3295 | output_section_statement, | |
3296 | lang_allocating_phase_enum, | |
3297 | dot, | |
3298 | &newdot); | |
3299 | ||
3300 | if (newdot != dot) | |
3301 | { | |
252b5132 RH |
3302 | if (output_section_statement == abs_output_section) |
3303 | { | |
3304 | /* If we don't have an output section, then just adjust | |
3305 | the default memory address. */ | |
6feb9908 AM |
3306 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
3307 | FALSE)->current = newdot; | |
252b5132 | 3308 | } |
b3327aad | 3309 | else |
252b5132 | 3310 | { |
b3327aad AM |
3311 | /* Insert a pad after this statement. We can't |
3312 | put the pad before when relaxing, in case the | |
3313 | assignment references dot. */ | |
e5caa5e0 | 3314 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
3315 | output_section_statement->bfd_section, dot); |
3316 | ||
3317 | /* Don't neuter the pad below when relaxing. */ | |
3318 | s = s->header.next; | |
252b5132 RH |
3319 | } |
3320 | ||
9dfc8ab2 NC |
3321 | /* If dot is advanced, this implies that the section should |
3322 | have space allocated to it, unless the user has explicitly | |
3323 | stated that the section should never be loaded. */ | |
6feb9908 AM |
3324 | if (!(output_section_statement->flags |
3325 | & (SEC_NEVER_LOAD | SEC_ALLOC))) | |
9dfc8ab2 NC |
3326 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
3327 | ||
252b5132 RH |
3328 | dot = newdot; |
3329 | } | |
3330 | } | |
3331 | break; | |
3332 | ||
3333 | case lang_padding_statement_enum: | |
c0c330a7 AM |
3334 | /* If this is the first time lang_size_sections is called, |
3335 | we won't have any padding statements. If this is the | |
3336 | second or later passes when relaxing, we should allow | |
3337 | padding to shrink. If padding is needed on this pass, it | |
3338 | will be added back in. */ | |
3339 | s->padding_statement.size = 0; | |
6e814ff8 AM |
3340 | |
3341 | /* Make sure output_offset is valid. If relaxation shrinks | |
3342 | the section and this pad isn't needed, it's possible to | |
3343 | have output_offset larger than the final size of the | |
3344 | section. bfd_set_section_contents will complain even for | |
3345 | a pad size of zero. */ | |
3346 | s->padding_statement.output_offset | |
3347 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
3348 | break; |
3349 | ||
3350 | case lang_group_statement_enum: | |
2d20f7bf JJ |
3351 | dot = lang_size_sections_1 (s->group_statement.children.head, |
3352 | output_section_statement, | |
3353 | &s->group_statement.children.head, | |
cf888e70 | 3354 | fill, dot, relax, check_regions); |
252b5132 RH |
3355 | break; |
3356 | ||
3357 | default: | |
3358 | FAIL (); | |
3359 | break; | |
3360 | ||
c0c330a7 | 3361 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
3362 | case lang_address_statement_enum: |
3363 | break; | |
3364 | } | |
3365 | prev = &s->header.next; | |
3366 | } | |
3367 | return dot; | |
3368 | } | |
3369 | ||
2d20f7bf | 3370 | bfd_vma |
1579bae1 AM |
3371 | lang_size_sections |
3372 | (lang_statement_union_type *s, | |
3373 | lang_output_section_statement_type *output_section_statement, | |
3374 | lang_statement_union_type **prev, | |
3375 | fill_type *fill, | |
3376 | bfd_vma dot, | |
3377 | bfd_boolean *relax, | |
3378 | bfd_boolean check_regions) | |
2d20f7bf JJ |
3379 | { |
3380 | bfd_vma result; | |
3381 | ||
420e579c HPN |
3382 | /* Callers of exp_fold_tree need to increment this. */ |
3383 | lang_statement_iteration++; | |
3384 | ||
2d20f7bf JJ |
3385 | exp_data_seg.phase = exp_dataseg_none; |
3386 | result = lang_size_sections_1 (s, output_section_statement, prev, fill, | |
cf888e70 | 3387 | dot, relax, check_regions); |
8c37241b JJ |
3388 | if (exp_data_seg.phase == exp_dataseg_end_seen |
3389 | && link_info.relro && exp_data_seg.relro_end) | |
3390 | { | |
3391 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try | |
3392 | to put exp_data_seg.relro on a (common) page boundary. */ | |
a4f5ad88 | 3393 | bfd_vma old_base, relro_end; |
8c37241b JJ |
3394 | |
3395 | exp_data_seg.phase = exp_dataseg_relro_adjust; | |
a4f5ad88 JJ |
3396 | old_base = exp_data_seg.base; |
3397 | exp_data_seg.base += (-exp_data_seg.relro_end | |
3398 | & (exp_data_seg.pagesize - 1)); | |
3399 | /* Compute the expected PT_GNU_RELRO segment end. */ | |
3400 | relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1) | |
3401 | & (exp_data_seg.pagesize - 1); | |
8c37241b JJ |
3402 | result = lang_size_sections_1 (s, output_section_statement, prev, fill, |
3403 | dot, relax, check_regions); | |
a4f5ad88 JJ |
3404 | if (exp_data_seg.relro_end > relro_end) |
3405 | { | |
3406 | /* The alignment of sections between DATA_SEGMENT_ALIGN | |
3407 | and DATA_SEGMENT_RELRO_END caused huge padding to be | |
3408 | inserted at DATA_SEGMENT_RELRO_END. Try some other base. */ | |
3409 | asection *sec; | |
3410 | unsigned int max_alignment_power = 0; | |
3411 | ||
3412 | /* Find maximum alignment power of sections between | |
3413 | DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */ | |
3414 | for (sec = output_bfd->sections; sec; sec = sec->next) | |
3415 | if (sec->vma >= exp_data_seg.base | |
3416 | && sec->vma < exp_data_seg.relro_end | |
3417 | && sec->alignment_power > max_alignment_power) | |
3418 | max_alignment_power = sec->alignment_power; | |
3419 | ||
3420 | if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize) | |
3421 | { | |
3422 | if (exp_data_seg.base - (1 << max_alignment_power) | |
3423 | < old_base) | |
3424 | exp_data_seg.base += exp_data_seg.pagesize; | |
3425 | exp_data_seg.base -= (1 << max_alignment_power); | |
3426 | result = lang_size_sections_1 (s, output_section_statement, | |
3427 | prev, fill, dot, relax, | |
3428 | check_regions); | |
3429 | } | |
3430 | } | |
8c37241b JJ |
3431 | link_info.relro_start = exp_data_seg.base; |
3432 | link_info.relro_end = exp_data_seg.relro_end; | |
3433 | } | |
3434 | else if (exp_data_seg.phase == exp_dataseg_end_seen) | |
2d20f7bf JJ |
3435 | { |
3436 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether | |
3437 | a page could be saved in the data segment. */ | |
3438 | bfd_vma first, last; | |
3439 | ||
3440 | first = -exp_data_seg.base & (exp_data_seg.pagesize - 1); | |
3441 | last = exp_data_seg.end & (exp_data_seg.pagesize - 1); | |
3442 | if (first && last | |
3443 | && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1)) | |
3444 | != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1))) | |
3445 | && first + last <= exp_data_seg.pagesize) | |
3446 | { | |
3447 | exp_data_seg.phase = exp_dataseg_adjust; | |
1b493742 | 3448 | lang_statement_iteration++; |
2d20f7bf | 3449 | result = lang_size_sections_1 (s, output_section_statement, prev, |
cf888e70 | 3450 | fill, dot, relax, check_regions); |
2d20f7bf JJ |
3451 | } |
3452 | } | |
3453 | ||
3454 | return result; | |
3455 | } | |
3456 | ||
420e579c HPN |
3457 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
3458 | ||
3459 | static bfd_vma | |
3460 | lang_do_assignments_1 | |
1579bae1 AM |
3461 | (lang_statement_union_type *s, |
3462 | lang_output_section_statement_type *output_section_statement, | |
3463 | fill_type *fill, | |
3464 | bfd_vma dot) | |
252b5132 | 3465 | { |
1579bae1 | 3466 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3467 | { |
3468 | switch (s->header.type) | |
3469 | { | |
3470 | case lang_constructors_statement_enum: | |
420e579c HPN |
3471 | dot = lang_do_assignments_1 (constructor_list.head, |
3472 | output_section_statement, | |
3473 | fill, | |
3474 | dot); | |
252b5132 RH |
3475 | break; |
3476 | ||
3477 | case lang_output_section_statement_enum: | |
3478 | { | |
d1778b88 | 3479 | lang_output_section_statement_type *os; |
252b5132 | 3480 | |
d1778b88 | 3481 | os = &(s->output_section_statement); |
252b5132 RH |
3482 | if (os->bfd_section != NULL) |
3483 | { | |
3484 | dot = os->bfd_section->vma; | |
4a43e768 | 3485 | lang_do_assignments_1 (os->children.head, os, os->fill, dot); |
3737f867 JJ |
3486 | /* .tbss sections effectively have zero size. */ |
3487 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
3488 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
3489 | || link_info.relocatable) | |
eea6121a | 3490 | dot += TO_ADDR (os->bfd_section->size); |
252b5132 | 3491 | } |
c13b1b77 | 3492 | if (os->load_base) |
252b5132 RH |
3493 | { |
3494 | /* If nothing has been placed into the output section then | |
4de2d33d | 3495 | it won't have a bfd_section. */ |
5f992e62 | 3496 | if (os->bfd_section) |
252b5132 | 3497 | { |
5f992e62 | 3498 | os->bfd_section->lma |
08da4cac KH |
3499 | = exp_get_abs_int (os->load_base, 0, "load base", |
3500 | lang_final_phase_enum); | |
252b5132 RH |
3501 | } |
3502 | } | |
3503 | } | |
3504 | break; | |
3505 | case lang_wild_statement_enum: | |
3506 | ||
420e579c HPN |
3507 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
3508 | output_section_statement, | |
3509 | fill, dot); | |
252b5132 RH |
3510 | |
3511 | break; | |
3512 | ||
3513 | case lang_object_symbols_statement_enum: | |
3514 | case lang_output_statement_enum: | |
3515 | case lang_target_statement_enum: | |
3516 | #if 0 | |
3517 | case lang_common_statement_enum: | |
3518 | #endif | |
3519 | break; | |
3520 | case lang_data_statement_enum: | |
3521 | { | |
3522 | etree_value_type value; | |
3523 | ||
3524 | value = exp_fold_tree (s->data_statement.exp, | |
3525 | abs_output_section, | |
3526 | lang_final_phase_enum, dot, &dot); | |
7c519c12 | 3527 | if (!value.valid_p) |
252b5132 | 3528 | einfo (_("%F%P: invalid data statement\n")); |
384d938f NS |
3529 | s->data_statement.value |
3530 | = value.value + value.section->bfd_section->vma; | |
252b5132 | 3531 | } |
b7a26f91 KH |
3532 | { |
3533 | unsigned int size; | |
08da4cac KH |
3534 | switch (s->data_statement.type) |
3535 | { | |
3536 | default: | |
3537 | abort (); | |
3538 | case QUAD: | |
3539 | case SQUAD: | |
3540 | size = QUAD_SIZE; | |
3541 | break; | |
3542 | case LONG: | |
3543 | size = LONG_SIZE; | |
3544 | break; | |
3545 | case SHORT: | |
3546 | size = SHORT_SIZE; | |
3547 | break; | |
3548 | case BYTE: | |
3549 | size = BYTE_SIZE; | |
3550 | break; | |
3551 | } | |
e5caa5e0 AM |
3552 | if (size < TO_SIZE ((unsigned) 1)) |
3553 | size = TO_SIZE ((unsigned) 1); | |
3554 | dot += TO_ADDR (size); | |
08da4cac | 3555 | } |
252b5132 RH |
3556 | break; |
3557 | ||
3558 | case lang_reloc_statement_enum: | |
3559 | { | |
3560 | etree_value_type value; | |
3561 | ||
3562 | value = exp_fold_tree (s->reloc_statement.addend_exp, | |
3563 | abs_output_section, | |
3564 | lang_final_phase_enum, dot, &dot); | |
3565 | s->reloc_statement.addend_value = value.value; | |
7c519c12 | 3566 | if (!value.valid_p) |
252b5132 RH |
3567 | einfo (_("%F%P: invalid reloc statement\n")); |
3568 | } | |
e5caa5e0 | 3569 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
3570 | break; |
3571 | ||
3572 | case lang_input_section_enum: | |
3573 | { | |
3574 | asection *in = s->input_section.section; | |
3575 | ||
57ceae94 | 3576 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 3577 | dot += TO_ADDR (in->size); |
252b5132 RH |
3578 | } |
3579 | break; | |
3580 | ||
3581 | case lang_input_statement_enum: | |
3582 | break; | |
3583 | case lang_fill_statement_enum: | |
3584 | fill = s->fill_statement.fill; | |
3585 | break; | |
3586 | case lang_assignment_statement_enum: | |
3587 | { | |
3588 | exp_fold_tree (s->assignment_statement.exp, | |
3589 | output_section_statement, | |
3590 | lang_final_phase_enum, | |
3591 | dot, | |
3592 | &dot); | |
3593 | } | |
3594 | ||
3595 | break; | |
3596 | case lang_padding_statement_enum: | |
e5caa5e0 | 3597 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
3598 | break; |
3599 | ||
3600 | case lang_group_statement_enum: | |
420e579c HPN |
3601 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
3602 | output_section_statement, | |
3603 | fill, dot); | |
252b5132 RH |
3604 | |
3605 | break; | |
3606 | ||
3607 | default: | |
3608 | FAIL (); | |
3609 | break; | |
3610 | case lang_address_statement_enum: | |
3611 | break; | |
3612 | } | |
3613 | ||
3614 | } | |
3615 | return dot; | |
3616 | } | |
3617 | ||
f2241121 | 3618 | void |
6feb9908 AM |
3619 | lang_do_assignments |
3620 | (lang_statement_union_type *s, | |
3621 | lang_output_section_statement_type *output_section_statement, | |
3622 | fill_type *fill, | |
3623 | bfd_vma dot) | |
420e579c HPN |
3624 | { |
3625 | /* Callers of exp_fold_tree need to increment this. */ | |
3626 | lang_statement_iteration++; | |
3627 | lang_do_assignments_1 (s, output_section_statement, fill, dot); | |
3628 | } | |
3629 | ||
252b5132 RH |
3630 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
3631 | operator .startof. (section_name), it produces an undefined symbol | |
3632 | .startof.section_name. Similarly, when it sees | |
3633 | .sizeof. (section_name), it produces an undefined symbol | |
3634 | .sizeof.section_name. For all the output sections, we look for | |
3635 | such symbols, and set them to the correct value. */ | |
3636 | ||
3637 | static void | |
1579bae1 | 3638 | lang_set_startof (void) |
252b5132 RH |
3639 | { |
3640 | asection *s; | |
3641 | ||
1049f94e | 3642 | if (link_info.relocatable) |
252b5132 RH |
3643 | return; |
3644 | ||
3645 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
3646 | { | |
3647 | const char *secname; | |
3648 | char *buf; | |
3649 | struct bfd_link_hash_entry *h; | |
3650 | ||
3651 | secname = bfd_get_section_name (output_bfd, s); | |
3652 | buf = xmalloc (10 + strlen (secname)); | |
3653 | ||
3654 | sprintf (buf, ".startof.%s", secname); | |
b34976b6 | 3655 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
3656 | if (h != NULL && h->type == bfd_link_hash_undefined) |
3657 | { | |
3658 | h->type = bfd_link_hash_defined; | |
3659 | h->u.def.value = bfd_get_section_vma (output_bfd, s); | |
3660 | h->u.def.section = bfd_abs_section_ptr; | |
3661 | } | |
3662 | ||
3663 | sprintf (buf, ".sizeof.%s", secname); | |
b34976b6 | 3664 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
3665 | if (h != NULL && h->type == bfd_link_hash_undefined) |
3666 | { | |
3667 | h->type = bfd_link_hash_defined; | |
eea6121a | 3668 | h->u.def.value = TO_ADDR (s->size); |
252b5132 RH |
3669 | h->u.def.section = bfd_abs_section_ptr; |
3670 | } | |
3671 | ||
3672 | free (buf); | |
3673 | } | |
3674 | } | |
3675 | ||
3676 | static void | |
1579bae1 | 3677 | lang_finish (void) |
252b5132 RH |
3678 | { |
3679 | struct bfd_link_hash_entry *h; | |
b34976b6 | 3680 | bfd_boolean warn; |
252b5132 | 3681 | |
1049f94e | 3682 | if (link_info.relocatable || link_info.shared) |
b34976b6 | 3683 | warn = FALSE; |
252b5132 | 3684 | else |
b34976b6 | 3685 | warn = TRUE; |
252b5132 | 3686 | |
1579bae1 | 3687 | if (entry_symbol.name == NULL) |
252b5132 RH |
3688 | { |
3689 | /* No entry has been specified. Look for start, but don't warn | |
3690 | if we don't find it. */ | |
e3e942e9 | 3691 | entry_symbol.name = "start"; |
b34976b6 | 3692 | warn = FALSE; |
252b5132 RH |
3693 | } |
3694 | ||
e3e942e9 | 3695 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
b34976b6 | 3696 | FALSE, FALSE, TRUE); |
1579bae1 | 3697 | if (h != NULL |
252b5132 RH |
3698 | && (h->type == bfd_link_hash_defined |
3699 | || h->type == bfd_link_hash_defweak) | |
3700 | && h->u.def.section->output_section != NULL) | |
3701 | { | |
3702 | bfd_vma val; | |
3703 | ||
3704 | val = (h->u.def.value | |
3705 | + bfd_get_section_vma (output_bfd, | |
3706 | h->u.def.section->output_section) | |
3707 | + h->u.def.section->output_offset); | |
3708 | if (! bfd_set_start_address (output_bfd, val)) | |
e3e942e9 | 3709 | einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
3710 | } |
3711 | else | |
3712 | { | |
3713 | bfd_vma val; | |
5f992e62 | 3714 | const char *send; |
252b5132 RH |
3715 | |
3716 | /* We couldn't find the entry symbol. Try parsing it as a | |
3717 | number. */ | |
e3e942e9 | 3718 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
3719 | if (*send == '\0') |
3720 | { | |
3721 | if (! bfd_set_start_address (output_bfd, val)) | |
3722 | einfo (_("%P%F: can't set start address\n")); | |
3723 | } | |
3724 | else | |
3725 | { | |
3726 | asection *ts; | |
3727 | ||
3728 | /* Can't find the entry symbol, and it's not a number. Use | |
3729 | the first address in the text section. */ | |
1e281515 | 3730 | ts = bfd_get_section_by_name (output_bfd, entry_section); |
1579bae1 | 3731 | if (ts != NULL) |
252b5132 RH |
3732 | { |
3733 | if (warn) | |
6feb9908 AM |
3734 | einfo (_("%P: warning: cannot find entry symbol %s;" |
3735 | " defaulting to %V\n"), | |
e3e942e9 AM |
3736 | entry_symbol.name, |
3737 | bfd_get_section_vma (output_bfd, ts)); | |
252b5132 RH |
3738 | if (! bfd_set_start_address (output_bfd, |
3739 | bfd_get_section_vma (output_bfd, | |
3740 | ts))) | |
3741 | einfo (_("%P%F: can't set start address\n")); | |
3742 | } | |
3743 | else | |
3744 | { | |
3745 | if (warn) | |
6feb9908 AM |
3746 | einfo (_("%P: warning: cannot find entry symbol %s;" |
3747 | " not setting start address\n"), | |
e3e942e9 | 3748 | entry_symbol.name); |
252b5132 RH |
3749 | } |
3750 | } | |
3751 | } | |
420e579c | 3752 | |
7115639b AM |
3753 | /* Don't bfd_hash_table_free (&lang_definedness_table); |
3754 | map file output may result in a call of lang_track_definedness. */ | |
252b5132 RH |
3755 | } |
3756 | ||
3757 | /* This is a small function used when we want to ignore errors from | |
3758 | BFD. */ | |
3759 | ||
3760 | static void | |
87f2a346 | 3761 | ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) |
252b5132 RH |
3762 | { |
3763 | /* Don't do anything. */ | |
3764 | } | |
3765 | ||
3766 | /* Check that the architecture of all the input files is compatible | |
3767 | with the output file. Also call the backend to let it do any | |
3768 | other checking that is needed. */ | |
3769 | ||
3770 | static void | |
1579bae1 | 3771 | lang_check (void) |
252b5132 RH |
3772 | { |
3773 | lang_statement_union_type *file; | |
3774 | bfd *input_bfd; | |
5f992e62 | 3775 | const bfd_arch_info_type *compatible; |
252b5132 | 3776 | |
1579bae1 | 3777 | for (file = file_chain.head; file != NULL; file = file->input_statement.next) |
252b5132 RH |
3778 | { |
3779 | input_bfd = file->input_statement.the_bfd; | |
6feb9908 AM |
3780 | compatible |
3781 | = bfd_arch_get_compatible (input_bfd, output_bfd, | |
3782 | command_line.accept_unknown_input_arch); | |
30cba025 AM |
3783 | |
3784 | /* In general it is not possible to perform a relocatable | |
3785 | link between differing object formats when the input | |
3786 | file has relocations, because the relocations in the | |
3787 | input format may not have equivalent representations in | |
3788 | the output format (and besides BFD does not translate | |
3789 | relocs for other link purposes than a final link). */ | |
1049f94e | 3790 | if ((link_info.relocatable || link_info.emitrelocations) |
30cba025 | 3791 | && (compatible == NULL |
d35a52e2 | 3792 | || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd)) |
30cba025 AM |
3793 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
3794 | { | |
6feb9908 AM |
3795 | einfo (_("%P%F: Relocatable linking with relocations from" |
3796 | " format %s (%B) to format %s (%B) is not supported\n"), | |
30cba025 AM |
3797 | bfd_get_target (input_bfd), input_bfd, |
3798 | bfd_get_target (output_bfd), output_bfd); | |
3799 | /* einfo with %F exits. */ | |
3800 | } | |
3801 | ||
252b5132 RH |
3802 | if (compatible == NULL) |
3803 | { | |
3804 | if (command_line.warn_mismatch) | |
6feb9908 AM |
3805 | einfo (_("%P: warning: %s architecture of input file `%B'" |
3806 | " is incompatible with %s output\n"), | |
252b5132 RH |
3807 | bfd_printable_name (input_bfd), input_bfd, |
3808 | bfd_printable_name (output_bfd)); | |
3809 | } | |
b9247304 | 3810 | else if (bfd_count_sections (input_bfd)) |
252b5132 | 3811 | { |
b9247304 | 3812 | /* If the input bfd has no contents, it shouldn't set the |
b7a26f91 | 3813 | private data of the output bfd. */ |
b9247304 | 3814 | |
252b5132 RH |
3815 | bfd_error_handler_type pfn = NULL; |
3816 | ||
3817 | /* If we aren't supposed to warn about mismatched input | |
3818 | files, temporarily set the BFD error handler to a | |
3819 | function which will do nothing. We still want to call | |
3820 | bfd_merge_private_bfd_data, since it may set up | |
3821 | information which is needed in the output file. */ | |
3822 | if (! command_line.warn_mismatch) | |
3823 | pfn = bfd_set_error_handler (ignore_bfd_errors); | |
3824 | if (! bfd_merge_private_bfd_data (input_bfd, output_bfd)) | |
3825 | { | |
3826 | if (command_line.warn_mismatch) | |
6feb9908 AM |
3827 | einfo (_("%P%X: failed to merge target specific data" |
3828 | " of file %B\n"), input_bfd); | |
252b5132 RH |
3829 | } |
3830 | if (! command_line.warn_mismatch) | |
3831 | bfd_set_error_handler (pfn); | |
3832 | } | |
3833 | } | |
3834 | } | |
3835 | ||
3836 | /* Look through all the global common symbols and attach them to the | |
3837 | correct section. The -sort-common command line switch may be used | |
3838 | to roughly sort the entries by size. */ | |
3839 | ||
3840 | static void | |
1579bae1 | 3841 | lang_common (void) |
252b5132 | 3842 | { |
4818e05f AM |
3843 | if (command_line.inhibit_common_definition) |
3844 | return; | |
1049f94e | 3845 | if (link_info.relocatable |
252b5132 RH |
3846 | && ! command_line.force_common_definition) |
3847 | return; | |
3848 | ||
3849 | if (! config.sort_common) | |
1579bae1 | 3850 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
3851 | else |
3852 | { | |
3853 | int power; | |
3854 | ||
3855 | for (power = 4; power >= 0; power--) | |
1579bae1 | 3856 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
252b5132 RH |
3857 | } |
3858 | } | |
3859 | ||
3860 | /* Place one common symbol in the correct section. */ | |
3861 | ||
b34976b6 | 3862 | static bfd_boolean |
1579bae1 | 3863 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
3864 | { |
3865 | unsigned int power_of_two; | |
3866 | bfd_vma size; | |
3867 | asection *section; | |
3868 | ||
3869 | if (h->type != bfd_link_hash_common) | |
b34976b6 | 3870 | return TRUE; |
252b5132 RH |
3871 | |
3872 | size = h->u.c.size; | |
3873 | power_of_two = h->u.c.p->alignment_power; | |
3874 | ||
3875 | if (config.sort_common | |
3876 | && power_of_two < (unsigned int) *(int *) info) | |
b34976b6 | 3877 | return TRUE; |
252b5132 RH |
3878 | |
3879 | section = h->u.c.p->section; | |
3880 | ||
e5caa5e0 | 3881 | /* Increase the size of the section to align the common sym. */ |
eea6121a AM |
3882 | section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1; |
3883 | section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift)); | |
252b5132 RH |
3884 | |
3885 | /* Adjust the alignment if necessary. */ | |
3886 | if (power_of_two > section->alignment_power) | |
3887 | section->alignment_power = power_of_two; | |
3888 | ||
3889 | /* Change the symbol from common to defined. */ | |
3890 | h->type = bfd_link_hash_defined; | |
3891 | h->u.def.section = section; | |
eea6121a | 3892 | h->u.def.value = section->size; |
252b5132 RH |
3893 | |
3894 | /* Increase the size of the section. */ | |
eea6121a | 3895 | section->size += size; |
252b5132 RH |
3896 | |
3897 | /* Make sure the section is allocated in memory, and make sure that | |
3898 | it is no longer a common section. */ | |
3899 | section->flags |= SEC_ALLOC; | |
08da4cac | 3900 | section->flags &= ~SEC_IS_COMMON; |
252b5132 RH |
3901 | |
3902 | if (config.map_file != NULL) | |
3903 | { | |
b34976b6 | 3904 | static bfd_boolean header_printed; |
252b5132 RH |
3905 | int len; |
3906 | char *name; | |
3907 | char buf[50]; | |
3908 | ||
3909 | if (! header_printed) | |
3910 | { | |
3911 | minfo (_("\nAllocating common symbols\n")); | |
3912 | minfo (_("Common symbol size file\n\n")); | |
b34976b6 | 3913 | header_printed = TRUE; |
252b5132 RH |
3914 | } |
3915 | ||
3916 | name = demangle (h->root.string); | |
3917 | minfo ("%s", name); | |
3918 | len = strlen (name); | |
3919 | free (name); | |
3920 | ||
3921 | if (len >= 19) | |
3922 | { | |
3923 | print_nl (); | |
3924 | len = 0; | |
3925 | } | |
3926 | while (len < 20) | |
3927 | { | |
3928 | print_space (); | |
3929 | ++len; | |
3930 | } | |
3931 | ||
3932 | minfo ("0x"); | |
3933 | if (size <= 0xffffffff) | |
3934 | sprintf (buf, "%lx", (unsigned long) size); | |
3935 | else | |
3936 | sprintf_vma (buf, size); | |
3937 | minfo ("%s", buf); | |
3938 | len = strlen (buf); | |
3939 | ||
3940 | while (len < 16) | |
3941 | { | |
3942 | print_space (); | |
3943 | ++len; | |
3944 | } | |
3945 | ||
3946 | minfo ("%B\n", section->owner); | |
3947 | } | |
3948 | ||
b34976b6 | 3949 | return TRUE; |
252b5132 RH |
3950 | } |
3951 | ||
08da4cac KH |
3952 | /* Run through the input files and ensure that every input section has |
3953 | somewhere to go. If one is found without a destination then create | |
3954 | an input request and place it into the statement tree. */ | |
252b5132 RH |
3955 | |
3956 | static void | |
1579bae1 | 3957 | lang_place_orphans (void) |
252b5132 | 3958 | { |
e50d8076 | 3959 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
3960 | { |
3961 | asection *s; | |
3962 | ||
1579bae1 | 3963 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 3964 | { |
1579bae1 | 3965 | if (s->output_section == NULL) |
252b5132 | 3966 | { |
396a2467 | 3967 | /* This section of the file is not attached, root |
08da4cac | 3968 | around for a sensible place for it to go. */ |
252b5132 RH |
3969 | |
3970 | if (file->just_syms_flag) | |
164e712d AM |
3971 | abort (); |
3972 | ||
3973 | if ((s->flags & SEC_EXCLUDE) != 0) | |
3974 | s->output_section = bfd_abs_section_ptr; | |
252b5132 RH |
3975 | else if (strcmp (s->name, "COMMON") == 0) |
3976 | { | |
3977 | /* This is a lonely common section which must have | |
3978 | come from an archive. We attach to the section | |
3979 | with the wildcard. */ | |
1049f94e | 3980 | if (! link_info.relocatable |
252b5132 RH |
3981 | || command_line.force_common_definition) |
3982 | { | |
3983 | if (default_common_section == NULL) | |
3984 | { | |
3985 | #if 0 | |
3986 | /* This message happens when using the | |
3987 | svr3.ifile linker script, so I have | |
3988 | disabled it. */ | |
6feb9908 AM |
3989 | info_msg (_("%P: no [COMMON] command," |
3990 | " defaulting to .bss\n")); | |
252b5132 RH |
3991 | #endif |
3992 | default_common_section = | |
3993 | lang_output_section_statement_lookup (".bss"); | |
3994 | ||
3995 | } | |
39dcfe18 AM |
3996 | lang_add_section (&default_common_section->children, s, |
3997 | default_common_section, file); | |
252b5132 RH |
3998 | } |
3999 | } | |
4000 | else if (ldemul_place_orphan (file, s)) | |
4001 | ; | |
4002 | else | |
4003 | { | |
39dcfe18 | 4004 | lang_output_section_statement_type *os; |
252b5132 | 4005 | |
39dcfe18 AM |
4006 | os = lang_output_section_statement_lookup (s->name); |
4007 | lang_add_section (&os->children, s, os, file); | |
252b5132 RH |
4008 | } |
4009 | } | |
4010 | } | |
4011 | } | |
4012 | } | |
4013 | ||
252b5132 | 4014 | void |
1579bae1 | 4015 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 4016 | { |
aa8804e4 | 4017 | flagword *ptr_flags; |
252b5132 | 4018 | |
aa8804e4 | 4019 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
252b5132 RH |
4020 | while (*flags) |
4021 | { | |
4022 | switch (*flags) | |
4023 | { | |
252b5132 RH |
4024 | case 'A': case 'a': |
4025 | *ptr_flags |= SEC_ALLOC; | |
4026 | break; | |
4027 | ||
4028 | case 'R': case 'r': | |
4029 | *ptr_flags |= SEC_READONLY; | |
4030 | break; | |
4031 | ||
4032 | case 'W': case 'w': | |
4033 | *ptr_flags |= SEC_DATA; | |
4034 | break; | |
4035 | ||
4036 | case 'X': case 'x': | |
4037 | *ptr_flags |= SEC_CODE; | |
4038 | break; | |
4039 | ||
4040 | case 'L': case 'l': | |
4041 | case 'I': case 'i': | |
4042 | *ptr_flags |= SEC_LOAD; | |
4043 | break; | |
4044 | ||
4045 | default: | |
4046 | einfo (_("%P%F: invalid syntax in flags\n")); | |
4047 | break; | |
4048 | } | |
4049 | flags++; | |
4050 | } | |
4051 | } | |
4052 | ||
4053 | /* Call a function on each input file. This function will be called | |
4054 | on an archive, but not on the elements. */ | |
4055 | ||
4056 | void | |
1579bae1 | 4057 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
4058 | { |
4059 | lang_input_statement_type *f; | |
4060 | ||
4061 | for (f = (lang_input_statement_type *) input_file_chain.head; | |
4062 | f != NULL; | |
4063 | f = (lang_input_statement_type *) f->next_real_file) | |
4064 | func (f); | |
4065 | } | |
4066 | ||
4067 | /* Call a function on each file. The function will be called on all | |
4068 | the elements of an archive which are included in the link, but will | |
4069 | not be called on the archive file itself. */ | |
4070 | ||
4071 | void | |
1579bae1 | 4072 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 4073 | { |
e50d8076 | 4074 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 RH |
4075 | { |
4076 | func (f); | |
4077 | } | |
4078 | } | |
4079 | ||
252b5132 | 4080 | void |
1579bae1 | 4081 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 RH |
4082 | { |
4083 | bfd **pp; | |
4084 | ||
4085 | lang_statement_append (&file_chain, | |
4086 | (lang_statement_union_type *) entry, | |
4087 | &entry->next); | |
4088 | ||
4089 | /* The BFD linker needs to have a list of all input BFDs involved in | |
4090 | a link. */ | |
1579bae1 | 4091 | ASSERT (entry->the_bfd->link_next == NULL); |
252b5132 | 4092 | ASSERT (entry->the_bfd != output_bfd); |
1579bae1 | 4093 | for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next) |
252b5132 RH |
4094 | ; |
4095 | *pp = entry->the_bfd; | |
1579bae1 | 4096 | entry->the_bfd->usrdata = entry; |
252b5132 RH |
4097 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
4098 | ||
4099 | /* Look through the sections and check for any which should not be | |
4100 | included in the link. We need to do this now, so that we can | |
4101 | notice when the backend linker tries to report multiple | |
4102 | definition errors for symbols which are in sections we aren't | |
4103 | going to link. FIXME: It might be better to entirely ignore | |
4104 | symbols which are defined in sections which are going to be | |
4105 | discarded. This would require modifying the backend linker for | |
4106 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
4107 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
4108 | ||
1579bae1 | 4109 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
4110 | } |
4111 | ||
4112 | void | |
1579bae1 | 4113 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
4114 | { |
4115 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 4116 | if (!had_output_filename || !from_script) |
252b5132 RH |
4117 | { |
4118 | output_filename = name; | |
b34976b6 | 4119 | had_output_filename = TRUE; |
252b5132 RH |
4120 | } |
4121 | } | |
4122 | ||
252b5132 RH |
4123 | static lang_output_section_statement_type *current_section; |
4124 | ||
4125 | static int | |
1579bae1 | 4126 | topower (int x) |
252b5132 RH |
4127 | { |
4128 | unsigned int i = 1; | |
4129 | int l; | |
4130 | ||
4131 | if (x < 0) | |
4132 | return -1; | |
4133 | ||
5f992e62 | 4134 | for (l = 0; l < 32; l++) |
252b5132 RH |
4135 | { |
4136 | if (i >= (unsigned int) x) | |
4137 | return l; | |
4138 | i <<= 1; | |
4139 | } | |
4140 | ||
4141 | return 0; | |
4142 | } | |
4143 | ||
aea4bd9d | 4144 | lang_output_section_statement_type * |
1579bae1 AM |
4145 | lang_enter_output_section_statement (const char *output_section_statement_name, |
4146 | etree_type *address_exp, | |
4147 | enum section_type sectype, | |
1579bae1 AM |
4148 | etree_type *align, |
4149 | etree_type *subalign, | |
0841712e JJ |
4150 | etree_type *ebase, |
4151 | int constraint) | |
252b5132 RH |
4152 | { |
4153 | lang_output_section_statement_type *os; | |
4154 | ||
4155 | current_section = | |
4156 | os = | |
0841712e JJ |
4157 | lang_output_section_statement_lookup_1 (output_section_statement_name, |
4158 | constraint); | |
252b5132 | 4159 | |
08da4cac KH |
4160 | /* Add this statement to tree. */ |
4161 | #if 0 | |
4162 | add_statement (lang_output_section_statement_enum, | |
4163 | output_section_statement); | |
4164 | #endif | |
4165 | /* Make next things chain into subchain of this. */ | |
252b5132 | 4166 | |
1579bae1 | 4167 | if (os->addr_tree == NULL) |
08da4cac KH |
4168 | { |
4169 | os->addr_tree = address_exp; | |
4170 | } | |
252b5132 RH |
4171 | os->sectype = sectype; |
4172 | if (sectype != noload_section) | |
4173 | os->flags = SEC_NO_FLAGS; | |
4174 | else | |
4175 | os->flags = SEC_NEVER_LOAD; | |
e5caa5e0 | 4176 | os->block_value = 1; |
252b5132 RH |
4177 | stat_ptr = &os->children; |
4178 | ||
08da4cac KH |
4179 | os->subsection_alignment = |
4180 | topower (exp_get_value_int (subalign, -1, "subsection alignment", 0)); | |
4181 | os->section_alignment = | |
4182 | topower (exp_get_value_int (align, -1, "section alignment", 0)); | |
252b5132 RH |
4183 | |
4184 | os->load_base = ebase; | |
aea4bd9d | 4185 | return os; |
252b5132 RH |
4186 | } |
4187 | ||
252b5132 | 4188 | void |
1579bae1 | 4189 | lang_final (void) |
252b5132 RH |
4190 | { |
4191 | lang_output_statement_type *new = | |
4192 | new_stat (lang_output_statement, stat_ptr); | |
4193 | ||
4194 | new->name = output_filename; | |
4195 | } | |
4196 | ||
08da4cac KH |
4197 | /* Reset the current counters in the regions. */ |
4198 | ||
e3dc8847 | 4199 | void |
1579bae1 | 4200 | lang_reset_memory_regions (void) |
252b5132 RH |
4201 | { |
4202 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 4203 | asection *o; |
252b5132 | 4204 | |
1579bae1 | 4205 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
4206 | { |
4207 | p->old_length = (bfd_size_type) (p->current - p->origin); | |
4208 | p->current = p->origin; | |
4209 | } | |
b3327aad AM |
4210 | |
4211 | for (o = output_bfd->sections; o != NULL; o = o->next) | |
1a23a9e6 AM |
4212 | { |
4213 | /* Save the last size for possible use by bfd_relax_section. */ | |
4214 | o->rawsize = o->size; | |
4215 | o->size = 0; | |
4216 | } | |
252b5132 RH |
4217 | } |
4218 | ||
164e712d | 4219 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
4220 | |
4221 | static void | |
1579bae1 AM |
4222 | gc_section_callback (lang_wild_statement_type *ptr, |
4223 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
4224 | asection *section, | |
4225 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
4226 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 4227 | { |
164e712d AM |
4228 | /* If the wild pattern was marked KEEP, the member sections |
4229 | should be as well. */ | |
4dec4d4e RH |
4230 | if (ptr->keep_sections) |
4231 | section->flags |= SEC_KEEP; | |
252b5132 RH |
4232 | } |
4233 | ||
252b5132 RH |
4234 | /* Iterate over sections marking them against GC. */ |
4235 | ||
4236 | static void | |
1579bae1 | 4237 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 4238 | { |
1579bae1 | 4239 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
4240 | { |
4241 | switch (s->header.type) | |
4242 | { | |
4243 | case lang_wild_statement_enum: | |
164e712d | 4244 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 4245 | break; |
252b5132 RH |
4246 | case lang_constructors_statement_enum: |
4247 | lang_gc_sections_1 (constructor_list.head); | |
4248 | break; | |
4249 | case lang_output_section_statement_enum: | |
4250 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
4251 | break; | |
4252 | case lang_group_statement_enum: | |
4253 | lang_gc_sections_1 (s->group_statement.children.head); | |
4254 | break; | |
4255 | default: | |
4256 | break; | |
4257 | } | |
4258 | } | |
4259 | } | |
4260 | ||
4261 | static void | |
1579bae1 | 4262 | lang_gc_sections (void) |
252b5132 RH |
4263 | { |
4264 | struct bfd_link_hash_entry *h; | |
e3e942e9 | 4265 | ldlang_undef_chain_list_type *ulist; |
252b5132 RH |
4266 | |
4267 | /* Keep all sections so marked in the link script. */ | |
4268 | ||
4269 | lang_gc_sections_1 (statement_list.head); | |
4270 | ||
e3e942e9 AM |
4271 | /* Keep all sections containing symbols undefined on the command-line, |
4272 | and the section containing the entry symbol. */ | |
252b5132 | 4273 | |
e3e942e9 | 4274 | for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next) |
252b5132 | 4275 | { |
5f992e62 | 4276 | h = bfd_link_hash_lookup (link_info.hash, ulist->name, |
b34976b6 | 4277 | FALSE, FALSE, FALSE); |
252b5132 | 4278 | |
1579bae1 | 4279 | if (h != NULL |
08da4cac KH |
4280 | && (h->type == bfd_link_hash_defined |
4281 | || h->type == bfd_link_hash_defweak) | |
252b5132 RH |
4282 | && ! bfd_is_abs_section (h->u.def.section)) |
4283 | { | |
4284 | h->u.def.section->flags |= SEC_KEEP; | |
4285 | } | |
4286 | } | |
4287 | ||
9ca57817 AM |
4288 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
4289 | the special case of debug info. (See bfd/stabs.c) | |
4290 | Twiddle the flag here, to simplify later linker code. */ | |
4291 | if (link_info.relocatable) | |
4292 | { | |
4293 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
4294 | { | |
4295 | asection *sec; | |
4296 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) | |
4297 | if ((sec->flags & SEC_DEBUGGING) == 0) | |
4298 | sec->flags &= ~SEC_EXCLUDE; | |
4299 | } | |
4300 | } | |
4301 | ||
164e712d AM |
4302 | if (command_line.gc_sections) |
4303 | bfd_gc_sections (output_bfd, &link_info); | |
252b5132 RH |
4304 | } |
4305 | ||
4306 | void | |
1579bae1 | 4307 | lang_process (void) |
252b5132 RH |
4308 | { |
4309 | lang_reasonable_defaults (); | |
4310 | current_target = default_target; | |
4311 | ||
08da4cac KH |
4312 | /* Open the output file. */ |
4313 | lang_for_each_statement (ldlang_open_output); | |
e5caa5e0 | 4314 | init_opb (); |
252b5132 RH |
4315 | |
4316 | ldemul_create_output_section_statements (); | |
4317 | ||
08da4cac | 4318 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
4319 | lang_place_undefineds (); |
4320 | ||
082b7297 L |
4321 | if (!bfd_section_already_linked_table_init ()) |
4322 | einfo (_("%P%F: Failed to create hash table\n")); | |
9503fd87 | 4323 | |
08da4cac | 4324 | /* Create a bfd for each input file. */ |
252b5132 | 4325 | current_target = default_target; |
b34976b6 | 4326 | open_input_bfds (statement_list.head, FALSE); |
252b5132 | 4327 | |
e3e942e9 AM |
4328 | link_info.gc_sym_list = &entry_symbol; |
4329 | if (entry_symbol.name == NULL) | |
4330 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
4331 | ||
252b5132 RH |
4332 | ldemul_after_open (); |
4333 | ||
082b7297 | 4334 | bfd_section_already_linked_table_free (); |
9503fd87 | 4335 | |
252b5132 RH |
4336 | /* Make sure that we're not mixing architectures. We call this |
4337 | after all the input files have been opened, but before we do any | |
4338 | other processing, so that any operations merge_private_bfd_data | |
4339 | does on the output file will be known during the rest of the | |
4340 | link. */ | |
4341 | lang_check (); | |
4342 | ||
4343 | /* Handle .exports instead of a version script if we're told to do so. */ | |
4344 | if (command_line.version_exports_section) | |
4345 | lang_do_version_exports_section (); | |
4346 | ||
4347 | /* Build all sets based on the information gathered from the input | |
4348 | files. */ | |
4349 | ldctor_build_sets (); | |
4350 | ||
4351 | /* Remove unreferenced sections if asked to. */ | |
164e712d | 4352 | lang_gc_sections (); |
252b5132 | 4353 | |
08da4cac | 4354 | /* Size up the common data. */ |
252b5132 RH |
4355 | lang_common (); |
4356 | ||
bcaa7b3e L |
4357 | /* Update wild statements. */ |
4358 | update_wild_statements (statement_list.head); | |
4359 | ||
252b5132 | 4360 | /* Run through the contours of the script and attach input sections |
08da4cac | 4361 | to the correct output sections. */ |
1579bae1 | 4362 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 4363 | |
08da4cac | 4364 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
4365 | lang_place_orphans (); |
4366 | ||
1049f94e | 4367 | if (! link_info.relocatable) |
862120bd | 4368 | { |
57ceae94 AM |
4369 | asection *found; |
4370 | ||
4371 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
4372 | sections, so that GCed sections are not merged, but before | |
4373 | assigning dynamic symbols, since removing whole input sections | |
4374 | is hard then. */ | |
4375 | bfd_merge_sections (output_bfd, &link_info); | |
4376 | ||
862120bd | 4377 | /* Look for a text section and set the readonly attribute in it. */ |
57ceae94 | 4378 | found = bfd_get_section_by_name (output_bfd, ".text"); |
862120bd | 4379 | |
1579bae1 | 4380 | if (found != NULL) |
862120bd AM |
4381 | { |
4382 | if (config.text_read_only) | |
4383 | found->flags |= SEC_READONLY; | |
4384 | else | |
4385 | found->flags &= ~SEC_READONLY; | |
4386 | } | |
4387 | } | |
4388 | ||
4389 | /* Do anything special before sizing sections. This is where ELF | |
4390 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
4391 | ldemul_before_allocation (); |
4392 | ||
1049f94e | 4393 | if (!link_info.relocatable) |
4bd5a393 AM |
4394 | strip_excluded_output_sections (); |
4395 | ||
252b5132 RH |
4396 | /* We must record the program headers before we try to fix the |
4397 | section positions, since they will affect SIZEOF_HEADERS. */ | |
4398 | lang_record_phdrs (); | |
4399 | ||
b3327aad | 4400 | /* Size up the sections. */ |
1579bae1 AM |
4401 | lang_size_sections (statement_list.head, abs_output_section, |
4402 | &statement_list.head, 0, 0, NULL, | |
cf888e70 | 4403 | command_line.relax ? FALSE : TRUE); |
b3327aad | 4404 | |
08da4cac | 4405 | /* Now run around and relax if we can. */ |
252b5132 RH |
4406 | if (command_line.relax) |
4407 | { | |
252b5132 | 4408 | /* Keep relaxing until bfd_relax_section gives up. */ |
b34976b6 | 4409 | bfd_boolean relax_again; |
b3327aad | 4410 | |
252b5132 RH |
4411 | do |
4412 | { | |
b34976b6 | 4413 | relax_again = FALSE; |
252b5132 RH |
4414 | |
4415 | /* Note: pe-dll.c does something like this also. If you find | |
4416 | you need to change this code, you probably need to change | |
08da4cac | 4417 | pe-dll.c also. DJ */ |
252b5132 RH |
4418 | |
4419 | /* Do all the assignments with our current guesses as to | |
4420 | section sizes. */ | |
1579bae1 AM |
4421 | lang_do_assignments (statement_list.head, abs_output_section, |
4422 | NULL, 0); | |
252b5132 | 4423 | |
5a46fe39 | 4424 | /* We must do this after lang_do_assignments, because it uses |
eea6121a | 4425 | size. */ |
5a46fe39 JW |
4426 | lang_reset_memory_regions (); |
4427 | ||
252b5132 | 4428 | /* Perform another relax pass - this time we know where the |
0d6d936f | 4429 | globals are, so can make a better guess. */ |
1579bae1 AM |
4430 | lang_size_sections (statement_list.head, abs_output_section, |
4431 | &statement_list.head, 0, 0, &relax_again, FALSE); | |
c7996ad6 L |
4432 | |
4433 | /* If the normal relax is done and the relax finalize pass | |
4434 | is not performed yet, we perform another relax pass. */ | |
d9c458fc | 4435 | if (!relax_again && link_info.need_relax_finalize) |
c7996ad6 | 4436 | { |
d9c458fc | 4437 | link_info.need_relax_finalize = FALSE; |
c7996ad6 L |
4438 | relax_again = TRUE; |
4439 | } | |
252b5132 RH |
4440 | } |
4441 | while (relax_again); | |
cf888e70 NC |
4442 | |
4443 | /* Final extra sizing to report errors. */ | |
1579bae1 | 4444 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
5a46fe39 | 4445 | lang_reset_memory_regions (); |
1579bae1 AM |
4446 | lang_size_sections (statement_list.head, abs_output_section, |
4447 | &statement_list.head, 0, 0, NULL, TRUE); | |
252b5132 | 4448 | } |
252b5132 RH |
4449 | |
4450 | /* See if anything special should be done now we know how big | |
4451 | everything is. */ | |
4452 | ldemul_after_allocation (); | |
4453 | ||
4454 | /* Fix any .startof. or .sizeof. symbols. */ | |
4455 | lang_set_startof (); | |
4456 | ||
08da4cac KH |
4457 | /* Do all the assignments, now that we know the final resting places |
4458 | of all the symbols. */ | |
252b5132 | 4459 | |
1579bae1 | 4460 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
252b5132 RH |
4461 | |
4462 | /* Make sure that the section addresses make sense. */ | |
1049f94e | 4463 | if (! link_info.relocatable |
252b5132 RH |
4464 | && command_line.check_section_addresses) |
4465 | lang_check_section_addresses (); | |
5f992e62 | 4466 | |
08da4cac | 4467 | /* Final stuffs. */ |
252b5132 RH |
4468 | |
4469 | ldemul_finish (); | |
4470 | lang_finish (); | |
4471 | } | |
4472 | ||
4473 | /* EXPORTED TO YACC */ | |
4474 | ||
4475 | void | |
1579bae1 AM |
4476 | lang_add_wild (struct wildcard_spec *filespec, |
4477 | struct wildcard_list *section_list, | |
4478 | bfd_boolean keep_sections) | |
252b5132 | 4479 | { |
b6bf44ba AM |
4480 | struct wildcard_list *curr, *next; |
4481 | lang_wild_statement_type *new; | |
4482 | ||
4483 | /* Reverse the list as the parser puts it back to front. */ | |
4484 | for (curr = section_list, section_list = NULL; | |
4485 | curr != NULL; | |
4486 | section_list = curr, curr = next) | |
4487 | { | |
4488 | if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0) | |
b34976b6 | 4489 | placed_commons = TRUE; |
252b5132 | 4490 | |
b6bf44ba AM |
4491 | next = curr->next; |
4492 | curr->next = section_list; | |
4493 | } | |
4494 | ||
4495 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 4496 | { |
b6bf44ba AM |
4497 | if (strcmp (filespec->name, "*") == 0) |
4498 | filespec->name = NULL; | |
4499 | else if (! wildcardp (filespec->name)) | |
b34976b6 | 4500 | lang_has_input_file = TRUE; |
252b5132 | 4501 | } |
b6bf44ba AM |
4502 | |
4503 | new = new_stat (lang_wild_statement, stat_ptr); | |
4504 | new->filename = NULL; | |
b34976b6 | 4505 | new->filenames_sorted = FALSE; |
b6bf44ba | 4506 | if (filespec != NULL) |
252b5132 | 4507 | { |
b6bf44ba | 4508 | new->filename = filespec->name; |
bcaa7b3e | 4509 | new->filenames_sorted = filespec->sorted == by_name; |
252b5132 | 4510 | } |
b6bf44ba | 4511 | new->section_list = section_list; |
252b5132 | 4512 | new->keep_sections = keep_sections; |
252b5132 RH |
4513 | lang_list_init (&new->children); |
4514 | } | |
4515 | ||
4516 | void | |
1579bae1 | 4517 | lang_section_start (const char *name, etree_type *address) |
252b5132 | 4518 | { |
d1778b88 | 4519 | lang_address_statement_type *ad; |
252b5132 | 4520 | |
d1778b88 | 4521 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
4522 | ad->section_name = name; |
4523 | ad->address = address; | |
4524 | } | |
4525 | ||
4526 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
4527 | because of a -e argument on the command line, or zero if this is | |
4528 | called by ENTRY in a linker script. Command line arguments take | |
4529 | precedence. */ | |
4530 | ||
4531 | void | |
1579bae1 | 4532 | lang_add_entry (const char *name, bfd_boolean cmdline) |
252b5132 | 4533 | { |
e3e942e9 | 4534 | if (entry_symbol.name == NULL |
252b5132 RH |
4535 | || cmdline |
4536 | || ! entry_from_cmdline) | |
4537 | { | |
e3e942e9 | 4538 | entry_symbol.name = name; |
252b5132 RH |
4539 | entry_from_cmdline = cmdline; |
4540 | } | |
4541 | } | |
4542 | ||
4543 | void | |
1579bae1 | 4544 | lang_add_target (const char *name) |
252b5132 RH |
4545 | { |
4546 | lang_target_statement_type *new = new_stat (lang_target_statement, | |
4547 | stat_ptr); | |
4548 | ||
4549 | new->target = name; | |
4550 | ||
4551 | } | |
4552 | ||
4553 | void | |
1579bae1 | 4554 | lang_add_map (const char *name) |
252b5132 RH |
4555 | { |
4556 | while (*name) | |
4557 | { | |
4558 | switch (*name) | |
4559 | { | |
08da4cac | 4560 | case 'F': |
b34976b6 | 4561 | map_option_f = TRUE; |
252b5132 RH |
4562 | break; |
4563 | } | |
4564 | name++; | |
4565 | } | |
4566 | } | |
4567 | ||
4568 | void | |
1579bae1 | 4569 | lang_add_fill (fill_type *fill) |
252b5132 RH |
4570 | { |
4571 | lang_fill_statement_type *new = new_stat (lang_fill_statement, | |
4572 | stat_ptr); | |
4573 | ||
2c382fb6 | 4574 | new->fill = fill; |
252b5132 RH |
4575 | } |
4576 | ||
4577 | void | |
1579bae1 | 4578 | lang_add_data (int type, union etree_union *exp) |
252b5132 RH |
4579 | { |
4580 | ||
4581 | lang_data_statement_type *new = new_stat (lang_data_statement, | |
4582 | stat_ptr); | |
4583 | ||
4584 | new->exp = exp; | |
4585 | new->type = type; | |
4586 | ||
4587 | } | |
4588 | ||
4589 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
4590 | generate. HOWTO is the corresponding howto structure (we could | |
4591 | look this up, but the caller has already done so). SECTION is the | |
4592 | section to generate a reloc against, or NAME is the name of the | |
4593 | symbol to generate a reloc against. Exactly one of SECTION and | |
4594 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
4595 | ||
4596 | void | |
1579bae1 AM |
4597 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
4598 | reloc_howto_type *howto, | |
4599 | asection *section, | |
4600 | const char *name, | |
4601 | union etree_union *addend) | |
252b5132 RH |
4602 | { |
4603 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 4604 | |
252b5132 RH |
4605 | p->reloc = reloc; |
4606 | p->howto = howto; | |
4607 | p->section = section; | |
4608 | p->name = name; | |
4609 | p->addend_exp = addend; | |
4610 | ||
4611 | p->addend_value = 0; | |
4612 | p->output_section = NULL; | |
4613 | p->output_vma = 0; | |
4614 | } | |
4615 | ||
4616 | lang_assignment_statement_type * | |
1579bae1 | 4617 | lang_add_assignment (etree_type *exp) |
252b5132 RH |
4618 | { |
4619 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, | |
4620 | stat_ptr); | |
4621 | ||
4622 | new->exp = exp; | |
4623 | return new; | |
4624 | } | |
4625 | ||
4626 | void | |
1579bae1 | 4627 | lang_add_attribute (enum statement_enum attribute) |
252b5132 RH |
4628 | { |
4629 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); | |
4630 | } | |
4631 | ||
4632 | void | |
1579bae1 | 4633 | lang_startup (const char *name) |
252b5132 | 4634 | { |
1579bae1 | 4635 | if (startup_file != NULL) |
252b5132 RH |
4636 | { |
4637 | einfo (_("%P%Fmultiple STARTUP files\n")); | |
4638 | } | |
4639 | first_file->filename = name; | |
4640 | first_file->local_sym_name = name; | |
b34976b6 | 4641 | first_file->real = TRUE; |
252b5132 RH |
4642 | |
4643 | startup_file = name; | |
4644 | } | |
4645 | ||
4646 | void | |
1579bae1 | 4647 | lang_float (bfd_boolean maybe) |
252b5132 RH |
4648 | { |
4649 | lang_float_flag = maybe; | |
4650 | } | |
4651 | ||
ee3cc2e2 RS |
4652 | |
4653 | /* Work out the load- and run-time regions from a script statement, and | |
4654 | store them in *LMA_REGION and *REGION respectively. | |
4655 | ||
a747ee4d NC |
4656 | MEMSPEC is the name of the run-time region, or the value of |
4657 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
4658 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
4659 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
4660 | had an explicit load address. | |
ee3cc2e2 RS |
4661 | |
4662 | It is an error to specify both a load region and a load address. */ | |
4663 | ||
4664 | static void | |
6bdafbeb NC |
4665 | lang_get_regions (lang_memory_region_type **region, |
4666 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
4667 | const char *memspec, |
4668 | const char *lma_memspec, | |
6bdafbeb NC |
4669 | bfd_boolean have_lma, |
4670 | bfd_boolean have_vma) | |
ee3cc2e2 | 4671 | { |
a747ee4d | 4672 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
ee3cc2e2 | 4673 | |
6feb9908 AM |
4674 | /* If no runtime region or VMA has been specified, but the load region |
4675 | has been specified, then use the load region for the runtime region | |
4676 | as well. */ | |
6bdafbeb NC |
4677 | if (lma_memspec != NULL |
4678 | && ! have_vma | |
4679 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) | |
ee3cc2e2 RS |
4680 | *region = *lma_region; |
4681 | else | |
a747ee4d | 4682 | *region = lang_memory_region_lookup (memspec, FALSE); |
ee3cc2e2 | 4683 | |
6bdafbeb | 4684 | if (have_lma && lma_memspec != 0) |
ee3cc2e2 RS |
4685 | einfo (_("%X%P:%S: section has both a load address and a load region\n")); |
4686 | } | |
4687 | ||
252b5132 | 4688 | void |
6bdafbeb NC |
4689 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
4690 | lang_output_section_phdr_list *phdrs, | |
4691 | const char *lma_memspec) | |
252b5132 | 4692 | { |
ee3cc2e2 RS |
4693 | lang_get_regions (¤t_section->region, |
4694 | ¤t_section->lma_region, | |
4695 | memspec, lma_memspec, | |
6bdafbeb NC |
4696 | current_section->load_base != NULL, |
4697 | current_section->addr_tree != NULL); | |
252b5132 | 4698 | current_section->fill = fill; |
252b5132 RH |
4699 | current_section->phdrs = phdrs; |
4700 | stat_ptr = &statement_list; | |
4701 | } | |
4702 | ||
08da4cac KH |
4703 | /* Create an absolute symbol with the given name with the value of the |
4704 | address of first byte of the section named. | |
4705 | ||
4706 | If the symbol already exists, then do nothing. */ | |
252b5132 | 4707 | |
252b5132 | 4708 | void |
1579bae1 | 4709 | lang_abs_symbol_at_beginning_of (const char *secname, const char *name) |
252b5132 RH |
4710 | { |
4711 | struct bfd_link_hash_entry *h; | |
4712 | ||
b34976b6 | 4713 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 4714 | if (h == NULL) |
252b5132 RH |
4715 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
4716 | ||
4717 | if (h->type == bfd_link_hash_new | |
4718 | || h->type == bfd_link_hash_undefined) | |
4719 | { | |
4720 | asection *sec; | |
4721 | ||
4722 | h->type = bfd_link_hash_defined; | |
4723 | ||
4724 | sec = bfd_get_section_by_name (output_bfd, secname); | |
1579bae1 | 4725 | if (sec == NULL) |
252b5132 RH |
4726 | h->u.def.value = 0; |
4727 | else | |
4728 | h->u.def.value = bfd_get_section_vma (output_bfd, sec); | |
4729 | ||
4730 | h->u.def.section = bfd_abs_section_ptr; | |
4731 | } | |
4732 | } | |
4733 | ||
08da4cac KH |
4734 | /* Create an absolute symbol with the given name with the value of the |
4735 | address of the first byte after the end of the section named. | |
4736 | ||
4737 | If the symbol already exists, then do nothing. */ | |
252b5132 | 4738 | |
252b5132 | 4739 | void |
1579bae1 | 4740 | lang_abs_symbol_at_end_of (const char *secname, const char *name) |
252b5132 RH |
4741 | { |
4742 | struct bfd_link_hash_entry *h; | |
4743 | ||
b34976b6 | 4744 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 4745 | if (h == NULL) |
252b5132 RH |
4746 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
4747 | ||
4748 | if (h->type == bfd_link_hash_new | |
4749 | || h->type == bfd_link_hash_undefined) | |
4750 | { | |
4751 | asection *sec; | |
4752 | ||
4753 | h->type = bfd_link_hash_defined; | |
4754 | ||
4755 | sec = bfd_get_section_by_name (output_bfd, secname); | |
1579bae1 | 4756 | if (sec == NULL) |
252b5132 RH |
4757 | h->u.def.value = 0; |
4758 | else | |
4759 | h->u.def.value = (bfd_get_section_vma (output_bfd, sec) | |
eea6121a | 4760 | + TO_ADDR (sec->size)); |
252b5132 RH |
4761 | |
4762 | h->u.def.section = bfd_abs_section_ptr; | |
4763 | } | |
4764 | } | |
4765 | ||
4766 | void | |
1579bae1 AM |
4767 | lang_statement_append (lang_statement_list_type *list, |
4768 | lang_statement_union_type *element, | |
4769 | lang_statement_union_type **field) | |
252b5132 RH |
4770 | { |
4771 | *(list->tail) = element; | |
4772 | list->tail = field; | |
4773 | } | |
4774 | ||
4775 | /* Set the output format type. -oformat overrides scripts. */ | |
4776 | ||
4777 | void | |
1579bae1 AM |
4778 | lang_add_output_format (const char *format, |
4779 | const char *big, | |
4780 | const char *little, | |
4781 | int from_script) | |
252b5132 RH |
4782 | { |
4783 | if (output_target == NULL || !from_script) | |
4784 | { | |
4785 | if (command_line.endian == ENDIAN_BIG | |
4786 | && big != NULL) | |
4787 | format = big; | |
4788 | else if (command_line.endian == ENDIAN_LITTLE | |
4789 | && little != NULL) | |
4790 | format = little; | |
4791 | ||
4792 | output_target = format; | |
4793 | } | |
4794 | } | |
4795 | ||
4796 | /* Enter a group. This creates a new lang_group_statement, and sets | |
4797 | stat_ptr to build new statements within the group. */ | |
4798 | ||
4799 | void | |
1579bae1 | 4800 | lang_enter_group (void) |
252b5132 RH |
4801 | { |
4802 | lang_group_statement_type *g; | |
4803 | ||
4804 | g = new_stat (lang_group_statement, stat_ptr); | |
4805 | lang_list_init (&g->children); | |
4806 | stat_ptr = &g->children; | |
4807 | } | |
4808 | ||
4809 | /* Leave a group. This just resets stat_ptr to start writing to the | |
4810 | regular list of statements again. Note that this will not work if | |
4811 | groups can occur inside anything else which can adjust stat_ptr, | |
4812 | but currently they can't. */ | |
4813 | ||
4814 | void | |
1579bae1 | 4815 | lang_leave_group (void) |
252b5132 RH |
4816 | { |
4817 | stat_ptr = &statement_list; | |
4818 | } | |
4819 | ||
4820 | /* Add a new program header. This is called for each entry in a PHDRS | |
4821 | command in a linker script. */ | |
4822 | ||
4823 | void | |
1579bae1 AM |
4824 | lang_new_phdr (const char *name, |
4825 | etree_type *type, | |
4826 | bfd_boolean filehdr, | |
4827 | bfd_boolean phdrs, | |
4828 | etree_type *at, | |
4829 | etree_type *flags) | |
252b5132 RH |
4830 | { |
4831 | struct lang_phdr *n, **pp; | |
4832 | ||
1579bae1 | 4833 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
4834 | n->next = NULL; |
4835 | n->name = name; | |
4836 | n->type = exp_get_value_int (type, 0, "program header type", | |
4837 | lang_final_phase_enum); | |
4838 | n->filehdr = filehdr; | |
4839 | n->phdrs = phdrs; | |
4840 | n->at = at; | |
4841 | n->flags = flags; | |
4842 | ||
4843 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4844 | ; | |
4845 | *pp = n; | |
4846 | } | |
4847 | ||
4848 | /* Record the program header information in the output BFD. FIXME: We | |
4849 | should not be calling an ELF specific function here. */ | |
4850 | ||
4851 | static void | |
1579bae1 | 4852 | lang_record_phdrs (void) |
252b5132 RH |
4853 | { |
4854 | unsigned int alc; | |
4855 | asection **secs; | |
6bdafbeb | 4856 | lang_output_section_phdr_list *last; |
252b5132 RH |
4857 | struct lang_phdr *l; |
4858 | lang_statement_union_type *u; | |
4859 | ||
4860 | alc = 10; | |
1579bae1 | 4861 | secs = xmalloc (alc * sizeof (asection *)); |
252b5132 RH |
4862 | last = NULL; |
4863 | for (l = lang_phdr_list; l != NULL; l = l->next) | |
4864 | { | |
4865 | unsigned int c; | |
4866 | flagword flags; | |
4867 | bfd_vma at; | |
4868 | ||
4869 | c = 0; | |
4870 | for (u = lang_output_section_statement.head; | |
4871 | u != NULL; | |
4872 | u = u->output_section_statement.next) | |
4873 | { | |
4874 | lang_output_section_statement_type *os; | |
6bdafbeb | 4875 | lang_output_section_phdr_list *pl; |
252b5132 RH |
4876 | |
4877 | os = &u->output_section_statement; | |
0841712e JJ |
4878 | if (os->constraint == -1) |
4879 | continue; | |
252b5132 RH |
4880 | |
4881 | pl = os->phdrs; | |
4882 | if (pl != NULL) | |
4883 | last = pl; | |
4884 | else | |
4885 | { | |
4886 | if (os->sectype == noload_section | |
4887 | || os->bfd_section == NULL | |
4888 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
4889 | continue; | |
4890 | pl = last; | |
4891 | } | |
4892 | ||
4893 | if (os->bfd_section == NULL) | |
4894 | continue; | |
4895 | ||
4896 | for (; pl != NULL; pl = pl->next) | |
4897 | { | |
4898 | if (strcmp (pl->name, l->name) == 0) | |
4899 | { | |
4900 | if (c >= alc) | |
4901 | { | |
4902 | alc *= 2; | |
1579bae1 | 4903 | secs = xrealloc (secs, alc * sizeof (asection *)); |
252b5132 RH |
4904 | } |
4905 | secs[c] = os->bfd_section; | |
4906 | ++c; | |
b34976b6 | 4907 | pl->used = TRUE; |
252b5132 RH |
4908 | } |
4909 | } | |
4910 | } | |
4911 | ||
4912 | if (l->flags == NULL) | |
4913 | flags = 0; | |
4914 | else | |
4915 | flags = exp_get_vma (l->flags, 0, "phdr flags", | |
4916 | lang_final_phase_enum); | |
4917 | ||
4918 | if (l->at == NULL) | |
4919 | at = 0; | |
4920 | else | |
4921 | at = exp_get_vma (l->at, 0, "phdr load address", | |
4922 | lang_final_phase_enum); | |
4923 | ||
4924 | if (! bfd_record_phdr (output_bfd, l->type, | |
d1778b88 | 4925 | l->flags != NULL, flags, l->at != NULL, |
252b5132 RH |
4926 | at, l->filehdr, l->phdrs, c, secs)) |
4927 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); | |
4928 | } | |
4929 | ||
4930 | free (secs); | |
4931 | ||
4932 | /* Make sure all the phdr assignments succeeded. */ | |
4933 | for (u = lang_output_section_statement.head; | |
4934 | u != NULL; | |
4935 | u = u->output_section_statement.next) | |
4936 | { | |
6bdafbeb | 4937 | lang_output_section_phdr_list *pl; |
252b5132 | 4938 | |
0841712e JJ |
4939 | if (u->output_section_statement.constraint == -1 |
4940 | || u->output_section_statement.bfd_section == NULL) | |
252b5132 RH |
4941 | continue; |
4942 | ||
4943 | for (pl = u->output_section_statement.phdrs; | |
4944 | pl != NULL; | |
4945 | pl = pl->next) | |
4946 | if (! pl->used && strcmp (pl->name, "NONE") != 0) | |
4947 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), | |
4948 | u->output_section_statement.name, pl->name); | |
4949 | } | |
4950 | } | |
4951 | ||
4952 | /* Record a list of sections which may not be cross referenced. */ | |
4953 | ||
4954 | void | |
6bdafbeb | 4955 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
4956 | { |
4957 | struct lang_nocrossrefs *n; | |
4958 | ||
1579bae1 | 4959 | n = xmalloc (sizeof *n); |
252b5132 RH |
4960 | n->next = nocrossref_list; |
4961 | n->list = l; | |
4962 | nocrossref_list = n; | |
4963 | ||
4964 | /* Set notice_all so that we get informed about all symbols. */ | |
b34976b6 | 4965 | link_info.notice_all = TRUE; |
252b5132 RH |
4966 | } |
4967 | \f | |
4968 | /* Overlay handling. We handle overlays with some static variables. */ | |
4969 | ||
4970 | /* The overlay virtual address. */ | |
4971 | static etree_type *overlay_vma; | |
7e7d5768 AM |
4972 | /* And subsection alignment. */ |
4973 | static etree_type *overlay_subalign; | |
252b5132 | 4974 | |
252b5132 RH |
4975 | /* An expression for the maximum section size seen so far. */ |
4976 | static etree_type *overlay_max; | |
4977 | ||
4978 | /* A list of all the sections in this overlay. */ | |
4979 | ||
89cdebba | 4980 | struct overlay_list { |
252b5132 RH |
4981 | struct overlay_list *next; |
4982 | lang_output_section_statement_type *os; | |
4983 | }; | |
4984 | ||
4985 | static struct overlay_list *overlay_list; | |
4986 | ||
4987 | /* Start handling an overlay. */ | |
4988 | ||
4989 | void | |
7e7d5768 | 4990 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
4991 | { |
4992 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
4993 | ASSERT (overlay_vma == NULL |
4994 | && overlay_subalign == NULL | |
4995 | && overlay_max == NULL); | |
252b5132 RH |
4996 | |
4997 | overlay_vma = vma_expr; | |
7e7d5768 | 4998 | overlay_subalign = subalign; |
252b5132 RH |
4999 | } |
5000 | ||
5001 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
5002 | lang_enter_output_section_statement with the correct VMA. |
5003 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
5004 | |
5005 | void | |
1579bae1 | 5006 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
5007 | { |
5008 | struct overlay_list *n; | |
5009 | etree_type *size; | |
5010 | ||
5011 | lang_enter_output_section_statement (name, overlay_vma, normal_section, | |
0841712e | 5012 | 0, overlay_subalign, 0, 0); |
252b5132 | 5013 | |
9f88b410 | 5014 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
5015 | sections on this one. This will work correctly even if `.' is |
5016 | used in the addresses. */ | |
5017 | if (overlay_list == NULL) | |
9f88b410 | 5018 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
5019 | |
5020 | /* Remember the section. */ | |
1579bae1 | 5021 | n = xmalloc (sizeof *n); |
252b5132 RH |
5022 | n->os = current_section; |
5023 | n->next = overlay_list; | |
5024 | overlay_list = n; | |
5025 | ||
5026 | size = exp_nameop (SIZEOF, name); | |
5027 | ||
252b5132 RH |
5028 | /* Arrange to work out the maximum section end address. */ |
5029 | if (overlay_max == NULL) | |
5030 | overlay_max = size; | |
5031 | else | |
5032 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
5033 | } | |
5034 | ||
5035 | /* Finish a section in an overlay. There isn't any special to do | |
5036 | here. */ | |
5037 | ||
5038 | void | |
1579bae1 | 5039 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 5040 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
5041 | { |
5042 | const char *name; | |
5043 | char *clean, *s2; | |
5044 | const char *s1; | |
5045 | char *buf; | |
5046 | ||
5047 | name = current_section->name; | |
5048 | ||
a747ee4d NC |
5049 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
5050 | region and that no load-time region has been specified. It doesn't | |
5051 | really matter what we say here, since lang_leave_overlay will | |
5052 | override it. */ | |
5053 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
5054 | |
5055 | /* Define the magic symbols. */ | |
5056 | ||
5057 | clean = xmalloc (strlen (name) + 1); | |
5058 | s2 = clean; | |
5059 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 5060 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
5061 | *s2++ = *s1; |
5062 | *s2 = '\0'; | |
5063 | ||
5064 | buf = xmalloc (strlen (clean) + sizeof "__load_start_"); | |
5065 | sprintf (buf, "__load_start_%s", clean); | |
5066 | lang_add_assignment (exp_assop ('=', buf, | |
5067 | exp_nameop (LOADADDR, name))); | |
5068 | ||
5069 | buf = xmalloc (strlen (clean) + sizeof "__load_stop_"); | |
5070 | sprintf (buf, "__load_stop_%s", clean); | |
5071 | lang_add_assignment (exp_assop ('=', buf, | |
5072 | exp_binop ('+', | |
5073 | exp_nameop (LOADADDR, name), | |
5074 | exp_nameop (SIZEOF, name)))); | |
5075 | ||
5076 | free (clean); | |
5077 | } | |
5078 | ||
5079 | /* Finish an overlay. If there are any overlay wide settings, this | |
5080 | looks through all the sections in the overlay and sets them. */ | |
5081 | ||
5082 | void | |
1579bae1 AM |
5083 | lang_leave_overlay (etree_type *lma_expr, |
5084 | int nocrossrefs, | |
5085 | fill_type *fill, | |
5086 | const char *memspec, | |
6bdafbeb | 5087 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 5088 | const char *lma_memspec) |
252b5132 RH |
5089 | { |
5090 | lang_memory_region_type *region; | |
562d3460 | 5091 | lang_memory_region_type *lma_region; |
252b5132 | 5092 | struct overlay_list *l; |
6bdafbeb | 5093 | lang_nocrossref_type *nocrossref; |
252b5132 | 5094 | |
ee3cc2e2 RS |
5095 | lang_get_regions (®ion, &lma_region, |
5096 | memspec, lma_memspec, | |
6bdafbeb | 5097 | lma_expr != NULL, FALSE); |
562d3460 | 5098 | |
252b5132 RH |
5099 | nocrossref = NULL; |
5100 | ||
9f88b410 RS |
5101 | /* After setting the size of the last section, set '.' to end of the |
5102 | overlay region. */ | |
5103 | if (overlay_list != NULL) | |
5104 | overlay_list->os->update_dot_tree | |
5105 | = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max)); | |
5106 | ||
252b5132 RH |
5107 | l = overlay_list; |
5108 | while (l != NULL) | |
5109 | { | |
5110 | struct overlay_list *next; | |
5111 | ||
1579bae1 | 5112 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 5113 | l->os->fill = fill; |
1545243b | 5114 | |
9f88b410 RS |
5115 | l->os->region = region; |
5116 | l->os->lma_region = lma_region; | |
5117 | ||
5118 | /* The first section has the load address specified in the | |
5119 | OVERLAY statement. The rest are worked out from that. | |
5120 | The base address is not needed (and should be null) if | |
5121 | an LMA region was specified. */ | |
5122 | if (l->next == 0) | |
5123 | l->os->load_base = lma_expr; | |
5124 | else if (lma_region == 0) | |
5125 | l->os->load_base = exp_binop ('+', | |
5126 | exp_nameop (LOADADDR, l->next->os->name), | |
5127 | exp_nameop (SIZEOF, l->next->os->name)); | |
1545243b | 5128 | |
252b5132 RH |
5129 | if (phdrs != NULL && l->os->phdrs == NULL) |
5130 | l->os->phdrs = phdrs; | |
5131 | ||
9f88b410 | 5132 | if (nocrossrefs) |
252b5132 | 5133 | { |
6bdafbeb | 5134 | lang_nocrossref_type *nc; |
252b5132 | 5135 | |
1579bae1 | 5136 | nc = xmalloc (sizeof *nc); |
252b5132 RH |
5137 | nc->name = l->os->name; |
5138 | nc->next = nocrossref; | |
5139 | nocrossref = nc; | |
5140 | } | |
5141 | ||
5142 | next = l->next; | |
5143 | free (l); | |
5144 | l = next; | |
5145 | } | |
5146 | ||
5147 | if (nocrossref != NULL) | |
5148 | lang_add_nocrossref (nocrossref); | |
5149 | ||
252b5132 | 5150 | overlay_vma = NULL; |
252b5132 RH |
5151 | overlay_list = NULL; |
5152 | overlay_max = NULL; | |
5153 | } | |
5154 | \f | |
5155 | /* Version handling. This is only useful for ELF. */ | |
5156 | ||
5157 | /* This global variable holds the version tree that we build. */ | |
5158 | ||
5159 | struct bfd_elf_version_tree *lang_elf_version_info; | |
5160 | ||
108ba305 JJ |
5161 | /* If PREV is NULL, return first version pattern matching particular symbol. |
5162 | If PREV is non-NULL, return first version pattern matching particular | |
5163 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 5164 | |
108ba305 JJ |
5165 | static struct bfd_elf_version_expr * |
5166 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
5167 | struct bfd_elf_version_expr *prev, | |
5168 | const char *sym) | |
252b5132 | 5169 | { |
108ba305 JJ |
5170 | const char *cxx_sym = sym; |
5171 | const char *java_sym = sym; | |
5172 | struct bfd_elf_version_expr *expr = NULL; | |
252b5132 | 5173 | |
108ba305 | 5174 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 5175 | { |
2f97444a | 5176 | cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI); |
108ba305 JJ |
5177 | if (!cxx_sym) |
5178 | cxx_sym = sym; | |
252b5132 | 5179 | } |
df816215 | 5180 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 5181 | { |
108ba305 JJ |
5182 | java_sym = cplus_demangle (sym, DMGL_JAVA); |
5183 | if (!java_sym) | |
5184 | java_sym = sym; | |
252b5132 RH |
5185 | } |
5186 | ||
5e35cbc2 | 5187 | if (head->htab && (prev == NULL || prev->symbol)) |
252b5132 | 5188 | { |
108ba305 JJ |
5189 | struct bfd_elf_version_expr e; |
5190 | ||
5191 | switch (prev ? prev->mask : 0) | |
5192 | { | |
5193 | case 0: | |
5194 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
5195 | { | |
5e35cbc2 | 5196 | e.symbol = sym; |
108ba305 | 5197 | expr = htab_find (head->htab, &e); |
5e35cbc2 | 5198 | while (expr && strcmp (expr->symbol, sym) == 0) |
108ba305 JJ |
5199 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
5200 | goto out_ret; | |
5201 | else | |
5202 | expr = expr->next; | |
5203 | } | |
5204 | /* Fallthrough */ | |
5205 | case BFD_ELF_VERSION_C_TYPE: | |
5206 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
5207 | { | |
5e35cbc2 | 5208 | e.symbol = cxx_sym; |
108ba305 | 5209 | expr = htab_find (head->htab, &e); |
7a995eb3 | 5210 | while (expr && strcmp (expr->symbol, cxx_sym) == 0) |
108ba305 JJ |
5211 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
5212 | goto out_ret; | |
5213 | else | |
5214 | expr = expr->next; | |
5215 | } | |
5216 | /* Fallthrough */ | |
5217 | case BFD_ELF_VERSION_CXX_TYPE: | |
5218 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
5219 | { | |
5e35cbc2 | 5220 | e.symbol = java_sym; |
108ba305 | 5221 | expr = htab_find (head->htab, &e); |
7a995eb3 | 5222 | while (expr && strcmp (expr->symbol, java_sym) == 0) |
108ba305 JJ |
5223 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
5224 | goto out_ret; | |
5225 | else | |
5226 | expr = expr->next; | |
5227 | } | |
5228 | /* Fallthrough */ | |
5229 | default: | |
5230 | break; | |
5231 | } | |
252b5132 | 5232 | } |
108ba305 JJ |
5233 | |
5234 | /* Finally, try the wildcards. */ | |
5e35cbc2 | 5235 | if (prev == NULL || prev->symbol) |
108ba305 | 5236 | expr = head->remaining; |
252b5132 | 5237 | else |
108ba305 JJ |
5238 | expr = prev->next; |
5239 | while (expr) | |
252b5132 | 5240 | { |
108ba305 JJ |
5241 | const char *s; |
5242 | ||
5243 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') | |
5244 | break; | |
5245 | ||
5246 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
5247 | s = java_sym; | |
5248 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
5249 | s = cxx_sym; | |
5250 | else | |
5251 | s = sym; | |
5e35cbc2 | 5252 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 JJ |
5253 | break; |
5254 | expr = expr->next; | |
252b5132 RH |
5255 | } |
5256 | ||
108ba305 JJ |
5257 | out_ret: |
5258 | if (cxx_sym != sym) | |
5259 | free ((char *) cxx_sym); | |
5260 | if (java_sym != sym) | |
5261 | free ((char *) java_sym); | |
5262 | return expr; | |
252b5132 RH |
5263 | } |
5264 | ||
5e35cbc2 L |
5265 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
5266 | return a string pointing to the symbol name. */ | |
5267 | ||
5268 | static const char * | |
5269 | realsymbol (const char *pattern) | |
5270 | { | |
5271 | const char *p; | |
5272 | bfd_boolean changed = FALSE, backslash = FALSE; | |
5273 | char *s, *symbol = xmalloc (strlen (pattern) + 1); | |
5274 | ||
5275 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
5276 | { | |
5277 | /* It is a glob pattern only if there is no preceding | |
5278 | backslash. */ | |
5279 | if (! backslash && (*p == '?' || *p == '*' || *p == '[')) | |
5280 | { | |
5281 | free (symbol); | |
5282 | return NULL; | |
5283 | } | |
5284 | ||
5285 | if (backslash) | |
5286 | { | |
5287 | /* Remove the preceding backslash. */ | |
5288 | *(s - 1) = *p; | |
5289 | changed = TRUE; | |
5290 | } | |
5291 | else | |
5292 | *s++ = *p; | |
5293 | ||
5294 | backslash = *p == '\\'; | |
5295 | } | |
5296 | ||
5297 | if (changed) | |
5298 | { | |
5299 | *s = '\0'; | |
5300 | return symbol; | |
5301 | } | |
5302 | else | |
5303 | { | |
5304 | free (symbol); | |
5305 | return pattern; | |
5306 | } | |
5307 | } | |
5308 | ||
252b5132 RH |
5309 | /* This is called for each variable name or match expression. */ |
5310 | ||
5311 | struct bfd_elf_version_expr * | |
1579bae1 AM |
5312 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
5313 | const char *new, | |
5314 | const char *lang) | |
252b5132 RH |
5315 | { |
5316 | struct bfd_elf_version_expr *ret; | |
5317 | ||
1579bae1 | 5318 | ret = xmalloc (sizeof *ret); |
252b5132 RH |
5319 | ret->next = orig; |
5320 | ret->pattern = new; | |
31941635 L |
5321 | ret->symver = 0; |
5322 | ret->script = 0; | |
5e35cbc2 | 5323 | ret->symbol = realsymbol (new); |
252b5132 RH |
5324 | |
5325 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 5326 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 5327 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 5328 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 5329 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 5330 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
5331 | else |
5332 | { | |
5333 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
5334 | lang); | |
108ba305 | 5335 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
5336 | } |
5337 | ||
fac1652d | 5338 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
5339 | } |
5340 | ||
5341 | /* This is called for each set of variable names and match | |
5342 | expressions. */ | |
5343 | ||
5344 | struct bfd_elf_version_tree * | |
1579bae1 AM |
5345 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
5346 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
5347 | { |
5348 | struct bfd_elf_version_tree *ret; | |
5349 | ||
108ba305 JJ |
5350 | ret = xcalloc (1, sizeof *ret); |
5351 | ret->globals.list = globals; | |
5352 | ret->locals.list = locals; | |
5353 | ret->match = lang_vers_match; | |
252b5132 | 5354 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
5355 | return ret; |
5356 | } | |
5357 | ||
5358 | /* This static variable keeps track of version indices. */ | |
5359 | ||
5360 | static int version_index; | |
5361 | ||
108ba305 JJ |
5362 | static hashval_t |
5363 | version_expr_head_hash (const void *p) | |
5364 | { | |
5365 | const struct bfd_elf_version_expr *e = p; | |
5366 | ||
5e35cbc2 | 5367 | return htab_hash_string (e->symbol); |
108ba305 JJ |
5368 | } |
5369 | ||
5370 | static int | |
5371 | version_expr_head_eq (const void *p1, const void *p2) | |
5372 | { | |
5373 | const struct bfd_elf_version_expr *e1 = p1; | |
5374 | const struct bfd_elf_version_expr *e2 = p2; | |
5375 | ||
5e35cbc2 | 5376 | return strcmp (e1->symbol, e2->symbol) == 0; |
108ba305 JJ |
5377 | } |
5378 | ||
5379 | static void | |
5380 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
5381 | { | |
5382 | size_t count = 0; | |
5383 | struct bfd_elf_version_expr *e, *next; | |
5384 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
5385 | ||
5386 | for (e = head->list; e; e = e->next) | |
5387 | { | |
5e35cbc2 | 5388 | if (e->symbol) |
108ba305 JJ |
5389 | count++; |
5390 | head->mask |= e->mask; | |
5391 | } | |
5392 | ||
5393 | if (count) | |
5394 | { | |
5395 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
5396 | version_expr_head_eq, NULL); | |
5397 | list_loc = &head->list; | |
5398 | remaining_loc = &head->remaining; | |
5399 | for (e = head->list; e; e = next) | |
5400 | { | |
5401 | next = e->next; | |
5e35cbc2 | 5402 | if (!e->symbol) |
108ba305 JJ |
5403 | { |
5404 | *remaining_loc = e; | |
5405 | remaining_loc = &e->next; | |
5406 | } | |
5407 | else | |
5408 | { | |
5409 | void **loc = htab_find_slot (head->htab, e, INSERT); | |
5410 | ||
5411 | if (*loc) | |
5412 | { | |
5413 | struct bfd_elf_version_expr *e1, *last; | |
5414 | ||
5415 | e1 = *loc; | |
5416 | last = NULL; | |
5417 | do | |
5418 | { | |
5419 | if (e1->mask == e->mask) | |
5420 | { | |
5421 | last = NULL; | |
5422 | break; | |
5423 | } | |
5424 | last = e1; | |
5425 | e1 = e1->next; | |
5426 | } | |
5e35cbc2 | 5427 | while (e1 && strcmp (e1->symbol, e->symbol) == 0); |
108ba305 JJ |
5428 | |
5429 | if (last == NULL) | |
5430 | { | |
5431 | /* This is a duplicate. */ | |
5432 | /* FIXME: Memory leak. Sometimes pattern is not | |
5433 | xmalloced alone, but in larger chunk of memory. */ | |
5e35cbc2 | 5434 | /* free (e->symbol); */ |
108ba305 JJ |
5435 | free (e); |
5436 | } | |
5437 | else | |
5438 | { | |
5439 | e->next = last->next; | |
5440 | last->next = e; | |
5441 | } | |
5442 | } | |
5443 | else | |
5444 | { | |
5445 | *loc = e; | |
5446 | *list_loc = e; | |
5447 | list_loc = &e->next; | |
5448 | } | |
5449 | } | |
5450 | } | |
5451 | *remaining_loc = NULL; | |
5452 | *list_loc = head->remaining; | |
5453 | } | |
5454 | else | |
5455 | head->remaining = head->list; | |
5456 | } | |
5457 | ||
252b5132 RH |
5458 | /* This is called when we know the name and dependencies of the |
5459 | version. */ | |
5460 | ||
5461 | void | |
1579bae1 AM |
5462 | lang_register_vers_node (const char *name, |
5463 | struct bfd_elf_version_tree *version, | |
5464 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
5465 | { |
5466 | struct bfd_elf_version_tree *t, **pp; | |
5467 | struct bfd_elf_version_expr *e1; | |
5468 | ||
6b9b879a JJ |
5469 | if (name == NULL) |
5470 | name = ""; | |
5471 | ||
5472 | if ((name[0] == '\0' && lang_elf_version_info != NULL) | |
5473 | || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0')) | |
5474 | { | |
6feb9908 AM |
5475 | einfo (_("%X%P: anonymous version tag cannot be combined" |
5476 | " with other version tags\n")); | |
5ed6aba4 | 5477 | free (version); |
6b9b879a JJ |
5478 | return; |
5479 | } | |
5480 | ||
252b5132 RH |
5481 | /* Make sure this node has a unique name. */ |
5482 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5483 | if (strcmp (t->name, name) == 0) | |
5484 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
5485 | ||
108ba305 JJ |
5486 | lang_finalize_version_expr_head (&version->globals); |
5487 | lang_finalize_version_expr_head (&version->locals); | |
5488 | ||
252b5132 RH |
5489 | /* Check the global and local match names, and make sure there |
5490 | aren't any duplicates. */ | |
5491 | ||
108ba305 | 5492 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
5493 | { |
5494 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5495 | { | |
5496 | struct bfd_elf_version_expr *e2; | |
5497 | ||
5e35cbc2 | 5498 | if (t->locals.htab && e1->symbol) |
108ba305 JJ |
5499 | { |
5500 | e2 = htab_find (t->locals.htab, e1); | |
5e35cbc2 | 5501 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
108ba305 JJ |
5502 | { |
5503 | if (e1->mask == e2->mask) | |
6feb9908 AM |
5504 | einfo (_("%X%P: duplicate expression `%s'" |
5505 | " in version information\n"), e1->symbol); | |
108ba305 JJ |
5506 | e2 = e2->next; |
5507 | } | |
5508 | } | |
5e35cbc2 | 5509 | else if (!e1->symbol) |
108ba305 | 5510 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
5511 | if (strcmp (e1->pattern, e2->pattern) == 0 |
5512 | && e1->mask == e2->mask) | |
5513 | einfo (_("%X%P: duplicate expression `%s'" | |
5514 | " in version information\n"), e1->pattern); | |
252b5132 RH |
5515 | } |
5516 | } | |
5517 | ||
108ba305 | 5518 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
5519 | { |
5520 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5521 | { | |
5522 | struct bfd_elf_version_expr *e2; | |
5523 | ||
5e35cbc2 | 5524 | if (t->globals.htab && e1->symbol) |
108ba305 JJ |
5525 | { |
5526 | e2 = htab_find (t->globals.htab, e1); | |
5e35cbc2 | 5527 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
108ba305 JJ |
5528 | { |
5529 | if (e1->mask == e2->mask) | |
6feb9908 AM |
5530 | einfo (_("%X%P: duplicate expression `%s'" |
5531 | " in version information\n"), | |
5e35cbc2 | 5532 | e1->symbol); |
108ba305 JJ |
5533 | e2 = e2->next; |
5534 | } | |
5535 | } | |
5e35cbc2 | 5536 | else if (!e1->symbol) |
108ba305 | 5537 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
5538 | if (strcmp (e1->pattern, e2->pattern) == 0 |
5539 | && e1->mask == e2->mask) | |
5540 | einfo (_("%X%P: duplicate expression `%s'" | |
5541 | " in version information\n"), e1->pattern); | |
252b5132 RH |
5542 | } |
5543 | } | |
5544 | ||
5545 | version->deps = deps; | |
5546 | version->name = name; | |
6b9b879a JJ |
5547 | if (name[0] != '\0') |
5548 | { | |
5549 | ++version_index; | |
5550 | version->vernum = version_index; | |
5551 | } | |
5552 | else | |
5553 | version->vernum = 0; | |
252b5132 RH |
5554 | |
5555 | for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next) | |
5556 | ; | |
5557 | *pp = version; | |
5558 | } | |
5559 | ||
5560 | /* This is called when we see a version dependency. */ | |
5561 | ||
5562 | struct bfd_elf_version_deps * | |
1579bae1 | 5563 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
5564 | { |
5565 | struct bfd_elf_version_deps *ret; | |
5566 | struct bfd_elf_version_tree *t; | |
5567 | ||
1579bae1 | 5568 | ret = xmalloc (sizeof *ret); |
252b5132 RH |
5569 | ret->next = list; |
5570 | ||
5571 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5572 | { | |
5573 | if (strcmp (t->name, name) == 0) | |
5574 | { | |
5575 | ret->version_needed = t; | |
5576 | return ret; | |
5577 | } | |
5578 | } | |
5579 | ||
5580 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
5581 | ||
5582 | return ret; | |
5583 | } | |
5584 | ||
5585 | static void | |
1579bae1 | 5586 | lang_do_version_exports_section (void) |
252b5132 RH |
5587 | { |
5588 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
5589 | ||
5590 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
5591 | { | |
5592 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
5593 | char *contents, *p; | |
5594 | bfd_size_type len; | |
5595 | ||
5596 | if (sec == NULL) | |
b7a26f91 | 5597 | continue; |
252b5132 | 5598 | |
eea6121a | 5599 | len = sec->size; |
252b5132 RH |
5600 | contents = xmalloc (len); |
5601 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) | |
6f9efd97 | 5602 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
5603 | |
5604 | p = contents; | |
89cdebba | 5605 | while (p < contents + len) |
252b5132 | 5606 | { |
313e35ee | 5607 | greg = lang_new_vers_pattern (greg, p, NULL); |
252b5132 RH |
5608 | p = strchr (p, '\0') + 1; |
5609 | } | |
5610 | ||
5611 | /* Do not free the contents, as we used them creating the regex. */ | |
5612 | ||
5613 | /* Do not include this section in the link. */ | |
6feb9908 | 5614 | sec->flags |= SEC_EXCLUDE; |
252b5132 RH |
5615 | } |
5616 | ||
313e35ee | 5617 | lreg = lang_new_vers_pattern (NULL, "*", NULL); |
252b5132 RH |
5618 | lang_register_vers_node (command_line.version_exports_section, |
5619 | lang_new_vers_node (greg, lreg), NULL); | |
5620 | } | |
577a0623 AM |
5621 | |
5622 | void | |
1579bae1 | 5623 | lang_add_unique (const char *name) |
577a0623 AM |
5624 | { |
5625 | struct unique_sections *ent; | |
5626 | ||
5627 | for (ent = unique_section_list; ent; ent = ent->next) | |
5628 | if (strcmp (ent->name, name) == 0) | |
5629 | return; | |
5630 | ||
1579bae1 | 5631 | ent = xmalloc (sizeof *ent); |
577a0623 AM |
5632 | ent->name = xstrdup (name); |
5633 | ent->next = unique_section_list; | |
5634 | unique_section_list = ent; | |
5635 | } |