| 1 | /* This module handles expression trees. |
| 2 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999 |
| 3 | Free Software Foundation, Inc. |
| 4 | Written by Steve Chamberlain of Cygnus Support (sac@cygnus.com). |
| 5 | |
| 6 | This file is part of GLD, the Gnu Linker. |
| 7 | |
| 8 | GLD is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2, or (at your option) |
| 11 | any later version. |
| 12 | |
| 13 | GLD is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with GLD; see the file COPYING. If not, write to the Free |
| 20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 21 | 02111-1307, USA. */ |
| 22 | |
| 23 | /* |
| 24 | This module is in charge of working out the contents of expressions. |
| 25 | |
| 26 | It has to keep track of the relative/absness of a symbol etc. This is |
| 27 | done by keeping all values in a struct (an etree_value_type) which |
| 28 | contains a value, a section to which it is relative and a valid bit. |
| 29 | |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "bfd.h" |
| 34 | #include "sysdep.h" |
| 35 | #include "bfdlink.h" |
| 36 | |
| 37 | #include "ld.h" |
| 38 | #include "ldmain.h" |
| 39 | #include "ldmisc.h" |
| 40 | #include "ldexp.h" |
| 41 | #include "ldgram.h" |
| 42 | #include "ldlang.h" |
| 43 | |
| 44 | static void exp_print_token PARAMS ((token_code_type code)); |
| 45 | static void make_abs PARAMS ((etree_value_type *ptr)); |
| 46 | static etree_value_type new_abs PARAMS ((bfd_vma value)); |
| 47 | static void check PARAMS ((lang_output_section_statement_type *os, |
| 48 | const char *name, const char *op)); |
| 49 | static etree_value_type new_rel |
| 50 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); |
| 51 | static etree_value_type new_rel_from_section |
| 52 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); |
| 53 | static etree_value_type fold_binary |
| 54 | PARAMS ((etree_type *tree, |
| 55 | lang_output_section_statement_type *current_section, |
| 56 | lang_phase_type allocation_done, |
| 57 | bfd_vma dot, bfd_vma *dotp)); |
| 58 | static etree_value_type fold_name |
| 59 | PARAMS ((etree_type *tree, |
| 60 | lang_output_section_statement_type *current_section, |
| 61 | lang_phase_type allocation_done, |
| 62 | bfd_vma dot)); |
| 63 | static etree_value_type exp_fold_tree_no_dot |
| 64 | PARAMS ((etree_type *tree, |
| 65 | lang_output_section_statement_type *current_section, |
| 66 | lang_phase_type allocation_done)); |
| 67 | |
| 68 | static void |
| 69 | exp_print_token (code) |
| 70 | token_code_type code; |
| 71 | { |
| 72 | static CONST struct |
| 73 | { |
| 74 | token_code_type code; |
| 75 | char *name; |
| 76 | } table[] = |
| 77 | { |
| 78 | { INT, "int" }, |
| 79 | { REL, "relocateable" }, |
| 80 | { NAME,"NAME" }, |
| 81 | { PLUSEQ,"+=" }, |
| 82 | { MINUSEQ,"-=" }, |
| 83 | { MULTEQ,"*=" }, |
| 84 | { DIVEQ,"/=" }, |
| 85 | { LSHIFTEQ,"<<=" }, |
| 86 | { RSHIFTEQ,">>=" }, |
| 87 | { ANDEQ,"&=" }, |
| 88 | { OREQ,"|=" }, |
| 89 | { OROR,"||" }, |
| 90 | { ANDAND,"&&" }, |
| 91 | { EQ,"==" }, |
| 92 | { NE,"!=" }, |
| 93 | { LE,"<=" }, |
| 94 | { GE,">=" }, |
| 95 | { LSHIFT,"<<" }, |
| 96 | { RSHIFT,">>=" }, |
| 97 | { ALIGN_K,"ALIGN" }, |
| 98 | { BLOCK,"BLOCK" }, |
| 99 | { SECTIONS,"SECTIONS" }, |
| 100 | { SIZEOF_HEADERS,"SIZEOF_HEADERS" }, |
| 101 | { NEXT,"NEXT" }, |
| 102 | { SIZEOF,"SIZEOF" }, |
| 103 | { ADDR,"ADDR" }, |
| 104 | { LOADADDR,"LOADADDR" }, |
| 105 | { MEMORY,"MEMORY" }, |
| 106 | { DEFINED,"DEFINED" }, |
| 107 | { TARGET_K,"TARGET" }, |
| 108 | { SEARCH_DIR,"SEARCH_DIR" }, |
| 109 | { MAP,"MAP" }, |
| 110 | { QUAD,"QUAD" }, |
| 111 | { SQUAD,"SQUAD" }, |
| 112 | { LONG,"LONG" }, |
| 113 | { SHORT,"SHORT" }, |
| 114 | { BYTE,"BYTE" }, |
| 115 | { ENTRY,"ENTRY" }, |
| 116 | { 0,(char *)NULL } |
| 117 | }; |
| 118 | unsigned int idx; |
| 119 | |
| 120 | for (idx = 0; table[idx].name != (char*)NULL; idx++) { |
| 121 | if (table[idx].code == code) { |
| 122 | fprintf(config.map_file, "%s", table[idx].name); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | /* Not in table, just print it alone */ |
| 127 | fprintf(config.map_file, "%c",code); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | make_abs (ptr) |
| 132 | etree_value_type *ptr; |
| 133 | { |
| 134 | asection *s = ptr->section->bfd_section; |
| 135 | ptr->value += s->vma; |
| 136 | ptr->section = abs_output_section; |
| 137 | } |
| 138 | |
| 139 | static etree_value_type |
| 140 | new_abs (value) |
| 141 | bfd_vma value; |
| 142 | { |
| 143 | etree_value_type new; |
| 144 | new.valid_p = true; |
| 145 | new.section = abs_output_section; |
| 146 | new.value = value; |
| 147 | return new; |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | check (os, name, op) |
| 152 | lang_output_section_statement_type *os; |
| 153 | const char *name; |
| 154 | const char *op; |
| 155 | { |
| 156 | if (os == NULL) |
| 157 | einfo (_("%F%P: %s uses undefined section %s\n"), op, name); |
| 158 | if (! os->processed) |
| 159 | einfo (_("%F%P: %s forward reference of section %s\n"), op, name); |
| 160 | } |
| 161 | |
| 162 | etree_type * |
| 163 | exp_intop (value) |
| 164 | bfd_vma value; |
| 165 | { |
| 166 | etree_type *new = (etree_type *) stat_alloc(sizeof(new->value)); |
| 167 | new->type.node_code = INT; |
| 168 | new->value.value = value; |
| 169 | new->type.node_class = etree_value; |
| 170 | return new; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /* Build an expression representing an unnamed relocateable value. */ |
| 175 | |
| 176 | etree_type * |
| 177 | exp_relop (section, value) |
| 178 | asection *section; |
| 179 | bfd_vma value; |
| 180 | { |
| 181 | etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel)); |
| 182 | new->type.node_code = REL; |
| 183 | new->type.node_class = etree_rel; |
| 184 | new->rel.section = section; |
| 185 | new->rel.value = value; |
| 186 | return new; |
| 187 | } |
| 188 | |
| 189 | static etree_value_type |
| 190 | new_rel (value, section) |
| 191 | bfd_vma value; |
| 192 | lang_output_section_statement_type *section; |
| 193 | { |
| 194 | etree_value_type new; |
| 195 | new.valid_p = true; |
| 196 | new.value = value; |
| 197 | new.section = section; |
| 198 | return new; |
| 199 | } |
| 200 | |
| 201 | static etree_value_type |
| 202 | new_rel_from_section (value, section) |
| 203 | bfd_vma value; |
| 204 | lang_output_section_statement_type *section; |
| 205 | { |
| 206 | etree_value_type new; |
| 207 | new.valid_p = true; |
| 208 | new.value = value; |
| 209 | new.section = section; |
| 210 | |
| 211 | new.value -= section->bfd_section->vma; |
| 212 | |
| 213 | return new; |
| 214 | } |
| 215 | |
| 216 | static etree_value_type |
| 217 | fold_binary (tree, current_section, allocation_done, dot, dotp) |
| 218 | etree_type *tree; |
| 219 | lang_output_section_statement_type *current_section; |
| 220 | lang_phase_type allocation_done; |
| 221 | bfd_vma dot; |
| 222 | bfd_vma *dotp; |
| 223 | { |
| 224 | etree_value_type result; |
| 225 | |
| 226 | result = exp_fold_tree (tree->binary.lhs, current_section, |
| 227 | allocation_done, dot, dotp); |
| 228 | if (result.valid_p) |
| 229 | { |
| 230 | etree_value_type other; |
| 231 | |
| 232 | other = exp_fold_tree (tree->binary.rhs, |
| 233 | current_section, |
| 234 | allocation_done, dot,dotp) ; |
| 235 | if (other.valid_p) |
| 236 | { |
| 237 | /* If the values are from different sections, or this is an |
| 238 | absolute expression, make both the source arguments |
| 239 | absolute. However, adding or subtracting an absolute |
| 240 | value from a relative value is meaningful, and is an |
| 241 | exception. */ |
| 242 | if (current_section != abs_output_section |
| 243 | && (other.section == abs_output_section |
| 244 | || (result.section == abs_output_section |
| 245 | && tree->type.node_code == '+')) |
| 246 | && (tree->type.node_code == '+' |
| 247 | || tree->type.node_code == '-')) |
| 248 | { |
| 249 | etree_value_type hold; |
| 250 | |
| 251 | /* If there is only one absolute term, make sure it is the |
| 252 | second one. */ |
| 253 | if (other.section != abs_output_section) |
| 254 | { |
| 255 | hold = result; |
| 256 | result = other; |
| 257 | other = hold; |
| 258 | } |
| 259 | } |
| 260 | else if (result.section != other.section |
| 261 | || current_section == abs_output_section) |
| 262 | { |
| 263 | make_abs(&result); |
| 264 | make_abs(&other); |
| 265 | } |
| 266 | |
| 267 | switch (tree->type.node_code) |
| 268 | { |
| 269 | case '%': |
| 270 | if (other.value == 0) |
| 271 | einfo (_("%F%S %% by zero\n")); |
| 272 | result.value = ((bfd_signed_vma) result.value |
| 273 | % (bfd_signed_vma) other.value); |
| 274 | break; |
| 275 | |
| 276 | case '/': |
| 277 | if (other.value == 0) |
| 278 | einfo (_("%F%S / by zero\n")); |
| 279 | result.value = ((bfd_signed_vma) result.value |
| 280 | / (bfd_signed_vma) other.value); |
| 281 | break; |
| 282 | |
| 283 | #define BOP(x,y) case x : result.value = result.value y other.value; break; |
| 284 | BOP('+',+); |
| 285 | BOP('*',*); |
| 286 | BOP('-',-); |
| 287 | BOP(LSHIFT,<<); |
| 288 | BOP(RSHIFT,>>); |
| 289 | BOP(EQ,==); |
| 290 | BOP(NE,!=); |
| 291 | BOP('<',<); |
| 292 | BOP('>',>); |
| 293 | BOP(LE,<=); |
| 294 | BOP(GE,>=); |
| 295 | BOP('&',&); |
| 296 | BOP('^',^); |
| 297 | BOP('|',|); |
| 298 | BOP(ANDAND,&&); |
| 299 | BOP(OROR,||); |
| 300 | |
| 301 | case MAX_K: |
| 302 | if (result.value < other.value) |
| 303 | result = other; |
| 304 | break; |
| 305 | |
| 306 | case MIN_K: |
| 307 | if (result.value > other.value) |
| 308 | result = other; |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | FAIL(); |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | result.valid_p = false; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return result; |
| 322 | } |
| 323 | |
| 324 | etree_value_type |
| 325 | invalid () |
| 326 | { |
| 327 | etree_value_type new; |
| 328 | new.valid_p = false; |
| 329 | return new; |
| 330 | } |
| 331 | |
| 332 | static etree_value_type |
| 333 | fold_name (tree, current_section, allocation_done, dot) |
| 334 | etree_type *tree; |
| 335 | lang_output_section_statement_type *current_section; |
| 336 | lang_phase_type allocation_done; |
| 337 | bfd_vma dot; |
| 338 | { |
| 339 | etree_value_type result; |
| 340 | switch (tree->type.node_code) |
| 341 | { |
| 342 | case SIZEOF_HEADERS: |
| 343 | if (allocation_done != lang_first_phase_enum) |
| 344 | { |
| 345 | result = new_abs ((bfd_vma) |
| 346 | bfd_sizeof_headers (output_bfd, |
| 347 | link_info.relocateable)); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | result.valid_p = false; |
| 352 | } |
| 353 | break; |
| 354 | case DEFINED: |
| 355 | if (allocation_done == lang_first_phase_enum) |
| 356 | result.valid_p = false; |
| 357 | else |
| 358 | { |
| 359 | struct bfd_link_hash_entry *h; |
| 360 | |
| 361 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, |
| 362 | tree->name.name, |
| 363 | false, false, true); |
| 364 | result.value = (h != (struct bfd_link_hash_entry *) NULL |
| 365 | && (h->type == bfd_link_hash_defined |
| 366 | || h->type == bfd_link_hash_defweak |
| 367 | || h->type == bfd_link_hash_common)); |
| 368 | result.section = 0; |
| 369 | result.valid_p = true; |
| 370 | } |
| 371 | break; |
| 372 | case NAME: |
| 373 | result.valid_p = false; |
| 374 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) |
| 375 | { |
| 376 | if (allocation_done != lang_first_phase_enum) |
| 377 | result = new_rel_from_section(dot, current_section); |
| 378 | else |
| 379 | result = invalid(); |
| 380 | } |
| 381 | else if (allocation_done != lang_first_phase_enum) |
| 382 | { |
| 383 | struct bfd_link_hash_entry *h; |
| 384 | |
| 385 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, |
| 386 | tree->name.name, |
| 387 | false, false, true); |
| 388 | if (h != NULL |
| 389 | && (h->type == bfd_link_hash_defined |
| 390 | || h->type == bfd_link_hash_defweak)) |
| 391 | { |
| 392 | if (bfd_is_abs_section (h->u.def.section)) |
| 393 | result = new_abs (h->u.def.value); |
| 394 | else if (allocation_done == lang_final_phase_enum |
| 395 | || allocation_done == lang_allocating_phase_enum) |
| 396 | { |
| 397 | asection *output_section; |
| 398 | |
| 399 | output_section = h->u.def.section->output_section; |
| 400 | if (output_section == NULL) |
| 401 | einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"), |
| 402 | tree->name.name); |
| 403 | else |
| 404 | { |
| 405 | lang_output_section_statement_type *os; |
| 406 | |
| 407 | os = (lang_output_section_statement_lookup |
| 408 | (bfd_get_section_name (output_bfd, |
| 409 | output_section))); |
| 410 | |
| 411 | /* FIXME: Is this correct if this section is |
| 412 | being linked with -R? */ |
| 413 | result = new_rel ((h->u.def.value |
| 414 | + h->u.def.section->output_offset), |
| 415 | os); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | else if (allocation_done == lang_final_phase_enum) |
| 420 | einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"), |
| 421 | tree->name.name); |
| 422 | } |
| 423 | break; |
| 424 | |
| 425 | case ADDR: |
| 426 | if (allocation_done != lang_first_phase_enum) |
| 427 | { |
| 428 | lang_output_section_statement_type *os; |
| 429 | |
| 430 | os = lang_output_section_find (tree->name.name); |
| 431 | check (os, tree->name.name, "ADDR"); |
| 432 | result = new_rel (0, os); |
| 433 | } |
| 434 | else |
| 435 | result = invalid (); |
| 436 | break; |
| 437 | |
| 438 | case LOADADDR: |
| 439 | if (allocation_done != lang_first_phase_enum) |
| 440 | { |
| 441 | lang_output_section_statement_type *os; |
| 442 | |
| 443 | os = lang_output_section_find (tree->name.name); |
| 444 | check (os, tree->name.name, "LOADADDR"); |
| 445 | if (os->load_base == NULL) |
| 446 | result = new_rel (0, os); |
| 447 | else |
| 448 | result = exp_fold_tree_no_dot (os->load_base, |
| 449 | abs_output_section, |
| 450 | allocation_done); |
| 451 | } |
| 452 | else |
| 453 | result = invalid (); |
| 454 | break; |
| 455 | |
| 456 | case SIZEOF: |
| 457 | if (allocation_done != lang_first_phase_enum) |
| 458 | { |
| 459 | lang_output_section_statement_type *os; |
| 460 | |
| 461 | os = lang_output_section_find (tree->name.name); |
| 462 | check (os, tree->name.name, "SIZEOF"); |
| 463 | result = new_abs (os->bfd_section->_raw_size); |
| 464 | } |
| 465 | else |
| 466 | result = invalid (); |
| 467 | break; |
| 468 | |
| 469 | default: |
| 470 | FAIL(); |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return result; |
| 475 | } |
| 476 | etree_value_type |
| 477 | exp_fold_tree (tree, current_section, allocation_done, dot, dotp) |
| 478 | etree_type *tree; |
| 479 | lang_output_section_statement_type *current_section; |
| 480 | lang_phase_type allocation_done; |
| 481 | bfd_vma dot; |
| 482 | bfd_vma *dotp; |
| 483 | { |
| 484 | etree_value_type result; |
| 485 | |
| 486 | if (tree == NULL) |
| 487 | { |
| 488 | result.valid_p = false; |
| 489 | return result; |
| 490 | } |
| 491 | |
| 492 | switch (tree->type.node_class) |
| 493 | { |
| 494 | case etree_value: |
| 495 | result = new_rel (tree->value.value, current_section); |
| 496 | break; |
| 497 | |
| 498 | case etree_rel: |
| 499 | if (allocation_done != lang_final_phase_enum) |
| 500 | result.valid_p = false; |
| 501 | else |
| 502 | result = new_rel ((tree->rel.value |
| 503 | + tree->rel.section->output_section->vma |
| 504 | + tree->rel.section->output_offset), |
| 505 | current_section); |
| 506 | break; |
| 507 | |
| 508 | case etree_assert: |
| 509 | result = exp_fold_tree (tree->assert_s.child, |
| 510 | current_section, |
| 511 | allocation_done, dot, dotp); |
| 512 | if (result.valid_p) |
| 513 | { |
| 514 | if (! result.value) |
| 515 | einfo ("%F%P: %s\n", tree->assert_s.message); |
| 516 | return result; |
| 517 | } |
| 518 | break; |
| 519 | |
| 520 | case etree_unary: |
| 521 | result = exp_fold_tree (tree->unary.child, |
| 522 | current_section, |
| 523 | allocation_done, dot, dotp); |
| 524 | if (result.valid_p) |
| 525 | { |
| 526 | switch (tree->type.node_code) |
| 527 | { |
| 528 | case ALIGN_K: |
| 529 | if (allocation_done != lang_first_phase_enum) |
| 530 | result = new_rel_from_section (ALIGN_N (dot, result.value), |
| 531 | current_section); |
| 532 | else |
| 533 | result.valid_p = false; |
| 534 | break; |
| 535 | |
| 536 | case ABSOLUTE: |
| 537 | if (allocation_done != lang_first_phase_enum && result.valid_p) |
| 538 | { |
| 539 | result.value += result.section->bfd_section->vma; |
| 540 | result.section = abs_output_section; |
| 541 | } |
| 542 | else |
| 543 | result.valid_p = false; |
| 544 | break; |
| 545 | |
| 546 | case '~': |
| 547 | make_abs (&result); |
| 548 | result.value = ~result.value; |
| 549 | break; |
| 550 | |
| 551 | case '!': |
| 552 | make_abs (&result); |
| 553 | result.value = !result.value; |
| 554 | break; |
| 555 | |
| 556 | case '-': |
| 557 | make_abs (&result); |
| 558 | result.value = -result.value; |
| 559 | break; |
| 560 | |
| 561 | case NEXT: |
| 562 | /* Return next place aligned to value. */ |
| 563 | if (allocation_done == lang_allocating_phase_enum) |
| 564 | { |
| 565 | make_abs (&result); |
| 566 | result.value = ALIGN_N (dot, result.value); |
| 567 | } |
| 568 | else |
| 569 | result.valid_p = false; |
| 570 | break; |
| 571 | |
| 572 | default: |
| 573 | FAIL (); |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | break; |
| 578 | |
| 579 | case etree_trinary: |
| 580 | result = exp_fold_tree (tree->trinary.cond, current_section, |
| 581 | allocation_done, dot, dotp); |
| 582 | if (result.valid_p) |
| 583 | result = exp_fold_tree ((result.value |
| 584 | ? tree->trinary.lhs |
| 585 | : tree->trinary.rhs), |
| 586 | current_section, |
| 587 | allocation_done, dot, dotp); |
| 588 | break; |
| 589 | |
| 590 | case etree_binary: |
| 591 | result = fold_binary (tree, current_section, allocation_done, |
| 592 | dot, dotp); |
| 593 | break; |
| 594 | |
| 595 | case etree_assign: |
| 596 | case etree_provide: |
| 597 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) |
| 598 | { |
| 599 | /* Assignment to dot can only be done during allocation */ |
| 600 | if (tree->type.node_class == etree_provide) |
| 601 | einfo (_("%F%S can not PROVIDE assignment to location counter\n")); |
| 602 | if (allocation_done == lang_allocating_phase_enum |
| 603 | || (allocation_done == lang_final_phase_enum |
| 604 | && current_section == abs_output_section)) |
| 605 | { |
| 606 | result = exp_fold_tree (tree->assign.src, |
| 607 | current_section, |
| 608 | lang_allocating_phase_enum, dot, |
| 609 | dotp); |
| 610 | if (! result.valid_p) |
| 611 | einfo (_("%F%S invalid assignment to location counter\n")); |
| 612 | else |
| 613 | { |
| 614 | if (current_section == NULL) |
| 615 | einfo (_("%F%S assignment to location counter invalid outside of SECTION\n")); |
| 616 | else |
| 617 | { |
| 618 | bfd_vma nextdot; |
| 619 | |
| 620 | nextdot = (result.value |
| 621 | + current_section->bfd_section->vma); |
| 622 | if (nextdot < dot |
| 623 | && current_section != abs_output_section) |
| 624 | { |
| 625 | einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"), |
| 626 | dot, nextdot); |
| 627 | } |
| 628 | else |
| 629 | *dotp = nextdot; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | result = exp_fold_tree (tree->assign.src, |
| 637 | current_section, allocation_done, |
| 638 | dot, dotp); |
| 639 | if (result.valid_p) |
| 640 | { |
| 641 | boolean create; |
| 642 | struct bfd_link_hash_entry *h; |
| 643 | |
| 644 | if (tree->type.node_class == etree_assign) |
| 645 | create = true; |
| 646 | else |
| 647 | create = false; |
| 648 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, |
| 649 | create, false, false); |
| 650 | if (h == (struct bfd_link_hash_entry *) NULL) |
| 651 | { |
| 652 | if (tree->type.node_class == etree_assign) |
| 653 | einfo (_("%P%F:%s: hash creation failed\n"), |
| 654 | tree->assign.dst); |
| 655 | } |
| 656 | else if (tree->type.node_class == etree_provide |
| 657 | && h->type != bfd_link_hash_undefined |
| 658 | && h->type != bfd_link_hash_common) |
| 659 | { |
| 660 | /* Do nothing. The symbol was defined by some |
| 661 | object. */ |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | /* FIXME: Should we worry if the symbol is already |
| 666 | defined? */ |
| 667 | h->type = bfd_link_hash_defined; |
| 668 | h->u.def.value = result.value; |
| 669 | h->u.def.section = result.section->bfd_section; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | break; |
| 674 | |
| 675 | case etree_name: |
| 676 | result = fold_name (tree, current_section, allocation_done, dot); |
| 677 | break; |
| 678 | |
| 679 | default: |
| 680 | FAIL (); |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | return result; |
| 685 | } |
| 686 | |
| 687 | static etree_value_type |
| 688 | exp_fold_tree_no_dot (tree, current_section, allocation_done) |
| 689 | etree_type *tree; |
| 690 | lang_output_section_statement_type *current_section; |
| 691 | lang_phase_type allocation_done; |
| 692 | { |
| 693 | return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma) |
| 694 | 0, (bfd_vma *)NULL); |
| 695 | } |
| 696 | |
| 697 | etree_type * |
| 698 | exp_binop (code, lhs, rhs) |
| 699 | int code; |
| 700 | etree_type *lhs; |
| 701 | etree_type *rhs; |
| 702 | { |
| 703 | etree_type value, *new; |
| 704 | etree_value_type r; |
| 705 | |
| 706 | value.type.node_code = code; |
| 707 | value.binary.lhs = lhs; |
| 708 | value.binary.rhs = rhs; |
| 709 | value.type.node_class = etree_binary; |
| 710 | r = exp_fold_tree_no_dot(&value, |
| 711 | abs_output_section, |
| 712 | lang_first_phase_enum ); |
| 713 | if (r.valid_p) |
| 714 | { |
| 715 | return exp_intop(r.value); |
| 716 | } |
| 717 | new = (etree_type *) stat_alloc (sizeof (new->binary)); |
| 718 | memcpy((char *)new, (char *)&value, sizeof(new->binary)); |
| 719 | return new; |
| 720 | } |
| 721 | |
| 722 | etree_type * |
| 723 | exp_trinop (code, cond, lhs, rhs) |
| 724 | int code; |
| 725 | etree_type *cond; |
| 726 | etree_type *lhs; |
| 727 | etree_type *rhs; |
| 728 | { |
| 729 | etree_type value, *new; |
| 730 | etree_value_type r; |
| 731 | value.type.node_code = code; |
| 732 | value.trinary.lhs = lhs; |
| 733 | value.trinary.cond = cond; |
| 734 | value.trinary.rhs = rhs; |
| 735 | value.type.node_class = etree_trinary; |
| 736 | r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type |
| 737 | *)NULL,lang_first_phase_enum); |
| 738 | if (r.valid_p) { |
| 739 | return exp_intop(r.value); |
| 740 | } |
| 741 | new = (etree_type *) stat_alloc (sizeof (new->trinary)); |
| 742 | memcpy((char *)new,(char *) &value, sizeof(new->trinary)); |
| 743 | return new; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | etree_type * |
| 748 | exp_unop (code, child) |
| 749 | int code; |
| 750 | etree_type *child; |
| 751 | { |
| 752 | etree_type value, *new; |
| 753 | |
| 754 | etree_value_type r; |
| 755 | value.unary.type.node_code = code; |
| 756 | value.unary.child = child; |
| 757 | value.unary.type.node_class = etree_unary; |
| 758 | r = exp_fold_tree_no_dot(&value,abs_output_section, |
| 759 | lang_first_phase_enum); |
| 760 | if (r.valid_p) { |
| 761 | return exp_intop(r.value); |
| 762 | } |
| 763 | new = (etree_type *) stat_alloc (sizeof (new->unary)); |
| 764 | memcpy((char *)new, (char *)&value, sizeof(new->unary)); |
| 765 | return new; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | etree_type * |
| 770 | exp_nameop (code, name) |
| 771 | int code; |
| 772 | CONST char *name; |
| 773 | { |
| 774 | etree_type value, *new; |
| 775 | etree_value_type r; |
| 776 | value.name.type.node_code = code; |
| 777 | value.name.name = name; |
| 778 | value.name.type.node_class = etree_name; |
| 779 | |
| 780 | |
| 781 | r = exp_fold_tree_no_dot(&value, |
| 782 | (lang_output_section_statement_type *)NULL, |
| 783 | lang_first_phase_enum); |
| 784 | if (r.valid_p) { |
| 785 | return exp_intop(r.value); |
| 786 | } |
| 787 | new = (etree_type *) stat_alloc (sizeof (new->name)); |
| 788 | memcpy((char *)new, (char *)&value, sizeof(new->name)); |
| 789 | return new; |
| 790 | |
| 791 | } |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | etree_type * |
| 797 | exp_assop (code, dst, src) |
| 798 | int code; |
| 799 | CONST char *dst; |
| 800 | etree_type *src; |
| 801 | { |
| 802 | etree_type value, *new; |
| 803 | |
| 804 | value.assign.type.node_code = code; |
| 805 | |
| 806 | |
| 807 | value.assign.src = src; |
| 808 | value.assign.dst = dst; |
| 809 | value.assign.type.node_class = etree_assign; |
| 810 | |
| 811 | #if 0 |
| 812 | if (exp_fold_tree_no_dot(&value, &result)) { |
| 813 | return exp_intop(result); |
| 814 | } |
| 815 | #endif |
| 816 | new = (etree_type*) stat_alloc (sizeof (new->assign)); |
| 817 | memcpy((char *)new, (char *)&value, sizeof(new->assign)); |
| 818 | return new; |
| 819 | } |
| 820 | |
| 821 | /* Handle PROVIDE. */ |
| 822 | |
| 823 | etree_type * |
| 824 | exp_provide (dst, src) |
| 825 | const char *dst; |
| 826 | etree_type *src; |
| 827 | { |
| 828 | etree_type *n; |
| 829 | |
| 830 | n = (etree_type *) stat_alloc (sizeof (n->assign)); |
| 831 | n->assign.type.node_code = '='; |
| 832 | n->assign.type.node_class = etree_provide; |
| 833 | n->assign.src = src; |
| 834 | n->assign.dst = dst; |
| 835 | return n; |
| 836 | } |
| 837 | |
| 838 | /* Handle ASSERT. */ |
| 839 | |
| 840 | etree_type * |
| 841 | exp_assert (exp, message) |
| 842 | etree_type *exp; |
| 843 | const char *message; |
| 844 | { |
| 845 | etree_type *n; |
| 846 | |
| 847 | n = (etree_type *) stat_alloc (sizeof (n->assert_s)); |
| 848 | n->assert_s.type.node_code = '!'; |
| 849 | n->assert_s.type.node_class = etree_assert; |
| 850 | n->assert_s.child = exp; |
| 851 | n->assert_s.message = message; |
| 852 | return n; |
| 853 | } |
| 854 | |
| 855 | void |
| 856 | exp_print_tree (tree) |
| 857 | etree_type *tree; |
| 858 | { |
| 859 | switch (tree->type.node_class) { |
| 860 | case etree_value: |
| 861 | minfo ("0x%v", tree->value.value); |
| 862 | return; |
| 863 | case etree_rel: |
| 864 | if (tree->rel.section->owner != NULL) |
| 865 | minfo ("%B:", tree->rel.section->owner); |
| 866 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); |
| 867 | return; |
| 868 | case etree_assign: |
| 869 | #if 0 |
| 870 | if (tree->assign.dst->sdefs != (asymbol *)NULL){ |
| 871 | fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name, |
| 872 | tree->assign.dst->sdefs->value); |
| 873 | } |
| 874 | else { |
| 875 | fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name); |
| 876 | } |
| 877 | #endif |
| 878 | fprintf(config.map_file,"%s",tree->assign.dst); |
| 879 | exp_print_token(tree->type.node_code); |
| 880 | exp_print_tree(tree->assign.src); |
| 881 | break; |
| 882 | case etree_provide: |
| 883 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); |
| 884 | exp_print_tree (tree->assign.src); |
| 885 | fprintf (config.map_file, ")"); |
| 886 | break; |
| 887 | case etree_binary: |
| 888 | fprintf(config.map_file,"("); |
| 889 | exp_print_tree(tree->binary.lhs); |
| 890 | exp_print_token(tree->type.node_code); |
| 891 | exp_print_tree(tree->binary.rhs); |
| 892 | fprintf(config.map_file,")"); |
| 893 | break; |
| 894 | case etree_trinary: |
| 895 | exp_print_tree(tree->trinary.cond); |
| 896 | fprintf(config.map_file,"?"); |
| 897 | exp_print_tree(tree->trinary.lhs); |
| 898 | fprintf(config.map_file,":"); |
| 899 | exp_print_tree(tree->trinary.rhs); |
| 900 | break; |
| 901 | case etree_unary: |
| 902 | exp_print_token(tree->unary.type.node_code); |
| 903 | if (tree->unary.child) |
| 904 | { |
| 905 | fprintf(config.map_file,"("); |
| 906 | exp_print_tree(tree->unary.child); |
| 907 | fprintf(config.map_file,")"); |
| 908 | } |
| 909 | |
| 910 | break; |
| 911 | |
| 912 | case etree_assert: |
| 913 | fprintf (config.map_file, "ASSERT ("); |
| 914 | exp_print_tree (tree->assert_s.child); |
| 915 | fprintf (config.map_file, ", %s)", tree->assert_s.message); |
| 916 | break; |
| 917 | |
| 918 | case etree_undef: |
| 919 | fprintf(config.map_file,"????????"); |
| 920 | break; |
| 921 | case etree_name: |
| 922 | if (tree->type.node_code == NAME) { |
| 923 | fprintf(config.map_file,"%s", tree->name.name); |
| 924 | } |
| 925 | else { |
| 926 | exp_print_token(tree->type.node_code); |
| 927 | if (tree->name.name) |
| 928 | fprintf(config.map_file,"(%s)", tree->name.name); |
| 929 | } |
| 930 | break; |
| 931 | default: |
| 932 | FAIL(); |
| 933 | break; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | bfd_vma |
| 938 | exp_get_vma (tree, def, name, allocation_done) |
| 939 | etree_type *tree; |
| 940 | bfd_vma def; |
| 941 | char *name; |
| 942 | lang_phase_type allocation_done; |
| 943 | { |
| 944 | etree_value_type r; |
| 945 | |
| 946 | if (tree != NULL) |
| 947 | { |
| 948 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); |
| 949 | if (! r.valid_p && name != NULL) |
| 950 | einfo (_("%F%S nonconstant expression for %s\n"), name); |
| 951 | return r.value; |
| 952 | } |
| 953 | else |
| 954 | return def; |
| 955 | } |
| 956 | |
| 957 | int |
| 958 | exp_get_value_int (tree,def,name, allocation_done) |
| 959 | etree_type *tree; |
| 960 | int def; |
| 961 | char *name; |
| 962 | lang_phase_type allocation_done; |
| 963 | { |
| 964 | return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | bfd_vma |
| 969 | exp_get_abs_int (tree, def, name, allocation_done) |
| 970 | etree_type *tree; |
| 971 | int def ATTRIBUTE_UNUSED; |
| 972 | char *name; |
| 973 | lang_phase_type allocation_done; |
| 974 | { |
| 975 | etree_value_type res; |
| 976 | res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); |
| 977 | |
| 978 | if (res.valid_p) |
| 979 | { |
| 980 | res.value += res.section->bfd_section->vma; |
| 981 | } |
| 982 | else { |
| 983 | einfo (_("%F%S non constant expression for %s\n"),name); |
| 984 | } |
| 985 | return res.value; |
| 986 | } |