* output.h (Output_section_lookup_maps::add_merge_section):
[binutils.git] / gold / expression.cc
blob134b0fa2a22a4c10a1b283c63629a36b58357376
1 // expression.cc -- expressions in linker scripts for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <string>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "symtab.h"
30 #include "layout.h"
31 #include "output.h"
32 #include "script.h"
33 #include "script-c.h"
35 namespace gold
38 // This file holds the code which handles linker expressions.
40 // The dot symbol, which linker scripts refer to simply as ".",
41 // requires special treatment. The dot symbol is set several times,
42 // section addresses will refer to it, output sections will change it,
43 // and it can be set based on the value of other symbols. We simplify
44 // the handling by prohibiting setting the dot symbol to the value of
45 // a non-absolute symbol.
47 // When evaluating the value of an expression, we pass in a pointer to
48 // this struct, so that the expression evaluation can find the
49 // information it needs.
51 struct Expression::Expression_eval_info
53 // The symbol table.
54 const Symbol_table* symtab;
55 // The layout--we use this to get section information.
56 const Layout* layout;
57 // Whether to check assertions.
58 bool check_assertions;
59 // Whether expressions can refer to the dot symbol. The dot symbol
60 // is only available within a SECTIONS clause.
61 bool is_dot_available;
62 // The current value of the dot symbol.
63 uint64_t dot_value;
64 // The section in which the dot symbol is defined; this is NULL if
65 // it is absolute.
66 Output_section* dot_section;
67 // Points to where the section of the result should be stored.
68 Output_section** result_section_pointer;
69 // Pointer to where the alignment of the result should be stored.
70 uint64_t* result_alignment_pointer;
73 // Evaluate an expression.
75 uint64_t
76 Expression::eval(const Symbol_table* symtab, const Layout* layout,
77 bool check_assertions)
79 Output_section* dummy;
80 return this->eval_maybe_dot(symtab, layout, check_assertions,
81 false, 0, NULL, &dummy, NULL);
84 // Evaluate an expression which may refer to the dot symbol.
86 uint64_t
87 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
88 bool check_assertions, uint64_t dot_value,
89 Output_section* dot_section,
90 Output_section** result_section_pointer,
91 uint64_t* result_alignment_pointer)
93 return this->eval_maybe_dot(symtab, layout, check_assertions, true,
94 dot_value, dot_section, result_section_pointer,
95 result_alignment_pointer);
98 // Evaluate an expression which may or may not refer to the dot
99 // symbol.
101 uint64_t
102 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
103 bool check_assertions, bool is_dot_available,
104 uint64_t dot_value, Output_section* dot_section,
105 Output_section** result_section_pointer,
106 uint64_t* result_alignment_pointer)
108 Expression_eval_info eei;
109 eei.symtab = symtab;
110 eei.layout = layout;
111 eei.check_assertions = check_assertions;
112 eei.is_dot_available = is_dot_available;
113 eei.dot_value = dot_value;
114 eei.dot_section = dot_section;
116 // We assume the value is absolute, and only set this to a section
117 // if we find a section relative reference.
118 *result_section_pointer = NULL;
119 eei.result_section_pointer = result_section_pointer;
121 eei.result_alignment_pointer = result_alignment_pointer;
123 return this->value(&eei);
126 // A number.
128 class Integer_expression : public Expression
130 public:
131 Integer_expression(uint64_t val)
132 : val_(val)
135 uint64_t
136 value(const Expression_eval_info*)
137 { return this->val_; }
139 void
140 print(FILE* f) const
141 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
143 private:
144 uint64_t val_;
147 extern "C" Expression*
148 script_exp_integer(uint64_t val)
150 return new Integer_expression(val);
153 // An expression whose value is the value of a symbol.
155 class Symbol_expression : public Expression
157 public:
158 Symbol_expression(const char* name, size_t length)
159 : name_(name, length)
162 uint64_t
163 value(const Expression_eval_info*);
165 void
166 print(FILE* f) const
167 { fprintf(f, "%s", this->name_.c_str()); }
169 private:
170 std::string name_;
173 uint64_t
174 Symbol_expression::value(const Expression_eval_info* eei)
176 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
177 if (sym == NULL || !sym->is_defined())
179 gold_error(_("undefined symbol '%s' referenced in expression"),
180 this->name_.c_str());
181 return 0;
184 *eei->result_section_pointer = sym->output_section();
186 if (parameters->target().get_size() == 32)
187 return eei->symtab->get_sized_symbol<32>(sym)->value();
188 else if (parameters->target().get_size() == 64)
189 return eei->symtab->get_sized_symbol<64>(sym)->value();
190 else
191 gold_unreachable();
194 // An expression whose value is the value of the special symbol ".".
195 // This is only valid within a SECTIONS clause.
197 class Dot_expression : public Expression
199 public:
200 Dot_expression()
203 uint64_t
204 value(const Expression_eval_info*);
206 void
207 print(FILE* f) const
208 { fprintf(f, "."); }
211 uint64_t
212 Dot_expression::value(const Expression_eval_info* eei)
214 if (!eei->is_dot_available)
216 gold_error(_("invalid reference to dot symbol outside of "
217 "SECTIONS clause"));
218 return 0;
220 *eei->result_section_pointer = eei->dot_section;
221 return eei->dot_value;
224 // A string. This is either the name of a symbol, or ".".
226 extern "C" Expression*
227 script_exp_string(const char* name, size_t length)
229 if (length == 1 && name[0] == '.')
230 return new Dot_expression();
231 else
232 return new Symbol_expression(name, length);
235 // A unary expression.
237 class Unary_expression : public Expression
239 public:
240 Unary_expression(Expression* arg)
241 : arg_(arg)
244 ~Unary_expression()
245 { delete this->arg_; }
247 protected:
248 uint64_t
249 arg_value(const Expression_eval_info* eei,
250 Output_section** arg_section_pointer) const
252 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
253 eei->check_assertions,
254 eei->is_dot_available,
255 eei->dot_value,
256 eei->dot_section,
257 arg_section_pointer,
258 eei->result_alignment_pointer);
261 void
262 arg_print(FILE* f) const
263 { this->arg_->print(f); }
265 private:
266 Expression* arg_;
269 // Handle unary operators. We use a preprocessor macro as a hack to
270 // capture the C operator.
272 #define UNARY_EXPRESSION(NAME, OPERATOR) \
273 class Unary_ ## NAME : public Unary_expression \
275 public: \
276 Unary_ ## NAME(Expression* arg) \
277 : Unary_expression(arg) \
278 { } \
280 uint64_t \
281 value(const Expression_eval_info* eei) \
283 Output_section* arg_section; \
284 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
285 if (arg_section != NULL && parameters->options().relocatable()) \
286 gold_warning(_("unary " #NAME " applied to section " \
287 "relative value")); \
288 return ret; \
291 void \
292 print(FILE* f) const \
294 fprintf(f, "(%s ", #OPERATOR); \
295 this->arg_print(f); \
296 fprintf(f, ")"); \
298 }; \
300 extern "C" Expression* \
301 script_exp_unary_ ## NAME(Expression* arg) \
303 return new Unary_ ## NAME(arg); \
306 UNARY_EXPRESSION(minus, -)
307 UNARY_EXPRESSION(logical_not, !)
308 UNARY_EXPRESSION(bitwise_not, ~)
310 // A binary expression.
312 class Binary_expression : public Expression
314 public:
315 Binary_expression(Expression* left, Expression* right)
316 : left_(left), right_(right)
319 ~Binary_expression()
321 delete this->left_;
322 delete this->right_;
325 protected:
326 uint64_t
327 left_value(const Expression_eval_info* eei,
328 Output_section** section_pointer,
329 uint64_t* alignment_pointer) const
331 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
332 eei->check_assertions,
333 eei->is_dot_available,
334 eei->dot_value,
335 eei->dot_section,
336 section_pointer,
337 alignment_pointer);
340 uint64_t
341 right_value(const Expression_eval_info* eei,
342 Output_section** section_pointer,
343 uint64_t* alignment_pointer) const
345 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
346 eei->check_assertions,
347 eei->is_dot_available,
348 eei->dot_value,
349 eei->dot_section,
350 section_pointer,
351 alignment_pointer);
354 void
355 left_print(FILE* f) const
356 { this->left_->print(f); }
358 void
359 right_print(FILE* f) const
360 { this->right_->print(f); }
362 // This is a call to function FUNCTION_NAME. Print it. This is for
363 // debugging.
364 void
365 print_function(FILE* f, const char *function_name) const
367 fprintf(f, "%s(", function_name);
368 this->left_print(f);
369 fprintf(f, ", ");
370 this->right_print(f);
371 fprintf(f, ")");
374 private:
375 Expression* left_;
376 Expression* right_;
379 // Handle binary operators. We use a preprocessor macro as a hack to
380 // capture the C operator. KEEP_LEFT means that if the left operand
381 // is section relative and the right operand is not, the result uses
382 // the same section as the left operand. KEEP_RIGHT is the same with
383 // left and right swapped. IS_DIV means that we need to give an error
384 // if the right operand is zero. WARN means that we should warn if
385 // used on section relative values in a relocatable link. We always
386 // warn if used on values in different sections in a relocatable link.
388 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
389 class Binary_ ## NAME : public Binary_expression \
391 public: \
392 Binary_ ## NAME(Expression* left, Expression* right) \
393 : Binary_expression(left, right) \
394 { } \
396 uint64_t \
397 value(const Expression_eval_info* eei) \
399 Output_section* left_section; \
400 uint64_t left_alignment; \
401 uint64_t left = this->left_value(eei, &left_section, \
402 &left_alignment); \
403 Output_section* right_section; \
404 uint64_t right_alignment; \
405 uint64_t right = this->right_value(eei, &right_section, \
406 &right_alignment); \
407 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
409 *eei->result_section_pointer = right_section; \
410 if (eei->result_alignment_pointer != NULL) \
411 *eei->result_alignment_pointer = right_alignment; \
413 else if (KEEP_LEFT \
414 && left_section != NULL \
415 && right_section == NULL) \
417 *eei->result_section_pointer = left_section; \
418 if (eei->result_alignment_pointer != NULL) \
419 *eei->result_alignment_pointer = right_alignment; \
421 else if ((WARN || left_section != right_section) \
422 && (left_section != NULL || right_section != NULL) \
423 && parameters->options().relocatable()) \
424 gold_warning(_("binary " #NAME " applied to section " \
425 "relative value")); \
426 if (IS_DIV && right == 0) \
428 gold_error(_(#NAME " by zero")); \
429 return 0; \
431 return left OPERATOR right; \
434 void \
435 print(FILE* f) const \
437 fprintf(f, "("); \
438 this->left_print(f); \
439 fprintf(f, " %s ", #OPERATOR); \
440 this->right_print(f); \
441 fprintf(f, ")"); \
443 }; \
445 extern "C" Expression* \
446 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
448 return new Binary_ ## NAME(left, right); \
451 BINARY_EXPRESSION(mult, *, false, false, false, true)
452 BINARY_EXPRESSION(div, /, false, false, true, true)
453 BINARY_EXPRESSION(mod, %, false, false, true, true)
454 BINARY_EXPRESSION(add, +, true, true, false, true)
455 BINARY_EXPRESSION(sub, -, true, false, false, false)
456 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
457 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
458 BINARY_EXPRESSION(eq, ==, false, false, false, false)
459 BINARY_EXPRESSION(ne, !=, false, false, false, false)
460 BINARY_EXPRESSION(le, <=, false, false, false, false)
461 BINARY_EXPRESSION(ge, >=, false, false, false, false)
462 BINARY_EXPRESSION(lt, <, false, false, false, false)
463 BINARY_EXPRESSION(gt, >, false, false, false, false)
464 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
465 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
466 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
467 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
468 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
470 // A trinary expression.
472 class Trinary_expression : public Expression
474 public:
475 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
476 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
479 ~Trinary_expression()
481 delete this->arg1_;
482 delete this->arg2_;
483 delete this->arg3_;
486 protected:
487 uint64_t
488 arg1_value(const Expression_eval_info* eei,
489 Output_section** section_pointer) const
491 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
492 eei->check_assertions,
493 eei->is_dot_available,
494 eei->dot_value,
495 eei->dot_section,
496 section_pointer,
497 NULL);
500 uint64_t
501 arg2_value(const Expression_eval_info* eei,
502 Output_section** section_pointer,
503 uint64_t* alignment_pointer) const
505 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
506 eei->check_assertions,
507 eei->is_dot_available,
508 eei->dot_value,
509 eei->dot_section,
510 section_pointer,
511 alignment_pointer);
514 uint64_t
515 arg3_value(const Expression_eval_info* eei,
516 Output_section** section_pointer,
517 uint64_t* alignment_pointer) const
519 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
520 eei->check_assertions,
521 eei->is_dot_available,
522 eei->dot_value,
523 eei->dot_section,
524 section_pointer,
525 alignment_pointer);
528 void
529 arg1_print(FILE* f) const
530 { this->arg1_->print(f); }
532 void
533 arg2_print(FILE* f) const
534 { this->arg2_->print(f); }
536 void
537 arg3_print(FILE* f) const
538 { this->arg3_->print(f); }
540 private:
541 Expression* arg1_;
542 Expression* arg2_;
543 Expression* arg3_;
546 // The conditional operator.
548 class Trinary_cond : public Trinary_expression
550 public:
551 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
552 : Trinary_expression(arg1, arg2, arg3)
555 uint64_t
556 value(const Expression_eval_info* eei)
558 Output_section* arg1_section;
559 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
560 return (arg1
561 ? this->arg2_value(eei, eei->result_section_pointer,
562 eei->result_alignment_pointer)
563 : this->arg3_value(eei, eei->result_section_pointer,
564 eei->result_alignment_pointer));
567 void
568 print(FILE* f) const
570 fprintf(f, "(");
571 this->arg1_print(f);
572 fprintf(f, " ? ");
573 this->arg2_print(f);
574 fprintf(f, " : ");
575 this->arg3_print(f);
576 fprintf(f, ")");
580 extern "C" Expression*
581 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
583 return new Trinary_cond(arg1, arg2, arg3);
586 // Max function.
588 class Max_expression : public Binary_expression
590 public:
591 Max_expression(Expression* left, Expression* right)
592 : Binary_expression(left, right)
595 uint64_t
596 value(const Expression_eval_info* eei)
598 Output_section* left_section;
599 uint64_t left_alignment;
600 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
601 Output_section* right_section;
602 uint64_t right_alignment;
603 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
604 if (left_section == right_section)
605 *eei->result_section_pointer = left_section;
606 else if ((left_section != NULL || right_section != NULL)
607 && parameters->options().relocatable())
608 gold_warning(_("max applied to section relative value"));
609 if (eei->result_alignment_pointer != NULL)
611 uint64_t ra = *eei->result_alignment_pointer;
612 if (left > right)
613 ra = std::max(ra, left_alignment);
614 else if (right > left)
615 ra = std::max(ra, right_alignment);
616 else
617 ra = std::max(ra, std::max(left_alignment, right_alignment));
618 *eei->result_alignment_pointer = ra;
620 return std::max(left, right);
623 void
624 print(FILE* f) const
625 { this->print_function(f, "MAX"); }
628 extern "C" Expression*
629 script_exp_function_max(Expression* left, Expression* right)
631 return new Max_expression(left, right);
634 // Min function.
636 class Min_expression : public Binary_expression
638 public:
639 Min_expression(Expression* left, Expression* right)
640 : Binary_expression(left, right)
643 uint64_t
644 value(const Expression_eval_info* eei)
646 Output_section* left_section;
647 uint64_t left_alignment;
648 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
649 Output_section* right_section;
650 uint64_t right_alignment;
651 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
652 if (left_section == right_section)
653 *eei->result_section_pointer = left_section;
654 else if ((left_section != NULL || right_section != NULL)
655 && parameters->options().relocatable())
656 gold_warning(_("min applied to section relative value"));
657 if (eei->result_alignment_pointer != NULL)
659 uint64_t ra = *eei->result_alignment_pointer;
660 if (left < right)
661 ra = std::max(ra, left_alignment);
662 else if (right < left)
663 ra = std::max(ra, right_alignment);
664 else
665 ra = std::max(ra, std::max(left_alignment, right_alignment));
666 *eei->result_alignment_pointer = ra;
668 return std::min(left, right);
671 void
672 print(FILE* f) const
673 { this->print_function(f, "MIN"); }
676 extern "C" Expression*
677 script_exp_function_min(Expression* left, Expression* right)
679 return new Min_expression(left, right);
682 // Class Section_expression. This is a parent class used for
683 // functions which take the name of an output section.
685 class Section_expression : public Expression
687 public:
688 Section_expression(const char* section_name, size_t section_name_len)
689 : section_name_(section_name, section_name_len)
692 uint64_t
693 value(const Expression_eval_info*);
695 void
696 print(FILE* f) const
697 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
699 protected:
700 // The child class must implement this.
701 virtual uint64_t
702 value_from_output_section(const Expression_eval_info*,
703 Output_section*) = 0;
705 // The child class must implement this.
706 virtual uint64_t
707 value_from_script_output_section(uint64_t address, uint64_t load_address,
708 uint64_t addralign, uint64_t size) = 0;
710 // The child class must implement this.
711 virtual const char*
712 function_name() const = 0;
714 private:
715 std::string section_name_;
718 uint64_t
719 Section_expression::value(const Expression_eval_info* eei)
721 const char* section_name = this->section_name_.c_str();
722 Output_section* os = eei->layout->find_output_section(section_name);
723 if (os != NULL)
724 return this->value_from_output_section(eei, os);
726 uint64_t address;
727 uint64_t load_address;
728 uint64_t addralign;
729 uint64_t size;
730 const Script_options* ss = eei->layout->script_options();
731 if (ss->saw_sections_clause())
733 if (ss->script_sections()->get_output_section_info(section_name,
734 &address,
735 &load_address,
736 &addralign,
737 &size))
738 return this->value_from_script_output_section(address, load_address,
739 addralign, size);
742 gold_error("%s called on nonexistent output section '%s'",
743 this->function_name(), section_name);
744 return 0;
747 // ABSOLUTE function.
749 class Absolute_expression : public Unary_expression
751 public:
752 Absolute_expression(Expression* arg)
753 : Unary_expression(arg)
756 uint64_t
757 value(const Expression_eval_info* eei)
759 Output_section* dummy;
760 uint64_t ret = this->arg_value(eei, &dummy);
761 // Force the value to be absolute.
762 *eei->result_section_pointer = NULL;
763 return ret;
766 void
767 print(FILE* f) const
769 fprintf(f, "ABSOLUTE(");
770 this->arg_print(f);
771 fprintf(f, ")");
775 extern "C" Expression*
776 script_exp_function_absolute(Expression* arg)
778 return new Absolute_expression(arg);
781 // ALIGN function.
783 class Align_expression : public Binary_expression
785 public:
786 Align_expression(Expression* left, Expression* right)
787 : Binary_expression(left, right)
790 uint64_t
791 value(const Expression_eval_info* eei)
793 Output_section* align_section;
794 uint64_t align = this->right_value(eei, &align_section, NULL);
795 if (align_section != NULL
796 && parameters->options().relocatable())
797 gold_warning(_("aligning to section relative value"));
799 if (eei->result_alignment_pointer != NULL
800 && align > *eei->result_alignment_pointer)
802 uint64_t a = align;
803 while ((a & (a - 1)) != 0)
804 a &= a - 1;
805 *eei->result_alignment_pointer = a;
808 uint64_t value = this->left_value(eei, eei->result_section_pointer, NULL);
809 if (align <= 1)
810 return value;
811 return ((value + align - 1) / align) * align;
814 void
815 print(FILE* f) const
816 { this->print_function(f, "ALIGN"); }
819 extern "C" Expression*
820 script_exp_function_align(Expression* left, Expression* right)
822 return new Align_expression(left, right);
825 // ASSERT function.
827 class Assert_expression : public Unary_expression
829 public:
830 Assert_expression(Expression* arg, const char* message, size_t length)
831 : Unary_expression(arg), message_(message, length)
834 uint64_t
835 value(const Expression_eval_info* eei)
837 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
838 if (!value && eei->check_assertions)
839 gold_error("%s", this->message_.c_str());
840 return value;
843 void
844 print(FILE* f) const
846 fprintf(f, "ASSERT(");
847 this->arg_print(f);
848 fprintf(f, ", %s)", this->message_.c_str());
851 private:
852 std::string message_;
855 extern "C" Expression*
856 script_exp_function_assert(Expression* expr, const char* message,
857 size_t length)
859 return new Assert_expression(expr, message, length);
862 // ADDR function.
864 class Addr_expression : public Section_expression
866 public:
867 Addr_expression(const char* section_name, size_t section_name_len)
868 : Section_expression(section_name, section_name_len)
871 protected:
872 uint64_t
873 value_from_output_section(const Expression_eval_info* eei,
874 Output_section* os)
876 *eei->result_section_pointer = os;
877 return os->address();
880 uint64_t
881 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
882 uint64_t)
883 { return address; }
885 const char*
886 function_name() const
887 { return "ADDR"; }
890 extern "C" Expression*
891 script_exp_function_addr(const char* section_name, size_t section_name_len)
893 return new Addr_expression(section_name, section_name_len);
896 // ALIGNOF.
898 class Alignof_expression : public Section_expression
900 public:
901 Alignof_expression(const char* section_name, size_t section_name_len)
902 : Section_expression(section_name, section_name_len)
905 protected:
906 uint64_t
907 value_from_output_section(const Expression_eval_info*,
908 Output_section* os)
909 { return os->addralign(); }
911 uint64_t
912 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
913 uint64_t)
914 { return addralign; }
916 const char*
917 function_name() const
918 { return "ALIGNOF"; }
921 extern "C" Expression*
922 script_exp_function_alignof(const char* section_name, size_t section_name_len)
924 return new Alignof_expression(section_name, section_name_len);
927 // CONSTANT. It would be nice if we could simply evaluate this
928 // immediately and return an Integer_expression, but unfortunately we
929 // don't know the target.
931 class Constant_expression : public Expression
933 public:
934 Constant_expression(const char* name, size_t length);
936 uint64_t
937 value(const Expression_eval_info*);
939 void
940 print(FILE* f) const;
942 private:
943 enum Constant_function
945 CONSTANT_MAXPAGESIZE,
946 CONSTANT_COMMONPAGESIZE
949 Constant_function function_;
952 Constant_expression::Constant_expression(const char* name, size_t length)
954 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
955 this->function_ = CONSTANT_MAXPAGESIZE;
956 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
957 this->function_ = CONSTANT_COMMONPAGESIZE;
958 else
960 std::string s(name, length);
961 gold_error(_("unknown constant %s"), s.c_str());
962 this->function_ = CONSTANT_MAXPAGESIZE;
966 uint64_t
967 Constant_expression::value(const Expression_eval_info*)
969 switch (this->function_)
971 case CONSTANT_MAXPAGESIZE:
972 return parameters->target().abi_pagesize();
973 case CONSTANT_COMMONPAGESIZE:
974 return parameters->target().common_pagesize();
975 default:
976 gold_unreachable();
980 void
981 Constant_expression::print(FILE* f) const
983 const char* name;
984 switch (this->function_)
986 case CONSTANT_MAXPAGESIZE:
987 name = "MAXPAGESIZE";
988 break;
989 case CONSTANT_COMMONPAGESIZE:
990 name = "COMMONPAGESIZE";
991 break;
992 default:
993 gold_unreachable();
995 fprintf(f, "CONSTANT(%s)", name);
998 extern "C" Expression*
999 script_exp_function_constant(const char* name, size_t length)
1001 return new Constant_expression(name, length);
1004 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1005 // back to the general case.
1007 extern "C" Expression*
1008 script_exp_function_data_segment_align(Expression* left, Expression*)
1010 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
1011 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
1012 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
1013 e2);
1014 return script_exp_binary_add(e1, e3);
1017 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1019 extern "C" Expression*
1020 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
1022 return right;
1025 // DATA_SEGMENT_END. FIXME: This is not implemented.
1027 extern "C" Expression*
1028 script_exp_function_data_segment_end(Expression* val)
1030 return val;
1033 // DEFINED function.
1035 class Defined_expression : public Expression
1037 public:
1038 Defined_expression(const char* symbol_name, size_t symbol_name_len)
1039 : symbol_name_(symbol_name, symbol_name_len)
1042 uint64_t
1043 value(const Expression_eval_info* eei)
1045 Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
1046 return sym != NULL && sym->is_defined();
1049 void
1050 print(FILE* f) const
1051 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
1053 private:
1054 std::string symbol_name_;
1057 extern "C" Expression*
1058 script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
1060 return new Defined_expression(symbol_name, symbol_name_len);
1063 // LOADADDR function
1065 class Loadaddr_expression : public Section_expression
1067 public:
1068 Loadaddr_expression(const char* section_name, size_t section_name_len)
1069 : Section_expression(section_name, section_name_len)
1072 protected:
1073 uint64_t
1074 value_from_output_section(const Expression_eval_info* eei,
1075 Output_section* os)
1077 if (os->has_load_address())
1078 return os->load_address();
1079 else
1081 *eei->result_section_pointer = os;
1082 return os->address();
1086 uint64_t
1087 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1088 uint64_t)
1089 { return load_address; }
1091 const char*
1092 function_name() const
1093 { return "LOADADDR"; }
1096 extern "C" Expression*
1097 script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
1099 return new Loadaddr_expression(section_name, section_name_len);
1102 // SIZEOF function
1104 class Sizeof_expression : public Section_expression
1106 public:
1107 Sizeof_expression(const char* section_name, size_t section_name_len)
1108 : Section_expression(section_name, section_name_len)
1111 protected:
1112 uint64_t
1113 value_from_output_section(const Expression_eval_info*,
1114 Output_section* os)
1116 // We can not use data_size here, as the size of the section may
1117 // not have been finalized. Instead we get whatever the current
1118 // size is. This will work correctly for backward references in
1119 // linker scripts.
1120 return os->current_data_size();
1123 uint64_t
1124 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1125 uint64_t size)
1126 { return size; }
1128 const char*
1129 function_name() const
1130 { return "SIZEOF"; }
1133 extern "C" Expression*
1134 script_exp_function_sizeof(const char* section_name, size_t section_name_len)
1136 return new Sizeof_expression(section_name, section_name_len);
1139 // SIZEOF_HEADERS.
1141 class Sizeof_headers_expression : public Expression
1143 public:
1144 Sizeof_headers_expression()
1147 uint64_t
1148 value(const Expression_eval_info*);
1150 void
1151 print(FILE* f) const
1152 { fprintf(f, "SIZEOF_HEADERS"); }
1155 uint64_t
1156 Sizeof_headers_expression::value(const Expression_eval_info* eei)
1158 unsigned int ehdr_size;
1159 unsigned int phdr_size;
1160 if (parameters->target().get_size() == 32)
1162 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1163 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1165 else if (parameters->target().get_size() == 64)
1167 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
1168 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
1170 else
1171 gold_unreachable();
1173 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
1176 extern "C" Expression*
1177 script_exp_function_sizeof_headers()
1179 return new Sizeof_headers_expression();
1182 // SEGMENT_START.
1184 class Segment_start_expression : public Unary_expression
1186 public:
1187 Segment_start_expression(const char* segment_name, size_t segment_name_len,
1188 Expression* default_value)
1189 : Unary_expression(default_value),
1190 segment_name_(segment_name, segment_name_len)
1193 uint64_t
1194 value(const Expression_eval_info*);
1196 void
1197 print(FILE* f) const
1199 fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
1200 this->arg_print(f);
1201 fprintf(f, ")");
1204 private:
1205 std::string segment_name_;
1208 uint64_t
1209 Segment_start_expression::value(const Expression_eval_info* eei)
1211 // Check for command line overrides.
1212 if (parameters->options().user_set_Ttext()
1213 && this->segment_name_ == ".text")
1214 return parameters->options().Ttext();
1215 else if (parameters->options().user_set_Tdata()
1216 && this->segment_name_ == ".data")
1217 return parameters->options().Tdata();
1218 else if (parameters->options().user_set_Tbss()
1219 && this->segment_name_ == ".bss")
1220 return parameters->options().Tbss();
1221 else
1223 Output_section* dummy;
1224 uint64_t ret = this->arg_value(eei, &dummy);
1225 // Force the value to be absolute.
1226 *eei->result_section_pointer = NULL;
1227 return ret;
1231 extern "C" Expression*
1232 script_exp_function_segment_start(const char* segment_name,
1233 size_t segment_name_len,
1234 Expression* default_value)
1236 return new Segment_start_expression(segment_name, segment_name_len,
1237 default_value);
1240 // Functions for memory regions. These can not be implemented unless
1241 // and until we implement memory regions.
1243 extern "C" Expression*
1244 script_exp_function_origin(const char*, size_t)
1246 gold_fatal(_("ORIGIN not implemented"));
1249 extern "C" Expression*
1250 script_exp_function_length(const char*, size_t)
1252 gold_fatal(_("LENGTH not implemented"));
1255 } // End namespace gold.