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 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.
64 // The section in which the dot symbol is defined; this is NULL if
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.
76 Expression::eval(const Symbol_table* symtab, const Layout* layout,
77 bool check_assertions)
79 return this->eval_maybe_dot(symtab, layout, check_assertions,
80 false, 0, NULL, NULL, NULL, false);
83 // Evaluate an expression which may refer to the dot symbol.
86 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
87 bool check_assertions, uint64_t dot_value,
88 Output_section* dot_section,
89 Output_section** result_section_pointer,
90 uint64_t* result_alignment_pointer,
91 bool is_section_dot_assignment)
93 return this->eval_maybe_dot(symtab, layout, check_assertions, true,
94 dot_value, dot_section, result_section_pointer,
95 result_alignment_pointer,
96 is_section_dot_assignment);
99 // Evaluate an expression which may or may not refer to the dot
103 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
104 bool check_assertions, bool is_dot_available,
105 uint64_t dot_value, Output_section* dot_section,
106 Output_section** result_section_pointer,
107 uint64_t* result_alignment_pointer,
108 bool is_section_dot_assignment)
110 Expression_eval_info eei;
113 eei.check_assertions = check_assertions;
114 eei.is_dot_available = is_dot_available;
115 eei.dot_value = dot_value;
116 eei.dot_section = dot_section;
118 // We assume the value is absolute, and only set this to a section
119 // if we find a section-relative reference.
120 if (result_section_pointer != NULL)
121 *result_section_pointer = NULL;
122 eei.result_section_pointer = result_section_pointer;
124 eei.result_alignment_pointer = result_alignment_pointer;
126 uint64_t val = this->value(&eei);
128 // If this is an assignment to dot within a section, and the value
129 // is absolute, treat it as a section-relative offset.
130 if (is_section_dot_assignment && *result_section_pointer == NULL)
132 gold_assert(dot_section != NULL);
133 val += dot_section->address();
134 *result_section_pointer = dot_section;
141 class Integer_expression : public Expression
144 Integer_expression(uint64_t val)
149 value(const Expression_eval_info*)
150 { return this->val_; }
154 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
160 extern "C" Expression*
161 script_exp_integer(uint64_t val)
163 return new Integer_expression(val);
166 // An expression whose value is the value of a symbol.
168 class Symbol_expression : public Expression
171 Symbol_expression(const char* name, size_t length)
172 : name_(name, length)
176 value(const Expression_eval_info*);
180 { fprintf(f, "%s", this->name_.c_str()); }
187 Symbol_expression::value(const Expression_eval_info* eei)
189 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
190 if (sym == NULL || !sym->is_defined())
192 gold_error(_("undefined symbol '%s' referenced in expression"),
193 this->name_.c_str());
197 if (eei->result_section_pointer != NULL)
198 *eei->result_section_pointer = sym->output_section();
200 if (parameters->target().get_size() == 32)
201 return eei->symtab->get_sized_symbol<32>(sym)->value();
202 else if (parameters->target().get_size() == 64)
203 return eei->symtab->get_sized_symbol<64>(sym)->value();
208 // An expression whose value is the value of the special symbol ".".
209 // This is only valid within a SECTIONS clause.
211 class Dot_expression : public Expression
218 value(const Expression_eval_info*);
226 Dot_expression::value(const Expression_eval_info* eei)
228 if (!eei->is_dot_available)
230 gold_error(_("invalid reference to dot symbol outside of "
234 if (eei->result_section_pointer != NULL)
235 *eei->result_section_pointer = eei->dot_section;
236 return eei->dot_value;
239 // A string. This is either the name of a symbol, or ".".
241 extern "C" Expression*
242 script_exp_string(const char* name, size_t length)
244 if (length == 1 && name[0] == '.')
245 return new Dot_expression();
247 return new Symbol_expression(name, length);
250 // A unary expression.
252 class Unary_expression : public Expression
255 Unary_expression(Expression* arg)
260 { delete this->arg_; }
264 arg_value(const Expression_eval_info* eei,
265 Output_section** arg_section_pointer) const
267 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
268 eei->check_assertions,
269 eei->is_dot_available,
273 eei->result_alignment_pointer,
278 arg_print(FILE* f) const
279 { this->arg_->print(f); }
285 // Handle unary operators. We use a preprocessor macro as a hack to
286 // capture the C operator.
288 #define UNARY_EXPRESSION(NAME, OPERATOR) \
289 class Unary_ ## NAME : public Unary_expression \
292 Unary_ ## NAME(Expression* arg) \
293 : Unary_expression(arg) \
297 value(const Expression_eval_info* eei) \
299 Output_section* arg_section; \
300 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
301 if (arg_section != NULL && parameters->options().relocatable()) \
302 gold_warning(_("unary " #NAME " applied to section " \
303 "relative value")); \
308 print(FILE* f) const \
310 fprintf(f, "(%s ", #OPERATOR); \
311 this->arg_print(f); \
316 extern "C" Expression* \
317 script_exp_unary_ ## NAME(Expression* arg) \
319 return new Unary_ ## NAME(arg); \
322 UNARY_EXPRESSION(minus, -)
323 UNARY_EXPRESSION(logical_not, !)
324 UNARY_EXPRESSION(bitwise_not, ~)
326 // A binary expression.
328 class Binary_expression : public Expression
331 Binary_expression(Expression* left, Expression* right)
332 : left_(left), right_(right)
343 left_value(const Expression_eval_info* eei,
344 Output_section** section_pointer,
345 uint64_t* alignment_pointer) const
347 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
348 eei->check_assertions,
349 eei->is_dot_available,
358 right_value(const Expression_eval_info* eei,
359 Output_section** section_pointer,
360 uint64_t* alignment_pointer) const
362 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
363 eei->check_assertions,
364 eei->is_dot_available,
373 left_print(FILE* f) const
374 { this->left_->print(f); }
377 right_print(FILE* f) const
378 { this->right_->print(f); }
380 // This is a call to function FUNCTION_NAME. Print it. This is for
383 print_function(FILE* f, const char* function_name) const
385 fprintf(f, "%s(", function_name);
388 this->right_print(f);
397 // Handle binary operators. We use a preprocessor macro as a hack to
398 // capture the C operator. KEEP_LEFT means that if the left operand
399 // is section relative and the right operand is not, the result uses
400 // the same section as the left operand. KEEP_RIGHT is the same with
401 // left and right swapped. IS_DIV means that we need to give an error
402 // if the right operand is zero. WARN means that we should warn if
403 // used on section relative values in a relocatable link. We always
404 // warn if used on values in different sections in a relocatable link.
406 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
407 class Binary_ ## NAME : public Binary_expression \
410 Binary_ ## NAME(Expression* left, Expression* right) \
411 : Binary_expression(left, right) \
415 value(const Expression_eval_info* eei) \
417 Output_section* left_section; \
418 uint64_t left_alignment = 0; \
419 uint64_t left = this->left_value(eei, &left_section, \
421 Output_section* right_section; \
422 uint64_t right_alignment = 0; \
423 uint64_t right = this->right_value(eei, &right_section, \
425 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
427 if (eei->result_section_pointer != NULL) \
428 *eei->result_section_pointer = right_section; \
429 if (eei->result_alignment_pointer != NULL \
430 && right_alignment > *eei->result_alignment_pointer) \
431 *eei->result_alignment_pointer = right_alignment; \
434 && left_section != NULL \
435 && right_section == NULL) \
437 if (eei->result_section_pointer != NULL) \
438 *eei->result_section_pointer = left_section; \
439 if (eei->result_alignment_pointer != NULL \
440 && left_alignment > *eei->result_alignment_pointer) \
441 *eei->result_alignment_pointer = left_alignment; \
443 else if ((WARN || left_section != right_section) \
444 && (left_section != NULL || right_section != NULL) \
445 && parameters->options().relocatable()) \
446 gold_warning(_("binary " #NAME " applied to section " \
447 "relative value")); \
448 if (IS_DIV && right == 0) \
450 gold_error(_(#NAME " by zero")); \
453 return left OPERATOR right; \
457 print(FILE* f) const \
460 this->left_print(f); \
461 fprintf(f, " %s ", #OPERATOR); \
462 this->right_print(f); \
467 extern "C" Expression* \
468 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
470 return new Binary_ ## NAME(left, right); \
473 BINARY_EXPRESSION(mult, *, false, false, false, true)
474 BINARY_EXPRESSION(div, /, false, false, true, true)
475 BINARY_EXPRESSION(mod, %, false, false, true, true)
476 BINARY_EXPRESSION(add, +, true, true, false, true)
477 BINARY_EXPRESSION(sub, -, true, false, false, false)
478 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
479 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
480 BINARY_EXPRESSION(eq, ==, false, false, false, false)
481 BINARY_EXPRESSION(ne, !=, false, false, false, false)
482 BINARY_EXPRESSION(le, <=, false, false, false, false)
483 BINARY_EXPRESSION(ge, >=, false, false, false, false)
484 BINARY_EXPRESSION(lt, <, false, false, false, false)
485 BINARY_EXPRESSION(gt, >, false, false, false, false)
486 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
487 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
488 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
489 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
490 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
492 // A trinary expression.
494 class Trinary_expression : public Expression
497 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
498 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
501 ~Trinary_expression()
510 arg1_value(const Expression_eval_info* eei,
511 Output_section** section_pointer) const
513 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
514 eei->check_assertions,
515 eei->is_dot_available,
524 arg2_value(const Expression_eval_info* eei,
525 Output_section** section_pointer,
526 uint64_t* alignment_pointer) const
528 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
529 eei->check_assertions,
530 eei->is_dot_available,
539 arg3_value(const Expression_eval_info* eei,
540 Output_section** section_pointer,
541 uint64_t* alignment_pointer) const
543 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
544 eei->check_assertions,
545 eei->is_dot_available,
554 arg1_print(FILE* f) const
555 { this->arg1_->print(f); }
558 arg2_print(FILE* f) const
559 { this->arg2_->print(f); }
562 arg3_print(FILE* f) const
563 { this->arg3_->print(f); }
571 // The conditional operator.
573 class Trinary_cond : public Trinary_expression
576 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
577 : Trinary_expression(arg1, arg2, arg3)
581 value(const Expression_eval_info* eei)
583 Output_section* arg1_section;
584 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
586 ? this->arg2_value(eei, eei->result_section_pointer,
587 eei->result_alignment_pointer)
588 : this->arg3_value(eei, eei->result_section_pointer,
589 eei->result_alignment_pointer));
605 extern "C" Expression*
606 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
608 return new Trinary_cond(arg1, arg2, arg3);
613 class Max_expression : public Binary_expression
616 Max_expression(Expression* left, Expression* right)
617 : Binary_expression(left, right)
621 value(const Expression_eval_info* eei)
623 Output_section* left_section;
624 uint64_t left_alignment;
625 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
626 Output_section* right_section;
627 uint64_t right_alignment;
628 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
629 if (left_section == right_section)
631 if (eei->result_section_pointer != NULL)
632 *eei->result_section_pointer = left_section;
634 else if ((left_section != NULL || right_section != NULL)
635 && parameters->options().relocatable())
636 gold_warning(_("max applied to section relative value"));
637 if (eei->result_alignment_pointer != NULL)
639 uint64_t ra = *eei->result_alignment_pointer;
641 ra = std::max(ra, left_alignment);
642 else if (right > left)
643 ra = std::max(ra, right_alignment);
645 ra = std::max(ra, std::max(left_alignment, right_alignment));
646 *eei->result_alignment_pointer = ra;
648 return std::max(left, right);
653 { this->print_function(f, "MAX"); }
656 extern "C" Expression*
657 script_exp_function_max(Expression* left, Expression* right)
659 return new Max_expression(left, right);
664 class Min_expression : public Binary_expression
667 Min_expression(Expression* left, Expression* right)
668 : Binary_expression(left, right)
672 value(const Expression_eval_info* eei)
674 Output_section* left_section;
675 uint64_t left_alignment;
676 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
677 Output_section* right_section;
678 uint64_t right_alignment;
679 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
680 if (left_section == right_section)
682 if (eei->result_section_pointer != NULL)
683 *eei->result_section_pointer = left_section;
685 else if ((left_section != NULL || right_section != NULL)
686 && parameters->options().relocatable())
687 gold_warning(_("min applied to section relative value"));
688 if (eei->result_alignment_pointer != NULL)
690 uint64_t ra = *eei->result_alignment_pointer;
692 ra = std::max(ra, left_alignment);
693 else if (right < left)
694 ra = std::max(ra, right_alignment);
696 ra = std::max(ra, std::max(left_alignment, right_alignment));
697 *eei->result_alignment_pointer = ra;
699 return std::min(left, right);
704 { this->print_function(f, "MIN"); }
707 extern "C" Expression*
708 script_exp_function_min(Expression* left, Expression* right)
710 return new Min_expression(left, right);
713 // Class Section_expression. This is a parent class used for
714 // functions which take the name of an output section.
716 class Section_expression : public Expression
719 Section_expression(const char* section_name, size_t section_name_len)
720 : section_name_(section_name, section_name_len)
724 value(const Expression_eval_info*);
728 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
731 // The child class must implement this.
733 value_from_output_section(const Expression_eval_info*,
734 Output_section*) = 0;
736 // The child class must implement this.
738 value_from_script_output_section(uint64_t address, uint64_t load_address,
739 uint64_t addralign, uint64_t size) = 0;
741 // The child class must implement this.
743 function_name() const = 0;
746 std::string section_name_;
750 Section_expression::value(const Expression_eval_info* eei)
752 const char* section_name = this->section_name_.c_str();
753 Output_section* os = eei->layout->find_output_section(section_name);
755 return this->value_from_output_section(eei, os);
758 uint64_t load_address;
761 const Script_options* ss = eei->layout->script_options();
762 if (ss->saw_sections_clause())
764 if (ss->script_sections()->get_output_section_info(section_name,
769 return this->value_from_script_output_section(address, load_address,
773 gold_error("%s called on nonexistent output section '%s'",
774 this->function_name(), section_name);
778 // ABSOLUTE function.
780 class Absolute_expression : public Unary_expression
783 Absolute_expression(Expression* arg)
784 : Unary_expression(arg)
788 value(const Expression_eval_info* eei)
790 uint64_t ret = this->arg_value(eei, NULL);
791 // Force the value to be absolute.
792 if (eei->result_section_pointer != NULL)
793 *eei->result_section_pointer = NULL;
800 fprintf(f, "ABSOLUTE(");
806 extern "C" Expression*
807 script_exp_function_absolute(Expression* arg)
809 return new Absolute_expression(arg);
814 class Align_expression : public Binary_expression
817 Align_expression(Expression* left, Expression* right)
818 : Binary_expression(left, right)
822 value(const Expression_eval_info* eei)
824 Output_section* align_section;
825 uint64_t align = this->right_value(eei, &align_section, NULL);
826 if (align_section != NULL
827 && parameters->options().relocatable())
828 gold_warning(_("aligning to section relative value"));
830 if (eei->result_alignment_pointer != NULL
831 && align > *eei->result_alignment_pointer)
834 while ((a & (a - 1)) != 0)
836 *eei->result_alignment_pointer = a;
839 uint64_t value = this->left_value(eei, eei->result_section_pointer, NULL);
842 return ((value + align - 1) / align) * align;
847 { this->print_function(f, "ALIGN"); }
850 extern "C" Expression*
851 script_exp_function_align(Expression* left, Expression* right)
853 return new Align_expression(left, right);
858 class Assert_expression : public Unary_expression
861 Assert_expression(Expression* arg, const char* message, size_t length)
862 : Unary_expression(arg), message_(message, length)
866 value(const Expression_eval_info* eei)
868 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
869 if (!value && eei->check_assertions)
870 gold_error("%s", this->message_.c_str());
877 fprintf(f, "ASSERT(");
879 fprintf(f, ", %s)", this->message_.c_str());
883 std::string message_;
886 extern "C" Expression*
887 script_exp_function_assert(Expression* expr, const char* message,
890 return new Assert_expression(expr, message, length);
895 class Addr_expression : public Section_expression
898 Addr_expression(const char* section_name, size_t section_name_len)
899 : Section_expression(section_name, section_name_len)
904 value_from_output_section(const Expression_eval_info* eei,
907 if (eei->result_section_pointer != NULL)
908 *eei->result_section_pointer = os;
909 return os->address();
913 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
918 function_name() const
922 extern "C" Expression*
923 script_exp_function_addr(const char* section_name, size_t section_name_len)
925 return new Addr_expression(section_name, section_name_len);
930 class Alignof_expression : public Section_expression
933 Alignof_expression(const char* section_name, size_t section_name_len)
934 : Section_expression(section_name, section_name_len)
939 value_from_output_section(const Expression_eval_info*,
941 { return os->addralign(); }
944 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
946 { return addralign; }
949 function_name() const
950 { return "ALIGNOF"; }
953 extern "C" Expression*
954 script_exp_function_alignof(const char* section_name, size_t section_name_len)
956 return new Alignof_expression(section_name, section_name_len);
959 // CONSTANT. It would be nice if we could simply evaluate this
960 // immediately and return an Integer_expression, but unfortunately we
961 // don't know the target.
963 class Constant_expression : public Expression
966 Constant_expression(const char* name, size_t length);
969 value(const Expression_eval_info*);
972 print(FILE* f) const;
975 enum Constant_function
977 CONSTANT_MAXPAGESIZE,
978 CONSTANT_COMMONPAGESIZE
981 Constant_function function_;
984 Constant_expression::Constant_expression(const char* name, size_t length)
986 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
987 this->function_ = CONSTANT_MAXPAGESIZE;
988 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
989 this->function_ = CONSTANT_COMMONPAGESIZE;
992 std::string s(name, length);
993 gold_error(_("unknown constant %s"), s.c_str());
994 this->function_ = CONSTANT_MAXPAGESIZE;
999 Constant_expression::value(const Expression_eval_info*)
1001 switch (this->function_)
1003 case CONSTANT_MAXPAGESIZE:
1004 return parameters->target().abi_pagesize();
1005 case CONSTANT_COMMONPAGESIZE:
1006 return parameters->target().common_pagesize();
1013 Constant_expression::print(FILE* f) const
1016 switch (this->function_)
1018 case CONSTANT_MAXPAGESIZE:
1019 name = "MAXPAGESIZE";
1021 case CONSTANT_COMMONPAGESIZE:
1022 name = "COMMONPAGESIZE";
1027 fprintf(f, "CONSTANT(%s)", name);
1030 extern "C" Expression*
1031 script_exp_function_constant(const char* name, size_t length)
1033 return new Constant_expression(name, length);
1036 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1037 // back to the general case.
1039 extern "C" Expression*
1040 script_exp_function_data_segment_align(Expression* left, Expression*)
1042 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
1043 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
1044 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
1046 return script_exp_binary_add(e1, e3);
1049 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1051 extern "C" Expression*
1052 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
1057 // DATA_SEGMENT_END. FIXME: This is not implemented.
1059 extern "C" Expression*
1060 script_exp_function_data_segment_end(Expression* val)
1065 // DEFINED function.
1067 class Defined_expression : public Expression
1070 Defined_expression(const char* symbol_name, size_t symbol_name_len)
1071 : symbol_name_(symbol_name, symbol_name_len)
1075 value(const Expression_eval_info* eei)
1077 Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
1078 return sym != NULL && sym->is_defined();
1082 print(FILE* f) const
1083 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
1086 std::string symbol_name_;
1089 extern "C" Expression*
1090 script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
1092 return new Defined_expression(symbol_name, symbol_name_len);
1095 // LOADADDR function
1097 class Loadaddr_expression : public Section_expression
1100 Loadaddr_expression(const char* section_name, size_t section_name_len)
1101 : Section_expression(section_name, section_name_len)
1106 value_from_output_section(const Expression_eval_info* eei,
1109 if (os->has_load_address())
1110 return os->load_address();
1113 if (eei->result_section_pointer != NULL)
1114 *eei->result_section_pointer = os;
1115 return os->address();
1120 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1122 { return load_address; }
1125 function_name() const
1126 { return "LOADADDR"; }
1129 extern "C" Expression*
1130 script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
1132 return new Loadaddr_expression(section_name, section_name_len);
1137 class Sizeof_expression : public Section_expression
1140 Sizeof_expression(const char* section_name, size_t section_name_len)
1141 : Section_expression(section_name, section_name_len)
1146 value_from_output_section(const Expression_eval_info*,
1149 // We can not use data_size here, as the size of the section may
1150 // not have been finalized. Instead we get whatever the current
1151 // size is. This will work correctly for backward references in
1153 return os->current_data_size();
1157 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1162 function_name() const
1163 { return "SIZEOF"; }
1166 extern "C" Expression*
1167 script_exp_function_sizeof(const char* section_name, size_t section_name_len)
1169 return new Sizeof_expression(section_name, section_name_len);
1174 class Sizeof_headers_expression : public Expression
1177 Sizeof_headers_expression()
1181 value(const Expression_eval_info*);
1184 print(FILE* f) const
1185 { fprintf(f, "SIZEOF_HEADERS"); }
1189 Sizeof_headers_expression::value(const Expression_eval_info* eei)
1191 unsigned int ehdr_size;
1192 unsigned int phdr_size;
1193 if (parameters->target().get_size() == 32)
1195 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1196 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1198 else if (parameters->target().get_size() == 64)
1200 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
1201 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
1206 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
1209 extern "C" Expression*
1210 script_exp_function_sizeof_headers()
1212 return new Sizeof_headers_expression();
1217 class Segment_start_expression : public Unary_expression
1220 Segment_start_expression(const char* segment_name, size_t segment_name_len,
1221 Expression* default_value)
1222 : Unary_expression(default_value),
1223 segment_name_(segment_name, segment_name_len)
1227 value(const Expression_eval_info*);
1230 print(FILE* f) const
1232 fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
1238 std::string segment_name_;
1242 Segment_start_expression::value(const Expression_eval_info* eei)
1244 // Check for command line overrides.
1245 if (parameters->options().user_set_Ttext()
1246 && this->segment_name_ == ".text")
1247 return parameters->options().Ttext();
1248 else if (parameters->options().user_set_Tdata()
1249 && this->segment_name_ == ".data")
1250 return parameters->options().Tdata();
1251 else if (parameters->options().user_set_Tbss()
1252 && this->segment_name_ == ".bss")
1253 return parameters->options().Tbss();
1256 uint64_t ret = this->arg_value(eei, NULL);
1257 // Force the value to be absolute.
1258 if (eei->result_section_pointer != NULL)
1259 *eei->result_section_pointer = NULL;
1264 extern "C" Expression*
1265 script_exp_function_segment_start(const char* segment_name,
1266 size_t segment_name_len,
1267 Expression* default_value)
1269 return new Segment_start_expression(segment_name, segment_name_len,
1273 } // End namespace gold.