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