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