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);
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)
92 return this->eval_maybe_dot(symtab, layout, check_assertions, true,
93 dot_value, dot_section, result_section_pointer,
94 result_alignment_pointer);
97 // Evaluate an expression which may or may not refer to the dot
101 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
102 bool check_assertions, bool is_dot_available,
103 uint64_t dot_value, Output_section* dot_section,
104 Output_section** result_section_pointer,
105 uint64_t* result_alignment_pointer)
107 Expression_eval_info eei;
110 eei.check_assertions = check_assertions;
111 eei.is_dot_available = is_dot_available;
112 eei.dot_value = dot_value;
113 eei.dot_section = dot_section;
115 // We assume the value is absolute, and only set this to a section
116 // if we find a section relative reference.
117 if (result_section_pointer != NULL)
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);
128 class Integer_expression : public Expression
131 Integer_expression(uint64_t val)
136 value(const Expression_eval_info*)
137 { return this->val_; }
141 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->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
158 Symbol_expression(const char* name, size_t length)
159 : name_(name, length)
163 value(const Expression_eval_info*);
167 { fprintf(f, "%s", this->name_.c_str()); }
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());
184 if (eei->result_section_pointer != NULL)
185 *eei->result_section_pointer = sym->output_section();
187 if (parameters->target().get_size() == 32)
188 return eei->symtab->get_sized_symbol<32>(sym)->value();
189 else if (parameters->target().get_size() == 64)
190 return eei->symtab->get_sized_symbol<64>(sym)->value();
195 // An expression whose value is the value of the special symbol ".".
196 // This is only valid within a SECTIONS clause.
198 class Dot_expression : public Expression
205 value(const Expression_eval_info*);
213 Dot_expression::value(const Expression_eval_info* eei)
215 if (!eei->is_dot_available)
217 gold_error(_("invalid reference to dot symbol outside of "
221 if (eei->result_section_pointer != NULL)
222 *eei->result_section_pointer = eei->dot_section;
223 return eei->dot_value;
226 // A string. This is either the name of a symbol, or ".".
228 extern "C" Expression*
229 script_exp_string(const char* name, size_t length)
231 if (length == 1 && name[0] == '.')
232 return new Dot_expression();
234 return new Symbol_expression(name, length);
237 // A unary expression.
239 class Unary_expression : public Expression
242 Unary_expression(Expression* arg)
247 { delete this->arg_; }
251 arg_value(const Expression_eval_info* eei,
252 Output_section** arg_section_pointer) const
254 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
255 eei->check_assertions,
256 eei->is_dot_available,
260 eei->result_alignment_pointer);
264 arg_print(FILE* f) const
265 { this->arg_->print(f); }
271 // Handle unary operators. We use a preprocessor macro as a hack to
272 // capture the C operator.
274 #define UNARY_EXPRESSION(NAME, OPERATOR) \
275 class Unary_ ## NAME : public Unary_expression \
278 Unary_ ## NAME(Expression* arg) \
279 : Unary_expression(arg) \
283 value(const Expression_eval_info* eei) \
285 Output_section* arg_section; \
286 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
287 if (arg_section != NULL && parameters->options().relocatable()) \
288 gold_warning(_("unary " #NAME " applied to section " \
289 "relative value")); \
294 print(FILE* f) const \
296 fprintf(f, "(%s ", #OPERATOR); \
297 this->arg_print(f); \
302 extern "C" Expression* \
303 script_exp_unary_ ## NAME(Expression* arg) \
305 return new Unary_ ## NAME(arg); \
308 UNARY_EXPRESSION(minus, -)
309 UNARY_EXPRESSION(logical_not, !)
310 UNARY_EXPRESSION(bitwise_not, ~)
312 // A binary expression.
314 class Binary_expression : public Expression
317 Binary_expression(Expression* left, Expression* right)
318 : left_(left), right_(right)
329 left_value(const Expression_eval_info* eei,
330 Output_section** section_pointer,
331 uint64_t* alignment_pointer) const
333 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
334 eei->check_assertions,
335 eei->is_dot_available,
343 right_value(const Expression_eval_info* eei,
344 Output_section** section_pointer,
345 uint64_t* alignment_pointer) const
347 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
348 eei->check_assertions,
349 eei->is_dot_available,
357 left_print(FILE* f) const
358 { this->left_->print(f); }
361 right_print(FILE* f) const
362 { this->right_->print(f); }
364 // This is a call to function FUNCTION_NAME. Print it. This is for
367 print_function(FILE* f, const char* function_name) const
369 fprintf(f, "%s(", function_name);
372 this->right_print(f);
381 // Handle binary operators. We use a preprocessor macro as a hack to
382 // capture the C operator. KEEP_LEFT means that if the left operand
383 // is section relative and the right operand is not, the result uses
384 // the same section as the left operand. KEEP_RIGHT is the same with
385 // left and right swapped. IS_DIV means that we need to give an error
386 // if the right operand is zero. WARN means that we should warn if
387 // used on section relative values in a relocatable link. We always
388 // warn if used on values in different sections in a relocatable link.
390 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
391 class Binary_ ## NAME : public Binary_expression \
394 Binary_ ## NAME(Expression* left, Expression* right) \
395 : Binary_expression(left, right) \
399 value(const Expression_eval_info* eei) \
401 Output_section* left_section; \
402 uint64_t left_alignment; \
403 uint64_t left = this->left_value(eei, &left_section, \
405 Output_section* right_section; \
406 uint64_t right_alignment; \
407 uint64_t right = this->right_value(eei, &right_section, \
409 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
411 if (eei->result_section_pointer != NULL) \
412 *eei->result_section_pointer = right_section; \
413 if (eei->result_alignment_pointer != NULL) \
414 *eei->result_alignment_pointer = right_alignment; \
417 && left_section != NULL \
418 && right_section == NULL) \
420 if (eei->result_section_pointer != NULL) \
421 *eei->result_section_pointer = left_section; \
422 if (eei->result_alignment_pointer != NULL) \
423 *eei->result_alignment_pointer = right_alignment; \
425 else if ((WARN || left_section != right_section) \
426 && (left_section != NULL || right_section != NULL) \
427 && parameters->options().relocatable()) \
428 gold_warning(_("binary " #NAME " applied to section " \
429 "relative value")); \
430 if (IS_DIV && right == 0) \
432 gold_error(_(#NAME " by zero")); \
435 return left OPERATOR right; \
439 print(FILE* f) const \
442 this->left_print(f); \
443 fprintf(f, " %s ", #OPERATOR); \
444 this->right_print(f); \
449 extern "C" Expression* \
450 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
452 return new Binary_ ## NAME(left, right); \
455 BINARY_EXPRESSION(mult, *, false, false, false, true)
456 BINARY_EXPRESSION(div, /, false, false, true, true)
457 BINARY_EXPRESSION(mod, %, false, false, true, true)
458 BINARY_EXPRESSION(add, +, true, true, false, true)
459 BINARY_EXPRESSION(sub, -, true, false, false, false)
460 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
461 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
462 BINARY_EXPRESSION(eq, ==, false, false, false, false)
463 BINARY_EXPRESSION(ne, !=, false, false, false, false)
464 BINARY_EXPRESSION(le, <=, false, false, false, false)
465 BINARY_EXPRESSION(ge, >=, false, false, false, false)
466 BINARY_EXPRESSION(lt, <, false, false, false, false)
467 BINARY_EXPRESSION(gt, >, false, false, false, false)
468 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
469 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
470 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
471 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
472 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
474 // A trinary expression.
476 class Trinary_expression : public Expression
479 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
480 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
483 ~Trinary_expression()
492 arg1_value(const Expression_eval_info* eei,
493 Output_section** section_pointer) const
495 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
496 eei->check_assertions,
497 eei->is_dot_available,
505 arg2_value(const Expression_eval_info* eei,
506 Output_section** section_pointer,
507 uint64_t* alignment_pointer) const
509 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
510 eei->check_assertions,
511 eei->is_dot_available,
519 arg3_value(const Expression_eval_info* eei,
520 Output_section** section_pointer,
521 uint64_t* alignment_pointer) const
523 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
524 eei->check_assertions,
525 eei->is_dot_available,
533 arg1_print(FILE* f) const
534 { this->arg1_->print(f); }
537 arg2_print(FILE* f) const
538 { this->arg2_->print(f); }
541 arg3_print(FILE* f) const
542 { this->arg3_->print(f); }
550 // The conditional operator.
552 class Trinary_cond : public Trinary_expression
555 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
556 : Trinary_expression(arg1, arg2, arg3)
560 value(const Expression_eval_info* eei)
562 Output_section* arg1_section;
563 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
565 ? this->arg2_value(eei, eei->result_section_pointer,
566 eei->result_alignment_pointer)
567 : this->arg3_value(eei, eei->result_section_pointer,
568 eei->result_alignment_pointer));
584 extern "C" Expression*
585 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
587 return new Trinary_cond(arg1, arg2, arg3);
592 class Max_expression : public Binary_expression
595 Max_expression(Expression* left, Expression* right)
596 : Binary_expression(left, right)
600 value(const Expression_eval_info* eei)
602 Output_section* left_section;
603 uint64_t left_alignment;
604 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
605 Output_section* right_section;
606 uint64_t right_alignment;
607 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
608 if (left_section == right_section)
610 if (eei->result_section_pointer != NULL)
611 *eei->result_section_pointer = left_section;
613 else if ((left_section != NULL || right_section != NULL)
614 && parameters->options().relocatable())
615 gold_warning(_("max applied to section relative value"));
616 if (eei->result_alignment_pointer != NULL)
618 uint64_t ra = *eei->result_alignment_pointer;
620 ra = std::max(ra, left_alignment);
621 else if (right > left)
622 ra = std::max(ra, right_alignment);
624 ra = std::max(ra, std::max(left_alignment, right_alignment));
625 *eei->result_alignment_pointer = ra;
627 return std::max(left, right);
632 { this->print_function(f, "MAX"); }
635 extern "C" Expression*
636 script_exp_function_max(Expression* left, Expression* right)
638 return new Max_expression(left, right);
643 class Min_expression : public Binary_expression
646 Min_expression(Expression* left, Expression* right)
647 : Binary_expression(left, right)
651 value(const Expression_eval_info* eei)
653 Output_section* left_section;
654 uint64_t left_alignment;
655 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
656 Output_section* right_section;
657 uint64_t right_alignment;
658 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
659 if (left_section == right_section)
661 if (eei->result_section_pointer != NULL)
662 *eei->result_section_pointer = left_section;
664 else if ((left_section != NULL || right_section != NULL)
665 && parameters->options().relocatable())
666 gold_warning(_("min applied to section relative value"));
667 if (eei->result_alignment_pointer != NULL)
669 uint64_t ra = *eei->result_alignment_pointer;
671 ra = std::max(ra, left_alignment);
672 else if (right < left)
673 ra = std::max(ra, right_alignment);
675 ra = std::max(ra, std::max(left_alignment, right_alignment));
676 *eei->result_alignment_pointer = ra;
678 return std::min(left, right);
683 { this->print_function(f, "MIN"); }
686 extern "C" Expression*
687 script_exp_function_min(Expression* left, Expression* right)
689 return new Min_expression(left, right);
692 // Class Section_expression. This is a parent class used for
693 // functions which take the name of an output section.
695 class Section_expression : public Expression
698 Section_expression(const char* section_name, size_t section_name_len)
699 : section_name_(section_name, section_name_len)
703 value(const Expression_eval_info*);
707 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
710 // The child class must implement this.
712 value_from_output_section(const Expression_eval_info*,
713 Output_section*) = 0;
715 // The child class must implement this.
717 value_from_script_output_section(uint64_t address, uint64_t load_address,
718 uint64_t addralign, uint64_t size) = 0;
720 // The child class must implement this.
722 function_name() const = 0;
725 std::string section_name_;
729 Section_expression::value(const Expression_eval_info* eei)
731 const char* section_name = this->section_name_.c_str();
732 Output_section* os = eei->layout->find_output_section(section_name);
734 return this->value_from_output_section(eei, os);
737 uint64_t load_address;
740 const Script_options* ss = eei->layout->script_options();
741 if (ss->saw_sections_clause())
743 if (ss->script_sections()->get_output_section_info(section_name,
748 return this->value_from_script_output_section(address, load_address,
752 gold_error("%s called on nonexistent output section '%s'",
753 this->function_name(), section_name);
757 // ABSOLUTE function.
759 class Absolute_expression : public Unary_expression
762 Absolute_expression(Expression* arg)
763 : Unary_expression(arg)
767 value(const Expression_eval_info* eei)
769 uint64_t ret = this->arg_value(eei, NULL);
770 // Force the value to be absolute.
771 if (eei->result_section_pointer != NULL)
772 *eei->result_section_pointer = NULL;
779 fprintf(f, "ABSOLUTE(");
785 extern "C" Expression*
786 script_exp_function_absolute(Expression* arg)
788 return new Absolute_expression(arg);
793 class Align_expression : public Binary_expression
796 Align_expression(Expression* left, Expression* right)
797 : Binary_expression(left, right)
801 value(const Expression_eval_info* eei)
803 Output_section* align_section;
804 uint64_t align = this->right_value(eei, &align_section, NULL);
805 if (align_section != NULL
806 && parameters->options().relocatable())
807 gold_warning(_("aligning to section relative value"));
809 if (eei->result_alignment_pointer != NULL
810 && align > *eei->result_alignment_pointer)
813 while ((a & (a - 1)) != 0)
815 *eei->result_alignment_pointer = a;
818 uint64_t value = this->left_value(eei, eei->result_section_pointer, NULL);
821 return ((value + align - 1) / align) * align;
826 { this->print_function(f, "ALIGN"); }
829 extern "C" Expression*
830 script_exp_function_align(Expression* left, Expression* right)
832 return new Align_expression(left, right);
837 class Assert_expression : public Unary_expression
840 Assert_expression(Expression* arg, const char* message, size_t length)
841 : Unary_expression(arg), message_(message, length)
845 value(const Expression_eval_info* eei)
847 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
848 if (!value && eei->check_assertions)
849 gold_error("%s", this->message_.c_str());
856 fprintf(f, "ASSERT(");
858 fprintf(f, ", %s)", this->message_.c_str());
862 std::string message_;
865 extern "C" Expression*
866 script_exp_function_assert(Expression* expr, const char* message,
869 return new Assert_expression(expr, message, length);
874 class Addr_expression : public Section_expression
877 Addr_expression(const char* section_name, size_t section_name_len)
878 : Section_expression(section_name, section_name_len)
883 value_from_output_section(const Expression_eval_info* eei,
886 if (eei->result_section_pointer != NULL)
887 *eei->result_section_pointer = os;
888 return os->address();
892 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
897 function_name() const
901 extern "C" Expression*
902 script_exp_function_addr(const char* section_name, size_t section_name_len)
904 return new Addr_expression(section_name, section_name_len);
909 class Alignof_expression : public Section_expression
912 Alignof_expression(const char* section_name, size_t section_name_len)
913 : Section_expression(section_name, section_name_len)
918 value_from_output_section(const Expression_eval_info*,
920 { return os->addralign(); }
923 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
925 { return addralign; }
928 function_name() const
929 { return "ALIGNOF"; }
932 extern "C" Expression*
933 script_exp_function_alignof(const char* section_name, size_t section_name_len)
935 return new Alignof_expression(section_name, section_name_len);
938 // CONSTANT. It would be nice if we could simply evaluate this
939 // immediately and return an Integer_expression, but unfortunately we
940 // don't know the target.
942 class Constant_expression : public Expression
945 Constant_expression(const char* name, size_t length);
948 value(const Expression_eval_info*);
951 print(FILE* f) const;
954 enum Constant_function
956 CONSTANT_MAXPAGESIZE,
957 CONSTANT_COMMONPAGESIZE
960 Constant_function function_;
963 Constant_expression::Constant_expression(const char* name, size_t length)
965 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
966 this->function_ = CONSTANT_MAXPAGESIZE;
967 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
968 this->function_ = CONSTANT_COMMONPAGESIZE;
971 std::string s(name, length);
972 gold_error(_("unknown constant %s"), s.c_str());
973 this->function_ = CONSTANT_MAXPAGESIZE;
978 Constant_expression::value(const Expression_eval_info*)
980 switch (this->function_)
982 case CONSTANT_MAXPAGESIZE:
983 return parameters->target().abi_pagesize();
984 case CONSTANT_COMMONPAGESIZE:
985 return parameters->target().common_pagesize();
992 Constant_expression::print(FILE* f) const
995 switch (this->function_)
997 case CONSTANT_MAXPAGESIZE:
998 name = "MAXPAGESIZE";
1000 case CONSTANT_COMMONPAGESIZE:
1001 name = "COMMONPAGESIZE";
1006 fprintf(f, "CONSTANT(%s)", name);
1009 extern "C" Expression*
1010 script_exp_function_constant(const char* name, size_t length)
1012 return new Constant_expression(name, length);
1015 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1016 // back to the general case.
1018 extern "C" Expression*
1019 script_exp_function_data_segment_align(Expression* left, Expression*)
1021 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
1022 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
1023 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
1025 return script_exp_binary_add(e1, e3);
1028 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1030 extern "C" Expression*
1031 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
1036 // DATA_SEGMENT_END. FIXME: This is not implemented.
1038 extern "C" Expression*
1039 script_exp_function_data_segment_end(Expression* val)
1044 // DEFINED function.
1046 class Defined_expression : public Expression
1049 Defined_expression(const char* symbol_name, size_t symbol_name_len)
1050 : symbol_name_(symbol_name, symbol_name_len)
1054 value(const Expression_eval_info* eei)
1056 Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
1057 return sym != NULL && sym->is_defined();
1061 print(FILE* f) const
1062 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
1065 std::string symbol_name_;
1068 extern "C" Expression*
1069 script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
1071 return new Defined_expression(symbol_name, symbol_name_len);
1074 // LOADADDR function
1076 class Loadaddr_expression : public Section_expression
1079 Loadaddr_expression(const char* section_name, size_t section_name_len)
1080 : Section_expression(section_name, section_name_len)
1085 value_from_output_section(const Expression_eval_info* eei,
1088 if (os->has_load_address())
1089 return os->load_address();
1092 if (eei->result_section_pointer != NULL)
1093 *eei->result_section_pointer = os;
1094 return os->address();
1099 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1101 { return load_address; }
1104 function_name() const
1105 { return "LOADADDR"; }
1108 extern "C" Expression*
1109 script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
1111 return new Loadaddr_expression(section_name, section_name_len);
1116 class Sizeof_expression : public Section_expression
1119 Sizeof_expression(const char* section_name, size_t section_name_len)
1120 : Section_expression(section_name, section_name_len)
1125 value_from_output_section(const Expression_eval_info*,
1128 // We can not use data_size here, as the size of the section may
1129 // not have been finalized. Instead we get whatever the current
1130 // size is. This will work correctly for backward references in
1132 return os->current_data_size();
1136 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1141 function_name() const
1142 { return "SIZEOF"; }
1145 extern "C" Expression*
1146 script_exp_function_sizeof(const char* section_name, size_t section_name_len)
1148 return new Sizeof_expression(section_name, section_name_len);
1153 class Sizeof_headers_expression : public Expression
1156 Sizeof_headers_expression()
1160 value(const Expression_eval_info*);
1163 print(FILE* f) const
1164 { fprintf(f, "SIZEOF_HEADERS"); }
1168 Sizeof_headers_expression::value(const Expression_eval_info* eei)
1170 unsigned int ehdr_size;
1171 unsigned int phdr_size;
1172 if (parameters->target().get_size() == 32)
1174 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1175 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1177 else if (parameters->target().get_size() == 64)
1179 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
1180 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
1185 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
1188 extern "C" Expression*
1189 script_exp_function_sizeof_headers()
1191 return new Sizeof_headers_expression();
1196 class Segment_start_expression : public Unary_expression
1199 Segment_start_expression(const char* segment_name, size_t segment_name_len,
1200 Expression* default_value)
1201 : Unary_expression(default_value),
1202 segment_name_(segment_name, segment_name_len)
1206 value(const Expression_eval_info*);
1209 print(FILE* f) const
1211 fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
1217 std::string segment_name_;
1221 Segment_start_expression::value(const Expression_eval_info* eei)
1223 // Check for command line overrides.
1224 if (parameters->options().user_set_Ttext()
1225 && this->segment_name_ == ".text")
1226 return parameters->options().Ttext();
1227 else if (parameters->options().user_set_Tdata()
1228 && this->segment_name_ == ".data")
1229 return parameters->options().Tdata();
1230 else if (parameters->options().user_set_Tbss()
1231 && this->segment_name_ == ".bss")
1232 return parameters->options().Tbss();
1235 uint64_t ret = this->arg_value(eei, NULL);
1236 // Force the value to be absolute.
1237 if (eei->result_section_pointer != NULL)
1238 *eei->result_section_pointer = NULL;
1243 extern "C" Expression*
1244 script_exp_function_segment_start(const char* segment_name,
1245 size_t segment_name_len,
1246 Expression* default_value)
1248 return new Segment_start_expression(segment_name, segment_name_len,
1252 } // End namespace gold.