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