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 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.
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
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;
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);
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 *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();
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
204 value(const Expression_eval_info*);
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 "
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();
232 return new Symbol_expression(name, length);
235 // A unary expression.
237 class Unary_expression : public Expression
240 Unary_expression(Expression* arg)
245 { delete this->arg_; }
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,
258 eei->result_alignment_pointer);
262 arg_print(FILE* f) const
263 { this->arg_->print(f); }
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 \
276 Unary_ ## NAME(Expression* arg) \
277 : Unary_expression(arg) \
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")); \
292 print(FILE* f) const \
294 fprintf(f, "(%s ", #OPERATOR); \
295 this->arg_print(f); \
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
315 Binary_expression(Expression* left, Expression* right)
316 : left_(left), right_(right)
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,
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,
355 left_print(FILE* f) const
356 { this->left_->print(f); }
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
365 print_function(FILE* f, const char* function_name) const
367 fprintf(f, "%s(", function_name);
370 this->right_print(f);
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 \
392 Binary_ ## NAME(Expression* left, Expression* right) \
393 : Binary_expression(left, right) \
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, \
403 Output_section* right_section; \
404 uint64_t right_alignment; \
405 uint64_t right = this->right_value(eei, &right_section, \
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; \
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")); \
431 return left OPERATOR right; \
435 print(FILE* f) const \
438 this->left_print(f); \
439 fprintf(f, " %s ", #OPERATOR); \
440 this->right_print(f); \
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
475 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
476 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
479 ~Trinary_expression()
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,
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,
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,
529 arg1_print(FILE* f) const
530 { this->arg1_->print(f); }
533 arg2_print(FILE* f) const
534 { this->arg2_->print(f); }
537 arg3_print(FILE* f) const
538 { this->arg3_->print(f); }
546 // The conditional operator.
548 class Trinary_cond : public Trinary_expression
551 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
552 : Trinary_expression(arg1, arg2, arg3)
556 value(const Expression_eval_info* eei)
558 Output_section* arg1_section;
559 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
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));
580 extern "C" Expression*
581 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
583 return new Trinary_cond(arg1, arg2, arg3);
588 class Max_expression : public Binary_expression
591 Max_expression(Expression* left, Expression* right)
592 : Binary_expression(left, right)
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;
613 ra = std::max(ra, left_alignment);
614 else if (right > left)
615 ra = std::max(ra, right_alignment);
617 ra = std::max(ra, std::max(left_alignment, right_alignment));
618 *eei->result_alignment_pointer = ra;
620 return std::max(left, right);
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);
636 class Min_expression : public Binary_expression
639 Min_expression(Expression* left, Expression* right)
640 : Binary_expression(left, right)
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;
661 ra = std::max(ra, left_alignment);
662 else if (right < left)
663 ra = std::max(ra, right_alignment);
665 ra = std::max(ra, std::max(left_alignment, right_alignment));
666 *eei->result_alignment_pointer = ra;
668 return std::min(left, right);
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
688 Section_expression(const char* section_name, size_t section_name_len)
689 : section_name_(section_name, section_name_len)
693 value(const Expression_eval_info*);
697 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
700 // The child class must implement this.
702 value_from_output_section(const Expression_eval_info*,
703 Output_section*) = 0;
705 // The child class must implement this.
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.
712 function_name() const = 0;
715 std::string section_name_;
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);
724 return this->value_from_output_section(eei, os);
727 uint64_t load_address;
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,
738 return this->value_from_script_output_section(address, load_address,
742 gold_error("%s called on nonexistent output section '%s'",
743 this->function_name(), section_name);
747 // ABSOLUTE function.
749 class Absolute_expression : public Unary_expression
752 Absolute_expression(Expression* arg)
753 : Unary_expression(arg)
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;
769 fprintf(f, "ABSOLUTE(");
775 extern "C" Expression*
776 script_exp_function_absolute(Expression* arg)
778 return new Absolute_expression(arg);
783 class Align_expression : public Binary_expression
786 Align_expression(Expression* left, Expression* right)
787 : Binary_expression(left, right)
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)
803 while ((a & (a - 1)) != 0)
805 *eei->result_alignment_pointer = a;
808 uint64_t value = this->left_value(eei, eei->result_section_pointer, NULL);
811 return ((value + align - 1) / align) * align;
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);
827 class Assert_expression : public Unary_expression
830 Assert_expression(Expression* arg, const char* message, size_t length)
831 : Unary_expression(arg), message_(message, length)
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());
846 fprintf(f, "ASSERT(");
848 fprintf(f, ", %s)", this->message_.c_str());
852 std::string message_;
855 extern "C" Expression*
856 script_exp_function_assert(Expression* expr, const char* message,
859 return new Assert_expression(expr, message, length);
864 class Addr_expression : public Section_expression
867 Addr_expression(const char* section_name, size_t section_name_len)
868 : Section_expression(section_name, section_name_len)
873 value_from_output_section(const Expression_eval_info* eei,
876 *eei->result_section_pointer = os;
877 return os->address();
881 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
886 function_name() const
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);
898 class Alignof_expression : public Section_expression
901 Alignof_expression(const char* section_name, size_t section_name_len)
902 : Section_expression(section_name, section_name_len)
907 value_from_output_section(const Expression_eval_info*,
909 { return os->addralign(); }
912 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
914 { return addralign; }
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
934 Constant_expression(const char* name, size_t length);
937 value(const Expression_eval_info*);
940 print(FILE* f) const;
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;
960 std::string s(name, length);
961 gold_error(_("unknown constant %s"), s.c_str());
962 this->function_ = CONSTANT_MAXPAGESIZE;
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();
981 Constant_expression::print(FILE* f) const
984 switch (this->function_)
986 case CONSTANT_MAXPAGESIZE:
987 name = "MAXPAGESIZE";
989 case CONSTANT_COMMONPAGESIZE:
990 name = "COMMONPAGESIZE";
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),
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)
1025 // DATA_SEGMENT_END. FIXME: This is not implemented.
1027 extern "C" Expression*
1028 script_exp_function_data_segment_end(Expression* val)
1033 // DEFINED function.
1035 class Defined_expression : public Expression
1038 Defined_expression(const char* symbol_name, size_t symbol_name_len)
1039 : symbol_name_(symbol_name, symbol_name_len)
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();
1050 print(FILE* f) const
1051 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
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
1068 Loadaddr_expression(const char* section_name, size_t section_name_len)
1069 : Section_expression(section_name, section_name_len)
1074 value_from_output_section(const Expression_eval_info* eei,
1077 if (os->has_load_address())
1078 return os->load_address();
1081 *eei->result_section_pointer = os;
1082 return os->address();
1087 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1089 { return load_address; }
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);
1104 class Sizeof_expression : public Section_expression
1107 Sizeof_expression(const char* section_name, size_t section_name_len)
1108 : Section_expression(section_name, section_name_len)
1113 value_from_output_section(const Expression_eval_info*,
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
1120 return os->current_data_size();
1124 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
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);
1141 class Sizeof_headers_expression : public Expression
1144 Sizeof_headers_expression()
1148 value(const Expression_eval_info*);
1151 print(FILE* f) const
1152 { fprintf(f, "SIZEOF_HEADERS"); }
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;
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();
1184 class Segment_start_expression : public Unary_expression
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)
1194 value(const Expression_eval_info*);
1197 print(FILE* f) const
1199 fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
1205 std::string segment_name_;
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();
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;
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,
1240 } // End namespace gold.