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