daily update
[external/binutils.git] / ld / mri.c
1 /* mri.c -- handle MRI style linker scripts
2    Copyright (C) 1991-2014 Free Software Foundation, Inc.
3    Contributed by Steve Chamberlain <sac@cygnus.com>.
4
5    This file is part of the GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22
23 /* This bit does the tree decoration when MRI style link scripts
24    are parsed.  */
25
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "ld.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldmisc.h"
32 #include "mri.h"
33 #include <ldgram.h>
34 #include "libiberty.h"
35
36 struct section_name_struct {
37   struct section_name_struct *next;
38   const char *name;
39   const char *alias;
40   etree_type *vma;
41   etree_type *align;
42   etree_type *subalign;
43   int ok_to_load;
44 };
45
46 static unsigned int symbol_truncate = 10000;
47 static struct section_name_struct *order;
48 static struct section_name_struct *only_load;
49 static struct section_name_struct *address;
50 static struct section_name_struct *alias;
51
52 static struct section_name_struct *alignment;
53 static struct section_name_struct *subalignment;
54
55 static struct section_name_struct **
56 lookup (const char *name, struct section_name_struct **list)
57 {
58   struct section_name_struct **ptr = list;
59
60   while (*ptr)
61     {
62       if (strcmp (name, (*ptr)->name) == 0)
63         /* If this is a match, delete it, we only keep the last instance
64            of any name.  */
65         *ptr = (*ptr)->next;
66       else
67         ptr = &((*ptr)->next);
68     }
69
70   *ptr = (struct section_name_struct *)
71       xmalloc (sizeof (struct section_name_struct));
72   return ptr;
73 }
74
75 static void
76 mri_add_to_list (struct section_name_struct **list,
77                  const char *name,
78                  etree_type *vma,
79                  const char *zalias,
80                  etree_type *align,
81                  etree_type *subalign)
82 {
83   struct section_name_struct **ptr = lookup (name, list);
84
85   (*ptr)->name = name;
86   (*ptr)->vma = vma;
87   (*ptr)->next = NULL;
88   (*ptr)->ok_to_load = 0;
89   (*ptr)->alias = zalias;
90   (*ptr)->align = align;
91   (*ptr)->subalign = subalign;
92 }
93
94 void
95 mri_output_section (const char *name, etree_type *vma)
96 {
97   mri_add_to_list (&address, name, vma, 0, 0, 0);
98 }
99
100 /* If any ABSOLUTE <name> are in the script, only load those files
101    marked thus.  */
102
103 void
104 mri_only_load (const char *name)
105 {
106   mri_add_to_list (&only_load, name, 0, 0, 0, 0);
107 }
108
109 void
110 mri_base (etree_type *exp)
111 {
112   base = exp;
113 }
114
115 static int done_tree = 0;
116
117 void
118 mri_draw_tree (void)
119 {
120   if (done_tree)
121     return;
122
123   /* Now build the statements for the ldlang machine.  */
124
125   /* Attach the addresses of any which have addresses,
126      and add the ones not mentioned.  */
127   if (address != NULL)
128     {
129       struct section_name_struct *alist;
130       struct section_name_struct *olist;
131
132       if (order == NULL)
133         order = address;
134
135       for (alist = address;
136            alist != NULL;
137            alist = alist->next)
138         {
139           int done = 0;
140
141           for (olist = order; done == 0 && olist != NULL; olist = olist->next)
142             {
143               if (strcmp (alist->name, olist->name) == 0)
144                 {
145                   olist->vma = alist->vma;
146                   done = 1;
147                 }
148             }
149
150           if (!done)
151             {
152               /* Add this onto end of order list.  */
153               mri_add_to_list (&order, alist->name, alist->vma, 0, 0, 0);
154             }
155         }
156     }
157
158   /* If we're only supposed to load a subset of them in, then prune
159      the list.  */
160   if (only_load != NULL)
161     {
162       struct section_name_struct *ptr1;
163       struct section_name_struct *ptr2;
164
165       if (order == NULL)
166         order = only_load;
167
168       /* See if this name is in the list, if it is then we can load it.  */
169       for (ptr1 = only_load; ptr1; ptr1 = ptr1->next)
170         for (ptr2 = order; ptr2; ptr2 = ptr2->next)
171           if (strcmp (ptr2->name, ptr1->name) == 0)
172             ptr2->ok_to_load = 1;
173     }
174   else
175     {
176       /* No only load list, so everything is ok to load.  */
177       struct section_name_struct *ptr;
178
179       for (ptr = order; ptr; ptr = ptr->next)
180         ptr->ok_to_load = 1;
181     }
182
183   /* Create the order of sections to load.  */
184   if (order != NULL)
185     {
186       /* Been told to output the sections in a certain order.  */
187       struct section_name_struct *p = order;
188
189       while (p)
190         {
191           struct section_name_struct *aptr;
192           etree_type *align = 0;
193           etree_type *subalign = 0;
194           struct wildcard_list *tmp;
195
196           /* See if an alignment has been specified.  */
197           for (aptr = alignment; aptr; aptr = aptr->next)
198             if (strcmp (aptr->name, p->name) == 0)
199               align = aptr->align;
200
201           for (aptr = subalignment; aptr; aptr = aptr->next)
202             if (strcmp (aptr->name, p->name) == 0)
203               subalign = aptr->subalign;
204
205           if (base == 0)
206             base = p->vma ? p->vma : exp_nameop (NAME, ".");
207
208           lang_enter_output_section_statement (p->name, base,
209                                                p->ok_to_load ? normal_section : noload_section,
210                                                align, subalign, NULL, 0, 0);
211           base = 0;
212           tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
213           tmp->next = NULL;
214           tmp->spec.name = p->name;
215           tmp->spec.exclude_name_list = NULL;
216           tmp->spec.sorted = none;
217           tmp->spec.section_flag_list = NULL;
218           lang_add_wild (NULL, tmp, FALSE);
219
220           /* If there is an alias for this section, add it too.  */
221           for (aptr = alias; aptr; aptr = aptr->next)
222             if (strcmp (aptr->alias, p->name) == 0)
223               {
224                 tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
225                 tmp->next = NULL;
226                 tmp->spec.name = aptr->name;
227                 tmp->spec.exclude_name_list = NULL;
228                 tmp->spec.sorted = none;
229                 tmp->spec.section_flag_list = NULL;
230                 lang_add_wild (NULL, tmp, FALSE);
231               }
232
233           lang_leave_output_section_statement (0, "*default*", NULL, NULL);
234
235           p = p->next;
236         }
237     }
238
239   done_tree = 1;
240 }
241
242 void
243 mri_load (const char *name)
244 {
245   base = 0;
246   lang_add_input_file (name, lang_input_file_is_file_enum, NULL);
247 }
248
249 void
250 mri_order (const char *name)
251 {
252   mri_add_to_list (&order, name, 0, 0, 0, 0);
253 }
254
255 void
256 mri_alias (const char *want, const char *is, int isn)
257 {
258   if (!is)
259     {
260       char buf[20];
261
262       /* Some sections are digits.  */
263       sprintf (buf, "%d", isn);
264
265       is = xstrdup (buf);
266
267       if (is == NULL)
268         abort ();
269     }
270
271   mri_add_to_list (&alias, is, 0, want, 0, 0);
272 }
273
274 void
275 mri_name (const char *name)
276 {
277   lang_add_output (name, 1);
278 }
279
280 void
281 mri_format (const char *name)
282 {
283   if (strcmp (name, "S") == 0)
284     lang_add_output_format ("srec", NULL, NULL, 1);
285
286   else if (strcmp (name, "IEEE") == 0)
287     lang_add_output_format ("ieee", NULL, NULL, 1);
288
289   else if (strcmp (name, "COFF") == 0)
290     lang_add_output_format ("coff-m68k", NULL, NULL, 1);
291
292   else
293     einfo (_("%P%F: unknown format type %s\n"), name);
294 }
295
296 void
297 mri_public (const char *name, etree_type *exp)
298 {
299   lang_add_assignment (exp_assign (name, exp, FALSE));
300 }
301
302 void
303 mri_align (const char *name, etree_type *exp)
304 {
305   mri_add_to_list (&alignment, name, 0, 0, exp, 0);
306 }
307
308 void
309 mri_alignmod (const char *name, etree_type *exp)
310 {
311   mri_add_to_list (&subalignment, name, 0, 0, 0, exp);
312 }
313
314 void
315 mri_truncate (unsigned int exp)
316 {
317   symbol_truncate = exp;
318 }