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