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