expression.cc 36 KB

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