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