Fully implement the SECTIONS clause.
[deliverable/binutils-gdb.git] / gold / expression.cc
1 // expression.cc -- expressions in linker scripts for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <string>
26
27 #include "parameters.h"
28 #include "symtab.h"
29 #include "layout.h"
30 #include "output.h"
31 #include "script.h"
32 #include "script-c.h"
33
34 namespace gold
35 {
36
37 // This file holds the code which handles linker expressions.
38
39 // The dot symbol, which linker scripts refer to simply as ".",
40 // requires special treatment. The dot symbol is set several times,
41 // section addresses will refer to it, output sections will change it,
42 // and it can be set based on the value of other symbols. We simplify
43 // the handling by prohibiting setting the dot symbol to the value of
44 // a non-absolute symbol.
45
46 // When evaluating the value of an expression, we pass in a pointer to
47 // this struct, so that the expression evaluation can find the
48 // information it needs.
49
50 struct Expression::Expression_eval_info
51 {
52 // The symbol table.
53 const Symbol_table* symtab;
54 // The layout--we use this to get section information.
55 const Layout* layout;
56 // Whether expressions can refer to the dot symbol. The dot symbol
57 // is only available within a SECTIONS clause.
58 bool is_dot_available;
59 // Whether the dot symbol currently has a value.
60 bool dot_has_value;
61 // The current value of the dot symbol.
62 uint64_t dot_value;
63 // Points to the IS_ABSOLUTE variable, which is set to false if the
64 // expression uses a value which is not absolute.
65 bool* is_absolute;
66 };
67
68 // Evaluate an expression.
69
70 uint64_t
71 Expression::eval(const Symbol_table* symtab, const Layout* layout)
72 {
73 bool dummy;
74 return this->eval_maybe_dot(symtab, layout, false, false, 0, &dummy);
75 }
76
77 // Evaluate an expression which may refer to the dot symbol.
78
79 uint64_t
80 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
81 bool dot_has_value, uint64_t dot_value,
82 bool* is_absolute)
83 {
84 return this->eval_maybe_dot(symtab, layout, true, dot_has_value, dot_value,
85 is_absolute);
86 }
87
88 // Evaluate an expression which may or may not refer to the dot
89 // symbol.
90
91 uint64_t
92 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
93 bool is_dot_available, bool dot_has_value,
94 uint64_t dot_value, bool* is_absolute)
95 {
96 Expression_eval_info eei;
97 eei.symtab = symtab;
98 eei.layout = layout;
99 eei.is_dot_available = is_dot_available;
100 eei.dot_has_value = dot_has_value;
101 eei.dot_value = dot_value;
102
103 // We assume the value is absolute, and only set this to false if we
104 // find a section relative reference.
105 *is_absolute = true;
106 eei.is_absolute = is_absolute;
107
108 return this->value(&eei);
109 }
110
111 // A number.
112
113 class Integer_expression : public Expression
114 {
115 public:
116 Integer_expression(uint64_t val)
117 : val_(val)
118 { }
119
120 uint64_t
121 value(const Expression_eval_info*)
122 { return this->val_; }
123
124 void
125 print(FILE* f) const
126 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
127
128 private:
129 uint64_t val_;
130 };
131
132 extern "C" Expression*
133 script_exp_integer(uint64_t val)
134 {
135 return new Integer_expression(val);
136 }
137
138 // An expression whose value is the value of a symbol.
139
140 class Symbol_expression : public Expression
141 {
142 public:
143 Symbol_expression(const char* name, size_t length)
144 : name_(name, length)
145 { }
146
147 uint64_t
148 value(const Expression_eval_info*);
149
150 void
151 print(FILE* f) const
152 { fprintf(f, "%s", this->name_.c_str()); }
153
154 private:
155 std::string name_;
156 };
157
158 uint64_t
159 Symbol_expression::value(const Expression_eval_info* eei)
160 {
161 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
162 if (sym == NULL || !sym->is_defined())
163 {
164 gold_error(_("undefined symbol '%s' referenced in expression"),
165 this->name_.c_str());
166 return 0;
167 }
168
169 // If this symbol does not have an absolute value, then the whole
170 // expression does not have an absolute value. This is not strictly
171 // accurate: the subtraction of two symbols in the same section is
172 // absolute. This is unlikely to matter in practice, as this value
173 // is only used for error checking.
174 if (!sym->value_is_absolute())
175 *eei->is_absolute = false;
176
177 if (parameters->get_size() == 32)
178 return eei->symtab->get_sized_symbol<32>(sym)->value();
179 else if (parameters->get_size() == 64)
180 return eei->symtab->get_sized_symbol<64>(sym)->value();
181 else
182 gold_unreachable();
183 }
184
185 // An expression whose value is the value of the special symbol ".".
186 // This is only valid within a SECTIONS clause.
187
188 class Dot_expression : public Expression
189 {
190 public:
191 Dot_expression()
192 { }
193
194 uint64_t
195 value(const Expression_eval_info*);
196
197 void
198 print(FILE* f) const
199 { fprintf(f, "."); }
200 };
201
202 uint64_t
203 Dot_expression::value(const Expression_eval_info* eei)
204 {
205 if (!eei->is_dot_available)
206 {
207 gold_error(_("invalid reference to dot symbol outside of "
208 "SECTIONS clause"));
209 return 0;
210 }
211 else if (!eei->dot_has_value)
212 {
213 gold_error(_("invalid reference to dot symbol before "
214 "it has been given a value"));
215 return 0;
216 }
217 return eei->dot_value;
218 }
219
220 // A string. This is either the name of a symbol, or ".".
221
222 extern "C" Expression*
223 script_exp_string(const char* name, size_t length)
224 {
225 if (length == 1 && name[0] == '.')
226 return new Dot_expression();
227 else
228 return new Symbol_expression(name, length);
229 }
230
231 // A unary expression.
232
233 class Unary_expression : public Expression
234 {
235 public:
236 Unary_expression(Expression* arg)
237 : arg_(arg)
238 { }
239
240 ~Unary_expression()
241 { delete this->arg_; }
242
243 protected:
244 uint64_t
245 arg_value(const Expression_eval_info* eei) const
246 { return this->arg_->value(eei); }
247
248 void
249 arg_print(FILE* f) const
250 { this->arg_->print(f); }
251
252 private:
253 Expression* arg_;
254 };
255
256 // Handle unary operators. We use a preprocessor macro as a hack to
257 // capture the C operator.
258
259 #define UNARY_EXPRESSION(NAME, OPERATOR) \
260 class Unary_ ## NAME : public Unary_expression \
261 { \
262 public: \
263 Unary_ ## NAME(Expression* arg) \
264 : Unary_expression(arg) \
265 { } \
266 \
267 uint64_t \
268 value(const Expression_eval_info* eei) \
269 { return OPERATOR this->arg_value(eei); } \
270 \
271 void \
272 print(FILE* f) const \
273 { \
274 fprintf(f, "(%s ", #OPERATOR); \
275 this->arg_print(f); \
276 fprintf(f, ")"); \
277 } \
278 }; \
279 \
280 extern "C" Expression* \
281 script_exp_unary_ ## NAME(Expression* arg) \
282 { \
283 return new Unary_ ## NAME(arg); \
284 }
285
286 UNARY_EXPRESSION(minus, -)
287 UNARY_EXPRESSION(logical_not, !)
288 UNARY_EXPRESSION(bitwise_not, ~)
289
290 // A binary expression.
291
292 class Binary_expression : public Expression
293 {
294 public:
295 Binary_expression(Expression* left, Expression* right)
296 : left_(left), right_(right)
297 { }
298
299 ~Binary_expression()
300 {
301 delete this->left_;
302 delete this->right_;
303 }
304
305 protected:
306 uint64_t
307 left_value(const Expression_eval_info* eei) const
308 { return this->left_->value(eei); }
309
310 uint64_t
311 right_value(const Expression_eval_info* eei) const
312 { return this->right_->value(eei); }
313
314 void
315 left_print(FILE* f) const
316 { this->left_->print(f); }
317
318 void
319 right_print(FILE* f) const
320 { this->right_->print(f); }
321
322 // This is a call to function FUNCTION_NAME. Print it. This is for
323 // debugging.
324 void
325 print_function(FILE* f, const char *function_name) const
326 {
327 fprintf(f, "%s(", function_name);
328 this->left_print(f);
329 fprintf(f, ", ");
330 this->right_print(f);
331 fprintf(f, ")");
332 }
333
334 private:
335 Expression* left_;
336 Expression* right_;
337 };
338
339 // Handle binary operators. We use a preprocessor macro as a hack to
340 // capture the C operator.
341
342 #define BINARY_EXPRESSION(NAME, OPERATOR) \
343 class Binary_ ## NAME : public Binary_expression \
344 { \
345 public: \
346 Binary_ ## NAME(Expression* left, Expression* right) \
347 : Binary_expression(left, right) \
348 { } \
349 \
350 uint64_t \
351 value(const Expression_eval_info* eei) \
352 { \
353 return (this->left_value(eei) \
354 OPERATOR this->right_value(eei)); \
355 } \
356 \
357 void \
358 print(FILE* f) const \
359 { \
360 fprintf(f, "("); \
361 this->left_print(f); \
362 fprintf(f, " %s ", #OPERATOR); \
363 this->right_print(f); \
364 fprintf(f, ")"); \
365 } \
366 }; \
367 \
368 extern "C" Expression* \
369 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
370 { \
371 return new Binary_ ## NAME(left, right); \
372 }
373
374 BINARY_EXPRESSION(mult, *)
375 BINARY_EXPRESSION(div, /)
376 BINARY_EXPRESSION(mod, %)
377 BINARY_EXPRESSION(add, +)
378 BINARY_EXPRESSION(sub, -)
379 BINARY_EXPRESSION(lshift, <<)
380 BINARY_EXPRESSION(rshift, >>)
381 BINARY_EXPRESSION(eq, ==)
382 BINARY_EXPRESSION(ne, !=)
383 BINARY_EXPRESSION(le, <=)
384 BINARY_EXPRESSION(ge, >=)
385 BINARY_EXPRESSION(lt, <)
386 BINARY_EXPRESSION(gt, >)
387 BINARY_EXPRESSION(bitwise_and, &)
388 BINARY_EXPRESSION(bitwise_xor, ^)
389 BINARY_EXPRESSION(bitwise_or, |)
390 BINARY_EXPRESSION(logical_and, &&)
391 BINARY_EXPRESSION(logical_or, ||)
392
393 // A trinary expression.
394
395 class Trinary_expression : public Expression
396 {
397 public:
398 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
399 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
400 { }
401
402 ~Trinary_expression()
403 {
404 delete this->arg1_;
405 delete this->arg2_;
406 delete this->arg3_;
407 }
408
409 protected:
410 uint64_t
411 arg1_value(const Expression_eval_info* eei) const
412 { return this->arg1_->value(eei); }
413
414 uint64_t
415 arg2_value(const Expression_eval_info* eei) const
416 { return this->arg2_->value(eei); }
417
418 uint64_t
419 arg3_value(const Expression_eval_info* eei) const
420 { return this->arg3_->value(eei); }
421
422 void
423 arg1_print(FILE* f) const
424 { this->arg1_->print(f); }
425
426 void
427 arg2_print(FILE* f) const
428 { this->arg2_->print(f); }
429
430 void
431 arg3_print(FILE* f) const
432 { this->arg3_->print(f); }
433
434 private:
435 Expression* arg1_;
436 Expression* arg2_;
437 Expression* arg3_;
438 };
439
440 // The conditional operator.
441
442 class Trinary_cond : public Trinary_expression
443 {
444 public:
445 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
446 : Trinary_expression(arg1, arg2, arg3)
447 { }
448
449 uint64_t
450 value(const Expression_eval_info* eei)
451 {
452 return (this->arg1_value(eei)
453 ? this->arg2_value(eei)
454 : this->arg3_value(eei));
455 }
456
457 void
458 print(FILE* f) const
459 {
460 fprintf(f, "(");
461 this->arg1_print(f);
462 fprintf(f, " ? ");
463 this->arg2_print(f);
464 fprintf(f, " : ");
465 this->arg3_print(f);
466 fprintf(f, ")");
467 }
468 };
469
470 extern "C" Expression*
471 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
472 {
473 return new Trinary_cond(arg1, arg2, arg3);
474 }
475
476 // Max function.
477
478 class Max_expression : public Binary_expression
479 {
480 public:
481 Max_expression(Expression* left, Expression* right)
482 : Binary_expression(left, right)
483 { }
484
485 uint64_t
486 value(const Expression_eval_info* eei)
487 { return std::max(this->left_value(eei), this->right_value(eei)); }
488
489 void
490 print(FILE* f) const
491 { this->print_function(f, "MAX"); }
492 };
493
494 extern "C" Expression*
495 script_exp_function_max(Expression* left, Expression* right)
496 {
497 return new Max_expression(left, right);
498 }
499
500 // Min function.
501
502 class Min_expression : public Binary_expression
503 {
504 public:
505 Min_expression(Expression* left, Expression* right)
506 : Binary_expression(left, right)
507 { }
508
509 uint64_t
510 value(const Expression_eval_info* eei)
511 { return std::min(this->left_value(eei), this->right_value(eei)); }
512
513 void
514 print(FILE* f) const
515 { this->print_function(f, "MIN"); }
516 };
517
518 extern "C" Expression*
519 script_exp_function_min(Expression* left, Expression* right)
520 {
521 return new Min_expression(left, right);
522 }
523
524 // Align function.
525
526 class Align_expression : public Binary_expression
527 {
528 public:
529 Align_expression(Expression* left, Expression* right)
530 : Binary_expression(left, right)
531 { }
532
533 uint64_t
534 value(const Expression_eval_info* eei)
535 {
536 uint64_t align = this->right_value(eei);
537 uint64_t value = this->left_value(eei);
538 if (align <= 1)
539 return value;
540 return ((value + align - 1) / align) * align;
541 }
542
543 void
544 print(FILE* f) const
545 { this->print_function(f, "ALIGN"); }
546 };
547
548 extern "C" Expression*
549 script_exp_function_align(Expression* left, Expression* right)
550 {
551 return new Align_expression(left, right);
552 }
553
554 // Assert function.
555
556 class Assert_expression : public Unary_expression
557 {
558 public:
559 Assert_expression(Expression* arg, const char* message, size_t length)
560 : Unary_expression(arg), message_(message, length)
561 { }
562
563 uint64_t
564 value(const Expression_eval_info* eei)
565 {
566 uint64_t value = this->arg_value(eei);
567 if (!value)
568 gold_error("%s", this->message_.c_str());
569 return value;
570 }
571
572 void
573 print(FILE* f) const
574 {
575 fprintf(f, "ASSERT(");
576 this->arg_print(f);
577 fprintf(f, ", %s)", this->message_.c_str());
578 }
579
580 private:
581 std::string message_;
582 };
583
584 extern "C" Expression*
585 script_exp_function_assert(Expression* expr, const char* message,
586 size_t length)
587 {
588 return new Assert_expression(expr, message, length);
589 }
590
591 // Addr function.
592
593 class Addr_expression : public Expression
594 {
595 public:
596 Addr_expression(const char* section_name, size_t section_name_len)
597 : section_name_(section_name, section_name_len)
598 { }
599
600 uint64_t
601 value(const Expression_eval_info*);
602
603 void
604 print(FILE* f) const
605 { fprintf(f, "ADDR(%s)", this->section_name_.c_str()); }
606
607 private:
608 std::string section_name_;
609 };
610
611 uint64_t
612 Addr_expression::value(const Expression_eval_info* eei)
613 {
614 const char* section_name = this->section_name_.c_str();
615 Output_section* os = eei->layout->find_output_section(section_name);
616 if (os == NULL)
617 {
618 gold_error("ADDR called on nonexistent output section '%s'",
619 section_name);
620 return 0;
621 }
622
623 // Note that the address of a section is an absolute address, and we
624 // should not clear *EEI->IS_ABSOLUTE here.
625
626 return os->address();
627 }
628
629 extern "C" Expression*
630 script_exp_function_addr(const char* section_name, size_t section_name_len)
631 {
632 return new Addr_expression(section_name, section_name_len);
633 }
634
635 // Functions.
636
637 extern "C" Expression*
638 script_exp_function_defined(const char*, size_t)
639 {
640 gold_fatal(_("DEFINED not implemented"));
641 }
642
643 extern "C" Expression*
644 script_exp_function_sizeof_headers()
645 {
646 gold_fatal(_("SIZEOF_HEADERS not implemented"));
647 }
648
649 extern "C" Expression*
650 script_exp_function_alignof(const char*, size_t)
651 {
652 gold_fatal(_("ALIGNOF not implemented"));
653 }
654
655 extern "C" Expression*
656 script_exp_function_sizeof(const char*, size_t)
657 {
658 gold_fatal(_("SIZEOF not implemented"));
659 }
660
661 extern "C" Expression*
662 script_exp_function_loadaddr(const char*, size_t)
663 {
664 gold_fatal(_("LOADADDR not implemented"));
665 }
666
667 extern "C" Expression*
668 script_exp_function_origin(const char*, size_t)
669 {
670 gold_fatal(_("ORIGIN not implemented"));
671 }
672
673 extern "C" Expression*
674 script_exp_function_length(const char*, size_t)
675 {
676 gold_fatal(_("LENGTH not implemented"));
677 }
678
679 extern "C" Expression*
680 script_exp_function_constant(const char*, size_t)
681 {
682 gold_fatal(_("CONSTANT not implemented"));
683 }
684
685 extern "C" Expression*
686 script_exp_function_absolute(Expression*)
687 {
688 gold_fatal(_("ABSOLUTE not implemented"));
689 }
690
691 extern "C" Expression*
692 script_exp_function_data_segment_align(Expression*, Expression*)
693 {
694 gold_fatal(_("DATA_SEGMENT_ALIGN not implemented"));
695 }
696
697 extern "C" Expression*
698 script_exp_function_data_segment_relro_end(Expression*, Expression*)
699 {
700 gold_fatal(_("DATA_SEGMENT_RELRO_END not implemented"));
701 }
702
703 extern "C" Expression*
704 script_exp_function_data_segment_end(Expression*)
705 {
706 gold_fatal(_("DATA_SEGMENT_END not implemented"));
707 }
708
709 extern "C" Expression*
710 script_exp_function_segment_start(const char*, size_t, Expression*)
711 {
712 gold_fatal(_("SEGMENT_START not implemented"));
713 }
714
715 } // End namespace gold.
This page took 0.045281 seconds and 5 git commands to generate.