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