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