elf32-m68hc11.c revision 1.1 1 1.1 christos /* Motorola 68HC11-specific support for 32-bit ELF
2 1.1 christos Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2010, 2012
3 1.1 christos Free Software Foundation, Inc.
4 1.1 christos Contributed by Stephane Carrez (stcarrez (at) nerim.fr)
5 1.1 christos (Heavily copied from the D10V port by Martin Hunt (hunt (at) cygnus.com))
6 1.1 christos
7 1.1 christos This file is part of BFD, the Binary File Descriptor library.
8 1.1 christos
9 1.1 christos This program is free software; you can redistribute it and/or modify
10 1.1 christos it under the terms of the GNU General Public License as published by
11 1.1 christos the Free Software Foundation; either version 3 of the License, or
12 1.1 christos (at your option) any later version.
13 1.1 christos
14 1.1 christos This program is distributed in the hope that it will be useful,
15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 1.1 christos GNU General Public License for more details.
18 1.1 christos
19 1.1 christos You should have received a copy of the GNU General Public License
20 1.1 christos along with this program; if not, write to the Free Software
21 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 1.1 christos MA 02110-1301, USA. */
23 1.1 christos
24 1.1 christos #include "sysdep.h"
25 1.1 christos #include "bfd.h"
26 1.1 christos #include "bfdlink.h"
27 1.1 christos #include "libbfd.h"
28 1.1 christos #include "elf-bfd.h"
29 1.1 christos #include "elf32-m68hc1x.h"
30 1.1 christos #include "elf/m68hc11.h"
31 1.1 christos #include "opcode/m68hc11.h"
32 1.1 christos
33 1.1 christos /* Relocation functions. */
34 1.1 christos static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
35 1.1 christos (bfd *, bfd_reloc_code_real_type);
36 1.1 christos static void m68hc11_info_to_howto_rel
37 1.1 christos (bfd *, arelent *, Elf_Internal_Rela *);
38 1.1 christos
39 1.1 christos /* Trampoline generation. */
40 1.1 christos static bfd_boolean m68hc11_elf_size_one_stub
41 1.1 christos (struct bfd_hash_entry *gen_entry, void *in_arg);
42 1.1 christos static bfd_boolean m68hc11_elf_build_one_stub
43 1.1 christos (struct bfd_hash_entry *gen_entry, void *in_arg);
44 1.1 christos static struct bfd_link_hash_table* m68hc11_elf_bfd_link_hash_table_create
45 1.1 christos (bfd* abfd);
46 1.1 christos
47 1.1 christos /* Linker relaxation. */
48 1.1 christos static bfd_boolean m68hc11_elf_relax_section
49 1.1 christos (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
50 1.1 christos static void m68hc11_elf_relax_delete_bytes
51 1.1 christos (bfd *, asection *, bfd_vma, int);
52 1.1 christos static void m68hc11_relax_group
53 1.1 christos (bfd *, asection *, bfd_byte *, unsigned, unsigned long, unsigned long);
54 1.1 christos static int compare_reloc (const void *, const void *);
55 1.1 christos
56 1.1 christos /* Use REL instead of RELA to save space */
57 1.1 christos #define USE_REL 1
58 1.1 christos
59 1.1 christos /* The Motorola 68HC11 microcontroller only addresses 64Kb but we also
60 1.1 christos support a memory bank switching mechanism similar to 68HC12.
61 1.1 christos We must handle 8 and 16-bit relocations. The 32-bit relocation
62 1.1 christos are used for debugging sections (DWARF2) to represent a virtual
63 1.1 christos address.
64 1.1 christos The 3-bit and 16-bit PC rel relocation is only used by 68HC12. */
65 1.1 christos static reloc_howto_type elf_m68hc11_howto_table[] = {
66 1.1 christos /* This reloc does nothing. */
67 1.1 christos HOWTO (R_M68HC11_NONE, /* type */
68 1.1 christos 0, /* rightshift */
69 1.1 christos 2, /* size (0 = byte, 1 = short, 2 = long) */
70 1.1 christos 32, /* bitsize */
71 1.1 christos FALSE, /* pc_relative */
72 1.1 christos 0, /* bitpos */
73 1.1 christos complain_overflow_dont,/* complain_on_overflow */
74 1.1 christos bfd_elf_generic_reloc, /* special_function */
75 1.1 christos "R_M68HC11_NONE", /* name */
76 1.1 christos FALSE, /* partial_inplace */
77 1.1 christos 0, /* src_mask */
78 1.1 christos 0, /* dst_mask */
79 1.1 christos FALSE), /* pcrel_offset */
80 1.1 christos
81 1.1 christos /* A 8 bit absolute relocation */
82 1.1 christos HOWTO (R_M68HC11_8, /* type */
83 1.1 christos 0, /* rightshift */
84 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
85 1.1 christos 8, /* bitsize */
86 1.1 christos FALSE, /* pc_relative */
87 1.1 christos 0, /* bitpos */
88 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
89 1.1 christos bfd_elf_generic_reloc, /* special_function */
90 1.1 christos "R_M68HC11_8", /* name */
91 1.1 christos FALSE, /* partial_inplace */
92 1.1 christos 0x00ff, /* src_mask */
93 1.1 christos 0x00ff, /* dst_mask */
94 1.1 christos FALSE), /* pcrel_offset */
95 1.1 christos
96 1.1 christos /* A 8 bit absolute relocation (upper address) */
97 1.1 christos HOWTO (R_M68HC11_HI8, /* type */
98 1.1 christos 8, /* rightshift */
99 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
100 1.1 christos 8, /* bitsize */
101 1.1 christos FALSE, /* pc_relative */
102 1.1 christos 0, /* bitpos */
103 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
104 1.1 christos bfd_elf_generic_reloc, /* special_function */
105 1.1 christos "R_M68HC11_HI8", /* name */
106 1.1 christos FALSE, /* partial_inplace */
107 1.1 christos 0x00ff, /* src_mask */
108 1.1 christos 0x00ff, /* dst_mask */
109 1.1 christos FALSE), /* pcrel_offset */
110 1.1 christos
111 1.1 christos /* A 8 bit absolute relocation (upper address) */
112 1.1 christos HOWTO (R_M68HC11_LO8, /* type */
113 1.1 christos 0, /* rightshift */
114 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
115 1.1 christos 8, /* bitsize */
116 1.1 christos FALSE, /* pc_relative */
117 1.1 christos 0, /* bitpos */
118 1.1 christos complain_overflow_dont, /* complain_on_overflow */
119 1.1 christos bfd_elf_generic_reloc, /* special_function */
120 1.1 christos "R_M68HC11_LO8", /* name */
121 1.1 christos FALSE, /* partial_inplace */
122 1.1 christos 0x00ff, /* src_mask */
123 1.1 christos 0x00ff, /* dst_mask */
124 1.1 christos FALSE), /* pcrel_offset */
125 1.1 christos
126 1.1 christos /* A 8 bit PC-rel relocation */
127 1.1 christos HOWTO (R_M68HC11_PCREL_8, /* type */
128 1.1 christos 0, /* rightshift */
129 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
130 1.1 christos 8, /* bitsize */
131 1.1 christos TRUE, /* pc_relative */
132 1.1 christos 0, /* bitpos */
133 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
134 1.1 christos bfd_elf_generic_reloc, /* special_function */
135 1.1 christos "R_M68HC11_PCREL_8", /* name */
136 1.1 christos FALSE, /* partial_inplace */
137 1.1 christos 0x00ff, /* src_mask */
138 1.1 christos 0x00ff, /* dst_mask */
139 1.1 christos TRUE), /* pcrel_offset */
140 1.1 christos
141 1.1 christos /* A 16 bit absolute relocation */
142 1.1 christos HOWTO (R_M68HC11_16, /* type */
143 1.1 christos 0, /* rightshift */
144 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
145 1.1 christos 16, /* bitsize */
146 1.1 christos FALSE, /* pc_relative */
147 1.1 christos 0, /* bitpos */
148 1.1 christos complain_overflow_dont /*bitfield */ , /* complain_on_overflow */
149 1.1 christos bfd_elf_generic_reloc, /* special_function */
150 1.1 christos "R_M68HC11_16", /* name */
151 1.1 christos FALSE, /* partial_inplace */
152 1.1 christos 0xffff, /* src_mask */
153 1.1 christos 0xffff, /* dst_mask */
154 1.1 christos FALSE), /* pcrel_offset */
155 1.1 christos
156 1.1 christos /* A 32 bit absolute relocation. This one is never used for the
157 1.1 christos code relocation. It's used by gas for -gstabs generation. */
158 1.1 christos HOWTO (R_M68HC11_32, /* type */
159 1.1 christos 0, /* rightshift */
160 1.1 christos 2, /* size (0 = byte, 1 = short, 2 = long) */
161 1.1 christos 32, /* bitsize */
162 1.1 christos FALSE, /* pc_relative */
163 1.1 christos 0, /* bitpos */
164 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
165 1.1 christos bfd_elf_generic_reloc, /* special_function */
166 1.1 christos "R_M68HC11_32", /* name */
167 1.1 christos FALSE, /* partial_inplace */
168 1.1 christos 0xffffffff, /* src_mask */
169 1.1 christos 0xffffffff, /* dst_mask */
170 1.1 christos FALSE), /* pcrel_offset */
171 1.1 christos
172 1.1 christos /* A 3 bit absolute relocation */
173 1.1 christos HOWTO (R_M68HC11_3B, /* type */
174 1.1 christos 0, /* rightshift */
175 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
176 1.1 christos 3, /* bitsize */
177 1.1 christos FALSE, /* pc_relative */
178 1.1 christos 0, /* bitpos */
179 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
180 1.1 christos bfd_elf_generic_reloc, /* special_function */
181 1.1 christos "R_M68HC11_4B", /* name */
182 1.1 christos FALSE, /* partial_inplace */
183 1.1 christos 0x003, /* src_mask */
184 1.1 christos 0x003, /* dst_mask */
185 1.1 christos FALSE), /* pcrel_offset */
186 1.1 christos
187 1.1 christos /* A 16 bit PC-rel relocation */
188 1.1 christos HOWTO (R_M68HC11_PCREL_16, /* type */
189 1.1 christos 0, /* rightshift */
190 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
191 1.1 christos 16, /* bitsize */
192 1.1 christos TRUE, /* pc_relative */
193 1.1 christos 0, /* bitpos */
194 1.1 christos complain_overflow_dont, /* complain_on_overflow */
195 1.1 christos bfd_elf_generic_reloc, /* special_function */
196 1.1 christos "R_M68HC11_PCREL_16", /* name */
197 1.1 christos FALSE, /* partial_inplace */
198 1.1 christos 0xffff, /* src_mask */
199 1.1 christos 0xffff, /* dst_mask */
200 1.1 christos TRUE), /* pcrel_offset */
201 1.1 christos
202 1.1 christos /* GNU extension to record C++ vtable hierarchy */
203 1.1 christos HOWTO (R_M68HC11_GNU_VTINHERIT, /* type */
204 1.1 christos 0, /* rightshift */
205 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
206 1.1 christos 0, /* bitsize */
207 1.1 christos FALSE, /* pc_relative */
208 1.1 christos 0, /* bitpos */
209 1.1 christos complain_overflow_dont, /* complain_on_overflow */
210 1.1 christos NULL, /* special_function */
211 1.1 christos "R_M68HC11_GNU_VTINHERIT", /* name */
212 1.1 christos FALSE, /* partial_inplace */
213 1.1 christos 0, /* src_mask */
214 1.1 christos 0, /* dst_mask */
215 1.1 christos FALSE), /* pcrel_offset */
216 1.1 christos
217 1.1 christos /* GNU extension to record C++ vtable member usage */
218 1.1 christos HOWTO (R_M68HC11_GNU_VTENTRY, /* type */
219 1.1 christos 0, /* rightshift */
220 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
221 1.1 christos 0, /* bitsize */
222 1.1 christos FALSE, /* pc_relative */
223 1.1 christos 0, /* bitpos */
224 1.1 christos complain_overflow_dont, /* complain_on_overflow */
225 1.1 christos _bfd_elf_rel_vtable_reloc_fn, /* special_function */
226 1.1 christos "R_M68HC11_GNU_VTENTRY", /* name */
227 1.1 christos FALSE, /* partial_inplace */
228 1.1 christos 0, /* src_mask */
229 1.1 christos 0, /* dst_mask */
230 1.1 christos FALSE), /* pcrel_offset */
231 1.1 christos
232 1.1 christos /* A 24 bit relocation */
233 1.1 christos HOWTO (R_M68HC11_24, /* type */
234 1.1 christos 0, /* rightshift */
235 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
236 1.1 christos 24, /* bitsize */
237 1.1 christos FALSE, /* pc_relative */
238 1.1 christos 0, /* bitpos */
239 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
240 1.1 christos bfd_elf_generic_reloc, /* special_function */
241 1.1 christos "R_M68HC11_24", /* name */
242 1.1 christos FALSE, /* partial_inplace */
243 1.1 christos 0xffffff, /* src_mask */
244 1.1 christos 0xffffff, /* dst_mask */
245 1.1 christos FALSE), /* pcrel_offset */
246 1.1 christos
247 1.1 christos /* A 16-bit low relocation */
248 1.1 christos HOWTO (R_M68HC11_LO16, /* type */
249 1.1 christos 0, /* rightshift */
250 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
251 1.1 christos 16, /* bitsize */
252 1.1 christos FALSE, /* pc_relative */
253 1.1 christos 0, /* bitpos */
254 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
255 1.1 christos bfd_elf_generic_reloc, /* special_function */
256 1.1 christos "R_M68HC11_LO16", /* name */
257 1.1 christos FALSE, /* partial_inplace */
258 1.1 christos 0xffff, /* src_mask */
259 1.1 christos 0xffff, /* dst_mask */
260 1.1 christos FALSE), /* pcrel_offset */
261 1.1 christos
262 1.1 christos /* A page relocation */
263 1.1 christos HOWTO (R_M68HC11_PAGE, /* type */
264 1.1 christos 0, /* rightshift */
265 1.1 christos 0, /* size (0 = byte, 1 = short, 2 = long) */
266 1.1 christos 8, /* bitsize */
267 1.1 christos FALSE, /* pc_relative */
268 1.1 christos 0, /* bitpos */
269 1.1 christos complain_overflow_bitfield, /* complain_on_overflow */
270 1.1 christos bfd_elf_generic_reloc, /* special_function */
271 1.1 christos "R_M68HC11_PAGE", /* name */
272 1.1 christos FALSE, /* partial_inplace */
273 1.1 christos 0x00ff, /* src_mask */
274 1.1 christos 0x00ff, /* dst_mask */
275 1.1 christos FALSE), /* pcrel_offset */
276 1.1 christos
277 1.1 christos EMPTY_HOWTO (14),
278 1.1 christos EMPTY_HOWTO (15),
279 1.1 christos EMPTY_HOWTO (16),
280 1.1 christos EMPTY_HOWTO (17),
281 1.1 christos EMPTY_HOWTO (18),
282 1.1 christos EMPTY_HOWTO (19),
283 1.1 christos
284 1.1 christos /* Mark beginning of a jump instruction (any form). */
285 1.1 christos HOWTO (R_M68HC11_RL_JUMP, /* type */
286 1.1 christos 0, /* rightshift */
287 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
288 1.1 christos 0, /* bitsize */
289 1.1 christos FALSE, /* pc_relative */
290 1.1 christos 0, /* bitpos */
291 1.1 christos complain_overflow_dont, /* complain_on_overflow */
292 1.1 christos m68hc11_elf_ignore_reloc, /* special_function */
293 1.1 christos "R_M68HC11_RL_JUMP", /* name */
294 1.1 christos TRUE, /* partial_inplace */
295 1.1 christos 0, /* src_mask */
296 1.1 christos 0, /* dst_mask */
297 1.1 christos TRUE), /* pcrel_offset */
298 1.1 christos
299 1.1 christos /* Mark beginning of Gcc relaxation group instruction. */
300 1.1 christos HOWTO (R_M68HC11_RL_GROUP, /* type */
301 1.1 christos 0, /* rightshift */
302 1.1 christos 1, /* size (0 = byte, 1 = short, 2 = long) */
303 1.1 christos 0, /* bitsize */
304 1.1 christos FALSE, /* pc_relative */
305 1.1 christos 0, /* bitpos */
306 1.1 christos complain_overflow_dont, /* complain_on_overflow */
307 1.1 christos m68hc11_elf_ignore_reloc, /* special_function */
308 1.1 christos "R_M68HC11_RL_GROUP", /* name */
309 1.1 christos TRUE, /* partial_inplace */
310 1.1 christos 0, /* src_mask */
311 1.1 christos 0, /* dst_mask */
312 1.1 christos TRUE), /* pcrel_offset */
313 1.1 christos };
314 1.1 christos
315 1.1 christos /* Map BFD reloc types to M68HC11 ELF reloc types. */
316 1.1 christos
317 1.1 christos struct m68hc11_reloc_map
318 1.1 christos {
319 1.1 christos bfd_reloc_code_real_type bfd_reloc_val;
320 1.1 christos unsigned char elf_reloc_val;
321 1.1 christos };
322 1.1 christos
323 1.1 christos static const struct m68hc11_reloc_map m68hc11_reloc_map[] = {
324 1.1 christos {BFD_RELOC_NONE, R_M68HC11_NONE,},
325 1.1 christos {BFD_RELOC_8, R_M68HC11_8},
326 1.1 christos {BFD_RELOC_M68HC11_HI8, R_M68HC11_HI8},
327 1.1 christos {BFD_RELOC_M68HC11_LO8, R_M68HC11_LO8},
328 1.1 christos {BFD_RELOC_8_PCREL, R_M68HC11_PCREL_8},
329 1.1 christos {BFD_RELOC_16_PCREL, R_M68HC11_PCREL_16},
330 1.1 christos {BFD_RELOC_16, R_M68HC11_16},
331 1.1 christos {BFD_RELOC_32, R_M68HC11_32},
332 1.1 christos {BFD_RELOC_M68HC11_3B, R_M68HC11_3B},
333 1.1 christos
334 1.1 christos {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT},
335 1.1 christos {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY},
336 1.1 christos
337 1.1 christos {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16},
338 1.1 christos {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE},
339 1.1 christos {BFD_RELOC_M68HC11_24, R_M68HC11_24},
340 1.1 christos
341 1.1 christos {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP},
342 1.1 christos {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP},
343 1.1 christos };
344 1.1 christos
345 1.1 christos static reloc_howto_type *
346 1.1 christos bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
347 1.1 christos bfd_reloc_code_real_type code)
348 1.1 christos {
349 1.1 christos unsigned int i;
350 1.1 christos
351 1.1 christos for (i = 0;
352 1.1 christos i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map);
353 1.1 christos i++)
354 1.1 christos {
355 1.1 christos if (m68hc11_reloc_map[i].bfd_reloc_val == code)
356 1.1 christos return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val];
357 1.1 christos }
358 1.1 christos
359 1.1 christos return NULL;
360 1.1 christos }
361 1.1 christos
362 1.1 christos static reloc_howto_type *
363 1.1 christos bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
364 1.1 christos const char *r_name)
365 1.1 christos {
366 1.1 christos unsigned int i;
367 1.1 christos
368 1.1 christos for (i = 0;
369 1.1 christos i < (sizeof (elf_m68hc11_howto_table)
370 1.1 christos / sizeof (elf_m68hc11_howto_table[0]));
371 1.1 christos i++)
372 1.1 christos if (elf_m68hc11_howto_table[i].name != NULL
373 1.1 christos && strcasecmp (elf_m68hc11_howto_table[i].name, r_name) == 0)
374 1.1 christos return &elf_m68hc11_howto_table[i];
375 1.1 christos
376 1.1 christos return NULL;
377 1.1 christos }
378 1.1 christos
379 1.1 christos /* Set the howto pointer for an M68HC11 ELF reloc. */
380 1.1 christos
381 1.1 christos static void
382 1.1 christos m68hc11_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
383 1.1 christos arelent *cache_ptr, Elf_Internal_Rela *dst)
384 1.1 christos {
385 1.1 christos unsigned int r_type;
386 1.1 christos
387 1.1 christos r_type = ELF32_R_TYPE (dst->r_info);
388 1.1 christos BFD_ASSERT (r_type < (unsigned int) R_M68HC11_max);
389 1.1 christos cache_ptr->howto = &elf_m68hc11_howto_table[r_type];
390 1.1 christos }
391 1.1 christos
392 1.1 christos
393 1.1 christos /* Far trampoline generation. */
395 1.1 christos
396 1.1 christos /* Build a 68HC11 trampoline stub. */
397 1.1 christos static bfd_boolean
398 1.1 christos m68hc11_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
399 1.1 christos {
400 1.1 christos struct elf32_m68hc11_stub_hash_entry *stub_entry;
401 1.1 christos struct bfd_link_info *info;
402 1.1 christos struct m68hc11_elf_link_hash_table *htab;
403 1.1 christos asection *stub_sec;
404 1.1 christos bfd *stub_bfd;
405 1.1 christos bfd_byte *loc;
406 1.1 christos bfd_vma sym_value, phys_page, phys_addr;
407 1.1 christos
408 1.1 christos /* Massage our args to the form they really have. */
409 1.1 christos stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
410 1.1 christos info = (struct bfd_link_info *) in_arg;
411 1.1 christos
412 1.1 christos htab = m68hc11_elf_hash_table (info);
413 1.1 christos if (htab == NULL)
414 1.1 christos return FALSE;
415 1.1 christos
416 1.1 christos stub_sec = stub_entry->stub_sec;
417 1.1 christos
418 1.1 christos /* Make a note of the offset within the stubs for this entry. */
419 1.1 christos stub_entry->stub_offset = stub_sec->size;
420 1.1 christos stub_sec->size += 10;
421 1.1 christos loc = stub_sec->contents + stub_entry->stub_offset;
422 1.1 christos
423 1.1 christos stub_bfd = stub_sec->owner;
424 1.1 christos
425 1.1 christos /* Create the trampoline call stub:
426 1.1 christos
427 1.1 christos pshb
428 1.1 christos ldab #%page(symbol)
429 1.1 christos ldy #%addr(symbol)
430 1.1 christos jmp __trampoline
431 1.1 christos
432 1.1 christos */
433 1.1 christos sym_value = (stub_entry->target_value
434 1.1 christos + stub_entry->target_section->output_offset
435 1.1 christos + stub_entry->target_section->output_section->vma);
436 1.1 christos phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value);
437 1.1 christos phys_page = m68hc11_phys_page (&htab->pinfo, sym_value);
438 1.1 christos
439 1.1 christos /* pshb; ldab #%page(sym) */
440 1.1 christos bfd_put_8 (stub_bfd, 0x37, loc);
441 1.1 christos bfd_put_8 (stub_bfd, 0xC6, loc + 1);
442 1.1 christos bfd_put_8 (stub_bfd, phys_page, loc + 2);
443 1.1 christos loc += 3;
444 1.1 christos
445 1.1 christos /* ldy #%addr(sym) */
446 1.1 christos bfd_put_8 (stub_bfd, 0x18, loc);
447 1.1 christos bfd_put_8 (stub_bfd, 0xCE, loc + 1);
448 1.1 christos bfd_put_16 (stub_bfd, phys_addr, loc + 2);
449 1.1 christos loc += 4;
450 1.1 christos
451 1.1 christos /* jmp __trampoline */
452 1.1 christos bfd_put_8 (stub_bfd, 0x7E, loc);
453 1.1 christos bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1);
454 1.1 christos
455 1.1 christos return TRUE;
456 1.1 christos }
457 1.1 christos
458 1.1 christos /* As above, but don't actually build the stub. Just bump offset so
459 1.1 christos we know stub section sizes. */
460 1.1 christos
461 1.1 christos static bfd_boolean
462 1.1 christos m68hc11_elf_size_one_stub (struct bfd_hash_entry *gen_entry,
463 1.1 christos void *in_arg ATTRIBUTE_UNUSED)
464 1.1 christos {
465 1.1 christos struct elf32_m68hc11_stub_hash_entry *stub_entry;
466 1.1 christos
467 1.1 christos /* Massage our args to the form they really have. */
468 1.1 christos stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
469 1.1 christos
470 1.1 christos stub_entry->stub_sec->size += 10;
471 1.1 christos return TRUE;
472 1.1 christos }
473 1.1 christos
474 1.1 christos /* Create a 68HC11 ELF linker hash table. */
475 1.1 christos
476 1.1 christos static struct bfd_link_hash_table *
477 1.1 christos m68hc11_elf_bfd_link_hash_table_create (bfd *abfd)
478 1.1 christos {
479 1.1 christos struct m68hc11_elf_link_hash_table *ret;
480 1.1 christos
481 1.1 christos ret = m68hc11_elf_hash_table_create (abfd);
482 1.1 christos if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
483 1.1 christos return NULL;
484 1.1 christos
485 1.1 christos ret->size_one_stub = m68hc11_elf_size_one_stub;
486 1.1 christos ret->build_one_stub = m68hc11_elf_build_one_stub;
487 1.1 christos
488 1.1 christos return &ret->root.root;
489 1.1 christos }
490 1.1 christos
491 1.1 christos
492 1.1 christos /* 68HC11 Linker Relaxation. */
494 1.1 christos
495 1.1 christos struct m68hc11_direct_relax
496 1.1 christos {
497 1.1 christos const char *name;
498 1.1 christos unsigned char code;
499 1.1 christos unsigned char direct_code;
500 1.1 christos } m68hc11_direct_relax_table[] = {
501 1.1 christos { "adca", 0xB9, 0x99 },
502 1.1 christos { "adcb", 0xF9, 0xD9 },
503 1.1 christos { "adda", 0xBB, 0x9B },
504 1.1 christos { "addb", 0xFB, 0xDB },
505 1.1 christos { "addd", 0xF3, 0xD3 },
506 1.1 christos { "anda", 0xB4, 0x94 },
507 1.1 christos { "andb", 0xF4, 0xD4 },
508 1.1 christos { "cmpa", 0xB1, 0x91 },
509 1.1 christos { "cmpb", 0xF1, 0xD1 },
510 1.1 christos { "cpd", 0xB3, 0x93 },
511 1.1 christos { "cpxy", 0xBC, 0x9C },
512 1.1 christos /* { "cpy", 0xBC, 0x9C }, */
513 1.1 christos { "eora", 0xB8, 0x98 },
514 1.1 christos { "eorb", 0xF8, 0xD8 },
515 1.1 christos { "jsr", 0xBD, 0x9D },
516 1.1 christos { "ldaa", 0xB6, 0x96 },
517 1.1 christos { "ldab", 0xF6, 0xD6 },
518 1.1 christos { "ldd", 0xFC, 0xDC },
519 1.1 christos { "lds", 0xBE, 0x9E },
520 1.1 christos { "ldxy", 0xFE, 0xDE },
521 1.1 christos /* { "ldy", 0xFE, 0xDE },*/
522 1.1 christos { "oraa", 0xBA, 0x9A },
523 1.1 christos { "orab", 0xFA, 0xDA },
524 1.1 christos { "sbca", 0xB2, 0x92 },
525 1.1 christos { "sbcb", 0xF2, 0xD2 },
526 1.1 christos { "staa", 0xB7, 0x97 },
527 1.1 christos { "stab", 0xF7, 0xD7 },
528 1.1 christos { "std", 0xFD, 0xDD },
529 1.1 christos { "sts", 0xBF, 0x9F },
530 1.1 christos { "stxy", 0xFF, 0xDF },
531 1.1 christos /* { "sty", 0xFF, 0xDF },*/
532 1.1 christos { "suba", 0xB0, 0x90 },
533 1.1 christos { "subb", 0xF0, 0xD0 },
534 1.1 christos { "subd", 0xB3, 0x93 },
535 1.1 christos { 0, 0, 0 }
536 1.1 christos };
537 1.1 christos
538 1.1 christos static struct m68hc11_direct_relax *
539 1.1 christos find_relaxable_insn (unsigned char code)
540 1.1 christos {
541 1.1 christos int i;
542 1.1 christos
543 1.1 christos for (i = 0; m68hc11_direct_relax_table[i].name; i++)
544 1.1 christos if (m68hc11_direct_relax_table[i].code == code)
545 1.1 christos return &m68hc11_direct_relax_table[i];
546 1.1 christos
547 1.1 christos return 0;
548 1.1 christos }
549 1.1 christos
550 1.1 christos static int
551 1.1 christos compare_reloc (const void *e1, const void *e2)
552 1.1 christos {
553 1.1 christos const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
554 1.1 christos const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
555 1.1 christos
556 1.1 christos if (i1->r_offset == i2->r_offset)
557 1.1 christos return 0;
558 1.1 christos else
559 1.1 christos return i1->r_offset < i2->r_offset ? -1 : 1;
560 1.1 christos }
561 1.1 christos
562 1.1 christos #define M6811_OP_LDX_IMMEDIATE (0xCE)
563 1.1 christos
564 1.1 christos static void
565 1.1 christos m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
566 1.1 christos unsigned value, unsigned long offset,
567 1.1 christos unsigned long end_group)
568 1.1 christos {
569 1.1 christos unsigned char code;
570 1.1 christos unsigned long start_offset;
571 1.1 christos unsigned long ldx_offset = offset;
572 1.1 christos unsigned long ldx_size;
573 1.1 christos int can_delete_ldx;
574 1.1 christos int relax_ldy = 0;
575 1.1 christos
576 1.1 christos /* First instruction of the relax group must be a
577 1.1 christos LDX #value or LDY #value. If this is not the case,
578 1.1 christos ignore the relax group. */
579 1.1 christos code = bfd_get_8 (abfd, contents + offset);
580 1.1 christos if (code == 0x18)
581 1.1 christos {
582 1.1 christos relax_ldy++;
583 1.1 christos offset++;
584 1.1 christos code = bfd_get_8 (abfd, contents + offset);
585 1.1 christos }
586 1.1 christos ldx_size = offset - ldx_offset + 3;
587 1.1 christos offset += 3;
588 1.1 christos if (code != M6811_OP_LDX_IMMEDIATE || offset >= end_group)
589 1.1 christos return;
590 1.1 christos
591 1.1 christos
592 1.1 christos /* We can remove the LDX/LDY only when all bset/brclr instructions
593 1.1 christos of the relax group have been converted to use direct addressing
594 1.1 christos mode. */
595 1.1 christos can_delete_ldx = 1;
596 1.1 christos while (offset < end_group)
597 1.1 christos {
598 1.1 christos unsigned isize;
599 1.1 christos unsigned new_value;
600 1.1 christos int bset_use_y;
601 1.1 christos
602 1.1 christos bset_use_y = 0;
603 1.1 christos start_offset = offset;
604 1.1 christos code = bfd_get_8 (abfd, contents + offset);
605 1.1 christos if (code == 0x18)
606 1.1 christos {
607 1.1 christos bset_use_y++;
608 1.1 christos offset++;
609 1.1 christos code = bfd_get_8 (abfd, contents + offset);
610 1.1 christos }
611 1.1 christos
612 1.1 christos /* Check the instruction and translate to use direct addressing mode. */
613 1.1 christos switch (code)
614 1.1 christos {
615 1.1 christos /* bset */
616 1.1 christos case 0x1C:
617 1.1 christos code = 0x14;
618 1.1 christos isize = 3;
619 1.1 christos break;
620 1.1 christos
621 1.1 christos /* brclr */
622 1.1 christos case 0x1F:
623 1.1 christos code = 0x13;
624 1.1 christos isize = 4;
625 1.1 christos break;
626 1.1 christos
627 1.1 christos /* brset */
628 1.1 christos case 0x1E:
629 1.1 christos code = 0x12;
630 1.1 christos isize = 4;
631 1.1 christos break;
632 1.1 christos
633 1.1 christos /* bclr */
634 1.1 christos case 0x1D:
635 1.1 christos code = 0x15;
636 1.1 christos isize = 3;
637 1.1 christos break;
638 1.1 christos
639 1.1 christos /* This instruction is not recognized and we are not
640 1.1 christos at end of the relax group. Ignore and don't remove
641 1.1 christos the first LDX (we don't know what it is used for...). */
642 1.1 christos default:
643 1.1 christos return;
644 1.1 christos }
645 1.1 christos new_value = (unsigned) bfd_get_8 (abfd, contents + offset + 1);
646 1.1 christos new_value += value;
647 1.1 christos if ((new_value & 0xff00) == 0 && bset_use_y == relax_ldy)
648 1.1 christos {
649 1.1 christos bfd_put_8 (abfd, code, contents + offset);
650 1.1 christos bfd_put_8 (abfd, new_value, contents + offset + 1);
651 1.1 christos if (start_offset != offset)
652 1.1 christos {
653 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec, start_offset,
654 1.1 christos offset - start_offset);
655 1.1 christos end_group--;
656 1.1 christos }
657 1.1 christos }
658 1.1 christos else
659 1.1 christos {
660 1.1 christos can_delete_ldx = 0;
661 1.1 christos }
662 1.1 christos offset = start_offset + isize;
663 1.1 christos }
664 1.1 christos if (can_delete_ldx)
665 1.1 christos {
666 1.1 christos /* Remove the move instruction (3 or 4 bytes win). */
667 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec, ldx_offset, ldx_size);
668 1.1 christos }
669 1.1 christos }
670 1.1 christos
671 1.1 christos /* This function handles relaxing for the 68HC11.
672 1.1 christos
673 1.1 christos
674 1.1 christos and somewhat more difficult to support. */
675 1.1 christos
676 1.1 christos static bfd_boolean
677 1.1 christos m68hc11_elf_relax_section (bfd *abfd, asection *sec,
678 1.1 christos struct bfd_link_info *link_info, bfd_boolean *again)
679 1.1 christos {
680 1.1 christos Elf_Internal_Shdr *symtab_hdr;
681 1.1 christos Elf_Internal_Rela *internal_relocs;
682 1.1 christos Elf_Internal_Rela *free_relocs = NULL;
683 1.1 christos Elf_Internal_Rela *irel, *irelend;
684 1.1 christos bfd_byte *contents = NULL;
685 1.1 christos bfd_byte *free_contents = NULL;
686 1.1 christos Elf32_External_Sym *free_extsyms = NULL;
687 1.1 christos Elf_Internal_Rela *prev_insn_branch = NULL;
688 1.1 christos Elf_Internal_Rela *prev_insn_group = NULL;
689 1.1 christos unsigned insn_group_value = 0;
690 1.1 christos Elf_Internal_Sym *isymbuf = NULL;
691 1.1 christos
692 1.1 christos /* Assume nothing changes. */
693 1.1 christos *again = FALSE;
694 1.1 christos
695 1.1 christos /* We don't have to do anything for a relocatable link, if
696 1.1 christos this section does not have relocs, or if this is not a
697 1.1 christos code section. */
698 1.1 christos if (link_info->relocatable
699 1.1 christos || (sec->flags & SEC_RELOC) == 0
700 1.1 christos || sec->reloc_count == 0
701 1.1 christos || (sec->flags & SEC_CODE) == 0)
702 1.1 christos return TRUE;
703 1.1 christos
704 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
705 1.1 christos
706 1.1 christos /* Get a copy of the native relocations. */
707 1.1 christos internal_relocs = (_bfd_elf_link_read_relocs
708 1.1 christos (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
709 1.1 christos link_info->keep_memory));
710 1.1 christos if (internal_relocs == NULL)
711 1.1 christos goto error_return;
712 1.1 christos if (! link_info->keep_memory)
713 1.1 christos free_relocs = internal_relocs;
714 1.1 christos
715 1.1 christos /* Checking for branch relaxation relies on the relocations to
716 1.1 christos be sorted on 'r_offset'. This is not guaranteed so we must sort. */
717 1.1 christos qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
718 1.1 christos compare_reloc);
719 1.1 christos
720 1.1 christos /* Walk through them looking for relaxing opportunities. */
721 1.1 christos irelend = internal_relocs + sec->reloc_count;
722 1.1 christos for (irel = internal_relocs; irel < irelend; irel++)
723 1.1 christos {
724 1.1 christos bfd_vma symval;
725 1.1 christos bfd_vma value;
726 1.1 christos Elf_Internal_Sym *isym;
727 1.1 christos asection *sym_sec;
728 1.1 christos int is_far = 0;
729 1.1 christos
730 1.1 christos /* If this isn't something that can be relaxed, then ignore
731 1.1 christos this reloc. */
732 1.1 christos if (ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_16
733 1.1 christos && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_JUMP
734 1.1 christos && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_GROUP)
735 1.1 christos {
736 1.1 christos prev_insn_branch = 0;
737 1.1 christos prev_insn_group = 0;
738 1.1 christos continue;
739 1.1 christos }
740 1.1 christos
741 1.1 christos /* Get the section contents if we haven't done so already. */
742 1.1 christos if (contents == NULL)
743 1.1 christos {
744 1.1 christos /* Get cached copy if it exists. */
745 1.1 christos if (elf_section_data (sec)->this_hdr.contents != NULL)
746 1.1 christos contents = elf_section_data (sec)->this_hdr.contents;
747 1.1 christos else
748 1.1 christos {
749 1.1 christos /* Go get them off disk. */
750 1.1 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents))
751 1.1 christos goto error_return;
752 1.1 christos }
753 1.1 christos }
754 1.1 christos
755 1.1 christos /* Try to eliminate an unconditional 8 bit pc-relative branch
756 1.1 christos which immediately follows a conditional 8 bit pc-relative
757 1.1 christos branch around the unconditional branch.
758 1.1 christos
759 1.1 christos original: new:
760 1.1 christos bCC lab1 bCC' lab2
761 1.1 christos bra lab2
762 1.1 christos lab1: lab1:
763 1.1 christos
764 1.1 christos This happens when the bCC can't reach lab2 at assembly time,
765 1.1 christos but due to other relaxations it can reach at link time. */
766 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_JUMP)
767 1.1 christos {
768 1.1 christos Elf_Internal_Rela *nrel;
769 1.1 christos unsigned char code;
770 1.1 christos unsigned char roffset;
771 1.1 christos
772 1.1 christos prev_insn_branch = 0;
773 1.1 christos prev_insn_group = 0;
774 1.1 christos
775 1.1 christos /* Do nothing if this reloc is the last byte in the section. */
776 1.1 christos if (irel->r_offset + 2 >= sec->size)
777 1.1 christos continue;
778 1.1 christos
779 1.1 christos /* See if the next instruction is an unconditional pc-relative
780 1.1 christos branch, more often than not this test will fail, so we
781 1.1 christos test it first to speed things up. */
782 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset + 2);
783 1.1 christos if (code != 0x7e)
784 1.1 christos continue;
785 1.1 christos
786 1.1 christos /* Also make sure the next relocation applies to the next
787 1.1 christos instruction and that it's a pc-relative 8 bit branch. */
788 1.1 christos nrel = irel + 1;
789 1.1 christos if (nrel == irelend
790 1.1 christos || irel->r_offset + 3 != nrel->r_offset
791 1.1 christos || ELF32_R_TYPE (nrel->r_info) != (int) R_M68HC11_16)
792 1.1 christos continue;
793 1.1 christos
794 1.1 christos /* Make sure our destination immediately follows the
795 1.1 christos unconditional branch. */
796 1.1 christos roffset = bfd_get_8 (abfd, contents + irel->r_offset + 1);
797 1.1 christos if (roffset != 3)
798 1.1 christos continue;
799 1.1 christos
800 1.1 christos prev_insn_branch = irel;
801 1.1 christos prev_insn_group = 0;
802 1.1 christos continue;
803 1.1 christos }
804 1.1 christos
805 1.1 christos /* Read this BFD's symbols if we haven't done so already. */
806 1.1 christos if (isymbuf == NULL && symtab_hdr->sh_info != 0)
807 1.1 christos {
808 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
809 1.1 christos if (isymbuf == NULL)
810 1.1 christos isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
811 1.1 christos symtab_hdr->sh_info, 0,
812 1.1 christos NULL, NULL, NULL);
813 1.1 christos if (isymbuf == NULL)
814 1.1 christos goto error_return;
815 1.1 christos }
816 1.1 christos
817 1.1 christos /* Get the value of the symbol referred to by the reloc. */
818 1.1 christos if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
819 1.1 christos {
820 1.1 christos /* A local symbol. */
821 1.1 christos isym = isymbuf + ELF32_R_SYM (irel->r_info);
822 1.1 christos is_far = isym->st_other & STO_M68HC12_FAR;
823 1.1 christos sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
824 1.1 christos symval = (isym->st_value
825 1.1 christos + sym_sec->output_section->vma
826 1.1 christos + sym_sec->output_offset);
827 1.1 christos }
828 1.1 christos else
829 1.1 christos {
830 1.1 christos unsigned long indx;
831 1.1 christos struct elf_link_hash_entry *h;
832 1.1 christos
833 1.1 christos /* An external symbol. */
834 1.1 christos indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
835 1.1 christos h = elf_sym_hashes (abfd)[indx];
836 1.1 christos BFD_ASSERT (h != NULL);
837 1.1 christos if (h->root.type != bfd_link_hash_defined
838 1.1 christos && h->root.type != bfd_link_hash_defweak)
839 1.1 christos {
840 1.1 christos /* This appears to be a reference to an undefined
841 1.1 christos symbol. Just ignore it--it will be caught by the
842 1.1 christos regular reloc processing. */
843 1.1 christos prev_insn_branch = 0;
844 1.1 christos prev_insn_group = 0;
845 1.1 christos continue;
846 1.1 christos }
847 1.1 christos
848 1.1 christos is_far = h->other & STO_M68HC12_FAR;
849 1.1 christos isym = 0;
850 1.1 christos sym_sec = h->root.u.def.section;
851 1.1 christos symval = (h->root.u.def.value
852 1.1 christos + sym_sec->output_section->vma
853 1.1 christos + sym_sec->output_offset);
854 1.1 christos }
855 1.1 christos
856 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_GROUP)
857 1.1 christos {
858 1.1 christos prev_insn_branch = 0;
859 1.1 christos prev_insn_group = 0;
860 1.1 christos
861 1.1 christos /* Do nothing if this reloc is the last byte in the section. */
862 1.1 christos if (irel->r_offset == sec->size)
863 1.1 christos continue;
864 1.1 christos
865 1.1 christos prev_insn_group = irel;
866 1.1 christos insn_group_value = isym->st_value;
867 1.1 christos continue;
868 1.1 christos }
869 1.1 christos
870 1.1 christos /* When we relax some bytes, the size of our section changes.
871 1.1 christos This affects the layout of next input sections that go in our
872 1.1 christos output section. When the symbol is part of another section that
873 1.1 christos will go in the same output section as the current one, it's
874 1.1 christos final address may now be incorrect (too far). We must let the
875 1.1 christos linker re-compute all section offsets before processing this
876 1.1 christos reloc. Code example:
877 1.1 christos
878 1.1 christos Initial Final
879 1.1 christos .sect .text section size = 6 section size = 4
880 1.1 christos jmp foo
881 1.1 christos jmp bar
882 1.1 christos .sect .text.foo_bar output_offset = 6 output_offset = 4
883 1.1 christos foo: rts
884 1.1 christos bar: rts
885 1.1 christos
886 1.1 christos If we process the reloc now, the jmp bar is replaced by a
887 1.1 christos relative branch to the initial bar address (output_offset 6). */
888 1.1 christos if (*again && sym_sec != sec
889 1.1 christos && sym_sec->output_section == sec->output_section)
890 1.1 christos {
891 1.1 christos prev_insn_group = 0;
892 1.1 christos prev_insn_branch = 0;
893 1.1 christos continue;
894 1.1 christos }
895 1.1 christos
896 1.1 christos value = symval;
897 1.1 christos /* Try to turn a far branch to a near branch. */
898 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
899 1.1 christos && prev_insn_branch)
900 1.1 christos {
901 1.1 christos bfd_vma offset;
902 1.1 christos unsigned char code;
903 1.1 christos
904 1.1 christos offset = value - (prev_insn_branch->r_offset
905 1.1 christos + sec->output_section->vma
906 1.1 christos + sec->output_offset + 2);
907 1.1 christos
908 1.1 christos /* If the offset is still out of -128..+127 range,
909 1.1 christos leave that far branch unchanged. */
910 1.1 christos if ((offset & 0xff80) != 0 && (offset & 0xff80) != 0xff80)
911 1.1 christos {
912 1.1 christos prev_insn_branch = 0;
913 1.1 christos continue;
914 1.1 christos }
915 1.1 christos
916 1.1 christos /* Shrink the branch. */
917 1.1 christos code = bfd_get_8 (abfd, contents + prev_insn_branch->r_offset);
918 1.1 christos if (code == 0x7e)
919 1.1 christos {
920 1.1 christos code = 0x20;
921 1.1 christos bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
922 1.1 christos bfd_put_8 (abfd, 0xff,
923 1.1 christos contents + prev_insn_branch->r_offset + 1);
924 1.1 christos irel->r_offset = prev_insn_branch->r_offset + 1;
925 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
926 1.1 christos R_M68HC11_PCREL_8);
927 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec,
928 1.1 christos irel->r_offset + 1, 1);
929 1.1 christos }
930 1.1 christos else
931 1.1 christos {
932 1.1 christos code ^= 0x1;
933 1.1 christos bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
934 1.1 christos bfd_put_8 (abfd, 0xff,
935 1.1 christos contents + prev_insn_branch->r_offset + 1);
936 1.1 christos irel->r_offset = prev_insn_branch->r_offset + 1;
937 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
938 1.1 christos R_M68HC11_PCREL_8);
939 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec,
940 1.1 christos irel->r_offset + 1, 3);
941 1.1 christos }
942 1.1 christos prev_insn_branch = 0;
943 1.1 christos *again = TRUE;
944 1.1 christos }
945 1.1 christos
946 1.1 christos /* Try to turn a 16 bit address into a 8 bit page0 address. */
947 1.1 christos else if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
948 1.1 christos && (value & 0xff00) == 0)
949 1.1 christos {
950 1.1 christos unsigned char code;
951 1.1 christos unsigned short offset;
952 1.1 christos struct m68hc11_direct_relax *rinfo;
953 1.1 christos
954 1.1 christos prev_insn_branch = 0;
955 1.1 christos offset = bfd_get_16 (abfd, contents + irel->r_offset);
956 1.1 christos offset += value;
957 1.1 christos if ((offset & 0xff00) != 0)
958 1.1 christos {
959 1.1 christos prev_insn_group = 0;
960 1.1 christos continue;
961 1.1 christos }
962 1.1 christos
963 1.1 christos if (prev_insn_group)
964 1.1 christos {
965 1.1 christos unsigned long old_sec_size = sec->size;
966 1.1 christos
967 1.1 christos /* Note that we've changed the relocation contents, etc. */
968 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
969 1.1 christos free_relocs = NULL;
970 1.1 christos
971 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
972 1.1 christos free_contents = NULL;
973 1.1 christos
974 1.1 christos symtab_hdr->contents = (bfd_byte *) isymbuf;
975 1.1 christos free_extsyms = NULL;
976 1.1 christos
977 1.1 christos m68hc11_relax_group (abfd, sec, contents, offset,
978 1.1 christos prev_insn_group->r_offset,
979 1.1 christos insn_group_value);
980 1.1 christos irel = prev_insn_group;
981 1.1 christos prev_insn_group = 0;
982 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
983 1.1 christos R_M68HC11_NONE);
984 1.1 christos if (sec->size != old_sec_size)
985 1.1 christos *again = TRUE;
986 1.1 christos continue;
987 1.1 christos }
988 1.1 christos
989 1.1 christos /* Get the opcode. */
990 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
991 1.1 christos rinfo = find_relaxable_insn (code);
992 1.1 christos if (rinfo == 0)
993 1.1 christos {
994 1.1 christos prev_insn_group = 0;
995 1.1 christos continue;
996 1.1 christos }
997 1.1 christos
998 1.1 christos /* Note that we've changed the relocation contents, etc. */
999 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
1000 1.1 christos free_relocs = NULL;
1001 1.1 christos
1002 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
1003 1.1 christos free_contents = NULL;
1004 1.1 christos
1005 1.1 christos symtab_hdr->contents = (bfd_byte *) isymbuf;
1006 1.1 christos free_extsyms = NULL;
1007 1.1 christos
1008 1.1 christos /* Fix the opcode. */
1009 1.1 christos /* printf ("A relaxable case : 0x%02x (%s)\n",
1010 1.1 christos code, rinfo->name); */
1011 1.1 christos bfd_put_8 (abfd, rinfo->direct_code,
1012 1.1 christos contents + irel->r_offset - 1);
1013 1.1 christos
1014 1.1 christos /* Delete one byte of data (upper byte of address). */
1015 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 1);
1016 1.1 christos
1017 1.1 christos /* Fix the relocation's type. */
1018 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1019 1.1 christos R_M68HC11_8);
1020 1.1 christos
1021 1.1 christos /* That will change things, so, we should relax again. */
1022 1.1 christos *again = TRUE;
1023 1.1 christos }
1024 1.1 christos else if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_16 && !is_far)
1025 1.1 christos {
1026 1.1 christos unsigned char code;
1027 1.1 christos bfd_vma offset;
1028 1.1 christos
1029 1.1 christos prev_insn_branch = 0;
1030 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1031 1.1 christos if (code == 0x7e || code == 0xbd)
1032 1.1 christos {
1033 1.1 christos offset = value - (irel->r_offset
1034 1.1 christos + sec->output_section->vma
1035 1.1 christos + sec->output_offset + 1);
1036 1.1 christos offset += bfd_get_16 (abfd, contents + irel->r_offset);
1037 1.1 christos
1038 1.1 christos /* If the offset is still out of -128..+127 range,
1039 1.1 christos leave that far branch unchanged. */
1040 1.1 christos if ((offset & 0xff80) == 0 || (offset & 0xff80) == 0xff80)
1041 1.1 christos {
1042 1.1 christos
1043 1.1 christos /* Note that we've changed the relocation contents, etc. */
1044 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
1045 1.1 christos free_relocs = NULL;
1046 1.1 christos
1047 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
1048 1.1 christos free_contents = NULL;
1049 1.1 christos
1050 1.1 christos symtab_hdr->contents = (bfd_byte *) isymbuf;
1051 1.1 christos free_extsyms = NULL;
1052 1.1 christos
1053 1.1 christos /* Shrink the branch. */
1054 1.1 christos code = (code == 0x7e) ? 0x20 : 0x8d;
1055 1.1 christos bfd_put_8 (abfd, code,
1056 1.1 christos contents + irel->r_offset - 1);
1057 1.1 christos bfd_put_8 (abfd, 0xff,
1058 1.1 christos contents + irel->r_offset);
1059 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1060 1.1 christos R_M68HC11_PCREL_8);
1061 1.1 christos m68hc11_elf_relax_delete_bytes (abfd, sec,
1062 1.1 christos irel->r_offset + 1, 1);
1063 1.1 christos /* That will change things, so, we should relax again. */
1064 1.1 christos *again = TRUE;
1065 1.1 christos }
1066 1.1 christos }
1067 1.1 christos }
1068 1.1 christos prev_insn_branch = 0;
1069 1.1 christos prev_insn_group = 0;
1070 1.1 christos }
1071 1.1 christos
1072 1.1 christos if (free_relocs != NULL)
1073 1.1 christos {
1074 1.1 christos free (free_relocs);
1075 1.1 christos free_relocs = NULL;
1076 1.1 christos }
1077 1.1 christos
1078 1.1 christos if (free_contents != NULL)
1079 1.1 christos {
1080 1.1 christos if (! link_info->keep_memory)
1081 1.1 christos free (free_contents);
1082 1.1 christos else
1083 1.1 christos {
1084 1.1 christos /* Cache the section contents for elf_link_input_bfd. */
1085 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
1086 1.1 christos }
1087 1.1 christos free_contents = NULL;
1088 1.1 christos }
1089 1.1 christos
1090 1.1 christos if (free_extsyms != NULL)
1091 1.1 christos {
1092 1.1 christos if (! link_info->keep_memory)
1093 1.1 christos free (free_extsyms);
1094 1.1 christos else
1095 1.1 christos {
1096 1.1 christos /* Cache the symbols for elf_link_input_bfd. */
1097 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
1098 1.1 christos }
1099 1.1 christos free_extsyms = NULL;
1100 1.1 christos }
1101 1.1 christos
1102 1.1 christos return TRUE;
1103 1.1 christos
1104 1.1 christos error_return:
1105 1.1 christos if (free_relocs != NULL)
1106 1.1 christos free (free_relocs);
1107 1.1 christos if (free_contents != NULL)
1108 1.1 christos free (free_contents);
1109 1.1 christos if (free_extsyms != NULL)
1110 1.1 christos free (free_extsyms);
1111 1.1 christos return FALSE;
1112 1.1 christos }
1113 1.1 christos
1114 1.1 christos /* Delete some bytes from a section while relaxing. */
1115 1.1 christos
1116 1.1 christos static void
1117 1.1 christos m68hc11_elf_relax_delete_bytes (bfd *abfd, asection *sec,
1118 1.1 christos bfd_vma addr, int count)
1119 1.1 christos {
1120 1.1 christos Elf_Internal_Shdr *symtab_hdr;
1121 1.1 christos unsigned int sec_shndx;
1122 1.1 christos bfd_byte *contents;
1123 1.1 christos Elf_Internal_Rela *irel, *irelend;
1124 1.1 christos bfd_vma toaddr;
1125 1.1 christos Elf_Internal_Sym *isymbuf, *isym, *isymend;
1126 1.1 christos struct elf_link_hash_entry **sym_hashes;
1127 1.1 christos struct elf_link_hash_entry **end_hashes;
1128 1.1 christos unsigned int symcount;
1129 1.1 christos
1130 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1131 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1132 1.1 christos
1133 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1134 1.1 christos
1135 1.1 christos contents = elf_section_data (sec)->this_hdr.contents;
1136 1.1 christos
1137 1.1 christos toaddr = sec->size;
1138 1.1 christos
1139 1.1 christos irel = elf_section_data (sec)->relocs;
1140 1.1 christos irelend = irel + sec->reloc_count;
1141 1.1 christos
1142 1.1 christos /* Actually delete the bytes. */
1143 1.1 christos memmove (contents + addr, contents + addr + count,
1144 1.1 christos (size_t) (toaddr - addr - count));
1145 1.1 christos
1146 1.1 christos sec->size -= count;
1147 1.1 christos
1148 1.1 christos /* Adjust all the relocs. */
1149 1.1 christos for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1150 1.1 christos {
1151 1.1 christos unsigned char code;
1152 1.1 christos unsigned char offset;
1153 1.1 christos unsigned short raddr;
1154 1.1 christos unsigned long old_offset;
1155 1.1 christos int branch_pos;
1156 1.1 christos
1157 1.1 christos old_offset = irel->r_offset;
1158 1.1 christos
1159 1.1 christos /* See if this reloc was for the bytes we have deleted, in which
1160 1.1 christos case we no longer care about it. Don't delete relocs which
1161 1.1 christos represent addresses, though. */
1162 1.1 christos if (ELF32_R_TYPE (irel->r_info) != R_M68HC11_RL_JUMP
1163 1.1 christos && irel->r_offset >= addr && irel->r_offset < addr + count)
1164 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1165 1.1 christos R_M68HC11_NONE);
1166 1.1 christos
1167 1.1 christos if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_NONE)
1168 1.1 christos continue;
1169 1.1 christos
1170 1.1 christos /* Get the new reloc address. */
1171 1.1 christos if ((irel->r_offset > addr
1172 1.1 christos && irel->r_offset < toaddr))
1173 1.1 christos irel->r_offset -= count;
1174 1.1 christos
1175 1.1 christos /* If this is a PC relative reloc, see if the range it covers
1176 1.1 christos includes the bytes we have deleted. */
1177 1.1 christos switch (ELF32_R_TYPE (irel->r_info))
1178 1.1 christos {
1179 1.1 christos default:
1180 1.1 christos break;
1181 1.1 christos
1182 1.1 christos case R_M68HC11_RL_JUMP:
1183 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset);
1184 1.1 christos switch (code)
1185 1.1 christos {
1186 1.1 christos /* jsr and jmp instruction are also marked with RL_JUMP
1187 1.1 christos relocs but no adjustment must be made. */
1188 1.1 christos case 0x7e:
1189 1.1 christos case 0x9d:
1190 1.1 christos case 0xbd:
1191 1.1 christos continue;
1192 1.1 christos
1193 1.1 christos case 0x12:
1194 1.1 christos case 0x13:
1195 1.1 christos branch_pos = 3;
1196 1.1 christos raddr = 4;
1197 1.1 christos
1198 1.1 christos /* Special case when we translate a brclr N,y into brclr *<addr>
1199 1.1 christos In this case, the 0x18 page2 prefix is removed.
1200 1.1 christos The reloc offset is not modified but the instruction
1201 1.1 christos size is reduced by 1. */
1202 1.1 christos if (old_offset == addr)
1203 1.1 christos raddr++;
1204 1.1 christos break;
1205 1.1 christos
1206 1.1 christos case 0x1e:
1207 1.1 christos case 0x1f:
1208 1.1 christos branch_pos = 3;
1209 1.1 christos raddr = 4;
1210 1.1 christos break;
1211 1.1 christos
1212 1.1 christos case 0x18:
1213 1.1 christos branch_pos = 4;
1214 1.1 christos raddr = 5;
1215 1.1 christos break;
1216 1.1 christos
1217 1.1 christos default:
1218 1.1 christos branch_pos = 1;
1219 1.1 christos raddr = 2;
1220 1.1 christos break;
1221 1.1 christos }
1222 1.1 christos offset = bfd_get_8 (abfd, contents + irel->r_offset + branch_pos);
1223 1.1 christos raddr += old_offset;
1224 1.1 christos raddr += ((unsigned short) offset | ((offset & 0x80) ? 0xff00 : 0));
1225 1.1 christos if (irel->r_offset < addr && raddr > addr)
1226 1.1 christos {
1227 1.1 christos offset -= count;
1228 1.1 christos bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
1229 1.1 christos }
1230 1.1 christos else if (irel->r_offset >= addr && raddr <= addr)
1231 1.1 christos {
1232 1.1 christos offset += count;
1233 1.1 christos bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
1234 1.1 christos }
1235 1.1 christos else
1236 1.1 christos {
1237 1.1 christos /*printf ("Not adjusted 0x%04x [0x%4x 0x%4x]\n", raddr,
1238 1.1 christos irel->r_offset, addr);*/
1239 1.1 christos }
1240 1.1 christos
1241 1.1 christos break;
1242 1.1 christos }
1243 1.1 christos }
1244 1.1 christos
1245 1.1 christos /* Adjust the local symbols defined in this section. */
1246 1.1 christos isymend = isymbuf + symtab_hdr->sh_info;
1247 1.1 christos for (isym = isymbuf; isym < isymend; isym++)
1248 1.1 christos {
1249 1.1 christos if (isym->st_shndx == sec_shndx
1250 1.1 christos && isym->st_value > addr
1251 1.1 christos && isym->st_value <= toaddr)
1252 1.1 christos isym->st_value -= count;
1253 1.1 christos }
1254 1.1 christos
1255 1.1 christos /* Now adjust the global symbols defined in this section. */
1256 1.1 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1257 1.1 christos - symtab_hdr->sh_info);
1258 1.1 christos sym_hashes = elf_sym_hashes (abfd);
1259 1.1 christos end_hashes = sym_hashes + symcount;
1260 1.1 christos for (; sym_hashes < end_hashes; sym_hashes++)
1261 1.1 christos {
1262 1.1 christos struct elf_link_hash_entry *sym_hash = *sym_hashes;
1263 1.1 christos if ((sym_hash->root.type == bfd_link_hash_defined
1264 1.1 christos || sym_hash->root.type == bfd_link_hash_defweak)
1265 1.1 christos && sym_hash->root.u.def.section == sec
1266 1.1 christos && sym_hash->root.u.def.value > addr
1267 1.1 christos && sym_hash->root.u.def.value <= toaddr)
1268 1.1 christos {
1269 1.1 christos sym_hash->root.u.def.value -= count;
1270 1.1 christos }
1271 1.1 christos }
1272 1.1 christos }
1273 1.1 christos
1274 1.1 christos /* Specific sections:
1275 1.1 christos - The .page0 is a data section that is mapped in [0x0000..0x00FF].
1276 1.1 christos Page0 accesses are faster on the M68HC11. Soft registers used by GCC-m6811
1277 1.1 christos are located in .page0.
1278 1.1 christos - The .vectors is the section that represents the interrupt
1279 1.1 christos vectors. */
1280 1.1 christos static const struct bfd_elf_special_section elf32_m68hc11_special_sections[] =
1281 1.1 christos {
1282 1.1 christos { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1283 1.1 christos { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1284 1.1 christos { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1285 1.1 christos { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS, SHF_ALLOC },
1286 1.1 christos { NULL, 0, 0, 0, 0 }
1287 1.1 christos };
1288 1.1 christos
1289 1.1 christos #define ELF_ARCH bfd_arch_m68hc11
1291 1.1 christos #define ELF_TARGET_ID M68HC11_ELF_DATA
1292 1.1 christos #define ELF_MACHINE_CODE EM_68HC11
1293 1.1 christos #define ELF_MAXPAGESIZE 0x1000
1294 1.1 christos
1295 1.1 christos #define TARGET_BIG_SYM bfd_elf32_m68hc11_vec
1296 1.1 christos #define TARGET_BIG_NAME "elf32-m68hc11"
1297 1.1 christos
1298 1.1 christos #define elf_info_to_howto 0
1299 1.1 christos #define elf_info_to_howto_rel m68hc11_info_to_howto_rel
1300 1.1 christos #define bfd_elf32_bfd_relax_section m68hc11_elf_relax_section
1301 1.1 christos #define elf_backend_check_relocs elf32_m68hc11_check_relocs
1302 1.1 christos #define elf_backend_relocate_section elf32_m68hc11_relocate_section
1303 1.1 christos #define elf_backend_add_symbol_hook elf32_m68hc11_add_symbol_hook
1304 1.1 christos #define elf_backend_object_p 0
1305 1.1 christos #define elf_backend_final_write_processing 0
1306 1.1 christos #define elf_backend_can_gc_sections 1
1307 1.1 christos #define elf_backend_special_sections elf32_m68hc11_special_sections
1308 1.1 christos #define elf_backend_merge_symbol_attribute elf32_m68hc11_merge_symbol_attribute
1309 1.1 christos
1310 1.1 christos #define bfd_elf32_bfd_link_hash_table_create \
1311 1.1 christos m68hc11_elf_bfd_link_hash_table_create
1312 1.1 christos #define bfd_elf32_bfd_link_hash_table_free \
1313 1.1 christos m68hc11_elf_bfd_link_hash_table_free
1314 1.1 christos #define bfd_elf32_bfd_merge_private_bfd_data \
1315 1.1 christos _bfd_m68hc11_elf_merge_private_bfd_data
1316 1.1 christos #define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags
1317 1.1 christos #define bfd_elf32_bfd_print_private_bfd_data \
1318 _bfd_m68hc11_elf_print_private_bfd_data
1319
1320 #include "elf32-target.h"
1321