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