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