* mmo.c (File Layout): Correct place of misplaced blurb about
[binutils.git] / bfd / elf32-arm.h
blob0d9ae49ca30a90bea1e453ef38e039349e0324af
1 /* 32-bit ELF support for ARM
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #ifndef USE_REL
22 #define USE_REL 0
23 #endif
25 typedef unsigned long int insn32;
26 typedef unsigned short int insn16;
28 /* In lieu of proper flags, assume all EABIv4 objects are interworkable. */
29 #define INTERWORK_FLAG(abfd) \
30 (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) == EF_ARM_EABI_VER4 \
31 || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
33 /* The linker script knows the section names for placement.
34 The entry_names are used to do simple name mangling on the stubs.
35 Given a function name, and its type, the stub can be found. The
36 name can be changed. The only requirement is the %s be present. */
37 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
38 #define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb"
40 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
41 #define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm"
43 /* The name of the dynamic interpreter. This is put in the .interp
44 section. */
45 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
47 #ifdef FOUR_WORD_PLT
49 /* The first entry in a procedure linkage table looks like
50 this. It is set up so that any shared library function that is
51 called before the relocation has been set up calls the dynamic
52 linker first. */
53 static const bfd_vma elf32_arm_plt0_entry [] =
55 0xe52de004, /* str lr, [sp, #-4]! */
56 0xe59fe010, /* ldr lr, [pc, #16] */
57 0xe08fe00e, /* add lr, pc, lr */
58 0xe5bef008, /* ldr pc, [lr, #8]! */
61 /* Subsequent entries in a procedure linkage table look like
62 this. */
63 static const bfd_vma elf32_arm_plt_entry [] =
65 0xe28fc600, /* add ip, pc, #NN */
66 0xe28cca00, /* add ip, ip, #NN */
67 0xe5bcf000, /* ldr pc, [ip, #NN]! */
68 0x00000000, /* unused */
71 #else
73 /* The first entry in a procedure linkage table looks like
74 this. It is set up so that any shared library function that is
75 called before the relocation has been set up calls the dynamic
76 linker first. */
77 static const bfd_vma elf32_arm_plt0_entry [] =
79 0xe52de004, /* str lr, [sp, #-4]! */
80 0xe59fe004, /* ldr lr, [pc, #4] */
81 0xe08fe00e, /* add lr, pc, lr */
82 0xe5bef008, /* ldr pc, [lr, #8]! */
83 0x00000000, /* &GOT[0] - . */
86 /* Subsequent entries in a procedure linkage table look like
87 this. */
88 static const bfd_vma elf32_arm_plt_entry [] =
90 0xe28fc600, /* add ip, pc, #0xNN00000 */
91 0xe28cca00, /* add ip, ip, #0xNN000 */
92 0xe5bcf000, /* ldr pc, [ip, #0xNNN]! */
95 #endif
97 /* The entries in a PLT when using a DLL-based target with multiple
98 address spaces. */
99 static const bfd_vma elf32_arm_symbian_plt_entry [] =
101 0xe51ff004, /* ldr pr, [pc, #-4] */
102 0x00000000, /* dcd R_ARM_GLOB_DAT(X) */
105 /* Used to build a map of a section. This is required for mixed-endian
106 code/data. */
108 typedef struct elf32_elf_section_map
110 bfd_vma vma;
111 char type;
113 elf32_arm_section_map;
115 struct _arm_elf_section_data
117 struct bfd_elf_section_data elf;
118 int mapcount;
119 elf32_arm_section_map *map;
122 #define elf32_arm_section_data(sec) \
123 ((struct _arm_elf_section_data *) elf_section_data (sec))
125 /* The ARM linker needs to keep track of the number of relocs that it
126 decides to copy in check_relocs for each symbol. This is so that
127 it can discard PC relative relocs if it doesn't need them when
128 linking with -Bsymbolic. We store the information in a field
129 extending the regular ELF linker hash table. */
131 /* This structure keeps track of the number of PC relative relocs we
132 have copied for a given symbol. */
133 struct elf32_arm_relocs_copied
135 /* Next section. */
136 struct elf32_arm_relocs_copied * next;
137 /* A section in dynobj. */
138 asection * section;
139 /* Number of relocs copied in this section. */
140 bfd_size_type count;
143 /* Arm ELF linker hash entry. */
144 struct elf32_arm_link_hash_entry
146 struct elf_link_hash_entry root;
148 /* Number of PC relative relocs copied for this symbol. */
149 struct elf32_arm_relocs_copied * relocs_copied;
152 /* Traverse an arm ELF linker hash table. */
153 #define elf32_arm_link_hash_traverse(table, func, info) \
154 (elf_link_hash_traverse \
155 (&(table)->root, \
156 (bfd_boolean (*) (struct elf_link_hash_entry *, void *))) (func), \
157 (info)))
159 /* Get the ARM elf linker hash table from a link_info structure. */
160 #define elf32_arm_hash_table(info) \
161 ((struct elf32_arm_link_hash_table *) ((info)->hash))
163 /* ARM ELF linker hash table. */
164 struct elf32_arm_link_hash_table
166 /* The main hash table. */
167 struct elf_link_hash_table root;
169 /* The size in bytes of the section containing the Thumb-to-ARM glue. */
170 bfd_size_type thumb_glue_size;
172 /* The size in bytes of the section containing the ARM-to-Thumb glue. */
173 bfd_size_type arm_glue_size;
175 /* An arbitrary input BFD chosen to hold the glue sections. */
176 bfd * bfd_of_glue_owner;
178 /* A boolean indicating whether knowledge of the ARM's pipeline
179 length should be applied by the linker. */
180 int no_pipeline_knowledge;
182 /* Nonzero to output a BE8 image. */
183 int byteswap_code;
185 /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
186 Nonzero if R_ARM_TARGET1 means R_ARM_ABS32. */
187 int target1_is_rel;
189 /* The relocation to use for R_ARM_TARGET2 relocations. */
190 int target2_reloc;
192 /* The number of bytes in the initial entry in the PLT. */
193 bfd_size_type plt_header_size;
195 /* The number of bytes in the subsequent PLT etries. */
196 bfd_size_type plt_entry_size;
198 /* True if the target system is Symbian OS. */
199 int symbian_p;
201 /* Short-cuts to get to dynamic linker sections. */
202 asection *sgot;
203 asection *sgotplt;
204 asection *srelgot;
205 asection *splt;
206 asection *srelplt;
207 asection *sdynbss;
208 asection *srelbss;
210 /* Small local sym to section mapping cache. */
211 struct sym_sec_cache sym_sec;
214 /* Create an entry in an ARM ELF linker hash table. */
216 static struct bfd_hash_entry *
217 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
218 struct bfd_hash_table * table,
219 const char * string)
221 struct elf32_arm_link_hash_entry * ret =
222 (struct elf32_arm_link_hash_entry *) entry;
224 /* Allocate the structure if it has not already been allocated by a
225 subclass. */
226 if (ret == (struct elf32_arm_link_hash_entry *) NULL)
227 ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
228 if (ret == NULL)
229 return (struct bfd_hash_entry *) ret;
231 /* Call the allocation method of the superclass. */
232 ret = ((struct elf32_arm_link_hash_entry *)
233 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
234 table, string));
235 if (ret != NULL)
236 ret->relocs_copied = NULL;
238 return (struct bfd_hash_entry *) ret;
241 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
242 shortcuts to them in our hash table. */
244 static bfd_boolean
245 create_got_section (bfd *dynobj, struct bfd_link_info *info)
247 struct elf32_arm_link_hash_table *htab;
249 htab = elf32_arm_hash_table (info);
250 /* BPABI objects never have a GOT, or associated sections. */
251 if (htab->symbian_p)
252 return TRUE;
254 if (! _bfd_elf_create_got_section (dynobj, info))
255 return FALSE;
257 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
258 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
259 if (!htab->sgot || !htab->sgotplt)
260 abort ();
262 htab->srelgot = bfd_make_section (dynobj, ".rel.got");
263 if (htab->srelgot == NULL
264 || ! bfd_set_section_flags (dynobj, htab->srelgot,
265 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
266 | SEC_IN_MEMORY | SEC_LINKER_CREATED
267 | SEC_READONLY))
268 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
269 return FALSE;
270 return TRUE;
273 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
274 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
275 hash table. */
277 static bfd_boolean
278 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
280 struct elf32_arm_link_hash_table *htab;
282 htab = elf32_arm_hash_table (info);
283 if (!htab->sgot && !create_got_section (dynobj, info))
284 return FALSE;
286 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
287 return FALSE;
289 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
290 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
291 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
292 if (!info->shared)
293 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
295 if (!htab->splt
296 || !htab->srelplt
297 || !htab->sdynbss
298 || (!info->shared && !htab->srelbss))
299 abort ();
301 return TRUE;
304 /* Copy the extra info we tack onto an elf_link_hash_entry. */
306 static void
307 elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
308 struct elf_link_hash_entry *dir,
309 struct elf_link_hash_entry *ind)
311 struct elf32_arm_link_hash_entry *edir, *eind;
313 edir = (struct elf32_arm_link_hash_entry *) dir;
314 eind = (struct elf32_arm_link_hash_entry *) ind;
316 if (eind->relocs_copied != NULL)
318 if (edir->relocs_copied != NULL)
320 struct elf32_arm_relocs_copied **pp;
321 struct elf32_arm_relocs_copied *p;
323 if (ind->root.type == bfd_link_hash_indirect)
324 abort ();
326 /* Add reloc counts against the weak sym to the strong sym
327 list. Merge any entries against the same section. */
328 for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
330 struct elf32_arm_relocs_copied *q;
332 for (q = edir->relocs_copied; q != NULL; q = q->next)
333 if (q->section == p->section)
335 q->count += p->count;
336 *pp = p->next;
337 break;
339 if (q == NULL)
340 pp = &p->next;
342 *pp = edir->relocs_copied;
345 edir->relocs_copied = eind->relocs_copied;
346 eind->relocs_copied = NULL;
349 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
352 /* Create an ARM elf linker hash table. */
354 static struct bfd_link_hash_table *
355 elf32_arm_link_hash_table_create (bfd *abfd)
357 struct elf32_arm_link_hash_table *ret;
358 bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
360 ret = bfd_malloc (amt);
361 if (ret == NULL)
362 return NULL;
364 if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
365 elf32_arm_link_hash_newfunc))
367 free (ret);
368 return NULL;
371 ret->sgot = NULL;
372 ret->sgotplt = NULL;
373 ret->srelgot = NULL;
374 ret->splt = NULL;
375 ret->srelplt = NULL;
376 ret->sdynbss = NULL;
377 ret->srelbss = NULL;
378 ret->thumb_glue_size = 0;
379 ret->arm_glue_size = 0;
380 ret->bfd_of_glue_owner = NULL;
381 ret->no_pipeline_knowledge = 0;
382 ret->byteswap_code = 0;
383 ret->target1_is_rel = 0;
384 ret->target2_reloc = R_ARM_NONE;
385 #ifdef FOUR_WORD_PLT
386 ret->plt_header_size = 16;
387 ret->plt_entry_size = 16;
388 #else
389 ret->plt_header_size = 20;
390 ret->plt_entry_size = 12;
391 #endif
392 ret->symbian_p = 0;
393 ret->sym_sec.abfd = NULL;
395 return &ret->root.root;
398 /* Locate the Thumb encoded calling stub for NAME. */
400 static struct elf_link_hash_entry *
401 find_thumb_glue (struct bfd_link_info *link_info,
402 const char *name,
403 bfd *input_bfd)
405 char *tmp_name;
406 struct elf_link_hash_entry *hash;
407 struct elf32_arm_link_hash_table *hash_table;
409 /* We need a pointer to the armelf specific hash table. */
410 hash_table = elf32_arm_hash_table (link_info);
412 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
413 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
415 BFD_ASSERT (tmp_name);
417 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
419 hash = elf_link_hash_lookup
420 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
422 if (hash == NULL)
423 /* xgettext:c-format */
424 (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
425 input_bfd, tmp_name, name);
427 free (tmp_name);
429 return hash;
432 /* Locate the ARM encoded calling stub for NAME. */
434 static struct elf_link_hash_entry *
435 find_arm_glue (struct bfd_link_info *link_info,
436 const char *name,
437 bfd *input_bfd)
439 char *tmp_name;
440 struct elf_link_hash_entry *myh;
441 struct elf32_arm_link_hash_table *hash_table;
443 /* We need a pointer to the elfarm specific hash table. */
444 hash_table = elf32_arm_hash_table (link_info);
446 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
447 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
449 BFD_ASSERT (tmp_name);
451 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
453 myh = elf_link_hash_lookup
454 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
456 if (myh == NULL)
457 /* xgettext:c-format */
458 (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
459 input_bfd, tmp_name, name);
461 free (tmp_name);
463 return myh;
466 /* ARM->Thumb glue:
468 .arm
469 __func_from_arm:
470 ldr r12, __func_addr
471 bx r12
472 __func_addr:
473 .word func @ behave as if you saw a ARM_32 reloc. */
475 #define ARM2THUMB_GLUE_SIZE 12
476 static const insn32 a2t1_ldr_insn = 0xe59fc000;
477 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
478 static const insn32 a2t3_func_addr_insn = 0x00000001;
480 /* Thumb->ARM: Thumb->(non-interworking aware) ARM
482 .thumb .thumb
483 .align 2 .align 2
484 __func_from_thumb: __func_from_thumb:
485 bx pc push {r6, lr}
486 nop ldr r6, __func_addr
487 .arm mov lr, pc
488 __func_change_to_arm: bx r6
489 b func .arm
490 __func_back_to_thumb:
491 ldmia r13! {r6, lr}
492 bx lr
493 __func_addr:
494 .word func */
496 #define THUMB2ARM_GLUE_SIZE 8
497 static const insn16 t2a1_bx_pc_insn = 0x4778;
498 static const insn16 t2a2_noop_insn = 0x46c0;
499 static const insn32 t2a3_b_insn = 0xea000000;
501 #ifndef ELFARM_NABI_C_INCLUDED
502 bfd_boolean
503 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
505 asection * s;
506 bfd_byte * foo;
507 struct elf32_arm_link_hash_table * globals;
509 globals = elf32_arm_hash_table (info);
511 BFD_ASSERT (globals != NULL);
513 if (globals->arm_glue_size != 0)
515 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
517 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
518 ARM2THUMB_GLUE_SECTION_NAME);
520 BFD_ASSERT (s != NULL);
522 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
524 s->size = globals->arm_glue_size;
525 s->contents = foo;
528 if (globals->thumb_glue_size != 0)
530 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
532 s = bfd_get_section_by_name
533 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
535 BFD_ASSERT (s != NULL);
537 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
539 s->size = globals->thumb_glue_size;
540 s->contents = foo;
543 return TRUE;
546 static void
547 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
548 struct elf_link_hash_entry * h)
550 const char * name = h->root.root.string;
551 asection * s;
552 char * tmp_name;
553 struct elf_link_hash_entry * myh;
554 struct bfd_link_hash_entry * bh;
555 struct elf32_arm_link_hash_table * globals;
556 bfd_vma val;
558 globals = elf32_arm_hash_table (link_info);
560 BFD_ASSERT (globals != NULL);
561 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
563 s = bfd_get_section_by_name
564 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
566 BFD_ASSERT (s != NULL);
568 tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
570 BFD_ASSERT (tmp_name);
572 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
574 myh = elf_link_hash_lookup
575 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
577 if (myh != NULL)
579 /* We've already seen this guy. */
580 free (tmp_name);
581 return;
584 /* The only trick here is using hash_table->arm_glue_size as the value.
585 Even though the section isn't allocated yet, this is where we will be
586 putting it. */
587 bh = NULL;
588 val = globals->arm_glue_size + 1;
589 _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
590 tmp_name, BSF_GLOBAL, s, val,
591 NULL, TRUE, FALSE, &bh);
593 free (tmp_name);
595 globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
597 return;
600 static void
601 record_thumb_to_arm_glue (struct bfd_link_info *link_info,
602 struct elf_link_hash_entry *h)
604 const char *name = h->root.root.string;
605 asection *s;
606 char *tmp_name;
607 struct elf_link_hash_entry *myh;
608 struct bfd_link_hash_entry *bh;
609 struct elf32_arm_link_hash_table *hash_table;
610 char bind;
611 bfd_vma val;
613 hash_table = elf32_arm_hash_table (link_info);
615 BFD_ASSERT (hash_table != NULL);
616 BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
618 s = bfd_get_section_by_name
619 (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
621 BFD_ASSERT (s != NULL);
623 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
624 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
626 BFD_ASSERT (tmp_name);
628 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
630 myh = elf_link_hash_lookup
631 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
633 if (myh != NULL)
635 /* We've already seen this guy. */
636 free (tmp_name);
637 return;
640 bh = NULL;
641 val = hash_table->thumb_glue_size + 1;
642 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
643 tmp_name, BSF_GLOBAL, s, val,
644 NULL, TRUE, FALSE, &bh);
646 /* If we mark it 'Thumb', the disassembler will do a better job. */
647 myh = (struct elf_link_hash_entry *) bh;
648 bind = ELF_ST_BIND (myh->type);
649 myh->type = ELF_ST_INFO (bind, STT_ARM_TFUNC);
651 free (tmp_name);
653 #define CHANGE_TO_ARM "__%s_change_to_arm"
654 #define BACK_FROM_ARM "__%s_back_from_arm"
656 /* Allocate another symbol to mark where we switch to Arm mode. */
657 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
658 + strlen (CHANGE_TO_ARM) + 1);
660 BFD_ASSERT (tmp_name);
662 sprintf (tmp_name, CHANGE_TO_ARM, name);
664 bh = NULL;
665 val = hash_table->thumb_glue_size + 4,
666 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
667 tmp_name, BSF_LOCAL, s, val,
668 NULL, TRUE, FALSE, &bh);
670 free (tmp_name);
672 hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
674 return;
677 /* Add the glue sections to ABFD. This function is called from the
678 linker scripts in ld/emultempl/{armelf}.em. */
680 bfd_boolean
681 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
682 struct bfd_link_info *info)
684 flagword flags;
685 asection *sec;
687 /* If we are only performing a partial
688 link do not bother adding the glue. */
689 if (info->relocatable)
690 return TRUE;
692 sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
694 if (sec == NULL)
696 /* Note: we do not include the flag SEC_LINKER_CREATED, as this
697 will prevent elf_link_input_bfd() from processing the contents
698 of this section. */
699 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
701 sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
703 if (sec == NULL
704 || !bfd_set_section_flags (abfd, sec, flags)
705 || !bfd_set_section_alignment (abfd, sec, 2))
706 return FALSE;
708 /* Set the gc mark to prevent the section from being removed by garbage
709 collection, despite the fact that no relocs refer to this section. */
710 sec->gc_mark = 1;
713 sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
715 if (sec == NULL)
717 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
718 | SEC_CODE | SEC_READONLY;
720 sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
722 if (sec == NULL
723 || !bfd_set_section_flags (abfd, sec, flags)
724 || !bfd_set_section_alignment (abfd, sec, 2))
725 return FALSE;
727 sec->gc_mark = 1;
730 return TRUE;
733 /* Select a BFD to be used to hold the sections used by the glue code.
734 This function is called from the linker scripts in ld/emultempl/
735 {armelf/pe}.em */
737 bfd_boolean
738 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
740 struct elf32_arm_link_hash_table *globals;
742 /* If we are only performing a partial link
743 do not bother getting a bfd to hold the glue. */
744 if (info->relocatable)
745 return TRUE;
747 globals = elf32_arm_hash_table (info);
749 BFD_ASSERT (globals != NULL);
751 if (globals->bfd_of_glue_owner != NULL)
752 return TRUE;
754 /* Save the bfd for later use. */
755 globals->bfd_of_glue_owner = abfd;
757 return TRUE;
760 bfd_boolean
761 bfd_elf32_arm_process_before_allocation (bfd *abfd,
762 struct bfd_link_info *link_info,
763 int no_pipeline_knowledge,
764 int byteswap_code)
766 Elf_Internal_Shdr *symtab_hdr;
767 Elf_Internal_Rela *internal_relocs = NULL;
768 Elf_Internal_Rela *irel, *irelend;
769 bfd_byte *contents = NULL;
771 asection *sec;
772 struct elf32_arm_link_hash_table *globals;
774 /* If we are only performing a partial link do not bother
775 to construct any glue. */
776 if (link_info->relocatable)
777 return TRUE;
779 /* Here we have a bfd that is to be included on the link. We have a hook
780 to do reloc rummaging, before section sizes are nailed down. */
781 globals = elf32_arm_hash_table (link_info);
783 BFD_ASSERT (globals != NULL);
784 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
786 globals->no_pipeline_knowledge = no_pipeline_knowledge;
788 if (byteswap_code && !bfd_big_endian (abfd))
790 _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
791 abfd);
792 return FALSE;
794 globals->byteswap_code = byteswap_code;
796 /* Rummage around all the relocs and map the glue vectors. */
797 sec = abfd->sections;
799 if (sec == NULL)
800 return TRUE;
802 for (; sec != NULL; sec = sec->next)
804 if (sec->reloc_count == 0)
805 continue;
807 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
809 /* Load the relocs. */
810 internal_relocs
811 = _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
812 (Elf_Internal_Rela *) NULL, FALSE);
814 if (internal_relocs == NULL)
815 goto error_return;
817 irelend = internal_relocs + sec->reloc_count;
818 for (irel = internal_relocs; irel < irelend; irel++)
820 long r_type;
821 unsigned long r_index;
823 struct elf_link_hash_entry *h;
825 r_type = ELF32_R_TYPE (irel->r_info);
826 r_index = ELF32_R_SYM (irel->r_info);
828 /* These are the only relocation types we care about. */
829 if ( r_type != R_ARM_PC24
830 && r_type != R_ARM_THM_PC22)
831 continue;
833 /* Get the section contents if we haven't done so already. */
834 if (contents == NULL)
836 /* Get cached copy if it exists. */
837 if (elf_section_data (sec)->this_hdr.contents != NULL)
838 contents = elf_section_data (sec)->this_hdr.contents;
839 else
841 /* Go get them off disk. */
842 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
843 goto error_return;
847 /* If the relocation is not against a symbol it cannot concern us. */
848 h = NULL;
850 /* We don't care about local symbols. */
851 if (r_index < symtab_hdr->sh_info)
852 continue;
854 /* This is an external symbol. */
855 r_index -= symtab_hdr->sh_info;
856 h = (struct elf_link_hash_entry *)
857 elf_sym_hashes (abfd)[r_index];
859 /* If the relocation is against a static symbol it must be within
860 the current section and so cannot be a cross ARM/Thumb relocation. */
861 if (h == NULL)
862 continue;
864 switch (r_type)
866 case R_ARM_PC24:
867 /* This one is a call from arm code. We need to look up
868 the target of the call. If it is a thumb target, we
869 insert glue. */
870 if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
871 record_arm_to_thumb_glue (link_info, h);
872 break;
874 case R_ARM_THM_PC22:
875 /* This one is a call from thumb code. We look
876 up the target of the call. If it is not a thumb
877 target, we insert glue. */
878 if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
879 record_thumb_to_arm_glue (link_info, h);
880 break;
882 default:
883 break;
887 if (contents != NULL
888 && elf_section_data (sec)->this_hdr.contents != contents)
889 free (contents);
890 contents = NULL;
892 if (internal_relocs != NULL
893 && elf_section_data (sec)->relocs != internal_relocs)
894 free (internal_relocs);
895 internal_relocs = NULL;
898 return TRUE;
900 error_return:
901 if (contents != NULL
902 && elf_section_data (sec)->this_hdr.contents != contents)
903 free (contents);
904 if (internal_relocs != NULL
905 && elf_section_data (sec)->relocs != internal_relocs)
906 free (internal_relocs);
908 return FALSE;
910 #endif
913 #ifndef OLD_ARM_ABI
914 /* Set target relocation values needed during linking. */
916 void
917 bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
918 int target1_is_rel,
919 char * target2_type)
921 struct elf32_arm_link_hash_table *globals;
923 globals = elf32_arm_hash_table (link_info);
925 globals->target1_is_rel = target1_is_rel;
926 if (strcmp (target2_type, "rel") == 0)
927 globals->target2_reloc = R_ARM_REL32;
928 else if (strcmp (target2_type, "abs") == 0)
929 globals->target2_reloc = R_ARM_ABS32;
930 else if (strcmp (target2_type, "got-rel") == 0)
931 globals->target2_reloc = R_ARM_GOT_PREL;
932 else
934 _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
935 target2_type);
938 #endif
940 /* The thumb form of a long branch is a bit finicky, because the offset
941 encoding is split over two fields, each in it's own instruction. They
942 can occur in any order. So given a thumb form of long branch, and an
943 offset, insert the offset into the thumb branch and return finished
944 instruction.
946 It takes two thumb instructions to encode the target address. Each has
947 11 bits to invest. The upper 11 bits are stored in one (identified by
948 H-0.. see below), the lower 11 bits are stored in the other (identified
949 by H-1).
951 Combine together and shifted left by 1 (it's a half word address) and
952 there you have it.
954 Op: 1111 = F,
955 H-0, upper address-0 = 000
956 Op: 1111 = F,
957 H-1, lower address-0 = 800
959 They can be ordered either way, but the arm tools I've seen always put
960 the lower one first. It probably doesn't matter. krk@cygnus.com
962 XXX: Actually the order does matter. The second instruction (H-1)
963 moves the computed address into the PC, so it must be the second one
964 in the sequence. The problem, however is that whilst little endian code
965 stores the instructions in HI then LOW order, big endian code does the
966 reverse. nickc@cygnus.com. */
968 #define LOW_HI_ORDER 0xF800F000
969 #define HI_LOW_ORDER 0xF000F800
971 static insn32
972 insert_thumb_branch (insn32 br_insn, int rel_off)
974 unsigned int low_bits;
975 unsigned int high_bits;
977 BFD_ASSERT ((rel_off & 1) != 1);
979 rel_off >>= 1; /* Half word aligned address. */
980 low_bits = rel_off & 0x000007FF; /* The bottom 11 bits. */
981 high_bits = (rel_off >> 11) & 0x000007FF; /* The top 11 bits. */
983 if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
984 br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
985 else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
986 br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
987 else
988 /* FIXME: abort is probably not the right call. krk@cygnus.com */
989 abort (); /* Error - not a valid branch instruction form. */
991 return br_insn;
994 /* Thumb code calling an ARM function. */
996 static int
997 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
998 const char * name,
999 bfd * input_bfd,
1000 bfd * output_bfd,
1001 asection * input_section,
1002 bfd_byte * hit_data,
1003 asection * sym_sec,
1004 bfd_vma offset,
1005 bfd_signed_vma addend,
1006 bfd_vma val)
1008 asection * s = 0;
1009 bfd_vma my_offset;
1010 unsigned long int tmp;
1011 long int ret_offset;
1012 struct elf_link_hash_entry * myh;
1013 struct elf32_arm_link_hash_table * globals;
1015 myh = find_thumb_glue (info, name, input_bfd);
1016 if (myh == NULL)
1017 return FALSE;
1019 globals = elf32_arm_hash_table (info);
1021 BFD_ASSERT (globals != NULL);
1022 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1024 my_offset = myh->root.u.def.value;
1026 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1027 THUMB2ARM_GLUE_SECTION_NAME);
1029 BFD_ASSERT (s != NULL);
1030 BFD_ASSERT (s->contents != NULL);
1031 BFD_ASSERT (s->output_section != NULL);
1033 if ((my_offset & 0x01) == 0x01)
1035 if (sym_sec != NULL
1036 && sym_sec->owner != NULL
1037 && !INTERWORK_FLAG (sym_sec->owner))
1039 (*_bfd_error_handler)
1040 (_("%B(%s): warning: interworking not enabled.\n"
1041 " first occurrence: %B: thumb call to arm"),
1042 sym_sec->owner, input_bfd, name);
1044 return FALSE;
1047 --my_offset;
1048 myh->root.u.def.value = my_offset;
1050 bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
1051 s->contents + my_offset);
1053 bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
1054 s->contents + my_offset + 2);
1056 ret_offset =
1057 /* Address of destination of the stub. */
1058 ((bfd_signed_vma) val)
1059 - ((bfd_signed_vma)
1060 /* Offset from the start of the current section
1061 to the start of the stubs. */
1062 (s->output_offset
1063 /* Offset of the start of this stub from the start of the stubs. */
1064 + my_offset
1065 /* Address of the start of the current section. */
1066 + s->output_section->vma)
1067 /* The branch instruction is 4 bytes into the stub. */
1069 /* ARM branches work from the pc of the instruction + 8. */
1070 + 8);
1072 bfd_put_32 (output_bfd,
1073 (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
1074 s->contents + my_offset + 4);
1077 BFD_ASSERT (my_offset <= globals->thumb_glue_size);
1079 /* Now go back and fix up the original BL insn to point to here. */
1080 ret_offset =
1081 /* Address of where the stub is located. */
1082 (s->output_section->vma + s->output_offset + my_offset)
1083 /* Address of where the BL is located. */
1084 - (input_section->output_section->vma + input_section->output_offset
1085 + offset)
1086 /* Addend in the relocation. */
1087 - addend
1088 /* Biassing for PC-relative addressing. */
1089 - 8;
1091 tmp = bfd_get_32 (input_bfd, hit_data
1092 - input_section->vma);
1094 bfd_put_32 (output_bfd,
1095 (bfd_vma) insert_thumb_branch (tmp, ret_offset),
1096 hit_data - input_section->vma);
1098 return TRUE;
1101 /* Arm code calling a Thumb function. */
1103 static int
1104 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
1105 const char * name,
1106 bfd * input_bfd,
1107 bfd * output_bfd,
1108 asection * input_section,
1109 bfd_byte * hit_data,
1110 asection * sym_sec,
1111 bfd_vma offset,
1112 bfd_signed_vma addend,
1113 bfd_vma val)
1115 unsigned long int tmp;
1116 bfd_vma my_offset;
1117 asection * s;
1118 long int ret_offset;
1119 struct elf_link_hash_entry * myh;
1120 struct elf32_arm_link_hash_table * globals;
1122 myh = find_arm_glue (info, name, input_bfd);
1123 if (myh == NULL)
1124 return FALSE;
1126 globals = elf32_arm_hash_table (info);
1128 BFD_ASSERT (globals != NULL);
1129 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1131 my_offset = myh->root.u.def.value;
1132 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1133 ARM2THUMB_GLUE_SECTION_NAME);
1134 BFD_ASSERT (s != NULL);
1135 BFD_ASSERT (s->contents != NULL);
1136 BFD_ASSERT (s->output_section != NULL);
1138 if ((my_offset & 0x01) == 0x01)
1140 if (sym_sec != NULL
1141 && sym_sec->owner != NULL
1142 && !INTERWORK_FLAG (sym_sec->owner))
1144 (*_bfd_error_handler)
1145 (_("%B(%s): warning: interworking not enabled.\n"
1146 " first occurrence: %B: arm call to thumb"),
1147 sym_sec->owner, input_bfd, name);
1150 --my_offset;
1151 myh->root.u.def.value = my_offset;
1153 bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
1154 s->contents + my_offset);
1156 bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
1157 s->contents + my_offset + 4);
1159 /* It's a thumb address. Add the low order bit. */
1160 bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
1161 s->contents + my_offset + 8);
1164 BFD_ASSERT (my_offset <= globals->arm_glue_size);
1166 tmp = bfd_get_32 (input_bfd, hit_data);
1167 tmp = tmp & 0xFF000000;
1169 /* Somehow these are both 4 too far, so subtract 8. */
1170 ret_offset = (s->output_offset
1171 + my_offset
1172 + s->output_section->vma
1173 - (input_section->output_offset
1174 + input_section->output_section->vma
1175 + offset + addend)
1176 - 8);
1178 tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
1180 bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
1182 return TRUE;
1186 #ifndef OLD_ARM_ABI
1187 /* Some relocations map to different relocations depending on the
1188 target. Return the real relocation. */
1189 static int
1190 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
1191 int r_type)
1193 switch (r_type)
1195 case R_ARM_TARGET1:
1196 if (globals->target1_is_rel)
1197 return R_ARM_REL32;
1198 else
1199 return R_ARM_ABS32;
1201 case R_ARM_TARGET2:
1202 return globals->target2_reloc;
1204 default:
1205 return r_type;
1208 #endif /* OLD_ARM_ABI */
1211 /* Perform a relocation as part of a final link. */
1213 static bfd_reloc_status_type
1214 elf32_arm_final_link_relocate (reloc_howto_type * howto,
1215 bfd * input_bfd,
1216 bfd * output_bfd,
1217 asection * input_section,
1218 bfd_byte * contents,
1219 Elf_Internal_Rela * rel,
1220 bfd_vma value,
1221 struct bfd_link_info * info,
1222 asection * sym_sec,
1223 const char * sym_name,
1224 int sym_flags,
1225 struct elf_link_hash_entry * h)
1227 unsigned long r_type = howto->type;
1228 unsigned long r_symndx;
1229 bfd_byte * hit_data = contents + rel->r_offset;
1230 bfd * dynobj = NULL;
1231 Elf_Internal_Shdr * symtab_hdr;
1232 struct elf_link_hash_entry ** sym_hashes;
1233 bfd_vma * local_got_offsets;
1234 asection * sgot = NULL;
1235 asection * splt = NULL;
1236 asection * sreloc = NULL;
1237 bfd_vma addend;
1238 bfd_signed_vma signed_addend;
1239 struct elf32_arm_link_hash_table * globals;
1241 globals = elf32_arm_hash_table (info);
1243 #ifndef OLD_ARM_ABI
1244 /* Some relocation type map to different relocations depending on the
1245 target. We pick the right one here. */
1246 r_type = arm_real_reloc_type (globals, r_type);
1247 if (r_type != howto->type)
1248 howto = elf32_arm_howto_from_type (r_type);
1249 #endif /* OLD_ARM_ABI */
1251 /* If the start address has been set, then set the EF_ARM_HASENTRY
1252 flag. Setting this more than once is redundant, but the cost is
1253 not too high, and it keeps the code simple.
1255 The test is done here, rather than somewhere else, because the
1256 start address is only set just before the final link commences.
1258 Note - if the user deliberately sets a start address of 0, the
1259 flag will not be set. */
1260 if (bfd_get_start_address (output_bfd) != 0)
1261 elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
1263 dynobj = elf_hash_table (info)->dynobj;
1264 if (dynobj)
1266 sgot = bfd_get_section_by_name (dynobj, ".got");
1267 splt = bfd_get_section_by_name (dynobj, ".plt");
1269 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1270 sym_hashes = elf_sym_hashes (input_bfd);
1271 local_got_offsets = elf_local_got_offsets (input_bfd);
1272 r_symndx = ELF32_R_SYM (rel->r_info);
1274 #if USE_REL
1275 addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
1277 if (addend & ((howto->src_mask + 1) >> 1))
1279 signed_addend = -1;
1280 signed_addend &= ~ howto->src_mask;
1281 signed_addend |= addend;
1283 else
1284 signed_addend = addend;
1285 #else
1286 addend = signed_addend = rel->r_addend;
1287 #endif
1289 switch (r_type)
1291 case R_ARM_NONE:
1292 return bfd_reloc_ok;
1294 case R_ARM_PC24:
1295 case R_ARM_ABS32:
1296 case R_ARM_REL32:
1297 #ifndef OLD_ARM_ABI
1298 case R_ARM_XPC25:
1299 case R_ARM_PREL31:
1300 #endif
1301 case R_ARM_PLT32:
1302 /* r_symndx will be zero only for relocs against symbols
1303 from removed linkonce sections, or sections discarded by
1304 a linker script. */
1305 if (r_symndx == 0)
1306 return bfd_reloc_ok;
1308 /* Handle relocations which should use the PLT entry. ABS32/REL32
1309 will use the symbol's value, which may point to a PLT entry, but we
1310 don't need to handle that here. If we created a PLT entry, all
1311 branches in this object should go to it. */
1312 if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
1313 #ifndef OLD_ARM_ABI
1314 && r_type != R_ARM_PREL31
1315 #endif
1317 && h != NULL
1318 && splt != NULL
1319 && h->plt.offset != (bfd_vma) -1)
1321 /* If we've created a .plt section, and assigned a PLT entry to
1322 this function, it should not be known to bind locally. If
1323 it were, we would have cleared the PLT entry. */
1324 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1326 value = (splt->output_section->vma
1327 + splt->output_offset
1328 + h->plt.offset);
1329 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1330 contents, rel->r_offset, value,
1331 (bfd_vma) 0);
1334 /* When generating a shared object, these relocations are copied
1335 into the output file to be resolved at run time. */
1336 if (info->shared
1337 && (input_section->flags & SEC_ALLOC)
1338 && ((r_type != R_ARM_REL32
1339 #ifndef OLD_ARM_ABI
1340 && r_type != R_ARM_PREL31
1341 #endif
1342 ) || !SYMBOL_CALLS_LOCAL (info, h))
1343 && (h == NULL
1344 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1345 || h->root.type != bfd_link_hash_undefweak)
1346 && r_type != R_ARM_PC24
1347 && r_type != R_ARM_PLT32)
1349 Elf_Internal_Rela outrel;
1350 bfd_byte *loc;
1351 bfd_boolean skip, relocate;
1353 if (sreloc == NULL)
1355 const char * name;
1357 name = (bfd_elf_string_from_elf_section
1358 (input_bfd,
1359 elf_elfheader (input_bfd)->e_shstrndx,
1360 elf_section_data (input_section)->rel_hdr.sh_name));
1361 if (name == NULL)
1362 return bfd_reloc_notsupported;
1364 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
1365 && strcmp (bfd_get_section_name (input_bfd,
1366 input_section),
1367 name + 4) == 0);
1369 sreloc = bfd_get_section_by_name (dynobj, name);
1370 BFD_ASSERT (sreloc != NULL);
1373 skip = FALSE;
1374 relocate = FALSE;
1376 outrel.r_offset =
1377 _bfd_elf_section_offset (output_bfd, info, input_section,
1378 rel->r_offset);
1379 if (outrel.r_offset == (bfd_vma) -1)
1380 skip = TRUE;
1381 else if (outrel.r_offset == (bfd_vma) -2)
1382 skip = TRUE, relocate = TRUE;
1383 outrel.r_offset += (input_section->output_section->vma
1384 + input_section->output_offset);
1386 if (skip)
1387 memset (&outrel, 0, sizeof outrel);
1388 else if (h != NULL
1389 && h->dynindx != -1
1390 && (!info->shared
1391 || !info->symbolic
1392 || !h->def_regular))
1393 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1394 else
1396 /* This symbol is local, or marked to become local. */
1397 relocate = TRUE;
1398 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1401 loc = sreloc->contents;
1402 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
1403 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1405 /* If this reloc is against an external symbol, we do not want to
1406 fiddle with the addend. Otherwise, we need to include the symbol
1407 value so that it becomes an addend for the dynamic reloc. */
1408 if (! relocate)
1409 return bfd_reloc_ok;
1411 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1412 contents, rel->r_offset, value,
1413 (bfd_vma) 0);
1415 else switch (r_type)
1417 #ifndef OLD_ARM_ABI
1418 case R_ARM_XPC25: /* Arm BLX instruction. */
1419 #endif
1420 case R_ARM_PC24: /* Arm B/BL instruction */
1421 case R_ARM_PLT32:
1422 #ifndef OLD_ARM_ABI
1423 if (r_type == R_ARM_XPC25)
1425 /* Check for Arm calling Arm function. */
1426 /* FIXME: Should we translate the instruction into a BL
1427 instruction instead ? */
1428 if (sym_flags != STT_ARM_TFUNC)
1429 (*_bfd_error_handler)
1430 (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
1431 input_bfd,
1432 h ? h->root.root.string : "(local)");
1434 else
1435 #endif
1437 /* Check for Arm calling Thumb function. */
1438 if (sym_flags == STT_ARM_TFUNC)
1440 elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
1441 output_bfd, input_section,
1442 hit_data, sym_sec, rel->r_offset,
1443 signed_addend, value);
1444 return bfd_reloc_ok;
1448 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1449 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0)
1451 /* The old way of doing things. Trearing the addend as a
1452 byte sized field and adding in the pipeline offset. */
1453 value -= (input_section->output_section->vma
1454 + input_section->output_offset);
1455 value -= rel->r_offset;
1456 value += addend;
1458 if (! globals->no_pipeline_knowledge)
1459 value -= 8;
1461 else
1463 /* The ARM ELF ABI says that this reloc is computed as: S - P + A
1464 where:
1465 S is the address of the symbol in the relocation.
1466 P is address of the instruction being relocated.
1467 A is the addend (extracted from the instruction) in bytes.
1469 S is held in 'value'.
1470 P is the base address of the section containing the
1471 instruction plus the offset of the reloc into that
1472 section, ie:
1473 (input_section->output_section->vma +
1474 input_section->output_offset +
1475 rel->r_offset).
1476 A is the addend, converted into bytes, ie:
1477 (signed_addend * 4)
1479 Note: None of these operations have knowledge of the pipeline
1480 size of the processor, thus it is up to the assembler to
1481 encode this information into the addend. */
1482 value -= (input_section->output_section->vma
1483 + input_section->output_offset);
1484 value -= rel->r_offset;
1485 value += (signed_addend << howto->size);
1487 /* Previous versions of this code also used to add in the
1488 pipeline offset here. This is wrong because the linker is
1489 not supposed to know about such things, and one day it might
1490 change. In order to support old binaries that need the old
1491 behaviour however, so we attempt to detect which ABI was
1492 used to create the reloc. */
1493 if (! globals->no_pipeline_knowledge)
1495 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
1497 i_ehdrp = elf_elfheader (input_bfd);
1499 if (i_ehdrp->e_ident[EI_OSABI] == 0)
1500 value -= 8;
1504 signed_addend = value;
1505 signed_addend >>= howto->rightshift;
1507 /* It is not an error for an undefined weak reference to be
1508 out of range. Any program that branches to such a symbol
1509 is going to crash anyway, so there is no point worrying
1510 about getting the destination exactly right. */
1511 if (! h || h->root.type != bfd_link_hash_undefweak)
1513 /* Perform a signed range check. */
1514 if ( signed_addend > ((bfd_signed_vma) (howto->dst_mask >> 1))
1515 || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
1516 return bfd_reloc_overflow;
1519 #ifndef OLD_ARM_ABI
1520 /* If necessary set the H bit in the BLX instruction. */
1521 if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
1522 value = (signed_addend & howto->dst_mask)
1523 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
1524 | (1 << 24);
1525 else
1526 #endif
1527 value = (signed_addend & howto->dst_mask)
1528 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
1529 break;
1531 case R_ARM_ABS32:
1532 value += addend;
1533 if (sym_flags == STT_ARM_TFUNC)
1534 value |= 1;
1535 break;
1537 case R_ARM_REL32:
1538 value -= (input_section->output_section->vma
1539 + input_section->output_offset + rel->r_offset);
1540 value += addend;
1541 break;
1543 #ifndef OLD_ARM_ABI
1544 case R_ARM_PREL31:
1545 value -= (input_section->output_section->vma
1546 + input_section->output_offset + rel->r_offset);
1547 value += signed_addend;
1548 if (! h || h->root.type != bfd_link_hash_undefweak)
1550 /* Check for overflow */
1551 if ((value ^ (value >> 1)) & (1 << 30))
1552 return bfd_reloc_overflow;
1554 value &= 0x7fffffff;
1555 value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
1556 if (sym_flags == STT_ARM_TFUNC)
1557 value |= 1;
1558 break;
1559 #endif
1562 bfd_put_32 (input_bfd, value, hit_data);
1563 return bfd_reloc_ok;
1565 case R_ARM_ABS8:
1566 value += addend;
1567 if ((long) value > 0x7f || (long) value < -0x80)
1568 return bfd_reloc_overflow;
1570 bfd_put_8 (input_bfd, value, hit_data);
1571 return bfd_reloc_ok;
1573 case R_ARM_ABS16:
1574 value += addend;
1576 if ((long) value > 0x7fff || (long) value < -0x8000)
1577 return bfd_reloc_overflow;
1579 bfd_put_16 (input_bfd, value, hit_data);
1580 return bfd_reloc_ok;
1582 case R_ARM_ABS12:
1583 /* Support ldr and str instruction for the arm */
1584 /* Also thumb b (unconditional branch). ??? Really? */
1585 value += addend;
1587 if ((long) value > 0x7ff || (long) value < -0x800)
1588 return bfd_reloc_overflow;
1590 value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
1591 bfd_put_32 (input_bfd, value, hit_data);
1592 return bfd_reloc_ok;
1594 case R_ARM_THM_ABS5:
1595 /* Support ldr and str instructions for the thumb. */
1596 #if USE_REL
1597 /* Need to refetch addend. */
1598 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1599 /* ??? Need to determine shift amount from operand size. */
1600 addend >>= howto->rightshift;
1601 #endif
1602 value += addend;
1604 /* ??? Isn't value unsigned? */
1605 if ((long) value > 0x1f || (long) value < -0x10)
1606 return bfd_reloc_overflow;
1608 /* ??? Value needs to be properly shifted into place first. */
1609 value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
1610 bfd_put_16 (input_bfd, value, hit_data);
1611 return bfd_reloc_ok;
1613 #ifndef OLD_ARM_ABI
1614 case R_ARM_THM_XPC22:
1615 #endif
1616 case R_ARM_THM_PC22:
1617 /* Thumb BL (branch long instruction). */
1619 bfd_vma relocation;
1620 bfd_boolean overflow = FALSE;
1621 bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
1622 bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
1623 bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
1624 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1625 bfd_vma check;
1626 bfd_signed_vma signed_check;
1628 #if USE_REL
1629 /* Need to refetch the addend and squish the two 11 bit pieces
1630 together. */
1632 bfd_vma upper = upper_insn & 0x7ff;
1633 bfd_vma lower = lower_insn & 0x7ff;
1634 upper = (upper ^ 0x400) - 0x400; /* Sign extend. */
1635 addend = (upper << 12) | (lower << 1);
1636 signed_addend = addend;
1638 #endif
1639 #ifndef OLD_ARM_ABI
1640 if (r_type == R_ARM_THM_XPC22)
1642 /* Check for Thumb to Thumb call. */
1643 /* FIXME: Should we translate the instruction into a BL
1644 instruction instead ? */
1645 if (sym_flags == STT_ARM_TFUNC)
1646 (*_bfd_error_handler)
1647 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
1648 input_bfd,
1649 h ? h->root.root.string : "(local)");
1651 else
1652 #endif
1654 /* If it is not a call to Thumb, assume call to Arm.
1655 If it is a call relative to a section name, then it is not a
1656 function call at all, but rather a long jump. */
1657 if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION)
1659 if (elf32_thumb_to_arm_stub
1660 (info, sym_name, input_bfd, output_bfd, input_section,
1661 hit_data, sym_sec, rel->r_offset, signed_addend, value))
1662 return bfd_reloc_ok;
1663 else
1664 return bfd_reloc_dangerous;
1668 relocation = value + signed_addend;
1670 relocation -= (input_section->output_section->vma
1671 + input_section->output_offset
1672 + rel->r_offset);
1674 if (! globals->no_pipeline_knowledge)
1676 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form. */
1678 i_ehdrp = elf_elfheader (input_bfd);
1680 /* Previous versions of this code also used to add in the pipline
1681 offset here. This is wrong because the linker is not supposed
1682 to know about such things, and one day it might change. In order
1683 to support old binaries that need the old behaviour however, so
1684 we attempt to detect which ABI was used to create the reloc. */
1685 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
1686 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0
1687 || i_ehdrp->e_ident[EI_OSABI] == 0)
1688 relocation += 4;
1691 check = relocation >> howto->rightshift;
1693 /* If this is a signed value, the rightshift just dropped
1694 leading 1 bits (assuming twos complement). */
1695 if ((bfd_signed_vma) relocation >= 0)
1696 signed_check = check;
1697 else
1698 signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
1700 /* Assumes two's complement. */
1701 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1702 overflow = TRUE;
1704 #ifndef OLD_ARM_ABI
1705 if (r_type == R_ARM_THM_XPC22
1706 && ((lower_insn & 0x1800) == 0x0800))
1707 /* For a BLX instruction, make sure that the relocation is rounded up
1708 to a word boundary. This follows the semantics of the instruction
1709 which specifies that bit 1 of the target address will come from bit
1710 1 of the base address. */
1711 relocation = (relocation + 2) & ~ 3;
1712 #endif
1713 /* Put RELOCATION back into the insn. */
1714 upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
1715 lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
1717 /* Put the relocated value back in the object file: */
1718 bfd_put_16 (input_bfd, upper_insn, hit_data);
1719 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
1721 return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
1723 break;
1725 case R_ARM_THM_PC11:
1726 /* Thumb B (branch) instruction). */
1728 bfd_signed_vma relocation;
1729 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1730 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1731 bfd_signed_vma signed_check;
1733 #if USE_REL
1734 /* Need to refetch addend. */
1735 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
1736 if (addend & ((howto->src_mask + 1) >> 1))
1738 signed_addend = -1;
1739 signed_addend &= ~ howto->src_mask;
1740 signed_addend |= addend;
1742 else
1743 signed_addend = addend;
1744 /* The value in the insn has been right shifted. We need to
1745 undo this, so that we can perform the address calculation
1746 in terms of bytes. */
1747 signed_addend <<= howto->rightshift;
1748 #endif
1749 relocation = value + signed_addend;
1751 relocation -= (input_section->output_section->vma
1752 + input_section->output_offset
1753 + rel->r_offset);
1755 relocation >>= howto->rightshift;
1756 signed_check = relocation;
1757 relocation &= howto->dst_mask;
1758 relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
1760 bfd_put_16 (input_bfd, relocation, hit_data);
1762 /* Assumes two's complement. */
1763 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
1764 return bfd_reloc_overflow;
1766 return bfd_reloc_ok;
1769 #ifndef OLD_ARM_ABI
1770 case R_ARM_ALU_PCREL7_0:
1771 case R_ARM_ALU_PCREL15_8:
1772 case R_ARM_ALU_PCREL23_15:
1774 bfd_vma insn;
1775 bfd_vma relocation;
1777 insn = bfd_get_32 (input_bfd, hit_data);
1778 #if USE_REL
1779 /* Extract the addend. */
1780 addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
1781 signed_addend = addend;
1782 #endif
1783 relocation = value + signed_addend;
1785 relocation -= (input_section->output_section->vma
1786 + input_section->output_offset
1787 + rel->r_offset);
1788 insn = (insn & ~0xfff)
1789 | ((howto->bitpos << 7) & 0xf00)
1790 | ((relocation >> howto->bitpos) & 0xff);
1791 bfd_put_32 (input_bfd, value, hit_data);
1793 return bfd_reloc_ok;
1794 #endif
1796 case R_ARM_GNU_VTINHERIT:
1797 case R_ARM_GNU_VTENTRY:
1798 return bfd_reloc_ok;
1800 case R_ARM_COPY:
1801 return bfd_reloc_notsupported;
1803 case R_ARM_GLOB_DAT:
1804 return bfd_reloc_notsupported;
1806 case R_ARM_JUMP_SLOT:
1807 return bfd_reloc_notsupported;
1809 case R_ARM_RELATIVE:
1810 return bfd_reloc_notsupported;
1812 case R_ARM_GOTOFF:
1813 /* Relocation is relative to the start of the
1814 global offset table. */
1816 BFD_ASSERT (sgot != NULL);
1817 if (sgot == NULL)
1818 return bfd_reloc_notsupported;
1820 /* If we are addressing a Thumb function, we need to adjust the
1821 address by one, so that attempts to call the function pointer will
1822 correctly interpret it as Thumb code. */
1823 if (sym_flags == STT_ARM_TFUNC)
1824 value += 1;
1826 /* Note that sgot->output_offset is not involved in this
1827 calculation. We always want the start of .got. If we
1828 define _GLOBAL_OFFSET_TABLE in a different way, as is
1829 permitted by the ABI, we might have to change this
1830 calculation. */
1831 value -= sgot->output_section->vma;
1832 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1833 contents, rel->r_offset, value,
1834 (bfd_vma) 0);
1836 case R_ARM_GOTPC:
1837 /* Use global offset table as symbol value. */
1838 BFD_ASSERT (sgot != NULL);
1840 if (sgot == NULL)
1841 return bfd_reloc_notsupported;
1843 value = sgot->output_section->vma;
1844 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1845 contents, rel->r_offset, value,
1846 (bfd_vma) 0);
1848 case R_ARM_GOT32:
1849 #ifndef OLD_ARM_ABI
1850 case R_ARM_GOT_PREL:
1851 #endif
1852 /* Relocation is to the entry for this symbol in the
1853 global offset table. */
1854 if (sgot == NULL)
1855 return bfd_reloc_notsupported;
1857 if (h != NULL)
1859 bfd_vma off;
1860 bfd_boolean dyn;
1862 off = h->got.offset;
1863 BFD_ASSERT (off != (bfd_vma) -1);
1864 dyn = globals->root.dynamic_sections_created;
1866 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1867 || (info->shared
1868 && SYMBOL_REFERENCES_LOCAL (info, h))
1869 || (ELF_ST_VISIBILITY (h->other)
1870 && h->root.type == bfd_link_hash_undefweak))
1872 /* This is actually a static link, or it is a -Bsymbolic link
1873 and the symbol is defined locally. We must initialize this
1874 entry in the global offset table. Since the offset must
1875 always be a multiple of 4, we use the least significant bit
1876 to record whether we have initialized it already.
1878 When doing a dynamic link, we create a .rel.got relocation
1879 entry to initialize the value. This is done in the
1880 finish_dynamic_symbol routine. */
1881 if ((off & 1) != 0)
1882 off &= ~1;
1883 else
1885 /* If we are addressing a Thumb function, we need to
1886 adjust the address by one, so that attempts to
1887 call the function pointer will correctly
1888 interpret it as Thumb code. */
1889 if (sym_flags == STT_ARM_TFUNC)
1890 value |= 1;
1892 bfd_put_32 (output_bfd, value, sgot->contents + off);
1893 h->got.offset |= 1;
1897 value = sgot->output_offset + off;
1899 else
1901 bfd_vma off;
1903 BFD_ASSERT (local_got_offsets != NULL &&
1904 local_got_offsets[r_symndx] != (bfd_vma) -1);
1906 off = local_got_offsets[r_symndx];
1908 /* The offset must always be a multiple of 4. We use the
1909 least significant bit to record whether we have already
1910 generated the necessary reloc. */
1911 if ((off & 1) != 0)
1912 off &= ~1;
1913 else
1915 bfd_put_32 (output_bfd, value, sgot->contents + off);
1917 if (info->shared)
1919 asection * srelgot;
1920 Elf_Internal_Rela outrel;
1921 bfd_byte *loc;
1923 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1924 BFD_ASSERT (srelgot != NULL);
1926 outrel.r_offset = (sgot->output_section->vma
1927 + sgot->output_offset
1928 + off);
1929 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
1930 loc = srelgot->contents;
1931 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
1932 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
1935 local_got_offsets[r_symndx] |= 1;
1938 value = sgot->output_offset + off;
1940 if (r_type != R_ARM_GOT32)
1941 value += sgot->output_section->vma;
1943 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1944 contents, rel->r_offset, value,
1945 (bfd_vma) 0);
1947 case R_ARM_SBREL32:
1948 return bfd_reloc_notsupported;
1950 case R_ARM_AMP_VCALL9:
1951 return bfd_reloc_notsupported;
1953 case R_ARM_RSBREL32:
1954 return bfd_reloc_notsupported;
1956 case R_ARM_THM_RPC22:
1957 return bfd_reloc_notsupported;
1959 case R_ARM_RREL32:
1960 return bfd_reloc_notsupported;
1962 case R_ARM_RABS32:
1963 return bfd_reloc_notsupported;
1965 case R_ARM_RPC24:
1966 return bfd_reloc_notsupported;
1968 case R_ARM_RBASE:
1969 return bfd_reloc_notsupported;
1971 default:
1972 return bfd_reloc_notsupported;
1976 #if USE_REL
1977 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. */
1978 static void
1979 arm_add_to_rel (bfd * abfd,
1980 bfd_byte * address,
1981 reloc_howto_type * howto,
1982 bfd_signed_vma increment)
1984 bfd_signed_vma addend;
1986 if (howto->type == R_ARM_THM_PC22)
1988 int upper_insn, lower_insn;
1989 int upper, lower;
1991 upper_insn = bfd_get_16 (abfd, address);
1992 lower_insn = bfd_get_16 (abfd, address + 2);
1993 upper = upper_insn & 0x7ff;
1994 lower = lower_insn & 0x7ff;
1996 addend = (upper << 12) | (lower << 1);
1997 addend += increment;
1998 addend >>= 1;
2000 upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
2001 lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
2003 bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
2004 bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
2006 else
2008 bfd_vma contents;
2010 contents = bfd_get_32 (abfd, address);
2012 /* Get the (signed) value from the instruction. */
2013 addend = contents & howto->src_mask;
2014 if (addend & ((howto->src_mask + 1) >> 1))
2016 bfd_signed_vma mask;
2018 mask = -1;
2019 mask &= ~ howto->src_mask;
2020 addend |= mask;
2023 /* Add in the increment, (which is a byte value). */
2024 switch (howto->type)
2026 default:
2027 addend += increment;
2028 break;
2030 case R_ARM_PC24:
2031 addend <<= howto->size;
2032 addend += increment;
2034 /* Should we check for overflow here ? */
2036 /* Drop any undesired bits. */
2037 addend >>= howto->rightshift;
2038 break;
2041 contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
2043 bfd_put_32 (abfd, contents, address);
2046 #endif /* USE_REL */
2048 /* Relocate an ARM ELF section. */
2049 static bfd_boolean
2050 elf32_arm_relocate_section (bfd * output_bfd,
2051 struct bfd_link_info * info,
2052 bfd * input_bfd,
2053 asection * input_section,
2054 bfd_byte * contents,
2055 Elf_Internal_Rela * relocs,
2056 Elf_Internal_Sym * local_syms,
2057 asection ** local_sections)
2059 Elf_Internal_Shdr *symtab_hdr;
2060 struct elf_link_hash_entry **sym_hashes;
2061 Elf_Internal_Rela *rel;
2062 Elf_Internal_Rela *relend;
2063 const char *name;
2065 #if !USE_REL
2066 if (info->relocatable)
2067 return TRUE;
2068 #endif
2070 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2071 sym_hashes = elf_sym_hashes (input_bfd);
2073 rel = relocs;
2074 relend = relocs + input_section->reloc_count;
2075 for (; rel < relend; rel++)
2077 int r_type;
2078 reloc_howto_type * howto;
2079 unsigned long r_symndx;
2080 Elf_Internal_Sym * sym;
2081 asection * sec;
2082 struct elf_link_hash_entry * h;
2083 bfd_vma relocation;
2084 bfd_reloc_status_type r;
2085 arelent bfd_reloc;
2087 r_symndx = ELF32_R_SYM (rel->r_info);
2088 r_type = ELF32_R_TYPE (rel->r_info);
2090 if ( r_type == R_ARM_GNU_VTENTRY
2091 || r_type == R_ARM_GNU_VTINHERIT)
2092 continue;
2094 elf32_arm_info_to_howto (input_bfd, & bfd_reloc, rel);
2095 howto = bfd_reloc.howto;
2097 #if USE_REL
2098 if (info->relocatable)
2100 /* This is a relocatable link. We don't have to change
2101 anything, unless the reloc is against a section symbol,
2102 in which case we have to adjust according to where the
2103 section symbol winds up in the output section. */
2104 if (r_symndx < symtab_hdr->sh_info)
2106 sym = local_syms + r_symndx;
2107 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2109 sec = local_sections[r_symndx];
2110 arm_add_to_rel (input_bfd, contents + rel->r_offset,
2111 howto,
2112 (bfd_signed_vma) (sec->output_offset
2113 + sym->st_value));
2117 continue;
2119 #endif
2121 /* This is a final link. */
2122 h = NULL;
2123 sym = NULL;
2124 sec = NULL;
2126 if (r_symndx < symtab_hdr->sh_info)
2128 sym = local_syms + r_symndx;
2129 sec = local_sections[r_symndx];
2130 #if USE_REL
2131 relocation = (sec->output_section->vma
2132 + sec->output_offset
2133 + sym->st_value);
2134 if ((sec->flags & SEC_MERGE)
2135 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2137 asection *msec;
2138 bfd_vma addend, value;
2140 if (howto->rightshift)
2142 (*_bfd_error_handler)
2143 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
2144 input_bfd, input_section,
2145 (long) rel->r_offset, howto->name);
2146 return FALSE;
2149 value = bfd_get_32 (input_bfd, contents + rel->r_offset);
2151 /* Get the (signed) value from the instruction. */
2152 addend = value & howto->src_mask;
2153 if (addend & ((howto->src_mask + 1) >> 1))
2155 bfd_signed_vma mask;
2157 mask = -1;
2158 mask &= ~ howto->src_mask;
2159 addend |= mask;
2161 msec = sec;
2162 addend =
2163 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
2164 - relocation;
2165 addend += msec->output_section->vma + msec->output_offset;
2166 value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
2167 bfd_put_32 (input_bfd, value, contents + rel->r_offset);
2169 #else
2170 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2171 #endif
2173 else
2175 bfd_boolean warned;
2176 bfd_boolean unresolved_reloc;
2178 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2179 r_symndx, symtab_hdr, sym_hashes,
2180 h, sec, relocation,
2181 unresolved_reloc, warned);
2183 if (unresolved_reloc || relocation != 0)
2185 /* In these cases, we don't need the relocation value.
2186 We check specially because in some obscure cases
2187 sec->output_section will be NULL. */
2188 switch (r_type)
2190 case R_ARM_PC24:
2191 case R_ARM_ABS32:
2192 case R_ARM_THM_PC22:
2193 case R_ARM_PLT32:
2195 if (info->shared
2196 && ((!info->symbolic && h->dynindx != -1)
2197 || !h->def_regular)
2198 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2199 && ((input_section->flags & SEC_ALLOC) != 0
2200 /* DWARF will emit R_ARM_ABS32 relocations in its
2201 sections against symbols defined externally
2202 in shared libraries. We can't do anything
2203 with them here. */
2204 || ((input_section->flags & SEC_DEBUGGING) != 0
2205 && h->def_dynamic))
2207 relocation = 0;
2208 break;
2210 case R_ARM_GOTPC:
2211 relocation = 0;
2212 break;
2214 case R_ARM_GOT32:
2215 #ifndef OLD_ARM_ABI
2216 case R_ARM_GOT_PREL:
2217 #endif
2218 if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL
2219 (elf_hash_table (info)->dynamic_sections_created,
2220 info->shared, h))
2221 && (!info->shared
2222 || (!info->symbolic && h->dynindx != -1)
2223 || !h->def_regular))
2224 relocation = 0;
2225 break;
2227 default:
2228 if (unresolved_reloc)
2229 _bfd_error_handler
2230 (_("%B(%A): warning: unresolvable relocation %d against symbol `%s'"),
2231 input_bfd, input_section,
2232 r_type,
2233 h->root.root.string);
2234 break;
2239 if (h != NULL)
2240 name = h->root.root.string;
2241 else
2243 name = (bfd_elf_string_from_elf_section
2244 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2245 if (name == NULL || *name == '\0')
2246 name = bfd_section_name (input_bfd, sec);
2249 r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
2250 input_section, contents, rel,
2251 relocation, info, sec, name,
2252 (h ? ELF_ST_TYPE (h->type) :
2253 ELF_ST_TYPE (sym->st_info)), h);
2255 if (r != bfd_reloc_ok)
2257 const char * msg = (const char *) 0;
2259 switch (r)
2261 case bfd_reloc_overflow:
2262 /* If the overflowing reloc was to an undefined symbol,
2263 we have already printed one error message and there
2264 is no point complaining again. */
2265 if ((! h ||
2266 h->root.type != bfd_link_hash_undefined)
2267 && (!((*info->callbacks->reloc_overflow)
2268 (info, (h ? &h->root : NULL), name, howto->name,
2269 (bfd_vma) 0, input_bfd, input_section,
2270 rel->r_offset))))
2271 return FALSE;
2272 break;
2274 case bfd_reloc_undefined:
2275 if (!((*info->callbacks->undefined_symbol)
2276 (info, name, input_bfd, input_section,
2277 rel->r_offset, TRUE)))
2278 return FALSE;
2279 break;
2281 case bfd_reloc_outofrange:
2282 msg = _("internal error: out of range error");
2283 goto common_error;
2285 case bfd_reloc_notsupported:
2286 msg = _("internal error: unsupported relocation error");
2287 goto common_error;
2289 case bfd_reloc_dangerous:
2290 msg = _("internal error: dangerous error");
2291 goto common_error;
2293 default:
2294 msg = _("internal error: unknown error");
2295 /* fall through */
2297 common_error:
2298 if (!((*info->callbacks->warning)
2299 (info, msg, name, input_bfd, input_section,
2300 rel->r_offset)))
2301 return FALSE;
2302 break;
2307 return TRUE;
2310 /* Set the right machine number. */
2312 static bfd_boolean
2313 elf32_arm_object_p (bfd *abfd)
2315 unsigned int mach;
2317 mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
2319 if (mach != bfd_mach_arm_unknown)
2320 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2322 else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
2323 bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
2325 else
2326 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
2328 return TRUE;
2331 /* Function to keep ARM specific flags in the ELF header. */
2333 static bfd_boolean
2334 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
2336 if (elf_flags_init (abfd)
2337 && elf_elfheader (abfd)->e_flags != flags)
2339 if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
2341 if (flags & EF_ARM_INTERWORK)
2342 (*_bfd_error_handler)
2343 (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
2344 abfd);
2345 else
2346 _bfd_error_handler
2347 (_("Warning: Clearing the interworking flag of %B due to outside request"),
2348 abfd);
2351 else
2353 elf_elfheader (abfd)->e_flags = flags;
2354 elf_flags_init (abfd) = TRUE;
2357 return TRUE;
2360 /* Copy backend specific data from one object module to another. */
2362 static bfd_boolean
2363 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2365 flagword in_flags;
2366 flagword out_flags;
2368 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2369 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2370 return TRUE;
2372 in_flags = elf_elfheader (ibfd)->e_flags;
2373 out_flags = elf_elfheader (obfd)->e_flags;
2375 if (elf_flags_init (obfd)
2376 && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
2377 && in_flags != out_flags)
2379 /* Cannot mix APCS26 and APCS32 code. */
2380 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2381 return FALSE;
2383 /* Cannot mix float APCS and non-float APCS code. */
2384 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2385 return FALSE;
2387 /* If the src and dest have different interworking flags
2388 then turn off the interworking bit. */
2389 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2391 if (out_flags & EF_ARM_INTERWORK)
2392 _bfd_error_handler
2393 (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
2394 obfd, ibfd);
2396 in_flags &= ~EF_ARM_INTERWORK;
2399 /* Likewise for PIC, though don't warn for this case. */
2400 if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
2401 in_flags &= ~EF_ARM_PIC;
2404 elf_elfheader (obfd)->e_flags = in_flags;
2405 elf_flags_init (obfd) = TRUE;
2407 return TRUE;
2410 /* Merge backend specific data from an object file to the output
2411 object file when linking. */
2413 static bfd_boolean
2414 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
2416 flagword out_flags;
2417 flagword in_flags;
2418 bfd_boolean flags_compatible = TRUE;
2419 asection *sec;
2421 /* Check if we have the same endianess. */
2422 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2423 return FALSE;
2425 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2426 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2427 return TRUE;
2429 /* The input BFD must have had its flags initialised. */
2430 /* The following seems bogus to me -- The flags are initialized in
2431 the assembler but I don't think an elf_flags_init field is
2432 written into the object. */
2433 /* BFD_ASSERT (elf_flags_init (ibfd)); */
2435 in_flags = elf_elfheader (ibfd)->e_flags;
2436 out_flags = elf_elfheader (obfd)->e_flags;
2438 if (!elf_flags_init (obfd))
2440 /* If the input is the default architecture and had the default
2441 flags then do not bother setting the flags for the output
2442 architecture, instead allow future merges to do this. If no
2443 future merges ever set these flags then they will retain their
2444 uninitialised values, which surprise surprise, correspond
2445 to the default values. */
2446 if (bfd_get_arch_info (ibfd)->the_default
2447 && elf_elfheader (ibfd)->e_flags == 0)
2448 return TRUE;
2450 elf_flags_init (obfd) = TRUE;
2451 elf_elfheader (obfd)->e_flags = in_flags;
2453 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2454 && bfd_get_arch_info (obfd)->the_default)
2455 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2457 return TRUE;
2460 /* Determine what should happen if the input ARM architecture
2461 does not match the output ARM architecture. */
2462 if (! bfd_arm_merge_machines (ibfd, obfd))
2463 return FALSE;
2465 /* Identical flags must be compatible. */
2466 if (in_flags == out_flags)
2467 return TRUE;
2469 /* Check to see if the input BFD actually contains any sections. If
2470 not, its flags may not have been initialised either, but it
2471 cannot actually cause any incompatibility. Do not short-circuit
2472 dynamic objects; their section list may be emptied by
2473 elf_link_add_object_symbols.
2475 Also check to see if there are no code sections in the input.
2476 In this case there is no need to check for code specific flags.
2477 XXX - do we need to worry about floating-point format compatability
2478 in data sections ? */
2479 if (!(ibfd->flags & DYNAMIC))
2481 bfd_boolean null_input_bfd = TRUE;
2482 bfd_boolean only_data_sections = TRUE;
2484 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2486 /* Ignore synthetic glue sections. */
2487 if (strcmp (sec->name, ".glue_7")
2488 && strcmp (sec->name, ".glue_7t"))
2490 if ((bfd_get_section_flags (ibfd, sec)
2491 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2492 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2493 only_data_sections = FALSE;
2495 null_input_bfd = FALSE;
2496 break;
2500 if (null_input_bfd || only_data_sections)
2501 return TRUE;
2504 /* Complain about various flag mismatches. */
2505 if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
2507 _bfd_error_handler
2508 (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
2509 ibfd, obfd,
2510 (in_flags & EF_ARM_EABIMASK) >> 24,
2511 (out_flags & EF_ARM_EABIMASK) >> 24);
2512 return FALSE;
2515 /* Not sure what needs to be checked for EABI versions >= 1. */
2516 if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
2518 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
2520 _bfd_error_handler
2521 (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
2522 ibfd, obfd,
2523 in_flags & EF_ARM_APCS_26 ? 26 : 32,
2524 out_flags & EF_ARM_APCS_26 ? 26 : 32);
2525 flags_compatible = FALSE;
2528 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
2530 if (in_flags & EF_ARM_APCS_FLOAT)
2531 _bfd_error_handler
2532 (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
2533 ibfd, obfd);
2534 else
2535 _bfd_error_handler
2536 (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
2537 ibfd, obfd);
2539 flags_compatible = FALSE;
2542 if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
2544 if (in_flags & EF_ARM_VFP_FLOAT)
2545 _bfd_error_handler
2546 (_("ERROR: %B uses VFP instructions, whereas %B does not"),
2547 ibfd, obfd);
2548 else
2549 _bfd_error_handler
2550 (_("ERROR: %B uses FPA instructions, whereas %B does not"),
2551 ibfd, obfd);
2553 flags_compatible = FALSE;
2556 if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
2558 if (in_flags & EF_ARM_MAVERICK_FLOAT)
2559 _bfd_error_handler
2560 (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
2561 ibfd, obfd);
2562 else
2563 _bfd_error_handler
2564 (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
2565 ibfd, obfd);
2567 flags_compatible = FALSE;
2570 #ifdef EF_ARM_SOFT_FLOAT
2571 if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
2573 /* We can allow interworking between code that is VFP format
2574 layout, and uses either soft float or integer regs for
2575 passing floating point arguments and results. We already
2576 know that the APCS_FLOAT flags match; similarly for VFP
2577 flags. */
2578 if ((in_flags & EF_ARM_APCS_FLOAT) != 0
2579 || (in_flags & EF_ARM_VFP_FLOAT) == 0)
2581 if (in_flags & EF_ARM_SOFT_FLOAT)
2582 _bfd_error_handler
2583 (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
2584 ibfd, obfd);
2585 else
2586 _bfd_error_handler
2587 (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
2588 ibfd, obfd);
2590 flags_compatible = FALSE;
2593 #endif
2595 /* Interworking mismatch is only a warning. */
2596 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
2598 if (in_flags & EF_ARM_INTERWORK)
2600 _bfd_error_handler
2601 (_("Warning: %B supports interworking, whereas %B does not"),
2602 ibfd, obfd);
2604 else
2606 _bfd_error_handler
2607 (_("Warning: %B does not support interworking, whereas %B does"),
2608 ibfd, obfd);
2613 return flags_compatible;
2616 /* Display the flags field. */
2618 static bfd_boolean
2619 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
2621 FILE * file = (FILE *) ptr;
2622 unsigned long flags;
2624 BFD_ASSERT (abfd != NULL && ptr != NULL);
2626 /* Print normal ELF private data. */
2627 _bfd_elf_print_private_bfd_data (abfd, ptr);
2629 flags = elf_elfheader (abfd)->e_flags;
2630 /* Ignore init flag - it may not be set, despite the flags field
2631 containing valid data. */
2633 /* xgettext:c-format */
2634 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2636 switch (EF_ARM_EABI_VERSION (flags))
2638 case EF_ARM_EABI_UNKNOWN:
2639 /* The following flag bits are GNU extensions and not part of the
2640 official ARM ELF extended ABI. Hence they are only decoded if
2641 the EABI version is not set. */
2642 if (flags & EF_ARM_INTERWORK)
2643 fprintf (file, _(" [interworking enabled]"));
2645 if (flags & EF_ARM_APCS_26)
2646 fprintf (file, " [APCS-26]");
2647 else
2648 fprintf (file, " [APCS-32]");
2650 if (flags & EF_ARM_VFP_FLOAT)
2651 fprintf (file, _(" [VFP float format]"));
2652 else if (flags & EF_ARM_MAVERICK_FLOAT)
2653 fprintf (file, _(" [Maverick float format]"));
2654 else
2655 fprintf (file, _(" [FPA float format]"));
2657 if (flags & EF_ARM_APCS_FLOAT)
2658 fprintf (file, _(" [floats passed in float registers]"));
2660 if (flags & EF_ARM_PIC)
2661 fprintf (file, _(" [position independent]"));
2663 if (flags & EF_ARM_NEW_ABI)
2664 fprintf (file, _(" [new ABI]"));
2666 if (flags & EF_ARM_OLD_ABI)
2667 fprintf (file, _(" [old ABI]"));
2669 if (flags & EF_ARM_SOFT_FLOAT)
2670 fprintf (file, _(" [software FP]"));
2672 flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
2673 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
2674 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
2675 | EF_ARM_MAVERICK_FLOAT);
2676 break;
2678 case EF_ARM_EABI_VER1:
2679 fprintf (file, _(" [Version1 EABI]"));
2681 if (flags & EF_ARM_SYMSARESORTED)
2682 fprintf (file, _(" [sorted symbol table]"));
2683 else
2684 fprintf (file, _(" [unsorted symbol table]"));
2686 flags &= ~ EF_ARM_SYMSARESORTED;
2687 break;
2689 case EF_ARM_EABI_VER2:
2690 fprintf (file, _(" [Version2 EABI]"));
2692 if (flags & EF_ARM_SYMSARESORTED)
2693 fprintf (file, _(" [sorted symbol table]"));
2694 else
2695 fprintf (file, _(" [unsorted symbol table]"));
2697 if (flags & EF_ARM_DYNSYMSUSESEGIDX)
2698 fprintf (file, _(" [dynamic symbols use segment index]"));
2700 if (flags & EF_ARM_MAPSYMSFIRST)
2701 fprintf (file, _(" [mapping symbols precede others]"));
2703 flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
2704 | EF_ARM_MAPSYMSFIRST);
2705 break;
2707 case EF_ARM_EABI_VER3:
2708 fprintf (file, _(" [Version3 EABI]"));
2709 break;
2711 case EF_ARM_EABI_VER4:
2712 fprintf (file, _(" [Version4 EABI]"));
2714 if (flags & EF_ARM_BE8)
2715 fprintf (file, _(" [BE8]"));
2717 if (flags & EF_ARM_LE8)
2718 fprintf (file, _(" [LE8]"));
2720 flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
2721 break;
2723 default:
2724 fprintf (file, _(" <EABI version unrecognised>"));
2725 break;
2728 flags &= ~ EF_ARM_EABIMASK;
2730 if (flags & EF_ARM_RELEXEC)
2731 fprintf (file, _(" [relocatable executable]"));
2733 if (flags & EF_ARM_HASENTRY)
2734 fprintf (file, _(" [has entry point]"));
2736 flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
2738 if (flags)
2739 fprintf (file, _("<Unrecognised flag bits set>"));
2741 fputc ('\n', file);
2743 return TRUE;
2746 static int
2747 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
2749 switch (ELF_ST_TYPE (elf_sym->st_info))
2751 case STT_ARM_TFUNC:
2752 return ELF_ST_TYPE (elf_sym->st_info);
2754 case STT_ARM_16BIT:
2755 /* If the symbol is not an object, return the STT_ARM_16BIT flag.
2756 This allows us to distinguish between data used by Thumb instructions
2757 and non-data (which is probably code) inside Thumb regions of an
2758 executable. */
2759 if (type != STT_OBJECT)
2760 return ELF_ST_TYPE (elf_sym->st_info);
2761 break;
2763 default:
2764 break;
2767 return type;
2770 static asection *
2771 elf32_arm_gc_mark_hook (asection * sec,
2772 struct bfd_link_info * info ATTRIBUTE_UNUSED,
2773 Elf_Internal_Rela * rel,
2774 struct elf_link_hash_entry * h,
2775 Elf_Internal_Sym * sym)
2777 if (h != NULL)
2779 switch (ELF32_R_TYPE (rel->r_info))
2781 case R_ARM_GNU_VTINHERIT:
2782 case R_ARM_GNU_VTENTRY:
2783 break;
2785 default:
2786 switch (h->root.type)
2788 case bfd_link_hash_defined:
2789 case bfd_link_hash_defweak:
2790 return h->root.u.def.section;
2792 case bfd_link_hash_common:
2793 return h->root.u.c.p->section;
2795 default:
2796 break;
2800 else
2801 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2803 return NULL;
2806 /* Update the got entry reference counts for the section being removed. */
2808 static bfd_boolean
2809 elf32_arm_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
2810 struct bfd_link_info * info ATTRIBUTE_UNUSED,
2811 asection * sec ATTRIBUTE_UNUSED,
2812 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
2814 Elf_Internal_Shdr *symtab_hdr;
2815 struct elf_link_hash_entry **sym_hashes;
2816 bfd_signed_vma *local_got_refcounts;
2817 const Elf_Internal_Rela *rel, *relend;
2818 unsigned long r_symndx;
2819 struct elf_link_hash_entry *h;
2820 struct elf32_arm_link_hash_table * globals;
2822 globals = elf32_arm_hash_table (info);
2824 elf_section_data (sec)->local_dynrel = NULL;
2826 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2827 sym_hashes = elf_sym_hashes (abfd);
2828 local_got_refcounts = elf_local_got_refcounts (abfd);
2830 relend = relocs + sec->reloc_count;
2831 for (rel = relocs; rel < relend; rel++)
2833 int r_type;
2835 r_type = ELF32_R_TYPE (rel->r_info);
2836 #ifndef OLD_ARM_ABI
2837 r_type = arm_real_reloc_type (globals, r_type);
2838 #endif
2839 switch (r_type)
2841 case R_ARM_GOT32:
2842 #ifndef OLD_ARM_ABI
2843 case R_ARM_GOT_PREL:
2844 #endif
2845 r_symndx = ELF32_R_SYM (rel->r_info);
2846 if (r_symndx >= symtab_hdr->sh_info)
2848 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2849 if (h->got.refcount > 0)
2850 h->got.refcount -= 1;
2852 else if (local_got_refcounts != NULL)
2854 if (local_got_refcounts[r_symndx] > 0)
2855 local_got_refcounts[r_symndx] -= 1;
2857 break;
2859 case R_ARM_ABS32:
2860 case R_ARM_REL32:
2861 case R_ARM_PC24:
2862 case R_ARM_PLT32:
2863 #ifndef OLD_ARM_ABI
2864 case R_ARM_PREL31:
2865 #endif
2866 r_symndx = ELF32_R_SYM (rel->r_info);
2867 if (r_symndx >= symtab_hdr->sh_info)
2869 struct elf32_arm_link_hash_entry *eh;
2870 struct elf32_arm_relocs_copied **pp;
2871 struct elf32_arm_relocs_copied *p;
2873 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2875 if (h->plt.refcount > 0)
2876 h->plt.refcount -= 1;
2878 if (r_type == R_ARM_ABS32
2879 #ifndef OLD_ARM_ABI
2880 || r_type == R_ARM_PREL31
2881 #endif
2882 || r_type == R_ARM_REL32)
2884 eh = (struct elf32_arm_link_hash_entry *) h;
2886 for (pp = &eh->relocs_copied; (p = *pp) != NULL;
2887 pp = &p->next)
2888 if (p->section == sec)
2890 p->count -= 1;
2891 if (p->count == 0)
2892 *pp = p->next;
2893 break;
2897 break;
2899 default:
2900 break;
2904 return TRUE;
2907 /* Look through the relocs for a section during the first phase. */
2909 static bfd_boolean
2910 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
2911 asection *sec, const Elf_Internal_Rela *relocs)
2913 Elf_Internal_Shdr *symtab_hdr;
2914 struct elf_link_hash_entry **sym_hashes;
2915 struct elf_link_hash_entry **sym_hashes_end;
2916 const Elf_Internal_Rela *rel;
2917 const Elf_Internal_Rela *rel_end;
2918 bfd *dynobj;
2919 asection *sreloc;
2920 bfd_vma *local_got_offsets;
2921 struct elf32_arm_link_hash_table *htab;
2923 if (info->relocatable)
2924 return TRUE;
2926 htab = elf32_arm_hash_table (info);
2927 sreloc = NULL;
2929 dynobj = elf_hash_table (info)->dynobj;
2930 local_got_offsets = elf_local_got_offsets (abfd);
2932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2933 sym_hashes = elf_sym_hashes (abfd);
2934 sym_hashes_end = sym_hashes
2935 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2937 if (!elf_bad_symtab (abfd))
2938 sym_hashes_end -= symtab_hdr->sh_info;
2940 rel_end = relocs + sec->reloc_count;
2941 for (rel = relocs; rel < rel_end; rel++)
2943 struct elf_link_hash_entry *h;
2944 unsigned long r_symndx;
2945 int r_type;
2947 r_symndx = ELF32_R_SYM (rel->r_info);
2948 r_type = ELF32_R_TYPE (rel->r_info);
2949 #ifndef OLD_ARM_ABI
2950 r_type = arm_real_reloc_type (htab, r_type);
2951 #endif
2952 if (r_symndx < symtab_hdr->sh_info)
2953 h = NULL;
2954 else
2955 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2957 switch (r_type)
2959 case R_ARM_GOT32:
2960 #ifndef OLD_ARM_ABI
2961 case R_ARM_GOT_PREL:
2962 #endif
2963 /* This symbol requires a global offset table entry. */
2964 if (h != NULL)
2966 h->got.refcount++;
2968 else
2970 bfd_signed_vma *local_got_refcounts;
2972 /* This is a global offset table entry for a local symbol. */
2973 local_got_refcounts = elf_local_got_refcounts (abfd);
2974 if (local_got_refcounts == NULL)
2976 bfd_size_type size;
2978 size = symtab_hdr->sh_info;
2979 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2980 local_got_refcounts = bfd_zalloc (abfd, size);
2981 if (local_got_refcounts == NULL)
2982 return FALSE;
2983 elf_local_got_refcounts (abfd) = local_got_refcounts;
2985 local_got_refcounts[r_symndx] += 1;
2987 if (r_type == R_ARM_GOT32)
2988 break;
2989 /* Fall through. */
2991 case R_ARM_GOTOFF:
2992 case R_ARM_GOTPC:
2993 if (htab->sgot == NULL)
2995 if (htab->root.dynobj == NULL)
2996 htab->root.dynobj = abfd;
2997 if (!create_got_section (htab->root.dynobj, info))
2998 return FALSE;
3000 break;
3002 case R_ARM_ABS32:
3003 case R_ARM_REL32:
3004 case R_ARM_PC24:
3005 case R_ARM_PLT32:
3006 #ifndef OLD_ARM_ABI
3007 case R_ARM_PREL31:
3008 #endif
3009 if (h != NULL)
3011 /* If this reloc is in a read-only section, we might
3012 need a copy reloc. We can't check reliably at this
3013 stage whether the section is read-only, as input
3014 sections have not yet been mapped to output sections.
3015 Tentatively set the flag for now, and correct in
3016 adjust_dynamic_symbol. */
3017 if (!info->shared)
3018 h->non_got_ref = 1;
3020 /* We may need a .plt entry if the function this reloc
3021 refers to is in a different object. We can't tell for
3022 sure yet, because something later might force the
3023 symbol local. */
3024 if (r_type == R_ARM_PC24
3025 || r_type == R_ARM_PLT32)
3026 h->needs_plt = 1;
3028 /* If we create a PLT entry, this relocation will reference
3029 it, even if it's an ABS32 relocation. */
3030 h->plt.refcount += 1;
3033 /* If we are creating a shared library, and this is a reloc
3034 against a global symbol, or a non PC relative reloc
3035 against a local symbol, then we need to copy the reloc
3036 into the shared library. However, if we are linking with
3037 -Bsymbolic, we do not need to copy a reloc against a
3038 global symbol which is defined in an object we are
3039 including in the link (i.e., DEF_REGULAR is set). At
3040 this point we have not seen all the input files, so it is
3041 possible that DEF_REGULAR is not set now but will be set
3042 later (it is never cleared). We account for that
3043 possibility below by storing information in the
3044 relocs_copied field of the hash table entry. */
3045 if (info->shared
3046 && (sec->flags & SEC_ALLOC) != 0
3047 && ((r_type != R_ARM_PC24
3048 && r_type != R_ARM_PLT32
3049 #ifndef OLD_ARM_ABI
3050 && r_type != R_ARM_PREL31
3051 #endif
3052 && r_type != R_ARM_REL32)
3053 || (h != NULL
3054 && (! info->symbolic
3055 || !h->def_regular))))
3057 struct elf32_arm_relocs_copied *p, **head;
3059 /* When creating a shared object, we must copy these
3060 reloc types into the output file. We create a reloc
3061 section in dynobj and make room for this reloc. */
3062 if (sreloc == NULL)
3064 const char * name;
3066 name = (bfd_elf_string_from_elf_section
3067 (abfd,
3068 elf_elfheader (abfd)->e_shstrndx,
3069 elf_section_data (sec)->rel_hdr.sh_name));
3070 if (name == NULL)
3071 return FALSE;
3073 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
3074 && strcmp (bfd_get_section_name (abfd, sec),
3075 name + 4) == 0);
3077 sreloc = bfd_get_section_by_name (dynobj, name);
3078 if (sreloc == NULL)
3080 flagword flags;
3082 sreloc = bfd_make_section (dynobj, name);
3083 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3084 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3085 if ((sec->flags & SEC_ALLOC) != 0
3086 /* BPABI objects never have dynamic
3087 relocations mapped. */
3088 && !htab->symbian_p)
3089 flags |= SEC_ALLOC | SEC_LOAD;
3090 if (sreloc == NULL
3091 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3092 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
3093 return FALSE;
3096 elf_section_data (sec)->sreloc = sreloc;
3099 /* If this is a global symbol, we count the number of
3100 relocations we need for this symbol. */
3101 if (h != NULL)
3103 head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
3105 else
3107 /* Track dynamic relocs needed for local syms too.
3108 We really need local syms available to do this
3109 easily. Oh well. */
3111 asection *s;
3112 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3113 sec, r_symndx);
3114 if (s == NULL)
3115 return FALSE;
3117 head = ((struct elf32_arm_relocs_copied **)
3118 &elf_section_data (s)->local_dynrel);
3121 p = *head;
3122 if (p == NULL || p->section != sec)
3124 bfd_size_type amt = sizeof *p;
3126 p = bfd_alloc (htab->root.dynobj, amt);
3127 if (p == NULL)
3128 return FALSE;
3129 p->next = *head;
3130 *head = p;
3131 p->section = sec;
3132 p->count = 0;
3135 if (r_type == R_ARM_ABS32
3136 #ifndef OLD_ARM_ABI
3137 || r_type == R_ARM_PREL31
3138 #endif
3139 || r_type == R_ARM_REL32)
3140 p->count += 1;
3142 break;
3144 /* This relocation describes the C++ object vtable hierarchy.
3145 Reconstruct it for later use during GC. */
3146 case R_ARM_GNU_VTINHERIT:
3147 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3148 return FALSE;
3149 break;
3151 /* This relocation describes which C++ vtable entries are actually
3152 used. Record for later use during GC. */
3153 case R_ARM_GNU_VTENTRY:
3154 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
3155 return FALSE;
3156 break;
3160 return TRUE;
3163 static bfd_boolean
3164 is_arm_mapping_symbol_name (const char * name)
3166 return (name != NULL)
3167 && (name[0] == '$')
3168 && ((name[1] == 'a') || (name[1] == 't') || (name[1] == 'd'))
3169 && (name[2] == 0);
3172 /* Treat mapping symbols as special target symbols. */
3174 static bfd_boolean
3175 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
3177 return is_arm_mapping_symbol_name (sym->name);
3180 /* This is a copy of elf_find_function() from elf.c except that
3181 ARM mapping symbols are ignored when looking for function names
3182 and STT_ARM_TFUNC is considered to a function type. */
3184 static bfd_boolean
3185 arm_elf_find_function (bfd * abfd ATTRIBUTE_UNUSED,
3186 asection * section,
3187 asymbol ** symbols,
3188 bfd_vma offset,
3189 const char ** filename_ptr,
3190 const char ** functionname_ptr)
3192 const char * filename = NULL;
3193 asymbol * func = NULL;
3194 bfd_vma low_func = 0;
3195 asymbol ** p;
3197 for (p = symbols; *p != NULL; p++)
3199 elf_symbol_type *q;
3201 q = (elf_symbol_type *) *p;
3203 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3205 default:
3206 break;
3207 case STT_FILE:
3208 filename = bfd_asymbol_name (&q->symbol);
3209 break;
3210 case STT_FUNC:
3211 case STT_ARM_TFUNC:
3212 /* Skip $a and $t symbols. */
3213 if ((q->symbol.flags & BSF_LOCAL)
3214 && is_arm_mapping_symbol_name (q->symbol.name))
3215 continue;
3216 /* Fall through. */
3217 case STT_NOTYPE:
3218 if (bfd_get_section (&q->symbol) == section
3219 && q->symbol.value >= low_func
3220 && q->symbol.value <= offset)
3222 func = (asymbol *) q;
3223 low_func = q->symbol.value;
3225 break;
3229 if (func == NULL)
3230 return FALSE;
3232 if (filename_ptr)
3233 *filename_ptr = filename;
3234 if (functionname_ptr)
3235 *functionname_ptr = bfd_asymbol_name (func);
3237 return TRUE;
3241 /* Find the nearest line to a particular section and offset, for error
3242 reporting. This code is a duplicate of the code in elf.c, except
3243 that it uses arm_elf_find_function. */
3245 static bfd_boolean
3246 elf32_arm_find_nearest_line (bfd * abfd,
3247 asection * section,
3248 asymbol ** symbols,
3249 bfd_vma offset,
3250 const char ** filename_ptr,
3251 const char ** functionname_ptr,
3252 unsigned int * line_ptr)
3254 bfd_boolean found = FALSE;
3256 /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it. */
3258 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3259 filename_ptr, functionname_ptr,
3260 line_ptr, 0,
3261 & elf_tdata (abfd)->dwarf2_find_line_info))
3263 if (!*functionname_ptr)
3264 arm_elf_find_function (abfd, section, symbols, offset,
3265 *filename_ptr ? NULL : filename_ptr,
3266 functionname_ptr);
3268 return TRUE;
3271 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3272 & found, filename_ptr,
3273 functionname_ptr, line_ptr,
3274 & elf_tdata (abfd)->line_info))
3275 return FALSE;
3277 if (found && (*functionname_ptr || *line_ptr))
3278 return TRUE;
3280 if (symbols == NULL)
3281 return FALSE;
3283 if (! arm_elf_find_function (abfd, section, symbols, offset,
3284 filename_ptr, functionname_ptr))
3285 return FALSE;
3287 *line_ptr = 0;
3288 return TRUE;
3291 /* Adjust a symbol defined by a dynamic object and referenced by a
3292 regular object. The current definition is in some section of the
3293 dynamic object, but we're not including those sections. We have to
3294 change the definition to something the rest of the link can
3295 understand. */
3297 static bfd_boolean
3298 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
3299 struct elf_link_hash_entry * h)
3301 bfd * dynobj;
3302 asection * s;
3303 unsigned int power_of_two;
3305 dynobj = elf_hash_table (info)->dynobj;
3307 /* Make sure we know what is going on here. */
3308 BFD_ASSERT (dynobj != NULL
3309 && (h->needs_plt
3310 || h->u.weakdef != NULL
3311 || (h->def_dynamic
3312 && h->ref_regular
3313 && !h->def_regular)));
3315 /* If this is a function, put it in the procedure linkage table. We
3316 will fill in the contents of the procedure linkage table later,
3317 when we know the address of the .got section. */
3318 if (h->type == STT_FUNC
3319 || h->needs_plt)
3321 if (h->plt.refcount <= 0
3322 || SYMBOL_CALLS_LOCAL (info, h)
3323 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3324 && h->root.type == bfd_link_hash_undefweak))
3326 /* This case can occur if we saw a PLT32 reloc in an input
3327 file, but the symbol was never referred to by a dynamic
3328 object, or if all references were garbage collected. In
3329 such a case, we don't actually need to build a procedure
3330 linkage table, and we can just do a PC24 reloc instead. */
3331 h->plt.offset = (bfd_vma) -1;
3332 h->needs_plt = 0;
3335 return TRUE;
3337 else
3338 /* It's possible that we incorrectly decided a .plt reloc was
3339 needed for an R_ARM_PC24 reloc to a non-function sym in
3340 check_relocs. We can't decide accurately between function and
3341 non-function syms in check-relocs; Objects loaded later in
3342 the link may change h->type. So fix it now. */
3343 h->plt.offset = (bfd_vma) -1;
3345 /* If this is a weak symbol, and there is a real definition, the
3346 processor independent code will have arranged for us to see the
3347 real definition first, and we can just use the same value. */
3348 if (h->u.weakdef != NULL)
3350 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3351 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3352 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3353 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3354 return TRUE;
3357 /* This is a reference to a symbol defined by a dynamic object which
3358 is not a function. */
3360 /* If we are creating a shared library, we must presume that the
3361 only references to the symbol are via the global offset table.
3362 For such cases we need not do anything here; the relocations will
3363 be handled correctly by relocate_section. */
3364 if (info->shared)
3365 return TRUE;
3367 /* We must allocate the symbol in our .dynbss section, which will
3368 become part of the .bss section of the executable. There will be
3369 an entry for this symbol in the .dynsym section. The dynamic
3370 object will contain position independent code, so all references
3371 from the dynamic object to this symbol will go through the global
3372 offset table. The dynamic linker will use the .dynsym entry to
3373 determine the address it must put in the global offset table, so
3374 both the dynamic object and the regular object will refer to the
3375 same memory location for the variable. */
3376 s = bfd_get_section_by_name (dynobj, ".dynbss");
3377 BFD_ASSERT (s != NULL);
3379 /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
3380 copy the initial value out of the dynamic object and into the
3381 runtime process image. We need to remember the offset into the
3382 .rel.bss section we are going to use. */
3383 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3385 asection *srel;
3387 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
3388 BFD_ASSERT (srel != NULL);
3389 srel->size += sizeof (Elf32_External_Rel);
3390 h->needs_copy = 1;
3393 /* We need to figure out the alignment required for this symbol. I
3394 have no idea how ELF linkers handle this. */
3395 power_of_two = bfd_log2 (h->size);
3396 if (power_of_two > 3)
3397 power_of_two = 3;
3399 /* Apply the required alignment. */
3400 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3401 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3403 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3404 return FALSE;
3407 /* Define the symbol as being at this point in the section. */
3408 h->root.u.def.section = s;
3409 h->root.u.def.value = s->size;
3411 /* Increment the section size to make room for the symbol. */
3412 s->size += h->size;
3414 return TRUE;
3417 /* Allocate space in .plt, .got and associated reloc sections for
3418 dynamic relocs. */
3420 static bfd_boolean
3421 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
3423 struct bfd_link_info *info;
3424 struct elf32_arm_link_hash_table *htab;
3425 struct elf32_arm_link_hash_entry *eh;
3426 struct elf32_arm_relocs_copied *p;
3428 if (h->root.type == bfd_link_hash_indirect)
3429 return TRUE;
3431 if (h->root.type == bfd_link_hash_warning)
3432 /* When warning symbols are created, they **replace** the "real"
3433 entry in the hash table, thus we never get to see the real
3434 symbol in a hash traversal. So look at it now. */
3435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3437 info = (struct bfd_link_info *) inf;
3438 htab = elf32_arm_hash_table (info);
3440 if (htab->root.dynamic_sections_created
3441 && h->plt.refcount > 0)
3443 /* Make sure this symbol is output as a dynamic symbol.
3444 Undefined weak syms won't yet be marked as dynamic. */
3445 if (h->dynindx == -1
3446 && !h->forced_local)
3448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3449 return FALSE;
3452 if (info->shared
3453 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3455 asection *s = htab->splt;
3457 /* If this is the first .plt entry, make room for the special
3458 first entry. */
3459 if (s->size == 0)
3460 s->size += htab->plt_header_size;
3462 h->plt.offset = s->size;
3464 /* If this symbol is not defined in a regular file, and we are
3465 not generating a shared library, then set the symbol to this
3466 location in the .plt. This is required to make function
3467 pointers compare as equal between the normal executable and
3468 the shared library. */
3469 if (! info->shared
3470 && !h->def_regular)
3472 h->root.u.def.section = s;
3473 h->root.u.def.value = h->plt.offset;
3476 /* Make room for this entry. */
3477 s->size += htab->plt_entry_size;
3479 if (!htab->symbian_p)
3480 /* We also need to make an entry in the .got.plt section, which
3481 will be placed in the .got section by the linker script. */
3482 htab->sgotplt->size += 4;
3484 /* We also need to make an entry in the .rel.plt section. */
3485 htab->srelplt->size += sizeof (Elf32_External_Rel);
3487 else
3489 h->plt.offset = (bfd_vma) -1;
3490 h->needs_plt = 0;
3493 else
3495 h->plt.offset = (bfd_vma) -1;
3496 h->needs_plt = 0;
3499 if (h->got.refcount > 0)
3501 asection *s;
3502 bfd_boolean dyn;
3504 /* Make sure this symbol is output as a dynamic symbol.
3505 Undefined weak syms won't yet be marked as dynamic. */
3506 if (h->dynindx == -1
3507 && !h->forced_local)
3509 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3510 return FALSE;
3513 if (!htab->symbian_p)
3515 s = htab->sgot;
3516 h->got.offset = s->size;
3517 s->size += 4;
3518 dyn = htab->root.dynamic_sections_created;
3519 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3520 || h->root.type != bfd_link_hash_undefweak)
3521 && (info->shared
3522 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3523 htab->srelgot->size += sizeof (Elf32_External_Rel);
3526 else
3527 h->got.offset = (bfd_vma) -1;
3529 eh = (struct elf32_arm_link_hash_entry *) h;
3530 if (eh->relocs_copied == NULL)
3531 return TRUE;
3533 /* In the shared -Bsymbolic case, discard space allocated for
3534 dynamic pc-relative relocs against symbols which turn out to be
3535 defined in regular objects. For the normal shared case, discard
3536 space for pc-relative relocs that have become local due to symbol
3537 visibility changes. */
3539 if (info->shared)
3541 /* Discard relocs on undefined weak syms with non-default
3542 visibility. */
3543 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3544 && h->root.type == bfd_link_hash_undefweak)
3545 eh->relocs_copied = NULL;
3547 else
3549 /* For the non-shared case, discard space for relocs against
3550 symbols which turn out to need copy relocs or are not
3551 dynamic. */
3553 if (!h->non_got_ref
3554 && ((h->def_dynamic
3555 && !h->def_regular)
3556 || (htab->root.dynamic_sections_created
3557 && (h->root.type == bfd_link_hash_undefweak
3558 || h->root.type == bfd_link_hash_undefined))))
3560 /* Make sure this symbol is output as a dynamic symbol.
3561 Undefined weak syms won't yet be marked as dynamic. */
3562 if (h->dynindx == -1
3563 && !h->forced_local)
3565 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3566 return FALSE;
3569 /* If that succeeded, we know we'll be keeping all the
3570 relocs. */
3571 if (h->dynindx != -1)
3572 goto keep;
3575 eh->relocs_copied = NULL;
3577 keep: ;
3580 /* Finally, allocate space. */
3581 for (p = eh->relocs_copied; p != NULL; p = p->next)
3583 asection *sreloc = elf_section_data (p->section)->sreloc;
3584 sreloc->size += p->count * sizeof (Elf32_External_Rel);
3587 return TRUE;
3590 /* Set the sizes of the dynamic sections. */
3592 static bfd_boolean
3593 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
3594 struct bfd_link_info * info)
3596 bfd * dynobj;
3597 asection * s;
3598 bfd_boolean plt;
3599 bfd_boolean relocs;
3600 bfd *ibfd;
3601 struct elf32_arm_link_hash_table *htab;
3603 htab = elf32_arm_hash_table (info);
3604 dynobj = elf_hash_table (info)->dynobj;
3605 BFD_ASSERT (dynobj != NULL);
3607 if (elf_hash_table (info)->dynamic_sections_created)
3609 /* Set the contents of the .interp section to the interpreter. */
3610 if (info->executable)
3612 s = bfd_get_section_by_name (dynobj, ".interp");
3613 BFD_ASSERT (s != NULL);
3614 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3615 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3619 /* Set up .got offsets for local syms, and space for local dynamic
3620 relocs. */
3621 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3623 bfd_signed_vma *local_got;
3624 bfd_signed_vma *end_local_got;
3625 char *local_tls_type;
3626 bfd_size_type locsymcount;
3627 Elf_Internal_Shdr *symtab_hdr;
3628 asection *srel;
3630 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3631 continue;
3633 for (s = ibfd->sections; s != NULL; s = s->next)
3635 struct elf32_arm_relocs_copied *p;
3637 for (p = *((struct elf32_arm_relocs_copied **)
3638 &elf_section_data (s)->local_dynrel);
3639 p != NULL;
3640 p = p->next)
3642 if (!bfd_is_abs_section (p->section)
3643 && bfd_is_abs_section (p->section->output_section))
3645 /* Input section has been discarded, either because
3646 it is a copy of a linkonce section or due to
3647 linker script /DISCARD/, so we'll be discarding
3648 the relocs too. */
3650 else if (p->count != 0)
3652 srel = elf_section_data (p->section)->sreloc;
3653 srel->size += p->count * sizeof (Elf32_External_Rel);
3654 if ((p->section->output_section->flags & SEC_READONLY) != 0)
3655 info->flags |= DF_TEXTREL;
3660 local_got = elf_local_got_refcounts (ibfd);
3661 if (!local_got)
3662 continue;
3664 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3665 locsymcount = symtab_hdr->sh_info;
3666 end_local_got = local_got + locsymcount;
3667 s = htab->sgot;
3668 srel = htab->srelgot;
3669 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3671 if (*local_got > 0)
3673 *local_got = s->size;
3674 s->size += 4;
3675 if (info->shared)
3676 srel->size += sizeof (Elf32_External_Rel);
3678 else
3679 *local_got = (bfd_vma) -1;
3683 /* Allocate global sym .plt and .got entries, and space for global
3684 sym dynamic relocs. */
3685 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3687 /* The check_relocs and adjust_dynamic_symbol entry points have
3688 determined the sizes of the various dynamic sections. Allocate
3689 memory for them. */
3690 plt = FALSE;
3691 relocs = FALSE;
3692 for (s = dynobj->sections; s != NULL; s = s->next)
3694 const char * name;
3695 bfd_boolean strip;
3697 if ((s->flags & SEC_LINKER_CREATED) == 0)
3698 continue;
3700 /* It's OK to base decisions on the section name, because none
3701 of the dynobj section names depend upon the input files. */
3702 name = bfd_get_section_name (dynobj, s);
3704 strip = FALSE;
3706 if (strcmp (name, ".plt") == 0)
3708 if (s->size == 0)
3710 /* Strip this section if we don't need it; see the
3711 comment below. */
3712 strip = TRUE;
3714 else
3716 /* Remember whether there is a PLT. */
3717 plt = TRUE;
3720 else if (strncmp (name, ".rel", 4) == 0)
3722 if (s->size == 0)
3724 /* If we don't need this section, strip it from the
3725 output file. This is mostly to handle .rel.bss and
3726 .rel.plt. We must create both sections in
3727 create_dynamic_sections, because they must be created
3728 before the linker maps input sections to output
3729 sections. The linker does that before
3730 adjust_dynamic_symbol is called, and it is that
3731 function which decides whether anything needs to go
3732 into these sections. */
3733 strip = TRUE;
3735 else
3737 /* Remember whether there are any reloc sections other
3738 than .rel.plt. */
3739 if (strcmp (name, ".rel.plt") != 0)
3740 relocs = TRUE;
3742 /* We use the reloc_count field as a counter if we need
3743 to copy relocs into the output file. */
3744 s->reloc_count = 0;
3747 else if (strncmp (name, ".got", 4) != 0)
3749 /* It's not one of our sections, so don't allocate space. */
3750 continue;
3753 if (strip)
3755 _bfd_strip_section_from_output (info, s);
3756 continue;
3759 /* Allocate memory for the section contents. */
3760 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3761 if (s->contents == NULL && s->size != 0)
3762 return FALSE;
3765 if (elf_hash_table (info)->dynamic_sections_created)
3767 /* Add some entries to the .dynamic section. We fill in the
3768 values later, in elf32_arm_finish_dynamic_sections, but we
3769 must add the entries now so that we get the correct size for
3770 the .dynamic section. The DT_DEBUG entry is filled in by the
3771 dynamic linker and used by the debugger. */
3772 #define add_dynamic_entry(TAG, VAL) \
3773 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3775 if (!info->shared)
3777 if (!add_dynamic_entry (DT_DEBUG, 0))
3778 return FALSE;
3781 if (plt)
3783 if ( !add_dynamic_entry (DT_PLTGOT, 0)
3784 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3785 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3786 || !add_dynamic_entry (DT_JMPREL, 0))
3787 return FALSE;
3790 if (relocs)
3792 if ( !add_dynamic_entry (DT_REL, 0)
3793 || !add_dynamic_entry (DT_RELSZ, 0)
3794 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3795 return FALSE;
3798 if ((info->flags & DF_TEXTREL) != 0)
3800 if (!add_dynamic_entry (DT_TEXTREL, 0))
3801 return FALSE;
3802 info->flags |= DF_TEXTREL;
3805 #undef add_synamic_entry
3807 return TRUE;
3810 /* Finish up dynamic symbol handling. We set the contents of various
3811 dynamic sections here. */
3813 static bfd_boolean
3814 elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
3815 struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
3817 bfd * dynobj;
3818 struct elf32_arm_link_hash_table *htab;
3820 dynobj = elf_hash_table (info)->dynobj;
3821 htab = elf32_arm_hash_table (info);
3823 if (h->plt.offset != (bfd_vma) -1)
3825 asection * splt;
3826 asection * srel;
3827 bfd_byte *loc;
3828 bfd_vma plt_index;
3829 Elf_Internal_Rela rel;
3831 /* This symbol has an entry in the procedure linkage table. Set
3832 it up. */
3834 BFD_ASSERT (h->dynindx != -1);
3836 splt = bfd_get_section_by_name (dynobj, ".plt");
3837 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
3838 BFD_ASSERT (splt != NULL && srel != NULL);
3840 /* Get the index in the procedure linkage table which
3841 corresponds to this symbol. This is the index of this symbol
3842 in all the symbols for which we are making plt entries. The
3843 first entry in the procedure linkage table is reserved. */
3844 plt_index = ((h->plt.offset - htab->plt_header_size)
3845 / htab->plt_entry_size);
3847 /* Fill in the entry in the procedure linkage table. */
3848 if (htab->symbian_p)
3850 unsigned i;
3851 for (i = 0; i < htab->plt_entry_size / 4; ++i)
3852 bfd_put_32 (output_bfd,
3853 elf32_arm_symbian_plt_entry[i],
3854 splt->contents + h->plt.offset + 4 * i);
3856 /* Fill in the entry in the .rel.plt section. */
3857 rel.r_offset = (splt->output_section->vma
3858 + splt->output_offset
3859 + h->plt.offset + 4 * (i - 1));
3860 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
3862 else
3864 bfd_vma got_offset;
3865 bfd_vma got_displacement;
3866 asection * sgot;
3868 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3869 BFD_ASSERT (sgot != NULL);
3871 /* Get the offset into the .got table of the entry that
3872 corresponds to this function. Each .got entry is 4 bytes.
3873 The first three are reserved. */
3874 got_offset = (plt_index + 3) * 4;
3876 /* Calculate the displacement between the PLT slot and the
3877 entry in the GOT. */
3878 got_displacement = (sgot->output_section->vma
3879 + sgot->output_offset
3880 + got_offset
3881 - splt->output_section->vma
3882 - splt->output_offset
3883 - h->plt.offset
3884 - 8);
3886 BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
3888 bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
3889 splt->contents + h->plt.offset + 0);
3890 bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
3891 splt->contents + h->plt.offset + 4);
3892 bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
3893 splt->contents + h->plt.offset + 8);
3894 #ifdef FOUR_WORD_PLT
3895 bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
3896 splt->contents + h->plt.offset + 12);
3897 #endif
3899 /* Fill in the entry in the global offset table. */
3900 bfd_put_32 (output_bfd,
3901 (splt->output_section->vma
3902 + splt->output_offset),
3903 sgot->contents + got_offset);
3905 /* Fill in the entry in the .rel.plt section. */
3906 rel.r_offset = (sgot->output_section->vma
3907 + sgot->output_offset
3908 + got_offset);
3909 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
3912 loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
3913 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3915 if (!h->def_regular)
3917 /* Mark the symbol as undefined, rather than as defined in
3918 the .plt section. Leave the value alone. */
3919 sym->st_shndx = SHN_UNDEF;
3920 /* If the symbol is weak, we do need to clear the value.
3921 Otherwise, the PLT entry would provide a definition for
3922 the symbol even if the symbol wasn't defined anywhere,
3923 and so the symbol would never be NULL. */
3924 if (!h->ref_regular_nonweak)
3925 sym->st_value = 0;
3929 if (h->got.offset != (bfd_vma) -1)
3931 asection * sgot;
3932 asection * srel;
3933 Elf_Internal_Rela rel;
3934 bfd_byte *loc;
3936 /* This symbol has an entry in the global offset table. Set it
3937 up. */
3938 sgot = bfd_get_section_by_name (dynobj, ".got");
3939 srel = bfd_get_section_by_name (dynobj, ".rel.got");
3940 BFD_ASSERT (sgot != NULL && srel != NULL);
3942 rel.r_offset = (sgot->output_section->vma
3943 + sgot->output_offset
3944 + (h->got.offset &~ (bfd_vma) 1));
3946 /* If this is a static link, or it is a -Bsymbolic link and the
3947 symbol is defined locally or was forced to be local because
3948 of a version file, we just want to emit a RELATIVE reloc.
3949 The entry in the global offset table will already have been
3950 initialized in the relocate_section function. */
3951 if (info->shared
3952 && SYMBOL_REFERENCES_LOCAL (info, h))
3954 BFD_ASSERT((h->got.offset & 1) != 0);
3955 rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
3957 else
3959 BFD_ASSERT((h->got.offset & 1) == 0);
3960 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3961 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
3964 loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
3965 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3968 if (h->needs_copy)
3970 asection * s;
3971 Elf_Internal_Rela rel;
3972 bfd_byte *loc;
3974 /* This symbol needs a copy reloc. Set it up. */
3975 BFD_ASSERT (h->dynindx != -1
3976 && (h->root.type == bfd_link_hash_defined
3977 || h->root.type == bfd_link_hash_defweak));
3979 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3980 ".rel.bss");
3981 BFD_ASSERT (s != NULL);
3983 rel.r_offset = (h->root.u.def.value
3984 + h->root.u.def.section->output_section->vma
3985 + h->root.u.def.section->output_offset);
3986 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
3987 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
3988 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3991 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3992 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3993 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3994 sym->st_shndx = SHN_ABS;
3996 return TRUE;
3999 /* Finish up the dynamic sections. */
4001 static bfd_boolean
4002 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
4004 bfd * dynobj;
4005 asection * sgot;
4006 asection * sdyn;
4008 dynobj = elf_hash_table (info)->dynobj;
4010 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4011 BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
4012 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4014 if (elf_hash_table (info)->dynamic_sections_created)
4016 asection *splt;
4017 Elf32_External_Dyn *dyncon, *dynconend;
4018 struct elf32_arm_link_hash_table *htab;
4020 htab = elf32_arm_hash_table (info);
4021 splt = bfd_get_section_by_name (dynobj, ".plt");
4022 BFD_ASSERT (splt != NULL && sdyn != NULL);
4024 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4025 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4027 for (; dyncon < dynconend; dyncon++)
4029 Elf_Internal_Dyn dyn;
4030 const char * name;
4031 asection * s;
4033 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4035 switch (dyn.d_tag)
4037 unsigned int type;
4039 default:
4040 break;
4042 case DT_HASH:
4043 name = ".hash";
4044 goto get_vma_if_bpabi;
4045 case DT_STRTAB:
4046 name = ".dynstr";
4047 goto get_vma_if_bpabi;
4048 case DT_SYMTAB:
4049 name = ".dynsym";
4050 goto get_vma_if_bpabi;
4051 case DT_VERSYM:
4052 name = ".gnu.version";
4053 goto get_vma_if_bpabi;
4054 case DT_VERDEF:
4055 name = ".gnu.version_d";
4056 goto get_vma_if_bpabi;
4057 case DT_VERNEED:
4058 name = ".gnu.version_r";
4059 goto get_vma_if_bpabi;
4061 case DT_PLTGOT:
4062 name = ".got";
4063 goto get_vma;
4064 case DT_JMPREL:
4065 name = ".rel.plt";
4066 get_vma:
4067 s = bfd_get_section_by_name (output_bfd, name);
4068 BFD_ASSERT (s != NULL);
4069 if (!htab->symbian_p)
4070 dyn.d_un.d_ptr = s->vma;
4071 else
4072 /* In the BPABI, tags in the PT_DYNAMIC section point
4073 at the file offset, not the memory address, for the
4074 convenience of the post linker. */
4075 dyn.d_un.d_ptr = s->filepos;
4076 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4077 break;
4079 get_vma_if_bpabi:
4080 if (htab->symbian_p)
4081 goto get_vma;
4082 break;
4084 case DT_PLTRELSZ:
4085 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
4086 BFD_ASSERT (s != NULL);
4087 dyn.d_un.d_val = s->size;
4088 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4089 break;
4091 case DT_RELSZ:
4092 if (!htab->symbian_p)
4094 /* My reading of the SVR4 ABI indicates that the
4095 procedure linkage table relocs (DT_JMPREL) should be
4096 included in the overall relocs (DT_REL). This is
4097 what Solaris does. However, UnixWare can not handle
4098 that case. Therefore, we override the DT_RELSZ entry
4099 here to make it not include the JMPREL relocs. Since
4100 the linker script arranges for .rel.plt to follow all
4101 other relocation sections, we don't have to worry
4102 about changing the DT_REL entry. */
4103 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
4104 if (s != NULL)
4105 dyn.d_un.d_val -= s->size;
4106 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4107 break;
4109 /* Fall through */
4111 case DT_REL:
4112 case DT_RELA:
4113 case DT_RELASZ:
4114 /* In the BPABI, the DT_REL tag must point at the file
4115 offset, not the VMA, of the first relocation
4116 section. So, we use code similar to that in
4117 elflink.c, but do not check for SHF_ALLOC on the
4118 relcoation section, since relocations sections are
4119 never allocated under the BPABI. The comments above
4120 about Unixware notwithstanding, we include all of the
4121 relocations here. */
4122 if (htab->symbian_p)
4124 unsigned int i;
4125 type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
4126 ? SHT_REL : SHT_RELA);
4127 dyn.d_un.d_val = 0;
4128 for (i = 1; i < elf_numsections (output_bfd); i++)
4130 Elf_Internal_Shdr *hdr
4131 = elf_elfsections (output_bfd)[i];
4132 if (hdr->sh_type == type)
4134 if (dyn.d_tag == DT_RELSZ
4135 || dyn.d_tag == DT_RELASZ)
4136 dyn.d_un.d_val += hdr->sh_size;
4137 else if (dyn.d_un.d_val == 0
4138 || hdr->sh_offset < dyn.d_un.d_val)
4139 dyn.d_un.d_val = hdr->sh_offset;
4142 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4144 break;
4146 /* Set the bottom bit of DT_INIT/FINI if the
4147 corresponding function is Thumb. */
4148 case DT_INIT:
4149 name = info->init_function;
4150 goto get_sym;
4151 case DT_FINI:
4152 name = info->fini_function;
4153 get_sym:
4154 /* If it wasn't set by elf_bfd_final_link
4155 then there is nothing to adjust. */
4156 if (dyn.d_un.d_val != 0)
4158 struct elf_link_hash_entry * eh;
4160 eh = elf_link_hash_lookup (elf_hash_table (info), name,
4161 FALSE, FALSE, TRUE);
4162 if (eh != (struct elf_link_hash_entry *) NULL
4163 && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
4165 dyn.d_un.d_val |= 1;
4166 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4169 break;
4173 /* Fill in the first entry in the procedure linkage table. */
4174 if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
4176 bfd_vma got_displacement;
4178 /* Calculate the displacement between the PLT slot and &GOT[0]. */
4179 got_displacement = (sgot->output_section->vma
4180 + sgot->output_offset
4181 - splt->output_section->vma
4182 - splt->output_offset
4183 - 16);
4185 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents + 0);
4186 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents + 4);
4187 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents + 8);
4188 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
4189 #ifdef FOUR_WORD_PLT
4190 /* The displacement value goes in the otherwise-unused last word of
4191 the second entry. */
4192 bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
4193 #else
4194 bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
4195 #endif
4198 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4199 really seem like the right value. */
4200 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4203 /* Fill in the first three entries in the global offset table. */
4204 if (sgot)
4206 if (sgot->size > 0)
4208 if (sdyn == NULL)
4209 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4210 else
4211 bfd_put_32 (output_bfd,
4212 sdyn->output_section->vma + sdyn->output_offset,
4213 sgot->contents);
4214 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4215 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4218 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4221 return TRUE;
4224 static void
4225 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
4227 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
4228 struct elf32_arm_link_hash_table *globals;
4230 i_ehdrp = elf_elfheader (abfd);
4232 i_ehdrp->e_ident[EI_OSABI] = ARM_ELF_OS_ABI_VERSION;
4233 i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
4235 if (link_info)
4237 globals = elf32_arm_hash_table (link_info);
4238 if (globals->byteswap_code)
4239 i_ehdrp->e_flags |= EF_ARM_BE8;
4243 static enum elf_reloc_type_class
4244 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
4246 switch ((int) ELF32_R_TYPE (rela->r_info))
4248 case R_ARM_RELATIVE:
4249 return reloc_class_relative;
4250 case R_ARM_JUMP_SLOT:
4251 return reloc_class_plt;
4252 case R_ARM_COPY:
4253 return reloc_class_copy;
4254 default:
4255 return reloc_class_normal;
4259 /* Set the right machine number for an Arm ELF file. */
4261 static bfd_boolean
4262 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
4264 if (hdr->sh_type == SHT_NOTE)
4265 *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
4267 return TRUE;
4270 static void
4271 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
4273 bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
4276 /* Return TRUE if this is an unwinding table entry. */
4278 static bfd_boolean
4279 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
4281 size_t len1, len2;
4283 len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
4284 len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
4285 return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
4286 || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
4290 /* Set the type and flags for an ARM section. We do this by
4291 the section name, which is a hack, but ought to work. */
4293 static bfd_boolean
4294 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
4296 const char * name;
4298 name = bfd_get_section_name (abfd, sec);
4300 if (is_arm_elf_unwind_section_name (abfd, name))
4302 hdr->sh_type = SHT_ARM_EXIDX;
4303 hdr->sh_flags |= SHF_LINK_ORDER;
4305 return TRUE;
4308 /* Handle an ARM specific section when reading an object file.
4309 This is called when elf.c finds a section with an unknown type. */
4311 static bfd_boolean
4312 elf32_arm_section_from_shdr (bfd *abfd,
4313 Elf_Internal_Shdr * hdr,
4314 const char *name)
4316 /* There ought to be a place to keep ELF backend specific flags, but
4317 at the moment there isn't one. We just keep track of the
4318 sections by their name, instead. Fortunately, the ABI gives
4319 names for all the ARM specific sections, so we will probably get
4320 away with this. */
4321 switch (hdr->sh_type)
4323 case SHT_ARM_EXIDX:
4324 break;
4326 default:
4327 return FALSE;
4330 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
4331 return FALSE;
4333 return TRUE;
4336 /* Called for each symbol. Builds a section map based on mapping symbols.
4337 Does not alter any of the symbols. */
4339 static bfd_boolean
4340 elf32_arm_output_symbol_hook (struct bfd_link_info *info,
4341 const char *name,
4342 Elf_Internal_Sym *elfsym,
4343 asection *input_sec,
4344 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4346 int mapcount;
4347 elf32_arm_section_map *map;
4348 struct elf32_arm_link_hash_table *globals;
4350 /* Only do this on final link. */
4351 if (info->relocatable)
4352 return TRUE;
4354 /* Only build a map if we need to byteswap code. */
4355 globals = elf32_arm_hash_table (info);
4356 if (!globals->byteswap_code)
4357 return TRUE;
4359 /* We only want mapping symbols. */
4360 if (! is_arm_mapping_symbol_name (name))
4361 return TRUE;
4363 mapcount = ++(elf32_arm_section_data (input_sec)->mapcount);
4364 map = elf32_arm_section_data (input_sec)->map;
4365 /* TODO: This may be inefficient, but we probably don't usually have many
4366 mapping symbols per section. */
4367 map = bfd_realloc (map, mapcount * sizeof (elf32_arm_section_map));
4368 elf32_arm_section_data (input_sec)->map = map;
4370 map[mapcount - 1].vma = elfsym->st_value;
4371 map[mapcount - 1].type = name[1];
4372 return TRUE;
4376 /* Allocate target specific section data. */
4378 static bfd_boolean
4379 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
4381 struct _arm_elf_section_data *sdata;
4382 bfd_size_type amt = sizeof (*sdata);
4384 sdata = bfd_zalloc (abfd, amt);
4385 if (sdata == NULL)
4386 return FALSE;
4387 sec->used_by_bfd = sdata;
4389 return _bfd_elf_new_section_hook (abfd, sec);
4393 /* Used to order a list of mapping symbols by address. */
4395 static int
4396 elf32_arm_compare_mapping (const void * a, const void * b)
4398 return ((const elf32_arm_section_map *) a)->vma
4399 > ((const elf32_arm_section_map *) b)->vma;
4403 /* Do code byteswapping. Return FALSE afterwards so that the section is
4404 written out as normal. */
4406 static bfd_boolean
4407 elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
4408 bfd_byte *contents)
4410 int mapcount;
4411 elf32_arm_section_map *map;
4412 bfd_vma ptr;
4413 bfd_vma end;
4414 bfd_vma offset;
4415 bfd_byte tmp;
4416 int i;
4418 mapcount = elf32_arm_section_data (sec)->mapcount;
4419 map = elf32_arm_section_data (sec)->map;
4421 if (mapcount == 0)
4422 return FALSE;
4424 qsort (map, mapcount, sizeof (elf32_arm_section_map),
4425 elf32_arm_compare_mapping);
4427 offset = sec->output_section->vma + sec->output_offset;
4428 ptr = map[0].vma - offset;
4429 for (i = 0; i < mapcount; i++)
4431 if (i == mapcount - 1)
4432 end = sec->size;
4433 else
4434 end = map[i + 1].vma - offset;
4436 switch (map[i].type)
4438 case 'a':
4439 /* Byte swap code words. */
4440 while (ptr + 3 < end)
4442 tmp = contents[ptr];
4443 contents[ptr] = contents[ptr + 3];
4444 contents[ptr + 3] = tmp;
4445 tmp = contents[ptr + 1];
4446 contents[ptr + 1] = contents[ptr + 2];
4447 contents[ptr + 2] = tmp;
4448 ptr += 4;
4450 break;
4452 case 't':
4453 /* Byte swap code halfwords. */
4454 while (ptr + 1 < end)
4456 tmp = contents[ptr];
4457 contents[ptr] = contents[ptr + 1];
4458 contents[ptr + 1] = tmp;
4459 ptr += 2;
4461 break;
4463 case 'd':
4464 /* Leave data alone. */
4465 break;
4467 ptr = end;
4469 free (map);
4470 return FALSE;
4473 #define ELF_ARCH bfd_arch_arm
4474 #define ELF_MACHINE_CODE EM_ARM
4475 #ifdef __QNXTARGET__
4476 #define ELF_MAXPAGESIZE 0x1000
4477 #else
4478 #define ELF_MAXPAGESIZE 0x8000
4479 #endif
4481 #define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
4482 #define bfd_elf32_bfd_merge_private_bfd_data elf32_arm_merge_private_bfd_data
4483 #define bfd_elf32_bfd_set_private_flags elf32_arm_set_private_flags
4484 #define bfd_elf32_bfd_print_private_bfd_data elf32_arm_print_private_bfd_data
4485 #define bfd_elf32_bfd_link_hash_table_create elf32_arm_link_hash_table_create
4486 #define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup
4487 #define bfd_elf32_find_nearest_line elf32_arm_find_nearest_line
4488 #define bfd_elf32_new_section_hook elf32_arm_new_section_hook
4489 #define bfd_elf32_bfd_is_target_special_symbol elf32_arm_is_target_special_symbol
4491 #define elf_backend_get_symbol_type elf32_arm_get_symbol_type
4492 #define elf_backend_gc_mark_hook elf32_arm_gc_mark_hook
4493 #define elf_backend_gc_sweep_hook elf32_arm_gc_sweep_hook
4494 #define elf_backend_check_relocs elf32_arm_check_relocs
4495 #define elf_backend_relocate_section elf32_arm_relocate_section
4496 #define elf_backend_write_section elf32_arm_write_section
4497 #define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
4498 #define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
4499 #define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
4500 #define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
4501 #define elf_backend_link_output_symbol_hook elf32_arm_output_symbol_hook
4502 #define elf_backend_size_dynamic_sections elf32_arm_size_dynamic_sections
4503 #define elf_backend_post_process_headers elf32_arm_post_process_headers
4504 #define elf_backend_reloc_type_class elf32_arm_reloc_type_class
4505 #define elf_backend_object_p elf32_arm_object_p
4506 #define elf_backend_section_flags elf32_arm_section_flags
4507 #define elf_backend_fake_sections elf32_arm_fake_sections
4508 #define elf_backend_section_from_shdr elf32_arm_section_from_shdr
4509 #define elf_backend_final_write_processing elf32_arm_final_write_processing
4510 #define elf_backend_copy_indirect_symbol elf32_arm_copy_indirect_symbol
4512 #define elf_backend_can_refcount 1
4513 #define elf_backend_can_gc_sections 1
4514 #define elf_backend_plt_readonly 1
4515 #define elf_backend_want_got_plt 1
4516 #define elf_backend_want_plt_sym 0
4517 #if !USE_REL
4518 #define elf_backend_rela_normal 1
4519 #endif
4521 #define elf_backend_got_header_size 12
4523 #include "elf32-target.h"