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.
28 #include "parameters.h"
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
54 const Symbol_table* symtab;
55 // The layout--we use this to get section information.
57 // Whether expressions can refer to the dot symbol. The dot symbol
58 // is only available within a SECTIONS clause.
59 bool is_dot_available;
60 // The current value of the dot symbol.
62 // The section in which the dot symbol is defined; this is NULL if
64 Output_section* dot_section;
65 // Points to where the section of the result should be stored.
66 Output_section** result_section_pointer;
69 // Evaluate an expression.
72 Expression::eval(const Symbol_table* symtab, const Layout* layout)
74 Output_section* dummy;
75 return this->eval_maybe_dot(symtab, layout, false, 0, NULL, &dummy);
78 // Evaluate an expression which may refer to the dot symbol.
81 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
82 uint64_t dot_value, Output_section* dot_section,
83 Output_section** result_section_pointer)
85 return this->eval_maybe_dot(symtab, layout, true, dot_value, dot_section,
86 result_section_pointer);
89 // Evaluate an expression which may or may not refer to the dot
93 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
94 bool is_dot_available, uint64_t dot_value,
95 Output_section* dot_section,
96 Output_section** result_section_pointer)
98 Expression_eval_info eei;
101 eei.is_dot_available = is_dot_available;
102 eei.dot_value = dot_value;
103 eei.dot_section = dot_section;
105 // We assume the value is absolute, and only set this to a section
106 // if we find a section relative reference.
107 *result_section_pointer = NULL;
108 eei.result_section_pointer = result_section_pointer;
110 return this->value(&eei);
115 class Integer_expression : public Expression
118 Integer_expression(uint64_t val)
123 value(const Expression_eval_info*)
124 { return this->val_; }
128 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
134 extern "C" Expression*
135 script_exp_integer(uint64_t val)
137 return new Integer_expression(val);
140 // An expression whose value is the value of a symbol.
142 class Symbol_expression : public Expression
145 Symbol_expression(const char* name, size_t length)
146 : name_(name, length)
150 value(const Expression_eval_info*);
154 { fprintf(f, "%s", this->name_.c_str()); }
161 Symbol_expression::value(const Expression_eval_info* eei)
163 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
164 if (sym == NULL || !sym->is_defined())
166 gold_error(_("undefined symbol '%s' referenced in expression"),
167 this->name_.c_str());
171 *eei->result_section_pointer = sym->output_section();
173 if (parameters->get_size() == 32)
174 return eei->symtab->get_sized_symbol<32>(sym)->value();
175 else if (parameters->get_size() == 64)
176 return eei->symtab->get_sized_symbol<64>(sym)->value();
181 // An expression whose value is the value of the special symbol ".".
182 // This is only valid within a SECTIONS clause.
184 class Dot_expression : public Expression
191 value(const Expression_eval_info*);
199 Dot_expression::value(const Expression_eval_info* eei)
201 if (!eei->is_dot_available)
203 gold_error(_("invalid reference to dot symbol outside of "
207 *eei->result_section_pointer = eei->dot_section;
208 return eei->dot_value;
211 // A string. This is either the name of a symbol, or ".".
213 extern "C" Expression*
214 script_exp_string(const char* name, size_t length)
216 if (length == 1 && name[0] == '.')
217 return new Dot_expression();
219 return new Symbol_expression(name, length);
222 // A unary expression.
224 class Unary_expression : public Expression
227 Unary_expression(Expression* arg)
232 { delete this->arg_; }
236 arg_value(const Expression_eval_info* eei,
237 Output_section** arg_section_pointer) const
239 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
240 eei->is_dot_available,
243 arg_section_pointer);
247 arg_print(FILE* f) const
248 { this->arg_->print(f); }
254 // Handle unary operators. We use a preprocessor macro as a hack to
255 // capture the C operator.
257 #define UNARY_EXPRESSION(NAME, OPERATOR) \
258 class Unary_ ## NAME : public Unary_expression \
261 Unary_ ## NAME(Expression* arg) \
262 : Unary_expression(arg) \
266 value(const Expression_eval_info* eei) \
268 Output_section* arg_section; \
269 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
270 if (arg_section != NULL && parameters->output_is_object()) \
271 gold_warning(_("unary " #NAME " applied to section " \
272 "relative value")); \
277 print(FILE* f) const \
279 fprintf(f, "(%s ", #OPERATOR); \
280 this->arg_print(f); \
285 extern "C" Expression* \
286 script_exp_unary_ ## NAME(Expression* arg) \
288 return new Unary_ ## NAME(arg); \
291 UNARY_EXPRESSION(minus, -)
292 UNARY_EXPRESSION(logical_not, !)
293 UNARY_EXPRESSION(bitwise_not, ~)
295 // A binary expression.
297 class Binary_expression : public Expression
300 Binary_expression(Expression* left, Expression* right)
301 : left_(left), right_(right)
312 left_value(const Expression_eval_info* eei,
313 Output_section** section_pointer) const
315 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
316 eei->is_dot_available,
323 right_value(const Expression_eval_info* eei,
324 Output_section** section_pointer) const
326 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
327 eei->is_dot_available,
334 left_print(FILE* f) const
335 { this->left_->print(f); }
338 right_print(FILE* f) const
339 { this->right_->print(f); }
341 // This is a call to function FUNCTION_NAME. Print it. This is for
344 print_function(FILE* f, const char *function_name) const
346 fprintf(f, "%s(", function_name);
349 this->right_print(f);
358 // Handle binary operators. We use a preprocessor macro as a hack to
359 // capture the C operator. KEEP_LEFT means that if the left operand
360 // is section relative and the right operand is not, the result uses
361 // the same section as the left operand. KEEP_RIGHT is the same with
362 // left and right swapped. IS_DIV means that we need to give an error
363 // if the right operand is zero. WARN means that we should warn if
364 // used on section relative values in a relocatable link. We always
365 // warn if used on values in different sections in a relocatable link.
367 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
368 class Binary_ ## NAME : public Binary_expression \
371 Binary_ ## NAME(Expression* left, Expression* right) \
372 : Binary_expression(left, right) \
376 value(const Expression_eval_info* eei) \
378 Output_section* left_section; \
379 uint64_t left = this->left_value(eei, &left_section); \
380 Output_section* right_section; \
381 uint64_t right = this->right_value(eei, &right_section); \
382 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
383 *eei->result_section_pointer = right_section; \
385 && left_section != NULL \
386 && right_section == NULL) \
387 *eei->result_section_pointer = left_section; \
388 else if ((WARN || left_section != right_section) \
389 && (left_section != NULL || right_section != NULL) \
390 && parameters->output_is_object()) \
391 gold_warning(_("binary " #NAME " applied to section " \
392 "relative value")); \
393 if (IS_DIV && right == 0) \
395 gold_error(_(#NAME " by zero")); \
398 return left OPERATOR right; \
402 print(FILE* f) const \
405 this->left_print(f); \
406 fprintf(f, " %s ", #OPERATOR); \
407 this->right_print(f); \
412 extern "C" Expression* \
413 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
415 return new Binary_ ## NAME(left, right); \
418 BINARY_EXPRESSION(mult, *, false, false, false, true)
419 BINARY_EXPRESSION(div, /, false, false, true, true)
420 BINARY_EXPRESSION(mod, %, false, false, true, true)
421 BINARY_EXPRESSION(add, +, true, true, false, true)
422 BINARY_EXPRESSION(sub, -, true, false, false, false)
423 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
424 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
425 BINARY_EXPRESSION(eq, ==, false, false, false, false)
426 BINARY_EXPRESSION(ne, !=, false, false, false, false)
427 BINARY_EXPRESSION(le, <=, false, false, false, false)
428 BINARY_EXPRESSION(ge, >=, false, false, false, false)
429 BINARY_EXPRESSION(lt, <, false, false, false, false)
430 BINARY_EXPRESSION(gt, >, false, false, false, false)
431 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
432 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
433 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
434 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
435 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
437 // A trinary expression.
439 class Trinary_expression : public Expression
442 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
443 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
446 ~Trinary_expression()
455 arg1_value(const Expression_eval_info* eei,
456 Output_section** section_pointer) const
458 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
459 eei->is_dot_available,
466 arg2_value(const Expression_eval_info* eei,
467 Output_section** section_pointer) const
469 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
470 eei->is_dot_available,
477 arg3_value(const Expression_eval_info* eei,
478 Output_section** section_pointer) const
480 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
481 eei->is_dot_available,
488 arg1_print(FILE* f) const
489 { this->arg1_->print(f); }
492 arg2_print(FILE* f) const
493 { this->arg2_->print(f); }
496 arg3_print(FILE* f) const
497 { this->arg3_->print(f); }
505 // The conditional operator.
507 class Trinary_cond : public Trinary_expression
510 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
511 : Trinary_expression(arg1, arg2, arg3)
515 value(const Expression_eval_info* eei)
517 Output_section* arg1_section;
518 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
520 ? this->arg2_value(eei, eei->result_section_pointer)
521 : this->arg3_value(eei, eei->result_section_pointer));
537 extern "C" Expression*
538 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
540 return new Trinary_cond(arg1, arg2, arg3);
545 class Max_expression : public Binary_expression
548 Max_expression(Expression* left, Expression* right)
549 : Binary_expression(left, right)
553 value(const Expression_eval_info* eei)
555 Output_section* left_section;
556 uint64_t left = this->left_value(eei, &left_section);
557 Output_section* right_section;
558 uint64_t right = this->right_value(eei, &right_section);
559 if (left_section == right_section)
560 *eei->result_section_pointer = left_section;
561 else if ((left_section != NULL || right_section != NULL)
562 && parameters->output_is_object())
563 gold_warning(_("max applied to section relative value"));
564 return std::max(left, right);
569 { this->print_function(f, "MAX"); }
572 extern "C" Expression*
573 script_exp_function_max(Expression* left, Expression* right)
575 return new Max_expression(left, right);
580 class Min_expression : public Binary_expression
583 Min_expression(Expression* left, Expression* right)
584 : Binary_expression(left, right)
588 value(const Expression_eval_info* eei)
590 Output_section* left_section;
591 uint64_t left = this->left_value(eei, &left_section);
592 Output_section* right_section;
593 uint64_t right = this->right_value(eei, &right_section);
594 if (left_section == right_section)
595 *eei->result_section_pointer = left_section;
596 else if ((left_section != NULL || right_section != NULL)
597 && parameters->output_is_object())
598 gold_warning(_("min applied to section relative value"));
599 return std::min(left, right);
604 { this->print_function(f, "MIN"); }
607 extern "C" Expression*
608 script_exp_function_min(Expression* left, Expression* right)
610 return new Min_expression(left, right);
615 class Align_expression : public Binary_expression
618 Align_expression(Expression* left, Expression* right)
619 : Binary_expression(left, right)
623 value(const Expression_eval_info* eei)
625 Output_section* align_section;
626 uint64_t align = this->right_value(eei, &align_section);
627 if (align_section != NULL
628 && parameters->output_is_object())
629 gold_warning(_("aligning to section relative value"));
631 uint64_t value = this->left_value(eei, eei->result_section_pointer);
634 return ((value + align - 1) / align) * align;
639 { this->print_function(f, "ALIGN"); }
642 extern "C" Expression*
643 script_exp_function_align(Expression* left, Expression* right)
645 return new Align_expression(left, right);
650 class Assert_expression : public Unary_expression
653 Assert_expression(Expression* arg, const char* message, size_t length)
654 : Unary_expression(arg), message_(message, length)
658 value(const Expression_eval_info* eei)
660 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
662 gold_error("%s", this->message_.c_str());
669 fprintf(f, "ASSERT(");
671 fprintf(f, ", %s)", this->message_.c_str());
675 std::string message_;
678 extern "C" Expression*
679 script_exp_function_assert(Expression* expr, const char* message,
682 return new Assert_expression(expr, message, length);
687 class Addr_expression : public Expression
690 Addr_expression(const char* section_name, size_t section_name_len)
691 : section_name_(section_name, section_name_len)
695 value(const Expression_eval_info*);
699 { fprintf(f, "ADDR(%s)", this->section_name_.c_str()); }
702 std::string section_name_;
706 Addr_expression::value(const Expression_eval_info* eei)
708 const char* section_name = this->section_name_.c_str();
709 Output_section* os = eei->layout->find_output_section(section_name);
712 gold_error("ADDR called on nonexistent output section '%s'",
717 *eei->result_section_pointer = os;
719 return os->address();
722 extern "C" Expression*
723 script_exp_function_addr(const char* section_name, size_t section_name_len)
725 return new Addr_expression(section_name, section_name_len);
728 // CONSTANT. It would be nice if we could simply evaluate this
729 // immediately and return an Integer_expression, but unfortunately we
730 // don't know the target.
732 class Constant_expression : public Expression
735 Constant_expression(const char* name, size_t length);
738 value(const Expression_eval_info*);
741 print(FILE* f) const;
744 enum Constant_function
746 CONSTANT_MAXPAGESIZE,
747 CONSTANT_COMMONPAGESIZE
750 Constant_function function_;
753 Constant_expression::Constant_expression(const char* name, size_t length)
755 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
756 this->function_ = CONSTANT_MAXPAGESIZE;
757 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
758 this->function_ = CONSTANT_COMMONPAGESIZE;
761 std::string s(name, length);
762 gold_error(_("unknown constant %s"), s.c_str());
763 this->function_ = CONSTANT_MAXPAGESIZE;
768 Constant_expression::value(const Expression_eval_info*)
770 switch (this->function_)
772 case CONSTANT_MAXPAGESIZE:
773 return parameters->target()->abi_pagesize();
774 case CONSTANT_COMMONPAGESIZE:
775 return parameters->target()->common_pagesize();
782 Constant_expression::print(FILE* f) const
785 switch (this->function_)
787 case CONSTANT_MAXPAGESIZE:
788 name = "MAXPAGESIZE";
790 case CONSTANT_COMMONPAGESIZE:
791 name = "COMMONPAGESIZE";
796 fprintf(f, "CONSTANT(%s)", name);
799 extern "C" Expression*
800 script_exp_function_constant(const char* name, size_t length)
802 return new Constant_expression(name, length);
805 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
806 // back to the general case.
808 extern "C" Expression*
809 script_exp_function_data_segment_align(Expression* left, Expression*)
811 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
812 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
813 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
815 return script_exp_binary_add(e1, e3);
818 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
820 extern "C" Expression*
821 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
826 // DATA_SEGMENT_END. FIXME: This is not implemented.
828 extern "C" Expression*
829 script_exp_function_data_segment_end(Expression* val)
836 class Sizeof_headers_expression : public Expression
839 Sizeof_headers_expression()
843 value(const Expression_eval_info*);
847 { fprintf(f, "SIZEOF_HEADERS"); }
851 Sizeof_headers_expression::value(const Expression_eval_info* eei)
853 unsigned int ehdr_size;
854 unsigned int phdr_size;
855 if (parameters->get_size() == 32)
857 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
858 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
860 else if (parameters->get_size() == 64)
862 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
863 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
868 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
871 extern "C" Expression*
872 script_exp_function_sizeof_headers()
874 return new Sizeof_headers_expression();
879 extern "C" Expression*
880 script_exp_function_defined(const char*, size_t)
882 gold_fatal(_("DEFINED not implemented"));
885 extern "C" Expression*
886 script_exp_function_alignof(const char*, size_t)
888 gold_fatal(_("ALIGNOF not implemented"));
891 extern "C" Expression*
892 script_exp_function_sizeof(const char*, size_t)
894 gold_fatal(_("SIZEOF not implemented"));
897 extern "C" Expression*
898 script_exp_function_loadaddr(const char*, size_t)
900 gold_fatal(_("LOADADDR not implemented"));
903 extern "C" Expression*
904 script_exp_function_origin(const char*, size_t)
906 gold_fatal(_("ORIGIN not implemented"));
909 extern "C" Expression*
910 script_exp_function_length(const char*, size_t)
912 gold_fatal(_("LENGTH not implemented"));
915 extern "C" Expression*
916 script_exp_function_absolute(Expression*)
918 gold_fatal(_("ABSOLUTE not implemented"));
921 extern "C" Expression*
922 script_exp_function_segment_start(const char*, size_t, Expression*)
924 gold_fatal(_("SEGMENT_START not implemented"));
927 } // End namespace gold.