2001-03-26 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / reloc16.c
blob7de8d95b8edf5e017ad25a4c44e559a03e5bd6f1
1 /* 8 and 16 bit COFF relocation functions, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2000
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>. */
24 /* These routines are used by coff-h8300 and coff-z8k to do
25 relocation.
27 FIXME: This code should be rewritten to support the new COFF
28 linker. Basically, they need to deal with COFF relocs rather than
29 BFD generic relocs. They should store the relocs in some location
30 where coff_link_input_bfd can find them (and coff_link_input_bfd
31 should be changed to use this location rather than rereading the
32 file) (unless info->keep_memory is false, in which case they should
33 free up the relocs after dealing with them). */
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "libbfd.h"
38 #include "bfdlink.h"
39 #include "genlink.h"
40 #include "coff/internal.h"
41 #include "libcoff.h"
43 bfd_vma
44 bfd_coff_reloc16_get_value (reloc, link_info, input_section)
45 arelent *reloc;
46 struct bfd_link_info *link_info;
47 asection *input_section;
49 bfd_vma value;
50 asymbol *symbol = *(reloc->sym_ptr_ptr);
51 /* A symbol holds a pointer to a section, and an offset from the
52 base of the section. To relocate, we find where the section will
53 live in the output and add that in. */
55 if (bfd_is_und_section (symbol->section)
56 || bfd_is_com_section (symbol->section))
58 struct bfd_link_hash_entry *h;
60 /* The symbol is undefined in this BFD. Look it up in the
61 global linker hash table. FIXME: This should be changed when
62 we convert this stuff to use a specific final_link function
63 and change the interface to bfd_relax_section to not require
64 the generic symbols. */
65 h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
66 bfd_asymbol_name (symbol),
67 false, false, true);
68 if (h != (struct bfd_link_hash_entry *) NULL
69 && (h->type == bfd_link_hash_defined
70 || h->type == bfd_link_hash_defweak))
71 value = (h->u.def.value
72 + h->u.def.section->output_section->vma
73 + h->u.def.section->output_offset);
74 else if (h != (struct bfd_link_hash_entry *) NULL
75 && h->type == bfd_link_hash_common)
76 value = h->u.c.size;
77 else
79 if (!((*link_info->callbacks->undefined_symbol)
80 (link_info, bfd_asymbol_name (symbol),
81 input_section->owner, input_section, reloc->address,
82 true)))
83 abort ();
84 value = 0;
87 else
89 value = symbol->value
90 + symbol->section->output_offset
91 + symbol->section->output_section->vma;
94 /* Add the value contained in the relocation. */
95 value += reloc->addend;
97 return value;
100 void
101 bfd_perform_slip (abfd, slip, input_section, value)
102 bfd *abfd;
103 unsigned int slip;
104 asection *input_section;
105 bfd_vma value;
107 asymbol **s;
109 s = _bfd_generic_link_get_symbols (abfd);
110 BFD_ASSERT (s != (asymbol **) NULL);
112 /* Find all symbols past this point, and make them know
113 what's happened. */
114 while (*s)
116 asymbol *p = *s;
117 if (p->section == input_section)
119 /* This was pointing into this section, so mangle it. */
120 if (p->value > value)
122 p->value -= slip;
123 if (p->udata.p != NULL)
125 struct generic_link_hash_entry *h;
127 h = (struct generic_link_hash_entry *) p->udata.p;
128 BFD_ASSERT (h->root.type == bfd_link_hash_defined
129 || h->root.type == bfd_link_hash_defweak);
130 h->root.u.def.value -= slip;
131 BFD_ASSERT (h->root.u.def.value == p->value);
135 s++;
139 boolean
140 bfd_coff_reloc16_relax_section (abfd, i, link_info, again)
141 bfd *abfd;
142 asection *i;
143 struct bfd_link_info *link_info;
144 boolean *again;
146 /* Get enough memory to hold the stuff. */
147 bfd *input_bfd = i->owner;
148 asection *input_section = i;
149 int *shrinks;
150 int shrink = 0;
151 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
152 arelent **reloc_vector = NULL;
153 long reloc_count;
155 /* We only do global relaxation once. It is not safe to do it multiple
156 times (see discussion of the "shrinks" array below). */
157 *again = false;
159 if (reloc_size < 0)
160 return false;
162 reloc_vector = (arelent **) bfd_malloc (reloc_size);
163 if (!reloc_vector && reloc_size > 0)
164 return false;
166 /* Get the relocs and think about them. */
167 reloc_count =
168 bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
169 _bfd_generic_link_get_symbols (input_bfd));
170 if (reloc_count < 0)
172 free (reloc_vector);
173 return false;
176 /* The reloc16.c and related relaxing code is very simple, the price
177 for that simplicity is we can only call this function once for
178 each section.
180 So, to get the best results within that limitation, we do multiple
181 relaxing passes over each section here. That involves keeping track
182 of the "shrink" at each reloc in the section. This allows us to
183 accurately determine the relative location of two relocs within
184 this section.
186 In theory, if we kept the "shrinks" array for each section for the
187 entire link, we could use the generic relaxing code in the linker
188 and get better results, particularly for jsr->bsr and 24->16 bit
189 memory reference relaxations. */
191 if (reloc_count > 0)
193 int another_pass = 0;
195 /* Allocate and initialize the shrinks array for this section.
196 The last element is used as an accumlator of shrinks. */
197 shrinks = (int *) bfd_malloc ((reloc_count + 1) * sizeof (int));
198 memset (shrinks, 0, (reloc_count + 1) * sizeof (int));
200 /* Loop until nothing changes in this section. */
201 do {
202 arelent **parent;
203 unsigned int i;
204 long j;
206 another_pass = 0;
208 for (i = 0, parent = reloc_vector; *parent; parent++, i++)
210 /* Let the target/machine dependent code examine each reloc
211 in this section and attempt to shrink it. */
212 shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
213 shrinks[i], link_info);
215 /* If it shrunk, note it in the shrinks array and set up for
216 another pass. */
217 if (shrink != shrinks[i])
219 another_pass = 1;
220 for (j = i + 1; j <= reloc_count; j++)
221 shrinks[j] += shrink - shrinks[i];
225 while (another_pass);
227 shrink = shrinks[reloc_count];
228 free ((char *) shrinks);
231 input_section->_cooked_size -= shrink;
232 free ((char *) reloc_vector);
233 return true;
236 bfd_byte *
237 bfd_coff_reloc16_get_relocated_section_contents(in_abfd,
238 link_info,
239 link_order,
240 data,
241 relocateable,
242 symbols)
243 bfd *in_abfd;
244 struct bfd_link_info *link_info;
245 struct bfd_link_order *link_order;
246 bfd_byte *data;
247 boolean relocateable;
248 asymbol **symbols;
250 /* Get enough memory to hold the stuff. */
251 bfd *input_bfd = link_order->u.indirect.section->owner;
252 asection *input_section = link_order->u.indirect.section;
253 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
254 arelent **reloc_vector;
255 long reloc_count;
257 if (reloc_size < 0)
258 return NULL;
260 /* If producing relocateable output, don't bother to relax. */
261 if (relocateable)
262 return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
263 link_order,
264 data, relocateable,
265 symbols);
267 /* Read in the section. */
268 if (!bfd_get_section_contents(input_bfd,
269 input_section,
270 data,
272 input_section->_raw_size))
273 return NULL;
275 reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
276 if (!reloc_vector && reloc_size != 0)
277 return NULL;
279 reloc_count = bfd_canonicalize_reloc (input_bfd,
280 input_section,
281 reloc_vector,
282 symbols);
283 if (reloc_count < 0)
285 free (reloc_vector);
286 return NULL;
289 if (reloc_count > 0)
291 arelent **parent = reloc_vector;
292 arelent *reloc;
293 unsigned int dst_address = 0;
294 unsigned int src_address = 0;
295 unsigned int run;
296 unsigned int idx;
298 /* Find how long a run we can do. */
299 while (dst_address < link_order->size)
301 reloc = *parent;
302 if (reloc)
304 /* Note that the relaxing didn't tie up the addresses in the
305 relocation, so we use the original address to work out the
306 run of non-relocated data. */
307 run = reloc->address - src_address;
308 parent++;
310 else
312 run = link_order->size - dst_address;
315 /* Copy the bytes. */
316 for (idx = 0; idx < run; idx++)
317 data[dst_address++] = data[src_address++];
319 /* Now do the relocation. */
320 if (reloc)
322 bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
323 reloc, data, &src_address,
324 &dst_address);
328 free ((char *) reloc_vector);
329 return data;