490a0d9261f6d663f4d60c7c9eee3f5572992c09
[deliverable/binutils-gdb.git] / bfd / elf32-arm.h
1 /* 32-bit ELF support for ARM
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #ifndef USE_REL
22 #define USE_REL 0
23 #endif
24
25 typedef unsigned long int insn32;
26 typedef unsigned short int insn16;
27
28 static bfd_boolean elf32_arm_set_private_flags
29 PARAMS ((bfd *, flagword));
30 static bfd_boolean elf32_arm_copy_private_bfd_data
31 PARAMS ((bfd *, bfd *));
32 static bfd_boolean elf32_arm_merge_private_bfd_data
33 PARAMS ((bfd *, bfd *));
34 static bfd_boolean elf32_arm_print_private_bfd_data
35 PARAMS ((bfd *, PTR));
36 static int elf32_arm_get_symbol_type
37 PARAMS (( Elf_Internal_Sym *, int));
38 static struct bfd_link_hash_table *elf32_arm_link_hash_table_create
39 PARAMS ((bfd *));
40 static bfd_reloc_status_type elf32_arm_final_link_relocate
41 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
42 Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
43 const char *, int, struct elf_link_hash_entry *));
44 static insn32 insert_thumb_branch
45 PARAMS ((insn32, int));
46 static struct elf_link_hash_entry *find_thumb_glue
47 PARAMS ((struct bfd_link_info *, const char *, bfd *));
48 static struct elf_link_hash_entry *find_arm_glue
49 PARAMS ((struct bfd_link_info *, const char *, bfd *));
50 static void elf32_arm_post_process_headers
51 PARAMS ((bfd *, struct bfd_link_info *));
52 static int elf32_arm_to_thumb_stub
53 PARAMS ((struct bfd_link_info *, const char *, bfd *, bfd *, asection *,
54 bfd_byte *, asection *, bfd_vma, bfd_signed_vma, bfd_vma));
55 static int elf32_thumb_to_arm_stub
56 PARAMS ((struct bfd_link_info *, const char *, bfd *, bfd *, asection *,
57 bfd_byte *, asection *, bfd_vma, bfd_signed_vma, bfd_vma));
58 static bfd_boolean elf32_arm_relocate_section
59 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
60 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
61 static asection * elf32_arm_gc_mark_hook
62 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
63 struct elf_link_hash_entry *, Elf_Internal_Sym *));
64 static bfd_boolean elf32_arm_gc_sweep_hook
65 PARAMS ((bfd *, struct bfd_link_info *, asection *,
66 const Elf_Internal_Rela *));
67 static bfd_boolean elf32_arm_check_relocs
68 PARAMS ((bfd *, struct bfd_link_info *, asection *,
69 const Elf_Internal_Rela *));
70 static bfd_boolean elf32_arm_find_nearest_line
71 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
72 const char **, unsigned int *));
73 static bfd_boolean elf32_arm_adjust_dynamic_symbol
74 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static bfd_boolean elf32_arm_size_dynamic_sections
76 PARAMS ((bfd *, struct bfd_link_info *));
77 static bfd_boolean elf32_arm_finish_dynamic_symbol
78 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
79 Elf_Internal_Sym *));
80 static bfd_boolean elf32_arm_finish_dynamic_sections
81 PARAMS ((bfd *, struct bfd_link_info *));
82 static struct bfd_hash_entry * elf32_arm_link_hash_newfunc
83 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
84 #if USE_REL
85 static void arm_add_to_rel
86 PARAMS ((bfd *, bfd_byte *, reloc_howto_type *, bfd_signed_vma));
87 #endif
88 static bfd_boolean allocate_dynrelocs
89 PARAMS ((struct elf_link_hash_entry *h, PTR inf));
90 static bfd_boolean create_got_section
91 PARAMS ((bfd * dynobj, struct bfd_link_info * info));
92 static bfd_boolean elf32_arm_create_dynamic_sections
93 PARAMS ((bfd * dynobj, struct bfd_link_info * info));
94 static enum elf_reloc_type_class elf32_arm_reloc_type_class
95 PARAMS ((const Elf_Internal_Rela *));
96 static bfd_boolean elf32_arm_object_p
97 PARAMS ((bfd *));
98
99 #ifndef ELFARM_NABI_C_INCLUDED
100 static void record_arm_to_thumb_glue
101 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
102 static void record_thumb_to_arm_glue
103 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
104 bfd_boolean bfd_elf32_arm_allocate_interworking_sections
105 PARAMS ((struct bfd_link_info *));
106 bfd_boolean bfd_elf32_arm_get_bfd_for_interworking
107 PARAMS ((bfd *, struct bfd_link_info *));
108 bfd_boolean bfd_elf32_arm_process_before_allocation
109 PARAMS ((bfd *, struct bfd_link_info *, int, int));
110 #endif
111
112
113 #define INTERWORK_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK)
114
115 /* The linker script knows the section names for placement.
116 The entry_names are used to do simple name mangling on the stubs.
117 Given a function name, and its type, the stub can be found. The
118 name can be changed. The only requirement is the %s be present. */
119 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
120 #define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb"
121
122 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
123 #define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm"
124
125 /* The name of the dynamic interpreter. This is put in the .interp
126 section. */
127 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
128
129 #ifdef FOUR_WORD_PLT
130
131 /* The size in bytes of the special first entry in the procedure
132 linkage table. */
133 #define PLT_HEADER_SIZE 16
134
135 /* The size in bytes of an entry in the procedure linkage table. */
136 #define PLT_ENTRY_SIZE 16
137
138 /* The first entry in a procedure linkage table looks like
139 this. It is set up so that any shared library function that is
140 called before the relocation has been set up calls the dynamic
141 linker first. */
142 static const bfd_vma elf32_arm_plt0_entry [PLT_HEADER_SIZE / 4] =
143 {
144 0xe52de004, /* str lr, [sp, #-4]! */
145 0xe59fe010, /* ldr lr, [pc, #16] */
146 0xe08fe00e, /* add lr, pc, lr */
147 0xe5bef008, /* ldr pc, [lr, #8]! */
148 };
149
150 /* Subsequent entries in a procedure linkage table look like
151 this. */
152 static const bfd_vma elf32_arm_plt_entry [PLT_ENTRY_SIZE / 4] =
153 {
154 0xe28fc600, /* add ip, pc, #NN */
155 0xe28cca00, /* add ip, ip, #NN */
156 0xe5bcf000, /* ldr pc, [ip, #NN]! */
157 0x00000000, /* unused */
158 };
159
160 #else
161
162 /* The size in bytes of the special first entry in the procedure
163 linkage table. */
164 #define PLT_HEADER_SIZE 20
165
166 /* The size in bytes of an entry in the procedure linkage table. */
167 #define PLT_ENTRY_SIZE 12
168
169 /* The first entry in a procedure linkage table looks like
170 this. It is set up so that any shared library function that is
171 called before the relocation has been set up calls the dynamic
172 linker first. */
173 static const bfd_vma elf32_arm_plt0_entry [PLT_HEADER_SIZE / 4] =
174 {
175 0xe52de004, /* str lr, [sp, #-4]! */
176 0xe59fe004, /* ldr lr, [pc, #4] */
177 0xe08fe00e, /* add lr, pc, lr */
178 0xe5bef008, /* ldr pc, [lr, #8]! */
179 0x00000000, /* &GOT[0] - . */
180 };
181
182 /* Subsequent entries in a procedure linkage table look like
183 this. */
184 static const bfd_vma elf32_arm_plt_entry [PLT_ENTRY_SIZE / 4] =
185 {
186 0xe28fc600, /* add ip, pc, #0xNN00000 */
187 0xe28cca00, /* add ip, ip, #0xNN000 */
188 0xe5bcf000, /* ldr pc, [ip, #0xNNN]! */
189 };
190
191 #endif
192
193 /* Used to build a map of a section. This is required for mixed-endian
194 code/data. */
195
196 typedef struct elf32_elf_section_map
197 {
198 bfd_vma vma;
199 char type;
200 }
201 elf32_arm_section_map;
202
203 struct _arm_elf_section_data
204 {
205 struct bfd_elf_section_data elf;
206 int mapcount;
207 elf32_arm_section_map *map;
208 };
209
210 #define elf32_arm_section_data(sec) \
211 ((struct _arm_elf_section_data *) elf_section_data (sec))
212
213 /* The ARM linker needs to keep track of the number of relocs that it
214 decides to copy in check_relocs for each symbol. This is so that
215 it can discard PC relative relocs if it doesn't need them when
216 linking with -Bsymbolic. We store the information in a field
217 extending the regular ELF linker hash table. */
218
219 /* This structure keeps track of the number of PC relative relocs we
220 have copied for a given symbol. */
221 struct elf32_arm_relocs_copied
222 {
223 /* Next section. */
224 struct elf32_arm_relocs_copied * next;
225 /* A section in dynobj. */
226 asection * section;
227 /* Number of relocs copied in this section. */
228 bfd_size_type count;
229 };
230
231 /* Arm ELF linker hash entry. */
232 struct elf32_arm_link_hash_entry
233 {
234 struct elf_link_hash_entry root;
235
236 /* Number of PC relative relocs copied for this symbol. */
237 struct elf32_arm_relocs_copied * relocs_copied;
238 };
239
240 /* Traverse an arm ELF linker hash table. */
241 #define elf32_arm_link_hash_traverse(table, func, info) \
242 (elf_link_hash_traverse \
243 (&(table)->root, \
244 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
245 (info)))
246
247 /* Get the ARM elf linker hash table from a link_info structure. */
248 #define elf32_arm_hash_table(info) \
249 ((struct elf32_arm_link_hash_table *) ((info)->hash))
250
251 /* ARM ELF linker hash table. */
252 struct elf32_arm_link_hash_table
253 {
254 /* The main hash table. */
255 struct elf_link_hash_table root;
256
257 /* The size in bytes of the section containing the Thumb-to-ARM glue. */
258 bfd_size_type thumb_glue_size;
259
260 /* The size in bytes of the section containing the ARM-to-Thumb glue. */
261 bfd_size_type arm_glue_size;
262
263 /* An arbitrary input BFD chosen to hold the glue sections. */
264 bfd * bfd_of_glue_owner;
265
266 /* A boolean indicating whether knowledge of the ARM's pipeline
267 length should be applied by the linker. */
268 int no_pipeline_knowledge;
269
270 /* Nonzero to output a BE8 image. */
271 int byteswap_code;
272
273 /* Short-cuts to get to dynamic linker sections. */
274 asection *sgot;
275 asection *sgotplt;
276 asection *srelgot;
277 asection *splt;
278 asection *srelplt;
279 asection *sdynbss;
280 asection *srelbss;
281
282 /* Small local sym to section mapping cache. */
283 struct sym_sec_cache sym_sec;
284 };
285
286 /* Create an entry in an ARM ELF linker hash table. */
287
288 static struct bfd_hash_entry *
289 elf32_arm_link_hash_newfunc (entry, table, string)
290 struct bfd_hash_entry * entry;
291 struct bfd_hash_table * table;
292 const char * string;
293 {
294 struct elf32_arm_link_hash_entry * ret =
295 (struct elf32_arm_link_hash_entry *) entry;
296
297 /* Allocate the structure if it has not already been allocated by a
298 subclass. */
299 if (ret == (struct elf32_arm_link_hash_entry *) NULL)
300 ret = ((struct elf32_arm_link_hash_entry *)
301 bfd_hash_allocate (table,
302 sizeof (struct elf32_arm_link_hash_entry)));
303 if (ret == (struct elf32_arm_link_hash_entry *) NULL)
304 return (struct bfd_hash_entry *) ret;
305
306 /* Call the allocation method of the superclass. */
307 ret = ((struct elf32_arm_link_hash_entry *)
308 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
309 table, string));
310 if (ret != (struct elf32_arm_link_hash_entry *) NULL)
311 ret->relocs_copied = NULL;
312
313 return (struct bfd_hash_entry *) ret;
314 }
315
316 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
317 shortcuts to them in our hash table. */
318
319 static bfd_boolean
320 create_got_section (dynobj, info)
321 bfd *dynobj;
322 struct bfd_link_info *info;
323 {
324 struct elf32_arm_link_hash_table *htab;
325
326 if (! _bfd_elf_create_got_section (dynobj, info))
327 return FALSE;
328
329 htab = elf32_arm_hash_table (info);
330 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
331 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
332 if (!htab->sgot || !htab->sgotplt)
333 abort ();
334
335 htab->srelgot = bfd_make_section (dynobj, ".rel.got");
336 if (htab->srelgot == NULL
337 || ! bfd_set_section_flags (dynobj, htab->srelgot,
338 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
339 | SEC_IN_MEMORY | SEC_LINKER_CREATED
340 | SEC_READONLY))
341 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
342 return FALSE;
343 return TRUE;
344 }
345
346 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
347 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
348 hash table. */
349
350 static bfd_boolean
351 elf32_arm_create_dynamic_sections (dynobj, info)
352 bfd *dynobj;
353 struct bfd_link_info *info;
354 {
355 struct elf32_arm_link_hash_table *htab;
356
357 htab = elf32_arm_hash_table (info);
358 if (!htab->sgot && !create_got_section (dynobj, info))
359 return FALSE;
360
361 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
362 return FALSE;
363
364 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
365 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
366 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
367 if (!info->shared)
368 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
369
370 if (!htab->splt || !htab->srelplt || !htab->sdynbss
371 || (!info->shared && !htab->srelbss))
372 abort ();
373
374 return TRUE;
375 }
376
377 /* Copy the extra info we tack onto an elf_link_hash_entry. */
378
379 static void
380 elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
381 struct elf_link_hash_entry *dir,
382 struct elf_link_hash_entry *ind)
383 {
384 struct elf32_arm_link_hash_entry *edir, *eind;
385
386 edir = (struct elf32_arm_link_hash_entry *) dir;
387 eind = (struct elf32_arm_link_hash_entry *) ind;
388
389 if (eind->relocs_copied != NULL)
390 {
391 if (edir->relocs_copied != NULL)
392 {
393 struct elf32_arm_relocs_copied **pp;
394 struct elf32_arm_relocs_copied *p;
395
396 if (ind->root.type == bfd_link_hash_indirect)
397 abort ();
398
399 /* Add reloc counts against the weak sym to the strong sym
400 list. Merge any entries against the same section. */
401 for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
402 {
403 struct elf32_arm_relocs_copied *q;
404
405 for (q = edir->relocs_copied; q != NULL; q = q->next)
406 if (q->section == p->section)
407 {
408 q->count += p->count;
409 *pp = p->next;
410 break;
411 }
412 if (q == NULL)
413 pp = &p->next;
414 }
415 *pp = edir->relocs_copied;
416 }
417
418 edir->relocs_copied = eind->relocs_copied;
419 eind->relocs_copied = NULL;
420 }
421
422 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
423 }
424
425 /* Create an ARM elf linker hash table. */
426
427 static struct bfd_link_hash_table *
428 elf32_arm_link_hash_table_create (abfd)
429 bfd *abfd;
430 {
431 struct elf32_arm_link_hash_table *ret;
432 bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
433
434 ret = (struct elf32_arm_link_hash_table *) bfd_malloc (amt);
435 if (ret == (struct elf32_arm_link_hash_table *) NULL)
436 return NULL;
437
438 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
439 elf32_arm_link_hash_newfunc))
440 {
441 free (ret);
442 return NULL;
443 }
444
445 ret->sgot = NULL;
446 ret->sgotplt = NULL;
447 ret->srelgot = NULL;
448 ret->splt = NULL;
449 ret->srelplt = NULL;
450 ret->sdynbss = NULL;
451 ret->srelbss = NULL;
452 ret->thumb_glue_size = 0;
453 ret->arm_glue_size = 0;
454 ret->bfd_of_glue_owner = NULL;
455 ret->no_pipeline_knowledge = 0;
456 ret->byteswap_code = 0;
457 ret->sym_sec.abfd = NULL;
458
459 return &ret->root.root;
460 }
461
462 /* Locate the Thumb encoded calling stub for NAME. */
463
464 static struct elf_link_hash_entry *
465 find_thumb_glue (link_info, name, input_bfd)
466 struct bfd_link_info *link_info;
467 const char *name;
468 bfd *input_bfd;
469 {
470 char *tmp_name;
471 struct elf_link_hash_entry *hash;
472 struct elf32_arm_link_hash_table *hash_table;
473
474 /* We need a pointer to the armelf specific hash table. */
475 hash_table = elf32_arm_hash_table (link_info);
476
477 tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
478 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
479
480 BFD_ASSERT (tmp_name);
481
482 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
483
484 hash = elf_link_hash_lookup
485 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
486
487 if (hash == NULL)
488 /* xgettext:c-format */
489 (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
490 input_bfd, tmp_name, name);
491
492 free (tmp_name);
493
494 return hash;
495 }
496
497 /* Locate the ARM encoded calling stub for NAME. */
498
499 static struct elf_link_hash_entry *
500 find_arm_glue (link_info, name, input_bfd)
501 struct bfd_link_info *link_info;
502 const char *name;
503 bfd *input_bfd;
504 {
505 char *tmp_name;
506 struct elf_link_hash_entry *myh;
507 struct elf32_arm_link_hash_table *hash_table;
508
509 /* We need a pointer to the elfarm specific hash table. */
510 hash_table = elf32_arm_hash_table (link_info);
511
512 tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
513 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
514
515 BFD_ASSERT (tmp_name);
516
517 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
518
519 myh = elf_link_hash_lookup
520 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
521
522 if (myh == NULL)
523 /* xgettext:c-format */
524 (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
525 input_bfd, tmp_name, name);
526
527 free (tmp_name);
528
529 return myh;
530 }
531
532 /* ARM->Thumb glue:
533
534 .arm
535 __func_from_arm:
536 ldr r12, __func_addr
537 bx r12
538 __func_addr:
539 .word func @ behave as if you saw a ARM_32 reloc. */
540
541 #define ARM2THUMB_GLUE_SIZE 12
542 static const insn32 a2t1_ldr_insn = 0xe59fc000;
543 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
544 static const insn32 a2t3_func_addr_insn = 0x00000001;
545
546 /* Thumb->ARM: Thumb->(non-interworking aware) ARM
547
548 .thumb .thumb
549 .align 2 .align 2
550 __func_from_thumb: __func_from_thumb:
551 bx pc push {r6, lr}
552 nop ldr r6, __func_addr
553 .arm mov lr, pc
554 __func_change_to_arm: bx r6
555 b func .arm
556 __func_back_to_thumb:
557 ldmia r13! {r6, lr}
558 bx lr
559 __func_addr:
560 .word func */
561
562 #define THUMB2ARM_GLUE_SIZE 8
563 static const insn16 t2a1_bx_pc_insn = 0x4778;
564 static const insn16 t2a2_noop_insn = 0x46c0;
565 static const insn32 t2a3_b_insn = 0xea000000;
566
567 #ifndef ELFARM_NABI_C_INCLUDED
568 bfd_boolean
569 bfd_elf32_arm_allocate_interworking_sections (info)
570 struct bfd_link_info * info;
571 {
572 asection * s;
573 bfd_byte * foo;
574 struct elf32_arm_link_hash_table * globals;
575
576 globals = elf32_arm_hash_table (info);
577
578 BFD_ASSERT (globals != NULL);
579
580 if (globals->arm_glue_size != 0)
581 {
582 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
583
584 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
585 ARM2THUMB_GLUE_SECTION_NAME);
586
587 BFD_ASSERT (s != NULL);
588
589 foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
590 globals->arm_glue_size);
591
592 s->size = globals->arm_glue_size;
593 s->contents = foo;
594 }
595
596 if (globals->thumb_glue_size != 0)
597 {
598 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
599
600 s = bfd_get_section_by_name
601 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
602
603 BFD_ASSERT (s != NULL);
604
605 foo = (bfd_byte *) bfd_alloc (globals->bfd_of_glue_owner,
606 globals->thumb_glue_size);
607
608 s->size = globals->thumb_glue_size;
609 s->contents = foo;
610 }
611
612 return TRUE;
613 }
614
615 static void
616 record_arm_to_thumb_glue (link_info, h)
617 struct bfd_link_info * link_info;
618 struct elf_link_hash_entry * h;
619 {
620 const char * name = h->root.root.string;
621 asection * s;
622 char * tmp_name;
623 struct elf_link_hash_entry * myh;
624 struct bfd_link_hash_entry * bh;
625 struct elf32_arm_link_hash_table * globals;
626 bfd_vma val;
627
628 globals = elf32_arm_hash_table (link_info);
629
630 BFD_ASSERT (globals != NULL);
631 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
632
633 s = bfd_get_section_by_name
634 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
635
636 BFD_ASSERT (s != NULL);
637
638 tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
639 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
640
641 BFD_ASSERT (tmp_name);
642
643 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
644
645 myh = elf_link_hash_lookup
646 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
647
648 if (myh != NULL)
649 {
650 /* We've already seen this guy. */
651 free (tmp_name);
652 return;
653 }
654
655 /* The only trick here is using hash_table->arm_glue_size as the value. Even
656 though the section isn't allocated yet, this is where we will be putting
657 it. */
658 bh = NULL;
659 val = globals->arm_glue_size + 1;
660 _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
661 tmp_name, BSF_GLOBAL, s, val,
662 NULL, TRUE, FALSE, &bh);
663
664 free (tmp_name);
665
666 globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
667
668 return;
669 }
670
671 static void
672 record_thumb_to_arm_glue (link_info, h)
673 struct bfd_link_info *link_info;
674 struct elf_link_hash_entry *h;
675 {
676 const char *name = h->root.root.string;
677 asection *s;
678 char *tmp_name;
679 struct elf_link_hash_entry *myh;
680 struct bfd_link_hash_entry *bh;
681 struct elf32_arm_link_hash_table *hash_table;
682 char bind;
683 bfd_vma val;
684
685 hash_table = elf32_arm_hash_table (link_info);
686
687 BFD_ASSERT (hash_table != NULL);
688 BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
689
690 s = bfd_get_section_by_name
691 (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
692
693 BFD_ASSERT (s != NULL);
694
695 tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
696 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
697
698 BFD_ASSERT (tmp_name);
699
700 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
701
702 myh = elf_link_hash_lookup
703 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
704
705 if (myh != NULL)
706 {
707 /* We've already seen this guy. */
708 free (tmp_name);
709 return;
710 }
711
712 bh = NULL;
713 val = hash_table->thumb_glue_size + 1;
714 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
715 tmp_name, BSF_GLOBAL, s, val,
716 NULL, TRUE, FALSE, &bh);
717
718 /* If we mark it 'Thumb', the disassembler will do a better job. */
719 myh = (struct elf_link_hash_entry *) bh;
720 bind = ELF_ST_BIND (myh->type);
721 myh->type = ELF_ST_INFO (bind, STT_ARM_TFUNC);
722
723 free (tmp_name);
724
725 #define CHANGE_TO_ARM "__%s_change_to_arm"
726 #define BACK_FROM_ARM "__%s_back_from_arm"
727
728 /* Allocate another symbol to mark where we switch to Arm mode. */
729 tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
730 + strlen (CHANGE_TO_ARM) + 1);
731
732 BFD_ASSERT (tmp_name);
733
734 sprintf (tmp_name, CHANGE_TO_ARM, name);
735
736 bh = NULL;
737 val = hash_table->thumb_glue_size + 4,
738 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
739 tmp_name, BSF_LOCAL, s, val,
740 NULL, TRUE, FALSE, &bh);
741
742 free (tmp_name);
743
744 hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
745
746 return;
747 }
748
749 /* Add the glue sections to ABFD. This function is called from the
750 linker scripts in ld/emultempl/{armelf}.em. */
751
752 bfd_boolean
753 bfd_elf32_arm_add_glue_sections_to_bfd (abfd, info)
754 bfd *abfd;
755 struct bfd_link_info *info;
756 {
757 flagword flags;
758 asection *sec;
759
760 /* If we are only performing a partial
761 link do not bother adding the glue. */
762 if (info->relocatable)
763 return TRUE;
764
765 sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
766
767 if (sec == NULL)
768 {
769 /* Note: we do not include the flag SEC_LINKER_CREATED, as this
770 will prevent elf_link_input_bfd() from processing the contents
771 of this section. */
772 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
773
774 sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
775
776 if (sec == NULL
777 || !bfd_set_section_flags (abfd, sec, flags)
778 || !bfd_set_section_alignment (abfd, sec, 2))
779 return FALSE;
780
781 /* Set the gc mark to prevent the section from being removed by garbage
782 collection, despite the fact that no relocs refer to this section. */
783 sec->gc_mark = 1;
784 }
785
786 sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
787
788 if (sec == NULL)
789 {
790 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
791
792 sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
793
794 if (sec == NULL
795 || !bfd_set_section_flags (abfd, sec, flags)
796 || !bfd_set_section_alignment (abfd, sec, 2))
797 return FALSE;
798
799 sec->gc_mark = 1;
800 }
801
802 return TRUE;
803 }
804
805 /* Select a BFD to be used to hold the sections used by the glue code.
806 This function is called from the linker scripts in ld/emultempl/
807 {armelf/pe}.em */
808
809 bfd_boolean
810 bfd_elf32_arm_get_bfd_for_interworking (abfd, info)
811 bfd *abfd;
812 struct bfd_link_info *info;
813 {
814 struct elf32_arm_link_hash_table *globals;
815
816 /* If we are only performing a partial link
817 do not bother getting a bfd to hold the glue. */
818 if (info->relocatable)
819 return TRUE;
820
821 globals = elf32_arm_hash_table (info);
822
823 BFD_ASSERT (globals != NULL);
824
825 if (globals->bfd_of_glue_owner != NULL)
826 return TRUE;
827
828 /* Save the bfd for later use. */
829 globals->bfd_of_glue_owner = abfd;
830
831 return TRUE;
832 }
833
834 bfd_boolean
835 bfd_elf32_arm_process_before_allocation (abfd, link_info,
836 no_pipeline_knowledge,
837 byteswap_code)
838 bfd *abfd;
839 struct bfd_link_info *link_info;
840 int no_pipeline_knowledge;
841 int byteswap_code;
842 {
843 Elf_Internal_Shdr *symtab_hdr;
844 Elf_Internal_Rela *internal_relocs = NULL;
845 Elf_Internal_Rela *irel, *irelend;
846 bfd_byte *contents = NULL;
847
848 asection *sec;
849 struct elf32_arm_link_hash_table *globals;
850
851 /* If we are only performing a partial link do not bother
852 to construct any glue. */
853 if (link_info->relocatable)
854 return TRUE;
855
856 /* Here we have a bfd that is to be included on the link. We have a hook
857 to do reloc rummaging, before section sizes are nailed down. */
858 globals = elf32_arm_hash_table (link_info);
859
860 BFD_ASSERT (globals != NULL);
861 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
862
863 globals->no_pipeline_knowledge = no_pipeline_knowledge;
864 if (byteswap_code && !bfd_big_endian (abfd))
865 {
866 _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
867 abfd);
868 return FALSE;
869 }
870 globals->byteswap_code = byteswap_code;
871
872 /* Rummage around all the relocs and map the glue vectors. */
873 sec = abfd->sections;
874
875 if (sec == NULL)
876 return TRUE;
877
878 for (; sec != NULL; sec = sec->next)
879 {
880 if (sec->reloc_count == 0)
881 continue;
882
883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
884
885 /* Load the relocs. */
886 internal_relocs
887 = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
888 (Elf_Internal_Rela *) NULL, FALSE);
889
890 if (internal_relocs == NULL)
891 goto error_return;
892
893 irelend = internal_relocs + sec->reloc_count;
894 for (irel = internal_relocs; irel < irelend; irel++)
895 {
896 long r_type;
897 unsigned long r_index;
898
899 struct elf_link_hash_entry *h;
900
901 r_type = ELF32_R_TYPE (irel->r_info);
902 r_index = ELF32_R_SYM (irel->r_info);
903
904 /* These are the only relocation types we care about. */
905 if ( r_type != R_ARM_PC24
906 && r_type != R_ARM_THM_PC22)
907 continue;
908
909 /* Get the section contents if we haven't done so already. */
910 if (contents == NULL)
911 {
912 /* Get cached copy if it exists. */
913 if (elf_section_data (sec)->this_hdr.contents != NULL)
914 contents = elf_section_data (sec)->this_hdr.contents;
915 else
916 {
917 /* Go get them off disk. */
918 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
919 goto error_return;
920 }
921 }
922
923 /* If the relocation is not against a symbol it cannot concern us. */
924 h = NULL;
925
926 /* We don't care about local symbols. */
927 if (r_index < symtab_hdr->sh_info)
928 continue;
929
930 /* This is an external symbol. */
931 r_index -= symtab_hdr->sh_info;
932 h = (struct elf_link_hash_entry *)
933 elf_sym_hashes (abfd)[r_index];
934
935 /* If the relocation is against a static symbol it must be within
936 the current section and so cannot be a cross ARM/Thumb relocation. */
937 if (h == NULL)
938 continue;
939
940 switch (r_type)
941 {
942 case R_ARM_PC24:
943 /* This one is a call from arm code. We need to look up
944 the target of the call. If it is a thumb target, we
945 insert glue. */
946 if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
947 record_arm_to_thumb_glue (link_info, h);
948 break;
949
950 case R_ARM_THM_PC22:
951 /* This one is a call from thumb code. We look
952 up the target of the call. If it is not a thumb
953 target, we insert glue. */
954 if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
955 record_thumb_to_arm_glue (link_info, h);
956 break;
957
958 default:
959 break;
960 }
961 }
962
963 if (contents != NULL
964 && elf_section_data (sec)->this_hdr.contents != contents)
965 free (contents);
966 contents = NULL;
967
968 if (internal_relocs != NULL
969 && elf_section_data (sec)->relocs != internal_relocs)
970 free (internal_relocs);
971 internal_relocs = NULL;
972 }
973
974 return TRUE;
975
976 error_return:
977 if (contents != NULL
978 && elf_section_data (sec)->this_hdr.contents != contents)
979 free (contents);
980 if (internal_relocs != NULL
981 && elf_section_data (sec)->relocs != internal_relocs)
982 free (internal_relocs);
983
984 return FALSE;
985 }
986 #endif
987
988 /* The thumb form of a long branch is a bit finicky, because the offset
989 encoding is split over two fields, each in it's own instruction. They
990 can occur in any order. So given a thumb form of long branch, and an
991 offset, insert the offset into the thumb branch and return finished
992 instruction.
993
994 It takes two thumb instructions to encode the target address. Each has
995 11 bits to invest. The upper 11 bits are stored in one (identified by
996 H-0.. see below), the lower 11 bits are stored in the other (identified
997 by H-1).
998
999 Combine together and shifted left by 1 (it's a half word address) and
1000 there you have it.
1001
1002 Op: 1111 = F,
1003 H-0, upper address-0 = 000
1004 Op: 1111 = F,
1005 H-1, lower address-0 = 800
1006
1007 They can be ordered either way, but the arm tools I've seen always put
1008 the lower one first. It probably doesn't matter. krk@cygnus.com
1009
1010 XXX: Actually the order does matter. The second instruction (H-1)
1011 moves the computed address into the PC, so it must be the second one
1012 in the sequence. The problem, however is that whilst little endian code
1013 stores the instructions in HI then LOW order, big endian code does the
1014 reverse. nickc@cygnus.com. */
1015
1016 #define LOW_HI_ORDER 0xF800F000
1017 #define HI_LOW_ORDER 0xF000F800
1018
1019 static insn32
1020 insert_thumb_branch (br_insn, rel_off)
1021 insn32 br_insn;
1022 int rel_off;
1023 {
1024 unsigned int low_bits;
1025 unsigned int high_bits;
1026
1027 BFD_ASSERT ((rel_off & 1) != 1);
1028
1029 rel_off >>= 1; /* Half word aligned address. */
1030 low_bits = rel_off & 0x000007FF; /* The bottom 11 bits. */
1031 high_bits = (rel_off >> 11) & 0x000007FF; /* The top 11 bits. */
1032
1033 if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
1034 br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
1035 else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
1036 br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
1037 else
1038 /* FIXME: abort is probably not the right call. krk@cygnus.com */
1039 abort (); /* error - not a valid branch instruction form. */
1040
1041 return br_insn;
1042 }
1043
1044 /* Thumb code calling an ARM function. */
1045
1046 static int
1047 elf32_thumb_to_arm_stub (info, name, input_bfd, output_bfd, input_section,
1048 hit_data, sym_sec, offset, addend, val)
1049 struct bfd_link_info * info;
1050 const char * name;
1051 bfd * input_bfd;
1052 bfd * output_bfd;
1053 asection * input_section;
1054 bfd_byte * hit_data;
1055 asection * sym_sec;
1056 bfd_vma offset;
1057 bfd_signed_vma addend;
1058 bfd_vma val;
1059 {
1060 asection * s = 0;
1061 bfd_vma my_offset;
1062 unsigned long int tmp;
1063 long int ret_offset;
1064 struct elf_link_hash_entry * myh;
1065 struct elf32_arm_link_hash_table * globals;
1066
1067 myh = find_thumb_glue (info, name, input_bfd);
1068 if (myh == NULL)
1069 return FALSE;
1070
1071 globals = elf32_arm_hash_table (info);
1072
1073 BFD_ASSERT (globals != NULL);
1074 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1075
1076 my_offset = myh->root.u.def.value;
1077
1078 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1079 THUMB2ARM_GLUE_SECTION_NAME);
1080
1081 BFD_ASSERT (s != NULL);
1082 BFD_ASSERT (s->contents != NULL);
1083 BFD_ASSERT (s->output_section != NULL);
1084
1085 if ((my_offset & 0x01) == 0x01)
1086 {
1087 if (sym_sec != NULL
1088 && sym_sec->owner != NULL
1089 && !INTERWORK_FLAG (sym_sec->owner))
1090 {
1091 (*_bfd_error_handler)
1092 (_("%B(%s): warning: interworking not enabled.\n"
1093 " first occurrence: %B: thumb call to arm"),
1094 sym_sec->owner, input_bfd, name);
1095
1096 return FALSE;
1097 }
1098
1099 --my_offset;
1100 myh->root.u.def.value = my_offset;
1101
1102 bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
1103 s->contents + my_offset);
1104
1105 bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
1106 s->contents + my_offset + 2);
1107
1108 ret_offset =
1109 /* Address of destination of the stub. */
1110 ((bfd_signed_vma) val)
1111 - ((bfd_signed_vma)
1112 /* Offset from the start of the current section to the start of the stubs. */
1113 (s->output_offset
1114 /* Offset of the start of this stub from the start of the stubs. */
1115 + my_offset
1116 /* Address of the start of the current section. */
1117 + s->output_section->vma)
1118 /* The branch instruction is 4 bytes into the stub. */
1119 + 4
1120 /* ARM branches work from the pc of the instruction + 8. */
1121 + 8);
1122
1123 bfd_put_32 (output_bfd,
1124 (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
1125 s->contents + my_offset + 4);
1126 }
1127
1128 BFD_ASSERT (my_offset <= globals->thumb_glue_size);
1129
1130 /* Now go back and fix up the original BL insn to point to here. */
1131 ret_offset =
1132 /* Address of where the stub is located. */
1133 (s->output_section->vma + s->output_offset + my_offset)
1134 /* Address of where the BL is located. */
1135 - (input_section->output_section->vma + input_section->output_offset + offset)
1136 /* Addend in the relocation. */
1137 - addend
1138 /* Biassing for PC-relative addressing. */
1139 - 8;
1140
1141 tmp = bfd_get_32 (input_bfd, hit_data
1142 - input_section->vma);
1143
1144 bfd_put_32 (output_bfd,
1145 (bfd_vma) insert_thumb_branch (tmp, ret_offset),
1146 hit_data - input_section->vma);
1147
1148 return TRUE;
1149 }
1150
1151 /* Arm code calling a Thumb function. */
1152
1153 static int
1154 elf32_arm_to_thumb_stub (info, name, input_bfd, output_bfd, input_section,
1155 hit_data, sym_sec, offset, addend, val)
1156 struct bfd_link_info * info;
1157 const char * name;
1158 bfd * input_bfd;
1159 bfd * output_bfd;
1160 asection * input_section;
1161 bfd_byte * hit_data;
1162 asection * sym_sec;
1163 bfd_vma offset;
1164 bfd_signed_vma addend;
1165 bfd_vma val;
1166 {
1167 unsigned long int tmp;
1168 bfd_vma my_offset;
1169 asection * s;
1170 long int ret_offset;
1171 struct elf_link_hash_entry * myh;
1172 struct elf32_arm_link_hash_table * globals;
1173
1174 myh = find_arm_glue (info, name, input_bfd);
1175 if (myh == NULL)
1176 return FALSE;
1177
1178 globals = elf32_arm_hash_table (info);
1179
1180 BFD_ASSERT (globals != NULL);
1181 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1182
1183 my_offset = myh->root.u.def.value;
1184 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1185 ARM2THUMB_GLUE_SECTION_NAME);
1186 BFD_ASSERT (s != NULL);
1187 BFD_ASSERT (s->contents != NULL);
1188 BFD_ASSERT (s->output_section != NULL);
1189
1190 if ((my_offset & 0x01) == 0x01)
1191 {
1192 if (sym_sec != NULL
1193 && sym_sec->owner != NULL
1194 && !INTERWORK_FLAG (sym_sec->owner))
1195 {
1196 (*_bfd_error_handler)
1197 (_("%B(%s): warning: interworking not enabled.\n"
1198 " first occurrence: %B: arm call to thumb"),
1199 sym_sec->owner, input_bfd, name);
1200 }
1201
1202 --my_offset;
1203 myh->root.u.def.value = my_offset;
1204
1205 bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
1206 s->contents + my_offset);
1207
1208 bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
1209 s->contents + my_offset + 4);
1210
1211 /* It's a thumb address. Add the low order bit. */
1212 bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
1213 s->contents + my_offset + 8);
1214 }
1215
1216 BFD_ASSERT (my_offset <= globals->arm_glue_size);
1217
1218 tmp = bfd_get_32 (input_bfd, hit_data);
1219 tmp = tmp & 0xFF000000;
1220
1221 /* Somehow these are both 4 too far, so subtract 8. */
1222 ret_offset = (s->output_offset
1223 + my_offset
1224 + s->output_section->vma
1225 - (input_section->output_offset
1226 + input_section->output_section->vma
1227 + offset + addend)
1228 - 8);
1229
1230 tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
1231
1232 bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
1233
1234 return TRUE;
1235 }
1236
1237 /* Perform a relocation as part of a final link. */
1238
1239 static bfd_reloc_status_type
1240 elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
1241 input_section, contents, rel, value,
1242 info, sym_sec, sym_name, sym_flags, h)
1243 reloc_howto_type * howto;
1244 bfd * input_bfd;
1245 bfd * output_bfd;
1246 asection * input_section;
1247 bfd_byte * contents;
1248 Elf_Internal_Rela * rel;
1249 bfd_vma value;
1250 struct bfd_link_info * info;
1251 asection * sym_sec;
1252 const char * sym_name;
1253 int sym_flags;
1254 struct elf_link_hash_entry * h;
1255 {
1256 unsigned long r_type = howto->type;
1257 unsigned long r_symndx;
1258 bfd_byte * hit_data = contents + rel->r_offset;
1259 bfd * dynobj = NULL;
1260 Elf_Internal_Shdr * symtab_hdr;
1261 struct elf_link_hash_entry ** sym_hashes;
1262 bfd_vma * local_got_offsets;
1263 asection * sgot = NULL;
1264 asection * splt = NULL;
1265 asection * sreloc = NULL;
1266 bfd_vma addend;
1267 bfd_signed_vma signed_addend;
1268 struct elf32_arm_link_hash_table * globals;
1269
1270 /* If the start address has been set, then set the EF_ARM_HASENTRY
1271 flag. Setting this more than once is redundant, but the cost is
1272 not too high, and it keeps the code simple.
1273
1274 The test is done here, rather than somewhere else, because the
1275 start address is only set just before the final link commences.
1276
1277 Note - if the user deliberately sets a start address of 0, the
1278 flag will not be set. */
1279 if (bfd_get_start_address (output_bfd) != 0)
1280 elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
1281
1282 globals = elf32_arm_hash_table (info);
1283
1284 dynobj = elf_hash_table (info)->dynobj;
1285 if (dynobj)
1286 {
1287 sgot = bfd_get_section_by_name (dynobj, ".got");
1288 splt = bfd_get_section_by_name (dynobj, ".plt");
1289 }
1290 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1291 sym_hashes = elf_sym_hashes (input_bfd);
1292 local_got_offsets = elf_local_got_offsets (input_bfd);
1293 r_symndx = ELF32_R_SYM (rel->r_info);
1294
1295 #if USE_REL
1296 addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
1297
1298 if (addend & ((howto->src_mask + 1) >> 1))
1299 {
1300 signed_addend = -1;
1301 signed_addend &= ~ howto->src_mask;
1302 signed_addend |= addend;
1303 }
1304 else
1305 signed_addend = addend;
1306 #else
1307 addend = signed_addend = rel->r_addend;
1308 #endif
1309
1310 switch (r_type)
1311 {
1312 case R_ARM_NONE:
1313 return bfd_reloc_ok;
1314
1315 case R_ARM_PC24:
1316 case R_ARM_ABS32:
1317 case R_ARM_REL32:
1318 #ifndef OLD_ARM_ABI
1319 case R_ARM_XPC25:
1320 #endif
1321 case R_ARM_PLT32:
1322 /* r_symndx will be zero only for relocs against symbols
1323 from removed linkonce sections, or sections discarded by
1324 a linker script. */
1325 if (r_symndx == 0)
1326 return bfd_reloc_ok;
1327
1328 /* Handle relocations which should use the PLT entry. ABS32/REL32
1329 will use the symbol's value, which may point to a PLT entry, but we
1330 don't need to handle that here. If we created a PLT entry, all
1331 branches in this object should go to it. */
1332 if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
1333 && h != NULL
1334 && splt != NULL
1335 && h->plt.offset != (bfd_vma) -1)
1336 {
1337 /* If we've created a .plt section, and assigned a PLT entry to
1338 this function, it should not be known to bind locally. If
1339 it were, we would have cleared the PLT entry. */
1340 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1341
1342 value = (splt->output_section->vma
1343 + splt->output_offset
1344 + h->plt.offset);
1345 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1346 contents, rel->r_offset, value,
1347 (bfd_vma) 0);
1348 }
1349
1350 /* When generating a shared object, these relocations are copied
1351 into the output file to be resolved at run time. */
1352 if (info->shared
1353 && (input_section->flags & SEC_ALLOC)
1354 && (r_type != R_ARM_REL32
1355 || !SYMBOL_CALLS_LOCAL (info, h))
1356 && (h == NULL
1357 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1358 || h->root.type != bfd_link_hash_undefweak)
1359 && r_type != R_ARM_PC24
1360 && r_type != R_ARM_PLT32)
1361 {
1362 Elf_Internal_Rela outrel;
1363 bfd_byte *loc;
1364 bfd_boolean skip, relocate;
1365
1366 if (sreloc == NULL)
1367 {
1368 const char * name;
1369
1370 name = (bfd_elf_string_from_elf_section
1371 (input_bfd,
1372 elf_elfheader (input_bfd)->e_shstrndx,
1373 elf_section_data (input_section)->rel_hdr.sh_name));
1374 if (name == NULL)
1375 return bfd_reloc_notsupported;
1376
1377 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
1378 && strcmp (bfd_get_section_name (input_bfd,
1379 input_section),
1380 name + 4) == 0);
1381
1382 sreloc = bfd_get_section_by_name (dynobj, name);
1383 BFD_ASSERT (sreloc != NULL);
1384 }
1385
1386 skip = FALSE;
1387 relocate = FALSE;
1388
1389 outrel.r_offset =
1390 _bfd_elf_section_offset (output_bfd, info, input_section,
1391 rel->r_offset);
1392 if (outrel.r_offset == (bfd_vma) -1)
1393 skip = TRUE;
1394 else if (outrel.r_offset == (bfd_vma) -2)
1395 skip = TRUE, relocate = TRUE;
1396 outrel.r_offset += (input_section->output_section->vma
1397 + input_section->output_offset);
1398
1399 if (skip)
1400 memset (&outrel, 0, sizeof outrel);
1401 else if (h != NULL
1402 && h->dynindx != -1
1403 && (!info->shared
1404 || !info->symbolic
1405 || (h->elf_link_hash_flags
1406 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1407 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1408 else
1409 {
1410 /* This symbol is local, or marked to become local. */
1411 relocate = TRUE;
1412 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1413 }
1414
1415 loc = sreloc->contents;
1416 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
1417 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1418
1419 /* If this reloc is against an external symbol, we do not want to
1420 fiddle with the addend. Otherwise, we need to include the symbol
1421 value so that it becomes an addend for the dynamic reloc. */
1422 if (! relocate)
1423 return bfd_reloc_ok;
1424
1425 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1426 contents, rel->r_offset, value,
1427 (bfd_vma) 0);
1428 }
1429 else switch (r_type)
1430 {
1431 #ifndef OLD_ARM_ABI
1432 case R_ARM_XPC25: /* Arm BLX instruction. */
1433 #endif
1434 case R_ARM_PC24: /* Arm B/BL instruction */
1435 case R_ARM_PLT32:
1436 #ifndef OLD_ARM_ABI
1437 if (r_type == R_ARM_XPC25)
1438 {
1439 /* Check for Arm calling Arm function. */
1440 /* FIXME: Should we translate the instruction into a BL
1441 instruction instead ? */
1442 if (sym_flags != STT_ARM_TFUNC)
1443 (*_bfd_error_handler)
1444 (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
1445 input_bfd,
1446 h ? h->root.root.string : "(local)");
1447 }
1448 else
1449 #endif
1450 {
1451 /* Check for Arm calling Thumb function. */
1452 if (sym_flags == STT_ARM_TFUNC)
1453 {
1454 elf32_arm_to_thumb_stub (info, sym_name, input_bfd, output_bfd,
1455 input_section, hit_data, sym_sec, rel->r_offset,
1456 signed_addend, value);
1457 return bfd_reloc_ok;
1458 }
1459 }
1460
1461 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1462 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0)
1463 {
1464 /* The old way of doing things. Trearing the addend as a
1465 byte sized field and adding in the pipeline offset. */
1466 value -= (input_section->output_section->vma
1467 + input_section->output_offset);
1468 value -= rel->r_offset;
1469 value += addend;
1470
1471 if (! globals->no_pipeline_knowledge)
1472 value -= 8;
1473 }
1474 else
1475 {
1476 /* The ARM ELF ABI says that this reloc is computed as: S - P + A
1477 where:
1478 S is the address of the symbol in the relocation.
1479 P is address of the instruction being relocated.
1480 A is the addend (extracted from the instruction) in bytes.
1481
1482 S is held in 'value'.
1483 P is the base address of the section containing the instruction
1484 plus the offset of the reloc into that section, ie:
1485 (input_section->output_section->vma +
1486 input_section->output_offset +
1487 rel->r_offset).
1488 A is the addend, converted into bytes, ie:
1489 (signed_addend * 4)
1490
1491 Note: None of these operations have knowledge of the pipeline
1492 size of the processor, thus it is up to the assembler to encode
1493 this information into the addend. */
1494 value -= (input_section->output_section->vma
1495 + input_section->output_offset);
1496 value -= rel->r_offset;
1497 value += (signed_addend << howto->size);
1498
1499 /* Previous versions of this code also used to add in the pipeline
1500 offset here. This is wrong because the linker is not supposed
1501 to know about such things, and one day it might change. In order
1502 to support old binaries that need the old behaviour however, so
1503 we attempt to detect which ABI was used to create the reloc. */
1504 if (! globals->no_pipeline_knowledge)
1505 {
1506 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
1507
1508 i_ehdrp = elf_elfheader (input_bfd);
1509
1510 if (i_ehdrp->e_ident[EI_OSABI] == 0)
1511 value -= 8;
1512 }
1513 }
1514
1515 signed_addend = value;
1516 signed_addend >>= howto->rightshift;
1517
1518 /* It is not an error for an undefined weak reference to be
1519 out of range. Any program that branches to such a symbol
1520 is going to crash anyway, so there is no point worrying
1521 about getting the destination exactly right. */
1522 if (! h || h->root.type != bfd_link_hash_undefweak)
1523 {
1524 /* Perform a signed range check. */
1525 if ( signed_addend > ((bfd_signed_vma) (howto->dst_mask >> 1))
1526 || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
1527 return bfd_reloc_overflow;
1528 }
1529
1530 #ifndef OLD_ARM_ABI
1531 /* If necessary set the H bit in the BLX instruction. */
1532 if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
1533 value = (signed_addend & howto->dst_mask)
1534 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
1535 | (1 << 24);
1536 else
1537 #endif
1538 value = (signed_addend & howto->dst_mask)
1539 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
1540 break;
1541
1542 case R_ARM_ABS32:
1543 value += addend;
1544 if (sym_flags == STT_ARM_TFUNC)
1545 value |= 1;
1546 break;
1547
1548 case R_ARM_REL32:
1549 value -= (input_section->output_section->vma
1550 + input_section->output_offset + rel->r_offset);
1551 value += addend;
1552 break;
1553 }
1554
1555 bfd_put_32 (input_bfd, value, hit_data);
1556 return bfd_reloc_ok;
1557
1558 case R_ARM_ABS8:
1559 value += addend;
1560 if ((long) value > 0x7f || (long) value < -0x80)
1561 return bfd_reloc_overflow;
1562
1563 bfd_put_8 (input_bfd, value, hit_data);
1564 return bfd_reloc_ok;
1565
1566 case R_ARM_ABS16:
1567 value += addend;
1568
1569 if ((long) value > 0x7fff || (long) value < -0x8000)
1570 return bfd_reloc_overflow;
1571
1572 bfd_put_16 (input_bfd, value, hit_data);
1573 return bfd_reloc_ok;
1574
1575 case R_ARM_ABS12:
1576 /* Support ldr and str instruction for the arm */
1577 /* Also thumb b (unconditional branch). ??? Really? */
1578 value += addend;
1579
1580 if ((long) value > 0x7ff || (long) value < -0x800)
1581 return bfd_reloc_overflow;
1582
1583 value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
1584 bfd_put_32 (input_bfd, value, hit_data);
1585 return bfd_reloc_ok;
1586
1587 case R_ARM_THM_ABS5:
1588 /* Support ldr and str instructions for the thumb. */
1589 #if USE_REL
1590 /* Need to refetch addend. */
1591 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1592 /* ??? Need to determine shift amount from operand size. */
1593 addend >>= howto->rightshift;
1594 #endif
1595 value += addend;
1596
1597 /* ??? Isn't value unsigned? */
1598 if ((long) value > 0x1f || (long) value < -0x10)
1599 return bfd_reloc_overflow;
1600
1601 /* ??? Value needs to be properly shifted into place first. */
1602 value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
1603 bfd_put_16 (input_bfd, value, hit_data);
1604 return bfd_reloc_ok;
1605
1606 #ifndef OLD_ARM_ABI
1607 case R_ARM_THM_XPC22:
1608 #endif
1609 case R_ARM_THM_PC22:
1610 /* Thumb BL (branch long instruction). */
1611 {
1612 bfd_vma relocation;
1613 bfd_boolean overflow = FALSE;
1614 bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
1615 bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
1616 bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
1617 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1618 bfd_vma check;
1619 bfd_signed_vma signed_check;
1620
1621 #if USE_REL
1622 /* Need to refetch the addend and squish the two 11 bit pieces
1623 together. */
1624 {
1625 bfd_vma upper = upper_insn & 0x7ff;
1626 bfd_vma lower = lower_insn & 0x7ff;
1627 upper = (upper ^ 0x400) - 0x400; /* Sign extend. */
1628 addend = (upper << 12) | (lower << 1);
1629 signed_addend = addend;
1630 }
1631 #endif
1632 #ifndef OLD_ARM_ABI
1633 if (r_type == R_ARM_THM_XPC22)
1634 {
1635 /* Check for Thumb to Thumb call. */
1636 /* FIXME: Should we translate the instruction into a BL
1637 instruction instead ? */
1638 if (sym_flags == STT_ARM_TFUNC)
1639 (*_bfd_error_handler)
1640 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
1641 input_bfd,
1642 h ? h->root.root.string : "(local)");
1643 }
1644 else
1645 #endif
1646 {
1647 /* If it is not a call to Thumb, assume call to Arm.
1648 If it is a call relative to a section name, then it is not a
1649 function call at all, but rather a long jump. */
1650 if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION)
1651 {
1652 if (elf32_thumb_to_arm_stub
1653 (info, sym_name, input_bfd, output_bfd, input_section,
1654 hit_data, sym_sec, rel->r_offset, signed_addend, value))
1655 return bfd_reloc_ok;
1656 else
1657 return bfd_reloc_dangerous;
1658 }
1659 }
1660
1661 relocation = value + signed_addend;
1662
1663 relocation -= (input_section->output_section->vma
1664 + input_section->output_offset
1665 + rel->r_offset);
1666
1667 if (! globals->no_pipeline_knowledge)
1668 {
1669 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form. */
1670
1671 i_ehdrp = elf_elfheader (input_bfd);
1672
1673 /* Previous versions of this code also used to add in the pipline
1674 offset here. This is wrong because the linker is not supposed
1675 to know about such things, and one day it might change. In order
1676 to support old binaries that need the old behaviour however, so
1677 we attempt to detect which ABI was used to create the reloc. */
1678 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1679 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0
1680 || i_ehdrp->e_ident[EI_OSABI] == 0)
1681 relocation += 4;
1682 }
1683
1684 check = relocation >> howto->rightshift;
1685
1686 /* If this is a signed value, the rightshift just dropped
1687 leading 1 bits (assuming twos complement). */
1688 if ((bfd_signed_vma) relocation >= 0)
1689 signed_check = check;
1690 else
1691 signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
1692
1693 /* Assumes two's complement. */
1694 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1695 overflow = TRUE;
1696
1697 #ifndef OLD_ARM_ABI
1698 if (r_type == R_ARM_THM_XPC22
1699 && ((lower_insn & 0x1800) == 0x0800))
1700 /* For a BLX instruction, make sure that the relocation is rounded up
1701 to a word boundary. This follows the semantics of the instruction
1702 which specifies that bit 1 of the target address will come from bit
1703 1 of the base address. */
1704 relocation = (relocation + 2) & ~ 3;
1705 #endif
1706 /* Put RELOCATION back into the insn. */
1707 upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
1708 lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
1709
1710 /* Put the relocated value back in the object file: */
1711 bfd_put_16 (input_bfd, upper_insn, hit_data);
1712 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
1713
1714 return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
1715 }
1716 break;
1717
1718 case R_ARM_THM_PC11:
1719 /* Thumb B (branch) instruction). */
1720 {
1721 bfd_signed_vma relocation;
1722 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1723 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1724 bfd_signed_vma signed_check;
1725
1726 #if USE_REL
1727 /* Need to refetch addend. */
1728 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1729 if (addend & ((howto->src_mask + 1) >> 1))
1730 {
1731 signed_addend = -1;
1732 signed_addend &= ~ howto->src_mask;
1733 signed_addend |= addend;
1734 }
1735 else
1736 signed_addend = addend;
1737 /* The value in the insn has been right shifted. We need to
1738 undo this, so that we can perform the address calculation
1739 in terms of bytes. */
1740 signed_addend <<= howto->rightshift;
1741 #endif
1742 relocation = value + signed_addend;
1743
1744 relocation -= (input_section->output_section->vma
1745 + input_section->output_offset
1746 + rel->r_offset);
1747
1748 relocation >>= howto->rightshift;
1749 signed_check = relocation;
1750 relocation &= howto->dst_mask;
1751 relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
1752
1753 bfd_put_16 (input_bfd, relocation, hit_data);
1754
1755 /* Assumes two's complement. */
1756 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1757 return bfd_reloc_overflow;
1758
1759 return bfd_reloc_ok;
1760 }
1761
1762 #ifndef OLD_ARM_ABI
1763 case R_ARM_ALU_PCREL7_0:
1764 case R_ARM_ALU_PCREL15_8:
1765 case R_ARM_ALU_PCREL23_15:
1766 {
1767 bfd_vma insn;
1768 bfd_vma relocation;
1769
1770 insn = bfd_get_32 (input_bfd, hit_data);
1771 #if USE_REL
1772 /* Extract the addend. */
1773 addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
1774 signed_addend = addend;
1775 #endif
1776 relocation = value + signed_addend;
1777
1778 relocation -= (input_section->output_section->vma
1779 + input_section->output_offset
1780 + rel->r_offset);
1781 insn = (insn & ~0xfff)
1782 | ((howto->bitpos << 7) & 0xf00)
1783 | ((relocation >> howto->bitpos) & 0xff);
1784 bfd_put_32 (input_bfd, value, hit_data);
1785 }
1786 return bfd_reloc_ok;
1787 #endif
1788
1789 case R_ARM_GNU_VTINHERIT:
1790 case R_ARM_GNU_VTENTRY:
1791 return bfd_reloc_ok;
1792
1793 case R_ARM_COPY:
1794 return bfd_reloc_notsupported;
1795
1796 case R_ARM_GLOB_DAT:
1797 return bfd_reloc_notsupported;
1798
1799 case R_ARM_JUMP_SLOT:
1800 return bfd_reloc_notsupported;
1801
1802 case R_ARM_RELATIVE:
1803 return bfd_reloc_notsupported;
1804
1805 case R_ARM_GOTOFF:
1806 /* Relocation is relative to the start of the
1807 global offset table. */
1808
1809 BFD_ASSERT (sgot != NULL);
1810 if (sgot == NULL)
1811 return bfd_reloc_notsupported;
1812
1813 /* If we are addressing a Thumb function, we need to adjust the
1814 address by one, so that attempts to call the function pointer will
1815 correctly interpret it as Thumb code. */
1816 if (sym_flags == STT_ARM_TFUNC)
1817 value += 1;
1818
1819 /* Note that sgot->output_offset is not involved in this
1820 calculation. We always want the start of .got. If we
1821 define _GLOBAL_OFFSET_TABLE in a different way, as is
1822 permitted by the ABI, we might have to change this
1823 calculation. */
1824 value -= sgot->output_section->vma;
1825 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1826 contents, rel->r_offset, value,
1827 (bfd_vma) 0);
1828
1829 case R_ARM_GOTPC:
1830 /* Use global offset table as symbol value. */
1831 BFD_ASSERT (sgot != NULL);
1832
1833 if (sgot == NULL)
1834 return bfd_reloc_notsupported;
1835
1836 value = sgot->output_section->vma;
1837 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1838 contents, rel->r_offset, value,
1839 (bfd_vma) 0);
1840
1841 case R_ARM_GOT32:
1842 /* Relocation is to the entry for this symbol in the
1843 global offset table. */
1844 if (sgot == NULL)
1845 return bfd_reloc_notsupported;
1846
1847 if (h != NULL)
1848 {
1849 bfd_vma off;
1850 bfd_boolean dyn;
1851
1852 off = h->got.offset;
1853 BFD_ASSERT (off != (bfd_vma) -1);
1854 dyn = globals->root.dynamic_sections_created;
1855
1856 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1857 || (info->shared
1858 && SYMBOL_REFERENCES_LOCAL (info, h))
1859 || (ELF_ST_VISIBILITY (h->other)
1860 && h->root.type == bfd_link_hash_undefweak))
1861 {
1862 /* This is actually a static link, or it is a -Bsymbolic link
1863 and the symbol is defined locally. We must initialize this
1864 entry in the global offset table. Since the offset must
1865 always be a multiple of 4, we use the least significant bit
1866 to record whether we have initialized it already.
1867
1868 When doing a dynamic link, we create a .rel.got relocation
1869 entry to initialize the value. This is done in the
1870 finish_dynamic_symbol routine. */
1871 if ((off & 1) != 0)
1872 off &= ~1;
1873 else
1874 {
1875 /* If we are addressing a Thumb function, we need to
1876 adjust the address by one, so that attempts to
1877 call the function pointer will correctly
1878 interpret it as Thumb code. */
1879 if (sym_flags == STT_ARM_TFUNC)
1880 value |= 1;
1881
1882 bfd_put_32 (output_bfd, value, sgot->contents + off);
1883 h->got.offset |= 1;
1884 }
1885 }
1886
1887 value = sgot->output_offset + off;
1888 }
1889 else
1890 {
1891 bfd_vma off;
1892
1893 BFD_ASSERT (local_got_offsets != NULL &&
1894 local_got_offsets[r_symndx] != (bfd_vma) -1);
1895
1896 off = local_got_offsets[r_symndx];
1897
1898 /* The offset must always be a multiple of 4. We use the
1899 least significant bit to record whether we have already
1900 generated the necessary reloc. */
1901 if ((off & 1) != 0)
1902 off &= ~1;
1903 else
1904 {
1905 bfd_put_32 (output_bfd, value, sgot->contents + off);
1906
1907 if (info->shared)
1908 {
1909 asection * srelgot;
1910 Elf_Internal_Rela outrel;
1911 bfd_byte *loc;
1912
1913 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1914 BFD_ASSERT (srelgot != NULL);
1915
1916 outrel.r_offset = (sgot->output_section->vma
1917 + sgot->output_offset
1918 + off);
1919 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1920 loc = srelgot->contents;
1921 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
1922 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1923 }
1924
1925 local_got_offsets[r_symndx] |= 1;
1926 }
1927
1928 value = sgot->output_offset + off;
1929 }
1930
1931 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1932 contents, rel->r_offset, value,
1933 (bfd_vma) 0);
1934
1935 case R_ARM_SBREL32:
1936 return bfd_reloc_notsupported;
1937
1938 case R_ARM_AMP_VCALL9:
1939 return bfd_reloc_notsupported;
1940
1941 case R_ARM_RSBREL32:
1942 return bfd_reloc_notsupported;
1943
1944 case R_ARM_THM_RPC22:
1945 return bfd_reloc_notsupported;
1946
1947 case R_ARM_RREL32:
1948 return bfd_reloc_notsupported;
1949
1950 case R_ARM_RABS32:
1951 return bfd_reloc_notsupported;
1952
1953 case R_ARM_RPC24:
1954 return bfd_reloc_notsupported;
1955
1956 case R_ARM_RBASE:
1957 return bfd_reloc_notsupported;
1958
1959 default:
1960 return bfd_reloc_notsupported;
1961 }
1962 }
1963
1964 #if USE_REL
1965 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. */
1966 static void
1967 arm_add_to_rel (abfd, address, howto, increment)
1968 bfd * abfd;
1969 bfd_byte * address;
1970 reloc_howto_type * howto;
1971 bfd_signed_vma increment;
1972 {
1973 bfd_signed_vma addend;
1974
1975 if (howto->type == R_ARM_THM_PC22)
1976 {
1977 int upper_insn, lower_insn;
1978 int upper, lower;
1979
1980 upper_insn = bfd_get_16 (abfd, address);
1981 lower_insn = bfd_get_16 (abfd, address + 2);
1982 upper = upper_insn & 0x7ff;
1983 lower = lower_insn & 0x7ff;
1984
1985 addend = (upper << 12) | (lower << 1);
1986 addend += increment;
1987 addend >>= 1;
1988
1989 upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
1990 lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
1991
1992 bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
1993 bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
1994 }
1995 else
1996 {
1997 bfd_vma contents;
1998
1999 contents = bfd_get_32 (abfd, address);
2000
2001 /* Get the (signed) value from the instruction. */
2002 addend = contents & howto->src_mask;
2003 if (addend & ((howto->src_mask + 1) >> 1))
2004 {
2005 bfd_signed_vma mask;
2006
2007 mask = -1;
2008 mask &= ~ howto->src_mask;
2009 addend |= mask;
2010 }
2011
2012 /* Add in the increment, (which is a byte value). */
2013 switch (howto->type)
2014 {
2015 default:
2016 addend += increment;
2017 break;
2018
2019 case R_ARM_PC24:
2020 addend <<= howto->size;
2021 addend += increment;
2022
2023 /* Should we check for overflow here ? */
2024
2025 /* Drop any undesired bits. */
2026 addend >>= howto->rightshift;
2027 break;
2028 }
2029
2030 contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
2031
2032 bfd_put_32 (abfd, contents, address);
2033 }
2034 }
2035 #endif /* USE_REL */
2036
2037 /* Relocate an ARM ELF section. */
2038 static bfd_boolean
2039 elf32_arm_relocate_section (output_bfd, info, input_bfd, input_section,
2040 contents, relocs, local_syms, local_sections)
2041 bfd *output_bfd;
2042 struct bfd_link_info *info;
2043 bfd *input_bfd;
2044 asection *input_section;
2045 bfd_byte *contents;
2046 Elf_Internal_Rela *relocs;
2047 Elf_Internal_Sym *local_syms;
2048 asection **local_sections;
2049 {
2050 Elf_Internal_Shdr *symtab_hdr;
2051 struct elf_link_hash_entry **sym_hashes;
2052 Elf_Internal_Rela *rel;
2053 Elf_Internal_Rela *relend;
2054 const char *name;
2055
2056 #if !USE_REL
2057 if (info->relocatable)
2058 return TRUE;
2059 #endif
2060
2061 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2062 sym_hashes = elf_sym_hashes (input_bfd);
2063
2064 rel = relocs;
2065 relend = relocs + input_section->reloc_count;
2066 for (; rel < relend; rel++)
2067 {
2068 int r_type;
2069 reloc_howto_type * howto;
2070 unsigned long r_symndx;
2071 Elf_Internal_Sym * sym;
2072 asection * sec;
2073 struct elf_link_hash_entry * h;
2074 bfd_vma relocation;
2075 bfd_reloc_status_type r;
2076 arelent bfd_reloc;
2077
2078 r_symndx = ELF32_R_SYM (rel->r_info);
2079 r_type = ELF32_R_TYPE (rel->r_info);
2080
2081 if ( r_type == R_ARM_GNU_VTENTRY
2082 || r_type == R_ARM_GNU_VTINHERIT)
2083 continue;
2084
2085 elf32_arm_info_to_howto (input_bfd, & bfd_reloc, rel);
2086 howto = bfd_reloc.howto;
2087
2088 #if USE_REL
2089 if (info->relocatable)
2090 {
2091 /* This is a relocatable link. We don't have to change
2092 anything, unless the reloc is against a section symbol,
2093 in which case we have to adjust according to where the
2094 section symbol winds up in the output section. */
2095 if (r_symndx < symtab_hdr->sh_info)
2096 {
2097 sym = local_syms + r_symndx;
2098 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2099 {
2100 sec = local_sections[r_symndx];
2101 arm_add_to_rel (input_bfd, contents + rel->r_offset,
2102 howto,
2103 (bfd_signed_vma) (sec->output_offset
2104 + sym->st_value));
2105 }
2106 }
2107
2108 continue;
2109 }
2110 #endif
2111
2112 /* This is a final link. */
2113 h = NULL;
2114 sym = NULL;
2115 sec = NULL;
2116
2117 if (r_symndx < symtab_hdr->sh_info)
2118 {
2119 sym = local_syms + r_symndx;
2120 sec = local_sections[r_symndx];
2121 #if USE_REL
2122 relocation = (sec->output_section->vma
2123 + sec->output_offset
2124 + sym->st_value);
2125 if ((sec->flags & SEC_MERGE)
2126 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2127 {
2128 asection *msec;
2129 bfd_vma addend, value;
2130
2131 if (howto->rightshift)
2132 {
2133 (*_bfd_error_handler)
2134 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
2135 input_bfd, input_section,
2136 (long) rel->r_offset, howto->name);
2137 return FALSE;
2138 }
2139
2140 value = bfd_get_32 (input_bfd, contents + rel->r_offset);
2141
2142 /* Get the (signed) value from the instruction. */
2143 addend = value & howto->src_mask;
2144 if (addend & ((howto->src_mask + 1) >> 1))
2145 {
2146 bfd_signed_vma mask;
2147
2148 mask = -1;
2149 mask &= ~ howto->src_mask;
2150 addend |= mask;
2151 }
2152 msec = sec;
2153 addend =
2154 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
2155 - relocation;
2156 addend += msec->output_section->vma + msec->output_offset;
2157 value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
2158 bfd_put_32 (input_bfd, value, contents + rel->r_offset);
2159 }
2160 #else
2161 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2162 #endif
2163 }
2164 else
2165 {
2166 bfd_boolean warned;
2167 bfd_boolean unresolved_reloc;
2168
2169 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2170 r_symndx, symtab_hdr, sym_hashes,
2171 h, sec, relocation,
2172 unresolved_reloc, warned);
2173
2174 if (unresolved_reloc || relocation != 0)
2175 {
2176 /* In these cases, we don't need the relocation value.
2177 We check specially because in some obscure cases
2178 sec->output_section will be NULL. */
2179 switch (r_type)
2180 {
2181 case R_ARM_PC24:
2182 case R_ARM_ABS32:
2183 case R_ARM_THM_PC22:
2184 case R_ARM_PLT32:
2185
2186 if (info->shared
2187 && (
2188 (!info->symbolic && h->dynindx != -1)
2189 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2190 )
2191 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2192 && ((input_section->flags & SEC_ALLOC) != 0
2193 /* DWARF will emit R_ARM_ABS32 relocations in its
2194 sections against symbols defined externally
2195 in shared libraries. We can't do anything
2196 with them here. */
2197 || ((input_section->flags & SEC_DEBUGGING) != 0
2198 && (h->elf_link_hash_flags
2199 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2200 )
2201 relocation = 0;
2202 break;
2203
2204 case R_ARM_GOTPC:
2205 relocation = 0;
2206 break;
2207
2208 case R_ARM_GOT32:
2209 if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL
2210 (elf_hash_table (info)->dynamic_sections_created,
2211 info->shared, h))
2212 && (!info->shared
2213 || (!info->symbolic && h->dynindx != -1)
2214 || (h->elf_link_hash_flags
2215 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2216 relocation = 0;
2217 break;
2218
2219 default:
2220 if (unresolved_reloc)
2221 _bfd_error_handler
2222 (_("%B(%A): warning: unresolvable relocation %d against symbol `%s'"),
2223 input_bfd, input_section,
2224 r_type,
2225 h->root.root.string);
2226 break;
2227 }
2228 }
2229 }
2230
2231 if (h != NULL)
2232 name = h->root.root.string;
2233 else
2234 {
2235 name = (bfd_elf_string_from_elf_section
2236 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2237 if (name == NULL || *name == '\0')
2238 name = bfd_section_name (input_bfd, sec);
2239 }
2240
2241 r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
2242 input_section, contents, rel,
2243 relocation, info, sec, name,
2244 (h ? ELF_ST_TYPE (h->type) :
2245 ELF_ST_TYPE (sym->st_info)), h);
2246
2247 if (r != bfd_reloc_ok)
2248 {
2249 const char * msg = (const char *) 0;
2250
2251 switch (r)
2252 {
2253 case bfd_reloc_overflow:
2254 /* If the overflowing reloc was to an undefined symbol,
2255 we have already printed one error message and there
2256 is no point complaining again. */
2257 if ((! h ||
2258 h->root.type != bfd_link_hash_undefined)
2259 && (!((*info->callbacks->reloc_overflow)
2260 (info, name, howto->name, (bfd_vma) 0,
2261 input_bfd, input_section, rel->r_offset))))
2262 return FALSE;
2263 break;
2264
2265 case bfd_reloc_undefined:
2266 if (!((*info->callbacks->undefined_symbol)
2267 (info, name, input_bfd, input_section,
2268 rel->r_offset, TRUE)))
2269 return FALSE;
2270 break;
2271
2272 case bfd_reloc_outofrange:
2273 msg = _("internal error: out of range error");
2274 goto common_error;
2275
2276 case bfd_reloc_notsupported:
2277 msg = _("internal error: unsupported relocation error");
2278 goto common_error;
2279
2280 case bfd_reloc_dangerous:
2281 msg = _("internal error: dangerous error");
2282 goto common_error;
2283
2284 default:
2285 msg = _("internal error: unknown error");
2286 /* fall through */
2287
2288 common_error:
2289 if (!((*info->callbacks->warning)
2290 (info, msg, name, input_bfd, input_section,
2291 rel->r_offset)))
2292 return FALSE;
2293 break;
2294 }
2295 }
2296 }
2297
2298 return TRUE;
2299 }
2300
2301 /* Set the right machine number. */
2302
2303 static bfd_boolean
2304 elf32_arm_object_p (abfd)
2305 bfd *abfd;
2306 {
2307 unsigned int mach;
2308
2309 mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
2310
2311 if (mach != bfd_mach_arm_unknown)
2312 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2313
2314 else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
2315 bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
2316
2317 else
2318 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2319
2320 return TRUE;
2321 }
2322
2323 /* Function to keep ARM specific flags in the ELF header. */
2324 static bfd_boolean
2325 elf32_arm_set_private_flags (abfd, flags)
2326 bfd *abfd;
2327 flagword flags;
2328 {
2329 if (elf_flags_init (abfd)
2330 && elf_elfheader (abfd)->e_flags != flags)
2331 {
2332 if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
2333 {
2334 if (flags & EF_ARM_INTERWORK)
2335 (*_bfd_error_handler)
2336 (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
2337 abfd);
2338 else
2339 _bfd_error_handler
2340 (_("Warning: Clearing the interworking flag of %B due to outside request"),
2341 abfd);
2342 }
2343 }
2344 else
2345 {
2346 elf_elfheader (abfd)->e_flags = flags;
2347 elf_flags_init (abfd) = TRUE;
2348 }
2349
2350 return TRUE;
2351 }
2352
2353 /* Copy backend specific data from one object module to another. */
2354
2355 static bfd_boolean
2356 elf32_arm_copy_private_bfd_data (ibfd, obfd)
2357 bfd *ibfd;
2358 bfd *obfd;
2359 {
2360 flagword in_flags;
2361 flagword out_flags;
2362
2363 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2364 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2365 return TRUE;
2366
2367 in_flags = elf_elfheader (ibfd)->e_flags;
2368 out_flags = elf_elfheader (obfd)->e_flags;
2369
2370 if (elf_flags_init (obfd)
2371 && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
2372 && in_flags != out_flags)
2373 {
2374 /* Cannot mix APCS26 and APCS32 code. */
2375 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2376 return FALSE;
2377
2378 /* Cannot mix float APCS and non-float APCS code. */
2379 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2380 return FALSE;
2381
2382 /* If the src and dest have different interworking flags
2383 then turn off the interworking bit. */
2384 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2385 {
2386 if (out_flags & EF_ARM_INTERWORK)
2387 _bfd_error_handler
2388 (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
2389 obfd, ibfd);
2390
2391 in_flags &= ~EF_ARM_INTERWORK;
2392 }
2393
2394 /* Likewise for PIC, though don't warn for this case. */
2395 if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
2396 in_flags &= ~EF_ARM_PIC;
2397 }
2398
2399 elf_elfheader (obfd)->e_flags = in_flags;
2400 elf_flags_init (obfd) = TRUE;
2401
2402 return TRUE;
2403 }
2404
2405 /* Merge backend specific data from an object file to the output
2406 object file when linking. */
2407
2408 static bfd_boolean
2409 elf32_arm_merge_private_bfd_data (ibfd, obfd)
2410 bfd * ibfd;
2411 bfd * obfd;
2412 {
2413 flagword out_flags;
2414 flagword in_flags;
2415 bfd_boolean flags_compatible = TRUE;
2416 asection *sec;
2417
2418 /* Check if we have the same endianess. */
2419 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2420 return FALSE;
2421
2422 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2423 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2424 return TRUE;
2425
2426 /* The input BFD must have had its flags initialised. */
2427 /* The following seems bogus to me -- The flags are initialized in
2428 the assembler but I don't think an elf_flags_init field is
2429 written into the object. */
2430 /* BFD_ASSERT (elf_flags_init (ibfd)); */
2431
2432 in_flags = elf_elfheader (ibfd)->e_flags;
2433 out_flags = elf_elfheader (obfd)->e_flags;
2434
2435 if (!elf_flags_init (obfd))
2436 {
2437 /* If the input is the default architecture and had the default
2438 flags then do not bother setting the flags for the output
2439 architecture, instead allow future merges to do this. If no
2440 future merges ever set these flags then they will retain their
2441 uninitialised values, which surprise surprise, correspond
2442 to the default values. */
2443 if (bfd_get_arch_info (ibfd)->the_default
2444 && elf_elfheader (ibfd)->e_flags == 0)
2445 return TRUE;
2446
2447 elf_flags_init (obfd) = TRUE;
2448 elf_elfheader (obfd)->e_flags = in_flags;
2449
2450 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2451 && bfd_get_arch_info (obfd)->the_default)
2452 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2453
2454 return TRUE;
2455 }
2456
2457 /* Determine what should happen if the input ARM architecture
2458 does not match the output ARM architecture. */
2459 if (! bfd_arm_merge_machines (ibfd, obfd))
2460 return FALSE;
2461
2462 /* Identical flags must be compatible. */
2463 if (in_flags == out_flags)
2464 return TRUE;
2465
2466 /* Check to see if the input BFD actually contains any sections. If
2467 not, its flags may not have been initialised either, but it
2468 cannot actually cause any incompatibility. Do not short-circuit
2469 dynamic objects; their section list may be emptied by
2470 elf_link_add_object_symbols.
2471
2472 Also check to see if there are no code sections in the input.
2473 In this case there is no need to check for code specific flags.
2474 XXX - do we need to worry about floating-point format compatability
2475 in data sections ? */
2476 if (!(ibfd->flags & DYNAMIC))
2477 {
2478 bfd_boolean null_input_bfd = TRUE;
2479 bfd_boolean only_data_sections = TRUE;
2480
2481 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2482 {
2483 /* Ignore synthetic glue sections. */
2484 if (strcmp (sec->name, ".glue_7")
2485 && strcmp (sec->name, ".glue_7t"))
2486 {
2487 if ((bfd_get_section_flags (ibfd, sec)
2488 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2489 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2490 only_data_sections = FALSE;
2491
2492 null_input_bfd = FALSE;
2493 break;
2494 }
2495 }
2496
2497 if (null_input_bfd || only_data_sections)
2498 return TRUE;
2499 }
2500
2501 /* Complain about various flag mismatches. */
2502 if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
2503 {
2504 _bfd_error_handler
2505 (_("ERROR: %B is compiled for EABI version %d, whereas %B is compiled for version %d"),
2506 ibfd, obfd,
2507 (in_flags & EF_ARM_EABIMASK) >> 24,
2508 (out_flags & EF_ARM_EABIMASK) >> 24);
2509 return FALSE;
2510 }
2511
2512 /* Not sure what needs to be checked for EABI versions >= 1. */
2513 if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
2514 {
2515 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2516 {
2517 _bfd_error_handler
2518 (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
2519 ibfd, obfd,
2520 in_flags & EF_ARM_APCS_26 ? 26 : 32,
2521 out_flags & EF_ARM_APCS_26 ? 26 : 32);
2522 flags_compatible = FALSE;
2523 }
2524
2525 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2526 {
2527 if (in_flags & EF_ARM_APCS_FLOAT)
2528 _bfd_error_handler
2529 (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
2530 ibfd, obfd);
2531 else
2532 _bfd_error_handler
2533 (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
2534 ibfd, obfd);
2535
2536 flags_compatible = FALSE;
2537 }
2538
2539 if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
2540 {
2541 if (in_flags & EF_ARM_VFP_FLOAT)
2542 _bfd_error_handler
2543 (_("ERROR: %B uses VFP instructions, whereas %B does not"),
2544 ibfd, obfd);
2545 else
2546 _bfd_error_handler
2547 (_("ERROR: %B uses FPA instructions, whereas %B does not"),
2548 ibfd, obfd);
2549
2550 flags_compatible = FALSE;
2551 }
2552
2553 if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
2554 {
2555 if (in_flags & EF_ARM_MAVERICK_FLOAT)
2556 _bfd_error_handler
2557 (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
2558 ibfd, obfd);
2559 else
2560 _bfd_error_handler
2561 (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
2562 ibfd, obfd);
2563
2564 flags_compatible = FALSE;
2565 }
2566
2567 #ifdef EF_ARM_SOFT_FLOAT
2568 if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
2569 {
2570 /* We can allow interworking between code that is VFP format
2571 layout, and uses either soft float or integer regs for
2572 passing floating point arguments and results. We already
2573 know that the APCS_FLOAT flags match; similarly for VFP
2574 flags. */
2575 if ((in_flags & EF_ARM_APCS_FLOAT) != 0
2576 || (in_flags & EF_ARM_VFP_FLOAT) == 0)
2577 {
2578 if (in_flags & EF_ARM_SOFT_FLOAT)
2579 _bfd_error_handler
2580 (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
2581 ibfd, obfd);
2582 else
2583 _bfd_error_handler
2584 (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
2585 ibfd, obfd);
2586
2587 flags_compatible = FALSE;
2588 }
2589 }
2590 #endif
2591
2592 /* Interworking mismatch is only a warning. */
2593 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2594 {
2595 if (in_flags & EF_ARM_INTERWORK)
2596 {
2597 _bfd_error_handler
2598 (_("Warning: %B supports interworking, whereas %B does not"),
2599 ibfd, obfd);
2600 }
2601 else
2602 {
2603 _bfd_error_handler
2604 (_("Warning: %B does not support interworking, whereas %B does"),
2605 ibfd, obfd);
2606 }
2607 }
2608 }
2609
2610 return flags_compatible;
2611 }
2612
2613 /* Display the flags field. */
2614
2615 static bfd_boolean
2616 elf32_arm_print_private_bfd_data (abfd, ptr)
2617 bfd *abfd;
2618 PTR ptr;
2619 {
2620 FILE * file = (FILE *) ptr;
2621 unsigned long flags;
2622
2623 BFD_ASSERT (abfd != NULL && ptr != NULL);
2624
2625 /* Print normal ELF private data. */
2626 _bfd_elf_print_private_bfd_data (abfd, ptr);
2627
2628 flags = elf_elfheader (abfd)->e_flags;
2629 /* Ignore init flag - it may not be set, despite the flags field
2630 containing valid data. */
2631
2632 /* xgettext:c-format */
2633 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2634
2635 switch (EF_ARM_EABI_VERSION (flags))
2636 {
2637 case EF_ARM_EABI_UNKNOWN:
2638 /* The following flag bits are GNU extensions and not part of the
2639 official ARM ELF extended ABI. Hence they are only decoded if
2640 the EABI version is not set. */
2641 if (flags & EF_ARM_INTERWORK)
2642 fprintf (file, _(" [interworking enabled]"));
2643
2644 if (flags & EF_ARM_APCS_26)
2645 fprintf (file, " [APCS-26]");
2646 else
2647 fprintf (file, " [APCS-32]");
2648
2649 if (flags & EF_ARM_VFP_FLOAT)
2650 fprintf (file, _(" [VFP float format]"));
2651 else if (flags & EF_ARM_MAVERICK_FLOAT)
2652 fprintf (file, _(" [Maverick float format]"));
2653 else
2654 fprintf (file, _(" [FPA float format]"));
2655
2656 if (flags & EF_ARM_APCS_FLOAT)
2657 fprintf (file, _(" [floats passed in float registers]"));
2658
2659 if (flags & EF_ARM_PIC)
2660 fprintf (file, _(" [position independent]"));
2661
2662 if (flags & EF_ARM_NEW_ABI)
2663 fprintf (file, _(" [new ABI]"));
2664
2665 if (flags & EF_ARM_OLD_ABI)
2666 fprintf (file, _(" [old ABI]"));
2667
2668 if (flags & EF_ARM_SOFT_FLOAT)
2669 fprintf (file, _(" [software FP]"));
2670
2671 flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
2672 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
2673 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
2674 | EF_ARM_MAVERICK_FLOAT);
2675 break;
2676
2677 case EF_ARM_EABI_VER1:
2678 fprintf (file, _(" [Version1 EABI]"));
2679
2680 if (flags & EF_ARM_SYMSARESORTED)
2681 fprintf (file, _(" [sorted symbol table]"));
2682 else
2683 fprintf (file, _(" [unsorted symbol table]"));
2684
2685 flags &= ~ EF_ARM_SYMSARESORTED;
2686 break;
2687
2688 case EF_ARM_EABI_VER2:
2689 fprintf (file, _(" [Version2 EABI]"));
2690
2691 if (flags & EF_ARM_SYMSARESORTED)
2692 fprintf (file, _(" [sorted symbol table]"));
2693 else
2694 fprintf (file, _(" [unsorted symbol table]"));
2695
2696 if (flags & EF_ARM_DYNSYMSUSESEGIDX)
2697 fprintf (file, _(" [dynamic symbols use segment index]"));
2698
2699 if (flags & EF_ARM_MAPSYMSFIRST)
2700 fprintf (file, _(" [mapping symbols precede others]"));
2701
2702 flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
2703 | EF_ARM_MAPSYMSFIRST);
2704 break;
2705
2706 case EF_ARM_EABI_VER3:
2707 fprintf (file, _(" [Version3 EABI]"));
2708
2709 if (flags & EF_ARM_BE8)
2710 fprintf (file, _(" [BE8]"));
2711
2712 if (flags & EF_ARM_LE8)
2713 fprintf (file, _(" [LE8]"));
2714
2715 flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
2716 break;
2717
2718 default:
2719 fprintf (file, _(" <EABI version unrecognised>"));
2720 break;
2721 }
2722
2723 flags &= ~ EF_ARM_EABIMASK;
2724
2725 if (flags & EF_ARM_RELEXEC)
2726 fprintf (file, _(" [relocatable executable]"));
2727
2728 if (flags & EF_ARM_HASENTRY)
2729 fprintf (file, _(" [has entry point]"));
2730
2731 flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
2732
2733 if (flags)
2734 fprintf (file, _("<Unrecognised flag bits set>"));
2735
2736 fputc ('\n', file);
2737
2738 return TRUE;
2739 }
2740
2741 static int
2742 elf32_arm_get_symbol_type (elf_sym, type)
2743 Elf_Internal_Sym * elf_sym;
2744 int type;
2745 {
2746 switch (ELF_ST_TYPE (elf_sym->st_info))
2747 {
2748 case STT_ARM_TFUNC:
2749 return ELF_ST_TYPE (elf_sym->st_info);
2750
2751 case STT_ARM_16BIT:
2752 /* If the symbol is not an object, return the STT_ARM_16BIT flag.
2753 This allows us to distinguish between data used by Thumb instructions
2754 and non-data (which is probably code) inside Thumb regions of an
2755 executable. */
2756 if (type != STT_OBJECT)
2757 return ELF_ST_TYPE (elf_sym->st_info);
2758 break;
2759
2760 default:
2761 break;
2762 }
2763
2764 return type;
2765 }
2766
2767 static asection *
2768 elf32_arm_gc_mark_hook (sec, info, rel, h, sym)
2769 asection *sec;
2770 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2771 Elf_Internal_Rela *rel;
2772 struct elf_link_hash_entry *h;
2773 Elf_Internal_Sym *sym;
2774 {
2775 if (h != NULL)
2776 {
2777 switch (ELF32_R_TYPE (rel->r_info))
2778 {
2779 case R_ARM_GNU_VTINHERIT:
2780 case R_ARM_GNU_VTENTRY:
2781 break;
2782
2783 default:
2784 switch (h->root.type)
2785 {
2786 case bfd_link_hash_defined:
2787 case bfd_link_hash_defweak:
2788 return h->root.u.def.section;
2789
2790 case bfd_link_hash_common:
2791 return h->root.u.c.p->section;
2792
2793 default:
2794 break;
2795 }
2796 }
2797 }
2798 else
2799 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2800
2801 return NULL;
2802 }
2803
2804 /* Update the got entry reference counts for the section being removed. */
2805
2806 static bfd_boolean
2807 elf32_arm_gc_sweep_hook (abfd, info, sec, relocs)
2808 bfd *abfd ATTRIBUTE_UNUSED;
2809 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2810 asection *sec ATTRIBUTE_UNUSED;
2811 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2812 {
2813 Elf_Internal_Shdr *symtab_hdr;
2814 struct elf_link_hash_entry **sym_hashes;
2815 bfd_signed_vma *local_got_refcounts;
2816 const Elf_Internal_Rela *rel, *relend;
2817 unsigned long r_symndx;
2818 struct elf_link_hash_entry *h;
2819
2820 elf_section_data (sec)->local_dynrel = NULL;
2821
2822 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2823 sym_hashes = elf_sym_hashes (abfd);
2824 local_got_refcounts = elf_local_got_refcounts (abfd);
2825
2826 relend = relocs + sec->reloc_count;
2827 for (rel = relocs; rel < relend; rel++)
2828 switch (ELF32_R_TYPE (rel->r_info))
2829 {
2830 case R_ARM_GOT32:
2831 r_symndx = ELF32_R_SYM (rel->r_info);
2832 if (r_symndx >= symtab_hdr->sh_info)
2833 {
2834 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2835 if (h->got.refcount > 0)
2836 h->got.refcount -= 1;
2837 }
2838 else if (local_got_refcounts != NULL)
2839 {
2840 if (local_got_refcounts[r_symndx] > 0)
2841 local_got_refcounts[r_symndx] -= 1;
2842 }
2843 break;
2844
2845 case R_ARM_ABS32:
2846 case R_ARM_REL32:
2847 case R_ARM_PC24:
2848 case R_ARM_PLT32:
2849 r_symndx = ELF32_R_SYM (rel->r_info);
2850 if (r_symndx >= symtab_hdr->sh_info)
2851 {
2852 struct elf32_arm_link_hash_entry *eh;
2853 struct elf32_arm_relocs_copied **pp;
2854 struct elf32_arm_relocs_copied *p;
2855
2856 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2857
2858 if (h->plt.refcount > 0)
2859 h->plt.refcount -= 1;
2860
2861 if (ELF32_R_TYPE (rel->r_info) == R_ARM_ABS32
2862 || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
2863 {
2864 eh = (struct elf32_arm_link_hash_entry *) h;
2865
2866 for (pp = &eh->relocs_copied; (p = *pp) != NULL;
2867 pp = &p->next)
2868 if (p->section == sec)
2869 {
2870 p->count -= 1;
2871 if (p->count == 0)
2872 *pp = p->next;
2873 break;
2874 }
2875 }
2876 }
2877 break;
2878
2879 default:
2880 break;
2881 }
2882
2883 return TRUE;
2884 }
2885
2886 /* Look through the relocs for a section during the first phase. */
2887
2888 static bfd_boolean
2889 elf32_arm_check_relocs (abfd, info, sec, relocs)
2890 bfd *abfd;
2891 struct bfd_link_info *info;
2892 asection *sec;
2893 const Elf_Internal_Rela *relocs;
2894 {
2895 Elf_Internal_Shdr *symtab_hdr;
2896 struct elf_link_hash_entry **sym_hashes;
2897 struct elf_link_hash_entry **sym_hashes_end;
2898 const Elf_Internal_Rela *rel;
2899 const Elf_Internal_Rela *rel_end;
2900 bfd *dynobj;
2901 asection *sreloc;
2902 bfd_vma *local_got_offsets;
2903 struct elf32_arm_link_hash_table *htab;
2904
2905 if (info->relocatable)
2906 return TRUE;
2907
2908 htab = elf32_arm_hash_table (info);
2909 sreloc = NULL;
2910
2911 dynobj = elf_hash_table (info)->dynobj;
2912 local_got_offsets = elf_local_got_offsets (abfd);
2913
2914 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2915 sym_hashes = elf_sym_hashes (abfd);
2916 sym_hashes_end = sym_hashes
2917 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2918
2919 if (!elf_bad_symtab (abfd))
2920 sym_hashes_end -= symtab_hdr->sh_info;
2921
2922 rel_end = relocs + sec->reloc_count;
2923 for (rel = relocs; rel < rel_end; rel++)
2924 {
2925 struct elf_link_hash_entry *h;
2926 unsigned long r_symndx;
2927
2928 r_symndx = ELF32_R_SYM (rel->r_info);
2929 if (r_symndx < symtab_hdr->sh_info)
2930 h = NULL;
2931 else
2932 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2933
2934 switch (ELF32_R_TYPE (rel->r_info))
2935 {
2936 case R_ARM_GOT32:
2937 /* This symbol requires a global offset table entry. */
2938 if (h != NULL)
2939 {
2940 h->got.refcount++;
2941 }
2942 else
2943 {
2944 bfd_signed_vma *local_got_refcounts;
2945
2946 /* This is a global offset table entry for a local symbol. */
2947 local_got_refcounts = elf_local_got_refcounts (abfd);
2948 if (local_got_refcounts == NULL)
2949 {
2950 bfd_size_type size;
2951
2952 size = symtab_hdr->sh_info;
2953 size *= (sizeof (bfd_signed_vma) + sizeof(char));
2954 local_got_refcounts = ((bfd_signed_vma *)
2955 bfd_zalloc (abfd, size));
2956 if (local_got_refcounts == NULL)
2957 return FALSE;
2958 elf_local_got_refcounts (abfd) = local_got_refcounts;
2959 }
2960 local_got_refcounts[r_symndx] += 1;
2961 }
2962 break;
2963
2964 case R_ARM_GOTOFF:
2965 case R_ARM_GOTPC:
2966 if (htab->sgot == NULL)
2967 {
2968 if (htab->root.dynobj == NULL)
2969 htab->root.dynobj = abfd;
2970 if (!create_got_section (htab->root.dynobj, info))
2971 return FALSE;
2972 }
2973 break;
2974
2975 case R_ARM_ABS32:
2976 case R_ARM_REL32:
2977 case R_ARM_PC24:
2978 case R_ARM_PLT32:
2979 if (h != NULL)
2980 {
2981 /* If this reloc is in a read-only section, we might
2982 need a copy reloc. We can't check reliably at this
2983 stage whether the section is read-only, as input
2984 sections have not yet been mapped to output sections.
2985 Tentatively set the flag for now, and correct in
2986 adjust_dynamic_symbol. */
2987 if (!info->shared)
2988 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2989
2990 /* We may need a .plt entry if the function this reloc
2991 refers to is in a different object. We can't tell for
2992 sure yet, because something later might force the
2993 symbol local. */
2994 if (ELF32_R_TYPE (rel->r_info) == R_ARM_PC24
2995 || ELF32_R_TYPE (rel->r_info) == R_ARM_PLT32)
2996 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2997
2998 /* If we create a PLT entry, this relocation will reference
2999 it, even if it's an ABS32 relocation. */
3000 h->plt.refcount += 1;
3001 }
3002
3003 /* If we are creating a shared library, and this is a reloc
3004 against a global symbol, or a non PC relative reloc
3005 against a local symbol, then we need to copy the reloc
3006 into the shared library. However, if we are linking with
3007 -Bsymbolic, we do not need to copy a reloc against a
3008 global symbol which is defined in an object we are
3009 including in the link (i.e., DEF_REGULAR is set). At
3010 this point we have not seen all the input files, so it is
3011 possible that DEF_REGULAR is not set now but will be set
3012 later (it is never cleared). We account for that
3013 possibility below by storing information in the
3014 relocs_copied field of the hash table entry. */
3015 if (info->shared
3016 && (sec->flags & SEC_ALLOC) != 0
3017 && ((ELF32_R_TYPE (rel->r_info) != R_ARM_PC24
3018 && ELF32_R_TYPE (rel->r_info) != R_ARM_PLT32
3019 && ELF32_R_TYPE (rel->r_info) != R_ARM_REL32)
3020 || (h != NULL
3021 && (! info->symbolic
3022 || (h->elf_link_hash_flags
3023 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3024 {
3025 struct elf32_arm_relocs_copied *p, **head;
3026
3027 /* When creating a shared object, we must copy these
3028 reloc types into the output file. We create a reloc
3029 section in dynobj and make room for this reloc. */
3030 if (sreloc == NULL)
3031 {
3032 const char * name;
3033
3034 name = (bfd_elf_string_from_elf_section
3035 (abfd,
3036 elf_elfheader (abfd)->e_shstrndx,
3037 elf_section_data (sec)->rel_hdr.sh_name));
3038 if (name == NULL)
3039 return FALSE;
3040
3041 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
3042 && strcmp (bfd_get_section_name (abfd, sec),
3043 name + 4) == 0);
3044
3045 sreloc = bfd_get_section_by_name (dynobj, name);
3046 if (sreloc == NULL)
3047 {
3048 flagword flags;
3049
3050 sreloc = bfd_make_section (dynobj, name);
3051 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3052 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3053 if ((sec->flags & SEC_ALLOC) != 0)
3054 flags |= SEC_ALLOC | SEC_LOAD;
3055 if (sreloc == NULL
3056 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3057 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
3058 return FALSE;
3059 }
3060
3061 elf_section_data (sec)->sreloc = sreloc;
3062 }
3063
3064 /* If this is a global symbol, we count the number of
3065 relocations we need for this symbol. */
3066 if (h != NULL)
3067 {
3068 head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
3069 }
3070 else
3071 {
3072 /* Track dynamic relocs needed for local syms too.
3073 We really need local syms available to do this
3074 easily. Oh well. */
3075
3076 asection *s;
3077 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3078 sec, r_symndx);
3079 if (s == NULL)
3080 return FALSE;
3081
3082 head = ((struct elf32_arm_relocs_copied **)
3083 &elf_section_data (s)->local_dynrel);
3084 }
3085
3086 p = *head;
3087 if (p == NULL || p->section != sec)
3088 {
3089 bfd_size_type amt = sizeof *p;
3090 p = bfd_alloc (htab->root.dynobj, amt);
3091 if (p == NULL)
3092 return FALSE;
3093 p->next = *head;
3094 *head = p;
3095 p->section = sec;
3096 p->count = 0;
3097 }
3098
3099 if (ELF32_R_TYPE (rel->r_info) == R_ARM_ABS32
3100 || ELF32_R_TYPE (rel->r_info) == R_ARM_REL32)
3101 p->count += 1;
3102 }
3103 break;
3104
3105 /* This relocation describes the C++ object vtable hierarchy.
3106 Reconstruct it for later use during GC. */
3107 case R_ARM_GNU_VTINHERIT:
3108 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3109 return FALSE;
3110 break;
3111
3112 /* This relocation describes which C++ vtable entries are actually
3113 used. Record for later use during GC. */
3114 case R_ARM_GNU_VTENTRY:
3115 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
3116 return FALSE;
3117 break;
3118 }
3119 }
3120
3121 return TRUE;
3122 }
3123
3124 /* Find the nearest line to a particular section and offset, for error
3125 reporting. This code is a duplicate of the code in elf.c, except
3126 that it also accepts STT_ARM_TFUNC as a symbol that names a function. */
3127
3128 static bfd_boolean
3129 elf32_arm_find_nearest_line
3130 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
3131 bfd *abfd;
3132 asection *section;
3133 asymbol **symbols;
3134 bfd_vma offset;
3135 const char **filename_ptr;
3136 const char **functionname_ptr;
3137 unsigned int *line_ptr;
3138 {
3139 bfd_boolean found;
3140 const char *filename;
3141 asymbol *func;
3142 bfd_vma low_func;
3143 asymbol **p;
3144
3145 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3146 filename_ptr, functionname_ptr,
3147 line_ptr, 0,
3148 &elf_tdata (abfd)->dwarf2_find_line_info))
3149 return TRUE;
3150
3151 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3152 &found, filename_ptr,
3153 functionname_ptr, line_ptr,
3154 &elf_tdata (abfd)->line_info))
3155 return FALSE;
3156
3157 if (found)
3158 return TRUE;
3159
3160 if (symbols == NULL)
3161 return FALSE;
3162
3163 filename = NULL;
3164 func = NULL;
3165 low_func = 0;
3166
3167 for (p = symbols; *p != NULL; p++)
3168 {
3169 elf_symbol_type *q;
3170
3171 q = (elf_symbol_type *) *p;
3172
3173 if (bfd_get_section (&q->symbol) != section)
3174 continue;
3175
3176 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3177 {
3178 default:
3179 break;
3180 case STT_FILE:
3181 filename = bfd_asymbol_name (&q->symbol);
3182 break;
3183 case STT_NOTYPE:
3184 case STT_FUNC:
3185 case STT_ARM_TFUNC:
3186 if (q->symbol.section == section
3187 && q->symbol.value >= low_func
3188 && q->symbol.value <= offset)
3189 {
3190 func = (asymbol *) q;
3191 low_func = q->symbol.value;
3192 }
3193 break;
3194 }
3195 }
3196
3197 if (func == NULL)
3198 return FALSE;
3199
3200 *filename_ptr = filename;
3201 *functionname_ptr = bfd_asymbol_name (func);
3202 *line_ptr = 0;
3203
3204 return TRUE;
3205 }
3206
3207 /* Adjust a symbol defined by a dynamic object and referenced by a
3208 regular object. The current definition is in some section of the
3209 dynamic object, but we're not including those sections. We have to
3210 change the definition to something the rest of the link can
3211 understand. */
3212
3213 static bfd_boolean
3214 elf32_arm_adjust_dynamic_symbol (info, h)
3215 struct bfd_link_info * info;
3216 struct elf_link_hash_entry * h;
3217 {
3218 bfd * dynobj;
3219 asection * s;
3220 unsigned int power_of_two;
3221
3222 dynobj = elf_hash_table (info)->dynobj;
3223
3224 /* Make sure we know what is going on here. */
3225 BFD_ASSERT (dynobj != NULL
3226 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3227 || h->weakdef != NULL
3228 || ((h->elf_link_hash_flags
3229 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3230 && (h->elf_link_hash_flags
3231 & ELF_LINK_HASH_REF_REGULAR) != 0
3232 && (h->elf_link_hash_flags
3233 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3234
3235 /* If this is a function, put it in the procedure linkage table. We
3236 will fill in the contents of the procedure linkage table later,
3237 when we know the address of the .got section. */
3238 if (h->type == STT_FUNC
3239 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3240 {
3241 if (h->plt.refcount <= 0
3242 || SYMBOL_CALLS_LOCAL (info, h)
3243 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3244 && h->root.type == bfd_link_hash_undefweak))
3245 {
3246 /* This case can occur if we saw a PLT32 reloc in an input
3247 file, but the symbol was never referred to by a dynamic
3248 object, or if all references were garbage collected. In
3249 such a case, we don't actually need to build a procedure
3250 linkage table, and we can just do a PC24 reloc instead. */
3251 h->plt.offset = (bfd_vma) -1;
3252 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3253 }
3254
3255 return TRUE;
3256 }
3257 else
3258 /* It's possible that we incorrectly decided a .plt reloc was
3259 needed for an R_ARM_PC24 reloc to a non-function sym in
3260 check_relocs. We can't decide accurately between function and
3261 non-function syms in check-relocs; Objects loaded later in
3262 the link may change h->type. So fix it now. */
3263 h->plt.offset = (bfd_vma) -1;
3264
3265 /* If this is a weak symbol, and there is a real definition, the
3266 processor independent code will have arranged for us to see the
3267 real definition first, and we can just use the same value. */
3268 if (h->weakdef != NULL)
3269 {
3270 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3271 || h->weakdef->root.type == bfd_link_hash_defweak);
3272 h->root.u.def.section = h->weakdef->root.u.def.section;
3273 h->root.u.def.value = h->weakdef->root.u.def.value;
3274 return TRUE;
3275 }
3276
3277 /* This is a reference to a symbol defined by a dynamic object which
3278 is not a function. */
3279
3280 /* If we are creating a shared library, we must presume that the
3281 only references to the symbol are via the global offset table.
3282 For such cases we need not do anything here; the relocations will
3283 be handled correctly by relocate_section. */
3284 if (info->shared)
3285 return TRUE;
3286
3287 /* We must allocate the symbol in our .dynbss section, which will
3288 become part of the .bss section of the executable. There will be
3289 an entry for this symbol in the .dynsym section. The dynamic
3290 object will contain position independent code, so all references
3291 from the dynamic object to this symbol will go through the global
3292 offset table. The dynamic linker will use the .dynsym entry to
3293 determine the address it must put in the global offset table, so
3294 both the dynamic object and the regular object will refer to the
3295 same memory location for the variable. */
3296 s = bfd_get_section_by_name (dynobj, ".dynbss");
3297 BFD_ASSERT (s != NULL);
3298
3299 /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
3300 copy the initial value out of the dynamic object and into the
3301 runtime process image. We need to remember the offset into the
3302 .rel.bss section we are going to use. */
3303 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3304 {
3305 asection *srel;
3306
3307 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
3308 BFD_ASSERT (srel != NULL);
3309 srel->size += sizeof (Elf32_External_Rel);
3310 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3311 }
3312
3313 /* We need to figure out the alignment required for this symbol. I
3314 have no idea how ELF linkers handle this. */
3315 power_of_two = bfd_log2 (h->size);
3316 if (power_of_two > 3)
3317 power_of_two = 3;
3318
3319 /* Apply the required alignment. */
3320 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3321 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3322 {
3323 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3324 return FALSE;
3325 }
3326
3327 /* Define the symbol as being at this point in the section. */
3328 h->root.u.def.section = s;
3329 h->root.u.def.value = s->size;
3330
3331 /* Increment the section size to make room for the symbol. */
3332 s->size += h->size;
3333
3334 return TRUE;
3335 }
3336
3337 /* Allocate space in .plt, .got and associated reloc sections for
3338 dynamic relocs. */
3339
3340 static bfd_boolean
3341 allocate_dynrelocs (h, inf)
3342 struct elf_link_hash_entry *h;
3343 PTR inf;
3344 {
3345 struct bfd_link_info *info;
3346 struct elf32_arm_link_hash_table *htab;
3347 struct elf32_arm_link_hash_entry *eh;
3348 struct elf32_arm_relocs_copied *p;
3349
3350 if (h->root.type == bfd_link_hash_indirect)
3351 return TRUE;
3352
3353 if (h->root.type == bfd_link_hash_warning)
3354 /* When warning symbols are created, they **replace** the "real"
3355 entry in the hash table, thus we never get to see the real
3356 symbol in a hash traversal. So look at it now. */
3357 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3358
3359 info = (struct bfd_link_info *) inf;
3360 htab = elf32_arm_hash_table (info);
3361
3362 if (htab->root.dynamic_sections_created
3363 && h->plt.refcount > 0)
3364 {
3365 /* Make sure this symbol is output as a dynamic symbol.
3366 Undefined weak syms won't yet be marked as dynamic. */
3367 if (h->dynindx == -1
3368 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3369 {
3370 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3371 return FALSE;
3372 }
3373
3374 if (info->shared
3375 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3376 {
3377 asection *s = htab->splt;
3378
3379 /* If this is the first .plt entry, make room for the special
3380 first entry. */
3381 if (s->size == 0)
3382 s->size += PLT_HEADER_SIZE;
3383
3384 h->plt.offset = s->size;
3385
3386 /* If this symbol is not defined in a regular file, and we are
3387 not generating a shared library, then set the symbol to this
3388 location in the .plt. This is required to make function
3389 pointers compare as equal between the normal executable and
3390 the shared library. */
3391 if (! info->shared
3392 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3393 {
3394 h->root.u.def.section = s;
3395 h->root.u.def.value = h->plt.offset;
3396 }
3397
3398 /* Make room for this entry. */
3399 s->size += PLT_ENTRY_SIZE;
3400
3401 /* We also need to make an entry in the .got.plt section, which
3402 will be placed in the .got section by the linker script. */
3403 htab->sgotplt->size += 4;
3404
3405 /* We also need to make an entry in the .rel.plt section. */
3406 htab->srelplt->size += sizeof (Elf32_External_Rel);
3407 }
3408 else
3409 {
3410 h->plt.offset = (bfd_vma) -1;
3411 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3412 }
3413 }
3414 else
3415 {
3416 h->plt.offset = (bfd_vma) -1;
3417 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3418 }
3419
3420 if (h->got.refcount > 0)
3421 {
3422 asection *s;
3423 bfd_boolean dyn;
3424
3425 /* Make sure this symbol is output as a dynamic symbol.
3426 Undefined weak syms won't yet be marked as dynamic. */
3427 if (h->dynindx == -1
3428 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3429 {
3430 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3431 return FALSE;
3432 }
3433
3434 s = htab->sgot;
3435 h->got.offset = s->size;
3436 s->size += 4;
3437 dyn = htab->root.dynamic_sections_created;
3438 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3439 || h->root.type != bfd_link_hash_undefweak)
3440 && (info->shared
3441 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3442 htab->srelgot->size += sizeof (Elf32_External_Rel);
3443 }
3444 else
3445 h->got.offset = (bfd_vma) -1;
3446
3447 eh = (struct elf32_arm_link_hash_entry *) h;
3448 if (eh->relocs_copied == NULL)
3449 return TRUE;
3450
3451 /* In the shared -Bsymbolic case, discard space allocated for
3452 dynamic pc-relative relocs against symbols which turn out to be
3453 defined in regular objects. For the normal shared case, discard
3454 space for pc-relative relocs that have become local due to symbol
3455 visibility changes. */
3456
3457 if (info->shared)
3458 {
3459 /* Discard relocs on undefined weak syms with non-default
3460 visibility. */
3461 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3462 && h->root.type == bfd_link_hash_undefweak)
3463 eh->relocs_copied = NULL;
3464 }
3465 else
3466 {
3467 /* For the non-shared case, discard space for relocs against
3468 symbols which turn out to need copy relocs or are not
3469 dynamic. */
3470
3471 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3472 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3473 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3474 || (htab->root.dynamic_sections_created
3475 && (h->root.type == bfd_link_hash_undefweak
3476 || h->root.type == bfd_link_hash_undefined))))
3477 {
3478 /* Make sure this symbol is output as a dynamic symbol.
3479 Undefined weak syms won't yet be marked as dynamic. */
3480 if (h->dynindx == -1
3481 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3482 {
3483 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3484 return FALSE;
3485 }
3486
3487 /* If that succeeded, we know we'll be keeping all the
3488 relocs. */
3489 if (h->dynindx != -1)
3490 goto keep;
3491 }
3492
3493 eh->relocs_copied = NULL;
3494
3495 keep: ;
3496 }
3497
3498 /* Finally, allocate space. */
3499 for (p = eh->relocs_copied; p != NULL; p = p->next)
3500 {
3501 asection *sreloc = elf_section_data (p->section)->sreloc;
3502 sreloc->size += p->count * sizeof (Elf32_External_Rel);
3503 }
3504
3505 return TRUE;
3506 }
3507
3508 /* Set the sizes of the dynamic sections. */
3509
3510 static bfd_boolean
3511 elf32_arm_size_dynamic_sections (output_bfd, info)
3512 bfd * output_bfd ATTRIBUTE_UNUSED;
3513 struct bfd_link_info * info;
3514 {
3515 bfd * dynobj;
3516 asection * s;
3517 bfd_boolean plt;
3518 bfd_boolean relocs;
3519 bfd *ibfd;
3520 struct elf32_arm_link_hash_table *htab;
3521
3522 htab = elf32_arm_hash_table (info);
3523 dynobj = elf_hash_table (info)->dynobj;
3524 BFD_ASSERT (dynobj != NULL);
3525
3526 if (elf_hash_table (info)->dynamic_sections_created)
3527 {
3528 /* Set the contents of the .interp section to the interpreter. */
3529 if (info->executable)
3530 {
3531 s = bfd_get_section_by_name (dynobj, ".interp");
3532 BFD_ASSERT (s != NULL);
3533 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3534 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3535 }
3536 }
3537
3538 /* Set up .got offsets for local syms, and space for local dynamic
3539 relocs. */
3540 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3541 {
3542 bfd_signed_vma *local_got;
3543 bfd_signed_vma *end_local_got;
3544 char *local_tls_type;
3545 bfd_size_type locsymcount;
3546 Elf_Internal_Shdr *symtab_hdr;
3547 asection *srel;
3548
3549 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3550 continue;
3551
3552 for (s = ibfd->sections; s != NULL; s = s->next)
3553 {
3554 struct elf32_arm_relocs_copied *p;
3555
3556 for (p = *((struct elf32_arm_relocs_copied **)
3557 &elf_section_data (s)->local_dynrel);
3558 p != NULL;
3559 p = p->next)
3560 {
3561 if (!bfd_is_abs_section (p->section)
3562 && bfd_is_abs_section (p->section->output_section))
3563 {
3564 /* Input section has been discarded, either because
3565 it is a copy of a linkonce section or due to
3566 linker script /DISCARD/, so we'll be discarding
3567 the relocs too. */
3568 }
3569 else if (p->count != 0)
3570 {
3571 srel = elf_section_data (p->section)->sreloc;
3572 srel->size += p->count * sizeof (Elf32_External_Rel);
3573 if ((p->section->output_section->flags & SEC_READONLY) != 0)
3574 info->flags |= DF_TEXTREL;
3575 }
3576 }
3577 }
3578
3579 local_got = elf_local_got_refcounts (ibfd);
3580 if (!local_got)
3581 continue;
3582
3583 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3584 locsymcount = symtab_hdr->sh_info;
3585 end_local_got = local_got + locsymcount;
3586 s = htab->sgot;
3587 srel = htab->srelgot;
3588 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3589 {
3590 if (*local_got > 0)
3591 {
3592 *local_got = s->size;
3593 s->size += 4;
3594 if (info->shared)
3595 srel->size += sizeof (Elf32_External_Rel);
3596 }
3597 else
3598 *local_got = (bfd_vma) -1;
3599 }
3600 }
3601
3602 /* Allocate global sym .plt and .got entries, and space for global
3603 sym dynamic relocs. */
3604 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
3605
3606 /* The check_relocs and adjust_dynamic_symbol entry points have
3607 determined the sizes of the various dynamic sections. Allocate
3608 memory for them. */
3609 plt = FALSE;
3610 relocs = FALSE;
3611 for (s = dynobj->sections; s != NULL; s = s->next)
3612 {
3613 const char * name;
3614 bfd_boolean strip;
3615
3616 if ((s->flags & SEC_LINKER_CREATED) == 0)
3617 continue;
3618
3619 /* It's OK to base decisions on the section name, because none
3620 of the dynobj section names depend upon the input files. */
3621 name = bfd_get_section_name (dynobj, s);
3622
3623 strip = FALSE;
3624
3625 if (strcmp (name, ".plt") == 0)
3626 {
3627 if (s->size == 0)
3628 {
3629 /* Strip this section if we don't need it; see the
3630 comment below. */
3631 strip = TRUE;
3632 }
3633 else
3634 {
3635 /* Remember whether there is a PLT. */
3636 plt = TRUE;
3637 }
3638 }
3639 else if (strncmp (name, ".rel", 4) == 0)
3640 {
3641 if (s->size == 0)
3642 {
3643 /* If we don't need this section, strip it from the
3644 output file. This is mostly to handle .rel.bss and
3645 .rel.plt. We must create both sections in
3646 create_dynamic_sections, because they must be created
3647 before the linker maps input sections to output
3648 sections. The linker does that before
3649 adjust_dynamic_symbol is called, and it is that
3650 function which decides whether anything needs to go
3651 into these sections. */
3652 strip = TRUE;
3653 }
3654 else
3655 {
3656 /* Remember whether there are any reloc sections other
3657 than .rel.plt. */
3658 if (strcmp (name, ".rel.plt") != 0)
3659 relocs = TRUE;
3660
3661 /* We use the reloc_count field as a counter if we need
3662 to copy relocs into the output file. */
3663 s->reloc_count = 0;
3664 }
3665 }
3666 else if (strncmp (name, ".got", 4) != 0)
3667 {
3668 /* It's not one of our sections, so don't allocate space. */
3669 continue;
3670 }
3671
3672 if (strip)
3673 {
3674 _bfd_strip_section_from_output (info, s);
3675 continue;
3676 }
3677
3678 /* Allocate memory for the section contents. */
3679 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3680 if (s->contents == NULL && s->size != 0)
3681 return FALSE;
3682 }
3683
3684 if (elf_hash_table (info)->dynamic_sections_created)
3685 {
3686 /* Add some entries to the .dynamic section. We fill in the
3687 values later, in elf32_arm_finish_dynamic_sections, but we
3688 must add the entries now so that we get the correct size for
3689 the .dynamic section. The DT_DEBUG entry is filled in by the
3690 dynamic linker and used by the debugger. */
3691 #define add_dynamic_entry(TAG, VAL) \
3692 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3693
3694 if (!info->shared)
3695 {
3696 if (!add_dynamic_entry (DT_DEBUG, 0))
3697 return FALSE;
3698 }
3699
3700 if (plt)
3701 {
3702 if ( !add_dynamic_entry (DT_PLTGOT, 0)
3703 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3704 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3705 || !add_dynamic_entry (DT_JMPREL, 0))
3706 return FALSE;
3707 }
3708
3709 if (relocs)
3710 {
3711 if ( !add_dynamic_entry (DT_REL, 0)
3712 || !add_dynamic_entry (DT_RELSZ, 0)
3713 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3714 return FALSE;
3715 }
3716
3717 if ((info->flags & DF_TEXTREL) != 0)
3718 {
3719 if (!add_dynamic_entry (DT_TEXTREL, 0))
3720 return FALSE;
3721 info->flags |= DF_TEXTREL;
3722 }
3723 }
3724 #undef add_synamic_entry
3725
3726 return TRUE;
3727 }
3728
3729 /* Finish up dynamic symbol handling. We set the contents of various
3730 dynamic sections here. */
3731
3732 static bfd_boolean
3733 elf32_arm_finish_dynamic_symbol (output_bfd, info, h, sym)
3734 bfd * output_bfd;
3735 struct bfd_link_info * info;
3736 struct elf_link_hash_entry * h;
3737 Elf_Internal_Sym * sym;
3738 {
3739 bfd * dynobj;
3740
3741 dynobj = elf_hash_table (info)->dynobj;
3742
3743 if (h->plt.offset != (bfd_vma) -1)
3744 {
3745 asection * splt;
3746 asection * sgot;
3747 asection * srel;
3748 bfd_vma plt_index;
3749 bfd_vma got_offset;
3750 Elf_Internal_Rela rel;
3751 bfd_byte *loc;
3752 bfd_vma got_displacement;
3753
3754 /* This symbol has an entry in the procedure linkage table. Set
3755 it up. */
3756
3757 BFD_ASSERT (h->dynindx != -1);
3758
3759 splt = bfd_get_section_by_name (dynobj, ".plt");
3760 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3761 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
3762 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
3763
3764 /* Get the index in the procedure linkage table which
3765 corresponds to this symbol. This is the index of this symbol
3766 in all the symbols for which we are making plt entries. The
3767 first entry in the procedure linkage table is reserved. */
3768 plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3769
3770 /* Get the offset into the .got table of the entry that
3771 corresponds to this function. Each .got entry is 4 bytes.
3772 The first three are reserved. */
3773 got_offset = (plt_index + 3) * 4;
3774
3775 /* Calculate the displacement between the PLT slot and the
3776 entry in the GOT. */
3777 got_displacement = (sgot->output_section->vma
3778 + sgot->output_offset
3779 + got_offset
3780 - splt->output_section->vma
3781 - splt->output_offset
3782 - h->plt.offset
3783 - 8);
3784
3785 BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
3786
3787 /* Fill in the entry in the procedure linkage table. */
3788 bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
3789 splt->contents + h->plt.offset + 0);
3790 bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
3791 splt->contents + h->plt.offset + 4);
3792 bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
3793 splt->contents + h->plt.offset + 8);
3794 #ifdef FOUR_WORD_PLT
3795 bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
3796 splt->contents + h->plt.offset + 12);
3797 #endif
3798
3799 /* Fill in the entry in the global offset table. */
3800 bfd_put_32 (output_bfd,
3801 (splt->output_section->vma
3802 + splt->output_offset),
3803 sgot->contents + got_offset);
3804
3805 /* Fill in the entry in the .rel.plt section. */
3806 rel.r_offset = (sgot->output_section->vma
3807 + sgot->output_offset
3808 + got_offset);
3809 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
3810 loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
3811 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3812
3813 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3814 {
3815 /* Mark the symbol as undefined, rather than as defined in
3816 the .plt section. Leave the value alone. */
3817 sym->st_shndx = SHN_UNDEF;
3818 /* If the symbol is weak, we do need to clear the value.
3819 Otherwise, the PLT entry would provide a definition for
3820 the symbol even if the symbol wasn't defined anywhere,
3821 and so the symbol would never be NULL. */
3822 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
3823 == 0)
3824 sym->st_value = 0;
3825 }
3826 }
3827
3828 if (h->got.offset != (bfd_vma) -1)
3829 {
3830 asection * sgot;
3831 asection * srel;
3832 Elf_Internal_Rela rel;
3833 bfd_byte *loc;
3834
3835 /* This symbol has an entry in the global offset table. Set it
3836 up. */
3837 sgot = bfd_get_section_by_name (dynobj, ".got");
3838 srel = bfd_get_section_by_name (dynobj, ".rel.got");
3839 BFD_ASSERT (sgot != NULL && srel != NULL);
3840
3841 rel.r_offset = (sgot->output_section->vma
3842 + sgot->output_offset
3843 + (h->got.offset &~ (bfd_vma) 1));
3844
3845 /* If this is a static link, or it is a -Bsymbolic link and the
3846 symbol is defined locally or was forced to be local because
3847 of a version file, we just want to emit a RELATIVE reloc.
3848 The entry in the global offset table will already have been
3849 initialized in the relocate_section function. */
3850 if (info->shared
3851 && SYMBOL_REFERENCES_LOCAL (info, h))
3852 {
3853 BFD_ASSERT((h->got.offset & 1) != 0);
3854 rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
3855 }
3856 else
3857 {
3858 BFD_ASSERT((h->got.offset & 1) == 0);
3859 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3860 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
3861 }
3862
3863 loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
3864 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3865 }
3866
3867 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3868 {
3869 asection * s;
3870 Elf_Internal_Rela rel;
3871 bfd_byte *loc;
3872
3873 /* This symbol needs a copy reloc. Set it up. */
3874 BFD_ASSERT (h->dynindx != -1
3875 && (h->root.type == bfd_link_hash_defined
3876 || h->root.type == bfd_link_hash_defweak));
3877
3878 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3879 ".rel.bss");
3880 BFD_ASSERT (s != NULL);
3881
3882 rel.r_offset = (h->root.u.def.value
3883 + h->root.u.def.section->output_section->vma
3884 + h->root.u.def.section->output_offset);
3885 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
3886 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
3887 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3888 }
3889
3890 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3891 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3892 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3893 sym->st_shndx = SHN_ABS;
3894
3895 return TRUE;
3896 }
3897
3898 /* Finish up the dynamic sections. */
3899
3900 static bfd_boolean
3901 elf32_arm_finish_dynamic_sections (output_bfd, info)
3902 bfd * output_bfd;
3903 struct bfd_link_info * info;
3904 {
3905 bfd * dynobj;
3906 asection * sgot;
3907 asection * sdyn;
3908
3909 dynobj = elf_hash_table (info)->dynobj;
3910
3911 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3912 BFD_ASSERT (sgot != NULL);
3913 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3914
3915 if (elf_hash_table (info)->dynamic_sections_created)
3916 {
3917 asection *splt;
3918 Elf32_External_Dyn *dyncon, *dynconend;
3919
3920 splt = bfd_get_section_by_name (dynobj, ".plt");
3921 BFD_ASSERT (splt != NULL && sdyn != NULL);
3922
3923 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3924 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3925
3926 for (; dyncon < dynconend; dyncon++)
3927 {
3928 Elf_Internal_Dyn dyn;
3929 const char * name;
3930 asection * s;
3931
3932 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3933
3934 switch (dyn.d_tag)
3935 {
3936 default:
3937 break;
3938
3939 case DT_PLTGOT:
3940 name = ".got";
3941 goto get_vma;
3942 case DT_JMPREL:
3943 name = ".rel.plt";
3944 get_vma:
3945 s = bfd_get_section_by_name (output_bfd, name);
3946 BFD_ASSERT (s != NULL);
3947 dyn.d_un.d_ptr = s->vma;
3948 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3949 break;
3950
3951 case DT_PLTRELSZ:
3952 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
3953 BFD_ASSERT (s != NULL);
3954 dyn.d_un.d_val = s->size;
3955 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3956 break;
3957
3958 case DT_RELSZ:
3959 /* My reading of the SVR4 ABI indicates that the
3960 procedure linkage table relocs (DT_JMPREL) should be
3961 included in the overall relocs (DT_REL). This is
3962 what Solaris does. However, UnixWare can not handle
3963 that case. Therefore, we override the DT_RELSZ entry
3964 here to make it not include the JMPREL relocs. Since
3965 the linker script arranges for .rel.plt to follow all
3966 other relocation sections, we don't have to worry
3967 about changing the DT_REL entry. */
3968 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
3969 if (s != NULL)
3970 dyn.d_un.d_val -= s->size;
3971 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3972 break;
3973
3974 /* Set the bottom bit of DT_INIT/FINI if the
3975 corresponding function is Thumb. */
3976 case DT_INIT:
3977 name = info->init_function;
3978 goto get_sym;
3979 case DT_FINI:
3980 name = info->fini_function;
3981 get_sym:
3982 /* If it wasn't set by elf_bfd_final_link
3983 then there is nothing to adjust. */
3984 if (dyn.d_un.d_val != 0)
3985 {
3986 struct elf_link_hash_entry * eh;
3987
3988 eh = elf_link_hash_lookup (elf_hash_table (info), name,
3989 FALSE, FALSE, TRUE);
3990 if (eh != (struct elf_link_hash_entry *) NULL
3991 && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
3992 {
3993 dyn.d_un.d_val |= 1;
3994 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3995 }
3996 }
3997 break;
3998 }
3999 }
4000
4001 /* Fill in the first entry in the procedure linkage table. */
4002 if (splt->size > 0)
4003 {
4004 bfd_vma got_displacement;
4005
4006 /* Calculate the displacement between the PLT slot and &GOT[0]. */
4007 got_displacement = (sgot->output_section->vma
4008 + sgot->output_offset
4009 - splt->output_section->vma
4010 - splt->output_offset
4011 - 16);
4012
4013 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents + 0);
4014 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents + 4);
4015 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents + 8);
4016 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
4017 #ifdef FOUR_WORD_PLT
4018 /* The displacement value goes in the otherwise-unused last word of
4019 the second entry. */
4020 bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
4021 #else
4022 bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
4023 #endif
4024 }
4025
4026 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4027 really seem like the right value. */
4028 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4029 }
4030
4031 /* Fill in the first three entries in the global offset table. */
4032 if (sgot->size > 0)
4033 {
4034 if (sdyn == NULL)
4035 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4036 else
4037 bfd_put_32 (output_bfd,
4038 sdyn->output_section->vma + sdyn->output_offset,
4039 sgot->contents);
4040 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4041 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4042 }
4043
4044 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4045
4046 return TRUE;
4047 }
4048
4049 static void
4050 elf32_arm_post_process_headers (abfd, link_info)
4051 bfd * abfd;
4052 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
4053 {
4054 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
4055 struct elf32_arm_link_hash_table *globals;
4056
4057 i_ehdrp = elf_elfheader (abfd);
4058
4059 i_ehdrp->e_ident[EI_OSABI] = ARM_ELF_OS_ABI_VERSION;
4060 i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
4061
4062 if (link_info)
4063 {
4064 globals = elf32_arm_hash_table (link_info);
4065 if (globals->byteswap_code)
4066 i_ehdrp->e_flags |= EF_ARM_BE8;
4067 }
4068 }
4069
4070 static enum elf_reloc_type_class
4071 elf32_arm_reloc_type_class (rela)
4072 const Elf_Internal_Rela *rela;
4073 {
4074 switch ((int) ELF32_R_TYPE (rela->r_info))
4075 {
4076 case R_ARM_RELATIVE:
4077 return reloc_class_relative;
4078 case R_ARM_JUMP_SLOT:
4079 return reloc_class_plt;
4080 case R_ARM_COPY:
4081 return reloc_class_copy;
4082 default:
4083 return reloc_class_normal;
4084 }
4085 }
4086
4087 static bfd_boolean elf32_arm_section_flags PARAMS ((flagword *, const Elf_Internal_Shdr *));
4088 static void elf32_arm_final_write_processing PARAMS ((bfd *, bfd_boolean));
4089
4090 /* Set the right machine number for an Arm ELF file. */
4091
4092 static bfd_boolean
4093 elf32_arm_section_flags (flags, hdr)
4094 flagword *flags;
4095 const Elf_Internal_Shdr *hdr;
4096 {
4097 if (hdr->sh_type == SHT_NOTE)
4098 *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
4099
4100 return TRUE;
4101 }
4102
4103 static void
4104 elf32_arm_final_write_processing (abfd, linker)
4105 bfd *abfd;
4106 bfd_boolean linker ATTRIBUTE_UNUSED;
4107 {
4108 bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
4109 }
4110
4111
4112 /* Called for each symbol. Builds a section map based on mapping symbols.
4113 Does not alter any of the symbols. */
4114
4115 static bfd_boolean
4116 elf32_arm_output_symbol_hook (struct bfd_link_info *info,
4117 const char *name,
4118 Elf_Internal_Sym *elfsym,
4119 asection *input_sec,
4120 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4121 {
4122 int mapcount;
4123 elf32_arm_section_map *map;
4124 struct elf32_arm_link_hash_table *globals;
4125
4126 /* Only do this on final link. */
4127 if (info->relocatable)
4128 return TRUE;
4129
4130 /* Only build a map if we need to byteswap code. */
4131 globals = elf32_arm_hash_table (info);
4132 if (!globals->byteswap_code)
4133 return TRUE;
4134
4135 /* We only want mapping symbols. */
4136 if (name == NULL
4137 || name[0] != '$'
4138 || (name[1] != 'a'
4139 && name[1] != 't'
4140 && name[1] != 'd'))
4141 return TRUE;
4142
4143 mapcount = ++(elf32_arm_section_data (input_sec)->mapcount);
4144 map = elf32_arm_section_data (input_sec)->map;
4145 /* TODO: This may be inefficient, but we probably don't usually have many
4146 mapping symbols per section. */
4147 map = bfd_realloc (map, mapcount * sizeof (elf32_arm_section_map));
4148 elf32_arm_section_data (input_sec)->map = map;
4149
4150 map[mapcount - 1].vma = elfsym->st_value;
4151 map[mapcount - 1].type = name[1];
4152 return TRUE;
4153 }
4154
4155
4156 /* Allocate target specific section data. */
4157
4158 static bfd_boolean
4159 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
4160 {
4161 struct _arm_elf_section_data *sdata;
4162 bfd_size_type amt = sizeof (*sdata);
4163
4164 sdata = bfd_zalloc (abfd, amt);
4165 if (sdata == NULL)
4166 return FALSE;
4167 sec->used_by_bfd = sdata;
4168
4169 return _bfd_elf_new_section_hook (abfd, sec);
4170 }
4171
4172
4173 /* Used to order a list of mapping symbols by address. */
4174
4175 static int
4176 elf32_arm_compare_mapping (const void * a, const void * b)
4177 {
4178 return ((const elf32_arm_section_map *) a)->vma
4179 > ((const elf32_arm_section_map *) b)->vma;
4180 }
4181
4182
4183 /* Do code byteswapping. Return FALSE afterwards so that the section is
4184 written out as normal. */
4185
4186 static bfd_boolean
4187 elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
4188 bfd_byte *contents)
4189 {
4190 int mapcount;
4191 elf32_arm_section_map *map;
4192 bfd_vma ptr;
4193 bfd_vma end;
4194 bfd_vma offset;
4195 bfd_byte tmp;
4196 int i;
4197
4198 mapcount = elf32_arm_section_data (sec)->mapcount;
4199 map = elf32_arm_section_data (sec)->map;
4200
4201 if (mapcount == 0)
4202 return FALSE;
4203
4204 qsort (map, mapcount, sizeof (elf32_arm_section_map),
4205 elf32_arm_compare_mapping);
4206
4207 offset = sec->output_section->vma + sec->output_offset;
4208 ptr = map[0].vma - offset;
4209 for (i = 0; i < mapcount; i++)
4210 {
4211 if (i == mapcount - 1)
4212 end = sec->size;
4213 else
4214 end = map[i + 1].vma - offset;
4215
4216 switch (map[i].type)
4217 {
4218 case 'a':
4219 /* Byte swap code words. */
4220 while (ptr + 3 < end)
4221 {
4222 tmp = contents[ptr];
4223 contents[ptr] = contents[ptr + 3];
4224 contents[ptr + 3] = tmp;
4225 tmp = contents[ptr + 1];
4226 contents[ptr + 1] = contents[ptr + 2];
4227 contents[ptr + 2] = tmp;
4228 ptr += 4;
4229 }
4230 break;
4231
4232 case 't':
4233 /* Byte swap code halfwords. */
4234 while (ptr + 1 < end)
4235 {
4236 tmp = contents[ptr];
4237 contents[ptr] = contents[ptr + 1];
4238 contents[ptr + 1] = tmp;
4239 ptr += 2;
4240 }
4241 break;
4242
4243 case 'd':
4244 /* Leave data alone. */
4245 break;
4246 }
4247 ptr = end;
4248 }
4249 free (map);
4250 return FALSE;
4251 }
4252
4253 #define ELF_ARCH bfd_arch_arm
4254 #define ELF_MACHINE_CODE EM_ARM
4255 #ifdef __QNXTARGET__
4256 #define ELF_MAXPAGESIZE 0x1000
4257 #else
4258 #define ELF_MAXPAGESIZE 0x8000
4259 #endif
4260
4261 #define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
4262 #define bfd_elf32_bfd_merge_private_bfd_data elf32_arm_merge_private_bfd_data
4263 #define bfd_elf32_bfd_set_private_flags elf32_arm_set_private_flags
4264 #define bfd_elf32_bfd_print_private_bfd_data elf32_arm_print_private_bfd_data
4265 #define bfd_elf32_bfd_link_hash_table_create elf32_arm_link_hash_table_create
4266 #define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup
4267 #define bfd_elf32_find_nearest_line elf32_arm_find_nearest_line
4268 #define bfd_elf32_new_section_hook elf32_arm_new_section_hook
4269
4270 #define elf_backend_get_symbol_type elf32_arm_get_symbol_type
4271 #define elf_backend_gc_mark_hook elf32_arm_gc_mark_hook
4272 #define elf_backend_gc_sweep_hook elf32_arm_gc_sweep_hook
4273 #define elf_backend_check_relocs elf32_arm_check_relocs
4274 #define elf_backend_relocate_section elf32_arm_relocate_section
4275 #define elf_backend_write_section elf32_arm_write_section
4276 #define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
4277 #define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
4278 #define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
4279 #define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
4280 #define elf_backend_link_output_symbol_hook elf32_arm_output_symbol_hook
4281 #define elf_backend_size_dynamic_sections elf32_arm_size_dynamic_sections
4282 #define elf_backend_post_process_headers elf32_arm_post_process_headers
4283 #define elf_backend_reloc_type_class elf32_arm_reloc_type_class
4284 #define elf_backend_object_p elf32_arm_object_p
4285 #define elf_backend_section_flags elf32_arm_section_flags
4286 #define elf_backend_final_write_processing elf32_arm_final_write_processing
4287 #define elf_backend_copy_indirect_symbol elf32_arm_copy_indirect_symbol
4288
4289 #define elf_backend_can_refcount 1
4290 #define elf_backend_can_gc_sections 1
4291 #define elf_backend_plt_readonly 1
4292 #define elf_backend_want_got_plt 1
4293 #define elf_backend_want_plt_sym 0
4294 #if !USE_REL
4295 #define elf_backend_rela_normal 1
4296 #endif
4297
4298 #define elf_backend_got_header_size 12
4299
4300 #include "elf32-target.h"
4301
This page took 0.118025 seconds and 4 git commands to generate.