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