Commit | Line | Data |
---|---|---|
e20873a7 | 1 | /* Linker command language support. |
a4aeaacf | 2 | Copyright 1991, 1992, 1993 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 | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
2fa0b342 | 20 | #include "bfd.h" |
075d7359 | 21 | #include "sysdep.h" |
2fa0b342 DHW |
22 | |
23 | #include "ld.h" | |
24 | #include "ldmain.h" | |
25 | #include "ldsym.h" | |
7bc4a0d7 | 26 | #include "ldgram.h" |
81016051 | 27 | #include "ldwarn.h" |
2fa0b342 DHW |
28 | #include "ldlang.h" |
29 | #include "ldexp.h" | |
8c514453 | 30 | #include "ldemul.h" |
2fa0b342 | 31 | #include "ldlex.h" |
3f8d46e7 | 32 | #include "ldmisc.h" |
c611e285 SC |
33 | #include "ldindr.h" |
34 | #include "ldctor.h" | |
b98bd3db | 35 | |
2fa0b342 | 36 | /* FORWARDS */ |
e20873a7 JG |
37 | static void print_statements PARAMS ((void)); |
38 | static void print_statement PARAMS ((lang_statement_union_type *, | |
075d7359 | 39 | lang_output_section_statement_type *)); |
5af8e9d6 KR |
40 | static lang_statement_union_type *new_statement PARAMS ((enum statement_enum, |
41 | size_t, | |
42 | lang_statement_list_type*)); | |
43 | ||
2fa0b342 | 44 | |
d4c02e29 | 45 | /* LOCALS */ |
bfbdc80f | 46 | static struct obstack stat_obstack; |
075d7359 | 47 | |
bfbdc80f SC |
48 | #define obstack_chunk_alloc ldmalloc |
49 | #define obstack_chunk_free free | |
075d7359 | 50 | static CONST char *startup_file; |
d4c02e29 | 51 | static lang_statement_list_type input_file_chain; |
81016051 SC |
52 | |
53 | /* Points to the last statement in the .data section, so we can add | |
54 | stuff to the data section without pain */ | |
55 | static lang_statement_list_type end_of_data_section_statement_list; | |
56 | ||
c611e285 SC |
57 | /* List of statements needed to handle constructors */ |
58 | extern lang_statement_list_type constructor_list; | |
81016051 | 59 | |
d4c02e29 SC |
60 | static boolean placed_commons = false; |
61 | static lang_output_section_statement_type *default_common_section; | |
62 | static boolean map_option_f; | |
63 | static bfd_vma print_dot; | |
64 | static lang_input_statement_type *first_file; | |
65 | static lang_statement_list_type lang_output_section_statement; | |
66 | static CONST char *current_target; | |
67 | static CONST char *output_target; | |
68 | static size_t longest_section_name = 8; | |
d4c02e29 SC |
69 | static section_userdata_type common_section_userdata; |
70 | static lang_statement_list_type statement_list; | |
ffc50032 | 71 | |
2fa0b342 | 72 | /* EXPORTS */ |
c611e285 | 73 | boolean relaxing; |
ffc50032 | 74 | lang_output_section_statement_type *abs_output_section; |
d4c02e29 SC |
75 | lang_statement_list_type *stat_ptr = &statement_list; |
76 | lang_input_statement_type *script_file = 0; | |
075d7359 SC |
77 | lang_statement_list_type file_chain = |
78 | {0}; | |
d4c02e29 | 79 | CONST char *entry_symbol = 0; |
3f8d46e7 | 80 | bfd_size_type largest_section = 0; |
d4c02e29 SC |
81 | boolean lang_has_input_file = false; |
82 | lang_output_section_statement_type *create_object_symbols = 0; | |
83 | boolean had_output_filename = false; | |
84 | boolean lang_float_flag = false; | |
97fbbaca | 85 | boolean delete_output_file_on_failure = false; |
075d7359 | 86 | |
d4c02e29 | 87 | /* IMPORTS */ |
075d7359 | 88 | extern char *default_target; |
2fa0b342 | 89 | |
d4c02e29 | 90 | extern char *current_file; |
2fa0b342 | 91 | extern bfd *output_bfd; |
2fa0b342 DHW |
92 | extern enum bfd_architecture ldfile_output_architecture; |
93 | extern unsigned long ldfile_output_machine; | |
94 | extern char *ldfile_output_machine_name; | |
2fa0b342 | 95 | extern ldsym_type *symbol_head; |
d4c02e29 | 96 | extern unsigned int commons_pending; |
2fa0b342 DHW |
97 | extern args_type command_line; |
98 | extern ld_config_type config; | |
2fa0b342 | 99 | extern boolean write_map; |
1b8a42f3 | 100 | extern int g_switch_value; |
2fa0b342 | 101 | |
9fce28ed SC |
102 | |
103 | etree_type *base; /* Relocation base - or null */ | |
104 | ||
105 | ||
2fa0b342 DHW |
106 | #ifdef __STDC__ |
107 | #define cat(a,b) a##b | |
108 | #else | |
109 | #define cat(a,b) a/**/b | |
110 | #endif | |
111 | ||
112 | #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y) | |
113 | ||
114 | #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma) | |
115 | ||
116 | #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section)) | |
117 | ||
e20873a7 | 118 | void lang_add_data PARAMS ((int type, union etree_union * exp)); |
bfbdc80f SC |
119 | |
120 | PTR | |
8ddef552 DM |
121 | stat_alloc (size) |
122 | size_t size; | |
bfbdc80f | 123 | { |
075d7359 | 124 | return obstack_alloc (&stat_obstack, size); |
bfbdc80f | 125 | } |
075d7359 | 126 | static void |
8ddef552 DM |
127 | print_size (value) |
128 | size_t value; | |
3f8d46e7 | 129 | { |
075d7359 | 130 | fprintf (config.map_file, "%5x", (unsigned) value); |
3f8d46e7 | 131 | } |
075d7359 | 132 | static void |
8ddef552 DM |
133 | print_alignment (value) |
134 | unsigned int value; | |
3f8d46e7 | 135 | { |
075d7359 | 136 | fprintf (config.map_file, "2**%1u", value); |
3f8d46e7 | 137 | } |
075d7359 SC |
138 | static void |
139 | DEFUN (print_fill, (value), | |
8ddef552 | 140 | fill_type value) |
3f8d46e7 | 141 | { |
075d7359 | 142 | fprintf (config.map_file, "%04x", (unsigned) value); |
3f8d46e7 JG |
143 | } |
144 | ||
d4c02e29 | 145 | |
075d7359 | 146 | static void |
8ddef552 DM |
147 | print_section (name) |
148 | CONST char *CONST name; | |
3f8d46e7 | 149 | { |
075d7359 | 150 | fprintf (config.map_file, "%*s", -longest_section_name, name); |
3f8d46e7 | 151 | } |
2fa0b342 | 152 | |
1418c83b SC |
153 | /*---------------------------------------------------------------------- |
154 | lang_for_each_statement walks the parse tree and calls the provided | |
155 | function for each node | |
156 | */ | |
157 | ||
075d7359 | 158 | static void |
8ddef552 DM |
159 | lang_for_each_statement_worker (func, s) |
160 | void (*func) (); | |
161 | lang_statement_union_type *s; | |
1418c83b | 162 | { |
075d7359 SC |
163 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
164 | { | |
165 | func (s); | |
1418c83b | 166 | |
075d7359 SC |
167 | switch (s->header.type) |
168 | { | |
81016051 | 169 | case lang_constructors_statement_enum: |
075d7359 | 170 | lang_for_each_statement_worker (func, constructor_list.head); |
81016051 | 171 | break; |
1418c83b SC |
172 | case lang_output_section_statement_enum: |
173 | lang_for_each_statement_worker | |
075d7359 | 174 | (func, |
1418c83b SC |
175 | s->output_section_statement.children.head); |
176 | break; | |
177 | case lang_wild_statement_enum: | |
178 | lang_for_each_statement_worker | |
075d7359 | 179 | (func, |
1418c83b SC |
180 | s->wild_statement.children.head); |
181 | break; | |
182 | case lang_data_statement_enum: | |
183 | case lang_object_symbols_statement_enum: | |
184 | case lang_output_statement_enum: | |
185 | case lang_target_statement_enum: | |
186 | case lang_input_section_enum: | |
187 | case lang_input_statement_enum: | |
1418c83b SC |
188 | case lang_assignment_statement_enum: |
189 | case lang_padding_statement_enum: | |
190 | case lang_address_statement_enum: | |
191 | break; | |
192 | default: | |
075d7359 | 193 | FAIL (); |
1418c83b SC |
194 | break; |
195 | } | |
075d7359 | 196 | } |
1418c83b SC |
197 | } |
198 | ||
199 | void | |
8ddef552 DM |
200 | lang_for_each_statement (func) |
201 | void (*func) (); | |
1418c83b | 202 | { |
075d7359 SC |
203 | lang_for_each_statement_worker (func, |
204 | statement_list.head); | |
1418c83b | 205 | } |
075d7359 | 206 | |
1418c83b | 207 | /*----------------------------------------------------------------------*/ |
075d7359 | 208 | void |
8ddef552 DM |
209 | lang_list_init (list) |
210 | lang_statement_list_type *list; | |
2fa0b342 | 211 | { |
075d7359 SC |
212 | list->head = (lang_statement_union_type *) NULL; |
213 | list->tail = &list->head; | |
2fa0b342 DHW |
214 | } |
215 | ||
1418c83b | 216 | /*---------------------------------------------------------------------- |
075d7359 | 217 | |
1418c83b SC |
218 | build a new statement node for the parse tree |
219 | ||
220 | */ | |
2fa0b342 DHW |
221 | |
222 | static | |
075d7359 | 223 | lang_statement_union_type * |
8ddef552 DM |
224 | new_statement (type, size, list) |
225 | enum statement_enum type; | |
5af8e9d6 | 226 | size_t size; |
8ddef552 | 227 | lang_statement_list_type * list; |
2fa0b342 DHW |
228 | { |
229 | lang_statement_union_type *new = (lang_statement_union_type *) | |
075d7359 SC |
230 | stat_alloc (size); |
231 | ||
2fa0b342 | 232 | new->header.type = type; |
075d7359 SC |
233 | new->header.next = (lang_statement_union_type *) NULL; |
234 | lang_statement_append (list, new, &new->header.next); | |
2fa0b342 DHW |
235 | return new; |
236 | } | |
237 | ||
1418c83b SC |
238 | /* |
239 | Build a new input file node for the language. There are several ways | |
240 | in which we treat an input file, eg, we only look at symbols, or | |
241 | prefix it with a -l etc. | |
242 | ||
243 | We can be supplied with requests for input files more than once; | |
244 | they may, for example be split over serveral lines like foo.o(.text) | |
245 | foo.o(.data) etc, so when asked for a file we check that we havn't | |
246 | got it already so we don't duplicate the bfd. | |
247 | ||
248 | */ | |
2fa0b342 | 249 | static lang_input_statement_type * |
8ddef552 DM |
250 | new_afile (name, file_type, target) |
251 | CONST char *CONST name; | |
252 | CONST lang_input_file_enum_type file_type; | |
253 | CONST char *CONST target; | |
2fa0b342 | 254 | { |
dc4726c2 | 255 | |
075d7359 SC |
256 | lang_input_statement_type *p = new_stat (lang_input_statement, |
257 | stat_ptr); | |
258 | ||
2fa0b342 DHW |
259 | lang_has_input_file = true; |
260 | p->target = target; | |
ee4af9e8 | 261 | p->complained = false; |
075d7359 SC |
262 | switch (file_type) |
263 | { | |
264 | case lang_input_file_is_symbols_only_enum: | |
265 | p->filename = name; | |
266 | p->is_archive = false; | |
267 | p->real = true; | |
268 | p->local_sym_name = name; | |
269 | p->just_syms_flag = true; | |
270 | p->search_dirs_flag = false; | |
271 | break; | |
272 | case lang_input_file_is_fake_enum: | |
273 | p->filename = name; | |
274 | p->is_archive = false; | |
275 | p->real = false; | |
276 | p->local_sym_name = name; | |
277 | p->just_syms_flag = false; | |
278 | p->search_dirs_flag = false; | |
279 | break; | |
280 | case lang_input_file_is_l_enum: | |
281 | p->is_archive = true; | |
282 | p->filename = name; | |
283 | p->real = true; | |
284 | p->local_sym_name = concat ("-l", name, ""); | |
285 | p->just_syms_flag = false; | |
286 | p->search_dirs_flag = true; | |
287 | break; | |
288 | case lang_input_file_is_search_file_enum: | |
289 | case lang_input_file_is_marker_enum: | |
290 | p->filename = name; | |
291 | p->is_archive = false; | |
292 | p->real = true; | |
293 | p->local_sym_name = name; | |
294 | p->just_syms_flag = false; | |
295 | p->search_dirs_flag = true; | |
296 | break; | |
297 | case lang_input_file_is_file_enum: | |
298 | p->filename = name; | |
299 | p->is_archive = false; | |
300 | p->real = true; | |
301 | p->local_sym_name = name; | |
302 | p->just_syms_flag = false; | |
303 | p->search_dirs_flag = false; | |
304 | break; | |
305 | default: | |
306 | FAIL (); | |
307 | } | |
308 | p->asymbols = (asymbol **) NULL; | |
309 | p->superfile = (lang_input_statement_type *) NULL; | |
310 | p->next_real_file = (lang_statement_union_type *) NULL; | |
311 | p->next = (lang_statement_union_type *) NULL; | |
2fa0b342 | 312 | p->symbol_count = 0; |
075d7359 SC |
313 | p->common_output_section = (asection *) NULL; |
314 | lang_statement_append (&input_file_chain, | |
315 | (lang_statement_union_type *) p, | |
316 | &p->next_real_file); | |
2fa0b342 DHW |
317 | return p; |
318 | } | |
319 | ||
320 | lang_input_statement_type * | |
8ddef552 DM |
321 | lang_add_input_file (name, file_type, target) |
322 | CONST char *name; | |
323 | lang_input_file_enum_type file_type; | |
324 | CONST char *target; | |
2fa0b342 DHW |
325 | { |
326 | /* Look it up or build a new one */ | |
1418c83b | 327 | lang_has_input_file = true; |
a4aeaacf | 328 | |
1418c83b | 329 | #if 0 |
2fa0b342 DHW |
330 | lang_input_statement_type *p; |
331 | ||
075d7359 SC |
332 | for (p = (lang_input_statement_type *) input_file_chain.head; |
333 | p != (lang_input_statement_type *) NULL; | |
334 | p = (lang_input_statement_type *) (p->next_real_file)) | |
335 | { | |
336 | /* Sometimes we have incomplete entries in here */ | |
337 | if (p->filename != (char *) NULL) | |
338 | { | |
339 | if (strcmp (name, p->filename) == 0) | |
340 | return p; | |
1418c83b | 341 | } |
075d7359 SC |
342 | |
343 | } | |
1418c83b | 344 | #endif |
075d7359 | 345 | return new_afile (name, file_type, target); |
2fa0b342 DHW |
346 | } |
347 | ||
81150d34 | 348 | void |
8ddef552 DM |
349 | lang_add_keepsyms_file (filename) |
350 | CONST char *filename; | |
81150d34 ILT |
351 | { |
352 | extern strip_symbols_type strip_symbols; | |
353 | if (keepsyms_file != 0) | |
a4aeaacf | 354 | info_msg ("%X%P: error: duplicated keep-symbols-file value\n"); |
81150d34 ILT |
355 | keepsyms_file = filename; |
356 | if (strip_symbols != STRIP_NONE) | |
a4aeaacf | 357 | info_msg ("%P: `-keep-only-symbols-file' overrides `-s' and `-S'\n"); |
81150d34 ILT |
358 | strip_symbols = STRIP_SOME; |
359 | } | |
360 | ||
1418c83b | 361 | /* Build enough state so that the parser can build its tree */ |
2fa0b342 | 362 | void |
8ddef552 | 363 | lang_init () |
2fa0b342 | 364 | { |
075d7359 | 365 | obstack_begin (&stat_obstack, 1000); |
2fa0b342 | 366 | |
075d7359 | 367 | stat_ptr = &statement_list; |
bfbdc80f | 368 | |
075d7359 | 369 | lang_list_init (stat_ptr); |
2fa0b342 | 370 | |
075d7359 SC |
371 | lang_list_init (&input_file_chain); |
372 | lang_list_init (&lang_output_section_statement); | |
373 | lang_list_init (&file_chain); | |
374 | first_file = lang_add_input_file ((char *) NULL, | |
375 | lang_input_file_is_marker_enum, | |
376 | (char *) NULL); | |
377 | abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); | |
9d1fe8a4 | 378 | |
bfbdc80f | 379 | abs_output_section->bfd_section = &bfd_abs_section; |
9d1fe8a4 | 380 | |
2fa0b342 DHW |
381 | } |
382 | ||
1418c83b | 383 | /*---------------------------------------------------------------------- |
075d7359 SC |
384 | A region is an area of memory declared with the |
385 | MEMORY { name:org=exp, len=exp ... } | |
386 | syntax. | |
2fa0b342 | 387 | |
1418c83b | 388 | We maintain a list of all the regions here |
2fa0b342 | 389 | |
1418c83b SC |
390 | If no regions are specified in the script, then the default is used |
391 | which is created when looked up to be the entire data space | |
2fa0b342 DHW |
392 | */ |
393 | ||
394 | static lang_memory_region_type *lang_memory_region_list; | |
395 | static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; | |
396 | ||
397 | lang_memory_region_type * | |
8ddef552 DM |
398 | lang_memory_region_lookup (name) |
399 | CONST char *CONST name; | |
2fa0b342 DHW |
400 | { |
401 | ||
075d7359 SC |
402 | lang_memory_region_type *p = lang_memory_region_list; |
403 | ||
1418c83b | 404 | for (p = lang_memory_region_list; |
075d7359 SC |
405 | p != (lang_memory_region_type *) NULL; |
406 | p = p->next) | |
407 | { | |
408 | if (strcmp (p->name, name) == 0) | |
409 | { | |
410 | return p; | |
411 | } | |
2fa0b342 | 412 | } |
075d7359 | 413 | if (strcmp (name, "*default*") == 0) |
2fa0b342 | 414 | { |
075d7359 SC |
415 | /* This is the default region, dig out first one on the list */ |
416 | if (lang_memory_region_list != (lang_memory_region_type *) NULL) | |
417 | { | |
418 | return lang_memory_region_list; | |
419 | } | |
2fa0b342 | 420 | } |
075d7359 SC |
421 | { |
422 | lang_memory_region_type *new = | |
423 | (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type))); | |
424 | ||
425 | new->name = buystring (name); | |
426 | new->next = (lang_memory_region_type *) NULL; | |
427 | ||
428 | *lang_memory_region_list_tail = new; | |
429 | lang_memory_region_list_tail = &new->next; | |
430 | new->origin = 0; | |
8ddef552 | 431 | new->length = ~(bfd_size_type)0; |
075d7359 SC |
432 | new->current = 0; |
433 | new->had_full_message = false; | |
434 | ||
435 | return new; | |
436 | } | |
2fa0b342 DHW |
437 | } |
438 | ||
439 | ||
2fa0b342 | 440 | lang_output_section_statement_type * |
8ddef552 DM |
441 | lang_output_section_find (name) |
442 | CONST char *CONST name; | |
2fa0b342 DHW |
443 | { |
444 | lang_statement_union_type *u; | |
445 | lang_output_section_statement_type *lookup; | |
446 | ||
447 | for (u = lang_output_section_statement.head; | |
075d7359 | 448 | u != (lang_statement_union_type *) NULL; |
2fa0b342 | 449 | u = lookup->next) |
075d7359 SC |
450 | { |
451 | lookup = &u->output_section_statement; | |
452 | if (strcmp (name, lookup->name) == 0) | |
453 | { | |
1418c83b SC |
454 | return lookup; |
455 | } | |
075d7359 SC |
456 | } |
457 | return (lang_output_section_statement_type *) NULL; | |
2fa0b342 DHW |
458 | } |
459 | ||
460 | lang_output_section_statement_type * | |
8ddef552 DM |
461 | lang_output_section_statement_lookup (name) |
462 | CONST char *CONST name; | |
2fa0b342 DHW |
463 | { |
464 | lang_output_section_statement_type *lookup; | |
2fa0b342 | 465 | |
075d7359 SC |
466 | lookup = lang_output_section_find (name); |
467 | if (lookup == (lang_output_section_statement_type *) NULL) | |
468 | { | |
2fa0b342 | 469 | |
075d7359 SC |
470 | lookup = (lang_output_section_statement_type *) |
471 | new_stat (lang_output_section_statement, stat_ptr); | |
472 | lookup->region = (lang_memory_region_type *) NULL; | |
473 | lookup->fill = 0; | |
474 | lookup->block_value = 1; | |
475 | lookup->name = name; | |
476 | ||
477 | lookup->next = (lang_statement_union_type *) NULL; | |
478 | lookup->bfd_section = (asection *) NULL; | |
479 | lookup->processed = false; | |
ae475b39 | 480 | lookup->loadable = 1; |
075d7359 SC |
481 | lookup->addr_tree = (etree_type *) NULL; |
482 | lang_list_init (&lookup->children); | |
483 | ||
1b8a42f3 ILT |
484 | lookup->memspec = (CONST char *) NULL; |
485 | lookup->flags = 0; | |
486 | lookup->subsection_alignment = -1; | |
487 | lookup->section_alignment = -1; | |
488 | lookup->load_base = (union etree_union *) NULL; | |
489 | ||
075d7359 SC |
490 | lang_statement_append (&lang_output_section_statement, |
491 | (lang_statement_union_type *) lookup, | |
492 | &lookup->next); | |
493 | } | |
494 | return lookup; | |
495 | } | |
2fa0b342 | 496 | |
9d1fe8a4 | 497 | /*ARGSUSED*/ |
2fa0b342 | 498 | static void |
8ddef552 DM |
499 | print_flags (ignore_flags) |
500 | int *ignore_flags; | |
2fa0b342 | 501 | { |
075d7359 | 502 | fprintf (config.map_file, "("); |
2fa0b342 | 503 | #if 0 |
075d7359 SC |
504 | if (flags->flag_read) |
505 | fprintf (outfile, "R"); | |
506 | if (flags->flag_write) | |
507 | fprintf (outfile, "W"); | |
508 | if (flags->flag_executable) | |
509 | fprintf (outfile, "X"); | |
510 | if (flags->flag_loadable) | |
511 | fprintf (outfile, "L"); | |
2fa0b342 | 512 | #endif |
ae475b39 | 513 | fprintf (config.map_file, ")"); |
2fa0b342 DHW |
514 | } |
515 | ||
516 | void | |
8ddef552 | 517 | lang_map () |
2fa0b342 DHW |
518 | { |
519 | lang_memory_region_type *m; | |
075d7359 SC |
520 | |
521 | fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n"); | |
48491e2e | 522 | #ifdef HOST_64_BIT |
075d7359 | 523 | fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n"); |
48491e2e | 524 | #else |
ae475b39 SC |
525 | fprintf (config.map_file, |
526 | "name\t\torigin length r_size c_size is attributes\n"); | |
527 | ||
48491e2e | 528 | #endif |
2fa0b342 | 529 | for (m = lang_memory_region_list; |
075d7359 SC |
530 | m != (lang_memory_region_type *) NULL; |
531 | m = m->next) | |
2fa0b342 | 532 | { |
075d7359 SC |
533 | fprintf (config.map_file, "%-16s", m->name); |
534 | print_address (m->origin); | |
535 | print_space (); | |
8ddef552 | 536 | print_address ((bfd_vma)m->length); |
075d7359 | 537 | print_space (); |
8ddef552 | 538 | print_address ((bfd_vma)m->old_length); |
ae475b39 SC |
539 | print_space(); |
540 | print_address (m->current - m->origin); | |
541 | print_space(); | |
542 | if (m->old_length) | |
543 | fprintf(config.map_file," %2d%% ", ( m->current - m->origin) * 100 / m->old_length); | |
075d7359 SC |
544 | print_flags (&m->flags); |
545 | fprintf (config.map_file, "\n"); | |
2fa0b342 | 546 | } |
075d7359 SC |
547 | fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n"); |
548 | fprintf (config.map_file, "output input virtual\n"); | |
549 | fprintf (config.map_file, "section section address tsize\n\n"); | |
2fa0b342 | 550 | |
075d7359 | 551 | print_statements (); |
2fa0b342 DHW |
552 | |
553 | } | |
554 | ||
555 | /* | |
556 | * | |
557 | */ | |
075d7359 | 558 | static void |
8ddef552 DM |
559 | init_os (s) |
560 | lang_output_section_statement_type * s; | |
2fa0b342 | 561 | { |
9d1fe8a4 SC |
562 | /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/ |
563 | section_userdata_type *new = | |
075d7359 SC |
564 | (section_userdata_type *) |
565 | stat_alloc ((bfd_size_type) (sizeof (section_userdata_type))); | |
566 | ||
567 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); | |
568 | if (s->bfd_section == (asection *) NULL) | |
569 | s->bfd_section = bfd_make_section (output_bfd, s->name); | |
570 | if (s->bfd_section == (asection *) NULL) | |
571 | { | |
ddddcdf0 | 572 | einfo ("%P%F: output format %s cannot represent section called %s\n", |
075d7359 | 573 | output_bfd->xvec->name, s->name); |
9d1fe8a4 | 574 | } |
2fa0b342 | 575 | s->bfd_section->output_section = s->bfd_section; |
ae475b39 | 576 | /* s->bfd_section->flags = s->flags;*/ |
7bc4a0d7 | 577 | |
2fa0b342 DHW |
578 | /* We initialize an output sections output offset to minus its own */ |
579 | /* vma to allow us to output a section through itself */ | |
580 | s->bfd_section->output_offset = 0; | |
075d7359 | 581 | get_userdata (s->bfd_section) = (PTR) new; |
c611e285 | 582 | |
2fa0b342 DHW |
583 | } |
584 | ||
1418c83b SC |
585 | /*********************************************************************** |
586 | The wild routines. | |
587 | ||
588 | These expand statements like *(.text) and foo.o to a list of | |
589 | explicit actions, like foo.o(.text), bar.o(.text) and | |
590 | foo.o(.text,.data) . | |
591 | ||
592 | The toplevel routine, wild, takes a statement, section, file and | |
593 | target. If either the section or file is null it is taken to be the | |
594 | wildcard. Seperate lang_input_section statements are created for | |
595 | each part of the expanstion, and placed after the statement provided. | |
596 | ||
597 | */ | |
075d7359 | 598 | |
2fa0b342 | 599 | static void |
8ddef552 DM |
600 | wild_doit (ptr, section, output, file) |
601 | lang_statement_list_type * ptr; | |
602 | asection * section; | |
603 | lang_output_section_statement_type * output; | |
604 | lang_input_statement_type * file; | |
2fa0b342 | 605 | { |
075d7359 | 606 | if (output->bfd_section == (asection *) NULL) |
ae475b39 SC |
607 | { |
608 | init_os (output); | |
e9b63852 ILT |
609 | /* Initialize the vma and size to the existing section. This will |
610 | be overriden in lang_size_sections unless SEC_NEVER_LOAD gets | |
611 | set. */ | |
612 | if (section != (asection *) NULL) | |
613 | { | |
614 | bfd_set_section_vma (0, output->bfd_section, | |
615 | bfd_section_vma (0, section)); | |
616 | output->bfd_section->_raw_size = section->_raw_size; | |
617 | } | |
ae475b39 | 618 | } |
2fa0b342 | 619 | |
075d7359 SC |
620 | if (section != (asection *) NULL |
621 | && section->output_section == (asection *) NULL) | |
ae475b39 SC |
622 | { |
623 | /* Add a section reference to the list */ | |
624 | lang_input_section_type *new = new_stat (lang_input_section, ptr); | |
625 | ||
626 | new->section = section; | |
627 | new->ifile = file; | |
628 | section->output_section = output->bfd_section; | |
29f33467 SC |
629 | |
630 | /* Be selective about what the output section inherits from the | |
631 | input section */ | |
632 | ||
8ddef552 DM |
633 | if ((section->flags & SEC_SHARED_LIBRARY) != 0) |
634 | section->output_section->flags |= section->flags; | |
635 | else | |
636 | section->output_section->flags |= section->flags & ~SEC_NEVER_LOAD; | |
29f33467 | 637 | |
ae475b39 | 638 | if (!output->loadable) |
075d7359 | 639 | { |
29f33467 | 640 | /* Turn off load flag */ |
ae475b39 | 641 | output->bfd_section->flags &= ~SEC_LOAD; |
29f33467 | 642 | output->bfd_section->flags |= SEC_NEVER_LOAD; |
ae475b39 SC |
643 | } |
644 | if (section->alignment_power > output->bfd_section->alignment_power) | |
645 | { | |
646 | output->bfd_section->alignment_power = section->alignment_power; | |
075d7359 | 647 | } |
e20873a7 JG |
648 | /* If supplied an aligmnet, then force it */ |
649 | if (output->section_alignment != -1) | |
650 | { | |
651 | output->bfd_section->alignment_power = output->section_alignment; | |
652 | } | |
ae475b39 | 653 | } |
2fa0b342 DHW |
654 | } |
655 | ||
656 | static asection * | |
8ddef552 DM |
657 | our_bfd_get_section_by_name (abfd, section) |
658 | bfd * abfd; | |
659 | CONST char *section; | |
2fa0b342 | 660 | { |
075d7359 | 661 | return bfd_get_section_by_name (abfd, section); |
2fa0b342 | 662 | } |
1418c83b | 663 | |
075d7359 | 664 | static void |
8ddef552 DM |
665 | wild_section (ptr, section, file, output) |
666 | lang_wild_statement_type * ptr; | |
667 | CONST char *section; | |
668 | lang_input_statement_type * file; | |
669 | lang_output_section_statement_type * output; | |
2fa0b342 DHW |
670 | { |
671 | asection *s; | |
075d7359 SC |
672 | |
673 | if (file->just_syms_flag == false) | |
674 | { | |
675 | if (section == (char *) NULL) | |
676 | { | |
677 | /* Do the creation to all sections in the file */ | |
678 | for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next) | |
29f33467 SC |
679 | { |
680 | /* except for bss */ | |
681 | if ((s->flags & SEC_IS_COMMON) == 0) | |
075d7359 SC |
682 | { |
683 | wild_doit (&ptr->children, s, output, file); | |
684 | } | |
29f33467 | 685 | } |
075d7359 SC |
686 | } |
687 | else | |
688 | { | |
689 | /* Do the creation to the named section only */ | |
690 | wild_doit (&ptr->children, | |
691 | our_bfd_get_section_by_name (file->the_bfd, section), | |
692 | output, file); | |
693 | } | |
2fa0b342 | 694 | } |
2fa0b342 DHW |
695 | } |
696 | ||
1418c83b SC |
697 | /* passed a file name (which must have been seen already and added to |
698 | the statement tree. We will see if it has been opened already and | |
699 | had its symbols read. If not then we'll read it. | |
2fa0b342 | 700 | |
1418c83b SC |
701 | Archives are pecuilar here. We may open them once, but if they do |
702 | not define anything we need at the time, they won't have all their | |
703 | symbols read. If we need them later, we'll have to redo it. | |
704 | */ | |
2fa0b342 | 705 | static |
1418c83b | 706 | lang_input_statement_type * |
8ddef552 DM |
707 | lookup_name (name) |
708 | CONST char *CONST name; | |
2fa0b342 DHW |
709 | { |
710 | lang_input_statement_type *search; | |
075d7359 SC |
711 | |
712 | for (search = (lang_input_statement_type *) input_file_chain.head; | |
713 | search != (lang_input_statement_type *) NULL; | |
714 | search = (lang_input_statement_type *) search->next_real_file) | |
715 | { | |
716 | if (search->filename == (char *) NULL && name == (char *) NULL) | |
717 | { | |
2fa0b342 DHW |
718 | return search; |
719 | } | |
075d7359 SC |
720 | if (search->filename != (char *) NULL && name != (char *) NULL) |
721 | { | |
722 | if (strcmp (search->filename, name) == 0) | |
723 | { | |
724 | ldmain_open_file_read_symbol (search); | |
725 | return search; | |
726 | } | |
1418c83b | 727 | } |
075d7359 | 728 | } |
2fa0b342 | 729 | |
075d7359 SC |
730 | /* There isn't an afile entry for this file yet, this must be |
731 | because the name has only appeared inside a load script and not | |
1418c83b | 732 | on the command line */ |
075d7359 SC |
733 | search = new_afile (name, lang_input_file_is_file_enum, default_target); |
734 | ldmain_open_file_read_symbol (search); | |
2fa0b342 | 735 | return search; |
1418c83b SC |
736 | |
737 | ||
2fa0b342 DHW |
738 | } |
739 | ||
740 | static void | |
8ddef552 DM |
741 | wild (s, section, file, target, output) |
742 | lang_wild_statement_type * s; | |
743 | CONST char *CONST section; | |
744 | CONST char *CONST file; | |
745 | CONST char *CONST target; | |
746 | lang_output_section_statement_type * output; | |
2fa0b342 DHW |
747 | { |
748 | lang_input_statement_type *f; | |
075d7359 SC |
749 | |
750 | if (file == (char *) NULL) | |
751 | { | |
752 | /* Perform the iteration over all files in the list */ | |
753 | for (f = (lang_input_statement_type *) file_chain.head; | |
754 | f != (lang_input_statement_type *) NULL; | |
755 | f = (lang_input_statement_type *) f->next) | |
756 | { | |
757 | wild_section (s, section, f, output); | |
758 | } | |
29f33467 SC |
759 | /* Once more for the script file */ |
760 | wild_section(s, section, script_file, output); | |
075d7359 SC |
761 | } |
762 | else | |
763 | { | |
764 | /* Perform the iteration over a single file */ | |
765 | wild_section (s, section, lookup_name (file), output); | |
766 | } | |
767 | if (section != (char *) NULL | |
768 | && strcmp (section, "COMMON") == 0 | |
769 | && default_common_section == (lang_output_section_statement_type *) NULL) | |
770 | { | |
771 | /* Remember the section that common is going to incase we later | |
772 | get something which doesn't know where to put it */ | |
773 | default_common_section = output; | |
2fa0b342 | 774 | } |
2fa0b342 DHW |
775 | } |
776 | ||
777 | /* | |
075d7359 | 778 | read in all the files |
2fa0b342 | 779 | */ |
97fbbaca | 780 | |
075d7359 | 781 | static bfd * |
8ddef552 DM |
782 | open_output (name) |
783 | CONST char *CONST name; | |
2fa0b342 | 784 | { |
30d1a390 SC |
785 | extern unsigned long ldfile_output_machine; |
786 | extern enum bfd_architecture ldfile_output_architecture; | |
787 | ||
1418c83b | 788 | extern CONST char *output_filename; |
097879bc | 789 | bfd *output; |
097879bc | 790 | |
075d7359 | 791 | if (output_target == (char *) NULL) |
ae475b39 SC |
792 | { |
793 | if (current_target != (char *) NULL) | |
794 | output_target = current_target; | |
795 | else | |
796 | output_target = default_target; | |
797 | } | |
075d7359 SC |
798 | output = bfd_openw (name, output_target); |
799 | output_filename = name; | |
800 | ||
801 | if (output == (bfd *) NULL) | |
802 | { | |
ae475b39 SC |
803 | if (bfd_error == invalid_target) |
804 | { | |
ddddcdf0 | 805 | einfo ("%P%F: target %s not found\n", output_target); |
ae475b39 | 806 | } |
ddddcdf0 | 807 | einfo ("%P%F: cannot open output file %s: %E\n", name); |
075d7359 | 808 | } |
30d1a390 | 809 | |
97fbbaca JL |
810 | delete_output_file_on_failure = 1; |
811 | ||
30d1a390 | 812 | /* output->flags |= D_PAGED;*/ |
075d7359 | 813 | |
ddddcdf0 ILT |
814 | if (! bfd_set_format (output, bfd_object)) |
815 | einfo ("%P%F:%s: can not make object file: %E\n", name); | |
816 | if (! bfd_set_arch_mach (output, | |
817 | ldfile_output_architecture, | |
818 | ldfile_output_machine)) | |
819 | einfo ("%P%F:%s: can not set architecture: %E\n", name); | |
820 | ||
1b8a42f3 | 821 | bfd_set_gp_size (output, g_switch_value); |
2fa0b342 DHW |
822 | return output; |
823 | } | |
1418c83b SC |
824 | |
825 | ||
097879bc | 826 | |
1418c83b | 827 | |
2fa0b342 | 828 | static void |
8ddef552 DM |
829 | ldlang_open_output (statement) |
830 | lang_statement_union_type * statement; | |
2fa0b342 | 831 | { |
075d7359 SC |
832 | switch (statement->header.type) |
833 | { | |
834 | case lang_output_statement_enum: | |
835 | output_bfd = open_output (statement->output_statement.name); | |
836 | ldemul_set_output_arch (); | |
837 | if (config.magic_demand_paged && !config.relocateable_output) | |
838 | output_bfd->flags |= D_PAGED; | |
839 | else | |
840 | output_bfd->flags &= ~D_PAGED; | |
841 | if (config.text_read_only) | |
842 | output_bfd->flags |= WP_TEXT; | |
843 | else | |
844 | output_bfd->flags &= ~WP_TEXT; | |
845 | break; | |
1418c83b | 846 | |
075d7359 SC |
847 | case lang_target_statement_enum: |
848 | current_target = statement->target_statement.target; | |
849 | break; | |
850 | default: | |
851 | break; | |
852 | } | |
1418c83b | 853 | } |
2fa0b342 | 854 | |
1418c83b | 855 | static void |
8ddef552 DM |
856 | open_input_bfds (statement) |
857 | lang_statement_union_type * statement; | |
1418c83b | 858 | { |
075d7359 SC |
859 | switch (statement->header.type) |
860 | { | |
1418c83b | 861 | case lang_target_statement_enum: |
075d7359 SC |
862 | current_target = statement->target_statement.target; |
863 | break; | |
864 | case lang_wild_statement_enum: | |
865 | /* Maybe we should load the file's symbols */ | |
866 | if (statement->wild_statement.filename) | |
867 | { | |
868 | (void) lookup_name (statement->wild_statement.filename); | |
869 | } | |
870 | break; | |
871 | case lang_input_statement_enum: | |
872 | if (statement->input_statement.real == true) | |
873 | { | |
874 | statement->input_statement.target = current_target; | |
875 | lookup_name (statement->input_statement.filename); | |
876 | } | |
877 | break; | |
878 | default: | |
879 | break; | |
880 | } | |
2fa0b342 | 881 | } |
075d7359 | 882 | |
2fa0b342 DHW |
883 | /* If there are [COMMONS] statements, put a wild one into the bss section */ |
884 | ||
885 | static void | |
075d7359 | 886 | lang_reasonable_defaults () |
2fa0b342 | 887 | { |
ffc50032 | 888 | |
ffc50032 | 889 | |
075d7359 | 890 | |
1418c83b | 891 | #if 0 |
075d7359 SC |
892 | lang_output_section_statement_lookup (".text"); |
893 | lang_output_section_statement_lookup (".data"); | |
8cb5eb31 | 894 | |
075d7359 SC |
895 | default_common_section = |
896 | lang_output_section_statement_lookup (".bss"); | |
8cb5eb31 | 897 | |
1418c83b | 898 | |
075d7359 SC |
899 | if (placed_commons == false) |
900 | { | |
901 | lang_wild_statement_type *new = | |
902 | new_stat (lang_wild_statement, | |
903 | &default_common_section->children); | |
904 | ||
905 | new->section_name = "COMMON"; | |
906 | new->filename = (char *) NULL; | |
907 | lang_list_init (&new->children); | |
908 | } | |
1418c83b | 909 | #endif |
8cb5eb31 | 910 | |
2fa0b342 DHW |
911 | } |
912 | ||
1418c83b SC |
913 | /* |
914 | Add the supplied name to the symbol table as an undefined reference. | |
915 | Remove items from the chain as we open input bfds | |
916 | */ | |
075d7359 SC |
917 | typedef struct ldlang_undef_chain_list |
918 | { | |
f177a611 | 919 | struct ldlang_undef_chain_list *next; |
1418c83b | 920 | char *name; |
075d7359 | 921 | } ldlang_undef_chain_list_type; |
1418c83b SC |
922 | |
923 | static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head; | |
924 | ||
925 | void | |
8ddef552 DM |
926 | ldlang_add_undef (name) |
927 | CONST char *CONST name; | |
2fa0b342 | 928 | { |
1418c83b | 929 | ldlang_undef_chain_list_type *new = |
075d7359 SC |
930 | (ldlang_undef_chain_list_type |
931 | *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type))); | |
1418c83b SC |
932 | |
933 | new->next = ldlang_undef_chain_list_head; | |
934 | ldlang_undef_chain_list_head = new; | |
935 | ||
075d7359 | 936 | new->name = buystring (name); |
1418c83b | 937 | } |
075d7359 | 938 | |
1418c83b SC |
939 | /* Run through the list of undefineds created above and place them |
940 | into the linker hash table as undefined symbols belonging to the | |
941 | script file. | |
942 | */ | |
943 | static void | |
8ddef552 | 944 | lang_place_undefineds () |
1418c83b SC |
945 | { |
946 | ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head; | |
1418c83b | 947 | |
075d7359 SC |
948 | while (ptr != (ldlang_undef_chain_list_type *) NULL) |
949 | { | |
950 | asymbol *def; | |
951 | asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **))); | |
952 | ||
953 | def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd); | |
954 | *def_ptr = def; | |
955 | def->name = ptr->name; | |
956 | def->section = &bfd_und_section; | |
ddddcdf0 | 957 | enter_global_ref (def_ptr, ptr->name); |
075d7359 SC |
958 | ptr = ptr->next; |
959 | } | |
960 | } | |
1418c83b SC |
961 | |
962 | /* Copy important data from out internal form to the bfd way. Also | |
963 | create a section for the dummy file | |
964 | */ | |
965 | ||
966 | static void | |
8ddef552 | 967 | lang_create_output_section_statements () |
1418c83b | 968 | { |
075d7359 SC |
969 | lang_statement_union_type *os; |
970 | ||
1418c83b | 971 | for (os = lang_output_section_statement.head; |
075d7359 SC |
972 | os != (lang_statement_union_type *) NULL; |
973 | os = os->output_section_statement.next) | |
974 | { | |
975 | lang_output_section_statement_type *s = | |
1418c83b | 976 | &os->output_section_statement; |
075d7359 SC |
977 | |
978 | init_os (s); | |
979 | } | |
1418c83b SC |
980 | |
981 | } | |
982 | ||
983 | static void | |
8ddef552 | 984 | lang_init_script_file () |
1418c83b | 985 | { |
29f33467 | 986 | script_file = lang_add_input_file ("command line", |
075d7359 SC |
987 | lang_input_file_is_fake_enum, |
988 | (char *) NULL); | |
29f33467 | 989 | script_file->the_bfd = bfd_create ("command line", output_bfd); |
1418c83b | 990 | script_file->symbol_count = 0; |
29f33467 SC |
991 | script_file->the_bfd->sections = 0; |
992 | ||
993 | /* The user data of a bfd points to the input statement attatched */ | |
994 | script_file->the_bfd->usrdata = (void *)script_file; | |
995 | script_file->common_section = | |
996 | bfd_make_section(script_file->the_bfd,"COMMON"); | |
997 | ||
998 | abs_output_section = | |
999 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); | |
9d1fe8a4 | 1000 | |
075d7359 | 1001 | abs_output_section->bfd_section = &bfd_abs_section; |
9d1fe8a4 | 1002 | |
2fa0b342 DHW |
1003 | } |
1004 | ||
2fa0b342 DHW |
1005 | /* Open input files and attatch to output sections */ |
1006 | static void | |
8ddef552 DM |
1007 | map_input_to_output_sections (s, target, output_section_statement) |
1008 | lang_statement_union_type * s; | |
1009 | CONST char *target; | |
1010 | lang_output_section_statement_type * output_section_statement; | |
2fa0b342 | 1011 | { |
075d7359 | 1012 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
2fa0b342 | 1013 | { |
075d7359 | 1014 | switch (s->header.type) |
2fa0b342 | 1015 | { |
075d7359 SC |
1016 | |
1017 | ||
1018 | case lang_wild_statement_enum: | |
1019 | wild (&s->wild_statement, s->wild_statement.section_name, | |
1020 | s->wild_statement.filename, target, | |
1021 | output_section_statement); | |
1022 | ||
1023 | break; | |
1024 | case lang_constructors_statement_enum: | |
1025 | map_input_to_output_sections (constructor_list.head, | |
1026 | target, | |
1027 | output_section_statement); | |
1028 | break; | |
1029 | case lang_output_section_statement_enum: | |
1030 | map_input_to_output_sections (s->output_section_statement.children.head, | |
1031 | target, | |
1032 | &s->output_section_statement); | |
1033 | break; | |
1034 | case lang_output_statement_enum: | |
1035 | break; | |
1036 | case lang_target_statement_enum: | |
1037 | target = s->target_statement.target; | |
1038 | break; | |
1039 | case lang_fill_statement_enum: | |
1040 | case lang_input_section_enum: | |
1041 | case lang_object_symbols_statement_enum: | |
1042 | case lang_data_statement_enum: | |
1043 | case lang_assignment_statement_enum: | |
1044 | case lang_padding_statement_enum: | |
1045 | break; | |
1046 | case lang_afile_asection_pair_statement_enum: | |
1047 | FAIL (); | |
1048 | break; | |
1049 | case lang_address_statement_enum: | |
1050 | /* Mark the specified section with the supplied address */ | |
1051 | { | |
1052 | lang_output_section_statement_type *os = | |
2fa0b342 | 1053 | lang_output_section_statement_lookup |
075d7359 SC |
1054 | (s->address_statement.section_name); |
1055 | ||
1056 | os->addr_tree = s->address_statement.address; | |
1057 | if (os->bfd_section == (asection *) NULL) | |
1058 | { | |
ddddcdf0 | 1059 | einfo ("%P%F: cannot set the address of undefined section %s\n", |
075d7359 SC |
1060 | s->address_statement.section_name); |
1061 | } | |
48491e2e | 1062 | } |
075d7359 SC |
1063 | break; |
1064 | case lang_input_statement_enum: | |
1065 | /* A standard input statement, has no wildcards */ | |
1066 | /* ldmain_open_file_read_symbol(&s->input_statement);*/ | |
1067 | break; | |
2fa0b342 | 1068 | } |
2fa0b342 DHW |
1069 | } |
1070 | } | |
1071 | ||
1072 | ||
1073 | ||
1074 | ||
1075 | ||
075d7359 | 1076 | static void |
8ddef552 DM |
1077 | print_output_section_statement (output_section_statement) |
1078 | lang_output_section_statement_type * output_section_statement; | |
2fa0b342 DHW |
1079 | { |
1080 | asection *section = output_section_statement->bfd_section; | |
075d7359 SC |
1081 | |
1082 | print_nl (); | |
1083 | print_section (output_section_statement->name); | |
1084 | ||
e20873a7 | 1085 | |
075d7359 | 1086 | if (section) |
e20873a7 JG |
1087 | { |
1088 | print_dot = section->vma; | |
1089 | print_space (); | |
1090 | print_section (""); | |
1091 | print_space (); | |
1092 | print_address (section->vma); | |
1093 | print_space (); | |
1094 | print_size (section->_raw_size); | |
1095 | print_space(); | |
1096 | print_size(section->_cooked_size); | |
1097 | print_space (); | |
1098 | print_alignment (section->alignment_power); | |
1099 | print_space (); | |
2fa0b342 | 1100 | #if 0 |
e20873a7 JG |
1101 | fprintf (config.map_file, "%s flags", output_section_statement->region->name); |
1102 | print_flags (stdout, &output_section_statement->flags); | |
2fa0b342 | 1103 | #endif |
e20873a7 JG |
1104 | if (section->flags & SEC_LOAD) |
1105 | fprintf (config.map_file, "load "); | |
1106 | if (section->flags & SEC_ALLOC) | |
1107 | fprintf (config.map_file, "alloc "); | |
1108 | if (section->flags & SEC_RELOC) | |
1109 | fprintf (config.map_file, "reloc "); | |
1110 | if (section->flags & SEC_HAS_CONTENTS) | |
1111 | fprintf (config.map_file, "contents "); | |
2fa0b342 | 1112 | |
e20873a7 | 1113 | } |
075d7359 | 1114 | else |
e20873a7 JG |
1115 | { |
1116 | fprintf (config.map_file, "No attached output section"); | |
1117 | } | |
1118 | print_nl (); | |
9fce28ed SC |
1119 | if (output_section_statement->load_base) |
1120 | { | |
1121 | int b = exp_get_value_int(output_section_statement->load_base, | |
1122 | 0, "output base", lang_final_phase_enum); | |
1123 | printf("Output address %08x\n", b); | |
1124 | } | |
e20873a7 JG |
1125 | if (output_section_statement->section_alignment >= 0 |
1126 | || output_section_statement->section_alignment >= 0) | |
1127 | { | |
1128 | printf("\t\t\t\t\tforced alignment "); | |
1129 | if ( output_section_statement->section_alignment >= 0) | |
075d7359 | 1130 | { |
e20873a7 | 1131 | printf("section 2**%d ",output_section_statement->section_alignment ); |
075d7359 | 1132 | } |
e20873a7 JG |
1133 | if ( output_section_statement->subsection_alignment >= 0) |
1134 | { | |
1135 | printf("subsection 2**%d ",output_section_statement->subsection_alignment ); | |
1136 | } | |
1137 | ||
1138 | print_nl (); | |
1139 | } | |
075d7359 SC |
1140 | print_statement (output_section_statement->children.head, |
1141 | output_section_statement); | |
2fa0b342 DHW |
1142 | |
1143 | } | |
1144 | ||
075d7359 | 1145 | static void |
8ddef552 DM |
1146 | print_assignment (assignment, output_section) |
1147 | lang_assignment_statement_type * assignment; | |
1148 | lang_output_section_statement_type * output_section; | |
2fa0b342 DHW |
1149 | { |
1150 | etree_value_type result; | |
075d7359 SC |
1151 | |
1152 | print_section (""); | |
1153 | print_space (); | |
1154 | print_section (""); | |
1155 | print_space (); | |
1156 | print_address (print_dot); | |
1157 | print_space (); | |
1158 | result = exp_fold_tree (assignment->exp->assign.src, | |
1159 | output_section, | |
1160 | lang_final_phase_enum, | |
1161 | print_dot, | |
1162 | &print_dot); | |
1163 | ||
1164 | if (result.valid) | |
1165 | { | |
1166 | print_address (result.value); | |
1167 | } | |
1168 | else | |
1169 | { | |
1170 | fprintf (config.map_file, "*undefined*"); | |
1171 | } | |
1172 | print_space (); | |
1173 | exp_print_tree (assignment->exp); | |
1174 | ||
1175 | fprintf (config.map_file, "\n"); | |
2fa0b342 DHW |
1176 | } |
1177 | ||
1178 | static void | |
8ddef552 DM |
1179 | print_input_statement (statm) |
1180 | lang_input_statement_type * statm; | |
2fa0b342 | 1181 | { |
075d7359 SC |
1182 | if (statm->filename != (char *) NULL) |
1183 | { | |
1184 | fprintf (config.map_file, "LOAD %s\n", statm->filename); | |
1185 | } | |
2fa0b342 DHW |
1186 | } |
1187 | ||
075d7359 | 1188 | static void |
8ddef552 DM |
1189 | print_symbol (q) |
1190 | asymbol * q; | |
2fa0b342 | 1191 | { |
075d7359 SC |
1192 | print_section (""); |
1193 | fprintf (config.map_file, " "); | |
1194 | print_section (""); | |
1195 | fprintf (config.map_file, " "); | |
1196 | print_address (outside_symbol_address (q)); | |
1197 | fprintf (config.map_file, " %s", q->name ? q->name : " "); | |
a4aeaacf RS |
1198 | if (q->flags & BSF_WEAK) |
1199 | fprintf (config.map_file, " *weak*"); | |
075d7359 | 1200 | print_nl (); |
2fa0b342 | 1201 | } |
1418c83b | 1202 | |
075d7359 | 1203 | static void |
8ddef552 DM |
1204 | print_input_section (in) |
1205 | lang_input_section_type * in; | |
2fa0b342 DHW |
1206 | { |
1207 | asection *i = in->section; | |
aa34a7c3 | 1208 | int size = i->reloc_done ? |
075d7359 SC |
1209 | bfd_get_section_size_after_reloc (i) : |
1210 | bfd_get_section_size_before_reloc (i); | |
1418c83b | 1211 | |
075d7359 SC |
1212 | if (size != 0) |
1213 | { | |
1214 | print_section (""); | |
1215 | fprintf (config.map_file, " "); | |
1216 | print_section (i->name); | |
1217 | fprintf (config.map_file, " "); | |
1218 | if (i->output_section) | |
1219 | { | |
1220 | print_address (i->output_section->vma + i->output_offset); | |
1221 | fprintf (config.map_file, " "); | |
ae475b39 SC |
1222 | print_size (i->_raw_size); |
1223 | fprintf (config.map_file, " "); | |
1224 | print_size(i->_cooked_size); | |
075d7359 SC |
1225 | fprintf (config.map_file, " "); |
1226 | print_alignment (i->alignment_power); | |
1227 | fprintf (config.map_file, " "); | |
1228 | if (in->ifile) | |
1229 | { | |
2fa0b342 | 1230 | |
075d7359 | 1231 | bfd *abfd = in->ifile->the_bfd; |
2fa0b342 | 1232 | |
075d7359 SC |
1233 | if (in->ifile->just_syms_flag == true) |
1234 | { | |
1235 | fprintf (config.map_file, "symbols only "); | |
1236 | } | |
2fa0b342 | 1237 | |
075d7359 SC |
1238 | fprintf (config.map_file, " %s ", abfd->xvec->name); |
1239 | if (abfd->my_archive != (bfd *) NULL) | |
1240 | { | |
1241 | fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename, | |
1242 | abfd->filename); | |
1243 | } | |
1244 | else | |
1245 | { | |
1246 | fprintf (config.map_file, "%s", abfd->filename); | |
1247 | } | |
1248 | fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd)); | |
1249 | print_nl (); | |
2fa0b342 | 1250 | |
075d7359 SC |
1251 | /* Find all the symbols in this file defined in this section */ |
1252 | ||
ae475b39 SC |
1253 | if (in->ifile->symbol_count) |
1254 | { | |
1255 | asymbol **p; | |
075d7359 | 1256 | |
ae475b39 SC |
1257 | for (p = in->ifile->asymbols; *p; p++) |
1258 | { | |
1259 | asymbol *q = *p; | |
075d7359 | 1260 | |
a4aeaacf RS |
1261 | if (bfd_get_section (q) == i |
1262 | && (q->flags & (BSF_GLOBAL | BSF_WEAK)) != 0) | |
ae475b39 SC |
1263 | { |
1264 | print_symbol (q); | |
1265 | } | |
1266 | } | |
1267 | } | |
075d7359 SC |
1268 | } |
1269 | else | |
1270 | { | |
1271 | print_nl (); | |
1272 | } | |
1273 | ||
1274 | ||
1275 | print_dot = outside_section_address (i) + size; | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | fprintf (config.map_file, "No output section allocated\n"); | |
1280 | } | |
1281 | } | |
1282 | } | |
2fa0b342 DHW |
1283 | |
1284 | static void | |
8ddef552 DM |
1285 | print_fill_statement (fill) |
1286 | lang_fill_statement_type * fill; | |
075d7359 SC |
1287 | { |
1288 | fprintf (config.map_file, "FILL mask "); | |
1289 | print_fill (fill->fill); | |
1290 | } | |
1291 | ||
1292 | static void | |
8ddef552 DM |
1293 | print_data_statement (data) |
1294 | lang_data_statement_type * data; | |
2fa0b342 DHW |
1295 | { |
1296 | /* bfd_vma value; */ | |
075d7359 SC |
1297 | print_section (""); |
1298 | print_space (); | |
1299 | print_section (""); | |
1300 | print_space (); | |
65c552e3 | 1301 | /* ASSERT(print_dot == data->output_vma);*/ |
2fa0b342 | 1302 | |
075d7359 SC |
1303 | print_address (data->output_vma + data->output_section->vma); |
1304 | print_space (); | |
1305 | print_address (data->value); | |
1306 | print_space (); | |
1307 | switch (data->type) | |
1308 | { | |
1309 | case BYTE: | |
1310 | fprintf (config.map_file, "BYTE "); | |
1311 | print_dot += BYTE_SIZE; | |
1312 | break; | |
1313 | case SHORT: | |
1314 | fprintf (config.map_file, "SHORT "); | |
1315 | print_dot += SHORT_SIZE; | |
1316 | break; | |
1317 | case LONG: | |
1318 | fprintf (config.map_file, "LONG "); | |
1319 | print_dot += LONG_SIZE; | |
1320 | break; | |
1321 | } | |
1322 | ||
1323 | exp_print_tree (data->exp); | |
2fa0b342 | 1324 | |
075d7359 | 1325 | fprintf (config.map_file, "\n"); |
2fa0b342 DHW |
1326 | } |
1327 | ||
1328 | ||
1329 | static void | |
8ddef552 DM |
1330 | print_padding_statement (s) |
1331 | lang_padding_statement_type * s; | |
075d7359 SC |
1332 | { |
1333 | print_section (""); | |
1334 | print_space (); | |
1335 | print_section ("*fill*"); | |
1336 | print_space (); | |
1337 | print_address (s->output_offset + s->output_section->vma); | |
1338 | print_space (); | |
1339 | print_size (s->size); | |
1340 | print_space (); | |
1341 | print_fill (s->fill); | |
1342 | print_nl (); | |
ffc50032 | 1343 | |
aa34a7c3 | 1344 | print_dot = s->output_offset + s->output_section->vma + s->size; |
ffc50032 | 1345 | |
2fa0b342 DHW |
1346 | } |
1347 | ||
075d7359 | 1348 | static void |
8ddef552 DM |
1349 | print_wild_statement (w, os) |
1350 | lang_wild_statement_type * w; | |
1351 | lang_output_section_statement_type * os; | |
2fa0b342 | 1352 | { |
075d7359 SC |
1353 | fprintf (config.map_file, " from "); |
1354 | if (w->filename != (char *) NULL) | |
1355 | { | |
1356 | fprintf (config.map_file, "%s", w->filename); | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | fprintf (config.map_file, "*"); | |
1361 | } | |
1362 | if (w->section_name != (char *) NULL) | |
1363 | { | |
1364 | fprintf (config.map_file, "(%s)", w->section_name); | |
1365 | } | |
1366 | else | |
1367 | { | |
1368 | fprintf (config.map_file, "(*)"); | |
1369 | } | |
1370 | print_nl (); | |
1371 | print_statement (w->children.head, os); | |
2fa0b342 DHW |
1372 | |
1373 | } | |
1374 | static void | |
8ddef552 DM |
1375 | print_statement (s, os) |
1376 | lang_statement_union_type * s; | |
1377 | lang_output_section_statement_type * os; | |
2fa0b342 | 1378 | { |
075d7359 | 1379 | while (s) |
c611e285 | 1380 | { |
075d7359 | 1381 | switch (s->header.type) |
c611e285 | 1382 | { |
075d7359 SC |
1383 | case lang_constructors_statement_enum: |
1384 | fprintf (config.map_file, "constructors:\n"); | |
1385 | print_statement (constructor_list.head, os); | |
1386 | break; | |
1387 | case lang_wild_statement_enum: | |
1388 | print_wild_statement (&s->wild_statement, os); | |
1389 | break; | |
1390 | default: | |
1391 | fprintf (config.map_file, "Fail with %d\n", s->header.type); | |
1392 | FAIL (); | |
1393 | break; | |
1394 | case lang_address_statement_enum: | |
1395 | fprintf (config.map_file, "address\n"); | |
1396 | break; | |
1397 | case lang_object_symbols_statement_enum: | |
1398 | fprintf (config.map_file, "object symbols\n"); | |
1399 | break; | |
1400 | case lang_fill_statement_enum: | |
1401 | print_fill_statement (&s->fill_statement); | |
1402 | break; | |
1403 | case lang_data_statement_enum: | |
1404 | print_data_statement (&s->data_statement); | |
1405 | break; | |
1406 | case lang_input_section_enum: | |
1407 | print_input_section (&s->input_section); | |
1408 | break; | |
1409 | case lang_padding_statement_enum: | |
1410 | print_padding_statement (&s->padding_statement); | |
1411 | break; | |
1412 | case lang_output_section_statement_enum: | |
1413 | print_output_section_statement (&s->output_section_statement); | |
1414 | break; | |
1415 | case lang_assignment_statement_enum: | |
1416 | print_assignment (&s->assignment_statement, | |
1417 | os); | |
1418 | break; | |
1419 | case lang_target_statement_enum: | |
1420 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
1421 | break; | |
1422 | case lang_output_statement_enum: | |
1423 | fprintf (config.map_file, "OUTPUT(%s %s)\n", | |
c611e285 | 1424 | s->output_statement.name, |
e20873a7 | 1425 | output_target ? output_target : ""); |
075d7359 SC |
1426 | break; |
1427 | case lang_input_statement_enum: | |
1428 | print_input_statement (&s->input_statement); | |
1429 | break; | |
1430 | case lang_afile_asection_pair_statement_enum: | |
1431 | FAIL (); | |
1432 | break; | |
c611e285 | 1433 | } |
075d7359 | 1434 | s = s->next; |
2fa0b342 | 1435 | } |
2fa0b342 DHW |
1436 | } |
1437 | ||
1438 | ||
1439 | static void | |
8ddef552 | 1440 | print_statements () |
2fa0b342 | 1441 | { |
075d7359 SC |
1442 | print_statement (statement_list.head, |
1443 | abs_output_section); | |
1444 | ||
2fa0b342 DHW |
1445 | } |
1446 | ||
1447 | static bfd_vma | |
075d7359 | 1448 | DEFUN (insert_pad, (this_ptr, fill, power, output_section_statement, dot), |
8ddef552 DM |
1449 | lang_statement_union_type ** this_ptr AND |
1450 | fill_type fill AND | |
1451 | unsigned int power AND | |
1452 | asection * output_section_statement AND | |
1453 | bfd_vma dot) | |
075d7359 SC |
1454 | { |
1455 | /* Align this section first to the | |
2fa0b342 DHW |
1456 | input sections requirement, then |
1457 | to the output section's requirement. | |
075d7359 | 1458 | If this alignment is > than any seen before, |
2fa0b342 DHW |
1459 | then record it too. Perform the alignment by |
1460 | inserting a magic 'padding' statement. | |
1461 | */ | |
1462 | ||
075d7359 | 1463 | unsigned int alignment_needed = align_power (dot, power) - dot; |
2fa0b342 | 1464 | |
075d7359 | 1465 | if (alignment_needed != 0) |
2fa0b342 | 1466 | { |
075d7359 SC |
1467 | lang_statement_union_type *new = |
1468 | (lang_statement_union_type *) | |
1469 | stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type))); | |
1470 | ||
2fa0b342 DHW |
1471 | /* Link into existing chain */ |
1472 | new->header.next = *this_ptr; | |
1473 | *this_ptr = new; | |
1474 | new->header.type = lang_padding_statement_enum; | |
1475 | new->padding_statement.output_section = output_section_statement; | |
1476 | new->padding_statement.output_offset = | |
1477 | dot - output_section_statement->vma; | |
1478 | new->padding_statement.fill = fill; | |
1479 | new->padding_statement.size = alignment_needed; | |
1480 | } | |
1481 | ||
1482 | ||
1483 | /* Remember the most restrictive alignment */ | |
075d7359 SC |
1484 | if (power > output_section_statement->alignment_power) |
1485 | { | |
1486 | output_section_statement->alignment_power = power; | |
1487 | } | |
c611e285 | 1488 | output_section_statement->_raw_size += alignment_needed; |
2fa0b342 DHW |
1489 | return alignment_needed + dot; |
1490 | ||
1491 | } | |
1492 | ||
1418c83b | 1493 | /* Work out how much this section will move the dot point */ |
075d7359 | 1494 | static bfd_vma |
8ddef552 DM |
1495 | DEFUN (size_input_section, (this_ptr, output_section_statement, fill, dot, relax), |
1496 | lang_statement_union_type ** this_ptr AND | |
1497 | lang_output_section_statement_type * output_section_statement AND | |
1498 | unsigned short fill AND | |
1499 | bfd_vma dot AND | |
1500 | boolean relax) | |
2fa0b342 DHW |
1501 | { |
1502 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
1503 | asection *i = is->section; | |
2fa0b342 | 1504 | |
075d7359 SC |
1505 | if (is->ifile->just_syms_flag == false) |
1506 | { | |
e20873a7 JG |
1507 | if (output_section_statement->subsection_alignment != -1) |
1508 | i->alignment_power = | |
1509 | output_section_statement->subsection_alignment; | |
1510 | ||
075d7359 SC |
1511 | dot = insert_pad (this_ptr, fill, i->alignment_power, |
1512 | output_section_statement->bfd_section, dot); | |
1513 | ||
1514 | /* remember the largest size so we can malloc the largest area | |
1515 | needed for the output stage. Only remember the size of sections | |
1516 | which we will actually allocate */ | |
8ddef552 | 1517 | if ((i->flags & SEC_HAS_CONTENTS) != 0 |
075d7359 SC |
1518 | && (bfd_get_section_size_before_reloc (i) > largest_section)) |
1519 | { | |
1520 | largest_section = bfd_get_section_size_before_reloc (i); | |
1521 | } | |
2fa0b342 | 1522 | |
075d7359 | 1523 | /* Remember where in the output section this input section goes */ |
ac004870 | 1524 | |
075d7359 SC |
1525 | i->output_offset = dot - output_section_statement->bfd_section->vma; |
1526 | ||
ae475b39 SC |
1527 | /* Mark how big the output section must be to contain this now |
1528 | */ | |
1529 | if (relax) | |
1530 | { | |
1531 | dot += i->_cooked_size; | |
1532 | } | |
1533 | else | |
1534 | { | |
1535 | dot += i->_raw_size; | |
1536 | } | |
1537 | output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma; | |
075d7359 | 1538 | } |
ac004870 | 1539 | else |
075d7359 SC |
1540 | { |
1541 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
1542 | } | |
2fa0b342 | 1543 | |
075d7359 | 1544 | return dot; |
2fa0b342 DHW |
1545 | } |
1546 | ||
c611e285 SC |
1547 | /* Sizing happens in two passes, first pass we allocate worst case |
1548 | stuff. The second pass (if relaxing), we use what we learnt to | |
1549 | change the size of some relocs from worst case to better | |
1550 | */ | |
1551 | static boolean had_relax; | |
1552 | ||
1553 | static bfd_vma | |
8ddef552 DM |
1554 | DEFUN (lang_size_sections, (s, output_section_statement, prev, fill, dot, relax), |
1555 | lang_statement_union_type * s AND | |
1556 | lang_output_section_statement_type * output_section_statement AND | |
1557 | lang_statement_union_type ** prev AND | |
1558 | unsigned short fill AND | |
1559 | bfd_vma dot AND | |
1560 | boolean relax) | |
c611e285 SC |
1561 | { |
1562 | /* Size up the sections from their constituent parts */ | |
075d7359 | 1563 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
ae475b39 SC |
1564 | { |
1565 | switch (s->header.type) | |
075d7359 | 1566 | { |
c611e285 | 1567 | |
ae475b39 SC |
1568 | case lang_output_section_statement_enum: |
1569 | { | |
1570 | bfd_vma after; | |
1571 | lang_output_section_statement_type *os = &s->output_section_statement; | |
1572 | ||
e20873a7 JG |
1573 | /* If this is a shared library section, don't change the size |
1574 | and address. */ | |
1575 | if (os->bfd_section->flags & SEC_SHARED_LIBRARY) | |
e9b63852 ILT |
1576 | break; |
1577 | ||
ae475b39 SC |
1578 | if (os->bfd_section == &bfd_abs_section) |
1579 | { | |
1580 | /* No matter what happens, an abs section starts at zero */ | |
1581 | bfd_set_section_vma (0, os->bfd_section, 0); | |
1582 | } | |
1583 | else | |
1584 | { | |
1585 | if (os->addr_tree == (etree_type *) NULL) | |
1586 | { | |
1587 | /* No address specified for this section, get one | |
1588 | from the region specification | |
1589 | */ | |
1590 | if (os->region == (lang_memory_region_type *) NULL) | |
1591 | { | |
1592 | os->region = lang_memory_region_lookup ("*default*"); | |
1593 | } | |
1594 | dot = os->region->current; | |
1595 | } | |
1596 | else | |
1597 | { | |
1598 | etree_value_type r; | |
1599 | ||
1600 | r = exp_fold_tree (os->addr_tree, | |
1601 | abs_output_section, | |
1602 | lang_allocating_phase_enum, | |
1603 | dot, &dot); | |
1604 | if (r.valid == false) | |
1605 | { | |
1606 | einfo ("%F%S: non constant address expression for section %s\n", | |
1607 | os->name); | |
1608 | } | |
1609 | dot = r.value; | |
1610 | } | |
1611 | /* The section starts here */ | |
1612 | /* First, align to what the section needs */ | |
1613 | ||
1614 | ||
1615 | dot = align_power (dot, os->bfd_section->alignment_power); | |
1616 | bfd_set_section_vma (0, os->bfd_section, dot); | |
9fce28ed SC |
1617 | |
1618 | if (os->load_base) { | |
1619 | os->bfd_section->lma | |
1620 | = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum); | |
1621 | } | |
ae475b39 SC |
1622 | } |
1623 | ||
1624 | ||
1625 | os->bfd_section->output_offset = 0; | |
1626 | ||
1627 | (void) lang_size_sections (os->children.head, os, &os->children.head, | |
1628 | os->fill, dot, relax); | |
1629 | /* Ignore the size of the input sections, use the vma and size to */ | |
1630 | /* align against */ | |
1631 | ||
8ddef552 | 1632 | after = ALIGN_N (os->bfd_section->vma + |
97fbbaca JL |
1633 | os->bfd_section->_raw_size, |
1634 | /* The coercion here is important, see ld.h. */ | |
1635 | (bfd_vma) os->block_value); | |
ae475b39 SC |
1636 | |
1637 | os->bfd_section->_raw_size = after - os->bfd_section->vma; | |
1638 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; | |
1639 | os->processed = true; | |
1640 | ||
1641 | /* Replace into region ? */ | |
1642 | if (os->addr_tree == (etree_type *) NULL | |
1643 | && os->region != (lang_memory_region_type *) NULL) | |
1644 | { | |
1645 | os->region->current = dot; | |
1646 | /* Make sure this isn't silly */ | |
9fce28ed SC |
1647 | if (( os->region->current |
1648 | > os->region->origin + os->region->length) | |
1649 | || ( os->region->origin > os->region->current )) | |
1650 | { | |
ddddcdf0 | 1651 | einfo ("%X%P: region %s is full (%B section %s)\n", |
9fce28ed SC |
1652 | os->region->name, |
1653 | os->bfd_section->owner, | |
1654 | os->bfd_section->name); | |
1655 | /* Reset the region pointer */ | |
1656 | os->region->current = 0; | |
ae475b39 | 1657 | |
9fce28ed | 1658 | } |
ae475b39 SC |
1659 | |
1660 | } | |
1661 | } | |
9d1fe8a4 | 1662 | |
ae475b39 SC |
1663 | break; |
1664 | case lang_constructors_statement_enum: | |
1665 | dot = lang_size_sections (constructor_list.head, | |
1666 | output_section_statement, | |
1667 | &s->wild_statement.children.head, | |
1668 | fill, | |
1669 | dot, relax); | |
1670 | break; | |
9d1fe8a4 | 1671 | |
ae475b39 SC |
1672 | case lang_data_statement_enum: |
1673 | { | |
1674 | unsigned int size = 0; | |
1675 | ||
1676 | s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma; | |
1677 | s->data_statement.output_section = | |
1678 | output_section_statement->bfd_section; | |
1679 | ||
1680 | switch (s->data_statement.type) | |
1681 | { | |
1682 | case LONG: | |
1683 | size = LONG_SIZE; | |
1684 | break; | |
1685 | case SHORT: | |
1686 | size = SHORT_SIZE; | |
1687 | break; | |
1688 | case BYTE: | |
1689 | size = BYTE_SIZE; | |
1690 | break; | |
1691 | ||
1692 | } | |
1693 | dot += size; | |
1694 | output_section_statement->bfd_section->_raw_size += size; | |
1695 | } | |
1696 | break; | |
c611e285 | 1697 | |
ae475b39 | 1698 | case lang_wild_statement_enum: |
c611e285 | 1699 | |
ae475b39 SC |
1700 | dot = lang_size_sections (s->wild_statement.children.head, |
1701 | output_section_statement, | |
1702 | &s->wild_statement.children.head, | |
c611e285 | 1703 | |
ae475b39 | 1704 | fill, dot, relax); |
c611e285 | 1705 | |
ae475b39 | 1706 | break; |
c611e285 | 1707 | |
ae475b39 SC |
1708 | case lang_object_symbols_statement_enum: |
1709 | create_object_symbols = output_section_statement; | |
1710 | break; | |
1711 | case lang_output_statement_enum: | |
1712 | case lang_target_statement_enum: | |
1713 | break; | |
1714 | case lang_input_section_enum: | |
1715 | if (relax) | |
1716 | { | |
1717 | relaxing = true; | |
c611e285 | 1718 | |
ae475b39 SC |
1719 | if( relax_section (prev)) |
1720 | had_relax = true; | |
1721 | relaxing = false; | |
075d7359 | 1722 | |
ae475b39 SC |
1723 | } |
1724 | else { | |
1725 | (*prev)->input_section.section->_cooked_size = | |
1726 | (*prev)->input_section.section->_raw_size ; | |
075d7359 | 1727 | |
ae475b39 SC |
1728 | } |
1729 | dot = size_input_section (prev, | |
1730 | output_section_statement, | |
1731 | output_section_statement->fill, | |
1732 | dot, relax); | |
1733 | break; | |
1734 | case lang_input_statement_enum: | |
1735 | break; | |
1736 | case lang_fill_statement_enum: | |
1737 | s->fill_statement.output_section = output_section_statement->bfd_section; | |
075d7359 | 1738 | |
ae475b39 SC |
1739 | fill = s->fill_statement.fill; |
1740 | break; | |
1741 | case lang_assignment_statement_enum: | |
1742 | { | |
1743 | bfd_vma newdot = dot; | |
1744 | ||
1745 | exp_fold_tree (s->assignment_statement.exp, | |
1746 | output_section_statement, | |
1747 | lang_allocating_phase_enum, | |
1748 | dot, | |
1749 | &newdot); | |
1750 | ||
1751 | if (newdot != dot && !relax) | |
1752 | /* We've been moved ! so insert a pad */ | |
1753 | { | |
1754 | lang_statement_union_type *new = | |
1755 | (lang_statement_union_type *) | |
1756 | stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type))); | |
1757 | ||
1758 | /* Link into existing chain */ | |
1759 | new->header.next = *prev; | |
1760 | *prev = new; | |
1761 | new->header.type = lang_padding_statement_enum; | |
1762 | new->padding_statement.output_section = | |
1763 | output_section_statement->bfd_section; | |
1764 | new->padding_statement.output_offset = | |
1765 | dot - output_section_statement->bfd_section->vma; | |
1766 | new->padding_statement.fill = fill; | |
1767 | new->padding_statement.size = newdot - dot; | |
1768 | output_section_statement->bfd_section->_raw_size += | |
1769 | new->padding_statement.size; | |
1770 | dot = newdot; | |
1771 | } | |
1772 | } | |
075d7359 | 1773 | |
ae475b39 SC |
1774 | break; |
1775 | default: | |
1776 | FAIL (); | |
1777 | break; | |
1778 | /* This can only get here when relaxing is turned on */ | |
1779 | case lang_padding_statement_enum: | |
075d7359 | 1780 | |
ae475b39 SC |
1781 | case lang_address_statement_enum: |
1782 | break; | |
075d7359 | 1783 | } |
ae475b39 SC |
1784 | prev = &s->header.next; |
1785 | } | |
c611e285 SC |
1786 | return dot; |
1787 | } | |
9d1fe8a4 | 1788 | |
2fa0b342 | 1789 | static bfd_vma |
075d7359 | 1790 | DEFUN (lang_do_assignments, (s, output_section_statement, fill, dot), |
8ddef552 DM |
1791 | lang_statement_union_type * s AND |
1792 | lang_output_section_statement_type * output_section_statement AND | |
1793 | unsigned short fill AND | |
1794 | bfd_vma dot) | |
2fa0b342 DHW |
1795 | { |
1796 | ||
075d7359 | 1797 | for (; s != (lang_statement_union_type *) NULL; s = s->next) |
2fa0b342 | 1798 | { |
075d7359 | 1799 | switch (s->header.type) |
2fa0b342 | 1800 | { |
075d7359 SC |
1801 | case lang_constructors_statement_enum: |
1802 | dot = lang_do_assignments (constructor_list.head, | |
1803 | output_section_statement, | |
1804 | fill, | |
1805 | dot); | |
1806 | break; | |
1807 | ||
1808 | case lang_output_section_statement_enum: | |
1809 | { | |
1810 | lang_output_section_statement_type *os = | |
2fa0b342 | 1811 | &(s->output_section_statement); |
2fa0b342 | 1812 | |
075d7359 SC |
1813 | dot = os->bfd_section->vma; |
1814 | (void) lang_do_assignments (os->children.head, os, os->fill, dot); | |
1815 | dot = os->bfd_section->vma + os->bfd_section->_raw_size; | |
1816 | } | |
1817 | break; | |
1818 | case lang_wild_statement_enum: | |
2fa0b342 | 1819 | |
075d7359 SC |
1820 | dot = lang_do_assignments (s->wild_statement.children.head, |
1821 | output_section_statement, | |
1822 | fill, dot); | |
2fa0b342 | 1823 | |
075d7359 SC |
1824 | break; |
1825 | ||
1826 | case lang_object_symbols_statement_enum: | |
1827 | case lang_output_statement_enum: | |
1828 | case lang_target_statement_enum: | |
1418c83b | 1829 | #if 0 |
075d7359 | 1830 | case lang_common_statement_enum: |
1418c83b | 1831 | #endif |
2fa0b342 | 1832 | break; |
075d7359 SC |
1833 | case lang_data_statement_enum: |
1834 | { | |
1835 | etree_value_type value; | |
1836 | ||
1837 | value = exp_fold_tree (s->data_statement.exp, | |
1838 | abs_output_section, | |
1839 | lang_final_phase_enum, dot, &dot); | |
1840 | s->data_statement.value = value.value; | |
1841 | if (value.valid == false) | |
ddddcdf0 | 1842 | einfo ("%F%P: invalid data statement\n"); |
075d7359 SC |
1843 | } |
1844 | switch (s->data_statement.type) | |
1845 | { | |
1846 | case LONG: | |
1847 | dot += LONG_SIZE; | |
1848 | break; | |
1849 | case SHORT: | |
1850 | dot += SHORT_SIZE; | |
1851 | break; | |
1852 | case BYTE: | |
1853 | dot += BYTE_SIZE; | |
1854 | break; | |
1855 | } | |
2fa0b342 | 1856 | break; |
075d7359 SC |
1857 | case lang_input_section_enum: |
1858 | { | |
1859 | asection *in = s->input_section.section; | |
1860 | ||
1861 | dot += bfd_get_section_size_before_reloc (in); | |
1862 | } | |
2fa0b342 | 1863 | break; |
2fa0b342 | 1864 | |
075d7359 SC |
1865 | case lang_input_statement_enum: |
1866 | break; | |
1867 | case lang_fill_statement_enum: | |
1868 | fill = s->fill_statement.fill; | |
1869 | break; | |
1870 | case lang_assignment_statement_enum: | |
1871 | { | |
1872 | exp_fold_tree (s->assignment_statement.exp, | |
1873 | output_section_statement, | |
1874 | lang_final_phase_enum, | |
1875 | dot, | |
1876 | &dot); | |
1877 | } | |
1878 | ||
1879 | break; | |
1880 | case lang_padding_statement_enum: | |
1881 | dot += s->padding_statement.size; | |
1882 | break; | |
1883 | default: | |
1884 | FAIL (); | |
1885 | break; | |
1886 | case lang_address_statement_enum: | |
1887 | break; | |
1888 | } | |
2fa0b342 DHW |
1889 | |
1890 | } | |
1891 | return dot; | |
1892 | } | |
1893 | ||
1894 | ||
1895 | ||
075d7359 | 1896 | static void |
8ddef552 | 1897 | lang_relocate_globals () |
075d7359 | 1898 | { |
2fa0b342 | 1899 | /* |
1418c83b SC |
1900 | Each ldsym_type maintains a chain of pointers to asymbols which |
1901 | references the definition. Replace each pointer to the referenence | |
1902 | with a pointer to only one place, preferably the definition. If | |
1903 | the defintion isn't available then the common symbol, and if | |
1904 | there isn't one of them then choose one reference. | |
1905 | */ | |
2fa0b342 | 1906 | |
075d7359 SC |
1907 | FOR_EACH_LDSYM (lgs) |
1908 | { | |
2fa0b342 | 1909 | asymbol *it; |
55636aa7 ILT |
1910 | |
1911 | /* Skip indirect symbols. */ | |
1912 | if (lgs->flags & SYM_INDIRECT) | |
1913 | continue; | |
2fa0b342 | 1914 | |
075d7359 SC |
1915 | if (lgs->sdefs_chain) |
1916 | { | |
1917 | it = *(lgs->sdefs_chain); | |
1918 | } | |
1919 | else if (lgs->scoms_chain != (asymbol **) NULL) | |
1920 | { | |
1921 | it = *(lgs->scoms_chain); | |
1922 | } | |
1923 | else if (lgs->srefs_chain != (asymbol **) NULL) | |
1924 | { | |
1925 | it = *(lgs->srefs_chain); | |
1926 | } | |
1927 | else | |
1928 | { | |
1929 | /* This can happen when the command line asked for a symbol to | |
1930 | be -u */ | |
1931 | it = (asymbol *) NULL; | |
1932 | } | |
1933 | if (it != (asymbol *) NULL) | |
1934 | { | |
29f33467 | 1935 | asymbol **prev = 0; |
075d7359 SC |
1936 | asymbol **ptr = lgs->srefs_chain;; |
1937 | if (lgs->flags & SYM_WARNING) | |
1938 | { | |
1939 | produce_warnings (lgs, it); | |
1940 | } | |
1941 | ||
29f33467 SC |
1942 | while (ptr != (asymbol **) NULL |
1943 | && ptr != prev) | |
075d7359 | 1944 | { |
1418c83b | 1945 | asymbol *ref = *ptr; |
29f33467 | 1946 | prev = ptr; |
1418c83b | 1947 | *ptr = it; |
075d7359 | 1948 | ptr = (asymbol **) (ref->udata); |
1418c83b | 1949 | } |
075d7359 | 1950 | } |
2fa0b342 DHW |
1951 | } |
1952 | } | |
1953 | ||
1954 | ||
1955 | ||
2fa0b342 | 1956 | static void |
8ddef552 | 1957 | lang_finish () |
2fa0b342 DHW |
1958 | { |
1959 | ldsym_type *lgs; | |
81150d34 | 1960 | int warn = config.relocateable_output != true; |
075d7359 | 1961 | if (entry_symbol == (char *) NULL) |
22a78f0d SC |
1962 | { |
1963 | /* No entry has been specified, look for start, but don't warn */ | |
1964 | entry_symbol = "start"; | |
1965 | warn =0; | |
1966 | } | |
075d7359 SC |
1967 | lgs = ldsym_get_soft (entry_symbol); |
1968 | if (lgs && lgs->sdefs_chain) | |
22a78f0d SC |
1969 | { |
1970 | asymbol *sy = *(lgs->sdefs_chain); | |
075d7359 | 1971 | |
22a78f0d SC |
1972 | /* We can set the entry address*/ |
1973 | bfd_set_start_address (output_bfd, | |
1974 | outside_symbol_address (sy)); | |
2fa0b342 | 1975 | |
22a78f0d | 1976 | } |
075d7359 | 1977 | else |
22a78f0d | 1978 | { |
ddddcdf0 | 1979 | /* Cannot find anything reasonable, |
22a78f0d SC |
1980 | use the first address in the text section |
1981 | */ | |
1982 | asection *ts = bfd_get_section_by_name (output_bfd, ".text"); | |
1983 | if (ts) | |
075d7359 | 1984 | { |
22a78f0d | 1985 | if (warn) |
ddddcdf0 | 1986 | einfo ("%P: warning: cannot find entry symbol %s, defaulting to %V\n", |
22a78f0d | 1987 | entry_symbol, ts->vma); |
075d7359 | 1988 | |
22a78f0d | 1989 | bfd_set_start_address (output_bfd, ts->vma); |
075d7359 | 1990 | } |
22a78f0d SC |
1991 | else |
1992 | { | |
1993 | if (warn) | |
ddddcdf0 | 1994 | einfo ("%P: warning: cannot find entry symbol %s, not setting start address\n", |
22a78f0d SC |
1995 | entry_symbol); |
1996 | } | |
1997 | } | |
2fa0b342 DHW |
1998 | } |
1999 | ||
2000 | /* By now we know the target architecture, and we may have an */ | |
2001 | /* ldfile_output_machine_name */ | |
2002 | static void | |
8ddef552 | 2003 | lang_check () |
2fa0b342 DHW |
2004 | { |
2005 | lang_statement_union_type *file; | |
075d7359 SC |
2006 | bfd *input_bfd; |
2007 | unsigned long input_machine; | |
2008 | enum bfd_architecture input_architecture; | |
2009 | CONST bfd_arch_info_type *compatible; | |
7bc4a0d7 | 2010 | |
2fa0b342 | 2011 | for (file = file_chain.head; |
075d7359 SC |
2012 | file != (lang_statement_union_type *) NULL; |
2013 | file = file->input_statement.next) | |
2014 | { | |
075d7359 | 2015 | input_bfd = file->input_statement.the_bfd; |
7bc4a0d7 | 2016 | |
075d7359 SC |
2017 | input_machine = bfd_get_mach (input_bfd); |
2018 | input_architecture = bfd_get_arch (input_bfd); | |
7bc4a0d7 | 2019 | |
7bc4a0d7 | 2020 | |
075d7359 SC |
2021 | /* Inspect the architecture and ensure we're linking like with |
2022 | like */ | |
7bc4a0d7 | 2023 | |
075d7359 SC |
2024 | compatible = bfd_arch_get_compatible (input_bfd, |
2025 | output_bfd); | |
22a78f0d | 2026 | |
075d7359 SC |
2027 | if (compatible) |
2028 | { | |
2029 | ldfile_output_machine = compatible->mach; | |
2030 | ldfile_output_architecture = compatible->arch; | |
2031 | } | |
2032 | else | |
2033 | { | |
7bc4a0d7 | 2034 | |
a4aeaacf | 2035 | info_msg ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n", |
075d7359 SC |
2036 | bfd_printable_name (input_bfd), input_bfd, |
2037 | bfd_printable_name (output_bfd)); | |
7bc4a0d7 | 2038 | |
ddddcdf0 ILT |
2039 | if (! bfd_set_arch_mach (output_bfd, |
2040 | input_architecture, | |
2041 | input_machine)) | |
2042 | einfo ("%P%F:%s: can't set architecture: %E\n", | |
2043 | bfd_get_filename (output_bfd)); | |
075d7359 | 2044 | } |
2fa0b342 | 2045 | |
075d7359 SC |
2046 | } |
2047 | } | |
2fa0b342 DHW |
2048 | |
2049 | /* | |
075d7359 SC |
2050 | * run through all the global common symbols and tie them |
2051 | * to the output section requested. | |
b6316534 SC |
2052 | * |
2053 | As an experiment we do this 4 times, once for all the byte sizes, | |
2054 | then all the two bytes, all the four bytes and then everything else | |
2055 | */ | |
2fa0b342 DHW |
2056 | |
2057 | static void | |
8ddef552 | 2058 | lang_common () |
2fa0b342 DHW |
2059 | { |
2060 | ldsym_type *lgs; | |
b6316534 | 2061 | size_t power; |
075d7359 | 2062 | |
2fa0b342 | 2063 | if (config.relocateable_output == false || |
075d7359 SC |
2064 | command_line.force_common_definition == true) |
2065 | { | |
2066 | for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1) | |
1418c83b | 2067 | { |
075d7359 SC |
2068 | for (lgs = symbol_head; |
2069 | lgs != (ldsym_type *) NULL; | |
2070 | lgs = lgs->next) | |
2071 | { | |
2072 | asymbol *com; | |
2073 | unsigned int power_of_two; | |
2074 | size_t size; | |
2075 | size_t align; | |
1418c83b | 2076 | |
075d7359 | 2077 | if (lgs->scoms_chain != (asymbol **) NULL) |
1418c83b | 2078 | { |
075d7359 SC |
2079 | com = *(lgs->scoms_chain); |
2080 | size = com->value; | |
2081 | switch (size) | |
2082 | { | |
2083 | case 0: | |
2084 | case 1: | |
2085 | align = 1; | |
2086 | power_of_two = 0; | |
2087 | break; | |
2088 | case 2: | |
2089 | power_of_two = 1; | |
2090 | align = 2; | |
2091 | break; | |
2092 | case 3: | |
2093 | case 4: | |
2094 | power_of_two = 2; | |
2095 | align = 4; | |
2096 | break; | |
2097 | case 5: | |
2098 | case 6: | |
2099 | case 7: | |
2100 | case 8: | |
2101 | power_of_two = 3; | |
2102 | align = 8; | |
2103 | break; | |
2104 | default: | |
2105 | power_of_two = 4; | |
2106 | align = 16; | |
2107 | break; | |
2108 | } | |
2109 | if (config.sort_common == false || align == power) | |
2110 | { | |
29f33467 SC |
2111 | bfd *symbfd; |
2112 | ||
075d7359 SC |
2113 | /* Change from a common symbol into a definition of |
2114 | a symbol */ | |
2115 | lgs->sdefs_chain = lgs->scoms_chain; | |
2116 | lgs->scoms_chain = (asymbol **) NULL; | |
2117 | commons_pending--; | |
29f33467 | 2118 | |
075d7359 | 2119 | /* Point to the correct common section */ |
29f33467 SC |
2120 | symbfd = bfd_asymbol_bfd (com); |
2121 | if (com->section == &bfd_com_section) | |
2122 | com->section = | |
2123 | ((lang_input_statement_type *) symbfd->usrdata) | |
2124 | ->common_section; | |
2125 | else | |
2126 | { | |
2127 | CONST char *name; | |
2128 | asection *newsec; | |
2129 | ||
2130 | name = bfd_get_section_name (symbfd, | |
2131 | com->section); | |
2132 | newsec = bfd_get_section_by_name (symbfd, | |
2133 | name); | |
2134 | /* BFD backend must provide this section. */ | |
2135 | if (newsec == (asection *) NULL) | |
ddddcdf0 | 2136 | einfo ("%P%F: no output section %s", name); |
29f33467 SC |
2137 | com->section = newsec; |
2138 | } | |
2139 | ||
075d7359 SC |
2140 | /* Fix the size of the common section */ |
2141 | ||
2142 | com->section->_raw_size = | |
97fbbaca JL |
2143 | ALIGN_N (com->section->_raw_size, |
2144 | /* The coercion here is important, see ld.h. */ | |
2145 | (bfd_vma) align); | |
075d7359 SC |
2146 | |
2147 | /* Remember if this is the biggest alignment ever seen */ | |
2148 | if (power_of_two > com->section->alignment_power) | |
2149 | { | |
2150 | com->section->alignment_power = power_of_two; | |
2151 | } | |
2152 | ||
2153 | /* Symbol stops being common and starts being global, but | |
2154 | we remember that it was common once. */ | |
2155 | ||
2156 | com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON; | |
2157 | com->value = com->section->_raw_size; | |
2158 | ||
29f33467 | 2159 | if (write_map && config.map_file) |
075d7359 SC |
2160 | { |
2161 | fprintf (config.map_file, "Allocating common %s: %x at %x %s\n", | |
2162 | lgs->name, | |
2163 | (unsigned) size, | |
2164 | (unsigned) com->value, | |
29f33467 | 2165 | bfd_asymbol_bfd(com)->filename); |
075d7359 SC |
2166 | } |
2167 | ||
2168 | com->section->_raw_size += size; | |
2169 | ||
2170 | } | |
1418c83b | 2171 | } |
097879bc | 2172 | |
b6316534 | 2173 | } |
2fa0b342 | 2174 | } |
b6316534 | 2175 | } |
1418c83b SC |
2176 | |
2177 | ||
2fa0b342 DHW |
2178 | } |
2179 | ||
2180 | /* | |
075d7359 | 2181 | run through the input files and ensure that every input |
2fa0b342 DHW |
2182 | section has somewhere to go. If one is found without |
2183 | a destination then create an input request and place it | |
2184 | into the statement tree. | |
2185 | */ | |
2186 | ||
075d7359 | 2187 | static void |
8ddef552 | 2188 | lang_place_orphans () |
2fa0b342 DHW |
2189 | { |
2190 | lang_input_statement_type *file; | |
1418c83b | 2191 | |
075d7359 SC |
2192 | for (file = (lang_input_statement_type *) file_chain.head; |
2193 | file != (lang_input_statement_type *) NULL; | |
2194 | file = (lang_input_statement_type *) file->next) | |
2195 | { | |
2196 | asection *s; | |
2197 | ||
2198 | for (s = file->the_bfd->sections; | |
2199 | s != (asection *) NULL; | |
2200 | s = s->next) | |
2201 | { | |
2202 | if (s->output_section == (asection *) NULL) | |
2203 | { | |
2204 | /* This section of the file is not attatched, root | |
2205 | around for a sensible place for it to go */ | |
2206 | ||
2207 | if (file->common_section == s) | |
2208 | { | |
2209 | /* This is a lonely common section which must | |
2210 | have come from an archive. We attatch to the | |
2211 | section with the wildcard */ | |
2212 | if (config.relocateable_output != true | |
2213 | && command_line.force_common_definition == false) | |
2214 | { | |
2215 | if (default_common_section == | |
2216 | (lang_output_section_statement_type *) NULL) | |
2217 | { | |
a4aeaacf | 2218 | info_msg ("%P: no [COMMON] command, defaulting to .bss\n"); |
075d7359 SC |
2219 | |
2220 | default_common_section = | |
2221 | lang_output_section_statement_lookup (".bss"); | |
2222 | ||
2223 | } | |
2224 | wild_doit (&default_common_section->children, s, | |
2225 | default_common_section, file); | |
2226 | } | |
2227 | } | |
2228 | else | |
2229 | { | |
2230 | lang_output_section_statement_type *os = | |
2231 | lang_output_section_statement_lookup (s->name); | |
2fa0b342 | 2232 | |
075d7359 SC |
2233 | wild_doit (&os->children, s, os, file); |
2234 | } | |
2235 | } | |
2fa0b342 | 2236 | } |
2fa0b342 | 2237 | } |
2fa0b342 DHW |
2238 | } |
2239 | ||
2240 | ||
2fa0b342 | 2241 | void |
8ddef552 DM |
2242 | lang_set_flags (ptr, flags) |
2243 | int *ptr; | |
2244 | CONST char *flags; | |
2fa0b342 | 2245 | { |
075d7359 SC |
2246 | boolean state = false; |
2247 | ||
2248 | *ptr = 0; | |
2fa0b342 | 2249 | while (*flags) |
075d7359 SC |
2250 | { |
2251 | if (*flags == '!') | |
2252 | { | |
2253 | state = false; | |
2254 | flags++; | |
2255 | } | |
2256 | else | |
2257 | state = true; | |
2258 | switch (*flags) | |
2259 | { | |
2260 | case 'R': | |
2261 | /* ptr->flag_read = state; */ | |
2262 | break; | |
2263 | case 'W': | |
2264 | /* ptr->flag_write = state; */ | |
2265 | break; | |
2266 | case 'X': | |
2267 | /* ptr->flag_executable= state;*/ | |
2268 | break; | |
2269 | case 'L': | |
2270 | case 'I': | |
2271 | /* ptr->flag_loadable= state;*/ | |
2272 | break; | |
2273 | default: | |
ddddcdf0 | 2274 | einfo ("%P%F: invalid syntax in flags\n"); |
075d7359 SC |
2275 | break; |
2276 | } | |
dc4726c2 SC |
2277 | flags++; |
2278 | } | |
2fa0b342 DHW |
2279 | } |
2280 | ||
2281 | ||
2282 | ||
2283 | void | |
8ddef552 DM |
2284 | lang_for_each_file (func) |
2285 | void (*func) PARAMS ((lang_input_statement_type *)); | |
2fa0b342 DHW |
2286 | { |
2287 | lang_input_statement_type *f; | |
075d7359 SC |
2288 | |
2289 | for (f = (lang_input_statement_type *) file_chain.head; | |
2290 | f != (lang_input_statement_type *) NULL; | |
2291 | f = (lang_input_statement_type *) f->next) | |
2292 | { | |
2293 | func (f); | |
2294 | } | |
2fa0b342 DHW |
2295 | } |
2296 | ||
2297 | ||
2298 | void | |
8ddef552 DM |
2299 | lang_for_each_input_section (func) |
2300 | void (*func) PARAMS ((bfd * ab, asection * as)); | |
2fa0b342 DHW |
2301 | { |
2302 | lang_input_statement_type *f; | |
075d7359 SC |
2303 | |
2304 | for (f = (lang_input_statement_type *) file_chain.head; | |
2305 | f != (lang_input_statement_type *) NULL; | |
2306 | f = (lang_input_statement_type *) f->next) | |
2fa0b342 DHW |
2307 | { |
2308 | asection *s; | |
075d7359 | 2309 | |
2fa0b342 | 2310 | for (s = f->the_bfd->sections; |
075d7359 SC |
2311 | s != (asection *) NULL; |
2312 | s = s->next) | |
2313 | { | |
2314 | func (f->the_bfd, s); | |
2315 | } | |
2fa0b342 DHW |
2316 | } |
2317 | } | |
2318 | ||
2319 | ||
2320 | ||
075d7359 | 2321 | void |
8ddef552 DM |
2322 | ldlang_add_file (entry) |
2323 | lang_input_statement_type * entry; | |
2fa0b342 | 2324 | { |
1418c83b | 2325 | |
075d7359 SC |
2326 | lang_statement_append (&file_chain, |
2327 | (lang_statement_union_type *) entry, | |
2328 | &entry->next); | |
2fa0b342 DHW |
2329 | } |
2330 | ||
2fa0b342 | 2331 | void |
8ddef552 DM |
2332 | lang_add_output (name) |
2333 | CONST char *name; | |
2fa0b342 | 2334 | { |
075d7359 SC |
2335 | lang_output_statement_type *new = new_stat (lang_output_statement, |
2336 | stat_ptr); | |
2337 | ||
2fa0b342 DHW |
2338 | new->name = name; |
2339 | had_output_filename = true; | |
2340 | } | |
2341 | ||
2342 | ||
2343 | static lang_output_section_statement_type *current_section; | |
2344 | ||
e20873a7 JG |
2345 | static int topower(x) |
2346 | int x; | |
2347 | { | |
2348 | unsigned int i = 1; | |
2349 | int l; | |
2350 | if (x < 0) return -1; | |
2351 | for (l = 0; l < 32; l++) | |
2352 | { | |
2353 | if (i >= x) return l; | |
2354 | i<<=1; | |
2355 | } | |
29f33467 | 2356 | return 0; |
e20873a7 | 2357 | } |
2fa0b342 | 2358 | void |
8ddef552 DM |
2359 | lang_enter_output_section_statement (output_section_statement_name, |
2360 | address_exp, flags, block_value, | |
2361 | align, subalign, base) | |
2362 | char *output_section_statement_name; | |
2363 | etree_type * address_exp; | |
2364 | int flags; | |
2365 | bfd_vma block_value; | |
2366 | etree_type *align; | |
2367 | etree_type *subalign; | |
2368 | etree_type *base; | |
2fa0b342 DHW |
2369 | { |
2370 | lang_output_section_statement_type *os; | |
075d7359 SC |
2371 | |
2372 | current_section = | |
e20873a7 | 2373 | os = |
075d7359 SC |
2374 | lang_output_section_statement_lookup (output_section_statement_name); |
2375 | ||
2376 | ||
2fa0b342 | 2377 | |
2fa0b342 DHW |
2378 | /* Add this statement to tree */ |
2379 | /* add_statement(lang_output_section_statement_enum, | |
2380 | output_section_statement);*/ | |
2381 | /* Make next things chain into subchain of this */ | |
2382 | ||
2383 | if (os->addr_tree == | |
075d7359 | 2384 | (etree_type *) NULL) |
e20873a7 JG |
2385 | { |
2386 | os->addr_tree = | |
2387 | address_exp; | |
2388 | } | |
7bc4a0d7 | 2389 | os->flags = flags; |
ae475b39 SC |
2390 | if (flags & SEC_NEVER_LOAD) |
2391 | os->loadable = 0; | |
2392 | else | |
2393 | os->loadable = 1; | |
29f33467 | 2394 | os->block_value = block_value ? block_value : 1; |
075d7359 | 2395 | stat_ptr = &os->children; |
2fa0b342 | 2396 | |
e20873a7 JG |
2397 | os->subsection_alignment = topower( |
2398 | exp_get_value_int(subalign, -1, | |
2399 | "subsection alignment", | |
2400 | 0)); | |
2401 | os->section_alignment = topower( | |
2402 | exp_get_value_int(align, -1, | |
2403 | "section alignment", 0)); | |
9fce28ed SC |
2404 | |
2405 | os->load_base = base; | |
2fa0b342 DHW |
2406 | } |
2407 | ||
9fce28ed | 2408 | |
075d7359 | 2409 | void |
8ddef552 | 2410 | lang_final () |
2fa0b342 | 2411 | { |
075d7359 SC |
2412 | if (had_output_filename == false) |
2413 | { | |
2414 | extern CONST char *output_filename; | |
2fa0b342 | 2415 | |
075d7359 SC |
2416 | lang_add_output (output_filename); |
2417 | } | |
2418 | } | |
2fa0b342 | 2419 | |
c611e285 SC |
2420 | /* Reset the current counters in the regions */ |
2421 | static void | |
8ddef552 | 2422 | reset_memory_regions () |
c611e285 | 2423 | { |
075d7359 SC |
2424 | lang_memory_region_type *p = lang_memory_region_list; |
2425 | ||
c611e285 | 2426 | for (p = lang_memory_region_list; |
075d7359 SC |
2427 | p != (lang_memory_region_type *) NULL; |
2428 | p = p->next) | |
2429 | { | |
8ddef552 | 2430 | p->old_length = (bfd_size_type) (p->current - p->origin); |
075d7359 SC |
2431 | p->current = p->origin; |
2432 | } | |
c611e285 | 2433 | } |
2fa0b342 DHW |
2434 | |
2435 | ||
ae475b39 | 2436 | |
1418c83b | 2437 | asymbol * |
075d7359 | 2438 | DEFUN (create_symbol, (name, flags, section), |
8ddef552 DM |
2439 | CONST char *name AND |
2440 | flagword flags AND | |
2441 | asection * section) | |
2fa0b342 DHW |
2442 | { |
2443 | extern lang_input_statement_type *script_file; | |
075d7359 SC |
2444 | asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **))); |
2445 | ||
2fa0b342 | 2446 | /* Add this definition to script file */ |
075d7359 | 2447 | asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd); |
075d7359 | 2448 | def->name = buystring (name); |
2fa0b342 DHW |
2449 | def->udata = 0; |
2450 | def->flags = flags; | |
2451 | def->section = section; | |
2fa0b342 | 2452 | *def_ptr = def; |
ddddcdf0 | 2453 | enter_global_ref (def_ptr, name); |
2fa0b342 DHW |
2454 | return def; |
2455 | } | |
2456 | ||
2fa0b342 | 2457 | void |
8ddef552 | 2458 | lang_process () |
075d7359 | 2459 | { |
075d7359 | 2460 | lang_reasonable_defaults (); |
1418c83b SC |
2461 | current_target = default_target; |
2462 | ||
075d7359 | 2463 | lang_for_each_statement (ldlang_open_output); /* Open the output file */ |
1418c83b SC |
2464 | /* For each output section statement, create a section in the output |
2465 | file */ | |
075d7359 | 2466 | lang_create_output_section_statements (); |
1418c83b | 2467 | |
97fbbaca JL |
2468 | ldemul_create_output_section_statements (); |
2469 | ||
1418c83b | 2470 | /* Create a dummy bfd for the script */ |
075d7359 | 2471 | lang_init_script_file (); |
1418c83b SC |
2472 | |
2473 | /* Add to the hash table all undefineds on the command line */ | |
075d7359 | 2474 | lang_place_undefineds (); |
1418c83b SC |
2475 | |
2476 | /* Create a bfd for each input file */ | |
2477 | current_target = default_target; | |
075d7359 | 2478 | lang_for_each_statement (open_input_bfds); |
1418c83b | 2479 | |
1418c83b | 2480 | /* Run through the contours of the script and attatch input sections |
075d7359 | 2481 | to the correct output sections |
1418c83b | 2482 | */ |
075d7359 SC |
2483 | find_constructors (); |
2484 | map_input_to_output_sections (statement_list.head, (char *) NULL, | |
2485 | (lang_output_section_statement_type *) NULL); | |
1418c83b | 2486 | |
81016051 | 2487 | |
1418c83b | 2488 | /* Find any sections not attatched explicitly and handle them */ |
075d7359 | 2489 | lang_place_orphans (); |
1418c83b SC |
2490 | |
2491 | /* Size up the common data */ | |
075d7359 | 2492 | lang_common (); |
1418c83b | 2493 | |
075d7359 | 2494 | ldemul_before_allocation (); |
1418c83b | 2495 | |
c611e285 | 2496 | |
ae475b39 SC |
2497 | #if 0 |
2498 | had_relax = true; | |
2499 | while (had_relax) | |
2500 | { | |
2501 | ||
2502 | had_relax = false; | |
c611e285 | 2503 | |
ae475b39 SC |
2504 | lang_size_sections (statement_list.head, |
2505 | (lang_output_section_statement_type *) NULL, | |
2506 | &(statement_list.head), 0, (bfd_vma) 0, true); | |
2507 | /* FIXME. Until the code in relax is fixed so that it only reads in | |
2508 | stuff once, we cant iterate since there is no way for the linker to | |
2509 | know what has been patched and what hasn't */ | |
2510 | break; | |
2511 | ||
2512 | } | |
2513 | #endif | |
c611e285 | 2514 | |
c611e285 | 2515 | /* Now run around and relax if we can */ |
075d7359 | 2516 | if (command_line.relax) |
c611e285 | 2517 | { |
97fbbaca JL |
2518 | /* First time round is a trial run to get the 'worst case' |
2519 | addresses of the objects if there was no relaxing. */ | |
ae475b39 SC |
2520 | lang_size_sections (statement_list.head, |
2521 | (lang_output_section_statement_type *) NULL, | |
2522 | &(statement_list.head), 0, (bfd_vma) 0, false); | |
c611e285 | 2523 | |
97fbbaca JL |
2524 | /* Move the global symbols around so the second pass of relaxing |
2525 | can see them. */ | |
2526 | lang_relocate_globals (); | |
075d7359 | 2527 | |
97fbbaca | 2528 | reset_memory_regions (); |
075d7359 | 2529 | |
97fbbaca JL |
2530 | /* Do all the assignments, now that we know the final resting |
2531 | places of all the symbols. */ | |
ae475b39 | 2532 | |
97fbbaca JL |
2533 | lang_do_assignments (statement_list.head, |
2534 | abs_output_section, | |
2535 | 0, (bfd_vma) 0); | |
ae475b39 SC |
2536 | |
2537 | /* Perform another relax pass - this time we know where the | |
97fbbaca | 2538 | globals are, so can make better guess. */ |
ae475b39 SC |
2539 | lang_size_sections (statement_list.head, |
2540 | (lang_output_section_statement_type *) NULL, | |
2541 | &(statement_list.head), 0, (bfd_vma) 0, true); | |
ae475b39 | 2542 | } |
ae475b39 SC |
2543 | else |
2544 | { | |
97fbbaca | 2545 | /* Size up the sections. */ |
ae475b39 SC |
2546 | lang_size_sections (statement_list.head, |
2547 | abs_output_section, | |
2548 | &(statement_list.head), 0, (bfd_vma) 0, false); | |
075d7359 | 2549 | } |
c611e285 | 2550 | |
1418c83b | 2551 | /* See if anything special should be done now we know how big |
97fbbaca | 2552 | everything is. */ |
075d7359 | 2553 | ldemul_after_allocation (); |
1418c83b SC |
2554 | |
2555 | /* Do all the assignments, now that we know the final restingplaces | |
2556 | of all the symbols */ | |
2557 | ||
075d7359 SC |
2558 | lang_do_assignments (statement_list.head, |
2559 | abs_output_section, | |
2560 | 0, (bfd_vma) 0); | |
1418c83b | 2561 | |
c611e285 | 2562 | |
ffc50032 | 2563 | /* Move the global symbols around */ |
075d7359 | 2564 | lang_relocate_globals (); |
ffc50032 | 2565 | |
1418c83b SC |
2566 | /* Make sure that we're not mixing architectures */ |
2567 | ||
075d7359 | 2568 | lang_check (); |
1418c83b | 2569 | |
1418c83b | 2570 | /* Final stuffs */ |
97fbbaca JL |
2571 | |
2572 | ldemul_finish (); | |
2573 | /* Size up the sections. */ | |
2574 | lang_size_sections (statement_list.head, | |
2575 | abs_output_section, | |
2576 | &(statement_list.head), 0, (bfd_vma) 0, false); | |
075d7359 | 2577 | lang_finish (); |
2fa0b342 DHW |
2578 | } |
2579 | ||
2fa0b342 | 2580 | /* EXPORTED TO YACC */ |
1418c83b | 2581 | |
2fa0b342 | 2582 | void |
8ddef552 DM |
2583 | lang_add_wild (section_name, filename) |
2584 | CONST char *CONST section_name; | |
2585 | CONST char *CONST filename; | |
1418c83b | 2586 | { |
075d7359 SC |
2587 | lang_wild_statement_type *new = new_stat (lang_wild_statement, |
2588 | stat_ptr); | |
1418c83b | 2589 | |
075d7359 SC |
2590 | if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0) |
2591 | { | |
2592 | placed_commons = true; | |
2593 | } | |
2594 | if (filename != (char *) NULL) | |
2595 | { | |
2596 | lang_has_input_file = true; | |
2597 | } | |
1418c83b SC |
2598 | new->section_name = section_name; |
2599 | new->filename = filename; | |
075d7359 | 2600 | lang_list_init (&new->children); |
1418c83b | 2601 | } |
075d7359 | 2602 | |
1418c83b | 2603 | void |
8ddef552 DM |
2604 | lang_section_start (name, address) |
2605 | CONST char *name; | |
2606 | etree_type * address; | |
2fa0b342 | 2607 | { |
075d7359 SC |
2608 | lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr); |
2609 | ||
2fa0b342 DHW |
2610 | ad->section_name = name; |
2611 | ad->address = address; | |
2612 | } | |
1418c83b | 2613 | |
075d7359 | 2614 | void |
8ddef552 DM |
2615 | lang_add_entry (name) |
2616 | CONST char *name; | |
2fa0b342 DHW |
2617 | { |
2618 | entry_symbol = name; | |
2619 | } | |
2620 | ||
2621 | void | |
8ddef552 DM |
2622 | lang_add_target (name) |
2623 | CONST char *name; | |
2fa0b342 | 2624 | { |
075d7359 SC |
2625 | lang_target_statement_type *new = new_stat (lang_target_statement, |
2626 | stat_ptr); | |
2627 | ||
2fa0b342 DHW |
2628 | new->target = name; |
2629 | ||
2630 | } | |
2fa0b342 | 2631 | |
2fa0b342 | 2632 | void |
8ddef552 DM |
2633 | lang_add_map (name) |
2634 | CONST char *name; | |
2fa0b342 | 2635 | { |
075d7359 SC |
2636 | while (*name) |
2637 | { | |
2638 | switch (*name) | |
2639 | { | |
2640 | case 'F': | |
2641 | map_option_f = true; | |
2642 | break; | |
2643 | } | |
2644 | name++; | |
2fa0b342 | 2645 | } |
2fa0b342 DHW |
2646 | } |
2647 | ||
075d7359 | 2648 | void |
8ddef552 DM |
2649 | lang_add_fill (exp) |
2650 | int exp; | |
2fa0b342 | 2651 | { |
075d7359 SC |
2652 | lang_fill_statement_type *new = new_stat (lang_fill_statement, |
2653 | stat_ptr); | |
2654 | ||
2fa0b342 DHW |
2655 | new->fill = exp; |
2656 | } | |
2657 | ||
075d7359 | 2658 | void |
8ddef552 DM |
2659 | lang_add_data (type, exp) |
2660 | int type; | |
2661 | union etree_union *exp; | |
2fa0b342 DHW |
2662 | { |
2663 | ||
075d7359 | 2664 | lang_data_statement_type *new = new_stat (lang_data_statement, |
2fa0b342 | 2665 | stat_ptr); |
075d7359 SC |
2666 | |
2667 | new->exp = exp; | |
2668 | new->type = type; | |
2fa0b342 DHW |
2669 | |
2670 | } | |
075d7359 | 2671 | |
2fa0b342 | 2672 | void |
8ddef552 DM |
2673 | lang_add_assignment (exp) |
2674 | etree_type * exp; | |
2fa0b342 | 2675 | { |
075d7359 SC |
2676 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, |
2677 | stat_ptr); | |
2678 | ||
2fa0b342 DHW |
2679 | new->exp = exp; |
2680 | } | |
2681 | ||
2682 | void | |
8ddef552 DM |
2683 | lang_add_attribute (attribute) |
2684 | enum statement_enum attribute; | |
2fa0b342 | 2685 | { |
075d7359 | 2686 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); |
2fa0b342 DHW |
2687 | } |
2688 | ||
075d7359 | 2689 | void |
8ddef552 DM |
2690 | lang_startup (name) |
2691 | CONST char *name; | |
2fa0b342 | 2692 | { |
075d7359 SC |
2693 | if (startup_file != (char *) NULL) |
2694 | { | |
ddddcdf0 | 2695 | einfo ("%P%Fmultiple STARTUP files\n"); |
075d7359 | 2696 | } |
2fa0b342 DHW |
2697 | first_file->filename = name; |
2698 | first_file->local_sym_name = name; | |
2699 | ||
075d7359 | 2700 | startup_file = name; |
2fa0b342 | 2701 | } |
075d7359 SC |
2702 | |
2703 | void | |
8ddef552 DM |
2704 | lang_float (maybe) |
2705 | boolean maybe; | |
2fa0b342 DHW |
2706 | { |
2707 | lang_float_flag = maybe; | |
2708 | } | |
2709 | ||
075d7359 | 2710 | void |
8ddef552 DM |
2711 | lang_leave_output_section_statement (fill, memspec) |
2712 | bfd_vma fill; | |
2713 | CONST char *memspec; | |
2fa0b342 DHW |
2714 | { |
2715 | current_section->fill = fill; | |
075d7359 | 2716 | current_section->region = lang_memory_region_lookup (memspec); |
2fa0b342 | 2717 | stat_ptr = &statement_list; |
81016051 SC |
2718 | |
2719 | /* We remember if we are closing a .data section, since we use it to | |
2720 | store constructors in */ | |
075d7359 SC |
2721 | if (strcmp (current_section->name, ".data") == 0) |
2722 | { | |
2723 | end_of_data_section_statement_list = statement_list; | |
81016051 | 2724 | |
075d7359 | 2725 | } |
2fa0b342 | 2726 | } |
075d7359 | 2727 | |
9f32f7c2 SC |
2728 | /* |
2729 | Create an absolute symbol with the given name with the value of the | |
2730 | address of first byte of the section named. | |
2fa0b342 | 2731 | |
9f32f7c2 SC |
2732 | If the symbol already exists, then do nothing. |
2733 | */ | |
8cb5eb31 | 2734 | void |
8ddef552 DM |
2735 | lang_abs_symbol_at_beginning_of (section, name) |
2736 | CONST char *section; | |
2737 | CONST char *name; | |
075d7359 SC |
2738 | { |
2739 | if (ldsym_undefined (name)) | |
2740 | { | |
2741 | asection *s = bfd_get_section_by_name (output_bfd, section); | |
2742 | asymbol *def = create_symbol (name, | |
2743 | BSF_GLOBAL | BSF_EXPORT, | |
2744 | &bfd_abs_section); | |
2745 | ||
2746 | if (s != (asection *) NULL) | |
2747 | { | |
2748 | def->value = s->vma; | |
2749 | } | |
2750 | else | |
2751 | { | |
2752 | def->value = 0; | |
2753 | } | |
9f32f7c2 | 2754 | } |
8cb5eb31 SC |
2755 | } |
2756 | ||
9f32f7c2 SC |
2757 | /* |
2758 | Create an absolute symbol with the given name with the value of the | |
2759 | address of the first byte after the end of the section named. | |
2760 | ||
2761 | If the symbol already exists, then do nothing. | |
2762 | */ | |
2fa0b342 | 2763 | void |
8ddef552 DM |
2764 | lang_abs_symbol_at_end_of (section, name) |
2765 | CONST char *section; | |
2766 | CONST char *name; | |
075d7359 SC |
2767 | { |
2768 | if (ldsym_undefined (name)) | |
2769 | { | |
2770 | asection *s = bfd_get_section_by_name (output_bfd, section); | |
2771 | ||
2772 | /* Add a symbol called _end */ | |
2773 | asymbol *def = create_symbol (name, | |
2774 | BSF_GLOBAL | BSF_EXPORT, | |
2775 | &bfd_abs_section); | |
2776 | ||
2777 | if (s != (asection *) NULL) | |
2778 | { | |
2779 | def->value = s->vma + s->_raw_size; | |
2780 | } | |
2781 | else | |
2782 | { | |
2783 | def->value = 0; | |
2784 | } | |
9f32f7c2 | 2785 | } |
2fa0b342 DHW |
2786 | } |
2787 | ||
075d7359 | 2788 | void |
8ddef552 DM |
2789 | lang_statement_append (list, element, field) |
2790 | lang_statement_list_type * list; | |
2791 | lang_statement_union_type * element; | |
2792 | lang_statement_union_type ** field; | |
2fa0b342 DHW |
2793 | { |
2794 | *(list->tail) = element; | |
2795 | list->tail = field; | |
2796 | } | |
2797 | ||
173a0c3d | 2798 | /* Set the output format type. -oformat overrides scripts. */ |
097879bc | 2799 | void |
173a0c3d | 2800 | lang_add_output_format (format, from_script) |
8ddef552 | 2801 | CONST char *format; |
173a0c3d | 2802 | int from_script; |
097879bc | 2803 | { |
173a0c3d DM |
2804 | if (!from_script || output_target == NULL) |
2805 | output_target = format; | |
097879bc | 2806 | } |