mri.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* mri.c -- handle MRI style linker scripts
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Contributed 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. /* This bit does the tree decoration when MRI style link scripts
  18. are parsed. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "bfdlink.h"
  22. #include "ctf-api.h"
  23. #include "ld.h"
  24. #include "ldexp.h"
  25. #include "ldlang.h"
  26. #include "ldmisc.h"
  27. #include "mri.h"
  28. #include <ldgram.h>
  29. #include "libiberty.h"
  30. struct section_name_struct {
  31. struct section_name_struct *next;
  32. const char *name;
  33. const char *alias;
  34. etree_type *vma;
  35. etree_type *align;
  36. etree_type *subalign;
  37. int ok_to_load;
  38. };
  39. static unsigned int symbol_truncate = 10000;
  40. static etree_type *base; /* Relocation base - or null */
  41. static struct section_name_struct *order;
  42. static struct section_name_struct *only_load;
  43. static struct section_name_struct *address;
  44. static struct section_name_struct *alias;
  45. static struct section_name_struct *alignment;
  46. static struct section_name_struct *subalignment;
  47. static struct section_name_struct **
  48. lookup (const char *name, struct section_name_struct **list)
  49. {
  50. struct section_name_struct **ptr = list;
  51. while (*ptr)
  52. {
  53. if (strcmp (name, (*ptr)->name) == 0)
  54. /* If this is a match, delete it, we only keep the last instance
  55. of any name. */
  56. *ptr = (*ptr)->next;
  57. else
  58. ptr = &((*ptr)->next);
  59. }
  60. *ptr = (struct section_name_struct *)
  61. xmalloc (sizeof (struct section_name_struct));
  62. return ptr;
  63. }
  64. static void
  65. mri_add_to_list (struct section_name_struct **list,
  66. const char *name,
  67. etree_type *vma,
  68. const char *zalias,
  69. etree_type *align,
  70. etree_type *subalign)
  71. {
  72. struct section_name_struct **ptr = lookup (name, list);
  73. (*ptr)->name = name;
  74. (*ptr)->vma = vma;
  75. (*ptr)->next = NULL;
  76. (*ptr)->ok_to_load = 0;
  77. (*ptr)->alias = zalias;
  78. (*ptr)->align = align;
  79. (*ptr)->subalign = subalign;
  80. }
  81. void
  82. mri_output_section (const char *name, etree_type *vma)
  83. {
  84. mri_add_to_list (&address, name, vma, 0, 0, 0);
  85. }
  86. /* If any ABSOLUTE <name> are in the script, only load those files
  87. marked thus. */
  88. void
  89. mri_only_load (const char *name)
  90. {
  91. mri_add_to_list (&only_load, name, 0, 0, 0, 0);
  92. }
  93. void
  94. mri_base (etree_type *exp)
  95. {
  96. base = exp;
  97. }
  98. static int done_tree = 0;
  99. void
  100. mri_draw_tree (void)
  101. {
  102. if (done_tree)
  103. return;
  104. /* Now build the statements for the ldlang machine. */
  105. /* Attach the addresses of any which have addresses,
  106. and add the ones not mentioned. */
  107. if (address != NULL)
  108. {
  109. struct section_name_struct *alist;
  110. struct section_name_struct *olist;
  111. if (order == NULL)
  112. order = address;
  113. for (alist = address;
  114. alist != NULL;
  115. alist = alist->next)
  116. {
  117. int done = 0;
  118. for (olist = order; done == 0 && olist != NULL; olist = olist->next)
  119. {
  120. if (strcmp (alist->name, olist->name) == 0)
  121. {
  122. olist->vma = alist->vma;
  123. done = 1;
  124. }
  125. }
  126. if (!done)
  127. {
  128. /* Add this onto end of order list. */
  129. mri_add_to_list (&order, alist->name, alist->vma, 0, 0, 0);
  130. }
  131. }
  132. }
  133. /* If we're only supposed to load a subset of them in, then prune
  134. the list. */
  135. if (only_load != NULL)
  136. {
  137. struct section_name_struct *ptr1;
  138. struct section_name_struct *ptr2;
  139. if (order == NULL)
  140. order = only_load;
  141. /* See if this name is in the list, if it is then we can load it. */
  142. for (ptr1 = only_load; ptr1; ptr1 = ptr1->next)
  143. for (ptr2 = order; ptr2; ptr2 = ptr2->next)
  144. if (strcmp (ptr2->name, ptr1->name) == 0)
  145. ptr2->ok_to_load = 1;
  146. }
  147. else
  148. {
  149. /* No only load list, so everything is ok to load. */
  150. struct section_name_struct *ptr;
  151. for (ptr = order; ptr; ptr = ptr->next)
  152. ptr->ok_to_load = 1;
  153. }
  154. /* Create the order of sections to load. */
  155. if (order != NULL)
  156. {
  157. /* Been told to output the sections in a certain order. */
  158. struct section_name_struct *p = order;
  159. while (p)
  160. {
  161. struct section_name_struct *aptr;
  162. etree_type *align = 0;
  163. etree_type *subalign = 0;
  164. struct wildcard_list *tmp;
  165. /* See if an alignment has been specified. */
  166. for (aptr = alignment; aptr; aptr = aptr->next)
  167. if (strcmp (aptr->name, p->name) == 0)
  168. align = aptr->align;
  169. for (aptr = subalignment; aptr; aptr = aptr->next)
  170. if (strcmp (aptr->name, p->name) == 0)
  171. subalign = aptr->subalign;
  172. if (base == 0)
  173. base = p->vma ? p->vma : exp_nameop (NAME, ".");
  174. lang_enter_output_section_statement (p->name, base,
  175. p->ok_to_load ? normal_section : noload_section, 0,
  176. align, subalign, NULL, 0, 0);
  177. base = 0;
  178. tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
  179. tmp->next = NULL;
  180. tmp->spec.name = p->name;
  181. tmp->spec.exclude_name_list = NULL;
  182. tmp->spec.sorted = none;
  183. tmp->spec.section_flag_list = NULL;
  184. lang_add_wild (NULL, tmp, false);
  185. /* If there is an alias for this section, add it too. */
  186. for (aptr = alias; aptr; aptr = aptr->next)
  187. if (strcmp (aptr->alias, p->name) == 0)
  188. {
  189. tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
  190. tmp->next = NULL;
  191. tmp->spec.name = aptr->name;
  192. tmp->spec.exclude_name_list = NULL;
  193. tmp->spec.sorted = none;
  194. tmp->spec.section_flag_list = NULL;
  195. lang_add_wild (NULL, tmp, false);
  196. }
  197. lang_leave_output_section_statement (0, "*default*", NULL, NULL);
  198. p = p->next;
  199. }
  200. }
  201. done_tree = 1;
  202. }
  203. void
  204. mri_load (const char *name)
  205. {
  206. base = 0;
  207. lang_add_input_file (name, lang_input_file_is_file_enum, NULL);
  208. }
  209. void
  210. mri_order (const char *name)
  211. {
  212. mri_add_to_list (&order, name, 0, 0, 0, 0);
  213. }
  214. void
  215. mri_alias (const char *want, const char *is, int isn)
  216. {
  217. if (!is)
  218. {
  219. char buf[20];
  220. /* Some sections are digits. */
  221. sprintf (buf, "%d", isn);
  222. is = xstrdup (buf);
  223. if (is == NULL)
  224. abort ();
  225. }
  226. mri_add_to_list (&alias, is, 0, want, 0, 0);
  227. }
  228. void
  229. mri_name (const char *name)
  230. {
  231. lang_add_output (name, 1);
  232. }
  233. void
  234. mri_format (const char *name)
  235. {
  236. if (strcmp (name, "S") == 0)
  237. lang_add_output_format ("srec", NULL, NULL, 1);
  238. else
  239. einfo (_("%F%P: unknown format type %s\n"), name);
  240. }
  241. void
  242. mri_public (const char *name, etree_type *exp)
  243. {
  244. lang_add_assignment (exp_assign (name, exp, false));
  245. }
  246. void
  247. mri_align (const char *name, etree_type *exp)
  248. {
  249. mri_add_to_list (&alignment, name, 0, 0, exp, 0);
  250. }
  251. void
  252. mri_alignmod (const char *name, etree_type *exp)
  253. {
  254. mri_add_to_list (&subalignment, name, 0, 0, 0, exp);
  255. }
  256. void
  257. mri_truncate (unsigned int exp)
  258. {
  259. symbol_truncate = exp;
  260. }