ldctor.c 9.5 KB

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