gdb: fix building on Darwin 9
[deliverable/binutils-gdb.git] / ld / ldexp.c
CommitLineData
252b5132 1/* This module handles expression trees.
a2b64bed 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
dab69f68 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
87f2a346 4 Free Software Foundation, Inc.
8c95a62e 5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
252b5132 6
f96b4a7b 7 This file is part of the GNU Binutils.
252b5132 8
f96b4a7b 9 This program is free software; you can redistribute it and/or modify
3ec57632 10 it under the terms of the GNU General Public License as published by
f96b4a7b
NC
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
252b5132 13
f96b4a7b 14 This program is distributed in the hope that it will be useful,
3ec57632
NC
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
252b5132 18
3ec57632 19 You should have received a copy of the GNU General Public License
f96b4a7b
NC
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
252b5132 24
8c95a62e 25/* This module is in charge of working out the contents of expressions.
252b5132 26
8c95a62e
KH
27 It has to keep track of the relative/absness of a symbol etc. This
28 is done by keeping all values in a struct (an etree_value_type)
29 which contains a value, a section to which it is relative and a
30 valid bit. */
252b5132 31
252b5132 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
252b5132
RH
34#include "bfdlink.h"
35
36#include "ld.h"
37#include "ldmain.h"
38#include "ldmisc.h"
39#include "ldexp.h"
f856040a 40#include "ldlex.h"
df2a7313 41#include <ldgram.h>
252b5132 42#include "ldlang.h"
c7d701b0 43#include "libiberty.h"
2c382fb6 44#include "safe-ctype.h"
252b5132 45
e9ee469a 46static void exp_fold_tree_1 (etree_type *);
e9ee469a 47static bfd_vma align_n (bfd_vma, bfd_vma);
2d20f7bf 48
ba916c8a
MM
49segment_type *segments;
50
e9ee469a 51struct ldexp_control expld;
fbbb9ac5 52
7b17f854 53/* Print the string representation of the given token. Surround it
b34976b6 54 with spaces if INFIX_P is TRUE. */
7b17f854 55
252b5132 56static void
1579bae1 57exp_print_token (token_code_type code, int infix_p)
252b5132 58{
4da711b1 59 static const struct
c7d701b0 60 {
8c95a62e 61 token_code_type code;
67baf8c4 62 const char * name;
c7d701b0
NC
63 }
64 table[] =
65 {
8c95a62e 66 { INT, "int" },
8c95a62e
KH
67 { NAME, "NAME" },
68 { PLUSEQ, "+=" },
69 { MINUSEQ, "-=" },
70 { MULTEQ, "*=" },
71 { DIVEQ, "/=" },
72 { LSHIFTEQ, "<<=" },
73 { RSHIFTEQ, ">>=" },
74 { ANDEQ, "&=" },
75 { OREQ, "|=" },
76 { OROR, "||" },
77 { ANDAND, "&&" },
78 { EQ, "==" },
79 { NE, "!=" },
80 { LE, "<=" },
81 { GE, ">=" },
82 { LSHIFT, "<<" },
7cecdbff 83 { RSHIFT, ">>" },
8c95a62e
KH
84 { ALIGN_K, "ALIGN" },
85 { BLOCK, "BLOCK" },
c7d701b0
NC
86 { QUAD, "QUAD" },
87 { SQUAD, "SQUAD" },
88 { LONG, "LONG" },
89 { SHORT, "SHORT" },
90 { BYTE, "BYTE" },
8c95a62e
KH
91 { SECTIONS, "SECTIONS" },
92 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
8c95a62e
KH
93 { MEMORY, "MEMORY" },
94 { DEFINED, "DEFINED" },
95 { TARGET_K, "TARGET" },
96 { SEARCH_DIR, "SEARCH_DIR" },
97 { MAP, "MAP" },
8c95a62e 98 { ENTRY, "ENTRY" },
c7d701b0 99 { NEXT, "NEXT" },
362c1d1a 100 { ALIGNOF, "ALIGNOF" },
c7d701b0
NC
101 { SIZEOF, "SIZEOF" },
102 { ADDR, "ADDR" },
103 { LOADADDR, "LOADADDR" },
24718e3b 104 { CONSTANT, "CONSTANT" },
8c0848b5
AM
105 { ABSOLUTE, "ABSOLUTE" },
106 { MAX_K, "MAX" },
107 { MIN_K, "MIN" },
108 { ASSERT_K, "ASSERT" },
1049f94e 109 { REL, "relocatable" },
2d20f7bf 110 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
8c37241b 111 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
ba916c8a 112 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
3ec57632
NC
113 { ORIGIN, "ORIGIN" },
114 { LENGTH, "LENGTH" },
ba916c8a 115 { SEGMENT_START, "SEGMENT_START" }
8c95a62e 116 };
252b5132
RH
117 unsigned int idx;
118
7b17f854
RS
119 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
120 if (table[idx].code == code)
121 break;
c7d701b0 122
7b17f854
RS
123 if (infix_p)
124 fputc (' ', config.map_file);
125
126 if (idx < ARRAY_SIZE (table))
127 fputs (table[idx].name, config.map_file);
128 else if (code < 127)
129 fputc (code, config.map_file);
c7d701b0 130 else
7b17f854
RS
131 fprintf (config.map_file, "<code %d>", code);
132
133 if (infix_p)
134 fputc (' ', config.map_file);
252b5132
RH
135}
136
4de2d33d 137static void
e9ee469a 138make_abs (void)
252b5132 139{
7542af2a
AM
140 if (expld.result.section != NULL)
141 expld.result.value += expld.result.section->vma;
e9ee469a 142 expld.result.section = bfd_abs_section_ptr;
252b5132
RH
143}
144
e9ee469a 145static void
1579bae1 146new_abs (bfd_vma value)
252b5132 147{
e9ee469a
AM
148 expld.result.valid_p = TRUE;
149 expld.result.section = bfd_abs_section_ptr;
150 expld.result.value = value;
151 expld.result.str = NULL;
252b5132
RH
152}
153
252b5132 154etree_type *
1579bae1 155exp_intop (bfd_vma value)
252b5132 156{
d3ce72d0
NC
157 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
158 new_e->type.node_code = INT;
dab69f68 159 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
160 new_e->type.lineno = lineno;
161 new_e->value.value = value;
162 new_e->value.str = NULL;
163 new_e->type.node_class = etree_value;
164 return new_e;
2c382fb6 165}
252b5132 166
2c382fb6 167etree_type *
1579bae1 168exp_bigintop (bfd_vma value, char *str)
2c382fb6 169{
d3ce72d0
NC
170 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
171 new_e->type.node_code = INT;
dab69f68 172 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
173 new_e->type.lineno = lineno;
174 new_e->value.value = value;
175 new_e->value.str = str;
176 new_e->type.node_class = etree_value;
177 return new_e;
252b5132
RH
178}
179
1049f94e 180/* Build an expression representing an unnamed relocatable value. */
252b5132
RH
181
182etree_type *
1579bae1 183exp_relop (asection *section, bfd_vma value)
252b5132 184{
d3ce72d0
NC
185 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
186 new_e->type.node_code = REL;
dab69f68 187 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
188 new_e->type.lineno = lineno;
189 new_e->type.node_class = etree_rel;
190 new_e->rel.section = section;
191 new_e->rel.value = value;
192 return new_e;
252b5132
RH
193}
194
7542af2a
AM
195static void
196new_number (bfd_vma value)
197{
198 expld.result.valid_p = TRUE;
199 expld.result.value = value;
200 expld.result.str = NULL;
201 expld.result.section = NULL;
202}
203
e9ee469a 204static void
5942515f 205new_rel (bfd_vma value, asection *section)
252b5132 206{
e9ee469a
AM
207 expld.result.valid_p = TRUE;
208 expld.result.value = value;
5942515f 209 expld.result.str = NULL;
e9ee469a 210 expld.result.section = section;
252b5132
RH
211}
212
e9ee469a
AM
213static void
214new_rel_from_abs (bfd_vma value)
252b5132 215{
d2667025
AM
216 asection *s = expld.section;
217
218 if (s == bfd_abs_section_ptr && expld.phase == lang_final_phase_enum)
219 s = section_for_dot ();
e9ee469a 220 expld.result.valid_p = TRUE;
d2667025 221 expld.result.value = value - s->vma;
e9ee469a 222 expld.result.str = NULL;
d2667025 223 expld.result.section = s;
252b5132
RH
224}
225
e9ee469a
AM
226static void
227fold_unary (etree_type *tree)
0ae1cf52 228{
e9ee469a
AM
229 exp_fold_tree_1 (tree->unary.child);
230 if (expld.result.valid_p)
0ae1cf52
AM
231 {
232 switch (tree->type.node_code)
233 {
234 case ALIGN_K:
e9ee469a 235 if (expld.phase != lang_first_phase_enum)
dea2f0a8 236 new_rel_from_abs (align_n (expld.dot, expld.result.value));
0ae1cf52 237 else
e9ee469a 238 expld.result.valid_p = FALSE;
0ae1cf52
AM
239 break;
240
241 case ABSOLUTE:
e9ee469a 242 make_abs ();
0ae1cf52
AM
243 break;
244
245 case '~':
e9ee469a 246 expld.result.value = ~expld.result.value;
0ae1cf52
AM
247 break;
248
249 case '!':
e9ee469a 250 expld.result.value = !expld.result.value;
0ae1cf52
AM
251 break;
252
253 case '-':
e9ee469a 254 expld.result.value = -expld.result.value;
0ae1cf52
AM
255 break;
256
257 case NEXT:
258 /* Return next place aligned to value. */
e9ee469a 259 if (expld.phase != lang_first_phase_enum)
0ae1cf52 260 {
e9ee469a
AM
261 make_abs ();
262 expld.result.value = align_n (expld.dot, expld.result.value);
0ae1cf52
AM
263 }
264 else
e9ee469a 265 expld.result.valid_p = FALSE;
0ae1cf52
AM
266 break;
267
268 case DATA_SEGMENT_END:
ea7c2e6c
AM
269 if (expld.phase == lang_first_phase_enum
270 || expld.section != bfd_abs_section_ptr)
0ae1cf52 271 {
ea7c2e6c
AM
272 expld.result.valid_p = FALSE;
273 }
274 else if (expld.dataseg.phase == exp_dataseg_align_seen
275 || expld.dataseg.phase == exp_dataseg_relro_seen)
276 {
277 expld.dataseg.phase = exp_dataseg_end_seen;
278 expld.dataseg.end = expld.result.value;
279 }
280 else if (expld.dataseg.phase == exp_dataseg_done
281 || expld.dataseg.phase == exp_dataseg_adjust
282 || expld.dataseg.phase == exp_dataseg_relro_adjust)
283 {
284 /* OK. */
0ae1cf52
AM
285 }
286 else
e9ee469a 287 expld.result.valid_p = FALSE;
0ae1cf52
AM
288 break;
289
290 default:
291 FAIL ();
292 break;
293 }
294 }
0ae1cf52
AM
295}
296
e9ee469a
AM
297static void
298fold_binary (etree_type *tree)
252b5132 299{
4ac0c898 300 etree_value_type lhs;
e9ee469a 301 exp_fold_tree_1 (tree->binary.lhs);
ba916c8a
MM
302
303 /* The SEGMENT_START operator is special because its first
8c0848b5
AM
304 operand is a string, not the name of a symbol. Note that the
305 operands have been swapped, so binary.lhs is second (default)
306 operand, binary.rhs is first operand. */
e9ee469a 307 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
ba916c8a
MM
308 {
309 const char *segment_name;
310 segment_type *seg;
7542af2a 311
ba916c8a
MM
312 /* Check to see if the user has overridden the default
313 value. */
314 segment_name = tree->binary.rhs->name.name;
315 for (seg = segments; seg; seg = seg->next)
316 if (strcmp (seg->name, segment_name) == 0)
317 {
c8ce5710
L
318 if (!seg->used
319 && config.magic_demand_paged
320 && (seg->value % config.maxpagesize) != 0)
321 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
322 segment_name);
ba916c8a 323 seg->used = TRUE;
7542af2a 324 new_rel_from_abs (seg->value);
ba916c8a
MM
325 break;
326 }
4ac0c898 327 return;
ba916c8a 328 }
252b5132 329
4ac0c898
AM
330 lhs = expld.result;
331 exp_fold_tree_1 (tree->binary.rhs);
332 expld.result.valid_p &= lhs.valid_p;
333
334 if (expld.result.valid_p)
335 {
7542af2a 336 if (lhs.section != expld.result.section)
252b5132 337 {
7542af2a
AM
338 /* If the values are from different sections, and neither is
339 just a number, make both the source arguments absolute. */
340 if (expld.result.section != NULL
341 && lhs.section != NULL)
342 {
343 make_abs ();
344 lhs.value += lhs.section->vma;
9bc8bb33 345 lhs.section = bfd_abs_section_ptr;
7542af2a
AM
346 }
347
348 /* If the rhs is just a number, keep the lhs section. */
349 else if (expld.result.section == NULL)
9bc8bb33
AM
350 {
351 expld.result.section = lhs.section;
352 /* Make this NULL so that we know one of the operands
353 was just a number, for later tests. */
354 lhs.section = NULL;
355 }
4ac0c898 356 }
9bc8bb33
AM
357 /* At this point we know that both operands have the same
358 section, or at least one of them is a plain number. */
252b5132 359
4ac0c898
AM
360 switch (tree->type.node_code)
361 {
9bc8bb33
AM
362 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
363 keep the section of one of their operands only when the
364 other operand is a plain number. Losing the section when
365 operating on two symbols, ie. a result of a plain number,
366 is required for subtraction and XOR. It's justifiable
367 for the other operations on the grounds that adding,
368 multiplying etc. two section relative values does not
369 really make sense unless they are just treated as
370 numbers.
371 The same argument could be made for many expressions
372 involving one symbol and a number. For example,
373 "1 << x" and "100 / x" probably should not be given the
374 section of x. The trouble is that if we fuss about such
375 things the rules become complex and it is onerous to
376 document ld expression evaluation. */
e9ee469a 377#define BOP(x, y) \
7542af2a
AM
378 case x: \
379 expld.result.value = lhs.value y expld.result.value; \
9bc8bb33
AM
380 if (expld.result.section == lhs.section) \
381 expld.result.section = NULL; \
7542af2a
AM
382 break;
383
9bc8bb33
AM
384 /* Comparison operators, logical AND, and logical OR always
385 return a plain number. */
7542af2a
AM
386#define BOPN(x, y) \
387 case x: \
388 expld.result.value = lhs.value y expld.result.value; \
389 expld.result.section = NULL; \
390 break;
e9ee469a 391
4ac0c898
AM
392 BOP ('+', +);
393 BOP ('*', *);
394 BOP ('-', -);
395 BOP (LSHIFT, <<);
396 BOP (RSHIFT, >>);
4ac0c898
AM
397 BOP ('&', &);
398 BOP ('^', ^);
399 BOP ('|', |);
7542af2a
AM
400 BOPN (EQ, ==);
401 BOPN (NE, !=);
402 BOPN ('<', <);
403 BOPN ('>', >);
404 BOPN (LE, <=);
405 BOPN (GE, >=);
406 BOPN (ANDAND, &&);
407 BOPN (OROR, ||);
4ac0c898 408
9bc8bb33
AM
409 case '%':
410 if (expld.result.value != 0)
411 expld.result.value = ((bfd_signed_vma) lhs.value
412 % (bfd_signed_vma) expld.result.value);
413 else if (expld.phase != lang_mark_phase_enum)
dab69f68 414 einfo (_("%F%S %% by zero\n"), tree->binary.rhs);
9bc8bb33
AM
415 if (expld.result.section == lhs.section)
416 expld.result.section = NULL;
417 break;
418
419 case '/':
420 if (expld.result.value != 0)
421 expld.result.value = ((bfd_signed_vma) lhs.value
422 / (bfd_signed_vma) expld.result.value);
423 else if (expld.phase != lang_mark_phase_enum)
dab69f68 424 einfo (_("%F%S / by zero\n"), tree->binary.rhs);
9bc8bb33
AM
425 if (expld.result.section == lhs.section)
426 expld.result.section = NULL;
427 break;
428
4ac0c898
AM
429 case MAX_K:
430 if (lhs.value > expld.result.value)
431 expld.result.value = lhs.value;
432 break;
252b5132 433
4ac0c898
AM
434 case MIN_K:
435 if (lhs.value < expld.result.value)
436 expld.result.value = lhs.value;
437 break;
252b5132 438
4ac0c898
AM
439 case ALIGN_K:
440 expld.result.value = align_n (lhs.value, expld.result.value);
441 break;
c468c8bc 442
4ac0c898
AM
443 case DATA_SEGMENT_ALIGN:
444 expld.dataseg.relro = exp_dataseg_relro_start;
ea7c2e6c
AM
445 if (expld.phase == lang_first_phase_enum
446 || expld.section != bfd_abs_section_ptr)
447 expld.result.valid_p = FALSE;
448 else
4ac0c898
AM
449 {
450 bfd_vma maxpage = lhs.value;
451 bfd_vma commonpage = expld.result.value;
2d20f7bf 452
4ac0c898
AM
453 expld.result.value = align_n (expld.dot, maxpage);
454 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
455 expld.result.value = expld.dataseg.base;
ea7c2e6c
AM
456 else if (expld.dataseg.phase == exp_dataseg_adjust)
457 {
458 if (commonpage < maxpage)
459 expld.result.value += ((expld.dot + commonpage - 1)
460 & (maxpage - commonpage));
461 }
462 else
4ac0c898
AM
463 {
464 expld.result.value += expld.dot & (maxpage - 1);
ea7c2e6c
AM
465 if (expld.dataseg.phase == exp_dataseg_done)
466 {
467 /* OK. */
468 }
469 else if (expld.dataseg.phase == exp_dataseg_none)
2d20f7bf 470 {
4ac0c898
AM
471 expld.dataseg.phase = exp_dataseg_align_seen;
472 expld.dataseg.min_base = expld.dot;
473 expld.dataseg.base = expld.result.value;
474 expld.dataseg.pagesize = commonpage;
475 expld.dataseg.maxpagesize = maxpage;
476 expld.dataseg.relro_end = 0;
2d20f7bf 477 }
ea7c2e6c
AM
478 else
479 expld.result.valid_p = FALSE;
2d20f7bf 480 }
4ac0c898 481 }
4ac0c898 482 break;
e9ee469a 483
4ac0c898
AM
484 case DATA_SEGMENT_RELRO_END:
485 expld.dataseg.relro = exp_dataseg_relro_end;
ea7c2e6c
AM
486 if (expld.phase == lang_first_phase_enum
487 || expld.section != bfd_abs_section_ptr)
488 expld.result.valid_p = FALSE;
489 else if (expld.dataseg.phase == exp_dataseg_align_seen
490 || expld.dataseg.phase == exp_dataseg_adjust
491 || expld.dataseg.phase == exp_dataseg_relro_adjust
492 || expld.dataseg.phase == exp_dataseg_done)
4ac0c898
AM
493 {
494 if (expld.dataseg.phase == exp_dataseg_align_seen
495 || expld.dataseg.phase == exp_dataseg_relro_adjust)
496 expld.dataseg.relro_end = lhs.value + expld.result.value;
e9ee469a 497
4ac0c898
AM
498 if (expld.dataseg.phase == exp_dataseg_relro_adjust
499 && (expld.dataseg.relro_end
500 & (expld.dataseg.pagesize - 1)))
501 {
502 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
503 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
504 expld.result.value = (expld.dataseg.relro_end
505 - expld.result.value);
a4f5ad88
JJ
506 }
507 else
4ac0c898 508 expld.result.value = lhs.value;
a4f5ad88 509
4ac0c898
AM
510 if (expld.dataseg.phase == exp_dataseg_align_seen)
511 expld.dataseg.phase = exp_dataseg_relro_seen;
252b5132 512 }
4ac0c898
AM
513 else
514 expld.result.valid_p = FALSE;
515 break;
516
517 default:
518 FAIL ();
252b5132 519 }
252b5132 520 }
252b5132
RH
521}
522
e9ee469a
AM
523static void
524fold_trinary (etree_type *tree)
0ae1cf52 525{
e9ee469a
AM
526 exp_fold_tree_1 (tree->trinary.cond);
527 if (expld.result.valid_p)
528 exp_fold_tree_1 (expld.result.value
529 ? tree->trinary.lhs
530 : tree->trinary.rhs);
0ae1cf52
AM
531}
532
e9ee469a
AM
533static void
534fold_name (etree_type *tree)
252b5132 535{
e9ee469a 536 memset (&expld.result, 0, sizeof (expld.result));
c468c8bc 537
4de2d33d 538 switch (tree->type.node_code)
8c95a62e
KH
539 {
540 case SIZEOF_HEADERS:
e9ee469a
AM
541 if (expld.phase != lang_first_phase_enum)
542 {
543 bfd_vma hdr_size = 0;
544 /* Don't find the real header size if only marking sections;
545 The bfd function may cache incorrect data. */
546 if (expld.phase != lang_mark_phase_enum)
f13a99db 547 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
7542af2a 548 new_number (hdr_size);
e9ee469a 549 }
8c95a62e 550 break;
67469e1f 551
8c95a62e 552 case DEFINED:
e9ee469a 553 if (expld.phase == lang_first_phase_enum)
1b493742 554 lang_track_definedness (tree->name.name);
8c95a62e
KH
555 else
556 {
557 struct bfd_link_hash_entry *h;
420e579c
HPN
558 int def_iteration
559 = lang_symbol_definition_iteration (tree->name.name);
8c95a62e 560
f13a99db
AM
561 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
562 &link_info,
8c95a62e 563 tree->name.name,
b34976b6 564 FALSE, FALSE, TRUE);
7542af2a
AM
565 new_number (h != NULL
566 && (h->type == bfd_link_hash_defined
567 || h->type == bfd_link_hash_defweak
568 || h->type == bfd_link_hash_common)
569 && (def_iteration == lang_statement_iteration
570 || def_iteration == -1));
8c95a62e
KH
571 }
572 break;
67469e1f 573
8c95a62e 574 case NAME:
4194268f
AM
575 if (expld.assign_name != NULL
576 && strcmp (expld.assign_name, tree->name.name) == 0)
577 expld.assign_name = NULL;
e9ee469a
AM
578 if (expld.phase == lang_first_phase_enum)
579 ;
580 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
581 new_rel_from_abs (expld.dot);
582 else
8c95a62e
KH
583 {
584 struct bfd_link_hash_entry *h;
585
f13a99db
AM
586 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
587 &link_info,
8c95a62e 588 tree->name.name,
1b493742
NS
589 TRUE, FALSE, TRUE);
590 if (!h)
591 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
592 else if (h->type == bfd_link_hash_defined
593 || h->type == bfd_link_hash_defweak)
8c95a62e 594 {
7542af2a 595 asection *output_section;
8c95a62e 596
7542af2a
AM
597 output_section = h->u.def.section->output_section;
598 if (output_section == NULL)
599 {
2aa9aad9
NC
600 if (expld.phase == lang_mark_phase_enum)
601 new_rel (h->u.def.value, h->u.def.section);
602 else
7542af2a
AM
603 einfo (_("%X%S: unresolvable symbol `%s'"
604 " referenced in expression\n"),
dab69f68 605 tree, tree->name.name);
8c95a62e 606 }
5c3049d2
AM
607 else if (output_section == bfd_abs_section_ptr
608 && (expld.section != bfd_abs_section_ptr
01554a74 609 || config.sane_expr))
abf4be64 610 new_number (h->u.def.value + h->u.def.section->output_offset);
7542af2a
AM
611 else
612 new_rel (h->u.def.value + h->u.def.section->output_offset,
613 output_section);
8c95a62e 614 }
e9ee469a 615 else if (expld.phase == lang_final_phase_enum
ec8460b5
AM
616 || (expld.phase != lang_mark_phase_enum
617 && expld.assigning_to_dot))
dab69f68
AM
618 einfo (_("%F%S: undefined symbol `%s'"
619 " referenced in expression\n"),
620 tree, tree->name.name);
1b493742
NS
621 else if (h->type == bfd_link_hash_new)
622 {
623 h->type = bfd_link_hash_undefined;
624 h->u.undef.abfd = NULL;
3eda52aa 625 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
a010d60f 626 bfd_link_add_undef (link_info.hash, h);
1b493742 627 }
8c95a62e
KH
628 }
629 break;
630
631 case ADDR:
e9ee469a 632 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
633 {
634 lang_output_section_statement_type *os;
635
636 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
637 if (os == NULL)
638 {
639 if (expld.phase == lang_final_phase_enum)
dab69f68
AM
640 einfo (_("%F%S: undefined section `%s'"
641 " referenced in expression\n"),
642 tree, tree->name.name);
cc3e2771
NS
643 }
644 else if (os->processed_vma)
5942515f 645 new_rel (0, os->bfd_section);
8c95a62e 646 }
8c95a62e
KH
647 break;
648
649 case LOADADDR:
e9ee469a 650 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
651 {
652 lang_output_section_statement_type *os;
653
654 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
655 if (os == NULL)
656 {
657 if (expld.phase == lang_final_phase_enum)
dab69f68
AM
658 einfo (_("%F%S: undefined section `%s'"
659 " referenced in expression\n"),
660 tree, tree->name.name);
cc3e2771
NS
661 }
662 else if (os->processed_lma)
1b493742 663 {
e9ee469a 664 if (os->load_base == NULL)
3e23777d 665 new_abs (os->bfd_section->lma);
e9ee469a 666 else
67469e1f
AM
667 {
668 exp_fold_tree_1 (os->load_base);
819da74e
AM
669 if (expld.result.valid_p)
670 make_abs ();
67469e1f 671 }
1b493742 672 }
8c95a62e 673 }
8c95a62e
KH
674 break;
675
676 case SIZEOF:
362c1d1a 677 case ALIGNOF:
e9ee469a 678 if (expld.phase != lang_first_phase_enum)
8c95a62e 679 {
8c95a62e
KH
680 lang_output_section_statement_type *os;
681
682 os = lang_output_section_find (tree->name.name);
5397b1fe 683 if (os == NULL)
cc3e2771
NS
684 {
685 if (expld.phase == lang_final_phase_enum)
dab69f68
AM
686 einfo (_("%F%S: undefined section `%s'"
687 " referenced in expression\n"),
688 tree, tree->name.name);
7542af2a 689 new_number (0);
cc3e2771 690 }
17d6eea5 691 else if (os->bfd_section != NULL)
362c1d1a
NS
692 {
693 bfd_vma val;
694
695 if (tree->type.node_code == SIZEOF)
f13a99db
AM
696 val = (os->bfd_section->size
697 / bfd_octets_per_byte (link_info.output_bfd));
362c1d1a
NS
698 else
699 val = (bfd_vma)1 << os->bfd_section->alignment_power;
700
7542af2a 701 new_number (val);
362c1d1a 702 }
17d6eea5
L
703 else
704 new_number (0);
8c95a62e 705 }
8c95a62e
KH
706 break;
707
3ec57632
NC
708 case LENGTH:
709 {
710 lang_memory_region_type *mem;
711
712 mem = lang_memory_region_lookup (tree->name.name, FALSE);
713 if (mem != NULL)
7542af2a 714 new_number (mem->length);
3ec57632 715 else
e9ee469a 716 einfo (_("%F%S: undefined MEMORY region `%s'"
dab69f68
AM
717 " referenced in expression\n"),
718 tree, tree->name.name);
3ec57632
NC
719 }
720 break;
721
722 case ORIGIN:
7542af2a
AM
723 if (expld.phase != lang_first_phase_enum)
724 {
725 lang_memory_region_type *mem;
3ec57632 726
7542af2a
AM
727 mem = lang_memory_region_lookup (tree->name.name, FALSE);
728 if (mem != NULL)
729 new_rel_from_abs (mem->origin);
730 else
731 einfo (_("%F%S: undefined MEMORY region `%s'"
dab69f68
AM
732 " referenced in expression\n"),
733 tree, tree->name.name);
7542af2a 734 }
3ec57632
NC
735 break;
736
24718e3b
L
737 case CONSTANT:
738 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
7542af2a 739 new_number (config.maxpagesize);
24718e3b 740 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
7542af2a 741 new_number (config.commonpagesize);
24718e3b
L
742 else
743 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
dab69f68 744 tree, tree->name.name);
24718e3b
L
745 break;
746
8c95a62e
KH
747 default:
748 FAIL ();
749 break;
750 }
252b5132 751}
8c95a62e 752
e9ee469a
AM
753static void
754exp_fold_tree_1 (etree_type *tree)
252b5132 755{
252b5132
RH
756 if (tree == NULL)
757 {
e9ee469a
AM
758 memset (&expld.result, 0, sizeof (expld.result));
759 return;
252b5132
RH
760 }
761
4de2d33d 762 switch (tree->type.node_class)
252b5132
RH
763 {
764 case etree_value:
5c3049d2 765 if (expld.section == bfd_abs_section_ptr
01554a74 766 && !config.sane_expr)
5c3049d2
AM
767 new_abs (tree->value.value);
768 else
769 new_number (tree->value.value);
5942515f 770 expld.result.str = tree->value.str;
252b5132
RH
771 break;
772
773 case etree_rel:
e9ee469a
AM
774 if (expld.phase != lang_first_phase_enum)
775 {
776 asection *output_section = tree->rel.section->output_section;
777 new_rel (tree->rel.value + tree->rel.section->output_offset,
5942515f 778 output_section);
e9ee469a 779 }
252b5132 780 else
e9ee469a 781 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
782 break;
783
784 case etree_assert:
e9ee469a 785 exp_fold_tree_1 (tree->assert_s.child);
5397b1fe
AM
786 if (expld.phase == lang_final_phase_enum && !expld.result.value)
787 einfo ("%X%P: %s\n", tree->assert_s.message);
252b5132
RH
788 break;
789
790 case etree_unary:
e9ee469a 791 fold_unary (tree);
252b5132
RH
792 break;
793
794 case etree_binary:
e9ee469a 795 fold_binary (tree);
252b5132 796 break;
0ae1cf52
AM
797
798 case etree_trinary:
e9ee469a 799 fold_trinary (tree);
0ae1cf52 800 break;
252b5132
RH
801
802 case etree_assign:
803 case etree_provide:
b46a87b1 804 case etree_provided:
252b5132
RH
805 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
806 {
b46a87b1 807 if (tree->type.node_class != etree_assign)
dab69f68
AM
808 einfo (_("%F%S can not PROVIDE assignment to"
809 " location counter\n"), tree);
ec8460b5 810 if (expld.phase != lang_first_phase_enum)
252b5132 811 {
fbbb9ac5 812 /* Notify the folder that this is an assignment to dot. */
e9ee469a
AM
813 expld.assigning_to_dot = TRUE;
814 exp_fold_tree_1 (tree->assign.src);
815 expld.assigning_to_dot = FALSE;
816
817 if (!expld.result.valid_p)
818 {
819 if (expld.phase != lang_mark_phase_enum)
dab69f68
AM
820 einfo (_("%F%S invalid assignment to"
821 " location counter\n"), tree);
e9ee469a
AM
822 }
823 else if (expld.dotp == NULL)
824 einfo (_("%F%S assignment to location counter"
ec8460b5
AM
825 " invalid outside of SECTIONS\n"), tree);
826
827 /* After allocation, assignment to dot should not be
828 done inside an output section since allocation adds a
829 padding statement that effectively duplicates the
830 assignment. */
831 else if (expld.phase <= lang_allocating_phase_enum
832 || expld.section == bfd_abs_section_ptr)
252b5132 833 {
e9ee469a
AM
834 bfd_vma nextdot;
835
7542af2a
AM
836 nextdot = expld.result.value;
837 if (expld.result.section != NULL)
838 nextdot += expld.result.section->vma;
839 else
840 nextdot += expld.section->vma;
e9ee469a
AM
841 if (nextdot < expld.dot
842 && expld.section != bfd_abs_section_ptr)
843 einfo (_("%F%S cannot move location counter backwards"
dab69f68
AM
844 " (from %V to %V)\n"),
845 tree, expld.dot, nextdot);
252b5132
RH
846 else
847 {
e9ee469a
AM
848 expld.dot = nextdot;
849 *expld.dotp = nextdot;
252b5132
RH
850 }
851 }
852 }
8b3d8fa8 853 else
e9ee469a 854 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
855 }
856 else
857 {
e9ee469a 858 struct bfd_link_hash_entry *h = NULL;
252b5132 859
e9ee469a
AM
860 if (tree->type.node_class == etree_provide)
861 {
252b5132 862 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
e9ee469a
AM
863 FALSE, FALSE, TRUE);
864 if (h == NULL
865 || (h->type != bfd_link_hash_new
866 && h->type != bfd_link_hash_undefined
867 && h->type != bfd_link_hash_common))
868 {
869 /* Do nothing. The symbol was never referenced, or was
870 defined by some object. */
871 break;
872 }
873 }
874
4194268f 875 expld.assign_name = tree->assign.dst;
e9ee469a 876 exp_fold_tree_1 (tree->assign.src);
4194268f
AM
877 /* expld.assign_name remaining equal to tree->assign.dst
878 below indicates the evaluation of tree->assign.src did
879 not use the value of tree->assign.dst. We don't allow
880 self assignment until the final phase for two reasons:
881 1) Expressions are evaluated multiple times. With
882 relaxation, the number of times may vary.
883 2) Section relative symbol values cannot be correctly
884 converted to absolute values, as is required by many
885 expressions, until final section sizing is complete. */
886 if ((expld.result.valid_p
887 && (expld.phase == lang_final_phase_enum
888 || expld.assign_name != NULL))
2aa9aad9 889 || (expld.phase <= lang_mark_phase_enum
01554a74 890 && tree->type.node_class == etree_assign
eb8476a6 891 && tree->assign.defsym))
e9ee469a 892 {
1579bae1 893 if (h == NULL)
67010b46 894 {
e9ee469a
AM
895 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
896 TRUE, FALSE, TRUE);
897 if (h == NULL)
67010b46
NC
898 einfo (_("%P%F:%s: hash creation failed\n"),
899 tree->assign.dst);
900 }
e9ee469a
AM
901
902 /* FIXME: Should we worry if the symbol is already
903 defined? */
904 lang_update_definedness (tree->assign.dst, h);
905 h->type = bfd_link_hash_defined;
906 h->u.def.value = expld.result.value;
7542af2a
AM
907 if (expld.result.section == NULL)
908 expld.result.section = expld.section;
e9ee469a
AM
909 h->u.def.section = expld.result.section;
910 if (tree->type.node_class == etree_provide)
911 tree->type.node_class = etree_provided;
1338dd10
PB
912
913 /* Copy the symbol type if this is a simple assignment of
f37a7048
NS
914 one symbol to another. This could be more general
915 (e.g. a ?: operator with NAMEs in each branch). */
1338dd10
PB
916 if (tree->assign.src->type.node_class == etree_name)
917 {
918 struct bfd_link_hash_entry *hsrc;
919
920 hsrc = bfd_link_hash_lookup (link_info.hash,
921 tree->assign.src->name.name,
922 FALSE, FALSE, TRUE);
923 if (hsrc)
924 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
925 hsrc);
926 }
252b5132 927 }
e092cb30
AM
928 else if (expld.phase == lang_final_phase_enum)
929 {
930 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
931 FALSE, FALSE, TRUE);
932 if (h != NULL
933 && h->type == bfd_link_hash_new)
934 h->type = bfd_link_hash_undefined;
935 }
4194268f 936 expld.assign_name = NULL;
252b5132
RH
937 }
938 break;
939
940 case etree_name:
e9ee469a 941 fold_name (tree);
252b5132
RH
942 break;
943
944 default:
945 FAIL ();
e9ee469a 946 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
947 break;
948 }
252b5132
RH
949}
950
e9ee469a
AM
951void
952exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
75ff4589 953{
e9ee469a
AM
954 expld.dot = *dotp;
955 expld.dotp = dotp;
956 expld.section = current_section;
957 exp_fold_tree_1 (tree);
75ff4589
L
958}
959
e759c116 960void
e9ee469a 961exp_fold_tree_no_dot (etree_type *tree)
252b5132 962{
e9ee469a
AM
963 expld.dot = 0;
964 expld.dotp = NULL;
965 expld.section = bfd_abs_section_ptr;
966 exp_fold_tree_1 (tree);
252b5132
RH
967}
968
969etree_type *
1579bae1 970exp_binop (int code, etree_type *lhs, etree_type *rhs)
252b5132 971{
d3ce72d0 972 etree_type value, *new_e;
252b5132
RH
973
974 value.type.node_code = code;
dab69f68 975 value.type.filename = lhs->type.filename;
f856040a 976 value.type.lineno = lhs->type.lineno;
252b5132
RH
977 value.binary.lhs = lhs;
978 value.binary.rhs = rhs;
979 value.type.node_class = etree_binary;
e9ee469a
AM
980 exp_fold_tree_no_dot (&value);
981 if (expld.result.valid_p)
982 return exp_intop (expld.result.value);
983
d3ce72d0
NC
984 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
985 memcpy (new_e, &value, sizeof (new_e->binary));
986 return new_e;
252b5132
RH
987}
988
989etree_type *
1579bae1 990exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
252b5132 991{
d3ce72d0 992 etree_type value, *new_e;
e9ee469a 993
252b5132 994 value.type.node_code = code;
dab69f68
AM
995 value.type.filename = cond->type.filename;
996 value.type.lineno = cond->type.lineno;
252b5132
RH
997 value.trinary.lhs = lhs;
998 value.trinary.cond = cond;
999 value.trinary.rhs = rhs;
1000 value.type.node_class = etree_trinary;
e9ee469a
AM
1001 exp_fold_tree_no_dot (&value);
1002 if (expld.result.valid_p)
1003 return exp_intop (expld.result.value);
c7d701b0 1004
d3ce72d0
NC
1005 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
1006 memcpy (new_e, &value, sizeof (new_e->trinary));
1007 return new_e;
252b5132
RH
1008}
1009
252b5132 1010etree_type *
1579bae1 1011exp_unop (int code, etree_type *child)
252b5132 1012{
d3ce72d0 1013 etree_type value, *new_e;
252b5132 1014
252b5132 1015 value.unary.type.node_code = code;
dab69f68 1016 value.unary.type.filename = child->type.filename;
f856040a 1017 value.unary.type.lineno = child->type.lineno;
252b5132
RH
1018 value.unary.child = child;
1019 value.unary.type.node_class = etree_unary;
e9ee469a
AM
1020 exp_fold_tree_no_dot (&value);
1021 if (expld.result.valid_p)
1022 return exp_intop (expld.result.value);
c7d701b0 1023
d3ce72d0
NC
1024 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
1025 memcpy (new_e, &value, sizeof (new_e->unary));
1026 return new_e;
252b5132
RH
1027}
1028
252b5132 1029etree_type *
1579bae1 1030exp_nameop (int code, const char *name)
252b5132 1031{
d3ce72d0 1032 etree_type value, *new_e;
e9ee469a 1033
252b5132 1034 value.name.type.node_code = code;
dab69f68 1035 value.name.type.filename = ldlex_filename ();
f856040a 1036 value.name.type.lineno = lineno;
252b5132
RH
1037 value.name.name = name;
1038 value.name.type.node_class = etree_name;
1039
e9ee469a
AM
1040 exp_fold_tree_no_dot (&value);
1041 if (expld.result.valid_p)
1042 return exp_intop (expld.result.value);
c7d701b0 1043
d3ce72d0
NC
1044 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1045 memcpy (new_e, &value, sizeof (new_e->name));
1046 return new_e;
252b5132
RH
1047
1048}
1049
2e57b2af
AM
1050static etree_type *
1051exp_assop (const char *dst,
1052 etree_type *src,
1053 enum node_tree_enum class,
eb8476a6 1054 bfd_boolean defsym,
2e57b2af 1055 bfd_boolean hidden)
252b5132
RH
1056{
1057 etree_type *n;
1058
1e9cc1c2 1059 n = (etree_type *) stat_alloc (sizeof (n->assign));
252b5132 1060 n->assign.type.node_code = '=';
dab69f68 1061 n->assign.type.filename = src->type.filename;
f856040a 1062 n->assign.type.lineno = src->type.lineno;
2e57b2af 1063 n->assign.type.node_class = class;
252b5132
RH
1064 n->assign.src = src;
1065 n->assign.dst = dst;
eb8476a6 1066 n->assign.defsym = defsym;
7af8e998 1067 n->assign.hidden = hidden;
252b5132
RH
1068 return n;
1069}
1070
eb8476a6
MR
1071/* Handle linker script assignments and HIDDEN. */
1072
2e57b2af 1073etree_type *
eb8476a6 1074exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
2e57b2af 1075{
eb8476a6 1076 return exp_assop (dst, src, etree_assign, FALSE, hidden);
2e57b2af
AM
1077}
1078
eb8476a6
MR
1079/* Handle --defsym command-line option. */
1080
2e57b2af
AM
1081etree_type *
1082exp_defsym (const char *dst, etree_type *src)
1083{
eb8476a6 1084 return exp_assop (dst, src, etree_assign, TRUE, FALSE);
2e57b2af
AM
1085}
1086
1087/* Handle PROVIDE. */
1088
1089etree_type *
1090exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1091{
eb8476a6 1092 return exp_assop (dst, src, etree_provide, FALSE, hidden);
2e57b2af
AM
1093}
1094
252b5132
RH
1095/* Handle ASSERT. */
1096
1097etree_type *
1579bae1 1098exp_assert (etree_type *exp, const char *message)
252b5132
RH
1099{
1100 etree_type *n;
1101
1e9cc1c2 1102 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
252b5132 1103 n->assert_s.type.node_code = '!';
dab69f68 1104 n->assert_s.type.filename = exp->type.filename;
f856040a 1105 n->assert_s.type.lineno = exp->type.lineno;
252b5132
RH
1106 n->assert_s.type.node_class = etree_assert;
1107 n->assert_s.child = exp;
1108 n->assert_s.message = message;
1109 return n;
1110}
1111
4de2d33d 1112void
1579bae1 1113exp_print_tree (etree_type *tree)
252b5132 1114{
ae78bbeb
AM
1115 bfd_boolean function_like;
1116
c7d701b0
NC
1117 if (config.map_file == NULL)
1118 config.map_file = stderr;
b7a26f91 1119
c7d701b0
NC
1120 if (tree == NULL)
1121 {
1122 minfo ("NULL TREE\n");
1123 return;
1124 }
b7a26f91 1125
8c95a62e
KH
1126 switch (tree->type.node_class)
1127 {
1128 case etree_value:
1129 minfo ("0x%v", tree->value.value);
1130 return;
1131 case etree_rel:
1132 if (tree->rel.section->owner != NULL)
1133 minfo ("%B:", tree->rel.section->owner);
1134 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1135 return;
1136 case etree_assign:
ae78bbeb 1137 fputs (tree->assign.dst, config.map_file);
b34976b6 1138 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1139 exp_print_tree (tree->assign.src);
1140 break;
1141 case etree_provide:
b46a87b1 1142 case etree_provided:
8c95a62e
KH
1143 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1144 exp_print_tree (tree->assign.src);
ae78bbeb 1145 fputc (')', config.map_file);
8c95a62e
KH
1146 break;
1147 case etree_binary:
ae78bbeb
AM
1148 function_like = FALSE;
1149 switch (tree->type.node_code)
1150 {
1151 case MAX_K:
1152 case MIN_K:
1153 case ALIGN_K:
1154 case DATA_SEGMENT_ALIGN:
1155 case DATA_SEGMENT_RELRO_END:
1156 function_like = TRUE;
67baf8c4
TG
1157 break;
1158 case SEGMENT_START:
1159 /* Special handling because arguments are in reverse order and
1160 the segment name is quoted. */
1161 exp_print_token (tree->type.node_code, FALSE);
1162 fputs (" (\"", config.map_file);
1163 exp_print_tree (tree->binary.rhs);
1164 fputs ("\", ", config.map_file);
1165 exp_print_tree (tree->binary.lhs);
1166 fputc (')', config.map_file);
1167 return;
ae78bbeb
AM
1168 }
1169 if (function_like)
1170 {
1171 exp_print_token (tree->type.node_code, FALSE);
1172 fputc (' ', config.map_file);
1173 }
1174 fputc ('(', config.map_file);
8c95a62e 1175 exp_print_tree (tree->binary.lhs);
ae78bbeb
AM
1176 if (function_like)
1177 fprintf (config.map_file, ", ");
1178 else
1179 exp_print_token (tree->type.node_code, TRUE);
8c95a62e 1180 exp_print_tree (tree->binary.rhs);
ae78bbeb 1181 fputc (')', config.map_file);
8c95a62e
KH
1182 break;
1183 case etree_trinary:
1184 exp_print_tree (tree->trinary.cond);
ae78bbeb 1185 fputc ('?', config.map_file);
8c95a62e 1186 exp_print_tree (tree->trinary.lhs);
ae78bbeb 1187 fputc (':', config.map_file);
8c95a62e
KH
1188 exp_print_tree (tree->trinary.rhs);
1189 break;
1190 case etree_unary:
b34976b6 1191 exp_print_token (tree->unary.type.node_code, FALSE);
8c95a62e
KH
1192 if (tree->unary.child)
1193 {
7b17f854 1194 fprintf (config.map_file, " (");
8c95a62e 1195 exp_print_tree (tree->unary.child);
ae78bbeb 1196 fputc (')', config.map_file);
8c95a62e
KH
1197 }
1198 break;
1199
1200 case etree_assert:
1201 fprintf (config.map_file, "ASSERT (");
1202 exp_print_tree (tree->assert_s.child);
1203 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1204 break;
1205
8c95a62e
KH
1206 case etree_name:
1207 if (tree->type.node_code == NAME)
ae78bbeb 1208 fputs (tree->name.name, config.map_file);
8c95a62e
KH
1209 else
1210 {
b34976b6 1211 exp_print_token (tree->type.node_code, FALSE);
8c95a62e 1212 if (tree->name.name)
7b17f854 1213 fprintf (config.map_file, " (%s)", tree->name.name);
8c95a62e
KH
1214 }
1215 break;
1216 default:
1217 FAIL ();
1218 break;
252b5132 1219 }
252b5132
RH
1220}
1221
1222bfd_vma
e9ee469a 1223exp_get_vma (etree_type *tree, bfd_vma def, char *name)
252b5132 1224{
252b5132
RH
1225 if (tree != NULL)
1226 {
e9ee469a
AM
1227 exp_fold_tree_no_dot (tree);
1228 if (expld.result.valid_p)
1229 return expld.result.value;
1230 else if (name != NULL && expld.phase != lang_mark_phase_enum)
dab69f68
AM
1231 einfo (_("%F%S: nonconstant expression for %s\n"),
1232 tree, name);
252b5132 1233 }
e9ee469a 1234 return def;
252b5132
RH
1235}
1236
4de2d33d 1237int
e9ee469a 1238exp_get_value_int (etree_type *tree, int def, char *name)
252b5132 1239{
e9ee469a 1240 return exp_get_vma (tree, def, name);
252b5132
RH
1241}
1242
2c382fb6 1243fill_type *
e9ee469a 1244exp_get_fill (etree_type *tree, fill_type *def, char *name)
2c382fb6
AM
1245{
1246 fill_type *fill;
2c382fb6
AM
1247 size_t len;
1248 unsigned int val;
1249
1250 if (tree == NULL)
1251 return def;
1252
e9ee469a
AM
1253 exp_fold_tree_no_dot (tree);
1254 if (!expld.result.valid_p)
1255 {
1256 if (name != NULL && expld.phase != lang_mark_phase_enum)
dab69f68
AM
1257 einfo (_("%F%S: nonconstant expression for %s\n"),
1258 tree, name);
e9ee469a
AM
1259 return def;
1260 }
2c382fb6 1261
e9ee469a 1262 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
2c382fb6
AM
1263 {
1264 unsigned char *dst;
1265 unsigned char *s;
1e9cc1c2 1266 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
2c382fb6
AM
1267 fill->size = (len + 1) / 2;
1268 dst = fill->data;
e9ee469a 1269 s = (unsigned char *) expld.result.str;
2c382fb6
AM
1270 val = 0;
1271 do
1272 {
1273 unsigned int digit;
1274
1275 digit = *s++ - '0';
1276 if (digit > 9)
1277 digit = (digit - 'A' + '0' + 10) & 0xf;
1278 val <<= 4;
1279 val += digit;
1280 --len;
1281 if ((len & 1) == 0)
1282 {
1283 *dst++ = val;
1284 val = 0;
1285 }
1286 }
1287 while (len != 0);
1288 }
1289 else
1290 {
1e9cc1c2 1291 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
e9ee469a 1292 val = expld.result.value;
2c382fb6
AM
1293 fill->data[0] = (val >> 24) & 0xff;
1294 fill->data[1] = (val >> 16) & 0xff;
1295 fill->data[2] = (val >> 8) & 0xff;
1296 fill->data[3] = (val >> 0) & 0xff;
1297 fill->size = 4;
1298 }
1299 return fill;
1300}
1301
252b5132 1302bfd_vma
e9ee469a 1303exp_get_abs_int (etree_type *tree, int def, char *name)
252b5132 1304{
e9ee469a
AM
1305 if (tree != NULL)
1306 {
1307 exp_fold_tree_no_dot (tree);
c7d701b0 1308
e9ee469a
AM
1309 if (expld.result.valid_p)
1310 {
7542af2a
AM
1311 if (expld.result.section != NULL)
1312 expld.result.value += expld.result.section->vma;
e9ee469a
AM
1313 return expld.result.value;
1314 }
1315 else if (name != NULL && expld.phase != lang_mark_phase_enum)
f856040a 1316 {
dab69f68
AM
1317 einfo (_("%F%S: nonconstant expression for %s\n"),
1318 tree, name);
f856040a 1319 }
e9ee469a
AM
1320 }
1321 return def;
252b5132 1322}
c553bb91 1323
e5caa5e0
AM
1324static bfd_vma
1325align_n (bfd_vma value, bfd_vma align)
c553bb91
AM
1326{
1327 if (align <= 1)
1328 return value;
1329
1330 value = (value + align - 1) / align;
1331 return value * align;
1332}
This page took 0.713828 seconds and 4 git commands to generate.