ldctor.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* ldctor.c -- constructor support routines
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. By Steve Chamberlain <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. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "safe-ctype.h"
  21. #include "ctf-api.h"
  22. #include "ld.h"
  23. #include "ldexp.h"
  24. #include "ldlang.h"
  25. #include "ldmisc.h"
  26. #include <ldgram.h>
  27. #include "ldmain.h"
  28. #include "ldctor.h"
  29. /* The list of statements needed to handle constructors. These are
  30. invoked by the command CONSTRUCTORS in the linker script. */
  31. lang_statement_list_type constructor_list;
  32. /* Whether the constructors should be sorted. Note that this is
  33. global for the entire link; we assume that there is only a single
  34. CONSTRUCTORS command in the linker script. */
  35. bool constructors_sorted;
  36. /* The sets we have seen. */
  37. struct set_info *sets;
  38. /* Add an entry to a set. H is the entry in the linker hash table.
  39. RELOC is the relocation to use for an entry in the set. SECTION
  40. and VALUE are the value to add. This is called during the first
  41. phase of the link, when we are still gathering symbols together.
  42. We just record the information now. The ldctor_build_sets
  43. function will construct the sets. */
  44. void
  45. ldctor_add_set_entry (struct bfd_link_hash_entry *h,
  46. bfd_reloc_code_real_type reloc,
  47. const char *name,
  48. asection *section,
  49. bfd_vma value)
  50. {
  51. struct set_info *p;
  52. struct set_element *e;
  53. struct set_element **epp;
  54. for (p = sets; p != NULL; p = p->next)
  55. if (p->h == h)
  56. break;
  57. if (p == NULL)
  58. {
  59. p = (struct set_info *) xmalloc (sizeof (struct set_info));
  60. p->next = sets;
  61. sets = p;
  62. p->h = h;
  63. p->reloc = reloc;
  64. p->count = 0;
  65. p->elements = NULL;
  66. }
  67. else
  68. {
  69. if (p->reloc != reloc)
  70. {
  71. einfo (_("%X%P: different relocs used in set %s\n"),
  72. h->root.string);
  73. return;
  74. }
  75. /* Don't permit a set to be constructed from different object
  76. file formats. The same reloc may have different results. We
  77. actually could sometimes handle this, but the case is
  78. unlikely to ever arise. Sometimes constructor symbols are in
  79. unusual sections, such as the absolute section--this appears
  80. to be the case in Linux a.out--and in such cases we just
  81. assume everything is OK. */
  82. if (p->elements != NULL
  83. && section->owner != NULL
  84. && p->elements->section->owner != NULL
  85. && strcmp (bfd_get_target (section->owner),
  86. bfd_get_target (p->elements->section->owner)) != 0)
  87. {
  88. einfo (_("%X%P: different object file formats composing set %s\n"),
  89. h->root.string);
  90. return;
  91. }
  92. }
  93. e = (struct set_element *) xmalloc (sizeof (struct set_element));
  94. e->u.next = NULL;
  95. e->name = name;
  96. e->section = section;
  97. e->value = value;
  98. for (epp = &p->elements; *epp != NULL; epp = &(*epp)->u.next)
  99. ;
  100. *epp = e;
  101. ++p->count;
  102. }
  103. /* Get the priority of a g++ global constructor or destructor from the
  104. symbol name. */
  105. static int
  106. ctor_prio (const char *name)
  107. {
  108. /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
  109. There might be extra leading underscores, and the $ characters
  110. might be something else. The I might be a D. */
  111. while (*name == '_')
  112. ++name;
  113. if (!startswith (name, "GLOBAL_"))
  114. return -1;
  115. name += sizeof "GLOBAL_" - 1;
  116. if (name[0] != name[2])
  117. return -1;
  118. if (name[1] != 'I' && name[1] != 'D')
  119. return -1;
  120. if (!ISDIGIT (name[3]))
  121. return -1;
  122. return atoi (name + 3);
  123. }
  124. /* This function is used to sort constructor elements by priority. It
  125. is called via qsort. */
  126. static int
  127. ctor_cmp (const void *p1, const void *p2)
  128. {
  129. const struct set_element *pe1 = *(const struct set_element **) p1;
  130. const struct set_element *pe2 = *(const struct set_element **) p2;
  131. const char *n1;
  132. const char *n2;
  133. int prio1;
  134. int prio2;
  135. n1 = pe1->name;
  136. if (n1 == NULL)
  137. n1 = "";
  138. n2 = pe2->name;
  139. if (n2 == NULL)
  140. n2 = "";
  141. /* We need to sort in reverse order by priority. When two
  142. constructors have the same priority, we should maintain their
  143. current relative position. */
  144. prio1 = ctor_prio (n1);
  145. prio2 = ctor_prio (n2);
  146. /* We sort in reverse order because that is what g++ expects. */
  147. if (prio1 < prio2)
  148. return 1;
  149. if (prio1 > prio2)
  150. return -1;
  151. /* Force a stable sort. */
  152. if (pe1->u.idx < pe2->u.idx)
  153. return -1;
  154. if (pe1->u.idx > pe2->u.idx)
  155. return 1;
  156. return 0;
  157. }
  158. /* This function is called after the first phase of the link and
  159. before the second phase. At this point all set information has
  160. been gathered. We now put the statements to build the sets
  161. themselves into constructor_list. */
  162. void
  163. ldctor_build_sets (void)
  164. {
  165. static bool called;
  166. bool header_printed;
  167. struct set_info *p;
  168. /* The emulation code may call us directly, but we only want to do
  169. this once. */
  170. if (called)
  171. return;
  172. called = true;
  173. if (constructors_sorted)
  174. {
  175. for (p = sets; p != NULL; p = p->next)
  176. {
  177. int c, i;
  178. struct set_element *e, *enext;
  179. struct set_element **array;
  180. if (p->elements == NULL)
  181. continue;
  182. c = 0;
  183. for (e = p->elements; e != NULL; e = e->u.next)
  184. ++c;
  185. array = (struct set_element **) xmalloc (c * sizeof *array);
  186. i = 0;
  187. for (e = p->elements; e != NULL; e = enext)
  188. {
  189. array[i] = e;
  190. enext = e->u.next;
  191. e->u.idx = i;
  192. ++i;
  193. }
  194. qsort (array, c, sizeof *array, ctor_cmp);
  195. e = array[0];
  196. p->elements = e;
  197. for (i = 0; i < c - 1; i++)
  198. array[i]->u.next = array[i + 1];
  199. array[i]->u.next = NULL;
  200. free (array);
  201. }
  202. }
  203. lang_list_init (&constructor_list);
  204. push_stat_ptr (&constructor_list);
  205. header_printed = false;
  206. for (p = sets; p != NULL; p = p->next)
  207. {
  208. struct set_element *e;
  209. reloc_howto_type *howto;
  210. int reloc_size, size;
  211. /* If the symbol is defined, we may have been invoked from
  212. collect, and the sets may already have been built, so we do
  213. not do anything. */
  214. if (p->h->type == bfd_link_hash_defined
  215. || p->h->type == bfd_link_hash_defweak)
  216. continue;
  217. /* For each set we build:
  218. set:
  219. .long number_of_elements
  220. .long element0
  221. ...
  222. .long elementN
  223. .long 0
  224. except that we use the right size instead of .long. When
  225. generating relocatable output, we generate relocs instead of
  226. addresses. */
  227. howto = bfd_reloc_type_lookup (link_info.output_bfd, p->reloc);
  228. if (howto == NULL)
  229. {
  230. if (bfd_link_relocatable (&link_info))
  231. {
  232. einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
  233. bfd_get_target (link_info.output_bfd),
  234. bfd_get_reloc_code_name (p->reloc),
  235. p->h->root.string);
  236. continue;
  237. }
  238. /* If this is not a relocatable link, all we need is the
  239. size, which we can get from the input BFD. */
  240. if (p->elements->section->owner != NULL)
  241. howto = bfd_reloc_type_lookup (p->elements->section->owner,
  242. p->reloc);
  243. if (howto == NULL)
  244. {
  245. /* See PR 20911 for a reproducer. */
  246. if (p->elements->section->owner == NULL)
  247. einfo (_("%X%P: special section %s does not support reloc %s for set %s\n"),
  248. bfd_section_name (p->elements->section),
  249. bfd_get_reloc_code_name (p->reloc),
  250. p->h->root.string);
  251. else
  252. einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
  253. bfd_get_target (p->elements->section->owner),
  254. bfd_get_reloc_code_name (p->reloc),
  255. p->h->root.string);
  256. continue;
  257. }
  258. }
  259. reloc_size = bfd_get_reloc_size (howto);
  260. switch (reloc_size)
  261. {
  262. case 1: size = BYTE; break;
  263. case 2: size = SHORT; break;
  264. case 4: size = LONG; break;
  265. case 8:
  266. if (howto->complain_on_overflow == complain_overflow_signed)
  267. size = SQUAD;
  268. else
  269. size = QUAD;
  270. break;
  271. default:
  272. einfo (_("%X%P: unsupported size %d for set %s\n"),
  273. bfd_get_reloc_size (howto), p->h->root.string);
  274. size = LONG;
  275. break;
  276. }
  277. lang_add_assignment (exp_assign (".",
  278. exp_unop (ALIGN_K,
  279. exp_intop (reloc_size)),
  280. false));
  281. lang_add_assignment (exp_assign (p->h->root.string,
  282. exp_nameop (NAME, "."),
  283. false));
  284. lang_add_data (size, exp_intop (p->count));
  285. for (e = p->elements; e != NULL; e = e->u.next)
  286. {
  287. if (config.map_file != NULL)
  288. {
  289. int len;
  290. if (!header_printed)
  291. {
  292. minfo (_("\nSet Symbol\n\n"));
  293. header_printed = true;
  294. }
  295. minfo ("%s", p->h->root.string);
  296. len = strlen (p->h->root.string);
  297. if (len >= 19)
  298. {
  299. print_nl ();
  300. len = 0;
  301. }
  302. while (len < 20)
  303. {
  304. print_space ();
  305. ++len;
  306. }
  307. if (e->name != NULL)
  308. minfo ("%pT\n", e->name);
  309. else
  310. minfo ("%G\n", e->section->owner, e->section, e->value);
  311. }
  312. /* Need SEC_KEEP for --gc-sections. */
  313. if (!bfd_is_abs_section (e->section))
  314. e->section->flags |= SEC_KEEP;
  315. if (bfd_link_relocatable (&link_info))
  316. lang_add_reloc (p->reloc, howto, e->section, e->name,
  317. exp_intop (e->value));
  318. else
  319. lang_add_data (size, exp_relop (e->section, e->value));
  320. }
  321. lang_add_data (size, exp_intop (0));
  322. }
  323. pop_stat_ptr ();
  324. }