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