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