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