]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldexp.c
PR ld/12726
[thirdparty/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,
ea7c2e6c 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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;
c7d701b0
NC
62 char * name;
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;
159 new_e->type.lineno = lineno;
160 new_e->value.value = value;
161 new_e->value.str = NULL;
162 new_e->type.node_class = etree_value;
163 return new_e;
2c382fb6 164}
252b5132 165
2c382fb6 166etree_type *
1579bae1 167exp_bigintop (bfd_vma value, char *str)
2c382fb6 168{
d3ce72d0
NC
169 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
170 new_e->type.node_code = INT;
171 new_e->type.lineno = lineno;
172 new_e->value.value = value;
173 new_e->value.str = str;
174 new_e->type.node_class = etree_value;
175 return new_e;
252b5132
RH
176}
177
1049f94e 178/* Build an expression representing an unnamed relocatable value. */
252b5132
RH
179
180etree_type *
1579bae1 181exp_relop (asection *section, bfd_vma value)
252b5132 182{
d3ce72d0
NC
183 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
184 new_e->type.node_code = REL;
185 new_e->type.lineno = lineno;
186 new_e->type.node_class = etree_rel;
187 new_e->rel.section = section;
188 new_e->rel.value = value;
189 return new_e;
252b5132
RH
190}
191
7542af2a
AM
192static void
193new_number (bfd_vma value)
194{
195 expld.result.valid_p = TRUE;
196 expld.result.value = value;
197 expld.result.str = NULL;
198 expld.result.section = NULL;
199}
200
e9ee469a 201static void
5942515f 202new_rel (bfd_vma value, asection *section)
252b5132 203{
e9ee469a
AM
204 expld.result.valid_p = TRUE;
205 expld.result.value = value;
5942515f 206 expld.result.str = NULL;
e9ee469a 207 expld.result.section = section;
252b5132
RH
208}
209
e9ee469a
AM
210static void
211new_rel_from_abs (bfd_vma value)
252b5132 212{
e9ee469a
AM
213 expld.result.valid_p = TRUE;
214 expld.result.value = value - expld.section->vma;
215 expld.result.str = NULL;
216 expld.result.section = expld.section;
252b5132
RH
217}
218
e9ee469a
AM
219static void
220fold_unary (etree_type *tree)
0ae1cf52 221{
e9ee469a
AM
222 exp_fold_tree_1 (tree->unary.child);
223 if (expld.result.valid_p)
0ae1cf52
AM
224 {
225 switch (tree->type.node_code)
226 {
227 case ALIGN_K:
e9ee469a 228 if (expld.phase != lang_first_phase_enum)
dea2f0a8 229 new_rel_from_abs (align_n (expld.dot, expld.result.value));
0ae1cf52 230 else
e9ee469a 231 expld.result.valid_p = FALSE;
0ae1cf52
AM
232 break;
233
234 case ABSOLUTE:
e9ee469a 235 make_abs ();
0ae1cf52
AM
236 break;
237
238 case '~':
e9ee469a 239 expld.result.value = ~expld.result.value;
0ae1cf52
AM
240 break;
241
242 case '!':
e9ee469a 243 expld.result.value = !expld.result.value;
0ae1cf52
AM
244 break;
245
246 case '-':
e9ee469a 247 expld.result.value = -expld.result.value;
0ae1cf52
AM
248 break;
249
250 case NEXT:
251 /* Return next place aligned to value. */
e9ee469a 252 if (expld.phase != lang_first_phase_enum)
0ae1cf52 253 {
e9ee469a
AM
254 make_abs ();
255 expld.result.value = align_n (expld.dot, expld.result.value);
0ae1cf52
AM
256 }
257 else
e9ee469a 258 expld.result.valid_p = FALSE;
0ae1cf52
AM
259 break;
260
261 case DATA_SEGMENT_END:
ea7c2e6c
AM
262 if (expld.phase == lang_first_phase_enum
263 || expld.section != bfd_abs_section_ptr)
0ae1cf52 264 {
ea7c2e6c
AM
265 expld.result.valid_p = FALSE;
266 }
267 else if (expld.dataseg.phase == exp_dataseg_align_seen
268 || expld.dataseg.phase == exp_dataseg_relro_seen)
269 {
270 expld.dataseg.phase = exp_dataseg_end_seen;
271 expld.dataseg.end = expld.result.value;
272 }
273 else if (expld.dataseg.phase == exp_dataseg_done
274 || expld.dataseg.phase == exp_dataseg_adjust
275 || expld.dataseg.phase == exp_dataseg_relro_adjust)
276 {
277 /* OK. */
0ae1cf52
AM
278 }
279 else
e9ee469a 280 expld.result.valid_p = FALSE;
0ae1cf52
AM
281 break;
282
283 default:
284 FAIL ();
285 break;
286 }
287 }
0ae1cf52
AM
288}
289
e9ee469a
AM
290static void
291fold_binary (etree_type *tree)
252b5132 292{
4ac0c898 293 etree_value_type lhs;
e9ee469a 294 exp_fold_tree_1 (tree->binary.lhs);
ba916c8a
MM
295
296 /* The SEGMENT_START operator is special because its first
8c0848b5
AM
297 operand is a string, not the name of a symbol. Note that the
298 operands have been swapped, so binary.lhs is second (default)
299 operand, binary.rhs is first operand. */
e9ee469a 300 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
ba916c8a
MM
301 {
302 const char *segment_name;
303 segment_type *seg;
7542af2a 304
ba916c8a
MM
305 /* Check to see if the user has overridden the default
306 value. */
307 segment_name = tree->binary.rhs->name.name;
308 for (seg = segments; seg; seg = seg->next)
309 if (strcmp (seg->name, segment_name) == 0)
310 {
c8ce5710
L
311 if (!seg->used
312 && config.magic_demand_paged
313 && (seg->value % config.maxpagesize) != 0)
314 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
315 segment_name);
ba916c8a 316 seg->used = TRUE;
7542af2a 317 new_rel_from_abs (seg->value);
ba916c8a
MM
318 break;
319 }
4ac0c898 320 return;
ba916c8a 321 }
252b5132 322
4ac0c898
AM
323 lhs = expld.result;
324 exp_fold_tree_1 (tree->binary.rhs);
325 expld.result.valid_p &= lhs.valid_p;
326
327 if (expld.result.valid_p)
328 {
7542af2a 329 if (lhs.section != expld.result.section)
252b5132 330 {
7542af2a
AM
331 /* If the values are from different sections, and neither is
332 just a number, make both the source arguments absolute. */
333 if (expld.result.section != NULL
334 && lhs.section != NULL)
335 {
336 make_abs ();
337 lhs.value += lhs.section->vma;
9bc8bb33 338 lhs.section = bfd_abs_section_ptr;
7542af2a
AM
339 }
340
341 /* If the rhs is just a number, keep the lhs section. */
342 else if (expld.result.section == NULL)
9bc8bb33
AM
343 {
344 expld.result.section = lhs.section;
345 /* Make this NULL so that we know one of the operands
346 was just a number, for later tests. */
347 lhs.section = NULL;
348 }
4ac0c898 349 }
9bc8bb33
AM
350 /* At this point we know that both operands have the same
351 section, or at least one of them is a plain number. */
252b5132 352
4ac0c898
AM
353 switch (tree->type.node_code)
354 {
9bc8bb33
AM
355 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
356 keep the section of one of their operands only when the
357 other operand is a plain number. Losing the section when
358 operating on two symbols, ie. a result of a plain number,
359 is required for subtraction and XOR. It's justifiable
360 for the other operations on the grounds that adding,
361 multiplying etc. two section relative values does not
362 really make sense unless they are just treated as
363 numbers.
364 The same argument could be made for many expressions
365 involving one symbol and a number. For example,
366 "1 << x" and "100 / x" probably should not be given the
367 section of x. The trouble is that if we fuss about such
368 things the rules become complex and it is onerous to
369 document ld expression evaluation. */
e9ee469a 370#define BOP(x, y) \
7542af2a
AM
371 case x: \
372 expld.result.value = lhs.value y expld.result.value; \
9bc8bb33
AM
373 if (expld.result.section == lhs.section) \
374 expld.result.section = NULL; \
7542af2a
AM
375 break;
376
9bc8bb33
AM
377 /* Comparison operators, logical AND, and logical OR always
378 return a plain number. */
7542af2a
AM
379#define BOPN(x, y) \
380 case x: \
381 expld.result.value = lhs.value y expld.result.value; \
382 expld.result.section = NULL; \
383 break;
e9ee469a 384
4ac0c898
AM
385 BOP ('+', +);
386 BOP ('*', *);
387 BOP ('-', -);
388 BOP (LSHIFT, <<);
389 BOP (RSHIFT, >>);
4ac0c898
AM
390 BOP ('&', &);
391 BOP ('^', ^);
392 BOP ('|', |);
7542af2a
AM
393 BOPN (EQ, ==);
394 BOPN (NE, !=);
395 BOPN ('<', <);
396 BOPN ('>', >);
397 BOPN (LE, <=);
398 BOPN (GE, >=);
399 BOPN (ANDAND, &&);
400 BOPN (OROR, ||);
4ac0c898 401
9bc8bb33
AM
402 case '%':
403 if (expld.result.value != 0)
404 expld.result.value = ((bfd_signed_vma) lhs.value
405 % (bfd_signed_vma) expld.result.value);
406 else if (expld.phase != lang_mark_phase_enum)
407 einfo (_("%F%S %% by zero\n"));
408 if (expld.result.section == lhs.section)
409 expld.result.section = NULL;
410 break;
411
412 case '/':
413 if (expld.result.value != 0)
414 expld.result.value = ((bfd_signed_vma) lhs.value
415 / (bfd_signed_vma) expld.result.value);
416 else if (expld.phase != lang_mark_phase_enum)
417 einfo (_("%F%S / by zero\n"));
418 if (expld.result.section == lhs.section)
419 expld.result.section = NULL;
420 break;
421
4ac0c898
AM
422 case MAX_K:
423 if (lhs.value > expld.result.value)
424 expld.result.value = lhs.value;
425 break;
252b5132 426
4ac0c898
AM
427 case MIN_K:
428 if (lhs.value < expld.result.value)
429 expld.result.value = lhs.value;
430 break;
252b5132 431
4ac0c898
AM
432 case ALIGN_K:
433 expld.result.value = align_n (lhs.value, expld.result.value);
434 break;
c468c8bc 435
4ac0c898
AM
436 case DATA_SEGMENT_ALIGN:
437 expld.dataseg.relro = exp_dataseg_relro_start;
ea7c2e6c
AM
438 if (expld.phase == lang_first_phase_enum
439 || expld.section != bfd_abs_section_ptr)
440 expld.result.valid_p = FALSE;
441 else
4ac0c898
AM
442 {
443 bfd_vma maxpage = lhs.value;
444 bfd_vma commonpage = expld.result.value;
2d20f7bf 445
4ac0c898
AM
446 expld.result.value = align_n (expld.dot, maxpage);
447 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
448 expld.result.value = expld.dataseg.base;
ea7c2e6c
AM
449 else if (expld.dataseg.phase == exp_dataseg_adjust)
450 {
451 if (commonpage < maxpage)
452 expld.result.value += ((expld.dot + commonpage - 1)
453 & (maxpage - commonpage));
454 }
455 else
4ac0c898
AM
456 {
457 expld.result.value += expld.dot & (maxpage - 1);
ea7c2e6c
AM
458 if (expld.dataseg.phase == exp_dataseg_done)
459 {
460 /* OK. */
461 }
462 else if (expld.dataseg.phase == exp_dataseg_none)
2d20f7bf 463 {
4ac0c898
AM
464 expld.dataseg.phase = exp_dataseg_align_seen;
465 expld.dataseg.min_base = expld.dot;
466 expld.dataseg.base = expld.result.value;
467 expld.dataseg.pagesize = commonpage;
468 expld.dataseg.maxpagesize = maxpage;
469 expld.dataseg.relro_end = 0;
2d20f7bf 470 }
ea7c2e6c
AM
471 else
472 expld.result.valid_p = FALSE;
2d20f7bf 473 }
4ac0c898 474 }
4ac0c898 475 break;
e9ee469a 476
4ac0c898
AM
477 case DATA_SEGMENT_RELRO_END:
478 expld.dataseg.relro = exp_dataseg_relro_end;
ea7c2e6c
AM
479 if (expld.phase == lang_first_phase_enum
480 || expld.section != bfd_abs_section_ptr)
481 expld.result.valid_p = FALSE;
482 else if (expld.dataseg.phase == exp_dataseg_align_seen
483 || expld.dataseg.phase == exp_dataseg_adjust
484 || expld.dataseg.phase == exp_dataseg_relro_adjust
485 || expld.dataseg.phase == exp_dataseg_done)
4ac0c898
AM
486 {
487 if (expld.dataseg.phase == exp_dataseg_align_seen
488 || expld.dataseg.phase == exp_dataseg_relro_adjust)
489 expld.dataseg.relro_end = lhs.value + expld.result.value;
e9ee469a 490
4ac0c898
AM
491 if (expld.dataseg.phase == exp_dataseg_relro_adjust
492 && (expld.dataseg.relro_end
493 & (expld.dataseg.pagesize - 1)))
494 {
495 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
496 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
497 expld.result.value = (expld.dataseg.relro_end
498 - expld.result.value);
a4f5ad88
JJ
499 }
500 else
4ac0c898 501 expld.result.value = lhs.value;
a4f5ad88 502
4ac0c898
AM
503 if (expld.dataseg.phase == exp_dataseg_align_seen)
504 expld.dataseg.phase = exp_dataseg_relro_seen;
252b5132 505 }
4ac0c898
AM
506 else
507 expld.result.valid_p = FALSE;
508 break;
509
510 default:
511 FAIL ();
252b5132 512 }
252b5132 513 }
252b5132
RH
514}
515
e9ee469a
AM
516static void
517fold_trinary (etree_type *tree)
0ae1cf52 518{
e9ee469a
AM
519 exp_fold_tree_1 (tree->trinary.cond);
520 if (expld.result.valid_p)
521 exp_fold_tree_1 (expld.result.value
522 ? tree->trinary.lhs
523 : tree->trinary.rhs);
0ae1cf52
AM
524}
525
e9ee469a
AM
526static void
527fold_name (etree_type *tree)
252b5132 528{
e9ee469a 529 memset (&expld.result, 0, sizeof (expld.result));
c468c8bc 530
4de2d33d 531 switch (tree->type.node_code)
8c95a62e
KH
532 {
533 case SIZEOF_HEADERS:
e9ee469a
AM
534 if (expld.phase != lang_first_phase_enum)
535 {
536 bfd_vma hdr_size = 0;
537 /* Don't find the real header size if only marking sections;
538 The bfd function may cache incorrect data. */
539 if (expld.phase != lang_mark_phase_enum)
f13a99db 540 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
7542af2a 541 new_number (hdr_size);
e9ee469a 542 }
8c95a62e 543 break;
67469e1f 544
8c95a62e 545 case DEFINED:
e9ee469a 546 if (expld.phase == lang_first_phase_enum)
1b493742 547 lang_track_definedness (tree->name.name);
8c95a62e
KH
548 else
549 {
550 struct bfd_link_hash_entry *h;
420e579c
HPN
551 int def_iteration
552 = lang_symbol_definition_iteration (tree->name.name);
8c95a62e 553
f13a99db
AM
554 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
555 &link_info,
8c95a62e 556 tree->name.name,
b34976b6 557 FALSE, FALSE, TRUE);
7542af2a
AM
558 new_number (h != NULL
559 && (h->type == bfd_link_hash_defined
560 || h->type == bfd_link_hash_defweak
561 || h->type == bfd_link_hash_common)
562 && (def_iteration == lang_statement_iteration
563 || def_iteration == -1));
8c95a62e
KH
564 }
565 break;
67469e1f 566
8c95a62e 567 case NAME:
e9ee469a
AM
568 if (expld.phase == lang_first_phase_enum)
569 ;
570 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
571 new_rel_from_abs (expld.dot);
572 else
8c95a62e
KH
573 {
574 struct bfd_link_hash_entry *h;
575
f13a99db
AM
576 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
577 &link_info,
8c95a62e 578 tree->name.name,
1b493742
NS
579 TRUE, FALSE, TRUE);
580 if (!h)
581 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
582 else if (h->type == bfd_link_hash_defined
583 || h->type == bfd_link_hash_defweak)
8c95a62e 584 {
7542af2a 585 asection *output_section;
8c95a62e 586
7542af2a
AM
587 output_section = h->u.def.section->output_section;
588 if (output_section == NULL)
589 {
590 if (expld.phase != lang_mark_phase_enum)
591 einfo (_("%X%S: unresolvable symbol `%s'"
592 " referenced in expression\n"),
593 tree->name.name);
8c95a62e 594 }
5c3049d2
AM
595 else if (output_section == bfd_abs_section_ptr
596 && (expld.section != bfd_abs_section_ptr
01554a74 597 || config.sane_expr))
abf4be64 598 new_number (h->u.def.value + h->u.def.section->output_offset);
7542af2a
AM
599 else
600 new_rel (h->u.def.value + h->u.def.section->output_offset,
601 output_section);
8c95a62e 602 }
e9ee469a
AM
603 else if (expld.phase == lang_final_phase_enum
604 || expld.assigning_to_dot)
8c95a62e
KH
605 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
606 tree->name.name);
1b493742
NS
607 else if (h->type == bfd_link_hash_new)
608 {
609 h->type = bfd_link_hash_undefined;
610 h->u.undef.abfd = NULL;
3eda52aa 611 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
a010d60f 612 bfd_link_add_undef (link_info.hash, h);
1b493742 613 }
8c95a62e
KH
614 }
615 break;
616
617 case ADDR:
e9ee469a 618 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
619 {
620 lang_output_section_statement_type *os;
621
622 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
623 if (os == NULL)
624 {
625 if (expld.phase == lang_final_phase_enum)
626 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
627 tree->name.name);
628 }
629 else if (os->processed_vma)
5942515f 630 new_rel (0, os->bfd_section);
8c95a62e 631 }
8c95a62e
KH
632 break;
633
634 case LOADADDR:
e9ee469a 635 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
636 {
637 lang_output_section_statement_type *os;
638
639 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
640 if (os == NULL)
641 {
642 if (expld.phase == lang_final_phase_enum)
643 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
644 tree->name.name);
645 }
646 else if (os->processed_lma)
1b493742 647 {
e9ee469a 648 if (os->load_base == NULL)
3e23777d 649 new_abs (os->bfd_section->lma);
e9ee469a 650 else
67469e1f
AM
651 {
652 exp_fold_tree_1 (os->load_base);
819da74e
AM
653 if (expld.result.valid_p)
654 make_abs ();
67469e1f 655 }
1b493742 656 }
8c95a62e 657 }
8c95a62e
KH
658 break;
659
660 case SIZEOF:
362c1d1a 661 case ALIGNOF:
e9ee469a 662 if (expld.phase != lang_first_phase_enum)
8c95a62e 663 {
8c95a62e
KH
664 lang_output_section_statement_type *os;
665
666 os = lang_output_section_find (tree->name.name);
5397b1fe 667 if (os == NULL)
cc3e2771
NS
668 {
669 if (expld.phase == lang_final_phase_enum)
670 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
671 tree->name.name);
7542af2a 672 new_number (0);
cc3e2771 673 }
cde9e0be 674 else if (os->processed_vma)
362c1d1a
NS
675 {
676 bfd_vma val;
677
678 if (tree->type.node_code == SIZEOF)
f13a99db
AM
679 val = (os->bfd_section->size
680 / bfd_octets_per_byte (link_info.output_bfd));
362c1d1a
NS
681 else
682 val = (bfd_vma)1 << os->bfd_section->alignment_power;
683
7542af2a 684 new_number (val);
362c1d1a 685 }
8c95a62e 686 }
8c95a62e
KH
687 break;
688
3ec57632
NC
689 case LENGTH:
690 {
691 lang_memory_region_type *mem;
692
693 mem = lang_memory_region_lookup (tree->name.name, FALSE);
694 if (mem != NULL)
7542af2a 695 new_number (mem->length);
3ec57632 696 else
e9ee469a
AM
697 einfo (_("%F%S: undefined MEMORY region `%s'"
698 " referenced in expression\n"), tree->name.name);
3ec57632
NC
699 }
700 break;
701
702 case ORIGIN:
7542af2a
AM
703 if (expld.phase != lang_first_phase_enum)
704 {
705 lang_memory_region_type *mem;
3ec57632 706
7542af2a
AM
707 mem = lang_memory_region_lookup (tree->name.name, FALSE);
708 if (mem != NULL)
709 new_rel_from_abs (mem->origin);
710 else
711 einfo (_("%F%S: undefined MEMORY region `%s'"
712 " referenced in expression\n"), tree->name.name);
713 }
3ec57632
NC
714 break;
715
24718e3b
L
716 case CONSTANT:
717 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
7542af2a 718 new_number (config.maxpagesize);
24718e3b 719 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
7542af2a 720 new_number (config.commonpagesize);
24718e3b
L
721 else
722 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
723 tree->name.name);
724 break;
725
8c95a62e
KH
726 default:
727 FAIL ();
728 break;
729 }
252b5132 730}
8c95a62e 731
e9ee469a
AM
732static void
733exp_fold_tree_1 (etree_type *tree)
252b5132 734{
252b5132
RH
735 if (tree == NULL)
736 {
e9ee469a
AM
737 memset (&expld.result, 0, sizeof (expld.result));
738 return;
252b5132
RH
739 }
740
4de2d33d 741 switch (tree->type.node_class)
252b5132
RH
742 {
743 case etree_value:
5c3049d2 744 if (expld.section == bfd_abs_section_ptr
01554a74 745 && !config.sane_expr)
5c3049d2
AM
746 new_abs (tree->value.value);
747 else
748 new_number (tree->value.value);
5942515f 749 expld.result.str = tree->value.str;
252b5132
RH
750 break;
751
752 case etree_rel:
e9ee469a
AM
753 if (expld.phase != lang_first_phase_enum)
754 {
755 asection *output_section = tree->rel.section->output_section;
756 new_rel (tree->rel.value + tree->rel.section->output_offset,
5942515f 757 output_section);
e9ee469a 758 }
252b5132 759 else
e9ee469a 760 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
761 break;
762
763 case etree_assert:
e9ee469a 764 exp_fold_tree_1 (tree->assert_s.child);
5397b1fe
AM
765 if (expld.phase == lang_final_phase_enum && !expld.result.value)
766 einfo ("%X%P: %s\n", tree->assert_s.message);
252b5132
RH
767 break;
768
769 case etree_unary:
e9ee469a 770 fold_unary (tree);
252b5132
RH
771 break;
772
773 case etree_binary:
e9ee469a 774 fold_binary (tree);
252b5132 775 break;
0ae1cf52
AM
776
777 case etree_trinary:
e9ee469a 778 fold_trinary (tree);
0ae1cf52 779 break;
252b5132
RH
780
781 case etree_assign:
782 case etree_provide:
b46a87b1 783 case etree_provided:
252b5132
RH
784 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
785 {
b46a87b1 786 if (tree->type.node_class != etree_assign)
252b5132 787 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
2f65ac72
AM
788 /* After allocation, assignment to dot should not be done inside
789 an output section since allocation adds a padding statement
790 that effectively duplicates the assignment. */
e9ee469a
AM
791 if (expld.phase == lang_mark_phase_enum
792 || expld.phase == lang_allocating_phase_enum
2f65ac72
AM
793 || ((expld.phase == lang_assigning_phase_enum
794 || expld.phase == lang_final_phase_enum)
e9ee469a 795 && expld.section == bfd_abs_section_ptr))
252b5132 796 {
fbbb9ac5 797 /* Notify the folder that this is an assignment to dot. */
e9ee469a
AM
798 expld.assigning_to_dot = TRUE;
799 exp_fold_tree_1 (tree->assign.src);
800 expld.assigning_to_dot = FALSE;
801
802 if (!expld.result.valid_p)
803 {
804 if (expld.phase != lang_mark_phase_enum)
805 einfo (_("%F%S invalid assignment to location counter\n"));
806 }
807 else if (expld.dotp == NULL)
808 einfo (_("%F%S assignment to location counter"
809 " invalid outside of SECTION\n"));
252b5132
RH
810 else
811 {
e9ee469a
AM
812 bfd_vma nextdot;
813
7542af2a
AM
814 nextdot = expld.result.value;
815 if (expld.result.section != NULL)
816 nextdot += expld.result.section->vma;
817 else
818 nextdot += expld.section->vma;
e9ee469a
AM
819 if (nextdot < expld.dot
820 && expld.section != bfd_abs_section_ptr)
821 einfo (_("%F%S cannot move location counter backwards"
822 " (from %V to %V)\n"), expld.dot, nextdot);
252b5132
RH
823 else
824 {
e9ee469a
AM
825 expld.dot = nextdot;
826 *expld.dotp = nextdot;
252b5132
RH
827 }
828 }
829 }
8b3d8fa8 830 else
e9ee469a 831 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
832 }
833 else
834 {
e9ee469a 835 struct bfd_link_hash_entry *h = NULL;
252b5132 836
e9ee469a
AM
837 if (tree->type.node_class == etree_provide)
838 {
252b5132 839 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
e9ee469a
AM
840 FALSE, FALSE, TRUE);
841 if (h == NULL
842 || (h->type != bfd_link_hash_new
843 && h->type != bfd_link_hash_undefined
844 && h->type != bfd_link_hash_common))
845 {
846 /* Do nothing. The symbol was never referenced, or was
847 defined by some object. */
848 break;
849 }
850 }
851
852 exp_fold_tree_1 (tree->assign.src);
e759c116
AM
853 if (expld.result.valid_p
854 || (expld.phase == lang_first_phase_enum
01554a74
AM
855 && tree->type.node_class == etree_assign
856 && tree->assign.hidden))
e9ee469a 857 {
1579bae1 858 if (h == NULL)
67010b46 859 {
e9ee469a
AM
860 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
861 TRUE, FALSE, TRUE);
862 if (h == NULL)
67010b46
NC
863 einfo (_("%P%F:%s: hash creation failed\n"),
864 tree->assign.dst);
865 }
e9ee469a
AM
866
867 /* FIXME: Should we worry if the symbol is already
868 defined? */
869 lang_update_definedness (tree->assign.dst, h);
870 h->type = bfd_link_hash_defined;
871 h->u.def.value = expld.result.value;
7542af2a
AM
872 if (expld.result.section == NULL)
873 expld.result.section = expld.section;
e9ee469a
AM
874 h->u.def.section = expld.result.section;
875 if (tree->type.node_class == etree_provide)
876 tree->type.node_class = etree_provided;
1338dd10
PB
877
878 /* Copy the symbol type if this is a simple assignment of
879 one symbol to annother. */
880 if (tree->assign.src->type.node_class == etree_name)
881 {
882 struct bfd_link_hash_entry *hsrc;
883
884 hsrc = bfd_link_hash_lookup (link_info.hash,
885 tree->assign.src->name.name,
886 FALSE, FALSE, TRUE);
887 if (hsrc)
888 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
889 hsrc);
890 }
252b5132 891 }
e092cb30
AM
892 else if (expld.phase == lang_final_phase_enum)
893 {
894 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
895 FALSE, FALSE, TRUE);
896 if (h != NULL
897 && h->type == bfd_link_hash_new)
898 h->type = bfd_link_hash_undefined;
899 }
252b5132
RH
900 }
901 break;
902
903 case etree_name:
e9ee469a 904 fold_name (tree);
252b5132
RH
905 break;
906
907 default:
908 FAIL ();
e9ee469a 909 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
910 break;
911 }
252b5132
RH
912}
913
e9ee469a
AM
914void
915exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
75ff4589 916{
e9ee469a
AM
917 expld.dot = *dotp;
918 expld.dotp = dotp;
919 expld.section = current_section;
920 exp_fold_tree_1 (tree);
75ff4589
L
921}
922
e759c116 923void
e9ee469a 924exp_fold_tree_no_dot (etree_type *tree)
252b5132 925{
e9ee469a
AM
926 expld.dot = 0;
927 expld.dotp = NULL;
928 expld.section = bfd_abs_section_ptr;
929 exp_fold_tree_1 (tree);
252b5132
RH
930}
931
932etree_type *
1579bae1 933exp_binop (int code, etree_type *lhs, etree_type *rhs)
252b5132 934{
d3ce72d0 935 etree_type value, *new_e;
252b5132
RH
936
937 value.type.node_code = code;
f856040a 938 value.type.lineno = lhs->type.lineno;
252b5132
RH
939 value.binary.lhs = lhs;
940 value.binary.rhs = rhs;
941 value.type.node_class = etree_binary;
e9ee469a
AM
942 exp_fold_tree_no_dot (&value);
943 if (expld.result.valid_p)
944 return exp_intop (expld.result.value);
945
d3ce72d0
NC
946 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
947 memcpy (new_e, &value, sizeof (new_e->binary));
948 return new_e;
252b5132
RH
949}
950
951etree_type *
1579bae1 952exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
252b5132 953{
d3ce72d0 954 etree_type value, *new_e;
e9ee469a 955
252b5132 956 value.type.node_code = code;
f856040a 957 value.type.lineno = lhs->type.lineno;
252b5132
RH
958 value.trinary.lhs = lhs;
959 value.trinary.cond = cond;
960 value.trinary.rhs = rhs;
961 value.type.node_class = etree_trinary;
e9ee469a
AM
962 exp_fold_tree_no_dot (&value);
963 if (expld.result.valid_p)
964 return exp_intop (expld.result.value);
c7d701b0 965
d3ce72d0
NC
966 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
967 memcpy (new_e, &value, sizeof (new_e->trinary));
968 return new_e;
252b5132
RH
969}
970
252b5132 971etree_type *
1579bae1 972exp_unop (int code, etree_type *child)
252b5132 973{
d3ce72d0 974 etree_type value, *new_e;
252b5132 975
252b5132 976 value.unary.type.node_code = code;
f856040a 977 value.unary.type.lineno = child->type.lineno;
252b5132
RH
978 value.unary.child = child;
979 value.unary.type.node_class = etree_unary;
e9ee469a
AM
980 exp_fold_tree_no_dot (&value);
981 if (expld.result.valid_p)
982 return exp_intop (expld.result.value);
c7d701b0 983
d3ce72d0
NC
984 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
985 memcpy (new_e, &value, sizeof (new_e->unary));
986 return new_e;
252b5132
RH
987}
988
252b5132 989etree_type *
1579bae1 990exp_nameop (int code, const char *name)
252b5132 991{
d3ce72d0 992 etree_type value, *new_e;
e9ee469a 993
252b5132 994 value.name.type.node_code = code;
f856040a 995 value.name.type.lineno = lineno;
252b5132
RH
996 value.name.name = name;
997 value.name.type.node_class = etree_name;
998
e9ee469a
AM
999 exp_fold_tree_no_dot (&value);
1000 if (expld.result.valid_p)
1001 return exp_intop (expld.result.value);
c7d701b0 1002
d3ce72d0
NC
1003 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1004 memcpy (new_e, &value, sizeof (new_e->name));
1005 return new_e;
252b5132
RH
1006
1007}
1008
2e57b2af
AM
1009static etree_type *
1010exp_assop (const char *dst,
1011 etree_type *src,
1012 enum node_tree_enum class,
1013 bfd_boolean hidden)
252b5132
RH
1014{
1015 etree_type *n;
1016
1e9cc1c2 1017 n = (etree_type *) stat_alloc (sizeof (n->assign));
252b5132 1018 n->assign.type.node_code = '=';
f856040a 1019 n->assign.type.lineno = src->type.lineno;
2e57b2af 1020 n->assign.type.node_class = class;
252b5132
RH
1021 n->assign.src = src;
1022 n->assign.dst = dst;
7af8e998 1023 n->assign.hidden = hidden;
252b5132
RH
1024 return n;
1025}
1026
2e57b2af
AM
1027etree_type *
1028exp_assign (const char *dst, etree_type *src)
1029{
1030 return exp_assop (dst, src, etree_assign, FALSE);
1031}
1032
1033etree_type *
1034exp_defsym (const char *dst, etree_type *src)
1035{
1036 return exp_assop (dst, src, etree_assign, TRUE);
1037}
1038
1039/* Handle PROVIDE. */
1040
1041etree_type *
1042exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1043{
1044 return exp_assop (dst, src, etree_provide, hidden);
1045}
1046
252b5132
RH
1047/* Handle ASSERT. */
1048
1049etree_type *
1579bae1 1050exp_assert (etree_type *exp, const char *message)
252b5132
RH
1051{
1052 etree_type *n;
1053
1e9cc1c2 1054 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
252b5132 1055 n->assert_s.type.node_code = '!';
f856040a 1056 n->assert_s.type.lineno = exp->type.lineno;
252b5132
RH
1057 n->assert_s.type.node_class = etree_assert;
1058 n->assert_s.child = exp;
1059 n->assert_s.message = message;
1060 return n;
1061}
1062
4de2d33d 1063void
1579bae1 1064exp_print_tree (etree_type *tree)
252b5132 1065{
ae78bbeb
AM
1066 bfd_boolean function_like;
1067
c7d701b0
NC
1068 if (config.map_file == NULL)
1069 config.map_file = stderr;
b7a26f91 1070
c7d701b0
NC
1071 if (tree == NULL)
1072 {
1073 minfo ("NULL TREE\n");
1074 return;
1075 }
b7a26f91 1076
8c95a62e
KH
1077 switch (tree->type.node_class)
1078 {
1079 case etree_value:
1080 minfo ("0x%v", tree->value.value);
1081 return;
1082 case etree_rel:
1083 if (tree->rel.section->owner != NULL)
1084 minfo ("%B:", tree->rel.section->owner);
1085 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1086 return;
1087 case etree_assign:
ae78bbeb 1088 fputs (tree->assign.dst, config.map_file);
b34976b6 1089 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1090 exp_print_tree (tree->assign.src);
1091 break;
1092 case etree_provide:
b46a87b1 1093 case etree_provided:
8c95a62e
KH
1094 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1095 exp_print_tree (tree->assign.src);
ae78bbeb 1096 fputc (')', config.map_file);
8c95a62e
KH
1097 break;
1098 case etree_binary:
ae78bbeb
AM
1099 function_like = FALSE;
1100 switch (tree->type.node_code)
1101 {
1102 case MAX_K:
1103 case MIN_K:
1104 case ALIGN_K:
1105 case DATA_SEGMENT_ALIGN:
1106 case DATA_SEGMENT_RELRO_END:
1107 function_like = TRUE;
1108 }
1109 if (function_like)
1110 {
1111 exp_print_token (tree->type.node_code, FALSE);
1112 fputc (' ', config.map_file);
1113 }
1114 fputc ('(', config.map_file);
8c95a62e 1115 exp_print_tree (tree->binary.lhs);
ae78bbeb
AM
1116 if (function_like)
1117 fprintf (config.map_file, ", ");
1118 else
1119 exp_print_token (tree->type.node_code, TRUE);
8c95a62e 1120 exp_print_tree (tree->binary.rhs);
ae78bbeb 1121 fputc (')', config.map_file);
8c95a62e
KH
1122 break;
1123 case etree_trinary:
1124 exp_print_tree (tree->trinary.cond);
ae78bbeb 1125 fputc ('?', config.map_file);
8c95a62e 1126 exp_print_tree (tree->trinary.lhs);
ae78bbeb 1127 fputc (':', config.map_file);
8c95a62e
KH
1128 exp_print_tree (tree->trinary.rhs);
1129 break;
1130 case etree_unary:
b34976b6 1131 exp_print_token (tree->unary.type.node_code, FALSE);
8c95a62e
KH
1132 if (tree->unary.child)
1133 {
7b17f854 1134 fprintf (config.map_file, " (");
8c95a62e 1135 exp_print_tree (tree->unary.child);
ae78bbeb 1136 fputc (')', config.map_file);
8c95a62e
KH
1137 }
1138 break;
1139
1140 case etree_assert:
1141 fprintf (config.map_file, "ASSERT (");
1142 exp_print_tree (tree->assert_s.child);
1143 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1144 break;
1145
8c95a62e
KH
1146 case etree_name:
1147 if (tree->type.node_code == NAME)
ae78bbeb 1148 fputs (tree->name.name, config.map_file);
8c95a62e
KH
1149 else
1150 {
b34976b6 1151 exp_print_token (tree->type.node_code, FALSE);
8c95a62e 1152 if (tree->name.name)
7b17f854 1153 fprintf (config.map_file, " (%s)", tree->name.name);
8c95a62e
KH
1154 }
1155 break;
1156 default:
1157 FAIL ();
1158 break;
252b5132 1159 }
252b5132
RH
1160}
1161
1162bfd_vma
e9ee469a 1163exp_get_vma (etree_type *tree, bfd_vma def, char *name)
252b5132 1164{
252b5132
RH
1165 if (tree != NULL)
1166 {
e9ee469a
AM
1167 exp_fold_tree_no_dot (tree);
1168 if (expld.result.valid_p)
1169 return expld.result.value;
1170 else if (name != NULL && expld.phase != lang_mark_phase_enum)
c58dea77 1171 einfo (_("%F%S: nonconstant expression for %s\n"), name);
252b5132 1172 }
e9ee469a 1173 return def;
252b5132
RH
1174}
1175
4de2d33d 1176int
e9ee469a 1177exp_get_value_int (etree_type *tree, int def, char *name)
252b5132 1178{
e9ee469a 1179 return exp_get_vma (tree, def, name);
252b5132
RH
1180}
1181
2c382fb6 1182fill_type *
e9ee469a 1183exp_get_fill (etree_type *tree, fill_type *def, char *name)
2c382fb6
AM
1184{
1185 fill_type *fill;
2c382fb6
AM
1186 size_t len;
1187 unsigned int val;
1188
1189 if (tree == NULL)
1190 return def;
1191
e9ee469a
AM
1192 exp_fold_tree_no_dot (tree);
1193 if (!expld.result.valid_p)
1194 {
1195 if (name != NULL && expld.phase != lang_mark_phase_enum)
c58dea77 1196 einfo (_("%F%S: nonconstant expression for %s\n"), name);
e9ee469a
AM
1197 return def;
1198 }
2c382fb6 1199
e9ee469a 1200 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
2c382fb6
AM
1201 {
1202 unsigned char *dst;
1203 unsigned char *s;
1e9cc1c2 1204 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
2c382fb6
AM
1205 fill->size = (len + 1) / 2;
1206 dst = fill->data;
e9ee469a 1207 s = (unsigned char *) expld.result.str;
2c382fb6
AM
1208 val = 0;
1209 do
1210 {
1211 unsigned int digit;
1212
1213 digit = *s++ - '0';
1214 if (digit > 9)
1215 digit = (digit - 'A' + '0' + 10) & 0xf;
1216 val <<= 4;
1217 val += digit;
1218 --len;
1219 if ((len & 1) == 0)
1220 {
1221 *dst++ = val;
1222 val = 0;
1223 }
1224 }
1225 while (len != 0);
1226 }
1227 else
1228 {
1e9cc1c2 1229 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
e9ee469a 1230 val = expld.result.value;
2c382fb6
AM
1231 fill->data[0] = (val >> 24) & 0xff;
1232 fill->data[1] = (val >> 16) & 0xff;
1233 fill->data[2] = (val >> 8) & 0xff;
1234 fill->data[3] = (val >> 0) & 0xff;
1235 fill->size = 4;
1236 }
1237 return fill;
1238}
1239
252b5132 1240bfd_vma
e9ee469a 1241exp_get_abs_int (etree_type *tree, int def, char *name)
252b5132 1242{
e9ee469a
AM
1243 if (tree != NULL)
1244 {
1245 exp_fold_tree_no_dot (tree);
c7d701b0 1246
e9ee469a
AM
1247 if (expld.result.valid_p)
1248 {
7542af2a
AM
1249 if (expld.result.section != NULL)
1250 expld.result.value += expld.result.section->vma;
e9ee469a
AM
1251 return expld.result.value;
1252 }
1253 else if (name != NULL && expld.phase != lang_mark_phase_enum)
f856040a
L
1254 {
1255 lineno = tree->type.lineno;
1256 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1257 }
e9ee469a
AM
1258 }
1259 return def;
252b5132 1260}
c553bb91 1261
e5caa5e0
AM
1262static bfd_vma
1263align_n (bfd_vma value, bfd_vma align)
c553bb91
AM
1264{
1265 if (align <= 1)
1266 return value;
1267
1268 value = (value + align - 1) / align;
1269 return value * align;
1270}