]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-ip2k.c
bfd/
[thirdparty/binutils-gdb.git] / bfd / elf32-ip2k.c
1 /* Ubicom IP2xxx specific support for 32-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/ip2k.h"
25
26 /* Struct used to pass miscellaneous paramaters which
27 helps to avoid overly long parameter lists. */
28 struct misc
29 {
30 Elf_Internal_Shdr * symtab_hdr;
31 Elf_Internal_Rela * irelbase;
32 bfd_byte * contents;
33 Elf_Internal_Sym * isymbuf;
34 };
35
36 struct ip2k_opcode
37 {
38 unsigned short opcode;
39 unsigned short mask;
40 };
41
42 /* Prototypes. */
43 static reloc_howto_type *ip2k_reloc_type_lookup
44 PARAMS ((bfd *, bfd_reloc_code_real_type));
45 static int ip2k_is_opcode
46 PARAMS ((bfd_byte *, const struct ip2k_opcode *));
47 static bfd_vma symbol_value
48 PARAMS ((bfd *, Elf_Internal_Shdr *, Elf_Internal_Sym *,
49 Elf_Internal_Rela *));
50 static void ip2k_get_mem
51 PARAMS ((bfd *, bfd_byte *, int, bfd_byte *));
52 static bfd_vma ip2k_nominal_page_bits
53 PARAMS ((bfd *, asection *, bfd_vma, bfd_byte *));
54 static bfd_boolean ip2k_test_page_insn
55 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, struct misc *));
56 static bfd_boolean ip2k_delete_page_insn
57 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_boolean *, struct misc *));
58 static int ip2k_is_switch_table_128
59 PARAMS ((bfd *, asection *, bfd_vma, bfd_byte *));
60 static bfd_boolean ip2k_relax_switch_table_128
61 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_boolean *, struct misc *));
62 static int ip2k_is_switch_table_256
63 PARAMS ((bfd *, asection *, bfd_vma, bfd_byte *));
64 static bfd_boolean ip2k_relax_switch_table_256
65 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_boolean *, struct misc *));
66 static bfd_boolean ip2k_elf_relax_section
67 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
68 static bfd_boolean ip2k_elf_relax_section_page
69 PARAMS ((bfd *, asection *, bfd_boolean *, struct misc *, unsigned long, unsigned long));
70 static void adjust_all_relocations
71 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int, int));
72 static bfd_boolean ip2k_elf_relax_delete_bytes
73 PARAMS ((bfd *, asection *, bfd_vma, int));
74 static void ip2k_info_to_howto_rela
75 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
76 static bfd_reloc_status_type ip2k_final_link_relocate
77 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
78 Elf_Internal_Rela *, bfd_vma));
79 static bfd_boolean ip2k_elf_relocate_section
80 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
81 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
82 static asection *ip2k_elf_gc_mark_hook
83 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
84 struct elf_link_hash_entry *, Elf_Internal_Sym *));
85 static bfd_boolean ip2k_elf_gc_sweep_hook
86 PARAMS ((bfd *, struct bfd_link_info *, asection *,
87 const Elf_Internal_Rela *));
88
89 static bfd_boolean ip2k_relaxed = FALSE;
90
91 static const struct ip2k_opcode ip2k_page_opcode[] =
92 {
93 {0x0010, 0xFFF8}, /* page */
94 {0x0000, 0x0000},
95 };
96
97 #define IS_PAGE_OPCODE(code) \
98 ip2k_is_opcode (code, ip2k_page_opcode)
99
100 static const struct ip2k_opcode ip2k_jmp_opcode[] =
101 {
102 {0xE000, 0xE000}, /* jmp */
103 {0x0000, 0x0000},
104 };
105
106 #define IS_JMP_OPCODE(code) \
107 ip2k_is_opcode (code, ip2k_jmp_opcode)
108
109 static const struct ip2k_opcode ip2k_call_opcode[] =
110 {
111 {0xC000, 0xE000}, /* call */
112 {0x0000, 0x0000},
113 };
114
115 #define IS_CALL_OPCODE(code) \
116 ip2k_is_opcode (code, ip2k_call_opcode)
117
118 static const struct ip2k_opcode ip2k_snc_opcode[] =
119 {
120 {0xA00B, 0xFFFF}, /* snc */
121 {0x0000, 0x0000},
122 };
123
124 #define IS_SNC_OPCODE(code) \
125 ip2k_is_opcode (code, ip2k_snc_opcode)
126
127 static const struct ip2k_opcode ip2k_inc_1sp_opcode[] =
128 {
129 {0x2B81, 0xFFFF}, /* inc 1(SP) */
130 {0x0000, 0x0000},
131 };
132
133 #define IS_INC_1SP_OPCODE(code) \
134 ip2k_is_opcode (code, ip2k_inc_1sp_opcode)
135
136 static const struct ip2k_opcode ip2k_add_2sp_w_opcode[] =
137 {
138 {0x1F82, 0xFFFF}, /* add 2(SP),w */
139 {0x0000, 0x0000},
140 };
141
142 #define IS_ADD_2SP_W_OPCODE(code) \
143 ip2k_is_opcode (code, ip2k_add_2sp_w_opcode)
144
145 static const struct ip2k_opcode ip2k_add_w_wreg_opcode[] =
146 {
147 {0x1C0A, 0xFFFF}, /* add w,wreg */
148 {0x1E0A, 0xFFFF}, /* add wreg,w */
149 {0x0000, 0x0000},
150 };
151
152 #define IS_ADD_W_WREG_OPCODE(code) \
153 ip2k_is_opcode (code, ip2k_add_w_wreg_opcode)
154
155 static const struct ip2k_opcode ip2k_add_pcl_w_opcode[] =
156 {
157 {0x1E09, 0xFFFF}, /* add pcl,w */
158 {0x0000, 0x0000},
159 };
160
161 #define IS_ADD_PCL_W_OPCODE(code) \
162 ip2k_is_opcode (code, ip2k_add_pcl_w_opcode)
163
164 static const struct ip2k_opcode ip2k_skip_opcodes[] =
165 {
166 {0xB000, 0xF000}, /* sb */
167 {0xA000, 0xF000}, /* snb */
168 {0x7600, 0xFE00}, /* cse/csne #lit */
169 {0x5800, 0xFC00}, /* incsnz */
170 {0x4C00, 0xFC00}, /* decsnz */
171 {0x4000, 0xFC00}, /* cse/csne */
172 {0x3C00, 0xFC00}, /* incsz */
173 {0x2C00, 0xFC00}, /* decsz */
174 {0x0000, 0x0000},
175 };
176
177 #define IS_SKIP_OPCODE(code) \
178 ip2k_is_opcode (code, ip2k_skip_opcodes)
179
180 /* Relocation tables. */
181 static reloc_howto_type ip2k_elf_howto_table [] =
182 {
183 #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \
184 HOWTO(t, /* type */ \
185 rs, /* rightshift */ \
186 s, /* size (0 = byte, 1 = short, 2 = long) */ \
187 bs, /* bitsize */ \
188 pr, /* pc_relative */ \
189 bp, /* bitpos */ \
190 complain_overflow_dont,/* complain_on_overflow */ \
191 bfd_elf_generic_reloc,/* special_function */ \
192 name, /* name */ \
193 FALSE, /* partial_inplace */ \
194 sm, /* src_mask */ \
195 dm, /* dst_mask */ \
196 pr) /* pcrel_offset */
197
198 /* This reloc does nothing. */
199 IP2K_HOWTO (R_IP2K_NONE, 0,2,32, FALSE, 0, "R_IP2K_NONE", 0, 0),
200 /* A 16 bit absolute relocation. */
201 IP2K_HOWTO (R_IP2K_16, 0,1,16, FALSE, 0, "R_IP2K_16", 0, 0xffff),
202 /* A 32 bit absolute relocation. */
203 IP2K_HOWTO (R_IP2K_32, 0,2,32, FALSE, 0, "R_IP2K_32", 0, 0xffffffff),
204 /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */
205 IP2K_HOWTO (R_IP2K_FR9, 0,1,9, FALSE, 0, "R_IP2K_FR9", 0, 0x00ff),
206 /* A 4-bit data relocation. */
207 IP2K_HOWTO (R_IP2K_BANK, 8,1,4, FALSE, 0, "R_IP2K_BANK", 0, 0x000f),
208 /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */
209 IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, FALSE, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff),
210 /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */
211 IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, FALSE, 0, "R_IP2K_PAGE3", 0, 0x0007),
212 /* Two 8-bit data relocations. */
213 IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, FALSE, 0, "R_IP2K_LO8DATA", 0, 0x00ff),
214 IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, FALSE, 0, "R_IP2K_HI8DATA", 0, 0x00ff),
215 /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */
216 IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, FALSE, 0, "R_IP2K_LO8INSN", 0, 0x00ff),
217 IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, FALSE, 0, "R_IP2K_HI8INSN", 0, 0x00ff),
218
219 /* Special 1 bit relocation for SKIP instructions. */
220 IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, FALSE, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000),
221 /* 16 bit word address. */
222 IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, FALSE, 0, "R_IP2K_TEXT", 0, 0xffff),
223 /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */
224 IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, FALSE, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f),
225 /* Bits 23:16 of an address. */
226 IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, FALSE, 0, "R_IP2K_EX8DATA", 0, 0x00ff),
227 };
228
229
230 /* Map BFD reloc types to IP2K ELF reloc types. */
231 static reloc_howto_type *
232 ip2k_reloc_type_lookup (abfd, code)
233 bfd * abfd ATTRIBUTE_UNUSED;
234 bfd_reloc_code_real_type code;
235 {
236 /* Note that the ip2k_elf_howto_table is indxed by the R_
237 constants. Thus, the order that the howto records appear in the
238 table *must* match the order of the relocation types defined in
239 include/elf/ip2k.h. */
240
241 switch (code)
242 {
243 case BFD_RELOC_NONE:
244 return &ip2k_elf_howto_table[ (int) R_IP2K_NONE];
245 case BFD_RELOC_16:
246 return &ip2k_elf_howto_table[ (int) R_IP2K_16];
247 case BFD_RELOC_32:
248 return &ip2k_elf_howto_table[ (int) R_IP2K_32];
249 case BFD_RELOC_IP2K_FR9:
250 return &ip2k_elf_howto_table[ (int) R_IP2K_FR9];
251 case BFD_RELOC_IP2K_BANK:
252 return &ip2k_elf_howto_table[ (int) R_IP2K_BANK];
253 case BFD_RELOC_IP2K_ADDR16CJP:
254 return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP];
255 case BFD_RELOC_IP2K_PAGE3:
256 return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3];
257 case BFD_RELOC_IP2K_LO8DATA:
258 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA];
259 case BFD_RELOC_IP2K_HI8DATA:
260 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA];
261 case BFD_RELOC_IP2K_LO8INSN:
262 return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN];
263 case BFD_RELOC_IP2K_HI8INSN:
264 return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN];
265 case BFD_RELOC_IP2K_PC_SKIP:
266 return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP];
267 case BFD_RELOC_IP2K_TEXT:
268 return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
269 case BFD_RELOC_IP2K_FR_OFFSET:
270 return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET];
271 case BFD_RELOC_IP2K_EX8DATA:
272 return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA];
273 default:
274 /* Pacify gcc -Wall. */
275 return NULL;
276 }
277 return NULL;
278 }
279
280 static void
281 ip2k_get_mem (abfd, addr, length, ptr)
282 bfd *abfd ATTRIBUTE_UNUSED;
283 bfd_byte *addr;
284 int length;
285 bfd_byte *ptr;
286 {
287 while (length --)
288 * ptr ++ = bfd_get_8 (abfd, addr ++);
289 }
290
291 static bfd_boolean
292 ip2k_is_opcode (code, opcodes)
293 bfd_byte *code;
294 const struct ip2k_opcode *opcodes;
295 {
296 unsigned short insn = (code[0] << 8) | code[1];
297
298 while (opcodes->mask != 0)
299 {
300 if ((insn & opcodes->mask) == opcodes->opcode)
301 return TRUE;
302
303 opcodes ++;
304 }
305
306 return FALSE;
307 }
308
309 #define PAGENO(ABSADDR) ((ABSADDR) & 0xFFFFC000)
310 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
311
312 #define UNDEFINED_SYMBOL (~(bfd_vma)0)
313
314 /* Return the value of the symbol associated with the relocation IREL. */
315
316 static bfd_vma
317 symbol_value (abfd, symtab_hdr, isymbuf, irel)
318 bfd *abfd;
319 Elf_Internal_Shdr *symtab_hdr;
320 Elf_Internal_Sym *isymbuf;
321 Elf_Internal_Rela *irel;
322 {
323 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
324 {
325 Elf_Internal_Sym *isym;
326 asection *sym_sec;
327
328 isym = isymbuf + ELF32_R_SYM (irel->r_info);
329 if (isym->st_shndx == SHN_UNDEF)
330 sym_sec = bfd_und_section_ptr;
331 else if (isym->st_shndx == SHN_ABS)
332 sym_sec = bfd_abs_section_ptr;
333 else if (isym->st_shndx == SHN_COMMON)
334 sym_sec = bfd_com_section_ptr;
335 else
336 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
337
338 return isym->st_value + BASEADDR (sym_sec);
339 }
340 else
341 {
342 unsigned long indx;
343 struct elf_link_hash_entry *h;
344
345 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
346 h = elf_sym_hashes (abfd)[indx];
347 BFD_ASSERT (h != NULL);
348
349 if (h->root.type != bfd_link_hash_defined
350 && h->root.type != bfd_link_hash_defweak)
351 return UNDEFINED_SYMBOL;
352
353 return (h->root.u.def.value + BASEADDR (h->root.u.def.section));
354 }
355 }
356
357 /* Returns the expected page state for the given instruction not including
358 the effect of page instructions. */
359
360 static bfd_vma
361 ip2k_nominal_page_bits (abfd, sec, addr, contents)
362 bfd *abfd ATTRIBUTE_UNUSED;
363 asection *sec;
364 bfd_vma addr;
365 bfd_byte *contents;
366 {
367 bfd_vma page = PAGENO (BASEADDR (sec) + addr);
368
369 /* Check if section flows into this page. If not then the page
370 bits are assumed to match the PC. This will be true unless
371 the user has a page instruction without a call/jump, in which
372 case they are on their own. */
373 if (PAGENO (BASEADDR (sec)) == page)
374 return page;
375
376 /* Section flows across page boundary. The page bits should match
377 the PC unless there is a possible flow from the previous page,
378 in which case it is not possible to determine the value of the
379 page bits. */
380 while (PAGENO (BASEADDR (sec) + addr - 2) == page)
381 {
382 bfd_byte code[2];
383
384 addr -= 2;
385 ip2k_get_mem (abfd, contents + addr, 2, code);
386 if (!IS_PAGE_OPCODE (code))
387 continue;
388
389 /* Found a page instruction, check if jump table. */
390 if (ip2k_is_switch_table_128 (abfd, sec, addr, contents) != -1)
391 /* Jump table => page is conditional. */
392 continue;
393
394 if (ip2k_is_switch_table_256 (abfd, sec, addr, contents) != -1)
395 /* Jump table => page is conditional. */
396 continue;
397
398 /* Found a page instruction, check if conditional. */
399 if (addr >= 2)
400 {
401 ip2k_get_mem (abfd, contents + addr - 2, 2, code);
402 if (IS_SKIP_OPCODE (code))
403 /* Page is conditional. */
404 continue;
405 }
406
407 /* Unconditional page instruction => page bits should be correct. */
408 return page;
409 }
410
411 /* Flow from previous page => page bits are impossible to determine. */
412 return 0;
413 }
414
415 static bfd_boolean
416 ip2k_test_page_insn (abfd, sec, irel, misc)
417 bfd *abfd ATTRIBUTE_UNUSED;
418 asection *sec;
419 Elf_Internal_Rela *irel;
420 struct misc *misc;
421 {
422 bfd_vma symval;
423
424 /* Get the value of the symbol referred to by the reloc. */
425 symval = symbol_value (abfd, misc->symtab_hdr, misc->isymbuf, irel);
426 if (symval == UNDEFINED_SYMBOL)
427 /* This appears to be a reference to an undefined
428 symbol. Just ignore it--it will be caught by the
429 regular reloc processing. */
430 return FALSE;
431
432 /* Test if we can delete this page instruction. */
433 if (PAGENO (symval + irel->r_addend) !=
434 ip2k_nominal_page_bits (abfd, sec, irel->r_offset, misc->contents))
435 return FALSE;
436
437 return TRUE;
438 }
439
440 static bfd_boolean
441 ip2k_delete_page_insn (abfd, sec, irel, again, misc)
442 bfd *abfd ATTRIBUTE_UNUSED;
443 asection *sec;
444 Elf_Internal_Rela *irel;
445 bfd_boolean *again;
446 struct misc *misc;
447 {
448 /* Note that we've changed the relocs, section contents, etc. */
449 elf_section_data (sec)->relocs = misc->irelbase;
450 elf_section_data (sec)->this_hdr.contents = misc->contents;
451 misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
452
453 /* Fix the relocation's type. */
454 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_IP2K_NONE);
455
456 /* Delete the PAGE insn. */
457 if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
458 return FALSE;
459
460 /* Modified => will need to iterate relaxation again. */
461 *again = TRUE;
462
463 return TRUE;
464 }
465
466 /* Determine if the instruction sequence matches that for
467 the prologue of a switch dispatch table with fewer than
468 128 entries.
469
470 sc
471 page $nnn0
472 jmp $nnn0
473 add w,wreg
474 add pcl,w
475 addr=>
476 page $nnn1
477 jmp $nnn1
478 page $nnn2
479 jmp $nnn2
480 ...
481 page $nnnN
482 jmp $nnnN
483
484 After relaxation.
485 sc
486 page $nnn0
487 jmp $nnn0
488 add pcl,w
489 addr=>
490 jmp $nnn1
491 jmp $nnn2
492 ...
493 jmp $nnnN */
494
495 static int
496 ip2k_is_switch_table_128 (abfd, sec, addr, contents)
497 bfd *abfd ATTRIBUTE_UNUSED;
498 asection *sec;
499 bfd_vma addr;
500 bfd_byte *contents;
501 {
502 bfd_byte code[4];
503 int index = 0;
504
505 /* Check current page-jmp. */
506 if (addr + 4 > sec->size)
507 return -1;
508
509 ip2k_get_mem (abfd, contents + addr, 4, code);
510
511 if ((! IS_PAGE_OPCODE (code + 0))
512 || (! IS_JMP_OPCODE (code + 2)))
513 return -1;
514
515 /* Search back. */
516 while (1)
517 {
518 if (addr < 4)
519 return -1;
520
521 /* Check previous 2 instructions. */
522 ip2k_get_mem (abfd, contents + addr - 4, 4, code);
523 if ((IS_ADD_W_WREG_OPCODE (code + 0))
524 && (IS_ADD_PCL_W_OPCODE (code + 2)))
525 return index;
526
527 if ((! IS_PAGE_OPCODE (code + 0))
528 || (! IS_JMP_OPCODE (code + 2)))
529 return -1;
530
531 index++;
532 addr -= 4;
533 }
534 }
535
536 static bfd_boolean
537 ip2k_relax_switch_table_128 (abfd, sec, irel, again, misc)
538 bfd *abfd ATTRIBUTE_UNUSED;
539 asection *sec;
540 Elf_Internal_Rela *irel;
541 bfd_boolean *again;
542 struct misc *misc;
543 {
544 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
545 Elf_Internal_Rela *ireltest = irel;
546 bfd_byte code[4];
547 bfd_vma addr;
548
549 /* Test all page instructions. */
550 addr = irel->r_offset;
551 while (1)
552 {
553 if (addr + 4 > sec->size)
554 break;
555
556 ip2k_get_mem (abfd, misc->contents + addr, 4, code);
557 if ((! IS_PAGE_OPCODE (code + 0))
558 || (! IS_JMP_OPCODE (code + 2)))
559 break;
560
561 /* Validate relocation entry (every entry should have a matching
562 relocation entry). */
563 if (ireltest >= irelend)
564 {
565 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
566 return FALSE;
567 }
568
569 if (ireltest->r_offset != addr)
570 {
571 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
572 return FALSE;
573 }
574
575 if (! ip2k_test_page_insn (abfd, sec, ireltest, misc))
576 /* Un-removable page insn => nothing can be done. */
577 return TRUE;
578
579 addr += 4;
580 ireltest += 2;
581 }
582
583 /* Relaxable. Adjust table header. */
584 ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 4, code);
585 if ((! IS_ADD_W_WREG_OPCODE (code + 0))
586 || (! IS_ADD_PCL_W_OPCODE (code + 2)))
587 {
588 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
589 return FALSE;
590 }
591
592 if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset - 4, 2))
593 return FALSE;
594
595 *again = TRUE;
596
597 /* Delete all page instructions in table. */
598 while (irel < ireltest)
599 {
600 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
601 return FALSE;
602 irel += 2;
603 }
604
605 return TRUE;
606 }
607
608 /* Determine if the instruction sequence matches that for
609 the prologue switch dispatch table with fewer than
610 256 entries but more than 127.
611
612 Before relaxation.
613 push %lo8insn(label) ; Push address of table
614 push %hi8insn(label)
615 add w,wreg ; index*2 => offset
616 snc ; CARRY SET?
617 inc 1(sp) ; Propagate MSB into table address
618 add 2(sp),w ; Add low bits of offset to table address
619 snc ; and handle any carry-out
620 inc 1(sp)
621 addr=>
622 page __indjmp ; Do an indirect jump to that location
623 jmp __indjmp
624 label: ; case dispatch table starts here
625 page $nnn1
626 jmp $nnn1
627 page $nnn2
628 jmp $nnn2
629 ...
630 page $nnnN
631 jmp $nnnN
632
633 After relaxation.
634 push %lo8insn(label) ; Push address of table
635 push %hi8insn(label)
636 add 2(sp),w ; Add low bits of offset to table address
637 snc ; and handle any carry-out
638 inc 1(sp)
639 addr=>
640 page __indjmp ; Do an indirect jump to that location
641 jmp __indjmp
642 label: ; case dispatch table starts here
643 jmp $nnn1
644 jmp $nnn2
645 ...
646 jmp $nnnN */
647
648 static int
649 ip2k_is_switch_table_256 (abfd, sec, addr, contents)
650 bfd *abfd ATTRIBUTE_UNUSED;
651 asection *sec;
652 bfd_vma addr;
653 bfd_byte *contents;
654 {
655 bfd_byte code[16];
656 int index = 0;
657
658 /* Check current page-jmp. */
659 if (addr + 4 > sec->size)
660 return -1;
661
662 ip2k_get_mem (abfd, contents + addr, 4, code);
663 if ((! IS_PAGE_OPCODE (code + 0))
664 || (! IS_JMP_OPCODE (code + 2)))
665 return -1;
666
667 /* Search back. */
668 while (1)
669 {
670 if (addr < 16)
671 return -1;
672
673 /* Check previous 8 instructions. */
674 ip2k_get_mem (abfd, contents + addr - 16, 16, code);
675 if ((IS_ADD_W_WREG_OPCODE (code + 0))
676 && (IS_SNC_OPCODE (code + 2))
677 && (IS_INC_1SP_OPCODE (code + 4))
678 && (IS_ADD_2SP_W_OPCODE (code + 6))
679 && (IS_SNC_OPCODE (code + 8))
680 && (IS_INC_1SP_OPCODE (code + 10))
681 && (IS_PAGE_OPCODE (code + 12))
682 && (IS_JMP_OPCODE (code + 14)))
683 return index;
684
685 if ((IS_ADD_W_WREG_OPCODE (code + 2))
686 && (IS_SNC_OPCODE (code + 4))
687 && (IS_INC_1SP_OPCODE (code + 6))
688 && (IS_ADD_2SP_W_OPCODE (code + 8))
689 && (IS_SNC_OPCODE (code + 10))
690 && (IS_INC_1SP_OPCODE (code + 12))
691 && (IS_JMP_OPCODE (code + 14)))
692 return index;
693
694 if ((! IS_PAGE_OPCODE (code + 0))
695 || (! IS_JMP_OPCODE (code + 2)))
696 return -1;
697
698 index++;
699 addr -= 4;
700 }
701 }
702
703 static bfd_boolean
704 ip2k_relax_switch_table_256 (abfd, sec, irel, again, misc)
705 bfd *abfd ATTRIBUTE_UNUSED;
706 asection *sec;
707 Elf_Internal_Rela *irel;
708 bfd_boolean *again;
709 struct misc *misc;
710 {
711 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
712 Elf_Internal_Rela *ireltest = irel;
713 bfd_byte code[12];
714 bfd_vma addr;
715
716 /* Test all page instructions. */
717 addr = irel->r_offset;
718
719 while (1)
720 {
721 if (addr + 4 > sec->size)
722 break;
723
724 ip2k_get_mem (abfd, misc->contents + addr, 4, code);
725
726 if ((! IS_PAGE_OPCODE (code + 0))
727 || (! IS_JMP_OPCODE (code + 2)))
728 break;
729
730 /* Validate relocation entry (every entry should have a matching
731 relocation entry). */
732 if (ireltest >= irelend)
733 {
734 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
735 return FALSE;
736 }
737
738 if (ireltest->r_offset != addr)
739 {
740 _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
741 return FALSE;
742 }
743
744 if (!ip2k_test_page_insn (abfd, sec, ireltest, misc))
745 /* Un-removable page insn => nothing can be done. */
746 return TRUE;
747
748 addr += 4;
749 ireltest += 2;
750 }
751
752 /* Relaxable. Adjust table header. */
753 ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 2, code);
754 if (IS_PAGE_OPCODE (code))
755 addr = irel->r_offset - 16;
756 else
757 addr = irel->r_offset - 14;
758
759 ip2k_get_mem (abfd, misc->contents + addr, 12, code);
760 if ((!IS_ADD_W_WREG_OPCODE (code + 0))
761 || (!IS_SNC_OPCODE (code + 2))
762 || (!IS_INC_1SP_OPCODE (code + 4))
763 || (!IS_ADD_2SP_W_OPCODE (code + 6))
764 || (!IS_SNC_OPCODE (code + 8))
765 || (!IS_INC_1SP_OPCODE (code + 10)))
766 {
767 _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
768 return FALSE;
769 }
770
771 /* Delete first 3 opcodes. */
772 if (!ip2k_elf_relax_delete_bytes (abfd, sec, addr + 0, 6))
773 return FALSE;
774
775 *again = TRUE;
776
777 /* Delete all page instructions in table. */
778 while (irel < ireltest)
779 {
780 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
781 return FALSE;
782 irel += 2;
783 }
784
785 return TRUE;
786 }
787
788 /* This function handles relaxing for the ip2k.
789
790 Principle: Start with the first page and remove page instructions that
791 are not require on this first page. By removing page instructions more
792 code will fit into this page - repeat until nothing more can be achieved
793 for this page. Move on to the next page.
794
795 Processing the pages one at a time from the lowest page allows a removal
796 only policy to be used - pages can be removed but are never reinserted. */
797
798 static bfd_boolean
799 ip2k_elf_relax_section (abfd, sec, link_info, again)
800 bfd *abfd;
801 asection *sec;
802 struct bfd_link_info *link_info;
803 bfd_boolean *again;
804 {
805 Elf_Internal_Shdr *symtab_hdr;
806 Elf_Internal_Rela *internal_relocs;
807 bfd_byte *contents = NULL;
808 Elf_Internal_Sym *isymbuf = NULL;
809 static asection * first_section = NULL;
810 static unsigned long search_addr;
811 static unsigned long page_start = 0;
812 static unsigned long page_end = 0;
813 static unsigned int pass = 0;
814 static bfd_boolean new_pass = FALSE;
815 static bfd_boolean changed = FALSE;
816 struct misc misc;
817 asection *stab;
818
819 /* Assume nothing changes. */
820 *again = FALSE;
821
822 if (first_section == NULL)
823 {
824 ip2k_relaxed = TRUE;
825 first_section = sec;
826 }
827
828 if (first_section == sec)
829 {
830 pass++;
831 new_pass = TRUE;
832 }
833
834 /* We don't have to do anything for a relocatable link,
835 if this section does not have relocs, or if this is
836 not a code section. */
837 if (link_info->relocatable
838 || (sec->flags & SEC_RELOC) == 0
839 || sec->reloc_count == 0
840 || (sec->flags & SEC_CODE) == 0)
841 return TRUE;
842
843 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
844
845 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL,
846 (Elf_Internal_Rela *)NULL,
847 link_info->keep_memory);
848 if (internal_relocs == NULL)
849 goto error_return;
850
851 /* Make sure the stac.rela stuff gets read in. */
852 stab = bfd_get_section_by_name (abfd, ".stab");
853
854 if (stab)
855 {
856 /* So stab does exits. */
857 Elf_Internal_Rela * irelbase;
858
859 irelbase = _bfd_elf_link_read_relocs (abfd, stab, NULL,
860 (Elf_Internal_Rela *)NULL,
861 link_info->keep_memory);
862 }
863
864 /* Get section contents cached copy if it exists. */
865 if (contents == NULL)
866 {
867 /* Get cached copy if it exists. */
868 if (elf_section_data (sec)->this_hdr.contents != NULL)
869 contents = elf_section_data (sec)->this_hdr.contents;
870 else
871 {
872 /* Go get them off disk. */
873 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
874 goto error_return;
875 }
876 }
877
878 /* Read this BFD's symbols cached copy if it exists. */
879 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
880 {
881 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
882 if (isymbuf == NULL)
883 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
884 symtab_hdr->sh_info, 0,
885 NULL, NULL, NULL);
886 if (isymbuf == NULL)
887 goto error_return;
888 }
889
890 misc.symtab_hdr = symtab_hdr;
891 misc.isymbuf = isymbuf;
892 misc.irelbase = internal_relocs;
893 misc.contents = contents;
894
895 /* This is where all the relaxation actually get done. */
896 if ((pass == 1) || (new_pass && !changed))
897 {
898 /* On the first pass we simply search for the lowest page that
899 we havn't relaxed yet. Note that the pass count is reset
900 each time a page is complete in order to move on to the next page.
901 If we can't find any more pages then we are finished. */
902 if (new_pass)
903 {
904 pass = 1;
905 new_pass = FALSE;
906 changed = TRUE; /* Pre-initialize to break out of pass 1. */
907 search_addr = 0xFFFFFFFF;
908 }
909
910 if ((BASEADDR (sec) + sec->size < search_addr)
911 && (BASEADDR (sec) + sec->size > page_end))
912 {
913 if (BASEADDR (sec) <= page_end)
914 search_addr = page_end + 1;
915 else
916 search_addr = BASEADDR (sec);
917
918 /* Found a page => more work to do. */
919 *again = TRUE;
920 }
921 }
922 else
923 {
924 if (new_pass)
925 {
926 new_pass = FALSE;
927 changed = FALSE;
928 page_start = PAGENO (search_addr);
929 page_end = page_start | 0x00003FFF;
930 }
931
932 /* Only process sections in range. */
933 if ((BASEADDR (sec) + sec->size >= page_start)
934 && (BASEADDR (sec) <= page_end))
935 {
936 if (!ip2k_elf_relax_section_page (abfd, sec, &changed, &misc, page_start, page_end))
937 return FALSE;
938 }
939 *again = TRUE;
940 }
941
942 /* Perform some house keeping after relaxing the section. */
943
944 if (isymbuf != NULL
945 && symtab_hdr->contents != (unsigned char *) isymbuf)
946 {
947 if (! link_info->keep_memory)
948 free (isymbuf);
949 else
950 symtab_hdr->contents = (unsigned char *) isymbuf;
951 }
952
953 if (contents != NULL
954 && elf_section_data (sec)->this_hdr.contents != contents)
955 {
956 if (! link_info->keep_memory)
957 free (contents);
958 else
959 {
960 /* Cache the section contents for elf_link_input_bfd. */
961 elf_section_data (sec)->this_hdr.contents = contents;
962 }
963 }
964
965 if (internal_relocs != NULL
966 && elf_section_data (sec)->relocs != internal_relocs)
967 free (internal_relocs);
968
969 return TRUE;
970
971 error_return:
972 if (isymbuf != NULL
973 && symtab_hdr->contents != (unsigned char *) isymbuf)
974 free (isymbuf);
975 if (contents != NULL
976 && elf_section_data (sec)->this_hdr.contents != contents)
977 free (contents);
978 if (internal_relocs != NULL
979 && elf_section_data (sec)->relocs != internal_relocs)
980 free (internal_relocs);
981 return FALSE;
982 }
983
984 /* This function handles relaxation of a section in a specific page. */
985
986 static bfd_boolean
987 ip2k_elf_relax_section_page (abfd, sec, again, misc, page_start, page_end)
988 bfd *abfd;
989 asection *sec;
990 bfd_boolean *again;
991 struct misc *misc;
992 unsigned long page_start;
993 unsigned long page_end;
994 {
995 Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
996 Elf_Internal_Rela *irel;
997 int switch_table_128;
998 int switch_table_256;
999
1000 /* Walk thru the section looking for relaxation opportunities. */
1001 for (irel = misc->irelbase; irel < irelend; irel++)
1002 {
1003 if (ELF32_R_TYPE (irel->r_info) != (int) R_IP2K_PAGE3)
1004 /* Ignore non page instructions. */
1005 continue;
1006
1007 if (BASEADDR (sec) + irel->r_offset < page_start)
1008 /* Ignore page instructions on earlier page - they have
1009 already been processed. Remember that there is code flow
1010 that crosses a page boundary. */
1011 continue;
1012
1013 if (BASEADDR (sec) + irel->r_offset > page_end)
1014 /* Flow beyond end of page => nothing more to do for this page. */
1015 return TRUE;
1016
1017 /* Detect switch tables. */
1018 switch_table_128 = ip2k_is_switch_table_128 (abfd, sec, irel->r_offset, misc->contents);
1019 switch_table_256 = ip2k_is_switch_table_256 (abfd, sec, irel->r_offset, misc->contents);
1020
1021 if ((switch_table_128 > 0) || (switch_table_256 > 0))
1022 /* If the index is greater than 0 then it has already been processed. */
1023 continue;
1024
1025 if (switch_table_128 == 0)
1026 {
1027 if (!ip2k_relax_switch_table_128 (abfd, sec, irel, again, misc))
1028 return FALSE;
1029
1030 continue;
1031 }
1032
1033 if (switch_table_256 == 0)
1034 {
1035 if (!ip2k_relax_switch_table_256 (abfd, sec, irel, again, misc))
1036 return FALSE;
1037
1038 continue;
1039 }
1040
1041 /* Simple relax. */
1042 if (ip2k_test_page_insn (abfd, sec, irel, misc))
1043 {
1044 if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
1045 return FALSE;
1046
1047 continue;
1048 }
1049 }
1050
1051 return TRUE;
1052 }
1053
1054 /* Parts of a Stabs entry. */
1055
1056 #define STRDXOFF (0)
1057 #define TYPEOFF (4)
1058 #define OTHEROFF (5)
1059 #define DESCOFF (6)
1060 #define VALOFF (8)
1061 #define STABSIZE (12)
1062
1063 /* Adjust all the relocations entries after adding or inserting instructions. */
1064
1065 static void
1066 adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj)
1067 bfd *abfd;
1068 asection *sec;
1069 bfd_vma addr;
1070 bfd_vma endaddr;
1071 int count;
1072 int noadj;
1073 {
1074 Elf_Internal_Shdr *symtab_hdr;
1075 Elf_Internal_Sym *isymbuf, *isym, *isymend;
1076 unsigned int shndx;
1077 bfd_byte *contents;
1078 Elf_Internal_Rela *irel, *irelend, *irelbase;
1079 struct elf_link_hash_entry **sym_hashes;
1080 struct elf_link_hash_entry **end_hashes;
1081 unsigned int symcount;
1082 asection *stab;
1083
1084 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1085 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1086
1087 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1088
1089 contents = elf_section_data (sec)->this_hdr.contents;
1090
1091 irelbase = elf_section_data (sec)->relocs;
1092 irelend = irelbase + sec->reloc_count;
1093
1094 for (irel = irelbase; irel < irelend; irel++)
1095 {
1096 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
1097 {
1098 /* Get the value of the symbol referred to by the reloc. */
1099 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1100 {
1101 asection *sym_sec;
1102
1103 /* A local symbol. */
1104 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1105 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1106
1107 if (isym->st_shndx == shndx)
1108 {
1109 bfd_vma baseaddr = BASEADDR (sec);
1110 bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
1111 + irel->r_addend;
1112
1113 if ((baseaddr + addr + noadj) <= symval
1114 && symval < (baseaddr + endaddr))
1115 irel->r_addend += count;
1116 }
1117 }
1118 }
1119
1120 /* Do this only for PC space relocations. */
1121 if (addr <= irel->r_offset && irel->r_offset < endaddr)
1122 irel->r_offset += count;
1123 }
1124
1125 /* Now fix the stab relocations. */
1126 stab = bfd_get_section_by_name (abfd, ".stab");
1127 if (stab)
1128 {
1129 bfd_byte *stabcontents, *stabend, *stabp;
1130 bfd_size_type stab_size = stab->rawsize ? stab->rawsize : stab->size;
1131
1132 irelbase = elf_section_data (stab)->relocs;
1133 irelend = irelbase + stab->reloc_count;
1134
1135 /* Pull out the contents of the stab section. */
1136 if (elf_section_data (stab)->this_hdr.contents != NULL)
1137 stabcontents = elf_section_data (stab)->this_hdr.contents;
1138 else
1139 {
1140 if (!bfd_malloc_and_get_section (abfd, stab, &stabcontents))
1141 {
1142 if (stabcontents != NULL)
1143 free (stabcontents);
1144 return;
1145 }
1146
1147 /* We need to remember this. */
1148 elf_section_data (stab)->this_hdr.contents = stabcontents;
1149 }
1150
1151 stabend = stabcontents + stab_size;
1152
1153 for (irel = irelbase; irel < irelend; irel++)
1154 {
1155 if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE)
1156 {
1157 /* Get the value of the symbol referred to by the reloc. */
1158 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1159 {
1160 asection *sym_sec;
1161
1162 /* A local symbol. */
1163 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1164 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1165
1166 if (sym_sec == sec)
1167 {
1168 const char *name;
1169 unsigned long strx;
1170 unsigned char type, other;
1171 unsigned short desc;
1172 bfd_vma value;
1173 bfd_vma baseaddr = BASEADDR (sec);
1174 bfd_vma symval = BASEADDR (sym_sec) + isym->st_value
1175 + irel->r_addend;
1176
1177 if ((baseaddr + addr) <= symval
1178 && symval <= (baseaddr + endaddr))
1179 irel->r_addend += count;
1180
1181 /* Go hunt up a function and fix its line info if needed. */
1182 stabp = stabcontents + irel->r_offset - 8;
1183
1184 /* Go pullout the stab entry. */
1185 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1186 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1187 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1188 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1189 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1190
1191 name = bfd_get_stab_name (type);
1192
1193 if (strcmp (name, "FUN") == 0)
1194 {
1195 int function_adjusted = 0;
1196
1197 if (symval > (baseaddr + addr))
1198 /* Not in this function. */
1199 continue;
1200
1201 /* Hey we got a function hit. */
1202 stabp += STABSIZE;
1203 for (;stabp < stabend; stabp += STABSIZE)
1204 {
1205 /* Go pullout the stab entry. */
1206 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1207 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1208 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1209 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1210 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1211
1212 name = bfd_get_stab_name (type);
1213
1214 if (strcmp (name, "FUN") == 0)
1215 {
1216 /* Hit another function entry. */
1217 if (function_adjusted)
1218 {
1219 /* Adjust the value. */
1220 value += count;
1221
1222 /* We need to put it back. */
1223 bfd_h_put_32 (abfd, value,stabp + VALOFF);
1224 }
1225
1226 /* And then bale out. */
1227 break;
1228 }
1229
1230 if (strcmp (name, "SLINE") == 0)
1231 {
1232 /* Got a line entry. */
1233 if ((baseaddr + addr) <= (symval + value))
1234 {
1235 /* Adjust the line entry. */
1236 value += count;
1237
1238 /* We need to put it back. */
1239 bfd_h_put_32 (abfd, value,stabp + VALOFF);
1240 function_adjusted = 1;
1241 }
1242 }
1243 }
1244 }
1245 }
1246 }
1247 }
1248 }
1249 }
1250
1251 /* When adding an instruction back it is sometimes necessary to move any
1252 global or local symbol that was referencing the first instruction of
1253 the moved block to refer to the first instruction of the inserted block.
1254
1255 For example adding a PAGE instruction before a CALL or JMP requires
1256 that any label on the CALL or JMP is moved to the PAGE insn. */
1257 addr += noadj;
1258
1259 /* Adjust the local symbols defined in this section. */
1260 isymend = isymbuf + symtab_hdr->sh_info;
1261 for (isym = isymbuf; isym < isymend; isym++)
1262 {
1263 if (isym->st_shndx == shndx
1264 && addr <= isym->st_value
1265 && isym->st_value < endaddr)
1266 isym->st_value += count;
1267 }
1268
1269 /* Now adjust the global symbols defined in this section. */
1270 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1271 - symtab_hdr->sh_info);
1272 sym_hashes = elf_sym_hashes (abfd);
1273 end_hashes = sym_hashes + symcount;
1274 for (; sym_hashes < end_hashes; sym_hashes++)
1275 {
1276 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1277
1278 if ((sym_hash->root.type == bfd_link_hash_defined
1279 || sym_hash->root.type == bfd_link_hash_defweak)
1280 && sym_hash->root.u.def.section == sec)
1281 {
1282 if (addr <= sym_hash->root.u.def.value
1283 && sym_hash->root.u.def.value < endaddr)
1284 sym_hash->root.u.def.value += count;
1285 }
1286 }
1287
1288 return;
1289 }
1290
1291 /* Delete some bytes from a section while relaxing. */
1292
1293 static bfd_boolean
1294 ip2k_elf_relax_delete_bytes (abfd, sec, addr, count)
1295 bfd *abfd;
1296 asection *sec;
1297 bfd_vma addr;
1298 int count;
1299 {
1300 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1301 bfd_vma endaddr = sec->size;
1302
1303 /* Actually delete the bytes. */
1304 memmove (contents + addr, contents + addr + count,
1305 endaddr - addr - count);
1306
1307 sec->size -= count;
1308
1309 adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
1310 return TRUE;
1311 }
1312
1313 /* -------------------------------------------------------------------- */
1314
1315 /* XXX: The following code is the result of a cut&paste. This unfortunate
1316 practice is very widespread in the various target back-end files. */
1317
1318 /* Set the howto pointer for a IP2K ELF reloc. */
1319
1320 static void
1321 ip2k_info_to_howto_rela (abfd, cache_ptr, dst)
1322 bfd * abfd ATTRIBUTE_UNUSED;
1323 arelent * cache_ptr;
1324 Elf_Internal_Rela * dst;
1325 {
1326 unsigned int r_type;
1327
1328 r_type = ELF32_R_TYPE (dst->r_info);
1329 switch (r_type)
1330 {
1331 default:
1332 cache_ptr->howto = & ip2k_elf_howto_table [r_type];
1333 break;
1334 }
1335 }
1336
1337 /* Perform a single relocation.
1338 By default we use the standard BFD routines. */
1339
1340 static bfd_reloc_status_type
1341 ip2k_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1342 relocation)
1343 reloc_howto_type * howto;
1344 bfd * input_bfd;
1345 asection * input_section;
1346 bfd_byte * contents;
1347 Elf_Internal_Rela * rel;
1348 bfd_vma relocation;
1349 {
1350 static bfd_vma page_addr = 0;
1351
1352 bfd_reloc_status_type r = bfd_reloc_ok;
1353 switch (howto->type)
1354 {
1355 /* Handle data space relocations. */
1356 case R_IP2K_FR9:
1357 case R_IP2K_BANK:
1358 if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE)
1359 relocation &= ~IP2K_DATA_MASK;
1360 else
1361 r = bfd_reloc_notsupported;
1362 break;
1363
1364 case R_IP2K_LO8DATA:
1365 case R_IP2K_HI8DATA:
1366 case R_IP2K_EX8DATA:
1367 break;
1368
1369 /* Handle insn space relocations. */
1370 case R_IP2K_PAGE3:
1371 page_addr = BASEADDR (input_section) + rel->r_offset;
1372 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1373 relocation &= ~IP2K_INSN_MASK;
1374 else
1375 r = bfd_reloc_notsupported;
1376 break;
1377
1378 case R_IP2K_ADDR16CJP:
1379 if (BASEADDR (input_section) + rel->r_offset != page_addr + 2)
1380 {
1381 /* No preceding page instruction, verify that it isn't needed. */
1382 if (PAGENO (relocation + rel->r_addend) !=
1383 ip2k_nominal_page_bits (input_bfd, input_section,
1384 rel->r_offset, contents))
1385 _bfd_error_handler (_("ip2k linker: missing page instruction at 0x%08lx (dest = 0x%08lx)."),
1386 BASEADDR (input_section) + rel->r_offset,
1387 relocation + rel->r_addend);
1388 }
1389 else if (ip2k_relaxed)
1390 {
1391 /* Preceding page instruction. Verify that the page instruction is
1392 really needed. One reason for the relaxation to miss a page is if
1393 the section is not marked as executable. */
1394 if (!ip2k_is_switch_table_128 (input_bfd, input_section, rel->r_offset - 2, contents) &&
1395 !ip2k_is_switch_table_256 (input_bfd, input_section, rel->r_offset - 2, contents) &&
1396 (PAGENO (relocation + rel->r_addend) ==
1397 ip2k_nominal_page_bits (input_bfd, input_section,
1398 rel->r_offset - 2, contents)))
1399 _bfd_error_handler (_("ip2k linker: redundant page instruction at 0x%08lx (dest = 0x%08lx)."),
1400 page_addr,
1401 relocation + rel->r_addend);
1402 }
1403 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1404 relocation &= ~IP2K_INSN_MASK;
1405 else
1406 r = bfd_reloc_notsupported;
1407 break;
1408
1409 case R_IP2K_LO8INSN:
1410 case R_IP2K_HI8INSN:
1411 case R_IP2K_PC_SKIP:
1412 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1413 relocation &= ~IP2K_INSN_MASK;
1414 else
1415 r = bfd_reloc_notsupported;
1416 break;
1417
1418 case R_IP2K_16:
1419 /* If this is a relocation involving a TEXT
1420 symbol, reduce it to a word address. */
1421 if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE)
1422 howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT];
1423 break;
1424
1425 /* Pass others through. */
1426 default:
1427 break;
1428 }
1429
1430 /* Only install relocation if above tests did not disqualify it. */
1431 if (r == bfd_reloc_ok)
1432 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1433 contents, rel->r_offset,
1434 relocation, rel->r_addend);
1435
1436 return r;
1437 }
1438
1439 /* Relocate a IP2K ELF section.
1440
1441 The RELOCATE_SECTION function is called by the new ELF backend linker
1442 to handle the relocations for a section.
1443
1444 The relocs are always passed as Rela structures; if the section
1445 actually uses Rel structures, the r_addend field will always be
1446 zero.
1447
1448 This function is responsible for adjusting the section contents as
1449 necessary, and (if using Rela relocs and generating a relocatable
1450 output file) adjusting the reloc addend as necessary.
1451
1452 This function does not have to worry about setting the reloc
1453 address or the reloc symbol index.
1454
1455 LOCAL_SYMS is a pointer to the swapped in local symbols.
1456
1457 LOCAL_SECTIONS is an array giving the section in the input file
1458 corresponding to the st_shndx field of each local symbol.
1459
1460 The global hash table entry for the global symbols can be found
1461 via elf_sym_hashes (input_bfd).
1462
1463 When generating relocatable output, this function must handle
1464 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1465 going to be the section symbol corresponding to the output
1466 section, which means that the addend must be adjusted
1467 accordingly. */
1468
1469 static bfd_boolean
1470 ip2k_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1471 contents, relocs, local_syms, local_sections)
1472 bfd *output_bfd ATTRIBUTE_UNUSED;
1473 struct bfd_link_info *info;
1474 bfd *input_bfd;
1475 asection *input_section;
1476 bfd_byte *contents;
1477 Elf_Internal_Rela *relocs;
1478 Elf_Internal_Sym *local_syms;
1479 asection **local_sections;
1480 {
1481 Elf_Internal_Shdr *symtab_hdr;
1482 struct elf_link_hash_entry **sym_hashes;
1483 Elf_Internal_Rela *rel;
1484 Elf_Internal_Rela *relend;
1485
1486 if (info->relocatable)
1487 return TRUE;
1488
1489 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1490 sym_hashes = elf_sym_hashes (input_bfd);
1491 relend = relocs + input_section->reloc_count;
1492
1493 for (rel = relocs; rel < relend; rel ++)
1494 {
1495 reloc_howto_type * howto;
1496 unsigned long r_symndx;
1497 Elf_Internal_Sym * sym;
1498 asection * sec;
1499 struct elf_link_hash_entry * h;
1500 bfd_vma relocation;
1501 bfd_reloc_status_type r;
1502 const char * name = NULL;
1503 int r_type;
1504
1505 /* This is a final link. */
1506 r_type = ELF32_R_TYPE (rel->r_info);
1507 r_symndx = ELF32_R_SYM (rel->r_info);
1508 howto = ip2k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
1509 h = NULL;
1510 sym = NULL;
1511 sec = NULL;
1512
1513 if (r_symndx < symtab_hdr->sh_info)
1514 {
1515 sym = local_syms + r_symndx;
1516 sec = local_sections [r_symndx];
1517 relocation = BASEADDR (sec) + sym->st_value;
1518
1519 name = bfd_elf_string_from_elf_section
1520 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1521 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1522 }
1523 else
1524 {
1525 bfd_boolean warned;
1526 bfd_boolean unresolved_reloc;
1527
1528 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1529 r_symndx, symtab_hdr, sym_hashes,
1530 h, sec, relocation,
1531 unresolved_reloc, warned);
1532
1533 name = h->root.root.string;
1534 }
1535
1536 /* Finally, the sole IP2K-specific part. */
1537 r = ip2k_final_link_relocate (howto, input_bfd, input_section,
1538 contents, rel, relocation);
1539
1540 if (r != bfd_reloc_ok)
1541 {
1542 const char * msg = (const char *) NULL;
1543
1544 switch (r)
1545 {
1546 case bfd_reloc_overflow:
1547 r = info->callbacks->reloc_overflow
1548 (info, (h ? &h->root : NULL), name, howto->name,
1549 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1550 break;
1551
1552 case bfd_reloc_undefined:
1553 r = info->callbacks->undefined_symbol
1554 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1555 break;
1556
1557 case bfd_reloc_outofrange:
1558 msg = _("internal error: out of range error");
1559 break;
1560
1561 /* This is how ip2k_final_link_relocate tells us of a non-kosher
1562 reference between insn & data address spaces. */
1563 case bfd_reloc_notsupported:
1564 if (sym != NULL) /* Only if it's not an unresolved symbol. */
1565 msg = _("unsupported relocation between data/insn address spaces");
1566 break;
1567
1568 case bfd_reloc_dangerous:
1569 msg = _("internal error: dangerous relocation");
1570 break;
1571
1572 default:
1573 msg = _("internal error: unknown error");
1574 break;
1575 }
1576
1577 if (msg)
1578 r = info->callbacks->warning
1579 (info, msg, name, input_bfd, input_section, rel->r_offset);
1580
1581 if (! r)
1582 return FALSE;
1583 }
1584 }
1585
1586 return TRUE;
1587 }
1588
1589 static asection *
1590 ip2k_elf_gc_mark_hook (sec, info, rel, h, sym)
1591 asection *sec;
1592 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1593 Elf_Internal_Rela *rel;
1594 struct elf_link_hash_entry *h;
1595 Elf_Internal_Sym *sym;
1596 {
1597 if (h != NULL)
1598 {
1599 switch (ELF32_R_TYPE (rel->r_info))
1600 {
1601 #if 0
1602 case R_IP2K_GNU_VTINHERIT:
1603 case R_IP2K_GNU_VTENTRY:
1604 break;
1605 #endif
1606
1607 default:
1608 switch (h->root.type)
1609 {
1610 case bfd_link_hash_defined:
1611 case bfd_link_hash_defweak:
1612 return h->root.u.def.section;
1613
1614 case bfd_link_hash_common:
1615 return h->root.u.c.p->section;
1616
1617 default:
1618 break;
1619 }
1620 }
1621 }
1622 else
1623 {
1624 if (!(elf_bad_symtab (sec->owner)
1625 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1626 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1627 && sym->st_shndx != SHN_COMMON))
1628 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1629 }
1630 return NULL;
1631 }
1632
1633 static bfd_boolean
1634 ip2k_elf_gc_sweep_hook (abfd, info, sec, relocs)
1635 bfd *abfd ATTRIBUTE_UNUSED;
1636 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1637 asection *sec ATTRIBUTE_UNUSED;
1638 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1639 {
1640 /* We don't use got and plt entries for ip2k. */
1641 return TRUE;
1642 }
1643
1644 #define TARGET_BIG_SYM bfd_elf32_ip2k_vec
1645 #define TARGET_BIG_NAME "elf32-ip2k"
1646
1647 #define ELF_ARCH bfd_arch_ip2k
1648 #define ELF_MACHINE_CODE EM_IP2K
1649 #define ELF_MACHINE_ALT1 EM_IP2K_OLD
1650 #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K. */
1651
1652 #define elf_info_to_howto_rel NULL
1653 #define elf_info_to_howto ip2k_info_to_howto_rela
1654
1655 #define elf_backend_can_gc_sections 1
1656 #define elf_backend_rela_normal 1
1657 #define elf_backend_gc_mark_hook ip2k_elf_gc_mark_hook
1658 #define elf_backend_gc_sweep_hook ip2k_elf_gc_sweep_hook
1659 #define elf_backend_relocate_section ip2k_elf_relocate_section
1660
1661 #define elf_symbol_leading_char '_'
1662 #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup
1663 #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section
1664
1665 #include "elf32-target.h"