xtensaelf.em 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. #
  4. # This file is part of the GNU Binutils.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. # MA 02110-1301, USA.
  20. #
  21. # This file is sourced from elf32.em, and defines extra xtensa-elf
  22. # specific routines.
  23. #
  24. fragment <<EOF
  25. #include <xtensa-config.h>
  26. #include "../bfd/elf-bfd.h"
  27. #include "../bfd/libbfd.h"
  28. #include "elf/xtensa.h"
  29. #include "bfd.h"
  30. /* Provide default values for new configuration settings. */
  31. #ifndef XSHAL_ABI
  32. #define XSHAL_ABI 0
  33. #endif
  34. static void xtensa_wild_group_interleave (lang_statement_union_type *);
  35. static void xtensa_colocate_output_literals (lang_statement_union_type *);
  36. static void xtensa_strip_inconsistent_linkonce_sections
  37. (lang_statement_list_type *);
  38. /* This number is irrelevant until we turn on use_literal_pages */
  39. static bfd_vma xtensa_page_power = 12; /* 4K pages. */
  40. /* To force a page break between literals and text, change
  41. xtensa_use_literal_pages to "TRUE". */
  42. static bfd_boolean xtensa_use_literal_pages = FALSE;
  43. #define EXTRA_VALIDATION 0
  44. static char *
  45. elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
  46. char **argv ATTRIBUTE_UNUSED)
  47. {
  48. if (XCHAL_HAVE_BE)
  49. return "${BIG_OUTPUT_FORMAT}";
  50. else
  51. return "${LITTLE_OUTPUT_FORMAT}";
  52. }
  53. static void
  54. elf_xtensa_before_parse (void)
  55. {
  56. /* Just call the default hook.... Tensilica's version of this function
  57. does some other work that isn't relevant here. */
  58. gld${EMULATION_NAME}_before_parse ();
  59. }
  60. static void
  61. remove_section (bfd *abfd, asection *os)
  62. {
  63. asection **spp;
  64. for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
  65. if (*spp == os)
  66. {
  67. *spp = os->next;
  68. os->owner->section_count--;
  69. break;
  70. }
  71. }
  72. static bfd_boolean
  73. replace_insn_sec_with_prop_sec (bfd *abfd,
  74. const char *insn_sec_name,
  75. const char *prop_sec_name,
  76. char **error_message)
  77. {
  78. asection *insn_sec;
  79. asection *prop_sec;
  80. bfd_byte *prop_contents = NULL;
  81. bfd_byte *insn_contents = NULL;
  82. unsigned entry_count;
  83. unsigned entry;
  84. Elf_Internal_Shdr *rel_hdr;
  85. Elf_Internal_Rela *internal_relocs = NULL;
  86. unsigned reloc_count;
  87. *error_message = "";
  88. insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
  89. if (insn_sec == NULL)
  90. return TRUE;
  91. entry_count = insn_sec->size / 8;
  92. prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
  93. if (prop_sec != NULL && insn_sec != NULL)
  94. {
  95. *error_message = _("file already has property tables");
  96. return FALSE;
  97. }
  98. if (insn_sec->size != 0)
  99. {
  100. insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
  101. if (insn_contents == NULL)
  102. {
  103. *error_message = _("out of memory");
  104. goto cleanup;
  105. }
  106. if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
  107. (file_ptr) 0, insn_sec->size))
  108. {
  109. *error_message = _("failed to read section contents");
  110. goto cleanup;
  111. }
  112. }
  113. /* Create a property table section for it. */
  114. prop_sec_name = strdup (prop_sec_name);
  115. prop_sec = bfd_make_section_with_flags
  116. (abfd, prop_sec_name, bfd_get_section_flags (abfd, insn_sec));
  117. if (prop_sec == NULL
  118. || ! bfd_set_section_alignment (abfd, prop_sec, 2))
  119. {
  120. *error_message = _("could not create new section");
  121. goto cleanup;
  122. }
  123. prop_sec->size = entry_count * 12;
  124. prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
  125. elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
  126. /* The entry size and size must be set to allow the linker to compute
  127. the number of relocations since it does not use reloc_count. */
  128. rel_hdr = _bfd_elf_single_rel_hdr (prop_sec);
  129. rel_hdr->sh_entsize = sizeof (Elf32_External_Rela);
  130. rel_hdr->sh_size = _bfd_elf_single_rel_hdr (insn_sec)->sh_size;
  131. if (prop_contents == NULL && prop_sec->size != 0)
  132. {
  133. *error_message = _("could not allocate section contents");
  134. goto cleanup;
  135. }
  136. /* Read the relocations. */
  137. reloc_count = insn_sec->reloc_count;
  138. if (reloc_count != 0)
  139. {
  140. /* If there is already an internal_reloc, then save it so that the
  141. read_relocs function freshly allocates a copy. */
  142. Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
  143. elf_section_data (insn_sec)->relocs = NULL;
  144. internal_relocs =
  145. _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
  146. elf_section_data (insn_sec)->relocs = saved_relocs;
  147. if (internal_relocs == NULL)
  148. {
  149. *error_message = _("out of memory");
  150. goto cleanup;
  151. }
  152. }
  153. /* Create a relocation section for the property section. */
  154. if (internal_relocs != NULL)
  155. {
  156. elf_section_data (prop_sec)->relocs = internal_relocs;
  157. prop_sec->reloc_count = reloc_count;
  158. }
  159. /* Now copy each insn table entry to the prop table entry with
  160. appropriate flags. */
  161. for (entry = 0; entry < entry_count; ++entry)
  162. {
  163. unsigned value;
  164. unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_NO_TRANSFORM
  165. | XTENSA_PROP_INSN_NO_REORDER);
  166. value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
  167. bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
  168. value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
  169. bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
  170. bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
  171. }
  172. /* Now copy all of the relocations. Change offsets for the
  173. instruction table section to offsets in the property table
  174. section. */
  175. if (internal_relocs)
  176. {
  177. unsigned i;
  178. for (i = 0; i < reloc_count; i++)
  179. {
  180. Elf_Internal_Rela *rela;
  181. unsigned r_offset;
  182. rela = &internal_relocs[i];
  183. /* If this relocation is to the .xt.insn section,
  184. change the section number and the offset. */
  185. r_offset = rela->r_offset;
  186. r_offset += 4 * (r_offset / 8);
  187. rela->r_offset = r_offset;
  188. }
  189. }
  190. remove_section (abfd, insn_sec);
  191. if (insn_contents)
  192. free (insn_contents);
  193. return TRUE;
  194. cleanup:
  195. if (prop_sec && prop_sec->owner)
  196. remove_section (abfd, prop_sec);
  197. if (insn_contents)
  198. free (insn_contents);
  199. if (internal_relocs)
  200. free (internal_relocs);
  201. return FALSE;
  202. }
  203. #define PROP_SEC_BASE_NAME ".xt.prop"
  204. #define INSN_SEC_BASE_NAME ".xt.insn"
  205. #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
  206. static void
  207. replace_instruction_table_sections (bfd *abfd, asection *sec)
  208. {
  209. char *message = "";
  210. const char *insn_sec_name = NULL;
  211. char *prop_sec_name = NULL;
  212. char *owned_prop_sec_name = NULL;
  213. const char *sec_name;
  214. sec_name = bfd_get_section_name (abfd, sec);
  215. if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
  216. {
  217. insn_sec_name = INSN_SEC_BASE_NAME;
  218. prop_sec_name = PROP_SEC_BASE_NAME;
  219. }
  220. else if (CONST_STRNEQ (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME))
  221. {
  222. insn_sec_name = sec_name;
  223. owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
  224. prop_sec_name = owned_prop_sec_name;
  225. strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
  226. strcat (prop_sec_name,
  227. sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
  228. }
  229. if (insn_sec_name != NULL)
  230. {
  231. if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
  232. &message))
  233. {
  234. einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
  235. insn_sec_name, abfd, message);
  236. }
  237. }
  238. if (owned_prop_sec_name)
  239. free (owned_prop_sec_name);
  240. }
  241. /* This is called after all input sections have been opened to convert
  242. instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
  243. tables (.xt.prop) before any section placement. */
  244. static void
  245. elf_xtensa_after_open (void)
  246. {
  247. /* First call the ELF version. */
  248. gld${EMULATION_NAME}_after_open ();
  249. /* Now search the input files looking for instruction table sections. */
  250. LANG_FOR_EACH_INPUT_STATEMENT (f)
  251. {
  252. asection *sec = f->the_bfd->sections;
  253. asection *next_sec;
  254. /* Do not use bfd_map_over_sections here since we are removing
  255. sections as we iterate. */
  256. while (sec != NULL)
  257. {
  258. next_sec = sec->next;
  259. replace_instruction_table_sections (f->the_bfd, sec);
  260. sec = next_sec;
  261. }
  262. }
  263. }
  264. static bfd_boolean
  265. xt_config_info_unpack_and_check (char *data,
  266. bfd_boolean *pmismatch,
  267. char **pmsg)
  268. {
  269. char *d, *key;
  270. unsigned num;
  271. *pmismatch = FALSE;
  272. d = data;
  273. while (*d)
  274. {
  275. key = d;
  276. d = strchr (d, '=');
  277. if (! d)
  278. goto error;
  279. /* Overwrite the equal sign. */
  280. *d++ = 0;
  281. /* Check if this is a quoted string or a number. */
  282. if (*d == '"')
  283. {
  284. /* No string values are currently checked by LD;
  285. just skip over the quotes. */
  286. d++;
  287. d = strchr (d, '"');
  288. if (! d)
  289. goto error;
  290. /* Overwrite the trailing quote. */
  291. *d++ = 0;
  292. }
  293. else
  294. {
  295. if (*d == 0)
  296. goto error;
  297. num = strtoul (d, &d, 0);
  298. if (! strcmp (key, "ABI"))
  299. {
  300. if (num != XSHAL_ABI)
  301. {
  302. *pmismatch = TRUE;
  303. *pmsg = "ABI does not match";
  304. }
  305. }
  306. else if (! strcmp (key, "USE_ABSOLUTE_LITERALS"))
  307. {
  308. if (num != XSHAL_USE_ABSOLUTE_LITERALS)
  309. {
  310. *pmismatch = TRUE;
  311. *pmsg = "incompatible use of the Extended L32R option";
  312. }
  313. }
  314. }
  315. if (*d++ != '\n')
  316. goto error;
  317. }
  318. return TRUE;
  319. error:
  320. return FALSE;
  321. }
  322. #define XTINFO_NAME "Xtensa_Info"
  323. #define XTINFO_NAMESZ 12
  324. #define XTINFO_TYPE 1
  325. static void
  326. check_xtensa_info (bfd *abfd, asection *info_sec)
  327. {
  328. char *data, *errmsg = "";
  329. bfd_boolean mismatch;
  330. data = xmalloc (info_sec->size);
  331. if (! bfd_get_section_contents (abfd, info_sec, data, 0, info_sec->size))
  332. einfo (_("%F%P:%B: cannot read contents of section %A\n"), abfd, info_sec);
  333. if (info_sec->size > 24
  334. && info_sec->size >= 24 + bfd_get_32 (abfd, data + 4)
  335. && bfd_get_32 (abfd, data + 0) == XTINFO_NAMESZ
  336. && bfd_get_32 (abfd, data + 8) == XTINFO_TYPE
  337. && strcmp (data + 12, XTINFO_NAME) == 0
  338. && xt_config_info_unpack_and_check (data + 12 + XTINFO_NAMESZ,
  339. &mismatch, &errmsg))
  340. {
  341. if (mismatch)
  342. einfo (_("%P:%B: warning: incompatible Xtensa configuration (%s)\n"),
  343. abfd, errmsg);
  344. }
  345. else
  346. einfo (_("%P:%B: warning: cannot parse .xtensa.info section\n"), abfd);
  347. free (data);
  348. }
  349. /* This is called after the sections have been attached to output
  350. sections, but before any sizes or addresses have been set. */
  351. static void
  352. elf_xtensa_before_allocation (void)
  353. {
  354. asection *info_sec, *first_info_sec;
  355. bfd *first_bfd;
  356. bfd_boolean is_big_endian = XCHAL_HAVE_BE;
  357. /* Check that the output endianness matches the Xtensa
  358. configuration. The BFD library always includes both big and
  359. little endian target vectors for Xtensa, but it only supports the
  360. detailed instruction encode/decode operations (such as are
  361. required to process relocations) for the selected Xtensa
  362. configuration. */
  363. if (is_big_endian
  364. && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
  365. {
  366. einfo (_("%F%P: little endian output does not match "
  367. "Xtensa configuration\n"));
  368. }
  369. if (!is_big_endian
  370. && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
  371. {
  372. einfo (_("%F%P: big endian output does not match "
  373. "Xtensa configuration\n"));
  374. }
  375. /* Keep track of the first input .xtensa.info section, and as a fallback,
  376. the first input bfd where a .xtensa.info section could be created.
  377. After the input .xtensa.info has been checked, the contents of the
  378. first one will be replaced with the output .xtensa.info table. */
  379. first_info_sec = 0;
  380. first_bfd = 0;
  381. LANG_FOR_EACH_INPUT_STATEMENT (f)
  382. {
  383. /* Check that the endianness for each input file matches the output.
  384. The merge_private_bfd_data hook has already reported any mismatches
  385. as errors, but those errors are not fatal. At this point, we
  386. cannot go any further if there are any mismatches. */
  387. if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
  388. || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
  389. einfo (_("%F%P: cross-endian linking for %B not supported\n"),
  390. f->the_bfd);
  391. if (! first_bfd)
  392. first_bfd = f->the_bfd;
  393. info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
  394. if (! info_sec)
  395. continue;
  396. if (! first_info_sec)
  397. first_info_sec = info_sec;
  398. /* Unpack the .xtensa.info section and check it against the current
  399. Xtensa configuration. */
  400. check_xtensa_info (f->the_bfd, info_sec);
  401. /* Do not include this copy of .xtensa.info in the output. */
  402. info_sec->size = 0;
  403. info_sec->flags |= SEC_EXCLUDE;
  404. }
  405. /* Reuse the first .xtensa.info input section to hold the output
  406. .xtensa.info; or, if none were found, create a new section in the
  407. first input bfd (assuming there is one). */
  408. info_sec = first_info_sec;
  409. if (! info_sec && first_bfd)
  410. {
  411. info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
  412. SEC_HAS_CONTENTS | SEC_READONLY);
  413. if (! info_sec)
  414. einfo (_("%F%P: failed to create .xtensa.info section\n"));
  415. }
  416. if (info_sec)
  417. {
  418. int xtensa_info_size;
  419. char *data;
  420. info_sec->flags &= ~SEC_EXCLUDE;
  421. info_sec->flags |= SEC_IN_MEMORY;
  422. data = xmalloc (100);
  423. sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
  424. XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
  425. xtensa_info_size = strlen (data) + 1;
  426. /* Add enough null terminators to pad to a word boundary. */
  427. do
  428. data[xtensa_info_size++] = 0;
  429. while ((xtensa_info_size & 3) != 0);
  430. info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
  431. info_sec->contents = xmalloc (info_sec->size);
  432. bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
  433. bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
  434. bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
  435. memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
  436. memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
  437. free (data);
  438. }
  439. /* Enable relaxation by default if the "--no-relax" option was not
  440. specified. This is done here instead of in the before_parse hook
  441. because there is a check in main() to prohibit use of --relax and
  442. -r together and that combination should be allowed for Xtensa. */
  443. if (RELAXATION_DISABLED_BY_DEFAULT)
  444. ENABLE_RELAXATION;
  445. xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
  446. gld${EMULATION_NAME}_before_allocation ();
  447. xtensa_wild_group_interleave (stat_ptr->head);
  448. if (RELAXATION_ENABLED)
  449. xtensa_colocate_output_literals (stat_ptr->head);
  450. /* TBD: We need to force the page alignments to here and only do
  451. them as needed for the entire output section. Finally, if this
  452. is a relocatable link then we need to add alignment notes so
  453. that the literals can be separated later. */
  454. }
  455. typedef struct wildcard_list section_name_list;
  456. typedef struct reloc_deps_e_t reloc_deps_e;
  457. typedef struct reloc_deps_section_t reloc_deps_section;
  458. typedef struct reloc_deps_graph_t reloc_deps_graph;
  459. struct reloc_deps_e_t
  460. {
  461. asection *src; /* Contains l32rs. */
  462. asection *tgt; /* Contains literals. */
  463. reloc_deps_e *next;
  464. };
  465. /* Place these in the userdata field. */
  466. struct reloc_deps_section_t
  467. {
  468. reloc_deps_e *preds;
  469. reloc_deps_e *succs;
  470. bfd_boolean is_only_literal;
  471. };
  472. struct reloc_deps_graph_t
  473. {
  474. size_t count;
  475. size_t size;
  476. asection **sections;
  477. };
  478. static void xtensa_layout_wild
  479. (const reloc_deps_graph *, lang_wild_statement_type *);
  480. typedef void (*deps_callback_t) (asection *, /* src_sec */
  481. bfd_vma, /* src_offset */
  482. asection *, /* target_sec */
  483. bfd_vma, /* target_offset */
  484. void *); /* closure */
  485. extern bfd_boolean xtensa_callback_required_dependence
  486. (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
  487. static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
  488. static bfd_boolean ld_local_file_relocations_fit
  489. (lang_statement_union_type *, const reloc_deps_graph *);
  490. static bfd_vma ld_assign_relative_paged_dot
  491. (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
  492. bfd_boolean);
  493. static bfd_vma ld_xtensa_insert_page_offsets
  494. (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
  495. #if EXTRA_VALIDATION
  496. static size_t ld_count_children (lang_statement_union_type *);
  497. #endif
  498. extern lang_statement_list_type constructor_list;
  499. static reloc_deps_section *
  500. xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  501. asection *sec)
  502. {
  503. /* We have a separate function for this so that
  504. we could in the future keep a completely independent
  505. structure that maps a section to its dependence edges.
  506. For now, we place these in the sec->userdata field. */
  507. reloc_deps_section *sec_deps = sec->userdata;
  508. return sec_deps;
  509. }
  510. static void
  511. xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  512. asection *sec,
  513. reloc_deps_section *deps_section)
  514. {
  515. sec->userdata = deps_section;
  516. }
  517. /* This is used to keep a list of all of the sections participating in
  518. the graph so we can clean them up quickly. */
  519. static void
  520. xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
  521. {
  522. if (deps->size <= deps->count)
  523. {
  524. asection **new_sections;
  525. size_t i;
  526. size_t new_size;
  527. new_size = deps->size * 2;
  528. if (new_size == 0)
  529. new_size = 20;
  530. new_sections = xmalloc (sizeof (asection *) * new_size);
  531. memset (new_sections, 0, sizeof (asection *) * new_size);
  532. for (i = 0; i < deps->count; i++)
  533. {
  534. new_sections[i] = deps->sections[i];
  535. }
  536. if (deps->sections != NULL)
  537. free (deps->sections);
  538. deps->sections = new_sections;
  539. deps->size = new_size;
  540. }
  541. deps->sections[deps->count] = sec;
  542. deps->count++;
  543. }
  544. static void
  545. free_reloc_deps_graph (reloc_deps_graph *deps)
  546. {
  547. size_t i;
  548. for (i = 0; i < deps->count; i++)
  549. {
  550. asection *sec = deps->sections[i];
  551. reloc_deps_section *sec_deps;
  552. sec_deps = xtensa_get_section_deps (deps, sec);
  553. if (sec_deps)
  554. {
  555. reloc_deps_e *next;
  556. while (sec_deps->succs != NULL)
  557. {
  558. next = sec_deps->succs->next;
  559. free (sec_deps->succs);
  560. sec_deps->succs = next;
  561. }
  562. while (sec_deps->preds != NULL)
  563. {
  564. next = sec_deps->preds->next;
  565. free (sec_deps->preds);
  566. sec_deps->preds = next;
  567. }
  568. free (sec_deps);
  569. }
  570. xtensa_set_section_deps (deps, sec, NULL);
  571. }
  572. if (deps->sections)
  573. free (deps->sections);
  574. free (deps);
  575. }
  576. static bfd_boolean
  577. section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  578. lang_statement_union_type *s)
  579. {
  580. asection *sec;
  581. const reloc_deps_section *sec_deps;
  582. if (s->header.type != lang_input_section_enum)
  583. return FALSE;
  584. sec = s->input_section.section;
  585. sec_deps = xtensa_get_section_deps (deps, sec);
  586. return sec_deps && sec_deps->succs != NULL;
  587. }
  588. static bfd_boolean
  589. section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  590. lang_statement_union_type *s)
  591. {
  592. asection *sec;
  593. const reloc_deps_section *sec_deps;
  594. if (s->header.type != lang_input_section_enum)
  595. return FALSE;
  596. sec = s->input_section.section;
  597. sec_deps = xtensa_get_section_deps (deps, sec);
  598. return sec_deps && sec_deps->preds != NULL;
  599. }
  600. static bfd_boolean
  601. section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  602. lang_statement_union_type *s)
  603. {
  604. return (section_is_source (deps, s)
  605. || section_is_target (deps, s));
  606. }
  607. typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
  608. typedef struct xtensa_ld_iter_t xtensa_ld_iter;
  609. struct xtensa_ld_iter_t
  610. {
  611. lang_statement_union_type *parent; /* Parent of the list. */
  612. lang_statement_list_type *l; /* List that holds it. */
  613. lang_statement_union_type **loc; /* Place in the list. */
  614. };
  615. struct xtensa_ld_iter_stack_t
  616. {
  617. xtensa_ld_iter iterloc; /* List that hold it. */
  618. xtensa_ld_iter_stack *next; /* Next in the stack. */
  619. xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
  620. };
  621. static void
  622. ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
  623. {
  624. lang_statement_union_type *to_next;
  625. lang_statement_union_type *current_next;
  626. lang_statement_union_type **e;
  627. #if EXTRA_VALIDATION
  628. size_t old_to_count, new_to_count;
  629. size_t old_current_count, new_current_count;
  630. #endif
  631. if (to == current)
  632. return;
  633. #if EXTRA_VALIDATION
  634. old_to_count = ld_count_children (to->parent);
  635. old_current_count = ld_count_children (current->parent);
  636. #endif
  637. to_next = *(to->loc);
  638. current_next = (*current->loc)->header.next;
  639. *(to->loc) = *(current->loc);
  640. *(current->loc) = current_next;
  641. (*(to->loc))->header.next = to_next;
  642. /* reset "to" list tail */
  643. for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
  644. ;
  645. to->l->tail = e;
  646. /* reset "current" list tail */
  647. for (e = &current->l->head; *e != NULL; e = &(*e)->header.next)
  648. ;
  649. current->l->tail = e;
  650. #if EXTRA_VALIDATION
  651. new_to_count = ld_count_children (to->parent);
  652. new_current_count = ld_count_children (current->parent);
  653. ASSERT ((old_to_count + old_current_count)
  654. == (new_to_count + new_current_count));
  655. #endif
  656. }
  657. /* Can only be called with lang_statements that have lists. Returns
  658. FALSE if the list is empty. */
  659. static bfd_boolean
  660. iter_stack_empty (xtensa_ld_iter_stack **stack_p)
  661. {
  662. return *stack_p == NULL;
  663. }
  664. static bfd_boolean
  665. iter_stack_push (xtensa_ld_iter_stack **stack_p,
  666. lang_statement_union_type *parent)
  667. {
  668. xtensa_ld_iter_stack *stack;
  669. lang_statement_list_type *l = NULL;
  670. switch (parent->header.type)
  671. {
  672. case lang_output_section_statement_enum:
  673. l = &parent->output_section_statement.children;
  674. break;
  675. case lang_wild_statement_enum:
  676. l = &parent->wild_statement.children;
  677. break;
  678. case lang_group_statement_enum:
  679. l = &parent->group_statement.children;
  680. break;
  681. default:
  682. ASSERT (0);
  683. return FALSE;
  684. }
  685. /* Empty. do not push. */
  686. if (l->tail == &l->head)
  687. return FALSE;
  688. stack = xmalloc (sizeof (xtensa_ld_iter_stack));
  689. memset (stack, 0, sizeof (xtensa_ld_iter_stack));
  690. stack->iterloc.parent = parent;
  691. stack->iterloc.l = l;
  692. stack->iterloc.loc = &l->head;
  693. stack->next = *stack_p;
  694. stack->prev = NULL;
  695. if (*stack_p != NULL)
  696. (*stack_p)->prev = stack;
  697. *stack_p = stack;
  698. return TRUE;
  699. }
  700. static void
  701. iter_stack_pop (xtensa_ld_iter_stack **stack_p)
  702. {
  703. xtensa_ld_iter_stack *stack;
  704. stack = *stack_p;
  705. if (stack == NULL)
  706. {
  707. ASSERT (stack != NULL);
  708. return;
  709. }
  710. if (stack->next != NULL)
  711. stack->next->prev = NULL;
  712. *stack_p = stack->next;
  713. free (stack);
  714. }
  715. /* This MUST be called if, during iteration, the user changes the
  716. underlying structure. It will check for a NULL current and advance
  717. accordingly. */
  718. static void
  719. iter_stack_update (xtensa_ld_iter_stack **stack_p)
  720. {
  721. if (!iter_stack_empty (stack_p)
  722. && (*(*stack_p)->iterloc.loc) == NULL)
  723. {
  724. iter_stack_pop (stack_p);
  725. while (!iter_stack_empty (stack_p)
  726. && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
  727. {
  728. iter_stack_pop (stack_p);
  729. }
  730. if (!iter_stack_empty (stack_p))
  731. (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
  732. }
  733. }
  734. static void
  735. iter_stack_next (xtensa_ld_iter_stack **stack_p)
  736. {
  737. xtensa_ld_iter_stack *stack;
  738. lang_statement_union_type *current;
  739. stack = *stack_p;
  740. current = *stack->iterloc.loc;
  741. /* If we are on the first element. */
  742. if (current != NULL)
  743. {
  744. switch (current->header.type)
  745. {
  746. case lang_output_section_statement_enum:
  747. case lang_wild_statement_enum:
  748. case lang_group_statement_enum:
  749. /* If the list if not empty, we are done. */
  750. if (iter_stack_push (stack_p, *stack->iterloc.loc))
  751. return;
  752. /* Otherwise increment the pointer as normal. */
  753. break;
  754. default:
  755. break;
  756. }
  757. }
  758. while (!iter_stack_empty (stack_p)
  759. && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
  760. {
  761. iter_stack_pop (stack_p);
  762. }
  763. if (!iter_stack_empty (stack_p))
  764. (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
  765. }
  766. static lang_statement_union_type *
  767. iter_stack_current (xtensa_ld_iter_stack **stack_p)
  768. {
  769. return *((*stack_p)->iterloc.loc);
  770. }
  771. /* The iter stack is a preorder. */
  772. static void
  773. iter_stack_create (xtensa_ld_iter_stack **stack_p,
  774. lang_statement_union_type *parent)
  775. {
  776. iter_stack_push (stack_p, parent);
  777. }
  778. static void
  779. iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
  780. {
  781. *front = (*stack_p)->iterloc;
  782. }
  783. static void
  784. xtensa_colocate_literals (reloc_deps_graph *deps,
  785. lang_statement_union_type *statement)
  786. {
  787. /* Keep a stack of pointers to control iteration through the contours. */
  788. xtensa_ld_iter_stack *stack = NULL;
  789. xtensa_ld_iter_stack **stack_p = &stack;
  790. xtensa_ld_iter front; /* Location where new insertion should occur. */
  791. xtensa_ld_iter *front_p = NULL;
  792. xtensa_ld_iter current; /* Location we are checking. */
  793. xtensa_ld_iter *current_p = NULL;
  794. bfd_boolean in_literals = FALSE;
  795. if (deps->count == 0)
  796. return;
  797. iter_stack_create (stack_p, statement);
  798. while (!iter_stack_empty (stack_p))
  799. {
  800. bfd_boolean skip_increment = FALSE;
  801. lang_statement_union_type *l = iter_stack_current (stack_p);
  802. switch (l->header.type)
  803. {
  804. case lang_assignment_statement_enum:
  805. /* Any assignment statement should block reordering across it. */
  806. front_p = NULL;
  807. in_literals = FALSE;
  808. break;
  809. case lang_input_section_enum:
  810. if (front_p == NULL)
  811. {
  812. in_literals = (section_is_target (deps, l)
  813. && !section_is_source (deps, l));
  814. if (in_literals)
  815. {
  816. front_p = &front;
  817. iter_stack_copy_current (stack_p, front_p);
  818. }
  819. }
  820. else
  821. {
  822. bfd_boolean is_target;
  823. current_p = &current;
  824. iter_stack_copy_current (stack_p, current_p);
  825. is_target = (section_is_target (deps, l)
  826. && !section_is_source (deps, l));
  827. if (in_literals)
  828. {
  829. iter_stack_copy_current (stack_p, front_p);
  830. if (!is_target)
  831. in_literals = FALSE;
  832. }
  833. else
  834. {
  835. if (is_target)
  836. {
  837. /* Try to insert in place. */
  838. ld_xtensa_move_section_after (front_p, current_p);
  839. ld_assign_relative_paged_dot (0x100000,
  840. statement,
  841. deps,
  842. xtensa_use_literal_pages);
  843. /* We use this code because it's already written. */
  844. if (!ld_local_file_relocations_fit (statement, deps))
  845. {
  846. /* Move it back. */
  847. ld_xtensa_move_section_after (current_p, front_p);
  848. /* Reset the literal placement. */
  849. iter_stack_copy_current (stack_p, front_p);
  850. }
  851. else
  852. {
  853. /* Move front pointer up by one. */
  854. front_p->loc = &(*front_p->loc)->header.next;
  855. /* Do not increment the current pointer. */
  856. skip_increment = TRUE;
  857. }
  858. }
  859. }
  860. }
  861. break;
  862. default:
  863. break;
  864. }
  865. if (!skip_increment)
  866. iter_stack_next (stack_p);
  867. else
  868. /* Be careful to update the stack_p if it now is a null. */
  869. iter_stack_update (stack_p);
  870. }
  871. lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
  872. }
  873. static void
  874. xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
  875. lang_wild_statement_type *w)
  876. {
  877. /* Keep a front pointer and a current pointer. */
  878. lang_statement_union_type **front;
  879. lang_statement_union_type **current;
  880. /* Walk to the end of the targets. */
  881. for (front = &w->children.head;
  882. (*front != NULL) && section_is_source_or_target (deps, *front);
  883. front = &(*front)->header.next)
  884. ;
  885. if (*front == NULL)
  886. return;
  887. current = &(*front)->header.next;
  888. while (*current != NULL)
  889. {
  890. if (section_is_source_or_target (deps, *current))
  891. {
  892. /* Insert in place. */
  893. xtensa_ld_iter front_iter;
  894. xtensa_ld_iter current_iter;
  895. front_iter.parent = (lang_statement_union_type *) w;
  896. front_iter.l = &w->children;
  897. front_iter.loc = front;
  898. current_iter.parent = (lang_statement_union_type *) w;
  899. current_iter.l = &w->children;
  900. current_iter.loc = current;
  901. ld_xtensa_move_section_after (&front_iter, &current_iter);
  902. front = &(*front)->header.next;
  903. }
  904. else
  905. {
  906. current = &(*current)->header.next;
  907. }
  908. }
  909. }
  910. static bfd_boolean
  911. deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
  912. {
  913. const reloc_deps_section *sec_deps;
  914. const reloc_deps_e *sec_deps_e;
  915. sec_deps = xtensa_get_section_deps (deps, src);
  916. if (sec_deps == NULL)
  917. return FALSE;
  918. for (sec_deps_e = sec_deps->succs;
  919. sec_deps_e != NULL;
  920. sec_deps_e = sec_deps_e->next)
  921. {
  922. ASSERT (sec_deps_e->src == src);
  923. if (sec_deps_e->tgt == tgt)
  924. return TRUE;
  925. }
  926. return FALSE;
  927. }
  928. static bfd_boolean
  929. deps_has_edge (const reloc_deps_graph *deps,
  930. lang_statement_union_type *src,
  931. lang_statement_union_type *tgt)
  932. {
  933. if (!section_is_source (deps, src))
  934. return FALSE;
  935. if (!section_is_target (deps, tgt))
  936. return FALSE;
  937. if (src->header.type != lang_input_section_enum)
  938. return FALSE;
  939. if (tgt->header.type != lang_input_section_enum)
  940. return FALSE;
  941. return deps_has_sec_edge (deps, src->input_section.section,
  942. tgt->input_section.section);
  943. }
  944. static void
  945. add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
  946. {
  947. reloc_deps_section *src_sec_deps;
  948. reloc_deps_section *tgt_sec_deps;
  949. reloc_deps_e *src_edge;
  950. reloc_deps_e *tgt_edge;
  951. if (deps_has_sec_edge (deps, src_sec, tgt_sec))
  952. return;
  953. src_sec_deps = xtensa_get_section_deps (deps, src_sec);
  954. if (src_sec_deps == NULL)
  955. {
  956. /* Add a section. */
  957. src_sec_deps = xmalloc (sizeof (reloc_deps_section));
  958. memset (src_sec_deps, 0, sizeof (reloc_deps_section));
  959. src_sec_deps->is_only_literal = 0;
  960. src_sec_deps->preds = NULL;
  961. src_sec_deps->succs = NULL;
  962. xtensa_set_section_deps (deps, src_sec, src_sec_deps);
  963. xtensa_append_section_deps (deps, src_sec);
  964. }
  965. tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
  966. if (tgt_sec_deps == NULL)
  967. {
  968. /* Add a section. */
  969. tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
  970. memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
  971. tgt_sec_deps->is_only_literal = 0;
  972. tgt_sec_deps->preds = NULL;
  973. tgt_sec_deps->succs = NULL;
  974. xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
  975. xtensa_append_section_deps (deps, tgt_sec);
  976. }
  977. /* Add the edges. */
  978. src_edge = xmalloc (sizeof (reloc_deps_e));
  979. memset (src_edge, 0, sizeof (reloc_deps_e));
  980. src_edge->src = src_sec;
  981. src_edge->tgt = tgt_sec;
  982. src_edge->next = src_sec_deps->succs;
  983. src_sec_deps->succs = src_edge;
  984. tgt_edge = xmalloc (sizeof (reloc_deps_e));
  985. memset (tgt_edge, 0, sizeof (reloc_deps_e));
  986. tgt_edge->src = src_sec;
  987. tgt_edge->tgt = tgt_sec;
  988. tgt_edge->next = tgt_sec_deps->preds;
  989. tgt_sec_deps->preds = tgt_edge;
  990. }
  991. static void
  992. build_deps_graph_callback (asection *src_sec,
  993. bfd_vma src_offset ATTRIBUTE_UNUSED,
  994. asection *target_sec,
  995. bfd_vma target_offset ATTRIBUTE_UNUSED,
  996. void *closure)
  997. {
  998. reloc_deps_graph *deps = closure;
  999. /* If the target is defined. */
  1000. if (target_sec != NULL)
  1001. add_deps_edge (deps, src_sec, target_sec);
  1002. }
  1003. static reloc_deps_graph *
  1004. ld_build_required_section_dependence (lang_statement_union_type *s)
  1005. {
  1006. reloc_deps_graph *deps;
  1007. xtensa_ld_iter_stack *stack = NULL;
  1008. deps = xmalloc (sizeof (reloc_deps_graph));
  1009. deps->sections = NULL;
  1010. deps->count = 0;
  1011. deps->size = 0;
  1012. for (iter_stack_create (&stack, s);
  1013. !iter_stack_empty (&stack);
  1014. iter_stack_next (&stack))
  1015. {
  1016. lang_statement_union_type *l = iter_stack_current (&stack);
  1017. if (l->header.type == lang_input_section_enum)
  1018. {
  1019. lang_input_section_type *input;
  1020. input = &l->input_section;
  1021. xtensa_callback_required_dependence (input->section->owner,
  1022. input->section,
  1023. &link_info,
  1024. /* Use the same closure. */
  1025. build_deps_graph_callback,
  1026. deps);
  1027. }
  1028. }
  1029. return deps;
  1030. }
  1031. #if EXTRA_VALIDATION
  1032. static size_t
  1033. ld_count_children (lang_statement_union_type *s)
  1034. {
  1035. size_t count = 0;
  1036. xtensa_ld_iter_stack *stack = NULL;
  1037. for (iter_stack_create (&stack, s);
  1038. !iter_stack_empty (&stack);
  1039. iter_stack_next (&stack))
  1040. {
  1041. lang_statement_union_type *l = iter_stack_current (&stack);
  1042. ASSERT (l != NULL);
  1043. count++;
  1044. }
  1045. return count;
  1046. }
  1047. #endif /* EXTRA_VALIDATION */
  1048. /* Check if a particular section is included in the link. This will only
  1049. be true for one instance of a particular linkonce section. */
  1050. static bfd_boolean input_section_found = FALSE;
  1051. static asection *input_section_target = NULL;
  1052. static void
  1053. input_section_linked_worker (lang_statement_union_type *statement)
  1054. {
  1055. if ((statement->header.type == lang_input_section_enum
  1056. && (statement->input_section.section == input_section_target)))
  1057. input_section_found = TRUE;
  1058. }
  1059. static bfd_boolean
  1060. input_section_linked (asection *sec)
  1061. {
  1062. input_section_found = FALSE;
  1063. input_section_target = sec;
  1064. lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
  1065. return input_section_found;
  1066. }
  1067. /* Strip out any linkonce property tables or XCC exception tables where the
  1068. associated linkonce text is from a different object file. Normally,
  1069. a matching set of linkonce sections is taken from the same object file,
  1070. but sometimes the files are compiled differently so that some of the
  1071. linkonce sections are not present in all files. Stripping the
  1072. inconsistent sections like this is not completely robust -- a much
  1073. better solution is to use comdat groups. */
  1074. static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
  1075. static bfd_boolean
  1076. is_inconsistent_linkonce_section (asection *sec)
  1077. {
  1078. bfd *abfd = sec->owner;
  1079. const char *sec_name = bfd_get_section_name (abfd, sec);
  1080. const char *name;
  1081. if ((bfd_get_section_flags (abfd, sec) & SEC_LINK_ONCE) == 0
  1082. || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
  1083. return FALSE;
  1084. /* Check if this is an Xtensa property section or an exception table
  1085. for Tensilica's XCC compiler. */
  1086. name = sec_name + linkonce_len;
  1087. if (CONST_STRNEQ (name, "prop."))
  1088. name = strchr (name + 5, '.') ? strchr (name + 5, '.') + 1 : name + 5;
  1089. else if (name[1] == '.'
  1090. && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
  1091. name += 2;
  1092. else
  1093. name = 0;
  1094. if (name)
  1095. {
  1096. char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
  1097. asection *dep_sec;
  1098. /* Get the associated linkonce text section and check if it is
  1099. included in the link. If not, this section is inconsistent
  1100. and should be stripped. */
  1101. strcpy (dep_sec_name, ".gnu.linkonce.t.");
  1102. strcat (dep_sec_name, name);
  1103. dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
  1104. if (dep_sec == NULL || ! input_section_linked (dep_sec))
  1105. {
  1106. free (dep_sec_name);
  1107. return TRUE;
  1108. }
  1109. free (dep_sec_name);
  1110. }
  1111. return FALSE;
  1112. }
  1113. static void
  1114. xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
  1115. {
  1116. lang_statement_union_type **s_p = &slist->head;
  1117. while (*s_p)
  1118. {
  1119. lang_statement_union_type *s = *s_p;
  1120. lang_statement_union_type *s_next = (*s_p)->header.next;
  1121. switch (s->header.type)
  1122. {
  1123. case lang_input_section_enum:
  1124. if (is_inconsistent_linkonce_section (s->input_section.section))
  1125. {
  1126. s->input_section.section->output_section = bfd_abs_section_ptr;
  1127. *s_p = s_next;
  1128. continue;
  1129. }
  1130. break;
  1131. case lang_constructors_statement_enum:
  1132. xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
  1133. break;
  1134. case lang_output_section_statement_enum:
  1135. if (s->output_section_statement.children.head)
  1136. xtensa_strip_inconsistent_linkonce_sections
  1137. (&s->output_section_statement.children);
  1138. break;
  1139. case lang_wild_statement_enum:
  1140. xtensa_strip_inconsistent_linkonce_sections
  1141. (&s->wild_statement.children);
  1142. break;
  1143. case lang_group_statement_enum:
  1144. xtensa_strip_inconsistent_linkonce_sections
  1145. (&s->group_statement.children);
  1146. break;
  1147. case lang_data_statement_enum:
  1148. case lang_reloc_statement_enum:
  1149. case lang_object_symbols_statement_enum:
  1150. case lang_output_statement_enum:
  1151. case lang_target_statement_enum:
  1152. case lang_input_statement_enum:
  1153. case lang_assignment_statement_enum:
  1154. case lang_padding_statement_enum:
  1155. case lang_address_statement_enum:
  1156. case lang_fill_statement_enum:
  1157. break;
  1158. default:
  1159. FAIL ();
  1160. break;
  1161. }
  1162. s_p = &(*s_p)->header.next;
  1163. }
  1164. /* Reset the tail of the list, in case the last entry was removed. */
  1165. if (s_p != slist->tail)
  1166. slist->tail = s_p;
  1167. }
  1168. static void
  1169. xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
  1170. {
  1171. lang_wild_statement_type *w;
  1172. reloc_deps_graph *deps;
  1173. if (statement->header.type == lang_wild_statement_enum)
  1174. {
  1175. #if EXTRA_VALIDATION
  1176. size_t old_child_count;
  1177. size_t new_child_count;
  1178. #endif
  1179. bfd_boolean no_reorder;
  1180. w = &statement->wild_statement;
  1181. no_reorder = FALSE;
  1182. /* If it has 0 or 1 section bound, then do not reorder. */
  1183. if (w->children.head == NULL
  1184. || (w->children.head->header.type == lang_input_section_enum
  1185. && w->children.head->header.next == NULL))
  1186. no_reorder = TRUE;
  1187. if (w->filenames_sorted)
  1188. no_reorder = TRUE;
  1189. /* Check for sorting in a section list wildcard spec as well. */
  1190. if (!no_reorder)
  1191. {
  1192. struct wildcard_list *l;
  1193. for (l = w->section_list; l != NULL; l = l->next)
  1194. {
  1195. if (l->spec.sorted == TRUE)
  1196. {
  1197. no_reorder = TRUE;
  1198. break;
  1199. }
  1200. }
  1201. }
  1202. /* Special case until the NOREORDER linker directive is supported:
  1203. *(.init) output sections and *(.fini) specs may NOT be reordered. */
  1204. /* Check for sorting in a section list wildcard spec as well. */
  1205. if (!no_reorder)
  1206. {
  1207. struct wildcard_list *l;
  1208. for (l = w->section_list; l != NULL; l = l->next)
  1209. {
  1210. if (l->spec.name
  1211. && ((strcmp (".init", l->spec.name) == 0)
  1212. || (strcmp (".fini", l->spec.name) == 0)))
  1213. {
  1214. no_reorder = TRUE;
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. #if EXTRA_VALIDATION
  1220. old_child_count = ld_count_children (statement);
  1221. #endif
  1222. /* It is now officially a target. Build the graph of source
  1223. section -> target section (kept as a list of edges). */
  1224. deps = ld_build_required_section_dependence (statement);
  1225. /* If this wildcard does not reorder.... */
  1226. if (!no_reorder && deps->count != 0)
  1227. {
  1228. /* First check for reverse dependences. Fix if possible. */
  1229. xtensa_layout_wild (deps, w);
  1230. xtensa_move_dependencies_to_front (deps, w);
  1231. #if EXTRA_VALIDATION
  1232. new_child_count = ld_count_children (statement);
  1233. ASSERT (new_child_count == old_child_count);
  1234. #endif
  1235. xtensa_colocate_literals (deps, statement);
  1236. #if EXTRA_VALIDATION
  1237. new_child_count = ld_count_children (statement);
  1238. ASSERT (new_child_count == old_child_count);
  1239. #endif
  1240. }
  1241. /* Clean up. */
  1242. free_reloc_deps_graph (deps);
  1243. }
  1244. }
  1245. static void
  1246. xtensa_wild_group_interleave (lang_statement_union_type *s)
  1247. {
  1248. lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
  1249. }
  1250. static void
  1251. xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
  1252. {
  1253. /* If it does not fit initially, we need to do this step. Move all
  1254. of the wild literal sections to a new list, then move each of
  1255. them back in just before the first section they depend on. */
  1256. lang_statement_union_type **s_p;
  1257. #if EXTRA_VALIDATION
  1258. size_t old_count, new_count;
  1259. size_t ct1, ct2;
  1260. #endif
  1261. lang_wild_statement_type literal_wild;
  1262. literal_wild.header.next = NULL;
  1263. literal_wild.header.type = lang_wild_statement_enum;
  1264. literal_wild.filename = NULL;
  1265. literal_wild.filenames_sorted = FALSE;
  1266. literal_wild.section_list = NULL;
  1267. literal_wild.keep_sections = FALSE;
  1268. literal_wild.children.head = NULL;
  1269. literal_wild.children.tail = &literal_wild.children.head;
  1270. #if EXTRA_VALIDATION
  1271. old_count = ld_count_children ((lang_statement_union_type*) w);
  1272. #endif
  1273. s_p = &w->children.head;
  1274. while (*s_p != NULL)
  1275. {
  1276. lang_statement_union_type *l = *s_p;
  1277. if (l->header.type == lang_input_section_enum)
  1278. {
  1279. if (section_is_target (deps, l)
  1280. && ! section_is_source (deps, l))
  1281. {
  1282. /* Detach. */
  1283. *s_p = l->header.next;
  1284. if (*s_p == NULL)
  1285. w->children.tail = s_p;
  1286. l->header.next = NULL;
  1287. /* Append. */
  1288. *literal_wild.children.tail = l;
  1289. literal_wild.children.tail = &l->header.next;
  1290. continue;
  1291. }
  1292. }
  1293. s_p = &(*s_p)->header.next;
  1294. }
  1295. #if EXTRA_VALIDATION
  1296. ct1 = ld_count_children ((lang_statement_union_type*) w);
  1297. ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
  1298. ASSERT (old_count == (ct1 + ct2));
  1299. #endif
  1300. /* Now place them back in front of their dependent sections. */
  1301. while (literal_wild.children.head != NULL)
  1302. {
  1303. lang_statement_union_type *lit = literal_wild.children.head;
  1304. bfd_boolean placed = FALSE;
  1305. #if EXTRA_VALIDATION
  1306. ASSERT (ct2 > 0);
  1307. ct2--;
  1308. #endif
  1309. /* Detach. */
  1310. literal_wild.children.head = lit->header.next;
  1311. if (literal_wild.children.head == NULL)
  1312. literal_wild.children.tail = &literal_wild.children.head;
  1313. lit->header.next = NULL;
  1314. /* Find a spot to place it. */
  1315. for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
  1316. {
  1317. lang_statement_union_type *src = *s_p;
  1318. if (deps_has_edge (deps, src, lit))
  1319. {
  1320. /* Place it here. */
  1321. lit->header.next = *s_p;
  1322. *s_p = lit;
  1323. placed = TRUE;
  1324. break;
  1325. }
  1326. }
  1327. if (!placed)
  1328. {
  1329. /* Put it at the end. */
  1330. *w->children.tail = lit;
  1331. w->children.tail = &lit->header.next;
  1332. }
  1333. }
  1334. #if EXTRA_VALIDATION
  1335. new_count = ld_count_children ((lang_statement_union_type*) w);
  1336. ASSERT (new_count == old_count);
  1337. #endif
  1338. }
  1339. static void
  1340. xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
  1341. {
  1342. reloc_deps_graph *deps;
  1343. if (statement->header.type == lang_output_section_statement_enum)
  1344. {
  1345. /* Now, we walk over the contours of the output section statement.
  1346. First we build the literal section dependences as before.
  1347. At the first uniquely_literal section, we mark it as a good
  1348. spot to place other literals. Continue walking (and counting
  1349. sizes) until we find the next literal section. If this
  1350. section can be moved to the first one, then we move it. If
  1351. we every find a modification of ".", start over. If we find
  1352. a labeling of the current location, start over. Finally, at
  1353. the end, if we require page alignment, add page alignments. */
  1354. #if EXTRA_VALIDATION
  1355. size_t old_child_count;
  1356. size_t new_child_count;
  1357. #endif
  1358. bfd_boolean no_reorder = FALSE;
  1359. #if EXTRA_VALIDATION
  1360. old_child_count = ld_count_children (statement);
  1361. #endif
  1362. /* It is now officially a target. Build the graph of source
  1363. section -> target section (kept as a list of edges). */
  1364. deps = ld_build_required_section_dependence (statement);
  1365. /* If this wildcard does not reorder.... */
  1366. if (!no_reorder)
  1367. {
  1368. /* First check for reverse dependences. Fix if possible. */
  1369. xtensa_colocate_literals (deps, statement);
  1370. #if EXTRA_VALIDATION
  1371. new_child_count = ld_count_children (statement);
  1372. ASSERT (new_child_count == old_child_count);
  1373. #endif
  1374. }
  1375. /* Insert align/offset assignment statement. */
  1376. if (xtensa_use_literal_pages)
  1377. {
  1378. ld_xtensa_insert_page_offsets (0, statement, deps,
  1379. xtensa_use_literal_pages);
  1380. lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
  1381. statement);
  1382. }
  1383. /* Clean up. */
  1384. free_reloc_deps_graph (deps);
  1385. }
  1386. }
  1387. static void
  1388. xtensa_colocate_output_literals (lang_statement_union_type *s)
  1389. {
  1390. lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
  1391. }
  1392. static void
  1393. xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
  1394. {
  1395. switch (statement->header.type)
  1396. {
  1397. case lang_input_section_enum:
  1398. {
  1399. asection *bfd_section = statement->input_section.section;
  1400. bfd_section->output_offset = 0;
  1401. }
  1402. break;
  1403. default:
  1404. break;
  1405. }
  1406. }
  1407. static bfd_vma
  1408. ld_assign_relative_paged_dot (bfd_vma dot,
  1409. lang_statement_union_type *s,
  1410. const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
  1411. bfd_boolean lit_align)
  1412. {
  1413. /* Walk through all of the input statements in this wild statement
  1414. assign dot to all of them. */
  1415. xtensa_ld_iter_stack *stack = NULL;
  1416. xtensa_ld_iter_stack **stack_p = &stack;
  1417. bfd_boolean first_section = FALSE;
  1418. bfd_boolean in_literals = FALSE;
  1419. for (iter_stack_create (stack_p, s);
  1420. !iter_stack_empty (stack_p);
  1421. iter_stack_next (stack_p))
  1422. {
  1423. lang_statement_union_type *l = iter_stack_current (stack_p);
  1424. switch (l->header.type)
  1425. {
  1426. case lang_input_section_enum:
  1427. {
  1428. asection *section = l->input_section.section;
  1429. size_t align_pow = section->alignment_power;
  1430. bfd_boolean do_xtensa_alignment = FALSE;
  1431. if (lit_align)
  1432. {
  1433. bfd_boolean sec_is_target = section_is_target (deps, l);
  1434. bfd_boolean sec_is_source = section_is_source (deps, l);
  1435. if (section->size != 0
  1436. && (first_section
  1437. || (in_literals && !sec_is_target)
  1438. || (!in_literals && sec_is_target)))
  1439. {
  1440. do_xtensa_alignment = TRUE;
  1441. }
  1442. first_section = FALSE;
  1443. if (section->size != 0)
  1444. in_literals = (sec_is_target && !sec_is_source);
  1445. }
  1446. if (do_xtensa_alignment && xtensa_page_power != 0)
  1447. dot += (1 << xtensa_page_power);
  1448. dot = align_power (dot, align_pow);
  1449. section->output_offset = dot;
  1450. dot += section->size;
  1451. }
  1452. break;
  1453. case lang_fill_statement_enum:
  1454. dot += l->fill_statement.size;
  1455. break;
  1456. case lang_padding_statement_enum:
  1457. dot += l->padding_statement.size;
  1458. break;
  1459. default:
  1460. break;
  1461. }
  1462. }
  1463. return dot;
  1464. }
  1465. static bfd_boolean
  1466. ld_local_file_relocations_fit (lang_statement_union_type *statement,
  1467. const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
  1468. {
  1469. /* Walk over all of the dependencies that we identified and make
  1470. sure that IF the source and target are here (addr != 0):
  1471. 1) target addr < source addr
  1472. 2) (roundup(source + source_size, 4) - rounddown(target, 4))
  1473. < (256K - (1 << bad align))
  1474. Need a worst-case proof.... */
  1475. xtensa_ld_iter_stack *stack = NULL;
  1476. xtensa_ld_iter_stack **stack_p = &stack;
  1477. size_t max_align_power = 0;
  1478. size_t align_penalty = 256;
  1479. reloc_deps_e *e;
  1480. size_t i;
  1481. /* Find the worst-case alignment requirement for this set of statements. */
  1482. for (iter_stack_create (stack_p, statement);
  1483. !iter_stack_empty (stack_p);
  1484. iter_stack_next (stack_p))
  1485. {
  1486. lang_statement_union_type *l = iter_stack_current (stack_p);
  1487. if (l->header.type == lang_input_section_enum)
  1488. {
  1489. lang_input_section_type *input = &l->input_section;
  1490. asection *section = input->section;
  1491. if (section->alignment_power > max_align_power)
  1492. max_align_power = section->alignment_power;
  1493. }
  1494. }
  1495. /* Now check that everything fits. */
  1496. for (i = 0; i < deps->count; i++)
  1497. {
  1498. asection *sec = deps->sections[i];
  1499. const reloc_deps_section *deps_section =
  1500. xtensa_get_section_deps (deps, sec);
  1501. if (deps_section)
  1502. {
  1503. /* We choose to walk through the successors. */
  1504. for (e = deps_section->succs; e != NULL; e = e->next)
  1505. {
  1506. if (e->src != e->tgt
  1507. && e->src->output_section == e->tgt->output_section
  1508. && e->src->output_offset != 0
  1509. && e->tgt->output_offset != 0)
  1510. {
  1511. bfd_vma l32r_addr =
  1512. align_power (e->src->output_offset + e->src->size, 2);
  1513. bfd_vma target_addr = e->tgt->output_offset & ~3;
  1514. if (l32r_addr < target_addr)
  1515. {
  1516. fflush (stdout);
  1517. fprintf (stderr, "Warning: "
  1518. "l32r target section before l32r\n");
  1519. fflush (stderr);
  1520. return FALSE;
  1521. }
  1522. if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
  1523. return FALSE;
  1524. }
  1525. }
  1526. }
  1527. }
  1528. return TRUE;
  1529. }
  1530. static bfd_vma
  1531. ld_xtensa_insert_page_offsets (bfd_vma dot,
  1532. lang_statement_union_type *s,
  1533. reloc_deps_graph *deps,
  1534. bfd_boolean lit_align)
  1535. {
  1536. xtensa_ld_iter_stack *stack = NULL;
  1537. xtensa_ld_iter_stack **stack_p = &stack;
  1538. bfd_boolean first_section = FALSE;
  1539. bfd_boolean in_literals = FALSE;
  1540. if (!lit_align)
  1541. return FALSE;
  1542. for (iter_stack_create (stack_p, s);
  1543. !iter_stack_empty (stack_p);
  1544. iter_stack_next (stack_p))
  1545. {
  1546. lang_statement_union_type *l = iter_stack_current (stack_p);
  1547. switch (l->header.type)
  1548. {
  1549. case lang_input_section_enum:
  1550. {
  1551. asection *section = l->input_section.section;
  1552. bfd_boolean do_xtensa_alignment = FALSE;
  1553. if (lit_align)
  1554. {
  1555. if (section->size != 0
  1556. && (first_section
  1557. || (in_literals && !section_is_target (deps, l))
  1558. || (!in_literals && section_is_target (deps, l))))
  1559. {
  1560. do_xtensa_alignment = TRUE;
  1561. }
  1562. first_section = FALSE;
  1563. if (section->size != 0)
  1564. {
  1565. in_literals = (section_is_target (deps, l)
  1566. && !section_is_source (deps, l));
  1567. }
  1568. }
  1569. if (do_xtensa_alignment && xtensa_page_power != 0)
  1570. {
  1571. /* Create an expression that increments the current address,
  1572. i.e., "dot", by (1 << xtensa_align_power). */
  1573. etree_type *name_op = exp_nameop (NAME, ".");
  1574. etree_type *addend_op = exp_intop (1 << xtensa_page_power);
  1575. etree_type *add_op = exp_binop ('+', name_op, addend_op);
  1576. etree_type *assign_op = exp_assign (".", add_op, FALSE);
  1577. lang_assignment_statement_type *assign_stmt;
  1578. lang_statement_union_type *assign_union;
  1579. lang_statement_list_type tmplist;
  1580. /* There is hidden state in "lang_add_assignment". It
  1581. appends the new assignment statement to the stat_ptr
  1582. list. Thus, we swap it before and after the call. */
  1583. lang_list_init (&tmplist);
  1584. push_stat_ptr (&tmplist);
  1585. /* Warning: side effect; statement appended to stat_ptr. */
  1586. assign_stmt = lang_add_assignment (assign_op);
  1587. assign_union = (lang_statement_union_type *) assign_stmt;
  1588. pop_stat_ptr ();
  1589. assign_union->header.next = l;
  1590. *(*stack_p)->iterloc.loc = assign_union;
  1591. iter_stack_next (stack_p);
  1592. }
  1593. }
  1594. break;
  1595. default:
  1596. break;
  1597. }
  1598. }
  1599. return dot;
  1600. }
  1601. EOF
  1602. # Define some shell vars to insert bits of code into the standard ELF
  1603. # parse_args and list_options functions.
  1604. #
  1605. PARSE_AND_LIST_PROLOGUE='
  1606. #define OPTION_OPT_SIZEOPT (300)
  1607. #define OPTION_LITERAL_MOVEMENT (OPTION_OPT_SIZEOPT + 1)
  1608. #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
  1609. extern int elf32xtensa_size_opt;
  1610. extern int elf32xtensa_no_literal_movement;
  1611. '
  1612. PARSE_AND_LIST_LONGOPTS='
  1613. { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
  1614. { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
  1615. { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
  1616. '
  1617. PARSE_AND_LIST_OPTIONS='
  1618. fprintf (file, _("\
  1619. --size-opt When relaxing longcalls, prefer size\n\
  1620. optimization over branch target alignment\n"));
  1621. '
  1622. PARSE_AND_LIST_ARGS_CASES='
  1623. case OPTION_OPT_SIZEOPT:
  1624. elf32xtensa_size_opt = 1;
  1625. break;
  1626. case OPTION_LITERAL_MOVEMENT:
  1627. elf32xtensa_no_literal_movement = 0;
  1628. break;
  1629. case OPTION_NO_LITERAL_MOVEMENT:
  1630. elf32xtensa_no_literal_movement = 1;
  1631. break;
  1632. '
  1633. # Replace some of the standard ELF functions with our own versions.
  1634. #
  1635. LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
  1636. LDEMUL_AFTER_OPEN=elf_xtensa_after_open
  1637. LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
  1638. LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation