ldexp.c 44 KB

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