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