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