Commit | Line | Data |
---|---|---|
99c513f6 DD |
1 | /* Renesas RL78 specific support for 32-bit ELF. |
2 | Copyright (C) 2011 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "sysdep.h" | |
22 | #include "bfd.h" | |
23 | #include "bfd_stdint.h" | |
24 | #include "libbfd.h" | |
25 | #include "elf-bfd.h" | |
26 | #include "elf/rl78.h" | |
27 | #include "libiberty.h" | |
28 | ||
29 | #define valid_16bit_address(v) ((v) <= 0x0ffff || (v) >= 0xf0000) | |
30 | ||
31 | #define RL78REL(n,sz,bit,shift,complain,pcrel) \ | |
32 | HOWTO (R_RL78_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \ | |
33 | bfd_elf_generic_reloc, "R_RL78_" #n, FALSE, 0, ~0, FALSE) | |
34 | ||
35 | /* Note that the relocations around 0x7f are internal to this file; | |
36 | feel free to move them as needed to avoid conflicts with published | |
37 | relocation numbers. */ | |
38 | ||
39 | static reloc_howto_type rl78_elf_howto_table [] = | |
40 | { | |
41 | RL78REL (NONE, 0, 0, 0, dont, FALSE), | |
42 | RL78REL (DIR32, 2, 32, 0, signed, FALSE), | |
43 | RL78REL (DIR24S, 2, 24, 0, signed, FALSE), | |
44 | RL78REL (DIR16, 1, 16, 0, dont, FALSE), | |
45 | RL78REL (DIR16U, 1, 16, 0, unsigned, FALSE), | |
46 | RL78REL (DIR16S, 1, 16, 0, signed, FALSE), | |
47 | RL78REL (DIR8, 0, 8, 0, dont, FALSE), | |
48 | RL78REL (DIR8U, 0, 8, 0, unsigned, FALSE), | |
49 | RL78REL (DIR8S, 0, 8, 0, signed, FALSE), | |
50 | RL78REL (DIR24S_PCREL, 2, 24, 0, signed, TRUE), | |
51 | RL78REL (DIR16S_PCREL, 1, 16, 0, signed, TRUE), | |
52 | RL78REL (DIR8S_PCREL, 0, 8, 0, signed, TRUE), | |
53 | RL78REL (DIR16UL, 1, 16, 2, unsigned, FALSE), | |
54 | RL78REL (DIR16UW, 1, 16, 1, unsigned, FALSE), | |
55 | RL78REL (DIR8UL, 0, 8, 2, unsigned, FALSE), | |
56 | RL78REL (DIR8UW, 0, 8, 1, unsigned, FALSE), | |
57 | RL78REL (DIR32_REV, 1, 16, 0, dont, FALSE), | |
58 | RL78REL (DIR16_REV, 1, 16, 0, dont, FALSE), | |
59 | RL78REL (DIR3U_PCREL, 0, 3, 0, dont, TRUE), | |
60 | ||
61 | EMPTY_HOWTO (0x13), | |
62 | EMPTY_HOWTO (0x14), | |
63 | EMPTY_HOWTO (0x15), | |
64 | EMPTY_HOWTO (0x16), | |
65 | EMPTY_HOWTO (0x17), | |
66 | EMPTY_HOWTO (0x18), | |
67 | EMPTY_HOWTO (0x19), | |
68 | EMPTY_HOWTO (0x1a), | |
69 | EMPTY_HOWTO (0x1b), | |
70 | EMPTY_HOWTO (0x1c), | |
71 | EMPTY_HOWTO (0x1d), | |
72 | EMPTY_HOWTO (0x1e), | |
73 | EMPTY_HOWTO (0x1f), | |
74 | ||
75 | EMPTY_HOWTO (0x20), | |
76 | EMPTY_HOWTO (0x21), | |
77 | EMPTY_HOWTO (0x22), | |
78 | EMPTY_HOWTO (0x23), | |
79 | EMPTY_HOWTO (0x24), | |
80 | EMPTY_HOWTO (0x25), | |
81 | EMPTY_HOWTO (0x26), | |
82 | EMPTY_HOWTO (0x27), | |
83 | EMPTY_HOWTO (0x28), | |
84 | EMPTY_HOWTO (0x29), | |
85 | EMPTY_HOWTO (0x2a), | |
86 | EMPTY_HOWTO (0x2b), | |
87 | EMPTY_HOWTO (0x2c), | |
9cea966c | 88 | RL78REL (RH_RELAX, 0, 0, 0, dont, FALSE), |
99c513f6 DD |
89 | |
90 | EMPTY_HOWTO (0x2e), | |
91 | EMPTY_HOWTO (0x2f), | |
92 | EMPTY_HOWTO (0x30), | |
93 | EMPTY_HOWTO (0x31), | |
94 | EMPTY_HOWTO (0x32), | |
95 | EMPTY_HOWTO (0x33), | |
96 | EMPTY_HOWTO (0x34), | |
97 | EMPTY_HOWTO (0x35), | |
98 | EMPTY_HOWTO (0x36), | |
99 | EMPTY_HOWTO (0x37), | |
100 | EMPTY_HOWTO (0x38), | |
101 | EMPTY_HOWTO (0x39), | |
102 | EMPTY_HOWTO (0x3a), | |
103 | EMPTY_HOWTO (0x3b), | |
104 | EMPTY_HOWTO (0x3c), | |
105 | EMPTY_HOWTO (0x3d), | |
106 | EMPTY_HOWTO (0x3e), | |
107 | EMPTY_HOWTO (0x3f), | |
108 | EMPTY_HOWTO (0x40), | |
109 | ||
110 | RL78REL (ABS32, 2, 32, 0, dont, FALSE), | |
111 | RL78REL (ABS24S, 2, 24, 0, signed, FALSE), | |
112 | RL78REL (ABS16, 1, 16, 0, dont, FALSE), | |
113 | RL78REL (ABS16U, 1, 16, 0, unsigned, FALSE), | |
114 | RL78REL (ABS16S, 1, 16, 0, signed, FALSE), | |
115 | RL78REL (ABS8, 0, 8, 0, dont, FALSE), | |
116 | RL78REL (ABS8U, 0, 8, 0, unsigned, FALSE), | |
117 | RL78REL (ABS8S, 0, 8, 0, signed, FALSE), | |
118 | RL78REL (ABS24S_PCREL, 2, 24, 0, signed, TRUE), | |
119 | RL78REL (ABS16S_PCREL, 1, 16, 0, signed, TRUE), | |
120 | RL78REL (ABS8S_PCREL, 0, 8, 0, signed, TRUE), | |
121 | RL78REL (ABS16UL, 1, 16, 0, unsigned, FALSE), | |
122 | RL78REL (ABS16UW, 1, 16, 0, unsigned, FALSE), | |
123 | RL78REL (ABS8UL, 0, 8, 0, unsigned, FALSE), | |
124 | RL78REL (ABS8UW, 0, 8, 0, unsigned, FALSE), | |
125 | RL78REL (ABS32_REV, 2, 32, 0, dont, FALSE), | |
126 | RL78REL (ABS16_REV, 1, 16, 0, dont, FALSE), | |
127 | ||
128 | #define STACK_REL_P(x) ((x) <= R_RL78_ABS16_REV && (x) >= R_RL78_ABS32) | |
129 | ||
130 | EMPTY_HOWTO (0x52), | |
131 | EMPTY_HOWTO (0x53), | |
132 | EMPTY_HOWTO (0x54), | |
133 | EMPTY_HOWTO (0x55), | |
134 | EMPTY_HOWTO (0x56), | |
135 | EMPTY_HOWTO (0x57), | |
136 | EMPTY_HOWTO (0x58), | |
137 | EMPTY_HOWTO (0x59), | |
138 | EMPTY_HOWTO (0x5a), | |
139 | EMPTY_HOWTO (0x5b), | |
140 | EMPTY_HOWTO (0x5c), | |
141 | EMPTY_HOWTO (0x5d), | |
142 | EMPTY_HOWTO (0x5e), | |
143 | EMPTY_HOWTO (0x5f), | |
144 | EMPTY_HOWTO (0x60), | |
145 | EMPTY_HOWTO (0x61), | |
146 | EMPTY_HOWTO (0x62), | |
147 | EMPTY_HOWTO (0x63), | |
148 | EMPTY_HOWTO (0x64), | |
149 | EMPTY_HOWTO (0x65), | |
150 | EMPTY_HOWTO (0x66), | |
151 | EMPTY_HOWTO (0x67), | |
152 | EMPTY_HOWTO (0x68), | |
153 | EMPTY_HOWTO (0x69), | |
154 | EMPTY_HOWTO (0x6a), | |
155 | EMPTY_HOWTO (0x6b), | |
156 | EMPTY_HOWTO (0x6c), | |
157 | EMPTY_HOWTO (0x6d), | |
158 | EMPTY_HOWTO (0x6e), | |
159 | EMPTY_HOWTO (0x6f), | |
160 | EMPTY_HOWTO (0x70), | |
161 | EMPTY_HOWTO (0x71), | |
162 | EMPTY_HOWTO (0x72), | |
163 | EMPTY_HOWTO (0x73), | |
164 | EMPTY_HOWTO (0x74), | |
165 | EMPTY_HOWTO (0x75), | |
166 | EMPTY_HOWTO (0x76), | |
167 | EMPTY_HOWTO (0x77), | |
168 | ||
169 | EMPTY_HOWTO (0x78), | |
170 | EMPTY_HOWTO (0x79), | |
171 | EMPTY_HOWTO (0x7a), | |
172 | EMPTY_HOWTO (0x7b), | |
173 | EMPTY_HOWTO (0x7c), | |
174 | EMPTY_HOWTO (0x7d), | |
175 | EMPTY_HOWTO (0x7e), | |
176 | EMPTY_HOWTO (0x7f), | |
177 | ||
178 | RL78REL (SYM, 2, 32, 0, dont, FALSE), | |
179 | RL78REL (OPneg, 2, 32, 0, dont, FALSE), | |
180 | RL78REL (OPadd, 2, 32, 0, dont, FALSE), | |
181 | RL78REL (OPsub, 2, 32, 0, dont, FALSE), | |
182 | RL78REL (OPmul, 2, 32, 0, dont, FALSE), | |
183 | RL78REL (OPdiv, 2, 32, 0, dont, FALSE), | |
184 | RL78REL (OPshla, 2, 32, 0, dont, FALSE), | |
185 | RL78REL (OPshra, 2, 32, 0, dont, FALSE), | |
186 | RL78REL (OPsctsize, 2, 32, 0, dont, FALSE), | |
187 | EMPTY_HOWTO (0x89), | |
188 | EMPTY_HOWTO (0x8a), | |
189 | EMPTY_HOWTO (0x8b), | |
190 | EMPTY_HOWTO (0x8c), | |
191 | RL78REL (OPscttop, 2, 32, 0, dont, FALSE), | |
192 | EMPTY_HOWTO (0x8e), | |
193 | EMPTY_HOWTO (0x8f), | |
194 | RL78REL (OPand, 2, 32, 0, dont, FALSE), | |
195 | RL78REL (OPor, 2, 32, 0, dont, FALSE), | |
196 | RL78REL (OPxor, 2, 32, 0, dont, FALSE), | |
197 | RL78REL (OPnot, 2, 32, 0, dont, FALSE), | |
198 | RL78REL (OPmod, 2, 32, 0, dont, FALSE), | |
199 | RL78REL (OPromtop, 2, 32, 0, dont, FALSE), | |
200 | RL78REL (OPramtop, 2, 32, 0, dont, FALSE) | |
201 | }; | |
202 | \f | |
203 | /* Map BFD reloc types to RL78 ELF reloc types. */ | |
204 | ||
205 | struct rl78_reloc_map | |
206 | { | |
207 | bfd_reloc_code_real_type bfd_reloc_val; | |
208 | unsigned int rl78_reloc_val; | |
209 | }; | |
210 | ||
211 | static const struct rl78_reloc_map rl78_reloc_map [] = | |
212 | { | |
213 | { BFD_RELOC_NONE, R_RL78_NONE }, | |
214 | { BFD_RELOC_8, R_RL78_DIR8S }, | |
215 | { BFD_RELOC_16, R_RL78_DIR16S }, | |
216 | { BFD_RELOC_24, R_RL78_DIR24S }, | |
217 | { BFD_RELOC_32, R_RL78_DIR32 }, | |
218 | { BFD_RELOC_RL78_16_OP, R_RL78_DIR16 }, | |
219 | { BFD_RELOC_RL78_DIR3U_PCREL, R_RL78_DIR3U_PCREL }, | |
220 | { BFD_RELOC_8_PCREL, R_RL78_DIR8S_PCREL }, | |
221 | { BFD_RELOC_16_PCREL, R_RL78_DIR16S_PCREL }, | |
222 | { BFD_RELOC_24_PCREL, R_RL78_DIR24S_PCREL }, | |
223 | { BFD_RELOC_RL78_8U, R_RL78_DIR8U }, | |
224 | { BFD_RELOC_RL78_16U, R_RL78_DIR16U }, | |
225 | { BFD_RELOC_RL78_SYM, R_RL78_SYM }, | |
226 | { BFD_RELOC_RL78_OP_SUBTRACT, R_RL78_OPsub }, | |
227 | { BFD_RELOC_RL78_OP_NEG, R_RL78_OPneg }, | |
228 | { BFD_RELOC_RL78_OP_AND, R_RL78_OPand }, | |
229 | { BFD_RELOC_RL78_OP_SHRA, R_RL78_OPshra }, | |
230 | { BFD_RELOC_RL78_ABS8, R_RL78_ABS8 }, | |
231 | { BFD_RELOC_RL78_ABS16, R_RL78_ABS16 }, | |
232 | { BFD_RELOC_RL78_ABS16_REV, R_RL78_ABS16_REV }, | |
233 | { BFD_RELOC_RL78_ABS32, R_RL78_ABS32 }, | |
234 | { BFD_RELOC_RL78_ABS32_REV, R_RL78_ABS32_REV }, | |
235 | { BFD_RELOC_RL78_ABS16UL, R_RL78_ABS16UL }, | |
236 | { BFD_RELOC_RL78_ABS16UW, R_RL78_ABS16UW }, | |
9cea966c DD |
237 | { BFD_RELOC_RL78_ABS16U, R_RL78_ABS16U }, |
238 | { BFD_RELOC_RL78_RELAX, R_RL78_RH_RELAX } | |
99c513f6 DD |
239 | }; |
240 | ||
241 | static reloc_howto_type * | |
242 | rl78_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED, | |
243 | bfd_reloc_code_real_type code) | |
244 | { | |
245 | unsigned int i; | |
246 | ||
247 | if (code == BFD_RELOC_RL78_32_OP) | |
248 | return rl78_elf_howto_table + R_RL78_DIR32; | |
249 | ||
250 | for (i = ARRAY_SIZE (rl78_reloc_map); --i;) | |
251 | if (rl78_reloc_map [i].bfd_reloc_val == code) | |
252 | return rl78_elf_howto_table + rl78_reloc_map[i].rl78_reloc_val; | |
253 | ||
254 | return NULL; | |
255 | } | |
256 | ||
257 | static reloc_howto_type * | |
258 | rl78_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name) | |
259 | { | |
260 | unsigned int i; | |
261 | ||
262 | for (i = 0; i < ARRAY_SIZE (rl78_elf_howto_table); i++) | |
263 | if (rl78_elf_howto_table[i].name != NULL | |
264 | && strcasecmp (rl78_elf_howto_table[i].name, r_name) == 0) | |
265 | return rl78_elf_howto_table + i; | |
266 | ||
267 | return NULL; | |
268 | } | |
269 | ||
270 | /* Set the howto pointer for an RL78 ELF reloc. */ | |
271 | ||
272 | static void | |
273 | rl78_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED, | |
274 | arelent * cache_ptr, | |
275 | Elf_Internal_Rela * dst) | |
276 | { | |
277 | unsigned int r_type; | |
278 | ||
279 | r_type = ELF32_R_TYPE (dst->r_info); | |
280 | BFD_ASSERT (r_type < (unsigned int) R_RL78_max); | |
281 | cache_ptr->howto = rl78_elf_howto_table + r_type; | |
282 | } | |
283 | \f | |
284 | static bfd_vma | |
285 | get_symbol_value (const char * name, | |
286 | bfd_reloc_status_type * status, | |
287 | struct bfd_link_info * info, | |
288 | bfd * input_bfd, | |
289 | asection * input_section, | |
290 | int offset) | |
291 | { | |
292 | bfd_vma value = 0; | |
293 | struct bfd_link_hash_entry * h; | |
294 | ||
295 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); | |
296 | ||
297 | if (h == NULL | |
298 | || (h->type != bfd_link_hash_defined | |
299 | && h->type != bfd_link_hash_defweak)) | |
300 | * status = info->callbacks->undefined_symbol | |
301 | (info, name, input_bfd, input_section, offset, TRUE); | |
302 | else | |
303 | value = (h->u.def.value | |
304 | + h->u.def.section->output_section->vma | |
305 | + h->u.def.section->output_offset); | |
306 | ||
307 | return value; | |
308 | } | |
309 | ||
310 | static bfd_vma | |
311 | get_romstart (bfd_reloc_status_type * status, | |
312 | struct bfd_link_info * info, | |
313 | bfd * abfd, | |
314 | asection * sec, | |
315 | int offset) | |
316 | { | |
317 | static bfd_boolean cached = FALSE; | |
318 | static bfd_vma cached_value = 0; | |
319 | ||
320 | if (!cached) | |
321 | { | |
322 | cached_value = get_symbol_value ("_start", status, info, abfd, sec, offset); | |
323 | cached = TRUE; | |
324 | } | |
325 | return cached_value; | |
326 | } | |
327 | ||
328 | static bfd_vma | |
329 | get_ramstart (bfd_reloc_status_type * status, | |
330 | struct bfd_link_info * info, | |
331 | bfd * abfd, | |
332 | asection * sec, | |
333 | int offset) | |
334 | { | |
335 | static bfd_boolean cached = FALSE; | |
336 | static bfd_vma cached_value = 0; | |
337 | ||
338 | if (!cached) | |
339 | { | |
340 | cached_value = get_symbol_value ("__datastart", status, info, abfd, sec, offset); | |
341 | cached = TRUE; | |
342 | } | |
343 | return cached_value; | |
344 | } | |
345 | ||
346 | #define NUM_STACK_ENTRIES 16 | |
347 | static int32_t rl78_stack [ NUM_STACK_ENTRIES ]; | |
348 | static unsigned int rl78_stack_top; | |
349 | ||
350 | #define RL78_STACK_PUSH(val) \ | |
351 | do \ | |
352 | { \ | |
353 | if (rl78_stack_top < NUM_STACK_ENTRIES) \ | |
354 | rl78_stack [rl78_stack_top ++] = (val); \ | |
355 | else \ | |
356 | r = bfd_reloc_dangerous; \ | |
357 | } \ | |
358 | while (0) | |
359 | ||
360 | #define RL78_STACK_POP(dest) \ | |
361 | do \ | |
362 | { \ | |
363 | if (rl78_stack_top > 0) \ | |
364 | (dest) = rl78_stack [-- rl78_stack_top]; \ | |
365 | else \ | |
366 | (dest) = 0, r = bfd_reloc_dangerous; \ | |
367 | } \ | |
368 | while (0) | |
369 | ||
370 | /* Relocate an RL78 ELF section. | |
371 | There is some attempt to make this function usable for many architectures, | |
372 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], | |
373 | if only to serve as a learning tool. | |
374 | ||
375 | The RELOCATE_SECTION function is called by the new ELF backend linker | |
376 | to handle the relocations for a section. | |
377 | ||
378 | The relocs are always passed as Rela structures; if the section | |
379 | actually uses Rel structures, the r_addend field will always be | |
380 | zero. | |
381 | ||
382 | This function is responsible for adjusting the section contents as | |
383 | necessary, and (if using Rela relocs and generating a relocatable | |
384 | output file) adjusting the reloc addend as necessary. | |
385 | ||
386 | This function does not have to worry about setting the reloc | |
387 | address or the reloc symbol index. | |
388 | ||
389 | LOCAL_SYMS is a pointer to the swapped in local symbols. | |
390 | ||
391 | LOCAL_SECTIONS is an array giving the section in the input file | |
392 | corresponding to the st_shndx field of each local symbol. | |
393 | ||
394 | The global hash table entry for the global symbols can be found | |
395 | via elf_sym_hashes (input_bfd). | |
396 | ||
397 | When generating relocatable output, this function must handle | |
398 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is | |
399 | going to be the section symbol corresponding to the output | |
400 | section, which means that the addend must be adjusted | |
401 | accordingly. */ | |
402 | ||
403 | static bfd_boolean | |
404 | rl78_elf_relocate_section | |
405 | (bfd * output_bfd, | |
406 | struct bfd_link_info * info, | |
407 | bfd * input_bfd, | |
408 | asection * input_section, | |
409 | bfd_byte * contents, | |
410 | Elf_Internal_Rela * relocs, | |
411 | Elf_Internal_Sym * local_syms, | |
412 | asection ** local_sections) | |
413 | { | |
414 | Elf_Internal_Shdr * symtab_hdr; | |
415 | struct elf_link_hash_entry ** sym_hashes; | |
416 | Elf_Internal_Rela * rel; | |
417 | Elf_Internal_Rela * relend; | |
418 | bfd *dynobj; | |
419 | asection *splt; | |
420 | ||
421 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
422 | sym_hashes = elf_sym_hashes (input_bfd); | |
423 | relend = relocs + input_section->reloc_count; | |
424 | ||
425 | dynobj = elf_hash_table (info)->dynobj; | |
426 | splt = NULL; | |
427 | if (dynobj != NULL) | |
428 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
429 | ||
430 | for (rel = relocs; rel < relend; rel ++) | |
431 | { | |
432 | reloc_howto_type * howto; | |
433 | unsigned long r_symndx; | |
434 | Elf_Internal_Sym * sym; | |
435 | asection * sec; | |
436 | struct elf_link_hash_entry * h; | |
437 | bfd_vma relocation; | |
438 | bfd_reloc_status_type r; | |
439 | const char * name = NULL; | |
440 | bfd_boolean unresolved_reloc = TRUE; | |
441 | int r_type; | |
442 | ||
443 | r_type = ELF32_R_TYPE (rel->r_info); | |
444 | r_symndx = ELF32_R_SYM (rel->r_info); | |
445 | ||
446 | howto = rl78_elf_howto_table + ELF32_R_TYPE (rel->r_info); | |
447 | h = NULL; | |
448 | sym = NULL; | |
449 | sec = NULL; | |
450 | relocation = 0; | |
451 | ||
452 | if (r_symndx < symtab_hdr->sh_info) | |
453 | { | |
454 | sym = local_syms + r_symndx; | |
455 | sec = local_sections [r_symndx]; | |
456 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel); | |
457 | ||
458 | name = bfd_elf_string_from_elf_section | |
459 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
460 | name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name; | |
461 | } | |
462 | else | |
463 | { | |
464 | bfd_boolean warned; | |
465 | ||
466 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, | |
467 | r_symndx, symtab_hdr, sym_hashes, h, | |
468 | sec, relocation, unresolved_reloc, | |
469 | warned); | |
470 | ||
471 | name = h->root.root.string; | |
472 | } | |
473 | ||
474 | if (sec != NULL && elf_discarded_section (sec)) | |
475 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, | |
476 | rel, relend, howto, contents); | |
477 | ||
478 | if (info->relocatable) | |
479 | { | |
480 | /* This is a relocatable link. We don't have to change | |
481 | anything, unless the reloc is against a section symbol, | |
482 | in which case we have to adjust according to where the | |
483 | section symbol winds up in the output section. */ | |
484 | if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
485 | rel->r_addend += sec->output_offset; | |
486 | continue; | |
487 | } | |
488 | ||
489 | switch (ELF32_R_TYPE (rel->r_info)) | |
490 | { | |
491 | case R_RL78_DIR16S: | |
492 | { | |
493 | bfd_vma *plt_offset; | |
494 | ||
495 | if (h != NULL) | |
496 | plt_offset = &h->plt.offset; | |
497 | else | |
498 | plt_offset = elf_local_got_offsets (input_bfd) + r_symndx; | |
499 | ||
500 | /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)", | |
501 | relocation, *plt_offset);*/ | |
9cea966c | 502 | if (! valid_16bit_address (relocation)) |
99c513f6 | 503 | { |
99c513f6 DD |
504 | /* If this is the first time we've processed this symbol, |
505 | fill in the plt entry with the correct symbol address. */ | |
506 | if ((*plt_offset & 1) == 0) | |
507 | { | |
508 | unsigned int x; | |
509 | ||
510 | x = 0x000000ec; /* br !!abs24 */ | |
511 | x |= (relocation << 8) & 0xffffff00; | |
512 | bfd_put_32 (input_bfd, x, splt->contents + *plt_offset); | |
513 | *plt_offset |= 1; | |
514 | } | |
515 | ||
516 | relocation = (splt->output_section->vma | |
517 | + splt->output_offset | |
518 | + (*plt_offset & -2)); | |
519 | if (name) | |
520 | { | |
521 | char *newname = bfd_malloc (strlen(name)+5); | |
522 | strcpy (newname, name); | |
523 | strcat(newname, ".plt"); | |
524 | _bfd_generic_link_add_one_symbol (info, | |
525 | input_bfd, | |
526 | newname, | |
527 | BSF_FUNCTION | BSF_WEAK, | |
528 | splt, | |
529 | (*plt_offset & -2), | |
530 | 0, | |
531 | 1, | |
532 | 0, | |
533 | 0); | |
534 | } | |
535 | } | |
536 | } | |
537 | break; | |
538 | } | |
539 | ||
540 | if (h != NULL && h->root.type == bfd_link_hash_undefweak) | |
541 | /* If the symbol is undefined and weak | |
542 | then the relocation resolves to zero. */ | |
543 | relocation = 0; | |
544 | else | |
545 | { | |
546 | if (howto->pc_relative) | |
547 | { | |
548 | relocation -= (input_section->output_section->vma | |
549 | + input_section->output_offset | |
550 | + rel->r_offset); | |
551 | relocation -= bfd_get_reloc_size (howto); | |
552 | } | |
553 | ||
554 | relocation += rel->r_addend; | |
555 | } | |
556 | ||
557 | r = bfd_reloc_ok; | |
558 | ||
559 | #define RANGE(a,b) if (a > (long) relocation || (long) relocation > b) r = bfd_reloc_overflow | |
560 | #define ALIGN(m) if (relocation & m) r = bfd_reloc_other; | |
561 | #define OP(i) (contents[rel->r_offset + (i)]) | |
562 | ||
563 | /* Opcode relocs are always big endian. Data relocs are bi-endian. */ | |
564 | switch (r_type) | |
565 | { | |
566 | case R_RL78_NONE: | |
567 | break; | |
568 | ||
9cea966c DD |
569 | case R_RL78_RH_RELAX: |
570 | break; | |
571 | ||
99c513f6 DD |
572 | case R_RL78_DIR8S_PCREL: |
573 | RANGE (-128, 127); | |
574 | OP (0) = relocation; | |
575 | break; | |
576 | ||
577 | case R_RL78_DIR8S: | |
578 | RANGE (-128, 255); | |
579 | OP (0) = relocation; | |
580 | break; | |
581 | ||
582 | case R_RL78_DIR8U: | |
583 | RANGE (0, 255); | |
584 | OP (0) = relocation; | |
585 | break; | |
586 | ||
587 | case R_RL78_DIR16S_PCREL: | |
588 | RANGE (-32768, 32767); | |
589 | OP (0) = relocation; | |
590 | OP (1) = relocation >> 8; | |
591 | break; | |
592 | ||
593 | case R_RL78_DIR16S: | |
594 | if ((relocation & 0xf0000) == 0xf0000) | |
595 | relocation &= 0xffff; | |
596 | RANGE (-32768, 65535); | |
597 | OP (0) = relocation; | |
598 | OP (1) = relocation >> 8; | |
599 | break; | |
600 | ||
601 | case R_RL78_DIR16U: | |
602 | RANGE (0, 65536); | |
603 | OP (0) = relocation; | |
604 | OP (1) = relocation >> 8; | |
605 | break; | |
606 | ||
607 | case R_RL78_DIR16: | |
608 | RANGE (-32768, 65536); | |
609 | OP (0) = relocation; | |
610 | OP (1) = relocation >> 8; | |
611 | break; | |
612 | ||
613 | case R_RL78_DIR16_REV: | |
614 | RANGE (-32768, 65536); | |
615 | OP (1) = relocation; | |
616 | OP (0) = relocation >> 8; | |
617 | break; | |
618 | ||
619 | case R_RL78_DIR3U_PCREL: | |
620 | RANGE (3, 10); | |
621 | OP (0) &= 0xf8; | |
622 | OP (0) |= relocation & 0x07; | |
623 | break; | |
624 | ||
625 | case R_RL78_DIR24S_PCREL: | |
626 | RANGE (-0x800000, 0x7fffff); | |
627 | OP (0) = relocation; | |
628 | OP (1) = relocation >> 8; | |
629 | OP (2) = relocation >> 16; | |
630 | break; | |
631 | ||
632 | case R_RL78_DIR24S: | |
633 | RANGE (-0x800000, 0x7fffff); | |
634 | OP (0) = relocation; | |
635 | OP (1) = relocation >> 8; | |
636 | OP (2) = relocation >> 16; | |
637 | break; | |
638 | ||
639 | case R_RL78_DIR32: | |
640 | OP (0) = relocation; | |
641 | OP (1) = relocation >> 8; | |
642 | OP (2) = relocation >> 16; | |
643 | OP (3) = relocation >> 24; | |
644 | break; | |
645 | ||
646 | case R_RL78_DIR32_REV: | |
647 | OP (3) = relocation; | |
648 | OP (2) = relocation >> 8; | |
649 | OP (1) = relocation >> 16; | |
650 | OP (0) = relocation >> 24; | |
651 | break; | |
652 | ||
9cea966c | 653 | case R_RL78_RH_SFR: |
23503fba | 654 | printf("SFR 0x%" BFD_VMA_FMT "x\n", relocation); |
9cea966c DD |
655 | RANGE (0xfff00, 0xfffff); |
656 | OP (0) = relocation & 0xff; | |
657 | break; | |
658 | ||
659 | case R_RL78_RH_SADDR: | |
23503fba | 660 | printf("SADDR 0x%" BFD_VMA_FMT "x\n", relocation); |
9cea966c DD |
661 | RANGE (0xffe20, 0xfff1f); |
662 | OP (0) = relocation & 0xff; | |
663 | printf(" - in\n"); | |
664 | break; | |
665 | ||
99c513f6 DD |
666 | /* Complex reloc handling: */ |
667 | ||
668 | case R_RL78_ABS32: | |
669 | RL78_STACK_POP (relocation); | |
670 | OP (0) = relocation; | |
671 | OP (1) = relocation >> 8; | |
672 | OP (2) = relocation >> 16; | |
673 | OP (3) = relocation >> 24; | |
674 | break; | |
675 | ||
676 | case R_RL78_ABS32_REV: | |
677 | RL78_STACK_POP (relocation); | |
678 | OP (3) = relocation; | |
679 | OP (2) = relocation >> 8; | |
680 | OP (1) = relocation >> 16; | |
681 | OP (0) = relocation >> 24; | |
682 | break; | |
683 | ||
684 | case R_RL78_ABS24S_PCREL: | |
685 | case R_RL78_ABS24S: | |
686 | RL78_STACK_POP (relocation); | |
687 | RANGE (-0x800000, 0x7fffff); | |
688 | OP (0) = relocation; | |
689 | OP (1) = relocation >> 8; | |
690 | OP (2) = relocation >> 16; | |
691 | break; | |
692 | ||
693 | case R_RL78_ABS16: | |
694 | RL78_STACK_POP (relocation); | |
695 | RANGE (-32768, 65535); | |
696 | OP (0) = relocation; | |
697 | OP (1) = relocation >> 8; | |
698 | break; | |
699 | ||
700 | case R_RL78_ABS16_REV: | |
701 | RL78_STACK_POP (relocation); | |
702 | RANGE (-32768, 65535); | |
703 | OP (1) = relocation; | |
704 | OP (0) = relocation >> 8; | |
705 | break; | |
706 | ||
707 | case R_RL78_ABS16S_PCREL: | |
708 | case R_RL78_ABS16S: | |
709 | RL78_STACK_POP (relocation); | |
710 | RANGE (-32768, 32767); | |
711 | OP (0) = relocation; | |
712 | OP (1) = relocation >> 8; | |
713 | break; | |
714 | ||
715 | case R_RL78_ABS16U: | |
716 | RL78_STACK_POP (relocation); | |
717 | RANGE (0, 65536); | |
718 | OP (0) = relocation; | |
719 | OP (1) = relocation >> 8; | |
720 | break; | |
721 | ||
722 | case R_RL78_ABS16UL: | |
723 | RL78_STACK_POP (relocation); | |
724 | relocation >>= 2; | |
725 | RANGE (0, 65536); | |
726 | OP (0) = relocation; | |
727 | OP (1) = relocation >> 8; | |
728 | break; | |
729 | ||
730 | case R_RL78_ABS16UW: | |
731 | RL78_STACK_POP (relocation); | |
732 | relocation >>= 1; | |
733 | RANGE (0, 65536); | |
734 | OP (0) = relocation; | |
735 | OP (1) = relocation >> 8; | |
736 | break; | |
737 | ||
738 | case R_RL78_ABS8: | |
739 | RL78_STACK_POP (relocation); | |
740 | RANGE (-128, 255); | |
741 | OP (0) = relocation; | |
742 | break; | |
743 | ||
744 | case R_RL78_ABS8U: | |
745 | RL78_STACK_POP (relocation); | |
746 | RANGE (0, 255); | |
747 | OP (0) = relocation; | |
748 | break; | |
749 | ||
750 | case R_RL78_ABS8UL: | |
751 | RL78_STACK_POP (relocation); | |
752 | relocation >>= 2; | |
753 | RANGE (0, 255); | |
754 | OP (0) = relocation; | |
755 | break; | |
756 | ||
757 | case R_RL78_ABS8UW: | |
758 | RL78_STACK_POP (relocation); | |
759 | relocation >>= 1; | |
760 | RANGE (0, 255); | |
761 | OP (0) = relocation; | |
762 | break; | |
763 | ||
764 | case R_RL78_ABS8S_PCREL: | |
765 | case R_RL78_ABS8S: | |
766 | RL78_STACK_POP (relocation); | |
767 | RANGE (-128, 127); | |
768 | OP (0) = relocation; | |
769 | break; | |
770 | ||
771 | case R_RL78_SYM: | |
772 | if (r_symndx < symtab_hdr->sh_info) | |
773 | RL78_STACK_PUSH (sec->output_section->vma | |
774 | + sec->output_offset | |
775 | + sym->st_value | |
776 | + rel->r_addend); | |
777 | else | |
778 | { | |
779 | if (h != NULL | |
780 | && (h->root.type == bfd_link_hash_defined | |
781 | || h->root.type == bfd_link_hash_defweak)) | |
782 | RL78_STACK_PUSH (h->root.u.def.value | |
783 | + sec->output_section->vma | |
784 | + sec->output_offset | |
785 | + rel->r_addend); | |
786 | else | |
787 | _bfd_error_handler (_("Warning: RL78_SYM reloc with an unknown symbol")); | |
788 | } | |
789 | break; | |
790 | ||
791 | case R_RL78_OPneg: | |
792 | { | |
793 | int32_t tmp; | |
794 | ||
795 | RL78_STACK_POP (tmp); | |
796 | tmp = - tmp; | |
797 | RL78_STACK_PUSH (tmp); | |
798 | } | |
799 | break; | |
800 | ||
801 | case R_RL78_OPadd: | |
802 | { | |
803 | int32_t tmp1, tmp2; | |
804 | ||
805 | RL78_STACK_POP (tmp2); | |
806 | RL78_STACK_POP (tmp1); | |
807 | tmp1 += tmp2; | |
808 | RL78_STACK_PUSH (tmp1); | |
809 | } | |
810 | break; | |
811 | ||
812 | case R_RL78_OPsub: | |
813 | { | |
814 | int32_t tmp1, tmp2; | |
815 | ||
816 | RL78_STACK_POP (tmp2); | |
817 | RL78_STACK_POP (tmp1); | |
818 | tmp2 -= tmp1; | |
819 | RL78_STACK_PUSH (tmp2); | |
820 | } | |
821 | break; | |
822 | ||
823 | case R_RL78_OPmul: | |
824 | { | |
825 | int32_t tmp1, tmp2; | |
826 | ||
827 | RL78_STACK_POP (tmp2); | |
828 | RL78_STACK_POP (tmp1); | |
829 | tmp1 *= tmp2; | |
830 | RL78_STACK_PUSH (tmp1); | |
831 | } | |
832 | break; | |
833 | ||
834 | case R_RL78_OPdiv: | |
835 | { | |
836 | int32_t tmp1, tmp2; | |
837 | ||
838 | RL78_STACK_POP (tmp2); | |
839 | RL78_STACK_POP (tmp1); | |
840 | tmp1 /= tmp2; | |
841 | RL78_STACK_PUSH (tmp1); | |
842 | } | |
843 | break; | |
844 | ||
845 | case R_RL78_OPshla: | |
846 | { | |
847 | int32_t tmp1, tmp2; | |
848 | ||
849 | RL78_STACK_POP (tmp2); | |
850 | RL78_STACK_POP (tmp1); | |
851 | tmp1 <<= tmp2; | |
852 | RL78_STACK_PUSH (tmp1); | |
853 | } | |
854 | break; | |
855 | ||
856 | case R_RL78_OPshra: | |
857 | { | |
858 | int32_t tmp1, tmp2; | |
859 | ||
860 | RL78_STACK_POP (tmp2); | |
861 | RL78_STACK_POP (tmp1); | |
862 | tmp1 >>= tmp2; | |
863 | RL78_STACK_PUSH (tmp1); | |
864 | } | |
865 | break; | |
866 | ||
867 | case R_RL78_OPsctsize: | |
868 | RL78_STACK_PUSH (input_section->size); | |
869 | break; | |
870 | ||
871 | case R_RL78_OPscttop: | |
872 | RL78_STACK_PUSH (input_section->output_section->vma); | |
873 | break; | |
874 | ||
875 | case R_RL78_OPand: | |
876 | { | |
877 | int32_t tmp1, tmp2; | |
878 | ||
879 | RL78_STACK_POP (tmp2); | |
880 | RL78_STACK_POP (tmp1); | |
881 | tmp1 &= tmp2; | |
882 | RL78_STACK_PUSH (tmp1); | |
883 | } | |
884 | break; | |
885 | ||
886 | case R_RL78_OPor: | |
887 | { | |
888 | int32_t tmp1, tmp2; | |
889 | ||
890 | RL78_STACK_POP (tmp2); | |
891 | RL78_STACK_POP (tmp1); | |
892 | tmp1 |= tmp2; | |
893 | RL78_STACK_PUSH (tmp1); | |
894 | } | |
895 | break; | |
896 | ||
897 | case R_RL78_OPxor: | |
898 | { | |
899 | int32_t tmp1, tmp2; | |
900 | ||
901 | RL78_STACK_POP (tmp2); | |
902 | RL78_STACK_POP (tmp1); | |
903 | tmp1 ^= tmp2; | |
904 | RL78_STACK_PUSH (tmp1); | |
905 | } | |
906 | break; | |
907 | ||
908 | case R_RL78_OPnot: | |
909 | { | |
910 | int32_t tmp; | |
911 | ||
912 | RL78_STACK_POP (tmp); | |
913 | tmp = ~ tmp; | |
914 | RL78_STACK_PUSH (tmp); | |
915 | } | |
916 | break; | |
917 | ||
918 | case R_RL78_OPmod: | |
919 | { | |
920 | int32_t tmp1, tmp2; | |
921 | ||
922 | RL78_STACK_POP (tmp2); | |
923 | RL78_STACK_POP (tmp1); | |
924 | tmp1 %= tmp2; | |
925 | RL78_STACK_PUSH (tmp1); | |
926 | } | |
927 | break; | |
928 | ||
929 | case R_RL78_OPromtop: | |
930 | RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset)); | |
931 | break; | |
932 | ||
933 | case R_RL78_OPramtop: | |
934 | RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset)); | |
935 | break; | |
936 | ||
937 | default: | |
938 | r = bfd_reloc_notsupported; | |
939 | break; | |
940 | } | |
941 | ||
942 | if (r != bfd_reloc_ok) | |
943 | { | |
944 | const char * msg = NULL; | |
945 | ||
946 | switch (r) | |
947 | { | |
948 | case bfd_reloc_overflow: | |
949 | /* Catch the case of a missing function declaration | |
950 | and emit a more helpful error message. */ | |
951 | if (r_type == R_RL78_DIR24S_PCREL) | |
952 | msg = _("%B(%A): error: call to undefined function '%s'"); | |
953 | else | |
954 | r = info->callbacks->reloc_overflow | |
955 | (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0, | |
956 | input_bfd, input_section, rel->r_offset); | |
957 | break; | |
958 | ||
959 | case bfd_reloc_undefined: | |
960 | r = info->callbacks->undefined_symbol | |
961 | (info, name, input_bfd, input_section, rel->r_offset, | |
962 | TRUE); | |
963 | break; | |
964 | ||
965 | case bfd_reloc_other: | |
966 | msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area"); | |
967 | break; | |
968 | ||
969 | case bfd_reloc_outofrange: | |
970 | msg = _("%B(%A): internal error: out of range error"); | |
971 | break; | |
972 | ||
973 | case bfd_reloc_notsupported: | |
974 | msg = _("%B(%A): internal error: unsupported relocation error"); | |
975 | break; | |
976 | ||
977 | case bfd_reloc_dangerous: | |
978 | msg = _("%B(%A): internal error: dangerous relocation"); | |
979 | break; | |
980 | ||
981 | default: | |
982 | msg = _("%B(%A): internal error: unknown error"); | |
983 | break; | |
984 | } | |
985 | ||
986 | if (msg) | |
987 | _bfd_error_handler (msg, input_bfd, input_section, name); | |
988 | ||
989 | if (! r) | |
990 | return FALSE; | |
991 | } | |
992 | } | |
993 | ||
994 | return TRUE; | |
995 | } | |
996 | \f | |
997 | /* Function to set the ELF flag bits. */ | |
998 | ||
999 | static bfd_boolean | |
1000 | rl78_elf_set_private_flags (bfd * abfd, flagword flags) | |
1001 | { | |
1002 | elf_elfheader (abfd)->e_flags = flags; | |
1003 | elf_flags_init (abfd) = TRUE; | |
1004 | return TRUE; | |
1005 | } | |
1006 | ||
1007 | static bfd_boolean no_warn_mismatch = FALSE; | |
1008 | ||
1009 | void bfd_elf32_rl78_set_target_flags (bfd_boolean); | |
1010 | ||
1011 | void | |
1012 | bfd_elf32_rl78_set_target_flags (bfd_boolean user_no_warn_mismatch) | |
1013 | { | |
1014 | no_warn_mismatch = user_no_warn_mismatch; | |
1015 | } | |
1016 | ||
1017 | /* Merge backend specific data from an object file to the output | |
1018 | object file when linking. */ | |
1019 | ||
1020 | static bfd_boolean | |
1021 | rl78_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd) | |
1022 | { | |
99c513f6 DD |
1023 | flagword new_flags; |
1024 | bfd_boolean error = FALSE; | |
1025 | ||
1026 | new_flags = elf_elfheader (ibfd)->e_flags; | |
99c513f6 DD |
1027 | |
1028 | if (!elf_flags_init (obfd)) | |
1029 | { | |
1030 | /* First call, no flags set. */ | |
1031 | elf_flags_init (obfd) = TRUE; | |
1032 | elf_elfheader (obfd)->e_flags = new_flags; | |
1033 | } | |
1034 | ||
1035 | return !error; | |
1036 | } | |
1037 | \f | |
1038 | static bfd_boolean | |
1039 | rl78_elf_print_private_bfd_data (bfd * abfd, void * ptr) | |
1040 | { | |
1041 | FILE * file = (FILE *) ptr; | |
1042 | flagword flags; | |
1043 | ||
1044 | BFD_ASSERT (abfd != NULL && ptr != NULL); | |
1045 | ||
1046 | /* Print normal ELF private data. */ | |
1047 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
1048 | ||
1049 | flags = elf_elfheader (abfd)->e_flags; | |
1050 | fprintf (file, _("private flags = 0x%lx:"), (long) flags); | |
1051 | ||
1052 | fputc ('\n', file); | |
1053 | return TRUE; | |
1054 | } | |
1055 | ||
1056 | /* Return the MACH for an e_flags value. */ | |
1057 | ||
1058 | static int | |
1059 | elf32_rl78_machine (bfd * abfd) | |
1060 | { | |
1061 | if ((elf_elfheader (abfd)->e_flags & EF_RL78_CPU_MASK) == EF_RL78_CPU_RL78) | |
1062 | return bfd_mach_rl78; | |
1063 | ||
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | static bfd_boolean | |
1068 | rl78_elf_object_p (bfd * abfd) | |
1069 | { | |
1070 | bfd_default_set_arch_mach (abfd, bfd_arch_rl78, | |
1071 | elf32_rl78_machine (abfd)); | |
1072 | return TRUE; | |
1073 | } | |
1074 | \f | |
1075 | #ifdef DEBUG | |
1076 | void | |
1077 | rl78_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms) | |
1078 | { | |
1079 | size_t locsymcount; | |
1080 | Elf_Internal_Sym * isymbuf; | |
1081 | Elf_Internal_Sym * isymend; | |
1082 | Elf_Internal_Sym * isym; | |
1083 | Elf_Internal_Shdr * symtab_hdr; | |
1084 | bfd_boolean free_internal = FALSE, free_external = FALSE; | |
1085 | char * st_info_str; | |
1086 | char * st_info_stb_str; | |
1087 | char * st_other_str; | |
1088 | char * st_shndx_str; | |
1089 | ||
1090 | if (! internal_syms) | |
1091 | { | |
1092 | internal_syms = bfd_malloc (1000); | |
1093 | free_internal = 1; | |
1094 | } | |
1095 | if (! external_syms) | |
1096 | { | |
1097 | external_syms = bfd_malloc (1000); | |
1098 | free_external = 1; | |
1099 | } | |
1100 | ||
1101 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1102 | locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
1103 | if (free_internal) | |
1104 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
1105 | symtab_hdr->sh_info, 0, | |
1106 | internal_syms, external_syms, NULL); | |
1107 | else | |
1108 | isymbuf = internal_syms; | |
1109 | isymend = isymbuf + locsymcount; | |
1110 | ||
1111 | for (isym = isymbuf ; isym < isymend ; isym++) | |
1112 | { | |
1113 | switch (ELF_ST_TYPE (isym->st_info)) | |
1114 | { | |
1115 | case STT_FUNC: st_info_str = "STT_FUNC"; | |
1116 | case STT_SECTION: st_info_str = "STT_SECTION"; | |
1117 | case STT_FILE: st_info_str = "STT_FILE"; | |
1118 | case STT_OBJECT: st_info_str = "STT_OBJECT"; | |
1119 | case STT_TLS: st_info_str = "STT_TLS"; | |
1120 | default: st_info_str = ""; | |
1121 | } | |
1122 | switch (ELF_ST_BIND (isym->st_info)) | |
1123 | { | |
1124 | case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; | |
1125 | case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; | |
1126 | default: st_info_stb_str = ""; | |
1127 | } | |
1128 | switch (ELF_ST_VISIBILITY (isym->st_other)) | |
1129 | { | |
1130 | case STV_DEFAULT: st_other_str = "STV_DEFAULT"; | |
1131 | case STV_INTERNAL: st_other_str = "STV_INTERNAL"; | |
1132 | case STV_PROTECTED: st_other_str = "STV_PROTECTED"; | |
1133 | default: st_other_str = ""; | |
1134 | } | |
1135 | switch (isym->st_shndx) | |
1136 | { | |
1137 | case SHN_ABS: st_shndx_str = "SHN_ABS"; | |
1138 | case SHN_COMMON: st_shndx_str = "SHN_COMMON"; | |
1139 | case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; | |
1140 | default: st_shndx_str = ""; | |
1141 | } | |
1142 | ||
1143 | printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s " | |
1144 | "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n", | |
1145 | isym, | |
1146 | (unsigned long) isym->st_value, | |
1147 | (unsigned long) isym->st_size, | |
1148 | isym->st_name, | |
1149 | bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link, | |
1150 | isym->st_name), | |
1151 | isym->st_info, st_info_str, st_info_stb_str, | |
1152 | isym->st_other, st_other_str, | |
1153 | isym->st_shndx, st_shndx_str); | |
1154 | } | |
1155 | if (free_internal) | |
1156 | free (internal_syms); | |
1157 | if (free_external) | |
1158 | free (external_syms); | |
1159 | } | |
1160 | ||
1161 | char * | |
1162 | rl78_get_reloc (long reloc) | |
1163 | { | |
1164 | if (0 <= reloc && reloc < R_RL78_max) | |
1165 | return rl78_elf_howto_table[reloc].name; | |
1166 | return ""; | |
1167 | } | |
1168 | #endif /* DEBUG */ | |
1169 | ||
1170 | \f | |
1171 | /* support PLT for 16-bit references to 24-bit functions. */ | |
1172 | ||
1173 | /* We support 16-bit pointers to code above 64k by generating a thunk | |
1174 | below 64k containing a JMP instruction to the final address. */ | |
1175 | ||
1176 | static bfd_boolean | |
1177 | rl78_elf_check_relocs | |
1178 | (bfd * abfd, | |
1179 | struct bfd_link_info * info, | |
1180 | asection * sec, | |
1181 | const Elf_Internal_Rela * relocs) | |
1182 | { | |
1183 | Elf_Internal_Shdr * symtab_hdr; | |
1184 | struct elf_link_hash_entry ** sym_hashes; | |
1185 | const Elf_Internal_Rela * rel; | |
1186 | const Elf_Internal_Rela * rel_end; | |
1187 | bfd_vma *local_plt_offsets; | |
1188 | asection *splt; | |
1189 | bfd *dynobj; | |
1190 | ||
1191 | if (info->relocatable) | |
1192 | return TRUE; | |
1193 | ||
1194 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1195 | sym_hashes = elf_sym_hashes (abfd); | |
1196 | local_plt_offsets = elf_local_got_offsets (abfd); | |
1197 | splt = NULL; | |
1198 | dynobj = elf_hash_table(info)->dynobj; | |
1199 | ||
1200 | rel_end = relocs + sec->reloc_count; | |
1201 | for (rel = relocs; rel < rel_end; rel++) | |
1202 | { | |
1203 | struct elf_link_hash_entry *h; | |
1204 | unsigned long r_symndx; | |
1205 | bfd_vma *offset; | |
1206 | ||
1207 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1208 | if (r_symndx < symtab_hdr->sh_info) | |
1209 | h = NULL; | |
1210 | else | |
1211 | { | |
1212 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1213 | while (h->root.type == bfd_link_hash_indirect | |
1214 | || h->root.type == bfd_link_hash_warning) | |
1215 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1216 | } | |
1217 | ||
1218 | switch (ELF32_R_TYPE (rel->r_info)) | |
1219 | { | |
1220 | /* This relocation describes a 16-bit pointer to a function. | |
1221 | We may need to allocate a thunk in low memory; reserve memory | |
1222 | for it now. */ | |
1223 | case R_RL78_DIR16S: | |
1224 | if (dynobj == NULL) | |
1225 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
1226 | if (splt == NULL) | |
1227 | { | |
1228 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1229 | if (splt == NULL) | |
1230 | { | |
1231 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
1232 | | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
1233 | | SEC_READONLY | SEC_CODE); | |
1234 | splt = bfd_make_section_with_flags (dynobj, ".plt", flags); | |
1235 | if (splt == NULL | |
1236 | || ! bfd_set_section_alignment (dynobj, splt, 1)) | |
1237 | return FALSE; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if (h != NULL) | |
1242 | offset = &h->plt.offset; | |
1243 | else | |
1244 | { | |
1245 | if (local_plt_offsets == NULL) | |
1246 | { | |
1247 | size_t size; | |
1248 | unsigned int i; | |
1249 | ||
1250 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
1251 | local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
1252 | if (local_plt_offsets == NULL) | |
1253 | return FALSE; | |
1254 | elf_local_got_offsets (abfd) = local_plt_offsets; | |
1255 | ||
1256 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
1257 | local_plt_offsets[i] = (bfd_vma) -1; | |
1258 | } | |
1259 | offset = &local_plt_offsets[r_symndx]; | |
1260 | } | |
1261 | ||
1262 | if (*offset == (bfd_vma) -1) | |
1263 | { | |
1264 | *offset = splt->size; | |
1265 | splt->size += 4; | |
1266 | } | |
1267 | break; | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | return TRUE; | |
1272 | } | |
1273 | ||
1274 | /* This must exist if dynobj is ever set. */ | |
1275 | ||
1276 | static bfd_boolean | |
1277 | rl78_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED, | |
1278 | struct bfd_link_info *info) | |
1279 | { | |
1280 | bfd *dynobj; | |
1281 | asection *splt; | |
1282 | ||
9cea966c DD |
1283 | /* As an extra sanity check, verify that all plt entries have been |
1284 | filled in. However, relaxing might have changed the relocs so | |
1285 | that some plt entries don't get filled in, so we have to skip | |
1286 | this check if we're relaxing. Unfortunately, check_relocs is | |
1287 | called before relaxation. */ | |
99c513f6 | 1288 | |
9cea966c | 1289 | if (info->relax_trip > 0) |
99c513f6 | 1290 | { |
9cea966c DD |
1291 | if ((dynobj = elf_hash_table (info)->dynobj) != NULL |
1292 | && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL) | |
99c513f6 | 1293 | { |
9cea966c DD |
1294 | bfd_byte *contents = splt->contents; |
1295 | unsigned int i, size = splt->size; | |
1296 | for (i = 0; i < size; i += 4) | |
1297 | { | |
1298 | unsigned int x = bfd_get_32 (dynobj, contents + i); | |
1299 | BFD_ASSERT (x != 0); | |
1300 | } | |
99c513f6 DD |
1301 | } |
1302 | } | |
1303 | ||
1304 | return TRUE; | |
1305 | } | |
1306 | ||
1307 | static bfd_boolean | |
1308 | rl78_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED, | |
1309 | struct bfd_link_info *info) | |
1310 | { | |
1311 | bfd *dynobj; | |
1312 | asection *splt; | |
1313 | ||
1314 | if (info->relocatable) | |
1315 | return TRUE; | |
1316 | ||
1317 | dynobj = elf_hash_table (info)->dynobj; | |
1318 | if (dynobj == NULL) | |
1319 | return TRUE; | |
1320 | ||
1321 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1322 | BFD_ASSERT (splt != NULL); | |
1323 | ||
1324 | splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size); | |
1325 | if (splt->contents == NULL) | |
1326 | return FALSE; | |
1327 | ||
1328 | return TRUE; | |
1329 | } | |
1330 | ||
1331 | \f | |
1332 | ||
1333 | /* Handle relaxing. */ | |
1334 | ||
1335 | /* A subroutine of rl78_elf_relax_section. If the global symbol H | |
1336 | is within the low 64k, remove any entry for it in the plt. */ | |
1337 | ||
1338 | struct relax_plt_data | |
1339 | { | |
1340 | asection *splt; | |
1341 | bfd_boolean *again; | |
1342 | }; | |
1343 | ||
1344 | static bfd_boolean | |
1345 | rl78_relax_plt_check (struct elf_link_hash_entry *h, | |
1346 | PTR xdata) | |
1347 | { | |
1348 | struct relax_plt_data *data = (struct relax_plt_data *) xdata; | |
1349 | ||
1350 | if (h->plt.offset != (bfd_vma) -1) | |
1351 | { | |
1352 | bfd_vma address; | |
1353 | ||
1354 | if (h->root.type == bfd_link_hash_undefined | |
1355 | || h->root.type == bfd_link_hash_undefweak) | |
1356 | address = 0; | |
1357 | else | |
1358 | address = (h->root.u.def.section->output_section->vma | |
1359 | + h->root.u.def.section->output_offset | |
1360 | + h->root.u.def.value); | |
1361 | ||
1362 | if (valid_16bit_address (address)) | |
1363 | { | |
1364 | h->plt.offset = -1; | |
1365 | data->splt->size -= 4; | |
1366 | *data->again = TRUE; | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | return TRUE; | |
1371 | } | |
1372 | ||
1373 | /* A subroutine of rl78_elf_relax_section. If the global symbol H | |
1374 | previously had a plt entry, give it a new entry offset. */ | |
1375 | ||
1376 | static bfd_boolean | |
1377 | rl78_relax_plt_realloc (struct elf_link_hash_entry *h, | |
1378 | PTR xdata) | |
1379 | { | |
1380 | bfd_vma *entry = (bfd_vma *) xdata; | |
1381 | ||
1382 | if (h->plt.offset != (bfd_vma) -1) | |
1383 | { | |
1384 | h->plt.offset = *entry; | |
1385 | *entry += 4; | |
1386 | } | |
1387 | ||
1388 | return TRUE; | |
1389 | } | |
1390 | ||
1391 | static bfd_boolean | |
1392 | rl78_elf_relax_plt_section (bfd *dynobj, | |
1393 | asection *splt, | |
1394 | struct bfd_link_info *info, | |
1395 | bfd_boolean *again) | |
1396 | { | |
1397 | struct relax_plt_data relax_plt_data; | |
1398 | bfd *ibfd; | |
1399 | ||
1400 | /* Assume nothing changes. */ | |
1401 | *again = FALSE; | |
1402 | ||
1403 | if (info->relocatable) | |
1404 | return TRUE; | |
1405 | ||
1406 | /* We only relax the .plt section at the moment. */ | |
1407 | if (dynobj != elf_hash_table (info)->dynobj | |
1408 | || strcmp (splt->name, ".plt") != 0) | |
1409 | return TRUE; | |
1410 | ||
1411 | /* Quick check for an empty plt. */ | |
1412 | if (splt->size == 0) | |
1413 | return TRUE; | |
1414 | ||
1415 | /* Map across all global symbols; see which ones happen to | |
1416 | fall in the low 64k. */ | |
1417 | relax_plt_data.splt = splt; | |
1418 | relax_plt_data.again = again; | |
1419 | elf_link_hash_traverse (elf_hash_table (info), rl78_relax_plt_check, | |
1420 | &relax_plt_data); | |
1421 | ||
1422 | /* Likewise for local symbols, though that's somewhat less convenient | |
1423 | as we have to walk the list of input bfds and swap in symbol data. */ | |
1424 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1425 | { | |
1426 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1427 | Elf_Internal_Shdr *symtab_hdr; | |
1428 | Elf_Internal_Sym *isymbuf = NULL; | |
1429 | unsigned int idx; | |
1430 | ||
1431 | if (! local_plt_offsets) | |
1432 | continue; | |
1433 | ||
1434 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1435 | if (symtab_hdr->sh_info != 0) | |
1436 | { | |
1437 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1438 | if (isymbuf == NULL) | |
1439 | isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr, | |
1440 | symtab_hdr->sh_info, 0, | |
1441 | NULL, NULL, NULL); | |
1442 | if (isymbuf == NULL) | |
1443 | return FALSE; | |
1444 | } | |
1445 | ||
1446 | for (idx = 0; idx < symtab_hdr->sh_info; ++idx) | |
1447 | { | |
1448 | Elf_Internal_Sym *isym; | |
1449 | asection *tsec; | |
1450 | bfd_vma address; | |
1451 | ||
1452 | if (local_plt_offsets[idx] == (bfd_vma) -1) | |
1453 | continue; | |
1454 | ||
1455 | isym = &isymbuf[idx]; | |
1456 | if (isym->st_shndx == SHN_UNDEF) | |
1457 | continue; | |
1458 | else if (isym->st_shndx == SHN_ABS) | |
1459 | tsec = bfd_abs_section_ptr; | |
1460 | else if (isym->st_shndx == SHN_COMMON) | |
1461 | tsec = bfd_com_section_ptr; | |
1462 | else | |
1463 | tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx); | |
1464 | ||
1465 | address = (tsec->output_section->vma | |
1466 | + tsec->output_offset | |
1467 | + isym->st_value); | |
1468 | if (valid_16bit_address (address)) | |
1469 | { | |
1470 | local_plt_offsets[idx] = -1; | |
1471 | splt->size -= 4; | |
1472 | *again = TRUE; | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | if (isymbuf != NULL | |
1477 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1478 | { | |
1479 | if (! info->keep_memory) | |
1480 | free (isymbuf); | |
1481 | else | |
1482 | { | |
1483 | /* Cache the symbols for elf_link_input_bfd. */ | |
1484 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1485 | } | |
1486 | } | |
1487 | } | |
1488 | ||
1489 | /* If we changed anything, walk the symbols again to reallocate | |
1490 | .plt entry addresses. */ | |
1491 | if (*again && splt->size > 0) | |
1492 | { | |
1493 | bfd_vma entry = 0; | |
1494 | ||
1495 | elf_link_hash_traverse (elf_hash_table (info), | |
1496 | rl78_relax_plt_realloc, &entry); | |
1497 | ||
1498 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1499 | { | |
1500 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1501 | unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info; | |
1502 | unsigned int idx; | |
1503 | ||
1504 | if (! local_plt_offsets) | |
1505 | continue; | |
1506 | ||
1507 | for (idx = 0; idx < nlocals; ++idx) | |
1508 | if (local_plt_offsets[idx] != (bfd_vma) -1) | |
1509 | { | |
1510 | local_plt_offsets[idx] = entry; | |
1511 | entry += 4; | |
1512 | } | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | return TRUE; | |
1517 | } | |
1518 | ||
9cea966c DD |
1519 | /* Delete some bytes from a section while relaxing. */ |
1520 | ||
1521 | static bfd_boolean | |
1522 | elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count, | |
1523 | Elf_Internal_Rela *alignment_rel, int force_snip) | |
1524 | { | |
1525 | Elf_Internal_Shdr * symtab_hdr; | |
1526 | unsigned int sec_shndx; | |
1527 | bfd_byte * contents; | |
1528 | Elf_Internal_Rela * irel; | |
1529 | Elf_Internal_Rela * irelend; | |
1530 | Elf_Internal_Sym * isym; | |
1531 | Elf_Internal_Sym * isymend; | |
1532 | bfd_vma toaddr; | |
1533 | unsigned int symcount; | |
1534 | struct elf_link_hash_entry ** sym_hashes; | |
1535 | struct elf_link_hash_entry ** end_hashes; | |
1536 | ||
1537 | if (!alignment_rel) | |
1538 | force_snip = 1; | |
1539 | ||
1540 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1541 | ||
1542 | contents = elf_section_data (sec)->this_hdr.contents; | |
1543 | ||
1544 | /* The deletion must stop at the next alignment boundary, if | |
1545 | ALIGNMENT_REL is non-NULL. */ | |
1546 | toaddr = sec->size; | |
1547 | if (alignment_rel) | |
1548 | toaddr = alignment_rel->r_offset; | |
1549 | ||
1550 | irel = elf_section_data (sec)->relocs; | |
1551 | irelend = irel + sec->reloc_count; | |
1552 | ||
1553 | /* Actually delete the bytes. */ | |
1554 | memmove (contents + addr, contents + addr + count, | |
1555 | (size_t) (toaddr - addr - count)); | |
1556 | ||
1557 | /* If we don't have an alignment marker to worry about, we can just | |
1558 | shrink the section. Otherwise, we have to fill in the newly | |
1559 | created gap with NOP insns (0x03). */ | |
1560 | if (force_snip) | |
1561 | sec->size -= count; | |
1562 | else | |
1563 | memset (contents + toaddr - count, 0x03, count); | |
1564 | ||
1565 | /* Adjust all the relocs. */ | |
1566 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
1567 | { | |
1568 | /* Get the new reloc address. */ | |
1569 | if (irel->r_offset > addr | |
1570 | && (irel->r_offset < toaddr | |
1571 | || (force_snip && irel->r_offset == toaddr))) | |
1572 | irel->r_offset -= count; | |
1573 | ||
1574 | /* If we see an ALIGN marker at the end of the gap, we move it | |
1575 | to the beginning of the gap, since marking these gaps is what | |
1576 | they're for. */ | |
1577 | if (irel->r_offset == toaddr | |
1578 | && ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX | |
1579 | && irel->r_addend & RL78_RELAXA_ALIGN) | |
1580 | irel->r_offset -= count; | |
1581 | } | |
1582 | ||
1583 | /* Adjust the local symbols defined in this section. */ | |
1584 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1585 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1586 | isymend = isym + symtab_hdr->sh_info; | |
1587 | ||
1588 | for (; isym < isymend; isym++) | |
1589 | { | |
1590 | /* If the symbol is in the range of memory we just moved, we | |
1591 | have to adjust its value. */ | |
1592 | if (isym->st_shndx == sec_shndx | |
1593 | && isym->st_value > addr | |
1594 | && isym->st_value < toaddr) | |
1595 | isym->st_value -= count; | |
1596 | ||
1597 | /* If the symbol *spans* the bytes we just deleted (i.e. it's | |
1598 | *end* is in the moved bytes but it's *start* isn't), then we | |
1599 | must adjust its size. */ | |
1600 | if (isym->st_shndx == sec_shndx | |
1601 | && isym->st_value < addr | |
1602 | && isym->st_value + isym->st_size > addr | |
1603 | && isym->st_value + isym->st_size < toaddr) | |
1604 | isym->st_size -= count; | |
1605 | } | |
1606 | ||
1607 | /* Now adjust the global symbols defined in this section. */ | |
1608 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
1609 | - symtab_hdr->sh_info); | |
1610 | sym_hashes = elf_sym_hashes (abfd); | |
1611 | end_hashes = sym_hashes + symcount; | |
1612 | ||
1613 | for (; sym_hashes < end_hashes; sym_hashes++) | |
1614 | { | |
1615 | struct elf_link_hash_entry *sym_hash = *sym_hashes; | |
1616 | ||
1617 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1618 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1619 | && sym_hash->root.u.def.section == sec) | |
1620 | { | |
1621 | /* As above, adjust the value if needed. */ | |
1622 | if (sym_hash->root.u.def.value > addr | |
1623 | && sym_hash->root.u.def.value < toaddr) | |
1624 | sym_hash->root.u.def.value -= count; | |
1625 | ||
1626 | /* As above, adjust the size if needed. */ | |
1627 | if (sym_hash->root.u.def.value < addr | |
1628 | && sym_hash->root.u.def.value + sym_hash->size > addr | |
1629 | && sym_hash->root.u.def.value + sym_hash->size < toaddr) | |
1630 | sym_hash->size -= count; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | return TRUE; | |
1635 | } | |
1636 | ||
1637 | /* Used to sort relocs by address. If relocs have the same address, | |
1638 | we maintain their relative order, except that R_RL78_RH_RELAX | |
1639 | alignment relocs must be the first reloc for any given address. */ | |
1640 | ||
1641 | static void | |
1642 | reloc_bubblesort (Elf_Internal_Rela * r, int count) | |
1643 | { | |
1644 | int i; | |
1645 | bfd_boolean again; | |
1646 | bfd_boolean swappit; | |
1647 | ||
1648 | /* This is almost a classic bubblesort. It's the slowest sort, but | |
1649 | we're taking advantage of the fact that the relocations are | |
1650 | mostly in order already (the assembler emits them that way) and | |
1651 | we need relocs with the same address to remain in the same | |
1652 | relative order. */ | |
1653 | again = TRUE; | |
1654 | while (again) | |
1655 | { | |
1656 | again = FALSE; | |
1657 | for (i = 0; i < count - 1; i ++) | |
1658 | { | |
1659 | if (r[i].r_offset > r[i + 1].r_offset) | |
1660 | swappit = TRUE; | |
1661 | else if (r[i].r_offset < r[i + 1].r_offset) | |
1662 | swappit = FALSE; | |
1663 | else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX | |
1664 | && (r[i + 1].r_addend & RL78_RELAXA_ALIGN)) | |
1665 | swappit = TRUE; | |
1666 | else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX | |
1667 | && (r[i + 1].r_addend & RL78_RELAXA_ELIGN) | |
1668 | && !(ELF32_R_TYPE (r[i].r_info) == R_RL78_RH_RELAX | |
1669 | && (r[i].r_addend & RL78_RELAXA_ALIGN))) | |
1670 | swappit = TRUE; | |
1671 | else | |
1672 | swappit = FALSE; | |
1673 | ||
1674 | if (swappit) | |
1675 | { | |
1676 | Elf_Internal_Rela tmp; | |
1677 | ||
1678 | tmp = r[i]; | |
1679 | r[i] = r[i + 1]; | |
1680 | r[i + 1] = tmp; | |
1681 | /* If we do move a reloc back, re-scan to see if it | |
1682 | needs to be moved even further back. This avoids | |
1683 | most of the O(n^2) behavior for our cases. */ | |
1684 | if (i > 0) | |
1685 | i -= 2; | |
1686 | again = TRUE; | |
1687 | } | |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | ||
1693 | #define OFFSET_FOR_RELOC(rel, lrel, scale) \ | |
1694 | rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \ | |
1695 | lrel, abfd, sec, link_info, scale) | |
1696 | ||
1697 | static bfd_vma | |
1698 | rl78_offset_for_reloc (bfd * abfd, | |
1699 | Elf_Internal_Rela * rel, | |
1700 | Elf_Internal_Shdr * symtab_hdr, | |
1701 | Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED, | |
1702 | Elf_Internal_Sym * intsyms, | |
1703 | Elf_Internal_Rela ** lrel, | |
1704 | bfd * input_bfd, | |
1705 | asection * input_section, | |
1706 | struct bfd_link_info * info, | |
1707 | int * scale) | |
1708 | { | |
1709 | bfd_vma symval; | |
1710 | bfd_reloc_status_type r; | |
1711 | ||
1712 | *scale = 1; | |
1713 | ||
1714 | /* REL is the first of 1..N relocations. We compute the symbol | |
1715 | value for each relocation, then combine them if needed. LREL | |
1716 | gets a pointer to the last relocation used. */ | |
1717 | while (1) | |
1718 | { | |
1719 | int32_t tmp1, tmp2; | |
1720 | ||
1721 | /* Get the value of the symbol referred to by the reloc. */ | |
1722 | if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info) | |
1723 | { | |
1724 | /* A local symbol. */ | |
1725 | Elf_Internal_Sym *isym; | |
1726 | asection *ssec; | |
1727 | ||
1728 | isym = intsyms + ELF32_R_SYM (rel->r_info); | |
1729 | ||
1730 | if (isym->st_shndx == SHN_UNDEF) | |
1731 | ssec = bfd_und_section_ptr; | |
1732 | else if (isym->st_shndx == SHN_ABS) | |
1733 | ssec = bfd_abs_section_ptr; | |
1734 | else if (isym->st_shndx == SHN_COMMON) | |
1735 | ssec = bfd_com_section_ptr; | |
1736 | else | |
1737 | ssec = bfd_section_from_elf_index (abfd, | |
1738 | isym->st_shndx); | |
1739 | ||
1740 | /* Initial symbol value. */ | |
1741 | symval = isym->st_value; | |
1742 | ||
1743 | /* GAS may have made this symbol relative to a section, in | |
1744 | which case, we have to add the addend to find the | |
1745 | symbol. */ | |
1746 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
1747 | symval += rel->r_addend; | |
1748 | ||
1749 | if (ssec) | |
1750 | { | |
1751 | if ((ssec->flags & SEC_MERGE) | |
1752 | && ssec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
1753 | symval = _bfd_merged_section_offset (abfd, & ssec, | |
1754 | elf_section_data (ssec)->sec_info, | |
1755 | symval); | |
1756 | } | |
1757 | ||
1758 | /* Now make the offset relative to where the linker is putting it. */ | |
1759 | if (ssec) | |
1760 | symval += | |
1761 | ssec->output_section->vma + ssec->output_offset; | |
1762 | ||
1763 | symval += rel->r_addend; | |
1764 | } | |
1765 | else | |
1766 | { | |
1767 | unsigned long indx; | |
1768 | struct elf_link_hash_entry * h; | |
1769 | ||
1770 | /* An external symbol. */ | |
1771 | indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info; | |
1772 | h = elf_sym_hashes (abfd)[indx]; | |
1773 | BFD_ASSERT (h != NULL); | |
1774 | ||
1775 | if (h->root.type != bfd_link_hash_defined | |
1776 | && h->root.type != bfd_link_hash_defweak) | |
1777 | { | |
1778 | /* This appears to be a reference to an undefined | |
1779 | symbol. Just ignore it--it will be caught by the | |
1780 | regular reloc processing. */ | |
1781 | if (lrel) | |
1782 | *lrel = rel; | |
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | symval = (h->root.u.def.value | |
1787 | + h->root.u.def.section->output_section->vma | |
1788 | + h->root.u.def.section->output_offset); | |
1789 | ||
1790 | symval += rel->r_addend; | |
1791 | } | |
1792 | ||
1793 | switch (ELF32_R_TYPE (rel->r_info)) | |
1794 | { | |
1795 | case R_RL78_SYM: | |
1796 | RL78_STACK_PUSH (symval); | |
1797 | break; | |
1798 | ||
1799 | case R_RL78_OPneg: | |
1800 | RL78_STACK_POP (tmp1); | |
1801 | tmp1 = - tmp1; | |
1802 | RL78_STACK_PUSH (tmp1); | |
1803 | break; | |
1804 | ||
1805 | case R_RL78_OPadd: | |
1806 | RL78_STACK_POP (tmp1); | |
1807 | RL78_STACK_POP (tmp2); | |
1808 | tmp1 += tmp2; | |
1809 | RL78_STACK_PUSH (tmp1); | |
1810 | break; | |
1811 | ||
1812 | case R_RL78_OPsub: | |
1813 | RL78_STACK_POP (tmp1); | |
1814 | RL78_STACK_POP (tmp2); | |
1815 | tmp2 -= tmp1; | |
1816 | RL78_STACK_PUSH (tmp2); | |
1817 | break; | |
1818 | ||
1819 | case R_RL78_OPmul: | |
1820 | RL78_STACK_POP (tmp1); | |
1821 | RL78_STACK_POP (tmp2); | |
1822 | tmp1 *= tmp2; | |
1823 | RL78_STACK_PUSH (tmp1); | |
1824 | break; | |
1825 | ||
1826 | case R_RL78_OPdiv: | |
1827 | RL78_STACK_POP (tmp1); | |
1828 | RL78_STACK_POP (tmp2); | |
1829 | tmp1 /= tmp2; | |
1830 | RL78_STACK_PUSH (tmp1); | |
1831 | break; | |
1832 | ||
1833 | case R_RL78_OPshla: | |
1834 | RL78_STACK_POP (tmp1); | |
1835 | RL78_STACK_POP (tmp2); | |
1836 | tmp1 <<= tmp2; | |
1837 | RL78_STACK_PUSH (tmp1); | |
1838 | break; | |
1839 | ||
1840 | case R_RL78_OPshra: | |
1841 | RL78_STACK_POP (tmp1); | |
1842 | RL78_STACK_POP (tmp2); | |
1843 | tmp1 >>= tmp2; | |
1844 | RL78_STACK_PUSH (tmp1); | |
1845 | break; | |
1846 | ||
1847 | case R_RL78_OPsctsize: | |
1848 | RL78_STACK_PUSH (input_section->size); | |
1849 | break; | |
1850 | ||
1851 | case R_RL78_OPscttop: | |
1852 | RL78_STACK_PUSH (input_section->output_section->vma); | |
1853 | break; | |
1854 | ||
1855 | case R_RL78_OPand: | |
1856 | RL78_STACK_POP (tmp1); | |
1857 | RL78_STACK_POP (tmp2); | |
1858 | tmp1 &= tmp2; | |
1859 | RL78_STACK_PUSH (tmp1); | |
1860 | break; | |
1861 | ||
1862 | case R_RL78_OPor: | |
1863 | RL78_STACK_POP (tmp1); | |
1864 | RL78_STACK_POP (tmp2); | |
1865 | tmp1 |= tmp2; | |
1866 | RL78_STACK_PUSH (tmp1); | |
1867 | break; | |
1868 | ||
1869 | case R_RL78_OPxor: | |
1870 | RL78_STACK_POP (tmp1); | |
1871 | RL78_STACK_POP (tmp2); | |
1872 | tmp1 ^= tmp2; | |
1873 | RL78_STACK_PUSH (tmp1); | |
1874 | break; | |
1875 | ||
1876 | case R_RL78_OPnot: | |
1877 | RL78_STACK_POP (tmp1); | |
1878 | tmp1 = ~ tmp1; | |
1879 | RL78_STACK_PUSH (tmp1); | |
1880 | break; | |
1881 | ||
1882 | case R_RL78_OPmod: | |
1883 | RL78_STACK_POP (tmp1); | |
1884 | RL78_STACK_POP (tmp2); | |
1885 | tmp1 %= tmp2; | |
1886 | RL78_STACK_PUSH (tmp1); | |
1887 | break; | |
1888 | ||
1889 | case R_RL78_OPromtop: | |
1890 | RL78_STACK_PUSH (get_romstart (&r, info, input_bfd, input_section, rel->r_offset)); | |
1891 | break; | |
1892 | ||
1893 | case R_RL78_OPramtop: | |
1894 | RL78_STACK_PUSH (get_ramstart (&r, info, input_bfd, input_section, rel->r_offset)); | |
1895 | break; | |
1896 | ||
1897 | case R_RL78_DIR16UL: | |
1898 | case R_RL78_DIR8UL: | |
1899 | case R_RL78_ABS16UL: | |
1900 | case R_RL78_ABS8UL: | |
1901 | if (rl78_stack_top) | |
1902 | RL78_STACK_POP (symval); | |
1903 | if (lrel) | |
1904 | *lrel = rel; | |
1905 | *scale = 4; | |
1906 | return symval; | |
1907 | ||
1908 | case R_RL78_DIR16UW: | |
1909 | case R_RL78_DIR8UW: | |
1910 | case R_RL78_ABS16UW: | |
1911 | case R_RL78_ABS8UW: | |
1912 | if (rl78_stack_top) | |
1913 | RL78_STACK_POP (symval); | |
1914 | if (lrel) | |
1915 | *lrel = rel; | |
1916 | *scale = 2; | |
1917 | return symval; | |
1918 | ||
1919 | default: | |
1920 | if (rl78_stack_top) | |
1921 | RL78_STACK_POP (symval); | |
1922 | if (lrel) | |
1923 | *lrel = rel; | |
1924 | return symval; | |
1925 | } | |
1926 | ||
1927 | rel ++; | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | struct { | |
1932 | int prefix; /* or -1 for "no prefix" */ | |
1933 | int insn; /* or -1 for "end of list" */ | |
1934 | int insn_for_saddr; /* or -1 for "no alternative" */ | |
1935 | int insn_for_sfr; /* or -1 for "no alternative" */ | |
1936 | } relax_addr16[] = { | |
1937 | { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */ | |
1938 | { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */ | |
1939 | { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */ | |
1940 | { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */ | |
1941 | ||
1942 | { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */ | |
1943 | { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */ | |
1944 | { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */ | |
1945 | { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */ | |
1946 | { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */ | |
1947 | { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */ | |
1948 | { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */ | |
1949 | { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */ | |
1950 | ||
1951 | { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */ | |
1952 | { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */ | |
1953 | { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */ | |
1954 | { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */ | |
1955 | { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */ | |
1956 | ||
1957 | { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */ | |
1958 | { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */ | |
1959 | { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */ | |
1960 | { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */ | |
1961 | ||
1962 | { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */ | |
1963 | { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */ | |
1964 | { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */ | |
1965 | ||
1966 | { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */ | |
1967 | { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */ | |
1968 | { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */ | |
1969 | { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */ | |
1970 | { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */ | |
1971 | { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */ | |
1972 | ||
1973 | { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */ | |
1974 | ||
1975 | { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */ | |
1976 | { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */ | |
1977 | { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */ | |
1978 | { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */ | |
1979 | { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */ | |
1980 | { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */ | |
1981 | { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */ | |
1982 | { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */ | |
1983 | ||
1984 | { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */ | |
1985 | { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */ | |
1986 | { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */ | |
1987 | { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */ | |
1988 | { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */ | |
1989 | { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */ | |
1990 | { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */ | |
1991 | { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */ | |
1992 | ||
1993 | { -1, -1, -1, -1 } | |
1994 | }; | |
1995 | ||
1996 | /* Relax one section. */ | |
1997 | ||
99c513f6 DD |
1998 | static bfd_boolean |
1999 | rl78_elf_relax_section | |
2000 | (bfd * abfd, | |
2001 | asection * sec, | |
2002 | struct bfd_link_info * link_info, | |
2003 | bfd_boolean * again) | |
2004 | { | |
9cea966c DD |
2005 | Elf_Internal_Shdr * symtab_hdr; |
2006 | Elf_Internal_Shdr * shndx_hdr; | |
2007 | Elf_Internal_Rela * internal_relocs; | |
2008 | Elf_Internal_Rela * free_relocs = NULL; | |
2009 | Elf_Internal_Rela * irel; | |
2010 | Elf_Internal_Rela * srel; | |
2011 | Elf_Internal_Rela * irelend; | |
2012 | Elf_Internal_Rela * next_alignment; | |
9cea966c DD |
2013 | bfd_byte * contents = NULL; |
2014 | bfd_byte * free_contents = NULL; | |
2015 | Elf_Internal_Sym * intsyms = NULL; | |
2016 | Elf_Internal_Sym * free_intsyms = NULL; | |
2017 | Elf_External_Sym_Shndx * shndx_buf = NULL; | |
2018 | bfd_vma pc; | |
9cea966c DD |
2019 | bfd_vma symval ATTRIBUTE_UNUSED = 0; |
2020 | int pcrel ATTRIBUTE_UNUSED = 0; | |
2021 | int code ATTRIBUTE_UNUSED = 0; | |
2022 | int section_alignment_glue; | |
2023 | int scale; | |
2024 | ||
99c513f6 DD |
2025 | if (abfd == elf_hash_table (link_info)->dynobj |
2026 | && strcmp (sec->name, ".plt") == 0) | |
2027 | return rl78_elf_relax_plt_section (abfd, sec, link_info, again); | |
2028 | ||
2029 | /* Assume nothing changes. */ | |
2030 | *again = FALSE; | |
9cea966c DD |
2031 | |
2032 | /* We don't have to do anything for a relocatable link, if | |
2033 | this section does not have relocs, or if this is not a | |
2034 | code section. */ | |
2035 | if (link_info->relocatable | |
2036 | || (sec->flags & SEC_RELOC) == 0 | |
2037 | || sec->reloc_count == 0 | |
2038 | || (sec->flags & SEC_CODE) == 0) | |
2039 | return TRUE; | |
2040 | ||
2041 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2042 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
2043 | ||
9cea966c DD |
2044 | /* Get the section contents. */ |
2045 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
2046 | contents = elf_section_data (sec)->this_hdr.contents; | |
2047 | /* Go get them off disk. */ | |
2048 | else | |
2049 | { | |
2050 | if (! bfd_malloc_and_get_section (abfd, sec, &contents)) | |
2051 | goto error_return; | |
2052 | elf_section_data (sec)->this_hdr.contents = contents; | |
2053 | } | |
2054 | ||
2055 | /* Read this BFD's symbols. */ | |
2056 | /* Get cached copy if it exists. */ | |
2057 | if (symtab_hdr->contents != NULL) | |
2058 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
2059 | else | |
2060 | { | |
2061 | intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL); | |
2062 | symtab_hdr->contents = (bfd_byte *) intsyms; | |
2063 | } | |
2064 | ||
2065 | if (shndx_hdr->sh_size != 0) | |
2066 | { | |
2067 | bfd_size_type amt; | |
2068 | ||
2069 | amt = symtab_hdr->sh_info; | |
2070 | amt *= sizeof (Elf_External_Sym_Shndx); | |
2071 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
2072 | if (shndx_buf == NULL) | |
2073 | goto error_return; | |
2074 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
2075 | || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt) | |
2076 | goto error_return; | |
2077 | shndx_hdr->contents = (bfd_byte *) shndx_buf; | |
2078 | } | |
2079 | ||
2080 | /* Get a copy of the native relocations. */ | |
2081 | internal_relocs = (_bfd_elf_link_read_relocs | |
2082 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
2083 | link_info->keep_memory)); | |
2084 | if (internal_relocs == NULL) | |
2085 | goto error_return; | |
2086 | if (! link_info->keep_memory) | |
2087 | free_relocs = internal_relocs; | |
2088 | ||
2089 | /* The RL_ relocs must be just before the operand relocs they go | |
2090 | with, so we must sort them to guarantee this. We use bubblesort | |
2091 | instead of qsort so we can guarantee that relocs with the same | |
2092 | address remain in the same relative order. */ | |
2093 | reloc_bubblesort (internal_relocs, sec->reloc_count); | |
2094 | ||
2095 | /* Walk through them looking for relaxing opportunities. */ | |
2096 | irelend = internal_relocs + sec->reloc_count; | |
2097 | ||
2098 | ||
2099 | /* This will either be NULL or a pointer to the next alignment | |
2100 | relocation. */ | |
2101 | next_alignment = internal_relocs; | |
9cea966c DD |
2102 | |
2103 | /* We calculate worst case shrinkage caused by alignment directives. | |
2104 | No fool-proof, but better than either ignoring the problem or | |
2105 | doing heavy duty analysis of all the alignment markers in all | |
2106 | input sections. */ | |
2107 | section_alignment_glue = 0; | |
2108 | for (irel = internal_relocs; irel < irelend; irel++) | |
2109 | if (ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX | |
2110 | && irel->r_addend & RL78_RELAXA_ALIGN) | |
2111 | { | |
2112 | int this_glue = 1 << (irel->r_addend & RL78_RELAXA_ANUM); | |
2113 | ||
2114 | if (section_alignment_glue < this_glue) | |
2115 | section_alignment_glue = this_glue; | |
2116 | } | |
2117 | /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte | |
2118 | shrinkage. */ | |
2119 | section_alignment_glue *= 2; | |
2120 | ||
2121 | for (irel = internal_relocs; irel < irelend; irel++) | |
2122 | { | |
2123 | unsigned char *insn; | |
2124 | int nrelocs; | |
2125 | ||
2126 | /* The insns we care about are all marked with one of these. */ | |
2127 | if (ELF32_R_TYPE (irel->r_info) != R_RL78_RH_RELAX) | |
2128 | continue; | |
2129 | ||
2130 | if (irel->r_addend & RL78_RELAXA_ALIGN | |
2131 | || next_alignment == internal_relocs) | |
2132 | { | |
2133 | /* When we delete bytes, we need to maintain all the alignments | |
2134 | indicated. In addition, we need to be careful about relaxing | |
2135 | jumps across alignment boundaries - these displacements | |
2136 | *grow* when we delete bytes. For now, don't shrink | |
2137 | displacements across an alignment boundary, just in case. | |
2138 | Note that this only affects relocations to the same | |
2139 | section. */ | |
9cea966c DD |
2140 | next_alignment += 2; |
2141 | while (next_alignment < irelend | |
2142 | && (ELF32_R_TYPE (next_alignment->r_info) != R_RL78_RH_RELAX | |
2143 | || !(next_alignment->r_addend & RL78_RELAXA_ELIGN))) | |
2144 | next_alignment ++; | |
2145 | if (next_alignment >= irelend || next_alignment->r_offset == 0) | |
2146 | next_alignment = NULL; | |
2147 | } | |
2148 | ||
2149 | /* When we hit alignment markers, see if we've shrunk enough | |
2150 | before them to reduce the gap without violating the alignment | |
2151 | requirements. */ | |
2152 | if (irel->r_addend & RL78_RELAXA_ALIGN) | |
2153 | { | |
2154 | /* At this point, the next relocation *should* be the ELIGN | |
2155 | end marker. */ | |
2156 | Elf_Internal_Rela *erel = irel + 1; | |
2157 | unsigned int alignment, nbytes; | |
2158 | ||
2159 | if (ELF32_R_TYPE (erel->r_info) != R_RL78_RH_RELAX) | |
2160 | continue; | |
2161 | if (!(erel->r_addend & RL78_RELAXA_ELIGN)) | |
2162 | continue; | |
2163 | ||
2164 | alignment = 1 << (irel->r_addend & RL78_RELAXA_ANUM); | |
2165 | ||
2166 | if (erel->r_offset - irel->r_offset < alignment) | |
2167 | continue; | |
2168 | ||
2169 | nbytes = erel->r_offset - irel->r_offset; | |
2170 | nbytes /= alignment; | |
2171 | nbytes *= alignment; | |
2172 | ||
2173 | elf32_rl78_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment, | |
2174 | erel->r_offset == sec->size); | |
2175 | *again = TRUE; | |
2176 | ||
2177 | continue; | |
2178 | } | |
2179 | ||
2180 | if (irel->r_addend & RL78_RELAXA_ELIGN) | |
2181 | continue; | |
2182 | ||
2183 | insn = contents + irel->r_offset; | |
2184 | ||
2185 | nrelocs = irel->r_addend & RL78_RELAXA_RNUM; | |
2186 | ||
2187 | /* At this point, we have an insn that is a candidate for linker | |
2188 | relaxation. There are NRELOCS relocs following that may be | |
2189 | relaxed, although each reloc may be made of more than one | |
2190 | reloc entry (such as gp-rel symbols). */ | |
2191 | ||
2192 | /* Get the value of the symbol referred to by the reloc. Just | |
2193 | in case this is the last reloc in the list, use the RL's | |
2194 | addend to choose between this reloc (no addend) or the next | |
2195 | (yes addend, which means at least one following reloc). */ | |
2196 | ||
2197 | /* srel points to the "current" reloction for this insn - | |
2198 | actually the last reloc for a given operand, which is the one | |
2199 | we need to update. We check the relaxations in the same | |
2200 | order that the relocations happen, so we'll just push it | |
2201 | along as we go. */ | |
2202 | srel = irel; | |
2203 | ||
2204 | pc = sec->output_section->vma + sec->output_offset | |
2205 | + srel->r_offset; | |
2206 | ||
2207 | #define GET_RELOC \ | |
2208 | symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \ | |
2209 | pcrel = symval - pc + srel->r_addend; \ | |
2210 | nrelocs --; | |
2211 | ||
2212 | #define SNIPNR(offset, nbytes) \ | |
2213 | elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0); | |
2214 | #define SNIP(offset, nbytes, newtype) \ | |
2215 | SNIPNR (offset, nbytes); \ | |
2216 | srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype) | |
2217 | ||
2218 | /* The order of these bit tests must match the order that the | |
2219 | relocs appear in. Since we sorted those by offset, we can | |
2220 | predict them. */ | |
2221 | ||
2222 | /*----------------------------------------------------------------------*/ | |
2223 | /* EF ad BR $rel8 pcrel | |
2224 | ED al ah BR !abs16 abs | |
2225 | EE al ah BR $!rel16 pcrel | |
2226 | EC al ah as BR !!abs20 abs | |
2227 | ||
2228 | FD al ah CALL !abs16 abs | |
2229 | FE al ah CALL $!rel16 pcrel | |
2230 | FC al ah as CALL !!abs20 abs | |
2231 | ||
2232 | DC ad BC $rel8 | |
2233 | DE ad BNC $rel8 | |
2234 | DD ad BZ $rel8 | |
2235 | DF ad BNZ $rel8 | |
2236 | 61 C3 ad BH $rel8 | |
2237 | 61 D3 ad BNH $rel8 | |
2238 | 61 C8 EF ad SKC ; BR $rel8 | |
2239 | 61 D8 EF ad SKNC ; BR $rel8 | |
2240 | 61 E8 EF ad SKZ ; BR $rel8 | |
2241 | 61 F8 EF ad SKNZ ; BR $rel8 | |
2242 | 61 E3 EF ad SKH ; BR $rel8 | |
2243 | 61 F3 EF ad SKNH ; BR $rel8 | |
2244 | */ | |
2245 | ||
2246 | if (irel->r_addend & RL78_RELAXA_BRA) | |
2247 | { | |
2248 | GET_RELOC; | |
2249 | ||
2250 | switch (insn[0]) | |
2251 | { | |
2252 | case 0xec: /* BR !!abs20 */ | |
2253 | ||
2254 | if (pcrel < 127 | |
2255 | && pcrel > -127) | |
2256 | { | |
2257 | insn[0] = 0xef; | |
2258 | insn[1] = pcrel; | |
2259 | SNIP (2, 2, R_RL78_DIR8S_PCREL); | |
2260 | *again = TRUE; | |
2261 | } | |
2262 | else if (symval < 65536) | |
2263 | { | |
2264 | insn[0] = 0xed; | |
2265 | insn[1] = symval & 0xff; | |
2266 | insn[2] = symval >> 8; | |
2267 | SNIP (2, 1, R_RL78_DIR16S); | |
2268 | *again = TRUE; | |
2269 | } | |
2270 | else if (pcrel < 32767 | |
2271 | && pcrel > -32767) | |
2272 | { | |
2273 | insn[0] = 0xee; | |
2274 | insn[1] = pcrel & 0xff; | |
2275 | insn[2] = pcrel >> 8; | |
2276 | SNIP (2, 1, R_RL78_DIR16S_PCREL); | |
2277 | *again = TRUE; | |
2278 | } | |
2279 | break; | |
2280 | ||
2281 | case 0xee: /* BR $!pcrel16 */ | |
2282 | case 0xed: /* BR $!abs16 */ | |
2283 | if (pcrel < 127 | |
2284 | && pcrel > -127) | |
2285 | { | |
2286 | insn[0] = 0xef; | |
2287 | insn[1] = pcrel; | |
2288 | SNIP (2, 1, R_RL78_DIR8S_PCREL); | |
2289 | *again = TRUE; | |
2290 | } | |
2291 | break; | |
2292 | ||
2293 | case 0xfc: /* CALL !!abs20 */ | |
2294 | if (symval < 65536) | |
2295 | { | |
2296 | insn[0] = 0xfd; | |
2297 | insn[1] = symval & 0xff; | |
2298 | insn[2] = symval >> 8; | |
2299 | SNIP (2, 1, R_RL78_DIR16S); | |
2300 | *again = TRUE; | |
2301 | } | |
2302 | else if (pcrel < 32767 | |
2303 | && pcrel > -32767) | |
2304 | { | |
2305 | insn[0] = 0xfe; | |
2306 | insn[1] = pcrel & 0xff; | |
2307 | insn[2] = pcrel >> 8; | |
2308 | SNIP (2, 1, R_RL78_DIR16S_PCREL); | |
2309 | *again = TRUE; | |
2310 | } | |
2311 | break; | |
2312 | ||
2313 | case 0x61: /* PREFIX */ | |
2314 | /* For SKIP/BR, we change the BR opcode and delete the | |
2315 | SKIP. That way, we don't have to find and change the | |
2316 | relocation for the BR. */ | |
2317 | switch (insn[1]) | |
2318 | { | |
2319 | case 0xc8: /* SKC */ | |
2320 | if (insn[2] == 0xef) | |
2321 | { | |
2322 | insn[2] = 0xde; /* BNC */ | |
2323 | SNIPNR (0, 2); | |
2324 | } | |
2325 | break; | |
2326 | ||
2327 | case 0xd8: /* SKNC */ | |
2328 | if (insn[2] == 0xef) | |
2329 | { | |
2330 | insn[2] = 0xdc; /* BC */ | |
2331 | SNIPNR (0, 2); | |
2332 | } | |
2333 | break; | |
2334 | ||
2335 | case 0xe8: /* SKZ */ | |
2336 | if (insn[2] == 0xef) | |
2337 | { | |
2338 | insn[2] = 0xdf; /* BNZ */ | |
2339 | SNIPNR (0, 2); | |
2340 | } | |
2341 | break; | |
2342 | ||
2343 | case 0xf8: /* SKNZ */ | |
2344 | if (insn[2] == 0xef) | |
2345 | { | |
2346 | insn[2] = 0xdd; /* BZ */ | |
2347 | SNIPNR (0, 2); | |
2348 | } | |
2349 | break; | |
2350 | ||
2351 | case 0xe3: /* SKH */ | |
2352 | if (insn[2] == 0xef) | |
2353 | { | |
2354 | insn[2] = 0xd3; /* BNH */ | |
2355 | SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */ | |
2356 | } | |
2357 | break; | |
2358 | ||
2359 | case 0xf3: /* SKNH */ | |
2360 | if (insn[2] == 0xef) | |
2361 | { | |
2362 | insn[2] = 0xc3; /* BH */ | |
2363 | SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */ | |
2364 | } | |
2365 | break; | |
2366 | } | |
2367 | break; | |
2368 | } | |
2369 | ||
2370 | } | |
2371 | ||
2372 | if (irel->r_addend & RL78_RELAXA_ADDR16) | |
2373 | { | |
2374 | /*----------------------------------------------------------------------*/ | |
2375 | /* Some insns have both a 16-bit address operand and an 8-bit | |
2376 | variant if the address is within a special range: | |
2377 | ||
2378 | Address 16-bit operand SADDR range SFR range | |
2379 | FFF00-FFFFF 0xff00-0xffff 0x00-0xff | |
2380 | FFE20-FFF1F 0xfe20-0xff1f 0x00-0xff | |
2381 | ||
2382 | The RELAX_ADDR16[] array has the insn encodings for the | |
2383 | 16-bit operand version, as well as the SFR and SADDR | |
2384 | variants. We only need to replace the encodings and | |
2385 | adjust the operand. | |
2386 | ||
2387 | Note: we intentionally do not attempt to decode and skip | |
2388 | any ES: prefix, as adding ES: means the addr16 (likely) | |
2389 | no longer points to saddr/sfr space. | |
2390 | */ | |
2391 | ||
2392 | int is_sfr; | |
2393 | int is_saddr; | |
2394 | int idx; | |
2395 | int poff; | |
2396 | ||
2397 | GET_RELOC; | |
2398 | ||
23503fba JK |
2399 | printf ("relax_addr16 detected, " |
2400 | "symval 0x%" BFD_VMA_FMT "x %02x %02x\n", | |
2401 | symval, insn[0], insn[1]); | |
9cea966c DD |
2402 | |
2403 | if (0xffe20 <= symval && symval <= 0xfffff) | |
2404 | { | |
2405 | ||
2406 | is_saddr = (0xffe20 <= symval && symval <= 0xfff1f); | |
2407 | is_sfr = (0xfff00 <= symval && symval <= 0xfffff); | |
2408 | ||
2409 | for (idx = 0; relax_addr16[idx].insn != -1; idx ++) | |
2410 | { | |
2411 | if (relax_addr16[idx].prefix != -1 | |
2412 | && insn[0] == relax_addr16[idx].prefix | |
2413 | && insn[1] == relax_addr16[idx].insn) | |
2414 | { | |
2415 | poff = 1; | |
2416 | } | |
2417 | else if (relax_addr16[idx].prefix == -1 | |
2418 | && insn[0] == relax_addr16[idx].insn) | |
2419 | { | |
2420 | poff = 0; | |
2421 | } | |
2422 | else | |
2423 | continue; | |
2424 | ||
2425 | /* We have a matched insn, and poff is 0 or 1 depending | |
2426 | on the base pattern size. */ | |
2427 | ||
2428 | if (is_sfr && relax_addr16[idx].insn_for_sfr != -1) | |
2429 | { | |
2430 | insn[poff] = relax_addr16[idx].insn_for_sfr; | |
2431 | SNIP (poff+2, 1, R_RL78_RH_SFR); | |
2432 | printf(" - replaced by SFR\n"); | |
2433 | } | |
2434 | ||
2435 | else if (is_saddr && relax_addr16[idx].insn_for_saddr != -1) | |
2436 | { | |
2437 | insn[poff] = relax_addr16[idx].insn_for_saddr; | |
2438 | SNIP (poff+2, 1, R_RL78_RH_SADDR); | |
2439 | printf(" - replaced by SADDR\n"); | |
2440 | } | |
2441 | ||
2442 | } | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | /*----------------------------------------------------------------------*/ | |
2447 | ||
2448 | } | |
2449 | ||
2450 | return TRUE; | |
2451 | ||
2452 | error_return: | |
2453 | if (free_relocs != NULL) | |
2454 | free (free_relocs); | |
2455 | ||
2456 | if (free_contents != NULL) | |
2457 | free (free_contents); | |
2458 | ||
2459 | if (shndx_buf != NULL) | |
2460 | { | |
2461 | shndx_hdr->contents = NULL; | |
2462 | free (shndx_buf); | |
2463 | } | |
2464 | ||
2465 | if (free_intsyms != NULL) | |
2466 | free (free_intsyms); | |
2467 | ||
99c513f6 DD |
2468 | return TRUE; |
2469 | } | |
2470 | ||
2471 | \f | |
2472 | ||
2473 | #define ELF_ARCH bfd_arch_rl78 | |
2474 | #define ELF_MACHINE_CODE EM_RL78 | |
2475 | #define ELF_MAXPAGESIZE 0x1000 | |
2476 | ||
2477 | #define TARGET_LITTLE_SYM bfd_elf32_rl78_vec | |
2478 | #define TARGET_LITTLE_NAME "elf32-rl78" | |
2479 | ||
2480 | #define elf_info_to_howto_rel NULL | |
2481 | #define elf_info_to_howto rl78_info_to_howto_rela | |
2482 | #define elf_backend_object_p rl78_elf_object_p | |
2483 | #define elf_backend_relocate_section rl78_elf_relocate_section | |
2484 | #define elf_symbol_leading_char ('_') | |
2485 | #define elf_backend_can_gc_sections 1 | |
2486 | ||
2487 | #define bfd_elf32_bfd_reloc_type_lookup rl78_reloc_type_lookup | |
2488 | #define bfd_elf32_bfd_reloc_name_lookup rl78_reloc_name_lookup | |
2489 | #define bfd_elf32_bfd_set_private_flags rl78_elf_set_private_flags | |
2490 | #define bfd_elf32_bfd_merge_private_bfd_data rl78_elf_merge_private_bfd_data | |
2491 | #define bfd_elf32_bfd_print_private_bfd_data rl78_elf_print_private_bfd_data | |
2492 | ||
2493 | #define bfd_elf32_bfd_relax_section rl78_elf_relax_section | |
2494 | #define elf_backend_check_relocs rl78_elf_check_relocs | |
2495 | #define elf_backend_always_size_sections \ | |
2496 | rl78_elf_always_size_sections | |
2497 | #define elf_backend_finish_dynamic_sections \ | |
2498 | rl78_elf_finish_dynamic_sections | |
2499 | ||
2500 | #include "elf32-target.h" |