Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* This module handles expression trees. |
2e53f7d6 | 2 | Copyright 1991-2013 Free Software Foundation, Inc. |
8c95a62e | 3 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. |
252b5132 | 4 | |
f96b4a7b | 5 | This file is part of the GNU Binutils. |
252b5132 | 6 | |
f96b4a7b | 7 | This program is free software; you can redistribute it and/or modify |
3ec57632 | 8 | it under the terms of the GNU General Public License as published by |
f96b4a7b NC |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
252b5132 | 11 | |
f96b4a7b | 12 | This program is distributed in the hope that it will be useful, |
3ec57632 NC |
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. | |
252b5132 | 16 | |
3ec57632 | 17 | You should have received a copy of the GNU General Public License |
f96b4a7b NC |
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 | ||
252b5132 | 22 | |
8c95a62e | 23 | /* This module is in charge of working out the contents of expressions. |
252b5132 | 24 | |
8c95a62e KH |
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. */ | |
252b5132 | 29 | |
252b5132 | 30 | #include "sysdep.h" |
3db64b00 | 31 | #include "bfd.h" |
252b5132 RH |
32 | #include "bfdlink.h" |
33 | ||
34 | #include "ld.h" | |
35 | #include "ldmain.h" | |
36 | #include "ldmisc.h" | |
37 | #include "ldexp.h" | |
f856040a | 38 | #include "ldlex.h" |
df2a7313 | 39 | #include <ldgram.h> |
252b5132 | 40 | #include "ldlang.h" |
c7d701b0 | 41 | #include "libiberty.h" |
2c382fb6 | 42 | #include "safe-ctype.h" |
252b5132 | 43 | |
e9ee469a | 44 | static void exp_fold_tree_1 (etree_type *); |
e9ee469a | 45 | static bfd_vma align_n (bfd_vma, bfd_vma); |
2d20f7bf | 46 | |
ba916c8a MM |
47 | segment_type *segments; |
48 | ||
e9ee469a | 49 | struct ldexp_control expld; |
fbbb9ac5 | 50 | |
7b17f854 | 51 | /* Print the string representation of the given token. Surround it |
b34976b6 | 52 | with spaces if INFIX_P is TRUE. */ |
7b17f854 | 53 | |
252b5132 | 54 | static void |
1579bae1 | 55 | exp_print_token (token_code_type code, int infix_p) |
252b5132 | 56 | { |
4da711b1 | 57 | static const struct |
c7d701b0 | 58 | { |
8c95a62e | 59 | token_code_type code; |
67baf8c4 | 60 | const char * name; |
c7d701b0 NC |
61 | } |
62 | table[] = | |
63 | { | |
8c95a62e | 64 | { INT, "int" }, |
8c95a62e KH |
65 | { NAME, "NAME" }, |
66 | { PLUSEQ, "+=" }, | |
67 | { MINUSEQ, "-=" }, | |
68 | { MULTEQ, "*=" }, | |
69 | { DIVEQ, "/=" }, | |
70 | { LSHIFTEQ, "<<=" }, | |
71 | { RSHIFTEQ, ">>=" }, | |
72 | { ANDEQ, "&=" }, | |
73 | { OREQ, "|=" }, | |
74 | { OROR, "||" }, | |
75 | { ANDAND, "&&" }, | |
76 | { EQ, "==" }, | |
77 | { NE, "!=" }, | |
78 | { LE, "<=" }, | |
79 | { GE, ">=" }, | |
80 | { LSHIFT, "<<" }, | |
7cecdbff | 81 | { RSHIFT, ">>" }, |
2e53f7d6 | 82 | { LOG2CEIL, "LOG2CEIL" }, |
8c95a62e KH |
83 | { ALIGN_K, "ALIGN" }, |
84 | { BLOCK, "BLOCK" }, | |
c7d701b0 NC |
85 | { QUAD, "QUAD" }, |
86 | { SQUAD, "SQUAD" }, | |
87 | { LONG, "LONG" }, | |
88 | { SHORT, "SHORT" }, | |
89 | { BYTE, "BYTE" }, | |
8c95a62e KH |
90 | { SECTIONS, "SECTIONS" }, |
91 | { SIZEOF_HEADERS, "SIZEOF_HEADERS" }, | |
8c95a62e KH |
92 | { MEMORY, "MEMORY" }, |
93 | { DEFINED, "DEFINED" }, | |
94 | { TARGET_K, "TARGET" }, | |
95 | { SEARCH_DIR, "SEARCH_DIR" }, | |
96 | { MAP, "MAP" }, | |
8c95a62e | 97 | { ENTRY, "ENTRY" }, |
c7d701b0 | 98 | { NEXT, "NEXT" }, |
362c1d1a | 99 | { ALIGNOF, "ALIGNOF" }, |
c7d701b0 NC |
100 | { SIZEOF, "SIZEOF" }, |
101 | { ADDR, "ADDR" }, | |
102 | { LOADADDR, "LOADADDR" }, | |
24718e3b | 103 | { CONSTANT, "CONSTANT" }, |
8c0848b5 AM |
104 | { ABSOLUTE, "ABSOLUTE" }, |
105 | { MAX_K, "MAX" }, | |
106 | { MIN_K, "MIN" }, | |
107 | { ASSERT_K, "ASSERT" }, | |
1049f94e | 108 | { REL, "relocatable" }, |
2d20f7bf | 109 | { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" }, |
8c37241b | 110 | { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" }, |
ba916c8a | 111 | { DATA_SEGMENT_END, "DATA_SEGMENT_END" }, |
3ec57632 NC |
112 | { ORIGIN, "ORIGIN" }, |
113 | { LENGTH, "LENGTH" }, | |
ba916c8a | 114 | { SEGMENT_START, "SEGMENT_START" } |
8c95a62e | 115 | }; |
252b5132 RH |
116 | unsigned int idx; |
117 | ||
7b17f854 RS |
118 | for (idx = 0; idx < ARRAY_SIZE (table); idx++) |
119 | if (table[idx].code == code) | |
120 | break; | |
c7d701b0 | 121 | |
7b17f854 RS |
122 | if (infix_p) |
123 | fputc (' ', config.map_file); | |
124 | ||
125 | if (idx < ARRAY_SIZE (table)) | |
126 | fputs (table[idx].name, config.map_file); | |
127 | else if (code < 127) | |
128 | fputc (code, config.map_file); | |
c7d701b0 | 129 | else |
7b17f854 RS |
130 | fprintf (config.map_file, "<code %d>", code); |
131 | ||
132 | if (infix_p) | |
133 | fputc (' ', config.map_file); | |
252b5132 RH |
134 | } |
135 | ||
2e53f7d6 NC |
136 | static void |
137 | make_log2ceil (void) | |
138 | { | |
139 | bfd_vma value = expld.result.value; | |
140 | bfd_vma result = -1; | |
141 | bfd_boolean round_up = FALSE; | |
142 | ||
143 | do | |
144 | { | |
145 | result++; | |
146 | /* If more than one bit is set in the value we will need to round up. */ | |
147 | if ((value > 1) && (value & 1)) | |
148 | round_up = TRUE; | |
149 | } | |
150 | while (value >>= 1); | |
151 | ||
152 | if (round_up) | |
153 | result += 1; | |
154 | expld.result.section = NULL; | |
155 | expld.result.value = result; | |
156 | } | |
157 | ||
4de2d33d | 158 | static void |
e9ee469a | 159 | make_abs (void) |
252b5132 | 160 | { |
7542af2a AM |
161 | if (expld.result.section != NULL) |
162 | expld.result.value += expld.result.section->vma; | |
e9ee469a | 163 | expld.result.section = bfd_abs_section_ptr; |
252b5132 RH |
164 | } |
165 | ||
e9ee469a | 166 | static void |
1579bae1 | 167 | new_abs (bfd_vma value) |
252b5132 | 168 | { |
e9ee469a AM |
169 | expld.result.valid_p = TRUE; |
170 | expld.result.section = bfd_abs_section_ptr; | |
171 | expld.result.value = value; | |
172 | expld.result.str = NULL; | |
252b5132 RH |
173 | } |
174 | ||
252b5132 | 175 | etree_type * |
1579bae1 | 176 | exp_intop (bfd_vma value) |
252b5132 | 177 | { |
d3ce72d0 NC |
178 | etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value)); |
179 | new_e->type.node_code = INT; | |
dab69f68 | 180 | new_e->type.filename = ldlex_filename (); |
d3ce72d0 NC |
181 | new_e->type.lineno = lineno; |
182 | new_e->value.value = value; | |
183 | new_e->value.str = NULL; | |
184 | new_e->type.node_class = etree_value; | |
185 | return new_e; | |
2c382fb6 | 186 | } |
252b5132 | 187 | |
2c382fb6 | 188 | etree_type * |
1579bae1 | 189 | exp_bigintop (bfd_vma value, char *str) |
2c382fb6 | 190 | { |
d3ce72d0 NC |
191 | etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value)); |
192 | new_e->type.node_code = INT; | |
dab69f68 | 193 | new_e->type.filename = ldlex_filename (); |
d3ce72d0 NC |
194 | new_e->type.lineno = lineno; |
195 | new_e->value.value = value; | |
196 | new_e->value.str = str; | |
197 | new_e->type.node_class = etree_value; | |
198 | return new_e; | |
252b5132 RH |
199 | } |
200 | ||
1049f94e | 201 | /* Build an expression representing an unnamed relocatable value. */ |
252b5132 RH |
202 | |
203 | etree_type * | |
1579bae1 | 204 | exp_relop (asection *section, bfd_vma value) |
252b5132 | 205 | { |
d3ce72d0 NC |
206 | etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel)); |
207 | new_e->type.node_code = REL; | |
dab69f68 | 208 | new_e->type.filename = ldlex_filename (); |
d3ce72d0 NC |
209 | new_e->type.lineno = lineno; |
210 | new_e->type.node_class = etree_rel; | |
211 | new_e->rel.section = section; | |
212 | new_e->rel.value = value; | |
213 | return new_e; | |
252b5132 RH |
214 | } |
215 | ||
7542af2a AM |
216 | static void |
217 | new_number (bfd_vma value) | |
218 | { | |
219 | expld.result.valid_p = TRUE; | |
220 | expld.result.value = value; | |
221 | expld.result.str = NULL; | |
222 | expld.result.section = NULL; | |
223 | } | |
224 | ||
e9ee469a | 225 | static void |
5942515f | 226 | new_rel (bfd_vma value, asection *section) |
252b5132 | 227 | { |
e9ee469a AM |
228 | expld.result.valid_p = TRUE; |
229 | expld.result.value = value; | |
5942515f | 230 | expld.result.str = NULL; |
e9ee469a | 231 | expld.result.section = section; |
252b5132 RH |
232 | } |
233 | ||
e9ee469a AM |
234 | static void |
235 | new_rel_from_abs (bfd_vma value) | |
252b5132 | 236 | { |
d2667025 AM |
237 | asection *s = expld.section; |
238 | ||
239 | if (s == bfd_abs_section_ptr && expld.phase == lang_final_phase_enum) | |
240 | s = section_for_dot (); | |
e9ee469a | 241 | expld.result.valid_p = TRUE; |
d2667025 | 242 | expld.result.value = value - s->vma; |
e9ee469a | 243 | expld.result.str = NULL; |
d2667025 | 244 | expld.result.section = s; |
252b5132 RH |
245 | } |
246 | ||
e9ee469a AM |
247 | static void |
248 | fold_unary (etree_type *tree) | |
0ae1cf52 | 249 | { |
e9ee469a AM |
250 | exp_fold_tree_1 (tree->unary.child); |
251 | if (expld.result.valid_p) | |
0ae1cf52 AM |
252 | { |
253 | switch (tree->type.node_code) | |
254 | { | |
255 | case ALIGN_K: | |
e9ee469a | 256 | if (expld.phase != lang_first_phase_enum) |
dea2f0a8 | 257 | new_rel_from_abs (align_n (expld.dot, expld.result.value)); |
0ae1cf52 | 258 | else |
e9ee469a | 259 | expld.result.valid_p = FALSE; |
0ae1cf52 AM |
260 | break; |
261 | ||
262 | case ABSOLUTE: | |
e9ee469a | 263 | make_abs (); |
0ae1cf52 AM |
264 | break; |
265 | ||
2e53f7d6 NC |
266 | case LOG2CEIL: |
267 | make_log2ceil (); | |
268 | break; | |
269 | ||
0ae1cf52 | 270 | case '~': |
e9ee469a | 271 | expld.result.value = ~expld.result.value; |
0ae1cf52 AM |
272 | break; |
273 | ||
274 | case '!': | |
e9ee469a | 275 | expld.result.value = !expld.result.value; |
0ae1cf52 AM |
276 | break; |
277 | ||
278 | case '-': | |
e9ee469a | 279 | expld.result.value = -expld.result.value; |
0ae1cf52 AM |
280 | break; |
281 | ||
282 | case NEXT: | |
283 | /* Return next place aligned to value. */ | |
e9ee469a | 284 | if (expld.phase != lang_first_phase_enum) |
0ae1cf52 | 285 | { |
e9ee469a AM |
286 | make_abs (); |
287 | expld.result.value = align_n (expld.dot, expld.result.value); | |
0ae1cf52 AM |
288 | } |
289 | else | |
e9ee469a | 290 | expld.result.valid_p = FALSE; |
0ae1cf52 AM |
291 | break; |
292 | ||
293 | case DATA_SEGMENT_END: | |
ea7c2e6c AM |
294 | if (expld.phase == lang_first_phase_enum |
295 | || expld.section != bfd_abs_section_ptr) | |
0ae1cf52 | 296 | { |
ea7c2e6c AM |
297 | expld.result.valid_p = FALSE; |
298 | } | |
299 | else if (expld.dataseg.phase == exp_dataseg_align_seen | |
300 | || expld.dataseg.phase == exp_dataseg_relro_seen) | |
301 | { | |
302 | expld.dataseg.phase = exp_dataseg_end_seen; | |
303 | expld.dataseg.end = expld.result.value; | |
304 | } | |
305 | else if (expld.dataseg.phase == exp_dataseg_done | |
306 | || expld.dataseg.phase == exp_dataseg_adjust | |
307 | || expld.dataseg.phase == exp_dataseg_relro_adjust) | |
308 | { | |
309 | /* OK. */ | |
0ae1cf52 AM |
310 | } |
311 | else | |
e9ee469a | 312 | expld.result.valid_p = FALSE; |
0ae1cf52 AM |
313 | break; |
314 | ||
315 | default: | |
316 | FAIL (); | |
317 | break; | |
318 | } | |
319 | } | |
0ae1cf52 AM |
320 | } |
321 | ||
e9ee469a AM |
322 | static void |
323 | fold_binary (etree_type *tree) | |
252b5132 | 324 | { |
4ac0c898 | 325 | etree_value_type lhs; |
e9ee469a | 326 | exp_fold_tree_1 (tree->binary.lhs); |
ba916c8a MM |
327 | |
328 | /* The SEGMENT_START operator is special because its first | |
8c0848b5 AM |
329 | operand is a string, not the name of a symbol. Note that the |
330 | operands have been swapped, so binary.lhs is second (default) | |
331 | operand, binary.rhs is first operand. */ | |
e9ee469a | 332 | if (expld.result.valid_p && tree->type.node_code == SEGMENT_START) |
ba916c8a MM |
333 | { |
334 | const char *segment_name; | |
335 | segment_type *seg; | |
7542af2a | 336 | |
ba916c8a MM |
337 | /* Check to see if the user has overridden the default |
338 | value. */ | |
339 | segment_name = tree->binary.rhs->name.name; | |
e4492aa0 | 340 | for (seg = segments; seg; seg = seg->next) |
ba916c8a MM |
341 | if (strcmp (seg->name, segment_name) == 0) |
342 | { | |
c8ce5710 L |
343 | if (!seg->used |
344 | && config.magic_demand_paged | |
345 | && (seg->value % config.maxpagesize) != 0) | |
346 | einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"), | |
347 | segment_name); | |
ba916c8a | 348 | seg->used = TRUE; |
7542af2a | 349 | new_rel_from_abs (seg->value); |
ba916c8a MM |
350 | break; |
351 | } | |
4ac0c898 | 352 | return; |
ba916c8a | 353 | } |
252b5132 | 354 | |
4ac0c898 AM |
355 | lhs = expld.result; |
356 | exp_fold_tree_1 (tree->binary.rhs); | |
357 | expld.result.valid_p &= lhs.valid_p; | |
358 | ||
359 | if (expld.result.valid_p) | |
360 | { | |
7542af2a | 361 | if (lhs.section != expld.result.section) |
252b5132 | 362 | { |
7542af2a AM |
363 | /* If the values are from different sections, and neither is |
364 | just a number, make both the source arguments absolute. */ | |
365 | if (expld.result.section != NULL | |
366 | && lhs.section != NULL) | |
367 | { | |
368 | make_abs (); | |
369 | lhs.value += lhs.section->vma; | |
9bc8bb33 | 370 | lhs.section = bfd_abs_section_ptr; |
7542af2a AM |
371 | } |
372 | ||
373 | /* If the rhs is just a number, keep the lhs section. */ | |
374 | else if (expld.result.section == NULL) | |
9bc8bb33 AM |
375 | { |
376 | expld.result.section = lhs.section; | |
377 | /* Make this NULL so that we know one of the operands | |
378 | was just a number, for later tests. */ | |
379 | lhs.section = NULL; | |
380 | } | |
4ac0c898 | 381 | } |
9bc8bb33 AM |
382 | /* At this point we know that both operands have the same |
383 | section, or at least one of them is a plain number. */ | |
252b5132 | 384 | |
4ac0c898 AM |
385 | switch (tree->type.node_code) |
386 | { | |
9bc8bb33 AM |
387 | /* Arithmetic operators, bitwise AND, bitwise OR and XOR |
388 | keep the section of one of their operands only when the | |
389 | other operand is a plain number. Losing the section when | |
390 | operating on two symbols, ie. a result of a plain number, | |
391 | is required for subtraction and XOR. It's justifiable | |
392 | for the other operations on the grounds that adding, | |
393 | multiplying etc. two section relative values does not | |
394 | really make sense unless they are just treated as | |
395 | numbers. | |
396 | The same argument could be made for many expressions | |
397 | involving one symbol and a number. For example, | |
398 | "1 << x" and "100 / x" probably should not be given the | |
399 | section of x. The trouble is that if we fuss about such | |
400 | things the rules become complex and it is onerous to | |
401 | document ld expression evaluation. */ | |
e9ee469a | 402 | #define BOP(x, y) \ |
7542af2a AM |
403 | case x: \ |
404 | expld.result.value = lhs.value y expld.result.value; \ | |
9bc8bb33 AM |
405 | if (expld.result.section == lhs.section) \ |
406 | expld.result.section = NULL; \ | |
7542af2a AM |
407 | break; |
408 | ||
9bc8bb33 AM |
409 | /* Comparison operators, logical AND, and logical OR always |
410 | return a plain number. */ | |
7542af2a AM |
411 | #define BOPN(x, y) \ |
412 | case x: \ | |
413 | expld.result.value = lhs.value y expld.result.value; \ | |
414 | expld.result.section = NULL; \ | |
415 | break; | |
e9ee469a | 416 | |
4ac0c898 AM |
417 | BOP ('+', +); |
418 | BOP ('*', *); | |
419 | BOP ('-', -); | |
420 | BOP (LSHIFT, <<); | |
421 | BOP (RSHIFT, >>); | |
4ac0c898 AM |
422 | BOP ('&', &); |
423 | BOP ('^', ^); | |
424 | BOP ('|', |); | |
7542af2a AM |
425 | BOPN (EQ, ==); |
426 | BOPN (NE, !=); | |
427 | BOPN ('<', <); | |
428 | BOPN ('>', >); | |
429 | BOPN (LE, <=); | |
430 | BOPN (GE, >=); | |
431 | BOPN (ANDAND, &&); | |
432 | BOPN (OROR, ||); | |
4ac0c898 | 433 | |
9bc8bb33 AM |
434 | case '%': |
435 | if (expld.result.value != 0) | |
436 | expld.result.value = ((bfd_signed_vma) lhs.value | |
437 | % (bfd_signed_vma) expld.result.value); | |
438 | else if (expld.phase != lang_mark_phase_enum) | |
dab69f68 | 439 | einfo (_("%F%S %% by zero\n"), tree->binary.rhs); |
9bc8bb33 AM |
440 | if (expld.result.section == lhs.section) |
441 | expld.result.section = NULL; | |
442 | break; | |
443 | ||
444 | case '/': | |
445 | if (expld.result.value != 0) | |
446 | expld.result.value = ((bfd_signed_vma) lhs.value | |
447 | / (bfd_signed_vma) expld.result.value); | |
448 | else if (expld.phase != lang_mark_phase_enum) | |
dab69f68 | 449 | einfo (_("%F%S / by zero\n"), tree->binary.rhs); |
9bc8bb33 AM |
450 | if (expld.result.section == lhs.section) |
451 | expld.result.section = NULL; | |
452 | break; | |
453 | ||
4ac0c898 AM |
454 | case MAX_K: |
455 | if (lhs.value > expld.result.value) | |
456 | expld.result.value = lhs.value; | |
457 | break; | |
252b5132 | 458 | |
4ac0c898 AM |
459 | case MIN_K: |
460 | if (lhs.value < expld.result.value) | |
461 | expld.result.value = lhs.value; | |
462 | break; | |
252b5132 | 463 | |
4ac0c898 AM |
464 | case ALIGN_K: |
465 | expld.result.value = align_n (lhs.value, expld.result.value); | |
466 | break; | |
c468c8bc | 467 | |
4ac0c898 AM |
468 | case DATA_SEGMENT_ALIGN: |
469 | expld.dataseg.relro = exp_dataseg_relro_start; | |
ea7c2e6c AM |
470 | if (expld.phase == lang_first_phase_enum |
471 | || expld.section != bfd_abs_section_ptr) | |
472 | expld.result.valid_p = FALSE; | |
473 | else | |
4ac0c898 AM |
474 | { |
475 | bfd_vma maxpage = lhs.value; | |
476 | bfd_vma commonpage = expld.result.value; | |
2d20f7bf | 477 | |
4ac0c898 AM |
478 | expld.result.value = align_n (expld.dot, maxpage); |
479 | if (expld.dataseg.phase == exp_dataseg_relro_adjust) | |
480 | expld.result.value = expld.dataseg.base; | |
ea7c2e6c AM |
481 | else if (expld.dataseg.phase == exp_dataseg_adjust) |
482 | { | |
483 | if (commonpage < maxpage) | |
484 | expld.result.value += ((expld.dot + commonpage - 1) | |
485 | & (maxpage - commonpage)); | |
486 | } | |
487 | else | |
4ac0c898 AM |
488 | { |
489 | expld.result.value += expld.dot & (maxpage - 1); | |
ea7c2e6c AM |
490 | if (expld.dataseg.phase == exp_dataseg_done) |
491 | { | |
492 | /* OK. */ | |
493 | } | |
494 | else if (expld.dataseg.phase == exp_dataseg_none) | |
2d20f7bf | 495 | { |
4ac0c898 AM |
496 | expld.dataseg.phase = exp_dataseg_align_seen; |
497 | expld.dataseg.min_base = expld.dot; | |
498 | expld.dataseg.base = expld.result.value; | |
499 | expld.dataseg.pagesize = commonpage; | |
500 | expld.dataseg.maxpagesize = maxpage; | |
501 | expld.dataseg.relro_end = 0; | |
2d20f7bf | 502 | } |
ea7c2e6c AM |
503 | else |
504 | expld.result.valid_p = FALSE; | |
2d20f7bf | 505 | } |
4ac0c898 | 506 | } |
4ac0c898 | 507 | break; |
e9ee469a | 508 | |
4ac0c898 AM |
509 | case DATA_SEGMENT_RELRO_END: |
510 | expld.dataseg.relro = exp_dataseg_relro_end; | |
ea7c2e6c AM |
511 | if (expld.phase == lang_first_phase_enum |
512 | || expld.section != bfd_abs_section_ptr) | |
513 | expld.result.valid_p = FALSE; | |
514 | else if (expld.dataseg.phase == exp_dataseg_align_seen | |
515 | || expld.dataseg.phase == exp_dataseg_adjust | |
516 | || expld.dataseg.phase == exp_dataseg_relro_adjust | |
517 | || expld.dataseg.phase == exp_dataseg_done) | |
4ac0c898 AM |
518 | { |
519 | if (expld.dataseg.phase == exp_dataseg_align_seen | |
520 | || expld.dataseg.phase == exp_dataseg_relro_adjust) | |
521 | expld.dataseg.relro_end = lhs.value + expld.result.value; | |
e9ee469a | 522 | |
4ac0c898 AM |
523 | if (expld.dataseg.phase == exp_dataseg_relro_adjust |
524 | && (expld.dataseg.relro_end | |
525 | & (expld.dataseg.pagesize - 1))) | |
526 | { | |
527 | expld.dataseg.relro_end += expld.dataseg.pagesize - 1; | |
528 | expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1); | |
529 | expld.result.value = (expld.dataseg.relro_end | |
530 | - expld.result.value); | |
a4f5ad88 JJ |
531 | } |
532 | else | |
4ac0c898 | 533 | expld.result.value = lhs.value; |
a4f5ad88 | 534 | |
4ac0c898 AM |
535 | if (expld.dataseg.phase == exp_dataseg_align_seen) |
536 | expld.dataseg.phase = exp_dataseg_relro_seen; | |
252b5132 | 537 | } |
4ac0c898 AM |
538 | else |
539 | expld.result.valid_p = FALSE; | |
540 | break; | |
541 | ||
542 | default: | |
543 | FAIL (); | |
252b5132 | 544 | } |
252b5132 | 545 | } |
252b5132 RH |
546 | } |
547 | ||
e9ee469a AM |
548 | static void |
549 | fold_trinary (etree_type *tree) | |
0ae1cf52 | 550 | { |
e9ee469a AM |
551 | exp_fold_tree_1 (tree->trinary.cond); |
552 | if (expld.result.valid_p) | |
553 | exp_fold_tree_1 (expld.result.value | |
554 | ? tree->trinary.lhs | |
555 | : tree->trinary.rhs); | |
0ae1cf52 AM |
556 | } |
557 | ||
e9ee469a AM |
558 | static void |
559 | fold_name (etree_type *tree) | |
252b5132 | 560 | { |
e9ee469a | 561 | memset (&expld.result, 0, sizeof (expld.result)); |
c468c8bc | 562 | |
4de2d33d | 563 | switch (tree->type.node_code) |
8c95a62e KH |
564 | { |
565 | case SIZEOF_HEADERS: | |
e9ee469a AM |
566 | if (expld.phase != lang_first_phase_enum) |
567 | { | |
568 | bfd_vma hdr_size = 0; | |
569 | /* Don't find the real header size if only marking sections; | |
570 | The bfd function may cache incorrect data. */ | |
571 | if (expld.phase != lang_mark_phase_enum) | |
f13a99db | 572 | hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info); |
7542af2a | 573 | new_number (hdr_size); |
e9ee469a | 574 | } |
8c95a62e | 575 | break; |
67469e1f | 576 | |
8c95a62e | 577 | case DEFINED: |
e9ee469a | 578 | if (expld.phase == lang_first_phase_enum) |
1b493742 | 579 | lang_track_definedness (tree->name.name); |
8c95a62e KH |
580 | else |
581 | { | |
582 | struct bfd_link_hash_entry *h; | |
420e579c HPN |
583 | int def_iteration |
584 | = lang_symbol_definition_iteration (tree->name.name); | |
8c95a62e | 585 | |
f13a99db AM |
586 | h = bfd_wrapped_link_hash_lookup (link_info.output_bfd, |
587 | &link_info, | |
8c95a62e | 588 | tree->name.name, |
b34976b6 | 589 | FALSE, FALSE, TRUE); |
7542af2a AM |
590 | new_number (h != NULL |
591 | && (h->type == bfd_link_hash_defined | |
592 | || h->type == bfd_link_hash_defweak | |
593 | || h->type == bfd_link_hash_common) | |
594 | && (def_iteration == lang_statement_iteration | |
595 | || def_iteration == -1)); | |
8c95a62e KH |
596 | } |
597 | break; | |
67469e1f | 598 | |
8c95a62e | 599 | case NAME: |
4194268f AM |
600 | if (expld.assign_name != NULL |
601 | && strcmp (expld.assign_name, tree->name.name) == 0) | |
602 | expld.assign_name = NULL; | |
e9ee469a AM |
603 | if (expld.phase == lang_first_phase_enum) |
604 | ; | |
605 | else if (tree->name.name[0] == '.' && tree->name.name[1] == 0) | |
606 | new_rel_from_abs (expld.dot); | |
607 | else | |
8c95a62e KH |
608 | { |
609 | struct bfd_link_hash_entry *h; | |
610 | ||
f13a99db AM |
611 | h = bfd_wrapped_link_hash_lookup (link_info.output_bfd, |
612 | &link_info, | |
8c95a62e | 613 | tree->name.name, |
1b493742 NS |
614 | TRUE, FALSE, TRUE); |
615 | if (!h) | |
616 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); | |
617 | else if (h->type == bfd_link_hash_defined | |
618 | || h->type == bfd_link_hash_defweak) | |
8c95a62e | 619 | { |
7542af2a | 620 | asection *output_section; |
8c95a62e | 621 | |
7542af2a AM |
622 | output_section = h->u.def.section->output_section; |
623 | if (output_section == NULL) | |
624 | { | |
2aa9aad9 NC |
625 | if (expld.phase == lang_mark_phase_enum) |
626 | new_rel (h->u.def.value, h->u.def.section); | |
627 | else | |
7542af2a AM |
628 | einfo (_("%X%S: unresolvable symbol `%s'" |
629 | " referenced in expression\n"), | |
dab69f68 | 630 | tree, tree->name.name); |
8c95a62e | 631 | } |
5c3049d2 AM |
632 | else if (output_section == bfd_abs_section_ptr |
633 | && (expld.section != bfd_abs_section_ptr | |
01554a74 | 634 | || config.sane_expr)) |
abf4be64 | 635 | new_number (h->u.def.value + h->u.def.section->output_offset); |
7542af2a AM |
636 | else |
637 | new_rel (h->u.def.value + h->u.def.section->output_offset, | |
638 | output_section); | |
8c95a62e | 639 | } |
e9ee469a | 640 | else if (expld.phase == lang_final_phase_enum |
ec8460b5 AM |
641 | || (expld.phase != lang_mark_phase_enum |
642 | && expld.assigning_to_dot)) | |
dab69f68 AM |
643 | einfo (_("%F%S: undefined symbol `%s'" |
644 | " referenced in expression\n"), | |
645 | tree, tree->name.name); | |
1b493742 NS |
646 | else if (h->type == bfd_link_hash_new) |
647 | { | |
648 | h->type = bfd_link_hash_undefined; | |
649 | h->u.undef.abfd = NULL; | |
3eda52aa | 650 | if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail) |
a010d60f | 651 | bfd_link_add_undef (link_info.hash, h); |
1b493742 | 652 | } |
8c95a62e KH |
653 | } |
654 | break; | |
655 | ||
656 | case ADDR: | |
e9ee469a | 657 | if (expld.phase != lang_first_phase_enum) |
8c95a62e KH |
658 | { |
659 | lang_output_section_statement_type *os; | |
660 | ||
661 | os = lang_output_section_find (tree->name.name); | |
cc3e2771 NS |
662 | if (os == NULL) |
663 | { | |
664 | if (expld.phase == lang_final_phase_enum) | |
dab69f68 AM |
665 | einfo (_("%F%S: undefined section `%s'" |
666 | " referenced in expression\n"), | |
667 | tree, tree->name.name); | |
cc3e2771 NS |
668 | } |
669 | else if (os->processed_vma) | |
5942515f | 670 | new_rel (0, os->bfd_section); |
8c95a62e | 671 | } |
8c95a62e KH |
672 | break; |
673 | ||
674 | case LOADADDR: | |
e9ee469a | 675 | if (expld.phase != lang_first_phase_enum) |
8c95a62e KH |
676 | { |
677 | lang_output_section_statement_type *os; | |
678 | ||
679 | os = lang_output_section_find (tree->name.name); | |
cc3e2771 NS |
680 | if (os == NULL) |
681 | { | |
682 | if (expld.phase == lang_final_phase_enum) | |
dab69f68 AM |
683 | einfo (_("%F%S: undefined section `%s'" |
684 | " referenced in expression\n"), | |
685 | tree, tree->name.name); | |
cc3e2771 NS |
686 | } |
687 | else if (os->processed_lma) | |
1b493742 | 688 | { |
e9ee469a | 689 | if (os->load_base == NULL) |
3e23777d | 690 | new_abs (os->bfd_section->lma); |
e9ee469a | 691 | else |
67469e1f AM |
692 | { |
693 | exp_fold_tree_1 (os->load_base); | |
819da74e AM |
694 | if (expld.result.valid_p) |
695 | make_abs (); | |
67469e1f | 696 | } |
1b493742 | 697 | } |
8c95a62e | 698 | } |
8c95a62e KH |
699 | break; |
700 | ||
701 | case SIZEOF: | |
362c1d1a | 702 | case ALIGNOF: |
e9ee469a | 703 | if (expld.phase != lang_first_phase_enum) |
8c95a62e | 704 | { |
8c95a62e KH |
705 | lang_output_section_statement_type *os; |
706 | ||
707 | os = lang_output_section_find (tree->name.name); | |
5397b1fe | 708 | if (os == NULL) |
cc3e2771 NS |
709 | { |
710 | if (expld.phase == lang_final_phase_enum) | |
dab69f68 AM |
711 | einfo (_("%F%S: undefined section `%s'" |
712 | " referenced in expression\n"), | |
713 | tree, tree->name.name); | |
7542af2a | 714 | new_number (0); |
cc3e2771 | 715 | } |
17d6eea5 | 716 | else if (os->bfd_section != NULL) |
362c1d1a NS |
717 | { |
718 | bfd_vma val; | |
719 | ||
720 | if (tree->type.node_code == SIZEOF) | |
f13a99db AM |
721 | val = (os->bfd_section->size |
722 | / bfd_octets_per_byte (link_info.output_bfd)); | |
362c1d1a NS |
723 | else |
724 | val = (bfd_vma)1 << os->bfd_section->alignment_power; | |
e4492aa0 | 725 | |
7542af2a | 726 | new_number (val); |
362c1d1a | 727 | } |
17d6eea5 L |
728 | else |
729 | new_number (0); | |
8c95a62e | 730 | } |
8c95a62e KH |
731 | break; |
732 | ||
3ec57632 NC |
733 | case LENGTH: |
734 | { | |
735 | lang_memory_region_type *mem; | |
e4492aa0 L |
736 | |
737 | mem = lang_memory_region_lookup (tree->name.name, FALSE); | |
738 | if (mem != NULL) | |
7542af2a | 739 | new_number (mem->length); |
e4492aa0 | 740 | else |
e9ee469a | 741 | einfo (_("%F%S: undefined MEMORY region `%s'" |
dab69f68 AM |
742 | " referenced in expression\n"), |
743 | tree, tree->name.name); | |
3ec57632 NC |
744 | } |
745 | break; | |
746 | ||
747 | case ORIGIN: | |
7542af2a AM |
748 | if (expld.phase != lang_first_phase_enum) |
749 | { | |
750 | lang_memory_region_type *mem; | |
e4492aa0 L |
751 | |
752 | mem = lang_memory_region_lookup (tree->name.name, FALSE); | |
753 | if (mem != NULL) | |
7542af2a | 754 | new_rel_from_abs (mem->origin); |
e4492aa0 | 755 | else |
7542af2a | 756 | einfo (_("%F%S: undefined MEMORY region `%s'" |
dab69f68 AM |
757 | " referenced in expression\n"), |
758 | tree, tree->name.name); | |
7542af2a | 759 | } |
3ec57632 NC |
760 | break; |
761 | ||
24718e3b L |
762 | case CONSTANT: |
763 | if (strcmp (tree->name.name, "MAXPAGESIZE") == 0) | |
7542af2a | 764 | new_number (config.maxpagesize); |
24718e3b | 765 | else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0) |
7542af2a | 766 | new_number (config.commonpagesize); |
24718e3b L |
767 | else |
768 | einfo (_("%F%S: unknown constant `%s' referenced in expression\n"), | |
dab69f68 | 769 | tree, tree->name.name); |
24718e3b L |
770 | break; |
771 | ||
8c95a62e KH |
772 | default: |
773 | FAIL (); | |
774 | break; | |
775 | } | |
252b5132 | 776 | } |
8c95a62e | 777 | |
e9ee469a AM |
778 | static void |
779 | exp_fold_tree_1 (etree_type *tree) | |
252b5132 | 780 | { |
252b5132 RH |
781 | if (tree == NULL) |
782 | { | |
e9ee469a AM |
783 | memset (&expld.result, 0, sizeof (expld.result)); |
784 | return; | |
252b5132 RH |
785 | } |
786 | ||
4de2d33d | 787 | switch (tree->type.node_class) |
252b5132 RH |
788 | { |
789 | case etree_value: | |
5c3049d2 | 790 | if (expld.section == bfd_abs_section_ptr |
01554a74 | 791 | && !config.sane_expr) |
5c3049d2 AM |
792 | new_abs (tree->value.value); |
793 | else | |
794 | new_number (tree->value.value); | |
5942515f | 795 | expld.result.str = tree->value.str; |
252b5132 RH |
796 | break; |
797 | ||
798 | case etree_rel: | |
e9ee469a AM |
799 | if (expld.phase != lang_first_phase_enum) |
800 | { | |
801 | asection *output_section = tree->rel.section->output_section; | |
802 | new_rel (tree->rel.value + tree->rel.section->output_offset, | |
5942515f | 803 | output_section); |
e9ee469a | 804 | } |
252b5132 | 805 | else |
e9ee469a | 806 | memset (&expld.result, 0, sizeof (expld.result)); |
252b5132 RH |
807 | break; |
808 | ||
809 | case etree_assert: | |
e9ee469a | 810 | exp_fold_tree_1 (tree->assert_s.child); |
5397b1fe AM |
811 | if (expld.phase == lang_final_phase_enum && !expld.result.value) |
812 | einfo ("%X%P: %s\n", tree->assert_s.message); | |
252b5132 RH |
813 | break; |
814 | ||
815 | case etree_unary: | |
e9ee469a | 816 | fold_unary (tree); |
252b5132 RH |
817 | break; |
818 | ||
819 | case etree_binary: | |
e9ee469a | 820 | fold_binary (tree); |
252b5132 | 821 | break; |
0ae1cf52 AM |
822 | |
823 | case etree_trinary: | |
e9ee469a | 824 | fold_trinary (tree); |
0ae1cf52 | 825 | break; |
252b5132 RH |
826 | |
827 | case etree_assign: | |
828 | case etree_provide: | |
b46a87b1 | 829 | case etree_provided: |
252b5132 RH |
830 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) |
831 | { | |
b46a87b1 | 832 | if (tree->type.node_class != etree_assign) |
dab69f68 AM |
833 | einfo (_("%F%S can not PROVIDE assignment to" |
834 | " location counter\n"), tree); | |
ec8460b5 | 835 | if (expld.phase != lang_first_phase_enum) |
252b5132 | 836 | { |
fbbb9ac5 | 837 | /* Notify the folder that this is an assignment to dot. */ |
e9ee469a AM |
838 | expld.assigning_to_dot = TRUE; |
839 | exp_fold_tree_1 (tree->assign.src); | |
840 | expld.assigning_to_dot = FALSE; | |
841 | ||
842 | if (!expld.result.valid_p) | |
843 | { | |
844 | if (expld.phase != lang_mark_phase_enum) | |
dab69f68 AM |
845 | einfo (_("%F%S invalid assignment to" |
846 | " location counter\n"), tree); | |
e9ee469a AM |
847 | } |
848 | else if (expld.dotp == NULL) | |
849 | einfo (_("%F%S assignment to location counter" | |
ec8460b5 AM |
850 | " invalid outside of SECTIONS\n"), tree); |
851 | ||
852 | /* After allocation, assignment to dot should not be | |
853 | done inside an output section since allocation adds a | |
854 | padding statement that effectively duplicates the | |
855 | assignment. */ | |
856 | else if (expld.phase <= lang_allocating_phase_enum | |
857 | || expld.section == bfd_abs_section_ptr) | |
252b5132 | 858 | { |
e9ee469a AM |
859 | bfd_vma nextdot; |
860 | ||
7542af2a AM |
861 | nextdot = expld.result.value; |
862 | if (expld.result.section != NULL) | |
863 | nextdot += expld.result.section->vma; | |
864 | else | |
865 | nextdot += expld.section->vma; | |
e9ee469a AM |
866 | if (nextdot < expld.dot |
867 | && expld.section != bfd_abs_section_ptr) | |
868 | einfo (_("%F%S cannot move location counter backwards" | |
dab69f68 AM |
869 | " (from %V to %V)\n"), |
870 | tree, expld.dot, nextdot); | |
252b5132 RH |
871 | else |
872 | { | |
e9ee469a AM |
873 | expld.dot = nextdot; |
874 | *expld.dotp = nextdot; | |
252b5132 RH |
875 | } |
876 | } | |
877 | } | |
8b3d8fa8 | 878 | else |
e9ee469a | 879 | memset (&expld.result, 0, sizeof (expld.result)); |
252b5132 RH |
880 | } |
881 | else | |
882 | { | |
e9ee469a | 883 | struct bfd_link_hash_entry *h = NULL; |
252b5132 | 884 | |
e9ee469a AM |
885 | if (tree->type.node_class == etree_provide) |
886 | { | |
252b5132 | 887 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, |
e9ee469a AM |
888 | FALSE, FALSE, TRUE); |
889 | if (h == NULL | |
890 | || (h->type != bfd_link_hash_new | |
891 | && h->type != bfd_link_hash_undefined | |
892 | && h->type != bfd_link_hash_common)) | |
893 | { | |
894 | /* Do nothing. The symbol was never referenced, or was | |
895 | defined by some object. */ | |
896 | break; | |
897 | } | |
898 | } | |
899 | ||
4194268f | 900 | expld.assign_name = tree->assign.dst; |
e9ee469a | 901 | exp_fold_tree_1 (tree->assign.src); |
4194268f AM |
902 | /* expld.assign_name remaining equal to tree->assign.dst |
903 | below indicates the evaluation of tree->assign.src did | |
904 | not use the value of tree->assign.dst. We don't allow | |
905 | self assignment until the final phase for two reasons: | |
906 | 1) Expressions are evaluated multiple times. With | |
907 | relaxation, the number of times may vary. | |
908 | 2) Section relative symbol values cannot be correctly | |
909 | converted to absolute values, as is required by many | |
910 | expressions, until final section sizing is complete. */ | |
911 | if ((expld.result.valid_p | |
912 | && (expld.phase == lang_final_phase_enum | |
913 | || expld.assign_name != NULL)) | |
2aa9aad9 | 914 | || (expld.phase <= lang_mark_phase_enum |
01554a74 | 915 | && tree->type.node_class == etree_assign |
eb8476a6 | 916 | && tree->assign.defsym)) |
e9ee469a | 917 | { |
1579bae1 | 918 | if (h == NULL) |
67010b46 | 919 | { |
e9ee469a AM |
920 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, |
921 | TRUE, FALSE, TRUE); | |
922 | if (h == NULL) | |
67010b46 NC |
923 | einfo (_("%P%F:%s: hash creation failed\n"), |
924 | tree->assign.dst); | |
925 | } | |
e9ee469a AM |
926 | |
927 | /* FIXME: Should we worry if the symbol is already | |
928 | defined? */ | |
929 | lang_update_definedness (tree->assign.dst, h); | |
930 | h->type = bfd_link_hash_defined; | |
931 | h->u.def.value = expld.result.value; | |
7542af2a AM |
932 | if (expld.result.section == NULL) |
933 | expld.result.section = expld.section; | |
e9ee469a AM |
934 | h->u.def.section = expld.result.section; |
935 | if (tree->type.node_class == etree_provide) | |
936 | tree->type.node_class = etree_provided; | |
1338dd10 PB |
937 | |
938 | /* Copy the symbol type if this is a simple assignment of | |
f37a7048 NS |
939 | one symbol to another. This could be more general |
940 | (e.g. a ?: operator with NAMEs in each branch). */ | |
1338dd10 PB |
941 | if (tree->assign.src->type.node_class == etree_name) |
942 | { | |
943 | struct bfd_link_hash_entry *hsrc; | |
944 | ||
945 | hsrc = bfd_link_hash_lookup (link_info.hash, | |
946 | tree->assign.src->name.name, | |
947 | FALSE, FALSE, TRUE); | |
948 | if (hsrc) | |
949 | bfd_copy_link_hash_symbol_type (link_info.output_bfd, h, | |
950 | hsrc); | |
951 | } | |
252b5132 | 952 | } |
e092cb30 AM |
953 | else if (expld.phase == lang_final_phase_enum) |
954 | { | |
955 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, | |
956 | FALSE, FALSE, TRUE); | |
957 | if (h != NULL | |
958 | && h->type == bfd_link_hash_new) | |
959 | h->type = bfd_link_hash_undefined; | |
960 | } | |
4194268f | 961 | expld.assign_name = NULL; |
252b5132 RH |
962 | } |
963 | break; | |
964 | ||
965 | case etree_name: | |
e9ee469a | 966 | fold_name (tree); |
252b5132 RH |
967 | break; |
968 | ||
969 | default: | |
970 | FAIL (); | |
e9ee469a | 971 | memset (&expld.result, 0, sizeof (expld.result)); |
252b5132 RH |
972 | break; |
973 | } | |
252b5132 RH |
974 | } |
975 | ||
e9ee469a AM |
976 | void |
977 | exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp) | |
75ff4589 | 978 | { |
e9ee469a AM |
979 | expld.dot = *dotp; |
980 | expld.dotp = dotp; | |
981 | expld.section = current_section; | |
982 | exp_fold_tree_1 (tree); | |
75ff4589 L |
983 | } |
984 | ||
e759c116 | 985 | void |
e9ee469a | 986 | exp_fold_tree_no_dot (etree_type *tree) |
252b5132 | 987 | { |
e9ee469a AM |
988 | expld.dot = 0; |
989 | expld.dotp = NULL; | |
990 | expld.section = bfd_abs_section_ptr; | |
991 | exp_fold_tree_1 (tree); | |
252b5132 RH |
992 | } |
993 | ||
994 | etree_type * | |
1579bae1 | 995 | exp_binop (int code, etree_type *lhs, etree_type *rhs) |
252b5132 | 996 | { |
d3ce72d0 | 997 | etree_type value, *new_e; |
252b5132 RH |
998 | |
999 | value.type.node_code = code; | |
dab69f68 | 1000 | value.type.filename = lhs->type.filename; |
f856040a | 1001 | value.type.lineno = lhs->type.lineno; |
252b5132 RH |
1002 | value.binary.lhs = lhs; |
1003 | value.binary.rhs = rhs; | |
1004 | value.type.node_class = etree_binary; | |
e9ee469a AM |
1005 | exp_fold_tree_no_dot (&value); |
1006 | if (expld.result.valid_p) | |
1007 | return exp_intop (expld.result.value); | |
1008 | ||
d3ce72d0 NC |
1009 | new_e = (etree_type *) stat_alloc (sizeof (new_e->binary)); |
1010 | memcpy (new_e, &value, sizeof (new_e->binary)); | |
1011 | return new_e; | |
252b5132 RH |
1012 | } |
1013 | ||
1014 | etree_type * | |
1579bae1 | 1015 | exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs) |
252b5132 | 1016 | { |
d3ce72d0 | 1017 | etree_type value, *new_e; |
e9ee469a | 1018 | |
252b5132 | 1019 | value.type.node_code = code; |
dab69f68 AM |
1020 | value.type.filename = cond->type.filename; |
1021 | value.type.lineno = cond->type.lineno; | |
252b5132 RH |
1022 | value.trinary.lhs = lhs; |
1023 | value.trinary.cond = cond; | |
1024 | value.trinary.rhs = rhs; | |
1025 | value.type.node_class = etree_trinary; | |
e9ee469a AM |
1026 | exp_fold_tree_no_dot (&value); |
1027 | if (expld.result.valid_p) | |
1028 | return exp_intop (expld.result.value); | |
c7d701b0 | 1029 | |
d3ce72d0 NC |
1030 | new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary)); |
1031 | memcpy (new_e, &value, sizeof (new_e->trinary)); | |
1032 | return new_e; | |
252b5132 RH |
1033 | } |
1034 | ||
252b5132 | 1035 | etree_type * |
1579bae1 | 1036 | exp_unop (int code, etree_type *child) |
252b5132 | 1037 | { |
d3ce72d0 | 1038 | etree_type value, *new_e; |
252b5132 | 1039 | |
252b5132 | 1040 | value.unary.type.node_code = code; |
dab69f68 | 1041 | value.unary.type.filename = child->type.filename; |
f856040a | 1042 | value.unary.type.lineno = child->type.lineno; |
252b5132 RH |
1043 | value.unary.child = child; |
1044 | value.unary.type.node_class = etree_unary; | |
e9ee469a AM |
1045 | exp_fold_tree_no_dot (&value); |
1046 | if (expld.result.valid_p) | |
1047 | return exp_intop (expld.result.value); | |
c7d701b0 | 1048 | |
d3ce72d0 NC |
1049 | new_e = (etree_type *) stat_alloc (sizeof (new_e->unary)); |
1050 | memcpy (new_e, &value, sizeof (new_e->unary)); | |
1051 | return new_e; | |
252b5132 RH |
1052 | } |
1053 | ||
252b5132 | 1054 | etree_type * |
1579bae1 | 1055 | exp_nameop (int code, const char *name) |
252b5132 | 1056 | { |
d3ce72d0 | 1057 | etree_type value, *new_e; |
e9ee469a | 1058 | |
252b5132 | 1059 | value.name.type.node_code = code; |
dab69f68 | 1060 | value.name.type.filename = ldlex_filename (); |
f856040a | 1061 | value.name.type.lineno = lineno; |
252b5132 RH |
1062 | value.name.name = name; |
1063 | value.name.type.node_class = etree_name; | |
1064 | ||
e9ee469a AM |
1065 | exp_fold_tree_no_dot (&value); |
1066 | if (expld.result.valid_p) | |
1067 | return exp_intop (expld.result.value); | |
c7d701b0 | 1068 | |
d3ce72d0 NC |
1069 | new_e = (etree_type *) stat_alloc (sizeof (new_e->name)); |
1070 | memcpy (new_e, &value, sizeof (new_e->name)); | |
1071 | return new_e; | |
252b5132 RH |
1072 | |
1073 | } | |
1074 | ||
2e57b2af AM |
1075 | static etree_type * |
1076 | exp_assop (const char *dst, | |
1077 | etree_type *src, | |
1078 | enum node_tree_enum class, | |
eb8476a6 | 1079 | bfd_boolean defsym, |
2e57b2af | 1080 | bfd_boolean hidden) |
252b5132 RH |
1081 | { |
1082 | etree_type *n; | |
1083 | ||
1e9cc1c2 | 1084 | n = (etree_type *) stat_alloc (sizeof (n->assign)); |
252b5132 | 1085 | n->assign.type.node_code = '='; |
dab69f68 | 1086 | n->assign.type.filename = src->type.filename; |
f856040a | 1087 | n->assign.type.lineno = src->type.lineno; |
2e57b2af | 1088 | n->assign.type.node_class = class; |
252b5132 RH |
1089 | n->assign.src = src; |
1090 | n->assign.dst = dst; | |
eb8476a6 | 1091 | n->assign.defsym = defsym; |
7af8e998 | 1092 | n->assign.hidden = hidden; |
252b5132 RH |
1093 | return n; |
1094 | } | |
1095 | ||
eb8476a6 MR |
1096 | /* Handle linker script assignments and HIDDEN. */ |
1097 | ||
2e57b2af | 1098 | etree_type * |
eb8476a6 | 1099 | exp_assign (const char *dst, etree_type *src, bfd_boolean hidden) |
2e57b2af | 1100 | { |
eb8476a6 | 1101 | return exp_assop (dst, src, etree_assign, FALSE, hidden); |
2e57b2af AM |
1102 | } |
1103 | ||
eb8476a6 MR |
1104 | /* Handle --defsym command-line option. */ |
1105 | ||
2e57b2af AM |
1106 | etree_type * |
1107 | exp_defsym (const char *dst, etree_type *src) | |
1108 | { | |
eb8476a6 | 1109 | return exp_assop (dst, src, etree_assign, TRUE, FALSE); |
2e57b2af AM |
1110 | } |
1111 | ||
1112 | /* Handle PROVIDE. */ | |
1113 | ||
1114 | etree_type * | |
1115 | exp_provide (const char *dst, etree_type *src, bfd_boolean hidden) | |
1116 | { | |
eb8476a6 | 1117 | return exp_assop (dst, src, etree_provide, FALSE, hidden); |
2e57b2af AM |
1118 | } |
1119 | ||
252b5132 RH |
1120 | /* Handle ASSERT. */ |
1121 | ||
1122 | etree_type * | |
1579bae1 | 1123 | exp_assert (etree_type *exp, const char *message) |
252b5132 RH |
1124 | { |
1125 | etree_type *n; | |
1126 | ||
1e9cc1c2 | 1127 | n = (etree_type *) stat_alloc (sizeof (n->assert_s)); |
252b5132 | 1128 | n->assert_s.type.node_code = '!'; |
dab69f68 | 1129 | n->assert_s.type.filename = exp->type.filename; |
f856040a | 1130 | n->assert_s.type.lineno = exp->type.lineno; |
252b5132 RH |
1131 | n->assert_s.type.node_class = etree_assert; |
1132 | n->assert_s.child = exp; | |
1133 | n->assert_s.message = message; | |
1134 | return n; | |
1135 | } | |
1136 | ||
4de2d33d | 1137 | void |
1579bae1 | 1138 | exp_print_tree (etree_type *tree) |
252b5132 | 1139 | { |
ae78bbeb AM |
1140 | bfd_boolean function_like; |
1141 | ||
c7d701b0 NC |
1142 | if (config.map_file == NULL) |
1143 | config.map_file = stderr; | |
b7a26f91 | 1144 | |
c7d701b0 NC |
1145 | if (tree == NULL) |
1146 | { | |
1147 | minfo ("NULL TREE\n"); | |
1148 | return; | |
1149 | } | |
b7a26f91 | 1150 | |
8c95a62e KH |
1151 | switch (tree->type.node_class) |
1152 | { | |
1153 | case etree_value: | |
1154 | minfo ("0x%v", tree->value.value); | |
1155 | return; | |
1156 | case etree_rel: | |
1157 | if (tree->rel.section->owner != NULL) | |
1158 | minfo ("%B:", tree->rel.section->owner); | |
1159 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); | |
1160 | return; | |
1161 | case etree_assign: | |
ae78bbeb | 1162 | fputs (tree->assign.dst, config.map_file); |
b34976b6 | 1163 | exp_print_token (tree->type.node_code, TRUE); |
8c95a62e KH |
1164 | exp_print_tree (tree->assign.src); |
1165 | break; | |
1166 | case etree_provide: | |
b46a87b1 | 1167 | case etree_provided: |
8c95a62e KH |
1168 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); |
1169 | exp_print_tree (tree->assign.src); | |
ae78bbeb | 1170 | fputc (')', config.map_file); |
8c95a62e KH |
1171 | break; |
1172 | case etree_binary: | |
ae78bbeb AM |
1173 | function_like = FALSE; |
1174 | switch (tree->type.node_code) | |
1175 | { | |
1176 | case MAX_K: | |
1177 | case MIN_K: | |
1178 | case ALIGN_K: | |
1179 | case DATA_SEGMENT_ALIGN: | |
1180 | case DATA_SEGMENT_RELRO_END: | |
1181 | function_like = TRUE; | |
67baf8c4 TG |
1182 | break; |
1183 | case SEGMENT_START: | |
1184 | /* Special handling because arguments are in reverse order and | |
1185 | the segment name is quoted. */ | |
1186 | exp_print_token (tree->type.node_code, FALSE); | |
1187 | fputs (" (\"", config.map_file); | |
1188 | exp_print_tree (tree->binary.rhs); | |
1189 | fputs ("\", ", config.map_file); | |
1190 | exp_print_tree (tree->binary.lhs); | |
1191 | fputc (')', config.map_file); | |
1192 | return; | |
ae78bbeb AM |
1193 | } |
1194 | if (function_like) | |
1195 | { | |
1196 | exp_print_token (tree->type.node_code, FALSE); | |
1197 | fputc (' ', config.map_file); | |
1198 | } | |
1199 | fputc ('(', config.map_file); | |
8c95a62e | 1200 | exp_print_tree (tree->binary.lhs); |
ae78bbeb AM |
1201 | if (function_like) |
1202 | fprintf (config.map_file, ", "); | |
1203 | else | |
1204 | exp_print_token (tree->type.node_code, TRUE); | |
8c95a62e | 1205 | exp_print_tree (tree->binary.rhs); |
ae78bbeb | 1206 | fputc (')', config.map_file); |
8c95a62e KH |
1207 | break; |
1208 | case etree_trinary: | |
1209 | exp_print_tree (tree->trinary.cond); | |
ae78bbeb | 1210 | fputc ('?', config.map_file); |
8c95a62e | 1211 | exp_print_tree (tree->trinary.lhs); |
ae78bbeb | 1212 | fputc (':', config.map_file); |
8c95a62e KH |
1213 | exp_print_tree (tree->trinary.rhs); |
1214 | break; | |
1215 | case etree_unary: | |
b34976b6 | 1216 | exp_print_token (tree->unary.type.node_code, FALSE); |
8c95a62e KH |
1217 | if (tree->unary.child) |
1218 | { | |
7b17f854 | 1219 | fprintf (config.map_file, " ("); |
8c95a62e | 1220 | exp_print_tree (tree->unary.child); |
ae78bbeb | 1221 | fputc (')', config.map_file); |
8c95a62e KH |
1222 | } |
1223 | break; | |
1224 | ||
1225 | case etree_assert: | |
1226 | fprintf (config.map_file, "ASSERT ("); | |
1227 | exp_print_tree (tree->assert_s.child); | |
1228 | fprintf (config.map_file, ", %s)", tree->assert_s.message); | |
1229 | break; | |
1230 | ||
8c95a62e KH |
1231 | case etree_name: |
1232 | if (tree->type.node_code == NAME) | |
ae78bbeb | 1233 | fputs (tree->name.name, config.map_file); |
8c95a62e KH |
1234 | else |
1235 | { | |
b34976b6 | 1236 | exp_print_token (tree->type.node_code, FALSE); |
8c95a62e | 1237 | if (tree->name.name) |
7b17f854 | 1238 | fprintf (config.map_file, " (%s)", tree->name.name); |
8c95a62e KH |
1239 | } |
1240 | break; | |
1241 | default: | |
1242 | FAIL (); | |
1243 | break; | |
252b5132 | 1244 | } |
252b5132 RH |
1245 | } |
1246 | ||
1247 | bfd_vma | |
e9ee469a | 1248 | exp_get_vma (etree_type *tree, bfd_vma def, char *name) |
252b5132 | 1249 | { |
252b5132 RH |
1250 | if (tree != NULL) |
1251 | { | |
e9ee469a AM |
1252 | exp_fold_tree_no_dot (tree); |
1253 | if (expld.result.valid_p) | |
1254 | return expld.result.value; | |
1255 | else if (name != NULL && expld.phase != lang_mark_phase_enum) | |
dab69f68 AM |
1256 | einfo (_("%F%S: nonconstant expression for %s\n"), |
1257 | tree, name); | |
252b5132 | 1258 | } |
e9ee469a | 1259 | return def; |
252b5132 RH |
1260 | } |
1261 | ||
4de2d33d | 1262 | int |
e9ee469a | 1263 | exp_get_value_int (etree_type *tree, int def, char *name) |
252b5132 | 1264 | { |
e9ee469a | 1265 | return exp_get_vma (tree, def, name); |
252b5132 RH |
1266 | } |
1267 | ||
2c382fb6 | 1268 | fill_type * |
e9ee469a | 1269 | exp_get_fill (etree_type *tree, fill_type *def, char *name) |
2c382fb6 AM |
1270 | { |
1271 | fill_type *fill; | |
2c382fb6 AM |
1272 | size_t len; |
1273 | unsigned int val; | |
1274 | ||
1275 | if (tree == NULL) | |
1276 | return def; | |
1277 | ||
e9ee469a AM |
1278 | exp_fold_tree_no_dot (tree); |
1279 | if (!expld.result.valid_p) | |
1280 | { | |
1281 | if (name != NULL && expld.phase != lang_mark_phase_enum) | |
dab69f68 AM |
1282 | einfo (_("%F%S: nonconstant expression for %s\n"), |
1283 | tree, name); | |
e9ee469a AM |
1284 | return def; |
1285 | } | |
2c382fb6 | 1286 | |
e9ee469a | 1287 | if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0) |
2c382fb6 AM |
1288 | { |
1289 | unsigned char *dst; | |
1290 | unsigned char *s; | |
1e9cc1c2 | 1291 | fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1); |
2c382fb6 AM |
1292 | fill->size = (len + 1) / 2; |
1293 | dst = fill->data; | |
e9ee469a | 1294 | s = (unsigned char *) expld.result.str; |
2c382fb6 AM |
1295 | val = 0; |
1296 | do | |
1297 | { | |
1298 | unsigned int digit; | |
1299 | ||
1300 | digit = *s++ - '0'; | |
1301 | if (digit > 9) | |
1302 | digit = (digit - 'A' + '0' + 10) & 0xf; | |
1303 | val <<= 4; | |
1304 | val += digit; | |
1305 | --len; | |
1306 | if ((len & 1) == 0) | |
1307 | { | |
1308 | *dst++ = val; | |
1309 | val = 0; | |
1310 | } | |
1311 | } | |
1312 | while (len != 0); | |
1313 | } | |
1314 | else | |
1315 | { | |
1e9cc1c2 | 1316 | fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1); |
e9ee469a | 1317 | val = expld.result.value; |
2c382fb6 AM |
1318 | fill->data[0] = (val >> 24) & 0xff; |
1319 | fill->data[1] = (val >> 16) & 0xff; | |
1320 | fill->data[2] = (val >> 8) & 0xff; | |
1321 | fill->data[3] = (val >> 0) & 0xff; | |
1322 | fill->size = 4; | |
1323 | } | |
1324 | return fill; | |
1325 | } | |
1326 | ||
252b5132 | 1327 | bfd_vma |
e9ee469a | 1328 | exp_get_abs_int (etree_type *tree, int def, char *name) |
252b5132 | 1329 | { |
e9ee469a AM |
1330 | if (tree != NULL) |
1331 | { | |
1332 | exp_fold_tree_no_dot (tree); | |
c7d701b0 | 1333 | |
e9ee469a AM |
1334 | if (expld.result.valid_p) |
1335 | { | |
7542af2a AM |
1336 | if (expld.result.section != NULL) |
1337 | expld.result.value += expld.result.section->vma; | |
e9ee469a AM |
1338 | return expld.result.value; |
1339 | } | |
1340 | else if (name != NULL && expld.phase != lang_mark_phase_enum) | |
f856040a | 1341 | { |
dab69f68 AM |
1342 | einfo (_("%F%S: nonconstant expression for %s\n"), |
1343 | tree, name); | |
f856040a | 1344 | } |
e9ee469a AM |
1345 | } |
1346 | return def; | |
252b5132 | 1347 | } |
c553bb91 | 1348 | |
e5caa5e0 AM |
1349 | static bfd_vma |
1350 | align_n (bfd_vma value, bfd_vma align) | |
c553bb91 AM |
1351 | { |
1352 | if (align <= 1) | |
1353 | return value; | |
1354 | ||
1355 | value = (value + align - 1) / align; | |
1356 | return value * align; | |
1357 | } |