1 // expression.cc -- expressions in linker scripts for gold
3 // Copyright (C) 2006-2024 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
*);
209 set_expr_sym_in_real_elf(Symbol_table
* symtab
) const
211 Symbol
* sym
= symtab
->lookup(this->name_
.c_str());
213 sym
->set_in_real_elf();
218 { fprintf(f
, "%s", this->name_
.c_str()); }
225 Symbol_expression::value(const Expression_eval_info
* eei
)
227 Symbol
* sym
= eei
->symtab
->lookup(this->name_
.c_str());
228 if (sym
== NULL
|| !sym
->is_defined())
230 gold_error(_("undefined symbol '%s' referenced in expression"),
231 this->name_
.c_str());
235 if (eei
->result_section_pointer
!= NULL
)
236 *eei
->result_section_pointer
= sym
->output_section();
237 if (eei
->type_pointer
!= NULL
)
238 *eei
->type_pointer
= sym
->type();
239 if (eei
->vis_pointer
!= NULL
)
240 *eei
->vis_pointer
= sym
->visibility();
241 if (eei
->nonvis_pointer
!= NULL
)
242 *eei
->nonvis_pointer
= sym
->nonvis();
244 if (parameters
->target().get_size() == 32)
245 return eei
->symtab
->get_sized_symbol
<32>(sym
)->value();
246 else if (parameters
->target().get_size() == 64)
247 return eei
->symtab
->get_sized_symbol
<64>(sym
)->value();
252 // An expression whose value is the value of the special symbol ".".
253 // This is only valid within a SECTIONS clause.
255 class Dot_expression
: public Expression
262 value(const Expression_eval_info
*);
270 Dot_expression::value(const Expression_eval_info
* eei
)
272 if (!eei
->is_dot_available
)
274 gold_error(_("invalid reference to dot symbol outside of "
278 if (eei
->result_section_pointer
!= NULL
)
279 *eei
->result_section_pointer
= eei
->dot_section
;
280 return eei
->dot_value
;
283 // A string. This is either the name of a symbol, or ".".
285 extern "C" Expression
*
286 script_exp_string(const char* name
, size_t length
)
288 if (length
== 1 && name
[0] == '.')
289 return new Dot_expression();
291 return new Symbol_expression(name
, length
);
294 // A unary expression.
296 class Unary_expression
: public Expression
299 Unary_expression(Expression
* arg
)
304 { delete this->arg_
; }
308 arg_value(const Expression_eval_info
* eei
,
309 Output_section
** arg_section_pointer
) const
311 return this->arg_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
312 eei
->check_assertions
,
313 eei
->is_dot_available
,
317 eei
->result_alignment_pointer
,
322 eei
->is_valid_pointer
);
326 arg_print(FILE* f
) const
327 { this->arg_
->print(f
); }
330 set_expr_sym_in_real_elf(Symbol_table
* symtab
) const
331 { return this->arg_
->set_expr_sym_in_real_elf(symtab
); }
337 // Handle unary operators. We use a preprocessor macro as a hack to
338 // capture the C operator.
340 #define UNARY_EXPRESSION(NAME, OPERATOR) \
341 class Unary_ ## NAME : public Unary_expression \
344 Unary_ ## NAME(Expression* arg) \
345 : Unary_expression(arg) \
349 value(const Expression_eval_info* eei) \
351 Output_section* arg_section; \
352 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
353 if (arg_section != NULL && parameters->options().relocatable()) \
354 gold_warning(_("unary " #NAME " applied to section " \
355 "relative value")); \
360 print(FILE* f) const \
362 fprintf(f, "(%s ", #OPERATOR); \
363 this->arg_print(f); \
368 extern "C" Expression* \
369 script_exp_unary_ ## NAME(Expression* arg) \
371 return new Unary_ ## NAME(arg); \
374 UNARY_EXPRESSION(minus
, -)
375 UNARY_EXPRESSION(logical_not
, !)
376 UNARY_EXPRESSION(bitwise_not
, ~)
378 // A binary expression.
380 class Binary_expression
: public Expression
383 Binary_expression(Expression
* left
, Expression
* right
)
384 : left_(left
), right_(right
)
395 left_value(const Expression_eval_info
* eei
,
396 Output_section
** section_pointer
,
397 uint64_t* alignment_pointer
) const
399 return this->left_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
400 eei
->check_assertions
,
401 eei
->is_dot_available
,
410 eei
->is_valid_pointer
);
414 right_value(const Expression_eval_info
* eei
,
415 Output_section
** section_pointer
,
416 uint64_t* alignment_pointer
) const
418 return this->right_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
419 eei
->check_assertions
,
420 eei
->is_dot_available
,
429 eei
->is_valid_pointer
);
433 left_print(FILE* f
) const
434 { this->left_
->print(f
); }
437 right_print(FILE* f
) const
438 { this->right_
->print(f
); }
440 // This is a call to function FUNCTION_NAME. Print it. This is for
443 print_function(FILE* f
, const char* function_name
) const
445 fprintf(f
, "%s(", function_name
);
448 this->right_print(f
);
453 set_expr_sym_in_real_elf(Symbol_table
* symtab
) const
455 this->left_
->set_expr_sym_in_real_elf(symtab
);
456 this->right_
->set_expr_sym_in_real_elf(symtab
);
464 // Handle binary operators. We use a preprocessor macro as a hack to
465 // capture the C operator. KEEP_LEFT means that if the left operand
466 // is section relative and the right operand is not, the result uses
467 // the same section as the left operand. KEEP_RIGHT is the same with
468 // left and right swapped. IS_DIV means that we need to give an error
469 // if the right operand is zero. WARN means that we should warn if
470 // used on section relative values in a relocatable link. We always
471 // warn if used on values in different sections in a relocatable link.
473 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
474 class Binary_ ## NAME : public Binary_expression \
477 Binary_ ## NAME(Expression* left, Expression* right) \
478 : Binary_expression(left, right) \
482 value(const Expression_eval_info* eei) \
484 Output_section* left_section; \
485 uint64_t left_alignment = 0; \
486 uint64_t left = this->left_value(eei, &left_section, \
488 Output_section* right_section; \
489 uint64_t right_alignment = 0; \
490 uint64_t right = this->right_value(eei, &right_section, \
492 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
494 if (eei->result_section_pointer != NULL) \
495 *eei->result_section_pointer = right_section; \
496 if (eei->result_alignment_pointer != NULL \
497 && right_alignment > *eei->result_alignment_pointer) \
498 *eei->result_alignment_pointer = right_alignment; \
501 && left_section != NULL \
502 && right_section == NULL) \
504 if (eei->result_section_pointer != NULL) \
505 *eei->result_section_pointer = left_section; \
506 if (eei->result_alignment_pointer != NULL \
507 && left_alignment > *eei->result_alignment_pointer) \
508 *eei->result_alignment_pointer = left_alignment; \
510 else if ((WARN || left_section != right_section) \
511 && (left_section != NULL || right_section != NULL) \
512 && parameters->options().relocatable()) \
513 gold_warning(_("binary " #NAME " applied to section " \
514 "relative value")); \
515 if (IS_DIV && right == 0) \
517 gold_error(_(#NAME " by zero")); \
520 return left OPERATOR right; \
524 print(FILE* f) const \
527 this->left_print(f); \
528 fprintf(f, " %s ", #OPERATOR); \
529 this->right_print(f); \
534 extern "C" Expression* \
535 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
537 return new Binary_ ## NAME(left, right); \
540 BINARY_EXPRESSION(mult
, *, false, false, false, true)
541 BINARY_EXPRESSION(div
, /, false, false, true, true)
542 BINARY_EXPRESSION(mod
, %, false, false, true, true)
543 BINARY_EXPRESSION(add
, +, true, true, false, true)
544 BINARY_EXPRESSION(sub
, -, true, false, false, false)
545 BINARY_EXPRESSION(lshift
, <<, false, false, false, true)
546 BINARY_EXPRESSION(rshift
, >>, false, false, false, true)
547 BINARY_EXPRESSION(eq
, ==, false, false, false, false)
548 BINARY_EXPRESSION(ne
, !=, false, false, false, false)
549 BINARY_EXPRESSION(le
, <=, false, false, false, false)
550 BINARY_EXPRESSION(ge
, >=, false, false, false, false)
551 BINARY_EXPRESSION(lt
, <, false, false, false, false)
552 BINARY_EXPRESSION(gt
, >, false, false, false, false)
553 BINARY_EXPRESSION(bitwise_and
, &, true, true, false, true)
554 BINARY_EXPRESSION(bitwise_xor
, ^, true, true, false, true)
555 BINARY_EXPRESSION(bitwise_or
, |, true, true, false, true)
556 BINARY_EXPRESSION(logical_and
, &&, false, false, false, true)
557 BINARY_EXPRESSION(logical_or
, ||, false, false, false, true)
559 // A trinary expression.
561 class Trinary_expression
: public Expression
564 Trinary_expression(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
565 : arg1_(arg1
), arg2_(arg2
), arg3_(arg3
)
568 ~Trinary_expression()
577 arg1_value(const Expression_eval_info
* eei
,
578 Output_section
** section_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 arg2_value(const Expression_eval_info
* eei
,
596 Output_section
** section_pointer
,
597 uint64_t* alignment_pointer
) const
599 return this->arg2_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
600 eei
->check_assertions
,
601 eei
->is_dot_available
,
610 eei
->is_valid_pointer
);
614 arg3_value(const Expression_eval_info
* eei
,
615 Output_section
** section_pointer
,
616 uint64_t* alignment_pointer
) const
618 return this->arg3_
->eval_maybe_dot(eei
->symtab
, eei
->layout
,
619 eei
->check_assertions
,
620 eei
->is_dot_available
,
629 eei
->is_valid_pointer
);
633 arg1_print(FILE* f
) const
634 { this->arg1_
->print(f
); }
637 arg2_print(FILE* f
) const
638 { this->arg2_
->print(f
); }
641 arg3_print(FILE* f
) const
642 { this->arg3_
->print(f
); }
645 set_expr_sym_in_real_elf(Symbol_table
* symtab
) const
647 this->arg1_
->set_expr_sym_in_real_elf(symtab
);
648 this->arg2_
->set_expr_sym_in_real_elf(symtab
);
649 this->arg3_
->set_expr_sym_in_real_elf(symtab
);
658 // The conditional operator.
660 class Trinary_cond
: public Trinary_expression
663 Trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
664 : Trinary_expression(arg1
, arg2
, arg3
)
668 value(const Expression_eval_info
* eei
)
670 Output_section
* arg1_section
;
671 uint64_t arg1
= this->arg1_value(eei
, &arg1_section
);
673 ? this->arg2_value(eei
, eei
->result_section_pointer
,
674 eei
->result_alignment_pointer
)
675 : this->arg3_value(eei
, eei
->result_section_pointer
,
676 eei
->result_alignment_pointer
));
692 extern "C" Expression
*
693 script_exp_trinary_cond(Expression
* arg1
, Expression
* arg2
, Expression
* arg3
)
695 return new Trinary_cond(arg1
, arg2
, arg3
);
700 class Max_expression
: public Binary_expression
703 Max_expression(Expression
* left
, Expression
* right
)
704 : Binary_expression(left
, right
)
708 value(const Expression_eval_info
* eei
)
710 Output_section
* left_section
;
711 uint64_t left_alignment
;
712 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
713 Output_section
* right_section
;
714 uint64_t right_alignment
;
715 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
716 if (left_section
== right_section
)
718 if (eei
->result_section_pointer
!= NULL
)
719 *eei
->result_section_pointer
= left_section
;
721 else if ((left_section
!= NULL
|| right_section
!= NULL
)
722 && parameters
->options().relocatable())
723 gold_warning(_("max applied to section relative value"));
724 if (eei
->result_alignment_pointer
!= NULL
)
726 uint64_t ra
= *eei
->result_alignment_pointer
;
728 ra
= std::max(ra
, left_alignment
);
729 else if (right
> left
)
730 ra
= std::max(ra
, right_alignment
);
732 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
733 *eei
->result_alignment_pointer
= ra
;
735 return std::max(left
, right
);
740 { this->print_function(f
, "MAX"); }
743 extern "C" Expression
*
744 script_exp_function_max(Expression
* left
, Expression
* right
)
746 return new Max_expression(left
, right
);
751 class Min_expression
: public Binary_expression
754 Min_expression(Expression
* left
, Expression
* right
)
755 : Binary_expression(left
, right
)
759 value(const Expression_eval_info
* eei
)
761 Output_section
* left_section
;
762 uint64_t left_alignment
;
763 uint64_t left
= this->left_value(eei
, &left_section
, &left_alignment
);
764 Output_section
* right_section
;
765 uint64_t right_alignment
;
766 uint64_t right
= this->right_value(eei
, &right_section
, &right_alignment
);
767 if (left_section
== right_section
)
769 if (eei
->result_section_pointer
!= NULL
)
770 *eei
->result_section_pointer
= left_section
;
772 else if ((left_section
!= NULL
|| right_section
!= NULL
)
773 && parameters
->options().relocatable())
774 gold_warning(_("min applied to section relative value"));
775 if (eei
->result_alignment_pointer
!= NULL
)
777 uint64_t ra
= *eei
->result_alignment_pointer
;
779 ra
= std::max(ra
, left_alignment
);
780 else if (right
< left
)
781 ra
= std::max(ra
, right_alignment
);
783 ra
= std::max(ra
, std::max(left_alignment
, right_alignment
));
784 *eei
->result_alignment_pointer
= ra
;
786 return std::min(left
, right
);
791 { this->print_function(f
, "MIN"); }
794 extern "C" Expression
*
795 script_exp_function_min(Expression
* left
, Expression
* right
)
797 return new Min_expression(left
, right
);
800 // Class Section_expression. This is a parent class used for
801 // functions which take the name of an output section.
803 class Section_expression
: public Expression
806 Section_expression(const char* section_name
, size_t section_name_len
)
807 : section_name_(section_name
, section_name_len
)
811 value(const Expression_eval_info
*);
815 { fprintf(f
, "%s(%s)", this->function_name(), this->section_name_
.c_str()); }
818 // The child class must implement this.
820 value_from_output_section(const Expression_eval_info
*,
821 Output_section
*) = 0;
823 // The child class must implement this.
825 value_from_script_output_section(uint64_t address
, uint64_t load_address
,
826 uint64_t addralign
, uint64_t size
) = 0;
828 // The child class must implement this.
830 function_name() const = 0;
833 std::string section_name_
;
837 Section_expression::value(const Expression_eval_info
* eei
)
839 const char* section_name
= this->section_name_
.c_str();
840 Output_section
* os
= eei
->layout
->find_output_section(section_name
);
842 return this->value_from_output_section(eei
, os
);
845 uint64_t load_address
;
848 const Script_options
* ss
= eei
->layout
->script_options();
849 if (ss
->saw_sections_clause())
851 if (ss
->script_sections()->get_output_section_info(section_name
,
856 return this->value_from_script_output_section(address
, load_address
,
860 gold_error("%s called on nonexistent output section '%s'",
861 this->function_name(), section_name
);
865 // ABSOLUTE function.
867 class Absolute_expression
: public Unary_expression
870 Absolute_expression(Expression
* arg
)
871 : Unary_expression(arg
)
875 value(const Expression_eval_info
* eei
)
877 uint64_t ret
= this->arg_value(eei
, NULL
);
878 // Force the value to be absolute.
879 if (eei
->result_section_pointer
!= NULL
)
880 *eei
->result_section_pointer
= NULL
;
887 fprintf(f
, "ABSOLUTE(");
893 extern "C" Expression
*
894 script_exp_function_absolute(Expression
* arg
)
896 return new Absolute_expression(arg
);
901 class Align_expression
: public Binary_expression
904 Align_expression(Expression
* left
, Expression
* right
)
905 : Binary_expression(left
, right
)
909 value(const Expression_eval_info
* eei
)
911 Output_section
* align_section
;
912 uint64_t align
= this->right_value(eei
, &align_section
, NULL
);
913 if (align_section
!= NULL
914 && parameters
->options().relocatable())
915 gold_warning(_("aligning to section relative value"));
917 if (eei
->result_alignment_pointer
!= NULL
918 && align
> *eei
->result_alignment_pointer
)
921 while ((a
& (a
- 1)) != 0)
923 *eei
->result_alignment_pointer
= a
;
926 uint64_t value
= this->left_value(eei
, eei
->result_section_pointer
, NULL
);
929 return ((value
+ align
- 1) / align
) * align
;
934 { this->print_function(f
, "ALIGN"); }
937 extern "C" Expression
*
938 script_exp_function_align(Expression
* left
, Expression
* right
)
940 return new Align_expression(left
, right
);
945 class Assert_expression
: public Unary_expression
948 Assert_expression(Expression
* arg
, const char* message
, size_t length
)
949 : Unary_expression(arg
), message_(message
, length
)
953 value(const Expression_eval_info
* eei
)
955 uint64_t value
= this->arg_value(eei
, eei
->result_section_pointer
);
956 if (!value
&& eei
->check_assertions
)
957 gold_error("%s", this->message_
.c_str());
964 fprintf(f
, "ASSERT(");
966 fprintf(f
, ", %s)", this->message_
.c_str());
970 std::string message_
;
973 extern "C" Expression
*
974 script_exp_function_assert(Expression
* expr
, const char* message
,
977 return new Assert_expression(expr
, message
, length
);
982 class Addr_expression
: public Section_expression
985 Addr_expression(const char* section_name
, size_t section_name_len
)
986 : Section_expression(section_name
, section_name_len
)
991 value_from_output_section(const Expression_eval_info
* eei
,
994 if (eei
->result_section_pointer
!= NULL
)
995 *eei
->result_section_pointer
= os
;
996 if (os
->is_address_valid())
997 return os
->address();
998 *eei
->is_valid_pointer
= false;
1003 value_from_script_output_section(uint64_t address
, uint64_t, uint64_t,
1008 function_name() const
1012 extern "C" Expression
*
1013 script_exp_function_addr(const char* section_name
, size_t section_name_len
)
1015 return new Addr_expression(section_name
, section_name_len
);
1020 class Alignof_expression
: public Section_expression
1023 Alignof_expression(const char* section_name
, size_t section_name_len
)
1024 : Section_expression(section_name
, section_name_len
)
1029 value_from_output_section(const Expression_eval_info
*,
1031 { return os
->addralign(); }
1034 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign
,
1036 { return addralign
; }
1039 function_name() const
1040 { return "ALIGNOF"; }
1043 extern "C" Expression
*
1044 script_exp_function_alignof(const char* section_name
, size_t section_name_len
)
1046 return new Alignof_expression(section_name
, section_name_len
);
1049 // CONSTANT. It would be nice if we could simply evaluate this
1050 // immediately and return an Integer_expression, but unfortunately we
1051 // don't know the target.
1053 class Constant_expression
: public Expression
1056 Constant_expression(const char* name
, size_t length
);
1059 value(const Expression_eval_info
*);
1062 print(FILE* f
) const;
1065 enum Constant_function
1067 CONSTANT_MAXPAGESIZE
,
1068 CONSTANT_COMMONPAGESIZE
1071 Constant_function function_
;
1074 Constant_expression::Constant_expression(const char* name
, size_t length
)
1076 if (length
== 11 && strncmp(name
, "MAXPAGESIZE", length
) == 0)
1077 this->function_
= CONSTANT_MAXPAGESIZE
;
1078 else if (length
== 14 && strncmp(name
, "COMMONPAGESIZE", length
) == 0)
1079 this->function_
= CONSTANT_COMMONPAGESIZE
;
1082 std::string
s(name
, length
);
1083 gold_error(_("unknown constant %s"), s
.c_str());
1084 this->function_
= CONSTANT_MAXPAGESIZE
;
1089 Constant_expression::value(const Expression_eval_info
*)
1091 switch (this->function_
)
1093 case CONSTANT_MAXPAGESIZE
:
1094 return parameters
->target().abi_pagesize();
1095 case CONSTANT_COMMONPAGESIZE
:
1096 return parameters
->target().common_pagesize();
1103 Constant_expression::print(FILE* f
) const
1106 switch (this->function_
)
1108 case CONSTANT_MAXPAGESIZE
:
1109 name
= "MAXPAGESIZE";
1111 case CONSTANT_COMMONPAGESIZE
:
1112 name
= "COMMONPAGESIZE";
1117 fprintf(f
, "CONSTANT(%s)", name
);
1120 extern "C" Expression
*
1121 script_exp_function_constant(const char* name
, size_t length
)
1123 return new Constant_expression(name
, length
);
1126 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1127 // back to the general case.
1129 extern "C" Expression
*
1130 script_exp_function_data_segment_align(Expression
* left
, Expression
*)
1132 Expression
* e1
= script_exp_function_align(script_exp_string(".", 1), left
);
1133 Expression
* e2
= script_exp_binary_sub(left
, script_exp_integer(1));
1134 Expression
* e3
= script_exp_binary_bitwise_and(script_exp_string(".", 1),
1136 return script_exp_binary_add(e1
, e3
);
1139 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1141 extern "C" Expression
*
1142 script_exp_function_data_segment_relro_end(Expression
*, Expression
* right
)
1147 // DATA_SEGMENT_END. FIXME: This is not implemented.
1149 extern "C" Expression
*
1150 script_exp_function_data_segment_end(Expression
* val
)
1155 // DEFINED function.
1157 class Defined_expression
: public Expression
1160 Defined_expression(const char* symbol_name
, size_t symbol_name_len
)
1161 : symbol_name_(symbol_name
, symbol_name_len
)
1165 value(const Expression_eval_info
* eei
)
1167 Symbol
* sym
= eei
->symtab
->lookup(this->symbol_name_
.c_str());
1168 return sym
!= NULL
&& sym
->is_defined();
1172 print(FILE* f
) const
1173 { fprintf(f
, "DEFINED(%s)", this->symbol_name_
.c_str()); }
1176 std::string symbol_name_
;
1179 extern "C" Expression
*
1180 script_exp_function_defined(const char* symbol_name
, size_t symbol_name_len
)
1182 return new Defined_expression(symbol_name
, symbol_name_len
);
1185 // LOADADDR function
1187 class Loadaddr_expression
: public Section_expression
1190 Loadaddr_expression(const char* section_name
, size_t section_name_len
)
1191 : Section_expression(section_name
, section_name_len
)
1196 value_from_output_section(const Expression_eval_info
* eei
,
1199 if (os
->has_load_address())
1200 return os
->load_address();
1203 if (eei
->result_section_pointer
!= NULL
)
1204 *eei
->result_section_pointer
= os
;
1205 return os
->address();
1210 value_from_script_output_section(uint64_t, uint64_t load_address
, uint64_t,
1212 { return load_address
; }
1215 function_name() const
1216 { return "LOADADDR"; }
1219 extern "C" Expression
*
1220 script_exp_function_loadaddr(const char* section_name
, size_t section_name_len
)
1222 return new Loadaddr_expression(section_name
, section_name_len
);
1227 class Sizeof_expression
: public Section_expression
1230 Sizeof_expression(const char* section_name
, size_t section_name_len
)
1231 : Section_expression(section_name
, section_name_len
)
1236 value_from_output_section(const Expression_eval_info
*,
1239 // We can not use data_size here, as the size of the section may
1240 // not have been finalized. Instead we get whatever the current
1241 // size is. This will work correctly for backward references in
1243 return os
->current_data_size();
1247 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1252 function_name() const
1253 { return "SIZEOF"; }
1256 extern "C" Expression
*
1257 script_exp_function_sizeof(const char* section_name
, size_t section_name_len
)
1259 return new Sizeof_expression(section_name
, section_name_len
);
1264 class Sizeof_headers_expression
: public Expression
1267 Sizeof_headers_expression()
1271 value(const Expression_eval_info
*);
1274 print(FILE* f
) const
1275 { fprintf(f
, "SIZEOF_HEADERS"); }
1279 Sizeof_headers_expression::value(const Expression_eval_info
* eei
)
1281 unsigned int ehdr_size
;
1282 unsigned int phdr_size
;
1283 if (parameters
->target().get_size() == 32)
1285 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
1286 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
1288 else if (parameters
->target().get_size() == 64)
1290 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
1291 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
1296 return ehdr_size
+ phdr_size
* eei
->layout
->expected_segment_count();
1299 extern "C" Expression
*
1300 script_exp_function_sizeof_headers()
1302 return new Sizeof_headers_expression();
1307 class Segment_start_expression
: public Unary_expression
1310 Segment_start_expression(const char* segment_name
, size_t segment_name_len
,
1311 Expression
* default_value
)
1312 : Unary_expression(default_value
),
1313 segment_name_(segment_name
, segment_name_len
)
1317 value(const Expression_eval_info
*);
1320 print(FILE* f
) const
1322 fprintf(f
, "SEGMENT_START(\"%s\", ", this->segment_name_
.c_str());
1328 std::string segment_name_
;
1332 Segment_start_expression::value(const Expression_eval_info
* eei
)
1334 // Check for command line overrides.
1335 if (parameters
->options().user_set_Ttext()
1336 && this->segment_name_
== ".text")
1337 return parameters
->options().Ttext();
1338 else if (parameters
->options().user_set_Tdata()
1339 && this->segment_name_
== ".data")
1340 return parameters
->options().Tdata();
1341 else if (parameters
->options().user_set_Tbss()
1342 && this->segment_name_
== ".bss")
1343 return parameters
->options().Tbss();
1346 uint64_t ret
= this->arg_value(eei
, NULL
);
1347 // Force the value to be absolute.
1348 if (eei
->result_section_pointer
!= NULL
)
1349 *eei
->result_section_pointer
= NULL
;
1354 extern "C" Expression
*
1355 script_exp_function_segment_start(const char* segment_name
,
1356 size_t segment_name_len
,
1357 Expression
* default_value
)
1359 return new Segment_start_expression(segment_name
, segment_name_len
,
1363 } // End namespace gold.