OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gold/] [expression.cc] - Blame information for rev 205

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.