reloc16.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* 8 and 16 bit COFF relocation functions, for BFD.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  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. /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>. */
  18. /* These routines are used by coff-z8k to do relocation.
  19. FIXME: This code should be rewritten to support the new COFF
  20. linker. Basically, they need to deal with COFF relocs rather than
  21. BFD generic relocs. They should store the relocs in some location
  22. where coff_link_input_bfd can find them (and coff_link_input_bfd
  23. should be changed to use this location rather than rereading the
  24. file) (unless info->keep_memory is FALSE, in which case they should
  25. free up the relocs after dealing with them). */
  26. #include "sysdep.h"
  27. #include "bfd.h"
  28. #include "libbfd.h"
  29. #include "bfdlink.h"
  30. #include "genlink.h"
  31. #include "coff/internal.h"
  32. #include "libcoff.h"
  33. bfd_vma
  34. bfd_coff_reloc16_get_value (arelent *reloc,
  35. struct bfd_link_info *link_info,
  36. asection *input_section)
  37. {
  38. bfd_vma value;
  39. asymbol *symbol = *(reloc->sym_ptr_ptr);
  40. /* A symbol holds a pointer to a section, and an offset from the
  41. base of the section. To relocate, we find where the section will
  42. live in the output and add that in. */
  43. if (bfd_is_und_section (symbol->section)
  44. || bfd_is_com_section (symbol->section))
  45. {
  46. struct bfd_link_hash_entry *h;
  47. /* The symbol is undefined in this BFD. Look it up in the
  48. global linker hash table. FIXME: This should be changed when
  49. we convert this stuff to use a specific final_link function
  50. and change the interface to bfd_relax_section to not require
  51. the generic symbols. */
  52. h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
  53. bfd_asymbol_name (symbol),
  54. false, false, true);
  55. if (h != (struct bfd_link_hash_entry *) NULL
  56. && (h->type == bfd_link_hash_defined
  57. || h->type == bfd_link_hash_defweak))
  58. value = (h->u.def.value
  59. + h->u.def.section->output_section->vma
  60. + h->u.def.section->output_offset);
  61. else if (h != (struct bfd_link_hash_entry *) NULL
  62. && h->type == bfd_link_hash_common)
  63. value = h->u.c.size;
  64. else if (h != (struct bfd_link_hash_entry *) NULL
  65. && h->type == bfd_link_hash_undefweak)
  66. /* This is a GNU extension. */
  67. value = 0;
  68. else
  69. {
  70. (*link_info->callbacks->undefined_symbol)
  71. (link_info, bfd_asymbol_name (symbol),
  72. input_section->owner, input_section, reloc->address, true);
  73. value = 0;
  74. }
  75. }
  76. else
  77. {
  78. value = symbol->value
  79. + symbol->section->output_offset
  80. + symbol->section->output_section->vma;
  81. }
  82. /* Add the value contained in the relocation. */
  83. value += reloc->addend;
  84. return value;
  85. }
  86. void
  87. bfd_perform_slip (bfd *abfd,
  88. unsigned int slip,
  89. asection *input_section,
  90. bfd_vma value)
  91. {
  92. asymbol **s;
  93. s = _bfd_generic_link_get_symbols (abfd);
  94. BFD_ASSERT (s != (asymbol **) NULL);
  95. /* Find all symbols past this point, and make them know
  96. what's happened. */
  97. while (*s)
  98. {
  99. asymbol *p = *s;
  100. if (p->section == input_section)
  101. {
  102. /* This was pointing into this section, so mangle it. */
  103. if (p->value > value)
  104. {
  105. p->value -= slip;
  106. if (p->udata.p != NULL)
  107. {
  108. struct generic_link_hash_entry *h;
  109. h = (struct generic_link_hash_entry *) p->udata.p;
  110. BFD_ASSERT (h->root.type == bfd_link_hash_defined
  111. || h->root.type == bfd_link_hash_defweak);
  112. h->root.u.def.value -= slip;
  113. BFD_ASSERT (h->root.u.def.value == p->value);
  114. }
  115. }
  116. }
  117. s++;
  118. }
  119. }
  120. bool
  121. bfd_coff_reloc16_relax_section (bfd *abfd,
  122. asection *input_section,
  123. struct bfd_link_info *link_info,
  124. bool *again)
  125. {
  126. /* Get enough memory to hold the stuff. */
  127. bfd *input_bfd = input_section->owner;
  128. unsigned *shrinks;
  129. unsigned shrink = 0;
  130. long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  131. arelent **reloc_vector = NULL;
  132. long reloc_count;
  133. if (bfd_link_relocatable (link_info))
  134. (*link_info->callbacks->einfo)
  135. (_("%P%F: --relax and -r may not be used together\n"));
  136. /* We only do global relaxation once. It is not safe to do it multiple
  137. times (see discussion of the "shrinks" array below). */
  138. *again = false;
  139. if (reloc_size < 0)
  140. return false;
  141. reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
  142. if (!reloc_vector && reloc_size > 0)
  143. return false;
  144. /* Get the relocs and think about them. */
  145. reloc_count =
  146. bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
  147. _bfd_generic_link_get_symbols (input_bfd));
  148. if (reloc_count < 0)
  149. {
  150. free (reloc_vector);
  151. return false;
  152. }
  153. /* The reloc16.c and related relaxing code is very simple, the price
  154. for that simplicity is we can only call this function once for
  155. each section.
  156. So, to get the best results within that limitation, we do multiple
  157. relaxing passes over each section here. That involves keeping track
  158. of the "shrink" at each reloc in the section. This allows us to
  159. accurately determine the relative location of two relocs within
  160. this section.
  161. In theory, if we kept the "shrinks" array for each section for the
  162. entire link, we could use the generic relaxing code in the linker
  163. and get better results, particularly for jsr->bsr and 24->16 bit
  164. memory reference relaxations. */
  165. if (reloc_count > 0)
  166. {
  167. int another_pass = 0;
  168. bfd_size_type amt;
  169. /* Allocate and initialize the shrinks array for this section.
  170. The last element is used as an accumulator of shrinks. */
  171. amt = reloc_count + 1;
  172. amt *= sizeof (unsigned);
  173. shrinks = (unsigned *) bfd_zmalloc (amt);
  174. /* Loop until nothing changes in this section. */
  175. do
  176. {
  177. arelent **parent;
  178. unsigned int i;
  179. long j;
  180. another_pass = 0;
  181. for (i = 0, parent = reloc_vector; *parent; parent++, i++)
  182. {
  183. /* Let the target/machine dependent code examine each reloc
  184. in this section and attempt to shrink it. */
  185. shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
  186. shrinks[i], link_info);
  187. /* If it shrunk, note it in the shrinks array and set up for
  188. another pass. */
  189. if (shrink != shrinks[i])
  190. {
  191. another_pass = 1;
  192. for (j = i + 1; j <= reloc_count; j++)
  193. shrinks[j] += shrink - shrinks[i];
  194. }
  195. }
  196. }
  197. while (another_pass);
  198. shrink = shrinks[reloc_count];
  199. free ((char *) shrinks);
  200. }
  201. input_section->rawsize = input_section->size;
  202. input_section->size -= shrink;
  203. free ((char *) reloc_vector);
  204. return true;
  205. }
  206. bfd_byte *
  207. bfd_coff_reloc16_get_relocated_section_contents
  208. (bfd *in_abfd,
  209. struct bfd_link_info *link_info,
  210. struct bfd_link_order *link_order,
  211. bfd_byte *data,
  212. bool relocatable,
  213. asymbol **symbols)
  214. {
  215. /* Get enough memory to hold the stuff. */
  216. bfd *input_bfd = link_order->u.indirect.section->owner;
  217. asection *input_section = link_order->u.indirect.section;
  218. long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  219. arelent **reloc_vector;
  220. long reloc_count;
  221. bfd_size_type sz;
  222. if (reloc_size < 0)
  223. return NULL;
  224. /* If producing relocatable output, don't bother to relax. */
  225. if (relocatable)
  226. return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
  227. link_order,
  228. data, relocatable,
  229. symbols);
  230. /* Read in the section. */
  231. sz = input_section->rawsize ? input_section->rawsize : input_section->size;
  232. if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
  233. return NULL;
  234. reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
  235. if (!reloc_vector && reloc_size != 0)
  236. return NULL;
  237. reloc_count = bfd_canonicalize_reloc (input_bfd,
  238. input_section,
  239. reloc_vector,
  240. symbols);
  241. if (reloc_count < 0)
  242. {
  243. free (reloc_vector);
  244. return NULL;
  245. }
  246. if (reloc_count > 0)
  247. {
  248. arelent **parent = reloc_vector;
  249. arelent *reloc;
  250. unsigned int dst_address = 0;
  251. unsigned int src_address = 0;
  252. unsigned int run;
  253. unsigned int idx;
  254. /* Find how long a run we can do. */
  255. while (dst_address < link_order->size)
  256. {
  257. reloc = *parent;
  258. if (reloc)
  259. {
  260. /* Note that the relaxing didn't tie up the addresses in the
  261. relocation, so we use the original address to work out the
  262. run of non-relocated data. */
  263. run = reloc->address - src_address;
  264. parent++;
  265. }
  266. else
  267. {
  268. run = link_order->size - dst_address;
  269. }
  270. /* Copy the bytes. */
  271. for (idx = 0; idx < run; idx++)
  272. data[dst_address++] = data[src_address++];
  273. /* Now do the relocation. */
  274. if (reloc)
  275. {
  276. bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
  277. reloc, data, &src_address,
  278. &dst_address);
  279. }
  280. }
  281. }
  282. free ((char *) reloc_vector);
  283. return data;
  284. }