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