Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / expression.cc
CommitLineData
e5756efb
ILT
1// expression.cc -- expressions in linker scripts for gold
2
219d1afa 3// Copyright (C) 2006-2018 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;
e051745c
CC
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;
1757d35c
CC
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;
e5756efb
ILT
81};
82
83// Evaluate an expression.
84
85uint64_t
919ed24c
ILT
86Expression::eval(const Symbol_table* symtab, const Layout* layout,
87 bool check_assertions)
a445fddf 88{
e051745c 89 return this->eval_maybe_dot(symtab, layout, check_assertions, false, 0,
1757d35c 90 NULL, NULL, NULL, NULL, NULL, NULL, false, NULL);
a445fddf
ILT
91}
92
93// Evaluate an expression which may refer to the dot symbol.
94
95uint64_t
96Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
919ed24c
ILT
97 bool check_assertions, uint64_t dot_value,
98 Output_section* dot_section,
f6973bdc 99 Output_section** result_section_pointer,
286adcf4
CC
100 uint64_t* result_alignment_pointer,
101 bool is_section_dot_assignment)
a445fddf 102{
919ed24c 103 return this->eval_maybe_dot(symtab, layout, check_assertions, true,
f6973bdc 104 dot_value, dot_section, result_section_pointer,
e051745c 105 result_alignment_pointer, NULL, NULL, NULL,
1757d35c 106 is_section_dot_assignment, NULL);
a445fddf
ILT
107}
108
109// Evaluate an expression which may or may not refer to the dot
110// symbol.
111
112uint64_t
113Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
919ed24c
ILT
114 bool check_assertions, bool is_dot_available,
115 uint64_t dot_value, Output_section* dot_section,
f6973bdc 116 Output_section** result_section_pointer,
286adcf4 117 uint64_t* result_alignment_pointer,
e051745c
CC
118 elfcpp::STT* type_pointer,
119 elfcpp::STV* vis_pointer,
120 unsigned char* nonvis_pointer,
1757d35c
CC
121 bool is_section_dot_assignment,
122 bool* is_valid_pointer)
e5756efb
ILT
123{
124 Expression_eval_info eei;
125 eei.symtab = symtab;
126 eei.layout = layout;
919ed24c 127 eei.check_assertions = check_assertions;
a445fddf 128 eei.is_dot_available = is_dot_available;
a445fddf 129 eei.dot_value = dot_value;
77e65537 130 eei.dot_section = dot_section;
a445fddf 131
77e65537 132 // We assume the value is absolute, and only set this to a section
286adcf4 133 // if we find a section-relative reference.
bacff3ab
NC
134 if (result_section_pointer != NULL)
135 *result_section_pointer = NULL;
77e65537 136 eei.result_section_pointer = result_section_pointer;
a445fddf 137
e051745c
CC
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
f6973bdc
ILT
144 eei.result_alignment_pointer = result_alignment_pointer;
145
1757d35c
CC
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
286adcf4
CC
151 uint64_t val = this->value(&eei);
152
1757d35c
CC
153 if (is_valid_pointer != NULL)
154 *is_valid_pointer = is_valid;
155 else
156 gold_assert(is_valid);
157
286adcf4
CC
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;
e5756efb
ILT
167}
168
169// A number.
170
171class Integer_expression : public Expression
172{
173 public:
174 Integer_expression(uint64_t val)
175 : val_(val)
176 { }
177
178 uint64_t
179 value(const Expression_eval_info*)
180 { return this->val_; }
181
494e05f4
ILT
182 void
183 print(FILE* f) const
184 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
185
e5756efb
ILT
186 private:
187 uint64_t val_;
188};
189
190extern "C" Expression*
191script_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
198class Symbol_expression : public Expression
199{
200 public:
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
494e05f4
ILT
208 void
209 print(FILE* f) const
210 { fprintf(f, "%s", this->name_.c_str()); }
211
e5756efb
ILT
212 private:
213 std::string name_;
214};
215
216uint64_t
217Symbol_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
bacff3ab
NC
227 if (eei->result_section_pointer != NULL)
228 *eei->result_section_pointer = sym->output_section();
e051745c
CC
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();
a445fddf 235
8851ecca 236 if (parameters->target().get_size() == 32)
e5756efb 237 return eei->symtab->get_sized_symbol<32>(sym)->value();
8851ecca 238 else if (parameters->target().get_size() == 64)
e5756efb
ILT
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
247class Dot_expression : public Expression
248{
249 public:
250 Dot_expression()
251 { }
252
253 uint64_t
254 value(const Expression_eval_info*);
494e05f4
ILT
255
256 void
257 print(FILE* f) const
258 { fprintf(f, "."); }
e5756efb
ILT
259};
260
261uint64_t
a445fddf 262Dot_expression::value(const Expression_eval_info* eei)
e5756efb 263{
a445fddf
ILT
264 if (!eei->is_dot_available)
265 {
266 gold_error(_("invalid reference to dot symbol outside of "
267 "SECTIONS clause"));
268 return 0;
269 }
bacff3ab
NC
270 if (eei->result_section_pointer != NULL)
271 *eei->result_section_pointer = eei->dot_section;
a445fddf 272 return eei->dot_value;
e5756efb
ILT
273}
274
275// A string. This is either the name of a symbol, or ".".
276
277extern "C" Expression*
278script_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
288class Unary_expression : public Expression
289{
290 public:
291 Unary_expression(Expression* arg)
292 : arg_(arg)
293 { }
294
295 ~Unary_expression()
296 { delete this->arg_; }
297
298 protected:
299 uint64_t
77e65537
ILT
300 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,
919ed24c 304 eei->check_assertions,
77e65537
ILT
305 eei->is_dot_available,
306 eei->dot_value,
307 eei->dot_section,
f6973bdc 308 arg_section_pointer,
286adcf4 309 eei->result_alignment_pointer,
e051745c
CC
310 NULL,
311 NULL,
312 NULL,
1757d35c
CC
313 false,
314 eei->is_valid_pointer);
77e65537 315 }
e5756efb 316
494e05f4
ILT
317 void
318 arg_print(FILE* f) const
319 { this->arg_->print(f); }
320
e5756efb
ILT
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
77e65537
ILT
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); \
8851ecca 341 if (arg_section != NULL && parameters->options().relocatable()) \
77e65537
ILT
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); \
e5756efb
ILT
360 }
361
362UNARY_EXPRESSION(minus, -)
363UNARY_EXPRESSION(logical_not, !)
364UNARY_EXPRESSION(bitwise_not, ~)
365
366// A binary expression.
367
368class Binary_expression : public Expression
369{
370 public:
371 Binary_expression(Expression* left, Expression* right)
372 : left_(left), right_(right)
373 { }
374
375 ~Binary_expression()
376 {
377 delete this->left_;
378 delete this->right_;
379 }
380
381 protected:
382 uint64_t
77e65537 383 left_value(const Expression_eval_info* eei,
f6973bdc
ILT
384 Output_section** section_pointer,
385 uint64_t* alignment_pointer) const
77e65537
ILT
386 {
387 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
919ed24c 388 eei->check_assertions,
77e65537
ILT
389 eei->is_dot_available,
390 eei->dot_value,
391 eei->dot_section,
f6973bdc 392 section_pointer,
286adcf4 393 alignment_pointer,
e051745c
CC
394 NULL,
395 NULL,
396 NULL,
1757d35c
CC
397 false,
398 eei->is_valid_pointer);
77e65537 399 }
e5756efb
ILT
400
401 uint64_t
77e65537 402 right_value(const Expression_eval_info* eei,
f6973bdc
ILT
403 Output_section** section_pointer,
404 uint64_t* alignment_pointer) const
77e65537
ILT
405 {
406 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
919ed24c 407 eei->check_assertions,
77e65537
ILT
408 eei->is_dot_available,
409 eei->dot_value,
410 eei->dot_section,
f6973bdc 411 section_pointer,
286adcf4 412 alignment_pointer,
e051745c
CC
413 NULL,
414 NULL,
415 NULL,
1757d35c
CC
416 false,
417 eei->is_valid_pointer);
77e65537 418 }
e5756efb 419
494e05f4
ILT
420 void
421 left_print(FILE* f) const
422 { this->left_->print(f); }
423
424 void
425 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
ca09d69a 431 print_function(FILE* f, const char* function_name) const
494e05f4
ILT
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
e5756efb
ILT
440 private:
441 Expression* left_;
442 Expression* right_;
443};
444
445// Handle binary operators. We use a preprocessor macro as a hack to
77e65537
ILT
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) \
e5756efb
ILT
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 { \
77e65537 465 Output_section* left_section; \
0ad220c9 466 uint64_t left_alignment = 0; \
f6973bdc
ILT
467 uint64_t left = this->left_value(eei, &left_section, \
468 &left_alignment); \
77e65537 469 Output_section* right_section; \
0ad220c9 470 uint64_t right_alignment = 0; \
f6973bdc
ILT
471 uint64_t right = this->right_value(eei, &right_section, \
472 &right_alignment); \
77e65537 473 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
f6973bdc 474 { \
bacff3ab
NC
475 if (eei->result_section_pointer != NULL) \
476 *eei->result_section_pointer = right_section; \
0ad220c9
DK
477 if (eei->result_alignment_pointer != NULL \
478 && right_alignment > *eei->result_alignment_pointer) \
f6973bdc
ILT
479 *eei->result_alignment_pointer = right_alignment; \
480 } \
77e65537
ILT
481 else if (KEEP_LEFT \
482 && left_section != NULL \
483 && right_section == NULL) \
f6973bdc 484 { \
bacff3ab
NC
485 if (eei->result_section_pointer != NULL) \
486 *eei->result_section_pointer = left_section; \
0ad220c9
DK
487 if (eei->result_alignment_pointer != NULL \
488 && left_alignment > *eei->result_alignment_pointer) \
489 *eei->result_alignment_pointer = left_alignment; \
f6973bdc 490 } \
77e65537
ILT
491 else if ((WARN || left_section != right_section) \
492 && (left_section != NULL || right_section != NULL) \
8851ecca 493 && parameters->options().relocatable()) \
77e65537
ILT
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; \
494e05f4
ILT
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, ")"); \
e5756efb
ILT
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
77e65537
ILT
521BINARY_EXPRESSION(mult, *, false, false, false, true)
522BINARY_EXPRESSION(div, /, false, false, true, true)
523BINARY_EXPRESSION(mod, %, false, false, true, true)
524BINARY_EXPRESSION(add, +, true, true, false, true)
525BINARY_EXPRESSION(sub, -, true, false, false, false)
526BINARY_EXPRESSION(lshift, <<, false, false, false, true)
527BINARY_EXPRESSION(rshift, >>, false, false, false, true)
528BINARY_EXPRESSION(eq, ==, false, false, false, false)
529BINARY_EXPRESSION(ne, !=, false, false, false, false)
530BINARY_EXPRESSION(le, <=, false, false, false, false)
531BINARY_EXPRESSION(ge, >=, false, false, false, false)
532BINARY_EXPRESSION(lt, <, false, false, false, false)
533BINARY_EXPRESSION(gt, >, false, false, false, false)
534BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
535BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
536BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
537BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
538BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
e5756efb
ILT
539
540// A trinary expression.
541
542class Trinary_expression : public Expression
543{
544 public:
545 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
546 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
547 { }
548
549 ~Trinary_expression()
550 {
551 delete this->arg1_;
552 delete this->arg2_;
553 delete this->arg3_;
554 }
555
556 protected:
557 uint64_t
77e65537
ILT
558 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,
919ed24c 562 eei->check_assertions,
77e65537
ILT
563 eei->is_dot_available,
564 eei->dot_value,
565 eei->dot_section,
f6973bdc 566 section_pointer,
286adcf4 567 NULL,
e051745c
CC
568 NULL,
569 NULL,
570 NULL,
1757d35c
CC
571 false,
572 eei->is_valid_pointer);
77e65537 573 }
e5756efb
ILT
574
575 uint64_t
77e65537 576 arg2_value(const Expression_eval_info* eei,
f6973bdc
ILT
577 Output_section** section_pointer,
578 uint64_t* alignment_pointer) const
77e65537
ILT
579 {
580 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
919ed24c 581 eei->check_assertions,
77e65537
ILT
582 eei->is_dot_available,
583 eei->dot_value,
584 eei->dot_section,
f6973bdc 585 section_pointer,
286adcf4 586 alignment_pointer,
e051745c
CC
587 NULL,
588 NULL,
589 NULL,
1757d35c
CC
590 false,
591 eei->is_valid_pointer);
77e65537 592 }
e5756efb
ILT
593
594 uint64_t
77e65537 595 arg3_value(const Expression_eval_info* eei,
f6973bdc
ILT
596 Output_section** section_pointer,
597 uint64_t* alignment_pointer) const
77e65537
ILT
598 {
599 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
919ed24c 600 eei->check_assertions,
77e65537
ILT
601 eei->is_dot_available,
602 eei->dot_value,
603 eei->dot_section,
f6973bdc 604 section_pointer,
286adcf4 605 alignment_pointer,
e051745c
CC
606 NULL,
607 NULL,
608 NULL,
1757d35c
CC
609 false,
610 eei->is_valid_pointer);
77e65537 611 }
e5756efb 612
494e05f4
ILT
613 void
614 arg1_print(FILE* f) const
615 { this->arg1_->print(f); }
616
617 void
618 arg2_print(FILE* f) const
619 { this->arg2_->print(f); }
620
621 void
622 arg3_print(FILE* f) const
623 { this->arg3_->print(f); }
624
e5756efb
ILT
625 private:
626 Expression* arg1_;
627 Expression* arg2_;
628 Expression* arg3_;
629};
630
631// The conditional operator.
632
633class Trinary_cond : public Trinary_expression
634{
635 public:
636 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
637 : Trinary_expression(arg1, arg2, arg3)
638 { }
639
640 uint64_t
641 value(const Expression_eval_info* eei)
642 {
77e65537
ILT
643 Output_section* arg1_section;
644 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
645 return (arg1
f6973bdc
ILT
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));
e5756efb 650 }
494e05f4
ILT
651
652 void
653 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 }
e5756efb
ILT
663};
664
665extern "C" Expression*
666script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
667{
668 return new Trinary_cond(arg1, arg2, arg3);
669}
670
671// Max function.
672
673class Max_expression : public Binary_expression
674{
675 public:
676 Max_expression(Expression* left, Expression* right)
677 : Binary_expression(left, right)
678 { }
679
680 uint64_t
681 value(const Expression_eval_info* eei)
77e65537
ILT
682 {
683 Output_section* left_section;
f6973bdc
ILT
684 uint64_t left_alignment;
685 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
77e65537 686 Output_section* right_section;
f6973bdc
ILT
687 uint64_t right_alignment;
688 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
77e65537 689 if (left_section == right_section)
bacff3ab
NC
690 {
691 if (eei->result_section_pointer != NULL)
692 *eei->result_section_pointer = left_section;
693 }
77e65537 694 else if ((left_section != NULL || right_section != NULL)
8851ecca 695 && parameters->options().relocatable())
77e65537 696 gold_warning(_("max applied to section relative value"));
f6973bdc
ILT
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 }
77e65537
ILT
708 return std::max(left, right);
709 }
494e05f4
ILT
710
711 void
712 print(FILE* f) const
713 { this->print_function(f, "MAX"); }
e5756efb
ILT
714};
715
716extern "C" Expression*
717script_exp_function_max(Expression* left, Expression* right)
718{
719 return new Max_expression(left, right);
720}
721
722// Min function.
723
724class Min_expression : public Binary_expression
725{
726 public:
727 Min_expression(Expression* left, Expression* right)
728 : Binary_expression(left, right)
729 { }
730
731 uint64_t
732 value(const Expression_eval_info* eei)
77e65537
ILT
733 {
734 Output_section* left_section;
f6973bdc
ILT
735 uint64_t left_alignment;
736 uint64_t left = this->left_value(eei, &left_section, &left_alignment);
77e65537 737 Output_section* right_section;
f6973bdc
ILT
738 uint64_t right_alignment;
739 uint64_t right = this->right_value(eei, &right_section, &right_alignment);
77e65537 740 if (left_section == right_section)
bacff3ab
NC
741 {
742 if (eei->result_section_pointer != NULL)
743 *eei->result_section_pointer = left_section;
744 }
77e65537 745 else if ((left_section != NULL || right_section != NULL)
8851ecca 746 && parameters->options().relocatable())
77e65537 747 gold_warning(_("min applied to section relative value"));
f6973bdc
ILT
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 }
77e65537
ILT
759 return std::min(left, right);
760 }
494e05f4
ILT
761
762 void
763 print(FILE* f) const
764 { this->print_function(f, "MIN"); }
e5756efb
ILT
765};
766
767extern "C" Expression*
768script_exp_function_min(Expression* left, Expression* right)
769{
770 return new Min_expression(left, right);
771}
772
7508a093
ILT
773// Class Section_expression. This is a parent class used for
774// functions which take the name of an output section.
775
776class Section_expression : public Expression
777{
778 public:
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
787 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
8f2eb564
ILT
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
7508a093
ILT
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
809uint64_t
810Section_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);
8f2eb564
ILT
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())
7508a093 823 {
8f2eb564
ILT
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);
7508a093
ILT
831 }
832
8f2eb564
ILT
833 gold_error("%s called on nonexistent output section '%s'",
834 this->function_name(), section_name);
835 return 0;
7508a093
ILT
836}
837
3edc73f2
ILT
838// ABSOLUTE function.
839
840class Absolute_expression : public Unary_expression
841{
842 public:
843 Absolute_expression(Expression* arg)
844 : Unary_expression(arg)
845 { }
846
847 uint64_t
848 value(const Expression_eval_info* eei)
849 {
bacff3ab 850 uint64_t ret = this->arg_value(eei, NULL);
3edc73f2 851 // Force the value to be absolute.
bacff3ab
NC
852 if (eei->result_section_pointer != NULL)
853 *eei->result_section_pointer = NULL;
3edc73f2
ILT
854 return ret;
855 }
856
857 void
858 print(FILE* f) const
859 {
860 fprintf(f, "ABSOLUTE(");
861 this->arg_print(f);
862 fprintf(f, ")");
863 }
864};
865
866extern "C" Expression*
867script_exp_function_absolute(Expression* arg)
868{
869 return new Absolute_expression(arg);
870}
871
872// ALIGN function.
e5756efb
ILT
873
874class Align_expression : public Binary_expression
875{
876 public:
877 Align_expression(Expression* left, Expression* right)
878 : Binary_expression(left, right)
879 { }
880
881 uint64_t
882 value(const Expression_eval_info* eei)
883 {
77e65537 884 Output_section* align_section;
f6973bdc 885 uint64_t align = this->right_value(eei, &align_section, NULL);
77e65537 886 if (align_section != NULL
8851ecca 887 && parameters->options().relocatable())
77e65537
ILT
888 gold_warning(_("aligning to section relative value"));
889
f6973bdc
ILT
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);
e5756efb 900 if (align <= 1)
2ea97941
ILT
901 return value;
902 return ((value + align - 1) / align) * align;
e5756efb 903 }
494e05f4
ILT
904
905 void
906 print(FILE* f) const
907 { this->print_function(f, "ALIGN"); }
e5756efb
ILT
908};
909
910extern "C" Expression*
911script_exp_function_align(Expression* left, Expression* right)
912{
913 return new Align_expression(left, right);
914}
915
3edc73f2 916// ASSERT function.
e5756efb
ILT
917
918class Assert_expression : public Unary_expression
919{
920 public:
921 Assert_expression(Expression* arg, const char* message, size_t length)
922 : Unary_expression(arg), message_(message, length)
923 { }
924
925 uint64_t
926 value(const Expression_eval_info* eei)
927 {
2ea97941
ILT
928 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
929 if (!value && eei->check_assertions)
e5756efb 930 gold_error("%s", this->message_.c_str());
2ea97941 931 return value;
e5756efb
ILT
932 }
933
494e05f4
ILT
934 void
935 print(FILE* f) const
936 {
937 fprintf(f, "ASSERT(");
938 this->arg_print(f);
939 fprintf(f, ", %s)", this->message_.c_str());
940 }
941
e5756efb
ILT
942 private:
943 std::string message_;
944};
945
946extern "C" Expression*
947script_exp_function_assert(Expression* expr, const char* message,
948 size_t length)
949{
950 return new Assert_expression(expr, message, length);
951}
952
7508a093 953// ADDR function.
494e05f4 954
7508a093 955class Addr_expression : public Section_expression
494e05f4
ILT
956{
957 public:
958 Addr_expression(const char* section_name, size_t section_name_len)
7508a093 959 : Section_expression(section_name, section_name_len)
494e05f4
ILT
960 { }
961
7508a093 962 protected:
494e05f4 963 uint64_t
3edc73f2 964 value_from_output_section(const Expression_eval_info* eei,
7508a093
ILT
965 Output_section* os)
966 {
bacff3ab
NC
967 if (eei->result_section_pointer != NULL)
968 *eei->result_section_pointer = os;
1757d35c
CC
969 if (os->is_address_valid())
970 return os->address();
971 *eei->is_valid_pointer = false;
972 return 0;
7508a093 973 }
494e05f4 974
8f2eb564
ILT
975 uint64_t
976 value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
977 uint64_t)
978 { return address; }
979
7508a093
ILT
980 const char*
981 function_name() const
982 { return "ADDR"; }
494e05f4
ILT
983};
984
494e05f4
ILT
985extern "C" Expression*
986script_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
3edc73f2
ILT
991// ALIGNOF.
992
993class Alignof_expression : public Section_expression
994{
995 public:
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
1002 value_from_output_section(const Expression_eval_info*,
1003 Output_section* os)
1004 { return os->addralign(); }
1005
8f2eb564
ILT
1006 uint64_t
1007 value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
1008 uint64_t)
1009 { return addralign; }
1010
3edc73f2
ILT
1011 const char*
1012 function_name() const
1013 { return "ALIGNOF"; }
1014};
1015
1016extern "C" Expression*
1017script_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
3802b2dd
ILT
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
1026class 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 };
e5756efb 1043
3802b2dd
ILT
1044 Constant_function function_;
1045};
1046
1047Constant_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
1061uint64_t
1062Constant_expression::value(const Expression_eval_info*)
1063{
1064 switch (this->function_)
1065 {
1066 case CONSTANT_MAXPAGESIZE:
8851ecca 1067 return parameters->target().abi_pagesize();
3802b2dd 1068 case CONSTANT_COMMONPAGESIZE:
8851ecca 1069 return parameters->target().common_pagesize();
3802b2dd
ILT
1070 default:
1071 gold_unreachable();
1072 }
1073}
1074
1075void
1076Constant_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
e5756efb 1093extern "C" Expression*
3802b2dd 1094script_exp_function_constant(const char* name, size_t length)
e5756efb 1095{
3802b2dd 1096 return new Constant_expression(name, length);
e5756efb
ILT
1097}
1098
3802b2dd
ILT
1099// DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
1100// back to the general case.
1101
e5756efb 1102extern "C" Expression*
3802b2dd 1103script_exp_function_data_segment_align(Expression* left, Expression*)
e5756efb 1104{
3802b2dd
ILT
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);
e5756efb
ILT
1110}
1111
3802b2dd
ILT
1112// DATA_SEGMENT_RELRO. FIXME: This is not implemented.
1113
e5756efb 1114extern "C" Expression*
3802b2dd 1115script_exp_function_data_segment_relro_end(Expression*, Expression* right)
e5756efb 1116{
3802b2dd 1117 return right;
e5756efb
ILT
1118}
1119
3802b2dd
ILT
1120// DATA_SEGMENT_END. FIXME: This is not implemented.
1121
e5756efb 1122extern "C" Expression*
3802b2dd 1123script_exp_function_data_segment_end(Expression* val)
e5756efb 1124{
3802b2dd
ILT
1125 return val;
1126}
1127
3edc73f2
ILT
1128// DEFINED function.
1129
1130class Defined_expression : public Expression
1131{
1132 public:
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
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
1145 print(FILE* f) const
1146 { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
1147
1148 private:
1149 std::string symbol_name_;
1150};
1151
1152extern "C" Expression*
1153script_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
7508a093
ILT
1158// LOADADDR function
1159
1160class Loadaddr_expression : public Section_expression
1161{
1162 public:
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
3edc73f2 1169 value_from_output_section(const Expression_eval_info* eei,
7508a093
ILT
1170 Output_section* os)
1171 {
1172 if (os->has_load_address())
1173 return os->load_address();
1174 else
1175 {
bacff3ab
NC
1176 if (eei->result_section_pointer != NULL)
1177 *eei->result_section_pointer = os;
7508a093
ILT
1178 return os->address();
1179 }
1180 }
1181
8f2eb564
ILT
1182 uint64_t
1183 value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
1184 uint64_t)
1185 { return load_address; }
1186
7508a093
ILT
1187 const char*
1188 function_name() const
1189 { return "LOADADDR"; }
1190};
1191
1192extern "C" Expression*
1193script_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
1200class Sizeof_expression : public Section_expression
1201{
1202 public:
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
3edc73f2 1209 value_from_output_section(const Expression_eval_info*,
7508a093
ILT
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
8f2eb564
ILT
1219 uint64_t
1220 value_from_script_output_section(uint64_t, uint64_t, uint64_t,
1221 uint64_t size)
1222 { return size; }
1223
7508a093
ILT
1224 const char*
1225 function_name() const
1226 { return "SIZEOF"; }
1227};
1228
1229extern "C" Expression*
1230script_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
3802b2dd
ILT
1235// SIZEOF_HEADERS.
1236
1237class Sizeof_headers_expression : public Expression
1238{
1239 public:
1240 Sizeof_headers_expression()
1241 { }
1242
1243 uint64_t
1244 value(const Expression_eval_info*);
1245
1246 void
1247 print(FILE* f) const
1248 { fprintf(f, "SIZEOF_HEADERS"); }
1249};
1250
1251uint64_t
1252Sizeof_headers_expression::value(const Expression_eval_info* eei)
1253{
1254 unsigned int ehdr_size;
1255 unsigned int phdr_size;
8851ecca 1256 if (parameters->target().get_size() == 32)
3802b2dd
ILT
1257 {
1258 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
1259 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
1260 }
8851ecca 1261 else if (parameters->target().get_size() == 64)
3802b2dd
ILT
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();
e5756efb
ILT
1270}
1271
e5756efb 1272extern "C" Expression*
3802b2dd 1273script_exp_function_sizeof_headers()
e5756efb 1274{
3802b2dd 1275 return new Sizeof_headers_expression();
e5756efb
ILT
1276}
1277
3c12dcdb
DK
1278// SEGMENT_START.
1279
1280class Segment_start_expression : public Unary_expression
1281{
1282 public:
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
1293 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
1304uint64_t
1305Segment_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 {
bacff3ab 1319 uint64_t ret = this->arg_value(eei, NULL);
3c12dcdb 1320 // Force the value to be absolute.
bacff3ab
NC
1321 if (eei->result_section_pointer != NULL)
1322 *eei->result_section_pointer = NULL;
3c12dcdb
DK
1323 return ret;
1324 }
1325}
3802b2dd 1326
e5756efb 1327extern "C" Expression*
3c12dcdb
DK
1328script_exp_function_segment_start(const char* segment_name,
1329 size_t segment_name_len,
1330 Expression* default_value)
e5756efb 1331{
3c12dcdb
DK
1332 return new Segment_start_expression(segment_name, segment_name_len,
1333 default_value);
e5756efb
ILT
1334}
1335
e5756efb 1336} // End namespace gold.
This page took 0.484185 seconds and 4 git commands to generate.