]>
Commit | Line | Data |
---|---|---|
e20873a7 | 1 | /* Linker command language support. |
307585f1 | 2 | Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. |
075d7359 | 3 | |
2fa0b342 DHW |
4 | This file is part of GLD, the Gnu Linker. |
5 | ||
6 | GLD is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 1, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GLD is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GLD; see the file COPYING. If not, write to | |
943fbd5b | 18 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
2fa0b342 | 19 | |
2fa0b342 | 20 | #include "bfd.h" |
075d7359 | 21 | #include "sysdep.h" |
4c2123b6 | 22 | #include "libiberty.h" |
c477527c | 23 | #include "bfdlink.h" |
2fa0b342 DHW |
24 | |
25 | #include "ld.h" | |
fcf276c4 | 26 | #include "ldmain.h" |
7bc4a0d7 | 27 | #include "ldgram.h" |
2fa0b342 | 28 | #include "ldexp.h" |
fcf276c4 | 29 | #include "ldlang.h" |
8c514453 | 30 | #include "ldemul.h" |
2fa0b342 | 31 | #include "ldlex.h" |
3f8d46e7 | 32 | #include "ldmisc.h" |
c611e285 | 33 | #include "ldctor.h" |
fcf276c4 | 34 | #include "ldfile.h" |
b98bd3db | 35 | |
2fa0b342 | 36 | /* FORWARDS */ |
5af8e9d6 KR |
37 | static lang_statement_union_type *new_statement PARAMS ((enum statement_enum, |
38 | size_t, | |
39 | lang_statement_list_type*)); | |
40 | ||
2fa0b342 | 41 | |
d4c02e29 | 42 | /* LOCALS */ |
bfbdc80f | 43 | static struct obstack stat_obstack; |
075d7359 | 44 | |
0b2f8d2e | 45 | #define obstack_chunk_alloc xmalloc |
bfbdc80f | 46 | #define obstack_chunk_free free |
075d7359 | 47 | static CONST char *startup_file; |
d4c02e29 SC |
48 | static lang_statement_list_type input_file_chain; |
49 | static boolean placed_commons = false; | |
50 | static lang_output_section_statement_type *default_common_section; | |
51 | static boolean map_option_f; | |
52 | static bfd_vma print_dot; | |
53 | static lang_input_statement_type *first_file; | |
54 | static lang_statement_list_type lang_output_section_statement; | |
55 | static CONST char *current_target; | |
56 | static CONST char *output_target; | |
fcf276c4 | 57 | static int longest_section_name = 8; |
d4c02e29 | 58 | static lang_statement_list_type statement_list; |
061e65f8 | 59 | static struct lang_phdr *lang_phdr_list; |
ffc50032 | 60 | |
9f629407 ILT |
61 | static void print_size PARAMS ((size_t value)); |
62 | static void print_alignment PARAMS ((unsigned int value)); | |
63 | static void print_fill PARAMS ((fill_type value)); | |
64 | static void print_section PARAMS ((const char *name)); | |
65 | static void lang_for_each_statement_worker | |
66 | PARAMS ((void (*func) (lang_statement_union_type *), | |
67 | lang_statement_union_type *s)); | |
68 | static lang_input_statement_type *new_afile | |
69 | PARAMS ((const char *name, lang_input_file_enum_type file_type, | |
193c5f93 | 70 | const char *target, boolean add_to_list)); |
9f629407 ILT |
71 | static void print_flags PARAMS ((int *ignore_flags)); |
72 | static void init_os PARAMS ((lang_output_section_statement_type *s)); | |
9f629407 ILT |
73 | static void wild_section PARAMS ((lang_wild_statement_type *ptr, |
74 | const char *section, | |
75 | lang_input_statement_type *file, | |
76 | lang_output_section_statement_type *output)); | |
193c5f93 | 77 | static lang_input_statement_type *lookup_name PARAMS ((const char *name)); |
6a02a973 ILT |
78 | static void load_symbols PARAMS ((lang_input_statement_type *entry, |
79 | lang_statement_list_type *)); | |
9f629407 ILT |
80 | static void wild PARAMS ((lang_wild_statement_type *s, |
81 | const char *section, const char *file, | |
82 | const char *target, | |
83 | lang_output_section_statement_type *output)); | |
84 | static bfd *open_output PARAMS ((const char *name)); | |
85 | static void ldlang_open_output PARAMS ((lang_statement_union_type *statement)); | |
309c8153 ILT |
86 | static void open_input_bfds |
87 | PARAMS ((lang_statement_union_type *statement, boolean)); | |
9f629407 ILT |
88 | static void lang_reasonable_defaults PARAMS ((void)); |
89 | static void lang_place_undefineds PARAMS ((void)); | |
9f629407 ILT |
90 | static void map_input_to_output_sections |
91 | PARAMS ((lang_statement_union_type *s, | |
92 | const char *target, | |
93 | lang_output_section_statement_type *output_section_statement)); | |
94 | static void print_output_section_statement | |
95 | PARAMS ((lang_output_section_statement_type *output_section_statement)); | |
96 | static void print_assignment | |
97 | PARAMS ((lang_assignment_statement_type *assignment, | |
98 | lang_output_section_statement_type *output_section)); | |
99 | static void print_input_statement PARAMS ((lang_input_statement_type *statm)); | |
9f629407 ILT |
100 | static void print_input_section PARAMS ((lang_input_section_type *in)); |
101 | static void print_fill_statement PARAMS ((lang_fill_statement_type *fill)); | |
102 | static void print_data_statement PARAMS ((lang_data_statement_type *data)); | |
4fdbafb2 | 103 | static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc)); |
9f629407 ILT |
104 | static void print_padding_statement PARAMS ((lang_padding_statement_type *s)); |
105 | static void print_wild_statement | |
106 | PARAMS ((lang_wild_statement_type *w, | |
107 | lang_output_section_statement_type *os)); | |
309c8153 ILT |
108 | static void print_group |
109 | PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *)); | |
9f629407 ILT |
110 | static void print_statement PARAMS ((lang_statement_union_type *s, |
111 | lang_output_section_statement_type *os)); | |
708f7fbc ILT |
112 | static void print_statement_list PARAMS ((lang_statement_union_type *s, |
113 | lang_output_section_statement_type *os)); | |
9f629407 ILT |
114 | static void print_statements PARAMS ((void)); |
115 | static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr, | |
116 | fill_type fill, unsigned int power, | |
117 | asection *output_section_statement, | |
118 | bfd_vma dot)); | |
119 | static bfd_vma size_input_section | |
120 | PARAMS ((lang_statement_union_type **this_ptr, | |
121 | lang_output_section_statement_type *output_section_statement, | |
c477527c | 122 | fill_type fill, bfd_vma dot, boolean relax)); |
9f629407 ILT |
123 | static void lang_finish PARAMS ((void)); |
124 | static void lang_check PARAMS ((void)); | |
125 | static void lang_common PARAMS ((void)); | |
c477527c | 126 | static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR)); |
9f629407 ILT |
127 | static void lang_place_orphans PARAMS ((void)); |
128 | static int topower PARAMS ((int)); | |
1d169acc | 129 | static void lang_set_startof PARAMS ((void)); |
9f629407 | 130 | static void reset_memory_regions PARAMS ((void)); |
061e65f8 | 131 | static void lang_record_phdrs PARAMS ((void)); |
9f629407 | 132 | |
2fa0b342 | 133 | /* EXPORTS */ |
ffc50032 | 134 | lang_output_section_statement_type *abs_output_section; |
d4c02e29 | 135 | lang_statement_list_type *stat_ptr = &statement_list; |
a4d2a48e | 136 | lang_statement_list_type file_chain = { 0 }; |
061e65f8 | 137 | const char *entry_symbol = NULL; |
f054ce20 | 138 | boolean entry_from_cmdline; |
d4c02e29 | 139 | boolean lang_has_input_file = false; |
d4c02e29 SC |
140 | boolean had_output_filename = false; |
141 | boolean lang_float_flag = false; | |
97fbbaca | 142 | boolean delete_output_file_on_failure = false; |
075d7359 | 143 | |
9fce28ed SC |
144 | etree_type *base; /* Relocation base - or null */ |
145 | ||
146 | ||
b897ed67 | 147 | #if defined(__STDC__) || defined(ALMOST_STDC) |
2fa0b342 DHW |
148 | #define cat(a,b) a##b |
149 | #else | |
150 | #define cat(a,b) a/**/b | |
151 | #endif | |
152 | ||
153 | #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y) | |
154 | ||
155 | #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma) | |
156 | ||
157 | #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section)) | |
158 | ||
bfbdc80f | 159 | PTR |
8ddef552 DM |
160 | stat_alloc (size) |
161 | size_t size; | |
bfbdc80f | 162 | { |
075d7359 | 163 | return obstack_alloc (&stat_obstack, size); |
bfbdc80f | 164 | } |
9f629407 | 165 | |
075d7359 | 166 | static void |
8ddef552 DM |
167 | print_size (value) |
168 | size_t value; | |
3f8d46e7 | 169 | { |
075d7359 | 170 | fprintf (config.map_file, "%5x", (unsigned) value); |
3f8d46e7 | 171 | } |
9f629407 | 172 | |
075d7359 | 173 | static void |
8ddef552 DM |
174 | print_alignment (value) |
175 | unsigned int value; | |
3f8d46e7 | 176 | { |
075d7359 | 177 | fprintf (config.map_file, "2**%1u", value); |
3f8d46e7 | 178 | } |
9f629407 | 179 | |
075d7359 | 180 | static void |
9f629407 ILT |
181 | print_fill (value) |
182 | fill_type value; | |
3f8d46e7 | 183 | { |
075d7359 | 184 | fprintf (config.map_file, "%04x", (unsigned) value); |
3f8d46e7 JG |
185 | } |
186 | ||
075d7359 | 187 | static void |
8ddef552 | 188 | print_section (name) |
9f629407 | 189 | CONST char *name; |
3f8d46e7 | 190 | { |
075d7359 | 191 | fprintf (config.map_file, "%*s", -longest_section_name, name); |
3f8d46e7 | 192 | } |
2fa0b342 | 193 | |
1418c83b SC |
194 | /*---------------------------------------------------------------------- |
195 | lang_for_each_statement walks the parse tree and calls the provided | |
196 | function for each node | |
197 | */ | |
198 | ||
075d7359 | 199 | static void |
8ddef552 | 200 | lang_for_each_statement_worker (func, s) |
c477527c | 201 | void (*func) PARAMS ((lang_statement_union_type *)); |
8ddef552 | 202 | lang_statement_union_type *s; |
1418c83b | 203 | { |
075d7359 SC |
204 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
205 | { | |
206 | func (s); | |
1418c83b | 207 | |
075d7359 SC |
208 | switch (s->header.type) |
209 | { | |
81016051 | 210 | case lang_constructors_statement_enum: |
075d7359 | 211 | lang_for_each_statement_worker (func, constructor_list.head); |
81016051 | 212 | break; |
1418c83b SC |
213 | case lang_output_section_statement_enum: |
214 | lang_for_each_statement_worker | |
075d7359 | 215 | (func, |
1418c83b SC |
216 | s->output_section_statement.children.head); |
217 | break; | |
218 | case lang_wild_statement_enum: | |
219 | lang_for_each_statement_worker | |
075d7359 | 220 | (func, |
1418c83b SC |
221 | s->wild_statement.children.head); |
222 | break; | |
309c8153 ILT |
223 | case lang_group_statement_enum: |
224 | lang_for_each_statement_worker (func, | |
225 | s->group_statement.children.head); | |
226 | break; | |
1418c83b | 227 | case lang_data_statement_enum: |
4fdbafb2 | 228 | case lang_reloc_statement_enum: |
1418c83b SC |
229 | case lang_object_symbols_statement_enum: |
230 | case lang_output_statement_enum: | |
231 | case lang_target_statement_enum: | |
232 | case lang_input_section_enum: | |
233 | case lang_input_statement_enum: | |
1418c83b SC |
234 | case lang_assignment_statement_enum: |
235 | case lang_padding_statement_enum: | |
236 | case lang_address_statement_enum: | |
237 | break; | |
238 | default: | |
075d7359 | 239 | FAIL (); |
1418c83b SC |
240 | break; |
241 | } | |
075d7359 | 242 | } |
1418c83b SC |
243 | } |
244 | ||
245 | void | |
8ddef552 | 246 | lang_for_each_statement (func) |
c477527c | 247 | void (*func) PARAMS ((lang_statement_union_type *)); |
1418c83b | 248 | { |
075d7359 SC |
249 | lang_for_each_statement_worker (func, |
250 | statement_list.head); | |
1418c83b | 251 | } |
075d7359 | 252 | |
1418c83b | 253 | /*----------------------------------------------------------------------*/ |
075d7359 | 254 | void |
8ddef552 DM |
255 | lang_list_init (list) |
256 | lang_statement_list_type *list; | |
2fa0b342 | 257 | { |
075d7359 SC |
258 | list->head = (lang_statement_union_type *) NULL; |
259 | list->tail = &list->head; | |
2fa0b342 DHW |
260 | } |
261 | ||
1418c83b | 262 | /*---------------------------------------------------------------------- |
075d7359 | 263 | |
1418c83b SC |
264 | build a new statement node for the parse tree |
265 | ||
266 | */ | |
2fa0b342 DHW |
267 | |
268 | static | |
075d7359 | 269 | lang_statement_union_type * |
8ddef552 DM |
270 | new_statement (type, size, list) |
271 | enum statement_enum type; | |
5af8e9d6 | 272 | size_t size; |
8ddef552 | 273 | lang_statement_list_type * list; |
2fa0b342 DHW |
274 | { |
275 | lang_statement_union_type *new = (lang_statement_union_type *) | |
075d7359 SC |
276 | stat_alloc (size); |
277 | ||
2fa0b342 | 278 | new->header.type = type; |
075d7359 SC |
279 | new->header.next = (lang_statement_union_type *) NULL; |
280 | lang_statement_append (list, new, &new->header.next); | |
2fa0b342 DHW |
281 | return new; |
282 | } | |
283 | ||
1418c83b SC |
284 | /* |
285 | Build a new input file node for the language. There are several ways | |
286 | in which we treat an input file, eg, we only look at symbols, or | |
287 | prefix it with a -l etc. | |
288 | ||
289 | We can be supplied with requests for input files more than once; | |
290 | they may, for example be split over serveral lines like foo.o(.text) | |
291 | foo.o(.data) etc, so when asked for a file we check that we havn't | |
292 | got it already so we don't duplicate the bfd. | |
293 | ||
294 | */ | |
2fa0b342 | 295 | static lang_input_statement_type * |
193c5f93 | 296 | new_afile (name, file_type, target, add_to_list) |
9f629407 ILT |
297 | CONST char *name; |
298 | lang_input_file_enum_type file_type; | |
299 | CONST char *target; | |
193c5f93 | 300 | boolean add_to_list; |
2fa0b342 | 301 | { |
193c5f93 | 302 | lang_input_statement_type *p; |
dc4726c2 | 303 | |
193c5f93 ILT |
304 | if (add_to_list) |
305 | p = new_stat (lang_input_statement, stat_ptr); | |
306 | else | |
307 | { | |
308 | p = ((lang_input_statement_type *) | |
309 | stat_alloc (sizeof (lang_input_statement_type))); | |
310 | p->header.next = NULL; | |
311 | } | |
075d7359 | 312 | |
2fa0b342 DHW |
313 | lang_has_input_file = true; |
314 | p->target = target; | |
ee4af9e8 | 315 | p->complained = false; |
075d7359 SC |
316 | switch (file_type) |
317 | { | |
318 | case lang_input_file_is_symbols_only_enum: | |
319 | p->filename = name; | |
320 | p->is_archive = false; | |
321 | p->real = true; | |
322 | p->local_sym_name = name; | |
323 | p->just_syms_flag = true; | |
324 | p->search_dirs_flag = false; | |
325 | break; | |
326 | case lang_input_file_is_fake_enum: | |
327 | p->filename = name; | |
328 | p->is_archive = false; | |
329 | p->real = false; | |
330 | p->local_sym_name = name; | |
331 | p->just_syms_flag = false; | |
332 | p->search_dirs_flag = false; | |
333 | break; | |
334 | case lang_input_file_is_l_enum: | |
335 | p->is_archive = true; | |
336 | p->filename = name; | |
337 | p->real = true; | |
ef76742f | 338 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
075d7359 SC |
339 | p->just_syms_flag = false; |
340 | p->search_dirs_flag = true; | |
341 | break; | |
075d7359 | 342 | case lang_input_file_is_marker_enum: |
193c5f93 ILT |
343 | p->filename = name; |
344 | p->is_archive = false; | |
345 | p->real = false; | |
346 | p->local_sym_name = name; | |
347 | p->just_syms_flag = false; | |
348 | p->search_dirs_flag = true; | |
349 | break; | |
350 | case lang_input_file_is_search_file_enum: | |
075d7359 SC |
351 | p->filename = name; |
352 | p->is_archive = false; | |
353 | p->real = true; | |
354 | p->local_sym_name = name; | |
355 | p->just_syms_flag = false; | |
356 | p->search_dirs_flag = true; | |
357 | break; | |
358 | case lang_input_file_is_file_enum: | |
359 | p->filename = name; | |
360 | p->is_archive = false; | |
361 | p->real = true; | |
362 | p->local_sym_name = name; | |
363 | p->just_syms_flag = false; | |
364 | p->search_dirs_flag = false; | |
365 | break; | |
366 | default: | |
367 | FAIL (); | |
368 | } | |
193c5f93 | 369 | p->the_bfd = (bfd *) NULL; |
075d7359 | 370 | p->asymbols = (asymbol **) NULL; |
075d7359 SC |
371 | p->next_real_file = (lang_statement_union_type *) NULL; |
372 | p->next = (lang_statement_union_type *) NULL; | |
2fa0b342 | 373 | p->symbol_count = 0; |
075d7359 | 374 | p->common_output_section = (asection *) NULL; |
1d169acc | 375 | p->dynamic = config.dynamic_link; |
5e6cd559 | 376 | p->loaded = false; |
075d7359 SC |
377 | lang_statement_append (&input_file_chain, |
378 | (lang_statement_union_type *) p, | |
379 | &p->next_real_file); | |
2fa0b342 DHW |
380 | return p; |
381 | } | |
382 | ||
383 | lang_input_statement_type * | |
8ddef552 DM |
384 | lang_add_input_file (name, file_type, target) |
385 | CONST char *name; | |
386 | lang_input_file_enum_type file_type; | |
387 | CONST char *target; | |
2fa0b342 | 388 | { |
1418c83b | 389 | lang_has_input_file = true; |
193c5f93 | 390 | return new_afile (name, file_type, target, true); |
2fa0b342 DHW |
391 | } |
392 | ||
1418c83b | 393 | /* Build enough state so that the parser can build its tree */ |
2fa0b342 | 394 | void |
8ddef552 | 395 | lang_init () |
2fa0b342 | 396 | { |
075d7359 | 397 | obstack_begin (&stat_obstack, 1000); |
2fa0b342 | 398 | |
075d7359 | 399 | stat_ptr = &statement_list; |
bfbdc80f | 400 | |
075d7359 | 401 | lang_list_init (stat_ptr); |
2fa0b342 | 402 | |
075d7359 SC |
403 | lang_list_init (&input_file_chain); |
404 | lang_list_init (&lang_output_section_statement); | |
405 | lang_list_init (&file_chain); | |
406 | first_file = lang_add_input_file ((char *) NULL, | |
407 | lang_input_file_is_marker_enum, | |
408 | (char *) NULL); | |
409 | abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); | |
9d1fe8a4 | 410 | |
686739e2 | 411 | abs_output_section->bfd_section = bfd_abs_section_ptr; |
9d1fe8a4 | 412 | |
2fa0b342 DHW |
413 | } |
414 | ||
1418c83b | 415 | /*---------------------------------------------------------------------- |
075d7359 SC |
416 | A region is an area of memory declared with the |
417 | MEMORY { name:org=exp, len=exp ... } | |
418 | syntax. | |
2fa0b342 | 419 | |
1418c83b | 420 | We maintain a list of all the regions here |
2fa0b342 | 421 | |
1418c83b SC |
422 | If no regions are specified in the script, then the default is used |
423 | which is created when looked up to be the entire data space | |
2fa0b342 DHW |
424 | */ |
425 | ||
426 | static lang_memory_region_type *lang_memory_region_list; | |
427 | static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; | |
428 | ||
429 | lang_memory_region_type * | |
8ddef552 DM |
430 | lang_memory_region_lookup (name) |
431 | CONST char *CONST name; | |
2fa0b342 DHW |
432 | { |
433 | ||
075d7359 SC |
434 | lang_memory_region_type *p = lang_memory_region_list; |
435 | ||
1418c83b | 436 | for (p = lang_memory_region_list; |
075d7359 SC |
437 | p != (lang_memory_region_type *) NULL; |
438 | p = p->next) | |
439 | { | |
440 | if (strcmp (p->name, name) == 0) | |
441 | { | |
442 | return p; | |
443 | } | |
2fa0b342 | 444 | } |
36ea6198 ILT |
445 | |
446 | #if 0 | |
447 | /* This code used to always use the first region in the list as the | |
448 | default region. I changed it to instead use a region | |
449 | encompassing all of memory as the default region. This permits | |
450 | NOLOAD sections to work reasonably without requiring a region. | |
451 | People should specify what region they mean, if they really want | |
452 | a region. */ | |
075d7359 | 453 | if (strcmp (name, "*default*") == 0) |
2fa0b342 | 454 | { |
075d7359 SC |
455 | if (lang_memory_region_list != (lang_memory_region_type *) NULL) |
456 | { | |
457 | return lang_memory_region_list; | |
458 | } | |
2fa0b342 | 459 | } |
36ea6198 ILT |
460 | #endif |
461 | ||
075d7359 SC |
462 | { |
463 | lang_memory_region_type *new = | |
1d169acc | 464 | (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type)); |
075d7359 SC |
465 | |
466 | new->name = buystring (name); | |
467 | new->next = (lang_memory_region_type *) NULL; | |
468 | ||
469 | *lang_memory_region_list_tail = new; | |
470 | lang_memory_region_list_tail = &new->next; | |
471 | new->origin = 0; | |
8ddef552 | 472 | new->length = ~(bfd_size_type)0; |
075d7359 SC |
473 | new->current = 0; |
474 | new->had_full_message = false; | |
475 | ||
476 | return new; | |
477 | } | |
2fa0b342 DHW |
478 | } |
479 | ||
480 | ||
2fa0b342 | 481 | lang_output_section_statement_type * |
8ddef552 DM |
482 | lang_output_section_find (name) |
483 | CONST char *CONST name; | |
2fa0b342 DHW |
484 | { |
485 | lang_statement_union_type *u; | |
486 | lang_output_section_statement_type *lookup; | |
487 | ||
488 | for (u = lang_output_section_statement.head; | |
075d7359 | 489 | u != (lang_statement_union_type *) NULL; |
2fa0b342 | 490 | u = lookup->next) |
075d7359 SC |
491 | { |
492 | lookup = &u->output_section_statement; | |
493 | if (strcmp (name, lookup->name) == 0) | |
494 | { | |
1418c83b SC |
495 | return lookup; |
496 | } | |
075d7359 SC |
497 | } |
498 | return (lang_output_section_statement_type *) NULL; | |
2fa0b342 DHW |
499 | } |
500 | ||
501 | lang_output_section_statement_type * | |
8ddef552 DM |
502 | lang_output_section_statement_lookup (name) |
503 | CONST char *CONST name; | |
2fa0b342 DHW |
504 | { |
505 | lang_output_section_statement_type *lookup; | |
2fa0b342 | 506 | |
075d7359 SC |
507 | lookup = lang_output_section_find (name); |
508 | if (lookup == (lang_output_section_statement_type *) NULL) | |
509 | { | |
2fa0b342 | 510 | |
075d7359 SC |
511 | lookup = (lang_output_section_statement_type *) |
512 | new_stat (lang_output_section_statement, stat_ptr); | |
513 | lookup->region = (lang_memory_region_type *) NULL; | |
514 | lookup->fill = 0; | |
515 | lookup->block_value = 1; | |
516 | lookup->name = name; | |
517 | ||
518 | lookup->next = (lang_statement_union_type *) NULL; | |
519 | lookup->bfd_section = (asection *) NULL; | |
520 | lookup->processed = false; | |
ae475b39 | 521 | lookup->loadable = 1; |
075d7359 SC |
522 | lookup->addr_tree = (etree_type *) NULL; |
523 | lang_list_init (&lookup->children); | |
524 | ||
1b8a42f3 ILT |
525 | lookup->memspec = (CONST char *) NULL; |
526 | lookup->flags = 0; | |
527 | lookup->subsection_alignment = -1; | |
528 | lookup->section_alignment = -1; | |
529 | lookup->load_base = (union etree_union *) NULL; | |
061e65f8 | 530 | lookup->phdrs = NULL; |
1b8a42f3 | 531 | |
075d7359 SC |
532 | lang_statement_append (&lang_output_section_statement, |
533 | (lang_statement_union_type *) lookup, | |
534 | &lookup->next); | |
535 | } | |
536 | return lookup; | |
537 | } | |
2fa0b342 | 538 | |
9d1fe8a4 | 539 | /*ARGSUSED*/ |
2fa0b342 | 540 | static void |
8ddef552 DM |
541 | print_flags (ignore_flags) |
542 | int *ignore_flags; | |
2fa0b342 | 543 | { |
075d7359 | 544 | fprintf (config.map_file, "("); |
2fa0b342 | 545 | #if 0 |
075d7359 SC |
546 | if (flags->flag_read) |
547 | fprintf (outfile, "R"); | |
548 | if (flags->flag_write) | |
549 | fprintf (outfile, "W"); | |
550 | if (flags->flag_executable) | |
551 | fprintf (outfile, "X"); | |
552 | if (flags->flag_loadable) | |
553 | fprintf (outfile, "L"); | |
2fa0b342 | 554 | #endif |
ae475b39 | 555 | fprintf (config.map_file, ")"); |
2fa0b342 DHW |
556 | } |
557 | ||
558 | void | |
8ddef552 | 559 | lang_map () |
2fa0b342 DHW |
560 | { |
561 | lang_memory_region_type *m; | |
075d7359 SC |
562 | |
563 | fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n"); | |
36ea6198 | 564 | #ifdef BFD64 |
075d7359 | 565 | fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n"); |
48491e2e | 566 | #else |
ae475b39 SC |
567 | fprintf (config.map_file, |
568 | "name\t\torigin length r_size c_size is attributes\n"); | |
569 | ||
48491e2e | 570 | #endif |
2fa0b342 | 571 | for (m = lang_memory_region_list; |
075d7359 SC |
572 | m != (lang_memory_region_type *) NULL; |
573 | m = m->next) | |
2fa0b342 | 574 | { |
075d7359 SC |
575 | fprintf (config.map_file, "%-16s", m->name); |
576 | print_address (m->origin); | |
577 | print_space (); | |
8ddef552 | 578 | print_address ((bfd_vma)m->length); |
075d7359 | 579 | print_space (); |
8ddef552 | 580 | print_address ((bfd_vma)m->old_length); |
ae475b39 SC |
581 | print_space(); |
582 | print_address (m->current - m->origin); | |
583 | print_space(); | |
584 | if (m->old_length) | |
fcf276c4 ILT |
585 | fprintf (config.map_file, " %2d%% ", |
586 | (int) ((m->current - m->origin) * 100 / m->old_length)); | |
075d7359 SC |
587 | print_flags (&m->flags); |
588 | fprintf (config.map_file, "\n"); | |
2fa0b342 | 589 | } |
075d7359 SC |
590 | fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n"); |
591 | fprintf (config.map_file, "output input virtual\n"); | |
592 | fprintf (config.map_file, "section section address tsize\n\n"); | |
2fa0b342 | 593 | |
075d7359 | 594 | print_statements (); |
2fa0b342 DHW |
595 | |
596 | } | |
597 | ||
598 | /* | |
599 | * | |
600 | */ | |
075d7359 | 601 | static void |
8ddef552 DM |
602 | init_os (s) |
603 | lang_output_section_statement_type * s; | |
2fa0b342 | 604 | { |
9d1fe8a4 SC |
605 | /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/ |
606 | section_userdata_type *new = | |
075d7359 | 607 | (section_userdata_type *) |
1d169acc | 608 | stat_alloc (sizeof (section_userdata_type)); |
075d7359 SC |
609 | |
610 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); | |
611 | if (s->bfd_section == (asection *) NULL) | |
612 | s->bfd_section = bfd_make_section (output_bfd, s->name); | |
613 | if (s->bfd_section == (asection *) NULL) | |
614 | { | |
ddddcdf0 | 615 | einfo ("%P%F: output format %s cannot represent section called %s\n", |
075d7359 | 616 | output_bfd->xvec->name, s->name); |
9d1fe8a4 | 617 | } |
2fa0b342 | 618 | s->bfd_section->output_section = s->bfd_section; |
7bc4a0d7 | 619 | |
2fa0b342 DHW |
620 | /* We initialize an output sections output offset to minus its own */ |
621 | /* vma to allow us to output a section through itself */ | |
622 | s->bfd_section->output_offset = 0; | |
075d7359 | 623 | get_userdata (s->bfd_section) = (PTR) new; |
c611e285 | 624 | |
2fa0b342 DHW |
625 | } |
626 | ||
1418c83b SC |
627 | /*********************************************************************** |
628 | The wild routines. | |
629 | ||
630 | These expand statements like *(.text) and foo.o to a list of | |
631 | explicit actions, like foo.o(.text), bar.o(.text) and | |
632 | foo.o(.text,.data) . | |
633 | ||
634 | The toplevel routine, wild, takes a statement, section, file and | |
635 | target. If either the section or file is null it is taken to be the | |
636 | wildcard. Seperate lang_input_section statements are created for | |
637 | each part of the expanstion, and placed after the statement provided. | |
638 | ||
639 | */ | |
075d7359 | 640 | |
0b3499f6 | 641 | void |
8ddef552 DM |
642 | wild_doit (ptr, section, output, file) |
643 | lang_statement_list_type * ptr; | |
644 | asection * section; | |
645 | lang_output_section_statement_type * output; | |
646 | lang_input_statement_type * file; | |
2fa0b342 | 647 | { |
075d7359 | 648 | if (output->bfd_section == (asection *) NULL) |
ae475b39 | 649 | init_os (output); |
2fa0b342 | 650 | |
075d7359 SC |
651 | if (section != (asection *) NULL |
652 | && section->output_section == (asection *) NULL) | |
ae475b39 SC |
653 | { |
654 | /* Add a section reference to the list */ | |
655 | lang_input_section_type *new = new_stat (lang_input_section, ptr); | |
656 | ||
657 | new->section = section; | |
658 | new->ifile = file; | |
659 | section->output_section = output->bfd_section; | |
29f33467 | 660 | |
27f7237e ILT |
661 | /* We don't copy the SEC_NEVER_LOAD flag from an input section to |
662 | an output section, because we want to be able to include a | |
663 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
664 | section (I don't know why we want to do this, but we do). | |
665 | build_link_order in ldwrite.c handles this case by turning the | |
666 | embedded SEC_NEVER_LOAD section into a fill. */ | |
667 | section->output_section->flags |= | |
668 | section->flags & (flagword) (~ SEC_NEVER_LOAD); | |
29f33467 | 669 | |
ae475b39 | 670 | if (!output->loadable) |
075d7359 | 671 | { |
29f33467 | 672 | /* Turn off load flag */ |
ae475b39 | 673 | output->bfd_section->flags &= ~SEC_LOAD; |
29f33467 | 674 | output->bfd_section->flags |= SEC_NEVER_LOAD; |
ae475b39 SC |
675 | } |
676 | if (section->alignment_power > output->bfd_section->alignment_power) | |
677 | { | |
678 | output->bfd_section->alignment_power = section->alignment_power; | |
075d7359 | 679 | } |
e20873a7 JG |
680 | /* If supplied an aligmnet, then force it */ |
681 | if (output->section_alignment != -1) | |
682 | { | |
683 | output->bfd_section->alignment_power = output->section_alignment; | |
684 | } | |
ae475b39 | 685 | } |
2fa0b342 DHW |
686 | } |
687 | ||
075d7359 | 688 | static void |
8ddef552 | 689 | wild_section (ptr, section, file, output) |
309c8153 ILT |
690 | lang_wild_statement_type *ptr; |
691 | const char *section; | |
692 | lang_input_statement_type *file; | |
693 | lang_output_section_statement_type *output; | |
2fa0b342 | 694 | { |
075d7359 SC |
695 | if (file->just_syms_flag == false) |
696 | { | |
309c8153 ILT |
697 | register asection *s; |
698 | ||
699 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
075d7359 | 700 | { |
309c8153 ILT |
701 | /* Attach all sections named SECTION. If SECTION is NULL, |
702 | then attach all sections. | |
703 | ||
704 | Previously, if SECTION was NULL, this code did not call | |
705 | wild_doit if the SEC_IS_COMMON flag was set for the | |
706 | section. I did not understand that, and I took it out. | |
707 | [email protected]. */ | |
708 | ||
709 | if (section == NULL | |
710 | || strcmp (bfd_get_section_name (file->the_bfd, s), | |
711 | section) == 0) | |
f9d3d71a | 712 | wild_doit (&ptr->children, s, output, file); |
075d7359 | 713 | } |
2fa0b342 | 714 | } |
2fa0b342 DHW |
715 | } |
716 | ||
1418c83b SC |
717 | /* passed a file name (which must have been seen already and added to |
718 | the statement tree. We will see if it has been opened already and | |
719 | had its symbols read. If not then we'll read it. | |
2fa0b342 | 720 | |
1418c83b SC |
721 | Archives are pecuilar here. We may open them once, but if they do |
722 | not define anything we need at the time, they won't have all their | |
309c8153 | 723 | symbols read. If we need them later, we'll have to redo it. */ |
193c5f93 ILT |
724 | static lang_input_statement_type * |
725 | lookup_name (name) | |
9f629407 | 726 | CONST char *name; |
2fa0b342 DHW |
727 | { |
728 | lang_input_statement_type *search; | |
075d7359 SC |
729 | |
730 | for (search = (lang_input_statement_type *) input_file_chain.head; | |
731 | search != (lang_input_statement_type *) NULL; | |
732 | search = (lang_input_statement_type *) search->next_real_file) | |
733 | { | |
734 | if (search->filename == (char *) NULL && name == (char *) NULL) | |
c477527c ILT |
735 | return search; |
736 | if (search->filename != (char *) NULL | |
737 | && name != (char *) NULL | |
738 | && strcmp (search->filename, name) == 0) | |
739 | break; | |
075d7359 | 740 | } |
2fa0b342 | 741 | |
c477527c | 742 | if (search == (lang_input_statement_type *) NULL) |
193c5f93 ILT |
743 | search = new_afile (name, lang_input_file_is_file_enum, default_target, |
744 | false); | |
c477527c | 745 | |
5e6cd559 ILT |
746 | /* If we have already added this file, or this file is not real |
747 | (FIXME: can that ever actually happen?) or the name is NULL | |
748 | (FIXME: can that ever actually happen?) don't add this file. */ | |
193c5f93 | 749 | if (search->loaded |
5e6cd559 ILT |
750 | || ! search->real |
751 | || search->filename == (const char *) NULL) | |
752 | return search; | |
753 | ||
6a02a973 | 754 | load_symbols (search, (lang_statement_list_type *) NULL); |
193c5f93 ILT |
755 | |
756 | return search; | |
757 | } | |
758 | ||
759 | /* Get the symbols for an input file. */ | |
c477527c | 760 | |
193c5f93 | 761 | static void |
6a02a973 | 762 | load_symbols (entry, place) |
193c5f93 | 763 | lang_input_statement_type *entry; |
6a02a973 | 764 | lang_statement_list_type *place; |
193c5f93 | 765 | { |
0b3499f6 ILT |
766 | char **matching; |
767 | ||
193c5f93 ILT |
768 | if (entry->loaded) |
769 | return; | |
770 | ||
771 | ldfile_open_file (entry); | |
772 | ||
0b3499f6 ILT |
773 | if (! bfd_check_format (entry->the_bfd, bfd_archive) |
774 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
775 | { | |
776 | bfd_error_type err; | |
6a02a973 | 777 | lang_statement_list_type *hold; |
0b3499f6 ILT |
778 | |
779 | err = bfd_get_error (); | |
780 | if (err == bfd_error_file_ambiguously_recognized) | |
781 | { | |
782 | char **p; | |
783 | ||
784 | einfo ("%B: file not recognized: %E\n", entry->the_bfd); | |
785 | einfo ("%B: matching formats:", entry->the_bfd); | |
786 | for (p = matching; *p != NULL; p++) | |
787 | einfo (" %s", *p); | |
788 | einfo ("%F\n"); | |
789 | } | |
6a02a973 ILT |
790 | else if (err != bfd_error_file_not_recognized |
791 | || place == NULL) | |
0b3499f6 ILT |
792 | einfo ("%F%B: file not recognized: %E\n", entry->the_bfd); |
793 | ||
794 | /* Try to interpret the file as a linker script. */ | |
795 | ||
796 | bfd_close (entry->the_bfd); | |
797 | entry->the_bfd = NULL; | |
798 | ||
799 | ldfile_open_command_file (entry->filename); | |
800 | ||
6a02a973 ILT |
801 | hold = stat_ptr; |
802 | stat_ptr = place; | |
803 | ||
0b3499f6 ILT |
804 | ldfile_assumed_script = true; |
805 | parser_input = input_script; | |
806 | yyparse (); | |
807 | ldfile_assumed_script = false; | |
808 | ||
6a02a973 ILT |
809 | stat_ptr = hold; |
810 | ||
0b3499f6 ILT |
811 | return; |
812 | } | |
813 | ||
814 | /* We don't call ldlang_add_file for an archive. Instead, the | |
815 | add_symbols entry point will call ldlang_add_file, via the | |
816 | add_archive_element callback, for each element of the archive | |
817 | which is used. */ | |
f400bbbb | 818 | switch (bfd_get_format (entry->the_bfd)) |
5e6cd559 | 819 | { |
f400bbbb ILT |
820 | default: |
821 | break; | |
822 | ||
823 | case bfd_object: | |
193c5f93 | 824 | ldlang_add_file (entry); |
5e6cd559 | 825 | if (trace_files || trace_file_tries) |
193c5f93 | 826 | info_msg ("%I\n", entry); |
f400bbbb ILT |
827 | break; |
828 | ||
829 | case bfd_archive: | |
830 | if (whole_archive) | |
831 | { | |
832 | bfd *member = bfd_openr_next_archived_file (entry->the_bfd, | |
833 | (bfd *) NULL); | |
834 | while (member != NULL) | |
835 | { | |
836 | if (! bfd_check_format (member, bfd_object)) | |
837 | einfo ("%F%B: object %B in archive is not object\n", | |
838 | entry->the_bfd, member); | |
839 | if (! ((*link_info.callbacks->add_archive_element) | |
840 | (&link_info, member, "-whole-archive"))) | |
841 | abort (); | |
842 | if (! bfd_link_add_symbols (member, &link_info)) | |
843 | einfo ("%F%B: could not read symbols: %E\n", member); | |
844 | member = bfd_openr_next_archived_file (entry->the_bfd, | |
845 | member); | |
846 | } | |
847 | ||
848 | entry->loaded = true; | |
849 | ||
850 | return; | |
851 | } | |
5e6cd559 | 852 | } |
1418c83b | 853 | |
0b3499f6 | 854 | if (! bfd_link_add_symbols (entry->the_bfd, &link_info)) |
193c5f93 | 855 | einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd); |
5e6cd559 | 856 | |
193c5f93 | 857 | entry->loaded = true; |
2fa0b342 DHW |
858 | } |
859 | ||
860 | static void | |
8ddef552 DM |
861 | wild (s, section, file, target, output) |
862 | lang_wild_statement_type * s; | |
9f629407 ILT |
863 | CONST char *section; |
864 | CONST char *file; | |
865 | CONST char *target; | |
8ddef552 | 866 | lang_output_section_statement_type * output; |
2fa0b342 DHW |
867 | { |
868 | lang_input_statement_type *f; | |
075d7359 SC |
869 | |
870 | if (file == (char *) NULL) | |
871 | { | |
872 | /* Perform the iteration over all files in the list */ | |
873 | for (f = (lang_input_statement_type *) file_chain.head; | |
874 | f != (lang_input_statement_type *) NULL; | |
875 | f = (lang_input_statement_type *) f->next) | |
876 | { | |
877 | wild_section (s, section, f, output); | |
878 | } | |
879 | } | |
880 | else | |
881 | { | |
882 | /* Perform the iteration over a single file */ | |
193c5f93 | 883 | wild_section (s, section, lookup_name (file), output); |
075d7359 SC |
884 | } |
885 | if (section != (char *) NULL | |
886 | && strcmp (section, "COMMON") == 0 | |
887 | && default_common_section == (lang_output_section_statement_type *) NULL) | |
888 | { | |
889 | /* Remember the section that common is going to incase we later | |
890 | get something which doesn't know where to put it */ | |
891 | default_common_section = output; | |
2fa0b342 | 892 | } |
2fa0b342 DHW |
893 | } |
894 | ||
895 | /* | |
075d7359 | 896 | read in all the files |
2fa0b342 | 897 | */ |
97fbbaca | 898 | |
075d7359 | 899 | static bfd * |
8ddef552 | 900 | open_output (name) |
9f629407 | 901 | CONST char *name; |
2fa0b342 | 902 | { |
097879bc | 903 | bfd *output; |
097879bc | 904 | |
075d7359 | 905 | if (output_target == (char *) NULL) |
ae475b39 SC |
906 | { |
907 | if (current_target != (char *) NULL) | |
908 | output_target = current_target; | |
909 | else | |
910 | output_target = default_target; | |
911 | } | |
075d7359 | 912 | output = bfd_openw (name, output_target); |
075d7359 SC |
913 | |
914 | if (output == (bfd *) NULL) | |
915 | { | |
5bcb7f28 | 916 | if (bfd_get_error () == bfd_error_invalid_target) |
ae475b39 | 917 | { |
ddddcdf0 | 918 | einfo ("%P%F: target %s not found\n", output_target); |
ae475b39 | 919 | } |
ddddcdf0 | 920 | einfo ("%P%F: cannot open output file %s: %E\n", name); |
075d7359 | 921 | } |
30d1a390 | 922 | |
97fbbaca JL |
923 | delete_output_file_on_failure = 1; |
924 | ||
30d1a390 | 925 | /* output->flags |= D_PAGED;*/ |
075d7359 | 926 | |
ddddcdf0 ILT |
927 | if (! bfd_set_format (output, bfd_object)) |
928 | einfo ("%P%F:%s: can not make object file: %E\n", name); | |
929 | if (! bfd_set_arch_mach (output, | |
930 | ldfile_output_architecture, | |
931 | ldfile_output_machine)) | |
932 | einfo ("%P%F:%s: can not set architecture: %E\n", name); | |
933 | ||
c477527c ILT |
934 | link_info.hash = bfd_link_hash_table_create (output); |
935 | if (link_info.hash == (struct bfd_link_hash_table *) NULL) | |
936 | einfo ("%P%F: can not create link hash table: %E\n"); | |
937 | ||
1b8a42f3 | 938 | bfd_set_gp_size (output, g_switch_value); |
2fa0b342 DHW |
939 | return output; |
940 | } | |
1418c83b SC |
941 | |
942 | ||
097879bc | 943 | |
1418c83b | 944 | |
2fa0b342 | 945 | static void |
8ddef552 DM |
946 | ldlang_open_output (statement) |
947 | lang_statement_union_type * statement; | |
2fa0b342 | 948 | { |
075d7359 SC |
949 | switch (statement->header.type) |
950 | { | |
c477527c ILT |
951 | case lang_output_statement_enum: |
952 | ASSERT (output_bfd == (bfd *) NULL); | |
075d7359 SC |
953 | output_bfd = open_output (statement->output_statement.name); |
954 | ldemul_set_output_arch (); | |
c477527c | 955 | if (config.magic_demand_paged && !link_info.relocateable) |
075d7359 SC |
956 | output_bfd->flags |= D_PAGED; |
957 | else | |
958 | output_bfd->flags &= ~D_PAGED; | |
959 | if (config.text_read_only) | |
960 | output_bfd->flags |= WP_TEXT; | |
961 | else | |
962 | output_bfd->flags &= ~WP_TEXT; | |
c96386c4 ILT |
963 | if (config.traditional_format) |
964 | output_bfd->flags |= BFD_TRADITIONAL_FORMAT; | |
965 | else | |
966 | output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; | |
075d7359 | 967 | break; |
1418c83b | 968 | |
075d7359 SC |
969 | case lang_target_statement_enum: |
970 | current_target = statement->target_statement.target; | |
971 | break; | |
972 | default: | |
973 | break; | |
974 | } | |
1418c83b | 975 | } |
2fa0b342 | 976 | |
309c8153 ILT |
977 | /* Open all the input files. */ |
978 | ||
1418c83b | 979 | static void |
309c8153 ILT |
980 | open_input_bfds (s, force) |
981 | lang_statement_union_type *s; | |
982 | boolean force; | |
1418c83b | 983 | { |
309c8153 | 984 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
075d7359 | 985 | { |
309c8153 | 986 | switch (s->header.type) |
075d7359 | 987 | { |
309c8153 ILT |
988 | case lang_constructors_statement_enum: |
989 | open_input_bfds (constructor_list.head, force); | |
990 | break; | |
991 | case lang_output_section_statement_enum: | |
992 | open_input_bfds (s->output_section_statement.children.head, force); | |
993 | break; | |
994 | case lang_wild_statement_enum: | |
995 | /* Maybe we should load the file's symbols */ | |
996 | if (s->wild_statement.filename) | |
997 | (void) lookup_name (s->wild_statement.filename); | |
998 | open_input_bfds (s->wild_statement.children.head, force); | |
999 | break; | |
1000 | case lang_group_statement_enum: | |
1001 | { | |
1002 | struct bfd_link_hash_entry *undefs; | |
1003 | ||
1004 | /* We must continually search the entries in the group | |
1005 | until no new symbols are added to the list of undefined | |
1006 | symbols. */ | |
1007 | ||
1008 | do | |
1009 | { | |
1010 | undefs = link_info.hash->undefs_tail; | |
1011 | open_input_bfds (s->group_statement.children.head, true); | |
1012 | } | |
1013 | while (undefs != link_info.hash->undefs_tail); | |
1014 | } | |
1015 | break; | |
1016 | case lang_target_statement_enum: | |
1017 | current_target = s->target_statement.target; | |
1018 | break; | |
1019 | case lang_input_statement_enum: | |
1020 | if (s->input_statement.real == true) | |
1021 | { | |
6a02a973 ILT |
1022 | lang_statement_list_type add; |
1023 | ||
309c8153 ILT |
1024 | s->input_statement.target = current_target; |
1025 | ||
1026 | /* If we are being called from within a group, and this | |
1027 | is an archive which has already been searched, then | |
1028 | force it to be researched. */ | |
1029 | if (force | |
1030 | && s->input_statement.loaded | |
1031 | && bfd_check_format (s->input_statement.the_bfd, | |
1032 | bfd_archive)) | |
1033 | s->input_statement.loaded = false; | |
1034 | ||
6a02a973 ILT |
1035 | lang_list_init (&add); |
1036 | ||
1037 | load_symbols (&s->input_statement, &add); | |
1038 | ||
1039 | if (add.head != NULL) | |
1040 | { | |
1041 | *add.tail = s->next; | |
1042 | s->next = add.head; | |
1043 | } | |
309c8153 ILT |
1044 | } |
1045 | break; | |
1046 | default: | |
1047 | break; | |
075d7359 | 1048 | } |
075d7359 | 1049 | } |
2fa0b342 | 1050 | } |
075d7359 | 1051 | |
2fa0b342 DHW |
1052 | /* If there are [COMMONS] statements, put a wild one into the bss section */ |
1053 | ||
1054 | static void | |
075d7359 | 1055 | lang_reasonable_defaults () |
2fa0b342 | 1056 | { |
1418c83b | 1057 | #if 0 |
075d7359 SC |
1058 | lang_output_section_statement_lookup (".text"); |
1059 | lang_output_section_statement_lookup (".data"); | |
8cb5eb31 | 1060 | |
075d7359 SC |
1061 | default_common_section = |
1062 | lang_output_section_statement_lookup (".bss"); | |
8cb5eb31 | 1063 | |
1418c83b | 1064 | |
075d7359 SC |
1065 | if (placed_commons == false) |
1066 | { | |
1067 | lang_wild_statement_type *new = | |
1068 | new_stat (lang_wild_statement, | |
1069 | &default_common_section->children); | |
1070 | ||
1071 | new->section_name = "COMMON"; | |
1072 | new->filename = (char *) NULL; | |
1073 | lang_list_init (&new->children); | |
1074 | } | |
1418c83b | 1075 | #endif |
8cb5eb31 | 1076 | |
2fa0b342 DHW |
1077 | } |
1078 | ||
1418c83b SC |
1079 | /* |
1080 | Add the supplied name to the symbol table as an undefined reference. | |
1081 | Remove items from the chain as we open input bfds | |
1082 | */ | |
075d7359 SC |
1083 | typedef struct ldlang_undef_chain_list |
1084 | { | |
f177a611 | 1085 | struct ldlang_undef_chain_list *next; |
1418c83b | 1086 | char *name; |
075d7359 | 1087 | } ldlang_undef_chain_list_type; |
1418c83b SC |
1088 | |
1089 | static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head; | |
1090 | ||
1091 | void | |
8ddef552 DM |
1092 | ldlang_add_undef (name) |
1093 | CONST char *CONST name; | |
2fa0b342 | 1094 | { |
1418c83b | 1095 | ldlang_undef_chain_list_type *new = |
1d169acc ILT |
1096 | ((ldlang_undef_chain_list_type *) |
1097 | stat_alloc (sizeof (ldlang_undef_chain_list_type))); | |
1418c83b SC |
1098 | |
1099 | new->next = ldlang_undef_chain_list_head; | |
1100 | ldlang_undef_chain_list_head = new; | |
1101 | ||
075d7359 | 1102 | new->name = buystring (name); |
1418c83b | 1103 | } |
075d7359 | 1104 | |
1418c83b SC |
1105 | /* Run through the list of undefineds created above and place them |
1106 | into the linker hash table as undefined symbols belonging to the | |
1107 | script file. | |
1108 | */ | |
1109 | static void | |
8ddef552 | 1110 | lang_place_undefineds () |
1418c83b | 1111 | { |
c477527c | 1112 | ldlang_undef_chain_list_type *ptr; |
1418c83b | 1113 | |
c477527c ILT |
1114 | for (ptr = ldlang_undef_chain_list_head; |
1115 | ptr != (ldlang_undef_chain_list_type *) NULL; | |
1116 | ptr = ptr->next) | |
075d7359 | 1117 | { |
c477527c | 1118 | struct bfd_link_hash_entry *h; |
075d7359 | 1119 | |
c477527c ILT |
1120 | h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true); |
1121 | if (h == (struct bfd_link_hash_entry *) NULL) | |
1122 | einfo ("%P%F: bfd_link_hash_lookup failed: %E"); | |
1123 | if (h->type == bfd_link_hash_new) | |
1124 | { | |
1125 | h->type = bfd_link_hash_undefined; | |
1126 | h->u.undef.abfd = NULL; | |
1127 | bfd_link_add_undef (link_info.hash, h); | |
1128 | } | |
075d7359 SC |
1129 | } |
1130 | } | |
1418c83b | 1131 | |
2fa0b342 DHW |
1132 | /* Open input files and attatch to output sections */ |
1133 | static void | |
8ddef552 DM |
1134 | map_input_to_output_sections (s, target, output_section_statement) |
1135 | lang_statement_union_type * s; | |
1136 | CONST char *target; | |
1137 | lang_output_section_statement_type * output_section_statement; | |
2fa0b342 | 1138 | { |
075d7359 | 1139 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
2fa0b342 | 1140 | { |
075d7359 | 1141 | switch (s->header.type) |
2fa0b342 | 1142 | { |
075d7359 SC |
1143 | |
1144 | ||
1145 | case lang_wild_statement_enum: | |
1146 | wild (&s->wild_statement, s->wild_statement.section_name, | |
1147 | s->wild_statement.filename, target, | |
1148 | output_section_statement); | |
1149 | ||
1150 | break; | |
1151 | case lang_constructors_statement_enum: | |
1152 | map_input_to_output_sections (constructor_list.head, | |
1153 | target, | |
1154 | output_section_statement); | |
1155 | break; | |
1156 | case lang_output_section_statement_enum: | |
1157 | map_input_to_output_sections (s->output_section_statement.children.head, | |
1158 | target, | |
1159 | &s->output_section_statement); | |
1160 | break; | |
1161 | case lang_output_statement_enum: | |
1162 | break; | |
1163 | case lang_target_statement_enum: | |
1164 | target = s->target_statement.target; | |
1165 | break; | |
309c8153 ILT |
1166 | case lang_group_statement_enum: |
1167 | map_input_to_output_sections (s->group_statement.children.head, | |
1168 | target, | |
1169 | output_section_statement); | |
1170 | break; | |
075d7359 SC |
1171 | case lang_fill_statement_enum: |
1172 | case lang_input_section_enum: | |
1173 | case lang_object_symbols_statement_enum: | |
1174 | case lang_data_statement_enum: | |
4fdbafb2 | 1175 | case lang_reloc_statement_enum: |
075d7359 SC |
1176 | case lang_assignment_statement_enum: |
1177 | case lang_padding_statement_enum: | |
f9d3d71a ILT |
1178 | case lang_input_statement_enum: |
1179 | if (output_section_statement != NULL | |
1180 | && output_section_statement->bfd_section == NULL) | |
1181 | init_os (output_section_statement); | |
075d7359 SC |
1182 | break; |
1183 | case lang_afile_asection_pair_statement_enum: | |
1184 | FAIL (); | |
1185 | break; | |
1186 | case lang_address_statement_enum: | |
1187 | /* Mark the specified section with the supplied address */ | |
1188 | { | |
1189 | lang_output_section_statement_type *os = | |
ef01a8a8 ILT |
1190 | lang_output_section_statement_lookup |
1191 | (s->address_statement.section_name); | |
075d7359 | 1192 | |
ef01a8a8 ILT |
1193 | if (os->bfd_section == NULL) |
1194 | init_os (os); | |
075d7359 | 1195 | os->addr_tree = s->address_statement.address; |
48491e2e | 1196 | } |
075d7359 | 1197 | break; |
2fa0b342 | 1198 | } |
2fa0b342 DHW |
1199 | } |
1200 | } | |
1201 | ||
075d7359 | 1202 | static void |
8ddef552 DM |
1203 | print_output_section_statement (output_section_statement) |
1204 | lang_output_section_statement_type * output_section_statement; | |
2fa0b342 DHW |
1205 | { |
1206 | asection *section = output_section_statement->bfd_section; | |
075d7359 SC |
1207 | |
1208 | print_nl (); | |
1209 | print_section (output_section_statement->name); | |
1210 | ||
e20873a7 | 1211 | |
075d7359 | 1212 | if (section) |
e20873a7 JG |
1213 | { |
1214 | print_dot = section->vma; | |
1215 | print_space (); | |
1216 | print_section (""); | |
1217 | print_space (); | |
1218 | print_address (section->vma); | |
1219 | print_space (); | |
1220 | print_size (section->_raw_size); | |
3deb20fb | 1221 | print_space (); |
e20873a7 JG |
1222 | print_size(section->_cooked_size); |
1223 | print_space (); | |
1224 | print_alignment (section->alignment_power); | |
1225 | print_space (); | |
2fa0b342 | 1226 | #if 0 |
e20873a7 JG |
1227 | fprintf (config.map_file, "%s flags", output_section_statement->region->name); |
1228 | print_flags (stdout, &output_section_statement->flags); | |
2fa0b342 | 1229 | #endif |
e20873a7 JG |
1230 | if (section->flags & SEC_LOAD) |
1231 | fprintf (config.map_file, "load "); | |
1232 | if (section->flags & SEC_ALLOC) | |
1233 | fprintf (config.map_file, "alloc "); | |
1234 | if (section->flags & SEC_RELOC) | |
1235 | fprintf (config.map_file, "reloc "); | |
1236 | if (section->flags & SEC_HAS_CONTENTS) | |
1237 | fprintf (config.map_file, "contents "); | |
2fa0b342 | 1238 | |
e20873a7 | 1239 | } |
075d7359 | 1240 | else |
e20873a7 | 1241 | { |
269773c1 | 1242 | fprintf (config.map_file, " (no attached output section)"); |
e20873a7 JG |
1243 | } |
1244 | print_nl (); | |
9fce28ed SC |
1245 | if (output_section_statement->load_base) |
1246 | { | |
f054ce20 | 1247 | bfd_vma b = exp_get_abs_int(output_section_statement->load_base, |
9fce28ed | 1248 | 0, "output base", lang_final_phase_enum); |
f054ce20 ILT |
1249 | fprintf (config.map_file, "Output address "); |
1250 | fprintf_vma (config.map_file, b); | |
1251 | fprintf (config.map_file, "\n"); | |
9fce28ed | 1252 | } |
e20873a7 | 1253 | if (output_section_statement->section_alignment >= 0 |
fa9dea80 | 1254 | || output_section_statement->subsection_alignment >= 0) |
e20873a7 | 1255 | { |
3deb20fb SC |
1256 | fprintf (config.map_file, "\t\t\t\t\tforced alignment "); |
1257 | if (output_section_statement->section_alignment >= 0) | |
075d7359 | 1258 | { |
3deb20fb | 1259 | fprintf (config.map_file, "section 2**%d ",output_section_statement->section_alignment ); |
075d7359 | 1260 | } |
e20873a7 JG |
1261 | if ( output_section_statement->subsection_alignment >= 0) |
1262 | { | |
3deb20fb | 1263 | fprintf (config.map_file, "subsection 2**%d ",output_section_statement->subsection_alignment ); |
e20873a7 JG |
1264 | } |
1265 | ||
1266 | print_nl (); | |
1267 | } | |
708f7fbc ILT |
1268 | print_statement_list (output_section_statement->children.head, |
1269 | output_section_statement); | |
2fa0b342 DHW |
1270 | |
1271 | } | |
1272 | ||
075d7359 | 1273 | static void |
8ddef552 DM |
1274 | print_assignment (assignment, output_section) |
1275 | lang_assignment_statement_type * assignment; | |
1276 | lang_output_section_statement_type * output_section; | |
2fa0b342 DHW |
1277 | { |
1278 | etree_value_type result; | |
075d7359 SC |
1279 | |
1280 | print_section (""); | |
1281 | print_space (); | |
1282 | print_section (""); | |
1283 | print_space (); | |
1284 | print_address (print_dot); | |
1285 | print_space (); | |
1286 | result = exp_fold_tree (assignment->exp->assign.src, | |
1287 | output_section, | |
1288 | lang_final_phase_enum, | |
1289 | print_dot, | |
1290 | &print_dot); | |
1291 | ||
1292 | if (result.valid) | |
1293 | { | |
1294 | print_address (result.value); | |
1295 | } | |
1296 | else | |
1297 | { | |
1298 | fprintf (config.map_file, "*undefined*"); | |
1299 | } | |
1300 | print_space (); | |
1301 | exp_print_tree (assignment->exp); | |
1302 | ||
1303 | fprintf (config.map_file, "\n"); | |
2fa0b342 DHW |
1304 | } |
1305 | ||
1306 | static void | |
8ddef552 DM |
1307 | print_input_statement (statm) |
1308 | lang_input_statement_type * statm; | |
2fa0b342 | 1309 | { |
075d7359 SC |
1310 | if (statm->filename != (char *) NULL) |
1311 | { | |
1312 | fprintf (config.map_file, "LOAD %s\n", statm->filename); | |
1313 | } | |
2fa0b342 DHW |
1314 | } |
1315 | ||
804c8601 SC |
1316 | /* Print all the defined symbols for the abfd provided by in the supplied |
1317 | section. | |
1318 | */ | |
1319 | ||
1320 | static boolean | |
1321 | print_one_symbol (hash_entry, ptr) | |
1322 | struct bfd_link_hash_entry *hash_entry; | |
1323 | PTR ptr; | |
2fa0b342 | 1324 | { |
804c8601 SC |
1325 | asection * sec = (asection *)ptr; |
1326 | ||
8ed88239 ILT |
1327 | if (hash_entry->type == bfd_link_hash_defined |
1328 | || hash_entry->type == bfd_link_hash_defweak) | |
804c8601 SC |
1329 | { |
1330 | if (sec == hash_entry->u.def.section) { | |
1331 | print_section (""); | |
1332 | fprintf (config.map_file, " "); | |
1333 | print_section (""); | |
1334 | fprintf (config.map_file, " "); | |
1335 | print_address (hash_entry->u.def.value + outside_section_address (sec)); | |
1336 | fprintf (config.map_file, " %s", hash_entry->root.string); | |
1337 | print_nl (); | |
1338 | } | |
1339 | } | |
09a5aa5e KR |
1340 | |
1341 | return true; | |
2fa0b342 | 1342 | } |
1418c83b | 1343 | |
075d7359 | 1344 | static void |
8ddef552 DM |
1345 | print_input_section (in) |
1346 | lang_input_section_type * in; | |
2fa0b342 DHW |
1347 | { |
1348 | asection *i = in->section; | |
f078dc7c | 1349 | bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size; |
1418c83b | 1350 | |
075d7359 SC |
1351 | if (size != 0) |
1352 | { | |
1353 | print_section (""); | |
1354 | fprintf (config.map_file, " "); | |
1355 | print_section (i->name); | |
1356 | fprintf (config.map_file, " "); | |
1357 | if (i->output_section) | |
1358 | { | |
1359 | print_address (i->output_section->vma + i->output_offset); | |
1360 | fprintf (config.map_file, " "); | |
ae475b39 SC |
1361 | print_size (i->_raw_size); |
1362 | fprintf (config.map_file, " "); | |
1363 | print_size(i->_cooked_size); | |
075d7359 SC |
1364 | fprintf (config.map_file, " "); |
1365 | print_alignment (i->alignment_power); | |
1366 | fprintf (config.map_file, " "); | |
1367 | if (in->ifile) | |
1368 | { | |
2fa0b342 | 1369 | |
075d7359 | 1370 | bfd *abfd = in->ifile->the_bfd; |
2fa0b342 | 1371 | |
075d7359 SC |
1372 | if (in->ifile->just_syms_flag == true) |
1373 | { | |
1374 | fprintf (config.map_file, "symbols only "); | |
1375 | } | |
2fa0b342 | 1376 | |
075d7359 SC |
1377 | fprintf (config.map_file, " %s ", abfd->xvec->name); |
1378 | if (abfd->my_archive != (bfd *) NULL) | |
1379 | { | |
1380 | fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename, | |
1381 | abfd->filename); | |
1382 | } | |
1383 | else | |
1384 | { | |
1385 | fprintf (config.map_file, "%s", abfd->filename); | |
1386 | } | |
1387 | fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd)); | |
1388 | print_nl (); | |
2fa0b342 | 1389 | |
804c8601 SC |
1390 | /* Print all the symbols */ |
1391 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i); | |
075d7359 SC |
1392 | } |
1393 | else | |
1394 | { | |
1395 | print_nl (); | |
1396 | } | |
1397 | ||
1398 | ||
1399 | print_dot = outside_section_address (i) + size; | |
1400 | } | |
1401 | else | |
1402 | { | |
1403 | fprintf (config.map_file, "No output section allocated\n"); | |
1404 | } | |
1405 | } | |
1406 | } | |
2fa0b342 DHW |
1407 | |
1408 | static void | |
8ddef552 DM |
1409 | print_fill_statement (fill) |
1410 | lang_fill_statement_type * fill; | |
075d7359 SC |
1411 | { |
1412 | fprintf (config.map_file, "FILL mask "); | |
1413 | print_fill (fill->fill); | |
1414 | } | |
1415 | ||
1416 | static void | |
8ddef552 DM |
1417 | print_data_statement (data) |
1418 | lang_data_statement_type * data; | |
2fa0b342 DHW |
1419 | { |
1420 | /* bfd_vma value; */ | |
075d7359 SC |
1421 | print_section (""); |
1422 | print_space (); | |
1423 | print_section (""); | |
1424 | print_space (); | |
65c552e3 | 1425 | /* ASSERT(print_dot == data->output_vma);*/ |
2fa0b342 | 1426 | |
708f7fbc ILT |
1427 | /* data->output_section may be NULL if called from gdb. */ |
1428 | if (data->output_section) | |
1429 | print_address (data->output_vma + data->output_section->vma); | |
1430 | else | |
1431 | print_address (data->output_vma); | |
075d7359 SC |
1432 | print_space (); |
1433 | print_address (data->value); | |
1434 | print_space (); | |
1435 | switch (data->type) | |
1436 | { | |
1437 | case BYTE: | |
1438 | fprintf (config.map_file, "BYTE "); | |
1439 | print_dot += BYTE_SIZE; | |
1440 | break; | |
1441 | case SHORT: | |
1442 | fprintf (config.map_file, "SHORT "); | |
1443 | print_dot += SHORT_SIZE; | |
1444 | break; | |
1445 | case LONG: | |
1446 | fprintf (config.map_file, "LONG "); | |
1447 | print_dot += LONG_SIZE; | |
1448 | break; | |
c477527c ILT |
1449 | case QUAD: |
1450 | fprintf (config.map_file, "QUAD "); | |
1451 | print_dot += QUAD_SIZE; | |
1452 | break; | |
075d7359 SC |
1453 | } |
1454 | ||
1455 | exp_print_tree (data->exp); | |
2fa0b342 | 1456 | |
075d7359 | 1457 | fprintf (config.map_file, "\n"); |
2fa0b342 DHW |
1458 | } |
1459 | ||
4fdbafb2 ILT |
1460 | /* Print a reloc statement. */ |
1461 | ||
1462 | static void | |
1463 | print_reloc_statement (reloc) | |
1464 | lang_reloc_statement_type *reloc; | |
1465 | { | |
1466 | print_section (""); | |
1467 | print_space (); | |
1468 | print_section (""); | |
1469 | print_space (); | |
1470 | ||
1471 | /* ASSERT(print_dot == data->output_vma);*/ | |
1472 | ||
708f7fbc ILT |
1473 | /* reloc->output_section may be NULL if called from gdb. */ |
1474 | if (reloc->output_section) | |
1475 | print_address (reloc->output_vma + reloc->output_section->vma); | |
1476 | else | |
1477 | print_address (reloc->output_vma); | |
4fdbafb2 ILT |
1478 | print_space (); |
1479 | print_address (reloc->addend_value); | |
1480 | print_space (); | |
1481 | ||
1482 | fprintf (config.map_file, "RELOC %s ", reloc->howto->name); | |
1483 | ||
1484 | print_dot += bfd_get_reloc_size (reloc->howto); | |
1485 | ||
1486 | exp_print_tree (reloc->addend_exp); | |
1487 | ||
1488 | fprintf (config.map_file, "\n"); | |
1489 | } | |
2fa0b342 DHW |
1490 | |
1491 | static void | |
8ddef552 DM |
1492 | print_padding_statement (s) |
1493 | lang_padding_statement_type * s; | |
075d7359 SC |
1494 | { |
1495 | print_section (""); | |
1496 | print_space (); | |
1497 | print_section ("*fill*"); | |
1498 | print_space (); | |
708f7fbc ILT |
1499 | /* s->output_section may be NULL if called from gdb. */ |
1500 | if (s->output_section) | |
1501 | print_address (s->output_offset + s->output_section->vma); | |
1502 | else | |
1503 | print_address (s->output_offset); | |
075d7359 SC |
1504 | print_space (); |
1505 | print_size (s->size); | |
1506 | print_space (); | |
1507 | print_fill (s->fill); | |
1508 | print_nl (); | |
ffc50032 | 1509 | |
708f7fbc ILT |
1510 | print_dot = s->output_offset + s->size; |
1511 | /* s->output_section may be NULL if called from gdb. */ | |
1512 | if (s->output_section) | |
1513 | print_dot += s->output_section->vma; | |
2fa0b342 DHW |
1514 | } |
1515 | ||
075d7359 | 1516 | static void |
8ddef552 DM |
1517 | print_wild_statement (w, os) |
1518 | lang_wild_statement_type * w; | |
1519 | lang_output_section_statement_type * os; | |
2fa0b342 | 1520 | { |
075d7359 SC |
1521 | fprintf (config.map_file, " from "); |
1522 | if (w->filename != (char *) NULL) | |
1523 | { | |
1524 | fprintf (config.map_file, "%s", w->filename); | |
1525 | } | |
1526 | else | |
1527 | { | |
1528 | fprintf (config.map_file, "*"); | |
1529 | } | |
1530 | if (w->section_name != (char *) NULL) | |
1531 | { | |
1532 | fprintf (config.map_file, "(%s)", w->section_name); | |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | fprintf (config.map_file, "(*)"); | |
1537 | } | |
1538 | print_nl (); | |
708f7fbc | 1539 | print_statement_list (w->children.head, os); |
2fa0b342 DHW |
1540 | |
1541 | } | |
309c8153 ILT |
1542 | |
1543 | /* Print a group statement. */ | |
1544 | ||
1545 | static void | |
1546 | print_group (s, os) | |
1547 | lang_group_statement_type *s; | |
1548 | lang_output_section_statement_type *os; | |
1549 | { | |
1550 | fprintf (config.map_file, "START GROUP\n"); | |
708f7fbc | 1551 | print_statement_list (s->children.head, os); |
309c8153 ILT |
1552 | fprintf (config.map_file, "END GROUP\n"); |
1553 | } | |
1554 | ||
708f7fbc ILT |
1555 | /* Print the list of statements in S. |
1556 | This can be called for any statement type. */ | |
1557 | ||
2fa0b342 | 1558 | static void |
708f7fbc | 1559 | print_statement_list (s, os) |
8ddef552 DM |
1560 | lang_statement_union_type * s; |
1561 | lang_output_section_statement_type * os; | |
2fa0b342 | 1562 | { |
075d7359 | 1563 | while (s) |
c611e285 | 1564 | { |
708f7fbc | 1565 | print_statement (s, os); |
075d7359 | 1566 | s = s->next; |
2fa0b342 | 1567 | } |
2fa0b342 DHW |
1568 | } |
1569 | ||
708f7fbc ILT |
1570 | /* Print the first statement in statement list S. |
1571 | This can be called for any statement type. */ | |
1572 | ||
1573 | static void | |
1574 | print_statement (s, os) | |
1575 | lang_statement_union_type * s; | |
1576 | lang_output_section_statement_type * os; | |
1577 | { | |
1578 | switch (s->header.type) | |
1579 | { | |
1580 | case lang_constructors_statement_enum: | |
1581 | fprintf (config.map_file, "constructors:\n"); | |
1582 | print_statement_list (constructor_list.head, os); | |
1583 | break; | |
1584 | case lang_wild_statement_enum: | |
1585 | print_wild_statement (&s->wild_statement, os); | |
1586 | break; | |
1587 | default: | |
1588 | fprintf (config.map_file, "Fail with %d\n", s->header.type); | |
1589 | FAIL (); | |
1590 | break; | |
1591 | case lang_address_statement_enum: | |
1592 | fprintf (config.map_file, "address\n"); | |
1593 | break; | |
1594 | case lang_object_symbols_statement_enum: | |
1595 | fprintf (config.map_file, "object symbols\n"); | |
1596 | break; | |
1597 | case lang_fill_statement_enum: | |
1598 | print_fill_statement (&s->fill_statement); | |
1599 | break; | |
1600 | case lang_data_statement_enum: | |
1601 | print_data_statement (&s->data_statement); | |
1602 | break; | |
1603 | case lang_reloc_statement_enum: | |
1604 | print_reloc_statement (&s->reloc_statement); | |
1605 | break; | |
1606 | case lang_input_section_enum: | |
1607 | print_input_section (&s->input_section); | |
1608 | break; | |
1609 | case lang_padding_statement_enum: | |
1610 | print_padding_statement (&s->padding_statement); | |
1611 | break; | |
1612 | case lang_output_section_statement_enum: | |
1613 | print_output_section_statement (&s->output_section_statement); | |
1614 | break; | |
1615 | case lang_assignment_statement_enum: | |
1616 | print_assignment (&s->assignment_statement, | |
1617 | os); | |
1618 | break; | |
1619 | case lang_target_statement_enum: | |
1620 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
1621 | break; | |
1622 | case lang_output_statement_enum: | |
1623 | fprintf (config.map_file, "OUTPUT(%s %s)\n", | |
1624 | s->output_statement.name, | |
1625 | output_target ? output_target : ""); | |
1626 | break; | |
1627 | case lang_input_statement_enum: | |
1628 | print_input_statement (&s->input_statement); | |
1629 | break; | |
1630 | case lang_group_statement_enum: | |
1631 | print_group (&s->group_statement, os); | |
1632 | break; | |
1633 | case lang_afile_asection_pair_statement_enum: | |
1634 | FAIL (); | |
1635 | break; | |
1636 | } | |
1637 | } | |
1638 | ||
2fa0b342 DHW |
1639 | |
1640 | static void | |
8ddef552 | 1641 | print_statements () |
2fa0b342 | 1642 | { |
708f7fbc | 1643 | print_statement_list (statement_list.head, |
075d7359 SC |
1644 | abs_output_section); |
1645 | ||
2fa0b342 DHW |
1646 | } |
1647 | ||
708f7fbc ILT |
1648 | /* Print the first N statements in statement list S to STDERR. |
1649 | If N == 0, nothing is printed. | |
1650 | If N < 0, the entire list is printed. | |
1651 | Intended to be called from GDB. */ | |
1652 | ||
1653 | void | |
1654 | dprint_statement (s, n) | |
1655 | lang_statement_union_type * s; | |
1656 | int n; | |
1657 | { | |
1658 | FILE *map_save = config.map_file; | |
1659 | ||
1660 | config.map_file = stderr; | |
1661 | ||
1662 | if (n < 0) | |
1663 | print_statement_list (s, abs_output_section); | |
1664 | else | |
1665 | { | |
1666 | while (--n >= 0) | |
1667 | { | |
1668 | print_statement (s, abs_output_section); | |
1669 | s = s->next; | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | config.map_file = map_save; | |
1674 | } | |
1675 | ||
2fa0b342 | 1676 | static bfd_vma |
9f629407 ILT |
1677 | insert_pad (this_ptr, fill, power, output_section_statement, dot) |
1678 | lang_statement_union_type ** this_ptr; | |
1679 | fill_type fill; | |
1680 | unsigned int power; | |
1681 | asection * output_section_statement; | |
1682 | bfd_vma dot; | |
075d7359 SC |
1683 | { |
1684 | /* Align this section first to the | |
2fa0b342 DHW |
1685 | input sections requirement, then |
1686 | to the output section's requirement. | |
075d7359 | 1687 | If this alignment is > than any seen before, |
2fa0b342 DHW |
1688 | then record it too. Perform the alignment by |
1689 | inserting a magic 'padding' statement. | |
1690 | */ | |
1691 | ||
075d7359 | 1692 | unsigned int alignment_needed = align_power (dot, power) - dot; |
2fa0b342 | 1693 | |
075d7359 | 1694 | if (alignment_needed != 0) |
2fa0b342 | 1695 | { |
075d7359 | 1696 | lang_statement_union_type *new = |
1d169acc ILT |
1697 | ((lang_statement_union_type *) |
1698 | stat_alloc (sizeof (lang_padding_statement_type))); | |
075d7359 | 1699 | |
2fa0b342 DHW |
1700 | /* Link into existing chain */ |
1701 | new->header.next = *this_ptr; | |
1702 | *this_ptr = new; | |
1703 | new->header.type = lang_padding_statement_enum; | |
1704 | new->padding_statement.output_section = output_section_statement; | |
1705 | new->padding_statement.output_offset = | |
1706 | dot - output_section_statement->vma; | |
1707 | new->padding_statement.fill = fill; | |
1708 | new->padding_statement.size = alignment_needed; | |
1709 | } | |
1710 | ||
1711 | ||
1712 | /* Remember the most restrictive alignment */ | |
075d7359 SC |
1713 | if (power > output_section_statement->alignment_power) |
1714 | { | |
1715 | output_section_statement->alignment_power = power; | |
1716 | } | |
c611e285 | 1717 | output_section_statement->_raw_size += alignment_needed; |
2fa0b342 DHW |
1718 | return alignment_needed + dot; |
1719 | ||
1720 | } | |
1721 | ||
1418c83b | 1722 | /* Work out how much this section will move the dot point */ |
075d7359 | 1723 | static bfd_vma |
9f629407 ILT |
1724 | size_input_section (this_ptr, output_section_statement, fill, dot, relax) |
1725 | lang_statement_union_type ** this_ptr; | |
1726 | lang_output_section_statement_type * output_section_statement; | |
c477527c | 1727 | fill_type fill; |
9f629407 ILT |
1728 | bfd_vma dot; |
1729 | boolean relax; | |
2fa0b342 DHW |
1730 | { |
1731 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
1732 | asection *i = is->section; | |
2fa0b342 | 1733 | |
075d7359 SC |
1734 | if (is->ifile->just_syms_flag == false) |
1735 | { | |
e20873a7 JG |
1736 | if (output_section_statement->subsection_alignment != -1) |
1737 | i->alignment_power = | |
1738 | output_section_statement->subsection_alignment; | |
1739 | ||
075d7359 SC |
1740 | dot = insert_pad (this_ptr, fill, i->alignment_power, |
1741 | output_section_statement->bfd_section, dot); | |
1742 | ||
075d7359 | 1743 | /* Remember where in the output section this input section goes */ |
ac004870 | 1744 | |
075d7359 SC |
1745 | i->output_offset = dot - output_section_statement->bfd_section->vma; |
1746 | ||
ae475b39 SC |
1747 | /* Mark how big the output section must be to contain this now |
1748 | */ | |
f078dc7c ILT |
1749 | if (i->_cooked_size != 0) |
1750 | dot += i->_cooked_size; | |
ae475b39 | 1751 | else |
f078dc7c | 1752 | dot += i->_raw_size; |
ae475b39 | 1753 | output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma; |
075d7359 | 1754 | } |
ac004870 | 1755 | else |
075d7359 SC |
1756 | { |
1757 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
1758 | } | |
2fa0b342 | 1759 | |
075d7359 | 1760 | return dot; |
2fa0b342 DHW |
1761 | } |
1762 | ||
193c5f93 ILT |
1763 | /* This variable indicates whether bfd_relax_section should be called |
1764 | again. */ | |
1765 | ||
1766 | static boolean relax_again; | |
1767 | ||
1768 | /* Set the sizes for all the output sections. */ | |
c611e285 | 1769 | |
a62494c4 | 1770 | bfd_vma |
9f629407 ILT |
1771 | lang_size_sections (s, output_section_statement, prev, fill, dot, relax) |
1772 | lang_statement_union_type * s; | |
1773 | lang_output_section_statement_type * output_section_statement; | |
1774 | lang_statement_union_type ** prev; | |
c477527c | 1775 | fill_type fill; |
9f629407 ILT |
1776 | bfd_vma dot; |
1777 | boolean relax; | |
c611e285 SC |
1778 | { |
1779 | /* Size up the sections from their constituent parts */ | |
075d7359 | 1780 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
ae475b39 SC |
1781 | { |
1782 | switch (s->header.type) | |
075d7359 | 1783 | { |
c611e285 | 1784 | |
ae475b39 SC |
1785 | case lang_output_section_statement_enum: |
1786 | { | |
1787 | bfd_vma after; | |
1788 | lang_output_section_statement_type *os = &s->output_section_statement; | |
1789 | ||
f9d3d71a ILT |
1790 | if (os->bfd_section == NULL) |
1791 | { | |
1792 | /* This section was never actually created. */ | |
1793 | break; | |
1794 | } | |
1795 | ||
27f7237e ILT |
1796 | /* If this is a COFF shared library section, use the size and |
1797 | address from the input section. FIXME: This is COFF | |
1798 | specific; it would be cleaner if there were some other way | |
1799 | to do this, but nothing simple comes to mind. */ | |
1800 | if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) | |
1801 | { | |
1802 | asection *input; | |
1803 | ||
1804 | if (os->children.head == NULL | |
1805 | || os->children.head->next != NULL | |
1806 | || os->children.head->header.type != lang_input_section_enum) | |
1807 | einfo ("%P%X: Internal error on COFF shared library section %s", | |
1808 | os->name); | |
1809 | ||
1810 | input = os->children.head->input_section.section; | |
1811 | bfd_set_section_vma (os->bfd_section->owner, | |
1812 | os->bfd_section, | |
1813 | bfd_section_vma (input->owner, input)); | |
1814 | os->bfd_section->_raw_size = input->_raw_size; | |
1815 | break; | |
1816 | } | |
e9b63852 | 1817 | |
686739e2 | 1818 | if (bfd_is_abs_section (os->bfd_section)) |
ae475b39 SC |
1819 | { |
1820 | /* No matter what happens, an abs section starts at zero */ | |
686739e2 | 1821 | ASSERT (os->bfd_section->vma == 0); |
ae475b39 SC |
1822 | } |
1823 | else | |
1824 | { | |
1825 | if (os->addr_tree == (etree_type *) NULL) | |
1826 | { | |
1827 | /* No address specified for this section, get one | |
1828 | from the region specification | |
1829 | */ | |
1830 | if (os->region == (lang_memory_region_type *) NULL) | |
1831 | { | |
1832 | os->region = lang_memory_region_lookup ("*default*"); | |
1833 | } | |
1834 | dot = os->region->current; | |
feaa9c4b ILT |
1835 | if (os->section_alignment == -1) |
1836 | dot = align_power (dot, os->bfd_section->alignment_power); | |
ae475b39 SC |
1837 | } |
1838 | else | |
1839 | { | |
1840 | etree_value_type r; | |
1841 | ||
1842 | r = exp_fold_tree (os->addr_tree, | |
1843 | abs_output_section, | |
1844 | lang_allocating_phase_enum, | |
1845 | dot, &dot); | |
1846 | if (r.valid == false) | |
1847 | { | |
1848 | einfo ("%F%S: non constant address expression for section %s\n", | |
1849 | os->name); | |
1850 | } | |
1851 | dot = r.value; | |
1852 | } | |
1853 | /* The section starts here */ | |
1854 | /* First, align to what the section needs */ | |
1855 | ||
a62494c4 JL |
1856 | if (os->section_alignment != -1) |
1857 | dot = align_power (dot, os->section_alignment); | |
ae475b39 | 1858 | |
ae475b39 | 1859 | bfd_set_section_vma (0, os->bfd_section, dot); |
9fce28ed | 1860 | |
686739e2 ILT |
1861 | os->bfd_section->output_offset = 0; |
1862 | } | |
ae475b39 SC |
1863 | |
1864 | (void) lang_size_sections (os->children.head, os, &os->children.head, | |
1865 | os->fill, dot, relax); | |
1866 | /* Ignore the size of the input sections, use the vma and size to */ | |
1867 | /* align against */ | |
1868 | ||
8ddef552 | 1869 | after = ALIGN_N (os->bfd_section->vma + |
97fbbaca JL |
1870 | os->bfd_section->_raw_size, |
1871 | /* The coercion here is important, see ld.h. */ | |
1872 | (bfd_vma) os->block_value); | |
ae475b39 | 1873 | |
686739e2 ILT |
1874 | if (bfd_is_abs_section (os->bfd_section)) |
1875 | ASSERT (after == os->bfd_section->vma); | |
1876 | else | |
1877 | os->bfd_section->_raw_size = after - os->bfd_section->vma; | |
ae475b39 SC |
1878 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; |
1879 | os->processed = true; | |
1880 | ||
1881 | /* Replace into region ? */ | |
686739e2 | 1882 | if (os->region != (lang_memory_region_type *) NULL) |
e802f0be DE |
1883 | { |
1884 | os->region->current = dot; | |
1885 | /* Make sure this isn't silly. */ | |
1886 | if ((os->region->current < os->region->origin) | |
1887 | || (os->region->current | |
1888 | > os->region->origin + os->region->length)) | |
1889 | { | |
1890 | if (os->addr_tree != (etree_type *) NULL) | |
1891 | { | |
1892 | einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n", | |
1893 | os->region->current, | |
1894 | os->bfd_section->owner, | |
1895 | os->bfd_section->name, | |
1896 | os->region->name); | |
1897 | } | |
1898 | else | |
1899 | { | |
1900 | einfo ("%X%P: region %s is full (%B section %s)\n", | |
1901 | os->region->name, | |
1902 | os->bfd_section->owner, | |
1903 | os->bfd_section->name); | |
1904 | } | |
1905 | /* Reset the region pointer. */ | |
1906 | os->region->current = os->region->origin; | |
1907 | } | |
1908 | } | |
ae475b39 | 1909 | } |
e802f0be | 1910 | break; |
9d1fe8a4 | 1911 | |
ae475b39 SC |
1912 | case lang_constructors_statement_enum: |
1913 | dot = lang_size_sections (constructor_list.head, | |
1914 | output_section_statement, | |
1915 | &s->wild_statement.children.head, | |
1916 | fill, | |
1917 | dot, relax); | |
1918 | break; | |
9d1fe8a4 | 1919 | |
ae475b39 SC |
1920 | case lang_data_statement_enum: |
1921 | { | |
1922 | unsigned int size = 0; | |
1923 | ||
1924 | s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma; | |
1925 | s->data_statement.output_section = | |
1926 | output_section_statement->bfd_section; | |
1927 | ||
1928 | switch (s->data_statement.type) | |
1929 | { | |
c477527c ILT |
1930 | case QUAD: |
1931 | size = QUAD_SIZE; | |
1932 | break; | |
ae475b39 SC |
1933 | case LONG: |
1934 | size = LONG_SIZE; | |
1935 | break; | |
1936 | case SHORT: | |
1937 | size = SHORT_SIZE; | |
1938 | break; | |
1939 | case BYTE: | |
1940 | size = BYTE_SIZE; | |
1941 | break; | |
1942 | ||
1943 | } | |
1944 | dot += size; | |
1945 | output_section_statement->bfd_section->_raw_size += size; | |
36ea6198 ILT |
1946 | /* The output section gets contents, and then we inspect for |
1947 | any flags set in the input script which override any ALLOC */ | |
943fbd5b | 1948 | output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS; |
36ea6198 ILT |
1949 | if (!(output_section_statement->flags & SEC_NEVER_LOAD)) { |
1950 | output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD; | |
1951 | } | |
ae475b39 SC |
1952 | } |
1953 | break; | |
c611e285 | 1954 | |
4fdbafb2 ILT |
1955 | case lang_reloc_statement_enum: |
1956 | { | |
1957 | int size; | |
1958 | ||
1959 | s->reloc_statement.output_vma = | |
1960 | dot - output_section_statement->bfd_section->vma; | |
1961 | s->reloc_statement.output_section = | |
1962 | output_section_statement->bfd_section; | |
1963 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
1964 | dot += size; | |
1965 | output_section_statement->bfd_section->_raw_size += size; | |
1966 | } | |
1967 | break; | |
1968 | ||
ae475b39 | 1969 | case lang_wild_statement_enum: |
c611e285 | 1970 | |
ae475b39 SC |
1971 | dot = lang_size_sections (s->wild_statement.children.head, |
1972 | output_section_statement, | |
1973 | &s->wild_statement.children.head, | |
c611e285 | 1974 | |
ae475b39 | 1975 | fill, dot, relax); |
c611e285 | 1976 | |
ae475b39 | 1977 | break; |
c611e285 | 1978 | |
ae475b39 | 1979 | case lang_object_symbols_statement_enum: |
c477527c ILT |
1980 | link_info.create_object_symbols_section = |
1981 | output_section_statement->bfd_section; | |
ae475b39 SC |
1982 | break; |
1983 | case lang_output_statement_enum: | |
1984 | case lang_target_statement_enum: | |
1985 | break; | |
1986 | case lang_input_section_enum: | |
ae475b39 | 1987 | { |
c477527c ILT |
1988 | asection *i; |
1989 | ||
193c5f93 ILT |
1990 | i = (*prev)->input_section.section; |
1991 | if (! relax) | |
1992 | i->_cooked_size = i->_raw_size; | |
1993 | else | |
755f42fe | 1994 | { |
193c5f93 | 1995 | boolean again; |
075d7359 | 1996 | |
193c5f93 ILT |
1997 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) |
1998 | einfo ("%P%F: can't relax section: %E\n"); | |
1999 | if (again) | |
2000 | relax_again = true; | |
2001 | } | |
2002 | dot = size_input_section (prev, | |
2003 | output_section_statement, | |
2004 | output_section_statement->fill, | |
2005 | dot, relax); | |
ae475b39 | 2006 | } |
ae475b39 SC |
2007 | break; |
2008 | case lang_input_statement_enum: | |
2009 | break; | |
2010 | case lang_fill_statement_enum: | |
2011 | s->fill_statement.output_section = output_section_statement->bfd_section; | |
075d7359 | 2012 | |
ae475b39 SC |
2013 | fill = s->fill_statement.fill; |
2014 | break; | |
2015 | case lang_assignment_statement_enum: | |
2016 | { | |
2017 | bfd_vma newdot = dot; | |
2018 | ||
2019 | exp_fold_tree (s->assignment_statement.exp, | |
2020 | output_section_statement, | |
2021 | lang_allocating_phase_enum, | |
2022 | dot, | |
2023 | &newdot); | |
2024 | ||
2025 | if (newdot != dot && !relax) | |
27f7237e ILT |
2026 | { |
2027 | /* The assignment changed dot. Insert a pad. */ | |
2028 | if (output_section_statement == abs_output_section) | |
2029 | { | |
2030 | /* If we don't have an output section, then just adjust | |
2031 | the default memory address. */ | |
2032 | lang_memory_region_lookup ("*default*")->current = newdot; | |
2033 | } | |
2034 | else | |
2035 | { | |
2036 | lang_statement_union_type *new = | |
2037 | ((lang_statement_union_type *) | |
2038 | stat_alloc (sizeof (lang_padding_statement_type))); | |
2039 | ||
2040 | /* Link into existing chain */ | |
2041 | new->header.next = *prev; | |
2042 | *prev = new; | |
2043 | new->header.type = lang_padding_statement_enum; | |
2044 | new->padding_statement.output_section = | |
2045 | output_section_statement->bfd_section; | |
2046 | new->padding_statement.output_offset = | |
2047 | dot - output_section_statement->bfd_section->vma; | |
2048 | new->padding_statement.fill = fill; | |
2049 | new->padding_statement.size = newdot - dot; | |
2050 | output_section_statement->bfd_section->_raw_size += | |
2051 | new->padding_statement.size; | |
2052 | } | |
2053 | ||
2054 | dot = newdot; | |
2055 | } | |
ae475b39 | 2056 | } |
27f7237e | 2057 | break; |
9d87af56 ILT |
2058 | |
2059 | case lang_padding_statement_enum: | |
2060 | /* If we are relaxing, and this is not the first pass, some | |
2061 | padding statements may have been inserted during previous | |
2062 | passes. We may have to move the padding statement to a new | |
2063 | location if dot has a different value at this point in this | |
2064 | pass than it did at this point in the previous pass. */ | |
2065 | s->padding_statement.output_offset = | |
2066 | dot - output_section_statement->bfd_section->vma; | |
2067 | dot += s->padding_statement.size; | |
708f7fbc ILT |
2068 | output_section_statement->bfd_section->_raw_size += |
2069 | s->padding_statement.size; | |
9d87af56 ILT |
2070 | break; |
2071 | ||
309c8153 ILT |
2072 | case lang_group_statement_enum: |
2073 | dot = lang_size_sections (s->group_statement.children.head, | |
2074 | output_section_statement, | |
2075 | &s->group_statement.children.head, | |
2076 | fill, dot, relax); | |
2077 | break; | |
2078 | ||
ae475b39 SC |
2079 | default: |
2080 | FAIL (); | |
2081 | break; | |
9d87af56 | 2082 | |
ae475b39 | 2083 | /* This can only get here when relaxing is turned on */ |
075d7359 | 2084 | |
ae475b39 SC |
2085 | case lang_address_statement_enum: |
2086 | break; | |
075d7359 | 2087 | } |
ae475b39 SC |
2088 | prev = &s->header.next; |
2089 | } | |
c611e285 SC |
2090 | return dot; |
2091 | } | |
9d1fe8a4 | 2092 | |
309c8153 | 2093 | bfd_vma |
9f629407 ILT |
2094 | lang_do_assignments (s, output_section_statement, fill, dot) |
2095 | lang_statement_union_type * s; | |
2096 | lang_output_section_statement_type * output_section_statement; | |
c477527c | 2097 | fill_type fill; |
9f629407 | 2098 | bfd_vma dot; |
2fa0b342 | 2099 | { |
075d7359 | 2100 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
2fa0b342 | 2101 | { |
075d7359 | 2102 | switch (s->header.type) |
2fa0b342 | 2103 | { |
075d7359 SC |
2104 | case lang_constructors_statement_enum: |
2105 | dot = lang_do_assignments (constructor_list.head, | |
2106 | output_section_statement, | |
2107 | fill, | |
2108 | dot); | |
2109 | break; | |
2110 | ||
2111 | case lang_output_section_statement_enum: | |
2112 | { | |
2113 | lang_output_section_statement_type *os = | |
269773c1 | 2114 | &(s->output_section_statement); |
2fa0b342 | 2115 | |
269773c1 ILT |
2116 | if (os->bfd_section != NULL) |
2117 | { | |
2118 | dot = os->bfd_section->vma; | |
2119 | (void) lang_do_assignments (os->children.head, os, | |
2120 | os->fill, dot); | |
2121 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; | |
2122 | } | |
0b3499f6 ILT |
2123 | if (os->load_base) |
2124 | { | |
467a0380 SC |
2125 | /* If nothing has been placed into the output section then |
2126 | it won't have a bfd_section. */ | |
2127 | if (os->bfd_section) | |
2128 | { | |
2129 | os->bfd_section->lma | |
2130 | = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum); | |
2131 | } | |
0b3499f6 | 2132 | } |
075d7359 SC |
2133 | } |
2134 | break; | |
2135 | case lang_wild_statement_enum: | |
2fa0b342 | 2136 | |
075d7359 SC |
2137 | dot = lang_do_assignments (s->wild_statement.children.head, |
2138 | output_section_statement, | |
2139 | fill, dot); | |
2fa0b342 | 2140 | |
075d7359 SC |
2141 | break; |
2142 | ||
2143 | case lang_object_symbols_statement_enum: | |
2144 | case lang_output_statement_enum: | |
2145 | case lang_target_statement_enum: | |
1418c83b | 2146 | #if 0 |
075d7359 | 2147 | case lang_common_statement_enum: |
1418c83b | 2148 | #endif |
2fa0b342 | 2149 | break; |
075d7359 SC |
2150 | case lang_data_statement_enum: |
2151 | { | |
2152 | etree_value_type value; | |
2153 | ||
2154 | value = exp_fold_tree (s->data_statement.exp, | |
2155 | abs_output_section, | |
2156 | lang_final_phase_enum, dot, &dot); | |
2157 | s->data_statement.value = value.value; | |
2158 | if (value.valid == false) | |
ddddcdf0 | 2159 | einfo ("%F%P: invalid data statement\n"); |
075d7359 SC |
2160 | } |
2161 | switch (s->data_statement.type) | |
2162 | { | |
c477527c ILT |
2163 | case QUAD: |
2164 | dot += QUAD_SIZE; | |
2165 | break; | |
075d7359 SC |
2166 | case LONG: |
2167 | dot += LONG_SIZE; | |
2168 | break; | |
2169 | case SHORT: | |
2170 | dot += SHORT_SIZE; | |
2171 | break; | |
2172 | case BYTE: | |
2173 | dot += BYTE_SIZE; | |
2174 | break; | |
2175 | } | |
2fa0b342 | 2176 | break; |
4fdbafb2 ILT |
2177 | |
2178 | case lang_reloc_statement_enum: | |
2179 | { | |
2180 | etree_value_type value; | |
2181 | ||
2182 | value = exp_fold_tree (s->reloc_statement.addend_exp, | |
2183 | abs_output_section, | |
2184 | lang_final_phase_enum, dot, &dot); | |
2185 | s->reloc_statement.addend_value = value.value; | |
2186 | if (value.valid == false) | |
2187 | einfo ("%F%P: invalid reloc statement\n"); | |
2188 | } | |
2189 | dot += bfd_get_reloc_size (s->reloc_statement.howto); | |
2190 | break; | |
2191 | ||
075d7359 SC |
2192 | case lang_input_section_enum: |
2193 | { | |
2194 | asection *in = s->input_section.section; | |
2195 | ||
f078dc7c ILT |
2196 | if (in->_cooked_size != 0) |
2197 | dot += in->_cooked_size; | |
2198 | else | |
2199 | dot += in->_raw_size; | |
075d7359 | 2200 | } |
2fa0b342 | 2201 | break; |
2fa0b342 | 2202 | |
075d7359 SC |
2203 | case lang_input_statement_enum: |
2204 | break; | |
2205 | case lang_fill_statement_enum: | |
2206 | fill = s->fill_statement.fill; | |
2207 | break; | |
2208 | case lang_assignment_statement_enum: | |
2209 | { | |
2210 | exp_fold_tree (s->assignment_statement.exp, | |
2211 | output_section_statement, | |
2212 | lang_final_phase_enum, | |
2213 | dot, | |
2214 | &dot); | |
2215 | } | |
2216 | ||
2217 | break; | |
2218 | case lang_padding_statement_enum: | |
2219 | dot += s->padding_statement.size; | |
2220 | break; | |
309c8153 ILT |
2221 | |
2222 | case lang_group_statement_enum: | |
2223 | dot = lang_do_assignments (s->group_statement.children.head, | |
2224 | output_section_statement, | |
2225 | fill, dot); | |
2226 | ||
2227 | break; | |
2228 | ||
075d7359 SC |
2229 | default: |
2230 | FAIL (); | |
2231 | break; | |
2232 | case lang_address_statement_enum: | |
2233 | break; | |
2234 | } | |
2fa0b342 DHW |
2235 | |
2236 | } | |
2237 | return dot; | |
2238 | } | |
2239 | ||
1d169acc ILT |
2240 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
2241 | operator .startof. (section_name), it produces an undefined symbol | |
2242 | .startof.section_name. Similarly, when it sees | |
2243 | .sizeof. (section_name), it produces an undefined symbol | |
2244 | .sizeof.section_name. For all the output sections, we look for | |
2245 | such symbols, and set them to the correct value. */ | |
2246 | ||
2247 | static void | |
2248 | lang_set_startof () | |
2249 | { | |
2250 | asection *s; | |
2251 | ||
307585f1 ILT |
2252 | if (link_info.relocateable) |
2253 | return; | |
2254 | ||
1d169acc ILT |
2255 | for (s = output_bfd->sections; s != NULL; s = s->next) |
2256 | { | |
2257 | const char *secname; | |
2258 | char *buf; | |
2259 | struct bfd_link_hash_entry *h; | |
2260 | ||
2261 | secname = bfd_get_section_name (output_bfd, s); | |
2262 | buf = xmalloc (10 + strlen (secname)); | |
2263 | ||
2264 | sprintf (buf, ".startof.%s", secname); | |
2265 | h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true); | |
2266 | if (h != NULL && h->type == bfd_link_hash_undefined) | |
2267 | { | |
2268 | h->type = bfd_link_hash_defined; | |
2269 | h->u.def.value = bfd_get_section_vma (output_bfd, s); | |
2270 | h->u.def.section = bfd_abs_section_ptr; | |
2271 | } | |
2272 | ||
2273 | sprintf (buf, ".sizeof.%s", secname); | |
2274 | h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true); | |
2275 | if (h != NULL && h->type == bfd_link_hash_undefined) | |
2276 | { | |
2277 | h->type = bfd_link_hash_defined; | |
2278 | if (s->_cooked_size != 0) | |
2279 | h->u.def.value = s->_cooked_size; | |
2280 | else | |
2281 | h->u.def.value = s->_raw_size; | |
2282 | h->u.def.section = bfd_abs_section_ptr; | |
2283 | } | |
2284 | ||
2285 | free (buf); | |
2286 | } | |
2287 | } | |
2288 | ||
2fa0b342 | 2289 | static void |
8ddef552 | 2290 | lang_finish () |
2fa0b342 | 2291 | { |
c477527c | 2292 | struct bfd_link_hash_entry *h; |
cc38364d KR |
2293 | boolean warn; |
2294 | ||
2295 | if (link_info.relocateable || link_info.shared) | |
2296 | warn = false; | |
2297 | else | |
2298 | warn = true; | |
075d7359 | 2299 | |
c477527c ILT |
2300 | if (entry_symbol == (char *) NULL) |
2301 | { | |
2302 | /* No entry has been specified. Look for start, but don't warn | |
2303 | if we don't find it. */ | |
2304 | entry_symbol = "start"; | |
2305 | warn = false; | |
2306 | } | |
2fa0b342 | 2307 | |
c477527c ILT |
2308 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true); |
2309 | if (h != (struct bfd_link_hash_entry *) NULL | |
8ed88239 | 2310 | && (h->type == bfd_link_hash_defined |
061e65f8 ILT |
2311 | || h->type == bfd_link_hash_defweak) |
2312 | && h->u.def.section->output_section != NULL) | |
075d7359 | 2313 | { |
c477527c | 2314 | bfd_vma val; |
075d7359 | 2315 | |
c477527c ILT |
2316 | val = (h->u.def.value |
2317 | + bfd_get_section_vma (output_bfd, | |
2318 | h->u.def.section->output_section) | |
2319 | + h->u.def.section->output_offset); | |
2320 | if (! bfd_set_start_address (output_bfd, val)) | |
2321 | einfo ("%P%F:%s: can't set start address\n", entry_symbol); | |
075d7359 | 2322 | } |
c477527c | 2323 | else |
22a78f0d | 2324 | { |
c477527c ILT |
2325 | asection *ts; |
2326 | ||
2327 | /* Can't find the entry symbol. Use the first address in the | |
2328 | text section. */ | |
2329 | ts = bfd_get_section_by_name (output_bfd, ".text"); | |
2330 | if (ts != (asection *) NULL) | |
2331 | { | |
2332 | if (warn) | |
2333 | einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n", | |
2334 | entry_symbol, bfd_get_section_vma (output_bfd, ts)); | |
2335 | if (! bfd_set_start_address (output_bfd, | |
2336 | bfd_get_section_vma (output_bfd, ts))) | |
2337 | einfo ("%P%F: can't set start address\n"); | |
2338 | } | |
2339 | else | |
2340 | { | |
2341 | if (warn) | |
2342 | einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n", | |
2343 | entry_symbol); | |
2344 | } | |
22a78f0d | 2345 | } |
2fa0b342 DHW |
2346 | } |
2347 | ||
0b3499f6 | 2348 | /* Check that the architecture of all the input files is compatible |
f400bbbb ILT |
2349 | with the output file. Also call the backend to let it do any |
2350 | other checking that is needed. */ | |
0b3499f6 | 2351 | |
2fa0b342 | 2352 | static void |
8ddef552 | 2353 | lang_check () |
2fa0b342 DHW |
2354 | { |
2355 | lang_statement_union_type *file; | |
075d7359 | 2356 | bfd *input_bfd; |
075d7359 | 2357 | CONST bfd_arch_info_type *compatible; |
7bc4a0d7 | 2358 | |
2fa0b342 | 2359 | for (file = file_chain.head; |
075d7359 SC |
2360 | file != (lang_statement_union_type *) NULL; |
2361 | file = file->input_statement.next) | |
2362 | { | |
075d7359 | 2363 | input_bfd = file->input_statement.the_bfd; |
075d7359 SC |
2364 | compatible = bfd_arch_get_compatible (input_bfd, |
2365 | output_bfd); | |
0b3499f6 ILT |
2366 | if (compatible == NULL) |
2367 | einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n", | |
2368 | bfd_printable_name (input_bfd), input_bfd, | |
2369 | bfd_printable_name (output_bfd)); | |
f400bbbb ILT |
2370 | |
2371 | else | |
2372 | bfd_merge_private_bfd_data (input_bfd, output_bfd); | |
075d7359 SC |
2373 | } |
2374 | } | |
2fa0b342 | 2375 | |
c477527c ILT |
2376 | /* Look through all the global common symbols and attach them to the |
2377 | correct section. The -sort-common command line switch may be used | |
2378 | to roughly sort the entries by size. */ | |
2fa0b342 DHW |
2379 | |
2380 | static void | |
8ddef552 | 2381 | lang_common () |
2fa0b342 | 2382 | { |
c477527c ILT |
2383 | if (link_info.relocateable |
2384 | && ! command_line.force_common_definition) | |
2385 | return; | |
075d7359 | 2386 | |
c477527c ILT |
2387 | if (! config.sort_common) |
2388 | bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL); | |
2389 | else | |
075d7359 | 2390 | { |
fa9dea80 | 2391 | int power; |
1418c83b | 2392 | |
fa9dea80 | 2393 | for (power = 4; power >= 0; power--) |
c477527c ILT |
2394 | bfd_link_hash_traverse (link_info.hash, lang_one_common, |
2395 | (PTR) &power); | |
2396 | } | |
2397 | } | |
29f33467 | 2398 | |
c477527c | 2399 | /* Place one common symbol in the correct section. */ |
075d7359 | 2400 | |
c477527c ILT |
2401 | static boolean |
2402 | lang_one_common (h, info) | |
2403 | struct bfd_link_hash_entry *h; | |
2404 | PTR info; | |
2405 | { | |
2406 | unsigned int power_of_two; | |
2407 | bfd_vma size; | |
c477527c | 2408 | asection *section; |
075d7359 | 2409 | |
c477527c ILT |
2410 | if (h->type != bfd_link_hash_common) |
2411 | return true; | |
075d7359 | 2412 | |
c477527c | 2413 | size = h->u.c.size; |
f400bbbb | 2414 | power_of_two = h->u.c.p->alignment_power; |
36c6e8c3 ILT |
2415 | |
2416 | if (config.sort_common | |
f054ce20 | 2417 | && power_of_two < (unsigned int) *(int *) info) |
c477527c | 2418 | return true; |
075d7359 | 2419 | |
f400bbbb | 2420 | section = h->u.c.p->section; |
075d7359 | 2421 | |
c477527c | 2422 | /* Increase the size of the section. */ |
36c6e8c3 ILT |
2423 | section->_raw_size = ALIGN_N (section->_raw_size, |
2424 | (bfd_size_type) (1 << power_of_two)); | |
075d7359 | 2425 | |
c477527c ILT |
2426 | /* Adjust the alignment if necessary. */ |
2427 | if (power_of_two > section->alignment_power) | |
2428 | section->alignment_power = power_of_two; | |
075d7359 | 2429 | |
c477527c ILT |
2430 | /* Change the symbol from common to defined. */ |
2431 | h->type = bfd_link_hash_defined; | |
2432 | h->u.def.section = section; | |
2433 | h->u.def.value = section->_raw_size; | |
097879bc | 2434 | |
c477527c ILT |
2435 | /* Increase the size of the section. */ |
2436 | section->_raw_size += size; | |
1418c83b | 2437 | |
809391bd ILT |
2438 | /* Make sure the section is allocated in memory. */ |
2439 | section->flags |= SEC_ALLOC; | |
2440 | ||
b495c314 | 2441 | if (config.map_file != NULL) |
c477527c ILT |
2442 | fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n", |
2443 | h->root.string, (unsigned long) size, | |
2444 | (unsigned long) h->u.def.value, section->owner->filename); | |
1418c83b | 2445 | |
c477527c | 2446 | return true; |
2fa0b342 DHW |
2447 | } |
2448 | ||
2449 | /* | |
075d7359 | 2450 | run through the input files and ensure that every input |
2fa0b342 DHW |
2451 | section has somewhere to go. If one is found without |
2452 | a destination then create an input request and place it | |
2453 | into the statement tree. | |
2454 | */ | |
2455 | ||
075d7359 | 2456 | static void |
8ddef552 | 2457 | lang_place_orphans () |
2fa0b342 DHW |
2458 | { |
2459 | lang_input_statement_type *file; | |
1418c83b | 2460 | |
075d7359 SC |
2461 | for (file = (lang_input_statement_type *) file_chain.head; |
2462 | file != (lang_input_statement_type *) NULL; | |
2463 | file = (lang_input_statement_type *) file->next) | |
2464 | { | |
2465 | asection *s; | |
2466 | ||
2467 | for (s = file->the_bfd->sections; | |
2468 | s != (asection *) NULL; | |
2469 | s = s->next) | |
2470 | { | |
2471 | if (s->output_section == (asection *) NULL) | |
2472 | { | |
2473 | /* This section of the file is not attatched, root | |
2474 | around for a sensible place for it to go */ | |
2475 | ||
e802f0be DE |
2476 | if (file->just_syms_flag) |
2477 | { | |
2478 | /* We are only retrieving symbol values from this | |
2479 | file. We want the symbols to act as though the | |
2480 | values in the file are absolute. */ | |
2481 | s->output_section = bfd_abs_section_ptr; | |
2482 | s->output_offset = s->vma; | |
2483 | } | |
2484 | else if (file->common_section == s) | |
075d7359 SC |
2485 | { |
2486 | /* This is a lonely common section which must | |
2487 | have come from an archive. We attatch to the | |
2488 | section with the wildcard */ | |
c477527c ILT |
2489 | if (! link_info.relocateable |
2490 | && ! command_line.force_common_definition) | |
075d7359 SC |
2491 | { |
2492 | if (default_common_section == | |
2493 | (lang_output_section_statement_type *) NULL) | |
2494 | { | |
a4aeaacf | 2495 | info_msg ("%P: no [COMMON] command, defaulting to .bss\n"); |
075d7359 SC |
2496 | |
2497 | default_common_section = | |
2498 | lang_output_section_statement_lookup (".bss"); | |
2499 | ||
2500 | } | |
2501 | wild_doit (&default_common_section->children, s, | |
2502 | default_common_section, file); | |
2503 | } | |
2504 | } | |
0b3499f6 ILT |
2505 | else if (ldemul_place_orphan (file, s)) |
2506 | ; | |
075d7359 SC |
2507 | else |
2508 | { | |
2509 | lang_output_section_statement_type *os = | |
2510 | lang_output_section_statement_lookup (s->name); | |
2fa0b342 | 2511 | |
075d7359 SC |
2512 | wild_doit (&os->children, s, os, file); |
2513 | } | |
2514 | } | |
2fa0b342 | 2515 | } |
2fa0b342 | 2516 | } |
2fa0b342 DHW |
2517 | } |
2518 | ||
2519 | ||
2fa0b342 | 2520 | void |
8ddef552 DM |
2521 | lang_set_flags (ptr, flags) |
2522 | int *ptr; | |
2523 | CONST char *flags; | |
2fa0b342 | 2524 | { |
075d7359 SC |
2525 | boolean state = false; |
2526 | ||
2527 | *ptr = 0; | |
2fa0b342 | 2528 | while (*flags) |
075d7359 SC |
2529 | { |
2530 | if (*flags == '!') | |
2531 | { | |
2532 | state = false; | |
2533 | flags++; | |
2534 | } | |
2535 | else | |
2536 | state = true; | |
2537 | switch (*flags) | |
2538 | { | |
2539 | case 'R': | |
2540 | /* ptr->flag_read = state; */ | |
2541 | break; | |
2542 | case 'W': | |
2543 | /* ptr->flag_write = state; */ | |
2544 | break; | |
2545 | case 'X': | |
2546 | /* ptr->flag_executable= state;*/ | |
2547 | break; | |
2548 | case 'L': | |
2549 | case 'I': | |
2550 | /* ptr->flag_loadable= state;*/ | |
2551 | break; | |
2552 | default: | |
ddddcdf0 | 2553 | einfo ("%P%F: invalid syntax in flags\n"); |
075d7359 SC |
2554 | break; |
2555 | } | |
dc4726c2 SC |
2556 | flags++; |
2557 | } | |
2fa0b342 DHW |
2558 | } |
2559 | ||
a4d2a48e ILT |
2560 | /* Call a function on each input file. This function will be called |
2561 | on an archive, but not on the elements. */ | |
2562 | ||
2563 | void | |
2564 | lang_for_each_input_file (func) | |
2565 | void (*func) PARAMS ((lang_input_statement_type *)); | |
2566 | { | |
2567 | lang_input_statement_type *f; | |
2568 | ||
2569 | for (f = (lang_input_statement_type *) input_file_chain.head; | |
2570 | f != NULL; | |
2571 | f = (lang_input_statement_type *) f->next_real_file) | |
2572 | func (f); | |
2573 | } | |
2fa0b342 | 2574 | |
a4d2a48e ILT |
2575 | /* Call a function on each file. The function will be called on all |
2576 | the elements of an archive which are included in the link, but will | |
2577 | not be called on the archive file itself. */ | |
2fa0b342 DHW |
2578 | |
2579 | void | |
8ddef552 DM |
2580 | lang_for_each_file (func) |
2581 | void (*func) PARAMS ((lang_input_statement_type *)); | |
2fa0b342 DHW |
2582 | { |
2583 | lang_input_statement_type *f; | |
075d7359 SC |
2584 | |
2585 | for (f = (lang_input_statement_type *) file_chain.head; | |
2586 | f != (lang_input_statement_type *) NULL; | |
2587 | f = (lang_input_statement_type *) f->next) | |
2588 | { | |
2589 | func (f); | |
2590 | } | |
2fa0b342 DHW |
2591 | } |
2592 | ||
9f629407 ILT |
2593 | #if 0 |
2594 | ||
2595 | /* Not used. */ | |
2fa0b342 DHW |
2596 | |
2597 | void | |
8ddef552 DM |
2598 | lang_for_each_input_section (func) |
2599 | void (*func) PARAMS ((bfd * ab, asection * as)); | |
2fa0b342 DHW |
2600 | { |
2601 | lang_input_statement_type *f; | |
075d7359 SC |
2602 | |
2603 | for (f = (lang_input_statement_type *) file_chain.head; | |
2604 | f != (lang_input_statement_type *) NULL; | |
2605 | f = (lang_input_statement_type *) f->next) | |
2fa0b342 DHW |
2606 | { |
2607 | asection *s; | |
075d7359 | 2608 | |
2fa0b342 | 2609 | for (s = f->the_bfd->sections; |
075d7359 SC |
2610 | s != (asection *) NULL; |
2611 | s = s->next) | |
2612 | { | |
2613 | func (f->the_bfd, s); | |
2614 | } | |
2fa0b342 DHW |
2615 | } |
2616 | } | |
2617 | ||
9f629407 | 2618 | #endif |
2fa0b342 | 2619 | |
075d7359 | 2620 | void |
8ddef552 DM |
2621 | ldlang_add_file (entry) |
2622 | lang_input_statement_type * entry; | |
2fa0b342 | 2623 | { |
5e6cd559 ILT |
2624 | bfd **pp; |
2625 | ||
075d7359 SC |
2626 | lang_statement_append (&file_chain, |
2627 | (lang_statement_union_type *) entry, | |
2628 | &entry->next); | |
c477527c ILT |
2629 | |
2630 | /* The BFD linker needs to have a list of all input BFDs involved in | |
2631 | a link. */ | |
2632 | ASSERT (entry->the_bfd->link_next == (bfd *) NULL); | |
2633 | ASSERT (entry->the_bfd != output_bfd); | |
5e6cd559 ILT |
2634 | for (pp = &link_info.input_bfds; |
2635 | *pp != (bfd *) NULL; | |
2636 | pp = &(*pp)->link_next) | |
2637 | ; | |
2638 | *pp = entry->the_bfd; | |
c477527c | 2639 | entry->the_bfd->usrdata = (PTR) entry; |
fc1dfb71 | 2640 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
2fa0b342 DHW |
2641 | } |
2642 | ||
2fa0b342 | 2643 | void |
2c6635a4 | 2644 | lang_add_output (name, from_script) |
8ddef552 | 2645 | CONST char *name; |
2c6635a4 | 2646 | int from_script; |
2fa0b342 | 2647 | { |
2c6635a4 ILT |
2648 | /* Make -o on command line override OUTPUT in script. */ |
2649 | if (had_output_filename == false || !from_script) | |
2650 | { | |
2651 | output_filename = name; | |
2652 | had_output_filename = true; | |
2653 | } | |
2fa0b342 DHW |
2654 | } |
2655 | ||
2656 | ||
2657 | static lang_output_section_statement_type *current_section; | |
2658 | ||
f054ce20 ILT |
2659 | static int |
2660 | topower (x) | |
9f629407 | 2661 | int x; |
e20873a7 | 2662 | { |
f054ce20 | 2663 | unsigned int i = 1; |
e20873a7 | 2664 | int l; |
f054ce20 ILT |
2665 | |
2666 | if (x < 0) | |
2667 | return -1; | |
2668 | ||
e20873a7 | 2669 | for (l = 0; l < 32; l++) |
f054ce20 ILT |
2670 | { |
2671 | if (i >= (unsigned int) x) | |
2672 | return l; | |
2673 | i <<= 1; | |
2674 | } | |
2675 | ||
29f33467 | 2676 | return 0; |
e20873a7 | 2677 | } |
2fa0b342 | 2678 | void |
8ddef552 DM |
2679 | lang_enter_output_section_statement (output_section_statement_name, |
2680 | address_exp, flags, block_value, | |
9f629407 | 2681 | align, subalign, ebase) |
fcf276c4 | 2682 | const char *output_section_statement_name; |
8ddef552 DM |
2683 | etree_type * address_exp; |
2684 | int flags; | |
2685 | bfd_vma block_value; | |
2686 | etree_type *align; | |
2687 | etree_type *subalign; | |
9f629407 | 2688 | etree_type *ebase; |
2fa0b342 DHW |
2689 | { |
2690 | lang_output_section_statement_type *os; | |
075d7359 SC |
2691 | |
2692 | current_section = | |
e20873a7 | 2693 | os = |
075d7359 SC |
2694 | lang_output_section_statement_lookup (output_section_statement_name); |
2695 | ||
2696 | ||
2fa0b342 | 2697 | |
2fa0b342 DHW |
2698 | /* Add this statement to tree */ |
2699 | /* add_statement(lang_output_section_statement_enum, | |
2700 | output_section_statement);*/ | |
2701 | /* Make next things chain into subchain of this */ | |
2702 | ||
2703 | if (os->addr_tree == | |
075d7359 | 2704 | (etree_type *) NULL) |
e20873a7 JG |
2705 | { |
2706 | os->addr_tree = | |
2707 | address_exp; | |
2708 | } | |
7bc4a0d7 | 2709 | os->flags = flags; |
ae475b39 SC |
2710 | if (flags & SEC_NEVER_LOAD) |
2711 | os->loadable = 0; | |
2712 | else | |
2713 | os->loadable = 1; | |
29f33467 | 2714 | os->block_value = block_value ? block_value : 1; |
075d7359 | 2715 | stat_ptr = &os->children; |
2fa0b342 | 2716 | |
e20873a7 JG |
2717 | os->subsection_alignment = topower( |
2718 | exp_get_value_int(subalign, -1, | |
2719 | "subsection alignment", | |
2720 | 0)); | |
2721 | os->section_alignment = topower( | |
2722 | exp_get_value_int(align, -1, | |
2723 | "section alignment", 0)); | |
9fce28ed | 2724 | |
9f629407 | 2725 | os->load_base = ebase; |
2fa0b342 DHW |
2726 | } |
2727 | ||
9fce28ed | 2728 | |
075d7359 | 2729 | void |
8ddef552 | 2730 | lang_final () |
2fa0b342 | 2731 | { |
2c6635a4 ILT |
2732 | lang_output_statement_type *new = |
2733 | new_stat (lang_output_statement, stat_ptr); | |
2fa0b342 | 2734 | |
2c6635a4 | 2735 | new->name = output_filename; |
075d7359 | 2736 | } |
2fa0b342 | 2737 | |
c611e285 SC |
2738 | /* Reset the current counters in the regions */ |
2739 | static void | |
8ddef552 | 2740 | reset_memory_regions () |
c611e285 | 2741 | { |
075d7359 SC |
2742 | lang_memory_region_type *p = lang_memory_region_list; |
2743 | ||
c611e285 | 2744 | for (p = lang_memory_region_list; |
075d7359 SC |
2745 | p != (lang_memory_region_type *) NULL; |
2746 | p = p->next) | |
2747 | { | |
8ddef552 | 2748 | p->old_length = (bfd_size_type) (p->current - p->origin); |
075d7359 SC |
2749 | p->current = p->origin; |
2750 | } | |
c611e285 | 2751 | } |
2fa0b342 | 2752 | |
2fa0b342 | 2753 | void |
8ddef552 | 2754 | lang_process () |
075d7359 | 2755 | { |
075d7359 | 2756 | lang_reasonable_defaults (); |
1418c83b SC |
2757 | current_target = default_target; |
2758 | ||
075d7359 | 2759 | lang_for_each_statement (ldlang_open_output); /* Open the output file */ |
1418c83b | 2760 | |
97fbbaca JL |
2761 | ldemul_create_output_section_statements (); |
2762 | ||
1418c83b | 2763 | /* Add to the hash table all undefineds on the command line */ |
075d7359 | 2764 | lang_place_undefineds (); |
1418c83b SC |
2765 | |
2766 | /* Create a bfd for each input file */ | |
2767 | current_target = default_target; | |
309c8153 | 2768 | open_input_bfds (statement_list.head, false); |
1418c83b | 2769 | |
cc38364d KR |
2770 | ldemul_after_open (); |
2771 | ||
c477527c ILT |
2772 | /* Build all sets based on the information gathered from the input |
2773 | files. */ | |
2774 | ldctor_build_sets (); | |
2775 | ||
4fdbafb2 ILT |
2776 | /* Size up the common data */ |
2777 | lang_common (); | |
2778 | ||
1418c83b | 2779 | /* Run through the contours of the script and attatch input sections |
075d7359 | 2780 | to the correct output sections |
1418c83b | 2781 | */ |
075d7359 SC |
2782 | map_input_to_output_sections (statement_list.head, (char *) NULL, |
2783 | (lang_output_section_statement_type *) NULL); | |
1418c83b | 2784 | |
81016051 | 2785 | |
1418c83b | 2786 | /* Find any sections not attatched explicitly and handle them */ |
075d7359 | 2787 | lang_place_orphans (); |
1418c83b | 2788 | |
075d7359 | 2789 | ldemul_before_allocation (); |
1418c83b | 2790 | |
061e65f8 ILT |
2791 | /* We must record the program headers before we try to fix the |
2792 | section positions, since they will affect SIZEOF_HEADERS. */ | |
2793 | lang_record_phdrs (); | |
2794 | ||
c611e285 | 2795 | /* Now run around and relax if we can */ |
075d7359 | 2796 | if (command_line.relax) |
c611e285 | 2797 | { |
97fbbaca JL |
2798 | /* First time round is a trial run to get the 'worst case' |
2799 | addresses of the objects if there was no relaxing. */ | |
ae475b39 | 2800 | lang_size_sections (statement_list.head, |
193c5f93 | 2801 | abs_output_section, |
ae475b39 | 2802 | &(statement_list.head), 0, (bfd_vma) 0, false); |
c611e285 | 2803 | |
193c5f93 ILT |
2804 | /* Keep relaxing until bfd_relax_section gives up. */ |
2805 | do | |
2806 | { | |
1d169acc ILT |
2807 | reset_memory_regions (); |
2808 | ||
193c5f93 | 2809 | relax_again = false; |
ae475b39 | 2810 | |
193c5f93 ILT |
2811 | /* Do all the assignments with our current guesses as to |
2812 | section sizes. */ | |
2813 | lang_do_assignments (statement_list.head, | |
2814 | abs_output_section, | |
2815 | (fill_type) 0, (bfd_vma) 0); | |
ae475b39 | 2816 | |
193c5f93 ILT |
2817 | /* Perform another relax pass - this time we know where the |
2818 | globals are, so can make better guess. */ | |
2819 | lang_size_sections (statement_list.head, | |
2820 | abs_output_section, | |
2821 | &(statement_list.head), 0, (bfd_vma) 0, true); | |
2822 | } | |
2823 | while (relax_again); | |
ae475b39 | 2824 | } |
ae475b39 SC |
2825 | else |
2826 | { | |
97fbbaca | 2827 | /* Size up the sections. */ |
ae475b39 SC |
2828 | lang_size_sections (statement_list.head, |
2829 | abs_output_section, | |
2830 | &(statement_list.head), 0, (bfd_vma) 0, false); | |
075d7359 | 2831 | } |
c611e285 | 2832 | |
1418c83b | 2833 | /* See if anything special should be done now we know how big |
97fbbaca | 2834 | everything is. */ |
075d7359 | 2835 | ldemul_after_allocation (); |
1418c83b | 2836 | |
1d169acc ILT |
2837 | /* Fix any .startof. or .sizeof. symbols. */ |
2838 | lang_set_startof (); | |
2839 | ||
1418c83b SC |
2840 | /* Do all the assignments, now that we know the final restingplaces |
2841 | of all the symbols */ | |
2842 | ||
075d7359 SC |
2843 | lang_do_assignments (statement_list.head, |
2844 | abs_output_section, | |
c477527c | 2845 | (fill_type) 0, (bfd_vma) 0); |
ffc50032 | 2846 | |
1418c83b SC |
2847 | /* Make sure that we're not mixing architectures */ |
2848 | ||
075d7359 | 2849 | lang_check (); |
1418c83b | 2850 | |
1418c83b | 2851 | /* Final stuffs */ |
97fbbaca JL |
2852 | |
2853 | ldemul_finish (); | |
075d7359 | 2854 | lang_finish (); |
2fa0b342 DHW |
2855 | } |
2856 | ||
2fa0b342 | 2857 | /* EXPORTED TO YACC */ |
1418c83b | 2858 | |
2fa0b342 | 2859 | void |
8ddef552 DM |
2860 | lang_add_wild (section_name, filename) |
2861 | CONST char *CONST section_name; | |
2862 | CONST char *CONST filename; | |
1418c83b | 2863 | { |
075d7359 SC |
2864 | lang_wild_statement_type *new = new_stat (lang_wild_statement, |
2865 | stat_ptr); | |
1418c83b | 2866 | |
075d7359 SC |
2867 | if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0) |
2868 | { | |
2869 | placed_commons = true; | |
2870 | } | |
2871 | if (filename != (char *) NULL) | |
2872 | { | |
2873 | lang_has_input_file = true; | |
2874 | } | |
1418c83b SC |
2875 | new->section_name = section_name; |
2876 | new->filename = filename; | |
075d7359 | 2877 | lang_list_init (&new->children); |
1418c83b | 2878 | } |
075d7359 | 2879 | |
1418c83b | 2880 | void |
8ddef552 DM |
2881 | lang_section_start (name, address) |
2882 | CONST char *name; | |
2883 | etree_type * address; | |
2fa0b342 | 2884 | { |
075d7359 SC |
2885 | lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr); |
2886 | ||
2fa0b342 DHW |
2887 | ad->section_name = name; |
2888 | ad->address = address; | |
2889 | } | |
1418c83b | 2890 | |
60e8a534 ILT |
2891 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called |
2892 | because of a -e argument on the command line, or zero if this is | |
2893 | called by ENTRY in a linker script. Command line arguments take | |
2894 | precedence. */ | |
2895 | ||
3f38a017 SC |
2896 | /* WINDOWS_NT. When an entry point has been specified, we will also force |
2897 | this symbol to be defined by calling ldlang_add_undef (equivalent to | |
2898 | having switch -u entry_name on the command line). The reason we do | |
2899 | this is so that the user doesn't have to because they would have to use | |
2900 | the -u switch if they were specifying an entry point other than | |
2901 | _mainCRTStartup. Specifically, if creating a windows application, entry | |
2902 | point _WinMainCRTStartup must be specified. | |
2903 | What I have found for non console applications (entry not _mainCRTStartup) | |
2904 | is that the .obj that contains mainCRTStartup is brought in since it is | |
2905 | the first encountered in libc.lib and it has other symbols in it which will | |
2906 | be pulled in by the link process. To avoid this, adding -u with the entry | |
2907 | point name specified forces the correct .obj to be used. We can avoid | |
2908 | making the user do this by always adding the entry point name as an | |
2909 | undefined symbol. */ | |
2910 | ||
075d7359 | 2911 | void |
60e8a534 | 2912 | lang_add_entry (name, cmdline) |
8ddef552 | 2913 | CONST char *name; |
f054ce20 | 2914 | boolean cmdline; |
2fa0b342 | 2915 | { |
60e8a534 ILT |
2916 | if (entry_symbol == NULL |
2917 | || cmdline | |
f054ce20 | 2918 | || ! entry_from_cmdline) |
60e8a534 ILT |
2919 | { |
2920 | entry_symbol = name; | |
f054ce20 | 2921 | entry_from_cmdline = cmdline; |
60e8a534 | 2922 | } |
943fbd5b | 2923 | #if 0 |
3f38a017 SC |
2924 | /* don't do this yet. It seems to work (the executables run), but the |
2925 | image created is very different from what I was getting before indicating | |
2926 | that something else is being pulled in. When everything else is working, | |
2927 | then try to put this back in to see if it will do the right thing for | |
2928 | other more complicated applications */ | |
2929 | ldlang_add_undef (name); | |
2930 | #endif | |
2fa0b342 DHW |
2931 | } |
2932 | ||
2933 | void | |
8ddef552 DM |
2934 | lang_add_target (name) |
2935 | CONST char *name; | |
2fa0b342 | 2936 | { |
075d7359 SC |
2937 | lang_target_statement_type *new = new_stat (lang_target_statement, |
2938 | stat_ptr); | |
2939 | ||
2fa0b342 DHW |
2940 | new->target = name; |
2941 | ||
2942 | } | |
2fa0b342 | 2943 | |
2fa0b342 | 2944 | void |
8ddef552 DM |
2945 | lang_add_map (name) |
2946 | CONST char *name; | |
2fa0b342 | 2947 | { |
075d7359 SC |
2948 | while (*name) |
2949 | { | |
2950 | switch (*name) | |
2951 | { | |
2952 | case 'F': | |
2953 | map_option_f = true; | |
2954 | break; | |
2955 | } | |
2956 | name++; | |
2fa0b342 | 2957 | } |
2fa0b342 DHW |
2958 | } |
2959 | ||
075d7359 | 2960 | void |
8ddef552 DM |
2961 | lang_add_fill (exp) |
2962 | int exp; | |
2fa0b342 | 2963 | { |
075d7359 SC |
2964 | lang_fill_statement_type *new = new_stat (lang_fill_statement, |
2965 | stat_ptr); | |
2966 | ||
2fa0b342 DHW |
2967 | new->fill = exp; |
2968 | } | |
2969 | ||
075d7359 | 2970 | void |
8ddef552 DM |
2971 | lang_add_data (type, exp) |
2972 | int type; | |
2973 | union etree_union *exp; | |
2fa0b342 DHW |
2974 | { |
2975 | ||
075d7359 | 2976 | lang_data_statement_type *new = new_stat (lang_data_statement, |
2fa0b342 | 2977 | stat_ptr); |
075d7359 SC |
2978 | |
2979 | new->exp = exp; | |
2980 | new->type = type; | |
2fa0b342 DHW |
2981 | |
2982 | } | |
075d7359 | 2983 | |
4fdbafb2 ILT |
2984 | /* Create a new reloc statement. RELOC is the BFD relocation type to |
2985 | generate. HOWTO is the corresponding howto structure (we could | |
2986 | look this up, but the caller has already done so). SECTION is the | |
2987 | section to generate a reloc against, or NAME is the name of the | |
2988 | symbol to generate a reloc against. Exactly one of SECTION and | |
2989 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
2990 | ||
2991 | void | |
2992 | lang_add_reloc (reloc, howto, section, name, addend) | |
2993 | bfd_reloc_code_real_type reloc; | |
cc38364d | 2994 | reloc_howto_type *howto; |
4fdbafb2 ILT |
2995 | asection *section; |
2996 | const char *name; | |
2997 | union etree_union *addend; | |
2998 | { | |
2999 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
3000 | ||
3001 | p->reloc = reloc; | |
3002 | p->howto = howto; | |
3003 | p->section = section; | |
3004 | p->name = name; | |
3005 | p->addend_exp = addend; | |
3006 | ||
3007 | p->addend_value = 0; | |
3008 | p->output_section = NULL; | |
3009 | p->output_vma = 0; | |
3010 | } | |
3011 | ||
2fa0b342 | 3012 | void |
8ddef552 DM |
3013 | lang_add_assignment (exp) |
3014 | etree_type * exp; | |
2fa0b342 | 3015 | { |
075d7359 SC |
3016 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, |
3017 | stat_ptr); | |
3018 | ||
2fa0b342 DHW |
3019 | new->exp = exp; |
3020 | } | |
3021 | ||
3022 | void | |
8ddef552 DM |
3023 | lang_add_attribute (attribute) |
3024 | enum statement_enum attribute; | |
2fa0b342 | 3025 | { |
075d7359 | 3026 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); |
2fa0b342 DHW |
3027 | } |
3028 | ||
075d7359 | 3029 | void |
8ddef552 DM |
3030 | lang_startup (name) |
3031 | CONST char *name; | |
2fa0b342 | 3032 | { |
075d7359 SC |
3033 | if (startup_file != (char *) NULL) |
3034 | { | |
ddddcdf0 | 3035 | einfo ("%P%Fmultiple STARTUP files\n"); |
075d7359 | 3036 | } |
2fa0b342 DHW |
3037 | first_file->filename = name; |
3038 | first_file->local_sym_name = name; | |
193c5f93 | 3039 | first_file->real = true; |
2fa0b342 | 3040 | |
075d7359 | 3041 | startup_file = name; |
2fa0b342 | 3042 | } |
075d7359 SC |
3043 | |
3044 | void | |
8ddef552 DM |
3045 | lang_float (maybe) |
3046 | boolean maybe; | |
2fa0b342 DHW |
3047 | { |
3048 | lang_float_flag = maybe; | |
3049 | } | |
3050 | ||
075d7359 | 3051 | void |
8ddef552 DM |
3052 | lang_leave_output_section_statement (fill, memspec) |
3053 | bfd_vma fill; | |
3054 | CONST char *memspec; | |
2fa0b342 DHW |
3055 | { |
3056 | current_section->fill = fill; | |
075d7359 | 3057 | current_section->region = lang_memory_region_lookup (memspec); |
2fa0b342 DHW |
3058 | stat_ptr = &statement_list; |
3059 | } | |
075d7359 | 3060 | |
9f32f7c2 SC |
3061 | /* |
3062 | Create an absolute symbol with the given name with the value of the | |
3063 | address of first byte of the section named. | |
2fa0b342 | 3064 | |
9f32f7c2 SC |
3065 | If the symbol already exists, then do nothing. |
3066 | */ | |
8cb5eb31 | 3067 | void |
c477527c ILT |
3068 | lang_abs_symbol_at_beginning_of (secname, name) |
3069 | const char *secname; | |
3070 | const char *name; | |
075d7359 | 3071 | { |
c477527c ILT |
3072 | struct bfd_link_hash_entry *h; |
3073 | ||
3074 | h = bfd_link_hash_lookup (link_info.hash, name, true, true, true); | |
3075 | if (h == (struct bfd_link_hash_entry *) NULL) | |
3076 | einfo ("%P%F: bfd_link_hash_lookup failed: %E\n"); | |
3077 | ||
3078 | if (h->type == bfd_link_hash_new | |
3079 | || h->type == bfd_link_hash_undefined) | |
075d7359 | 3080 | { |
c477527c | 3081 | asection *sec; |
075d7359 | 3082 | |
c477527c ILT |
3083 | h->type = bfd_link_hash_defined; |
3084 | ||
3085 | sec = bfd_get_section_by_name (output_bfd, secname); | |
3086 | if (sec == (asection *) NULL) | |
3087 | h->u.def.value = 0; | |
075d7359 | 3088 | else |
c477527c ILT |
3089 | h->u.def.value = bfd_get_section_vma (output_bfd, sec); |
3090 | ||
686739e2 | 3091 | h->u.def.section = bfd_abs_section_ptr; |
9f32f7c2 | 3092 | } |
8cb5eb31 SC |
3093 | } |
3094 | ||
9f32f7c2 SC |
3095 | /* |
3096 | Create an absolute symbol with the given name with the value of the | |
3097 | address of the first byte after the end of the section named. | |
3098 | ||
3099 | If the symbol already exists, then do nothing. | |
3100 | */ | |
2fa0b342 | 3101 | void |
c477527c ILT |
3102 | lang_abs_symbol_at_end_of (secname, name) |
3103 | const char *secname; | |
3104 | const char *name; | |
075d7359 | 3105 | { |
c477527c ILT |
3106 | struct bfd_link_hash_entry *h; |
3107 | ||
3108 | h = bfd_link_hash_lookup (link_info.hash, name, true, true, true); | |
3109 | if (h == (struct bfd_link_hash_entry *) NULL) | |
3110 | einfo ("%P%F: bfd_link_hash_lookup failed: %E\n"); | |
3111 | ||
3112 | if (h->type == bfd_link_hash_new | |
3113 | || h->type == bfd_link_hash_undefined) | |
075d7359 | 3114 | { |
c477527c | 3115 | asection *sec; |
075d7359 | 3116 | |
c477527c | 3117 | h->type = bfd_link_hash_defined; |
075d7359 | 3118 | |
c477527c ILT |
3119 | sec = bfd_get_section_by_name (output_bfd, secname); |
3120 | if (sec == (asection *) NULL) | |
3121 | h->u.def.value = 0; | |
075d7359 | 3122 | else |
c477527c ILT |
3123 | h->u.def.value = (bfd_get_section_vma (output_bfd, sec) |
3124 | + bfd_section_size (output_bfd, sec)); | |
3125 | ||
686739e2 | 3126 | h->u.def.section = bfd_abs_section_ptr; |
9f32f7c2 | 3127 | } |
2fa0b342 DHW |
3128 | } |
3129 | ||
075d7359 | 3130 | void |
8ddef552 DM |
3131 | lang_statement_append (list, element, field) |
3132 | lang_statement_list_type * list; | |
3133 | lang_statement_union_type * element; | |
3134 | lang_statement_union_type ** field; | |
2fa0b342 DHW |
3135 | { |
3136 | *(list->tail) = element; | |
3137 | list->tail = field; | |
3138 | } | |
3139 | ||
173a0c3d | 3140 | /* Set the output format type. -oformat overrides scripts. */ |
6a02a973 | 3141 | |
097879bc | 3142 | void |
6a02a973 ILT |
3143 | lang_add_output_format (format, big, little, from_script) |
3144 | const char *format; | |
3145 | const char *big; | |
3146 | const char *little; | |
173a0c3d | 3147 | int from_script; |
097879bc | 3148 | { |
2c6635a4 | 3149 | if (output_target == NULL || !from_script) |
6a02a973 ILT |
3150 | { |
3151 | if (command_line.endian == ENDIAN_BIG | |
3152 | && big != NULL) | |
3153 | format = big; | |
3154 | else if (command_line.endian == ENDIAN_LITTLE | |
3155 | && little != NULL) | |
3156 | format = little; | |
3157 | ||
3158 | output_target = format; | |
3159 | } | |
097879bc | 3160 | } |
309c8153 ILT |
3161 | |
3162 | /* Enter a group. This creates a new lang_group_statement, and sets | |
3163 | stat_ptr to build new statements within the group. */ | |
3164 | ||
3165 | void | |
3166 | lang_enter_group () | |
3167 | { | |
3168 | lang_group_statement_type *g; | |
3169 | ||
3170 | g = new_stat (lang_group_statement, stat_ptr); | |
3171 | lang_list_init (&g->children); | |
3172 | stat_ptr = &g->children; | |
3173 | } | |
3174 | ||
3175 | /* Leave a group. This just resets stat_ptr to start writing to the | |
3176 | regular list of statements again. Note that this will not work if | |
3177 | groups can occur inside anything else which can adjust stat_ptr, | |
3178 | but currently they can't. */ | |
3179 | ||
3180 | void | |
3181 | lang_leave_group () | |
3182 | { | |
3183 | stat_ptr = &statement_list; | |
3184 | } | |
061e65f8 ILT |
3185 | |
3186 | /* Add a new program header. This is called for each entry in a PHDRS | |
3187 | command in a linker script. */ | |
3188 | ||
3189 | void | |
708f7fbc | 3190 | lang_new_phdr (name, type, filehdr, phdrs, at, flags) |
061e65f8 ILT |
3191 | const char *name; |
3192 | etree_type *type; | |
708f7fbc ILT |
3193 | boolean filehdr; |
3194 | boolean phdrs; | |
061e65f8 | 3195 | etree_type *at; |
708f7fbc | 3196 | etree_type *flags; |
061e65f8 ILT |
3197 | { |
3198 | struct lang_phdr *n, **pp; | |
3199 | ||
3200 | n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr)); | |
3201 | n->next = NULL; | |
3202 | n->name = name; | |
3203 | n->type = exp_get_value_int (type, 0, "program header type", | |
3204 | lang_final_phase_enum); | |
708f7fbc ILT |
3205 | n->filehdr = filehdr; |
3206 | n->phdrs = phdrs; | |
061e65f8 | 3207 | n->at = at; |
708f7fbc | 3208 | n->flags = flags; |
061e65f8 ILT |
3209 | |
3210 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
3211 | ; | |
3212 | *pp = n; | |
3213 | } | |
3214 | ||
3215 | /* Record that a section should be placed in a phdr. */ | |
3216 | ||
3217 | void | |
3218 | lang_section_in_phdr (name) | |
3219 | const char *name; | |
3220 | { | |
3221 | struct lang_output_section_phdr_list *n; | |
3222 | ||
3223 | n = ((struct lang_output_section_phdr_list *) | |
3224 | stat_alloc (sizeof (struct lang_output_section_phdr_list))); | |
3225 | n->name = name; | |
3226 | n->used = false; | |
3227 | n->next = current_section->phdrs; | |
3228 | current_section->phdrs = n; | |
3229 | } | |
3230 | ||
3231 | /* Record the program header information in the output BFD. FIXME: We | |
3232 | should not be calling an ELF specific function here. */ | |
3233 | ||
3234 | static void | |
3235 | lang_record_phdrs () | |
3236 | { | |
3237 | unsigned int alc; | |
3238 | asection **secs; | |
3239 | struct lang_output_section_phdr_list *last; | |
3240 | struct lang_phdr *l; | |
3241 | lang_statement_union_type *u; | |
3242 | ||
3243 | alc = 10; | |
3244 | secs = xmalloc (alc * sizeof (asection *)); | |
3245 | last = NULL; | |
3246 | for (l = lang_phdr_list; l != NULL; l = l->next) | |
3247 | { | |
3248 | unsigned int c; | |
708f7fbc | 3249 | flagword flags; |
061e65f8 ILT |
3250 | bfd_vma at; |
3251 | ||
3252 | c = 0; | |
3253 | for (u = lang_output_section_statement.head; | |
3254 | u != NULL; | |
3255 | u = u->output_section_statement.next) | |
3256 | { | |
3257 | lang_output_section_statement_type *os; | |
3258 | struct lang_output_section_phdr_list *pl; | |
3259 | ||
3260 | os = &u->output_section_statement; | |
3261 | ||
708f7fbc | 3262 | pl = os->phdrs; |
061e65f8 ILT |
3263 | if (pl != NULL) |
3264 | last = pl; | |
3265 | else | |
3266 | { | |
3267 | if (! os->loadable | |
3268 | || os->bfd_section == NULL | |
3269 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
3270 | continue; | |
3271 | pl = last; | |
3272 | } | |
3273 | ||
3274 | if (os->bfd_section == NULL) | |
3275 | continue; | |
3276 | ||
3277 | for (; pl != NULL; pl = pl->next) | |
3278 | { | |
3279 | if (strcmp (pl->name, l->name) == 0) | |
3280 | { | |
3281 | if (c >= alc) | |
3282 | { | |
3283 | alc *= 2; | |
3284 | secs = xrealloc (secs, alc * sizeof (asection *)); | |
3285 | } | |
3286 | secs[c] = os->bfd_section; | |
3287 | ++c; | |
3288 | pl->used = true; | |
3289 | } | |
3290 | } | |
3291 | } | |
3292 | ||
708f7fbc ILT |
3293 | if (l->flags == NULL) |
3294 | flags = 0; | |
3295 | else | |
3296 | flags = exp_get_vma (l->flags, 0, "phdr flags", | |
3297 | lang_final_phase_enum); | |
3298 | ||
061e65f8 ILT |
3299 | if (l->at == NULL) |
3300 | at = 0; | |
3301 | else | |
3302 | at = exp_get_vma (l->at, 0, "phdr load address", | |
3303 | lang_final_phase_enum); | |
708f7fbc ILT |
3304 | |
3305 | if (! bfd_record_phdr (output_bfd, l->type, | |
3306 | l->flags == NULL ? false : true, | |
3307 | flags, | |
061e65f8 | 3308 | l->at == NULL ? false : true, |
708f7fbc | 3309 | at, l->filehdr, l->phdrs, c, secs)) |
061e65f8 ILT |
3310 | einfo ("%F%P: bfd_record_phdr failed: %E\n"); |
3311 | } | |
3312 | ||
3313 | free (secs); | |
3314 | ||
3315 | /* Make sure all the phdr assignments succeeded. */ | |
3316 | for (u = lang_output_section_statement.head; | |
3317 | u != NULL; | |
3318 | u = u->output_section_statement.next) | |
3319 | { | |
3320 | struct lang_output_section_phdr_list *pl; | |
3321 | ||
3322 | if (u->output_section_statement.bfd_section == NULL) | |
3323 | continue; | |
3324 | ||
3325 | for (pl = u->output_section_statement.phdrs; | |
3326 | pl != NULL; | |
3327 | pl = pl->next) | |
3328 | if (! pl->used && strcmp (pl->name, "NONE") != 0) | |
3329 | einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n", | |
3330 | u->output_section_statement.name, pl->name); | |
3331 | } | |
3332 | } |