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