bfd/
[binutils.git] / gold / expression.cc
blob25f3ac33936a30a604d6af5a3b68ebecc733a933
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.
23 #include "gold.h"
25 #include <string>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "symtab.h"
30 #include "layout.h"
31 #include "output.h"
32 #include "script.h"
33 #include "script-c.h"
35 namespace gold
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
53 // The symbol table.
54 const Symbol_table* symtab;
55 // The layout--we use this to get section information.
56 const Layout* layout;
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.
63 uint64_t dot_value;
64 // The section in which the dot symbol is defined; this is NULL if
65 // it is absolute.
66 Output_section* dot_section;
67 // Points to where the section of the result should be stored.
68 Output_section** result_section_pointer;
71 // Evaluate an expression.
73 uint64_t
74 Expression::eval(const Symbol_table* symtab, const Layout* layout,
75 bool check_assertions)
77 Output_section* dummy;
78 return this->eval_maybe_dot(symtab, layout, check_assertions,
79 false, 0, NULL, &dummy);
82 // Evaluate an expression which may refer to the dot symbol.
84 uint64_t
85 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
86 bool check_assertions, uint64_t dot_value,
87 Output_section* dot_section,
88 Output_section** result_section_pointer)
90 return this->eval_maybe_dot(symtab, layout, check_assertions, true,
91 dot_value, dot_section, result_section_pointer);
94 // Evaluate an expression which may or may not refer to the dot
95 // symbol.
97 uint64_t
98 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
99 bool check_assertions, bool is_dot_available,
100 uint64_t dot_value, Output_section* dot_section,
101 Output_section** result_section_pointer)
103 Expression_eval_info eei;
104 eei.symtab = symtab;
105 eei.layout = layout;
106 eei.check_assertions = check_assertions;
107 eei.is_dot_available = is_dot_available;
108 eei.dot_value = dot_value;
109 eei.dot_section = dot_section;
111 // We assume the value is absolute, and only set this to a section
112 // if we find a section relative reference.
113 *result_section_pointer = NULL;
114 eei.result_section_pointer = result_section_pointer;
116 return this->value(&eei);
119 // A number.
121 class Integer_expression : public Expression
123 public:
124 Integer_expression(uint64_t val)
125 : val_(val)
128 uint64_t
129 value(const Expression_eval_info*)
130 { return this->val_; }
132 void
133 print(FILE* f) const
134 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
136 private:
137 uint64_t val_;
140 extern "C" Expression*
141 script_exp_integer(uint64_t val)
143 return new Integer_expression(val);
146 // An expression whose value is the value of a symbol.
148 class Symbol_expression : public Expression
150 public:
151 Symbol_expression(const char* name, size_t length)
152 : name_(name, length)
155 uint64_t
156 value(const Expression_eval_info*);
158 void
159 print(FILE* f) const
160 { fprintf(f, "%s", this->name_.c_str()); }
162 private:
163 std::string name_;
166 uint64_t
167 Symbol_expression::value(const Expression_eval_info* eei)
169 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
170 if (sym == NULL || !sym->is_defined())
172 gold_error(_("undefined symbol '%s' referenced in expression"),
173 this->name_.c_str());
174 return 0;
177 *eei->result_section_pointer = sym->output_section();
179 if (parameters->target().get_size() == 32)
180 return eei->symtab->get_sized_symbol<32>(sym)->value();
181 else if (parameters->target().get_size() == 64)
182 return eei->symtab->get_sized_symbol<64>(sym)->value();
183 else
184 gold_unreachable();
187 // An expression whose value is the value of the special symbol ".".
188 // This is only valid within a SECTIONS clause.
190 class Dot_expression : public Expression
192 public:
193 Dot_expression()
196 uint64_t
197 value(const Expression_eval_info*);
199 void
200 print(FILE* f) const
201 { fprintf(f, "."); }
204 uint64_t
205 Dot_expression::value(const Expression_eval_info* eei)
207 if (!eei->is_dot_available)
209 gold_error(_("invalid reference to dot symbol outside of "
210 "SECTIONS clause"));
211 return 0;
213 *eei->result_section_pointer = eei->dot_section;
214 return eei->dot_value;
217 // A string. This is either the name of a symbol, or ".".
219 extern "C" Expression*
220 script_exp_string(const char* name, size_t length)
222 if (length == 1 && name[0] == '.')
223 return new Dot_expression();
224 else
225 return new Symbol_expression(name, length);
228 // A unary expression.
230 class Unary_expression : public Expression
232 public:
233 Unary_expression(Expression* arg)
234 : arg_(arg)
237 ~Unary_expression()
238 { delete this->arg_; }
240 protected:
241 uint64_t
242 arg_value(const Expression_eval_info* eei,
243 Output_section** arg_section_pointer) const
245 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
246 eei->check_assertions,
247 eei->is_dot_available,
248 eei->dot_value,
249 eei->dot_section,
250 arg_section_pointer);
253 void
254 arg_print(FILE* f) const
255 { this->arg_->print(f); }
257 private:
258 Expression* arg_;
261 // Handle unary operators. We use a preprocessor macro as a hack to
262 // capture the C operator.
264 #define UNARY_EXPRESSION(NAME, OPERATOR) \
265 class Unary_ ## NAME : public Unary_expression \
267 public: \
268 Unary_ ## NAME(Expression* arg) \
269 : Unary_expression(arg) \
270 { } \
272 uint64_t \
273 value(const Expression_eval_info* eei) \
275 Output_section* arg_section; \
276 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
277 if (arg_section != NULL && parameters->options().relocatable()) \
278 gold_warning(_("unary " #NAME " applied to section " \
279 "relative value")); \
280 return ret; \
283 void \
284 print(FILE* f) const \
286 fprintf(f, "(%s ", #OPERATOR); \
287 this->arg_print(f); \
288 fprintf(f, ")"); \
290 }; \
292 extern "C" Expression* \
293 script_exp_unary_ ## NAME(Expression* arg) \
295 return new Unary_ ## NAME(arg); \
298 UNARY_EXPRESSION(minus, -)
299 UNARY_EXPRESSION(logical_not, !)
300 UNARY_EXPRESSION(bitwise_not, ~)
302 // A binary expression.
304 class Binary_expression : public Expression
306 public:
307 Binary_expression(Expression* left, Expression* right)
308 : left_(left), right_(right)
311 ~Binary_expression()
313 delete this->left_;
314 delete this->right_;
317 protected:
318 uint64_t
319 left_value(const Expression_eval_info* eei,
320 Output_section** section_pointer) const
322 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
323 eei->check_assertions,
324 eei->is_dot_available,
325 eei->dot_value,
326 eei->dot_section,
327 section_pointer);
330 uint64_t
331 right_value(const Expression_eval_info* eei,
332 Output_section** section_pointer) const
334 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
335 eei->check_assertions,
336 eei->is_dot_available,
337 eei->dot_value,
338 eei->dot_section,
339 section_pointer);
342 void
343 left_print(FILE* f) const
344 { this->left_->print(f); }
346 void
347 right_print(FILE* f) const
348 { this->right_->print(f); }
350 // This is a call to function FUNCTION_NAME. Print it. This is for
351 // debugging.
352 void
353 print_function(FILE* f, const char *function_name) const
355 fprintf(f, "%s(", function_name);
356 this->left_print(f);
357 fprintf(f, ", ");
358 this->right_print(f);
359 fprintf(f, ")");
362 private:
363 Expression* left_;
364 Expression* right_;
367 // Handle binary operators. We use a preprocessor macro as a hack to
368 // capture the C operator. KEEP_LEFT means that if the left operand
369 // is section relative and the right operand is not, the result uses
370 // the same section as the left operand. KEEP_RIGHT is the same with
371 // left and right swapped. IS_DIV means that we need to give an error
372 // if the right operand is zero. WARN means that we should warn if
373 // used on section relative values in a relocatable link. We always
374 // warn if used on values in different sections in a relocatable link.
376 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
377 class Binary_ ## NAME : public Binary_expression \
379 public: \
380 Binary_ ## NAME(Expression* left, Expression* right) \
381 : Binary_expression(left, right) \
382 { } \
384 uint64_t \
385 value(const Expression_eval_info* eei) \
387 Output_section* left_section; \
388 uint64_t left = this->left_value(eei, &left_section); \
389 Output_section* right_section; \
390 uint64_t right = this->right_value(eei, &right_section); \
391 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
392 *eei->result_section_pointer = right_section; \
393 else if (KEEP_LEFT \
394 && left_section != NULL \
395 && right_section == NULL) \
396 *eei->result_section_pointer = left_section; \
397 else if ((WARN || left_section != right_section) \
398 && (left_section != NULL || right_section != NULL) \
399 && parameters->options().relocatable()) \
400 gold_warning(_("binary " #NAME " applied to section " \
401 "relative value")); \
402 if (IS_DIV && right == 0) \
404 gold_error(_(#NAME " by zero")); \
405 return 0; \
407 return left OPERATOR right; \
410 void \
411 print(FILE* f) const \
413 fprintf(f, "("); \
414 this->left_print(f); \
415 fprintf(f, " %s ", #OPERATOR); \
416 this->right_print(f); \
417 fprintf(f, ")"); \
419 }; \
421 extern "C" Expression* \
422 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
424 return new Binary_ ## NAME(left, right); \
427 BINARY_EXPRESSION(mult, *, false, false, false, true)
428 BINARY_EXPRESSION(div, /, false, false, true, true)
429 BINARY_EXPRESSION(mod, %, false, false, true, true)
430 BINARY_EXPRESSION(add, +, true, true, false, true)
431 BINARY_EXPRESSION(sub, -, true, false, false, false)
432 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
433 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
434 BINARY_EXPRESSION(eq, ==, false, false, false, false)
435 BINARY_EXPRESSION(ne, !=, false, false, false, false)
436 BINARY_EXPRESSION(le, <=, false, false, false, false)
437 BINARY_EXPRESSION(ge, >=, false, false, false, false)
438 BINARY_EXPRESSION(lt, <, false, false, false, false)
439 BINARY_EXPRESSION(gt, >, false, false, false, false)
440 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
441 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
442 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
443 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
444 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
446 // A trinary expression.
448 class Trinary_expression : public Expression
450 public:
451 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
452 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
455 ~Trinary_expression()
457 delete this->arg1_;
458 delete this->arg2_;
459 delete this->arg3_;
462 protected:
463 uint64_t
464 arg1_value(const Expression_eval_info* eei,
465 Output_section** section_pointer) const
467 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
468 eei->check_assertions,
469 eei->is_dot_available,
470 eei->dot_value,
471 eei->dot_section,
472 section_pointer);
475 uint64_t
476 arg2_value(const Expression_eval_info* eei,
477 Output_section** section_pointer) const
479 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
480 eei->check_assertions,
481 eei->is_dot_available,
482 eei->dot_value,
483 eei->dot_section,
484 section_pointer);
487 uint64_t
488 arg3_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,
494 eei->dot_value,
495 eei->dot_section,
496 section_pointer);
499 void
500 arg1_print(FILE* f) const
501 { this->arg1_->print(f); }
503 void
504 arg2_print(FILE* f) const
505 { this->arg2_->print(f); }
507 void
508 arg3_print(FILE* f) const
509 { this->arg3_->print(f); }
511 private:
512 Expression* arg1_;
513 Expression* arg2_;
514 Expression* arg3_;
517 // The conditional operator.
519 class Trinary_cond : public Trinary_expression
521 public:
522 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
523 : Trinary_expression(arg1, arg2, arg3)
526 uint64_t
527 value(const Expression_eval_info* eei)
529 Output_section* arg1_section;
530 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
531 return (arg1
532 ? this->arg2_value(eei, eei->result_section_pointer)
533 : this->arg3_value(eei, eei->result_section_pointer));
536 void
537 print(FILE* f) const
539 fprintf(f, "(");
540 this->arg1_print(f);
541 fprintf(f, " ? ");
542 this->arg2_print(f);
543 fprintf(f, " : ");
544 this->arg3_print(f);
545 fprintf(f, ")");
549 extern "C" Expression*
550 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
552 return new Trinary_cond(arg1, arg2, arg3);
555 // Max function.
557 class Max_expression : public Binary_expression
559 public:
560 Max_expression(Expression* left, Expression* right)
561 : Binary_expression(left, right)
564 uint64_t
565 value(const Expression_eval_info* eei)
567 Output_section* left_section;
568 uint64_t left = this->left_value(eei, &left_section);
569 Output_section* right_section;
570 uint64_t right = this->right_value(eei, &right_section);
571 if (left_section == right_section)
572 *eei->result_section_pointer = left_section;
573 else if ((left_section != NULL || right_section != NULL)
574 && parameters->options().relocatable())
575 gold_warning(_("max applied to section relative value"));
576 return std::max(left, right);
579 void
580 print(FILE* f) const
581 { this->print_function(f, "MAX"); }
584 extern "C" Expression*
585 script_exp_function_max(Expression* left, Expression* right)
587 return new Max_expression(left, right);
590 // Min function.
592 class Min_expression : public Binary_expression
594 public:
595 Min_expression(Expression* left, Expression* right)
596 : Binary_expression(left, right)
599 uint64_t
600 value(const Expression_eval_info* eei)
602 Output_section* left_section;
603 uint64_t left = this->left_value(eei, &left_section);
604 Output_section* right_section;
605 uint64_t right = this->right_value(eei, &right_section);
606 if (left_section == right_section)
607 *eei->result_section_pointer = left_section;
608 else if ((left_section != NULL || right_section != NULL)
609 && parameters->options().relocatable())
610 gold_warning(_("min applied to section relative value"));
611 return std::min(left, right);
614 void
615 print(FILE* f) const
616 { this->print_function(f, "MIN"); }
619 extern "C" Expression*
620 script_exp_function_min(Expression* left, Expression* right)
622 return new Min_expression(left, right);
625 // Class Section_expression. This is a parent class used for
626 // functions which take the name of an output section.
628 class Section_expression : public Expression
630 public:
631 Section_expression(const char* section_name, size_t section_name_len)
632 : section_name_(section_name, section_name_len)
635 uint64_t
636 value(const Expression_eval_info*);
638 void
639 print(FILE* f) const
640 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
642 protected:
643 // The child class must implement this.
644 virtual uint64_t
645 value_from_output_section(const Expression_eval_info*,
646 Output_section*) = 0;
648 // The child class must implement this.
649 virtual uint64_t
650 value_from_script_output_section(uint64_t address, uint64_t load_address,
651 uint64_t addralign, uint64_t size) = 0;
653 // The child class must implement this.
654 virtual const char*
655 function_name() const = 0;
657 private:
658 std::string section_name_;
661 uint64_t
662 Section_expression::value(const Expression_eval_info* eei)
664 const char* section_name = this->section_name_.c_str();
665 Output_section* os = eei->layout->find_output_section(section_name);
666 if (os != NULL)
667 return this->value_from_output_section(eei, os);
669 uint64_t address;
670 uint64_t load_address;
671 uint64_t addralign;
672 uint64_t size;
673 const Script_options* ss = eei->layout->script_options();
674 if (ss->saw_sections_clause())
676 if (ss->script_sections()->get_output_section_info(section_name,
677 &address,
678 &load_address,
679 &addralign,
680 &size))
681 return this->value_from_script_output_section(address, load_address,
682 addralign, size);
685 gold_error("%s called on nonexistent output section '%s'",
686 this->function_name(), section_name);
687 return 0;
690 // ABSOLUTE function.
692 class Absolute_expression : public Unary_expression
694 public:
695 Absolute_expression(Expression* arg)
696 : Unary_expression(arg)
699 uint64_t
700 value(const Expression_eval_info* eei)
702 Output_section* dummy;
703 uint64_t ret = this->arg_value(eei, &dummy);
704 // Force the value to be absolute.
705 *eei->result_section_pointer = NULL;
706 return ret;
709 void
710 print(FILE* f) const
712 fprintf(f, "ABSOLUTE(");
713 this->arg_print(f);
714 fprintf(f, ")");
718 extern "C" Expression*
719 script_exp_function_absolute(Expression* arg)
721 return new Absolute_expression(arg);
724 // ALIGN function.
726 class Align_expression : public Binary_expression
728 public:
729 Align_expression(Expression* left, Expression* right)
730 : Binary_expression(left, right)
733 uint64_t
734 value(const Expression_eval_info* eei)
736 Output_section* align_section;
737 uint64_t align = this->right_value(eei, &align_section);
738 if (align_section != NULL
739 && parameters->options().relocatable())
740 gold_warning(_("aligning to section relative value"));
742 uint64_t value = this->left_value(eei, eei->result_section_pointer);
743 if (align <= 1)
744 return value;
745 return ((value + align - 1) / align) * align;
748 void
749 print(FILE* f) const
750 { this->print_function(f, "ALIGN"); }
753 extern "C" Expression*
754 script_exp_function_align(Expression* left, Expression* right)
756 return new Align_expression(left, right);
759 // ASSERT function.
761 class Assert_expression : public Unary_expression
763 public:
764 Assert_expression(Expression* arg, const char* message, size_t length)
765 : Unary_expression(arg), message_(message, length)
768 uint64_t
769 value(const Expression_eval_info* eei)
771 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
772 if (!value && eei->check_assertions)
773 gold_error("%s", this->message_.c_str());
774 return value;
777 void
778 print(FILE* f) const
780 fprintf(f, "ASSERT(");
781 this->arg_print(f);
782 fprintf(f, ", %s)", this->message_.c_str());
785 private:
786 std::string message_;
789 extern "C" Expression*
790 script_exp_function_assert(Expression* expr, const char* message,
791 size_t length)
793 return new Assert_expression(expr, message, length);
796 // ADDR function.
798 class Addr_expression : public Section_expression
800 public:
801 Addr_expression(const char* section_name, size_t section_name_len)
802 : Section_expression(section_name, section_name_len)
805 protected:
806 uint64_t
807 value_from_output_section(const Expression_eval_info* eei,
808 Output_section* os)
810 *eei->result_section_pointer = os;
811 return os->address();
814 uint64_t
815 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
816 uint64_t)
817 { return address; }
819 const char*
820 function_name() const
821 { return "ADDR"; }
824 extern "C" Expression*
825 script_exp_function_addr(const char* section_name, size_t section_name_len)
827 return new Addr_expression(section_name, section_name_len);
830 // ALIGNOF.
832 class Alignof_expression : public Section_expression
834 public:
835 Alignof_expression(const char* section_name, size_t section_name_len)
836 : Section_expression(section_name, section_name_len)
839 protected:
840 uint64_t
841 value_from_output_section(const Expression_eval_info*,
842 Output_section* os)
843 { return os->addralign(); }
845 uint64_t
846 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
847 uint64_t)
848 { return addralign; }
850 const char*
851 function_name() const
852 { return "ALIGNOF"; }
855 extern "C" Expression*
856 script_exp_function_alignof(const char* section_name, size_t section_name_len)
858 return new Alignof_expression(section_name, section_name_len);
861 // CONSTANT. It would be nice if we could simply evaluate this
862 // immediately and return an Integer_expression, but unfortunately we
863 // don't know the target.
865 class Constant_expression : public Expression
867 public:
868 Constant_expression(const char* name, size_t length);
870 uint64_t
871 value(const Expression_eval_info*);
873 void
874 print(FILE* f) const;
876 private:
877 enum Constant_function
879 CONSTANT_MAXPAGESIZE,
880 CONSTANT_COMMONPAGESIZE
883 Constant_function function_;
886 Constant_expression::Constant_expression(const char* name, size_t length)
888 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
889 this->function_ = CONSTANT_MAXPAGESIZE;
890 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
891 this->function_ = CONSTANT_COMMONPAGESIZE;
892 else
894 std::string s(name, length);
895 gold_error(_("unknown constant %s"), s.c_str());
896 this->function_ = CONSTANT_MAXPAGESIZE;
900 uint64_t
901 Constant_expression::value(const Expression_eval_info*)
903 switch (this->function_)
905 case CONSTANT_MAXPAGESIZE:
906 return parameters->target().abi_pagesize();
907 case CONSTANT_COMMONPAGESIZE:
908 return parameters->target().common_pagesize();
909 default:
910 gold_unreachable();
914 void
915 Constant_expression::print(FILE* f) const
917 const char* name;
918 switch (this->function_)
920 case CONSTANT_MAXPAGESIZE:
921 name = "MAXPAGESIZE";
922 break;
923 case CONSTANT_COMMONPAGESIZE:
924 name = "COMMONPAGESIZE";
925 break;
926 default:
927 gold_unreachable();
929 fprintf(f, "CONSTANT(%s)", name);
932 extern "C" Expression*
933 script_exp_function_constant(const char* name, size_t length)
935 return new Constant_expression(name, length);
938 // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
939 // back to the general case.
941 extern "C" Expression*
942 script_exp_function_data_segment_align(Expression* left, Expression*)
944 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
945 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
946 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
947 e2);
948 return script_exp_binary_add(e1, e3);
951 // DATA_SEGMENT_RELRO. FIXME: This is not implemented.
953 extern "C" Expression*
954 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
956 return right;
959 // DATA_SEGMENT_END. FIXME: This is not implemented.
961 extern "C" Expression*
962 script_exp_function_data_segment_end(Expression* val)
964 return val;
967 // DEFINED function.
969 class Defined_expression : public Expression
971 public:
972 Defined_expression(const char* symbol_name, size_t symbol_name_len)
973 : symbol_name_(symbol_name, symbol_name_len)
976 uint64_t
977 value(const Expression_eval_info* eei)
979 Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
980 return sym != NULL && sym->is_defined();
983 void
984 print(FILE* f) const
985 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
987 private:
988 std::string symbol_name_;
991 extern "C" Expression*
992 script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
994 return new Defined_expression(symbol_name, symbol_name_len);
997 // LOADADDR function
999 class Loadaddr_expression : public Section_expression
1001 public:
1002 Loadaddr_expression(const char* section_name, size_t section_name_len)
1003 : Section_expression(section_name, section_name_len)
1006 protected:
1007 uint64_t
1008 value_from_output_section(const Expression_eval_info* eei,
1009 Output_section* os)
1011 if (os->has_load_address())
1012 return os->load_address();
1013 else
1015 *eei->result_section_pointer = os;
1016 return os->address();
1020 uint64_t
1021 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1022 uint64_t)
1023 { return load_address; }
1025 const char*
1026 function_name() const
1027 { return "LOADADDR"; }
1030 extern "C" Expression*
1031 script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
1033 return new Loadaddr_expression(section_name, section_name_len);
1036 // SIZEOF function
1038 class Sizeof_expression : public Section_expression
1040 public:
1041 Sizeof_expression(const char* section_name, size_t section_name_len)
1042 : Section_expression(section_name, section_name_len)
1045 protected:
1046 uint64_t
1047 value_from_output_section(const Expression_eval_info*,
1048 Output_section* os)
1050 // We can not use data_size here, as the size of the section may
1051 // not have been finalized. Instead we get whatever the current
1052 // size is. This will work correctly for backward references in
1053 // linker scripts.
1054 return os->current_data_size();
1057 uint64_t
1058 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1059 uint64_t size)
1060 { return size; }
1062 const char*
1063 function_name() const
1064 { return "SIZEOF"; }
1067 extern "C" Expression*
1068 script_exp_function_sizeof(const char* section_name, size_t section_name_len)
1070 return new Sizeof_expression(section_name, section_name_len);
1073 // SIZEOF_HEADERS.
1075 class Sizeof_headers_expression : public Expression
1077 public:
1078 Sizeof_headers_expression()
1081 uint64_t
1082 value(const Expression_eval_info*);
1084 void
1085 print(FILE* f) const
1086 { fprintf(f, "SIZEOF_HEADERS"); }
1089 uint64_t
1090 Sizeof_headers_expression::value(const Expression_eval_info* eei)
1092 unsigned int ehdr_size;
1093 unsigned int phdr_size;
1094 if (parameters->target().get_size() == 32)
1096 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1097 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1099 else if (parameters->target().get_size() == 64)
1101 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
1102 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
1104 else
1105 gold_unreachable();
1107 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
1110 extern "C" Expression*
1111 script_exp_function_sizeof_headers()
1113 return new Sizeof_headers_expression();
1116 // In the GNU linker SEGMENT_START basically returns the value for
1117 // -Ttext, -Tdata, or -Tbss. We could implement this by copying the
1118 // values from General_options to Parameters. But I doubt that
1119 // anybody actually uses it. The point of it for the GNU linker was
1120 // because -Ttext set the address of the .text section rather than the
1121 // text segment. In gold -Ttext sets the text segment address anyhow.
1123 extern "C" Expression*
1124 script_exp_function_segment_start(const char*, size_t, Expression*)
1126 gold_fatal(_("SEGMENT_START not implemented"));
1129 // Functions for memory regions. These can not be implemented unless
1130 // and until we implement memory regions.
1132 extern "C" Expression*
1133 script_exp_function_origin(const char*, size_t)
1135 gold_fatal(_("ORIGIN not implemented"));
1138 extern "C" Expression*
1139 script_exp_function_length(const char*, size_t)
1141 gold_fatal(_("LENGTH not implemented"));
1144 } // End namespace gold.