]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/reloc16.c
Fix gdb.tui/completion.exp test
[thirdparty/binutils-gdb.git] / bfd / reloc16.c
CommitLineData
252b5132 1/* 8 and 16 bit COFF relocation functions, for BFD.
2571583a 2 Copyright (C) 1990-2017 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 {
1a72702b
AM
83 (*link_info->callbacks->undefined_symbol)
84 (link_info, bfd_asymbol_name (symbol),
85 input_section->owner, input_section, reloc->address, TRUE);
252b5132
RH
86 value = 0;
87 }
88 }
289c596c 89 else
252b5132 90 {
289c596c
NC
91 value = symbol->value
92 + symbol->section->output_offset
93 + symbol->section->output_section->vma;
252b5132 94 }
289c596c
NC
95
96 /* Add the value contained in the relocation. */
252b5132 97 value += reloc->addend;
289c596c 98
252b5132
RH
99 return value;
100}
101
102void
2c3fc389
NC
103bfd_perform_slip (bfd *abfd,
104 unsigned int slip,
105 asection *input_section,
106 bfd_vma value)
252b5132
RH
107{
108 asymbol **s;
109
110 s = _bfd_generic_link_get_symbols (abfd);
111 BFD_ASSERT (s != (asymbol **) NULL);
112
113 /* Find all symbols past this point, and make them know
289c596c
NC
114 what's happened. */
115 while (*s)
252b5132
RH
116 {
117 asymbol *p = *s;
289c596c 118 if (p->section == input_section)
252b5132 119 {
289c596c 120 /* This was pointing into this section, so mangle it. */
252b5132
RH
121 if (p->value > value)
122 {
123 p->value -= slip;
124 if (p->udata.p != NULL)
125 {
126 struct generic_link_hash_entry *h;
127
128 h = (struct generic_link_hash_entry *) p->udata.p;
129 BFD_ASSERT (h->root.type == bfd_link_hash_defined
130 || h->root.type == bfd_link_hash_defweak);
131 h->root.u.def.value -= slip;
132 BFD_ASSERT (h->root.u.def.value == p->value);
133 }
134 }
135 }
136 s++;
289c596c 137 }
252b5132
RH
138}
139
b34976b6 140bfd_boolean
2c3fc389
NC
141bfd_coff_reloc16_relax_section (bfd *abfd,
142 asection *input_section,
143 struct bfd_link_info *link_info,
144 bfd_boolean *again)
252b5132 145{
289c596c 146 /* Get enough memory to hold the stuff. */
dc810e39
AM
147 bfd *input_bfd = input_section->owner;
148 unsigned *shrinks;
149 unsigned shrink = 0;
252b5132
RH
150 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
151 arelent **reloc_vector = NULL;
152 long reloc_count;
153
0e1862bb 154 if (bfd_link_relocatable (link_info))
c8a1f254
NS
155 (*link_info->callbacks->einfo)
156 (_("%P%F: --relax and -r may not be used together\n"));
157
252b5132
RH
158 /* We only do global relaxation once. It is not safe to do it multiple
159 times (see discussion of the "shrinks" array below). */
b34976b6 160 *again = FALSE;
252b5132
RH
161
162 if (reloc_size < 0)
b34976b6 163 return FALSE;
252b5132 164
dc810e39 165 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
252b5132 166 if (!reloc_vector && reloc_size > 0)
b34976b6 167 return FALSE;
252b5132 168
289c596c 169 /* Get the relocs and think about them. */
252b5132
RH
170 reloc_count =
171 bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
172 _bfd_generic_link_get_symbols (input_bfd));
173 if (reloc_count < 0)
174 {
175 free (reloc_vector);
b34976b6 176 return FALSE;
252b5132
RH
177 }
178
179 /* The reloc16.c and related relaxing code is very simple, the price
180 for that simplicity is we can only call this function once for
181 each section.
182
183 So, to get the best results within that limitation, we do multiple
184 relaxing passes over each section here. That involves keeping track
185 of the "shrink" at each reloc in the section. This allows us to
186 accurately determine the relative location of two relocs within
187 this section.
188
189 In theory, if we kept the "shrinks" array for each section for the
190 entire link, we could use the generic relaxing code in the linker
191 and get better results, particularly for jsr->bsr and 24->16 bit
192 memory reference relaxations. */
289c596c 193
252b5132
RH
194 if (reloc_count > 0)
195 {
196 int another_pass = 0;
dc810e39 197 bfd_size_type amt;
252b5132 198
c4d5c859 199 /* Allocate and initialize the shrinks array for this section.
7dee875e 200 The last element is used as an accumulator of shrinks. */
dc810e39
AM
201 amt = reloc_count + 1;
202 amt *= sizeof (unsigned);
9bab7074 203 shrinks = (unsigned *) bfd_zmalloc (amt);
252b5132
RH
204
205 /* Loop until nothing changes in this section. */
bc7eab72
KH
206 do
207 {
208 arelent **parent;
209 unsigned int i;
210 long j;
211
212 another_pass = 0;
213
214 for (i = 0, parent = reloc_vector; *parent; parent++, i++)
215 {
216 /* Let the target/machine dependent code examine each reloc
217 in this section and attempt to shrink it. */
218 shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
219 shrinks[i], link_info);
220
221 /* If it shrunk, note it in the shrinks array and set up for
222 another pass. */
223 if (shrink != shrinks[i])
224 {
225 another_pass = 1;
226 for (j = i + 1; j <= reloc_count; j++)
227 shrinks[j] += shrink - shrinks[i];
228 }
229 }
230 }
289c596c 231 while (another_pass);
252b5132 232
c4d5c859 233 shrink = shrinks[reloc_count];
289c596c 234 free ((char *) shrinks);
252b5132
RH
235 }
236
e87a64e1 237 input_section->rawsize = input_section->size;
eea6121a 238 input_section->size -= shrink;
289c596c 239 free ((char *) reloc_vector);
b34976b6 240 return TRUE;
252b5132
RH
241}
242
243bfd_byte *
2c3fc389
NC
244bfd_coff_reloc16_get_relocated_section_contents
245 (bfd *in_abfd,
246 struct bfd_link_info *link_info,
247 struct bfd_link_order *link_order,
248 bfd_byte *data,
249 bfd_boolean relocatable,
250 asymbol **symbols)
252b5132 251{
289c596c 252 /* Get enough memory to hold the stuff. */
252b5132
RH
253 bfd *input_bfd = link_order->u.indirect.section->owner;
254 asection *input_section = link_order->u.indirect.section;
255 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
256 arelent **reloc_vector;
257 long reloc_count;
eea6121a 258 bfd_size_type sz;
252b5132
RH
259
260 if (reloc_size < 0)
261 return NULL;
262
1049f94e
AM
263 /* If producing relocatable output, don't bother to relax. */
264 if (relocatable)
252b5132
RH
265 return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
266 link_order,
1049f94e 267 data, relocatable,
252b5132
RH
268 symbols);
269
289c596c 270 /* Read in the section. */
eea6121a
AM
271 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
272 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
252b5132 273 return NULL;
289c596c 274
dc810e39 275 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
252b5132
RH
276 if (!reloc_vector && reloc_size != 0)
277 return NULL;
289c596c
NC
278
279 reloc_count = bfd_canonicalize_reloc (input_bfd,
252b5132
RH
280 input_section,
281 reloc_vector,
282 symbols);
283 if (reloc_count < 0)
284 {
285 free (reloc_vector);
286 return NULL;
287 }
289c596c 288
252b5132
RH
289 if (reloc_count > 0)
290 {
291 arelent **parent = reloc_vector;
289c596c 292 arelent *reloc;
252b5132
RH
293 unsigned int dst_address = 0;
294 unsigned int src_address = 0;
295 unsigned int run;
296 unsigned int idx;
289c596c
NC
297
298 /* Find how long a run we can do. */
299 while (dst_address < link_order->size)
252b5132
RH
300 {
301 reloc = *parent;
289c596c 302 if (reloc)
252b5132
RH
303 {
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
289c596c 306 run of non-relocated data. */
252b5132
RH
307 run = reloc->address - src_address;
308 parent++;
309 }
289c596c 310 else
252b5132
RH
311 {
312 run = link_order->size - dst_address;
313 }
289c596c
NC
314
315 /* Copy the bytes. */
252b5132 316 for (idx = 0; idx < run; idx++)
289c596c
NC
317 data[dst_address++] = data[src_address++];
318
319 /* Now do the relocation. */
320 if (reloc)
252b5132
RH
321 {
322 bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
323 reloc, data, &src_address,
324 &dst_address);
289c596c 325 }
252b5132
RH
326 }
327 }
289c596c 328 free ((char *) reloc_vector);
252b5132
RH
329 return data;
330}