xref: /netbsd/external/gpl3/gdb.old/dist/ld/ldlang.c (revision 56bb7041)
1*56bb7041Schristos /* Linker command language support.
2*56bb7041Schristos    Copyright (C) 1991-2020 Free Software Foundation, Inc.
3*56bb7041Schristos 
4*56bb7041Schristos    This file is part of the GNU Binutils.
5*56bb7041Schristos 
6*56bb7041Schristos    This program is free software; you can redistribute it and/or modify
7*56bb7041Schristos    it under the terms of the GNU General Public License as published by
8*56bb7041Schristos    the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos    (at your option) any later version.
10*56bb7041Schristos 
11*56bb7041Schristos    This program is distributed in the hope that it will be useful,
12*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*56bb7041Schristos    GNU General Public License for more details.
15*56bb7041Schristos 
16*56bb7041Schristos    You should have received a copy of the GNU General Public License
17*56bb7041Schristos    along with this program; if not, write to the Free Software
18*56bb7041Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19*56bb7041Schristos    MA 02110-1301, USA.  */
20*56bb7041Schristos 
21*56bb7041Schristos #include "sysdep.h"
22*56bb7041Schristos #include <limits.h>
23*56bb7041Schristos #include "bfd.h"
24*56bb7041Schristos #include "libiberty.h"
25*56bb7041Schristos #include "filenames.h"
26*56bb7041Schristos #include "safe-ctype.h"
27*56bb7041Schristos #include "obstack.h"
28*56bb7041Schristos #include "bfdlink.h"
29*56bb7041Schristos #include "ctf-api.h"
30*56bb7041Schristos 
31*56bb7041Schristos #include "ld.h"
32*56bb7041Schristos #include "ldmain.h"
33*56bb7041Schristos #include "ldexp.h"
34*56bb7041Schristos #include "ldlang.h"
35*56bb7041Schristos #include <ldgram.h>
36*56bb7041Schristos #include "ldlex.h"
37*56bb7041Schristos #include "ldmisc.h"
38*56bb7041Schristos #include "ldctor.h"
39*56bb7041Schristos #include "ldfile.h"
40*56bb7041Schristos #include "ldemul.h"
41*56bb7041Schristos #include "fnmatch.h"
42*56bb7041Schristos #include "demangle.h"
43*56bb7041Schristos #include "hashtab.h"
44*56bb7041Schristos #include "elf-bfd.h"
45*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
46*56bb7041Schristos #include "plugin.h"
47*56bb7041Schristos #endif /* BFD_SUPPORTS_PLUGINS */
48*56bb7041Schristos 
49*56bb7041Schristos #ifndef offsetof
50*56bb7041Schristos #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
51*56bb7041Schristos #endif
52*56bb7041Schristos 
53*56bb7041Schristos /* Convert between addresses in bytes and sizes in octets.
54*56bb7041Schristos    For currently supported targets, octets_per_byte is always a power
55*56bb7041Schristos    of two, so we can use shifts.  */
56*56bb7041Schristos #define TO_ADDR(X) ((X) >> opb_shift)
57*56bb7041Schristos #define TO_SIZE(X) ((X) << opb_shift)
58*56bb7041Schristos 
59*56bb7041Schristos /* Local variables.  */
60*56bb7041Schristos static struct obstack stat_obstack;
61*56bb7041Schristos static struct obstack map_obstack;
62*56bb7041Schristos 
63*56bb7041Schristos #define obstack_chunk_alloc xmalloc
64*56bb7041Schristos #define obstack_chunk_free free
65*56bb7041Schristos static const char *entry_symbol_default = "start";
66*56bb7041Schristos static bfd_boolean map_head_is_link_order = FALSE;
67*56bb7041Schristos static lang_output_section_statement_type *default_common_section;
68*56bb7041Schristos static bfd_boolean map_option_f;
69*56bb7041Schristos static bfd_vma print_dot;
70*56bb7041Schristos static lang_input_statement_type *first_file;
71*56bb7041Schristos static const char *current_target;
72*56bb7041Schristos /* Header for list of statements corresponding to any files involved in the
73*56bb7041Schristos    link, either specified from the command-line or added implicitely (eg.
74*56bb7041Schristos    archive member used to resolved undefined symbol, wildcard statement from
75*56bb7041Schristos    linker script, etc.).  Next pointer is in next field of a
76*56bb7041Schristos    lang_statement_header_type (reached via header field in a
77*56bb7041Schristos    lang_statement_union).  */
78*56bb7041Schristos static lang_statement_list_type statement_list;
79*56bb7041Schristos static lang_statement_list_type *stat_save[10];
80*56bb7041Schristos static lang_statement_list_type **stat_save_ptr = &stat_save[0];
81*56bb7041Schristos static struct unique_sections *unique_section_list;
82*56bb7041Schristos static struct asneeded_minfo *asneeded_list_head;
83*56bb7041Schristos static unsigned int opb_shift = 0;
84*56bb7041Schristos 
85*56bb7041Schristos /* Forward declarations.  */
86*56bb7041Schristos static void exp_init_os (etree_type *);
87*56bb7041Schristos static lang_input_statement_type *lookup_name (const char *);
88*56bb7041Schristos static void insert_undefined (const char *);
89*56bb7041Schristos static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
90*56bb7041Schristos static void print_statement (lang_statement_union_type *,
91*56bb7041Schristos 			     lang_output_section_statement_type *);
92*56bb7041Schristos static void print_statement_list (lang_statement_union_type *,
93*56bb7041Schristos 				  lang_output_section_statement_type *);
94*56bb7041Schristos static void print_statements (void);
95*56bb7041Schristos static void print_input_section (asection *, bfd_boolean);
96*56bb7041Schristos static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
97*56bb7041Schristos static void lang_record_phdrs (void);
98*56bb7041Schristos static void lang_do_version_exports_section (void);
99*56bb7041Schristos static void lang_finalize_version_expr_head
100*56bb7041Schristos   (struct bfd_elf_version_expr_head *);
101*56bb7041Schristos static void lang_do_memory_regions (void);
102*56bb7041Schristos 
103*56bb7041Schristos /* Exported variables.  */
104*56bb7041Schristos const char *output_target;
105*56bb7041Schristos lang_output_section_statement_type *abs_output_section;
106*56bb7041Schristos lang_statement_list_type lang_os_list;
107*56bb7041Schristos lang_statement_list_type *stat_ptr = &statement_list;
108*56bb7041Schristos /* Header for list of statements corresponding to files used in the final
109*56bb7041Schristos    executable.  This can be either object file specified on the command-line
110*56bb7041Schristos    or library member resolving an undefined reference.  Next pointer is in next
111*56bb7041Schristos    field of a lang_input_statement_type (reached via input_statement field in a
112*56bb7041Schristos    lang_statement_union).  */
113*56bb7041Schristos lang_statement_list_type file_chain = { NULL, NULL };
114*56bb7041Schristos /* Header for list of statements corresponding to files specified on the
115*56bb7041Schristos    command-line for linking.  It thus contains real object files and archive
116*56bb7041Schristos    but not archive members.  Next pointer is in next_real_file field of a
117*56bb7041Schristos    lang_input_statement_type statement (reached via input_statement field in a
118*56bb7041Schristos    lang_statement_union).  */
119*56bb7041Schristos lang_statement_list_type input_file_chain;
120*56bb7041Schristos static const char *current_input_file;
121*56bb7041Schristos struct bfd_elf_dynamic_list **current_dynamic_list_p;
122*56bb7041Schristos struct bfd_sym_chain entry_symbol = { NULL, NULL };
123*56bb7041Schristos const char *entry_section = ".text";
124*56bb7041Schristos struct lang_input_statement_flags input_flags;
125*56bb7041Schristos bfd_boolean entry_from_cmdline;
126*56bb7041Schristos bfd_boolean lang_has_input_file = FALSE;
127*56bb7041Schristos bfd_boolean had_output_filename = FALSE;
128*56bb7041Schristos bfd_boolean lang_float_flag = FALSE;
129*56bb7041Schristos bfd_boolean delete_output_file_on_failure = FALSE;
130*56bb7041Schristos struct lang_phdr *lang_phdr_list;
131*56bb7041Schristos struct lang_nocrossrefs *nocrossref_list;
132*56bb7041Schristos struct asneeded_minfo **asneeded_list_tail;
133*56bb7041Schristos #ifdef ENABLE_LIBCTF
134*56bb7041Schristos static ctf_file_t *ctf_output;
135*56bb7041Schristos #endif
136*56bb7041Schristos 
137*56bb7041Schristos /* Functions that traverse the linker script and might evaluate
138*56bb7041Schristos    DEFINED() need to increment this at the start of the traversal.  */
139*56bb7041Schristos int lang_statement_iteration = 0;
140*56bb7041Schristos 
141*56bb7041Schristos /* Count times through one_lang_size_sections_pass after mark phase.  */
142*56bb7041Schristos static int lang_sizing_iteration = 0;
143*56bb7041Schristos 
144*56bb7041Schristos /* Return TRUE if the PATTERN argument is a wildcard pattern.
145*56bb7041Schristos    Although backslashes are treated specially if a pattern contains
146*56bb7041Schristos    wildcards, we do not consider the mere presence of a backslash to
147*56bb7041Schristos    be enough to cause the pattern to be treated as a wildcard.
148*56bb7041Schristos    That lets us handle DOS filenames more naturally.  */
149*56bb7041Schristos #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
150*56bb7041Schristos 
151*56bb7041Schristos #define new_stat(x, y) \
152*56bb7041Schristos   (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
153*56bb7041Schristos 
154*56bb7041Schristos #define outside_section_address(q) \
155*56bb7041Schristos   ((q)->output_offset + (q)->output_section->vma)
156*56bb7041Schristos 
157*56bb7041Schristos #define outside_symbol_address(q) \
158*56bb7041Schristos   ((q)->value + outside_section_address (q->section))
159*56bb7041Schristos 
160*56bb7041Schristos /* CTF sections smaller than this are not compressed: compression of
161*56bb7041Schristos    dictionaries this small doesn't gain much, and this lets consumers mmap the
162*56bb7041Schristos    sections directly out of the ELF file and use them with no decompression
163*56bb7041Schristos    overhead if they want to.  */
164*56bb7041Schristos #define CTF_COMPRESSION_THRESHOLD 4096
165*56bb7041Schristos 
166*56bb7041Schristos void *
stat_alloc(size_t size)167*56bb7041Schristos stat_alloc (size_t size)
168*56bb7041Schristos {
169*56bb7041Schristos   return obstack_alloc (&stat_obstack, size);
170*56bb7041Schristos }
171*56bb7041Schristos 
172*56bb7041Schristos static int
name_match(const char * pattern,const char * name)173*56bb7041Schristos name_match (const char *pattern, const char *name)
174*56bb7041Schristos {
175*56bb7041Schristos   if (wildcardp (pattern))
176*56bb7041Schristos     return fnmatch (pattern, name, 0);
177*56bb7041Schristos   return strcmp (pattern, name);
178*56bb7041Schristos }
179*56bb7041Schristos 
180*56bb7041Schristos static char *
ldirname(const char * name)181*56bb7041Schristos ldirname (const char *name)
182*56bb7041Schristos {
183*56bb7041Schristos   const char *base = lbasename (name);
184*56bb7041Schristos   char *dirname;
185*56bb7041Schristos 
186*56bb7041Schristos   while (base > name && IS_DIR_SEPARATOR (base[-1]))
187*56bb7041Schristos     --base;
188*56bb7041Schristos   if (base == name)
189*56bb7041Schristos     return strdup (".");
190*56bb7041Schristos   dirname = strdup (name);
191*56bb7041Schristos   dirname[base - name] = '\0';
192*56bb7041Schristos   return dirname;
193*56bb7041Schristos }
194*56bb7041Schristos 
195*56bb7041Schristos /* If PATTERN is of the form archive:file, return a pointer to the
196*56bb7041Schristos    separator.  If not, return NULL.  */
197*56bb7041Schristos 
198*56bb7041Schristos static char *
archive_path(const char * pattern)199*56bb7041Schristos archive_path (const char *pattern)
200*56bb7041Schristos {
201*56bb7041Schristos   char *p = NULL;
202*56bb7041Schristos 
203*56bb7041Schristos   if (link_info.path_separator == 0)
204*56bb7041Schristos     return p;
205*56bb7041Schristos 
206*56bb7041Schristos   p = strchr (pattern, link_info.path_separator);
207*56bb7041Schristos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
208*56bb7041Schristos   if (p == NULL || link_info.path_separator != ':')
209*56bb7041Schristos     return p;
210*56bb7041Schristos 
211*56bb7041Schristos   /* Assume a match on the second char is part of drive specifier,
212*56bb7041Schristos      as in "c:\silly.dos".  */
213*56bb7041Schristos   if (p == pattern + 1 && ISALPHA (*pattern))
214*56bb7041Schristos     p = strchr (p + 1, link_info.path_separator);
215*56bb7041Schristos #endif
216*56bb7041Schristos   return p;
217*56bb7041Schristos }
218*56bb7041Schristos 
219*56bb7041Schristos /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
220*56bb7041Schristos    return whether F matches FILE_SPEC.  */
221*56bb7041Schristos 
222*56bb7041Schristos static bfd_boolean
input_statement_is_archive_path(const char * file_spec,char * sep,lang_input_statement_type * f)223*56bb7041Schristos input_statement_is_archive_path (const char *file_spec, char *sep,
224*56bb7041Schristos 				 lang_input_statement_type *f)
225*56bb7041Schristos {
226*56bb7041Schristos   bfd_boolean match = FALSE;
227*56bb7041Schristos 
228*56bb7041Schristos   if ((*(sep + 1) == 0
229*56bb7041Schristos        || name_match (sep + 1, f->filename) == 0)
230*56bb7041Schristos       && ((sep != file_spec)
231*56bb7041Schristos 	  == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
232*56bb7041Schristos     {
233*56bb7041Schristos       match = TRUE;
234*56bb7041Schristos 
235*56bb7041Schristos       if (sep != file_spec)
236*56bb7041Schristos 	{
237*56bb7041Schristos 	  const char *aname = bfd_get_filename (f->the_bfd->my_archive);
238*56bb7041Schristos 	  *sep = 0;
239*56bb7041Schristos 	  match = name_match (file_spec, aname) == 0;
240*56bb7041Schristos 	  *sep = link_info.path_separator;
241*56bb7041Schristos 	}
242*56bb7041Schristos     }
243*56bb7041Schristos   return match;
244*56bb7041Schristos }
245*56bb7041Schristos 
246*56bb7041Schristos static bfd_boolean
unique_section_p(const asection * sec,const lang_output_section_statement_type * os)247*56bb7041Schristos unique_section_p (const asection *sec,
248*56bb7041Schristos 		  const lang_output_section_statement_type *os)
249*56bb7041Schristos {
250*56bb7041Schristos   struct unique_sections *unam;
251*56bb7041Schristos   const char *secnam;
252*56bb7041Schristos 
253*56bb7041Schristos   if (!link_info.resolve_section_groups
254*56bb7041Schristos       && sec->owner != NULL
255*56bb7041Schristos       && bfd_is_group_section (sec->owner, sec))
256*56bb7041Schristos     return !(os != NULL
257*56bb7041Schristos 	     && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
258*56bb7041Schristos 
259*56bb7041Schristos   secnam = sec->name;
260*56bb7041Schristos   for (unam = unique_section_list; unam; unam = unam->next)
261*56bb7041Schristos     if (name_match (unam->name, secnam) == 0)
262*56bb7041Schristos       return TRUE;
263*56bb7041Schristos 
264*56bb7041Schristos   return FALSE;
265*56bb7041Schristos }
266*56bb7041Schristos 
267*56bb7041Schristos /* Generic traversal routines for finding matching sections.  */
268*56bb7041Schristos 
269*56bb7041Schristos /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
270*56bb7041Schristos    false.  */
271*56bb7041Schristos 
272*56bb7041Schristos static bfd_boolean
walk_wild_file_in_exclude_list(struct name_list * exclude_list,lang_input_statement_type * file)273*56bb7041Schristos walk_wild_file_in_exclude_list (struct name_list *exclude_list,
274*56bb7041Schristos 				lang_input_statement_type *file)
275*56bb7041Schristos {
276*56bb7041Schristos   struct name_list *list_tmp;
277*56bb7041Schristos 
278*56bb7041Schristos   for (list_tmp = exclude_list;
279*56bb7041Schristos        list_tmp;
280*56bb7041Schristos        list_tmp = list_tmp->next)
281*56bb7041Schristos     {
282*56bb7041Schristos       char *p = archive_path (list_tmp->name);
283*56bb7041Schristos 
284*56bb7041Schristos       if (p != NULL)
285*56bb7041Schristos 	{
286*56bb7041Schristos 	  if (input_statement_is_archive_path (list_tmp->name, p, file))
287*56bb7041Schristos 	    return TRUE;
288*56bb7041Schristos 	}
289*56bb7041Schristos 
290*56bb7041Schristos       else if (name_match (list_tmp->name, file->filename) == 0)
291*56bb7041Schristos 	return TRUE;
292*56bb7041Schristos 
293*56bb7041Schristos       /* FIXME: Perhaps remove the following at some stage?  Matching
294*56bb7041Schristos 	 unadorned archives like this was never documented and has
295*56bb7041Schristos 	 been superceded by the archive:path syntax.  */
296*56bb7041Schristos       else if (file->the_bfd != NULL
297*56bb7041Schristos 	       && file->the_bfd->my_archive != NULL
298*56bb7041Schristos 	       && name_match (list_tmp->name,
299*56bb7041Schristos 			      bfd_get_filename (file->the_bfd->my_archive)) == 0)
300*56bb7041Schristos 	return TRUE;
301*56bb7041Schristos     }
302*56bb7041Schristos 
303*56bb7041Schristos   return FALSE;
304*56bb7041Schristos }
305*56bb7041Schristos 
306*56bb7041Schristos /* Try processing a section against a wildcard.  This just calls
307*56bb7041Schristos    the callback unless the filename exclusion list is present
308*56bb7041Schristos    and excludes the file.  It's hardly ever present so this
309*56bb7041Schristos    function is very fast.  */
310*56bb7041Schristos 
311*56bb7041Schristos static void
walk_wild_consider_section(lang_wild_statement_type * ptr,lang_input_statement_type * file,asection * s,struct wildcard_list * sec,callback_t callback,void * data)312*56bb7041Schristos walk_wild_consider_section (lang_wild_statement_type *ptr,
313*56bb7041Schristos 			    lang_input_statement_type *file,
314*56bb7041Schristos 			    asection *s,
315*56bb7041Schristos 			    struct wildcard_list *sec,
316*56bb7041Schristos 			    callback_t callback,
317*56bb7041Schristos 			    void *data)
318*56bb7041Schristos {
319*56bb7041Schristos   /* Don't process sections from files which were excluded.  */
320*56bb7041Schristos   if (walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, file))
321*56bb7041Schristos     return;
322*56bb7041Schristos 
323*56bb7041Schristos   (*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
324*56bb7041Schristos }
325*56bb7041Schristos 
326*56bb7041Schristos /* Lowest common denominator routine that can handle everything correctly,
327*56bb7041Schristos    but slowly.  */
328*56bb7041Schristos 
329*56bb7041Schristos static void
walk_wild_section_general(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)330*56bb7041Schristos walk_wild_section_general (lang_wild_statement_type *ptr,
331*56bb7041Schristos 			   lang_input_statement_type *file,
332*56bb7041Schristos 			   callback_t callback,
333*56bb7041Schristos 			   void *data)
334*56bb7041Schristos {
335*56bb7041Schristos   asection *s;
336*56bb7041Schristos   struct wildcard_list *sec;
337*56bb7041Schristos 
338*56bb7041Schristos   for (s = file->the_bfd->sections; s != NULL; s = s->next)
339*56bb7041Schristos     {
340*56bb7041Schristos       sec = ptr->section_list;
341*56bb7041Schristos       if (sec == NULL)
342*56bb7041Schristos 	(*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
343*56bb7041Schristos 
344*56bb7041Schristos       while (sec != NULL)
345*56bb7041Schristos 	{
346*56bb7041Schristos 	  bfd_boolean skip = FALSE;
347*56bb7041Schristos 
348*56bb7041Schristos 	  if (sec->spec.name != NULL)
349*56bb7041Schristos 	    {
350*56bb7041Schristos 	      const char *sname = bfd_section_name (s);
351*56bb7041Schristos 
352*56bb7041Schristos 	      skip = name_match (sec->spec.name, sname) != 0;
353*56bb7041Schristos 	    }
354*56bb7041Schristos 
355*56bb7041Schristos 	  if (!skip)
356*56bb7041Schristos 	    walk_wild_consider_section (ptr, file, s, sec, callback, data);
357*56bb7041Schristos 
358*56bb7041Schristos 	  sec = sec->next;
359*56bb7041Schristos 	}
360*56bb7041Schristos     }
361*56bb7041Schristos }
362*56bb7041Schristos 
363*56bb7041Schristos /* Routines to find a single section given its name.  If there's more
364*56bb7041Schristos    than one section with that name, we report that.  */
365*56bb7041Schristos 
366*56bb7041Schristos typedef struct
367*56bb7041Schristos {
368*56bb7041Schristos   asection *found_section;
369*56bb7041Schristos   bfd_boolean multiple_sections_found;
370*56bb7041Schristos } section_iterator_callback_data;
371*56bb7041Schristos 
372*56bb7041Schristos static bfd_boolean
section_iterator_callback(bfd * abfd ATTRIBUTE_UNUSED,asection * s,void * data)373*56bb7041Schristos section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
374*56bb7041Schristos {
375*56bb7041Schristos   section_iterator_callback_data *d = (section_iterator_callback_data *) data;
376*56bb7041Schristos 
377*56bb7041Schristos   if (d->found_section != NULL)
378*56bb7041Schristos     {
379*56bb7041Schristos       d->multiple_sections_found = TRUE;
380*56bb7041Schristos       return TRUE;
381*56bb7041Schristos     }
382*56bb7041Schristos 
383*56bb7041Schristos   d->found_section = s;
384*56bb7041Schristos   return FALSE;
385*56bb7041Schristos }
386*56bb7041Schristos 
387*56bb7041Schristos static asection *
find_section(lang_input_statement_type * file,struct wildcard_list * sec,bfd_boolean * multiple_sections_found)388*56bb7041Schristos find_section (lang_input_statement_type *file,
389*56bb7041Schristos 	      struct wildcard_list *sec,
390*56bb7041Schristos 	      bfd_boolean *multiple_sections_found)
391*56bb7041Schristos {
392*56bb7041Schristos   section_iterator_callback_data cb_data = { NULL, FALSE };
393*56bb7041Schristos 
394*56bb7041Schristos   bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
395*56bb7041Schristos 			      section_iterator_callback, &cb_data);
396*56bb7041Schristos   *multiple_sections_found = cb_data.multiple_sections_found;
397*56bb7041Schristos   return cb_data.found_section;
398*56bb7041Schristos }
399*56bb7041Schristos 
400*56bb7041Schristos /* Code for handling simple wildcards without going through fnmatch,
401*56bb7041Schristos    which can be expensive because of charset translations etc.  */
402*56bb7041Schristos 
403*56bb7041Schristos /* A simple wild is a literal string followed by a single '*',
404*56bb7041Schristos    where the literal part is at least 4 characters long.  */
405*56bb7041Schristos 
406*56bb7041Schristos static bfd_boolean
is_simple_wild(const char * name)407*56bb7041Schristos is_simple_wild (const char *name)
408*56bb7041Schristos {
409*56bb7041Schristos   size_t len = strcspn (name, "*?[");
410*56bb7041Schristos   return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
411*56bb7041Schristos }
412*56bb7041Schristos 
413*56bb7041Schristos static bfd_boolean
match_simple_wild(const char * pattern,const char * name)414*56bb7041Schristos match_simple_wild (const char *pattern, const char *name)
415*56bb7041Schristos {
416*56bb7041Schristos   /* The first four characters of the pattern are guaranteed valid
417*56bb7041Schristos      non-wildcard characters.  So we can go faster.  */
418*56bb7041Schristos   if (pattern[0] != name[0] || pattern[1] != name[1]
419*56bb7041Schristos       || pattern[2] != name[2] || pattern[3] != name[3])
420*56bb7041Schristos     return FALSE;
421*56bb7041Schristos 
422*56bb7041Schristos   pattern += 4;
423*56bb7041Schristos   name += 4;
424*56bb7041Schristos   while (*pattern != '*')
425*56bb7041Schristos     if (*name++ != *pattern++)
426*56bb7041Schristos       return FALSE;
427*56bb7041Schristos 
428*56bb7041Schristos   return TRUE;
429*56bb7041Schristos }
430*56bb7041Schristos 
431*56bb7041Schristos /* Return the numerical value of the init_priority attribute from
432*56bb7041Schristos    section name NAME.  */
433*56bb7041Schristos 
434*56bb7041Schristos static int
get_init_priority(const asection * sec)435*56bb7041Schristos get_init_priority (const asection *sec)
436*56bb7041Schristos {
437*56bb7041Schristos   const char *name = bfd_section_name (sec);
438*56bb7041Schristos   const char *dot;
439*56bb7041Schristos 
440*56bb7041Schristos   /* GCC uses the following section names for the init_priority
441*56bb7041Schristos      attribute with numerical values 101 to 65535 inclusive. A
442*56bb7041Schristos      lower value means a higher priority.
443*56bb7041Schristos 
444*56bb7041Schristos      1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
445*56bb7041Schristos 	decimal numerical value of the init_priority attribute.
446*56bb7041Schristos 	The order of execution in .init_array is forward and
447*56bb7041Schristos 	.fini_array is backward.
448*56bb7041Schristos      2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
449*56bb7041Schristos 	decimal numerical value of the init_priority attribute.
450*56bb7041Schristos 	The order of execution in .ctors is backward and .dtors
451*56bb7041Schristos 	is forward.
452*56bb7041Schristos 
453*56bb7041Schristos      .init_array.NNNNN sections would normally be placed in an output
454*56bb7041Schristos      .init_array section, .fini_array.NNNNN in .fini_array,
455*56bb7041Schristos      .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors.  This means
456*56bb7041Schristos      we should sort by increasing number (and could just use
457*56bb7041Schristos      SORT_BY_NAME in scripts).  However if .ctors.NNNNN sections are
458*56bb7041Schristos      being placed in .init_array (which may also contain
459*56bb7041Schristos      .init_array.NNNNN sections) or .dtors.NNNNN sections are being
460*56bb7041Schristos      placed in .fini_array then we need to extract the init_priority
461*56bb7041Schristos      attribute and sort on that.  */
462*56bb7041Schristos   dot = strrchr (name, '.');
463*56bb7041Schristos   if (dot != NULL && ISDIGIT (dot[1]))
464*56bb7041Schristos     {
465*56bb7041Schristos       char *end;
466*56bb7041Schristos       unsigned long init_priority = strtoul (dot + 1, &end, 10);
467*56bb7041Schristos       if (*end == 0)
468*56bb7041Schristos 	{
469*56bb7041Schristos 	  if (dot == name + 6
470*56bb7041Schristos 	      && (strncmp (name, ".ctors", 6) == 0
471*56bb7041Schristos 		  || strncmp (name, ".dtors", 6) == 0))
472*56bb7041Schristos 	    init_priority = 65535 - init_priority;
473*56bb7041Schristos 	  if (init_priority <= INT_MAX)
474*56bb7041Schristos 	    return init_priority;
475*56bb7041Schristos 	}
476*56bb7041Schristos     }
477*56bb7041Schristos   return -1;
478*56bb7041Schristos }
479*56bb7041Schristos 
480*56bb7041Schristos /* Compare sections ASEC and BSEC according to SORT.  */
481*56bb7041Schristos 
482*56bb7041Schristos static int
compare_section(sort_type sort,asection * asec,asection * bsec)483*56bb7041Schristos compare_section (sort_type sort, asection *asec, asection *bsec)
484*56bb7041Schristos {
485*56bb7041Schristos   int ret;
486*56bb7041Schristos   int a_priority, b_priority;
487*56bb7041Schristos 
488*56bb7041Schristos   switch (sort)
489*56bb7041Schristos     {
490*56bb7041Schristos     default:
491*56bb7041Schristos       abort ();
492*56bb7041Schristos 
493*56bb7041Schristos     case by_init_priority:
494*56bb7041Schristos       a_priority = get_init_priority (asec);
495*56bb7041Schristos       b_priority = get_init_priority (bsec);
496*56bb7041Schristos       if (a_priority < 0 || b_priority < 0)
497*56bb7041Schristos 	goto sort_by_name;
498*56bb7041Schristos       ret = a_priority - b_priority;
499*56bb7041Schristos       if (ret)
500*56bb7041Schristos 	break;
501*56bb7041Schristos       else
502*56bb7041Schristos 	goto sort_by_name;
503*56bb7041Schristos 
504*56bb7041Schristos     case by_alignment_name:
505*56bb7041Schristos       ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
506*56bb7041Schristos       if (ret)
507*56bb7041Schristos 	break;
508*56bb7041Schristos       /* Fall through.  */
509*56bb7041Schristos 
510*56bb7041Schristos     case by_name:
511*56bb7041Schristos     sort_by_name:
512*56bb7041Schristos       ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
513*56bb7041Schristos       break;
514*56bb7041Schristos 
515*56bb7041Schristos     case by_name_alignment:
516*56bb7041Schristos       ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
517*56bb7041Schristos       if (ret)
518*56bb7041Schristos 	break;
519*56bb7041Schristos       /* Fall through.  */
520*56bb7041Schristos 
521*56bb7041Schristos     case by_alignment:
522*56bb7041Schristos       ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
523*56bb7041Schristos       break;
524*56bb7041Schristos     }
525*56bb7041Schristos 
526*56bb7041Schristos   return ret;
527*56bb7041Schristos }
528*56bb7041Schristos 
529*56bb7041Schristos /* Build a Binary Search Tree to sort sections, unlike insertion sort
530*56bb7041Schristos    used in wild_sort(). BST is considerably faster if the number of
531*56bb7041Schristos    of sections are large.  */
532*56bb7041Schristos 
533*56bb7041Schristos static lang_section_bst_type **
wild_sort_fast(lang_wild_statement_type * wild,struct wildcard_list * sec,lang_input_statement_type * file ATTRIBUTE_UNUSED,asection * section)534*56bb7041Schristos wild_sort_fast (lang_wild_statement_type *wild,
535*56bb7041Schristos 		struct wildcard_list *sec,
536*56bb7041Schristos 		lang_input_statement_type *file ATTRIBUTE_UNUSED,
537*56bb7041Schristos 		asection *section)
538*56bb7041Schristos {
539*56bb7041Schristos   lang_section_bst_type **tree;
540*56bb7041Schristos 
541*56bb7041Schristos   tree = &wild->tree;
542*56bb7041Schristos   if (!wild->filenames_sorted
543*56bb7041Schristos       && (sec == NULL || sec->spec.sorted == none))
544*56bb7041Schristos     {
545*56bb7041Schristos       /* Append at the right end of tree.  */
546*56bb7041Schristos       while (*tree)
547*56bb7041Schristos 	tree = &((*tree)->right);
548*56bb7041Schristos       return tree;
549*56bb7041Schristos     }
550*56bb7041Schristos 
551*56bb7041Schristos   while (*tree)
552*56bb7041Schristos     {
553*56bb7041Schristos       /* Find the correct node to append this section.  */
554*56bb7041Schristos       if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
555*56bb7041Schristos 	tree = &((*tree)->left);
556*56bb7041Schristos       else
557*56bb7041Schristos 	tree = &((*tree)->right);
558*56bb7041Schristos     }
559*56bb7041Schristos 
560*56bb7041Schristos   return tree;
561*56bb7041Schristos }
562*56bb7041Schristos 
563*56bb7041Schristos /* Use wild_sort_fast to build a BST to sort sections.  */
564*56bb7041Schristos 
565*56bb7041Schristos static void
output_section_callback_fast(lang_wild_statement_type * ptr,struct wildcard_list * sec,asection * section,struct flag_info * sflag_list ATTRIBUTE_UNUSED,lang_input_statement_type * file,void * output)566*56bb7041Schristos output_section_callback_fast (lang_wild_statement_type *ptr,
567*56bb7041Schristos 			      struct wildcard_list *sec,
568*56bb7041Schristos 			      asection *section,
569*56bb7041Schristos 			      struct flag_info *sflag_list ATTRIBUTE_UNUSED,
570*56bb7041Schristos 			      lang_input_statement_type *file,
571*56bb7041Schristos 			      void *output)
572*56bb7041Schristos {
573*56bb7041Schristos   lang_section_bst_type *node;
574*56bb7041Schristos   lang_section_bst_type **tree;
575*56bb7041Schristos   lang_output_section_statement_type *os;
576*56bb7041Schristos 
577*56bb7041Schristos   os = (lang_output_section_statement_type *) output;
578*56bb7041Schristos 
579*56bb7041Schristos   if (unique_section_p (section, os))
580*56bb7041Schristos     return;
581*56bb7041Schristos 
582*56bb7041Schristos   node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
583*56bb7041Schristos   node->left = 0;
584*56bb7041Schristos   node->right = 0;
585*56bb7041Schristos   node->section = section;
586*56bb7041Schristos 
587*56bb7041Schristos   tree = wild_sort_fast (ptr, sec, file, section);
588*56bb7041Schristos   if (tree != NULL)
589*56bb7041Schristos     *tree = node;
590*56bb7041Schristos }
591*56bb7041Schristos 
592*56bb7041Schristos /* Convert a sorted sections' BST back to list form.  */
593*56bb7041Schristos 
594*56bb7041Schristos static void
output_section_callback_tree_to_list(lang_wild_statement_type * ptr,lang_section_bst_type * tree,void * output)595*56bb7041Schristos output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
596*56bb7041Schristos 				      lang_section_bst_type *tree,
597*56bb7041Schristos 				      void *output)
598*56bb7041Schristos {
599*56bb7041Schristos   if (tree->left)
600*56bb7041Schristos     output_section_callback_tree_to_list (ptr, tree->left, output);
601*56bb7041Schristos 
602*56bb7041Schristos   lang_add_section (&ptr->children, tree->section, NULL,
603*56bb7041Schristos 		    (lang_output_section_statement_type *) output);
604*56bb7041Schristos 
605*56bb7041Schristos   if (tree->right)
606*56bb7041Schristos     output_section_callback_tree_to_list (ptr, tree->right, output);
607*56bb7041Schristos 
608*56bb7041Schristos   free (tree);
609*56bb7041Schristos }
610*56bb7041Schristos 
611*56bb7041Schristos /* Specialized, optimized routines for handling different kinds of
612*56bb7041Schristos    wildcards */
613*56bb7041Schristos 
614*56bb7041Schristos static void
walk_wild_section_specs1_wild0(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)615*56bb7041Schristos walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
616*56bb7041Schristos 				lang_input_statement_type *file,
617*56bb7041Schristos 				callback_t callback,
618*56bb7041Schristos 				void *data)
619*56bb7041Schristos {
620*56bb7041Schristos   /* We can just do a hash lookup for the section with the right name.
621*56bb7041Schristos      But if that lookup discovers more than one section with the name
622*56bb7041Schristos      (should be rare), we fall back to the general algorithm because
623*56bb7041Schristos      we would otherwise have to sort the sections to make sure they
624*56bb7041Schristos      get processed in the bfd's order.  */
625*56bb7041Schristos   bfd_boolean multiple_sections_found;
626*56bb7041Schristos   struct wildcard_list *sec0 = ptr->handler_data[0];
627*56bb7041Schristos   asection *s0 = find_section (file, sec0, &multiple_sections_found);
628*56bb7041Schristos 
629*56bb7041Schristos   if (multiple_sections_found)
630*56bb7041Schristos     walk_wild_section_general (ptr, file, callback, data);
631*56bb7041Schristos   else if (s0)
632*56bb7041Schristos     walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
633*56bb7041Schristos }
634*56bb7041Schristos 
635*56bb7041Schristos static void
walk_wild_section_specs1_wild1(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)636*56bb7041Schristos walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
637*56bb7041Schristos 				lang_input_statement_type *file,
638*56bb7041Schristos 				callback_t callback,
639*56bb7041Schristos 				void *data)
640*56bb7041Schristos {
641*56bb7041Schristos   asection *s;
642*56bb7041Schristos   struct wildcard_list *wildsec0 = ptr->handler_data[0];
643*56bb7041Schristos 
644*56bb7041Schristos   for (s = file->the_bfd->sections; s != NULL; s = s->next)
645*56bb7041Schristos     {
646*56bb7041Schristos       const char *sname = bfd_section_name (s);
647*56bb7041Schristos       bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
648*56bb7041Schristos 
649*56bb7041Schristos       if (!skip)
650*56bb7041Schristos 	walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
651*56bb7041Schristos     }
652*56bb7041Schristos }
653*56bb7041Schristos 
654*56bb7041Schristos static void
walk_wild_section_specs2_wild1(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)655*56bb7041Schristos walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
656*56bb7041Schristos 				lang_input_statement_type *file,
657*56bb7041Schristos 				callback_t callback,
658*56bb7041Schristos 				void *data)
659*56bb7041Schristos {
660*56bb7041Schristos   asection *s;
661*56bb7041Schristos   struct wildcard_list *sec0 = ptr->handler_data[0];
662*56bb7041Schristos   struct wildcard_list *wildsec1 = ptr->handler_data[1];
663*56bb7041Schristos   bfd_boolean multiple_sections_found;
664*56bb7041Schristos   asection *s0 = find_section (file, sec0, &multiple_sections_found);
665*56bb7041Schristos 
666*56bb7041Schristos   if (multiple_sections_found)
667*56bb7041Schristos     {
668*56bb7041Schristos       walk_wild_section_general (ptr, file, callback, data);
669*56bb7041Schristos       return;
670*56bb7041Schristos     }
671*56bb7041Schristos 
672*56bb7041Schristos   /* Note that if the section was not found, s0 is NULL and
673*56bb7041Schristos      we'll simply never succeed the s == s0 test below.  */
674*56bb7041Schristos   for (s = file->the_bfd->sections; s != NULL; s = s->next)
675*56bb7041Schristos     {
676*56bb7041Schristos       /* Recall that in this code path, a section cannot satisfy more
677*56bb7041Schristos 	 than one spec, so if s == s0 then it cannot match
678*56bb7041Schristos 	 wildspec1.  */
679*56bb7041Schristos       if (s == s0)
680*56bb7041Schristos 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
681*56bb7041Schristos       else
682*56bb7041Schristos 	{
683*56bb7041Schristos 	  const char *sname = bfd_section_name (s);
684*56bb7041Schristos 	  bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
685*56bb7041Schristos 
686*56bb7041Schristos 	  if (!skip)
687*56bb7041Schristos 	    walk_wild_consider_section (ptr, file, s, wildsec1, callback,
688*56bb7041Schristos 					data);
689*56bb7041Schristos 	}
690*56bb7041Schristos     }
691*56bb7041Schristos }
692*56bb7041Schristos 
693*56bb7041Schristos static void
walk_wild_section_specs3_wild2(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)694*56bb7041Schristos walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
695*56bb7041Schristos 				lang_input_statement_type *file,
696*56bb7041Schristos 				callback_t callback,
697*56bb7041Schristos 				void *data)
698*56bb7041Schristos {
699*56bb7041Schristos   asection *s;
700*56bb7041Schristos   struct wildcard_list *sec0 = ptr->handler_data[0];
701*56bb7041Schristos   struct wildcard_list *wildsec1 = ptr->handler_data[1];
702*56bb7041Schristos   struct wildcard_list *wildsec2 = ptr->handler_data[2];
703*56bb7041Schristos   bfd_boolean multiple_sections_found;
704*56bb7041Schristos   asection *s0 = find_section (file, sec0, &multiple_sections_found);
705*56bb7041Schristos 
706*56bb7041Schristos   if (multiple_sections_found)
707*56bb7041Schristos     {
708*56bb7041Schristos       walk_wild_section_general (ptr, file, callback, data);
709*56bb7041Schristos       return;
710*56bb7041Schristos     }
711*56bb7041Schristos 
712*56bb7041Schristos   for (s = file->the_bfd->sections; s != NULL; s = s->next)
713*56bb7041Schristos     {
714*56bb7041Schristos       if (s == s0)
715*56bb7041Schristos 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
716*56bb7041Schristos       else
717*56bb7041Schristos 	{
718*56bb7041Schristos 	  const char *sname = bfd_section_name (s);
719*56bb7041Schristos 	  bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
720*56bb7041Schristos 
721*56bb7041Schristos 	  if (!skip)
722*56bb7041Schristos 	    walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
723*56bb7041Schristos 	  else
724*56bb7041Schristos 	    {
725*56bb7041Schristos 	      skip = !match_simple_wild (wildsec2->spec.name, sname);
726*56bb7041Schristos 	      if (!skip)
727*56bb7041Schristos 		walk_wild_consider_section (ptr, file, s, wildsec2, callback,
728*56bb7041Schristos 					    data);
729*56bb7041Schristos 	    }
730*56bb7041Schristos 	}
731*56bb7041Schristos     }
732*56bb7041Schristos }
733*56bb7041Schristos 
734*56bb7041Schristos static void
walk_wild_section_specs4_wild2(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)735*56bb7041Schristos walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
736*56bb7041Schristos 				lang_input_statement_type *file,
737*56bb7041Schristos 				callback_t callback,
738*56bb7041Schristos 				void *data)
739*56bb7041Schristos {
740*56bb7041Schristos   asection *s;
741*56bb7041Schristos   struct wildcard_list *sec0 = ptr->handler_data[0];
742*56bb7041Schristos   struct wildcard_list *sec1 = ptr->handler_data[1];
743*56bb7041Schristos   struct wildcard_list *wildsec2 = ptr->handler_data[2];
744*56bb7041Schristos   struct wildcard_list *wildsec3 = ptr->handler_data[3];
745*56bb7041Schristos   bfd_boolean multiple_sections_found;
746*56bb7041Schristos   asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
747*56bb7041Schristos 
748*56bb7041Schristos   if (multiple_sections_found)
749*56bb7041Schristos     {
750*56bb7041Schristos       walk_wild_section_general (ptr, file, callback, data);
751*56bb7041Schristos       return;
752*56bb7041Schristos     }
753*56bb7041Schristos 
754*56bb7041Schristos   s1 = find_section (file, sec1, &multiple_sections_found);
755*56bb7041Schristos   if (multiple_sections_found)
756*56bb7041Schristos     {
757*56bb7041Schristos       walk_wild_section_general (ptr, file, callback, data);
758*56bb7041Schristos       return;
759*56bb7041Schristos     }
760*56bb7041Schristos 
761*56bb7041Schristos   for (s = file->the_bfd->sections; s != NULL; s = s->next)
762*56bb7041Schristos     {
763*56bb7041Schristos       if (s == s0)
764*56bb7041Schristos 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
765*56bb7041Schristos       else
766*56bb7041Schristos 	if (s == s1)
767*56bb7041Schristos 	  walk_wild_consider_section (ptr, file, s, sec1, callback, data);
768*56bb7041Schristos 	else
769*56bb7041Schristos 	  {
770*56bb7041Schristos 	    const char *sname = bfd_section_name (s);
771*56bb7041Schristos 	    bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
772*56bb7041Schristos 						   sname);
773*56bb7041Schristos 
774*56bb7041Schristos 	    if (!skip)
775*56bb7041Schristos 	      walk_wild_consider_section (ptr, file, s, wildsec2, callback,
776*56bb7041Schristos 					  data);
777*56bb7041Schristos 	    else
778*56bb7041Schristos 	      {
779*56bb7041Schristos 		skip = !match_simple_wild (wildsec3->spec.name, sname);
780*56bb7041Schristos 		if (!skip)
781*56bb7041Schristos 		  walk_wild_consider_section (ptr, file, s, wildsec3,
782*56bb7041Schristos 					      callback, data);
783*56bb7041Schristos 	      }
784*56bb7041Schristos 	  }
785*56bb7041Schristos     }
786*56bb7041Schristos }
787*56bb7041Schristos 
788*56bb7041Schristos static void
walk_wild_section(lang_wild_statement_type * ptr,lang_input_statement_type * file,callback_t callback,void * data)789*56bb7041Schristos walk_wild_section (lang_wild_statement_type *ptr,
790*56bb7041Schristos 		   lang_input_statement_type *file,
791*56bb7041Schristos 		   callback_t callback,
792*56bb7041Schristos 		   void *data)
793*56bb7041Schristos {
794*56bb7041Schristos   if (file->flags.just_syms)
795*56bb7041Schristos     return;
796*56bb7041Schristos 
797*56bb7041Schristos   (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
798*56bb7041Schristos }
799*56bb7041Schristos 
800*56bb7041Schristos /* Returns TRUE when name1 is a wildcard spec that might match
801*56bb7041Schristos    something name2 can match.  We're conservative: we return FALSE
802*56bb7041Schristos    only if the prefixes of name1 and name2 are different up to the
803*56bb7041Schristos    first wildcard character.  */
804*56bb7041Schristos 
805*56bb7041Schristos static bfd_boolean
wild_spec_can_overlap(const char * name1,const char * name2)806*56bb7041Schristos wild_spec_can_overlap (const char *name1, const char *name2)
807*56bb7041Schristos {
808*56bb7041Schristos   size_t prefix1_len = strcspn (name1, "?*[");
809*56bb7041Schristos   size_t prefix2_len = strcspn (name2, "?*[");
810*56bb7041Schristos   size_t min_prefix_len;
811*56bb7041Schristos 
812*56bb7041Schristos   /* Note that if there is no wildcard character, then we treat the
813*56bb7041Schristos      terminating 0 as part of the prefix.  Thus ".text" won't match
814*56bb7041Schristos      ".text." or ".text.*", for example.  */
815*56bb7041Schristos   if (name1[prefix1_len] == '\0')
816*56bb7041Schristos     prefix1_len++;
817*56bb7041Schristos   if (name2[prefix2_len] == '\0')
818*56bb7041Schristos     prefix2_len++;
819*56bb7041Schristos 
820*56bb7041Schristos   min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
821*56bb7041Schristos 
822*56bb7041Schristos   return memcmp (name1, name2, min_prefix_len) == 0;
823*56bb7041Schristos }
824*56bb7041Schristos 
825*56bb7041Schristos /* Select specialized code to handle various kinds of wildcard
826*56bb7041Schristos    statements.  */
827*56bb7041Schristos 
828*56bb7041Schristos static void
analyze_walk_wild_section_handler(lang_wild_statement_type * ptr)829*56bb7041Schristos analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
830*56bb7041Schristos {
831*56bb7041Schristos   int sec_count = 0;
832*56bb7041Schristos   int wild_name_count = 0;
833*56bb7041Schristos   struct wildcard_list *sec;
834*56bb7041Schristos   int signature;
835*56bb7041Schristos   int data_counter;
836*56bb7041Schristos 
837*56bb7041Schristos   ptr->walk_wild_section_handler = walk_wild_section_general;
838*56bb7041Schristos   ptr->handler_data[0] = NULL;
839*56bb7041Schristos   ptr->handler_data[1] = NULL;
840*56bb7041Schristos   ptr->handler_data[2] = NULL;
841*56bb7041Schristos   ptr->handler_data[3] = NULL;
842*56bb7041Schristos   ptr->tree = NULL;
843*56bb7041Schristos 
844*56bb7041Schristos   /* Count how many wildcard_specs there are, and how many of those
845*56bb7041Schristos      actually use wildcards in the name.  Also, bail out if any of the
846*56bb7041Schristos      wildcard names are NULL. (Can this actually happen?
847*56bb7041Schristos      walk_wild_section used to test for it.)  And bail out if any
848*56bb7041Schristos      of the wildcards are more complex than a simple string
849*56bb7041Schristos      ending in a single '*'.  */
850*56bb7041Schristos   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
851*56bb7041Schristos     {
852*56bb7041Schristos       ++sec_count;
853*56bb7041Schristos       if (sec->spec.name == NULL)
854*56bb7041Schristos 	return;
855*56bb7041Schristos       if (wildcardp (sec->spec.name))
856*56bb7041Schristos 	{
857*56bb7041Schristos 	  ++wild_name_count;
858*56bb7041Schristos 	  if (!is_simple_wild (sec->spec.name))
859*56bb7041Schristos 	    return;
860*56bb7041Schristos 	}
861*56bb7041Schristos     }
862*56bb7041Schristos 
863*56bb7041Schristos   /* The zero-spec case would be easy to optimize but it doesn't
864*56bb7041Schristos      happen in practice.  Likewise, more than 4 specs doesn't
865*56bb7041Schristos      happen in practice.  */
866*56bb7041Schristos   if (sec_count == 0 || sec_count > 4)
867*56bb7041Schristos     return;
868*56bb7041Schristos 
869*56bb7041Schristos   /* Check that no two specs can match the same section.  */
870*56bb7041Schristos   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
871*56bb7041Schristos     {
872*56bb7041Schristos       struct wildcard_list *sec2;
873*56bb7041Schristos       for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
874*56bb7041Schristos 	{
875*56bb7041Schristos 	  if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
876*56bb7041Schristos 	    return;
877*56bb7041Schristos 	}
878*56bb7041Schristos     }
879*56bb7041Schristos 
880*56bb7041Schristos   signature = (sec_count << 8) + wild_name_count;
881*56bb7041Schristos   switch (signature)
882*56bb7041Schristos     {
883*56bb7041Schristos     case 0x0100:
884*56bb7041Schristos       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
885*56bb7041Schristos       break;
886*56bb7041Schristos     case 0x0101:
887*56bb7041Schristos       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
888*56bb7041Schristos       break;
889*56bb7041Schristos     case 0x0201:
890*56bb7041Schristos       ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
891*56bb7041Schristos       break;
892*56bb7041Schristos     case 0x0302:
893*56bb7041Schristos       ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
894*56bb7041Schristos       break;
895*56bb7041Schristos     case 0x0402:
896*56bb7041Schristos       ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
897*56bb7041Schristos       break;
898*56bb7041Schristos     default:
899*56bb7041Schristos       return;
900*56bb7041Schristos     }
901*56bb7041Schristos 
902*56bb7041Schristos   /* Now fill the data array with pointers to the specs, first the
903*56bb7041Schristos      specs with non-wildcard names, then the specs with wildcard
904*56bb7041Schristos      names.  It's OK to process the specs in different order from the
905*56bb7041Schristos      given order, because we've already determined that no section
906*56bb7041Schristos      will match more than one spec.  */
907*56bb7041Schristos   data_counter = 0;
908*56bb7041Schristos   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
909*56bb7041Schristos     if (!wildcardp (sec->spec.name))
910*56bb7041Schristos       ptr->handler_data[data_counter++] = sec;
911*56bb7041Schristos   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
912*56bb7041Schristos     if (wildcardp (sec->spec.name))
913*56bb7041Schristos       ptr->handler_data[data_counter++] = sec;
914*56bb7041Schristos }
915*56bb7041Schristos 
916*56bb7041Schristos /* Handle a wild statement for a single file F.  */
917*56bb7041Schristos 
918*56bb7041Schristos static void
walk_wild_file(lang_wild_statement_type * s,lang_input_statement_type * f,callback_t callback,void * data)919*56bb7041Schristos walk_wild_file (lang_wild_statement_type *s,
920*56bb7041Schristos 		lang_input_statement_type *f,
921*56bb7041Schristos 		callback_t callback,
922*56bb7041Schristos 		void *data)
923*56bb7041Schristos {
924*56bb7041Schristos   if (walk_wild_file_in_exclude_list (s->exclude_name_list, f))
925*56bb7041Schristos     return;
926*56bb7041Schristos 
927*56bb7041Schristos   if (f->the_bfd == NULL
928*56bb7041Schristos       || !bfd_check_format (f->the_bfd, bfd_archive))
929*56bb7041Schristos     walk_wild_section (s, f, callback, data);
930*56bb7041Schristos   else
931*56bb7041Schristos     {
932*56bb7041Schristos       bfd *member;
933*56bb7041Schristos 
934*56bb7041Schristos       /* This is an archive file.  We must map each member of the
935*56bb7041Schristos 	 archive separately.  */
936*56bb7041Schristos       member = bfd_openr_next_archived_file (f->the_bfd, NULL);
937*56bb7041Schristos       while (member != NULL)
938*56bb7041Schristos 	{
939*56bb7041Schristos 	  /* When lookup_name is called, it will call the add_symbols
940*56bb7041Schristos 	     entry point for the archive.  For each element of the
941*56bb7041Schristos 	     archive which is included, BFD will call ldlang_add_file,
942*56bb7041Schristos 	     which will set the usrdata field of the member to the
943*56bb7041Schristos 	     lang_input_statement.  */
944*56bb7041Schristos 	  if (bfd_usrdata (member) != NULL)
945*56bb7041Schristos 	    walk_wild_section (s, bfd_usrdata (member), callback, data);
946*56bb7041Schristos 
947*56bb7041Schristos 	  member = bfd_openr_next_archived_file (f->the_bfd, member);
948*56bb7041Schristos 	}
949*56bb7041Schristos     }
950*56bb7041Schristos }
951*56bb7041Schristos 
952*56bb7041Schristos static void
walk_wild(lang_wild_statement_type * s,callback_t callback,void * data)953*56bb7041Schristos walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
954*56bb7041Schristos {
955*56bb7041Schristos   const char *file_spec = s->filename;
956*56bb7041Schristos   char *p;
957*56bb7041Schristos 
958*56bb7041Schristos   if (file_spec == NULL)
959*56bb7041Schristos     {
960*56bb7041Schristos       /* Perform the iteration over all files in the list.  */
961*56bb7041Schristos       LANG_FOR_EACH_INPUT_STATEMENT (f)
962*56bb7041Schristos 	{
963*56bb7041Schristos 	  walk_wild_file (s, f, callback, data);
964*56bb7041Schristos 	}
965*56bb7041Schristos     }
966*56bb7041Schristos   else if ((p = archive_path (file_spec)) != NULL)
967*56bb7041Schristos     {
968*56bb7041Schristos       LANG_FOR_EACH_INPUT_STATEMENT (f)
969*56bb7041Schristos 	{
970*56bb7041Schristos 	  if (input_statement_is_archive_path (file_spec, p, f))
971*56bb7041Schristos 	    walk_wild_file (s, f, callback, data);
972*56bb7041Schristos 	}
973*56bb7041Schristos     }
974*56bb7041Schristos   else if (wildcardp (file_spec))
975*56bb7041Schristos     {
976*56bb7041Schristos       LANG_FOR_EACH_INPUT_STATEMENT (f)
977*56bb7041Schristos 	{
978*56bb7041Schristos 	  if (fnmatch (file_spec, f->filename, 0) == 0)
979*56bb7041Schristos 	    walk_wild_file (s, f, callback, data);
980*56bb7041Schristos 	}
981*56bb7041Schristos     }
982*56bb7041Schristos   else
983*56bb7041Schristos     {
984*56bb7041Schristos       lang_input_statement_type *f;
985*56bb7041Schristos 
986*56bb7041Schristos       /* Perform the iteration over a single file.  */
987*56bb7041Schristos       f = lookup_name (file_spec);
988*56bb7041Schristos       if (f)
989*56bb7041Schristos 	walk_wild_file (s, f, callback, data);
990*56bb7041Schristos     }
991*56bb7041Schristos }
992*56bb7041Schristos 
993*56bb7041Schristos /* lang_for_each_statement walks the parse tree and calls the provided
994*56bb7041Schristos    function for each node, except those inside output section statements
995*56bb7041Schristos    with constraint set to -1.  */
996*56bb7041Schristos 
997*56bb7041Schristos void
lang_for_each_statement_worker(void (* func)(lang_statement_union_type *),lang_statement_union_type * s)998*56bb7041Schristos lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
999*56bb7041Schristos 				lang_statement_union_type *s)
1000*56bb7041Schristos {
1001*56bb7041Schristos   for (; s != NULL; s = s->header.next)
1002*56bb7041Schristos     {
1003*56bb7041Schristos       func (s);
1004*56bb7041Schristos 
1005*56bb7041Schristos       switch (s->header.type)
1006*56bb7041Schristos 	{
1007*56bb7041Schristos 	case lang_constructors_statement_enum:
1008*56bb7041Schristos 	  lang_for_each_statement_worker (func, constructor_list.head);
1009*56bb7041Schristos 	  break;
1010*56bb7041Schristos 	case lang_output_section_statement_enum:
1011*56bb7041Schristos 	  if (s->output_section_statement.constraint != -1)
1012*56bb7041Schristos 	    lang_for_each_statement_worker
1013*56bb7041Schristos 	      (func, s->output_section_statement.children.head);
1014*56bb7041Schristos 	  break;
1015*56bb7041Schristos 	case lang_wild_statement_enum:
1016*56bb7041Schristos 	  lang_for_each_statement_worker (func,
1017*56bb7041Schristos 					  s->wild_statement.children.head);
1018*56bb7041Schristos 	  break;
1019*56bb7041Schristos 	case lang_group_statement_enum:
1020*56bb7041Schristos 	  lang_for_each_statement_worker (func,
1021*56bb7041Schristos 					  s->group_statement.children.head);
1022*56bb7041Schristos 	  break;
1023*56bb7041Schristos 	case lang_data_statement_enum:
1024*56bb7041Schristos 	case lang_reloc_statement_enum:
1025*56bb7041Schristos 	case lang_object_symbols_statement_enum:
1026*56bb7041Schristos 	case lang_output_statement_enum:
1027*56bb7041Schristos 	case lang_target_statement_enum:
1028*56bb7041Schristos 	case lang_input_section_enum:
1029*56bb7041Schristos 	case lang_input_statement_enum:
1030*56bb7041Schristos 	case lang_assignment_statement_enum:
1031*56bb7041Schristos 	case lang_padding_statement_enum:
1032*56bb7041Schristos 	case lang_address_statement_enum:
1033*56bb7041Schristos 	case lang_fill_statement_enum:
1034*56bb7041Schristos 	case lang_insert_statement_enum:
1035*56bb7041Schristos 	  break;
1036*56bb7041Schristos 	default:
1037*56bb7041Schristos 	  FAIL ();
1038*56bb7041Schristos 	  break;
1039*56bb7041Schristos 	}
1040*56bb7041Schristos     }
1041*56bb7041Schristos }
1042*56bb7041Schristos 
1043*56bb7041Schristos void
lang_for_each_statement(void (* func)(lang_statement_union_type *))1044*56bb7041Schristos lang_for_each_statement (void (*func) (lang_statement_union_type *))
1045*56bb7041Schristos {
1046*56bb7041Schristos   lang_for_each_statement_worker (func, statement_list.head);
1047*56bb7041Schristos }
1048*56bb7041Schristos 
1049*56bb7041Schristos /*----------------------------------------------------------------------*/
1050*56bb7041Schristos 
1051*56bb7041Schristos void
lang_list_init(lang_statement_list_type * list)1052*56bb7041Schristos lang_list_init (lang_statement_list_type *list)
1053*56bb7041Schristos {
1054*56bb7041Schristos   list->head = NULL;
1055*56bb7041Schristos   list->tail = &list->head;
1056*56bb7041Schristos }
1057*56bb7041Schristos 
1058*56bb7041Schristos static void
lang_statement_append(lang_statement_list_type * list,void * element,void * field)1059*56bb7041Schristos lang_statement_append (lang_statement_list_type *list,
1060*56bb7041Schristos 		       void *element,
1061*56bb7041Schristos 		       void *field)
1062*56bb7041Schristos {
1063*56bb7041Schristos   *(list->tail) = element;
1064*56bb7041Schristos   list->tail = field;
1065*56bb7041Schristos }
1066*56bb7041Schristos 
1067*56bb7041Schristos void
push_stat_ptr(lang_statement_list_type * new_ptr)1068*56bb7041Schristos push_stat_ptr (lang_statement_list_type *new_ptr)
1069*56bb7041Schristos {
1070*56bb7041Schristos   if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
1071*56bb7041Schristos     abort ();
1072*56bb7041Schristos   *stat_save_ptr++ = stat_ptr;
1073*56bb7041Schristos   stat_ptr = new_ptr;
1074*56bb7041Schristos }
1075*56bb7041Schristos 
1076*56bb7041Schristos void
pop_stat_ptr(void)1077*56bb7041Schristos pop_stat_ptr (void)
1078*56bb7041Schristos {
1079*56bb7041Schristos   if (stat_save_ptr <= stat_save)
1080*56bb7041Schristos     abort ();
1081*56bb7041Schristos   stat_ptr = *--stat_save_ptr;
1082*56bb7041Schristos }
1083*56bb7041Schristos 
1084*56bb7041Schristos /* Build a new statement node for the parse tree.  */
1085*56bb7041Schristos 
1086*56bb7041Schristos static lang_statement_union_type *
new_statement(enum statement_enum type,size_t size,lang_statement_list_type * list)1087*56bb7041Schristos new_statement (enum statement_enum type,
1088*56bb7041Schristos 	       size_t size,
1089*56bb7041Schristos 	       lang_statement_list_type *list)
1090*56bb7041Schristos {
1091*56bb7041Schristos   lang_statement_union_type *new_stmt;
1092*56bb7041Schristos 
1093*56bb7041Schristos   new_stmt = stat_alloc (size);
1094*56bb7041Schristos   new_stmt->header.type = type;
1095*56bb7041Schristos   new_stmt->header.next = NULL;
1096*56bb7041Schristos   lang_statement_append (list, new_stmt, &new_stmt->header.next);
1097*56bb7041Schristos   return new_stmt;
1098*56bb7041Schristos }
1099*56bb7041Schristos 
1100*56bb7041Schristos /* Build a new input file node for the language.  There are several
1101*56bb7041Schristos    ways in which we treat an input file, eg, we only look at symbols,
1102*56bb7041Schristos    or prefix it with a -l etc.
1103*56bb7041Schristos 
1104*56bb7041Schristos    We can be supplied with requests for input files more than once;
1105*56bb7041Schristos    they may, for example be split over several lines like foo.o(.text)
1106*56bb7041Schristos    foo.o(.data) etc, so when asked for a file we check that we haven't
1107*56bb7041Schristos    got it already so we don't duplicate the bfd.  */
1108*56bb7041Schristos 
1109*56bb7041Schristos static lang_input_statement_type *
new_afile(const char * name,lang_input_file_enum_type file_type,const char * target,const char * from_filename)1110*56bb7041Schristos new_afile (const char *name,
1111*56bb7041Schristos 	   lang_input_file_enum_type file_type,
1112*56bb7041Schristos 	   const char *target,
1113*56bb7041Schristos 	   const char *from_filename)
1114*56bb7041Schristos {
1115*56bb7041Schristos   lang_input_statement_type *p;
1116*56bb7041Schristos 
1117*56bb7041Schristos   lang_has_input_file = TRUE;
1118*56bb7041Schristos 
1119*56bb7041Schristos   p = new_stat (lang_input_statement, stat_ptr);
1120*56bb7041Schristos   memset (&p->the_bfd, 0,
1121*56bb7041Schristos 	  sizeof (*p) - offsetof (lang_input_statement_type, the_bfd));
1122*56bb7041Schristos   p->extra_search_path = NULL;
1123*56bb7041Schristos   p->target = target;
1124*56bb7041Schristos   p->flags.dynamic = input_flags.dynamic;
1125*56bb7041Schristos   p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic;
1126*56bb7041Schristos   p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular;
1127*56bb7041Schristos   p->flags.whole_archive = input_flags.whole_archive;
1128*56bb7041Schristos   p->flags.sysrooted = input_flags.sysrooted;
1129*56bb7041Schristos 
1130*56bb7041Schristos   switch (file_type)
1131*56bb7041Schristos     {
1132*56bb7041Schristos     case lang_input_file_is_symbols_only_enum:
1133*56bb7041Schristos       p->filename = name;
1134*56bb7041Schristos       p->local_sym_name = name;
1135*56bb7041Schristos       p->flags.real = TRUE;
1136*56bb7041Schristos       p->flags.just_syms = TRUE;
1137*56bb7041Schristos       break;
1138*56bb7041Schristos     case lang_input_file_is_fake_enum:
1139*56bb7041Schristos       p->filename = name;
1140*56bb7041Schristos       p->local_sym_name = name;
1141*56bb7041Schristos       break;
1142*56bb7041Schristos     case lang_input_file_is_l_enum:
1143*56bb7041Schristos       if (name[0] == ':' && name[1] != '\0')
1144*56bb7041Schristos 	{
1145*56bb7041Schristos 	  p->filename = name + 1;
1146*56bb7041Schristos 	  p->flags.full_name_provided = TRUE;
1147*56bb7041Schristos 	}
1148*56bb7041Schristos       else
1149*56bb7041Schristos 	p->filename = name;
1150*56bb7041Schristos       p->local_sym_name = concat ("-l", name, (const char *) NULL);
1151*56bb7041Schristos       p->flags.maybe_archive = TRUE;
1152*56bb7041Schristos       p->flags.real = TRUE;
1153*56bb7041Schristos       p->flags.search_dirs = TRUE;
1154*56bb7041Schristos       break;
1155*56bb7041Schristos     case lang_input_file_is_marker_enum:
1156*56bb7041Schristos       p->filename = name;
1157*56bb7041Schristos       p->local_sym_name = name;
1158*56bb7041Schristos       p->flags.search_dirs = TRUE;
1159*56bb7041Schristos       break;
1160*56bb7041Schristos     case lang_input_file_is_search_file_enum:
1161*56bb7041Schristos       p->filename = name;
1162*56bb7041Schristos       p->local_sym_name = name;
1163*56bb7041Schristos       /* If name is a relative path, search the directory of the current linker
1164*56bb7041Schristos          script first. */
1165*56bb7041Schristos       if (from_filename && !IS_ABSOLUTE_PATH (name))
1166*56bb7041Schristos         p->extra_search_path = ldirname (from_filename);
1167*56bb7041Schristos       p->flags.real = TRUE;
1168*56bb7041Schristos       p->flags.search_dirs = TRUE;
1169*56bb7041Schristos       break;
1170*56bb7041Schristos     case lang_input_file_is_file_enum:
1171*56bb7041Schristos       p->filename = name;
1172*56bb7041Schristos       p->local_sym_name = name;
1173*56bb7041Schristos       p->flags.real = TRUE;
1174*56bb7041Schristos       break;
1175*56bb7041Schristos     default:
1176*56bb7041Schristos       FAIL ();
1177*56bb7041Schristos     }
1178*56bb7041Schristos 
1179*56bb7041Schristos   lang_statement_append (&input_file_chain, p, &p->next_real_file);
1180*56bb7041Schristos   return p;
1181*56bb7041Schristos }
1182*56bb7041Schristos 
1183*56bb7041Schristos lang_input_statement_type *
lang_add_input_file(const char * name,lang_input_file_enum_type file_type,const char * target)1184*56bb7041Schristos lang_add_input_file (const char *name,
1185*56bb7041Schristos 		     lang_input_file_enum_type file_type,
1186*56bb7041Schristos 		     const char *target)
1187*56bb7041Schristos {
1188*56bb7041Schristos   if (name != NULL
1189*56bb7041Schristos       && (*name == '=' || CONST_STRNEQ (name, "$SYSROOT")))
1190*56bb7041Schristos     {
1191*56bb7041Schristos       lang_input_statement_type *ret;
1192*56bb7041Schristos       char *sysrooted_name
1193*56bb7041Schristos 	= concat (ld_sysroot,
1194*56bb7041Schristos 		  name + (*name == '=' ? 1 : strlen ("$SYSROOT")),
1195*56bb7041Schristos 		  (const char *) NULL);
1196*56bb7041Schristos 
1197*56bb7041Schristos       /* We've now forcibly prepended the sysroot, making the input
1198*56bb7041Schristos 	 file independent of the context.  Therefore, temporarily
1199*56bb7041Schristos 	 force a non-sysrooted context for this statement, so it won't
1200*56bb7041Schristos 	 get the sysroot prepended again when opened.  (N.B. if it's a
1201*56bb7041Schristos 	 script, any child nodes with input files starting with "/"
1202*56bb7041Schristos 	 will be handled as "sysrooted" as they'll be found to be
1203*56bb7041Schristos 	 within the sysroot subdirectory.)  */
1204*56bb7041Schristos       unsigned int outer_sysrooted = input_flags.sysrooted;
1205*56bb7041Schristos       input_flags.sysrooted = 0;
1206*56bb7041Schristos       ret = new_afile (sysrooted_name, file_type, target, NULL);
1207*56bb7041Schristos       input_flags.sysrooted = outer_sysrooted;
1208*56bb7041Schristos       return ret;
1209*56bb7041Schristos     }
1210*56bb7041Schristos 
1211*56bb7041Schristos   return new_afile (name, file_type, target, current_input_file);
1212*56bb7041Schristos }
1213*56bb7041Schristos 
1214*56bb7041Schristos struct out_section_hash_entry
1215*56bb7041Schristos {
1216*56bb7041Schristos   struct bfd_hash_entry root;
1217*56bb7041Schristos   lang_statement_union_type s;
1218*56bb7041Schristos };
1219*56bb7041Schristos 
1220*56bb7041Schristos /* The hash table.  */
1221*56bb7041Schristos 
1222*56bb7041Schristos static struct bfd_hash_table output_section_statement_table;
1223*56bb7041Schristos 
1224*56bb7041Schristos /* Support routines for the hash table used by lang_output_section_find,
1225*56bb7041Schristos    initialize the table, fill in an entry and remove the table.  */
1226*56bb7041Schristos 
1227*56bb7041Schristos static struct bfd_hash_entry *
output_section_statement_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)1228*56bb7041Schristos output_section_statement_newfunc (struct bfd_hash_entry *entry,
1229*56bb7041Schristos 				  struct bfd_hash_table *table,
1230*56bb7041Schristos 				  const char *string)
1231*56bb7041Schristos {
1232*56bb7041Schristos   lang_output_section_statement_type **nextp;
1233*56bb7041Schristos   struct out_section_hash_entry *ret;
1234*56bb7041Schristos 
1235*56bb7041Schristos   if (entry == NULL)
1236*56bb7041Schristos     {
1237*56bb7041Schristos       entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
1238*56bb7041Schristos 							   sizeof (*ret));
1239*56bb7041Schristos       if (entry == NULL)
1240*56bb7041Schristos 	return entry;
1241*56bb7041Schristos     }
1242*56bb7041Schristos 
1243*56bb7041Schristos   entry = bfd_hash_newfunc (entry, table, string);
1244*56bb7041Schristos   if (entry == NULL)
1245*56bb7041Schristos     return entry;
1246*56bb7041Schristos 
1247*56bb7041Schristos   ret = (struct out_section_hash_entry *) entry;
1248*56bb7041Schristos   memset (&ret->s, 0, sizeof (ret->s));
1249*56bb7041Schristos   ret->s.header.type = lang_output_section_statement_enum;
1250*56bb7041Schristos   ret->s.output_section_statement.subsection_alignment = NULL;
1251*56bb7041Schristos   ret->s.output_section_statement.section_alignment = NULL;
1252*56bb7041Schristos   ret->s.output_section_statement.block_value = 1;
1253*56bb7041Schristos   lang_list_init (&ret->s.output_section_statement.children);
1254*56bb7041Schristos   lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1255*56bb7041Schristos 
1256*56bb7041Schristos   /* For every output section statement added to the list, except the
1257*56bb7041Schristos      first one, lang_os_list.tail points to the "next"
1258*56bb7041Schristos      field of the last element of the list.  */
1259*56bb7041Schristos   if (lang_os_list.head != NULL)
1260*56bb7041Schristos     ret->s.output_section_statement.prev
1261*56bb7041Schristos       = ((lang_output_section_statement_type *)
1262*56bb7041Schristos 	 ((char *) lang_os_list.tail
1263*56bb7041Schristos 	  - offsetof (lang_output_section_statement_type, next)));
1264*56bb7041Schristos 
1265*56bb7041Schristos   /* GCC's strict aliasing rules prevent us from just casting the
1266*56bb7041Schristos      address, so we store the pointer in a variable and cast that
1267*56bb7041Schristos      instead.  */
1268*56bb7041Schristos   nextp = &ret->s.output_section_statement.next;
1269*56bb7041Schristos   lang_statement_append (&lang_os_list, &ret->s, nextp);
1270*56bb7041Schristos   return &ret->root;
1271*56bb7041Schristos }
1272*56bb7041Schristos 
1273*56bb7041Schristos static void
output_section_statement_table_init(void)1274*56bb7041Schristos output_section_statement_table_init (void)
1275*56bb7041Schristos {
1276*56bb7041Schristos   if (!bfd_hash_table_init_n (&output_section_statement_table,
1277*56bb7041Schristos 			      output_section_statement_newfunc,
1278*56bb7041Schristos 			      sizeof (struct out_section_hash_entry),
1279*56bb7041Schristos 			      61))
1280*56bb7041Schristos     einfo (_("%F%P: can not create hash table: %E\n"));
1281*56bb7041Schristos }
1282*56bb7041Schristos 
1283*56bb7041Schristos static void
output_section_statement_table_free(void)1284*56bb7041Schristos output_section_statement_table_free (void)
1285*56bb7041Schristos {
1286*56bb7041Schristos   bfd_hash_table_free (&output_section_statement_table);
1287*56bb7041Schristos }
1288*56bb7041Schristos 
1289*56bb7041Schristos /* Build enough state so that the parser can build its tree.  */
1290*56bb7041Schristos 
1291*56bb7041Schristos void
lang_init(void)1292*56bb7041Schristos lang_init (void)
1293*56bb7041Schristos {
1294*56bb7041Schristos   obstack_begin (&stat_obstack, 1000);
1295*56bb7041Schristos 
1296*56bb7041Schristos   stat_ptr = &statement_list;
1297*56bb7041Schristos 
1298*56bb7041Schristos   output_section_statement_table_init ();
1299*56bb7041Schristos 
1300*56bb7041Schristos   lang_list_init (stat_ptr);
1301*56bb7041Schristos 
1302*56bb7041Schristos   lang_list_init (&input_file_chain);
1303*56bb7041Schristos   lang_list_init (&lang_os_list);
1304*56bb7041Schristos   lang_list_init (&file_chain);
1305*56bb7041Schristos   first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1306*56bb7041Schristos 				    NULL);
1307*56bb7041Schristos   abs_output_section =
1308*56bb7041Schristos     lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
1309*56bb7041Schristos 
1310*56bb7041Schristos   abs_output_section->bfd_section = bfd_abs_section_ptr;
1311*56bb7041Schristos 
1312*56bb7041Schristos   asneeded_list_head = NULL;
1313*56bb7041Schristos   asneeded_list_tail = &asneeded_list_head;
1314*56bb7041Schristos }
1315*56bb7041Schristos 
1316*56bb7041Schristos void
lang_finish(void)1317*56bb7041Schristos lang_finish (void)
1318*56bb7041Schristos {
1319*56bb7041Schristos   output_section_statement_table_free ();
1320*56bb7041Schristos }
1321*56bb7041Schristos 
1322*56bb7041Schristos /*----------------------------------------------------------------------
1323*56bb7041Schristos   A region is an area of memory declared with the
1324*56bb7041Schristos   MEMORY {  name:org=exp, len=exp ... }
1325*56bb7041Schristos   syntax.
1326*56bb7041Schristos 
1327*56bb7041Schristos   We maintain a list of all the regions here.
1328*56bb7041Schristos 
1329*56bb7041Schristos   If no regions are specified in the script, then the default is used
1330*56bb7041Schristos   which is created when looked up to be the entire data space.
1331*56bb7041Schristos 
1332*56bb7041Schristos   If create is true we are creating a region inside a MEMORY block.
1333*56bb7041Schristos   In this case it is probably an error to create a region that has
1334*56bb7041Schristos   already been created.  If we are not inside a MEMORY block it is
1335*56bb7041Schristos   dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1336*56bb7041Schristos   and so we issue a warning.
1337*56bb7041Schristos 
1338*56bb7041Schristos   Each region has at least one name.  The first name is either
1339*56bb7041Schristos   DEFAULT_MEMORY_REGION or the name given in the MEMORY block.  You can add
1340*56bb7041Schristos   alias names to an existing region within a script with
1341*56bb7041Schristos   REGION_ALIAS (alias, region_name).  Each name corresponds to at most one
1342*56bb7041Schristos   region.  */
1343*56bb7041Schristos 
1344*56bb7041Schristos static lang_memory_region_type *lang_memory_region_list;
1345*56bb7041Schristos static lang_memory_region_type **lang_memory_region_list_tail
1346*56bb7041Schristos   = &lang_memory_region_list;
1347*56bb7041Schristos 
1348*56bb7041Schristos lang_memory_region_type *
lang_memory_region_lookup(const char * const name,bfd_boolean create)1349*56bb7041Schristos lang_memory_region_lookup (const char *const name, bfd_boolean create)
1350*56bb7041Schristos {
1351*56bb7041Schristos   lang_memory_region_name *n;
1352*56bb7041Schristos   lang_memory_region_type *r;
1353*56bb7041Schristos   lang_memory_region_type *new_region;
1354*56bb7041Schristos 
1355*56bb7041Schristos   /* NAME is NULL for LMA memspecs if no region was specified.  */
1356*56bb7041Schristos   if (name == NULL)
1357*56bb7041Schristos     return NULL;
1358*56bb7041Schristos 
1359*56bb7041Schristos   for (r = lang_memory_region_list; r != NULL; r = r->next)
1360*56bb7041Schristos     for (n = &r->name_list; n != NULL; n = n->next)
1361*56bb7041Schristos       if (strcmp (n->name, name) == 0)
1362*56bb7041Schristos 	{
1363*56bb7041Schristos 	  if (create)
1364*56bb7041Schristos 	    einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1365*56bb7041Schristos 		   NULL, name);
1366*56bb7041Schristos 	  return r;
1367*56bb7041Schristos 	}
1368*56bb7041Schristos 
1369*56bb7041Schristos   if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1370*56bb7041Schristos     einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1371*56bb7041Schristos 	   NULL, name);
1372*56bb7041Schristos 
1373*56bb7041Schristos   new_region = stat_alloc (sizeof (lang_memory_region_type));
1374*56bb7041Schristos 
1375*56bb7041Schristos   new_region->name_list.name = xstrdup (name);
1376*56bb7041Schristos   new_region->name_list.next = NULL;
1377*56bb7041Schristos   new_region->next = NULL;
1378*56bb7041Schristos   new_region->origin_exp = NULL;
1379*56bb7041Schristos   new_region->origin = 0;
1380*56bb7041Schristos   new_region->length_exp = NULL;
1381*56bb7041Schristos   new_region->length = ~(bfd_size_type) 0;
1382*56bb7041Schristos   new_region->current = 0;
1383*56bb7041Schristos   new_region->last_os = NULL;
1384*56bb7041Schristos   new_region->flags = 0;
1385*56bb7041Schristos   new_region->not_flags = 0;
1386*56bb7041Schristos   new_region->had_full_message = FALSE;
1387*56bb7041Schristos 
1388*56bb7041Schristos   *lang_memory_region_list_tail = new_region;
1389*56bb7041Schristos   lang_memory_region_list_tail = &new_region->next;
1390*56bb7041Schristos 
1391*56bb7041Schristos   return new_region;
1392*56bb7041Schristos }
1393*56bb7041Schristos 
1394*56bb7041Schristos void
lang_memory_region_alias(const char * alias,const char * region_name)1395*56bb7041Schristos lang_memory_region_alias (const char *alias, const char *region_name)
1396*56bb7041Schristos {
1397*56bb7041Schristos   lang_memory_region_name *n;
1398*56bb7041Schristos   lang_memory_region_type *r;
1399*56bb7041Schristos   lang_memory_region_type *region;
1400*56bb7041Schristos 
1401*56bb7041Schristos   /* The default region must be unique.  This ensures that it is not necessary
1402*56bb7041Schristos      to iterate through the name list if someone wants the check if a region is
1403*56bb7041Schristos      the default memory region.  */
1404*56bb7041Schristos   if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
1405*56bb7041Schristos       || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
1406*56bb7041Schristos     einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL);
1407*56bb7041Schristos 
1408*56bb7041Schristos   /* Look for the target region and check if the alias is not already
1409*56bb7041Schristos      in use.  */
1410*56bb7041Schristos   region = NULL;
1411*56bb7041Schristos   for (r = lang_memory_region_list; r != NULL; r = r->next)
1412*56bb7041Schristos     for (n = &r->name_list; n != NULL; n = n->next)
1413*56bb7041Schristos       {
1414*56bb7041Schristos 	if (region == NULL && strcmp (n->name, region_name) == 0)
1415*56bb7041Schristos 	  region = r;
1416*56bb7041Schristos 	if (strcmp (n->name, alias) == 0)
1417*56bb7041Schristos 	  einfo (_("%F%P:%pS: error: redefinition of memory region "
1418*56bb7041Schristos 		   "alias `%s'\n"),
1419*56bb7041Schristos 		 NULL, alias);
1420*56bb7041Schristos       }
1421*56bb7041Schristos 
1422*56bb7041Schristos   /* Check if the target region exists.  */
1423*56bb7041Schristos   if (region == NULL)
1424*56bb7041Schristos     einfo (_("%F%P:%pS: error: memory region `%s' "
1425*56bb7041Schristos 	     "for alias `%s' does not exist\n"),
1426*56bb7041Schristos 	   NULL, region_name, alias);
1427*56bb7041Schristos 
1428*56bb7041Schristos   /* Add alias to region name list.  */
1429*56bb7041Schristos   n = stat_alloc (sizeof (lang_memory_region_name));
1430*56bb7041Schristos   n->name = xstrdup (alias);
1431*56bb7041Schristos   n->next = region->name_list.next;
1432*56bb7041Schristos   region->name_list.next = n;
1433*56bb7041Schristos }
1434*56bb7041Schristos 
1435*56bb7041Schristos static lang_memory_region_type *
lang_memory_default(asection * section)1436*56bb7041Schristos lang_memory_default (asection *section)
1437*56bb7041Schristos {
1438*56bb7041Schristos   lang_memory_region_type *p;
1439*56bb7041Schristos 
1440*56bb7041Schristos   flagword sec_flags = section->flags;
1441*56bb7041Schristos 
1442*56bb7041Schristos   /* Override SEC_DATA to mean a writable section.  */
1443*56bb7041Schristos   if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1444*56bb7041Schristos     sec_flags |= SEC_DATA;
1445*56bb7041Schristos 
1446*56bb7041Schristos   for (p = lang_memory_region_list; p != NULL; p = p->next)
1447*56bb7041Schristos     {
1448*56bb7041Schristos       if ((p->flags & sec_flags) != 0
1449*56bb7041Schristos 	  && (p->not_flags & sec_flags) == 0)
1450*56bb7041Schristos 	{
1451*56bb7041Schristos 	  return p;
1452*56bb7041Schristos 	}
1453*56bb7041Schristos     }
1454*56bb7041Schristos   return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1455*56bb7041Schristos }
1456*56bb7041Schristos 
1457*56bb7041Schristos /* Get the output section statement directly from the userdata.  */
1458*56bb7041Schristos 
1459*56bb7041Schristos lang_output_section_statement_type *
lang_output_section_get(const asection * output_section)1460*56bb7041Schristos lang_output_section_get (const asection *output_section)
1461*56bb7041Schristos {
1462*56bb7041Schristos   return bfd_section_userdata (output_section);
1463*56bb7041Schristos }
1464*56bb7041Schristos 
1465*56bb7041Schristos /* Find or create an output_section_statement with the given NAME.
1466*56bb7041Schristos    If CONSTRAINT is non-zero match one with that constraint, otherwise
1467*56bb7041Schristos    match any non-negative constraint.  If CREATE, always make a
1468*56bb7041Schristos    new output_section_statement for SPECIAL CONSTRAINT.  */
1469*56bb7041Schristos 
1470*56bb7041Schristos lang_output_section_statement_type *
lang_output_section_statement_lookup(const char * name,int constraint,bfd_boolean create)1471*56bb7041Schristos lang_output_section_statement_lookup (const char *name,
1472*56bb7041Schristos 				      int constraint,
1473*56bb7041Schristos 				      bfd_boolean create)
1474*56bb7041Schristos {
1475*56bb7041Schristos   struct out_section_hash_entry *entry;
1476*56bb7041Schristos 
1477*56bb7041Schristos   entry = ((struct out_section_hash_entry *)
1478*56bb7041Schristos 	   bfd_hash_lookup (&output_section_statement_table, name,
1479*56bb7041Schristos 			    create, FALSE));
1480*56bb7041Schristos   if (entry == NULL)
1481*56bb7041Schristos     {
1482*56bb7041Schristos       if (create)
1483*56bb7041Schristos 	einfo (_("%F%P: failed creating section `%s': %E\n"), name);
1484*56bb7041Schristos       return NULL;
1485*56bb7041Schristos     }
1486*56bb7041Schristos 
1487*56bb7041Schristos   if (entry->s.output_section_statement.name != NULL)
1488*56bb7041Schristos     {
1489*56bb7041Schristos       /* We have a section of this name, but it might not have the correct
1490*56bb7041Schristos 	 constraint.  */
1491*56bb7041Schristos       struct out_section_hash_entry *last_ent;
1492*56bb7041Schristos 
1493*56bb7041Schristos       name = entry->s.output_section_statement.name;
1494*56bb7041Schristos       if (create && constraint == SPECIAL)
1495*56bb7041Schristos 	/* Not traversing to the end reverses the order of the second
1496*56bb7041Schristos 	   and subsequent SPECIAL sections in the hash table chain,
1497*56bb7041Schristos 	   but that shouldn't matter.  */
1498*56bb7041Schristos 	last_ent = entry;
1499*56bb7041Schristos       else
1500*56bb7041Schristos 	do
1501*56bb7041Schristos 	  {
1502*56bb7041Schristos 	    if (constraint == entry->s.output_section_statement.constraint
1503*56bb7041Schristos 		|| (constraint == 0
1504*56bb7041Schristos 		    && entry->s.output_section_statement.constraint >= 0))
1505*56bb7041Schristos 	      return &entry->s.output_section_statement;
1506*56bb7041Schristos 	    last_ent = entry;
1507*56bb7041Schristos 	    entry = (struct out_section_hash_entry *) entry->root.next;
1508*56bb7041Schristos 	  }
1509*56bb7041Schristos 	while (entry != NULL
1510*56bb7041Schristos 	       && name == entry->s.output_section_statement.name);
1511*56bb7041Schristos 
1512*56bb7041Schristos       if (!create)
1513*56bb7041Schristos 	return NULL;
1514*56bb7041Schristos 
1515*56bb7041Schristos       entry
1516*56bb7041Schristos 	= ((struct out_section_hash_entry *)
1517*56bb7041Schristos 	   output_section_statement_newfunc (NULL,
1518*56bb7041Schristos 					     &output_section_statement_table,
1519*56bb7041Schristos 					     name));
1520*56bb7041Schristos       if (entry == NULL)
1521*56bb7041Schristos 	{
1522*56bb7041Schristos 	  einfo (_("%F%P: failed creating section `%s': %E\n"), name);
1523*56bb7041Schristos 	  return NULL;
1524*56bb7041Schristos 	}
1525*56bb7041Schristos       entry->root = last_ent->root;
1526*56bb7041Schristos       last_ent->root.next = &entry->root;
1527*56bb7041Schristos     }
1528*56bb7041Schristos 
1529*56bb7041Schristos   entry->s.output_section_statement.name = name;
1530*56bb7041Schristos   entry->s.output_section_statement.constraint = constraint;
1531*56bb7041Schristos   return &entry->s.output_section_statement;
1532*56bb7041Schristos }
1533*56bb7041Schristos 
1534*56bb7041Schristos /* Find the next output_section_statement with the same name as OS.
1535*56bb7041Schristos    If CONSTRAINT is non-zero, find one with that constraint otherwise
1536*56bb7041Schristos    match any non-negative constraint.  */
1537*56bb7041Schristos 
1538*56bb7041Schristos lang_output_section_statement_type *
next_matching_output_section_statement(lang_output_section_statement_type * os,int constraint)1539*56bb7041Schristos next_matching_output_section_statement (lang_output_section_statement_type *os,
1540*56bb7041Schristos 					int constraint)
1541*56bb7041Schristos {
1542*56bb7041Schristos   /* All output_section_statements are actually part of a
1543*56bb7041Schristos      struct out_section_hash_entry.  */
1544*56bb7041Schristos   struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
1545*56bb7041Schristos     ((char *) os
1546*56bb7041Schristos      - offsetof (struct out_section_hash_entry, s.output_section_statement));
1547*56bb7041Schristos   const char *name = os->name;
1548*56bb7041Schristos 
1549*56bb7041Schristos   ASSERT (name == entry->root.string);
1550*56bb7041Schristos   do
1551*56bb7041Schristos     {
1552*56bb7041Schristos       entry = (struct out_section_hash_entry *) entry->root.next;
1553*56bb7041Schristos       if (entry == NULL
1554*56bb7041Schristos 	  || name != entry->s.output_section_statement.name)
1555*56bb7041Schristos 	return NULL;
1556*56bb7041Schristos     }
1557*56bb7041Schristos   while (constraint != entry->s.output_section_statement.constraint
1558*56bb7041Schristos 	 && (constraint != 0
1559*56bb7041Schristos 	     || entry->s.output_section_statement.constraint < 0));
1560*56bb7041Schristos 
1561*56bb7041Schristos   return &entry->s.output_section_statement;
1562*56bb7041Schristos }
1563*56bb7041Schristos 
1564*56bb7041Schristos /* A variant of lang_output_section_find used by place_orphan.
1565*56bb7041Schristos    Returns the output statement that should precede a new output
1566*56bb7041Schristos    statement for SEC.  If an exact match is found on certain flags,
1567*56bb7041Schristos    sets *EXACT too.  */
1568*56bb7041Schristos 
1569*56bb7041Schristos lang_output_section_statement_type *
lang_output_section_find_by_flags(const asection * sec,flagword sec_flags,lang_output_section_statement_type ** exact,lang_match_sec_type_func match_type)1570*56bb7041Schristos lang_output_section_find_by_flags (const asection *sec,
1571*56bb7041Schristos 				   flagword sec_flags,
1572*56bb7041Schristos 				   lang_output_section_statement_type **exact,
1573*56bb7041Schristos 				   lang_match_sec_type_func match_type)
1574*56bb7041Schristos {
1575*56bb7041Schristos   lang_output_section_statement_type *first, *look, *found;
1576*56bb7041Schristos   flagword look_flags, differ;
1577*56bb7041Schristos 
1578*56bb7041Schristos   /* We know the first statement on this list is *ABS*.  May as well
1579*56bb7041Schristos      skip it.  */
1580*56bb7041Schristos   first = (void *) lang_os_list.head;
1581*56bb7041Schristos   first = first->next;
1582*56bb7041Schristos 
1583*56bb7041Schristos   /* First try for an exact match.  */
1584*56bb7041Schristos   found = NULL;
1585*56bb7041Schristos   for (look = first; look; look = look->next)
1586*56bb7041Schristos     {
1587*56bb7041Schristos       look_flags = look->flags;
1588*56bb7041Schristos       if (look->bfd_section != NULL)
1589*56bb7041Schristos 	{
1590*56bb7041Schristos 	  look_flags = look->bfd_section->flags;
1591*56bb7041Schristos 	  if (match_type && !match_type (link_info.output_bfd,
1592*56bb7041Schristos 					 look->bfd_section,
1593*56bb7041Schristos 					 sec->owner, sec))
1594*56bb7041Schristos 	    continue;
1595*56bb7041Schristos 	}
1596*56bb7041Schristos       differ = look_flags ^ sec_flags;
1597*56bb7041Schristos       if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1598*56bb7041Schristos 		      | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1599*56bb7041Schristos 	found = look;
1600*56bb7041Schristos     }
1601*56bb7041Schristos   if (found != NULL)
1602*56bb7041Schristos     {
1603*56bb7041Schristos       if (exact != NULL)
1604*56bb7041Schristos 	*exact = found;
1605*56bb7041Schristos       return found;
1606*56bb7041Schristos     }
1607*56bb7041Schristos 
1608*56bb7041Schristos   if ((sec_flags & SEC_CODE) != 0
1609*56bb7041Schristos       && (sec_flags & SEC_ALLOC) != 0)
1610*56bb7041Schristos     {
1611*56bb7041Schristos       /* Try for a rw code section.  */
1612*56bb7041Schristos       for (look = first; look; look = look->next)
1613*56bb7041Schristos 	{
1614*56bb7041Schristos 	  look_flags = look->flags;
1615*56bb7041Schristos 	  if (look->bfd_section != NULL)
1616*56bb7041Schristos 	    {
1617*56bb7041Schristos 	      look_flags = look->bfd_section->flags;
1618*56bb7041Schristos 	      if (match_type && !match_type (link_info.output_bfd,
1619*56bb7041Schristos 					     look->bfd_section,
1620*56bb7041Schristos 					     sec->owner, sec))
1621*56bb7041Schristos 		continue;
1622*56bb7041Schristos 	    }
1623*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1624*56bb7041Schristos 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1625*56bb7041Schristos 			  | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1626*56bb7041Schristos 	    found = look;
1627*56bb7041Schristos 	}
1628*56bb7041Schristos     }
1629*56bb7041Schristos   else if ((sec_flags & SEC_READONLY) != 0
1630*56bb7041Schristos 	   && (sec_flags & SEC_ALLOC) != 0)
1631*56bb7041Schristos     {
1632*56bb7041Schristos       /* .rodata can go after .text, .sdata2 after .rodata.  */
1633*56bb7041Schristos       for (look = first; look; look = look->next)
1634*56bb7041Schristos 	{
1635*56bb7041Schristos 	  look_flags = look->flags;
1636*56bb7041Schristos 	  if (look->bfd_section != NULL)
1637*56bb7041Schristos 	    {
1638*56bb7041Schristos 	      look_flags = look->bfd_section->flags;
1639*56bb7041Schristos 	      if (match_type && !match_type (link_info.output_bfd,
1640*56bb7041Schristos 					     look->bfd_section,
1641*56bb7041Schristos 					     sec->owner, sec))
1642*56bb7041Schristos 		continue;
1643*56bb7041Schristos 	    }
1644*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1645*56bb7041Schristos 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1646*56bb7041Schristos 			  | SEC_READONLY | SEC_SMALL_DATA))
1647*56bb7041Schristos 	      || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1648*56bb7041Schristos 			      | SEC_READONLY))
1649*56bb7041Schristos 		  && !(look_flags & SEC_SMALL_DATA)))
1650*56bb7041Schristos 	    found = look;
1651*56bb7041Schristos 	}
1652*56bb7041Schristos     }
1653*56bb7041Schristos   else if ((sec_flags & SEC_THREAD_LOCAL) != 0
1654*56bb7041Schristos 	   && (sec_flags & SEC_ALLOC) != 0)
1655*56bb7041Schristos     {
1656*56bb7041Schristos       /* .tdata can go after .data, .tbss after .tdata.  Treat .tbss
1657*56bb7041Schristos 	 as if it were a loaded section, and don't use match_type.  */
1658*56bb7041Schristos       bfd_boolean seen_thread_local = FALSE;
1659*56bb7041Schristos 
1660*56bb7041Schristos       match_type = NULL;
1661*56bb7041Schristos       for (look = first; look; look = look->next)
1662*56bb7041Schristos 	{
1663*56bb7041Schristos 	  look_flags = look->flags;
1664*56bb7041Schristos 	  if (look->bfd_section != NULL)
1665*56bb7041Schristos 	    look_flags = look->bfd_section->flags;
1666*56bb7041Schristos 
1667*56bb7041Schristos 	  differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS);
1668*56bb7041Schristos 	  if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC)))
1669*56bb7041Schristos 	    {
1670*56bb7041Schristos 	      /* .tdata and .tbss must be adjacent and in that order.  */
1671*56bb7041Schristos 	      if (!(look_flags & SEC_LOAD)
1672*56bb7041Schristos 		  && (sec_flags & SEC_LOAD))
1673*56bb7041Schristos 		/* ..so if we're at a .tbss section and we're placing
1674*56bb7041Schristos 		   a .tdata section stop looking and return the
1675*56bb7041Schristos 		   previous section.  */
1676*56bb7041Schristos 		break;
1677*56bb7041Schristos 	      found = look;
1678*56bb7041Schristos 	      seen_thread_local = TRUE;
1679*56bb7041Schristos 	    }
1680*56bb7041Schristos 	  else if (seen_thread_local)
1681*56bb7041Schristos 	    break;
1682*56bb7041Schristos 	  else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD)))
1683*56bb7041Schristos 	    found = look;
1684*56bb7041Schristos 	}
1685*56bb7041Schristos     }
1686*56bb7041Schristos   else if ((sec_flags & SEC_SMALL_DATA) != 0
1687*56bb7041Schristos 	   && (sec_flags & SEC_ALLOC) != 0)
1688*56bb7041Schristos     {
1689*56bb7041Schristos       /* .sdata goes after .data, .sbss after .sdata.  */
1690*56bb7041Schristos       for (look = first; look; look = look->next)
1691*56bb7041Schristos 	{
1692*56bb7041Schristos 	  look_flags = look->flags;
1693*56bb7041Schristos 	  if (look->bfd_section != NULL)
1694*56bb7041Schristos 	    {
1695*56bb7041Schristos 	      look_flags = look->bfd_section->flags;
1696*56bb7041Schristos 	      if (match_type && !match_type (link_info.output_bfd,
1697*56bb7041Schristos 					     look->bfd_section,
1698*56bb7041Schristos 					     sec->owner, sec))
1699*56bb7041Schristos 		continue;
1700*56bb7041Schristos 	    }
1701*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1702*56bb7041Schristos 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1703*56bb7041Schristos 			  | SEC_THREAD_LOCAL))
1704*56bb7041Schristos 	      || ((look_flags & SEC_SMALL_DATA)
1705*56bb7041Schristos 		  && !(sec_flags & SEC_HAS_CONTENTS)))
1706*56bb7041Schristos 	    found = look;
1707*56bb7041Schristos 	}
1708*56bb7041Schristos     }
1709*56bb7041Schristos   else if ((sec_flags & SEC_HAS_CONTENTS) != 0
1710*56bb7041Schristos 	   && (sec_flags & SEC_ALLOC) != 0)
1711*56bb7041Schristos     {
1712*56bb7041Schristos       /* .data goes after .rodata.  */
1713*56bb7041Schristos       for (look = first; look; look = look->next)
1714*56bb7041Schristos 	{
1715*56bb7041Schristos 	  look_flags = look->flags;
1716*56bb7041Schristos 	  if (look->bfd_section != NULL)
1717*56bb7041Schristos 	    {
1718*56bb7041Schristos 	      look_flags = look->bfd_section->flags;
1719*56bb7041Schristos 	      if (match_type && !match_type (link_info.output_bfd,
1720*56bb7041Schristos 					     look->bfd_section,
1721*56bb7041Schristos 					     sec->owner, sec))
1722*56bb7041Schristos 		continue;
1723*56bb7041Schristos 	    }
1724*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1725*56bb7041Schristos 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1726*56bb7041Schristos 			  | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1727*56bb7041Schristos 	    found = look;
1728*56bb7041Schristos 	}
1729*56bb7041Schristos     }
1730*56bb7041Schristos   else if ((sec_flags & SEC_ALLOC) != 0)
1731*56bb7041Schristos     {
1732*56bb7041Schristos       /* .bss goes after any other alloc section.  */
1733*56bb7041Schristos       for (look = first; look; look = look->next)
1734*56bb7041Schristos 	{
1735*56bb7041Schristos 	  look_flags = look->flags;
1736*56bb7041Schristos 	  if (look->bfd_section != NULL)
1737*56bb7041Schristos 	    {
1738*56bb7041Schristos 	      look_flags = look->bfd_section->flags;
1739*56bb7041Schristos 	      if (match_type && !match_type (link_info.output_bfd,
1740*56bb7041Schristos 					     look->bfd_section,
1741*56bb7041Schristos 					     sec->owner, sec))
1742*56bb7041Schristos 		continue;
1743*56bb7041Schristos 	    }
1744*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1745*56bb7041Schristos 	  if (!(differ & SEC_ALLOC))
1746*56bb7041Schristos 	    found = look;
1747*56bb7041Schristos 	}
1748*56bb7041Schristos     }
1749*56bb7041Schristos   else
1750*56bb7041Schristos     {
1751*56bb7041Schristos       /* non-alloc go last.  */
1752*56bb7041Schristos       for (look = first; look; look = look->next)
1753*56bb7041Schristos 	{
1754*56bb7041Schristos 	  look_flags = look->flags;
1755*56bb7041Schristos 	  if (look->bfd_section != NULL)
1756*56bb7041Schristos 	    look_flags = look->bfd_section->flags;
1757*56bb7041Schristos 	  differ = look_flags ^ sec_flags;
1758*56bb7041Schristos 	  if (!(differ & SEC_DEBUGGING))
1759*56bb7041Schristos 	    found = look;
1760*56bb7041Schristos 	}
1761*56bb7041Schristos       return found;
1762*56bb7041Schristos     }
1763*56bb7041Schristos 
1764*56bb7041Schristos   if (found || !match_type)
1765*56bb7041Schristos     return found;
1766*56bb7041Schristos 
1767*56bb7041Schristos   return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL);
1768*56bb7041Schristos }
1769*56bb7041Schristos 
1770*56bb7041Schristos /* Find the last output section before given output statement.
1771*56bb7041Schristos    Used by place_orphan.  */
1772*56bb7041Schristos 
1773*56bb7041Schristos static asection *
output_prev_sec_find(lang_output_section_statement_type * os)1774*56bb7041Schristos output_prev_sec_find (lang_output_section_statement_type *os)
1775*56bb7041Schristos {
1776*56bb7041Schristos   lang_output_section_statement_type *lookup;
1777*56bb7041Schristos 
1778*56bb7041Schristos   for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1779*56bb7041Schristos     {
1780*56bb7041Schristos       if (lookup->constraint < 0)
1781*56bb7041Schristos 	continue;
1782*56bb7041Schristos 
1783*56bb7041Schristos       if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1784*56bb7041Schristos 	return lookup->bfd_section;
1785*56bb7041Schristos     }
1786*56bb7041Schristos 
1787*56bb7041Schristos   return NULL;
1788*56bb7041Schristos }
1789*56bb7041Schristos 
1790*56bb7041Schristos /* Look for a suitable place for a new output section statement.  The
1791*56bb7041Schristos    idea is to skip over anything that might be inside a SECTIONS {}
1792*56bb7041Schristos    statement in a script, before we find another output section
1793*56bb7041Schristos    statement.  Assignments to "dot" before an output section statement
1794*56bb7041Schristos    are assumed to belong to it, except in two cases;  The first
1795*56bb7041Schristos    assignment to dot, and assignments before non-alloc sections.
1796*56bb7041Schristos    Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1797*56bb7041Schristos    similar assignments that set the initial address, or we might
1798*56bb7041Schristos    insert non-alloc note sections among assignments setting end of
1799*56bb7041Schristos    image symbols.  */
1800*56bb7041Schristos 
1801*56bb7041Schristos static lang_statement_union_type **
insert_os_after(lang_output_section_statement_type * after)1802*56bb7041Schristos insert_os_after (lang_output_section_statement_type *after)
1803*56bb7041Schristos {
1804*56bb7041Schristos   lang_statement_union_type **where;
1805*56bb7041Schristos   lang_statement_union_type **assign = NULL;
1806*56bb7041Schristos   bfd_boolean ignore_first;
1807*56bb7041Schristos 
1808*56bb7041Schristos   ignore_first = after == (void *) lang_os_list.head;
1809*56bb7041Schristos 
1810*56bb7041Schristos   for (where = &after->header.next;
1811*56bb7041Schristos        *where != NULL;
1812*56bb7041Schristos        where = &(*where)->header.next)
1813*56bb7041Schristos     {
1814*56bb7041Schristos       switch ((*where)->header.type)
1815*56bb7041Schristos 	{
1816*56bb7041Schristos 	case lang_assignment_statement_enum:
1817*56bb7041Schristos 	  if (assign == NULL)
1818*56bb7041Schristos 	    {
1819*56bb7041Schristos 	      lang_assignment_statement_type *ass;
1820*56bb7041Schristos 
1821*56bb7041Schristos 	      ass = &(*where)->assignment_statement;
1822*56bb7041Schristos 	      if (ass->exp->type.node_class != etree_assert
1823*56bb7041Schristos 		  && ass->exp->assign.dst[0] == '.'
1824*56bb7041Schristos 		  && ass->exp->assign.dst[1] == 0)
1825*56bb7041Schristos 		{
1826*56bb7041Schristos 		  if (!ignore_first)
1827*56bb7041Schristos 		    assign = where;
1828*56bb7041Schristos 		  ignore_first = FALSE;
1829*56bb7041Schristos 		}
1830*56bb7041Schristos 	    }
1831*56bb7041Schristos 	  continue;
1832*56bb7041Schristos 	case lang_wild_statement_enum:
1833*56bb7041Schristos 	case lang_input_section_enum:
1834*56bb7041Schristos 	case lang_object_symbols_statement_enum:
1835*56bb7041Schristos 	case lang_fill_statement_enum:
1836*56bb7041Schristos 	case lang_data_statement_enum:
1837*56bb7041Schristos 	case lang_reloc_statement_enum:
1838*56bb7041Schristos 	case lang_padding_statement_enum:
1839*56bb7041Schristos 	case lang_constructors_statement_enum:
1840*56bb7041Schristos 	  assign = NULL;
1841*56bb7041Schristos 	  ignore_first = FALSE;
1842*56bb7041Schristos 	  continue;
1843*56bb7041Schristos 	case lang_output_section_statement_enum:
1844*56bb7041Schristos 	  if (assign != NULL)
1845*56bb7041Schristos 	    {
1846*56bb7041Schristos 	      asection *s = (*where)->output_section_statement.bfd_section;
1847*56bb7041Schristos 
1848*56bb7041Schristos 	      if (s == NULL
1849*56bb7041Schristos 		  || s->map_head.s == NULL
1850*56bb7041Schristos 		  || (s->flags & SEC_ALLOC) != 0)
1851*56bb7041Schristos 		where = assign;
1852*56bb7041Schristos 	    }
1853*56bb7041Schristos 	  break;
1854*56bb7041Schristos 	case lang_input_statement_enum:
1855*56bb7041Schristos 	case lang_address_statement_enum:
1856*56bb7041Schristos 	case lang_target_statement_enum:
1857*56bb7041Schristos 	case lang_output_statement_enum:
1858*56bb7041Schristos 	case lang_group_statement_enum:
1859*56bb7041Schristos 	case lang_insert_statement_enum:
1860*56bb7041Schristos 	  continue;
1861*56bb7041Schristos 	}
1862*56bb7041Schristos       break;
1863*56bb7041Schristos     }
1864*56bb7041Schristos 
1865*56bb7041Schristos   return where;
1866*56bb7041Schristos }
1867*56bb7041Schristos 
1868*56bb7041Schristos lang_output_section_statement_type *
lang_insert_orphan(asection * s,const char * secname,int constraint,lang_output_section_statement_type * after,struct orphan_save * place,etree_type * address,lang_statement_list_type * add_child)1869*56bb7041Schristos lang_insert_orphan (asection *s,
1870*56bb7041Schristos 		    const char *secname,
1871*56bb7041Schristos 		    int constraint,
1872*56bb7041Schristos 		    lang_output_section_statement_type *after,
1873*56bb7041Schristos 		    struct orphan_save *place,
1874*56bb7041Schristos 		    etree_type *address,
1875*56bb7041Schristos 		    lang_statement_list_type *add_child)
1876*56bb7041Schristos {
1877*56bb7041Schristos   lang_statement_list_type add;
1878*56bb7041Schristos   lang_output_section_statement_type *os;
1879*56bb7041Schristos   lang_output_section_statement_type **os_tail;
1880*56bb7041Schristos 
1881*56bb7041Schristos   /* If we have found an appropriate place for the output section
1882*56bb7041Schristos      statements for this orphan, add them to our own private list,
1883*56bb7041Schristos      inserting them later into the global statement list.  */
1884*56bb7041Schristos   if (after != NULL)
1885*56bb7041Schristos     {
1886*56bb7041Schristos       lang_list_init (&add);
1887*56bb7041Schristos       push_stat_ptr (&add);
1888*56bb7041Schristos     }
1889*56bb7041Schristos 
1890*56bb7041Schristos   if (bfd_link_relocatable (&link_info)
1891*56bb7041Schristos       || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1892*56bb7041Schristos     address = exp_intop (0);
1893*56bb7041Schristos 
1894*56bb7041Schristos   os_tail = (lang_output_section_statement_type **) lang_os_list.tail;
1895*56bb7041Schristos   os = lang_enter_output_section_statement (secname, address, normal_section,
1896*56bb7041Schristos 					    NULL, NULL, NULL, constraint, 0);
1897*56bb7041Schristos 
1898*56bb7041Schristos   if (add_child == NULL)
1899*56bb7041Schristos     add_child = &os->children;
1900*56bb7041Schristos   lang_add_section (add_child, s, NULL, os);
1901*56bb7041Schristos 
1902*56bb7041Schristos   if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
1903*56bb7041Schristos     {
1904*56bb7041Schristos       const char *region = (after->region
1905*56bb7041Schristos 			    ? after->region->name_list.name
1906*56bb7041Schristos 			    : DEFAULT_MEMORY_REGION);
1907*56bb7041Schristos       const char *lma_region = (after->lma_region
1908*56bb7041Schristos 				? after->lma_region->name_list.name
1909*56bb7041Schristos 				: NULL);
1910*56bb7041Schristos       lang_leave_output_section_statement (NULL, region, after->phdrs,
1911*56bb7041Schristos 					   lma_region);
1912*56bb7041Schristos     }
1913*56bb7041Schristos   else
1914*56bb7041Schristos     lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
1915*56bb7041Schristos 					 NULL);
1916*56bb7041Schristos 
1917*56bb7041Schristos   /* Restore the global list pointer.  */
1918*56bb7041Schristos   if (after != NULL)
1919*56bb7041Schristos     pop_stat_ptr ();
1920*56bb7041Schristos 
1921*56bb7041Schristos   if (after != NULL && os->bfd_section != NULL)
1922*56bb7041Schristos     {
1923*56bb7041Schristos       asection *snew, *as;
1924*56bb7041Schristos       bfd_boolean place_after = place->stmt == NULL;
1925*56bb7041Schristos       bfd_boolean insert_after = TRUE;
1926*56bb7041Schristos 
1927*56bb7041Schristos       snew = os->bfd_section;
1928*56bb7041Schristos 
1929*56bb7041Schristos       /* Shuffle the bfd section list to make the output file look
1930*56bb7041Schristos 	 neater.  This is really only cosmetic.  */
1931*56bb7041Schristos       if (place->section == NULL
1932*56bb7041Schristos 	  && after != (void *) lang_os_list.head)
1933*56bb7041Schristos 	{
1934*56bb7041Schristos 	  asection *bfd_section = after->bfd_section;
1935*56bb7041Schristos 
1936*56bb7041Schristos 	  /* If the output statement hasn't been used to place any input
1937*56bb7041Schristos 	     sections (and thus doesn't have an output bfd_section),
1938*56bb7041Schristos 	     look for the closest prior output statement having an
1939*56bb7041Schristos 	     output section.  */
1940*56bb7041Schristos 	  if (bfd_section == NULL)
1941*56bb7041Schristos 	    bfd_section = output_prev_sec_find (after);
1942*56bb7041Schristos 
1943*56bb7041Schristos 	  if (bfd_section != NULL && bfd_section != snew)
1944*56bb7041Schristos 	    place->section = &bfd_section->next;
1945*56bb7041Schristos 	}
1946*56bb7041Schristos 
1947*56bb7041Schristos       if (place->section == NULL)
1948*56bb7041Schristos 	place->section = &link_info.output_bfd->sections;
1949*56bb7041Schristos 
1950*56bb7041Schristos       as = *place->section;
1951*56bb7041Schristos 
1952*56bb7041Schristos       if (!as)
1953*56bb7041Schristos 	{
1954*56bb7041Schristos 	  /* Put the section at the end of the list.  */
1955*56bb7041Schristos 
1956*56bb7041Schristos 	  /* Unlink the section.  */
1957*56bb7041Schristos 	  bfd_section_list_remove (link_info.output_bfd, snew);
1958*56bb7041Schristos 
1959*56bb7041Schristos 	  /* Now tack it back on in the right place.  */
1960*56bb7041Schristos 	  bfd_section_list_append (link_info.output_bfd, snew);
1961*56bb7041Schristos 	}
1962*56bb7041Schristos       else if ((bfd_get_flavour (link_info.output_bfd)
1963*56bb7041Schristos 		== bfd_target_elf_flavour)
1964*56bb7041Schristos 	       && (bfd_get_flavour (s->owner)
1965*56bb7041Schristos 		   == bfd_target_elf_flavour)
1966*56bb7041Schristos 	       && ((elf_section_type (s) == SHT_NOTE
1967*56bb7041Schristos 		    && (s->flags & SEC_LOAD) != 0)
1968*56bb7041Schristos 		   || (elf_section_type (as) == SHT_NOTE
1969*56bb7041Schristos 		       && (as->flags & SEC_LOAD) != 0)))
1970*56bb7041Schristos 	{
1971*56bb7041Schristos 	  /* Make sure that output note sections are grouped and sorted
1972*56bb7041Schristos 	     by alignments when inserting a note section or insert a
1973*56bb7041Schristos 	     section after a note section,  */
1974*56bb7041Schristos 	  asection *sec;
1975*56bb7041Schristos 	  /* A specific section after which the output note section
1976*56bb7041Schristos 	     should be placed.  */
1977*56bb7041Schristos 	  asection *after_sec;
1978*56bb7041Schristos 	  /* True if we need to insert the orphan section after a
1979*56bb7041Schristos 	     specific section to maintain output note section order.  */
1980*56bb7041Schristos 	  bfd_boolean after_sec_note = FALSE;
1981*56bb7041Schristos 
1982*56bb7041Schristos 	  static asection *first_orphan_note = NULL;
1983*56bb7041Schristos 
1984*56bb7041Schristos 	  /* Group and sort output note section by alignments in
1985*56bb7041Schristos 	     ascending order.  */
1986*56bb7041Schristos 	  after_sec = NULL;
1987*56bb7041Schristos 	  if (elf_section_type (s) == SHT_NOTE
1988*56bb7041Schristos 	      && (s->flags & SEC_LOAD) != 0)
1989*56bb7041Schristos 	    {
1990*56bb7041Schristos 	      /* Search from the beginning for the last output note
1991*56bb7041Schristos 		 section with equal or larger alignments.  NB: Don't
1992*56bb7041Schristos 		 place orphan note section after non-note sections.  */
1993*56bb7041Schristos 
1994*56bb7041Schristos 	      first_orphan_note = NULL;
1995*56bb7041Schristos 	      for (sec = link_info.output_bfd->sections;
1996*56bb7041Schristos 		   (sec != NULL
1997*56bb7041Schristos 		    && !bfd_is_abs_section (sec));
1998*56bb7041Schristos 		   sec = sec->next)
1999*56bb7041Schristos 		if (sec != snew
2000*56bb7041Schristos 		    && elf_section_type (sec) == SHT_NOTE
2001*56bb7041Schristos 		    && (sec->flags & SEC_LOAD) != 0)
2002*56bb7041Schristos 		  {
2003*56bb7041Schristos 		    if (!first_orphan_note)
2004*56bb7041Schristos 		      first_orphan_note = sec;
2005*56bb7041Schristos 		    if (sec->alignment_power >= s->alignment_power)
2006*56bb7041Schristos 		      after_sec = sec;
2007*56bb7041Schristos 		  }
2008*56bb7041Schristos 		else if (first_orphan_note)
2009*56bb7041Schristos 		  {
2010*56bb7041Schristos 		    /* Stop if there is non-note section after the first
2011*56bb7041Schristos 		       orphan note section.  */
2012*56bb7041Schristos 		    break;
2013*56bb7041Schristos 		  }
2014*56bb7041Schristos 
2015*56bb7041Schristos 	      /* If this will be the first orphan note section, it can
2016*56bb7041Schristos 		 be placed at the default location.  */
2017*56bb7041Schristos 	      after_sec_note = first_orphan_note != NULL;
2018*56bb7041Schristos 	      if (after_sec == NULL && after_sec_note)
2019*56bb7041Schristos 		{
2020*56bb7041Schristos 		  /* If all output note sections have smaller
2021*56bb7041Schristos 		     alignments, place the section before all
2022*56bb7041Schristos 		     output orphan note sections.  */
2023*56bb7041Schristos 		  after_sec = first_orphan_note;
2024*56bb7041Schristos 		  insert_after = FALSE;
2025*56bb7041Schristos 		}
2026*56bb7041Schristos 	    }
2027*56bb7041Schristos 	  else if (first_orphan_note)
2028*56bb7041Schristos 	    {
2029*56bb7041Schristos 	      /* Don't place non-note sections in the middle of orphan
2030*56bb7041Schristos 	         note sections.  */
2031*56bb7041Schristos 	      after_sec_note = TRUE;
2032*56bb7041Schristos 	      after_sec = as;
2033*56bb7041Schristos 	      for (sec = as->next;
2034*56bb7041Schristos 		   (sec != NULL
2035*56bb7041Schristos 		    && !bfd_is_abs_section (sec));
2036*56bb7041Schristos 		   sec = sec->next)
2037*56bb7041Schristos 		if (elf_section_type (sec) == SHT_NOTE
2038*56bb7041Schristos 		    && (sec->flags & SEC_LOAD) != 0)
2039*56bb7041Schristos 		  after_sec = sec;
2040*56bb7041Schristos 	    }
2041*56bb7041Schristos 
2042*56bb7041Schristos 	  if (after_sec_note)
2043*56bb7041Schristos 	    {
2044*56bb7041Schristos 	      if (after_sec)
2045*56bb7041Schristos 		{
2046*56bb7041Schristos 		  /* Search forward to insert OS after AFTER_SEC output
2047*56bb7041Schristos 		     statement.  */
2048*56bb7041Schristos 		  lang_output_section_statement_type *stmt, *next;
2049*56bb7041Schristos 		  bfd_boolean found = FALSE;
2050*56bb7041Schristos 		  for (stmt = after; stmt != NULL; stmt = next)
2051*56bb7041Schristos 		    {
2052*56bb7041Schristos 		      next = stmt->next;
2053*56bb7041Schristos 		      if (insert_after)
2054*56bb7041Schristos 			{
2055*56bb7041Schristos 			  if (stmt->bfd_section == after_sec)
2056*56bb7041Schristos 			    {
2057*56bb7041Schristos 			      place_after = TRUE;
2058*56bb7041Schristos 			      found = TRUE;
2059*56bb7041Schristos 			      after = stmt;
2060*56bb7041Schristos 			      break;
2061*56bb7041Schristos 			    }
2062*56bb7041Schristos 			}
2063*56bb7041Schristos 		      else
2064*56bb7041Schristos 			{
2065*56bb7041Schristos 			  /* If INSERT_AFTER is FALSE, place OS before
2066*56bb7041Schristos 			     AFTER_SEC output statement.  */
2067*56bb7041Schristos 			  if (next && next->bfd_section == after_sec)
2068*56bb7041Schristos 			    {
2069*56bb7041Schristos 			      place_after = TRUE;
2070*56bb7041Schristos 			      found = TRUE;
2071*56bb7041Schristos 			      after = stmt;
2072*56bb7041Schristos 			      break;
2073*56bb7041Schristos 			    }
2074*56bb7041Schristos 			}
2075*56bb7041Schristos 		    }
2076*56bb7041Schristos 
2077*56bb7041Schristos 		  /* Search backward to insert OS after AFTER_SEC output
2078*56bb7041Schristos 		     statement.  */
2079*56bb7041Schristos 		  if (!found)
2080*56bb7041Schristos 		    for (stmt = after; stmt != NULL; stmt = stmt->prev)
2081*56bb7041Schristos 		      {
2082*56bb7041Schristos 			if (insert_after)
2083*56bb7041Schristos 			  {
2084*56bb7041Schristos 			    if (stmt->bfd_section == after_sec)
2085*56bb7041Schristos 			      {
2086*56bb7041Schristos 				place_after = TRUE;
2087*56bb7041Schristos 				after = stmt;
2088*56bb7041Schristos 				break;
2089*56bb7041Schristos 			      }
2090*56bb7041Schristos 			  }
2091*56bb7041Schristos 			else
2092*56bb7041Schristos 			  {
2093*56bb7041Schristos 			    /* If INSERT_AFTER is FALSE, place OS before
2094*56bb7041Schristos 			       AFTER_SEC output statement.  */
2095*56bb7041Schristos 			    if (stmt->next->bfd_section == after_sec)
2096*56bb7041Schristos 			      {
2097*56bb7041Schristos 				place_after = TRUE;
2098*56bb7041Schristos 				after = stmt;
2099*56bb7041Schristos 				break;
2100*56bb7041Schristos 			      }
2101*56bb7041Schristos 			  }
2102*56bb7041Schristos 		      }
2103*56bb7041Schristos 		}
2104*56bb7041Schristos 
2105*56bb7041Schristos 	      if (after_sec == NULL
2106*56bb7041Schristos 		  || (insert_after && after_sec->next != snew)
2107*56bb7041Schristos 		  || (!insert_after && after_sec->prev != snew))
2108*56bb7041Schristos 		{
2109*56bb7041Schristos 		  /* Unlink the section.  */
2110*56bb7041Schristos 		  bfd_section_list_remove (link_info.output_bfd, snew);
2111*56bb7041Schristos 
2112*56bb7041Schristos 		  /* Place SNEW after AFTER_SEC.  If AFTER_SEC is NULL,
2113*56bb7041Schristos 		     prepend SNEW.  */
2114*56bb7041Schristos 		  if (after_sec)
2115*56bb7041Schristos 		    {
2116*56bb7041Schristos 		      if (insert_after)
2117*56bb7041Schristos 			bfd_section_list_insert_after (link_info.output_bfd,
2118*56bb7041Schristos 						       after_sec, snew);
2119*56bb7041Schristos 		      else
2120*56bb7041Schristos 			bfd_section_list_insert_before (link_info.output_bfd,
2121*56bb7041Schristos 						       after_sec, snew);
2122*56bb7041Schristos 		    }
2123*56bb7041Schristos 		  else
2124*56bb7041Schristos 		    bfd_section_list_prepend (link_info.output_bfd, snew);
2125*56bb7041Schristos 		}
2126*56bb7041Schristos 	    }
2127*56bb7041Schristos 	  else if (as != snew && as->prev != snew)
2128*56bb7041Schristos 	    {
2129*56bb7041Schristos 	      /* Unlink the section.  */
2130*56bb7041Schristos 	      bfd_section_list_remove (link_info.output_bfd, snew);
2131*56bb7041Schristos 
2132*56bb7041Schristos 	      /* Now tack it back on in the right place.  */
2133*56bb7041Schristos 	      bfd_section_list_insert_before (link_info.output_bfd,
2134*56bb7041Schristos 					      as, snew);
2135*56bb7041Schristos 	    }
2136*56bb7041Schristos 	}
2137*56bb7041Schristos       else if (as != snew && as->prev != snew)
2138*56bb7041Schristos 	{
2139*56bb7041Schristos 	  /* Unlink the section.  */
2140*56bb7041Schristos 	  bfd_section_list_remove (link_info.output_bfd, snew);
2141*56bb7041Schristos 
2142*56bb7041Schristos 	  /* Now tack it back on in the right place.  */
2143*56bb7041Schristos 	  bfd_section_list_insert_before (link_info.output_bfd, as, snew);
2144*56bb7041Schristos 	}
2145*56bb7041Schristos 
2146*56bb7041Schristos       /* Save the end of this list.  Further ophans of this type will
2147*56bb7041Schristos 	 follow the one we've just added.  */
2148*56bb7041Schristos       place->section = &snew->next;
2149*56bb7041Schristos 
2150*56bb7041Schristos       /* The following is non-cosmetic.  We try to put the output
2151*56bb7041Schristos 	 statements in some sort of reasonable order here, because they
2152*56bb7041Schristos 	 determine the final load addresses of the orphan sections.
2153*56bb7041Schristos 	 In addition, placing output statements in the wrong order may
2154*56bb7041Schristos 	 require extra segments.  For instance, given a typical
2155*56bb7041Schristos 	 situation of all read-only sections placed in one segment and
2156*56bb7041Schristos 	 following that a segment containing all the read-write
2157*56bb7041Schristos 	 sections, we wouldn't want to place an orphan read/write
2158*56bb7041Schristos 	 section before or amongst the read-only ones.  */
2159*56bb7041Schristos       if (add.head != NULL)
2160*56bb7041Schristos 	{
2161*56bb7041Schristos 	  lang_output_section_statement_type *newly_added_os;
2162*56bb7041Schristos 
2163*56bb7041Schristos 	  /* Place OS after AFTER if AFTER_NOTE is TRUE.  */
2164*56bb7041Schristos 	  if (place_after)
2165*56bb7041Schristos 	    {
2166*56bb7041Schristos 	      lang_statement_union_type **where = insert_os_after (after);
2167*56bb7041Schristos 
2168*56bb7041Schristos 	      *add.tail = *where;
2169*56bb7041Schristos 	      *where = add.head;
2170*56bb7041Schristos 
2171*56bb7041Schristos 	      place->os_tail = &after->next;
2172*56bb7041Schristos 	    }
2173*56bb7041Schristos 	  else
2174*56bb7041Schristos 	    {
2175*56bb7041Schristos 	      /* Put it after the last orphan statement we added.  */
2176*56bb7041Schristos 	      *add.tail = *place->stmt;
2177*56bb7041Schristos 	      *place->stmt = add.head;
2178*56bb7041Schristos 	    }
2179*56bb7041Schristos 
2180*56bb7041Schristos 	  /* Fix the global list pointer if we happened to tack our
2181*56bb7041Schristos 	     new list at the tail.  */
2182*56bb7041Schristos 	  if (*stat_ptr->tail == add.head)
2183*56bb7041Schristos 	    stat_ptr->tail = add.tail;
2184*56bb7041Schristos 
2185*56bb7041Schristos 	  /* Save the end of this list.  */
2186*56bb7041Schristos 	  place->stmt = add.tail;
2187*56bb7041Schristos 
2188*56bb7041Schristos 	  /* Do the same for the list of output section statements.  */
2189*56bb7041Schristos 	  newly_added_os = *os_tail;
2190*56bb7041Schristos 	  *os_tail = NULL;
2191*56bb7041Schristos 	  newly_added_os->prev = (lang_output_section_statement_type *)
2192*56bb7041Schristos 	    ((char *) place->os_tail
2193*56bb7041Schristos 	     - offsetof (lang_output_section_statement_type, next));
2194*56bb7041Schristos 	  newly_added_os->next = *place->os_tail;
2195*56bb7041Schristos 	  if (newly_added_os->next != NULL)
2196*56bb7041Schristos 	    newly_added_os->next->prev = newly_added_os;
2197*56bb7041Schristos 	  *place->os_tail = newly_added_os;
2198*56bb7041Schristos 	  place->os_tail = &newly_added_os->next;
2199*56bb7041Schristos 
2200*56bb7041Schristos 	  /* Fixing the global list pointer here is a little different.
2201*56bb7041Schristos 	     We added to the list in lang_enter_output_section_statement,
2202*56bb7041Schristos 	     trimmed off the new output_section_statment above when
2203*56bb7041Schristos 	     assigning *os_tail = NULL, but possibly added it back in
2204*56bb7041Schristos 	     the same place when assigning *place->os_tail.  */
2205*56bb7041Schristos 	  if (*os_tail == NULL)
2206*56bb7041Schristos 	    lang_os_list.tail = (lang_statement_union_type **) os_tail;
2207*56bb7041Schristos 	}
2208*56bb7041Schristos     }
2209*56bb7041Schristos   return os;
2210*56bb7041Schristos }
2211*56bb7041Schristos 
2212*56bb7041Schristos static void
lang_print_asneeded(void)2213*56bb7041Schristos lang_print_asneeded (void)
2214*56bb7041Schristos {
2215*56bb7041Schristos   struct asneeded_minfo *m;
2216*56bb7041Schristos 
2217*56bb7041Schristos   if (asneeded_list_head == NULL)
2218*56bb7041Schristos     return;
2219*56bb7041Schristos 
2220*56bb7041Schristos   minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2221*56bb7041Schristos 
2222*56bb7041Schristos   for (m = asneeded_list_head; m != NULL; m = m->next)
2223*56bb7041Schristos     {
2224*56bb7041Schristos       size_t len;
2225*56bb7041Schristos 
2226*56bb7041Schristos       minfo ("%s", m->soname);
2227*56bb7041Schristos       len = strlen (m->soname);
2228*56bb7041Schristos 
2229*56bb7041Schristos       if (len >= 29)
2230*56bb7041Schristos 	{
2231*56bb7041Schristos 	  print_nl ();
2232*56bb7041Schristos 	  len = 0;
2233*56bb7041Schristos 	}
2234*56bb7041Schristos       while (len < 30)
2235*56bb7041Schristos 	{
2236*56bb7041Schristos 	  print_space ();
2237*56bb7041Schristos 	  ++len;
2238*56bb7041Schristos 	}
2239*56bb7041Schristos 
2240*56bb7041Schristos       if (m->ref != NULL)
2241*56bb7041Schristos 	minfo ("%pB ", m->ref);
2242*56bb7041Schristos       minfo ("(%pT)\n", m->name);
2243*56bb7041Schristos     }
2244*56bb7041Schristos }
2245*56bb7041Schristos 
2246*56bb7041Schristos static void
lang_map_flags(flagword flag)2247*56bb7041Schristos lang_map_flags (flagword flag)
2248*56bb7041Schristos {
2249*56bb7041Schristos   if (flag & SEC_ALLOC)
2250*56bb7041Schristos     minfo ("a");
2251*56bb7041Schristos 
2252*56bb7041Schristos   if (flag & SEC_CODE)
2253*56bb7041Schristos     minfo ("x");
2254*56bb7041Schristos 
2255*56bb7041Schristos   if (flag & SEC_READONLY)
2256*56bb7041Schristos     minfo ("r");
2257*56bb7041Schristos 
2258*56bb7041Schristos   if (flag & SEC_DATA)
2259*56bb7041Schristos     minfo ("w");
2260*56bb7041Schristos 
2261*56bb7041Schristos   if (flag & SEC_LOAD)
2262*56bb7041Schristos     minfo ("l");
2263*56bb7041Schristos }
2264*56bb7041Schristos 
2265*56bb7041Schristos void
lang_map(void)2266*56bb7041Schristos lang_map (void)
2267*56bb7041Schristos {
2268*56bb7041Schristos   lang_memory_region_type *m;
2269*56bb7041Schristos   bfd_boolean dis_header_printed = FALSE;
2270*56bb7041Schristos 
2271*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
2272*56bb7041Schristos     {
2273*56bb7041Schristos       asection *s;
2274*56bb7041Schristos 
2275*56bb7041Schristos       if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
2276*56bb7041Schristos 	  || file->flags.just_syms)
2277*56bb7041Schristos 	continue;
2278*56bb7041Schristos 
2279*56bb7041Schristos       if (config.print_map_discarded)
2280*56bb7041Schristos 	for (s = file->the_bfd->sections; s != NULL; s = s->next)
2281*56bb7041Schristos 	  if ((s->output_section == NULL
2282*56bb7041Schristos 	       || s->output_section->owner != link_info.output_bfd)
2283*56bb7041Schristos 	      && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
2284*56bb7041Schristos 	    {
2285*56bb7041Schristos 	      if (! dis_header_printed)
2286*56bb7041Schristos 		{
2287*56bb7041Schristos 		  fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
2288*56bb7041Schristos 		  dis_header_printed = TRUE;
2289*56bb7041Schristos 		}
2290*56bb7041Schristos 
2291*56bb7041Schristos 	      print_input_section (s, TRUE);
2292*56bb7041Schristos 	    }
2293*56bb7041Schristos     }
2294*56bb7041Schristos 
2295*56bb7041Schristos   minfo (_("\nMemory Configuration\n\n"));
2296*56bb7041Schristos   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
2297*56bb7041Schristos 	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
2298*56bb7041Schristos 
2299*56bb7041Schristos   for (m = lang_memory_region_list; m != NULL; m = m->next)
2300*56bb7041Schristos     {
2301*56bb7041Schristos       char buf[100];
2302*56bb7041Schristos       int len;
2303*56bb7041Schristos 
2304*56bb7041Schristos       fprintf (config.map_file, "%-16s ", m->name_list.name);
2305*56bb7041Schristos 
2306*56bb7041Schristos       sprintf_vma (buf, m->origin);
2307*56bb7041Schristos       minfo ("0x%s ", buf);
2308*56bb7041Schristos       len = strlen (buf);
2309*56bb7041Schristos       while (len < 16)
2310*56bb7041Schristos 	{
2311*56bb7041Schristos 	  print_space ();
2312*56bb7041Schristos 	  ++len;
2313*56bb7041Schristos 	}
2314*56bb7041Schristos 
2315*56bb7041Schristos       minfo ("0x%V", m->length);
2316*56bb7041Schristos       if (m->flags || m->not_flags)
2317*56bb7041Schristos 	{
2318*56bb7041Schristos #ifndef BFD64
2319*56bb7041Schristos 	  minfo ("        ");
2320*56bb7041Schristos #endif
2321*56bb7041Schristos 	  if (m->flags)
2322*56bb7041Schristos 	    {
2323*56bb7041Schristos 	      print_space ();
2324*56bb7041Schristos 	      lang_map_flags (m->flags);
2325*56bb7041Schristos 	    }
2326*56bb7041Schristos 
2327*56bb7041Schristos 	  if (m->not_flags)
2328*56bb7041Schristos 	    {
2329*56bb7041Schristos 	      minfo (" !");
2330*56bb7041Schristos 	      lang_map_flags (m->not_flags);
2331*56bb7041Schristos 	    }
2332*56bb7041Schristos 	}
2333*56bb7041Schristos 
2334*56bb7041Schristos       print_nl ();
2335*56bb7041Schristos     }
2336*56bb7041Schristos 
2337*56bb7041Schristos   fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
2338*56bb7041Schristos 
2339*56bb7041Schristos   if (!link_info.reduce_memory_overheads)
2340*56bb7041Schristos     {
2341*56bb7041Schristos       obstack_begin (&map_obstack, 1000);
2342*56bb7041Schristos       bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
2343*56bb7041Schristos     }
2344*56bb7041Schristos   expld.phase = lang_fixed_phase_enum;
2345*56bb7041Schristos   lang_statement_iteration++;
2346*56bb7041Schristos   print_statements ();
2347*56bb7041Schristos 
2348*56bb7041Schristos   ldemul_extra_map_file_text (link_info.output_bfd, &link_info,
2349*56bb7041Schristos 			      config.map_file);
2350*56bb7041Schristos }
2351*56bb7041Schristos 
2352*56bb7041Schristos static bfd_boolean
sort_def_symbol(struct bfd_link_hash_entry * hash_entry,void * info ATTRIBUTE_UNUSED)2353*56bb7041Schristos sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
2354*56bb7041Schristos 		 void *info ATTRIBUTE_UNUSED)
2355*56bb7041Schristos {
2356*56bb7041Schristos   if ((hash_entry->type == bfd_link_hash_defined
2357*56bb7041Schristos        || hash_entry->type == bfd_link_hash_defweak)
2358*56bb7041Schristos       && hash_entry->u.def.section->owner != link_info.output_bfd
2359*56bb7041Schristos       && hash_entry->u.def.section->owner != NULL)
2360*56bb7041Schristos     {
2361*56bb7041Schristos       input_section_userdata_type *ud;
2362*56bb7041Schristos       struct map_symbol_def *def;
2363*56bb7041Schristos 
2364*56bb7041Schristos       ud = bfd_section_userdata (hash_entry->u.def.section);
2365*56bb7041Schristos       if (!ud)
2366*56bb7041Schristos 	{
2367*56bb7041Schristos 	  ud = stat_alloc (sizeof (*ud));
2368*56bb7041Schristos 	  bfd_set_section_userdata (hash_entry->u.def.section, ud);
2369*56bb7041Schristos 	  ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2370*56bb7041Schristos 	  ud->map_symbol_def_count = 0;
2371*56bb7041Schristos 	}
2372*56bb7041Schristos       else if (!ud->map_symbol_def_tail)
2373*56bb7041Schristos 	ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2374*56bb7041Schristos 
2375*56bb7041Schristos       def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
2376*56bb7041Schristos       def->entry = hash_entry;
2377*56bb7041Schristos       *(ud->map_symbol_def_tail) = def;
2378*56bb7041Schristos       ud->map_symbol_def_tail = &def->next;
2379*56bb7041Schristos       ud->map_symbol_def_count++;
2380*56bb7041Schristos     }
2381*56bb7041Schristos   return TRUE;
2382*56bb7041Schristos }
2383*56bb7041Schristos 
2384*56bb7041Schristos /* Initialize an output section.  */
2385*56bb7041Schristos 
2386*56bb7041Schristos static void
init_os(lang_output_section_statement_type * s,flagword flags)2387*56bb7041Schristos init_os (lang_output_section_statement_type *s, flagword flags)
2388*56bb7041Schristos {
2389*56bb7041Schristos   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
2390*56bb7041Schristos     einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
2391*56bb7041Schristos 
2392*56bb7041Schristos   if (s->constraint != SPECIAL)
2393*56bb7041Schristos     s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
2394*56bb7041Schristos   if (s->bfd_section == NULL)
2395*56bb7041Schristos     s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
2396*56bb7041Schristos 							 s->name, flags);
2397*56bb7041Schristos   if (s->bfd_section == NULL)
2398*56bb7041Schristos     {
2399*56bb7041Schristos       einfo (_("%F%P: output format %s cannot represent section"
2400*56bb7041Schristos 	       " called %s: %E\n"),
2401*56bb7041Schristos 	     link_info.output_bfd->xvec->name, s->name);
2402*56bb7041Schristos     }
2403*56bb7041Schristos   s->bfd_section->output_section = s->bfd_section;
2404*56bb7041Schristos   s->bfd_section->output_offset = 0;
2405*56bb7041Schristos 
2406*56bb7041Schristos   /* Set the userdata of the output section to the output section
2407*56bb7041Schristos      statement to avoid lookup.  */
2408*56bb7041Schristos   bfd_set_section_userdata (s->bfd_section, s);
2409*56bb7041Schristos 
2410*56bb7041Schristos   /* If there is a base address, make sure that any sections it might
2411*56bb7041Schristos      mention are initialized.  */
2412*56bb7041Schristos   if (s->addr_tree != NULL)
2413*56bb7041Schristos     exp_init_os (s->addr_tree);
2414*56bb7041Schristos 
2415*56bb7041Schristos   if (s->load_base != NULL)
2416*56bb7041Schristos     exp_init_os (s->load_base);
2417*56bb7041Schristos 
2418*56bb7041Schristos   /* If supplied an alignment, set it.  */
2419*56bb7041Schristos   if (s->section_alignment != NULL)
2420*56bb7041Schristos     s->bfd_section->alignment_power = exp_get_power (s->section_alignment,
2421*56bb7041Schristos 						     "section alignment");
2422*56bb7041Schristos }
2423*56bb7041Schristos 
2424*56bb7041Schristos /* Make sure that all output sections mentioned in an expression are
2425*56bb7041Schristos    initialized.  */
2426*56bb7041Schristos 
2427*56bb7041Schristos static void
exp_init_os(etree_type * exp)2428*56bb7041Schristos exp_init_os (etree_type *exp)
2429*56bb7041Schristos {
2430*56bb7041Schristos   switch (exp->type.node_class)
2431*56bb7041Schristos     {
2432*56bb7041Schristos     case etree_assign:
2433*56bb7041Schristos     case etree_provide:
2434*56bb7041Schristos     case etree_provided:
2435*56bb7041Schristos       exp_init_os (exp->assign.src);
2436*56bb7041Schristos       break;
2437*56bb7041Schristos 
2438*56bb7041Schristos     case etree_binary:
2439*56bb7041Schristos       exp_init_os (exp->binary.lhs);
2440*56bb7041Schristos       exp_init_os (exp->binary.rhs);
2441*56bb7041Schristos       break;
2442*56bb7041Schristos 
2443*56bb7041Schristos     case etree_trinary:
2444*56bb7041Schristos       exp_init_os (exp->trinary.cond);
2445*56bb7041Schristos       exp_init_os (exp->trinary.lhs);
2446*56bb7041Schristos       exp_init_os (exp->trinary.rhs);
2447*56bb7041Schristos       break;
2448*56bb7041Schristos 
2449*56bb7041Schristos     case etree_assert:
2450*56bb7041Schristos       exp_init_os (exp->assert_s.child);
2451*56bb7041Schristos       break;
2452*56bb7041Schristos 
2453*56bb7041Schristos     case etree_unary:
2454*56bb7041Schristos       exp_init_os (exp->unary.child);
2455*56bb7041Schristos       break;
2456*56bb7041Schristos 
2457*56bb7041Schristos     case etree_name:
2458*56bb7041Schristos       switch (exp->type.node_code)
2459*56bb7041Schristos 	{
2460*56bb7041Schristos 	case ADDR:
2461*56bb7041Schristos 	case LOADADDR:
2462*56bb7041Schristos 	case SIZEOF:
2463*56bb7041Schristos 	  {
2464*56bb7041Schristos 	    lang_output_section_statement_type *os;
2465*56bb7041Schristos 
2466*56bb7041Schristos 	    os = lang_output_section_find (exp->name.name);
2467*56bb7041Schristos 	    if (os != NULL && os->bfd_section == NULL)
2468*56bb7041Schristos 	      init_os (os, 0);
2469*56bb7041Schristos 	  }
2470*56bb7041Schristos 	}
2471*56bb7041Schristos       break;
2472*56bb7041Schristos 
2473*56bb7041Schristos     default:
2474*56bb7041Schristos       break;
2475*56bb7041Schristos     }
2476*56bb7041Schristos }
2477*56bb7041Schristos 
2478*56bb7041Schristos static void
section_already_linked(bfd * abfd,asection * sec,void * data)2479*56bb7041Schristos section_already_linked (bfd *abfd, asection *sec, void *data)
2480*56bb7041Schristos {
2481*56bb7041Schristos   lang_input_statement_type *entry = (lang_input_statement_type *) data;
2482*56bb7041Schristos 
2483*56bb7041Schristos   /* If we are only reading symbols from this object, then we want to
2484*56bb7041Schristos      discard all sections.  */
2485*56bb7041Schristos   if (entry->flags.just_syms)
2486*56bb7041Schristos     {
2487*56bb7041Schristos       bfd_link_just_syms (abfd, sec, &link_info);
2488*56bb7041Schristos       return;
2489*56bb7041Schristos     }
2490*56bb7041Schristos 
2491*56bb7041Schristos   /* Deal with SHF_EXCLUDE ELF sections.  */
2492*56bb7041Schristos   if (!bfd_link_relocatable (&link_info)
2493*56bb7041Schristos       && (abfd->flags & BFD_PLUGIN) == 0
2494*56bb7041Schristos       && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2495*56bb7041Schristos     sec->output_section = bfd_abs_section_ptr;
2496*56bb7041Schristos 
2497*56bb7041Schristos   if (!(abfd->flags & DYNAMIC))
2498*56bb7041Schristos     bfd_section_already_linked (abfd, sec, &link_info);
2499*56bb7041Schristos }
2500*56bb7041Schristos 
2501*56bb7041Schristos 
2502*56bb7041Schristos /* Returns true if SECTION is one we know will be discarded based on its
2503*56bb7041Schristos    section flags, otherwise returns false.  */
2504*56bb7041Schristos 
2505*56bb7041Schristos static bfd_boolean
lang_discard_section_p(asection * section)2506*56bb7041Schristos lang_discard_section_p (asection *section)
2507*56bb7041Schristos {
2508*56bb7041Schristos   bfd_boolean discard;
2509*56bb7041Schristos   flagword flags = section->flags;
2510*56bb7041Schristos 
2511*56bb7041Schristos   /* Discard sections marked with SEC_EXCLUDE.  */
2512*56bb7041Schristos   discard = (flags & SEC_EXCLUDE) != 0;
2513*56bb7041Schristos 
2514*56bb7041Schristos   /* Discard the group descriptor sections when we're finally placing the
2515*56bb7041Schristos      sections from within the group.  */
2516*56bb7041Schristos   if ((flags & SEC_GROUP) != 0
2517*56bb7041Schristos       && link_info.resolve_section_groups)
2518*56bb7041Schristos     discard = TRUE;
2519*56bb7041Schristos 
2520*56bb7041Schristos   /* Discard debugging sections if we are stripping debugging
2521*56bb7041Schristos      information.  */
2522*56bb7041Schristos   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2523*56bb7041Schristos       && (flags & SEC_DEBUGGING) != 0)
2524*56bb7041Schristos     discard = TRUE;
2525*56bb7041Schristos 
2526*56bb7041Schristos   return discard;
2527*56bb7041Schristos }
2528*56bb7041Schristos 
2529*56bb7041Schristos /* The wild routines.
2530*56bb7041Schristos 
2531*56bb7041Schristos    These expand statements like *(.text) and foo.o to a list of
2532*56bb7041Schristos    explicit actions, like foo.o(.text), bar.o(.text) and
2533*56bb7041Schristos    foo.o(.text, .data).  */
2534*56bb7041Schristos 
2535*56bb7041Schristos /* Add SECTION to the output section OUTPUT.  Do this by creating a
2536*56bb7041Schristos    lang_input_section statement which is placed at PTR.  */
2537*56bb7041Schristos 
2538*56bb7041Schristos void
lang_add_section(lang_statement_list_type * ptr,asection * section,struct flag_info * sflag_info,lang_output_section_statement_type * output)2539*56bb7041Schristos lang_add_section (lang_statement_list_type *ptr,
2540*56bb7041Schristos 		  asection *section,
2541*56bb7041Schristos 		  struct flag_info *sflag_info,
2542*56bb7041Schristos 		  lang_output_section_statement_type *output)
2543*56bb7041Schristos {
2544*56bb7041Schristos   flagword flags = section->flags;
2545*56bb7041Schristos 
2546*56bb7041Schristos   bfd_boolean discard;
2547*56bb7041Schristos   lang_input_section_type *new_section;
2548*56bb7041Schristos   bfd *abfd = link_info.output_bfd;
2549*56bb7041Schristos 
2550*56bb7041Schristos   /* Is this section one we know should be discarded?  */
2551*56bb7041Schristos   discard = lang_discard_section_p (section);
2552*56bb7041Schristos 
2553*56bb7041Schristos   /* Discard input sections which are assigned to a section named
2554*56bb7041Schristos      DISCARD_SECTION_NAME.  */
2555*56bb7041Schristos   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2556*56bb7041Schristos     discard = TRUE;
2557*56bb7041Schristos 
2558*56bb7041Schristos   if (discard)
2559*56bb7041Schristos     {
2560*56bb7041Schristos       if (section->output_section == NULL)
2561*56bb7041Schristos 	{
2562*56bb7041Schristos 	  /* This prevents future calls from assigning this section.  */
2563*56bb7041Schristos 	  section->output_section = bfd_abs_section_ptr;
2564*56bb7041Schristos 	}
2565*56bb7041Schristos       else if (link_info.non_contiguous_regions_warnings)
2566*56bb7041Schristos 	einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2567*56bb7041Schristos 		 "section `%pA' from '%pB' match /DISCARD/ clause.\n"),
2568*56bb7041Schristos 	       NULL, section, section->owner);
2569*56bb7041Schristos 
2570*56bb7041Schristos       return;
2571*56bb7041Schristos     }
2572*56bb7041Schristos 
2573*56bb7041Schristos   if (sflag_info)
2574*56bb7041Schristos     {
2575*56bb7041Schristos       bfd_boolean keep;
2576*56bb7041Schristos 
2577*56bb7041Schristos       keep = bfd_lookup_section_flags (&link_info, sflag_info, section);
2578*56bb7041Schristos       if (!keep)
2579*56bb7041Schristos 	return;
2580*56bb7041Schristos     }
2581*56bb7041Schristos 
2582*56bb7041Schristos   if (section->output_section != NULL)
2583*56bb7041Schristos     {
2584*56bb7041Schristos       if (!link_info.non_contiguous_regions)
2585*56bb7041Schristos 	return;
2586*56bb7041Schristos 
2587*56bb7041Schristos       /* SECTION has already been handled in a special way
2588*56bb7041Schristos 	 (eg. LINK_ONCE): skip it.  */
2589*56bb7041Schristos       if (bfd_is_abs_section (section->output_section))
2590*56bb7041Schristos 	return;
2591*56bb7041Schristos 
2592*56bb7041Schristos       /* Already assigned to the same output section, do not process
2593*56bb7041Schristos 	 it again, to avoid creating loops between duplicate sections
2594*56bb7041Schristos 	 later.  */
2595*56bb7041Schristos       if (section->output_section == output->bfd_section)
2596*56bb7041Schristos 	return;
2597*56bb7041Schristos 
2598*56bb7041Schristos       if (link_info.non_contiguous_regions_warnings && output->bfd_section)
2599*56bb7041Schristos 	einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2600*56bb7041Schristos 		 "change behaviour for section `%pA' from '%pB' (assigned to "
2601*56bb7041Schristos 		 "%pA, but additional match: %pA)\n"),
2602*56bb7041Schristos 	       NULL, section, section->owner, section->output_section,
2603*56bb7041Schristos 	       output->bfd_section);
2604*56bb7041Schristos 
2605*56bb7041Schristos       /* SECTION has already been assigned to an output section, but
2606*56bb7041Schristos 	 the user allows it to be mapped to another one in case it
2607*56bb7041Schristos 	 overflows. We'll later update the actual output section in
2608*56bb7041Schristos 	 size_input_section as appropriate.  */
2609*56bb7041Schristos     }
2610*56bb7041Schristos 
2611*56bb7041Schristos   /* We don't copy the SEC_NEVER_LOAD flag from an input section
2612*56bb7041Schristos      to an output section, because we want to be able to include a
2613*56bb7041Schristos      SEC_NEVER_LOAD section in the middle of an otherwise loaded
2614*56bb7041Schristos      section (I don't know why we want to do this, but we do).
2615*56bb7041Schristos      build_link_order in ldwrite.c handles this case by turning
2616*56bb7041Schristos      the embedded SEC_NEVER_LOAD section into a fill.  */
2617*56bb7041Schristos   flags &= ~ SEC_NEVER_LOAD;
2618*56bb7041Schristos 
2619*56bb7041Schristos   /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2620*56bb7041Schristos      already been processed.  One reason to do this is that on pe
2621*56bb7041Schristos      format targets, .text$foo sections go into .text and it's odd
2622*56bb7041Schristos      to see .text with SEC_LINK_ONCE set.  */
2623*56bb7041Schristos   if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP))
2624*56bb7041Schristos     {
2625*56bb7041Schristos       if (link_info.resolve_section_groups)
2626*56bb7041Schristos 	flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
2627*56bb7041Schristos       else
2628*56bb7041Schristos 	flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC);
2629*56bb7041Schristos     }
2630*56bb7041Schristos   else if (!bfd_link_relocatable (&link_info))
2631*56bb7041Schristos     flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
2632*56bb7041Schristos 
2633*56bb7041Schristos   switch (output->sectype)
2634*56bb7041Schristos     {
2635*56bb7041Schristos     case normal_section:
2636*56bb7041Schristos     case overlay_section:
2637*56bb7041Schristos     case first_overlay_section:
2638*56bb7041Schristos       break;
2639*56bb7041Schristos     case noalloc_section:
2640*56bb7041Schristos       flags &= ~SEC_ALLOC;
2641*56bb7041Schristos       break;
2642*56bb7041Schristos     case noload_section:
2643*56bb7041Schristos       flags &= ~SEC_LOAD;
2644*56bb7041Schristos       flags |= SEC_NEVER_LOAD;
2645*56bb7041Schristos       /* Unfortunately GNU ld has managed to evolve two different
2646*56bb7041Schristos 	 meanings to NOLOAD in scripts.  ELF gets a .bss style noload,
2647*56bb7041Schristos 	 alloc, no contents section.  All others get a noload, noalloc
2648*56bb7041Schristos 	 section.  */
2649*56bb7041Schristos       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
2650*56bb7041Schristos 	flags &= ~SEC_HAS_CONTENTS;
2651*56bb7041Schristos       else
2652*56bb7041Schristos 	flags &= ~SEC_ALLOC;
2653*56bb7041Schristos       break;
2654*56bb7041Schristos     }
2655*56bb7041Schristos 
2656*56bb7041Schristos   if (output->bfd_section == NULL)
2657*56bb7041Schristos     init_os (output, flags);
2658*56bb7041Schristos 
2659*56bb7041Schristos   /* If SEC_READONLY is not set in the input section, then clear
2660*56bb7041Schristos      it from the output section.  */
2661*56bb7041Schristos   output->bfd_section->flags &= flags | ~SEC_READONLY;
2662*56bb7041Schristos 
2663*56bb7041Schristos   if (output->bfd_section->linker_has_input)
2664*56bb7041Schristos     {
2665*56bb7041Schristos       /* Only set SEC_READONLY flag on the first input section.  */
2666*56bb7041Schristos       flags &= ~ SEC_READONLY;
2667*56bb7041Schristos 
2668*56bb7041Schristos       /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
2669*56bb7041Schristos       if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2670*56bb7041Schristos 	  != (flags & (SEC_MERGE | SEC_STRINGS))
2671*56bb7041Schristos 	  || ((flags & SEC_MERGE) != 0
2672*56bb7041Schristos 	      && output->bfd_section->entsize != section->entsize))
2673*56bb7041Schristos 	{
2674*56bb7041Schristos 	  output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2675*56bb7041Schristos 	  flags &= ~ (SEC_MERGE | SEC_STRINGS);
2676*56bb7041Schristos 	}
2677*56bb7041Schristos     }
2678*56bb7041Schristos   output->bfd_section->flags |= flags;
2679*56bb7041Schristos 
2680*56bb7041Schristos   if (!output->bfd_section->linker_has_input)
2681*56bb7041Schristos     {
2682*56bb7041Schristos       output->bfd_section->linker_has_input = 1;
2683*56bb7041Schristos       /* This must happen after flags have been updated.  The output
2684*56bb7041Schristos 	 section may have been created before we saw its first input
2685*56bb7041Schristos 	 section, eg. for a data statement.  */
2686*56bb7041Schristos       bfd_init_private_section_data (section->owner, section,
2687*56bb7041Schristos 				     link_info.output_bfd,
2688*56bb7041Schristos 				     output->bfd_section,
2689*56bb7041Schristos 				     &link_info);
2690*56bb7041Schristos       if ((flags & SEC_MERGE) != 0)
2691*56bb7041Schristos 	output->bfd_section->entsize = section->entsize;
2692*56bb7041Schristos     }
2693*56bb7041Schristos 
2694*56bb7041Schristos   if ((flags & SEC_TIC54X_BLOCK) != 0
2695*56bb7041Schristos       && bfd_get_arch (section->owner) == bfd_arch_tic54x)
2696*56bb7041Schristos     {
2697*56bb7041Schristos       /* FIXME: This value should really be obtained from the bfd...  */
2698*56bb7041Schristos       output->block_value = 128;
2699*56bb7041Schristos     }
2700*56bb7041Schristos 
2701*56bb7041Schristos   if (section->alignment_power > output->bfd_section->alignment_power)
2702*56bb7041Schristos     output->bfd_section->alignment_power = section->alignment_power;
2703*56bb7041Schristos 
2704*56bb7041Schristos   section->output_section = output->bfd_section;
2705*56bb7041Schristos 
2706*56bb7041Schristos   if (!map_head_is_link_order)
2707*56bb7041Schristos     {
2708*56bb7041Schristos       asection *s = output->bfd_section->map_tail.s;
2709*56bb7041Schristos       output->bfd_section->map_tail.s = section;
2710*56bb7041Schristos       section->map_head.s = NULL;
2711*56bb7041Schristos       section->map_tail.s = s;
2712*56bb7041Schristos       if (s != NULL)
2713*56bb7041Schristos 	s->map_head.s = section;
2714*56bb7041Schristos       else
2715*56bb7041Schristos 	output->bfd_section->map_head.s = section;
2716*56bb7041Schristos     }
2717*56bb7041Schristos 
2718*56bb7041Schristos   /* Add a section reference to the list.  */
2719*56bb7041Schristos   new_section = new_stat (lang_input_section, ptr);
2720*56bb7041Schristos   new_section->section = section;
2721*56bb7041Schristos }
2722*56bb7041Schristos 
2723*56bb7041Schristos /* Handle wildcard sorting.  This returns the lang_input_section which
2724*56bb7041Schristos    should follow the one we are going to create for SECTION and FILE,
2725*56bb7041Schristos    based on the sorting requirements of WILD.  It returns NULL if the
2726*56bb7041Schristos    new section should just go at the end of the current list.  */
2727*56bb7041Schristos 
2728*56bb7041Schristos static lang_statement_union_type *
wild_sort(lang_wild_statement_type * wild,struct wildcard_list * sec,lang_input_statement_type * file,asection * section)2729*56bb7041Schristos wild_sort (lang_wild_statement_type *wild,
2730*56bb7041Schristos 	   struct wildcard_list *sec,
2731*56bb7041Schristos 	   lang_input_statement_type *file,
2732*56bb7041Schristos 	   asection *section)
2733*56bb7041Schristos {
2734*56bb7041Schristos   lang_statement_union_type *l;
2735*56bb7041Schristos 
2736*56bb7041Schristos   if (!wild->filenames_sorted
2737*56bb7041Schristos       && (sec == NULL || sec->spec.sorted == none))
2738*56bb7041Schristos     return NULL;
2739*56bb7041Schristos 
2740*56bb7041Schristos   for (l = wild->children.head; l != NULL; l = l->header.next)
2741*56bb7041Schristos     {
2742*56bb7041Schristos       lang_input_section_type *ls;
2743*56bb7041Schristos 
2744*56bb7041Schristos       if (l->header.type != lang_input_section_enum)
2745*56bb7041Schristos 	continue;
2746*56bb7041Schristos       ls = &l->input_section;
2747*56bb7041Schristos 
2748*56bb7041Schristos       /* Sorting by filename takes precedence over sorting by section
2749*56bb7041Schristos 	 name.  */
2750*56bb7041Schristos 
2751*56bb7041Schristos       if (wild->filenames_sorted)
2752*56bb7041Schristos 	{
2753*56bb7041Schristos 	  const char *fn, *ln;
2754*56bb7041Schristos 	  bfd_boolean fa, la;
2755*56bb7041Schristos 	  int i;
2756*56bb7041Schristos 
2757*56bb7041Schristos 	  /* The PE support for the .idata section as generated by
2758*56bb7041Schristos 	     dlltool assumes that files will be sorted by the name of
2759*56bb7041Schristos 	     the archive and then the name of the file within the
2760*56bb7041Schristos 	     archive.  */
2761*56bb7041Schristos 
2762*56bb7041Schristos 	  if (file->the_bfd != NULL
2763*56bb7041Schristos 	      && file->the_bfd->my_archive != NULL)
2764*56bb7041Schristos 	    {
2765*56bb7041Schristos 	      fn = bfd_get_filename (file->the_bfd->my_archive);
2766*56bb7041Schristos 	      fa = TRUE;
2767*56bb7041Schristos 	    }
2768*56bb7041Schristos 	  else
2769*56bb7041Schristos 	    {
2770*56bb7041Schristos 	      fn = file->filename;
2771*56bb7041Schristos 	      fa = FALSE;
2772*56bb7041Schristos 	    }
2773*56bb7041Schristos 
2774*56bb7041Schristos 	  if (ls->section->owner->my_archive != NULL)
2775*56bb7041Schristos 	    {
2776*56bb7041Schristos 	      ln = bfd_get_filename (ls->section->owner->my_archive);
2777*56bb7041Schristos 	      la = TRUE;
2778*56bb7041Schristos 	    }
2779*56bb7041Schristos 	  else
2780*56bb7041Schristos 	    {
2781*56bb7041Schristos 	      ln = bfd_get_filename (ls->section->owner);
2782*56bb7041Schristos 	      la = FALSE;
2783*56bb7041Schristos 	    }
2784*56bb7041Schristos 
2785*56bb7041Schristos 	  i = filename_cmp (fn, ln);
2786*56bb7041Schristos 	  if (i > 0)
2787*56bb7041Schristos 	    continue;
2788*56bb7041Schristos 	  else if (i < 0)
2789*56bb7041Schristos 	    break;
2790*56bb7041Schristos 
2791*56bb7041Schristos 	  if (fa || la)
2792*56bb7041Schristos 	    {
2793*56bb7041Schristos 	      if (fa)
2794*56bb7041Schristos 		fn = file->filename;
2795*56bb7041Schristos 	      if (la)
2796*56bb7041Schristos 		ln = bfd_get_filename (ls->section->owner);
2797*56bb7041Schristos 
2798*56bb7041Schristos 	      i = filename_cmp (fn, ln);
2799*56bb7041Schristos 	      if (i > 0)
2800*56bb7041Schristos 		continue;
2801*56bb7041Schristos 	      else if (i < 0)
2802*56bb7041Schristos 		break;
2803*56bb7041Schristos 	    }
2804*56bb7041Schristos 	}
2805*56bb7041Schristos 
2806*56bb7041Schristos       /* Here either the files are not sorted by name, or we are
2807*56bb7041Schristos 	 looking at the sections for this file.  */
2808*56bb7041Schristos 
2809*56bb7041Schristos       if (sec != NULL
2810*56bb7041Schristos 	  && sec->spec.sorted != none
2811*56bb7041Schristos 	  && sec->spec.sorted != by_none)
2812*56bb7041Schristos 	if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2813*56bb7041Schristos 	  break;
2814*56bb7041Schristos     }
2815*56bb7041Schristos 
2816*56bb7041Schristos   return l;
2817*56bb7041Schristos }
2818*56bb7041Schristos 
2819*56bb7041Schristos /* Expand a wild statement for a particular FILE.  SECTION may be
2820*56bb7041Schristos    NULL, in which case it is a wild card.  */
2821*56bb7041Schristos 
2822*56bb7041Schristos static void
output_section_callback(lang_wild_statement_type * ptr,struct wildcard_list * sec,asection * section,struct flag_info * sflag_info,lang_input_statement_type * file,void * output)2823*56bb7041Schristos output_section_callback (lang_wild_statement_type *ptr,
2824*56bb7041Schristos 			 struct wildcard_list *sec,
2825*56bb7041Schristos 			 asection *section,
2826*56bb7041Schristos 			 struct flag_info *sflag_info,
2827*56bb7041Schristos 			 lang_input_statement_type *file,
2828*56bb7041Schristos 			 void *output)
2829*56bb7041Schristos {
2830*56bb7041Schristos   lang_statement_union_type *before;
2831*56bb7041Schristos   lang_output_section_statement_type *os;
2832*56bb7041Schristos 
2833*56bb7041Schristos   os = (lang_output_section_statement_type *) output;
2834*56bb7041Schristos 
2835*56bb7041Schristos   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
2836*56bb7041Schristos   if (unique_section_p (section, os))
2837*56bb7041Schristos     return;
2838*56bb7041Schristos 
2839*56bb7041Schristos   before = wild_sort (ptr, sec, file, section);
2840*56bb7041Schristos 
2841*56bb7041Schristos   /* Here BEFORE points to the lang_input_section which
2842*56bb7041Schristos      should follow the one we are about to add.  If BEFORE
2843*56bb7041Schristos      is NULL, then the section should just go at the end
2844*56bb7041Schristos      of the current list.  */
2845*56bb7041Schristos 
2846*56bb7041Schristos   if (before == NULL)
2847*56bb7041Schristos     lang_add_section (&ptr->children, section, sflag_info, os);
2848*56bb7041Schristos   else
2849*56bb7041Schristos     {
2850*56bb7041Schristos       lang_statement_list_type list;
2851*56bb7041Schristos       lang_statement_union_type **pp;
2852*56bb7041Schristos 
2853*56bb7041Schristos       lang_list_init (&list);
2854*56bb7041Schristos       lang_add_section (&list, section, sflag_info, os);
2855*56bb7041Schristos 
2856*56bb7041Schristos       /* If we are discarding the section, LIST.HEAD will
2857*56bb7041Schristos 	 be NULL.  */
2858*56bb7041Schristos       if (list.head != NULL)
2859*56bb7041Schristos 	{
2860*56bb7041Schristos 	  ASSERT (list.head->header.next == NULL);
2861*56bb7041Schristos 
2862*56bb7041Schristos 	  for (pp = &ptr->children.head;
2863*56bb7041Schristos 	       *pp != before;
2864*56bb7041Schristos 	       pp = &(*pp)->header.next)
2865*56bb7041Schristos 	    ASSERT (*pp != NULL);
2866*56bb7041Schristos 
2867*56bb7041Schristos 	  list.head->header.next = *pp;
2868*56bb7041Schristos 	  *pp = list.head;
2869*56bb7041Schristos 	}
2870*56bb7041Schristos     }
2871*56bb7041Schristos }
2872*56bb7041Schristos 
2873*56bb7041Schristos /* Check if all sections in a wild statement for a particular FILE
2874*56bb7041Schristos    are readonly.  */
2875*56bb7041Schristos 
2876*56bb7041Schristos static void
check_section_callback(lang_wild_statement_type * ptr ATTRIBUTE_UNUSED,struct wildcard_list * sec ATTRIBUTE_UNUSED,asection * section,struct flag_info * sflag_info ATTRIBUTE_UNUSED,lang_input_statement_type * file ATTRIBUTE_UNUSED,void * output)2877*56bb7041Schristos check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2878*56bb7041Schristos 			struct wildcard_list *sec ATTRIBUTE_UNUSED,
2879*56bb7041Schristos 			asection *section,
2880*56bb7041Schristos 			struct flag_info *sflag_info ATTRIBUTE_UNUSED,
2881*56bb7041Schristos 			lang_input_statement_type *file ATTRIBUTE_UNUSED,
2882*56bb7041Schristos 			void *output)
2883*56bb7041Schristos {
2884*56bb7041Schristos   lang_output_section_statement_type *os;
2885*56bb7041Schristos 
2886*56bb7041Schristos   os = (lang_output_section_statement_type *) output;
2887*56bb7041Schristos 
2888*56bb7041Schristos   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
2889*56bb7041Schristos   if (unique_section_p (section, os))
2890*56bb7041Schristos     return;
2891*56bb7041Schristos 
2892*56bb7041Schristos   if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2893*56bb7041Schristos     os->all_input_readonly = FALSE;
2894*56bb7041Schristos }
2895*56bb7041Schristos 
2896*56bb7041Schristos /* This is passed a file name which must have been seen already and
2897*56bb7041Schristos    added to the statement tree.  We will see if it has been opened
2898*56bb7041Schristos    already and had its symbols read.  If not then we'll read it.  */
2899*56bb7041Schristos 
2900*56bb7041Schristos static lang_input_statement_type *
lookup_name(const char * name)2901*56bb7041Schristos lookup_name (const char *name)
2902*56bb7041Schristos {
2903*56bb7041Schristos   lang_input_statement_type *search;
2904*56bb7041Schristos 
2905*56bb7041Schristos   for (search = (void *) input_file_chain.head;
2906*56bb7041Schristos        search != NULL;
2907*56bb7041Schristos        search = search->next_real_file)
2908*56bb7041Schristos     {
2909*56bb7041Schristos       /* Use the local_sym_name as the name of the file that has
2910*56bb7041Schristos 	 already been loaded as filename might have been transformed
2911*56bb7041Schristos 	 via the search directory lookup mechanism.  */
2912*56bb7041Schristos       const char *filename = search->local_sym_name;
2913*56bb7041Schristos 
2914*56bb7041Schristos       if (filename != NULL
2915*56bb7041Schristos 	  && filename_cmp (filename, name) == 0)
2916*56bb7041Schristos 	break;
2917*56bb7041Schristos     }
2918*56bb7041Schristos 
2919*56bb7041Schristos   if (search == NULL)
2920*56bb7041Schristos     {
2921*56bb7041Schristos       /* Arrange to splice the input statement added by new_afile into
2922*56bb7041Schristos 	 statement_list after the current input_file_chain tail.
2923*56bb7041Schristos 	 We know input_file_chain is not an empty list, and that
2924*56bb7041Schristos 	 lookup_name was called via open_input_bfds.  Later calls to
2925*56bb7041Schristos 	 lookup_name should always match an existing input_statement.  */
2926*56bb7041Schristos       lang_statement_union_type **tail = stat_ptr->tail;
2927*56bb7041Schristos       lang_statement_union_type **after
2928*56bb7041Schristos 	= (void *) ((char *) input_file_chain.tail
2929*56bb7041Schristos 		    - offsetof (lang_input_statement_type, next_real_file)
2930*56bb7041Schristos 		    + offsetof (lang_input_statement_type, header.next));
2931*56bb7041Schristos       lang_statement_union_type *rest = *after;
2932*56bb7041Schristos       stat_ptr->tail = after;
2933*56bb7041Schristos       search = new_afile (name, lang_input_file_is_search_file_enum,
2934*56bb7041Schristos 			  default_target, NULL);
2935*56bb7041Schristos       *stat_ptr->tail = rest;
2936*56bb7041Schristos       if (*tail == NULL)
2937*56bb7041Schristos 	stat_ptr->tail = tail;
2938*56bb7041Schristos     }
2939*56bb7041Schristos 
2940*56bb7041Schristos   /* If we have already added this file, or this file is not real
2941*56bb7041Schristos      don't add this file.  */
2942*56bb7041Schristos   if (search->flags.loaded || !search->flags.real)
2943*56bb7041Schristos     return search;
2944*56bb7041Schristos 
2945*56bb7041Schristos   if (!load_symbols (search, NULL))
2946*56bb7041Schristos     return NULL;
2947*56bb7041Schristos 
2948*56bb7041Schristos   return search;
2949*56bb7041Schristos }
2950*56bb7041Schristos 
2951*56bb7041Schristos /* Save LIST as a list of libraries whose symbols should not be exported.  */
2952*56bb7041Schristos 
2953*56bb7041Schristos struct excluded_lib
2954*56bb7041Schristos {
2955*56bb7041Schristos   char *name;
2956*56bb7041Schristos   struct excluded_lib *next;
2957*56bb7041Schristos };
2958*56bb7041Schristos static struct excluded_lib *excluded_libs;
2959*56bb7041Schristos 
2960*56bb7041Schristos void
add_excluded_libs(const char * list)2961*56bb7041Schristos add_excluded_libs (const char *list)
2962*56bb7041Schristos {
2963*56bb7041Schristos   const char *p = list, *end;
2964*56bb7041Schristos 
2965*56bb7041Schristos   while (*p != '\0')
2966*56bb7041Schristos     {
2967*56bb7041Schristos       struct excluded_lib *entry;
2968*56bb7041Schristos       end = strpbrk (p, ",:");
2969*56bb7041Schristos       if (end == NULL)
2970*56bb7041Schristos 	end = p + strlen (p);
2971*56bb7041Schristos       entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
2972*56bb7041Schristos       entry->next = excluded_libs;
2973*56bb7041Schristos       entry->name = (char *) xmalloc (end - p + 1);
2974*56bb7041Schristos       memcpy (entry->name, p, end - p);
2975*56bb7041Schristos       entry->name[end - p] = '\0';
2976*56bb7041Schristos       excluded_libs = entry;
2977*56bb7041Schristos       if (*end == '\0')
2978*56bb7041Schristos 	break;
2979*56bb7041Schristos       p = end + 1;
2980*56bb7041Schristos     }
2981*56bb7041Schristos }
2982*56bb7041Schristos 
2983*56bb7041Schristos static void
check_excluded_libs(bfd * abfd)2984*56bb7041Schristos check_excluded_libs (bfd *abfd)
2985*56bb7041Schristos {
2986*56bb7041Schristos   struct excluded_lib *lib = excluded_libs;
2987*56bb7041Schristos 
2988*56bb7041Schristos   while (lib)
2989*56bb7041Schristos     {
2990*56bb7041Schristos       int len = strlen (lib->name);
2991*56bb7041Schristos       const char *filename = lbasename (bfd_get_filename (abfd));
2992*56bb7041Schristos 
2993*56bb7041Schristos       if (strcmp (lib->name, "ALL") == 0)
2994*56bb7041Schristos 	{
2995*56bb7041Schristos 	  abfd->no_export = TRUE;
2996*56bb7041Schristos 	  return;
2997*56bb7041Schristos 	}
2998*56bb7041Schristos 
2999*56bb7041Schristos       if (filename_ncmp (lib->name, filename, len) == 0
3000*56bb7041Schristos 	  && (filename[len] == '\0'
3001*56bb7041Schristos 	      || (filename[len] == '.' && filename[len + 1] == 'a'
3002*56bb7041Schristos 		  && filename[len + 2] == '\0')))
3003*56bb7041Schristos 	{
3004*56bb7041Schristos 	  abfd->no_export = TRUE;
3005*56bb7041Schristos 	  return;
3006*56bb7041Schristos 	}
3007*56bb7041Schristos 
3008*56bb7041Schristos       lib = lib->next;
3009*56bb7041Schristos     }
3010*56bb7041Schristos }
3011*56bb7041Schristos 
3012*56bb7041Schristos /* Get the symbols for an input file.  */
3013*56bb7041Schristos 
3014*56bb7041Schristos bfd_boolean
load_symbols(lang_input_statement_type * entry,lang_statement_list_type * place)3015*56bb7041Schristos load_symbols (lang_input_statement_type *entry,
3016*56bb7041Schristos 	      lang_statement_list_type *place)
3017*56bb7041Schristos {
3018*56bb7041Schristos   char **matching;
3019*56bb7041Schristos 
3020*56bb7041Schristos   if (entry->flags.loaded)
3021*56bb7041Schristos     return TRUE;
3022*56bb7041Schristos 
3023*56bb7041Schristos   ldfile_open_file (entry);
3024*56bb7041Schristos 
3025*56bb7041Schristos   /* Do not process further if the file was missing.  */
3026*56bb7041Schristos   if (entry->flags.missing_file)
3027*56bb7041Schristos     return TRUE;
3028*56bb7041Schristos 
3029*56bb7041Schristos   if (trace_files || verbose)
3030*56bb7041Schristos     info_msg ("%pI\n", entry);
3031*56bb7041Schristos 
3032*56bb7041Schristos   if (!bfd_check_format (entry->the_bfd, bfd_archive)
3033*56bb7041Schristos       && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
3034*56bb7041Schristos     {
3035*56bb7041Schristos       bfd_error_type err;
3036*56bb7041Schristos       struct lang_input_statement_flags save_flags;
3037*56bb7041Schristos       extern FILE *yyin;
3038*56bb7041Schristos 
3039*56bb7041Schristos       err = bfd_get_error ();
3040*56bb7041Schristos 
3041*56bb7041Schristos       /* See if the emulation has some special knowledge.  */
3042*56bb7041Schristos       if (ldemul_unrecognized_file (entry))
3043*56bb7041Schristos 	return TRUE;
3044*56bb7041Schristos 
3045*56bb7041Schristos       if (err == bfd_error_file_ambiguously_recognized)
3046*56bb7041Schristos 	{
3047*56bb7041Schristos 	  char **p;
3048*56bb7041Schristos 
3049*56bb7041Schristos 	  einfo (_("%P: %pB: file not recognized: %E;"
3050*56bb7041Schristos 		   " matching formats:"), entry->the_bfd);
3051*56bb7041Schristos 	  for (p = matching; *p != NULL; p++)
3052*56bb7041Schristos 	    einfo (" %s", *p);
3053*56bb7041Schristos 	  einfo ("%F\n");
3054*56bb7041Schristos 	}
3055*56bb7041Schristos       else if (err != bfd_error_file_not_recognized
3056*56bb7041Schristos 	       || place == NULL)
3057*56bb7041Schristos 	einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd);
3058*56bb7041Schristos 
3059*56bb7041Schristos       bfd_close (entry->the_bfd);
3060*56bb7041Schristos       entry->the_bfd = NULL;
3061*56bb7041Schristos 
3062*56bb7041Schristos       /* Try to interpret the file as a linker script.  */
3063*56bb7041Schristos       save_flags = input_flags;
3064*56bb7041Schristos       ldfile_open_command_file (entry->filename);
3065*56bb7041Schristos 
3066*56bb7041Schristos       push_stat_ptr (place);
3067*56bb7041Schristos       input_flags.add_DT_NEEDED_for_regular
3068*56bb7041Schristos 	= entry->flags.add_DT_NEEDED_for_regular;
3069*56bb7041Schristos       input_flags.add_DT_NEEDED_for_dynamic
3070*56bb7041Schristos 	= entry->flags.add_DT_NEEDED_for_dynamic;
3071*56bb7041Schristos       input_flags.whole_archive = entry->flags.whole_archive;
3072*56bb7041Schristos       input_flags.dynamic = entry->flags.dynamic;
3073*56bb7041Schristos 
3074*56bb7041Schristos       ldfile_assumed_script = TRUE;
3075*56bb7041Schristos       parser_input = input_script;
3076*56bb7041Schristos       current_input_file = entry->filename;
3077*56bb7041Schristos       yyparse ();
3078*56bb7041Schristos       current_input_file = NULL;
3079*56bb7041Schristos       ldfile_assumed_script = FALSE;
3080*56bb7041Schristos 
3081*56bb7041Schristos       /* missing_file is sticky.  sysrooted will already have been
3082*56bb7041Schristos 	 restored when seeing EOF in yyparse, but no harm to restore
3083*56bb7041Schristos 	 again.  */
3084*56bb7041Schristos       save_flags.missing_file |= input_flags.missing_file;
3085*56bb7041Schristos       input_flags = save_flags;
3086*56bb7041Schristos       pop_stat_ptr ();
3087*56bb7041Schristos       fclose (yyin);
3088*56bb7041Schristos       yyin = NULL;
3089*56bb7041Schristos       entry->flags.loaded = TRUE;
3090*56bb7041Schristos 
3091*56bb7041Schristos       return TRUE;
3092*56bb7041Schristos     }
3093*56bb7041Schristos 
3094*56bb7041Schristos   if (ldemul_recognized_file (entry))
3095*56bb7041Schristos     return TRUE;
3096*56bb7041Schristos 
3097*56bb7041Schristos   /* We don't call ldlang_add_file for an archive.  Instead, the
3098*56bb7041Schristos      add_symbols entry point will call ldlang_add_file, via the
3099*56bb7041Schristos      add_archive_element callback, for each element of the archive
3100*56bb7041Schristos      which is used.  */
3101*56bb7041Schristos   switch (bfd_get_format (entry->the_bfd))
3102*56bb7041Schristos     {
3103*56bb7041Schristos     default:
3104*56bb7041Schristos       break;
3105*56bb7041Schristos 
3106*56bb7041Schristos     case bfd_object:
3107*56bb7041Schristos       if (!entry->flags.reload)
3108*56bb7041Schristos 	ldlang_add_file (entry);
3109*56bb7041Schristos       break;
3110*56bb7041Schristos 
3111*56bb7041Schristos     case bfd_archive:
3112*56bb7041Schristos       check_excluded_libs (entry->the_bfd);
3113*56bb7041Schristos 
3114*56bb7041Schristos       bfd_set_usrdata (entry->the_bfd, entry);
3115*56bb7041Schristos       if (entry->flags.whole_archive)
3116*56bb7041Schristos 	{
3117*56bb7041Schristos 	  bfd *member = NULL;
3118*56bb7041Schristos 	  bfd_boolean loaded = TRUE;
3119*56bb7041Schristos 
3120*56bb7041Schristos 	  for (;;)
3121*56bb7041Schristos 	    {
3122*56bb7041Schristos 	      bfd *subsbfd;
3123*56bb7041Schristos 	      member = bfd_openr_next_archived_file (entry->the_bfd, member);
3124*56bb7041Schristos 
3125*56bb7041Schristos 	      if (member == NULL)
3126*56bb7041Schristos 		break;
3127*56bb7041Schristos 
3128*56bb7041Schristos 	      if (!bfd_check_format (member, bfd_object))
3129*56bb7041Schristos 		{
3130*56bb7041Schristos 		  einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3131*56bb7041Schristos 			 entry->the_bfd, member);
3132*56bb7041Schristos 		  loaded = FALSE;
3133*56bb7041Schristos 		}
3134*56bb7041Schristos 
3135*56bb7041Schristos 	      subsbfd = member;
3136*56bb7041Schristos 	      if (!(*link_info.callbacks
3137*56bb7041Schristos 		    ->add_archive_element) (&link_info, member,
3138*56bb7041Schristos 					    "--whole-archive", &subsbfd))
3139*56bb7041Schristos 		abort ();
3140*56bb7041Schristos 
3141*56bb7041Schristos 	      /* Potentially, the add_archive_element hook may have set a
3142*56bb7041Schristos 		 substitute BFD for us.  */
3143*56bb7041Schristos 	      if (!bfd_link_add_symbols (subsbfd, &link_info))
3144*56bb7041Schristos 		{
3145*56bb7041Schristos 		  einfo (_("%F%P: %pB: error adding symbols: %E\n"), member);
3146*56bb7041Schristos 		  loaded = FALSE;
3147*56bb7041Schristos 		}
3148*56bb7041Schristos 	    }
3149*56bb7041Schristos 
3150*56bb7041Schristos 	  entry->flags.loaded = loaded;
3151*56bb7041Schristos 	  return loaded;
3152*56bb7041Schristos 	}
3153*56bb7041Schristos       break;
3154*56bb7041Schristos     }
3155*56bb7041Schristos 
3156*56bb7041Schristos   if (bfd_link_add_symbols (entry->the_bfd, &link_info))
3157*56bb7041Schristos     entry->flags.loaded = TRUE;
3158*56bb7041Schristos   else
3159*56bb7041Schristos     einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd);
3160*56bb7041Schristos 
3161*56bb7041Schristos   return entry->flags.loaded;
3162*56bb7041Schristos }
3163*56bb7041Schristos 
3164*56bb7041Schristos /* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
3165*56bb7041Schristos    may be NULL, indicating that it is a wildcard.  Separate
3166*56bb7041Schristos    lang_input_section statements are created for each part of the
3167*56bb7041Schristos    expansion; they are added after the wild statement S.  OUTPUT is
3168*56bb7041Schristos    the output section.  */
3169*56bb7041Schristos 
3170*56bb7041Schristos static void
wild(lang_wild_statement_type * s,const char * target ATTRIBUTE_UNUSED,lang_output_section_statement_type * output)3171*56bb7041Schristos wild (lang_wild_statement_type *s,
3172*56bb7041Schristos       const char *target ATTRIBUTE_UNUSED,
3173*56bb7041Schristos       lang_output_section_statement_type *output)
3174*56bb7041Schristos {
3175*56bb7041Schristos   struct wildcard_list *sec;
3176*56bb7041Schristos 
3177*56bb7041Schristos   if (s->handler_data[0]
3178*56bb7041Schristos       && s->handler_data[0]->spec.sorted == by_name
3179*56bb7041Schristos       && !s->filenames_sorted)
3180*56bb7041Schristos     {
3181*56bb7041Schristos       lang_section_bst_type *tree;
3182*56bb7041Schristos 
3183*56bb7041Schristos       walk_wild (s, output_section_callback_fast, output);
3184*56bb7041Schristos 
3185*56bb7041Schristos       tree = s->tree;
3186*56bb7041Schristos       if (tree)
3187*56bb7041Schristos 	{
3188*56bb7041Schristos 	  output_section_callback_tree_to_list (s, tree, output);
3189*56bb7041Schristos 	  s->tree = NULL;
3190*56bb7041Schristos 	}
3191*56bb7041Schristos     }
3192*56bb7041Schristos   else
3193*56bb7041Schristos     walk_wild (s, output_section_callback, output);
3194*56bb7041Schristos 
3195*56bb7041Schristos   if (default_common_section == NULL)
3196*56bb7041Schristos     for (sec = s->section_list; sec != NULL; sec = sec->next)
3197*56bb7041Schristos       if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
3198*56bb7041Schristos 	{
3199*56bb7041Schristos 	  /* Remember the section that common is going to in case we
3200*56bb7041Schristos 	     later get something which doesn't know where to put it.  */
3201*56bb7041Schristos 	  default_common_section = output;
3202*56bb7041Schristos 	  break;
3203*56bb7041Schristos 	}
3204*56bb7041Schristos }
3205*56bb7041Schristos 
3206*56bb7041Schristos /* Return TRUE iff target is the sought target.  */
3207*56bb7041Schristos 
3208*56bb7041Schristos static int
get_target(const bfd_target * target,void * data)3209*56bb7041Schristos get_target (const bfd_target *target, void *data)
3210*56bb7041Schristos {
3211*56bb7041Schristos   const char *sought = (const char *) data;
3212*56bb7041Schristos 
3213*56bb7041Schristos   return strcmp (target->name, sought) == 0;
3214*56bb7041Schristos }
3215*56bb7041Schristos 
3216*56bb7041Schristos /* Like strcpy() but convert to lower case as well.  */
3217*56bb7041Schristos 
3218*56bb7041Schristos static void
stricpy(char * dest,const char * src)3219*56bb7041Schristos stricpy (char *dest, const char *src)
3220*56bb7041Schristos {
3221*56bb7041Schristos   char c;
3222*56bb7041Schristos 
3223*56bb7041Schristos   while ((c = *src++) != 0)
3224*56bb7041Schristos     *dest++ = TOLOWER (c);
3225*56bb7041Schristos 
3226*56bb7041Schristos   *dest = 0;
3227*56bb7041Schristos }
3228*56bb7041Schristos 
3229*56bb7041Schristos /* Remove the first occurrence of needle (if any) in haystack
3230*56bb7041Schristos    from haystack.  */
3231*56bb7041Schristos 
3232*56bb7041Schristos static void
strcut(char * haystack,const char * needle)3233*56bb7041Schristos strcut (char *haystack, const char *needle)
3234*56bb7041Schristos {
3235*56bb7041Schristos   haystack = strstr (haystack, needle);
3236*56bb7041Schristos 
3237*56bb7041Schristos   if (haystack)
3238*56bb7041Schristos     {
3239*56bb7041Schristos       char *src;
3240*56bb7041Schristos 
3241*56bb7041Schristos       for (src = haystack + strlen (needle); *src;)
3242*56bb7041Schristos 	*haystack++ = *src++;
3243*56bb7041Schristos 
3244*56bb7041Schristos       *haystack = 0;
3245*56bb7041Schristos     }
3246*56bb7041Schristos }
3247*56bb7041Schristos 
3248*56bb7041Schristos /* Compare two target format name strings.
3249*56bb7041Schristos    Return a value indicating how "similar" they are.  */
3250*56bb7041Schristos 
3251*56bb7041Schristos static int
name_compare(const char * first,const char * second)3252*56bb7041Schristos name_compare (const char *first, const char *second)
3253*56bb7041Schristos {
3254*56bb7041Schristos   char *copy1;
3255*56bb7041Schristos   char *copy2;
3256*56bb7041Schristos   int result;
3257*56bb7041Schristos 
3258*56bb7041Schristos   copy1 = (char *) xmalloc (strlen (first) + 1);
3259*56bb7041Schristos   copy2 = (char *) xmalloc (strlen (second) + 1);
3260*56bb7041Schristos 
3261*56bb7041Schristos   /* Convert the names to lower case.  */
3262*56bb7041Schristos   stricpy (copy1, first);
3263*56bb7041Schristos   stricpy (copy2, second);
3264*56bb7041Schristos 
3265*56bb7041Schristos   /* Remove size and endian strings from the name.  */
3266*56bb7041Schristos   strcut (copy1, "big");
3267*56bb7041Schristos   strcut (copy1, "little");
3268*56bb7041Schristos   strcut (copy2, "big");
3269*56bb7041Schristos   strcut (copy2, "little");
3270*56bb7041Schristos 
3271*56bb7041Schristos   /* Return a value based on how many characters match,
3272*56bb7041Schristos      starting from the beginning.   If both strings are
3273*56bb7041Schristos      the same then return 10 * their length.  */
3274*56bb7041Schristos   for (result = 0; copy1[result] == copy2[result]; result++)
3275*56bb7041Schristos     if (copy1[result] == 0)
3276*56bb7041Schristos       {
3277*56bb7041Schristos 	result *= 10;
3278*56bb7041Schristos 	break;
3279*56bb7041Schristos       }
3280*56bb7041Schristos 
3281*56bb7041Schristos   free (copy1);
3282*56bb7041Schristos   free (copy2);
3283*56bb7041Schristos 
3284*56bb7041Schristos   return result;
3285*56bb7041Schristos }
3286*56bb7041Schristos 
3287*56bb7041Schristos /* Set by closest_target_match() below.  */
3288*56bb7041Schristos static const bfd_target *winner;
3289*56bb7041Schristos 
3290*56bb7041Schristos /* Scan all the valid bfd targets looking for one that has the endianness
3291*56bb7041Schristos    requirement that was specified on the command line, and is the nearest
3292*56bb7041Schristos    match to the original output target.  */
3293*56bb7041Schristos 
3294*56bb7041Schristos static int
closest_target_match(const bfd_target * target,void * data)3295*56bb7041Schristos closest_target_match (const bfd_target *target, void *data)
3296*56bb7041Schristos {
3297*56bb7041Schristos   const bfd_target *original = (const bfd_target *) data;
3298*56bb7041Schristos 
3299*56bb7041Schristos   if (command_line.endian == ENDIAN_BIG
3300*56bb7041Schristos       && target->byteorder != BFD_ENDIAN_BIG)
3301*56bb7041Schristos     return 0;
3302*56bb7041Schristos 
3303*56bb7041Schristos   if (command_line.endian == ENDIAN_LITTLE
3304*56bb7041Schristos       && target->byteorder != BFD_ENDIAN_LITTLE)
3305*56bb7041Schristos     return 0;
3306*56bb7041Schristos 
3307*56bb7041Schristos   /* Must be the same flavour.  */
3308*56bb7041Schristos   if (target->flavour != original->flavour)
3309*56bb7041Schristos     return 0;
3310*56bb7041Schristos 
3311*56bb7041Schristos   /* Ignore generic big and little endian elf vectors.  */
3312*56bb7041Schristos   if (strcmp (target->name, "elf32-big") == 0
3313*56bb7041Schristos       || strcmp (target->name, "elf64-big") == 0
3314*56bb7041Schristos       || strcmp (target->name, "elf32-little") == 0
3315*56bb7041Schristos       || strcmp (target->name, "elf64-little") == 0)
3316*56bb7041Schristos     return 0;
3317*56bb7041Schristos 
3318*56bb7041Schristos   /* If we have not found a potential winner yet, then record this one.  */
3319*56bb7041Schristos   if (winner == NULL)
3320*56bb7041Schristos     {
3321*56bb7041Schristos       winner = target;
3322*56bb7041Schristos       return 0;
3323*56bb7041Schristos     }
3324*56bb7041Schristos 
3325*56bb7041Schristos   /* Oh dear, we now have two potential candidates for a successful match.
3326*56bb7041Schristos      Compare their names and choose the better one.  */
3327*56bb7041Schristos   if (name_compare (target->name, original->name)
3328*56bb7041Schristos       > name_compare (winner->name, original->name))
3329*56bb7041Schristos     winner = target;
3330*56bb7041Schristos 
3331*56bb7041Schristos   /* Keep on searching until wqe have checked them all.  */
3332*56bb7041Schristos   return 0;
3333*56bb7041Schristos }
3334*56bb7041Schristos 
3335*56bb7041Schristos /* Return the BFD target format of the first input file.  */
3336*56bb7041Schristos 
3337*56bb7041Schristos static const char *
get_first_input_target(void)3338*56bb7041Schristos get_first_input_target (void)
3339*56bb7041Schristos {
3340*56bb7041Schristos   const char *target = NULL;
3341*56bb7041Schristos 
3342*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (s)
3343*56bb7041Schristos     {
3344*56bb7041Schristos       if (s->header.type == lang_input_statement_enum
3345*56bb7041Schristos 	  && s->flags.real)
3346*56bb7041Schristos 	{
3347*56bb7041Schristos 	  ldfile_open_file (s);
3348*56bb7041Schristos 
3349*56bb7041Schristos 	  if (s->the_bfd != NULL
3350*56bb7041Schristos 	      && bfd_check_format (s->the_bfd, bfd_object))
3351*56bb7041Schristos 	    {
3352*56bb7041Schristos 	      target = bfd_get_target (s->the_bfd);
3353*56bb7041Schristos 
3354*56bb7041Schristos 	      if (target != NULL)
3355*56bb7041Schristos 		break;
3356*56bb7041Schristos 	    }
3357*56bb7041Schristos 	}
3358*56bb7041Schristos     }
3359*56bb7041Schristos 
3360*56bb7041Schristos   return target;
3361*56bb7041Schristos }
3362*56bb7041Schristos 
3363*56bb7041Schristos const char *
lang_get_output_target(void)3364*56bb7041Schristos lang_get_output_target (void)
3365*56bb7041Schristos {
3366*56bb7041Schristos   const char *target;
3367*56bb7041Schristos 
3368*56bb7041Schristos   /* Has the user told us which output format to use?  */
3369*56bb7041Schristos   if (output_target != NULL)
3370*56bb7041Schristos     return output_target;
3371*56bb7041Schristos 
3372*56bb7041Schristos   /* No - has the current target been set to something other than
3373*56bb7041Schristos      the default?  */
3374*56bb7041Schristos   if (current_target != default_target && current_target != NULL)
3375*56bb7041Schristos     return current_target;
3376*56bb7041Schristos 
3377*56bb7041Schristos   /* No - can we determine the format of the first input file?  */
3378*56bb7041Schristos   target = get_first_input_target ();
3379*56bb7041Schristos   if (target != NULL)
3380*56bb7041Schristos     return target;
3381*56bb7041Schristos 
3382*56bb7041Schristos   /* Failed - use the default output target.  */
3383*56bb7041Schristos   return default_target;
3384*56bb7041Schristos }
3385*56bb7041Schristos 
3386*56bb7041Schristos /* Open the output file.  */
3387*56bb7041Schristos 
3388*56bb7041Schristos static void
open_output(const char * name)3389*56bb7041Schristos open_output (const char *name)
3390*56bb7041Schristos {
3391*56bb7041Schristos   output_target = lang_get_output_target ();
3392*56bb7041Schristos 
3393*56bb7041Schristos   /* Has the user requested a particular endianness on the command
3394*56bb7041Schristos      line?  */
3395*56bb7041Schristos   if (command_line.endian != ENDIAN_UNSET)
3396*56bb7041Schristos     {
3397*56bb7041Schristos       /* Get the chosen target.  */
3398*56bb7041Schristos       const bfd_target *target
3399*56bb7041Schristos 	= bfd_iterate_over_targets (get_target, (void *) output_target);
3400*56bb7041Schristos 
3401*56bb7041Schristos       /* If the target is not supported, we cannot do anything.  */
3402*56bb7041Schristos       if (target != NULL)
3403*56bb7041Schristos 	{
3404*56bb7041Schristos 	  enum bfd_endian desired_endian;
3405*56bb7041Schristos 
3406*56bb7041Schristos 	  if (command_line.endian == ENDIAN_BIG)
3407*56bb7041Schristos 	    desired_endian = BFD_ENDIAN_BIG;
3408*56bb7041Schristos 	  else
3409*56bb7041Schristos 	    desired_endian = BFD_ENDIAN_LITTLE;
3410*56bb7041Schristos 
3411*56bb7041Schristos 	  /* See if the target has the wrong endianness.  This should
3412*56bb7041Schristos 	     not happen if the linker script has provided big and
3413*56bb7041Schristos 	     little endian alternatives, but some scrips don't do
3414*56bb7041Schristos 	     this.  */
3415*56bb7041Schristos 	  if (target->byteorder != desired_endian)
3416*56bb7041Schristos 	    {
3417*56bb7041Schristos 	      /* If it does, then see if the target provides
3418*56bb7041Schristos 		 an alternative with the correct endianness.  */
3419*56bb7041Schristos 	      if (target->alternative_target != NULL
3420*56bb7041Schristos 		  && (target->alternative_target->byteorder == desired_endian))
3421*56bb7041Schristos 		output_target = target->alternative_target->name;
3422*56bb7041Schristos 	      else
3423*56bb7041Schristos 		{
3424*56bb7041Schristos 		  /* Try to find a target as similar as possible to
3425*56bb7041Schristos 		     the default target, but which has the desired
3426*56bb7041Schristos 		     endian characteristic.  */
3427*56bb7041Schristos 		  bfd_iterate_over_targets (closest_target_match,
3428*56bb7041Schristos 					    (void *) target);
3429*56bb7041Schristos 
3430*56bb7041Schristos 		  /* Oh dear - we could not find any targets that
3431*56bb7041Schristos 		     satisfy our requirements.  */
3432*56bb7041Schristos 		  if (winner == NULL)
3433*56bb7041Schristos 		    einfo (_("%P: warning: could not find any targets"
3434*56bb7041Schristos 			     " that match endianness requirement\n"));
3435*56bb7041Schristos 		  else
3436*56bb7041Schristos 		    output_target = winner->name;
3437*56bb7041Schristos 		}
3438*56bb7041Schristos 	    }
3439*56bb7041Schristos 	}
3440*56bb7041Schristos     }
3441*56bb7041Schristos 
3442*56bb7041Schristos   link_info.output_bfd = bfd_openw (name, output_target);
3443*56bb7041Schristos 
3444*56bb7041Schristos   if (link_info.output_bfd == NULL)
3445*56bb7041Schristos     {
3446*56bb7041Schristos       if (bfd_get_error () == bfd_error_invalid_target)
3447*56bb7041Schristos 	einfo (_("%F%P: target %s not found\n"), output_target);
3448*56bb7041Schristos 
3449*56bb7041Schristos       einfo (_("%F%P: cannot open output file %s: %E\n"), name);
3450*56bb7041Schristos     }
3451*56bb7041Schristos 
3452*56bb7041Schristos   delete_output_file_on_failure = TRUE;
3453*56bb7041Schristos 
3454*56bb7041Schristos   if (!bfd_set_format (link_info.output_bfd, bfd_object))
3455*56bb7041Schristos     einfo (_("%F%P: %s: can not make object file: %E\n"), name);
3456*56bb7041Schristos   if (!bfd_set_arch_mach (link_info.output_bfd,
3457*56bb7041Schristos 			   ldfile_output_architecture,
3458*56bb7041Schristos 			   ldfile_output_machine))
3459*56bb7041Schristos     einfo (_("%F%P: %s: can not set architecture: %E\n"), name);
3460*56bb7041Schristos 
3461*56bb7041Schristos   link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
3462*56bb7041Schristos   if (link_info.hash == NULL)
3463*56bb7041Schristos     einfo (_("%F%P: can not create hash table: %E\n"));
3464*56bb7041Schristos 
3465*56bb7041Schristos   bfd_set_gp_size (link_info.output_bfd, g_switch_value);
3466*56bb7041Schristos }
3467*56bb7041Schristos 
3468*56bb7041Schristos static void
ldlang_open_output(lang_statement_union_type * statement)3469*56bb7041Schristos ldlang_open_output (lang_statement_union_type *statement)
3470*56bb7041Schristos {
3471*56bb7041Schristos   switch (statement->header.type)
3472*56bb7041Schristos     {
3473*56bb7041Schristos     case lang_output_statement_enum:
3474*56bb7041Schristos       ASSERT (link_info.output_bfd == NULL);
3475*56bb7041Schristos       open_output (statement->output_statement.name);
3476*56bb7041Schristos       ldemul_set_output_arch ();
3477*56bb7041Schristos       if (config.magic_demand_paged
3478*56bb7041Schristos 	  && !bfd_link_relocatable (&link_info))
3479*56bb7041Schristos 	link_info.output_bfd->flags |= D_PAGED;
3480*56bb7041Schristos       else
3481*56bb7041Schristos 	link_info.output_bfd->flags &= ~D_PAGED;
3482*56bb7041Schristos       if (config.text_read_only)
3483*56bb7041Schristos 	link_info.output_bfd->flags |= WP_TEXT;
3484*56bb7041Schristos       else
3485*56bb7041Schristos 	link_info.output_bfd->flags &= ~WP_TEXT;
3486*56bb7041Schristos       if (link_info.traditional_format)
3487*56bb7041Schristos 	link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
3488*56bb7041Schristos       else
3489*56bb7041Schristos 	link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
3490*56bb7041Schristos       break;
3491*56bb7041Schristos 
3492*56bb7041Schristos     case lang_target_statement_enum:
3493*56bb7041Schristos       current_target = statement->target_statement.target;
3494*56bb7041Schristos       break;
3495*56bb7041Schristos     default:
3496*56bb7041Schristos       break;
3497*56bb7041Schristos     }
3498*56bb7041Schristos }
3499*56bb7041Schristos 
3500*56bb7041Schristos static void
init_opb(asection * s)3501*56bb7041Schristos init_opb (asection *s)
3502*56bb7041Schristos {
3503*56bb7041Schristos   unsigned int x;
3504*56bb7041Schristos 
3505*56bb7041Schristos   opb_shift = 0;
3506*56bb7041Schristos   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
3507*56bb7041Schristos       && s != NULL
3508*56bb7041Schristos       && (s->flags & SEC_ELF_OCTETS) != 0)
3509*56bb7041Schristos     return;
3510*56bb7041Schristos 
3511*56bb7041Schristos   x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3512*56bb7041Schristos 				     ldfile_output_machine);
3513*56bb7041Schristos   if (x > 1)
3514*56bb7041Schristos     while ((x & 1) == 0)
3515*56bb7041Schristos       {
3516*56bb7041Schristos 	x >>= 1;
3517*56bb7041Schristos 	++opb_shift;
3518*56bb7041Schristos       }
3519*56bb7041Schristos   ASSERT (x == 1);
3520*56bb7041Schristos }
3521*56bb7041Schristos 
3522*56bb7041Schristos /* Open all the input files.  */
3523*56bb7041Schristos 
3524*56bb7041Schristos enum open_bfd_mode
3525*56bb7041Schristos   {
3526*56bb7041Schristos     OPEN_BFD_NORMAL = 0,
3527*56bb7041Schristos     OPEN_BFD_FORCE = 1,
3528*56bb7041Schristos     OPEN_BFD_RESCAN = 2
3529*56bb7041Schristos   };
3530*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3531*56bb7041Schristos static lang_input_statement_type *plugin_insert = NULL;
3532*56bb7041Schristos static struct bfd_link_hash_entry *plugin_undefs = NULL;
3533*56bb7041Schristos #endif
3534*56bb7041Schristos 
3535*56bb7041Schristos static void
open_input_bfds(lang_statement_union_type * s,enum open_bfd_mode mode)3536*56bb7041Schristos open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
3537*56bb7041Schristos {
3538*56bb7041Schristos   for (; s != NULL; s = s->header.next)
3539*56bb7041Schristos     {
3540*56bb7041Schristos       switch (s->header.type)
3541*56bb7041Schristos 	{
3542*56bb7041Schristos 	case lang_constructors_statement_enum:
3543*56bb7041Schristos 	  open_input_bfds (constructor_list.head, mode);
3544*56bb7041Schristos 	  break;
3545*56bb7041Schristos 	case lang_output_section_statement_enum:
3546*56bb7041Schristos 	  open_input_bfds (s->output_section_statement.children.head, mode);
3547*56bb7041Schristos 	  break;
3548*56bb7041Schristos 	case lang_wild_statement_enum:
3549*56bb7041Schristos 	  /* Maybe we should load the file's symbols.  */
3550*56bb7041Schristos 	  if ((mode & OPEN_BFD_RESCAN) == 0
3551*56bb7041Schristos 	      && s->wild_statement.filename
3552*56bb7041Schristos 	      && !wildcardp (s->wild_statement.filename)
3553*56bb7041Schristos 	      && !archive_path (s->wild_statement.filename))
3554*56bb7041Schristos 	    lookup_name (s->wild_statement.filename);
3555*56bb7041Schristos 	  open_input_bfds (s->wild_statement.children.head, mode);
3556*56bb7041Schristos 	  break;
3557*56bb7041Schristos 	case lang_group_statement_enum:
3558*56bb7041Schristos 	  {
3559*56bb7041Schristos 	    struct bfd_link_hash_entry *undefs;
3560*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3561*56bb7041Schristos 	    lang_input_statement_type *plugin_insert_save;
3562*56bb7041Schristos #endif
3563*56bb7041Schristos 
3564*56bb7041Schristos 	    /* We must continually search the entries in the group
3565*56bb7041Schristos 	       until no new symbols are added to the list of undefined
3566*56bb7041Schristos 	       symbols.  */
3567*56bb7041Schristos 
3568*56bb7041Schristos 	    do
3569*56bb7041Schristos 	      {
3570*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3571*56bb7041Schristos 		plugin_insert_save = plugin_insert;
3572*56bb7041Schristos #endif
3573*56bb7041Schristos 		undefs = link_info.hash->undefs_tail;
3574*56bb7041Schristos 		open_input_bfds (s->group_statement.children.head,
3575*56bb7041Schristos 				 mode | OPEN_BFD_FORCE);
3576*56bb7041Schristos 	      }
3577*56bb7041Schristos 	    while (undefs != link_info.hash->undefs_tail
3578*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3579*56bb7041Schristos 		   /* Objects inserted by a plugin, which are loaded
3580*56bb7041Schristos 		      before we hit this loop, may have added new
3581*56bb7041Schristos 		      undefs.  */
3582*56bb7041Schristos 		   || (plugin_insert != plugin_insert_save && plugin_undefs)
3583*56bb7041Schristos #endif
3584*56bb7041Schristos 		   );
3585*56bb7041Schristos 	  }
3586*56bb7041Schristos 	  break;
3587*56bb7041Schristos 	case lang_target_statement_enum:
3588*56bb7041Schristos 	  current_target = s->target_statement.target;
3589*56bb7041Schristos 	  break;
3590*56bb7041Schristos 	case lang_input_statement_enum:
3591*56bb7041Schristos 	  if (s->input_statement.flags.real)
3592*56bb7041Schristos 	    {
3593*56bb7041Schristos 	      lang_statement_union_type **os_tail;
3594*56bb7041Schristos 	      lang_statement_list_type add;
3595*56bb7041Schristos 	      bfd *abfd;
3596*56bb7041Schristos 
3597*56bb7041Schristos 	      s->input_statement.target = current_target;
3598*56bb7041Schristos 
3599*56bb7041Schristos 	      /* If we are being called from within a group, and this
3600*56bb7041Schristos 		 is an archive which has already been searched, then
3601*56bb7041Schristos 		 force it to be researched unless the whole archive
3602*56bb7041Schristos 		 has been loaded already.  Do the same for a rescan.
3603*56bb7041Schristos 		 Likewise reload --as-needed shared libs.  */
3604*56bb7041Schristos 	      if (mode != OPEN_BFD_NORMAL
3605*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3606*56bb7041Schristos 		  && ((mode & OPEN_BFD_RESCAN) == 0
3607*56bb7041Schristos 		      || plugin_insert == NULL)
3608*56bb7041Schristos #endif
3609*56bb7041Schristos 		  && s->input_statement.flags.loaded
3610*56bb7041Schristos 		  && (abfd = s->input_statement.the_bfd) != NULL
3611*56bb7041Schristos 		  && ((bfd_get_format (abfd) == bfd_archive
3612*56bb7041Schristos 		       && !s->input_statement.flags.whole_archive)
3613*56bb7041Schristos 		      || (bfd_get_format (abfd) == bfd_object
3614*56bb7041Schristos 			  && ((abfd->flags) & DYNAMIC) != 0
3615*56bb7041Schristos 			  && s->input_statement.flags.add_DT_NEEDED_for_regular
3616*56bb7041Schristos 			  && bfd_get_flavour (abfd) == bfd_target_elf_flavour
3617*56bb7041Schristos 			  && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)))
3618*56bb7041Schristos 		{
3619*56bb7041Schristos 		  s->input_statement.flags.loaded = FALSE;
3620*56bb7041Schristos 		  s->input_statement.flags.reload = TRUE;
3621*56bb7041Schristos 		}
3622*56bb7041Schristos 
3623*56bb7041Schristos 	      os_tail = lang_os_list.tail;
3624*56bb7041Schristos 	      lang_list_init (&add);
3625*56bb7041Schristos 
3626*56bb7041Schristos 	      if (!load_symbols (&s->input_statement, &add))
3627*56bb7041Schristos 		config.make_executable = FALSE;
3628*56bb7041Schristos 
3629*56bb7041Schristos 	      if (add.head != NULL)
3630*56bb7041Schristos 		{
3631*56bb7041Schristos 		  /* If this was a script with output sections then
3632*56bb7041Schristos 		     tack any added statements on to the end of the
3633*56bb7041Schristos 		     list.  This avoids having to reorder the output
3634*56bb7041Schristos 		     section statement list.  Very likely the user
3635*56bb7041Schristos 		     forgot -T, and whatever we do here will not meet
3636*56bb7041Schristos 		     naive user expectations.  */
3637*56bb7041Schristos 		  if (os_tail != lang_os_list.tail)
3638*56bb7041Schristos 		    {
3639*56bb7041Schristos 		      einfo (_("%P: warning: %s contains output sections;"
3640*56bb7041Schristos 			       " did you forget -T?\n"),
3641*56bb7041Schristos 			     s->input_statement.filename);
3642*56bb7041Schristos 		      *stat_ptr->tail = add.head;
3643*56bb7041Schristos 		      stat_ptr->tail = add.tail;
3644*56bb7041Schristos 		    }
3645*56bb7041Schristos 		  else
3646*56bb7041Schristos 		    {
3647*56bb7041Schristos 		      *add.tail = s->header.next;
3648*56bb7041Schristos 		      s->header.next = add.head;
3649*56bb7041Schristos 		    }
3650*56bb7041Schristos 		}
3651*56bb7041Schristos 	    }
3652*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
3653*56bb7041Schristos 	  /* If we have found the point at which a plugin added new
3654*56bb7041Schristos 	     files, clear plugin_insert to enable archive rescan.  */
3655*56bb7041Schristos 	  if (&s->input_statement == plugin_insert)
3656*56bb7041Schristos 	    plugin_insert = NULL;
3657*56bb7041Schristos #endif
3658*56bb7041Schristos 	  break;
3659*56bb7041Schristos 	case lang_assignment_statement_enum:
3660*56bb7041Schristos 	  if (s->assignment_statement.exp->type.node_class != etree_assert)
3661*56bb7041Schristos 	    exp_fold_tree_no_dot (s->assignment_statement.exp);
3662*56bb7041Schristos 	  break;
3663*56bb7041Schristos 	default:
3664*56bb7041Schristos 	  break;
3665*56bb7041Schristos 	}
3666*56bb7041Schristos     }
3667*56bb7041Schristos 
3668*56bb7041Schristos   /* Exit if any of the files were missing.  */
3669*56bb7041Schristos   if (input_flags.missing_file)
3670*56bb7041Schristos     einfo ("%F");
3671*56bb7041Schristos }
3672*56bb7041Schristos 
3673*56bb7041Schristos #ifdef ENABLE_LIBCTF
3674*56bb7041Schristos /* Emit CTF errors and warnings.  fp can be NULL to report errors/warnings
3675*56bb7041Schristos    that happened specifically at CTF open time.  */
3676*56bb7041Schristos static void
lang_ctf_errs_warnings(ctf_file_t * fp)3677*56bb7041Schristos lang_ctf_errs_warnings (ctf_file_t *fp)
3678*56bb7041Schristos {
3679*56bb7041Schristos   ctf_next_t *i = NULL;
3680*56bb7041Schristos   char *text;
3681*56bb7041Schristos   int is_warning;
3682*56bb7041Schristos   int err;
3683*56bb7041Schristos 
3684*56bb7041Schristos   while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL)
3685*56bb7041Schristos     {
3686*56bb7041Schristos       einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"),
3687*56bb7041Schristos 	     text);
3688*56bb7041Schristos       free (text);
3689*56bb7041Schristos     }
3690*56bb7041Schristos   if (err != ECTF_NEXT_END)
3691*56bb7041Schristos     {
3692*56bb7041Schristos       einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3693*56bb7041Schristos 	     ctf_errmsg (err));
3694*56bb7041Schristos     }
3695*56bb7041Schristos 
3696*56bb7041Schristos   /* `err' returns errors from the error/warning iterator in particular.
3697*56bb7041Schristos      These never assert.  But if we have an fp, that could have recorded
3698*56bb7041Schristos      an assertion failure: assert if it has done so.  */
3699*56bb7041Schristos   ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL);
3700*56bb7041Schristos }
3701*56bb7041Schristos 
3702*56bb7041Schristos /* Open the CTF sections in the input files with libctf: if any were opened,
3703*56bb7041Schristos    create a fake input file that we'll write the merged CTF data to later
3704*56bb7041Schristos    on.  */
3705*56bb7041Schristos 
3706*56bb7041Schristos static void
ldlang_open_ctf(void)3707*56bb7041Schristos ldlang_open_ctf (void)
3708*56bb7041Schristos {
3709*56bb7041Schristos   int any_ctf = 0;
3710*56bb7041Schristos   int err;
3711*56bb7041Schristos 
3712*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
3713*56bb7041Schristos     {
3714*56bb7041Schristos       asection *sect;
3715*56bb7041Schristos 
3716*56bb7041Schristos       /* Incoming files from the compiler have a single ctf_file_t in them
3717*56bb7041Schristos 	 (which is presented to us by the libctf API in a ctf_archive_t
3718*56bb7041Schristos 	 wrapper): files derived from a previous relocatable link have a CTF
3719*56bb7041Schristos 	 archive containing possibly many CTF files.  */
3720*56bb7041Schristos 
3721*56bb7041Schristos       if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL)
3722*56bb7041Schristos 	{
3723*56bb7041Schristos 	  if (err != ECTF_NOCTFDATA)
3724*56bb7041Schristos 	    {
3725*56bb7041Schristos 	      lang_ctf_errs_warnings (NULL);
3726*56bb7041Schristos 	      einfo (_("%P: warning: CTF section in %pB not loaded; "
3727*56bb7041Schristos 		       "its types will be discarded: %s\n"), file->the_bfd,
3728*56bb7041Schristos 		     ctf_errmsg (err));
3729*56bb7041Schristos 	    }
3730*56bb7041Schristos 	  continue;
3731*56bb7041Schristos 	}
3732*56bb7041Schristos 
3733*56bb7041Schristos       /* Prevent the contents of this section from being written, while
3734*56bb7041Schristos 	 requiring the section itself to be duplicated in the output, but only
3735*56bb7041Schristos 	 once.  */
3736*56bb7041Schristos       /* This section must exist if ctf_bfdopen() succeeded.  */
3737*56bb7041Schristos       sect = bfd_get_section_by_name (file->the_bfd, ".ctf");
3738*56bb7041Schristos       sect->size = 0;
3739*56bb7041Schristos       sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED;
3740*56bb7041Schristos 
3741*56bb7041Schristos       if (any_ctf)
3742*56bb7041Schristos 	sect->flags |= SEC_EXCLUDE;
3743*56bb7041Schristos       any_ctf = 1;
3744*56bb7041Schristos     }
3745*56bb7041Schristos 
3746*56bb7041Schristos   if (!any_ctf)
3747*56bb7041Schristos     {
3748*56bb7041Schristos       ctf_output = NULL;
3749*56bb7041Schristos       return;
3750*56bb7041Schristos     }
3751*56bb7041Schristos 
3752*56bb7041Schristos   if ((ctf_output = ctf_create (&err)) != NULL)
3753*56bb7041Schristos     return;
3754*56bb7041Schristos 
3755*56bb7041Schristos   einfo (_("%P: warning: CTF output not created: `%s'\n"),
3756*56bb7041Schristos 	 ctf_errmsg (err));
3757*56bb7041Schristos 
3758*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (errfile)
3759*56bb7041Schristos     ctf_close (errfile->the_ctf);
3760*56bb7041Schristos }
3761*56bb7041Schristos 
3762*56bb7041Schristos /* Merge together CTF sections.  After this, only the symtab-dependent
3763*56bb7041Schristos    function and data object sections need adjustment.  */
3764*56bb7041Schristos 
3765*56bb7041Schristos static void
lang_merge_ctf(void)3766*56bb7041Schristos lang_merge_ctf (void)
3767*56bb7041Schristos {
3768*56bb7041Schristos   asection *output_sect;
3769*56bb7041Schristos   int flags = 0;
3770*56bb7041Schristos 
3771*56bb7041Schristos   if (!ctf_output)
3772*56bb7041Schristos     return;
3773*56bb7041Schristos 
3774*56bb7041Schristos   output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
3775*56bb7041Schristos 
3776*56bb7041Schristos   /* If the section was discarded, don't waste time merging.  */
3777*56bb7041Schristos   if (output_sect == NULL)
3778*56bb7041Schristos     {
3779*56bb7041Schristos       ctf_file_close (ctf_output);
3780*56bb7041Schristos       ctf_output = NULL;
3781*56bb7041Schristos 
3782*56bb7041Schristos       LANG_FOR_EACH_INPUT_STATEMENT (file)
3783*56bb7041Schristos 	{
3784*56bb7041Schristos 	  ctf_close (file->the_ctf);
3785*56bb7041Schristos 	  file->the_ctf = NULL;
3786*56bb7041Schristos 	}
3787*56bb7041Schristos       return;
3788*56bb7041Schristos     }
3789*56bb7041Schristos 
3790*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
3791*56bb7041Schristos     {
3792*56bb7041Schristos       if (!file->the_ctf)
3793*56bb7041Schristos 	continue;
3794*56bb7041Schristos 
3795*56bb7041Schristos       /* Takes ownership of file->the_ctf.  */
3796*56bb7041Schristos       if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0)
3797*56bb7041Schristos 	{
3798*56bb7041Schristos 	  einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3799*56bb7041Schristos 		 file->the_bfd, ctf_errmsg (ctf_errno (ctf_output)));
3800*56bb7041Schristos 	  ctf_close (file->the_ctf);
3801*56bb7041Schristos 	  file->the_ctf = NULL;
3802*56bb7041Schristos 	  continue;
3803*56bb7041Schristos 	}
3804*56bb7041Schristos     }
3805*56bb7041Schristos 
3806*56bb7041Schristos   if (!config.ctf_share_duplicated)
3807*56bb7041Schristos     flags = CTF_LINK_SHARE_UNCONFLICTED;
3808*56bb7041Schristos   else
3809*56bb7041Schristos     flags = CTF_LINK_SHARE_DUPLICATED;
3810*56bb7041Schristos   if (!config.ctf_variables)
3811*56bb7041Schristos     flags |= CTF_LINK_OMIT_VARIABLES_SECTION;
3812*56bb7041Schristos 
3813*56bb7041Schristos   if (ctf_link (ctf_output, flags) < 0)
3814*56bb7041Schristos     {
3815*56bb7041Schristos       lang_ctf_errs_warnings (ctf_output);
3816*56bb7041Schristos       einfo (_("%P: warning: CTF linking failed; "
3817*56bb7041Schristos 	       "output will have no CTF section: %s\n"),
3818*56bb7041Schristos 	     ctf_errmsg (ctf_errno (ctf_output)));
3819*56bb7041Schristos       if (output_sect)
3820*56bb7041Schristos 	{
3821*56bb7041Schristos 	  output_sect->size = 0;
3822*56bb7041Schristos 	  output_sect->flags |= SEC_EXCLUDE;
3823*56bb7041Schristos 	}
3824*56bb7041Schristos     }
3825*56bb7041Schristos   /* Output any lingering errors that didn't come from ctf_link.  */
3826*56bb7041Schristos   lang_ctf_errs_warnings (ctf_output);
3827*56bb7041Schristos }
3828*56bb7041Schristos 
3829*56bb7041Schristos /* Let the emulation examine the symbol table and strtab to help it optimize the
3830*56bb7041Schristos    CTF, if supported.  */
3831*56bb7041Schristos 
3832*56bb7041Schristos void
ldlang_ctf_apply_strsym(struct elf_sym_strtab * syms,bfd_size_type symcount,struct elf_strtab_hash * symstrtab)3833*56bb7041Schristos ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms, bfd_size_type symcount,
3834*56bb7041Schristos 			 struct elf_strtab_hash *symstrtab)
3835*56bb7041Schristos {
3836*56bb7041Schristos   ldemul_examine_strtab_for_ctf (ctf_output, syms, symcount, symstrtab);
3837*56bb7041Schristos }
3838*56bb7041Schristos 
3839*56bb7041Schristos /* Write out the CTF section.  Called early, if the emulation isn't going to
3840*56bb7041Schristos    need to dedup against the strtab and symtab, then possibly called from the
3841*56bb7041Schristos    target linker code if the dedup has happened.  */
3842*56bb7041Schristos static void
lang_write_ctf(int late)3843*56bb7041Schristos lang_write_ctf (int late)
3844*56bb7041Schristos {
3845*56bb7041Schristos   size_t output_size;
3846*56bb7041Schristos   asection *output_sect;
3847*56bb7041Schristos 
3848*56bb7041Schristos   if (!ctf_output)
3849*56bb7041Schristos     return;
3850*56bb7041Schristos 
3851*56bb7041Schristos   if (late)
3852*56bb7041Schristos     {
3853*56bb7041Schristos       /* Emit CTF late if this emulation says it can do so.  */
3854*56bb7041Schristos       if (ldemul_emit_ctf_early ())
3855*56bb7041Schristos 	return;
3856*56bb7041Schristos     }
3857*56bb7041Schristos   else
3858*56bb7041Schristos     {
3859*56bb7041Schristos       if (!ldemul_emit_ctf_early ())
3860*56bb7041Schristos 	return;
3861*56bb7041Schristos     }
3862*56bb7041Schristos 
3863*56bb7041Schristos   /* Emit CTF.  */
3864*56bb7041Schristos 
3865*56bb7041Schristos   output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
3866*56bb7041Schristos   if (output_sect)
3867*56bb7041Schristos     {
3868*56bb7041Schristos       output_sect->contents = ctf_link_write (ctf_output, &output_size,
3869*56bb7041Schristos 					      CTF_COMPRESSION_THRESHOLD);
3870*56bb7041Schristos       output_sect->size = output_size;
3871*56bb7041Schristos       output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP;
3872*56bb7041Schristos 
3873*56bb7041Schristos       lang_ctf_errs_warnings (ctf_output);
3874*56bb7041Schristos       if (!output_sect->contents)
3875*56bb7041Schristos 	{
3876*56bb7041Schristos 	  einfo (_("%P: warning: CTF section emission failed; "
3877*56bb7041Schristos 		   "output will have no CTF section: %s\n"),
3878*56bb7041Schristos 		 ctf_errmsg (ctf_errno (ctf_output)));
3879*56bb7041Schristos 	  output_sect->size = 0;
3880*56bb7041Schristos 	  output_sect->flags |= SEC_EXCLUDE;
3881*56bb7041Schristos 	}
3882*56bb7041Schristos     }
3883*56bb7041Schristos 
3884*56bb7041Schristos   /* This also closes every CTF input file used in the link.  */
3885*56bb7041Schristos   ctf_file_close (ctf_output);
3886*56bb7041Schristos   ctf_output = NULL;
3887*56bb7041Schristos 
3888*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
3889*56bb7041Schristos     file->the_ctf = NULL;
3890*56bb7041Schristos }
3891*56bb7041Schristos 
3892*56bb7041Schristos /* Write out the CTF section late, if the emulation needs that.  */
3893*56bb7041Schristos 
3894*56bb7041Schristos void
ldlang_write_ctf_late(void)3895*56bb7041Schristos ldlang_write_ctf_late (void)
3896*56bb7041Schristos {
3897*56bb7041Schristos   /* Trigger a "late call", if the emulation needs one.  */
3898*56bb7041Schristos 
3899*56bb7041Schristos   lang_write_ctf (1);
3900*56bb7041Schristos }
3901*56bb7041Schristos #else
3902*56bb7041Schristos static void
ldlang_open_ctf(void)3903*56bb7041Schristos ldlang_open_ctf (void)
3904*56bb7041Schristos {
3905*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
3906*56bb7041Schristos     {
3907*56bb7041Schristos       asection *sect;
3908*56bb7041Schristos 
3909*56bb7041Schristos       /* If built without CTF, warn and delete all CTF sections from the output.
3910*56bb7041Schristos 	 (The alternative would be to simply concatenate them, which does not
3911*56bb7041Schristos 	 yield a valid CTF section.)  */
3912*56bb7041Schristos 
3913*56bb7041Schristos       if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL)
3914*56bb7041Schristos 	{
3915*56bb7041Schristos 	    einfo (_("%P: warning: CTF section in %pB not linkable: "
3916*56bb7041Schristos 		     "%P was built without support for CTF\n"), file->the_bfd);
3917*56bb7041Schristos 	    sect->size = 0;
3918*56bb7041Schristos 	    sect->flags |= SEC_EXCLUDE;
3919*56bb7041Schristos 	}
3920*56bb7041Schristos     }
3921*56bb7041Schristos }
3922*56bb7041Schristos 
lang_merge_ctf(void)3923*56bb7041Schristos static void lang_merge_ctf (void) {}
3924*56bb7041Schristos void
ldlang_ctf_apply_strsym(struct elf_sym_strtab * syms ATTRIBUTE_UNUSED,bfd_size_type symcount ATTRIBUTE_UNUSED,struct elf_strtab_hash * symstrtab ATTRIBUTE_UNUSED)3925*56bb7041Schristos ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms ATTRIBUTE_UNUSED,
3926*56bb7041Schristos 			 bfd_size_type symcount ATTRIBUTE_UNUSED,
3927*56bb7041Schristos 			 struct elf_strtab_hash *symstrtab ATTRIBUTE_UNUSED)
3928*56bb7041Schristos {
3929*56bb7041Schristos }
lang_write_ctf(int late ATTRIBUTE_UNUSED)3930*56bb7041Schristos static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {}
ldlang_write_ctf_late(void)3931*56bb7041Schristos void ldlang_write_ctf_late (void) {}
3932*56bb7041Schristos #endif
3933*56bb7041Schristos 
3934*56bb7041Schristos /* Add the supplied name to the symbol table as an undefined reference.
3935*56bb7041Schristos    This is a two step process as the symbol table doesn't even exist at
3936*56bb7041Schristos    the time the ld command line is processed.  First we put the name
3937*56bb7041Schristos    on a list, then, once the output file has been opened, transfer the
3938*56bb7041Schristos    name to the symbol table.  */
3939*56bb7041Schristos 
3940*56bb7041Schristos typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3941*56bb7041Schristos 
3942*56bb7041Schristos #define ldlang_undef_chain_list_head entry_symbol.next
3943*56bb7041Schristos 
3944*56bb7041Schristos void
ldlang_add_undef(const char * const name,bfd_boolean cmdline ATTRIBUTE_UNUSED)3945*56bb7041Schristos ldlang_add_undef (const char *const name, bfd_boolean cmdline ATTRIBUTE_UNUSED)
3946*56bb7041Schristos {
3947*56bb7041Schristos   ldlang_undef_chain_list_type *new_undef;
3948*56bb7041Schristos 
3949*56bb7041Schristos   new_undef = stat_alloc (sizeof (*new_undef));
3950*56bb7041Schristos   new_undef->next = ldlang_undef_chain_list_head;
3951*56bb7041Schristos   ldlang_undef_chain_list_head = new_undef;
3952*56bb7041Schristos 
3953*56bb7041Schristos   new_undef->name = xstrdup (name);
3954*56bb7041Schristos 
3955*56bb7041Schristos   if (link_info.output_bfd != NULL)
3956*56bb7041Schristos     insert_undefined (new_undef->name);
3957*56bb7041Schristos }
3958*56bb7041Schristos 
3959*56bb7041Schristos /* Insert NAME as undefined in the symbol table.  */
3960*56bb7041Schristos 
3961*56bb7041Schristos static void
insert_undefined(const char * name)3962*56bb7041Schristos insert_undefined (const char *name)
3963*56bb7041Schristos {
3964*56bb7041Schristos   struct bfd_link_hash_entry *h;
3965*56bb7041Schristos 
3966*56bb7041Schristos   h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3967*56bb7041Schristos   if (h == NULL)
3968*56bb7041Schristos     einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3969*56bb7041Schristos   if (h->type == bfd_link_hash_new)
3970*56bb7041Schristos     {
3971*56bb7041Schristos       h->type = bfd_link_hash_undefined;
3972*56bb7041Schristos       h->u.undef.abfd = NULL;
3973*56bb7041Schristos       h->non_ir_ref_regular = TRUE;
3974*56bb7041Schristos       if (is_elf_hash_table (link_info.hash))
3975*56bb7041Schristos 	((struct elf_link_hash_entry *) h)->mark = 1;
3976*56bb7041Schristos       bfd_link_add_undef (link_info.hash, h);
3977*56bb7041Schristos     }
3978*56bb7041Schristos }
3979*56bb7041Schristos 
3980*56bb7041Schristos /* Run through the list of undefineds created above and place them
3981*56bb7041Schristos    into the linker hash table as undefined symbols belonging to the
3982*56bb7041Schristos    script file.  */
3983*56bb7041Schristos 
3984*56bb7041Schristos static void
lang_place_undefineds(void)3985*56bb7041Schristos lang_place_undefineds (void)
3986*56bb7041Schristos {
3987*56bb7041Schristos   ldlang_undef_chain_list_type *ptr;
3988*56bb7041Schristos 
3989*56bb7041Schristos   for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3990*56bb7041Schristos     insert_undefined (ptr->name);
3991*56bb7041Schristos }
3992*56bb7041Schristos 
3993*56bb7041Schristos /* Structure used to build the list of symbols that the user has required
3994*56bb7041Schristos    be defined.  */
3995*56bb7041Schristos 
3996*56bb7041Schristos struct require_defined_symbol
3997*56bb7041Schristos {
3998*56bb7041Schristos   const char *name;
3999*56bb7041Schristos   struct require_defined_symbol *next;
4000*56bb7041Schristos };
4001*56bb7041Schristos 
4002*56bb7041Schristos /* The list of symbols that the user has required be defined.  */
4003*56bb7041Schristos 
4004*56bb7041Schristos static struct require_defined_symbol *require_defined_symbol_list;
4005*56bb7041Schristos 
4006*56bb7041Schristos /* Add a new symbol NAME to the list of symbols that are required to be
4007*56bb7041Schristos    defined.  */
4008*56bb7041Schristos 
4009*56bb7041Schristos void
ldlang_add_require_defined(const char * const name)4010*56bb7041Schristos ldlang_add_require_defined (const char *const name)
4011*56bb7041Schristos {
4012*56bb7041Schristos   struct require_defined_symbol *ptr;
4013*56bb7041Schristos 
4014*56bb7041Schristos   ldlang_add_undef (name, TRUE);
4015*56bb7041Schristos   ptr = stat_alloc (sizeof (*ptr));
4016*56bb7041Schristos   ptr->next = require_defined_symbol_list;
4017*56bb7041Schristos   ptr->name = strdup (name);
4018*56bb7041Schristos   require_defined_symbol_list = ptr;
4019*56bb7041Schristos }
4020*56bb7041Schristos 
4021*56bb7041Schristos /* Check that all symbols the user required to be defined, are defined,
4022*56bb7041Schristos    raise an error if we find a symbol that is not defined.  */
4023*56bb7041Schristos 
4024*56bb7041Schristos static void
ldlang_check_require_defined_symbols(void)4025*56bb7041Schristos ldlang_check_require_defined_symbols (void)
4026*56bb7041Schristos {
4027*56bb7041Schristos   struct require_defined_symbol *ptr;
4028*56bb7041Schristos 
4029*56bb7041Schristos   for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next)
4030*56bb7041Schristos     {
4031*56bb7041Schristos       struct bfd_link_hash_entry *h;
4032*56bb7041Schristos 
4033*56bb7041Schristos       h = bfd_link_hash_lookup (link_info.hash, ptr->name,
4034*56bb7041Schristos 				FALSE, FALSE, TRUE);
4035*56bb7041Schristos       if (h == NULL
4036*56bb7041Schristos 	  || (h->type != bfd_link_hash_defined
4037*56bb7041Schristos 	      && h->type != bfd_link_hash_defweak))
4038*56bb7041Schristos 	einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name);
4039*56bb7041Schristos     }
4040*56bb7041Schristos }
4041*56bb7041Schristos 
4042*56bb7041Schristos /* Check for all readonly or some readwrite sections.  */
4043*56bb7041Schristos 
4044*56bb7041Schristos static void
check_input_sections(lang_statement_union_type * s,lang_output_section_statement_type * output_section_statement)4045*56bb7041Schristos check_input_sections
4046*56bb7041Schristos   (lang_statement_union_type *s,
4047*56bb7041Schristos    lang_output_section_statement_type *output_section_statement)
4048*56bb7041Schristos {
4049*56bb7041Schristos   for (; s != NULL; s = s->header.next)
4050*56bb7041Schristos     {
4051*56bb7041Schristos       switch (s->header.type)
4052*56bb7041Schristos 	{
4053*56bb7041Schristos 	case lang_wild_statement_enum:
4054*56bb7041Schristos 	  walk_wild (&s->wild_statement, check_section_callback,
4055*56bb7041Schristos 		     output_section_statement);
4056*56bb7041Schristos 	  if (!output_section_statement->all_input_readonly)
4057*56bb7041Schristos 	    return;
4058*56bb7041Schristos 	  break;
4059*56bb7041Schristos 	case lang_constructors_statement_enum:
4060*56bb7041Schristos 	  check_input_sections (constructor_list.head,
4061*56bb7041Schristos 				output_section_statement);
4062*56bb7041Schristos 	  if (!output_section_statement->all_input_readonly)
4063*56bb7041Schristos 	    return;
4064*56bb7041Schristos 	  break;
4065*56bb7041Schristos 	case lang_group_statement_enum:
4066*56bb7041Schristos 	  check_input_sections (s->group_statement.children.head,
4067*56bb7041Schristos 				output_section_statement);
4068*56bb7041Schristos 	  if (!output_section_statement->all_input_readonly)
4069*56bb7041Schristos 	    return;
4070*56bb7041Schristos 	  break;
4071*56bb7041Schristos 	default:
4072*56bb7041Schristos 	  break;
4073*56bb7041Schristos 	}
4074*56bb7041Schristos     }
4075*56bb7041Schristos }
4076*56bb7041Schristos 
4077*56bb7041Schristos /* Update wildcard statements if needed.  */
4078*56bb7041Schristos 
4079*56bb7041Schristos static void
update_wild_statements(lang_statement_union_type * s)4080*56bb7041Schristos update_wild_statements (lang_statement_union_type *s)
4081*56bb7041Schristos {
4082*56bb7041Schristos   struct wildcard_list *sec;
4083*56bb7041Schristos 
4084*56bb7041Schristos   switch (sort_section)
4085*56bb7041Schristos     {
4086*56bb7041Schristos     default:
4087*56bb7041Schristos       FAIL ();
4088*56bb7041Schristos 
4089*56bb7041Schristos     case none:
4090*56bb7041Schristos       break;
4091*56bb7041Schristos 
4092*56bb7041Schristos     case by_name:
4093*56bb7041Schristos     case by_alignment:
4094*56bb7041Schristos       for (; s != NULL; s = s->header.next)
4095*56bb7041Schristos 	{
4096*56bb7041Schristos 	  switch (s->header.type)
4097*56bb7041Schristos 	    {
4098*56bb7041Schristos 	    default:
4099*56bb7041Schristos 	      break;
4100*56bb7041Schristos 
4101*56bb7041Schristos 	    case lang_wild_statement_enum:
4102*56bb7041Schristos 	      for (sec = s->wild_statement.section_list; sec != NULL;
4103*56bb7041Schristos 		   sec = sec->next)
4104*56bb7041Schristos 		/* Don't sort .init/.fini sections.  */
4105*56bb7041Schristos 		if (strcmp (sec->spec.name, ".init") != 0
4106*56bb7041Schristos 		    && strcmp (sec->spec.name, ".fini") != 0)
4107*56bb7041Schristos 		  switch (sec->spec.sorted)
4108*56bb7041Schristos 		    {
4109*56bb7041Schristos 		    case none:
4110*56bb7041Schristos 		      sec->spec.sorted = sort_section;
4111*56bb7041Schristos 		      break;
4112*56bb7041Schristos 		    case by_name:
4113*56bb7041Schristos 		      if (sort_section == by_alignment)
4114*56bb7041Schristos 			sec->spec.sorted = by_name_alignment;
4115*56bb7041Schristos 		      break;
4116*56bb7041Schristos 		    case by_alignment:
4117*56bb7041Schristos 		      if (sort_section == by_name)
4118*56bb7041Schristos 			sec->spec.sorted = by_alignment_name;
4119*56bb7041Schristos 		      break;
4120*56bb7041Schristos 		    default:
4121*56bb7041Schristos 		      break;
4122*56bb7041Schristos 		    }
4123*56bb7041Schristos 	      break;
4124*56bb7041Schristos 
4125*56bb7041Schristos 	    case lang_constructors_statement_enum:
4126*56bb7041Schristos 	      update_wild_statements (constructor_list.head);
4127*56bb7041Schristos 	      break;
4128*56bb7041Schristos 
4129*56bb7041Schristos 	    case lang_output_section_statement_enum:
4130*56bb7041Schristos 	      update_wild_statements
4131*56bb7041Schristos 		(s->output_section_statement.children.head);
4132*56bb7041Schristos 	      break;
4133*56bb7041Schristos 
4134*56bb7041Schristos 	    case lang_group_statement_enum:
4135*56bb7041Schristos 	      update_wild_statements (s->group_statement.children.head);
4136*56bb7041Schristos 	      break;
4137*56bb7041Schristos 	    }
4138*56bb7041Schristos 	}
4139*56bb7041Schristos       break;
4140*56bb7041Schristos     }
4141*56bb7041Schristos }
4142*56bb7041Schristos 
4143*56bb7041Schristos /* Open input files and attach to output sections.  */
4144*56bb7041Schristos 
4145*56bb7041Schristos static void
map_input_to_output_sections(lang_statement_union_type * s,const char * target,lang_output_section_statement_type * os)4146*56bb7041Schristos map_input_to_output_sections
4147*56bb7041Schristos   (lang_statement_union_type *s, const char *target,
4148*56bb7041Schristos    lang_output_section_statement_type *os)
4149*56bb7041Schristos {
4150*56bb7041Schristos   for (; s != NULL; s = s->header.next)
4151*56bb7041Schristos     {
4152*56bb7041Schristos       lang_output_section_statement_type *tos;
4153*56bb7041Schristos       flagword flags;
4154*56bb7041Schristos 
4155*56bb7041Schristos       switch (s->header.type)
4156*56bb7041Schristos 	{
4157*56bb7041Schristos 	case lang_wild_statement_enum:
4158*56bb7041Schristos 	  wild (&s->wild_statement, target, os);
4159*56bb7041Schristos 	  break;
4160*56bb7041Schristos 	case lang_constructors_statement_enum:
4161*56bb7041Schristos 	  map_input_to_output_sections (constructor_list.head,
4162*56bb7041Schristos 					target,
4163*56bb7041Schristos 					os);
4164*56bb7041Schristos 	  break;
4165*56bb7041Schristos 	case lang_output_section_statement_enum:
4166*56bb7041Schristos 	  tos = &s->output_section_statement;
4167*56bb7041Schristos 	  if (tos->constraint != 0)
4168*56bb7041Schristos 	    {
4169*56bb7041Schristos 	      if (tos->constraint != ONLY_IF_RW
4170*56bb7041Schristos 		  && tos->constraint != ONLY_IF_RO)
4171*56bb7041Schristos 		break;
4172*56bb7041Schristos 	      tos->all_input_readonly = TRUE;
4173*56bb7041Schristos 	      check_input_sections (tos->children.head, tos);
4174*56bb7041Schristos 	      if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
4175*56bb7041Schristos 		{
4176*56bb7041Schristos 		  tos->constraint = -1;
4177*56bb7041Schristos 		  break;
4178*56bb7041Schristos 		}
4179*56bb7041Schristos 	    }
4180*56bb7041Schristos 	  map_input_to_output_sections (tos->children.head,
4181*56bb7041Schristos 					target,
4182*56bb7041Schristos 					tos);
4183*56bb7041Schristos 	  break;
4184*56bb7041Schristos 	case lang_output_statement_enum:
4185*56bb7041Schristos 	  break;
4186*56bb7041Schristos 	case lang_target_statement_enum:
4187*56bb7041Schristos 	  target = s->target_statement.target;
4188*56bb7041Schristos 	  break;
4189*56bb7041Schristos 	case lang_group_statement_enum:
4190*56bb7041Schristos 	  map_input_to_output_sections (s->group_statement.children.head,
4191*56bb7041Schristos 					target,
4192*56bb7041Schristos 					os);
4193*56bb7041Schristos 	  break;
4194*56bb7041Schristos 	case lang_data_statement_enum:
4195*56bb7041Schristos 	  /* Make sure that any sections mentioned in the expression
4196*56bb7041Schristos 	     are initialized.  */
4197*56bb7041Schristos 	  exp_init_os (s->data_statement.exp);
4198*56bb7041Schristos 	  /* The output section gets CONTENTS, ALLOC and LOAD, but
4199*56bb7041Schristos 	     these may be overridden by the script.  */
4200*56bb7041Schristos 	  flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
4201*56bb7041Schristos 	  switch (os->sectype)
4202*56bb7041Schristos 	    {
4203*56bb7041Schristos 	    case normal_section:
4204*56bb7041Schristos 	    case overlay_section:
4205*56bb7041Schristos 	    case first_overlay_section:
4206*56bb7041Schristos 	      break;
4207*56bb7041Schristos 	    case noalloc_section:
4208*56bb7041Schristos 	      flags = SEC_HAS_CONTENTS;
4209*56bb7041Schristos 	      break;
4210*56bb7041Schristos 	    case noload_section:
4211*56bb7041Schristos 	      if (bfd_get_flavour (link_info.output_bfd)
4212*56bb7041Schristos 		  == bfd_target_elf_flavour)
4213*56bb7041Schristos 		flags = SEC_NEVER_LOAD | SEC_ALLOC;
4214*56bb7041Schristos 	      else
4215*56bb7041Schristos 		flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
4216*56bb7041Schristos 	      break;
4217*56bb7041Schristos 	    }
4218*56bb7041Schristos 	  if (os->bfd_section == NULL)
4219*56bb7041Schristos 	    init_os (os, flags);
4220*56bb7041Schristos 	  else
4221*56bb7041Schristos 	    os->bfd_section->flags |= flags;
4222*56bb7041Schristos 	  break;
4223*56bb7041Schristos 	case lang_input_section_enum:
4224*56bb7041Schristos 	  break;
4225*56bb7041Schristos 	case lang_fill_statement_enum:
4226*56bb7041Schristos 	case lang_object_symbols_statement_enum:
4227*56bb7041Schristos 	case lang_reloc_statement_enum:
4228*56bb7041Schristos 	case lang_padding_statement_enum:
4229*56bb7041Schristos 	case lang_input_statement_enum:
4230*56bb7041Schristos 	  if (os != NULL && os->bfd_section == NULL)
4231*56bb7041Schristos 	    init_os (os, 0);
4232*56bb7041Schristos 	  break;
4233*56bb7041Schristos 	case lang_assignment_statement_enum:
4234*56bb7041Schristos 	  if (os != NULL && os->bfd_section == NULL)
4235*56bb7041Schristos 	    init_os (os, 0);
4236*56bb7041Schristos 
4237*56bb7041Schristos 	  /* Make sure that any sections mentioned in the assignment
4238*56bb7041Schristos 	     are initialized.  */
4239*56bb7041Schristos 	  exp_init_os (s->assignment_statement.exp);
4240*56bb7041Schristos 	  break;
4241*56bb7041Schristos 	case lang_address_statement_enum:
4242*56bb7041Schristos 	  /* Mark the specified section with the supplied address.
4243*56bb7041Schristos 	     If this section was actually a segment marker, then the
4244*56bb7041Schristos 	     directive is ignored if the linker script explicitly
4245*56bb7041Schristos 	     processed the segment marker.  Originally, the linker
4246*56bb7041Schristos 	     treated segment directives (like -Ttext on the
4247*56bb7041Schristos 	     command-line) as section directives.  We honor the
4248*56bb7041Schristos 	     section directive semantics for backwards compatibility;
4249*56bb7041Schristos 	     linker scripts that do not specifically check for
4250*56bb7041Schristos 	     SEGMENT_START automatically get the old semantics.  */
4251*56bb7041Schristos 	  if (!s->address_statement.segment
4252*56bb7041Schristos 	      || !s->address_statement.segment->used)
4253*56bb7041Schristos 	    {
4254*56bb7041Schristos 	      const char *name = s->address_statement.section_name;
4255*56bb7041Schristos 
4256*56bb7041Schristos 	      /* Create the output section statement here so that
4257*56bb7041Schristos 		 orphans with a set address will be placed after other
4258*56bb7041Schristos 		 script sections.  If we let the orphan placement code
4259*56bb7041Schristos 		 place them in amongst other sections then the address
4260*56bb7041Schristos 		 will affect following script sections, which is
4261*56bb7041Schristos 		 likely to surprise naive users.  */
4262*56bb7041Schristos 	      tos = lang_output_section_statement_lookup (name, 0, TRUE);
4263*56bb7041Schristos 	      tos->addr_tree = s->address_statement.address;
4264*56bb7041Schristos 	      if (tos->bfd_section == NULL)
4265*56bb7041Schristos 		init_os (tos, 0);
4266*56bb7041Schristos 	    }
4267*56bb7041Schristos 	  break;
4268*56bb7041Schristos 	case lang_insert_statement_enum:
4269*56bb7041Schristos 	  break;
4270*56bb7041Schristos 	}
4271*56bb7041Schristos     }
4272*56bb7041Schristos }
4273*56bb7041Schristos 
4274*56bb7041Schristos /* An insert statement snips out all the linker statements from the
4275*56bb7041Schristos    start of the list and places them after the output section
4276*56bb7041Schristos    statement specified by the insert.  This operation is complicated
4277*56bb7041Schristos    by the fact that we keep a doubly linked list of output section
4278*56bb7041Schristos    statements as well as the singly linked list of all statements.
4279*56bb7041Schristos    FIXME someday: Twiddling with the list not only moves statements
4280*56bb7041Schristos    from the user's script but also input and group statements that are
4281*56bb7041Schristos    built from command line object files and --start-group.  We only
4282*56bb7041Schristos    get away with this because the list pointers used by file_chain
4283*56bb7041Schristos    and input_file_chain are not reordered, and processing via
4284*56bb7041Schristos    statement_list after this point mostly ignores input statements.
4285*56bb7041Schristos    One exception is the map file, where LOAD and START GROUP/END GROUP
4286*56bb7041Schristos    can end up looking odd.  */
4287*56bb7041Schristos 
4288*56bb7041Schristos static void
process_insert_statements(lang_statement_union_type ** start)4289*56bb7041Schristos process_insert_statements (lang_statement_union_type **start)
4290*56bb7041Schristos {
4291*56bb7041Schristos   lang_statement_union_type **s;
4292*56bb7041Schristos   lang_output_section_statement_type *first_os = NULL;
4293*56bb7041Schristos   lang_output_section_statement_type *last_os = NULL;
4294*56bb7041Schristos   lang_output_section_statement_type *os;
4295*56bb7041Schristos 
4296*56bb7041Schristos   s = start;
4297*56bb7041Schristos   while (*s != NULL)
4298*56bb7041Schristos     {
4299*56bb7041Schristos       if ((*s)->header.type == lang_output_section_statement_enum)
4300*56bb7041Schristos 	{
4301*56bb7041Schristos 	  /* Keep pointers to the first and last output section
4302*56bb7041Schristos 	     statement in the sequence we may be about to move.  */
4303*56bb7041Schristos 	  os = &(*s)->output_section_statement;
4304*56bb7041Schristos 
4305*56bb7041Schristos 	  ASSERT (last_os == NULL || last_os->next == os);
4306*56bb7041Schristos 	  last_os = os;
4307*56bb7041Schristos 
4308*56bb7041Schristos 	  /* Set constraint negative so that lang_output_section_find
4309*56bb7041Schristos 	     won't match this output section statement.  At this
4310*56bb7041Schristos 	     stage in linking constraint has values in the range
4311*56bb7041Schristos 	     [-1, ONLY_IN_RW].  */
4312*56bb7041Schristos 	  last_os->constraint = -2 - last_os->constraint;
4313*56bb7041Schristos 	  if (first_os == NULL)
4314*56bb7041Schristos 	    first_os = last_os;
4315*56bb7041Schristos 	}
4316*56bb7041Schristos       else if ((*s)->header.type == lang_group_statement_enum)
4317*56bb7041Schristos 	{
4318*56bb7041Schristos 	  /* A user might put -T between --start-group and
4319*56bb7041Schristos 	     --end-group.  One way this odd construct might arise is
4320*56bb7041Schristos 	     from a wrapper around ld to change library search
4321*56bb7041Schristos 	     behaviour.  For example:
4322*56bb7041Schristos 	     #! /bin/sh
4323*56bb7041Schristos 	     exec real_ld --start-group "$@" --end-group
4324*56bb7041Schristos 	     This isn't completely unreasonable so go looking inside a
4325*56bb7041Schristos 	     group statement for insert statements.  */
4326*56bb7041Schristos 	  process_insert_statements (&(*s)->group_statement.children.head);
4327*56bb7041Schristos 	}
4328*56bb7041Schristos       else if ((*s)->header.type == lang_insert_statement_enum)
4329*56bb7041Schristos 	{
4330*56bb7041Schristos 	  lang_insert_statement_type *i = &(*s)->insert_statement;
4331*56bb7041Schristos 	  lang_output_section_statement_type *where;
4332*56bb7041Schristos 	  lang_statement_union_type **ptr;
4333*56bb7041Schristos 	  lang_statement_union_type *first;
4334*56bb7041Schristos 
4335*56bb7041Schristos 	  if (link_info.non_contiguous_regions)
4336*56bb7041Schristos 	    {
4337*56bb7041Schristos 	      einfo (_("warning: INSERT statement in linker script is "
4338*56bb7041Schristos 		       "incompatible with --enable-non-contiguous-regions.\n"));
4339*56bb7041Schristos 	    }
4340*56bb7041Schristos 
4341*56bb7041Schristos 	  where = lang_output_section_find (i->where);
4342*56bb7041Schristos 	  if (where != NULL && i->is_before)
4343*56bb7041Schristos 	    {
4344*56bb7041Schristos 	      do
4345*56bb7041Schristos 		where = where->prev;
4346*56bb7041Schristos 	      while (where != NULL && where->constraint < 0);
4347*56bb7041Schristos 	    }
4348*56bb7041Schristos 	  if (where == NULL)
4349*56bb7041Schristos 	    {
4350*56bb7041Schristos 	      einfo (_("%F%P: %s not found for insert\n"), i->where);
4351*56bb7041Schristos 	      return;
4352*56bb7041Schristos 	    }
4353*56bb7041Schristos 
4354*56bb7041Schristos 	  /* Deal with reordering the output section statement list.  */
4355*56bb7041Schristos 	  if (last_os != NULL)
4356*56bb7041Schristos 	    {
4357*56bb7041Schristos 	      asection *first_sec, *last_sec;
4358*56bb7041Schristos 	      struct lang_output_section_statement_struct **next;
4359*56bb7041Schristos 
4360*56bb7041Schristos 	      /* Snip out the output sections we are moving.  */
4361*56bb7041Schristos 	      first_os->prev->next = last_os->next;
4362*56bb7041Schristos 	      if (last_os->next == NULL)
4363*56bb7041Schristos 		{
4364*56bb7041Schristos 		  next = &first_os->prev->next;
4365*56bb7041Schristos 		  lang_os_list.tail = (lang_statement_union_type **) next;
4366*56bb7041Schristos 		}
4367*56bb7041Schristos 	      else
4368*56bb7041Schristos 		last_os->next->prev = first_os->prev;
4369*56bb7041Schristos 	      /* Add them in at the new position.  */
4370*56bb7041Schristos 	      last_os->next = where->next;
4371*56bb7041Schristos 	      if (where->next == NULL)
4372*56bb7041Schristos 		{
4373*56bb7041Schristos 		  next = &last_os->next;
4374*56bb7041Schristos 		  lang_os_list.tail = (lang_statement_union_type **) next;
4375*56bb7041Schristos 		}
4376*56bb7041Schristos 	      else
4377*56bb7041Schristos 		where->next->prev = last_os;
4378*56bb7041Schristos 	      first_os->prev = where;
4379*56bb7041Schristos 	      where->next = first_os;
4380*56bb7041Schristos 
4381*56bb7041Schristos 	      /* Move the bfd sections in the same way.  */
4382*56bb7041Schristos 	      first_sec = NULL;
4383*56bb7041Schristos 	      last_sec = NULL;
4384*56bb7041Schristos 	      for (os = first_os; os != NULL; os = os->next)
4385*56bb7041Schristos 		{
4386*56bb7041Schristos 		  os->constraint = -2 - os->constraint;
4387*56bb7041Schristos 		  if (os->bfd_section != NULL
4388*56bb7041Schristos 		      && os->bfd_section->owner != NULL)
4389*56bb7041Schristos 		    {
4390*56bb7041Schristos 		      last_sec = os->bfd_section;
4391*56bb7041Schristos 		      if (first_sec == NULL)
4392*56bb7041Schristos 			first_sec = last_sec;
4393*56bb7041Schristos 		    }
4394*56bb7041Schristos 		  if (os == last_os)
4395*56bb7041Schristos 		    break;
4396*56bb7041Schristos 		}
4397*56bb7041Schristos 	      if (last_sec != NULL)
4398*56bb7041Schristos 		{
4399*56bb7041Schristos 		  asection *sec = where->bfd_section;
4400*56bb7041Schristos 		  if (sec == NULL)
4401*56bb7041Schristos 		    sec = output_prev_sec_find (where);
4402*56bb7041Schristos 
4403*56bb7041Schristos 		  /* The place we want to insert must come after the
4404*56bb7041Schristos 		     sections we are moving.  So if we find no
4405*56bb7041Schristos 		     section or if the section is the same as our
4406*56bb7041Schristos 		     last section, then no move is needed.  */
4407*56bb7041Schristos 		  if (sec != NULL && sec != last_sec)
4408*56bb7041Schristos 		    {
4409*56bb7041Schristos 		      /* Trim them off.  */
4410*56bb7041Schristos 		      if (first_sec->prev != NULL)
4411*56bb7041Schristos 			first_sec->prev->next = last_sec->next;
4412*56bb7041Schristos 		      else
4413*56bb7041Schristos 			link_info.output_bfd->sections = last_sec->next;
4414*56bb7041Schristos 		      if (last_sec->next != NULL)
4415*56bb7041Schristos 			last_sec->next->prev = first_sec->prev;
4416*56bb7041Schristos 		      else
4417*56bb7041Schristos 			link_info.output_bfd->section_last = first_sec->prev;
4418*56bb7041Schristos 		      /* Add back.  */
4419*56bb7041Schristos 		      last_sec->next = sec->next;
4420*56bb7041Schristos 		      if (sec->next != NULL)
4421*56bb7041Schristos 			sec->next->prev = last_sec;
4422*56bb7041Schristos 		      else
4423*56bb7041Schristos 			link_info.output_bfd->section_last = last_sec;
4424*56bb7041Schristos 		      first_sec->prev = sec;
4425*56bb7041Schristos 		      sec->next = first_sec;
4426*56bb7041Schristos 		    }
4427*56bb7041Schristos 		}
4428*56bb7041Schristos 
4429*56bb7041Schristos 	      first_os = NULL;
4430*56bb7041Schristos 	      last_os = NULL;
4431*56bb7041Schristos 	    }
4432*56bb7041Schristos 
4433*56bb7041Schristos 	  ptr = insert_os_after (where);
4434*56bb7041Schristos 	  /* Snip everything from the start of the list, up to and
4435*56bb7041Schristos 	     including the insert statement we are currently processing.  */
4436*56bb7041Schristos 	  first = *start;
4437*56bb7041Schristos 	  *start = (*s)->header.next;
4438*56bb7041Schristos 	  /* Add them back where they belong, minus the insert.  */
4439*56bb7041Schristos 	  *s = *ptr;
4440*56bb7041Schristos 	  if (*s == NULL)
4441*56bb7041Schristos 	    statement_list.tail = s;
4442*56bb7041Schristos 	  *ptr = first;
4443*56bb7041Schristos 	  s = start;
4444*56bb7041Schristos 	  continue;
4445*56bb7041Schristos 	}
4446*56bb7041Schristos       s = &(*s)->header.next;
4447*56bb7041Schristos     }
4448*56bb7041Schristos 
4449*56bb7041Schristos   /* Undo constraint twiddling.  */
4450*56bb7041Schristos   for (os = first_os; os != NULL; os = os->next)
4451*56bb7041Schristos     {
4452*56bb7041Schristos       os->constraint = -2 - os->constraint;
4453*56bb7041Schristos       if (os == last_os)
4454*56bb7041Schristos 	break;
4455*56bb7041Schristos     }
4456*56bb7041Schristos }
4457*56bb7041Schristos 
4458*56bb7041Schristos /* An output section might have been removed after its statement was
4459*56bb7041Schristos    added.  For example, ldemul_before_allocation can remove dynamic
4460*56bb7041Schristos    sections if they turn out to be not needed.  Clean them up here.  */
4461*56bb7041Schristos 
4462*56bb7041Schristos void
strip_excluded_output_sections(void)4463*56bb7041Schristos strip_excluded_output_sections (void)
4464*56bb7041Schristos {
4465*56bb7041Schristos   lang_output_section_statement_type *os;
4466*56bb7041Schristos 
4467*56bb7041Schristos   /* Run lang_size_sections (if not already done).  */
4468*56bb7041Schristos   if (expld.phase != lang_mark_phase_enum)
4469*56bb7041Schristos     {
4470*56bb7041Schristos       expld.phase = lang_mark_phase_enum;
4471*56bb7041Schristos       expld.dataseg.phase = exp_seg_none;
4472*56bb7041Schristos       one_lang_size_sections_pass (NULL, FALSE);
4473*56bb7041Schristos       lang_reset_memory_regions ();
4474*56bb7041Schristos     }
4475*56bb7041Schristos 
4476*56bb7041Schristos   for (os = (void *) lang_os_list.head;
4477*56bb7041Schristos        os != NULL;
4478*56bb7041Schristos        os = os->next)
4479*56bb7041Schristos     {
4480*56bb7041Schristos       asection *output_section;
4481*56bb7041Schristos       bfd_boolean exclude;
4482*56bb7041Schristos 
4483*56bb7041Schristos       if (os->constraint < 0)
4484*56bb7041Schristos 	continue;
4485*56bb7041Schristos 
4486*56bb7041Schristos       output_section = os->bfd_section;
4487*56bb7041Schristos       if (output_section == NULL)
4488*56bb7041Schristos 	continue;
4489*56bb7041Schristos 
4490*56bb7041Schristos       exclude = (output_section->rawsize == 0
4491*56bb7041Schristos 		 && (output_section->flags & SEC_KEEP) == 0
4492*56bb7041Schristos 		 && !bfd_section_removed_from_list (link_info.output_bfd,
4493*56bb7041Schristos 						    output_section));
4494*56bb7041Schristos 
4495*56bb7041Schristos       /* Some sections have not yet been sized, notably .gnu.version,
4496*56bb7041Schristos 	 .dynsym, .dynstr and .hash.  These all have SEC_LINKER_CREATED
4497*56bb7041Schristos 	 input sections, so don't drop output sections that have such
4498*56bb7041Schristos 	 input sections unless they are also marked SEC_EXCLUDE.  */
4499*56bb7041Schristos       if (exclude && output_section->map_head.s != NULL)
4500*56bb7041Schristos 	{
4501*56bb7041Schristos 	  asection *s;
4502*56bb7041Schristos 
4503*56bb7041Schristos 	  for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
4504*56bb7041Schristos 	    if ((s->flags & SEC_EXCLUDE) == 0
4505*56bb7041Schristos 		&& ((s->flags & SEC_LINKER_CREATED) != 0
4506*56bb7041Schristos 		    || link_info.emitrelocations))
4507*56bb7041Schristos 	      {
4508*56bb7041Schristos 		exclude = FALSE;
4509*56bb7041Schristos 		break;
4510*56bb7041Schristos 	      }
4511*56bb7041Schristos 	}
4512*56bb7041Schristos 
4513*56bb7041Schristos       if (exclude)
4514*56bb7041Schristos 	{
4515*56bb7041Schristos 	  /* We don't set bfd_section to NULL since bfd_section of the
4516*56bb7041Schristos 	     removed output section statement may still be used.  */
4517*56bb7041Schristos 	  if (!os->update_dot)
4518*56bb7041Schristos 	    os->ignored = TRUE;
4519*56bb7041Schristos 	  output_section->flags |= SEC_EXCLUDE;
4520*56bb7041Schristos 	  bfd_section_list_remove (link_info.output_bfd, output_section);
4521*56bb7041Schristos 	  link_info.output_bfd->section_count--;
4522*56bb7041Schristos 	}
4523*56bb7041Schristos     }
4524*56bb7041Schristos }
4525*56bb7041Schristos 
4526*56bb7041Schristos /* Called from ldwrite to clear out asection.map_head and
4527*56bb7041Schristos    asection.map_tail for use as link_orders in ldwrite.  */
4528*56bb7041Schristos 
4529*56bb7041Schristos void
lang_clear_os_map(void)4530*56bb7041Schristos lang_clear_os_map (void)
4531*56bb7041Schristos {
4532*56bb7041Schristos   lang_output_section_statement_type *os;
4533*56bb7041Schristos 
4534*56bb7041Schristos   if (map_head_is_link_order)
4535*56bb7041Schristos     return;
4536*56bb7041Schristos 
4537*56bb7041Schristos   for (os = (void *) lang_os_list.head;
4538*56bb7041Schristos        os != NULL;
4539*56bb7041Schristos        os = os->next)
4540*56bb7041Schristos     {
4541*56bb7041Schristos       asection *output_section;
4542*56bb7041Schristos 
4543*56bb7041Schristos       if (os->constraint < 0)
4544*56bb7041Schristos 	continue;
4545*56bb7041Schristos 
4546*56bb7041Schristos       output_section = os->bfd_section;
4547*56bb7041Schristos       if (output_section == NULL)
4548*56bb7041Schristos 	continue;
4549*56bb7041Schristos 
4550*56bb7041Schristos       /* TODO: Don't just junk map_head.s, turn them into link_orders.  */
4551*56bb7041Schristos       output_section->map_head.link_order = NULL;
4552*56bb7041Schristos       output_section->map_tail.link_order = NULL;
4553*56bb7041Schristos     }
4554*56bb7041Schristos 
4555*56bb7041Schristos   /* Stop future calls to lang_add_section from messing with map_head
4556*56bb7041Schristos      and map_tail link_order fields.  */
4557*56bb7041Schristos   map_head_is_link_order = TRUE;
4558*56bb7041Schristos }
4559*56bb7041Schristos 
4560*56bb7041Schristos static void
print_output_section_statement(lang_output_section_statement_type * output_section_statement)4561*56bb7041Schristos print_output_section_statement
4562*56bb7041Schristos   (lang_output_section_statement_type *output_section_statement)
4563*56bb7041Schristos {
4564*56bb7041Schristos   asection *section = output_section_statement->bfd_section;
4565*56bb7041Schristos   int len;
4566*56bb7041Schristos 
4567*56bb7041Schristos   if (output_section_statement != abs_output_section)
4568*56bb7041Schristos     {
4569*56bb7041Schristos       minfo ("\n%s", output_section_statement->name);
4570*56bb7041Schristos 
4571*56bb7041Schristos       if (section != NULL)
4572*56bb7041Schristos 	{
4573*56bb7041Schristos 	  print_dot = section->vma;
4574*56bb7041Schristos 
4575*56bb7041Schristos 	  len = strlen (output_section_statement->name);
4576*56bb7041Schristos 	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
4577*56bb7041Schristos 	    {
4578*56bb7041Schristos 	      print_nl ();
4579*56bb7041Schristos 	      len = 0;
4580*56bb7041Schristos 	    }
4581*56bb7041Schristos 	  while (len < SECTION_NAME_MAP_LENGTH)
4582*56bb7041Schristos 	    {
4583*56bb7041Schristos 	      print_space ();
4584*56bb7041Schristos 	      ++len;
4585*56bb7041Schristos 	    }
4586*56bb7041Schristos 
4587*56bb7041Schristos 	  minfo ("0x%V %W", section->vma, TO_ADDR (section->size));
4588*56bb7041Schristos 
4589*56bb7041Schristos 	  if (section->vma != section->lma)
4590*56bb7041Schristos 	    minfo (_(" load address 0x%V"), section->lma);
4591*56bb7041Schristos 
4592*56bb7041Schristos 	  if (output_section_statement->update_dot_tree != NULL)
4593*56bb7041Schristos 	    exp_fold_tree (output_section_statement->update_dot_tree,
4594*56bb7041Schristos 			   bfd_abs_section_ptr, &print_dot);
4595*56bb7041Schristos 	}
4596*56bb7041Schristos 
4597*56bb7041Schristos       print_nl ();
4598*56bb7041Schristos     }
4599*56bb7041Schristos 
4600*56bb7041Schristos   print_statement_list (output_section_statement->children.head,
4601*56bb7041Schristos 			output_section_statement);
4602*56bb7041Schristos }
4603*56bb7041Schristos 
4604*56bb7041Schristos static void
print_assignment(lang_assignment_statement_type * assignment,lang_output_section_statement_type * output_section)4605*56bb7041Schristos print_assignment (lang_assignment_statement_type *assignment,
4606*56bb7041Schristos 		  lang_output_section_statement_type *output_section)
4607*56bb7041Schristos {
4608*56bb7041Schristos   unsigned int i;
4609*56bb7041Schristos   bfd_boolean is_dot;
4610*56bb7041Schristos   etree_type *tree;
4611*56bb7041Schristos   asection *osec;
4612*56bb7041Schristos 
4613*56bb7041Schristos   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4614*56bb7041Schristos     print_space ();
4615*56bb7041Schristos 
4616*56bb7041Schristos   if (assignment->exp->type.node_class == etree_assert)
4617*56bb7041Schristos     {
4618*56bb7041Schristos       is_dot = FALSE;
4619*56bb7041Schristos       tree = assignment->exp->assert_s.child;
4620*56bb7041Schristos     }
4621*56bb7041Schristos   else
4622*56bb7041Schristos     {
4623*56bb7041Schristos       const char *dst = assignment->exp->assign.dst;
4624*56bb7041Schristos 
4625*56bb7041Schristos       is_dot = (dst[0] == '.' && dst[1] == 0);
4626*56bb7041Schristos       tree = assignment->exp;
4627*56bb7041Schristos     }
4628*56bb7041Schristos 
4629*56bb7041Schristos   osec = output_section->bfd_section;
4630*56bb7041Schristos   if (osec == NULL)
4631*56bb7041Schristos     osec = bfd_abs_section_ptr;
4632*56bb7041Schristos 
4633*56bb7041Schristos   if (assignment->exp->type.node_class != etree_provide)
4634*56bb7041Schristos     exp_fold_tree (tree, osec, &print_dot);
4635*56bb7041Schristos   else
4636*56bb7041Schristos     expld.result.valid_p = FALSE;
4637*56bb7041Schristos 
4638*56bb7041Schristos   if (expld.result.valid_p)
4639*56bb7041Schristos     {
4640*56bb7041Schristos       bfd_vma value;
4641*56bb7041Schristos 
4642*56bb7041Schristos       if (assignment->exp->type.node_class == etree_assert
4643*56bb7041Schristos 	  || is_dot
4644*56bb7041Schristos 	  || expld.assign_name != NULL)
4645*56bb7041Schristos 	{
4646*56bb7041Schristos 	  value = expld.result.value;
4647*56bb7041Schristos 
4648*56bb7041Schristos 	  if (expld.result.section != NULL)
4649*56bb7041Schristos 	    value += expld.result.section->vma;
4650*56bb7041Schristos 
4651*56bb7041Schristos 	  minfo ("0x%V", value);
4652*56bb7041Schristos 	  if (is_dot)
4653*56bb7041Schristos 	    print_dot = value;
4654*56bb7041Schristos 	}
4655*56bb7041Schristos       else
4656*56bb7041Schristos 	{
4657*56bb7041Schristos 	  struct bfd_link_hash_entry *h;
4658*56bb7041Schristos 
4659*56bb7041Schristos 	  h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
4660*56bb7041Schristos 				    FALSE, FALSE, TRUE);
4661*56bb7041Schristos 	  if (h != NULL
4662*56bb7041Schristos 	      && (h->type == bfd_link_hash_defined
4663*56bb7041Schristos 		  || h->type == bfd_link_hash_defweak))
4664*56bb7041Schristos 	    {
4665*56bb7041Schristos 	      value = h->u.def.value;
4666*56bb7041Schristos 	      value += h->u.def.section->output_section->vma;
4667*56bb7041Schristos 	      value += h->u.def.section->output_offset;
4668*56bb7041Schristos 
4669*56bb7041Schristos 	      minfo ("[0x%V]", value);
4670*56bb7041Schristos 	    }
4671*56bb7041Schristos 	  else
4672*56bb7041Schristos 	    minfo ("[unresolved]");
4673*56bb7041Schristos 	}
4674*56bb7041Schristos     }
4675*56bb7041Schristos   else
4676*56bb7041Schristos     {
4677*56bb7041Schristos       if (assignment->exp->type.node_class == etree_provide)
4678*56bb7041Schristos 	minfo ("[!provide]");
4679*56bb7041Schristos       else
4680*56bb7041Schristos 	minfo ("*undef*   ");
4681*56bb7041Schristos #ifdef BFD64
4682*56bb7041Schristos       minfo ("        ");
4683*56bb7041Schristos #endif
4684*56bb7041Schristos     }
4685*56bb7041Schristos   expld.assign_name = NULL;
4686*56bb7041Schristos 
4687*56bb7041Schristos   minfo ("                ");
4688*56bb7041Schristos   exp_print_tree (assignment->exp);
4689*56bb7041Schristos   print_nl ();
4690*56bb7041Schristos }
4691*56bb7041Schristos 
4692*56bb7041Schristos static void
print_input_statement(lang_input_statement_type * statm)4693*56bb7041Schristos print_input_statement (lang_input_statement_type *statm)
4694*56bb7041Schristos {
4695*56bb7041Schristos   if (statm->filename != NULL)
4696*56bb7041Schristos     fprintf (config.map_file, "LOAD %s\n", statm->filename);
4697*56bb7041Schristos }
4698*56bb7041Schristos 
4699*56bb7041Schristos /* Print all symbols defined in a particular section.  This is called
4700*56bb7041Schristos    via bfd_link_hash_traverse, or by print_all_symbols.  */
4701*56bb7041Schristos 
4702*56bb7041Schristos bfd_boolean
print_one_symbol(struct bfd_link_hash_entry * hash_entry,void * ptr)4703*56bb7041Schristos print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
4704*56bb7041Schristos {
4705*56bb7041Schristos   asection *sec = (asection *) ptr;
4706*56bb7041Schristos 
4707*56bb7041Schristos   if ((hash_entry->type == bfd_link_hash_defined
4708*56bb7041Schristos        || hash_entry->type == bfd_link_hash_defweak)
4709*56bb7041Schristos       && sec == hash_entry->u.def.section)
4710*56bb7041Schristos     {
4711*56bb7041Schristos       int i;
4712*56bb7041Schristos 
4713*56bb7041Schristos       for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4714*56bb7041Schristos 	print_space ();
4715*56bb7041Schristos       minfo ("0x%V   ",
4716*56bb7041Schristos 	     (hash_entry->u.def.value
4717*56bb7041Schristos 	      + hash_entry->u.def.section->output_offset
4718*56bb7041Schristos 	      + hash_entry->u.def.section->output_section->vma));
4719*56bb7041Schristos 
4720*56bb7041Schristos       minfo ("             %pT\n", hash_entry->root.string);
4721*56bb7041Schristos     }
4722*56bb7041Schristos 
4723*56bb7041Schristos   return TRUE;
4724*56bb7041Schristos }
4725*56bb7041Schristos 
4726*56bb7041Schristos static int
hash_entry_addr_cmp(const void * a,const void * b)4727*56bb7041Schristos hash_entry_addr_cmp (const void *a, const void *b)
4728*56bb7041Schristos {
4729*56bb7041Schristos   const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
4730*56bb7041Schristos   const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
4731*56bb7041Schristos 
4732*56bb7041Schristos   if (l->u.def.value < r->u.def.value)
4733*56bb7041Schristos     return -1;
4734*56bb7041Schristos   else if (l->u.def.value > r->u.def.value)
4735*56bb7041Schristos     return 1;
4736*56bb7041Schristos   else
4737*56bb7041Schristos     return 0;
4738*56bb7041Schristos }
4739*56bb7041Schristos 
4740*56bb7041Schristos static void
print_all_symbols(asection * sec)4741*56bb7041Schristos print_all_symbols (asection *sec)
4742*56bb7041Schristos {
4743*56bb7041Schristos   input_section_userdata_type *ud = bfd_section_userdata (sec);
4744*56bb7041Schristos   struct map_symbol_def *def;
4745*56bb7041Schristos   struct bfd_link_hash_entry **entries;
4746*56bb7041Schristos   unsigned int i;
4747*56bb7041Schristos 
4748*56bb7041Schristos   if (!ud)
4749*56bb7041Schristos     return;
4750*56bb7041Schristos 
4751*56bb7041Schristos   *ud->map_symbol_def_tail = 0;
4752*56bb7041Schristos 
4753*56bb7041Schristos   /* Sort the symbols by address.  */
4754*56bb7041Schristos   entries = (struct bfd_link_hash_entry **)
4755*56bb7041Schristos       obstack_alloc (&map_obstack,
4756*56bb7041Schristos 		     ud->map_symbol_def_count * sizeof (*entries));
4757*56bb7041Schristos 
4758*56bb7041Schristos   for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
4759*56bb7041Schristos     entries[i] = def->entry;
4760*56bb7041Schristos 
4761*56bb7041Schristos   qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
4762*56bb7041Schristos 	 hash_entry_addr_cmp);
4763*56bb7041Schristos 
4764*56bb7041Schristos   /* Print the symbols.  */
4765*56bb7041Schristos   for (i = 0; i < ud->map_symbol_def_count; i++)
4766*56bb7041Schristos     ldemul_print_symbol (entries[i], sec);
4767*56bb7041Schristos 
4768*56bb7041Schristos   obstack_free (&map_obstack, entries);
4769*56bb7041Schristos }
4770*56bb7041Schristos 
4771*56bb7041Schristos /* Print information about an input section to the map file.  */
4772*56bb7041Schristos 
4773*56bb7041Schristos static void
print_input_section(asection * i,bfd_boolean is_discarded)4774*56bb7041Schristos print_input_section (asection *i, bfd_boolean is_discarded)
4775*56bb7041Schristos {
4776*56bb7041Schristos   bfd_size_type size = i->size;
4777*56bb7041Schristos   int len;
4778*56bb7041Schristos   bfd_vma addr;
4779*56bb7041Schristos 
4780*56bb7041Schristos   init_opb (i);
4781*56bb7041Schristos 
4782*56bb7041Schristos   print_space ();
4783*56bb7041Schristos   minfo ("%s", i->name);
4784*56bb7041Schristos 
4785*56bb7041Schristos   len = 1 + strlen (i->name);
4786*56bb7041Schristos   if (len >= SECTION_NAME_MAP_LENGTH - 1)
4787*56bb7041Schristos     {
4788*56bb7041Schristos       print_nl ();
4789*56bb7041Schristos       len = 0;
4790*56bb7041Schristos     }
4791*56bb7041Schristos   while (len < SECTION_NAME_MAP_LENGTH)
4792*56bb7041Schristos     {
4793*56bb7041Schristos       print_space ();
4794*56bb7041Schristos       ++len;
4795*56bb7041Schristos     }
4796*56bb7041Schristos 
4797*56bb7041Schristos   if (i->output_section != NULL
4798*56bb7041Schristos       && i->output_section->owner == link_info.output_bfd)
4799*56bb7041Schristos     addr = i->output_section->vma + i->output_offset;
4800*56bb7041Schristos   else
4801*56bb7041Schristos     {
4802*56bb7041Schristos       addr = print_dot;
4803*56bb7041Schristos       if (!is_discarded)
4804*56bb7041Schristos 	size = 0;
4805*56bb7041Schristos     }
4806*56bb7041Schristos 
4807*56bb7041Schristos   minfo ("0x%V %W %pB\n", addr, TO_ADDR (size), i->owner);
4808*56bb7041Schristos 
4809*56bb7041Schristos   if (size != i->rawsize && i->rawsize != 0)
4810*56bb7041Schristos     {
4811*56bb7041Schristos       len = SECTION_NAME_MAP_LENGTH + 3;
4812*56bb7041Schristos #ifdef BFD64
4813*56bb7041Schristos       len += 16;
4814*56bb7041Schristos #else
4815*56bb7041Schristos       len += 8;
4816*56bb7041Schristos #endif
4817*56bb7041Schristos       while (len > 0)
4818*56bb7041Schristos 	{
4819*56bb7041Schristos 	  print_space ();
4820*56bb7041Schristos 	  --len;
4821*56bb7041Schristos 	}
4822*56bb7041Schristos 
4823*56bb7041Schristos       minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize));
4824*56bb7041Schristos     }
4825*56bb7041Schristos 
4826*56bb7041Schristos   if (i->output_section != NULL
4827*56bb7041Schristos       && i->output_section->owner == link_info.output_bfd)
4828*56bb7041Schristos     {
4829*56bb7041Schristos       if (link_info.reduce_memory_overheads)
4830*56bb7041Schristos 	bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i);
4831*56bb7041Schristos       else
4832*56bb7041Schristos 	print_all_symbols (i);
4833*56bb7041Schristos 
4834*56bb7041Schristos       /* Update print_dot, but make sure that we do not move it
4835*56bb7041Schristos 	 backwards - this could happen if we have overlays and a
4836*56bb7041Schristos 	 later overlay is shorter than an earier one.  */
4837*56bb7041Schristos       if (addr + TO_ADDR (size) > print_dot)
4838*56bb7041Schristos 	print_dot = addr + TO_ADDR (size);
4839*56bb7041Schristos     }
4840*56bb7041Schristos }
4841*56bb7041Schristos 
4842*56bb7041Schristos static void
print_fill_statement(lang_fill_statement_type * fill)4843*56bb7041Schristos print_fill_statement (lang_fill_statement_type *fill)
4844*56bb7041Schristos {
4845*56bb7041Schristos   size_t size;
4846*56bb7041Schristos   unsigned char *p;
4847*56bb7041Schristos   fputs (" FILL mask 0x", config.map_file);
4848*56bb7041Schristos   for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
4849*56bb7041Schristos     fprintf (config.map_file, "%02x", *p);
4850*56bb7041Schristos   fputs ("\n", config.map_file);
4851*56bb7041Schristos }
4852*56bb7041Schristos 
4853*56bb7041Schristos static void
print_data_statement(lang_data_statement_type * data)4854*56bb7041Schristos print_data_statement (lang_data_statement_type *data)
4855*56bb7041Schristos {
4856*56bb7041Schristos   int i;
4857*56bb7041Schristos   bfd_vma addr;
4858*56bb7041Schristos   bfd_size_type size;
4859*56bb7041Schristos   const char *name;
4860*56bb7041Schristos 
4861*56bb7041Schristos   init_opb (data->output_section);
4862*56bb7041Schristos   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4863*56bb7041Schristos     print_space ();
4864*56bb7041Schristos 
4865*56bb7041Schristos   addr = data->output_offset;
4866*56bb7041Schristos   if (data->output_section != NULL)
4867*56bb7041Schristos     addr += data->output_section->vma;
4868*56bb7041Schristos 
4869*56bb7041Schristos   switch (data->type)
4870*56bb7041Schristos     {
4871*56bb7041Schristos     default:
4872*56bb7041Schristos       abort ();
4873*56bb7041Schristos     case BYTE:
4874*56bb7041Schristos       size = BYTE_SIZE;
4875*56bb7041Schristos       name = "BYTE";
4876*56bb7041Schristos       break;
4877*56bb7041Schristos     case SHORT:
4878*56bb7041Schristos       size = SHORT_SIZE;
4879*56bb7041Schristos       name = "SHORT";
4880*56bb7041Schristos       break;
4881*56bb7041Schristos     case LONG:
4882*56bb7041Schristos       size = LONG_SIZE;
4883*56bb7041Schristos       name = "LONG";
4884*56bb7041Schristos       break;
4885*56bb7041Schristos     case QUAD:
4886*56bb7041Schristos       size = QUAD_SIZE;
4887*56bb7041Schristos       name = "QUAD";
4888*56bb7041Schristos       break;
4889*56bb7041Schristos     case SQUAD:
4890*56bb7041Schristos       size = QUAD_SIZE;
4891*56bb7041Schristos       name = "SQUAD";
4892*56bb7041Schristos       break;
4893*56bb7041Schristos     }
4894*56bb7041Schristos 
4895*56bb7041Schristos   if (size < TO_SIZE ((unsigned) 1))
4896*56bb7041Schristos     size = TO_SIZE ((unsigned) 1);
4897*56bb7041Schristos   minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value);
4898*56bb7041Schristos 
4899*56bb7041Schristos   if (data->exp->type.node_class != etree_value)
4900*56bb7041Schristos     {
4901*56bb7041Schristos       print_space ();
4902*56bb7041Schristos       exp_print_tree (data->exp);
4903*56bb7041Schristos     }
4904*56bb7041Schristos 
4905*56bb7041Schristos   print_nl ();
4906*56bb7041Schristos 
4907*56bb7041Schristos   print_dot = addr + TO_ADDR (size);
4908*56bb7041Schristos }
4909*56bb7041Schristos 
4910*56bb7041Schristos /* Print an address statement.  These are generated by options like
4911*56bb7041Schristos    -Ttext.  */
4912*56bb7041Schristos 
4913*56bb7041Schristos static void
print_address_statement(lang_address_statement_type * address)4914*56bb7041Schristos print_address_statement (lang_address_statement_type *address)
4915*56bb7041Schristos {
4916*56bb7041Schristos   minfo (_("Address of section %s set to "), address->section_name);
4917*56bb7041Schristos   exp_print_tree (address->address);
4918*56bb7041Schristos   print_nl ();
4919*56bb7041Schristos }
4920*56bb7041Schristos 
4921*56bb7041Schristos /* Print a reloc statement.  */
4922*56bb7041Schristos 
4923*56bb7041Schristos static void
print_reloc_statement(lang_reloc_statement_type * reloc)4924*56bb7041Schristos print_reloc_statement (lang_reloc_statement_type *reloc)
4925*56bb7041Schristos {
4926*56bb7041Schristos   int i;
4927*56bb7041Schristos   bfd_vma addr;
4928*56bb7041Schristos   bfd_size_type size;
4929*56bb7041Schristos 
4930*56bb7041Schristos   init_opb (reloc->output_section);
4931*56bb7041Schristos   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4932*56bb7041Schristos     print_space ();
4933*56bb7041Schristos 
4934*56bb7041Schristos   addr = reloc->output_offset;
4935*56bb7041Schristos   if (reloc->output_section != NULL)
4936*56bb7041Schristos     addr += reloc->output_section->vma;
4937*56bb7041Schristos 
4938*56bb7041Schristos   size = bfd_get_reloc_size (reloc->howto);
4939*56bb7041Schristos 
4940*56bb7041Schristos   minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name);
4941*56bb7041Schristos 
4942*56bb7041Schristos   if (reloc->name != NULL)
4943*56bb7041Schristos     minfo ("%s+", reloc->name);
4944*56bb7041Schristos   else
4945*56bb7041Schristos     minfo ("%s+", reloc->section->name);
4946*56bb7041Schristos 
4947*56bb7041Schristos   exp_print_tree (reloc->addend_exp);
4948*56bb7041Schristos 
4949*56bb7041Schristos   print_nl ();
4950*56bb7041Schristos 
4951*56bb7041Schristos   print_dot = addr + TO_ADDR (size);
4952*56bb7041Schristos }
4953*56bb7041Schristos 
4954*56bb7041Schristos static void
print_padding_statement(lang_padding_statement_type * s)4955*56bb7041Schristos print_padding_statement (lang_padding_statement_type *s)
4956*56bb7041Schristos {
4957*56bb7041Schristos   int len;
4958*56bb7041Schristos   bfd_vma addr;
4959*56bb7041Schristos 
4960*56bb7041Schristos   init_opb (s->output_section);
4961*56bb7041Schristos   minfo (" *fill*");
4962*56bb7041Schristos 
4963*56bb7041Schristos   len = sizeof " *fill*" - 1;
4964*56bb7041Schristos   while (len < SECTION_NAME_MAP_LENGTH)
4965*56bb7041Schristos     {
4966*56bb7041Schristos       print_space ();
4967*56bb7041Schristos       ++len;
4968*56bb7041Schristos     }
4969*56bb7041Schristos 
4970*56bb7041Schristos   addr = s->output_offset;
4971*56bb7041Schristos   if (s->output_section != NULL)
4972*56bb7041Schristos     addr += s->output_section->vma;
4973*56bb7041Schristos   minfo ("0x%V %W ", addr, TO_ADDR (s->size));
4974*56bb7041Schristos 
4975*56bb7041Schristos   if (s->fill->size != 0)
4976*56bb7041Schristos     {
4977*56bb7041Schristos       size_t size;
4978*56bb7041Schristos       unsigned char *p;
4979*56bb7041Schristos       for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
4980*56bb7041Schristos 	fprintf (config.map_file, "%02x", *p);
4981*56bb7041Schristos     }
4982*56bb7041Schristos 
4983*56bb7041Schristos   print_nl ();
4984*56bb7041Schristos 
4985*56bb7041Schristos   print_dot = addr + TO_ADDR (s->size);
4986*56bb7041Schristos }
4987*56bb7041Schristos 
4988*56bb7041Schristos static void
print_wild_statement(lang_wild_statement_type * w,lang_output_section_statement_type * os)4989*56bb7041Schristos print_wild_statement (lang_wild_statement_type *w,
4990*56bb7041Schristos 		      lang_output_section_statement_type *os)
4991*56bb7041Schristos {
4992*56bb7041Schristos   struct wildcard_list *sec;
4993*56bb7041Schristos 
4994*56bb7041Schristos   print_space ();
4995*56bb7041Schristos 
4996*56bb7041Schristos   if (w->exclude_name_list)
4997*56bb7041Schristos     {
4998*56bb7041Schristos       name_list *tmp;
4999*56bb7041Schristos       minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name);
5000*56bb7041Schristos       for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next)
5001*56bb7041Schristos 	minfo (" %s", tmp->name);
5002*56bb7041Schristos       minfo (") ");
5003*56bb7041Schristos     }
5004*56bb7041Schristos 
5005*56bb7041Schristos   if (w->filenames_sorted)
5006*56bb7041Schristos     minfo ("SORT_BY_NAME(");
5007*56bb7041Schristos   if (w->filename != NULL)
5008*56bb7041Schristos     minfo ("%s", w->filename);
5009*56bb7041Schristos   else
5010*56bb7041Schristos     minfo ("*");
5011*56bb7041Schristos   if (w->filenames_sorted)
5012*56bb7041Schristos     minfo (")");
5013*56bb7041Schristos 
5014*56bb7041Schristos   minfo ("(");
5015*56bb7041Schristos   for (sec = w->section_list; sec; sec = sec->next)
5016*56bb7041Schristos     {
5017*56bb7041Schristos       int closing_paren = 0;
5018*56bb7041Schristos 
5019*56bb7041Schristos       switch (sec->spec.sorted)
5020*56bb7041Schristos 	{
5021*56bb7041Schristos 	case none:
5022*56bb7041Schristos 	  break;
5023*56bb7041Schristos 
5024*56bb7041Schristos 	case by_name:
5025*56bb7041Schristos 	  minfo ("SORT_BY_NAME(");
5026*56bb7041Schristos 	  closing_paren = 1;
5027*56bb7041Schristos 	  break;
5028*56bb7041Schristos 
5029*56bb7041Schristos 	case by_alignment:
5030*56bb7041Schristos 	  minfo ("SORT_BY_ALIGNMENT(");
5031*56bb7041Schristos 	  closing_paren = 1;
5032*56bb7041Schristos 	  break;
5033*56bb7041Schristos 
5034*56bb7041Schristos 	case by_name_alignment:
5035*56bb7041Schristos 	  minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5036*56bb7041Schristos 	  closing_paren = 2;
5037*56bb7041Schristos 	  break;
5038*56bb7041Schristos 
5039*56bb7041Schristos 	case by_alignment_name:
5040*56bb7041Schristos 	  minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5041*56bb7041Schristos 	  closing_paren = 2;
5042*56bb7041Schristos 	  break;
5043*56bb7041Schristos 
5044*56bb7041Schristos 	case by_none:
5045*56bb7041Schristos 	  minfo ("SORT_NONE(");
5046*56bb7041Schristos 	  closing_paren = 1;
5047*56bb7041Schristos 	  break;
5048*56bb7041Schristos 
5049*56bb7041Schristos 	case by_init_priority:
5050*56bb7041Schristos 	  minfo ("SORT_BY_INIT_PRIORITY(");
5051*56bb7041Schristos 	  closing_paren = 1;
5052*56bb7041Schristos 	  break;
5053*56bb7041Schristos 	}
5054*56bb7041Schristos 
5055*56bb7041Schristos       if (sec->spec.exclude_name_list != NULL)
5056*56bb7041Schristos 	{
5057*56bb7041Schristos 	  name_list *tmp;
5058*56bb7041Schristos 	  minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
5059*56bb7041Schristos 	  for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
5060*56bb7041Schristos 	    minfo (" %s", tmp->name);
5061*56bb7041Schristos 	  minfo (") ");
5062*56bb7041Schristos 	}
5063*56bb7041Schristos       if (sec->spec.name != NULL)
5064*56bb7041Schristos 	minfo ("%s", sec->spec.name);
5065*56bb7041Schristos       else
5066*56bb7041Schristos 	minfo ("*");
5067*56bb7041Schristos       for (;closing_paren > 0; closing_paren--)
5068*56bb7041Schristos 	minfo (")");
5069*56bb7041Schristos       if (sec->next)
5070*56bb7041Schristos 	minfo (" ");
5071*56bb7041Schristos     }
5072*56bb7041Schristos   minfo (")");
5073*56bb7041Schristos 
5074*56bb7041Schristos   print_nl ();
5075*56bb7041Schristos 
5076*56bb7041Schristos   print_statement_list (w->children.head, os);
5077*56bb7041Schristos }
5078*56bb7041Schristos 
5079*56bb7041Schristos /* Print a group statement.  */
5080*56bb7041Schristos 
5081*56bb7041Schristos static void
print_group(lang_group_statement_type * s,lang_output_section_statement_type * os)5082*56bb7041Schristos print_group (lang_group_statement_type *s,
5083*56bb7041Schristos 	     lang_output_section_statement_type *os)
5084*56bb7041Schristos {
5085*56bb7041Schristos   fprintf (config.map_file, "START GROUP\n");
5086*56bb7041Schristos   print_statement_list (s->children.head, os);
5087*56bb7041Schristos   fprintf (config.map_file, "END GROUP\n");
5088*56bb7041Schristos }
5089*56bb7041Schristos 
5090*56bb7041Schristos /* Print the list of statements in S.
5091*56bb7041Schristos    This can be called for any statement type.  */
5092*56bb7041Schristos 
5093*56bb7041Schristos static void
print_statement_list(lang_statement_union_type * s,lang_output_section_statement_type * os)5094*56bb7041Schristos print_statement_list (lang_statement_union_type *s,
5095*56bb7041Schristos 		      lang_output_section_statement_type *os)
5096*56bb7041Schristos {
5097*56bb7041Schristos   while (s != NULL)
5098*56bb7041Schristos     {
5099*56bb7041Schristos       print_statement (s, os);
5100*56bb7041Schristos       s = s->header.next;
5101*56bb7041Schristos     }
5102*56bb7041Schristos }
5103*56bb7041Schristos 
5104*56bb7041Schristos /* Print the first statement in statement list S.
5105*56bb7041Schristos    This can be called for any statement type.  */
5106*56bb7041Schristos 
5107*56bb7041Schristos static void
print_statement(lang_statement_union_type * s,lang_output_section_statement_type * os)5108*56bb7041Schristos print_statement (lang_statement_union_type *s,
5109*56bb7041Schristos 		 lang_output_section_statement_type *os)
5110*56bb7041Schristos {
5111*56bb7041Schristos   switch (s->header.type)
5112*56bb7041Schristos     {
5113*56bb7041Schristos     default:
5114*56bb7041Schristos       fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
5115*56bb7041Schristos       FAIL ();
5116*56bb7041Schristos       break;
5117*56bb7041Schristos     case lang_constructors_statement_enum:
5118*56bb7041Schristos       if (constructor_list.head != NULL)
5119*56bb7041Schristos 	{
5120*56bb7041Schristos 	  if (constructors_sorted)
5121*56bb7041Schristos 	    minfo (" SORT (CONSTRUCTORS)\n");
5122*56bb7041Schristos 	  else
5123*56bb7041Schristos 	    minfo (" CONSTRUCTORS\n");
5124*56bb7041Schristos 	  print_statement_list (constructor_list.head, os);
5125*56bb7041Schristos 	}
5126*56bb7041Schristos       break;
5127*56bb7041Schristos     case lang_wild_statement_enum:
5128*56bb7041Schristos       print_wild_statement (&s->wild_statement, os);
5129*56bb7041Schristos       break;
5130*56bb7041Schristos     case lang_address_statement_enum:
5131*56bb7041Schristos       print_address_statement (&s->address_statement);
5132*56bb7041Schristos       break;
5133*56bb7041Schristos     case lang_object_symbols_statement_enum:
5134*56bb7041Schristos       minfo (" CREATE_OBJECT_SYMBOLS\n");
5135*56bb7041Schristos       break;
5136*56bb7041Schristos     case lang_fill_statement_enum:
5137*56bb7041Schristos       print_fill_statement (&s->fill_statement);
5138*56bb7041Schristos       break;
5139*56bb7041Schristos     case lang_data_statement_enum:
5140*56bb7041Schristos       print_data_statement (&s->data_statement);
5141*56bb7041Schristos       break;
5142*56bb7041Schristos     case lang_reloc_statement_enum:
5143*56bb7041Schristos       print_reloc_statement (&s->reloc_statement);
5144*56bb7041Schristos       break;
5145*56bb7041Schristos     case lang_input_section_enum:
5146*56bb7041Schristos       print_input_section (s->input_section.section, FALSE);
5147*56bb7041Schristos       break;
5148*56bb7041Schristos     case lang_padding_statement_enum:
5149*56bb7041Schristos       print_padding_statement (&s->padding_statement);
5150*56bb7041Schristos       break;
5151*56bb7041Schristos     case lang_output_section_statement_enum:
5152*56bb7041Schristos       print_output_section_statement (&s->output_section_statement);
5153*56bb7041Schristos       break;
5154*56bb7041Schristos     case lang_assignment_statement_enum:
5155*56bb7041Schristos       print_assignment (&s->assignment_statement, os);
5156*56bb7041Schristos       break;
5157*56bb7041Schristos     case lang_target_statement_enum:
5158*56bb7041Schristos       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
5159*56bb7041Schristos       break;
5160*56bb7041Schristos     case lang_output_statement_enum:
5161*56bb7041Schristos       minfo ("OUTPUT(%s", s->output_statement.name);
5162*56bb7041Schristos       if (output_target != NULL)
5163*56bb7041Schristos 	minfo (" %s", output_target);
5164*56bb7041Schristos       minfo (")\n");
5165*56bb7041Schristos       break;
5166*56bb7041Schristos     case lang_input_statement_enum:
5167*56bb7041Schristos       print_input_statement (&s->input_statement);
5168*56bb7041Schristos       break;
5169*56bb7041Schristos     case lang_group_statement_enum:
5170*56bb7041Schristos       print_group (&s->group_statement, os);
5171*56bb7041Schristos       break;
5172*56bb7041Schristos     case lang_insert_statement_enum:
5173*56bb7041Schristos       minfo ("INSERT %s %s\n",
5174*56bb7041Schristos 	     s->insert_statement.is_before ? "BEFORE" : "AFTER",
5175*56bb7041Schristos 	     s->insert_statement.where);
5176*56bb7041Schristos       break;
5177*56bb7041Schristos     }
5178*56bb7041Schristos }
5179*56bb7041Schristos 
5180*56bb7041Schristos static void
print_statements(void)5181*56bb7041Schristos print_statements (void)
5182*56bb7041Schristos {
5183*56bb7041Schristos   print_statement_list (statement_list.head, abs_output_section);
5184*56bb7041Schristos }
5185*56bb7041Schristos 
5186*56bb7041Schristos /* Print the first N statements in statement list S to STDERR.
5187*56bb7041Schristos    If N == 0, nothing is printed.
5188*56bb7041Schristos    If N < 0, the entire list is printed.
5189*56bb7041Schristos    Intended to be called from GDB.  */
5190*56bb7041Schristos 
5191*56bb7041Schristos void
dprint_statement(lang_statement_union_type * s,int n)5192*56bb7041Schristos dprint_statement (lang_statement_union_type *s, int n)
5193*56bb7041Schristos {
5194*56bb7041Schristos   FILE *map_save = config.map_file;
5195*56bb7041Schristos 
5196*56bb7041Schristos   config.map_file = stderr;
5197*56bb7041Schristos 
5198*56bb7041Schristos   if (n < 0)
5199*56bb7041Schristos     print_statement_list (s, abs_output_section);
5200*56bb7041Schristos   else
5201*56bb7041Schristos     {
5202*56bb7041Schristos       while (s && --n >= 0)
5203*56bb7041Schristos 	{
5204*56bb7041Schristos 	  print_statement (s, abs_output_section);
5205*56bb7041Schristos 	  s = s->header.next;
5206*56bb7041Schristos 	}
5207*56bb7041Schristos     }
5208*56bb7041Schristos 
5209*56bb7041Schristos   config.map_file = map_save;
5210*56bb7041Schristos }
5211*56bb7041Schristos 
5212*56bb7041Schristos static void
insert_pad(lang_statement_union_type ** ptr,fill_type * fill,bfd_size_type alignment_needed,asection * output_section,bfd_vma dot)5213*56bb7041Schristos insert_pad (lang_statement_union_type **ptr,
5214*56bb7041Schristos 	    fill_type *fill,
5215*56bb7041Schristos 	    bfd_size_type alignment_needed,
5216*56bb7041Schristos 	    asection *output_section,
5217*56bb7041Schristos 	    bfd_vma dot)
5218*56bb7041Schristos {
5219*56bb7041Schristos   static fill_type zero_fill;
5220*56bb7041Schristos   lang_statement_union_type *pad = NULL;
5221*56bb7041Schristos 
5222*56bb7041Schristos   if (ptr != &statement_list.head)
5223*56bb7041Schristos     pad = ((lang_statement_union_type *)
5224*56bb7041Schristos 	   ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
5225*56bb7041Schristos   if (pad != NULL
5226*56bb7041Schristos       && pad->header.type == lang_padding_statement_enum
5227*56bb7041Schristos       && pad->padding_statement.output_section == output_section)
5228*56bb7041Schristos     {
5229*56bb7041Schristos       /* Use the existing pad statement.  */
5230*56bb7041Schristos     }
5231*56bb7041Schristos   else if ((pad = *ptr) != NULL
5232*56bb7041Schristos 	   && pad->header.type == lang_padding_statement_enum
5233*56bb7041Schristos 	   && pad->padding_statement.output_section == output_section)
5234*56bb7041Schristos     {
5235*56bb7041Schristos       /* Use the existing pad statement.  */
5236*56bb7041Schristos     }
5237*56bb7041Schristos   else
5238*56bb7041Schristos     {
5239*56bb7041Schristos       /* Make a new padding statement, linked into existing chain.  */
5240*56bb7041Schristos       pad = stat_alloc (sizeof (lang_padding_statement_type));
5241*56bb7041Schristos       pad->header.next = *ptr;
5242*56bb7041Schristos       *ptr = pad;
5243*56bb7041Schristos       pad->header.type = lang_padding_statement_enum;
5244*56bb7041Schristos       pad->padding_statement.output_section = output_section;
5245*56bb7041Schristos       if (fill == NULL)
5246*56bb7041Schristos 	fill = &zero_fill;
5247*56bb7041Schristos       pad->padding_statement.fill = fill;
5248*56bb7041Schristos     }
5249*56bb7041Schristos   pad->padding_statement.output_offset = dot - output_section->vma;
5250*56bb7041Schristos   pad->padding_statement.size = alignment_needed;
5251*56bb7041Schristos   if (!(output_section->flags & SEC_FIXED_SIZE))
5252*56bb7041Schristos     output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed)
5253*56bb7041Schristos 				    - output_section->vma);
5254*56bb7041Schristos }
5255*56bb7041Schristos 
5256*56bb7041Schristos /* Work out how much this section will move the dot point.  */
5257*56bb7041Schristos 
5258*56bb7041Schristos static bfd_vma
size_input_section(lang_statement_union_type ** this_ptr,lang_output_section_statement_type * output_section_statement,fill_type * fill,bfd_boolean * removed,bfd_vma dot)5259*56bb7041Schristos size_input_section
5260*56bb7041Schristos   (lang_statement_union_type **this_ptr,
5261*56bb7041Schristos    lang_output_section_statement_type *output_section_statement,
5262*56bb7041Schristos    fill_type *fill,
5263*56bb7041Schristos    bfd_boolean *removed,
5264*56bb7041Schristos    bfd_vma dot)
5265*56bb7041Schristos {
5266*56bb7041Schristos   lang_input_section_type *is = &((*this_ptr)->input_section);
5267*56bb7041Schristos   asection *i = is->section;
5268*56bb7041Schristos   asection *o = output_section_statement->bfd_section;
5269*56bb7041Schristos   *removed = 0;
5270*56bb7041Schristos 
5271*56bb7041Schristos   if (link_info.non_contiguous_regions)
5272*56bb7041Schristos     {
5273*56bb7041Schristos       /* If the input section I has already been successfully assigned
5274*56bb7041Schristos 	 to an output section other than O, don't bother with it and
5275*56bb7041Schristos 	 let the caller remove it from the list.  Keep processing in
5276*56bb7041Schristos 	 case we have already handled O, because the repeated passes
5277*56bb7041Schristos 	 have reinitialized its size.  */
5278*56bb7041Schristos       if (i->already_assigned && i->already_assigned != o)
5279*56bb7041Schristos 	{
5280*56bb7041Schristos 	  *removed = 1;
5281*56bb7041Schristos 	  return dot;
5282*56bb7041Schristos 	}
5283*56bb7041Schristos     }
5284*56bb7041Schristos 
5285*56bb7041Schristos   if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5286*56bb7041Schristos     i->output_offset = i->vma - o->vma;
5287*56bb7041Schristos   else if (((i->flags & SEC_EXCLUDE) != 0)
5288*56bb7041Schristos 	   || output_section_statement->ignored)
5289*56bb7041Schristos     i->output_offset = dot - o->vma;
5290*56bb7041Schristos   else
5291*56bb7041Schristos     {
5292*56bb7041Schristos       bfd_size_type alignment_needed;
5293*56bb7041Schristos 
5294*56bb7041Schristos       /* Align this section first to the input sections requirement,
5295*56bb7041Schristos 	 then to the output section's requirement.  If this alignment
5296*56bb7041Schristos 	 is greater than any seen before, then record it too.  Perform
5297*56bb7041Schristos 	 the alignment by inserting a magic 'padding' statement.  */
5298*56bb7041Schristos 
5299*56bb7041Schristos       if (output_section_statement->subsection_alignment != NULL)
5300*56bb7041Schristos 	i->alignment_power
5301*56bb7041Schristos 	  = exp_get_power (output_section_statement->subsection_alignment,
5302*56bb7041Schristos 			   "subsection alignment");
5303*56bb7041Schristos 
5304*56bb7041Schristos       if (o->alignment_power < i->alignment_power)
5305*56bb7041Schristos 	o->alignment_power = i->alignment_power;
5306*56bb7041Schristos 
5307*56bb7041Schristos       alignment_needed = align_power (dot, i->alignment_power) - dot;
5308*56bb7041Schristos 
5309*56bb7041Schristos       if (alignment_needed != 0)
5310*56bb7041Schristos 	{
5311*56bb7041Schristos 	  insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
5312*56bb7041Schristos 	  dot += alignment_needed;
5313*56bb7041Schristos 	}
5314*56bb7041Schristos 
5315*56bb7041Schristos       if (link_info.non_contiguous_regions)
5316*56bb7041Schristos 	{
5317*56bb7041Schristos 	  /* If I would overflow O, let the caller remove I from the
5318*56bb7041Schristos 	     list.  */
5319*56bb7041Schristos 	  if (output_section_statement->region)
5320*56bb7041Schristos 	    {
5321*56bb7041Schristos 	      bfd_vma end = output_section_statement->region->origin
5322*56bb7041Schristos 		+ output_section_statement->region->length;
5323*56bb7041Schristos 
5324*56bb7041Schristos 	      if (dot + TO_ADDR (i->size) > end)
5325*56bb7041Schristos 		{
5326*56bb7041Schristos 		  if (i->flags & SEC_LINKER_CREATED)
5327*56bb7041Schristos 		    einfo (_("%F%P: Output section '%s' not large enough for the "
5328*56bb7041Schristos 			     "linker-created stubs section '%s'.\n"),
5329*56bb7041Schristos 			   i->output_section->name, i->name);
5330*56bb7041Schristos 
5331*56bb7041Schristos 		  if (i->rawsize && i->rawsize != i->size)
5332*56bb7041Schristos 		    einfo (_("%F%P: Relaxation not supported with "
5333*56bb7041Schristos 			     "--enable-non-contiguous-regions (section '%s' "
5334*56bb7041Schristos 			     "would overflow '%s' after it changed size).\n"),
5335*56bb7041Schristos 			   i->name, i->output_section->name);
5336*56bb7041Schristos 
5337*56bb7041Schristos 		  *removed = 1;
5338*56bb7041Schristos 		  dot = end;
5339*56bb7041Schristos 		  i->output_section = NULL;
5340*56bb7041Schristos 		  return dot;
5341*56bb7041Schristos 		}
5342*56bb7041Schristos 	    }
5343*56bb7041Schristos 	}
5344*56bb7041Schristos 
5345*56bb7041Schristos       /* Remember where in the output section this input section goes.  */
5346*56bb7041Schristos       i->output_offset = dot - o->vma;
5347*56bb7041Schristos 
5348*56bb7041Schristos       /* Mark how big the output section must be to contain this now.  */
5349*56bb7041Schristos       dot += TO_ADDR (i->size);
5350*56bb7041Schristos       if (!(o->flags & SEC_FIXED_SIZE))
5351*56bb7041Schristos 	o->size = TO_SIZE (dot - o->vma);
5352*56bb7041Schristos 
5353*56bb7041Schristos       if (link_info.non_contiguous_regions)
5354*56bb7041Schristos 	{
5355*56bb7041Schristos 	  /* Record that I was successfully assigned to O, and update
5356*56bb7041Schristos 	     its actual output section too.  */
5357*56bb7041Schristos 	  i->already_assigned = o;
5358*56bb7041Schristos 	  i->output_section = o;
5359*56bb7041Schristos 	}
5360*56bb7041Schristos     }
5361*56bb7041Schristos 
5362*56bb7041Schristos   return dot;
5363*56bb7041Schristos }
5364*56bb7041Schristos 
5365*56bb7041Schristos struct check_sec
5366*56bb7041Schristos {
5367*56bb7041Schristos   asection *sec;
5368*56bb7041Schristos   bfd_boolean warned;
5369*56bb7041Schristos };
5370*56bb7041Schristos 
5371*56bb7041Schristos static int
sort_sections_by_lma(const void * arg1,const void * arg2)5372*56bb7041Schristos sort_sections_by_lma (const void *arg1, const void *arg2)
5373*56bb7041Schristos {
5374*56bb7041Schristos   const asection *sec1 = ((const struct check_sec *) arg1)->sec;
5375*56bb7041Schristos   const asection *sec2 = ((const struct check_sec *) arg2)->sec;
5376*56bb7041Schristos 
5377*56bb7041Schristos   if (sec1->lma < sec2->lma)
5378*56bb7041Schristos     return -1;
5379*56bb7041Schristos   else if (sec1->lma > sec2->lma)
5380*56bb7041Schristos     return 1;
5381*56bb7041Schristos   else if (sec1->id < sec2->id)
5382*56bb7041Schristos     return -1;
5383*56bb7041Schristos   else if (sec1->id > sec2->id)
5384*56bb7041Schristos     return 1;
5385*56bb7041Schristos 
5386*56bb7041Schristos   return 0;
5387*56bb7041Schristos }
5388*56bb7041Schristos 
5389*56bb7041Schristos static int
sort_sections_by_vma(const void * arg1,const void * arg2)5390*56bb7041Schristos sort_sections_by_vma (const void *arg1, const void *arg2)
5391*56bb7041Schristos {
5392*56bb7041Schristos   const asection *sec1 = ((const struct check_sec *) arg1)->sec;
5393*56bb7041Schristos   const asection *sec2 = ((const struct check_sec *) arg2)->sec;
5394*56bb7041Schristos 
5395*56bb7041Schristos   if (sec1->vma < sec2->vma)
5396*56bb7041Schristos     return -1;
5397*56bb7041Schristos   else if (sec1->vma > sec2->vma)
5398*56bb7041Schristos     return 1;
5399*56bb7041Schristos   else if (sec1->id < sec2->id)
5400*56bb7041Schristos     return -1;
5401*56bb7041Schristos   else if (sec1->id > sec2->id)
5402*56bb7041Schristos     return 1;
5403*56bb7041Schristos 
5404*56bb7041Schristos   return 0;
5405*56bb7041Schristos }
5406*56bb7041Schristos 
5407*56bb7041Schristos #define IS_TBSS(s) \
5408*56bb7041Schristos   ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5409*56bb7041Schristos 
5410*56bb7041Schristos #define IGNORE_SECTION(s) \
5411*56bb7041Schristos   ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5412*56bb7041Schristos 
5413*56bb7041Schristos /* Check to see if any allocated sections overlap with other allocated
5414*56bb7041Schristos    sections.  This can happen if a linker script specifies the output
5415*56bb7041Schristos    section addresses of the two sections.  Also check whether any memory
5416*56bb7041Schristos    region has overflowed.  */
5417*56bb7041Schristos 
5418*56bb7041Schristos static void
lang_check_section_addresses(void)5419*56bb7041Schristos lang_check_section_addresses (void)
5420*56bb7041Schristos {
5421*56bb7041Schristos   asection *s, *p;
5422*56bb7041Schristos   struct check_sec *sections;
5423*56bb7041Schristos   size_t i, count;
5424*56bb7041Schristos   bfd_vma addr_mask;
5425*56bb7041Schristos   bfd_vma s_start;
5426*56bb7041Schristos   bfd_vma s_end;
5427*56bb7041Schristos   bfd_vma p_start = 0;
5428*56bb7041Schristos   bfd_vma p_end = 0;
5429*56bb7041Schristos   lang_memory_region_type *m;
5430*56bb7041Schristos   bfd_boolean overlays;
5431*56bb7041Schristos 
5432*56bb7041Schristos   /* Detect address space overflow on allocated sections.  */
5433*56bb7041Schristos   addr_mask = ((bfd_vma) 1 <<
5434*56bb7041Schristos 	       (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1;
5435*56bb7041Schristos   addr_mask = (addr_mask << 1) + 1;
5436*56bb7041Schristos   for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5437*56bb7041Schristos     if ((s->flags & SEC_ALLOC) != 0)
5438*56bb7041Schristos       {
5439*56bb7041Schristos 	s_end = (s->vma + s->size) & addr_mask;
5440*56bb7041Schristos 	if (s_end != 0 && s_end < (s->vma & addr_mask))
5441*56bb7041Schristos 	  einfo (_("%X%P: section %s VMA wraps around address space\n"),
5442*56bb7041Schristos 		 s->name);
5443*56bb7041Schristos 	else
5444*56bb7041Schristos 	  {
5445*56bb7041Schristos 	    s_end = (s->lma + s->size) & addr_mask;
5446*56bb7041Schristos 	    if (s_end != 0 && s_end < (s->lma & addr_mask))
5447*56bb7041Schristos 	      einfo (_("%X%P: section %s LMA wraps around address space\n"),
5448*56bb7041Schristos 		     s->name);
5449*56bb7041Schristos 	  }
5450*56bb7041Schristos       }
5451*56bb7041Schristos 
5452*56bb7041Schristos   if (bfd_count_sections (link_info.output_bfd) <= 1)
5453*56bb7041Schristos     return;
5454*56bb7041Schristos 
5455*56bb7041Schristos   count = bfd_count_sections (link_info.output_bfd);
5456*56bb7041Schristos   sections = XNEWVEC (struct check_sec, count);
5457*56bb7041Schristos 
5458*56bb7041Schristos   /* Scan all sections in the output list.  */
5459*56bb7041Schristos   count = 0;
5460*56bb7041Schristos   for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5461*56bb7041Schristos     {
5462*56bb7041Schristos       if (IGNORE_SECTION (s)
5463*56bb7041Schristos 	  || s->size == 0)
5464*56bb7041Schristos 	continue;
5465*56bb7041Schristos 
5466*56bb7041Schristos       sections[count].sec = s;
5467*56bb7041Schristos       sections[count].warned = FALSE;
5468*56bb7041Schristos       count++;
5469*56bb7041Schristos     }
5470*56bb7041Schristos 
5471*56bb7041Schristos   if (count <= 1)
5472*56bb7041Schristos     {
5473*56bb7041Schristos       free (sections);
5474*56bb7041Schristos       return;
5475*56bb7041Schristos     }
5476*56bb7041Schristos 
5477*56bb7041Schristos   qsort (sections, count, sizeof (*sections), sort_sections_by_lma);
5478*56bb7041Schristos 
5479*56bb7041Schristos   /* First check section LMAs.  There should be no overlap of LMAs on
5480*56bb7041Schristos      loadable sections, even with overlays.  */
5481*56bb7041Schristos   for (p = NULL, i = 0; i < count; i++)
5482*56bb7041Schristos     {
5483*56bb7041Schristos       s = sections[i].sec;
5484*56bb7041Schristos       init_opb (s);
5485*56bb7041Schristos       if ((s->flags & SEC_LOAD) != 0)
5486*56bb7041Schristos 	{
5487*56bb7041Schristos 	  s_start = s->lma;
5488*56bb7041Schristos 	  s_end = s_start + TO_ADDR (s->size) - 1;
5489*56bb7041Schristos 
5490*56bb7041Schristos 	  /* Look for an overlap.  We have sorted sections by lma, so
5491*56bb7041Schristos 	     we know that s_start >= p_start.  Besides the obvious
5492*56bb7041Schristos 	     case of overlap when the current section starts before
5493*56bb7041Schristos 	     the previous one ends, we also must have overlap if the
5494*56bb7041Schristos 	     previous section wraps around the address space.  */
5495*56bb7041Schristos 	  if (p != NULL
5496*56bb7041Schristos 	      && (s_start <= p_end
5497*56bb7041Schristos 		  || p_end < p_start))
5498*56bb7041Schristos 	    {
5499*56bb7041Schristos 	      einfo (_("%X%P: section %s LMA [%V,%V]"
5500*56bb7041Schristos 		       " overlaps section %s LMA [%V,%V]\n"),
5501*56bb7041Schristos 		     s->name, s_start, s_end, p->name, p_start, p_end);
5502*56bb7041Schristos 	      sections[i].warned = TRUE;
5503*56bb7041Schristos 	    }
5504*56bb7041Schristos 	  p = s;
5505*56bb7041Schristos 	  p_start = s_start;
5506*56bb7041Schristos 	  p_end = s_end;
5507*56bb7041Schristos 	}
5508*56bb7041Schristos     }
5509*56bb7041Schristos 
5510*56bb7041Schristos   /* If any non-zero size allocated section (excluding tbss) starts at
5511*56bb7041Schristos      exactly the same VMA as another such section, then we have
5512*56bb7041Schristos      overlays.  Overlays generated by the OVERLAY keyword will have
5513*56bb7041Schristos      this property.  It is possible to intentionally generate overlays
5514*56bb7041Schristos      that fail this test, but it would be unusual.  */
5515*56bb7041Schristos   qsort (sections, count, sizeof (*sections), sort_sections_by_vma);
5516*56bb7041Schristos   overlays = FALSE;
5517*56bb7041Schristos   p_start = sections[0].sec->vma;
5518*56bb7041Schristos   for (i = 1; i < count; i++)
5519*56bb7041Schristos     {
5520*56bb7041Schristos       s_start = sections[i].sec->vma;
5521*56bb7041Schristos       if (p_start == s_start)
5522*56bb7041Schristos 	{
5523*56bb7041Schristos 	  overlays = TRUE;
5524*56bb7041Schristos 	  break;
5525*56bb7041Schristos 	}
5526*56bb7041Schristos       p_start = s_start;
5527*56bb7041Schristos     }
5528*56bb7041Schristos 
5529*56bb7041Schristos   /* Now check section VMAs if no overlays were detected.  */
5530*56bb7041Schristos   if (!overlays)
5531*56bb7041Schristos     {
5532*56bb7041Schristos       for (p = NULL, i = 0; i < count; i++)
5533*56bb7041Schristos 	{
5534*56bb7041Schristos 	  s = sections[i].sec;
5535*56bb7041Schristos 	  init_opb (s);
5536*56bb7041Schristos 	  s_start = s->vma;
5537*56bb7041Schristos 	  s_end = s_start + TO_ADDR (s->size) - 1;
5538*56bb7041Schristos 
5539*56bb7041Schristos 	  if (p != NULL
5540*56bb7041Schristos 	      && !sections[i].warned
5541*56bb7041Schristos 	      && (s_start <= p_end
5542*56bb7041Schristos 		  || p_end < p_start))
5543*56bb7041Schristos 	    einfo (_("%X%P: section %s VMA [%V,%V]"
5544*56bb7041Schristos 		     " overlaps section %s VMA [%V,%V]\n"),
5545*56bb7041Schristos 		   s->name, s_start, s_end, p->name, p_start, p_end);
5546*56bb7041Schristos 	  p = s;
5547*56bb7041Schristos 	  p_start = s_start;
5548*56bb7041Schristos 	  p_end = s_end;
5549*56bb7041Schristos 	}
5550*56bb7041Schristos     }
5551*56bb7041Schristos 
5552*56bb7041Schristos   free (sections);
5553*56bb7041Schristos 
5554*56bb7041Schristos   /* If any memory region has overflowed, report by how much.
5555*56bb7041Schristos      We do not issue this diagnostic for regions that had sections
5556*56bb7041Schristos      explicitly placed outside their bounds; os_region_check's
5557*56bb7041Schristos      diagnostics are adequate for that case.
5558*56bb7041Schristos 
5559*56bb7041Schristos      FIXME: It is conceivable that m->current - (m->origin + m->length)
5560*56bb7041Schristos      might overflow a 32-bit integer.  There is, alas, no way to print
5561*56bb7041Schristos      a bfd_vma quantity in decimal.  */
5562*56bb7041Schristos   for (m = lang_memory_region_list; m; m = m->next)
5563*56bb7041Schristos     if (m->had_full_message)
5564*56bb7041Schristos       {
5565*56bb7041Schristos 	unsigned long over = m->current - (m->origin + m->length);
5566*56bb7041Schristos 	einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5567*56bb7041Schristos 			 "%X%P: region `%s' overflowed by %lu bytes\n",
5568*56bb7041Schristos 			 over),
5569*56bb7041Schristos 	       m->name_list.name, over);
5570*56bb7041Schristos       }
5571*56bb7041Schristos }
5572*56bb7041Schristos 
5573*56bb7041Schristos /* Make sure the new address is within the region.  We explicitly permit the
5574*56bb7041Schristos    current address to be at the exact end of the region when the address is
5575*56bb7041Schristos    non-zero, in case the region is at the end of addressable memory and the
5576*56bb7041Schristos    calculation wraps around.  */
5577*56bb7041Schristos 
5578*56bb7041Schristos static void
os_region_check(lang_output_section_statement_type * os,lang_memory_region_type * region,etree_type * tree,bfd_vma rbase)5579*56bb7041Schristos os_region_check (lang_output_section_statement_type *os,
5580*56bb7041Schristos 		 lang_memory_region_type *region,
5581*56bb7041Schristos 		 etree_type *tree,
5582*56bb7041Schristos 		 bfd_vma rbase)
5583*56bb7041Schristos {
5584*56bb7041Schristos   if ((region->current < region->origin
5585*56bb7041Schristos        || (region->current - region->origin > region->length))
5586*56bb7041Schristos       && ((region->current != region->origin + region->length)
5587*56bb7041Schristos 	  || rbase == 0))
5588*56bb7041Schristos     {
5589*56bb7041Schristos       if (tree != NULL)
5590*56bb7041Schristos 	{
5591*56bb7041Schristos 	  einfo (_("%X%P: address 0x%v of %pB section `%s'"
5592*56bb7041Schristos 		   " is not within region `%s'\n"),
5593*56bb7041Schristos 		 region->current,
5594*56bb7041Schristos 		 os->bfd_section->owner,
5595*56bb7041Schristos 		 os->bfd_section->name,
5596*56bb7041Schristos 		 region->name_list.name);
5597*56bb7041Schristos 	}
5598*56bb7041Schristos       else if (!region->had_full_message)
5599*56bb7041Schristos 	{
5600*56bb7041Schristos 	  region->had_full_message = TRUE;
5601*56bb7041Schristos 
5602*56bb7041Schristos 	  einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5603*56bb7041Schristos 		 os->bfd_section->owner,
5604*56bb7041Schristos 		 os->bfd_section->name,
5605*56bb7041Schristos 		 region->name_list.name);
5606*56bb7041Schristos 	}
5607*56bb7041Schristos     }
5608*56bb7041Schristos }
5609*56bb7041Schristos 
5610*56bb7041Schristos static void
ldlang_check_relro_region(lang_statement_union_type * s,seg_align_type * seg)5611*56bb7041Schristos ldlang_check_relro_region (lang_statement_union_type *s,
5612*56bb7041Schristos 			   seg_align_type *seg)
5613*56bb7041Schristos {
5614*56bb7041Schristos   if (seg->relro == exp_seg_relro_start)
5615*56bb7041Schristos     {
5616*56bb7041Schristos       if (!seg->relro_start_stat)
5617*56bb7041Schristos 	seg->relro_start_stat = s;
5618*56bb7041Schristos       else
5619*56bb7041Schristos 	{
5620*56bb7041Schristos 	  ASSERT (seg->relro_start_stat == s);
5621*56bb7041Schristos 	}
5622*56bb7041Schristos     }
5623*56bb7041Schristos   else if (seg->relro == exp_seg_relro_end)
5624*56bb7041Schristos     {
5625*56bb7041Schristos       if (!seg->relro_end_stat)
5626*56bb7041Schristos 	seg->relro_end_stat = s;
5627*56bb7041Schristos       else
5628*56bb7041Schristos 	{
5629*56bb7041Schristos 	  ASSERT (seg->relro_end_stat == s);
5630*56bb7041Schristos 	}
5631*56bb7041Schristos     }
5632*56bb7041Schristos }
5633*56bb7041Schristos 
5634*56bb7041Schristos /* Set the sizes for all the output sections.  */
5635*56bb7041Schristos 
5636*56bb7041Schristos static bfd_vma
lang_size_sections_1(lang_statement_union_type ** prev,lang_output_section_statement_type * output_section_statement,fill_type * fill,bfd_vma dot,bfd_boolean * relax,bfd_boolean check_regions)5637*56bb7041Schristos lang_size_sections_1
5638*56bb7041Schristos   (lang_statement_union_type **prev,
5639*56bb7041Schristos    lang_output_section_statement_type *output_section_statement,
5640*56bb7041Schristos    fill_type *fill,
5641*56bb7041Schristos    bfd_vma dot,
5642*56bb7041Schristos    bfd_boolean *relax,
5643*56bb7041Schristos    bfd_boolean check_regions)
5644*56bb7041Schristos {
5645*56bb7041Schristos   lang_statement_union_type *s;
5646*56bb7041Schristos   lang_statement_union_type *prev_s = NULL;
5647*56bb7041Schristos   bfd_boolean removed_prev_s = FALSE;
5648*56bb7041Schristos 
5649*56bb7041Schristos   /* Size up the sections from their constituent parts.  */
5650*56bb7041Schristos   for (s = *prev; s != NULL; prev_s = s, s = s->header.next)
5651*56bb7041Schristos     {
5652*56bb7041Schristos       bfd_boolean removed=FALSE;
5653*56bb7041Schristos 
5654*56bb7041Schristos       switch (s->header.type)
5655*56bb7041Schristos 	{
5656*56bb7041Schristos 	case lang_output_section_statement_enum:
5657*56bb7041Schristos 	  {
5658*56bb7041Schristos 	    bfd_vma newdot, after, dotdelta;
5659*56bb7041Schristos 	    lang_output_section_statement_type *os;
5660*56bb7041Schristos 	    lang_memory_region_type *r;
5661*56bb7041Schristos 	    int section_alignment = 0;
5662*56bb7041Schristos 
5663*56bb7041Schristos 	    os = &s->output_section_statement;
5664*56bb7041Schristos 	    init_opb (os->bfd_section);
5665*56bb7041Schristos 	    if (os->constraint == -1)
5666*56bb7041Schristos 	      break;
5667*56bb7041Schristos 
5668*56bb7041Schristos 	    /* FIXME: We shouldn't need to zero section vmas for ld -r
5669*56bb7041Schristos 	       here, in lang_insert_orphan, or in the default linker scripts.
5670*56bb7041Schristos 	       This is covering for coff backend linker bugs.  See PR6945.  */
5671*56bb7041Schristos 	    if (os->addr_tree == NULL
5672*56bb7041Schristos 		&& bfd_link_relocatable (&link_info)
5673*56bb7041Schristos 		&& (bfd_get_flavour (link_info.output_bfd)
5674*56bb7041Schristos 		    == bfd_target_coff_flavour))
5675*56bb7041Schristos 	      os->addr_tree = exp_intop (0);
5676*56bb7041Schristos 	    if (os->addr_tree != NULL)
5677*56bb7041Schristos 	      {
5678*56bb7041Schristos 		os->processed_vma = FALSE;
5679*56bb7041Schristos 		exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
5680*56bb7041Schristos 
5681*56bb7041Schristos 		if (expld.result.valid_p)
5682*56bb7041Schristos 		  {
5683*56bb7041Schristos 		    dot = expld.result.value;
5684*56bb7041Schristos 		    if (expld.result.section != NULL)
5685*56bb7041Schristos 		      dot += expld.result.section->vma;
5686*56bb7041Schristos 		  }
5687*56bb7041Schristos 		else if (expld.phase != lang_mark_phase_enum)
5688*56bb7041Schristos 		  einfo (_("%F%P:%pS: non constant or forward reference"
5689*56bb7041Schristos 			   " address expression for section %s\n"),
5690*56bb7041Schristos 			 os->addr_tree, os->name);
5691*56bb7041Schristos 	      }
5692*56bb7041Schristos 
5693*56bb7041Schristos 	    if (os->bfd_section == NULL)
5694*56bb7041Schristos 	      /* This section was removed or never actually created.  */
5695*56bb7041Schristos 	      break;
5696*56bb7041Schristos 
5697*56bb7041Schristos 	    /* If this is a COFF shared library section, use the size and
5698*56bb7041Schristos 	       address from the input section.  FIXME: This is COFF
5699*56bb7041Schristos 	       specific; it would be cleaner if there were some other way
5700*56bb7041Schristos 	       to do this, but nothing simple comes to mind.  */
5701*56bb7041Schristos 	    if (((bfd_get_flavour (link_info.output_bfd)
5702*56bb7041Schristos 		  == bfd_target_ecoff_flavour)
5703*56bb7041Schristos 		 || (bfd_get_flavour (link_info.output_bfd)
5704*56bb7041Schristos 		     == bfd_target_coff_flavour))
5705*56bb7041Schristos 		&& (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
5706*56bb7041Schristos 	      {
5707*56bb7041Schristos 		asection *input;
5708*56bb7041Schristos 
5709*56bb7041Schristos 		if (os->children.head == NULL
5710*56bb7041Schristos 		    || os->children.head->header.next != NULL
5711*56bb7041Schristos 		    || (os->children.head->header.type
5712*56bb7041Schristos 			!= lang_input_section_enum))
5713*56bb7041Schristos 		  einfo (_("%X%P: internal error on COFF shared library"
5714*56bb7041Schristos 			   " section %s\n"), os->name);
5715*56bb7041Schristos 
5716*56bb7041Schristos 		input = os->children.head->input_section.section;
5717*56bb7041Schristos 		bfd_set_section_vma (os->bfd_section,
5718*56bb7041Schristos 				     bfd_section_vma (input));
5719*56bb7041Schristos 		if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
5720*56bb7041Schristos 		  os->bfd_section->size = input->size;
5721*56bb7041Schristos 		break;
5722*56bb7041Schristos 	      }
5723*56bb7041Schristos 
5724*56bb7041Schristos 	    newdot = dot;
5725*56bb7041Schristos 	    dotdelta = 0;
5726*56bb7041Schristos 	    if (bfd_is_abs_section (os->bfd_section))
5727*56bb7041Schristos 	      {
5728*56bb7041Schristos 		/* No matter what happens, an abs section starts at zero.  */
5729*56bb7041Schristos 		ASSERT (os->bfd_section->vma == 0);
5730*56bb7041Schristos 	      }
5731*56bb7041Schristos 	    else
5732*56bb7041Schristos 	      {
5733*56bb7041Schristos 		if (os->addr_tree == NULL)
5734*56bb7041Schristos 		  {
5735*56bb7041Schristos 		    /* No address specified for this section, get one
5736*56bb7041Schristos 		       from the region specification.  */
5737*56bb7041Schristos 		    if (os->region == NULL
5738*56bb7041Schristos 			|| ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
5739*56bb7041Schristos 			    && os->region->name_list.name[0] == '*'
5740*56bb7041Schristos 			    && strcmp (os->region->name_list.name,
5741*56bb7041Schristos 				       DEFAULT_MEMORY_REGION) == 0))
5742*56bb7041Schristos 		      {
5743*56bb7041Schristos 			os->region = lang_memory_default (os->bfd_section);
5744*56bb7041Schristos 		      }
5745*56bb7041Schristos 
5746*56bb7041Schristos 		    /* If a loadable section is using the default memory
5747*56bb7041Schristos 		       region, and some non default memory regions were
5748*56bb7041Schristos 		       defined, issue an error message.  */
5749*56bb7041Schristos 		    if (!os->ignored
5750*56bb7041Schristos 			&& !IGNORE_SECTION (os->bfd_section)
5751*56bb7041Schristos 			&& !bfd_link_relocatable (&link_info)
5752*56bb7041Schristos 			&& check_regions
5753*56bb7041Schristos 			&& strcmp (os->region->name_list.name,
5754*56bb7041Schristos 				   DEFAULT_MEMORY_REGION) == 0
5755*56bb7041Schristos 			&& lang_memory_region_list != NULL
5756*56bb7041Schristos 			&& (strcmp (lang_memory_region_list->name_list.name,
5757*56bb7041Schristos 				    DEFAULT_MEMORY_REGION) != 0
5758*56bb7041Schristos 			    || lang_memory_region_list->next != NULL)
5759*56bb7041Schristos 			&& lang_sizing_iteration == 1)
5760*56bb7041Schristos 		      {
5761*56bb7041Schristos 			/* By default this is an error rather than just a
5762*56bb7041Schristos 			   warning because if we allocate the section to the
5763*56bb7041Schristos 			   default memory region we can end up creating an
5764*56bb7041Schristos 			   excessively large binary, or even seg faulting when
5765*56bb7041Schristos 			   attempting to perform a negative seek.  See
5766*56bb7041Schristos 			   sources.redhat.com/ml/binutils/2003-04/msg00423.html
5767*56bb7041Schristos 			   for an example of this.  This behaviour can be
5768*56bb7041Schristos 			   overridden by the using the --no-check-sections
5769*56bb7041Schristos 			   switch.  */
5770*56bb7041Schristos 			if (command_line.check_section_addresses)
5771*56bb7041Schristos 			  einfo (_("%F%P: error: no memory region specified"
5772*56bb7041Schristos 				   " for loadable section `%s'\n"),
5773*56bb7041Schristos 				 bfd_section_name (os->bfd_section));
5774*56bb7041Schristos 			else
5775*56bb7041Schristos 			  einfo (_("%P: warning: no memory region specified"
5776*56bb7041Schristos 				   " for loadable section `%s'\n"),
5777*56bb7041Schristos 				 bfd_section_name (os->bfd_section));
5778*56bb7041Schristos 		      }
5779*56bb7041Schristos 
5780*56bb7041Schristos 		    newdot = os->region->current;
5781*56bb7041Schristos 		    section_alignment = os->bfd_section->alignment_power;
5782*56bb7041Schristos 		  }
5783*56bb7041Schristos 		else
5784*56bb7041Schristos 		  section_alignment = exp_get_power (os->section_alignment,
5785*56bb7041Schristos 						     "section alignment");
5786*56bb7041Schristos 
5787*56bb7041Schristos 		/* Align to what the section needs.  */
5788*56bb7041Schristos 		if (section_alignment > 0)
5789*56bb7041Schristos 		  {
5790*56bb7041Schristos 		    bfd_vma savedot = newdot;
5791*56bb7041Schristos 		    bfd_vma diff = 0;
5792*56bb7041Schristos 
5793*56bb7041Schristos 		    newdot = align_power (newdot, section_alignment);
5794*56bb7041Schristos 		    dotdelta = newdot - savedot;
5795*56bb7041Schristos 
5796*56bb7041Schristos 		    if (lang_sizing_iteration == 1)
5797*56bb7041Schristos 		      diff = dotdelta;
5798*56bb7041Schristos 		    else if (lang_sizing_iteration > 1)
5799*56bb7041Schristos 		      {
5800*56bb7041Schristos 			/* Only report adjustments that would change
5801*56bb7041Schristos 			   alignment from what we have already reported.  */
5802*56bb7041Schristos 			diff = newdot - os->bfd_section->vma;
5803*56bb7041Schristos 			if (!(diff & (((bfd_vma) 1 << section_alignment) - 1)))
5804*56bb7041Schristos 			  diff = 0;
5805*56bb7041Schristos 		      }
5806*56bb7041Schristos 		    if (diff != 0
5807*56bb7041Schristos 			&& (config.warn_section_align
5808*56bb7041Schristos 			    || os->addr_tree != NULL))
5809*56bb7041Schristos 		      einfo (_("%P: warning: "
5810*56bb7041Schristos 			       "start of section %s changed by %ld\n"),
5811*56bb7041Schristos 			     os->name, (long) diff);
5812*56bb7041Schristos 		  }
5813*56bb7041Schristos 
5814*56bb7041Schristos 		bfd_set_section_vma (os->bfd_section, newdot);
5815*56bb7041Schristos 
5816*56bb7041Schristos 		os->bfd_section->output_offset = 0;
5817*56bb7041Schristos 	      }
5818*56bb7041Schristos 
5819*56bb7041Schristos 	    lang_size_sections_1 (&os->children.head, os,
5820*56bb7041Schristos 				  os->fill, newdot, relax, check_regions);
5821*56bb7041Schristos 
5822*56bb7041Schristos 	    os->processed_vma = TRUE;
5823*56bb7041Schristos 
5824*56bb7041Schristos 	    if (bfd_is_abs_section (os->bfd_section) || os->ignored)
5825*56bb7041Schristos 	      /* Except for some special linker created sections,
5826*56bb7041Schristos 		 no output section should change from zero size
5827*56bb7041Schristos 		 after strip_excluded_output_sections.  A non-zero
5828*56bb7041Schristos 		 size on an ignored section indicates that some
5829*56bb7041Schristos 		 input section was not sized early enough.  */
5830*56bb7041Schristos 	      ASSERT (os->bfd_section->size == 0);
5831*56bb7041Schristos 	    else
5832*56bb7041Schristos 	      {
5833*56bb7041Schristos 		dot = os->bfd_section->vma;
5834*56bb7041Schristos 
5835*56bb7041Schristos 		/* Put the section within the requested block size, or
5836*56bb7041Schristos 		   align at the block boundary.  */
5837*56bb7041Schristos 		after = ((dot
5838*56bb7041Schristos 			  + TO_ADDR (os->bfd_section->size)
5839*56bb7041Schristos 			  + os->block_value - 1)
5840*56bb7041Schristos 			 & - (bfd_vma) os->block_value);
5841*56bb7041Schristos 
5842*56bb7041Schristos 		if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
5843*56bb7041Schristos 		  os->bfd_section->size = TO_SIZE (after
5844*56bb7041Schristos 						   - os->bfd_section->vma);
5845*56bb7041Schristos 	      }
5846*56bb7041Schristos 
5847*56bb7041Schristos 	    /* Set section lma.  */
5848*56bb7041Schristos 	    r = os->region;
5849*56bb7041Schristos 	    if (r == NULL)
5850*56bb7041Schristos 	      r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
5851*56bb7041Schristos 
5852*56bb7041Schristos 	    if (os->load_base)
5853*56bb7041Schristos 	      {
5854*56bb7041Schristos 		bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
5855*56bb7041Schristos 		os->bfd_section->lma = lma;
5856*56bb7041Schristos 	      }
5857*56bb7041Schristos 	    else if (os->lma_region != NULL)
5858*56bb7041Schristos 	      {
5859*56bb7041Schristos 		bfd_vma lma = os->lma_region->current;
5860*56bb7041Schristos 
5861*56bb7041Schristos 		if (os->align_lma_with_input)
5862*56bb7041Schristos 		  lma += dotdelta;
5863*56bb7041Schristos 		else
5864*56bb7041Schristos 		  {
5865*56bb7041Schristos 		    /* When LMA_REGION is the same as REGION, align the LMA
5866*56bb7041Schristos 		       as we did for the VMA, possibly including alignment
5867*56bb7041Schristos 		       from the bfd section.  If a different region, then
5868*56bb7041Schristos 		       only align according to the value in the output
5869*56bb7041Schristos 		       statement.  */
5870*56bb7041Schristos 		    if (os->lma_region != os->region)
5871*56bb7041Schristos 		      section_alignment = exp_get_power (os->section_alignment,
5872*56bb7041Schristos 							 "section alignment");
5873*56bb7041Schristos 		    if (section_alignment > 0)
5874*56bb7041Schristos 		      lma = align_power (lma, section_alignment);
5875*56bb7041Schristos 		  }
5876*56bb7041Schristos 		os->bfd_section->lma = lma;
5877*56bb7041Schristos 	      }
5878*56bb7041Schristos 	    else if (r->last_os != NULL
5879*56bb7041Schristos 		     && (os->bfd_section->flags & SEC_ALLOC) != 0)
5880*56bb7041Schristos 	      {
5881*56bb7041Schristos 		bfd_vma lma;
5882*56bb7041Schristos 		asection *last;
5883*56bb7041Schristos 
5884*56bb7041Schristos 		last = r->last_os->output_section_statement.bfd_section;
5885*56bb7041Schristos 
5886*56bb7041Schristos 		/* A backwards move of dot should be accompanied by
5887*56bb7041Schristos 		   an explicit assignment to the section LMA (ie.
5888*56bb7041Schristos 		   os->load_base set) because backwards moves can
5889*56bb7041Schristos 		   create overlapping LMAs.  */
5890*56bb7041Schristos 		if (dot < last->vma
5891*56bb7041Schristos 		    && os->bfd_section->size != 0
5892*56bb7041Schristos 		    && dot + TO_ADDR (os->bfd_section->size) <= last->vma)
5893*56bb7041Schristos 		  {
5894*56bb7041Schristos 		    /* If dot moved backwards then leave lma equal to
5895*56bb7041Schristos 		       vma.  This is the old default lma, which might
5896*56bb7041Schristos 		       just happen to work when the backwards move is
5897*56bb7041Schristos 		       sufficiently large.  Nag if this changes anything,
5898*56bb7041Schristos 		       so people can fix their linker scripts.  */
5899*56bb7041Schristos 
5900*56bb7041Schristos 		    if (last->vma != last->lma)
5901*56bb7041Schristos 		      einfo (_("%P: warning: dot moved backwards "
5902*56bb7041Schristos 			       "before `%s'\n"), os->name);
5903*56bb7041Schristos 		  }
5904*56bb7041Schristos 		else
5905*56bb7041Schristos 		  {
5906*56bb7041Schristos 		    /* If this is an overlay, set the current lma to that
5907*56bb7041Schristos 		       at the end of the previous section.  */
5908*56bb7041Schristos 		    if (os->sectype == overlay_section)
5909*56bb7041Schristos 		      lma = last->lma + TO_ADDR (last->size);
5910*56bb7041Schristos 
5911*56bb7041Schristos 		    /* Otherwise, keep the same lma to vma relationship
5912*56bb7041Schristos 		       as the previous section.  */
5913*56bb7041Schristos 		    else
5914*56bb7041Schristos 		      lma = os->bfd_section->vma + last->lma - last->vma;
5915*56bb7041Schristos 
5916*56bb7041Schristos 		    if (section_alignment > 0)
5917*56bb7041Schristos 		      lma = align_power (lma, section_alignment);
5918*56bb7041Schristos 		    os->bfd_section->lma = lma;
5919*56bb7041Schristos 		  }
5920*56bb7041Schristos 	      }
5921*56bb7041Schristos 	    os->processed_lma = TRUE;
5922*56bb7041Schristos 
5923*56bb7041Schristos 	    /* Keep track of normal sections using the default
5924*56bb7041Schristos 	       lma region.  We use this to set the lma for
5925*56bb7041Schristos 	       following sections.  Overlays or other linker
5926*56bb7041Schristos 	       script assignment to lma might mean that the
5927*56bb7041Schristos 	       default lma == vma is incorrect.
5928*56bb7041Schristos 	       To avoid warnings about dot moving backwards when using
5929*56bb7041Schristos 	       -Ttext, don't start tracking sections until we find one
5930*56bb7041Schristos 	       of non-zero size or with lma set differently to vma.
5931*56bb7041Schristos 	       Do this tracking before we short-cut the loop so that we
5932*56bb7041Schristos 	       track changes for the case where the section size is zero,
5933*56bb7041Schristos 	       but the lma is set differently to the vma.  This is
5934*56bb7041Schristos 	       important, if an orphan section is placed after an
5935*56bb7041Schristos 	       otherwise empty output section that has an explicit lma
5936*56bb7041Schristos 	       set, we want that lma reflected in the orphans lma.  */
5937*56bb7041Schristos 	    if (((!IGNORE_SECTION (os->bfd_section)
5938*56bb7041Schristos 		  && (os->bfd_section->size != 0
5939*56bb7041Schristos 		      || (r->last_os == NULL
5940*56bb7041Schristos 			  && os->bfd_section->vma != os->bfd_section->lma)
5941*56bb7041Schristos 		      || (r->last_os != NULL
5942*56bb7041Schristos 			  && dot >= (r->last_os->output_section_statement
5943*56bb7041Schristos 				     .bfd_section->vma))))
5944*56bb7041Schristos 		 || os->sectype == first_overlay_section)
5945*56bb7041Schristos 		&& os->lma_region == NULL
5946*56bb7041Schristos 		&& !bfd_link_relocatable (&link_info))
5947*56bb7041Schristos 	      r->last_os = s;
5948*56bb7041Schristos 
5949*56bb7041Schristos 	    if (bfd_is_abs_section (os->bfd_section) || os->ignored)
5950*56bb7041Schristos 	      break;
5951*56bb7041Schristos 
5952*56bb7041Schristos 	    /* .tbss sections effectively have zero size.  */
5953*56bb7041Schristos 	    if (!IS_TBSS (os->bfd_section)
5954*56bb7041Schristos 		|| bfd_link_relocatable (&link_info))
5955*56bb7041Schristos 	      dotdelta = TO_ADDR (os->bfd_section->size);
5956*56bb7041Schristos 	    else
5957*56bb7041Schristos 	      dotdelta = 0;
5958*56bb7041Schristos 	    dot += dotdelta;
5959*56bb7041Schristos 
5960*56bb7041Schristos 	    if (os->update_dot_tree != 0)
5961*56bb7041Schristos 	      exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5962*56bb7041Schristos 
5963*56bb7041Schristos 	    /* Update dot in the region ?
5964*56bb7041Schristos 	       We only do this if the section is going to be allocated,
5965*56bb7041Schristos 	       since unallocated sections do not contribute to the region's
5966*56bb7041Schristos 	       overall size in memory.  */
5967*56bb7041Schristos 	    if (os->region != NULL
5968*56bb7041Schristos 		&& (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))
5969*56bb7041Schristos 	      {
5970*56bb7041Schristos 		os->region->current = dot;
5971*56bb7041Schristos 
5972*56bb7041Schristos 		if (check_regions)
5973*56bb7041Schristos 		  /* Make sure the new address is within the region.  */
5974*56bb7041Schristos 		  os_region_check (os, os->region, os->addr_tree,
5975*56bb7041Schristos 				   os->bfd_section->vma);
5976*56bb7041Schristos 
5977*56bb7041Schristos 		if (os->lma_region != NULL && os->lma_region != os->region
5978*56bb7041Schristos 		    && ((os->bfd_section->flags & SEC_LOAD)
5979*56bb7041Schristos 			|| os->align_lma_with_input))
5980*56bb7041Schristos 		  {
5981*56bb7041Schristos 		    os->lma_region->current = os->bfd_section->lma + dotdelta;
5982*56bb7041Schristos 
5983*56bb7041Schristos 		    if (check_regions)
5984*56bb7041Schristos 		      os_region_check (os, os->lma_region, NULL,
5985*56bb7041Schristos 				       os->bfd_section->lma);
5986*56bb7041Schristos 		  }
5987*56bb7041Schristos 	      }
5988*56bb7041Schristos 	  }
5989*56bb7041Schristos 	  break;
5990*56bb7041Schristos 
5991*56bb7041Schristos 	case lang_constructors_statement_enum:
5992*56bb7041Schristos 	  dot = lang_size_sections_1 (&constructor_list.head,
5993*56bb7041Schristos 				      output_section_statement,
5994*56bb7041Schristos 				      fill, dot, relax, check_regions);
5995*56bb7041Schristos 	  break;
5996*56bb7041Schristos 
5997*56bb7041Schristos 	case lang_data_statement_enum:
5998*56bb7041Schristos 	  {
5999*56bb7041Schristos 	    unsigned int size = 0;
6000*56bb7041Schristos 
6001*56bb7041Schristos 	    s->data_statement.output_offset =
6002*56bb7041Schristos 	      dot - output_section_statement->bfd_section->vma;
6003*56bb7041Schristos 	    s->data_statement.output_section =
6004*56bb7041Schristos 	      output_section_statement->bfd_section;
6005*56bb7041Schristos 
6006*56bb7041Schristos 	    /* We might refer to provided symbols in the expression, and
6007*56bb7041Schristos 	       need to mark them as needed.  */
6008*56bb7041Schristos 	    exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
6009*56bb7041Schristos 
6010*56bb7041Schristos 	    switch (s->data_statement.type)
6011*56bb7041Schristos 	      {
6012*56bb7041Schristos 	      default:
6013*56bb7041Schristos 		abort ();
6014*56bb7041Schristos 	      case QUAD:
6015*56bb7041Schristos 	      case SQUAD:
6016*56bb7041Schristos 		size = QUAD_SIZE;
6017*56bb7041Schristos 		break;
6018*56bb7041Schristos 	      case LONG:
6019*56bb7041Schristos 		size = LONG_SIZE;
6020*56bb7041Schristos 		break;
6021*56bb7041Schristos 	      case SHORT:
6022*56bb7041Schristos 		size = SHORT_SIZE;
6023*56bb7041Schristos 		break;
6024*56bb7041Schristos 	      case BYTE:
6025*56bb7041Schristos 		size = BYTE_SIZE;
6026*56bb7041Schristos 		break;
6027*56bb7041Schristos 	      }
6028*56bb7041Schristos 	    if (size < TO_SIZE ((unsigned) 1))
6029*56bb7041Schristos 	      size = TO_SIZE ((unsigned) 1);
6030*56bb7041Schristos 	    dot += TO_ADDR (size);
6031*56bb7041Schristos 	    if (!(output_section_statement->bfd_section->flags
6032*56bb7041Schristos 		  & SEC_FIXED_SIZE))
6033*56bb7041Schristos 	      output_section_statement->bfd_section->size
6034*56bb7041Schristos 		= TO_SIZE (dot - output_section_statement->bfd_section->vma);
6035*56bb7041Schristos 
6036*56bb7041Schristos 	  }
6037*56bb7041Schristos 	  break;
6038*56bb7041Schristos 
6039*56bb7041Schristos 	case lang_reloc_statement_enum:
6040*56bb7041Schristos 	  {
6041*56bb7041Schristos 	    int size;
6042*56bb7041Schristos 
6043*56bb7041Schristos 	    s->reloc_statement.output_offset =
6044*56bb7041Schristos 	      dot - output_section_statement->bfd_section->vma;
6045*56bb7041Schristos 	    s->reloc_statement.output_section =
6046*56bb7041Schristos 	      output_section_statement->bfd_section;
6047*56bb7041Schristos 	    size = bfd_get_reloc_size (s->reloc_statement.howto);
6048*56bb7041Schristos 	    dot += TO_ADDR (size);
6049*56bb7041Schristos 	    if (!(output_section_statement->bfd_section->flags
6050*56bb7041Schristos 		  & SEC_FIXED_SIZE))
6051*56bb7041Schristos 	      output_section_statement->bfd_section->size
6052*56bb7041Schristos 		= TO_SIZE (dot - output_section_statement->bfd_section->vma);
6053*56bb7041Schristos 	  }
6054*56bb7041Schristos 	  break;
6055*56bb7041Schristos 
6056*56bb7041Schristos 	case lang_wild_statement_enum:
6057*56bb7041Schristos 	  dot = lang_size_sections_1 (&s->wild_statement.children.head,
6058*56bb7041Schristos 				      output_section_statement,
6059*56bb7041Schristos 				      fill, dot, relax, check_regions);
6060*56bb7041Schristos 	  break;
6061*56bb7041Schristos 
6062*56bb7041Schristos 	case lang_object_symbols_statement_enum:
6063*56bb7041Schristos 	  link_info.create_object_symbols_section
6064*56bb7041Schristos 	    = output_section_statement->bfd_section;
6065*56bb7041Schristos 	  output_section_statement->bfd_section->flags |= SEC_KEEP;
6066*56bb7041Schristos 	  break;
6067*56bb7041Schristos 
6068*56bb7041Schristos 	case lang_output_statement_enum:
6069*56bb7041Schristos 	case lang_target_statement_enum:
6070*56bb7041Schristos 	  break;
6071*56bb7041Schristos 
6072*56bb7041Schristos 	case lang_input_section_enum:
6073*56bb7041Schristos 	  {
6074*56bb7041Schristos 	    asection *i;
6075*56bb7041Schristos 
6076*56bb7041Schristos 	    i = s->input_section.section;
6077*56bb7041Schristos 	    if (relax)
6078*56bb7041Schristos 	      {
6079*56bb7041Schristos 		bfd_boolean again;
6080*56bb7041Schristos 
6081*56bb7041Schristos 		if (!bfd_relax_section (i->owner, i, &link_info, &again))
6082*56bb7041Schristos 		  einfo (_("%F%P: can't relax section: %E\n"));
6083*56bb7041Schristos 		if (again)
6084*56bb7041Schristos 		  *relax = TRUE;
6085*56bb7041Schristos 	      }
6086*56bb7041Schristos 	    dot = size_input_section (prev, output_section_statement,
6087*56bb7041Schristos 				      fill, &removed, dot);
6088*56bb7041Schristos 	  }
6089*56bb7041Schristos 	  break;
6090*56bb7041Schristos 
6091*56bb7041Schristos 	case lang_input_statement_enum:
6092*56bb7041Schristos 	  break;
6093*56bb7041Schristos 
6094*56bb7041Schristos 	case lang_fill_statement_enum:
6095*56bb7041Schristos 	  s->fill_statement.output_section =
6096*56bb7041Schristos 	    output_section_statement->bfd_section;
6097*56bb7041Schristos 
6098*56bb7041Schristos 	  fill = s->fill_statement.fill;
6099*56bb7041Schristos 	  break;
6100*56bb7041Schristos 
6101*56bb7041Schristos 	case lang_assignment_statement_enum:
6102*56bb7041Schristos 	  {
6103*56bb7041Schristos 	    bfd_vma newdot = dot;
6104*56bb7041Schristos 	    etree_type *tree = s->assignment_statement.exp;
6105*56bb7041Schristos 
6106*56bb7041Schristos 	    expld.dataseg.relro = exp_seg_relro_none;
6107*56bb7041Schristos 
6108*56bb7041Schristos 	    exp_fold_tree (tree,
6109*56bb7041Schristos 			   output_section_statement->bfd_section,
6110*56bb7041Schristos 			   &newdot);
6111*56bb7041Schristos 
6112*56bb7041Schristos 	    ldlang_check_relro_region (s, &expld.dataseg);
6113*56bb7041Schristos 
6114*56bb7041Schristos 	    expld.dataseg.relro = exp_seg_relro_none;
6115*56bb7041Schristos 
6116*56bb7041Schristos 	    /* This symbol may be relative to this section.  */
6117*56bb7041Schristos 	    if ((tree->type.node_class == etree_provided
6118*56bb7041Schristos 		 || tree->type.node_class == etree_assign)
6119*56bb7041Schristos 		&& (tree->assign.dst [0] != '.'
6120*56bb7041Schristos 		    || tree->assign.dst [1] != '\0'))
6121*56bb7041Schristos 	      output_section_statement->update_dot = 1;
6122*56bb7041Schristos 
6123*56bb7041Schristos 	    if (!output_section_statement->ignored)
6124*56bb7041Schristos 	      {
6125*56bb7041Schristos 		if (output_section_statement == abs_output_section)
6126*56bb7041Schristos 		  {
6127*56bb7041Schristos 		    /* If we don't have an output section, then just adjust
6128*56bb7041Schristos 		       the default memory address.  */
6129*56bb7041Schristos 		    lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
6130*56bb7041Schristos 					       FALSE)->current = newdot;
6131*56bb7041Schristos 		  }
6132*56bb7041Schristos 		else if (newdot != dot)
6133*56bb7041Schristos 		  {
6134*56bb7041Schristos 		    /* Insert a pad after this statement.  We can't
6135*56bb7041Schristos 		       put the pad before when relaxing, in case the
6136*56bb7041Schristos 		       assignment references dot.  */
6137*56bb7041Schristos 		    insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
6138*56bb7041Schristos 				output_section_statement->bfd_section, dot);
6139*56bb7041Schristos 
6140*56bb7041Schristos 		    /* Don't neuter the pad below when relaxing.  */
6141*56bb7041Schristos 		    s = s->header.next;
6142*56bb7041Schristos 
6143*56bb7041Schristos 		    /* If dot is advanced, this implies that the section
6144*56bb7041Schristos 		       should have space allocated to it, unless the
6145*56bb7041Schristos 		       user has explicitly stated that the section
6146*56bb7041Schristos 		       should not be allocated.  */
6147*56bb7041Schristos 		    if (output_section_statement->sectype != noalloc_section
6148*56bb7041Schristos 			&& (output_section_statement->sectype != noload_section
6149*56bb7041Schristos 			    || (bfd_get_flavour (link_info.output_bfd)
6150*56bb7041Schristos 				== bfd_target_elf_flavour)))
6151*56bb7041Schristos 		      output_section_statement->bfd_section->flags |= SEC_ALLOC;
6152*56bb7041Schristos 		  }
6153*56bb7041Schristos 		dot = newdot;
6154*56bb7041Schristos 	      }
6155*56bb7041Schristos 	  }
6156*56bb7041Schristos 	  break;
6157*56bb7041Schristos 
6158*56bb7041Schristos 	case lang_padding_statement_enum:
6159*56bb7041Schristos 	  /* If this is the first time lang_size_sections is called,
6160*56bb7041Schristos 	     we won't have any padding statements.  If this is the
6161*56bb7041Schristos 	     second or later passes when relaxing, we should allow
6162*56bb7041Schristos 	     padding to shrink.  If padding is needed on this pass, it
6163*56bb7041Schristos 	     will be added back in.  */
6164*56bb7041Schristos 	  s->padding_statement.size = 0;
6165*56bb7041Schristos 
6166*56bb7041Schristos 	  /* Make sure output_offset is valid.  If relaxation shrinks
6167*56bb7041Schristos 	     the section and this pad isn't needed, it's possible to
6168*56bb7041Schristos 	     have output_offset larger than the final size of the
6169*56bb7041Schristos 	     section.  bfd_set_section_contents will complain even for
6170*56bb7041Schristos 	     a pad size of zero.  */
6171*56bb7041Schristos 	  s->padding_statement.output_offset
6172*56bb7041Schristos 	    = dot - output_section_statement->bfd_section->vma;
6173*56bb7041Schristos 	  break;
6174*56bb7041Schristos 
6175*56bb7041Schristos 	case lang_group_statement_enum:
6176*56bb7041Schristos 	  dot = lang_size_sections_1 (&s->group_statement.children.head,
6177*56bb7041Schristos 				      output_section_statement,
6178*56bb7041Schristos 				      fill, dot, relax, check_regions);
6179*56bb7041Schristos 	  break;
6180*56bb7041Schristos 
6181*56bb7041Schristos 	case lang_insert_statement_enum:
6182*56bb7041Schristos 	  break;
6183*56bb7041Schristos 
6184*56bb7041Schristos 	  /* We can only get here when relaxing is turned on.  */
6185*56bb7041Schristos 	case lang_address_statement_enum:
6186*56bb7041Schristos 	  break;
6187*56bb7041Schristos 
6188*56bb7041Schristos 	default:
6189*56bb7041Schristos 	  FAIL ();
6190*56bb7041Schristos 	  break;
6191*56bb7041Schristos 	}
6192*56bb7041Schristos 
6193*56bb7041Schristos       /* If an input section doesn't fit in the current output
6194*56bb7041Schristos 	 section, remove it from the list.  Handle the case where we
6195*56bb7041Schristos 	 have to remove an input_section statement here: there is a
6196*56bb7041Schristos 	 special case to remove the first element of the list.  */
6197*56bb7041Schristos       if (link_info.non_contiguous_regions && removed)
6198*56bb7041Schristos 	{
6199*56bb7041Schristos 	  /* If we removed the first element during the previous
6200*56bb7041Schristos 	     iteration, override the loop assignment of prev_s.  */
6201*56bb7041Schristos 	  if (removed_prev_s)
6202*56bb7041Schristos 	      prev_s = NULL;
6203*56bb7041Schristos 
6204*56bb7041Schristos 	  if (prev_s)
6205*56bb7041Schristos 	    {
6206*56bb7041Schristos 	      /* If there was a real previous input section, just skip
6207*56bb7041Schristos 		 the current one.  */
6208*56bb7041Schristos 	      prev_s->header.next=s->header.next;
6209*56bb7041Schristos 	      s = prev_s;
6210*56bb7041Schristos 	      removed_prev_s = FALSE;
6211*56bb7041Schristos 	    }
6212*56bb7041Schristos 	  else
6213*56bb7041Schristos 	    {
6214*56bb7041Schristos 	      /* Remove the first input section of the list.  */
6215*56bb7041Schristos 	      *prev = s->header.next;
6216*56bb7041Schristos 	      removed_prev_s = TRUE;
6217*56bb7041Schristos 	    }
6218*56bb7041Schristos 
6219*56bb7041Schristos 	  /* Move to next element, unless we removed the head of the
6220*56bb7041Schristos 	     list.  */
6221*56bb7041Schristos 	  if (!removed_prev_s)
6222*56bb7041Schristos 	    prev = &s->header.next;
6223*56bb7041Schristos 	}
6224*56bb7041Schristos       else
6225*56bb7041Schristos 	{
6226*56bb7041Schristos 	  prev = &s->header.next;
6227*56bb7041Schristos 	  removed_prev_s = FALSE;
6228*56bb7041Schristos 	}
6229*56bb7041Schristos     }
6230*56bb7041Schristos   return dot;
6231*56bb7041Schristos }
6232*56bb7041Schristos 
6233*56bb7041Schristos /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6234*56bb7041Schristos    The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6235*56bb7041Schristos    CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6236*56bb7041Schristos    segments.  We are allowed an opportunity to override this decision.  */
6237*56bb7041Schristos 
6238*56bb7041Schristos bfd_boolean
ldlang_override_segment_assignment(struct bfd_link_info * info ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * current_section,asection * previous_section,bfd_boolean new_segment)6239*56bb7041Schristos ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6240*56bb7041Schristos 				    bfd *abfd ATTRIBUTE_UNUSED,
6241*56bb7041Schristos 				    asection *current_section,
6242*56bb7041Schristos 				    asection *previous_section,
6243*56bb7041Schristos 				    bfd_boolean new_segment)
6244*56bb7041Schristos {
6245*56bb7041Schristos   lang_output_section_statement_type *cur;
6246*56bb7041Schristos   lang_output_section_statement_type *prev;
6247*56bb7041Schristos 
6248*56bb7041Schristos   /* The checks below are only necessary when the BFD library has decided
6249*56bb7041Schristos      that the two sections ought to be placed into the same segment.  */
6250*56bb7041Schristos   if (new_segment)
6251*56bb7041Schristos     return TRUE;
6252*56bb7041Schristos 
6253*56bb7041Schristos   /* Paranoia checks.  */
6254*56bb7041Schristos   if (current_section == NULL || previous_section == NULL)
6255*56bb7041Schristos     return new_segment;
6256*56bb7041Schristos 
6257*56bb7041Schristos   /* If this flag is set, the target never wants code and non-code
6258*56bb7041Schristos      sections comingled in the same segment.  */
6259*56bb7041Schristos   if (config.separate_code
6260*56bb7041Schristos       && ((current_section->flags ^ previous_section->flags) & SEC_CODE))
6261*56bb7041Schristos     return TRUE;
6262*56bb7041Schristos 
6263*56bb7041Schristos   /* Find the memory regions associated with the two sections.
6264*56bb7041Schristos      We call lang_output_section_find() here rather than scanning the list
6265*56bb7041Schristos      of output sections looking for a matching section pointer because if
6266*56bb7041Schristos      we have a large number of sections then a hash lookup is faster.  */
6267*56bb7041Schristos   cur  = lang_output_section_find (current_section->name);
6268*56bb7041Schristos   prev = lang_output_section_find (previous_section->name);
6269*56bb7041Schristos 
6270*56bb7041Schristos   /* More paranoia.  */
6271*56bb7041Schristos   if (cur == NULL || prev == NULL)
6272*56bb7041Schristos     return new_segment;
6273*56bb7041Schristos 
6274*56bb7041Schristos   /* If the regions are different then force the sections to live in
6275*56bb7041Schristos      different segments.  See the email thread starting at the following
6276*56bb7041Schristos      URL for the reasons why this is necessary:
6277*56bb7041Schristos      http://sourceware.org/ml/binutils/2007-02/msg00216.html  */
6278*56bb7041Schristos   return cur->region != prev->region;
6279*56bb7041Schristos }
6280*56bb7041Schristos 
6281*56bb7041Schristos void
one_lang_size_sections_pass(bfd_boolean * relax,bfd_boolean check_regions)6282*56bb7041Schristos one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
6283*56bb7041Schristos {
6284*56bb7041Schristos   lang_statement_iteration++;
6285*56bb7041Schristos   if (expld.phase != lang_mark_phase_enum)
6286*56bb7041Schristos     lang_sizing_iteration++;
6287*56bb7041Schristos   lang_size_sections_1 (&statement_list.head, abs_output_section,
6288*56bb7041Schristos 			0, 0, relax, check_regions);
6289*56bb7041Schristos }
6290*56bb7041Schristos 
6291*56bb7041Schristos static bfd_boolean
lang_size_segment(seg_align_type * seg)6292*56bb7041Schristos lang_size_segment (seg_align_type *seg)
6293*56bb7041Schristos {
6294*56bb7041Schristos   /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6295*56bb7041Schristos      a page could be saved in the data segment.  */
6296*56bb7041Schristos   bfd_vma first, last;
6297*56bb7041Schristos 
6298*56bb7041Schristos   first = -seg->base & (seg->pagesize - 1);
6299*56bb7041Schristos   last = seg->end & (seg->pagesize - 1);
6300*56bb7041Schristos   if (first && last
6301*56bb7041Schristos       && ((seg->base & ~(seg->pagesize - 1))
6302*56bb7041Schristos 	  != (seg->end & ~(seg->pagesize - 1)))
6303*56bb7041Schristos       && first + last <= seg->pagesize)
6304*56bb7041Schristos     {
6305*56bb7041Schristos       seg->phase = exp_seg_adjust;
6306*56bb7041Schristos       return TRUE;
6307*56bb7041Schristos     }
6308*56bb7041Schristos 
6309*56bb7041Schristos   seg->phase = exp_seg_done;
6310*56bb7041Schristos   return FALSE;
6311*56bb7041Schristos }
6312*56bb7041Schristos 
6313*56bb7041Schristos static bfd_vma
lang_size_relro_segment_1(seg_align_type * seg)6314*56bb7041Schristos lang_size_relro_segment_1 (seg_align_type *seg)
6315*56bb7041Schristos {
6316*56bb7041Schristos   bfd_vma relro_end, desired_end;
6317*56bb7041Schristos   asection *sec;
6318*56bb7041Schristos 
6319*56bb7041Schristos   /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end.  */
6320*56bb7041Schristos   relro_end = ((seg->relro_end + seg->pagesize - 1)
6321*56bb7041Schristos 	       & ~(seg->pagesize - 1));
6322*56bb7041Schristos 
6323*56bb7041Schristos   /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END.  */
6324*56bb7041Schristos   desired_end = relro_end - seg->relro_offset;
6325*56bb7041Schristos 
6326*56bb7041Schristos   /* For sections in the relro segment..  */
6327*56bb7041Schristos   for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev)
6328*56bb7041Schristos     if ((sec->flags & SEC_ALLOC) != 0
6329*56bb7041Schristos 	&& sec->vma >= seg->base
6330*56bb7041Schristos 	&& sec->vma < seg->relro_end - seg->relro_offset)
6331*56bb7041Schristos       {
6332*56bb7041Schristos 	/* Where do we want to put this section so that it ends as
6333*56bb7041Schristos 	   desired?  */
6334*56bb7041Schristos 	bfd_vma start, end, bump;
6335*56bb7041Schristos 
6336*56bb7041Schristos 	end = start = sec->vma;
6337*56bb7041Schristos 	if (!IS_TBSS (sec))
6338*56bb7041Schristos 	  end += TO_ADDR (sec->size);
6339*56bb7041Schristos 	bump = desired_end - end;
6340*56bb7041Schristos 	/* We'd like to increase START by BUMP, but we must heed
6341*56bb7041Schristos 	   alignment so the increase might be less than optimum.  */
6342*56bb7041Schristos 	start += bump;
6343*56bb7041Schristos 	start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1);
6344*56bb7041Schristos 	/* This is now the desired end for the previous section.  */
6345*56bb7041Schristos 	desired_end = start;
6346*56bb7041Schristos       }
6347*56bb7041Schristos 
6348*56bb7041Schristos   seg->phase = exp_seg_relro_adjust;
6349*56bb7041Schristos   ASSERT (desired_end >= seg->base);
6350*56bb7041Schristos   seg->base = desired_end;
6351*56bb7041Schristos   return relro_end;
6352*56bb7041Schristos }
6353*56bb7041Schristos 
6354*56bb7041Schristos static bfd_boolean
lang_size_relro_segment(bfd_boolean * relax,bfd_boolean check_regions)6355*56bb7041Schristos lang_size_relro_segment (bfd_boolean *relax, bfd_boolean check_regions)
6356*56bb7041Schristos {
6357*56bb7041Schristos   bfd_boolean do_reset = FALSE;
6358*56bb7041Schristos   bfd_boolean do_data_relro;
6359*56bb7041Schristos   bfd_vma data_initial_base, data_relro_end;
6360*56bb7041Schristos 
6361*56bb7041Schristos   if (link_info.relro && expld.dataseg.relro_end)
6362*56bb7041Schristos     {
6363*56bb7041Schristos       do_data_relro = TRUE;
6364*56bb7041Schristos       data_initial_base = expld.dataseg.base;
6365*56bb7041Schristos       data_relro_end = lang_size_relro_segment_1 (&expld.dataseg);
6366*56bb7041Schristos     }
6367*56bb7041Schristos   else
6368*56bb7041Schristos     {
6369*56bb7041Schristos       do_data_relro = FALSE;
6370*56bb7041Schristos       data_initial_base = data_relro_end = 0;
6371*56bb7041Schristos     }
6372*56bb7041Schristos 
6373*56bb7041Schristos   if (do_data_relro)
6374*56bb7041Schristos     {
6375*56bb7041Schristos       lang_reset_memory_regions ();
6376*56bb7041Schristos       one_lang_size_sections_pass (relax, check_regions);
6377*56bb7041Schristos 
6378*56bb7041Schristos       /* Assignments to dot, or to output section address in a user
6379*56bb7041Schristos 	 script have increased padding over the original.  Revert.  */
6380*56bb7041Schristos       if (do_data_relro && expld.dataseg.relro_end > data_relro_end)
6381*56bb7041Schristos 	{
6382*56bb7041Schristos 	  expld.dataseg.base = data_initial_base;;
6383*56bb7041Schristos 	  do_reset = TRUE;
6384*56bb7041Schristos 	}
6385*56bb7041Schristos     }
6386*56bb7041Schristos 
6387*56bb7041Schristos   if (!do_data_relro && lang_size_segment (&expld.dataseg))
6388*56bb7041Schristos     do_reset = TRUE;
6389*56bb7041Schristos 
6390*56bb7041Schristos   return do_reset;
6391*56bb7041Schristos }
6392*56bb7041Schristos 
6393*56bb7041Schristos void
lang_size_sections(bfd_boolean * relax,bfd_boolean check_regions)6394*56bb7041Schristos lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
6395*56bb7041Schristos {
6396*56bb7041Schristos   expld.phase = lang_allocating_phase_enum;
6397*56bb7041Schristos   expld.dataseg.phase = exp_seg_none;
6398*56bb7041Schristos 
6399*56bb7041Schristos   one_lang_size_sections_pass (relax, check_regions);
6400*56bb7041Schristos 
6401*56bb7041Schristos   if (expld.dataseg.phase != exp_seg_end_seen)
6402*56bb7041Schristos     expld.dataseg.phase = exp_seg_done;
6403*56bb7041Schristos 
6404*56bb7041Schristos   if (expld.dataseg.phase == exp_seg_end_seen)
6405*56bb7041Schristos     {
6406*56bb7041Schristos       bfd_boolean do_reset
6407*56bb7041Schristos 	= lang_size_relro_segment (relax, check_regions);
6408*56bb7041Schristos 
6409*56bb7041Schristos       if (do_reset)
6410*56bb7041Schristos 	{
6411*56bb7041Schristos 	  lang_reset_memory_regions ();
6412*56bb7041Schristos 	  one_lang_size_sections_pass (relax, check_regions);
6413*56bb7041Schristos 	}
6414*56bb7041Schristos 
6415*56bb7041Schristos       if (link_info.relro && expld.dataseg.relro_end)
6416*56bb7041Schristos 	{
6417*56bb7041Schristos 	  link_info.relro_start = expld.dataseg.base;
6418*56bb7041Schristos 	  link_info.relro_end = expld.dataseg.relro_end;
6419*56bb7041Schristos 	}
6420*56bb7041Schristos     }
6421*56bb7041Schristos }
6422*56bb7041Schristos 
6423*56bb7041Schristos static lang_output_section_statement_type *current_section;
6424*56bb7041Schristos static lang_assignment_statement_type *current_assign;
6425*56bb7041Schristos static bfd_boolean prefer_next_section;
6426*56bb7041Schristos 
6427*56bb7041Schristos /* Worker function for lang_do_assignments.  Recursiveness goes here.  */
6428*56bb7041Schristos 
6429*56bb7041Schristos static bfd_vma
lang_do_assignments_1(lang_statement_union_type * s,lang_output_section_statement_type * current_os,fill_type * fill,bfd_vma dot,bfd_boolean * found_end)6430*56bb7041Schristos lang_do_assignments_1 (lang_statement_union_type *s,
6431*56bb7041Schristos 		       lang_output_section_statement_type *current_os,
6432*56bb7041Schristos 		       fill_type *fill,
6433*56bb7041Schristos 		       bfd_vma dot,
6434*56bb7041Schristos 		       bfd_boolean *found_end)
6435*56bb7041Schristos {
6436*56bb7041Schristos   for (; s != NULL; s = s->header.next)
6437*56bb7041Schristos     {
6438*56bb7041Schristos       switch (s->header.type)
6439*56bb7041Schristos 	{
6440*56bb7041Schristos 	case lang_constructors_statement_enum:
6441*56bb7041Schristos 	  dot = lang_do_assignments_1 (constructor_list.head,
6442*56bb7041Schristos 				       current_os, fill, dot, found_end);
6443*56bb7041Schristos 	  break;
6444*56bb7041Schristos 
6445*56bb7041Schristos 	case lang_output_section_statement_enum:
6446*56bb7041Schristos 	  {
6447*56bb7041Schristos 	    lang_output_section_statement_type *os;
6448*56bb7041Schristos 	    bfd_vma newdot;
6449*56bb7041Schristos 
6450*56bb7041Schristos 	    os = &(s->output_section_statement);
6451*56bb7041Schristos 	    os->after_end = *found_end;
6452*56bb7041Schristos 	    init_opb (os->bfd_section);
6453*56bb7041Schristos 	    if (os->bfd_section != NULL && !os->ignored)
6454*56bb7041Schristos 	      {
6455*56bb7041Schristos 		if ((os->bfd_section->flags & SEC_ALLOC) != 0)
6456*56bb7041Schristos 		  {
6457*56bb7041Schristos 		    current_section = os;
6458*56bb7041Schristos 		    prefer_next_section = FALSE;
6459*56bb7041Schristos 		  }
6460*56bb7041Schristos 		dot = os->bfd_section->vma;
6461*56bb7041Schristos 	      }
6462*56bb7041Schristos 	    newdot = lang_do_assignments_1 (os->children.head,
6463*56bb7041Schristos 					    os, os->fill, dot, found_end);
6464*56bb7041Schristos 	    if (!os->ignored)
6465*56bb7041Schristos 	      {
6466*56bb7041Schristos 		if (os->bfd_section != NULL)
6467*56bb7041Schristos 		  {
6468*56bb7041Schristos 		    /* .tbss sections effectively have zero size.  */
6469*56bb7041Schristos 		    if (!IS_TBSS (os->bfd_section)
6470*56bb7041Schristos 			|| bfd_link_relocatable (&link_info))
6471*56bb7041Schristos 		      dot += TO_ADDR (os->bfd_section->size);
6472*56bb7041Schristos 
6473*56bb7041Schristos 		    if (os->update_dot_tree != NULL)
6474*56bb7041Schristos 		      exp_fold_tree (os->update_dot_tree,
6475*56bb7041Schristos 				     bfd_abs_section_ptr, &dot);
6476*56bb7041Schristos 		  }
6477*56bb7041Schristos 		else
6478*56bb7041Schristos 		  dot = newdot;
6479*56bb7041Schristos 	      }
6480*56bb7041Schristos 	  }
6481*56bb7041Schristos 	  break;
6482*56bb7041Schristos 
6483*56bb7041Schristos 	case lang_wild_statement_enum:
6484*56bb7041Schristos 
6485*56bb7041Schristos 	  dot = lang_do_assignments_1 (s->wild_statement.children.head,
6486*56bb7041Schristos 				       current_os, fill, dot, found_end);
6487*56bb7041Schristos 	  break;
6488*56bb7041Schristos 
6489*56bb7041Schristos 	case lang_object_symbols_statement_enum:
6490*56bb7041Schristos 	case lang_output_statement_enum:
6491*56bb7041Schristos 	case lang_target_statement_enum:
6492*56bb7041Schristos 	  break;
6493*56bb7041Schristos 
6494*56bb7041Schristos 	case lang_data_statement_enum:
6495*56bb7041Schristos 	  exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
6496*56bb7041Schristos 	  if (expld.result.valid_p)
6497*56bb7041Schristos 	    {
6498*56bb7041Schristos 	      s->data_statement.value = expld.result.value;
6499*56bb7041Schristos 	      if (expld.result.section != NULL)
6500*56bb7041Schristos 		s->data_statement.value += expld.result.section->vma;
6501*56bb7041Schristos 	    }
6502*56bb7041Schristos 	  else if (expld.phase == lang_final_phase_enum)
6503*56bb7041Schristos 	    einfo (_("%F%P: invalid data statement\n"));
6504*56bb7041Schristos 	  {
6505*56bb7041Schristos 	    unsigned int size;
6506*56bb7041Schristos 	    switch (s->data_statement.type)
6507*56bb7041Schristos 	      {
6508*56bb7041Schristos 	      default:
6509*56bb7041Schristos 		abort ();
6510*56bb7041Schristos 	      case QUAD:
6511*56bb7041Schristos 	      case SQUAD:
6512*56bb7041Schristos 		size = QUAD_SIZE;
6513*56bb7041Schristos 		break;
6514*56bb7041Schristos 	      case LONG:
6515*56bb7041Schristos 		size = LONG_SIZE;
6516*56bb7041Schristos 		break;
6517*56bb7041Schristos 	      case SHORT:
6518*56bb7041Schristos 		size = SHORT_SIZE;
6519*56bb7041Schristos 		break;
6520*56bb7041Schristos 	      case BYTE:
6521*56bb7041Schristos 		size = BYTE_SIZE;
6522*56bb7041Schristos 		break;
6523*56bb7041Schristos 	      }
6524*56bb7041Schristos 	    if (size < TO_SIZE ((unsigned) 1))
6525*56bb7041Schristos 	      size = TO_SIZE ((unsigned) 1);
6526*56bb7041Schristos 	    dot += TO_ADDR (size);
6527*56bb7041Schristos 	  }
6528*56bb7041Schristos 	  break;
6529*56bb7041Schristos 
6530*56bb7041Schristos 	case lang_reloc_statement_enum:
6531*56bb7041Schristos 	  exp_fold_tree (s->reloc_statement.addend_exp,
6532*56bb7041Schristos 			 bfd_abs_section_ptr, &dot);
6533*56bb7041Schristos 	  if (expld.result.valid_p)
6534*56bb7041Schristos 	    s->reloc_statement.addend_value = expld.result.value;
6535*56bb7041Schristos 	  else if (expld.phase == lang_final_phase_enum)
6536*56bb7041Schristos 	    einfo (_("%F%P: invalid reloc statement\n"));
6537*56bb7041Schristos 	  dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
6538*56bb7041Schristos 	  break;
6539*56bb7041Schristos 
6540*56bb7041Schristos 	case lang_input_section_enum:
6541*56bb7041Schristos 	  {
6542*56bb7041Schristos 	    asection *in = s->input_section.section;
6543*56bb7041Schristos 
6544*56bb7041Schristos 	    if ((in->flags & SEC_EXCLUDE) == 0)
6545*56bb7041Schristos 	      dot += TO_ADDR (in->size);
6546*56bb7041Schristos 	  }
6547*56bb7041Schristos 	  break;
6548*56bb7041Schristos 
6549*56bb7041Schristos 	case lang_input_statement_enum:
6550*56bb7041Schristos 	  break;
6551*56bb7041Schristos 
6552*56bb7041Schristos 	case lang_fill_statement_enum:
6553*56bb7041Schristos 	  fill = s->fill_statement.fill;
6554*56bb7041Schristos 	  break;
6555*56bb7041Schristos 
6556*56bb7041Schristos 	case lang_assignment_statement_enum:
6557*56bb7041Schristos 	  current_assign = &s->assignment_statement;
6558*56bb7041Schristos 	  if (current_assign->exp->type.node_class != etree_assert)
6559*56bb7041Schristos 	    {
6560*56bb7041Schristos 	      const char *p = current_assign->exp->assign.dst;
6561*56bb7041Schristos 
6562*56bb7041Schristos 	      if (current_os == abs_output_section && p[0] == '.' && p[1] == 0)
6563*56bb7041Schristos 		prefer_next_section = TRUE;
6564*56bb7041Schristos 
6565*56bb7041Schristos 	      while (*p == '_')
6566*56bb7041Schristos 		++p;
6567*56bb7041Schristos 	      if (strcmp (p, "end") == 0)
6568*56bb7041Schristos 		*found_end = TRUE;
6569*56bb7041Schristos 	    }
6570*56bb7041Schristos 	  exp_fold_tree (s->assignment_statement.exp,
6571*56bb7041Schristos 			 (current_os->bfd_section != NULL
6572*56bb7041Schristos 			  ? current_os->bfd_section : bfd_und_section_ptr),
6573*56bb7041Schristos 			 &dot);
6574*56bb7041Schristos 	  break;
6575*56bb7041Schristos 
6576*56bb7041Schristos 	case lang_padding_statement_enum:
6577*56bb7041Schristos 	  dot += TO_ADDR (s->padding_statement.size);
6578*56bb7041Schristos 	  break;
6579*56bb7041Schristos 
6580*56bb7041Schristos 	case lang_group_statement_enum:
6581*56bb7041Schristos 	  dot = lang_do_assignments_1 (s->group_statement.children.head,
6582*56bb7041Schristos 				       current_os, fill, dot, found_end);
6583*56bb7041Schristos 	  break;
6584*56bb7041Schristos 
6585*56bb7041Schristos 	case lang_insert_statement_enum:
6586*56bb7041Schristos 	  break;
6587*56bb7041Schristos 
6588*56bb7041Schristos 	case lang_address_statement_enum:
6589*56bb7041Schristos 	  break;
6590*56bb7041Schristos 
6591*56bb7041Schristos 	default:
6592*56bb7041Schristos 	  FAIL ();
6593*56bb7041Schristos 	  break;
6594*56bb7041Schristos 	}
6595*56bb7041Schristos     }
6596*56bb7041Schristos   return dot;
6597*56bb7041Schristos }
6598*56bb7041Schristos 
6599*56bb7041Schristos void
lang_do_assignments(lang_phase_type phase)6600*56bb7041Schristos lang_do_assignments (lang_phase_type phase)
6601*56bb7041Schristos {
6602*56bb7041Schristos   bfd_boolean found_end = FALSE;
6603*56bb7041Schristos 
6604*56bb7041Schristos   current_section = NULL;
6605*56bb7041Schristos   prefer_next_section = FALSE;
6606*56bb7041Schristos   expld.phase = phase;
6607*56bb7041Schristos   lang_statement_iteration++;
6608*56bb7041Schristos   lang_do_assignments_1 (statement_list.head,
6609*56bb7041Schristos 			 abs_output_section, NULL, 0, &found_end);
6610*56bb7041Schristos }
6611*56bb7041Schristos 
6612*56bb7041Schristos /* For an assignment statement outside of an output section statement,
6613*56bb7041Schristos    choose the best of neighbouring output sections to use for values
6614*56bb7041Schristos    of "dot".  */
6615*56bb7041Schristos 
6616*56bb7041Schristos asection *
section_for_dot(void)6617*56bb7041Schristos section_for_dot (void)
6618*56bb7041Schristos {
6619*56bb7041Schristos   asection *s;
6620*56bb7041Schristos 
6621*56bb7041Schristos   /* Assignments belong to the previous output section, unless there
6622*56bb7041Schristos      has been an assignment to "dot", in which case following
6623*56bb7041Schristos      assignments belong to the next output section.  (The assumption
6624*56bb7041Schristos      is that an assignment to "dot" is setting up the address for the
6625*56bb7041Schristos      next output section.)  Except that past the assignment to "_end"
6626*56bb7041Schristos      we always associate with the previous section.  This exception is
6627*56bb7041Schristos      for targets like SH that define an alloc .stack or other
6628*56bb7041Schristos      weirdness after non-alloc sections.  */
6629*56bb7041Schristos   if (current_section == NULL || prefer_next_section)
6630*56bb7041Schristos     {
6631*56bb7041Schristos       lang_statement_union_type *stmt;
6632*56bb7041Schristos       lang_output_section_statement_type *os;
6633*56bb7041Schristos 
6634*56bb7041Schristos       for (stmt = (lang_statement_union_type *) current_assign;
6635*56bb7041Schristos 	   stmt != NULL;
6636*56bb7041Schristos 	   stmt = stmt->header.next)
6637*56bb7041Schristos 	if (stmt->header.type == lang_output_section_statement_enum)
6638*56bb7041Schristos 	  break;
6639*56bb7041Schristos 
6640*56bb7041Schristos       os = &stmt->output_section_statement;
6641*56bb7041Schristos       while (os != NULL
6642*56bb7041Schristos 	     && !os->after_end
6643*56bb7041Schristos 	     && (os->bfd_section == NULL
6644*56bb7041Schristos 		 || (os->bfd_section->flags & SEC_EXCLUDE) != 0
6645*56bb7041Schristos 		 || bfd_section_removed_from_list (link_info.output_bfd,
6646*56bb7041Schristos 						   os->bfd_section)))
6647*56bb7041Schristos 	os = os->next;
6648*56bb7041Schristos 
6649*56bb7041Schristos       if (current_section == NULL || os == NULL || !os->after_end)
6650*56bb7041Schristos 	{
6651*56bb7041Schristos 	  if (os != NULL)
6652*56bb7041Schristos 	    s = os->bfd_section;
6653*56bb7041Schristos 	  else
6654*56bb7041Schristos 	    s = link_info.output_bfd->section_last;
6655*56bb7041Schristos 	  while (s != NULL
6656*56bb7041Schristos 		 && ((s->flags & SEC_ALLOC) == 0
6657*56bb7041Schristos 		     || (s->flags & SEC_THREAD_LOCAL) != 0))
6658*56bb7041Schristos 	    s = s->prev;
6659*56bb7041Schristos 	  if (s != NULL)
6660*56bb7041Schristos 	    return s;
6661*56bb7041Schristos 
6662*56bb7041Schristos 	  return bfd_abs_section_ptr;
6663*56bb7041Schristos 	}
6664*56bb7041Schristos     }
6665*56bb7041Schristos 
6666*56bb7041Schristos   s = current_section->bfd_section;
6667*56bb7041Schristos 
6668*56bb7041Schristos   /* The section may have been stripped.  */
6669*56bb7041Schristos   while (s != NULL
6670*56bb7041Schristos 	 && ((s->flags & SEC_EXCLUDE) != 0
6671*56bb7041Schristos 	     || (s->flags & SEC_ALLOC) == 0
6672*56bb7041Schristos 	     || (s->flags & SEC_THREAD_LOCAL) != 0
6673*56bb7041Schristos 	     || bfd_section_removed_from_list (link_info.output_bfd, s)))
6674*56bb7041Schristos     s = s->prev;
6675*56bb7041Schristos   if (s == NULL)
6676*56bb7041Schristos     s = link_info.output_bfd->sections;
6677*56bb7041Schristos   while (s != NULL
6678*56bb7041Schristos 	 && ((s->flags & SEC_ALLOC) == 0
6679*56bb7041Schristos 	     || (s->flags & SEC_THREAD_LOCAL) != 0))
6680*56bb7041Schristos     s = s->next;
6681*56bb7041Schristos   if (s != NULL)
6682*56bb7041Schristos     return s;
6683*56bb7041Schristos 
6684*56bb7041Schristos   return bfd_abs_section_ptr;
6685*56bb7041Schristos }
6686*56bb7041Schristos 
6687*56bb7041Schristos /* Array of __start/__stop/.startof./.sizeof/ symbols.  */
6688*56bb7041Schristos 
6689*56bb7041Schristos static struct bfd_link_hash_entry **start_stop_syms;
6690*56bb7041Schristos static size_t start_stop_count = 0;
6691*56bb7041Schristos static size_t start_stop_alloc = 0;
6692*56bb7041Schristos 
6693*56bb7041Schristos /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6694*56bb7041Schristos    to start_stop_syms.  */
6695*56bb7041Schristos 
6696*56bb7041Schristos static void
lang_define_start_stop(const char * symbol,asection * sec)6697*56bb7041Schristos lang_define_start_stop (const char *symbol, asection *sec)
6698*56bb7041Schristos {
6699*56bb7041Schristos   struct bfd_link_hash_entry *h;
6700*56bb7041Schristos 
6701*56bb7041Schristos   h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec);
6702*56bb7041Schristos   if (h != NULL)
6703*56bb7041Schristos     {
6704*56bb7041Schristos       if (start_stop_count == start_stop_alloc)
6705*56bb7041Schristos 	{
6706*56bb7041Schristos 	  start_stop_alloc = 2 * start_stop_alloc + 10;
6707*56bb7041Schristos 	  start_stop_syms
6708*56bb7041Schristos 	    = xrealloc (start_stop_syms,
6709*56bb7041Schristos 			start_stop_alloc * sizeof (*start_stop_syms));
6710*56bb7041Schristos 	}
6711*56bb7041Schristos       start_stop_syms[start_stop_count++] = h;
6712*56bb7041Schristos     }
6713*56bb7041Schristos }
6714*56bb7041Schristos 
6715*56bb7041Schristos /* Check for input sections whose names match references to
6716*56bb7041Schristos    __start_SECNAME or __stop_SECNAME symbols.  Give the symbols
6717*56bb7041Schristos    preliminary definitions.  */
6718*56bb7041Schristos 
6719*56bb7041Schristos static void
lang_init_start_stop(void)6720*56bb7041Schristos lang_init_start_stop (void)
6721*56bb7041Schristos {
6722*56bb7041Schristos   bfd *abfd;
6723*56bb7041Schristos   asection *s;
6724*56bb7041Schristos   char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd);
6725*56bb7041Schristos 
6726*56bb7041Schristos   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
6727*56bb7041Schristos     for (s = abfd->sections; s != NULL; s = s->next)
6728*56bb7041Schristos       {
6729*56bb7041Schristos 	const char *ps;
6730*56bb7041Schristos 	const char *secname = s->name;
6731*56bb7041Schristos 
6732*56bb7041Schristos 	for (ps = secname; *ps != '\0'; ps++)
6733*56bb7041Schristos 	  if (!ISALNUM ((unsigned char) *ps) && *ps != '_')
6734*56bb7041Schristos 	    break;
6735*56bb7041Schristos 	if (*ps == '\0')
6736*56bb7041Schristos 	  {
6737*56bb7041Schristos 	    char *symbol = (char *) xmalloc (10 + strlen (secname));
6738*56bb7041Schristos 
6739*56bb7041Schristos 	    symbol[0] = leading_char;
6740*56bb7041Schristos 	    sprintf (symbol + (leading_char != 0), "__start_%s", secname);
6741*56bb7041Schristos 	    lang_define_start_stop (symbol, s);
6742*56bb7041Schristos 
6743*56bb7041Schristos 	    symbol[1] = leading_char;
6744*56bb7041Schristos 	    memcpy (symbol + 1 + (leading_char != 0), "__stop", 6);
6745*56bb7041Schristos 	    lang_define_start_stop (symbol + 1, s);
6746*56bb7041Schristos 
6747*56bb7041Schristos 	    free (symbol);
6748*56bb7041Schristos 	  }
6749*56bb7041Schristos       }
6750*56bb7041Schristos }
6751*56bb7041Schristos 
6752*56bb7041Schristos /* Iterate over start_stop_syms.  */
6753*56bb7041Schristos 
6754*56bb7041Schristos static void
foreach_start_stop(void (* func)(struct bfd_link_hash_entry *))6755*56bb7041Schristos foreach_start_stop (void (*func) (struct bfd_link_hash_entry *))
6756*56bb7041Schristos {
6757*56bb7041Schristos   size_t i;
6758*56bb7041Schristos 
6759*56bb7041Schristos   for (i = 0; i < start_stop_count; ++i)
6760*56bb7041Schristos     func (start_stop_syms[i]);
6761*56bb7041Schristos }
6762*56bb7041Schristos 
6763*56bb7041Schristos /* __start and __stop symbols are only supposed to be defined by the
6764*56bb7041Schristos    linker for orphan sections, but we now extend that to sections that
6765*56bb7041Schristos    map to an output section of the same name.  The symbols were
6766*56bb7041Schristos    defined early for --gc-sections, before we mapped input to output
6767*56bb7041Schristos    sections, so undo those that don't satisfy this rule.  */
6768*56bb7041Schristos 
6769*56bb7041Schristos static void
undef_start_stop(struct bfd_link_hash_entry * h)6770*56bb7041Schristos undef_start_stop (struct bfd_link_hash_entry *h)
6771*56bb7041Schristos {
6772*56bb7041Schristos   if (h->ldscript_def)
6773*56bb7041Schristos     return;
6774*56bb7041Schristos 
6775*56bb7041Schristos   if (h->u.def.section->output_section == NULL
6776*56bb7041Schristos       || h->u.def.section->output_section->owner != link_info.output_bfd
6777*56bb7041Schristos       || strcmp (h->u.def.section->name,
6778*56bb7041Schristos 		 h->u.def.section->output_section->name) != 0)
6779*56bb7041Schristos     {
6780*56bb7041Schristos       asection *sec = bfd_get_section_by_name (link_info.output_bfd,
6781*56bb7041Schristos 					       h->u.def.section->name);
6782*56bb7041Schristos       if (sec != NULL)
6783*56bb7041Schristos 	{
6784*56bb7041Schristos 	  /* When there are more than one input sections with the same
6785*56bb7041Schristos 	     section name, SECNAME, linker picks the first one to define
6786*56bb7041Schristos 	     __start_SECNAME and __stop_SECNAME symbols.  When the first
6787*56bb7041Schristos 	     input section is removed by comdat group, we need to check
6788*56bb7041Schristos 	     if there is still an output section with section name
6789*56bb7041Schristos 	     SECNAME.  */
6790*56bb7041Schristos 	  asection *i;
6791*56bb7041Schristos 	  for (i = sec->map_head.s; i != NULL; i = i->map_head.s)
6792*56bb7041Schristos 	    if (strcmp (h->u.def.section->name, i->name) == 0)
6793*56bb7041Schristos 	      {
6794*56bb7041Schristos 		h->u.def.section = i;
6795*56bb7041Schristos 		return;
6796*56bb7041Schristos 	      }
6797*56bb7041Schristos 	}
6798*56bb7041Schristos       h->type = bfd_link_hash_undefined;
6799*56bb7041Schristos       h->u.undef.abfd = NULL;
6800*56bb7041Schristos     }
6801*56bb7041Schristos }
6802*56bb7041Schristos 
6803*56bb7041Schristos static void
lang_undef_start_stop(void)6804*56bb7041Schristos lang_undef_start_stop (void)
6805*56bb7041Schristos {
6806*56bb7041Schristos   foreach_start_stop (undef_start_stop);
6807*56bb7041Schristos }
6808*56bb7041Schristos 
6809*56bb7041Schristos /* Check for output sections whose names match references to
6810*56bb7041Schristos    .startof.SECNAME or .sizeof.SECNAME symbols.  Give the symbols
6811*56bb7041Schristos    preliminary definitions.  */
6812*56bb7041Schristos 
6813*56bb7041Schristos static void
lang_init_startof_sizeof(void)6814*56bb7041Schristos lang_init_startof_sizeof (void)
6815*56bb7041Schristos {
6816*56bb7041Schristos   asection *s;
6817*56bb7041Schristos 
6818*56bb7041Schristos   for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
6819*56bb7041Schristos     {
6820*56bb7041Schristos       const char *secname = s->name;
6821*56bb7041Schristos       char *symbol = (char *) xmalloc (10 + strlen (secname));
6822*56bb7041Schristos 
6823*56bb7041Schristos       sprintf (symbol, ".startof.%s", secname);
6824*56bb7041Schristos       lang_define_start_stop (symbol, s);
6825*56bb7041Schristos 
6826*56bb7041Schristos       memcpy (symbol + 1, ".size", 5);
6827*56bb7041Schristos       lang_define_start_stop (symbol + 1, s);
6828*56bb7041Schristos       free (symbol);
6829*56bb7041Schristos     }
6830*56bb7041Schristos }
6831*56bb7041Schristos 
6832*56bb7041Schristos /* Set .startof., .sizeof., __start and __stop symbols final values.  */
6833*56bb7041Schristos 
6834*56bb7041Schristos static void
set_start_stop(struct bfd_link_hash_entry * h)6835*56bb7041Schristos set_start_stop (struct bfd_link_hash_entry *h)
6836*56bb7041Schristos {
6837*56bb7041Schristos   if (h->ldscript_def
6838*56bb7041Schristos       || h->type != bfd_link_hash_defined)
6839*56bb7041Schristos     return;
6840*56bb7041Schristos 
6841*56bb7041Schristos   if (h->root.string[0] == '.')
6842*56bb7041Schristos     {
6843*56bb7041Schristos       /* .startof. or .sizeof. symbol.
6844*56bb7041Schristos 	 .startof. already has final value.  */
6845*56bb7041Schristos       if (h->root.string[2] == 'i')
6846*56bb7041Schristos 	{
6847*56bb7041Schristos 	  /* .sizeof.  */
6848*56bb7041Schristos 	  h->u.def.value = TO_ADDR (h->u.def.section->size);
6849*56bb7041Schristos 	  h->u.def.section = bfd_abs_section_ptr;
6850*56bb7041Schristos 	}
6851*56bb7041Schristos     }
6852*56bb7041Schristos   else
6853*56bb7041Schristos     {
6854*56bb7041Schristos       /* __start or __stop symbol.  */
6855*56bb7041Schristos       int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0;
6856*56bb7041Schristos 
6857*56bb7041Schristos       h->u.def.section = h->u.def.section->output_section;
6858*56bb7041Schristos       if (h->root.string[4 + has_lead] == 'o')
6859*56bb7041Schristos 	{
6860*56bb7041Schristos 	  /* __stop_ */
6861*56bb7041Schristos 	  h->u.def.value = TO_ADDR (h->u.def.section->size);
6862*56bb7041Schristos 	}
6863*56bb7041Schristos     }
6864*56bb7041Schristos }
6865*56bb7041Schristos 
6866*56bb7041Schristos static void
lang_finalize_start_stop(void)6867*56bb7041Schristos lang_finalize_start_stop (void)
6868*56bb7041Schristos {
6869*56bb7041Schristos   foreach_start_stop (set_start_stop);
6870*56bb7041Schristos }
6871*56bb7041Schristos 
6872*56bb7041Schristos static void
lang_end(void)6873*56bb7041Schristos lang_end (void)
6874*56bb7041Schristos {
6875*56bb7041Schristos   struct bfd_link_hash_entry *h;
6876*56bb7041Schristos   bfd_boolean warn;
6877*56bb7041Schristos 
6878*56bb7041Schristos   if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections)
6879*56bb7041Schristos       || bfd_link_dll (&link_info))
6880*56bb7041Schristos     warn = entry_from_cmdline;
6881*56bb7041Schristos   else
6882*56bb7041Schristos     warn = TRUE;
6883*56bb7041Schristos 
6884*56bb7041Schristos   /* Force the user to specify a root when generating a relocatable with
6885*56bb7041Schristos      --gc-sections, unless --gc-keep-exported was also given.  */
6886*56bb7041Schristos   if (bfd_link_relocatable (&link_info)
6887*56bb7041Schristos       && link_info.gc_sections
6888*56bb7041Schristos       && !link_info.gc_keep_exported)
6889*56bb7041Schristos     {
6890*56bb7041Schristos       struct bfd_sym_chain *sym;
6891*56bb7041Schristos 
6892*56bb7041Schristos       for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next)
6893*56bb7041Schristos 	{
6894*56bb7041Schristos 	  h = bfd_link_hash_lookup (link_info.hash, sym->name,
6895*56bb7041Schristos 				    FALSE, FALSE, FALSE);
6896*56bb7041Schristos 	  if (h != NULL
6897*56bb7041Schristos 	      && (h->type == bfd_link_hash_defined
6898*56bb7041Schristos 		  || h->type == bfd_link_hash_defweak)
6899*56bb7041Schristos 	      && !bfd_is_const_section (h->u.def.section))
6900*56bb7041Schristos 	    break;
6901*56bb7041Schristos 	}
6902*56bb7041Schristos       if (!sym)
6903*56bb7041Schristos 	einfo (_("%F%P: --gc-sections requires a defined symbol root "
6904*56bb7041Schristos 		 "specified by -e or -u\n"));
6905*56bb7041Schristos     }
6906*56bb7041Schristos 
6907*56bb7041Schristos   if (entry_symbol.name == NULL)
6908*56bb7041Schristos     {
6909*56bb7041Schristos       /* No entry has been specified.  Look for the default entry, but
6910*56bb7041Schristos 	 don't warn if we don't find it.  */
6911*56bb7041Schristos       entry_symbol.name = entry_symbol_default;
6912*56bb7041Schristos       warn = FALSE;
6913*56bb7041Schristos     }
6914*56bb7041Schristos 
6915*56bb7041Schristos   h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
6916*56bb7041Schristos 			    FALSE, FALSE, TRUE);
6917*56bb7041Schristos   if (h != NULL
6918*56bb7041Schristos       && (h->type == bfd_link_hash_defined
6919*56bb7041Schristos 	  || h->type == bfd_link_hash_defweak)
6920*56bb7041Schristos       && h->u.def.section->output_section != NULL)
6921*56bb7041Schristos     {
6922*56bb7041Schristos       bfd_vma val;
6923*56bb7041Schristos 
6924*56bb7041Schristos       val = (h->u.def.value
6925*56bb7041Schristos 	     + bfd_section_vma (h->u.def.section->output_section)
6926*56bb7041Schristos 	     + h->u.def.section->output_offset);
6927*56bb7041Schristos       if (!bfd_set_start_address (link_info.output_bfd, val))
6928*56bb7041Schristos 	einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name);
6929*56bb7041Schristos     }
6930*56bb7041Schristos   else
6931*56bb7041Schristos     {
6932*56bb7041Schristos       bfd_vma val;
6933*56bb7041Schristos       const char *send;
6934*56bb7041Schristos 
6935*56bb7041Schristos       /* We couldn't find the entry symbol.  Try parsing it as a
6936*56bb7041Schristos 	 number.  */
6937*56bb7041Schristos       val = bfd_scan_vma (entry_symbol.name, &send, 0);
6938*56bb7041Schristos       if (*send == '\0')
6939*56bb7041Schristos 	{
6940*56bb7041Schristos 	  if (!bfd_set_start_address (link_info.output_bfd, val))
6941*56bb7041Schristos 	    einfo (_("%F%P: can't set start address\n"));
6942*56bb7041Schristos 	}
6943*56bb7041Schristos       else
6944*56bb7041Schristos 	{
6945*56bb7041Schristos 	  asection *ts;
6946*56bb7041Schristos 
6947*56bb7041Schristos 	  /* Can't find the entry symbol, and it's not a number.  Use
6948*56bb7041Schristos 	     the first address in the text section.  */
6949*56bb7041Schristos 	  ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
6950*56bb7041Schristos 	  if (ts != NULL)
6951*56bb7041Schristos 	    {
6952*56bb7041Schristos 	      if (warn)
6953*56bb7041Schristos 		einfo (_("%P: warning: cannot find entry symbol %s;"
6954*56bb7041Schristos 			 " defaulting to %V\n"),
6955*56bb7041Schristos 		       entry_symbol.name,
6956*56bb7041Schristos 		       bfd_section_vma (ts));
6957*56bb7041Schristos 	      if (!bfd_set_start_address (link_info.output_bfd,
6958*56bb7041Schristos 					  bfd_section_vma (ts)))
6959*56bb7041Schristos 		einfo (_("%F%P: can't set start address\n"));
6960*56bb7041Schristos 	    }
6961*56bb7041Schristos 	  else
6962*56bb7041Schristos 	    {
6963*56bb7041Schristos 	      if (warn)
6964*56bb7041Schristos 		einfo (_("%P: warning: cannot find entry symbol %s;"
6965*56bb7041Schristos 			 " not setting start address\n"),
6966*56bb7041Schristos 		       entry_symbol.name);
6967*56bb7041Schristos 	    }
6968*56bb7041Schristos 	}
6969*56bb7041Schristos     }
6970*56bb7041Schristos }
6971*56bb7041Schristos 
6972*56bb7041Schristos /* This is a small function used when we want to ignore errors from
6973*56bb7041Schristos    BFD.  */
6974*56bb7041Schristos 
6975*56bb7041Schristos static void
ignore_bfd_errors(const char * fmt ATTRIBUTE_UNUSED,va_list ap ATTRIBUTE_UNUSED)6976*56bb7041Schristos ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED,
6977*56bb7041Schristos 		   va_list ap ATTRIBUTE_UNUSED)
6978*56bb7041Schristos {
6979*56bb7041Schristos   /* Don't do anything.  */
6980*56bb7041Schristos }
6981*56bb7041Schristos 
6982*56bb7041Schristos /* Check that the architecture of all the input files is compatible
6983*56bb7041Schristos    with the output file.  Also call the backend to let it do any
6984*56bb7041Schristos    other checking that is needed.  */
6985*56bb7041Schristos 
6986*56bb7041Schristos static void
lang_check(void)6987*56bb7041Schristos lang_check (void)
6988*56bb7041Schristos {
6989*56bb7041Schristos   lang_input_statement_type *file;
6990*56bb7041Schristos   bfd *input_bfd;
6991*56bb7041Schristos   const bfd_arch_info_type *compatible;
6992*56bb7041Schristos 
6993*56bb7041Schristos   for (file = (void *) file_chain.head;
6994*56bb7041Schristos        file != NULL;
6995*56bb7041Schristos        file = file->next)
6996*56bb7041Schristos     {
6997*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
6998*56bb7041Schristos       /* Don't check format of files claimed by plugin.  */
6999*56bb7041Schristos       if (file->flags.claimed)
7000*56bb7041Schristos 	continue;
7001*56bb7041Schristos #endif /* BFD_SUPPORTS_PLUGINS */
7002*56bb7041Schristos       input_bfd = file->the_bfd;
7003*56bb7041Schristos       compatible
7004*56bb7041Schristos 	= bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
7005*56bb7041Schristos 				   command_line.accept_unknown_input_arch);
7006*56bb7041Schristos 
7007*56bb7041Schristos       /* In general it is not possible to perform a relocatable
7008*56bb7041Schristos 	 link between differing object formats when the input
7009*56bb7041Schristos 	 file has relocations, because the relocations in the
7010*56bb7041Schristos 	 input format may not have equivalent representations in
7011*56bb7041Schristos 	 the output format (and besides BFD does not translate
7012*56bb7041Schristos 	 relocs for other link purposes than a final link).  */
7013*56bb7041Schristos       if (!file->flags.just_syms
7014*56bb7041Schristos 	  && (bfd_link_relocatable (&link_info)
7015*56bb7041Schristos 	      || link_info.emitrelocations)
7016*56bb7041Schristos 	  && (compatible == NULL
7017*56bb7041Schristos 	      || (bfd_get_flavour (input_bfd)
7018*56bb7041Schristos 		  != bfd_get_flavour (link_info.output_bfd)))
7019*56bb7041Schristos 	  && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
7020*56bb7041Schristos 	{
7021*56bb7041Schristos 	  einfo (_("%F%P: relocatable linking with relocations from"
7022*56bb7041Schristos 		   " format %s (%pB) to format %s (%pB) is not supported\n"),
7023*56bb7041Schristos 		 bfd_get_target (input_bfd), input_bfd,
7024*56bb7041Schristos 		 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
7025*56bb7041Schristos 	  /* einfo with %F exits.  */
7026*56bb7041Schristos 	}
7027*56bb7041Schristos 
7028*56bb7041Schristos       if (compatible == NULL)
7029*56bb7041Schristos 	{
7030*56bb7041Schristos 	  if (command_line.warn_mismatch)
7031*56bb7041Schristos 	    einfo (_("%X%P: %s architecture of input file `%pB'"
7032*56bb7041Schristos 		     " is incompatible with %s output\n"),
7033*56bb7041Schristos 		   bfd_printable_name (input_bfd), input_bfd,
7034*56bb7041Schristos 		   bfd_printable_name (link_info.output_bfd));
7035*56bb7041Schristos 	}
7036*56bb7041Schristos 
7037*56bb7041Schristos       /* If the input bfd has no contents, it shouldn't set the
7038*56bb7041Schristos 	 private data of the output bfd.  */
7039*56bb7041Schristos       else if (!file->flags.just_syms
7040*56bb7041Schristos 	       && ((input_bfd->flags & DYNAMIC) != 0
7041*56bb7041Schristos 		   || bfd_count_sections (input_bfd) != 0))
7042*56bb7041Schristos 	{
7043*56bb7041Schristos 	  bfd_error_handler_type pfn = NULL;
7044*56bb7041Schristos 
7045*56bb7041Schristos 	  /* If we aren't supposed to warn about mismatched input
7046*56bb7041Schristos 	     files, temporarily set the BFD error handler to a
7047*56bb7041Schristos 	     function which will do nothing.  We still want to call
7048*56bb7041Schristos 	     bfd_merge_private_bfd_data, since it may set up
7049*56bb7041Schristos 	     information which is needed in the output file.  */
7050*56bb7041Schristos 	  if (!command_line.warn_mismatch)
7051*56bb7041Schristos 	    pfn = bfd_set_error_handler (ignore_bfd_errors);
7052*56bb7041Schristos 	  if (!bfd_merge_private_bfd_data (input_bfd, &link_info))
7053*56bb7041Schristos 	    {
7054*56bb7041Schristos 	      if (command_line.warn_mismatch)
7055*56bb7041Schristos 		einfo (_("%X%P: failed to merge target specific data"
7056*56bb7041Schristos 			 " of file %pB\n"), input_bfd);
7057*56bb7041Schristos 	    }
7058*56bb7041Schristos 	  if (!command_line.warn_mismatch)
7059*56bb7041Schristos 	    bfd_set_error_handler (pfn);
7060*56bb7041Schristos 	}
7061*56bb7041Schristos     }
7062*56bb7041Schristos }
7063*56bb7041Schristos 
7064*56bb7041Schristos /* Look through all the global common symbols and attach them to the
7065*56bb7041Schristos    correct section.  The -sort-common command line switch may be used
7066*56bb7041Schristos    to roughly sort the entries by alignment.  */
7067*56bb7041Schristos 
7068*56bb7041Schristos static void
lang_common(void)7069*56bb7041Schristos lang_common (void)
7070*56bb7041Schristos {
7071*56bb7041Schristos   if (link_info.inhibit_common_definition)
7072*56bb7041Schristos     return;
7073*56bb7041Schristos   if (bfd_link_relocatable (&link_info)
7074*56bb7041Schristos       && !command_line.force_common_definition)
7075*56bb7041Schristos     return;
7076*56bb7041Schristos 
7077*56bb7041Schristos   if (!config.sort_common)
7078*56bb7041Schristos     bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
7079*56bb7041Schristos   else
7080*56bb7041Schristos     {
7081*56bb7041Schristos       unsigned int power;
7082*56bb7041Schristos 
7083*56bb7041Schristos       if (config.sort_common == sort_descending)
7084*56bb7041Schristos 	{
7085*56bb7041Schristos 	  for (power = 4; power > 0; power--)
7086*56bb7041Schristos 	    bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7087*56bb7041Schristos 
7088*56bb7041Schristos 	  power = 0;
7089*56bb7041Schristos 	  bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7090*56bb7041Schristos 	}
7091*56bb7041Schristos       else
7092*56bb7041Schristos 	{
7093*56bb7041Schristos 	  for (power = 0; power <= 4; power++)
7094*56bb7041Schristos 	    bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7095*56bb7041Schristos 
7096*56bb7041Schristos 	  power = (unsigned int) -1;
7097*56bb7041Schristos 	  bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7098*56bb7041Schristos 	}
7099*56bb7041Schristos     }
7100*56bb7041Schristos }
7101*56bb7041Schristos 
7102*56bb7041Schristos /* Place one common symbol in the correct section.  */
7103*56bb7041Schristos 
7104*56bb7041Schristos static bfd_boolean
lang_one_common(struct bfd_link_hash_entry * h,void * info)7105*56bb7041Schristos lang_one_common (struct bfd_link_hash_entry *h, void *info)
7106*56bb7041Schristos {
7107*56bb7041Schristos   unsigned int power_of_two;
7108*56bb7041Schristos   bfd_vma size;
7109*56bb7041Schristos   asection *section;
7110*56bb7041Schristos 
7111*56bb7041Schristos   if (h->type != bfd_link_hash_common)
7112*56bb7041Schristos     return TRUE;
7113*56bb7041Schristos 
7114*56bb7041Schristos   size = h->u.c.size;
7115*56bb7041Schristos   power_of_two = h->u.c.p->alignment_power;
7116*56bb7041Schristos 
7117*56bb7041Schristos   if (config.sort_common == sort_descending
7118*56bb7041Schristos       && power_of_two < *(unsigned int *) info)
7119*56bb7041Schristos     return TRUE;
7120*56bb7041Schristos   else if (config.sort_common == sort_ascending
7121*56bb7041Schristos 	   && power_of_two > *(unsigned int *) info)
7122*56bb7041Schristos     return TRUE;
7123*56bb7041Schristos 
7124*56bb7041Schristos   section = h->u.c.p->section;
7125*56bb7041Schristos   if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
7126*56bb7041Schristos     einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7127*56bb7041Schristos 	   h->root.string);
7128*56bb7041Schristos 
7129*56bb7041Schristos   if (config.map_file != NULL)
7130*56bb7041Schristos     {
7131*56bb7041Schristos       static bfd_boolean header_printed;
7132*56bb7041Schristos       int len;
7133*56bb7041Schristos       char *name;
7134*56bb7041Schristos       char buf[50];
7135*56bb7041Schristos 
7136*56bb7041Schristos       if (!header_printed)
7137*56bb7041Schristos 	{
7138*56bb7041Schristos 	  minfo (_("\nAllocating common symbols\n"));
7139*56bb7041Schristos 	  minfo (_("Common symbol       size              file\n\n"));
7140*56bb7041Schristos 	  header_printed = TRUE;
7141*56bb7041Schristos 	}
7142*56bb7041Schristos 
7143*56bb7041Schristos       name = bfd_demangle (link_info.output_bfd, h->root.string,
7144*56bb7041Schristos 			   DMGL_ANSI | DMGL_PARAMS);
7145*56bb7041Schristos       if (name == NULL)
7146*56bb7041Schristos 	{
7147*56bb7041Schristos 	  minfo ("%s", h->root.string);
7148*56bb7041Schristos 	  len = strlen (h->root.string);
7149*56bb7041Schristos 	}
7150*56bb7041Schristos       else
7151*56bb7041Schristos 	{
7152*56bb7041Schristos 	  minfo ("%s", name);
7153*56bb7041Schristos 	  len = strlen (name);
7154*56bb7041Schristos 	  free (name);
7155*56bb7041Schristos 	}
7156*56bb7041Schristos 
7157*56bb7041Schristos       if (len >= 19)
7158*56bb7041Schristos 	{
7159*56bb7041Schristos 	  print_nl ();
7160*56bb7041Schristos 	  len = 0;
7161*56bb7041Schristos 	}
7162*56bb7041Schristos       while (len < 20)
7163*56bb7041Schristos 	{
7164*56bb7041Schristos 	  print_space ();
7165*56bb7041Schristos 	  ++len;
7166*56bb7041Schristos 	}
7167*56bb7041Schristos 
7168*56bb7041Schristos       minfo ("0x");
7169*56bb7041Schristos       if (size <= 0xffffffff)
7170*56bb7041Schristos 	sprintf (buf, "%lx", (unsigned long) size);
7171*56bb7041Schristos       else
7172*56bb7041Schristos 	sprintf_vma (buf, size);
7173*56bb7041Schristos       minfo ("%s", buf);
7174*56bb7041Schristos       len = strlen (buf);
7175*56bb7041Schristos 
7176*56bb7041Schristos       while (len < 16)
7177*56bb7041Schristos 	{
7178*56bb7041Schristos 	  print_space ();
7179*56bb7041Schristos 	  ++len;
7180*56bb7041Schristos 	}
7181*56bb7041Schristos 
7182*56bb7041Schristos       minfo ("%pB\n", section->owner);
7183*56bb7041Schristos     }
7184*56bb7041Schristos 
7185*56bb7041Schristos   return TRUE;
7186*56bb7041Schristos }
7187*56bb7041Schristos 
7188*56bb7041Schristos /* Handle a single orphan section S, placing the orphan into an appropriate
7189*56bb7041Schristos    output section.  The effects of the --orphan-handling command line
7190*56bb7041Schristos    option are handled here.  */
7191*56bb7041Schristos 
7192*56bb7041Schristos static void
ldlang_place_orphan(asection * s)7193*56bb7041Schristos ldlang_place_orphan (asection *s)
7194*56bb7041Schristos {
7195*56bb7041Schristos   if (config.orphan_handling == orphan_handling_discard)
7196*56bb7041Schristos     {
7197*56bb7041Schristos       lang_output_section_statement_type *os;
7198*56bb7041Schristos       os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0,
7199*56bb7041Schristos 						 TRUE);
7200*56bb7041Schristos       if (os->addr_tree == NULL
7201*56bb7041Schristos 	  && (bfd_link_relocatable (&link_info)
7202*56bb7041Schristos 	      || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
7203*56bb7041Schristos 	os->addr_tree = exp_intop (0);
7204*56bb7041Schristos       lang_add_section (&os->children, s, NULL, os);
7205*56bb7041Schristos     }
7206*56bb7041Schristos   else
7207*56bb7041Schristos     {
7208*56bb7041Schristos       lang_output_section_statement_type *os;
7209*56bb7041Schristos       const char *name = s->name;
7210*56bb7041Schristos       int constraint = 0;
7211*56bb7041Schristos 
7212*56bb7041Schristos       if (config.orphan_handling == orphan_handling_error)
7213*56bb7041Schristos 	einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7214*56bb7041Schristos 	       s, s->owner);
7215*56bb7041Schristos 
7216*56bb7041Schristos       if (config.unique_orphan_sections || unique_section_p (s, NULL))
7217*56bb7041Schristos 	constraint = SPECIAL;
7218*56bb7041Schristos 
7219*56bb7041Schristos       os = ldemul_place_orphan (s, name, constraint);
7220*56bb7041Schristos       if (os == NULL)
7221*56bb7041Schristos 	{
7222*56bb7041Schristos 	  os = lang_output_section_statement_lookup (name, constraint, TRUE);
7223*56bb7041Schristos 	  if (os->addr_tree == NULL
7224*56bb7041Schristos 	      && (bfd_link_relocatable (&link_info)
7225*56bb7041Schristos 		  || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
7226*56bb7041Schristos 	    os->addr_tree = exp_intop (0);
7227*56bb7041Schristos 	  lang_add_section (&os->children, s, NULL, os);
7228*56bb7041Schristos 	}
7229*56bb7041Schristos 
7230*56bb7041Schristos       if (config.orphan_handling == orphan_handling_warn)
7231*56bb7041Schristos 	einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7232*56bb7041Schristos 		 "placed in section `%s'\n"),
7233*56bb7041Schristos 	       s, s->owner, os->name);
7234*56bb7041Schristos     }
7235*56bb7041Schristos }
7236*56bb7041Schristos 
7237*56bb7041Schristos /* Run through the input files and ensure that every input section has
7238*56bb7041Schristos    somewhere to go.  If one is found without a destination then create
7239*56bb7041Schristos    an input request and place it into the statement tree.  */
7240*56bb7041Schristos 
7241*56bb7041Schristos static void
lang_place_orphans(void)7242*56bb7041Schristos lang_place_orphans (void)
7243*56bb7041Schristos {
7244*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (file)
7245*56bb7041Schristos     {
7246*56bb7041Schristos       asection *s;
7247*56bb7041Schristos 
7248*56bb7041Schristos       for (s = file->the_bfd->sections; s != NULL; s = s->next)
7249*56bb7041Schristos 	{
7250*56bb7041Schristos 	  if (s->output_section == NULL)
7251*56bb7041Schristos 	    {
7252*56bb7041Schristos 	      /* This section of the file is not attached, root
7253*56bb7041Schristos 		 around for a sensible place for it to go.  */
7254*56bb7041Schristos 
7255*56bb7041Schristos 	      if (file->flags.just_syms)
7256*56bb7041Schristos 		bfd_link_just_syms (file->the_bfd, s, &link_info);
7257*56bb7041Schristos 	      else if (lang_discard_section_p (s))
7258*56bb7041Schristos 		s->output_section = bfd_abs_section_ptr;
7259*56bb7041Schristos 	      else if (strcmp (s->name, "COMMON") == 0)
7260*56bb7041Schristos 		{
7261*56bb7041Schristos 		  /* This is a lonely common section which must have
7262*56bb7041Schristos 		     come from an archive.  We attach to the section
7263*56bb7041Schristos 		     with the wildcard.  */
7264*56bb7041Schristos 		  if (!bfd_link_relocatable (&link_info)
7265*56bb7041Schristos 		      || command_line.force_common_definition)
7266*56bb7041Schristos 		    {
7267*56bb7041Schristos 		      if (default_common_section == NULL)
7268*56bb7041Schristos 			default_common_section
7269*56bb7041Schristos 			  = lang_output_section_statement_lookup (".bss", 0,
7270*56bb7041Schristos 								  TRUE);
7271*56bb7041Schristos 		      lang_add_section (&default_common_section->children, s,
7272*56bb7041Schristos 					NULL, default_common_section);
7273*56bb7041Schristos 		    }
7274*56bb7041Schristos 		}
7275*56bb7041Schristos 	      else
7276*56bb7041Schristos 		ldlang_place_orphan (s);
7277*56bb7041Schristos 	    }
7278*56bb7041Schristos 	}
7279*56bb7041Schristos     }
7280*56bb7041Schristos }
7281*56bb7041Schristos 
7282*56bb7041Schristos void
lang_set_flags(lang_memory_region_type * ptr,const char * flags,int invert)7283*56bb7041Schristos lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
7284*56bb7041Schristos {
7285*56bb7041Schristos   flagword *ptr_flags;
7286*56bb7041Schristos 
7287*56bb7041Schristos   ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
7288*56bb7041Schristos 
7289*56bb7041Schristos   while (*flags)
7290*56bb7041Schristos     {
7291*56bb7041Schristos       switch (*flags)
7292*56bb7041Schristos 	{
7293*56bb7041Schristos 	  /* PR 17900: An exclamation mark in the attributes reverses
7294*56bb7041Schristos 	     the sense of any of the attributes that follow.  */
7295*56bb7041Schristos 	case '!':
7296*56bb7041Schristos 	  invert = !invert;
7297*56bb7041Schristos 	  ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
7298*56bb7041Schristos 	  break;
7299*56bb7041Schristos 
7300*56bb7041Schristos 	case 'A': case 'a':
7301*56bb7041Schristos 	  *ptr_flags |= SEC_ALLOC;
7302*56bb7041Schristos 	  break;
7303*56bb7041Schristos 
7304*56bb7041Schristos 	case 'R': case 'r':
7305*56bb7041Schristos 	  *ptr_flags |= SEC_READONLY;
7306*56bb7041Schristos 	  break;
7307*56bb7041Schristos 
7308*56bb7041Schristos 	case 'W': case 'w':
7309*56bb7041Schristos 	  *ptr_flags |= SEC_DATA;
7310*56bb7041Schristos 	  break;
7311*56bb7041Schristos 
7312*56bb7041Schristos 	case 'X': case 'x':
7313*56bb7041Schristos 	  *ptr_flags |= SEC_CODE;
7314*56bb7041Schristos 	  break;
7315*56bb7041Schristos 
7316*56bb7041Schristos 	case 'L': case 'l':
7317*56bb7041Schristos 	case 'I': case 'i':
7318*56bb7041Schristos 	  *ptr_flags |= SEC_LOAD;
7319*56bb7041Schristos 	  break;
7320*56bb7041Schristos 
7321*56bb7041Schristos 	default:
7322*56bb7041Schristos 	  einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7323*56bb7041Schristos 		 *flags, *flags);
7324*56bb7041Schristos 	  break;
7325*56bb7041Schristos 	}
7326*56bb7041Schristos       flags++;
7327*56bb7041Schristos     }
7328*56bb7041Schristos }
7329*56bb7041Schristos 
7330*56bb7041Schristos /* Call a function on each real input file.  This function will be
7331*56bb7041Schristos    called on an archive, but not on the elements.  */
7332*56bb7041Schristos 
7333*56bb7041Schristos void
lang_for_each_input_file(void (* func)(lang_input_statement_type *))7334*56bb7041Schristos lang_for_each_input_file (void (*func) (lang_input_statement_type *))
7335*56bb7041Schristos {
7336*56bb7041Schristos   lang_input_statement_type *f;
7337*56bb7041Schristos 
7338*56bb7041Schristos   for (f = (void *) input_file_chain.head;
7339*56bb7041Schristos        f != NULL;
7340*56bb7041Schristos        f = f->next_real_file)
7341*56bb7041Schristos     if (f->flags.real)
7342*56bb7041Schristos       func (f);
7343*56bb7041Schristos }
7344*56bb7041Schristos 
7345*56bb7041Schristos /* Call a function on each real file.  The function will be called on
7346*56bb7041Schristos    all the elements of an archive which are included in the link, but
7347*56bb7041Schristos    will not be called on the archive file itself.  */
7348*56bb7041Schristos 
7349*56bb7041Schristos void
lang_for_each_file(void (* func)(lang_input_statement_type *))7350*56bb7041Schristos lang_for_each_file (void (*func) (lang_input_statement_type *))
7351*56bb7041Schristos {
7352*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (f)
7353*56bb7041Schristos     {
7354*56bb7041Schristos       if (f->flags.real)
7355*56bb7041Schristos 	func (f);
7356*56bb7041Schristos     }
7357*56bb7041Schristos }
7358*56bb7041Schristos 
7359*56bb7041Schristos void
ldlang_add_file(lang_input_statement_type * entry)7360*56bb7041Schristos ldlang_add_file (lang_input_statement_type *entry)
7361*56bb7041Schristos {
7362*56bb7041Schristos   lang_statement_append (&file_chain, entry, &entry->next);
7363*56bb7041Schristos 
7364*56bb7041Schristos   /* The BFD linker needs to have a list of all input BFDs involved in
7365*56bb7041Schristos      a link.  */
7366*56bb7041Schristos   ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next
7367*56bb7041Schristos 	  && entry->the_bfd->link.next == NULL);
7368*56bb7041Schristos   ASSERT (entry->the_bfd != link_info.output_bfd);
7369*56bb7041Schristos 
7370*56bb7041Schristos   *link_info.input_bfds_tail = entry->the_bfd;
7371*56bb7041Schristos   link_info.input_bfds_tail = &entry->the_bfd->link.next;
7372*56bb7041Schristos   bfd_set_usrdata (entry->the_bfd, entry);
7373*56bb7041Schristos   bfd_set_gp_size (entry->the_bfd, g_switch_value);
7374*56bb7041Schristos 
7375*56bb7041Schristos   /* Look through the sections and check for any which should not be
7376*56bb7041Schristos      included in the link.  We need to do this now, so that we can
7377*56bb7041Schristos      notice when the backend linker tries to report multiple
7378*56bb7041Schristos      definition errors for symbols which are in sections we aren't
7379*56bb7041Schristos      going to link.  FIXME: It might be better to entirely ignore
7380*56bb7041Schristos      symbols which are defined in sections which are going to be
7381*56bb7041Schristos      discarded.  This would require modifying the backend linker for
7382*56bb7041Schristos      each backend which might set the SEC_LINK_ONCE flag.  If we do
7383*56bb7041Schristos      this, we should probably handle SEC_EXCLUDE in the same way.  */
7384*56bb7041Schristos 
7385*56bb7041Schristos   bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
7386*56bb7041Schristos }
7387*56bb7041Schristos 
7388*56bb7041Schristos void
lang_add_output(const char * name,int from_script)7389*56bb7041Schristos lang_add_output (const char *name, int from_script)
7390*56bb7041Schristos {
7391*56bb7041Schristos   /* Make -o on command line override OUTPUT in script.  */
7392*56bb7041Schristos   if (!had_output_filename || !from_script)
7393*56bb7041Schristos     {
7394*56bb7041Schristos       output_filename = name;
7395*56bb7041Schristos       had_output_filename = TRUE;
7396*56bb7041Schristos     }
7397*56bb7041Schristos }
7398*56bb7041Schristos 
7399*56bb7041Schristos 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,int constraint,int align_with_input)7400*56bb7041Schristos lang_enter_output_section_statement (const char *output_section_statement_name,
7401*56bb7041Schristos 				     etree_type *address_exp,
7402*56bb7041Schristos 				     enum section_type sectype,
7403*56bb7041Schristos 				     etree_type *align,
7404*56bb7041Schristos 				     etree_type *subalign,
7405*56bb7041Schristos 				     etree_type *ebase,
7406*56bb7041Schristos 				     int constraint,
7407*56bb7041Schristos 				     int align_with_input)
7408*56bb7041Schristos {
7409*56bb7041Schristos   lang_output_section_statement_type *os;
7410*56bb7041Schristos 
7411*56bb7041Schristos   os = lang_output_section_statement_lookup (output_section_statement_name,
7412*56bb7041Schristos 					     constraint, TRUE);
7413*56bb7041Schristos   current_section = os;
7414*56bb7041Schristos 
7415*56bb7041Schristos   if (os->addr_tree == NULL)
7416*56bb7041Schristos     {
7417*56bb7041Schristos       os->addr_tree = address_exp;
7418*56bb7041Schristos     }
7419*56bb7041Schristos   os->sectype = sectype;
7420*56bb7041Schristos   if (sectype != noload_section)
7421*56bb7041Schristos     os->flags = SEC_NO_FLAGS;
7422*56bb7041Schristos   else
7423*56bb7041Schristos     os->flags = SEC_NEVER_LOAD;
7424*56bb7041Schristos   os->block_value = 1;
7425*56bb7041Schristos 
7426*56bb7041Schristos   /* Make next things chain into subchain of this.  */
7427*56bb7041Schristos   push_stat_ptr (&os->children);
7428*56bb7041Schristos 
7429*56bb7041Schristos   os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT;
7430*56bb7041Schristos   if (os->align_lma_with_input && align != NULL)
7431*56bb7041Schristos     einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7432*56bb7041Schristos 	   NULL);
7433*56bb7041Schristos 
7434*56bb7041Schristos   os->subsection_alignment = subalign;
7435*56bb7041Schristos   os->section_alignment = align;
7436*56bb7041Schristos 
7437*56bb7041Schristos   os->load_base = ebase;
7438*56bb7041Schristos   return os;
7439*56bb7041Schristos }
7440*56bb7041Schristos 
7441*56bb7041Schristos void
lang_final(void)7442*56bb7041Schristos lang_final (void)
7443*56bb7041Schristos {
7444*56bb7041Schristos   lang_output_statement_type *new_stmt;
7445*56bb7041Schristos 
7446*56bb7041Schristos   new_stmt = new_stat (lang_output_statement, stat_ptr);
7447*56bb7041Schristos   new_stmt->name = output_filename;
7448*56bb7041Schristos }
7449*56bb7041Schristos 
7450*56bb7041Schristos /* Reset the current counters in the regions.  */
7451*56bb7041Schristos 
7452*56bb7041Schristos void
lang_reset_memory_regions(void)7453*56bb7041Schristos lang_reset_memory_regions (void)
7454*56bb7041Schristos {
7455*56bb7041Schristos   lang_memory_region_type *p = lang_memory_region_list;
7456*56bb7041Schristos   asection *o;
7457*56bb7041Schristos   lang_output_section_statement_type *os;
7458*56bb7041Schristos 
7459*56bb7041Schristos   for (p = lang_memory_region_list; p != NULL; p = p->next)
7460*56bb7041Schristos     {
7461*56bb7041Schristos       p->current = p->origin;
7462*56bb7041Schristos       p->last_os = NULL;
7463*56bb7041Schristos     }
7464*56bb7041Schristos 
7465*56bb7041Schristos   for (os = (void *) lang_os_list.head;
7466*56bb7041Schristos        os != NULL;
7467*56bb7041Schristos        os = os->next)
7468*56bb7041Schristos     {
7469*56bb7041Schristos       os->processed_vma = FALSE;
7470*56bb7041Schristos       os->processed_lma = FALSE;
7471*56bb7041Schristos     }
7472*56bb7041Schristos 
7473*56bb7041Schristos   for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
7474*56bb7041Schristos     {
7475*56bb7041Schristos       /* Save the last size for possible use by bfd_relax_section.  */
7476*56bb7041Schristos       o->rawsize = o->size;
7477*56bb7041Schristos       if (!(o->flags & SEC_FIXED_SIZE))
7478*56bb7041Schristos 	o->size = 0;
7479*56bb7041Schristos     }
7480*56bb7041Schristos }
7481*56bb7041Schristos 
7482*56bb7041Schristos /* Worker for lang_gc_sections_1.  */
7483*56bb7041Schristos 
7484*56bb7041Schristos static void
gc_section_callback(lang_wild_statement_type * ptr,struct wildcard_list * sec ATTRIBUTE_UNUSED,asection * section,struct flag_info * sflag_info ATTRIBUTE_UNUSED,lang_input_statement_type * file ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)7485*56bb7041Schristos gc_section_callback (lang_wild_statement_type *ptr,
7486*56bb7041Schristos 		     struct wildcard_list *sec ATTRIBUTE_UNUSED,
7487*56bb7041Schristos 		     asection *section,
7488*56bb7041Schristos 		     struct flag_info *sflag_info ATTRIBUTE_UNUSED,
7489*56bb7041Schristos 		     lang_input_statement_type *file ATTRIBUTE_UNUSED,
7490*56bb7041Schristos 		     void *data ATTRIBUTE_UNUSED)
7491*56bb7041Schristos {
7492*56bb7041Schristos   /* If the wild pattern was marked KEEP, the member sections
7493*56bb7041Schristos      should be as well.  */
7494*56bb7041Schristos   if (ptr->keep_sections)
7495*56bb7041Schristos     section->flags |= SEC_KEEP;
7496*56bb7041Schristos }
7497*56bb7041Schristos 
7498*56bb7041Schristos /* Iterate over sections marking them against GC.  */
7499*56bb7041Schristos 
7500*56bb7041Schristos static void
lang_gc_sections_1(lang_statement_union_type * s)7501*56bb7041Schristos lang_gc_sections_1 (lang_statement_union_type *s)
7502*56bb7041Schristos {
7503*56bb7041Schristos   for (; s != NULL; s = s->header.next)
7504*56bb7041Schristos     {
7505*56bb7041Schristos       switch (s->header.type)
7506*56bb7041Schristos 	{
7507*56bb7041Schristos 	case lang_wild_statement_enum:
7508*56bb7041Schristos 	  walk_wild (&s->wild_statement, gc_section_callback, NULL);
7509*56bb7041Schristos 	  break;
7510*56bb7041Schristos 	case lang_constructors_statement_enum:
7511*56bb7041Schristos 	  lang_gc_sections_1 (constructor_list.head);
7512*56bb7041Schristos 	  break;
7513*56bb7041Schristos 	case lang_output_section_statement_enum:
7514*56bb7041Schristos 	  lang_gc_sections_1 (s->output_section_statement.children.head);
7515*56bb7041Schristos 	  break;
7516*56bb7041Schristos 	case lang_group_statement_enum:
7517*56bb7041Schristos 	  lang_gc_sections_1 (s->group_statement.children.head);
7518*56bb7041Schristos 	  break;
7519*56bb7041Schristos 	default:
7520*56bb7041Schristos 	  break;
7521*56bb7041Schristos 	}
7522*56bb7041Schristos     }
7523*56bb7041Schristos }
7524*56bb7041Schristos 
7525*56bb7041Schristos static void
lang_gc_sections(void)7526*56bb7041Schristos lang_gc_sections (void)
7527*56bb7041Schristos {
7528*56bb7041Schristos   /* Keep all sections so marked in the link script.  */
7529*56bb7041Schristos   lang_gc_sections_1 (statement_list.head);
7530*56bb7041Schristos 
7531*56bb7041Schristos   /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7532*56bb7041Schristos      the special case of debug info.  (See bfd/stabs.c)
7533*56bb7041Schristos      Twiddle the flag here, to simplify later linker code.  */
7534*56bb7041Schristos   if (bfd_link_relocatable (&link_info))
7535*56bb7041Schristos     {
7536*56bb7041Schristos       LANG_FOR_EACH_INPUT_STATEMENT (f)
7537*56bb7041Schristos 	{
7538*56bb7041Schristos 	  asection *sec;
7539*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
7540*56bb7041Schristos 	  if (f->flags.claimed)
7541*56bb7041Schristos 	    continue;
7542*56bb7041Schristos #endif
7543*56bb7041Schristos 	  for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
7544*56bb7041Schristos 	    if ((sec->flags & SEC_DEBUGGING) == 0)
7545*56bb7041Schristos 	      sec->flags &= ~SEC_EXCLUDE;
7546*56bb7041Schristos 	}
7547*56bb7041Schristos     }
7548*56bb7041Schristos 
7549*56bb7041Schristos   if (link_info.gc_sections)
7550*56bb7041Schristos     bfd_gc_sections (link_info.output_bfd, &link_info);
7551*56bb7041Schristos }
7552*56bb7041Schristos 
7553*56bb7041Schristos /* Worker for lang_find_relro_sections_1.  */
7554*56bb7041Schristos 
7555*56bb7041Schristos static void
find_relro_section_callback(lang_wild_statement_type * ptr ATTRIBUTE_UNUSED,struct wildcard_list * sec ATTRIBUTE_UNUSED,asection * section,struct flag_info * sflag_info ATTRIBUTE_UNUSED,lang_input_statement_type * file ATTRIBUTE_UNUSED,void * data)7556*56bb7041Schristos find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
7557*56bb7041Schristos 			     struct wildcard_list *sec ATTRIBUTE_UNUSED,
7558*56bb7041Schristos 			     asection *section,
7559*56bb7041Schristos 			     struct flag_info *sflag_info ATTRIBUTE_UNUSED,
7560*56bb7041Schristos 			     lang_input_statement_type *file ATTRIBUTE_UNUSED,
7561*56bb7041Schristos 			     void *data)
7562*56bb7041Schristos {
7563*56bb7041Schristos   /* Discarded, excluded and ignored sections effectively have zero
7564*56bb7041Schristos      size.  */
7565*56bb7041Schristos   if (section->output_section != NULL
7566*56bb7041Schristos       && section->output_section->owner == link_info.output_bfd
7567*56bb7041Schristos       && (section->output_section->flags & SEC_EXCLUDE) == 0
7568*56bb7041Schristos       && !IGNORE_SECTION (section)
7569*56bb7041Schristos       && section->size != 0)
7570*56bb7041Schristos     {
7571*56bb7041Schristos       bfd_boolean *has_relro_section = (bfd_boolean *) data;
7572*56bb7041Schristos       *has_relro_section = TRUE;
7573*56bb7041Schristos     }
7574*56bb7041Schristos }
7575*56bb7041Schristos 
7576*56bb7041Schristos /* Iterate over sections for relro sections.  */
7577*56bb7041Schristos 
7578*56bb7041Schristos static void
lang_find_relro_sections_1(lang_statement_union_type * s,seg_align_type * seg,bfd_boolean * has_relro_section)7579*56bb7041Schristos lang_find_relro_sections_1 (lang_statement_union_type *s,
7580*56bb7041Schristos 			    seg_align_type *seg,
7581*56bb7041Schristos 			    bfd_boolean *has_relro_section)
7582*56bb7041Schristos {
7583*56bb7041Schristos   if (*has_relro_section)
7584*56bb7041Schristos     return;
7585*56bb7041Schristos 
7586*56bb7041Schristos   for (; s != NULL; s = s->header.next)
7587*56bb7041Schristos     {
7588*56bb7041Schristos       if (s == seg->relro_end_stat)
7589*56bb7041Schristos 	break;
7590*56bb7041Schristos 
7591*56bb7041Schristos       switch (s->header.type)
7592*56bb7041Schristos 	{
7593*56bb7041Schristos 	case lang_wild_statement_enum:
7594*56bb7041Schristos 	  walk_wild (&s->wild_statement,
7595*56bb7041Schristos 		     find_relro_section_callback,
7596*56bb7041Schristos 		     has_relro_section);
7597*56bb7041Schristos 	  break;
7598*56bb7041Schristos 	case lang_constructors_statement_enum:
7599*56bb7041Schristos 	  lang_find_relro_sections_1 (constructor_list.head,
7600*56bb7041Schristos 				      seg, has_relro_section);
7601*56bb7041Schristos 	  break;
7602*56bb7041Schristos 	case lang_output_section_statement_enum:
7603*56bb7041Schristos 	  lang_find_relro_sections_1 (s->output_section_statement.children.head,
7604*56bb7041Schristos 				      seg, has_relro_section);
7605*56bb7041Schristos 	  break;
7606*56bb7041Schristos 	case lang_group_statement_enum:
7607*56bb7041Schristos 	  lang_find_relro_sections_1 (s->group_statement.children.head,
7608*56bb7041Schristos 				      seg, has_relro_section);
7609*56bb7041Schristos 	  break;
7610*56bb7041Schristos 	default:
7611*56bb7041Schristos 	  break;
7612*56bb7041Schristos 	}
7613*56bb7041Schristos     }
7614*56bb7041Schristos }
7615*56bb7041Schristos 
7616*56bb7041Schristos static void
lang_find_relro_sections(void)7617*56bb7041Schristos lang_find_relro_sections (void)
7618*56bb7041Schristos {
7619*56bb7041Schristos   bfd_boolean has_relro_section = FALSE;
7620*56bb7041Schristos 
7621*56bb7041Schristos   /* Check all sections in the link script.  */
7622*56bb7041Schristos 
7623*56bb7041Schristos   lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
7624*56bb7041Schristos 			      &expld.dataseg, &has_relro_section);
7625*56bb7041Schristos 
7626*56bb7041Schristos   if (!has_relro_section)
7627*56bb7041Schristos     link_info.relro = FALSE;
7628*56bb7041Schristos }
7629*56bb7041Schristos 
7630*56bb7041Schristos /* Relax all sections until bfd_relax_section gives up.  */
7631*56bb7041Schristos 
7632*56bb7041Schristos void
lang_relax_sections(bfd_boolean need_layout)7633*56bb7041Schristos lang_relax_sections (bfd_boolean need_layout)
7634*56bb7041Schristos {
7635*56bb7041Schristos   if (RELAXATION_ENABLED)
7636*56bb7041Schristos     {
7637*56bb7041Schristos       /* We may need more than one relaxation pass.  */
7638*56bb7041Schristos       int i = link_info.relax_pass;
7639*56bb7041Schristos 
7640*56bb7041Schristos       /* The backend can use it to determine the current pass.  */
7641*56bb7041Schristos       link_info.relax_pass = 0;
7642*56bb7041Schristos 
7643*56bb7041Schristos       while (i--)
7644*56bb7041Schristos 	{
7645*56bb7041Schristos 	  /* Keep relaxing until bfd_relax_section gives up.  */
7646*56bb7041Schristos 	  bfd_boolean relax_again;
7647*56bb7041Schristos 
7648*56bb7041Schristos 	  link_info.relax_trip = -1;
7649*56bb7041Schristos 	  do
7650*56bb7041Schristos 	    {
7651*56bb7041Schristos 	      link_info.relax_trip++;
7652*56bb7041Schristos 
7653*56bb7041Schristos 	      /* Note: pe-dll.c does something like this also.  If you find
7654*56bb7041Schristos 		 you need to change this code, you probably need to change
7655*56bb7041Schristos 		 pe-dll.c also.  DJ  */
7656*56bb7041Schristos 
7657*56bb7041Schristos 	      /* Do all the assignments with our current guesses as to
7658*56bb7041Schristos 		 section sizes.  */
7659*56bb7041Schristos 	      lang_do_assignments (lang_assigning_phase_enum);
7660*56bb7041Schristos 
7661*56bb7041Schristos 	      /* We must do this after lang_do_assignments, because it uses
7662*56bb7041Schristos 		 size.  */
7663*56bb7041Schristos 	      lang_reset_memory_regions ();
7664*56bb7041Schristos 
7665*56bb7041Schristos 	      /* Perform another relax pass - this time we know where the
7666*56bb7041Schristos 		 globals are, so can make a better guess.  */
7667*56bb7041Schristos 	      relax_again = FALSE;
7668*56bb7041Schristos 	      lang_size_sections (&relax_again, FALSE);
7669*56bb7041Schristos 	    }
7670*56bb7041Schristos 	  while (relax_again);
7671*56bb7041Schristos 
7672*56bb7041Schristos 	  link_info.relax_pass++;
7673*56bb7041Schristos 	}
7674*56bb7041Schristos       need_layout = TRUE;
7675*56bb7041Schristos     }
7676*56bb7041Schristos 
7677*56bb7041Schristos   if (need_layout)
7678*56bb7041Schristos     {
7679*56bb7041Schristos       /* Final extra sizing to report errors.  */
7680*56bb7041Schristos       lang_do_assignments (lang_assigning_phase_enum);
7681*56bb7041Schristos       lang_reset_memory_regions ();
7682*56bb7041Schristos       lang_size_sections (NULL, TRUE);
7683*56bb7041Schristos     }
7684*56bb7041Schristos }
7685*56bb7041Schristos 
7686*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
7687*56bb7041Schristos /* Find the insert point for the plugin's replacement files.  We
7688*56bb7041Schristos    place them after the first claimed real object file, or if the
7689*56bb7041Schristos    first claimed object is an archive member, after the last real
7690*56bb7041Schristos    object file immediately preceding the archive.  In the event
7691*56bb7041Schristos    no objects have been claimed at all, we return the first dummy
7692*56bb7041Schristos    object file on the list as the insert point; that works, but
7693*56bb7041Schristos    the callee must be careful when relinking the file_chain as it
7694*56bb7041Schristos    is not actually on that chain, only the statement_list and the
7695*56bb7041Schristos    input_file list; in that case, the replacement files must be
7696*56bb7041Schristos    inserted at the head of the file_chain.  */
7697*56bb7041Schristos 
7698*56bb7041Schristos static lang_input_statement_type *
find_replacements_insert_point(bfd_boolean * before)7699*56bb7041Schristos find_replacements_insert_point (bfd_boolean *before)
7700*56bb7041Schristos {
7701*56bb7041Schristos   lang_input_statement_type *claim1, *lastobject;
7702*56bb7041Schristos   lastobject = (void *) input_file_chain.head;
7703*56bb7041Schristos   for (claim1 = (void *) file_chain.head;
7704*56bb7041Schristos        claim1 != NULL;
7705*56bb7041Schristos        claim1 = claim1->next)
7706*56bb7041Schristos     {
7707*56bb7041Schristos       if (claim1->flags.claimed)
7708*56bb7041Schristos 	{
7709*56bb7041Schristos 	  *before = claim1->flags.claim_archive;
7710*56bb7041Schristos 	  return claim1->flags.claim_archive ? lastobject : claim1;
7711*56bb7041Schristos 	}
7712*56bb7041Schristos       /* Update lastobject if this is a real object file.  */
7713*56bb7041Schristos       if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL)
7714*56bb7041Schristos 	lastobject = claim1;
7715*56bb7041Schristos     }
7716*56bb7041Schristos   /* No files were claimed by the plugin.  Choose the last object
7717*56bb7041Schristos      file found on the list (maybe the first, dummy entry) as the
7718*56bb7041Schristos      insert point.  */
7719*56bb7041Schristos   *before = FALSE;
7720*56bb7041Schristos   return lastobject;
7721*56bb7041Schristos }
7722*56bb7041Schristos 
7723*56bb7041Schristos /* Find where to insert ADD, an archive element or shared library
7724*56bb7041Schristos    added during a rescan.  */
7725*56bb7041Schristos 
7726*56bb7041Schristos static lang_input_statement_type **
find_rescan_insertion(lang_input_statement_type * add)7727*56bb7041Schristos find_rescan_insertion (lang_input_statement_type *add)
7728*56bb7041Schristos {
7729*56bb7041Schristos   bfd *add_bfd = add->the_bfd;
7730*56bb7041Schristos   lang_input_statement_type *f;
7731*56bb7041Schristos   lang_input_statement_type *last_loaded = NULL;
7732*56bb7041Schristos   lang_input_statement_type *before = NULL;
7733*56bb7041Schristos   lang_input_statement_type **iter = NULL;
7734*56bb7041Schristos 
7735*56bb7041Schristos   if (add_bfd->my_archive != NULL)
7736*56bb7041Schristos     add_bfd = add_bfd->my_archive;
7737*56bb7041Schristos 
7738*56bb7041Schristos   /* First look through the input file chain, to find an object file
7739*56bb7041Schristos      before the one we've rescanned.  Normal object files always
7740*56bb7041Schristos      appear on both the input file chain and the file chain, so this
7741*56bb7041Schristos      lets us get quickly to somewhere near the correct place on the
7742*56bb7041Schristos      file chain if it is full of archive elements.  Archives don't
7743*56bb7041Schristos      appear on the file chain, but if an element has been extracted
7744*56bb7041Schristos      then their input_statement->next points at it.  */
7745*56bb7041Schristos   for (f = (void *) input_file_chain.head;
7746*56bb7041Schristos        f != NULL;
7747*56bb7041Schristos        f = f->next_real_file)
7748*56bb7041Schristos     {
7749*56bb7041Schristos       if (f->the_bfd == add_bfd)
7750*56bb7041Schristos 	{
7751*56bb7041Schristos 	  before = last_loaded;
7752*56bb7041Schristos 	  if (f->next != NULL)
7753*56bb7041Schristos 	    return &f->next->next;
7754*56bb7041Schristos 	}
7755*56bb7041Schristos       if (f->the_bfd != NULL && f->next != NULL)
7756*56bb7041Schristos 	last_loaded = f;
7757*56bb7041Schristos     }
7758*56bb7041Schristos 
7759*56bb7041Schristos   for (iter = before ? &before->next : &file_chain.head->input_statement.next;
7760*56bb7041Schristos        *iter != NULL;
7761*56bb7041Schristos        iter = &(*iter)->next)
7762*56bb7041Schristos     if (!(*iter)->flags.claim_archive
7763*56bb7041Schristos 	&& (*iter)->the_bfd->my_archive == NULL)
7764*56bb7041Schristos       break;
7765*56bb7041Schristos 
7766*56bb7041Schristos   return iter;
7767*56bb7041Schristos }
7768*56bb7041Schristos 
7769*56bb7041Schristos /* Insert SRCLIST into DESTLIST after given element by chaining
7770*56bb7041Schristos    on FIELD as the next-pointer.  (Counterintuitively does not need
7771*56bb7041Schristos    a pointer to the actual after-node itself, just its chain field.)  */
7772*56bb7041Schristos 
7773*56bb7041Schristos static void
lang_list_insert_after(lang_statement_list_type * destlist,lang_statement_list_type * srclist,lang_statement_union_type ** field)7774*56bb7041Schristos lang_list_insert_after (lang_statement_list_type *destlist,
7775*56bb7041Schristos 			lang_statement_list_type *srclist,
7776*56bb7041Schristos 			lang_statement_union_type **field)
7777*56bb7041Schristos {
7778*56bb7041Schristos   *(srclist->tail) = *field;
7779*56bb7041Schristos   *field = srclist->head;
7780*56bb7041Schristos   if (destlist->tail == field)
7781*56bb7041Schristos     destlist->tail = srclist->tail;
7782*56bb7041Schristos }
7783*56bb7041Schristos 
7784*56bb7041Schristos /* Detach new nodes added to DESTLIST since the time ORIGLIST
7785*56bb7041Schristos    was taken as a copy of it and leave them in ORIGLIST.  */
7786*56bb7041Schristos 
7787*56bb7041Schristos static void
lang_list_remove_tail(lang_statement_list_type * destlist,lang_statement_list_type * origlist)7788*56bb7041Schristos lang_list_remove_tail (lang_statement_list_type *destlist,
7789*56bb7041Schristos 		       lang_statement_list_type *origlist)
7790*56bb7041Schristos {
7791*56bb7041Schristos   union lang_statement_union **savetail;
7792*56bb7041Schristos   /* Check that ORIGLIST really is an earlier state of DESTLIST.  */
7793*56bb7041Schristos   ASSERT (origlist->head == destlist->head);
7794*56bb7041Schristos   savetail = origlist->tail;
7795*56bb7041Schristos   origlist->head = *(savetail);
7796*56bb7041Schristos   origlist->tail = destlist->tail;
7797*56bb7041Schristos   destlist->tail = savetail;
7798*56bb7041Schristos   *savetail = NULL;
7799*56bb7041Schristos }
7800*56bb7041Schristos 
7801*56bb7041Schristos static lang_statement_union_type **
find_next_input_statement(lang_statement_union_type ** s)7802*56bb7041Schristos find_next_input_statement (lang_statement_union_type **s)
7803*56bb7041Schristos {
7804*56bb7041Schristos   for ( ; *s; s = &(*s)->header.next)
7805*56bb7041Schristos     {
7806*56bb7041Schristos       lang_statement_union_type **t;
7807*56bb7041Schristos       switch ((*s)->header.type)
7808*56bb7041Schristos 	{
7809*56bb7041Schristos 	case lang_input_statement_enum:
7810*56bb7041Schristos 	  return s;
7811*56bb7041Schristos 	case lang_wild_statement_enum:
7812*56bb7041Schristos 	  t = &(*s)->wild_statement.children.head;
7813*56bb7041Schristos 	  break;
7814*56bb7041Schristos 	case lang_group_statement_enum:
7815*56bb7041Schristos 	  t = &(*s)->group_statement.children.head;
7816*56bb7041Schristos 	  break;
7817*56bb7041Schristos 	case lang_output_section_statement_enum:
7818*56bb7041Schristos 	  t = &(*s)->output_section_statement.children.head;
7819*56bb7041Schristos 	  break;
7820*56bb7041Schristos 	default:
7821*56bb7041Schristos 	  continue;
7822*56bb7041Schristos 	}
7823*56bb7041Schristos       t = find_next_input_statement (t);
7824*56bb7041Schristos       if (*t)
7825*56bb7041Schristos 	return t;
7826*56bb7041Schristos     }
7827*56bb7041Schristos   return s;
7828*56bb7041Schristos }
7829*56bb7041Schristos #endif /* BFD_SUPPORTS_PLUGINS */
7830*56bb7041Schristos 
7831*56bb7041Schristos /* Add NAME to the list of garbage collection entry points.  */
7832*56bb7041Schristos 
7833*56bb7041Schristos void
lang_add_gc_name(const char * name)7834*56bb7041Schristos lang_add_gc_name (const char *name)
7835*56bb7041Schristos {
7836*56bb7041Schristos   struct bfd_sym_chain *sym;
7837*56bb7041Schristos 
7838*56bb7041Schristos   if (name == NULL)
7839*56bb7041Schristos     return;
7840*56bb7041Schristos 
7841*56bb7041Schristos   sym = stat_alloc (sizeof (*sym));
7842*56bb7041Schristos 
7843*56bb7041Schristos   sym->next = link_info.gc_sym_list;
7844*56bb7041Schristos   sym->name = name;
7845*56bb7041Schristos   link_info.gc_sym_list = sym;
7846*56bb7041Schristos }
7847*56bb7041Schristos 
7848*56bb7041Schristos /* Check relocations.  */
7849*56bb7041Schristos 
7850*56bb7041Schristos static void
lang_check_relocs(void)7851*56bb7041Schristos lang_check_relocs (void)
7852*56bb7041Schristos {
7853*56bb7041Schristos   if (link_info.check_relocs_after_open_input)
7854*56bb7041Schristos     {
7855*56bb7041Schristos       bfd *abfd;
7856*56bb7041Schristos 
7857*56bb7041Schristos       for (abfd = link_info.input_bfds;
7858*56bb7041Schristos 	   abfd != (bfd *) NULL; abfd = abfd->link.next)
7859*56bb7041Schristos 	if (!bfd_link_check_relocs (abfd, &link_info))
7860*56bb7041Schristos 	  {
7861*56bb7041Schristos 	    /* No object output, fail return.  */
7862*56bb7041Schristos 	    config.make_executable = FALSE;
7863*56bb7041Schristos 	    /* Note: we do not abort the loop, but rather
7864*56bb7041Schristos 	       continue the scan in case there are other
7865*56bb7041Schristos 	       bad relocations to report.  */
7866*56bb7041Schristos 	  }
7867*56bb7041Schristos     }
7868*56bb7041Schristos }
7869*56bb7041Schristos 
7870*56bb7041Schristos /* Look through all output sections looking for places where we can
7871*56bb7041Schristos    propagate forward the lma region.  */
7872*56bb7041Schristos 
7873*56bb7041Schristos static void
lang_propagate_lma_regions(void)7874*56bb7041Schristos lang_propagate_lma_regions (void)
7875*56bb7041Schristos {
7876*56bb7041Schristos   lang_output_section_statement_type *os;
7877*56bb7041Schristos 
7878*56bb7041Schristos   for (os = (void *) lang_os_list.head;
7879*56bb7041Schristos        os != NULL;
7880*56bb7041Schristos        os = os->next)
7881*56bb7041Schristos     {
7882*56bb7041Schristos       if (os->prev != NULL
7883*56bb7041Schristos 	  && os->lma_region == NULL
7884*56bb7041Schristos 	  && os->load_base == NULL
7885*56bb7041Schristos 	  && os->addr_tree == NULL
7886*56bb7041Schristos 	  && os->region == os->prev->region)
7887*56bb7041Schristos 	os->lma_region = os->prev->lma_region;
7888*56bb7041Schristos     }
7889*56bb7041Schristos }
7890*56bb7041Schristos 
7891*56bb7041Schristos void
lang_process(void)7892*56bb7041Schristos lang_process (void)
7893*56bb7041Schristos {
7894*56bb7041Schristos   /* Finalize dynamic list.  */
7895*56bb7041Schristos   if (link_info.dynamic_list)
7896*56bb7041Schristos     lang_finalize_version_expr_head (&link_info.dynamic_list->head);
7897*56bb7041Schristos 
7898*56bb7041Schristos   current_target = default_target;
7899*56bb7041Schristos 
7900*56bb7041Schristos   /* Open the output file.  */
7901*56bb7041Schristos   lang_for_each_statement (ldlang_open_output);
7902*56bb7041Schristos   init_opb (NULL);
7903*56bb7041Schristos 
7904*56bb7041Schristos   ldemul_create_output_section_statements ();
7905*56bb7041Schristos 
7906*56bb7041Schristos   /* Add to the hash table all undefineds on the command line.  */
7907*56bb7041Schristos   lang_place_undefineds ();
7908*56bb7041Schristos 
7909*56bb7041Schristos   if (!bfd_section_already_linked_table_init ())
7910*56bb7041Schristos     einfo (_("%F%P: can not create hash table: %E\n"));
7911*56bb7041Schristos 
7912*56bb7041Schristos   /* Create a bfd for each input file.  */
7913*56bb7041Schristos   current_target = default_target;
7914*56bb7041Schristos   lang_statement_iteration++;
7915*56bb7041Schristos   open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
7916*56bb7041Schristos   /* open_input_bfds also handles assignments, so we can give values
7917*56bb7041Schristos      to symbolic origin/length now.  */
7918*56bb7041Schristos   lang_do_memory_regions ();
7919*56bb7041Schristos 
7920*56bb7041Schristos #if BFD_SUPPORTS_PLUGINS
7921*56bb7041Schristos   if (link_info.lto_plugin_active)
7922*56bb7041Schristos     {
7923*56bb7041Schristos       lang_statement_list_type added;
7924*56bb7041Schristos       lang_statement_list_type files, inputfiles;
7925*56bb7041Schristos 
7926*56bb7041Schristos       /* Now all files are read, let the plugin(s) decide if there
7927*56bb7041Schristos 	 are any more to be added to the link before we call the
7928*56bb7041Schristos 	 emulation's after_open hook.  We create a private list of
7929*56bb7041Schristos 	 input statements for this purpose, which we will eventually
7930*56bb7041Schristos 	 insert into the global statement list after the first claimed
7931*56bb7041Schristos 	 file.  */
7932*56bb7041Schristos       added = *stat_ptr;
7933*56bb7041Schristos       /* We need to manipulate all three chains in synchrony.  */
7934*56bb7041Schristos       files = file_chain;
7935*56bb7041Schristos       inputfiles = input_file_chain;
7936*56bb7041Schristos       if (plugin_call_all_symbols_read ())
7937*56bb7041Schristos 	einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7938*56bb7041Schristos 	       plugin_error_plugin ());
7939*56bb7041Schristos       link_info.lto_all_symbols_read = TRUE;
7940*56bb7041Schristos       /* Open any newly added files, updating the file chains.  */
7941*56bb7041Schristos       plugin_undefs = link_info.hash->undefs_tail;
7942*56bb7041Schristos       open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
7943*56bb7041Schristos       if (plugin_undefs == link_info.hash->undefs_tail)
7944*56bb7041Schristos 	plugin_undefs = NULL;
7945*56bb7041Schristos       /* Restore the global list pointer now they have all been added.  */
7946*56bb7041Schristos       lang_list_remove_tail (stat_ptr, &added);
7947*56bb7041Schristos       /* And detach the fresh ends of the file lists.  */
7948*56bb7041Schristos       lang_list_remove_tail (&file_chain, &files);
7949*56bb7041Schristos       lang_list_remove_tail (&input_file_chain, &inputfiles);
7950*56bb7041Schristos       /* Were any new files added?  */
7951*56bb7041Schristos       if (added.head != NULL)
7952*56bb7041Schristos 	{
7953*56bb7041Schristos 	  /* If so, we will insert them into the statement list immediately
7954*56bb7041Schristos 	     after the first input file that was claimed by the plugin,
7955*56bb7041Schristos 	     unless that file was an archive in which case it is inserted
7956*56bb7041Schristos 	     immediately before.  */
7957*56bb7041Schristos 	  bfd_boolean before;
7958*56bb7041Schristos 	  lang_statement_union_type **prev;
7959*56bb7041Schristos 	  plugin_insert = find_replacements_insert_point (&before);
7960*56bb7041Schristos 	  /* If a plugin adds input files without having claimed any, we
7961*56bb7041Schristos 	     don't really have a good idea where to place them.  Just putting
7962*56bb7041Schristos 	     them at the start or end of the list is liable to leave them
7963*56bb7041Schristos 	     outside the crtbegin...crtend range.  */
7964*56bb7041Schristos 	  ASSERT (plugin_insert != NULL);
7965*56bb7041Schristos 	  /* Splice the new statement list into the old one.  */
7966*56bb7041Schristos 	  prev = &plugin_insert->header.next;
7967*56bb7041Schristos 	  if (before)
7968*56bb7041Schristos 	    {
7969*56bb7041Schristos 	      prev = find_next_input_statement (prev);
7970*56bb7041Schristos 	      if (*prev != (void *) plugin_insert->next_real_file)
7971*56bb7041Schristos 		{
7972*56bb7041Schristos 		  /* We didn't find the expected input statement.
7973*56bb7041Schristos 		     Fall back to adding after plugin_insert.  */
7974*56bb7041Schristos 		  prev = &plugin_insert->header.next;
7975*56bb7041Schristos 		}
7976*56bb7041Schristos 	    }
7977*56bb7041Schristos 	  lang_list_insert_after (stat_ptr, &added, prev);
7978*56bb7041Schristos 	  /* Likewise for the file chains.  */
7979*56bb7041Schristos 	  lang_list_insert_after (&input_file_chain, &inputfiles,
7980*56bb7041Schristos 				  (void *) &plugin_insert->next_real_file);
7981*56bb7041Schristos 	  /* We must be careful when relinking file_chain; we may need to
7982*56bb7041Schristos 	     insert the new files at the head of the list if the insert
7983*56bb7041Schristos 	     point chosen is the dummy first input file.  */
7984*56bb7041Schristos 	  if (plugin_insert->filename)
7985*56bb7041Schristos 	    lang_list_insert_after (&file_chain, &files,
7986*56bb7041Schristos 				    (void *) &plugin_insert->next);
7987*56bb7041Schristos 	  else
7988*56bb7041Schristos 	    lang_list_insert_after (&file_chain, &files, &file_chain.head);
7989*56bb7041Schristos 
7990*56bb7041Schristos 	  /* Rescan archives in case new undefined symbols have appeared.  */
7991*56bb7041Schristos 	  files = file_chain;
7992*56bb7041Schristos 	  lang_statement_iteration++;
7993*56bb7041Schristos 	  open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
7994*56bb7041Schristos 	  lang_list_remove_tail (&file_chain, &files);
7995*56bb7041Schristos 	  while (files.head != NULL)
7996*56bb7041Schristos 	    {
7997*56bb7041Schristos 	      lang_input_statement_type **insert;
7998*56bb7041Schristos 	      lang_input_statement_type **iter, *temp;
7999*56bb7041Schristos 	      bfd *my_arch;
8000*56bb7041Schristos 
8001*56bb7041Schristos 	      insert = find_rescan_insertion (&files.head->input_statement);
8002*56bb7041Schristos 	      /* All elements from an archive can be added at once.  */
8003*56bb7041Schristos 	      iter = &files.head->input_statement.next;
8004*56bb7041Schristos 	      my_arch = files.head->input_statement.the_bfd->my_archive;
8005*56bb7041Schristos 	      if (my_arch != NULL)
8006*56bb7041Schristos 		for (; *iter != NULL; iter = &(*iter)->next)
8007*56bb7041Schristos 		  if ((*iter)->the_bfd->my_archive != my_arch)
8008*56bb7041Schristos 		    break;
8009*56bb7041Schristos 	      temp = *insert;
8010*56bb7041Schristos 	      *insert = &files.head->input_statement;
8011*56bb7041Schristos 	      files.head = (lang_statement_union_type *) *iter;
8012*56bb7041Schristos 	      *iter = temp;
8013*56bb7041Schristos 	      if (my_arch != NULL)
8014*56bb7041Schristos 		{
8015*56bb7041Schristos 		  lang_input_statement_type *parent = bfd_usrdata (my_arch);
8016*56bb7041Schristos 		  if (parent != NULL)
8017*56bb7041Schristos 		    parent->next = (lang_input_statement_type *)
8018*56bb7041Schristos 		      ((char *) iter
8019*56bb7041Schristos 		       - offsetof (lang_input_statement_type, next));
8020*56bb7041Schristos 		}
8021*56bb7041Schristos 	    }
8022*56bb7041Schristos 	}
8023*56bb7041Schristos     }
8024*56bb7041Schristos #endif /* BFD_SUPPORTS_PLUGINS */
8025*56bb7041Schristos 
8026*56bb7041Schristos   /* Make sure that nobody has tried to add a symbol to this list
8027*56bb7041Schristos      before now.  */
8028*56bb7041Schristos   ASSERT (link_info.gc_sym_list == NULL);
8029*56bb7041Schristos 
8030*56bb7041Schristos   link_info.gc_sym_list = &entry_symbol;
8031*56bb7041Schristos 
8032*56bb7041Schristos   if (entry_symbol.name == NULL)
8033*56bb7041Schristos     {
8034*56bb7041Schristos       link_info.gc_sym_list = ldlang_undef_chain_list_head;
8035*56bb7041Schristos 
8036*56bb7041Schristos       /* entry_symbol is normally initialied by a ENTRY definition in the
8037*56bb7041Schristos 	 linker script or the -e command line option.  But if neither of
8038*56bb7041Schristos 	 these have been used, the target specific backend may still have
8039*56bb7041Schristos 	 provided an entry symbol via a call to lang_default_entry().
8040*56bb7041Schristos 	 Unfortunately this value will not be processed until lang_end()
8041*56bb7041Schristos 	 is called, long after this function has finished.  So detect this
8042*56bb7041Schristos 	 case here and add the target's entry symbol to the list of starting
8043*56bb7041Schristos 	 points for garbage collection resolution.  */
8044*56bb7041Schristos       lang_add_gc_name (entry_symbol_default);
8045*56bb7041Schristos     }
8046*56bb7041Schristos 
8047*56bb7041Schristos   lang_add_gc_name (link_info.init_function);
8048*56bb7041Schristos   lang_add_gc_name (link_info.fini_function);
8049*56bb7041Schristos 
8050*56bb7041Schristos   ldemul_after_open ();
8051*56bb7041Schristos   if (config.map_file != NULL)
8052*56bb7041Schristos     lang_print_asneeded ();
8053*56bb7041Schristos 
8054*56bb7041Schristos   ldlang_open_ctf ();
8055*56bb7041Schristos 
8056*56bb7041Schristos   bfd_section_already_linked_table_free ();
8057*56bb7041Schristos 
8058*56bb7041Schristos   /* Make sure that we're not mixing architectures.  We call this
8059*56bb7041Schristos      after all the input files have been opened, but before we do any
8060*56bb7041Schristos      other processing, so that any operations merge_private_bfd_data
8061*56bb7041Schristos      does on the output file will be known during the rest of the
8062*56bb7041Schristos      link.  */
8063*56bb7041Schristos   lang_check ();
8064*56bb7041Schristos 
8065*56bb7041Schristos   /* Handle .exports instead of a version script if we're told to do so.  */
8066*56bb7041Schristos   if (command_line.version_exports_section)
8067*56bb7041Schristos     lang_do_version_exports_section ();
8068*56bb7041Schristos 
8069*56bb7041Schristos   /* Build all sets based on the information gathered from the input
8070*56bb7041Schristos      files.  */
8071*56bb7041Schristos   ldctor_build_sets ();
8072*56bb7041Schristos 
8073*56bb7041Schristos   /* Give initial values for __start and __stop symbols, so that  ELF
8074*56bb7041Schristos      gc_sections will keep sections referenced by these symbols.  Must
8075*56bb7041Schristos      be done before lang_do_assignments below.  */
8076*56bb7041Schristos   if (config.build_constructors)
8077*56bb7041Schristos     lang_init_start_stop ();
8078*56bb7041Schristos 
8079*56bb7041Schristos   /* PR 13683: We must rerun the assignments prior to running garbage
8080*56bb7041Schristos      collection in order to make sure that all symbol aliases are resolved.  */
8081*56bb7041Schristos   lang_do_assignments (lang_mark_phase_enum);
8082*56bb7041Schristos   expld.phase = lang_first_phase_enum;
8083*56bb7041Schristos 
8084*56bb7041Schristos   /* Size up the common data.  */
8085*56bb7041Schristos   lang_common ();
8086*56bb7041Schristos 
8087*56bb7041Schristos   /* Remove unreferenced sections if asked to.  */
8088*56bb7041Schristos   lang_gc_sections ();
8089*56bb7041Schristos 
8090*56bb7041Schristos   /* Check relocations.  */
8091*56bb7041Schristos   lang_check_relocs ();
8092*56bb7041Schristos 
8093*56bb7041Schristos   ldemul_after_check_relocs ();
8094*56bb7041Schristos 
8095*56bb7041Schristos   /* Update wild statements.  */
8096*56bb7041Schristos   update_wild_statements (statement_list.head);
8097*56bb7041Schristos 
8098*56bb7041Schristos   /* Run through the contours of the script and attach input sections
8099*56bb7041Schristos      to the correct output sections.  */
8100*56bb7041Schristos   lang_statement_iteration++;
8101*56bb7041Schristos   map_input_to_output_sections (statement_list.head, NULL, NULL);
8102*56bb7041Schristos 
8103*56bb7041Schristos   /* Start at the statement immediately after the special abs_section
8104*56bb7041Schristos      output statement, so that it isn't reordered.  */
8105*56bb7041Schristos   process_insert_statements (&lang_os_list.head->header.next);
8106*56bb7041Schristos 
8107*56bb7041Schristos   ldemul_before_place_orphans ();
8108*56bb7041Schristos 
8109*56bb7041Schristos   /* Find any sections not attached explicitly and handle them.  */
8110*56bb7041Schristos   lang_place_orphans ();
8111*56bb7041Schristos 
8112*56bb7041Schristos   if (!bfd_link_relocatable (&link_info))
8113*56bb7041Schristos     {
8114*56bb7041Schristos       asection *found;
8115*56bb7041Schristos 
8116*56bb7041Schristos       /* Merge SEC_MERGE sections.  This has to be done after GC of
8117*56bb7041Schristos 	 sections, so that GCed sections are not merged, but before
8118*56bb7041Schristos 	 assigning dynamic symbols, since removing whole input sections
8119*56bb7041Schristos 	 is hard then.  */
8120*56bb7041Schristos       bfd_merge_sections (link_info.output_bfd, &link_info);
8121*56bb7041Schristos 
8122*56bb7041Schristos       /* Look for a text section and set the readonly attribute in it.  */
8123*56bb7041Schristos       found = bfd_get_section_by_name (link_info.output_bfd, ".text");
8124*56bb7041Schristos 
8125*56bb7041Schristos       if (found != NULL)
8126*56bb7041Schristos 	{
8127*56bb7041Schristos 	  if (config.text_read_only)
8128*56bb7041Schristos 	    found->flags |= SEC_READONLY;
8129*56bb7041Schristos 	  else
8130*56bb7041Schristos 	    found->flags &= ~SEC_READONLY;
8131*56bb7041Schristos 	}
8132*56bb7041Schristos     }
8133*56bb7041Schristos 
8134*56bb7041Schristos   /* Merge together CTF sections.  After this, only the symtab-dependent
8135*56bb7041Schristos      function and data object sections need adjustment.  */
8136*56bb7041Schristos   lang_merge_ctf ();
8137*56bb7041Schristos 
8138*56bb7041Schristos   /* Emit the CTF, iff the emulation doesn't need to do late emission after
8139*56bb7041Schristos      examining things laid out late, like the strtab.  */
8140*56bb7041Schristos   lang_write_ctf (0);
8141*56bb7041Schristos 
8142*56bb7041Schristos   /* Copy forward lma regions for output sections in same lma region.  */
8143*56bb7041Schristos   lang_propagate_lma_regions ();
8144*56bb7041Schristos 
8145*56bb7041Schristos   /* Defining __start/__stop symbols early for --gc-sections to work
8146*56bb7041Schristos      around a glibc build problem can result in these symbols being
8147*56bb7041Schristos      defined when they should not be.  Fix them now.  */
8148*56bb7041Schristos   if (config.build_constructors)
8149*56bb7041Schristos     lang_undef_start_stop ();
8150*56bb7041Schristos 
8151*56bb7041Schristos   /* Define .startof./.sizeof. symbols with preliminary values before
8152*56bb7041Schristos      dynamic symbols are created.  */
8153*56bb7041Schristos   if (!bfd_link_relocatable (&link_info))
8154*56bb7041Schristos     lang_init_startof_sizeof ();
8155*56bb7041Schristos 
8156*56bb7041Schristos   /* Do anything special before sizing sections.  This is where ELF
8157*56bb7041Schristos      and other back-ends size dynamic sections.  */
8158*56bb7041Schristos   ldemul_before_allocation ();
8159*56bb7041Schristos 
8160*56bb7041Schristos   /* We must record the program headers before we try to fix the
8161*56bb7041Schristos      section positions, since they will affect SIZEOF_HEADERS.  */
8162*56bb7041Schristos   lang_record_phdrs ();
8163*56bb7041Schristos 
8164*56bb7041Schristos   /* Check relro sections.  */
8165*56bb7041Schristos   if (link_info.relro && !bfd_link_relocatable (&link_info))
8166*56bb7041Schristos     lang_find_relro_sections ();
8167*56bb7041Schristos 
8168*56bb7041Schristos   /* Size up the sections.  */
8169*56bb7041Schristos   lang_size_sections (NULL, !RELAXATION_ENABLED);
8170*56bb7041Schristos 
8171*56bb7041Schristos   /* See if anything special should be done now we know how big
8172*56bb7041Schristos      everything is.  This is where relaxation is done.  */
8173*56bb7041Schristos   ldemul_after_allocation ();
8174*56bb7041Schristos 
8175*56bb7041Schristos   /* Fix any __start, __stop, .startof. or .sizeof. symbols.  */
8176*56bb7041Schristos   lang_finalize_start_stop ();
8177*56bb7041Schristos 
8178*56bb7041Schristos   /* Do all the assignments again, to report errors.  Assignment
8179*56bb7041Schristos      statements are processed multiple times, updating symbols; In
8180*56bb7041Schristos      open_input_bfds, lang_do_assignments, and lang_size_sections.
8181*56bb7041Schristos      Since lang_relax_sections calls lang_do_assignments, symbols are
8182*56bb7041Schristos      also updated in ldemul_after_allocation.  */
8183*56bb7041Schristos   lang_do_assignments (lang_final_phase_enum);
8184*56bb7041Schristos 
8185*56bb7041Schristos   ldemul_finish ();
8186*56bb7041Schristos 
8187*56bb7041Schristos   /* Convert absolute symbols to section relative.  */
8188*56bb7041Schristos   ldexp_finalize_syms ();
8189*56bb7041Schristos 
8190*56bb7041Schristos   /* Make sure that the section addresses make sense.  */
8191*56bb7041Schristos   if (command_line.check_section_addresses)
8192*56bb7041Schristos     lang_check_section_addresses ();
8193*56bb7041Schristos 
8194*56bb7041Schristos   /* Check any required symbols are known.  */
8195*56bb7041Schristos   ldlang_check_require_defined_symbols ();
8196*56bb7041Schristos 
8197*56bb7041Schristos   lang_end ();
8198*56bb7041Schristos }
8199*56bb7041Schristos 
8200*56bb7041Schristos /* EXPORTED TO YACC */
8201*56bb7041Schristos 
8202*56bb7041Schristos void
lang_add_wild(struct wildcard_spec * filespec,struct wildcard_list * section_list,bfd_boolean keep_sections)8203*56bb7041Schristos lang_add_wild (struct wildcard_spec *filespec,
8204*56bb7041Schristos 	       struct wildcard_list *section_list,
8205*56bb7041Schristos 	       bfd_boolean keep_sections)
8206*56bb7041Schristos {
8207*56bb7041Schristos   struct wildcard_list *curr, *next;
8208*56bb7041Schristos   lang_wild_statement_type *new_stmt;
8209*56bb7041Schristos 
8210*56bb7041Schristos   /* Reverse the list as the parser puts it back to front.  */
8211*56bb7041Schristos   for (curr = section_list, section_list = NULL;
8212*56bb7041Schristos        curr != NULL;
8213*56bb7041Schristos        section_list = curr, curr = next)
8214*56bb7041Schristos     {
8215*56bb7041Schristos       next = curr->next;
8216*56bb7041Schristos       curr->next = section_list;
8217*56bb7041Schristos     }
8218*56bb7041Schristos 
8219*56bb7041Schristos   if (filespec != NULL && filespec->name != NULL)
8220*56bb7041Schristos     {
8221*56bb7041Schristos       if (strcmp (filespec->name, "*") == 0)
8222*56bb7041Schristos 	filespec->name = NULL;
8223*56bb7041Schristos       else if (!wildcardp (filespec->name))
8224*56bb7041Schristos 	lang_has_input_file = TRUE;
8225*56bb7041Schristos     }
8226*56bb7041Schristos 
8227*56bb7041Schristos   new_stmt = new_stat (lang_wild_statement, stat_ptr);
8228*56bb7041Schristos   new_stmt->filename = NULL;
8229*56bb7041Schristos   new_stmt->filenames_sorted = FALSE;
8230*56bb7041Schristos   new_stmt->section_flag_list = NULL;
8231*56bb7041Schristos   new_stmt->exclude_name_list = NULL;
8232*56bb7041Schristos   if (filespec != NULL)
8233*56bb7041Schristos     {
8234*56bb7041Schristos       new_stmt->filename = filespec->name;
8235*56bb7041Schristos       new_stmt->filenames_sorted = filespec->sorted == by_name;
8236*56bb7041Schristos       new_stmt->section_flag_list = filespec->section_flag_list;
8237*56bb7041Schristos       new_stmt->exclude_name_list = filespec->exclude_name_list;
8238*56bb7041Schristos     }
8239*56bb7041Schristos   new_stmt->section_list = section_list;
8240*56bb7041Schristos   new_stmt->keep_sections = keep_sections;
8241*56bb7041Schristos   lang_list_init (&new_stmt->children);
8242*56bb7041Schristos   analyze_walk_wild_section_handler (new_stmt);
8243*56bb7041Schristos }
8244*56bb7041Schristos 
8245*56bb7041Schristos void
lang_section_start(const char * name,etree_type * address,const segment_type * segment)8246*56bb7041Schristos lang_section_start (const char *name, etree_type *address,
8247*56bb7041Schristos 		    const segment_type *segment)
8248*56bb7041Schristos {
8249*56bb7041Schristos   lang_address_statement_type *ad;
8250*56bb7041Schristos 
8251*56bb7041Schristos   ad = new_stat (lang_address_statement, stat_ptr);
8252*56bb7041Schristos   ad->section_name = name;
8253*56bb7041Schristos   ad->address = address;
8254*56bb7041Schristos   ad->segment = segment;
8255*56bb7041Schristos }
8256*56bb7041Schristos 
8257*56bb7041Schristos /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
8258*56bb7041Schristos    because of a -e argument on the command line, or zero if this is
8259*56bb7041Schristos    called by ENTRY in a linker script.  Command line arguments take
8260*56bb7041Schristos    precedence.  */
8261*56bb7041Schristos 
8262*56bb7041Schristos void
lang_add_entry(const char * name,bfd_boolean cmdline)8263*56bb7041Schristos lang_add_entry (const char *name, bfd_boolean cmdline)
8264*56bb7041Schristos {
8265*56bb7041Schristos   if (entry_symbol.name == NULL
8266*56bb7041Schristos       || cmdline
8267*56bb7041Schristos       || !entry_from_cmdline)
8268*56bb7041Schristos     {
8269*56bb7041Schristos       entry_symbol.name = name;
8270*56bb7041Schristos       entry_from_cmdline = cmdline;
8271*56bb7041Schristos     }
8272*56bb7041Schristos }
8273*56bb7041Schristos 
8274*56bb7041Schristos /* Set the default start symbol to NAME.  .em files should use this,
8275*56bb7041Schristos    not lang_add_entry, to override the use of "start" if neither the
8276*56bb7041Schristos    linker script nor the command line specifies an entry point.  NAME
8277*56bb7041Schristos    must be permanently allocated.  */
8278*56bb7041Schristos void
lang_default_entry(const char * name)8279*56bb7041Schristos lang_default_entry (const char *name)
8280*56bb7041Schristos {
8281*56bb7041Schristos   entry_symbol_default = name;
8282*56bb7041Schristos }
8283*56bb7041Schristos 
8284*56bb7041Schristos void
lang_add_target(const char * name)8285*56bb7041Schristos lang_add_target (const char *name)
8286*56bb7041Schristos {
8287*56bb7041Schristos   lang_target_statement_type *new_stmt;
8288*56bb7041Schristos 
8289*56bb7041Schristos   new_stmt = new_stat (lang_target_statement, stat_ptr);
8290*56bb7041Schristos   new_stmt->target = name;
8291*56bb7041Schristos }
8292*56bb7041Schristos 
8293*56bb7041Schristos void
lang_add_map(const char * name)8294*56bb7041Schristos lang_add_map (const char *name)
8295*56bb7041Schristos {
8296*56bb7041Schristos   while (*name)
8297*56bb7041Schristos     {
8298*56bb7041Schristos       switch (*name)
8299*56bb7041Schristos 	{
8300*56bb7041Schristos 	case 'F':
8301*56bb7041Schristos 	  map_option_f = TRUE;
8302*56bb7041Schristos 	  break;
8303*56bb7041Schristos 	}
8304*56bb7041Schristos       name++;
8305*56bb7041Schristos     }
8306*56bb7041Schristos }
8307*56bb7041Schristos 
8308*56bb7041Schristos void
lang_add_fill(fill_type * fill)8309*56bb7041Schristos lang_add_fill (fill_type *fill)
8310*56bb7041Schristos {
8311*56bb7041Schristos   lang_fill_statement_type *new_stmt;
8312*56bb7041Schristos 
8313*56bb7041Schristos   new_stmt = new_stat (lang_fill_statement, stat_ptr);
8314*56bb7041Schristos   new_stmt->fill = fill;
8315*56bb7041Schristos }
8316*56bb7041Schristos 
8317*56bb7041Schristos void
lang_add_data(int type,union etree_union * exp)8318*56bb7041Schristos lang_add_data (int type, union etree_union *exp)
8319*56bb7041Schristos {
8320*56bb7041Schristos   lang_data_statement_type *new_stmt;
8321*56bb7041Schristos 
8322*56bb7041Schristos   new_stmt = new_stat (lang_data_statement, stat_ptr);
8323*56bb7041Schristos   new_stmt->exp = exp;
8324*56bb7041Schristos   new_stmt->type = type;
8325*56bb7041Schristos }
8326*56bb7041Schristos 
8327*56bb7041Schristos /* Create a new reloc statement.  RELOC is the BFD relocation type to
8328*56bb7041Schristos    generate.  HOWTO is the corresponding howto structure (we could
8329*56bb7041Schristos    look this up, but the caller has already done so).  SECTION is the
8330*56bb7041Schristos    section to generate a reloc against, or NAME is the name of the
8331*56bb7041Schristos    symbol to generate a reloc against.  Exactly one of SECTION and
8332*56bb7041Schristos    NAME must be NULL.  ADDEND is an expression for the addend.  */
8333*56bb7041Schristos 
8334*56bb7041Schristos void
lang_add_reloc(bfd_reloc_code_real_type reloc,reloc_howto_type * howto,asection * section,const char * name,union etree_union * addend)8335*56bb7041Schristos lang_add_reloc (bfd_reloc_code_real_type reloc,
8336*56bb7041Schristos 		reloc_howto_type *howto,
8337*56bb7041Schristos 		asection *section,
8338*56bb7041Schristos 		const char *name,
8339*56bb7041Schristos 		union etree_union *addend)
8340*56bb7041Schristos {
8341*56bb7041Schristos   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
8342*56bb7041Schristos 
8343*56bb7041Schristos   p->reloc = reloc;
8344*56bb7041Schristos   p->howto = howto;
8345*56bb7041Schristos   p->section = section;
8346*56bb7041Schristos   p->name = name;
8347*56bb7041Schristos   p->addend_exp = addend;
8348*56bb7041Schristos 
8349*56bb7041Schristos   p->addend_value = 0;
8350*56bb7041Schristos   p->output_section = NULL;
8351*56bb7041Schristos   p->output_offset = 0;
8352*56bb7041Schristos }
8353*56bb7041Schristos 
8354*56bb7041Schristos lang_assignment_statement_type *
lang_add_assignment(etree_type * exp)8355*56bb7041Schristos lang_add_assignment (etree_type *exp)
8356*56bb7041Schristos {
8357*56bb7041Schristos   lang_assignment_statement_type *new_stmt;
8358*56bb7041Schristos 
8359*56bb7041Schristos   new_stmt = new_stat (lang_assignment_statement, stat_ptr);
8360*56bb7041Schristos   new_stmt->exp = exp;
8361*56bb7041Schristos   return new_stmt;
8362*56bb7041Schristos }
8363*56bb7041Schristos 
8364*56bb7041Schristos void
lang_add_attribute(enum statement_enum attribute)8365*56bb7041Schristos lang_add_attribute (enum statement_enum attribute)
8366*56bb7041Schristos {
8367*56bb7041Schristos   new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
8368*56bb7041Schristos }
8369*56bb7041Schristos 
8370*56bb7041Schristos void
lang_startup(const char * name)8371*56bb7041Schristos lang_startup (const char *name)
8372*56bb7041Schristos {
8373*56bb7041Schristos   if (first_file->filename != NULL)
8374*56bb7041Schristos     {
8375*56bb7041Schristos       einfo (_("%F%P: multiple STARTUP files\n"));
8376*56bb7041Schristos     }
8377*56bb7041Schristos   first_file->filename = name;
8378*56bb7041Schristos   first_file->local_sym_name = name;
8379*56bb7041Schristos   first_file->flags.real = TRUE;
8380*56bb7041Schristos }
8381*56bb7041Schristos 
8382*56bb7041Schristos void
lang_float(bfd_boolean maybe)8383*56bb7041Schristos lang_float (bfd_boolean maybe)
8384*56bb7041Schristos {
8385*56bb7041Schristos   lang_float_flag = maybe;
8386*56bb7041Schristos }
8387*56bb7041Schristos 
8388*56bb7041Schristos 
8389*56bb7041Schristos /* Work out the load- and run-time regions from a script statement, and
8390*56bb7041Schristos    store them in *LMA_REGION and *REGION respectively.
8391*56bb7041Schristos 
8392*56bb7041Schristos    MEMSPEC is the name of the run-time region, or the value of
8393*56bb7041Schristos    DEFAULT_MEMORY_REGION if the statement didn't specify one.
8394*56bb7041Schristos    LMA_MEMSPEC is the name of the load-time region, or null if the
8395*56bb7041Schristos    statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8396*56bb7041Schristos    had an explicit load address.
8397*56bb7041Schristos 
8398*56bb7041Schristos    It is an error to specify both a load region and a load address.  */
8399*56bb7041Schristos 
8400*56bb7041Schristos 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)8401*56bb7041Schristos lang_get_regions (lang_memory_region_type **region,
8402*56bb7041Schristos 		  lang_memory_region_type **lma_region,
8403*56bb7041Schristos 		  const char *memspec,
8404*56bb7041Schristos 		  const char *lma_memspec,
8405*56bb7041Schristos 		  bfd_boolean have_lma,
8406*56bb7041Schristos 		  bfd_boolean have_vma)
8407*56bb7041Schristos {
8408*56bb7041Schristos   *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
8409*56bb7041Schristos 
8410*56bb7041Schristos   /* If no runtime region or VMA has been specified, but the load region
8411*56bb7041Schristos      has been specified, then use the load region for the runtime region
8412*56bb7041Schristos      as well.  */
8413*56bb7041Schristos   if (lma_memspec != NULL
8414*56bb7041Schristos       && !have_vma
8415*56bb7041Schristos       && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
8416*56bb7041Schristos     *region = *lma_region;
8417*56bb7041Schristos   else
8418*56bb7041Schristos     *region = lang_memory_region_lookup (memspec, FALSE);
8419*56bb7041Schristos 
8420*56bb7041Schristos   if (have_lma && lma_memspec != 0)
8421*56bb7041Schristos     einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8422*56bb7041Schristos 	   NULL);
8423*56bb7041Schristos }
8424*56bb7041Schristos 
8425*56bb7041Schristos void
lang_leave_output_section_statement(fill_type * fill,const char * memspec,lang_output_section_phdr_list * phdrs,const char * lma_memspec)8426*56bb7041Schristos lang_leave_output_section_statement (fill_type *fill, const char *memspec,
8427*56bb7041Schristos 				     lang_output_section_phdr_list *phdrs,
8428*56bb7041Schristos 				     const char *lma_memspec)
8429*56bb7041Schristos {
8430*56bb7041Schristos   lang_get_regions (&current_section->region,
8431*56bb7041Schristos 		    &current_section->lma_region,
8432*56bb7041Schristos 		    memspec, lma_memspec,
8433*56bb7041Schristos 		    current_section->load_base != NULL,
8434*56bb7041Schristos 		    current_section->addr_tree != NULL);
8435*56bb7041Schristos 
8436*56bb7041Schristos   current_section->fill = fill;
8437*56bb7041Schristos   current_section->phdrs = phdrs;
8438*56bb7041Schristos   pop_stat_ptr ();
8439*56bb7041Schristos }
8440*56bb7041Schristos 
8441*56bb7041Schristos /* Set the output format type.  -oformat overrides scripts.  */
8442*56bb7041Schristos 
8443*56bb7041Schristos void
lang_add_output_format(const char * format,const char * big,const char * little,int from_script)8444*56bb7041Schristos lang_add_output_format (const char *format,
8445*56bb7041Schristos 			const char *big,
8446*56bb7041Schristos 			const char *little,
8447*56bb7041Schristos 			int from_script)
8448*56bb7041Schristos {
8449*56bb7041Schristos   if (output_target == NULL || !from_script)
8450*56bb7041Schristos     {
8451*56bb7041Schristos       if (command_line.endian == ENDIAN_BIG
8452*56bb7041Schristos 	  && big != NULL)
8453*56bb7041Schristos 	format = big;
8454*56bb7041Schristos       else if (command_line.endian == ENDIAN_LITTLE
8455*56bb7041Schristos 	       && little != NULL)
8456*56bb7041Schristos 	format = little;
8457*56bb7041Schristos 
8458*56bb7041Schristos       output_target = format;
8459*56bb7041Schristos     }
8460*56bb7041Schristos }
8461*56bb7041Schristos 
8462*56bb7041Schristos void
lang_add_insert(const char * where,int is_before)8463*56bb7041Schristos lang_add_insert (const char *where, int is_before)
8464*56bb7041Schristos {
8465*56bb7041Schristos   lang_insert_statement_type *new_stmt;
8466*56bb7041Schristos 
8467*56bb7041Schristos   new_stmt = new_stat (lang_insert_statement, stat_ptr);
8468*56bb7041Schristos   new_stmt->where = where;
8469*56bb7041Schristos   new_stmt->is_before = is_before;
8470*56bb7041Schristos   saved_script_handle = previous_script_handle;
8471*56bb7041Schristos }
8472*56bb7041Schristos 
8473*56bb7041Schristos /* Enter a group.  This creates a new lang_group_statement, and sets
8474*56bb7041Schristos    stat_ptr to build new statements within the group.  */
8475*56bb7041Schristos 
8476*56bb7041Schristos void
lang_enter_group(void)8477*56bb7041Schristos lang_enter_group (void)
8478*56bb7041Schristos {
8479*56bb7041Schristos   lang_group_statement_type *g;
8480*56bb7041Schristos 
8481*56bb7041Schristos   g = new_stat (lang_group_statement, stat_ptr);
8482*56bb7041Schristos   lang_list_init (&g->children);
8483*56bb7041Schristos   push_stat_ptr (&g->children);
8484*56bb7041Schristos }
8485*56bb7041Schristos 
8486*56bb7041Schristos /* Leave a group.  This just resets stat_ptr to start writing to the
8487*56bb7041Schristos    regular list of statements again.  Note that this will not work if
8488*56bb7041Schristos    groups can occur inside anything else which can adjust stat_ptr,
8489*56bb7041Schristos    but currently they can't.  */
8490*56bb7041Schristos 
8491*56bb7041Schristos void
lang_leave_group(void)8492*56bb7041Schristos lang_leave_group (void)
8493*56bb7041Schristos {
8494*56bb7041Schristos   pop_stat_ptr ();
8495*56bb7041Schristos }
8496*56bb7041Schristos 
8497*56bb7041Schristos /* Add a new program header.  This is called for each entry in a PHDRS
8498*56bb7041Schristos    command in a linker script.  */
8499*56bb7041Schristos 
8500*56bb7041Schristos void
lang_new_phdr(const char * name,etree_type * type,bfd_boolean filehdr,bfd_boolean phdrs,etree_type * at,etree_type * flags)8501*56bb7041Schristos lang_new_phdr (const char *name,
8502*56bb7041Schristos 	       etree_type *type,
8503*56bb7041Schristos 	       bfd_boolean filehdr,
8504*56bb7041Schristos 	       bfd_boolean phdrs,
8505*56bb7041Schristos 	       etree_type *at,
8506*56bb7041Schristos 	       etree_type *flags)
8507*56bb7041Schristos {
8508*56bb7041Schristos   struct lang_phdr *n, **pp;
8509*56bb7041Schristos   bfd_boolean hdrs;
8510*56bb7041Schristos 
8511*56bb7041Schristos   n = stat_alloc (sizeof (struct lang_phdr));
8512*56bb7041Schristos   n->next = NULL;
8513*56bb7041Schristos   n->name = name;
8514*56bb7041Schristos   n->type = exp_get_vma (type, 0, "program header type");
8515*56bb7041Schristos   n->filehdr = filehdr;
8516*56bb7041Schristos   n->phdrs = phdrs;
8517*56bb7041Schristos   n->at = at;
8518*56bb7041Schristos   n->flags = flags;
8519*56bb7041Schristos 
8520*56bb7041Schristos   hdrs = n->type == 1 && (phdrs || filehdr);
8521*56bb7041Schristos 
8522*56bb7041Schristos   for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
8523*56bb7041Schristos     if (hdrs
8524*56bb7041Schristos 	&& (*pp)->type == 1
8525*56bb7041Schristos 	&& !((*pp)->filehdr || (*pp)->phdrs))
8526*56bb7041Schristos       {
8527*56bb7041Schristos 	einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8528*56bb7041Schristos 		 " when prior PT_LOAD headers lack them\n"), NULL);
8529*56bb7041Schristos 	hdrs = FALSE;
8530*56bb7041Schristos       }
8531*56bb7041Schristos 
8532*56bb7041Schristos   *pp = n;
8533*56bb7041Schristos }
8534*56bb7041Schristos 
8535*56bb7041Schristos /* Record the program header information in the output BFD.  FIXME: We
8536*56bb7041Schristos    should not be calling an ELF specific function here.  */
8537*56bb7041Schristos 
8538*56bb7041Schristos static void
lang_record_phdrs(void)8539*56bb7041Schristos lang_record_phdrs (void)
8540*56bb7041Schristos {
8541*56bb7041Schristos   unsigned int alc;
8542*56bb7041Schristos   asection **secs;
8543*56bb7041Schristos   lang_output_section_phdr_list *last;
8544*56bb7041Schristos   struct lang_phdr *l;
8545*56bb7041Schristos   lang_output_section_statement_type *os;
8546*56bb7041Schristos 
8547*56bb7041Schristos   alc = 10;
8548*56bb7041Schristos   secs = (asection **) xmalloc (alc * sizeof (asection *));
8549*56bb7041Schristos   last = NULL;
8550*56bb7041Schristos 
8551*56bb7041Schristos   for (l = lang_phdr_list; l != NULL; l = l->next)
8552*56bb7041Schristos     {
8553*56bb7041Schristos       unsigned int c;
8554*56bb7041Schristos       flagword flags;
8555*56bb7041Schristos       bfd_vma at;
8556*56bb7041Schristos 
8557*56bb7041Schristos       c = 0;
8558*56bb7041Schristos       for (os = (void *) lang_os_list.head;
8559*56bb7041Schristos 	   os != NULL;
8560*56bb7041Schristos 	   os = os->next)
8561*56bb7041Schristos 	{
8562*56bb7041Schristos 	  lang_output_section_phdr_list *pl;
8563*56bb7041Schristos 
8564*56bb7041Schristos 	  if (os->constraint < 0)
8565*56bb7041Schristos 	    continue;
8566*56bb7041Schristos 
8567*56bb7041Schristos 	  pl = os->phdrs;
8568*56bb7041Schristos 	  if (pl != NULL)
8569*56bb7041Schristos 	    last = pl;
8570*56bb7041Schristos 	  else
8571*56bb7041Schristos 	    {
8572*56bb7041Schristos 	      if (os->sectype == noload_section
8573*56bb7041Schristos 		  || os->bfd_section == NULL
8574*56bb7041Schristos 		  || (os->bfd_section->flags & SEC_ALLOC) == 0)
8575*56bb7041Schristos 		continue;
8576*56bb7041Schristos 
8577*56bb7041Schristos 	      /* Don't add orphans to PT_INTERP header.  */
8578*56bb7041Schristos 	      if (l->type == 3)
8579*56bb7041Schristos 		continue;
8580*56bb7041Schristos 
8581*56bb7041Schristos 	      if (last == NULL)
8582*56bb7041Schristos 		{
8583*56bb7041Schristos 		  lang_output_section_statement_type *tmp_os;
8584*56bb7041Schristos 
8585*56bb7041Schristos 		  /* If we have not run across a section with a program
8586*56bb7041Schristos 		     header assigned to it yet, then scan forwards to find
8587*56bb7041Schristos 		     one.  This prevents inconsistencies in the linker's
8588*56bb7041Schristos 		     behaviour when a script has specified just a single
8589*56bb7041Schristos 		     header and there are sections in that script which are
8590*56bb7041Schristos 		     not assigned to it, and which occur before the first
8591*56bb7041Schristos 		     use of that header. See here for more details:
8592*56bb7041Schristos 		     http://sourceware.org/ml/binutils/2007-02/msg00291.html  */
8593*56bb7041Schristos 		  for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
8594*56bb7041Schristos 		    if (tmp_os->phdrs)
8595*56bb7041Schristos 		      {
8596*56bb7041Schristos 			last = tmp_os->phdrs;
8597*56bb7041Schristos 			break;
8598*56bb7041Schristos 		      }
8599*56bb7041Schristos 		  if (last == NULL)
8600*56bb7041Schristos 		    einfo (_("%F%P: no sections assigned to phdrs\n"));
8601*56bb7041Schristos 		}
8602*56bb7041Schristos 	      pl = last;
8603*56bb7041Schristos 	    }
8604*56bb7041Schristos 
8605*56bb7041Schristos 	  if (os->bfd_section == NULL)
8606*56bb7041Schristos 	    continue;
8607*56bb7041Schristos 
8608*56bb7041Schristos 	  for (; pl != NULL; pl = pl->next)
8609*56bb7041Schristos 	    {
8610*56bb7041Schristos 	      if (strcmp (pl->name, l->name) == 0)
8611*56bb7041Schristos 		{
8612*56bb7041Schristos 		  if (c >= alc)
8613*56bb7041Schristos 		    {
8614*56bb7041Schristos 		      alc *= 2;
8615*56bb7041Schristos 		      secs = (asection **) xrealloc (secs,
8616*56bb7041Schristos 						     alc * sizeof (asection *));
8617*56bb7041Schristos 		    }
8618*56bb7041Schristos 		  secs[c] = os->bfd_section;
8619*56bb7041Schristos 		  ++c;
8620*56bb7041Schristos 		  pl->used = TRUE;
8621*56bb7041Schristos 		}
8622*56bb7041Schristos 	    }
8623*56bb7041Schristos 	}
8624*56bb7041Schristos 
8625*56bb7041Schristos       if (l->flags == NULL)
8626*56bb7041Schristos 	flags = 0;
8627*56bb7041Schristos       else
8628*56bb7041Schristos 	flags = exp_get_vma (l->flags, 0, "phdr flags");
8629*56bb7041Schristos 
8630*56bb7041Schristos       if (l->at == NULL)
8631*56bb7041Schristos 	at = 0;
8632*56bb7041Schristos       else
8633*56bb7041Schristos 	at = exp_get_vma (l->at, 0, "phdr load address");
8634*56bb7041Schristos 
8635*56bb7041Schristos       if (!bfd_record_phdr (link_info.output_bfd, l->type,
8636*56bb7041Schristos 			    l->flags != NULL, flags, l->at != NULL,
8637*56bb7041Schristos 			    at, l->filehdr, l->phdrs, c, secs))
8638*56bb7041Schristos 	einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8639*56bb7041Schristos     }
8640*56bb7041Schristos 
8641*56bb7041Schristos   free (secs);
8642*56bb7041Schristos 
8643*56bb7041Schristos   /* Make sure all the phdr assignments succeeded.  */
8644*56bb7041Schristos   for (os = (void *) lang_os_list.head;
8645*56bb7041Schristos        os != NULL;
8646*56bb7041Schristos        os = os->next)
8647*56bb7041Schristos     {
8648*56bb7041Schristos       lang_output_section_phdr_list *pl;
8649*56bb7041Schristos 
8650*56bb7041Schristos       if (os->constraint < 0
8651*56bb7041Schristos 	  || os->bfd_section == NULL)
8652*56bb7041Schristos 	continue;
8653*56bb7041Schristos 
8654*56bb7041Schristos       for (pl = os->phdrs;
8655*56bb7041Schristos 	   pl != NULL;
8656*56bb7041Schristos 	   pl = pl->next)
8657*56bb7041Schristos 	if (!pl->used && strcmp (pl->name, "NONE") != 0)
8658*56bb7041Schristos 	  einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8659*56bb7041Schristos 		 os->name, pl->name);
8660*56bb7041Schristos     }
8661*56bb7041Schristos }
8662*56bb7041Schristos 
8663*56bb7041Schristos /* Record a list of sections which may not be cross referenced.  */
8664*56bb7041Schristos 
8665*56bb7041Schristos void
lang_add_nocrossref(lang_nocrossref_type * l)8666*56bb7041Schristos lang_add_nocrossref (lang_nocrossref_type *l)
8667*56bb7041Schristos {
8668*56bb7041Schristos   struct lang_nocrossrefs *n;
8669*56bb7041Schristos 
8670*56bb7041Schristos   n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
8671*56bb7041Schristos   n->next = nocrossref_list;
8672*56bb7041Schristos   n->list = l;
8673*56bb7041Schristos   n->onlyfirst = FALSE;
8674*56bb7041Schristos   nocrossref_list = n;
8675*56bb7041Schristos 
8676*56bb7041Schristos   /* Set notice_all so that we get informed about all symbols.  */
8677*56bb7041Schristos   link_info.notice_all = TRUE;
8678*56bb7041Schristos }
8679*56bb7041Schristos 
8680*56bb7041Schristos /* Record a section that cannot be referenced from a list of sections.  */
8681*56bb7041Schristos 
8682*56bb7041Schristos void
lang_add_nocrossref_to(lang_nocrossref_type * l)8683*56bb7041Schristos lang_add_nocrossref_to (lang_nocrossref_type *l)
8684*56bb7041Schristos {
8685*56bb7041Schristos   lang_add_nocrossref (l);
8686*56bb7041Schristos   nocrossref_list->onlyfirst = TRUE;
8687*56bb7041Schristos }
8688*56bb7041Schristos 
8689*56bb7041Schristos /* Overlay handling.  We handle overlays with some static variables.  */
8690*56bb7041Schristos 
8691*56bb7041Schristos /* The overlay virtual address.  */
8692*56bb7041Schristos static etree_type *overlay_vma;
8693*56bb7041Schristos /* And subsection alignment.  */
8694*56bb7041Schristos static etree_type *overlay_subalign;
8695*56bb7041Schristos 
8696*56bb7041Schristos /* An expression for the maximum section size seen so far.  */
8697*56bb7041Schristos static etree_type *overlay_max;
8698*56bb7041Schristos 
8699*56bb7041Schristos /* A list of all the sections in this overlay.  */
8700*56bb7041Schristos 
8701*56bb7041Schristos struct overlay_list {
8702*56bb7041Schristos   struct overlay_list *next;
8703*56bb7041Schristos   lang_output_section_statement_type *os;
8704*56bb7041Schristos };
8705*56bb7041Schristos 
8706*56bb7041Schristos static struct overlay_list *overlay_list;
8707*56bb7041Schristos 
8708*56bb7041Schristos /* Start handling an overlay.  */
8709*56bb7041Schristos 
8710*56bb7041Schristos void
lang_enter_overlay(etree_type * vma_expr,etree_type * subalign)8711*56bb7041Schristos lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
8712*56bb7041Schristos {
8713*56bb7041Schristos   /* The grammar should prevent nested overlays from occurring.  */
8714*56bb7041Schristos   ASSERT (overlay_vma == NULL
8715*56bb7041Schristos 	  && overlay_subalign == NULL
8716*56bb7041Schristos 	  && overlay_max == NULL);
8717*56bb7041Schristos 
8718*56bb7041Schristos   overlay_vma = vma_expr;
8719*56bb7041Schristos   overlay_subalign = subalign;
8720*56bb7041Schristos }
8721*56bb7041Schristos 
8722*56bb7041Schristos /* Start a section in an overlay.  We handle this by calling
8723*56bb7041Schristos    lang_enter_output_section_statement with the correct VMA.
8724*56bb7041Schristos    lang_leave_overlay sets up the LMA and memory regions.  */
8725*56bb7041Schristos 
8726*56bb7041Schristos void
lang_enter_overlay_section(const char * name)8727*56bb7041Schristos lang_enter_overlay_section (const char *name)
8728*56bb7041Schristos {
8729*56bb7041Schristos   struct overlay_list *n;
8730*56bb7041Schristos   etree_type *size;
8731*56bb7041Schristos 
8732*56bb7041Schristos   lang_enter_output_section_statement (name, overlay_vma, overlay_section,
8733*56bb7041Schristos 				       0, overlay_subalign, 0, 0, 0);
8734*56bb7041Schristos 
8735*56bb7041Schristos   /* If this is the first section, then base the VMA of future
8736*56bb7041Schristos      sections on this one.  This will work correctly even if `.' is
8737*56bb7041Schristos      used in the addresses.  */
8738*56bb7041Schristos   if (overlay_list == NULL)
8739*56bb7041Schristos     overlay_vma = exp_nameop (ADDR, name);
8740*56bb7041Schristos 
8741*56bb7041Schristos   /* Remember the section.  */
8742*56bb7041Schristos   n = (struct overlay_list *) xmalloc (sizeof *n);
8743*56bb7041Schristos   n->os = current_section;
8744*56bb7041Schristos   n->next = overlay_list;
8745*56bb7041Schristos   overlay_list = n;
8746*56bb7041Schristos 
8747*56bb7041Schristos   size = exp_nameop (SIZEOF, name);
8748*56bb7041Schristos 
8749*56bb7041Schristos   /* Arrange to work out the maximum section end address.  */
8750*56bb7041Schristos   if (overlay_max == NULL)
8751*56bb7041Schristos     overlay_max = size;
8752*56bb7041Schristos   else
8753*56bb7041Schristos     overlay_max = exp_binop (MAX_K, overlay_max, size);
8754*56bb7041Schristos }
8755*56bb7041Schristos 
8756*56bb7041Schristos /* Finish a section in an overlay.  There isn't any special to do
8757*56bb7041Schristos    here.  */
8758*56bb7041Schristos 
8759*56bb7041Schristos void
lang_leave_overlay_section(fill_type * fill,lang_output_section_phdr_list * phdrs)8760*56bb7041Schristos lang_leave_overlay_section (fill_type *fill,
8761*56bb7041Schristos 			    lang_output_section_phdr_list *phdrs)
8762*56bb7041Schristos {
8763*56bb7041Schristos   const char *name;
8764*56bb7041Schristos   char *clean, *s2;
8765*56bb7041Schristos   const char *s1;
8766*56bb7041Schristos   char *buf;
8767*56bb7041Schristos 
8768*56bb7041Schristos   name = current_section->name;
8769*56bb7041Schristos 
8770*56bb7041Schristos   /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8771*56bb7041Schristos      region and that no load-time region has been specified.  It doesn't
8772*56bb7041Schristos      really matter what we say here, since lang_leave_overlay will
8773*56bb7041Schristos      override it.  */
8774*56bb7041Schristos   lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
8775*56bb7041Schristos 
8776*56bb7041Schristos   /* Define the magic symbols.  */
8777*56bb7041Schristos 
8778*56bb7041Schristos   clean = (char *) xmalloc (strlen (name) + 1);
8779*56bb7041Schristos   s2 = clean;
8780*56bb7041Schristos   for (s1 = name; *s1 != '\0'; s1++)
8781*56bb7041Schristos     if (ISALNUM (*s1) || *s1 == '_')
8782*56bb7041Schristos       *s2++ = *s1;
8783*56bb7041Schristos   *s2 = '\0';
8784*56bb7041Schristos 
8785*56bb7041Schristos   buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
8786*56bb7041Schristos   sprintf (buf, "__load_start_%s", clean);
8787*56bb7041Schristos   lang_add_assignment (exp_provide (buf,
8788*56bb7041Schristos 				    exp_nameop (LOADADDR, name),
8789*56bb7041Schristos 				    FALSE));
8790*56bb7041Schristos 
8791*56bb7041Schristos   buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
8792*56bb7041Schristos   sprintf (buf, "__load_stop_%s", clean);
8793*56bb7041Schristos   lang_add_assignment (exp_provide (buf,
8794*56bb7041Schristos 				    exp_binop ('+',
8795*56bb7041Schristos 					       exp_nameop (LOADADDR, name),
8796*56bb7041Schristos 					       exp_nameop (SIZEOF, name)),
8797*56bb7041Schristos 				    FALSE));
8798*56bb7041Schristos 
8799*56bb7041Schristos   free (clean);
8800*56bb7041Schristos }
8801*56bb7041Schristos 
8802*56bb7041Schristos /* Finish an overlay.  If there are any overlay wide settings, this
8803*56bb7041Schristos    looks through all the sections in the overlay and sets them.  */
8804*56bb7041Schristos 
8805*56bb7041Schristos 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)8806*56bb7041Schristos lang_leave_overlay (etree_type *lma_expr,
8807*56bb7041Schristos 		    int nocrossrefs,
8808*56bb7041Schristos 		    fill_type *fill,
8809*56bb7041Schristos 		    const char *memspec,
8810*56bb7041Schristos 		    lang_output_section_phdr_list *phdrs,
8811*56bb7041Schristos 		    const char *lma_memspec)
8812*56bb7041Schristos {
8813*56bb7041Schristos   lang_memory_region_type *region;
8814*56bb7041Schristos   lang_memory_region_type *lma_region;
8815*56bb7041Schristos   struct overlay_list *l;
8816*56bb7041Schristos   lang_nocrossref_type *nocrossref;
8817*56bb7041Schristos 
8818*56bb7041Schristos   lang_get_regions (&region, &lma_region,
8819*56bb7041Schristos 		    memspec, lma_memspec,
8820*56bb7041Schristos 		    lma_expr != NULL, FALSE);
8821*56bb7041Schristos 
8822*56bb7041Schristos   nocrossref = NULL;
8823*56bb7041Schristos 
8824*56bb7041Schristos   /* After setting the size of the last section, set '.' to end of the
8825*56bb7041Schristos      overlay region.  */
8826*56bb7041Schristos   if (overlay_list != NULL)
8827*56bb7041Schristos     {
8828*56bb7041Schristos       overlay_list->os->update_dot = 1;
8829*56bb7041Schristos       overlay_list->os->update_dot_tree
8830*56bb7041Schristos 	= exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), FALSE);
8831*56bb7041Schristos     }
8832*56bb7041Schristos 
8833*56bb7041Schristos   l = overlay_list;
8834*56bb7041Schristos   while (l != NULL)
8835*56bb7041Schristos     {
8836*56bb7041Schristos       struct overlay_list *next;
8837*56bb7041Schristos 
8838*56bb7041Schristos       if (fill != NULL && l->os->fill == NULL)
8839*56bb7041Schristos 	l->os->fill = fill;
8840*56bb7041Schristos 
8841*56bb7041Schristos       l->os->region = region;
8842*56bb7041Schristos       l->os->lma_region = lma_region;
8843*56bb7041Schristos 
8844*56bb7041Schristos       /* The first section has the load address specified in the
8845*56bb7041Schristos 	 OVERLAY statement.  The rest are worked out from that.
8846*56bb7041Schristos 	 The base address is not needed (and should be null) if
8847*56bb7041Schristos 	 an LMA region was specified.  */
8848*56bb7041Schristos       if (l->next == 0)
8849*56bb7041Schristos 	{
8850*56bb7041Schristos 	  l->os->load_base = lma_expr;
8851*56bb7041Schristos 	  l->os->sectype = first_overlay_section;
8852*56bb7041Schristos 	}
8853*56bb7041Schristos       if (phdrs != NULL && l->os->phdrs == NULL)
8854*56bb7041Schristos 	l->os->phdrs = phdrs;
8855*56bb7041Schristos 
8856*56bb7041Schristos       if (nocrossrefs)
8857*56bb7041Schristos 	{
8858*56bb7041Schristos 	  lang_nocrossref_type *nc;
8859*56bb7041Schristos 
8860*56bb7041Schristos 	  nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
8861*56bb7041Schristos 	  nc->name = l->os->name;
8862*56bb7041Schristos 	  nc->next = nocrossref;
8863*56bb7041Schristos 	  nocrossref = nc;
8864*56bb7041Schristos 	}
8865*56bb7041Schristos 
8866*56bb7041Schristos       next = l->next;
8867*56bb7041Schristos       free (l);
8868*56bb7041Schristos       l = next;
8869*56bb7041Schristos     }
8870*56bb7041Schristos 
8871*56bb7041Schristos   if (nocrossref != NULL)
8872*56bb7041Schristos     lang_add_nocrossref (nocrossref);
8873*56bb7041Schristos 
8874*56bb7041Schristos   overlay_vma = NULL;
8875*56bb7041Schristos   overlay_list = NULL;
8876*56bb7041Schristos   overlay_max = NULL;
8877*56bb7041Schristos   overlay_subalign = NULL;
8878*56bb7041Schristos }
8879*56bb7041Schristos 
8880*56bb7041Schristos /* Version handling.  This is only useful for ELF.  */
8881*56bb7041Schristos 
8882*56bb7041Schristos /* If PREV is NULL, return first version pattern matching particular symbol.
8883*56bb7041Schristos    If PREV is non-NULL, return first version pattern matching particular
8884*56bb7041Schristos    symbol after PREV (previously returned by lang_vers_match).  */
8885*56bb7041Schristos 
8886*56bb7041Schristos 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)8887*56bb7041Schristos lang_vers_match (struct bfd_elf_version_expr_head *head,
8888*56bb7041Schristos 		 struct bfd_elf_version_expr *prev,
8889*56bb7041Schristos 		 const char *sym)
8890*56bb7041Schristos {
8891*56bb7041Schristos   const char *c_sym;
8892*56bb7041Schristos   const char *cxx_sym = sym;
8893*56bb7041Schristos   const char *java_sym = sym;
8894*56bb7041Schristos   struct bfd_elf_version_expr *expr = NULL;
8895*56bb7041Schristos   enum demangling_styles curr_style;
8896*56bb7041Schristos 
8897*56bb7041Schristos   curr_style = CURRENT_DEMANGLING_STYLE;
8898*56bb7041Schristos   cplus_demangle_set_style (no_demangling);
8899*56bb7041Schristos   c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS);
8900*56bb7041Schristos   if (!c_sym)
8901*56bb7041Schristos     c_sym = sym;
8902*56bb7041Schristos   cplus_demangle_set_style (curr_style);
8903*56bb7041Schristos 
8904*56bb7041Schristos   if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
8905*56bb7041Schristos     {
8906*56bb7041Schristos       cxx_sym = bfd_demangle (link_info.output_bfd, sym,
8907*56bb7041Schristos 			      DMGL_PARAMS | DMGL_ANSI);
8908*56bb7041Schristos       if (!cxx_sym)
8909*56bb7041Schristos 	cxx_sym = sym;
8910*56bb7041Schristos     }
8911*56bb7041Schristos   if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
8912*56bb7041Schristos     {
8913*56bb7041Schristos       java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA);
8914*56bb7041Schristos       if (!java_sym)
8915*56bb7041Schristos 	java_sym = sym;
8916*56bb7041Schristos     }
8917*56bb7041Schristos 
8918*56bb7041Schristos   if (head->htab && (prev == NULL || prev->literal))
8919*56bb7041Schristos     {
8920*56bb7041Schristos       struct bfd_elf_version_expr e;
8921*56bb7041Schristos 
8922*56bb7041Schristos       switch (prev ? prev->mask : 0)
8923*56bb7041Schristos 	{
8924*56bb7041Schristos 	case 0:
8925*56bb7041Schristos 	  if (head->mask & BFD_ELF_VERSION_C_TYPE)
8926*56bb7041Schristos 	    {
8927*56bb7041Schristos 	      e.pattern = c_sym;
8928*56bb7041Schristos 	      expr = (struct bfd_elf_version_expr *)
8929*56bb7041Schristos 		  htab_find ((htab_t) head->htab, &e);
8930*56bb7041Schristos 	      while (expr && strcmp (expr->pattern, c_sym) == 0)
8931*56bb7041Schristos 		if (expr->mask == BFD_ELF_VERSION_C_TYPE)
8932*56bb7041Schristos 		  goto out_ret;
8933*56bb7041Schristos 		else
8934*56bb7041Schristos 		  expr = expr->next;
8935*56bb7041Schristos 	    }
8936*56bb7041Schristos 	  /* Fallthrough */
8937*56bb7041Schristos 	case BFD_ELF_VERSION_C_TYPE:
8938*56bb7041Schristos 	  if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
8939*56bb7041Schristos 	    {
8940*56bb7041Schristos 	      e.pattern = cxx_sym;
8941*56bb7041Schristos 	      expr = (struct bfd_elf_version_expr *)
8942*56bb7041Schristos 		  htab_find ((htab_t) head->htab, &e);
8943*56bb7041Schristos 	      while (expr && strcmp (expr->pattern, cxx_sym) == 0)
8944*56bb7041Schristos 		if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
8945*56bb7041Schristos 		  goto out_ret;
8946*56bb7041Schristos 		else
8947*56bb7041Schristos 		  expr = expr->next;
8948*56bb7041Schristos 	    }
8949*56bb7041Schristos 	  /* Fallthrough */
8950*56bb7041Schristos 	case BFD_ELF_VERSION_CXX_TYPE:
8951*56bb7041Schristos 	  if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
8952*56bb7041Schristos 	    {
8953*56bb7041Schristos 	      e.pattern = java_sym;
8954*56bb7041Schristos 	      expr = (struct bfd_elf_version_expr *)
8955*56bb7041Schristos 		  htab_find ((htab_t) head->htab, &e);
8956*56bb7041Schristos 	      while (expr && strcmp (expr->pattern, java_sym) == 0)
8957*56bb7041Schristos 		if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
8958*56bb7041Schristos 		  goto out_ret;
8959*56bb7041Schristos 		else
8960*56bb7041Schristos 		  expr = expr->next;
8961*56bb7041Schristos 	    }
8962*56bb7041Schristos 	  /* Fallthrough */
8963*56bb7041Schristos 	default:
8964*56bb7041Schristos 	  break;
8965*56bb7041Schristos 	}
8966*56bb7041Schristos     }
8967*56bb7041Schristos 
8968*56bb7041Schristos   /* Finally, try the wildcards.  */
8969*56bb7041Schristos   if (prev == NULL || prev->literal)
8970*56bb7041Schristos     expr = head->remaining;
8971*56bb7041Schristos   else
8972*56bb7041Schristos     expr = prev->next;
8973*56bb7041Schristos   for (; expr; expr = expr->next)
8974*56bb7041Schristos     {
8975*56bb7041Schristos       const char *s;
8976*56bb7041Schristos 
8977*56bb7041Schristos       if (!expr->pattern)
8978*56bb7041Schristos 	continue;
8979*56bb7041Schristos 
8980*56bb7041Schristos       if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
8981*56bb7041Schristos 	break;
8982*56bb7041Schristos 
8983*56bb7041Schristos       if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
8984*56bb7041Schristos 	s = java_sym;
8985*56bb7041Schristos       else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
8986*56bb7041Schristos 	s = cxx_sym;
8987*56bb7041Schristos       else
8988*56bb7041Schristos 	s = c_sym;
8989*56bb7041Schristos       if (fnmatch (expr->pattern, s, 0) == 0)
8990*56bb7041Schristos 	break;
8991*56bb7041Schristos     }
8992*56bb7041Schristos 
8993*56bb7041Schristos  out_ret:
8994*56bb7041Schristos   if (c_sym != sym)
8995*56bb7041Schristos     free ((char *) c_sym);
8996*56bb7041Schristos   if (cxx_sym != sym)
8997*56bb7041Schristos     free ((char *) cxx_sym);
8998*56bb7041Schristos   if (java_sym != sym)
8999*56bb7041Schristos     free ((char *) java_sym);
9000*56bb7041Schristos   return expr;
9001*56bb7041Schristos }
9002*56bb7041Schristos 
9003*56bb7041Schristos /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9004*56bb7041Schristos    return a pointer to the symbol name with any backslash quotes removed.  */
9005*56bb7041Schristos 
9006*56bb7041Schristos static const char *
realsymbol(const char * pattern)9007*56bb7041Schristos realsymbol (const char *pattern)
9008*56bb7041Schristos {
9009*56bb7041Schristos   const char *p;
9010*56bb7041Schristos   bfd_boolean changed = FALSE, backslash = FALSE;
9011*56bb7041Schristos   char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
9012*56bb7041Schristos 
9013*56bb7041Schristos   for (p = pattern, s = symbol; *p != '\0'; ++p)
9014*56bb7041Schristos     {
9015*56bb7041Schristos       /* It is a glob pattern only if there is no preceding
9016*56bb7041Schristos 	 backslash.  */
9017*56bb7041Schristos       if (backslash)
9018*56bb7041Schristos 	{
9019*56bb7041Schristos 	  /* Remove the preceding backslash.  */
9020*56bb7041Schristos 	  *(s - 1) = *p;
9021*56bb7041Schristos 	  backslash = FALSE;
9022*56bb7041Schristos 	  changed = TRUE;
9023*56bb7041Schristos 	}
9024*56bb7041Schristos       else
9025*56bb7041Schristos 	{
9026*56bb7041Schristos 	  if (*p == '?' || *p == '*' || *p == '[')
9027*56bb7041Schristos 	    {
9028*56bb7041Schristos 	      free (symbol);
9029*56bb7041Schristos 	      return NULL;
9030*56bb7041Schristos 	    }
9031*56bb7041Schristos 
9032*56bb7041Schristos 	  *s++ = *p;
9033*56bb7041Schristos 	  backslash = *p == '\\';
9034*56bb7041Schristos 	}
9035*56bb7041Schristos     }
9036*56bb7041Schristos 
9037*56bb7041Schristos   if (changed)
9038*56bb7041Schristos     {
9039*56bb7041Schristos       *s = '\0';
9040*56bb7041Schristos       return symbol;
9041*56bb7041Schristos     }
9042*56bb7041Schristos   else
9043*56bb7041Schristos     {
9044*56bb7041Schristos       free (symbol);
9045*56bb7041Schristos       return pattern;
9046*56bb7041Schristos     }
9047*56bb7041Schristos }
9048*56bb7041Schristos 
9049*56bb7041Schristos /* This is called for each variable name or match expression.  NEW_NAME is
9050*56bb7041Schristos    the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9051*56bb7041Schristos    pattern to be matched against symbol names.  */
9052*56bb7041Schristos 
9053*56bb7041Schristos struct bfd_elf_version_expr *
lang_new_vers_pattern(struct bfd_elf_version_expr * orig,const char * new_name,const char * lang,bfd_boolean literal_p)9054*56bb7041Schristos lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
9055*56bb7041Schristos 		       const char *new_name,
9056*56bb7041Schristos 		       const char *lang,
9057*56bb7041Schristos 		       bfd_boolean literal_p)
9058*56bb7041Schristos {
9059*56bb7041Schristos   struct bfd_elf_version_expr *ret;
9060*56bb7041Schristos 
9061*56bb7041Schristos   ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
9062*56bb7041Schristos   ret->next = orig;
9063*56bb7041Schristos   ret->symver = 0;
9064*56bb7041Schristos   ret->script = 0;
9065*56bb7041Schristos   ret->literal = TRUE;
9066*56bb7041Schristos   ret->pattern = literal_p ? new_name : realsymbol (new_name);
9067*56bb7041Schristos   if (ret->pattern == NULL)
9068*56bb7041Schristos     {
9069*56bb7041Schristos       ret->pattern = new_name;
9070*56bb7041Schristos       ret->literal = FALSE;
9071*56bb7041Schristos     }
9072*56bb7041Schristos 
9073*56bb7041Schristos   if (lang == NULL || strcasecmp (lang, "C") == 0)
9074*56bb7041Schristos     ret->mask = BFD_ELF_VERSION_C_TYPE;
9075*56bb7041Schristos   else if (strcasecmp (lang, "C++") == 0)
9076*56bb7041Schristos     ret->mask = BFD_ELF_VERSION_CXX_TYPE;
9077*56bb7041Schristos   else if (strcasecmp (lang, "Java") == 0)
9078*56bb7041Schristos     ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
9079*56bb7041Schristos   else
9080*56bb7041Schristos     {
9081*56bb7041Schristos       einfo (_("%X%P: unknown language `%s' in version information\n"),
9082*56bb7041Schristos 	     lang);
9083*56bb7041Schristos       ret->mask = BFD_ELF_VERSION_C_TYPE;
9084*56bb7041Schristos     }
9085*56bb7041Schristos 
9086*56bb7041Schristos   return ldemul_new_vers_pattern (ret);
9087*56bb7041Schristos }
9088*56bb7041Schristos 
9089*56bb7041Schristos /* This is called for each set of variable names and match
9090*56bb7041Schristos    expressions.  */
9091*56bb7041Schristos 
9092*56bb7041Schristos struct bfd_elf_version_tree *
lang_new_vers_node(struct bfd_elf_version_expr * globals,struct bfd_elf_version_expr * locals)9093*56bb7041Schristos lang_new_vers_node (struct bfd_elf_version_expr *globals,
9094*56bb7041Schristos 		    struct bfd_elf_version_expr *locals)
9095*56bb7041Schristos {
9096*56bb7041Schristos   struct bfd_elf_version_tree *ret;
9097*56bb7041Schristos 
9098*56bb7041Schristos   ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
9099*56bb7041Schristos   ret->globals.list = globals;
9100*56bb7041Schristos   ret->locals.list = locals;
9101*56bb7041Schristos   ret->match = lang_vers_match;
9102*56bb7041Schristos   ret->name_indx = (unsigned int) -1;
9103*56bb7041Schristos   return ret;
9104*56bb7041Schristos }
9105*56bb7041Schristos 
9106*56bb7041Schristos /* This static variable keeps track of version indices.  */
9107*56bb7041Schristos 
9108*56bb7041Schristos static int version_index;
9109*56bb7041Schristos 
9110*56bb7041Schristos static hashval_t
version_expr_head_hash(const void * p)9111*56bb7041Schristos version_expr_head_hash (const void *p)
9112*56bb7041Schristos {
9113*56bb7041Schristos   const struct bfd_elf_version_expr *e =
9114*56bb7041Schristos       (const struct bfd_elf_version_expr *) p;
9115*56bb7041Schristos 
9116*56bb7041Schristos   return htab_hash_string (e->pattern);
9117*56bb7041Schristos }
9118*56bb7041Schristos 
9119*56bb7041Schristos static int
version_expr_head_eq(const void * p1,const void * p2)9120*56bb7041Schristos version_expr_head_eq (const void *p1, const void *p2)
9121*56bb7041Schristos {
9122*56bb7041Schristos   const struct bfd_elf_version_expr *e1 =
9123*56bb7041Schristos       (const struct bfd_elf_version_expr *) p1;
9124*56bb7041Schristos   const struct bfd_elf_version_expr *e2 =
9125*56bb7041Schristos       (const struct bfd_elf_version_expr *) p2;
9126*56bb7041Schristos 
9127*56bb7041Schristos   return strcmp (e1->pattern, e2->pattern) == 0;
9128*56bb7041Schristos }
9129*56bb7041Schristos 
9130*56bb7041Schristos static void
lang_finalize_version_expr_head(struct bfd_elf_version_expr_head * head)9131*56bb7041Schristos lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
9132*56bb7041Schristos {
9133*56bb7041Schristos   size_t count = 0;
9134*56bb7041Schristos   struct bfd_elf_version_expr *e, *next;
9135*56bb7041Schristos   struct bfd_elf_version_expr **list_loc, **remaining_loc;
9136*56bb7041Schristos 
9137*56bb7041Schristos   for (e = head->list; e; e = e->next)
9138*56bb7041Schristos     {
9139*56bb7041Schristos       if (e->literal)
9140*56bb7041Schristos 	count++;
9141*56bb7041Schristos       head->mask |= e->mask;
9142*56bb7041Schristos     }
9143*56bb7041Schristos 
9144*56bb7041Schristos   if (count)
9145*56bb7041Schristos     {
9146*56bb7041Schristos       head->htab = htab_create (count * 2, version_expr_head_hash,
9147*56bb7041Schristos 				version_expr_head_eq, NULL);
9148*56bb7041Schristos       list_loc = &head->list;
9149*56bb7041Schristos       remaining_loc = &head->remaining;
9150*56bb7041Schristos       for (e = head->list; e; e = next)
9151*56bb7041Schristos 	{
9152*56bb7041Schristos 	  next = e->next;
9153*56bb7041Schristos 	  if (!e->literal)
9154*56bb7041Schristos 	    {
9155*56bb7041Schristos 	      *remaining_loc = e;
9156*56bb7041Schristos 	      remaining_loc = &e->next;
9157*56bb7041Schristos 	    }
9158*56bb7041Schristos 	  else
9159*56bb7041Schristos 	    {
9160*56bb7041Schristos 	      void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
9161*56bb7041Schristos 
9162*56bb7041Schristos 	      if (*loc)
9163*56bb7041Schristos 		{
9164*56bb7041Schristos 		  struct bfd_elf_version_expr *e1, *last;
9165*56bb7041Schristos 
9166*56bb7041Schristos 		  e1 = (struct bfd_elf_version_expr *) *loc;
9167*56bb7041Schristos 		  last = NULL;
9168*56bb7041Schristos 		  do
9169*56bb7041Schristos 		    {
9170*56bb7041Schristos 		      if (e1->mask == e->mask)
9171*56bb7041Schristos 			{
9172*56bb7041Schristos 			  last = NULL;
9173*56bb7041Schristos 			  break;
9174*56bb7041Schristos 			}
9175*56bb7041Schristos 		      last = e1;
9176*56bb7041Schristos 		      e1 = e1->next;
9177*56bb7041Schristos 		    }
9178*56bb7041Schristos 		  while (e1 && strcmp (e1->pattern, e->pattern) == 0);
9179*56bb7041Schristos 
9180*56bb7041Schristos 		  if (last == NULL)
9181*56bb7041Schristos 		    {
9182*56bb7041Schristos 		      /* This is a duplicate.  */
9183*56bb7041Schristos 		      /* FIXME: Memory leak.  Sometimes pattern is not
9184*56bb7041Schristos 			 xmalloced alone, but in larger chunk of memory.  */
9185*56bb7041Schristos 		      /* free (e->pattern); */
9186*56bb7041Schristos 		      free (e);
9187*56bb7041Schristos 		    }
9188*56bb7041Schristos 		  else
9189*56bb7041Schristos 		    {
9190*56bb7041Schristos 		      e->next = last->next;
9191*56bb7041Schristos 		      last->next = e;
9192*56bb7041Schristos 		    }
9193*56bb7041Schristos 		}
9194*56bb7041Schristos 	      else
9195*56bb7041Schristos 		{
9196*56bb7041Schristos 		  *loc = e;
9197*56bb7041Schristos 		  *list_loc = e;
9198*56bb7041Schristos 		  list_loc = &e->next;
9199*56bb7041Schristos 		}
9200*56bb7041Schristos 	    }
9201*56bb7041Schristos 	}
9202*56bb7041Schristos       *remaining_loc = NULL;
9203*56bb7041Schristos       *list_loc = head->remaining;
9204*56bb7041Schristos     }
9205*56bb7041Schristos   else
9206*56bb7041Schristos     head->remaining = head->list;
9207*56bb7041Schristos }
9208*56bb7041Schristos 
9209*56bb7041Schristos /* This is called when we know the name and dependencies of the
9210*56bb7041Schristos    version.  */
9211*56bb7041Schristos 
9212*56bb7041Schristos void
lang_register_vers_node(const char * name,struct bfd_elf_version_tree * version,struct bfd_elf_version_deps * deps)9213*56bb7041Schristos lang_register_vers_node (const char *name,
9214*56bb7041Schristos 			 struct bfd_elf_version_tree *version,
9215*56bb7041Schristos 			 struct bfd_elf_version_deps *deps)
9216*56bb7041Schristos {
9217*56bb7041Schristos   struct bfd_elf_version_tree *t, **pp;
9218*56bb7041Schristos   struct bfd_elf_version_expr *e1;
9219*56bb7041Schristos 
9220*56bb7041Schristos   if (name == NULL)
9221*56bb7041Schristos     name = "";
9222*56bb7041Schristos 
9223*56bb7041Schristos   if (link_info.version_info != NULL
9224*56bb7041Schristos       && (name[0] == '\0' || link_info.version_info->name[0] == '\0'))
9225*56bb7041Schristos     {
9226*56bb7041Schristos       einfo (_("%X%P: anonymous version tag cannot be combined"
9227*56bb7041Schristos 	       " with other version tags\n"));
9228*56bb7041Schristos       free (version);
9229*56bb7041Schristos       return;
9230*56bb7041Schristos     }
9231*56bb7041Schristos 
9232*56bb7041Schristos   /* Make sure this node has a unique name.  */
9233*56bb7041Schristos   for (t = link_info.version_info; t != NULL; t = t->next)
9234*56bb7041Schristos     if (strcmp (t->name, name) == 0)
9235*56bb7041Schristos       einfo (_("%X%P: duplicate version tag `%s'\n"), name);
9236*56bb7041Schristos 
9237*56bb7041Schristos   lang_finalize_version_expr_head (&version->globals);
9238*56bb7041Schristos   lang_finalize_version_expr_head (&version->locals);
9239*56bb7041Schristos 
9240*56bb7041Schristos   /* Check the global and local match names, and make sure there
9241*56bb7041Schristos      aren't any duplicates.  */
9242*56bb7041Schristos 
9243*56bb7041Schristos   for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
9244*56bb7041Schristos     {
9245*56bb7041Schristos       for (t = link_info.version_info; t != NULL; t = t->next)
9246*56bb7041Schristos 	{
9247*56bb7041Schristos 	  struct bfd_elf_version_expr *e2;
9248*56bb7041Schristos 
9249*56bb7041Schristos 	  if (t->locals.htab && e1->literal)
9250*56bb7041Schristos 	    {
9251*56bb7041Schristos 	      e2 = (struct bfd_elf_version_expr *)
9252*56bb7041Schristos 		  htab_find ((htab_t) t->locals.htab, e1);
9253*56bb7041Schristos 	      while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
9254*56bb7041Schristos 		{
9255*56bb7041Schristos 		  if (e1->mask == e2->mask)
9256*56bb7041Schristos 		    einfo (_("%X%P: duplicate expression `%s'"
9257*56bb7041Schristos 			     " in version information\n"), e1->pattern);
9258*56bb7041Schristos 		  e2 = e2->next;
9259*56bb7041Schristos 		}
9260*56bb7041Schristos 	    }
9261*56bb7041Schristos 	  else if (!e1->literal)
9262*56bb7041Schristos 	    for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
9263*56bb7041Schristos 	      if (strcmp (e1->pattern, e2->pattern) == 0
9264*56bb7041Schristos 		  && e1->mask == e2->mask)
9265*56bb7041Schristos 		einfo (_("%X%P: duplicate expression `%s'"
9266*56bb7041Schristos 			 " in version information\n"), e1->pattern);
9267*56bb7041Schristos 	}
9268*56bb7041Schristos     }
9269*56bb7041Schristos 
9270*56bb7041Schristos   for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
9271*56bb7041Schristos     {
9272*56bb7041Schristos       for (t = link_info.version_info; t != NULL; t = t->next)
9273*56bb7041Schristos 	{
9274*56bb7041Schristos 	  struct bfd_elf_version_expr *e2;
9275*56bb7041Schristos 
9276*56bb7041Schristos 	  if (t->globals.htab && e1->literal)
9277*56bb7041Schristos 	    {
9278*56bb7041Schristos 	      e2 = (struct bfd_elf_version_expr *)
9279*56bb7041Schristos 		  htab_find ((htab_t) t->globals.htab, e1);
9280*56bb7041Schristos 	      while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
9281*56bb7041Schristos 		{
9282*56bb7041Schristos 		  if (e1->mask == e2->mask)
9283*56bb7041Schristos 		    einfo (_("%X%P: duplicate expression `%s'"
9284*56bb7041Schristos 			     " in version information\n"),
9285*56bb7041Schristos 			   e1->pattern);
9286*56bb7041Schristos 		  e2 = e2->next;
9287*56bb7041Schristos 		}
9288*56bb7041Schristos 	    }
9289*56bb7041Schristos 	  else if (!e1->literal)
9290*56bb7041Schristos 	    for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
9291*56bb7041Schristos 	      if (strcmp (e1->pattern, e2->pattern) == 0
9292*56bb7041Schristos 		  && e1->mask == e2->mask)
9293*56bb7041Schristos 		einfo (_("%X%P: duplicate expression `%s'"
9294*56bb7041Schristos 			 " in version information\n"), e1->pattern);
9295*56bb7041Schristos 	}
9296*56bb7041Schristos     }
9297*56bb7041Schristos 
9298*56bb7041Schristos   version->deps = deps;
9299*56bb7041Schristos   version->name = name;
9300*56bb7041Schristos   if (name[0] != '\0')
9301*56bb7041Schristos     {
9302*56bb7041Schristos       ++version_index;
9303*56bb7041Schristos       version->vernum = version_index;
9304*56bb7041Schristos     }
9305*56bb7041Schristos   else
9306*56bb7041Schristos     version->vernum = 0;
9307*56bb7041Schristos 
9308*56bb7041Schristos   for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next)
9309*56bb7041Schristos     ;
9310*56bb7041Schristos   *pp = version;
9311*56bb7041Schristos }
9312*56bb7041Schristos 
9313*56bb7041Schristos /* This is called when we see a version dependency.  */
9314*56bb7041Schristos 
9315*56bb7041Schristos struct bfd_elf_version_deps *
lang_add_vers_depend(struct bfd_elf_version_deps * list,const char * name)9316*56bb7041Schristos lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
9317*56bb7041Schristos {
9318*56bb7041Schristos   struct bfd_elf_version_deps *ret;
9319*56bb7041Schristos   struct bfd_elf_version_tree *t;
9320*56bb7041Schristos 
9321*56bb7041Schristos   ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
9322*56bb7041Schristos   ret->next = list;
9323*56bb7041Schristos 
9324*56bb7041Schristos   for (t = link_info.version_info; t != NULL; t = t->next)
9325*56bb7041Schristos     {
9326*56bb7041Schristos       if (strcmp (t->name, name) == 0)
9327*56bb7041Schristos 	{
9328*56bb7041Schristos 	  ret->version_needed = t;
9329*56bb7041Schristos 	  return ret;
9330*56bb7041Schristos 	}
9331*56bb7041Schristos     }
9332*56bb7041Schristos 
9333*56bb7041Schristos   einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
9334*56bb7041Schristos 
9335*56bb7041Schristos   ret->version_needed = NULL;
9336*56bb7041Schristos   return ret;
9337*56bb7041Schristos }
9338*56bb7041Schristos 
9339*56bb7041Schristos static void
lang_do_version_exports_section(void)9340*56bb7041Schristos lang_do_version_exports_section (void)
9341*56bb7041Schristos {
9342*56bb7041Schristos   struct bfd_elf_version_expr *greg = NULL, *lreg;
9343*56bb7041Schristos 
9344*56bb7041Schristos   LANG_FOR_EACH_INPUT_STATEMENT (is)
9345*56bb7041Schristos     {
9346*56bb7041Schristos       asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
9347*56bb7041Schristos       char *contents, *p;
9348*56bb7041Schristos       bfd_size_type len;
9349*56bb7041Schristos 
9350*56bb7041Schristos       if (sec == NULL)
9351*56bb7041Schristos 	continue;
9352*56bb7041Schristos 
9353*56bb7041Schristos       len = sec->size;
9354*56bb7041Schristos       contents = (char *) xmalloc (len);
9355*56bb7041Schristos       if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
9356*56bb7041Schristos 	einfo (_("%X%P: unable to read .exports section contents\n"), sec);
9357*56bb7041Schristos 
9358*56bb7041Schristos       p = contents;
9359*56bb7041Schristos       while (p < contents + len)
9360*56bb7041Schristos 	{
9361*56bb7041Schristos 	  greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
9362*56bb7041Schristos 	  p = strchr (p, '\0') + 1;
9363*56bb7041Schristos 	}
9364*56bb7041Schristos 
9365*56bb7041Schristos       /* Do not free the contents, as we used them creating the regex.  */
9366*56bb7041Schristos 
9367*56bb7041Schristos       /* Do not include this section in the link.  */
9368*56bb7041Schristos       sec->flags |= SEC_EXCLUDE | SEC_KEEP;
9369*56bb7041Schristos     }
9370*56bb7041Schristos 
9371*56bb7041Schristos   lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
9372*56bb7041Schristos   lang_register_vers_node (command_line.version_exports_section,
9373*56bb7041Schristos 			   lang_new_vers_node (greg, lreg), NULL);
9374*56bb7041Schristos }
9375*56bb7041Schristos 
9376*56bb7041Schristos /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */
9377*56bb7041Schristos 
9378*56bb7041Schristos static void
lang_do_memory_regions(void)9379*56bb7041Schristos lang_do_memory_regions (void)
9380*56bb7041Schristos {
9381*56bb7041Schristos   lang_memory_region_type *r = lang_memory_region_list;
9382*56bb7041Schristos 
9383*56bb7041Schristos   for (; r != NULL; r = r->next)
9384*56bb7041Schristos     {
9385*56bb7041Schristos       if (r->origin_exp)
9386*56bb7041Schristos 	{
9387*56bb7041Schristos 	  exp_fold_tree_no_dot (r->origin_exp);
9388*56bb7041Schristos 	  if (expld.result.valid_p)
9389*56bb7041Schristos 	    {
9390*56bb7041Schristos 	      r->origin = expld.result.value;
9391*56bb7041Schristos 	      r->current = r->origin;
9392*56bb7041Schristos 	    }
9393*56bb7041Schristos 	  else
9394*56bb7041Schristos 	    einfo (_("%F%P: invalid origin for memory region %s\n"),
9395*56bb7041Schristos 		   r->name_list.name);
9396*56bb7041Schristos 	}
9397*56bb7041Schristos       if (r->length_exp)
9398*56bb7041Schristos 	{
9399*56bb7041Schristos 	  exp_fold_tree_no_dot (r->length_exp);
9400*56bb7041Schristos 	  if (expld.result.valid_p)
9401*56bb7041Schristos 	    r->length = expld.result.value;
9402*56bb7041Schristos 	  else
9403*56bb7041Schristos 	    einfo (_("%F%P: invalid length for memory region %s\n"),
9404*56bb7041Schristos 		   r->name_list.name);
9405*56bb7041Schristos 	}
9406*56bb7041Schristos     }
9407*56bb7041Schristos }
9408*56bb7041Schristos 
9409*56bb7041Schristos void
lang_add_unique(const char * name)9410*56bb7041Schristos lang_add_unique (const char *name)
9411*56bb7041Schristos {
9412*56bb7041Schristos   struct unique_sections *ent;
9413*56bb7041Schristos 
9414*56bb7041Schristos   for (ent = unique_section_list; ent; ent = ent->next)
9415*56bb7041Schristos     if (strcmp (ent->name, name) == 0)
9416*56bb7041Schristos       return;
9417*56bb7041Schristos 
9418*56bb7041Schristos   ent = (struct unique_sections *) xmalloc (sizeof *ent);
9419*56bb7041Schristos   ent->name = xstrdup (name);
9420*56bb7041Schristos   ent->next = unique_section_list;
9421*56bb7041Schristos   unique_section_list = ent;
9422*56bb7041Schristos }
9423*56bb7041Schristos 
9424*56bb7041Schristos /* Append the list of dynamic symbols to the existing one.  */
9425*56bb7041Schristos 
9426*56bb7041Schristos void
lang_append_dynamic_list(struct bfd_elf_dynamic_list ** list_p,struct bfd_elf_version_expr * dynamic)9427*56bb7041Schristos lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p,
9428*56bb7041Schristos 			  struct bfd_elf_version_expr *dynamic)
9429*56bb7041Schristos {
9430*56bb7041Schristos   if (*list_p)
9431*56bb7041Schristos     {
9432*56bb7041Schristos       struct bfd_elf_version_expr *tail;
9433*56bb7041Schristos       for (tail = dynamic; tail->next != NULL; tail = tail->next)
9434*56bb7041Schristos 	;
9435*56bb7041Schristos       tail->next = (*list_p)->head.list;
9436*56bb7041Schristos       (*list_p)->head.list = dynamic;
9437*56bb7041Schristos     }
9438*56bb7041Schristos   else
9439*56bb7041Schristos     {
9440*56bb7041Schristos       struct bfd_elf_dynamic_list *d;
9441*56bb7041Schristos 
9442*56bb7041Schristos       d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
9443*56bb7041Schristos       d->head.list = dynamic;
9444*56bb7041Schristos       d->match = lang_vers_match;
9445*56bb7041Schristos       *list_p = d;
9446*56bb7041Schristos     }
9447*56bb7041Schristos }
9448*56bb7041Schristos 
9449*56bb7041Schristos /* Append the list of C++ typeinfo dynamic symbols to the existing
9450*56bb7041Schristos    one.  */
9451*56bb7041Schristos 
9452*56bb7041Schristos void
lang_append_dynamic_list_cpp_typeinfo(void)9453*56bb7041Schristos lang_append_dynamic_list_cpp_typeinfo (void)
9454*56bb7041Schristos {
9455*56bb7041Schristos   const char *symbols[] =
9456*56bb7041Schristos     {
9457*56bb7041Schristos       "typeinfo name for*",
9458*56bb7041Schristos       "typeinfo for*"
9459*56bb7041Schristos     };
9460*56bb7041Schristos   struct bfd_elf_version_expr *dynamic = NULL;
9461*56bb7041Schristos   unsigned int i;
9462*56bb7041Schristos 
9463*56bb7041Schristos   for (i = 0; i < ARRAY_SIZE (symbols); i++)
9464*56bb7041Schristos     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
9465*56bb7041Schristos 				     FALSE);
9466*56bb7041Schristos 
9467*56bb7041Schristos   lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
9468*56bb7041Schristos }
9469*56bb7041Schristos 
9470*56bb7041Schristos /* Append the list of C++ operator new and delete dynamic symbols to the
9471*56bb7041Schristos    existing one.  */
9472*56bb7041Schristos 
9473*56bb7041Schristos void
lang_append_dynamic_list_cpp_new(void)9474*56bb7041Schristos lang_append_dynamic_list_cpp_new (void)
9475*56bb7041Schristos {
9476*56bb7041Schristos   const char *symbols[] =
9477*56bb7041Schristos     {
9478*56bb7041Schristos       "operator new*",
9479*56bb7041Schristos       "operator delete*"
9480*56bb7041Schristos     };
9481*56bb7041Schristos   struct bfd_elf_version_expr *dynamic = NULL;
9482*56bb7041Schristos   unsigned int i;
9483*56bb7041Schristos 
9484*56bb7041Schristos   for (i = 0; i < ARRAY_SIZE (symbols); i++)
9485*56bb7041Schristos     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
9486*56bb7041Schristos 				     FALSE);
9487*56bb7041Schristos 
9488*56bb7041Schristos   lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
9489*56bb7041Schristos }
9490*56bb7041Schristos 
9491*56bb7041Schristos /* Scan a space and/or comma separated string of features.  */
9492*56bb7041Schristos 
9493*56bb7041Schristos void
lang_ld_feature(char * str)9494*56bb7041Schristos lang_ld_feature (char *str)
9495*56bb7041Schristos {
9496*56bb7041Schristos   char *p, *q;
9497*56bb7041Schristos 
9498*56bb7041Schristos   p = str;
9499*56bb7041Schristos   while (*p)
9500*56bb7041Schristos     {
9501*56bb7041Schristos       char sep;
9502*56bb7041Schristos       while (*p == ',' || ISSPACE (*p))
9503*56bb7041Schristos 	++p;
9504*56bb7041Schristos       if (!*p)
9505*56bb7041Schristos 	break;
9506*56bb7041Schristos       q = p + 1;
9507*56bb7041Schristos       while (*q && *q != ',' && !ISSPACE (*q))
9508*56bb7041Schristos 	++q;
9509*56bb7041Schristos       sep = *q;
9510*56bb7041Schristos       *q = 0;
9511*56bb7041Schristos       if (strcasecmp (p, "SANE_EXPR") == 0)
9512*56bb7041Schristos 	config.sane_expr = TRUE;
9513*56bb7041Schristos       else
9514*56bb7041Schristos 	einfo (_("%X%P: unknown feature `%s'\n"), p);
9515*56bb7041Schristos       *q = sep;
9516*56bb7041Schristos       p = q;
9517*56bb7041Schristos     }
9518*56bb7041Schristos }
9519*56bb7041Schristos 
9520*56bb7041Schristos /* Pretty print memory amount.  */
9521*56bb7041Schristos 
9522*56bb7041Schristos static void
lang_print_memory_size(bfd_vma sz)9523*56bb7041Schristos lang_print_memory_size (bfd_vma sz)
9524*56bb7041Schristos {
9525*56bb7041Schristos   if ((sz & 0x3fffffff) == 0)
9526*56bb7041Schristos     printf ("%10" BFD_VMA_FMT "u GB", sz >> 30);
9527*56bb7041Schristos   else if ((sz & 0xfffff) == 0)
9528*56bb7041Schristos     printf ("%10" BFD_VMA_FMT "u MB", sz >> 20);
9529*56bb7041Schristos   else if ((sz & 0x3ff) == 0)
9530*56bb7041Schristos     printf ("%10" BFD_VMA_FMT "u KB", sz >> 10);
9531*56bb7041Schristos   else
9532*56bb7041Schristos     printf (" %10" BFD_VMA_FMT "u B", sz);
9533*56bb7041Schristos }
9534*56bb7041Schristos 
9535*56bb7041Schristos /* Implement --print-memory-usage: disply per region memory usage.  */
9536*56bb7041Schristos 
9537*56bb7041Schristos void
lang_print_memory_usage(void)9538*56bb7041Schristos lang_print_memory_usage (void)
9539*56bb7041Schristos {
9540*56bb7041Schristos   lang_memory_region_type *r;
9541*56bb7041Schristos 
9542*56bb7041Schristos   printf ("Memory region         Used Size  Region Size  %%age Used\n");
9543*56bb7041Schristos   for (r = lang_memory_region_list; r->next != NULL; r = r->next)
9544*56bb7041Schristos     {
9545*56bb7041Schristos       bfd_vma used_length = r->current - r->origin;
9546*56bb7041Schristos 
9547*56bb7041Schristos       printf ("%16s: ",r->name_list.name);
9548*56bb7041Schristos       lang_print_memory_size (used_length);
9549*56bb7041Schristos       lang_print_memory_size ((bfd_vma) r->length);
9550*56bb7041Schristos 
9551*56bb7041Schristos       if (r->length != 0)
9552*56bb7041Schristos 	{
9553*56bb7041Schristos 	  double percent = used_length * 100.0 / r->length;
9554*56bb7041Schristos 	  printf ("    %6.2f%%", percent);
9555*56bb7041Schristos 	}
9556*56bb7041Schristos       printf ("\n");
9557*56bb7041Schristos     }
9558*56bb7041Schristos }
9559