* gas/mips/branch-misc-2pic-64.d (#name): Have a unique name
[deliverable/binutils-gdb.git] / gold / expression.cc
CommitLineData
e5756efb
ILT
1// expression.cc -- expressions in linker scripts for gold
2
3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
25#include <string>
26
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;
a445fddf
ILT
57 // Whether expressions can refer to the dot symbol. The dot symbol
58 // is only available within a SECTIONS clause.
59 bool is_dot_available;
a445fddf
ILT
60 // The current value of the dot symbol.
61 uint64_t dot_value;
77e65537
ILT
62 // The section in which the dot symbol is defined; this is NULL if
63 // it is absolute.
64 Output_section* dot_section;
65 // Points to where the section of the result should be stored.
66 Output_section** result_section_pointer;
e5756efb
ILT
67};
68
69// Evaluate an expression.
70
71uint64_t
72Expression::eval(const Symbol_table* symtab, const Layout* layout)
a445fddf 73{
77e65537
ILT
74 Output_section* dummy;
75 return this->eval_maybe_dot(symtab, layout, false, 0, NULL, &dummy);
a445fddf
ILT
76}
77
78// Evaluate an expression which may refer to the dot symbol.
79
80uint64_t
81Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
77e65537
ILT
82 uint64_t dot_value, Output_section* dot_section,
83 Output_section** result_section_pointer)
a445fddf 84{
77e65537
ILT
85 return this->eval_maybe_dot(symtab, layout, true, dot_value, dot_section,
86 result_section_pointer);
a445fddf
ILT
87}
88
89// Evaluate an expression which may or may not refer to the dot
90// symbol.
91
92uint64_t
93Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
77e65537
ILT
94 bool is_dot_available, uint64_t dot_value,
95 Output_section* dot_section,
96 Output_section** result_section_pointer)
e5756efb
ILT
97{
98 Expression_eval_info eei;
99 eei.symtab = symtab;
100 eei.layout = layout;
a445fddf 101 eei.is_dot_available = is_dot_available;
a445fddf 102 eei.dot_value = dot_value;
77e65537 103 eei.dot_section = dot_section;
a445fddf 104
77e65537
ILT
105 // We assume the value is absolute, and only set this to a section
106 // if we find a section relative reference.
107 *result_section_pointer = NULL;
108 eei.result_section_pointer = result_section_pointer;
a445fddf 109
e5756efb
ILT
110 return this->value(&eei);
111}
112
113// A number.
114
115class Integer_expression : public Expression
116{
117 public:
118 Integer_expression(uint64_t val)
119 : val_(val)
120 { }
121
122 uint64_t
123 value(const Expression_eval_info*)
124 { return this->val_; }
125
494e05f4
ILT
126 void
127 print(FILE* f) const
128 { fprintf(f, "0x%llx", static_cast<unsigned long long>(this->val_)); }
129
e5756efb
ILT
130 private:
131 uint64_t val_;
132};
133
134extern "C" Expression*
135script_exp_integer(uint64_t val)
136{
137 return new Integer_expression(val);
138}
139
140// An expression whose value is the value of a symbol.
141
142class Symbol_expression : public Expression
143{
144 public:
145 Symbol_expression(const char* name, size_t length)
146 : name_(name, length)
147 { }
148
149 uint64_t
150 value(const Expression_eval_info*);
151
494e05f4
ILT
152 void
153 print(FILE* f) const
154 { fprintf(f, "%s", this->name_.c_str()); }
155
e5756efb
ILT
156 private:
157 std::string name_;
158};
159
160uint64_t
161Symbol_expression::value(const Expression_eval_info* eei)
162{
163 Symbol* sym = eei->symtab->lookup(this->name_.c_str());
164 if (sym == NULL || !sym->is_defined())
165 {
166 gold_error(_("undefined symbol '%s' referenced in expression"),
167 this->name_.c_str());
168 return 0;
169 }
170
77e65537 171 *eei->result_section_pointer = sym->output_section();
a445fddf 172
e5756efb
ILT
173 if (parameters->get_size() == 32)
174 return eei->symtab->get_sized_symbol<32>(sym)->value();
175 else if (parameters->get_size() == 64)
176 return eei->symtab->get_sized_symbol<64>(sym)->value();
177 else
178 gold_unreachable();
179}
180
181// An expression whose value is the value of the special symbol ".".
182// This is only valid within a SECTIONS clause.
183
184class Dot_expression : public Expression
185{
186 public:
187 Dot_expression()
188 { }
189
190 uint64_t
191 value(const Expression_eval_info*);
494e05f4
ILT
192
193 void
194 print(FILE* f) const
195 { fprintf(f, "."); }
e5756efb
ILT
196};
197
198uint64_t
a445fddf 199Dot_expression::value(const Expression_eval_info* eei)
e5756efb 200{
a445fddf
ILT
201 if (!eei->is_dot_available)
202 {
203 gold_error(_("invalid reference to dot symbol outside of "
204 "SECTIONS clause"));
205 return 0;
206 }
77e65537 207 *eei->result_section_pointer = eei->dot_section;
a445fddf 208 return eei->dot_value;
e5756efb
ILT
209}
210
211// A string. This is either the name of a symbol, or ".".
212
213extern "C" Expression*
214script_exp_string(const char* name, size_t length)
215{
216 if (length == 1 && name[0] == '.')
217 return new Dot_expression();
218 else
219 return new Symbol_expression(name, length);
220}
221
222// A unary expression.
223
224class Unary_expression : public Expression
225{
226 public:
227 Unary_expression(Expression* arg)
228 : arg_(arg)
229 { }
230
231 ~Unary_expression()
232 { delete this->arg_; }
233
234 protected:
235 uint64_t
77e65537
ILT
236 arg_value(const Expression_eval_info* eei,
237 Output_section** arg_section_pointer) const
238 {
239 return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
240 eei->is_dot_available,
241 eei->dot_value,
242 eei->dot_section,
243 arg_section_pointer);
244 }
e5756efb 245
494e05f4
ILT
246 void
247 arg_print(FILE* f) const
248 { this->arg_->print(f); }
249
e5756efb
ILT
250 private:
251 Expression* arg_;
252};
253
254// Handle unary operators. We use a preprocessor macro as a hack to
255// capture the C operator.
256
77e65537
ILT
257#define UNARY_EXPRESSION(NAME, OPERATOR) \
258 class Unary_ ## NAME : public Unary_expression \
259 { \
260 public: \
261 Unary_ ## NAME(Expression* arg) \
262 : Unary_expression(arg) \
263 { } \
264 \
265 uint64_t \
266 value(const Expression_eval_info* eei) \
267 { \
268 Output_section* arg_section; \
269 uint64_t ret = OPERATOR this->arg_value(eei, &arg_section); \
270 if (arg_section != NULL && parameters->output_is_object()) \
271 gold_warning(_("unary " #NAME " applied to section " \
272 "relative value")); \
273 return ret; \
274 } \
275 \
276 void \
277 print(FILE* f) const \
278 { \
279 fprintf(f, "(%s ", #OPERATOR); \
280 this->arg_print(f); \
281 fprintf(f, ")"); \
282 } \
283 }; \
284 \
285 extern "C" Expression* \
286 script_exp_unary_ ## NAME(Expression* arg) \
287 { \
288 return new Unary_ ## NAME(arg); \
e5756efb
ILT
289 }
290
291UNARY_EXPRESSION(minus, -)
292UNARY_EXPRESSION(logical_not, !)
293UNARY_EXPRESSION(bitwise_not, ~)
294
295// A binary expression.
296
297class Binary_expression : public Expression
298{
299 public:
300 Binary_expression(Expression* left, Expression* right)
301 : left_(left), right_(right)
302 { }
303
304 ~Binary_expression()
305 {
306 delete this->left_;
307 delete this->right_;
308 }
309
310 protected:
311 uint64_t
77e65537
ILT
312 left_value(const Expression_eval_info* eei,
313 Output_section** section_pointer) const
314 {
315 return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
316 eei->is_dot_available,
317 eei->dot_value,
318 eei->dot_section,
319 section_pointer);
320 }
e5756efb
ILT
321
322 uint64_t
77e65537
ILT
323 right_value(const Expression_eval_info* eei,
324 Output_section** section_pointer) const
325 {
326 return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
327 eei->is_dot_available,
328 eei->dot_value,
329 eei->dot_section,
330 section_pointer);
331 }
e5756efb 332
494e05f4
ILT
333 void
334 left_print(FILE* f) const
335 { this->left_->print(f); }
336
337 void
338 right_print(FILE* f) const
339 { this->right_->print(f); }
340
341 // This is a call to function FUNCTION_NAME. Print it. This is for
342 // debugging.
343 void
344 print_function(FILE* f, const char *function_name) const
345 {
346 fprintf(f, "%s(", function_name);
347 this->left_print(f);
348 fprintf(f, ", ");
349 this->right_print(f);
350 fprintf(f, ")");
351 }
352
e5756efb
ILT
353 private:
354 Expression* left_;
355 Expression* right_;
356};
357
358// Handle binary operators. We use a preprocessor macro as a hack to
77e65537
ILT
359// capture the C operator. KEEP_LEFT means that if the left operand
360// is section relative and the right operand is not, the result uses
361// the same section as the left operand. KEEP_RIGHT is the same with
362// left and right swapped. IS_DIV means that we need to give an error
363// if the right operand is zero. WARN means that we should warn if
364// used on section relative values in a relocatable link. We always
365// warn if used on values in different sections in a relocatable link.
366
367#define BINARY_EXPRESSION(NAME, OPERATOR, KEEP_LEFT, KEEP_RIGHT, IS_DIV, WARN) \
e5756efb
ILT
368 class Binary_ ## NAME : public Binary_expression \
369 { \
370 public: \
371 Binary_ ## NAME(Expression* left, Expression* right) \
372 : Binary_expression(left, right) \
373 { } \
374 \
375 uint64_t \
376 value(const Expression_eval_info* eei) \
377 { \
77e65537
ILT
378 Output_section* left_section; \
379 uint64_t left = this->left_value(eei, &left_section); \
380 Output_section* right_section; \
381 uint64_t right = this->right_value(eei, &right_section); \
382 if (KEEP_RIGHT && left_section == NULL && right_section != NULL) \
383 *eei->result_section_pointer = right_section; \
384 else if (KEEP_LEFT \
385 && left_section != NULL \
386 && right_section == NULL) \
387 *eei->result_section_pointer = left_section; \
388 else if ((WARN || left_section != right_section) \
389 && (left_section != NULL || right_section != NULL) \
390 && parameters->output_is_object()) \
391 gold_warning(_("binary " #NAME " applied to section " \
392 "relative value")); \
393 if (IS_DIV && right == 0) \
394 { \
395 gold_error(_(#NAME " by zero")); \
396 return 0; \
397 } \
398 return left OPERATOR right; \
494e05f4
ILT
399 } \
400 \
401 void \
402 print(FILE* f) const \
403 { \
404 fprintf(f, "("); \
405 this->left_print(f); \
406 fprintf(f, " %s ", #OPERATOR); \
407 this->right_print(f); \
408 fprintf(f, ")"); \
e5756efb
ILT
409 } \
410 }; \
411 \
412 extern "C" Expression* \
413 script_exp_binary_ ## NAME(Expression* left, Expression* right) \
414 { \
415 return new Binary_ ## NAME(left, right); \
416 }
417
77e65537
ILT
418BINARY_EXPRESSION(mult, *, false, false, false, true)
419BINARY_EXPRESSION(div, /, false, false, true, true)
420BINARY_EXPRESSION(mod, %, false, false, true, true)
421BINARY_EXPRESSION(add, +, true, true, false, true)
422BINARY_EXPRESSION(sub, -, true, false, false, false)
423BINARY_EXPRESSION(lshift, <<, false, false, false, true)
424BINARY_EXPRESSION(rshift, >>, false, false, false, true)
425BINARY_EXPRESSION(eq, ==, false, false, false, false)
426BINARY_EXPRESSION(ne, !=, false, false, false, false)
427BINARY_EXPRESSION(le, <=, false, false, false, false)
428BINARY_EXPRESSION(ge, >=, false, false, false, false)
429BINARY_EXPRESSION(lt, <, false, false, false, false)
430BINARY_EXPRESSION(gt, >, false, false, false, false)
431BINARY_EXPRESSION(bitwise_and, &, true, true, false, true)
432BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true)
433BINARY_EXPRESSION(bitwise_or, |, true, true, false, true)
434BINARY_EXPRESSION(logical_and, &&, false, false, false, true)
435BINARY_EXPRESSION(logical_or, ||, false, false, false, true)
e5756efb
ILT
436
437// A trinary expression.
438
439class Trinary_expression : public Expression
440{
441 public:
442 Trinary_expression(Expression* arg1, Expression* arg2, Expression* arg3)
443 : arg1_(arg1), arg2_(arg2), arg3_(arg3)
444 { }
445
446 ~Trinary_expression()
447 {
448 delete this->arg1_;
449 delete this->arg2_;
450 delete this->arg3_;
451 }
452
453 protected:
454 uint64_t
77e65537
ILT
455 arg1_value(const Expression_eval_info* eei,
456 Output_section** section_pointer) const
457 {
458 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
459 eei->is_dot_available,
460 eei->dot_value,
461 eei->dot_section,
462 section_pointer);
463 }
e5756efb
ILT
464
465 uint64_t
77e65537
ILT
466 arg2_value(const Expression_eval_info* eei,
467 Output_section** section_pointer) const
468 {
469 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
470 eei->is_dot_available,
471 eei->dot_value,
472 eei->dot_section,
473 section_pointer);
474 }
e5756efb
ILT
475
476 uint64_t
77e65537
ILT
477 arg3_value(const Expression_eval_info* eei,
478 Output_section** section_pointer) const
479 {
480 return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
481 eei->is_dot_available,
482 eei->dot_value,
483 eei->dot_section,
484 section_pointer);
485 }
e5756efb 486
494e05f4
ILT
487 void
488 arg1_print(FILE* f) const
489 { this->arg1_->print(f); }
490
491 void
492 arg2_print(FILE* f) const
493 { this->arg2_->print(f); }
494
495 void
496 arg3_print(FILE* f) const
497 { this->arg3_->print(f); }
498
e5756efb
ILT
499 private:
500 Expression* arg1_;
501 Expression* arg2_;
502 Expression* arg3_;
503};
504
505// The conditional operator.
506
507class Trinary_cond : public Trinary_expression
508{
509 public:
510 Trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
511 : Trinary_expression(arg1, arg2, arg3)
512 { }
513
514 uint64_t
515 value(const Expression_eval_info* eei)
516 {
77e65537
ILT
517 Output_section* arg1_section;
518 uint64_t arg1 = this->arg1_value(eei, &arg1_section);
519 return (arg1
520 ? this->arg2_value(eei, eei->result_section_pointer)
521 : this->arg3_value(eei, eei->result_section_pointer));
e5756efb 522 }
494e05f4
ILT
523
524 void
525 print(FILE* f) const
526 {
527 fprintf(f, "(");
528 this->arg1_print(f);
529 fprintf(f, " ? ");
530 this->arg2_print(f);
531 fprintf(f, " : ");
532 this->arg3_print(f);
533 fprintf(f, ")");
534 }
e5756efb
ILT
535};
536
537extern "C" Expression*
538script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3)
539{
540 return new Trinary_cond(arg1, arg2, arg3);
541}
542
543// Max function.
544
545class Max_expression : public Binary_expression
546{
547 public:
548 Max_expression(Expression* left, Expression* right)
549 : Binary_expression(left, right)
550 { }
551
552 uint64_t
553 value(const Expression_eval_info* eei)
77e65537
ILT
554 {
555 Output_section* left_section;
556 uint64_t left = this->left_value(eei, &left_section);
557 Output_section* right_section;
558 uint64_t right = this->right_value(eei, &right_section);
559 if (left_section == right_section)
560 *eei->result_section_pointer = left_section;
561 else if ((left_section != NULL || right_section != NULL)
562 && parameters->output_is_object())
563 gold_warning(_("max applied to section relative value"));
564 return std::max(left, right);
565 }
494e05f4
ILT
566
567 void
568 print(FILE* f) const
569 { this->print_function(f, "MAX"); }
e5756efb
ILT
570};
571
572extern "C" Expression*
573script_exp_function_max(Expression* left, Expression* right)
574{
575 return new Max_expression(left, right);
576}
577
578// Min function.
579
580class Min_expression : public Binary_expression
581{
582 public:
583 Min_expression(Expression* left, Expression* right)
584 : Binary_expression(left, right)
585 { }
586
587 uint64_t
588 value(const Expression_eval_info* eei)
77e65537
ILT
589 {
590 Output_section* left_section;
591 uint64_t left = this->left_value(eei, &left_section);
592 Output_section* right_section;
593 uint64_t right = this->right_value(eei, &right_section);
594 if (left_section == right_section)
595 *eei->result_section_pointer = left_section;
596 else if ((left_section != NULL || right_section != NULL)
597 && parameters->output_is_object())
598 gold_warning(_("min applied to section relative value"));
599 return std::min(left, right);
600 }
494e05f4
ILT
601
602 void
603 print(FILE* f) const
604 { this->print_function(f, "MIN"); }
e5756efb
ILT
605};
606
607extern "C" Expression*
608script_exp_function_min(Expression* left, Expression* right)
609{
610 return new Min_expression(left, right);
611}
612
7508a093
ILT
613// Class Section_expression. This is a parent class used for
614// functions which take the name of an output section.
615
616class Section_expression : public Expression
617{
618 public:
619 Section_expression(const char* section_name, size_t section_name_len)
620 : section_name_(section_name, section_name_len)
621 { }
622
623 uint64_t
624 value(const Expression_eval_info*);
625
626 void
627 print(FILE* f) const
628 { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
629
630 protected:
631 // The child class must implement this.
632 virtual uint64_t
633 value_from_output_section(const Expression_eval_info*,
634 Output_section*) = 0;
635
636 // The child class must implement this.
637 virtual const char*
638 function_name() const = 0;
639
640 private:
641 std::string section_name_;
642};
643
644uint64_t
645Section_expression::value(const Expression_eval_info* eei)
646{
647 const char* section_name = this->section_name_.c_str();
648 Output_section* os = eei->layout->find_output_section(section_name);
649 if (os == NULL)
650 {
651 gold_error("%s called on nonexistent output section '%s'",
652 this->function_name(), section_name);
653 return 0;
654 }
655
656 return this->value_from_output_section(eei, os);
657}
658
e5756efb
ILT
659// Align function.
660
661class Align_expression : public Binary_expression
662{
663 public:
664 Align_expression(Expression* left, Expression* right)
665 : Binary_expression(left, right)
666 { }
667
668 uint64_t
669 value(const Expression_eval_info* eei)
670 {
77e65537
ILT
671 Output_section* align_section;
672 uint64_t align = this->right_value(eei, &align_section);
673 if (align_section != NULL
674 && parameters->output_is_object())
675 gold_warning(_("aligning to section relative value"));
676
677 uint64_t value = this->left_value(eei, eei->result_section_pointer);
e5756efb
ILT
678 if (align <= 1)
679 return value;
680 return ((value + align - 1) / align) * align;
681 }
494e05f4
ILT
682
683 void
684 print(FILE* f) const
685 { this->print_function(f, "ALIGN"); }
e5756efb
ILT
686};
687
688extern "C" Expression*
689script_exp_function_align(Expression* left, Expression* right)
690{
691 return new Align_expression(left, right);
692}
693
694// Assert function.
695
696class Assert_expression : public Unary_expression
697{
698 public:
699 Assert_expression(Expression* arg, const char* message, size_t length)
700 : Unary_expression(arg), message_(message, length)
701 { }
702
703 uint64_t
704 value(const Expression_eval_info* eei)
705 {
77e65537 706 uint64_t value = this->arg_value(eei, eei->result_section_pointer);
e5756efb
ILT
707 if (!value)
708 gold_error("%s", this->message_.c_str());
709 return value;
710 }
711
494e05f4
ILT
712 void
713 print(FILE* f) const
714 {
715 fprintf(f, "ASSERT(");
716 this->arg_print(f);
717 fprintf(f, ", %s)", this->message_.c_str());
718 }
719
e5756efb
ILT
720 private:
721 std::string message_;
722};
723
724extern "C" Expression*
725script_exp_function_assert(Expression* expr, const char* message,
726 size_t length)
727{
728 return new Assert_expression(expr, message, length);
729}
730
7508a093 731// ADDR function.
494e05f4 732
7508a093 733class Addr_expression : public Section_expression
494e05f4
ILT
734{
735 public:
736 Addr_expression(const char* section_name, size_t section_name_len)
7508a093 737 : Section_expression(section_name, section_name_len)
494e05f4
ILT
738 { }
739
7508a093 740 protected:
494e05f4 741 uint64_t
7508a093
ILT
742 value_from_output_section(const Expression_eval_info *eei,
743 Output_section* os)
744 {
745 *eei->result_section_pointer = os;
746 return os->address();
747 }
494e05f4 748
7508a093
ILT
749 const char*
750 function_name() const
751 { return "ADDR"; }
494e05f4
ILT
752};
753
494e05f4
ILT
754extern "C" Expression*
755script_exp_function_addr(const char* section_name, size_t section_name_len)
756{
757 return new Addr_expression(section_name, section_name_len);
758}
759
3802b2dd
ILT
760// CONSTANT. It would be nice if we could simply evaluate this
761// immediately and return an Integer_expression, but unfortunately we
762// don't know the target.
763
764class Constant_expression : public Expression
765{
766 public:
767 Constant_expression(const char* name, size_t length);
768
769 uint64_t
770 value(const Expression_eval_info*);
771
772 void
773 print(FILE* f) const;
774
775 private:
776 enum Constant_function
777 {
778 CONSTANT_MAXPAGESIZE,
779 CONSTANT_COMMONPAGESIZE
780 };
e5756efb 781
3802b2dd
ILT
782 Constant_function function_;
783};
784
785Constant_expression::Constant_expression(const char* name, size_t length)
786{
787 if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0)
788 this->function_ = CONSTANT_MAXPAGESIZE;
789 else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0)
790 this->function_ = CONSTANT_COMMONPAGESIZE;
791 else
792 {
793 std::string s(name, length);
794 gold_error(_("unknown constant %s"), s.c_str());
795 this->function_ = CONSTANT_MAXPAGESIZE;
796 }
797}
798
799uint64_t
800Constant_expression::value(const Expression_eval_info*)
801{
802 switch (this->function_)
803 {
804 case CONSTANT_MAXPAGESIZE:
805 return parameters->target()->abi_pagesize();
806 case CONSTANT_COMMONPAGESIZE:
807 return parameters->target()->common_pagesize();
808 default:
809 gold_unreachable();
810 }
811}
812
813void
814Constant_expression::print(FILE* f) const
815{
816 const char* name;
817 switch (this->function_)
818 {
819 case CONSTANT_MAXPAGESIZE:
820 name = "MAXPAGESIZE";
821 break;
822 case CONSTANT_COMMONPAGESIZE:
823 name = "COMMONPAGESIZE";
824 break;
825 default:
826 gold_unreachable();
827 }
828 fprintf(f, "CONSTANT(%s)", name);
829}
830
e5756efb 831extern "C" Expression*
3802b2dd 832script_exp_function_constant(const char* name, size_t length)
e5756efb 833{
3802b2dd 834 return new Constant_expression(name, length);
e5756efb
ILT
835}
836
3802b2dd
ILT
837// DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall
838// back to the general case.
839
e5756efb 840extern "C" Expression*
3802b2dd 841script_exp_function_data_segment_align(Expression* left, Expression*)
e5756efb 842{
3802b2dd
ILT
843 Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left);
844 Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1));
845 Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1),
846 e2);
847 return script_exp_binary_add(e1, e3);
e5756efb
ILT
848}
849
3802b2dd
ILT
850// DATA_SEGMENT_RELRO. FIXME: This is not implemented.
851
e5756efb 852extern "C" Expression*
3802b2dd 853script_exp_function_data_segment_relro_end(Expression*, Expression* right)
e5756efb 854{
3802b2dd 855 return right;
e5756efb
ILT
856}
857
3802b2dd
ILT
858// DATA_SEGMENT_END. FIXME: This is not implemented.
859
e5756efb 860extern "C" Expression*
3802b2dd 861script_exp_function_data_segment_end(Expression* val)
e5756efb 862{
3802b2dd
ILT
863 return val;
864}
865
7508a093
ILT
866// LOADADDR function
867
868class Loadaddr_expression : public Section_expression
869{
870 public:
871 Loadaddr_expression(const char* section_name, size_t section_name_len)
872 : Section_expression(section_name, section_name_len)
873 { }
874
875 protected:
876 uint64_t
877 value_from_output_section(const Expression_eval_info *eei,
878 Output_section* os)
879 {
880 if (os->has_load_address())
881 return os->load_address();
882 else
883 {
884 *eei->result_section_pointer = os;
885 return os->address();
886 }
887 }
888
889 const char*
890 function_name() const
891 { return "LOADADDR"; }
892};
893
894extern "C" Expression*
895script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
896{
897 return new Loadaddr_expression(section_name, section_name_len);
898}
899
900// SIZEOF function
901
902class Sizeof_expression : public Section_expression
903{
904 public:
905 Sizeof_expression(const char* section_name, size_t section_name_len)
906 : Section_expression(section_name, section_name_len)
907 { }
908
909 protected:
910 uint64_t
911 value_from_output_section(const Expression_eval_info *,
912 Output_section* os)
913 {
914 // We can not use data_size here, as the size of the section may
915 // not have been finalized. Instead we get whatever the current
916 // size is. This will work correctly for backward references in
917 // linker scripts.
918 return os->current_data_size();
919 }
920
921 const char*
922 function_name() const
923 { return "SIZEOF"; }
924};
925
926extern "C" Expression*
927script_exp_function_sizeof(const char* section_name, size_t section_name_len)
928{
929 return new Sizeof_expression(section_name, section_name_len);
930}
931
3802b2dd
ILT
932// SIZEOF_HEADERS.
933
934class Sizeof_headers_expression : public Expression
935{
936 public:
937 Sizeof_headers_expression()
938 { }
939
940 uint64_t
941 value(const Expression_eval_info*);
942
943 void
944 print(FILE* f) const
945 { fprintf(f, "SIZEOF_HEADERS"); }
946};
947
948uint64_t
949Sizeof_headers_expression::value(const Expression_eval_info* eei)
950{
951 unsigned int ehdr_size;
952 unsigned int phdr_size;
953 if (parameters->get_size() == 32)
954 {
955 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
956 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
957 }
958 else if (parameters->get_size() == 64)
959 {
960 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
961 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
962 }
963 else
964 gold_unreachable();
965
966 return ehdr_size + phdr_size * eei->layout->expected_segment_count();
e5756efb
ILT
967}
968
e5756efb 969extern "C" Expression*
3802b2dd 970script_exp_function_sizeof_headers()
e5756efb 971{
3802b2dd 972 return new Sizeof_headers_expression();
e5756efb
ILT
973}
974
3802b2dd
ILT
975// Functions.
976
e5756efb 977extern "C" Expression*
3802b2dd 978script_exp_function_defined(const char*, size_t)
e5756efb 979{
3802b2dd 980 gold_fatal(_("DEFINED not implemented"));
e5756efb
ILT
981}
982
983extern "C" Expression*
3802b2dd 984script_exp_function_alignof(const char*, size_t)
e5756efb 985{
3802b2dd 986 gold_fatal(_("ALIGNOF not implemented"));
e5756efb
ILT
987}
988
e5756efb 989extern "C" Expression*
3802b2dd 990script_exp_function_origin(const char*, size_t)
e5756efb 991{
3802b2dd 992 gold_fatal(_("ORIGIN not implemented"));
e5756efb
ILT
993}
994
995extern "C" Expression*
3802b2dd 996script_exp_function_length(const char*, size_t)
e5756efb 997{
3802b2dd 998 gold_fatal(_("LENGTH not implemented"));
e5756efb
ILT
999}
1000
1001extern "C" Expression*
3802b2dd 1002script_exp_function_absolute(Expression*)
e5756efb 1003{
3802b2dd 1004 gold_fatal(_("ABSOLUTE not implemented"));
e5756efb
ILT
1005}
1006
1007extern "C" Expression*
1008script_exp_function_segment_start(const char*, size_t, Expression*)
1009{
1010 gold_fatal(_("SEGMENT_START not implemented"));
1011}
1012
1013} // End namespace gold.
This page took 0.091496 seconds and 4 git commands to generate.