Commit | Line | Data |
---|---|---|
c2dcd04e | 1 | /* BFD back-end for Renesas H8/300 ELF binaries. |
2627de83 | 2 | Copyright 1993, 1995, 1998, 1999, 2001, 2002, 2003, 2004 |
73ff0d56 | 3 | Free Software Foundation, Inc. |
e01b0e69 | 4 | |
e514ac71 | 5 | This file is part of BFD, the Binary File Descriptor library. |
e01b0e69 | 6 | |
e514ac71 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 2 of the License, or | |
10 | (at your option) any later version. | |
e01b0e69 | 11 | |
e514ac71 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. | |
e01b0e69 | 16 | |
e514ac71 NC |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
e01b0e69 JR |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "elf/h8.h" | |
26 | ||
27 | static reloc_howto_type *elf32_h8_reloc_type_lookup | |
c6baf75e | 28 | (bfd *abfd, bfd_reloc_code_real_type code); |
e01b0e69 | 29 | static void elf32_h8_info_to_howto |
c6baf75e | 30 | (bfd *, arelent *, Elf_Internal_Rela *); |
5e47149d | 31 | static void elf32_h8_info_to_howto_rel |
c6baf75e | 32 | (bfd *, arelent *, Elf_Internal_Rela *); |
96ef1419 KH |
33 | static unsigned long elf32_h8_mach (flagword); |
34 | static void elf32_h8_final_write_processing (bfd *, bfd_boolean); | |
35 | static bfd_boolean elf32_h8_object_p (bfd *); | |
36 | static bfd_boolean elf32_h8_merge_private_bfd_data (bfd *, bfd *); | |
b34976b6 | 37 | static bfd_boolean elf32_h8_relax_section |
c6baf75e | 38 | (bfd *, asection *, struct bfd_link_info *, bfd_boolean *); |
b34976b6 | 39 | static bfd_boolean elf32_h8_relax_delete_bytes |
c6baf75e | 40 | (bfd *, asection *, bfd_vma, int); |
96ef1419 | 41 | static bfd_boolean elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma); |
dc810e39 | 42 | static bfd_byte *elf32_h8_get_relocated_section_contents |
c6baf75e RS |
43 | (bfd *, struct bfd_link_info *, struct bfd_link_order *, |
44 | bfd_byte *, bfd_boolean, asymbol **); | |
96ef1419 | 45 | static asection *elf32_h8_gc_mark_hook |
2627de83 AM |
46 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, |
47 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
48 | static bfd_boolean elf32_h8_gc_sweep_hook | |
49 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
5e47149d | 50 | static bfd_reloc_status_type elf32_h8_final_link_relocate |
c6baf75e RS |
51 | (unsigned long, bfd *, bfd *, asection *, |
52 | bfd_byte *, bfd_vma, bfd_vma, bfd_vma, | |
53 | struct bfd_link_info *, asection *, int); | |
b34976b6 | 54 | static bfd_boolean elf32_h8_relocate_section |
c6baf75e RS |
55 | (bfd *, struct bfd_link_info *, bfd *, asection *, |
56 | bfd_byte *, Elf_Internal_Rela *, | |
57 | Elf_Internal_Sym *, asection **); | |
dc810e39 | 58 | static bfd_reloc_status_type special |
c6baf75e | 59 | (bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **); |
e01b0e69 JR |
60 | |
61 | /* This does not include any relocation information, but should be | |
62 | good enough for GDB or objdump to read the file. */ | |
63 | ||
bc7eab72 | 64 | static reloc_howto_type h8_elf_howto_table[] = { |
e01b0e69 JR |
65 | #define R_H8_NONE_X 0 |
66 | HOWTO (R_H8_NONE, /* type */ | |
67 | 0, /* rightshift */ | |
68 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
69 | 0, /* bitsize */ | |
b34976b6 | 70 | FALSE, /* pc_relative */ |
e01b0e69 | 71 | 0, /* bitpos */ |
9d29900b NC |
72 | complain_overflow_dont,/* complain_on_overflow */ |
73 | special, /* special_function */ | |
e01b0e69 | 74 | "R_H8_NONE", /* name */ |
b34976b6 | 75 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
76 | 0, /* src_mask */ |
77 | 0, /* dst_mask */ | |
b34976b6 | 78 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
79 | #define R_H8_DIR32_X (R_H8_NONE_X + 1) |
80 | HOWTO (R_H8_DIR32, /* type */ | |
81 | 0, /* rightshift */ | |
82 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
83 | 32, /* bitsize */ | |
b34976b6 | 84 | FALSE, /* pc_relative */ |
e01b0e69 | 85 | 0, /* bitpos */ |
9d29900b NC |
86 | complain_overflow_dont,/* complain_on_overflow */ |
87 | special, /* special_function */ | |
e01b0e69 | 88 | "R_H8_DIR32", /* name */ |
b34976b6 | 89 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
90 | 0, /* src_mask */ |
91 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 92 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
93 | #define R_H8_DIR16_X (R_H8_DIR32_X + 1) |
94 | HOWTO (R_H8_DIR16, /* type */ | |
95 | 0, /* rightshift */ | |
96 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
97 | 16, /* bitsize */ | |
b34976b6 | 98 | FALSE, /* pc_relative */ |
e01b0e69 | 99 | 0, /* bitpos */ |
9d29900b NC |
100 | complain_overflow_dont,/* complain_on_overflow */ |
101 | special, /* special_function */ | |
e01b0e69 | 102 | "R_H8_DIR16", /* name */ |
b34976b6 | 103 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
104 | 0, /* src_mask */ |
105 | 0x0000ffff, /* dst_mask */ | |
b34976b6 | 106 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
107 | #define R_H8_DIR8_X (R_H8_DIR16_X + 1) |
108 | HOWTO (R_H8_DIR8, /* type */ | |
109 | 0, /* rightshift */ | |
110 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
111 | 8, /* bitsize */ | |
b34976b6 | 112 | FALSE, /* pc_relative */ |
e01b0e69 | 113 | 0, /* bitpos */ |
9d29900b NC |
114 | complain_overflow_dont,/* complain_on_overflow */ |
115 | special, /* special_function */ | |
116 | "R_H8_DIR8", /* name */ | |
b34976b6 | 117 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
118 | 0, /* src_mask */ |
119 | 0x000000ff, /* dst_mask */ | |
b34976b6 | 120 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
121 | #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1) |
122 | HOWTO (R_H8_DIR16A8, /* type */ | |
123 | 0, /* rightshift */ | |
124 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
125 | 16, /* bitsize */ | |
b34976b6 | 126 | FALSE, /* pc_relative */ |
e01b0e69 JR |
127 | 0, /* bitpos */ |
128 | complain_overflow_bitfield, /* complain_on_overflow */ | |
9d29900b | 129 | special, /* special_function */ |
e01b0e69 | 130 | "R_H8_DIR16A8", /* name */ |
b34976b6 | 131 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
132 | 0, /* src_mask */ |
133 | 0x0000ffff, /* dst_mask */ | |
b34976b6 | 134 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
135 | #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1) |
136 | HOWTO (R_H8_DIR16R8, /* type */ | |
137 | 0, /* rightshift */ | |
138 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
139 | 16, /* bitsize */ | |
b34976b6 | 140 | FALSE, /* pc_relative */ |
e01b0e69 JR |
141 | 0, /* bitpos */ |
142 | complain_overflow_bitfield, /* complain_on_overflow */ | |
9d29900b | 143 | special, /* special_function */ |
e01b0e69 | 144 | "R_H8_DIR16R8", /* name */ |
b34976b6 | 145 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
146 | 0, /* src_mask */ |
147 | 0x0000ffff, /* dst_mask */ | |
b34976b6 | 148 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
149 | #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1) |
150 | HOWTO (R_H8_DIR24A8, /* type */ | |
151 | 0, /* rightshift */ | |
152 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
153 | 24, /* bitsize */ | |
b34976b6 | 154 | FALSE, /* pc_relative */ |
e01b0e69 JR |
155 | 0, /* bitpos */ |
156 | complain_overflow_bitfield, /* complain_on_overflow */ | |
9d29900b | 157 | special, /* special_function */ |
e01b0e69 | 158 | "R_H8_DIR24A8", /* name */ |
b34976b6 | 159 | TRUE, /* partial_inplace */ |
e01b0e69 JR |
160 | 0xff000000, /* src_mask */ |
161 | 0x00ffffff, /* dst_mask */ | |
b34976b6 | 162 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
163 | #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1) |
164 | HOWTO (R_H8_DIR24R8, /* type */ | |
165 | 0, /* rightshift */ | |
166 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
167 | 24, /* bitsize */ | |
b34976b6 | 168 | FALSE, /* pc_relative */ |
e01b0e69 JR |
169 | 0, /* bitpos */ |
170 | complain_overflow_bitfield, /* complain_on_overflow */ | |
9d29900b | 171 | special, /* special_function */ |
e01b0e69 | 172 | "R_H8_DIR24R8", /* name */ |
b34976b6 | 173 | TRUE, /* partial_inplace */ |
e01b0e69 JR |
174 | 0xff000000, /* src_mask */ |
175 | 0x00ffffff, /* dst_mask */ | |
b34976b6 | 176 | FALSE), /* pcrel_offset */ |
e01b0e69 JR |
177 | #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1) |
178 | HOWTO (R_H8_DIR32A16, /* type */ | |
179 | 0, /* rightshift */ | |
180 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
181 | 32, /* bitsize */ | |
b34976b6 | 182 | FALSE, /* pc_relative */ |
e01b0e69 | 183 | 0, /* bitpos */ |
9d29900b NC |
184 | complain_overflow_dont,/* complain_on_overflow */ |
185 | special, /* special_function */ | |
8c17da6e | 186 | "R_H8_DIR32A16", /* name */ |
b34976b6 | 187 | FALSE, /* partial_inplace */ |
e01b0e69 JR |
188 | 0, /* src_mask */ |
189 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 190 | FALSE), /* pcrel_offset */ |
f2352488 JL |
191 | #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1) |
192 | HOWTO (R_H8_PCREL16, /* type */ | |
193 | 0, /* rightshift */ | |
194 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
195 | 16, /* bitsize */ | |
b34976b6 | 196 | TRUE, /* pc_relative */ |
f2352488 | 197 | 0, /* bitpos */ |
9d29900b NC |
198 | complain_overflow_signed,/* complain_on_overflow */ |
199 | special, /* special_function */ | |
f2352488 | 200 | "R_H8_PCREL16", /* name */ |
b34976b6 | 201 | FALSE, /* partial_inplace */ |
f2352488 JL |
202 | 0xffff, /* src_mask */ |
203 | 0xffff, /* dst_mask */ | |
b34976b6 | 204 | TRUE), /* pcrel_offset */ |
f2352488 JL |
205 | #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1) |
206 | HOWTO (R_H8_PCREL8, /* type */ | |
207 | 0, /* rightshift */ | |
208 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
209 | 8, /* bitsize */ | |
b34976b6 | 210 | TRUE, /* pc_relative */ |
f2352488 | 211 | 0, /* bitpos */ |
9d29900b NC |
212 | complain_overflow_signed,/* complain_on_overflow */ |
213 | special, /* special_function */ | |
f2352488 | 214 | "R_H8_PCREL8", /* name */ |
b34976b6 | 215 | FALSE, /* partial_inplace */ |
f2352488 JL |
216 | 0xff, /* src_mask */ |
217 | 0xff, /* dst_mask */ | |
b34976b6 | 218 | TRUE), /* pcrel_offset */ |
e01b0e69 JR |
219 | }; |
220 | ||
221 | /* This structure is used to map BFD reloc codes to H8 ELF relocs. */ | |
222 | ||
bc7eab72 | 223 | struct elf_reloc_map { |
e01b0e69 JR |
224 | bfd_reloc_code_real_type bfd_reloc_val; |
225 | unsigned char howto_index; | |
226 | }; | |
227 | ||
6288878d | 228 | /* An array mapping BFD reloc codes to H8 ELF relocs. */ |
e01b0e69 | 229 | |
bc7eab72 | 230 | static const struct elf_reloc_map h8_reloc_map[] = { |
e01b0e69 JR |
231 | { BFD_RELOC_NONE, R_H8_NONE_X }, |
232 | { BFD_RELOC_32, R_H8_DIR32_X }, | |
233 | { BFD_RELOC_16, R_H8_DIR16_X }, | |
234 | { BFD_RELOC_8, R_H8_DIR8_X }, | |
235 | { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X }, | |
236 | { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X }, | |
237 | { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X }, | |
238 | { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X }, | |
239 | { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X }, | |
f2352488 JL |
240 | { BFD_RELOC_16_PCREL, R_H8_PCREL16_X }, |
241 | { BFD_RELOC_8_PCREL, R_H8_PCREL8_X }, | |
e01b0e69 JR |
242 | }; |
243 | ||
0a83638b | 244 | |
e01b0e69 | 245 | static reloc_howto_type * |
c6baf75e RS |
246 | elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
247 | bfd_reloc_code_real_type code) | |
e01b0e69 JR |
248 | { |
249 | unsigned int i; | |
250 | ||
251 | for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
252 | { | |
253 | if (h8_reloc_map[i].bfd_reloc_val == code) | |
254 | return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index]; | |
255 | } | |
256 | return NULL; | |
257 | } | |
258 | ||
259 | static void | |
c6baf75e RS |
260 | elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc, |
261 | Elf_Internal_Rela *elf_reloc) | |
e01b0e69 JR |
262 | { |
263 | unsigned int r; | |
264 | unsigned int i; | |
265 | ||
266 | r = ELF32_R_TYPE (elf_reloc->r_info); | |
267 | for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++) | |
bc7eab72 | 268 | if (h8_elf_howto_table[i].type == r) |
e01b0e69 JR |
269 | { |
270 | bfd_reloc->howto = &h8_elf_howto_table[i]; | |
271 | return; | |
272 | } | |
273 | abort (); | |
274 | } | |
275 | ||
276 | static void | |
c6baf75e RS |
277 | elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc, |
278 | Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED) | |
e01b0e69 JR |
279 | { |
280 | unsigned int r; | |
281 | ||
282 | abort (); | |
283 | r = ELF32_R_TYPE (elf_reloc->r_info); | |
284 | bfd_reloc->howto = &h8_elf_howto_table[r]; | |
285 | } | |
286 | ||
a00c9dbc JL |
287 | /* Special handling for H8/300 relocs. |
288 | We only come here for pcrel stuff and return normally if not an -r link. | |
289 | When doing -r, we can't do any arithmetic for the pcrel stuff, because | |
290 | we support relaxing on the H8/300 series chips. */ | |
291 | static bfd_reloc_status_type | |
c6baf75e RS |
292 | special (bfd *abfd ATTRIBUTE_UNUSED, |
293 | arelent *reloc_entry ATTRIBUTE_UNUSED, | |
294 | asymbol *symbol ATTRIBUTE_UNUSED, | |
295 | PTR data ATTRIBUTE_UNUSED, | |
296 | asection *input_section ATTRIBUTE_UNUSED, | |
297 | bfd *output_bfd, | |
298 | char **error_message ATTRIBUTE_UNUSED) | |
a00c9dbc JL |
299 | { |
300 | if (output_bfd == (bfd *) NULL) | |
301 | return bfd_reloc_continue; | |
302 | ||
303 | /* Adjust the reloc address to that in the output section. */ | |
304 | reloc_entry->address += input_section->output_offset; | |
305 | return bfd_reloc_ok; | |
306 | } | |
5e47149d JL |
307 | |
308 | /* Perform a relocation as part of a final link. */ | |
309 | static bfd_reloc_status_type | |
c6baf75e RS |
310 | elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd, |
311 | bfd *output_bfd ATTRIBUTE_UNUSED, | |
312 | asection *input_section ATTRIBUTE_UNUSED, | |
313 | bfd_byte *contents, bfd_vma offset, | |
314 | bfd_vma value, bfd_vma addend, | |
315 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
316 | asection *sym_sec ATTRIBUTE_UNUSED, | |
317 | int is_local ATTRIBUTE_UNUSED) | |
5e47149d JL |
318 | { |
319 | bfd_byte *hit_data = contents + offset; | |
320 | ||
321 | switch (r_type) | |
322 | { | |
5e47149d JL |
323 | case R_H8_NONE: |
324 | return bfd_reloc_ok; | |
325 | ||
326 | case R_H8_DIR32: | |
327 | case R_H8_DIR32A16: | |
a00c9dbc | 328 | case R_H8_DIR24A8: |
5e47149d JL |
329 | value += addend; |
330 | bfd_put_32 (input_bfd, value, hit_data); | |
331 | return bfd_reloc_ok; | |
332 | ||
333 | case R_H8_DIR16: | |
334 | case R_H8_DIR16A8: | |
335 | case R_H8_DIR16R8: | |
336 | value += addend; | |
337 | bfd_put_16 (input_bfd, value, hit_data); | |
338 | return bfd_reloc_ok; | |
339 | ||
340 | /* AKA R_RELBYTE */ | |
341 | case R_H8_DIR8: | |
342 | value += addend; | |
343 | ||
5e47149d JL |
344 | bfd_put_8 (input_bfd, value, hit_data); |
345 | return bfd_reloc_ok; | |
346 | ||
5e47149d JL |
347 | case R_H8_DIR24R8: |
348 | value += addend; | |
349 | ||
a00c9dbc | 350 | /* HIT_DATA is the address for the first byte for the relocated |
e804e836 | 351 | value. Subtract 1 so that we can manipulate the data in 32-bit |
a00c9dbc JL |
352 | hunks. */ |
353 | hit_data--; | |
354 | ||
355 | /* Clear out the top byte in value. */ | |
5e47149d | 356 | value &= 0xffffff; |
a00c9dbc JL |
357 | |
358 | /* Retrieve the type byte for value from the section contents. */ | |
5e47149d | 359 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); |
a00c9dbc | 360 | |
e804e836 | 361 | /* Now scribble it out in one 32-bit hunk. */ |
5e47149d JL |
362 | bfd_put_32 (input_bfd, value, hit_data); |
363 | return bfd_reloc_ok; | |
364 | ||
f2352488 JL |
365 | case R_H8_PCREL16: |
366 | value -= (input_section->output_section->vma | |
367 | + input_section->output_offset); | |
368 | value -= offset; | |
369 | value += addend; | |
370 | ||
a00c9dbc JL |
371 | /* The value is relative to the start of the instruction, |
372 | not the relocation offset. Subtract 2 to account for | |
373 | this minor issue. */ | |
374 | value -= 2; | |
375 | ||
f2352488 JL |
376 | bfd_put_16 (input_bfd, value, hit_data); |
377 | return bfd_reloc_ok; | |
378 | ||
379 | case R_H8_PCREL8: | |
380 | value -= (input_section->output_section->vma | |
381 | + input_section->output_offset); | |
382 | value -= offset; | |
383 | value += addend; | |
384 | ||
a00c9dbc JL |
385 | /* The value is relative to the start of the instruction, |
386 | not the relocation offset. Subtract 1 to account for | |
387 | this minor issue. */ | |
388 | value -= 1; | |
389 | ||
f2352488 JL |
390 | bfd_put_8 (input_bfd, value, hit_data); |
391 | return bfd_reloc_ok; | |
392 | ||
5e47149d JL |
393 | default: |
394 | return bfd_reloc_notsupported; | |
395 | } | |
396 | } | |
397 | \f | |
398 | /* Relocate an H8 ELF section. */ | |
b34976b6 | 399 | static bfd_boolean |
c6baf75e RS |
400 | elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info, |
401 | bfd *input_bfd, asection *input_section, | |
402 | bfd_byte *contents, Elf_Internal_Rela *relocs, | |
403 | Elf_Internal_Sym *local_syms, | |
404 | asection **local_sections) | |
5e47149d JL |
405 | { |
406 | Elf_Internal_Shdr *symtab_hdr; | |
407 | struct elf_link_hash_entry **sym_hashes; | |
408 | Elf_Internal_Rela *rel, *relend; | |
409 | ||
1049f94e | 410 | if (info->relocatable) |
b34976b6 | 411 | return TRUE; |
f0fe0e16 | 412 | |
5e47149d JL |
413 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
414 | sym_hashes = elf_sym_hashes (input_bfd); | |
415 | ||
416 | rel = relocs; | |
417 | relend = relocs + input_section->reloc_count; | |
418 | for (; rel < relend; rel++) | |
419 | { | |
dc810e39 | 420 | unsigned int r_type; |
5e47149d JL |
421 | unsigned long r_symndx; |
422 | Elf_Internal_Sym *sym; | |
423 | asection *sec; | |
424 | struct elf_link_hash_entry *h; | |
425 | bfd_vma relocation; | |
426 | bfd_reloc_status_type r; | |
427 | ||
f0fe0e16 | 428 | /* This is a final link. */ |
5e47149d JL |
429 | r_symndx = ELF32_R_SYM (rel->r_info); |
430 | r_type = ELF32_R_TYPE (rel->r_info); | |
5e47149d JL |
431 | h = NULL; |
432 | sym = NULL; | |
433 | sec = NULL; | |
434 | if (r_symndx < symtab_hdr->sh_info) | |
435 | { | |
436 | sym = local_syms + r_symndx; | |
437 | sec = local_sections[r_symndx]; | |
8517fae7 | 438 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
5e47149d JL |
439 | } |
440 | else | |
441 | { | |
442 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
443 | while (h->root.type == bfd_link_hash_indirect | |
444 | || h->root.type == bfd_link_hash_warning) | |
445 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
446 | if (h->root.type == bfd_link_hash_defined | |
447 | || h->root.type == bfd_link_hash_defweak) | |
448 | { | |
449 | sec = h->root.u.def.section; | |
450 | relocation = (h->root.u.def.value | |
451 | + sec->output_section->vma | |
452 | + sec->output_offset); | |
453 | } | |
454 | else if (h->root.type == bfd_link_hash_undefweak) | |
455 | relocation = 0; | |
456 | else | |
457 | { | |
458 | if (! ((*info->callbacks->undefined_symbol) | |
459 | (info, h->root.root.string, input_bfd, | |
b34976b6 AM |
460 | input_section, rel->r_offset, TRUE))) |
461 | return FALSE; | |
5e47149d JL |
462 | relocation = 0; |
463 | } | |
464 | } | |
465 | ||
466 | r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd, | |
467 | input_section, | |
468 | contents, rel->r_offset, | |
469 | relocation, rel->r_addend, | |
470 | info, sec, h == NULL); | |
471 | ||
472 | if (r != bfd_reloc_ok) | |
473 | { | |
474 | const char *name; | |
475 | const char *msg = (const char *) 0; | |
476 | arelent bfd_reloc; | |
477 | reloc_howto_type *howto; | |
478 | ||
479 | elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel); | |
480 | howto = bfd_reloc.howto; | |
dc810e39 | 481 | |
5e47149d JL |
482 | if (h != NULL) |
483 | name = h->root.root.string; | |
484 | else | |
485 | { | |
486 | name = (bfd_elf_string_from_elf_section | |
487 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
488 | if (name == NULL || *name == '\0') | |
489 | name = bfd_section_name (input_bfd, sec); | |
490 | } | |
491 | ||
492 | switch (r) | |
493 | { | |
494 | case bfd_reloc_overflow: | |
495 | if (! ((*info->callbacks->reloc_overflow) | |
496 | (info, name, howto->name, (bfd_vma) 0, | |
497 | input_bfd, input_section, rel->r_offset))) | |
b34976b6 | 498 | return FALSE; |
5e47149d JL |
499 | break; |
500 | ||
501 | case bfd_reloc_undefined: | |
502 | if (! ((*info->callbacks->undefined_symbol) | |
503 | (info, name, input_bfd, input_section, | |
b34976b6 AM |
504 | rel->r_offset, TRUE))) |
505 | return FALSE; | |
5e47149d JL |
506 | break; |
507 | ||
508 | case bfd_reloc_outofrange: | |
509 | msg = _("internal error: out of range error"); | |
510 | goto common_error; | |
511 | ||
512 | case bfd_reloc_notsupported: | |
513 | msg = _("internal error: unsupported relocation error"); | |
514 | goto common_error; | |
515 | ||
516 | case bfd_reloc_dangerous: | |
517 | msg = _("internal error: dangerous error"); | |
518 | goto common_error; | |
519 | ||
520 | default: | |
521 | msg = _("internal error: unknown error"); | |
522 | /* fall through */ | |
523 | ||
524 | common_error: | |
525 | if (!((*info->callbacks->warning) | |
526 | (info, msg, name, input_bfd, input_section, | |
527 | rel->r_offset))) | |
b34976b6 | 528 | return FALSE; |
5e47149d JL |
529 | break; |
530 | } | |
531 | } | |
532 | } | |
533 | ||
b34976b6 | 534 | return TRUE; |
5e47149d JL |
535 | } |
536 | ||
0a83638b JL |
537 | /* Object files encode the specific H8 model they were compiled |
538 | for in the ELF flags field. | |
539 | ||
540 | Examine that field and return the proper BFD machine type for | |
541 | the object file. */ | |
dc810e39 | 542 | static unsigned long |
c6baf75e | 543 | elf32_h8_mach (flagword flags) |
0a83638b JL |
544 | { |
545 | switch (flags & EF_H8_MACH) | |
546 | { | |
547 | case E_H8_MACH_H8300: | |
548 | default: | |
549 | return bfd_mach_h8300; | |
550 | ||
551 | case E_H8_MACH_H8300H: | |
552 | return bfd_mach_h8300h; | |
553 | ||
554 | case E_H8_MACH_H8300S: | |
555 | return bfd_mach_h8300s; | |
8d9cd6b1 NC |
556 | |
557 | case E_H8_MACH_H8300HN: | |
558 | return bfd_mach_h8300hn; | |
559 | ||
560 | case E_H8_MACH_H8300SN: | |
561 | return bfd_mach_h8300sn; | |
5d1db417 MS |
562 | |
563 | case E_H8_MACH_H8300SX: | |
564 | return bfd_mach_h8300sx; | |
f4984206 RS |
565 | |
566 | case E_H8_MACH_H8300SXN: | |
567 | return bfd_mach_h8300sxn; | |
0a83638b JL |
568 | } |
569 | } | |
570 | ||
571 | /* The final processing done just before writing out a H8 ELF object | |
572 | file. We use this opportunity to encode the BFD machine type | |
573 | into the flags field in the object file. */ | |
574 | ||
dc810e39 | 575 | static void |
c6baf75e RS |
576 | elf32_h8_final_write_processing (bfd *abfd, |
577 | bfd_boolean linker ATTRIBUTE_UNUSED) | |
0a83638b JL |
578 | { |
579 | unsigned long val; | |
580 | ||
581 | switch (bfd_get_mach (abfd)) | |
582 | { | |
583 | default: | |
584 | case bfd_mach_h8300: | |
585 | val = E_H8_MACH_H8300; | |
586 | break; | |
587 | ||
588 | case bfd_mach_h8300h: | |
589 | val = E_H8_MACH_H8300H; | |
590 | break; | |
591 | ||
592 | case bfd_mach_h8300s: | |
593 | val = E_H8_MACH_H8300S; | |
594 | break; | |
8d9cd6b1 NC |
595 | |
596 | case bfd_mach_h8300hn: | |
597 | val = E_H8_MACH_H8300HN; | |
598 | break; | |
599 | ||
600 | case bfd_mach_h8300sn: | |
601 | val = E_H8_MACH_H8300SN; | |
602 | break; | |
5d1db417 MS |
603 | |
604 | case bfd_mach_h8300sx: | |
605 | val = E_H8_MACH_H8300SX; | |
606 | break; | |
f4984206 RS |
607 | |
608 | case bfd_mach_h8300sxn: | |
609 | val = E_H8_MACH_H8300SXN; | |
610 | break; | |
0a83638b JL |
611 | } |
612 | ||
613 | elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH); | |
614 | elf_elfheader (abfd)->e_flags |= val; | |
615 | } | |
616 | ||
617 | /* Return nonzero if ABFD represents a valid H8 ELF object file; also | |
618 | record the encoded machine type found in the ELF flags. */ | |
619 | ||
b34976b6 | 620 | static bfd_boolean |
c6baf75e | 621 | elf32_h8_object_p (bfd *abfd) |
0a83638b JL |
622 | { |
623 | bfd_default_set_arch_mach (abfd, bfd_arch_h8300, | |
624 | elf32_h8_mach (elf_elfheader (abfd)->e_flags)); | |
b34976b6 | 625 | return TRUE; |
0a83638b JL |
626 | } |
627 | ||
628 | /* Merge backend specific data from an object file to the output | |
629 | object file when linking. The only data we need to copy at this | |
630 | time is the architecture/machine information. */ | |
631 | ||
b34976b6 | 632 | static bfd_boolean |
c6baf75e | 633 | elf32_h8_merge_private_bfd_data (bfd *ibfd, bfd *obfd) |
0a83638b JL |
634 | { |
635 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
636 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 637 | return TRUE; |
0a83638b JL |
638 | |
639 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
640 | && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) | |
641 | { | |
642 | if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
96ef1419 KH |
643 | bfd_get_mach (ibfd))) |
644 | return FALSE; | |
0a83638b JL |
645 | } |
646 | ||
b34976b6 | 647 | return TRUE; |
0a83638b JL |
648 | } |
649 | ||
5907e628 JL |
650 | /* This function handles relaxing for the H8.. |
651 | ||
4cc11e76 | 652 | There are a few relaxing opportunities available on the H8: |
5907e628 JL |
653 | |
654 | jmp/jsr:24 -> bra/bsr:8 2 bytes | |
655 | The jmp may be completely eliminated if the previous insn is a | |
656 | conditional branch to the insn after the jump. In that case | |
657 | we invert the branch and delete the jump and save 4 bytes. | |
658 | ||
659 | bCC:16 -> bCC:8 2 bytes | |
660 | bsr:16 -> bsr:8 2 bytes | |
661 | ||
630a7b0a KH |
662 | bset:16 -> bset:8 2 bytes |
663 | bset:24/32 -> bset:8 4 bytes | |
664 | (also applicable to other bit manipulation instructions) | |
665 | ||
5907e628 JL |
666 | mov.b:16 -> mov.b:8 2 bytes |
667 | mov.b:24/32 -> mov.b:8 4 bytes | |
668 | ||
76f99c63 | 669 | mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */ |
5907e628 | 670 | |
b34976b6 | 671 | static bfd_boolean |
c6baf75e RS |
672 | elf32_h8_relax_section (bfd *abfd, asection *sec, |
673 | struct bfd_link_info *link_info, bfd_boolean *again) | |
5907e628 JL |
674 | { |
675 | Elf_Internal_Shdr *symtab_hdr; | |
676 | Elf_Internal_Rela *internal_relocs; | |
5907e628 JL |
677 | Elf_Internal_Rela *irel, *irelend; |
678 | bfd_byte *contents = NULL; | |
6cdc0ccc | 679 | Elf_Internal_Sym *isymbuf = NULL; |
5907e628 JL |
680 | static asection *last_input_section = NULL; |
681 | static Elf_Internal_Rela *last_reloc = NULL; | |
682 | ||
683 | /* Assume nothing changes. */ | |
b34976b6 | 684 | *again = FALSE; |
5907e628 | 685 | |
1049f94e | 686 | /* We don't have to do anything for a relocatable link, if |
5907e628 JL |
687 | this section does not have relocs, or if this is not a |
688 | code section. */ | |
1049f94e | 689 | if (link_info->relocatable |
5907e628 JL |
690 | || (sec->flags & SEC_RELOC) == 0 |
691 | || sec->reloc_count == 0 | |
692 | || (sec->flags & SEC_CODE) == 0) | |
b34976b6 | 693 | return TRUE; |
5907e628 JL |
694 | |
695 | /* If this is the first time we have been called for this section, | |
696 | initialize the cooked size. */ | |
697 | if (sec->_cooked_size == 0) | |
698 | sec->_cooked_size = sec->_raw_size; | |
699 | ||
700 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
701 | ||
702 | /* Get a copy of the native relocations. */ | |
45d6a902 | 703 | internal_relocs = (_bfd_elf_link_read_relocs |
5907e628 JL |
704 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, |
705 | link_info->keep_memory)); | |
706 | if (internal_relocs == NULL) | |
707 | goto error_return; | |
5907e628 JL |
708 | |
709 | if (sec != last_input_section) | |
710 | last_reloc = NULL; | |
711 | ||
712 | last_input_section = sec; | |
713 | ||
714 | /* Walk through the relocs looking for relaxing opportunities. */ | |
715 | irelend = internal_relocs + sec->reloc_count; | |
716 | for (irel = internal_relocs; irel < irelend; irel++) | |
717 | { | |
718 | bfd_vma symval; | |
719 | ||
720 | /* Keep track of the previous reloc so that we can delete | |
721 | some long jumps created by the compiler. */ | |
722 | if (irel != internal_relocs) | |
723 | last_reloc = irel - 1; | |
724 | ||
76f99c63 JL |
725 | if (ELF32_R_TYPE (irel->r_info) != R_H8_DIR24R8 |
726 | && ELF32_R_TYPE (irel->r_info) != R_H8_PCREL16 | |
727 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR16A8 | |
728 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR24A8 | |
729 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR32A16) | |
730 | continue; | |
731 | ||
5907e628 JL |
732 | /* Get the section contents if we haven't done so already. */ |
733 | if (contents == NULL) | |
734 | { | |
735 | /* Get cached copy if it exists. */ | |
736 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
737 | contents = elf_section_data (sec)->this_hdr.contents; | |
738 | else | |
739 | { | |
740 | /* Go get them off disk. */ | |
741 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
742 | if (contents == NULL) | |
743 | goto error_return; | |
5907e628 JL |
744 | |
745 | if (! bfd_get_section_contents (abfd, sec, contents, | |
746 | (file_ptr) 0, sec->_raw_size)) | |
747 | goto error_return; | |
748 | } | |
749 | } | |
750 | ||
9ad5cbcf | 751 | /* Read this BFD's local symbols if we haven't done so already. */ |
6cdc0ccc | 752 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) |
5907e628 | 753 | { |
6cdc0ccc AM |
754 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
755 | if (isymbuf == NULL) | |
756 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
757 | symtab_hdr->sh_info, 0, | |
758 | NULL, NULL, NULL); | |
759 | if (isymbuf == NULL) | |
760 | goto error_return; | |
5907e628 JL |
761 | } |
762 | ||
763 | /* Get the value of the symbol referred to by the reloc. */ | |
764 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
765 | { | |
5907e628 | 766 | /* A local symbol. */ |
6cdc0ccc AM |
767 | Elf_Internal_Sym *isym; |
768 | asection *sym_sec; | |
5907e628 | 769 | |
32ac2c9a | 770 | isym = isymbuf + ELF32_R_SYM (irel->r_info); |
6cdc0ccc | 771 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
03d14457 NC |
772 | symval = isym->st_value; |
773 | /* If the reloc is absolute, it will not have | |
774 | a symbol or section associated with it. */ | |
775 | if (sym_sec) | |
776 | symval += sym_sec->output_section->vma | |
777 | + sym_sec->output_offset; | |
5907e628 JL |
778 | } |
779 | else | |
780 | { | |
781 | unsigned long indx; | |
782 | struct elf_link_hash_entry *h; | |
783 | ||
784 | /* An external symbol. */ | |
785 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
786 | h = elf_sym_hashes (abfd)[indx]; | |
787 | BFD_ASSERT (h != NULL); | |
788 | if (h->root.type != bfd_link_hash_defined | |
789 | && h->root.type != bfd_link_hash_defweak) | |
790 | { | |
791 | /* This appears to be a reference to an undefined | |
792 | symbol. Just ignore it--it will be caught by the | |
793 | regular reloc processing. */ | |
794 | continue; | |
795 | } | |
796 | ||
797 | symval = (h->root.u.def.value | |
798 | + h->root.u.def.section->output_section->vma | |
799 | + h->root.u.def.section->output_offset); | |
800 | } | |
801 | ||
802 | /* For simplicity of coding, we are going to modify the section | |
803 | contents, the section relocs, and the BFD symbol table. We | |
804 | must tell the rest of the code not to free up this | |
805 | information. It would be possible to instead create a table | |
806 | of changes which have to be made, as is done in coff-mips.c; | |
807 | that would be more work, but would require less memory when | |
808 | the linker is run. */ | |
809 | switch (ELF32_R_TYPE (irel->r_info)) | |
810 | { | |
e804e836 | 811 | /* Try to turn a 24-bit absolute branch/call into an 8-bit |
5907e628 JL |
812 | pc-relative branch/call. */ |
813 | case R_H8_DIR24R8: | |
814 | { | |
815 | bfd_vma value = symval + irel->r_addend; | |
816 | bfd_vma dot, gap; | |
817 | ||
818 | /* Get the address of this instruction. */ | |
819 | dot = (sec->output_section->vma | |
820 | + sec->output_offset + irel->r_offset - 1); | |
821 | ||
822 | /* Compute the distance from this insn to the branch target. */ | |
823 | gap = value - dot; | |
824 | ||
825 | /* If the distance is within -126..+130 inclusive, then we can | |
826 | relax this jump. +130 is valid since the target will move | |
827 | two bytes closer if we do relax this branch. */ | |
dc810e39 | 828 | if ((int) gap >= -126 && (int) gap <= 130) |
5907e628 JL |
829 | { |
830 | unsigned char code; | |
831 | ||
832 | /* Note that we've changed the relocs, section contents, | |
833 | etc. */ | |
834 | elf_section_data (sec)->relocs = internal_relocs; | |
5907e628 | 835 | elf_section_data (sec)->this_hdr.contents = contents; |
6cdc0ccc | 836 | symtab_hdr->contents = (unsigned char *) isymbuf; |
5907e628 | 837 | |
e514ac71 NC |
838 | /* Get the instruction code being relaxed. */ |
839 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
840 | ||
5907e628 JL |
841 | /* If the previous instruction conditionally jumped around |
842 | this instruction, we may be able to reverse the condition | |
843 | and redirect the previous instruction to the target of | |
844 | this instruction. | |
845 | ||
846 | Such sequences are used by the compiler to deal with | |
e514ac71 NC |
847 | long conditional branches. |
848 | ||
849 | Only perform this optimisation for jumps (code 0x5a) not | |
850 | subroutine calls, as otherwise it could transform: | |
b34976b6 | 851 | |
e514ac71 NC |
852 | mov.w r0,r0 |
853 | beq .L1 | |
854 | jsr @_bar | |
855 | .L1: rts | |
856 | _bar: rts | |
857 | into: | |
858 | mov.w r0,r0 | |
859 | bne _bar | |
860 | rts | |
861 | _bar: rts | |
862 | ||
863 | which changes the call (jsr) into a branch (bne). */ | |
864 | if (code == 0x5a | |
865 | && (int) gap <= 130 | |
dc810e39 | 866 | && (int) gap >= -128 |
5907e628 JL |
867 | && last_reloc |
868 | && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8 | |
869 | && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info) | |
870 | { | |
871 | bfd_vma last_value; | |
872 | asection *last_sym_sec; | |
6cdc0ccc | 873 | Elf_Internal_Sym *last_sym; |
5907e628 JL |
874 | |
875 | /* We will need to examine the symbol used by the | |
876 | previous relocation. */ | |
dc810e39 | 877 | |
6cdc0ccc | 878 | last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info); |
5907e628 | 879 | last_sym_sec |
6cdc0ccc AM |
880 | = bfd_section_from_elf_index (abfd, last_sym->st_shndx); |
881 | last_value = (last_sym->st_value | |
5907e628 JL |
882 | + last_sym_sec->output_section->vma |
883 | + last_sym_sec->output_offset); | |
884 | ||
885 | /* Verify that the previous relocation was for a | |
886 | branch around this instruction and that no symbol | |
887 | exists at the current location. */ | |
888 | if (last_value == dot + 4 | |
889 | && last_reloc->r_offset + 2 == irel->r_offset | |
9ad5cbcf | 890 | && ! elf32_h8_symbol_address_p (abfd, sec, dot)) |
5907e628 JL |
891 | { |
892 | /* We can eliminate this jump. Twiddle the | |
893 | previous relocation as necessary. */ | |
894 | irel->r_info | |
895 | = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
896 | ELF32_R_TYPE (R_H8_NONE)); | |
897 | ||
bc7eab72 | 898 | last_reloc->r_info |
5907e628 | 899 | = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
bc7eab72 KH |
900 | ELF32_R_TYPE (R_H8_PCREL8)); |
901 | last_reloc->r_addend = irel->r_addend; | |
5907e628 JL |
902 | |
903 | code = bfd_get_8 (abfd, | |
904 | contents + last_reloc->r_offset - 1); | |
905 | code ^= 1; | |
906 | bfd_put_8 (abfd, | |
907 | code, | |
908 | contents + last_reloc->r_offset - 1); | |
909 | ||
910 | /* Delete four bytes of data. */ | |
911 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
912 | irel->r_offset - 1, | |
913 | 4)) | |
914 | goto error_return; | |
915 | ||
b34976b6 | 916 | *again = TRUE; |
5907e628 JL |
917 | break; |
918 | } | |
919 | } | |
920 | ||
5907e628 JL |
921 | if (code == 0x5e) |
922 | bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1); | |
923 | else if (code == 0x5a) | |
924 | bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1); | |
925 | else | |
926 | abort (); | |
927 | ||
928 | /* Fix the relocation's type. */ | |
929 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
930 | R_H8_PCREL8); | |
931 | ||
932 | /* Delete two bytes of data. */ | |
933 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
934 | irel->r_offset + 1, 2)) | |
935 | goto error_return; | |
936 | ||
937 | /* That will change things, so, we should relax again. | |
938 | Note that this is not required, and it may be slow. */ | |
b34976b6 | 939 | *again = TRUE; |
5907e628 JL |
940 | } |
941 | break; | |
942 | } | |
943 | ||
e804e836 | 944 | /* Try to turn a 16-bit pc-relative branch into a 8-bit pc-relative |
5907e628 JL |
945 | branch. */ |
946 | case R_H8_PCREL16: | |
947 | { | |
948 | bfd_vma value = symval + irel->r_addend; | |
949 | bfd_vma dot; | |
950 | bfd_vma gap; | |
951 | ||
952 | /* Get the address of this instruction. */ | |
953 | dot = (sec->output_section->vma | |
954 | + sec->output_offset | |
955 | + irel->r_offset - 2); | |
dc810e39 | 956 | |
5907e628 JL |
957 | gap = value - dot; |
958 | ||
959 | /* If the distance is within -126..+130 inclusive, then we can | |
960 | relax this jump. +130 is valid since the target will move | |
961 | two bytes closer if we do relax this branch. */ | |
bc7eab72 | 962 | if ((int) gap >= -126 && (int) gap <= 130) |
5907e628 | 963 | { |
bc7eab72 | 964 | unsigned char code; |
5907e628 | 965 | |
bc7eab72 | 966 | /* Note that we've changed the relocs, section contents, |
5907e628 | 967 | etc. */ |
bc7eab72 KH |
968 | elf_section_data (sec)->relocs = internal_relocs; |
969 | elf_section_data (sec)->this_hdr.contents = contents; | |
6cdc0ccc | 970 | symtab_hdr->contents = (unsigned char *) isymbuf; |
5907e628 | 971 | |
bc7eab72 KH |
972 | /* Get the opcode. */ |
973 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
5907e628 JL |
974 | |
975 | if (code == 0x58) | |
976 | { | |
977 | /* bCC:16 -> bCC:8 */ | |
978 | /* Get the condition code from the original insn. */ | |
979 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
980 | code &= 0xf0; | |
981 | code >>= 4; | |
982 | code |= 0x40; | |
983 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
984 | } | |
985 | else if (code == 0x5c) | |
986 | bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2); | |
987 | else | |
988 | abort (); | |
989 | ||
990 | /* Fix the relocation's type. */ | |
bc7eab72 | 991 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
5907e628 | 992 | R_H8_PCREL8); |
bc7eab72 | 993 | irel->r_offset--; |
5907e628 | 994 | |
bc7eab72 KH |
995 | /* Delete two bytes of data. */ |
996 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
5907e628 JL |
997 | irel->r_offset + 1, 2)) |
998 | goto error_return; | |
999 | ||
bc7eab72 | 1000 | /* That will change things, so, we should relax again. |
5907e628 | 1001 | Note that this is not required, and it may be slow. */ |
b34976b6 | 1002 | *again = TRUE; |
5907e628 JL |
1003 | } |
1004 | break; | |
1005 | } | |
1006 | ||
630a7b0a KH |
1007 | /* This is a 16-bit absolute address in one of the following |
1008 | instructions: | |
1009 | ||
1010 | "band", "bclr", "biand", "bild", "bior", "bist", "bixor", | |
1011 | "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and | |
1012 | "mov.b" | |
1013 | ||
1014 | We may relax this into an 8-bit absolute address if it's in | |
1015 | the right range. */ | |
5907e628 JL |
1016 | case R_H8_DIR16A8: |
1017 | { | |
7a9823f1 | 1018 | bfd_vma value; |
5907e628 | 1019 | |
7a9823f1 RS |
1020 | value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); |
1021 | if (value >= 0xffffff00u) | |
5907e628 | 1022 | { |
bc7eab72 | 1023 | unsigned char code; |
ca9a79a1 | 1024 | unsigned char temp_code; |
5907e628 | 1025 | |
bc7eab72 | 1026 | /* Note that we've changed the relocs, section contents, |
5907e628 | 1027 | etc. */ |
bc7eab72 KH |
1028 | elf_section_data (sec)->relocs = internal_relocs; |
1029 | elf_section_data (sec)->this_hdr.contents = contents; | |
6cdc0ccc | 1030 | symtab_hdr->contents = (unsigned char *) isymbuf; |
5907e628 | 1031 | |
bc7eab72 KH |
1032 | /* Get the opcode. */ |
1033 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
5907e628 | 1034 | |
630a7b0a KH |
1035 | /* All instructions with R_H8_DIR16A8 start with |
1036 | 0x6a. */ | |
bc7eab72 | 1037 | if (code != 0x6a) |
5907e628 JL |
1038 | abort (); |
1039 | ||
ca9a79a1 | 1040 | temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
630a7b0a KH |
1041 | /* If this is a mov.b instruction, clear the lower |
1042 | nibble, which contains the source/destination | |
1043 | register number. */ | |
ca9a79a1 NC |
1044 | if ((temp_code & 0x10) != 0x10) |
1045 | temp_code &= 0xf0; | |
5907e628 | 1046 | |
ca9a79a1 NC |
1047 | switch (temp_code) |
1048 | { | |
1049 | case 0x00: | |
630a7b0a | 1050 | /* This is mov.b @aa:16,Rd. */ |
ca9a79a1 NC |
1051 | bfd_put_8 (abfd, (code & 0xf) | 0x20, |
1052 | contents + irel->r_offset - 2); | |
1053 | break; | |
1054 | case 0x80: | |
630a7b0a | 1055 | /* This is mov.b Rs,@aa:16. */ |
ca9a79a1 NC |
1056 | bfd_put_8 (abfd, (code & 0xf) | 0x30, |
1057 | contents + irel->r_offset - 2); | |
1058 | break; | |
1059 | case 0x18: | |
630a7b0a KH |
1060 | /* This is a bit-maniputation instruction that |
1061 | stores one bit into memory, one of "bclr", | |
1062 | "bist", "bnot", "bset", and "bst". */ | |
ca9a79a1 NC |
1063 | bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2); |
1064 | break; | |
1065 | case 0x10: | |
630a7b0a KH |
1066 | /* This is a bit-maniputation instruction that |
1067 | loads one bit from memory, one of "band", | |
1068 | "biand", "bild", "bior", "bixor", "bld", "bor", | |
1069 | "btst", and "bxor". */ | |
ca9a79a1 NC |
1070 | bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2); |
1071 | break; | |
1072 | default: | |
1073 | abort (); | |
1074 | } | |
5907e628 | 1075 | |
bc7eab72 KH |
1076 | /* Fix the relocation's type. */ |
1077 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
5907e628 JL |
1078 | R_H8_DIR8); |
1079 | ||
8c17da6e NC |
1080 | /* Move the relocation. */ |
1081 | irel->r_offset--; | |
1082 | ||
bc7eab72 KH |
1083 | /* Delete two bytes of data. */ |
1084 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
5907e628 JL |
1085 | irel->r_offset + 1, 2)) |
1086 | goto error_return; | |
1087 | ||
bc7eab72 | 1088 | /* That will change things, so, we should relax again. |
5907e628 | 1089 | Note that this is not required, and it may be slow. */ |
b34976b6 | 1090 | *again = TRUE; |
5907e628 JL |
1091 | } |
1092 | break; | |
1093 | } | |
1094 | ||
630a7b0a KH |
1095 | /* This is a 24-bit absolute address in one of the following |
1096 | instructions: | |
1097 | ||
1098 | "band", "bclr", "biand", "bild", "bior", "bist", "bixor", | |
1099 | "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and | |
1100 | "mov.b" | |
1101 | ||
1102 | We may relax this into an 8-bit absolute address if it's in | |
1103 | the right range. */ | |
5907e628 JL |
1104 | case R_H8_DIR24A8: |
1105 | { | |
7a9823f1 | 1106 | bfd_vma value; |
5907e628 | 1107 | |
7a9823f1 RS |
1108 | value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); |
1109 | if (value >= 0xffffff00u) | |
5907e628 | 1110 | { |
bc7eab72 | 1111 | unsigned char code; |
ca9a79a1 | 1112 | unsigned char temp_code; |
5907e628 | 1113 | |
bc7eab72 | 1114 | /* Note that we've changed the relocs, section contents, |
5907e628 | 1115 | etc. */ |
bc7eab72 KH |
1116 | elf_section_data (sec)->relocs = internal_relocs; |
1117 | elf_section_data (sec)->this_hdr.contents = contents; | |
6cdc0ccc | 1118 | symtab_hdr->contents = (unsigned char *) isymbuf; |
5907e628 | 1119 | |
bc7eab72 KH |
1120 | /* Get the opcode. */ |
1121 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
5907e628 | 1122 | |
630a7b0a KH |
1123 | /* All instructions with R_H8_DIR24A8 start with |
1124 | 0x6a. */ | |
bc7eab72 | 1125 | if (code != 0x6a) |
5907e628 JL |
1126 | abort (); |
1127 | ||
ca9a79a1 NC |
1128 | temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
1129 | ||
630a7b0a KH |
1130 | /* If this is a mov.b instruction, clear the lower |
1131 | nibble, which contains the source/destination | |
1132 | register number. */ | |
ca9a79a1 NC |
1133 | if ((temp_code & 0x30) != 0x30) |
1134 | temp_code &= 0xf0; | |
5907e628 | 1135 | |
ca9a79a1 | 1136 | switch (temp_code) |
03d14457 | 1137 | { |
7a9823f1 | 1138 | case 0x20: |
630a7b0a | 1139 | /* This is mov.b @aa:24/32,Rd. */ |
03d14457 NC |
1140 | bfd_put_8 (abfd, (code & 0xf) | 0x20, |
1141 | contents + irel->r_offset - 2); | |
1142 | break; | |
7a9823f1 | 1143 | case 0xa0: |
630a7b0a | 1144 | /* This is mov.b Rs,@aa:24/32. */ |
03d14457 NC |
1145 | bfd_put_8 (abfd, (code & 0xf) | 0x30, |
1146 | contents + irel->r_offset - 2); | |
1147 | break; | |
ca9a79a1 | 1148 | case 0x38: |
630a7b0a KH |
1149 | /* This is a bit-maniputation instruction that |
1150 | stores one bit into memory, one of "bclr", | |
1151 | "bist", "bnot", "bset", and "bst". */ | |
ca9a79a1 NC |
1152 | bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2); |
1153 | break; | |
1154 | case 0x30: | |
630a7b0a KH |
1155 | /* This is a bit-maniputation instruction that |
1156 | loads one bit from memory, one of "band", | |
1157 | "biand", "bild", "bior", "bixor", "bld", "bor", | |
1158 | "btst", and "bxor". */ | |
ca9a79a1 NC |
1159 | bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2); |
1160 | break; | |
03d14457 | 1161 | default: |
ca9a79a1 | 1162 | abort(); |
03d14457 NC |
1163 | } |
1164 | ||
bc7eab72 KH |
1165 | /* Fix the relocation's type. */ |
1166 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
5907e628 | 1167 | R_H8_DIR8); |
7a9823f1 | 1168 | irel->r_offset--; |
5907e628 | 1169 | |
bc7eab72 | 1170 | /* Delete two bytes of data. */ |
7a9823f1 RS |
1171 | if (!elf32_h8_relax_delete_bytes (abfd, sec, |
1172 | irel->r_offset + 1, 4)) | |
5907e628 JL |
1173 | goto error_return; |
1174 | ||
bc7eab72 | 1175 | /* That will change things, so, we should relax again. |
5907e628 | 1176 | Note that this is not required, and it may be slow. */ |
b34976b6 | 1177 | *again = TRUE; |
7a9823f1 | 1178 | break; |
5907e628 JL |
1179 | } |
1180 | } | |
1181 | ||
ca9a79a1 | 1182 | /* Fall through. */ |
5907e628 | 1183 | |
e804e836 KH |
1184 | /* This is a 24-/32-bit absolute address in a "mov" insn, |
1185 | which may become a 16-bit absolute address if it is in the | |
1186 | right range. */ | |
5907e628 JL |
1187 | case R_H8_DIR32A16: |
1188 | { | |
7a9823f1 | 1189 | bfd_vma value; |
5907e628 | 1190 | |
7a9823f1 RS |
1191 | value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); |
1192 | if (value <= 0x7fff || value >= 0xffff8000u) | |
5907e628 | 1193 | { |
bc7eab72 | 1194 | unsigned char code; |
5907e628 | 1195 | |
bc7eab72 | 1196 | /* Note that we've changed the relocs, section contents, |
5907e628 | 1197 | etc. */ |
bc7eab72 KH |
1198 | elf_section_data (sec)->relocs = internal_relocs; |
1199 | elf_section_data (sec)->this_hdr.contents = contents; | |
6cdc0ccc | 1200 | symtab_hdr->contents = (unsigned char *) isymbuf; |
5907e628 | 1201 | |
bc7eab72 KH |
1202 | /* Get the opcode. */ |
1203 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
5907e628 | 1204 | |
bc7eab72 KH |
1205 | /* We just need to turn off bit 0x20. */ |
1206 | code &= ~0x20; | |
5907e628 | 1207 | |
bc7eab72 | 1208 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
5907e628 | 1209 | |
bc7eab72 KH |
1210 | /* Fix the relocation's type. */ |
1211 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
8c17da6e | 1212 | R_H8_DIR16); |
5907e628 | 1213 | |
bc7eab72 KH |
1214 | /* Delete two bytes of data. */ |
1215 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
5907e628 JL |
1216 | irel->r_offset + 1, 2)) |
1217 | goto error_return; | |
1218 | ||
bc7eab72 | 1219 | /* That will change things, so, we should relax again. |
5907e628 | 1220 | Note that this is not required, and it may be slow. */ |
b34976b6 | 1221 | *again = TRUE; |
5907e628 JL |
1222 | } |
1223 | break; | |
1224 | } | |
1225 | ||
1226 | default: | |
1227 | break; | |
1228 | } | |
1229 | } | |
1230 | ||
6cdc0ccc AM |
1231 | if (isymbuf != NULL |
1232 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
5907e628 | 1233 | { |
6cdc0ccc AM |
1234 | if (! link_info->keep_memory) |
1235 | free (isymbuf); | |
1236 | else | |
1237 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
5907e628 JL |
1238 | } |
1239 | ||
6cdc0ccc AM |
1240 | if (contents != NULL |
1241 | && elf_section_data (sec)->this_hdr.contents != contents) | |
5907e628 JL |
1242 | { |
1243 | if (! link_info->keep_memory) | |
6cdc0ccc | 1244 | free (contents); |
5907e628 JL |
1245 | else |
1246 | { | |
1247 | /* Cache the section contents for elf_link_input_bfd. */ | |
1248 | elf_section_data (sec)->this_hdr.contents = contents; | |
1249 | } | |
9ad5cbcf AM |
1250 | } |
1251 | ||
6cdc0ccc AM |
1252 | if (internal_relocs != NULL |
1253 | && elf_section_data (sec)->relocs != internal_relocs) | |
1254 | free (internal_relocs); | |
5907e628 | 1255 | |
b34976b6 | 1256 | return TRUE; |
5907e628 JL |
1257 | |
1258 | error_return: | |
6cdc0ccc AM |
1259 | if (isymbuf != NULL |
1260 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1261 | free (isymbuf); | |
1262 | if (contents != NULL | |
1263 | && elf_section_data (sec)->this_hdr.contents != contents) | |
1264 | free (contents); | |
1265 | if (internal_relocs != NULL | |
1266 | && elf_section_data (sec)->relocs != internal_relocs) | |
1267 | free (internal_relocs); | |
b34976b6 | 1268 | return FALSE; |
5907e628 JL |
1269 | } |
1270 | ||
1271 | /* Delete some bytes from a section while relaxing. */ | |
1272 | ||
b34976b6 | 1273 | static bfd_boolean |
c6baf75e | 1274 | elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count) |
5907e628 JL |
1275 | { |
1276 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 1277 | unsigned int sec_shndx; |
5907e628 JL |
1278 | bfd_byte *contents; |
1279 | Elf_Internal_Rela *irel, *irelend; | |
1280 | Elf_Internal_Rela *irelalign; | |
6cdc0ccc AM |
1281 | Elf_Internal_Sym *isym; |
1282 | Elf_Internal_Sym *isymend; | |
5907e628 | 1283 | bfd_vma toaddr; |
9ad5cbcf AM |
1284 | struct elf_link_hash_entry **sym_hashes; |
1285 | struct elf_link_hash_entry **end_hashes; | |
1286 | unsigned int symcount; | |
5907e628 | 1287 | |
9ad5cbcf | 1288 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
5907e628 JL |
1289 | |
1290 | contents = elf_section_data (sec)->this_hdr.contents; | |
1291 | ||
1292 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1293 | power larger than the number of bytes we are deleting. */ | |
1294 | ||
1295 | irelalign = NULL; | |
1296 | toaddr = sec->_cooked_size; | |
1297 | ||
1298 | irel = elf_section_data (sec)->relocs; | |
1299 | irelend = irel + sec->reloc_count; | |
1300 | ||
1301 | /* Actually delete the bytes. */ | |
dc810e39 AM |
1302 | memmove (contents + addr, contents + addr + count, |
1303 | (size_t) (toaddr - addr - count)); | |
5907e628 JL |
1304 | sec->_cooked_size -= count; |
1305 | ||
1306 | /* Adjust all the relocs. */ | |
1307 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
1308 | { | |
1309 | /* Get the new reloc address. */ | |
1310 | if ((irel->r_offset > addr | |
1311 | && irel->r_offset < toaddr)) | |
1312 | irel->r_offset -= count; | |
1313 | } | |
1314 | ||
1315 | /* Adjust the local symbols defined in this section. */ | |
6cdc0ccc AM |
1316 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1317 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1318 | isymend = isym + symtab_hdr->sh_info; | |
1319 | for (; isym < isymend; isym++) | |
5907e628 | 1320 | { |
6cdc0ccc AM |
1321 | if (isym->st_shndx == sec_shndx |
1322 | && isym->st_value > addr | |
1323 | && isym->st_value < toaddr) | |
1324 | isym->st_value -= count; | |
5907e628 JL |
1325 | } |
1326 | ||
1327 | /* Now adjust the global symbols defined in this section. */ | |
9ad5cbcf AM |
1328 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
1329 | - symtab_hdr->sh_info); | |
1330 | sym_hashes = elf_sym_hashes (abfd); | |
1331 | end_hashes = sym_hashes + symcount; | |
1332 | for (; sym_hashes < end_hashes; sym_hashes++) | |
5907e628 | 1333 | { |
9ad5cbcf AM |
1334 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
1335 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1336 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1337 | && sym_hash->root.u.def.section == sec | |
1338 | && sym_hash->root.u.def.value > addr | |
1339 | && sym_hash->root.u.def.value < toaddr) | |
5907e628 | 1340 | { |
9ad5cbcf | 1341 | sym_hash->root.u.def.value -= count; |
5907e628 JL |
1342 | } |
1343 | } | |
1344 | ||
b34976b6 | 1345 | return TRUE; |
5907e628 JL |
1346 | } |
1347 | ||
b34976b6 AM |
1348 | /* Return TRUE if a symbol exists at the given address, else return |
1349 | FALSE. */ | |
1350 | static bfd_boolean | |
c6baf75e | 1351 | elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr) |
5907e628 JL |
1352 | { |
1353 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 1354 | unsigned int sec_shndx; |
6cdc0ccc AM |
1355 | Elf_Internal_Sym *isym; |
1356 | Elf_Internal_Sym *isymend; | |
9ad5cbcf AM |
1357 | struct elf_link_hash_entry **sym_hashes; |
1358 | struct elf_link_hash_entry **end_hashes; | |
1359 | unsigned int symcount; | |
5907e628 | 1360 | |
9ad5cbcf | 1361 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
5907e628 JL |
1362 | |
1363 | /* Examine all the symbols. */ | |
9ad5cbcf | 1364 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
6cdc0ccc AM |
1365 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; |
1366 | isymend = isym + symtab_hdr->sh_info; | |
1367 | for (; isym < isymend; isym++) | |
5907e628 | 1368 | { |
6cdc0ccc AM |
1369 | if (isym->st_shndx == sec_shndx |
1370 | && isym->st_value == addr) | |
b34976b6 | 1371 | return TRUE; |
5907e628 JL |
1372 | } |
1373 | ||
9ad5cbcf AM |
1374 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
1375 | - symtab_hdr->sh_info); | |
1376 | sym_hashes = elf_sym_hashes (abfd); | |
1377 | end_hashes = sym_hashes + symcount; | |
1378 | for (; sym_hashes < end_hashes; sym_hashes++) | |
5907e628 | 1379 | { |
9ad5cbcf AM |
1380 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
1381 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1382 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1383 | && sym_hash->root.u.def.section == sec | |
1384 | && sym_hash->root.u.def.value == addr) | |
b34976b6 | 1385 | return TRUE; |
5907e628 | 1386 | } |
9ad5cbcf | 1387 | |
b34976b6 | 1388 | return FALSE; |
5907e628 JL |
1389 | } |
1390 | ||
1391 | /* This is a version of bfd_generic_get_relocated_section_contents | |
1392 | which uses elf32_h8_relocate_section. */ | |
1393 | ||
1394 | static bfd_byte * | |
c6baf75e RS |
1395 | elf32_h8_get_relocated_section_contents (bfd *output_bfd, |
1396 | struct bfd_link_info *link_info, | |
1397 | struct bfd_link_order *link_order, | |
1398 | bfd_byte *data, | |
1399 | bfd_boolean relocatable, | |
1400 | asymbol **symbols) | |
5907e628 JL |
1401 | { |
1402 | Elf_Internal_Shdr *symtab_hdr; | |
1403 | asection *input_section = link_order->u.indirect.section; | |
1404 | bfd *input_bfd = input_section->owner; | |
1405 | asection **sections = NULL; | |
1406 | Elf_Internal_Rela *internal_relocs = NULL; | |
6cdc0ccc | 1407 | Elf_Internal_Sym *isymbuf = NULL; |
5907e628 JL |
1408 | |
1409 | /* We only need to handle the case of relaxing, or of having a | |
1410 | particular set of section contents, specially. */ | |
1049f94e | 1411 | if (relocatable |
5907e628 JL |
1412 | || elf_section_data (input_section)->this_hdr.contents == NULL) |
1413 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
1414 | link_order, data, | |
1049f94e | 1415 | relocatable, |
5907e628 JL |
1416 | symbols); |
1417 | ||
1418 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1419 | ||
1420 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
dc810e39 | 1421 | (size_t) input_section->_raw_size); |
5907e628 JL |
1422 | |
1423 | if ((input_section->flags & SEC_RELOC) != 0 | |
1424 | && input_section->reloc_count > 0) | |
1425 | { | |
5907e628 | 1426 | asection **secpp; |
6cdc0ccc | 1427 | Elf_Internal_Sym *isym, *isymend; |
9ad5cbcf | 1428 | bfd_size_type amt; |
5907e628 | 1429 | |
45d6a902 | 1430 | internal_relocs = (_bfd_elf_link_read_relocs |
5907e628 | 1431 | (input_bfd, input_section, (PTR) NULL, |
b34976b6 | 1432 | (Elf_Internal_Rela *) NULL, FALSE)); |
5907e628 JL |
1433 | if (internal_relocs == NULL) |
1434 | goto error_return; | |
1435 | ||
6cdc0ccc AM |
1436 | if (symtab_hdr->sh_info != 0) |
1437 | { | |
1438 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1439 | if (isymbuf == NULL) | |
1440 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, | |
1441 | symtab_hdr->sh_info, 0, | |
1442 | NULL, NULL, NULL); | |
1443 | if (isymbuf == NULL) | |
1444 | goto error_return; | |
1445 | } | |
5907e628 | 1446 | |
9ad5cbcf AM |
1447 | amt = symtab_hdr->sh_info; |
1448 | amt *= sizeof (asection *); | |
1449 | sections = (asection **) bfd_malloc (amt); | |
1450 | if (sections == NULL && amt != 0) | |
5907e628 JL |
1451 | goto error_return; |
1452 | ||
6cdc0ccc AM |
1453 | isymend = isymbuf + symtab_hdr->sh_info; |
1454 | for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) | |
5907e628 JL |
1455 | { |
1456 | asection *isec; | |
1457 | ||
6cdc0ccc | 1458 | if (isym->st_shndx == SHN_UNDEF) |
5907e628 | 1459 | isec = bfd_und_section_ptr; |
6cdc0ccc | 1460 | else if (isym->st_shndx == SHN_ABS) |
5907e628 | 1461 | isec = bfd_abs_section_ptr; |
6cdc0ccc | 1462 | else if (isym->st_shndx == SHN_COMMON) |
5907e628 JL |
1463 | isec = bfd_com_section_ptr; |
1464 | else | |
6cdc0ccc | 1465 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
5907e628 JL |
1466 | |
1467 | *secpp = isec; | |
1468 | } | |
1469 | ||
1470 | if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd, | |
1471 | input_section, data, internal_relocs, | |
6cdc0ccc | 1472 | isymbuf, sections)) |
5907e628 JL |
1473 | goto error_return; |
1474 | ||
1475 | if (sections != NULL) | |
1476 | free (sections); | |
6cdc0ccc AM |
1477 | if (isymbuf != NULL |
1478 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1479 | free (isymbuf); | |
1480 | if (elf_section_data (input_section)->relocs != internal_relocs) | |
5907e628 | 1481 | free (internal_relocs); |
5907e628 JL |
1482 | } |
1483 | ||
1484 | return data; | |
1485 | ||
1486 | error_return: | |
5907e628 JL |
1487 | if (sections != NULL) |
1488 | free (sections); | |
6cdc0ccc AM |
1489 | if (isymbuf != NULL |
1490 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1491 | free (isymbuf); | |
1492 | if (internal_relocs != NULL | |
1493 | && elf_section_data (input_section)->relocs != internal_relocs) | |
1494 | free (internal_relocs); | |
5907e628 JL |
1495 | return NULL; |
1496 | } | |
1497 | ||
2627de83 AM |
1498 | static asection * |
1499 | elf32_h8_gc_mark_hook (asection *sec, | |
1500 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
1501 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
1502 | struct elf_link_hash_entry *h, | |
1503 | Elf_Internal_Sym *sym) | |
1504 | { | |
1505 | if (h != NULL) | |
1506 | { | |
1507 | switch (h->root.type) | |
1508 | { | |
96ef1419 KH |
1509 | case bfd_link_hash_defined: |
1510 | case bfd_link_hash_defweak: | |
2627de83 | 1511 | return h->root.u.def.section; |
96ef1419 KH |
1512 | |
1513 | case bfd_link_hash_common: | |
2627de83 | 1514 | return h->root.u.c.p->section; |
96ef1419 KH |
1515 | |
1516 | default: | |
2627de83 AM |
1517 | break; |
1518 | } | |
1519 | } | |
1520 | else | |
96ef1419 | 1521 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
2627de83 AM |
1522 | return NULL; |
1523 | } | |
1524 | ||
1525 | static bfd_boolean | |
1526 | elf32_h8_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
1527 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
96ef1419 | 1528 | asection *sec ATTRIBUTE_UNUSED, |
2627de83 AM |
1529 | const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED) |
1530 | { | |
1531 | return TRUE; | |
1532 | } | |
1533 | ||
0a83638b | 1534 | |
e01b0e69 JR |
1535 | #define TARGET_BIG_SYM bfd_elf32_h8300_vec |
1536 | #define TARGET_BIG_NAME "elf32-h8300" | |
1537 | #define ELF_ARCH bfd_arch_h8300 | |
1538 | #define ELF_MACHINE_CODE EM_H8_300 | |
1539 | #define ELF_MAXPAGESIZE 0x1 | |
1540 | #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup | |
1541 | #define elf_info_to_howto elf32_h8_info_to_howto | |
1542 | #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel | |
1543 | ||
0a83638b JL |
1544 | /* So we can set/examine bits in e_flags to get the specific |
1545 | H8 architecture in use. */ | |
1546 | #define elf_backend_final_write_processing \ | |
1547 | elf32_h8_final_write_processing | |
1548 | #define elf_backend_object_p \ | |
1549 | elf32_h8_object_p | |
1550 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
1551 | elf32_h8_merge_private_bfd_data | |
2627de83 AM |
1552 | #define elf_backend_gc_mark_hook elf32_h8_gc_mark_hook |
1553 | #define elf_backend_gc_sweep_hook elf32_h8_gc_sweep_hook | |
0a83638b | 1554 | |
e01b0e69 JR |
1555 | /* ??? when elf_backend_relocate_section is not defined, elf32-target.h |
1556 | defaults to using _bfd_generic_link_hash_table_create, but | |
1557 | elflink.h:bfd_elf32_size_dynamic_sections uses | |
1558 | dynobj = elf_hash_table (info)->dynobj; | |
1559 | and thus requires an elf hash table. */ | |
1560 | #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create | |
1561 | ||
5e47149d JL |
1562 | /* Use an H8 specific linker, not the ELF generic linker. */ |
1563 | #define elf_backend_relocate_section elf32_h8_relocate_section | |
f0fe0e16 | 1564 | #define elf_backend_rela_normal 1 |
2627de83 | 1565 | #define elf_backend_can_gc_sections 1 |
5e47149d | 1566 | |
5907e628 JL |
1567 | /* And relaxing stuff. */ |
1568 | #define bfd_elf32_bfd_relax_section elf32_h8_relax_section | |
1569 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
1570 | elf32_h8_get_relocated_section_contents | |
1571 | ||
1572 | ||
e01b0e69 | 1573 | #include "elf32-target.h" |