* README: Remove claim that MEMORY is not supported.
[external/binutils.git] / gold / expression.cc
1 // expression.cc -- expressions in linker scripts for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <string>
26
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"
34
35 namespace gold
36 {
37
38 // This file holds the code which handles linker expressions.
39
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.
46
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.
50
51 struct Expression::Expression_eval_info
52 {
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;
69   // Pointer to where the alignment of the result should be stored.
70   uint64_t* result_alignment_pointer;
71 };
72
73 // Evaluate an expression.
74
75 uint64_t
76 Expression::eval(const Symbol_table* symtab, const Layout* layout,
77                  bool check_assertions)
78 {
79   Output_section* dummy;
80   return this->eval_maybe_dot(symtab, layout, check_assertions,
81                               false, 0, NULL, &dummy, NULL);
82 }
83
84 // Evaluate an expression which may refer to the dot symbol.
85
86 uint64_t
87 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
88                           bool check_assertions, uint64_t dot_value,
89                           Output_section* dot_section,
90                           Output_section** result_section_pointer,
91                           uint64_t* result_alignment_pointer)
92 {
93   return this->eval_maybe_dot(symtab, layout, check_assertions, true,
94                               dot_value, dot_section, result_section_pointer,
95                               result_alignment_pointer);
96 }
97
98 // Evaluate an expression which may or may not refer to the dot
99 // symbol.
100
101 uint64_t
102 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
103                            bool check_assertions, bool is_dot_available,
104                            uint64_t dot_value, Output_section* dot_section,
105                            Output_section** result_section_pointer,
106                            uint64_t* result_alignment_pointer)
107 {
108   Expression_eval_info eei;
109   eei.symtab = symtab;
110   eei.layout = layout;
111   eei.check_assertions = check_assertions;
112   eei.is_dot_available = is_dot_available;
113   eei.dot_value = dot_value;
114   eei.dot_section = dot_section;
115
116   // We assume the value is absolute, and only set this to a section
117   // if we find a section relative reference.
118   *result_section_pointer = NULL;
119   eei.result_section_pointer = result_section_pointer;
120
121   eei.result_alignment_pointer = result_alignment_pointer;
122
123   return this->value(&eei);
124 }
125
126 // A number.
127
128 class Integer_expression : public Expression
129 {
130  public:
131   Integer_expression(uint64_t val)
132     : val_(val)
133   { }
134
135   uint64_t
136   value(const Expression_eval_info*)
137   { return this->val_; }
138
139   void
140   print(FILE* f) const
141   { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
142
143  private:
144   uint64_t val_;
145 };
146
147 extern "C" Expression*
148 script_exp_integer(uint64_t val)
149 {
150   return new Integer_expression(val);
151 }
152
153 // An expression whose value is the value of a symbol.
154
155 class Symbol_expression : public Expression
156 {
157  public:
158   Symbol_expression(const char* name, size_t length)
159     : name_(name, length)
160   { }
161
162   uint64_t
163   value(const Expression_eval_info*);
164
165   void
166   print(FILE* f) const
167   { fprintf(f, "%s", this->name_.c_str()); }
168
169  private:
170   std::string name_;
171 };
172
173 uint64_t
174 Symbol_expression::value(const Expression_eval_info* eei)
175 {
176   Symbol* sym = eei->symtab->lookup(this->name_.c_str());
177   if (sym == NULL || !sym->is_defined())
178     {
179       gold_error(_("undefined symbol '%s' referenced in expression"),
180                  this->name_.c_str());
181       return 0;
182     }
183
184   *eei->result_section_pointer = sym->output_section();
185
186   if (parameters->target().get_size() == 32)
187     return eei->symtab->get_sized_symbol<32>(sym)->value();
188   else if (parameters->target().get_size() == 64)
189     return eei->symtab->get_sized_symbol<64>(sym)->value();
190   else
191     gold_unreachable();
192 }
193
194 // An expression whose value is the value of the special symbol ".".
195 // This is only valid within a SECTIONS clause.
196
197 class Dot_expression : public Expression
198 {
199  public:
200   Dot_expression()
201   { }
202
203   uint64_t
204   value(const Expression_eval_info*);
205
206   void
207   print(FILE* f) const
208   { fprintf(f, "."); }
209 };
210
211 uint64_t
212 Dot_expression::value(const Expression_eval_info* eei)
213 {
214   if (!eei->is_dot_available)
215     {
216       gold_error(_("invalid reference to dot symbol outside of "
217                    "SECTIONS clause"));
218       return 0;
219     }
220   *eei->result_section_pointer = eei->dot_section;
221   return eei->dot_value;
222 }
223
224 // A string.  This is either the name of a symbol, or ".".
225
226 extern "C" Expression*
227 script_exp_string(const char* name, size_t length)
228 {
229   if (length == 1 && name[0] == '.')
230     return new Dot_expression();
231   else
232     return new Symbol_expression(name, length);
233 }
234
235 // A unary expression.
236
237 class Unary_expression : public Expression
238 {
239  public:
240   Unary_expression(Expression* arg)
241     : arg_(arg)
242   { }
243
244   ~Unary_expression()
245   { delete this->arg_; }
246
247  protected:
248   uint64_t
249   arg_value(const Expression_eval_info* eei,
250             Output_section** arg_section_pointer) const
251   {
252     return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
253                                       eei->check_assertions,
254                                       eei->is_dot_available,
255                                       eei->dot_value,
256                                       eei->dot_section,
257                                       arg_section_pointer,
258                                       eei->result_alignment_pointer);
259   }
260
261   void
262   arg_print(FILE* f) const
263   { this->arg_->print(f); }
264
265  private:
266   Expression* arg_;
267 };
268
269 // Handle unary operators.  We use a preprocessor macro as a hack to
270 // capture the C operator.
271
272 #define UNARY_EXPRESSION(NAME, OPERATOR)                                \
273   class Unary_ ## NAME : public Unary_expression                        \
274   {                                                                     \
275   public:                                                               \
276     Unary_ ## NAME(Expression* arg)                                     \
277       : Unary_expression(arg)                                           \
278     { }                                                                 \
279                                                                         \
280     uint64_t                                                            \
281     value(const Expression_eval_info* eei)                              \
282     {                                                                   \
283       Output_section* arg_section;                                      \
284       uint64_t ret = OPERATOR this->arg_value(eei, &arg_section);       \
285       if (arg_section != NULL && parameters->options().relocatable())   \
286         gold_warning(_("unary " #NAME " applied to section "            \
287                        "relative value"));                              \
288       return ret;                                                       \
289     }                                                                   \
290                                                                         \
291     void                                                                \
292     print(FILE* f) const                                                \
293     {                                                                   \
294       fprintf(f, "(%s ", #OPERATOR);                                    \
295       this->arg_print(f);                                               \
296       fprintf(f, ")");                                                  \
297     }                                                                   \
298   };                                                                    \
299                                                                         \
300   extern "C" Expression*                                                \
301   script_exp_unary_ ## NAME(Expression* arg)                            \
302   {                                                                     \
303       return new Unary_ ## NAME(arg);                                   \
304   }
305
306 UNARY_EXPRESSION(minus, -)
307 UNARY_EXPRESSION(logical_not, !)
308 UNARY_EXPRESSION(bitwise_not, ~)
309
310 // A binary expression.
311
312 class Binary_expression : public Expression
313 {
314  public:
315   Binary_expression(Expression* left, Expression* right)
316     : left_(left), right_(right)
317   { }
318
319   ~Binary_expression()
320   {
321     delete this->left_;
322     delete this->right_;
323   }
324
325  protected:
326   uint64_t
327   left_value(const Expression_eval_info* eei,
328              Output_section** section_pointer,
329              uint64_t* alignment_pointer) const
330   {
331     return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
332                                        eei->check_assertions,
333                                        eei->is_dot_available,
334                                        eei->dot_value,
335                                        eei->dot_section,
336                                        section_pointer,
337                                        alignment_pointer);
338   }
339
340   uint64_t
341   right_value(const Expression_eval_info* eei,
342               Output_section** section_pointer,
343               uint64_t* alignment_pointer) const
344   {
345     return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
346                                         eei->check_assertions,
347                                         eei->is_dot_available,
348                                         eei->dot_value,
349                                         eei->dot_section,
350                                         section_pointer,
351                                         alignment_pointer);
352   }
353
354   void
355   left_print(FILE* f) const
356   { this->left_->print(f); }
357
358   void
359   right_print(FILE* f) const
360   { this->right_->print(f); }
361
362   // This is a call to function FUNCTION_NAME.  Print it.  This is for
363   // debugging.
364   void
365   print_function(FILE* f, const char* function_name) const
366   {
367     fprintf(f, "%s(", function_name);
368     this->left_print(f);
369     fprintf(f, ", ");
370     this->right_print(f);
371     fprintf(f, ")");
372   }
373
374  private:
375   Expression* left_;
376   Expression* right_;
377 };
378
379 // Handle binary operators.  We use a preprocessor macro as a hack to
380 // capture the C operator.  KEEP_LEFT means that if the left operand
381 // is section relative and the right operand is not, the result uses
382 // the same section as the left operand.  KEEP_RIGHT is the same with
383 // left and right swapped.  IS_DIV means that we need to give an error
384 // if the right operand is zero.  WARN means that we should warn if
385 // used on section relative values in a relocatable link.  We always
386 // warn if used on values in different sections in a relocatable link.
387
388 #define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
389   class Binary_ ## NAME : public Binary_expression                      \
390   {                                                                     \
391   public:                                                               \
392     Binary_ ## NAME(Expression* left, Expression* right)                \
393       : Binary_expression(left, right)                                  \
394     { }                                                                 \
395                                                                         \
396     uint64_t                                                            \
397     value(const Expression_eval_info* eei)                              \
398     {                                                                   \
399       Output_section* left_section;                                     \
400       uint64_t left_alignment;                                          \
401       uint64_t left = this->left_value(eei, &left_section,              \
402                                        &left_alignment);                \
403       Output_section* right_section;                                    \
404       uint64_t right_alignment;                                         \
405       uint64_t right = this->right_value(eei, &right_section,           \
406                                          &right_alignment);             \
407       if (KEEP_RIGHT && left_section == NULL && right_section != NULL)  \
408         {                                                               \
409           *eei->result_section_pointer = right_section;                 \
410           if (eei->result_alignment_pointer != NULL)                    \
411             *eei->result_alignment_pointer = right_alignment;           \
412         }                                                               \
413       else if (KEEP_LEFT                                                \
414                && left_section != NULL                                  \
415                && right_section == NULL)                                \
416         {                                                               \
417           *eei->result_section_pointer = left_section;                  \
418           if (eei->result_alignment_pointer != NULL)                    \
419             *eei->result_alignment_pointer = right_alignment;           \
420         }                                                               \
421       else if ((WARN || left_section != right_section)                  \
422                && (left_section != NULL || right_section != NULL)       \
423                && parameters->options().relocatable())                  \
424         gold_warning(_("binary " #NAME " applied to section "           \
425                        "relative value"));                              \
426       if (IS_DIV && right == 0)                                         \
427         {                                                               \
428           gold_error(_(#NAME " by zero"));                              \
429           return 0;                                                     \
430         }                                                               \
431       return left OPERATOR right;                                       \
432     }                                                                   \
433                                                                         \
434     void                                                                \
435     print(FILE* f) const                                                \
436     {                                                                   \
437       fprintf(f, "(");                                                  \
438       this->left_print(f);                                              \
439       fprintf(f, " %s ", #OPERATOR);                                    \
440       this->right_print(f);                                             \
441       fprintf(f, ")");                                                  \
442     }                                                                   \
443   };                                                                    \
444                                                                         \
445   extern "C" Expression*                                                \
446   script_exp_binary_ ## NAME(Expression* left, Expression* right)       \
447   {                                                                     \
448     return new Binary_ ## NAME(left, right);                            \
449   }
450
451 BINARY_EXPRESSION(mult, *, false, false, false, true)
452 BINARY_EXPRESSION(div, /, false, false, true, true)
453 BINARY_EXPRESSION(mod, %, false, false, true, true)
454 BINARY_EXPRESSION(add, +, true, true, false, true)
455 BINARY_EXPRESSION(sub, -, true, false, false, false)
456 BINARY_EXPRESSION(lshift, <<, false, false, false, true)
457 BINARY_EXPRESSION(rshift, >>, false, false, false, true)
458 BINARY_EXPRESSION(eq, ==, false, false, false, false)
459 BINARY_EXPRESSION(ne, !=, false, false, false, false)
460 BINARY_EXPRESSION(le, <=, false, false, false, false)
461 BINARY_EXPRESSION(ge, >=, false, false, false, false)
462 BINARY_EXPRESSION(lt, <, false, false, false, false)
463 BINARY_EXPRESSION(gt, >, false, false, false, false)
464 BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
465 BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
466 BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
467 BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
468 BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
469
470 // A trinary expression.
471
472 class Trinary_expression : public Expression
473 {
474  public:
475   Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
476     : arg1_(arg1), arg2_(arg2), arg3_(arg3)
477   { }
478
479   ~Trinary_expression()
480   {
481     delete this->arg1_;
482     delete this->arg2_;
483     delete this->arg3_;
484   }
485
486  protected:
487   uint64_t
488   arg1_value(const Expression_eval_info* eei,
489              Output_section** section_pointer) const
490   {
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,
497                                        NULL);
498   }
499
500   uint64_t
501   arg2_value(const Expression_eval_info* eei,
502              Output_section** section_pointer,
503              uint64_t* alignment_pointer) const
504   {
505     return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
506                                        eei->check_assertions,
507                                        eei->is_dot_available,
508                                        eei->dot_value,
509                                        eei->dot_section,
510                                        section_pointer,
511                                        alignment_pointer);
512   }
513
514   uint64_t
515   arg3_value(const Expression_eval_info* eei,
516              Output_section** section_pointer,
517              uint64_t* alignment_pointer) const
518   {
519     return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
520                                        eei->check_assertions,
521                                        eei->is_dot_available,
522                                        eei->dot_value,
523                                        eei->dot_section,
524                                        section_pointer,
525                                        alignment_pointer);
526   }
527
528   void
529   arg1_print(FILE* f) const
530   { this->arg1_->print(f); }
531
532   void
533   arg2_print(FILE* f) const
534   { this->arg2_->print(f); }
535
536   void
537   arg3_print(FILE* f) const
538   { this->arg3_->print(f); }
539
540  private:
541   Expression* arg1_;
542   Expression* arg2_;
543   Expression* arg3_;
544 };
545
546 // The conditional operator.
547
548 class Trinary_cond : public Trinary_expression
549 {
550  public:
551   Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
552     : Trinary_expression(arg1, arg2, arg3)
553   { }
554
555   uint64_t
556   value(const Expression_eval_info* eei)
557   {
558     Output_section* arg1_section;
559     uint64_t arg1 = this->arg1_value(eei, &arg1_section);
560     return (arg1
561             ? this->arg2_value(eei, eei->result_section_pointer,
562                                eei->result_alignment_pointer)
563             : this->arg3_value(eei, eei->result_section_pointer,
564                                eei->result_alignment_pointer));
565   }
566
567   void
568   print(FILE* f) const
569   {
570     fprintf(f, "(");
571     this->arg1_print(f);
572     fprintf(f, " ? ");
573     this->arg2_print(f);
574     fprintf(f, " : ");
575     this->arg3_print(f);
576     fprintf(f, ")");
577   }
578 };
579
580 extern "C" Expression*
581 script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
582 {
583   return new Trinary_cond(arg1, arg2, arg3);
584 }
585
586 // Max function.
587
588 class Max_expression : public Binary_expression
589 {
590  public:
591   Max_expression(Expression* left, Expression* right)
592     : Binary_expression(left, right)
593   { }
594
595   uint64_t
596   value(const Expression_eval_info* eei)
597   {
598     Output_section* left_section;
599     uint64_t left_alignment;
600     uint64_t left = this->left_value(eei, &left_section, &left_alignment);
601     Output_section* right_section;
602     uint64_t right_alignment;
603     uint64_t right = this->right_value(eei, &right_section, &right_alignment);
604     if (left_section == right_section)
605       *eei->result_section_pointer = left_section;
606     else if ((left_section != NULL || right_section != NULL)
607              && parameters->options().relocatable())
608       gold_warning(_("max applied to section relative value"));
609     if (eei->result_alignment_pointer != NULL)
610       {
611         uint64_t ra = *eei->result_alignment_pointer;
612         if (left > right)
613           ra = std::max(ra, left_alignment);
614         else if (right > left)
615           ra = std::max(ra, right_alignment);
616         else
617           ra = std::max(ra, std::max(left_alignment, right_alignment));
618         *eei->result_alignment_pointer = ra;
619       }
620     return std::max(left, right);
621   }
622
623   void
624   print(FILE* f) const
625   { this->print_function(f, "MAX"); }
626 };
627
628 extern "C" Expression*
629 script_exp_function_max(Expression* left, Expression* right)
630 {
631   return new Max_expression(left, right);
632 }
633
634 // Min function.
635
636 class Min_expression : public Binary_expression
637 {
638  public:
639   Min_expression(Expression* left, Expression* right)
640     : Binary_expression(left, right)
641   { }
642
643   uint64_t
644   value(const Expression_eval_info* eei)
645   {
646     Output_section* left_section;
647     uint64_t left_alignment;
648     uint64_t left = this->left_value(eei, &left_section, &left_alignment);
649     Output_section* right_section;
650     uint64_t right_alignment;
651     uint64_t right = this->right_value(eei, &right_section, &right_alignment);
652     if (left_section == right_section)
653       *eei->result_section_pointer = left_section;
654     else if ((left_section != NULL || right_section != NULL)
655              && parameters->options().relocatable())
656       gold_warning(_("min applied to section relative value"));
657     if (eei->result_alignment_pointer != NULL)
658       {
659         uint64_t ra = *eei->result_alignment_pointer;
660         if (left < right)
661           ra = std::max(ra, left_alignment);
662         else if (right < left)
663           ra = std::max(ra, right_alignment);
664         else
665           ra = std::max(ra, std::max(left_alignment, right_alignment));
666         *eei->result_alignment_pointer = ra;
667       }
668     return std::min(left, right);
669   }
670
671   void
672   print(FILE* f) const
673   { this->print_function(f, "MIN"); }
674 };
675
676 extern "C" Expression*
677 script_exp_function_min(Expression* left, Expression* right)
678 {
679   return new Min_expression(left, right);
680 }
681
682 // Class Section_expression.  This is a parent class used for
683 // functions which take the name of an output section.
684
685 class Section_expression : public Expression
686 {
687  public:
688   Section_expression(const char* section_name, size_t section_name_len)
689     : section_name_(section_name, section_name_len)
690   { }
691
692   uint64_t
693   value(const Expression_eval_info*);
694
695   void
696   print(FILE* f) const
697   { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
698
699  protected:
700   // The child class must implement this.
701   virtual uint64_t
702   value_from_output_section(const Expression_eval_info*,
703                             Output_section*) = 0;
704
705   // The child class must implement this.
706   virtual uint64_t
707   value_from_script_output_section(uint64_t address, uint64_t load_address,
708                                    uint64_t addralign, uint64_t size) = 0;
709
710   // The child class must implement this.
711   virtual const char*
712   function_name() const = 0;
713
714  private:
715   std::string section_name_;
716 };
717
718 uint64_t
719 Section_expression::value(const Expression_eval_info* eei)
720 {
721   const char* section_name = this->section_name_.c_str();
722   Output_section* os = eei->layout->find_output_section(section_name);
723   if (os != NULL)
724     return this->value_from_output_section(eei, os);
725
726   uint64_t address;
727   uint64_t load_address;
728   uint64_t addralign;
729   uint64_t size;
730   const Script_options* ss = eei->layout->script_options();
731   if (ss->saw_sections_clause())
732     {
733       if (ss->script_sections()->get_output_section_info(section_name,
734                                                          &address,
735                                                          &load_address,
736                                                          &addralign,
737                                                          &size))
738         return this->value_from_script_output_section(address, load_address,
739                                                       addralign, size);
740     }
741
742   gold_error("%s called on nonexistent output section '%s'",
743              this->function_name(), section_name);
744   return 0;
745 }
746
747 // ABSOLUTE function.
748
749 class Absolute_expression : public Unary_expression
750 {
751  public:
752   Absolute_expression(Expression* arg)
753     : Unary_expression(arg)
754   { }
755
756   uint64_t
757   value(const Expression_eval_info* eei)
758   {
759     Output_section* dummy;
760     uint64_t ret = this->arg_value(eei, &dummy);
761     // Force the value to be absolute.
762     *eei->result_section_pointer = NULL;
763     return ret;
764   }
765
766   void
767   print(FILE* f) const
768   {
769     fprintf(f, "ABSOLUTE(");
770     this->arg_print(f);
771     fprintf(f, ")");
772   }
773 };
774
775 extern "C" Expression*
776 script_exp_function_absolute(Expression* arg)
777 {
778   return new Absolute_expression(arg);
779 }
780
781 // ALIGN function.
782
783 class Align_expression : public Binary_expression
784 {
785  public:
786   Align_expression(Expression* left, Expression* right)
787     : Binary_expression(left, right)
788   { }
789
790   uint64_t
791   value(const Expression_eval_info* eei)
792   {
793     Output_section* align_section;
794     uint64_t align = this->right_value(eei, &align_section, NULL);
795     if (align_section != NULL
796         && parameters->options().relocatable())
797       gold_warning(_("aligning to section relative value"));
798
799     if (eei->result_alignment_pointer != NULL
800         && align > *eei->result_alignment_pointer)
801       {
802         uint64_t a = align;
803         while ((a & (a - 1)) != 0)
804           a &= a - 1;
805         *eei->result_alignment_pointer = a;
806       }
807
808     uint64_t value = this->left_value(eei, eei->result_section_pointer, NULL);
809     if (align <= 1)
810       return value;
811     return ((value + align - 1) / align) * align;
812   }
813
814   void
815   print(FILE* f) const
816   { this->print_function(f, "ALIGN"); }
817 };
818
819 extern "C" Expression*
820 script_exp_function_align(Expression* left, Expression* right)
821 {
822   return new Align_expression(left, right);
823 }
824
825 // ASSERT function.
826
827 class Assert_expression : public Unary_expression
828 {
829  public:
830   Assert_expression(Expression* arg, const char* message, size_t length)
831     : Unary_expression(arg), message_(message, length)
832   { }
833
834   uint64_t
835   value(const Expression_eval_info* eei)
836   {
837     uint64_t value = this->arg_value(eei, eei->result_section_pointer);
838     if (!value && eei->check_assertions)
839       gold_error("%s", this->message_.c_str());
840     return value;
841   }
842
843   void
844   print(FILE* f) const
845   {
846     fprintf(f, "ASSERT(");
847     this->arg_print(f);
848     fprintf(f, ", %s)", this->message_.c_str());
849   }
850
851  private:
852   std::string message_;
853 };
854
855 extern "C" Expression*
856 script_exp_function_assert(Expression* expr, const char* message,
857                            size_t length)
858 {
859   return new Assert_expression(expr, message, length);
860 }
861
862 // ADDR function.
863
864 class Addr_expression : public Section_expression
865 {
866  public:
867   Addr_expression(const char* section_name, size_t section_name_len)
868     : Section_expression(section_name, section_name_len)
869   { }
870
871  protected:
872   uint64_t
873   value_from_output_section(const Expression_eval_info* eei,
874                             Output_section* os)
875   {
876     *eei->result_section_pointer = os;
877     return os->address();
878   }
879
880   uint64_t
881   value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
882                                    uint64_t)
883   { return address; }
884
885   const char*
886   function_name() const
887   { return "ADDR"; }
888 };
889
890 extern "C" Expression*
891 script_exp_function_addr(const char* section_name, size_t section_name_len)
892 {
893   return new Addr_expression(section_name, section_name_len);
894 }
895
896 // ALIGNOF.
897
898 class Alignof_expression : public Section_expression
899 {
900  public:
901   Alignof_expression(const char* section_name, size_t section_name_len)
902     : Section_expression(section_name, section_name_len)
903   { }
904
905  protected:
906   uint64_t
907   value_from_output_section(const Expression_eval_info*,
908                             Output_section* os)
909   { return os->addralign(); }
910
911   uint64_t
912   value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
913                                    uint64_t)
914   { return addralign; }
915
916   const char*
917   function_name() const
918   { return "ALIGNOF"; }
919 };
920
921 extern "C" Expression*
922 script_exp_function_alignof(const char* section_name, size_t section_name_len)
923 {
924   return new Alignof_expression(section_name, section_name_len);
925 }
926
927 // CONSTANT.  It would be nice if we could simply evaluate this
928 // immediately and return an Integer_expression, but unfortunately we
929 // don't know the target.
930
931 class Constant_expression : public Expression
932 {
933  public:
934   Constant_expression(const char* name, size_t length);
935
936   uint64_t
937   value(const Expression_eval_info*);
938
939   void
940   print(FILE* f) const;
941
942  private:
943   enum Constant_function
944   {
945     CONSTANT_MAXPAGESIZE,
946     CONSTANT_COMMONPAGESIZE
947   };
948
949   Constant_function function_;
950 };
951
952 Constant_expression::Constant_expression(const char* name, size_t length)
953 {
954   if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
955     this->function_ = CONSTANT_MAXPAGESIZE;
956   else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
957     this->function_ = CONSTANT_COMMONPAGESIZE;
958   else
959     {
960       std::string s(name, length);
961       gold_error(_("unknown constant %s"), s.c_str());
962       this->function_ = CONSTANT_MAXPAGESIZE;
963     }
964 }
965
966 uint64_t
967 Constant_expression::value(const Expression_eval_info*)
968 {
969   switch (this->function_)
970     {
971     case CONSTANT_MAXPAGESIZE:
972       return parameters->target().abi_pagesize();
973     case CONSTANT_COMMONPAGESIZE:
974       return parameters->target().common_pagesize();
975     default:
976       gold_unreachable();
977     }
978 }
979
980 void
981 Constant_expression::print(FILE* f) const
982 {
983   const char* name;
984   switch (this->function_)
985     {
986     case CONSTANT_MAXPAGESIZE:
987       name = "MAXPAGESIZE";
988       break;
989     case CONSTANT_COMMONPAGESIZE:
990       name = "COMMONPAGESIZE";
991       break;
992     default:
993       gold_unreachable();
994     }
995   fprintf(f, "CONSTANT(%s)", name);
996 }
997   
998 extern "C" Expression*
999 script_exp_function_constant(const char* name, size_t length)
1000 {
1001   return new Constant_expression(name, length);
1002 }
1003
1004 // DATA_SEGMENT_ALIGN.  FIXME: we don't implement this; we always fall
1005 // back to the general case.
1006
1007 extern "C" Expression*
1008 script_exp_function_data_segment_align(Expression* left, Expression*)
1009 {
1010   Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
1011   Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
1012   Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
1013                                                  e2);
1014   return script_exp_binary_add(e1, e3);
1015 }
1016
1017 // DATA_SEGMENT_RELRO.  FIXME: This is not implemented.
1018
1019 extern "C" Expression*
1020 script_exp_function_data_segment_relro_end(Expression*, Expression* right)
1021 {
1022   return right;
1023 }
1024
1025 // DATA_SEGMENT_END.  FIXME: This is not implemented.
1026
1027 extern "C" Expression*
1028 script_exp_function_data_segment_end(Expression* val)
1029 {
1030   return val;
1031 }
1032
1033 // DEFINED function.
1034
1035 class Defined_expression : public Expression
1036 {
1037  public:
1038   Defined_expression(const char* symbol_name, size_t symbol_name_len)
1039     : symbol_name_(symbol_name, symbol_name_len)
1040   { }
1041
1042   uint64_t
1043   value(const Expression_eval_info* eei)
1044   {
1045     Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
1046     return sym != NULL && sym->is_defined();
1047   }
1048
1049   void
1050   print(FILE* f) const
1051   { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
1052
1053  private:
1054   std::string symbol_name_;
1055 };
1056
1057 extern "C" Expression*
1058 script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
1059 {
1060   return new Defined_expression(symbol_name, symbol_name_len);
1061 }
1062
1063 // LOADADDR function
1064
1065 class Loadaddr_expression : public Section_expression
1066 {
1067  public:
1068   Loadaddr_expression(const char* section_name, size_t section_name_len)
1069     : Section_expression(section_name, section_name_len)
1070   { }
1071
1072  protected:
1073   uint64_t
1074   value_from_output_section(const Expression_eval_info* eei,
1075                             Output_section* os)
1076   {
1077     if (os->has_load_address())
1078       return os->load_address();
1079     else
1080       {
1081         *eei->result_section_pointer = os;
1082         return os->address();
1083       }
1084   }
1085
1086   uint64_t
1087   value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1088                                    uint64_t)
1089   { return load_address; }
1090
1091   const char*
1092   function_name() const
1093   { return "LOADADDR"; }
1094 };
1095
1096 extern "C" Expression*
1097 script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
1098 {
1099   return new Loadaddr_expression(section_name, section_name_len);
1100 }
1101
1102 // SIZEOF function
1103
1104 class Sizeof_expression : public Section_expression
1105 {
1106  public:
1107   Sizeof_expression(const char* section_name, size_t section_name_len)
1108     : Section_expression(section_name, section_name_len)
1109   { }
1110
1111  protected:
1112   uint64_t
1113   value_from_output_section(const Expression_eval_info*,
1114                             Output_section* os)
1115   {
1116     // We can not use data_size here, as the size of the section may
1117     // not have been finalized.  Instead we get whatever the current
1118     // size is.  This will work correctly for backward references in
1119     // linker scripts.
1120     return os->current_data_size();
1121   }
1122
1123   uint64_t
1124   value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1125                                    uint64_t size)
1126   { return size; }
1127
1128   const char*
1129   function_name() const
1130   { return "SIZEOF"; }
1131 };
1132
1133 extern "C" Expression*
1134 script_exp_function_sizeof(const char* section_name, size_t section_name_len)
1135 {
1136   return new Sizeof_expression(section_name, section_name_len);
1137 }
1138
1139 // SIZEOF_HEADERS.
1140
1141 class Sizeof_headers_expression : public Expression
1142 {
1143  public:
1144   Sizeof_headers_expression()
1145   { }
1146
1147   uint64_t
1148   value(const Expression_eval_info*);
1149
1150   void
1151   print(FILE* f) const
1152   { fprintf(f, "SIZEOF_HEADERS"); }
1153 };
1154
1155 uint64_t
1156 Sizeof_headers_expression::value(const Expression_eval_info* eei)
1157 {
1158   unsigned int ehdr_size;
1159   unsigned int phdr_size;
1160   if (parameters->target().get_size() == 32)
1161     {
1162       ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1163       phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1164     }
1165   else if (parameters->target().get_size() == 64)
1166     {
1167       ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
1168       phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
1169     }
1170   else
1171     gold_unreachable();
1172
1173   return ehdr_size + phdr_size * eei->layout->expected_segment_count();
1174 }
1175
1176 extern "C" Expression*
1177 script_exp_function_sizeof_headers()
1178 {
1179   return new Sizeof_headers_expression();
1180 }
1181
1182 // SEGMENT_START.
1183
1184 class Segment_start_expression : public Unary_expression
1185 {
1186  public:
1187   Segment_start_expression(const char* segment_name, size_t segment_name_len,
1188                            Expression* default_value)
1189     : Unary_expression(default_value),
1190       segment_name_(segment_name, segment_name_len)
1191   { }
1192
1193   uint64_t
1194   value(const Expression_eval_info*);
1195
1196   void
1197   print(FILE* f) const
1198   {
1199     fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
1200     this->arg_print(f);
1201     fprintf(f, ")");
1202   }
1203
1204  private:
1205   std::string segment_name_;
1206 };
1207
1208 uint64_t
1209 Segment_start_expression::value(const Expression_eval_info* eei)
1210 {
1211   // Check for command line overrides.
1212   if (parameters->options().user_set_Ttext()
1213       && this->segment_name_ == ".text")
1214     return parameters->options().Ttext();
1215   else if (parameters->options().user_set_Tdata()
1216            && this->segment_name_ == ".data")
1217     return parameters->options().Tdata();
1218   else if (parameters->options().user_set_Tbss()
1219            && this->segment_name_ == ".bss")
1220     return parameters->options().Tbss();
1221   else
1222     {
1223       Output_section* dummy;
1224       uint64_t ret = this->arg_value(eei, &dummy);
1225       // Force the value to be absolute.
1226       *eei->result_section_pointer = NULL;
1227       return ret;
1228     }
1229 }
1230
1231 extern "C" Expression*
1232 script_exp_function_segment_start(const char* segment_name,
1233                                   size_t segment_name_len,
1234                                   Expression* default_value)
1235 {
1236   return new Segment_start_expression(segment_name, segment_name_len,
1237                                       default_value);
1238 }
1239
1240 } // End namespace gold.