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