Commit | Line | Data |
---|---|---|
efaa65c9 | 1 | /* 32-bit ELF support for ARM |
95c5070c | 2 | Copyright 1998, 1999 Free Software Foundation, Inc. |
efaa65c9 | 3 | |
2c3c46ad | 4 | This file is part of BFD, the Binary File Descriptor library. |
efaa65c9 | 5 | |
2c3c46ad CM |
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. | |
efaa65c9 | 10 | |
2c3c46ad CM |
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. | |
efaa65c9 | 15 | |
2c3c46ad CM |
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. */ | |
efaa65c9 | 19 | |
efaa65c9 | 20 | |
2c3c46ad CM |
21 | typedef unsigned long int insn32; |
22 | typedef unsigned short int insn16; | |
23 | ||
efaa65c9 | 24 | static reloc_howto_type *elf32_arm_reloc_type_lookup |
2c3c46ad | 25 | PARAMS ((bfd * abfd, bfd_reloc_code_real_type code)); |
efaa65c9 CM |
26 | static void elf32_arm_info_to_howto |
27 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
0e7361bc NC |
28 | static boolean elf32_arm_set_private_flags |
29 | PARAMS ((bfd *, flagword)); | |
30 | static boolean elf32_arm_copy_private_bfd_data | |
31 | PARAMS ((bfd *, bfd *)); | |
32 | static boolean elf32_arm_merge_private_bfd_data | |
33 | PARAMS ((bfd *, bfd *)); | |
34 | static boolean elf32_arm_print_private_bfd_data | |
35 | PARAMS ((bfd *, PTR)); | |
bb3b4377 | 36 | static int elf32_arm_get_symbol_type |
948beb4a | 37 | PARAMS (( Elf_Internal_Sym *, int)); |
2c3c46ad CM |
38 | static struct bfd_link_hash_table *elf32_arm_link_hash_table_create |
39 | PARAMS ((bfd *)); | |
40 | ||
41 | ||
42 | static insn32 insert_thumb_branch | |
43 | PARAMS ((insn32, int)); | |
44 | static struct elf_link_hash_entry *find_thumb_glue | |
45 | PARAMS ((struct bfd_link_info *, CONST char *, bfd *)); | |
46 | static struct elf_link_hash_entry *find_arm_glue | |
47 | PARAMS ((struct bfd_link_info *, CONST char *, bfd *)); | |
48 | static void record_arm_to_thumb_glue | |
49 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
50 | static void record_thumb_to_arm_glue | |
51 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
52 | ||
53 | /* The linker script knows the section names for placement. | |
54 | The entry_names are used to do simple name mangling on the stubs. | |
55 | Given a function name, and its type, the stub can be found. The | |
56 | name can be changed. The only requirement is the %s be present. | |
57 | */ | |
58 | ||
59 | #define INTERWORK_FLAG( abfd ) (elf_elfheader (abfd)->e_flags & EF_INTERWORK) | |
60 | ||
61 | #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t" | |
62 | #define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb" | |
63 | ||
64 | #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7" | |
65 | #define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm" | |
66 | ||
67 | /* Get the ARM elf linker hash table from a link_info structure. */ | |
68 | #define elf32_arm_hash_table(info) \ | |
69 | ((struct elf32_arm_link_hash_table *) ((info)->hash)) | |
70 | ||
71 | /* ARM ELF linker hash table */ | |
72 | struct elf32_arm_link_hash_table | |
73 | { | |
74 | /* The main hash table. */ | |
75 | struct elf_link_hash_table root; | |
76 | ||
77 | /* The size in bytes of the section containg the Thumb-to-ARM glue. */ | |
78 | long int thumb_glue_size; | |
79 | ||
80 | /* The size in bytes of the section containg the ARM-to-Thumb glue. */ | |
81 | long int arm_glue_size; | |
82 | ||
83 | /* An arbitary input BFD chosen to hold the glue sections. */ | |
84 | bfd *bfd_of_glue_owner; | |
85 | ||
86 | }; | |
87 | ||
88 | ||
89 | ||
90 | /* Create an ARM elf linker hash table */ | |
91 | ||
92 | static struct bfd_link_hash_table * | |
93 | elf32_arm_link_hash_table_create (abfd) | |
94 | bfd *abfd; | |
95 | { | |
96 | struct elf32_arm_link_hash_table *ret; | |
97 | ||
98 | ret = ((struct elf32_arm_link_hash_table *) | |
99 | bfd_alloc (abfd, sizeof (struct elf32_arm_link_hash_table))); | |
100 | if (ret == (struct elf32_arm_link_hash_table *) NULL) | |
101 | return NULL; | |
102 | ||
103 | if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, | |
104 | _bfd_elf_link_hash_newfunc)) | |
105 | { | |
106 | bfd_release (abfd, ret); | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | ret->thumb_glue_size = 0; | |
111 | ret->arm_glue_size = 0; | |
112 | ret->bfd_of_glue_owner = NULL; | |
113 | ||
114 | return &ret->root.root; | |
115 | } | |
116 | ||
117 | static struct elf_link_hash_entry * | |
118 | find_thumb_glue (link_info, name, input_bfd) | |
119 | struct bfd_link_info *link_info; | |
120 | CONST char *name; | |
121 | bfd *input_bfd; | |
122 | { | |
123 | char *tmp_name; | |
124 | struct elf_link_hash_entry *hash; | |
125 | struct elf32_arm_link_hash_table *hash_table; | |
126 | ||
127 | /* We need a pointer to the armelf specific hash table. */ | |
128 | hash_table = elf32_arm_hash_table (link_info); | |
129 | ||
130 | ||
131 | tmp_name = ((char *) | |
132 | bfd_malloc (strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1)); | |
133 | ||
134 | BFD_ASSERT (tmp_name); | |
135 | ||
136 | sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name); | |
137 | ||
138 | hash = elf_link_hash_lookup | |
139 | (&(hash_table)->root, tmp_name, false, false, true); | |
140 | ||
141 | if (hash == NULL) | |
142 | /* xgettext:c-format */ | |
143 | _bfd_error_handler (_ ("%s: unable to find THUMB glue '%s' for `%s'"), | |
144 | bfd_get_filename (input_bfd), tmp_name, name); | |
145 | ||
146 | free (tmp_name); | |
147 | ||
148 | return hash; | |
149 | } | |
150 | ||
151 | static struct elf_link_hash_entry * | |
152 | find_arm_glue (link_info, name, input_bfd) | |
153 | struct bfd_link_info *link_info; | |
154 | CONST char *name; | |
155 | bfd *input_bfd; | |
156 | { | |
157 | char *tmp_name; | |
158 | struct elf_link_hash_entry *myh; | |
159 | struct elf32_arm_link_hash_table *hash_table; | |
160 | ||
161 | /* We need a pointer to the elfarm specific hash table. */ | |
162 | hash_table = elf32_arm_hash_table (link_info); | |
163 | ||
164 | tmp_name = ((char *) | |
165 | bfd_malloc (strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1)); | |
166 | ||
167 | BFD_ASSERT (tmp_name); | |
168 | ||
169 | sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name); | |
170 | ||
171 | myh = elf_link_hash_lookup | |
172 | (&(hash_table)->root, tmp_name, false, false, true); | |
173 | ||
174 | if (myh == NULL) | |
175 | /* xgettext:c-format */ | |
176 | _bfd_error_handler (_ ("%s: unable to find ARM glue '%s' for `%s'"), | |
177 | bfd_get_filename (input_bfd), tmp_name, name); | |
178 | ||
179 | free (tmp_name); | |
180 | ||
181 | return myh; | |
182 | } | |
183 | ||
184 | /* | |
185 | ARM->Thumb glue: | |
186 | ||
187 | .arm | |
188 | __func_from_arm: | |
189 | ldr r12, __func_addr | |
190 | bx r12 | |
191 | __func_addr: | |
192 | .word func @ behave as if you saw a ARM_32 reloc | |
193 | */ | |
194 | ||
195 | #define ARM2THUMB_GLUE_SIZE 12 | |
196 | static const insn32 a2t1_ldr_insn = 0xe59fc000; | |
197 | static const insn32 a2t2_bx_r12_insn = 0xe12fff1c; | |
198 | static const insn32 a2t3_func_addr_insn = 0x00000001; | |
199 | ||
200 | /* | |
201 | Thumb->ARM: Thumb->(non-interworking aware) ARM | |
202 | ||
203 | .thumb .thumb | |
204 | .align 2 .align 2 | |
205 | __func_from_thumb: __func_from_thumb: | |
206 | bx pc push {r6, lr} | |
207 | nop ldr r6, __func_addr | |
208 | .arm mov lr, pc | |
209 | __func_change_to_arm: bx r6 | |
210 | b func .arm | |
211 | __func_back_to_thumb: | |
212 | ldmia r13! {r6, lr} | |
213 | bx lr | |
214 | __func_addr: | |
215 | .word func | |
216 | */ | |
217 | ||
218 | #define THUMB2ARM_GLUE_SIZE 8 | |
219 | static const insn16 t2a1_bx_pc_insn = 0x4778; | |
220 | static const insn16 t2a2_noop_insn = 0x46c0; | |
221 | static const insn32 t2a3_b_insn = 0xea000000; | |
222 | ||
223 | static const insn16 t2a1_push_insn = 0xb540; | |
224 | static const insn16 t2a2_ldr_insn = 0x4e03; | |
225 | static const insn16 t2a3_mov_insn = 0x46fe; | |
226 | static const insn16 t2a4_bx_insn = 0x4730; | |
227 | static const insn32 t2a5_pop_insn = 0xe8bd4040; | |
228 | static const insn32 t2a6_bx_insn = 0xe12fff1e; | |
229 | ||
230 | boolean | |
231 | bfd_elf32_arm_allocate_interworking_sections (info) | |
232 | struct bfd_link_info *info; | |
233 | { | |
234 | asection *s; | |
235 | bfd_byte *foo; | |
236 | struct elf32_arm_link_hash_table *globals; | |
237 | ||
238 | globals = elf32_arm_hash_table (info); | |
239 | ||
240 | BFD_ASSERT (globals != NULL); | |
241 | ||
242 | if (globals->arm_glue_size != 0) | |
243 | { | |
244 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
245 | ||
246 | s = bfd_get_section_by_name | |
247 | (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME); | |
248 | ||
249 | BFD_ASSERT (s != NULL); | |
250 | ||
251 | foo = (bfd_byte *) bfd_alloc | |
252 | (globals->bfd_of_glue_owner, globals->arm_glue_size); | |
253 | ||
254 | s->_raw_size = s->_cooked_size = globals->arm_glue_size; | |
255 | s->contents = foo; | |
256 | } | |
257 | ||
258 | if (globals->thumb_glue_size != 0) | |
259 | { | |
260 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
261 | ||
262 | s = bfd_get_section_by_name | |
263 | (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME); | |
264 | ||
265 | BFD_ASSERT (s != NULL); | |
266 | ||
267 | foo = (bfd_byte *) bfd_alloc | |
268 | (globals->bfd_of_glue_owner, globals->thumb_glue_size); | |
269 | ||
270 | s->_raw_size = s->_cooked_size = globals->thumb_glue_size; | |
271 | s->contents = foo; | |
272 | } | |
273 | ||
274 | return true; | |
275 | } | |
276 | ||
277 | static void | |
278 | record_arm_to_thumb_glue (link_info, h) | |
279 | struct bfd_link_info *link_info; | |
280 | struct elf_link_hash_entry *h; | |
281 | { | |
282 | const char *name = h->root.root.string; | |
283 | register asection *s; | |
284 | char *tmp_name; | |
285 | struct elf_link_hash_entry *myh; | |
286 | struct elf32_arm_link_hash_table *globals; | |
287 | ||
288 | globals = elf32_arm_hash_table (link_info); | |
289 | ||
290 | BFD_ASSERT (globals != NULL); | |
291 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
292 | ||
293 | s = bfd_get_section_by_name | |
294 | (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME); | |
295 | ||
296 | ||
297 | BFD_ASSERT (s != NULL); | |
298 | ||
299 | tmp_name = ((char *) | |
300 | bfd_malloc (strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1)); | |
301 | ||
302 | BFD_ASSERT (tmp_name); | |
303 | ||
304 | sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name); | |
305 | ||
306 | myh = elf_link_hash_lookup | |
307 | (&(globals)->root, tmp_name, false, false, true); | |
308 | ||
309 | if (myh != NULL) | |
310 | { | |
311 | free (tmp_name); | |
312 | return; /* we've already seen this guy */ | |
313 | } | |
314 | ||
315 | /* The only trick here is using hash_table->arm_glue_size as the value. Even | |
316 | though the section isn't allocated yet, this is where we will be putting | |
317 | it. */ | |
318 | ||
319 | _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner, tmp_name, | |
320 | BSF_GLOBAL, | |
321 | s, globals->arm_glue_size + 1, | |
322 | NULL, true, false, | |
323 | (struct bfd_link_hash_entry **) &myh); | |
324 | ||
325 | free (tmp_name); | |
326 | ||
327 | globals->arm_glue_size += ARM2THUMB_GLUE_SIZE; | |
328 | ||
329 | return; | |
330 | } | |
331 | ||
332 | static void | |
333 | record_thumb_to_arm_glue (link_info, h) | |
334 | struct bfd_link_info *link_info; | |
335 | struct elf_link_hash_entry *h; | |
336 | { | |
337 | const char *name = h->root.root.string; | |
338 | register asection *s; | |
339 | char *tmp_name; | |
340 | struct elf_link_hash_entry *myh; | |
341 | struct elf32_arm_link_hash_table *hash_table; | |
bb3b4377 | 342 | char bind; |
2c3c46ad CM |
343 | |
344 | hash_table = elf32_arm_hash_table (link_info); | |
345 | ||
346 | BFD_ASSERT (hash_table != NULL); | |
347 | BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL); | |
348 | ||
349 | s = bfd_get_section_by_name | |
350 | (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME); | |
351 | ||
352 | BFD_ASSERT (s != NULL); | |
353 | ||
354 | tmp_name = (char *) bfd_malloc (strlen (name) + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1); | |
355 | ||
356 | BFD_ASSERT (tmp_name); | |
357 | ||
358 | sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name); | |
359 | ||
360 | myh = elf_link_hash_lookup | |
361 | (&(hash_table)->root, tmp_name, false, false, true); | |
362 | ||
363 | if (myh != NULL) | |
364 | { | |
365 | free (tmp_name); | |
366 | return; /* we've already seen this guy */ | |
367 | } | |
368 | ||
369 | _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner, tmp_name, | |
370 | BSF_GLOBAL, s, hash_table->thumb_glue_size + 1, | |
371 | NULL, true, false, | |
372 | (struct bfd_link_hash_entry **) &myh); | |
373 | ||
374 | /* If we mark it 'thumb', the disassembler will do a better job. */ | |
bb3b4377 CM |
375 | bind = ELF_ST_BIND (myh->type); |
376 | myh->type = ELF_ST_INFO (bind, STT_ARM_TFUNC); | |
2c3c46ad CM |
377 | |
378 | free (tmp_name); | |
379 | ||
380 | /* Allocate another symbol to mark where we switch to arm mode. */ | |
381 | ||
382 | #define CHANGE_TO_ARM "__%s_change_to_arm" | |
383 | #define BACK_FROM_ARM "__%s_back_from_arm" | |
384 | ||
385 | tmp_name = (char *) bfd_malloc (strlen (name) + strlen (CHANGE_TO_ARM) + 1); | |
386 | ||
387 | BFD_ASSERT (tmp_name); | |
388 | ||
389 | sprintf (tmp_name, CHANGE_TO_ARM, name); | |
390 | ||
391 | myh = NULL; | |
392 | ||
393 | _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner, tmp_name, | |
394 | BSF_LOCAL, s, hash_table->thumb_glue_size + 4, | |
395 | NULL, true, false, | |
396 | (struct bfd_link_hash_entry **) &myh); | |
397 | ||
398 | free (tmp_name); | |
399 | ||
400 | hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE; | |
401 | ||
402 | return; | |
403 | } | |
404 | ||
405 | /* Select a BFD to be used to hold the sections used by the glue code. | |
406 | This function is called from the linker scripts in ld/emultempl/ | |
407 | {armelf/pe}.em */ | |
408 | boolean | |
409 | bfd_elf32_arm_get_bfd_for_interworking (abfd, info) | |
410 | bfd *abfd; | |
411 | struct bfd_link_info *info; | |
412 | { | |
413 | struct elf32_arm_link_hash_table *globals; | |
414 | flagword flags; | |
415 | asection *sec; | |
416 | ||
417 | /* If we are only performing a partial link do not bother | |
418 | getting a bfd to hold the glue. */ | |
419 | if (info->relocateable) | |
420 | return true; | |
421 | ||
422 | globals = elf32_arm_hash_table (info); | |
423 | ||
424 | BFD_ASSERT (globals != NULL); | |
425 | ||
426 | if (globals->bfd_of_glue_owner != NULL) | |
427 | return true; | |
428 | ||
429 | sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME); | |
430 | ||
431 | if (sec == NULL) | |
432 | { | |
433 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
434 | ||
435 | sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME); | |
436 | ||
437 | if (sec == NULL | |
438 | || !bfd_set_section_flags (abfd, sec, flags) | |
439 | || !bfd_set_section_alignment (abfd, sec, 2)) | |
440 | return false; | |
441 | } | |
442 | ||
443 | sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME); | |
444 | ||
445 | if (sec == NULL) | |
446 | { | |
447 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
448 | ||
449 | sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME); | |
450 | ||
451 | if (sec == NULL | |
452 | || !bfd_set_section_flags (abfd, sec, flags) | |
453 | || !bfd_set_section_alignment (abfd, sec, 2)) | |
454 | return false; | |
455 | } | |
456 | ||
457 | /* Save the bfd for later use. */ | |
458 | globals->bfd_of_glue_owner = abfd; | |
459 | ||
460 | return true; | |
461 | } | |
462 | ||
463 | boolean | |
464 | bfd_elf32_arm_process_before_allocation (abfd, link_info) | |
465 | bfd *abfd; | |
466 | struct bfd_link_info *link_info; | |
467 | { | |
468 | Elf_Internal_Shdr *symtab_hdr; | |
469 | Elf_Internal_Rela *free_relocs = NULL; | |
470 | Elf_Internal_Rela *irel, *irelend; | |
471 | bfd_byte *contents = NULL; | |
472 | bfd_byte *free_contents = NULL; | |
473 | Elf32_External_Sym *extsyms = NULL; | |
474 | Elf32_External_Sym *free_extsyms = NULL; | |
475 | ||
476 | asection *sec; | |
477 | struct elf32_arm_link_hash_table *globals; | |
478 | ||
479 | /* If we are only performing a partial link do not bother | |
480 | to construct any glue. */ | |
481 | if (link_info->relocateable) | |
482 | return true; | |
483 | ||
484 | /* Here we have a bfd that is to be included on the link. We have a hook | |
485 | to do reloc rummaging, before section sizes are nailed down. */ | |
486 | ||
2c3c46ad CM |
487 | globals = elf32_arm_hash_table (link_info); |
488 | ||
489 | BFD_ASSERT (globals != NULL); | |
490 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
491 | ||
492 | /* Rummage around all the relocs and map the glue vectors. */ | |
493 | sec = abfd->sections; | |
494 | ||
495 | if (sec == NULL) | |
496 | return true; | |
497 | ||
498 | for (; sec != NULL; sec = sec->next) | |
499 | { | |
2c3c46ad CM |
500 | if (sec->reloc_count == 0) |
501 | continue; | |
502 | ||
503 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
504 | /* Load the relocs. */ | |
505 | ||
506 | irel = (_bfd_elf32_link_read_relocs (abfd, sec, (PTR) NULL, | |
507 | (Elf_Internal_Rela *) NULL, false)); | |
508 | ||
bb3b4377 | 509 | BFD_ASSERT (irel != 0); |
2c3c46ad CM |
510 | |
511 | irelend = irel + sec->reloc_count; | |
512 | for (; irel < irelend; irel++) | |
513 | { | |
514 | long r_type; | |
515 | unsigned long r_index; | |
516 | unsigned char code; | |
517 | ||
518 | struct elf_link_hash_entry *h; | |
519 | ||
520 | r_type = ELF32_R_TYPE (irel->r_info); | |
521 | r_index = ELF32_R_SYM (irel->r_info); | |
522 | ||
523 | /* These are the only relocation types we care about */ | |
524 | if (r_type != R_ARM_PC24 | |
525 | && r_type != R_ARM_THM_PC22) | |
526 | continue; | |
527 | ||
528 | /* Get the section contents if we haven't done so already. */ | |
529 | if (contents == NULL) | |
530 | { | |
531 | /* Get cached copy if it exists. */ | |
532 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
533 | contents = elf_section_data (sec)->this_hdr.contents; | |
534 | else | |
535 | { | |
536 | /* Go get them off disk. */ | |
537 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
538 | if (contents == NULL) | |
539 | goto error_return; | |
540 | free_contents = contents; | |
541 | ||
542 | if (!bfd_get_section_contents (abfd, sec, contents, | |
543 | (file_ptr) 0, sec->_raw_size)) | |
544 | goto error_return; | |
545 | } | |
546 | } | |
547 | ||
548 | /* Read this BFD's symbols if we haven't done so already. */ | |
549 | if (extsyms == NULL) | |
550 | { | |
551 | /* Get cached copy if it exists. */ | |
552 | if (symtab_hdr->contents != NULL) | |
553 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
554 | else | |
555 | { | |
556 | /* Go get them off disk. */ | |
557 | extsyms = ((Elf32_External_Sym *) | |
558 | bfd_malloc (symtab_hdr->sh_size)); | |
559 | if (extsyms == NULL) | |
560 | goto error_return; | |
561 | free_extsyms = extsyms; | |
562 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
563 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd) | |
564 | != symtab_hdr->sh_size)) | |
565 | goto error_return; | |
566 | } | |
567 | } | |
568 | ||
569 | /* If the relocation is not against a symbol it cannot concern us. */ | |
570 | ||
571 | h = NULL; | |
572 | ||
573 | /* We don't care about local symbols */ | |
574 | if (r_index < symtab_hdr->sh_info) | |
575 | continue; | |
576 | ||
577 | /* This is an external symbol */ | |
578 | r_index -= symtab_hdr->sh_info; | |
579 | h = (struct elf_link_hash_entry *) | |
580 | elf_sym_hashes (abfd)[r_index]; | |
581 | ||
582 | /* If the relocation is against a static symbol it must be within | |
583 | the current section and so cannot be a cross ARM/Thumb relocation. */ | |
584 | if (h == NULL) | |
585 | continue; | |
586 | ||
587 | switch (r_type) | |
588 | { | |
589 | case R_ARM_PC24: | |
590 | /* This one is a call from arm code. We need to look up | |
591 | the target of the call. If it is a thumb target, we | |
592 | insert glue. */ | |
593 | ||
bb3b4377 | 594 | if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC) |
2c3c46ad CM |
595 | record_arm_to_thumb_glue (link_info, h); |
596 | break; | |
597 | ||
598 | case R_ARM_THM_PC22: | |
bb3b4377 CM |
599 | /* This one is a call from thumb code. We look |
600 | up the target of the call. If it is not a thumb | |
601 | target, we insert glue. */ | |
2c3c46ad | 602 | |
bb3b4377 CM |
603 | if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC) |
604 | record_thumb_to_arm_glue (link_info, h); | |
2c3c46ad CM |
605 | break; |
606 | ||
607 | default: | |
608 | break; | |
609 | } | |
610 | } | |
611 | } | |
612 | ||
613 | return true; | |
614 | error_return: | |
615 | if (free_relocs != NULL) | |
616 | free (free_relocs); | |
617 | if (free_contents != NULL) | |
618 | free (free_contents); | |
619 | if (free_extsyms != NULL) | |
620 | free (free_extsyms); | |
621 | return false; | |
622 | ||
623 | } | |
efaa65c9 | 624 | |
efaa65c9 | 625 | struct elf32_arm_reloc_map |
2c3c46ad CM |
626 | { |
627 | unsigned char bfd_reloc_val; | |
628 | unsigned char elf_reloc_val; | |
629 | }; | |
efaa65c9 CM |
630 | |
631 | static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] = | |
632 | { | |
2c3c46ad CM |
633 | {BFD_RELOC_NONE, R_ARM_NONE,}, |
634 | {BFD_RELOC_ARM_PCREL_BRANCH, R_ARM_PC24,}, | |
635 | {BFD_RELOC_32, R_ARM_ABS32,}, | |
636 | {BFD_RELOC_32_PCREL, R_ARM_REL32,}, | |
637 | {BFD_RELOC_8, R_ARM_ABS8,}, | |
638 | {BFD_RELOC_16, R_ARM_ABS16,}, | |
639 | {BFD_RELOC_ARM_OFFSET_IMM, R_ARM_ABS12,}, | |
640 | {BFD_RELOC_ARM_THUMB_OFFSET, R_ARM_THM_ABS5,}, | |
641 | {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_PC22,}, | |
948beb4a CM |
642 | {BFD_RELOC_VTABLE_INHERIT, R_ARM_GNU_VTINHERIT }, |
643 | {BFD_RELOC_VTABLE_ENTRY, R_ARM_GNU_VTENTRY }, | |
2c3c46ad CM |
644 | {BFD_RELOC_NONE, R_ARM_SBREL32,}, |
645 | {BFD_RELOC_NONE, R_ARM_AMP_VCALL9,}, | |
646 | {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_PC11,}, | |
647 | {BFD_RELOC_THUMB_PCREL_BRANCH9, R_ARM_THM_PC9,} | |
efaa65c9 CM |
648 | }; |
649 | ||
650 | static reloc_howto_type * | |
651 | elf32_arm_reloc_type_lookup (abfd, code) | |
652 | bfd *abfd; | |
653 | bfd_reloc_code_real_type code; | |
654 | { | |
655 | unsigned int i; | |
2c3c46ad | 656 | |
efaa65c9 | 657 | for (i = 0; |
2c3c46ad | 658 | i < sizeof (elf32_arm_reloc_map) / sizeof (struct elf32_arm_reloc_map); |
efaa65c9 CM |
659 | i++) |
660 | { | |
661 | if (elf32_arm_reloc_map[i].bfd_reloc_val == code) | |
2c3c46ad | 662 | return &elf32_arm_howto_table[elf32_arm_reloc_map[i].elf_reloc_val]; |
efaa65c9 | 663 | } |
2c3c46ad | 664 | |
efaa65c9 CM |
665 | return NULL; |
666 | } | |
667 | ||
2c3c46ad CM |
668 | /* The thumb form of a long branch is a bit finicky, because the offset |
669 | encoding is split over two fields, each in it's own instruction. They | |
670 | can occur in any order. So given a thumb form of long branch, and an | |
671 | offset, insert the offset into the thumb branch and return finished | |
672 | instruction. | |
673 | ||
674 | It takes two thumb instructions to encode the target address. Each has | |
675 | 11 bits to invest. The upper 11 bits are stored in one (identifed by | |
676 | H-0.. see below), the lower 11 bits are stored in the other (identified | |
677 | by H-1). | |
678 | ||
679 | Combine together and shifted left by 1 (it's a half word address) and | |
680 | there you have it. | |
681 | ||
682 | Op: 1111 = F, | |
683 | H-0, upper address-0 = 000 | |
684 | Op: 1111 = F, | |
685 | H-1, lower address-0 = 800 | |
686 | ||
687 | They can be ordered either way, but the arm tools I've seen always put | |
688 | the lower one first. It probably doesn't matter. krk@cygnus.com | |
689 | ||
690 | XXX: Actually the order does matter. The second instruction (H-1) | |
691 | moves the computed address into the PC, so it must be the second one | |
692 | in the sequence. The problem, however is that whilst little endian code | |
693 | stores the instructions in HI then LOW order, big endian code does the | |
694 | reverse. nickc@cygnus.com */ | |
695 | ||
696 | #define LOW_HI_ORDER 0xF800F000 | |
697 | #define HI_LOW_ORDER 0xF000F800 | |
698 | ||
699 | static insn32 | |
700 | insert_thumb_branch (br_insn, rel_off) | |
701 | insn32 br_insn; | |
702 | int rel_off; | |
703 | { | |
704 | unsigned int low_bits; | |
705 | unsigned int high_bits; | |
706 | ||
707 | ||
708 | BFD_ASSERT ((rel_off & 1) != 1); | |
709 | ||
710 | rel_off >>= 1; /* half word aligned address */ | |
711 | low_bits = rel_off & 0x000007FF; /* the bottom 11 bits */ | |
712 | high_bits = (rel_off >> 11) & 0x000007FF; /* the top 11 bits */ | |
713 | ||
714 | if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER) | |
715 | br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits; | |
716 | else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER) | |
717 | br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits; | |
718 | else | |
719 | abort (); /* error - not a valid branch instruction form */ | |
720 | ||
721 | /* FIXME: abort is probably not the right call. krk@cygnus.com */ | |
722 | ||
723 | return br_insn; | |
724 | } | |
774852e9 NC |
725 | |
726 | /* Thumb code calling an ARM function */ | |
727 | int | |
2c3c46ad CM |
728 | elf32_thumb_to_arm_stub (info, name, input_bfd, output_bfd, input_section, |
729 | hit_data, sym_sec, offset, addend, val) | |
730 | struct bfd_link_info *info; | |
731 | char *name; | |
732 | bfd *input_bfd; | |
733 | bfd *output_bfd; | |
734 | asection *input_section; | |
735 | bfd_byte *hit_data; | |
736 | asection *sym_sec; | |
737 | int offset; | |
738 | int addend; | |
739 | bfd_vma val; | |
740 | { | |
2c3c46ad CM |
741 | asection *s = 0; |
742 | long int my_offset; | |
743 | unsigned long int tmp; | |
744 | long int ret_offset; | |
745 | struct elf_link_hash_entry *myh; | |
746 | struct elf32_arm_link_hash_table *globals; | |
747 | ||
748 | myh = find_thumb_glue (info, name, input_bfd); | |
749 | if (myh == NULL) | |
750 | return false; | |
751 | ||
752 | globals = elf32_arm_hash_table (info); | |
753 | ||
754 | BFD_ASSERT (globals != NULL); | |
755 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
756 | ||
757 | my_offset = myh->root.u.def.value; | |
758 | ||
759 | s = bfd_get_section_by_name (globals->bfd_of_glue_owner, | |
760 | THUMB2ARM_GLUE_SECTION_NAME); | |
761 | ||
762 | BFD_ASSERT (s != NULL); | |
763 | BFD_ASSERT (s->contents != NULL); | |
764 | BFD_ASSERT (s->output_section != NULL); | |
765 | ||
766 | if ((my_offset & 0x01) == 0x01) | |
767 | { | |
774852e9 NC |
768 | if (sym_sec != NULL |
769 | && sym_sec->owner != NULL | |
2c3c46ad CM |
770 | && !INTERWORK_FLAG (sym_sec->owner)) |
771 | { | |
772 | _bfd_error_handler | |
773 | (_ ("%s(%s): warning: interworking not enabled."), | |
774 | bfd_get_filename (sym_sec->owner), name); | |
775 | _bfd_error_handler | |
776 | (_ (" first occurrence: %s: thumb call to arm"), | |
777 | bfd_get_filename (input_bfd)); | |
a61c9386 NC |
778 | |
779 | return false; | |
2c3c46ad CM |
780 | } |
781 | ||
782 | --my_offset; | |
783 | myh->root.u.def.value = my_offset; | |
784 | ||
785 | bfd_put_16 (output_bfd, t2a1_bx_pc_insn, | |
786 | s->contents + my_offset); | |
787 | ||
788 | bfd_put_16 (output_bfd, t2a2_noop_insn, | |
789 | s->contents + my_offset + 2); | |
790 | ||
791 | ret_offset = | |
792 | ((bfd_signed_vma) val) /* Address of destination of the stub */ | |
793 | - ((bfd_signed_vma) | |
794 | (s->output_offset /* Offset from the start of the current section to the start of the stubs. */ | |
795 | + my_offset /* Offset of the start of this stub from the start of the stubs. */ | |
796 | + s->output_section->vma) /* Address of the start of the current section. */ | |
797 | + 4 /* The branch instruction is 4 bytes into the stub. */ | |
798 | + 8); /* ARM branches work from the pc of the instruction + 8. */ | |
799 | ||
800 | bfd_put_32 (output_bfd, | |
801 | t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF), | |
802 | s->contents + my_offset + 4); | |
803 | } | |
804 | ||
805 | BFD_ASSERT (my_offset <= globals->thumb_glue_size); | |
806 | ||
807 | /* Now go back and fix up the original BL insn to point | |
808 | to here. */ | |
809 | ret_offset = | |
810 | s->output_offset | |
811 | + my_offset | |
812 | - (input_section->output_offset | |
813 | + offset + addend) | |
814 | - 4; | |
815 | ||
816 | tmp = bfd_get_32 (input_bfd, hit_data | |
817 | - input_section->vma); | |
818 | ||
819 | bfd_put_32 (output_bfd, | |
820 | insert_thumb_branch (tmp, ret_offset), | |
821 | hit_data - input_section->vma); | |
a61c9386 NC |
822 | |
823 | return true; | |
2c3c46ad | 824 | } |
774852e9 NC |
825 | |
826 | /* Arm code calling a Thumb function */ | |
827 | int | |
2c3c46ad CM |
828 | elf32_arm_to_thumb_stub (info, name, input_bfd, output_bfd, input_section, |
829 | hit_data, sym_sec, offset, addend, val) | |
830 | ||
831 | struct bfd_link_info *info; | |
832 | char *name; | |
833 | bfd *input_bfd; | |
834 | bfd *output_bfd; | |
835 | asection *input_section; | |
836 | bfd_byte *hit_data; | |
837 | asection *sym_sec; | |
838 | int offset; | |
839 | int addend; | |
840 | bfd_vma val; | |
841 | { | |
2c3c46ad CM |
842 | unsigned long int tmp; |
843 | long int my_offset; | |
844 | asection *s; | |
845 | long int ret_offset; | |
846 | struct elf_link_hash_entry *myh; | |
847 | struct elf32_arm_link_hash_table *globals; | |
848 | ||
849 | myh = find_arm_glue (info, name, input_bfd); | |
850 | if (myh == NULL) | |
851 | return false; | |
852 | ||
853 | globals = elf32_arm_hash_table (info); | |
854 | ||
855 | BFD_ASSERT (globals != NULL); | |
856 | BFD_ASSERT (globals->bfd_of_glue_owner != NULL); | |
857 | ||
858 | my_offset = myh->root.u.def.value; | |
859 | s = bfd_get_section_by_name (globals->bfd_of_glue_owner, | |
860 | ARM2THUMB_GLUE_SECTION_NAME); | |
861 | BFD_ASSERT (s != NULL); | |
862 | BFD_ASSERT (s->contents != NULL); | |
863 | BFD_ASSERT (s->output_section != NULL); | |
864 | ||
865 | if ((my_offset & 0x01) == 0x01) | |
866 | { | |
774852e9 NC |
867 | if (sym_sec != NULL |
868 | && sym_sec->owner != NULL | |
2c3c46ad CM |
869 | && !INTERWORK_FLAG (sym_sec->owner)) |
870 | { | |
871 | _bfd_error_handler | |
872 | (_ ("%s(%s): warning: interworking not enabled."), | |
873 | bfd_get_filename (sym_sec->owner), name); | |
874 | _bfd_error_handler | |
875 | (_ (" first occurrence: %s: arm call to thumb"), | |
876 | bfd_get_filename (input_bfd)); | |
877 | } | |
878 | --my_offset; | |
879 | myh->root.u.def.value = my_offset; | |
880 | ||
881 | bfd_put_32 (output_bfd, a2t1_ldr_insn, | |
882 | s->contents + my_offset); | |
883 | ||
884 | bfd_put_32 (output_bfd, a2t2_bx_r12_insn, | |
885 | s->contents + my_offset + 4); | |
886 | ||
887 | /* It's a thumb address. Add the low order bit. */ | |
888 | bfd_put_32 (output_bfd, val | a2t3_func_addr_insn, | |
889 | s->contents + my_offset + 8); | |
890 | } | |
891 | ||
892 | BFD_ASSERT (my_offset <= globals->arm_glue_size); | |
893 | ||
894 | tmp = bfd_get_32 (input_bfd, hit_data); | |
895 | tmp = tmp & 0xFF000000; | |
896 | ||
897 | /* Somehow these are both 4 too far, so subtract 8. */ | |
898 | ret_offset = s->output_offset | |
899 | + my_offset | |
900 | + s->output_section->vma | |
901 | - (input_section->output_offset | |
902 | + input_section->output_section->vma | |
903 | + offset + addend) | |
904 | - 8; | |
905 | ||
906 | tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF); | |
907 | ||
908 | bfd_put_32 (output_bfd, tmp, hit_data | |
909 | - input_section->vma); | |
910 | ||
a61c9386 NC |
911 | |
912 | return true; | |
2c3c46ad | 913 | } |
774852e9 | 914 | |
efaa65c9 CM |
915 | /* Perform a relocation as part of a final link. */ |
916 | static bfd_reloc_status_type | |
917 | elf32_arm_final_link_relocate (howto, input_bfd, output_bfd, | |
2c3c46ad CM |
918 | input_section, contents, offset, value, |
919 | addend, info, sym_sec, sym_name, sym_flags) | |
efaa65c9 CM |
920 | reloc_howto_type *howto; |
921 | bfd *input_bfd; | |
922 | bfd *output_bfd; | |
923 | asection *input_section; | |
924 | bfd_byte *contents; | |
925 | bfd_vma offset; | |
926 | bfd_vma value; | |
927 | bfd_vma addend; | |
928 | struct bfd_link_info *info; | |
929 | asection *sym_sec; | |
774852e9 | 930 | const char *sym_name; |
a17e14c1 | 931 | unsigned char sym_flags; |
efaa65c9 CM |
932 | { |
933 | unsigned long r_type = howto->type; | |
934 | bfd_byte *hit_data = contents + offset; | |
935 | ||
936 | switch (r_type) | |
937 | { | |
938 | ||
939 | case R_ARM_NONE: | |
940 | return bfd_reloc_ok; | |
941 | ||
942 | case R_ARM_PC24: | |
2c3c46ad CM |
943 | /* Arm B/BL instruction */ |
944 | ||
95c5070c CM |
945 | #ifdef USE_REL |
946 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
947 | #endif | |
2c3c46ad | 948 | /* check for arm calling thumb function */ |
bb3b4377 | 949 | if (sym_flags == STT_ARM_TFUNC) |
2c3c46ad CM |
950 | { |
951 | elf32_arm_to_thumb_stub (info, sym_name, input_bfd, output_bfd, | |
952 | input_section, hit_data, sym_sec, offset, addend, value); | |
953 | return bfd_reloc_ok; | |
954 | } | |
955 | ||
956 | value = value + addend; | |
957 | value -= (input_section->output_section->vma | |
958 | + input_section->output_offset + 8); | |
959 | value -= offset; | |
960 | value = value >> howto->rightshift; | |
efaa65c9 | 961 | |
efaa65c9 CM |
962 | value &= 0xffffff; |
963 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); | |
964 | bfd_put_32 (input_bfd, value, hit_data); | |
965 | return bfd_reloc_ok; | |
966 | ||
967 | case R_ARM_ABS32: | |
95c5070c CM |
968 | #ifdef USE_REL |
969 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
970 | #endif | |
efaa65c9 | 971 | value += addend; |
bb3b4377 | 972 | if (sym_flags == STT_ARM_TFUNC) |
2c3c46ad | 973 | value |= 1; |
efaa65c9 CM |
974 | bfd_put_32 (input_bfd, value, hit_data); |
975 | return bfd_reloc_ok; | |
976 | ||
977 | case R_ARM_REL32: | |
95c5070c CM |
978 | #ifdef USE_REL |
979 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
980 | #endif | |
efaa65c9 | 981 | value -= (input_section->output_section->vma |
2c3c46ad | 982 | + input_section->output_offset); |
efaa65c9 CM |
983 | value += addend; |
984 | ||
985 | bfd_put_32 (input_bfd, value, hit_data); | |
986 | return bfd_reloc_ok; | |
987 | ||
988 | case R_ARM_ABS8: | |
95c5070c CM |
989 | #ifdef USE_REL |
990 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
991 | #endif | |
efaa65c9 | 992 | value += addend; |
efaa65c9 | 993 | if ((long) value > 0x7f || (long) value < -0x80) |
2c3c46ad CM |
994 | return bfd_reloc_overflow; |
995 | ||
efaa65c9 CM |
996 | bfd_put_8 (input_bfd, value, hit_data); |
997 | return bfd_reloc_ok; | |
998 | ||
999 | case R_ARM_ABS16: | |
95c5070c CM |
1000 | #ifdef USE_REL |
1001 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
1002 | #endif | |
efaa65c9 CM |
1003 | value += addend; |
1004 | ||
1005 | if ((long) value > 0x7fff || (long) value < -0x8000) | |
2c3c46ad CM |
1006 | return bfd_reloc_overflow; |
1007 | ||
efaa65c9 CM |
1008 | bfd_put_16 (input_bfd, value, hit_data); |
1009 | return bfd_reloc_ok; | |
1010 | ||
1011 | case R_ARM_ABS12: | |
1012 | /* Support ldr and str instruction for the arm */ | |
1013 | /* Also thumb b (unconditional branch) */ | |
95c5070c CM |
1014 | #ifdef USE_REL |
1015 | addend = (bfd_get_32 (input_bfd, hit_data) & howto->src_mask); | |
1016 | #endif | |
efaa65c9 CM |
1017 | value += addend; |
1018 | ||
1019 | if ((long) value > 0x7ff || (long) value < -0x800) | |
2c3c46ad | 1020 | return bfd_reloc_overflow; |
efaa65c9 CM |
1021 | |
1022 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000); | |
1023 | bfd_put_32 (input_bfd, value, hit_data); | |
1024 | return bfd_reloc_ok; | |
1025 | ||
1026 | case R_ARM_THM_ABS5: | |
1027 | /* Support ldr and str instructions for the thumb. */ | |
1028 | value += addend; | |
1029 | ||
1030 | if ((long) value > 0x1f || (long) value < -0x10) | |
2c3c46ad | 1031 | return bfd_reloc_overflow; |
efaa65c9 CM |
1032 | |
1033 | value |= bfd_get_16 (input_bfd, hit_data) & 0xf82f; | |
1034 | bfd_put_16 (input_bfd, value, hit_data); | |
1035 | return bfd_reloc_ok; | |
efaa65c9 | 1036 | |
efaa65c9 | 1037 | |
efaa65c9 | 1038 | case R_ARM_THM_PC22: |
2c3c46ad | 1039 | /* thumb BL (branch long instruction). */ |
efaa65c9 | 1040 | { |
2c3c46ad CM |
1041 | bfd_vma relocation; |
1042 | boolean overflow = false; | |
1043 | bfd_vma insn = bfd_get_32 (input_bfd, hit_data); | |
1044 | bfd_vma src_mask = 0x007FFFFE; | |
1045 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; | |
1046 | bfd_signed_vma reloc_signed_min = ~reloc_signed_max; | |
1047 | bfd_vma check; | |
1048 | bfd_signed_vma signed_check; | |
1049 | bfd_vma add; | |
1050 | bfd_signed_vma signed_add; | |
1051 | ||
bb3b4377 CM |
1052 | /* If it's not a call to thumb, assume call to arm */ |
1053 | if (sym_flags != STT_ARM_TFUNC) | |
2c3c46ad | 1054 | { |
a61c9386 NC |
1055 | if (elf32_thumb_to_arm_stub |
1056 | (info, sym_name, input_bfd, output_bfd, input_section, | |
1057 | hit_data, sym_sec, offset, addend, value)) | |
1058 | return bfd_reloc_ok; | |
1059 | else | |
1060 | return bfd_reloc_dangerous; | |
2c3c46ad CM |
1061 | } |
1062 | ||
1063 | relocation = value + addend; | |
1064 | relocation -= (input_section->output_section->vma + input_section->output_offset); | |
1065 | relocation -= offset; | |
1066 | ||
1067 | check = relocation >> howto->rightshift; | |
1068 | ||
1069 | /* If this is a signed value, the rightshift just dropped | |
1070 | leading 1 bits (assuming twos complement). */ | |
1071 | if ((bfd_signed_vma) relocation >= 0) | |
1072 | signed_check = check; | |
1073 | else | |
1074 | signed_check = (check | ((bfd_vma) - 1 & ~((bfd_vma) - 1 >> howto->rightshift))); | |
1075 | ||
1076 | /* Get the value from the object file. */ | |
1077 | if (bfd_big_endian (input_bfd)) | |
1078 | add = (((insn) & 0x07ff0000) >> 4) | (((insn) & 0x7ff) << 1); | |
1079 | else | |
1080 | add = ((((insn) & 0x7ff) << 12) | (((insn) & 0x07ff0000) >> 15)); | |
1081 | ||
1082 | /* Get the value from the object file with an appropriate sign. | |
1083 | The expression involving howto->src_mask isolates the upper | |
1084 | bit of src_mask. If that bit is set in the value we are | |
1085 | adding, it is negative, and we subtract out that number times | |
1086 | two. If src_mask includes the highest possible bit, then we | |
1087 | can not get the upper bit, but that does not matter since | |
1088 | signed_add needs no adjustment to become negative in that case. */ | |
1089 | ||
1090 | signed_add = add; | |
1091 | ||
1092 | if ((add & (((~src_mask) >> 1) & src_mask)) != 0) | |
1093 | signed_add -= (((~src_mask) >> 1) & src_mask) << 1; | |
1094 | ||
1095 | /* Add the value from the object file, shifted so that it is a | |
1096 | straight number. */ | |
1097 | /* howto->bitpos == 0 */ | |
1098 | ||
1099 | signed_check += signed_add; | |
1100 | relocation += signed_add; | |
1101 | ||
1102 | /* Assumes two's complement. */ | |
1103 | if (signed_check > reloc_signed_max | |
1104 | || signed_check < reloc_signed_min) | |
1105 | overflow = true; | |
1106 | ||
1107 | /* Put RELOCATION into the correct bits: */ | |
1108 | ||
1109 | if (bfd_big_endian (input_bfd)) | |
1110 | relocation = (((relocation & 0xffe) >> 1) | ((relocation << 4) & 0x07ff0000)); | |
1111 | else | |
1112 | relocation = (((relocation & 0xffe) << 15) | ((relocation >> 12) & 0x7ff)); | |
1113 | ||
1114 | /* Add RELOCATION to the correct bits of X: */ | |
1115 | insn = ((insn & ~howto->dst_mask) | relocation); | |
1116 | ||
1117 | /* Put the relocated value back in the object file: */ | |
1118 | bfd_put_32 (input_bfd, insn, hit_data); | |
1119 | ||
1120 | return (overflow ? bfd_reloc_overflow : bfd_reloc_ok); | |
efaa65c9 CM |
1121 | } |
1122 | break; | |
2c3c46ad | 1123 | |
948beb4a CM |
1124 | case R_ARM_GNU_VTINHERIT: |
1125 | case R_ARM_GNU_VTENTRY: | |
1126 | return bfd_reloc_ok; | |
1127 | ||
efaa65c9 | 1128 | case R_ARM_SBREL32: |
a17e14c1 | 1129 | return bfd_reloc_notsupported; |
efaa65c9 CM |
1130 | |
1131 | case R_ARM_AMP_VCALL9: | |
1132 | return bfd_reloc_notsupported; | |
1133 | ||
1134 | case R_ARM_RSBREL32: | |
1135 | return bfd_reloc_notsupported; | |
1136 | ||
1137 | case R_ARM_THM_RPC22: | |
1138 | return bfd_reloc_notsupported; | |
1139 | ||
1140 | case R_ARM_RREL32: | |
1141 | return bfd_reloc_notsupported; | |
1142 | ||
1143 | case R_ARM_RABS32: | |
1144 | return bfd_reloc_notsupported; | |
1145 | ||
1146 | case R_ARM_RPC24: | |
1147 | return bfd_reloc_notsupported; | |
1148 | ||
1149 | case R_ARM_RBASE: | |
1150 | return bfd_reloc_notsupported; | |
1151 | ||
1152 | default: | |
1153 | return bfd_reloc_notsupported; | |
1154 | } | |
1155 | } | |
774852e9 NC |
1156 | |
1157 | ||
efaa65c9 CM |
1158 | /* Relocate an ARM ELF section. */ |
1159 | static boolean | |
1160 | elf32_arm_relocate_section (output_bfd, info, input_bfd, input_section, | |
2c3c46ad | 1161 | contents, relocs, local_syms, local_sections) |
efaa65c9 CM |
1162 | bfd *output_bfd; |
1163 | struct bfd_link_info *info; | |
1164 | bfd *input_bfd; | |
1165 | asection *input_section; | |
1166 | bfd_byte *contents; | |
1167 | Elf_Internal_Rela *relocs; | |
1168 | Elf_Internal_Sym *local_syms; | |
1169 | asection **local_sections; | |
1170 | { | |
1171 | Elf_Internal_Shdr *symtab_hdr; | |
1172 | struct elf_link_hash_entry **sym_hashes; | |
1173 | Elf_Internal_Rela *rel, *relend; | |
774852e9 | 1174 | const char *name; |
efaa65c9 CM |
1175 | |
1176 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1177 | sym_hashes = elf_sym_hashes (input_bfd); | |
1178 | ||
1179 | rel = relocs; | |
1180 | relend = relocs + input_section->reloc_count; | |
1181 | for (; rel < relend; rel++) | |
1182 | { | |
1183 | int r_type; | |
1184 | reloc_howto_type *howto; | |
1185 | unsigned long r_symndx; | |
1186 | Elf_Internal_Sym *sym; | |
1187 | asection *sec; | |
1188 | struct elf_link_hash_entry *h; | |
1189 | bfd_vma relocation; | |
1190 | bfd_reloc_status_type r; | |
1191 | ||
1192 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1193 | r_type = ELF32_R_TYPE (rel->r_info); | |
948beb4a CM |
1194 | |
1195 | if (r_type == R_ARM_GNU_VTENTRY | |
1196 | || r_type == R_ARM_GNU_VTINHERIT ) | |
1197 | continue; | |
1198 | ||
efaa65c9 CM |
1199 | howto = elf32_arm_howto_table + r_type; |
1200 | ||
1201 | if (info->relocateable) | |
1202 | { | |
1203 | /* This is a relocateable link. We don't have to change | |
2c3c46ad CM |
1204 | anything, unless the reloc is against a section symbol, |
1205 | in which case we have to adjust according to where the | |
1206 | section symbol winds up in the output section. */ | |
efaa65c9 CM |
1207 | if (r_symndx < symtab_hdr->sh_info) |
1208 | { | |
1209 | sym = local_syms + r_symndx; | |
1210 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1211 | { | |
1212 | sec = local_sections[r_symndx]; | |
1213 | rel->r_addend += sec->output_offset + sym->st_value; | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | continue; | |
1218 | } | |
1219 | ||
1220 | /* This is a final link. */ | |
1221 | h = NULL; | |
1222 | sym = NULL; | |
1223 | sec = NULL; | |
1224 | if (r_symndx < symtab_hdr->sh_info) | |
1225 | { | |
1226 | sym = local_syms + r_symndx; | |
1227 | sec = local_sections[r_symndx]; | |
1228 | relocation = (sec->output_section->vma | |
1229 | + sec->output_offset | |
1230 | + sym->st_value); | |
1231 | } | |
1232 | else | |
1233 | { | |
1234 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1235 | while (h->root.type == bfd_link_hash_indirect | |
1236 | || h->root.type == bfd_link_hash_warning) | |
1237 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1238 | if (h->root.type == bfd_link_hash_defined | |
1239 | || h->root.type == bfd_link_hash_defweak) | |
1240 | { | |
1241 | sec = h->root.u.def.section; | |
1242 | relocation = (h->root.u.def.value | |
1243 | + sec->output_section->vma | |
1244 | + sec->output_offset); | |
1245 | } | |
1246 | else if (h->root.type == bfd_link_hash_undefweak) | |
1247 | relocation = 0; | |
1248 | else | |
1249 | { | |
2c3c46ad CM |
1250 | if (!((*info->callbacks->undefined_symbol) |
1251 | (info, h->root.root.string, input_bfd, | |
1252 | input_section, rel->r_offset))) | |
efaa65c9 CM |
1253 | return false; |
1254 | relocation = 0; | |
1255 | } | |
1256 | } | |
1257 | ||
774852e9 NC |
1258 | if (h != NULL) |
1259 | name = h->root.root.string; | |
1260 | else | |
1261 | { | |
1262 | name = (bfd_elf_string_from_elf_section | |
1263 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
1264 | if (name == NULL || *name == '\0') | |
1265 | name = bfd_section_name (input_bfd, sec); | |
1266 | } | |
1267 | ||
efaa65c9 | 1268 | r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd, |
2c3c46ad CM |
1269 | input_section, |
1270 | contents, rel->r_offset, | |
1271 | relocation, rel->r_addend, | |
774852e9 | 1272 | info, sec, name, |
a61c9386 NC |
1273 | (h ? ELF_ST_TYPE (h->type) : |
1274 | ELF_ST_TYPE (sym->st_info))); | |
efaa65c9 CM |
1275 | |
1276 | if (r != bfd_reloc_ok) | |
1277 | { | |
a61c9386 | 1278 | const char * msg = (const char *) 0; |
efaa65c9 | 1279 | |
efaa65c9 CM |
1280 | switch (r) |
1281 | { | |
1282 | case bfd_reloc_overflow: | |
2c3c46ad CM |
1283 | if (!((*info->callbacks->reloc_overflow) |
1284 | (info, name, howto->name, (bfd_vma) 0, | |
1285 | input_bfd, input_section, rel->r_offset))) | |
efaa65c9 CM |
1286 | return false; |
1287 | break; | |
1288 | ||
1289 | case bfd_reloc_undefined: | |
2c3c46ad CM |
1290 | if (!((*info->callbacks->undefined_symbol) |
1291 | (info, name, input_bfd, input_section, | |
1292 | rel->r_offset))) | |
efaa65c9 CM |
1293 | return false; |
1294 | break; | |
1295 | ||
1296 | case bfd_reloc_outofrange: | |
2c3c46ad | 1297 | msg = _ ("internal error: out of range error"); |
efaa65c9 CM |
1298 | goto common_error; |
1299 | ||
1300 | case bfd_reloc_notsupported: | |
2c3c46ad | 1301 | msg = _ ("internal error: unsupported relocation error"); |
efaa65c9 CM |
1302 | goto common_error; |
1303 | ||
1304 | case bfd_reloc_dangerous: | |
2c3c46ad | 1305 | msg = _ ("internal error: dangerous error"); |
efaa65c9 CM |
1306 | goto common_error; |
1307 | ||
1308 | default: | |
2c3c46ad | 1309 | msg = _ ("internal error: unknown error"); |
efaa65c9 CM |
1310 | /* fall through */ |
1311 | ||
1312 | common_error: | |
1313 | if (!((*info->callbacks->warning) | |
1314 | (info, msg, name, input_bfd, input_section, | |
1315 | rel->r_offset))) | |
1316 | return false; | |
1317 | break; | |
1318 | } | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | return true; | |
1323 | } | |
1324 | ||
0e7361bc NC |
1325 | /* Function to keep ARM specific flags in the ELF header. */ |
1326 | static boolean | |
1327 | elf32_arm_set_private_flags (abfd, flags) | |
2c3c46ad | 1328 | bfd *abfd; |
0e7361bc NC |
1329 | flagword flags; |
1330 | { | |
1331 | if (elf_flags_init (abfd) | |
1332 | && elf_elfheader (abfd)->e_flags != flags) | |
1333 | { | |
1334 | if (flags & EF_INTERWORK) | |
2c3c46ad | 1335 | _bfd_error_handler (_ ("\ |
0e7361bc NC |
1336 | Warning: Not setting interwork flag of %s since it has already been specified as non-interworking"), |
1337 | bfd_get_filename (abfd)); | |
1338 | else | |
2c3c46ad | 1339 | _bfd_error_handler (_ ("\ |
0e7361bc | 1340 | Warning: Clearing the interwork flag of %s due to outside request"), |
2c3c46ad | 1341 | bfd_get_filename (abfd)); |
0e7361bc NC |
1342 | } |
1343 | else | |
1344 | { | |
1345 | elf_elfheader (abfd)->e_flags = flags; | |
1346 | elf_flags_init (abfd) = true; | |
1347 | } | |
2c3c46ad | 1348 | |
0e7361bc NC |
1349 | return true; |
1350 | } | |
1351 | ||
1352 | /* Copy backend specific data from one object module to another */ | |
1353 | static boolean | |
1354 | elf32_arm_copy_private_bfd_data (ibfd, obfd) | |
2c3c46ad CM |
1355 | bfd *ibfd; |
1356 | bfd *obfd; | |
0e7361bc NC |
1357 | { |
1358 | flagword in_flags; | |
1359 | flagword out_flags; | |
2c3c46ad CM |
1360 | |
1361 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
0e7361bc NC |
1362 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
1363 | return true; | |
1364 | ||
2c3c46ad | 1365 | in_flags = elf_elfheader (ibfd)->e_flags; |
0e7361bc | 1366 | out_flags = elf_elfheader (obfd)->e_flags; |
2c3c46ad CM |
1367 | |
1368 | if (elf_flags_init (obfd) && in_flags != out_flags) | |
0e7361bc NC |
1369 | { |
1370 | /* Cannot mix PIC and non-PIC code. */ | |
1371 | if ((in_flags & EF_PIC) != (out_flags & EF_PIC)) | |
1372 | return false; | |
2c3c46ad | 1373 | |
0e7361bc NC |
1374 | /* Cannot mix APCS26 and APCS32 code. */ |
1375 | if ((in_flags & EF_APCS_26) != (out_flags & EF_APCS_26)) | |
1376 | return false; | |
2c3c46ad | 1377 | |
0e7361bc NC |
1378 | /* Cannot mix float APCS and non-float APCS code. */ |
1379 | if ((in_flags & EF_APCS_FLOAT) != (out_flags & EF_APCS_FLOAT)) | |
1380 | return false; | |
1381 | ||
1382 | /* If the src and dest have different interworking flags | |
2c3c46ad | 1383 | then turn off the interworking bit. */ |
0e7361bc NC |
1384 | if ((in_flags & EF_INTERWORK) != (out_flags & EF_INTERWORK)) |
1385 | { | |
1386 | if (out_flags & EF_INTERWORK) | |
2c3c46ad | 1387 | _bfd_error_handler (_ ("\ |
0e7361bc | 1388 | Warning: Clearing the interwork flag in %s because non-interworking code in %s has been linked with it"), |
2c3c46ad CM |
1389 | bfd_get_filename (obfd), bfd_get_filename (ibfd)); |
1390 | ||
1391 | in_flags &= ~EF_INTERWORK; | |
0e7361bc NC |
1392 | } |
1393 | } | |
1394 | ||
1395 | elf_elfheader (obfd)->e_flags = in_flags; | |
1396 | elf_flags_init (obfd) = true; | |
2c3c46ad | 1397 | |
0e7361bc NC |
1398 | return true; |
1399 | } | |
1400 | ||
1401 | /* Merge backend specific data from an object file to the output | |
1402 | object file when linking. */ | |
1403 | static boolean | |
1404 | elf32_arm_merge_private_bfd_data (ibfd, obfd) | |
2c3c46ad CM |
1405 | bfd *ibfd; |
1406 | bfd *obfd; | |
0e7361bc NC |
1407 | { |
1408 | flagword out_flags; | |
1409 | flagword in_flags; | |
1410 | ||
2c3c46ad | 1411 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
0e7361bc NC |
1412 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
1413 | return true; | |
1414 | ||
1415 | /* The input BFD must have had its flags initialised. */ | |
a17e14c1 CM |
1416 | /* The following seems bogus to me -- The flags are initialized in |
1417 | the assembler but I don't think an elf_flags_init field is | |
1418 | written into the object */ | |
1419 | /* BFD_ASSERT (elf_flags_init (ibfd)); */ | |
2c3c46ad CM |
1420 | |
1421 | in_flags = elf_elfheader (ibfd)->e_flags; | |
0e7361bc NC |
1422 | out_flags = elf_elfheader (obfd)->e_flags; |
1423 | ||
2c3c46ad | 1424 | if (!elf_flags_init (obfd)) |
0e7361bc NC |
1425 | { |
1426 | /* If the input is the default architecture then do not | |
2c3c46ad CM |
1427 | bother setting the flags for the output architecture, |
1428 | instead allow future merges to do this. If no future | |
1429 | merges ever set these flags then they will retain their | |
1430 | unitialised values, which surprise surprise, correspond | |
1431 | to the default values. */ | |
0e7361bc NC |
1432 | if (bfd_get_arch_info (ibfd)->the_default) |
1433 | return true; | |
2c3c46ad | 1434 | |
0e7361bc NC |
1435 | elf_flags_init (obfd) = true; |
1436 | elf_elfheader (obfd)->e_flags = in_flags; | |
1437 | ||
1438 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
1439 | && bfd_get_arch_info (obfd)->the_default) | |
1440 | return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd)); | |
1441 | ||
1442 | return true; | |
1443 | } | |
1444 | ||
1445 | /* Check flag compatibility. */ | |
1446 | if (in_flags == out_flags) | |
1447 | return true; | |
1448 | ||
1449 | /* Complain about various flag mismatches. */ | |
2c3c46ad | 1450 | |
0e7361bc | 1451 | if ((in_flags & EF_APCS_26) != (out_flags & EF_APCS_26)) |
2c3c46ad | 1452 | _bfd_error_handler (_ ("\ |
0e7361bc NC |
1453 | Error: %s compiled for APCS-%d, whereas %s is compiled for APCS-%d"), |
1454 | bfd_get_filename (ibfd), | |
1455 | in_flags & EF_APCS_26 ? 26 : 32, | |
1456 | bfd_get_filename (obfd), | |
1457 | out_flags & EF_APCS_26 ? 26 : 32); | |
2c3c46ad | 1458 | |
0e7361bc | 1459 | if ((in_flags & EF_APCS_FLOAT) != (out_flags & EF_APCS_FLOAT)) |
2c3c46ad | 1460 | _bfd_error_handler (_ ("\ |
0e7361bc NC |
1461 | Error: %s passes floats in %s registers, whereas %s passes them in %s registers"), |
1462 | bfd_get_filename (ibfd), | |
2c3c46ad | 1463 | in_flags & EF_APCS_FLOAT ? _ ("float") : _ ("integer"), |
0e7361bc | 1464 | bfd_get_filename (obfd), |
2c3c46ad CM |
1465 | out_flags & EF_APCS_26 ? _ ("float") : _ ("integer")); |
1466 | ||
0e7361bc | 1467 | if ((in_flags & EF_PIC) != (out_flags & EF_PIC)) |
2c3c46ad | 1468 | _bfd_error_handler (_ ("\ |
0e7361bc NC |
1469 | Error: %s is compiled as position %s code, whereas %s is not"), |
1470 | bfd_get_filename (ibfd), | |
2c3c46ad | 1471 | in_flags & EF_PIC ? _ ("independent") : _ ("dependent"), |
0e7361bc NC |
1472 | bfd_get_filename (obfd)); |
1473 | ||
1474 | /* Interworking mismatch is only a warning. */ | |
1475 | if ((in_flags & EF_INTERWORK) != (out_flags & EF_INTERWORK)) | |
1476 | { | |
2c3c46ad | 1477 | _bfd_error_handler (_ ("\ |
0e7361bc NC |
1478 | Warning: %s %s interworking, whereas %s %s"), |
1479 | bfd_get_filename (ibfd), | |
2c3c46ad | 1480 | in_flags & EF_INTERWORK ? _ ("supports") : _ ("does not support"), |
0e7361bc | 1481 | bfd_get_filename (obfd), |
2c3c46ad | 1482 | out_flags & EF_INTERWORK ? _ ("does not") : _ ("does")); |
0e7361bc NC |
1483 | return true; |
1484 | } | |
1485 | ||
1486 | return false; | |
1487 | } | |
1488 | ||
1489 | /* Display the flags field */ | |
1490 | static boolean | |
1491 | elf32_arm_print_private_bfd_data (abfd, ptr) | |
2c3c46ad CM |
1492 | bfd *abfd; |
1493 | PTR ptr; | |
0e7361bc | 1494 | { |
2c3c46ad CM |
1495 | FILE *file = (FILE *) ptr; |
1496 | ||
0e7361bc NC |
1497 | BFD_ASSERT (abfd != NULL && ptr != NULL); |
1498 | ||
1499 | /* Print normal ELF private data. */ | |
1500 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
1501 | ||
1502 | /* Ignore init flag - it may not be set, despite the flags field containing valid data. */ | |
2c3c46ad | 1503 | |
0e7361bc | 1504 | /* xgettext:c-format */ |
2c3c46ad | 1505 | fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags); |
0e7361bc NC |
1506 | |
1507 | if (elf_elfheader (abfd)->e_flags & EF_INTERWORK) | |
2c3c46ad | 1508 | fprintf (file, _ (" [interworking enabled]")); |
0e7361bc | 1509 | else |
2c3c46ad CM |
1510 | fprintf (file, _ (" [interworking not enabled]")); |
1511 | ||
0e7361bc | 1512 | if (elf_elfheader (abfd)->e_flags & EF_APCS_26) |
2c3c46ad | 1513 | fprintf (file, _ (" [APCS-26]")); |
0e7361bc | 1514 | else |
2c3c46ad CM |
1515 | fprintf (file, _ (" [APCS-32]")); |
1516 | ||
0e7361bc | 1517 | if (elf_elfheader (abfd)->e_flags & EF_APCS_FLOAT) |
2c3c46ad | 1518 | fprintf (file, _ (" [floats passed in float registers]")); |
0e7361bc | 1519 | else |
2c3c46ad CM |
1520 | fprintf (file, _ (" [floats passed in integer registers]")); |
1521 | ||
0e7361bc | 1522 | if (elf_elfheader (abfd)->e_flags & EF_PIC) |
2c3c46ad | 1523 | fprintf (file, _ (" [position independent]")); |
0e7361bc | 1524 | else |
2c3c46ad CM |
1525 | fprintf (file, _ (" [absolute position]")); |
1526 | ||
0e7361bc | 1527 | fputc ('\n', file); |
2c3c46ad | 1528 | |
0e7361bc NC |
1529 | return true; |
1530 | } | |
1531 | ||
bb3b4377 | 1532 | static int |
948beb4a | 1533 | elf32_arm_get_symbol_type (elf_sym, type) |
a61c9386 | 1534 | Elf_Internal_Sym * elf_sym; |
948beb4a | 1535 | int type; |
bb3b4377 | 1536 | { |
948beb4a CM |
1537 | if (ELF_ST_TYPE (elf_sym->st_info) == STT_ARM_TFUNC) |
1538 | return ELF_ST_TYPE (elf_sym->st_info); | |
1539 | else | |
1540 | return type; | |
bb3b4377 CM |
1541 | } |
1542 | ||
948beb4a CM |
1543 | static asection * |
1544 | elf32_arm_gc_mark_hook (abfd, info, rel, h, sym) | |
1545 | bfd *abfd; | |
1546 | struct bfd_link_info *info; | |
1547 | Elf_Internal_Rela *rel; | |
1548 | struct elf_link_hash_entry *h; | |
1549 | Elf_Internal_Sym *sym; | |
1550 | { | |
1551 | if (h != NULL) | |
1552 | { | |
1553 | switch (ELF32_R_TYPE (rel->r_info)) | |
1554 | { | |
1555 | case R_ARM_GNU_VTINHERIT: | |
1556 | case R_ARM_GNU_VTENTRY: | |
1557 | break; | |
1558 | ||
1559 | default: | |
948beb4a CM |
1560 | switch (h->root.type) |
1561 | { | |
1562 | case bfd_link_hash_defined: | |
1563 | case bfd_link_hash_defweak: | |
1564 | return h->root.u.def.section; | |
1565 | ||
1566 | case bfd_link_hash_common: | |
1567 | return h->root.u.c.p->section; | |
1568 | } | |
1569 | } | |
1570 | } | |
1571 | else | |
1572 | { | |
1573 | if (!(elf_bad_symtab (abfd) | |
1574 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
1575 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
1576 | && sym->st_shndx != SHN_COMMON)) | |
1577 | { | |
1578 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
1579 | } | |
1580 | } | |
1581 | return NULL; | |
1582 | } | |
0e7361bc | 1583 | |
948beb4a CM |
1584 | static boolean |
1585 | elf32_arm_gc_sweep_hook (abfd, info, sec, relocs) | |
1586 | bfd *abfd; | |
1587 | struct bfd_link_info *info; | |
1588 | asection *sec; | |
1589 | const Elf_Internal_Rela *relocs; | |
1590 | { | |
1591 | /* we don't use got and plt entries for armelf */ | |
1592 | return true; | |
1593 | } | |
a61c9386 | 1594 | |
948beb4a CM |
1595 | /* Look through the relocs for a section during the first phase. |
1596 | Since we don't do .gots or .plts, we just need to consider the | |
1597 | virtual table relocs for gc. */ | |
1598 | ||
1599 | static boolean | |
1600 | elf32_arm_check_relocs (abfd, info, sec, relocs) | |
1601 | bfd *abfd; | |
1602 | struct bfd_link_info *info; | |
1603 | asection *sec; | |
1604 | const Elf_Internal_Rela *relocs; | |
1605 | { | |
1606 | Elf_Internal_Shdr *symtab_hdr; | |
1607 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
1608 | const Elf_Internal_Rela *rel; | |
1609 | const Elf_Internal_Rela *rel_end; | |
1610 | ||
1611 | if (info->relocateable) | |
1612 | return true; | |
1613 | ||
1614 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1615 | sym_hashes = elf_sym_hashes (abfd); | |
1616 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym); | |
1617 | if (!elf_bad_symtab (abfd)) | |
1618 | sym_hashes_end -= symtab_hdr->sh_info; | |
1619 | ||
1620 | rel_end = relocs + sec->reloc_count; | |
1621 | for (rel = relocs; rel < rel_end; rel++) | |
1622 | { | |
1623 | struct elf_link_hash_entry *h; | |
1624 | unsigned long r_symndx; | |
1625 | ||
1626 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1627 | if (r_symndx < symtab_hdr->sh_info) | |
1628 | h = NULL; | |
1629 | else | |
1630 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1631 | ||
1632 | switch (ELF32_R_TYPE (rel->r_info)) | |
1633 | { | |
1634 | /* This relocation describes the C++ object vtable hierarchy. | |
1635 | Reconstruct it for later use during GC. */ | |
1636 | case R_ARM_GNU_VTINHERIT: | |
1637 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
1638 | return false; | |
1639 | break; | |
1640 | ||
1641 | /* This relocation describes which C++ vtable entries are actually | |
1642 | used. Record for later use during GC. */ | |
1643 | case R_ARM_GNU_VTENTRY: | |
1644 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
1645 | return false; | |
1646 | break; | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | return true; | |
1651 | } | |
a61c9386 | 1652 | |
948beb4a | 1653 | |
a61c9386 NC |
1654 | /* Find the nearest line to a particular section and offset, for error |
1655 | reporting. This code is a duplicate of the code in elf.c, except | |
1656 | that it also accepts STT_ARM_TFUNC as a symbol that names a function. */ | |
1657 | ||
1658 | boolean | |
1659 | elf32_arm_find_nearest_line | |
1660 | (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr) | |
1661 | bfd * abfd; | |
1662 | asection * section; | |
1663 | asymbol ** symbols; | |
1664 | bfd_vma offset; | |
1665 | CONST char ** filename_ptr; | |
1666 | CONST char ** functionname_ptr; | |
1667 | unsigned int * line_ptr; | |
1668 | { | |
1669 | boolean found; | |
1670 | const char * filename; | |
1671 | asymbol * func; | |
1672 | bfd_vma low_func; | |
1673 | asymbol ** p; | |
1674 | ||
1675 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
1676 | filename_ptr, functionname_ptr, | |
1677 | line_ptr)) | |
1678 | return true; | |
1679 | ||
1680 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
1681 | &found, filename_ptr, | |
1682 | functionname_ptr, line_ptr, | |
1683 | &elf_tdata (abfd)->line_info)) | |
1684 | return false; | |
1685 | ||
1686 | if (found) | |
1687 | return true; | |
1688 | ||
1689 | if (symbols == NULL) | |
1690 | return false; | |
1691 | ||
1692 | filename = NULL; | |
1693 | func = NULL; | |
1694 | low_func = 0; | |
1695 | ||
1696 | for (p = symbols; *p != NULL; p++) | |
1697 | { | |
1698 | elf_symbol_type *q; | |
1699 | ||
1700 | q = (elf_symbol_type *) *p; | |
1701 | ||
1702 | if (bfd_get_section (&q->symbol) != section) | |
1703 | continue; | |
1704 | ||
1705 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
1706 | { | |
1707 | default: | |
1708 | break; | |
1709 | case STT_FILE: | |
1710 | filename = bfd_asymbol_name (&q->symbol); | |
1711 | break; | |
1712 | case STT_NOTYPE: | |
1713 | case STT_FUNC: | |
1714 | case STT_ARM_TFUNC: | |
1715 | if (q->symbol.section == section | |
1716 | && q->symbol.value >= low_func | |
1717 | && q->symbol.value <= offset) | |
1718 | { | |
1719 | func = (asymbol *) q; | |
1720 | low_func = q->symbol.value; | |
1721 | } | |
1722 | break; | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | if (func == NULL) | |
1727 | return false; | |
1728 | ||
1729 | *filename_ptr = filename; | |
1730 | *functionname_ptr = bfd_asymbol_name (func); | |
1731 | *line_ptr = 0; | |
1732 | ||
1733 | return true; | |
1734 | } | |
1735 | ||
efaa65c9 CM |
1736 | #define ELF_ARCH bfd_arch_arm |
1737 | #define ELF_MACHINE_CODE EM_ARM | |
1738 | ||
0e7361bc | 1739 | #define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup |
0e7361bc NC |
1740 | #define elf_backend_relocate_section elf32_arm_relocate_section |
1741 | #define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data | |
1742 | #define bfd_elf32_bfd_merge_private_bfd_data elf32_arm_merge_private_bfd_data | |
1743 | #define bfd_elf32_bfd_set_private_flags elf32_arm_set_private_flags | |
1744 | #define bfd_elf32_bfd_print_private_bfd_data elf32_arm_print_private_bfd_data | |
2c3c46ad | 1745 | #define bfd_elf32_bfd_link_hash_table_create elf32_arm_link_hash_table_create |
a61c9386 | 1746 | #define bfd_elf32_find_nearest_line elf32_arm_find_nearest_line |
bb3b4377 | 1747 | #define elf_backend_get_symbol_type elf32_arm_get_symbol_type |
948beb4a CM |
1748 | #define elf_backend_gc_mark_hook elf32_arm_gc_mark_hook |
1749 | #define elf_backend_gc_sweep_hook elf32_arm_gc_sweep_hook | |
1750 | #define elf_backend_check_relocs elf32_arm_check_relocs | |
1751 | ||
1752 | #define elf_backend_can_gc_sections 1 | |
efaa65c9 CM |
1753 | |
1754 | #include "elf32-target.h" |