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