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