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