Fixtypos in ChangeLogs, fix copyright dates in files
[platform/upstream/binutils.git] / ld / mri.c
1 /* mri.c -- handle MRI style linker scripts
2    Copyright 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000
3    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 **lookup
57   PARAMS ((const char *name, struct section_name_struct **list));
58 static void mri_add_to_list PARAMS ((struct section_name_struct **list,
59                                      const char *name, etree_type *vma,
60                                      const char *zalias, etree_type *align,
61                                      etree_type *subalign));
62
63 static struct section_name_struct **
64 lookup (name, list)
65      CONST char *name;
66      struct section_name_struct **list;
67 {
68   struct section_name_struct **ptr = list;
69
70   while (*ptr)
71     {
72       if (strcmp (name, (*ptr)->name) == 0)
73         /* If this is a match, delete it, we only keep the last instance
74            of any name.  */
75         *ptr = (*ptr)->next;
76       else
77         ptr = &((*ptr)->next);
78     }
79
80   *ptr = (struct section_name_struct *) xmalloc (sizeof (struct section_name_struct));
81   return ptr;
82 }
83
84 static void
85 mri_add_to_list (list, name, vma, zalias, align, subalign)
86      struct section_name_struct **list;
87      CONST char *name;
88      etree_type *vma;
89      CONST char *zalias;
90      etree_type *align;
91      etree_type *subalign;
92 {
93   struct section_name_struct **ptr = lookup (name, list);
94
95   (*ptr)->name = name;
96   (*ptr)->vma = vma;
97   (*ptr)->next = (struct section_name_struct *) NULL;
98   (*ptr)->ok_to_load = 0;
99   (*ptr)->alias = zalias;
100   (*ptr)->align = align;
101   (*ptr)->subalign = subalign;
102 }
103
104 void
105 mri_output_section (name, vma)
106      CONST char *name;
107      etree_type *vma;
108 {
109   mri_add_to_list (&address, name, vma, 0, 0, 0);
110 }
111
112 /* If any ABSOLUTE <name> are in the script, only load those files
113    marked thus.  */
114
115 void
116 mri_only_load (name)
117      CONST char *name;
118 {
119   mri_add_to_list (&only_load, name, 0, 0, 0, 0);
120 }
121
122 void
123 mri_base (exp)
124      etree_type *exp;
125 {
126   base = exp;
127 }
128
129 static int done_tree = 0;
130
131 void
132 mri_draw_tree ()
133 {
134   if (done_tree)
135     return;
136
137 #if 0   /* We don't bother with memory regions.  */
138   /* Create the regions.  */
139   {
140     lang_memory_region_type *r;
141
142     r = lang_memory_region_lookup("long");
143     r->current = r->origin = exp_get_vma (base, (bfd_vma)0, "origin",
144                                           lang_first_phase_enum);
145     r->length = (bfd_size_type) exp_get_vma (0, (bfd_vma) ~((bfd_size_type)0),
146                                              "length", lang_first_phase_enum);
147   }
148 #endif
149
150   /* Now build the statements for the ldlang machine.  */
151
152   /* Attatch the addresses of any which have addresses,
153      and add the ones not mentioned.  */
154   if (address != (struct section_name_struct *) NULL)
155     {
156       struct section_name_struct *alist;
157       struct section_name_struct *olist;
158
159       if (order == (struct section_name_struct *) NULL)
160         order = address;
161
162       for (alist = address;
163            alist != (struct section_name_struct *) NULL;
164            alist = alist->next)
165         {
166           int done = 0;
167
168           for (olist = order;
169                done == 0 && olist != (struct section_name_struct *) NULL;
170                olist = olist->next)
171             {
172               if (strcmp (alist->name, olist->name) == 0)
173                 {
174                   olist->vma = alist->vma;
175                   done = 1;
176                 }
177             }
178
179           if (!done)
180             {
181               /* Add this onto end of order list.  */
182               mri_add_to_list (&order, alist->name, alist->vma, 0, 0, 0);
183             }
184         }
185     }
186
187   /* If we're only supposed to load a subset of them in, then prune
188      the list.  */
189   if (only_load != (struct section_name_struct *) NULL)
190     {
191       struct section_name_struct *ptr1;
192       struct section_name_struct *ptr2;
193
194       if (order == (struct section_name_struct *) NULL)
195         order = only_load;
196
197       /* See if this name is in the list, if it is then we can load it.  */
198       for (ptr1 = only_load; ptr1; ptr1 = ptr1->next)
199         for (ptr2 = order; ptr2; ptr2 = ptr2->next)
200           if (strcmp (ptr2->name, ptr1->name) == 0)
201             ptr2->ok_to_load = 1;
202     }
203   else
204     {
205       /* No only load list, so everything is ok to load.  */
206       struct section_name_struct *ptr;
207
208       for (ptr = order; ptr; ptr = ptr->next)
209         ptr->ok_to_load = 1;
210     }
211
212   /* Create the order of sections to load.  */
213   if (order != (struct section_name_struct *) NULL)
214     {
215       /* Been told to output the sections in a certain order.  */
216       struct section_name_struct *p = order;
217
218       while (p)
219         {
220           struct section_name_struct *aptr;
221           etree_type *align = 0;
222           etree_type *subalign = 0;
223
224           /* See if an alignment has been specified.  */
225           for (aptr = alignment; aptr; aptr = aptr->next)
226             if (strcmp (aptr->name, p->name) == 0)
227               align = aptr->align;
228
229           for (aptr = subalignment; aptr; aptr = aptr->next)
230             if (strcmp (aptr->name, p->name) == 0)
231               subalign = aptr->subalign;
232
233           if (base == 0)
234             base = p->vma ? p->vma : exp_nameop (NAME, ".");
235
236           lang_enter_output_section_statement (p->name, base,
237                                                p->ok_to_load ? 0 : noload_section,
238                                                1, align, subalign,
239                                                (etree_type *) NULL);
240           base = 0;
241           lang_add_wild (p->name, false, (char *) NULL, false, false, NULL);
242
243           /* If there is an alias for this section, add it too.  */
244           for (aptr = alias; aptr; aptr = aptr->next)
245             if (strcmp (aptr->alias, p->name) == 0)
246               lang_add_wild (aptr->name, false, (char *) NULL, false, false, NULL);
247
248           lang_leave_output_section_statement
249             (0, "*default*", (struct lang_output_section_phdr_list *) NULL,
250              "*default*");
251
252           p = p->next;
253         }
254     }
255
256   done_tree = 1;
257 }
258
259 void
260 mri_load (name)
261      CONST char *name;
262 {
263   base = 0;
264   lang_add_input_file (name,
265                        lang_input_file_is_file_enum, (char *) NULL);
266 #if 0
267   lang_leave_output_section_statement (0, "*default*");
268 #endif
269 }
270
271 void
272 mri_order (name)
273      CONST char *name;
274 {
275   mri_add_to_list (&order, name, 0, 0, 0, 0);
276 }
277
278 void
279 mri_alias (want, is, isn)
280      CONST char *want;
281      CONST char *is;
282      int isn;
283 {
284   if (!is)
285     {
286       char buf[20];
287
288       /* Some sections are digits.  */
289       sprintf (buf, "%d", isn);
290
291       is = xstrdup (buf);
292
293       if (is == NULL)
294         abort ();
295     }
296
297   mri_add_to_list (&alias, is, 0, want, 0, 0);
298 }
299
300 void
301 mri_name (name)
302      CONST char *name;
303 {
304   lang_add_output (name, 1);
305 }
306
307 void
308 mri_format (name)
309      CONST char *name;
310 {
311   if (strcmp (name, "S") == 0)
312     lang_add_output_format ("srec", (char *) NULL, (char *) NULL, 1);
313
314   else if (strcmp (name, "IEEE") == 0)
315     lang_add_output_format ("ieee", (char *) NULL, (char *) NULL, 1);
316
317   else if (strcmp (name, "COFF") == 0)
318     lang_add_output_format ("coff-m68k", (char *) NULL, (char *) NULL, 1);
319
320   else
321     einfo (_("%P%F: unknown format type %s\n"), name);
322 }
323
324 void
325 mri_public (name, exp)
326      CONST char *name;
327      etree_type *exp;
328 {
329   lang_add_assignment (exp_assop ('=', name, exp));
330 }
331
332 void
333 mri_align (name, exp)
334      CONST char *name;
335      etree_type *exp;
336 {
337   mri_add_to_list (&alignment, name, 0, 0, exp, 0);
338 }
339
340 void
341 mri_alignmod (name, exp)
342      CONST char *name;
343      etree_type *exp;
344 {
345   mri_add_to_list (&subalignment, name, 0, 0, 0, exp);
346 }
347
348 void
349 mri_truncate (exp)
350      unsigned int exp;
351 {
352   symbol_truncate = exp;
353 }