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