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