Merge from vendor branch INTEL_ACPICA:
[dragonfly.git] / contrib / binutils-2.15 / ld / mri.c
1 /* mri.c -- handle MRI style linker scripts
2    Copyright 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2002,
3    2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD 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 2, or (at your option)
10 any later version.
11
12 GLD 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 GLD; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21
22    This bit does the tree decoration when MRI style link scripts
23    are parsed.
24
25    Contributed by Steve Chamberlain <sac@cygnus.com>.  */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "ld.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldmisc.h"
33 #include "mri.h"
34 #include <ldgram.h>
35 #include "libiberty.h"
36
37 struct section_name_struct {
38   struct section_name_struct *next;
39   const char *name;
40   const char *alias;
41   etree_type *vma;
42   etree_type *align;
43   etree_type *subalign;
44   int ok_to_load;
45 };
46
47 unsigned int symbol_truncate = 10000;
48 struct section_name_struct *order;
49 struct section_name_struct *only_load;
50 struct section_name_struct *address;
51 struct section_name_struct *alias;
52
53 struct section_name_struct *alignment;
54 struct section_name_struct *subalignment;
55
56 static struct section_name_struct **
57 lookup (const char *name, struct section_name_struct **list)
58 {
59   struct section_name_struct **ptr = list;
60
61   while (*ptr)
62     {
63       if (strcmp (name, (*ptr)->name) == 0)
64         /* If this is a match, delete it, we only keep the last instance
65            of any name.  */
66         *ptr = (*ptr)->next;
67       else
68         ptr = &((*ptr)->next);
69     }
70
71   *ptr = 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 #if 0   /* We don't bother with memory regions.  */
124   /* Create the regions.  */
125   {
126     lang_memory_region_type *r;
127
128     r = lang_memory_region_lookup("long");
129     r->current = r->origin = exp_get_vma (base, (bfd_vma)0, "origin",
130                                           lang_first_phase_enum);
131     r->length = (bfd_size_type) exp_get_vma (0, ~(bfd_vma) 0, "length",
132                                              lang_first_phase_enum);
133   }
134 #endif
135
136   /* Now build the statements for the ldlang machine.  */
137
138   /* Attach the addresses of any which have addresses,
139      and add the ones not mentioned.  */
140   if (address != NULL)
141     {
142       struct section_name_struct *alist;
143       struct section_name_struct *olist;
144
145       if (order == NULL)
146         order = address;
147
148       for (alist = address;
149            alist != NULL;
150            alist = alist->next)
151         {
152           int done = 0;
153
154           for (olist = order; done == 0 && olist != NULL; olist = olist->next)
155             {
156               if (strcmp (alist->name, olist->name) == 0)
157                 {
158                   olist->vma = alist->vma;
159                   done = 1;
160                 }
161             }
162
163           if (!done)
164             {
165               /* Add this onto end of order list.  */
166               mri_add_to_list (&order, alist->name, alist->vma, 0, 0, 0);
167             }
168         }
169     }
170
171   /* If we're only supposed to load a subset of them in, then prune
172      the list.  */
173   if (only_load != NULL)
174     {
175       struct section_name_struct *ptr1;
176       struct section_name_struct *ptr2;
177
178       if (order == NULL)
179         order = only_load;
180
181       /* See if this name is in the list, if it is then we can load it.  */
182       for (ptr1 = only_load; ptr1; ptr1 = ptr1->next)
183         for (ptr2 = order; ptr2; ptr2 = ptr2->next)
184           if (strcmp (ptr2->name, ptr1->name) == 0)
185             ptr2->ok_to_load = 1;
186     }
187   else
188     {
189       /* No only load list, so everything is ok to load.  */
190       struct section_name_struct *ptr;
191
192       for (ptr = order; ptr; ptr = ptr->next)
193         ptr->ok_to_load = 1;
194     }
195
196   /* Create the order of sections to load.  */
197   if (order != NULL)
198     {
199       /* Been told to output the sections in a certain order.  */
200       struct section_name_struct *p = order;
201
202       while (p)
203         {
204           struct section_name_struct *aptr;
205           etree_type *align = 0;
206           etree_type *subalign = 0;
207           struct wildcard_list *tmp;
208
209           /* See if an alignment has been specified.  */
210           for (aptr = alignment; aptr; aptr = aptr->next)
211             if (strcmp (aptr->name, p->name) == 0)
212               align = aptr->align;
213
214           for (aptr = subalignment; aptr; aptr = aptr->next)
215             if (strcmp (aptr->name, p->name) == 0)
216               subalign = aptr->subalign;
217
218           if (base == 0)
219             base = p->vma ? p->vma : exp_nameop (NAME, ".");
220
221           lang_enter_output_section_statement (p->name, base,
222                                                p->ok_to_load ? 0 : noload_section,
223                                                align, subalign, NULL);
224           base = 0;
225           tmp = xmalloc (sizeof *tmp);
226           tmp->next = NULL;
227           tmp->spec.name = p->name;
228           tmp->spec.exclude_name_list = NULL;
229           tmp->spec.sorted = FALSE;
230           lang_add_wild (NULL, tmp, FALSE);
231
232           /* If there is an alias for this section, add it too.  */
233           for (aptr = alias; aptr; aptr = aptr->next)
234             if (strcmp (aptr->alias, p->name) == 0)
235               {
236                 tmp = xmalloc (sizeof *tmp);
237                 tmp->next = NULL;
238                 tmp->spec.name = aptr->name;
239                 tmp->spec.exclude_name_list = NULL;
240                 tmp->spec.sorted = FALSE;
241                 lang_add_wild (NULL, tmp, FALSE);
242               }
243
244           lang_leave_output_section_statement (0, "*default*", NULL, NULL);
245
246           p = p->next;
247         }
248     }
249
250   done_tree = 1;
251 }
252
253 void
254 mri_load (const char *name)
255 {
256   base = 0;
257   lang_add_input_file (name, lang_input_file_is_file_enum, NULL);
258 #if 0
259   lang_leave_output_section_statement (0, "*default*");
260 #endif
261 }
262
263 void
264 mri_order (const char *name)
265 {
266   mri_add_to_list (&order, name, 0, 0, 0, 0);
267 }
268
269 void
270 mri_alias (const char *want, const char *is, int isn)
271 {
272   if (!is)
273     {
274       char buf[20];
275
276       /* Some sections are digits.  */
277       sprintf (buf, "%d", isn);
278
279       is = xstrdup (buf);
280
281       if (is == NULL)
282         abort ();
283     }
284
285   mri_add_to_list (&alias, is, 0, want, 0, 0);
286 }
287
288 void
289 mri_name (const char *name)
290 {
291   lang_add_output (name, 1);
292 }
293
294 void
295 mri_format (const char *name)
296 {
297   if (strcmp (name, "S") == 0)
298     lang_add_output_format ("srec", NULL, NULL, 1);
299
300   else if (strcmp (name, "IEEE") == 0)
301     lang_add_output_format ("ieee", NULL, NULL, 1);
302
303   else if (strcmp (name, "COFF") == 0)
304     lang_add_output_format ("coff-m68k", NULL, NULL, 1);
305
306   else
307     einfo (_("%P%F: unknown format type %s\n"), name);
308 }
309
310 void
311 mri_public (const char *name, etree_type *exp)
312 {
313   lang_add_assignment (exp_assop ('=', name, exp));
314 }
315
316 void
317 mri_align (const char *name, etree_type *exp)
318 {
319   mri_add_to_list (&alignment, name, 0, 0, exp, 0);
320 }
321
322 void
323 mri_alignmod (const char *name, etree_type *exp)
324 {
325   mri_add_to_list (&subalignment, name, 0, 0, 0, exp);
326 }
327
328 void
329 mri_truncate (unsigned int exp)
330 {
331   symbol_truncate = exp;
332 }