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