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