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