ldelfgen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* Emulation code used by all ELF targets.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bfdlink.h"
  19. #include "ctf-api.h"
  20. #include "ld.h"
  21. #include "ldmain.h"
  22. #include "ldmisc.h"
  23. #include "ldexp.h"
  24. #include "ldlang.h"
  25. #include "ldctor.h"
  26. #include "elf-bfd.h"
  27. #include "elf/internal.h"
  28. #include "ldelfgen.h"
  29. /* Info attached to an output_section_statement about input sections,
  30. used when sorting SHF_LINK_ORDER sections. */
  31. struct os_sections
  32. {
  33. /* Size allocated for isec. */
  34. unsigned int alloc;
  35. /* Used entries in isec. */
  36. unsigned int count;
  37. /* How many are SHF_LINK_ORDER. */
  38. unsigned int ordered;
  39. /* Input sections attached to this output section. */
  40. struct os_sections_input {
  41. lang_input_section_type *is;
  42. unsigned int idx;
  43. } isec[1];
  44. };
  45. /* Add IS to data kept for OS. */
  46. static bool
  47. add_link_order_input_section (lang_input_section_type *is,
  48. lang_output_section_statement_type *os)
  49. {
  50. struct os_sections *os_info = os->data;
  51. asection *s;
  52. if (os_info == NULL)
  53. {
  54. os_info = xmalloc (sizeof (*os_info) + 63 * sizeof (*os_info->isec));
  55. os_info->alloc = 64;
  56. os_info->count = 0;
  57. os_info->ordered = 0;
  58. os->data = os_info;
  59. }
  60. if (os_info->count == os_info->alloc)
  61. {
  62. size_t want;
  63. os_info->alloc *= 2;
  64. want = sizeof (*os_info) + (os_info->alloc - 1) * sizeof (*os_info->isec);
  65. os_info = xrealloc (os_info, want);
  66. os->data = os_info;
  67. }
  68. os_info->isec[os_info->count].is = is;
  69. os_info->isec[os_info->count].idx = os_info->count;
  70. os_info->count++;
  71. s = is->section;
  72. if (bfd_get_flavour (s->owner) == bfd_target_elf_flavour
  73. && (s->flags & SEC_LINKER_CREATED) == 0
  74. && elf_linked_to_section (s) != NULL)
  75. os_info->ordered++;
  76. return false;
  77. }
  78. /* Run over the linker's statement list, extracting info about input
  79. sections attached to each output section. */
  80. static bool
  81. link_order_scan (lang_statement_union_type *u,
  82. lang_output_section_statement_type *os)
  83. {
  84. asection *s;
  85. bool ret = false;
  86. for (; u != NULL; u = u->header.next)
  87. {
  88. switch (u->header.type)
  89. {
  90. case lang_wild_statement_enum:
  91. if (link_order_scan (u->wild_statement.children.head, os))
  92. ret = true;
  93. break;
  94. case lang_constructors_statement_enum:
  95. if (link_order_scan (constructor_list.head, os))
  96. ret = true;
  97. break;
  98. case lang_output_section_statement_enum:
  99. if (u->output_section_statement.constraint != -1
  100. && link_order_scan (u->output_section_statement.children.head,
  101. &u->output_section_statement))
  102. ret = true;
  103. break;
  104. case lang_group_statement_enum:
  105. if (link_order_scan (u->group_statement.children.head, os))
  106. ret = true;
  107. break;
  108. case lang_input_section_enum:
  109. s = u->input_section.section;
  110. if (s->output_section != NULL
  111. && s->output_section->owner == link_info.output_bfd
  112. && (s->output_section->flags & SEC_EXCLUDE) == 0
  113. && ((s->output_section->flags & SEC_HAS_CONTENTS) != 0
  114. || ((s->output_section->flags & (SEC_LOAD | SEC_THREAD_LOCAL))
  115. == (SEC_LOAD | SEC_THREAD_LOCAL))))
  116. if (add_link_order_input_section (&u->input_section, os))
  117. ret = true;
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. return ret;
  124. }
  125. /* Compare two sections based on the locations of the sections they are
  126. linked to. Used by fixup_link_order. */
  127. static int
  128. compare_link_order (const void *a, const void *b)
  129. {
  130. const struct os_sections_input *ai = a;
  131. const struct os_sections_input *bi = b;
  132. asection *asec = NULL;
  133. asection *bsec = NULL;
  134. bfd_vma apos, bpos;
  135. if (bfd_get_flavour (ai->is->section->owner) == bfd_target_elf_flavour)
  136. asec = elf_linked_to_section (ai->is->section);
  137. if (bfd_get_flavour (bi->is->section->owner) == bfd_target_elf_flavour)
  138. bsec = elf_linked_to_section (bi->is->section);
  139. /* Place unordered sections before ordered sections. */
  140. if (asec == NULL || bsec == NULL)
  141. {
  142. if (bsec != NULL)
  143. return -1;
  144. else if (asec != NULL)
  145. return 1;
  146. return ai->idx - bi->idx;
  147. }
  148. apos = asec->output_section->lma + asec->output_offset;
  149. bpos = bsec->output_section->lma + bsec->output_offset;
  150. if (apos < bpos)
  151. return -1;
  152. else if (apos > bpos)
  153. return 1;
  154. if (! bfd_link_relocatable (&link_info))
  155. {
  156. /* The only way we should get matching LMAs is when the first of
  157. the two sections has zero size, or asec and bsec are the
  158. same section. */
  159. if (asec->size < bsec->size)
  160. return -1;
  161. else if (asec->size > bsec->size)
  162. return 1;
  163. }
  164. /* If they are both zero size then they almost certainly have the same
  165. VMA and thus are not ordered with respect to each other. Test VMA
  166. anyway, and fall back to idx to make the result reproducible across
  167. qsort implementations. */
  168. apos = asec->output_section->vma + asec->output_offset;
  169. bpos = bsec->output_section->vma + bsec->output_offset;
  170. if (apos < bpos)
  171. return -1;
  172. else if (apos > bpos)
  173. return 1;
  174. else
  175. return ai->idx - bi->idx;
  176. }
  177. /* Rearrange sections with SHF_LINK_ORDER into the same order as their
  178. linked sections. */
  179. static bool
  180. fixup_link_order (lang_output_section_statement_type *os)
  181. {
  182. struct os_sections *os_info = os->data;
  183. unsigned int i, j;
  184. lang_input_section_type **orig_is;
  185. asection **save_s;
  186. for (i = 0; i < os_info->count; i = j)
  187. {
  188. /* Normally a linker script will select SHF_LINK_ORDER sections
  189. with an input section wildcard something like the following:
  190. *(.IA_64.unwind* .gnu.linkonce.ia64unw.*)
  191. However if some other random sections are smashed into an
  192. output section, or if SHF_LINK_ORDER are split up by the
  193. linker script, then we only want to sort sections matching a
  194. given wildcard. That's the purpose of the pattern test. */
  195. for (j = i + 1; j < os_info->count; j++)
  196. if (os_info->isec[j].is->pattern != os_info->isec[i].is->pattern)
  197. break;
  198. if (j - i > 1)
  199. qsort (&os_info->isec[i], j - i, sizeof (*os_info->isec),
  200. compare_link_order);
  201. }
  202. for (i = 0; i < os_info->count; i++)
  203. if (os_info->isec[i].idx != i)
  204. break;
  205. if (i == os_info->count)
  206. return false;
  207. /* Now reorder the linker input section statements to reflect the
  208. proper sorting. The is done by rewriting the existing statements
  209. rather than fiddling with lists, since the only thing we need to
  210. change is the bfd section pointer. */
  211. orig_is = xmalloc (os_info->count * sizeof (*orig_is));
  212. save_s = xmalloc (os_info->count * sizeof (*save_s));
  213. for (i = 0; i < os_info->count; i++)
  214. {
  215. orig_is[os_info->isec[i].idx] = os_info->isec[i].is;
  216. save_s[i] = os_info->isec[i].is->section;
  217. }
  218. for (i = 0; i < os_info->count; i++)
  219. if (os_info->isec[i].idx != i)
  220. {
  221. orig_is[i]->section = save_s[i];
  222. /* Restore os_info to pristine state before the qsort, for the
  223. next pass over sections. */
  224. os_info->isec[i].is = orig_is[i];
  225. os_info->isec[i].idx = i;
  226. }
  227. free (save_s);
  228. free (orig_is);
  229. return true;
  230. }
  231. void
  232. ldelf_map_segments (bool need_layout)
  233. {
  234. int tries = 10;
  235. static bool done_link_order_scan = false;
  236. do
  237. {
  238. lang_relax_sections (need_layout);
  239. need_layout = false;
  240. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  241. {
  242. lang_output_section_statement_type *os;
  243. if (!done_link_order_scan)
  244. {
  245. link_order_scan (statement_list.head, NULL);
  246. done_link_order_scan = true;
  247. }
  248. for (os = (void *) lang_os_list.head; os != NULL; os = os->next)
  249. {
  250. struct os_sections *os_info = os->data;
  251. if (os_info != NULL && os_info->ordered != 0)
  252. {
  253. if (os_info->ordered != os_info->count
  254. && bfd_link_relocatable (&link_info))
  255. {
  256. einfo (_("%F%P: "
  257. "%pA has both ordered and unordered sections\n"),
  258. os->bfd_section);
  259. return;
  260. }
  261. if (os_info->count > 1
  262. && fixup_link_order (os))
  263. need_layout = true;
  264. }
  265. }
  266. }
  267. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
  268. && !bfd_link_relocatable (&link_info))
  269. {
  270. bfd_size_type phdr_size;
  271. phdr_size = elf_program_header_size (link_info.output_bfd);
  272. /* If we don't have user supplied phdrs, throw away any
  273. previous linker generated program headers. */
  274. if (lang_phdr_list == NULL)
  275. elf_seg_map (link_info.output_bfd) = NULL;
  276. if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd,
  277. &link_info,
  278. &need_layout))
  279. einfo (_("%F%P: map sections to segments failed: %E\n"));
  280. if (phdr_size != elf_program_header_size (link_info.output_bfd))
  281. {
  282. if (tries > 6)
  283. /* The first few times we allow any change to
  284. phdr_size . */
  285. need_layout = true;
  286. else if (phdr_size
  287. < elf_program_header_size (link_info.output_bfd))
  288. /* After that we only allow the size to grow. */
  289. need_layout = true;
  290. else
  291. elf_program_header_size (link_info.output_bfd) = phdr_size;
  292. }
  293. }
  294. }
  295. while (need_layout && --tries);
  296. if (tries == 0)
  297. einfo (_("%F%P: looping in map_segments\n"));
  298. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
  299. && lang_phdr_list == NULL)
  300. {
  301. /* If we don't have user supplied phdrs, strip zero-sized dynamic
  302. sections and regenerate program headers. */
  303. const struct elf_backend_data *bed
  304. = get_elf_backend_data (link_info.output_bfd);
  305. if (bed->elf_backend_strip_zero_sized_dynamic_sections
  306. && !bed->elf_backend_strip_zero_sized_dynamic_sections
  307. (&link_info))
  308. einfo (_("%F%P: failed to strip zero-sized dynamic sections\n"));
  309. }
  310. }
  311. #ifdef ENABLE_LIBCTF
  312. /* We want to emit CTF early if and only if we are not targetting ELF with this
  313. invocation. */
  314. int
  315. ldelf_emit_ctf_early (void)
  316. {
  317. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  318. return 0;
  319. return 1;
  320. }
  321. /* Callbacks used to map from bfd types to libctf types, under libctf's
  322. control. */
  323. struct ctf_strtab_iter_cb_arg
  324. {
  325. struct elf_strtab_hash *strtab;
  326. size_t next_i;
  327. size_t next_idx;
  328. };
  329. /* Return strings from the strtab to libctf, one by one. Returns NULL when
  330. iteration is complete. */
  331. static const char *
  332. ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
  333. {
  334. bfd_size_type off;
  335. const char *ret;
  336. struct ctf_strtab_iter_cb_arg *arg =
  337. (struct ctf_strtab_iter_cb_arg *) arg_;
  338. /* There is no zeroth string. */
  339. if (arg->next_i == 0)
  340. arg->next_i = 1;
  341. /* Hunt through strings until we fall off the end or find one with
  342. a nonzero refcount. */
  343. do
  344. {
  345. if (arg->next_i >= _bfd_elf_strtab_len (arg->strtab))
  346. {
  347. arg->next_i = 0;
  348. return NULL;
  349. }
  350. ret = _bfd_elf_strtab_str (arg->strtab, arg->next_i++, &off);
  351. }
  352. while (ret == NULL);
  353. *offset = off;
  354. /* If we've overflowed, we cannot share any further strings: the CTF
  355. format cannot encode strings with such high offsets. */
  356. if (*offset != off)
  357. return NULL;
  358. return ret;
  359. }
  360. void
  361. ldelf_acquire_strings_for_ctf
  362. (struct ctf_dict *ctf_output, struct elf_strtab_hash *strtab)
  363. {
  364. struct ctf_strtab_iter_cb_arg args = { strtab, 0, 0 };
  365. if (!ctf_output)
  366. return;
  367. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  368. {
  369. if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb,
  370. &args) < 0)
  371. einfo (_("%F%P: warning: CTF strtab association failed; strings will "
  372. "not be shared: %s\n"),
  373. ctf_errmsg (ctf_errno (ctf_output)));
  374. }
  375. }
  376. void
  377. ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
  378. struct elf_internal_sym *sym)
  379. {
  380. ctf_link_sym_t lsym;
  381. if (!ctf_output)
  382. return;
  383. /* New symbol. */
  384. if (sym != NULL)
  385. {
  386. lsym.st_name = NULL;
  387. lsym.st_nameidx = sym->st_name;
  388. lsym.st_nameidx_set = 1;
  389. lsym.st_symidx = symidx;
  390. lsym.st_shndx = sym->st_shndx;
  391. lsym.st_type = ELF_ST_TYPE (sym->st_info);
  392. lsym.st_value = sym->st_value;
  393. if (ctf_link_add_linker_symbol (ctf_output, &lsym) < 0)
  394. {
  395. einfo (_("%F%P: warning: CTF symbol addition failed; CTF will "
  396. "not be tied to symbols: %s\n"),
  397. ctf_errmsg (ctf_errno (ctf_output)));
  398. }
  399. }
  400. else
  401. {
  402. /* Shuffle all the symbols. */
  403. if (ctf_link_shuffle_syms (ctf_output) < 0)
  404. einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will "
  405. "not be tied to symbols: %s\n"),
  406. ctf_errmsg (ctf_errno (ctf_output)));
  407. }
  408. }
  409. #else
  410. int
  411. ldelf_emit_ctf_early (void)
  412. {
  413. return 0;
  414. }
  415. void
  416. ldelf_acquire_strings_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
  417. struct elf_strtab_hash *strtab ATTRIBUTE_UNUSED)
  418. {}
  419. void
  420. ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
  421. int symidx ATTRIBUTE_UNUSED,
  422. struct elf_internal_sym *sym ATTRIBUTE_UNUSED)
  423. {}
  424. #endif