expression.cc 35 KB

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