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