Commit | Line | Data |
---|---|---|
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 | ||
35 | namespace 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 | ||
51 | struct 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 | ||
71 | uint64_t | |
72 | Expression::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 | ||
80 | uint64_t | |
81 | Expression::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 | ||
92 | uint64_t | |
93 | Expression::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 | ||
115 | class 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 | ||
134 | extern "C" Expression* | |
135 | script_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 | ||
142 | class 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 | ||
160 | uint64_t | |
161 | Symbol_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 | ||
184 | class 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 | ||
198 | uint64_t | |
a445fddf | 199 | Dot_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 | ||
213 | extern "C" Expression* | |
214 | script_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 | ||
224 | class 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 | ||
291 | UNARY_EXPRESSION(minus, -) | |
292 | UNARY_EXPRESSION(logical_not, !) | |
293 | UNARY_EXPRESSION(bitwise_not, ~) | |
294 | ||
295 | // A binary expression. | |
296 | ||
297 | class 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 |
418 | BINARY_EXPRESSION(mult, *, false, false, false, true) |
419 | BINARY_EXPRESSION(div, /, false, false, true, true) | |
420 | BINARY_EXPRESSION(mod, %, false, false, true, true) | |
421 | BINARY_EXPRESSION(add, +, true, true, false, true) | |
422 | BINARY_EXPRESSION(sub, -, true, false, false, false) | |
423 | BINARY_EXPRESSION(lshift, <<, false, false, false, true) | |
424 | BINARY_EXPRESSION(rshift, >>, false, false, false, true) | |
425 | BINARY_EXPRESSION(eq, ==, false, false, false, false) | |
426 | BINARY_EXPRESSION(ne, !=, false, false, false, false) | |
427 | BINARY_EXPRESSION(le, <=, false, false, false, false) | |
428 | BINARY_EXPRESSION(ge, >=, false, false, false, false) | |
429 | BINARY_EXPRESSION(lt, <, false, false, false, false) | |
430 | BINARY_EXPRESSION(gt, >, false, false, false, false) | |
431 | BINARY_EXPRESSION(bitwise_and, &, true, true, false, true) | |
432 | BINARY_EXPRESSION(bitwise_xor, ^, true, true, false, true) | |
433 | BINARY_EXPRESSION(bitwise_or, |, true, true, false, true) | |
434 | BINARY_EXPRESSION(logical_and, &&, false, false, false, true) | |
435 | BINARY_EXPRESSION(logical_or, ||, false, false, false, true) | |
e5756efb ILT |
436 | |
437 | // A trinary expression. | |
438 | ||
439 | class 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 | ||
507 | class 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 | ||
537 | extern "C" Expression* | |
538 | script_exp_trinary_cond(Expression* arg1, Expression* arg2, Expression* arg3) | |
539 | { | |
540 | return new Trinary_cond(arg1, arg2, arg3); | |
541 | } | |
542 | ||
543 | // Max function. | |
544 | ||
545 | class 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 | ||
572 | extern "C" Expression* | |
573 | script_exp_function_max(Expression* left, Expression* right) | |
574 | { | |
575 | return new Max_expression(left, right); | |
576 | } | |
577 | ||
578 | // Min function. | |
579 | ||
580 | class 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 | ||
607 | extern "C" Expression* | |
608 | script_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 | ||
616 | class 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 | ||
644 | uint64_t | |
645 | Section_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 | ||
3edc73f2 ILT |
659 | // ABSOLUTE function. |
660 | ||
661 | class Absolute_expression : public Unary_expression | |
662 | { | |
663 | public: | |
664 | Absolute_expression(Expression* arg) | |
665 | : Unary_expression(arg) | |
666 | { } | |
667 | ||
668 | uint64_t | |
669 | value(const Expression_eval_info* eei) | |
670 | { | |
671 | Output_section* dummy; | |
672 | uint64_t ret = this->arg_value(eei, &dummy); | |
673 | // Force the value to be absolute. | |
674 | *eei->result_section_pointer = NULL; | |
675 | return ret; | |
676 | } | |
677 | ||
678 | void | |
679 | print(FILE* f) const | |
680 | { | |
681 | fprintf(f, "ABSOLUTE("); | |
682 | this->arg_print(f); | |
683 | fprintf(f, ")"); | |
684 | } | |
685 | }; | |
686 | ||
687 | extern "C" Expression* | |
688 | script_exp_function_absolute(Expression* arg) | |
689 | { | |
690 | return new Absolute_expression(arg); | |
691 | } | |
692 | ||
693 | // ALIGN function. | |
e5756efb ILT |
694 | |
695 | class Align_expression : public Binary_expression | |
696 | { | |
697 | public: | |
698 | Align_expression(Expression* left, Expression* right) | |
699 | : Binary_expression(left, right) | |
700 | { } | |
701 | ||
702 | uint64_t | |
703 | value(const Expression_eval_info* eei) | |
704 | { | |
77e65537 ILT |
705 | Output_section* align_section; |
706 | uint64_t align = this->right_value(eei, &align_section); | |
707 | if (align_section != NULL | |
708 | && parameters->output_is_object()) | |
709 | gold_warning(_("aligning to section relative value")); | |
710 | ||
711 | uint64_t value = this->left_value(eei, eei->result_section_pointer); | |
e5756efb ILT |
712 | if (align <= 1) |
713 | return value; | |
714 | return ((value + align - 1) / align) * align; | |
715 | } | |
494e05f4 ILT |
716 | |
717 | void | |
718 | print(FILE* f) const | |
719 | { this->print_function(f, "ALIGN"); } | |
e5756efb ILT |
720 | }; |
721 | ||
722 | extern "C" Expression* | |
723 | script_exp_function_align(Expression* left, Expression* right) | |
724 | { | |
725 | return new Align_expression(left, right); | |
726 | } | |
727 | ||
3edc73f2 | 728 | // ASSERT function. |
e5756efb ILT |
729 | |
730 | class Assert_expression : public Unary_expression | |
731 | { | |
732 | public: | |
733 | Assert_expression(Expression* arg, const char* message, size_t length) | |
734 | : Unary_expression(arg), message_(message, length) | |
735 | { } | |
736 | ||
737 | uint64_t | |
738 | value(const Expression_eval_info* eei) | |
739 | { | |
77e65537 | 740 | uint64_t value = this->arg_value(eei, eei->result_section_pointer); |
e5756efb ILT |
741 | if (!value) |
742 | gold_error("%s", this->message_.c_str()); | |
743 | return value; | |
744 | } | |
745 | ||
494e05f4 ILT |
746 | void |
747 | print(FILE* f) const | |
748 | { | |
749 | fprintf(f, "ASSERT("); | |
750 | this->arg_print(f); | |
751 | fprintf(f, ", %s)", this->message_.c_str()); | |
752 | } | |
753 | ||
e5756efb ILT |
754 | private: |
755 | std::string message_; | |
756 | }; | |
757 | ||
758 | extern "C" Expression* | |
759 | script_exp_function_assert(Expression* expr, const char* message, | |
760 | size_t length) | |
761 | { | |
762 | return new Assert_expression(expr, message, length); | |
763 | } | |
764 | ||
7508a093 | 765 | // ADDR function. |
494e05f4 | 766 | |
7508a093 | 767 | class Addr_expression : public Section_expression |
494e05f4 ILT |
768 | { |
769 | public: | |
770 | Addr_expression(const char* section_name, size_t section_name_len) | |
7508a093 | 771 | : Section_expression(section_name, section_name_len) |
494e05f4 ILT |
772 | { } |
773 | ||
7508a093 | 774 | protected: |
494e05f4 | 775 | uint64_t |
3edc73f2 | 776 | value_from_output_section(const Expression_eval_info* eei, |
7508a093 ILT |
777 | Output_section* os) |
778 | { | |
779 | *eei->result_section_pointer = os; | |
780 | return os->address(); | |
781 | } | |
494e05f4 | 782 | |
7508a093 ILT |
783 | const char* |
784 | function_name() const | |
785 | { return "ADDR"; } | |
494e05f4 ILT |
786 | }; |
787 | ||
494e05f4 ILT |
788 | extern "C" Expression* |
789 | script_exp_function_addr(const char* section_name, size_t section_name_len) | |
790 | { | |
791 | return new Addr_expression(section_name, section_name_len); | |
792 | } | |
793 | ||
3edc73f2 ILT |
794 | // ALIGNOF. |
795 | ||
796 | class Alignof_expression : public Section_expression | |
797 | { | |
798 | public: | |
799 | Alignof_expression(const char* section_name, size_t section_name_len) | |
800 | : Section_expression(section_name, section_name_len) | |
801 | { } | |
802 | ||
803 | protected: | |
804 | uint64_t | |
805 | value_from_output_section(const Expression_eval_info*, | |
806 | Output_section* os) | |
807 | { return os->addralign(); } | |
808 | ||
809 | const char* | |
810 | function_name() const | |
811 | { return "ALIGNOF"; } | |
812 | }; | |
813 | ||
814 | extern "C" Expression* | |
815 | script_exp_function_alignof(const char* section_name, size_t section_name_len) | |
816 | { | |
817 | return new Alignof_expression(section_name, section_name_len); | |
818 | } | |
819 | ||
3802b2dd ILT |
820 | // CONSTANT. It would be nice if we could simply evaluate this |
821 | // immediately and return an Integer_expression, but unfortunately we | |
822 | // don't know the target. | |
823 | ||
824 | class Constant_expression : public Expression | |
825 | { | |
826 | public: | |
827 | Constant_expression(const char* name, size_t length); | |
828 | ||
829 | uint64_t | |
830 | value(const Expression_eval_info*); | |
831 | ||
832 | void | |
833 | print(FILE* f) const; | |
834 | ||
835 | private: | |
836 | enum Constant_function | |
837 | { | |
838 | CONSTANT_MAXPAGESIZE, | |
839 | CONSTANT_COMMONPAGESIZE | |
840 | }; | |
e5756efb | 841 | |
3802b2dd ILT |
842 | Constant_function function_; |
843 | }; | |
844 | ||
845 | Constant_expression::Constant_expression(const char* name, size_t length) | |
846 | { | |
847 | if (length == 11 && strncmp(name, "MAXPAGESIZE", length) == 0) | |
848 | this->function_ = CONSTANT_MAXPAGESIZE; | |
849 | else if (length == 14 && strncmp(name, "COMMONPAGESIZE", length) == 0) | |
850 | this->function_ = CONSTANT_COMMONPAGESIZE; | |
851 | else | |
852 | { | |
853 | std::string s(name, length); | |
854 | gold_error(_("unknown constant %s"), s.c_str()); | |
855 | this->function_ = CONSTANT_MAXPAGESIZE; | |
856 | } | |
857 | } | |
858 | ||
859 | uint64_t | |
860 | Constant_expression::value(const Expression_eval_info*) | |
861 | { | |
862 | switch (this->function_) | |
863 | { | |
864 | case CONSTANT_MAXPAGESIZE: | |
865 | return parameters->target()->abi_pagesize(); | |
866 | case CONSTANT_COMMONPAGESIZE: | |
867 | return parameters->target()->common_pagesize(); | |
868 | default: | |
869 | gold_unreachable(); | |
870 | } | |
871 | } | |
872 | ||
873 | void | |
874 | Constant_expression::print(FILE* f) const | |
875 | { | |
876 | const char* name; | |
877 | switch (this->function_) | |
878 | { | |
879 | case CONSTANT_MAXPAGESIZE: | |
880 | name = "MAXPAGESIZE"; | |
881 | break; | |
882 | case CONSTANT_COMMONPAGESIZE: | |
883 | name = "COMMONPAGESIZE"; | |
884 | break; | |
885 | default: | |
886 | gold_unreachable(); | |
887 | } | |
888 | fprintf(f, "CONSTANT(%s)", name); | |
889 | } | |
890 | ||
e5756efb | 891 | extern "C" Expression* |
3802b2dd | 892 | script_exp_function_constant(const char* name, size_t length) |
e5756efb | 893 | { |
3802b2dd | 894 | return new Constant_expression(name, length); |
e5756efb ILT |
895 | } |
896 | ||
3802b2dd ILT |
897 | // DATA_SEGMENT_ALIGN. FIXME: we don't implement this; we always fall |
898 | // back to the general case. | |
899 | ||
e5756efb | 900 | extern "C" Expression* |
3802b2dd | 901 | script_exp_function_data_segment_align(Expression* left, Expression*) |
e5756efb | 902 | { |
3802b2dd ILT |
903 | Expression* e1 = script_exp_function_align(script_exp_string(".", 1), left); |
904 | Expression* e2 = script_exp_binary_sub(left, script_exp_integer(1)); | |
905 | Expression* e3 = script_exp_binary_bitwise_and(script_exp_string(".", 1), | |
906 | e2); | |
907 | return script_exp_binary_add(e1, e3); | |
e5756efb ILT |
908 | } |
909 | ||
3802b2dd ILT |
910 | // DATA_SEGMENT_RELRO. FIXME: This is not implemented. |
911 | ||
e5756efb | 912 | extern "C" Expression* |
3802b2dd | 913 | script_exp_function_data_segment_relro_end(Expression*, Expression* right) |
e5756efb | 914 | { |
3802b2dd | 915 | return right; |
e5756efb ILT |
916 | } |
917 | ||
3802b2dd ILT |
918 | // DATA_SEGMENT_END. FIXME: This is not implemented. |
919 | ||
e5756efb | 920 | extern "C" Expression* |
3802b2dd | 921 | script_exp_function_data_segment_end(Expression* val) |
e5756efb | 922 | { |
3802b2dd ILT |
923 | return val; |
924 | } | |
925 | ||
3edc73f2 ILT |
926 | // DEFINED function. |
927 | ||
928 | class Defined_expression : public Expression | |
929 | { | |
930 | public: | |
931 | Defined_expression(const char* symbol_name, size_t symbol_name_len) | |
932 | : symbol_name_(symbol_name, symbol_name_len) | |
933 | { } | |
934 | ||
935 | uint64_t | |
936 | value(const Expression_eval_info* eei) | |
937 | { | |
938 | Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str()); | |
939 | return sym != NULL && sym->is_defined(); | |
940 | } | |
941 | ||
942 | void | |
943 | print(FILE* f) const | |
944 | { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); } | |
945 | ||
946 | private: | |
947 | std::string symbol_name_; | |
948 | }; | |
949 | ||
950 | extern "C" Expression* | |
951 | script_exp_function_defined(const char* symbol_name, size_t symbol_name_len) | |
952 | { | |
953 | return new Defined_expression(symbol_name, symbol_name_len); | |
954 | } | |
955 | ||
7508a093 ILT |
956 | // LOADADDR function |
957 | ||
958 | class Loadaddr_expression : public Section_expression | |
959 | { | |
960 | public: | |
961 | Loadaddr_expression(const char* section_name, size_t section_name_len) | |
962 | : Section_expression(section_name, section_name_len) | |
963 | { } | |
964 | ||
965 | protected: | |
966 | uint64_t | |
3edc73f2 | 967 | value_from_output_section(const Expression_eval_info* eei, |
7508a093 ILT |
968 | Output_section* os) |
969 | { | |
970 | if (os->has_load_address()) | |
971 | return os->load_address(); | |
972 | else | |
973 | { | |
974 | *eei->result_section_pointer = os; | |
975 | return os->address(); | |
976 | } | |
977 | } | |
978 | ||
979 | const char* | |
980 | function_name() const | |
981 | { return "LOADADDR"; } | |
982 | }; | |
983 | ||
984 | extern "C" Expression* | |
985 | script_exp_function_loadaddr(const char* section_name, size_t section_name_len) | |
986 | { | |
987 | return new Loadaddr_expression(section_name, section_name_len); | |
988 | } | |
989 | ||
990 | // SIZEOF function | |
991 | ||
992 | class Sizeof_expression : public Section_expression | |
993 | { | |
994 | public: | |
995 | Sizeof_expression(const char* section_name, size_t section_name_len) | |
996 | : Section_expression(section_name, section_name_len) | |
997 | { } | |
998 | ||
999 | protected: | |
1000 | uint64_t | |
3edc73f2 | 1001 | value_from_output_section(const Expression_eval_info*, |
7508a093 ILT |
1002 | Output_section* os) |
1003 | { | |
1004 | // We can not use data_size here, as the size of the section may | |
1005 | // not have been finalized. Instead we get whatever the current | |
1006 | // size is. This will work correctly for backward references in | |
1007 | // linker scripts. | |
1008 | return os->current_data_size(); | |
1009 | } | |
1010 | ||
1011 | const char* | |
1012 | function_name() const | |
1013 | { return "SIZEOF"; } | |
1014 | }; | |
1015 | ||
1016 | extern "C" Expression* | |
1017 | script_exp_function_sizeof(const char* section_name, size_t section_name_len) | |
1018 | { | |
1019 | return new Sizeof_expression(section_name, section_name_len); | |
1020 | } | |
1021 | ||
3802b2dd ILT |
1022 | // SIZEOF_HEADERS. |
1023 | ||
1024 | class Sizeof_headers_expression : public Expression | |
1025 | { | |
1026 | public: | |
1027 | Sizeof_headers_expression() | |
1028 | { } | |
1029 | ||
1030 | uint64_t | |
1031 | value(const Expression_eval_info*); | |
1032 | ||
1033 | void | |
1034 | print(FILE* f) const | |
1035 | { fprintf(f, "SIZEOF_HEADERS"); } | |
1036 | }; | |
1037 | ||
1038 | uint64_t | |
1039 | Sizeof_headers_expression::value(const Expression_eval_info* eei) | |
1040 | { | |
1041 | unsigned int ehdr_size; | |
1042 | unsigned int phdr_size; | |
1043 | if (parameters->get_size() == 32) | |
1044 | { | |
1045 | ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size; | |
1046 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; | |
1047 | } | |
1048 | else if (parameters->get_size() == 64) | |
1049 | { | |
1050 | ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
1051 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; | |
1052 | } | |
1053 | else | |
1054 | gold_unreachable(); | |
1055 | ||
1056 | return ehdr_size + phdr_size * eei->layout->expected_segment_count(); | |
e5756efb ILT |
1057 | } |
1058 | ||
e5756efb | 1059 | extern "C" Expression* |
3802b2dd | 1060 | script_exp_function_sizeof_headers() |
e5756efb | 1061 | { |
3802b2dd | 1062 | return new Sizeof_headers_expression(); |
e5756efb ILT |
1063 | } |
1064 | ||
3edc73f2 ILT |
1065 | // In the GNU linker SEGMENT_START basically returns the value for |
1066 | // -Ttext, -Tdata, or -Tbss. We could implement this by copying the | |
1067 | // values from General_options to Parameters. But I doubt that | |
1068 | // anybody actually uses it. The point of it for the GNU linker was | |
1069 | // because -Ttext set the address of the .text section rather than the | |
1070 | // text segment. In gold -Ttext sets the text segment address anyhow. | |
3802b2dd | 1071 | |
e5756efb | 1072 | extern "C" Expression* |
3edc73f2 | 1073 | script_exp_function_segment_start(const char*, size_t, Expression*) |
e5756efb | 1074 | { |
3edc73f2 | 1075 | gold_fatal(_("SEGMENT_START not implemented")); |
e5756efb ILT |
1076 | } |
1077 | ||
3edc73f2 ILT |
1078 | // Functions for memory regions. These can not be implemented unless |
1079 | // and until we implement memory regions. | |
e5756efb | 1080 | |
e5756efb | 1081 | extern "C" Expression* |
3802b2dd | 1082 | script_exp_function_origin(const char*, size_t) |
e5756efb | 1083 | { |
3802b2dd | 1084 | gold_fatal(_("ORIGIN not implemented")); |
e5756efb ILT |
1085 | } |
1086 | ||
1087 | extern "C" Expression* | |
3802b2dd | 1088 | script_exp_function_length(const char*, size_t) |
e5756efb | 1089 | { |
3802b2dd | 1090 | gold_fatal(_("LENGTH not implemented")); |
e5756efb ILT |
1091 | } |
1092 | ||
e5756efb | 1093 | } // End namespace gold. |