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