]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldexp.c
gas/
[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,
aa820537 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
AM
46static void exp_fold_tree_1 (etree_type *);
47static void exp_fold_tree_no_dot (etree_type *);
48static bfd_vma align_n (bfd_vma, bfd_vma);
2d20f7bf 49
ba916c8a
MM
50segment_type *segments;
51
e9ee469a 52struct ldexp_control expld;
fbbb9ac5 53
7b17f854 54/* Print the string representation of the given token. Surround it
b34976b6 55 with spaces if INFIX_P is TRUE. */
7b17f854 56
252b5132 57static void
1579bae1 58exp_print_token (token_code_type code, int infix_p)
252b5132 59{
4da711b1 60 static const struct
c7d701b0 61 {
8c95a62e 62 token_code_type code;
c7d701b0
NC
63 char * name;
64 }
65 table[] =
66 {
8c95a62e 67 { INT, "int" },
8c95a62e
KH
68 { NAME, "NAME" },
69 { PLUSEQ, "+=" },
70 { MINUSEQ, "-=" },
71 { MULTEQ, "*=" },
72 { DIVEQ, "/=" },
73 { LSHIFTEQ, "<<=" },
74 { RSHIFTEQ, ">>=" },
75 { ANDEQ, "&=" },
76 { OREQ, "|=" },
77 { OROR, "||" },
78 { ANDAND, "&&" },
79 { EQ, "==" },
80 { NE, "!=" },
81 { LE, "<=" },
82 { GE, ">=" },
83 { LSHIFT, "<<" },
7cecdbff 84 { RSHIFT, ">>" },
8c95a62e
KH
85 { ALIGN_K, "ALIGN" },
86 { BLOCK, "BLOCK" },
c7d701b0
NC
87 { QUAD, "QUAD" },
88 { SQUAD, "SQUAD" },
89 { LONG, "LONG" },
90 { SHORT, "SHORT" },
91 { BYTE, "BYTE" },
8c95a62e
KH
92 { SECTIONS, "SECTIONS" },
93 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
8c95a62e
KH
94 { MEMORY, "MEMORY" },
95 { DEFINED, "DEFINED" },
96 { TARGET_K, "TARGET" },
97 { SEARCH_DIR, "SEARCH_DIR" },
98 { MAP, "MAP" },
8c95a62e 99 { ENTRY, "ENTRY" },
c7d701b0 100 { NEXT, "NEXT" },
362c1d1a 101 { ALIGNOF, "ALIGNOF" },
c7d701b0
NC
102 { SIZEOF, "SIZEOF" },
103 { ADDR, "ADDR" },
104 { LOADADDR, "LOADADDR" },
24718e3b 105 { CONSTANT, "CONSTANT" },
8c0848b5
AM
106 { ABSOLUTE, "ABSOLUTE" },
107 { MAX_K, "MAX" },
108 { MIN_K, "MIN" },
109 { ASSERT_K, "ASSERT" },
1049f94e 110 { REL, "relocatable" },
2d20f7bf 111 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
8c37241b 112 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
ba916c8a 113 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
3ec57632
NC
114 { ORIGIN, "ORIGIN" },
115 { LENGTH, "LENGTH" },
ba916c8a 116 { SEGMENT_START, "SEGMENT_START" }
8c95a62e 117 };
252b5132
RH
118 unsigned int idx;
119
7b17f854
RS
120 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
121 if (table[idx].code == code)
122 break;
c7d701b0 123
7b17f854
RS
124 if (infix_p)
125 fputc (' ', config.map_file);
126
127 if (idx < ARRAY_SIZE (table))
128 fputs (table[idx].name, config.map_file);
129 else if (code < 127)
130 fputc (code, config.map_file);
c7d701b0 131 else
7b17f854
RS
132 fprintf (config.map_file, "<code %d>", code);
133
134 if (infix_p)
135 fputc (' ', config.map_file);
252b5132
RH
136}
137
4de2d33d 138static void
e9ee469a 139make_abs (void)
252b5132 140{
e9ee469a
AM
141 expld.result.value += expld.result.section->vma;
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
e9ee469a
AM
192static void
193new_rel (bfd_vma value, char *str, asection *section)
252b5132 194{
e9ee469a
AM
195 expld.result.valid_p = TRUE;
196 expld.result.value = value;
197 expld.result.str = str;
198 expld.result.section = section;
252b5132
RH
199}
200
e9ee469a
AM
201static void
202new_rel_from_abs (bfd_vma value)
252b5132 203{
e9ee469a
AM
204 expld.result.valid_p = TRUE;
205 expld.result.value = value - expld.section->vma;
206 expld.result.str = NULL;
207 expld.result.section = expld.section;
252b5132
RH
208}
209
e9ee469a
AM
210static void
211fold_unary (etree_type *tree)
0ae1cf52 212{
e9ee469a
AM
213 exp_fold_tree_1 (tree->unary.child);
214 if (expld.result.valid_p)
0ae1cf52
AM
215 {
216 switch (tree->type.node_code)
217 {
218 case ALIGN_K:
e9ee469a 219 if (expld.phase != lang_first_phase_enum)
dea2f0a8 220 new_rel_from_abs (align_n (expld.dot, expld.result.value));
0ae1cf52 221 else
e9ee469a 222 expld.result.valid_p = FALSE;
0ae1cf52
AM
223 break;
224
225 case ABSOLUTE:
e9ee469a 226 make_abs ();
0ae1cf52
AM
227 break;
228
229 case '~':
e9ee469a
AM
230 make_abs ();
231 expld.result.value = ~expld.result.value;
0ae1cf52
AM
232 break;
233
234 case '!':
e9ee469a
AM
235 make_abs ();
236 expld.result.value = !expld.result.value;
0ae1cf52
AM
237 break;
238
239 case '-':
e9ee469a
AM
240 make_abs ();
241 expld.result.value = -expld.result.value;
0ae1cf52
AM
242 break;
243
244 case NEXT:
245 /* Return next place aligned to value. */
e9ee469a 246 if (expld.phase != lang_first_phase_enum)
0ae1cf52 247 {
e9ee469a
AM
248 make_abs ();
249 expld.result.value = align_n (expld.dot, expld.result.value);
0ae1cf52
AM
250 }
251 else
e9ee469a 252 expld.result.valid_p = FALSE;
0ae1cf52
AM
253 break;
254
255 case DATA_SEGMENT_END:
e9ee469a
AM
256 if (expld.phase != lang_first_phase_enum
257 && expld.section == bfd_abs_section_ptr
258 && (expld.dataseg.phase == exp_dataseg_align_seen
259 || expld.dataseg.phase == exp_dataseg_relro_seen
260 || expld.dataseg.phase == exp_dataseg_adjust
261 || expld.dataseg.phase == exp_dataseg_relro_adjust
262 || expld.phase == lang_final_phase_enum))
0ae1cf52 263 {
e9ee469a
AM
264 if (expld.dataseg.phase == exp_dataseg_align_seen
265 || expld.dataseg.phase == exp_dataseg_relro_seen)
0ae1cf52 266 {
e9ee469a
AM
267 expld.dataseg.phase = exp_dataseg_end_seen;
268 expld.dataseg.end = expld.result.value;
0ae1cf52
AM
269 }
270 }
271 else
e9ee469a 272 expld.result.valid_p = FALSE;
0ae1cf52
AM
273 break;
274
275 default:
276 FAIL ();
277 break;
278 }
279 }
0ae1cf52
AM
280}
281
e9ee469a
AM
282static void
283fold_binary (etree_type *tree)
252b5132 284{
4ac0c898 285 etree_value_type lhs;
e9ee469a 286 exp_fold_tree_1 (tree->binary.lhs);
ba916c8a
MM
287
288 /* The SEGMENT_START operator is special because its first
8c0848b5
AM
289 operand is a string, not the name of a symbol. Note that the
290 operands have been swapped, so binary.lhs is second (default)
291 operand, binary.rhs is first operand. */
e9ee469a 292 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
ba916c8a
MM
293 {
294 const char *segment_name;
295 segment_type *seg;
296 /* Check to see if the user has overridden the default
297 value. */
298 segment_name = tree->binary.rhs->name.name;
299 for (seg = segments; seg; seg = seg->next)
300 if (strcmp (seg->name, segment_name) == 0)
301 {
302 seg->used = TRUE;
e9ee469a
AM
303 expld.result.value = seg->value;
304 expld.result.str = NULL;
8c0848b5 305 expld.result.section = expld.section;
ba916c8a
MM
306 break;
307 }
4ac0c898 308 return;
ba916c8a 309 }
252b5132 310
4ac0c898
AM
311 lhs = expld.result;
312 exp_fold_tree_1 (tree->binary.rhs);
313 expld.result.valid_p &= lhs.valid_p;
314
315 if (expld.result.valid_p)
316 {
317 /* If the values are from different sections, or this is an
318 absolute expression, make both the source arguments
319 absolute. However, adding or subtracting an absolute
320 value from a relative value is meaningful, and is an
321 exception. */
322 if (expld.section != bfd_abs_section_ptr
323 && lhs.section == bfd_abs_section_ptr
324 && tree->type.node_code == '+')
252b5132 325 {
4ac0c898
AM
326 /* Keep the section of the rhs term. */
327 expld.result.value = lhs.value + expld.result.value;
328 return;
329 }
330 else if (expld.section != bfd_abs_section_ptr
331 && expld.result.section == bfd_abs_section_ptr
332 && (tree->type.node_code == '+'
333 || tree->type.node_code == '-'))
334 {
335 /* Keep the section of the lhs term. */
336 expld.result.section = lhs.section;
337 }
338 else if (expld.result.section != lhs.section
339 || expld.section == bfd_abs_section_ptr)
340 {
341 make_abs ();
342 lhs.value += lhs.section->vma;
343 }
252b5132 344
4ac0c898
AM
345 switch (tree->type.node_code)
346 {
347 case '%':
348 if (expld.result.value != 0)
349 expld.result.value = ((bfd_signed_vma) lhs.value
350 % (bfd_signed_vma) expld.result.value);
351 else if (expld.phase != lang_mark_phase_enum)
352 einfo (_("%F%S %% by zero\n"));
353 break;
252b5132 354
4ac0c898
AM
355 case '/':
356 if (expld.result.value != 0)
357 expld.result.value = ((bfd_signed_vma) lhs.value
358 / (bfd_signed_vma) expld.result.value);
359 else if (expld.phase != lang_mark_phase_enum)
360 einfo (_("%F%S / by zero\n"));
361 break;
252b5132 362
e9ee469a
AM
363#define BOP(x, y) \
364 case x: \
365 expld.result.value = lhs.value y expld.result.value; \
366 break;
367
4ac0c898
AM
368 BOP ('+', +);
369 BOP ('*', *);
370 BOP ('-', -);
371 BOP (LSHIFT, <<);
372 BOP (RSHIFT, >>);
373 BOP (EQ, ==);
374 BOP (NE, !=);
375 BOP ('<', <);
376 BOP ('>', >);
377 BOP (LE, <=);
378 BOP (GE, >=);
379 BOP ('&', &);
380 BOP ('^', ^);
381 BOP ('|', |);
382 BOP (ANDAND, &&);
383 BOP (OROR, ||);
384
385 case MAX_K:
386 if (lhs.value > expld.result.value)
387 expld.result.value = lhs.value;
388 break;
252b5132 389
4ac0c898
AM
390 case MIN_K:
391 if (lhs.value < expld.result.value)
392 expld.result.value = lhs.value;
393 break;
252b5132 394
4ac0c898
AM
395 case ALIGN_K:
396 expld.result.value = align_n (lhs.value, expld.result.value);
397 break;
c468c8bc 398
4ac0c898
AM
399 case DATA_SEGMENT_ALIGN:
400 expld.dataseg.relro = exp_dataseg_relro_start;
401 if (expld.phase != lang_first_phase_enum
402 && expld.section == bfd_abs_section_ptr
403 && (expld.dataseg.phase == exp_dataseg_none
404 || expld.dataseg.phase == exp_dataseg_adjust
405 || expld.dataseg.phase == exp_dataseg_relro_adjust
406 || expld.phase == lang_final_phase_enum))
407 {
408 bfd_vma maxpage = lhs.value;
409 bfd_vma commonpage = expld.result.value;
2d20f7bf 410
4ac0c898
AM
411 expld.result.value = align_n (expld.dot, maxpage);
412 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
413 expld.result.value = expld.dataseg.base;
414 else if (expld.dataseg.phase != exp_dataseg_adjust)
415 {
416 expld.result.value += expld.dot & (maxpage - 1);
417 if (expld.phase == lang_allocating_phase_enum)
2d20f7bf 418 {
4ac0c898
AM
419 expld.dataseg.phase = exp_dataseg_align_seen;
420 expld.dataseg.min_base = expld.dot;
421 expld.dataseg.base = expld.result.value;
422 expld.dataseg.pagesize = commonpage;
423 expld.dataseg.maxpagesize = maxpage;
424 expld.dataseg.relro_end = 0;
2d20f7bf 425 }
2d20f7bf 426 }
4ac0c898
AM
427 else if (commonpage < maxpage)
428 expld.result.value += ((expld.dot + commonpage - 1)
429 & (maxpage - commonpage));
430 }
431 else
432 expld.result.valid_p = FALSE;
433 break;
e9ee469a 434
4ac0c898
AM
435 case DATA_SEGMENT_RELRO_END:
436 expld.dataseg.relro = exp_dataseg_relro_end;
437 if (expld.phase != lang_first_phase_enum
438 && (expld.dataseg.phase == exp_dataseg_align_seen
439 || expld.dataseg.phase == exp_dataseg_adjust
440 || expld.dataseg.phase == exp_dataseg_relro_adjust
441 || expld.phase == lang_final_phase_enum))
442 {
443 if (expld.dataseg.phase == exp_dataseg_align_seen
444 || expld.dataseg.phase == exp_dataseg_relro_adjust)
445 expld.dataseg.relro_end = lhs.value + expld.result.value;
e9ee469a 446
4ac0c898
AM
447 if (expld.dataseg.phase == exp_dataseg_relro_adjust
448 && (expld.dataseg.relro_end
449 & (expld.dataseg.pagesize - 1)))
450 {
451 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
452 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
453 expld.result.value = (expld.dataseg.relro_end
454 - expld.result.value);
a4f5ad88
JJ
455 }
456 else
4ac0c898 457 expld.result.value = lhs.value;
a4f5ad88 458
4ac0c898
AM
459 if (expld.dataseg.phase == exp_dataseg_align_seen)
460 expld.dataseg.phase = exp_dataseg_relro_seen;
252b5132 461 }
4ac0c898
AM
462 else
463 expld.result.valid_p = FALSE;
464 break;
465
466 default:
467 FAIL ();
252b5132 468 }
252b5132 469 }
252b5132
RH
470}
471
e9ee469a
AM
472static void
473fold_trinary (etree_type *tree)
0ae1cf52 474{
e9ee469a
AM
475 exp_fold_tree_1 (tree->trinary.cond);
476 if (expld.result.valid_p)
477 exp_fold_tree_1 (expld.result.value
478 ? tree->trinary.lhs
479 : tree->trinary.rhs);
0ae1cf52
AM
480}
481
e9ee469a
AM
482static void
483fold_name (etree_type *tree)
252b5132 484{
e9ee469a 485 memset (&expld.result, 0, sizeof (expld.result));
c468c8bc 486
4de2d33d 487 switch (tree->type.node_code)
8c95a62e
KH
488 {
489 case SIZEOF_HEADERS:
e9ee469a
AM
490 if (expld.phase != lang_first_phase_enum)
491 {
492 bfd_vma hdr_size = 0;
493 /* Don't find the real header size if only marking sections;
494 The bfd function may cache incorrect data. */
495 if (expld.phase != lang_mark_phase_enum)
f13a99db 496 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
e9ee469a
AM
497 new_abs (hdr_size);
498 }
8c95a62e 499 break;
67469e1f 500
8c95a62e 501 case DEFINED:
e9ee469a 502 if (expld.phase == lang_first_phase_enum)
1b493742 503 lang_track_definedness (tree->name.name);
8c95a62e
KH
504 else
505 {
506 struct bfd_link_hash_entry *h;
420e579c
HPN
507 int def_iteration
508 = lang_symbol_definition_iteration (tree->name.name);
8c95a62e 509
f13a99db
AM
510 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
511 &link_info,
8c95a62e 512 tree->name.name,
b34976b6 513 FALSE, FALSE, TRUE);
e9ee469a
AM
514 expld.result.value = (h != NULL
515 && (h->type == bfd_link_hash_defined
516 || h->type == bfd_link_hash_defweak
517 || h->type == bfd_link_hash_common)
518 && (def_iteration == lang_statement_iteration
519 || def_iteration == -1));
8c0848b5 520 expld.result.section = expld.section;
e9ee469a 521 expld.result.valid_p = TRUE;
8c95a62e
KH
522 }
523 break;
67469e1f 524
8c95a62e 525 case NAME:
e9ee469a
AM
526 if (expld.phase == lang_first_phase_enum)
527 ;
528 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
529 new_rel_from_abs (expld.dot);
530 else
8c95a62e
KH
531 {
532 struct bfd_link_hash_entry *h;
533
f13a99db
AM
534 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
535 &link_info,
8c95a62e 536 tree->name.name,
1b493742
NS
537 TRUE, FALSE, TRUE);
538 if (!h)
539 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
540 else if (h->type == bfd_link_hash_defined
541 || h->type == bfd_link_hash_defweak)
8c95a62e
KH
542 {
543 if (bfd_is_abs_section (h->u.def.section))
e9ee469a
AM
544 new_abs (h->u.def.value);
545 else
8c95a62e
KH
546 {
547 asection *output_section;
548
549 output_section = h->u.def.section->output_section;
550 if (output_section == NULL)
8c95a62e 551 {
e9ee469a
AM
552 if (expld.phase != lang_mark_phase_enum)
553 einfo (_("%X%S: unresolvable symbol `%s'"
554 " referenced in expression\n"),
555 tree->name.name);
8c95a62e 556 }
e9ee469a
AM
557 else
558 new_rel (h->u.def.value + h->u.def.section->output_offset,
559 NULL, output_section);
8c95a62e
KH
560 }
561 }
e9ee469a
AM
562 else if (expld.phase == lang_final_phase_enum
563 || expld.assigning_to_dot)
8c95a62e
KH
564 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
565 tree->name.name);
1b493742
NS
566 else if (h->type == bfd_link_hash_new)
567 {
568 h->type = bfd_link_hash_undefined;
569 h->u.undef.abfd = NULL;
3eda52aa 570 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
a010d60f 571 bfd_link_add_undef (link_info.hash, h);
1b493742 572 }
8c95a62e
KH
573 }
574 break;
575
576 case ADDR:
e9ee469a 577 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
578 {
579 lang_output_section_statement_type *os;
580
581 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
582 if (os == NULL)
583 {
584 if (expld.phase == lang_final_phase_enum)
585 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
586 tree->name.name);
587 }
588 else if (os->processed_vma)
e9ee469a 589 new_rel (0, NULL, os->bfd_section);
8c95a62e 590 }
8c95a62e
KH
591 break;
592
593 case LOADADDR:
e9ee469a 594 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
595 {
596 lang_output_section_statement_type *os;
597
598 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
599 if (os == NULL)
600 {
601 if (expld.phase == lang_final_phase_enum)
602 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
603 tree->name.name);
604 }
605 else if (os->processed_lma)
1b493742 606 {
e9ee469a 607 if (os->load_base == NULL)
3e23777d 608 new_abs (os->bfd_section->lma);
e9ee469a 609 else
67469e1f
AM
610 {
611 exp_fold_tree_1 (os->load_base);
819da74e
AM
612 if (expld.result.valid_p)
613 make_abs ();
67469e1f 614 }
1b493742 615 }
8c95a62e 616 }
8c95a62e
KH
617 break;
618
619 case SIZEOF:
362c1d1a 620 case ALIGNOF:
e9ee469a 621 if (expld.phase != lang_first_phase_enum)
8c95a62e 622 {
8c95a62e
KH
623 lang_output_section_statement_type *os;
624
625 os = lang_output_section_find (tree->name.name);
5397b1fe 626 if (os == NULL)
cc3e2771
NS
627 {
628 if (expld.phase == lang_final_phase_enum)
629 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
630 tree->name.name);
631 new_abs (0);
632 }
cde9e0be 633 else if (os->processed_vma)
362c1d1a
NS
634 {
635 bfd_vma val;
636
637 if (tree->type.node_code == SIZEOF)
f13a99db
AM
638 val = (os->bfd_section->size
639 / bfd_octets_per_byte (link_info.output_bfd));
362c1d1a
NS
640 else
641 val = (bfd_vma)1 << os->bfd_section->alignment_power;
642
643 new_abs (val);
644 }
8c95a62e 645 }
8c95a62e
KH
646 break;
647
3ec57632
NC
648 case LENGTH:
649 {
650 lang_memory_region_type *mem;
651
652 mem = lang_memory_region_lookup (tree->name.name, FALSE);
653 if (mem != NULL)
e9ee469a 654 new_abs (mem->length);
3ec57632 655 else
e9ee469a
AM
656 einfo (_("%F%S: undefined MEMORY region `%s'"
657 " referenced in expression\n"), tree->name.name);
3ec57632
NC
658 }
659 break;
660
661 case ORIGIN:
662 {
663 lang_memory_region_type *mem;
664
665 mem = lang_memory_region_lookup (tree->name.name, FALSE);
666 if (mem != NULL)
e9ee469a 667 new_abs (mem->origin);
3ec57632 668 else
e9ee469a
AM
669 einfo (_("%F%S: undefined MEMORY region `%s'"
670 " referenced in expression\n"), tree->name.name);
3ec57632
NC
671 }
672 break;
673
24718e3b
L
674 case CONSTANT:
675 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
8be573a7 676 new_abs (config.maxpagesize);
24718e3b 677 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
8be573a7 678 new_abs (config.commonpagesize);
24718e3b
L
679 else
680 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
681 tree->name.name);
682 break;
683
8c95a62e
KH
684 default:
685 FAIL ();
686 break;
687 }
252b5132 688}
8c95a62e 689
e9ee469a
AM
690static void
691exp_fold_tree_1 (etree_type *tree)
252b5132 692{
252b5132
RH
693 if (tree == NULL)
694 {
e9ee469a
AM
695 memset (&expld.result, 0, sizeof (expld.result));
696 return;
252b5132
RH
697 }
698
4de2d33d 699 switch (tree->type.node_class)
252b5132
RH
700 {
701 case etree_value:
e9ee469a 702 new_rel (tree->value.value, tree->value.str, expld.section);
252b5132
RH
703 break;
704
705 case etree_rel:
e9ee469a
AM
706 if (expld.phase != lang_first_phase_enum)
707 {
708 asection *output_section = tree->rel.section->output_section;
709 new_rel (tree->rel.value + tree->rel.section->output_offset,
710 NULL, output_section);
711 }
252b5132 712 else
e9ee469a 713 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
714 break;
715
716 case etree_assert:
e9ee469a 717 exp_fold_tree_1 (tree->assert_s.child);
5397b1fe
AM
718 if (expld.phase == lang_final_phase_enum && !expld.result.value)
719 einfo ("%X%P: %s\n", tree->assert_s.message);
252b5132
RH
720 break;
721
722 case etree_unary:
e9ee469a 723 fold_unary (tree);
252b5132
RH
724 break;
725
726 case etree_binary:
e9ee469a 727 fold_binary (tree);
252b5132 728 break;
0ae1cf52
AM
729
730 case etree_trinary:
e9ee469a 731 fold_trinary (tree);
0ae1cf52 732 break;
252b5132
RH
733
734 case etree_assign:
735 case etree_provide:
b46a87b1 736 case etree_provided:
252b5132
RH
737 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
738 {
c7d701b0 739 /* Assignment to dot can only be done during allocation. */
b46a87b1 740 if (tree->type.node_class != etree_assign)
252b5132 741 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
e9ee469a
AM
742 if (expld.phase == lang_mark_phase_enum
743 || expld.phase == lang_allocating_phase_enum
744 || (expld.phase == lang_final_phase_enum
745 && expld.section == bfd_abs_section_ptr))
252b5132 746 {
fbbb9ac5 747 /* Notify the folder that this is an assignment to dot. */
e9ee469a
AM
748 expld.assigning_to_dot = TRUE;
749 exp_fold_tree_1 (tree->assign.src);
750 expld.assigning_to_dot = FALSE;
751
752 if (!expld.result.valid_p)
753 {
754 if (expld.phase != lang_mark_phase_enum)
755 einfo (_("%F%S invalid assignment to location counter\n"));
756 }
757 else if (expld.dotp == NULL)
758 einfo (_("%F%S assignment to location counter"
759 " invalid outside of SECTION\n"));
252b5132
RH
760 else
761 {
e9ee469a
AM
762 bfd_vma nextdot;
763
764 nextdot = expld.result.value + expld.section->vma;
765 if (nextdot < expld.dot
766 && expld.section != bfd_abs_section_ptr)
767 einfo (_("%F%S cannot move location counter backwards"
768 " (from %V to %V)\n"), expld.dot, nextdot);
252b5132
RH
769 else
770 {
e9ee469a
AM
771 expld.dot = nextdot;
772 *expld.dotp = nextdot;
252b5132
RH
773 }
774 }
775 }
8b3d8fa8 776 else
e9ee469a 777 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
778 }
779 else
780 {
e9ee469a 781 struct bfd_link_hash_entry *h = NULL;
252b5132 782
e9ee469a
AM
783 if (tree->type.node_class == etree_provide)
784 {
252b5132 785 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
e9ee469a
AM
786 FALSE, FALSE, TRUE);
787 if (h == NULL
788 || (h->type != bfd_link_hash_new
789 && h->type != bfd_link_hash_undefined
790 && h->type != bfd_link_hash_common))
791 {
792 /* Do nothing. The symbol was never referenced, or was
793 defined by some object. */
794 break;
795 }
796 }
797
798 exp_fold_tree_1 (tree->assign.src);
799 if (expld.result.valid_p)
800 {
1579bae1 801 if (h == NULL)
67010b46 802 {
e9ee469a
AM
803 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
804 TRUE, FALSE, TRUE);
805 if (h == NULL)
67010b46
NC
806 einfo (_("%P%F:%s: hash creation failed\n"),
807 tree->assign.dst);
808 }
e9ee469a
AM
809
810 /* FIXME: Should we worry if the symbol is already
811 defined? */
812 lang_update_definedness (tree->assign.dst, h);
813 h->type = bfd_link_hash_defined;
814 h->u.def.value = expld.result.value;
815 h->u.def.section = expld.result.section;
816 if (tree->type.node_class == etree_provide)
817 tree->type.node_class = etree_provided;
1338dd10
PB
818
819 /* Copy the symbol type if this is a simple assignment of
820 one symbol to annother. */
821 if (tree->assign.src->type.node_class == etree_name)
822 {
823 struct bfd_link_hash_entry *hsrc;
824
825 hsrc = bfd_link_hash_lookup (link_info.hash,
826 tree->assign.src->name.name,
827 FALSE, FALSE, TRUE);
828 if (hsrc)
829 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
830 hsrc);
831 }
252b5132 832 }
e092cb30
AM
833 else if (expld.phase == lang_final_phase_enum)
834 {
835 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
836 FALSE, FALSE, TRUE);
837 if (h != NULL
838 && h->type == bfd_link_hash_new)
839 h->type = bfd_link_hash_undefined;
840 }
252b5132
RH
841 }
842 break;
843
844 case etree_name:
e9ee469a 845 fold_name (tree);
252b5132
RH
846 break;
847
848 default:
849 FAIL ();
e9ee469a 850 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
851 break;
852 }
252b5132
RH
853}
854
e9ee469a
AM
855void
856exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
75ff4589 857{
e9ee469a
AM
858 expld.dot = *dotp;
859 expld.dotp = dotp;
860 expld.section = current_section;
861 exp_fold_tree_1 (tree);
75ff4589
L
862}
863
e9ee469a
AM
864static void
865exp_fold_tree_no_dot (etree_type *tree)
252b5132 866{
e9ee469a
AM
867 expld.dot = 0;
868 expld.dotp = NULL;
869 expld.section = bfd_abs_section_ptr;
870 exp_fold_tree_1 (tree);
252b5132
RH
871}
872
873etree_type *
1579bae1 874exp_binop (int code, etree_type *lhs, etree_type *rhs)
252b5132 875{
d3ce72d0 876 etree_type value, *new_e;
252b5132
RH
877
878 value.type.node_code = code;
f856040a 879 value.type.lineno = lhs->type.lineno;
252b5132
RH
880 value.binary.lhs = lhs;
881 value.binary.rhs = rhs;
882 value.type.node_class = etree_binary;
e9ee469a
AM
883 exp_fold_tree_no_dot (&value);
884 if (expld.result.valid_p)
885 return exp_intop (expld.result.value);
886
d3ce72d0
NC
887 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
888 memcpy (new_e, &value, sizeof (new_e->binary));
889 return new_e;
252b5132
RH
890}
891
892etree_type *
1579bae1 893exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
252b5132 894{
d3ce72d0 895 etree_type value, *new_e;
e9ee469a 896
252b5132 897 value.type.node_code = code;
f856040a 898 value.type.lineno = lhs->type.lineno;
252b5132
RH
899 value.trinary.lhs = lhs;
900 value.trinary.cond = cond;
901 value.trinary.rhs = rhs;
902 value.type.node_class = etree_trinary;
e9ee469a
AM
903 exp_fold_tree_no_dot (&value);
904 if (expld.result.valid_p)
905 return exp_intop (expld.result.value);
c7d701b0 906
d3ce72d0
NC
907 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
908 memcpy (new_e, &value, sizeof (new_e->trinary));
909 return new_e;
252b5132
RH
910}
911
252b5132 912etree_type *
1579bae1 913exp_unop (int code, etree_type *child)
252b5132 914{
d3ce72d0 915 etree_type value, *new_e;
252b5132 916
252b5132 917 value.unary.type.node_code = code;
f856040a 918 value.unary.type.lineno = child->type.lineno;
252b5132
RH
919 value.unary.child = child;
920 value.unary.type.node_class = etree_unary;
e9ee469a
AM
921 exp_fold_tree_no_dot (&value);
922 if (expld.result.valid_p)
923 return exp_intop (expld.result.value);
c7d701b0 924
d3ce72d0
NC
925 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
926 memcpy (new_e, &value, sizeof (new_e->unary));
927 return new_e;
252b5132
RH
928}
929
252b5132 930etree_type *
1579bae1 931exp_nameop (int code, const char *name)
252b5132 932{
d3ce72d0 933 etree_type value, *new_e;
e9ee469a 934
252b5132 935 value.name.type.node_code = code;
f856040a 936 value.name.type.lineno = lineno;
252b5132
RH
937 value.name.name = name;
938 value.name.type.node_class = etree_name;
939
e9ee469a
AM
940 exp_fold_tree_no_dot (&value);
941 if (expld.result.valid_p)
942 return exp_intop (expld.result.value);
c7d701b0 943
d3ce72d0
NC
944 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
945 memcpy (new_e, &value, sizeof (new_e->name));
946 return new_e;
252b5132
RH
947
948}
949
252b5132 950etree_type *
1579bae1 951exp_assop (int code, const char *dst, etree_type *src)
252b5132 952{
d3ce72d0
NC
953 etree_type *new_e;
954
955 new_e = (etree_type *) stat_alloc (sizeof (new_e->assign));
956 new_e->type.node_code = code;
957 new_e->type.lineno = src->type.lineno;
958 new_e->type.node_class = etree_assign;
959 new_e->assign.src = src;
960 new_e->assign.dst = dst;
961 return new_e;
252b5132
RH
962}
963
964/* Handle PROVIDE. */
965
966etree_type *
7af8e998 967exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
252b5132
RH
968{
969 etree_type *n;
970
1e9cc1c2 971 n = (etree_type *) stat_alloc (sizeof (n->assign));
252b5132 972 n->assign.type.node_code = '=';
f856040a 973 n->assign.type.lineno = src->type.lineno;
252b5132
RH
974 n->assign.type.node_class = etree_provide;
975 n->assign.src = src;
976 n->assign.dst = dst;
7af8e998 977 n->assign.hidden = hidden;
252b5132
RH
978 return n;
979}
980
981/* Handle ASSERT. */
982
983etree_type *
1579bae1 984exp_assert (etree_type *exp, const char *message)
252b5132
RH
985{
986 etree_type *n;
987
1e9cc1c2 988 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
252b5132 989 n->assert_s.type.node_code = '!';
f856040a 990 n->assert_s.type.lineno = exp->type.lineno;
252b5132
RH
991 n->assert_s.type.node_class = etree_assert;
992 n->assert_s.child = exp;
993 n->assert_s.message = message;
994 return n;
995}
996
4de2d33d 997void
1579bae1 998exp_print_tree (etree_type *tree)
252b5132 999{
c7d701b0
NC
1000 if (config.map_file == NULL)
1001 config.map_file = stderr;
b7a26f91 1002
c7d701b0
NC
1003 if (tree == NULL)
1004 {
1005 minfo ("NULL TREE\n");
1006 return;
1007 }
b7a26f91 1008
8c95a62e
KH
1009 switch (tree->type.node_class)
1010 {
1011 case etree_value:
1012 minfo ("0x%v", tree->value.value);
1013 return;
1014 case etree_rel:
1015 if (tree->rel.section->owner != NULL)
1016 minfo ("%B:", tree->rel.section->owner);
1017 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1018 return;
1019 case etree_assign:
8c95a62e 1020 fprintf (config.map_file, "%s", tree->assign.dst);
b34976b6 1021 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1022 exp_print_tree (tree->assign.src);
1023 break;
1024 case etree_provide:
b46a87b1 1025 case etree_provided:
8c95a62e
KH
1026 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1027 exp_print_tree (tree->assign.src);
1028 fprintf (config.map_file, ")");
1029 break;
1030 case etree_binary:
1031 fprintf (config.map_file, "(");
1032 exp_print_tree (tree->binary.lhs);
b34976b6 1033 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1034 exp_print_tree (tree->binary.rhs);
1035 fprintf (config.map_file, ")");
1036 break;
1037 case etree_trinary:
1038 exp_print_tree (tree->trinary.cond);
1039 fprintf (config.map_file, "?");
1040 exp_print_tree (tree->trinary.lhs);
1041 fprintf (config.map_file, ":");
1042 exp_print_tree (tree->trinary.rhs);
1043 break;
1044 case etree_unary:
b34976b6 1045 exp_print_token (tree->unary.type.node_code, FALSE);
8c95a62e
KH
1046 if (tree->unary.child)
1047 {
7b17f854 1048 fprintf (config.map_file, " (");
8c95a62e
KH
1049 exp_print_tree (tree->unary.child);
1050 fprintf (config.map_file, ")");
1051 }
1052 break;
1053
1054 case etree_assert:
1055 fprintf (config.map_file, "ASSERT (");
1056 exp_print_tree (tree->assert_s.child);
1057 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1058 break;
1059
8c95a62e
KH
1060 case etree_name:
1061 if (tree->type.node_code == NAME)
1062 {
1063 fprintf (config.map_file, "%s", tree->name.name);
1064 }
1065 else
1066 {
b34976b6 1067 exp_print_token (tree->type.node_code, FALSE);
8c95a62e 1068 if (tree->name.name)
7b17f854 1069 fprintf (config.map_file, " (%s)", tree->name.name);
8c95a62e
KH
1070 }
1071 break;
1072 default:
1073 FAIL ();
1074 break;
252b5132 1075 }
252b5132
RH
1076}
1077
1078bfd_vma
e9ee469a 1079exp_get_vma (etree_type *tree, bfd_vma def, char *name)
252b5132 1080{
252b5132
RH
1081 if (tree != NULL)
1082 {
e9ee469a
AM
1083 exp_fold_tree_no_dot (tree);
1084 if (expld.result.valid_p)
1085 return expld.result.value;
1086 else if (name != NULL && expld.phase != lang_mark_phase_enum)
c58dea77 1087 einfo (_("%F%S: nonconstant expression for %s\n"), name);
252b5132 1088 }
e9ee469a 1089 return def;
252b5132
RH
1090}
1091
4de2d33d 1092int
e9ee469a 1093exp_get_value_int (etree_type *tree, int def, char *name)
252b5132 1094{
e9ee469a 1095 return exp_get_vma (tree, def, name);
252b5132
RH
1096}
1097
2c382fb6 1098fill_type *
e9ee469a 1099exp_get_fill (etree_type *tree, fill_type *def, char *name)
2c382fb6
AM
1100{
1101 fill_type *fill;
2c382fb6
AM
1102 size_t len;
1103 unsigned int val;
1104
1105 if (tree == NULL)
1106 return def;
1107
e9ee469a
AM
1108 exp_fold_tree_no_dot (tree);
1109 if (!expld.result.valid_p)
1110 {
1111 if (name != NULL && expld.phase != lang_mark_phase_enum)
c58dea77 1112 einfo (_("%F%S: nonconstant expression for %s\n"), name);
e9ee469a
AM
1113 return def;
1114 }
2c382fb6 1115
e9ee469a 1116 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
2c382fb6
AM
1117 {
1118 unsigned char *dst;
1119 unsigned char *s;
1e9cc1c2 1120 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
2c382fb6
AM
1121 fill->size = (len + 1) / 2;
1122 dst = fill->data;
e9ee469a 1123 s = (unsigned char *) expld.result.str;
2c382fb6
AM
1124 val = 0;
1125 do
1126 {
1127 unsigned int digit;
1128
1129 digit = *s++ - '0';
1130 if (digit > 9)
1131 digit = (digit - 'A' + '0' + 10) & 0xf;
1132 val <<= 4;
1133 val += digit;
1134 --len;
1135 if ((len & 1) == 0)
1136 {
1137 *dst++ = val;
1138 val = 0;
1139 }
1140 }
1141 while (len != 0);
1142 }
1143 else
1144 {
1e9cc1c2 1145 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
e9ee469a 1146 val = expld.result.value;
2c382fb6
AM
1147 fill->data[0] = (val >> 24) & 0xff;
1148 fill->data[1] = (val >> 16) & 0xff;
1149 fill->data[2] = (val >> 8) & 0xff;
1150 fill->data[3] = (val >> 0) & 0xff;
1151 fill->size = 4;
1152 }
1153 return fill;
1154}
1155
252b5132 1156bfd_vma
e9ee469a 1157exp_get_abs_int (etree_type *tree, int def, char *name)
252b5132 1158{
e9ee469a
AM
1159 if (tree != NULL)
1160 {
1161 exp_fold_tree_no_dot (tree);
c7d701b0 1162
e9ee469a
AM
1163 if (expld.result.valid_p)
1164 {
1165 expld.result.value += expld.result.section->vma;
1166 return expld.result.value;
1167 }
1168 else if (name != NULL && expld.phase != lang_mark_phase_enum)
f856040a
L
1169 {
1170 lineno = tree->type.lineno;
1171 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1172 }
e9ee469a
AM
1173 }
1174 return def;
252b5132 1175}
c553bb91 1176
e5caa5e0
AM
1177static bfd_vma
1178align_n (bfd_vma value, bfd_vma align)
c553bb91
AM
1179{
1180 if (align <= 1)
1181 return value;
1182
1183 value = (value + align - 1) / align;
1184 return value * align;
1185}