Commit | Line | Data |
---|---|---|
cf88bb9f NC |
1 | /* Scenix IP2xxx specific support for 32-bit ELF |
2 | Copyright 2000, 2001, 2002 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 | bfd_byte * free_contents; | |
34 | Elf32_External_Sym * extsyms; | |
35 | Elf32_External_Sym * free_extsyms; | |
36 | Elf_Internal_Rela * free_relocs; | |
37 | }; | |
38 | ||
39 | /* Prototypes. */ | |
40 | static reloc_howto_type * ip2k_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
41 | static void ip2k_info_to_howto_rela PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
42 | static asection * ip2k_elf_gc_mark_hook PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, struct elf_link_hash_entry *, Elf_Internal_Sym *)); | |
43 | static boolean ip2k_elf_gc_sweep_hook PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *)); | |
44 | static bfd_vma symbol_value PARAMS ((bfd *, Elf_Internal_Shdr *, Elf32_External_Sym *, Elf_Internal_Rela *)); | |
45 | static void adjust_all_relocations PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int, int)); | |
46 | static boolean ip2k_elf_relax_delete_bytes PARAMS ((bfd *, asection *, bfd_vma, int)); | |
47 | static boolean ip2k_elf_relax_add_bytes PARAMS ((bfd *, asection *, bfd_vma, const bfd_byte *, int, int)); | |
48 | static boolean add_page_insn PARAMS ((bfd *, asection *, Elf_Internal_Rela *, struct misc *)); | |
49 | static boolean ip2k_elf_relax_section PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *)); | |
50 | static boolean relax_switch_dispatch_tables_pass1 PARAMS ((bfd *, asection *, bfd_vma, struct misc *)); | |
51 | static boolean unrelax_dispatch_table_entries PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, boolean *, struct misc *)); | |
52 | static boolean unrelax_switch_dispatch_tables_passN PARAMS ((bfd *, asection *, bfd_vma, boolean *, struct misc *)); | |
53 | static boolean is_switch_128_dispatch_table_p PARAMS ((bfd *, bfd_vma, boolean, struct misc *)); | |
54 | static boolean is_switch_256_dispatch_table_p PARAMS ((bfd *, bfd_vma, boolean, struct misc *)); | |
55 | static void tidyup_after_error PARAMS ((struct misc *)); | |
56 | static boolean ip2k_elf_relax_section_pass1 PARAMS ((bfd *, asection *, boolean *, struct misc *)); | |
57 | static boolean ip2k_elf_relax_section_passN PARAMS ((bfd *, asection *, boolean *, boolean *, struct misc *)); | |
58 | static bfd_reloc_status_type ip2k_final_link_relocate PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, bfd_vma)); | |
59 | static boolean ip2k_elf_relocate_section PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
60 | ||
61 | #define IS_OPCODE(CODE0,CODE1,OPCODE) \ | |
62 | ((CODE0) == (OPCODE)[0] && (CODE1) == (OPCODE)[1]) | |
63 | ||
64 | #define PAGE_INSN_0 0x00 | |
65 | #define PAGE_INSN_1 0x10 | |
66 | ||
67 | static const bfd_byte page_opcode[] = | |
68 | { | |
69 | PAGE_INSN_0, PAGE_INSN_1 | |
70 | }; | |
71 | ||
72 | #define IS_PAGE_OPCODE(CODE0,CODE1) \ | |
73 | IS_OPCODE (CODE0, CODE1, page_opcode) | |
74 | ||
75 | #define JMP_INSN_0 0xE0 | |
76 | #define JMP_INSN_1 0x00 | |
77 | ||
78 | static const bfd_byte jmp_opcode[] = | |
79 | { | |
80 | JMP_INSN_0, JMP_INSN_1 | |
81 | }; | |
82 | ||
83 | #define IS_JMP_OPCODE(CODE0,CODE1) \ | |
84 | IS_OPCODE (CODE0, CODE1, jmp_opcode) | |
85 | ||
86 | #define CALL_INSN_0 0xC0 | |
87 | #define CALL_INSN_1 0x00 | |
88 | ||
89 | static const bfd_byte call_opcode[] = | |
90 | { | |
91 | CALL_INSN_0, CALL_INSN_1 | |
92 | }; | |
93 | ||
94 | #define IS_CALL_OPCODE(CODE0,CODE1) \ | |
95 | IS_OPCODE (CODE0, CODE1, call_opcode) | |
96 | ||
97 | #define ADD_PCL_W_INSN_0 0x1E | |
98 | #define ADD_PCL_W_INSN_1 0x09 | |
99 | ||
100 | static const bfd_byte add_pcl_w_opcode[] = | |
101 | { | |
102 | ADD_PCL_W_INSN_0, ADD_PCL_W_INSN_1 | |
103 | }; | |
104 | ||
105 | #define IS_ADD_PCL_W_OPCODE(CODE0,CODE1) \ | |
106 | IS_OPCODE (CODE0, CODE1, add_pcl_w_opcode) | |
107 | ||
108 | #define ADD_W_WREG_INSN_0 0x1C | |
109 | #define ADD_W_WREG_INSN_1 0x0A | |
110 | ||
111 | static const bfd_byte add_w_wreg_opcode[] = | |
112 | { | |
113 | ADD_W_WREG_INSN_0, ADD_W_WREG_INSN_1 | |
114 | }; | |
115 | ||
116 | #define IS_ADD_W_WREG_OPCODE(CODE0,CODE1) \ | |
117 | IS_OPCODE (CODE0, CODE1, add_w_wreg_opcode) | |
118 | ||
119 | #define SNC_INSN_0 0xA0 | |
120 | #define SNC_INSN_1 0x0B | |
121 | ||
122 | static const bfd_byte snc_opcode[] = | |
123 | { | |
124 | SNC_INSN_0, SNC_INSN_1 | |
125 | }; | |
126 | ||
127 | #define IS_SNC_OPCODE(CODE0,CODE1) \ | |
128 | IS_OPCODE (CODE0, CODE1, snc_opcode) | |
129 | ||
130 | #define INC_1_SP_INSN_0 0x2B | |
131 | #define INC_1_SP_INSN_1 0x81 | |
132 | ||
133 | static const bfd_byte inc_1_sp_opcode[] = | |
134 | { | |
135 | INC_1_SP_INSN_0, INC_1_SP_INSN_1 | |
136 | }; | |
137 | ||
138 | #define IS_INC_1_SP_OPCODE(CODE0,CODE1) \ | |
139 | IS_OPCODE (CODE0, CODE1, inc_1_sp_opcode) | |
140 | ||
141 | #define ADD_2_SP_W_INSN_0 0x1F | |
142 | #define ADD_2_SP_W_INSN_1 0x82 | |
143 | ||
144 | static const bfd_byte add_2_sp_w_opcode[] = | |
145 | { | |
146 | ADD_2_SP_W_INSN_0, ADD_2_SP_W_INSN_1 | |
147 | }; | |
148 | ||
149 | #define IS_ADD_2_SP_W_OPCODE(CODE0,CODE1) \ | |
150 | IS_OPCODE (CODE0, CODE1, add_2_sp_w_opcode) | |
151 | ||
152 | /* Relocation tables. */ | |
153 | static reloc_howto_type ip2k_elf_howto_table [] = | |
154 | { | |
155 | #define IP2K_HOWTO(t,rs,s,bs,pr,bp,name,sm,dm) \ | |
156 | HOWTO(t, /* type */ \ | |
157 | rs, /* rightshift */ \ | |
158 | s, /* size (0 = byte, 1 = short, 2 = long) */ \ | |
159 | bs, /* bitsize */ \ | |
160 | pr, /* pc_relative */ \ | |
161 | bp, /* bitpos */ \ | |
162 | complain_overflow_dont,/* complain_on_overflow */ \ | |
163 | bfd_elf_generic_reloc,/* special_function */ \ | |
164 | name, /* name */ \ | |
165 | false, /* partial_inplace */ \ | |
166 | sm, /* src_mask */ \ | |
167 | dm, /* dst_mask */ \ | |
168 | pr) /* pcrel_offset */ | |
169 | ||
170 | /* This reloc does nothing. */ | |
171 | IP2K_HOWTO (R_IP2K_NONE, 0,2,32, false, 0, "R_IP2K_NONE", 0, 0), | |
172 | /* A 16 bit absolute relocation. */ | |
173 | IP2K_HOWTO (R_IP2K_16, 0,1,16, false, 0, "R_IP2K_16", 0, 0xffff), | |
174 | /* A 32 bit absolute relocation. */ | |
175 | IP2K_HOWTO (R_IP2K_32, 0,2,32, false, 0, "R_IP2K_32", 0, 0xffffffff), | |
176 | /* A 8-bit data relocation for the FR9 field. Ninth bit is computed specially. */ | |
177 | IP2K_HOWTO (R_IP2K_FR9, 0,1,9, false, 0, "R_IP2K_FR9", 0, 0x00ff), | |
178 | /* A 4-bit data relocation. */ | |
179 | IP2K_HOWTO (R_IP2K_BANK, 8,1,4, false, 0, "R_IP2K_BANK", 0, 0x000f), | |
180 | /* A 13-bit insn relocation - word address => right-shift 1 bit extra. */ | |
181 | IP2K_HOWTO (R_IP2K_ADDR16CJP, 1,1,13, false, 0, "R_IP2K_ADDR16CJP", 0, 0x1fff), | |
182 | /* A 3-bit insn relocation - word address => right-shift 1 bit extra. */ | |
183 | IP2K_HOWTO (R_IP2K_PAGE3, 14,1,3, false, 0, "R_IP2K_PAGE3", 0, 0x0007), | |
184 | /* Two 8-bit data relocations. */ | |
185 | IP2K_HOWTO (R_IP2K_LO8DATA, 0,1,8, false, 0, "R_IP2K_LO8DATA", 0, 0x00ff), | |
186 | IP2K_HOWTO (R_IP2K_HI8DATA, 8,1,8, false, 0, "R_IP2K_HI8DATA", 0, 0x00ff), | |
187 | /* Two 8-bit insn relocations. word address => right-shift 1 bit extra. */ | |
188 | IP2K_HOWTO (R_IP2K_LO8INSN, 1,1,8, false, 0, "R_IP2K_LO8INSN", 0, 0x00ff), | |
189 | IP2K_HOWTO (R_IP2K_HI8INSN, 9,1,8, false, 0, "R_IP2K_HI8INSN", 0, 0x00ff), | |
190 | ||
191 | /* Special 1 bit relocation for SKIP instructions. */ | |
192 | IP2K_HOWTO (R_IP2K_PC_SKIP, 1,1,1, false, 12, "R_IP2K_PC_SKIP", 0xfffe, 0x1000), | |
193 | /* 16 bit word address. */ | |
194 | IP2K_HOWTO (R_IP2K_TEXT, 1,1,16, false, 0, "R_IP2K_TEXT", 0, 0xffff), | |
195 | /* A 7-bit offset relocation for the FR9 field. Eigth and ninth bit comes from insn. */ | |
196 | IP2K_HOWTO (R_IP2K_FR_OFFSET, 0,1,9, false, 0, "R_IP2K_FR_OFFSET", 0x180, 0x007f), | |
197 | /* Bits 23:16 of an address. */ | |
198 | IP2K_HOWTO (R_IP2K_EX8DATA, 16,1,8, false, 0, "R_IP2K_EX8DATA", 0, 0x00ff), | |
199 | }; | |
200 | ||
201 | ||
202 | /* Map BFD reloc types to IP2K ELF reloc types. */ | |
203 | static reloc_howto_type * | |
204 | ip2k_reloc_type_lookup (abfd, code) | |
205 | bfd * abfd ATTRIBUTE_UNUSED; | |
206 | bfd_reloc_code_real_type code; | |
207 | { | |
208 | /* Note that the ip2k_elf_howto_table is indxed by the R_ | |
209 | constants. Thus, the order that the howto records appear in the | |
210 | table *must* match the order of the relocation types defined in | |
211 | include/elf/ip2k.h. */ | |
212 | ||
213 | switch (code) | |
214 | { | |
215 | case BFD_RELOC_NONE: | |
216 | return &ip2k_elf_howto_table[ (int) R_IP2K_NONE]; | |
217 | case BFD_RELOC_16: | |
218 | return &ip2k_elf_howto_table[ (int) R_IP2K_16]; | |
219 | case BFD_RELOC_32: | |
220 | return &ip2k_elf_howto_table[ (int) R_IP2K_32]; | |
221 | case BFD_RELOC_IP2K_FR9: | |
222 | return &ip2k_elf_howto_table[ (int) R_IP2K_FR9]; | |
223 | case BFD_RELOC_IP2K_BANK: | |
224 | return &ip2k_elf_howto_table[ (int) R_IP2K_BANK]; | |
225 | case BFD_RELOC_IP2K_ADDR16CJP: | |
226 | return &ip2k_elf_howto_table[ (int) R_IP2K_ADDR16CJP]; | |
227 | case BFD_RELOC_IP2K_PAGE3: | |
228 | return &ip2k_elf_howto_table[ (int) R_IP2K_PAGE3]; | |
229 | case BFD_RELOC_IP2K_LO8DATA: | |
230 | return &ip2k_elf_howto_table[ (int) R_IP2K_LO8DATA]; | |
231 | case BFD_RELOC_IP2K_HI8DATA: | |
232 | return &ip2k_elf_howto_table[ (int) R_IP2K_HI8DATA]; | |
233 | case BFD_RELOC_IP2K_LO8INSN: | |
234 | return &ip2k_elf_howto_table[ (int) R_IP2K_LO8INSN]; | |
235 | case BFD_RELOC_IP2K_HI8INSN: | |
236 | return &ip2k_elf_howto_table[ (int) R_IP2K_HI8INSN]; | |
237 | case BFD_RELOC_IP2K_PC_SKIP: | |
238 | return &ip2k_elf_howto_table[ (int) R_IP2K_PC_SKIP]; | |
239 | case BFD_RELOC_IP2K_TEXT: | |
240 | return &ip2k_elf_howto_table[ (int) R_IP2K_TEXT]; | |
241 | case BFD_RELOC_IP2K_FR_OFFSET: | |
242 | return &ip2k_elf_howto_table[ (int) R_IP2K_FR_OFFSET]; | |
243 | case BFD_RELOC_IP2K_EX8DATA: | |
244 | return &ip2k_elf_howto_table[ (int) R_IP2K_EX8DATA]; | |
245 | default: | |
246 | /* Pacify gcc -Wall. */ | |
247 | return NULL; | |
248 | } | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | #define PAGENO(ABSADDR) ((ABSADDR) & 0x1C000) | |
253 | #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset) | |
254 | ||
255 | #define UNDEFINED_SYMBOL (~(bfd_vma)0) | |
256 | ||
257 | /* Return the value of the symbol associated with the relocation IREL. */ | |
258 | ||
259 | static bfd_vma | |
260 | symbol_value (abfd, symtab_hdr, extsyms, irel) | |
261 | bfd *abfd; | |
262 | Elf_Internal_Shdr *symtab_hdr; | |
263 | Elf32_External_Sym *extsyms; | |
264 | Elf_Internal_Rela *irel; | |
265 | { | |
266 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
267 | { | |
268 | Elf_External_Sym_Shndx *sym_shndx; | |
269 | Elf_Internal_Shdr *shndx_hdr; | |
270 | Elf_Internal_Sym isym; | |
271 | asection *sym_sec; | |
272 | ||
273 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
274 | sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
275 | sym_shndx = sym_shndx ? sym_shndx + ELF32_R_SYM (irel->r_info) : NULL; | |
276 | bfd_elf32_swap_symbol_in (abfd, extsyms + ELF32_R_SYM (irel->r_info), | |
277 | sym_shndx, &isym); | |
278 | if (isym.st_shndx == SHN_UNDEF) | |
279 | sym_sec = bfd_und_section_ptr; | |
280 | else if (isym.st_shndx == SHN_ABS) | |
281 | sym_sec = bfd_abs_section_ptr; | |
282 | else if (isym.st_shndx == SHN_COMMON) | |
283 | sym_sec = bfd_com_section_ptr; | |
284 | else | |
285 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); | |
286 | ||
287 | return isym.st_value + BASEADDR (sym_sec); | |
288 | } | |
289 | else | |
290 | { | |
291 | unsigned long indx; | |
292 | struct elf_link_hash_entry *h; | |
293 | ||
294 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
295 | h = elf_sym_hashes (abfd)[indx]; | |
296 | BFD_ASSERT (h != NULL); | |
297 | ||
298 | if (h->root.type != bfd_link_hash_defined | |
299 | && h->root.type != bfd_link_hash_defweak) | |
300 | return UNDEFINED_SYMBOL; | |
301 | ||
302 | return (h->root.u.def.value + BASEADDR (h->root.u.def.section)); | |
303 | } | |
304 | } | |
305 | ||
306 | /* Determine if the instruction sequence matches that for | |
307 | the prologue of a switch dispatch table with fewer than | |
308 | 128 entries. | |
309 | ||
310 | sc | |
311 | page $nnn0 | |
312 | jmp $nnn0 | |
313 | add w,wreg | |
314 | add pcl,w | |
315 | addr=> | |
316 | page $nnn1 | |
317 | jmp $nnn1 | |
318 | page $nnn2 | |
319 | jmp $nnn2 | |
320 | ... | |
321 | page $nnnN | |
322 | jmp $nnnN | |
323 | ||
324 | After relaxation. | |
325 | sc | |
326 | page $nnn0 | |
327 | jmp $nnn0 | |
328 | add pcl,w | |
329 | addr=> | |
330 | jmp $nnn1 | |
331 | jmp $nnn2 | |
332 | ... | |
333 | jmp $nnnN */ | |
334 | ||
335 | static boolean | |
336 | is_switch_128_dispatch_table_p (abfd, addr, relaxed, misc) | |
337 | bfd *abfd ATTRIBUTE_UNUSED; | |
338 | bfd_vma addr; | |
339 | boolean relaxed; | |
340 | struct misc *misc; | |
341 | { | |
342 | bfd_byte code0, code1; | |
343 | ||
344 | if (addr < (3 * 2)) | |
345 | return false; | |
346 | ||
347 | code0 = bfd_get_8 (abfd, misc->contents + addr - 2); | |
348 | code1 = bfd_get_8 (abfd, misc->contents + addr - 1); | |
349 | ||
350 | /* Is it ADD PCL,W */ | |
351 | if (! IS_ADD_PCL_W_OPCODE (code0, code1)) | |
352 | return false; | |
353 | ||
354 | code0 = bfd_get_8 (abfd, misc->contents + addr - 4); | |
355 | code1 = bfd_get_8 (abfd, misc->contents + addr - 3); | |
356 | ||
357 | if (relaxed) | |
358 | /* Is it ADD W,WREG */ | |
359 | return ! IS_ADD_W_WREG_OPCODE (code0, code1); | |
360 | ||
361 | else | |
362 | { | |
363 | /* Is it ADD W,WREG */ | |
364 | if (! IS_ADD_W_WREG_OPCODE (code0, code1)) | |
365 | return false; | |
366 | ||
367 | code0 = bfd_get_8 (abfd, misc->contents + addr - 6); | |
368 | code1 = bfd_get_8 (abfd, misc->contents + addr - 5); | |
369 | ||
370 | /* Is it JMP $nnnn */ | |
371 | if (! IS_JMP_OPCODE (code0, code1)) | |
372 | return false; | |
373 | } | |
374 | ||
375 | /* It looks like we've found the prologue for | |
376 | a 1-127 entry switch dispatch table. */ | |
377 | return true; | |
378 | } | |
379 | ||
380 | /* Determine if the instruction sequence matches that for | |
381 | the prologue switch dispatch table with fewer than | |
382 | 256 entries but more than 127. | |
383 | ||
384 | Before relaxation. | |
385 | push %lo8insn(label) ; Push address of table | |
386 | push %hi8insn(label) | |
387 | add w,wreg ; index*2 => offset | |
388 | snc ; CARRY SET? | |
389 | inc 1(sp) ; Propagate MSB into table address | |
390 | add 2(sp),w ; Add low bits of offset to table address | |
391 | snc ; and handle any carry-out | |
392 | inc 1(sp) | |
393 | addr=> | |
394 | page __indjmp ; Do an indirect jump to that location | |
395 | jmp __indjmp | |
396 | label: ; case dispatch table starts here | |
397 | page $nnn1 | |
398 | jmp $nnn1 | |
399 | page $nnn2 | |
400 | jmp $nnn2 | |
401 | ... | |
402 | page $nnnN | |
403 | jmp $nnnN | |
404 | ||
405 | After relaxation. | |
406 | push %lo8insn(label) ; Push address of table | |
407 | push %hi8insn(label) | |
408 | add 2(sp),w ; Add low bits of offset to table address | |
409 | snc ; and handle any carry-out | |
410 | inc 1(sp) | |
411 | addr=> | |
412 | page __indjmp ; Do an indirect jump to that location | |
413 | jmp __indjmp | |
414 | label: ; case dispatch table starts here | |
415 | jmp $nnn1 | |
416 | jmp $nnn2 | |
417 | ... | |
418 | jmp $nnnN */ | |
419 | ||
420 | static boolean | |
421 | is_switch_256_dispatch_table_p (abfd, addr, relaxed, misc) | |
422 | bfd *abfd ATTRIBUTE_UNUSED; | |
423 | bfd_vma addr; | |
424 | boolean relaxed; | |
425 | struct misc *misc; | |
426 | { | |
427 | bfd_byte code0, code1; | |
428 | ||
429 | if (addr < (8 * 2)) | |
430 | return false; | |
431 | ||
432 | code0 = bfd_get_8 (abfd, misc->contents + addr - 2); | |
433 | code1 = bfd_get_8 (abfd, misc->contents + addr - 1); | |
434 | ||
435 | /* Is it INC 1(SP). */ | |
436 | if (! IS_INC_1_SP_OPCODE (code0, code1)) | |
437 | return false; | |
438 | ||
439 | code0 = bfd_get_8 (abfd, misc->contents + addr - 4); | |
440 | code1 = bfd_get_8 (abfd, misc->contents + addr - 3); | |
441 | ||
442 | /* Is it SNC. */ | |
443 | if (! IS_SNC_OPCODE (code0, code1)) | |
444 | return false; | |
445 | ||
446 | code0 = bfd_get_8 (abfd, misc->contents + addr - 6); | |
447 | code1 = bfd_get_8 (abfd, misc->contents + addr - 5); | |
448 | ||
449 | /* Is it ADD 2(SP),W. */ | |
450 | if (! IS_ADD_2_SP_W_OPCODE (code0, code1)) | |
451 | return false; | |
452 | ||
453 | code0 = bfd_get_8 (abfd, misc->contents + addr - 8); | |
454 | code1 = bfd_get_8 (abfd, misc->contents + addr - 7); | |
455 | ||
456 | if (relaxed) | |
457 | /* Is it INC 1(SP). */ | |
458 | return ! IS_INC_1_SP_OPCODE (code0, code1); | |
459 | ||
460 | else | |
461 | { | |
462 | /* Is it INC 1(SP). */ | |
463 | if (! IS_INC_1_SP_OPCODE (code0, code1)) | |
464 | return false; | |
465 | ||
466 | code0 = bfd_get_8 (abfd, misc->contents + addr - 10); | |
467 | code1 = bfd_get_8 (abfd, misc->contents + addr - 9); | |
468 | ||
469 | /* Is it SNC. */ | |
470 | if (! IS_SNC_OPCODE (code0, code1)) | |
471 | return false; | |
472 | ||
473 | code0 = bfd_get_8 (abfd, misc->contents + addr - 12); | |
474 | code1 = bfd_get_8 (abfd, misc->contents + addr - 11); | |
475 | ||
476 | /* Is it ADD W,WREG. */ | |
477 | if (! IS_ADD_W_WREG_OPCODE (code0, code1)) | |
478 | return false; | |
479 | } | |
480 | ||
481 | /* It looks like we've found the prologue for | |
482 | a 128-255 entry switch dispatch table. */ | |
483 | return true; | |
484 | } | |
485 | ||
486 | static boolean | |
487 | relax_switch_dispatch_tables_pass1 (abfd, sec, addr, misc) | |
488 | bfd *abfd; | |
489 | asection *sec; | |
490 | bfd_vma addr; | |
491 | struct misc *misc; | |
492 | { | |
493 | if (addr + 3 < sec->_cooked_size) | |
494 | { | |
495 | bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr + 2); | |
496 | bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 3); | |
497 | ||
498 | if (IS_JMP_OPCODE (code0, code1) | |
499 | && is_switch_128_dispatch_table_p (abfd, addr, false, misc)) | |
500 | { | |
501 | /* Delete ADD W,WREG from prologue. */ | |
502 | ip2k_elf_relax_delete_bytes (abfd, sec, addr - (2 * 2), (1 * 2)); | |
503 | return true; | |
504 | } | |
505 | ||
506 | if (IS_JMP_OPCODE (code0, code1) | |
507 | && is_switch_256_dispatch_table_p (abfd, addr, false, misc)) | |
508 | { | |
509 | /* Delete ADD W,WREG; SNC ; INC 1(SP) from prologue. */ | |
510 | ip2k_elf_relax_delete_bytes (abfd, sec, addr - 6 * 2, 3 * 2); | |
511 | return true; | |
512 | } | |
513 | } | |
514 | ||
515 | return true; | |
516 | } | |
517 | ||
518 | static boolean | |
519 | unrelax_dispatch_table_entries (abfd, sec, first, last, changed, misc) | |
520 | bfd *abfd; | |
521 | asection *sec; | |
522 | bfd_vma first; | |
523 | bfd_vma last; | |
524 | boolean *changed; | |
525 | struct misc *misc; | |
526 | { | |
527 | bfd_vma addr = first; | |
528 | ||
529 | while (addr < last) | |
530 | { | |
531 | bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr); | |
532 | bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr + 1); | |
533 | ||
534 | /* We are only expecting to find PAGE or JMP insns | |
535 | in the dispatch table. If we find anything else | |
536 | something has gone wrong failed the relaxation | |
537 | which will cause the link to be aborted. */ | |
538 | ||
539 | if (IS_PAGE_OPCODE (code0, code1)) | |
540 | /* Skip the PAGE and JMP insns. */ | |
541 | addr += 4; | |
542 | else if (IS_JMP_OPCODE (code0, code1)) | |
543 | { | |
544 | Elf_Internal_Rela * irelend = misc->irelbase | |
545 | + sec->reloc_count; | |
546 | Elf_Internal_Rela * irel; | |
547 | ||
548 | /* Find the relocation entry. */ | |
549 | for (irel = misc->irelbase; irel < irelend; irel++) | |
550 | { | |
551 | if (irel->r_offset == addr | |
552 | && ELF32_R_TYPE (irel->r_info) == R_IP2K_ADDR16CJP) | |
553 | { | |
554 | if (! add_page_insn (abfd, sec, irel, misc)) | |
555 | /* Something has gone wrong. */ | |
556 | return false; | |
557 | ||
558 | *changed = true; | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
563 | /* If we fell off the end something has gone wrong. */ | |
564 | if (irel >= irelend) | |
565 | /* Something has gone wrong. */ | |
566 | return false; | |
567 | ||
568 | /* Skip the PAGE and JMP isns. */ | |
569 | addr += 4; | |
570 | /* Acount for the new PAGE insn. */ | |
571 | last += 2; | |
572 | } | |
573 | else | |
574 | /* Something has gone wrong. */ | |
575 | return false; | |
576 | } | |
577 | ||
578 | return true; | |
579 | } | |
580 | ||
581 | static boolean | |
582 | unrelax_switch_dispatch_tables_passN (abfd, sec, addr, changed, misc) | |
583 | bfd *abfd; | |
584 | asection *sec; | |
585 | bfd_vma addr; | |
586 | boolean *changed; | |
587 | struct misc *misc; | |
588 | { | |
589 | if (2 <= addr && (addr + 3) < sec->_cooked_size) | |
590 | { | |
591 | bfd_byte code0 = bfd_get_8 (abfd, misc->contents + addr - 2); | |
592 | bfd_byte code1 = bfd_get_8 (abfd, misc->contents + addr - 1); | |
593 | ||
594 | if (IS_PAGE_OPCODE (code0, code1)) | |
595 | { | |
596 | addr -= 2; | |
597 | code0 = bfd_get_8 (abfd, misc->contents + addr + 2); | |
598 | code1 = bfd_get_8 (abfd, misc->contents + addr + 3); | |
599 | } | |
600 | else | |
601 | { | |
602 | code0 = bfd_get_8 (abfd, misc->contents + addr); | |
603 | code1 = bfd_get_8 (abfd, misc->contents + addr + 1); | |
604 | } | |
605 | ||
606 | if (IS_JMP_OPCODE (code0, code1) | |
607 | && is_switch_128_dispatch_table_p (abfd, addr, true, misc)) | |
608 | { | |
609 | bfd_vma first = addr; | |
610 | bfd_vma last = first; | |
611 | boolean relaxed = true; | |
612 | ||
613 | /* On the final pass we must check if *all* entries in the | |
614 | dispatch table are relaxed. If *any* are not relaxed | |
615 | then we must unrelax *all* the entries in the dispach | |
616 | table and also unrelax the dispatch table prologue. */ | |
617 | ||
618 | /* Find the last entry in the dispach table. */ | |
619 | while (last < sec->_cooked_size) | |
620 | { | |
621 | code0 = bfd_get_8 (abfd, misc->contents + last); | |
622 | code1 = bfd_get_8 (abfd, misc->contents + last + 1); | |
623 | ||
624 | if (IS_PAGE_OPCODE (code0, code1)) | |
625 | relaxed = false; | |
626 | else if (! IS_JMP_OPCODE (code0, code1)) | |
627 | break; | |
628 | ||
629 | last += 2; | |
630 | } | |
631 | ||
632 | /* We should have found the end of the dispatch table | |
633 | before reaching the end of the section. If we've have | |
634 | reached the end then fail the relaxation which will | |
635 | cause the link to be aborted. */ | |
636 | if (last >= sec->_cooked_size) | |
637 | /* Something has gone wrong. */ | |
638 | return false; | |
639 | ||
640 | /* If we found an unrelaxed entry then | |
641 | unlrelax all the switch table entries. */ | |
642 | if (! relaxed ) | |
643 | { | |
644 | if (! unrelax_dispatch_table_entries (abfd, sec, first, | |
645 | last, changed, misc)) | |
646 | /* Something has gone wrong. */ | |
647 | return false; | |
648 | ||
649 | if (! is_switch_128_dispatch_table_p (abfd, addr, true, misc)) | |
650 | /* Something has gone wrong. */ | |
651 | return false; | |
652 | ||
653 | /* Unrelax the prologue. */ | |
654 | ||
655 | /* Insert an ADD W,WREG insnstruction. */ | |
656 | if (! ip2k_elf_relax_add_bytes (abfd, sec, | |
657 | addr - 2, | |
658 | add_w_wreg_opcode, | |
659 | sizeof (add_w_wreg_opcode), | |
660 | 0)) | |
661 | /* Something has gone wrong. */ | |
662 | return false; | |
663 | } | |
664 | ||
665 | return true; | |
666 | } | |
667 | ||
668 | if (IS_JMP_OPCODE (code0, code1) | |
669 | && is_switch_256_dispatch_table_p (abfd, addr, true, misc)) | |
670 | { | |
671 | bfd_vma first = addr; | |
672 | bfd_vma last; | |
673 | boolean relaxed = true; | |
674 | ||
675 | /* On the final pass we must check if *all* entries in the | |
676 | dispatch table are relaxed. If *any* are not relaxed | |
677 | then we must unrelax *all* the entries in the dispach | |
678 | table and also unrelax the dispatch table prologue. */ | |
679 | ||
680 | /* Note the 1st PAGE/JMP instructions are part of the | |
681 | prologue and can safely be relaxed. */ | |
682 | ||
683 | code0 = bfd_get_8 (abfd, misc->contents + first); | |
684 | code1 = bfd_get_8 (abfd, misc->contents + first + 1); | |
685 | ||
686 | if (IS_PAGE_OPCODE (code0, code1)) | |
687 | { | |
688 | first += 2; | |
689 | code0 = bfd_get_8 (abfd, misc->contents + first); | |
690 | code1 = bfd_get_8 (abfd, misc->contents + first + 1); | |
691 | } | |
692 | ||
693 | if (! IS_JMP_OPCODE (code0, code1)) | |
694 | /* Something has gone wrong. */ | |
695 | return false; | |
696 | ||
697 | first += 2; | |
698 | last = first; | |
699 | ||
700 | /* Find the last entry in the dispach table. */ | |
701 | while (last < sec->_cooked_size) | |
702 | { | |
703 | code0 = bfd_get_8 (abfd, misc->contents + last); | |
704 | code1 = bfd_get_8 (abfd, misc->contents + last + 1); | |
705 | ||
706 | if (IS_PAGE_OPCODE (code0, code1)) | |
707 | relaxed = false; | |
708 | else if (! IS_JMP_OPCODE (code0, code1)) | |
709 | break; | |
710 | ||
711 | last += 2; | |
712 | } | |
713 | ||
714 | /* We should have found the end of the dispatch table | |
715 | before reaching the end of the section. If we have | |
716 | reached the end of the section then fail the | |
717 | relaxation. */ | |
718 | if (last >= sec->_cooked_size) | |
719 | return false; | |
720 | ||
721 | /* If we found an unrelaxed entry then | |
722 | unrelax all the switch table entries. */ | |
723 | if (! relaxed) | |
724 | { | |
725 | if (! unrelax_dispatch_table_entries (abfd, sec, first, | |
726 | last, changed, misc)) | |
727 | return false; | |
728 | ||
729 | if (! is_switch_256_dispatch_table_p (abfd, addr, true, misc)) | |
730 | return false; | |
731 | ||
732 | /* Unrelax the prologue. */ | |
733 | ||
734 | /* Insert an INC 1(SP) insnstruction. */ | |
735 | if (! ip2k_elf_relax_add_bytes (abfd, sec, | |
736 | addr - 6, | |
737 | inc_1_sp_opcode, | |
738 | sizeof (inc_1_sp_opcode), | |
739 | 0)) | |
740 | return false; | |
741 | ||
742 | /* Insert an SNC insnstruction. */ | |
743 | if (! ip2k_elf_relax_add_bytes (abfd, sec, | |
744 | addr - 6, | |
745 | snc_opcode, | |
746 | sizeof (snc_opcode), | |
747 | 0)) | |
748 | return false; | |
749 | ||
750 | /* Insert an ADD W,WREG insnstruction. */ | |
751 | if (! ip2k_elf_relax_add_bytes (abfd, sec, | |
752 | addr - 6, | |
753 | add_w_wreg_opcode, | |
754 | sizeof (add_w_wreg_opcode), | |
755 | 0)) | |
756 | return false; | |
757 | } | |
758 | ||
759 | return true; | |
760 | } | |
761 | } | |
762 | ||
763 | return true; | |
764 | } | |
765 | ||
766 | /* This function handles relaxing for the ip2k. */ | |
767 | ||
768 | static boolean | |
769 | ip2k_elf_relax_section (abfd, sec, link_info, again) | |
770 | bfd *abfd; | |
771 | asection *sec; | |
772 | struct bfd_link_info *link_info; | |
773 | boolean *again; | |
774 | { | |
775 | Elf_External_Sym_Shndx *shndx_buf; | |
776 | Elf_Internal_Shdr *shndx_hdr; | |
777 | static asection * first_section = NULL; | |
778 | static asection * last_section = NULL; | |
779 | static boolean changed = false; | |
780 | static boolean final_pass = false; | |
781 | static unsigned int pass = 0; | |
782 | struct misc misc; | |
783 | asection *stab; | |
784 | ||
785 | /* Assume nothing changes. */ | |
786 | *again = false; | |
787 | ||
788 | if (first_section == NULL) | |
789 | first_section = sec; | |
790 | ||
791 | if (first_section == sec) | |
792 | { | |
793 | changed = false; | |
794 | pass++; | |
795 | } | |
796 | ||
797 | /* If we make too many passes then it's a sign that | |
798 | something is wrong and we fail the relaxation. | |
799 | Note if everything is working correctly then the | |
800 | relaxation should converge reasonably quickly. */ | |
801 | if (pass == 4096) | |
802 | return false; | |
803 | ||
804 | /* We don't have to do anything for a relocatable link, | |
805 | if this section does not have relocs, or if this is | |
806 | not a code section. */ | |
807 | if (link_info->relocateable | |
808 | || (sec->flags & SEC_RELOC) == 0 | |
809 | || sec->reloc_count == 0 | |
810 | || (sec->flags & SEC_CODE) == 0) | |
811 | return true; | |
812 | ||
813 | if (pass == 1) | |
814 | last_section = sec; | |
815 | ||
816 | misc.symtab_hdr = NULL; | |
817 | misc.irelbase = NULL; | |
818 | misc.contents = NULL; | |
819 | misc.free_contents = NULL; | |
820 | misc.extsyms = NULL; | |
821 | misc.free_extsyms = NULL; | |
822 | misc.free_relocs = NULL; | |
823 | ||
824 | /* If this is the first time we have been called | |
825 | for this section, initialise the cooked size. */ | |
826 | if (sec->_cooked_size == 0) | |
827 | sec->_cooked_size = sec->_raw_size; | |
828 | ||
829 | misc.symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
830 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
831 | ||
832 | misc.irelbase = _bfd_elf32_link_read_relocs (abfd, sec, NULL, | |
833 | (Elf_Internal_Rela *)NULL, | |
834 | link_info->keep_memory); | |
835 | if (misc.irelbase == NULL) | |
836 | { | |
837 | tidyup_after_error (&misc); | |
838 | return false; | |
839 | } | |
840 | ||
841 | if (! link_info->keep_memory) | |
842 | misc.free_relocs = misc.irelbase; | |
843 | ||
844 | /* Make sure the stac.rela stuff gets read in. */ | |
845 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
846 | ||
847 | if (stab) | |
848 | { | |
849 | /* So stab does exits. */ | |
850 | Elf_Internal_Rela * irelbase; | |
851 | ||
852 | irelbase = _bfd_elf32_link_read_relocs (abfd, stab, NULL, | |
853 | (Elf_Internal_Rela *)NULL, | |
854 | link_info->keep_memory); | |
855 | } | |
856 | ||
857 | /* Get section contents cached copy if it exists. */ | |
858 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
859 | misc.contents = elf_section_data (sec)->this_hdr.contents; | |
860 | else | |
861 | { | |
862 | /* Go get them of disk. */ | |
863 | misc.contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
864 | if (misc.contents == NULL) | |
865 | { | |
866 | tidyup_after_error (&misc); | |
867 | return false; | |
868 | } | |
869 | ||
870 | misc.free_contents = misc.contents; | |
871 | if (! bfd_get_section_contents (abfd, sec, misc.contents, | |
872 | (file_ptr)0, | |
873 | sec->_raw_size)) | |
874 | { | |
875 | tidyup_after_error (&misc); | |
876 | return false; | |
877 | } | |
878 | } | |
879 | ||
880 | /* Read this BFD's symbols cached copy if it exists. */ | |
881 | if (misc.symtab_hdr->contents != NULL) | |
882 | misc.extsyms = (Elf32_External_Sym *) misc.symtab_hdr->contents; | |
883 | else | |
884 | { | |
885 | /* Go get them off disk. */ | |
886 | misc.extsyms = ((Elf32_External_Sym *)bfd_malloc (misc.symtab_hdr->sh_size)); | |
887 | if (misc.extsyms == NULL) | |
888 | { | |
889 | tidyup_after_error (&misc); | |
890 | return false; | |
891 | } | |
892 | ||
893 | misc.free_extsyms = misc.extsyms; | |
894 | if (bfd_seek (abfd, misc.symtab_hdr->sh_offset, SEEK_SET) != 0 | |
895 | || (bfd_read (misc.extsyms, 1, misc.symtab_hdr->sh_size, abfd) | |
896 | != misc.symtab_hdr->sh_size)) | |
897 | { | |
898 | tidyup_after_error (&misc); | |
899 | return false; | |
900 | } | |
901 | } | |
902 | ||
903 | if (shndx_hdr->sh_size != 0) | |
904 | { | |
905 | bfd_size_type amt; | |
906 | ||
907 | amt = misc.symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx); | |
908 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
909 | if (shndx_buf == NULL) | |
910 | { | |
911 | tidyup_after_error (&misc); | |
912 | return false; | |
913 | } | |
914 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
915 | || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt) | |
916 | { | |
917 | tidyup_after_error (&misc); | |
918 | return false; | |
919 | } | |
920 | shndx_hdr->contents = (PTR) shndx_buf; | |
921 | } | |
922 | ||
923 | /* This is where all the relaxation actually get done. */ | |
924 | ||
925 | if (pass == 1) | |
926 | { | |
927 | /* On the first pass we remove *all* page instructions and | |
928 | relax the prolog for switch dispatch tables. This gets | |
929 | us to the starting point for subsequent passes where | |
930 | we add page instructions back in as needed. */ | |
931 | ||
932 | if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, &misc)) | |
933 | { | |
934 | tidyup_after_error (&misc); | |
935 | return false; | |
936 | } | |
937 | ||
938 | changed |= *again; | |
939 | } | |
940 | else | |
941 | { | |
942 | /* Add page instructions back in as needed but we ignore | |
943 | the issue with sections (functions) crossing a page | |
944 | boundary until we have converged to an approximate | |
945 | solution (i.e. nothing has changed on this relaxation | |
946 | pass) and we then know roughly where the page boundaries | |
947 | will end up. | |
948 | ||
949 | After we have have converged to an approximate solution | |
950 | we set the final pass flag and continue relaxing. On these | |
951 | final passes if a section (function) cross page boundary | |
952 | we will add *all* the page instructions back into such | |
953 | sections. | |
954 | ||
955 | After adding *all* page instructions back into a section | |
956 | which crosses a page bounbdary we reset the final pass flag | |
957 | so the we will again interate until we find a new approximate | |
958 | solution which is closer to the final solution. */ | |
959 | ||
960 | if (! ip2k_elf_relax_section_passN (abfd, sec, again, | |
961 | &final_pass, &misc)) | |
962 | { | |
963 | tidyup_after_error (&misc); | |
964 | return false; | |
965 | } | |
966 | ||
967 | changed |= *again; | |
968 | ||
969 | /* If nothing has changed on this relaxation | |
970 | pass restart the final relaxaton pass. */ | |
971 | if (! changed && last_section == sec) | |
972 | { | |
973 | /* If this was the final pass and we didn't reset | |
974 | the final pass flag then we are done, otherwise | |
975 | do another final pass. */ | |
976 | if (! final_pass) | |
977 | { | |
978 | final_pass = true; | |
979 | *again = true; | |
980 | } | |
981 | } | |
982 | } | |
983 | ||
984 | /* Perform some house keeping after relaxing the section. */ | |
985 | ||
986 | if (misc.free_relocs != NULL) | |
987 | { | |
988 | free (misc.free_relocs); | |
989 | misc.free_relocs = NULL; | |
990 | } | |
991 | ||
992 | if (misc.free_contents != NULL) | |
993 | { | |
994 | if (! link_info->keep_memory) | |
995 | free (misc.free_contents); | |
996 | else | |
997 | { | |
998 | /* Cache the section contents for elf_link_input_bfd. */ | |
999 | elf_section_data (sec)->this_hdr.contents = misc.contents; | |
1000 | } | |
1001 | ||
1002 | misc.free_contents = NULL; | |
1003 | } | |
1004 | ||
1005 | if (misc.free_extsyms != NULL) | |
1006 | { | |
1007 | if (! link_info->keep_memory) | |
1008 | free (misc.free_extsyms); | |
1009 | else | |
1010 | { | |
1011 | /* Cache the symbols for elf_link_input_bfd. */ | |
1012 | misc.symtab_hdr->contents = misc.extsyms; | |
1013 | } | |
1014 | ||
1015 | misc.free_extsyms = NULL; | |
1016 | } | |
1017 | ||
1018 | return true; | |
1019 | } | |
1020 | ||
1021 | static void | |
1022 | tidyup_after_error (misc) | |
1023 | struct misc *misc; | |
1024 | { | |
1025 | if (misc->free_relocs != NULL) | |
1026 | { | |
1027 | free (misc->free_relocs); | |
1028 | misc->free_relocs = NULL; | |
1029 | } | |
1030 | ||
1031 | if (misc->free_contents != NULL) | |
1032 | { | |
1033 | free (misc->free_contents); | |
1034 | misc->free_contents = NULL; | |
1035 | } | |
1036 | ||
1037 | if (misc->free_extsyms != NULL) | |
1038 | { | |
1039 | free (misc->free_extsyms); | |
1040 | misc->free_extsyms = NULL; | |
1041 | } | |
1042 | ||
1043 | return; | |
1044 | } | |
1045 | ||
1046 | /* This function handles relaxation during the first pass. */ | |
1047 | ||
1048 | static boolean | |
1049 | ip2k_elf_relax_section_pass1 (abfd, sec, again, misc) | |
1050 | bfd *abfd; | |
1051 | asection *sec; | |
1052 | boolean *again; | |
1053 | struct misc * misc; | |
1054 | { | |
1055 | Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count; | |
1056 | Elf_Internal_Rela *irel; | |
1057 | ||
1058 | /* Walk thru the section looking for relaxation opertunities. */ | |
1059 | for (irel = misc->irelbase; irel < irelend; irel++) | |
1060 | { | |
1061 | if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_PAGE3) | |
1062 | { | |
1063 | bfd_byte code0 = bfd_get_8 (abfd, | |
1064 | misc->contents + irel->r_offset); | |
1065 | bfd_byte code1 = bfd_get_8 (abfd, | |
1066 | misc->contents + irel->r_offset + 1); | |
1067 | ||
1068 | /* Verify that this is the PAGE opcode. */ | |
1069 | if (IS_PAGE_OPCODE (code0, code1)) | |
1070 | { | |
1071 | /* Note that we've changed the relocs, section contents, etc. */ | |
1072 | elf_section_data (sec)->relocs = misc->irelbase; | |
1073 | misc->free_relocs = NULL; | |
1074 | ||
1075 | elf_section_data (sec)->this_hdr.contents = misc->contents; | |
1076 | misc->free_contents = NULL; | |
1077 | ||
1078 | misc->symtab_hdr->contents = (bfd_byte *) misc->extsyms; | |
1079 | misc->free_extsyms = NULL; | |
1080 | ||
1081 | /* Handle switch dispatch tables/prologues. */ | |
1082 | if (! relax_switch_dispatch_tables_pass1 (abfd, sec, | |
1083 | irel->r_offset, misc)) | |
1084 | return false; | |
1085 | ||
1086 | /* Fix the relocation's type. */ | |
1087 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1088 | R_IP2K_NONE); | |
1089 | ||
1090 | /* Delete the PAGE insn. */ | |
1091 | if (! ip2k_elf_relax_delete_bytes (abfd, sec, | |
1092 | irel->r_offset, | |
1093 | sizeof (page_opcode))) | |
1094 | return false; | |
1095 | ||
1096 | /* That will change things, so, we should relax again. | |
1097 | Note that this is not required, and it may be slow. */ | |
1098 | *again = true; | |
1099 | } | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | return true; | |
1104 | } | |
1105 | ||
1106 | /* This function handles relaxation for 2nd and subsequent passes. */ | |
1107 | ||
1108 | static boolean | |
1109 | ip2k_elf_relax_section_passN (abfd, sec, again, final_pass, misc) | |
1110 | bfd *abfd; | |
1111 | asection *sec; | |
1112 | boolean *again; | |
1113 | boolean *final_pass; | |
1114 | struct misc * misc; | |
1115 | { | |
1116 | Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count; | |
1117 | Elf_Internal_Rela *irel; | |
1118 | boolean add_all; | |
1119 | ||
1120 | /* If we are on the final relaxation pass and the section crosses | |
1121 | then set a flag to indicate that *all* page instructions need | |
1122 | to be added back into this section. */ | |
1123 | if (*final_pass) | |
1124 | { | |
1125 | add_all = (PAGENO (BASEADDR (sec)) | |
1126 | != PAGENO (BASEADDR (sec) + sec->_cooked_size)); | |
1127 | ||
1128 | /* If this section crosses a page boundary set the crossed | |
1129 | page boundary flag. */ | |
1130 | if (add_all) | |
1131 | sec->userdata = sec; | |
1132 | else | |
1133 | { | |
1134 | /* If the section had previously crossed a page boundary | |
1135 | but on this pass does not then reset crossed page | |
1136 | boundary flag and rerun the 1st relaxation pass on | |
1137 | this section. */ | |
1138 | if (sec->userdata) | |
1139 | { | |
1140 | sec->userdata = NULL; | |
1141 | if (! ip2k_elf_relax_section_pass1 (abfd, sec, again, misc)) | |
1142 | return false; | |
1143 | } | |
1144 | } | |
1145 | } | |
1146 | else | |
1147 | add_all = false; | |
1148 | ||
1149 | /* Walk thru the section looking for call/jmp | |
1150 | instructions which need a page instruction. */ | |
1151 | for (irel = misc->irelbase; irel < irelend; irel++) | |
1152 | { | |
1153 | if (ELF32_R_TYPE (irel->r_info) == (int) R_IP2K_ADDR16CJP) | |
1154 | { | |
1155 | /* Get the value of the symbol referred to by the reloc. */ | |
1156 | bfd_vma symval = symbol_value (abfd, misc->symtab_hdr, misc->extsyms, | |
1157 | irel); | |
1158 | bfd_byte code0, code1; | |
1159 | ||
1160 | if (symval == UNDEFINED_SYMBOL) | |
1161 | { | |
1162 | /* This appears to be a reference to an undefined | |
1163 | symbol. Just ignore it--it will be caught by the | |
1164 | regular reloc processing. */ | |
1165 | continue; | |
1166 | } | |
1167 | ||
1168 | /* For simplicity of coding, we are going to modify the section | |
1169 | contents, the section relocs, and the BFD symbol table. We | |
1170 | must tell the rest of the code not to free up this | |
1171 | information. It would be possible to instead create a table | |
1172 | of changes which have to be made, as is done in coff-mips.c; | |
1173 | that would be more work, but would require less memory when | |
1174 | the linker is run. */ | |
1175 | ||
1176 | /* Get the opcode. */ | |
1177 | code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset); | |
1178 | code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1); | |
1179 | ||
1180 | if (IS_JMP_OPCODE (code0, code1) || IS_CALL_OPCODE (code0, code1)) | |
1181 | { | |
1182 | if (*final_pass) | |
1183 | { | |
1184 | if (! unrelax_switch_dispatch_tables_passN (abfd, sec, | |
1185 | irel->r_offset, | |
1186 | again, misc)) | |
1187 | return false; | |
1188 | ||
1189 | if (*again) | |
1190 | add_all = false; | |
1191 | } | |
1192 | ||
1193 | code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 2); | |
1194 | code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset - 1); | |
1195 | ||
1196 | if (! IS_PAGE_OPCODE (code0, code1)) | |
1197 | { | |
1198 | bfd_vma value = symval + irel->r_addend; | |
1199 | bfd_vma addr = BASEADDR (sec) + irel->r_offset; | |
1200 | ||
1201 | if (add_all || PAGENO (addr) != PAGENO (value)) | |
1202 | { | |
1203 | if (! add_page_insn (abfd, sec, irel, misc)) | |
1204 | return false; | |
1205 | ||
1206 | /* That will have changed things, so, we must relax again. */ | |
1207 | *again = true; | |
1208 | } | |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | /* If anything changed reset the final pass flag. */ | |
1215 | if (*again) | |
1216 | *final_pass = false; | |
1217 | ||
1218 | return true; | |
1219 | } | |
1220 | ||
1221 | /* Parts of a Stabs entry. */ | |
1222 | ||
1223 | #define STRDXOFF (0) | |
1224 | #define TYPEOFF (4) | |
1225 | #define OTHEROFF (5) | |
1226 | #define DESCOFF (6) | |
1227 | #define VALOFF (8) | |
1228 | #define STABSIZE (12) | |
1229 | ||
1230 | /* Adjust all the relocations entries after adding or inserting instructions. */ | |
1231 | ||
1232 | static void | |
1233 | adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj) | |
1234 | bfd *abfd; | |
1235 | asection *sec; | |
1236 | bfd_vma addr; | |
1237 | bfd_vma endaddr; | |
1238 | int count; | |
1239 | int noadj; | |
1240 | { | |
1241 | Elf_Internal_Shdr *symtab_hdr; | |
1242 | Elf32_External_Sym *extsyms; | |
1243 | int shndx, index; | |
1244 | bfd_byte *contents; | |
1245 | Elf_Internal_Rela *irel, *irelend, *irelbase; | |
1246 | Elf32_External_Sym *esym, *esymend; | |
1247 | asection *stab; | |
1248 | bfd_byte *stabp, *stabend, *stabcontents; | |
1249 | Elf_Internal_Shdr *shndx_hdr; | |
1250 | Elf_External_Sym_Shndx *sym_shndx; | |
1251 | ||
1252 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1253 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1254 | ||
1255 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1256 | ||
1257 | contents = elf_section_data (sec)->this_hdr.contents; | |
1258 | ||
1259 | irelbase = elf_section_data (sec)->relocs; | |
1260 | irelend = irelbase + sec->reloc_count; | |
1261 | ||
1262 | for (irel = irelbase; irel < irelend; irel++) | |
1263 | { | |
1264 | if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE) | |
1265 | { | |
1266 | /* Get the value of the symbol referred to by the reloc. */ | |
1267 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
1268 | { | |
1269 | Elf_Internal_Sym isym; | |
1270 | asection *sym_sec; | |
1271 | Elf_External_Sym_Shndx *sym_shndx; | |
1272 | Elf_Internal_Shdr *shndx_hdr; | |
1273 | ||
1274 | /* A local symbol. */ | |
1275 | ||
1276 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1277 | sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
1278 | sym_shndx = (sym_shndx | |
1279 | ? sym_shndx + ELF32_R_SYM (irel->r_info) : NULL); | |
1280 | bfd_elf32_swap_symbol_in (abfd, | |
1281 | extsyms + ELF32_R_SYM (irel->r_info), | |
1282 | sym_shndx, &isym); | |
1283 | ||
1284 | if (isym.st_shndx == SHN_UNDEF) | |
1285 | sym_sec = bfd_und_section_ptr; | |
1286 | else if (isym.st_shndx == SHN_ABS) | |
1287 | sym_sec = bfd_abs_section_ptr; | |
1288 | else if (isym.st_shndx == SHN_COMMON) | |
1289 | sym_sec = bfd_com_section_ptr; | |
1290 | else | |
1291 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); | |
1292 | ||
1293 | if (sym_sec == sec) | |
1294 | { | |
1295 | bfd_vma baseaddr = BASEADDR (sec); | |
1296 | bfd_vma symval = BASEADDR (sym_sec) + isym.st_value | |
1297 | + irel->r_addend; | |
1298 | ||
1299 | if ((baseaddr + addr + noadj) <= symval | |
1300 | && symval < (baseaddr + endaddr)) | |
1301 | irel->r_addend += count; | |
1302 | } | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | /* Do this only for PC space relocations. */ | |
1307 | if (addr <= irel->r_offset && irel->r_offset < endaddr) | |
1308 | irel->r_offset += count; | |
1309 | } | |
1310 | ||
1311 | /* Now fix the stab relocations. */ | |
1312 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
1313 | if (stab) | |
1314 | { | |
1315 | irelbase = elf_section_data (stab)->relocs; | |
1316 | irelend = irelbase + stab->reloc_count; | |
1317 | ||
1318 | /* Pull out the contents of the stab section. */ | |
1319 | if (elf_section_data (stab)->this_hdr.contents != NULL) | |
1320 | stabcontents = elf_section_data (stab)->this_hdr.contents; | |
1321 | else | |
1322 | { | |
1323 | stabcontents = (bfd_byte *) bfd_alloc (abfd, stab->_raw_size); | |
1324 | if (stabcontents == NULL) | |
1325 | return; | |
1326 | if (! bfd_get_section_contents (abfd, stab, stabcontents, | |
1327 | (file_ptr) 0, stab->_raw_size)) | |
1328 | return; | |
1329 | ||
1330 | /* We need to remember this. */ | |
1331 | elf_section_data (stab)->this_hdr.contents = stabcontents; | |
1332 | } | |
1333 | ||
1334 | stabend = stabcontents + stab->_raw_size; | |
1335 | ||
1336 | for (irel = irelbase; irel < irelend; irel++) | |
1337 | { | |
1338 | if (ELF32_R_TYPE (irel->r_info) != R_IP2K_NONE) | |
1339 | { | |
1340 | /* Get the value of the symbol referred to by the reloc. */ | |
1341 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
1342 | { | |
1343 | Elf_Internal_Sym isym; | |
1344 | asection *sym_sec; | |
1345 | Elf_External_Sym_Shndx *sym_shndx; | |
1346 | Elf_Internal_Shdr *shndx_hdr; | |
1347 | ||
1348 | /* A local symbol. */ | |
1349 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1350 | sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
1351 | sym_shndx = (sym_shndx | |
1352 | ? sym_shndx + ELF32_R_SYM (irel->r_info) | |
1353 | : NULL); | |
1354 | ||
1355 | bfd_elf32_swap_symbol_in (abfd, | |
1356 | (extsyms | |
1357 | + ELF32_R_SYM (irel->r_info)), | |
1358 | sym_shndx, &isym); | |
1359 | ||
1360 | if (isym.st_shndx == SHN_UNDEF) | |
1361 | sym_sec = bfd_und_section_ptr; | |
1362 | else if (isym.st_shndx == SHN_ABS) | |
1363 | sym_sec = bfd_abs_section_ptr; | |
1364 | else if (isym.st_shndx == SHN_COMMON) | |
1365 | sym_sec = bfd_com_section_ptr; | |
1366 | else | |
1367 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); | |
1368 | ||
1369 | if (sym_sec == sec) | |
1370 | { | |
1371 | const char *name; | |
1372 | unsigned long strx; | |
1373 | unsigned char type, other; | |
1374 | unsigned short desc; | |
1375 | bfd_vma value; | |
1376 | bfd_vma baseaddr = BASEADDR (sec); | |
1377 | bfd_vma symval = BASEADDR (sym_sec) + isym.st_value | |
1378 | + irel->r_addend; | |
1379 | ||
1380 | if ((baseaddr + addr) <= symval | |
1381 | && symval <= (baseaddr + endaddr)) | |
1382 | irel->r_addend += count; | |
1383 | ||
1384 | /* Go hunt up a function and fix its line info if needed. */ | |
1385 | stabp = stabcontents + irel->r_offset - 8; | |
1386 | ||
1387 | /* Go pullout the stab entry. */ | |
1388 | strx = bfd_h_get_32 (abfd, stabp + STRDXOFF); | |
1389 | type = bfd_h_get_8 (abfd, stabp + TYPEOFF); | |
1390 | other = bfd_h_get_8 (abfd, stabp + OTHEROFF); | |
1391 | desc = bfd_h_get_16 (abfd, stabp + DESCOFF); | |
1392 | value = bfd_h_get_32 (abfd, stabp + VALOFF); | |
1393 | ||
1394 | name = bfd_get_stab_name (type); | |
1395 | ||
1396 | if (strcmp (name, "FUN") == 0) | |
1397 | { | |
1398 | int function_adjusted = 0; | |
1399 | ||
1400 | if (symval > (baseaddr + addr)) | |
1401 | /* Not in this function. */ | |
1402 | continue; | |
1403 | ||
1404 | /* Hey we got a function hit. */ | |
1405 | stabp += STABSIZE; | |
1406 | for (;stabp < stabend; stabp += STABSIZE) | |
1407 | { | |
1408 | /* Go pullout the stab entry. */ | |
1409 | strx = bfd_h_get_32 (abfd, stabp + STRDXOFF); | |
1410 | type = bfd_h_get_8 (abfd, stabp + TYPEOFF); | |
1411 | other = bfd_h_get_8 (abfd, stabp + OTHEROFF); | |
1412 | desc = bfd_h_get_16 (abfd, stabp + DESCOFF); | |
1413 | value = bfd_h_get_32 (abfd, stabp + VALOFF); | |
1414 | name = bfd_get_stab_name (type); | |
1415 | ||
1416 | if (strcmp (name, "FUN") == 0) | |
1417 | { | |
1418 | /* Hit another function entry. */ | |
1419 | if (function_adjusted) | |
1420 | { | |
1421 | /* Adjust the value. */ | |
1422 | value += count; | |
1423 | ||
1424 | /* We need to put it back. */ | |
1425 | bfd_h_put_32 (abfd, value,stabp + VALOFF); | |
1426 | } | |
1427 | ||
1428 | /* And then bale out. */ | |
1429 | break; | |
1430 | } | |
1431 | ||
1432 | if (strcmp (name, "SLINE") == 0) | |
1433 | { | |
1434 | /* Got a line entry. */ | |
1435 | if ((baseaddr + addr) <= (symval + value)) | |
1436 | { | |
1437 | /* Adjust the line entry. */ | |
1438 | value += count; | |
1439 | ||
1440 | /* We need to put it back. */ | |
1441 | bfd_h_put_32 (abfd, value,stabp + VALOFF); | |
1442 | function_adjusted = 1; | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | } | |
1447 | } | |
1448 | } | |
1449 | } | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | /* When adding an instruction back it is sometimes necessary to move any | |
1454 | global or local symbol that was referencing the first instruction of | |
1455 | the moved block to refer to the first instruction of the inserted block. | |
1456 | ||
1457 | For example adding a PAGE instruction before a CALL or JMP requires | |
1458 | that any label on the CALL or JMP is moved to the PAGE insn. */ | |
1459 | addr += noadj; | |
1460 | ||
1461 | /* Adjust the local symbols defined in this section. */ | |
1462 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1463 | sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
1464 | esym = extsyms; | |
1465 | esymend = esym + symtab_hdr->sh_info; | |
1466 | for (; esym < esymend; esym++, sym_shndx = (sym_shndx ? sym_shndx + 1: NULL)) | |
1467 | { | |
1468 | Elf_Internal_Sym isym; | |
1469 | Elf_External_Sym_Shndx dummy; | |
1470 | ||
1471 | bfd_elf32_swap_symbol_in (abfd, esym, sym_shndx, &isym); | |
1472 | ||
1473 | if (isym.st_shndx == shndx) | |
1474 | { | |
1475 | if (addr <= isym.st_value && isym.st_value < endaddr) | |
1476 | { | |
1477 | isym.st_value += count; | |
1478 | bfd_elf32_swap_symbol_out (abfd, &isym, esym, &dummy); | |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | /* Now adjust the global symbols defined in this section. */ | |
1484 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1485 | sym_shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
1486 | esym = extsyms + symtab_hdr->sh_info; | |
1487 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); | |
1488 | for (index = 0; esym < esymend; | |
1489 | esym++, index++, sym_shndx = (sym_shndx ? sym_shndx + 1: NULL)) | |
1490 | { | |
1491 | Elf_Internal_Sym isym; | |
1492 | struct elf_link_hash_entry *sym_hash; | |
1493 | ||
1494 | bfd_elf32_swap_symbol_in (abfd, esym, sym_shndx, &isym); | |
1495 | sym_hash = elf_sym_hashes (abfd)[index]; | |
1496 | ||
1497 | if (isym.st_shndx == shndx | |
1498 | && (sym_hash->root.type == bfd_link_hash_defined | |
1499 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1500 | && sym_hash->root.u.def.section == sec) | |
1501 | { | |
1502 | if (addr <= sym_hash->root.u.def.value | |
1503 | && sym_hash->root.u.def.value < endaddr) | |
1504 | { | |
1505 | Elf_External_Sym_Shndx dummy; | |
1506 | ||
1507 | sym_hash->root.u.def.value += count; | |
1508 | bfd_elf32_swap_symbol_out (abfd, &isym, esym, &dummy); | |
1509 | } | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | return; | |
1514 | } | |
1515 | ||
1516 | static boolean | |
1517 | add_page_insn (abfd, sec, irel, misc) | |
1518 | bfd *abfd; | |
1519 | asection *sec; | |
1520 | Elf_Internal_Rela *irel; | |
1521 | struct misc *misc; | |
1522 | { | |
1523 | /* Note that we've changed the relocs, section contents, etc. */ | |
1524 | elf_section_data (sec)->relocs = misc->irelbase; | |
1525 | misc->free_relocs = NULL; | |
1526 | ||
1527 | elf_section_data (sec)->this_hdr.contents = misc->contents; | |
1528 | misc->free_contents = NULL; | |
1529 | ||
1530 | misc->symtab_hdr->contents = (bfd_byte *) misc->extsyms; | |
1531 | misc->free_extsyms = NULL; | |
1532 | ||
1533 | /* Add the PAGE insn. */ | |
1534 | if (! ip2k_elf_relax_add_bytes (abfd, sec, irel->r_offset, | |
1535 | page_opcode, | |
1536 | sizeof (page_opcode), | |
1537 | sizeof (page_opcode))) | |
1538 | return false; | |
1539 | else | |
1540 | { | |
1541 | Elf32_Internal_Rela * jrel = irel - 1; | |
1542 | ||
1543 | /* Add relocation for PAGE insn added. */ | |
1544 | if (ELF32_R_TYPE (jrel->r_info) != R_IP2K_NONE) | |
1545 | { | |
1546 | bfd_byte code0, code1; | |
1547 | char *msg = NULL; | |
1548 | ||
1549 | /* Get the opcode. */ | |
1550 | code0 = bfd_get_8 (abfd, misc->contents + irel->r_offset); | |
1551 | code1 = bfd_get_8 (abfd, misc->contents + irel->r_offset + 1); | |
1552 | ||
1553 | if (IS_JMP_OPCODE (code0, code1)) | |
1554 | msg = "\tJMP instruction missing a preceeding PAGE instruction in %s\n\n"; | |
1555 | ||
1556 | else if (IS_CALL_OPCODE (code0, code1)) | |
1557 | msg = "\tCALL instruction missing a preceeding PAGE instruction in %s\n\n"; | |
1558 | ||
1559 | if (msg) | |
1560 | { | |
1561 | fprintf (stderr, "\n\t *** LINKER RELAXATION failure ***\n"); | |
1562 | fprintf (stderr, msg, sec->owner->filename); | |
1563 | } | |
1564 | ||
1565 | return false; | |
1566 | } | |
1567 | ||
1568 | jrel->r_addend = irel->r_addend; | |
1569 | jrel->r_offset = irel->r_offset - sizeof (page_opcode); | |
1570 | jrel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1571 | R_IP2K_PAGE3); | |
1572 | } | |
1573 | ||
1574 | return true; | |
1575 | } | |
1576 | ||
1577 | /* Insert bytes into a section while relaxing. */ | |
1578 | ||
1579 | static boolean | |
1580 | ip2k_elf_relax_add_bytes (abfd, sec, addr, bytes, count, noadj) | |
1581 | bfd *abfd; | |
1582 | asection *sec; | |
1583 | bfd_vma addr; | |
1584 | const bfd_byte *bytes; | |
1585 | int count; | |
1586 | int noadj; | |
1587 | { | |
1588 | bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; | |
1589 | bfd_vma endaddr = sec->_cooked_size; | |
1590 | ||
1591 | /* Make room to insert the bytes. */ | |
1592 | memmove (contents + addr + count, contents + addr, endaddr - addr); | |
1593 | ||
1594 | /* Insert the bytes into the section. */ | |
1595 | memcpy (contents + addr, bytes, count); | |
1596 | ||
1597 | sec->_cooked_size += count; | |
1598 | ||
1599 | adjust_all_relocations (abfd, sec, addr, endaddr, count, noadj); | |
1600 | return true; | |
1601 | } | |
1602 | ||
1603 | /* Delete some bytes from a section while relaxing. */ | |
1604 | ||
1605 | static boolean | |
1606 | ip2k_elf_relax_delete_bytes (abfd, sec, addr, count) | |
1607 | bfd *abfd; | |
1608 | asection *sec; | |
1609 | bfd_vma addr; | |
1610 | int count; | |
1611 | { | |
1612 | bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; | |
1613 | bfd_vma endaddr = sec->_cooked_size; | |
1614 | ||
1615 | /* Actually delete the bytes. */ | |
1616 | memmove (contents + addr, contents + addr + count, | |
1617 | endaddr - addr - count); | |
1618 | ||
1619 | sec->_cooked_size -= count; | |
1620 | ||
1621 | adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0); | |
1622 | return true; | |
1623 | } | |
1624 | ||
1625 | /* -------------------------------------------------------------------- */ | |
1626 | ||
1627 | /* XXX: The following code is the result of a cut&paste. This unfortunate | |
1628 | practice is very widespread in the various target back-end files. */ | |
1629 | ||
1630 | /* Set the howto pointer for a IP2K ELF reloc. */ | |
1631 | ||
1632 | static void | |
1633 | ip2k_info_to_howto_rela (abfd, cache_ptr, dst) | |
1634 | bfd * abfd ATTRIBUTE_UNUSED; | |
1635 | arelent * cache_ptr; | |
1636 | Elf32_Internal_Rela * dst; | |
1637 | { | |
1638 | unsigned int r_type; | |
1639 | ||
1640 | r_type = ELF32_R_TYPE (dst->r_info); | |
1641 | switch (r_type) | |
1642 | { | |
1643 | default: | |
1644 | cache_ptr->howto = & ip2k_elf_howto_table [r_type]; | |
1645 | break; | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | /* Perform a single relocation. | |
1650 | By default we use the standard BFD routines. */ | |
1651 | ||
1652 | static bfd_reloc_status_type | |
1653 | ip2k_final_link_relocate (howto, input_bfd, input_section, contents, rel, | |
1654 | relocation) | |
1655 | reloc_howto_type * howto; | |
1656 | bfd * input_bfd; | |
1657 | asection * input_section; | |
1658 | bfd_byte * contents; | |
1659 | Elf_Internal_Rela * rel; | |
1660 | bfd_vma relocation; | |
1661 | { | |
1662 | bfd_reloc_status_type r = bfd_reloc_ok; | |
1663 | ||
1664 | switch (howto->type) | |
1665 | { | |
1666 | /* Handle data space relocations. */ | |
1667 | case R_IP2K_FR9: | |
1668 | case R_IP2K_BANK: | |
1669 | if ((relocation & IP2K_DATA_MASK) == IP2K_DATA_VALUE) | |
1670 | relocation &= ~IP2K_DATA_MASK; | |
1671 | else | |
1672 | r = bfd_reloc_notsupported; | |
1673 | break; | |
1674 | ||
1675 | case R_IP2K_LO8DATA: | |
1676 | case R_IP2K_HI8DATA: | |
1677 | case R_IP2K_EX8DATA: | |
1678 | break; | |
1679 | ||
1680 | /* Handle insn space relocations. */ | |
1681 | case R_IP2K_ADDR16CJP: | |
1682 | case R_IP2K_PAGE3: | |
1683 | case R_IP2K_LO8INSN: | |
1684 | case R_IP2K_HI8INSN: | |
1685 | case R_IP2K_PC_SKIP: | |
1686 | if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE) | |
1687 | relocation &= ~IP2K_INSN_MASK; | |
1688 | else | |
1689 | r = bfd_reloc_notsupported; | |
1690 | break; | |
1691 | ||
1692 | case R_IP2K_16: | |
1693 | /* If this is a relocation involving a TEXT | |
1694 | symbol, reduce it to a word address. */ | |
1695 | if ((relocation & IP2K_INSN_MASK) == IP2K_INSN_VALUE) | |
1696 | howto = &ip2k_elf_howto_table[ (int) R_IP2K_TEXT]; | |
1697 | break; | |
1698 | ||
1699 | /* Pass others through. */ | |
1700 | default: | |
1701 | break; | |
1702 | } | |
1703 | ||
1704 | /* Only install relocation if above tests did not disqualify it. */ | |
1705 | if (r == bfd_reloc_ok) | |
1706 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
1707 | contents, rel->r_offset, | |
1708 | relocation, rel->r_addend); | |
1709 | ||
1710 | return r; | |
1711 | } | |
1712 | ||
1713 | /* Relocate a IP2K ELF section. | |
cf88bb9f NC |
1714 | |
1715 | The RELOCATE_SECTION function is called by the new ELF backend linker | |
1716 | to handle the relocations for a section. | |
1717 | ||
1718 | The relocs are always passed as Rela structures; if the section | |
1719 | actually uses Rel structures, the r_addend field will always be | |
1720 | zero. | |
1721 | ||
1722 | This function is responsible for adjusting the section contents as | |
1723 | necessary, and (if using Rela relocs and generating a relocateable | |
1724 | output file) adjusting the reloc addend as necessary. | |
1725 | ||
1726 | This function does not have to worry about setting the reloc | |
1727 | address or the reloc symbol index. | |
1728 | ||
1729 | LOCAL_SYMS is a pointer to the swapped in local symbols. | |
1730 | ||
1731 | LOCAL_SECTIONS is an array giving the section in the input file | |
1732 | corresponding to the st_shndx field of each local symbol. | |
1733 | ||
1734 | The global hash table entry for the global symbols can be found | |
1735 | via elf_sym_hashes (input_bfd). | |
1736 | ||
1737 | When generating relocateable output, this function must handle | |
1738 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is | |
1739 | going to be the section symbol corresponding to the output | |
1740 | section, which means that the addend must be adjusted | |
1741 | accordingly. */ | |
1742 | ||
1743 | static boolean | |
1744 | ip2k_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
1745 | contents, relocs, local_syms, local_sections) | |
1746 | bfd * output_bfd ATTRIBUTE_UNUSED; | |
1747 | struct bfd_link_info * info; | |
1748 | bfd * input_bfd; | |
1749 | asection * input_section; | |
1750 | bfd_byte * contents; | |
1751 | Elf_Internal_Rela * relocs; | |
1752 | Elf_Internal_Sym * local_syms; | |
1753 | asection ** local_sections; | |
1754 | { | |
1755 | Elf_Internal_Shdr * symtab_hdr; | |
1756 | struct elf_link_hash_entry ** sym_hashes; | |
1757 | Elf_Internal_Rela * rel; | |
1758 | Elf_Internal_Rela * relend; | |
1759 | ||
f0fe0e16 AM |
1760 | if (info->relocateable) |
1761 | return true; | |
1762 | ||
cf88bb9f NC |
1763 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
1764 | sym_hashes = elf_sym_hashes (input_bfd); | |
1765 | relend = relocs + input_section->reloc_count; | |
1766 | ||
1767 | for (rel = relocs; rel < relend; rel ++) | |
1768 | { | |
1769 | reloc_howto_type * howto; | |
1770 | unsigned long r_symndx; | |
1771 | Elf_Internal_Sym * sym; | |
1772 | asection * sec; | |
1773 | struct elf_link_hash_entry * h; | |
1774 | bfd_vma relocation; | |
1775 | bfd_reloc_status_type r; | |
1776 | const char * name = NULL; | |
1777 | int r_type; | |
1778 | ||
f0fe0e16 | 1779 | /* This is a final link. */ |
cf88bb9f | 1780 | r_type = ELF32_R_TYPE (rel->r_info); |
cf88bb9f | 1781 | r_symndx = ELF32_R_SYM (rel->r_info); |
cf88bb9f NC |
1782 | howto = ip2k_elf_howto_table + ELF32_R_TYPE (rel->r_info); |
1783 | h = NULL; | |
1784 | sym = NULL; | |
1785 | sec = NULL; | |
1786 | ||
1787 | if (r_symndx < symtab_hdr->sh_info) | |
1788 | { | |
1789 | sym = local_syms + r_symndx; | |
1790 | sec = local_sections [r_symndx]; | |
1791 | relocation = BASEADDR (sec) + sym->st_value; | |
1792 | ||
1793 | name = bfd_elf_string_from_elf_section | |
1794 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
1795 | name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name; | |
1796 | } | |
1797 | else | |
1798 | { | |
1799 | h = sym_hashes [r_symndx - symtab_hdr->sh_info]; | |
1800 | ||
1801 | while (h->root.type == bfd_link_hash_indirect | |
1802 | || h->root.type == bfd_link_hash_warning) | |
1803 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1804 | ||
1805 | name = h->root.root.string; | |
1806 | ||
1807 | if (h->root.type == bfd_link_hash_defined | |
1808 | || h->root.type == bfd_link_hash_defweak) | |
1809 | { | |
1810 | sec = h->root.u.def.section; | |
1811 | relocation = h->root.u.def.value + BASEADDR (sec); | |
1812 | } | |
1813 | else if (h->root.type == bfd_link_hash_undefweak) | |
1814 | { | |
1815 | relocation = 0; | |
1816 | } | |
1817 | else | |
1818 | { | |
1819 | if (! ((*info->callbacks->undefined_symbol) | |
1820 | (info, h->root.root.string, input_bfd, | |
1821 | input_section, rel->r_offset, | |
1822 | (! info->shared || info->no_undefined)))) | |
1823 | return false; | |
1824 | relocation = 0; | |
1825 | } | |
1826 | } | |
1827 | ||
1828 | /* Finally, the sole IP2K-specific part. */ | |
1829 | r = ip2k_final_link_relocate (howto, input_bfd, input_section, | |
1830 | contents, rel, relocation); | |
1831 | ||
1832 | if (r != bfd_reloc_ok) | |
1833 | { | |
1834 | const char * msg = (const char *) NULL; | |
1835 | ||
1836 | switch (r) | |
1837 | { | |
1838 | case bfd_reloc_overflow: | |
1839 | r = info->callbacks->reloc_overflow | |
1840 | (info, name, howto->name, (bfd_vma) 0, | |
1841 | input_bfd, input_section, rel->r_offset); | |
1842 | break; | |
1843 | ||
1844 | case bfd_reloc_undefined: | |
1845 | r = info->callbacks->undefined_symbol | |
1846 | (info, name, input_bfd, input_section, rel->r_offset, true); | |
1847 | break; | |
1848 | ||
1849 | case bfd_reloc_outofrange: | |
1850 | msg = _("internal error: out of range error"); | |
1851 | break; | |
1852 | ||
1853 | /* This is how ip2k_final_link_relocate tells us of a non-kosher | |
1854 | reference between insn & data address spaces. */ | |
1855 | case bfd_reloc_notsupported: | |
1856 | if (sym != NULL) /* Only if it's not an unresolved symbol. */ | |
1857 | msg = _("unsupported relocation between data/insn address spaces"); | |
1858 | break; | |
1859 | ||
1860 | case bfd_reloc_dangerous: | |
1861 | msg = _("internal error: dangerous relocation"); | |
1862 | break; | |
1863 | ||
1864 | default: | |
1865 | msg = _("internal error: unknown error"); | |
1866 | break; | |
1867 | } | |
1868 | ||
1869 | if (msg) | |
1870 | r = info->callbacks->warning | |
1871 | (info, msg, name, input_bfd, input_section, rel->r_offset); | |
1872 | ||
1873 | if (! r) | |
1874 | return false; | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | return true; | |
1879 | } | |
1880 | ||
1881 | static asection * | |
1882 | ip2k_elf_gc_mark_hook (sec, info, rel, h, sym) | |
1883 | asection *sec; | |
1884 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
1885 | Elf_Internal_Rela *rel; | |
1886 | struct elf_link_hash_entry *h; | |
1887 | Elf_Internal_Sym *sym; | |
1888 | { | |
1889 | if (h != NULL) | |
1890 | { | |
1891 | switch (ELF32_R_TYPE (rel->r_info)) | |
1892 | { | |
1893 | #if 0 | |
1894 | case R_IP2K_GNU_VTINHERIT: | |
1895 | case R_IP2K_GNU_VTENTRY: | |
1896 | break; | |
1897 | #endif | |
1898 | ||
1899 | default: | |
1900 | switch (h->root.type) | |
1901 | { | |
1902 | case bfd_link_hash_defined: | |
1903 | case bfd_link_hash_defweak: | |
1904 | return h->root.u.def.section; | |
1905 | ||
1906 | case bfd_link_hash_common: | |
1907 | return h->root.u.c.p->section; | |
1908 | ||
1909 | default: | |
1910 | break; | |
1911 | } | |
1912 | } | |
1913 | } | |
1914 | else | |
1915 | { | |
1916 | if (!(elf_bad_symtab (sec->owner) | |
1917 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
1918 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
1919 | && sym->st_shndx != SHN_COMMON)) | |
1920 | { | |
1921 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
1922 | } | |
1923 | } | |
1924 | return NULL; | |
1925 | } | |
1926 | ||
1927 | static boolean | |
1928 | ip2k_elf_gc_sweep_hook (abfd, info, sec, relocs) | |
1929 | bfd *abfd ATTRIBUTE_UNUSED; | |
1930 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
1931 | asection *sec ATTRIBUTE_UNUSED; | |
1932 | const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED; | |
1933 | { | |
1934 | /* we don't use got and plt entries for ip2k */ | |
1935 | return true; | |
1936 | } | |
1937 | ||
1938 | ||
1939 | /* -------------------------------------------------------------------- */ | |
1940 | ||
1941 | ||
1942 | #define TARGET_BIG_SYM bfd_elf32_ip2k_vec | |
1943 | #define TARGET_BIG_NAME "elf32-ip2k" | |
1944 | ||
1945 | #define ELF_ARCH bfd_arch_ip2k | |
1946 | #define ELF_MACHINE_CODE EM_IP2K | |
1947 | #define ELF_MAXPAGESIZE 1 /* No pages on the IP2K */ | |
1948 | ||
cf88bb9f NC |
1949 | #define elf_info_to_howto_rel NULL |
1950 | #define elf_info_to_howto ip2k_info_to_howto_rela | |
1951 | ||
1952 | #define elf_backend_can_gc_sections 1 | |
f0fe0e16 | 1953 | #define elf_backend_rela_normal 1 |
cf88bb9f NC |
1954 | #define elf_backend_gc_mark_hook ip2k_elf_gc_mark_hook |
1955 | #define elf_backend_gc_sweep_hook ip2k_elf_gc_sweep_hook | |
1956 | ||
1957 | #define elf_backend_relocate_section ip2k_elf_relocate_section | |
1958 | ||
1959 | #define elf_symbol_leading_char '_' | |
1960 | #define bfd_elf32_bfd_reloc_type_lookup ip2k_reloc_type_lookup | |
1961 | #define bfd_elf32_bfd_relax_section ip2k_elf_relax_section | |
1962 | ||
1963 | ||
1964 | #include "elf32-target.h" | |
1965 |