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
;
79 // Evaluate an expression.
82 Expression::eval(const Symbol_table
* symtab
, const Layout
* layout
,
83 bool check_assertions
)
85 return this->eval_maybe_dot(symtab
, layout
, check_assertions
, false, 0,
86 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, false);
89 // Evaluate an expression which may refer to the dot symbol.
92 Expression::eval_with_dot(const Symbol_table
* symtab
, const Layout
* layout
,
93 bool check_assertions
, uint64_t dot_value
,
94 Output_section
* dot_section
,
95 Output_section
** result_section_pointer
,
96 uint64_t* result_alignment_pointer
,
97 bool is_section_dot_assignment
)
99 return this->eval_maybe_dot(symtab
, layout
, check_assertions
, true,
100 dot_value
, dot_section
, result_section_pointer
,
101 result_alignment_pointer
, NULL
, NULL
, NULL
,
102 is_section_dot_assignment
);
105 // Evaluate an expression which may or may not refer to the dot
109 Expression::eval_maybe_dot(const Symbol_table
* symtab
, const Layout
* layout
,
110 bool check_assertions
, bool is_dot_available
,
111 uint64_t dot_value
, Output_section
* dot_section
,
112 Output_section
** result_section_pointer
,
113 uint64_t* result_alignment_pointer
,
114 elfcpp::STT
* type_pointer
,
115 elfcpp::STV
* vis_pointer
,
116 unsigned char* nonvis_pointer
,
117 bool is_section_dot_assignment
)
119 Expression_eval_info eei
;
122 eei
.check_assertions
= check_assertions
;
123 eei
.is_dot_available
= is_dot_available
;
124 eei
.dot_value
= dot_value
;
125 eei
.dot_section
= dot_section
;
127 // We assume the value is absolute, and only set this to a section
128 // if we find a section-relative reference.
129 if (result_section_pointer
!= NULL
)
130 *result_section_pointer
= NULL
;
131 eei
.result_section_pointer
= result_section_pointer
;
133 // For symbol=symbol assignments, we need to track the type, visibility,
134 // and remaining st_other bits.
135 eei
.type_pointer
= type_pointer
;
136 eei
.vis_pointer
= vis_pointer
;
137 eei
.nonvis_pointer
= nonvis_pointer
;
139 eei
.result_alignment_pointer
= result_alignment_pointer
;
141 uint64_t val
= this->value(&eei
);
143 // If this is an assignment to dot within a section, and the value
144 // is absolute, treat it as a section-relative offset.
145 if (is_section_dot_assignment
&& *result_section_pointer
== NULL
)
147 gold_assert(dot_section
!= NULL
);
148 val
+= dot_section
->address();
149 *result_section_pointer
= dot_section
;
156 class Integer_expression
: public Expression
159 Integer_expression(uint64_t val
)
164 value(const Expression_eval_info
*)
165 { return this->val_
; }
169 { fprintf(f
, "0x%llx", static_cast<unsigned long long>(this->val_
)); }
175 extern "C" Expression
*
176 script_exp_integer(uint64_t val
)
178 return new Integer_expression(val
);
181 // An expression whose value is the value of a symbol.
183 class Symbol_expression
: public Expression
186 Symbol_expression(const char* name
, size_t length
)
187 : name_(name
, length
)
191 value(const Expression_eval_info
*);
195 { fprintf(f
, "%s", this->name_
.c_str()); }
202 Symbol_expression::value(const Expression_eval_info
* eei
)
204 Symbol
* sym
= eei
->symtab
->lookup(this->name_
.c_str());
205 if (sym
== NULL
|| !sym
->is_defined())
207 gold_error(_("undefined symbol '%s' referenced in expression"),
208 this->name_
.c_str());
212 if (eei
->result_section_pointer
!= NULL
)
213 *eei
->result_section_pointer
= sym
->output_section();
214 if (eei
->type_pointer
!= NULL
)
215 *eei
->type_pointer
= sym
->type();
216 if (eei
->vis_pointer
!= NULL
)
217 *eei
->vis_pointer
= sym
->visibility();
218 if (eei
->nonvis_pointer
!= NULL
)
219 *eei
->nonvis_pointer
= sym
->nonvis();
221 if (parameters
->target().get_size() == 32)
222 return eei
->symtab
->get_sized_symbol
<32>(sym
)->value();
223 else if (parameters
->target().get_size() == 64)
224 return eei
->symtab
->get_sized_symbol
<64>(sym
)->value();
229 // An expression whose value is the value of the special symbol ".".
230 // This is only valid within a SECTIONS clause.
232 class Dot_expression
: public Expression
239 value(const Expression_eval_info
*);
247 Dot_expression::value(const Expression_eval_info
* eei
)
249 if (!eei
->is_dot_available
)
251 gold_error(_("invalid reference to dot symbol outside of "
255 if (eei
->result_section_pointer
!= NULL
)
256 *eei
->result_section_pointer
= eei
->dot_section
;
257 return eei
->dot_value
;
260 // A string. This is either the name of a symbol, or ".".
262 extern "C" Expression
*
263 script_exp_string(const char* name
, size_t length
)
265 if (length
== 1 && name
[0] == '.')
266 return new Dot_expression();
268 return new Symbol_expression(name
, length
);
271 // A unary expression.
273 class Unary_expression
: public Expression
276 Unary_expression(Expression
* arg
)
281 { delete this->arg_
; }
285 arg_value(const Expression_eval_info
* eei
,
286 Output_section
** arg_section_pointer
) const
288 return this->arg_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
289 eei
->check_assertions
,
290 eei
->is_dot_available
,
294 eei
->result_alignment_pointer
,
302 arg_print(FILE* f
) const
303 { this->arg_
->print(f
); }
309 // Handle unary operators. We use a preprocessor macro as a hack to
310 // capture the C operator.
312 #define UNARY_EXPRESSION(NAME, OPERATOR) \
313 class Unary_ ## NAME : public Unary_expression \
316 Unary_ ## NAME(Expression* arg) \
317 : Unary_expression(arg) \
321 value(const Expression_eval_info* eei) \
323 Output_section* arg_section; \
324 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
325 if (arg_section != NULL && parameters->options().relocatable()) \
326 gold_warning(_("unary " #NAME " applied to section " \
327 "relative value")); \
332 print(FILE* f) const \
334 fprintf(f, "(%s ", #OPERATOR); \
335 this->arg_print(f); \
340 extern "C" Expression* \
341 script_exp_unary_ ## NAME(Expression* arg) \
343 return new Unary_ ## NAME(arg); \
346 UNARY_EXPRESSION(minus
, -)
347 UNARY_EXPRESSION(logical_not
, !)
348 UNARY_EXPRESSION(bitwise_not
, ~)
350 // A binary expression.
352 class Binary_expression
: public Expression
355 Binary_expression(Expression
* left
, Expression
* right
)
356 : left_(left
), right_(right
)
367 left_value(const Expression_eval_info
* eei
,
368 Output_section
** section_pointer
,
369 uint64_t* alignment_pointer
) const
371 return this->left_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
372 eei
->check_assertions
,
373 eei
->is_dot_available
,
385 right_value(const Expression_eval_info
* eei
,
386 Output_section
** section_pointer
,
387 uint64_t* alignment_pointer
) const
389 return this->right_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
390 eei
->check_assertions
,
391 eei
->is_dot_available
,
403 left_print(FILE* f
) const
404 { this->left_
->print(f
); }
407 right_print(FILE* f
) const
408 { this->right_
->print(f
); }
410 // This is a call to function FUNCTION_NAME. Print it. This is for
413 print_function(FILE* f
, const char* function_name
) const
415 fprintf(f
, "%s(", function_name
);
418 this->right_print(f
);
427 // Handle binary operators. We use a preprocessor macro as a hack to
428 // capture the C operator. KEEP_LEFT means that if the left operand
429 // is section relative and the right operand is not, the result uses
430 // the same section as the left operand. KEEP_RIGHT is the same with
431 // left and right swapped. IS_DIV means that we need to give an error
432 // if the right operand is zero. WARN means that we should warn if
433 // used on section relative values in a relocatable link. We always
434 // warn if used on values in different sections in a relocatable link.
436 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
437 class Binary_ ## NAME : public Binary_expression \
440 Binary_ ## NAME(Expression* left, Expression* right) \
441 : Binary_expression(left, right) \
445 value(const Expression_eval_info* eei) \
447 Output_section* left_section; \
448 uint64_t left_alignment = 0; \
449 uint64_t left = this->left_value(eei, &left_section, \
451 Output_section* right_section; \
452 uint64_t right_alignment = 0; \
453 uint64_t right = this->right_value(eei, &right_section, \
455 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
457 if (eei->result_section_pointer != NULL) \
458 *eei->result_section_pointer = right_section; \
459 if (eei->result_alignment_pointer != NULL \
460 && right_alignment > *eei->result_alignment_pointer) \
461 *eei->result_alignment_pointer = right_alignment; \
464 && left_section != NULL \
465 && right_section == NULL) \
467 if (eei->result_section_pointer != NULL) \
468 *eei->result_section_pointer = left_section; \
469 if (eei->result_alignment_pointer != NULL \
470 && left_alignment > *eei->result_alignment_pointer) \
471 *eei->result_alignment_pointer = left_alignment; \
473 else if ((WARN || left_section != right_section) \
474 && (left_section != NULL || right_section != NULL) \
475 && parameters->options().relocatable()) \
476 gold_warning(_("binary " #NAME " applied to section " \
477 "relative value")); \
478 if (IS_DIV && right == 0) \
480 gold_error(_(#NAME " by zero")); \
483 return left OPERATOR right; \
487 print(FILE* f) const \
490 this->left_print(f); \
491 fprintf(f, " %s ", #OPERATOR); \
492 this->right_print(f); \
497 extern "C" Expression* \
498 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
500 return new Binary_ ## NAME(left, right); \
503 BINARY_EXPRESSION(mult
, *, false, false, false, true)
504 BINARY_EXPRESSION(div
, /, false, false, true, true)
505 BINARY_EXPRESSION(mod
, %, false, false, true, true)
506 BINARY_EXPRESSION(add
, +, true, true, false, true)
507 BINARY_EXPRESSION(sub
, -, true, false, false, false)
508 BINARY_EXPRESSION(lshift
, <<, false, false, false, true)
509 BINARY_EXPRESSION(rshift
, >>, false, false, false, true)
510 BINARY_EXPRESSION(eq
, ==, false, false, false, false)
511 BINARY_EXPRESSION(ne
, !=, false, false, false, false)
512 BINARY_EXPRESSION(le
, <=, false, false, false, false)
513 BINARY_EXPRESSION(ge
, >=, false, false, false, false)
514 BINARY_EXPRESSION(lt
, <, false, false, false, false)
515 BINARY_EXPRESSION(gt
, >, false, false, false, false)
516 BINARY_EXPRESSION(bitwise_and
, &, true, true, false, true)
517 BINARY_EXPRESSION(bitwise_xor
, ^, true, true, false, true)
518 BINARY_EXPRESSION(bitwise_or
, |, true, true, false, true)
519 BINARY_EXPRESSION(logical_and
, &&, false, false, false, true)
520 BINARY_EXPRESSION(logical_or
, ||, false, false, false, true)
522 // A trinary expression.
524 class Trinary_expression
: public Expression
527 Trinary_expression(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
528 : arg1_(arg1
), arg2_(arg2
), arg3_(arg3
)
531 ~Trinary_expression()
540 arg1_value(const Expression_eval_info
* eei
,
541 Output_section
** section_pointer
) const
543 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
544 eei
->check_assertions
,
545 eei
->is_dot_available
,
557 arg2_value(const Expression_eval_info
* eei
,
558 Output_section
** section_pointer
,
559 uint64_t* alignment_pointer
) const
561 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
562 eei
->check_assertions
,
563 eei
->is_dot_available
,
575 arg3_value(const Expression_eval_info
* eei
,
576 Output_section
** section_pointer
,
577 uint64_t* alignment_pointer
) const
579 return this->arg1_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
580 eei
->check_assertions
,
581 eei
->is_dot_available
,
593 arg1_print(FILE* f
) const
594 { this->arg1_
->print(f
); }
597 arg2_print(FILE* f
) const
598 { this->arg2_
->print(f
); }
601 arg3_print(FILE* f
) const
602 { this->arg3_
->print(f
); }
610 // The conditional operator.
612 class Trinary_cond
: public Trinary_expression
615 Trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
616 : Trinary_expression(arg1
, arg2
, arg3
)
620 value(const Expression_eval_info
* eei
)
622 Output_section
* arg1_section
;
623 uint64_t arg1
= this->arg1_value(eei
, &arg1_section
);
625 ? this->arg2_value(eei
, eei
->result_section_pointer
,
626 eei
->result_alignment_pointer
)
627 : this->arg3_value(eei
, eei
->result_section_pointer
,
628 eei
->result_alignment_pointer
));
644 extern "C" Expression
*
645 script_exp_trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
647 return new Trinary_cond(arg1
, arg2
, arg3
);
652 class Max_expression
: public Binary_expression
655 Max_expression(Expression
* left
, Expression
* right
)
656 : Binary_expression(left
, right
)
660 value(const Expression_eval_info
* eei
)
662 Output_section
* left_section
;
663 uint64_t left_alignment
;
664 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
665 Output_section
* right_section
;
666 uint64_t right_alignment
;
667 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
668 if (left_section
== right_section
)
670 if (eei
->result_section_pointer
!= NULL
)
671 *eei
->result_section_pointer
= left_section
;
673 else if ((left_section
!= NULL
|| right_section
!= NULL
)
674 && parameters
->options().relocatable())
675 gold_warning(_("max applied to section relative value"));
676 if (eei
->result_alignment_pointer
!= NULL
)
678 uint64_t ra
= *eei
->result_alignment_pointer
;
680 ra
= std::max(ra
, left_alignment
);
681 else if (right
> left
)
682 ra
= std::max(ra
, right_alignment
);
684 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
685 *eei
->result_alignment_pointer
= ra
;
687 return std::max(left
, right
);
692 { this->print_function(f
, "MAX"); }
695 extern "C" Expression
*
696 script_exp_function_max(Expression
* left
, Expression
* right
)
698 return new Max_expression(left
, right
);
703 class Min_expression
: public Binary_expression
706 Min_expression(Expression
* left
, Expression
* right
)
707 : Binary_expression(left
, right
)
711 value(const Expression_eval_info
* eei
)
713 Output_section
* left_section
;
714 uint64_t left_alignment
;
715 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
716 Output_section
* right_section
;
717 uint64_t right_alignment
;
718 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
719 if (left_section
== right_section
)
721 if (eei
->result_section_pointer
!= NULL
)
722 *eei
->result_section_pointer
= left_section
;
724 else if ((left_section
!= NULL
|| right_section
!= NULL
)
725 && parameters
->options().relocatable())
726 gold_warning(_("min applied to section relative value"));
727 if (eei
->result_alignment_pointer
!= NULL
)
729 uint64_t ra
= *eei
->result_alignment_pointer
;
731 ra
= std::max(ra
, left_alignment
);
732 else if (right
< left
)
733 ra
= std::max(ra
, right_alignment
);
735 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
736 *eei
->result_alignment_pointer
= ra
;
738 return std::min(left
, right
);
743 { this->print_function(f
, "MIN"); }
746 extern "C" Expression
*
747 script_exp_function_min(Expression
* left
, Expression
* right
)
749 return new Min_expression(left
, right
);
752 // Class Section_expression. This is a parent class used for
753 // functions which take the name of an output section.
755 class Section_expression
: public Expression
758 Section_expression(const char* section_name
, size_t section_name_len
)
759 : section_name_(section_name
, section_name_len
)
763 value(const Expression_eval_info
*);
767 { fprintf(f
, "%s(%s)", this->function_name(), this->section_name_
.c_str()); }
770 // The child class must implement this.
772 value_from_output_section(const Expression_eval_info
*,
773 Output_section
*) = 0;
775 // The child class must implement this.
777 value_from_script_output_section(uint64_t address
, uint64_t load_address
,
778 uint64_t addralign
, uint64_t size
) = 0;
780 // The child class must implement this.
782 function_name() const = 0;
785 std::string section_name_
;
789 Section_expression::value(const Expression_eval_info
* eei
)
791 const char* section_name
= this->section_name_
.c_str();
792 Output_section
* os
= eei
->layout
->find_output_section(section_name
);
794 return this->value_from_output_section(eei
, os
);
797 uint64_t load_address
;
800 const Script_options
* ss
= eei
->layout
->script_options();
801 if (ss
->saw_sections_clause())
803 if (ss
->script_sections()->get_output_section_info(section_name
,
808 return this->value_from_script_output_section(address
, load_address
,
812 gold_error("%s called on nonexistent output section '%s'",
813 this->function_name(), section_name
);
817 // ABSOLUTE function.
819 class Absolute_expression
: public Unary_expression
822 Absolute_expression(Expression
* arg
)
823 : Unary_expression(arg
)
827 value(const Expression_eval_info
* eei
)
829 uint64_t ret
= this->arg_value(eei
, NULL
);
830 // Force the value to be absolute.
831 if (eei
->result_section_pointer
!= NULL
)
832 *eei
->result_section_pointer
= NULL
;
839 fprintf(f
, "ABSOLUTE(");
845 extern "C" Expression
*
846 script_exp_function_absolute(Expression
* arg
)
848 return new Absolute_expression(arg
);
853 class Align_expression
: public Binary_expression
856 Align_expression(Expression
* left
, Expression
* right
)
857 : Binary_expression(left
, right
)
861 value(const Expression_eval_info
* eei
)
863 Output_section
* align_section
;
864 uint64_t align
= this->right_value(eei
, &align_section
, NULL
);
865 if (align_section
!= NULL
866 && parameters
->options().relocatable())
867 gold_warning(_("aligning to section relative value"));
869 if (eei
->result_alignment_pointer
!= NULL
870 && align
> *eei
->result_alignment_pointer
)
873 while ((a
& (a
- 1)) != 0)
875 *eei
->result_alignment_pointer
= a
;
878 uint64_t value
= this->left_value(eei
, eei
->result_section_pointer
, NULL
);
881 return ((value
+ align
- 1) / align
) * align
;
886 { this->print_function(f
, "ALIGN"); }
889 extern "C" Expression
*
890 script_exp_function_align(Expression
* left
, Expression
* right
)
892 return new Align_expression(left
, right
);
897 class Assert_expression
: public Unary_expression
900 Assert_expression(Expression
* arg
, const char* message
, size_t length
)
901 : Unary_expression(arg
), message_(message
, length
)
905 value(const Expression_eval_info
* eei
)
907 uint64_t value
= this->arg_value(eei
, eei
->result_section_pointer
);
908 if (!value
&& eei
->check_assertions
)
909 gold_error("%s", this->message_
.c_str());
916 fprintf(f
, "ASSERT(");
918 fprintf(f
, ", %s)", this->message_
.c_str());
922 std::string message_
;
925 extern "C" Expression
*
926 script_exp_function_assert(Expression
* expr
, const char* message
,
929 return new Assert_expression(expr
, message
, length
);
934 class Addr_expression
: public Section_expression
937 Addr_expression(const char* section_name
, size_t section_name_len
)
938 : Section_expression(section_name
, section_name_len
)
943 value_from_output_section(const Expression_eval_info
* eei
,
946 if (eei
->result_section_pointer
!= NULL
)
947 *eei
->result_section_pointer
= os
;
948 return os
->address();
952 value_from_script_output_section(uint64_t address
, uint64_t, uint64_t,
957 function_name() const
961 extern "C" Expression
*
962 script_exp_function_addr(const char* section_name
, size_t section_name_len
)
964 return new Addr_expression(section_name
, section_name_len
);
969 class Alignof_expression
: public Section_expression
972 Alignof_expression(const char* section_name
, size_t section_name_len
)
973 : Section_expression(section_name
, section_name_len
)
978 value_from_output_section(const Expression_eval_info
*,
980 { return os
->addralign(); }
983 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign
,
985 { return addralign
; }
988 function_name() const
989 { return "ALIGNOF"; }
992 extern "C" Expression
*
993 script_exp_function_alignof(const char* section_name
, size_t section_name_len
)
995 return new Alignof_expression(section_name
, section_name_len
);
998 // CONSTANT. It would be nice if we could simply evaluate this
999 // immediately and return an Integer_expression, but unfortunately we
1000 // don't know the target.
1002 class Constant_expression
: public Expression
1005 Constant_expression(const char* name
, size_t length
);
1008 value(const Expression_eval_info
*);
1011 print(FILE* f
) const;
1014 enum Constant_function
1016 CONSTANT_MAXPAGESIZE
,
1017 CONSTANT_COMMONPAGESIZE
1020 Constant_function function_
;
1023 Constant_expression::Constant_expression(const char* name
, size_t length
)
1025 if (length
== 11 && strncmp(name
, "MAXPAGESIZE", length
) == 0)
1026 this->function_
= CONSTANT_MAXPAGESIZE
;
1027 else if (length
== 14 && strncmp(name
, "COMMONPAGESIZE", length
) == 0)
1028 this->function_
= CONSTANT_COMMONPAGESIZE
;
1031 std::string
s(name
, length
);
1032 gold_error(_("unknown constant %s"), s
.c_str());
1033 this->function_
= CONSTANT_MAXPAGESIZE
;
1038 Constant_expression::value(const Expression_eval_info
*)
1040 switch (this->function_
)
1042 case CONSTANT_MAXPAGESIZE
:
1043 return parameters
->target().abi_pagesize();
1044 case CONSTANT_COMMONPAGESIZE
:
1045 return parameters
->target().common_pagesize();
1052 Constant_expression::print(FILE* f
) const
1055 switch (this->function_
)
1057 case CONSTANT_MAXPAGESIZE
:
1058 name
= "MAXPAGESIZE";
1060 case CONSTANT_COMMONPAGESIZE
:
1061 name
= "COMMONPAGESIZE";
1066 fprintf(f
, "CONSTANT(%s)", name
);
1069 extern "C" Expression
*
1070 script_exp_function_constant(const char* name
, size_t length
)
1072 return new Constant_expression(name
, length
);
1075 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1076 // back to the general case.
1078 extern "C" Expression
*
1079 script_exp_function_data_segment_align(Expression
* left
, Expression
*)
1081 Expression
* e1
= script_exp_function_align(script_exp_string(".", 1), left
);
1082 Expression
* e2
= script_exp_binary_sub(left
, script_exp_integer(1));
1083 Expression
* e3
= script_exp_binary_bitwise_and(script_exp_string(".", 1),
1085 return script_exp_binary_add(e1
, e3
);
1088 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1090 extern "C" Expression
*
1091 script_exp_function_data_segment_relro_end(Expression
*, Expression
* right
)
1096 // DATA_SEGMENT_END. FIXME: This is not implemented.
1098 extern "C" Expression
*
1099 script_exp_function_data_segment_end(Expression
* val
)
1104 // DEFINED function.
1106 class Defined_expression
: public Expression
1109 Defined_expression(const char* symbol_name
, size_t symbol_name_len
)
1110 : symbol_name_(symbol_name
, symbol_name_len
)
1114 value(const Expression_eval_info
* eei
)
1116 Symbol
* sym
= eei
->symtab
->lookup(this->symbol_name_
.c_str());
1117 return sym
!= NULL
&& sym
->is_defined();
1121 print(FILE* f
) const
1122 { fprintf(f
, "DEFINED(%s)", this->symbol_name_
.c_str()); }
1125 std::string symbol_name_
;
1128 extern "C" Expression
*
1129 script_exp_function_defined(const char* symbol_name
, size_t symbol_name_len
)
1131 return new Defined_expression(symbol_name
, symbol_name_len
);
1134 // LOADADDR function
1136 class Loadaddr_expression
: public Section_expression
1139 Loadaddr_expression(const char* section_name
, size_t section_name_len
)
1140 : Section_expression(section_name
, section_name_len
)
1145 value_from_output_section(const Expression_eval_info
* eei
,
1148 if (os
->has_load_address())
1149 return os
->load_address();
1152 if (eei
->result_section_pointer
!= NULL
)
1153 *eei
->result_section_pointer
= os
;
1154 return os
->address();
1159 value_from_script_output_section(uint64_t, uint64_t load_address
, uint64_t,
1161 { return load_address
; }
1164 function_name() const
1165 { return "LOADADDR"; }
1168 extern "C" Expression
*
1169 script_exp_function_loadaddr(const char* section_name
, size_t section_name_len
)
1171 return new Loadaddr_expression(section_name
, section_name_len
);
1176 class Sizeof_expression
: public Section_expression
1179 Sizeof_expression(const char* section_name
, size_t section_name_len
)
1180 : Section_expression(section_name
, section_name_len
)
1185 value_from_output_section(const Expression_eval_info
*,
1188 // We can not use data_size here, as the size of the section may
1189 // not have been finalized. Instead we get whatever the current
1190 // size is. This will work correctly for backward references in
1192 return os
->current_data_size();
1196 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1201 function_name() const
1202 { return "SIZEOF"; }
1205 extern "C" Expression
*
1206 script_exp_function_sizeof(const char* section_name
, size_t section_name_len
)
1208 return new Sizeof_expression(section_name
, section_name_len
);
1213 class Sizeof_headers_expression
: public Expression
1216 Sizeof_headers_expression()
1220 value(const Expression_eval_info
*);
1223 print(FILE* f
) const
1224 { fprintf(f
, "SIZEOF_HEADERS"); }
1228 Sizeof_headers_expression::value(const Expression_eval_info
* eei
)
1230 unsigned int ehdr_size
;
1231 unsigned int phdr_size
;
1232 if (parameters
->target().get_size() == 32)
1234 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
1235 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
1237 else if (parameters
->target().get_size() == 64)
1239 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
1240 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
1245 return ehdr_size
+ phdr_size
* eei
->layout
->expected_segment_count();
1248 extern "C" Expression
*
1249 script_exp_function_sizeof_headers()
1251 return new Sizeof_headers_expression();
1256 class Segment_start_expression
: public Unary_expression
1259 Segment_start_expression(const char* segment_name
, size_t segment_name_len
,
1260 Expression
* default_value
)
1261 : Unary_expression(default_value
),
1262 segment_name_(segment_name
, segment_name_len
)
1266 value(const Expression_eval_info
*);
1269 print(FILE* f
) const
1271 fprintf(f
, "SEGMENT_START(\"%s\", ", this->segment_name_
.c_str());
1277 std::string segment_name_
;
1281 Segment_start_expression::value(const Expression_eval_info
* eei
)
1283 // Check for command line overrides.
1284 if (parameters
->options().user_set_Ttext()
1285 && this->segment_name_
== ".text")
1286 return parameters
->options().Ttext();
1287 else if (parameters
->options().user_set_Tdata()
1288 && this->segment_name_
== ".data")
1289 return parameters
->options().Tdata();
1290 else if (parameters
->options().user_set_Tbss()
1291 && this->segment_name_
== ".bss")
1292 return parameters
->options().Tbss();
1295 uint64_t ret
= this->arg_value(eei
, NULL
);
1296 // Force the value to be absolute.
1297 if (eei
->result_section_pointer
!= NULL
)
1298 *eei
->result_section_pointer
= NULL
;
1303 extern "C" Expression
*
1304 script_exp_function_segment_start(const char* segment_name
,
1305 size_t segment_name_len
,
1306 Expression
* default_value
)
1308 return new Segment_start_expression(segment_name
, segment_name_len
,
1312 } // End namespace gold.