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