ldexp.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /* This module handles expression trees.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
  4. This file is part of the GNU Binutils.
  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. /* This module is in charge of working out the contents of expressions.
  18. It has to keep track of the relative/absness of a symbol etc. This
  19. is done by keeping all values in a struct (an etree_value_type)
  20. which contains a value, a section to which it is relative and a
  21. valid bit. */
  22. #include "sysdep.h"
  23. #include "bfd.h"
  24. #include "bfdlink.h"
  25. #include "ctf-api.h"
  26. #include "ld.h"
  27. #include "ldmain.h"
  28. #include "ldmisc.h"
  29. #include "ldexp.h"
  30. #include "ldlex.h"
  31. #include <ldgram.h>
  32. #include "ldlang.h"
  33. #include "libiberty.h"
  34. #include "safe-ctype.h"
  35. static void exp_fold_tree_1 (etree_type *);
  36. static bfd_vma align_n (bfd_vma, bfd_vma);
  37. segment_type *segments;
  38. struct ldexp_control expld;
  39. /* This structure records symbols for which we need to keep track of
  40. definedness for use in the DEFINED () test. It is also used in
  41. making absolute symbols section relative late in the link. */
  42. struct definedness_hash_entry
  43. {
  44. struct bfd_hash_entry root;
  45. /* If this symbol was assigned from "dot" outside of an output
  46. section statement, the section we'd like it relative to. */
  47. asection *final_sec;
  48. /* Low bits of iteration count. Symbols with matching iteration have
  49. been defined in this pass over the script. */
  50. unsigned int iteration : 8;
  51. /* Symbol was defined by an object file. */
  52. unsigned int by_object : 1;
  53. };
  54. static struct bfd_hash_table definedness_table;
  55. /* Print the string representation of the given token. Surround it
  56. with spaces if INFIX_P is TRUE. */
  57. static void
  58. exp_print_token (token_code_type code, int infix_p)
  59. {
  60. static const struct
  61. {
  62. token_code_type code;
  63. const char *name;
  64. }
  65. table[] =
  66. {
  67. { INT, "int" },
  68. { NAME, "NAME" },
  69. { PLUSEQ, "+=" },
  70. { MINUSEQ, "-=" },
  71. { MULTEQ, "*=" },
  72. { DIVEQ, "/=" },
  73. { LSHIFTEQ, "<<=" },
  74. { RSHIFTEQ, ">>=" },
  75. { ANDEQ, "&=" },
  76. { OREQ, "|=" },
  77. { OROR, "||" },
  78. { ANDAND, "&&" },
  79. { EQ, "==" },
  80. { NE, "!=" },
  81. { LE, "<=" },
  82. { GE, ">=" },
  83. { LSHIFT, "<<" },
  84. { RSHIFT, ">>" },
  85. { LOG2CEIL, "LOG2CEIL" },
  86. { ALIGN_K, "ALIGN" },
  87. { BLOCK, "BLOCK" },
  88. { QUAD, "QUAD" },
  89. { SQUAD, "SQUAD" },
  90. { LONG, "LONG" },
  91. { SHORT, "SHORT" },
  92. { BYTE, "BYTE" },
  93. { SECTIONS, "SECTIONS" },
  94. { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
  95. { MEMORY, "MEMORY" },
  96. { DEFINED, "DEFINED" },
  97. { TARGET_K, "TARGET" },
  98. { SEARCH_DIR, "SEARCH_DIR" },
  99. { MAP, "MAP" },
  100. { ENTRY, "ENTRY" },
  101. { NEXT, "NEXT" },
  102. { ALIGNOF, "ALIGNOF" },
  103. { SIZEOF, "SIZEOF" },
  104. { ADDR, "ADDR" },
  105. { LOADADDR, "LOADADDR" },
  106. { CONSTANT, "CONSTANT" },
  107. { ABSOLUTE, "ABSOLUTE" },
  108. { MAX_K, "MAX" },
  109. { MIN_K, "MIN" },
  110. { ASSERT_K, "ASSERT" },
  111. { REL, "relocatable" },
  112. { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
  113. { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
  114. { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
  115. { ORIGIN, "ORIGIN" },
  116. { LENGTH, "LENGTH" },
  117. { SEGMENT_START, "SEGMENT_START" }
  118. };
  119. unsigned int idx;
  120. for (idx = 0; idx < ARRAY_SIZE (table); idx++)
  121. if (table[idx].code == code)
  122. break;
  123. if (infix_p)
  124. fputc (' ', config.map_file);
  125. if (idx < ARRAY_SIZE (table))
  126. fputs (table[idx].name, config.map_file);
  127. else if (code < 127)
  128. fputc (code, config.map_file);
  129. else
  130. fprintf (config.map_file, "<code %d>", code);
  131. if (infix_p)
  132. fputc (' ', config.map_file);
  133. }
  134. static void
  135. make_log2ceil (void)
  136. {
  137. bfd_vma value = expld.result.value;
  138. bfd_vma result = -1;
  139. bool round_up = false;
  140. do
  141. {
  142. result++;
  143. /* If more than one bit is set in the value we will need to round up. */
  144. if ((value > 1) && (value & 1))
  145. round_up = true;
  146. }
  147. while (value >>= 1);
  148. if (round_up)
  149. result += 1;
  150. expld.result.section = NULL;
  151. expld.result.value = result;
  152. }
  153. static void
  154. make_abs (void)
  155. {
  156. if (expld.result.section != NULL)
  157. expld.result.value += expld.result.section->vma;
  158. expld.result.section = bfd_abs_section_ptr;
  159. expld.rel_from_abs = false;
  160. }
  161. static void
  162. new_abs (bfd_vma value)
  163. {
  164. expld.result.valid_p = true;
  165. expld.result.section = bfd_abs_section_ptr;
  166. expld.result.value = value;
  167. expld.result.str = NULL;
  168. }
  169. etree_type *
  170. exp_intop (bfd_vma value)
  171. {
  172. etree_type *new_e = stat_alloc (sizeof (new_e->value));
  173. new_e->type.node_code = INT;
  174. new_e->type.filename = ldlex_filename ();
  175. new_e->type.lineno = lineno;
  176. new_e->value.value = value;
  177. new_e->value.str = NULL;
  178. new_e->type.node_class = etree_value;
  179. return new_e;
  180. }
  181. etree_type *
  182. exp_bigintop (bfd_vma value, char *str)
  183. {
  184. etree_type *new_e = stat_alloc (sizeof (new_e->value));
  185. new_e->type.node_code = INT;
  186. new_e->type.filename = ldlex_filename ();
  187. new_e->type.lineno = lineno;
  188. new_e->value.value = value;
  189. new_e->value.str = str;
  190. new_e->type.node_class = etree_value;
  191. return new_e;
  192. }
  193. /* Build an expression representing an unnamed relocatable value. */
  194. etree_type *
  195. exp_relop (asection *section, bfd_vma value)
  196. {
  197. etree_type *new_e = stat_alloc (sizeof (new_e->rel));
  198. new_e->type.node_code = REL;
  199. new_e->type.filename = ldlex_filename ();
  200. new_e->type.lineno = lineno;
  201. new_e->type.node_class = etree_rel;
  202. new_e->rel.section = section;
  203. new_e->rel.value = value;
  204. return new_e;
  205. }
  206. static void
  207. new_number (bfd_vma value)
  208. {
  209. expld.result.valid_p = true;
  210. expld.result.value = value;
  211. expld.result.str = NULL;
  212. expld.result.section = NULL;
  213. }
  214. static void
  215. new_rel (bfd_vma value, asection *section)
  216. {
  217. expld.result.valid_p = true;
  218. expld.result.value = value;
  219. expld.result.str = NULL;
  220. expld.result.section = section;
  221. }
  222. static void
  223. new_rel_from_abs (bfd_vma value)
  224. {
  225. asection *s = expld.section;
  226. expld.rel_from_abs = true;
  227. expld.result.valid_p = true;
  228. expld.result.value = value - s->vma;
  229. expld.result.str = NULL;
  230. expld.result.section = s;
  231. }
  232. /* New-function for the definedness hash table. */
  233. static struct bfd_hash_entry *
  234. definedness_newfunc (struct bfd_hash_entry *entry,
  235. struct bfd_hash_table *table ATTRIBUTE_UNUSED,
  236. const char *name ATTRIBUTE_UNUSED)
  237. {
  238. struct definedness_hash_entry *ret = (struct definedness_hash_entry *) entry;
  239. if (ret == NULL)
  240. ret = (struct definedness_hash_entry *)
  241. bfd_hash_allocate (table, sizeof (struct definedness_hash_entry));
  242. if (ret == NULL)
  243. einfo (_("%F%P: bfd_hash_allocate failed creating symbol %s\n"), name);
  244. ret->by_object = 0;
  245. ret->iteration = 0;
  246. return &ret->root;
  247. }
  248. /* Called during processing of linker script script expressions.
  249. For symbols assigned in a linker script, return a struct describing
  250. where the symbol is defined relative to the current expression,
  251. otherwise return NULL. */
  252. static struct definedness_hash_entry *
  253. symbol_defined (const char *name)
  254. {
  255. return ((struct definedness_hash_entry *)
  256. bfd_hash_lookup (&definedness_table, name, false, false));
  257. }
  258. /* Update the definedness state of NAME. Return FALSE if script symbol
  259. is multiply defining a strong symbol in an object. */
  260. static bool
  261. update_definedness (const char *name, struct bfd_link_hash_entry *h)
  262. {
  263. bool ret;
  264. struct definedness_hash_entry *defentry
  265. = (struct definedness_hash_entry *)
  266. bfd_hash_lookup (&definedness_table, name, true, false);
  267. if (defentry == NULL)
  268. einfo (_("%F%P: bfd_hash_lookup failed creating symbol %s\n"), name);
  269. /* If the symbol was already defined, and not by a script, then it
  270. must be defined by an object file or by the linker target code. */
  271. ret = true;
  272. if (!h->ldscript_def
  273. && (h->type == bfd_link_hash_defined
  274. || h->type == bfd_link_hash_defweak
  275. || h->type == bfd_link_hash_common))
  276. {
  277. defentry->by_object = 1;
  278. if (h->type == bfd_link_hash_defined
  279. && h->u.def.section->output_section != NULL
  280. && !bfd_is_abs_section (h->u.def.section)
  281. && !h->linker_def)
  282. ret = false;
  283. }
  284. defentry->iteration = lang_statement_iteration;
  285. defentry->final_sec = bfd_abs_section_ptr;
  286. if (expld.phase == lang_final_phase_enum
  287. && expld.rel_from_abs
  288. && expld.result.section == bfd_abs_section_ptr)
  289. defentry->final_sec = section_for_dot ();
  290. return ret;
  291. }
  292. static void
  293. fold_segment_end (void)
  294. {
  295. seg_align_type *seg = &expld.dataseg;
  296. if (expld.phase == lang_first_phase_enum
  297. || expld.section != bfd_abs_section_ptr)
  298. {
  299. expld.result.valid_p = false;
  300. }
  301. else if (seg->phase == exp_seg_align_seen
  302. || seg->phase == exp_seg_relro_seen)
  303. {
  304. seg->phase = exp_seg_end_seen;
  305. seg->end = expld.result.value;
  306. }
  307. else if (seg->phase == exp_seg_done
  308. || seg->phase == exp_seg_adjust
  309. || seg->phase == exp_seg_relro_adjust)
  310. {
  311. /* OK. */
  312. }
  313. else
  314. expld.result.valid_p = false;
  315. }
  316. static void
  317. fold_unary (etree_type *tree)
  318. {
  319. exp_fold_tree_1 (tree->unary.child);
  320. if (expld.result.valid_p)
  321. {
  322. switch (tree->type.node_code)
  323. {
  324. case ALIGN_K:
  325. if (expld.phase != lang_first_phase_enum)
  326. new_rel_from_abs (align_n (expld.dot, expld.result.value));
  327. else
  328. expld.result.valid_p = false;
  329. break;
  330. case ABSOLUTE:
  331. make_abs ();
  332. break;
  333. case LOG2CEIL:
  334. make_log2ceil ();
  335. break;
  336. case '~':
  337. expld.result.value = ~expld.result.value;
  338. break;
  339. case '!':
  340. expld.result.value = !expld.result.value;
  341. break;
  342. case '-':
  343. expld.result.value = -expld.result.value;
  344. break;
  345. case NEXT:
  346. /* Return next place aligned to value. */
  347. if (expld.phase != lang_first_phase_enum)
  348. {
  349. make_abs ();
  350. expld.result.value = align_n (expld.dot, expld.result.value);
  351. }
  352. else
  353. expld.result.valid_p = false;
  354. break;
  355. case DATA_SEGMENT_END:
  356. fold_segment_end ();
  357. break;
  358. default:
  359. FAIL ();
  360. break;
  361. }
  362. }
  363. }
  364. /* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
  365. section of one of their operands only when the other operand is a
  366. plain number. Losing the section when operating on two symbols,
  367. ie. a result of a plain number, is required for subtraction and
  368. XOR. It's justifiable for the other operations on the grounds that
  369. adding, multiplying etc. two section relative values does not
  370. really make sense unless they are just treated as numbers.
  371. The same argument could be made for many expressions involving one
  372. symbol and a number. For example, "1 << x" and "100 / x" probably
  373. should not be given the section of x. The trouble is that if we
  374. fuss about such things the rules become complex and it is onerous
  375. to document ld expression evaluation. */
  376. static void
  377. arith_result_section (const etree_value_type *lhs)
  378. {
  379. if (expld.result.section == lhs->section)
  380. {
  381. if (expld.section == bfd_abs_section_ptr
  382. && !config.sane_expr)
  383. /* Duplicate the insanity in exp_fold_tree_1 case etree_value. */
  384. expld.result.section = bfd_abs_section_ptr;
  385. else
  386. expld.result.section = NULL;
  387. }
  388. }
  389. static void
  390. fold_segment_align (etree_value_type *lhs)
  391. {
  392. seg_align_type *seg = &expld.dataseg;
  393. seg->relro = exp_seg_relro_start;
  394. if (expld.phase == lang_first_phase_enum
  395. || expld.section != bfd_abs_section_ptr)
  396. expld.result.valid_p = false;
  397. else
  398. {
  399. bfd_vma maxpage = lhs->value;
  400. bfd_vma commonpage = expld.result.value;
  401. expld.result.value = align_n (expld.dot, maxpage);
  402. if (seg->phase == exp_seg_relro_adjust)
  403. expld.result.value = seg->base;
  404. else if (seg->phase == exp_seg_adjust)
  405. {
  406. if (commonpage < maxpage)
  407. expld.result.value += ((expld.dot + commonpage - 1)
  408. & (maxpage - commonpage));
  409. }
  410. else
  411. {
  412. if (!link_info.relro)
  413. expld.result.value += expld.dot & (maxpage - 1);
  414. if (seg->phase == exp_seg_done)
  415. {
  416. /* OK. */
  417. }
  418. else if (seg->phase == exp_seg_none)
  419. {
  420. seg->phase = exp_seg_align_seen;
  421. seg->base = expld.result.value;
  422. seg->commonpagesize = commonpage;
  423. seg->maxpagesize = maxpage;
  424. seg->relropagesize = maxpage;
  425. seg->relro_end = 0;
  426. }
  427. else
  428. expld.result.valid_p = false;
  429. }
  430. }
  431. }
  432. static void
  433. fold_segment_relro_end (etree_value_type *lhs)
  434. {
  435. seg_align_type *seg = &expld.dataseg;
  436. /* Operands swapped! XXX_SEGMENT_RELRO_END(offset,exp) has offset
  437. in expld.result and exp in lhs. */
  438. seg->relro = exp_seg_relro_end;
  439. seg->relro_offset = expld.result.value;
  440. if (expld.phase == lang_first_phase_enum
  441. || expld.section != bfd_abs_section_ptr)
  442. expld.result.valid_p = false;
  443. else if (seg->phase == exp_seg_align_seen
  444. || seg->phase == exp_seg_adjust
  445. || seg->phase == exp_seg_relro_adjust
  446. || seg->phase == exp_seg_done)
  447. {
  448. if (seg->phase == exp_seg_align_seen
  449. || seg->phase == exp_seg_relro_adjust)
  450. seg->relro_end = lhs->value + expld.result.value;
  451. if (seg->phase == exp_seg_relro_adjust
  452. && (seg->relro_end & (seg->relropagesize - 1)))
  453. {
  454. seg->relro_end += seg->relropagesize - 1;
  455. seg->relro_end &= ~(seg->relropagesize - 1);
  456. expld.result.value = seg->relro_end - expld.result.value;
  457. }
  458. else
  459. expld.result.value = lhs->value;
  460. if (seg->phase == exp_seg_align_seen)
  461. seg->phase = exp_seg_relro_seen;
  462. }
  463. else
  464. expld.result.valid_p = false;
  465. }
  466. static void
  467. fold_binary (etree_type *tree)
  468. {
  469. etree_value_type lhs;
  470. exp_fold_tree_1 (tree->binary.lhs);
  471. /* The SEGMENT_START operator is special because its first
  472. operand is a string, not the name of a symbol. Note that the
  473. operands have been swapped, so binary.lhs is second (default)
  474. operand, binary.rhs is first operand. */
  475. if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
  476. {
  477. bfd_vma value = expld.result.value;
  478. const char *segment_name;
  479. segment_type *seg;
  480. /* Check to see if the user has overridden the default
  481. value. */
  482. segment_name = tree->binary.rhs->name.name;
  483. for (seg = segments; seg; seg = seg->next)
  484. if (strcmp (seg->name, segment_name) == 0)
  485. {
  486. if (!seg->used
  487. && config.magic_demand_paged
  488. && link_info.maxpagesize != 0
  489. && (seg->value % link_info.maxpagesize) != 0)
  490. einfo (_("%P: warning: address of `%s' "
  491. "isn't multiple of maximum page size\n"),
  492. segment_name);
  493. seg->used = true;
  494. value = seg->value;
  495. break;
  496. }
  497. new_rel_from_abs (value);
  498. return;
  499. }
  500. lhs = expld.result;
  501. exp_fold_tree_1 (tree->binary.rhs);
  502. expld.result.valid_p &= lhs.valid_p;
  503. if (expld.result.valid_p)
  504. {
  505. if (lhs.section != expld.result.section)
  506. {
  507. /* If the values are from different sections, and neither is
  508. just a number, make both the source arguments absolute. */
  509. if (expld.result.section != NULL
  510. && lhs.section != NULL)
  511. {
  512. make_abs ();
  513. lhs.value += lhs.section->vma;
  514. lhs.section = bfd_abs_section_ptr;
  515. }
  516. /* If the rhs is just a number, keep the lhs section. */
  517. else if (expld.result.section == NULL)
  518. {
  519. expld.result.section = lhs.section;
  520. /* Make this NULL so that we know one of the operands
  521. was just a number, for later tests. */
  522. lhs.section = NULL;
  523. }
  524. }
  525. /* At this point we know that both operands have the same
  526. section, or at least one of them is a plain number. */
  527. switch (tree->type.node_code)
  528. {
  529. #define BOP(x, y) \
  530. case x: \
  531. expld.result.value = lhs.value y expld.result.value; \
  532. arith_result_section (&lhs); \
  533. break;
  534. /* Comparison operators, logical AND, and logical OR always
  535. return a plain number. */
  536. #define BOPN(x, y) \
  537. case x: \
  538. expld.result.value = lhs.value y expld.result.value; \
  539. expld.result.section = NULL; \
  540. break;
  541. BOP ('+', +);
  542. BOP ('*', *);
  543. BOP ('-', -);
  544. BOP (LSHIFT, <<);
  545. BOP (RSHIFT, >>);
  546. BOP ('&', &);
  547. BOP ('^', ^);
  548. BOP ('|', |);
  549. BOPN (EQ, ==);
  550. BOPN (NE, !=);
  551. BOPN ('<', <);
  552. BOPN ('>', >);
  553. BOPN (LE, <=);
  554. BOPN (GE, >=);
  555. BOPN (ANDAND, &&);
  556. BOPN (OROR, ||);
  557. case '%':
  558. if (expld.result.value != 0)
  559. expld.result.value = ((bfd_signed_vma) lhs.value
  560. % (bfd_signed_vma) expld.result.value);
  561. else if (expld.phase != lang_mark_phase_enum)
  562. einfo (_("%F%P:%pS %% by zero\n"), tree->binary.rhs);
  563. arith_result_section (&lhs);
  564. break;
  565. case '/':
  566. if (expld.result.value != 0)
  567. expld.result.value = ((bfd_signed_vma) lhs.value
  568. / (bfd_signed_vma) expld.result.value);
  569. else if (expld.phase != lang_mark_phase_enum)
  570. einfo (_("%F%P:%pS / by zero\n"), tree->binary.rhs);
  571. arith_result_section (&lhs);
  572. break;
  573. case MAX_K:
  574. if (lhs.value > expld.result.value)
  575. expld.result.value = lhs.value;
  576. break;
  577. case MIN_K:
  578. if (lhs.value < expld.result.value)
  579. expld.result.value = lhs.value;
  580. break;
  581. case ALIGN_K:
  582. expld.result.value = align_n (lhs.value, expld.result.value);
  583. break;
  584. case DATA_SEGMENT_ALIGN:
  585. fold_segment_align (&lhs);
  586. break;
  587. case DATA_SEGMENT_RELRO_END:
  588. fold_segment_relro_end (&lhs);
  589. break;
  590. default:
  591. FAIL ();
  592. }
  593. }
  594. }
  595. static void
  596. fold_trinary (etree_type *tree)
  597. {
  598. struct bfd_link_hash_entry *save = expld.assign_src;
  599. exp_fold_tree_1 (tree->trinary.cond);
  600. expld.assign_src = save;
  601. if (expld.result.valid_p)
  602. exp_fold_tree_1 (expld.result.value
  603. ? tree->trinary.lhs
  604. : tree->trinary.rhs);
  605. }
  606. static void
  607. fold_name (etree_type *tree)
  608. {
  609. struct bfd_link_hash_entry *h;
  610. struct definedness_hash_entry *def;
  611. memset (&expld.result, 0, sizeof (expld.result));
  612. switch (tree->type.node_code)
  613. {
  614. case SIZEOF_HEADERS:
  615. link_info.load_phdrs = 1;
  616. if (expld.phase != lang_first_phase_enum)
  617. {
  618. bfd_vma hdr_size = 0;
  619. /* Don't find the real header size if only marking sections;
  620. The bfd function may cache incorrect data. */
  621. if (expld.phase != lang_mark_phase_enum)
  622. hdr_size = (bfd_sizeof_headers (link_info.output_bfd, &link_info)
  623. / bfd_octets_per_byte (link_info.output_bfd, NULL));
  624. new_number (hdr_size);
  625. }
  626. break;
  627. case DEFINED:
  628. h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
  629. &link_info,
  630. tree->name.name,
  631. false, false, true);
  632. new_number (h != NULL
  633. && (h->type == bfd_link_hash_defined
  634. || h->type == bfd_link_hash_defweak
  635. || h->type == bfd_link_hash_common)
  636. && (!h->ldscript_def
  637. || (def = symbol_defined (tree->name.name)) == NULL
  638. || def->by_object
  639. || def->iteration == (lang_statement_iteration & 255)));
  640. break;
  641. case NAME:
  642. if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
  643. new_rel_from_abs (expld.dot);
  644. else
  645. {
  646. h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
  647. &link_info,
  648. tree->name.name,
  649. true, false, true);
  650. if (!h)
  651. {
  652. if (expld.phase != lang_first_phase_enum)
  653. einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
  654. }
  655. else if (h->type == bfd_link_hash_defined
  656. || h->type == bfd_link_hash_defweak)
  657. {
  658. asection *output_section;
  659. output_section = h->u.def.section->output_section;
  660. if (output_section == NULL)
  661. {
  662. if (expld.phase <= lang_mark_phase_enum)
  663. new_rel (h->u.def.value, h->u.def.section);
  664. else
  665. einfo (_("%X%P:%pS: unresolvable symbol `%s'"
  666. " referenced in expression\n"),
  667. tree, tree->name.name);
  668. }
  669. else if (output_section == bfd_abs_section_ptr
  670. && (expld.section != bfd_abs_section_ptr
  671. || config.sane_expr))
  672. new_number (h->u.def.value + h->u.def.section->output_offset);
  673. else
  674. new_rel (h->u.def.value + h->u.def.section->output_offset,
  675. output_section);
  676. }
  677. else if (expld.phase == lang_final_phase_enum
  678. || (expld.phase != lang_mark_phase_enum
  679. && expld.assigning_to_dot))
  680. einfo (_("%F%P:%pS: undefined symbol `%s'"
  681. " referenced in expression\n"),
  682. tree, tree->name.name);
  683. else if (h->type == bfd_link_hash_new)
  684. {
  685. h->type = bfd_link_hash_undefined;
  686. h->u.undef.abfd = NULL;
  687. if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
  688. bfd_link_add_undef (link_info.hash, h);
  689. }
  690. if (expld.assign_src == NULL)
  691. expld.assign_src = h;
  692. else
  693. expld.assign_src = (struct bfd_link_hash_entry *) - 1;
  694. /* Self-assignment is only allowed for absolute symbols
  695. defined in a linker script. */
  696. if (expld.assign_name != NULL
  697. && strcmp (expld.assign_name, tree->name.name) == 0
  698. && !(h != NULL
  699. && (h->type == bfd_link_hash_defined
  700. || h->type == bfd_link_hash_defweak)
  701. && h->u.def.section == bfd_abs_section_ptr
  702. && (def = symbol_defined (tree->name.name)) != NULL
  703. && def->iteration == (lang_statement_iteration & 255)))
  704. expld.assign_name = NULL;
  705. }
  706. break;
  707. case ADDR:
  708. if (expld.phase != lang_first_phase_enum)
  709. {
  710. lang_output_section_statement_type *os;
  711. os = lang_output_section_find (tree->name.name);
  712. if (os == NULL)
  713. {
  714. if (expld.phase == lang_final_phase_enum)
  715. einfo (_("%F%P:%pS: undefined section `%s'"
  716. " referenced in expression\n"),
  717. tree, tree->name.name);
  718. }
  719. else if (os->processed_vma)
  720. new_rel (0, os->bfd_section);
  721. }
  722. break;
  723. case LOADADDR:
  724. if (expld.phase != lang_first_phase_enum)
  725. {
  726. lang_output_section_statement_type *os;
  727. os = lang_output_section_find (tree->name.name);
  728. if (os == NULL)
  729. {
  730. if (expld.phase == lang_final_phase_enum)
  731. einfo (_("%F%P:%pS: undefined section `%s'"
  732. " referenced in expression\n"),
  733. tree, tree->name.name);
  734. }
  735. else if (os->processed_lma)
  736. {
  737. if (os->load_base == NULL)
  738. new_abs (os->bfd_section->lma);
  739. else
  740. {
  741. exp_fold_tree_1 (os->load_base);
  742. if (expld.result.valid_p)
  743. make_abs ();
  744. }
  745. }
  746. }
  747. break;
  748. case SIZEOF:
  749. case ALIGNOF:
  750. if (expld.phase != lang_first_phase_enum)
  751. {
  752. lang_output_section_statement_type *os;
  753. os = lang_output_section_find (tree->name.name);
  754. if (os == NULL)
  755. {
  756. if (expld.phase == lang_final_phase_enum)
  757. einfo (_("%F%P:%pS: undefined section `%s'"
  758. " referenced in expression\n"),
  759. tree, tree->name.name);
  760. new_number (0);
  761. }
  762. else if (os->bfd_section != NULL)
  763. {
  764. bfd_vma val;
  765. if (tree->type.node_code == SIZEOF)
  766. val = (os->bfd_section->size
  767. / bfd_octets_per_byte (link_info.output_bfd,
  768. os->bfd_section));
  769. else
  770. val = (bfd_vma)1 << os->bfd_section->alignment_power;
  771. new_number (val);
  772. }
  773. else
  774. new_number (0);
  775. }
  776. break;
  777. case LENGTH:
  778. {
  779. lang_memory_region_type *mem;
  780. mem = lang_memory_region_lookup (tree->name.name, false);
  781. if (mem != NULL)
  782. new_number (mem->length);
  783. else
  784. einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
  785. " referenced in expression\n"),
  786. tree, tree->name.name);
  787. }
  788. break;
  789. case ORIGIN:
  790. {
  791. lang_memory_region_type *mem;
  792. mem = lang_memory_region_lookup (tree->name.name, false);
  793. if (mem != NULL)
  794. new_rel_from_abs (mem->origin);
  795. else
  796. einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
  797. " referenced in expression\n"),
  798. tree, tree->name.name);
  799. }
  800. break;
  801. case CONSTANT:
  802. if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
  803. new_number (link_info.maxpagesize);
  804. else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
  805. new_number (link_info.commonpagesize);
  806. else
  807. einfo (_("%F%P:%pS: unknown constant `%s' referenced in expression\n"),
  808. tree, tree->name.name);
  809. break;
  810. default:
  811. FAIL ();
  812. break;
  813. }
  814. }
  815. /* Return true if TREE is '.'. */
  816. static bool
  817. is_dot (const etree_type *tree)
  818. {
  819. return (tree->type.node_class == etree_name
  820. && tree->type.node_code == NAME
  821. && tree->name.name[0] == '.'
  822. && tree->name.name[1] == 0);
  823. }
  824. /* Return true if TREE is a constant equal to VAL. */
  825. static bool
  826. is_value (const etree_type *tree, bfd_vma val)
  827. {
  828. return (tree->type.node_class == etree_value
  829. && tree->value.value == val);
  830. }
  831. /* Return true if TREE is an absolute symbol equal to VAL defined in
  832. a linker script. */
  833. static bool
  834. is_sym_value (const etree_type *tree, bfd_vma val)
  835. {
  836. struct bfd_link_hash_entry *h;
  837. struct definedness_hash_entry *def;
  838. return (tree->type.node_class == etree_name
  839. && tree->type.node_code == NAME
  840. && (def = symbol_defined (tree->name.name)) != NULL
  841. && def->iteration == (lang_statement_iteration & 255)
  842. && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
  843. &link_info,
  844. tree->name.name,
  845. false, false, true)) != NULL
  846. && h->ldscript_def
  847. && h->type == bfd_link_hash_defined
  848. && h->u.def.section == bfd_abs_section_ptr
  849. && h->u.def.value == val);
  850. }
  851. /* Return true if TREE is ". != 0". */
  852. static bool
  853. is_dot_ne_0 (const etree_type *tree)
  854. {
  855. return (tree->type.node_class == etree_binary
  856. && tree->type.node_code == NE
  857. && is_dot (tree->binary.lhs)
  858. && is_value (tree->binary.rhs, 0));
  859. }
  860. /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
  861. absolute constant with value 0 defined in a linker script. */
  862. static bool
  863. is_dot_plus_0 (const etree_type *tree)
  864. {
  865. return (tree->type.node_class == etree_binary
  866. && tree->type.node_code == '+'
  867. && is_dot (tree->binary.lhs)
  868. && (is_value (tree->binary.rhs, 0)
  869. || is_sym_value (tree->binary.rhs, 0)));
  870. }
  871. /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
  872. static bool
  873. is_align_conditional (const etree_type *tree)
  874. {
  875. if (tree->type.node_class == etree_unary
  876. && tree->type.node_code == ALIGN_K)
  877. {
  878. tree = tree->unary.child;
  879. return (tree->type.node_class == etree_trinary
  880. && is_dot_ne_0 (tree->trinary.cond)
  881. && is_value (tree->trinary.rhs, 1));
  882. }
  883. return false;
  884. }
  885. static void
  886. exp_fold_tree_1 (etree_type *tree)
  887. {
  888. if (tree == NULL)
  889. {
  890. memset (&expld.result, 0, sizeof (expld.result));
  891. return;
  892. }
  893. switch (tree->type.node_class)
  894. {
  895. case etree_value:
  896. if (expld.section == bfd_abs_section_ptr
  897. && !config.sane_expr)
  898. new_abs (tree->value.value);
  899. else
  900. new_number (tree->value.value);
  901. expld.result.str = tree->value.str;
  902. break;
  903. case etree_rel:
  904. if (expld.phase != lang_first_phase_enum)
  905. {
  906. asection *output_section = tree->rel.section->output_section;
  907. new_rel (tree->rel.value + tree->rel.section->output_offset,
  908. output_section);
  909. }
  910. else
  911. memset (&expld.result, 0, sizeof (expld.result));
  912. break;
  913. case etree_assert:
  914. exp_fold_tree_1 (tree->assert_s.child);
  915. if (expld.phase == lang_final_phase_enum && !expld.result.value)
  916. einfo ("%X%P: %s\n", tree->assert_s.message);
  917. break;
  918. case etree_unary:
  919. fold_unary (tree);
  920. break;
  921. case etree_binary:
  922. fold_binary (tree);
  923. break;
  924. case etree_trinary:
  925. fold_trinary (tree);
  926. break;
  927. case etree_assign:
  928. case etree_provide:
  929. case etree_provided:
  930. if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
  931. {
  932. if (tree->type.node_class != etree_assign)
  933. einfo (_("%F%P:%pS can not PROVIDE assignment to"
  934. " location counter\n"), tree);
  935. if (expld.phase != lang_first_phase_enum)
  936. {
  937. /* Notify the folder that this is an assignment to dot. */
  938. expld.assigning_to_dot = true;
  939. exp_fold_tree_1 (tree->assign.src);
  940. expld.assigning_to_dot = false;
  941. /* If we are assigning to dot inside an output section
  942. arrange to keep the section, except for certain
  943. expressions that evaluate to zero. We ignore . = 0,
  944. . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
  945. We can't ignore all expressions that evaluate to zero
  946. because an otherwise empty section might have padding
  947. added by an alignment expression that changes with
  948. relaxation. Such a section might have zero size
  949. before relaxation and so be stripped incorrectly. */
  950. if (expld.phase == lang_mark_phase_enum
  951. && expld.section != bfd_abs_section_ptr
  952. && expld.section != bfd_und_section_ptr
  953. && !(expld.result.valid_p
  954. && expld.result.value == 0
  955. && (is_value (tree->assign.src, 0)
  956. || is_sym_value (tree->assign.src, 0)
  957. || is_dot_plus_0 (tree->assign.src)
  958. || is_align_conditional (tree->assign.src))))
  959. expld.section->flags |= SEC_KEEP;
  960. if (!expld.result.valid_p
  961. || expld.section == bfd_und_section_ptr)
  962. {
  963. if (expld.phase != lang_mark_phase_enum)
  964. einfo (_("%F%P:%pS invalid assignment to"
  965. " location counter\n"), tree);
  966. }
  967. else if (expld.dotp == NULL)
  968. einfo (_("%F%P:%pS assignment to location counter"
  969. " invalid outside of SECTIONS\n"), tree);
  970. /* After allocation, assignment to dot should not be
  971. done inside an output section since allocation adds a
  972. padding statement that effectively duplicates the
  973. assignment. */
  974. else if (expld.phase <= lang_allocating_phase_enum
  975. || expld.section == bfd_abs_section_ptr)
  976. {
  977. bfd_vma nextdot;
  978. nextdot = expld.result.value;
  979. if (expld.result.section != NULL)
  980. nextdot += expld.result.section->vma;
  981. else
  982. nextdot += expld.section->vma;
  983. if (nextdot < expld.dot
  984. && expld.section != bfd_abs_section_ptr)
  985. einfo (_("%F%P:%pS cannot move location counter backwards"
  986. " (from %V to %V)\n"),
  987. tree, expld.dot, nextdot);
  988. else
  989. {
  990. expld.dot = nextdot;
  991. *expld.dotp = nextdot;
  992. }
  993. }
  994. }
  995. else
  996. memset (&expld.result, 0, sizeof (expld.result));
  997. }
  998. else
  999. {
  1000. struct bfd_link_hash_entry *h = NULL;
  1001. if (tree->type.node_class == etree_provide)
  1002. {
  1003. h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
  1004. false, false, true);
  1005. if (h == NULL
  1006. || !(h->type == bfd_link_hash_new
  1007. || h->type == bfd_link_hash_undefined
  1008. || h->type == bfd_link_hash_undefweak
  1009. || h->linker_def))
  1010. {
  1011. /* Do nothing. The symbol was never referenced, or
  1012. was defined in some object file. Note that
  1013. undefweak symbols are defined by PROVIDE. This
  1014. is to support glibc use of __rela_iplt_start and
  1015. similar weak references. */
  1016. break;
  1017. }
  1018. }
  1019. expld.assign_name = tree->assign.dst;
  1020. expld.assign_src = NULL;
  1021. exp_fold_tree_1 (tree->assign.src);
  1022. /* expld.assign_name remaining equal to tree->assign.dst
  1023. below indicates the evaluation of tree->assign.src did
  1024. not use the value of tree->assign.dst. We don't allow
  1025. self assignment until the final phase for two reasons:
  1026. 1) Expressions are evaluated multiple times. With
  1027. relaxation, the number of times may vary.
  1028. 2) Section relative symbol values cannot be correctly
  1029. converted to absolute values, as is required by many
  1030. expressions, until final section sizing is complete. */
  1031. if (expld.phase == lang_final_phase_enum
  1032. || expld.phase == lang_fixed_phase_enum
  1033. || expld.assign_name != NULL)
  1034. {
  1035. if (tree->type.node_class == etree_provide)
  1036. tree->type.node_class = etree_provided;
  1037. if (h == NULL)
  1038. {
  1039. h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
  1040. true, false, true);
  1041. if (h == NULL)
  1042. einfo (_("%F%P:%s: hash creation failed\n"),
  1043. tree->assign.dst);
  1044. }
  1045. /* If the expression is not valid then fake a zero value. In
  1046. the final phase any errors will already have been raised,
  1047. in earlier phases we want to create this definition so
  1048. that it can be seen by other expressions. */
  1049. if (!expld.result.valid_p
  1050. && h->type == bfd_link_hash_new)
  1051. {
  1052. expld.result.value = 0;
  1053. expld.result.section = NULL;
  1054. expld.result.valid_p = true;
  1055. }
  1056. if (expld.result.valid_p)
  1057. {
  1058. if (expld.result.section == NULL)
  1059. expld.result.section = expld.section;
  1060. if (!update_definedness (tree->assign.dst, h)
  1061. && expld.assign_name != NULL)
  1062. {
  1063. /* Symbol was already defined, and the script isn't
  1064. modifying the symbol value for some reason as in
  1065. ld-elf/var1 and ld-scripts/pr14962.
  1066. For now this is only a warning. */
  1067. unsigned int warn = link_info.warn_multiple_definition;
  1068. link_info.warn_multiple_definition = 1;
  1069. (*link_info.callbacks->multiple_definition)
  1070. (&link_info, h, link_info.output_bfd,
  1071. expld.result.section, expld.result.value);
  1072. link_info.warn_multiple_definition = warn;
  1073. }
  1074. if (expld.phase == lang_fixed_phase_enum)
  1075. {
  1076. if (h->type == bfd_link_hash_defined)
  1077. {
  1078. expld.result.value = h->u.def.value;
  1079. expld.result.section = h->u.def.section;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. h->type = bfd_link_hash_defined;
  1085. h->u.def.value = expld.result.value;
  1086. h->u.def.section = expld.result.section;
  1087. h->linker_def = ! tree->assign.type.lineno;
  1088. h->ldscript_def = 1;
  1089. h->rel_from_abs = expld.rel_from_abs;
  1090. if (tree->assign.hidden)
  1091. bfd_link_hide_symbol (link_info.output_bfd,
  1092. &link_info, h);
  1093. /* Copy the symbol type and set non_ir_ref_regular
  1094. on the source if this is an expression only
  1095. referencing a single symbol. (If the expression
  1096. contains ternary conditions, ignoring symbols on
  1097. false branches.) */
  1098. if (expld.assign_src != NULL
  1099. && (expld.assign_src
  1100. != (struct bfd_link_hash_entry *) -1))
  1101. {
  1102. bfd_copy_link_hash_symbol_type (link_info.output_bfd,
  1103. h, expld.assign_src);
  1104. expld.assign_src->non_ir_ref_regular = true;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. if (expld.phase != lang_fixed_phase_enum)
  1110. expld.assign_name = NULL;
  1111. }
  1112. break;
  1113. case etree_name:
  1114. fold_name (tree);
  1115. break;
  1116. default:
  1117. FAIL ();
  1118. memset (&expld.result, 0, sizeof (expld.result));
  1119. break;
  1120. }
  1121. }
  1122. void
  1123. exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
  1124. {
  1125. expld.rel_from_abs = false;
  1126. expld.dot = *dotp;
  1127. expld.dotp = dotp;
  1128. expld.section = current_section;
  1129. exp_fold_tree_1 (tree);
  1130. }
  1131. void
  1132. exp_fold_tree_no_dot (etree_type *tree)
  1133. {
  1134. expld.rel_from_abs = false;
  1135. expld.dot = 0;
  1136. expld.dotp = NULL;
  1137. expld.section = bfd_abs_section_ptr;
  1138. exp_fold_tree_1 (tree);
  1139. }
  1140. static void
  1141. exp_value_fold (etree_type *tree)
  1142. {
  1143. exp_fold_tree_no_dot (tree);
  1144. if (expld.result.valid_p)
  1145. {
  1146. tree->type.node_code = INT;
  1147. tree->value.value = expld.result.value;
  1148. tree->value.str = NULL;
  1149. tree->type.node_class = etree_value;
  1150. }
  1151. }
  1152. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  1153. etree_type *
  1154. exp_binop (int code, etree_type *lhs, etree_type *rhs)
  1155. {
  1156. etree_type *new_e = stat_alloc (MAX (sizeof (new_e->binary),
  1157. sizeof (new_e->value)));
  1158. new_e->type.node_code = code;
  1159. new_e->type.filename = lhs->type.filename;
  1160. new_e->type.lineno = lhs->type.lineno;
  1161. new_e->binary.lhs = lhs;
  1162. new_e->binary.rhs = rhs;
  1163. new_e->type.node_class = etree_binary;
  1164. if (lhs->type.node_class == etree_value
  1165. && rhs->type.node_class == etree_value
  1166. && code != ALIGN_K
  1167. && code != DATA_SEGMENT_ALIGN
  1168. && code != DATA_SEGMENT_RELRO_END)
  1169. exp_value_fold (new_e);
  1170. return new_e;
  1171. }
  1172. etree_type *
  1173. exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
  1174. {
  1175. etree_type *new_e = stat_alloc (MAX (sizeof (new_e->trinary),
  1176. sizeof (new_e->value)));
  1177. new_e->type.node_code = code;
  1178. new_e->type.filename = cond->type.filename;
  1179. new_e->type.lineno = cond->type.lineno;
  1180. new_e->trinary.lhs = lhs;
  1181. new_e->trinary.cond = cond;
  1182. new_e->trinary.rhs = rhs;
  1183. new_e->type.node_class = etree_trinary;
  1184. if (cond->type.node_class == etree_value
  1185. && lhs->type.node_class == etree_value
  1186. && rhs->type.node_class == etree_value)
  1187. exp_value_fold (new_e);
  1188. return new_e;
  1189. }
  1190. etree_type *
  1191. exp_unop (int code, etree_type *child)
  1192. {
  1193. etree_type *new_e = stat_alloc (MAX (sizeof (new_e->unary),
  1194. sizeof (new_e->value)));
  1195. new_e->unary.type.node_code = code;
  1196. new_e->unary.type.filename = child->type.filename;
  1197. new_e->unary.type.lineno = child->type.lineno;
  1198. new_e->unary.child = child;
  1199. new_e->unary.type.node_class = etree_unary;
  1200. if (child->type.node_class == etree_value
  1201. && code != ALIGN_K
  1202. && code != ABSOLUTE
  1203. && code != NEXT
  1204. && code != DATA_SEGMENT_END)
  1205. exp_value_fold (new_e);
  1206. return new_e;
  1207. }
  1208. etree_type *
  1209. exp_nameop (int code, const char *name)
  1210. {
  1211. etree_type *new_e = stat_alloc (sizeof (new_e->name));
  1212. new_e->name.type.node_code = code;
  1213. new_e->name.type.filename = ldlex_filename ();
  1214. new_e->name.type.lineno = lineno;
  1215. new_e->name.name = name;
  1216. new_e->name.type.node_class = etree_name;
  1217. return new_e;
  1218. }
  1219. static etree_type *
  1220. exp_assop (const char *dst,
  1221. etree_type *src,
  1222. enum node_tree_enum class,
  1223. bool hidden)
  1224. {
  1225. etree_type *n;
  1226. n = stat_alloc (sizeof (n->assign));
  1227. n->assign.type.node_code = '=';
  1228. n->assign.type.filename = src->type.filename;
  1229. n->assign.type.lineno = src->type.lineno;
  1230. n->assign.type.node_class = class;
  1231. n->assign.src = src;
  1232. n->assign.dst = dst;
  1233. n->assign.hidden = hidden;
  1234. return n;
  1235. }
  1236. /* Handle linker script assignments and HIDDEN. */
  1237. etree_type *
  1238. exp_assign (const char *dst, etree_type *src, bool hidden)
  1239. {
  1240. return exp_assop (dst, src, etree_assign, hidden);
  1241. }
  1242. /* Handle --defsym command-line option. */
  1243. etree_type *
  1244. exp_defsym (const char *dst, etree_type *src)
  1245. {
  1246. return exp_assop (dst, src, etree_assign, false);
  1247. }
  1248. /* Handle PROVIDE. */
  1249. etree_type *
  1250. exp_provide (const char *dst, etree_type *src, bool hidden)
  1251. {
  1252. return exp_assop (dst, src, etree_provide, hidden);
  1253. }
  1254. /* Handle ASSERT. */
  1255. etree_type *
  1256. exp_assert (etree_type *exp, const char *message)
  1257. {
  1258. etree_type *n;
  1259. n = stat_alloc (sizeof (n->assert_s));
  1260. n->assert_s.type.node_code = '!';
  1261. n->assert_s.type.filename = exp->type.filename;
  1262. n->assert_s.type.lineno = exp->type.lineno;
  1263. n->assert_s.type.node_class = etree_assert;
  1264. n->assert_s.child = exp;
  1265. n->assert_s.message = message;
  1266. return n;
  1267. }
  1268. void
  1269. exp_print_tree (etree_type *tree)
  1270. {
  1271. bool function_like;
  1272. if (config.map_file == NULL)
  1273. config.map_file = stderr;
  1274. if (tree == NULL)
  1275. {
  1276. minfo ("NULL TREE\n");
  1277. return;
  1278. }
  1279. switch (tree->type.node_class)
  1280. {
  1281. case etree_value:
  1282. minfo ("0x%v", tree->value.value);
  1283. return;
  1284. case etree_rel:
  1285. if (tree->rel.section->owner != NULL)
  1286. minfo ("%pB:", tree->rel.section->owner);
  1287. minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
  1288. return;
  1289. case etree_assign:
  1290. fputs (tree->assign.dst, config.map_file);
  1291. exp_print_token (tree->type.node_code, true);
  1292. exp_print_tree (tree->assign.src);
  1293. break;
  1294. case etree_provide:
  1295. case etree_provided:
  1296. fprintf (config.map_file, "PROVIDE (%s = ", tree->assign.dst);
  1297. exp_print_tree (tree->assign.src);
  1298. fputc (')', config.map_file);
  1299. break;
  1300. case etree_binary:
  1301. function_like = false;
  1302. switch (tree->type.node_code)
  1303. {
  1304. case MAX_K:
  1305. case MIN_K:
  1306. case ALIGN_K:
  1307. case DATA_SEGMENT_ALIGN:
  1308. case DATA_SEGMENT_RELRO_END:
  1309. function_like = true;
  1310. break;
  1311. case SEGMENT_START:
  1312. /* Special handling because arguments are in reverse order and
  1313. the segment name is quoted. */
  1314. exp_print_token (tree->type.node_code, false);
  1315. fputs (" (\"", config.map_file);
  1316. exp_print_tree (tree->binary.rhs);
  1317. fputs ("\", ", config.map_file);
  1318. exp_print_tree (tree->binary.lhs);
  1319. fputc (')', config.map_file);
  1320. return;
  1321. }
  1322. if (function_like)
  1323. {
  1324. exp_print_token (tree->type.node_code, false);
  1325. fputc (' ', config.map_file);
  1326. }
  1327. fputc ('(', config.map_file);
  1328. exp_print_tree (tree->binary.lhs);
  1329. if (function_like)
  1330. fprintf (config.map_file, ", ");
  1331. else
  1332. exp_print_token (tree->type.node_code, true);
  1333. exp_print_tree (tree->binary.rhs);
  1334. fputc (')', config.map_file);
  1335. break;
  1336. case etree_trinary:
  1337. exp_print_tree (tree->trinary.cond);
  1338. fputc ('?', config.map_file);
  1339. exp_print_tree (tree->trinary.lhs);
  1340. fputc (':', config.map_file);
  1341. exp_print_tree (tree->trinary.rhs);
  1342. break;
  1343. case etree_unary:
  1344. exp_print_token (tree->unary.type.node_code, false);
  1345. if (tree->unary.child)
  1346. {
  1347. fprintf (config.map_file, " (");
  1348. exp_print_tree (tree->unary.child);
  1349. fputc (')', config.map_file);
  1350. }
  1351. break;
  1352. case etree_assert:
  1353. fprintf (config.map_file, "ASSERT (");
  1354. exp_print_tree (tree->assert_s.child);
  1355. fprintf (config.map_file, ", %s)", tree->assert_s.message);
  1356. break;
  1357. case etree_name:
  1358. if (tree->type.node_code == NAME)
  1359. fputs (tree->name.name, config.map_file);
  1360. else
  1361. {
  1362. exp_print_token (tree->type.node_code, false);
  1363. if (tree->name.name)
  1364. fprintf (config.map_file, " (%s)", tree->name.name);
  1365. }
  1366. break;
  1367. default:
  1368. FAIL ();
  1369. break;
  1370. }
  1371. }
  1372. bfd_vma
  1373. exp_get_vma (etree_type *tree, bfd_vma def, char *name)
  1374. {
  1375. if (tree != NULL)
  1376. {
  1377. exp_fold_tree_no_dot (tree);
  1378. if (expld.result.valid_p)
  1379. return expld.result.value;
  1380. else if (name != NULL && expld.phase != lang_mark_phase_enum)
  1381. einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
  1382. tree, name);
  1383. }
  1384. return def;
  1385. }
  1386. /* Return the smallest non-negative integer such that two raised to
  1387. that power is at least as large as the vma evaluated at TREE, if
  1388. TREE is a non-NULL expression that can be resolved. If TREE is
  1389. NULL or cannot be resolved, return -1. */
  1390. int
  1391. exp_get_power (etree_type *tree, char *name)
  1392. {
  1393. bfd_vma x = exp_get_vma (tree, -1, name);
  1394. bfd_vma p2;
  1395. int n;
  1396. if (x == (bfd_vma) -1)
  1397. return -1;
  1398. for (n = 0, p2 = 1; p2 < x; ++n, p2 <<= 1)
  1399. if (p2 == 0)
  1400. break;
  1401. return n;
  1402. }
  1403. fill_type *
  1404. exp_get_fill (etree_type *tree, fill_type *def, char *name)
  1405. {
  1406. fill_type *fill;
  1407. size_t len;
  1408. unsigned int val;
  1409. if (tree == NULL)
  1410. return def;
  1411. exp_fold_tree_no_dot (tree);
  1412. if (!expld.result.valid_p)
  1413. {
  1414. if (name != NULL && expld.phase != lang_mark_phase_enum)
  1415. einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
  1416. tree, name);
  1417. return def;
  1418. }
  1419. if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
  1420. {
  1421. unsigned char *dst;
  1422. unsigned char *s;
  1423. fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
  1424. fill->size = (len + 1) / 2;
  1425. dst = fill->data;
  1426. s = (unsigned char *) expld.result.str;
  1427. val = 0;
  1428. do
  1429. {
  1430. unsigned int digit;
  1431. digit = *s++ - '0';
  1432. if (digit > 9)
  1433. digit = (digit - 'A' + '0' + 10) & 0xf;
  1434. val <<= 4;
  1435. val += digit;
  1436. --len;
  1437. if ((len & 1) == 0)
  1438. {
  1439. *dst++ = val;
  1440. val = 0;
  1441. }
  1442. }
  1443. while (len != 0);
  1444. }
  1445. else
  1446. {
  1447. fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
  1448. val = expld.result.value;
  1449. fill->data[0] = (val >> 24) & 0xff;
  1450. fill->data[1] = (val >> 16) & 0xff;
  1451. fill->data[2] = (val >> 8) & 0xff;
  1452. fill->data[3] = (val >> 0) & 0xff;
  1453. fill->size = 4;
  1454. }
  1455. return fill;
  1456. }
  1457. bfd_vma
  1458. exp_get_abs_int (etree_type *tree, int def, char *name)
  1459. {
  1460. if (tree != NULL)
  1461. {
  1462. exp_fold_tree_no_dot (tree);
  1463. if (expld.result.valid_p)
  1464. {
  1465. if (expld.result.section != NULL)
  1466. expld.result.value += expld.result.section->vma;
  1467. return expld.result.value;
  1468. }
  1469. else if (name != NULL && expld.phase != lang_mark_phase_enum)
  1470. {
  1471. einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
  1472. tree, name);
  1473. }
  1474. }
  1475. return def;
  1476. }
  1477. static bfd_vma
  1478. align_n (bfd_vma value, bfd_vma align)
  1479. {
  1480. if (align <= 1)
  1481. return value;
  1482. value = (value + align - 1) / align;
  1483. return value * align;
  1484. }
  1485. void
  1486. ldexp_init (void)
  1487. {
  1488. /* The value "13" is ad-hoc, somewhat related to the expected number of
  1489. assignments in a linker script. */
  1490. if (!bfd_hash_table_init_n (&definedness_table,
  1491. definedness_newfunc,
  1492. sizeof (struct definedness_hash_entry),
  1493. 13))
  1494. einfo (_("%F%P: can not create hash table: %E\n"));
  1495. }
  1496. /* Convert absolute symbols defined by a script from "dot" (also
  1497. SEGMENT_START or ORIGIN) outside of an output section statement,
  1498. to section relative. */
  1499. static bool
  1500. set_sym_sections (struct bfd_hash_entry *bh, void *inf ATTRIBUTE_UNUSED)
  1501. {
  1502. struct definedness_hash_entry *def = (struct definedness_hash_entry *) bh;
  1503. if (def->final_sec != bfd_abs_section_ptr)
  1504. {
  1505. struct bfd_link_hash_entry *h;
  1506. h = bfd_link_hash_lookup (link_info.hash, bh->string,
  1507. false, false, true);
  1508. if (h != NULL
  1509. && h->type == bfd_link_hash_defined
  1510. && h->u.def.section == bfd_abs_section_ptr)
  1511. {
  1512. h->u.def.value -= def->final_sec->vma;
  1513. h->u.def.section = def->final_sec;
  1514. }
  1515. }
  1516. return true;
  1517. }
  1518. void
  1519. ldexp_finalize_syms (void)
  1520. {
  1521. bfd_hash_traverse (&definedness_table, set_sym_sections, NULL);
  1522. }
  1523. /* Determine whether a symbol is going to remain absolute even after
  1524. ldexp_finalize_syms() has run. */
  1525. bool
  1526. ldexp_is_final_sym_absolute (const struct bfd_link_hash_entry *h)
  1527. {
  1528. if (h->type == bfd_link_hash_defined
  1529. && h->u.def.section == bfd_abs_section_ptr)
  1530. {
  1531. const struct definedness_hash_entry *def;
  1532. if (!h->ldscript_def)
  1533. return true;
  1534. def = symbol_defined (h->root.string);
  1535. if (def != NULL)
  1536. return def->final_sec == bfd_abs_section_ptr;
  1537. }
  1538. return false;
  1539. }
  1540. void
  1541. ldexp_finish (void)
  1542. {
  1543. bfd_hash_table_free (&definedness_table);
  1544. }