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