| 1 | /* mri.c -- handle MRI style linker scripts |
| 2 | Copyright 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, |
| 3 | 2002, 2003, 2004, 2005, 2007, 2011 Free Software Foundation, Inc. |
| 4 | Contributed by Steve Chamberlain <sac@cygnus.com>. |
| 5 | |
| 6 | This file is part of the GNU Binutils. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. */ |
| 22 | |
| 23 | |
| 24 | /* This bit does the tree decoration when MRI style link scripts |
| 25 | are parsed. */ |
| 26 | |
| 27 | #include "sysdep.h" |
| 28 | #include "bfd.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 | 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; |
| 52 | |
| 53 | static struct section_name_struct *alignment; |
| 54 | static 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 = (struct section_name_struct *) |
| 72 | xmalloc (sizeof (struct section_name_struct)); |
| 73 | return ptr; |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | mri_add_to_list (struct section_name_struct **list, |
| 78 | const char *name, |
| 79 | etree_type *vma, |
| 80 | const char *zalias, |
| 81 | etree_type *align, |
| 82 | etree_type *subalign) |
| 83 | { |
| 84 | struct section_name_struct **ptr = lookup (name, list); |
| 85 | |
| 86 | (*ptr)->name = name; |
| 87 | (*ptr)->vma = vma; |
| 88 | (*ptr)->next = NULL; |
| 89 | (*ptr)->ok_to_load = 0; |
| 90 | (*ptr)->alias = zalias; |
| 91 | (*ptr)->align = align; |
| 92 | (*ptr)->subalign = subalign; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | mri_output_section (const char *name, etree_type *vma) |
| 97 | { |
| 98 | mri_add_to_list (&address, name, vma, 0, 0, 0); |
| 99 | } |
| 100 | |
| 101 | /* If any ABSOLUTE <name> are in the script, only load those files |
| 102 | marked thus. */ |
| 103 | |
| 104 | void |
| 105 | mri_only_load (const char *name) |
| 106 | { |
| 107 | mri_add_to_list (&only_load, name, 0, 0, 0, 0); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | mri_base (etree_type *exp) |
| 112 | { |
| 113 | base = exp; |
| 114 | } |
| 115 | |
| 116 | static int done_tree = 0; |
| 117 | |
| 118 | void |
| 119 | mri_draw_tree (void) |
| 120 | { |
| 121 | if (done_tree) |
| 122 | return; |
| 123 | |
| 124 | /* Now build the statements for the ldlang machine. */ |
| 125 | |
| 126 | /* Attach the addresses of any which have addresses, |
| 127 | and add the ones not mentioned. */ |
| 128 | if (address != NULL) |
| 129 | { |
| 130 | struct section_name_struct *alist; |
| 131 | struct section_name_struct *olist; |
| 132 | |
| 133 | if (order == NULL) |
| 134 | order = address; |
| 135 | |
| 136 | for (alist = address; |
| 137 | alist != NULL; |
| 138 | alist = alist->next) |
| 139 | { |
| 140 | int done = 0; |
| 141 | |
| 142 | for (olist = order; done == 0 && olist != NULL; olist = olist->next) |
| 143 | { |
| 144 | if (strcmp (alist->name, olist->name) == 0) |
| 145 | { |
| 146 | olist->vma = alist->vma; |
| 147 | done = 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (!done) |
| 152 | { |
| 153 | /* Add this onto end of order list. */ |
| 154 | mri_add_to_list (&order, alist->name, alist->vma, 0, 0, 0); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* If we're only supposed to load a subset of them in, then prune |
| 160 | the list. */ |
| 161 | if (only_load != NULL) |
| 162 | { |
| 163 | struct section_name_struct *ptr1; |
| 164 | struct section_name_struct *ptr2; |
| 165 | |
| 166 | if (order == NULL) |
| 167 | order = only_load; |
| 168 | |
| 169 | /* See if this name is in the list, if it is then we can load it. */ |
| 170 | for (ptr1 = only_load; ptr1; ptr1 = ptr1->next) |
| 171 | for (ptr2 = order; ptr2; ptr2 = ptr2->next) |
| 172 | if (strcmp (ptr2->name, ptr1->name) == 0) |
| 173 | ptr2->ok_to_load = 1; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | /* No only load list, so everything is ok to load. */ |
| 178 | struct section_name_struct *ptr; |
| 179 | |
| 180 | for (ptr = order; ptr; ptr = ptr->next) |
| 181 | ptr->ok_to_load = 1; |
| 182 | } |
| 183 | |
| 184 | /* Create the order of sections to load. */ |
| 185 | if (order != NULL) |
| 186 | { |
| 187 | /* Been told to output the sections in a certain order. */ |
| 188 | struct section_name_struct *p = order; |
| 189 | |
| 190 | while (p) |
| 191 | { |
| 192 | struct section_name_struct *aptr; |
| 193 | etree_type *align = 0; |
| 194 | etree_type *subalign = 0; |
| 195 | struct wildcard_list *tmp; |
| 196 | |
| 197 | /* See if an alignment has been specified. */ |
| 198 | for (aptr = alignment; aptr; aptr = aptr->next) |
| 199 | if (strcmp (aptr->name, p->name) == 0) |
| 200 | align = aptr->align; |
| 201 | |
| 202 | for (aptr = subalignment; aptr; aptr = aptr->next) |
| 203 | if (strcmp (aptr->name, p->name) == 0) |
| 204 | subalign = aptr->subalign; |
| 205 | |
| 206 | if (base == 0) |
| 207 | base = p->vma ? p->vma : exp_nameop (NAME, "."); |
| 208 | |
| 209 | lang_enter_output_section_statement (p->name, base, |
| 210 | p->ok_to_load ? normal_section : noload_section, |
| 211 | align, subalign, NULL, 0); |
| 212 | base = 0; |
| 213 | tmp = (struct wildcard_list *) xmalloc (sizeof *tmp); |
| 214 | tmp->next = NULL; |
| 215 | tmp->spec.name = p->name; |
| 216 | tmp->spec.exclude_name_list = NULL; |
| 217 | tmp->spec.sorted = none; |
| 218 | tmp->spec.section_flag_list = NULL; |
| 219 | lang_add_wild (NULL, tmp, FALSE); |
| 220 | |
| 221 | /* If there is an alias for this section, add it too. */ |
| 222 | for (aptr = alias; aptr; aptr = aptr->next) |
| 223 | if (strcmp (aptr->alias, p->name) == 0) |
| 224 | { |
| 225 | tmp = (struct wildcard_list *) xmalloc (sizeof *tmp); |
| 226 | tmp->next = NULL; |
| 227 | tmp->spec.name = aptr->name; |
| 228 | tmp->spec.exclude_name_list = NULL; |
| 229 | tmp->spec.sorted = none; |
| 230 | tmp->spec.section_flag_list = NULL; |
| 231 | lang_add_wild (NULL, tmp, FALSE); |
| 232 | } |
| 233 | |
| 234 | lang_leave_output_section_statement (0, "*default*", NULL, NULL); |
| 235 | |
| 236 | p = p->next; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | done_tree = 1; |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | mri_load (const char *name) |
| 245 | { |
| 246 | base = 0; |
| 247 | lang_add_input_file (name, lang_input_file_is_file_enum, NULL); |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | mri_order (const char *name) |
| 252 | { |
| 253 | mri_add_to_list (&order, name, 0, 0, 0, 0); |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | mri_alias (const char *want, const char *is, int isn) |
| 258 | { |
| 259 | if (!is) |
| 260 | { |
| 261 | char buf[20]; |
| 262 | |
| 263 | /* Some sections are digits. */ |
| 264 | sprintf (buf, "%d", isn); |
| 265 | |
| 266 | is = xstrdup (buf); |
| 267 | |
| 268 | if (is == NULL) |
| 269 | abort (); |
| 270 | } |
| 271 | |
| 272 | mri_add_to_list (&alias, is, 0, want, 0, 0); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | mri_name (const char *name) |
| 277 | { |
| 278 | lang_add_output (name, 1); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | mri_format (const char *name) |
| 283 | { |
| 284 | if (strcmp (name, "S") == 0) |
| 285 | lang_add_output_format ("srec", NULL, NULL, 1); |
| 286 | |
| 287 | else if (strcmp (name, "IEEE") == 0) |
| 288 | lang_add_output_format ("ieee", NULL, NULL, 1); |
| 289 | |
| 290 | else if (strcmp (name, "COFF") == 0) |
| 291 | lang_add_output_format ("coff-m68k", NULL, NULL, 1); |
| 292 | |
| 293 | else |
| 294 | einfo (_("%P%F: unknown format type %s\n"), name); |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | mri_public (const char *name, etree_type *exp) |
| 299 | { |
| 300 | lang_add_assignment (exp_assign (name, exp)); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | mri_align (const char *name, etree_type *exp) |
| 305 | { |
| 306 | mri_add_to_list (&alignment, name, 0, 0, exp, 0); |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | mri_alignmod (const char *name, etree_type *exp) |
| 311 | { |
| 312 | mri_add_to_list (&subalignment, name, 0, 0, 0, exp); |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | mri_truncate (unsigned int exp) |
| 317 | { |
| 318 | symbol_truncate = exp; |
| 319 | } |