Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* This module handles expression trees. |
a2b64bed | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
e5caa5e0 | 3 | 2001, 2002, 2003, 2004 |
87f2a346 | 4 | Free Software Foundation, Inc. |
8c95a62e | 5 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. |
252b5132 RH |
6 | |
7 | This file is part of GLD, the Gnu Linker. | |
8 | ||
9 | GLD is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
13 | ||
14 | GLD is distributed in the hope that it will be useful, | |
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. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GLD; see the file COPYING. If not, write to the Free | |
21 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
22 | 02111-1307, USA. */ | |
23 | ||
8c95a62e | 24 | /* This module is in charge of working out the contents of expressions. |
252b5132 | 25 | |
8c95a62e KH |
26 | It has to keep track of the relative/absness of a symbol etc. This |
27 | is done by keeping all values in a struct (an etree_value_type) | |
28 | which contains a value, a section to which it is relative and a | |
29 | valid bit. */ | |
252b5132 | 30 | |
252b5132 RH |
31 | #include "bfd.h" |
32 | #include "sysdep.h" | |
33 | #include "bfdlink.h" | |
34 | ||
35 | #include "ld.h" | |
36 | #include "ldmain.h" | |
37 | #include "ldmisc.h" | |
38 | #include "ldexp.h" | |
df2a7313 | 39 | #include <ldgram.h> |
252b5132 | 40 | #include "ldlang.h" |
c7d701b0 | 41 | #include "libiberty.h" |
2c382fb6 | 42 | #include "safe-ctype.h" |
252b5132 | 43 | |
252b5132 | 44 | static etree_value_type exp_fold_tree_no_dot |
1579bae1 | 45 | (etree_type *, lang_output_section_statement_type *, lang_phase_type); |
e5caa5e0 AM |
46 | static bfd_vma align_n |
47 | (bfd_vma, bfd_vma); | |
252b5132 | 48 | |
2d20f7bf JJ |
49 | struct exp_data_seg exp_data_seg; |
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; |
c7d701b0 NC |
60 | char * name; |
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, ">>" }, |
8c95a62e KH |
82 | { ALIGN_K, "ALIGN" }, |
83 | { BLOCK, "BLOCK" }, | |
c7d701b0 NC |
84 | { QUAD, "QUAD" }, |
85 | { SQUAD, "SQUAD" }, | |
86 | { LONG, "LONG" }, | |
87 | { SHORT, "SHORT" }, | |
88 | { BYTE, "BYTE" }, | |
8c95a62e KH |
89 | { SECTIONS, "SECTIONS" }, |
90 | { SIZEOF_HEADERS, "SIZEOF_HEADERS" }, | |
8c95a62e KH |
91 | { MEMORY, "MEMORY" }, |
92 | { DEFINED, "DEFINED" }, | |
93 | { TARGET_K, "TARGET" }, | |
94 | { SEARCH_DIR, "SEARCH_DIR" }, | |
95 | { MAP, "MAP" }, | |
8c95a62e | 96 | { ENTRY, "ENTRY" }, |
c7d701b0 NC |
97 | { NEXT, "NEXT" }, |
98 | { SIZEOF, "SIZEOF" }, | |
99 | { ADDR, "ADDR" }, | |
100 | { LOADADDR, "LOADADDR" }, | |
101 | { MAX_K, "MAX_K" }, | |
1049f94e | 102 | { REL, "relocatable" }, |
2d20f7bf | 103 | { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" }, |
8c37241b | 104 | { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" }, |
2d20f7bf | 105 | { DATA_SEGMENT_END, "DATA_SEGMENT_END" } |
8c95a62e | 106 | }; |
252b5132 RH |
107 | unsigned int idx; |
108 | ||
7b17f854 RS |
109 | for (idx = 0; idx < ARRAY_SIZE (table); idx++) |
110 | if (table[idx].code == code) | |
111 | break; | |
c7d701b0 | 112 | |
7b17f854 RS |
113 | if (infix_p) |
114 | fputc (' ', config.map_file); | |
115 | ||
116 | if (idx < ARRAY_SIZE (table)) | |
117 | fputs (table[idx].name, config.map_file); | |
118 | else if (code < 127) | |
119 | fputc (code, config.map_file); | |
c7d701b0 | 120 | else |
7b17f854 RS |
121 | fprintf (config.map_file, "<code %d>", code); |
122 | ||
123 | if (infix_p) | |
124 | fputc (' ', config.map_file); | |
252b5132 RH |
125 | } |
126 | ||
4de2d33d | 127 | static void |
1579bae1 | 128 | make_abs (etree_value_type *ptr) |
252b5132 | 129 | { |
8c95a62e KH |
130 | asection *s = ptr->section->bfd_section; |
131 | ptr->value += s->vma; | |
132 | ptr->section = abs_output_section; | |
252b5132 RH |
133 | } |
134 | ||
135 | static etree_value_type | |
1579bae1 | 136 | new_abs (bfd_vma value) |
252b5132 RH |
137 | { |
138 | etree_value_type new; | |
b34976b6 | 139 | new.valid_p = TRUE; |
252b5132 RH |
140 | new.section = abs_output_section; |
141 | new.value = value; | |
142 | return new; | |
143 | } | |
144 | ||
252b5132 | 145 | etree_type * |
1579bae1 | 146 | exp_intop (bfd_vma value) |
252b5132 | 147 | { |
1579bae1 | 148 | etree_type *new = stat_alloc (sizeof (new->value)); |
252b5132 RH |
149 | new->type.node_code = INT; |
150 | new->value.value = value; | |
2c382fb6 | 151 | new->value.str = NULL; |
252b5132 RH |
152 | new->type.node_class = etree_value; |
153 | return new; | |
2c382fb6 | 154 | } |
252b5132 | 155 | |
2c382fb6 | 156 | etree_type * |
1579bae1 | 157 | exp_bigintop (bfd_vma value, char *str) |
2c382fb6 | 158 | { |
1579bae1 | 159 | etree_type *new = stat_alloc (sizeof (new->value)); |
2c382fb6 AM |
160 | new->type.node_code = INT; |
161 | new->value.value = value; | |
162 | new->value.str = str; | |
163 | new->type.node_class = etree_value; | |
164 | return new; | |
252b5132 RH |
165 | } |
166 | ||
1049f94e | 167 | /* Build an expression representing an unnamed relocatable value. */ |
252b5132 RH |
168 | |
169 | etree_type * | |
1579bae1 | 170 | exp_relop (asection *section, bfd_vma value) |
252b5132 | 171 | { |
1579bae1 | 172 | etree_type *new = stat_alloc (sizeof (new->rel)); |
252b5132 RH |
173 | new->type.node_code = REL; |
174 | new->type.node_class = etree_rel; | |
175 | new->rel.section = section; | |
176 | new->rel.value = value; | |
177 | return new; | |
178 | } | |
179 | ||
180 | static etree_value_type | |
1579bae1 AM |
181 | new_rel (bfd_vma value, |
182 | char *str, | |
183 | lang_output_section_statement_type *section) | |
252b5132 RH |
184 | { |
185 | etree_value_type new; | |
b34976b6 | 186 | new.valid_p = TRUE; |
252b5132 | 187 | new.value = value; |
2c382fb6 | 188 | new.str = str; |
252b5132 RH |
189 | new.section = section; |
190 | return new; | |
191 | } | |
192 | ||
193 | static etree_value_type | |
1579bae1 AM |
194 | new_rel_from_section (bfd_vma value, |
195 | lang_output_section_statement_type *section) | |
252b5132 RH |
196 | { |
197 | etree_value_type new; | |
b34976b6 | 198 | new.valid_p = TRUE; |
252b5132 | 199 | new.value = value; |
2c382fb6 | 200 | new.str = NULL; |
252b5132 RH |
201 | new.section = section; |
202 | ||
8c95a62e | 203 | new.value -= section->bfd_section->vma; |
252b5132 RH |
204 | |
205 | return new; | |
206 | } | |
207 | ||
0ae1cf52 | 208 | static etree_value_type |
1579bae1 AM |
209 | fold_unary (etree_type *tree, |
210 | lang_output_section_statement_type *current_section, | |
211 | lang_phase_type allocation_done, | |
212 | bfd_vma dot, | |
213 | bfd_vma *dotp) | |
0ae1cf52 AM |
214 | { |
215 | etree_value_type result; | |
216 | ||
217 | result = exp_fold_tree (tree->unary.child, | |
218 | current_section, | |
219 | allocation_done, dot, dotp); | |
220 | if (result.valid_p) | |
221 | { | |
222 | switch (tree->type.node_code) | |
223 | { | |
224 | case ALIGN_K: | |
225 | if (allocation_done != lang_first_phase_enum) | |
226 | result = new_rel_from_section (align_n (dot, result.value), | |
227 | current_section); | |
228 | else | |
b34976b6 | 229 | result.valid_p = FALSE; |
0ae1cf52 AM |
230 | break; |
231 | ||
232 | case ABSOLUTE: | |
233 | if (allocation_done != lang_first_phase_enum) | |
234 | { | |
235 | result.value += result.section->bfd_section->vma; | |
236 | result.section = abs_output_section; | |
237 | } | |
238 | else | |
b34976b6 | 239 | result.valid_p = FALSE; |
0ae1cf52 AM |
240 | break; |
241 | ||
242 | case '~': | |
243 | make_abs (&result); | |
244 | result.value = ~result.value; | |
245 | break; | |
246 | ||
247 | case '!': | |
248 | make_abs (&result); | |
249 | result.value = !result.value; | |
250 | break; | |
251 | ||
252 | case '-': | |
253 | make_abs (&result); | |
254 | result.value = -result.value; | |
255 | break; | |
256 | ||
257 | case NEXT: | |
258 | /* Return next place aligned to value. */ | |
259 | if (allocation_done == lang_allocating_phase_enum) | |
260 | { | |
261 | make_abs (&result); | |
262 | result.value = align_n (dot, result.value); | |
263 | } | |
264 | else | |
b34976b6 | 265 | result.valid_p = FALSE; |
0ae1cf52 AM |
266 | break; |
267 | ||
8c37241b JJ |
268 | case DATA_SEGMENT_RELRO_END: |
269 | if (allocation_done != lang_first_phase_enum | |
270 | && (exp_data_seg.phase == exp_dataseg_align_seen | |
271 | || exp_data_seg.phase == exp_dataseg_adjust | |
272 | || exp_data_seg.phase == exp_dataseg_relro_adjust | |
273 | || allocation_done != lang_allocating_phase_enum)) | |
274 | { | |
275 | if (exp_data_seg.phase == exp_dataseg_align_seen | |
276 | || exp_data_seg.phase == exp_dataseg_relro_adjust) | |
277 | exp_data_seg.relro_end | |
278 | = result.value + current_section->bfd_section->vma; | |
279 | if (exp_data_seg.phase == exp_dataseg_align_seen) | |
280 | exp_data_seg.phase = exp_dataseg_relro_seen; | |
281 | result.value = dot - current_section->bfd_section->vma; | |
282 | } | |
283 | else | |
284 | result.valid_p = FALSE; | |
285 | break; | |
286 | ||
0ae1cf52 AM |
287 | case DATA_SEGMENT_END: |
288 | if (allocation_done != lang_first_phase_enum | |
289 | && current_section == abs_output_section | |
290 | && (exp_data_seg.phase == exp_dataseg_align_seen | |
8c37241b | 291 | || exp_data_seg.phase == exp_dataseg_relro_seen |
0ae1cf52 | 292 | || exp_data_seg.phase == exp_dataseg_adjust |
8c37241b | 293 | || exp_data_seg.phase == exp_dataseg_relro_adjust |
0ae1cf52 AM |
294 | || allocation_done != lang_allocating_phase_enum)) |
295 | { | |
8c37241b JJ |
296 | if (exp_data_seg.phase == exp_dataseg_align_seen |
297 | || exp_data_seg.phase == exp_dataseg_relro_seen) | |
0ae1cf52 AM |
298 | { |
299 | exp_data_seg.phase = exp_dataseg_end_seen; | |
300 | exp_data_seg.end = result.value; | |
301 | } | |
302 | } | |
303 | else | |
b34976b6 | 304 | result.valid_p = FALSE; |
0ae1cf52 AM |
305 | break; |
306 | ||
307 | default: | |
308 | FAIL (); | |
309 | break; | |
310 | } | |
311 | } | |
312 | ||
313 | return result; | |
314 | } | |
315 | ||
4de2d33d | 316 | static etree_value_type |
1579bae1 AM |
317 | fold_binary (etree_type *tree, |
318 | lang_output_section_statement_type *current_section, | |
319 | lang_phase_type allocation_done, | |
320 | bfd_vma dot, | |
321 | bfd_vma *dotp) | |
252b5132 RH |
322 | { |
323 | etree_value_type result; | |
324 | ||
325 | result = exp_fold_tree (tree->binary.lhs, current_section, | |
326 | allocation_done, dot, dotp); | |
327 | if (result.valid_p) | |
328 | { | |
329 | etree_value_type other; | |
330 | ||
331 | other = exp_fold_tree (tree->binary.rhs, | |
332 | current_section, | |
8c95a62e | 333 | allocation_done, dot, dotp); |
252b5132 RH |
334 | if (other.valid_p) |
335 | { | |
336 | /* If the values are from different sections, or this is an | |
337 | absolute expression, make both the source arguments | |
338 | absolute. However, adding or subtracting an absolute | |
339 | value from a relative value is meaningful, and is an | |
340 | exception. */ | |
341 | if (current_section != abs_output_section | |
342 | && (other.section == abs_output_section | |
343 | || (result.section == abs_output_section | |
344 | && tree->type.node_code == '+')) | |
345 | && (tree->type.node_code == '+' | |
346 | || tree->type.node_code == '-')) | |
347 | { | |
252b5132 RH |
348 | if (other.section != abs_output_section) |
349 | { | |
0ae1cf52 AM |
350 | /* Keep the section of the other term. */ |
351 | if (tree->type.node_code == '+') | |
352 | other.value = result.value + other.value; | |
353 | else | |
354 | other.value = result.value - other.value; | |
355 | return other; | |
252b5132 RH |
356 | } |
357 | } | |
358 | else if (result.section != other.section | |
359 | || current_section == abs_output_section) | |
360 | { | |
8c95a62e KH |
361 | make_abs (&result); |
362 | make_abs (&other); | |
252b5132 RH |
363 | } |
364 | ||
4de2d33d | 365 | switch (tree->type.node_code) |
252b5132 RH |
366 | { |
367 | case '%': | |
368 | if (other.value == 0) | |
369 | einfo (_("%F%S %% by zero\n")); | |
370 | result.value = ((bfd_signed_vma) result.value | |
371 | % (bfd_signed_vma) other.value); | |
372 | break; | |
373 | ||
374 | case '/': | |
375 | if (other.value == 0) | |
376 | einfo (_("%F%S / by zero\n")); | |
377 | result.value = ((bfd_signed_vma) result.value | |
378 | / (bfd_signed_vma) other.value); | |
379 | break; | |
380 | ||
381 | #define BOP(x,y) case x : result.value = result.value y other.value; break; | |
8c95a62e KH |
382 | BOP ('+', +); |
383 | BOP ('*', *); | |
384 | BOP ('-', -); | |
385 | BOP (LSHIFT, <<); | |
386 | BOP (RSHIFT, >>); | |
387 | BOP (EQ, ==); | |
388 | BOP (NE, !=); | |
389 | BOP ('<', <); | |
390 | BOP ('>', >); | |
391 | BOP (LE, <=); | |
392 | BOP (GE, >=); | |
393 | BOP ('&', &); | |
394 | BOP ('^', ^); | |
395 | BOP ('|', |); | |
396 | BOP (ANDAND, &&); | |
397 | BOP (OROR, ||); | |
252b5132 RH |
398 | |
399 | case MAX_K: | |
400 | if (result.value < other.value) | |
401 | result = other; | |
402 | break; | |
403 | ||
404 | case MIN_K: | |
405 | if (result.value > other.value) | |
406 | result = other; | |
407 | break; | |
408 | ||
876f4090 NS |
409 | case ALIGN_K: |
410 | result.value = align_n (result.value, other.value); | |
411 | break; | |
412 | ||
2d20f7bf JJ |
413 | case DATA_SEGMENT_ALIGN: |
414 | if (allocation_done != lang_first_phase_enum | |
415 | && current_section == abs_output_section | |
416 | && (exp_data_seg.phase == exp_dataseg_none | |
417 | || exp_data_seg.phase == exp_dataseg_adjust | |
8c37241b | 418 | || exp_data_seg.phase == exp_dataseg_relro_adjust |
2d20f7bf JJ |
419 | || allocation_done != lang_allocating_phase_enum)) |
420 | { | |
421 | bfd_vma maxpage = result.value; | |
422 | ||
c553bb91 | 423 | result.value = align_n (dot, maxpage); |
8c37241b JJ |
424 | if (exp_data_seg.phase == exp_dataseg_relro_adjust) |
425 | { | |
426 | /* Attempt to align DATA_SEGMENT_RELRO_END at | |
427 | a common page boundary. */ | |
428 | bfd_vma relro; | |
429 | ||
875c4330 | 430 | result.value += dot & (maxpage - 1); |
8c37241b JJ |
431 | relro = exp_data_seg.relro_end - exp_data_seg.base; |
432 | result.value += -relro & (other.value - 1); | |
433 | exp_data_seg.base = result.value; | |
434 | } | |
435 | else if (exp_data_seg.phase != exp_dataseg_adjust) | |
2d20f7bf JJ |
436 | { |
437 | result.value += dot & (maxpage - 1); | |
438 | if (allocation_done == lang_allocating_phase_enum) | |
439 | { | |
440 | exp_data_seg.phase = exp_dataseg_align_seen; | |
441 | exp_data_seg.base = result.value; | |
442 | exp_data_seg.pagesize = other.value; | |
8c37241b | 443 | exp_data_seg.relro_end = 0; |
2d20f7bf JJ |
444 | } |
445 | } | |
446 | else if (other.value < maxpage) | |
50e60fb5 JJ |
447 | result.value += (dot + other.value - 1) |
448 | & (maxpage - other.value); | |
2d20f7bf JJ |
449 | } |
450 | else | |
b34976b6 | 451 | result.valid_p = FALSE; |
2d20f7bf JJ |
452 | break; |
453 | ||
252b5132 | 454 | default: |
8c95a62e | 455 | FAIL (); |
252b5132 RH |
456 | } |
457 | } | |
458 | else | |
459 | { | |
b34976b6 | 460 | result.valid_p = FALSE; |
252b5132 RH |
461 | } |
462 | } | |
463 | ||
464 | return result; | |
465 | } | |
466 | ||
0ae1cf52 | 467 | static etree_value_type |
1579bae1 AM |
468 | fold_trinary (etree_type *tree, |
469 | lang_output_section_statement_type *current_section, | |
470 | lang_phase_type allocation_done, | |
471 | bfd_vma dot, | |
472 | bfd_vma *dotp) | |
0ae1cf52 AM |
473 | { |
474 | etree_value_type result; | |
475 | ||
476 | result = exp_fold_tree (tree->trinary.cond, current_section, | |
477 | allocation_done, dot, dotp); | |
478 | if (result.valid_p) | |
479 | result = exp_fold_tree ((result.value | |
480 | ? tree->trinary.lhs | |
481 | : tree->trinary.rhs), | |
482 | current_section, | |
483 | allocation_done, dot, dotp); | |
484 | ||
485 | return result; | |
486 | } | |
487 | ||
4de2d33d | 488 | static etree_value_type |
1579bae1 AM |
489 | fold_name (etree_type *tree, |
490 | lang_output_section_statement_type *current_section, | |
491 | lang_phase_type allocation_done, | |
492 | bfd_vma dot) | |
252b5132 RH |
493 | { |
494 | etree_value_type result; | |
c7d701b0 | 495 | |
1b493742 NS |
496 | result.valid_p = FALSE; |
497 | ||
4de2d33d | 498 | switch (tree->type.node_code) |
8c95a62e KH |
499 | { |
500 | case SIZEOF_HEADERS: | |
501 | if (allocation_done != lang_first_phase_enum) | |
1b493742 NS |
502 | result = new_abs (bfd_sizeof_headers (output_bfd, |
503 | link_info.relocatable)); | |
8c95a62e KH |
504 | break; |
505 | case DEFINED: | |
506 | if (allocation_done == lang_first_phase_enum) | |
1b493742 | 507 | lang_track_definedness (tree->name.name); |
8c95a62e KH |
508 | else |
509 | { | |
510 | struct bfd_link_hash_entry *h; | |
420e579c HPN |
511 | int def_iteration |
512 | = lang_symbol_definition_iteration (tree->name.name); | |
8c95a62e KH |
513 | |
514 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
515 | tree->name.name, | |
b34976b6 | 516 | FALSE, FALSE, TRUE); |
1579bae1 | 517 | result.value = (h != NULL |
8c95a62e KH |
518 | && (h->type == bfd_link_hash_defined |
519 | || h->type == bfd_link_hash_defweak | |
420e579c HPN |
520 | || h->type == bfd_link_hash_common) |
521 | && (def_iteration == lang_statement_iteration | |
522 | || def_iteration == -1)); | |
6a33aff4 | 523 | result.section = abs_output_section; |
b34976b6 | 524 | result.valid_p = TRUE; |
8c95a62e KH |
525 | } |
526 | break; | |
527 | case NAME: | |
8c95a62e KH |
528 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) |
529 | { | |
530 | if (allocation_done != lang_first_phase_enum) | |
531 | result = new_rel_from_section (dot, current_section); | |
8c95a62e KH |
532 | } |
533 | else if (allocation_done != lang_first_phase_enum) | |
534 | { | |
535 | struct bfd_link_hash_entry *h; | |
536 | ||
537 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
538 | tree->name.name, | |
1b493742 NS |
539 | TRUE, FALSE, TRUE); |
540 | if (!h) | |
541 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); | |
542 | else if (h->type == bfd_link_hash_defined | |
543 | || h->type == bfd_link_hash_defweak) | |
8c95a62e KH |
544 | { |
545 | if (bfd_is_abs_section (h->u.def.section)) | |
546 | result = new_abs (h->u.def.value); | |
547 | else if (allocation_done == lang_final_phase_enum | |
548 | || allocation_done == lang_allocating_phase_enum) | |
549 | { | |
550 | asection *output_section; | |
551 | ||
552 | output_section = h->u.def.section->output_section; | |
553 | if (output_section == NULL) | |
554 | einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"), | |
555 | tree->name.name); | |
556 | else | |
557 | { | |
558 | lang_output_section_statement_type *os; | |
559 | ||
560 | os = (lang_output_section_statement_lookup | |
561 | (bfd_get_section_name (output_bfd, | |
562 | output_section))); | |
563 | ||
564 | /* FIXME: Is this correct if this section is | |
565 | being linked with -R? */ | |
566 | result = new_rel ((h->u.def.value | |
567 | + h->u.def.section->output_offset), | |
2c382fb6 | 568 | NULL, |
8c95a62e KH |
569 | os); |
570 | } | |
571 | } | |
572 | } | |
573 | else if (allocation_done == lang_final_phase_enum) | |
574 | einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"), | |
575 | tree->name.name); | |
1b493742 NS |
576 | else if (h->type == bfd_link_hash_new) |
577 | { | |
578 | h->type = bfd_link_hash_undefined; | |
579 | h->u.undef.abfd = NULL; | |
f6e332e6 | 580 | if (h->u.undef.next == NULL) |
a010d60f | 581 | bfd_link_add_undef (link_info.hash, h); |
1b493742 | 582 | } |
8c95a62e KH |
583 | } |
584 | break; | |
585 | ||
586 | case ADDR: | |
587 | if (allocation_done != lang_first_phase_enum) | |
588 | { | |
589 | lang_output_section_statement_type *os; | |
590 | ||
591 | os = lang_output_section_find (tree->name.name); | |
1b493742 NS |
592 | if (os && os->processed > 0) |
593 | result = new_rel (0, NULL, os); | |
8c95a62e | 594 | } |
8c95a62e KH |
595 | break; |
596 | ||
597 | case LOADADDR: | |
598 | if (allocation_done != lang_first_phase_enum) | |
599 | { | |
600 | lang_output_section_statement_type *os; | |
601 | ||
602 | os = lang_output_section_find (tree->name.name); | |
1b493742 NS |
603 | if (os && os->processed != 0) |
604 | { | |
605 | if (os->load_base == NULL) | |
606 | result = new_rel (0, NULL, os); | |
607 | else | |
608 | result = exp_fold_tree_no_dot (os->load_base, | |
609 | abs_output_section, | |
610 | allocation_done); | |
611 | } | |
8c95a62e | 612 | } |
8c95a62e KH |
613 | break; |
614 | ||
615 | case SIZEOF: | |
616 | if (allocation_done != lang_first_phase_enum) | |
617 | { | |
618 | int opb = bfd_octets_per_byte (output_bfd); | |
619 | lang_output_section_statement_type *os; | |
620 | ||
621 | os = lang_output_section_find (tree->name.name); | |
1b493742 | 622 | if (os && os->processed > 0) |
eea6121a | 623 | result = new_abs (os->bfd_section->size / opb); |
8c95a62e | 624 | } |
8c95a62e KH |
625 | break; |
626 | ||
627 | default: | |
628 | FAIL (); | |
629 | break; | |
630 | } | |
252b5132 RH |
631 | |
632 | return result; | |
633 | } | |
8c95a62e | 634 | |
4de2d33d | 635 | etree_value_type |
1579bae1 AM |
636 | exp_fold_tree (etree_type *tree, |
637 | lang_output_section_statement_type *current_section, | |
638 | lang_phase_type allocation_done, | |
639 | bfd_vma dot, | |
640 | bfd_vma *dotp) | |
252b5132 RH |
641 | { |
642 | etree_value_type result; | |
643 | ||
644 | if (tree == NULL) | |
645 | { | |
b34976b6 | 646 | result.valid_p = FALSE; |
252b5132 RH |
647 | return result; |
648 | } | |
649 | ||
4de2d33d | 650 | switch (tree->type.node_class) |
252b5132 RH |
651 | { |
652 | case etree_value: | |
2c382fb6 | 653 | result = new_rel (tree->value.value, tree->value.str, current_section); |
252b5132 RH |
654 | break; |
655 | ||
656 | case etree_rel: | |
657 | if (allocation_done != lang_final_phase_enum) | |
b34976b6 | 658 | result.valid_p = FALSE; |
252b5132 RH |
659 | else |
660 | result = new_rel ((tree->rel.value | |
661 | + tree->rel.section->output_section->vma | |
662 | + tree->rel.section->output_offset), | |
2c382fb6 | 663 | NULL, |
252b5132 RH |
664 | current_section); |
665 | break; | |
666 | ||
667 | case etree_assert: | |
668 | result = exp_fold_tree (tree->assert_s.child, | |
8c95a62e KH |
669 | current_section, |
670 | allocation_done, dot, dotp); | |
252b5132 RH |
671 | if (result.valid_p) |
672 | { | |
673 | if (! result.value) | |
674 | einfo ("%F%P: %s\n", tree->assert_s.message); | |
675 | return result; | |
676 | } | |
677 | break; | |
678 | ||
679 | case etree_unary: | |
0ae1cf52 AM |
680 | result = fold_unary (tree, current_section, allocation_done, |
681 | dot, dotp); | |
252b5132 RH |
682 | break; |
683 | ||
684 | case etree_binary: | |
685 | result = fold_binary (tree, current_section, allocation_done, | |
686 | dot, dotp); | |
687 | break; | |
0ae1cf52 AM |
688 | |
689 | case etree_trinary: | |
690 | result = fold_trinary (tree, current_section, allocation_done, | |
691 | dot, dotp); | |
692 | break; | |
252b5132 RH |
693 | |
694 | case etree_assign: | |
695 | case etree_provide: | |
b46a87b1 | 696 | case etree_provided: |
252b5132 RH |
697 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) |
698 | { | |
c7d701b0 | 699 | /* Assignment to dot can only be done during allocation. */ |
b46a87b1 | 700 | if (tree->type.node_class != etree_assign) |
252b5132 RH |
701 | einfo (_("%F%S can not PROVIDE assignment to location counter\n")); |
702 | if (allocation_done == lang_allocating_phase_enum | |
703 | || (allocation_done == lang_final_phase_enum | |
704 | && current_section == abs_output_section)) | |
705 | { | |
706 | result = exp_fold_tree (tree->assign.src, | |
707 | current_section, | |
2d20f7bf | 708 | allocation_done, dot, |
252b5132 RH |
709 | dotp); |
710 | if (! result.valid_p) | |
711 | einfo (_("%F%S invalid assignment to location counter\n")); | |
712 | else | |
713 | { | |
714 | if (current_section == NULL) | |
715 | einfo (_("%F%S assignment to location counter invalid outside of SECTION\n")); | |
716 | else | |
717 | { | |
718 | bfd_vma nextdot; | |
719 | ||
720 | nextdot = (result.value | |
721 | + current_section->bfd_section->vma); | |
722 | if (nextdot < dot | |
723 | && current_section != abs_output_section) | |
c7d701b0 NC |
724 | einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"), |
725 | dot, nextdot); | |
252b5132 | 726 | else |
4de2d33d | 727 | *dotp = nextdot; |
252b5132 RH |
728 | } |
729 | } | |
730 | } | |
731 | } | |
732 | else | |
733 | { | |
734 | result = exp_fold_tree (tree->assign.src, | |
735 | current_section, allocation_done, | |
736 | dot, dotp); | |
737 | if (result.valid_p) | |
738 | { | |
b34976b6 | 739 | bfd_boolean create; |
252b5132 RH |
740 | struct bfd_link_hash_entry *h; |
741 | ||
742 | if (tree->type.node_class == etree_assign) | |
b34976b6 | 743 | create = TRUE; |
252b5132 | 744 | else |
b34976b6 | 745 | create = FALSE; |
252b5132 | 746 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, |
1b493742 | 747 | create, FALSE, TRUE); |
1579bae1 | 748 | if (h == NULL) |
67010b46 | 749 | { |
1b493742 | 750 | if (create) |
67010b46 NC |
751 | einfo (_("%P%F:%s: hash creation failed\n"), |
752 | tree->assign.dst); | |
753 | } | |
754 | else if (tree->type.node_class == etree_provide | |
1b493742 | 755 | && h->type != bfd_link_hash_new |
67010b46 NC |
756 | && h->type != bfd_link_hash_undefined |
757 | && h->type != bfd_link_hash_common) | |
758 | { | |
759 | /* Do nothing. The symbol was defined by some | |
760 | object. */ | |
761 | } | |
252b5132 RH |
762 | else |
763 | { | |
764 | /* FIXME: Should we worry if the symbol is already | |
765 | defined? */ | |
420e579c | 766 | lang_update_definedness (tree->assign.dst, h); |
252b5132 RH |
767 | h->type = bfd_link_hash_defined; |
768 | h->u.def.value = result.value; | |
769 | h->u.def.section = result.section->bfd_section; | |
b46a87b1 L |
770 | if (tree->type.node_class == etree_provide) |
771 | tree->type.node_class = etree_provided; | |
252b5132 RH |
772 | } |
773 | } | |
774 | } | |
775 | break; | |
776 | ||
777 | case etree_name: | |
778 | result = fold_name (tree, current_section, allocation_done, dot); | |
779 | break; | |
780 | ||
781 | default: | |
782 | FAIL (); | |
783 | break; | |
784 | } | |
785 | ||
786 | return result; | |
787 | } | |
788 | ||
4de2d33d | 789 | static etree_value_type |
1579bae1 AM |
790 | exp_fold_tree_no_dot (etree_type *tree, |
791 | lang_output_section_statement_type *current_section, | |
792 | lang_phase_type allocation_done) | |
252b5132 | 793 | { |
1579bae1 | 794 | return exp_fold_tree (tree, current_section, allocation_done, 0, NULL); |
252b5132 RH |
795 | } |
796 | ||
797 | etree_type * | |
1579bae1 | 798 | exp_binop (int code, etree_type *lhs, etree_type *rhs) |
252b5132 RH |
799 | { |
800 | etree_type value, *new; | |
801 | etree_value_type r; | |
802 | ||
803 | value.type.node_code = code; | |
804 | value.binary.lhs = lhs; | |
805 | value.binary.rhs = rhs; | |
806 | value.type.node_class = etree_binary; | |
8c95a62e KH |
807 | r = exp_fold_tree_no_dot (&value, |
808 | abs_output_section, | |
809 | lang_first_phase_enum); | |
252b5132 RH |
810 | if (r.valid_p) |
811 | { | |
8c95a62e | 812 | return exp_intop (r.value); |
252b5132 | 813 | } |
1579bae1 AM |
814 | new = stat_alloc (sizeof (new->binary)); |
815 | memcpy (new, &value, sizeof (new->binary)); | |
252b5132 RH |
816 | return new; |
817 | } | |
818 | ||
819 | etree_type * | |
1579bae1 | 820 | exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs) |
252b5132 RH |
821 | { |
822 | etree_type value, *new; | |
823 | etree_value_type r; | |
824 | value.type.node_code = code; | |
825 | value.trinary.lhs = lhs; | |
826 | value.trinary.cond = cond; | |
827 | value.trinary.rhs = rhs; | |
828 | value.type.node_class = etree_trinary; | |
1579bae1 | 829 | r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum); |
8c95a62e | 830 | if (r.valid_p) |
c7d701b0 NC |
831 | return exp_intop (r.value); |
832 | ||
1579bae1 AM |
833 | new = stat_alloc (sizeof (new->trinary)); |
834 | memcpy (new, &value, sizeof (new->trinary)); | |
252b5132 RH |
835 | return new; |
836 | } | |
837 | ||
252b5132 | 838 | etree_type * |
1579bae1 | 839 | exp_unop (int code, etree_type *child) |
252b5132 RH |
840 | { |
841 | etree_type value, *new; | |
842 | ||
843 | etree_value_type r; | |
844 | value.unary.type.node_code = code; | |
845 | value.unary.child = child; | |
846 | value.unary.type.node_class = etree_unary; | |
8c95a62e KH |
847 | r = exp_fold_tree_no_dot (&value, abs_output_section, |
848 | lang_first_phase_enum); | |
849 | if (r.valid_p) | |
c7d701b0 NC |
850 | return exp_intop (r.value); |
851 | ||
1579bae1 AM |
852 | new = stat_alloc (sizeof (new->unary)); |
853 | memcpy (new, &value, sizeof (new->unary)); | |
252b5132 RH |
854 | return new; |
855 | } | |
856 | ||
252b5132 | 857 | etree_type * |
1579bae1 | 858 | exp_nameop (int code, const char *name) |
252b5132 RH |
859 | { |
860 | etree_type value, *new; | |
861 | etree_value_type r; | |
862 | value.name.type.node_code = code; | |
863 | value.name.name = name; | |
864 | value.name.type.node_class = etree_name; | |
865 | ||
1579bae1 | 866 | r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum); |
8c95a62e | 867 | if (r.valid_p) |
c7d701b0 NC |
868 | return exp_intop (r.value); |
869 | ||
1579bae1 AM |
870 | new = stat_alloc (sizeof (new->name)); |
871 | memcpy (new, &value, sizeof (new->name)); | |
252b5132 RH |
872 | return new; |
873 | ||
874 | } | |
875 | ||
252b5132 | 876 | etree_type * |
1579bae1 | 877 | exp_assop (int code, const char *dst, etree_type *src) |
252b5132 RH |
878 | { |
879 | etree_type value, *new; | |
880 | ||
881 | value.assign.type.node_code = code; | |
882 | ||
252b5132 RH |
883 | value.assign.src = src; |
884 | value.assign.dst = dst; | |
885 | value.assign.type.node_class = etree_assign; | |
886 | ||
887 | #if 0 | |
8c95a62e | 888 | if (exp_fold_tree_no_dot (&value, &result)) |
c7d701b0 | 889 | return exp_intop (result); |
252b5132 | 890 | #endif |
1579bae1 AM |
891 | new = stat_alloc (sizeof (new->assign)); |
892 | memcpy (new, &value, sizeof (new->assign)); | |
252b5132 RH |
893 | return new; |
894 | } | |
895 | ||
896 | /* Handle PROVIDE. */ | |
897 | ||
898 | etree_type * | |
1579bae1 | 899 | exp_provide (const char *dst, etree_type *src) |
252b5132 RH |
900 | { |
901 | etree_type *n; | |
902 | ||
1579bae1 | 903 | n = stat_alloc (sizeof (n->assign)); |
252b5132 RH |
904 | n->assign.type.node_code = '='; |
905 | n->assign.type.node_class = etree_provide; | |
906 | n->assign.src = src; | |
907 | n->assign.dst = dst; | |
908 | return n; | |
909 | } | |
910 | ||
911 | /* Handle ASSERT. */ | |
912 | ||
913 | etree_type * | |
1579bae1 | 914 | exp_assert (etree_type *exp, const char *message) |
252b5132 RH |
915 | { |
916 | etree_type *n; | |
917 | ||
1579bae1 | 918 | n = stat_alloc (sizeof (n->assert_s)); |
252b5132 RH |
919 | n->assert_s.type.node_code = '!'; |
920 | n->assert_s.type.node_class = etree_assert; | |
921 | n->assert_s.child = exp; | |
922 | n->assert_s.message = message; | |
923 | return n; | |
924 | } | |
925 | ||
4de2d33d | 926 | void |
1579bae1 | 927 | exp_print_tree (etree_type *tree) |
252b5132 | 928 | { |
c7d701b0 NC |
929 | if (config.map_file == NULL) |
930 | config.map_file = stderr; | |
b7a26f91 | 931 | |
c7d701b0 NC |
932 | if (tree == NULL) |
933 | { | |
934 | minfo ("NULL TREE\n"); | |
935 | return; | |
936 | } | |
b7a26f91 | 937 | |
8c95a62e KH |
938 | switch (tree->type.node_class) |
939 | { | |
940 | case etree_value: | |
941 | minfo ("0x%v", tree->value.value); | |
942 | return; | |
943 | case etree_rel: | |
944 | if (tree->rel.section->owner != NULL) | |
945 | minfo ("%B:", tree->rel.section->owner); | |
946 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); | |
947 | return; | |
948 | case etree_assign: | |
252b5132 | 949 | #if 0 |
1579bae1 | 950 | if (tree->assign.dst->sdefs != NULL) |
c7d701b0 NC |
951 | fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name, |
952 | tree->assign.dst->sdefs->value); | |
8c95a62e | 953 | else |
c7d701b0 | 954 | fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name); |
252b5132 | 955 | #endif |
8c95a62e | 956 | fprintf (config.map_file, "%s", tree->assign.dst); |
b34976b6 | 957 | exp_print_token (tree->type.node_code, TRUE); |
8c95a62e KH |
958 | exp_print_tree (tree->assign.src); |
959 | break; | |
960 | case etree_provide: | |
b46a87b1 | 961 | case etree_provided: |
8c95a62e KH |
962 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); |
963 | exp_print_tree (tree->assign.src); | |
964 | fprintf (config.map_file, ")"); | |
965 | break; | |
966 | case etree_binary: | |
967 | fprintf (config.map_file, "("); | |
968 | exp_print_tree (tree->binary.lhs); | |
b34976b6 | 969 | exp_print_token (tree->type.node_code, TRUE); |
8c95a62e KH |
970 | exp_print_tree (tree->binary.rhs); |
971 | fprintf (config.map_file, ")"); | |
972 | break; | |
973 | case etree_trinary: | |
974 | exp_print_tree (tree->trinary.cond); | |
975 | fprintf (config.map_file, "?"); | |
976 | exp_print_tree (tree->trinary.lhs); | |
977 | fprintf (config.map_file, ":"); | |
978 | exp_print_tree (tree->trinary.rhs); | |
979 | break; | |
980 | case etree_unary: | |
b34976b6 | 981 | exp_print_token (tree->unary.type.node_code, FALSE); |
8c95a62e KH |
982 | if (tree->unary.child) |
983 | { | |
7b17f854 | 984 | fprintf (config.map_file, " ("); |
8c95a62e KH |
985 | exp_print_tree (tree->unary.child); |
986 | fprintf (config.map_file, ")"); | |
987 | } | |
988 | break; | |
989 | ||
990 | case etree_assert: | |
991 | fprintf (config.map_file, "ASSERT ("); | |
992 | exp_print_tree (tree->assert_s.child); | |
993 | fprintf (config.map_file, ", %s)", tree->assert_s.message); | |
994 | break; | |
995 | ||
996 | case etree_undef: | |
997 | fprintf (config.map_file, "????????"); | |
998 | break; | |
999 | case etree_name: | |
1000 | if (tree->type.node_code == NAME) | |
1001 | { | |
1002 | fprintf (config.map_file, "%s", tree->name.name); | |
1003 | } | |
1004 | else | |
1005 | { | |
b34976b6 | 1006 | exp_print_token (tree->type.node_code, FALSE); |
8c95a62e | 1007 | if (tree->name.name) |
7b17f854 | 1008 | fprintf (config.map_file, " (%s)", tree->name.name); |
8c95a62e KH |
1009 | } |
1010 | break; | |
1011 | default: | |
1012 | FAIL (); | |
1013 | break; | |
252b5132 | 1014 | } |
252b5132 RH |
1015 | } |
1016 | ||
1017 | bfd_vma | |
1579bae1 AM |
1018 | exp_get_vma (etree_type *tree, |
1019 | bfd_vma def, | |
1020 | char *name, | |
1021 | lang_phase_type allocation_done) | |
252b5132 RH |
1022 | { |
1023 | etree_value_type r; | |
1024 | ||
1025 | if (tree != NULL) | |
1026 | { | |
1027 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1028 | if (! r.valid_p && name != NULL) | |
1029 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1030 | return r.value; | |
1031 | } | |
1032 | else | |
1033 | return def; | |
1034 | } | |
1035 | ||
4de2d33d | 1036 | int |
1579bae1 AM |
1037 | exp_get_value_int (etree_type *tree, |
1038 | int def, | |
1039 | char *name, | |
1040 | lang_phase_type allocation_done) | |
252b5132 | 1041 | { |
1579bae1 | 1042 | return exp_get_vma (tree, def, name, allocation_done); |
252b5132 RH |
1043 | } |
1044 | ||
2c382fb6 | 1045 | fill_type * |
1579bae1 AM |
1046 | exp_get_fill (etree_type *tree, |
1047 | fill_type *def, | |
1048 | char *name, | |
1049 | lang_phase_type allocation_done) | |
2c382fb6 AM |
1050 | { |
1051 | fill_type *fill; | |
1052 | etree_value_type r; | |
1053 | size_t len; | |
1054 | unsigned int val; | |
1055 | ||
1056 | if (tree == NULL) | |
1057 | return def; | |
1058 | ||
1059 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1060 | if (! r.valid_p && name != NULL) | |
1061 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1062 | ||
1063 | if (r.str != NULL && (len = strlen (r.str)) != 0) | |
1064 | { | |
1065 | unsigned char *dst; | |
1066 | unsigned char *s; | |
1579bae1 | 1067 | fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1); |
2c382fb6 AM |
1068 | fill->size = (len + 1) / 2; |
1069 | dst = fill->data; | |
1070 | s = r.str; | |
1071 | val = 0; | |
1072 | do | |
1073 | { | |
1074 | unsigned int digit; | |
1075 | ||
1076 | digit = *s++ - '0'; | |
1077 | if (digit > 9) | |
1078 | digit = (digit - 'A' + '0' + 10) & 0xf; | |
1079 | val <<= 4; | |
1080 | val += digit; | |
1081 | --len; | |
1082 | if ((len & 1) == 0) | |
1083 | { | |
1084 | *dst++ = val; | |
1085 | val = 0; | |
1086 | } | |
1087 | } | |
1088 | while (len != 0); | |
1089 | } | |
1090 | else | |
1091 | { | |
1579bae1 | 1092 | fill = xmalloc (4 + sizeof (*fill) - 1); |
2c382fb6 AM |
1093 | val = r.value; |
1094 | fill->data[0] = (val >> 24) & 0xff; | |
1095 | fill->data[1] = (val >> 16) & 0xff; | |
1096 | fill->data[2] = (val >> 8) & 0xff; | |
1097 | fill->data[3] = (val >> 0) & 0xff; | |
1098 | fill->size = 4; | |
1099 | } | |
1100 | return fill; | |
1101 | } | |
1102 | ||
252b5132 | 1103 | bfd_vma |
1579bae1 AM |
1104 | exp_get_abs_int (etree_type *tree, |
1105 | int def ATTRIBUTE_UNUSED, | |
1106 | char *name, | |
1107 | lang_phase_type allocation_done) | |
252b5132 RH |
1108 | { |
1109 | etree_value_type res; | |
1110 | res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1111 | ||
1112 | if (res.valid_p) | |
c7d701b0 | 1113 | res.value += res.section->bfd_section->vma; |
8c95a62e | 1114 | else |
c7d701b0 NC |
1115 | einfo (_("%F%S non constant expression for %s\n"), name); |
1116 | ||
252b5132 RH |
1117 | return res.value; |
1118 | } | |
c553bb91 | 1119 | |
e5caa5e0 AM |
1120 | static bfd_vma |
1121 | align_n (bfd_vma value, bfd_vma align) | |
c553bb91 AM |
1122 | { |
1123 | if (align <= 1) | |
1124 | return value; | |
1125 | ||
1126 | value = (value + align - 1) / align; | |
1127 | return value * align; | |
1128 | } |