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