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