Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Linker command language support. |
a2b64bed | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
e5caa5e0 | 3 | 2001, 2002, 2003, 2004 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
53b2a62f | 6 | This file is part of GLD, the Gnu Linker. |
252b5132 | 7 | |
53b2a62f NC |
8 | GLD is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
252b5132 | 12 | |
53b2a62f NC |
13 | GLD is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
53b2a62f NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with GLD; see the file COPYING. If not, write to the Free | |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-1307, USA. */ | |
252b5132 RH |
22 | |
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "libiberty.h" | |
3882b010 | 26 | #include "safe-ctype.h" |
252b5132 RH |
27 | #include "obstack.h" |
28 | #include "bfdlink.h" | |
29 | ||
30 | #include "ld.h" | |
31 | #include "ldmain.h" | |
252b5132 RH |
32 | #include "ldexp.h" |
33 | #include "ldlang.h" | |
df2a7313 | 34 | #include <ldgram.h> |
252b5132 RH |
35 | #include "ldlex.h" |
36 | #include "ldmisc.h" | |
37 | #include "ldctor.h" | |
38 | #include "ldfile.h" | |
b71e2778 | 39 | #include "ldemul.h" |
252b5132 RH |
40 | #include "fnmatch.h" |
41 | #include "demangle.h" | |
108ba305 | 42 | #include "hashtab.h" |
252b5132 | 43 | |
7abb6dea | 44 | #ifndef offsetof |
cf888e70 | 45 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
7abb6dea AM |
46 | #endif |
47 | ||
cf888e70 | 48 | /* Locals variables. */ |
252b5132 | 49 | static struct obstack stat_obstack; |
35835446 | 50 | static struct obstack map_obstack; |
252b5132 RH |
51 | |
52 | #define obstack_chunk_alloc xmalloc | |
53 | #define obstack_chunk_free free | |
5f992e62 | 54 | static const char *startup_file; |
252b5132 | 55 | static lang_statement_list_type input_file_chain; |
b34976b6 | 56 | static bfd_boolean placed_commons = FALSE; |
252b5132 | 57 | static lang_output_section_statement_type *default_common_section; |
b34976b6 | 58 | static bfd_boolean map_option_f; |
252b5132 RH |
59 | static bfd_vma print_dot; |
60 | static lang_input_statement_type *first_file; | |
5f992e62 AM |
61 | static const char *current_target; |
62 | static const char *output_target; | |
252b5132 RH |
63 | static lang_statement_list_type statement_list; |
64 | static struct lang_phdr *lang_phdr_list; | |
420e579c | 65 | static struct bfd_hash_table lang_definedness_table; |
252b5132 | 66 | |
cf888e70 | 67 | /* Forward declarations. */ |
1579bae1 | 68 | static void exp_init_os (etree_type *); |
35835446 | 69 | static void init_map_userdata (bfd *, asection *, void *); |
1579bae1 AM |
70 | static lang_input_statement_type *lookup_name (const char *); |
71 | static bfd_boolean load_symbols (lang_input_statement_type *, | |
72 | lang_statement_list_type *); | |
420e579c HPN |
73 | static struct bfd_hash_entry *lang_definedness_newfunc |
74 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); | |
1579bae1 | 75 | static void insert_undefined (const char *); |
35835446 JR |
76 | static void print_all_symbols (asection *); |
77 | static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *); | |
1579bae1 AM |
78 | static void print_statement (lang_statement_union_type *, |
79 | lang_output_section_statement_type *); | |
80 | static void print_statement_list (lang_statement_union_type *, | |
81 | lang_output_section_statement_type *); | |
82 | static void print_statements (void); | |
83 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); | |
84 | static void lang_record_phdrs (void); | |
85 | static void lang_do_version_exports_section (void); | |
86 | ||
87 | typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *, | |
88 | asection *, lang_input_statement_type *, void *); | |
5f992e62 | 89 | |
cf888e70 | 90 | /* Exported variables. */ |
252b5132 | 91 | lang_output_section_statement_type *abs_output_section; |
aea4bd9d | 92 | lang_statement_list_type lang_output_section_statement; |
252b5132 | 93 | lang_statement_list_type *stat_ptr = &statement_list; |
87f2a346 | 94 | lang_statement_list_type file_chain = { NULL, NULL }; |
e3e942e9 | 95 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
1e281515 | 96 | const char *entry_section = ".text"; |
b34976b6 AM |
97 | bfd_boolean entry_from_cmdline; |
98 | bfd_boolean lang_has_input_file = FALSE; | |
99 | bfd_boolean had_output_filename = FALSE; | |
100 | bfd_boolean lang_float_flag = FALSE; | |
101 | bfd_boolean delete_output_file_on_failure = FALSE; | |
252b5132 | 102 | struct lang_nocrossrefs *nocrossref_list; |
577a0623 | 103 | struct unique_sections *unique_section_list; |
e3f2db7f | 104 | static bfd_boolean ldlang_sysrooted_script = FALSE; |
420e579c | 105 | int lang_statement_iteration = 0; |
252b5132 RH |
106 | |
107 | etree_type *base; /* Relocation base - or null */ | |
108 | ||
61f5d054 L |
109 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
110 | Although backslashes are treated specially if a pattern contains | |
111 | wildcards, we do not consider the mere presence of a backslash to | |
112 | be enough to cause the pattern to be treated as a wildcard. | |
113 | That lets us handle DOS filenames more naturally. */ | |
114 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) | |
115 | ||
d1778b88 | 116 | #define new_stat(x, y) \ |
1579bae1 | 117 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
252b5132 | 118 | |
d1778b88 AM |
119 | #define outside_section_address(q) \ |
120 | ((q)->output_offset + (q)->output_section->vma) | |
252b5132 | 121 | |
d1778b88 AM |
122 | #define outside_symbol_address(q) \ |
123 | ((q)->value + outside_section_address (q->section)) | |
252b5132 RH |
124 | |
125 | #define SECTION_NAME_MAP_LENGTH (16) | |
126 | ||
1579bae1 AM |
127 | void * |
128 | stat_alloc (size_t size) | |
252b5132 RH |
129 | { |
130 | return obstack_alloc (&stat_obstack, size); | |
131 | } | |
132 | ||
b34976b6 | 133 | bfd_boolean |
d0d6a25b | 134 | unique_section_p (const asection *sec) |
577a0623 AM |
135 | { |
136 | struct unique_sections *unam; | |
d0d6a25b | 137 | const char *secnam; |
577a0623 | 138 | |
d0d6a25b AM |
139 | if (link_info.relocatable |
140 | && sec->owner != NULL | |
141 | && bfd_is_group_section (sec->owner, sec)) | |
142 | return TRUE; | |
143 | ||
144 | secnam = sec->name; | |
577a0623 AM |
145 | for (unam = unique_section_list; unam; unam = unam->next) |
146 | if (wildcardp (unam->name) | |
147 | ? fnmatch (unam->name, secnam, 0) == 0 | |
148 | : strcmp (unam->name, secnam) == 0) | |
149 | { | |
b34976b6 | 150 | return TRUE; |
577a0623 AM |
151 | } |
152 | ||
b34976b6 | 153 | return FALSE; |
577a0623 AM |
154 | } |
155 | ||
08da4cac | 156 | /* Generic traversal routines for finding matching sections. */ |
4dec4d4e RH |
157 | |
158 | static void | |
1579bae1 AM |
159 | walk_wild_section (lang_wild_statement_type *ptr, |
160 | lang_input_statement_type *file, | |
161 | callback_t callback, | |
162 | void *data) | |
4dec4d4e | 163 | { |
b6bf44ba AM |
164 | asection *s; |
165 | ||
166 | if (file->just_syms_flag) | |
167 | return; | |
168 | ||
169 | for (s = file->the_bfd->sections; s != NULL; s = s->next) | |
4dec4d4e | 170 | { |
b6bf44ba AM |
171 | struct wildcard_list *sec; |
172 | ||
173 | sec = ptr->section_list; | |
2181f54f AM |
174 | if (sec == NULL) |
175 | (*callback) (ptr, sec, s, file, data); | |
176 | ||
177 | while (sec != NULL) | |
08da4cac | 178 | { |
b34976b6 | 179 | bfd_boolean skip = FALSE; |
2181f54f AM |
180 | struct name_list *list_tmp; |
181 | ||
182 | /* Don't process sections from files which were | |
183 | excluded. */ | |
184 | for (list_tmp = sec->spec.exclude_name_list; | |
185 | list_tmp; | |
186 | list_tmp = list_tmp->next) | |
187 | { | |
188 | if (wildcardp (list_tmp->name)) | |
189 | skip = fnmatch (list_tmp->name, file->filename, 0) == 0; | |
190 | else | |
191 | skip = strcmp (list_tmp->name, file->filename) == 0; | |
4dec4d4e | 192 | |
34efb449 AO |
193 | /* If this file is part of an archive, and the archive is |
194 | excluded, exclude this file. */ | |
195 | if (! skip && file->the_bfd != NULL | |
196 | && file->the_bfd->my_archive != NULL | |
197 | && file->the_bfd->my_archive->filename != NULL) | |
198 | { | |
199 | if (wildcardp (list_tmp->name)) | |
200 | skip = fnmatch (list_tmp->name, | |
201 | file->the_bfd->my_archive->filename, | |
202 | 0) == 0; | |
203 | else | |
204 | skip = strcmp (list_tmp->name, | |
205 | file->the_bfd->my_archive->filename) == 0; | |
206 | } | |
207 | ||
2181f54f AM |
208 | if (skip) |
209 | break; | |
210 | } | |
211 | ||
212 | if (!skip && sec->spec.name != NULL) | |
b6bf44ba | 213 | { |
2181f54f AM |
214 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
215 | ||
216 | if (wildcardp (sec->spec.name)) | |
217 | skip = fnmatch (sec->spec.name, sname, 0) != 0; | |
218 | else | |
219 | skip = strcmp (sec->spec.name, sname) != 0; | |
b6bf44ba | 220 | } |
4dec4d4e | 221 | |
b6bf44ba AM |
222 | if (!skip) |
223 | (*callback) (ptr, sec, s, file, data); | |
4dec4d4e | 224 | |
2181f54f | 225 | sec = sec->next; |
4dec4d4e RH |
226 | } |
227 | } | |
228 | } | |
229 | ||
230 | /* Handle a wild statement for a single file F. */ | |
231 | ||
232 | static void | |
1579bae1 AM |
233 | walk_wild_file (lang_wild_statement_type *s, |
234 | lang_input_statement_type *f, | |
235 | callback_t callback, | |
236 | void *data) | |
4dec4d4e RH |
237 | { |
238 | if (f->the_bfd == NULL | |
239 | || ! bfd_check_format (f->the_bfd, bfd_archive)) | |
b6bf44ba | 240 | walk_wild_section (s, f, callback, data); |
4dec4d4e RH |
241 | else |
242 | { | |
243 | bfd *member; | |
244 | ||
245 | /* This is an archive file. We must map each member of the | |
246 | archive separately. */ | |
1579bae1 | 247 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
4dec4d4e RH |
248 | while (member != NULL) |
249 | { | |
250 | /* When lookup_name is called, it will call the add_symbols | |
251 | entry point for the archive. For each element of the | |
252 | archive which is included, BFD will call ldlang_add_file, | |
253 | which will set the usrdata field of the member to the | |
254 | lang_input_statement. */ | |
255 | if (member->usrdata != NULL) | |
256 | { | |
1579bae1 | 257 | walk_wild_section (s, member->usrdata, callback, data); |
4dec4d4e RH |
258 | } |
259 | ||
260 | member = bfd_openr_next_archived_file (f->the_bfd, member); | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | static void | |
1579bae1 | 266 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
4dec4d4e | 267 | { |
b6bf44ba AM |
268 | const char *file_spec = s->filename; |
269 | ||
270 | if (file_spec == NULL) | |
4dec4d4e RH |
271 | { |
272 | /* Perform the iteration over all files in the list. */ | |
e50d8076 | 273 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 274 | { |
b6bf44ba | 275 | walk_wild_file (s, f, callback, data); |
4dec4d4e RH |
276 | } |
277 | } | |
b6bf44ba | 278 | else if (wildcardp (file_spec)) |
4dec4d4e | 279 | { |
e50d8076 | 280 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
4dec4d4e | 281 | { |
b6bf44ba AM |
282 | if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0) |
283 | walk_wild_file (s, f, callback, data); | |
4dec4d4e RH |
284 | } |
285 | } | |
286 | else | |
287 | { | |
e50d8076 NC |
288 | lang_input_statement_type *f; |
289 | ||
4dec4d4e | 290 | /* Perform the iteration over a single file. */ |
b6bf44ba | 291 | f = lookup_name (file_spec); |
6770ec8c | 292 | if (f) |
b6bf44ba | 293 | walk_wild_file (s, f, callback, data); |
4dec4d4e | 294 | } |
5f992e62 AM |
295 | } |
296 | ||
08da4cac KH |
297 | /* lang_for_each_statement walks the parse tree and calls the provided |
298 | function for each node. */ | |
252b5132 RH |
299 | |
300 | static void | |
1579bae1 AM |
301 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
302 | lang_statement_union_type *s) | |
252b5132 | 303 | { |
1579bae1 | 304 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
305 | { |
306 | func (s); | |
307 | ||
308 | switch (s->header.type) | |
309 | { | |
310 | case lang_constructors_statement_enum: | |
311 | lang_for_each_statement_worker (func, constructor_list.head); | |
312 | break; | |
313 | case lang_output_section_statement_enum: | |
314 | lang_for_each_statement_worker | |
6feb9908 | 315 | (func, s->output_section_statement.children.head); |
252b5132 RH |
316 | break; |
317 | case lang_wild_statement_enum: | |
6feb9908 AM |
318 | lang_for_each_statement_worker (func, |
319 | s->wild_statement.children.head); | |
252b5132 RH |
320 | break; |
321 | case lang_group_statement_enum: | |
322 | lang_for_each_statement_worker (func, | |
323 | s->group_statement.children.head); | |
324 | break; | |
325 | case lang_data_statement_enum: | |
326 | case lang_reloc_statement_enum: | |
327 | case lang_object_symbols_statement_enum: | |
328 | case lang_output_statement_enum: | |
329 | case lang_target_statement_enum: | |
330 | case lang_input_section_enum: | |
331 | case lang_input_statement_enum: | |
332 | case lang_assignment_statement_enum: | |
333 | case lang_padding_statement_enum: | |
334 | case lang_address_statement_enum: | |
335 | case lang_fill_statement_enum: | |
336 | break; | |
337 | default: | |
338 | FAIL (); | |
339 | break; | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
344 | void | |
1579bae1 | 345 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
252b5132 | 346 | { |
08da4cac | 347 | lang_for_each_statement_worker (func, statement_list.head); |
252b5132 RH |
348 | } |
349 | ||
350 | /*----------------------------------------------------------------------*/ | |
08da4cac | 351 | |
252b5132 | 352 | void |
1579bae1 | 353 | lang_list_init (lang_statement_list_type *list) |
252b5132 | 354 | { |
1579bae1 | 355 | list->head = NULL; |
252b5132 RH |
356 | list->tail = &list->head; |
357 | } | |
358 | ||
08da4cac | 359 | /* Build a new statement node for the parse tree. */ |
252b5132 | 360 | |
08da4cac | 361 | static lang_statement_union_type * |
1579bae1 AM |
362 | new_statement (enum statement_enum type, |
363 | size_t size, | |
364 | lang_statement_list_type *list) | |
252b5132 | 365 | { |
1579bae1 | 366 | lang_statement_union_type *new; |
252b5132 | 367 | |
1579bae1 | 368 | new = stat_alloc (size); |
252b5132 | 369 | new->header.type = type; |
1579bae1 | 370 | new->header.next = NULL; |
252b5132 RH |
371 | lang_statement_append (list, new, &new->header.next); |
372 | return new; | |
373 | } | |
374 | ||
08da4cac KH |
375 | /* Build a new input file node for the language. There are several |
376 | ways in which we treat an input file, eg, we only look at symbols, | |
377 | or prefix it with a -l etc. | |
252b5132 | 378 | |
08da4cac | 379 | We can be supplied with requests for input files more than once; |
396a2467 | 380 | they may, for example be split over several lines like foo.o(.text) |
d1778b88 | 381 | foo.o(.data) etc, so when asked for a file we check that we haven't |
08da4cac | 382 | got it already so we don't duplicate the bfd. */ |
252b5132 | 383 | |
252b5132 | 384 | static lang_input_statement_type * |
1579bae1 AM |
385 | new_afile (const char *name, |
386 | lang_input_file_enum_type file_type, | |
387 | const char *target, | |
388 | bfd_boolean add_to_list) | |
252b5132 RH |
389 | { |
390 | lang_input_statement_type *p; | |
391 | ||
392 | if (add_to_list) | |
393 | p = new_stat (lang_input_statement, stat_ptr); | |
394 | else | |
395 | { | |
1579bae1 | 396 | p = stat_alloc (sizeof (lang_input_statement_type)); |
252b5132 RH |
397 | p->header.next = NULL; |
398 | } | |
399 | ||
b34976b6 | 400 | lang_has_input_file = TRUE; |
252b5132 | 401 | p->target = target; |
e3f2db7f | 402 | p->sysrooted = FALSE; |
252b5132 RH |
403 | switch (file_type) |
404 | { | |
405 | case lang_input_file_is_symbols_only_enum: | |
406 | p->filename = name; | |
b34976b6 AM |
407 | p->is_archive = FALSE; |
408 | p->real = TRUE; | |
252b5132 | 409 | p->local_sym_name = name; |
b34976b6 AM |
410 | p->just_syms_flag = TRUE; |
411 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
412 | break; |
413 | case lang_input_file_is_fake_enum: | |
414 | p->filename = name; | |
b34976b6 AM |
415 | p->is_archive = FALSE; |
416 | p->real = FALSE; | |
252b5132 | 417 | p->local_sym_name = name; |
b34976b6 AM |
418 | p->just_syms_flag = FALSE; |
419 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
420 | break; |
421 | case lang_input_file_is_l_enum: | |
b34976b6 | 422 | p->is_archive = TRUE; |
252b5132 | 423 | p->filename = name; |
b34976b6 | 424 | p->real = TRUE; |
1579bae1 | 425 | p->local_sym_name = concat ("-l", name, NULL); |
b34976b6 AM |
426 | p->just_syms_flag = FALSE; |
427 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
428 | break; |
429 | case lang_input_file_is_marker_enum: | |
430 | p->filename = name; | |
b34976b6 AM |
431 | p->is_archive = FALSE; |
432 | p->real = FALSE; | |
252b5132 | 433 | p->local_sym_name = name; |
b34976b6 AM |
434 | p->just_syms_flag = FALSE; |
435 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
436 | break; |
437 | case lang_input_file_is_search_file_enum: | |
e3f2db7f | 438 | p->sysrooted = ldlang_sysrooted_script; |
252b5132 | 439 | p->filename = name; |
b34976b6 AM |
440 | p->is_archive = FALSE; |
441 | p->real = TRUE; | |
252b5132 | 442 | p->local_sym_name = name; |
b34976b6 AM |
443 | p->just_syms_flag = FALSE; |
444 | p->search_dirs_flag = TRUE; | |
252b5132 RH |
445 | break; |
446 | case lang_input_file_is_file_enum: | |
447 | p->filename = name; | |
b34976b6 AM |
448 | p->is_archive = FALSE; |
449 | p->real = TRUE; | |
252b5132 | 450 | p->local_sym_name = name; |
b34976b6 AM |
451 | p->just_syms_flag = FALSE; |
452 | p->search_dirs_flag = FALSE; | |
252b5132 RH |
453 | break; |
454 | default: | |
455 | FAIL (); | |
456 | } | |
1579bae1 AM |
457 | p->the_bfd = NULL; |
458 | p->asymbols = NULL; | |
459 | p->next_real_file = NULL; | |
460 | p->next = NULL; | |
252b5132 RH |
461 | p->symbol_count = 0; |
462 | p->dynamic = config.dynamic_link; | |
e56f61be | 463 | p->add_needed = add_needed; |
4a43e768 | 464 | p->as_needed = as_needed; |
252b5132 | 465 | p->whole_archive = whole_archive; |
b34976b6 | 466 | p->loaded = FALSE; |
252b5132 RH |
467 | lang_statement_append (&input_file_chain, |
468 | (lang_statement_union_type *) p, | |
469 | &p->next_real_file); | |
470 | return p; | |
471 | } | |
472 | ||
473 | lang_input_statement_type * | |
1579bae1 AM |
474 | lang_add_input_file (const char *name, |
475 | lang_input_file_enum_type file_type, | |
476 | const char *target) | |
252b5132 | 477 | { |
b34976b6 AM |
478 | lang_has_input_file = TRUE; |
479 | return new_afile (name, file_type, target, TRUE); | |
252b5132 RH |
480 | } |
481 | ||
08da4cac KH |
482 | /* Build enough state so that the parser can build its tree. */ |
483 | ||
252b5132 | 484 | void |
1579bae1 | 485 | lang_init (void) |
252b5132 RH |
486 | { |
487 | obstack_begin (&stat_obstack, 1000); | |
488 | ||
489 | stat_ptr = &statement_list; | |
490 | ||
491 | lang_list_init (stat_ptr); | |
492 | ||
493 | lang_list_init (&input_file_chain); | |
494 | lang_list_init (&lang_output_section_statement); | |
495 | lang_list_init (&file_chain); | |
1579bae1 AM |
496 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
497 | NULL); | |
08da4cac KH |
498 | abs_output_section = |
499 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); | |
252b5132 RH |
500 | |
501 | abs_output_section->bfd_section = bfd_abs_section_ptr; | |
420e579c HPN |
502 | |
503 | /* The value "3" is ad-hoc, somewhat related to the expected number of | |
504 | DEFINED expressions in a linker script. For most default linker | |
505 | scripts, there are none. Why a hash table then? Well, it's somewhat | |
506 | simpler to re-use working machinery than using a linked list in terms | |
507 | of code-complexity here in ld, besides the initialization which just | |
508 | looks like other code here. */ | |
688c58f3 AM |
509 | if (!bfd_hash_table_init_n (&lang_definedness_table, |
510 | lang_definedness_newfunc, 3)) | |
420e579c HPN |
511 | einfo (_("%P%F: out of memory during initialization")); |
512 | ||
513 | /* Callers of exp_fold_tree need to increment this. */ | |
514 | lang_statement_iteration = 0; | |
252b5132 RH |
515 | } |
516 | ||
517 | /*---------------------------------------------------------------------- | |
08da4cac KH |
518 | A region is an area of memory declared with the |
519 | MEMORY { name:org=exp, len=exp ... } | |
520 | syntax. | |
252b5132 | 521 | |
08da4cac | 522 | We maintain a list of all the regions here. |
252b5132 | 523 | |
08da4cac | 524 | If no regions are specified in the script, then the default is used |
a747ee4d NC |
525 | which is created when looked up to be the entire data space. |
526 | ||
527 | If create is true we are creating a region inside a MEMORY block. | |
528 | In this case it is probably an error to create a region that has | |
529 | already been created. If we are not inside a MEMORY block it is | |
530 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) | |
531 | and so we issue a warning. */ | |
252b5132 RH |
532 | |
533 | static lang_memory_region_type *lang_memory_region_list; | |
6feb9908 AM |
534 | static lang_memory_region_type **lang_memory_region_list_tail |
535 | = &lang_memory_region_list; | |
252b5132 RH |
536 | |
537 | lang_memory_region_type * | |
a747ee4d | 538 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
252b5132 RH |
539 | { |
540 | lang_memory_region_type *p; | |
1579bae1 | 541 | lang_memory_region_type *new; |
252b5132 | 542 | |
9f88b410 RS |
543 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
544 | if (name == NULL) | |
545 | return NULL; | |
546 | ||
1579bae1 | 547 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
a747ee4d NC |
548 | if (strcmp (p->name, name) == 0) |
549 | { | |
550 | if (create) | |
6feb9908 AM |
551 | einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), |
552 | name); | |
1579bae1 | 553 | return p; |
a747ee4d | 554 | } |
252b5132 RH |
555 | |
556 | #if 0 | |
557 | /* This code used to always use the first region in the list as the | |
558 | default region. I changed it to instead use a region | |
559 | encompassing all of memory as the default region. This permits | |
560 | NOLOAD sections to work reasonably without requiring a region. | |
561 | People should specify what region they mean, if they really want | |
562 | a region. */ | |
a747ee4d | 563 | if (strcmp (name, DEFAULT_MEMORY_REGION) == 0) |
252b5132 | 564 | { |
1579bae1 AM |
565 | if (lang_memory_region_list != NULL) |
566 | return lang_memory_region_list; | |
252b5132 RH |
567 | } |
568 | #endif | |
569 | ||
a747ee4d NC |
570 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
571 | einfo (_("%P:%S: warning: memory region %s not declared\n"), name); | |
572 | ||
1579bae1 | 573 | new = stat_alloc (sizeof (lang_memory_region_type)); |
252b5132 | 574 | |
1579bae1 AM |
575 | new->name = xstrdup (name); |
576 | new->next = NULL; | |
252b5132 | 577 | |
1579bae1 AM |
578 | *lang_memory_region_list_tail = new; |
579 | lang_memory_region_list_tail = &new->next; | |
580 | new->origin = 0; | |
581 | new->flags = 0; | |
582 | new->not_flags = 0; | |
583 | new->length = ~(bfd_size_type) 0; | |
584 | new->current = 0; | |
585 | new->had_full_message = FALSE; | |
252b5132 | 586 | |
1579bae1 | 587 | return new; |
252b5132 RH |
588 | } |
589 | ||
5f992e62 | 590 | static lang_memory_region_type * |
1579bae1 | 591 | lang_memory_default (asection *section) |
252b5132 RH |
592 | { |
593 | lang_memory_region_type *p; | |
594 | ||
595 | flagword sec_flags = section->flags; | |
596 | ||
597 | /* Override SEC_DATA to mean a writable section. */ | |
598 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) | |
599 | sec_flags |= SEC_DATA; | |
600 | ||
1579bae1 | 601 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
602 | { |
603 | if ((p->flags & sec_flags) != 0 | |
604 | && (p->not_flags & sec_flags) == 0) | |
605 | { | |
606 | return p; | |
607 | } | |
608 | } | |
a747ee4d | 609 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
252b5132 RH |
610 | } |
611 | ||
0841712e JJ |
612 | static lang_output_section_statement_type * |
613 | lang_output_section_find_1 (const char *const name, int constraint) | |
252b5132 RH |
614 | { |
615 | lang_statement_union_type *u; | |
616 | lang_output_section_statement_type *lookup; | |
617 | ||
1579bae1 | 618 | for (u = lang_output_section_statement.head; u != NULL; u = lookup->next) |
252b5132 RH |
619 | { |
620 | lookup = &u->output_section_statement; | |
9f61903d | 621 | |
0841712e JJ |
622 | if (strcmp (name, lookup->name) == 0 |
623 | && lookup->constraint != -1 | |
624 | && (constraint == 0 || constraint == lookup->constraint)) | |
1579bae1 | 625 | return lookup; |
252b5132 | 626 | } |
1579bae1 | 627 | return NULL; |
252b5132 RH |
628 | } |
629 | ||
630 | lang_output_section_statement_type * | |
0841712e JJ |
631 | lang_output_section_find (const char *const name) |
632 | { | |
633 | return lang_output_section_find_1 (name, 0); | |
634 | } | |
635 | ||
636 | static lang_output_section_statement_type * | |
637 | lang_output_section_statement_lookup_1 (const char *const name, int constraint) | |
252b5132 RH |
638 | { |
639 | lang_output_section_statement_type *lookup; | |
640 | ||
0841712e | 641 | lookup = lang_output_section_find_1 (name, constraint); |
1579bae1 | 642 | if (lookup == NULL) |
252b5132 | 643 | { |
1579bae1 AM |
644 | lookup = new_stat (lang_output_section_statement, stat_ptr); |
645 | lookup->region = NULL; | |
646 | lookup->lma_region = NULL; | |
647 | lookup->fill = 0; | |
252b5132 RH |
648 | lookup->block_value = 1; |
649 | lookup->name = name; | |
650 | ||
1579bae1 AM |
651 | lookup->next = NULL; |
652 | lookup->bfd_section = NULL; | |
1b493742 | 653 | lookup->processed = 0; |
0841712e | 654 | lookup->constraint = constraint; |
252b5132 | 655 | lookup->sectype = normal_section; |
1579bae1 | 656 | lookup->addr_tree = NULL; |
252b5132 RH |
657 | lang_list_init (&lookup->children); |
658 | ||
1579bae1 | 659 | lookup->memspec = NULL; |
252b5132 RH |
660 | lookup->flags = 0; |
661 | lookup->subsection_alignment = -1; | |
662 | lookup->section_alignment = -1; | |
1579bae1 | 663 | lookup->load_base = NULL; |
9f88b410 | 664 | lookup->update_dot_tree = NULL; |
252b5132 RH |
665 | lookup->phdrs = NULL; |
666 | ||
667 | lang_statement_append (&lang_output_section_statement, | |
668 | (lang_statement_union_type *) lookup, | |
669 | &lookup->next); | |
670 | } | |
671 | return lookup; | |
672 | } | |
673 | ||
0841712e JJ |
674 | lang_output_section_statement_type * |
675 | lang_output_section_statement_lookup (const char *const name) | |
676 | { | |
677 | return lang_output_section_statement_lookup_1 (name, 0); | |
678 | } | |
679 | ||
252b5132 | 680 | static void |
1579bae1 | 681 | lang_map_flags (flagword flag) |
252b5132 RH |
682 | { |
683 | if (flag & SEC_ALLOC) | |
684 | minfo ("a"); | |
685 | ||
686 | if (flag & SEC_CODE) | |
687 | minfo ("x"); | |
688 | ||
689 | if (flag & SEC_READONLY) | |
690 | minfo ("r"); | |
691 | ||
692 | if (flag & SEC_DATA) | |
693 | minfo ("w"); | |
694 | ||
695 | if (flag & SEC_LOAD) | |
696 | minfo ("l"); | |
697 | } | |
698 | ||
699 | void | |
1579bae1 | 700 | lang_map (void) |
252b5132 RH |
701 | { |
702 | lang_memory_region_type *m; | |
35835446 | 703 | bfd *p; |
252b5132 RH |
704 | |
705 | minfo (_("\nMemory Configuration\n\n")); | |
706 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", | |
707 | _("Name"), _("Origin"), _("Length"), _("Attributes")); | |
708 | ||
1579bae1 | 709 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
252b5132 RH |
710 | { |
711 | char buf[100]; | |
712 | int len; | |
713 | ||
714 | fprintf (config.map_file, "%-16s ", m->name); | |
715 | ||
716 | sprintf_vma (buf, m->origin); | |
717 | minfo ("0x%s ", buf); | |
718 | len = strlen (buf); | |
719 | while (len < 16) | |
720 | { | |
721 | print_space (); | |
722 | ++len; | |
723 | } | |
724 | ||
725 | minfo ("0x%V", m->length); | |
726 | if (m->flags || m->not_flags) | |
727 | { | |
728 | #ifndef BFD64 | |
729 | minfo (" "); | |
730 | #endif | |
731 | if (m->flags) | |
732 | { | |
733 | print_space (); | |
734 | lang_map_flags (m->flags); | |
735 | } | |
736 | ||
737 | if (m->not_flags) | |
738 | { | |
739 | minfo (" !"); | |
740 | lang_map_flags (m->not_flags); | |
741 | } | |
742 | } | |
743 | ||
744 | print_nl (); | |
745 | } | |
746 | ||
747 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); | |
748 | ||
35835446 JR |
749 | if (! command_line.reduce_memory_overheads) |
750 | { | |
751 | obstack_begin (&map_obstack, 1000); | |
752 | for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next) | |
753 | bfd_map_over_sections (p, init_map_userdata, 0); | |
754 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); | |
755 | } | |
252b5132 RH |
756 | print_statements (); |
757 | } | |
758 | ||
35835446 JR |
759 | static void |
760 | init_map_userdata (abfd, sec, data) | |
761 | bfd *abfd ATTRIBUTE_UNUSED; | |
762 | asection *sec; | |
763 | void *data ATTRIBUTE_UNUSED; | |
764 | { | |
765 | fat_section_userdata_type *new_data | |
766 | = ((fat_section_userdata_type *) (stat_alloc | |
767 | (sizeof (fat_section_userdata_type)))); | |
768 | ||
769 | ASSERT (get_userdata (sec) == NULL); | |
770 | get_userdata (sec) = new_data; | |
771 | new_data->map_symbol_def_tail = &new_data->map_symbol_def_head; | |
772 | } | |
773 | ||
774 | static bfd_boolean | |
775 | sort_def_symbol (hash_entry, info) | |
776 | struct bfd_link_hash_entry *hash_entry; | |
777 | void *info ATTRIBUTE_UNUSED; | |
778 | { | |
779 | if (hash_entry->type == bfd_link_hash_defined | |
780 | || hash_entry->type == bfd_link_hash_defweak) | |
781 | { | |
782 | struct fat_user_section_struct *ud; | |
783 | struct map_symbol_def *def; | |
784 | ||
785 | ud = get_userdata (hash_entry->u.def.section); | |
786 | if (! ud) | |
787 | { | |
788 | /* ??? What do we have to do to initialize this beforehand? */ | |
789 | /* The first time we get here is bfd_abs_section... */ | |
790 | init_map_userdata (0, hash_entry->u.def.section, 0); | |
791 | ud = get_userdata (hash_entry->u.def.section); | |
792 | } | |
793 | else if (!ud->map_symbol_def_tail) | |
794 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; | |
76d7af2d | 795 | |
6feb9908 | 796 | def = obstack_alloc (&map_obstack, sizeof *def); |
35835446 | 797 | def->entry = hash_entry; |
76d7af2d | 798 | *(ud->map_symbol_def_tail) = def; |
35835446 JR |
799 | ud->map_symbol_def_tail = &def->next; |
800 | } | |
801 | return TRUE; | |
802 | } | |
803 | ||
252b5132 RH |
804 | /* Initialize an output section. */ |
805 | ||
806 | static void | |
1579bae1 | 807 | init_os (lang_output_section_statement_type *s) |
252b5132 | 808 | { |
35835446 | 809 | lean_section_userdata_type *new; |
252b5132 RH |
810 | |
811 | if (s->bfd_section != NULL) | |
812 | return; | |
813 | ||
814 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) | |
6f9efd97 | 815 | einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
252b5132 | 816 | |
35835446 | 817 | new = stat_alloc (SECTION_USERDATA_SIZE); |
76d7af2d | 818 | memset (new, 0, SECTION_USERDATA_SIZE); |
252b5132 RH |
819 | |
820 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); | |
1579bae1 | 821 | if (s->bfd_section == NULL) |
252b5132 | 822 | s->bfd_section = bfd_make_section (output_bfd, s->name); |
1579bae1 | 823 | if (s->bfd_section == NULL) |
252b5132 RH |
824 | { |
825 | einfo (_("%P%F: output format %s cannot represent section called %s\n"), | |
826 | output_bfd->xvec->name, s->name); | |
827 | } | |
828 | s->bfd_section->output_section = s->bfd_section; | |
829 | ||
08da4cac KH |
830 | /* We initialize an output sections output offset to minus its own |
831 | vma to allow us to output a section through itself. */ | |
252b5132 | 832 | s->bfd_section->output_offset = 0; |
1579bae1 | 833 | get_userdata (s->bfd_section) = new; |
252b5132 RH |
834 | |
835 | /* If there is a base address, make sure that any sections it might | |
836 | mention are initialized. */ | |
837 | if (s->addr_tree != NULL) | |
838 | exp_init_os (s->addr_tree); | |
18794b0c AM |
839 | |
840 | if (s->load_base != NULL) | |
841 | exp_init_os (s->load_base); | |
252b5132 RH |
842 | } |
843 | ||
844 | /* Make sure that all output sections mentioned in an expression are | |
845 | initialized. */ | |
846 | ||
847 | static void | |
1579bae1 | 848 | exp_init_os (etree_type *exp) |
252b5132 RH |
849 | { |
850 | switch (exp->type.node_class) | |
851 | { | |
852 | case etree_assign: | |
853 | exp_init_os (exp->assign.src); | |
854 | break; | |
855 | ||
856 | case etree_binary: | |
857 | exp_init_os (exp->binary.lhs); | |
858 | exp_init_os (exp->binary.rhs); | |
859 | break; | |
860 | ||
861 | case etree_trinary: | |
862 | exp_init_os (exp->trinary.cond); | |
863 | exp_init_os (exp->trinary.lhs); | |
864 | exp_init_os (exp->trinary.rhs); | |
865 | break; | |
866 | ||
b6ca8815 NS |
867 | case etree_assert: |
868 | exp_init_os (exp->assert_s.child); | |
869 | break; | |
870 | ||
252b5132 RH |
871 | case etree_unary: |
872 | exp_init_os (exp->unary.child); | |
873 | break; | |
874 | ||
875 | case etree_name: | |
876 | switch (exp->type.node_code) | |
877 | { | |
878 | case ADDR: | |
879 | case LOADADDR: | |
880 | case SIZEOF: | |
881 | { | |
882 | lang_output_section_statement_type *os; | |
883 | ||
884 | os = lang_output_section_find (exp->name.name); | |
885 | if (os != NULL && os->bfd_section == NULL) | |
886 | init_os (os); | |
887 | } | |
888 | } | |
889 | break; | |
890 | ||
891 | default: | |
892 | break; | |
893 | } | |
894 | } | |
9503fd87 | 895 | \f |
252b5132 | 896 | static void |
1579bae1 | 897 | section_already_linked (bfd *abfd, asection *sec, void *data) |
252b5132 | 898 | { |
1579bae1 | 899 | lang_input_statement_type *entry = data; |
252b5132 RH |
900 | |
901 | /* If we are only reading symbols from this object, then we want to | |
902 | discard all sections. */ | |
903 | if (entry->just_syms_flag) | |
904 | { | |
c2c01aa7 | 905 | bfd_link_just_syms (sec, &link_info); |
252b5132 RH |
906 | return; |
907 | } | |
908 | ||
082b7297 | 909 | bfd_section_already_linked (abfd, sec); |
252b5132 RH |
910 | } |
911 | \f | |
912 | /* The wild routines. | |
913 | ||
914 | These expand statements like *(.text) and foo.o to a list of | |
915 | explicit actions, like foo.o(.text), bar.o(.text) and | |
916 | foo.o(.text, .data). */ | |
917 | ||
252b5132 RH |
918 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
919 | lang_input_section statement which is placed at PTR. FILE is the | |
920 | input file which holds SECTION. */ | |
921 | ||
922 | void | |
1579bae1 AM |
923 | lang_add_section (lang_statement_list_type *ptr, |
924 | asection *section, | |
925 | lang_output_section_statement_type *output, | |
926 | lang_input_statement_type *file) | |
252b5132 | 927 | { |
164e712d | 928 | flagword flags = section->flags; |
b34976b6 | 929 | bfd_boolean discard; |
252b5132 | 930 | |
e49f5022 | 931 | /* Discard sections marked with SEC_EXCLUDE. */ |
b3096250 | 932 | discard = (flags & SEC_EXCLUDE) != 0; |
252b5132 RH |
933 | |
934 | /* Discard input sections which are assigned to a section named | |
935 | DISCARD_SECTION_NAME. */ | |
936 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) | |
b34976b6 | 937 | discard = TRUE; |
252b5132 RH |
938 | |
939 | /* Discard debugging sections if we are stripping debugging | |
940 | information. */ | |
941 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) | |
942 | && (flags & SEC_DEBUGGING) != 0) | |
b34976b6 | 943 | discard = TRUE; |
252b5132 RH |
944 | |
945 | if (discard) | |
946 | { | |
947 | if (section->output_section == NULL) | |
948 | { | |
949 | /* This prevents future calls from assigning this section. */ | |
950 | section->output_section = bfd_abs_section_ptr; | |
951 | } | |
952 | return; | |
953 | } | |
954 | ||
955 | if (section->output_section == NULL) | |
956 | { | |
b34976b6 | 957 | bfd_boolean first; |
252b5132 RH |
958 | lang_input_section_type *new; |
959 | flagword flags; | |
960 | ||
961 | if (output->bfd_section == NULL) | |
d1778b88 AM |
962 | init_os (output); |
963 | ||
964 | first = ! output->bfd_section->linker_has_input; | |
965 | output->bfd_section->linker_has_input = 1; | |
252b5132 | 966 | |
08da4cac | 967 | /* Add a section reference to the list. */ |
252b5132 RH |
968 | new = new_stat (lang_input_section, ptr); |
969 | ||
970 | new->section = section; | |
971 | new->ifile = file; | |
972 | section->output_section = output->bfd_section; | |
973 | ||
974 | flags = section->flags; | |
975 | ||
976 | /* We don't copy the SEC_NEVER_LOAD flag from an input section | |
977 | to an output section, because we want to be able to include a | |
978 | SEC_NEVER_LOAD section in the middle of an otherwise loaded | |
979 | section (I don't know why we want to do this, but we do). | |
980 | build_link_order in ldwrite.c handles this case by turning | |
981 | the embedded SEC_NEVER_LOAD section into a fill. */ | |
982 | ||
983 | flags &= ~ SEC_NEVER_LOAD; | |
984 | ||
985 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've | |
986 | already been processed. One reason to do this is that on pe | |
987 | format targets, .text$foo sections go into .text and it's odd | |
988 | to see .text with SEC_LINK_ONCE set. */ | |
989 | ||
1049f94e | 990 | if (! link_info.relocatable) |
252b5132 RH |
991 | flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES); |
992 | ||
993 | /* If this is not the first input section, and the SEC_READONLY | |
994 | flag is not currently set, then don't set it just because the | |
995 | input section has it set. */ | |
996 | ||
997 | if (! first && (section->output_section->flags & SEC_READONLY) == 0) | |
998 | flags &= ~ SEC_READONLY; | |
999 | ||
f5fa8ca2 JJ |
1000 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
1001 | if (! first | |
1002 | && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS)) | |
1003 | != (flags & (SEC_MERGE | SEC_STRINGS)) | |
1004 | || ((flags & SEC_MERGE) | |
1005 | && section->output_section->entsize != section->entsize))) | |
1006 | { | |
1007 | section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
1008 | flags &= ~ (SEC_MERGE | SEC_STRINGS); | |
1009 | } | |
1010 | ||
252b5132 RH |
1011 | section->output_section->flags |= flags; |
1012 | ||
f5fa8ca2 JJ |
1013 | if (flags & SEC_MERGE) |
1014 | section->output_section->entsize = section->entsize; | |
1015 | ||
252b5132 RH |
1016 | /* If SEC_READONLY is not set in the input section, then clear |
1017 | it from the output section. */ | |
1018 | if ((section->flags & SEC_READONLY) == 0) | |
1019 | section->output_section->flags &= ~SEC_READONLY; | |
1020 | ||
1021 | switch (output->sectype) | |
1022 | { | |
1023 | case normal_section: | |
1024 | break; | |
1025 | case dsect_section: | |
1026 | case copy_section: | |
1027 | case info_section: | |
1028 | case overlay_section: | |
1029 | output->bfd_section->flags &= ~SEC_ALLOC; | |
1030 | break; | |
1031 | case noload_section: | |
1032 | output->bfd_section->flags &= ~SEC_LOAD; | |
1033 | output->bfd_section->flags |= SEC_NEVER_LOAD; | |
1034 | break; | |
1035 | } | |
1036 | ||
667f5177 ILT |
1037 | /* Copy over SEC_SMALL_DATA. */ |
1038 | if (section->flags & SEC_SMALL_DATA) | |
1039 | section->output_section->flags |= SEC_SMALL_DATA; | |
9e41f973 | 1040 | |
252b5132 RH |
1041 | if (section->alignment_power > output->bfd_section->alignment_power) |
1042 | output->bfd_section->alignment_power = section->alignment_power; | |
1043 | ||
396a2467 | 1044 | /* If supplied an alignment, then force it. */ |
252b5132 RH |
1045 | if (output->section_alignment != -1) |
1046 | output->bfd_section->alignment_power = output->section_alignment; | |
74459f0e TW |
1047 | |
1048 | if (section->flags & SEC_BLOCK) | |
08da4cac KH |
1049 | { |
1050 | section->output_section->flags |= SEC_BLOCK; | |
1051 | /* FIXME: This value should really be obtained from the bfd... */ | |
1052 | output->block_value = 128; | |
1053 | } | |
252b5132 RH |
1054 | } |
1055 | } | |
1056 | ||
1057 | /* Handle wildcard sorting. This returns the lang_input_section which | |
1058 | should follow the one we are going to create for SECTION and FILE, | |
1059 | based on the sorting requirements of WILD. It returns NULL if the | |
1060 | new section should just go at the end of the current list. */ | |
1061 | ||
1062 | static lang_statement_union_type * | |
1579bae1 AM |
1063 | wild_sort (lang_wild_statement_type *wild, |
1064 | struct wildcard_list *sec, | |
1065 | lang_input_statement_type *file, | |
1066 | asection *section) | |
252b5132 RH |
1067 | { |
1068 | const char *section_name; | |
1069 | lang_statement_union_type *l; | |
1070 | ||
b6bf44ba | 1071 | if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted)) |
252b5132 RH |
1072 | return NULL; |
1073 | ||
1074 | section_name = bfd_get_section_name (file->the_bfd, section); | |
bba1a0c0 | 1075 | for (l = wild->children.head; l != NULL; l = l->header.next) |
252b5132 RH |
1076 | { |
1077 | lang_input_section_type *ls; | |
1078 | ||
1079 | if (l->header.type != lang_input_section_enum) | |
1080 | continue; | |
1081 | ls = &l->input_section; | |
1082 | ||
1083 | /* Sorting by filename takes precedence over sorting by section | |
1084 | name. */ | |
1085 | ||
1086 | if (wild->filenames_sorted) | |
1087 | { | |
1088 | const char *fn, *ln; | |
b34976b6 | 1089 | bfd_boolean fa, la; |
252b5132 RH |
1090 | int i; |
1091 | ||
1092 | /* The PE support for the .idata section as generated by | |
1093 | dlltool assumes that files will be sorted by the name of | |
1094 | the archive and then the name of the file within the | |
1095 | archive. */ | |
1096 | ||
1097 | if (file->the_bfd != NULL | |
1098 | && bfd_my_archive (file->the_bfd) != NULL) | |
1099 | { | |
1100 | fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); | |
b34976b6 | 1101 | fa = TRUE; |
252b5132 RH |
1102 | } |
1103 | else | |
1104 | { | |
1105 | fn = file->filename; | |
b34976b6 | 1106 | fa = FALSE; |
252b5132 RH |
1107 | } |
1108 | ||
1109 | if (ls->ifile->the_bfd != NULL | |
1110 | && bfd_my_archive (ls->ifile->the_bfd) != NULL) | |
1111 | { | |
1112 | ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd)); | |
b34976b6 | 1113 | la = TRUE; |
252b5132 RH |
1114 | } |
1115 | else | |
1116 | { | |
1117 | ln = ls->ifile->filename; | |
b34976b6 | 1118 | la = FALSE; |
252b5132 RH |
1119 | } |
1120 | ||
1121 | i = strcmp (fn, ln); | |
1122 | if (i > 0) | |
1123 | continue; | |
1124 | else if (i < 0) | |
1125 | break; | |
1126 | ||
1127 | if (fa || la) | |
1128 | { | |
1129 | if (fa) | |
1130 | fn = file->filename; | |
1131 | if (la) | |
1132 | ln = ls->ifile->filename; | |
1133 | ||
1134 | i = strcmp (fn, ln); | |
1135 | if (i > 0) | |
1136 | continue; | |
1137 | else if (i < 0) | |
1138 | break; | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | /* Here either the files are not sorted by name, or we are | |
1143 | looking at the sections for this file. */ | |
1144 | ||
b6bf44ba | 1145 | if (sec != NULL && sec->spec.sorted) |
252b5132 RH |
1146 | { |
1147 | if (strcmp (section_name, | |
1148 | bfd_get_section_name (ls->ifile->the_bfd, | |
1149 | ls->section)) | |
1150 | < 0) | |
1151 | break; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | return l; | |
1156 | } | |
1157 | ||
1158 | /* Expand a wild statement for a particular FILE. SECTION may be | |
1159 | NULL, in which case it is a wild card. */ | |
1160 | ||
1161 | static void | |
1579bae1 AM |
1162 | output_section_callback (lang_wild_statement_type *ptr, |
1163 | struct wildcard_list *sec, | |
1164 | asection *section, | |
1165 | lang_input_statement_type *file, | |
1166 | void *output) | |
4dec4d4e RH |
1167 | { |
1168 | lang_statement_union_type *before; | |
5f992e62 | 1169 | |
b6bf44ba | 1170 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
d0d6a25b | 1171 | if (unique_section_p (section)) |
b6bf44ba AM |
1172 | return; |
1173 | ||
b6bf44ba | 1174 | before = wild_sort (ptr, sec, file, section); |
5f992e62 | 1175 | |
4dec4d4e RH |
1176 | /* Here BEFORE points to the lang_input_section which |
1177 | should follow the one we are about to add. If BEFORE | |
1178 | is NULL, then the section should just go at the end | |
1179 | of the current list. */ | |
5f992e62 | 1180 | |
4dec4d4e | 1181 | if (before == NULL) |
39dcfe18 AM |
1182 | lang_add_section (&ptr->children, section, |
1183 | (lang_output_section_statement_type *) output, | |
1184 | file); | |
4dec4d4e | 1185 | else |
252b5132 | 1186 | { |
4dec4d4e RH |
1187 | lang_statement_list_type list; |
1188 | lang_statement_union_type **pp; | |
5f992e62 | 1189 | |
4dec4d4e | 1190 | lang_list_init (&list); |
39dcfe18 AM |
1191 | lang_add_section (&list, section, |
1192 | (lang_output_section_statement_type *) output, | |
1193 | file); | |
5f992e62 | 1194 | |
4dec4d4e RH |
1195 | /* If we are discarding the section, LIST.HEAD will |
1196 | be NULL. */ | |
1197 | if (list.head != NULL) | |
252b5132 | 1198 | { |
bba1a0c0 | 1199 | ASSERT (list.head->header.next == NULL); |
5f992e62 | 1200 | |
4dec4d4e RH |
1201 | for (pp = &ptr->children.head; |
1202 | *pp != before; | |
bba1a0c0 | 1203 | pp = &(*pp)->header.next) |
4dec4d4e | 1204 | ASSERT (*pp != NULL); |
5f992e62 | 1205 | |
bba1a0c0 | 1206 | list.head->header.next = *pp; |
4dec4d4e | 1207 | *pp = list.head; |
252b5132 RH |
1208 | } |
1209 | } | |
1210 | } | |
1211 | ||
0841712e JJ |
1212 | /* Check if all sections in a wild statement for a particular FILE |
1213 | are readonly. */ | |
1214 | ||
1215 | static void | |
1216 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, | |
1217 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
1218 | asection *section, | |
1219 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
6feb9908 | 1220 | void *data) |
0841712e JJ |
1221 | { |
1222 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ | |
1223 | if (unique_section_p (section)) | |
1224 | return; | |
1225 | ||
6feb9908 AM |
1226 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
1227 | ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE; | |
0841712e JJ |
1228 | } |
1229 | ||
252b5132 RH |
1230 | /* This is passed a file name which must have been seen already and |
1231 | added to the statement tree. We will see if it has been opened | |
1232 | already and had its symbols read. If not then we'll read it. */ | |
1233 | ||
1234 | static lang_input_statement_type * | |
1579bae1 | 1235 | lookup_name (const char *name) |
252b5132 RH |
1236 | { |
1237 | lang_input_statement_type *search; | |
1238 | ||
1239 | for (search = (lang_input_statement_type *) input_file_chain.head; | |
1579bae1 | 1240 | search != NULL; |
252b5132 RH |
1241 | search = (lang_input_statement_type *) search->next_real_file) |
1242 | { | |
0013291d NC |
1243 | /* Use the local_sym_name as the name of the file that has |
1244 | already been loaded as filename might have been transformed | |
1245 | via the search directory lookup mechanism. */ | |
1246 | const char * filename = search->local_sym_name; | |
1247 | ||
1248 | if (filename == NULL && name == NULL) | |
252b5132 | 1249 | return search; |
0013291d | 1250 | if (filename != NULL |
1579bae1 | 1251 | && name != NULL |
0013291d | 1252 | && strcmp (filename, name) == 0) |
252b5132 RH |
1253 | break; |
1254 | } | |
1255 | ||
1579bae1 | 1256 | if (search == NULL) |
6feb9908 AM |
1257 | search = new_afile (name, lang_input_file_is_search_file_enum, |
1258 | default_target, FALSE); | |
252b5132 RH |
1259 | |
1260 | /* If we have already added this file, or this file is not real | |
1261 | (FIXME: can that ever actually happen?) or the name is NULL | |
1262 | (FIXME: can that ever actually happen?) don't add this file. */ | |
1263 | if (search->loaded | |
1264 | || ! search->real | |
1579bae1 | 1265 | || search->filename == NULL) |
252b5132 RH |
1266 | return search; |
1267 | ||
1579bae1 | 1268 | if (! load_symbols (search, NULL)) |
6770ec8c | 1269 | return NULL; |
252b5132 RH |
1270 | |
1271 | return search; | |
1272 | } | |
1273 | ||
1274 | /* Get the symbols for an input file. */ | |
1275 | ||
b34976b6 | 1276 | static bfd_boolean |
1579bae1 AM |
1277 | load_symbols (lang_input_statement_type *entry, |
1278 | lang_statement_list_type *place) | |
252b5132 RH |
1279 | { |
1280 | char **matching; | |
1281 | ||
1282 | if (entry->loaded) | |
b34976b6 | 1283 | return TRUE; |
252b5132 RH |
1284 | |
1285 | ldfile_open_file (entry); | |
1286 | ||
1287 | if (! bfd_check_format (entry->the_bfd, bfd_archive) | |
1288 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) | |
1289 | { | |
1290 | bfd_error_type err; | |
1291 | lang_statement_list_type *hold; | |
b34976b6 | 1292 | bfd_boolean bad_load = TRUE; |
e3f2db7f | 1293 | bfd_boolean save_ldlang_sysrooted_script; |
b7a26f91 | 1294 | |
252b5132 | 1295 | err = bfd_get_error (); |
884fb58e NC |
1296 | |
1297 | /* See if the emulation has some special knowledge. */ | |
1298 | if (ldemul_unrecognized_file (entry)) | |
b34976b6 | 1299 | return TRUE; |
884fb58e | 1300 | |
252b5132 RH |
1301 | if (err == bfd_error_file_ambiguously_recognized) |
1302 | { | |
1303 | char **p; | |
1304 | ||
1305 | einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); | |
1306 | einfo (_("%B: matching formats:"), entry->the_bfd); | |
1307 | for (p = matching; *p != NULL; p++) | |
1308 | einfo (" %s", *p); | |
1309 | einfo ("%F\n"); | |
1310 | } | |
1311 | else if (err != bfd_error_file_not_recognized | |
1312 | || place == NULL) | |
6770ec8c NC |
1313 | einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); |
1314 | else | |
b34976b6 | 1315 | bad_load = FALSE; |
b7a26f91 | 1316 | |
252b5132 RH |
1317 | bfd_close (entry->the_bfd); |
1318 | entry->the_bfd = NULL; | |
1319 | ||
252b5132 | 1320 | /* Try to interpret the file as a linker script. */ |
252b5132 RH |
1321 | ldfile_open_command_file (entry->filename); |
1322 | ||
1323 | hold = stat_ptr; | |
1324 | stat_ptr = place; | |
e3f2db7f AO |
1325 | save_ldlang_sysrooted_script = ldlang_sysrooted_script; |
1326 | ldlang_sysrooted_script = entry->sysrooted; | |
252b5132 | 1327 | |
b34976b6 | 1328 | ldfile_assumed_script = TRUE; |
252b5132 RH |
1329 | parser_input = input_script; |
1330 | yyparse (); | |
b34976b6 | 1331 | ldfile_assumed_script = FALSE; |
252b5132 | 1332 | |
e3f2db7f | 1333 | ldlang_sysrooted_script = save_ldlang_sysrooted_script; |
252b5132 RH |
1334 | stat_ptr = hold; |
1335 | ||
6770ec8c | 1336 | return ! bad_load; |
252b5132 RH |
1337 | } |
1338 | ||
1339 | if (ldemul_recognized_file (entry)) | |
b34976b6 | 1340 | return TRUE; |
252b5132 RH |
1341 | |
1342 | /* We don't call ldlang_add_file for an archive. Instead, the | |
1343 | add_symbols entry point will call ldlang_add_file, via the | |
1344 | add_archive_element callback, for each element of the archive | |
1345 | which is used. */ | |
1346 | switch (bfd_get_format (entry->the_bfd)) | |
1347 | { | |
1348 | default: | |
1349 | break; | |
1350 | ||
1351 | case bfd_object: | |
1352 | ldlang_add_file (entry); | |
1353 | if (trace_files || trace_file_tries) | |
1354 | info_msg ("%I\n", entry); | |
1355 | break; | |
1356 | ||
1357 | case bfd_archive: | |
1358 | if (entry->whole_archive) | |
1359 | { | |
b7a26f91 | 1360 | bfd *member = NULL; |
b34976b6 | 1361 | bfd_boolean loaded = TRUE; |
6770ec8c NC |
1362 | |
1363 | for (;;) | |
252b5132 | 1364 | { |
6770ec8c NC |
1365 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
1366 | ||
1367 | if (member == NULL) | |
1368 | break; | |
b7a26f91 | 1369 | |
252b5132 | 1370 | if (! bfd_check_format (member, bfd_object)) |
6770ec8c NC |
1371 | { |
1372 | einfo (_("%F%B: member %B in archive is not an object\n"), | |
1373 | entry->the_bfd, member); | |
b34976b6 | 1374 | loaded = FALSE; |
6770ec8c NC |
1375 | } |
1376 | ||
252b5132 RH |
1377 | if (! ((*link_info.callbacks->add_archive_element) |
1378 | (&link_info, member, "--whole-archive"))) | |
1379 | abort (); | |
6770ec8c | 1380 | |
252b5132 | 1381 | if (! bfd_link_add_symbols (member, &link_info)) |
6770ec8c NC |
1382 | { |
1383 | einfo (_("%F%B: could not read symbols: %E\n"), member); | |
b34976b6 | 1384 | loaded = FALSE; |
6770ec8c | 1385 | } |
252b5132 RH |
1386 | } |
1387 | ||
6770ec8c NC |
1388 | entry->loaded = loaded; |
1389 | return loaded; | |
252b5132 | 1390 | } |
6770ec8c | 1391 | break; |
252b5132 RH |
1392 | } |
1393 | ||
03bdc404 | 1394 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
b34976b6 | 1395 | entry->loaded = TRUE; |
6770ec8c | 1396 | else |
252b5132 RH |
1397 | einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd); |
1398 | ||
6770ec8c | 1399 | return entry->loaded; |
252b5132 RH |
1400 | } |
1401 | ||
b6bf44ba AM |
1402 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
1403 | may be NULL, indicating that it is a wildcard. Separate | |
1404 | lang_input_section statements are created for each part of the | |
1405 | expansion; they are added after the wild statement S. OUTPUT is | |
1406 | the output section. */ | |
252b5132 RH |
1407 | |
1408 | static void | |
1579bae1 AM |
1409 | wild (lang_wild_statement_type *s, |
1410 | const char *target ATTRIBUTE_UNUSED, | |
1411 | lang_output_section_statement_type *output) | |
252b5132 | 1412 | { |
b6bf44ba | 1413 | struct wildcard_list *sec; |
252b5132 | 1414 | |
1579bae1 | 1415 | walk_wild (s, output_section_callback, output); |
b6bf44ba AM |
1416 | |
1417 | for (sec = s->section_list; sec != NULL; sec = sec->next) | |
252b5132 | 1418 | { |
b6bf44ba AM |
1419 | if (default_common_section != NULL) |
1420 | break; | |
1421 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) | |
1422 | { | |
1423 | /* Remember the section that common is going to in case we | |
b7a26f91 | 1424 | later get something which doesn't know where to put it. */ |
b6bf44ba AM |
1425 | default_common_section = output; |
1426 | } | |
252b5132 RH |
1427 | } |
1428 | } | |
1429 | ||
b34976b6 | 1430 | /* Return TRUE iff target is the sought target. */ |
08da4cac | 1431 | |
e50d8076 | 1432 | static int |
1579bae1 | 1433 | get_target (const bfd_target *target, void *data) |
e50d8076 | 1434 | { |
1579bae1 | 1435 | const char *sought = data; |
5f992e62 | 1436 | |
e50d8076 NC |
1437 | return strcmp (target->name, sought) == 0; |
1438 | } | |
1439 | ||
1440 | /* Like strcpy() but convert to lower case as well. */ | |
08da4cac | 1441 | |
e50d8076 | 1442 | static void |
1579bae1 | 1443 | stricpy (char *dest, char *src) |
e50d8076 NC |
1444 | { |
1445 | char c; | |
5f992e62 | 1446 | |
08da4cac | 1447 | while ((c = *src++) != 0) |
3882b010 | 1448 | *dest++ = TOLOWER (c); |
e50d8076 | 1449 | |
08da4cac | 1450 | *dest = 0; |
e50d8076 NC |
1451 | } |
1452 | ||
396a2467 | 1453 | /* Remove the first occurrence of needle (if any) in haystack |
e50d8076 | 1454 | from haystack. */ |
08da4cac | 1455 | |
e50d8076 | 1456 | static void |
1579bae1 | 1457 | strcut (char *haystack, char *needle) |
e50d8076 NC |
1458 | { |
1459 | haystack = strstr (haystack, needle); | |
5f992e62 | 1460 | |
e50d8076 NC |
1461 | if (haystack) |
1462 | { | |
08da4cac | 1463 | char *src; |
e50d8076 | 1464 | |
08da4cac KH |
1465 | for (src = haystack + strlen (needle); *src;) |
1466 | *haystack++ = *src++; | |
5f992e62 | 1467 | |
08da4cac | 1468 | *haystack = 0; |
e50d8076 NC |
1469 | } |
1470 | } | |
1471 | ||
1472 | /* Compare two target format name strings. | |
1473 | Return a value indicating how "similar" they are. */ | |
08da4cac | 1474 | |
e50d8076 | 1475 | static int |
1579bae1 | 1476 | name_compare (char *first, char *second) |
e50d8076 | 1477 | { |
08da4cac KH |
1478 | char *copy1; |
1479 | char *copy2; | |
1480 | int result; | |
5f992e62 | 1481 | |
e50d8076 NC |
1482 | copy1 = xmalloc (strlen (first) + 1); |
1483 | copy2 = xmalloc (strlen (second) + 1); | |
1484 | ||
1485 | /* Convert the names to lower case. */ | |
1486 | stricpy (copy1, first); | |
1487 | stricpy (copy2, second); | |
1488 | ||
1579bae1 | 1489 | /* Remove size and endian strings from the name. */ |
e50d8076 NC |
1490 | strcut (copy1, "big"); |
1491 | strcut (copy1, "little"); | |
1492 | strcut (copy2, "big"); | |
1493 | strcut (copy2, "little"); | |
1494 | ||
1495 | /* Return a value based on how many characters match, | |
1496 | starting from the beginning. If both strings are | |
1497 | the same then return 10 * their length. */ | |
08da4cac KH |
1498 | for (result = 0; copy1[result] == copy2[result]; result++) |
1499 | if (copy1[result] == 0) | |
e50d8076 NC |
1500 | { |
1501 | result *= 10; | |
1502 | break; | |
1503 | } | |
5f992e62 | 1504 | |
e50d8076 NC |
1505 | free (copy1); |
1506 | free (copy2); | |
1507 | ||
1508 | return result; | |
1509 | } | |
1510 | ||
1511 | /* Set by closest_target_match() below. */ | |
08da4cac | 1512 | static const bfd_target *winner; |
e50d8076 NC |
1513 | |
1514 | /* Scan all the valid bfd targets looking for one that has the endianness | |
1515 | requirement that was specified on the command line, and is the nearest | |
1516 | match to the original output target. */ | |
08da4cac | 1517 | |
e50d8076 | 1518 | static int |
1579bae1 | 1519 | closest_target_match (const bfd_target *target, void *data) |
e50d8076 | 1520 | { |
1579bae1 | 1521 | const bfd_target *original = data; |
5f992e62 | 1522 | |
08da4cac KH |
1523 | if (command_line.endian == ENDIAN_BIG |
1524 | && target->byteorder != BFD_ENDIAN_BIG) | |
e50d8076 | 1525 | return 0; |
5f992e62 | 1526 | |
08da4cac KH |
1527 | if (command_line.endian == ENDIAN_LITTLE |
1528 | && target->byteorder != BFD_ENDIAN_LITTLE) | |
e50d8076 NC |
1529 | return 0; |
1530 | ||
1531 | /* Must be the same flavour. */ | |
1532 | if (target->flavour != original->flavour) | |
1533 | return 0; | |
1534 | ||
1535 | /* If we have not found a potential winner yet, then record this one. */ | |
1536 | if (winner == NULL) | |
1537 | { | |
1538 | winner = target; | |
1539 | return 0; | |
1540 | } | |
1541 | ||
1542 | /* Oh dear, we now have two potential candidates for a successful match. | |
4de2d33d | 1543 | Compare their names and choose the better one. */ |
d1778b88 AM |
1544 | if (name_compare (target->name, original->name) |
1545 | > name_compare (winner->name, original->name)) | |
e50d8076 NC |
1546 | winner = target; |
1547 | ||
1548 | /* Keep on searching until wqe have checked them all. */ | |
1549 | return 0; | |
1550 | } | |
1551 | ||
1552 | /* Return the BFD target format of the first input file. */ | |
08da4cac | 1553 | |
e50d8076 | 1554 | static char * |
1579bae1 | 1555 | get_first_input_target (void) |
e50d8076 | 1556 | { |
08da4cac | 1557 | char *target = NULL; |
e50d8076 NC |
1558 | |
1559 | LANG_FOR_EACH_INPUT_STATEMENT (s) | |
1560 | { | |
1561 | if (s->header.type == lang_input_statement_enum | |
1562 | && s->real) | |
1563 | { | |
1564 | ldfile_open_file (s); | |
5f992e62 | 1565 | |
e50d8076 NC |
1566 | if (s->the_bfd != NULL |
1567 | && bfd_check_format (s->the_bfd, bfd_object)) | |
1568 | { | |
1569 | target = bfd_get_target (s->the_bfd); | |
5f992e62 | 1570 | |
e50d8076 NC |
1571 | if (target != NULL) |
1572 | break; | |
1573 | } | |
1574 | } | |
1575 | } | |
5f992e62 | 1576 | |
e50d8076 NC |
1577 | return target; |
1578 | } | |
1579 | ||
599917b8 | 1580 | const char * |
1579bae1 | 1581 | lang_get_output_target (void) |
599917b8 JJ |
1582 | { |
1583 | const char *target; | |
1584 | ||
1585 | /* Has the user told us which output format to use? */ | |
1579bae1 | 1586 | if (output_target != NULL) |
599917b8 JJ |
1587 | return output_target; |
1588 | ||
1589 | /* No - has the current target been set to something other than | |
1590 | the default? */ | |
1591 | if (current_target != default_target) | |
1592 | return current_target; | |
1593 | ||
1594 | /* No - can we determine the format of the first input file? */ | |
1595 | target = get_first_input_target (); | |
1596 | if (target != NULL) | |
1597 | return target; | |
1598 | ||
1599 | /* Failed - use the default output target. */ | |
1600 | return default_target; | |
1601 | } | |
1602 | ||
252b5132 RH |
1603 | /* Open the output file. */ |
1604 | ||
1605 | static bfd * | |
1579bae1 | 1606 | open_output (const char *name) |
252b5132 | 1607 | { |
08da4cac | 1608 | bfd *output; |
252b5132 | 1609 | |
599917b8 | 1610 | output_target = lang_get_output_target (); |
5f992e62 | 1611 | |
08da4cac KH |
1612 | /* Has the user requested a particular endianness on the command |
1613 | line? */ | |
e50d8076 NC |
1614 | if (command_line.endian != ENDIAN_UNSET) |
1615 | { | |
08da4cac | 1616 | const bfd_target *target; |
1b69a0bf | 1617 | enum bfd_endian desired_endian; |
e50d8076 NC |
1618 | |
1619 | /* Get the chosen target. */ | |
1579bae1 | 1620 | target = bfd_search_for_target (get_target, (void *) output_target); |
e50d8076 | 1621 | |
c13b1b77 NC |
1622 | /* If the target is not supported, we cannot do anything. */ |
1623 | if (target != NULL) | |
e50d8076 | 1624 | { |
c13b1b77 NC |
1625 | if (command_line.endian == ENDIAN_BIG) |
1626 | desired_endian = BFD_ENDIAN_BIG; | |
e50d8076 | 1627 | else |
c13b1b77 | 1628 | desired_endian = BFD_ENDIAN_LITTLE; |
5f992e62 AM |
1629 | |
1630 | /* See if the target has the wrong endianness. This should | |
1631 | not happen if the linker script has provided big and | |
1632 | little endian alternatives, but some scrips don't do | |
1633 | this. */ | |
c13b1b77 | 1634 | if (target->byteorder != desired_endian) |
e50d8076 | 1635 | { |
c13b1b77 NC |
1636 | /* If it does, then see if the target provides |
1637 | an alternative with the correct endianness. */ | |
1638 | if (target->alternative_target != NULL | |
1639 | && (target->alternative_target->byteorder == desired_endian)) | |
1640 | output_target = target->alternative_target->name; | |
e50d8076 | 1641 | else |
c13b1b77 | 1642 | { |
5f992e62 AM |
1643 | /* Try to find a target as similar as possible to |
1644 | the default target, but which has the desired | |
1645 | endian characteristic. */ | |
1579bae1 AM |
1646 | bfd_search_for_target (closest_target_match, |
1647 | (void *) target); | |
5f992e62 AM |
1648 | |
1649 | /* Oh dear - we could not find any targets that | |
1650 | satisfy our requirements. */ | |
c13b1b77 | 1651 | if (winner == NULL) |
6feb9908 AM |
1652 | einfo (_("%P: warning: could not find any targets" |
1653 | " that match endianness requirement\n")); | |
c13b1b77 NC |
1654 | else |
1655 | output_target = winner->name; | |
1656 | } | |
e50d8076 NC |
1657 | } |
1658 | } | |
252b5132 | 1659 | } |
5f992e62 | 1660 | |
252b5132 RH |
1661 | output = bfd_openw (name, output_target); |
1662 | ||
1579bae1 | 1663 | if (output == NULL) |
252b5132 RH |
1664 | { |
1665 | if (bfd_get_error () == bfd_error_invalid_target) | |
e50d8076 NC |
1666 | einfo (_("%P%F: target %s not found\n"), output_target); |
1667 | ||
252b5132 RH |
1668 | einfo (_("%P%F: cannot open output file %s: %E\n"), name); |
1669 | } | |
1670 | ||
b34976b6 | 1671 | delete_output_file_on_failure = TRUE; |
252b5132 | 1672 | |
08da4cac KH |
1673 | #if 0 |
1674 | output->flags |= D_PAGED; | |
1675 | #endif | |
252b5132 RH |
1676 | |
1677 | if (! bfd_set_format (output, bfd_object)) | |
1678 | einfo (_("%P%F:%s: can not make object file: %E\n"), name); | |
1679 | if (! bfd_set_arch_mach (output, | |
1680 | ldfile_output_architecture, | |
1681 | ldfile_output_machine)) | |
1682 | einfo (_("%P%F:%s: can not set architecture: %E\n"), name); | |
1683 | ||
1684 | link_info.hash = bfd_link_hash_table_create (output); | |
1579bae1 | 1685 | if (link_info.hash == NULL) |
252b5132 RH |
1686 | einfo (_("%P%F: can not create link hash table: %E\n")); |
1687 | ||
1688 | bfd_set_gp_size (output, g_switch_value); | |
1689 | return output; | |
1690 | } | |
1691 | ||
252b5132 | 1692 | static void |
1579bae1 | 1693 | ldlang_open_output (lang_statement_union_type *statement) |
252b5132 RH |
1694 | { |
1695 | switch (statement->header.type) | |
1696 | { | |
1697 | case lang_output_statement_enum: | |
1579bae1 | 1698 | ASSERT (output_bfd == NULL); |
252b5132 RH |
1699 | output_bfd = open_output (statement->output_statement.name); |
1700 | ldemul_set_output_arch (); | |
1049f94e | 1701 | if (config.magic_demand_paged && !link_info.relocatable) |
252b5132 RH |
1702 | output_bfd->flags |= D_PAGED; |
1703 | else | |
1704 | output_bfd->flags &= ~D_PAGED; | |
1705 | if (config.text_read_only) | |
1706 | output_bfd->flags |= WP_TEXT; | |
1707 | else | |
1708 | output_bfd->flags &= ~WP_TEXT; | |
1709 | if (link_info.traditional_format) | |
1710 | output_bfd->flags |= BFD_TRADITIONAL_FORMAT; | |
1711 | else | |
1712 | output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; | |
1713 | break; | |
1714 | ||
1715 | case lang_target_statement_enum: | |
1716 | current_target = statement->target_statement.target; | |
1717 | break; | |
1718 | default: | |
1719 | break; | |
1720 | } | |
1721 | } | |
1722 | ||
e5caa5e0 AM |
1723 | /* Convert between addresses in bytes and sizes in octets. |
1724 | For currently supported targets, octets_per_byte is always a power | |
1725 | of two, so we can use shifts. */ | |
1726 | #define TO_ADDR(X) ((X) >> opb_shift) | |
1727 | #define TO_SIZE(X) ((X) << opb_shift) | |
1728 | ||
1729 | /* Support the above. */ | |
1730 | static unsigned int opb_shift = 0; | |
1731 | ||
1732 | static void | |
1733 | init_opb (void) | |
1734 | { | |
1735 | unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, | |
1736 | ldfile_output_machine); | |
1737 | opb_shift = 0; | |
1738 | if (x > 1) | |
1739 | while ((x & 1) == 0) | |
1740 | { | |
1741 | x >>= 1; | |
1742 | ++opb_shift; | |
1743 | } | |
1744 | ASSERT (x == 1); | |
1745 | } | |
1746 | ||
252b5132 RH |
1747 | /* Open all the input files. */ |
1748 | ||
1749 | static void | |
1579bae1 | 1750 | open_input_bfds (lang_statement_union_type *s, bfd_boolean force) |
252b5132 | 1751 | { |
1579bae1 | 1752 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
1753 | { |
1754 | switch (s->header.type) | |
1755 | { | |
1756 | case lang_constructors_statement_enum: | |
1757 | open_input_bfds (constructor_list.head, force); | |
1758 | break; | |
1759 | case lang_output_section_statement_enum: | |
1760 | open_input_bfds (s->output_section_statement.children.head, force); | |
1761 | break; | |
1762 | case lang_wild_statement_enum: | |
08da4cac | 1763 | /* Maybe we should load the file's symbols. */ |
252b5132 RH |
1764 | if (s->wild_statement.filename |
1765 | && ! wildcardp (s->wild_statement.filename)) | |
4a43e768 | 1766 | lookup_name (s->wild_statement.filename); |
252b5132 RH |
1767 | open_input_bfds (s->wild_statement.children.head, force); |
1768 | break; | |
1769 | case lang_group_statement_enum: | |
1770 | { | |
1771 | struct bfd_link_hash_entry *undefs; | |
1772 | ||
1773 | /* We must continually search the entries in the group | |
08da4cac KH |
1774 | until no new symbols are added to the list of undefined |
1775 | symbols. */ | |
252b5132 RH |
1776 | |
1777 | do | |
1778 | { | |
1779 | undefs = link_info.hash->undefs_tail; | |
b34976b6 | 1780 | open_input_bfds (s->group_statement.children.head, TRUE); |
252b5132 RH |
1781 | } |
1782 | while (undefs != link_info.hash->undefs_tail); | |
1783 | } | |
1784 | break; | |
1785 | case lang_target_statement_enum: | |
1786 | current_target = s->target_statement.target; | |
1787 | break; | |
1788 | case lang_input_statement_enum: | |
e50d8076 | 1789 | if (s->input_statement.real) |
252b5132 RH |
1790 | { |
1791 | lang_statement_list_type add; | |
1792 | ||
1793 | s->input_statement.target = current_target; | |
1794 | ||
1795 | /* If we are being called from within a group, and this | |
1796 | is an archive which has already been searched, then | |
cd4c806a L |
1797 | force it to be researched unless the whole archive |
1798 | has been loaded already. */ | |
252b5132 | 1799 | if (force |
cd4c806a | 1800 | && !s->input_statement.whole_archive |
252b5132 RH |
1801 | && s->input_statement.loaded |
1802 | && bfd_check_format (s->input_statement.the_bfd, | |
1803 | bfd_archive)) | |
b34976b6 | 1804 | s->input_statement.loaded = FALSE; |
252b5132 | 1805 | |
d1778b88 | 1806 | lang_list_init (&add); |
1276aefa | 1807 | |
6770ec8c | 1808 | if (! load_symbols (&s->input_statement, &add)) |
b34976b6 | 1809 | config.make_executable = FALSE; |
252b5132 RH |
1810 | |
1811 | if (add.head != NULL) | |
1812 | { | |
bba1a0c0 AM |
1813 | *add.tail = s->header.next; |
1814 | s->header.next = add.head; | |
252b5132 RH |
1815 | } |
1816 | } | |
1817 | break; | |
1818 | default: | |
1819 | break; | |
1820 | } | |
1821 | } | |
1822 | } | |
1823 | ||
08da4cac KH |
1824 | /* If there are [COMMONS] statements, put a wild one into the bss |
1825 | section. */ | |
252b5132 RH |
1826 | |
1827 | static void | |
1579bae1 | 1828 | lang_reasonable_defaults (void) |
252b5132 RH |
1829 | { |
1830 | #if 0 | |
1831 | lang_output_section_statement_lookup (".text"); | |
1832 | lang_output_section_statement_lookup (".data"); | |
1833 | ||
08da4cac | 1834 | default_common_section = lang_output_section_statement_lookup (".bss"); |
252b5132 | 1835 | |
7c519c12 | 1836 | if (!placed_commons) |
252b5132 RH |
1837 | { |
1838 | lang_wild_statement_type *new = | |
1839 | new_stat (lang_wild_statement, | |
1840 | &default_common_section->children); | |
1841 | ||
1842 | new->section_name = "COMMON"; | |
1579bae1 | 1843 | new->filename = NULL; |
252b5132 RH |
1844 | lang_list_init (&new->children); |
1845 | } | |
1846 | #endif | |
252b5132 RH |
1847 | } |
1848 | ||
420e579c HPN |
1849 | /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */ |
1850 | ||
1851 | void | |
1852 | lang_track_definedness (const char *name) | |
1853 | { | |
1854 | if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL) | |
1855 | einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name); | |
1856 | } | |
1857 | ||
1858 | /* New-function for the definedness hash table. */ | |
1859 | ||
1860 | static struct bfd_hash_entry * | |
1861 | lang_definedness_newfunc (struct bfd_hash_entry *entry, | |
1862 | struct bfd_hash_table *table ATTRIBUTE_UNUSED, | |
1863 | const char *name ATTRIBUTE_UNUSED) | |
1864 | { | |
1865 | struct lang_definedness_hash_entry *ret | |
1866 | = (struct lang_definedness_hash_entry *) entry; | |
1867 | ||
1868 | if (ret == NULL) | |
1869 | ret = (struct lang_definedness_hash_entry *) | |
1870 | bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry)); | |
1871 | ||
1872 | if (ret == NULL) | |
1873 | einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name); | |
1874 | ||
1875 | ret->iteration = -1; | |
1876 | return &ret->root; | |
1877 | } | |
1878 | ||
1879 | /* Return the iteration when the definition of NAME was last updated. A | |
1880 | value of -1 means that the symbol is not defined in the linker script | |
1881 | or the command line, but may be defined in the linker symbol table. */ | |
1882 | ||
1883 | int | |
1884 | lang_symbol_definition_iteration (const char *name) | |
1885 | { | |
1886 | struct lang_definedness_hash_entry *defentry | |
1887 | = (struct lang_definedness_hash_entry *) | |
1888 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
1889 | ||
1890 | /* We've already created this one on the presence of DEFINED in the | |
1891 | script, so it can't be NULL unless something is borked elsewhere in | |
1892 | the code. */ | |
1893 | if (defentry == NULL) | |
1894 | FAIL (); | |
1895 | ||
1896 | return defentry->iteration; | |
1897 | } | |
1898 | ||
1899 | /* Update the definedness state of NAME. */ | |
1900 | ||
1901 | void | |
1902 | lang_update_definedness (const char *name, struct bfd_link_hash_entry *h) | |
1903 | { | |
1904 | struct lang_definedness_hash_entry *defentry | |
1905 | = (struct lang_definedness_hash_entry *) | |
1906 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); | |
1907 | ||
1908 | /* We don't keep track of symbols not tested with DEFINED. */ | |
1909 | if (defentry == NULL) | |
1910 | return; | |
1911 | ||
1912 | /* If the symbol was already defined, and not from an earlier statement | |
1913 | iteration, don't update the definedness iteration, because that'd | |
1914 | make the symbol seem defined in the linker script at this point, and | |
1915 | it wasn't; it was defined in some object. If we do anyway, DEFINED | |
1916 | would start to yield false before this point and the construct "sym = | |
1917 | DEFINED (sym) ? sym : X;" would change sym to X despite being defined | |
1918 | in an object. */ | |
1919 | if (h->type != bfd_link_hash_undefined | |
1920 | && h->type != bfd_link_hash_common | |
1921 | && h->type != bfd_link_hash_new | |
1922 | && defentry->iteration == -1) | |
1923 | return; | |
1924 | ||
1925 | defentry->iteration = lang_statement_iteration; | |
1926 | } | |
1927 | ||
08da4cac | 1928 | /* Add the supplied name to the symbol table as an undefined reference. |
fcf0e35b AM |
1929 | This is a two step process as the symbol table doesn't even exist at |
1930 | the time the ld command line is processed. First we put the name | |
1931 | on a list, then, once the output file has been opened, transfer the | |
1932 | name to the symbol table. */ | |
1933 | ||
e3e942e9 | 1934 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
252b5132 | 1935 | |
e3e942e9 | 1936 | #define ldlang_undef_chain_list_head entry_symbol.next |
252b5132 RH |
1937 | |
1938 | void | |
1579bae1 | 1939 | ldlang_add_undef (const char *const name) |
252b5132 RH |
1940 | { |
1941 | ldlang_undef_chain_list_type *new = | |
1579bae1 | 1942 | stat_alloc (sizeof (ldlang_undef_chain_list_type)); |
252b5132 RH |
1943 | |
1944 | new->next = ldlang_undef_chain_list_head; | |
1945 | ldlang_undef_chain_list_head = new; | |
1946 | ||
d1b2b2dc | 1947 | new->name = xstrdup (name); |
fcf0e35b AM |
1948 | |
1949 | if (output_bfd != NULL) | |
1950 | insert_undefined (new->name); | |
1951 | } | |
1952 | ||
1953 | /* Insert NAME as undefined in the symbol table. */ | |
1954 | ||
1955 | static void | |
1579bae1 | 1956 | insert_undefined (const char *name) |
fcf0e35b AM |
1957 | { |
1958 | struct bfd_link_hash_entry *h; | |
1959 | ||
b34976b6 | 1960 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
1579bae1 | 1961 | if (h == NULL) |
fcf0e35b AM |
1962 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
1963 | if (h->type == bfd_link_hash_new) | |
1964 | { | |
1965 | h->type = bfd_link_hash_undefined; | |
1966 | h->u.undef.abfd = NULL; | |
1967 | bfd_link_add_undef (link_info.hash, h); | |
1968 | } | |
252b5132 RH |
1969 | } |
1970 | ||
1971 | /* Run through the list of undefineds created above and place them | |
1972 | into the linker hash table as undefined symbols belonging to the | |
08da4cac KH |
1973 | script file. */ |
1974 | ||
252b5132 | 1975 | static void |
1579bae1 | 1976 | lang_place_undefineds (void) |
252b5132 RH |
1977 | { |
1978 | ldlang_undef_chain_list_type *ptr; | |
1979 | ||
1579bae1 AM |
1980 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
1981 | insert_undefined (ptr->name); | |
252b5132 RH |
1982 | } |
1983 | ||
0841712e JJ |
1984 | /* Check for all readonly or some readwrite sections. */ |
1985 | ||
1986 | static void | |
6feb9908 AM |
1987 | check_input_sections |
1988 | (lang_statement_union_type *s, | |
1989 | lang_output_section_statement_type *output_section_statement) | |
0841712e JJ |
1990 | { |
1991 | for (; s != (lang_statement_union_type *) NULL; s = s->header.next) | |
1992 | { | |
1993 | switch (s->header.type) | |
1994 | { | |
1995 | case lang_wild_statement_enum: | |
1996 | walk_wild (&s->wild_statement, check_section_callback, | |
1997 | output_section_statement); | |
1998 | if (! output_section_statement->all_input_readonly) | |
1999 | return; | |
2000 | break; | |
2001 | case lang_constructors_statement_enum: | |
2002 | check_input_sections (constructor_list.head, | |
2003 | output_section_statement); | |
2004 | if (! output_section_statement->all_input_readonly) | |
2005 | return; | |
2006 | break; | |
2007 | case lang_group_statement_enum: | |
2008 | check_input_sections (s->group_statement.children.head, | |
2009 | output_section_statement); | |
2010 | if (! output_section_statement->all_input_readonly) | |
2011 | return; | |
2012 | break; | |
2013 | default: | |
2014 | break; | |
2015 | } | |
2016 | } | |
2017 | } | |
2018 | ||
396a2467 | 2019 | /* Open input files and attach to output sections. */ |
08da4cac | 2020 | |
252b5132 | 2021 | static void |
1579bae1 AM |
2022 | map_input_to_output_sections |
2023 | (lang_statement_union_type *s, const char *target, | |
2024 | lang_output_section_statement_type *output_section_statement) | |
252b5132 | 2025 | { |
1579bae1 | 2026 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
2027 | { |
2028 | switch (s->header.type) | |
2029 | { | |
252b5132 | 2030 | case lang_wild_statement_enum: |
b6bf44ba | 2031 | wild (&s->wild_statement, target, output_section_statement); |
abc6ab0a | 2032 | break; |
252b5132 RH |
2033 | case lang_constructors_statement_enum: |
2034 | map_input_to_output_sections (constructor_list.head, | |
2035 | target, | |
2036 | output_section_statement); | |
2037 | break; | |
2038 | case lang_output_section_statement_enum: | |
0841712e JJ |
2039 | if (s->output_section_statement.constraint) |
2040 | { | |
2041 | if (s->output_section_statement.constraint == -1) | |
2042 | break; | |
2043 | s->output_section_statement.all_input_readonly = TRUE; | |
2044 | check_input_sections (s->output_section_statement.children.head, | |
2045 | &s->output_section_statement); | |
2046 | if ((s->output_section_statement.all_input_readonly | |
2047 | && s->output_section_statement.constraint == ONLY_IF_RW) | |
2048 | || (!s->output_section_statement.all_input_readonly | |
2049 | && s->output_section_statement.constraint == ONLY_IF_RO)) | |
2050 | { | |
2051 | s->output_section_statement.constraint = -1; | |
2052 | break; | |
2053 | } | |
2054 | } | |
2055 | ||
252b5132 RH |
2056 | map_input_to_output_sections (s->output_section_statement.children.head, |
2057 | target, | |
2058 | &s->output_section_statement); | |
2059 | break; | |
2060 | case lang_output_statement_enum: | |
2061 | break; | |
2062 | case lang_target_statement_enum: | |
2063 | target = s->target_statement.target; | |
2064 | break; | |
2065 | case lang_group_statement_enum: | |
2066 | map_input_to_output_sections (s->group_statement.children.head, | |
2067 | target, | |
2068 | output_section_statement); | |
2069 | break; | |
384d938f NS |
2070 | case lang_data_statement_enum: |
2071 | /* Make sure that any sections mentioned in the expression | |
2072 | are initialized. */ | |
2073 | exp_init_os (s->data_statement.exp); | |
2074 | /* FALLTHROUGH */ | |
252b5132 RH |
2075 | case lang_fill_statement_enum: |
2076 | case lang_input_section_enum: | |
2077 | case lang_object_symbols_statement_enum: | |
252b5132 RH |
2078 | case lang_reloc_statement_enum: |
2079 | case lang_padding_statement_enum: | |
2080 | case lang_input_statement_enum: | |
2081 | if (output_section_statement != NULL | |
2082 | && output_section_statement->bfd_section == NULL) | |
2083 | init_os (output_section_statement); | |
2084 | break; | |
2085 | case lang_assignment_statement_enum: | |
2086 | if (output_section_statement != NULL | |
2087 | && output_section_statement->bfd_section == NULL) | |
2088 | init_os (output_section_statement); | |
2089 | ||
2090 | /* Make sure that any sections mentioned in the assignment | |
08da4cac | 2091 | are initialized. */ |
252b5132 RH |
2092 | exp_init_os (s->assignment_statement.exp); |
2093 | break; | |
2094 | case lang_afile_asection_pair_statement_enum: | |
2095 | FAIL (); | |
2096 | break; | |
2097 | case lang_address_statement_enum: | |
08da4cac | 2098 | /* Mark the specified section with the supplied address. */ |
252b5132 RH |
2099 | { |
2100 | lang_output_section_statement_type *os = | |
2101 | lang_output_section_statement_lookup | |
2102 | (s->address_statement.section_name); | |
2103 | ||
2104 | if (os->bfd_section == NULL) | |
2105 | init_os (os); | |
2106 | os->addr_tree = s->address_statement.address; | |
2107 | } | |
2108 | break; | |
2109 | } | |
2110 | } | |
2111 | } | |
2112 | ||
4bd5a393 AM |
2113 | /* An output section might have been removed after its statement was |
2114 | added. For example, ldemul_before_allocation can remove dynamic | |
2115 | sections if they turn out to be not needed. Clean them up here. */ | |
2116 | ||
2117 | static void | |
1579bae1 | 2118 | strip_excluded_output_sections (void) |
4bd5a393 AM |
2119 | { |
2120 | lang_statement_union_type *u; | |
2121 | ||
2122 | for (u = lang_output_section_statement.head; | |
2123 | u != NULL; | |
2124 | u = u->output_section_statement.next) | |
2125 | { | |
2126 | lang_output_section_statement_type *os; | |
2127 | asection *s; | |
2128 | ||
2129 | os = &u->output_section_statement; | |
0841712e JJ |
2130 | if (os->constraint == -1) |
2131 | continue; | |
4bd5a393 AM |
2132 | s = os->bfd_section; |
2133 | if (s != NULL && (s->flags & SEC_EXCLUDE) != 0) | |
2134 | { | |
2135 | asection **p; | |
2136 | ||
2137 | os->bfd_section = NULL; | |
2138 | ||
2139 | for (p = &output_bfd->sections; *p; p = &(*p)->next) | |
2140 | if (*p == s) | |
2141 | { | |
2142 | bfd_section_list_remove (output_bfd, p); | |
2143 | output_bfd->section_count--; | |
2144 | break; | |
2145 | } | |
2146 | } | |
2147 | } | |
2148 | } | |
2149 | ||
252b5132 | 2150 | static void |
1579bae1 AM |
2151 | print_output_section_statement |
2152 | (lang_output_section_statement_type *output_section_statement) | |
252b5132 RH |
2153 | { |
2154 | asection *section = output_section_statement->bfd_section; | |
2155 | int len; | |
2156 | ||
2157 | if (output_section_statement != abs_output_section) | |
2158 | { | |
2159 | minfo ("\n%s", output_section_statement->name); | |
2160 | ||
2161 | if (section != NULL) | |
2162 | { | |
2163 | print_dot = section->vma; | |
2164 | ||
2165 | len = strlen (output_section_statement->name); | |
2166 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
2167 | { | |
2168 | print_nl (); | |
2169 | len = 0; | |
2170 | } | |
2171 | while (len < SECTION_NAME_MAP_LENGTH) | |
2172 | { | |
2173 | print_space (); | |
2174 | ++len; | |
2175 | } | |
2176 | ||
eea6121a | 2177 | minfo ("0x%V %W", section->vma, section->size); |
252b5132 RH |
2178 | |
2179 | if (output_section_statement->load_base != NULL) | |
2180 | { | |
2181 | bfd_vma addr; | |
2182 | ||
2183 | addr = exp_get_abs_int (output_section_statement->load_base, 0, | |
2184 | "load base", lang_final_phase_enum); | |
2185 | minfo (_(" load address 0x%V"), addr); | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | print_nl (); | |
2190 | } | |
2191 | ||
2192 | print_statement_list (output_section_statement->children.head, | |
2193 | output_section_statement); | |
2194 | } | |
2195 | ||
2196 | static void | |
1579bae1 AM |
2197 | print_assignment (lang_assignment_statement_type *assignment, |
2198 | lang_output_section_statement_type *output_section) | |
252b5132 RH |
2199 | { |
2200 | int i; | |
2201 | etree_value_type result; | |
2202 | ||
2203 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
2204 | print_space (); | |
2205 | ||
2206 | result = exp_fold_tree (assignment->exp->assign.src, output_section, | |
2207 | lang_final_phase_enum, print_dot, &print_dot); | |
2208 | if (result.valid_p) | |
7b17f854 RS |
2209 | { |
2210 | const char *dst; | |
2211 | bfd_vma value; | |
2212 | ||
2213 | value = result.value + result.section->bfd_section->vma; | |
2214 | dst = assignment->exp->assign.dst; | |
2215 | ||
2216 | minfo ("0x%V", value); | |
2217 | if (dst[0] == '.' && dst[1] == 0) | |
2218 | print_dot = value; | |
2219 | } | |
252b5132 RH |
2220 | else |
2221 | { | |
2222 | minfo ("*undef* "); | |
2223 | #ifdef BFD64 | |
2224 | minfo (" "); | |
2225 | #endif | |
2226 | } | |
2227 | ||
2228 | minfo (" "); | |
2229 | ||
2230 | exp_print_tree (assignment->exp); | |
2231 | ||
2232 | print_nl (); | |
2233 | } | |
2234 | ||
2235 | static void | |
1579bae1 | 2236 | print_input_statement (lang_input_statement_type *statm) |
252b5132 | 2237 | { |
1579bae1 | 2238 | if (statm->filename != NULL) |
252b5132 RH |
2239 | { |
2240 | fprintf (config.map_file, "LOAD %s\n", statm->filename); | |
2241 | } | |
2242 | } | |
2243 | ||
2244 | /* Print all symbols defined in a particular section. This is called | |
35835446 | 2245 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
252b5132 | 2246 | |
b34976b6 | 2247 | static bfd_boolean |
1579bae1 | 2248 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
252b5132 | 2249 | { |
1579bae1 | 2250 | asection *sec = ptr; |
252b5132 RH |
2251 | |
2252 | if ((hash_entry->type == bfd_link_hash_defined | |
2253 | || hash_entry->type == bfd_link_hash_defweak) | |
2254 | && sec == hash_entry->u.def.section) | |
2255 | { | |
2256 | int i; | |
2257 | ||
2258 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) | |
2259 | print_space (); | |
2260 | minfo ("0x%V ", | |
2261 | (hash_entry->u.def.value | |
2262 | + hash_entry->u.def.section->output_offset | |
2263 | + hash_entry->u.def.section->output_section->vma)); | |
2264 | ||
2265 | minfo (" %T\n", hash_entry->root.string); | |
2266 | } | |
2267 | ||
b34976b6 | 2268 | return TRUE; |
252b5132 RH |
2269 | } |
2270 | ||
35835446 JR |
2271 | static void |
2272 | print_all_symbols (sec) | |
2273 | asection *sec; | |
2274 | { | |
2275 | struct fat_user_section_struct *ud = get_userdata (sec); | |
2276 | struct map_symbol_def *def; | |
2277 | ||
2278 | *ud->map_symbol_def_tail = 0; | |
2279 | for (def = ud->map_symbol_def_head; def; def = def->next) | |
2280 | print_one_symbol (def->entry, sec); | |
2281 | } | |
2282 | ||
252b5132 RH |
2283 | /* Print information about an input section to the map file. */ |
2284 | ||
2285 | static void | |
1579bae1 | 2286 | print_input_section (lang_input_section_type *in) |
252b5132 RH |
2287 | { |
2288 | asection *i = in->section; | |
eea6121a | 2289 | bfd_size_type size = i->size; |
e5caa5e0 AM |
2290 | |
2291 | init_opb (); | |
252b5132 RH |
2292 | if (size != 0) |
2293 | { | |
57ceae94 AM |
2294 | int len; |
2295 | bfd_vma addr; | |
252b5132 | 2296 | |
57ceae94 | 2297 | print_space (); |
252b5132 RH |
2298 | minfo ("%s", i->name); |
2299 | ||
57ceae94 AM |
2300 | len = 1 + strlen (i->name); |
2301 | if (len >= SECTION_NAME_MAP_LENGTH - 1) | |
2302 | { | |
2303 | print_nl (); | |
2304 | len = 0; | |
2305 | } | |
2306 | while (len < SECTION_NAME_MAP_LENGTH) | |
252b5132 | 2307 | { |
57ceae94 AM |
2308 | print_space (); |
2309 | ++len; | |
2310 | } | |
252b5132 | 2311 | |
57ceae94 AM |
2312 | if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0) |
2313 | addr = i->output_section->vma + i->output_offset; | |
2314 | else | |
2315 | { | |
2316 | addr = print_dot; | |
2317 | size = 0; | |
2318 | } | |
252b5132 | 2319 | |
57ceae94 | 2320 | minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner); |
252b5132 | 2321 | |
eea6121a | 2322 | if (size != i->rawsize && i->rawsize != 0) |
57ceae94 AM |
2323 | { |
2324 | len = SECTION_NAME_MAP_LENGTH + 3; | |
252b5132 | 2325 | #ifdef BFD64 |
57ceae94 | 2326 | len += 16; |
252b5132 | 2327 | #else |
57ceae94 | 2328 | len += 8; |
252b5132 | 2329 | #endif |
57ceae94 AM |
2330 | while (len > 0) |
2331 | { | |
2332 | print_space (); | |
2333 | --len; | |
252b5132 RH |
2334 | } |
2335 | ||
eea6121a | 2336 | minfo (_("%W (size before relaxing)\n"), i->rawsize); |
57ceae94 AM |
2337 | } |
2338 | ||
2339 | if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0) | |
2340 | { | |
35835446 JR |
2341 | if (command_line.reduce_memory_overheads) |
2342 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); | |
2343 | else | |
2344 | print_all_symbols (i); | |
252b5132 | 2345 | |
57ceae94 | 2346 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
2347 | } |
2348 | } | |
2349 | } | |
2350 | ||
2351 | static void | |
1579bae1 | 2352 | print_fill_statement (lang_fill_statement_type *fill) |
252b5132 | 2353 | { |
2c382fb6 AM |
2354 | size_t size; |
2355 | unsigned char *p; | |
2356 | fputs (" FILL mask 0x", config.map_file); | |
2357 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) | |
2358 | fprintf (config.map_file, "%02x", *p); | |
2359 | fputs ("\n", config.map_file); | |
252b5132 RH |
2360 | } |
2361 | ||
2362 | static void | |
1579bae1 | 2363 | print_data_statement (lang_data_statement_type *data) |
252b5132 RH |
2364 | { |
2365 | int i; | |
2366 | bfd_vma addr; | |
2367 | bfd_size_type size; | |
2368 | const char *name; | |
2369 | ||
e5caa5e0 | 2370 | init_opb (); |
252b5132 RH |
2371 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
2372 | print_space (); | |
2373 | ||
2374 | addr = data->output_vma; | |
2375 | if (data->output_section != NULL) | |
2376 | addr += data->output_section->vma; | |
2377 | ||
2378 | switch (data->type) | |
2379 | { | |
2380 | default: | |
2381 | abort (); | |
2382 | case BYTE: | |
2383 | size = BYTE_SIZE; | |
2384 | name = "BYTE"; | |
2385 | break; | |
2386 | case SHORT: | |
2387 | size = SHORT_SIZE; | |
2388 | name = "SHORT"; | |
2389 | break; | |
2390 | case LONG: | |
2391 | size = LONG_SIZE; | |
2392 | name = "LONG"; | |
2393 | break; | |
2394 | case QUAD: | |
2395 | size = QUAD_SIZE; | |
2396 | name = "QUAD"; | |
2397 | break; | |
2398 | case SQUAD: | |
2399 | size = QUAD_SIZE; | |
2400 | name = "SQUAD"; | |
2401 | break; | |
2402 | } | |
2403 | ||
2404 | minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); | |
2405 | ||
2406 | if (data->exp->type.node_class != etree_value) | |
2407 | { | |
2408 | print_space (); | |
2409 | exp_print_tree (data->exp); | |
2410 | } | |
2411 | ||
2412 | print_nl (); | |
2413 | ||
e5caa5e0 | 2414 | print_dot = addr + TO_ADDR (size); |
252b5132 RH |
2415 | } |
2416 | ||
2417 | /* Print an address statement. These are generated by options like | |
2418 | -Ttext. */ | |
2419 | ||
2420 | static void | |
1579bae1 | 2421 | print_address_statement (lang_address_statement_type *address) |
252b5132 RH |
2422 | { |
2423 | minfo (_("Address of section %s set to "), address->section_name); | |
2424 | exp_print_tree (address->address); | |
2425 | print_nl (); | |
2426 | } | |
2427 | ||
2428 | /* Print a reloc statement. */ | |
2429 | ||
2430 | static void | |
1579bae1 | 2431 | print_reloc_statement (lang_reloc_statement_type *reloc) |
252b5132 RH |
2432 | { |
2433 | int i; | |
2434 | bfd_vma addr; | |
2435 | bfd_size_type size; | |
2436 | ||
e5caa5e0 | 2437 | init_opb (); |
252b5132 RH |
2438 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
2439 | print_space (); | |
2440 | ||
2441 | addr = reloc->output_vma; | |
2442 | if (reloc->output_section != NULL) | |
2443 | addr += reloc->output_section->vma; | |
2444 | ||
2445 | size = bfd_get_reloc_size (reloc->howto); | |
2446 | ||
2447 | minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); | |
2448 | ||
2449 | if (reloc->name != NULL) | |
2450 | minfo ("%s+", reloc->name); | |
2451 | else | |
2452 | minfo ("%s+", reloc->section->name); | |
2453 | ||
2454 | exp_print_tree (reloc->addend_exp); | |
2455 | ||
2456 | print_nl (); | |
2457 | ||
e5caa5e0 | 2458 | print_dot = addr + TO_ADDR (size); |
5f992e62 | 2459 | } |
252b5132 RH |
2460 | |
2461 | static void | |
1579bae1 | 2462 | print_padding_statement (lang_padding_statement_type *s) |
252b5132 RH |
2463 | { |
2464 | int len; | |
2465 | bfd_vma addr; | |
2466 | ||
e5caa5e0 | 2467 | init_opb (); |
252b5132 RH |
2468 | minfo (" *fill*"); |
2469 | ||
2470 | len = sizeof " *fill*" - 1; | |
2471 | while (len < SECTION_NAME_MAP_LENGTH) | |
2472 | { | |
2473 | print_space (); | |
2474 | ++len; | |
2475 | } | |
2476 | ||
2477 | addr = s->output_offset; | |
2478 | if (s->output_section != NULL) | |
2479 | addr += s->output_section->vma; | |
5f9b8920 | 2480 | minfo ("0x%V %W ", addr, (bfd_vma) s->size); |
252b5132 | 2481 | |
2c382fb6 AM |
2482 | if (s->fill->size != 0) |
2483 | { | |
2484 | size_t size; | |
2485 | unsigned char *p; | |
2486 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) | |
2487 | fprintf (config.map_file, "%02x", *p); | |
2488 | } | |
252b5132 RH |
2489 | |
2490 | print_nl (); | |
2491 | ||
e5caa5e0 | 2492 | print_dot = addr + TO_ADDR (s->size); |
252b5132 RH |
2493 | } |
2494 | ||
2495 | static void | |
1579bae1 AM |
2496 | print_wild_statement (lang_wild_statement_type *w, |
2497 | lang_output_section_statement_type *os) | |
252b5132 | 2498 | { |
b6bf44ba AM |
2499 | struct wildcard_list *sec; |
2500 | ||
252b5132 RH |
2501 | print_space (); |
2502 | ||
2503 | if (w->filenames_sorted) | |
2504 | minfo ("SORT("); | |
08da4cac | 2505 | if (w->filename != NULL) |
252b5132 RH |
2506 | minfo ("%s", w->filename); |
2507 | else | |
2508 | minfo ("*"); | |
2509 | if (w->filenames_sorted) | |
2510 | minfo (")"); | |
2511 | ||
2512 | minfo ("("); | |
b6bf44ba AM |
2513 | for (sec = w->section_list; sec; sec = sec->next) |
2514 | { | |
2515 | if (sec->spec.sorted) | |
2516 | minfo ("SORT("); | |
2517 | if (sec->spec.exclude_name_list != NULL) | |
2518 | { | |
2519 | name_list *tmp; | |
34786259 | 2520 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
b6bf44ba | 2521 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
34786259 AM |
2522 | minfo (" %s", tmp->name); |
2523 | minfo (") "); | |
b6bf44ba AM |
2524 | } |
2525 | if (sec->spec.name != NULL) | |
2526 | minfo ("%s", sec->spec.name); | |
2527 | else | |
2528 | minfo ("*"); | |
2529 | if (sec->spec.sorted) | |
2530 | minfo (")"); | |
34786259 AM |
2531 | if (sec->next) |
2532 | minfo (" "); | |
b6bf44ba | 2533 | } |
252b5132 RH |
2534 | minfo (")"); |
2535 | ||
2536 | print_nl (); | |
2537 | ||
2538 | print_statement_list (w->children.head, os); | |
2539 | } | |
2540 | ||
2541 | /* Print a group statement. */ | |
2542 | ||
2543 | static void | |
1579bae1 AM |
2544 | print_group (lang_group_statement_type *s, |
2545 | lang_output_section_statement_type *os) | |
252b5132 RH |
2546 | { |
2547 | fprintf (config.map_file, "START GROUP\n"); | |
2548 | print_statement_list (s->children.head, os); | |
2549 | fprintf (config.map_file, "END GROUP\n"); | |
2550 | } | |
2551 | ||
2552 | /* Print the list of statements in S. | |
2553 | This can be called for any statement type. */ | |
2554 | ||
2555 | static void | |
1579bae1 AM |
2556 | print_statement_list (lang_statement_union_type *s, |
2557 | lang_output_section_statement_type *os) | |
252b5132 RH |
2558 | { |
2559 | while (s != NULL) | |
2560 | { | |
2561 | print_statement (s, os); | |
bba1a0c0 | 2562 | s = s->header.next; |
252b5132 RH |
2563 | } |
2564 | } | |
2565 | ||
2566 | /* Print the first statement in statement list S. | |
2567 | This can be called for any statement type. */ | |
2568 | ||
2569 | static void | |
1579bae1 AM |
2570 | print_statement (lang_statement_union_type *s, |
2571 | lang_output_section_statement_type *os) | |
252b5132 RH |
2572 | { |
2573 | switch (s->header.type) | |
2574 | { | |
2575 | default: | |
2576 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); | |
2577 | FAIL (); | |
2578 | break; | |
2579 | case lang_constructors_statement_enum: | |
2580 | if (constructor_list.head != NULL) | |
2581 | { | |
2582 | if (constructors_sorted) | |
2583 | minfo (" SORT (CONSTRUCTORS)\n"); | |
2584 | else | |
2585 | minfo (" CONSTRUCTORS\n"); | |
2586 | print_statement_list (constructor_list.head, os); | |
2587 | } | |
2588 | break; | |
2589 | case lang_wild_statement_enum: | |
2590 | print_wild_statement (&s->wild_statement, os); | |
2591 | break; | |
2592 | case lang_address_statement_enum: | |
2593 | print_address_statement (&s->address_statement); | |
2594 | break; | |
2595 | case lang_object_symbols_statement_enum: | |
2596 | minfo (" CREATE_OBJECT_SYMBOLS\n"); | |
2597 | break; | |
2598 | case lang_fill_statement_enum: | |
2599 | print_fill_statement (&s->fill_statement); | |
2600 | break; | |
2601 | case lang_data_statement_enum: | |
2602 | print_data_statement (&s->data_statement); | |
2603 | break; | |
2604 | case lang_reloc_statement_enum: | |
2605 | print_reloc_statement (&s->reloc_statement); | |
2606 | break; | |
2607 | case lang_input_section_enum: | |
2608 | print_input_section (&s->input_section); | |
2609 | break; | |
2610 | case lang_padding_statement_enum: | |
2611 | print_padding_statement (&s->padding_statement); | |
2612 | break; | |
2613 | case lang_output_section_statement_enum: | |
2614 | print_output_section_statement (&s->output_section_statement); | |
2615 | break; | |
2616 | case lang_assignment_statement_enum: | |
2617 | print_assignment (&s->assignment_statement, os); | |
2618 | break; | |
2619 | case lang_target_statement_enum: | |
2620 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); | |
2621 | break; | |
2622 | case lang_output_statement_enum: | |
2623 | minfo ("OUTPUT(%s", s->output_statement.name); | |
2624 | if (output_target != NULL) | |
2625 | minfo (" %s", output_target); | |
2626 | minfo (")\n"); | |
2627 | break; | |
2628 | case lang_input_statement_enum: | |
2629 | print_input_statement (&s->input_statement); | |
2630 | break; | |
2631 | case lang_group_statement_enum: | |
2632 | print_group (&s->group_statement, os); | |
2633 | break; | |
2634 | case lang_afile_asection_pair_statement_enum: | |
2635 | FAIL (); | |
2636 | break; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | static void | |
1579bae1 | 2641 | print_statements (void) |
252b5132 RH |
2642 | { |
2643 | print_statement_list (statement_list.head, abs_output_section); | |
2644 | } | |
2645 | ||
2646 | /* Print the first N statements in statement list S to STDERR. | |
2647 | If N == 0, nothing is printed. | |
2648 | If N < 0, the entire list is printed. | |
2649 | Intended to be called from GDB. */ | |
2650 | ||
2651 | void | |
1579bae1 | 2652 | dprint_statement (lang_statement_union_type *s, int n) |
252b5132 RH |
2653 | { |
2654 | FILE *map_save = config.map_file; | |
2655 | ||
2656 | config.map_file = stderr; | |
2657 | ||
2658 | if (n < 0) | |
2659 | print_statement_list (s, abs_output_section); | |
2660 | else | |
2661 | { | |
2662 | while (s && --n >= 0) | |
2663 | { | |
2664 | print_statement (s, abs_output_section); | |
bba1a0c0 | 2665 | s = s->header.next; |
252b5132 RH |
2666 | } |
2667 | } | |
2668 | ||
2669 | config.map_file = map_save; | |
2670 | } | |
2671 | ||
b3327aad | 2672 | static void |
1579bae1 AM |
2673 | insert_pad (lang_statement_union_type **ptr, |
2674 | fill_type *fill, | |
2675 | unsigned int alignment_needed, | |
2676 | asection *output_section, | |
2677 | bfd_vma dot) | |
252b5132 | 2678 | { |
2c382fb6 | 2679 | static fill_type zero_fill = { 1, { 0 } }; |
b3327aad | 2680 | lang_statement_union_type *pad; |
b3327aad | 2681 | |
c0c330a7 | 2682 | pad = ((lang_statement_union_type *) |
2af02257 | 2683 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); |
b3327aad | 2684 | if (ptr != &statement_list.head |
2af02257 | 2685 | && pad->header.type == lang_padding_statement_enum |
b3327aad | 2686 | && pad->padding_statement.output_section == output_section) |
252b5132 | 2687 | { |
b3327aad AM |
2688 | /* Use the existing pad statement. The above test on output |
2689 | section is probably redundant, but it doesn't hurt to check. */ | |
252b5132 | 2690 | } |
b3327aad | 2691 | else |
252b5132 | 2692 | { |
b3327aad | 2693 | /* Make a new padding statement, linked into existing chain. */ |
1579bae1 | 2694 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
b3327aad AM |
2695 | pad->header.next = *ptr; |
2696 | *ptr = pad; | |
2697 | pad->header.type = lang_padding_statement_enum; | |
2698 | pad->padding_statement.output_section = output_section; | |
1579bae1 | 2699 | if (fill == NULL) |
2c382fb6 | 2700 | fill = &zero_fill; |
b3327aad | 2701 | pad->padding_statement.fill = fill; |
252b5132 | 2702 | } |
b3327aad AM |
2703 | pad->padding_statement.output_offset = dot - output_section->vma; |
2704 | pad->padding_statement.size = alignment_needed; | |
eea6121a | 2705 | output_section->size += alignment_needed; |
252b5132 RH |
2706 | } |
2707 | ||
08da4cac KH |
2708 | /* Work out how much this section will move the dot point. */ |
2709 | ||
252b5132 | 2710 | static bfd_vma |
6feb9908 AM |
2711 | size_input_section |
2712 | (lang_statement_union_type **this_ptr, | |
2713 | lang_output_section_statement_type *output_section_statement, | |
2714 | fill_type *fill, | |
2715 | bfd_vma dot) | |
252b5132 RH |
2716 | { |
2717 | lang_input_section_type *is = &((*this_ptr)->input_section); | |
2718 | asection *i = is->section; | |
2719 | ||
57ceae94 | 2720 | if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0) |
252b5132 | 2721 | { |
b3327aad AM |
2722 | unsigned int alignment_needed; |
2723 | asection *o; | |
2724 | ||
2725 | /* Align this section first to the input sections requirement, | |
2726 | then to the output section's requirement. If this alignment | |
2727 | is greater than any seen before, then record it too. Perform | |
2728 | the alignment by inserting a magic 'padding' statement. */ | |
2729 | ||
252b5132 | 2730 | if (output_section_statement->subsection_alignment != -1) |
b3327aad AM |
2731 | i->alignment_power = output_section_statement->subsection_alignment; |
2732 | ||
2733 | o = output_section_statement->bfd_section; | |
2734 | if (o->alignment_power < i->alignment_power) | |
2735 | o->alignment_power = i->alignment_power; | |
252b5132 | 2736 | |
b3327aad AM |
2737 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
2738 | ||
2739 | if (alignment_needed != 0) | |
2740 | { | |
e5caa5e0 | 2741 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
b3327aad AM |
2742 | dot += alignment_needed; |
2743 | } | |
252b5132 | 2744 | |
08da4cac | 2745 | /* Remember where in the output section this input section goes. */ |
252b5132 | 2746 | |
b3327aad | 2747 | i->output_offset = dot - o->vma; |
252b5132 | 2748 | |
08da4cac | 2749 | /* Mark how big the output section must be to contain this now. */ |
eea6121a AM |
2750 | dot += TO_ADDR (i->size); |
2751 | o->size = TO_SIZE (dot - o->vma); | |
252b5132 RH |
2752 | } |
2753 | else | |
2754 | { | |
2755 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; | |
2756 | } | |
2757 | ||
2758 | return dot; | |
2759 | } | |
2760 | ||
eea6121a | 2761 | #define IGNORE_SECTION(s) \ |
6feb9908 AM |
2762 | (((s->flags & SEC_THREAD_LOCAL) != 0 \ |
2763 | ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \ | |
2764 | : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \ | |
eea6121a | 2765 | || s->size == 0) |
d1778b88 | 2766 | |
252b5132 RH |
2767 | /* Check to see if any allocated sections overlap with other allocated |
2768 | sections. This can happen when the linker script specifically specifies | |
2769 | the output section addresses of the two sections. */ | |
08da4cac | 2770 | |
252b5132 | 2771 | static void |
1579bae1 | 2772 | lang_check_section_addresses (void) |
252b5132 | 2773 | { |
08da4cac | 2774 | asection *s; |
252b5132 RH |
2775 | |
2776 | /* Scan all sections in the output list. */ | |
2777 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
33275c22 | 2778 | { |
08da4cac | 2779 | asection *os; |
5f992e62 | 2780 | |
33275c22 | 2781 | /* Ignore sections which are not loaded or which have no contents. */ |
eea6121a | 2782 | if (IGNORE_SECTION (s)) |
33275c22 | 2783 | continue; |
5f992e62 | 2784 | |
33275c22 NC |
2785 | /* Once we reach section 's' stop our seach. This prevents two |
2786 | warning messages from being produced, one for 'section A overlaps | |
2787 | section B' and one for 'section B overlaps section A'. */ | |
2788 | for (os = output_bfd->sections; os != s; os = os->next) | |
2789 | { | |
2790 | bfd_vma s_start; | |
2791 | bfd_vma s_end; | |
2792 | bfd_vma os_start; | |
2793 | bfd_vma os_end; | |
5f992e62 | 2794 | |
33275c22 | 2795 | /* Only consider loadable sections with real contents. */ |
eea6121a | 2796 | if (IGNORE_SECTION (os)) |
33275c22 | 2797 | continue; |
252b5132 | 2798 | |
33275c22 NC |
2799 | /* We must check the sections' LMA addresses not their |
2800 | VMA addresses because overlay sections can have | |
2801 | overlapping VMAs but they must have distinct LMAs. */ | |
e5caa5e0 | 2802 | s_start = bfd_section_lma (output_bfd, s); |
33275c22 | 2803 | os_start = bfd_section_lma (output_bfd, os); |
eea6121a AM |
2804 | s_end = s_start + TO_ADDR (s->size) - 1; |
2805 | os_end = os_start + TO_ADDR (os->size) - 1; | |
5f992e62 | 2806 | |
33275c22 NC |
2807 | /* Look for an overlap. */ |
2808 | if ((s_end < os_start) || (s_start > os_end)) | |
2809 | continue; | |
5f992e62 | 2810 | |
33275c22 | 2811 | einfo ( |
252b5132 | 2812 | _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"), |
33275c22 | 2813 | s->name, s_start, s_end, os->name, os_start, os_end); |
5f992e62 | 2814 | |
33275c22 NC |
2815 | /* Once we have found one overlap for this section, |
2816 | stop looking for others. */ | |
2817 | break; | |
2818 | } | |
2819 | } | |
252b5132 RH |
2820 | } |
2821 | ||
562d3460 TW |
2822 | /* Make sure the new address is within the region. We explicitly permit the |
2823 | current address to be at the exact end of the region when the address is | |
2824 | non-zero, in case the region is at the end of addressable memory and the | |
5f992e62 | 2825 | calculation wraps around. */ |
562d3460 TW |
2826 | |
2827 | static void | |
1579bae1 | 2828 | os_region_check (lang_output_section_statement_type *os, |
6bdafbeb | 2829 | lang_memory_region_type *region, |
1579bae1 AM |
2830 | etree_type *tree, |
2831 | bfd_vma base) | |
562d3460 TW |
2832 | { |
2833 | if ((region->current < region->origin | |
2834 | || (region->current - region->origin > region->length)) | |
2835 | && ((region->current != region->origin + region->length) | |
b7a26f91 | 2836 | || base == 0)) |
562d3460 | 2837 | { |
1579bae1 | 2838 | if (tree != NULL) |
b7a26f91 | 2839 | { |
6feb9908 AM |
2840 | einfo (_("%X%P: address 0x%v of %B section %s" |
2841 | " is not within region %s\n"), | |
b7a26f91 KH |
2842 | region->current, |
2843 | os->bfd_section->owner, | |
2844 | os->bfd_section->name, | |
2845 | region->name); | |
2846 | } | |
562d3460 | 2847 | else |
b7a26f91 KH |
2848 | { |
2849 | einfo (_("%X%P: region %s is full (%B section %s)\n"), | |
2850 | region->name, | |
2851 | os->bfd_section->owner, | |
2852 | os->bfd_section->name); | |
2853 | } | |
562d3460 TW |
2854 | /* Reset the region pointer. */ |
2855 | region->current = region->origin; | |
2856 | } | |
2857 | } | |
2858 | ||
252b5132 RH |
2859 | /* Set the sizes for all the output sections. */ |
2860 | ||
2d20f7bf | 2861 | static bfd_vma |
1579bae1 AM |
2862 | lang_size_sections_1 |
2863 | (lang_statement_union_type *s, | |
2864 | lang_output_section_statement_type *output_section_statement, | |
2865 | lang_statement_union_type **prev, | |
2866 | fill_type *fill, | |
2867 | bfd_vma dot, | |
2868 | bfd_boolean *relax, | |
2869 | bfd_boolean check_regions) | |
252b5132 RH |
2870 | { |
2871 | /* Size up the sections from their constituent parts. */ | |
1579bae1 | 2872 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
2873 | { |
2874 | switch (s->header.type) | |
2875 | { | |
2876 | case lang_output_section_statement_enum: | |
2877 | { | |
2878 | bfd_vma after; | |
d1778b88 | 2879 | lang_output_section_statement_type *os; |
252b5132 | 2880 | |
d1778b88 | 2881 | os = &s->output_section_statement; |
252b5132 RH |
2882 | if (os->bfd_section == NULL) |
2883 | /* This section was never actually created. */ | |
2884 | break; | |
2885 | ||
2886 | /* If this is a COFF shared library section, use the size and | |
2887 | address from the input section. FIXME: This is COFF | |
2888 | specific; it would be cleaner if there were some other way | |
2889 | to do this, but nothing simple comes to mind. */ | |
2890 | if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) | |
2891 | { | |
08da4cac | 2892 | asection *input; |
252b5132 RH |
2893 | |
2894 | if (os->children.head == NULL | |
bba1a0c0 | 2895 | || os->children.head->header.next != NULL |
6feb9908 AM |
2896 | || (os->children.head->header.type |
2897 | != lang_input_section_enum)) | |
2898 | einfo (_("%P%X: Internal error on COFF shared library" | |
2899 | " section %s\n"), os->name); | |
252b5132 RH |
2900 | |
2901 | input = os->children.head->input_section.section; | |
2902 | bfd_set_section_vma (os->bfd_section->owner, | |
2903 | os->bfd_section, | |
2904 | bfd_section_vma (input->owner, input)); | |
eea6121a | 2905 | os->bfd_section->size = input->size; |
252b5132 RH |
2906 | break; |
2907 | } | |
2908 | ||
2909 | if (bfd_is_abs_section (os->bfd_section)) | |
2910 | { | |
2911 | /* No matter what happens, an abs section starts at zero. */ | |
2912 | ASSERT (os->bfd_section->vma == 0); | |
2913 | } | |
2914 | else | |
2915 | { | |
1579bae1 | 2916 | if (os->addr_tree == NULL) |
252b5132 RH |
2917 | { |
2918 | /* No address specified for this section, get one | |
2919 | from the region specification. */ | |
1579bae1 | 2920 | if (os->region == NULL |
6feb9908 | 2921 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
252b5132 | 2922 | && os->region->name[0] == '*' |
6feb9908 AM |
2923 | && strcmp (os->region->name, |
2924 | DEFAULT_MEMORY_REGION) == 0)) | |
252b5132 RH |
2925 | { |
2926 | os->region = lang_memory_default (os->bfd_section); | |
2927 | } | |
2928 | ||
2929 | /* If a loadable section is using the default memory | |
2930 | region, and some non default memory regions were | |
66184979 | 2931 | defined, issue an error message. */ |
eea6121a | 2932 | if (!IGNORE_SECTION (os->bfd_section) |
1049f94e | 2933 | && ! link_info.relocatable |
cf888e70 | 2934 | && check_regions |
6feb9908 AM |
2935 | && strcmp (os->region->name, |
2936 | DEFAULT_MEMORY_REGION) == 0 | |
252b5132 | 2937 | && lang_memory_region_list != NULL |
d1778b88 | 2938 | && (strcmp (lang_memory_region_list->name, |
a747ee4d | 2939 | DEFAULT_MEMORY_REGION) != 0 |
252b5132 | 2940 | || lang_memory_region_list->next != NULL)) |
66184979 NC |
2941 | { |
2942 | /* By default this is an error rather than just a | |
2943 | warning because if we allocate the section to the | |
2944 | default memory region we can end up creating an | |
07f3b6ad KH |
2945 | excessively large binary, or even seg faulting when |
2946 | attempting to perform a negative seek. See | |
6feb9908 | 2947 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
66184979 NC |
2948 | for an example of this. This behaviour can be |
2949 | overridden by the using the --no-check-sections | |
2950 | switch. */ | |
2951 | if (command_line.check_section_addresses) | |
6feb9908 AM |
2952 | einfo (_("%P%F: error: no memory region specified" |
2953 | " for loadable section `%s'\n"), | |
66184979 NC |
2954 | bfd_get_section_name (output_bfd, |
2955 | os->bfd_section)); | |
2956 | else | |
6feb9908 AM |
2957 | einfo (_("%P: warning: no memory region specified" |
2958 | " for loadable section `%s'\n"), | |
66184979 NC |
2959 | bfd_get_section_name (output_bfd, |
2960 | os->bfd_section)); | |
2961 | } | |
252b5132 RH |
2962 | |
2963 | dot = os->region->current; | |
5f992e62 | 2964 | |
252b5132 RH |
2965 | if (os->section_alignment == -1) |
2966 | { | |
2967 | bfd_vma olddot; | |
2968 | ||
2969 | olddot = dot; | |
d1778b88 AM |
2970 | dot = align_power (dot, |
2971 | os->bfd_section->alignment_power); | |
252b5132 RH |
2972 | |
2973 | if (dot != olddot && config.warn_section_align) | |
6feb9908 AM |
2974 | einfo (_("%P: warning: changing start of section" |
2975 | " %s by %u bytes\n"), | |
252b5132 RH |
2976 | os->name, (unsigned int) (dot - olddot)); |
2977 | } | |
2978 | } | |
2979 | else | |
2980 | { | |
2981 | etree_value_type r; | |
2982 | ||
1b493742 | 2983 | os->processed = -1; |
252b5132 RH |
2984 | r = exp_fold_tree (os->addr_tree, |
2985 | abs_output_section, | |
2986 | lang_allocating_phase_enum, | |
2987 | dot, &dot); | |
1b493742 NS |
2988 | os->processed = 0; |
2989 | ||
7c519c12 | 2990 | if (!r.valid_p) |
6feb9908 AM |
2991 | einfo (_("%F%S: non constant or forward reference" |
2992 | " address expression for section %s\n"), | |
7c519c12 AM |
2993 | os->name); |
2994 | ||
252b5132 RH |
2995 | dot = r.value + r.section->bfd_section->vma; |
2996 | } | |
5f992e62 | 2997 | |
252b5132 RH |
2998 | /* The section starts here. |
2999 | First, align to what the section needs. */ | |
3000 | ||
3001 | if (os->section_alignment != -1) | |
3002 | dot = align_power (dot, os->section_alignment); | |
3003 | ||
3004 | bfd_set_section_vma (0, os->bfd_section, dot); | |
5f992e62 | 3005 | |
252b5132 RH |
3006 | os->bfd_section->output_offset = 0; |
3007 | } | |
3008 | ||
2d20f7bf | 3009 | lang_size_sections_1 (os->children.head, os, &os->children.head, |
cf888e70 | 3010 | os->fill, dot, relax, check_regions); |
5f992e62 | 3011 | |
08da4cac KH |
3012 | /* Put the section within the requested block size, or |
3013 | align at the block boundary. */ | |
e5caa5e0 | 3014 | after = ((os->bfd_section->vma |
eea6121a | 3015 | + TO_ADDR (os->bfd_section->size) |
e5caa5e0 AM |
3016 | + os->block_value - 1) |
3017 | & - (bfd_vma) os->block_value); | |
252b5132 RH |
3018 | |
3019 | if (bfd_is_abs_section (os->bfd_section)) | |
3020 | ASSERT (after == os->bfd_section->vma); | |
3021 | else | |
eea6121a | 3022 | os->bfd_section->size |
e5caa5e0 | 3023 | = TO_SIZE (after - os->bfd_section->vma); |
9f88b410 | 3024 | |
e5caec89 NS |
3025 | dot = os->bfd_section->vma; |
3026 | /* .tbss sections effectively have zero size. */ | |
3027 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
3028 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
3029 | || link_info.relocatable) | |
eea6121a | 3030 | dot += TO_ADDR (os->bfd_section->size); |
e5caec89 | 3031 | |
1b493742 | 3032 | os->processed = 1; |
252b5132 | 3033 | |
9f88b410 RS |
3034 | if (os->update_dot_tree != 0) |
3035 | exp_fold_tree (os->update_dot_tree, abs_output_section, | |
3036 | lang_allocating_phase_enum, dot, &dot); | |
3037 | ||
252b5132 RH |
3038 | /* Update dot in the region ? |
3039 | We only do this if the section is going to be allocated, | |
3040 | since unallocated sections do not contribute to the region's | |
13392b77 | 3041 | overall size in memory. |
5f992e62 | 3042 | |
cce4c4c5 NC |
3043 | If the SEC_NEVER_LOAD bit is not set, it will affect the |
3044 | addresses of sections after it. We have to update | |
3045 | dot. */ | |
1579bae1 | 3046 | if (os->region != NULL |
6feb9908 AM |
3047 | && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0 |
3048 | || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))) | |
252b5132 RH |
3049 | { |
3050 | os->region->current = dot; | |
5f992e62 | 3051 | |
cf888e70 NC |
3052 | if (check_regions) |
3053 | /* Make sure the new address is within the region. */ | |
3054 | os_region_check (os, os->region, os->addr_tree, | |
3055 | os->bfd_section->vma); | |
08da4cac KH |
3056 | |
3057 | /* If there's no load address specified, use the run | |
3058 | region as the load region. */ | |
3059 | if (os->lma_region == NULL && os->load_base == NULL) | |
3060 | os->lma_region = os->region; | |
3061 | ||
ee3cc2e2 | 3062 | if (os->lma_region != NULL && os->lma_region != os->region) |
08da4cac | 3063 | { |
ee3cc2e2 RS |
3064 | /* Set load_base, which will be handled later. */ |
3065 | os->load_base = exp_intop (os->lma_region->current); | |
3066 | os->lma_region->current += | |
eea6121a | 3067 | TO_ADDR (os->bfd_section->size); |
cf888e70 NC |
3068 | if (check_regions) |
3069 | os_region_check (os, os->lma_region, NULL, | |
3070 | os->bfd_section->lma); | |
08da4cac | 3071 | } |
252b5132 RH |
3072 | } |
3073 | } | |
3074 | break; | |
3075 | ||
3076 | case lang_constructors_statement_enum: | |
2d20f7bf JJ |
3077 | dot = lang_size_sections_1 (constructor_list.head, |
3078 | output_section_statement, | |
3079 | &s->wild_statement.children.head, | |
cf888e70 | 3080 | fill, dot, relax, check_regions); |
252b5132 RH |
3081 | break; |
3082 | ||
3083 | case lang_data_statement_enum: | |
3084 | { | |
3085 | unsigned int size = 0; | |
3086 | ||
08da4cac KH |
3087 | s->data_statement.output_vma = |
3088 | dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
3089 | s->data_statement.output_section = |
3090 | output_section_statement->bfd_section; | |
3091 | ||
1b493742 NS |
3092 | /* We might refer to provided symbols in the expression, and |
3093 | need to mark them as needed. */ | |
3094 | exp_fold_tree (s->data_statement.exp, abs_output_section, | |
3095 | lang_allocating_phase_enum, dot, &dot); | |
3096 | ||
252b5132 RH |
3097 | switch (s->data_statement.type) |
3098 | { | |
08da4cac KH |
3099 | default: |
3100 | abort (); | |
252b5132 RH |
3101 | case QUAD: |
3102 | case SQUAD: | |
3103 | size = QUAD_SIZE; | |
3104 | break; | |
3105 | case LONG: | |
3106 | size = LONG_SIZE; | |
3107 | break; | |
3108 | case SHORT: | |
3109 | size = SHORT_SIZE; | |
3110 | break; | |
3111 | case BYTE: | |
3112 | size = BYTE_SIZE; | |
3113 | break; | |
3114 | } | |
e5caa5e0 AM |
3115 | if (size < TO_SIZE ((unsigned) 1)) |
3116 | size = TO_SIZE ((unsigned) 1); | |
3117 | dot += TO_ADDR (size); | |
eea6121a | 3118 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
3119 | /* The output section gets contents, and then we inspect for |
3120 | any flags set in the input script which override any ALLOC. */ | |
3121 | output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS; | |
08da4cac KH |
3122 | if (!(output_section_statement->flags & SEC_NEVER_LOAD)) |
3123 | { | |
3124 | output_section_statement->bfd_section->flags |= | |
3125 | SEC_ALLOC | SEC_LOAD; | |
3126 | } | |
252b5132 RH |
3127 | } |
3128 | break; | |
3129 | ||
3130 | case lang_reloc_statement_enum: | |
3131 | { | |
3132 | int size; | |
3133 | ||
3134 | s->reloc_statement.output_vma = | |
3135 | dot - output_section_statement->bfd_section->vma; | |
3136 | s->reloc_statement.output_section = | |
3137 | output_section_statement->bfd_section; | |
3138 | size = bfd_get_reloc_size (s->reloc_statement.howto); | |
e5caa5e0 | 3139 | dot += TO_ADDR (size); |
eea6121a | 3140 | output_section_statement->bfd_section->size += size; |
252b5132 RH |
3141 | } |
3142 | break; | |
5f992e62 | 3143 | |
252b5132 RH |
3144 | case lang_wild_statement_enum: |
3145 | ||
2d20f7bf JJ |
3146 | dot = lang_size_sections_1 (s->wild_statement.children.head, |
3147 | output_section_statement, | |
3148 | &s->wild_statement.children.head, | |
cf888e70 | 3149 | fill, dot, relax, check_regions); |
252b5132 RH |
3150 | |
3151 | break; | |
3152 | ||
3153 | case lang_object_symbols_statement_enum: | |
3154 | link_info.create_object_symbols_section = | |
3155 | output_section_statement->bfd_section; | |
3156 | break; | |
3157 | case lang_output_statement_enum: | |
3158 | case lang_target_statement_enum: | |
3159 | break; | |
3160 | case lang_input_section_enum: | |
3161 | { | |
3162 | asection *i; | |
3163 | ||
3164 | i = (*prev)->input_section.section; | |
eea6121a | 3165 | if (relax) |
252b5132 | 3166 | { |
b34976b6 | 3167 | bfd_boolean again; |
252b5132 RH |
3168 | |
3169 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) | |
3170 | einfo (_("%P%F: can't relax section: %E\n")); | |
3171 | if (again) | |
b34976b6 | 3172 | *relax = TRUE; |
252b5132 | 3173 | } |
b3327aad AM |
3174 | dot = size_input_section (prev, output_section_statement, |
3175 | output_section_statement->fill, dot); | |
252b5132 RH |
3176 | } |
3177 | break; | |
3178 | case lang_input_statement_enum: | |
3179 | break; | |
3180 | case lang_fill_statement_enum: | |
08da4cac KH |
3181 | s->fill_statement.output_section = |
3182 | output_section_statement->bfd_section; | |
252b5132 RH |
3183 | |
3184 | fill = s->fill_statement.fill; | |
3185 | break; | |
3186 | case lang_assignment_statement_enum: | |
3187 | { | |
3188 | bfd_vma newdot = dot; | |
3189 | ||
3190 | exp_fold_tree (s->assignment_statement.exp, | |
3191 | output_section_statement, | |
3192 | lang_allocating_phase_enum, | |
3193 | dot, | |
3194 | &newdot); | |
3195 | ||
3196 | if (newdot != dot) | |
3197 | { | |
252b5132 RH |
3198 | if (output_section_statement == abs_output_section) |
3199 | { | |
3200 | /* If we don't have an output section, then just adjust | |
3201 | the default memory address. */ | |
6feb9908 AM |
3202 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
3203 | FALSE)->current = newdot; | |
252b5132 | 3204 | } |
b3327aad | 3205 | else |
252b5132 | 3206 | { |
b3327aad AM |
3207 | /* Insert a pad after this statement. We can't |
3208 | put the pad before when relaxing, in case the | |
3209 | assignment references dot. */ | |
e5caa5e0 | 3210 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
b3327aad AM |
3211 | output_section_statement->bfd_section, dot); |
3212 | ||
3213 | /* Don't neuter the pad below when relaxing. */ | |
3214 | s = s->header.next; | |
252b5132 RH |
3215 | } |
3216 | ||
9dfc8ab2 NC |
3217 | /* If dot is advanced, this implies that the section should |
3218 | have space allocated to it, unless the user has explicitly | |
3219 | stated that the section should never be loaded. */ | |
6feb9908 AM |
3220 | if (!(output_section_statement->flags |
3221 | & (SEC_NEVER_LOAD | SEC_ALLOC))) | |
9dfc8ab2 NC |
3222 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
3223 | ||
252b5132 RH |
3224 | dot = newdot; |
3225 | } | |
3226 | } | |
3227 | break; | |
3228 | ||
3229 | case lang_padding_statement_enum: | |
c0c330a7 AM |
3230 | /* If this is the first time lang_size_sections is called, |
3231 | we won't have any padding statements. If this is the | |
3232 | second or later passes when relaxing, we should allow | |
3233 | padding to shrink. If padding is needed on this pass, it | |
3234 | will be added back in. */ | |
3235 | s->padding_statement.size = 0; | |
6e814ff8 AM |
3236 | |
3237 | /* Make sure output_offset is valid. If relaxation shrinks | |
3238 | the section and this pad isn't needed, it's possible to | |
3239 | have output_offset larger than the final size of the | |
3240 | section. bfd_set_section_contents will complain even for | |
3241 | a pad size of zero. */ | |
3242 | s->padding_statement.output_offset | |
3243 | = dot - output_section_statement->bfd_section->vma; | |
252b5132 RH |
3244 | break; |
3245 | ||
3246 | case lang_group_statement_enum: | |
2d20f7bf JJ |
3247 | dot = lang_size_sections_1 (s->group_statement.children.head, |
3248 | output_section_statement, | |
3249 | &s->group_statement.children.head, | |
cf888e70 | 3250 | fill, dot, relax, check_regions); |
252b5132 RH |
3251 | break; |
3252 | ||
3253 | default: | |
3254 | FAIL (); | |
3255 | break; | |
3256 | ||
c0c330a7 | 3257 | /* We can only get here when relaxing is turned on. */ |
252b5132 RH |
3258 | case lang_address_statement_enum: |
3259 | break; | |
3260 | } | |
3261 | prev = &s->header.next; | |
3262 | } | |
3263 | return dot; | |
3264 | } | |
3265 | ||
2d20f7bf | 3266 | bfd_vma |
1579bae1 AM |
3267 | lang_size_sections |
3268 | (lang_statement_union_type *s, | |
3269 | lang_output_section_statement_type *output_section_statement, | |
3270 | lang_statement_union_type **prev, | |
3271 | fill_type *fill, | |
3272 | bfd_vma dot, | |
3273 | bfd_boolean *relax, | |
3274 | bfd_boolean check_regions) | |
2d20f7bf JJ |
3275 | { |
3276 | bfd_vma result; | |
3277 | ||
420e579c HPN |
3278 | /* Callers of exp_fold_tree need to increment this. */ |
3279 | lang_statement_iteration++; | |
3280 | ||
2d20f7bf JJ |
3281 | exp_data_seg.phase = exp_dataseg_none; |
3282 | result = lang_size_sections_1 (s, output_section_statement, prev, fill, | |
cf888e70 | 3283 | dot, relax, check_regions); |
8c37241b JJ |
3284 | if (exp_data_seg.phase == exp_dataseg_end_seen |
3285 | && link_info.relro && exp_data_seg.relro_end) | |
3286 | { | |
3287 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try | |
3288 | to put exp_data_seg.relro on a (common) page boundary. */ | |
3289 | ||
3290 | exp_data_seg.phase = exp_dataseg_relro_adjust; | |
3291 | result = lang_size_sections_1 (s, output_section_statement, prev, fill, | |
3292 | dot, relax, check_regions); | |
3293 | link_info.relro_start = exp_data_seg.base; | |
3294 | link_info.relro_end = exp_data_seg.relro_end; | |
3295 | } | |
3296 | else if (exp_data_seg.phase == exp_dataseg_end_seen) | |
2d20f7bf JJ |
3297 | { |
3298 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether | |
3299 | a page could be saved in the data segment. */ | |
3300 | bfd_vma first, last; | |
3301 | ||
3302 | first = -exp_data_seg.base & (exp_data_seg.pagesize - 1); | |
3303 | last = exp_data_seg.end & (exp_data_seg.pagesize - 1); | |
3304 | if (first && last | |
3305 | && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1)) | |
3306 | != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1))) | |
3307 | && first + last <= exp_data_seg.pagesize) | |
3308 | { | |
3309 | exp_data_seg.phase = exp_dataseg_adjust; | |
1b493742 | 3310 | lang_statement_iteration++; |
2d20f7bf | 3311 | result = lang_size_sections_1 (s, output_section_statement, prev, |
cf888e70 | 3312 | fill, dot, relax, check_regions); |
2d20f7bf JJ |
3313 | } |
3314 | } | |
3315 | ||
3316 | return result; | |
3317 | } | |
3318 | ||
420e579c HPN |
3319 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
3320 | ||
3321 | static bfd_vma | |
3322 | lang_do_assignments_1 | |
1579bae1 AM |
3323 | (lang_statement_union_type *s, |
3324 | lang_output_section_statement_type *output_section_statement, | |
3325 | fill_type *fill, | |
3326 | bfd_vma dot) | |
252b5132 | 3327 | { |
1579bae1 | 3328 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
3329 | { |
3330 | switch (s->header.type) | |
3331 | { | |
3332 | case lang_constructors_statement_enum: | |
420e579c HPN |
3333 | dot = lang_do_assignments_1 (constructor_list.head, |
3334 | output_section_statement, | |
3335 | fill, | |
3336 | dot); | |
252b5132 RH |
3337 | break; |
3338 | ||
3339 | case lang_output_section_statement_enum: | |
3340 | { | |
d1778b88 | 3341 | lang_output_section_statement_type *os; |
252b5132 | 3342 | |
d1778b88 | 3343 | os = &(s->output_section_statement); |
252b5132 RH |
3344 | if (os->bfd_section != NULL) |
3345 | { | |
3346 | dot = os->bfd_section->vma; | |
4a43e768 | 3347 | lang_do_assignments_1 (os->children.head, os, os->fill, dot); |
3737f867 JJ |
3348 | /* .tbss sections effectively have zero size. */ |
3349 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 | |
3350 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 | |
3351 | || link_info.relocatable) | |
eea6121a | 3352 | dot += TO_ADDR (os->bfd_section->size); |
252b5132 | 3353 | } |
c13b1b77 | 3354 | if (os->load_base) |
252b5132 RH |
3355 | { |
3356 | /* If nothing has been placed into the output section then | |
4de2d33d | 3357 | it won't have a bfd_section. */ |
5f992e62 | 3358 | if (os->bfd_section) |
252b5132 | 3359 | { |
5f992e62 | 3360 | os->bfd_section->lma |
08da4cac KH |
3361 | = exp_get_abs_int (os->load_base, 0, "load base", |
3362 | lang_final_phase_enum); | |
252b5132 RH |
3363 | } |
3364 | } | |
3365 | } | |
3366 | break; | |
3367 | case lang_wild_statement_enum: | |
3368 | ||
420e579c HPN |
3369 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
3370 | output_section_statement, | |
3371 | fill, dot); | |
252b5132 RH |
3372 | |
3373 | break; | |
3374 | ||
3375 | case lang_object_symbols_statement_enum: | |
3376 | case lang_output_statement_enum: | |
3377 | case lang_target_statement_enum: | |
3378 | #if 0 | |
3379 | case lang_common_statement_enum: | |
3380 | #endif | |
3381 | break; | |
3382 | case lang_data_statement_enum: | |
3383 | { | |
3384 | etree_value_type value; | |
3385 | ||
3386 | value = exp_fold_tree (s->data_statement.exp, | |
3387 | abs_output_section, | |
3388 | lang_final_phase_enum, dot, &dot); | |
7c519c12 | 3389 | if (!value.valid_p) |
252b5132 | 3390 | einfo (_("%F%P: invalid data statement\n")); |
384d938f NS |
3391 | s->data_statement.value |
3392 | = value.value + value.section->bfd_section->vma; | |
252b5132 | 3393 | } |
b7a26f91 KH |
3394 | { |
3395 | unsigned int size; | |
08da4cac KH |
3396 | switch (s->data_statement.type) |
3397 | { | |
3398 | default: | |
3399 | abort (); | |
3400 | case QUAD: | |
3401 | case SQUAD: | |
3402 | size = QUAD_SIZE; | |
3403 | break; | |
3404 | case LONG: | |
3405 | size = LONG_SIZE; | |
3406 | break; | |
3407 | case SHORT: | |
3408 | size = SHORT_SIZE; | |
3409 | break; | |
3410 | case BYTE: | |
3411 | size = BYTE_SIZE; | |
3412 | break; | |
3413 | } | |
e5caa5e0 AM |
3414 | if (size < TO_SIZE ((unsigned) 1)) |
3415 | size = TO_SIZE ((unsigned) 1); | |
3416 | dot += TO_ADDR (size); | |
08da4cac | 3417 | } |
252b5132 RH |
3418 | break; |
3419 | ||
3420 | case lang_reloc_statement_enum: | |
3421 | { | |
3422 | etree_value_type value; | |
3423 | ||
3424 | value = exp_fold_tree (s->reloc_statement.addend_exp, | |
3425 | abs_output_section, | |
3426 | lang_final_phase_enum, dot, &dot); | |
3427 | s->reloc_statement.addend_value = value.value; | |
7c519c12 | 3428 | if (!value.valid_p) |
252b5132 RH |
3429 | einfo (_("%F%P: invalid reloc statement\n")); |
3430 | } | |
e5caa5e0 | 3431 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
252b5132 RH |
3432 | break; |
3433 | ||
3434 | case lang_input_section_enum: | |
3435 | { | |
3436 | asection *in = s->input_section.section; | |
3437 | ||
57ceae94 | 3438 | if ((in->flags & SEC_EXCLUDE) == 0) |
eea6121a | 3439 | dot += TO_ADDR (in->size); |
252b5132 RH |
3440 | } |
3441 | break; | |
3442 | ||
3443 | case lang_input_statement_enum: | |
3444 | break; | |
3445 | case lang_fill_statement_enum: | |
3446 | fill = s->fill_statement.fill; | |
3447 | break; | |
3448 | case lang_assignment_statement_enum: | |
3449 | { | |
3450 | exp_fold_tree (s->assignment_statement.exp, | |
3451 | output_section_statement, | |
3452 | lang_final_phase_enum, | |
3453 | dot, | |
3454 | &dot); | |
3455 | } | |
3456 | ||
3457 | break; | |
3458 | case lang_padding_statement_enum: | |
e5caa5e0 | 3459 | dot += TO_ADDR (s->padding_statement.size); |
252b5132 RH |
3460 | break; |
3461 | ||
3462 | case lang_group_statement_enum: | |
420e579c HPN |
3463 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
3464 | output_section_statement, | |
3465 | fill, dot); | |
252b5132 RH |
3466 | |
3467 | break; | |
3468 | ||
3469 | default: | |
3470 | FAIL (); | |
3471 | break; | |
3472 | case lang_address_statement_enum: | |
3473 | break; | |
3474 | } | |
3475 | ||
3476 | } | |
3477 | return dot; | |
3478 | } | |
3479 | ||
f2241121 | 3480 | void |
6feb9908 AM |
3481 | lang_do_assignments |
3482 | (lang_statement_union_type *s, | |
3483 | lang_output_section_statement_type *output_section_statement, | |
3484 | fill_type *fill, | |
3485 | bfd_vma dot) | |
420e579c HPN |
3486 | { |
3487 | /* Callers of exp_fold_tree need to increment this. */ | |
3488 | lang_statement_iteration++; | |
3489 | lang_do_assignments_1 (s, output_section_statement, fill, dot); | |
3490 | } | |
3491 | ||
252b5132 RH |
3492 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
3493 | operator .startof. (section_name), it produces an undefined symbol | |
3494 | .startof.section_name. Similarly, when it sees | |
3495 | .sizeof. (section_name), it produces an undefined symbol | |
3496 | .sizeof.section_name. For all the output sections, we look for | |
3497 | such symbols, and set them to the correct value. */ | |
3498 | ||
3499 | static void | |
1579bae1 | 3500 | lang_set_startof (void) |
252b5132 RH |
3501 | { |
3502 | asection *s; | |
3503 | ||
1049f94e | 3504 | if (link_info.relocatable) |
252b5132 RH |
3505 | return; |
3506 | ||
3507 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
3508 | { | |
3509 | const char *secname; | |
3510 | char *buf; | |
3511 | struct bfd_link_hash_entry *h; | |
3512 | ||
3513 | secname = bfd_get_section_name (output_bfd, s); | |
3514 | buf = xmalloc (10 + strlen (secname)); | |
3515 | ||
3516 | sprintf (buf, ".startof.%s", secname); | |
b34976b6 | 3517 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
3518 | if (h != NULL && h->type == bfd_link_hash_undefined) |
3519 | { | |
3520 | h->type = bfd_link_hash_defined; | |
3521 | h->u.def.value = bfd_get_section_vma (output_bfd, s); | |
3522 | h->u.def.section = bfd_abs_section_ptr; | |
3523 | } | |
3524 | ||
3525 | sprintf (buf, ".sizeof.%s", secname); | |
b34976b6 | 3526 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
252b5132 RH |
3527 | if (h != NULL && h->type == bfd_link_hash_undefined) |
3528 | { | |
3529 | h->type = bfd_link_hash_defined; | |
eea6121a | 3530 | h->u.def.value = TO_ADDR (s->size); |
252b5132 RH |
3531 | h->u.def.section = bfd_abs_section_ptr; |
3532 | } | |
3533 | ||
3534 | free (buf); | |
3535 | } | |
3536 | } | |
3537 | ||
3538 | static void | |
1579bae1 | 3539 | lang_finish (void) |
252b5132 RH |
3540 | { |
3541 | struct bfd_link_hash_entry *h; | |
b34976b6 | 3542 | bfd_boolean warn; |
252b5132 | 3543 | |
1049f94e | 3544 | if (link_info.relocatable || link_info.shared) |
b34976b6 | 3545 | warn = FALSE; |
252b5132 | 3546 | else |
b34976b6 | 3547 | warn = TRUE; |
252b5132 | 3548 | |
1579bae1 | 3549 | if (entry_symbol.name == NULL) |
252b5132 RH |
3550 | { |
3551 | /* No entry has been specified. Look for start, but don't warn | |
3552 | if we don't find it. */ | |
e3e942e9 | 3553 | entry_symbol.name = "start"; |
b34976b6 | 3554 | warn = FALSE; |
252b5132 RH |
3555 | } |
3556 | ||
e3e942e9 | 3557 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
b34976b6 | 3558 | FALSE, FALSE, TRUE); |
1579bae1 | 3559 | if (h != NULL |
252b5132 RH |
3560 | && (h->type == bfd_link_hash_defined |
3561 | || h->type == bfd_link_hash_defweak) | |
3562 | && h->u.def.section->output_section != NULL) | |
3563 | { | |
3564 | bfd_vma val; | |
3565 | ||
3566 | val = (h->u.def.value | |
3567 | + bfd_get_section_vma (output_bfd, | |
3568 | h->u.def.section->output_section) | |
3569 | + h->u.def.section->output_offset); | |
3570 | if (! bfd_set_start_address (output_bfd, val)) | |
e3e942e9 | 3571 | einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name); |
252b5132 RH |
3572 | } |
3573 | else | |
3574 | { | |
3575 | bfd_vma val; | |
5f992e62 | 3576 | const char *send; |
252b5132 RH |
3577 | |
3578 | /* We couldn't find the entry symbol. Try parsing it as a | |
3579 | number. */ | |
e3e942e9 | 3580 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
252b5132 RH |
3581 | if (*send == '\0') |
3582 | { | |
3583 | if (! bfd_set_start_address (output_bfd, val)) | |
3584 | einfo (_("%P%F: can't set start address\n")); | |
3585 | } | |
3586 | else | |
3587 | { | |
3588 | asection *ts; | |
3589 | ||
3590 | /* Can't find the entry symbol, and it's not a number. Use | |
3591 | the first address in the text section. */ | |
1e281515 | 3592 | ts = bfd_get_section_by_name (output_bfd, entry_section); |
1579bae1 | 3593 | if (ts != NULL) |
252b5132 RH |
3594 | { |
3595 | if (warn) | |
6feb9908 AM |
3596 | einfo (_("%P: warning: cannot find entry symbol %s;" |
3597 | " defaulting to %V\n"), | |
e3e942e9 AM |
3598 | entry_symbol.name, |
3599 | bfd_get_section_vma (output_bfd, ts)); | |
252b5132 RH |
3600 | if (! bfd_set_start_address (output_bfd, |
3601 | bfd_get_section_vma (output_bfd, | |
3602 | ts))) | |
3603 | einfo (_("%P%F: can't set start address\n")); | |
3604 | } | |
3605 | else | |
3606 | { | |
3607 | if (warn) | |
6feb9908 AM |
3608 | einfo (_("%P: warning: cannot find entry symbol %s;" |
3609 | " not setting start address\n"), | |
e3e942e9 | 3610 | entry_symbol.name); |
252b5132 RH |
3611 | } |
3612 | } | |
3613 | } | |
420e579c | 3614 | |
7115639b AM |
3615 | /* Don't bfd_hash_table_free (&lang_definedness_table); |
3616 | map file output may result in a call of lang_track_definedness. */ | |
252b5132 RH |
3617 | } |
3618 | ||
3619 | /* This is a small function used when we want to ignore errors from | |
3620 | BFD. */ | |
3621 | ||
3622 | static void | |
87f2a346 | 3623 | ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) |
252b5132 RH |
3624 | { |
3625 | /* Don't do anything. */ | |
3626 | } | |
3627 | ||
3628 | /* Check that the architecture of all the input files is compatible | |
3629 | with the output file. Also call the backend to let it do any | |
3630 | other checking that is needed. */ | |
3631 | ||
3632 | static void | |
1579bae1 | 3633 | lang_check (void) |
252b5132 RH |
3634 | { |
3635 | lang_statement_union_type *file; | |
3636 | bfd *input_bfd; | |
5f992e62 | 3637 | const bfd_arch_info_type *compatible; |
252b5132 | 3638 | |
1579bae1 | 3639 | for (file = file_chain.head; file != NULL; file = file->input_statement.next) |
252b5132 RH |
3640 | { |
3641 | input_bfd = file->input_statement.the_bfd; | |
6feb9908 AM |
3642 | compatible |
3643 | = bfd_arch_get_compatible (input_bfd, output_bfd, | |
3644 | command_line.accept_unknown_input_arch); | |
30cba025 AM |
3645 | |
3646 | /* In general it is not possible to perform a relocatable | |
3647 | link between differing object formats when the input | |
3648 | file has relocations, because the relocations in the | |
3649 | input format may not have equivalent representations in | |
3650 | the output format (and besides BFD does not translate | |
3651 | relocs for other link purposes than a final link). */ | |
1049f94e | 3652 | if ((link_info.relocatable || link_info.emitrelocations) |
30cba025 | 3653 | && (compatible == NULL |
d35a52e2 | 3654 | || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd)) |
30cba025 AM |
3655 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
3656 | { | |
6feb9908 AM |
3657 | einfo (_("%P%F: Relocatable linking with relocations from" |
3658 | " format %s (%B) to format %s (%B) is not supported\n"), | |
30cba025 AM |
3659 | bfd_get_target (input_bfd), input_bfd, |
3660 | bfd_get_target (output_bfd), output_bfd); | |
3661 | /* einfo with %F exits. */ | |
3662 | } | |
3663 | ||
252b5132 RH |
3664 | if (compatible == NULL) |
3665 | { | |
3666 | if (command_line.warn_mismatch) | |
6feb9908 AM |
3667 | einfo (_("%P: warning: %s architecture of input file `%B'" |
3668 | " is incompatible with %s output\n"), | |
252b5132 RH |
3669 | bfd_printable_name (input_bfd), input_bfd, |
3670 | bfd_printable_name (output_bfd)); | |
3671 | } | |
b9247304 | 3672 | else if (bfd_count_sections (input_bfd)) |
252b5132 | 3673 | { |
b9247304 | 3674 | /* If the input bfd has no contents, it shouldn't set the |
b7a26f91 | 3675 | private data of the output bfd. */ |
b9247304 | 3676 | |
252b5132 RH |
3677 | bfd_error_handler_type pfn = NULL; |
3678 | ||
3679 | /* If we aren't supposed to warn about mismatched input | |
3680 | files, temporarily set the BFD error handler to a | |
3681 | function which will do nothing. We still want to call | |
3682 | bfd_merge_private_bfd_data, since it may set up | |
3683 | information which is needed in the output file. */ | |
3684 | if (! command_line.warn_mismatch) | |
3685 | pfn = bfd_set_error_handler (ignore_bfd_errors); | |
3686 | if (! bfd_merge_private_bfd_data (input_bfd, output_bfd)) | |
3687 | { | |
3688 | if (command_line.warn_mismatch) | |
6feb9908 AM |
3689 | einfo (_("%P%X: failed to merge target specific data" |
3690 | " of file %B\n"), input_bfd); | |
252b5132 RH |
3691 | } |
3692 | if (! command_line.warn_mismatch) | |
3693 | bfd_set_error_handler (pfn); | |
3694 | } | |
3695 | } | |
3696 | } | |
3697 | ||
3698 | /* Look through all the global common symbols and attach them to the | |
3699 | correct section. The -sort-common command line switch may be used | |
3700 | to roughly sort the entries by size. */ | |
3701 | ||
3702 | static void | |
1579bae1 | 3703 | lang_common (void) |
252b5132 | 3704 | { |
4818e05f AM |
3705 | if (command_line.inhibit_common_definition) |
3706 | return; | |
1049f94e | 3707 | if (link_info.relocatable |
252b5132 RH |
3708 | && ! command_line.force_common_definition) |
3709 | return; | |
3710 | ||
3711 | if (! config.sort_common) | |
1579bae1 | 3712 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
252b5132 RH |
3713 | else |
3714 | { | |
3715 | int power; | |
3716 | ||
3717 | for (power = 4; power >= 0; power--) | |
1579bae1 | 3718 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
252b5132 RH |
3719 | } |
3720 | } | |
3721 | ||
3722 | /* Place one common symbol in the correct section. */ | |
3723 | ||
b34976b6 | 3724 | static bfd_boolean |
1579bae1 | 3725 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
252b5132 RH |
3726 | { |
3727 | unsigned int power_of_two; | |
3728 | bfd_vma size; | |
3729 | asection *section; | |
3730 | ||
3731 | if (h->type != bfd_link_hash_common) | |
b34976b6 | 3732 | return TRUE; |
252b5132 RH |
3733 | |
3734 | size = h->u.c.size; | |
3735 | power_of_two = h->u.c.p->alignment_power; | |
3736 | ||
3737 | if (config.sort_common | |
3738 | && power_of_two < (unsigned int) *(int *) info) | |
b34976b6 | 3739 | return TRUE; |
252b5132 RH |
3740 | |
3741 | section = h->u.c.p->section; | |
3742 | ||
e5caa5e0 | 3743 | /* Increase the size of the section to align the common sym. */ |
eea6121a AM |
3744 | section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1; |
3745 | section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift)); | |
252b5132 RH |
3746 | |
3747 | /* Adjust the alignment if necessary. */ | |
3748 | if (power_of_two > section->alignment_power) | |
3749 | section->alignment_power = power_of_two; | |
3750 | ||
3751 | /* Change the symbol from common to defined. */ | |
3752 | h->type = bfd_link_hash_defined; | |
3753 | h->u.def.section = section; | |
eea6121a | 3754 | h->u.def.value = section->size; |
252b5132 RH |
3755 | |
3756 | /* Increase the size of the section. */ | |
eea6121a | 3757 | section->size += size; |
252b5132 RH |
3758 | |
3759 | /* Make sure the section is allocated in memory, and make sure that | |
3760 | it is no longer a common section. */ | |
3761 | section->flags |= SEC_ALLOC; | |
08da4cac | 3762 | section->flags &= ~SEC_IS_COMMON; |
252b5132 RH |
3763 | |
3764 | if (config.map_file != NULL) | |
3765 | { | |
b34976b6 | 3766 | static bfd_boolean header_printed; |
252b5132 RH |
3767 | int len; |
3768 | char *name; | |
3769 | char buf[50]; | |
3770 | ||
3771 | if (! header_printed) | |
3772 | { | |
3773 | minfo (_("\nAllocating common symbols\n")); | |
3774 | minfo (_("Common symbol size file\n\n")); | |
b34976b6 | 3775 | header_printed = TRUE; |
252b5132 RH |
3776 | } |
3777 | ||
3778 | name = demangle (h->root.string); | |
3779 | minfo ("%s", name); | |
3780 | len = strlen (name); | |
3781 | free (name); | |
3782 | ||
3783 | if (len >= 19) | |
3784 | { | |
3785 | print_nl (); | |
3786 | len = 0; | |
3787 | } | |
3788 | while (len < 20) | |
3789 | { | |
3790 | print_space (); | |
3791 | ++len; | |
3792 | } | |
3793 | ||
3794 | minfo ("0x"); | |
3795 | if (size <= 0xffffffff) | |
3796 | sprintf (buf, "%lx", (unsigned long) size); | |
3797 | else | |
3798 | sprintf_vma (buf, size); | |
3799 | minfo ("%s", buf); | |
3800 | len = strlen (buf); | |
3801 | ||
3802 | while (len < 16) | |
3803 | { | |
3804 | print_space (); | |
3805 | ++len; | |
3806 | } | |
3807 | ||
3808 | minfo ("%B\n", section->owner); | |
3809 | } | |
3810 | ||
b34976b6 | 3811 | return TRUE; |
252b5132 RH |
3812 | } |
3813 | ||
08da4cac KH |
3814 | /* Run through the input files and ensure that every input section has |
3815 | somewhere to go. If one is found without a destination then create | |
3816 | an input request and place it into the statement tree. */ | |
252b5132 RH |
3817 | |
3818 | static void | |
1579bae1 | 3819 | lang_place_orphans (void) |
252b5132 | 3820 | { |
e50d8076 | 3821 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
252b5132 RH |
3822 | { |
3823 | asection *s; | |
3824 | ||
1579bae1 | 3825 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
252b5132 | 3826 | { |
1579bae1 | 3827 | if (s->output_section == NULL) |
252b5132 | 3828 | { |
396a2467 | 3829 | /* This section of the file is not attached, root |
08da4cac | 3830 | around for a sensible place for it to go. */ |
252b5132 RH |
3831 | |
3832 | if (file->just_syms_flag) | |
164e712d AM |
3833 | abort (); |
3834 | ||
3835 | if ((s->flags & SEC_EXCLUDE) != 0) | |
3836 | s->output_section = bfd_abs_section_ptr; | |
252b5132 RH |
3837 | else if (strcmp (s->name, "COMMON") == 0) |
3838 | { | |
3839 | /* This is a lonely common section which must have | |
3840 | come from an archive. We attach to the section | |
3841 | with the wildcard. */ | |
1049f94e | 3842 | if (! link_info.relocatable |
252b5132 RH |
3843 | || command_line.force_common_definition) |
3844 | { | |
3845 | if (default_common_section == NULL) | |
3846 | { | |
3847 | #if 0 | |
3848 | /* This message happens when using the | |
3849 | svr3.ifile linker script, so I have | |
3850 | disabled it. */ | |
6feb9908 AM |
3851 | info_msg (_("%P: no [COMMON] command," |
3852 | " defaulting to .bss\n")); | |
252b5132 RH |
3853 | #endif |
3854 | default_common_section = | |
3855 | lang_output_section_statement_lookup (".bss"); | |
3856 | ||
3857 | } | |
39dcfe18 AM |
3858 | lang_add_section (&default_common_section->children, s, |
3859 | default_common_section, file); | |
252b5132 RH |
3860 | } |
3861 | } | |
3862 | else if (ldemul_place_orphan (file, s)) | |
3863 | ; | |
3864 | else | |
3865 | { | |
39dcfe18 | 3866 | lang_output_section_statement_type *os; |
252b5132 | 3867 | |
39dcfe18 AM |
3868 | os = lang_output_section_statement_lookup (s->name); |
3869 | lang_add_section (&os->children, s, os, file); | |
252b5132 RH |
3870 | } |
3871 | } | |
3872 | } | |
3873 | } | |
3874 | } | |
3875 | ||
252b5132 | 3876 | void |
1579bae1 | 3877 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
252b5132 | 3878 | { |
aa8804e4 | 3879 | flagword *ptr_flags; |
252b5132 | 3880 | |
aa8804e4 | 3881 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
252b5132 RH |
3882 | while (*flags) |
3883 | { | |
3884 | switch (*flags) | |
3885 | { | |
252b5132 RH |
3886 | case 'A': case 'a': |
3887 | *ptr_flags |= SEC_ALLOC; | |
3888 | break; | |
3889 | ||
3890 | case 'R': case 'r': | |
3891 | *ptr_flags |= SEC_READONLY; | |
3892 | break; | |
3893 | ||
3894 | case 'W': case 'w': | |
3895 | *ptr_flags |= SEC_DATA; | |
3896 | break; | |
3897 | ||
3898 | case 'X': case 'x': | |
3899 | *ptr_flags |= SEC_CODE; | |
3900 | break; | |
3901 | ||
3902 | case 'L': case 'l': | |
3903 | case 'I': case 'i': | |
3904 | *ptr_flags |= SEC_LOAD; | |
3905 | break; | |
3906 | ||
3907 | default: | |
3908 | einfo (_("%P%F: invalid syntax in flags\n")); | |
3909 | break; | |
3910 | } | |
3911 | flags++; | |
3912 | } | |
3913 | } | |
3914 | ||
3915 | /* Call a function on each input file. This function will be called | |
3916 | on an archive, but not on the elements. */ | |
3917 | ||
3918 | void | |
1579bae1 | 3919 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
252b5132 RH |
3920 | { |
3921 | lang_input_statement_type *f; | |
3922 | ||
3923 | for (f = (lang_input_statement_type *) input_file_chain.head; | |
3924 | f != NULL; | |
3925 | f = (lang_input_statement_type *) f->next_real_file) | |
3926 | func (f); | |
3927 | } | |
3928 | ||
3929 | /* Call a function on each file. The function will be called on all | |
3930 | the elements of an archive which are included in the link, but will | |
3931 | not be called on the archive file itself. */ | |
3932 | ||
3933 | void | |
1579bae1 | 3934 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
252b5132 | 3935 | { |
e50d8076 | 3936 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
252b5132 RH |
3937 | { |
3938 | func (f); | |
3939 | } | |
3940 | } | |
3941 | ||
252b5132 | 3942 | void |
1579bae1 | 3943 | ldlang_add_file (lang_input_statement_type *entry) |
252b5132 RH |
3944 | { |
3945 | bfd **pp; | |
3946 | ||
3947 | lang_statement_append (&file_chain, | |
3948 | (lang_statement_union_type *) entry, | |
3949 | &entry->next); | |
3950 | ||
3951 | /* The BFD linker needs to have a list of all input BFDs involved in | |
3952 | a link. */ | |
1579bae1 | 3953 | ASSERT (entry->the_bfd->link_next == NULL); |
252b5132 | 3954 | ASSERT (entry->the_bfd != output_bfd); |
1579bae1 | 3955 | for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next) |
252b5132 RH |
3956 | ; |
3957 | *pp = entry->the_bfd; | |
1579bae1 | 3958 | entry->the_bfd->usrdata = entry; |
252b5132 RH |
3959 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
3960 | ||
3961 | /* Look through the sections and check for any which should not be | |
3962 | included in the link. We need to do this now, so that we can | |
3963 | notice when the backend linker tries to report multiple | |
3964 | definition errors for symbols which are in sections we aren't | |
3965 | going to link. FIXME: It might be better to entirely ignore | |
3966 | symbols which are defined in sections which are going to be | |
3967 | discarded. This would require modifying the backend linker for | |
3968 | each backend which might set the SEC_LINK_ONCE flag. If we do | |
3969 | this, we should probably handle SEC_EXCLUDE in the same way. */ | |
3970 | ||
1579bae1 | 3971 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
252b5132 RH |
3972 | } |
3973 | ||
3974 | void | |
1579bae1 | 3975 | lang_add_output (const char *name, int from_script) |
252b5132 RH |
3976 | { |
3977 | /* Make -o on command line override OUTPUT in script. */ | |
7c519c12 | 3978 | if (!had_output_filename || !from_script) |
252b5132 RH |
3979 | { |
3980 | output_filename = name; | |
b34976b6 | 3981 | had_output_filename = TRUE; |
252b5132 RH |
3982 | } |
3983 | } | |
3984 | ||
252b5132 RH |
3985 | static lang_output_section_statement_type *current_section; |
3986 | ||
3987 | static int | |
1579bae1 | 3988 | topower (int x) |
252b5132 RH |
3989 | { |
3990 | unsigned int i = 1; | |
3991 | int l; | |
3992 | ||
3993 | if (x < 0) | |
3994 | return -1; | |
3995 | ||
5f992e62 | 3996 | for (l = 0; l < 32; l++) |
252b5132 RH |
3997 | { |
3998 | if (i >= (unsigned int) x) | |
3999 | return l; | |
4000 | i <<= 1; | |
4001 | } | |
4002 | ||
4003 | return 0; | |
4004 | } | |
4005 | ||
aea4bd9d | 4006 | lang_output_section_statement_type * |
1579bae1 AM |
4007 | lang_enter_output_section_statement (const char *output_section_statement_name, |
4008 | etree_type *address_exp, | |
4009 | enum section_type sectype, | |
1579bae1 AM |
4010 | etree_type *align, |
4011 | etree_type *subalign, | |
0841712e JJ |
4012 | etree_type *ebase, |
4013 | int constraint) | |
252b5132 RH |
4014 | { |
4015 | lang_output_section_statement_type *os; | |
4016 | ||
4017 | current_section = | |
4018 | os = | |
0841712e JJ |
4019 | lang_output_section_statement_lookup_1 (output_section_statement_name, |
4020 | constraint); | |
252b5132 | 4021 | |
08da4cac KH |
4022 | /* Add this statement to tree. */ |
4023 | #if 0 | |
4024 | add_statement (lang_output_section_statement_enum, | |
4025 | output_section_statement); | |
4026 | #endif | |
4027 | /* Make next things chain into subchain of this. */ | |
252b5132 | 4028 | |
1579bae1 | 4029 | if (os->addr_tree == NULL) |
08da4cac KH |
4030 | { |
4031 | os->addr_tree = address_exp; | |
4032 | } | |
252b5132 RH |
4033 | os->sectype = sectype; |
4034 | if (sectype != noload_section) | |
4035 | os->flags = SEC_NO_FLAGS; | |
4036 | else | |
4037 | os->flags = SEC_NEVER_LOAD; | |
e5caa5e0 | 4038 | os->block_value = 1; |
252b5132 RH |
4039 | stat_ptr = &os->children; |
4040 | ||
08da4cac KH |
4041 | os->subsection_alignment = |
4042 | topower (exp_get_value_int (subalign, -1, "subsection alignment", 0)); | |
4043 | os->section_alignment = | |
4044 | topower (exp_get_value_int (align, -1, "section alignment", 0)); | |
252b5132 RH |
4045 | |
4046 | os->load_base = ebase; | |
aea4bd9d | 4047 | return os; |
252b5132 RH |
4048 | } |
4049 | ||
252b5132 | 4050 | void |
1579bae1 | 4051 | lang_final (void) |
252b5132 RH |
4052 | { |
4053 | lang_output_statement_type *new = | |
4054 | new_stat (lang_output_statement, stat_ptr); | |
4055 | ||
4056 | new->name = output_filename; | |
4057 | } | |
4058 | ||
08da4cac KH |
4059 | /* Reset the current counters in the regions. */ |
4060 | ||
e3dc8847 | 4061 | void |
1579bae1 | 4062 | lang_reset_memory_regions (void) |
252b5132 RH |
4063 | { |
4064 | lang_memory_region_type *p = lang_memory_region_list; | |
b3327aad | 4065 | asection *o; |
252b5132 | 4066 | |
1579bae1 | 4067 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
252b5132 RH |
4068 | { |
4069 | p->old_length = (bfd_size_type) (p->current - p->origin); | |
4070 | p->current = p->origin; | |
4071 | } | |
b3327aad AM |
4072 | |
4073 | for (o = output_bfd->sections; o != NULL; o = o->next) | |
1a23a9e6 AM |
4074 | { |
4075 | /* Save the last size for possible use by bfd_relax_section. */ | |
4076 | o->rawsize = o->size; | |
4077 | o->size = 0; | |
4078 | } | |
252b5132 RH |
4079 | } |
4080 | ||
164e712d | 4081 | /* Worker for lang_gc_sections_1. */ |
252b5132 RH |
4082 | |
4083 | static void | |
1579bae1 AM |
4084 | gc_section_callback (lang_wild_statement_type *ptr, |
4085 | struct wildcard_list *sec ATTRIBUTE_UNUSED, | |
4086 | asection *section, | |
4087 | lang_input_statement_type *file ATTRIBUTE_UNUSED, | |
4088 | void *data ATTRIBUTE_UNUSED) | |
252b5132 | 4089 | { |
164e712d AM |
4090 | /* If the wild pattern was marked KEEP, the member sections |
4091 | should be as well. */ | |
4dec4d4e RH |
4092 | if (ptr->keep_sections) |
4093 | section->flags |= SEC_KEEP; | |
252b5132 RH |
4094 | } |
4095 | ||
252b5132 RH |
4096 | /* Iterate over sections marking them against GC. */ |
4097 | ||
4098 | static void | |
1579bae1 | 4099 | lang_gc_sections_1 (lang_statement_union_type *s) |
252b5132 | 4100 | { |
1579bae1 | 4101 | for (; s != NULL; s = s->header.next) |
252b5132 RH |
4102 | { |
4103 | switch (s->header.type) | |
4104 | { | |
4105 | case lang_wild_statement_enum: | |
164e712d | 4106 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
abc6ab0a | 4107 | break; |
252b5132 RH |
4108 | case lang_constructors_statement_enum: |
4109 | lang_gc_sections_1 (constructor_list.head); | |
4110 | break; | |
4111 | case lang_output_section_statement_enum: | |
4112 | lang_gc_sections_1 (s->output_section_statement.children.head); | |
4113 | break; | |
4114 | case lang_group_statement_enum: | |
4115 | lang_gc_sections_1 (s->group_statement.children.head); | |
4116 | break; | |
4117 | default: | |
4118 | break; | |
4119 | } | |
4120 | } | |
4121 | } | |
4122 | ||
4123 | static void | |
1579bae1 | 4124 | lang_gc_sections (void) |
252b5132 RH |
4125 | { |
4126 | struct bfd_link_hash_entry *h; | |
e3e942e9 | 4127 | ldlang_undef_chain_list_type *ulist; |
252b5132 RH |
4128 | |
4129 | /* Keep all sections so marked in the link script. */ | |
4130 | ||
4131 | lang_gc_sections_1 (statement_list.head); | |
4132 | ||
e3e942e9 AM |
4133 | /* Keep all sections containing symbols undefined on the command-line, |
4134 | and the section containing the entry symbol. */ | |
252b5132 | 4135 | |
e3e942e9 | 4136 | for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next) |
252b5132 | 4137 | { |
5f992e62 | 4138 | h = bfd_link_hash_lookup (link_info.hash, ulist->name, |
b34976b6 | 4139 | FALSE, FALSE, FALSE); |
252b5132 | 4140 | |
1579bae1 | 4141 | if (h != NULL |
08da4cac KH |
4142 | && (h->type == bfd_link_hash_defined |
4143 | || h->type == bfd_link_hash_defweak) | |
252b5132 RH |
4144 | && ! bfd_is_abs_section (h->u.def.section)) |
4145 | { | |
4146 | h->u.def.section->flags |= SEC_KEEP; | |
4147 | } | |
4148 | } | |
4149 | ||
9ca57817 AM |
4150 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
4151 | the special case of debug info. (See bfd/stabs.c) | |
4152 | Twiddle the flag here, to simplify later linker code. */ | |
4153 | if (link_info.relocatable) | |
4154 | { | |
4155 | LANG_FOR_EACH_INPUT_STATEMENT (f) | |
4156 | { | |
4157 | asection *sec; | |
4158 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) | |
4159 | if ((sec->flags & SEC_DEBUGGING) == 0) | |
4160 | sec->flags &= ~SEC_EXCLUDE; | |
4161 | } | |
4162 | } | |
4163 | ||
164e712d AM |
4164 | if (command_line.gc_sections) |
4165 | bfd_gc_sections (output_bfd, &link_info); | |
252b5132 RH |
4166 | } |
4167 | ||
4168 | void | |
1579bae1 | 4169 | lang_process (void) |
252b5132 RH |
4170 | { |
4171 | lang_reasonable_defaults (); | |
4172 | current_target = default_target; | |
4173 | ||
08da4cac KH |
4174 | /* Open the output file. */ |
4175 | lang_for_each_statement (ldlang_open_output); | |
e5caa5e0 | 4176 | init_opb (); |
252b5132 RH |
4177 | |
4178 | ldemul_create_output_section_statements (); | |
4179 | ||
08da4cac | 4180 | /* Add to the hash table all undefineds on the command line. */ |
252b5132 RH |
4181 | lang_place_undefineds (); |
4182 | ||
082b7297 L |
4183 | if (!bfd_section_already_linked_table_init ()) |
4184 | einfo (_("%P%F: Failed to create hash table\n")); | |
9503fd87 | 4185 | |
08da4cac | 4186 | /* Create a bfd for each input file. */ |
252b5132 | 4187 | current_target = default_target; |
b34976b6 | 4188 | open_input_bfds (statement_list.head, FALSE); |
252b5132 | 4189 | |
e3e942e9 AM |
4190 | link_info.gc_sym_list = &entry_symbol; |
4191 | if (entry_symbol.name == NULL) | |
4192 | link_info.gc_sym_list = ldlang_undef_chain_list_head; | |
4193 | ||
252b5132 RH |
4194 | ldemul_after_open (); |
4195 | ||
082b7297 | 4196 | bfd_section_already_linked_table_free (); |
9503fd87 | 4197 | |
252b5132 RH |
4198 | /* Make sure that we're not mixing architectures. We call this |
4199 | after all the input files have been opened, but before we do any | |
4200 | other processing, so that any operations merge_private_bfd_data | |
4201 | does on the output file will be known during the rest of the | |
4202 | link. */ | |
4203 | lang_check (); | |
4204 | ||
4205 | /* Handle .exports instead of a version script if we're told to do so. */ | |
4206 | if (command_line.version_exports_section) | |
4207 | lang_do_version_exports_section (); | |
4208 | ||
4209 | /* Build all sets based on the information gathered from the input | |
4210 | files. */ | |
4211 | ldctor_build_sets (); | |
4212 | ||
4213 | /* Remove unreferenced sections if asked to. */ | |
164e712d | 4214 | lang_gc_sections (); |
252b5132 | 4215 | |
08da4cac | 4216 | /* Size up the common data. */ |
252b5132 RH |
4217 | lang_common (); |
4218 | ||
4219 | /* Run through the contours of the script and attach input sections | |
08da4cac | 4220 | to the correct output sections. */ |
1579bae1 | 4221 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
252b5132 | 4222 | |
08da4cac | 4223 | /* Find any sections not attached explicitly and handle them. */ |
252b5132 RH |
4224 | lang_place_orphans (); |
4225 | ||
1049f94e | 4226 | if (! link_info.relocatable) |
862120bd | 4227 | { |
57ceae94 AM |
4228 | asection *found; |
4229 | ||
4230 | /* Merge SEC_MERGE sections. This has to be done after GC of | |
4231 | sections, so that GCed sections are not merged, but before | |
4232 | assigning dynamic symbols, since removing whole input sections | |
4233 | is hard then. */ | |
4234 | bfd_merge_sections (output_bfd, &link_info); | |
4235 | ||
862120bd | 4236 | /* Look for a text section and set the readonly attribute in it. */ |
57ceae94 | 4237 | found = bfd_get_section_by_name (output_bfd, ".text"); |
862120bd | 4238 | |
1579bae1 | 4239 | if (found != NULL) |
862120bd AM |
4240 | { |
4241 | if (config.text_read_only) | |
4242 | found->flags |= SEC_READONLY; | |
4243 | else | |
4244 | found->flags &= ~SEC_READONLY; | |
4245 | } | |
4246 | } | |
4247 | ||
4248 | /* Do anything special before sizing sections. This is where ELF | |
4249 | and other back-ends size dynamic sections. */ | |
252b5132 RH |
4250 | ldemul_before_allocation (); |
4251 | ||
1049f94e | 4252 | if (!link_info.relocatable) |
4bd5a393 AM |
4253 | strip_excluded_output_sections (); |
4254 | ||
252b5132 RH |
4255 | /* We must record the program headers before we try to fix the |
4256 | section positions, since they will affect SIZEOF_HEADERS. */ | |
4257 | lang_record_phdrs (); | |
4258 | ||
b3327aad | 4259 | /* Size up the sections. */ |
1579bae1 AM |
4260 | lang_size_sections (statement_list.head, abs_output_section, |
4261 | &statement_list.head, 0, 0, NULL, | |
cf888e70 | 4262 | command_line.relax ? FALSE : TRUE); |
b3327aad | 4263 | |
08da4cac | 4264 | /* Now run around and relax if we can. */ |
252b5132 RH |
4265 | if (command_line.relax) |
4266 | { | |
252b5132 | 4267 | /* Keep relaxing until bfd_relax_section gives up. */ |
b34976b6 | 4268 | bfd_boolean relax_again; |
b3327aad | 4269 | |
252b5132 RH |
4270 | do |
4271 | { | |
b34976b6 | 4272 | relax_again = FALSE; |
252b5132 RH |
4273 | |
4274 | /* Note: pe-dll.c does something like this also. If you find | |
4275 | you need to change this code, you probably need to change | |
08da4cac | 4276 | pe-dll.c also. DJ */ |
252b5132 RH |
4277 | |
4278 | /* Do all the assignments with our current guesses as to | |
4279 | section sizes. */ | |
1579bae1 AM |
4280 | lang_do_assignments (statement_list.head, abs_output_section, |
4281 | NULL, 0); | |
252b5132 | 4282 | |
5a46fe39 | 4283 | /* We must do this after lang_do_assignments, because it uses |
eea6121a | 4284 | size. */ |
5a46fe39 JW |
4285 | lang_reset_memory_regions (); |
4286 | ||
252b5132 | 4287 | /* Perform another relax pass - this time we know where the |
0d6d936f | 4288 | globals are, so can make a better guess. */ |
1579bae1 AM |
4289 | lang_size_sections (statement_list.head, abs_output_section, |
4290 | &statement_list.head, 0, 0, &relax_again, FALSE); | |
c7996ad6 L |
4291 | |
4292 | /* If the normal relax is done and the relax finalize pass | |
4293 | is not performed yet, we perform another relax pass. */ | |
d9c458fc | 4294 | if (!relax_again && link_info.need_relax_finalize) |
c7996ad6 | 4295 | { |
d9c458fc | 4296 | link_info.need_relax_finalize = FALSE; |
c7996ad6 L |
4297 | relax_again = TRUE; |
4298 | } | |
252b5132 RH |
4299 | } |
4300 | while (relax_again); | |
cf888e70 NC |
4301 | |
4302 | /* Final extra sizing to report errors. */ | |
1579bae1 | 4303 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
5a46fe39 | 4304 | lang_reset_memory_regions (); |
1579bae1 AM |
4305 | lang_size_sections (statement_list.head, abs_output_section, |
4306 | &statement_list.head, 0, 0, NULL, TRUE); | |
252b5132 | 4307 | } |
252b5132 RH |
4308 | |
4309 | /* See if anything special should be done now we know how big | |
4310 | everything is. */ | |
4311 | ldemul_after_allocation (); | |
4312 | ||
4313 | /* Fix any .startof. or .sizeof. symbols. */ | |
4314 | lang_set_startof (); | |
4315 | ||
08da4cac KH |
4316 | /* Do all the assignments, now that we know the final resting places |
4317 | of all the symbols. */ | |
252b5132 | 4318 | |
1579bae1 | 4319 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
252b5132 RH |
4320 | |
4321 | /* Make sure that the section addresses make sense. */ | |
1049f94e | 4322 | if (! link_info.relocatable |
252b5132 RH |
4323 | && command_line.check_section_addresses) |
4324 | lang_check_section_addresses (); | |
5f992e62 | 4325 | |
08da4cac | 4326 | /* Final stuffs. */ |
252b5132 RH |
4327 | |
4328 | ldemul_finish (); | |
4329 | lang_finish (); | |
4330 | } | |
4331 | ||
4332 | /* EXPORTED TO YACC */ | |
4333 | ||
4334 | void | |
1579bae1 AM |
4335 | lang_add_wild (struct wildcard_spec *filespec, |
4336 | struct wildcard_list *section_list, | |
4337 | bfd_boolean keep_sections) | |
252b5132 | 4338 | { |
b6bf44ba AM |
4339 | struct wildcard_list *curr, *next; |
4340 | lang_wild_statement_type *new; | |
4341 | ||
4342 | /* Reverse the list as the parser puts it back to front. */ | |
4343 | for (curr = section_list, section_list = NULL; | |
4344 | curr != NULL; | |
4345 | section_list = curr, curr = next) | |
4346 | { | |
4347 | if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0) | |
b34976b6 | 4348 | placed_commons = TRUE; |
252b5132 | 4349 | |
b6bf44ba AM |
4350 | next = curr->next; |
4351 | curr->next = section_list; | |
4352 | } | |
4353 | ||
4354 | if (filespec != NULL && filespec->name != NULL) | |
252b5132 | 4355 | { |
b6bf44ba AM |
4356 | if (strcmp (filespec->name, "*") == 0) |
4357 | filespec->name = NULL; | |
4358 | else if (! wildcardp (filespec->name)) | |
b34976b6 | 4359 | lang_has_input_file = TRUE; |
252b5132 | 4360 | } |
b6bf44ba AM |
4361 | |
4362 | new = new_stat (lang_wild_statement, stat_ptr); | |
4363 | new->filename = NULL; | |
b34976b6 | 4364 | new->filenames_sorted = FALSE; |
b6bf44ba | 4365 | if (filespec != NULL) |
252b5132 | 4366 | { |
b6bf44ba AM |
4367 | new->filename = filespec->name; |
4368 | new->filenames_sorted = filespec->sorted; | |
252b5132 | 4369 | } |
b6bf44ba | 4370 | new->section_list = section_list; |
252b5132 | 4371 | new->keep_sections = keep_sections; |
252b5132 RH |
4372 | lang_list_init (&new->children); |
4373 | } | |
4374 | ||
4375 | void | |
1579bae1 | 4376 | lang_section_start (const char *name, etree_type *address) |
252b5132 | 4377 | { |
d1778b88 | 4378 | lang_address_statement_type *ad; |
252b5132 | 4379 | |
d1778b88 | 4380 | ad = new_stat (lang_address_statement, stat_ptr); |
252b5132 RH |
4381 | ad->section_name = name; |
4382 | ad->address = address; | |
4383 | } | |
4384 | ||
4385 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called | |
4386 | because of a -e argument on the command line, or zero if this is | |
4387 | called by ENTRY in a linker script. Command line arguments take | |
4388 | precedence. */ | |
4389 | ||
4390 | void | |
1579bae1 | 4391 | lang_add_entry (const char *name, bfd_boolean cmdline) |
252b5132 | 4392 | { |
e3e942e9 | 4393 | if (entry_symbol.name == NULL |
252b5132 RH |
4394 | || cmdline |
4395 | || ! entry_from_cmdline) | |
4396 | { | |
e3e942e9 | 4397 | entry_symbol.name = name; |
252b5132 RH |
4398 | entry_from_cmdline = cmdline; |
4399 | } | |
4400 | } | |
4401 | ||
4402 | void | |
1579bae1 | 4403 | lang_add_target (const char *name) |
252b5132 RH |
4404 | { |
4405 | lang_target_statement_type *new = new_stat (lang_target_statement, | |
4406 | stat_ptr); | |
4407 | ||
4408 | new->target = name; | |
4409 | ||
4410 | } | |
4411 | ||
4412 | void | |
1579bae1 | 4413 | lang_add_map (const char *name) |
252b5132 RH |
4414 | { |
4415 | while (*name) | |
4416 | { | |
4417 | switch (*name) | |
4418 | { | |
08da4cac | 4419 | case 'F': |
b34976b6 | 4420 | map_option_f = TRUE; |
252b5132 RH |
4421 | break; |
4422 | } | |
4423 | name++; | |
4424 | } | |
4425 | } | |
4426 | ||
4427 | void | |
1579bae1 | 4428 | lang_add_fill (fill_type *fill) |
252b5132 RH |
4429 | { |
4430 | lang_fill_statement_type *new = new_stat (lang_fill_statement, | |
4431 | stat_ptr); | |
4432 | ||
2c382fb6 | 4433 | new->fill = fill; |
252b5132 RH |
4434 | } |
4435 | ||
4436 | void | |
1579bae1 | 4437 | lang_add_data (int type, union etree_union *exp) |
252b5132 RH |
4438 | { |
4439 | ||
4440 | lang_data_statement_type *new = new_stat (lang_data_statement, | |
4441 | stat_ptr); | |
4442 | ||
4443 | new->exp = exp; | |
4444 | new->type = type; | |
4445 | ||
4446 | } | |
4447 | ||
4448 | /* Create a new reloc statement. RELOC is the BFD relocation type to | |
4449 | generate. HOWTO is the corresponding howto structure (we could | |
4450 | look this up, but the caller has already done so). SECTION is the | |
4451 | section to generate a reloc against, or NAME is the name of the | |
4452 | symbol to generate a reloc against. Exactly one of SECTION and | |
4453 | NAME must be NULL. ADDEND is an expression for the addend. */ | |
4454 | ||
4455 | void | |
1579bae1 AM |
4456 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
4457 | reloc_howto_type *howto, | |
4458 | asection *section, | |
4459 | const char *name, | |
4460 | union etree_union *addend) | |
252b5132 RH |
4461 | { |
4462 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); | |
5f992e62 | 4463 | |
252b5132 RH |
4464 | p->reloc = reloc; |
4465 | p->howto = howto; | |
4466 | p->section = section; | |
4467 | p->name = name; | |
4468 | p->addend_exp = addend; | |
4469 | ||
4470 | p->addend_value = 0; | |
4471 | p->output_section = NULL; | |
4472 | p->output_vma = 0; | |
4473 | } | |
4474 | ||
4475 | lang_assignment_statement_type * | |
1579bae1 | 4476 | lang_add_assignment (etree_type *exp) |
252b5132 RH |
4477 | { |
4478 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, | |
4479 | stat_ptr); | |
4480 | ||
4481 | new->exp = exp; | |
4482 | return new; | |
4483 | } | |
4484 | ||
4485 | void | |
1579bae1 | 4486 | lang_add_attribute (enum statement_enum attribute) |
252b5132 RH |
4487 | { |
4488 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); | |
4489 | } | |
4490 | ||
4491 | void | |
1579bae1 | 4492 | lang_startup (const char *name) |
252b5132 | 4493 | { |
1579bae1 | 4494 | if (startup_file != NULL) |
252b5132 RH |
4495 | { |
4496 | einfo (_("%P%Fmultiple STARTUP files\n")); | |
4497 | } | |
4498 | first_file->filename = name; | |
4499 | first_file->local_sym_name = name; | |
b34976b6 | 4500 | first_file->real = TRUE; |
252b5132 RH |
4501 | |
4502 | startup_file = name; | |
4503 | } | |
4504 | ||
4505 | void | |
1579bae1 | 4506 | lang_float (bfd_boolean maybe) |
252b5132 RH |
4507 | { |
4508 | lang_float_flag = maybe; | |
4509 | } | |
4510 | ||
ee3cc2e2 RS |
4511 | |
4512 | /* Work out the load- and run-time regions from a script statement, and | |
4513 | store them in *LMA_REGION and *REGION respectively. | |
4514 | ||
a747ee4d NC |
4515 | MEMSPEC is the name of the run-time region, or the value of |
4516 | DEFAULT_MEMORY_REGION if the statement didn't specify one. | |
4517 | LMA_MEMSPEC is the name of the load-time region, or null if the | |
4518 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement | |
4519 | had an explicit load address. | |
ee3cc2e2 RS |
4520 | |
4521 | It is an error to specify both a load region and a load address. */ | |
4522 | ||
4523 | static void | |
6bdafbeb NC |
4524 | lang_get_regions (lang_memory_region_type **region, |
4525 | lang_memory_region_type **lma_region, | |
1579bae1 AM |
4526 | const char *memspec, |
4527 | const char *lma_memspec, | |
6bdafbeb NC |
4528 | bfd_boolean have_lma, |
4529 | bfd_boolean have_vma) | |
ee3cc2e2 | 4530 | { |
a747ee4d | 4531 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
ee3cc2e2 | 4532 | |
6feb9908 AM |
4533 | /* If no runtime region or VMA has been specified, but the load region |
4534 | has been specified, then use the load region for the runtime region | |
4535 | as well. */ | |
6bdafbeb NC |
4536 | if (lma_memspec != NULL |
4537 | && ! have_vma | |
4538 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) | |
ee3cc2e2 RS |
4539 | *region = *lma_region; |
4540 | else | |
a747ee4d | 4541 | *region = lang_memory_region_lookup (memspec, FALSE); |
ee3cc2e2 | 4542 | |
6bdafbeb | 4543 | if (have_lma && lma_memspec != 0) |
ee3cc2e2 RS |
4544 | einfo (_("%X%P:%S: section has both a load address and a load region\n")); |
4545 | } | |
4546 | ||
252b5132 | 4547 | void |
6bdafbeb NC |
4548 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
4549 | lang_output_section_phdr_list *phdrs, | |
4550 | const char *lma_memspec) | |
252b5132 | 4551 | { |
ee3cc2e2 RS |
4552 | lang_get_regions (¤t_section->region, |
4553 | ¤t_section->lma_region, | |
4554 | memspec, lma_memspec, | |
6bdafbeb NC |
4555 | current_section->load_base != NULL, |
4556 | current_section->addr_tree != NULL); | |
252b5132 | 4557 | current_section->fill = fill; |
252b5132 RH |
4558 | current_section->phdrs = phdrs; |
4559 | stat_ptr = &statement_list; | |
4560 | } | |
4561 | ||
08da4cac KH |
4562 | /* Create an absolute symbol with the given name with the value of the |
4563 | address of first byte of the section named. | |
4564 | ||
4565 | If the symbol already exists, then do nothing. */ | |
252b5132 | 4566 | |
252b5132 | 4567 | void |
1579bae1 | 4568 | lang_abs_symbol_at_beginning_of (const char *secname, const char *name) |
252b5132 RH |
4569 | { |
4570 | struct bfd_link_hash_entry *h; | |
4571 | ||
b34976b6 | 4572 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 4573 | if (h == NULL) |
252b5132 RH |
4574 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
4575 | ||
4576 | if (h->type == bfd_link_hash_new | |
4577 | || h->type == bfd_link_hash_undefined) | |
4578 | { | |
4579 | asection *sec; | |
4580 | ||
4581 | h->type = bfd_link_hash_defined; | |
4582 | ||
4583 | sec = bfd_get_section_by_name (output_bfd, secname); | |
1579bae1 | 4584 | if (sec == NULL) |
252b5132 RH |
4585 | h->u.def.value = 0; |
4586 | else | |
4587 | h->u.def.value = bfd_get_section_vma (output_bfd, sec); | |
4588 | ||
4589 | h->u.def.section = bfd_abs_section_ptr; | |
4590 | } | |
4591 | } | |
4592 | ||
08da4cac KH |
4593 | /* Create an absolute symbol with the given name with the value of the |
4594 | address of the first byte after the end of the section named. | |
4595 | ||
4596 | If the symbol already exists, then do nothing. */ | |
252b5132 | 4597 | |
252b5132 | 4598 | void |
1579bae1 | 4599 | lang_abs_symbol_at_end_of (const char *secname, const char *name) |
252b5132 RH |
4600 | { |
4601 | struct bfd_link_hash_entry *h; | |
4602 | ||
b34976b6 | 4603 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
1579bae1 | 4604 | if (h == NULL) |
252b5132 RH |
4605 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
4606 | ||
4607 | if (h->type == bfd_link_hash_new | |
4608 | || h->type == bfd_link_hash_undefined) | |
4609 | { | |
4610 | asection *sec; | |
4611 | ||
4612 | h->type = bfd_link_hash_defined; | |
4613 | ||
4614 | sec = bfd_get_section_by_name (output_bfd, secname); | |
1579bae1 | 4615 | if (sec == NULL) |
252b5132 RH |
4616 | h->u.def.value = 0; |
4617 | else | |
4618 | h->u.def.value = (bfd_get_section_vma (output_bfd, sec) | |
eea6121a | 4619 | + TO_ADDR (sec->size)); |
252b5132 RH |
4620 | |
4621 | h->u.def.section = bfd_abs_section_ptr; | |
4622 | } | |
4623 | } | |
4624 | ||
4625 | void | |
1579bae1 AM |
4626 | lang_statement_append (lang_statement_list_type *list, |
4627 | lang_statement_union_type *element, | |
4628 | lang_statement_union_type **field) | |
252b5132 RH |
4629 | { |
4630 | *(list->tail) = element; | |
4631 | list->tail = field; | |
4632 | } | |
4633 | ||
4634 | /* Set the output format type. -oformat overrides scripts. */ | |
4635 | ||
4636 | void | |
1579bae1 AM |
4637 | lang_add_output_format (const char *format, |
4638 | const char *big, | |
4639 | const char *little, | |
4640 | int from_script) | |
252b5132 RH |
4641 | { |
4642 | if (output_target == NULL || !from_script) | |
4643 | { | |
4644 | if (command_line.endian == ENDIAN_BIG | |
4645 | && big != NULL) | |
4646 | format = big; | |
4647 | else if (command_line.endian == ENDIAN_LITTLE | |
4648 | && little != NULL) | |
4649 | format = little; | |
4650 | ||
4651 | output_target = format; | |
4652 | } | |
4653 | } | |
4654 | ||
4655 | /* Enter a group. This creates a new lang_group_statement, and sets | |
4656 | stat_ptr to build new statements within the group. */ | |
4657 | ||
4658 | void | |
1579bae1 | 4659 | lang_enter_group (void) |
252b5132 RH |
4660 | { |
4661 | lang_group_statement_type *g; | |
4662 | ||
4663 | g = new_stat (lang_group_statement, stat_ptr); | |
4664 | lang_list_init (&g->children); | |
4665 | stat_ptr = &g->children; | |
4666 | } | |
4667 | ||
4668 | /* Leave a group. This just resets stat_ptr to start writing to the | |
4669 | regular list of statements again. Note that this will not work if | |
4670 | groups can occur inside anything else which can adjust stat_ptr, | |
4671 | but currently they can't. */ | |
4672 | ||
4673 | void | |
1579bae1 | 4674 | lang_leave_group (void) |
252b5132 RH |
4675 | { |
4676 | stat_ptr = &statement_list; | |
4677 | } | |
4678 | ||
4679 | /* Add a new program header. This is called for each entry in a PHDRS | |
4680 | command in a linker script. */ | |
4681 | ||
4682 | void | |
1579bae1 AM |
4683 | lang_new_phdr (const char *name, |
4684 | etree_type *type, | |
4685 | bfd_boolean filehdr, | |
4686 | bfd_boolean phdrs, | |
4687 | etree_type *at, | |
4688 | etree_type *flags) | |
252b5132 RH |
4689 | { |
4690 | struct lang_phdr *n, **pp; | |
4691 | ||
1579bae1 | 4692 | n = stat_alloc (sizeof (struct lang_phdr)); |
252b5132 RH |
4693 | n->next = NULL; |
4694 | n->name = name; | |
4695 | n->type = exp_get_value_int (type, 0, "program header type", | |
4696 | lang_final_phase_enum); | |
4697 | n->filehdr = filehdr; | |
4698 | n->phdrs = phdrs; | |
4699 | n->at = at; | |
4700 | n->flags = flags; | |
4701 | ||
4702 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) | |
4703 | ; | |
4704 | *pp = n; | |
4705 | } | |
4706 | ||
4707 | /* Record the program header information in the output BFD. FIXME: We | |
4708 | should not be calling an ELF specific function here. */ | |
4709 | ||
4710 | static void | |
1579bae1 | 4711 | lang_record_phdrs (void) |
252b5132 RH |
4712 | { |
4713 | unsigned int alc; | |
4714 | asection **secs; | |
6bdafbeb | 4715 | lang_output_section_phdr_list *last; |
252b5132 RH |
4716 | struct lang_phdr *l; |
4717 | lang_statement_union_type *u; | |
4718 | ||
4719 | alc = 10; | |
1579bae1 | 4720 | secs = xmalloc (alc * sizeof (asection *)); |
252b5132 RH |
4721 | last = NULL; |
4722 | for (l = lang_phdr_list; l != NULL; l = l->next) | |
4723 | { | |
4724 | unsigned int c; | |
4725 | flagword flags; | |
4726 | bfd_vma at; | |
4727 | ||
4728 | c = 0; | |
4729 | for (u = lang_output_section_statement.head; | |
4730 | u != NULL; | |
4731 | u = u->output_section_statement.next) | |
4732 | { | |
4733 | lang_output_section_statement_type *os; | |
6bdafbeb | 4734 | lang_output_section_phdr_list *pl; |
252b5132 RH |
4735 | |
4736 | os = &u->output_section_statement; | |
0841712e JJ |
4737 | if (os->constraint == -1) |
4738 | continue; | |
252b5132 RH |
4739 | |
4740 | pl = os->phdrs; | |
4741 | if (pl != NULL) | |
4742 | last = pl; | |
4743 | else | |
4744 | { | |
4745 | if (os->sectype == noload_section | |
4746 | || os->bfd_section == NULL | |
4747 | || (os->bfd_section->flags & SEC_ALLOC) == 0) | |
4748 | continue; | |
4749 | pl = last; | |
4750 | } | |
4751 | ||
4752 | if (os->bfd_section == NULL) | |
4753 | continue; | |
4754 | ||
4755 | for (; pl != NULL; pl = pl->next) | |
4756 | { | |
4757 | if (strcmp (pl->name, l->name) == 0) | |
4758 | { | |
4759 | if (c >= alc) | |
4760 | { | |
4761 | alc *= 2; | |
1579bae1 | 4762 | secs = xrealloc (secs, alc * sizeof (asection *)); |
252b5132 RH |
4763 | } |
4764 | secs[c] = os->bfd_section; | |
4765 | ++c; | |
b34976b6 | 4766 | pl->used = TRUE; |
252b5132 RH |
4767 | } |
4768 | } | |
4769 | } | |
4770 | ||
4771 | if (l->flags == NULL) | |
4772 | flags = 0; | |
4773 | else | |
4774 | flags = exp_get_vma (l->flags, 0, "phdr flags", | |
4775 | lang_final_phase_enum); | |
4776 | ||
4777 | if (l->at == NULL) | |
4778 | at = 0; | |
4779 | else | |
4780 | at = exp_get_vma (l->at, 0, "phdr load address", | |
4781 | lang_final_phase_enum); | |
4782 | ||
4783 | if (! bfd_record_phdr (output_bfd, l->type, | |
d1778b88 | 4784 | l->flags != NULL, flags, l->at != NULL, |
252b5132 RH |
4785 | at, l->filehdr, l->phdrs, c, secs)) |
4786 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); | |
4787 | } | |
4788 | ||
4789 | free (secs); | |
4790 | ||
4791 | /* Make sure all the phdr assignments succeeded. */ | |
4792 | for (u = lang_output_section_statement.head; | |
4793 | u != NULL; | |
4794 | u = u->output_section_statement.next) | |
4795 | { | |
6bdafbeb | 4796 | lang_output_section_phdr_list *pl; |
252b5132 | 4797 | |
0841712e JJ |
4798 | if (u->output_section_statement.constraint == -1 |
4799 | || u->output_section_statement.bfd_section == NULL) | |
252b5132 RH |
4800 | continue; |
4801 | ||
4802 | for (pl = u->output_section_statement.phdrs; | |
4803 | pl != NULL; | |
4804 | pl = pl->next) | |
4805 | if (! pl->used && strcmp (pl->name, "NONE") != 0) | |
4806 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), | |
4807 | u->output_section_statement.name, pl->name); | |
4808 | } | |
4809 | } | |
4810 | ||
4811 | /* Record a list of sections which may not be cross referenced. */ | |
4812 | ||
4813 | void | |
6bdafbeb | 4814 | lang_add_nocrossref (lang_nocrossref_type *l) |
252b5132 RH |
4815 | { |
4816 | struct lang_nocrossrefs *n; | |
4817 | ||
1579bae1 | 4818 | n = xmalloc (sizeof *n); |
252b5132 RH |
4819 | n->next = nocrossref_list; |
4820 | n->list = l; | |
4821 | nocrossref_list = n; | |
4822 | ||
4823 | /* Set notice_all so that we get informed about all symbols. */ | |
b34976b6 | 4824 | link_info.notice_all = TRUE; |
252b5132 RH |
4825 | } |
4826 | \f | |
4827 | /* Overlay handling. We handle overlays with some static variables. */ | |
4828 | ||
4829 | /* The overlay virtual address. */ | |
4830 | static etree_type *overlay_vma; | |
7e7d5768 AM |
4831 | /* And subsection alignment. */ |
4832 | static etree_type *overlay_subalign; | |
252b5132 | 4833 | |
252b5132 RH |
4834 | /* An expression for the maximum section size seen so far. */ |
4835 | static etree_type *overlay_max; | |
4836 | ||
4837 | /* A list of all the sections in this overlay. */ | |
4838 | ||
89cdebba | 4839 | struct overlay_list { |
252b5132 RH |
4840 | struct overlay_list *next; |
4841 | lang_output_section_statement_type *os; | |
4842 | }; | |
4843 | ||
4844 | static struct overlay_list *overlay_list; | |
4845 | ||
4846 | /* Start handling an overlay. */ | |
4847 | ||
4848 | void | |
7e7d5768 | 4849 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
252b5132 RH |
4850 | { |
4851 | /* The grammar should prevent nested overlays from occurring. */ | |
7e7d5768 AM |
4852 | ASSERT (overlay_vma == NULL |
4853 | && overlay_subalign == NULL | |
4854 | && overlay_max == NULL); | |
252b5132 RH |
4855 | |
4856 | overlay_vma = vma_expr; | |
7e7d5768 | 4857 | overlay_subalign = subalign; |
252b5132 RH |
4858 | } |
4859 | ||
4860 | /* Start a section in an overlay. We handle this by calling | |
9f88b410 RS |
4861 | lang_enter_output_section_statement with the correct VMA. |
4862 | lang_leave_overlay sets up the LMA and memory regions. */ | |
252b5132 RH |
4863 | |
4864 | void | |
1579bae1 | 4865 | lang_enter_overlay_section (const char *name) |
252b5132 RH |
4866 | { |
4867 | struct overlay_list *n; | |
4868 | etree_type *size; | |
4869 | ||
4870 | lang_enter_output_section_statement (name, overlay_vma, normal_section, | |
0841712e | 4871 | 0, overlay_subalign, 0, 0); |
252b5132 | 4872 | |
9f88b410 | 4873 | /* If this is the first section, then base the VMA of future |
252b5132 RH |
4874 | sections on this one. This will work correctly even if `.' is |
4875 | used in the addresses. */ | |
4876 | if (overlay_list == NULL) | |
9f88b410 | 4877 | overlay_vma = exp_nameop (ADDR, name); |
252b5132 RH |
4878 | |
4879 | /* Remember the section. */ | |
1579bae1 | 4880 | n = xmalloc (sizeof *n); |
252b5132 RH |
4881 | n->os = current_section; |
4882 | n->next = overlay_list; | |
4883 | overlay_list = n; | |
4884 | ||
4885 | size = exp_nameop (SIZEOF, name); | |
4886 | ||
252b5132 RH |
4887 | /* Arrange to work out the maximum section end address. */ |
4888 | if (overlay_max == NULL) | |
4889 | overlay_max = size; | |
4890 | else | |
4891 | overlay_max = exp_binop (MAX_K, overlay_max, size); | |
4892 | } | |
4893 | ||
4894 | /* Finish a section in an overlay. There isn't any special to do | |
4895 | here. */ | |
4896 | ||
4897 | void | |
1579bae1 | 4898 | lang_leave_overlay_section (fill_type *fill, |
6bdafbeb | 4899 | lang_output_section_phdr_list *phdrs) |
252b5132 RH |
4900 | { |
4901 | const char *name; | |
4902 | char *clean, *s2; | |
4903 | const char *s1; | |
4904 | char *buf; | |
4905 | ||
4906 | name = current_section->name; | |
4907 | ||
a747ee4d NC |
4908 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
4909 | region and that no load-time region has been specified. It doesn't | |
4910 | really matter what we say here, since lang_leave_overlay will | |
4911 | override it. */ | |
4912 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); | |
252b5132 RH |
4913 | |
4914 | /* Define the magic symbols. */ | |
4915 | ||
4916 | clean = xmalloc (strlen (name) + 1); | |
4917 | s2 = clean; | |
4918 | for (s1 = name; *s1 != '\0'; s1++) | |
3882b010 | 4919 | if (ISALNUM (*s1) || *s1 == '_') |
252b5132 RH |
4920 | *s2++ = *s1; |
4921 | *s2 = '\0'; | |
4922 | ||
4923 | buf = xmalloc (strlen (clean) + sizeof "__load_start_"); | |
4924 | sprintf (buf, "__load_start_%s", clean); | |
4925 | lang_add_assignment (exp_assop ('=', buf, | |
4926 | exp_nameop (LOADADDR, name))); | |
4927 | ||
4928 | buf = xmalloc (strlen (clean) + sizeof "__load_stop_"); | |
4929 | sprintf (buf, "__load_stop_%s", clean); | |
4930 | lang_add_assignment (exp_assop ('=', buf, | |
4931 | exp_binop ('+', | |
4932 | exp_nameop (LOADADDR, name), | |
4933 | exp_nameop (SIZEOF, name)))); | |
4934 | ||
4935 | free (clean); | |
4936 | } | |
4937 | ||
4938 | /* Finish an overlay. If there are any overlay wide settings, this | |
4939 | looks through all the sections in the overlay and sets them. */ | |
4940 | ||
4941 | void | |
1579bae1 AM |
4942 | lang_leave_overlay (etree_type *lma_expr, |
4943 | int nocrossrefs, | |
4944 | fill_type *fill, | |
4945 | const char *memspec, | |
6bdafbeb | 4946 | lang_output_section_phdr_list *phdrs, |
1579bae1 | 4947 | const char *lma_memspec) |
252b5132 RH |
4948 | { |
4949 | lang_memory_region_type *region; | |
562d3460 | 4950 | lang_memory_region_type *lma_region; |
252b5132 | 4951 | struct overlay_list *l; |
6bdafbeb | 4952 | lang_nocrossref_type *nocrossref; |
252b5132 | 4953 | |
ee3cc2e2 RS |
4954 | lang_get_regions (®ion, &lma_region, |
4955 | memspec, lma_memspec, | |
6bdafbeb | 4956 | lma_expr != NULL, FALSE); |
562d3460 | 4957 | |
252b5132 RH |
4958 | nocrossref = NULL; |
4959 | ||
9f88b410 RS |
4960 | /* After setting the size of the last section, set '.' to end of the |
4961 | overlay region. */ | |
4962 | if (overlay_list != NULL) | |
4963 | overlay_list->os->update_dot_tree | |
4964 | = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max)); | |
4965 | ||
252b5132 RH |
4966 | l = overlay_list; |
4967 | while (l != NULL) | |
4968 | { | |
4969 | struct overlay_list *next; | |
4970 | ||
1579bae1 | 4971 | if (fill != NULL && l->os->fill == NULL) |
252b5132 | 4972 | l->os->fill = fill; |
1545243b | 4973 | |
9f88b410 RS |
4974 | l->os->region = region; |
4975 | l->os->lma_region = lma_region; | |
4976 | ||
4977 | /* The first section has the load address specified in the | |
4978 | OVERLAY statement. The rest are worked out from that. | |
4979 | The base address is not needed (and should be null) if | |
4980 | an LMA region was specified. */ | |
4981 | if (l->next == 0) | |
4982 | l->os->load_base = lma_expr; | |
4983 | else if (lma_region == 0) | |
4984 | l->os->load_base = exp_binop ('+', | |
4985 | exp_nameop (LOADADDR, l->next->os->name), | |
4986 | exp_nameop (SIZEOF, l->next->os->name)); | |
1545243b | 4987 | |
252b5132 RH |
4988 | if (phdrs != NULL && l->os->phdrs == NULL) |
4989 | l->os->phdrs = phdrs; | |
4990 | ||
9f88b410 | 4991 | if (nocrossrefs) |
252b5132 | 4992 | { |
6bdafbeb | 4993 | lang_nocrossref_type *nc; |
252b5132 | 4994 | |
1579bae1 | 4995 | nc = xmalloc (sizeof *nc); |
252b5132 RH |
4996 | nc->name = l->os->name; |
4997 | nc->next = nocrossref; | |
4998 | nocrossref = nc; | |
4999 | } | |
5000 | ||
5001 | next = l->next; | |
5002 | free (l); | |
5003 | l = next; | |
5004 | } | |
5005 | ||
5006 | if (nocrossref != NULL) | |
5007 | lang_add_nocrossref (nocrossref); | |
5008 | ||
252b5132 | 5009 | overlay_vma = NULL; |
252b5132 RH |
5010 | overlay_list = NULL; |
5011 | overlay_max = NULL; | |
5012 | } | |
5013 | \f | |
5014 | /* Version handling. This is only useful for ELF. */ | |
5015 | ||
5016 | /* This global variable holds the version tree that we build. */ | |
5017 | ||
5018 | struct bfd_elf_version_tree *lang_elf_version_info; | |
5019 | ||
108ba305 JJ |
5020 | /* If PREV is NULL, return first version pattern matching particular symbol. |
5021 | If PREV is non-NULL, return first version pattern matching particular | |
5022 | symbol after PREV (previously returned by lang_vers_match). */ | |
252b5132 | 5023 | |
108ba305 JJ |
5024 | static struct bfd_elf_version_expr * |
5025 | lang_vers_match (struct bfd_elf_version_expr_head *head, | |
5026 | struct bfd_elf_version_expr *prev, | |
5027 | const char *sym) | |
252b5132 | 5028 | { |
108ba305 JJ |
5029 | const char *cxx_sym = sym; |
5030 | const char *java_sym = sym; | |
5031 | struct bfd_elf_version_expr *expr = NULL; | |
252b5132 | 5032 | |
108ba305 | 5033 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
252b5132 | 5034 | { |
2f97444a | 5035 | cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI); |
108ba305 JJ |
5036 | if (!cxx_sym) |
5037 | cxx_sym = sym; | |
252b5132 | 5038 | } |
df816215 | 5039 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
252b5132 | 5040 | { |
108ba305 JJ |
5041 | java_sym = cplus_demangle (sym, DMGL_JAVA); |
5042 | if (!java_sym) | |
5043 | java_sym = sym; | |
252b5132 RH |
5044 | } |
5045 | ||
5e35cbc2 | 5046 | if (head->htab && (prev == NULL || prev->symbol)) |
252b5132 | 5047 | { |
108ba305 JJ |
5048 | struct bfd_elf_version_expr e; |
5049 | ||
5050 | switch (prev ? prev->mask : 0) | |
5051 | { | |
5052 | case 0: | |
5053 | if (head->mask & BFD_ELF_VERSION_C_TYPE) | |
5054 | { | |
5e35cbc2 | 5055 | e.symbol = sym; |
108ba305 | 5056 | expr = htab_find (head->htab, &e); |
5e35cbc2 | 5057 | while (expr && strcmp (expr->symbol, sym) == 0) |
108ba305 JJ |
5058 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
5059 | goto out_ret; | |
5060 | else | |
5061 | expr = expr->next; | |
5062 | } | |
5063 | /* Fallthrough */ | |
5064 | case BFD_ELF_VERSION_C_TYPE: | |
5065 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) | |
5066 | { | |
5e35cbc2 | 5067 | e.symbol = cxx_sym; |
108ba305 | 5068 | expr = htab_find (head->htab, &e); |
7a995eb3 | 5069 | while (expr && strcmp (expr->symbol, cxx_sym) == 0) |
108ba305 JJ |
5070 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
5071 | goto out_ret; | |
5072 | else | |
5073 | expr = expr->next; | |
5074 | } | |
5075 | /* Fallthrough */ | |
5076 | case BFD_ELF_VERSION_CXX_TYPE: | |
5077 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) | |
5078 | { | |
5e35cbc2 | 5079 | e.symbol = java_sym; |
108ba305 | 5080 | expr = htab_find (head->htab, &e); |
7a995eb3 | 5081 | while (expr && strcmp (expr->symbol, java_sym) == 0) |
108ba305 JJ |
5082 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
5083 | goto out_ret; | |
5084 | else | |
5085 | expr = expr->next; | |
5086 | } | |
5087 | /* Fallthrough */ | |
5088 | default: | |
5089 | break; | |
5090 | } | |
252b5132 | 5091 | } |
108ba305 JJ |
5092 | |
5093 | /* Finally, try the wildcards. */ | |
5e35cbc2 | 5094 | if (prev == NULL || prev->symbol) |
108ba305 | 5095 | expr = head->remaining; |
252b5132 | 5096 | else |
108ba305 JJ |
5097 | expr = prev->next; |
5098 | while (expr) | |
252b5132 | 5099 | { |
108ba305 JJ |
5100 | const char *s; |
5101 | ||
5102 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') | |
5103 | break; | |
5104 | ||
5105 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) | |
5106 | s = java_sym; | |
5107 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) | |
5108 | s = cxx_sym; | |
5109 | else | |
5110 | s = sym; | |
5e35cbc2 | 5111 | if (fnmatch (expr->pattern, s, 0) == 0) |
108ba305 JJ |
5112 | break; |
5113 | expr = expr->next; | |
252b5132 RH |
5114 | } |
5115 | ||
108ba305 JJ |
5116 | out_ret: |
5117 | if (cxx_sym != sym) | |
5118 | free ((char *) cxx_sym); | |
5119 | if (java_sym != sym) | |
5120 | free ((char *) java_sym); | |
5121 | return expr; | |
252b5132 RH |
5122 | } |
5123 | ||
5e35cbc2 L |
5124 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
5125 | return a string pointing to the symbol name. */ | |
5126 | ||
5127 | static const char * | |
5128 | realsymbol (const char *pattern) | |
5129 | { | |
5130 | const char *p; | |
5131 | bfd_boolean changed = FALSE, backslash = FALSE; | |
5132 | char *s, *symbol = xmalloc (strlen (pattern) + 1); | |
5133 | ||
5134 | for (p = pattern, s = symbol; *p != '\0'; ++p) | |
5135 | { | |
5136 | /* It is a glob pattern only if there is no preceding | |
5137 | backslash. */ | |
5138 | if (! backslash && (*p == '?' || *p == '*' || *p == '[')) | |
5139 | { | |
5140 | free (symbol); | |
5141 | return NULL; | |
5142 | } | |
5143 | ||
5144 | if (backslash) | |
5145 | { | |
5146 | /* Remove the preceding backslash. */ | |
5147 | *(s - 1) = *p; | |
5148 | changed = TRUE; | |
5149 | } | |
5150 | else | |
5151 | *s++ = *p; | |
5152 | ||
5153 | backslash = *p == '\\'; | |
5154 | } | |
5155 | ||
5156 | if (changed) | |
5157 | { | |
5158 | *s = '\0'; | |
5159 | return symbol; | |
5160 | } | |
5161 | else | |
5162 | { | |
5163 | free (symbol); | |
5164 | return pattern; | |
5165 | } | |
5166 | } | |
5167 | ||
252b5132 RH |
5168 | /* This is called for each variable name or match expression. */ |
5169 | ||
5170 | struct bfd_elf_version_expr * | |
1579bae1 AM |
5171 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
5172 | const char *new, | |
5173 | const char *lang) | |
252b5132 RH |
5174 | { |
5175 | struct bfd_elf_version_expr *ret; | |
5176 | ||
1579bae1 | 5177 | ret = xmalloc (sizeof *ret); |
252b5132 RH |
5178 | ret->next = orig; |
5179 | ret->pattern = new; | |
31941635 L |
5180 | ret->symver = 0; |
5181 | ret->script = 0; | |
5e35cbc2 | 5182 | ret->symbol = realsymbol (new); |
252b5132 RH |
5183 | |
5184 | if (lang == NULL || strcasecmp (lang, "C") == 0) | |
108ba305 | 5185 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 | 5186 | else if (strcasecmp (lang, "C++") == 0) |
108ba305 | 5187 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
252b5132 | 5188 | else if (strcasecmp (lang, "Java") == 0) |
108ba305 | 5189 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
252b5132 RH |
5190 | else |
5191 | { | |
5192 | einfo (_("%X%P: unknown language `%s' in version information\n"), | |
5193 | lang); | |
108ba305 | 5194 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
252b5132 RH |
5195 | } |
5196 | ||
fac1652d | 5197 | return ldemul_new_vers_pattern (ret); |
252b5132 RH |
5198 | } |
5199 | ||
5200 | /* This is called for each set of variable names and match | |
5201 | expressions. */ | |
5202 | ||
5203 | struct bfd_elf_version_tree * | |
1579bae1 AM |
5204 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
5205 | struct bfd_elf_version_expr *locals) | |
252b5132 RH |
5206 | { |
5207 | struct bfd_elf_version_tree *ret; | |
5208 | ||
108ba305 JJ |
5209 | ret = xcalloc (1, sizeof *ret); |
5210 | ret->globals.list = globals; | |
5211 | ret->locals.list = locals; | |
5212 | ret->match = lang_vers_match; | |
252b5132 | 5213 | ret->name_indx = (unsigned int) -1; |
252b5132 RH |
5214 | return ret; |
5215 | } | |
5216 | ||
5217 | /* This static variable keeps track of version indices. */ | |
5218 | ||
5219 | static int version_index; | |
5220 | ||
108ba305 JJ |
5221 | static hashval_t |
5222 | version_expr_head_hash (const void *p) | |
5223 | { | |
5224 | const struct bfd_elf_version_expr *e = p; | |
5225 | ||
5e35cbc2 | 5226 | return htab_hash_string (e->symbol); |
108ba305 JJ |
5227 | } |
5228 | ||
5229 | static int | |
5230 | version_expr_head_eq (const void *p1, const void *p2) | |
5231 | { | |
5232 | const struct bfd_elf_version_expr *e1 = p1; | |
5233 | const struct bfd_elf_version_expr *e2 = p2; | |
5234 | ||
5e35cbc2 | 5235 | return strcmp (e1->symbol, e2->symbol) == 0; |
108ba305 JJ |
5236 | } |
5237 | ||
5238 | static void | |
5239 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) | |
5240 | { | |
5241 | size_t count = 0; | |
5242 | struct bfd_elf_version_expr *e, *next; | |
5243 | struct bfd_elf_version_expr **list_loc, **remaining_loc; | |
5244 | ||
5245 | for (e = head->list; e; e = e->next) | |
5246 | { | |
5e35cbc2 | 5247 | if (e->symbol) |
108ba305 JJ |
5248 | count++; |
5249 | head->mask |= e->mask; | |
5250 | } | |
5251 | ||
5252 | if (count) | |
5253 | { | |
5254 | head->htab = htab_create (count * 2, version_expr_head_hash, | |
5255 | version_expr_head_eq, NULL); | |
5256 | list_loc = &head->list; | |
5257 | remaining_loc = &head->remaining; | |
5258 | for (e = head->list; e; e = next) | |
5259 | { | |
5260 | next = e->next; | |
5e35cbc2 | 5261 | if (!e->symbol) |
108ba305 JJ |
5262 | { |
5263 | *remaining_loc = e; | |
5264 | remaining_loc = &e->next; | |
5265 | } | |
5266 | else | |
5267 | { | |
5268 | void **loc = htab_find_slot (head->htab, e, INSERT); | |
5269 | ||
5270 | if (*loc) | |
5271 | { | |
5272 | struct bfd_elf_version_expr *e1, *last; | |
5273 | ||
5274 | e1 = *loc; | |
5275 | last = NULL; | |
5276 | do | |
5277 | { | |
5278 | if (e1->mask == e->mask) | |
5279 | { | |
5280 | last = NULL; | |
5281 | break; | |
5282 | } | |
5283 | last = e1; | |
5284 | e1 = e1->next; | |
5285 | } | |
5e35cbc2 | 5286 | while (e1 && strcmp (e1->symbol, e->symbol) == 0); |
108ba305 JJ |
5287 | |
5288 | if (last == NULL) | |
5289 | { | |
5290 | /* This is a duplicate. */ | |
5291 | /* FIXME: Memory leak. Sometimes pattern is not | |
5292 | xmalloced alone, but in larger chunk of memory. */ | |
5e35cbc2 | 5293 | /* free (e->symbol); */ |
108ba305 JJ |
5294 | free (e); |
5295 | } | |
5296 | else | |
5297 | { | |
5298 | e->next = last->next; | |
5299 | last->next = e; | |
5300 | } | |
5301 | } | |
5302 | else | |
5303 | { | |
5304 | *loc = e; | |
5305 | *list_loc = e; | |
5306 | list_loc = &e->next; | |
5307 | } | |
5308 | } | |
5309 | } | |
5310 | *remaining_loc = NULL; | |
5311 | *list_loc = head->remaining; | |
5312 | } | |
5313 | else | |
5314 | head->remaining = head->list; | |
5315 | } | |
5316 | ||
252b5132 RH |
5317 | /* This is called when we know the name and dependencies of the |
5318 | version. */ | |
5319 | ||
5320 | void | |
1579bae1 AM |
5321 | lang_register_vers_node (const char *name, |
5322 | struct bfd_elf_version_tree *version, | |
5323 | struct bfd_elf_version_deps *deps) | |
252b5132 RH |
5324 | { |
5325 | struct bfd_elf_version_tree *t, **pp; | |
5326 | struct bfd_elf_version_expr *e1; | |
5327 | ||
6b9b879a JJ |
5328 | if (name == NULL) |
5329 | name = ""; | |
5330 | ||
5331 | if ((name[0] == '\0' && lang_elf_version_info != NULL) | |
5332 | || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0')) | |
5333 | { | |
6feb9908 AM |
5334 | einfo (_("%X%P: anonymous version tag cannot be combined" |
5335 | " with other version tags\n")); | |
5ed6aba4 | 5336 | free (version); |
6b9b879a JJ |
5337 | return; |
5338 | } | |
5339 | ||
252b5132 RH |
5340 | /* Make sure this node has a unique name. */ |
5341 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5342 | if (strcmp (t->name, name) == 0) | |
5343 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); | |
5344 | ||
108ba305 JJ |
5345 | lang_finalize_version_expr_head (&version->globals); |
5346 | lang_finalize_version_expr_head (&version->locals); | |
5347 | ||
252b5132 RH |
5348 | /* Check the global and local match names, and make sure there |
5349 | aren't any duplicates. */ | |
5350 | ||
108ba305 | 5351 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
5352 | { |
5353 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5354 | { | |
5355 | struct bfd_elf_version_expr *e2; | |
5356 | ||
5e35cbc2 | 5357 | if (t->locals.htab && e1->symbol) |
108ba305 JJ |
5358 | { |
5359 | e2 = htab_find (t->locals.htab, e1); | |
5e35cbc2 | 5360 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
108ba305 JJ |
5361 | { |
5362 | if (e1->mask == e2->mask) | |
6feb9908 AM |
5363 | einfo (_("%X%P: duplicate expression `%s'" |
5364 | " in version information\n"), e1->symbol); | |
108ba305 JJ |
5365 | e2 = e2->next; |
5366 | } | |
5367 | } | |
5e35cbc2 | 5368 | else if (!e1->symbol) |
108ba305 | 5369 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
5370 | if (strcmp (e1->pattern, e2->pattern) == 0 |
5371 | && e1->mask == e2->mask) | |
5372 | einfo (_("%X%P: duplicate expression `%s'" | |
5373 | " in version information\n"), e1->pattern); | |
252b5132 RH |
5374 | } |
5375 | } | |
5376 | ||
108ba305 | 5377 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
252b5132 RH |
5378 | { |
5379 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5380 | { | |
5381 | struct bfd_elf_version_expr *e2; | |
5382 | ||
5e35cbc2 | 5383 | if (t->globals.htab && e1->symbol) |
108ba305 JJ |
5384 | { |
5385 | e2 = htab_find (t->globals.htab, e1); | |
5e35cbc2 | 5386 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
108ba305 JJ |
5387 | { |
5388 | if (e1->mask == e2->mask) | |
6feb9908 AM |
5389 | einfo (_("%X%P: duplicate expression `%s'" |
5390 | " in version information\n"), | |
5e35cbc2 | 5391 | e1->symbol); |
108ba305 JJ |
5392 | e2 = e2->next; |
5393 | } | |
5394 | } | |
5e35cbc2 | 5395 | else if (!e1->symbol) |
108ba305 | 5396 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
6feb9908 AM |
5397 | if (strcmp (e1->pattern, e2->pattern) == 0 |
5398 | && e1->mask == e2->mask) | |
5399 | einfo (_("%X%P: duplicate expression `%s'" | |
5400 | " in version information\n"), e1->pattern); | |
252b5132 RH |
5401 | } |
5402 | } | |
5403 | ||
5404 | version->deps = deps; | |
5405 | version->name = name; | |
6b9b879a JJ |
5406 | if (name[0] != '\0') |
5407 | { | |
5408 | ++version_index; | |
5409 | version->vernum = version_index; | |
5410 | } | |
5411 | else | |
5412 | version->vernum = 0; | |
252b5132 RH |
5413 | |
5414 | for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next) | |
5415 | ; | |
5416 | *pp = version; | |
5417 | } | |
5418 | ||
5419 | /* This is called when we see a version dependency. */ | |
5420 | ||
5421 | struct bfd_elf_version_deps * | |
1579bae1 | 5422 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
252b5132 RH |
5423 | { |
5424 | struct bfd_elf_version_deps *ret; | |
5425 | struct bfd_elf_version_tree *t; | |
5426 | ||
1579bae1 | 5427 | ret = xmalloc (sizeof *ret); |
252b5132 RH |
5428 | ret->next = list; |
5429 | ||
5430 | for (t = lang_elf_version_info; t != NULL; t = t->next) | |
5431 | { | |
5432 | if (strcmp (t->name, name) == 0) | |
5433 | { | |
5434 | ret->version_needed = t; | |
5435 | return ret; | |
5436 | } | |
5437 | } | |
5438 | ||
5439 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); | |
5440 | ||
5441 | return ret; | |
5442 | } | |
5443 | ||
5444 | static void | |
1579bae1 | 5445 | lang_do_version_exports_section (void) |
252b5132 RH |
5446 | { |
5447 | struct bfd_elf_version_expr *greg = NULL, *lreg; | |
5448 | ||
5449 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
5450 | { | |
5451 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); | |
5452 | char *contents, *p; | |
5453 | bfd_size_type len; | |
5454 | ||
5455 | if (sec == NULL) | |
b7a26f91 | 5456 | continue; |
252b5132 | 5457 | |
eea6121a | 5458 | len = sec->size; |
252b5132 RH |
5459 | contents = xmalloc (len); |
5460 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) | |
6f9efd97 | 5461 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
252b5132 RH |
5462 | |
5463 | p = contents; | |
89cdebba | 5464 | while (p < contents + len) |
252b5132 | 5465 | { |
313e35ee | 5466 | greg = lang_new_vers_pattern (greg, p, NULL); |
252b5132 RH |
5467 | p = strchr (p, '\0') + 1; |
5468 | } | |
5469 | ||
5470 | /* Do not free the contents, as we used them creating the regex. */ | |
5471 | ||
5472 | /* Do not include this section in the link. */ | |
6feb9908 | 5473 | sec->flags |= SEC_EXCLUDE; |
252b5132 RH |
5474 | } |
5475 | ||
313e35ee | 5476 | lreg = lang_new_vers_pattern (NULL, "*", NULL); |
252b5132 RH |
5477 | lang_register_vers_node (command_line.version_exports_section, |
5478 | lang_new_vers_node (greg, lreg), NULL); | |
5479 | } | |
577a0623 AM |
5480 | |
5481 | void | |
1579bae1 | 5482 | lang_add_unique (const char *name) |
577a0623 AM |
5483 | { |
5484 | struct unique_sections *ent; | |
5485 | ||
5486 | for (ent = unique_section_list; ent; ent = ent->next) | |
5487 | if (strcmp (ent->name, name) == 0) | |
5488 | return; | |
5489 | ||
1579bae1 | 5490 | ent = xmalloc (sizeof *ent); |
577a0623 AM |
5491 | ent->name = xstrdup (name); |
5492 | ent->next = unique_section_list; | |
5493 | unique_section_list = ent; | |
5494 | } |