1 // expression.cc -- expressions in linker scripts for gold
3 // Copyright (C) 2006-2015 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
;
71 // Pointer to where the type of the symbol on the RHS should be stored.
72 elfcpp::STT
* type_pointer
;
73 // Pointer to where the visibility of the symbol on the RHS should be stored.
74 elfcpp::STV
* vis_pointer
;
75 // Pointer to where the rest of the symbol's st_other field should be stored.
76 unsigned char* nonvis_pointer
;
77 // Whether the value is valid. In Symbol_assignment::set_if_absolute, we
78 // may be trying to evaluate the address of a section whose address is not
79 // yet finalized, and we need to fail the evaluation gracefully.
80 bool *is_valid_pointer
;
83 // Evaluate an expression.
86 Expression::eval(const Symbol_table
* symtab
, const Layout
* layout
,
87 bool check_assertions
)
89 return this->eval_maybe_dot(symtab
, layout
, check_assertions
, false, 0,
90 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, false, NULL
);
93 // Evaluate an expression which may refer to the dot symbol.
96 Expression::eval_with_dot(const Symbol_table
* symtab
, const Layout
* layout
,
97 bool check_assertions
, uint64_t dot_value
,
98 Output_section
* dot_section
,
99 Output_section
** result_section_pointer
,
100 uint64_t* result_alignment_pointer
,
101 bool is_section_dot_assignment
)
103 return this->eval_maybe_dot(symtab
, layout
, check_assertions
, true,
104 dot_value
, dot_section
, result_section_pointer
,
105 result_alignment_pointer
, NULL
, NULL
, NULL
,
106 is_section_dot_assignment
, NULL
);
109 // Evaluate an expression which may or may not refer to the dot
113 Expression::eval_maybe_dot(const Symbol_table
* symtab
, const Layout
* layout
,
114 bool check_assertions
, bool is_dot_available
,
115 uint64_t dot_value
, Output_section
* dot_section
,
116 Output_section
** result_section_pointer
,
117 uint64_t* result_alignment_pointer
,
118 elfcpp::STT
* type_pointer
,
119 elfcpp::STV
* vis_pointer
,
120 unsigned char* nonvis_pointer
,
121 bool is_section_dot_assignment
,
122 bool* is_valid_pointer
)
124 Expression_eval_info eei
;
127 eei
.check_assertions
= check_assertions
;
128 eei
.is_dot_available
= is_dot_available
;
129 eei
.dot_value
= dot_value
;
130 eei
.dot_section
= dot_section
;
132 // We assume the value is absolute, and only set this to a section
133 // if we find a section-relative reference.
134 if (result_section_pointer
!= NULL
)
135 *result_section_pointer
= NULL
;
136 eei
.result_section_pointer
= result_section_pointer
;
138 // For symbol=symbol assignments, we need to track the type, visibility,
139 // and remaining st_other bits.
140 eei
.type_pointer
= type_pointer
;
141 eei
.vis_pointer
= vis_pointer
;
142 eei
.nonvis_pointer
= nonvis_pointer
;
144 eei
.result_alignment_pointer
= result_alignment_pointer
;
146 // Assume the value is valid until we try to evaluate an expression
147 // that can't be evaluated yet.
148 bool is_valid
= true;
149 eei
.is_valid_pointer
= &is_valid
;
151 uint64_t val
= this->value(&eei
);
153 if (is_valid_pointer
!= NULL
)
154 *is_valid_pointer
= is_valid
;
156 gold_assert(is_valid
);
158 // If this is an assignment to dot within a section, and the value
159 // is absolute, treat it as a section-relative offset.
160 if (is_section_dot_assignment
&& *result_section_pointer
== NULL
)
162 gold_assert(dot_section
!= NULL
);
163 val
+= dot_section
->address();
164 *result_section_pointer
= dot_section
;
171 class Integer_expression
: public Expression
174 Integer_expression(uint64_t val
)
179 value(const Expression_eval_info
*)
180 { return this->val_
; }
184 { fprintf(f
, "0x%llx", static_cast<unsigned long long>(this->val_
)); }
190 extern "C" Expression
*
191 script_exp_integer(uint64_t val
)
193 return new Integer_expression(val
);
196 // An expression whose value is the value of a symbol.
198 class Symbol_expression
: public Expression
201 Symbol_expression(const char* name
, size_t length
)
202 : name_(name
, length
)
206 value(const Expression_eval_info
*);
210 { fprintf(f
, "%s", this->name_
.c_str()); }
217 Symbol_expression::value(const Expression_eval_info
* eei
)
219 Symbol
* sym
= eei
->symtab
->lookup(this->name_
.c_str());
220 if (sym
== NULL
|| !sym
->is_defined())
222 gold_error(_("undefined symbol '%s' referenced in expression"),
223 this->name_
.c_str());
227 if (eei
->result_section_pointer
!= NULL
)
228 *eei
->result_section_pointer
= sym
->output_section();
229 if (eei
->type_pointer
!= NULL
)
230 *eei
->type_pointer
= sym
->type();
231 if (eei
->vis_pointer
!= NULL
)
232 *eei
->vis_pointer
= sym
->visibility();
233 if (eei
->nonvis_pointer
!= NULL
)
234 *eei
->nonvis_pointer
= sym
->nonvis();
236 if (parameters
->target().get_size() == 32)
237 return eei
->symtab
->get_sized_symbol
<32>(sym
)->value();
238 else if (parameters
->target().get_size() == 64)
239 return eei
->symtab
->get_sized_symbol
<64>(sym
)->value();
244 // An expression whose value is the value of the special symbol ".".
245 // This is only valid within a SECTIONS clause.
247 class Dot_expression
: public Expression
254 value(const Expression_eval_info
*);
262 Dot_expression::value(const Expression_eval_info
* eei
)
264 if (!eei
->is_dot_available
)
266 gold_error(_("invalid reference to dot symbol outside of "
270 if (eei
->result_section_pointer
!= NULL
)
271 *eei
->result_section_pointer
= eei
->dot_section
;
272 return eei
->dot_value
;
275 // A string. This is either the name of a symbol, or ".".
277 extern "C" Expression
*
278 script_exp_string(const char* name
, size_t length
)
280 if (length
== 1 && name
[0] == '.')
281 return new Dot_expression();
283 return new Symbol_expression(name
, length
);
286 // A unary expression.
288 class Unary_expression
: public Expression
291 Unary_expression(Expression
* arg
)
296 { delete this->arg_
; }
300 arg_value(const Expression_eval_info
* eei
,
301 Output_section
** arg_section_pointer
) const
303 return this->arg_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
304 eei
->check_assertions
,
305 eei
->is_dot_available
,
309 eei
->result_alignment_pointer
,
314 eei
->is_valid_pointer
);
318 arg_print(FILE* f
) const
319 { this->arg_
->print(f
); }
325 // Handle unary operators. We use a preprocessor macro as a hack to
326 // capture the C operator.
328 #define UNARY_EXPRESSION(NAME, OPERATOR) \
329 class Unary_ ## NAME : public Unary_expression \
332 Unary_ ## NAME(Expression* arg) \
333 : Unary_expression(arg) \
337 value(const Expression_eval_info* eei) \
339 Output_section* arg_section; \
340 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
341 if (arg_section != NULL && parameters->options().relocatable()) \
342 gold_warning(_("unary " #NAME " applied to section " \
343 "relative value")); \
348 print(FILE* f) const \
350 fprintf(f, "(%s ", #OPERATOR); \
351 this->arg_print(f); \
356 extern "C" Expression* \
357 script_exp_unary_ ## NAME(Expression* arg) \
359 return new Unary_ ## NAME(arg); \
362 UNARY_EXPRESSION(minus
, -)
363 UNARY_EXPRESSION(logical_not
, !)
364 UNARY_EXPRESSION(bitwise_not
, ~)
366 // A binary expression.
368 class Binary_expression
: public Expression
371 Binary_expression(Expression
* left
, Expression
* right
)
372 : left_(left
), right_(right
)
383 left_value(const Expression_eval_info
* eei
,
384 Output_section
** section_pointer
,
385 uint64_t* alignment_pointer
) const
387 return this->left_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
388 eei
->check_assertions
,
389 eei
->is_dot_available
,
398 eei
->is_valid_pointer
);
402 right_value(const Expression_eval_info
* eei
,
403 Output_section
** section_pointer
,
404 uint64_t* alignment_pointer
) const
406 return this->right_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
407 eei
->check_assertions
,
408 eei
->is_dot_available
,
417 eei
->is_valid_pointer
);
421 left_print(FILE* f
) const
422 { this->left_
->print(f
); }
425 right_print(FILE* f
) const
426 { this->right_
->print(f
); }
428 // This is a call to function FUNCTION_NAME. Print it. This is for
431 print_function(FILE* f
, const char* function_name
) const
433 fprintf(f
, "%s(", function_name
);
436 this->right_print(f
);
445 // Handle binary operators. We use a preprocessor macro as a hack to
446 // capture the C operator. KEEP_LEFT means that if the left operand
447 // is section relative and the right operand is not, the result uses
448 // the same section as the left operand. KEEP_RIGHT is the same with
449 // left and right swapped. IS_DIV means that we need to give an error
450 // if the right operand is zero. WARN means that we should warn if
451 // used on section relative values in a relocatable link. We always
452 // warn if used on values in different sections in a relocatable link.
454 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
455 class Binary_ ## NAME : public Binary_expression \
458 Binary_ ## NAME(Expression* left, Expression* right) \
459 : Binary_expression(left, right) \
463 value(const Expression_eval_info* eei) \
465 Output_section* left_section; \
466 uint64_t left_alignment = 0; \
467 uint64_t left = this->left_value(eei, &left_section, \
469 Output_section* right_section; \
470 uint64_t right_alignment = 0; \
471 uint64_t right = this->right_value(eei, &right_section, \
473 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
475 if (eei->result_section_pointer != NULL) \
476 *eei->result_section_pointer = right_section; \
477 if (eei->result_alignment_pointer != NULL \
478 && right_alignment > *eei->result_alignment_pointer) \
479 *eei->result_alignment_pointer = right_alignment; \
482 && left_section != NULL \
483 && right_section == NULL) \
485 if (eei->result_section_pointer != NULL) \
486 *eei->result_section_pointer = left_section; \
487 if (eei->result_alignment_pointer != NULL \
488 && left_alignment > *eei->result_alignment_pointer) \
489 *eei->result_alignment_pointer = left_alignment; \
491 else if ((WARN || left_section != right_section) \
492 && (left_section != NULL || right_section != NULL) \
493 && parameters->options().relocatable()) \
494 gold_warning(_("binary " #NAME " applied to section " \
495 "relative value")); \
496 if (IS_DIV && right == 0) \
498 gold_error(_(#NAME " by zero")); \
501 return left OPERATOR right; \
505 print(FILE* f) const \
508 this->left_print(f); \
509 fprintf(f, " %s ", #OPERATOR); \
510 this->right_print(f); \
515 extern "C" Expression* \
516 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
518 return new Binary_ ## NAME(left, right); \
521 BINARY_EXPRESSION(mult
, *, false, false, false, true)
522 BINARY_EXPRESSION(div
, /, false, false, true, true)
523 BINARY_EXPRESSION(mod
, %, false, false, true, true)
524 BINARY_EXPRESSION(add
, +, true, true, false, true)
525 BINARY_EXPRESSION(sub
, -, true, false, false, false)
526 BINARY_EXPRESSION(lshift
, <<, false, false, false, true)
527 BINARY_EXPRESSION(rshift
, >>, false, false, false, true)
528 BINARY_EXPRESSION(eq
, ==, false, false, false, false)
529 BINARY_EXPRESSION(ne
, !=, false, false, false, false)
530 BINARY_EXPRESSION(le
, <=, false, false, false, false)
531 BINARY_EXPRESSION(ge
, >=, false, false, false, false)
532 BINARY_EXPRESSION(lt
, <, false, false, false, false)
533 BINARY_EXPRESSION(gt
, >, false, false, false, false)
534 BINARY_EXPRESSION(bitwise_and
, &, true, true, false, true)
535 BINARY_EXPRESSION(bitwise_xor
, ^, true, true, false, true)
536 BINARY_EXPRESSION(bitwise_or
, |, true, true, false, true)
537 BINARY_EXPRESSION(logical_and
, &&, false, false, false, true)
538 BINARY_EXPRESSION(logical_or
, ||, false, false, false, true)
540 // A trinary expression.
542 class Trinary_expression
: public Expression
545 Trinary_expression(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
546 : arg1_(arg1
), arg2_(arg2
), arg3_(arg3
)
549 ~Trinary_expression()
558 arg1_value(const Expression_eval_info
* eei
,
559 Output_section
** section_pointer
) const
561 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
562 eei
->check_assertions
,
563 eei
->is_dot_available
,
572 eei
->is_valid_pointer
);
576 arg2_value(const Expression_eval_info
* eei
,
577 Output_section
** section_pointer
,
578 uint64_t* alignment_pointer
) const
580 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
581 eei
->check_assertions
,
582 eei
->is_dot_available
,
591 eei
->is_valid_pointer
);
595 arg3_value(const Expression_eval_info
* eei
,
596 Output_section
** section_pointer
,
597 uint64_t* alignment_pointer
) const
599 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
600 eei
->check_assertions
,
601 eei
->is_dot_available
,
610 eei
->is_valid_pointer
);
614 arg1_print(FILE* f
) const
615 { this->arg1_
->print(f
); }
618 arg2_print(FILE* f
) const
619 { this->arg2_
->print(f
); }
622 arg3_print(FILE* f
) const
623 { this->arg3_
->print(f
); }
631 // The conditional operator.
633 class Trinary_cond
: public Trinary_expression
636 Trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
637 : Trinary_expression(arg1
, arg2
, arg3
)
641 value(const Expression_eval_info
* eei
)
643 Output_section
* arg1_section
;
644 uint64_t arg1
= this->arg1_value(eei
, &arg1_section
);
646 ? this->arg2_value(eei
, eei
->result_section_pointer
,
647 eei
->result_alignment_pointer
)
648 : this->arg3_value(eei
, eei
->result_section_pointer
,
649 eei
->result_alignment_pointer
));
665 extern "C" Expression
*
666 script_exp_trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
668 return new Trinary_cond(arg1
, arg2
, arg3
);
673 class Max_expression
: public Binary_expression
676 Max_expression(Expression
* left
, Expression
* right
)
677 : Binary_expression(left
, right
)
681 value(const Expression_eval_info
* eei
)
683 Output_section
* left_section
;
684 uint64_t left_alignment
;
685 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
686 Output_section
* right_section
;
687 uint64_t right_alignment
;
688 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
689 if (left_section
== right_section
)
691 if (eei
->result_section_pointer
!= NULL
)
692 *eei
->result_section_pointer
= left_section
;
694 else if ((left_section
!= NULL
|| right_section
!= NULL
)
695 && parameters
->options().relocatable())
696 gold_warning(_("max applied to section relative value"));
697 if (eei
->result_alignment_pointer
!= NULL
)
699 uint64_t ra
= *eei
->result_alignment_pointer
;
701 ra
= std::max(ra
, left_alignment
);
702 else if (right
> left
)
703 ra
= std::max(ra
, right_alignment
);
705 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
706 *eei
->result_alignment_pointer
= ra
;
708 return std::max(left
, right
);
713 { this->print_function(f
, "MAX"); }
716 extern "C" Expression
*
717 script_exp_function_max(Expression
* left
, Expression
* right
)
719 return new Max_expression(left
, right
);
724 class Min_expression
: public Binary_expression
727 Min_expression(Expression
* left
, Expression
* right
)
728 : Binary_expression(left
, right
)
732 value(const Expression_eval_info
* eei
)
734 Output_section
* left_section
;
735 uint64_t left_alignment
;
736 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
737 Output_section
* right_section
;
738 uint64_t right_alignment
;
739 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
740 if (left_section
== right_section
)
742 if (eei
->result_section_pointer
!= NULL
)
743 *eei
->result_section_pointer
= left_section
;
745 else if ((left_section
!= NULL
|| right_section
!= NULL
)
746 && parameters
->options().relocatable())
747 gold_warning(_("min applied to section relative value"));
748 if (eei
->result_alignment_pointer
!= NULL
)
750 uint64_t ra
= *eei
->result_alignment_pointer
;
752 ra
= std::max(ra
, left_alignment
);
753 else if (right
< left
)
754 ra
= std::max(ra
, right_alignment
);
756 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
757 *eei
->result_alignment_pointer
= ra
;
759 return std::min(left
, right
);
764 { this->print_function(f
, "MIN"); }
767 extern "C" Expression
*
768 script_exp_function_min(Expression
* left
, Expression
* right
)
770 return new Min_expression(left
, right
);
773 // Class Section_expression. This is a parent class used for
774 // functions which take the name of an output section.
776 class Section_expression
: public Expression
779 Section_expression(const char* section_name
, size_t section_name_len
)
780 : section_name_(section_name
, section_name_len
)
784 value(const Expression_eval_info
*);
788 { fprintf(f
, "%s(%s)", this->function_name(), this->section_name_
.c_str()); }
791 // The child class must implement this.
793 value_from_output_section(const Expression_eval_info
*,
794 Output_section
*) = 0;
796 // The child class must implement this.
798 value_from_script_output_section(uint64_t address
, uint64_t load_address
,
799 uint64_t addralign
, uint64_t size
) = 0;
801 // The child class must implement this.
803 function_name() const = 0;
806 std::string section_name_
;
810 Section_expression::value(const Expression_eval_info
* eei
)
812 const char* section_name
= this->section_name_
.c_str();
813 Output_section
* os
= eei
->layout
->find_output_section(section_name
);
815 return this->value_from_output_section(eei
, os
);
818 uint64_t load_address
;
821 const Script_options
* ss
= eei
->layout
->script_options();
822 if (ss
->saw_sections_clause())
824 if (ss
->script_sections()->get_output_section_info(section_name
,
829 return this->value_from_script_output_section(address
, load_address
,
833 gold_error("%s called on nonexistent output section '%s'",
834 this->function_name(), section_name
);
838 // ABSOLUTE function.
840 class Absolute_expression
: public Unary_expression
843 Absolute_expression(Expression
* arg
)
844 : Unary_expression(arg
)
848 value(const Expression_eval_info
* eei
)
850 uint64_t ret
= this->arg_value(eei
, NULL
);
851 // Force the value to be absolute.
852 if (eei
->result_section_pointer
!= NULL
)
853 *eei
->result_section_pointer
= NULL
;
860 fprintf(f
, "ABSOLUTE(");
866 extern "C" Expression
*
867 script_exp_function_absolute(Expression
* arg
)
869 return new Absolute_expression(arg
);
874 class Align_expression
: public Binary_expression
877 Align_expression(Expression
* left
, Expression
* right
)
878 : Binary_expression(left
, right
)
882 value(const Expression_eval_info
* eei
)
884 Output_section
* align_section
;
885 uint64_t align
= this->right_value(eei
, &align_section
, NULL
);
886 if (align_section
!= NULL
887 && parameters
->options().relocatable())
888 gold_warning(_("aligning to section relative value"));
890 if (eei
->result_alignment_pointer
!= NULL
891 && align
> *eei
->result_alignment_pointer
)
894 while ((a
& (a
- 1)) != 0)
896 *eei
->result_alignment_pointer
= a
;
899 uint64_t value
= this->left_value(eei
, eei
->result_section_pointer
, NULL
);
902 return ((value
+ align
- 1) / align
) * align
;
907 { this->print_function(f
, "ALIGN"); }
910 extern "C" Expression
*
911 script_exp_function_align(Expression
* left
, Expression
* right
)
913 return new Align_expression(left
, right
);
918 class Assert_expression
: public Unary_expression
921 Assert_expression(Expression
* arg
, const char* message
, size_t length
)
922 : Unary_expression(arg
), message_(message
, length
)
926 value(const Expression_eval_info
* eei
)
928 uint64_t value
= this->arg_value(eei
, eei
->result_section_pointer
);
929 if (!value
&& eei
->check_assertions
)
930 gold_error("%s", this->message_
.c_str());
937 fprintf(f
, "ASSERT(");
939 fprintf(f
, ", %s)", this->message_
.c_str());
943 std::string message_
;
946 extern "C" Expression
*
947 script_exp_function_assert(Expression
* expr
, const char* message
,
950 return new Assert_expression(expr
, message
, length
);
955 class Addr_expression
: public Section_expression
958 Addr_expression(const char* section_name
, size_t section_name_len
)
959 : Section_expression(section_name
, section_name_len
)
964 value_from_output_section(const Expression_eval_info
* eei
,
967 if (eei
->result_section_pointer
!= NULL
)
968 *eei
->result_section_pointer
= os
;
969 if (os
->is_address_valid())
970 return os
->address();
971 *eei
->is_valid_pointer
= false;
976 value_from_script_output_section(uint64_t address
, uint64_t, uint64_t,
981 function_name() const
985 extern "C" Expression
*
986 script_exp_function_addr(const char* section_name
, size_t section_name_len
)
988 return new Addr_expression(section_name
, section_name_len
);
993 class Alignof_expression
: public Section_expression
996 Alignof_expression(const char* section_name
, size_t section_name_len
)
997 : Section_expression(section_name
, section_name_len
)
1002 value_from_output_section(const Expression_eval_info
*,
1004 { return os
->addralign(); }
1007 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign
,
1009 { return addralign
; }
1012 function_name() const
1013 { return "ALIGNOF"; }
1016 extern "C" Expression
*
1017 script_exp_function_alignof(const char* section_name
, size_t section_name_len
)
1019 return new Alignof_expression(section_name
, section_name_len
);
1022 // CONSTANT. It would be nice if we could simply evaluate this
1023 // immediately and return an Integer_expression, but unfortunately we
1024 // don't know the target.
1026 class Constant_expression
: public Expression
1029 Constant_expression(const char* name
, size_t length
);
1032 value(const Expression_eval_info
*);
1035 print(FILE* f
) const;
1038 enum Constant_function
1040 CONSTANT_MAXPAGESIZE
,
1041 CONSTANT_COMMONPAGESIZE
1044 Constant_function function_
;
1047 Constant_expression::Constant_expression(const char* name
, size_t length
)
1049 if (length
== 11 && strncmp(name
, "MAXPAGESIZE", length
) == 0)
1050 this->function_
= CONSTANT_MAXPAGESIZE
;
1051 else if (length
== 14 && strncmp(name
, "COMMONPAGESIZE", length
) == 0)
1052 this->function_
= CONSTANT_COMMONPAGESIZE
;
1055 std::string
s(name
, length
);
1056 gold_error(_("unknown constant %s"), s
.c_str());
1057 this->function_
= CONSTANT_MAXPAGESIZE
;
1062 Constant_expression::value(const Expression_eval_info
*)
1064 switch (this->function_
)
1066 case CONSTANT_MAXPAGESIZE
:
1067 return parameters
->target().abi_pagesize();
1068 case CONSTANT_COMMONPAGESIZE
:
1069 return parameters
->target().common_pagesize();
1076 Constant_expression::print(FILE* f
) const
1079 switch (this->function_
)
1081 case CONSTANT_MAXPAGESIZE
:
1082 name
= "MAXPAGESIZE";
1084 case CONSTANT_COMMONPAGESIZE
:
1085 name
= "COMMONPAGESIZE";
1090 fprintf(f
, "CONSTANT(%s)", name
);
1093 extern "C" Expression
*
1094 script_exp_function_constant(const char* name
, size_t length
)
1096 return new Constant_expression(name
, length
);
1099 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1100 // back to the general case.
1102 extern "C" Expression
*
1103 script_exp_function_data_segment_align(Expression
* left
, Expression
*)
1105 Expression
* e1
= script_exp_function_align(script_exp_string(".", 1), left
);
1106 Expression
* e2
= script_exp_binary_sub(left
, script_exp_integer(1));
1107 Expression
* e3
= script_exp_binary_bitwise_and(script_exp_string(".", 1),
1109 return script_exp_binary_add(e1
, e3
);
1112 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1114 extern "C" Expression
*
1115 script_exp_function_data_segment_relro_end(Expression
*, Expression
* right
)
1120 // DATA_SEGMENT_END. FIXME: This is not implemented.
1122 extern "C" Expression
*
1123 script_exp_function_data_segment_end(Expression
* val
)
1128 // DEFINED function.
1130 class Defined_expression
: public Expression
1133 Defined_expression(const char* symbol_name
, size_t symbol_name_len
)
1134 : symbol_name_(symbol_name
, symbol_name_len
)
1138 value(const Expression_eval_info
* eei
)
1140 Symbol
* sym
= eei
->symtab
->lookup(this->symbol_name_
.c_str());
1141 return sym
!= NULL
&& sym
->is_defined();
1145 print(FILE* f
) const
1146 { fprintf(f
, "DEFINED(%s)", this->symbol_name_
.c_str()); }
1149 std::string symbol_name_
;
1152 extern "C" Expression
*
1153 script_exp_function_defined(const char* symbol_name
, size_t symbol_name_len
)
1155 return new Defined_expression(symbol_name
, symbol_name_len
);
1158 // LOADADDR function
1160 class Loadaddr_expression
: public Section_expression
1163 Loadaddr_expression(const char* section_name
, size_t section_name_len
)
1164 : Section_expression(section_name
, section_name_len
)
1169 value_from_output_section(const Expression_eval_info
* eei
,
1172 if (os
->has_load_address())
1173 return os
->load_address();
1176 if (eei
->result_section_pointer
!= NULL
)
1177 *eei
->result_section_pointer
= os
;
1178 return os
->address();
1183 value_from_script_output_section(uint64_t, uint64_t load_address
, uint64_t,
1185 { return load_address
; }
1188 function_name() const
1189 { return "LOADADDR"; }
1192 extern "C" Expression
*
1193 script_exp_function_loadaddr(const char* section_name
, size_t section_name_len
)
1195 return new Loadaddr_expression(section_name
, section_name_len
);
1200 class Sizeof_expression
: public Section_expression
1203 Sizeof_expression(const char* section_name
, size_t section_name_len
)
1204 : Section_expression(section_name
, section_name_len
)
1209 value_from_output_section(const Expression_eval_info
*,
1212 // We can not use data_size here, as the size of the section may
1213 // not have been finalized. Instead we get whatever the current
1214 // size is. This will work correctly for backward references in
1216 return os
->current_data_size();
1220 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1225 function_name() const
1226 { return "SIZEOF"; }
1229 extern "C" Expression
*
1230 script_exp_function_sizeof(const char* section_name
, size_t section_name_len
)
1232 return new Sizeof_expression(section_name
, section_name_len
);
1237 class Sizeof_headers_expression
: public Expression
1240 Sizeof_headers_expression()
1244 value(const Expression_eval_info
*);
1247 print(FILE* f
) const
1248 { fprintf(f
, "SIZEOF_HEADERS"); }
1252 Sizeof_headers_expression::value(const Expression_eval_info
* eei
)
1254 unsigned int ehdr_size
;
1255 unsigned int phdr_size
;
1256 if (parameters
->target().get_size() == 32)
1258 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
1259 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
1261 else if (parameters
->target().get_size() == 64)
1263 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
1264 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
1269 return ehdr_size
+ phdr_size
* eei
->layout
->expected_segment_count();
1272 extern "C" Expression
*
1273 script_exp_function_sizeof_headers()
1275 return new Sizeof_headers_expression();
1280 class Segment_start_expression
: public Unary_expression
1283 Segment_start_expression(const char* segment_name
, size_t segment_name_len
,
1284 Expression
* default_value
)
1285 : Unary_expression(default_value
),
1286 segment_name_(segment_name
, segment_name_len
)
1290 value(const Expression_eval_info
*);
1293 print(FILE* f
) const
1295 fprintf(f
, "SEGMENT_START(\"%s\", ", this->segment_name_
.c_str());
1301 std::string segment_name_
;
1305 Segment_start_expression::value(const Expression_eval_info
* eei
)
1307 // Check for command line overrides.
1308 if (parameters
->options().user_set_Ttext()
1309 && this->segment_name_
== ".text")
1310 return parameters
->options().Ttext();
1311 else if (parameters
->options().user_set_Tdata()
1312 && this->segment_name_
== ".data")
1313 return parameters
->options().Tdata();
1314 else if (parameters
->options().user_set_Tbss()
1315 && this->segment_name_
== ".bss")
1316 return parameters
->options().Tbss();
1319 uint64_t ret
= this->arg_value(eei
, NULL
);
1320 // Force the value to be absolute.
1321 if (eei
->result_section_pointer
!= NULL
)
1322 *eei
->result_section_pointer
= NULL
;
1327 extern "C" Expression
*
1328 script_exp_function_segment_start(const char* segment_name
,
1329 size_t segment_name_len
,
1330 Expression
* default_value
)
1332 return new Segment_start_expression(segment_name
, segment_name_len
,
1336 } // End namespace gold.