mri.c 7.3 KB

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