Commit | Line | Data |
---|---|---|
2fa0b342 DHW |
1 | /* Copyright (C) 1991 Free Software Foundation, Inc. |
2 | ||
3 | This file is part of GLD, the Gnu Linker. | |
4 | ||
5 | GLD is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 1, or (at your option) | |
8 | any later version. | |
9 | ||
10 | GLD is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with GLD; see the file COPYING. If not, write to | |
17 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 | ||
19 | /* $Id$ | |
20 | * | |
2fa0b342 DHW |
21 | */ |
22 | ||
23 | ||
24 | ||
25 | #include "sysdep.h" | |
26 | #include "bfd.h" | |
27 | ||
28 | #include "ld.h" | |
29 | #include "ldmain.h" | |
30 | #include "ldsym.h" | |
3f8d46e7 JG |
31 | #include "ldgramtb.h" |
32 | ||
2fa0b342 DHW |
33 | #include "ldlang.h" |
34 | #include "ldexp.h" | |
8c514453 | 35 | #include "ldemul.h" |
2fa0b342 | 36 | #include "ldlex.h" |
3f8d46e7 | 37 | #include "ldmisc.h" |
2fa0b342 DHW |
38 | /* FORWARDS */ |
39 | PROTO(static void, print_statements,(void)); | |
40 | PROTO(static void, print_statement,(lang_statement_union_type *, | |
41 | lang_output_section_statement_type *)); | |
42 | ||
43 | ||
d4c02e29 SC |
44 | /* LOCALS */ |
45 | static CONST char *startup_file; | |
46 | static lang_statement_list_type input_file_chain; | |
47 | static boolean placed_commons = false; | |
48 | static lang_output_section_statement_type *default_common_section; | |
49 | static boolean map_option_f; | |
50 | static bfd_vma print_dot; | |
51 | static lang_input_statement_type *first_file; | |
52 | static lang_statement_list_type lang_output_section_statement; | |
53 | static CONST char *current_target; | |
54 | static CONST char *output_target; | |
55 | static size_t longest_section_name = 8; | |
56 | static asection common_section; | |
57 | static section_userdata_type common_section_userdata; | |
58 | static lang_statement_list_type statement_list; | |
2fa0b342 | 59 | /* EXPORTS */ |
2fa0b342 | 60 | |
d4c02e29 SC |
61 | lang_statement_list_type *stat_ptr = &statement_list; |
62 | lang_input_statement_type *script_file = 0; | |
63 | boolean option_longmap = false; | |
64 | lang_statement_list_type file_chain = {0}; | |
65 | CONST char *entry_symbol = 0; | |
3f8d46e7 | 66 | bfd_size_type largest_section = 0; |
d4c02e29 SC |
67 | boolean lang_has_input_file = false; |
68 | lang_output_section_statement_type *create_object_symbols = 0; | |
69 | boolean had_output_filename = false; | |
70 | boolean lang_float_flag = false; | |
71 | /* IMPORTS */ | |
72 | extern char *default_target; | |
2fa0b342 | 73 | |
d4c02e29 SC |
74 | extern unsigned int undefined_global_sym_count; |
75 | extern char *current_file; | |
2fa0b342 | 76 | extern bfd *output_bfd; |
2fa0b342 DHW |
77 | extern enum bfd_architecture ldfile_output_architecture; |
78 | extern unsigned long ldfile_output_machine; | |
79 | extern char *ldfile_output_machine_name; | |
2fa0b342 | 80 | extern ldsym_type *symbol_head; |
d4c02e29 | 81 | extern unsigned int commons_pending; |
2fa0b342 DHW |
82 | extern args_type command_line; |
83 | extern ld_config_type config; | |
2fa0b342 | 84 | extern boolean had_script; |
2fa0b342 DHW |
85 | extern boolean write_map; |
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 | ||
3f8d46e7 JG |
100 | static void |
101 | DEFUN(print_size,(value), | |
102 | size_t value) | |
103 | { | |
104 | printf("%5x", (unsigned)value); | |
105 | } | |
106 | static void | |
107 | DEFUN(print_alignment,(value), | |
108 | unsigned int value) | |
109 | { | |
110 | printf("2**%2u",value); | |
111 | } | |
112 | static void | |
113 | DEFUN(print_fill,(value), | |
114 | fill_type value) | |
115 | { | |
116 | printf("%04x",(unsigned)value); | |
117 | } | |
118 | ||
d4c02e29 | 119 | |
3f8d46e7 JG |
120 | static void |
121 | DEFUN(print_section,(name), | |
122 | CONST char *CONST name) | |
123 | { | |
124 | printf("%*s", -longest_section_name, name); | |
125 | } | |
2fa0b342 | 126 | |
1418c83b SC |
127 | /*---------------------------------------------------------------------- |
128 | lang_for_each_statement walks the parse tree and calls the provided | |
129 | function for each node | |
130 | */ | |
131 | ||
132 | static void | |
133 | DEFUN(lang_for_each_statement_worker,(func, s), | |
134 | void (*func)() AND | |
135 | lang_statement_union_type *s) | |
136 | { | |
137 | for (; s != (lang_statement_union_type *)NULL ; s = s->next) | |
138 | { | |
139 | func(s); | |
140 | ||
141 | switch (s->header.type) { | |
142 | case lang_output_section_statement_enum: | |
143 | lang_for_each_statement_worker | |
144 | (func, | |
145 | s->output_section_statement.children.head); | |
146 | break; | |
147 | case lang_wild_statement_enum: | |
148 | lang_for_each_statement_worker | |
149 | (func, | |
150 | s->wild_statement.children.head); | |
151 | break; | |
152 | case lang_data_statement_enum: | |
153 | case lang_object_symbols_statement_enum: | |
154 | case lang_output_statement_enum: | |
155 | case lang_target_statement_enum: | |
156 | case lang_input_section_enum: | |
157 | case lang_input_statement_enum: | |
158 | case lang_fill_statement_enum: | |
159 | case lang_assignment_statement_enum: | |
160 | case lang_padding_statement_enum: | |
161 | case lang_address_statement_enum: | |
162 | break; | |
163 | default: | |
164 | FAIL(); | |
165 | break; | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | void | |
171 | DEFUN(lang_for_each_statement,(func), | |
172 | void (*func)()) | |
173 | { | |
174 | lang_for_each_statement_worker(func, | |
175 | statement_list.head); | |
176 | } | |
177 | /*----------------------------------------------------------------------*/ | |
178 | static void | |
179 | DEFUN(lang_list_init,(list), | |
180 | lang_statement_list_type *list) | |
2fa0b342 DHW |
181 | { |
182 | list->head = (lang_statement_union_type *)NULL; | |
183 | list->tail = &list->head; | |
184 | } | |
185 | ||
2fa0b342 | 186 | |
1418c83b SC |
187 | /*---------------------------------------------------------------------- |
188 | ||
189 | build a new statement node for the parse tree | |
190 | ||
191 | */ | |
2fa0b342 DHW |
192 | |
193 | static | |
1418c83b SC |
194 | lang_statement_union_type* |
195 | DEFUN(new_statement,(type, size, list), | |
196 | enum statement_enum type AND | |
3f8d46e7 | 197 | bfd_size_type size AND |
1418c83b | 198 | lang_statement_list_type *list) |
2fa0b342 DHW |
199 | { |
200 | lang_statement_union_type *new = (lang_statement_union_type *) | |
201 | ldmalloc(size); | |
202 | new->header.type = type; | |
203 | new->header.next = (lang_statement_union_type *)NULL; | |
1418c83b | 204 | lang_statement_append(list, new, &new->header.next); |
2fa0b342 DHW |
205 | return new; |
206 | } | |
207 | ||
1418c83b SC |
208 | /* |
209 | Build a new input file node for the language. There are several ways | |
210 | in which we treat an input file, eg, we only look at symbols, or | |
211 | prefix it with a -l etc. | |
212 | ||
213 | We can be supplied with requests for input files more than once; | |
214 | they may, for example be split over serveral lines like foo.o(.text) | |
215 | foo.o(.data) etc, so when asked for a file we check that we havn't | |
216 | got it already so we don't duplicate the bfd. | |
217 | ||
218 | */ | |
2fa0b342 | 219 | static lang_input_statement_type * |
1418c83b SC |
220 | DEFUN(new_afile, (name, file_type, target), |
221 | CONST char *CONST name AND | |
222 | CONST lang_input_file_enum_type file_type AND | |
223 | CONST char *CONST target) | |
2fa0b342 DHW |
224 | { |
225 | lang_input_statement_type *p = new_stat(lang_input_statement, | |
226 | stat_ptr); | |
227 | lang_has_input_file = true; | |
228 | p->target = target; | |
229 | switch (file_type) { | |
230 | case lang_input_file_is_symbols_only_enum: | |
231 | p->filename = name; | |
232 | p->is_archive =false; | |
233 | p->real = true; | |
234 | p->local_sym_name= name; | |
235 | p->just_syms_flag = true; | |
236 | p->search_dirs_flag = false; | |
237 | break; | |
238 | case lang_input_file_is_fake_enum: | |
239 | p->filename = name; | |
240 | p->is_archive =false; | |
241 | p->real = false; | |
242 | p->local_sym_name= name; | |
243 | p->just_syms_flag = false; | |
244 | p->search_dirs_flag =false; | |
2fa0b342 DHW |
245 | break; |
246 | case lang_input_file_is_l_enum: | |
247 | p->is_archive = true; | |
248 | p->filename = name; | |
249 | p->real = true; | |
250 | p->local_sym_name = concat("-l",name,""); | |
251 | p->just_syms_flag = false; | |
252 | p->search_dirs_flag = true; | |
253 | break; | |
2fa0b342 DHW |
254 | case lang_input_file_is_search_file_enum: |
255 | case lang_input_file_is_marker_enum: | |
256 | p->filename = name; | |
257 | p->is_archive =false; | |
258 | p->real = true; | |
259 | p->local_sym_name= name; | |
260 | p->just_syms_flag = false; | |
261 | p->search_dirs_flag =true; | |
262 | break; | |
2fa0b342 DHW |
263 | case lang_input_file_is_file_enum: |
264 | p->filename = name; | |
265 | p->is_archive =false; | |
266 | p->real = true; | |
267 | p->local_sym_name= name; | |
268 | p->just_syms_flag = false; | |
269 | p->search_dirs_flag =false; | |
270 | break; | |
2fa0b342 DHW |
271 | default: |
272 | FAIL(); | |
273 | } | |
274 | p->asymbols = (asymbol **)NULL; | |
275 | p->superfile = (lang_input_statement_type *)NULL; | |
2fa0b342 DHW |
276 | p->next_real_file = (lang_statement_union_type*)NULL; |
277 | p->next = (lang_statement_union_type*)NULL; | |
278 | p->symbol_count = 0; | |
279 | p->common_output_section = (asection *)NULL; | |
2fa0b342 DHW |
280 | lang_statement_append(&input_file_chain, |
281 | (lang_statement_union_type *)p, | |
282 | &p->next_real_file); | |
283 | return p; | |
284 | } | |
285 | ||
1418c83b SC |
286 | |
287 | ||
2fa0b342 | 288 | lang_input_statement_type * |
1418c83b SC |
289 | DEFUN(lang_add_input_file,(name, file_type, target), |
290 | char *name AND | |
291 | lang_input_file_enum_type file_type AND | |
292 | char *target) | |
2fa0b342 DHW |
293 | { |
294 | /* Look it up or build a new one */ | |
1418c83b SC |
295 | lang_has_input_file = true; |
296 | #if 0 | |
2fa0b342 DHW |
297 | lang_input_statement_type *p; |
298 | ||
299 | for (p = (lang_input_statement_type *)input_file_chain.head; | |
300 | p != (lang_input_statement_type *)NULL; | |
301 | p = (lang_input_statement_type *)(p->next_real_file)) | |
1418c83b SC |
302 | { |
303 | /* Sometimes we have incomplete entries in here */ | |
304 | if (p->filename != (char *)NULL) { | |
305 | if(strcmp(name,p->filename) == 0) return p; | |
306 | } | |
307 | ||
308 | } | |
309 | #endif | |
2fa0b342 DHW |
310 | return new_afile(name, file_type, target); |
311 | } | |
312 | ||
313 | ||
1418c83b | 314 | /* Build enough state so that the parser can build its tree */ |
2fa0b342 | 315 | void |
1418c83b | 316 | DEFUN_VOID(lang_init) |
2fa0b342 DHW |
317 | { |
318 | ||
319 | stat_ptr= &statement_list; | |
320 | lang_list_init(stat_ptr); | |
321 | ||
322 | lang_list_init(&input_file_chain); | |
323 | lang_list_init(&lang_output_section_statement); | |
324 | lang_list_init(&file_chain); | |
325 | first_file = lang_add_input_file((char *)NULL, | |
326 | lang_input_file_is_marker_enum, | |
327 | (char *)NULL); | |
2fa0b342 DHW |
328 | } |
329 | ||
2fa0b342 | 330 | |
1418c83b SC |
331 | /*---------------------------------------------------------------------- |
332 | A region is an area of memory declared with the | |
333 | MEMORY { name:org=exp, len=exp ... } | |
334 | syntax. | |
2fa0b342 | 335 | |
1418c83b | 336 | We maintain a list of all the regions here |
2fa0b342 | 337 | |
1418c83b SC |
338 | If no regions are specified in the script, then the default is used |
339 | which is created when looked up to be the entire data space | |
2fa0b342 DHW |
340 | */ |
341 | ||
342 | static lang_memory_region_type *lang_memory_region_list; | |
343 | static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; | |
344 | ||
345 | lang_memory_region_type * | |
1418c83b SC |
346 | DEFUN(lang_memory_region_lookup,(name), |
347 | CONST char *CONST name) | |
2fa0b342 DHW |
348 | { |
349 | ||
1418c83b SC |
350 | lang_memory_region_type *p = lang_memory_region_list; |
351 | for (p = lang_memory_region_list; | |
352 | p != ( lang_memory_region_type *)NULL; | |
353 | p = p->next) { | |
354 | if (strcmp(p->name, name) == 0) { | |
355 | return p; | |
2fa0b342 | 356 | } |
1418c83b SC |
357 | } |
358 | if (strcmp(name,"*default*")==0) { | |
359 | /* This is the default region, dig out first one on the list */ | |
360 | if (lang_memory_region_list != (lang_memory_region_type*)NULL){ | |
361 | return lang_memory_region_list; | |
2fa0b342 | 362 | } |
1418c83b | 363 | } |
2fa0b342 | 364 | { |
1418c83b | 365 | lang_memory_region_type *new = |
3f8d46e7 | 366 | (lang_memory_region_type *)ldmalloc((bfd_size_type)(sizeof(lang_memory_region_type))); |
1418c83b SC |
367 | new->name = buystring(name); |
368 | new->next = (lang_memory_region_type *)NULL; | |
369 | ||
370 | *lang_memory_region_list_tail = new; | |
371 | lang_memory_region_list_tail = &new->next; | |
372 | new->origin = 0; | |
373 | new->length = ~0; | |
374 | new->current = 0; | |
375 | return new; | |
2fa0b342 DHW |
376 | } |
377 | } | |
378 | ||
379 | ||
2fa0b342 | 380 | lang_output_section_statement_type * |
1418c83b SC |
381 | DEFUN(lang_output_section_find,(name), |
382 | CONST char * CONST name) | |
2fa0b342 DHW |
383 | { |
384 | lang_statement_union_type *u; | |
385 | lang_output_section_statement_type *lookup; | |
386 | ||
387 | for (u = lang_output_section_statement.head; | |
388 | u != (lang_statement_union_type *)NULL; | |
389 | u = lookup->next) | |
1418c83b SC |
390 | { |
391 | lookup = &u->output_section_statement; | |
392 | if (strcmp(name, lookup->name)==0) { | |
393 | return lookup; | |
394 | } | |
2fa0b342 | 395 | } |
2fa0b342 DHW |
396 | return (lang_output_section_statement_type *)NULL; |
397 | } | |
398 | ||
399 | lang_output_section_statement_type * | |
1418c83b SC |
400 | DEFUN(lang_output_section_statement_lookup,(name), |
401 | CONST char * CONST name) | |
2fa0b342 DHW |
402 | { |
403 | lang_output_section_statement_type *lookup; | |
404 | lookup =lang_output_section_find(name); | |
405 | if (lookup == (lang_output_section_statement_type *)NULL) { | |
406 | ||
407 | lookup =(lang_output_section_statement_type *) | |
408 | new_stat(lang_output_section_statement, stat_ptr); | |
409 | lookup->region = (lang_memory_region_type *)NULL; | |
410 | lookup->fill = 0; | |
411 | lookup->block_value = 1; | |
412 | lookup->name = name; | |
413 | ||
414 | lookup->next = (lang_statement_union_type*)NULL; | |
415 | lookup->bfd_section = (asection *)NULL; | |
416 | lookup->processed = false; | |
417 | lookup->addr_tree = (etree_type *)NULL; | |
418 | lang_list_init(&lookup->children); | |
419 | ||
420 | lang_statement_append(&lang_output_section_statement, | |
421 | (lang_statement_union_type *)lookup, | |
422 | &lookup->next); | |
423 | } | |
424 | return lookup; | |
425 | } | |
426 | ||
427 | ||
428 | ||
429 | ||
430 | ||
431 | static void | |
1418c83b SC |
432 | DEFUN(print_flags, (outfile, ignore_flags), |
433 | FILE *outfile AND | |
434 | lang_section_flags_type *ignore_flags) | |
2fa0b342 DHW |
435 | { |
436 | fprintf(outfile,"("); | |
437 | #if 0 | |
438 | if (flags->flag_read) fprintf(outfile,"R"); | |
439 | if (flags->flag_write) fprintf(outfile,"W"); | |
440 | if (flags->flag_executable) fprintf(outfile,"X"); | |
441 | if (flags->flag_loadable) fprintf(outfile,"L"); | |
442 | #endif | |
443 | fprintf(outfile,")"); | |
444 | } | |
445 | ||
446 | void | |
1418c83b SC |
447 | DEFUN(lang_map,(outfile), |
448 | FILE *outfile) | |
2fa0b342 DHW |
449 | { |
450 | lang_memory_region_type *m; | |
451 | fprintf(outfile,"**MEMORY CONFIGURATION**\n\n"); | |
452 | ||
453 | fprintf(outfile,"name\t\torigin\t\tlength\t\tattributes\n"); | |
454 | for (m = lang_memory_region_list; | |
455 | m != (lang_memory_region_type *)NULL; | |
456 | m = m->next) | |
457 | { | |
458 | fprintf(outfile,"%-16s", m->name); | |
3f8d46e7 JG |
459 | print_address(m->origin); |
460 | print_space(); | |
461 | print_address(m->length); | |
462 | print_space(); | |
2fa0b342 DHW |
463 | print_flags(outfile, &m->flags); |
464 | fprintf(outfile,"\n"); | |
465 | } | |
466 | fprintf(outfile,"\n\n**LINK EDITOR MEMORY MAP**\n\n"); | |
467 | fprintf(outfile,"output\t\tinput\t\tvirtual\n"); | |
468 | fprintf(outfile,"section\t\tsection\t\taddress\tsize\n\n"); | |
469 | ||
470 | print_statements(); | |
471 | ||
472 | } | |
473 | ||
474 | /* | |
475 | * | |
476 | */ | |
1418c83b SC |
477 | static void |
478 | DEFUN(init_os,(s), | |
479 | lang_output_section_statement_type *s) | |
2fa0b342 DHW |
480 | { |
481 | section_userdata_type *new = | |
482 | (section_userdata_type *) | |
3f8d46e7 | 483 | ldmalloc((bfd_size_type)(sizeof(section_userdata_type))); |
2fa0b342 DHW |
484 | |
485 | s->bfd_section = bfd_make_section(output_bfd, s->name); | |
486 | s->bfd_section->output_section = s->bfd_section; | |
487 | s->bfd_section->flags = SEC_NO_FLAGS; | |
488 | /* We initialize an output sections output offset to minus its own */ | |
489 | /* vma to allow us to output a section through itself */ | |
490 | s->bfd_section->output_offset = 0; | |
a0ad3035 | 491 | get_userdata( s->bfd_section) = (PTR)new; |
2fa0b342 DHW |
492 | } |
493 | ||
1418c83b SC |
494 | /*********************************************************************** |
495 | The wild routines. | |
496 | ||
497 | These expand statements like *(.text) and foo.o to a list of | |
498 | explicit actions, like foo.o(.text), bar.o(.text) and | |
499 | foo.o(.text,.data) . | |
500 | ||
501 | The toplevel routine, wild, takes a statement, section, file and | |
502 | target. If either the section or file is null it is taken to be the | |
503 | wildcard. Seperate lang_input_section statements are created for | |
504 | each part of the expanstion, and placed after the statement provided. | |
505 | ||
506 | */ | |
507 | ||
2fa0b342 | 508 | static void |
1418c83b SC |
509 | DEFUN(wild_doit,(ptr, section, output, file), |
510 | lang_statement_list_type *ptr AND | |
511 | asection *section AND | |
512 | lang_output_section_statement_type *output AND | |
513 | lang_input_statement_type *file) | |
2fa0b342 DHW |
514 | { |
515 | if(output->bfd_section == (asection *)NULL) | |
516 | { | |
517 | init_os(output); | |
518 | } | |
519 | ||
520 | if (section != (asection *)NULL | |
521 | && section->output_section == (asection *)NULL) { | |
522 | /* Add a section reference to the list */ | |
523 | lang_input_section_type *new = new_stat(lang_input_section, ptr); | |
524 | ||
525 | new->section = section; | |
526 | new->ifile = file; | |
527 | section->output_section = output->bfd_section; | |
528 | section->output_section->flags |= section->flags; | |
529 | if (section->alignment_power > output->bfd_section->alignment_power) { | |
530 | output->bfd_section->alignment_power = section->alignment_power; | |
531 | } | |
2fa0b342 DHW |
532 | } |
533 | } | |
534 | ||
535 | static asection * | |
1418c83b SC |
536 | DEFUN(our_bfd_get_section_by_name,(abfd, section), |
537 | bfd *abfd AND | |
538 | CONST char *section) | |
2fa0b342 DHW |
539 | { |
540 | return bfd_get_section_by_name(abfd, section); | |
2fa0b342 | 541 | } |
1418c83b | 542 | |
2fa0b342 | 543 | static void |
1418c83b SC |
544 | DEFUN(wild_section,(ptr, section, file , output), |
545 | lang_wild_statement_type *ptr AND | |
546 | CONST char *section AND | |
547 | lang_input_statement_type *file AND | |
548 | lang_output_section_statement_type *output) | |
2fa0b342 DHW |
549 | { |
550 | asection *s; | |
1418c83b SC |
551 | if (file->just_syms_flag == false) { |
552 | if (section == (char *)NULL) { | |
553 | /* Do the creation to all sections in the file */ | |
554 | for (s = file->the_bfd->sections; s != (asection *)NULL; s=s->next) { | |
555 | wild_doit(&ptr->children, s, output, file); | |
556 | } | |
557 | } | |
558 | else { | |
559 | /* Do the creation to the named section only */ | |
560 | wild_doit(&ptr->children, | |
561 | our_bfd_get_section_by_name(file->the_bfd, section), | |
562 | output, file); | |
2fa0b342 DHW |
563 | } |
564 | } | |
2fa0b342 DHW |
565 | } |
566 | ||
567 | ||
1418c83b SC |
568 | /* passed a file name (which must have been seen already and added to |
569 | the statement tree. We will see if it has been opened already and | |
570 | had its symbols read. If not then we'll read it. | |
2fa0b342 | 571 | |
1418c83b SC |
572 | Archives are pecuilar here. We may open them once, but if they do |
573 | not define anything we need at the time, they won't have all their | |
574 | symbols read. If we need them later, we'll have to redo it. | |
575 | */ | |
2fa0b342 | 576 | static |
1418c83b SC |
577 | lang_input_statement_type * |
578 | DEFUN(lookup_name,(name), | |
579 | CONST char * CONST name) | |
2fa0b342 DHW |
580 | { |
581 | lang_input_statement_type *search; | |
582 | for(search = (lang_input_statement_type *)input_file_chain.head; | |
583 | search != (lang_input_statement_type *)NULL; | |
584 | search = (lang_input_statement_type *)search->next_real_file) | |
1418c83b SC |
585 | { |
586 | if (search->filename == (char *)NULL && name == (char *)NULL) { | |
2fa0b342 DHW |
587 | return search; |
588 | } | |
1418c83b SC |
589 | if (search->filename != (char *)NULL && name != (char *)NULL) { |
590 | if (strcmp(search->filename, name) == 0) { | |
591 | ldmain_open_file_read_symbol(search); | |
592 | return search; | |
593 | } | |
594 | } | |
2fa0b342 | 595 | } |
2fa0b342 | 596 | |
1418c83b SC |
597 | /* There isn't an afile entry for this file yet, this must be |
598 | because the name has only appeared inside a load script and not | |
599 | on the command line */ | |
600 | search = new_afile(name, lang_input_file_is_file_enum, default_target); | |
601 | ldmain_open_file_read_symbol(search); | |
2fa0b342 | 602 | return search; |
1418c83b SC |
603 | |
604 | ||
2fa0b342 DHW |
605 | } |
606 | ||
607 | static void | |
1418c83b SC |
608 | DEFUN(wild,(s, section, file, target, output), |
609 | lang_wild_statement_type *s AND | |
610 | CONST char *CONST section AND | |
611 | CONST char *CONST file AND | |
612 | CONST char *CONST target AND | |
613 | lang_output_section_statement_type *output) | |
2fa0b342 DHW |
614 | { |
615 | lang_input_statement_type *f; | |
616 | if (file == (char *)NULL) { | |
617 | /* Perform the iteration over all files in the list */ | |
618 | for (f = (lang_input_statement_type *)file_chain.head; | |
619 | f != (lang_input_statement_type *)NULL; | |
620 | f = (lang_input_statement_type *)f->next) { | |
621 | wild_section(s, section, f, output); | |
622 | } | |
623 | } | |
624 | else { | |
625 | /* Perform the iteration over a single file */ | |
1418c83b | 626 | wild_section( s, section, lookup_name(file), output); |
2fa0b342 | 627 | } |
b0f36869 SC |
628 | if (section != (char *)NULL |
629 | && strcmp(section,"COMMON") == 0 | |
1418c83b SC |
630 | && default_common_section == (lang_output_section_statement_type*)NULL) |
631 | { | |
632 | /* Remember the section that common is going to incase we later | |
633 | get something which doesn't know where to put it */ | |
634 | default_common_section = output; | |
635 | } | |
2fa0b342 DHW |
636 | } |
637 | ||
638 | /* | |
639 | read in all the files | |
640 | */ | |
641 | static bfd * | |
097879bc SC |
642 | DEFUN(open_output,(name), |
643 | CONST char *CONST name) | |
2fa0b342 | 644 | { |
1418c83b | 645 | extern CONST char *output_filename; |
097879bc SC |
646 | bfd *output; |
647 | if (output_target == (char *)NULL) { | |
648 | if (current_target != (char *)NULL) | |
649 | output_target = current_target; | |
650 | else | |
651 | output_target = default_target; | |
652 | } | |
653 | output = bfd_openw(name, output_target); | |
2fa0b342 | 654 | output_filename = name; |
097879bc | 655 | |
2fa0b342 | 656 | if (output == (bfd *)NULL) |
1418c83b SC |
657 | { |
658 | if (bfd_error == invalid_target) { | |
097879bc | 659 | info("%P%F target %s not found\n", output_target); |
1418c83b SC |
660 | } |
661 | info("%P%F problem opening output file %s, %E", name); | |
2fa0b342 | 662 | } |
2fa0b342 DHW |
663 | |
664 | output->flags |= D_PAGED; | |
665 | bfd_set_format(output, bfd_object); | |
666 | return output; | |
667 | } | |
1418c83b SC |
668 | |
669 | ||
097879bc | 670 | |
1418c83b | 671 | |
2fa0b342 | 672 | static void |
1418c83b SC |
673 | DEFUN(ldlang_open_output,(statement), |
674 | lang_statement_union_type *statement) | |
2fa0b342 | 675 | { |
1418c83b SC |
676 | switch (statement->header.type) |
677 | { | |
678 | case lang_output_statement_enum: | |
097879bc | 679 | output_bfd = open_output(statement->output_statement.name); |
2fa0b342 | 680 | ldemul_set_output_arch(); |
2fa0b342 | 681 | break; |
1418c83b | 682 | |
2fa0b342 | 683 | case lang_target_statement_enum: |
1418c83b | 684 | current_target = statement->target_statement.target; |
2fa0b342 | 685 | break; |
1418c83b | 686 | default: |
2fa0b342 | 687 | break; |
1418c83b SC |
688 | } |
689 | } | |
2fa0b342 | 690 | |
1418c83b SC |
691 | static void |
692 | DEFUN(open_input_bfds,(statement), | |
693 | lang_statement_union_type *statement) | |
694 | { | |
695 | switch (statement->header.type) | |
696 | { | |
697 | case lang_target_statement_enum: | |
698 | current_target = statement->target_statement.target; | |
2fa0b342 | 699 | break; |
1418c83b SC |
700 | case lang_wild_statement_enum: |
701 | /* Maybe we should load the file's symbols */ | |
702 | if (statement->wild_statement.filename) | |
703 | { | |
704 | (void) lookup_name(statement->wild_statement.filename); | |
705 | } | |
2fa0b342 | 706 | break; |
1418c83b SC |
707 | case lang_input_statement_enum: |
708 | if (statement->input_statement.real == true) | |
709 | { | |
710 | statement->input_statement.target = current_target; | |
711 | lookup_name(statement->input_statement.filename); | |
712 | } | |
2fa0b342 | 713 | break; |
1418c83b | 714 | default: |
2fa0b342 DHW |
715 | break; |
716 | } | |
2fa0b342 | 717 | } |
2fa0b342 DHW |
718 | /* If there are [COMMONS] statements, put a wild one into the bss section */ |
719 | ||
720 | static void | |
721 | lang_reasonable_defaults() | |
722 | { | |
1418c83b | 723 | #if 0 |
8cb5eb31 SC |
724 | lang_output_section_statement_lookup(".text"); |
725 | lang_output_section_statement_lookup(".data"); | |
726 | ||
2fa0b342 DHW |
727 | default_common_section = |
728 | lang_output_section_statement_lookup(".bss"); | |
8cb5eb31 | 729 | |
1418c83b | 730 | |
2fa0b342 DHW |
731 | if (placed_commons == false) { |
732 | lang_wild_statement_type *new = | |
733 | new_stat(lang_wild_statement, | |
734 | &default_common_section->children); | |
735 | new->section_name = "COMMON"; | |
736 | new->filename = (char *)NULL; | |
737 | lang_list_init(&new->children); | |
738 | } | |
1418c83b | 739 | #endif |
8cb5eb31 | 740 | |
2fa0b342 DHW |
741 | } |
742 | ||
1418c83b SC |
743 | /* |
744 | Add the supplied name to the symbol table as an undefined reference. | |
745 | Remove items from the chain as we open input bfds | |
746 | */ | |
747 | typedef struct ldlang_undef_chain_list_struct { | |
748 | struct ldlang_undef_chain_list_struct *next; | |
749 | char *name; | |
750 | } ldlang_undef_chain_list_type; | |
751 | ||
752 | static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head; | |
753 | ||
754 | void | |
755 | DEFUN(ldlang_add_undef,(name), | |
756 | CONST char *CONST name) | |
2fa0b342 | 757 | { |
1418c83b SC |
758 | ldlang_undef_chain_list_type *new = |
759 | (ldlang_undef_chain_list_type | |
3f8d46e7 | 760 | *)ldmalloc((bfd_size_type)(sizeof(ldlang_undef_chain_list_type))); |
1418c83b SC |
761 | |
762 | new->next = ldlang_undef_chain_list_head; | |
763 | ldlang_undef_chain_list_head = new; | |
764 | ||
765 | new->name = buystring(name); | |
766 | } | |
767 | /* Run through the list of undefineds created above and place them | |
768 | into the linker hash table as undefined symbols belonging to the | |
769 | script file. | |
770 | */ | |
771 | static void | |
772 | DEFUN_VOID(lang_place_undefineds) | |
773 | { | |
774 | ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head; | |
775 | while (ptr != (ldlang_undef_chain_list_type*)NULL) { | |
776 | ldsym_type *sy = ldsym_get(ptr->name); | |
777 | asymbol *def; | |
3f8d46e7 | 778 | asymbol **def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **))); |
1418c83b SC |
779 | def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd); |
780 | *def_ptr= def; | |
781 | def->name = ptr->name; | |
782 | def->flags = BSF_UNDEFINED; | |
783 | def->section = (asection *)NULL; | |
784 | Q_enter_global_ref(def_ptr); | |
785 | ptr = ptr->next; | |
2fa0b342 | 786 | } |
1418c83b | 787 | } |
2fa0b342 | 788 | |
1418c83b SC |
789 | |
790 | ||
791 | /* Copy important data from out internal form to the bfd way. Also | |
792 | create a section for the dummy file | |
793 | */ | |
794 | ||
795 | static void | |
796 | DEFUN_VOID(lang_create_output_section_statements) | |
797 | { | |
798 | lang_statement_union_type*os; | |
799 | for (os = lang_output_section_statement.head; | |
800 | os != (lang_statement_union_type*)NULL; | |
801 | os = os->output_section_statement.next) { | |
802 | lang_output_section_statement_type *s = | |
803 | &os->output_section_statement; | |
804 | init_os(s); | |
805 | } | |
806 | ||
807 | } | |
808 | ||
809 | static void | |
810 | DEFUN_VOID(lang_init_script_file) | |
811 | { | |
812 | script_file = lang_add_input_file("script file", | |
813 | lang_input_file_is_fake_enum, | |
814 | (char *)NULL); | |
815 | script_file->the_bfd = bfd_create("script file", output_bfd); | |
816 | script_file->symbol_count = 0; | |
817 | script_file->the_bfd->sections = output_bfd->sections; | |
2fa0b342 DHW |
818 | } |
819 | ||
820 | ||
1418c83b SC |
821 | |
822 | ||
2fa0b342 DHW |
823 | /* Open input files and attatch to output sections */ |
824 | static void | |
1418c83b SC |
825 | DEFUN(map_input_to_output_sections,(s, target, output_section_statement), |
826 | lang_statement_union_type *s AND | |
827 | CONST char *target AND | |
828 | lang_output_section_statement_type *output_section_statement) | |
2fa0b342 DHW |
829 | { |
830 | for (; s != (lang_statement_union_type *)NULL ; s = s->next) | |
831 | { | |
832 | switch (s->header.type) { | |
833 | case lang_wild_statement_enum: | |
834 | wild(&s->wild_statement, s->wild_statement.section_name, | |
835 | s->wild_statement.filename, target, | |
836 | output_section_statement); | |
837 | ||
838 | break; | |
839 | ||
840 | case lang_output_section_statement_enum: | |
1418c83b | 841 | map_input_to_output_sections(s->output_section_statement.children.head, |
2fa0b342 DHW |
842 | target, |
843 | &s->output_section_statement); | |
844 | break; | |
845 | case lang_output_statement_enum: | |
846 | break; | |
847 | case lang_target_statement_enum: | |
848 | target = s->target_statement.target; | |
849 | break; | |
2fa0b342 DHW |
850 | case lang_fill_statement_enum: |
851 | case lang_input_section_enum: | |
852 | case lang_object_symbols_statement_enum: | |
853 | case lang_data_statement_enum: | |
1418c83b SC |
854 | case lang_assignment_statement_enum: |
855 | case lang_padding_statement_enum: | |
2fa0b342 DHW |
856 | break; |
857 | case lang_afile_asection_pair_statement_enum: | |
858 | FAIL(); | |
859 | break; | |
2fa0b342 DHW |
860 | case lang_address_statement_enum: |
861 | /* Mark the specified section with the supplied address */ | |
862 | { | |
863 | lang_output_section_statement_type *os = | |
864 | lang_output_section_statement_lookup | |
865 | (s->address_statement.section_name); | |
866 | os->addr_tree = s->address_statement.address; | |
867 | } | |
868 | break; | |
869 | case lang_input_statement_enum: | |
870 | /* A standard input statement, has no wildcards */ | |
1418c83b | 871 | /* ldmain_open_file_read_symbol(&s->input_statement);*/ |
2fa0b342 DHW |
872 | break; |
873 | } | |
874 | } | |
875 | } | |
876 | ||
877 | ||
878 | ||
879 | ||
880 | ||
881 | static void | |
1418c83b SC |
882 | DEFUN(print_output_section_statement,(output_section_statement), |
883 | lang_output_section_statement_type *output_section_statement) | |
2fa0b342 DHW |
884 | { |
885 | asection *section = output_section_statement->bfd_section; | |
886 | print_nl(); | |
887 | print_section(output_section_statement->name); | |
888 | ||
889 | if (section) { | |
890 | print_dot = section->vma; | |
891 | print_space(); | |
892 | print_section(""); | |
893 | print_space(); | |
894 | print_address(section->vma); | |
895 | print_space(); | |
896 | print_size(section->size); | |
897 | print_space(); | |
898 | print_alignment(section->alignment_power); | |
899 | print_space(); | |
900 | #if 0 | |
901 | printf("%s flags", output_section_statement->region->name); | |
902 | print_flags(stdout, &output_section_statement->flags); | |
903 | #endif | |
904 | ||
905 | } | |
906 | else { | |
907 | printf("No attached output section"); | |
908 | } | |
909 | print_nl(); | |
910 | print_statement(output_section_statement->children.head, | |
911 | output_section_statement); | |
912 | ||
913 | } | |
914 | ||
915 | static void | |
1418c83b SC |
916 | DEFUN(print_assignment,(assignment, output_section), |
917 | lang_assignment_statement_type *assignment AND | |
918 | lang_output_section_statement_type *output_section) | |
2fa0b342 DHW |
919 | { |
920 | etree_value_type result; | |
921 | print_section(""); | |
922 | print_space(); | |
923 | print_section(""); | |
924 | print_space(); | |
925 | print_address(print_dot); | |
926 | print_space(); | |
927 | result = exp_fold_tree(assignment->exp->assign.src, | |
928 | output_section, | |
929 | lang_final_phase_enum, | |
930 | print_dot, | |
931 | &print_dot); | |
932 | ||
933 | if (result.valid) { | |
934 | print_address(result.value); | |
935 | } | |
936 | else | |
1418c83b SC |
937 | { |
938 | printf("*undefined*"); | |
939 | } | |
2fa0b342 DHW |
940 | print_space(); |
941 | exp_print_tree(stdout, assignment->exp); | |
942 | printf("\n"); | |
943 | } | |
944 | ||
945 | static void | |
1418c83b SC |
946 | DEFUN(print_input_statement,(statm), |
947 | lang_input_statement_type *statm) | |
2fa0b342 | 948 | { |
070aa819 SC |
949 | if (statm->filename != (char *)NULL) { |
950 | printf("LOAD %s\n",statm->filename); | |
951 | } | |
2fa0b342 DHW |
952 | } |
953 | ||
1418c83b SC |
954 | static void |
955 | DEFUN(print_symbol,(q), | |
956 | asymbol *q) | |
2fa0b342 DHW |
957 | { |
958 | print_section(""); | |
959 | printf(" "); | |
960 | print_section(""); | |
961 | printf(" "); | |
962 | print_address(outside_symbol_address(q)); | |
963 | printf(" %s", q->name ? q->name : " "); | |
964 | print_nl(); | |
965 | } | |
1418c83b | 966 | |
2fa0b342 | 967 | static void |
1418c83b SC |
968 | DEFUN(print_input_section,(in), |
969 | lang_input_section_type *in) | |
2fa0b342 DHW |
970 | { |
971 | asection *i = in->section; | |
972 | ||
973 | if(i->size != 0) { | |
974 | print_section(""); | |
975 | printf(" "); | |
976 | print_section(i->name); | |
977 | printf(" "); | |
978 | if (i->output_section) { | |
979 | print_address(i->output_section->vma + i->output_offset); | |
980 | printf(" "); | |
981 | print_size(i->size); | |
982 | printf(" "); | |
983 | print_alignment(i->alignment_power); | |
984 | printf(" "); | |
985 | if (in->ifile) { | |
1418c83b | 986 | |
2fa0b342 | 987 | bfd *abfd = in->ifile->the_bfd; |
1418c83b SC |
988 | if (in->ifile->just_syms_flag == true) { |
989 | printf("symbols only "); | |
990 | } | |
991 | ||
2fa0b342 DHW |
992 | printf(" %s ",abfd->xvec->name); |
993 | if(abfd->my_archive != (bfd *)NULL) { | |
994 | printf("[%s]%s", abfd->my_archive->filename, | |
995 | abfd->filename); | |
996 | } | |
997 | else { | |
998 | printf("%s", abfd->filename); | |
999 | } | |
3f8d46e7 | 1000 | printf("(%d bytes)", (int)bfd_alloc_size(abfd)); |
2fa0b342 DHW |
1001 | print_nl(); |
1002 | ||
1003 | /* Find all the symbols in this file defined in this section */ | |
1418c83b SC |
1004 | { |
1005 | asymbol **p; | |
1006 | for (p = in->ifile->asymbols; *p; p++) { | |
1007 | asymbol *q = *p; | |
2fa0b342 | 1008 | |
1418c83b SC |
1009 | if (bfd_get_section(q) == i && q->flags & BSF_GLOBAL) { |
1010 | print_symbol(q); | |
1011 | } | |
2fa0b342 DHW |
1012 | } |
1013 | } | |
2fa0b342 DHW |
1014 | } |
1015 | else { | |
1016 | print_nl(); | |
1017 | } | |
1018 | ||
1019 | ||
1020 | print_dot = outside_section_address(i) + i->size; | |
1021 | } | |
1022 | else { | |
1023 | printf("No output section allocated\n"); | |
1024 | } | |
1025 | } | |
1026 | } | |
2fa0b342 | 1027 | |
2fa0b342 | 1028 | static void |
1418c83b SC |
1029 | DEFUN(print_fill_statement,(fill), |
1030 | lang_fill_statement_type *fill) | |
2fa0b342 DHW |
1031 | { |
1032 | printf("FILL mask "); | |
1033 | print_fill( fill->fill); | |
1034 | } | |
1035 | ||
1036 | static void | |
1418c83b SC |
1037 | DEFUN(print_data_statement,(data), |
1038 | lang_data_statement_type *data) | |
2fa0b342 DHW |
1039 | { |
1040 | /* bfd_vma value; */ | |
1041 | print_section(""); | |
1042 | print_space(); | |
1043 | print_section(""); | |
1044 | print_space(); | |
65c552e3 | 1045 | /* ASSERT(print_dot == data->output_vma);*/ |
2fa0b342 DHW |
1046 | |
1047 | print_address(data->output_vma); | |
1048 | print_space(); | |
1049 | print_address(data->value); | |
1050 | print_space(); | |
1051 | switch (data->type) { | |
1052 | case BYTE : | |
1053 | printf("BYTE "); | |
1054 | print_dot += BYTE_SIZE; | |
1055 | break; | |
1056 | case SHORT: | |
1057 | printf("SHORT "); | |
1058 | print_dot += SHORT_SIZE; | |
1059 | break; | |
1060 | case LONG: | |
1061 | printf("LONG "); | |
1062 | print_dot += LONG_SIZE; | |
1063 | break; | |
1064 | } | |
1065 | ||
1066 | exp_print_tree(stdout, data->exp); | |
1067 | ||
1068 | printf("\n"); | |
1069 | } | |
1070 | ||
1071 | ||
1072 | static void | |
1418c83b SC |
1073 | DEFUN(print_padding_statement,(s), |
1074 | lang_padding_statement_type *s) | |
2fa0b342 DHW |
1075 | { |
1076 | print_section(""); | |
1077 | print_space(); | |
1078 | print_section("*fill*"); | |
1079 | print_space(); | |
1080 | print_address(s->output_offset + s->output_section->vma); | |
1081 | print_space(); | |
1082 | print_size(s->size); | |
1083 | print_space(); | |
1084 | print_fill(s->fill); | |
1085 | print_nl(); | |
1086 | } | |
1087 | ||
1418c83b SC |
1088 | static void |
1089 | DEFUN(print_wild_statement,(w,os), | |
1090 | lang_wild_statement_type *w AND | |
1091 | lang_output_section_statement_type *os) | |
2fa0b342 DHW |
1092 | { |
1093 | if (w->filename != (char *)NULL) { | |
1094 | printf("%s",w->filename); | |
1095 | } | |
1096 | else { | |
1097 | printf("*"); | |
1098 | } | |
1099 | if (w->section_name != (char *)NULL) { | |
1100 | printf("(%s)",w->section_name); | |
1101 | } | |
1102 | else { | |
1103 | printf("(*)"); | |
1104 | } | |
1105 | print_nl(); | |
1106 | print_statement(w->children.head, os); | |
1107 | ||
1108 | } | |
1109 | static void | |
1418c83b SC |
1110 | DEFUN(print_statement,(s, os), |
1111 | lang_statement_union_type *s AND | |
1112 | lang_output_section_statement_type *os) | |
2fa0b342 DHW |
1113 | { |
1114 | while (s) { | |
1115 | switch (s->header.type) { | |
1116 | case lang_wild_statement_enum: | |
1418c83b SC |
1117 | print_wild_statement(&s->wild_statement, os); |
1118 | break; | |
2fa0b342 | 1119 | default: |
1418c83b | 1120 | printf("Fail with %d\n",s->header.type); |
2fa0b342 DHW |
1121 | FAIL(); |
1122 | break; | |
1418c83b SC |
1123 | case lang_address_statement_enum: |
1124 | printf("address\n"); | |
1125 | break; | |
2fa0b342 DHW |
1126 | break; |
1127 | case lang_object_symbols_statement_enum: | |
1128 | printf("object symbols\n"); | |
1129 | break; | |
1130 | case lang_fill_statement_enum: | |
1131 | print_fill_statement(&s->fill_statement); | |
1132 | break; | |
1133 | case lang_data_statement_enum: | |
1134 | print_data_statement(&s->data_statement); | |
1135 | break; | |
2fa0b342 DHW |
1136 | case lang_input_section_enum: |
1137 | print_input_section(&s->input_section); | |
1138 | break; | |
1139 | case lang_padding_statement_enum: | |
1140 | print_padding_statement(&s->padding_statement); | |
1141 | break; | |
1142 | case lang_output_section_statement_enum: | |
1143 | print_output_section_statement(&s->output_section_statement); | |
1144 | break; | |
1145 | case lang_assignment_statement_enum: | |
1146 | print_assignment(&s->assignment_statement, | |
1418c83b | 1147 | os); |
2fa0b342 DHW |
1148 | break; |
1149 | ||
1150 | ||
1151 | case lang_target_statement_enum: | |
1152 | printf("TARGET(%s)\n", s->target_statement.target); | |
1153 | break; | |
1154 | case lang_output_statement_enum: | |
097879bc SC |
1155 | printf("OUTPUT(%s %s)\n", |
1156 | s->output_statement.name, | |
1157 | output_target); | |
2fa0b342 DHW |
1158 | break; |
1159 | case lang_input_statement_enum: | |
1160 | print_input_statement(&s->input_statement); | |
1161 | break; | |
1162 | case lang_afile_asection_pair_statement_enum: | |
1163 | FAIL(); | |
1164 | break; | |
1165 | } | |
1166 | s = s->next; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | ||
1171 | static void | |
1418c83b | 1172 | DEFUN_VOID(print_statements) |
2fa0b342 DHW |
1173 | { |
1174 | print_statement(statement_list.head, | |
1175 | (lang_output_section_statement_type *)NULL); | |
1176 | } | |
1177 | ||
1178 | static bfd_vma | |
1418c83b SC |
1179 | DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot), |
1180 | lang_statement_union_type **this_ptr AND | |
1181 | fill_type fill AND | |
1182 | unsigned int power AND | |
1183 | asection * output_section_statement AND | |
1184 | bfd_vma dot) | |
2fa0b342 DHW |
1185 | { |
1186 | /* Align this section first to the | |
1187 | input sections requirement, then | |
1188 | to the output section's requirement. | |
1189 | If this alignment is > than any seen before, | |
1190 | then record it too. Perform the alignment by | |
1191 | inserting a magic 'padding' statement. | |
1192 | */ | |
1193 | ||
1194 | unsigned int alignment_needed = align_power(dot, power) - dot; | |
1195 | ||
1196 | if (alignment_needed != 0) | |
1197 | { | |
1198 | lang_statement_union_type *new = | |
1199 | (lang_statement_union_type *) | |
3f8d46e7 | 1200 | ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type))); |
2fa0b342 DHW |
1201 | /* Link into existing chain */ |
1202 | new->header.next = *this_ptr; | |
1203 | *this_ptr = new; | |
1204 | new->header.type = lang_padding_statement_enum; | |
1205 | new->padding_statement.output_section = output_section_statement; | |
1206 | new->padding_statement.output_offset = | |
1207 | dot - output_section_statement->vma; | |
1208 | new->padding_statement.fill = fill; | |
1209 | new->padding_statement.size = alignment_needed; | |
1210 | } | |
1211 | ||
1212 | ||
1213 | /* Remember the most restrictive alignment */ | |
1214 | if (power > output_section_statement->alignment_power) { | |
1215 | output_section_statement->alignment_power = power; | |
1216 | } | |
1217 | output_section_statement->size += alignment_needed; | |
1218 | return alignment_needed + dot; | |
1219 | ||
1220 | } | |
1221 | ||
1418c83b | 1222 | /* Work out how much this section will move the dot point */ |
2fa0b342 | 1223 | static bfd_vma |
1418c83b SC |
1224 | DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot), |
1225 | lang_statement_union_type **this_ptr AND | |
1226 | lang_output_section_statement_type*output_section_statement AND | |
1227 | unsigned short fill AND | |
1228 | bfd_vma dot) | |
2fa0b342 DHW |
1229 | { |
1230 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
1231 | asection *i = is->section; | |
1418c83b SC |
1232 | |
1233 | if (is->ifile->just_syms_flag == false) { | |
ac004870 SC |
1234 | dot = insert_pad(this_ptr, fill, i->alignment_power, |
1235 | output_section_statement->bfd_section, dot); | |
2fa0b342 | 1236 | |
ac004870 SC |
1237 | /* remember the largest size so we can malloc the largest area */ |
1238 | /* needed for the output stage */ | |
1239 | if (i->size > largest_section) { | |
1240 | largest_section = i->size; | |
1241 | } | |
2fa0b342 | 1242 | |
ac004870 | 1243 | /* Remember where in the output section this input section goes */ |
2fa0b342 | 1244 | |
ac004870 SC |
1245 | i->output_offset = dot - output_section_statement->bfd_section->vma; |
1246 | ||
1247 | /* Mark how big the output section must be to contain this now */ | |
1248 | dot += i->size; | |
1249 | output_section_statement->bfd_section->size = | |
1250 | dot - output_section_statement->bfd_section->vma; | |
1251 | } | |
1252 | else | |
1253 | { | |
1254 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
1255 | } | |
2fa0b342 DHW |
1256 | |
1257 | return dot ; | |
1258 | } | |
1259 | ||
1260 | ||
1261 | /* Work out the size of the output sections | |
1262 | from the sizes of the input sections */ | |
1263 | static bfd_vma | |
1418c83b SC |
1264 | DEFUN(lang_size_sections,(s, output_section_statement, prev, fill, dot), |
1265 | lang_statement_union_type *s AND | |
1266 | lang_output_section_statement_type * output_section_statement AND | |
1267 | lang_statement_union_type **prev AND | |
1268 | unsigned short fill AND | |
1269 | bfd_vma dot) | |
2fa0b342 DHW |
1270 | { |
1271 | /* Size up the sections from their constituent parts */ | |
1272 | for (; s != (lang_statement_union_type *)NULL ; s = s->next) | |
1418c83b SC |
1273 | { |
1274 | switch (s->header.type) { | |
1275 | case lang_output_section_statement_enum: | |
1276 | { | |
1277 | bfd_vma after; | |
1278 | lang_output_section_statement_type *os = | |
1279 | &(s->output_section_statement); | |
1280 | /* The start of a section */ | |
2fa0b342 | 1281 | |
1418c83b SC |
1282 | if (os->addr_tree == (etree_type *)NULL) { |
1283 | /* No address specified for this section, get one | |
1284 | from the region specification | |
1285 | */ | |
1286 | if (os->region == (lang_memory_region_type *)NULL) { | |
2fa0b342 | 1287 | os->region = lang_memory_region_lookup("*default*"); |
1418c83b SC |
1288 | } |
1289 | dot = os->region->current; | |
2fa0b342 | 1290 | } |
1418c83b SC |
1291 | else { |
1292 | etree_value_type r ; | |
1293 | r = exp_fold_tree(os->addr_tree, | |
1294 | (lang_output_section_statement_type *)NULL, | |
1295 | lang_allocating_phase_enum, | |
1296 | dot, &dot); | |
1297 | if (r.valid == false) { | |
1298 | info("%F%S: non constant address expression for section %s\n", | |
1299 | os->name); | |
1300 | } | |
1301 | dot = r.value; | |
1302 | } | |
1303 | /* The section starts here */ | |
1304 | /* First, align to what the section needs */ | |
2fa0b342 | 1305 | |
1418c83b SC |
1306 | dot = align_power(dot, os->bfd_section->alignment_power); |
1307 | os->bfd_section->vma = dot; | |
1308 | os->bfd_section->output_offset = 0; | |
2fa0b342 | 1309 | |
1418c83b SC |
1310 | (void) lang_size_sections(os->children.head, os, &os->children.head, |
1311 | os->fill, dot); | |
1312 | /* Ignore the size of the input sections, use the vma and size to */ | |
1313 | /* align against */ | |
2fa0b342 DHW |
1314 | |
1315 | ||
1418c83b SC |
1316 | after = ALIGN(os->bfd_section->vma + |
1317 | os->bfd_section->size, | |
1318 | os->block_value) ; | |
2fa0b342 DHW |
1319 | |
1320 | ||
1418c83b SC |
1321 | os->bfd_section->size = after - os->bfd_section->vma; |
1322 | dot = os->bfd_section->vma + os->bfd_section->size; | |
1323 | os->processed = true; | |
2fa0b342 | 1324 | |
1418c83b SC |
1325 | /* Replace into region ? */ |
1326 | if (os->addr_tree == (etree_type *)NULL | |
1327 | && os->region !=(lang_memory_region_type*)NULL ) { | |
1328 | os->region->current = dot; | |
1329 | } | |
1330 | } | |
2fa0b342 | 1331 | |
1418c83b | 1332 | break; |
2fa0b342 | 1333 | |
1418c83b SC |
1334 | case lang_data_statement_enum: |
1335 | { | |
1336 | unsigned int size; | |
65c552e3 | 1337 | s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma; |
1418c83b SC |
1338 | s->data_statement.output_section = |
1339 | output_section_statement->bfd_section; | |
2fa0b342 | 1340 | |
1418c83b SC |
1341 | switch (s->data_statement.type) { |
1342 | case LONG: | |
1343 | size = LONG_SIZE; | |
1344 | break; | |
1345 | case SHORT: | |
1346 | size = SHORT_SIZE; | |
1347 | break; | |
1348 | case BYTE: | |
1349 | size = BYTE_SIZE; | |
1350 | break; | |
2fa0b342 | 1351 | |
1418c83b SC |
1352 | } |
1353 | dot += size; | |
1354 | output_section_statement->bfd_section->size += size; | |
1355 | } | |
1356 | break; | |
2fa0b342 | 1357 | |
1418c83b | 1358 | case lang_wild_statement_enum: |
2fa0b342 | 1359 | |
1418c83b | 1360 | dot = lang_size_sections(s->wild_statement.children.head, |
2fa0b342 DHW |
1361 | output_section_statement, |
1362 | &s->wild_statement.children.head, | |
1363 | ||
1364 | fill, dot); | |
1365 | ||
1418c83b | 1366 | break; |
2fa0b342 | 1367 | |
1418c83b SC |
1368 | case lang_object_symbols_statement_enum: |
1369 | create_object_symbols = output_section_statement; | |
1370 | break; | |
1371 | case lang_output_statement_enum: | |
1372 | case lang_target_statement_enum: | |
1373 | break; | |
1374 | case lang_input_section_enum: | |
1375 | dot = size_input_section(prev, | |
2fa0b342 DHW |
1376 | output_section_statement, |
1377 | output_section_statement->fill, dot); | |
1418c83b SC |
1378 | break; |
1379 | case lang_input_statement_enum: | |
1380 | break; | |
1381 | case lang_fill_statement_enum: | |
1382 | fill = s->fill_statement.fill; | |
1383 | break; | |
1384 | case lang_assignment_statement_enum: | |
2fa0b342 | 1385 | { |
1418c83b SC |
1386 | bfd_vma newdot = dot; |
1387 | exp_fold_tree(s->assignment_statement.exp, | |
1388 | output_section_statement, | |
1389 | lang_allocating_phase_enum, | |
1390 | dot, | |
1391 | &newdot); | |
1392 | ||
1393 | if (newdot != dot) | |
1394 | /* We've been moved ! so insert a pad */ | |
1395 | { | |
1396 | lang_statement_union_type *new = | |
1397 | (lang_statement_union_type *) | |
3f8d46e7 | 1398 | ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type))); |
1418c83b SC |
1399 | /* Link into existing chain */ |
1400 | new->header.next = *prev; | |
1401 | *prev = new; | |
1402 | new->header.type = lang_padding_statement_enum; | |
1403 | new->padding_statement.output_section = | |
1404 | output_section_statement->bfd_section; | |
1405 | new->padding_statement.output_offset = | |
1406 | dot - output_section_statement->bfd_section->vma; | |
1407 | new->padding_statement.fill = fill; | |
1408 | new->padding_statement.size = newdot - dot; | |
1409 | output_section_statement->bfd_section->size += | |
1410 | new->padding_statement.size; | |
1411 | dot = newdot; | |
1412 | } | |
2fa0b342 | 1413 | } |
2fa0b342 | 1414 | |
1418c83b SC |
1415 | break; |
1416 | case lang_padding_statement_enum: | |
1417 | FAIL(); | |
1418 | break; | |
1419 | default: | |
1420 | FAIL(); | |
1421 | break; | |
1422 | case lang_address_statement_enum: | |
1423 | break; | |
1424 | } | |
1425 | prev = &s->header.next; | |
2fa0b342 | 1426 | } |
2fa0b342 DHW |
1427 | return dot; |
1428 | } | |
1429 | ||
1430 | ||
1431 | static bfd_vma | |
1418c83b SC |
1432 | DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot), |
1433 | lang_statement_union_type *s AND | |
1434 | lang_output_section_statement_type * output_section_statement AND | |
1435 | unsigned short fill AND | |
1436 | bfd_vma dot) | |
2fa0b342 DHW |
1437 | { |
1438 | ||
1439 | for (; s != (lang_statement_union_type *)NULL ; s = s->next) | |
1440 | { | |
1441 | switch (s->header.type) { | |
1442 | case lang_output_section_statement_enum: | |
1443 | { | |
1444 | lang_output_section_statement_type *os = | |
1445 | &(s->output_section_statement); | |
1446 | dot = os->bfd_section->vma; | |
1447 | (void) lang_do_assignments(os->children.head, os, os->fill, dot); | |
1448 | dot = os->bfd_section->vma + os->bfd_section->size; | |
1449 | } | |
1450 | break; | |
1451 | case lang_wild_statement_enum: | |
1452 | ||
1453 | dot = lang_do_assignments(s->wild_statement.children.head, | |
1454 | output_section_statement, | |
1455 | fill, dot); | |
1456 | ||
1457 | break; | |
1458 | ||
1459 | case lang_object_symbols_statement_enum: | |
1460 | case lang_output_statement_enum: | |
1461 | case lang_target_statement_enum: | |
1418c83b | 1462 | #if 0 |
2fa0b342 | 1463 | case lang_common_statement_enum: |
1418c83b | 1464 | #endif |
2fa0b342 DHW |
1465 | break; |
1466 | case lang_data_statement_enum: | |
1467 | { | |
1468 | etree_value_type value ; | |
1469 | value = exp_fold_tree(s->data_statement.exp, | |
1470 | 0, lang_final_phase_enum, dot, &dot); | |
1471 | s->data_statement.value = value.value; | |
1472 | if (value.valid == false) info("%F%P: Invalid data statement\n"); | |
1473 | } | |
1474 | switch (s->data_statement.type) { | |
1475 | case LONG: | |
1476 | dot += LONG_SIZE; | |
1477 | break; | |
1478 | case SHORT: | |
1479 | dot += SHORT_SIZE; | |
1480 | break; | |
1481 | case BYTE: | |
1482 | dot += BYTE_SIZE; | |
1483 | break; | |
1484 | } | |
1485 | break; | |
1486 | case lang_input_section_enum: | |
1487 | { | |
1488 | asection *in = s->input_section.section; | |
1489 | dot += in->size; | |
1490 | } | |
1491 | break; | |
1492 | ||
1493 | case lang_input_statement_enum: | |
1494 | break; | |
1495 | case lang_fill_statement_enum: | |
1496 | fill = s->fill_statement.fill; | |
1497 | break; | |
1498 | case lang_assignment_statement_enum: | |
1499 | { | |
1500 | exp_fold_tree(s->assignment_statement.exp, | |
1501 | output_section_statement, | |
1502 | lang_final_phase_enum, | |
1503 | dot, | |
1504 | &dot); | |
1505 | } | |
1506 | ||
1507 | break; | |
1508 | case lang_padding_statement_enum: | |
1509 | dot += s->padding_statement.size; | |
1510 | break; | |
1511 | default: | |
1512 | FAIL(); | |
1513 | break; | |
1514 | case lang_address_statement_enum: | |
1515 | break; | |
1516 | } | |
1517 | ||
1518 | } | |
1519 | return dot; | |
1520 | } | |
1521 | ||
1522 | ||
1523 | ||
1418c83b SC |
1524 | static void |
1525 | DEFUN_VOID(lang_relocate_globals) | |
2fa0b342 DHW |
1526 | { |
1527 | ||
1528 | /* | |
1418c83b SC |
1529 | Each ldsym_type maintains a chain of pointers to asymbols which |
1530 | references the definition. Replace each pointer to the referenence | |
1531 | with a pointer to only one place, preferably the definition. If | |
1532 | the defintion isn't available then the common symbol, and if | |
1533 | there isn't one of them then choose one reference. | |
1534 | */ | |
2fa0b342 DHW |
1535 | |
1536 | FOR_EACH_LDSYM(lgs) { | |
1537 | asymbol *it; | |
1538 | if (lgs->sdefs_chain) { | |
1539 | it = *(lgs->sdefs_chain); | |
1540 | } | |
1541 | else if (lgs->scoms_chain != (asymbol **)NULL) { | |
1542 | it = *(lgs->scoms_chain); | |
1543 | } | |
1544 | else if (lgs->srefs_chain != (asymbol **)NULL) { | |
1545 | it = *(lgs->srefs_chain); | |
1546 | } | |
1547 | else { | |
1418c83b SC |
1548 | /* This can happen when the command line asked for a symbol to |
1549 | be -u */ | |
1550 | it = (asymbol *)NULL; | |
2fa0b342 DHW |
1551 | } |
1552 | if (it != (asymbol *)NULL) | |
1418c83b SC |
1553 | { |
1554 | asymbol **ptr= lgs->srefs_chain; | |
2fa0b342 | 1555 | |
1418c83b SC |
1556 | while (ptr != (asymbol **)NULL) { |
1557 | asymbol *ref = *ptr; | |
1558 | *ptr = it; | |
1559 | ptr = (asymbol **)(ref->udata); | |
1560 | } | |
2fa0b342 | 1561 | } |
2fa0b342 DHW |
1562 | } |
1563 | } | |
1564 | ||
1565 | ||
1566 | ||
2fa0b342 | 1567 | static void |
1418c83b | 1568 | DEFUN_VOID(lang_finish) |
2fa0b342 DHW |
1569 | { |
1570 | ldsym_type *lgs; | |
1571 | ||
1572 | if (entry_symbol == (char *)NULL) { | |
1573 | /* No entry has been specified, look for start */ | |
1574 | entry_symbol = "start"; | |
1575 | } | |
1576 | lgs = ldsym_get_soft(entry_symbol); | |
1577 | if (lgs && lgs->sdefs_chain) { | |
1578 | asymbol *sy = *(lgs->sdefs_chain); | |
1579 | /* We can set the entry address*/ | |
1580 | bfd_set_start_address(output_bfd, | |
1581 | outside_symbol_address(sy)); | |
1582 | ||
1583 | } | |
1584 | else { | |
1585 | /* Can't find anything reasonable, | |
1586 | use the first address in the text section | |
1587 | */ | |
1588 | asection *ts = bfd_get_section_by_name(output_bfd, ".text"); | |
1589 | if (ts) { | |
1590 | bfd_set_start_address(output_bfd, ts->vma); | |
1591 | } | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | /* By now we know the target architecture, and we may have an */ | |
1596 | /* ldfile_output_machine_name */ | |
1597 | static void | |
1418c83b | 1598 | DEFUN_VOID(lang_check) |
2fa0b342 DHW |
1599 | { |
1600 | lang_statement_union_type *file; | |
3f8d46e7 JG |
1601 | unsigned long max_machine = bfd_get_machine(output_bfd); |
1602 | unsigned long max_machine_seen = 0; | |
1603 | bfd * input_bfd; | |
1604 | unsigned long input_machine; | |
1605 | char *out_arch, *out_arch2; | |
2fa0b342 | 1606 | |
2fa0b342 DHW |
1607 | for (file = file_chain.head; |
1608 | file != (lang_statement_union_type *)NULL; | |
1609 | file=file->input_statement.next) | |
1418c83b | 1610 | { |
3f8d46e7 JG |
1611 | input_bfd = file->input_statement.the_bfd; |
1612 | input_machine = bfd_get_machine(input_bfd); | |
1613 | ||
1614 | if ( input_machine > max_machine_seen ){ | |
1615 | max_machine_seen = input_machine; | |
1616 | } | |
1617 | ||
1618 | /* Inspect the architecture and ensure we're linking like with like */ | |
1619 | if ( (input_machine > max_machine) | |
1620 | || !bfd_arch_compatible( input_bfd, | |
1621 | output_bfd, | |
1622 | &ldfile_output_architecture, | |
1623 | NULL)) { | |
1624 | enum bfd_architecture this_architecture = | |
1625 | bfd_get_architecture(file->input_statement.the_bfd); | |
1626 | unsigned long this_machine = | |
1627 | ||
1418c83b | 1628 | bfd_get_machine(file->input_statement.the_bfd); |
3f8d46e7 JG |
1629 | |
1630 | /* Result of bfd_printable_arch_mach is not guaranteed to stick | |
1631 | around after next call, so we have to copy it. */ | |
1632 | out_arch = bfd_printable_arch_mach(ldfile_output_architecture, | |
1633 | ldfile_output_machine); | |
1634 | out_arch2 = ldmalloc (strlen (out_arch)+1); | |
1635 | strcpy (out_arch2, out_arch); | |
1636 | ||
1637 | info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n", | |
1638 | bfd_printable_arch_mach(this_architecture, this_machine), | |
1639 | input_bfd, | |
1640 | out_arch2); | |
1641 | free (out_arch2); | |
1418c83b SC |
1642 | ldfile_output_architecture = this_architecture; |
1643 | ldfile_output_machine = this_machine; | |
1644 | bfd_set_arch_mach(output_bfd, | |
1645 | ldfile_output_architecture, | |
1646 | ldfile_output_machine); | |
1418c83b | 1647 | } |
2fa0b342 | 1648 | } |
3f8d46e7 | 1649 | bfd_set_arch_mach(output_bfd,ldfile_output_architecture,max_machine_seen); |
2fa0b342 DHW |
1650 | } |
1651 | ||
1652 | ||
1653 | /* | |
1654 | * run through all the global common symbols and tie them | |
b6316534 SC |
1655 | * to the output section requested. |
1656 | * | |
1657 | As an experiment we do this 4 times, once for all the byte sizes, | |
1658 | then all the two bytes, all the four bytes and then everything else | |
1659 | */ | |
2fa0b342 DHW |
1660 | |
1661 | static void | |
1418c83b | 1662 | DEFUN_VOID(lang_common) |
2fa0b342 DHW |
1663 | { |
1664 | ldsym_type *lgs; | |
b6316534 | 1665 | size_t power; |
2fa0b342 DHW |
1666 | if (config.relocateable_output == false || |
1667 | command_line.force_common_definition== true) { | |
b6316534 SC |
1668 | for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<=1) { |
1669 | for (lgs = symbol_head; | |
1670 | lgs != (ldsym_type *)NULL; | |
1671 | lgs=lgs->next) | |
1418c83b SC |
1672 | { |
1673 | asymbol *com ; | |
1674 | unsigned int power_of_two; | |
1675 | size_t size; | |
1676 | size_t align; | |
1677 | if (lgs->scoms_chain != (asymbol **)NULL) { | |
1678 | com = *(lgs->scoms_chain); | |
1679 | size = com->value; | |
1680 | switch (size) { | |
1681 | case 0: | |
1682 | case 1: | |
1683 | align = 1; | |
1684 | power_of_two = 0; | |
1685 | break; | |
1686 | case 2: | |
1687 | power_of_two = 1; | |
1688 | align = 2; | |
1689 | break; | |
1690 | case 3: | |
1691 | case 4: | |
1692 | power_of_two =2; | |
1693 | align = 4; | |
1694 | break; | |
1695 | case 5: | |
1696 | case 6: | |
1697 | case 7: | |
1698 | case 8: | |
1699 | power_of_two = 3; | |
2fa0b342 | 1700 | align = 8; |
1418c83b SC |
1701 | break; |
1702 | default: | |
1703 | power_of_two = 4; | |
1704 | align = 16; | |
1705 | break; | |
2fa0b342 | 1706 | } |
b6316534 SC |
1707 | if (config.sort_common == false || align == power) { |
1708 | /* Change from a common symbol into a definition of | |
1709 | a symbol */ | |
1710 | lgs->sdefs_chain = lgs->scoms_chain; | |
1711 | lgs->scoms_chain = (asymbol **)NULL; | |
1712 | commons_pending--; | |
1713 | /* Point to the correct common section */ | |
1714 | com->section = | |
1715 | ((lang_input_statement_type *) | |
1716 | (com->the_bfd->usrdata))->common_section; | |
1717 | /* Fix the size of the common section */ | |
1718 | com->section->size = ALIGN(com->section->size, align); | |
1719 | ||
1720 | /* Remember if this is the biggest alignment ever seen */ | |
1721 | if (power_of_two > com->section->alignment_power) { | |
1722 | com->section->alignment_power = power_of_two; | |
1723 | } | |
2fa0b342 | 1724 | |
b6316534 SC |
1725 | /* Symbol stops being common and starts being global, but |
1726 | we remember that it was common once. */ | |
2fa0b342 | 1727 | |
b6316534 SC |
1728 | com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON; |
1729 | com->value = com->section->size; | |
1418c83b | 1730 | |
b6316534 | 1731 | if (write_map) |
1418c83b | 1732 | { |
b6316534 | 1733 | printf ("Allocating common %s: %x at %x %s\n", |
1418c83b SC |
1734 | lgs->name, |
1735 | (unsigned) size, | |
b6316534 SC |
1736 | (unsigned) com->value, |
1737 | com->the_bfd->filename); | |
1418c83b | 1738 | } |
097879bc | 1739 | |
b6316534 | 1740 | com->section->size += size; |
1418c83b | 1741 | |
b6316534 | 1742 | } |
1418c83b | 1743 | } |
b6316534 | 1744 | |
2fa0b342 | 1745 | } |
b6316534 | 1746 | } |
2fa0b342 | 1747 | } |
1418c83b SC |
1748 | |
1749 | ||
2fa0b342 DHW |
1750 | } |
1751 | ||
1752 | /* | |
1753 | run through the input files and ensure that every input | |
1754 | section has somewhere to go. If one is found without | |
1755 | a destination then create an input request and place it | |
1756 | into the statement tree. | |
1757 | */ | |
1758 | ||
1418c83b SC |
1759 | static void |
1760 | DEFUN_VOID(lang_place_orphans) | |
2fa0b342 DHW |
1761 | { |
1762 | lang_input_statement_type *file; | |
1763 | for (file = (lang_input_statement_type*)file_chain.head; | |
1764 | file != (lang_input_statement_type*)NULL; | |
1765 | file = (lang_input_statement_type*)file->next) { | |
1766 | asection *s; | |
1767 | for (s = file->the_bfd->sections; | |
1768 | s != (asection *)NULL; | |
1769 | s = s->next) { | |
1770 | if ( s->output_section == (asection *)NULL) { | |
1771 | /* This section of the file is not attatched, root | |
1772 | around for a sensible place for it to go */ | |
1773 | ||
1774 | if (file->common_section == s) { | |
1775 | /* This is a lonely common section which must | |
1776 | have come from an archive. We attatch to the | |
1777 | section with the wildcard */ | |
1418c83b SC |
1778 | if (config.relocateable_output != true |
1779 | && command_line.force_common_definition == false) { | |
1780 | if (default_common_section == | |
1781 | (lang_output_section_statement_type *)NULL) { | |
1782 | info("%P: No [COMMON] command, defaulting to .bss\n"); | |
1783 | ||
1784 | default_common_section = | |
1785 | lang_output_section_statement_lookup(".bss"); | |
1786 | ||
1787 | } | |
1788 | wild_doit(&default_common_section->children, s, | |
1789 | default_common_section, file); | |
1790 | } | |
2fa0b342 DHW |
1791 | } |
1792 | else { | |
1793 | lang_output_section_statement_type *os = | |
1794 | lang_output_section_statement_lookup(s->name); | |
1795 | ||
1796 | wild_doit(&os->children, s, os, file); | |
1797 | } | |
1798 | } | |
1799 | } | |
2fa0b342 DHW |
1800 | } |
1801 | } | |
1802 | ||
1803 | ||
2fa0b342 | 1804 | void |
1418c83b SC |
1805 | DEFUN(lang_set_flags,(ptr, flags), |
1806 | lang_section_flags_type *ptr AND | |
1807 | CONST char *flags) | |
2fa0b342 DHW |
1808 | { |
1809 | boolean state = true; | |
1810 | ptr->flag_read = false; | |
1811 | ptr->flag_write = false; | |
1812 | ptr->flag_executable = false; | |
1813 | ptr->flag_loadable= false; | |
1814 | while (*flags) | |
1418c83b SC |
1815 | { |
1816 | if (*flags == '!') { | |
1817 | state = false; | |
1818 | flags++; | |
1819 | } | |
1820 | else state = true; | |
1821 | switch (*flags) { | |
1822 | case 'R': | |
1823 | ptr->flag_read = state; | |
1824 | break; | |
1825 | case 'W': | |
1826 | ptr->flag_write = state; | |
1827 | break; | |
1828 | case 'X': | |
1829 | ptr->flag_executable= state; | |
1830 | break; | |
1831 | case 'L': | |
b6316534 | 1832 | case 'I': |
1418c83b SC |
1833 | ptr->flag_loadable= state; |
1834 | break; | |
1835 | default: | |
1836 | info("%P%F illegal syntax in flags\n"); | |
1837 | break; | |
1838 | } | |
2fa0b342 DHW |
1839 | flags++; |
1840 | } | |
2fa0b342 DHW |
1841 | } |
1842 | ||
1843 | ||
1844 | ||
1845 | void | |
1418c83b SC |
1846 | DEFUN(lang_for_each_file,(func), |
1847 | PROTO(void, (*func),(lang_input_statement_type *))) | |
2fa0b342 DHW |
1848 | { |
1849 | lang_input_statement_type *f; | |
1850 | for (f = (lang_input_statement_type *)file_chain.head; | |
1851 | f != (lang_input_statement_type *)NULL; | |
1852 | f = (lang_input_statement_type *)f->next) | |
1418c83b SC |
1853 | { |
1854 | func(f); | |
1855 | } | |
2fa0b342 DHW |
1856 | } |
1857 | ||
1858 | ||
1859 | void | |
1418c83b SC |
1860 | DEFUN(lang_for_each_input_section, (func), |
1861 | PROTO(void ,(*func),(bfd *ab, asection*as))) | |
2fa0b342 DHW |
1862 | { |
1863 | lang_input_statement_type *f; | |
1864 | for (f = (lang_input_statement_type *)file_chain.head; | |
1865 | f != (lang_input_statement_type *)NULL; | |
1866 | f = (lang_input_statement_type *)f->next) | |
1867 | { | |
1868 | asection *s; | |
1869 | for (s = f->the_bfd->sections; | |
1870 | s != (asection *)NULL; | |
1871 | s = s->next) { | |
1872 | func(f->the_bfd, s); | |
1873 | } | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | ||
1878 | ||
1879 | void | |
1418c83b SC |
1880 | DEFUN(ldlang_add_file,(entry), |
1881 | lang_input_statement_type *entry) | |
2fa0b342 | 1882 | { |
1418c83b | 1883 | |
2fa0b342 DHW |
1884 | lang_statement_append(&file_chain, |
1885 | (lang_statement_union_type *)entry, | |
1886 | &entry->next); | |
1887 | } | |
1888 | ||
1889 | ||
1890 | ||
1891 | void | |
1418c83b SC |
1892 | DEFUN(lang_add_output,(name), |
1893 | CONST char *name) | |
2fa0b342 DHW |
1894 | { |
1895 | lang_output_statement_type *new = new_stat(lang_output_statement, | |
1896 | stat_ptr); | |
1897 | new->name = name; | |
1898 | had_output_filename = true; | |
1899 | } | |
1900 | ||
1901 | ||
1902 | static lang_output_section_statement_type *current_section; | |
1903 | ||
1904 | void | |
1418c83b SC |
1905 | DEFUN(lang_enter_output_section_statement, |
1906 | (output_section_statement_name, | |
1907 | address_exp, | |
1908 | block_value), | |
1909 | char *output_section_statement_name AND | |
1910 | etree_type *address_exp AND | |
1911 | bfd_vma block_value) | |
2fa0b342 DHW |
1912 | { |
1913 | lang_output_section_statement_type *os; | |
1914 | current_section = | |
1915 | os = | |
1916 | lang_output_section_statement_lookup(output_section_statement_name); | |
1917 | ||
1918 | ||
1919 | /* Add this statement to tree */ | |
1920 | /* add_statement(lang_output_section_statement_enum, | |
1921 | output_section_statement);*/ | |
1922 | /* Make next things chain into subchain of this */ | |
1923 | ||
1924 | if (os->addr_tree == | |
1925 | (etree_type *)NULL) { | |
1926 | os->addr_tree = | |
1927 | address_exp; | |
1928 | } | |
1929 | os->block_value = block_value; | |
1930 | stat_ptr = & os->children; | |
1931 | ||
1932 | } | |
1933 | ||
1934 | ||
1935 | void | |
1418c83b | 1936 | DEFUN_VOID(lang_final) |
2fa0b342 DHW |
1937 | { |
1938 | if (had_output_filename == false) { | |
e89432c7 SC |
1939 | extern CONST char *output_filename; |
1940 | lang_add_output(output_filename); | |
2fa0b342 | 1941 | } |
2fa0b342 DHW |
1942 | } |
1943 | ||
1944 | ||
1945 | ||
1946 | ||
1947 | ||
1418c83b SC |
1948 | asymbol * |
1949 | DEFUN(create_symbol,(name, flags, section), | |
1950 | CONST char *name AND | |
1951 | flagword flags AND | |
1952 | asection *section) | |
2fa0b342 DHW |
1953 | { |
1954 | extern lang_input_statement_type *script_file; | |
3f8d46e7 | 1955 | asymbol **def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **))); |
2fa0b342 DHW |
1956 | /* Add this definition to script file */ |
1957 | asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd); | |
1418c83b | 1958 | def->name = buystring(name); |
2fa0b342 DHW |
1959 | def->udata = 0; |
1960 | def->flags = flags; | |
1961 | def->section = section; | |
1962 | ||
1963 | *def_ptr = def; | |
1964 | Q_enter_global_ref(def_ptr); | |
1965 | return def; | |
1966 | } | |
1967 | ||
1968 | ||
1969 | void | |
1418c83b SC |
1970 | DEFUN_VOID(lang_process) |
1971 | { | |
1972 | if (had_script == false) { | |
1973 | parse_line(ldemul_get_script()); | |
1974 | } | |
1975 | lang_reasonable_defaults(); | |
1976 | current_target = default_target; | |
1977 | ||
1978 | lang_for_each_statement(ldlang_open_output); /* Open the output file */ | |
1979 | /* For each output section statement, create a section in the output | |
1980 | file */ | |
1981 | lang_create_output_section_statements(); | |
1982 | ||
1983 | /* Create a dummy bfd for the script */ | |
1984 | lang_init_script_file(); | |
1985 | ||
1986 | /* Add to the hash table all undefineds on the command line */ | |
1987 | lang_place_undefineds(); | |
1988 | ||
1989 | /* Create a bfd for each input file */ | |
1990 | current_target = default_target; | |
1991 | lang_for_each_statement(open_input_bfds); | |
1992 | ||
a0ad3035 | 1993 | common_section.userdata = (PTR)&common_section_userdata; |
1418c83b SC |
1994 | |
1995 | /* Run through the contours of the script and attatch input sections | |
1996 | to the correct output sections | |
1997 | */ | |
1998 | map_input_to_output_sections(statement_list.head, (char *)NULL, | |
1999 | ( lang_output_section_statement_type *)NULL); | |
2000 | ||
2001 | /* Find any sections not attatched explicitly and handle them */ | |
2002 | lang_place_orphans(); | |
2003 | ||
2004 | /* Size up the common data */ | |
2005 | lang_common(); | |
2006 | ||
2007 | ldemul_before_allocation(); | |
2008 | ||
2009 | /* Size up the sections */ | |
2010 | lang_size_sections(statement_list.head, | |
2011 | (lang_output_section_statement_type *)NULL, | |
2012 | &(statement_list.head), 0, (bfd_vma)0); | |
2013 | ||
2014 | /* See if anything special should be done now we know how big | |
2015 | everything is */ | |
2016 | ldemul_after_allocation(); | |
2017 | ||
2018 | /* Do all the assignments, now that we know the final restingplaces | |
2019 | of all the symbols */ | |
2020 | ||
2021 | lang_do_assignments(statement_list.head, | |
2022 | (lang_output_section_statement_type *)NULL, | |
2023 | 0, (bfd_vma)0); | |
2024 | ||
2025 | /* Make sure that we're not mixing architectures */ | |
2026 | ||
2027 | lang_check(); | |
2028 | ||
2029 | /* Move the global symbols around */ | |
2030 | lang_relocate_globals(); | |
2031 | ||
2032 | /* Final stuffs */ | |
2033 | lang_finish(); | |
2fa0b342 DHW |
2034 | } |
2035 | ||
2036 | ||
2037 | /* EXPORTED TO YACC */ | |
1418c83b | 2038 | |
2fa0b342 | 2039 | void |
1418c83b SC |
2040 | DEFUN(lang_add_wild,(section_name, filename), |
2041 | CONST char *CONST section_name AND | |
2042 | CONST char *CONST filename) | |
2043 | { | |
2044 | lang_wild_statement_type *new = new_stat(lang_wild_statement, | |
2045 | stat_ptr); | |
2046 | ||
2047 | if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0) | |
2048 | { | |
2049 | placed_commons = true; | |
2050 | } | |
2051 | if (filename != (char *)NULL) { | |
2052 | lang_has_input_file = true; | |
2053 | } | |
2054 | new->section_name = section_name; | |
2055 | new->filename = filename; | |
2056 | lang_list_init(&new->children); | |
2057 | } | |
2058 | void | |
2059 | DEFUN(lang_section_start,(name, address), | |
2060 | CONST char *name AND | |
2061 | etree_type *address) | |
2fa0b342 DHW |
2062 | { |
2063 | lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr); | |
2064 | ad->section_name = name; | |
2065 | ad->address = address; | |
2066 | } | |
1418c83b SC |
2067 | |
2068 | void | |
2069 | DEFUN(lang_add_entry,(name), | |
2070 | CONST char *name) | |
2fa0b342 DHW |
2071 | { |
2072 | entry_symbol = name; | |
2073 | } | |
2074 | ||
2075 | void | |
1418c83b SC |
2076 | DEFUN(lang_add_target,(name), |
2077 | CONST char *name) | |
2fa0b342 DHW |
2078 | { |
2079 | lang_target_statement_type *new = new_stat(lang_target_statement, | |
2080 | stat_ptr); | |
2081 | new->target = name; | |
2082 | ||
2083 | } | |
2fa0b342 | 2084 | |
1418c83b SC |
2085 | |
2086 | ||
2fa0b342 DHW |
2087 | |
2088 | void | |
1418c83b SC |
2089 | DEFUN(lang_add_map,(name), |
2090 | CONST char *name) | |
2fa0b342 DHW |
2091 | { |
2092 | while (*name) { | |
2093 | switch (*name) { | |
2094 | case 'F': | |
2095 | map_option_f = true; | |
2096 | break; | |
2097 | } | |
2098 | name++; | |
2099 | } | |
2100 | } | |
2101 | ||
1418c83b SC |
2102 | void |
2103 | DEFUN(lang_add_fill,(exp), | |
2104 | int exp) | |
2fa0b342 DHW |
2105 | { |
2106 | lang_fill_statement_type *new = new_stat(lang_fill_statement, | |
2107 | stat_ptr); | |
2108 | new->fill = exp; | |
2109 | } | |
2110 | ||
1418c83b SC |
2111 | void |
2112 | DEFUN(lang_add_data,(type, exp), | |
2113 | int type AND | |
2114 | union etree_union *exp) | |
2fa0b342 DHW |
2115 | { |
2116 | ||
2117 | lang_data_statement_type *new = new_stat(lang_data_statement, | |
2118 | stat_ptr); | |
2119 | new->exp = exp; | |
2120 | new->type = type; | |
2121 | ||
2122 | } | |
2123 | void | |
1418c83b SC |
2124 | DEFUN(lang_add_assignment,(exp), |
2125 | etree_type *exp) | |
2fa0b342 DHW |
2126 | { |
2127 | lang_assignment_statement_type *new = new_stat(lang_assignment_statement, | |
2128 | stat_ptr); | |
2129 | new->exp = exp; | |
2130 | } | |
2131 | ||
2132 | void | |
1418c83b SC |
2133 | DEFUN(lang_add_attribute,(attribute), |
2134 | enum statement_enum attribute) | |
2fa0b342 DHW |
2135 | { |
2136 | new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr); | |
2137 | } | |
2138 | ||
2139 | ||
2140 | ||
2141 | void | |
1418c83b SC |
2142 | DEFUN(lang_startup,(name), |
2143 | CONST char *name) | |
2fa0b342 DHW |
2144 | { |
2145 | if (startup_file != (char *)NULL) { | |
2146 | info("%P%FMultiple STARTUP files\n"); | |
2147 | } | |
2148 | first_file->filename = name; | |
2149 | first_file->local_sym_name = name; | |
2150 | ||
2151 | startup_file= name; | |
2152 | } | |
2153 | void | |
1418c83b SC |
2154 | DEFUN(lang_float,(maybe), |
2155 | boolean maybe) | |
2fa0b342 DHW |
2156 | { |
2157 | lang_float_flag = maybe; | |
2158 | } | |
2159 | ||
2160 | void | |
1418c83b SC |
2161 | DEFUN(lang_leave_output_section_statement,(fill, memspec), |
2162 | bfd_vma fill AND | |
2163 | CONST char *memspec) | |
2fa0b342 DHW |
2164 | { |
2165 | current_section->fill = fill; | |
2166 | current_section->region = lang_memory_region_lookup(memspec); | |
2167 | stat_ptr = &statement_list; | |
2168 | } | |
9f32f7c2 SC |
2169 | /* |
2170 | Create an absolute symbol with the given name with the value of the | |
2171 | address of first byte of the section named. | |
2fa0b342 | 2172 | |
9f32f7c2 SC |
2173 | If the symbol already exists, then do nothing. |
2174 | */ | |
8cb5eb31 | 2175 | void |
1418c83b SC |
2176 | DEFUN(lang_abs_symbol_at_beginning_of,(section, name), |
2177 | CONST char *section AND | |
2178 | CONST char *name) | |
8cb5eb31 | 2179 | { |
c660714f | 2180 | if (ldsym_undefined(name)) { |
9f32f7c2 SC |
2181 | extern bfd *output_bfd; |
2182 | extern asymbol *create_symbol(); | |
2183 | asection *s = bfd_get_section_by_name(output_bfd, section); | |
2184 | asymbol *def = create_symbol(name, | |
2185 | BSF_GLOBAL | BSF_EXPORT | | |
2186 | BSF_ABSOLUTE, | |
2187 | (asection *)NULL); | |
2188 | if (s != (asection *)NULL) { | |
2189 | def->value = s->vma; | |
2190 | } | |
2191 | else { | |
2192 | def->value = 0; | |
2193 | } | |
8cb5eb31 SC |
2194 | } |
2195 | } | |
2196 | ||
9f32f7c2 SC |
2197 | /* |
2198 | Create an absolute symbol with the given name with the value of the | |
2199 | address of the first byte after the end of the section named. | |
2200 | ||
2201 | If the symbol already exists, then do nothing. | |
2202 | */ | |
2fa0b342 | 2203 | void |
1418c83b SC |
2204 | DEFUN(lang_abs_symbol_at_end_of,(section, name), |
2205 | CONST char *section AND | |
2206 | CONST char *name) | |
2fa0b342 | 2207 | { |
c660714f | 2208 | if (ldsym_undefined(name)){ |
9f32f7c2 SC |
2209 | extern bfd *output_bfd; |
2210 | extern asymbol *create_symbol(); | |
2211 | asection *s = bfd_get_section_by_name(output_bfd, section); | |
2212 | /* Add a symbol called _end */ | |
2213 | asymbol *def = create_symbol(name, | |
2214 | BSF_GLOBAL | BSF_EXPORT | | |
2215 | BSF_ABSOLUTE, | |
2216 | (asection *)NULL); | |
2217 | if (s != (asection *)NULL) { | |
2218 | def->value = s->vma + s->size; | |
2219 | } | |
2220 | else { | |
2221 | def->value = 0; | |
2222 | } | |
2fa0b342 DHW |
2223 | } |
2224 | } | |
2225 | ||
2226 | void | |
1418c83b SC |
2227 | DEFUN(lang_statement_append,(list, element, field), |
2228 | lang_statement_list_type *list AND | |
2229 | lang_statement_union_type *element AND | |
2230 | lang_statement_union_type **field) | |
2fa0b342 DHW |
2231 | { |
2232 | *(list->tail) = element; | |
2233 | list->tail = field; | |
2234 | } | |
2235 | ||
097879bc SC |
2236 | /* Set the output format type */ |
2237 | void | |
2238 | DEFUN(lang_add_output_format,(format), | |
2239 | CONST char *format) | |
2240 | { | |
2241 | output_target = format; | |
2242 | } | |
a37cc0c0 | 2243 |