1 /* 8 and 16 bit COFF relocation functions, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
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. */
23 Most of this hacked by Steve Chamberlain,
27 /* These routines are used by coff-h8300 and coff-z8k to do
30 FIXME: This code should be rewritten to support the new COFF
31 linker. Basically, they need to deal with COFF relocs rather than
32 BFD generic relocs. They should store the relocs in some location
33 where coff_link_input_bfd can find them (and coff_link_input_bfd
34 should be changed to use this location rather than rereading the
35 file) (unless info->keep_memory is false, in which case they should
36 free up the relocs after dealing with them). */
43 #include "coff/internal.h"
47 bfd_coff_reloc16_get_value (reloc, link_info, input_section)
49 struct bfd_link_info *link_info;
50 asection *input_section;
53 asymbol *symbol = *(reloc->sym_ptr_ptr);
54 /* A symbol holds a pointer to a section, and an offset from the
55 base of the section. To relocate, we find where the section will
56 live in the output and add that in */
58 if (bfd_is_und_section (symbol->section)
59 || bfd_is_com_section (symbol->section))
61 struct bfd_link_hash_entry *h;
63 /* The symbol is undefined in this BFD. Look it up in the
64 global linker hash table. FIXME: This should be changed when
65 we convert this stuff to use a specific final_link function
66 and change the interface to bfd_relax_section to not require
67 the generic symbols. */
68 h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
69 bfd_asymbol_name (symbol),
71 if (h != (struct bfd_link_hash_entry *) NULL
72 && (h->type == bfd_link_hash_defined
73 || h->type == bfd_link_hash_defweak))
74 value = (h->u.def.value
75 + h->u.def.section->output_section->vma
76 + h->u.def.section->output_offset);
77 else if (h != (struct bfd_link_hash_entry *) NULL
78 && h->type == bfd_link_hash_common)
82 if (! ((*link_info->callbacks->undefined_symbol)
83 (link_info, bfd_asymbol_name (symbol),
84 input_section->owner, input_section, reloc->address)))
91 value = symbol->value +
92 symbol->section->output_offset +
93 symbol->section->output_section->vma;
96 /* Add the value contained in the relocation */
97 value += reloc->addend;
103 bfd_perform_slip(abfd, slip, input_section, value)
106 asection *input_section;
111 s = _bfd_generic_link_get_symbols (abfd);
112 BFD_ASSERT (s != (asymbol **) NULL);
114 /* Find all symbols past this point, and make them know
119 if (p->section == input_section)
121 /* This was pointing into this section, so mangle it */
122 if (p->value > value)
125 if (p->udata.p != NULL)
127 struct generic_link_hash_entry *h;
129 h = (struct generic_link_hash_entry *) p->udata.p;
130 BFD_ASSERT (h->root.type == bfd_link_hash_defined
131 || h->root.type == bfd_link_hash_defweak);
132 h->root.u.def.value -= slip;
133 BFD_ASSERT (h->root.u.def.value == p->value);
142 bfd_coff_reloc16_relax_section (abfd, i, link_info, again)
145 struct bfd_link_info *link_info;
148 /* Get enough memory to hold the stuff */
149 bfd *input_bfd = i->owner;
150 asection *input_section = i;
153 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
154 arelent **reloc_vector = NULL;
157 /* We only do global relaxation once. It is not safe to do it multiple
158 times (see discussion of the "shrinks" array below). */
164 reloc_vector = (arelent **) bfd_malloc (reloc_size);
165 if (!reloc_vector && reloc_size > 0)
168 /* Get the relocs and think about them */
170 bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
171 _bfd_generic_link_get_symbols (input_bfd));
178 /* The reloc16.c and related relaxing code is very simple, the price
179 for that simplicity is we can only call this function once for
182 So, to get the best results within that limitation, we do multiple
183 relaxing passes over each section here. That involves keeping track
184 of the "shrink" at each reloc in the section. This allows us to
185 accurately determine the relative location of two relocs within
188 In theory, if we kept the "shrinks" array for each section for the
189 entire link, we could use the generic relaxing code in the linker
190 and get better results, particularly for jsr->bsr and 24->16 bit
191 memory reference relaxations. */
195 int another_pass = 0;
197 /* Allocate and initialize the shrinks array for this section. */
198 shrinks = (int *) bfd_malloc (reloc_count * sizeof (int));
199 memset (shrinks, 0, reloc_count * sizeof (int));
201 /* Loop until nothing changes in this section. */
209 for (i = 0, parent = reloc_vector; *parent; parent++, i++)
211 /* Let the target/machine dependent code examine each reloc
212 in this section and attempt to shrink it. */
213 shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
214 shrinks[i], link_info);
216 /* If it shrunk, note it in the shrinks array and set up for
218 if (shrink != shrinks[i])
221 for (j = i + 1; j < reloc_count; j++)
222 shrinks[j] += shrink - shrinks[i];
226 } while (another_pass);
228 free((char *)shrinks);
231 input_section->_cooked_size -= shrink;
232 free((char *)reloc_vector);
237 bfd_coff_reloc16_get_relocated_section_contents(in_abfd,
244 struct bfd_link_info *link_info;
245 struct bfd_link_order *link_order;
247 boolean relocateable;
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;
260 /* If producing relocateable output, don't bother to relax. */
262 return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
267 /* read in the section */
268 if (! bfd_get_section_contents(input_bfd,
272 input_section->_raw_size))
276 reloc_vector = (arelent **) bfd_malloc((size_t) reloc_size);
277 if (!reloc_vector && reloc_size != 0)
280 reloc_count = bfd_canonicalize_reloc (input_bfd,
292 arelent **parent = reloc_vector;
294 unsigned int dst_address = 0;
295 unsigned int src_address = 0;
299 /* Find how long a run we can do */
300 while (dst_address < link_order->size)
305 /* Note that the relaxing didn't tie up the addresses in the
306 relocation, so we use the original address to work out the
307 run of non-relocated data */
308 run = reloc->address - src_address;
313 run = link_order->size - dst_address;
316 for (idx = 0; idx < run; idx++)
318 data[dst_address++] = data[src_address++];
321 /* Now do the relocation */
325 bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
326 reloc, data, &src_address,
331 free((char *)reloc_vector);