12159047fSniklas /* Linker command language support.
2b55d4692Sfgsch Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3*007c2a45Smiod 2001, 2002, 2003, 2004
4b305b0f1Sespie Free Software Foundation, Inc.
52159047fSniklas
62159047fSniklas This file is part of GLD, the Gnu Linker.
72159047fSniklas
82159047fSniklas GLD is free software; you can redistribute it and/or modify
92159047fSniklas it under the terms of the GNU General Public License as published by
10b305b0f1Sespie the Free Software Foundation; either version 2, or (at your option)
112159047fSniklas any later version.
122159047fSniklas
132159047fSniklas GLD is distributed in the hope that it will be useful,
142159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
162159047fSniklas GNU General Public License for more details.
172159047fSniklas
182159047fSniklas You should have received a copy of the GNU General Public License
190c6d0228Sniklas along with GLD; see the file COPYING. If not, write to the Free
200c6d0228Sniklas Software Foundation, 59 Temple Place - Suite 330, Boston, MA
210c6d0228Sniklas 02111-1307, USA. */
222159047fSniklas
232159047fSniklas #include "bfd.h"
242159047fSniklas #include "sysdep.h"
252159047fSniklas #include "libiberty.h"
26c074d1c9Sdrahn #include "safe-ctype.h"
27b305b0f1Sespie #include "obstack.h"
282159047fSniklas #include "bfdlink.h"
292159047fSniklas
302159047fSniklas #include "ld.h"
312159047fSniklas #include "ldmain.h"
322159047fSniklas #include "ldexp.h"
332159047fSniklas #include "ldlang.h"
34c074d1c9Sdrahn #include <ldgram.h>
352159047fSniklas #include "ldlex.h"
362159047fSniklas #include "ldmisc.h"
372159047fSniklas #include "ldctor.h"
382159047fSniklas #include "ldfile.h"
39b55d4692Sfgsch #include "ldemul.h"
400c6d0228Sniklas #include "fnmatch.h"
41b305b0f1Sespie #include "demangle.h"
42*007c2a45Smiod #include "hashtab.h"
430c6d0228Sniklas
44c074d1c9Sdrahn #ifndef offsetof
45c074d1c9Sdrahn #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
46c074d1c9Sdrahn #endif
472159047fSniklas
48c074d1c9Sdrahn /* Locals variables. */
492159047fSniklas static struct obstack stat_obstack;
502159047fSniklas
512159047fSniklas #define obstack_chunk_alloc xmalloc
522159047fSniklas #define obstack_chunk_free free
53b55d4692Sfgsch static const char *startup_file;
542159047fSniklas static lang_statement_list_type input_file_chain;
55c074d1c9Sdrahn static bfd_boolean placed_commons = FALSE;
562159047fSniklas static lang_output_section_statement_type *default_common_section;
57c074d1c9Sdrahn static bfd_boolean map_option_f;
582159047fSniklas static bfd_vma print_dot;
592159047fSniklas static lang_input_statement_type *first_file;
60b55d4692Sfgsch static const char *current_target;
61b55d4692Sfgsch static const char *output_target;
622159047fSniklas static lang_statement_list_type statement_list;
63191aa565Sniklas static struct lang_phdr *lang_phdr_list;
64*007c2a45Smiod static struct bfd_hash_table lang_definedness_table;
652159047fSniklas
66c074d1c9Sdrahn /* Forward declarations. */
67*007c2a45Smiod static void exp_init_os (etree_type *);
68*007c2a45Smiod static bfd_boolean wildcardp (const char *);
69*007c2a45Smiod static lang_input_statement_type *lookup_name (const char *);
70*007c2a45Smiod static bfd_boolean load_symbols (lang_input_statement_type *,
71*007c2a45Smiod lang_statement_list_type *);
72*007c2a45Smiod static struct bfd_hash_entry *lang_definedness_newfunc
73*007c2a45Smiod (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
74*007c2a45Smiod static void insert_undefined (const char *);
75*007c2a45Smiod static void print_statement (lang_statement_union_type *,
76*007c2a45Smiod lang_output_section_statement_type *);
77*007c2a45Smiod static void print_statement_list (lang_statement_union_type *,
78*007c2a45Smiod lang_output_section_statement_type *);
79*007c2a45Smiod static void print_statements (void);
80*007c2a45Smiod static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
81*007c2a45Smiod static void lang_record_phdrs (void);
82*007c2a45Smiod static void lang_do_version_exports_section (void);
83*007c2a45Smiod
84*007c2a45Smiod typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
85*007c2a45Smiod asection *, lang_input_statement_type *, void *);
86b305b0f1Sespie
87c074d1c9Sdrahn /* Exported variables. */
882159047fSniklas lang_output_section_statement_type *abs_output_section;
896978ef19Sespie lang_statement_list_type lang_output_section_statement;
902159047fSniklas lang_statement_list_type *stat_ptr = &statement_list;
91b305b0f1Sespie lang_statement_list_type file_chain = { NULL, NULL };
92c074d1c9Sdrahn struct bfd_sym_chain entry_symbol = { NULL, NULL };
93c074d1c9Sdrahn const char *entry_section = ".text";
94c074d1c9Sdrahn bfd_boolean entry_from_cmdline;
95c074d1c9Sdrahn bfd_boolean lang_has_input_file = FALSE;
96c074d1c9Sdrahn bfd_boolean had_output_filename = FALSE;
97c074d1c9Sdrahn bfd_boolean lang_float_flag = FALSE;
98c074d1c9Sdrahn bfd_boolean delete_output_file_on_failure = FALSE;
990c6d0228Sniklas struct lang_nocrossrefs *nocrossref_list;
100b55d4692Sfgsch struct unique_sections *unique_section_list;
101c074d1c9Sdrahn static bfd_boolean ldlang_sysrooted_script = FALSE;
102*007c2a45Smiod int lang_statement_iteration = 0;
1032159047fSniklas
1042159047fSniklas etree_type *base; /* Relocation base - or null */
1052159047fSniklas
106b55d4692Sfgsch #define new_stat(x, y) \
107*007c2a45Smiod (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
1082159047fSniklas
109b55d4692Sfgsch #define outside_section_address(q) \
110b55d4692Sfgsch ((q)->output_offset + (q)->output_section->vma)
1112159047fSniklas
112b55d4692Sfgsch #define outside_symbol_address(q) \
113b55d4692Sfgsch ((q)->value + outside_section_address (q->section))
1142159047fSniklas
115191aa565Sniklas #define SECTION_NAME_MAP_LENGTH (16)
116191aa565Sniklas
117*007c2a45Smiod void *
stat_alloc(size_t size)118*007c2a45Smiod stat_alloc (size_t size)
1192159047fSniklas {
1202159047fSniklas return obstack_alloc (&stat_obstack, size);
1212159047fSniklas }
1222159047fSniklas
123c074d1c9Sdrahn bfd_boolean
unique_section_p(const char * secnam)124*007c2a45Smiod unique_section_p (const char *secnam)
125b55d4692Sfgsch {
126b55d4692Sfgsch struct unique_sections *unam;
127b55d4692Sfgsch
128b55d4692Sfgsch for (unam = unique_section_list; unam; unam = unam->next)
129b55d4692Sfgsch if (wildcardp (unam->name)
130b55d4692Sfgsch ? fnmatch (unam->name, secnam, 0) == 0
131b55d4692Sfgsch : strcmp (unam->name, secnam) == 0)
132b55d4692Sfgsch {
133c074d1c9Sdrahn return TRUE;
134b55d4692Sfgsch }
135b55d4692Sfgsch
136c074d1c9Sdrahn return FALSE;
137b55d4692Sfgsch }
138b55d4692Sfgsch
139b55d4692Sfgsch /* Generic traversal routines for finding matching sections. */
140b305b0f1Sespie
141b305b0f1Sespie static void
walk_wild_section(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)142*007c2a45Smiod walk_wild_section (lang_wild_statement_type *ptr,
143*007c2a45Smiod lang_input_statement_type *file,
144*007c2a45Smiod callback_t callback,
145*007c2a45Smiod void *data)
146b305b0f1Sespie {
147c074d1c9Sdrahn asection *s;
148b305b0f1Sespie
149c074d1c9Sdrahn if (file->just_syms_flag)
150b305b0f1Sespie return;
151b305b0f1Sespie
152b305b0f1Sespie for (s = file->the_bfd->sections; s != NULL; s = s->next)
153b305b0f1Sespie {
154c074d1c9Sdrahn struct wildcard_list *sec;
155c074d1c9Sdrahn
156c074d1c9Sdrahn sec = ptr->section_list;
157c074d1c9Sdrahn if (sec == NULL)
158c074d1c9Sdrahn (*callback) (ptr, sec, s, file, data);
159c074d1c9Sdrahn
160c074d1c9Sdrahn while (sec != NULL)
161c074d1c9Sdrahn {
162c074d1c9Sdrahn bfd_boolean skip = FALSE;
163c074d1c9Sdrahn struct name_list *list_tmp;
164c074d1c9Sdrahn
165c074d1c9Sdrahn /* Don't process sections from files which were
166c074d1c9Sdrahn excluded. */
167c074d1c9Sdrahn for (list_tmp = sec->spec.exclude_name_list;
168c074d1c9Sdrahn list_tmp;
169c074d1c9Sdrahn list_tmp = list_tmp->next)
170c074d1c9Sdrahn {
171c074d1c9Sdrahn if (wildcardp (list_tmp->name))
172c074d1c9Sdrahn skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
173c074d1c9Sdrahn else
174c074d1c9Sdrahn skip = strcmp (list_tmp->name, file->filename) == 0;
175c074d1c9Sdrahn
176c074d1c9Sdrahn /* If this file is part of an archive, and the archive is
177c074d1c9Sdrahn excluded, exclude this file. */
178c074d1c9Sdrahn if (! skip && file->the_bfd != NULL
179c074d1c9Sdrahn && file->the_bfd->my_archive != NULL
180c074d1c9Sdrahn && file->the_bfd->my_archive->filename != NULL)
181c074d1c9Sdrahn {
182c074d1c9Sdrahn if (wildcardp (list_tmp->name))
183c074d1c9Sdrahn skip = fnmatch (list_tmp->name,
184c074d1c9Sdrahn file->the_bfd->my_archive->filename,
185c074d1c9Sdrahn 0) == 0;
186c074d1c9Sdrahn else
187c074d1c9Sdrahn skip = strcmp (list_tmp->name,
188c074d1c9Sdrahn file->the_bfd->my_archive->filename) == 0;
189c074d1c9Sdrahn }
190c074d1c9Sdrahn
191c074d1c9Sdrahn if (skip)
192c074d1c9Sdrahn break;
193c074d1c9Sdrahn }
194c074d1c9Sdrahn
195c074d1c9Sdrahn if (!skip && sec->spec.name != NULL)
196c074d1c9Sdrahn {
197b55d4692Sfgsch const char *sname = bfd_get_section_name (file->the_bfd, s);
198b305b0f1Sespie
199c074d1c9Sdrahn if (wildcardp (sec->spec.name))
200c074d1c9Sdrahn skip = fnmatch (sec->spec.name, sname, 0) != 0;
201b305b0f1Sespie else
202c074d1c9Sdrahn skip = strcmp (sec->spec.name, sname) != 0;
203c074d1c9Sdrahn }
204b305b0f1Sespie
205c074d1c9Sdrahn if (!skip)
206c074d1c9Sdrahn (*callback) (ptr, sec, s, file, data);
207c074d1c9Sdrahn
208c074d1c9Sdrahn sec = sec->next;
209b305b0f1Sespie }
210b305b0f1Sespie }
211b305b0f1Sespie }
212b305b0f1Sespie
213b305b0f1Sespie /* Handle a wild statement for a single file F. */
214b305b0f1Sespie
215b305b0f1Sespie static void
walk_wild_file(lang_wild_statement_type * s,lang_input_statement_type * f,callback_t callback,void * data)216*007c2a45Smiod walk_wild_file (lang_wild_statement_type *s,
217*007c2a45Smiod lang_input_statement_type *f,
218*007c2a45Smiod callback_t callback,
219*007c2a45Smiod void *data)
220b305b0f1Sespie {
221b305b0f1Sespie if (f->the_bfd == NULL
222b305b0f1Sespie || ! bfd_check_format (f->the_bfd, bfd_archive))
223c074d1c9Sdrahn walk_wild_section (s, f, callback, data);
224b305b0f1Sespie else
225b305b0f1Sespie {
226b305b0f1Sespie bfd *member;
227b305b0f1Sespie
228b305b0f1Sespie /* This is an archive file. We must map each member of the
229b305b0f1Sespie archive separately. */
230*007c2a45Smiod member = bfd_openr_next_archived_file (f->the_bfd, NULL);
231b305b0f1Sespie while (member != NULL)
232b305b0f1Sespie {
233b305b0f1Sespie /* When lookup_name is called, it will call the add_symbols
234b305b0f1Sespie entry point for the archive. For each element of the
235b305b0f1Sespie archive which is included, BFD will call ldlang_add_file,
236b305b0f1Sespie which will set the usrdata field of the member to the
237b305b0f1Sespie lang_input_statement. */
238b305b0f1Sespie if (member->usrdata != NULL)
239b305b0f1Sespie {
240*007c2a45Smiod walk_wild_section (s, member->usrdata, callback, data);
241b305b0f1Sespie }
242b305b0f1Sespie
243b305b0f1Sespie member = bfd_openr_next_archived_file (f->the_bfd, member);
244b305b0f1Sespie }
245b305b0f1Sespie }
246b305b0f1Sespie }
247b305b0f1Sespie
248b305b0f1Sespie static void
walk_wild(lang_wild_statement_type * s,callback_t callback,void * data)249*007c2a45Smiod walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
250b305b0f1Sespie {
251c074d1c9Sdrahn const char *file_spec = s->filename;
252c074d1c9Sdrahn
253c074d1c9Sdrahn if (file_spec == NULL)
254b305b0f1Sespie {
255b305b0f1Sespie /* Perform the iteration over all files in the list. */
256b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (f)
257b305b0f1Sespie {
258c074d1c9Sdrahn walk_wild_file (s, f, callback, data);
259b305b0f1Sespie }
260b305b0f1Sespie }
261c074d1c9Sdrahn else if (wildcardp (file_spec))
262b305b0f1Sespie {
263b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (f)
264b305b0f1Sespie {
265c074d1c9Sdrahn if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
266c074d1c9Sdrahn walk_wild_file (s, f, callback, data);
267b305b0f1Sespie }
268b305b0f1Sespie }
269b305b0f1Sespie else
270b305b0f1Sespie {
271b305b0f1Sespie lang_input_statement_type *f;
272b305b0f1Sespie
273b305b0f1Sespie /* Perform the iteration over a single file. */
274c074d1c9Sdrahn f = lookup_name (file_spec);
275c074d1c9Sdrahn if (f)
276c074d1c9Sdrahn walk_wild_file (s, f, callback, data);
277b305b0f1Sespie }
278b305b0f1Sespie }
279b305b0f1Sespie
280b55d4692Sfgsch /* lang_for_each_statement walks the parse tree and calls the provided
281b55d4692Sfgsch function for each node. */
2822159047fSniklas
2832159047fSniklas static void
lang_for_each_statement_worker(void (* func)(lang_statement_union_type *),lang_statement_union_type * s)284*007c2a45Smiod lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
285*007c2a45Smiod lang_statement_union_type *s)
2862159047fSniklas {
287*007c2a45Smiod for (; s != NULL; s = s->header.next)
2882159047fSniklas {
2892159047fSniklas func (s);
2902159047fSniklas
2912159047fSniklas switch (s->header.type)
2922159047fSniklas {
2932159047fSniklas case lang_constructors_statement_enum:
2942159047fSniklas lang_for_each_statement_worker (func, constructor_list.head);
2952159047fSniklas break;
2962159047fSniklas case lang_output_section_statement_enum:
2972159047fSniklas lang_for_each_statement_worker
2982159047fSniklas (func,
2992159047fSniklas s->output_section_statement.children.head);
3002159047fSniklas break;
3012159047fSniklas case lang_wild_statement_enum:
3022159047fSniklas lang_for_each_statement_worker
3032159047fSniklas (func,
3042159047fSniklas s->wild_statement.children.head);
3052159047fSniklas break;
3062159047fSniklas case lang_group_statement_enum:
3072159047fSniklas lang_for_each_statement_worker (func,
3082159047fSniklas s->group_statement.children.head);
3092159047fSniklas break;
3102159047fSniklas case lang_data_statement_enum:
3112159047fSniklas case lang_reloc_statement_enum:
3122159047fSniklas case lang_object_symbols_statement_enum:
3132159047fSniklas case lang_output_statement_enum:
3142159047fSniklas case lang_target_statement_enum:
3152159047fSniklas case lang_input_section_enum:
3162159047fSniklas case lang_input_statement_enum:
3172159047fSniklas case lang_assignment_statement_enum:
3182159047fSniklas case lang_padding_statement_enum:
3192159047fSniklas case lang_address_statement_enum:
320191aa565Sniklas case lang_fill_statement_enum:
3212159047fSniklas break;
3222159047fSniklas default:
3232159047fSniklas FAIL ();
3242159047fSniklas break;
3252159047fSniklas }
3262159047fSniklas }
3272159047fSniklas }
3282159047fSniklas
3292159047fSniklas void
lang_for_each_statement(void (* func)(lang_statement_union_type *))330*007c2a45Smiod lang_for_each_statement (void (*func) (lang_statement_union_type *))
3312159047fSniklas {
332b55d4692Sfgsch lang_for_each_statement_worker (func, statement_list.head);
3332159047fSniklas }
3342159047fSniklas
3352159047fSniklas /*----------------------------------------------------------------------*/
336b55d4692Sfgsch
3372159047fSniklas void
lang_list_init(lang_statement_list_type * list)338*007c2a45Smiod lang_list_init (lang_statement_list_type *list)
3392159047fSniklas {
340*007c2a45Smiod list->head = NULL;
3412159047fSniklas list->tail = &list->head;
3422159047fSniklas }
3432159047fSniklas
344b55d4692Sfgsch /* Build a new statement node for the parse tree. */
3452159047fSniklas
346b55d4692Sfgsch static lang_statement_union_type *
new_statement(enum statement_enum type,size_t size,lang_statement_list_type * list)347*007c2a45Smiod new_statement (enum statement_enum type,
348*007c2a45Smiod size_t size,
349*007c2a45Smiod lang_statement_list_type *list)
3502159047fSniklas {
351*007c2a45Smiod lang_statement_union_type *new;
3522159047fSniklas
353*007c2a45Smiod new = stat_alloc (size);
3542159047fSniklas new->header.type = type;
355*007c2a45Smiod new->header.next = NULL;
3562159047fSniklas lang_statement_append (list, new, &new->header.next);
3572159047fSniklas return new;
3582159047fSniklas }
3592159047fSniklas
360b55d4692Sfgsch /* Build a new input file node for the language. There are several
361b55d4692Sfgsch ways in which we treat an input file, eg, we only look at symbols,
362b55d4692Sfgsch or prefix it with a -l etc.
3632159047fSniklas
3642159047fSniklas We can be supplied with requests for input files more than once;
365c074d1c9Sdrahn they may, for example be split over several lines like foo.o(.text)
366b55d4692Sfgsch foo.o(.data) etc, so when asked for a file we check that we haven't
367b55d4692Sfgsch got it already so we don't duplicate the bfd. */
3682159047fSniklas
3692159047fSniklas static lang_input_statement_type *
new_afile(const char * name,lang_input_file_enum_type file_type,const char * target,bfd_boolean add_to_list)370*007c2a45Smiod new_afile (const char *name,
371*007c2a45Smiod lang_input_file_enum_type file_type,
372*007c2a45Smiod const char *target,
373*007c2a45Smiod bfd_boolean add_to_list)
3742159047fSniklas {
3752159047fSniklas lang_input_statement_type *p;
3762159047fSniklas
3772159047fSniklas if (add_to_list)
3782159047fSniklas p = new_stat (lang_input_statement, stat_ptr);
3792159047fSniklas else
3802159047fSniklas {
381*007c2a45Smiod p = stat_alloc (sizeof (lang_input_statement_type));
3822159047fSniklas p->header.next = NULL;
3832159047fSniklas }
3842159047fSniklas
385c074d1c9Sdrahn lang_has_input_file = TRUE;
3862159047fSniklas p->target = target;
387c074d1c9Sdrahn p->sysrooted = FALSE;
3882159047fSniklas switch (file_type)
3892159047fSniklas {
3902159047fSniklas case lang_input_file_is_symbols_only_enum:
3912159047fSniklas p->filename = name;
392c074d1c9Sdrahn p->is_archive = FALSE;
393c074d1c9Sdrahn p->real = TRUE;
3942159047fSniklas p->local_sym_name = name;
395c074d1c9Sdrahn p->just_syms_flag = TRUE;
396c074d1c9Sdrahn p->search_dirs_flag = FALSE;
3972159047fSniklas break;
3982159047fSniklas case lang_input_file_is_fake_enum:
3992159047fSniklas p->filename = name;
400c074d1c9Sdrahn p->is_archive = FALSE;
401c074d1c9Sdrahn p->real = FALSE;
4022159047fSniklas p->local_sym_name = name;
403c074d1c9Sdrahn p->just_syms_flag = FALSE;
404c074d1c9Sdrahn p->search_dirs_flag = FALSE;
4052159047fSniklas break;
4062159047fSniklas case lang_input_file_is_l_enum:
407c074d1c9Sdrahn p->is_archive = TRUE;
4082159047fSniklas p->filename = name;
409c074d1c9Sdrahn p->real = TRUE;
410*007c2a45Smiod p->local_sym_name = concat ("-l", name, NULL);
411c074d1c9Sdrahn p->just_syms_flag = FALSE;
412c074d1c9Sdrahn p->search_dirs_flag = TRUE;
4132159047fSniklas break;
4142159047fSniklas case lang_input_file_is_marker_enum:
4152159047fSniklas p->filename = name;
416c074d1c9Sdrahn p->is_archive = FALSE;
417c074d1c9Sdrahn p->real = FALSE;
4182159047fSniklas p->local_sym_name = name;
419c074d1c9Sdrahn p->just_syms_flag = FALSE;
420c074d1c9Sdrahn p->search_dirs_flag = TRUE;
4212159047fSniklas break;
4222159047fSniklas case lang_input_file_is_search_file_enum:
423c074d1c9Sdrahn p->sysrooted = ldlang_sysrooted_script;
4242159047fSniklas p->filename = name;
425c074d1c9Sdrahn p->is_archive = FALSE;
426c074d1c9Sdrahn p->real = TRUE;
4272159047fSniklas p->local_sym_name = name;
428c074d1c9Sdrahn p->just_syms_flag = FALSE;
429c074d1c9Sdrahn p->search_dirs_flag = TRUE;
4302159047fSniklas break;
4312159047fSniklas case lang_input_file_is_file_enum:
4322159047fSniklas p->filename = name;
433c074d1c9Sdrahn p->is_archive = FALSE;
434c074d1c9Sdrahn p->real = TRUE;
4352159047fSniklas p->local_sym_name = name;
436c074d1c9Sdrahn p->just_syms_flag = FALSE;
437c074d1c9Sdrahn p->search_dirs_flag = FALSE;
4382159047fSniklas break;
4392159047fSniklas default:
4402159047fSniklas FAIL ();
4412159047fSniklas }
442*007c2a45Smiod p->the_bfd = NULL;
443*007c2a45Smiod p->asymbols = NULL;
444*007c2a45Smiod p->next_real_file = NULL;
445*007c2a45Smiod p->next = NULL;
4462159047fSniklas p->symbol_count = 0;
4472159047fSniklas p->dynamic = config.dynamic_link;
448*007c2a45Smiod p->as_needed = as_needed;
449191aa565Sniklas p->whole_archive = whole_archive;
450c074d1c9Sdrahn p->loaded = FALSE;
4512159047fSniklas lang_statement_append (&input_file_chain,
4522159047fSniklas (lang_statement_union_type *) p,
4532159047fSniklas &p->next_real_file);
4542159047fSniklas return p;
4552159047fSniklas }
4562159047fSniklas
4572159047fSniklas lang_input_statement_type *
lang_add_input_file(const char * name,lang_input_file_enum_type file_type,const char * target)458*007c2a45Smiod lang_add_input_file (const char *name,
459*007c2a45Smiod lang_input_file_enum_type file_type,
460*007c2a45Smiod const char *target)
4612159047fSniklas {
462c074d1c9Sdrahn lang_has_input_file = TRUE;
463c074d1c9Sdrahn return new_afile (name, file_type, target, TRUE);
4642159047fSniklas }
4652159047fSniklas
466b55d4692Sfgsch /* Build enough state so that the parser can build its tree. */
467b55d4692Sfgsch
4682159047fSniklas void
lang_init(void)469*007c2a45Smiod lang_init (void)
4702159047fSniklas {
4712159047fSniklas obstack_begin (&stat_obstack, 1000);
4722159047fSniklas
4732159047fSniklas stat_ptr = &statement_list;
4742159047fSniklas
4752159047fSniklas lang_list_init (stat_ptr);
4762159047fSniklas
4772159047fSniklas lang_list_init (&input_file_chain);
4782159047fSniklas lang_list_init (&lang_output_section_statement);
4792159047fSniklas lang_list_init (&file_chain);
480*007c2a45Smiod first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
481*007c2a45Smiod NULL);
482b55d4692Sfgsch abs_output_section =
483b55d4692Sfgsch lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
4842159047fSniklas
4852159047fSniklas abs_output_section->bfd_section = bfd_abs_section_ptr;
4862159047fSniklas
487*007c2a45Smiod /* The value "3" is ad-hoc, somewhat related to the expected number of
488*007c2a45Smiod DEFINED expressions in a linker script. For most default linker
489*007c2a45Smiod scripts, there are none. Why a hash table then? Well, it's somewhat
490*007c2a45Smiod simpler to re-use working machinery than using a linked list in terms
491*007c2a45Smiod of code-complexity here in ld, besides the initialization which just
492*007c2a45Smiod looks like other code here. */
493*007c2a45Smiod if (bfd_hash_table_init_n (&lang_definedness_table,
494*007c2a45Smiod lang_definedness_newfunc, 3) != TRUE)
495*007c2a45Smiod einfo (_("%P%F: out of memory during initialization"));
496*007c2a45Smiod
497*007c2a45Smiod /* Callers of exp_fold_tree need to increment this. */
498*007c2a45Smiod lang_statement_iteration = 0;
4992159047fSniklas }
5002159047fSniklas
5012159047fSniklas /*----------------------------------------------------------------------
5022159047fSniklas A region is an area of memory declared with the
5032159047fSniklas MEMORY { name:org=exp, len=exp ... }
5042159047fSniklas syntax.
5052159047fSniklas
506b55d4692Sfgsch We maintain a list of all the regions here.
5072159047fSniklas
5082159047fSniklas If no regions are specified in the script, then the default is used
509*007c2a45Smiod which is created when looked up to be the entire data space.
510*007c2a45Smiod
511*007c2a45Smiod If create is true we are creating a region inside a MEMORY block.
512*007c2a45Smiod In this case it is probably an error to create a region that has
513*007c2a45Smiod already been created. If we are not inside a MEMORY block it is
514*007c2a45Smiod dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
515*007c2a45Smiod and so we issue a warning. */
5162159047fSniklas
5172159047fSniklas static lang_memory_region_type *lang_memory_region_list;
5182159047fSniklas static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
5192159047fSniklas
5202159047fSniklas lang_memory_region_type *
lang_memory_region_lookup(const char * const name,bfd_boolean create)521*007c2a45Smiod lang_memory_region_lookup (const char *const name, bfd_boolean create)
5222159047fSniklas {
523b305b0f1Sespie lang_memory_region_type *p;
524*007c2a45Smiod lang_memory_region_type *new;
5252159047fSniklas
526c074d1c9Sdrahn /* NAME is NULL for LMA memspecs if no region was specified. */
527c074d1c9Sdrahn if (name == NULL)
528c074d1c9Sdrahn return NULL;
529c074d1c9Sdrahn
530*007c2a45Smiod for (p = lang_memory_region_list; p != NULL; p = p->next)
5312159047fSniklas if (strcmp (p->name, name) == 0)
5322159047fSniklas {
533*007c2a45Smiod if (create)
534*007c2a45Smiod einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name);
5352159047fSniklas return p;
5362159047fSniklas }
5372159047fSniklas
5382159047fSniklas #if 0
5392159047fSniklas /* This code used to always use the first region in the list as the
5402159047fSniklas default region. I changed it to instead use a region
5412159047fSniklas encompassing all of memory as the default region. This permits
5422159047fSniklas NOLOAD sections to work reasonably without requiring a region.
5432159047fSniklas People should specify what region they mean, if they really want
5442159047fSniklas a region. */
545*007c2a45Smiod if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
5462159047fSniklas {
547*007c2a45Smiod if (lang_memory_region_list != NULL)
5482159047fSniklas return lang_memory_region_list;
5492159047fSniklas }
5502159047fSniklas #endif
5512159047fSniklas
552*007c2a45Smiod if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
553*007c2a45Smiod einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
554*007c2a45Smiod
555*007c2a45Smiod new = stat_alloc (sizeof (lang_memory_region_type));
5562159047fSniklas
557b55d4692Sfgsch new->name = xstrdup (name);
558*007c2a45Smiod new->next = NULL;
5592159047fSniklas
5602159047fSniklas *lang_memory_region_list_tail = new;
5612159047fSniklas lang_memory_region_list_tail = &new->next;
5622159047fSniklas new->origin = 0;
563b305b0f1Sespie new->flags = 0;
564b305b0f1Sespie new->not_flags = 0;
5652159047fSniklas new->length = ~(bfd_size_type) 0;
5662159047fSniklas new->current = 0;
567c074d1c9Sdrahn new->had_full_message = FALSE;
5682159047fSniklas
5692159047fSniklas return new;
5702159047fSniklas }
5712159047fSniklas
572b55d4692Sfgsch static lang_memory_region_type *
lang_memory_default(asection * section)573*007c2a45Smiod lang_memory_default (asection *section)
574b305b0f1Sespie {
575b305b0f1Sespie lang_memory_region_type *p;
576b305b0f1Sespie
577b305b0f1Sespie flagword sec_flags = section->flags;
578b305b0f1Sespie
579b305b0f1Sespie /* Override SEC_DATA to mean a writable section. */
580b305b0f1Sespie if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
581b305b0f1Sespie sec_flags |= SEC_DATA;
582b305b0f1Sespie
583*007c2a45Smiod for (p = lang_memory_region_list; p != NULL; p = p->next)
584b305b0f1Sespie {
585b305b0f1Sespie if ((p->flags & sec_flags) != 0
586b305b0f1Sespie && (p->not_flags & sec_flags) == 0)
587b305b0f1Sespie {
588b305b0f1Sespie return p;
589b305b0f1Sespie }
590b305b0f1Sespie }
591*007c2a45Smiod return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
592b305b0f1Sespie }
593b305b0f1Sespie
5942159047fSniklas lang_output_section_statement_type *
lang_output_section_find(const char * const name)595*007c2a45Smiod lang_output_section_find (const char *const name)
5962159047fSniklas {
5972159047fSniklas lang_statement_union_type *u;
5982159047fSniklas lang_output_section_statement_type *lookup;
5992159047fSniklas
600*007c2a45Smiod for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
6012159047fSniklas {
6022159047fSniklas lookup = &u->output_section_statement;
6032159047fSniklas if (strcmp (name, lookup->name) == 0)
6042159047fSniklas return lookup;
6052159047fSniklas }
606*007c2a45Smiod return NULL;
6072159047fSniklas }
6082159047fSniklas
6092159047fSniklas lang_output_section_statement_type *
lang_output_section_statement_lookup(const char * const name)610*007c2a45Smiod lang_output_section_statement_lookup (const char *const name)
6112159047fSniklas {
6122159047fSniklas lang_output_section_statement_type *lookup;
6132159047fSniklas
6142159047fSniklas lookup = lang_output_section_find (name);
615*007c2a45Smiod if (lookup == NULL)
6162159047fSniklas {
617*007c2a45Smiod lookup = new_stat (lang_output_section_statement, stat_ptr);
618*007c2a45Smiod lookup->region = NULL;
619*007c2a45Smiod lookup->lma_region = NULL;
620*007c2a45Smiod lookup->fill = 0;
6212159047fSniklas lookup->block_value = 1;
6222159047fSniklas lookup->name = name;
6232159047fSniklas
624*007c2a45Smiod lookup->next = NULL;
625*007c2a45Smiod lookup->bfd_section = NULL;
626*007c2a45Smiod lookup->processed = 0;
6270c6d0228Sniklas lookup->sectype = normal_section;
628*007c2a45Smiod lookup->addr_tree = NULL;
6292159047fSniklas lang_list_init (&lookup->children);
6302159047fSniklas
631*007c2a45Smiod lookup->memspec = NULL;
6322159047fSniklas lookup->flags = 0;
6332159047fSniklas lookup->subsection_alignment = -1;
6342159047fSniklas lookup->section_alignment = -1;
635*007c2a45Smiod lookup->load_base = NULL;
636c074d1c9Sdrahn lookup->update_dot_tree = NULL;
637191aa565Sniklas lookup->phdrs = NULL;
6382159047fSniklas
6392159047fSniklas lang_statement_append (&lang_output_section_statement,
6402159047fSniklas (lang_statement_union_type *) lookup,
6412159047fSniklas &lookup->next);
6422159047fSniklas }
6432159047fSniklas return lookup;
6442159047fSniklas }
6452159047fSniklas
646b305b0f1Sespie static void
lang_map_flags(flagword flag)647*007c2a45Smiod lang_map_flags (flagword flag)
648b305b0f1Sespie {
649b305b0f1Sespie if (flag & SEC_ALLOC)
650b305b0f1Sespie minfo ("a");
651b305b0f1Sespie
652b305b0f1Sespie if (flag & SEC_CODE)
653b305b0f1Sespie minfo ("x");
654b305b0f1Sespie
655b305b0f1Sespie if (flag & SEC_READONLY)
656b305b0f1Sespie minfo ("r");
657b305b0f1Sespie
658b305b0f1Sespie if (flag & SEC_DATA)
659b305b0f1Sespie minfo ("w");
660b305b0f1Sespie
661b305b0f1Sespie if (flag & SEC_LOAD)
662b305b0f1Sespie minfo ("l");
663b305b0f1Sespie }
664b305b0f1Sespie
6652159047fSniklas void
lang_map(void)666*007c2a45Smiod lang_map (void)
6672159047fSniklas {
6682159047fSniklas lang_memory_region_type *m;
6692159047fSniklas
670b305b0f1Sespie minfo (_("\nMemory Configuration\n\n"));
671b305b0f1Sespie fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
672b305b0f1Sespie _("Name"), _("Origin"), _("Length"), _("Attributes"));
6732159047fSniklas
674*007c2a45Smiod for (m = lang_memory_region_list; m != NULL; m = m->next)
6752159047fSniklas {
676191aa565Sniklas char buf[100];
677191aa565Sniklas int len;
678191aa565Sniklas
6792159047fSniklas fprintf (config.map_file, "%-16s ", m->name);
680191aa565Sniklas
681191aa565Sniklas sprintf_vma (buf, m->origin);
682191aa565Sniklas minfo ("0x%s ", buf);
683191aa565Sniklas len = strlen (buf);
684191aa565Sniklas while (len < 16)
685191aa565Sniklas {
6862159047fSniklas print_space ();
687191aa565Sniklas ++len;
6882159047fSniklas }
689191aa565Sniklas
690b305b0f1Sespie minfo ("0x%V", m->length);
691b305b0f1Sespie if (m->flags || m->not_flags)
692b305b0f1Sespie {
693b305b0f1Sespie #ifndef BFD64
694b305b0f1Sespie minfo (" ");
695b305b0f1Sespie #endif
696b305b0f1Sespie if (m->flags)
697b305b0f1Sespie {
698b305b0f1Sespie print_space ();
699b305b0f1Sespie lang_map_flags (m->flags);
700191aa565Sniklas }
701191aa565Sniklas
702b305b0f1Sespie if (m->not_flags)
703b305b0f1Sespie {
704b305b0f1Sespie minfo (" !");
705b305b0f1Sespie lang_map_flags (m->not_flags);
706b305b0f1Sespie }
707b305b0f1Sespie }
708b305b0f1Sespie
709b305b0f1Sespie print_nl ();
710b305b0f1Sespie }
711b305b0f1Sespie
712b305b0f1Sespie fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
7132159047fSniklas
7142159047fSniklas print_statements ();
7152159047fSniklas }
7162159047fSniklas
7170c6d0228Sniklas /* Initialize an output section. */
7180c6d0228Sniklas
7192159047fSniklas static void
init_os(lang_output_section_statement_type * s)720*007c2a45Smiod init_os (lang_output_section_statement_type *s)
7212159047fSniklas {
722191aa565Sniklas section_userdata_type *new;
723191aa565Sniklas
7240c6d0228Sniklas if (s->bfd_section != NULL)
7250c6d0228Sniklas return;
7260c6d0228Sniklas
727191aa565Sniklas if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
728c074d1c9Sdrahn einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
729191aa565Sniklas
730*007c2a45Smiod new = stat_alloc (sizeof (section_userdata_type));
7312159047fSniklas
7322159047fSniklas s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
733*007c2a45Smiod if (s->bfd_section == NULL)
7342159047fSniklas s->bfd_section = bfd_make_section (output_bfd, s->name);
735*007c2a45Smiod if (s->bfd_section == NULL)
7362159047fSniklas {
737b305b0f1Sespie einfo (_("%P%F: output format %s cannot represent section called %s\n"),
7382159047fSniklas output_bfd->xvec->name, s->name);
7392159047fSniklas }
7402159047fSniklas s->bfd_section->output_section = s->bfd_section;
7412159047fSniklas
742b55d4692Sfgsch /* We initialize an output sections output offset to minus its own
743b55d4692Sfgsch vma to allow us to output a section through itself. */
7442159047fSniklas s->bfd_section->output_offset = 0;
745*007c2a45Smiod get_userdata (s->bfd_section) = new;
7460c6d0228Sniklas
7470c6d0228Sniklas /* If there is a base address, make sure that any sections it might
7480c6d0228Sniklas mention are initialized. */
7490c6d0228Sniklas if (s->addr_tree != NULL)
7500c6d0228Sniklas exp_init_os (s->addr_tree);
751c074d1c9Sdrahn
752c074d1c9Sdrahn if (s->load_base != NULL)
753c074d1c9Sdrahn exp_init_os (s->load_base);
7540c6d0228Sniklas }
7550c6d0228Sniklas
7560c6d0228Sniklas /* Make sure that all output sections mentioned in an expression are
7570c6d0228Sniklas initialized. */
7580c6d0228Sniklas
7590c6d0228Sniklas static void
exp_init_os(etree_type * exp)760*007c2a45Smiod exp_init_os (etree_type *exp)
7610c6d0228Sniklas {
7620c6d0228Sniklas switch (exp->type.node_class)
7630c6d0228Sniklas {
7640c6d0228Sniklas case etree_assign:
7650c6d0228Sniklas exp_init_os (exp->assign.src);
7660c6d0228Sniklas break;
7670c6d0228Sniklas
7680c6d0228Sniklas case etree_binary:
7690c6d0228Sniklas exp_init_os (exp->binary.lhs);
7700c6d0228Sniklas exp_init_os (exp->binary.rhs);
7710c6d0228Sniklas break;
7720c6d0228Sniklas
7730c6d0228Sniklas case etree_trinary:
7740c6d0228Sniklas exp_init_os (exp->trinary.cond);
7750c6d0228Sniklas exp_init_os (exp->trinary.lhs);
7760c6d0228Sniklas exp_init_os (exp->trinary.rhs);
7770c6d0228Sniklas break;
7780c6d0228Sniklas
779*007c2a45Smiod case etree_assert:
780*007c2a45Smiod exp_init_os (exp->assert_s.child);
781*007c2a45Smiod break;
782*007c2a45Smiod
7830c6d0228Sniklas case etree_unary:
7840c6d0228Sniklas exp_init_os (exp->unary.child);
7850c6d0228Sniklas break;
7860c6d0228Sniklas
7870c6d0228Sniklas case etree_name:
7880c6d0228Sniklas switch (exp->type.node_code)
7890c6d0228Sniklas {
7900c6d0228Sniklas case ADDR:
7910c6d0228Sniklas case LOADADDR:
7920c6d0228Sniklas case SIZEOF:
7930c6d0228Sniklas {
7940c6d0228Sniklas lang_output_section_statement_type *os;
7950c6d0228Sniklas
7960c6d0228Sniklas os = lang_output_section_find (exp->name.name);
7970c6d0228Sniklas if (os != NULL && os->bfd_section == NULL)
7980c6d0228Sniklas init_os (os);
7990c6d0228Sniklas }
8000c6d0228Sniklas }
8010c6d0228Sniklas break;
8020c6d0228Sniklas
8030c6d0228Sniklas default:
8040c6d0228Sniklas break;
8050c6d0228Sniklas }
8062159047fSniklas }
807b305b0f1Sespie
808191aa565Sniklas /* Sections marked with the SEC_LINK_ONCE flag should only be linked
809b305b0f1Sespie once into the output. This routine checks each section, and
810b305b0f1Sespie arrange to discard it if a section of the same name has already
811b305b0f1Sespie been linked. If the section has COMDAT information, then it uses
812b305b0f1Sespie that to decide whether the section should be included. This code
813b305b0f1Sespie assumes that all relevant sections have the SEC_LINK_ONCE flag set;
814b305b0f1Sespie that is, it does not depend solely upon the section name.
815b305b0f1Sespie section_already_linked is called via bfd_map_over_sections. */
816b305b0f1Sespie
817b305b0f1Sespie /* This is the shape of the elements inside the already_linked hash
818b305b0f1Sespie table. It maps a name onto a list of already_linked elements with
819b305b0f1Sespie the same name. It's possible to get more than one element in a
820b305b0f1Sespie list if the COMDAT sections have different names. */
821b305b0f1Sespie
822b305b0f1Sespie struct already_linked_hash_entry
823b305b0f1Sespie {
824b305b0f1Sespie struct bfd_hash_entry root;
825b305b0f1Sespie struct already_linked *entry;
826b305b0f1Sespie };
827b305b0f1Sespie
828b305b0f1Sespie struct already_linked
829b305b0f1Sespie {
830b305b0f1Sespie struct already_linked *next;
831b305b0f1Sespie asection *sec;
832b305b0f1Sespie };
833b305b0f1Sespie
834b305b0f1Sespie /* The hash table. */
835b305b0f1Sespie
836b305b0f1Sespie static struct bfd_hash_table already_linked_table;
837191aa565Sniklas
838191aa565Sniklas static void
section_already_linked(bfd * abfd,asection * sec,void * data)839*007c2a45Smiod section_already_linked (bfd *abfd, asection *sec, void *data)
840191aa565Sniklas {
841*007c2a45Smiod lang_input_statement_type *entry = data;
842191aa565Sniklas flagword flags;
843191aa565Sniklas const char *name;
844b305b0f1Sespie struct already_linked *l;
845b305b0f1Sespie struct already_linked_hash_entry *already_linked_list;
846191aa565Sniklas
847e93f7393Sniklas /* If we are only reading symbols from this object, then we want to
848e93f7393Sniklas discard all sections. */
849e93f7393Sniklas if (entry->just_syms_flag)
850e93f7393Sniklas {
851c074d1c9Sdrahn bfd_link_just_syms (sec, &link_info);
852e93f7393Sniklas return;
853e93f7393Sniklas }
854e93f7393Sniklas
855191aa565Sniklas flags = bfd_get_section_flags (abfd, sec);
856191aa565Sniklas
857191aa565Sniklas if ((flags & SEC_LINK_ONCE) == 0)
858191aa565Sniklas return;
859191aa565Sniklas
860b55d4692Sfgsch /* FIXME: When doing a relocatable link, we may have trouble
861b305b0f1Sespie copying relocations in other sections that refer to local symbols
862b305b0f1Sespie in the section being discarded. Those relocations will have to
863b305b0f1Sespie be converted somehow; as of this writing I'm not sure that any of
864b305b0f1Sespie the backends handle that correctly.
865b305b0f1Sespie
866b305b0f1Sespie It is tempting to instead not discard link once sections when
867b55d4692Sfgsch doing a relocatable link (technically, they should be discarded
868b305b0f1Sespie whenever we are building constructors). However, that fails,
869b305b0f1Sespie because the linker winds up combining all the link once sections
870b305b0f1Sespie into a single large link once section, which defeats the purpose
871b305b0f1Sespie of having link once sections in the first place.
872b305b0f1Sespie
873b55d4692Sfgsch Also, not merging link once sections in a relocatable link
874c074d1c9Sdrahn causes trouble for MIPS ELF, which relies on link once semantics
875b305b0f1Sespie to handle the .reginfo section correctly. */
876b305b0f1Sespie
877191aa565Sniklas name = bfd_get_section_name (abfd, sec);
878191aa565Sniklas
879b305b0f1Sespie already_linked_list =
880b305b0f1Sespie ((struct already_linked_hash_entry *)
881c074d1c9Sdrahn bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
882b305b0f1Sespie
883b305b0f1Sespie for (l = already_linked_list->entry; l != NULL; l = l->next)
884191aa565Sniklas {
885b305b0f1Sespie if (sec->comdat == NULL
886b305b0f1Sespie || l->sec->comdat == NULL
887b305b0f1Sespie || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
888191aa565Sniklas {
889191aa565Sniklas /* The section has already been linked. See if we should
890191aa565Sniklas issue a warning. */
891191aa565Sniklas switch (flags & SEC_LINK_DUPLICATES)
892191aa565Sniklas {
893191aa565Sniklas default:
894191aa565Sniklas abort ();
895191aa565Sniklas
896191aa565Sniklas case SEC_LINK_DUPLICATES_DISCARD:
897191aa565Sniklas break;
898191aa565Sniklas
899191aa565Sniklas case SEC_LINK_DUPLICATES_ONE_ONLY:
900b305b0f1Sespie if (sec->comdat == NULL)
901b305b0f1Sespie einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
902191aa565Sniklas abfd, name);
903b305b0f1Sespie else
904b305b0f1Sespie einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
905b305b0f1Sespie abfd, name, sec->comdat->name);
906191aa565Sniklas break;
907191aa565Sniklas
908191aa565Sniklas case SEC_LINK_DUPLICATES_SAME_CONTENTS:
909191aa565Sniklas /* FIXME: We should really dig out the contents of both
910191aa565Sniklas sections and memcmp them. The COFF/PE spec says that
911191aa565Sniklas the Microsoft linker does not implement this
912191aa565Sniklas correctly, so I'm not going to bother doing it
913191aa565Sniklas either. */
914191aa565Sniklas /* Fall through. */
915191aa565Sniklas case SEC_LINK_DUPLICATES_SAME_SIZE:
916191aa565Sniklas if (bfd_section_size (abfd, sec)
917191aa565Sniklas != bfd_section_size (l->sec->owner, l->sec))
918b305b0f1Sespie einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
919191aa565Sniklas abfd, name);
920191aa565Sniklas break;
921191aa565Sniklas }
922191aa565Sniklas
923c074d1c9Sdrahn /* Set the output_section field so that lang_add_section
924c074d1c9Sdrahn does not create a lang_input_section structure for this
925*007c2a45Smiod section. Since there might be a symbol in the section
926*007c2a45Smiod being discarded, we must retain a pointer to the section
927*007c2a45Smiod which we are really going to use. */
928191aa565Sniklas sec->output_section = bfd_abs_section_ptr;
929*007c2a45Smiod sec->kept_section = l->sec;
930c074d1c9Sdrahn
931c074d1c9Sdrahn if (flags & SEC_GROUP)
932c074d1c9Sdrahn bfd_discard_group (abfd, sec);
933191aa565Sniklas
934191aa565Sniklas return;
935191aa565Sniklas }
936191aa565Sniklas }
937191aa565Sniklas
938b305b0f1Sespie /* This is the first section with this name. Record it. Allocate
939b305b0f1Sespie the memory from the same obstack as the hash table is kept in. */
940191aa565Sniklas
941*007c2a45Smiod l = bfd_hash_allocate (&already_linked_table, sizeof *l);
942b305b0f1Sespie
943191aa565Sniklas l->sec = sec;
944b305b0f1Sespie l->next = already_linked_list->entry;
945b305b0f1Sespie already_linked_list->entry = l;
946b305b0f1Sespie }
947b305b0f1Sespie
948b305b0f1Sespie /* Support routines for the hash table used by section_already_linked,
949b305b0f1Sespie initialize the table, fill in an entry and remove the table. */
950b305b0f1Sespie
951b305b0f1Sespie static struct bfd_hash_entry *
already_linked_newfunc(struct bfd_hash_entry * entry ATTRIBUTE_UNUSED,struct bfd_hash_table * table,const char * string ATTRIBUTE_UNUSED)952*007c2a45Smiod already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
953*007c2a45Smiod struct bfd_hash_table *table,
954*007c2a45Smiod const char *string ATTRIBUTE_UNUSED)
955b305b0f1Sespie {
956b305b0f1Sespie struct already_linked_hash_entry *ret =
957b305b0f1Sespie bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
958b305b0f1Sespie
959b305b0f1Sespie ret->entry = NULL;
960b305b0f1Sespie
961*007c2a45Smiod return &ret->root;
962b305b0f1Sespie }
963b305b0f1Sespie
964b305b0f1Sespie static void
already_linked_table_init(void)965*007c2a45Smiod already_linked_table_init (void)
966b305b0f1Sespie {
967b305b0f1Sespie if (! bfd_hash_table_init_n (&already_linked_table,
968b305b0f1Sespie already_linked_newfunc,
969b305b0f1Sespie 42))
970b305b0f1Sespie einfo (_("%P%F: Failed to create hash table\n"));
971b305b0f1Sespie }
972b305b0f1Sespie
973b305b0f1Sespie static void
already_linked_table_free(void)974*007c2a45Smiod already_linked_table_free (void)
975b305b0f1Sespie {
976b305b0f1Sespie bfd_hash_table_free (&already_linked_table);
977191aa565Sniklas }
978191aa565Sniklas
979191aa565Sniklas /* The wild routines.
9802159047fSniklas
9812159047fSniklas These expand statements like *(.text) and foo.o to a list of
9822159047fSniklas explicit actions, like foo.o(.text), bar.o(.text) and
983191aa565Sniklas foo.o(.text, .data). */
9842159047fSniklas
985c074d1c9Sdrahn /* Return TRUE if the PATTERN argument is a wildcard pattern.
986b305b0f1Sespie Although backslashes are treated specially if a pattern contains
987b305b0f1Sespie wildcards, we do not consider the mere presence of a backslash to
988c074d1c9Sdrahn be enough to cause the pattern to be treated as a wildcard.
989b305b0f1Sespie That lets us handle DOS filenames more naturally. */
9900c6d0228Sniklas
991c074d1c9Sdrahn static bfd_boolean
wildcardp(const char * pattern)992*007c2a45Smiod wildcardp (const char *pattern)
9930c6d0228Sniklas {
9940c6d0228Sniklas const char *s;
9950c6d0228Sniklas
9960c6d0228Sniklas for (s = pattern; *s != '\0'; ++s)
9970c6d0228Sniklas if (*s == '?'
9980c6d0228Sniklas || *s == '*'
9990c6d0228Sniklas || *s == '[')
1000c074d1c9Sdrahn return TRUE;
1001c074d1c9Sdrahn return FALSE;
10020c6d0228Sniklas }
10030c6d0228Sniklas
1004191aa565Sniklas /* Add SECTION to the output section OUTPUT. Do this by creating a
1005191aa565Sniklas lang_input_section statement which is placed at PTR. FILE is the
1006191aa565Sniklas input file which holds SECTION. */
10072159047fSniklas
10082159047fSniklas void
lang_add_section(lang_statement_list_type * ptr,asection * section,lang_output_section_statement_type * output,lang_input_statement_type * file)1009*007c2a45Smiod lang_add_section (lang_statement_list_type *ptr,
1010*007c2a45Smiod asection *section,
1011*007c2a45Smiod lang_output_section_statement_type *output,
1012*007c2a45Smiod lang_input_statement_type *file)
10132159047fSniklas {
1014191aa565Sniklas flagword flags;
1015c074d1c9Sdrahn bfd_boolean discard;
1016191aa565Sniklas
1017191aa565Sniklas flags = bfd_get_section_flags (section->owner, section);
1018191aa565Sniklas
1019c074d1c9Sdrahn discard = FALSE;
1020191aa565Sniklas
1021c074d1c9Sdrahn /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1022c074d1c9Sdrahn link. Discard debugging sections marked with SEC_EXCLUDE on a
1023c074d1c9Sdrahn relocatable link too. */
1024c074d1c9Sdrahn if ((flags & SEC_EXCLUDE) != 0
1025*007c2a45Smiod && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
1026c074d1c9Sdrahn discard = TRUE;
1027191aa565Sniklas
1028191aa565Sniklas /* Discard input sections which are assigned to a section named
1029191aa565Sniklas DISCARD_SECTION_NAME. */
1030191aa565Sniklas if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1031c074d1c9Sdrahn discard = TRUE;
1032191aa565Sniklas
1033191aa565Sniklas /* Discard debugging sections if we are stripping debugging
1034191aa565Sniklas information. */
1035191aa565Sniklas if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1036191aa565Sniklas && (flags & SEC_DEBUGGING) != 0)
1037c074d1c9Sdrahn discard = TRUE;
1038191aa565Sniklas
1039191aa565Sniklas if (discard)
1040191aa565Sniklas {
1041191aa565Sniklas if (section->output_section == NULL)
1042191aa565Sniklas {
1043191aa565Sniklas /* This prevents future calls from assigning this section. */
1044191aa565Sniklas section->output_section = bfd_abs_section_ptr;
1045191aa565Sniklas }
1046191aa565Sniklas return;
1047191aa565Sniklas }
1048191aa565Sniklas
1049191aa565Sniklas if (section->output_section == NULL)
1050191aa565Sniklas {
1051c074d1c9Sdrahn bfd_boolean first;
1052191aa565Sniklas lang_input_section_type *new;
1053b305b0f1Sespie flagword flags;
1054191aa565Sniklas
1055191aa565Sniklas if (output->bfd_section == NULL)
10562159047fSniklas init_os (output);
10572159047fSniklas
1058b55d4692Sfgsch first = ! output->bfd_section->linker_has_input;
1059b55d4692Sfgsch output->bfd_section->linker_has_input = 1;
1060b55d4692Sfgsch
1061b55d4692Sfgsch /* Add a section reference to the list. */
1062191aa565Sniklas new = new_stat (lang_input_section, ptr);
10632159047fSniklas
10642159047fSniklas new->section = section;
10652159047fSniklas new->ifile = file;
10662159047fSniklas section->output_section = output->bfd_section;
10672159047fSniklas
1068b305b0f1Sespie flags = section->flags;
1069b305b0f1Sespie
1070191aa565Sniklas /* We don't copy the SEC_NEVER_LOAD flag from an input section
1071191aa565Sniklas to an output section, because we want to be able to include a
10722159047fSniklas SEC_NEVER_LOAD section in the middle of an otherwise loaded
10732159047fSniklas section (I don't know why we want to do this, but we do).
1074191aa565Sniklas build_link_order in ldwrite.c handles this case by turning
1075b305b0f1Sespie the embedded SEC_NEVER_LOAD section into a fill. */
1076191aa565Sniklas
1077b305b0f1Sespie flags &= ~ SEC_NEVER_LOAD;
1078191aa565Sniklas
1079b305b0f1Sespie /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1080b305b0f1Sespie already been processed. One reason to do this is that on pe
1081b305b0f1Sespie format targets, .text$foo sections go into .text and it's odd
1082b305b0f1Sespie to see .text with SEC_LINK_ONCE set. */
1083b305b0f1Sespie
1084*007c2a45Smiod if (! link_info.relocatable)
1085b305b0f1Sespie flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1086b305b0f1Sespie
1087b305b0f1Sespie /* If this is not the first input section, and the SEC_READONLY
1088b305b0f1Sespie flag is not currently set, then don't set it just because the
1089b305b0f1Sespie input section has it set. */
1090b305b0f1Sespie
1091b305b0f1Sespie if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1092b305b0f1Sespie flags &= ~ SEC_READONLY;
1093b305b0f1Sespie
1094c074d1c9Sdrahn /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1095c074d1c9Sdrahn if (! first
1096c074d1c9Sdrahn && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1097c074d1c9Sdrahn != (flags & (SEC_MERGE | SEC_STRINGS))
1098c074d1c9Sdrahn || ((flags & SEC_MERGE)
1099c074d1c9Sdrahn && section->output_section->entsize != section->entsize)))
1100c074d1c9Sdrahn {
1101c074d1c9Sdrahn section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1102c074d1c9Sdrahn flags &= ~ (SEC_MERGE | SEC_STRINGS);
1103c074d1c9Sdrahn }
1104c074d1c9Sdrahn
1105b305b0f1Sespie section->output_section->flags |= flags;
1106b305b0f1Sespie
1107c074d1c9Sdrahn if (flags & SEC_MERGE)
1108c074d1c9Sdrahn section->output_section->entsize = section->entsize;
1109c074d1c9Sdrahn
1110b305b0f1Sespie /* If SEC_READONLY is not set in the input section, then clear
1111b305b0f1Sespie it from the output section. */
1112b305b0f1Sespie if ((section->flags & SEC_READONLY) == 0)
1113b305b0f1Sespie section->output_section->flags &= ~SEC_READONLY;
11142159047fSniklas
11150c6d0228Sniklas switch (output->sectype)
11162159047fSniklas {
11170c6d0228Sniklas case normal_section:
11180c6d0228Sniklas break;
11190c6d0228Sniklas case dsect_section:
11200c6d0228Sniklas case copy_section:
11210c6d0228Sniklas case info_section:
11220c6d0228Sniklas case overlay_section:
11230c6d0228Sniklas output->bfd_section->flags &= ~SEC_ALLOC;
11240c6d0228Sniklas break;
11250c6d0228Sniklas case noload_section:
11262159047fSniklas output->bfd_section->flags &= ~SEC_LOAD;
11272159047fSniklas output->bfd_section->flags |= SEC_NEVER_LOAD;
11280c6d0228Sniklas break;
11292159047fSniklas }
1130191aa565Sniklas
1131b305b0f1Sespie /* Copy over SEC_SMALL_DATA. */
1132b305b0f1Sespie if (section->flags & SEC_SMALL_DATA)
1133b305b0f1Sespie section->output_section->flags |= SEC_SMALL_DATA;
1134b305b0f1Sespie
11352159047fSniklas if (section->alignment_power > output->bfd_section->alignment_power)
11362159047fSniklas output->bfd_section->alignment_power = section->alignment_power;
1137191aa565Sniklas
1138c074d1c9Sdrahn /* If supplied an alignment, then force it. */
11392159047fSniklas if (output->section_alignment != -1)
11402159047fSniklas output->bfd_section->alignment_power = output->section_alignment;
1141b55d4692Sfgsch
1142b55d4692Sfgsch if (section->flags & SEC_BLOCK)
1143b55d4692Sfgsch {
1144b55d4692Sfgsch section->output_section->flags |= SEC_BLOCK;
1145b55d4692Sfgsch /* FIXME: This value should really be obtained from the bfd... */
1146b55d4692Sfgsch output->block_value = 128;
1147b55d4692Sfgsch }
11482159047fSniklas }
11492159047fSniklas }
1150191aa565Sniklas
1151b305b0f1Sespie /* Handle wildcard sorting. This returns the lang_input_section which
1152b305b0f1Sespie should follow the one we are going to create for SECTION and FILE,
1153b305b0f1Sespie based on the sorting requirements of WILD. It returns NULL if the
1154b305b0f1Sespie new section should just go at the end of the current list. */
1155b305b0f1Sespie
1156b305b0f1Sespie static lang_statement_union_type *
wild_sort(lang_wild_statement_type * wild,struct wildcard_list * sec,lang_input_statement_type * file,asection * section)1157*007c2a45Smiod wild_sort (lang_wild_statement_type *wild,
1158*007c2a45Smiod struct wildcard_list *sec,
1159*007c2a45Smiod lang_input_statement_type *file,
1160*007c2a45Smiod asection *section)
1161b305b0f1Sespie {
1162b305b0f1Sespie const char *section_name;
1163b305b0f1Sespie lang_statement_union_type *l;
1164b305b0f1Sespie
1165c074d1c9Sdrahn if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1166b305b0f1Sespie return NULL;
1167b305b0f1Sespie
1168b305b0f1Sespie section_name = bfd_get_section_name (file->the_bfd, section);
1169c074d1c9Sdrahn for (l = wild->children.head; l != NULL; l = l->header.next)
1170b305b0f1Sespie {
1171b305b0f1Sespie lang_input_section_type *ls;
1172b305b0f1Sespie
1173b305b0f1Sespie if (l->header.type != lang_input_section_enum)
1174b305b0f1Sespie continue;
1175b305b0f1Sespie ls = &l->input_section;
1176b305b0f1Sespie
1177b305b0f1Sespie /* Sorting by filename takes precedence over sorting by section
1178b305b0f1Sespie name. */
1179b305b0f1Sespie
1180b305b0f1Sespie if (wild->filenames_sorted)
1181b305b0f1Sespie {
1182b305b0f1Sespie const char *fn, *ln;
1183c074d1c9Sdrahn bfd_boolean fa, la;
1184b305b0f1Sespie int i;
1185b305b0f1Sespie
1186b305b0f1Sespie /* The PE support for the .idata section as generated by
1187b305b0f1Sespie dlltool assumes that files will be sorted by the name of
1188b305b0f1Sespie the archive and then the name of the file within the
1189b305b0f1Sespie archive. */
1190b305b0f1Sespie
1191b305b0f1Sespie if (file->the_bfd != NULL
1192b305b0f1Sespie && bfd_my_archive (file->the_bfd) != NULL)
1193b305b0f1Sespie {
1194b305b0f1Sespie fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1195c074d1c9Sdrahn fa = TRUE;
1196b305b0f1Sespie }
1197b305b0f1Sespie else
1198b305b0f1Sespie {
1199b305b0f1Sespie fn = file->filename;
1200c074d1c9Sdrahn fa = FALSE;
1201b305b0f1Sespie }
1202b305b0f1Sespie
1203b305b0f1Sespie if (ls->ifile->the_bfd != NULL
1204b305b0f1Sespie && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1205b305b0f1Sespie {
1206b305b0f1Sespie ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1207c074d1c9Sdrahn la = TRUE;
1208b305b0f1Sespie }
1209b305b0f1Sespie else
1210b305b0f1Sespie {
1211b305b0f1Sespie ln = ls->ifile->filename;
1212c074d1c9Sdrahn la = FALSE;
1213b305b0f1Sespie }
1214b305b0f1Sespie
1215b305b0f1Sespie i = strcmp (fn, ln);
1216b305b0f1Sespie if (i > 0)
1217b305b0f1Sespie continue;
1218b305b0f1Sespie else if (i < 0)
1219b305b0f1Sespie break;
1220b305b0f1Sespie
1221b305b0f1Sespie if (fa || la)
1222b305b0f1Sespie {
1223b305b0f1Sespie if (fa)
1224b305b0f1Sespie fn = file->filename;
1225b305b0f1Sespie if (la)
1226b305b0f1Sespie ln = ls->ifile->filename;
1227b305b0f1Sespie
1228b305b0f1Sespie i = strcmp (fn, ln);
1229b305b0f1Sespie if (i > 0)
1230b305b0f1Sespie continue;
1231b305b0f1Sespie else if (i < 0)
1232b305b0f1Sespie break;
1233b305b0f1Sespie }
1234b305b0f1Sespie }
1235b305b0f1Sespie
1236b305b0f1Sespie /* Here either the files are not sorted by name, or we are
1237b305b0f1Sespie looking at the sections for this file. */
1238b305b0f1Sespie
1239c074d1c9Sdrahn if (sec != NULL && sec->spec.sorted)
1240b305b0f1Sespie {
1241b305b0f1Sespie if (strcmp (section_name,
1242b305b0f1Sespie bfd_get_section_name (ls->ifile->the_bfd,
1243b305b0f1Sespie ls->section))
1244b305b0f1Sespie < 0)
1245b305b0f1Sespie break;
1246b305b0f1Sespie }
1247b305b0f1Sespie }
1248b305b0f1Sespie
1249b305b0f1Sespie return l;
1250b305b0f1Sespie }
1251b305b0f1Sespie
1252191aa565Sniklas /* Expand a wild statement for a particular FILE. SECTION may be
1253191aa565Sniklas NULL, in which case it is a wild card. */
12542159047fSniklas
12552159047fSniklas static void
output_section_callback(lang_wild_statement_type * ptr,struct wildcard_list * sec,asection * section,lang_input_statement_type * file,void * output)1256*007c2a45Smiod output_section_callback (lang_wild_statement_type *ptr,
1257*007c2a45Smiod struct wildcard_list *sec,
1258*007c2a45Smiod asection *section,
1259*007c2a45Smiod lang_input_statement_type *file,
1260*007c2a45Smiod void *output)
12612159047fSniklas {
1262b305b0f1Sespie lang_statement_union_type *before;
12630c6d0228Sniklas
1264c074d1c9Sdrahn /* Exclude sections that match UNIQUE_SECTION_LIST. */
1265c074d1c9Sdrahn if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1266c074d1c9Sdrahn return;
1267c074d1c9Sdrahn
1268b305b0f1Sespie /* If the wild pattern was marked KEEP, the member sections
1269b305b0f1Sespie should be as well. */
1270b305b0f1Sespie if (ptr->keep_sections)
1271b305b0f1Sespie section->flags |= SEC_KEEP;
12722159047fSniklas
1273c074d1c9Sdrahn before = wild_sort (ptr, sec, file, section);
12740c6d0228Sniklas
1275b305b0f1Sespie /* Here BEFORE points to the lang_input_section which
1276b305b0f1Sespie should follow the one we are about to add. If BEFORE
1277b305b0f1Sespie is NULL, then the section should just go at the end
1278b305b0f1Sespie of the current list. */
12792159047fSniklas
1280b305b0f1Sespie if (before == NULL)
1281c074d1c9Sdrahn lang_add_section (&ptr->children, section,
1282b305b0f1Sespie (lang_output_section_statement_type *) output,
1283b305b0f1Sespie file);
12840c6d0228Sniklas else
12850c6d0228Sniklas {
1286b305b0f1Sespie lang_statement_list_type list;
1287b305b0f1Sespie lang_statement_union_type **pp;
12880c6d0228Sniklas
1289b305b0f1Sespie lang_list_init (&list);
1290c074d1c9Sdrahn lang_add_section (&list, section,
1291b305b0f1Sespie (lang_output_section_statement_type *) output,
1292b305b0f1Sespie file);
1293b305b0f1Sespie
1294b305b0f1Sespie /* If we are discarding the section, LIST.HEAD will
1295b305b0f1Sespie be NULL. */
1296b305b0f1Sespie if (list.head != NULL)
1297b305b0f1Sespie {
1298c074d1c9Sdrahn ASSERT (list.head->header.next == NULL);
1299b305b0f1Sespie
1300b305b0f1Sespie for (pp = &ptr->children.head;
1301b305b0f1Sespie *pp != before;
1302c074d1c9Sdrahn pp = &(*pp)->header.next)
1303b305b0f1Sespie ASSERT (*pp != NULL);
1304b305b0f1Sespie
1305c074d1c9Sdrahn list.head->header.next = *pp;
1306b305b0f1Sespie *pp = list.head;
13072159047fSniklas }
13082159047fSniklas }
13092159047fSniklas }
13102159047fSniklas
1311191aa565Sniklas /* This is passed a file name which must have been seen already and
1312191aa565Sniklas added to the statement tree. We will see if it has been opened
1313191aa565Sniklas already and had its symbols read. If not then we'll read it. */
13142159047fSniklas
13152159047fSniklas static lang_input_statement_type *
lookup_name(const char * name)1316*007c2a45Smiod lookup_name (const char *name)
13172159047fSniklas {
13182159047fSniklas lang_input_statement_type *search;
13192159047fSniklas
13202159047fSniklas for (search = (lang_input_statement_type *) input_file_chain.head;
1321*007c2a45Smiod search != NULL;
13222159047fSniklas search = (lang_input_statement_type *) search->next_real_file)
13232159047fSniklas {
1324*007c2a45Smiod /* Use the local_sym_name as the name of the file that has
1325*007c2a45Smiod already been loaded as filename might have been transformed
1326*007c2a45Smiod via the search directory lookup mechanism. */
1327*007c2a45Smiod const char * filename = search->local_sym_name;
1328*007c2a45Smiod
1329*007c2a45Smiod if (filename == NULL && name == NULL)
13302159047fSniklas return search;
1331*007c2a45Smiod if (filename != NULL
1332*007c2a45Smiod && name != NULL
1333*007c2a45Smiod && strcmp (filename, name) == 0)
13342159047fSniklas break;
13352159047fSniklas }
13362159047fSniklas
1337*007c2a45Smiod if (search == NULL)
1338*007c2a45Smiod search = new_afile (name, lang_input_file_is_search_file_enum, default_target,
1339c074d1c9Sdrahn FALSE);
13402159047fSniklas
13412159047fSniklas /* If we have already added this file, or this file is not real
13422159047fSniklas (FIXME: can that ever actually happen?) or the name is NULL
13432159047fSniklas (FIXME: can that ever actually happen?) don't add this file. */
13442159047fSniklas if (search->loaded
13452159047fSniklas || ! search->real
1346*007c2a45Smiod || search->filename == NULL)
13472159047fSniklas return search;
13482159047fSniklas
1349*007c2a45Smiod if (! load_symbols (search, NULL))
1350c074d1c9Sdrahn return NULL;
13512159047fSniklas
13522159047fSniklas return search;
13532159047fSniklas }
13542159047fSniklas
13552159047fSniklas /* Get the symbols for an input file. */
13562159047fSniklas
1357c074d1c9Sdrahn static bfd_boolean
load_symbols(lang_input_statement_type * entry,lang_statement_list_type * place)1358*007c2a45Smiod load_symbols (lang_input_statement_type *entry,
1359*007c2a45Smiod lang_statement_list_type *place)
13602159047fSniklas {
13612159047fSniklas char **matching;
13622159047fSniklas
13632159047fSniklas if (entry->loaded)
1364c074d1c9Sdrahn return TRUE;
13652159047fSniklas
13662159047fSniklas ldfile_open_file (entry);
13672159047fSniklas
13682159047fSniklas if (! bfd_check_format (entry->the_bfd, bfd_archive)
13692159047fSniklas && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
13702159047fSniklas {
13712159047fSniklas bfd_error_type err;
13722159047fSniklas lang_statement_list_type *hold;
1373c074d1c9Sdrahn bfd_boolean bad_load = TRUE;
1374c074d1c9Sdrahn bfd_boolean save_ldlang_sysrooted_script;
13752159047fSniklas
13762159047fSniklas err = bfd_get_error ();
1377b55d4692Sfgsch
1378b55d4692Sfgsch /* See if the emulation has some special knowledge. */
1379b55d4692Sfgsch if (ldemul_unrecognized_file (entry))
1380c074d1c9Sdrahn return TRUE;
1381b55d4692Sfgsch
13822159047fSniklas if (err == bfd_error_file_ambiguously_recognized)
13832159047fSniklas {
13842159047fSniklas char **p;
13852159047fSniklas
1386b305b0f1Sespie einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1387b305b0f1Sespie einfo (_("%B: matching formats:"), entry->the_bfd);
13882159047fSniklas for (p = matching; *p != NULL; p++)
13892159047fSniklas einfo (" %s", *p);
13902159047fSniklas einfo ("%F\n");
13912159047fSniklas }
13922159047fSniklas else if (err != bfd_error_file_not_recognized
13932159047fSniklas || place == NULL)
1394b305b0f1Sespie einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1395c074d1c9Sdrahn else
1396c074d1c9Sdrahn bad_load = FALSE;
13972159047fSniklas
13982159047fSniklas bfd_close (entry->the_bfd);
13992159047fSniklas entry->the_bfd = NULL;
14002159047fSniklas
1401191aa565Sniklas /* Try to interpret the file as a linker script. */
14022159047fSniklas ldfile_open_command_file (entry->filename);
14032159047fSniklas
14042159047fSniklas hold = stat_ptr;
14052159047fSniklas stat_ptr = place;
1406c074d1c9Sdrahn save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1407c074d1c9Sdrahn ldlang_sysrooted_script = entry->sysrooted;
14082159047fSniklas
1409c074d1c9Sdrahn ldfile_assumed_script = TRUE;
14102159047fSniklas parser_input = input_script;
14112159047fSniklas yyparse ();
1412c074d1c9Sdrahn ldfile_assumed_script = FALSE;
14132159047fSniklas
1414c074d1c9Sdrahn ldlang_sysrooted_script = save_ldlang_sysrooted_script;
14152159047fSniklas stat_ptr = hold;
14162159047fSniklas
1417c074d1c9Sdrahn return ! bad_load;
14182159047fSniklas }
14192159047fSniklas
1420b305b0f1Sespie if (ldemul_recognized_file (entry))
1421c074d1c9Sdrahn return TRUE;
1422b305b0f1Sespie
14232159047fSniklas /* We don't call ldlang_add_file for an archive. Instead, the
14242159047fSniklas add_symbols entry point will call ldlang_add_file, via the
14252159047fSniklas add_archive_element callback, for each element of the archive
14262159047fSniklas which is used. */
14272159047fSniklas switch (bfd_get_format (entry->the_bfd))
14282159047fSniklas {
14292159047fSniklas default:
14302159047fSniklas break;
14312159047fSniklas
14322159047fSniklas case bfd_object:
14332159047fSniklas ldlang_add_file (entry);
14342159047fSniklas if (trace_files || trace_file_tries)
14352159047fSniklas info_msg ("%I\n", entry);
14362159047fSniklas break;
14372159047fSniklas
14382159047fSniklas case bfd_archive:
1439191aa565Sniklas if (entry->whole_archive)
14402159047fSniklas {
1441c074d1c9Sdrahn bfd *member = NULL;
1442c074d1c9Sdrahn bfd_boolean loaded = TRUE;
1443c074d1c9Sdrahn
1444c074d1c9Sdrahn for (;;)
14452159047fSniklas {
1446c074d1c9Sdrahn member = bfd_openr_next_archived_file (entry->the_bfd, member);
1447c074d1c9Sdrahn
1448c074d1c9Sdrahn if (member == NULL)
1449c074d1c9Sdrahn break;
1450c074d1c9Sdrahn
14512159047fSniklas if (! bfd_check_format (member, bfd_object))
1452c074d1c9Sdrahn {
1453c074d1c9Sdrahn einfo (_("%F%B: member %B in archive is not an object\n"),
14542159047fSniklas entry->the_bfd, member);
1455c074d1c9Sdrahn loaded = FALSE;
1456c074d1c9Sdrahn }
1457c074d1c9Sdrahn
14582159047fSniklas if (! ((*link_info.callbacks->add_archive_element)
1459191aa565Sniklas (&link_info, member, "--whole-archive")))
14602159047fSniklas abort ();
1461c074d1c9Sdrahn
14622159047fSniklas if (! bfd_link_add_symbols (member, &link_info))
1463c074d1c9Sdrahn {
1464b305b0f1Sespie einfo (_("%F%B: could not read symbols: %E\n"), member);
1465c074d1c9Sdrahn loaded = FALSE;
14662159047fSniklas }
14672159047fSniklas }
14682159047fSniklas
1469c074d1c9Sdrahn entry->loaded = loaded;
1470c074d1c9Sdrahn return loaded;
1471c074d1c9Sdrahn }
1472c074d1c9Sdrahn break;
1473c074d1c9Sdrahn }
1474c074d1c9Sdrahn
1475c074d1c9Sdrahn if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1476c074d1c9Sdrahn entry->loaded = TRUE;
1477c074d1c9Sdrahn else
1478b305b0f1Sespie einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
14792159047fSniklas
1480c074d1c9Sdrahn return entry->loaded;
14812159047fSniklas }
14822159047fSniklas
1483c074d1c9Sdrahn /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1484c074d1c9Sdrahn may be NULL, indicating that it is a wildcard. Separate
1485c074d1c9Sdrahn lang_input_section statements are created for each part of the
1486c074d1c9Sdrahn expansion; they are added after the wild statement S. OUTPUT is
1487c074d1c9Sdrahn the output section. */
1488191aa565Sniklas
14892159047fSniklas static void
wild(lang_wild_statement_type * s,const char * target ATTRIBUTE_UNUSED,lang_output_section_statement_type * output)1490*007c2a45Smiod wild (lang_wild_statement_type *s,
1491*007c2a45Smiod const char *target ATTRIBUTE_UNUSED,
1492*007c2a45Smiod lang_output_section_statement_type *output)
14932159047fSniklas {
1494c074d1c9Sdrahn struct wildcard_list *sec;
1495191aa565Sniklas
1496*007c2a45Smiod walk_wild (s, output_section_callback, output);
1497c074d1c9Sdrahn
1498c074d1c9Sdrahn for (sec = s->section_list; sec != NULL; sec = sec->next)
14992159047fSniklas {
1500c074d1c9Sdrahn if (default_common_section != NULL)
1501c074d1c9Sdrahn break;
1502c074d1c9Sdrahn if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1503c074d1c9Sdrahn {
1504c074d1c9Sdrahn /* Remember the section that common is going to in case we
1505c074d1c9Sdrahn later get something which doesn't know where to put it. */
15062159047fSniklas default_common_section = output;
15072159047fSniklas }
15082159047fSniklas }
1509c074d1c9Sdrahn }
15102159047fSniklas
1511c074d1c9Sdrahn /* Return TRUE iff target is the sought target. */
1512b55d4692Sfgsch
1513b305b0f1Sespie static int
get_target(const bfd_target * target,void * data)1514*007c2a45Smiod get_target (const bfd_target *target, void *data)
1515b305b0f1Sespie {
1516*007c2a45Smiod const char *sought = data;
1517b305b0f1Sespie
1518b305b0f1Sespie return strcmp (target->name, sought) == 0;
1519b305b0f1Sespie }
1520b305b0f1Sespie
1521b305b0f1Sespie /* Like strcpy() but convert to lower case as well. */
1522b55d4692Sfgsch
1523b305b0f1Sespie static void
stricpy(char * dest,char * src)1524*007c2a45Smiod stricpy (char *dest, char *src)
1525b305b0f1Sespie {
1526b305b0f1Sespie char c;
1527b305b0f1Sespie
1528b305b0f1Sespie while ((c = *src++) != 0)
1529c074d1c9Sdrahn *dest++ = TOLOWER (c);
1530b305b0f1Sespie
1531b305b0f1Sespie *dest = 0;
1532b305b0f1Sespie }
1533b305b0f1Sespie
1534c074d1c9Sdrahn /* Remove the first occurrence of needle (if any) in haystack
1535b305b0f1Sespie from haystack. */
1536b55d4692Sfgsch
1537b305b0f1Sespie static void
strcut(char * haystack,char * needle)1538*007c2a45Smiod strcut (char *haystack, char *needle)
1539b305b0f1Sespie {
1540b305b0f1Sespie haystack = strstr (haystack, needle);
1541b305b0f1Sespie
1542b305b0f1Sespie if (haystack)
1543b305b0f1Sespie {
1544b305b0f1Sespie char *src;
1545b305b0f1Sespie
1546b305b0f1Sespie for (src = haystack + strlen (needle); *src;)
1547b305b0f1Sespie *haystack++ = *src++;
1548b305b0f1Sespie
1549b305b0f1Sespie *haystack = 0;
1550b305b0f1Sespie }
1551b305b0f1Sespie }
1552b305b0f1Sespie
1553b305b0f1Sespie /* Compare two target format name strings.
1554b305b0f1Sespie Return a value indicating how "similar" they are. */
1555b55d4692Sfgsch
1556b305b0f1Sespie static int
name_compare(char * first,char * second)1557*007c2a45Smiod name_compare (char *first, char *second)
1558b305b0f1Sespie {
1559b305b0f1Sespie char *copy1;
1560b305b0f1Sespie char *copy2;
1561b305b0f1Sespie int result;
1562b305b0f1Sespie
1563b305b0f1Sespie copy1 = xmalloc (strlen (first) + 1);
1564b305b0f1Sespie copy2 = xmalloc (strlen (second) + 1);
1565b305b0f1Sespie
1566b305b0f1Sespie /* Convert the names to lower case. */
1567b305b0f1Sespie stricpy (copy1, first);
1568b305b0f1Sespie stricpy (copy2, second);
1569b305b0f1Sespie
1570*007c2a45Smiod /* Remove size and endian strings from the name. */
1571b305b0f1Sespie strcut (copy1, "big");
1572b305b0f1Sespie strcut (copy1, "little");
1573b305b0f1Sespie strcut (copy2, "big");
1574b305b0f1Sespie strcut (copy2, "little");
1575b305b0f1Sespie
1576b305b0f1Sespie /* Return a value based on how many characters match,
1577b305b0f1Sespie starting from the beginning. If both strings are
1578b305b0f1Sespie the same then return 10 * their length. */
1579b305b0f1Sespie for (result = 0; copy1[result] == copy2[result]; result++)
1580b305b0f1Sespie if (copy1[result] == 0)
1581b305b0f1Sespie {
1582b305b0f1Sespie result *= 10;
1583b305b0f1Sespie break;
1584b305b0f1Sespie }
1585b305b0f1Sespie
1586b305b0f1Sespie free (copy1);
1587b305b0f1Sespie free (copy2);
1588b305b0f1Sespie
1589b305b0f1Sespie return result;
1590b305b0f1Sespie }
1591b305b0f1Sespie
1592b305b0f1Sespie /* Set by closest_target_match() below. */
1593b305b0f1Sespie static const bfd_target *winner;
1594b305b0f1Sespie
1595b305b0f1Sespie /* Scan all the valid bfd targets looking for one that has the endianness
1596b305b0f1Sespie requirement that was specified on the command line, and is the nearest
1597b305b0f1Sespie match to the original output target. */
1598b55d4692Sfgsch
1599b305b0f1Sespie static int
closest_target_match(const bfd_target * target,void * data)1600*007c2a45Smiod closest_target_match (const bfd_target *target, void *data)
1601b305b0f1Sespie {
1602*007c2a45Smiod const bfd_target *original = data;
1603b305b0f1Sespie
1604b55d4692Sfgsch if (command_line.endian == ENDIAN_BIG
1605b55d4692Sfgsch && target->byteorder != BFD_ENDIAN_BIG)
1606b305b0f1Sespie return 0;
1607b305b0f1Sespie
1608b55d4692Sfgsch if (command_line.endian == ENDIAN_LITTLE
1609b55d4692Sfgsch && target->byteorder != BFD_ENDIAN_LITTLE)
1610b305b0f1Sespie return 0;
1611b305b0f1Sespie
1612b305b0f1Sespie /* Must be the same flavour. */
1613b305b0f1Sespie if (target->flavour != original->flavour)
1614b305b0f1Sespie return 0;
1615b305b0f1Sespie
1616b305b0f1Sespie /* If we have not found a potential winner yet, then record this one. */
1617b305b0f1Sespie if (winner == NULL)
1618b305b0f1Sespie {
1619b305b0f1Sespie winner = target;
1620b305b0f1Sespie return 0;
1621b305b0f1Sespie }
1622b305b0f1Sespie
1623b305b0f1Sespie /* Oh dear, we now have two potential candidates for a successful match.
1624b305b0f1Sespie Compare their names and choose the better one. */
1625b55d4692Sfgsch if (name_compare (target->name, original->name)
1626b55d4692Sfgsch > name_compare (winner->name, original->name))
1627b305b0f1Sespie winner = target;
1628b305b0f1Sespie
1629b305b0f1Sespie /* Keep on searching until wqe have checked them all. */
1630b305b0f1Sespie return 0;
1631b305b0f1Sespie }
1632b305b0f1Sespie
1633b305b0f1Sespie /* Return the BFD target format of the first input file. */
1634b55d4692Sfgsch
1635b305b0f1Sespie static char *
get_first_input_target(void)1636*007c2a45Smiod get_first_input_target (void)
1637b305b0f1Sespie {
1638b305b0f1Sespie char *target = NULL;
1639b305b0f1Sespie
1640b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (s)
1641b305b0f1Sespie {
1642b305b0f1Sespie if (s->header.type == lang_input_statement_enum
1643b305b0f1Sespie && s->real)
1644b305b0f1Sespie {
1645b305b0f1Sespie ldfile_open_file (s);
1646b305b0f1Sespie
1647b305b0f1Sespie if (s->the_bfd != NULL
1648b305b0f1Sespie && bfd_check_format (s->the_bfd, bfd_object))
1649b305b0f1Sespie {
1650b305b0f1Sespie target = bfd_get_target (s->the_bfd);
1651b305b0f1Sespie
1652b305b0f1Sespie if (target != NULL)
1653b305b0f1Sespie break;
1654b305b0f1Sespie }
1655b305b0f1Sespie }
1656b305b0f1Sespie }
1657b305b0f1Sespie
1658b305b0f1Sespie return target;
1659b305b0f1Sespie }
1660b305b0f1Sespie
1661c074d1c9Sdrahn const char *
lang_get_output_target(void)1662*007c2a45Smiod lang_get_output_target (void)
1663c074d1c9Sdrahn {
1664c074d1c9Sdrahn const char *target;
1665c074d1c9Sdrahn
1666c074d1c9Sdrahn /* Has the user told us which output format to use? */
1667*007c2a45Smiod if (output_target != NULL)
1668c074d1c9Sdrahn return output_target;
1669c074d1c9Sdrahn
1670c074d1c9Sdrahn /* No - has the current target been set to something other than
1671c074d1c9Sdrahn the default? */
1672c074d1c9Sdrahn if (current_target != default_target)
1673c074d1c9Sdrahn return current_target;
1674c074d1c9Sdrahn
1675c074d1c9Sdrahn /* No - can we determine the format of the first input file? */
1676c074d1c9Sdrahn target = get_first_input_target ();
1677c074d1c9Sdrahn if (target != NULL)
1678c074d1c9Sdrahn return target;
1679c074d1c9Sdrahn
1680c074d1c9Sdrahn /* Failed - use the default output target. */
1681c074d1c9Sdrahn return default_target;
1682c074d1c9Sdrahn }
1683c074d1c9Sdrahn
1684191aa565Sniklas /* Open the output file. */
16852159047fSniklas
16862159047fSniklas static bfd *
open_output(const char * name)1687*007c2a45Smiod open_output (const char *name)
16882159047fSniklas {
16892159047fSniklas bfd *output;
16902159047fSniklas
1691c074d1c9Sdrahn output_target = lang_get_output_target ();
1692b305b0f1Sespie
1693b55d4692Sfgsch /* Has the user requested a particular endianness on the command
1694b55d4692Sfgsch line? */
1695b305b0f1Sespie if (command_line.endian != ENDIAN_UNSET)
1696b305b0f1Sespie {
1697b305b0f1Sespie const bfd_target *target;
1698b305b0f1Sespie enum bfd_endian desired_endian;
1699b305b0f1Sespie
1700b305b0f1Sespie /* Get the chosen target. */
1701*007c2a45Smiod target = bfd_search_for_target (get_target, (void *) output_target);
1702b305b0f1Sespie
1703b55d4692Sfgsch /* If the target is not supported, we cannot do anything. */
1704b55d4692Sfgsch if (target != NULL)
1705b55d4692Sfgsch {
1706b305b0f1Sespie if (command_line.endian == ENDIAN_BIG)
1707b305b0f1Sespie desired_endian = BFD_ENDIAN_BIG;
1708b305b0f1Sespie else
1709b305b0f1Sespie desired_endian = BFD_ENDIAN_LITTLE;
1710b305b0f1Sespie
1711b55d4692Sfgsch /* See if the target has the wrong endianness. This should
1712b55d4692Sfgsch not happen if the linker script has provided big and
1713b55d4692Sfgsch little endian alternatives, but some scrips don't do
1714b55d4692Sfgsch this. */
1715b305b0f1Sespie if (target->byteorder != desired_endian)
1716b305b0f1Sespie {
1717b305b0f1Sespie /* If it does, then see if the target provides
1718b305b0f1Sespie an alternative with the correct endianness. */
1719b305b0f1Sespie if (target->alternative_target != NULL
1720b305b0f1Sespie && (target->alternative_target->byteorder == desired_endian))
1721b305b0f1Sespie output_target = target->alternative_target->name;
1722b305b0f1Sespie else
1723b305b0f1Sespie {
1724b55d4692Sfgsch /* Try to find a target as similar as possible to
1725b55d4692Sfgsch the default target, but which has the desired
1726b55d4692Sfgsch endian characteristic. */
1727*007c2a45Smiod bfd_search_for_target (closest_target_match,
1728*007c2a45Smiod (void *) target);
1729b305b0f1Sespie
1730b55d4692Sfgsch /* Oh dear - we could not find any targets that
1731b55d4692Sfgsch satisfy our requirements. */
1732b305b0f1Sespie if (winner == NULL)
1733b305b0f1Sespie einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1734b305b0f1Sespie else
1735b305b0f1Sespie output_target = winner->name;
1736b305b0f1Sespie }
1737b305b0f1Sespie }
1738b305b0f1Sespie }
1739b55d4692Sfgsch }
1740b305b0f1Sespie
17412159047fSniklas output = bfd_openw (name, output_target);
17422159047fSniklas
1743*007c2a45Smiod if (output == NULL)
17442159047fSniklas {
17452159047fSniklas if (bfd_get_error () == bfd_error_invalid_target)
1746b305b0f1Sespie einfo (_("%P%F: target %s not found\n"), output_target);
1747b305b0f1Sespie
1748b305b0f1Sespie einfo (_("%P%F: cannot open output file %s: %E\n"), name);
17492159047fSniklas }
17502159047fSniklas
1751c074d1c9Sdrahn delete_output_file_on_failure = TRUE;
17522159047fSniklas
1753b55d4692Sfgsch #if 0
1754b55d4692Sfgsch output->flags |= D_PAGED;
1755b55d4692Sfgsch #endif
17562159047fSniklas
17572159047fSniklas if (! bfd_set_format (output, bfd_object))
1758b305b0f1Sespie einfo (_("%P%F:%s: can not make object file: %E\n"), name);
17592159047fSniklas if (! bfd_set_arch_mach (output,
17602159047fSniklas ldfile_output_architecture,
17612159047fSniklas ldfile_output_machine))
1762b305b0f1Sespie einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
17632159047fSniklas
17642159047fSniklas link_info.hash = bfd_link_hash_table_create (output);
1765*007c2a45Smiod if (link_info.hash == NULL)
1766b305b0f1Sespie einfo (_("%P%F: can not create link hash table: %E\n"));
17672159047fSniklas
17682159047fSniklas bfd_set_gp_size (output, g_switch_value);
17692159047fSniklas return output;
17702159047fSniklas }
17712159047fSniklas
17722159047fSniklas static void
ldlang_open_output(lang_statement_union_type * statement)1773*007c2a45Smiod ldlang_open_output (lang_statement_union_type *statement)
17742159047fSniklas {
17752159047fSniklas switch (statement->header.type)
17762159047fSniklas {
17772159047fSniklas case lang_output_statement_enum:
1778*007c2a45Smiod ASSERT (output_bfd == NULL);
17792159047fSniklas output_bfd = open_output (statement->output_statement.name);
17802159047fSniklas ldemul_set_output_arch ();
1781*007c2a45Smiod if (config.magic_demand_paged && !link_info.relocatable)
17822159047fSniklas output_bfd->flags |= D_PAGED;
17832159047fSniklas else
17842159047fSniklas output_bfd->flags &= ~D_PAGED;
17852159047fSniklas if (config.text_read_only)
17862159047fSniklas output_bfd->flags |= WP_TEXT;
17872159047fSniklas else
17882159047fSniklas output_bfd->flags &= ~WP_TEXT;
1789191aa565Sniklas if (link_info.traditional_format)
17902159047fSniklas output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
17912159047fSniklas else
17922159047fSniklas output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
17932159047fSniklas break;
17942159047fSniklas
17952159047fSniklas case lang_target_statement_enum:
17962159047fSniklas current_target = statement->target_statement.target;
17972159047fSniklas break;
17982159047fSniklas default:
17992159047fSniklas break;
18002159047fSniklas }
18012159047fSniklas }
18022159047fSniklas
1803*007c2a45Smiod /* Convert between addresses in bytes and sizes in octets.
1804*007c2a45Smiod For currently supported targets, octets_per_byte is always a power
1805*007c2a45Smiod of two, so we can use shifts. */
1806*007c2a45Smiod #define TO_ADDR(X) ((X) >> opb_shift)
1807*007c2a45Smiod #define TO_SIZE(X) ((X) << opb_shift)
1808*007c2a45Smiod
1809*007c2a45Smiod /* Support the above. */
1810*007c2a45Smiod static unsigned int opb_shift = 0;
1811*007c2a45Smiod
1812*007c2a45Smiod static void
init_opb(void)1813*007c2a45Smiod init_opb (void)
1814*007c2a45Smiod {
1815*007c2a45Smiod unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1816*007c2a45Smiod ldfile_output_machine);
1817*007c2a45Smiod opb_shift = 0;
1818*007c2a45Smiod if (x > 1)
1819*007c2a45Smiod while ((x & 1) == 0)
1820*007c2a45Smiod {
1821*007c2a45Smiod x >>= 1;
1822*007c2a45Smiod ++opb_shift;
1823*007c2a45Smiod }
1824*007c2a45Smiod ASSERT (x == 1);
1825*007c2a45Smiod }
1826*007c2a45Smiod
18272159047fSniklas /* Open all the input files. */
18282159047fSniklas
18292159047fSniklas static void
open_input_bfds(lang_statement_union_type * s,bfd_boolean force)1830*007c2a45Smiod open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
18312159047fSniklas {
1832*007c2a45Smiod for (; s != NULL; s = s->header.next)
18332159047fSniklas {
18342159047fSniklas switch (s->header.type)
18352159047fSniklas {
18362159047fSniklas case lang_constructors_statement_enum:
18372159047fSniklas open_input_bfds (constructor_list.head, force);
18382159047fSniklas break;
18392159047fSniklas case lang_output_section_statement_enum:
18402159047fSniklas open_input_bfds (s->output_section_statement.children.head, force);
18412159047fSniklas break;
18422159047fSniklas case lang_wild_statement_enum:
1843b55d4692Sfgsch /* Maybe we should load the file's symbols. */
18440c6d0228Sniklas if (s->wild_statement.filename
18450c6d0228Sniklas && ! wildcardp (s->wild_statement.filename))
1846*007c2a45Smiod lookup_name (s->wild_statement.filename);
18472159047fSniklas open_input_bfds (s->wild_statement.children.head, force);
18482159047fSniklas break;
18492159047fSniklas case lang_group_statement_enum:
18502159047fSniklas {
18512159047fSniklas struct bfd_link_hash_entry *undefs;
18522159047fSniklas
18532159047fSniklas /* We must continually search the entries in the group
18542159047fSniklas until no new symbols are added to the list of undefined
18552159047fSniklas symbols. */
18562159047fSniklas
18572159047fSniklas do
18582159047fSniklas {
18592159047fSniklas undefs = link_info.hash->undefs_tail;
1860c074d1c9Sdrahn open_input_bfds (s->group_statement.children.head, TRUE);
18612159047fSniklas }
18622159047fSniklas while (undefs != link_info.hash->undefs_tail);
18632159047fSniklas }
18642159047fSniklas break;
18652159047fSniklas case lang_target_statement_enum:
18662159047fSniklas current_target = s->target_statement.target;
18672159047fSniklas break;
18682159047fSniklas case lang_input_statement_enum:
1869b305b0f1Sespie if (s->input_statement.real)
18702159047fSniklas {
18712159047fSniklas lang_statement_list_type add;
18722159047fSniklas
18732159047fSniklas s->input_statement.target = current_target;
18742159047fSniklas
18752159047fSniklas /* If we are being called from within a group, and this
18762159047fSniklas is an archive which has already been searched, then
1877b55d4692Sfgsch force it to be researched unless the whole archive
1878b55d4692Sfgsch has been loaded already. */
18792159047fSniklas if (force
1880b55d4692Sfgsch && !s->input_statement.whole_archive
18812159047fSniklas && s->input_statement.loaded
18822159047fSniklas && bfd_check_format (s->input_statement.the_bfd,
18832159047fSniklas bfd_archive))
1884c074d1c9Sdrahn s->input_statement.loaded = FALSE;
18852159047fSniklas
18862159047fSniklas lang_list_init (&add);
18872159047fSniklas
1888c074d1c9Sdrahn if (! load_symbols (&s->input_statement, &add))
1889c074d1c9Sdrahn config.make_executable = FALSE;
18902159047fSniklas
18912159047fSniklas if (add.head != NULL)
18922159047fSniklas {
1893c074d1c9Sdrahn *add.tail = s->header.next;
1894c074d1c9Sdrahn s->header.next = add.head;
18952159047fSniklas }
18962159047fSniklas }
18972159047fSniklas break;
18982159047fSniklas default:
18992159047fSniklas break;
19002159047fSniklas }
19012159047fSniklas }
19022159047fSniklas }
19032159047fSniklas
1904b55d4692Sfgsch /* If there are [COMMONS] statements, put a wild one into the bss
1905b55d4692Sfgsch section. */
19062159047fSniklas
19072159047fSniklas static void
lang_reasonable_defaults(void)1908*007c2a45Smiod lang_reasonable_defaults (void)
19092159047fSniklas {
19102159047fSniklas #if 0
19112159047fSniklas lang_output_section_statement_lookup (".text");
19122159047fSniklas lang_output_section_statement_lookup (".data");
19132159047fSniklas
1914b55d4692Sfgsch default_common_section = lang_output_section_statement_lookup (".bss");
19152159047fSniklas
1916c074d1c9Sdrahn if (!placed_commons)
19172159047fSniklas {
19182159047fSniklas lang_wild_statement_type *new =
19192159047fSniklas new_stat (lang_wild_statement,
19202159047fSniklas &default_common_section->children);
19212159047fSniklas
19222159047fSniklas new->section_name = "COMMON";
1923*007c2a45Smiod new->filename = NULL;
19242159047fSniklas lang_list_init (&new->children);
19252159047fSniklas }
19262159047fSniklas #endif
19272159047fSniklas }
19282159047fSniklas
1929*007c2a45Smiod /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1930*007c2a45Smiod
1931*007c2a45Smiod void
lang_track_definedness(const char * name)1932*007c2a45Smiod lang_track_definedness (const char *name)
1933*007c2a45Smiod {
1934*007c2a45Smiod if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1935*007c2a45Smiod einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1936*007c2a45Smiod }
1937*007c2a45Smiod
1938*007c2a45Smiod /* New-function for the definedness hash table. */
1939*007c2a45Smiod
1940*007c2a45Smiod static struct bfd_hash_entry *
lang_definedness_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED)1941*007c2a45Smiod lang_definedness_newfunc (struct bfd_hash_entry *entry,
1942*007c2a45Smiod struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1943*007c2a45Smiod const char *name ATTRIBUTE_UNUSED)
1944*007c2a45Smiod {
1945*007c2a45Smiod struct lang_definedness_hash_entry *ret
1946*007c2a45Smiod = (struct lang_definedness_hash_entry *) entry;
1947*007c2a45Smiod
1948*007c2a45Smiod if (ret == NULL)
1949*007c2a45Smiod ret = (struct lang_definedness_hash_entry *)
1950*007c2a45Smiod bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1951*007c2a45Smiod
1952*007c2a45Smiod if (ret == NULL)
1953*007c2a45Smiod einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1954*007c2a45Smiod
1955*007c2a45Smiod ret->iteration = -1;
1956*007c2a45Smiod return &ret->root;
1957*007c2a45Smiod }
1958*007c2a45Smiod
1959*007c2a45Smiod /* Return the iteration when the definition of NAME was last updated. A
1960*007c2a45Smiod value of -1 means that the symbol is not defined in the linker script
1961*007c2a45Smiod or the command line, but may be defined in the linker symbol table. */
1962*007c2a45Smiod
1963*007c2a45Smiod int
lang_symbol_definition_iteration(const char * name)1964*007c2a45Smiod lang_symbol_definition_iteration (const char *name)
1965*007c2a45Smiod {
1966*007c2a45Smiod struct lang_definedness_hash_entry *defentry
1967*007c2a45Smiod = (struct lang_definedness_hash_entry *)
1968*007c2a45Smiod bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1969*007c2a45Smiod
1970*007c2a45Smiod /* We've already created this one on the presence of DEFINED in the
1971*007c2a45Smiod script, so it can't be NULL unless something is borked elsewhere in
1972*007c2a45Smiod the code. */
1973*007c2a45Smiod if (defentry == NULL)
1974*007c2a45Smiod FAIL ();
1975*007c2a45Smiod
1976*007c2a45Smiod return defentry->iteration;
1977*007c2a45Smiod }
1978*007c2a45Smiod
1979*007c2a45Smiod /* Update the definedness state of NAME. */
1980*007c2a45Smiod
1981*007c2a45Smiod void
lang_update_definedness(const char * name,struct bfd_link_hash_entry * h)1982*007c2a45Smiod lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1983*007c2a45Smiod {
1984*007c2a45Smiod struct lang_definedness_hash_entry *defentry
1985*007c2a45Smiod = (struct lang_definedness_hash_entry *)
1986*007c2a45Smiod bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1987*007c2a45Smiod
1988*007c2a45Smiod /* We don't keep track of symbols not tested with DEFINED. */
1989*007c2a45Smiod if (defentry == NULL)
1990*007c2a45Smiod return;
1991*007c2a45Smiod
1992*007c2a45Smiod /* If the symbol was already defined, and not from an earlier statement
1993*007c2a45Smiod iteration, don't update the definedness iteration, because that'd
1994*007c2a45Smiod make the symbol seem defined in the linker script at this point, and
1995*007c2a45Smiod it wasn't; it was defined in some object. If we do anyway, DEFINED
1996*007c2a45Smiod would start to yield false before this point and the construct "sym =
1997*007c2a45Smiod DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1998*007c2a45Smiod in an object. */
1999*007c2a45Smiod if (h->type != bfd_link_hash_undefined
2000*007c2a45Smiod && h->type != bfd_link_hash_common
2001*007c2a45Smiod && h->type != bfd_link_hash_new
2002*007c2a45Smiod && defentry->iteration == -1)
2003*007c2a45Smiod return;
2004*007c2a45Smiod
2005*007c2a45Smiod defentry->iteration = lang_statement_iteration;
2006*007c2a45Smiod }
2007*007c2a45Smiod
2008b55d4692Sfgsch /* Add the supplied name to the symbol table as an undefined reference.
2009c074d1c9Sdrahn This is a two step process as the symbol table doesn't even exist at
2010c074d1c9Sdrahn the time the ld command line is processed. First we put the name
2011c074d1c9Sdrahn on a list, then, once the output file has been opened, transfer the
2012c074d1c9Sdrahn name to the symbol table. */
20132159047fSniklas
2014c074d1c9Sdrahn typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2015c074d1c9Sdrahn
2016c074d1c9Sdrahn #define ldlang_undef_chain_list_head entry_symbol.next
20172159047fSniklas
20182159047fSniklas void
ldlang_add_undef(const char * const name)2019*007c2a45Smiod ldlang_add_undef (const char *const name)
20202159047fSniklas {
20212159047fSniklas ldlang_undef_chain_list_type *new =
2022*007c2a45Smiod stat_alloc (sizeof (ldlang_undef_chain_list_type));
20232159047fSniklas
20242159047fSniklas new->next = ldlang_undef_chain_list_head;
20252159047fSniklas ldlang_undef_chain_list_head = new;
20262159047fSniklas
2027b55d4692Sfgsch new->name = xstrdup (name);
2028c074d1c9Sdrahn
2029c074d1c9Sdrahn if (output_bfd != NULL)
2030c074d1c9Sdrahn insert_undefined (new->name);
2031c074d1c9Sdrahn }
2032c074d1c9Sdrahn
2033c074d1c9Sdrahn /* Insert NAME as undefined in the symbol table. */
2034c074d1c9Sdrahn
2035c074d1c9Sdrahn static void
insert_undefined(const char * name)2036*007c2a45Smiod insert_undefined (const char *name)
2037c074d1c9Sdrahn {
2038c074d1c9Sdrahn struct bfd_link_hash_entry *h;
2039c074d1c9Sdrahn
2040c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2041*007c2a45Smiod if (h == NULL)
2042c074d1c9Sdrahn einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2043c074d1c9Sdrahn if (h->type == bfd_link_hash_new)
2044c074d1c9Sdrahn {
2045c074d1c9Sdrahn h->type = bfd_link_hash_undefined;
2046c074d1c9Sdrahn h->u.undef.abfd = NULL;
2047c074d1c9Sdrahn bfd_link_add_undef (link_info.hash, h);
2048c074d1c9Sdrahn }
20492159047fSniklas }
20502159047fSniklas
20512159047fSniklas /* Run through the list of undefineds created above and place them
20522159047fSniklas into the linker hash table as undefined symbols belonging to the
2053b55d4692Sfgsch script file. */
2054b55d4692Sfgsch
20552159047fSniklas static void
lang_place_undefineds(void)2056*007c2a45Smiod lang_place_undefineds (void)
20572159047fSniklas {
20582159047fSniklas ldlang_undef_chain_list_type *ptr;
20592159047fSniklas
2060*007c2a45Smiod for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2061c074d1c9Sdrahn insert_undefined (ptr->name);
20622159047fSniklas }
20632159047fSniklas
2064c074d1c9Sdrahn /* Open input files and attach to output sections. */
2065b55d4692Sfgsch
20662159047fSniklas static void
map_input_to_output_sections(lang_statement_union_type * s,const char * target,lang_output_section_statement_type * output_section_statement)2067*007c2a45Smiod map_input_to_output_sections
2068*007c2a45Smiod (lang_statement_union_type *s, const char *target,
2069*007c2a45Smiod lang_output_section_statement_type *output_section_statement)
20702159047fSniklas {
2071*007c2a45Smiod for (; s != NULL; s = s->header.next)
20722159047fSniklas {
20732159047fSniklas switch (s->header.type)
20742159047fSniklas {
20752159047fSniklas case lang_wild_statement_enum:
2076c074d1c9Sdrahn wild (&s->wild_statement, target, output_section_statement);
20772159047fSniklas break;
20782159047fSniklas case lang_constructors_statement_enum:
20792159047fSniklas map_input_to_output_sections (constructor_list.head,
20802159047fSniklas target,
20812159047fSniklas output_section_statement);
20822159047fSniklas break;
20832159047fSniklas case lang_output_section_statement_enum:
20842159047fSniklas map_input_to_output_sections (s->output_section_statement.children.head,
20852159047fSniklas target,
20862159047fSniklas &s->output_section_statement);
20872159047fSniklas break;
20882159047fSniklas case lang_output_statement_enum:
20892159047fSniklas break;
20902159047fSniklas case lang_target_statement_enum:
20912159047fSniklas target = s->target_statement.target;
20922159047fSniklas break;
20932159047fSniklas case lang_group_statement_enum:
20942159047fSniklas map_input_to_output_sections (s->group_statement.children.head,
20952159047fSniklas target,
20962159047fSniklas output_section_statement);
20972159047fSniklas break;
2098*007c2a45Smiod case lang_data_statement_enum:
2099*007c2a45Smiod /* Make sure that any sections mentioned in the expression
2100*007c2a45Smiod are initialized. */
2101*007c2a45Smiod exp_init_os (s->data_statement.exp);
2102*007c2a45Smiod /* FALLTHROUGH */
21032159047fSniklas case lang_fill_statement_enum:
21042159047fSniklas case lang_input_section_enum:
21052159047fSniklas case lang_object_symbols_statement_enum:
21062159047fSniklas case lang_reloc_statement_enum:
21072159047fSniklas case lang_padding_statement_enum:
21082159047fSniklas case lang_input_statement_enum:
21092159047fSniklas if (output_section_statement != NULL
21102159047fSniklas && output_section_statement->bfd_section == NULL)
21112159047fSniklas init_os (output_section_statement);
21122159047fSniklas break;
21130c6d0228Sniklas case lang_assignment_statement_enum:
21140c6d0228Sniklas if (output_section_statement != NULL
21150c6d0228Sniklas && output_section_statement->bfd_section == NULL)
21160c6d0228Sniklas init_os (output_section_statement);
21170c6d0228Sniklas
21180c6d0228Sniklas /* Make sure that any sections mentioned in the assignment
21190c6d0228Sniklas are initialized. */
21200c6d0228Sniklas exp_init_os (s->assignment_statement.exp);
21210c6d0228Sniklas break;
21222159047fSniklas case lang_afile_asection_pair_statement_enum:
21232159047fSniklas FAIL ();
21242159047fSniklas break;
21252159047fSniklas case lang_address_statement_enum:
2126b55d4692Sfgsch /* Mark the specified section with the supplied address. */
21272159047fSniklas {
21282159047fSniklas lang_output_section_statement_type *os =
21292159047fSniklas lang_output_section_statement_lookup
21302159047fSniklas (s->address_statement.section_name);
21312159047fSniklas
21322159047fSniklas if (os->bfd_section == NULL)
21332159047fSniklas init_os (os);
21342159047fSniklas os->addr_tree = s->address_statement.address;
21352159047fSniklas }
21362159047fSniklas break;
21372159047fSniklas }
21382159047fSniklas }
21392159047fSniklas }
21402159047fSniklas
2141c074d1c9Sdrahn /* An output section might have been removed after its statement was
2142c074d1c9Sdrahn added. For example, ldemul_before_allocation can remove dynamic
2143c074d1c9Sdrahn sections if they turn out to be not needed. Clean them up here. */
2144c074d1c9Sdrahn
2145c074d1c9Sdrahn static void
strip_excluded_output_sections(void)2146*007c2a45Smiod strip_excluded_output_sections (void)
2147c074d1c9Sdrahn {
2148c074d1c9Sdrahn lang_statement_union_type *u;
2149c074d1c9Sdrahn
2150c074d1c9Sdrahn for (u = lang_output_section_statement.head;
2151c074d1c9Sdrahn u != NULL;
2152c074d1c9Sdrahn u = u->output_section_statement.next)
2153c074d1c9Sdrahn {
2154c074d1c9Sdrahn lang_output_section_statement_type *os;
2155c074d1c9Sdrahn asection *s;
2156c074d1c9Sdrahn
2157c074d1c9Sdrahn os = &u->output_section_statement;
2158c074d1c9Sdrahn s = os->bfd_section;
2159c074d1c9Sdrahn if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2160c074d1c9Sdrahn {
2161c074d1c9Sdrahn asection **p;
2162c074d1c9Sdrahn
2163c074d1c9Sdrahn os->bfd_section = NULL;
2164c074d1c9Sdrahn
2165c074d1c9Sdrahn for (p = &output_bfd->sections; *p; p = &(*p)->next)
2166c074d1c9Sdrahn if (*p == s)
2167c074d1c9Sdrahn {
2168c074d1c9Sdrahn bfd_section_list_remove (output_bfd, p);
2169c074d1c9Sdrahn output_bfd->section_count--;
2170c074d1c9Sdrahn break;
2171c074d1c9Sdrahn }
2172c074d1c9Sdrahn }
2173c074d1c9Sdrahn }
2174c074d1c9Sdrahn }
2175c074d1c9Sdrahn
21762159047fSniklas static void
print_output_section_statement(lang_output_section_statement_type * output_section_statement)2177*007c2a45Smiod print_output_section_statement
2178*007c2a45Smiod (lang_output_section_statement_type *output_section_statement)
21792159047fSniklas {
21802159047fSniklas asection *section = output_section_statement->bfd_section;
2181191aa565Sniklas int len;
21822159047fSniklas
2183191aa565Sniklas if (output_section_statement != abs_output_section)
2184191aa565Sniklas {
2185191aa565Sniklas minfo ("\n%s", output_section_statement->name);
21862159047fSniklas
2187191aa565Sniklas if (section != NULL)
21882159047fSniklas {
21892159047fSniklas print_dot = section->vma;
21902159047fSniklas
2191191aa565Sniklas len = strlen (output_section_statement->name);
2192191aa565Sniklas if (len >= SECTION_NAME_MAP_LENGTH - 1)
21932159047fSniklas {
21942159047fSniklas print_nl ();
2195191aa565Sniklas len = 0;
21962159047fSniklas }
2197191aa565Sniklas while (len < SECTION_NAME_MAP_LENGTH)
21982159047fSniklas {
2199191aa565Sniklas print_space ();
2200191aa565Sniklas ++len;
22012159047fSniklas }
2202191aa565Sniklas
2203191aa565Sniklas minfo ("0x%V %W", section->vma, section->_raw_size);
2204191aa565Sniklas
2205191aa565Sniklas if (output_section_statement->load_base != NULL)
22062159047fSniklas {
2207191aa565Sniklas bfd_vma addr;
2208191aa565Sniklas
2209191aa565Sniklas addr = exp_get_abs_int (output_section_statement->load_base, 0,
2210191aa565Sniklas "load base", lang_final_phase_enum);
2211b305b0f1Sespie minfo (_(" load address 0x%V"), addr);
2212191aa565Sniklas }
22132159047fSniklas }
22142159047fSniklas
22152159047fSniklas print_nl ();
22162159047fSniklas }
2217191aa565Sniklas
2218191aa565Sniklas print_statement_list (output_section_statement->children.head,
22192159047fSniklas output_section_statement);
22202159047fSniklas }
22212159047fSniklas
22222159047fSniklas static void
print_assignment(lang_assignment_statement_type * assignment,lang_output_section_statement_type * output_section)2223*007c2a45Smiod print_assignment (lang_assignment_statement_type *assignment,
2224*007c2a45Smiod lang_output_section_statement_type *output_section)
22252159047fSniklas {
2226191aa565Sniklas int i;
22272159047fSniklas etree_value_type result;
22282159047fSniklas
2229191aa565Sniklas for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
22302159047fSniklas print_space ();
22312159047fSniklas
2232191aa565Sniklas result = exp_fold_tree (assignment->exp->assign.src, output_section,
2233191aa565Sniklas lang_final_phase_enum, print_dot, &print_dot);
2234b305b0f1Sespie if (result.valid_p)
2235c074d1c9Sdrahn {
2236c074d1c9Sdrahn const char *dst;
2237c074d1c9Sdrahn bfd_vma value;
2238c074d1c9Sdrahn
2239c074d1c9Sdrahn value = result.value + result.section->bfd_section->vma;
2240c074d1c9Sdrahn dst = assignment->exp->assign.dst;
2241c074d1c9Sdrahn
2242c074d1c9Sdrahn minfo ("0x%V", value);
2243c074d1c9Sdrahn if (dst[0] == '.' && dst[1] == 0)
2244c074d1c9Sdrahn print_dot = value;
2245c074d1c9Sdrahn }
22462159047fSniklas else
22472159047fSniklas {
2248191aa565Sniklas minfo ("*undef* ");
2249191aa565Sniklas #ifdef BFD64
2250191aa565Sniklas minfo (" ");
2251191aa565Sniklas #endif
22522159047fSniklas }
2253191aa565Sniklas
2254191aa565Sniklas minfo (" ");
2255191aa565Sniklas
22562159047fSniklas exp_print_tree (assignment->exp);
22572159047fSniklas
2258191aa565Sniklas print_nl ();
22592159047fSniklas }
22602159047fSniklas
22612159047fSniklas static void
print_input_statement(lang_input_statement_type * statm)2262*007c2a45Smiod print_input_statement (lang_input_statement_type *statm)
22632159047fSniklas {
2264*007c2a45Smiod if (statm->filename != NULL)
22652159047fSniklas {
22662159047fSniklas fprintf (config.map_file, "LOAD %s\n", statm->filename);
22672159047fSniklas }
22682159047fSniklas }
22692159047fSniklas
2270191aa565Sniklas /* Print all symbols defined in a particular section. This is called
2271191aa565Sniklas via bfd_link_hash_traverse. */
22722159047fSniklas
2273c074d1c9Sdrahn static bfd_boolean
print_one_symbol(struct bfd_link_hash_entry * hash_entry,void * ptr)2274*007c2a45Smiod print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
22752159047fSniklas {
2276*007c2a45Smiod asection *sec = ptr;
22772159047fSniklas
2278191aa565Sniklas if ((hash_entry->type == bfd_link_hash_defined
22792159047fSniklas || hash_entry->type == bfd_link_hash_defweak)
2280191aa565Sniklas && sec == hash_entry->u.def.section)
22812159047fSniklas {
2282191aa565Sniklas int i;
2283191aa565Sniklas
2284191aa565Sniklas for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2285191aa565Sniklas print_space ();
2286191aa565Sniklas minfo ("0x%V ",
2287191aa565Sniklas (hash_entry->u.def.value
2288191aa565Sniklas + hash_entry->u.def.section->output_offset
2289191aa565Sniklas + hash_entry->u.def.section->output_section->vma));
2290191aa565Sniklas
2291191aa565Sniklas minfo (" %T\n", hash_entry->root.string);
22922159047fSniklas }
22932159047fSniklas
2294c074d1c9Sdrahn return TRUE;
22952159047fSniklas }
22962159047fSniklas
2297191aa565Sniklas /* Print information about an input section to the map file. */
2298191aa565Sniklas
22992159047fSniklas static void
print_input_section(lang_input_section_type * in)2300*007c2a45Smiod print_input_section (lang_input_section_type *in)
23012159047fSniklas {
23022159047fSniklas asection *i = in->section;
23032159047fSniklas bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2304*007c2a45Smiod
2305*007c2a45Smiod init_opb ();
23062159047fSniklas if (size != 0)
23072159047fSniklas {
2308191aa565Sniklas print_space ();
23092159047fSniklas
2310191aa565Sniklas minfo ("%s", i->name);
23112159047fSniklas
2312191aa565Sniklas if (i->output_section != NULL)
23132159047fSniklas {
2314191aa565Sniklas int len;
23152159047fSniklas
2316191aa565Sniklas len = 1 + strlen (i->name);
2317191aa565Sniklas if (len >= SECTION_NAME_MAP_LENGTH - 1)
23182159047fSniklas {
23192159047fSniklas print_nl ();
2320191aa565Sniklas len = 0;
2321191aa565Sniklas }
2322191aa565Sniklas while (len < SECTION_NAME_MAP_LENGTH)
2323191aa565Sniklas {
2324191aa565Sniklas print_space ();
2325191aa565Sniklas ++len;
2326191aa565Sniklas }
23272159047fSniklas
2328191aa565Sniklas minfo ("0x%V %W %B\n",
2329*007c2a45Smiod i->output_section->vma + i->output_offset, TO_ADDR (size),
2330191aa565Sniklas i->owner);
2331191aa565Sniklas
2332191aa565Sniklas if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2333191aa565Sniklas {
2334191aa565Sniklas len = SECTION_NAME_MAP_LENGTH + 3;
2335191aa565Sniklas #ifdef BFD64
2336191aa565Sniklas len += 16;
2337191aa565Sniklas #else
2338191aa565Sniklas len += 8;
2339191aa565Sniklas #endif
2340191aa565Sniklas while (len > 0)
2341191aa565Sniklas {
2342191aa565Sniklas print_space ();
2343191aa565Sniklas --len;
2344191aa565Sniklas }
2345191aa565Sniklas
2346b305b0f1Sespie minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2347191aa565Sniklas }
2348191aa565Sniklas
2349*007c2a45Smiod bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
23502159047fSniklas
2351*007c2a45Smiod print_dot = (i->output_section->vma + i->output_offset
2352*007c2a45Smiod + TO_ADDR (size));
23532159047fSniklas }
23542159047fSniklas }
23552159047fSniklas }
23562159047fSniklas
23572159047fSniklas static void
print_fill_statement(lang_fill_statement_type * fill)2358*007c2a45Smiod print_fill_statement (lang_fill_statement_type *fill)
23592159047fSniklas {
2360c074d1c9Sdrahn size_t size;
2361c074d1c9Sdrahn unsigned char *p;
2362c074d1c9Sdrahn fputs (" FILL mask 0x", config.map_file);
2363c074d1c9Sdrahn for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2364c074d1c9Sdrahn fprintf (config.map_file, "%02x", *p);
2365c074d1c9Sdrahn fputs ("\n", config.map_file);
23662159047fSniklas }
23672159047fSniklas
23682159047fSniklas static void
print_data_statement(lang_data_statement_type * data)2369*007c2a45Smiod print_data_statement (lang_data_statement_type *data)
23702159047fSniklas {
2371191aa565Sniklas int i;
2372191aa565Sniklas bfd_vma addr;
2373191aa565Sniklas bfd_size_type size;
2374191aa565Sniklas const char *name;
23752159047fSniklas
2376*007c2a45Smiod init_opb ();
2377191aa565Sniklas for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
23782159047fSniklas print_space ();
2379191aa565Sniklas
2380191aa565Sniklas addr = data->output_vma;
2381191aa565Sniklas if (data->output_section != NULL)
2382191aa565Sniklas addr += data->output_section->vma;
2383191aa565Sniklas
23842159047fSniklas switch (data->type)
23852159047fSniklas {
2386191aa565Sniklas default:
2387191aa565Sniklas abort ();
23882159047fSniklas case BYTE:
2389191aa565Sniklas size = BYTE_SIZE;
2390191aa565Sniklas name = "BYTE";
23912159047fSniklas break;
23922159047fSniklas case SHORT:
2393191aa565Sniklas size = SHORT_SIZE;
2394191aa565Sniklas name = "SHORT";
23952159047fSniklas break;
23962159047fSniklas case LONG:
2397191aa565Sniklas size = LONG_SIZE;
2398191aa565Sniklas name = "LONG";
23992159047fSniklas break;
24002159047fSniklas case QUAD:
2401191aa565Sniklas size = QUAD_SIZE;
2402191aa565Sniklas name = "QUAD";
24032159047fSniklas break;
2404b305b0f1Sespie case SQUAD:
2405b305b0f1Sespie size = QUAD_SIZE;
2406b305b0f1Sespie name = "SQUAD";
2407b305b0f1Sespie break;
24082159047fSniklas }
24092159047fSniklas
2410191aa565Sniklas minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
24112159047fSniklas
2412191aa565Sniklas if (data->exp->type.node_class != etree_value)
2413191aa565Sniklas {
2414191aa565Sniklas print_space ();
2415191aa565Sniklas exp_print_tree (data->exp);
2416191aa565Sniklas }
2417191aa565Sniklas
2418191aa565Sniklas print_nl ();
2419191aa565Sniklas
2420*007c2a45Smiod print_dot = addr + TO_ADDR (size);
2421191aa565Sniklas }
2422191aa565Sniklas
2423191aa565Sniklas /* Print an address statement. These are generated by options like
2424191aa565Sniklas -Ttext. */
2425191aa565Sniklas
2426191aa565Sniklas static void
print_address_statement(lang_address_statement_type * address)2427*007c2a45Smiod print_address_statement (lang_address_statement_type *address)
2428191aa565Sniklas {
2429b305b0f1Sespie minfo (_("Address of section %s set to "), address->section_name);
2430191aa565Sniklas exp_print_tree (address->address);
2431191aa565Sniklas print_nl ();
24322159047fSniklas }
24332159047fSniklas
24342159047fSniklas /* Print a reloc statement. */
24352159047fSniklas
24362159047fSniklas static void
print_reloc_statement(lang_reloc_statement_type * reloc)2437*007c2a45Smiod print_reloc_statement (lang_reloc_statement_type *reloc)
24382159047fSniklas {
2439191aa565Sniklas int i;
2440191aa565Sniklas bfd_vma addr;
2441191aa565Sniklas bfd_size_type size;
2442191aa565Sniklas
2443*007c2a45Smiod init_opb ();
2444191aa565Sniklas for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
24452159047fSniklas print_space ();
24462159047fSniklas
2447191aa565Sniklas addr = reloc->output_vma;
2448191aa565Sniklas if (reloc->output_section != NULL)
2449191aa565Sniklas addr += reloc->output_section->vma;
24502159047fSniklas
2451191aa565Sniklas size = bfd_get_reloc_size (reloc->howto);
24522159047fSniklas
2453191aa565Sniklas minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
24542159047fSniklas
2455191aa565Sniklas if (reloc->name != NULL)
2456191aa565Sniklas minfo ("%s+", reloc->name);
2457191aa565Sniklas else
2458191aa565Sniklas minfo ("%s+", reloc->section->name);
24592159047fSniklas
24602159047fSniklas exp_print_tree (reloc->addend_exp);
24612159047fSniklas
2462191aa565Sniklas print_nl ();
2463191aa565Sniklas
2464*007c2a45Smiod print_dot = addr + TO_ADDR (size);
24652159047fSniklas }
24662159047fSniklas
24672159047fSniklas static void
print_padding_statement(lang_padding_statement_type * s)2468*007c2a45Smiod print_padding_statement (lang_padding_statement_type *s)
24692159047fSniklas {
2470191aa565Sniklas int len;
2471191aa565Sniklas bfd_vma addr;
2472191aa565Sniklas
2473*007c2a45Smiod init_opb ();
2474191aa565Sniklas minfo (" *fill*");
2475191aa565Sniklas
2476191aa565Sniklas len = sizeof " *fill*" - 1;
2477191aa565Sniklas while (len < SECTION_NAME_MAP_LENGTH)
2478191aa565Sniklas {
24792159047fSniklas print_space ();
2480191aa565Sniklas ++len;
2481191aa565Sniklas }
2482191aa565Sniklas
2483191aa565Sniklas addr = s->output_offset;
2484191aa565Sniklas if (s->output_section != NULL)
2485191aa565Sniklas addr += s->output_section->vma;
2486191aa565Sniklas minfo ("0x%V %W ", addr, s->size);
2487191aa565Sniklas
2488c074d1c9Sdrahn if (s->fill->size != 0)
2489c074d1c9Sdrahn {
2490c074d1c9Sdrahn size_t size;
2491c074d1c9Sdrahn unsigned char *p;
2492c074d1c9Sdrahn for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2493c074d1c9Sdrahn fprintf (config.map_file, "%02x", *p);
2494c074d1c9Sdrahn }
2495191aa565Sniklas
24962159047fSniklas print_nl ();
24972159047fSniklas
2498*007c2a45Smiod print_dot = addr + TO_ADDR (s->size);
24992159047fSniklas }
25002159047fSniklas
25012159047fSniklas static void
print_wild_statement(lang_wild_statement_type * w,lang_output_section_statement_type * os)2502*007c2a45Smiod print_wild_statement (lang_wild_statement_type *w,
2503*007c2a45Smiod lang_output_section_statement_type *os)
25042159047fSniklas {
2505c074d1c9Sdrahn struct wildcard_list *sec;
2506c074d1c9Sdrahn
2507191aa565Sniklas print_space ();
25082159047fSniklas
2509b305b0f1Sespie if (w->filenames_sorted)
2510b305b0f1Sespie minfo ("SORT(");
2511191aa565Sniklas if (w->filename != NULL)
2512191aa565Sniklas minfo ("%s", w->filename);
2513191aa565Sniklas else
2514191aa565Sniklas minfo ("*");
2515b305b0f1Sespie if (w->filenames_sorted)
2516b305b0f1Sespie minfo (")");
2517191aa565Sniklas
2518b305b0f1Sespie minfo ("(");
2519c074d1c9Sdrahn for (sec = w->section_list; sec; sec = sec->next)
2520c074d1c9Sdrahn {
2521c074d1c9Sdrahn if (sec->spec.sorted)
2522b305b0f1Sespie minfo ("SORT(");
2523c074d1c9Sdrahn if (sec->spec.exclude_name_list != NULL)
2524c074d1c9Sdrahn {
2525c074d1c9Sdrahn name_list *tmp;
2526c074d1c9Sdrahn minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2527c074d1c9Sdrahn for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2528c074d1c9Sdrahn minfo (" %s", tmp->name);
2529c074d1c9Sdrahn minfo (") ");
2530c074d1c9Sdrahn }
2531c074d1c9Sdrahn if (sec->spec.name != NULL)
2532c074d1c9Sdrahn minfo ("%s", sec->spec.name);
2533191aa565Sniklas else
2534b305b0f1Sespie minfo ("*");
2535c074d1c9Sdrahn if (sec->spec.sorted)
2536b305b0f1Sespie minfo (")");
2537c074d1c9Sdrahn if (sec->next)
2538c074d1c9Sdrahn minfo (" ");
2539c074d1c9Sdrahn }
2540b305b0f1Sespie minfo (")");
2541191aa565Sniklas
2542191aa565Sniklas print_nl ();
2543191aa565Sniklas
2544191aa565Sniklas print_statement_list (w->children.head, os);
25452159047fSniklas }
25462159047fSniklas
25472159047fSniklas /* Print a group statement. */
25482159047fSniklas
25492159047fSniklas static void
print_group(lang_group_statement_type * s,lang_output_section_statement_type * os)2550*007c2a45Smiod print_group (lang_group_statement_type *s,
2551*007c2a45Smiod lang_output_section_statement_type *os)
25522159047fSniklas {
25532159047fSniklas fprintf (config.map_file, "START GROUP\n");
2554191aa565Sniklas print_statement_list (s->children.head, os);
25552159047fSniklas fprintf (config.map_file, "END GROUP\n");
25562159047fSniklas }
25572159047fSniklas
2558191aa565Sniklas /* Print the list of statements in S.
2559191aa565Sniklas This can be called for any statement type. */
2560191aa565Sniklas
2561191aa565Sniklas static void
print_statement_list(lang_statement_union_type * s,lang_output_section_statement_type * os)2562*007c2a45Smiod print_statement_list (lang_statement_union_type *s,
2563*007c2a45Smiod lang_output_section_statement_type *os)
2564191aa565Sniklas {
2565191aa565Sniklas while (s != NULL)
2566191aa565Sniklas {
2567191aa565Sniklas print_statement (s, os);
2568c074d1c9Sdrahn s = s->header.next;
2569191aa565Sniklas }
2570191aa565Sniklas }
2571191aa565Sniklas
2572191aa565Sniklas /* Print the first statement in statement list S.
2573191aa565Sniklas This can be called for any statement type. */
2574191aa565Sniklas
25752159047fSniklas static void
print_statement(lang_statement_union_type * s,lang_output_section_statement_type * os)2576*007c2a45Smiod print_statement (lang_statement_union_type *s,
2577*007c2a45Smiod lang_output_section_statement_type *os)
25782159047fSniklas {
25792159047fSniklas switch (s->header.type)
25802159047fSniklas {
25812159047fSniklas default:
2582b305b0f1Sespie fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
25832159047fSniklas FAIL ();
25842159047fSniklas break;
2585191aa565Sniklas case lang_constructors_statement_enum:
2586191aa565Sniklas if (constructor_list.head != NULL)
2587191aa565Sniklas {
2588b305b0f1Sespie if (constructors_sorted)
2589b305b0f1Sespie minfo (" SORT (CONSTRUCTORS)\n");
2590b305b0f1Sespie else
2591191aa565Sniklas minfo (" CONSTRUCTORS\n");
2592191aa565Sniklas print_statement_list (constructor_list.head, os);
2593191aa565Sniklas }
2594191aa565Sniklas break;
2595191aa565Sniklas case lang_wild_statement_enum:
2596191aa565Sniklas print_wild_statement (&s->wild_statement, os);
2597191aa565Sniklas break;
25982159047fSniklas case lang_address_statement_enum:
2599191aa565Sniklas print_address_statement (&s->address_statement);
26002159047fSniklas break;
26012159047fSniklas case lang_object_symbols_statement_enum:
2602191aa565Sniklas minfo (" CREATE_OBJECT_SYMBOLS\n");
26032159047fSniklas break;
26042159047fSniklas case lang_fill_statement_enum:
26052159047fSniklas print_fill_statement (&s->fill_statement);
26062159047fSniklas break;
26072159047fSniklas case lang_data_statement_enum:
26082159047fSniklas print_data_statement (&s->data_statement);
26092159047fSniklas break;
26102159047fSniklas case lang_reloc_statement_enum:
26112159047fSniklas print_reloc_statement (&s->reloc_statement);
26122159047fSniklas break;
26132159047fSniklas case lang_input_section_enum:
26142159047fSniklas print_input_section (&s->input_section);
26152159047fSniklas break;
26162159047fSniklas case lang_padding_statement_enum:
26172159047fSniklas print_padding_statement (&s->padding_statement);
26182159047fSniklas break;
26192159047fSniklas case lang_output_section_statement_enum:
26202159047fSniklas print_output_section_statement (&s->output_section_statement);
26212159047fSniklas break;
26222159047fSniklas case lang_assignment_statement_enum:
2623191aa565Sniklas print_assignment (&s->assignment_statement, os);
26242159047fSniklas break;
26252159047fSniklas case lang_target_statement_enum:
26262159047fSniklas fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
26272159047fSniklas break;
26282159047fSniklas case lang_output_statement_enum:
2629191aa565Sniklas minfo ("OUTPUT(%s", s->output_statement.name);
2630191aa565Sniklas if (output_target != NULL)
2631191aa565Sniklas minfo (" %s", output_target);
2632191aa565Sniklas minfo (")\n");
26332159047fSniklas break;
26342159047fSniklas case lang_input_statement_enum:
26352159047fSniklas print_input_statement (&s->input_statement);
26362159047fSniklas break;
26372159047fSniklas case lang_group_statement_enum:
26382159047fSniklas print_group (&s->group_statement, os);
26392159047fSniklas break;
26402159047fSniklas case lang_afile_asection_pair_statement_enum:
26412159047fSniklas FAIL ();
26422159047fSniklas break;
26432159047fSniklas }
26442159047fSniklas }
26452159047fSniklas
26462159047fSniklas static void
print_statements(void)2647*007c2a45Smiod print_statements (void)
26482159047fSniklas {
2649191aa565Sniklas print_statement_list (statement_list.head, abs_output_section);
2650191aa565Sniklas }
26512159047fSniklas
2652191aa565Sniklas /* Print the first N statements in statement list S to STDERR.
2653191aa565Sniklas If N == 0, nothing is printed.
2654191aa565Sniklas If N < 0, the entire list is printed.
2655191aa565Sniklas Intended to be called from GDB. */
2656191aa565Sniklas
2657191aa565Sniklas void
dprint_statement(lang_statement_union_type * s,int n)2658*007c2a45Smiod dprint_statement (lang_statement_union_type *s, int n)
2659191aa565Sniklas {
2660191aa565Sniklas FILE *map_save = config.map_file;
2661191aa565Sniklas
2662191aa565Sniklas config.map_file = stderr;
2663191aa565Sniklas
2664191aa565Sniklas if (n < 0)
2665191aa565Sniklas print_statement_list (s, abs_output_section);
2666191aa565Sniklas else
2667191aa565Sniklas {
2668191aa565Sniklas while (s && --n >= 0)
2669191aa565Sniklas {
2670191aa565Sniklas print_statement (s, abs_output_section);
2671c074d1c9Sdrahn s = s->header.next;
2672191aa565Sniklas }
2673191aa565Sniklas }
2674191aa565Sniklas
2675191aa565Sniklas config.map_file = map_save;
26762159047fSniklas }
26772159047fSniklas
2678c074d1c9Sdrahn static void
insert_pad(lang_statement_union_type ** ptr,fill_type * fill,unsigned int alignment_needed,asection * output_section,bfd_vma dot)2679*007c2a45Smiod insert_pad (lang_statement_union_type **ptr,
2680*007c2a45Smiod fill_type *fill,
2681*007c2a45Smiod unsigned int alignment_needed,
2682*007c2a45Smiod asection *output_section,
2683*007c2a45Smiod bfd_vma dot)
26842159047fSniklas {
2685c074d1c9Sdrahn static fill_type zero_fill = { 1, { 0 } };
2686c074d1c9Sdrahn lang_statement_union_type *pad;
26872159047fSniklas
2688c074d1c9Sdrahn pad = ((lang_statement_union_type *)
2689c074d1c9Sdrahn ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2690c074d1c9Sdrahn if (ptr != &statement_list.head
2691c074d1c9Sdrahn && pad->header.type == lang_padding_statement_enum
2692c074d1c9Sdrahn && pad->padding_statement.output_section == output_section)
26932159047fSniklas {
2694c074d1c9Sdrahn /* Use the existing pad statement. The above test on output
2695c074d1c9Sdrahn section is probably redundant, but it doesn't hurt to check. */
2696c074d1c9Sdrahn }
2697c074d1c9Sdrahn else
2698c074d1c9Sdrahn {
2699c074d1c9Sdrahn /* Make a new padding statement, linked into existing chain. */
2700*007c2a45Smiod pad = stat_alloc (sizeof (lang_padding_statement_type));
2701c074d1c9Sdrahn pad->header.next = *ptr;
2702c074d1c9Sdrahn *ptr = pad;
2703c074d1c9Sdrahn pad->header.type = lang_padding_statement_enum;
2704c074d1c9Sdrahn pad->padding_statement.output_section = output_section;
2705*007c2a45Smiod if (fill == NULL)
2706c074d1c9Sdrahn fill = &zero_fill;
2707c074d1c9Sdrahn pad->padding_statement.fill = fill;
27082159047fSniklas }
2709c074d1c9Sdrahn pad->padding_statement.output_offset = dot - output_section->vma;
2710c074d1c9Sdrahn pad->padding_statement.size = alignment_needed;
2711c074d1c9Sdrahn output_section->_raw_size += alignment_needed;
27122159047fSniklas }
27132159047fSniklas
2714b55d4692Sfgsch /* Work out how much this section will move the dot point. */
2715b55d4692Sfgsch
27162159047fSniklas static bfd_vma
size_input_section(lang_statement_union_type ** this_ptr,lang_output_section_statement_type * output_section_statement,fill_type * fill,bfd_vma dot)2717*007c2a45Smiod size_input_section (lang_statement_union_type **this_ptr,
2718*007c2a45Smiod lang_output_section_statement_type *output_section_statement,
2719*007c2a45Smiod fill_type *fill,
2720*007c2a45Smiod bfd_vma dot)
27212159047fSniklas {
27222159047fSniklas lang_input_section_type *is = &((*this_ptr)->input_section);
27232159047fSniklas asection *i = is->section;
2724c074d1c9Sdrahn
2725c074d1c9Sdrahn if (!is->ifile->just_syms_flag)
2726c074d1c9Sdrahn {
2727c074d1c9Sdrahn unsigned int alignment_needed;
2728c074d1c9Sdrahn asection *o;
27292159047fSniklas
2730c074d1c9Sdrahn /* Align this section first to the input sections requirement,
2731c074d1c9Sdrahn then to the output section's requirement. If this alignment
2732c074d1c9Sdrahn is greater than any seen before, then record it too. Perform
2733c074d1c9Sdrahn the alignment by inserting a magic 'padding' statement. */
2734c074d1c9Sdrahn
27352159047fSniklas if (output_section_statement->subsection_alignment != -1)
2736c074d1c9Sdrahn i->alignment_power = output_section_statement->subsection_alignment;
27372159047fSniklas
2738c074d1c9Sdrahn o = output_section_statement->bfd_section;
2739c074d1c9Sdrahn if (o->alignment_power < i->alignment_power)
2740c074d1c9Sdrahn o->alignment_power = i->alignment_power;
2741c074d1c9Sdrahn
2742c074d1c9Sdrahn alignment_needed = align_power (dot, i->alignment_power) - dot;
2743c074d1c9Sdrahn
2744c074d1c9Sdrahn if (alignment_needed != 0)
2745c074d1c9Sdrahn {
2746*007c2a45Smiod insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2747c074d1c9Sdrahn dot += alignment_needed;
2748c074d1c9Sdrahn }
27492159047fSniklas
2750b55d4692Sfgsch /* Remember where in the output section this input section goes. */
27512159047fSniklas
2752c074d1c9Sdrahn i->output_offset = dot - o->vma;
27532159047fSniklas
2754b55d4692Sfgsch /* Mark how big the output section must be to contain this now. */
27552159047fSniklas if (i->_cooked_size != 0)
2756*007c2a45Smiod dot += TO_ADDR (i->_cooked_size);
27572159047fSniklas else
2758*007c2a45Smiod dot += TO_ADDR (i->_raw_size);
2759*007c2a45Smiod o->_raw_size = TO_SIZE (dot - o->vma);
27602159047fSniklas }
27612159047fSniklas else
27622159047fSniklas {
27632159047fSniklas i->output_offset = i->vma - output_section_statement->bfd_section->vma;
27642159047fSniklas }
27652159047fSniklas
27662159047fSniklas return dot;
27672159047fSniklas }
27682159047fSniklas
2769b305b0f1Sespie #define IGNORE_SECTION(bfd, s) \
2770*007c2a45Smiod (((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2771*007c2a45Smiod ? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2772*007c2a45Smiod != SEC_LOAD) \
2773*007c2a45Smiod : ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2774*007c2a45Smiod != SEC_ALLOC)) \
2775b305b0f1Sespie || bfd_section_size (bfd, s) == 0)
2776b305b0f1Sespie
2777b305b0f1Sespie /* Check to see if any allocated sections overlap with other allocated
2778b305b0f1Sespie sections. This can happen when the linker script specifically specifies
2779b305b0f1Sespie the output section addresses of the two sections. */
2780b55d4692Sfgsch
2781b305b0f1Sespie static void
lang_check_section_addresses(void)2782*007c2a45Smiod lang_check_section_addresses (void)
2783b305b0f1Sespie {
2784b305b0f1Sespie asection *s;
2785b305b0f1Sespie
2786b305b0f1Sespie /* Scan all sections in the output list. */
2787b305b0f1Sespie for (s = output_bfd->sections; s != NULL; s = s->next)
2788b305b0f1Sespie {
2789b305b0f1Sespie asection *os;
2790b305b0f1Sespie
2791b305b0f1Sespie /* Ignore sections which are not loaded or which have no contents. */
2792b305b0f1Sespie if (IGNORE_SECTION (output_bfd, s))
2793b305b0f1Sespie continue;
2794b305b0f1Sespie
2795b305b0f1Sespie /* Once we reach section 's' stop our seach. This prevents two
2796b305b0f1Sespie warning messages from being produced, one for 'section A overlaps
2797b305b0f1Sespie section B' and one for 'section B overlaps section A'. */
2798b305b0f1Sespie for (os = output_bfd->sections; os != s; os = os->next)
2799b305b0f1Sespie {
2800b305b0f1Sespie bfd_vma s_start;
2801b305b0f1Sespie bfd_vma s_end;
2802b305b0f1Sespie bfd_vma os_start;
2803b305b0f1Sespie bfd_vma os_end;
2804b305b0f1Sespie
2805b305b0f1Sespie /* Only consider loadable sections with real contents. */
2806b305b0f1Sespie if (IGNORE_SECTION (output_bfd, os))
2807b305b0f1Sespie continue;
2808b305b0f1Sespie
2809b305b0f1Sespie /* We must check the sections' LMA addresses not their
2810b305b0f1Sespie VMA addresses because overlay sections can have
2811b305b0f1Sespie overlapping VMAs but they must have distinct LMAs. */
2812b305b0f1Sespie s_start = bfd_section_lma (output_bfd, s);
2813b305b0f1Sespie os_start = bfd_section_lma (output_bfd, os);
2814*007c2a45Smiod s_end = s_start + TO_ADDR (bfd_section_size (output_bfd, s)) - 1;
2815*007c2a45Smiod os_end = os_start + TO_ADDR (bfd_section_size (output_bfd, os)) - 1;
2816b305b0f1Sespie
2817b305b0f1Sespie /* Look for an overlap. */
2818b305b0f1Sespie if ((s_end < os_start) || (s_start > os_end))
2819b305b0f1Sespie continue;
2820b305b0f1Sespie
2821b305b0f1Sespie einfo (
2822b305b0f1Sespie _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2823b305b0f1Sespie s->name, s_start, s_end, os->name, os_start, os_end);
2824b305b0f1Sespie
2825b305b0f1Sespie /* Once we have found one overlap for this section,
2826b305b0f1Sespie stop looking for others. */
2827b305b0f1Sespie break;
2828b305b0f1Sespie }
2829b305b0f1Sespie }
2830b305b0f1Sespie }
2831b305b0f1Sespie
2832b305b0f1Sespie /* Make sure the new address is within the region. We explicitly permit the
2833b305b0f1Sespie current address to be at the exact end of the region when the address is
2834b305b0f1Sespie non-zero, in case the region is at the end of addressable memory and the
2835b305b0f1Sespie calculation wraps around. */
2836b305b0f1Sespie
2837b305b0f1Sespie static void
os_region_check(lang_output_section_statement_type * os,lang_memory_region_type * region,etree_type * tree,bfd_vma base)2838*007c2a45Smiod os_region_check (lang_output_section_statement_type *os,
2839*007c2a45Smiod lang_memory_region_type *region,
2840*007c2a45Smiod etree_type *tree,
2841*007c2a45Smiod bfd_vma base)
2842b305b0f1Sespie {
2843b305b0f1Sespie if ((region->current < region->origin
2844b305b0f1Sespie || (region->current - region->origin > region->length))
2845b305b0f1Sespie && ((region->current != region->origin + region->length)
2846b305b0f1Sespie || base == 0))
2847b305b0f1Sespie {
2848*007c2a45Smiod if (tree != NULL)
2849b305b0f1Sespie {
2850b305b0f1Sespie einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2851b305b0f1Sespie region->current,
2852b305b0f1Sespie os->bfd_section->owner,
2853b305b0f1Sespie os->bfd_section->name,
2854b305b0f1Sespie region->name);
2855b305b0f1Sespie }
2856b305b0f1Sespie else
2857b305b0f1Sespie {
2858b305b0f1Sespie einfo (_("%X%P: region %s is full (%B section %s)\n"),
2859b305b0f1Sespie region->name,
2860b305b0f1Sespie os->bfd_section->owner,
2861b305b0f1Sespie os->bfd_section->name);
2862b305b0f1Sespie }
2863b305b0f1Sespie /* Reset the region pointer. */
2864b305b0f1Sespie region->current = region->origin;
2865b305b0f1Sespie }
2866b305b0f1Sespie }
2867b305b0f1Sespie
28682159047fSniklas /* Set the sizes for all the output sections. */
28692159047fSniklas
2870c074d1c9Sdrahn static bfd_vma
lang_size_sections_1(lang_statement_union_type * s,lang_output_section_statement_type * output_section_statement,lang_statement_union_type ** prev,fill_type * fill,bfd_vma dot,bfd_boolean * relax,bfd_boolean check_regions)2871*007c2a45Smiod lang_size_sections_1
2872*007c2a45Smiod (lang_statement_union_type *s,
2873*007c2a45Smiod lang_output_section_statement_type *output_section_statement,
2874*007c2a45Smiod lang_statement_union_type **prev,
2875*007c2a45Smiod fill_type *fill,
2876*007c2a45Smiod bfd_vma dot,
2877*007c2a45Smiod bfd_boolean *relax,
2878*007c2a45Smiod bfd_boolean check_regions)
28792159047fSniklas {
2880b305b0f1Sespie /* Size up the sections from their constituent parts. */
2881*007c2a45Smiod for (; s != NULL; s = s->header.next)
28822159047fSniklas {
28832159047fSniklas switch (s->header.type)
28842159047fSniklas {
28852159047fSniklas case lang_output_section_statement_enum:
28862159047fSniklas {
28872159047fSniklas bfd_vma after;
2888b55d4692Sfgsch lang_output_section_statement_type *os;
28892159047fSniklas
2890b55d4692Sfgsch os = &s->output_section_statement;
28912159047fSniklas if (os->bfd_section == NULL)
28922159047fSniklas /* This section was never actually created. */
28932159047fSniklas break;
28942159047fSniklas
28952159047fSniklas /* If this is a COFF shared library section, use the size and
28962159047fSniklas address from the input section. FIXME: This is COFF
28972159047fSniklas specific; it would be cleaner if there were some other way
28982159047fSniklas to do this, but nothing simple comes to mind. */
28992159047fSniklas if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
29002159047fSniklas {
29012159047fSniklas asection *input;
29022159047fSniklas
29032159047fSniklas if (os->children.head == NULL
2904c074d1c9Sdrahn || os->children.head->header.next != NULL
29052159047fSniklas || os->children.head->header.type != lang_input_section_enum)
2906b305b0f1Sespie einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
29072159047fSniklas os->name);
29082159047fSniklas
29092159047fSniklas input = os->children.head->input_section.section;
29102159047fSniklas bfd_set_section_vma (os->bfd_section->owner,
29112159047fSniklas os->bfd_section,
29122159047fSniklas bfd_section_vma (input->owner, input));
29132159047fSniklas os->bfd_section->_raw_size = input->_raw_size;
29142159047fSniklas break;
29152159047fSniklas }
29162159047fSniklas
29172159047fSniklas if (bfd_is_abs_section (os->bfd_section))
29182159047fSniklas {
2919b305b0f1Sespie /* No matter what happens, an abs section starts at zero. */
29202159047fSniklas ASSERT (os->bfd_section->vma == 0);
29212159047fSniklas }
29222159047fSniklas else
29232159047fSniklas {
2924*007c2a45Smiod if (os->addr_tree == NULL)
29252159047fSniklas {
29262159047fSniklas /* No address specified for this section, get one
2927b305b0f1Sespie from the region specification. */
2928*007c2a45Smiod if (os->region == NULL
2929b305b0f1Sespie || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2930b305b0f1Sespie & (SEC_ALLOC | SEC_LOAD)) != 0)
2931b305b0f1Sespie && os->region->name[0] == '*'
2932*007c2a45Smiod && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
29332159047fSniklas {
2934b305b0f1Sespie os->region = lang_memory_default (os->bfd_section);
29352159047fSniklas }
2936b305b0f1Sespie
2937b305b0f1Sespie /* If a loadable section is using the default memory
2938b305b0f1Sespie region, and some non default memory regions were
2939*007c2a45Smiod defined, issue an error message. */
2940*007c2a45Smiod if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2941*007c2a45Smiod && ! link_info.relocatable
2942c074d1c9Sdrahn && check_regions
2943*007c2a45Smiod && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
2944b305b0f1Sespie && lang_memory_region_list != NULL
2945b55d4692Sfgsch && (strcmp (lang_memory_region_list->name,
2946*007c2a45Smiod DEFAULT_MEMORY_REGION) != 0
2947b305b0f1Sespie || lang_memory_region_list->next != NULL))
2948*007c2a45Smiod {
2949*007c2a45Smiod /* By default this is an error rather than just a
2950*007c2a45Smiod warning because if we allocate the section to the
2951*007c2a45Smiod default memory region we can end up creating an
2952*007c2a45Smiod excessively large binary, or even seg faulting when
2953*007c2a45Smiod attempting to perform a negative seek. See
2954*007c2a45Smiod http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2955*007c2a45Smiod for an example of this. This behaviour can be
2956*007c2a45Smiod overridden by the using the --no-check-sections
2957*007c2a45Smiod switch. */
2958*007c2a45Smiod if (command_line.check_section_addresses)
2959*007c2a45Smiod einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2960b55d4692Sfgsch bfd_get_section_name (output_bfd,
2961b55d4692Sfgsch os->bfd_section));
2962*007c2a45Smiod else
2963*007c2a45Smiod einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2964*007c2a45Smiod bfd_get_section_name (output_bfd,
2965*007c2a45Smiod os->bfd_section));
2966*007c2a45Smiod }
2967b305b0f1Sespie
29682159047fSniklas dot = os->region->current;
2969b305b0f1Sespie
29702159047fSniklas if (os->section_alignment == -1)
2971e93f7393Sniklas {
2972e93f7393Sniklas bfd_vma olddot;
2973e93f7393Sniklas
2974e93f7393Sniklas olddot = dot;
2975b55d4692Sfgsch dot = align_power (dot,
2976b55d4692Sfgsch os->bfd_section->alignment_power);
2977b305b0f1Sespie
2978e93f7393Sniklas if (dot != olddot && config.warn_section_align)
2979b305b0f1Sespie einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2980e93f7393Sniklas os->name, (unsigned int) (dot - olddot));
2981e93f7393Sniklas }
29822159047fSniklas }
29832159047fSniklas else
29842159047fSniklas {
29852159047fSniklas etree_value_type r;
29862159047fSniklas
2987*007c2a45Smiod os->processed = -1;
29882159047fSniklas r = exp_fold_tree (os->addr_tree,
29892159047fSniklas abs_output_section,
29902159047fSniklas lang_allocating_phase_enum,
29912159047fSniklas dot, &dot);
2992*007c2a45Smiod os->processed = 0;
2993*007c2a45Smiod
2994c074d1c9Sdrahn if (!r.valid_p)
2995*007c2a45Smiod einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
29962159047fSniklas os->name);
2997c074d1c9Sdrahn
29980c6d0228Sniklas dot = r.value + r.section->bfd_section->vma;
29992159047fSniklas }
3000b305b0f1Sespie
3001b305b0f1Sespie /* The section starts here.
3002b305b0f1Sespie First, align to what the section needs. */
30032159047fSniklas
30042159047fSniklas if (os->section_alignment != -1)
30052159047fSniklas dot = align_power (dot, os->section_alignment);
30062159047fSniklas
30072159047fSniklas bfd_set_section_vma (0, os->bfd_section, dot);
30082159047fSniklas
30092159047fSniklas os->bfd_section->output_offset = 0;
30102159047fSniklas }
30112159047fSniklas
3012c074d1c9Sdrahn lang_size_sections_1 (os->children.head, os, &os->children.head,
3013c074d1c9Sdrahn os->fill, dot, relax, check_regions);
30142159047fSniklas
3015b55d4692Sfgsch /* Put the section within the requested block size, or
3016b55d4692Sfgsch align at the block boundary. */
3017*007c2a45Smiod after = ((os->bfd_section->vma
3018*007c2a45Smiod + TO_ADDR (os->bfd_section->_raw_size)
3019*007c2a45Smiod + os->block_value - 1)
3020*007c2a45Smiod & - (bfd_vma) os->block_value);
30212159047fSniklas
30222159047fSniklas if (bfd_is_abs_section (os->bfd_section))
30232159047fSniklas ASSERT (after == os->bfd_section->vma);
30242159047fSniklas else
3025*007c2a45Smiod os->bfd_section->_raw_size
3026*007c2a45Smiod = TO_SIZE (after - os->bfd_section->vma);
3027c074d1c9Sdrahn
3028*007c2a45Smiod dot = os->bfd_section->vma;
3029*007c2a45Smiod /* .tbss sections effectively have zero size. */
3030*007c2a45Smiod if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3031*007c2a45Smiod || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3032*007c2a45Smiod || link_info.relocatable)
3033*007c2a45Smiod dot += TO_ADDR (os->bfd_section->_raw_size);
3034*007c2a45Smiod
3035*007c2a45Smiod os->processed = 1;
3036c074d1c9Sdrahn
3037c074d1c9Sdrahn if (os->update_dot_tree != 0)
3038c074d1c9Sdrahn exp_fold_tree (os->update_dot_tree, abs_output_section,
3039c074d1c9Sdrahn lang_allocating_phase_enum, dot, &dot);
30402159047fSniklas
3041b305b0f1Sespie /* Update dot in the region ?
3042b305b0f1Sespie We only do this if the section is going to be allocated,
3043b305b0f1Sespie since unallocated sections do not contribute to the region's
3044b305b0f1Sespie overall size in memory.
3045b305b0f1Sespie
3046b305b0f1Sespie If the SEC_NEVER_LOAD bit is not set, it will affect the
3047b305b0f1Sespie addresses of sections after it. We have to update
3048b305b0f1Sespie dot. */
3049*007c2a45Smiod if (os->region != NULL
3050b305b0f1Sespie && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3051b305b0f1Sespie & SEC_NEVER_LOAD) == 0
3052b305b0f1Sespie || (bfd_get_section_flags (output_bfd, os->bfd_section)
3053b305b0f1Sespie & (SEC_ALLOC | SEC_LOAD))))
30542159047fSniklas {
30552159047fSniklas os->region->current = dot;
3056b305b0f1Sespie
3057c074d1c9Sdrahn if (check_regions)
3058b305b0f1Sespie /* Make sure the new address is within the region. */
3059b305b0f1Sespie os_region_check (os, os->region, os->addr_tree,
3060b305b0f1Sespie os->bfd_section->vma);
3061b305b0f1Sespie
3062b55d4692Sfgsch /* If there's no load address specified, use the run
3063b55d4692Sfgsch region as the load region. */
3064b305b0f1Sespie if (os->lma_region == NULL && os->load_base == NULL)
3065b305b0f1Sespie os->lma_region = os->region;
3066b305b0f1Sespie
3067c074d1c9Sdrahn if (os->lma_region != NULL && os->lma_region != os->region)
3068b305b0f1Sespie {
3069b55d4692Sfgsch /* Set load_base, which will be handled later. */
3070c074d1c9Sdrahn os->load_base = exp_intop (os->lma_region->current);
3071b305b0f1Sespie os->lma_region->current +=
3072*007c2a45Smiod TO_ADDR (os->bfd_section->_raw_size);
3073c074d1c9Sdrahn if (check_regions)
3074b305b0f1Sespie os_region_check (os, os->lma_region, NULL,
3075b305b0f1Sespie os->bfd_section->lma);
30762159047fSniklas }
3077b305b0f1Sespie }
30782159047fSniklas }
30792159047fSniklas break;
30802159047fSniklas
30812159047fSniklas case lang_constructors_statement_enum:
3082c074d1c9Sdrahn dot = lang_size_sections_1 (constructor_list.head,
30832159047fSniklas output_section_statement,
30842159047fSniklas &s->wild_statement.children.head,
3085c074d1c9Sdrahn fill, dot, relax, check_regions);
30862159047fSniklas break;
30872159047fSniklas
30882159047fSniklas case lang_data_statement_enum:
30892159047fSniklas {
30902159047fSniklas unsigned int size = 0;
30912159047fSniklas
3092b55d4692Sfgsch s->data_statement.output_vma =
3093b55d4692Sfgsch dot - output_section_statement->bfd_section->vma;
30942159047fSniklas s->data_statement.output_section =
30952159047fSniklas output_section_statement->bfd_section;
30962159047fSniklas
3097*007c2a45Smiod /* We might refer to provided symbols in the expression, and
3098*007c2a45Smiod need to mark them as needed. */
3099*007c2a45Smiod exp_fold_tree (s->data_statement.exp, abs_output_section,
3100*007c2a45Smiod lang_allocating_phase_enum, dot, &dot);
3101*007c2a45Smiod
31022159047fSniklas switch (s->data_statement.type)
31032159047fSniklas {
3104b305b0f1Sespie default:
3105b305b0f1Sespie abort ();
31062159047fSniklas case QUAD:
3107b305b0f1Sespie case SQUAD:
31082159047fSniklas size = QUAD_SIZE;
31092159047fSniklas break;
31102159047fSniklas case LONG:
31112159047fSniklas size = LONG_SIZE;
31122159047fSniklas break;
31132159047fSniklas case SHORT:
31142159047fSniklas size = SHORT_SIZE;
31152159047fSniklas break;
31162159047fSniklas case BYTE:
31172159047fSniklas size = BYTE_SIZE;
31182159047fSniklas break;
31192159047fSniklas }
3120*007c2a45Smiod if (size < TO_SIZE ((unsigned) 1))
3121*007c2a45Smiod size = TO_SIZE ((unsigned) 1);
3122*007c2a45Smiod dot += TO_ADDR (size);
31232159047fSniklas output_section_statement->bfd_section->_raw_size += size;
31242159047fSniklas /* The output section gets contents, and then we inspect for
3125b305b0f1Sespie any flags set in the input script which override any ALLOC. */
31262159047fSniklas output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3127b55d4692Sfgsch if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3128b55d4692Sfgsch {
3129b55d4692Sfgsch output_section_statement->bfd_section->flags |=
3130b55d4692Sfgsch SEC_ALLOC | SEC_LOAD;
31312159047fSniklas }
31322159047fSniklas }
31332159047fSniklas break;
31342159047fSniklas
31352159047fSniklas case lang_reloc_statement_enum:
31362159047fSniklas {
31372159047fSniklas int size;
31382159047fSniklas
31392159047fSniklas s->reloc_statement.output_vma =
31402159047fSniklas dot - output_section_statement->bfd_section->vma;
31412159047fSniklas s->reloc_statement.output_section =
31422159047fSniklas output_section_statement->bfd_section;
31432159047fSniklas size = bfd_get_reloc_size (s->reloc_statement.howto);
3144*007c2a45Smiod dot += TO_ADDR (size);
31452159047fSniklas output_section_statement->bfd_section->_raw_size += size;
31462159047fSniklas }
31472159047fSniklas break;
31482159047fSniklas
31492159047fSniklas case lang_wild_statement_enum:
31502159047fSniklas
3151c074d1c9Sdrahn dot = lang_size_sections_1 (s->wild_statement.children.head,
31522159047fSniklas output_section_statement,
31532159047fSniklas &s->wild_statement.children.head,
3154c074d1c9Sdrahn fill, dot, relax, check_regions);
31552159047fSniklas
31562159047fSniklas break;
31572159047fSniklas
31582159047fSniklas case lang_object_symbols_statement_enum:
31592159047fSniklas link_info.create_object_symbols_section =
31602159047fSniklas output_section_statement->bfd_section;
31612159047fSniklas break;
31622159047fSniklas case lang_output_statement_enum:
31632159047fSniklas case lang_target_statement_enum:
31642159047fSniklas break;
31652159047fSniklas case lang_input_section_enum:
31662159047fSniklas {
31672159047fSniklas asection *i;
31682159047fSniklas
31692159047fSniklas i = (*prev)->input_section.section;
31702159047fSniklas if (! relax)
3171191aa565Sniklas {
3172191aa565Sniklas if (i->_cooked_size == 0)
31732159047fSniklas i->_cooked_size = i->_raw_size;
3174191aa565Sniklas }
31752159047fSniklas else
31762159047fSniklas {
3177c074d1c9Sdrahn bfd_boolean again;
31782159047fSniklas
31792159047fSniklas if (! bfd_relax_section (i->owner, i, &link_info, &again))
3180b305b0f1Sespie einfo (_("%P%F: can't relax section: %E\n"));
31812159047fSniklas if (again)
3182c074d1c9Sdrahn *relax = TRUE;
31832159047fSniklas }
3184c074d1c9Sdrahn dot = size_input_section (prev, output_section_statement,
3185c074d1c9Sdrahn output_section_statement->fill, dot);
31862159047fSniklas }
31872159047fSniklas break;
31882159047fSniklas case lang_input_statement_enum:
31892159047fSniklas break;
31902159047fSniklas case lang_fill_statement_enum:
3191b55d4692Sfgsch s->fill_statement.output_section =
3192b55d4692Sfgsch output_section_statement->bfd_section;
31932159047fSniklas
31942159047fSniklas fill = s->fill_statement.fill;
31952159047fSniklas break;
31962159047fSniklas case lang_assignment_statement_enum:
31972159047fSniklas {
31982159047fSniklas bfd_vma newdot = dot;
31992159047fSniklas
32002159047fSniklas exp_fold_tree (s->assignment_statement.exp,
32012159047fSniklas output_section_statement,
32022159047fSniklas lang_allocating_phase_enum,
32032159047fSniklas dot,
32042159047fSniklas &newdot);
32052159047fSniklas
3206b305b0f1Sespie if (newdot != dot)
32072159047fSniklas {
32082159047fSniklas if (output_section_statement == abs_output_section)
32092159047fSniklas {
32102159047fSniklas /* If we don't have an output section, then just adjust
32112159047fSniklas the default memory address. */
3212*007c2a45Smiod lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
32132159047fSniklas }
3214c074d1c9Sdrahn else
32152159047fSniklas {
3216c074d1c9Sdrahn /* Insert a pad after this statement. We can't
3217c074d1c9Sdrahn put the pad before when relaxing, in case the
3218c074d1c9Sdrahn assignment references dot. */
3219*007c2a45Smiod insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3220c074d1c9Sdrahn output_section_statement->bfd_section, dot);
32212159047fSniklas
3222c074d1c9Sdrahn /* Don't neuter the pad below when relaxing. */
3223c074d1c9Sdrahn s = s->header.next;
32242159047fSniklas }
32252159047fSniklas
3226*007c2a45Smiod /* If dot is advanced, this implies that the section should
3227*007c2a45Smiod have space allocated to it, unless the user has explicitly
3228*007c2a45Smiod stated that the section should never be loaded. */
3229*007c2a45Smiod if (!(output_section_statement->flags & (SEC_NEVER_LOAD | SEC_ALLOC)))
3230*007c2a45Smiod output_section_statement->bfd_section->flags |= SEC_ALLOC;
3231*007c2a45Smiod
32322159047fSniklas dot = newdot;
32332159047fSniklas }
32342159047fSniklas }
32352159047fSniklas break;
32362159047fSniklas
32372159047fSniklas case lang_padding_statement_enum:
3238c074d1c9Sdrahn /* If this is the first time lang_size_sections is called,
3239c074d1c9Sdrahn we won't have any padding statements. If this is the
3240c074d1c9Sdrahn second or later passes when relaxing, we should allow
3241c074d1c9Sdrahn padding to shrink. If padding is needed on this pass, it
3242c074d1c9Sdrahn will be added back in. */
3243c074d1c9Sdrahn s->padding_statement.size = 0;
3244c074d1c9Sdrahn
3245c074d1c9Sdrahn /* Make sure output_offset is valid. If relaxation shrinks
3246c074d1c9Sdrahn the section and this pad isn't needed, it's possible to
3247c074d1c9Sdrahn have output_offset larger than the final size of the
3248c074d1c9Sdrahn section. bfd_set_section_contents will complain even for
3249c074d1c9Sdrahn a pad size of zero. */
3250c074d1c9Sdrahn s->padding_statement.output_offset
3251c074d1c9Sdrahn = dot - output_section_statement->bfd_section->vma;
32522159047fSniklas break;
32532159047fSniklas
32542159047fSniklas case lang_group_statement_enum:
3255c074d1c9Sdrahn dot = lang_size_sections_1 (s->group_statement.children.head,
32562159047fSniklas output_section_statement,
32572159047fSniklas &s->group_statement.children.head,
3258c074d1c9Sdrahn fill, dot, relax, check_regions);
32592159047fSniklas break;
32602159047fSniklas
32612159047fSniklas default:
32622159047fSniklas FAIL ();
32632159047fSniklas break;
32642159047fSniklas
3265c074d1c9Sdrahn /* We can only get here when relaxing is turned on. */
32662159047fSniklas case lang_address_statement_enum:
32672159047fSniklas break;
32682159047fSniklas }
32692159047fSniklas prev = &s->header.next;
32702159047fSniklas }
32712159047fSniklas return dot;
32722159047fSniklas }
32732159047fSniklas
32742159047fSniklas bfd_vma
lang_size_sections(lang_statement_union_type * s,lang_output_section_statement_type * output_section_statement,lang_statement_union_type ** prev,fill_type * fill,bfd_vma dot,bfd_boolean * relax,bfd_boolean check_regions)3275*007c2a45Smiod lang_size_sections
3276*007c2a45Smiod (lang_statement_union_type *s,
3277*007c2a45Smiod lang_output_section_statement_type *output_section_statement,
3278*007c2a45Smiod lang_statement_union_type **prev,
3279*007c2a45Smiod fill_type *fill,
3280*007c2a45Smiod bfd_vma dot,
3281*007c2a45Smiod bfd_boolean *relax,
3282*007c2a45Smiod bfd_boolean check_regions)
3283c074d1c9Sdrahn {
3284c074d1c9Sdrahn bfd_vma result;
3285*007c2a45Smiod asection *o;
3286*007c2a45Smiod
3287*007c2a45Smiod /* Callers of exp_fold_tree need to increment this. */
3288*007c2a45Smiod lang_statement_iteration++;
3289c074d1c9Sdrahn
3290c074d1c9Sdrahn exp_data_seg.phase = exp_dataseg_none;
3291c074d1c9Sdrahn result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3292c074d1c9Sdrahn dot, relax, check_regions);
3293c074d1c9Sdrahn if (exp_data_seg.phase == exp_dataseg_end_seen)
3294c074d1c9Sdrahn {
3295c074d1c9Sdrahn /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3296c074d1c9Sdrahn a page could be saved in the data segment. */
3297c074d1c9Sdrahn bfd_vma first, last;
3298c074d1c9Sdrahn
3299c074d1c9Sdrahn first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3300c074d1c9Sdrahn last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3301c074d1c9Sdrahn if (first && last
3302c074d1c9Sdrahn && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3303c074d1c9Sdrahn != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3304c074d1c9Sdrahn && first + last <= exp_data_seg.pagesize)
3305c074d1c9Sdrahn {
3306c074d1c9Sdrahn exp_data_seg.phase = exp_dataseg_adjust;
3307*007c2a45Smiod lang_statement_iteration++;
3308c074d1c9Sdrahn result = lang_size_sections_1 (s, output_section_statement, prev,
3309c074d1c9Sdrahn fill, dot, relax, check_regions);
3310c074d1c9Sdrahn }
3311c074d1c9Sdrahn }
3312c074d1c9Sdrahn
3313*007c2a45Smiod /* Some backend relaxers want to refer to the output section size. Give
3314*007c2a45Smiod them a section size that does not change on the next call while they
3315*007c2a45Smiod relax. We can't set this at top because lang_reset_memory_regions
3316*007c2a45Smiod which is called before we get here, sets _raw_size to 0 on relaxing
3317*007c2a45Smiod rounds. */
3318*007c2a45Smiod for (o = output_bfd->sections; o != NULL; o = o->next)
3319*007c2a45Smiod o->_cooked_size = o->_raw_size;
3320*007c2a45Smiod
3321c074d1c9Sdrahn return result;
3322c074d1c9Sdrahn }
3323c074d1c9Sdrahn
3324*007c2a45Smiod /* Worker function for lang_do_assignments. Recursiveness goes here. */
3325b305b0f1Sespie
3326*007c2a45Smiod static bfd_vma
lang_do_assignments_1(lang_statement_union_type * s,lang_output_section_statement_type * output_section_statement,fill_type * fill,bfd_vma dot)3327*007c2a45Smiod lang_do_assignments_1
3328*007c2a45Smiod (lang_statement_union_type *s,
3329*007c2a45Smiod lang_output_section_statement_type *output_section_statement,
3330*007c2a45Smiod fill_type *fill,
3331*007c2a45Smiod bfd_vma dot)
3332*007c2a45Smiod {
3333*007c2a45Smiod for (; s != NULL; s = s->header.next)
33342159047fSniklas {
33352159047fSniklas switch (s->header.type)
33362159047fSniklas {
33372159047fSniklas case lang_constructors_statement_enum:
3338*007c2a45Smiod dot = lang_do_assignments_1 (constructor_list.head,
33392159047fSniklas output_section_statement,
33402159047fSniklas fill,
33412159047fSniklas dot);
33422159047fSniklas break;
33432159047fSniklas
33442159047fSniklas case lang_output_section_statement_enum:
33452159047fSniklas {
3346b55d4692Sfgsch lang_output_section_statement_type *os;
33472159047fSniklas
3348b55d4692Sfgsch os = &(s->output_section_statement);
33492159047fSniklas if (os->bfd_section != NULL)
33502159047fSniklas {
33512159047fSniklas dot = os->bfd_section->vma;
3352*007c2a45Smiod lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3353*007c2a45Smiod dot = (os->bfd_section->vma
3354*007c2a45Smiod + TO_ADDR (os->bfd_section->_raw_size));
3355b305b0f1Sespie
33562159047fSniklas }
33572159047fSniklas if (os->load_base)
33582159047fSniklas {
33592159047fSniklas /* If nothing has been placed into the output section then
33602159047fSniklas it won't have a bfd_section. */
33612159047fSniklas if (os->bfd_section)
33622159047fSniklas {
33632159047fSniklas os->bfd_section->lma
3364b55d4692Sfgsch = exp_get_abs_int (os->load_base, 0, "load base",
3365b55d4692Sfgsch lang_final_phase_enum);
33662159047fSniklas }
33672159047fSniklas }
33682159047fSniklas }
33692159047fSniklas break;
33702159047fSniklas case lang_wild_statement_enum:
33712159047fSniklas
3372*007c2a45Smiod dot = lang_do_assignments_1 (s->wild_statement.children.head,
33732159047fSniklas output_section_statement,
33742159047fSniklas fill, dot);
33752159047fSniklas
33762159047fSniklas break;
33772159047fSniklas
33782159047fSniklas case lang_object_symbols_statement_enum:
33792159047fSniklas case lang_output_statement_enum:
33802159047fSniklas case lang_target_statement_enum:
33812159047fSniklas #if 0
33822159047fSniklas case lang_common_statement_enum:
33832159047fSniklas #endif
33842159047fSniklas break;
33852159047fSniklas case lang_data_statement_enum:
33862159047fSniklas {
33872159047fSniklas etree_value_type value;
33882159047fSniklas
33892159047fSniklas value = exp_fold_tree (s->data_statement.exp,
33902159047fSniklas abs_output_section,
33912159047fSniklas lang_final_phase_enum, dot, &dot);
3392c074d1c9Sdrahn if (!value.valid_p)
3393b305b0f1Sespie einfo (_("%F%P: invalid data statement\n"));
3394*007c2a45Smiod s->data_statement.value
3395*007c2a45Smiod = value.value + value.section->bfd_section->vma;
33962159047fSniklas }
3397b305b0f1Sespie {
3398b305b0f1Sespie unsigned int size;
33992159047fSniklas switch (s->data_statement.type)
34002159047fSniklas {
3401b305b0f1Sespie default:
3402b305b0f1Sespie abort ();
34032159047fSniklas case QUAD:
3404b305b0f1Sespie case SQUAD:
3405b305b0f1Sespie size = QUAD_SIZE;
34062159047fSniklas break;
34072159047fSniklas case LONG:
3408b305b0f1Sespie size = LONG_SIZE;
34092159047fSniklas break;
34102159047fSniklas case SHORT:
3411b305b0f1Sespie size = SHORT_SIZE;
34122159047fSniklas break;
34132159047fSniklas case BYTE:
3414b305b0f1Sespie size = BYTE_SIZE;
34152159047fSniklas break;
34162159047fSniklas }
3417*007c2a45Smiod if (size < TO_SIZE ((unsigned) 1))
3418*007c2a45Smiod size = TO_SIZE ((unsigned) 1);
3419*007c2a45Smiod dot += TO_ADDR (size);
3420b305b0f1Sespie }
34212159047fSniklas break;
34222159047fSniklas
34232159047fSniklas case lang_reloc_statement_enum:
34242159047fSniklas {
34252159047fSniklas etree_value_type value;
34262159047fSniklas
34272159047fSniklas value = exp_fold_tree (s->reloc_statement.addend_exp,
34282159047fSniklas abs_output_section,
34292159047fSniklas lang_final_phase_enum, dot, &dot);
34302159047fSniklas s->reloc_statement.addend_value = value.value;
3431c074d1c9Sdrahn if (!value.valid_p)
3432b305b0f1Sespie einfo (_("%F%P: invalid reloc statement\n"));
34332159047fSniklas }
3434*007c2a45Smiod dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
34352159047fSniklas break;
34362159047fSniklas
34372159047fSniklas case lang_input_section_enum:
34382159047fSniklas {
34392159047fSniklas asection *in = s->input_section.section;
34402159047fSniklas
34412159047fSniklas if (in->_cooked_size != 0)
3442*007c2a45Smiod dot += TO_ADDR (in->_cooked_size);
34432159047fSniklas else
3444*007c2a45Smiod dot += TO_ADDR (in->_raw_size);
34452159047fSniklas }
34462159047fSniklas break;
34472159047fSniklas
34482159047fSniklas case lang_input_statement_enum:
34492159047fSniklas break;
34502159047fSniklas case lang_fill_statement_enum:
34512159047fSniklas fill = s->fill_statement.fill;
34522159047fSniklas break;
34532159047fSniklas case lang_assignment_statement_enum:
34542159047fSniklas {
34552159047fSniklas exp_fold_tree (s->assignment_statement.exp,
34562159047fSniklas output_section_statement,
34572159047fSniklas lang_final_phase_enum,
34582159047fSniklas dot,
34592159047fSniklas &dot);
34602159047fSniklas }
34612159047fSniklas
34622159047fSniklas break;
34632159047fSniklas case lang_padding_statement_enum:
3464*007c2a45Smiod dot += TO_ADDR (s->padding_statement.size);
34652159047fSniklas break;
34662159047fSniklas
34672159047fSniklas case lang_group_statement_enum:
3468*007c2a45Smiod dot = lang_do_assignments_1 (s->group_statement.children.head,
34692159047fSniklas output_section_statement,
34702159047fSniklas fill, dot);
34712159047fSniklas
34722159047fSniklas break;
34732159047fSniklas
34742159047fSniklas default:
34752159047fSniklas FAIL ();
34762159047fSniklas break;
34772159047fSniklas case lang_address_statement_enum:
34782159047fSniklas break;
34792159047fSniklas }
34802159047fSniklas
34812159047fSniklas }
34822159047fSniklas return dot;
34832159047fSniklas }
34842159047fSniklas
3485*007c2a45Smiod void
lang_do_assignments(lang_statement_union_type * s,lang_output_section_statement_type * output_section_statement,fill_type * fill,bfd_vma dot)3486*007c2a45Smiod lang_do_assignments (lang_statement_union_type *s,
3487*007c2a45Smiod lang_output_section_statement_type *output_section_statement,
3488*007c2a45Smiod fill_type *fill,
3489*007c2a45Smiod bfd_vma dot)
3490*007c2a45Smiod {
3491*007c2a45Smiod /* Callers of exp_fold_tree need to increment this. */
3492*007c2a45Smiod lang_statement_iteration++;
3493*007c2a45Smiod lang_do_assignments_1 (s, output_section_statement, fill, dot);
3494*007c2a45Smiod }
3495*007c2a45Smiod
34962159047fSniklas /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
34972159047fSniklas operator .startof. (section_name), it produces an undefined symbol
34982159047fSniklas .startof.section_name. Similarly, when it sees
34992159047fSniklas .sizeof. (section_name), it produces an undefined symbol
35002159047fSniklas .sizeof.section_name. For all the output sections, we look for
35012159047fSniklas such symbols, and set them to the correct value. */
35022159047fSniklas
35032159047fSniklas static void
lang_set_startof(void)3504*007c2a45Smiod lang_set_startof (void)
35052159047fSniklas {
35062159047fSniklas asection *s;
35072159047fSniklas
3508*007c2a45Smiod if (link_info.relocatable)
3509191aa565Sniklas return;
3510191aa565Sniklas
35112159047fSniklas for (s = output_bfd->sections; s != NULL; s = s->next)
35122159047fSniklas {
35132159047fSniklas const char *secname;
35142159047fSniklas char *buf;
35152159047fSniklas struct bfd_link_hash_entry *h;
35162159047fSniklas
35172159047fSniklas secname = bfd_get_section_name (output_bfd, s);
35182159047fSniklas buf = xmalloc (10 + strlen (secname));
35192159047fSniklas
35202159047fSniklas sprintf (buf, ".startof.%s", secname);
3521c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
35222159047fSniklas if (h != NULL && h->type == bfd_link_hash_undefined)
35232159047fSniklas {
35242159047fSniklas h->type = bfd_link_hash_defined;
35252159047fSniklas h->u.def.value = bfd_get_section_vma (output_bfd, s);
35262159047fSniklas h->u.def.section = bfd_abs_section_ptr;
35272159047fSniklas }
35282159047fSniklas
35292159047fSniklas sprintf (buf, ".sizeof.%s", secname);
3530c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
35312159047fSniklas if (h != NULL && h->type == bfd_link_hash_undefined)
35322159047fSniklas {
35332159047fSniklas h->type = bfd_link_hash_defined;
35342159047fSniklas if (s->_cooked_size != 0)
3535*007c2a45Smiod h->u.def.value = TO_ADDR (s->_cooked_size);
35362159047fSniklas else
3537*007c2a45Smiod h->u.def.value = TO_ADDR (s->_raw_size);
35382159047fSniklas h->u.def.section = bfd_abs_section_ptr;
35392159047fSniklas }
35402159047fSniklas
35412159047fSniklas free (buf);
35422159047fSniklas }
35432159047fSniklas }
35442159047fSniklas
35452159047fSniklas static void
lang_finish(void)3546*007c2a45Smiod lang_finish (void)
35472159047fSniklas {
35482159047fSniklas struct bfd_link_hash_entry *h;
3549c074d1c9Sdrahn bfd_boolean warn;
35502159047fSniklas
3551*007c2a45Smiod if (link_info.relocatable || link_info.shared)
3552c074d1c9Sdrahn warn = FALSE;
35532159047fSniklas else
3554c074d1c9Sdrahn warn = TRUE;
35552159047fSniklas
3556*007c2a45Smiod if (entry_symbol.name == NULL)
35572159047fSniklas {
35582159047fSniklas /* No entry has been specified. Look for start, but don't warn
35592159047fSniklas if we don't find it. */
3560c074d1c9Sdrahn entry_symbol.name = "start";
3561c074d1c9Sdrahn warn = FALSE;
35622159047fSniklas }
35632159047fSniklas
3564c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3565c074d1c9Sdrahn FALSE, FALSE, TRUE);
3566*007c2a45Smiod if (h != NULL
35672159047fSniklas && (h->type == bfd_link_hash_defined
35682159047fSniklas || h->type == bfd_link_hash_defweak)
35692159047fSniklas && h->u.def.section->output_section != NULL)
35702159047fSniklas {
35712159047fSniklas bfd_vma val;
35722159047fSniklas
35732159047fSniklas val = (h->u.def.value
35742159047fSniklas + bfd_get_section_vma (output_bfd,
35752159047fSniklas h->u.def.section->output_section)
35762159047fSniklas + h->u.def.section->output_offset);
35772159047fSniklas if (! bfd_set_start_address (output_bfd, val))
3578c074d1c9Sdrahn einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3579b305b0f1Sespie }
3580b305b0f1Sespie else
3581b305b0f1Sespie {
3582b305b0f1Sespie bfd_vma val;
3583b55d4692Sfgsch const char *send;
3584b305b0f1Sespie
3585b305b0f1Sespie /* We couldn't find the entry symbol. Try parsing it as a
3586b305b0f1Sespie number. */
3587c074d1c9Sdrahn val = bfd_scan_vma (entry_symbol.name, &send, 0);
3588b305b0f1Sespie if (*send == '\0')
3589b305b0f1Sespie {
3590b305b0f1Sespie if (! bfd_set_start_address (output_bfd, val))
3591b305b0f1Sespie einfo (_("%P%F: can't set start address\n"));
35922159047fSniklas }
35932159047fSniklas else
35942159047fSniklas {
35952159047fSniklas asection *ts;
35962159047fSniklas
3597b305b0f1Sespie /* Can't find the entry symbol, and it's not a number. Use
3598b305b0f1Sespie the first address in the text section. */
3599c074d1c9Sdrahn ts = bfd_get_section_by_name (output_bfd, entry_section);
3600*007c2a45Smiod if (ts != NULL)
36012159047fSniklas {
36022159047fSniklas if (warn)
3603b305b0f1Sespie einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3604c074d1c9Sdrahn entry_symbol.name,
3605c074d1c9Sdrahn bfd_get_section_vma (output_bfd, ts));
36062159047fSniklas if (! bfd_set_start_address (output_bfd,
3607b305b0f1Sespie bfd_get_section_vma (output_bfd,
3608b305b0f1Sespie ts)))
3609b305b0f1Sespie einfo (_("%P%F: can't set start address\n"));
36102159047fSniklas }
36112159047fSniklas else
36122159047fSniklas {
36132159047fSniklas if (warn)
3614b305b0f1Sespie einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3615c074d1c9Sdrahn entry_symbol.name);
36162159047fSniklas }
36172159047fSniklas }
36182159047fSniklas }
3619*007c2a45Smiod
3620*007c2a45Smiod bfd_hash_table_free (&lang_definedness_table);
3621b305b0f1Sespie }
3622b305b0f1Sespie
3623b305b0f1Sespie /* This is a small function used when we want to ignore errors from
3624b305b0f1Sespie BFD. */
3625b305b0f1Sespie
3626b305b0f1Sespie static void
ignore_bfd_errors(const char * s ATTRIBUTE_UNUSED,...)3627b305b0f1Sespie ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3628b305b0f1Sespie {
3629b305b0f1Sespie /* Don't do anything. */
3630b305b0f1Sespie }
36312159047fSniklas
36322159047fSniklas /* Check that the architecture of all the input files is compatible
36332159047fSniklas with the output file. Also call the backend to let it do any
36342159047fSniklas other checking that is needed. */
36352159047fSniklas
36362159047fSniklas static void
lang_check(void)3637*007c2a45Smiod lang_check (void)
36382159047fSniklas {
36392159047fSniklas lang_statement_union_type *file;
36402159047fSniklas bfd *input_bfd;
3641b55d4692Sfgsch const bfd_arch_info_type *compatible;
36422159047fSniklas
3643*007c2a45Smiod for (file = file_chain.head; file != NULL; file = file->input_statement.next)
36442159047fSniklas {
36452159047fSniklas input_bfd = file->input_statement.the_bfd;
3646c074d1c9Sdrahn compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3647c074d1c9Sdrahn command_line.accept_unknown_input_arch);
3648c074d1c9Sdrahn
3649c074d1c9Sdrahn /* In general it is not possible to perform a relocatable
3650c074d1c9Sdrahn link between differing object formats when the input
3651c074d1c9Sdrahn file has relocations, because the relocations in the
3652c074d1c9Sdrahn input format may not have equivalent representations in
3653c074d1c9Sdrahn the output format (and besides BFD does not translate
3654c074d1c9Sdrahn relocs for other link purposes than a final link). */
3655*007c2a45Smiod if ((link_info.relocatable || link_info.emitrelocations)
3656c074d1c9Sdrahn && (compatible == NULL
3657c074d1c9Sdrahn || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3658c074d1c9Sdrahn && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3659c074d1c9Sdrahn {
3660c074d1c9Sdrahn einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3661c074d1c9Sdrahn bfd_get_target (input_bfd), input_bfd,
3662c074d1c9Sdrahn bfd_get_target (output_bfd), output_bfd);
3663c074d1c9Sdrahn /* einfo with %F exits. */
3664c074d1c9Sdrahn }
3665c074d1c9Sdrahn
36662159047fSniklas if (compatible == NULL)
3667b305b0f1Sespie {
3668b305b0f1Sespie if (command_line.warn_mismatch)
3669b305b0f1Sespie einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
36702159047fSniklas bfd_printable_name (input_bfd), input_bfd,
36712159047fSniklas bfd_printable_name (output_bfd));
3672b305b0f1Sespie }
3673b55d4692Sfgsch else if (bfd_count_sections (input_bfd))
3674b305b0f1Sespie {
3675b55d4692Sfgsch /* If the input bfd has no contents, it shouldn't set the
3676b55d4692Sfgsch private data of the output bfd. */
3677b55d4692Sfgsch
3678b305b0f1Sespie bfd_error_handler_type pfn = NULL;
3679b305b0f1Sespie
3680b305b0f1Sespie /* If we aren't supposed to warn about mismatched input
3681b305b0f1Sespie files, temporarily set the BFD error handler to a
3682b305b0f1Sespie function which will do nothing. We still want to call
3683b305b0f1Sespie bfd_merge_private_bfd_data, since it may set up
3684b305b0f1Sespie information which is needed in the output file. */
3685b305b0f1Sespie if (! command_line.warn_mismatch)
3686b305b0f1Sespie pfn = bfd_set_error_handler (ignore_bfd_errors);
3687b305b0f1Sespie if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3688b305b0f1Sespie {
3689b305b0f1Sespie if (command_line.warn_mismatch)
3690*007c2a45Smiod einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3691b305b0f1Sespie input_bfd);
3692b305b0f1Sespie }
3693b305b0f1Sespie if (! command_line.warn_mismatch)
3694b305b0f1Sespie bfd_set_error_handler (pfn);
3695b305b0f1Sespie }
36962159047fSniklas }
36972159047fSniklas }
36982159047fSniklas
36992159047fSniklas /* Look through all the global common symbols and attach them to the
37002159047fSniklas correct section. The -sort-common command line switch may be used
37012159047fSniklas to roughly sort the entries by size. */
37022159047fSniklas
37032159047fSniklas static void
lang_common(void)3704*007c2a45Smiod lang_common (void)
37052159047fSniklas {
3706c074d1c9Sdrahn if (command_line.inhibit_common_definition)
3707c074d1c9Sdrahn return;
3708*007c2a45Smiod if (link_info.relocatable
37092159047fSniklas && ! command_line.force_common_definition)
37102159047fSniklas return;
37112159047fSniklas
37122159047fSniklas if (! config.sort_common)
3713*007c2a45Smiod bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
37142159047fSniklas else
37152159047fSniklas {
37162159047fSniklas int power;
37172159047fSniklas
37182159047fSniklas for (power = 4; power >= 0; power--)
3719*007c2a45Smiod bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
37202159047fSniklas }
37212159047fSniklas }
37222159047fSniklas
37232159047fSniklas /* Place one common symbol in the correct section. */
37242159047fSniklas
3725c074d1c9Sdrahn static bfd_boolean
lang_one_common(struct bfd_link_hash_entry * h,void * info)3726*007c2a45Smiod lang_one_common (struct bfd_link_hash_entry *h, void *info)
37272159047fSniklas {
37282159047fSniklas unsigned int power_of_two;
37292159047fSniklas bfd_vma size;
37302159047fSniklas asection *section;
37312159047fSniklas
37322159047fSniklas if (h->type != bfd_link_hash_common)
3733c074d1c9Sdrahn return TRUE;
37342159047fSniklas
37352159047fSniklas size = h->u.c.size;
37362159047fSniklas power_of_two = h->u.c.p->alignment_power;
37372159047fSniklas
37382159047fSniklas if (config.sort_common
37392159047fSniklas && power_of_two < (unsigned int) *(int *) info)
3740c074d1c9Sdrahn return TRUE;
37412159047fSniklas
37422159047fSniklas section = h->u.c.p->section;
37432159047fSniklas
3744*007c2a45Smiod /* Increase the size of the section to align the common sym. */
3745*007c2a45Smiod section->_cooked_size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3746*007c2a45Smiod section->_cooked_size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
37472159047fSniklas
37482159047fSniklas /* Adjust the alignment if necessary. */
37492159047fSniklas if (power_of_two > section->alignment_power)
37502159047fSniklas section->alignment_power = power_of_two;
37512159047fSniklas
37522159047fSniklas /* Change the symbol from common to defined. */
37532159047fSniklas h->type = bfd_link_hash_defined;
37542159047fSniklas h->u.def.section = section;
3755b305b0f1Sespie h->u.def.value = section->_cooked_size;
37562159047fSniklas
37572159047fSniklas /* Increase the size of the section. */
3758b305b0f1Sespie section->_cooked_size += size;
37592159047fSniklas
3760b305b0f1Sespie /* Make sure the section is allocated in memory, and make sure that
3761b305b0f1Sespie it is no longer a common section. */
37622159047fSniklas section->flags |= SEC_ALLOC;
3763b305b0f1Sespie section->flags &= ~SEC_IS_COMMON;
37642159047fSniklas
37652159047fSniklas if (config.map_file != NULL)
3766191aa565Sniklas {
3767c074d1c9Sdrahn static bfd_boolean header_printed;
3768191aa565Sniklas int len;
3769191aa565Sniklas char *name;
3770191aa565Sniklas char buf[50];
3771191aa565Sniklas
3772191aa565Sniklas if (! header_printed)
3773191aa565Sniklas {
3774b305b0f1Sespie minfo (_("\nAllocating common symbols\n"));
3775b305b0f1Sespie minfo (_("Common symbol size file\n\n"));
3776c074d1c9Sdrahn header_printed = TRUE;
3777191aa565Sniklas }
3778191aa565Sniklas
3779191aa565Sniklas name = demangle (h->root.string);
3780191aa565Sniklas minfo ("%s", name);
3781191aa565Sniklas len = strlen (name);
3782191aa565Sniklas free (name);
3783191aa565Sniklas
3784191aa565Sniklas if (len >= 19)
3785191aa565Sniklas {
3786191aa565Sniklas print_nl ();
3787191aa565Sniklas len = 0;
3788191aa565Sniklas }
3789191aa565Sniklas while (len < 20)
3790191aa565Sniklas {
3791191aa565Sniklas print_space ();
3792191aa565Sniklas ++len;
3793191aa565Sniklas }
3794191aa565Sniklas
3795191aa565Sniklas minfo ("0x");
3796191aa565Sniklas if (size <= 0xffffffff)
3797191aa565Sniklas sprintf (buf, "%lx", (unsigned long) size);
3798191aa565Sniklas else
3799191aa565Sniklas sprintf_vma (buf, size);
3800191aa565Sniklas minfo ("%s", buf);
3801191aa565Sniklas len = strlen (buf);
3802191aa565Sniklas
3803191aa565Sniklas while (len < 16)
3804191aa565Sniklas {
3805191aa565Sniklas print_space ();
3806191aa565Sniklas ++len;
3807191aa565Sniklas }
3808191aa565Sniklas
3809191aa565Sniklas minfo ("%B\n", section->owner);
3810191aa565Sniklas }
38112159047fSniklas
3812c074d1c9Sdrahn return TRUE;
38132159047fSniklas }
38142159047fSniklas
3815b55d4692Sfgsch /* Run through the input files and ensure that every input section has
3816b55d4692Sfgsch somewhere to go. If one is found without a destination then create
3817b55d4692Sfgsch an input request and place it into the statement tree. */
38182159047fSniklas
38192159047fSniklas static void
lang_place_orphans(void)3820*007c2a45Smiod lang_place_orphans (void)
38212159047fSniklas {
3822b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (file)
38232159047fSniklas {
38242159047fSniklas asection *s;
38252159047fSniklas
3826*007c2a45Smiod for (s = file->the_bfd->sections; s != NULL; s = s->next)
38272159047fSniklas {
3828*007c2a45Smiod if (s->output_section == NULL)
38292159047fSniklas {
3830c074d1c9Sdrahn /* This section of the file is not attached, root
3831b55d4692Sfgsch around for a sensible place for it to go. */
38322159047fSniklas
38332159047fSniklas if (file->just_syms_flag)
38342159047fSniklas {
3835c074d1c9Sdrahn abort ();
38362159047fSniklas }
38370c6d0228Sniklas else if (strcmp (s->name, "COMMON") == 0)
38382159047fSniklas {
38390c6d0228Sniklas /* This is a lonely common section which must have
38400c6d0228Sniklas come from an archive. We attach to the section
38410c6d0228Sniklas with the wildcard. */
3842*007c2a45Smiod if (! link_info.relocatable
38430c6d0228Sniklas || command_line.force_common_definition)
38442159047fSniklas {
38450c6d0228Sniklas if (default_common_section == NULL)
38462159047fSniklas {
38470c6d0228Sniklas #if 0
38480c6d0228Sniklas /* This message happens when using the
38490c6d0228Sniklas svr3.ifile linker script, so I have
38500c6d0228Sniklas disabled it. */
3851b305b0f1Sespie info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
38520c6d0228Sniklas #endif
38532159047fSniklas default_common_section =
38542159047fSniklas lang_output_section_statement_lookup (".bss");
38552159047fSniklas
38562159047fSniklas }
3857c074d1c9Sdrahn lang_add_section (&default_common_section->children, s,
38582159047fSniklas default_common_section, file);
38592159047fSniklas }
38602159047fSniklas }
38612159047fSniklas else if (ldemul_place_orphan (file, s))
38622159047fSniklas ;
38632159047fSniklas else
38642159047fSniklas {
3865c074d1c9Sdrahn lang_output_section_statement_type *os;
38662159047fSniklas
3867c074d1c9Sdrahn os = lang_output_section_statement_lookup (s->name);
3868c074d1c9Sdrahn lang_add_section (&os->children, s, os, file);
38692159047fSniklas }
38702159047fSniklas }
38712159047fSniklas }
38722159047fSniklas }
38732159047fSniklas }
38742159047fSniklas
38752159047fSniklas void
lang_set_flags(lang_memory_region_type * ptr,const char * flags,int invert)3876*007c2a45Smiod lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
38772159047fSniklas {
3878b305b0f1Sespie flagword *ptr_flags;
38792159047fSniklas
3880b305b0f1Sespie ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
38812159047fSniklas while (*flags)
38822159047fSniklas {
38832159047fSniklas switch (*flags)
38842159047fSniklas {
3885b305b0f1Sespie case 'A': case 'a':
3886b305b0f1Sespie *ptr_flags |= SEC_ALLOC;
38872159047fSniklas break;
3888b305b0f1Sespie
3889b305b0f1Sespie case 'R': case 'r':
3890b305b0f1Sespie *ptr_flags |= SEC_READONLY;
38912159047fSniklas break;
3892b305b0f1Sespie
3893b305b0f1Sespie case 'W': case 'w':
3894b305b0f1Sespie *ptr_flags |= SEC_DATA;
38952159047fSniklas break;
3896b305b0f1Sespie
3897b305b0f1Sespie case 'X': case 'x':
3898b305b0f1Sespie *ptr_flags |= SEC_CODE;
38992159047fSniklas break;
3900b305b0f1Sespie
3901b305b0f1Sespie case 'L': case 'l':
3902b305b0f1Sespie case 'I': case 'i':
3903b305b0f1Sespie *ptr_flags |= SEC_LOAD;
3904b305b0f1Sespie break;
3905b305b0f1Sespie
39062159047fSniklas default:
3907b305b0f1Sespie einfo (_("%P%F: invalid syntax in flags\n"));
39082159047fSniklas break;
39092159047fSniklas }
39102159047fSniklas flags++;
39112159047fSniklas }
39122159047fSniklas }
39132159047fSniklas
39142159047fSniklas /* Call a function on each input file. This function will be called
39152159047fSniklas on an archive, but not on the elements. */
39162159047fSniklas
39172159047fSniklas void
lang_for_each_input_file(void (* func)(lang_input_statement_type *))3918*007c2a45Smiod lang_for_each_input_file (void (*func) (lang_input_statement_type *))
39192159047fSniklas {
39202159047fSniklas lang_input_statement_type *f;
39212159047fSniklas
39222159047fSniklas for (f = (lang_input_statement_type *) input_file_chain.head;
39232159047fSniklas f != NULL;
39242159047fSniklas f = (lang_input_statement_type *) f->next_real_file)
39252159047fSniklas func (f);
39262159047fSniklas }
39272159047fSniklas
39282159047fSniklas /* Call a function on each file. The function will be called on all
39292159047fSniklas the elements of an archive which are included in the link, but will
39302159047fSniklas not be called on the archive file itself. */
39312159047fSniklas
39322159047fSniklas void
lang_for_each_file(void (* func)(lang_input_statement_type *))3933*007c2a45Smiod lang_for_each_file (void (*func) (lang_input_statement_type *))
39342159047fSniklas {
3935b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (f)
39362159047fSniklas {
39372159047fSniklas func (f);
39382159047fSniklas }
39392159047fSniklas }
39402159047fSniklas
39412159047fSniklas void
ldlang_add_file(lang_input_statement_type * entry)3942*007c2a45Smiod ldlang_add_file (lang_input_statement_type *entry)
39432159047fSniklas {
39442159047fSniklas bfd **pp;
39452159047fSniklas
39462159047fSniklas lang_statement_append (&file_chain,
39472159047fSniklas (lang_statement_union_type *) entry,
39482159047fSniklas &entry->next);
39492159047fSniklas
39502159047fSniklas /* The BFD linker needs to have a list of all input BFDs involved in
39512159047fSniklas a link. */
3952*007c2a45Smiod ASSERT (entry->the_bfd->link_next == NULL);
39532159047fSniklas ASSERT (entry->the_bfd != output_bfd);
3954*007c2a45Smiod for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
39552159047fSniklas ;
39562159047fSniklas *pp = entry->the_bfd;
3957*007c2a45Smiod entry->the_bfd->usrdata = entry;
39582159047fSniklas bfd_set_gp_size (entry->the_bfd, g_switch_value);
3959191aa565Sniklas
3960191aa565Sniklas /* Look through the sections and check for any which should not be
3961191aa565Sniklas included in the link. We need to do this now, so that we can
3962191aa565Sniklas notice when the backend linker tries to report multiple
3963191aa565Sniklas definition errors for symbols which are in sections we aren't
3964191aa565Sniklas going to link. FIXME: It might be better to entirely ignore
3965191aa565Sniklas symbols which are defined in sections which are going to be
3966191aa565Sniklas discarded. This would require modifying the backend linker for
3967191aa565Sniklas each backend which might set the SEC_LINK_ONCE flag. If we do
3968191aa565Sniklas this, we should probably handle SEC_EXCLUDE in the same way. */
3969191aa565Sniklas
3970*007c2a45Smiod bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
39712159047fSniklas }
39722159047fSniklas
39732159047fSniklas void
lang_add_output(const char * name,int from_script)3974*007c2a45Smiod lang_add_output (const char *name, int from_script)
39752159047fSniklas {
39762159047fSniklas /* Make -o on command line override OUTPUT in script. */
3977c074d1c9Sdrahn if (!had_output_filename || !from_script)
39782159047fSniklas {
39792159047fSniklas output_filename = name;
3980c074d1c9Sdrahn had_output_filename = TRUE;
39812159047fSniklas }
39822159047fSniklas }
39832159047fSniklas
39842159047fSniklas static lang_output_section_statement_type *current_section;
39852159047fSniklas
39862159047fSniklas static int
topower(int x)3987*007c2a45Smiod topower (int x)
39882159047fSniklas {
39892159047fSniklas unsigned int i = 1;
39902159047fSniklas int l;
39912159047fSniklas
39922159047fSniklas if (x < 0)
39932159047fSniklas return -1;
39942159047fSniklas
39952159047fSniklas for (l = 0; l < 32; l++)
39962159047fSniklas {
39972159047fSniklas if (i >= (unsigned int) x)
39982159047fSniklas return l;
39992159047fSniklas i <<= 1;
40002159047fSniklas }
40012159047fSniklas
40022159047fSniklas return 0;
40032159047fSniklas }
40040c6d0228Sniklas
40056978ef19Sespie lang_output_section_statement_type *
lang_enter_output_section_statement(const char * output_section_statement_name,etree_type * address_exp,enum section_type sectype,etree_type * align,etree_type * subalign,etree_type * ebase)4006*007c2a45Smiod lang_enter_output_section_statement (const char *output_section_statement_name,
4007*007c2a45Smiod etree_type *address_exp,
4008*007c2a45Smiod enum section_type sectype,
4009*007c2a45Smiod etree_type *align,
4010*007c2a45Smiod etree_type *subalign,
4011*007c2a45Smiod etree_type *ebase)
40122159047fSniklas {
40132159047fSniklas lang_output_section_statement_type *os;
40142159047fSniklas
40152159047fSniklas current_section =
40162159047fSniklas os =
40172159047fSniklas lang_output_section_statement_lookup (output_section_statement_name);
40182159047fSniklas
4019b55d4692Sfgsch /* Add this statement to tree. */
4020b55d4692Sfgsch #if 0
4021b55d4692Sfgsch add_statement (lang_output_section_statement_enum,
4022b55d4692Sfgsch output_section_statement);
4023b55d4692Sfgsch #endif
4024b55d4692Sfgsch /* Make next things chain into subchain of this. */
40252159047fSniklas
4026*007c2a45Smiod if (os->addr_tree == NULL)
40272159047fSniklas {
4028b55d4692Sfgsch os->addr_tree = address_exp;
40292159047fSniklas }
40300c6d0228Sniklas os->sectype = sectype;
40310c6d0228Sniklas if (sectype != noload_section)
40320c6d0228Sniklas os->flags = SEC_NO_FLAGS;
40332159047fSniklas else
40340c6d0228Sniklas os->flags = SEC_NEVER_LOAD;
4035*007c2a45Smiod os->block_value = 1;
40362159047fSniklas stat_ptr = &os->children;
40372159047fSniklas
4038b55d4692Sfgsch os->subsection_alignment =
4039b55d4692Sfgsch topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4040b55d4692Sfgsch os->section_alignment =
4041b55d4692Sfgsch topower (exp_get_value_int (align, -1, "section alignment", 0));
40422159047fSniklas
40432159047fSniklas os->load_base = ebase;
40446978ef19Sespie return os;
40452159047fSniklas }
40462159047fSniklas
40472159047fSniklas void
lang_final(void)4048*007c2a45Smiod lang_final (void)
40492159047fSniklas {
40502159047fSniklas lang_output_statement_type *new =
40512159047fSniklas new_stat (lang_output_statement, stat_ptr);
40522159047fSniklas
40532159047fSniklas new->name = output_filename;
40542159047fSniklas }
40552159047fSniklas
4056b55d4692Sfgsch /* Reset the current counters in the regions. */
4057b55d4692Sfgsch
4058c074d1c9Sdrahn void
lang_reset_memory_regions(void)4059*007c2a45Smiod lang_reset_memory_regions (void)
40602159047fSniklas {
40612159047fSniklas lang_memory_region_type *p = lang_memory_region_list;
4062c074d1c9Sdrahn asection *o;
40632159047fSniklas
4064*007c2a45Smiod for (p = lang_memory_region_list; p != NULL; p = p->next)
40652159047fSniklas {
40662159047fSniklas p->old_length = (bfd_size_type) (p->current - p->origin);
40672159047fSniklas p->current = p->origin;
40682159047fSniklas }
4069c074d1c9Sdrahn
4070c074d1c9Sdrahn for (o = output_bfd->sections; o != NULL; o = o->next)
4071c074d1c9Sdrahn o->_raw_size = 0;
40722159047fSniklas }
40732159047fSniklas
4074c074d1c9Sdrahn /* If the wild pattern was marked KEEP, the member sections
4075c074d1c9Sdrahn should be as well. */
4076b305b0f1Sespie
4077b305b0f1Sespie static void
gc_section_callback(lang_wild_statement_type * ptr,struct wildcard_list * sec ATTRIBUTE_UNUSED,asection * section,lang_input_statement_type * file ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)4078*007c2a45Smiod gc_section_callback (lang_wild_statement_type *ptr,
4079*007c2a45Smiod struct wildcard_list *sec ATTRIBUTE_UNUSED,
4080*007c2a45Smiod asection *section,
4081*007c2a45Smiod lang_input_statement_type *file ATTRIBUTE_UNUSED,
4082*007c2a45Smiod void *data ATTRIBUTE_UNUSED)
4083b305b0f1Sespie {
4084b305b0f1Sespie if (ptr->keep_sections)
4085b305b0f1Sespie section->flags |= SEC_KEEP;
4086b305b0f1Sespie }
4087b305b0f1Sespie
4088c074d1c9Sdrahn /* Handle a wild statement, marking it against GC. */
4089b305b0f1Sespie
4090b305b0f1Sespie static void
lang_gc_wild(lang_wild_statement_type * s)4091*007c2a45Smiod lang_gc_wild (lang_wild_statement_type *s)
4092b305b0f1Sespie {
4093c074d1c9Sdrahn walk_wild (s, gc_section_callback, NULL);
4094b305b0f1Sespie }
4095b305b0f1Sespie
4096b305b0f1Sespie /* Iterate over sections marking them against GC. */
4097b305b0f1Sespie
4098b305b0f1Sespie static void
lang_gc_sections_1(lang_statement_union_type * s)4099*007c2a45Smiod lang_gc_sections_1 (lang_statement_union_type *s)
4100b305b0f1Sespie {
4101*007c2a45Smiod for (; s != NULL; s = s->header.next)
4102b305b0f1Sespie {
4103b305b0f1Sespie switch (s->header.type)
4104b305b0f1Sespie {
4105b305b0f1Sespie case lang_wild_statement_enum:
4106c074d1c9Sdrahn lang_gc_wild (&s->wild_statement);
4107b305b0f1Sespie break;
4108b305b0f1Sespie case lang_constructors_statement_enum:
4109b305b0f1Sespie lang_gc_sections_1 (constructor_list.head);
4110b305b0f1Sespie break;
4111b305b0f1Sespie case lang_output_section_statement_enum:
4112b305b0f1Sespie lang_gc_sections_1 (s->output_section_statement.children.head);
4113b305b0f1Sespie break;
4114b305b0f1Sespie case lang_group_statement_enum:
4115b305b0f1Sespie lang_gc_sections_1 (s->group_statement.children.head);
4116b305b0f1Sespie break;
4117b305b0f1Sespie default:
4118b305b0f1Sespie break;
4119b305b0f1Sespie }
4120b305b0f1Sespie }
4121b305b0f1Sespie }
4122b305b0f1Sespie
4123b305b0f1Sespie static void
lang_gc_sections(void)4124*007c2a45Smiod lang_gc_sections (void)
4125b305b0f1Sespie {
4126b305b0f1Sespie struct bfd_link_hash_entry *h;
4127c074d1c9Sdrahn ldlang_undef_chain_list_type *ulist;
4128b305b0f1Sespie
4129b305b0f1Sespie /* Keep all sections so marked in the link script. */
4130b305b0f1Sespie
4131b305b0f1Sespie lang_gc_sections_1 (statement_list.head);
4132b305b0f1Sespie
4133c074d1c9Sdrahn /* Keep all sections containing symbols undefined on the command-line,
4134c074d1c9Sdrahn and the section containing the entry symbol. */
4135b305b0f1Sespie
4136c074d1c9Sdrahn for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4137b305b0f1Sespie {
4138b305b0f1Sespie h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4139c074d1c9Sdrahn FALSE, FALSE, FALSE);
4140b305b0f1Sespie
4141*007c2a45Smiod if (h != NULL
4142b305b0f1Sespie && (h->type == bfd_link_hash_defined
4143b305b0f1Sespie || h->type == bfd_link_hash_defweak)
4144b305b0f1Sespie && ! bfd_is_abs_section (h->u.def.section))
4145b305b0f1Sespie {
4146b305b0f1Sespie h->u.def.section->flags |= SEC_KEEP;
4147b305b0f1Sespie }
4148b305b0f1Sespie }
4149b305b0f1Sespie
4150b305b0f1Sespie bfd_gc_sections (output_bfd, &link_info);
4151b305b0f1Sespie }
4152b305b0f1Sespie
41532159047fSniklas void
lang_process(void)4154*007c2a45Smiod lang_process (void)
41552159047fSniklas {
41562159047fSniklas lang_reasonable_defaults ();
41572159047fSniklas current_target = default_target;
41582159047fSniklas
4159b55d4692Sfgsch /* Open the output file. */
4160b55d4692Sfgsch lang_for_each_statement (ldlang_open_output);
4161*007c2a45Smiod init_opb ();
41622159047fSniklas
41632159047fSniklas ldemul_create_output_section_statements ();
41642159047fSniklas
4165b55d4692Sfgsch /* Add to the hash table all undefineds on the command line. */
41662159047fSniklas lang_place_undefineds ();
41672159047fSniklas
4168b305b0f1Sespie already_linked_table_init ();
4169b305b0f1Sespie
4170b55d4692Sfgsch /* Create a bfd for each input file. */
41712159047fSniklas current_target = default_target;
4172c074d1c9Sdrahn open_input_bfds (statement_list.head, FALSE);
4173c074d1c9Sdrahn
4174c074d1c9Sdrahn link_info.gc_sym_list = &entry_symbol;
4175c074d1c9Sdrahn if (entry_symbol.name == NULL)
4176c074d1c9Sdrahn link_info.gc_sym_list = ldlang_undef_chain_list_head;
41772159047fSniklas
41782159047fSniklas ldemul_after_open ();
41792159047fSniklas
4180b305b0f1Sespie already_linked_table_free ();
4181b305b0f1Sespie
4182b305b0f1Sespie /* Make sure that we're not mixing architectures. We call this
4183b305b0f1Sespie after all the input files have been opened, but before we do any
4184b305b0f1Sespie other processing, so that any operations merge_private_bfd_data
4185b305b0f1Sespie does on the output file will be known during the rest of the
4186b305b0f1Sespie link. */
4187b305b0f1Sespie lang_check ();
4188b305b0f1Sespie
4189b305b0f1Sespie /* Handle .exports instead of a version script if we're told to do so. */
4190b305b0f1Sespie if (command_line.version_exports_section)
4191b305b0f1Sespie lang_do_version_exports_section ();
4192b305b0f1Sespie
41932159047fSniklas /* Build all sets based on the information gathered from the input
41942159047fSniklas files. */
41952159047fSniklas ldctor_build_sets ();
41962159047fSniklas
4197b305b0f1Sespie /* Remove unreferenced sections if asked to. */
4198b305b0f1Sespie if (command_line.gc_sections)
4199b305b0f1Sespie lang_gc_sections ();
4200b305b0f1Sespie
4201c074d1c9Sdrahn /* If there were any SEC_MERGE sections, finish their merging, so that
4202c074d1c9Sdrahn section sizes can be computed. This has to be done after GC of sections,
4203c074d1c9Sdrahn so that GCed sections are not merged, but before assigning output
4204c074d1c9Sdrahn sections, since removing whole input sections is hard then. */
4205c074d1c9Sdrahn bfd_merge_sections (output_bfd, &link_info);
4206c074d1c9Sdrahn
4207b55d4692Sfgsch /* Size up the common data. */
42082159047fSniklas lang_common ();
42092159047fSniklas
4210b305b0f1Sespie /* Run through the contours of the script and attach input sections
4211b55d4692Sfgsch to the correct output sections. */
4212*007c2a45Smiod map_input_to_output_sections (statement_list.head, NULL, NULL);
42132159047fSniklas
4214b55d4692Sfgsch /* Find any sections not attached explicitly and handle them. */
42152159047fSniklas lang_place_orphans ();
42162159047fSniklas
4217*007c2a45Smiod if (! link_info.relocatable)
4218c074d1c9Sdrahn {
4219c074d1c9Sdrahn /* Look for a text section and set the readonly attribute in it. */
4220c074d1c9Sdrahn asection *found = bfd_get_section_by_name (output_bfd, ".text");
4221c074d1c9Sdrahn
4222*007c2a45Smiod if (found != NULL)
4223c074d1c9Sdrahn {
4224c074d1c9Sdrahn if (config.text_read_only)
4225c074d1c9Sdrahn found->flags |= SEC_READONLY;
4226c074d1c9Sdrahn else
4227c074d1c9Sdrahn found->flags &= ~SEC_READONLY;
4228c074d1c9Sdrahn }
4229c074d1c9Sdrahn }
4230c074d1c9Sdrahn
4231c074d1c9Sdrahn /* Do anything special before sizing sections. This is where ELF
4232c074d1c9Sdrahn and other back-ends size dynamic sections. */
42332159047fSniklas ldemul_before_allocation ();
42342159047fSniklas
4235*007c2a45Smiod if (!link_info.relocatable)
4236c074d1c9Sdrahn strip_excluded_output_sections ();
4237c074d1c9Sdrahn
4238191aa565Sniklas /* We must record the program headers before we try to fix the
4239191aa565Sniklas section positions, since they will affect SIZEOF_HEADERS. */
4240191aa565Sniklas lang_record_phdrs ();
4241191aa565Sniklas
4242c074d1c9Sdrahn /* Size up the sections. */
4243*007c2a45Smiod lang_size_sections (statement_list.head, abs_output_section,
4244*007c2a45Smiod &statement_list.head, 0, 0, NULL,
4245c074d1c9Sdrahn command_line.relax ? FALSE : TRUE);
4246c074d1c9Sdrahn
4247b55d4692Sfgsch /* Now run around and relax if we can. */
42482159047fSniklas if (command_line.relax)
42492159047fSniklas {
42502159047fSniklas /* Keep relaxing until bfd_relax_section gives up. */
4251c074d1c9Sdrahn bfd_boolean relax_again;
4252c074d1c9Sdrahn
42532159047fSniklas do
42542159047fSniklas {
4255c074d1c9Sdrahn relax_again = FALSE;
42562159047fSniklas
4257b305b0f1Sespie /* Note: pe-dll.c does something like this also. If you find
4258b305b0f1Sespie you need to change this code, you probably need to change
4259b305b0f1Sespie pe-dll.c also. DJ */
4260b305b0f1Sespie
42612159047fSniklas /* Do all the assignments with our current guesses as to
42622159047fSniklas section sizes. */
4263*007c2a45Smiod lang_do_assignments (statement_list.head, abs_output_section,
4264*007c2a45Smiod NULL, 0);
4265*007c2a45Smiod
4266*007c2a45Smiod /* We must do this after lang_do_assignments, because it uses
4267*007c2a45Smiod _raw_size. */
4268*007c2a45Smiod lang_reset_memory_regions ();
42692159047fSniklas
42702159047fSniklas /* Perform another relax pass - this time we know where the
4271c074d1c9Sdrahn globals are, so can make a better guess. */
4272*007c2a45Smiod lang_size_sections (statement_list.head, abs_output_section,
4273*007c2a45Smiod &statement_list.head, 0, 0, &relax_again, FALSE);
4274c074d1c9Sdrahn
4275c074d1c9Sdrahn /* If the normal relax is done and the relax finalize pass
4276c074d1c9Sdrahn is not performed yet, we perform another relax pass. */
4277*007c2a45Smiod if (!relax_again && link_info.need_relax_finalize)
4278c074d1c9Sdrahn {
4279*007c2a45Smiod link_info.need_relax_finalize = FALSE;
4280c074d1c9Sdrahn relax_again = TRUE;
4281c074d1c9Sdrahn }
42822159047fSniklas }
42832159047fSniklas while (relax_again);
4284c074d1c9Sdrahn
4285c074d1c9Sdrahn /* Final extra sizing to report errors. */
4286*007c2a45Smiod lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4287c074d1c9Sdrahn lang_reset_memory_regions ();
4288*007c2a45Smiod lang_size_sections (statement_list.head, abs_output_section,
4289*007c2a45Smiod &statement_list.head, 0, 0, NULL, TRUE);
42902159047fSniklas }
42912159047fSniklas
42922159047fSniklas /* See if anything special should be done now we know how big
42932159047fSniklas everything is. */
42942159047fSniklas ldemul_after_allocation ();
42952159047fSniklas
42962159047fSniklas /* Fix any .startof. or .sizeof. symbols. */
42972159047fSniklas lang_set_startof ();
42982159047fSniklas
42992159047fSniklas /* Do all the assignments, now that we know the final resting places
4300b55d4692Sfgsch of all the symbols. */
43012159047fSniklas
4302*007c2a45Smiod lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
43032159047fSniklas
4304b305b0f1Sespie /* Make sure that the section addresses make sense. */
4305*007c2a45Smiod if (! link_info.relocatable
4306b305b0f1Sespie && command_line.check_section_addresses)
4307b305b0f1Sespie lang_check_section_addresses ();
43082159047fSniklas
4309b55d4692Sfgsch /* Final stuffs. */
43102159047fSniklas
43112159047fSniklas ldemul_finish ();
43122159047fSniklas lang_finish ();
43132159047fSniklas }
43142159047fSniklas
43152159047fSniklas /* EXPORTED TO YACC */
43162159047fSniklas
43172159047fSniklas void
lang_add_wild(struct wildcard_spec * filespec,struct wildcard_list * section_list,bfd_boolean keep_sections)4318*007c2a45Smiod lang_add_wild (struct wildcard_spec *filespec,
4319*007c2a45Smiod struct wildcard_list *section_list,
4320*007c2a45Smiod bfd_boolean keep_sections)
43212159047fSniklas {
4322c074d1c9Sdrahn struct wildcard_list *curr, *next;
4323c074d1c9Sdrahn lang_wild_statement_type *new;
43242159047fSniklas
4325c074d1c9Sdrahn /* Reverse the list as the parser puts it back to front. */
4326c074d1c9Sdrahn for (curr = section_list, section_list = NULL;
4327c074d1c9Sdrahn curr != NULL;
4328c074d1c9Sdrahn section_list = curr, curr = next)
43292159047fSniklas {
4330c074d1c9Sdrahn if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4331c074d1c9Sdrahn placed_commons = TRUE;
4332c074d1c9Sdrahn
4333c074d1c9Sdrahn next = curr->next;
4334c074d1c9Sdrahn curr->next = section_list;
43352159047fSniklas }
4336c074d1c9Sdrahn
4337c074d1c9Sdrahn if (filespec != NULL && filespec->name != NULL)
43382159047fSniklas {
4339c074d1c9Sdrahn if (strcmp (filespec->name, "*") == 0)
4340c074d1c9Sdrahn filespec->name = NULL;
4341c074d1c9Sdrahn else if (! wildcardp (filespec->name))
4342c074d1c9Sdrahn lang_has_input_file = TRUE;
43432159047fSniklas }
4344c074d1c9Sdrahn
4345c074d1c9Sdrahn new = new_stat (lang_wild_statement, stat_ptr);
4346c074d1c9Sdrahn new->filename = NULL;
4347c074d1c9Sdrahn new->filenames_sorted = FALSE;
4348c074d1c9Sdrahn if (filespec != NULL)
4349c074d1c9Sdrahn {
4350c074d1c9Sdrahn new->filename = filespec->name;
4351c074d1c9Sdrahn new->filenames_sorted = filespec->sorted;
4352c074d1c9Sdrahn }
4353c074d1c9Sdrahn new->section_list = section_list;
4354b305b0f1Sespie new->keep_sections = keep_sections;
43552159047fSniklas lang_list_init (&new->children);
43562159047fSniklas }
43572159047fSniklas
43582159047fSniklas void
lang_section_start(const char * name,etree_type * address)4359*007c2a45Smiod lang_section_start (const char *name, etree_type *address)
43602159047fSniklas {
4361b55d4692Sfgsch lang_address_statement_type *ad;
43622159047fSniklas
4363b55d4692Sfgsch ad = new_stat (lang_address_statement, stat_ptr);
43642159047fSniklas ad->section_name = name;
43652159047fSniklas ad->address = address;
43662159047fSniklas }
43672159047fSniklas
43682159047fSniklas /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
43692159047fSniklas because of a -e argument on the command line, or zero if this is
43702159047fSniklas called by ENTRY in a linker script. Command line arguments take
43712159047fSniklas precedence. */
43722159047fSniklas
43732159047fSniklas void
lang_add_entry(const char * name,bfd_boolean cmdline)4374*007c2a45Smiod lang_add_entry (const char *name, bfd_boolean cmdline)
43752159047fSniklas {
4376c074d1c9Sdrahn if (entry_symbol.name == NULL
43772159047fSniklas || cmdline
43782159047fSniklas || ! entry_from_cmdline)
43792159047fSniklas {
4380c074d1c9Sdrahn entry_symbol.name = name;
43812159047fSniklas entry_from_cmdline = cmdline;
43822159047fSniklas }
43832159047fSniklas }
43842159047fSniklas
43852159047fSniklas void
lang_add_target(const char * name)4386*007c2a45Smiod lang_add_target (const char *name)
43872159047fSniklas {
43882159047fSniklas lang_target_statement_type *new = new_stat (lang_target_statement,
43892159047fSniklas stat_ptr);
43902159047fSniklas
43912159047fSniklas new->target = name;
43922159047fSniklas
43932159047fSniklas }
43942159047fSniklas
43952159047fSniklas void
lang_add_map(const char * name)4396*007c2a45Smiod lang_add_map (const char *name)
43972159047fSniklas {
43982159047fSniklas while (*name)
43992159047fSniklas {
44002159047fSniklas switch (*name)
44012159047fSniklas {
44022159047fSniklas case 'F':
4403c074d1c9Sdrahn map_option_f = TRUE;
44042159047fSniklas break;
44052159047fSniklas }
44062159047fSniklas name++;
44072159047fSniklas }
44082159047fSniklas }
44092159047fSniklas
44102159047fSniklas void
lang_add_fill(fill_type * fill)4411*007c2a45Smiod lang_add_fill (fill_type *fill)
44122159047fSniklas {
44132159047fSniklas lang_fill_statement_type *new = new_stat (lang_fill_statement,
44142159047fSniklas stat_ptr);
44152159047fSniklas
4416c074d1c9Sdrahn new->fill = fill;
44172159047fSniklas }
44182159047fSniklas
44192159047fSniklas void
lang_add_data(int type,union etree_union * exp)4420*007c2a45Smiod lang_add_data (int type, union etree_union *exp)
44212159047fSniklas {
44222159047fSniklas
44232159047fSniklas lang_data_statement_type *new = new_stat (lang_data_statement,
44242159047fSniklas stat_ptr);
44252159047fSniklas
44262159047fSniklas new->exp = exp;
44272159047fSniklas new->type = type;
44282159047fSniklas
44292159047fSniklas }
44302159047fSniklas
44312159047fSniklas /* Create a new reloc statement. RELOC is the BFD relocation type to
44322159047fSniklas generate. HOWTO is the corresponding howto structure (we could
44332159047fSniklas look this up, but the caller has already done so). SECTION is the
44342159047fSniklas section to generate a reloc against, or NAME is the name of the
44352159047fSniklas symbol to generate a reloc against. Exactly one of SECTION and
44362159047fSniklas NAME must be NULL. ADDEND is an expression for the addend. */
44372159047fSniklas
44382159047fSniklas void
lang_add_reloc(bfd_reloc_code_real_type reloc,reloc_howto_type * howto,asection * section,const char * name,union etree_union * addend)4439*007c2a45Smiod lang_add_reloc (bfd_reloc_code_real_type reloc,
4440*007c2a45Smiod reloc_howto_type *howto,
4441*007c2a45Smiod asection *section,
4442*007c2a45Smiod const char *name,
4443*007c2a45Smiod union etree_union *addend)
44442159047fSniklas {
44452159047fSniklas lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
44462159047fSniklas
44472159047fSniklas p->reloc = reloc;
44482159047fSniklas p->howto = howto;
44492159047fSniklas p->section = section;
44502159047fSniklas p->name = name;
44512159047fSniklas p->addend_exp = addend;
44522159047fSniklas
44532159047fSniklas p->addend_value = 0;
44542159047fSniklas p->output_section = NULL;
44552159047fSniklas p->output_vma = 0;
44562159047fSniklas }
44572159047fSniklas
4458b305b0f1Sespie lang_assignment_statement_type *
lang_add_assignment(etree_type * exp)4459*007c2a45Smiod lang_add_assignment (etree_type *exp)
44602159047fSniklas {
44612159047fSniklas lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
44622159047fSniklas stat_ptr);
44632159047fSniklas
44642159047fSniklas new->exp = exp;
4465b305b0f1Sespie return new;
44662159047fSniklas }
44672159047fSniklas
44682159047fSniklas void
lang_add_attribute(enum statement_enum attribute)4469*007c2a45Smiod lang_add_attribute (enum statement_enum attribute)
44702159047fSniklas {
44712159047fSniklas new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
44722159047fSniklas }
44732159047fSniklas
44742159047fSniklas void
lang_startup(const char * name)4475*007c2a45Smiod lang_startup (const char *name)
44762159047fSniklas {
4477*007c2a45Smiod if (startup_file != NULL)
44782159047fSniklas {
4479b305b0f1Sespie einfo (_("%P%Fmultiple STARTUP files\n"));
44802159047fSniklas }
44812159047fSniklas first_file->filename = name;
44822159047fSniklas first_file->local_sym_name = name;
4483c074d1c9Sdrahn first_file->real = TRUE;
44842159047fSniklas
44852159047fSniklas startup_file = name;
44862159047fSniklas }
44872159047fSniklas
44882159047fSniklas void
lang_float(bfd_boolean maybe)4489*007c2a45Smiod lang_float (bfd_boolean maybe)
44902159047fSniklas {
44912159047fSniklas lang_float_flag = maybe;
44922159047fSniklas }
44932159047fSniklas
4494c074d1c9Sdrahn
4495c074d1c9Sdrahn /* Work out the load- and run-time regions from a script statement, and
4496c074d1c9Sdrahn store them in *LMA_REGION and *REGION respectively.
4497c074d1c9Sdrahn
4498*007c2a45Smiod MEMSPEC is the name of the run-time region, or the value of
4499*007c2a45Smiod DEFAULT_MEMORY_REGION if the statement didn't specify one.
4500*007c2a45Smiod LMA_MEMSPEC is the name of the load-time region, or null if the
4501*007c2a45Smiod statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4502*007c2a45Smiod had an explicit load address.
4503c074d1c9Sdrahn
4504c074d1c9Sdrahn It is an error to specify both a load region and a load address. */
4505c074d1c9Sdrahn
4506c074d1c9Sdrahn static void
lang_get_regions(lang_memory_region_type ** region,lang_memory_region_type ** lma_region,const char * memspec,const char * lma_memspec,bfd_boolean have_lma,bfd_boolean have_vma)4507*007c2a45Smiod lang_get_regions (lang_memory_region_type **region,
4508*007c2a45Smiod lang_memory_region_type **lma_region,
4509*007c2a45Smiod const char *memspec,
4510*007c2a45Smiod const char *lma_memspec,
4511*007c2a45Smiod bfd_boolean have_lma,
4512*007c2a45Smiod bfd_boolean have_vma)
4513c074d1c9Sdrahn {
4514*007c2a45Smiod *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4515c074d1c9Sdrahn
4516*007c2a45Smiod /* If no runtime region or VMA has been specified, but the load region has
4517*007c2a45Smiod been specified, then use the load region for the runtime region as well. */
4518*007c2a45Smiod if (lma_memspec != NULL
4519*007c2a45Smiod && ! have_vma
4520*007c2a45Smiod && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4521c074d1c9Sdrahn *region = *lma_region;
4522c074d1c9Sdrahn else
4523*007c2a45Smiod *region = lang_memory_region_lookup (memspec, FALSE);
4524c074d1c9Sdrahn
4525*007c2a45Smiod if (have_lma && lma_memspec != 0)
4526c074d1c9Sdrahn einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4527c074d1c9Sdrahn }
4528c074d1c9Sdrahn
45292159047fSniklas void
lang_leave_output_section_statement(fill_type * fill,const char * memspec,lang_output_section_phdr_list * phdrs,const char * lma_memspec)4530*007c2a45Smiod lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4531*007c2a45Smiod lang_output_section_phdr_list *phdrs,
4532*007c2a45Smiod const char *lma_memspec)
45332159047fSniklas {
4534c074d1c9Sdrahn lang_get_regions (¤t_section->region,
4535c074d1c9Sdrahn ¤t_section->lma_region,
4536c074d1c9Sdrahn memspec, lma_memspec,
4537*007c2a45Smiod current_section->load_base != NULL,
4538*007c2a45Smiod current_section->addr_tree != NULL);
45392159047fSniklas current_section->fill = fill;
45400c6d0228Sniklas current_section->phdrs = phdrs;
45412159047fSniklas stat_ptr = &statement_list;
45422159047fSniklas }
45432159047fSniklas
4544b55d4692Sfgsch /* Create an absolute symbol with the given name with the value of the
45452159047fSniklas address of first byte of the section named.
45462159047fSniklas
4547b55d4692Sfgsch If the symbol already exists, then do nothing. */
4548b55d4692Sfgsch
45492159047fSniklas void
lang_abs_symbol_at_beginning_of(const char * secname,const char * name)4550*007c2a45Smiod lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
45512159047fSniklas {
45522159047fSniklas struct bfd_link_hash_entry *h;
45532159047fSniklas
4554c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4555*007c2a45Smiod if (h == NULL)
4556b305b0f1Sespie einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
45572159047fSniklas
45582159047fSniklas if (h->type == bfd_link_hash_new
45592159047fSniklas || h->type == bfd_link_hash_undefined)
45602159047fSniklas {
45612159047fSniklas asection *sec;
45622159047fSniklas
45632159047fSniklas h->type = bfd_link_hash_defined;
45642159047fSniklas
45652159047fSniklas sec = bfd_get_section_by_name (output_bfd, secname);
4566*007c2a45Smiod if (sec == NULL)
45672159047fSniklas h->u.def.value = 0;
45682159047fSniklas else
45692159047fSniklas h->u.def.value = bfd_get_section_vma (output_bfd, sec);
45702159047fSniklas
45712159047fSniklas h->u.def.section = bfd_abs_section_ptr;
45722159047fSniklas }
45732159047fSniklas }
45742159047fSniklas
4575b55d4692Sfgsch /* Create an absolute symbol with the given name with the value of the
45762159047fSniklas address of the first byte after the end of the section named.
45772159047fSniklas
4578b55d4692Sfgsch If the symbol already exists, then do nothing. */
4579b55d4692Sfgsch
45802159047fSniklas void
lang_abs_symbol_at_end_of(const char * secname,const char * name)4581*007c2a45Smiod lang_abs_symbol_at_end_of (const char *secname, const char *name)
45822159047fSniklas {
45832159047fSniklas struct bfd_link_hash_entry *h;
45842159047fSniklas
4585c074d1c9Sdrahn h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4586*007c2a45Smiod if (h == NULL)
4587b305b0f1Sespie einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
45882159047fSniklas
45892159047fSniklas if (h->type == bfd_link_hash_new
45902159047fSniklas || h->type == bfd_link_hash_undefined)
45912159047fSniklas {
45922159047fSniklas asection *sec;
45932159047fSniklas
45942159047fSniklas h->type = bfd_link_hash_defined;
45952159047fSniklas
45962159047fSniklas sec = bfd_get_section_by_name (output_bfd, secname);
4597*007c2a45Smiod if (sec == NULL)
45982159047fSniklas h->u.def.value = 0;
45992159047fSniklas else
46002159047fSniklas h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4601*007c2a45Smiod + TO_ADDR (bfd_section_size (output_bfd, sec)));
46022159047fSniklas
46032159047fSniklas h->u.def.section = bfd_abs_section_ptr;
46042159047fSniklas }
46052159047fSniklas }
46062159047fSniklas
46072159047fSniklas void
lang_statement_append(lang_statement_list_type * list,lang_statement_union_type * element,lang_statement_union_type ** field)4608*007c2a45Smiod lang_statement_append (lang_statement_list_type *list,
4609*007c2a45Smiod lang_statement_union_type *element,
4610*007c2a45Smiod lang_statement_union_type **field)
46112159047fSniklas {
46122159047fSniklas *(list->tail) = element;
46132159047fSniklas list->tail = field;
46142159047fSniklas }
46152159047fSniklas
46162159047fSniklas /* Set the output format type. -oformat overrides scripts. */
46172159047fSniklas
46182159047fSniklas void
lang_add_output_format(const char * format,const char * big,const char * little,int from_script)4619*007c2a45Smiod lang_add_output_format (const char *format,
4620*007c2a45Smiod const char *big,
4621*007c2a45Smiod const char *little,
4622*007c2a45Smiod int from_script)
46232159047fSniklas {
46242159047fSniklas if (output_target == NULL || !from_script)
46252159047fSniklas {
46262159047fSniklas if (command_line.endian == ENDIAN_BIG
46272159047fSniklas && big != NULL)
46282159047fSniklas format = big;
46292159047fSniklas else if (command_line.endian == ENDIAN_LITTLE
46302159047fSniklas && little != NULL)
46312159047fSniklas format = little;
46322159047fSniklas
46332159047fSniklas output_target = format;
46342159047fSniklas }
46352159047fSniklas }
46362159047fSniklas
46372159047fSniklas /* Enter a group. This creates a new lang_group_statement, and sets
46382159047fSniklas stat_ptr to build new statements within the group. */
46392159047fSniklas
46402159047fSniklas void
lang_enter_group(void)4641*007c2a45Smiod lang_enter_group (void)
46422159047fSniklas {
46432159047fSniklas lang_group_statement_type *g;
46442159047fSniklas
46452159047fSniklas g = new_stat (lang_group_statement, stat_ptr);
46462159047fSniklas lang_list_init (&g->children);
46472159047fSniklas stat_ptr = &g->children;
46482159047fSniklas }
46492159047fSniklas
46502159047fSniklas /* Leave a group. This just resets stat_ptr to start writing to the
46512159047fSniklas regular list of statements again. Note that this will not work if
46522159047fSniklas groups can occur inside anything else which can adjust stat_ptr,
46532159047fSniklas but currently they can't. */
46542159047fSniklas
46552159047fSniklas void
lang_leave_group(void)4656*007c2a45Smiod lang_leave_group (void)
46572159047fSniklas {
46582159047fSniklas stat_ptr = &statement_list;
46592159047fSniklas }
4660191aa565Sniklas
4661191aa565Sniklas /* Add a new program header. This is called for each entry in a PHDRS
4662191aa565Sniklas command in a linker script. */
4663191aa565Sniklas
4664191aa565Sniklas void
lang_new_phdr(const char * name,etree_type * type,bfd_boolean filehdr,bfd_boolean phdrs,etree_type * at,etree_type * flags)4665*007c2a45Smiod lang_new_phdr (const char *name,
4666*007c2a45Smiod etree_type *type,
4667*007c2a45Smiod bfd_boolean filehdr,
4668*007c2a45Smiod bfd_boolean phdrs,
4669*007c2a45Smiod etree_type *at,
4670*007c2a45Smiod etree_type *flags)
4671191aa565Sniklas {
4672191aa565Sniklas struct lang_phdr *n, **pp;
4673191aa565Sniklas
4674*007c2a45Smiod n = stat_alloc (sizeof (struct lang_phdr));
4675191aa565Sniklas n->next = NULL;
4676191aa565Sniklas n->name = name;
4677191aa565Sniklas n->type = exp_get_value_int (type, 0, "program header type",
4678191aa565Sniklas lang_final_phase_enum);
4679191aa565Sniklas n->filehdr = filehdr;
4680191aa565Sniklas n->phdrs = phdrs;
4681191aa565Sniklas n->at = at;
4682191aa565Sniklas n->flags = flags;
4683191aa565Sniklas
4684191aa565Sniklas for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4685191aa565Sniklas ;
4686191aa565Sniklas *pp = n;
4687191aa565Sniklas }
4688191aa565Sniklas
4689191aa565Sniklas /* Record the program header information in the output BFD. FIXME: We
4690191aa565Sniklas should not be calling an ELF specific function here. */
4691191aa565Sniklas
4692191aa565Sniklas static void
lang_record_phdrs(void)4693*007c2a45Smiod lang_record_phdrs (void)
4694191aa565Sniklas {
4695191aa565Sniklas unsigned int alc;
4696191aa565Sniklas asection **secs;
4697*007c2a45Smiod lang_output_section_phdr_list *last;
4698191aa565Sniklas struct lang_phdr *l;
4699191aa565Sniklas lang_statement_union_type *u;
4700191aa565Sniklas
4701191aa565Sniklas alc = 10;
4702*007c2a45Smiod secs = xmalloc (alc * sizeof (asection *));
4703191aa565Sniklas last = NULL;
4704191aa565Sniklas for (l = lang_phdr_list; l != NULL; l = l->next)
4705191aa565Sniklas {
4706191aa565Sniklas unsigned int c;
4707191aa565Sniklas flagword flags;
4708191aa565Sniklas bfd_vma at;
4709191aa565Sniklas
4710191aa565Sniklas c = 0;
4711191aa565Sniklas for (u = lang_output_section_statement.head;
4712191aa565Sniklas u != NULL;
4713191aa565Sniklas u = u->output_section_statement.next)
4714191aa565Sniklas {
4715191aa565Sniklas lang_output_section_statement_type *os;
4716*007c2a45Smiod lang_output_section_phdr_list *pl;
4717191aa565Sniklas
4718191aa565Sniklas os = &u->output_section_statement;
4719191aa565Sniklas
4720191aa565Sniklas pl = os->phdrs;
4721191aa565Sniklas if (pl != NULL)
4722191aa565Sniklas last = pl;
4723191aa565Sniklas else
4724191aa565Sniklas {
47250c6d0228Sniklas if (os->sectype == noload_section
4726191aa565Sniklas || os->bfd_section == NULL
4727191aa565Sniklas || (os->bfd_section->flags & SEC_ALLOC) == 0)
4728191aa565Sniklas continue;
4729191aa565Sniklas pl = last;
4730191aa565Sniklas }
4731191aa565Sniklas
4732191aa565Sniklas if (os->bfd_section == NULL)
4733191aa565Sniklas continue;
4734191aa565Sniklas
4735191aa565Sniklas for (; pl != NULL; pl = pl->next)
4736191aa565Sniklas {
4737191aa565Sniklas if (strcmp (pl->name, l->name) == 0)
4738191aa565Sniklas {
4739191aa565Sniklas if (c >= alc)
4740191aa565Sniklas {
4741191aa565Sniklas alc *= 2;
4742*007c2a45Smiod secs = xrealloc (secs, alc * sizeof (asection *));
4743191aa565Sniklas }
4744191aa565Sniklas secs[c] = os->bfd_section;
4745191aa565Sniklas ++c;
4746c074d1c9Sdrahn pl->used = TRUE;
4747191aa565Sniklas }
4748191aa565Sniklas }
4749191aa565Sniklas }
4750191aa565Sniklas
4751191aa565Sniklas if (l->flags == NULL)
4752191aa565Sniklas flags = 0;
4753191aa565Sniklas else
4754191aa565Sniklas flags = exp_get_vma (l->flags, 0, "phdr flags",
4755191aa565Sniklas lang_final_phase_enum);
4756191aa565Sniklas
4757191aa565Sniklas if (l->at == NULL)
4758191aa565Sniklas at = 0;
4759191aa565Sniklas else
4760191aa565Sniklas at = exp_get_vma (l->at, 0, "phdr load address",
4761191aa565Sniklas lang_final_phase_enum);
4762191aa565Sniklas
4763191aa565Sniklas if (! bfd_record_phdr (output_bfd, l->type,
4764b55d4692Sfgsch l->flags != NULL, flags, l->at != NULL,
4765191aa565Sniklas at, l->filehdr, l->phdrs, c, secs))
4766b305b0f1Sespie einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4767191aa565Sniklas }
4768191aa565Sniklas
4769191aa565Sniklas free (secs);
4770191aa565Sniklas
4771191aa565Sniklas /* Make sure all the phdr assignments succeeded. */
4772191aa565Sniklas for (u = lang_output_section_statement.head;
4773191aa565Sniklas u != NULL;
4774191aa565Sniklas u = u->output_section_statement.next)
4775191aa565Sniklas {
4776*007c2a45Smiod lang_output_section_phdr_list *pl;
4777191aa565Sniklas
4778191aa565Sniklas if (u->output_section_statement.bfd_section == NULL)
4779191aa565Sniklas continue;
4780191aa565Sniklas
4781191aa565Sniklas for (pl = u->output_section_statement.phdrs;
4782191aa565Sniklas pl != NULL;
4783191aa565Sniklas pl = pl->next)
4784191aa565Sniklas if (! pl->used && strcmp (pl->name, "NONE") != 0)
4785b305b0f1Sespie einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4786191aa565Sniklas u->output_section_statement.name, pl->name);
4787191aa565Sniklas }
4788191aa565Sniklas }
47890c6d0228Sniklas
47900c6d0228Sniklas /* Record a list of sections which may not be cross referenced. */
47910c6d0228Sniklas
47920c6d0228Sniklas void
lang_add_nocrossref(lang_nocrossref_type * l)4793*007c2a45Smiod lang_add_nocrossref (lang_nocrossref_type *l)
47940c6d0228Sniklas {
47950c6d0228Sniklas struct lang_nocrossrefs *n;
47960c6d0228Sniklas
4797*007c2a45Smiod n = xmalloc (sizeof *n);
47980c6d0228Sniklas n->next = nocrossref_list;
47990c6d0228Sniklas n->list = l;
48000c6d0228Sniklas nocrossref_list = n;
48010c6d0228Sniklas
48020c6d0228Sniklas /* Set notice_all so that we get informed about all symbols. */
4803c074d1c9Sdrahn link_info.notice_all = TRUE;
48040c6d0228Sniklas }
48050c6d0228Sniklas
48060c6d0228Sniklas /* Overlay handling. We handle overlays with some static variables. */
48070c6d0228Sniklas
48080c6d0228Sniklas /* The overlay virtual address. */
48090c6d0228Sniklas static etree_type *overlay_vma;
4810*007c2a45Smiod /* And subsection alignment. */
4811*007c2a45Smiod static etree_type *overlay_subalign;
48120c6d0228Sniklas
48130c6d0228Sniklas /* An expression for the maximum section size seen so far. */
48140c6d0228Sniklas static etree_type *overlay_max;
48150c6d0228Sniklas
48160c6d0228Sniklas /* A list of all the sections in this overlay. */
48170c6d0228Sniklas
4818b55d4692Sfgsch struct overlay_list {
48190c6d0228Sniklas struct overlay_list *next;
48200c6d0228Sniklas lang_output_section_statement_type *os;
48210c6d0228Sniklas };
48220c6d0228Sniklas
48230c6d0228Sniklas static struct overlay_list *overlay_list;
48240c6d0228Sniklas
48250c6d0228Sniklas /* Start handling an overlay. */
48260c6d0228Sniklas
48270c6d0228Sniklas void
lang_enter_overlay(etree_type * vma_expr,etree_type * subalign)4828*007c2a45Smiod lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
48290c6d0228Sniklas {
48300c6d0228Sniklas /* The grammar should prevent nested overlays from occurring. */
4831*007c2a45Smiod ASSERT (overlay_vma == NULL
4832*007c2a45Smiod && overlay_subalign == NULL
4833*007c2a45Smiod && overlay_max == NULL);
48340c6d0228Sniklas
48350c6d0228Sniklas overlay_vma = vma_expr;
4836*007c2a45Smiod overlay_subalign = subalign;
48370c6d0228Sniklas }
48380c6d0228Sniklas
48390c6d0228Sniklas /* Start a section in an overlay. We handle this by calling
4840c074d1c9Sdrahn lang_enter_output_section_statement with the correct VMA.
4841c074d1c9Sdrahn lang_leave_overlay sets up the LMA and memory regions. */
48420c6d0228Sniklas
48430c6d0228Sniklas void
lang_enter_overlay_section(const char * name)4844*007c2a45Smiod lang_enter_overlay_section (const char *name)
48450c6d0228Sniklas {
48460c6d0228Sniklas struct overlay_list *n;
48470c6d0228Sniklas etree_type *size;
48480c6d0228Sniklas
48490c6d0228Sniklas lang_enter_output_section_statement (name, overlay_vma, normal_section,
4850*007c2a45Smiod 0, overlay_subalign, 0);
48510c6d0228Sniklas
4852c074d1c9Sdrahn /* If this is the first section, then base the VMA of future
48530c6d0228Sniklas sections on this one. This will work correctly even if `.' is
48540c6d0228Sniklas used in the addresses. */
48550c6d0228Sniklas if (overlay_list == NULL)
48560c6d0228Sniklas overlay_vma = exp_nameop (ADDR, name);
48570c6d0228Sniklas
48580c6d0228Sniklas /* Remember the section. */
4859*007c2a45Smiod n = xmalloc (sizeof *n);
48600c6d0228Sniklas n->os = current_section;
48610c6d0228Sniklas n->next = overlay_list;
48620c6d0228Sniklas overlay_list = n;
48630c6d0228Sniklas
48640c6d0228Sniklas size = exp_nameop (SIZEOF, name);
48650c6d0228Sniklas
48660c6d0228Sniklas /* Arrange to work out the maximum section end address. */
48670c6d0228Sniklas if (overlay_max == NULL)
48680c6d0228Sniklas overlay_max = size;
48690c6d0228Sniklas else
4870b305b0f1Sespie overlay_max = exp_binop (MAX_K, overlay_max, size);
48710c6d0228Sniklas }
48720c6d0228Sniklas
48730c6d0228Sniklas /* Finish a section in an overlay. There isn't any special to do
48740c6d0228Sniklas here. */
48750c6d0228Sniklas
48760c6d0228Sniklas void
lang_leave_overlay_section(fill_type * fill,lang_output_section_phdr_list * phdrs)4877*007c2a45Smiod lang_leave_overlay_section (fill_type *fill,
4878*007c2a45Smiod lang_output_section_phdr_list *phdrs)
48790c6d0228Sniklas {
48800c6d0228Sniklas const char *name;
48810c6d0228Sniklas char *clean, *s2;
48820c6d0228Sniklas const char *s1;
48830c6d0228Sniklas char *buf;
48840c6d0228Sniklas
48850c6d0228Sniklas name = current_section->name;
48860c6d0228Sniklas
4887*007c2a45Smiod /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4888*007c2a45Smiod region and that no load-time region has been specified. It doesn't
4889*007c2a45Smiod really matter what we say here, since lang_leave_overlay will
4890*007c2a45Smiod override it. */
4891*007c2a45Smiod lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
48920c6d0228Sniklas
48930c6d0228Sniklas /* Define the magic symbols. */
48940c6d0228Sniklas
48950c6d0228Sniklas clean = xmalloc (strlen (name) + 1);
48960c6d0228Sniklas s2 = clean;
48970c6d0228Sniklas for (s1 = name; *s1 != '\0'; s1++)
4898c074d1c9Sdrahn if (ISALNUM (*s1) || *s1 == '_')
48990c6d0228Sniklas *s2++ = *s1;
49000c6d0228Sniklas *s2 = '\0';
49010c6d0228Sniklas
49020c6d0228Sniklas buf = xmalloc (strlen (clean) + sizeof "__load_start_");
49030c6d0228Sniklas sprintf (buf, "__load_start_%s", clean);
49040c6d0228Sniklas lang_add_assignment (exp_assop ('=', buf,
49050c6d0228Sniklas exp_nameop (LOADADDR, name)));
49060c6d0228Sniklas
49070c6d0228Sniklas buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
49080c6d0228Sniklas sprintf (buf, "__load_stop_%s", clean);
49090c6d0228Sniklas lang_add_assignment (exp_assop ('=', buf,
49100c6d0228Sniklas exp_binop ('+',
49110c6d0228Sniklas exp_nameop (LOADADDR, name),
49120c6d0228Sniklas exp_nameop (SIZEOF, name))));
49130c6d0228Sniklas
49140c6d0228Sniklas free (clean);
49150c6d0228Sniklas }
49160c6d0228Sniklas
49170c6d0228Sniklas /* Finish an overlay. If there are any overlay wide settings, this
49180c6d0228Sniklas looks through all the sections in the overlay and sets them. */
49190c6d0228Sniklas
49200c6d0228Sniklas void
lang_leave_overlay(etree_type * lma_expr,int nocrossrefs,fill_type * fill,const char * memspec,lang_output_section_phdr_list * phdrs,const char * lma_memspec)4921*007c2a45Smiod lang_leave_overlay (etree_type *lma_expr,
4922*007c2a45Smiod int nocrossrefs,
4923*007c2a45Smiod fill_type *fill,
4924*007c2a45Smiod const char *memspec,
4925*007c2a45Smiod lang_output_section_phdr_list *phdrs,
4926*007c2a45Smiod const char *lma_memspec)
49270c6d0228Sniklas {
49280c6d0228Sniklas lang_memory_region_type *region;
4929b305b0f1Sespie lang_memory_region_type *lma_region;
49300c6d0228Sniklas struct overlay_list *l;
4931*007c2a45Smiod lang_nocrossref_type *nocrossref;
49320c6d0228Sniklas
4933c074d1c9Sdrahn lang_get_regions (®ion, &lma_region,
4934c074d1c9Sdrahn memspec, lma_memspec,
4935*007c2a45Smiod lma_expr != NULL, FALSE);
4936b305b0f1Sespie
49370c6d0228Sniklas nocrossref = NULL;
49380c6d0228Sniklas
4939c074d1c9Sdrahn /* After setting the size of the last section, set '.' to end of the
4940c074d1c9Sdrahn overlay region. */
4941c074d1c9Sdrahn if (overlay_list != NULL)
4942c074d1c9Sdrahn overlay_list->os->update_dot_tree
4943c074d1c9Sdrahn = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4944c074d1c9Sdrahn
49450c6d0228Sniklas l = overlay_list;
49460c6d0228Sniklas while (l != NULL)
49470c6d0228Sniklas {
49480c6d0228Sniklas struct overlay_list *next;
49490c6d0228Sniklas
4950*007c2a45Smiod if (fill != NULL && l->os->fill == NULL)
49510c6d0228Sniklas l->os->fill = fill;
4952c074d1c9Sdrahn
49530c6d0228Sniklas l->os->region = region;
4954b305b0f1Sespie l->os->lma_region = lma_region;
4955c074d1c9Sdrahn
4956c074d1c9Sdrahn /* The first section has the load address specified in the
4957c074d1c9Sdrahn OVERLAY statement. The rest are worked out from that.
4958c074d1c9Sdrahn The base address is not needed (and should be null) if
4959c074d1c9Sdrahn an LMA region was specified. */
4960c074d1c9Sdrahn if (l->next == 0)
4961c074d1c9Sdrahn l->os->load_base = lma_expr;
4962c074d1c9Sdrahn else if (lma_region == 0)
4963c074d1c9Sdrahn l->os->load_base = exp_binop ('+',
4964c074d1c9Sdrahn exp_nameop (LOADADDR, l->next->os->name),
4965c074d1c9Sdrahn exp_nameop (SIZEOF, l->next->os->name));
4966c074d1c9Sdrahn
49670c6d0228Sniklas if (phdrs != NULL && l->os->phdrs == NULL)
49680c6d0228Sniklas l->os->phdrs = phdrs;
49690c6d0228Sniklas
4970c074d1c9Sdrahn if (nocrossrefs)
49710c6d0228Sniklas {
4972*007c2a45Smiod lang_nocrossref_type *nc;
49730c6d0228Sniklas
4974*007c2a45Smiod nc = xmalloc (sizeof *nc);
49750c6d0228Sniklas nc->name = l->os->name;
49760c6d0228Sniklas nc->next = nocrossref;
49770c6d0228Sniklas nocrossref = nc;
49780c6d0228Sniklas }
49790c6d0228Sniklas
49800c6d0228Sniklas next = l->next;
49810c6d0228Sniklas free (l);
49820c6d0228Sniklas l = next;
49830c6d0228Sniklas }
49840c6d0228Sniklas
49850c6d0228Sniklas if (nocrossref != NULL)
49860c6d0228Sniklas lang_add_nocrossref (nocrossref);
49870c6d0228Sniklas
49880c6d0228Sniklas overlay_vma = NULL;
49890c6d0228Sniklas overlay_list = NULL;
49900c6d0228Sniklas overlay_max = NULL;
49910c6d0228Sniklas }
4992b305b0f1Sespie
4993b305b0f1Sespie /* Version handling. This is only useful for ELF. */
4994b305b0f1Sespie
4995b305b0f1Sespie /* This global variable holds the version tree that we build. */
4996b305b0f1Sespie
4997b305b0f1Sespie struct bfd_elf_version_tree *lang_elf_version_info;
4998b305b0f1Sespie
4999*007c2a45Smiod /* If PREV is NULL, return first version pattern matching particular symbol.
5000*007c2a45Smiod If PREV is non-NULL, return first version pattern matching particular
5001*007c2a45Smiod symbol after PREV (previously returned by lang_vers_match). */
5002*007c2a45Smiod
5003*007c2a45Smiod static struct bfd_elf_version_expr *
lang_vers_match(struct bfd_elf_version_expr_head * head,struct bfd_elf_version_expr * prev,const char * sym)5004*007c2a45Smiod lang_vers_match (struct bfd_elf_version_expr_head *head,
5005*007c2a45Smiod struct bfd_elf_version_expr *prev,
5006*007c2a45Smiod const char *sym)
5007b305b0f1Sespie {
5008*007c2a45Smiod const char *cxx_sym = sym;
5009*007c2a45Smiod const char *java_sym = sym;
5010*007c2a45Smiod struct bfd_elf_version_expr *expr = NULL;
5011*007c2a45Smiod
5012*007c2a45Smiod if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5013*007c2a45Smiod {
5014*007c2a45Smiod cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5015*007c2a45Smiod if (!cxx_sym)
5016*007c2a45Smiod cxx_sym = sym;
5017*007c2a45Smiod }
5018*007c2a45Smiod if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5019*007c2a45Smiod {
5020*007c2a45Smiod java_sym = cplus_demangle (sym, DMGL_JAVA);
5021*007c2a45Smiod if (!java_sym)
5022*007c2a45Smiod java_sym = sym;
5023b305b0f1Sespie }
5024b305b0f1Sespie
5025*007c2a45Smiod if (head->htab && (prev == NULL || prev->symbol))
5026b305b0f1Sespie {
5027*007c2a45Smiod struct bfd_elf_version_expr e;
5028*007c2a45Smiod
5029*007c2a45Smiod switch (prev ? prev->mask : 0)
5030*007c2a45Smiod {
5031*007c2a45Smiod case 0:
5032*007c2a45Smiod if (head->mask & BFD_ELF_VERSION_C_TYPE)
5033*007c2a45Smiod {
5034*007c2a45Smiod e.symbol = sym;
5035*007c2a45Smiod expr = htab_find (head->htab, &e);
5036*007c2a45Smiod while (expr && strcmp (expr->symbol, sym) == 0)
5037*007c2a45Smiod if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5038*007c2a45Smiod goto out_ret;
5039*007c2a45Smiod else
5040*007c2a45Smiod expr = expr->next;
5041*007c2a45Smiod }
5042*007c2a45Smiod /* Fallthrough */
5043*007c2a45Smiod case BFD_ELF_VERSION_C_TYPE:
5044*007c2a45Smiod if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5045*007c2a45Smiod {
5046*007c2a45Smiod e.symbol = cxx_sym;
5047*007c2a45Smiod expr = htab_find (head->htab, &e);
5048*007c2a45Smiod while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5049*007c2a45Smiod if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5050*007c2a45Smiod goto out_ret;
5051*007c2a45Smiod else
5052*007c2a45Smiod expr = expr->next;
5053*007c2a45Smiod }
5054*007c2a45Smiod /* Fallthrough */
5055*007c2a45Smiod case BFD_ELF_VERSION_CXX_TYPE:
5056*007c2a45Smiod if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5057*007c2a45Smiod {
5058*007c2a45Smiod e.symbol = java_sym;
5059*007c2a45Smiod expr = htab_find (head->htab, &e);
5060*007c2a45Smiod while (expr && strcmp (expr->symbol, java_sym) == 0)
5061*007c2a45Smiod if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5062*007c2a45Smiod goto out_ret;
5063*007c2a45Smiod else
5064*007c2a45Smiod expr = expr->next;
5065*007c2a45Smiod }
5066*007c2a45Smiod /* Fallthrough */
5067*007c2a45Smiod default:
5068*007c2a45Smiod break;
5069*007c2a45Smiod }
5070*007c2a45Smiod }
5071*007c2a45Smiod
5072*007c2a45Smiod /* Finally, try the wildcards. */
5073*007c2a45Smiod if (prev == NULL || prev->symbol)
5074*007c2a45Smiod expr = head->remaining;
5075*007c2a45Smiod else
5076*007c2a45Smiod expr = prev->next;
5077*007c2a45Smiod while (expr)
5078*007c2a45Smiod {
5079*007c2a45Smiod const char *s;
5080b305b0f1Sespie
5081b305b0f1Sespie if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5082*007c2a45Smiod break;
5083b305b0f1Sespie
5084*007c2a45Smiod if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5085*007c2a45Smiod s = java_sym;
5086*007c2a45Smiod else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5087*007c2a45Smiod s = cxx_sym;
5088*007c2a45Smiod else
5089*007c2a45Smiod s = sym;
5090*007c2a45Smiod if (fnmatch (expr->pattern, s, 0) == 0)
5091*007c2a45Smiod break;
5092*007c2a45Smiod expr = expr->next;
5093*007c2a45Smiod }
5094*007c2a45Smiod
5095*007c2a45Smiod out_ret:
5096*007c2a45Smiod if (cxx_sym != sym)
5097*007c2a45Smiod free ((char *) cxx_sym);
5098*007c2a45Smiod if (java_sym != sym)
5099*007c2a45Smiod free ((char *) java_sym);
5100*007c2a45Smiod return expr;
5101*007c2a45Smiod }
5102*007c2a45Smiod
5103*007c2a45Smiod /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5104*007c2a45Smiod return a string pointing to the symbol name. */
5105*007c2a45Smiod
5106*007c2a45Smiod static const char *
realsymbol(const char * pattern)5107*007c2a45Smiod realsymbol (const char *pattern)
5108b305b0f1Sespie {
5109*007c2a45Smiod const char *p;
5110*007c2a45Smiod bfd_boolean changed = FALSE, backslash = FALSE;
5111*007c2a45Smiod char *s, *symbol = xmalloc (strlen (pattern) + 1);
5112*007c2a45Smiod
5113*007c2a45Smiod for (p = pattern, s = symbol; *p != '\0'; ++p)
5114*007c2a45Smiod {
5115*007c2a45Smiod /* It is a glob pattern only if there is no preceding
5116*007c2a45Smiod backslash. */
5117*007c2a45Smiod if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5118*007c2a45Smiod {
5119*007c2a45Smiod free (symbol);
5120*007c2a45Smiod return NULL;
5121*007c2a45Smiod }
5122*007c2a45Smiod
5123*007c2a45Smiod if (backslash)
5124*007c2a45Smiod {
5125*007c2a45Smiod /* Remove the preceding backslash. */
5126*007c2a45Smiod *(s - 1) = *p;
5127*007c2a45Smiod changed = TRUE;
5128*007c2a45Smiod }
5129*007c2a45Smiod else
5130*007c2a45Smiod *s++ = *p;
5131*007c2a45Smiod
5132*007c2a45Smiod backslash = *p == '\\';
5133*007c2a45Smiod }
5134*007c2a45Smiod
5135*007c2a45Smiod if (changed)
5136*007c2a45Smiod {
5137*007c2a45Smiod *s = '\0';
5138*007c2a45Smiod return symbol;
5139b305b0f1Sespie }
5140b305b0f1Sespie else
5141b305b0f1Sespie {
5142*007c2a45Smiod free (symbol);
5143*007c2a45Smiod return pattern;
5144b305b0f1Sespie }
5145b305b0f1Sespie }
5146b305b0f1Sespie
5147b305b0f1Sespie /* This is called for each variable name or match expression. */
5148b305b0f1Sespie
5149b305b0f1Sespie struct bfd_elf_version_expr *
lang_new_vers_pattern(struct bfd_elf_version_expr * orig,const char * new,const char * lang)5150*007c2a45Smiod lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5151*007c2a45Smiod const char *new,
5152*007c2a45Smiod const char *lang)
5153b305b0f1Sespie {
5154b305b0f1Sespie struct bfd_elf_version_expr *ret;
5155b305b0f1Sespie
5156*007c2a45Smiod ret = xmalloc (sizeof *ret);
5157b305b0f1Sespie ret->next = orig;
5158b305b0f1Sespie ret->pattern = new;
5159c074d1c9Sdrahn ret->symver = 0;
5160c074d1c9Sdrahn ret->script = 0;
5161*007c2a45Smiod ret->symbol = realsymbol (new);
5162b305b0f1Sespie
5163b305b0f1Sespie if (lang == NULL || strcasecmp (lang, "C") == 0)
5164*007c2a45Smiod ret->mask = BFD_ELF_VERSION_C_TYPE;
5165b305b0f1Sespie else if (strcasecmp (lang, "C++") == 0)
5166*007c2a45Smiod ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5167b305b0f1Sespie else if (strcasecmp (lang, "Java") == 0)
5168*007c2a45Smiod ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5169b305b0f1Sespie else
5170b305b0f1Sespie {
5171b305b0f1Sespie einfo (_("%X%P: unknown language `%s' in version information\n"),
5172b305b0f1Sespie lang);
5173*007c2a45Smiod ret->mask = BFD_ELF_VERSION_C_TYPE;
5174b305b0f1Sespie }
5175b305b0f1Sespie
5176c074d1c9Sdrahn return ldemul_new_vers_pattern (ret);
5177b305b0f1Sespie }
5178b305b0f1Sespie
5179b305b0f1Sespie /* This is called for each set of variable names and match
5180b305b0f1Sespie expressions. */
5181b305b0f1Sespie
5182b305b0f1Sespie struct bfd_elf_version_tree *
lang_new_vers_node(struct bfd_elf_version_expr * globals,struct bfd_elf_version_expr * locals)5183*007c2a45Smiod lang_new_vers_node (struct bfd_elf_version_expr *globals,
5184*007c2a45Smiod struct bfd_elf_version_expr *locals)
5185b305b0f1Sespie {
5186b305b0f1Sespie struct bfd_elf_version_tree *ret;
5187b305b0f1Sespie
5188*007c2a45Smiod ret = xcalloc (1, sizeof *ret);
5189*007c2a45Smiod ret->globals.list = globals;
5190*007c2a45Smiod ret->locals.list = locals;
5191*007c2a45Smiod ret->match = lang_vers_match;
5192b305b0f1Sespie ret->name_indx = (unsigned int) -1;
5193b305b0f1Sespie return ret;
5194b305b0f1Sespie }
5195b305b0f1Sespie
5196b305b0f1Sespie /* This static variable keeps track of version indices. */
5197b305b0f1Sespie
5198b305b0f1Sespie static int version_index;
5199b305b0f1Sespie
5200*007c2a45Smiod static hashval_t
version_expr_head_hash(const void * p)5201*007c2a45Smiod version_expr_head_hash (const void *p)
5202*007c2a45Smiod {
5203*007c2a45Smiod const struct bfd_elf_version_expr *e = p;
5204*007c2a45Smiod
5205*007c2a45Smiod return htab_hash_string (e->symbol);
5206*007c2a45Smiod }
5207*007c2a45Smiod
5208*007c2a45Smiod static int
version_expr_head_eq(const void * p1,const void * p2)5209*007c2a45Smiod version_expr_head_eq (const void *p1, const void *p2)
5210*007c2a45Smiod {
5211*007c2a45Smiod const struct bfd_elf_version_expr *e1 = p1;
5212*007c2a45Smiod const struct bfd_elf_version_expr *e2 = p2;
5213*007c2a45Smiod
5214*007c2a45Smiod return strcmp (e1->symbol, e2->symbol) == 0;
5215*007c2a45Smiod }
5216*007c2a45Smiod
5217*007c2a45Smiod static void
lang_finalize_version_expr_head(struct bfd_elf_version_expr_head * head)5218*007c2a45Smiod lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5219*007c2a45Smiod {
5220*007c2a45Smiod size_t count = 0;
5221*007c2a45Smiod struct bfd_elf_version_expr *e, *next;
5222*007c2a45Smiod struct bfd_elf_version_expr **list_loc, **remaining_loc;
5223*007c2a45Smiod
5224*007c2a45Smiod for (e = head->list; e; e = e->next)
5225*007c2a45Smiod {
5226*007c2a45Smiod if (e->symbol)
5227*007c2a45Smiod count++;
5228*007c2a45Smiod head->mask |= e->mask;
5229*007c2a45Smiod }
5230*007c2a45Smiod
5231*007c2a45Smiod if (count)
5232*007c2a45Smiod {
5233*007c2a45Smiod head->htab = htab_create (count * 2, version_expr_head_hash,
5234*007c2a45Smiod version_expr_head_eq, NULL);
5235*007c2a45Smiod list_loc = &head->list;
5236*007c2a45Smiod remaining_loc = &head->remaining;
5237*007c2a45Smiod for (e = head->list; e; e = next)
5238*007c2a45Smiod {
5239*007c2a45Smiod next = e->next;
5240*007c2a45Smiod if (!e->symbol)
5241*007c2a45Smiod {
5242*007c2a45Smiod *remaining_loc = e;
5243*007c2a45Smiod remaining_loc = &e->next;
5244*007c2a45Smiod }
5245*007c2a45Smiod else
5246*007c2a45Smiod {
5247*007c2a45Smiod void **loc = htab_find_slot (head->htab, e, INSERT);
5248*007c2a45Smiod
5249*007c2a45Smiod if (*loc)
5250*007c2a45Smiod {
5251*007c2a45Smiod struct bfd_elf_version_expr *e1, *last;
5252*007c2a45Smiod
5253*007c2a45Smiod e1 = *loc;
5254*007c2a45Smiod last = NULL;
5255*007c2a45Smiod do
5256*007c2a45Smiod {
5257*007c2a45Smiod if (e1->mask == e->mask)
5258*007c2a45Smiod {
5259*007c2a45Smiod last = NULL;
5260*007c2a45Smiod break;
5261*007c2a45Smiod }
5262*007c2a45Smiod last = e1;
5263*007c2a45Smiod e1 = e1->next;
5264*007c2a45Smiod }
5265*007c2a45Smiod while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5266*007c2a45Smiod
5267*007c2a45Smiod if (last == NULL)
5268*007c2a45Smiod {
5269*007c2a45Smiod /* This is a duplicate. */
5270*007c2a45Smiod /* FIXME: Memory leak. Sometimes pattern is not
5271*007c2a45Smiod xmalloced alone, but in larger chunk of memory. */
5272*007c2a45Smiod /* free (e->symbol); */
5273*007c2a45Smiod free (e);
5274*007c2a45Smiod }
5275*007c2a45Smiod else
5276*007c2a45Smiod {
5277*007c2a45Smiod e->next = last->next;
5278*007c2a45Smiod last->next = e;
5279*007c2a45Smiod }
5280*007c2a45Smiod }
5281*007c2a45Smiod else
5282*007c2a45Smiod {
5283*007c2a45Smiod *loc = e;
5284*007c2a45Smiod *list_loc = e;
5285*007c2a45Smiod list_loc = &e->next;
5286*007c2a45Smiod }
5287*007c2a45Smiod }
5288*007c2a45Smiod }
5289*007c2a45Smiod *remaining_loc = NULL;
5290*007c2a45Smiod *list_loc = head->remaining;
5291*007c2a45Smiod }
5292*007c2a45Smiod else
5293*007c2a45Smiod head->remaining = head->list;
5294*007c2a45Smiod }
5295*007c2a45Smiod
5296b305b0f1Sespie /* This is called when we know the name and dependencies of the
5297b305b0f1Sespie version. */
5298b305b0f1Sespie
5299b305b0f1Sespie void
lang_register_vers_node(const char * name,struct bfd_elf_version_tree * version,struct bfd_elf_version_deps * deps)5300*007c2a45Smiod lang_register_vers_node (const char *name,
5301*007c2a45Smiod struct bfd_elf_version_tree *version,
5302*007c2a45Smiod struct bfd_elf_version_deps *deps)
5303b305b0f1Sespie {
5304b305b0f1Sespie struct bfd_elf_version_tree *t, **pp;
5305b305b0f1Sespie struct bfd_elf_version_expr *e1;
5306b305b0f1Sespie
5307c074d1c9Sdrahn if (name == NULL)
5308c074d1c9Sdrahn name = "";
5309c074d1c9Sdrahn
5310c074d1c9Sdrahn if ((name[0] == '\0' && lang_elf_version_info != NULL)
5311c074d1c9Sdrahn || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5312c074d1c9Sdrahn {
5313c074d1c9Sdrahn einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5314c074d1c9Sdrahn free (version);
5315c074d1c9Sdrahn return;
5316c074d1c9Sdrahn }
5317c074d1c9Sdrahn
5318b305b0f1Sespie /* Make sure this node has a unique name. */
5319b305b0f1Sespie for (t = lang_elf_version_info; t != NULL; t = t->next)
5320b305b0f1Sespie if (strcmp (t->name, name) == 0)
5321b305b0f1Sespie einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5322b305b0f1Sespie
5323*007c2a45Smiod lang_finalize_version_expr_head (&version->globals);
5324*007c2a45Smiod lang_finalize_version_expr_head (&version->locals);
5325*007c2a45Smiod
5326b305b0f1Sespie /* Check the global and local match names, and make sure there
5327b305b0f1Sespie aren't any duplicates. */
5328b305b0f1Sespie
5329*007c2a45Smiod for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5330b305b0f1Sespie {
5331b305b0f1Sespie for (t = lang_elf_version_info; t != NULL; t = t->next)
5332b305b0f1Sespie {
5333b305b0f1Sespie struct bfd_elf_version_expr *e2;
5334b305b0f1Sespie
5335*007c2a45Smiod if (t->locals.htab && e1->symbol)
5336*007c2a45Smiod {
5337*007c2a45Smiod e2 = htab_find (t->locals.htab, e1);
5338*007c2a45Smiod while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5339*007c2a45Smiod {
5340*007c2a45Smiod if (e1->mask == e2->mask)
5341*007c2a45Smiod einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5342*007c2a45Smiod e1->symbol);
5343*007c2a45Smiod e2 = e2->next;
5344*007c2a45Smiod }
5345*007c2a45Smiod }
5346*007c2a45Smiod else if (!e1->symbol)
5347*007c2a45Smiod for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5348*007c2a45Smiod if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5349b305b0f1Sespie einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5350b305b0f1Sespie e1->pattern);
5351b305b0f1Sespie }
5352b305b0f1Sespie }
5353b305b0f1Sespie
5354*007c2a45Smiod for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5355b305b0f1Sespie {
5356b305b0f1Sespie for (t = lang_elf_version_info; t != NULL; t = t->next)
5357b305b0f1Sespie {
5358b305b0f1Sespie struct bfd_elf_version_expr *e2;
5359b305b0f1Sespie
5360*007c2a45Smiod if (t->globals.htab && e1->symbol)
5361*007c2a45Smiod {
5362*007c2a45Smiod e2 = htab_find (t->globals.htab, e1);
5363*007c2a45Smiod while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5364*007c2a45Smiod {
5365*007c2a45Smiod if (e1->mask == e2->mask)
5366*007c2a45Smiod einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5367*007c2a45Smiod e1->symbol);
5368*007c2a45Smiod e2 = e2->next;
5369*007c2a45Smiod }
5370*007c2a45Smiod }
5371*007c2a45Smiod else if (!e1->symbol)
5372*007c2a45Smiod for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5373*007c2a45Smiod if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5374b305b0f1Sespie einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5375b305b0f1Sespie e1->pattern);
5376b305b0f1Sespie }
5377b305b0f1Sespie }
5378b305b0f1Sespie
5379b305b0f1Sespie version->deps = deps;
5380b305b0f1Sespie version->name = name;
5381c074d1c9Sdrahn if (name[0] != '\0')
5382c074d1c9Sdrahn {
5383b305b0f1Sespie ++version_index;
5384b305b0f1Sespie version->vernum = version_index;
5385c074d1c9Sdrahn }
5386c074d1c9Sdrahn else
5387c074d1c9Sdrahn version->vernum = 0;
5388b305b0f1Sespie
5389b305b0f1Sespie for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5390b305b0f1Sespie ;
5391b305b0f1Sespie *pp = version;
5392b305b0f1Sespie }
5393b305b0f1Sespie
5394b305b0f1Sespie /* This is called when we see a version dependency. */
5395b305b0f1Sespie
5396b305b0f1Sespie struct bfd_elf_version_deps *
lang_add_vers_depend(struct bfd_elf_version_deps * list,const char * name)5397*007c2a45Smiod lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5398b305b0f1Sespie {
5399b305b0f1Sespie struct bfd_elf_version_deps *ret;
5400b305b0f1Sespie struct bfd_elf_version_tree *t;
5401b305b0f1Sespie
5402*007c2a45Smiod ret = xmalloc (sizeof *ret);
5403b305b0f1Sespie ret->next = list;
5404b305b0f1Sespie
5405b305b0f1Sespie for (t = lang_elf_version_info; t != NULL; t = t->next)
5406b305b0f1Sespie {
5407b305b0f1Sespie if (strcmp (t->name, name) == 0)
5408b305b0f1Sespie {
5409b305b0f1Sespie ret->version_needed = t;
5410b305b0f1Sespie return ret;
5411b305b0f1Sespie }
5412b305b0f1Sespie }
5413b305b0f1Sespie
5414b305b0f1Sespie einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5415b305b0f1Sespie
5416b305b0f1Sespie return ret;
5417b305b0f1Sespie }
5418b305b0f1Sespie
5419b305b0f1Sespie static void
lang_do_version_exports_section(void)5420*007c2a45Smiod lang_do_version_exports_section (void)
5421b305b0f1Sespie {
5422b305b0f1Sespie struct bfd_elf_version_expr *greg = NULL, *lreg;
5423b305b0f1Sespie
5424b305b0f1Sespie LANG_FOR_EACH_INPUT_STATEMENT (is)
5425b305b0f1Sespie {
5426b305b0f1Sespie asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5427b305b0f1Sespie char *contents, *p;
5428b305b0f1Sespie bfd_size_type len;
5429b305b0f1Sespie
5430b305b0f1Sespie if (sec == NULL)
5431b305b0f1Sespie continue;
5432b305b0f1Sespie
5433b305b0f1Sespie len = bfd_section_size (is->the_bfd, sec);
5434b305b0f1Sespie contents = xmalloc (len);
5435b305b0f1Sespie if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5436c074d1c9Sdrahn einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5437b305b0f1Sespie
5438b305b0f1Sespie p = contents;
5439b305b0f1Sespie while (p < contents + len)
5440b305b0f1Sespie {
5441c074d1c9Sdrahn greg = lang_new_vers_pattern (greg, p, NULL);
5442b305b0f1Sespie p = strchr (p, '\0') + 1;
5443b305b0f1Sespie }
5444b305b0f1Sespie
5445b305b0f1Sespie /* Do not free the contents, as we used them creating the regex. */
5446b305b0f1Sespie
5447b305b0f1Sespie /* Do not include this section in the link. */
5448b305b0f1Sespie bfd_set_section_flags (is->the_bfd, sec,
5449b305b0f1Sespie bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5450b305b0f1Sespie }
5451b305b0f1Sespie
5452c074d1c9Sdrahn lreg = lang_new_vers_pattern (NULL, "*", NULL);
5453b305b0f1Sespie lang_register_vers_node (command_line.version_exports_section,
5454b305b0f1Sespie lang_new_vers_node (greg, lreg), NULL);
5455b305b0f1Sespie }
5456b55d4692Sfgsch
5457b55d4692Sfgsch void
lang_add_unique(const char * name)5458*007c2a45Smiod lang_add_unique (const char *name)
5459b55d4692Sfgsch {
5460b55d4692Sfgsch struct unique_sections *ent;
5461b55d4692Sfgsch
5462b55d4692Sfgsch for (ent = unique_section_list; ent; ent = ent->next)
5463b55d4692Sfgsch if (strcmp (ent->name, name) == 0)
5464b55d4692Sfgsch return;
5465b55d4692Sfgsch
5466*007c2a45Smiod ent = xmalloc (sizeof *ent);
5467b55d4692Sfgsch ent->name = xstrdup (name);
5468b55d4692Sfgsch ent->next = unique_section_list;
5469b55d4692Sfgsch unique_section_list = ent;
5470b55d4692Sfgsch }
5471