Update copyright years
[deliverable/binutils-gdb.git] / bfd / reloc16.c
CommitLineData
252b5132 1/* 8 and 16 bit COFF relocation functions, for BFD.
4b95cf5c 2 Copyright (C) 1990-2014 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support.
4
cd123cb7 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
cd123cb7
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
252b5132 11
cd123cb7
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22
289c596c 23/* Most of this hacked by Steve Chamberlain <sac@cygnus.com>. */
252b5132
RH
24
25/* These routines are used by coff-h8300 and coff-z8k to do
26 relocation.
27
28 FIXME: This code should be rewritten to support the new COFF
29 linker. Basically, they need to deal with COFF relocs rather than
30 BFD generic relocs. They should store the relocs in some location
31 where coff_link_input_bfd can find them (and coff_link_input_bfd
32 should be changed to use this location rather than rereading the
b34976b6 33 file) (unless info->keep_memory is FALSE, in which case they should
252b5132
RH
34 free up the relocs after dealing with them). */
35
252b5132 36#include "sysdep.h"
3db64b00 37#include "bfd.h"
252b5132
RH
38#include "libbfd.h"
39#include "bfdlink.h"
40#include "genlink.h"
41#include "coff/internal.h"
42#include "libcoff.h"
43
44bfd_vma
2c3fc389
NC
45bfd_coff_reloc16_get_value (arelent *reloc,
46 struct bfd_link_info *link_info,
47 asection *input_section)
252b5132
RH
48{
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
289c596c 53 live in the output and add that in. */
252b5132
RH
54
55 if (bfd_is_und_section (symbol->section)
56 || bfd_is_com_section (symbol->section))
57 {
58 struct bfd_link_hash_entry *h;
59
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),
b34976b6 67 FALSE, FALSE, TRUE);
252b5132
RH
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;
fc2db3b8
NC
77 else if (h != (struct bfd_link_hash_entry *) NULL
78 && h->type == bfd_link_hash_undefweak)
79 /* This is a GNU extension. */
80 value = 0;
252b5132
RH
81 else
82 {
289c596c
NC
83 if (!((*link_info->callbacks->undefined_symbol)
84 (link_info, bfd_asymbol_name (symbol),
85 input_section->owner, input_section, reloc->address,
b34976b6 86 TRUE)))
252b5132
RH
87 abort ();
88 value = 0;
89 }
90 }
289c596c 91 else
252b5132 92 {
289c596c
NC
93 value = symbol->value
94 + symbol->section->output_offset
95 + symbol->section->output_section->vma;
252b5132 96 }
289c596c
NC
97
98 /* Add the value contained in the relocation. */
252b5132 99 value += reloc->addend;
289c596c 100
252b5132
RH
101 return value;
102}
103
104void
2c3fc389
NC
105bfd_perform_slip (bfd *abfd,
106 unsigned int slip,
107 asection *input_section,
108 bfd_vma value)
252b5132
RH
109{
110 asymbol **s;
111
112 s = _bfd_generic_link_get_symbols (abfd);
113 BFD_ASSERT (s != (asymbol **) NULL);
114
115 /* Find all symbols past this point, and make them know
289c596c
NC
116 what's happened. */
117 while (*s)
252b5132
RH
118 {
119 asymbol *p = *s;
289c596c 120 if (p->section == input_section)
252b5132 121 {
289c596c 122 /* This was pointing into this section, so mangle it. */
252b5132
RH
123 if (p->value > value)
124 {
125 p->value -= slip;
126 if (p->udata.p != NULL)
127 {
128 struct generic_link_hash_entry *h;
129
130 h = (struct generic_link_hash_entry *) p->udata.p;
131 BFD_ASSERT (h->root.type == bfd_link_hash_defined
132 || h->root.type == bfd_link_hash_defweak);
133 h->root.u.def.value -= slip;
134 BFD_ASSERT (h->root.u.def.value == p->value);
135 }
136 }
137 }
138 s++;
289c596c 139 }
252b5132
RH
140}
141
b34976b6 142bfd_boolean
2c3fc389
NC
143bfd_coff_reloc16_relax_section (bfd *abfd,
144 asection *input_section,
145 struct bfd_link_info *link_info,
146 bfd_boolean *again)
252b5132 147{
289c596c 148 /* Get enough memory to hold the stuff. */
dc810e39
AM
149 bfd *input_bfd = input_section->owner;
150 unsigned *shrinks;
151 unsigned shrink = 0;
252b5132
RH
152 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
153 arelent **reloc_vector = NULL;
154 long reloc_count;
155
c8a1f254
NS
156 if (link_info->relocatable)
157 (*link_info->callbacks->einfo)
158 (_("%P%F: --relax and -r may not be used together\n"));
159
252b5132
RH
160 /* We only do global relaxation once. It is not safe to do it multiple
161 times (see discussion of the "shrinks" array below). */
b34976b6 162 *again = FALSE;
252b5132
RH
163
164 if (reloc_size < 0)
b34976b6 165 return FALSE;
252b5132 166
dc810e39 167 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
252b5132 168 if (!reloc_vector && reloc_size > 0)
b34976b6 169 return FALSE;
252b5132 170
289c596c 171 /* Get the relocs and think about them. */
252b5132
RH
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);
b34976b6 178 return FALSE;
252b5132
RH
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. */
289c596c 195
252b5132
RH
196 if (reloc_count > 0)
197 {
198 int another_pass = 0;
dc810e39 199 bfd_size_type amt;
252b5132 200
c4d5c859 201 /* Allocate and initialize the shrinks array for this section.
7dee875e 202 The last element is used as an accumulator of shrinks. */
dc810e39
AM
203 amt = reloc_count + 1;
204 amt *= sizeof (unsigned);
9bab7074 205 shrinks = (unsigned *) bfd_zmalloc (amt);
252b5132
RH
206
207 /* Loop until nothing changes in this section. */
bc7eab72
KH
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 }
289c596c 233 while (another_pass);
252b5132 234
c4d5c859 235 shrink = shrinks[reloc_count];
289c596c 236 free ((char *) shrinks);
252b5132
RH
237 }
238
e87a64e1 239 input_section->rawsize = input_section->size;
eea6121a 240 input_section->size -= shrink;
289c596c 241 free ((char *) reloc_vector);
b34976b6 242 return TRUE;
252b5132
RH
243}
244
245bfd_byte *
2c3fc389
NC
246bfd_coff_reloc16_get_relocated_section_contents
247 (bfd *in_abfd,
248 struct bfd_link_info *link_info,
249 struct bfd_link_order *link_order,
250 bfd_byte *data,
251 bfd_boolean relocatable,
252 asymbol **symbols)
252b5132 253{
289c596c 254 /* Get enough memory to hold the stuff. */
252b5132
RH
255 bfd *input_bfd = link_order->u.indirect.section->owner;
256 asection *input_section = link_order->u.indirect.section;
257 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
258 arelent **reloc_vector;
259 long reloc_count;
eea6121a 260 bfd_size_type sz;
252b5132
RH
261
262 if (reloc_size < 0)
263 return NULL;
264
1049f94e
AM
265 /* If producing relocatable output, don't bother to relax. */
266 if (relocatable)
252b5132
RH
267 return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
268 link_order,
1049f94e 269 data, relocatable,
252b5132
RH
270 symbols);
271
289c596c 272 /* Read in the section. */
eea6121a
AM
273 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
274 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
252b5132 275 return NULL;
289c596c 276
dc810e39 277 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
252b5132
RH
278 if (!reloc_vector && reloc_size != 0)
279 return NULL;
289c596c
NC
280
281 reloc_count = bfd_canonicalize_reloc (input_bfd,
252b5132
RH
282 input_section,
283 reloc_vector,
284 symbols);
285 if (reloc_count < 0)
286 {
287 free (reloc_vector);
288 return NULL;
289 }
289c596c 290
252b5132
RH
291 if (reloc_count > 0)
292 {
293 arelent **parent = reloc_vector;
289c596c 294 arelent *reloc;
252b5132
RH
295 unsigned int dst_address = 0;
296 unsigned int src_address = 0;
297 unsigned int run;
298 unsigned int idx;
289c596c
NC
299
300 /* Find how long a run we can do. */
301 while (dst_address < link_order->size)
252b5132
RH
302 {
303 reloc = *parent;
289c596c 304 if (reloc)
252b5132
RH
305 {
306 /* Note that the relaxing didn't tie up the addresses in the
307 relocation, so we use the original address to work out the
289c596c 308 run of non-relocated data. */
252b5132
RH
309 run = reloc->address - src_address;
310 parent++;
311 }
289c596c 312 else
252b5132
RH
313 {
314 run = link_order->size - dst_address;
315 }
289c596c
NC
316
317 /* Copy the bytes. */
252b5132 318 for (idx = 0; idx < run; idx++)
289c596c
NC
319 data[dst_address++] = data[src_address++];
320
321 /* Now do the relocation. */
322 if (reloc)
252b5132
RH
323 {
324 bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
325 reloc, data, &src_address,
326 &dst_address);
289c596c 327 }
252b5132
RH
328 }
329 }
289c596c 330 free ((char *) reloc_vector);
252b5132
RH
331 return data;
332}
This page took 0.758176 seconds and 4 git commands to generate.