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