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