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