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