* elf32-arm.c (elf32_arm_check_relocs): Increment count for all
[deliverable/binutils-gdb.git] / bfd / elf32-arm.c
CommitLineData
252b5132 1/* 32-bit ELF support for ARM
4e7fd91e 2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
d1f161ea 3 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
7f266840
DJ
21#include "elf/arm.h"
22#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26
27#ifndef NUM_ELEM
28#define NUM_ELEM(a) (sizeof (a) / (sizeof (a)[0]))
29#endif
30
7f266840
DJ
31#define elf_info_to_howto 0
32#define elf_info_to_howto_rel elf32_arm_info_to_howto
33
34#define ARM_ELF_ABI_VERSION 0
35#define ARM_ELF_OS_ABI_VERSION ELFOSABI_ARM
36
37static reloc_howto_type * elf32_arm_reloc_type_lookup
38 PARAMS ((bfd * abfd, bfd_reloc_code_real_type code));
39static bfd_boolean elf32_arm_nabi_grok_prstatus
40 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
41static bfd_boolean elf32_arm_nabi_grok_psinfo
42 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
43
44/* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
45 R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
46 in that slot. */
47
48static reloc_howto_type elf32_arm_howto_table[] =
49{
50 /* No relocation */
51 HOWTO (R_ARM_NONE, /* type */
52 0, /* rightshift */
53 0, /* size (0 = byte, 1 = short, 2 = long) */
54 0, /* bitsize */
55 FALSE, /* pc_relative */
56 0, /* bitpos */
57 complain_overflow_dont,/* complain_on_overflow */
58 bfd_elf_generic_reloc, /* special_function */
59 "R_ARM_NONE", /* name */
60 FALSE, /* partial_inplace */
61 0, /* src_mask */
62 0, /* dst_mask */
63 FALSE), /* pcrel_offset */
64
65 HOWTO (R_ARM_PC24, /* type */
66 2, /* rightshift */
67 2, /* size (0 = byte, 1 = short, 2 = long) */
68 24, /* bitsize */
69 TRUE, /* pc_relative */
70 0, /* bitpos */
71 complain_overflow_signed,/* complain_on_overflow */
72 bfd_elf_generic_reloc, /* special_function */
73 "R_ARM_PC24", /* name */
74 FALSE, /* partial_inplace */
75 0x00ffffff, /* src_mask */
76 0x00ffffff, /* dst_mask */
77 TRUE), /* pcrel_offset */
78
79 /* 32 bit absolute */
80 HOWTO (R_ARM_ABS32, /* type */
81 0, /* rightshift */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
83 32, /* bitsize */
84 FALSE, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_bitfield,/* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_ARM_ABS32", /* name */
89 FALSE, /* partial_inplace */
90 0xffffffff, /* src_mask */
91 0xffffffff, /* dst_mask */
92 FALSE), /* pcrel_offset */
93
94 /* standard 32bit pc-relative reloc */
95 HOWTO (R_ARM_REL32, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 32, /* bitsize */
99 TRUE, /* pc_relative */
100 0, /* bitpos */
101 complain_overflow_bitfield,/* complain_on_overflow */
102 bfd_elf_generic_reloc, /* special_function */
103 "R_ARM_REL32", /* name */
104 FALSE, /* partial_inplace */
105 0xffffffff, /* src_mask */
106 0xffffffff, /* dst_mask */
107 TRUE), /* pcrel_offset */
108
109 /* 8 bit absolute */
110 HOWTO (R_ARM_PC13, /* type */
111 0, /* rightshift */
112 0, /* size (0 = byte, 1 = short, 2 = long) */
113 8, /* bitsize */
114 FALSE, /* pc_relative */
115 0, /* bitpos */
116 complain_overflow_bitfield,/* complain_on_overflow */
117 bfd_elf_generic_reloc, /* special_function */
118 "R_ARM_PC13", /* name */
119 FALSE, /* partial_inplace */
120 0x000000ff, /* src_mask */
121 0x000000ff, /* dst_mask */
122 FALSE), /* pcrel_offset */
123
124 /* 16 bit absolute */
125 HOWTO (R_ARM_ABS16, /* type */
126 0, /* rightshift */
127 1, /* size (0 = byte, 1 = short, 2 = long) */
128 16, /* bitsize */
129 FALSE, /* pc_relative */
130 0, /* bitpos */
131 complain_overflow_bitfield,/* complain_on_overflow */
132 bfd_elf_generic_reloc, /* special_function */
133 "R_ARM_ABS16", /* name */
134 FALSE, /* partial_inplace */
135 0x0000ffff, /* src_mask */
136 0x0000ffff, /* dst_mask */
137 FALSE), /* pcrel_offset */
138
139 /* 12 bit absolute */
140 HOWTO (R_ARM_ABS12, /* type */
141 0, /* rightshift */
142 2, /* size (0 = byte, 1 = short, 2 = long) */
143 12, /* bitsize */
144 FALSE, /* pc_relative */
145 0, /* bitpos */
146 complain_overflow_bitfield,/* complain_on_overflow */
147 bfd_elf_generic_reloc, /* special_function */
148 "R_ARM_ABS12", /* name */
149 FALSE, /* partial_inplace */
150 0x000008ff, /* src_mask */
151 0x000008ff, /* dst_mask */
152 FALSE), /* pcrel_offset */
153
154 HOWTO (R_ARM_THM_ABS5, /* type */
155 6, /* rightshift */
156 1, /* size (0 = byte, 1 = short, 2 = long) */
157 5, /* bitsize */
158 FALSE, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_bitfield,/* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_ARM_THM_ABS5", /* name */
163 FALSE, /* partial_inplace */
164 0x000007e0, /* src_mask */
165 0x000007e0, /* dst_mask */
166 FALSE), /* pcrel_offset */
167
168 /* 8 bit absolute */
169 HOWTO (R_ARM_ABS8, /* type */
170 0, /* rightshift */
171 0, /* size (0 = byte, 1 = short, 2 = long) */
172 8, /* bitsize */
173 FALSE, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_bitfield,/* complain_on_overflow */
176 bfd_elf_generic_reloc, /* special_function */
177 "R_ARM_ABS8", /* name */
178 FALSE, /* partial_inplace */
179 0x000000ff, /* src_mask */
180 0x000000ff, /* dst_mask */
181 FALSE), /* pcrel_offset */
182
183 HOWTO (R_ARM_SBREL32, /* type */
184 0, /* rightshift */
185 2, /* size (0 = byte, 1 = short, 2 = long) */
186 32, /* bitsize */
187 FALSE, /* pc_relative */
188 0, /* bitpos */
189 complain_overflow_dont,/* complain_on_overflow */
190 bfd_elf_generic_reloc, /* special_function */
191 "R_ARM_SBREL32", /* name */
192 FALSE, /* partial_inplace */
193 0xffffffff, /* src_mask */
194 0xffffffff, /* dst_mask */
195 FALSE), /* pcrel_offset */
196
197 HOWTO (R_ARM_THM_PC22, /* type */
198 1, /* rightshift */
199 2, /* size (0 = byte, 1 = short, 2 = long) */
200 23, /* bitsize */
201 TRUE, /* pc_relative */
202 0, /* bitpos */
203 complain_overflow_signed,/* complain_on_overflow */
204 bfd_elf_generic_reloc, /* special_function */
205 "R_ARM_THM_PC22", /* name */
206 FALSE, /* partial_inplace */
207 0x07ff07ff, /* src_mask */
208 0x07ff07ff, /* dst_mask */
209 TRUE), /* pcrel_offset */
210
211 HOWTO (R_ARM_THM_PC8, /* type */
212 1, /* rightshift */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
214 8, /* bitsize */
215 TRUE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_signed,/* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_ARM_THM_PC8", /* name */
220 FALSE, /* partial_inplace */
221 0x000000ff, /* src_mask */
222 0x000000ff, /* dst_mask */
223 TRUE), /* pcrel_offset */
224
225 HOWTO (R_ARM_AMP_VCALL9, /* type */
226 1, /* rightshift */
227 1, /* size (0 = byte, 1 = short, 2 = long) */
228 8, /* bitsize */
229 TRUE, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_signed,/* complain_on_overflow */
232 bfd_elf_generic_reloc, /* special_function */
233 "R_ARM_AMP_VCALL9", /* name */
234 FALSE, /* partial_inplace */
235 0x000000ff, /* src_mask */
236 0x000000ff, /* dst_mask */
237 TRUE), /* pcrel_offset */
238
239 HOWTO (R_ARM_SWI24, /* type */
240 0, /* rightshift */
241 0, /* size (0 = byte, 1 = short, 2 = long) */
242 0, /* bitsize */
243 FALSE, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_signed,/* complain_on_overflow */
246 bfd_elf_generic_reloc, /* special_function */
247 "R_ARM_SWI24", /* name */
248 FALSE, /* partial_inplace */
249 0x00000000, /* src_mask */
250 0x00000000, /* dst_mask */
251 FALSE), /* pcrel_offset */
252
253 HOWTO (R_ARM_THM_SWI8, /* type */
254 0, /* rightshift */
255 0, /* size (0 = byte, 1 = short, 2 = long) */
256 0, /* bitsize */
257 FALSE, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_signed,/* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_ARM_SWI8", /* name */
262 FALSE, /* partial_inplace */
263 0x00000000, /* src_mask */
264 0x00000000, /* dst_mask */
265 FALSE), /* pcrel_offset */
266
267 /* BLX instruction for the ARM. */
268 HOWTO (R_ARM_XPC25, /* type */
269 2, /* rightshift */
270 2, /* size (0 = byte, 1 = short, 2 = long) */
271 25, /* bitsize */
272 TRUE, /* pc_relative */
273 0, /* bitpos */
274 complain_overflow_signed,/* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_ARM_XPC25", /* name */
277 FALSE, /* partial_inplace */
278 0x00ffffff, /* src_mask */
279 0x00ffffff, /* dst_mask */
280 TRUE), /* pcrel_offset */
281
282 /* BLX instruction for the Thumb. */
283 HOWTO (R_ARM_THM_XPC22, /* type */
284 2, /* rightshift */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
286 22, /* bitsize */
287 TRUE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_signed,/* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_ARM_THM_XPC22", /* name */
292 FALSE, /* partial_inplace */
293 0x07ff07ff, /* src_mask */
294 0x07ff07ff, /* dst_mask */
295 TRUE), /* pcrel_offset */
296
297 /* These next three relocs are not defined, but we need to fill the space. */
298
299 HOWTO (R_ARM_NONE, /* type */
300 0, /* rightshift */
301 0, /* size (0 = byte, 1 = short, 2 = long) */
302 0, /* bitsize */
303 FALSE, /* pc_relative */
304 0, /* bitpos */
305 complain_overflow_dont,/* complain_on_overflow */
306 bfd_elf_generic_reloc, /* special_function */
307 "R_ARM_unknown_17", /* name */
308 FALSE, /* partial_inplace */
309 0, /* src_mask */
310 0, /* dst_mask */
311 FALSE), /* pcrel_offset */
312
313 HOWTO (R_ARM_NONE, /* type */
314 0, /* rightshift */
315 0, /* size (0 = byte, 1 = short, 2 = long) */
316 0, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_dont,/* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_ARM_unknown_18", /* name */
322 FALSE, /* partial_inplace */
323 0, /* src_mask */
324 0, /* dst_mask */
325 FALSE), /* pcrel_offset */
326
327 HOWTO (R_ARM_NONE, /* type */
328 0, /* rightshift */
329 0, /* size (0 = byte, 1 = short, 2 = long) */
330 0, /* bitsize */
331 FALSE, /* pc_relative */
332 0, /* bitpos */
333 complain_overflow_dont,/* complain_on_overflow */
334 bfd_elf_generic_reloc, /* special_function */
335 "R_ARM_unknown_19", /* name */
336 FALSE, /* partial_inplace */
337 0, /* src_mask */
338 0, /* dst_mask */
339 FALSE), /* pcrel_offset */
340
341 /* Relocs used in ARM Linux */
342
343 HOWTO (R_ARM_COPY, /* type */
344 0, /* rightshift */
345 2, /* size (0 = byte, 1 = short, 2 = long) */
346 32, /* bitsize */
347 FALSE, /* pc_relative */
348 0, /* bitpos */
349 complain_overflow_bitfield,/* complain_on_overflow */
350 bfd_elf_generic_reloc, /* special_function */
351 "R_ARM_COPY", /* name */
352 TRUE, /* partial_inplace */
353 0xffffffff, /* src_mask */
354 0xffffffff, /* dst_mask */
355 FALSE), /* pcrel_offset */
356
357 HOWTO (R_ARM_GLOB_DAT, /* type */
358 0, /* rightshift */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
360 32, /* bitsize */
361 FALSE, /* pc_relative */
362 0, /* bitpos */
363 complain_overflow_bitfield,/* complain_on_overflow */
364 bfd_elf_generic_reloc, /* special_function */
365 "R_ARM_GLOB_DAT", /* name */
366 TRUE, /* partial_inplace */
367 0xffffffff, /* src_mask */
368 0xffffffff, /* dst_mask */
369 FALSE), /* pcrel_offset */
370
371 HOWTO (R_ARM_JUMP_SLOT, /* type */
372 0, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 32, /* bitsize */
375 FALSE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_bitfield,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_ARM_JUMP_SLOT", /* name */
380 TRUE, /* partial_inplace */
381 0xffffffff, /* src_mask */
382 0xffffffff, /* dst_mask */
383 FALSE), /* pcrel_offset */
384
385 HOWTO (R_ARM_RELATIVE, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 32, /* bitsize */
389 FALSE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_bitfield,/* complain_on_overflow */
392 bfd_elf_generic_reloc, /* special_function */
393 "R_ARM_RELATIVE", /* name */
394 TRUE, /* partial_inplace */
395 0xffffffff, /* src_mask */
396 0xffffffff, /* dst_mask */
397 FALSE), /* pcrel_offset */
398
399 HOWTO (R_ARM_GOTOFF, /* type */
400 0, /* rightshift */
401 2, /* size (0 = byte, 1 = short, 2 = long) */
402 32, /* bitsize */
403 FALSE, /* pc_relative */
404 0, /* bitpos */
405 complain_overflow_bitfield,/* complain_on_overflow */
406 bfd_elf_generic_reloc, /* special_function */
407 "R_ARM_GOTOFF", /* name */
408 TRUE, /* partial_inplace */
409 0xffffffff, /* src_mask */
410 0xffffffff, /* dst_mask */
411 FALSE), /* pcrel_offset */
412
413 HOWTO (R_ARM_GOTPC, /* type */
414 0, /* rightshift */
415 2, /* size (0 = byte, 1 = short, 2 = long) */
416 32, /* bitsize */
417 TRUE, /* pc_relative */
418 0, /* bitpos */
419 complain_overflow_bitfield,/* complain_on_overflow */
420 bfd_elf_generic_reloc, /* special_function */
421 "R_ARM_GOTPC", /* name */
422 TRUE, /* partial_inplace */
423 0xffffffff, /* src_mask */
424 0xffffffff, /* dst_mask */
425 TRUE), /* pcrel_offset */
426
427 HOWTO (R_ARM_GOT32, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 32, /* bitsize */
431 FALSE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_bitfield,/* complain_on_overflow */
434 bfd_elf_generic_reloc, /* special_function */
435 "R_ARM_GOT32", /* name */
436 TRUE, /* partial_inplace */
437 0xffffffff, /* src_mask */
438 0xffffffff, /* dst_mask */
439 FALSE), /* pcrel_offset */
440
441 HOWTO (R_ARM_PLT32, /* type */
442 2, /* rightshift */
443 2, /* size (0 = byte, 1 = short, 2 = long) */
444 26, /* bitsize */
445 TRUE, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_bitfield,/* complain_on_overflow */
448 bfd_elf_generic_reloc, /* special_function */
449 "R_ARM_PLT32", /* name */
450 TRUE, /* partial_inplace */
451 0x00ffffff, /* src_mask */
452 0x00ffffff, /* dst_mask */
453 TRUE), /* pcrel_offset */
454
455 HOWTO (R_ARM_CALL, /* type */
456 2, /* rightshift */
457 2, /* size (0 = byte, 1 = short, 2 = long) */
458 24, /* bitsize */
459 TRUE, /* pc_relative */
460 0, /* bitpos */
461 complain_overflow_signed,/* complain_on_overflow */
462 bfd_elf_generic_reloc, /* special_function */
463 "R_ARM_CALL", /* name */
464 FALSE, /* partial_inplace */
465 0x00ffffff, /* src_mask */
466 0x00ffffff, /* dst_mask */
467 TRUE), /* pcrel_offset */
468
469 HOWTO (R_ARM_JUMP24, /* type */
470 2, /* rightshift */
471 2, /* size (0 = byte, 1 = short, 2 = long) */
472 24, /* bitsize */
473 TRUE, /* pc_relative */
474 0, /* bitpos */
475 complain_overflow_signed,/* complain_on_overflow */
476 bfd_elf_generic_reloc, /* special_function */
477 "R_ARM_JUMP24", /* name */
478 FALSE, /* partial_inplace */
479 0x00ffffff, /* src_mask */
480 0x00ffffff, /* dst_mask */
481 TRUE), /* pcrel_offset */
482
483 HOWTO (R_ARM_NONE, /* type */
484 0, /* rightshift */
485 0, /* size (0 = byte, 1 = short, 2 = long) */
486 0, /* bitsize */
487 FALSE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_dont,/* complain_on_overflow */
490 bfd_elf_generic_reloc, /* special_function */
491 "R_ARM_unknown_30", /* name */
492 FALSE, /* partial_inplace */
493 0, /* src_mask */
494 0, /* dst_mask */
495 FALSE), /* pcrel_offset */
496
497 HOWTO (R_ARM_NONE, /* type */
498 0, /* rightshift */
499 0, /* size (0 = byte, 1 = short, 2 = long) */
500 0, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_dont,/* complain_on_overflow */
504 bfd_elf_generic_reloc, /* special_function */
505 "R_ARM_unknown_31", /* name */
506 FALSE, /* partial_inplace */
507 0, /* src_mask */
508 0, /* dst_mask */
509 FALSE), /* pcrel_offset */
510
511 HOWTO (R_ARM_ALU_PCREL7_0, /* type */
512 0, /* rightshift */
513 2, /* size (0 = byte, 1 = short, 2 = long) */
514 12, /* bitsize */
515 TRUE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont,/* complain_on_overflow */
518 bfd_elf_generic_reloc, /* special_function */
519 "R_ARM_ALU_PCREL_7_0", /* name */
520 FALSE, /* partial_inplace */
521 0x00000fff, /* src_mask */
522 0x00000fff, /* dst_mask */
523 TRUE), /* pcrel_offset */
524
525 HOWTO (R_ARM_ALU_PCREL15_8, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 12, /* bitsize */
529 TRUE, /* pc_relative */
530 8, /* bitpos */
531 complain_overflow_dont,/* complain_on_overflow */
532 bfd_elf_generic_reloc, /* special_function */
533 "R_ARM_ALU_PCREL_15_8",/* name */
534 FALSE, /* partial_inplace */
535 0x00000fff, /* src_mask */
536 0x00000fff, /* dst_mask */
537 TRUE), /* pcrel_offset */
538
539 HOWTO (R_ARM_ALU_PCREL23_15, /* type */
540 0, /* rightshift */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
542 12, /* bitsize */
543 TRUE, /* pc_relative */
544 16, /* bitpos */
545 complain_overflow_dont,/* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_ARM_ALU_PCREL_23_15",/* name */
548 FALSE, /* partial_inplace */
549 0x00000fff, /* src_mask */
550 0x00000fff, /* dst_mask */
551 TRUE), /* pcrel_offset */
552
553 HOWTO (R_ARM_LDR_SBREL_11_0, /* type */
554 0, /* rightshift */
555 2, /* size (0 = byte, 1 = short, 2 = long) */
556 12, /* bitsize */
557 FALSE, /* pc_relative */
558 0, /* bitpos */
559 complain_overflow_dont,/* complain_on_overflow */
560 bfd_elf_generic_reloc, /* special_function */
561 "R_ARM_LDR_SBREL_11_0",/* name */
562 FALSE, /* partial_inplace */
563 0x00000fff, /* src_mask */
564 0x00000fff, /* dst_mask */
565 FALSE), /* pcrel_offset */
566
567 HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
568 0, /* rightshift */
569 2, /* size (0 = byte, 1 = short, 2 = long) */
570 8, /* bitsize */
571 FALSE, /* pc_relative */
572 12, /* bitpos */
573 complain_overflow_dont,/* complain_on_overflow */
574 bfd_elf_generic_reloc, /* special_function */
575 "R_ARM_ALU_SBREL_19_12",/* name */
576 FALSE, /* partial_inplace */
577 0x000ff000, /* src_mask */
578 0x000ff000, /* dst_mask */
579 FALSE), /* pcrel_offset */
580
581 HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
582 0, /* rightshift */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
584 8, /* bitsize */
585 FALSE, /* pc_relative */
586 20, /* bitpos */
587 complain_overflow_dont,/* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_ARM_ALU_SBREL_27_20",/* name */
590 FALSE, /* partial_inplace */
591 0x0ff00000, /* src_mask */
592 0x0ff00000, /* dst_mask */
593 FALSE), /* pcrel_offset */
594
595 HOWTO (R_ARM_TARGET1, /* type */
596 0, /* rightshift */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
598 32, /* bitsize */
599 FALSE, /* pc_relative */
600 0, /* bitpos */
601 complain_overflow_dont,/* complain_on_overflow */
602 bfd_elf_generic_reloc, /* special_function */
603 "R_ARM_TARGET1", /* name */
604 FALSE, /* partial_inplace */
605 0xffffffff, /* src_mask */
606 0xffffffff, /* dst_mask */
607 FALSE), /* pcrel_offset */
608
609 HOWTO (R_ARM_ROSEGREL32, /* type */
610 0, /* rightshift */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
612 32, /* bitsize */
613 FALSE, /* pc_relative */
614 0, /* bitpos */
615 complain_overflow_dont,/* complain_on_overflow */
616 bfd_elf_generic_reloc, /* special_function */
617 "R_ARM_ROSEGREL32", /* name */
618 FALSE, /* partial_inplace */
619 0xffffffff, /* src_mask */
620 0xffffffff, /* dst_mask */
621 FALSE), /* pcrel_offset */
622
623 HOWTO (R_ARM_V4BX, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 32, /* bitsize */
627 FALSE, /* pc_relative */
628 0, /* bitpos */
629 complain_overflow_dont,/* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_ARM_V4BX", /* name */
632 FALSE, /* partial_inplace */
633 0xffffffff, /* src_mask */
634 0xffffffff, /* dst_mask */
635 FALSE), /* pcrel_offset */
636
637 HOWTO (R_ARM_TARGET2, /* type */
638 0, /* rightshift */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
640 32, /* bitsize */
641 FALSE, /* pc_relative */
642 0, /* bitpos */
643 complain_overflow_signed,/* complain_on_overflow */
644 bfd_elf_generic_reloc, /* special_function */
645 "R_ARM_TARGET2", /* name */
646 FALSE, /* partial_inplace */
647 0xffffffff, /* src_mask */
648 0xffffffff, /* dst_mask */
649 TRUE), /* pcrel_offset */
650
651 HOWTO (R_ARM_PREL31, /* type */
652 0, /* rightshift */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
654 31, /* bitsize */
655 TRUE, /* pc_relative */
656 0, /* bitpos */
657 complain_overflow_signed,/* complain_on_overflow */
658 bfd_elf_generic_reloc, /* special_function */
659 "R_ARM_PREL31", /* name */
660 FALSE, /* partial_inplace */
661 0x7fffffff, /* src_mask */
662 0x7fffffff, /* dst_mask */
663 TRUE), /* pcrel_offset */
664};
665
666 /* GNU extension to record C++ vtable hierarchy */
667static reloc_howto_type elf32_arm_vtinherit_howto =
668 HOWTO (R_ARM_GNU_VTINHERIT, /* type */
669 0, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 0, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
675 NULL, /* special_function */
676 "R_ARM_GNU_VTINHERIT", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0, /* dst_mask */
680 FALSE); /* pcrel_offset */
681
682 /* GNU extension to record C++ vtable member usage */
683static reloc_howto_type elf32_arm_vtentry_howto =
684 HOWTO (R_ARM_GNU_VTENTRY, /* type */
685 0, /* rightshift */
686 2, /* size (0 = byte, 1 = short, 2 = long) */
687 0, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
691 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
692 "R_ARM_GNU_VTENTRY", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0, /* dst_mask */
696 FALSE); /* pcrel_offset */
697
698 /* 12 bit pc relative */
699static reloc_howto_type elf32_arm_thm_pc11_howto =
700 HOWTO (R_ARM_THM_PC11, /* type */
701 1, /* rightshift */
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 11, /* bitsize */
704 TRUE, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_signed, /* complain_on_overflow */
707 bfd_elf_generic_reloc, /* special_function */
708 "R_ARM_THM_PC11", /* name */
709 FALSE, /* partial_inplace */
710 0x000007ff, /* src_mask */
711 0x000007ff, /* dst_mask */
712 TRUE); /* pcrel_offset */
713
714 /* 12 bit pc relative */
715static reloc_howto_type elf32_arm_thm_pc9_howto =
716 HOWTO (R_ARM_THM_PC9, /* type */
717 1, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 8, /* bitsize */
720 TRUE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 bfd_elf_generic_reloc, /* special_function */
724 "R_ARM_THM_PC9", /* name */
725 FALSE, /* partial_inplace */
726 0x000000ff, /* src_mask */
727 0x000000ff, /* dst_mask */
728 TRUE); /* pcrel_offset */
729
730/* Place relative GOT-indirect. */
731static reloc_howto_type elf32_arm_got_prel =
732 HOWTO (R_ARM_GOT_PREL, /* type */
733 0, /* rightshift */
734 2, /* size (0 = byte, 1 = short, 2 = long) */
735 32, /* bitsize */
736 TRUE, /* pc_relative */
737 0, /* bitpos */
738 complain_overflow_dont, /* complain_on_overflow */
739 bfd_elf_generic_reloc, /* special_function */
740 "R_ARM_GOT_PREL", /* name */
741 FALSE, /* partial_inplace */
742 0xffffffff, /* src_mask */
743 0xffffffff, /* dst_mask */
744 TRUE); /* pcrel_offset */
745
746/* Currently unused relocations. */
747static reloc_howto_type elf32_arm_r_howto[4] =
748{
749 HOWTO (R_ARM_RREL32, /* type */
750 0, /* rightshift */
751 0, /* size (0 = byte, 1 = short, 2 = long) */
752 0, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_dont,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_ARM_RREL32", /* name */
758 FALSE, /* partial_inplace */
759 0, /* src_mask */
760 0, /* dst_mask */
761 FALSE), /* pcrel_offset */
762
763 HOWTO (R_ARM_RABS32, /* type */
764 0, /* rightshift */
765 0, /* size (0 = byte, 1 = short, 2 = long) */
766 0, /* bitsize */
767 FALSE, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_dont,/* complain_on_overflow */
770 bfd_elf_generic_reloc, /* special_function */
771 "R_ARM_RABS32", /* name */
772 FALSE, /* partial_inplace */
773 0, /* src_mask */
774 0, /* dst_mask */
775 FALSE), /* pcrel_offset */
776
777 HOWTO (R_ARM_RPC24, /* type */
778 0, /* rightshift */
779 0, /* size (0 = byte, 1 = short, 2 = long) */
780 0, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_dont,/* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_ARM_RPC24", /* name */
786 FALSE, /* partial_inplace */
787 0, /* src_mask */
788 0, /* dst_mask */
789 FALSE), /* pcrel_offset */
790
791 HOWTO (R_ARM_RBASE, /* type */
792 0, /* rightshift */
793 0, /* size (0 = byte, 1 = short, 2 = long) */
794 0, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont,/* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_ARM_RBASE", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0, /* dst_mask */
803 FALSE) /* pcrel_offset */
804};
805
806static reloc_howto_type *
807elf32_arm_howto_from_type (unsigned int r_type)
808{
809 if (r_type < NUM_ELEM (elf32_arm_howto_table))
810 return &elf32_arm_howto_table[r_type];
811
812 switch (r_type)
813 {
814 case R_ARM_GOT_PREL:
815 return &elf32_arm_got_prel;
816
817 case R_ARM_GNU_VTINHERIT:
818 return &elf32_arm_vtinherit_howto;
819
820 case R_ARM_GNU_VTENTRY:
821 return &elf32_arm_vtentry_howto;
822
823 case R_ARM_THM_PC11:
824 return &elf32_arm_thm_pc11_howto;
825
826 case R_ARM_THM_PC9:
827 return &elf32_arm_thm_pc9_howto;
828
829 case R_ARM_RREL32:
830 case R_ARM_RABS32:
831 case R_ARM_RPC24:
832 case R_ARM_RBASE:
833 return &elf32_arm_r_howto[r_type - R_ARM_RREL32];
834
835 default:
836 return NULL;
837 }
838}
839
840static void
841elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
842 Elf_Internal_Rela * elf_reloc)
843{
844 unsigned int r_type;
845
846 r_type = ELF32_R_TYPE (elf_reloc->r_info);
847 bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
848}
849
850struct elf32_arm_reloc_map
851 {
852 bfd_reloc_code_real_type bfd_reloc_val;
853 unsigned char elf_reloc_val;
854 };
855
856/* All entries in this list must also be present in elf32_arm_howto_table. */
857static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
858 {
859 {BFD_RELOC_NONE, R_ARM_NONE},
860 {BFD_RELOC_ARM_PCREL_BRANCH, R_ARM_PC24},
861 {BFD_RELOC_ARM_PCREL_BLX, R_ARM_XPC25},
862 {BFD_RELOC_THUMB_PCREL_BLX, R_ARM_THM_XPC22},
863 {BFD_RELOC_32, R_ARM_ABS32},
864 {BFD_RELOC_32_PCREL, R_ARM_REL32},
865 {BFD_RELOC_8, R_ARM_ABS8},
866 {BFD_RELOC_16, R_ARM_ABS16},
867 {BFD_RELOC_ARM_OFFSET_IMM, R_ARM_ABS12},
868 {BFD_RELOC_ARM_THUMB_OFFSET, R_ARM_THM_ABS5},
869 {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_PC22},
870 {BFD_RELOC_ARM_COPY, R_ARM_COPY},
871 {BFD_RELOC_ARM_GLOB_DAT, R_ARM_GLOB_DAT},
872 {BFD_RELOC_ARM_JUMP_SLOT, R_ARM_JUMP_SLOT},
873 {BFD_RELOC_ARM_RELATIVE, R_ARM_RELATIVE},
874 {BFD_RELOC_ARM_GOTOFF, R_ARM_GOTOFF},
875 {BFD_RELOC_ARM_GOTPC, R_ARM_GOTPC},
876 {BFD_RELOC_ARM_GOT32, R_ARM_GOT32},
877 {BFD_RELOC_ARM_PLT32, R_ARM_PLT32},
878 {BFD_RELOC_ARM_TARGET1, R_ARM_TARGET1},
879 {BFD_RELOC_ARM_ROSEGREL32, R_ARM_ROSEGREL32},
880 {BFD_RELOC_ARM_SBREL32, R_ARM_SBREL32},
881 {BFD_RELOC_ARM_PREL31, R_ARM_PREL31},
882 {BFD_RELOC_ARM_TARGET2, R_ARM_TARGET2}
883 };
884
885static reloc_howto_type *
886elf32_arm_reloc_type_lookup (abfd, code)
887 bfd *abfd ATTRIBUTE_UNUSED;
888 bfd_reloc_code_real_type code;
889{
890 unsigned int i;
891
892 switch (code)
893 {
894 case BFD_RELOC_VTABLE_INHERIT:
895 return & elf32_arm_vtinherit_howto;
896
897 case BFD_RELOC_VTABLE_ENTRY:
898 return & elf32_arm_vtentry_howto;
899
900 case BFD_RELOC_THUMB_PCREL_BRANCH12:
901 return & elf32_arm_thm_pc11_howto;
902
903 case BFD_RELOC_THUMB_PCREL_BRANCH9:
904 return & elf32_arm_thm_pc9_howto;
905
906 default:
907 for (i = 0; i < NUM_ELEM (elf32_arm_reloc_map); i ++)
908 if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
909 return & elf32_arm_howto_table[elf32_arm_reloc_map[i].elf_reloc_val];
910
911 return NULL;
912 }
913}
914
915/* Support for core dump NOTE sections */
916static bfd_boolean
917elf32_arm_nabi_grok_prstatus (abfd, note)
918 bfd *abfd;
919 Elf_Internal_Note *note;
920{
921 int offset;
922 size_t size;
923
924 switch (note->descsz)
925 {
926 default:
927 return FALSE;
928
929 case 148: /* Linux/ARM 32-bit*/
930 /* pr_cursig */
931 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
932
933 /* pr_pid */
934 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
935
936 /* pr_reg */
937 offset = 72;
938 size = 72;
939
940 break;
941 }
942
943 /* Make a ".reg/999" section. */
944 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
945 size, note->descpos + offset);
946}
947
948static bfd_boolean
949elf32_arm_nabi_grok_psinfo (abfd, note)
950 bfd *abfd;
951 Elf_Internal_Note *note;
952{
953 switch (note->descsz)
954 {
955 default:
956 return FALSE;
957
958 case 124: /* Linux/ARM elf_prpsinfo */
959 elf_tdata (abfd)->core_program
960 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
961 elf_tdata (abfd)->core_command
962 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
963 }
964
965 /* Note that for some reason, a spurious space is tacked
966 onto the end of the args in some (at least one anyway)
967 implementations, so strip it off if it exists. */
968
969 {
970 char *command = elf_tdata (abfd)->core_command;
971 int n = strlen (command);
972
973 if (0 < n && command[n - 1] == ' ')
974 command[n - 1] = '\0';
975 }
976
977 return TRUE;
978}
979
980#define TARGET_LITTLE_SYM bfd_elf32_littlearm_vec
981#define TARGET_LITTLE_NAME "elf32-littlearm"
982#define TARGET_BIG_SYM bfd_elf32_bigarm_vec
983#define TARGET_BIG_NAME "elf32-bigarm"
984
985#define elf_backend_grok_prstatus elf32_arm_nabi_grok_prstatus
986#define elf_backend_grok_psinfo elf32_arm_nabi_grok_psinfo
987
252b5132
RH
988typedef unsigned long int insn32;
989typedef unsigned short int insn16;
990
8cb51566 991/* In lieu of proper flags, assume all EABIv4 objects are interworkable. */
57e8b36a 992#define INTERWORK_FLAG(abfd) \
8cb51566 993 (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) == EF_ARM_EABI_VER4 \
85a84e7a 994 || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
9b485d32 995
252b5132
RH
996/* The linker script knows the section names for placement.
997 The entry_names are used to do simple name mangling on the stubs.
998 Given a function name, and its type, the stub can be found. The
9b485d32 999 name can be changed. The only requirement is the %s be present. */
252b5132
RH
1000#define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1001#define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb"
1002
1003#define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1004#define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm"
1005
1006/* The name of the dynamic interpreter. This is put in the .interp
1007 section. */
1008#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
1009
5e681ec4
PB
1010#ifdef FOUR_WORD_PLT
1011
252b5132
RH
1012/* The first entry in a procedure linkage table looks like
1013 this. It is set up so that any shared library function that is
59f2c4e7 1014 called before the relocation has been set up calls the dynamic
9b485d32 1015 linker first. */
e5a52504 1016static const bfd_vma elf32_arm_plt0_entry [] =
5e681ec4
PB
1017 {
1018 0xe52de004, /* str lr, [sp, #-4]! */
1019 0xe59fe010, /* ldr lr, [pc, #16] */
1020 0xe08fe00e, /* add lr, pc, lr */
1021 0xe5bef008, /* ldr pc, [lr, #8]! */
1022 };
1023
1024/* Subsequent entries in a procedure linkage table look like
1025 this. */
e5a52504 1026static const bfd_vma elf32_arm_plt_entry [] =
5e681ec4
PB
1027 {
1028 0xe28fc600, /* add ip, pc, #NN */
1029 0xe28cca00, /* add ip, ip, #NN */
1030 0xe5bcf000, /* ldr pc, [ip, #NN]! */
1031 0x00000000, /* unused */
1032 };
1033
1034#else
1035
5e681ec4
PB
1036/* The first entry in a procedure linkage table looks like
1037 this. It is set up so that any shared library function that is
1038 called before the relocation has been set up calls the dynamic
1039 linker first. */
e5a52504 1040static const bfd_vma elf32_arm_plt0_entry [] =
917583ad 1041 {
5e681ec4
PB
1042 0xe52de004, /* str lr, [sp, #-4]! */
1043 0xe59fe004, /* ldr lr, [pc, #4] */
1044 0xe08fe00e, /* add lr, pc, lr */
1045 0xe5bef008, /* ldr pc, [lr, #8]! */
1046 0x00000000, /* &GOT[0] - . */
917583ad 1047 };
252b5132
RH
1048
1049/* Subsequent entries in a procedure linkage table look like
1050 this. */
e5a52504 1051static const bfd_vma elf32_arm_plt_entry [] =
5e681ec4
PB
1052 {
1053 0xe28fc600, /* add ip, pc, #0xNN00000 */
1054 0xe28cca00, /* add ip, ip, #0xNN000 */
1055 0xe5bcf000, /* ldr pc, [ip, #0xNNN]! */
1056 };
1057
1058#endif
252b5132 1059
b7693d02
DJ
1060/* An initial stub used if the PLT entry is referenced from Thumb code. */
1061#define PLT_THUMB_STUB_SIZE 4
1062static const bfd_vma elf32_arm_plt_thumb_stub [] =
1063 {
1064 0x4778, /* bx pc */
1065 0x46c0 /* nop */
1066 };
1067
e5a52504
MM
1068/* The entries in a PLT when using a DLL-based target with multiple
1069 address spaces. */
1070static const bfd_vma elf32_arm_symbian_plt_entry [] =
1071 {
83a358aa 1072 0xe51ff004, /* ldr pc, [pc, #-4] */
e5a52504
MM
1073 0x00000000, /* dcd R_ARM_GLOB_DAT(X) */
1074 };
1075
e489d0ae
PB
1076/* Used to build a map of a section. This is required for mixed-endian
1077 code/data. */
1078
1079typedef struct elf32_elf_section_map
1080{
1081 bfd_vma vma;
1082 char type;
1083}
1084elf32_arm_section_map;
1085
1086struct _arm_elf_section_data
1087{
1088 struct bfd_elf_section_data elf;
1089 int mapcount;
1090 elf32_arm_section_map *map;
1091};
1092
1093#define elf32_arm_section_data(sec) \
1094 ((struct _arm_elf_section_data *) elf_section_data (sec))
1095
252b5132
RH
1096/* The ARM linker needs to keep track of the number of relocs that it
1097 decides to copy in check_relocs for each symbol. This is so that
1098 it can discard PC relative relocs if it doesn't need them when
1099 linking with -Bsymbolic. We store the information in a field
1100 extending the regular ELF linker hash table. */
1101
1102/* This structure keeps track of the number of PC relative relocs we
1103 have copied for a given symbol. */
5e681ec4 1104struct elf32_arm_relocs_copied
917583ad
NC
1105 {
1106 /* Next section. */
5e681ec4 1107 struct elf32_arm_relocs_copied * next;
917583ad
NC
1108 /* A section in dynobj. */
1109 asection * section;
1110 /* Number of relocs copied in this section. */
1111 bfd_size_type count;
1112 };
252b5132 1113
ba96a88f 1114/* Arm ELF linker hash entry. */
252b5132 1115struct elf32_arm_link_hash_entry
917583ad
NC
1116 {
1117 struct elf_link_hash_entry root;
252b5132 1118
917583ad 1119 /* Number of PC relative relocs copied for this symbol. */
5e681ec4 1120 struct elf32_arm_relocs_copied * relocs_copied;
b7693d02
DJ
1121
1122 /* We reference count Thumb references to a PLT entry separately,
1123 so that we can emit the Thumb trampoline only if needed. */
1124 bfd_signed_vma plt_thumb_refcount;
1125
1126 /* Since PLT entries have variable size if the Thumb prologue is
1127 used, we need to record the index into .got.plt instead of
1128 recomputing it from the PLT offset. */
1129 bfd_signed_vma plt_got_offset;
917583ad 1130 };
252b5132 1131
252b5132 1132/* Traverse an arm ELF linker hash table. */
252b5132
RH
1133#define elf32_arm_link_hash_traverse(table, func, info) \
1134 (elf_link_hash_traverse \
1135 (&(table)->root, \
b7693d02 1136 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
252b5132
RH
1137 (info)))
1138
1139/* Get the ARM elf linker hash table from a link_info structure. */
1140#define elf32_arm_hash_table(info) \
1141 ((struct elf32_arm_link_hash_table *) ((info)->hash))
1142
9b485d32 1143/* ARM ELF linker hash table. */
252b5132 1144struct elf32_arm_link_hash_table
917583ad
NC
1145 {
1146 /* The main hash table. */
1147 struct elf_link_hash_table root;
252b5132 1148
4cc11e76 1149 /* The size in bytes of the section containing the Thumb-to-ARM glue. */
dc810e39 1150 bfd_size_type thumb_glue_size;
252b5132 1151
4cc11e76 1152 /* The size in bytes of the section containing the ARM-to-Thumb glue. */
dc810e39 1153 bfd_size_type arm_glue_size;
252b5132 1154
4cc11e76 1155 /* An arbitrary input BFD chosen to hold the glue sections. */
917583ad 1156 bfd * bfd_of_glue_owner;
ba96a88f 1157
e489d0ae
PB
1158 /* Nonzero to output a BE8 image. */
1159 int byteswap_code;
1160
9c504268
PB
1161 /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
1162 Nonzero if R_ARM_TARGET1 means R_ARM_ABS32. */
1163 int target1_is_rel;
1164
eb043451
PB
1165 /* The relocation to use for R_ARM_TARGET2 relocations. */
1166 int target2_reloc;
1167
319850b4
JB
1168 /* Nonzero to fix BX instructions for ARMv4 targets. */
1169 int fix_v4bx;
1170
e5a52504
MM
1171 /* The number of bytes in the initial entry in the PLT. */
1172 bfd_size_type plt_header_size;
1173
1174 /* The number of bytes in the subsequent PLT etries. */
1175 bfd_size_type plt_entry_size;
1176
1177 /* True if the target system is Symbian OS. */
1178 int symbian_p;
1179
4e7fd91e
PB
1180 /* True if the target uses REL relocations. */
1181 int use_rel;
1182
5e681ec4
PB
1183 /* Short-cuts to get to dynamic linker sections. */
1184 asection *sgot;
1185 asection *sgotplt;
1186 asection *srelgot;
1187 asection *splt;
1188 asection *srelplt;
1189 asection *sdynbss;
1190 asection *srelbss;
1191
1192 /* Small local sym to section mapping cache. */
1193 struct sym_sec_cache sym_sec;
b7693d02
DJ
1194
1195 /* For convenience in allocate_dynrelocs. */
1196 bfd * obfd;
917583ad 1197 };
252b5132 1198
780a67af
NC
1199/* Create an entry in an ARM ELF linker hash table. */
1200
1201static struct bfd_hash_entry *
57e8b36a
NC
1202elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
1203 struct bfd_hash_table * table,
1204 const char * string)
780a67af
NC
1205{
1206 struct elf32_arm_link_hash_entry * ret =
1207 (struct elf32_arm_link_hash_entry *) entry;
1208
1209 /* Allocate the structure if it has not already been allocated by a
1210 subclass. */
1211 if (ret == (struct elf32_arm_link_hash_entry *) NULL)
57e8b36a
NC
1212 ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
1213 if (ret == NULL)
780a67af
NC
1214 return (struct bfd_hash_entry *) ret;
1215
1216 /* Call the allocation method of the superclass. */
1217 ret = ((struct elf32_arm_link_hash_entry *)
1218 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1219 table, string));
57e8b36a 1220 if (ret != NULL)
b7693d02
DJ
1221 {
1222 ret->relocs_copied = NULL;
1223 ret->plt_thumb_refcount = 0;
1224 ret->plt_got_offset = -1;
1225 }
780a67af
NC
1226
1227 return (struct bfd_hash_entry *) ret;
1228}
1229
5e681ec4
PB
1230/* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
1231 shortcuts to them in our hash table. */
1232
1233static bfd_boolean
57e8b36a 1234create_got_section (bfd *dynobj, struct bfd_link_info *info)
5e681ec4
PB
1235{
1236 struct elf32_arm_link_hash_table *htab;
1237
e5a52504
MM
1238 htab = elf32_arm_hash_table (info);
1239 /* BPABI objects never have a GOT, or associated sections. */
1240 if (htab->symbian_p)
1241 return TRUE;
1242
5e681ec4
PB
1243 if (! _bfd_elf_create_got_section (dynobj, info))
1244 return FALSE;
1245
5e681ec4
PB
1246 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1247 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1248 if (!htab->sgot || !htab->sgotplt)
1249 abort ();
1250
1251 htab->srelgot = bfd_make_section (dynobj, ".rel.got");
1252 if (htab->srelgot == NULL
1253 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1254 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1255 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1256 | SEC_READONLY))
1257 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1258 return FALSE;
1259 return TRUE;
1260}
1261
1262/* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1263 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1264 hash table. */
1265
1266static bfd_boolean
57e8b36a 1267elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5e681ec4
PB
1268{
1269 struct elf32_arm_link_hash_table *htab;
1270
1271 htab = elf32_arm_hash_table (info);
1272 if (!htab->sgot && !create_got_section (dynobj, info))
1273 return FALSE;
1274
1275 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1276 return FALSE;
1277
1278 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1279 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
1280 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1281 if (!info->shared)
1282 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
1283
e5a52504
MM
1284 if (!htab->splt
1285 || !htab->srelplt
1286 || !htab->sdynbss
5e681ec4
PB
1287 || (!info->shared && !htab->srelbss))
1288 abort ();
1289
1290 return TRUE;
1291}
1292
1293/* Copy the extra info we tack onto an elf_link_hash_entry. */
1294
1295static void
1296elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
1297 struct elf_link_hash_entry *dir,
1298 struct elf_link_hash_entry *ind)
1299{
1300 struct elf32_arm_link_hash_entry *edir, *eind;
1301
1302 edir = (struct elf32_arm_link_hash_entry *) dir;
1303 eind = (struct elf32_arm_link_hash_entry *) ind;
1304
1305 if (eind->relocs_copied != NULL)
1306 {
1307 if (edir->relocs_copied != NULL)
1308 {
1309 struct elf32_arm_relocs_copied **pp;
1310 struct elf32_arm_relocs_copied *p;
1311
1312 if (ind->root.type == bfd_link_hash_indirect)
1313 abort ();
1314
1315 /* Add reloc counts against the weak sym to the strong sym
1316 list. Merge any entries against the same section. */
1317 for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
1318 {
1319 struct elf32_arm_relocs_copied *q;
1320
1321 for (q = edir->relocs_copied; q != NULL; q = q->next)
1322 if (q->section == p->section)
1323 {
5e681ec4
PB
1324 q->count += p->count;
1325 *pp = p->next;
1326 break;
1327 }
1328 if (q == NULL)
1329 pp = &p->next;
1330 }
1331 *pp = edir->relocs_copied;
1332 }
1333
1334 edir->relocs_copied = eind->relocs_copied;
1335 eind->relocs_copied = NULL;
1336 }
1337
b7693d02
DJ
1338 /* If the direct symbol already has an associated PLT entry, the
1339 indirect symbol should not. If it doesn't, swap refcount information
1340 from the indirect symbol. */
1341 if (edir->plt_thumb_refcount == 0)
1342 {
1343 edir->plt_thumb_refcount = eind->plt_thumb_refcount;
1344 eind->plt_thumb_refcount = 0;
1345 }
1346 else
1347 BFD_ASSERT (eind->plt_thumb_refcount == 0);
1348
5e681ec4
PB
1349 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1350}
1351
9b485d32 1352/* Create an ARM elf linker hash table. */
252b5132
RH
1353
1354static struct bfd_link_hash_table *
57e8b36a 1355elf32_arm_link_hash_table_create (bfd *abfd)
252b5132
RH
1356{
1357 struct elf32_arm_link_hash_table *ret;
dc810e39 1358 bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
252b5132 1359
57e8b36a
NC
1360 ret = bfd_malloc (amt);
1361 if (ret == NULL)
252b5132
RH
1362 return NULL;
1363
57e8b36a 1364 if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
780a67af 1365 elf32_arm_link_hash_newfunc))
252b5132 1366 {
e2d34d7d 1367 free (ret);
252b5132
RH
1368 return NULL;
1369 }
1370
5e681ec4
PB
1371 ret->sgot = NULL;
1372 ret->sgotplt = NULL;
1373 ret->srelgot = NULL;
1374 ret->splt = NULL;
1375 ret->srelplt = NULL;
1376 ret->sdynbss = NULL;
1377 ret->srelbss = NULL;
252b5132
RH
1378 ret->thumb_glue_size = 0;
1379 ret->arm_glue_size = 0;
1380 ret->bfd_of_glue_owner = NULL;
e489d0ae 1381 ret->byteswap_code = 0;
9c504268 1382 ret->target1_is_rel = 0;
eb043451 1383 ret->target2_reloc = R_ARM_NONE;
e5a52504
MM
1384#ifdef FOUR_WORD_PLT
1385 ret->plt_header_size = 16;
1386 ret->plt_entry_size = 16;
1387#else
1388 ret->plt_header_size = 20;
1389 ret->plt_entry_size = 12;
1390#endif
1391 ret->symbian_p = 0;
4e7fd91e 1392 ret->use_rel = 1;
5e681ec4 1393 ret->sym_sec.abfd = NULL;
b7693d02 1394 ret->obfd = abfd;
252b5132
RH
1395
1396 return &ret->root.root;
1397}
1398
9b485d32
NC
1399/* Locate the Thumb encoded calling stub for NAME. */
1400
252b5132 1401static struct elf_link_hash_entry *
57e8b36a
NC
1402find_thumb_glue (struct bfd_link_info *link_info,
1403 const char *name,
1404 bfd *input_bfd)
252b5132
RH
1405{
1406 char *tmp_name;
1407 struct elf_link_hash_entry *hash;
1408 struct elf32_arm_link_hash_table *hash_table;
1409
1410 /* We need a pointer to the armelf specific hash table. */
1411 hash_table = elf32_arm_hash_table (link_info);
1412
57e8b36a
NC
1413 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1414 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
252b5132
RH
1415
1416 BFD_ASSERT (tmp_name);
1417
1418 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1419
1420 hash = elf_link_hash_lookup
b34976b6 1421 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
252b5132
RH
1422
1423 if (hash == NULL)
1424 /* xgettext:c-format */
d003868e
AM
1425 (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
1426 input_bfd, tmp_name, name);
252b5132
RH
1427
1428 free (tmp_name);
1429
1430 return hash;
1431}
1432
9b485d32
NC
1433/* Locate the ARM encoded calling stub for NAME. */
1434
252b5132 1435static struct elf_link_hash_entry *
57e8b36a
NC
1436find_arm_glue (struct bfd_link_info *link_info,
1437 const char *name,
1438 bfd *input_bfd)
252b5132
RH
1439{
1440 char *tmp_name;
1441 struct elf_link_hash_entry *myh;
1442 struct elf32_arm_link_hash_table *hash_table;
1443
1444 /* We need a pointer to the elfarm specific hash table. */
1445 hash_table = elf32_arm_hash_table (link_info);
1446
57e8b36a
NC
1447 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1448 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
252b5132
RH
1449
1450 BFD_ASSERT (tmp_name);
1451
1452 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1453
1454 myh = elf_link_hash_lookup
b34976b6 1455 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
252b5132
RH
1456
1457 if (myh == NULL)
1458 /* xgettext:c-format */
d003868e
AM
1459 (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
1460 input_bfd, tmp_name, name);
252b5132
RH
1461
1462 free (tmp_name);
1463
1464 return myh;
1465}
1466
9b485d32 1467/* ARM->Thumb glue:
252b5132
RH
1468
1469 .arm
1470 __func_from_arm:
1471 ldr r12, __func_addr
1472 bx r12
1473 __func_addr:
9b485d32 1474 .word func @ behave as if you saw a ARM_32 reloc. */
252b5132
RH
1475
1476#define ARM2THUMB_GLUE_SIZE 12
1477static const insn32 a2t1_ldr_insn = 0xe59fc000;
1478static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
1479static const insn32 a2t3_func_addr_insn = 0x00000001;
1480
9b485d32 1481/* Thumb->ARM: Thumb->(non-interworking aware) ARM
252b5132
RH
1482
1483 .thumb .thumb
1484 .align 2 .align 2
1485 __func_from_thumb: __func_from_thumb:
1486 bx pc push {r6, lr}
1487 nop ldr r6, __func_addr
1488 .arm mov lr, pc
1489 __func_change_to_arm: bx r6
1490 b func .arm
1491 __func_back_to_thumb:
1492 ldmia r13! {r6, lr}
1493 bx lr
1494 __func_addr:
9b485d32 1495 .word func */
252b5132
RH
1496
1497#define THUMB2ARM_GLUE_SIZE 8
1498static const insn16 t2a1_bx_pc_insn = 0x4778;
1499static const insn16 t2a2_noop_insn = 0x46c0;
1500static const insn32 t2a3_b_insn = 0xea000000;
1501
7e392df6 1502#ifndef ELFARM_NABI_C_INCLUDED
b34976b6 1503bfd_boolean
57e8b36a 1504bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
252b5132
RH
1505{
1506 asection * s;
1507 bfd_byte * foo;
1508 struct elf32_arm_link_hash_table * globals;
1509
1510 globals = elf32_arm_hash_table (info);
1511
1512 BFD_ASSERT (globals != NULL);
1513
1514 if (globals->arm_glue_size != 0)
1515 {
1516 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1517
dc810e39
AM
1518 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1519 ARM2THUMB_GLUE_SECTION_NAME);
252b5132
RH
1520
1521 BFD_ASSERT (s != NULL);
1522
57e8b36a 1523 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
252b5132 1524
eea6121a 1525 s->size = globals->arm_glue_size;
252b5132
RH
1526 s->contents = foo;
1527 }
1528
1529 if (globals->thumb_glue_size != 0)
1530 {
1531 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1532
1533 s = bfd_get_section_by_name
1534 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1535
1536 BFD_ASSERT (s != NULL);
1537
57e8b36a 1538 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
252b5132 1539
eea6121a 1540 s->size = globals->thumb_glue_size;
252b5132
RH
1541 s->contents = foo;
1542 }
1543
b34976b6 1544 return TRUE;
252b5132
RH
1545}
1546
1547static void
57e8b36a
NC
1548record_arm_to_thumb_glue (struct bfd_link_info * link_info,
1549 struct elf_link_hash_entry * h)
252b5132
RH
1550{
1551 const char * name = h->root.root.string;
63b0f745 1552 asection * s;
252b5132
RH
1553 char * tmp_name;
1554 struct elf_link_hash_entry * myh;
14a793b2 1555 struct bfd_link_hash_entry * bh;
252b5132 1556 struct elf32_arm_link_hash_table * globals;
dc810e39 1557 bfd_vma val;
252b5132
RH
1558
1559 globals = elf32_arm_hash_table (link_info);
1560
1561 BFD_ASSERT (globals != NULL);
1562 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1563
1564 s = bfd_get_section_by_name
1565 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
1566
252b5132
RH
1567 BFD_ASSERT (s != NULL);
1568
57e8b36a 1569 tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
252b5132
RH
1570
1571 BFD_ASSERT (tmp_name);
1572
1573 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1574
1575 myh = elf_link_hash_lookup
b34976b6 1576 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
252b5132
RH
1577
1578 if (myh != NULL)
1579 {
9b485d32 1580 /* We've already seen this guy. */
252b5132 1581 free (tmp_name);
9b485d32 1582 return;
252b5132
RH
1583 }
1584
57e8b36a
NC
1585 /* The only trick here is using hash_table->arm_glue_size as the value.
1586 Even though the section isn't allocated yet, this is where we will be
1587 putting it. */
14a793b2 1588 bh = NULL;
dc810e39
AM
1589 val = globals->arm_glue_size + 1;
1590 _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
1591 tmp_name, BSF_GLOBAL, s, val,
b34976b6 1592 NULL, TRUE, FALSE, &bh);
252b5132 1593
b7693d02
DJ
1594 myh = (struct elf_link_hash_entry *) bh;
1595 myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1596 myh->forced_local = 1;
1597
252b5132
RH
1598 free (tmp_name);
1599
1600 globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
1601
1602 return;
1603}
1604
1605static void
57e8b36a
NC
1606record_thumb_to_arm_glue (struct bfd_link_info *link_info,
1607 struct elf_link_hash_entry *h)
252b5132
RH
1608{
1609 const char *name = h->root.root.string;
63b0f745 1610 asection *s;
252b5132
RH
1611 char *tmp_name;
1612 struct elf_link_hash_entry *myh;
14a793b2 1613 struct bfd_link_hash_entry *bh;
252b5132 1614 struct elf32_arm_link_hash_table *hash_table;
dc810e39 1615 bfd_vma val;
252b5132
RH
1616
1617 hash_table = elf32_arm_hash_table (link_info);
1618
1619 BFD_ASSERT (hash_table != NULL);
1620 BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
1621
1622 s = bfd_get_section_by_name
1623 (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1624
1625 BFD_ASSERT (s != NULL);
1626
57e8b36a
NC
1627 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1628 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
252b5132
RH
1629
1630 BFD_ASSERT (tmp_name);
1631
1632 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1633
1634 myh = elf_link_hash_lookup
b34976b6 1635 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
252b5132
RH
1636
1637 if (myh != NULL)
1638 {
9b485d32 1639 /* We've already seen this guy. */
252b5132 1640 free (tmp_name);
9b485d32 1641 return;
252b5132
RH
1642 }
1643
14a793b2 1644 bh = NULL;
dc810e39
AM
1645 val = hash_table->thumb_glue_size + 1;
1646 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1647 tmp_name, BSF_GLOBAL, s, val,
b34976b6 1648 NULL, TRUE, FALSE, &bh);
252b5132 1649
9b485d32 1650 /* If we mark it 'Thumb', the disassembler will do a better job. */
14a793b2 1651 myh = (struct elf_link_hash_entry *) bh;
b7693d02
DJ
1652 myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
1653 myh->forced_local = 1;
252b5132
RH
1654
1655 free (tmp_name);
1656
252b5132
RH
1657#define CHANGE_TO_ARM "__%s_change_to_arm"
1658#define BACK_FROM_ARM "__%s_back_from_arm"
1659
9b485d32 1660 /* Allocate another symbol to mark where we switch to Arm mode. */
57e8b36a
NC
1661 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1662 + strlen (CHANGE_TO_ARM) + 1);
252b5132
RH
1663
1664 BFD_ASSERT (tmp_name);
1665
1666 sprintf (tmp_name, CHANGE_TO_ARM, name);
1667
14a793b2 1668 bh = NULL;
dc810e39
AM
1669 val = hash_table->thumb_glue_size + 4,
1670 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1671 tmp_name, BSF_LOCAL, s, val,
b34976b6 1672 NULL, TRUE, FALSE, &bh);
252b5132
RH
1673
1674 free (tmp_name);
1675
1676 hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
1677
1678 return;
1679}
1680
8afb0e02
NC
1681/* Add the glue sections to ABFD. This function is called from the
1682 linker scripts in ld/emultempl/{armelf}.em. */
9b485d32 1683
b34976b6 1684bfd_boolean
57e8b36a
NC
1685bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
1686 struct bfd_link_info *info)
252b5132 1687{
252b5132
RH
1688 flagword flags;
1689 asection *sec;
1690
8afb0e02
NC
1691 /* If we are only performing a partial
1692 link do not bother adding the glue. */
1049f94e 1693 if (info->relocatable)
b34976b6 1694 return TRUE;
252b5132 1695
252b5132
RH
1696 sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1697
1698 if (sec == NULL)
1699 {
57db232e
NC
1700 /* Note: we do not include the flag SEC_LINKER_CREATED, as this
1701 will prevent elf_link_input_bfd() from processing the contents
1702 of this section. */
811b4bf6 1703 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
252b5132
RH
1704
1705 sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1706
1707 if (sec == NULL
1708 || !bfd_set_section_flags (abfd, sec, flags)
1709 || !bfd_set_section_alignment (abfd, sec, 2))
b34976b6 1710 return FALSE;
9a5aca8c 1711
57db232e
NC
1712 /* Set the gc mark to prevent the section from being removed by garbage
1713 collection, despite the fact that no relocs refer to this section. */
1714 sec->gc_mark = 1;
252b5132
RH
1715 }
1716
1717 sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1718
1719 if (sec == NULL)
1720 {
57e8b36a
NC
1721 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1722 | SEC_CODE | SEC_READONLY;
252b5132
RH
1723
1724 sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1725
1726 if (sec == NULL
1727 || !bfd_set_section_flags (abfd, sec, flags)
1728 || !bfd_set_section_alignment (abfd, sec, 2))
b34976b6 1729 return FALSE;
9a5aca8c 1730
57db232e 1731 sec->gc_mark = 1;
252b5132
RH
1732 }
1733
b34976b6 1734 return TRUE;
8afb0e02
NC
1735}
1736
1737/* Select a BFD to be used to hold the sections used by the glue code.
1738 This function is called from the linker scripts in ld/emultempl/
1739 {armelf/pe}.em */
1740
b34976b6 1741bfd_boolean
57e8b36a 1742bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
8afb0e02
NC
1743{
1744 struct elf32_arm_link_hash_table *globals;
1745
1746 /* If we are only performing a partial link
1747 do not bother getting a bfd to hold the glue. */
1049f94e 1748 if (info->relocatable)
b34976b6 1749 return TRUE;
8afb0e02 1750
b7693d02
DJ
1751 /* Make sure we don't attach the glue sections to a dynamic object. */
1752 BFD_ASSERT (!(abfd->flags & DYNAMIC));
1753
8afb0e02
NC
1754 globals = elf32_arm_hash_table (info);
1755
1756 BFD_ASSERT (globals != NULL);
1757
1758 if (globals->bfd_of_glue_owner != NULL)
b34976b6 1759 return TRUE;
8afb0e02 1760
252b5132
RH
1761 /* Save the bfd for later use. */
1762 globals->bfd_of_glue_owner = abfd;
cedb70c5 1763
b34976b6 1764 return TRUE;
252b5132
RH
1765}
1766
b34976b6 1767bfd_boolean
57e8b36a
NC
1768bfd_elf32_arm_process_before_allocation (bfd *abfd,
1769 struct bfd_link_info *link_info,
eb043451 1770 int byteswap_code)
252b5132
RH
1771{
1772 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 1773 Elf_Internal_Rela *internal_relocs = NULL;
252b5132
RH
1774 Elf_Internal_Rela *irel, *irelend;
1775 bfd_byte *contents = NULL;
252b5132
RH
1776
1777 asection *sec;
1778 struct elf32_arm_link_hash_table *globals;
1779
1780 /* If we are only performing a partial link do not bother
1781 to construct any glue. */
1049f94e 1782 if (link_info->relocatable)
b34976b6 1783 return TRUE;
252b5132
RH
1784
1785 /* Here we have a bfd that is to be included on the link. We have a hook
1786 to do reloc rummaging, before section sizes are nailed down. */
252b5132
RH
1787 globals = elf32_arm_hash_table (link_info);
1788
1789 BFD_ASSERT (globals != NULL);
1790 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1791
e489d0ae
PB
1792 if (byteswap_code && !bfd_big_endian (abfd))
1793 {
d003868e
AM
1794 _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
1795 abfd);
e489d0ae
PB
1796 return FALSE;
1797 }
1798 globals->byteswap_code = byteswap_code;
f21f3fe0 1799
252b5132
RH
1800 /* Rummage around all the relocs and map the glue vectors. */
1801 sec = abfd->sections;
1802
1803 if (sec == NULL)
b34976b6 1804 return TRUE;
252b5132
RH
1805
1806 for (; sec != NULL; sec = sec->next)
1807 {
1808 if (sec->reloc_count == 0)
1809 continue;
1810
1811 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
252b5132 1812
9b485d32 1813 /* Load the relocs. */
6cdc0ccc 1814 internal_relocs
57e8b36a 1815 = _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
45d6a902 1816 (Elf_Internal_Rela *) NULL, FALSE);
252b5132 1817
6cdc0ccc
AM
1818 if (internal_relocs == NULL)
1819 goto error_return;
252b5132 1820
6cdc0ccc
AM
1821 irelend = internal_relocs + sec->reloc_count;
1822 for (irel = internal_relocs; irel < irelend; irel++)
252b5132
RH
1823 {
1824 long r_type;
1825 unsigned long r_index;
252b5132
RH
1826
1827 struct elf_link_hash_entry *h;
1828
1829 r_type = ELF32_R_TYPE (irel->r_info);
1830 r_index = ELF32_R_SYM (irel->r_info);
1831
9b485d32 1832 /* These are the only relocation types we care about. */
ba96a88f 1833 if ( r_type != R_ARM_PC24
b7693d02 1834 && r_type != R_ARM_PLT32
5b5bb741
PB
1835#ifndef OLD_ARM_ABI
1836 && r_type != R_ARM_CALL
1837 && r_type != R_ARM_JUMP24
1838#endif
252b5132
RH
1839 && r_type != R_ARM_THM_PC22)
1840 continue;
1841
1842 /* Get the section contents if we haven't done so already. */
1843 if (contents == NULL)
1844 {
1845 /* Get cached copy if it exists. */
1846 if (elf_section_data (sec)->this_hdr.contents != NULL)
1847 contents = elf_section_data (sec)->this_hdr.contents;
1848 else
1849 {
1850 /* Go get them off disk. */
57e8b36a 1851 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
1852 goto error_return;
1853 }
1854 }
1855
a7c10850 1856 /* If the relocation is not against a symbol it cannot concern us. */
252b5132
RH
1857 h = NULL;
1858
9b485d32 1859 /* We don't care about local symbols. */
252b5132
RH
1860 if (r_index < symtab_hdr->sh_info)
1861 continue;
1862
9b485d32 1863 /* This is an external symbol. */
252b5132
RH
1864 r_index -= symtab_hdr->sh_info;
1865 h = (struct elf_link_hash_entry *)
1866 elf_sym_hashes (abfd)[r_index];
1867
1868 /* If the relocation is against a static symbol it must be within
1869 the current section and so cannot be a cross ARM/Thumb relocation. */
1870 if (h == NULL)
1871 continue;
1872
b7693d02
DJ
1873 /* If the call will go through a PLT entry then we do not need
1874 glue. */
1875 if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
1876 continue;
1877
252b5132
RH
1878 switch (r_type)
1879 {
1880 case R_ARM_PC24:
5b5bb741
PB
1881#ifndef OLD_ARM_ABI
1882 case R_ARM_CALL:
1883 case R_ARM_JUMP24:
1884#endif
252b5132 1885 /* This one is a call from arm code. We need to look up
2f0ca46a 1886 the target of the call. If it is a thumb target, we
252b5132 1887 insert glue. */
252b5132
RH
1888 if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
1889 record_arm_to_thumb_glue (link_info, h);
1890 break;
1891
1892 case R_ARM_THM_PC22:
f21f3fe0 1893 /* This one is a call from thumb code. We look
2f0ca46a 1894 up the target of the call. If it is not a thumb
bcbdc74c 1895 target, we insert glue. */
252b5132
RH
1896 if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
1897 record_thumb_to_arm_glue (link_info, h);
1898 break;
1899
1900 default:
1901 break;
1902 }
1903 }
6cdc0ccc
AM
1904
1905 if (contents != NULL
1906 && elf_section_data (sec)->this_hdr.contents != contents)
1907 free (contents);
1908 contents = NULL;
1909
1910 if (internal_relocs != NULL
1911 && elf_section_data (sec)->relocs != internal_relocs)
1912 free (internal_relocs);
1913 internal_relocs = NULL;
252b5132
RH
1914 }
1915
b34976b6 1916 return TRUE;
9a5aca8c 1917
252b5132 1918error_return:
6cdc0ccc
AM
1919 if (contents != NULL
1920 && elf_section_data (sec)->this_hdr.contents != contents)
1921 free (contents);
1922 if (internal_relocs != NULL
1923 && elf_section_data (sec)->relocs != internal_relocs)
1924 free (internal_relocs);
9a5aca8c 1925
b34976b6 1926 return FALSE;
252b5132 1927}
7e392df6 1928#endif
252b5132 1929
eb043451
PB
1930
1931#ifndef OLD_ARM_ABI
1932/* Set target relocation values needed during linking. */
1933
1934void
1935bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
1936 int target1_is_rel,
319850b4
JB
1937 char * target2_type,
1938 int fix_v4bx)
eb043451
PB
1939{
1940 struct elf32_arm_link_hash_table *globals;
1941
1942 globals = elf32_arm_hash_table (link_info);
1943
1944 globals->target1_is_rel = target1_is_rel;
1945 if (strcmp (target2_type, "rel") == 0)
1946 globals->target2_reloc = R_ARM_REL32;
eeac373a
PB
1947 else if (strcmp (target2_type, "abs") == 0)
1948 globals->target2_reloc = R_ARM_ABS32;
eb043451
PB
1949 else if (strcmp (target2_type, "got-rel") == 0)
1950 globals->target2_reloc = R_ARM_GOT_PREL;
1951 else
1952 {
1953 _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
1954 target2_type);
1955 }
319850b4 1956 globals->fix_v4bx = fix_v4bx;
eb043451
PB
1957}
1958#endif
1959
252b5132
RH
1960/* The thumb form of a long branch is a bit finicky, because the offset
1961 encoding is split over two fields, each in it's own instruction. They
f21f3fe0 1962 can occur in any order. So given a thumb form of long branch, and an
252b5132 1963 offset, insert the offset into the thumb branch and return finished
f21f3fe0 1964 instruction.
252b5132 1965
f21f3fe0 1966 It takes two thumb instructions to encode the target address. Each has
4cc11e76 1967 11 bits to invest. The upper 11 bits are stored in one (identified by
f21f3fe0
UD
1968 H-0.. see below), the lower 11 bits are stored in the other (identified
1969 by H-1).
252b5132 1970
f21f3fe0 1971 Combine together and shifted left by 1 (it's a half word address) and
252b5132
RH
1972 there you have it.
1973
1974 Op: 1111 = F,
1975 H-0, upper address-0 = 000
1976 Op: 1111 = F,
1977 H-1, lower address-0 = 800
1978
f21f3fe0 1979 They can be ordered either way, but the arm tools I've seen always put
252b5132
RH
1980 the lower one first. It probably doesn't matter. krk@cygnus.com
1981
1982 XXX: Actually the order does matter. The second instruction (H-1)
1983 moves the computed address into the PC, so it must be the second one
1984 in the sequence. The problem, however is that whilst little endian code
1985 stores the instructions in HI then LOW order, big endian code does the
dfc5f959 1986 reverse. nickc@cygnus.com. */
252b5132 1987
dfc5f959
NC
1988#define LOW_HI_ORDER 0xF800F000
1989#define HI_LOW_ORDER 0xF000F800
252b5132
RH
1990
1991static insn32
57e8b36a 1992insert_thumb_branch (insn32 br_insn, int rel_off)
252b5132
RH
1993{
1994 unsigned int low_bits;
1995 unsigned int high_bits;
1996
252b5132
RH
1997 BFD_ASSERT ((rel_off & 1) != 1);
1998
dfc5f959
NC
1999 rel_off >>= 1; /* Half word aligned address. */
2000 low_bits = rel_off & 0x000007FF; /* The bottom 11 bits. */
2001 high_bits = (rel_off >> 11) & 0x000007FF; /* The top 11 bits. */
252b5132
RH
2002
2003 if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
2004 br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
2005 else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
2006 br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
2007 else
9b485d32 2008 /* FIXME: abort is probably not the right call. krk@cygnus.com */
57e8b36a 2009 abort (); /* Error - not a valid branch instruction form. */
252b5132 2010
252b5132
RH
2011 return br_insn;
2012}
2013
9b485d32
NC
2014/* Thumb code calling an ARM function. */
2015
252b5132 2016static int
57e8b36a
NC
2017elf32_thumb_to_arm_stub (struct bfd_link_info * info,
2018 const char * name,
2019 bfd * input_bfd,
2020 bfd * output_bfd,
2021 asection * input_section,
2022 bfd_byte * hit_data,
2023 asection * sym_sec,
2024 bfd_vma offset,
2025 bfd_signed_vma addend,
2026 bfd_vma val)
252b5132 2027{
bcbdc74c 2028 asection * s = 0;
dc810e39 2029 bfd_vma my_offset;
252b5132
RH
2030 unsigned long int tmp;
2031 long int ret_offset;
bcbdc74c
NC
2032 struct elf_link_hash_entry * myh;
2033 struct elf32_arm_link_hash_table * globals;
252b5132
RH
2034
2035 myh = find_thumb_glue (info, name, input_bfd);
2036 if (myh == NULL)
b34976b6 2037 return FALSE;
252b5132
RH
2038
2039 globals = elf32_arm_hash_table (info);
2040
2041 BFD_ASSERT (globals != NULL);
2042 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2043
2044 my_offset = myh->root.u.def.value;
2045
2046 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2047 THUMB2ARM_GLUE_SECTION_NAME);
2048
2049 BFD_ASSERT (s != NULL);
2050 BFD_ASSERT (s->contents != NULL);
2051 BFD_ASSERT (s->output_section != NULL);
2052
2053 if ((my_offset & 0x01) == 0x01)
2054 {
2055 if (sym_sec != NULL
2056 && sym_sec->owner != NULL
2057 && !INTERWORK_FLAG (sym_sec->owner))
2058 {
8f615d07 2059 (*_bfd_error_handler)
d003868e
AM
2060 (_("%B(%s): warning: interworking not enabled.\n"
2061 " first occurrence: %B: thumb call to arm"),
2062 sym_sec->owner, input_bfd, name);
252b5132 2063
b34976b6 2064 return FALSE;
252b5132
RH
2065 }
2066
2067 --my_offset;
2068 myh->root.u.def.value = my_offset;
2069
dc810e39 2070 bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
252b5132
RH
2071 s->contents + my_offset);
2072
dc810e39 2073 bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
252b5132
RH
2074 s->contents + my_offset + 2);
2075
2076 ret_offset =
9b485d32
NC
2077 /* Address of destination of the stub. */
2078 ((bfd_signed_vma) val)
252b5132 2079 - ((bfd_signed_vma)
57e8b36a
NC
2080 /* Offset from the start of the current section
2081 to the start of the stubs. */
9b485d32
NC
2082 (s->output_offset
2083 /* Offset of the start of this stub from the start of the stubs. */
2084 + my_offset
2085 /* Address of the start of the current section. */
2086 + s->output_section->vma)
2087 /* The branch instruction is 4 bytes into the stub. */
2088 + 4
2089 /* ARM branches work from the pc of the instruction + 8. */
2090 + 8);
252b5132
RH
2091
2092 bfd_put_32 (output_bfd,
dc810e39 2093 (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
252b5132
RH
2094 s->contents + my_offset + 4);
2095 }
2096
2097 BFD_ASSERT (my_offset <= globals->thumb_glue_size);
2098
427bfd90
NC
2099 /* Now go back and fix up the original BL insn to point to here. */
2100 ret_offset =
2101 /* Address of where the stub is located. */
2102 (s->output_section->vma + s->output_offset + my_offset)
2103 /* Address of where the BL is located. */
57e8b36a
NC
2104 - (input_section->output_section->vma + input_section->output_offset
2105 + offset)
427bfd90
NC
2106 /* Addend in the relocation. */
2107 - addend
2108 /* Biassing for PC-relative addressing. */
2109 - 8;
252b5132
RH
2110
2111 tmp = bfd_get_32 (input_bfd, hit_data
2112 - input_section->vma);
2113
2114 bfd_put_32 (output_bfd,
dc810e39 2115 (bfd_vma) insert_thumb_branch (tmp, ret_offset),
252b5132
RH
2116 hit_data - input_section->vma);
2117
b34976b6 2118 return TRUE;
252b5132
RH
2119}
2120
9b485d32
NC
2121/* Arm code calling a Thumb function. */
2122
252b5132 2123static int
57e8b36a
NC
2124elf32_arm_to_thumb_stub (struct bfd_link_info * info,
2125 const char * name,
2126 bfd * input_bfd,
2127 bfd * output_bfd,
2128 asection * input_section,
2129 bfd_byte * hit_data,
2130 asection * sym_sec,
2131 bfd_vma offset,
2132 bfd_signed_vma addend,
2133 bfd_vma val)
252b5132
RH
2134{
2135 unsigned long int tmp;
dc810e39 2136 bfd_vma my_offset;
bcbdc74c 2137 asection * s;
252b5132 2138 long int ret_offset;
bcbdc74c
NC
2139 struct elf_link_hash_entry * myh;
2140 struct elf32_arm_link_hash_table * globals;
252b5132
RH
2141
2142 myh = find_arm_glue (info, name, input_bfd);
2143 if (myh == NULL)
b34976b6 2144 return FALSE;
252b5132
RH
2145
2146 globals = elf32_arm_hash_table (info);
2147
2148 BFD_ASSERT (globals != NULL);
2149 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2150
2151 my_offset = myh->root.u.def.value;
2152 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2153 ARM2THUMB_GLUE_SECTION_NAME);
2154 BFD_ASSERT (s != NULL);
2155 BFD_ASSERT (s->contents != NULL);
2156 BFD_ASSERT (s->output_section != NULL);
2157
2158 if ((my_offset & 0x01) == 0x01)
2159 {
2160 if (sym_sec != NULL
2161 && sym_sec->owner != NULL
2162 && !INTERWORK_FLAG (sym_sec->owner))
2163 {
8f615d07 2164 (*_bfd_error_handler)
d003868e
AM
2165 (_("%B(%s): warning: interworking not enabled.\n"
2166 " first occurrence: %B: arm call to thumb"),
2167 sym_sec->owner, input_bfd, name);
252b5132 2168 }
9b485d32 2169
252b5132
RH
2170 --my_offset;
2171 myh->root.u.def.value = my_offset;
2172
dc810e39 2173 bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
252b5132
RH
2174 s->contents + my_offset);
2175
dc810e39 2176 bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
252b5132
RH
2177 s->contents + my_offset + 4);
2178
2179 /* It's a thumb address. Add the low order bit. */
2180 bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
2181 s->contents + my_offset + 8);
2182 }
2183
2184 BFD_ASSERT (my_offset <= globals->arm_glue_size);
2185
2186 tmp = bfd_get_32 (input_bfd, hit_data);
2187 tmp = tmp & 0xFF000000;
2188
9b485d32 2189 /* Somehow these are both 4 too far, so subtract 8. */
dc810e39
AM
2190 ret_offset = (s->output_offset
2191 + my_offset
2192 + s->output_section->vma
2193 - (input_section->output_offset
2194 + input_section->output_section->vma
2195 + offset + addend)
2196 - 8);
9a5aca8c 2197
252b5132
RH
2198 tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
2199
dc810e39 2200 bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
252b5132 2201
b34976b6 2202 return TRUE;
252b5132
RH
2203}
2204
eb043451
PB
2205
2206#ifndef OLD_ARM_ABI
2207/* Some relocations map to different relocations depending on the
2208 target. Return the real relocation. */
2209static int
2210arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
2211 int r_type)
2212{
2213 switch (r_type)
2214 {
2215 case R_ARM_TARGET1:
2216 if (globals->target1_is_rel)
2217 return R_ARM_REL32;
2218 else
2219 return R_ARM_ABS32;
2220
2221 case R_ARM_TARGET2:
2222 return globals->target2_reloc;
2223
2224 default:
2225 return r_type;
2226 }
2227}
2228#endif /* OLD_ARM_ABI */
2229
2230
252b5132 2231/* Perform a relocation as part of a final link. */
9b485d32 2232
252b5132 2233static bfd_reloc_status_type
57e8b36a
NC
2234elf32_arm_final_link_relocate (reloc_howto_type * howto,
2235 bfd * input_bfd,
2236 bfd * output_bfd,
2237 asection * input_section,
2238 bfd_byte * contents,
2239 Elf_Internal_Rela * rel,
2240 bfd_vma value,
2241 struct bfd_link_info * info,
2242 asection * sym_sec,
2243 const char * sym_name,
2244 int sym_flags,
0945cdfd
DJ
2245 struct elf_link_hash_entry * h,
2246 bfd_boolean * unresolved_reloc_p)
252b5132
RH
2247{
2248 unsigned long r_type = howto->type;
2249 unsigned long r_symndx;
2250 bfd_byte * hit_data = contents + rel->r_offset;
2251 bfd * dynobj = NULL;
2252 Elf_Internal_Shdr * symtab_hdr;
2253 struct elf_link_hash_entry ** sym_hashes;
2254 bfd_vma * local_got_offsets;
2255 asection * sgot = NULL;
2256 asection * splt = NULL;
2257 asection * sreloc = NULL;
252b5132 2258 bfd_vma addend;
ba96a88f
NC
2259 bfd_signed_vma signed_addend;
2260 struct elf32_arm_link_hash_table * globals;
f21f3fe0 2261
9c504268
PB
2262 globals = elf32_arm_hash_table (info);
2263
2264#ifndef OLD_ARM_ABI
2265 /* Some relocation type map to different relocations depending on the
2266 target. We pick the right one here. */
eb043451
PB
2267 r_type = arm_real_reloc_type (globals, r_type);
2268 if (r_type != howto->type)
2269 howto = elf32_arm_howto_from_type (r_type);
9c504268
PB
2270#endif /* OLD_ARM_ABI */
2271
cac15327
NC
2272 /* If the start address has been set, then set the EF_ARM_HASENTRY
2273 flag. Setting this more than once is redundant, but the cost is
2274 not too high, and it keeps the code simple.
99e4ae17 2275
cac15327
NC
2276 The test is done here, rather than somewhere else, because the
2277 start address is only set just before the final link commences.
2278
2279 Note - if the user deliberately sets a start address of 0, the
2280 flag will not be set. */
2281 if (bfd_get_start_address (output_bfd) != 0)
2282 elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
99e4ae17 2283
252b5132
RH
2284 dynobj = elf_hash_table (info)->dynobj;
2285 if (dynobj)
2286 {
2287 sgot = bfd_get_section_by_name (dynobj, ".got");
2288 splt = bfd_get_section_by_name (dynobj, ".plt");
2289 }
2290 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2291 sym_hashes = elf_sym_hashes (input_bfd);
2292 local_got_offsets = elf_local_got_offsets (input_bfd);
2293 r_symndx = ELF32_R_SYM (rel->r_info);
2294
4e7fd91e 2295 if (globals->use_rel)
ba96a88f 2296 {
4e7fd91e
PB
2297 addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
2298
2299 if (addend & ((howto->src_mask + 1) >> 1))
2300 {
2301 signed_addend = -1;
2302 signed_addend &= ~ howto->src_mask;
2303 signed_addend |= addend;
2304 }
2305 else
2306 signed_addend = addend;
ba96a88f
NC
2307 }
2308 else
4e7fd91e 2309 addend = signed_addend = rel->r_addend;
f21f3fe0 2310
252b5132
RH
2311 switch (r_type)
2312 {
2313 case R_ARM_NONE:
28a094c2
DJ
2314 /* We don't need to find a value for this symbol. It's just a
2315 marker. */
2316 *unresolved_reloc_p = FALSE;
252b5132
RH
2317 return bfd_reloc_ok;
2318
2319 case R_ARM_PC24:
2320 case R_ARM_ABS32:
2321 case R_ARM_REL32:
dfc5f959 2322#ifndef OLD_ARM_ABI
5b5bb741
PB
2323 case R_ARM_CALL:
2324 case R_ARM_JUMP24:
dfc5f959 2325 case R_ARM_XPC25:
eb043451 2326 case R_ARM_PREL31:
dfc5f959 2327#endif
7359ea65 2328 case R_ARM_PLT32:
5e681ec4
PB
2329 /* r_symndx will be zero only for relocs against symbols
2330 from removed linkonce sections, or sections discarded by
2331 a linker script. */
2332 if (r_symndx == 0)
2333 return bfd_reloc_ok;
2334
7359ea65
DJ
2335 /* Handle relocations which should use the PLT entry. ABS32/REL32
2336 will use the symbol's value, which may point to a PLT entry, but we
2337 don't need to handle that here. If we created a PLT entry, all
2338 branches in this object should go to it. */
ee06dc07 2339 if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
7359ea65 2340 && h != NULL
c84cd8ee 2341 && splt != NULL
7359ea65
DJ
2342 && h->plt.offset != (bfd_vma) -1)
2343 {
c84cd8ee
DJ
2344 /* If we've created a .plt section, and assigned a PLT entry to
2345 this function, it should not be known to bind locally. If
2346 it were, we would have cleared the PLT entry. */
7359ea65
DJ
2347 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
2348
2349 value = (splt->output_section->vma
2350 + splt->output_offset
2351 + h->plt.offset);
0945cdfd 2352 *unresolved_reloc_p = FALSE;
7359ea65
DJ
2353 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2354 contents, rel->r_offset, value,
2355 (bfd_vma) 0);
2356 }
2357
67687978
PB
2358 /* When generating a shared object or relocatable executable, these
2359 relocations are copied into the output file to be resolved at
2360 run time. */
2361 if ((info->shared || globals->root.is_relocatable_executable)
7359ea65 2362 && (input_section->flags & SEC_ALLOC)
ee06dc07
PB
2363 && (r_type != R_ARM_REL32
2364 || !SYMBOL_CALLS_LOCAL (info, h))
7359ea65
DJ
2365 && (h == NULL
2366 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2367 || h->root.type != bfd_link_hash_undefweak)
2368 && r_type != R_ARM_PC24
5b5bb741
PB
2369#ifndef OLD_ARM_ABI
2370 && r_type != R_ARM_CALL
2371 && r_type != R_ARM_JUMP24
ee06dc07 2372 && r_type != R_ARM_PREL31
5b5bb741 2373#endif
7359ea65 2374 && r_type != R_ARM_PLT32)
252b5132 2375 {
947216bf
AM
2376 Elf_Internal_Rela outrel;
2377 bfd_byte *loc;
b34976b6 2378 bfd_boolean skip, relocate;
f21f3fe0 2379
0945cdfd
DJ
2380 *unresolved_reloc_p = FALSE;
2381
252b5132
RH
2382 if (sreloc == NULL)
2383 {
2384 const char * name;
f21f3fe0 2385
252b5132
RH
2386 name = (bfd_elf_string_from_elf_section
2387 (input_bfd,
2388 elf_elfheader (input_bfd)->e_shstrndx,
2389 elf_section_data (input_section)->rel_hdr.sh_name));
2390 if (name == NULL)
2391 return bfd_reloc_notsupported;
f21f3fe0 2392
252b5132
RH
2393 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
2394 && strcmp (bfd_get_section_name (input_bfd,
2395 input_section),
2396 name + 4) == 0);
f21f3fe0 2397
252b5132
RH
2398 sreloc = bfd_get_section_by_name (dynobj, name);
2399 BFD_ASSERT (sreloc != NULL);
2400 }
f21f3fe0 2401
b34976b6
AM
2402 skip = FALSE;
2403 relocate = FALSE;
f21f3fe0 2404
c629eae0
JJ
2405 outrel.r_offset =
2406 _bfd_elf_section_offset (output_bfd, info, input_section,
2407 rel->r_offset);
2408 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2409 skip = TRUE;
0bb2d96a 2410 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2411 skip = TRUE, relocate = TRUE;
252b5132
RH
2412 outrel.r_offset += (input_section->output_section->vma
2413 + input_section->output_offset);
f21f3fe0 2414
252b5132 2415 if (skip)
0bb2d96a 2416 memset (&outrel, 0, sizeof outrel);
5e681ec4
PB
2417 else if (h != NULL
2418 && h->dynindx != -1
7359ea65 2419 && (!info->shared
5e681ec4 2420 || !info->symbolic
f5385ebf 2421 || !h->def_regular))
5e681ec4 2422 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
252b5132
RH
2423 else
2424 {
a16385dc
MM
2425 int symbol;
2426
5e681ec4
PB
2427 /* This symbol is local, or marked to become local. */
2428 relocate = TRUE;
b7693d02
DJ
2429 if (sym_flags == STT_ARM_TFUNC)
2430 value |= 1;
a16385dc 2431 if (globals->symbian_p)
6366ff1e
MM
2432 {
2433 /* On Symbian OS, the data segment and text segement
2434 can be relocated independently. Therefore, we
2435 must indicate the segment to which this
2436 relocation is relative. The BPABI allows us to
2437 use any symbol in the right segment; we just use
2438 the section symbol as it is convenient. (We
2439 cannot use the symbol given by "h" directly as it
2440 will not appear in the dynamic symbol table.) */
2441 symbol = elf_section_data (sym_sec->output_section)->dynindx;
2442 BFD_ASSERT (symbol != 0);
2443 }
a16385dc
MM
2444 else
2445 /* On SVR4-ish systems, the dynamic loader cannot
2446 relocate the text and data segments independently,
2447 so the symbol does not matter. */
2448 symbol = 0;
2449 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
252b5132 2450 }
f21f3fe0 2451
947216bf
AM
2452 loc = sreloc->contents;
2453 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2454 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
9a5aca8c 2455
f21f3fe0 2456 /* If this reloc is against an external symbol, we do not want to
252b5132 2457 fiddle with the addend. Otherwise, we need to include the symbol
9b485d32 2458 value so that it becomes an addend for the dynamic reloc. */
252b5132
RH
2459 if (! relocate)
2460 return bfd_reloc_ok;
9a5aca8c 2461
f21f3fe0 2462 return _bfd_final_link_relocate (howto, input_bfd, input_section,
252b5132
RH
2463 contents, rel->r_offset, value,
2464 (bfd_vma) 0);
2465 }
2466 else switch (r_type)
2467 {
dfc5f959
NC
2468#ifndef OLD_ARM_ABI
2469 case R_ARM_XPC25: /* Arm BLX instruction. */
5b5bb741
PB
2470 case R_ARM_CALL:
2471 case R_ARM_JUMP24:
dfc5f959
NC
2472#endif
2473 case R_ARM_PC24: /* Arm B/BL instruction */
7359ea65 2474 case R_ARM_PLT32:
dfc5f959
NC
2475#ifndef OLD_ARM_ABI
2476 if (r_type == R_ARM_XPC25)
252b5132 2477 {
dfc5f959
NC
2478 /* Check for Arm calling Arm function. */
2479 /* FIXME: Should we translate the instruction into a BL
2480 instruction instead ? */
2481 if (sym_flags != STT_ARM_TFUNC)
d003868e
AM
2482 (*_bfd_error_handler)
2483 (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
2484 input_bfd,
2485 h ? h->root.root.string : "(local)");
dfc5f959
NC
2486 }
2487 else
2488#endif
2489 {
2490 /* Check for Arm calling Thumb function. */
2491 if (sym_flags == STT_ARM_TFUNC)
2492 {
57e8b36a
NC
2493 elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
2494 output_bfd, input_section,
2495 hit_data, sym_sec, rel->r_offset,
dfc5f959
NC
2496 signed_addend, value);
2497 return bfd_reloc_ok;
2498 }
252b5132 2499 }
ba96a88f 2500
dea514f5
PB
2501 /* The ARM ELF ABI says that this reloc is computed as: S - P + A
2502 where:
2503 S is the address of the symbol in the relocation.
2504 P is address of the instruction being relocated.
2505 A is the addend (extracted from the instruction) in bytes.
2506
2507 S is held in 'value'.
2508 P is the base address of the section containing the
2509 instruction plus the offset of the reloc into that
2510 section, ie:
2511 (input_section->output_section->vma +
2512 input_section->output_offset +
2513 rel->r_offset).
2514 A is the addend, converted into bytes, ie:
2515 (signed_addend * 4)
2516
2517 Note: None of these operations have knowledge of the pipeline
2518 size of the processor, thus it is up to the assembler to
2519 encode this information into the addend. */
2520 value -= (input_section->output_section->vma
2521 + input_section->output_offset);
2522 value -= rel->r_offset;
4e7fd91e
PB
2523 if (globals->use_rel)
2524 value += (signed_addend << howto->size);
2525 else
2526 /* RELA addends do not have to be adjusted by howto->size. */
2527 value += signed_addend;
23080146 2528
dcb5e6e6
NC
2529 signed_addend = value;
2530 signed_addend >>= howto->rightshift;
9a5aca8c 2531
59f2c4e7
NC
2532 /* It is not an error for an undefined weak reference to be
2533 out of range. Any program that branches to such a symbol
9a5aca8c
AM
2534 is going to crash anyway, so there is no point worrying
2535 about getting the destination exactly right. */
59f2c4e7
NC
2536 if (! h || h->root.type != bfd_link_hash_undefweak)
2537 {
9b485d32 2538 /* Perform a signed range check. */
dcb5e6e6 2539 if ( signed_addend > ((bfd_signed_vma) (howto->dst_mask >> 1))
59f2c4e7
NC
2540 || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
2541 return bfd_reloc_overflow;
2542 }
9a5aca8c 2543
dcb5e6e6
NC
2544#ifndef OLD_ARM_ABI
2545 /* If necessary set the H bit in the BLX instruction. */
2546 if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
2547 value = (signed_addend & howto->dst_mask)
2548 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
2549 | (1 << 24);
2550 else
2551#endif
2552 value = (signed_addend & howto->dst_mask)
2553 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
252b5132 2554 break;
f21f3fe0 2555
252b5132
RH
2556 case R_ARM_ABS32:
2557 value += addend;
2558 if (sym_flags == STT_ARM_TFUNC)
2559 value |= 1;
2560 break;
f21f3fe0 2561
252b5132
RH
2562 case R_ARM_REL32:
2563 value -= (input_section->output_section->vma
62efb346 2564 + input_section->output_offset + rel->r_offset);
252b5132
RH
2565 value += addend;
2566 break;
eb043451
PB
2567
2568#ifndef OLD_ARM_ABI
2569 case R_ARM_PREL31:
2570 value -= (input_section->output_section->vma
2571 + input_section->output_offset + rel->r_offset);
2572 value += signed_addend;
2573 if (! h || h->root.type != bfd_link_hash_undefweak)
2574 {
2575 /* Check for overflow */
2576 if ((value ^ (value >> 1)) & (1 << 30))
2577 return bfd_reloc_overflow;
2578 }
2579 value &= 0x7fffffff;
2580 value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
2581 if (sym_flags == STT_ARM_TFUNC)
2582 value |= 1;
2583 break;
2584#endif
252b5132 2585 }
f21f3fe0 2586
252b5132
RH
2587 bfd_put_32 (input_bfd, value, hit_data);
2588 return bfd_reloc_ok;
2589
2590 case R_ARM_ABS8:
2591 value += addend;
2592 if ((long) value > 0x7f || (long) value < -0x80)
2593 return bfd_reloc_overflow;
2594
2595 bfd_put_8 (input_bfd, value, hit_data);
2596 return bfd_reloc_ok;
2597
2598 case R_ARM_ABS16:
2599 value += addend;
2600
2601 if ((long) value > 0x7fff || (long) value < -0x8000)
2602 return bfd_reloc_overflow;
2603
2604 bfd_put_16 (input_bfd, value, hit_data);
2605 return bfd_reloc_ok;
2606
2607 case R_ARM_ABS12:
2608 /* Support ldr and str instruction for the arm */
2609 /* Also thumb b (unconditional branch). ??? Really? */
2610 value += addend;
2611
2612 if ((long) value > 0x7ff || (long) value < -0x800)
2613 return bfd_reloc_overflow;
2614
2615 value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
2616 bfd_put_32 (input_bfd, value, hit_data);
2617 return bfd_reloc_ok;
2618
2619 case R_ARM_THM_ABS5:
9b485d32 2620 /* Support ldr and str instructions for the thumb. */
4e7fd91e
PB
2621 if (globals->use_rel)
2622 {
2623 /* Need to refetch addend. */
2624 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
2625 /* ??? Need to determine shift amount from operand size. */
2626 addend >>= howto->rightshift;
2627 }
252b5132
RH
2628 value += addend;
2629
2630 /* ??? Isn't value unsigned? */
2631 if ((long) value > 0x1f || (long) value < -0x10)
2632 return bfd_reloc_overflow;
2633
2634 /* ??? Value needs to be properly shifted into place first. */
2635 value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
2636 bfd_put_16 (input_bfd, value, hit_data);
2637 return bfd_reloc_ok;
2638
dfc5f959
NC
2639#ifndef OLD_ARM_ABI
2640 case R_ARM_THM_XPC22:
2641#endif
252b5132 2642 case R_ARM_THM_PC22:
dfc5f959 2643 /* Thumb BL (branch long instruction). */
252b5132 2644 {
b34976b6
AM
2645 bfd_vma relocation;
2646 bfd_boolean overflow = FALSE;
2647 bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
2648 bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
df212a7e 2649 bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
ba96a88f 2650 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
b34976b6 2651 bfd_vma check;
252b5132 2652 bfd_signed_vma signed_check;
252b5132 2653
252b5132
RH
2654 /* Need to refetch the addend and squish the two 11 bit pieces
2655 together. */
4e7fd91e
PB
2656 if (globals->use_rel)
2657 {
2658 bfd_vma upper = upper_insn & 0x7ff;
2659 bfd_vma lower = lower_insn & 0x7ff;
2660 upper = (upper ^ 0x400) - 0x400; /* Sign extend. */
2661 addend = (upper << 12) | (lower << 1);
2662 signed_addend = addend;
2663 }
dfc5f959
NC
2664#ifndef OLD_ARM_ABI
2665 if (r_type == R_ARM_THM_XPC22)
2666 {
2667 /* Check for Thumb to Thumb call. */
2668 /* FIXME: Should we translate the instruction into a BL
2669 instruction instead ? */
2670 if (sym_flags == STT_ARM_TFUNC)
d003868e
AM
2671 (*_bfd_error_handler)
2672 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
2673 input_bfd,
2674 h ? h->root.root.string : "(local)");
dfc5f959
NC
2675 }
2676 else
2677#endif
252b5132 2678 {
dfc5f959
NC
2679 /* If it is not a call to Thumb, assume call to Arm.
2680 If it is a call relative to a section name, then it is not a
b7693d02
DJ
2681 function call at all, but rather a long jump. Calls through
2682 the PLT do not require stubs. */
2683 if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
2684 && (h == NULL || splt == NULL
2685 || h->plt.offset == (bfd_vma) -1))
dfc5f959
NC
2686 {
2687 if (elf32_thumb_to_arm_stub
2688 (info, sym_name, input_bfd, output_bfd, input_section,
2689 hit_data, sym_sec, rel->r_offset, signed_addend, value))
2690 return bfd_reloc_ok;
2691 else
2692 return bfd_reloc_dangerous;
2693 }
252b5132 2694 }
f21f3fe0 2695
b7693d02
DJ
2696 /* Handle calls via the PLT. */
2697 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
2698 {
2699 value = (splt->output_section->vma
2700 + splt->output_offset
2701 + h->plt.offset);
2702 /* Target the Thumb stub before the ARM PLT entry. */
2703 value -= 4;
0945cdfd 2704 *unresolved_reloc_p = FALSE;
b7693d02
DJ
2705 }
2706
ba96a88f 2707 relocation = value + signed_addend;
f21f3fe0 2708
252b5132 2709 relocation -= (input_section->output_section->vma
ba96a88f
NC
2710 + input_section->output_offset
2711 + rel->r_offset);
9a5aca8c 2712
252b5132
RH
2713 check = relocation >> howto->rightshift;
2714
2715 /* If this is a signed value, the rightshift just dropped
2716 leading 1 bits (assuming twos complement). */
2717 if ((bfd_signed_vma) relocation >= 0)
2718 signed_check = check;
2719 else
2720 signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
2721
252b5132 2722 /* Assumes two's complement. */
ba96a88f 2723 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
b34976b6 2724 overflow = TRUE;
252b5132 2725
df425bc0 2726#ifndef OLD_ARM_ABI
4f3c3dbb
NC
2727 if (r_type == R_ARM_THM_XPC22
2728 && ((lower_insn & 0x1800) == 0x0800))
c62e1cc3
NC
2729 /* For a BLX instruction, make sure that the relocation is rounded up
2730 to a word boundary. This follows the semantics of the instruction
2731 which specifies that bit 1 of the target address will come from bit
2732 1 of the base address. */
2733 relocation = (relocation + 2) & ~ 3;
99e4ae17 2734#endif
c62e1cc3
NC
2735 /* Put RELOCATION back into the insn. */
2736 upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
2737 lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
2738
252b5132
RH
2739 /* Put the relocated value back in the object file: */
2740 bfd_put_16 (input_bfd, upper_insn, hit_data);
2741 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
2742
2743 return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
2744 }
2745 break;
2746
51c5503b 2747 case R_ARM_THM_PC11:
e37c699c 2748 case R_ARM_THM_PC9:
51c5503b
NC
2749 /* Thumb B (branch) instruction). */
2750 {
6cf9e9fe 2751 bfd_signed_vma relocation;
51c5503b
NC
2752 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
2753 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
51c5503b
NC
2754 bfd_signed_vma signed_check;
2755
4e7fd91e 2756 if (globals->use_rel)
6cf9e9fe 2757 {
4e7fd91e
PB
2758 /* Need to refetch addend. */
2759 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
2760 if (addend & ((howto->src_mask + 1) >> 1))
2761 {
2762 signed_addend = -1;
2763 signed_addend &= ~ howto->src_mask;
2764 signed_addend |= addend;
2765 }
2766 else
2767 signed_addend = addend;
2768 /* The value in the insn has been right shifted. We need to
2769 undo this, so that we can perform the address calculation
2770 in terms of bytes. */
2771 signed_addend <<= howto->rightshift;
6cf9e9fe 2772 }
6cf9e9fe 2773 relocation = value + signed_addend;
51c5503b
NC
2774
2775 relocation -= (input_section->output_section->vma
2776 + input_section->output_offset
2777 + rel->r_offset);
2778
6cf9e9fe
NC
2779 relocation >>= howto->rightshift;
2780 signed_check = relocation;
2781 relocation &= howto->dst_mask;
51c5503b 2782 relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
cedb70c5 2783
51c5503b
NC
2784 bfd_put_16 (input_bfd, relocation, hit_data);
2785
2786 /* Assumes two's complement. */
2787 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
2788 return bfd_reloc_overflow;
2789
2790 return bfd_reloc_ok;
2791 }
cedb70c5 2792
1f433d93 2793#ifndef OLD_ARM_ABI
8375c36b
PB
2794 case R_ARM_ALU_PCREL7_0:
2795 case R_ARM_ALU_PCREL15_8:
2796 case R_ARM_ALU_PCREL23_15:
2797 {
2798 bfd_vma insn;
2799 bfd_vma relocation;
2800
2801 insn = bfd_get_32 (input_bfd, hit_data);
4e7fd91e
PB
2802 if (globals->use_rel)
2803 {
2804 /* Extract the addend. */
2805 addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
2806 signed_addend = addend;
2807 }
8375c36b
PB
2808 relocation = value + signed_addend;
2809
2810 relocation -= (input_section->output_section->vma
2811 + input_section->output_offset
2812 + rel->r_offset);
2813 insn = (insn & ~0xfff)
2814 | ((howto->bitpos << 7) & 0xf00)
2815 | ((relocation >> howto->bitpos) & 0xff);
2816 bfd_put_32 (input_bfd, value, hit_data);
2817 }
2818 return bfd_reloc_ok;
1f433d93 2819#endif
8375c36b 2820
252b5132
RH
2821 case R_ARM_GNU_VTINHERIT:
2822 case R_ARM_GNU_VTENTRY:
2823 return bfd_reloc_ok;
2824
2825 case R_ARM_COPY:
2826 return bfd_reloc_notsupported;
2827
2828 case R_ARM_GLOB_DAT:
2829 return bfd_reloc_notsupported;
2830
2831 case R_ARM_JUMP_SLOT:
2832 return bfd_reloc_notsupported;
2833
2834 case R_ARM_RELATIVE:
2835 return bfd_reloc_notsupported;
2836
2837 case R_ARM_GOTOFF:
2838 /* Relocation is relative to the start of the
2839 global offset table. */
2840
2841 BFD_ASSERT (sgot != NULL);
2842 if (sgot == NULL)
2843 return bfd_reloc_notsupported;
9a5aca8c 2844
cedb70c5 2845 /* If we are addressing a Thumb function, we need to adjust the
ee29b9fb
RE
2846 address by one, so that attempts to call the function pointer will
2847 correctly interpret it as Thumb code. */
2848 if (sym_flags == STT_ARM_TFUNC)
2849 value += 1;
2850
252b5132
RH
2851 /* Note that sgot->output_offset is not involved in this
2852 calculation. We always want the start of .got. If we
2853 define _GLOBAL_OFFSET_TABLE in a different way, as is
2854 permitted by the ABI, we might have to change this
9b485d32 2855 calculation. */
252b5132 2856 value -= sgot->output_section->vma;
f21f3fe0 2857 return _bfd_final_link_relocate (howto, input_bfd, input_section,
99e4ae17
AJ
2858 contents, rel->r_offset, value,
2859 (bfd_vma) 0);
252b5132
RH
2860
2861 case R_ARM_GOTPC:
a7c10850 2862 /* Use global offset table as symbol value. */
252b5132 2863 BFD_ASSERT (sgot != NULL);
f21f3fe0 2864
252b5132
RH
2865 if (sgot == NULL)
2866 return bfd_reloc_notsupported;
2867
0945cdfd 2868 *unresolved_reloc_p = FALSE;
252b5132 2869 value = sgot->output_section->vma;
f21f3fe0 2870 return _bfd_final_link_relocate (howto, input_bfd, input_section,
99e4ae17
AJ
2871 contents, rel->r_offset, value,
2872 (bfd_vma) 0);
f21f3fe0 2873
252b5132 2874 case R_ARM_GOT32:
eb043451
PB
2875#ifndef OLD_ARM_ABI
2876 case R_ARM_GOT_PREL:
2877#endif
252b5132 2878 /* Relocation is to the entry for this symbol in the
9b485d32 2879 global offset table. */
252b5132
RH
2880 if (sgot == NULL)
2881 return bfd_reloc_notsupported;
f21f3fe0 2882
252b5132
RH
2883 if (h != NULL)
2884 {
2885 bfd_vma off;
5e681ec4 2886 bfd_boolean dyn;
f21f3fe0 2887
252b5132
RH
2888 off = h->got.offset;
2889 BFD_ASSERT (off != (bfd_vma) -1);
5e681ec4 2890 dyn = globals->root.dynamic_sections_created;
f21f3fe0 2891
5e681ec4 2892 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
50d6c878 2893 || (info->shared
5e681ec4
PB
2894 && SYMBOL_REFERENCES_LOCAL (info, h))
2895 || (ELF_ST_VISIBILITY (h->other)
2896 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
2897 {
2898 /* This is actually a static link, or it is a -Bsymbolic link
2899 and the symbol is defined locally. We must initialize this
2900 entry in the global offset table. Since the offset must
2901 always be a multiple of 4, we use the least significant bit
2902 to record whether we have initialized it already.
f21f3fe0 2903
252b5132 2904 When doing a dynamic link, we create a .rel.got relocation
f21f3fe0 2905 entry to initialize the value. This is done in the
9b485d32 2906 finish_dynamic_symbol routine. */
252b5132
RH
2907 if ((off & 1) != 0)
2908 off &= ~1;
2909 else
2910 {
ee29b9fb
RE
2911 /* If we are addressing a Thumb function, we need to
2912 adjust the address by one, so that attempts to
2913 call the function pointer will correctly
2914 interpret it as Thumb code. */
2915 if (sym_flags == STT_ARM_TFUNC)
2916 value |= 1;
2917
252b5132
RH
2918 bfd_put_32 (output_bfd, value, sgot->contents + off);
2919 h->got.offset |= 1;
2920 }
2921 }
0945cdfd
DJ
2922 else
2923 *unresolved_reloc_p = FALSE;
f21f3fe0 2924
252b5132
RH
2925 value = sgot->output_offset + off;
2926 }
2927 else
2928 {
2929 bfd_vma off;
f21f3fe0 2930
252b5132
RH
2931 BFD_ASSERT (local_got_offsets != NULL &&
2932 local_got_offsets[r_symndx] != (bfd_vma) -1);
f21f3fe0 2933
252b5132 2934 off = local_got_offsets[r_symndx];
f21f3fe0 2935
252b5132
RH
2936 /* The offset must always be a multiple of 4. We use the
2937 least significant bit to record whether we have already
9b485d32 2938 generated the necessary reloc. */
252b5132
RH
2939 if ((off & 1) != 0)
2940 off &= ~1;
2941 else
2942 {
b7693d02
DJ
2943 /* If we are addressing a Thumb function, we need to
2944 adjust the address by one, so that attempts to
2945 call the function pointer will correctly
2946 interpret it as Thumb code. */
2947 if (sym_flags == STT_ARM_TFUNC)
2948 value |= 1;
2949
252b5132 2950 bfd_put_32 (output_bfd, value, sgot->contents + off);
f21f3fe0 2951
252b5132
RH
2952 if (info->shared)
2953 {
2954 asection * srelgot;
947216bf
AM
2955 Elf_Internal_Rela outrel;
2956 bfd_byte *loc;
f21f3fe0 2957
252b5132
RH
2958 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
2959 BFD_ASSERT (srelgot != NULL);
f21f3fe0 2960
252b5132 2961 outrel.r_offset = (sgot->output_section->vma
f21f3fe0 2962 + sgot->output_offset
252b5132
RH
2963 + off);
2964 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
947216bf
AM
2965 loc = srelgot->contents;
2966 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
2967 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
252b5132 2968 }
f21f3fe0 2969
252b5132
RH
2970 local_got_offsets[r_symndx] |= 1;
2971 }
f21f3fe0 2972
252b5132
RH
2973 value = sgot->output_offset + off;
2974 }
eb043451
PB
2975 if (r_type != R_ARM_GOT32)
2976 value += sgot->output_section->vma;
9a5aca8c 2977
f21f3fe0 2978 return _bfd_final_link_relocate (howto, input_bfd, input_section,
99e4ae17
AJ
2979 contents, rel->r_offset, value,
2980 (bfd_vma) 0);
f21f3fe0 2981
252b5132
RH
2982 case R_ARM_SBREL32:
2983 return bfd_reloc_notsupported;
2984
2985 case R_ARM_AMP_VCALL9:
2986 return bfd_reloc_notsupported;
2987
2988 case R_ARM_RSBREL32:
2989 return bfd_reloc_notsupported;
2990
2991 case R_ARM_THM_RPC22:
2992 return bfd_reloc_notsupported;
2993
2994 case R_ARM_RREL32:
2995 return bfd_reloc_notsupported;
2996
2997 case R_ARM_RABS32:
2998 return bfd_reloc_notsupported;
2999
3000 case R_ARM_RPC24:
3001 return bfd_reloc_notsupported;
3002
3003 case R_ARM_RBASE:
3004 return bfd_reloc_notsupported;
3005
319850b4
JB
3006 case R_ARM_V4BX:
3007 if (globals->fix_v4bx)
3008 {
3009 bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
3010
3011 /* Ensure that we have a BX instruction. */
3012 BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
3013
3014 /* Preserve Rm (lowest four bits) and the condition code
3015 (highest four bits). Other bits encode MOV PC,Rm. */
3016 insn = (insn & 0xf000000f) | 0x01a0f000;
3017
3018 bfd_put_32 (input_bfd, insn, hit_data);
3019 }
3020 return bfd_reloc_ok;
3021
252b5132
RH
3022 default:
3023 return bfd_reloc_notsupported;
3024 }
3025}
3026
98c1d4aa
NC
3027/* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. */
3028static void
57e8b36a
NC
3029arm_add_to_rel (bfd * abfd,
3030 bfd_byte * address,
3031 reloc_howto_type * howto,
3032 bfd_signed_vma increment)
98c1d4aa 3033{
98c1d4aa
NC
3034 bfd_signed_vma addend;
3035
9a5aca8c 3036 if (howto->type == R_ARM_THM_PC22)
98c1d4aa 3037 {
9a5aca8c
AM
3038 int upper_insn, lower_insn;
3039 int upper, lower;
98c1d4aa 3040
9a5aca8c
AM
3041 upper_insn = bfd_get_16 (abfd, address);
3042 lower_insn = bfd_get_16 (abfd, address + 2);
3043 upper = upper_insn & 0x7ff;
3044 lower = lower_insn & 0x7ff;
3045
3046 addend = (upper << 12) | (lower << 1);
ddda4409 3047 addend += increment;
9a5aca8c 3048 addend >>= 1;
98c1d4aa 3049
9a5aca8c
AM
3050 upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
3051 lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
3052
dc810e39
AM
3053 bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
3054 bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
9a5aca8c
AM
3055 }
3056 else
3057 {
3058 bfd_vma contents;
3059
3060 contents = bfd_get_32 (abfd, address);
3061
3062 /* Get the (signed) value from the instruction. */
3063 addend = contents & howto->src_mask;
3064 if (addend & ((howto->src_mask + 1) >> 1))
3065 {
3066 bfd_signed_vma mask;
3067
3068 mask = -1;
3069 mask &= ~ howto->src_mask;
3070 addend |= mask;
3071 }
3072
3073 /* Add in the increment, (which is a byte value). */
3074 switch (howto->type)
3075 {
3076 default:
3077 addend += increment;
3078 break;
3079
3080 case R_ARM_PC24:
5b5bb741
PB
3081#ifndef OLD_ARM_ABI
3082 case R_ARM_CALL:
3083 case R_ARM_JUMP24:
3084#endif
9a5aca8c 3085 addend <<= howto->size;
dc810e39 3086 addend += increment;
9a5aca8c
AM
3087
3088 /* Should we check for overflow here ? */
3089
3090 /* Drop any undesired bits. */
3091 addend >>= howto->rightshift;
3092 break;
3093 }
3094
3095 contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
3096
3097 bfd_put_32 (abfd, contents, address);
ddda4409 3098 }
98c1d4aa 3099}
252b5132
RH
3100
3101/* Relocate an ARM ELF section. */
b34976b6 3102static bfd_boolean
57e8b36a
NC
3103elf32_arm_relocate_section (bfd * output_bfd,
3104 struct bfd_link_info * info,
3105 bfd * input_bfd,
3106 asection * input_section,
3107 bfd_byte * contents,
3108 Elf_Internal_Rela * relocs,
3109 Elf_Internal_Sym * local_syms,
3110 asection ** local_sections)
252b5132 3111{
b34976b6
AM
3112 Elf_Internal_Shdr *symtab_hdr;
3113 struct elf_link_hash_entry **sym_hashes;
3114 Elf_Internal_Rela *rel;
3115 Elf_Internal_Rela *relend;
3116 const char *name;
b32d3aa2 3117 struct elf32_arm_link_hash_table * globals;
252b5132 3118
4e7fd91e
PB
3119 globals = elf32_arm_hash_table (info);
3120 if (info->relocatable && !globals->use_rel)
b34976b6 3121 return TRUE;
b491616a 3122
252b5132
RH
3123 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
3124 sym_hashes = elf_sym_hashes (input_bfd);
3125
3126 rel = relocs;
3127 relend = relocs + input_section->reloc_count;
3128 for (; rel < relend; rel++)
3129 {
ba96a88f
NC
3130 int r_type;
3131 reloc_howto_type * howto;
3132 unsigned long r_symndx;
3133 Elf_Internal_Sym * sym;
3134 asection * sec;
252b5132 3135 struct elf_link_hash_entry * h;
ba96a88f
NC
3136 bfd_vma relocation;
3137 bfd_reloc_status_type r;
3138 arelent bfd_reloc;
0945cdfd 3139 bfd_boolean unresolved_reloc = FALSE;
f21f3fe0 3140
252b5132 3141 r_symndx = ELF32_R_SYM (rel->r_info);
ba96a88f 3142 r_type = ELF32_R_TYPE (rel->r_info);
b32d3aa2 3143 r_type = arm_real_reloc_type (globals, r_type);
252b5132 3144
ba96a88f
NC
3145 if ( r_type == R_ARM_GNU_VTENTRY
3146 || r_type == R_ARM_GNU_VTINHERIT)
252b5132
RH
3147 continue;
3148
b32d3aa2 3149 bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
ba96a88f 3150 howto = bfd_reloc.howto;
252b5132 3151
4e7fd91e 3152 if (info->relocatable && globals->use_rel)
252b5132 3153 {
1049f94e 3154 /* This is a relocatable link. We don't have to change
252b5132
RH
3155 anything, unless the reloc is against a section symbol,
3156 in which case we have to adjust according to where the
3157 section symbol winds up in the output section. */
3158 if (r_symndx < symtab_hdr->sh_info)
3159 {
3160 sym = local_syms + r_symndx;
3161 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3162 {
3163 sec = local_sections[r_symndx];
98c1d4aa 3164 arm_add_to_rel (input_bfd, contents + rel->r_offset,
dc810e39
AM
3165 howto,
3166 (bfd_signed_vma) (sec->output_offset
3167 + sym->st_value));
252b5132
RH
3168 }
3169 }
3170
3171 continue;
3172 }
3173
3174 /* This is a final link. */
3175 h = NULL;
3176 sym = NULL;
3177 sec = NULL;
9b485d32 3178
252b5132
RH
3179 if (r_symndx < symtab_hdr->sh_info)
3180 {
3181 sym = local_syms + r_symndx;
3182 sec = local_sections[r_symndx];
4e7fd91e 3183 if (globals->use_rel)
f8df10f4 3184 {
4e7fd91e
PB
3185 relocation = (sec->output_section->vma
3186 + sec->output_offset
3187 + sym->st_value);
3188 if ((sec->flags & SEC_MERGE)
3189 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
f8df10f4 3190 {
4e7fd91e
PB
3191 asection *msec;
3192 bfd_vma addend, value;
3193
3194 if (howto->rightshift)
3195 {
3196 (*_bfd_error_handler)
3197 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3198 input_bfd, input_section,
3199 (long) rel->r_offset, howto->name);
3200 return FALSE;
3201 }
f8df10f4 3202
4e7fd91e 3203 value = bfd_get_32 (input_bfd, contents + rel->r_offset);
f8df10f4 3204
4e7fd91e
PB
3205 /* Get the (signed) value from the instruction. */
3206 addend = value & howto->src_mask;
3207 if (addend & ((howto->src_mask + 1) >> 1))
3208 {
3209 bfd_signed_vma mask;
f8df10f4 3210
4e7fd91e
PB
3211 mask = -1;
3212 mask &= ~ howto->src_mask;
3213 addend |= mask;
3214 }
3215 msec = sec;
3216 addend =
3217 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3218 - relocation;
3219 addend += msec->output_section->vma + msec->output_offset;
3220 value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
3221 bfd_put_32 (input_bfd, value, contents + rel->r_offset);
f8df10f4 3222 }
f8df10f4 3223 }
4e7fd91e
PB
3224 else
3225 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
3226 }
3227 else
3228 {
560e09e9 3229 bfd_boolean warned;
560e09e9 3230
b2a8e766
AM
3231 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3232 r_symndx, symtab_hdr, sym_hashes,
3233 h, sec, relocation,
3234 unresolved_reloc, warned);
252b5132
RH
3235 }
3236
3237 if (h != NULL)
3238 name = h->root.root.string;
3239 else
3240 {
3241 name = (bfd_elf_string_from_elf_section
3242 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3243 if (name == NULL || *name == '\0')
3244 name = bfd_section_name (input_bfd, sec);
3245 }
f21f3fe0 3246
252b5132
RH
3247 r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
3248 input_section, contents, rel,
3249 relocation, info, sec, name,
3250 (h ? ELF_ST_TYPE (h->type) :
0945cdfd
DJ
3251 ELF_ST_TYPE (sym->st_info)), h,
3252 &unresolved_reloc);
3253
3254 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3255 because such sections are not SEC_ALLOC and thus ld.so will
3256 not process them. */
3257 if (unresolved_reloc
3258 && !((input_section->flags & SEC_DEBUGGING) != 0
3259 && h->def_dynamic))
3260 {
3261 (*_bfd_error_handler)
3262 (_("%B(%A+0x%lx): warning: unresolvable relocation %d against symbol `%s'"),
3263 input_bfd, input_section, (long) rel->r_offset,
3264 r_type, h->root.root.string);
3265 return FALSE;
3266 }
252b5132
RH
3267
3268 if (r != bfd_reloc_ok)
3269 {
3270 const char * msg = (const char *) 0;
3271
3272 switch (r)
3273 {
3274 case bfd_reloc_overflow:
cf919dfd
PB
3275 /* If the overflowing reloc was to an undefined symbol,
3276 we have already printed one error message and there
3277 is no point complaining again. */
3278 if ((! h ||
3279 h->root.type != bfd_link_hash_undefined)
3280 && (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
3281 (info, (h ? &h->root : NULL), name, howto->name,
3282 (bfd_vma) 0, input_bfd, input_section,
3283 rel->r_offset))))
b34976b6 3284 return FALSE;
252b5132
RH
3285 break;
3286
3287 case bfd_reloc_undefined:
3288 if (!((*info->callbacks->undefined_symbol)
3289 (info, name, input_bfd, input_section,
b34976b6
AM
3290 rel->r_offset, TRUE)))
3291 return FALSE;
252b5132
RH
3292 break;
3293
3294 case bfd_reloc_outofrange:
9b485d32 3295 msg = _("internal error: out of range error");
252b5132
RH
3296 goto common_error;
3297
3298 case bfd_reloc_notsupported:
9b485d32 3299 msg = _("internal error: unsupported relocation error");
252b5132
RH
3300 goto common_error;
3301
3302 case bfd_reloc_dangerous:
9b485d32 3303 msg = _("internal error: dangerous error");
252b5132
RH
3304 goto common_error;
3305
3306 default:
9b485d32 3307 msg = _("internal error: unknown error");
252b5132
RH
3308 /* fall through */
3309
3310 common_error:
3311 if (!((*info->callbacks->warning)
3312 (info, msg, name, input_bfd, input_section,
3313 rel->r_offset)))
b34976b6 3314 return FALSE;
252b5132
RH
3315 break;
3316 }
3317 }
3318 }
3319
b34976b6 3320 return TRUE;
252b5132
RH
3321}
3322
c178919b
NC
3323/* Set the right machine number. */
3324
3325static bfd_boolean
57e8b36a 3326elf32_arm_object_p (bfd *abfd)
c178919b 3327{
5a6c6817 3328 unsigned int mach;
57e8b36a 3329
5a6c6817 3330 mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
c178919b 3331
5a6c6817
NC
3332 if (mach != bfd_mach_arm_unknown)
3333 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
3334
3335 else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
3336 bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
e16bb312 3337
e16bb312 3338 else
5a6c6817 3339 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
c178919b
NC
3340
3341 return TRUE;
3342}
3343
fc830a83 3344/* Function to keep ARM specific flags in the ELF header. */
3c9458e9 3345
b34976b6 3346static bfd_boolean
57e8b36a 3347elf32_arm_set_private_flags (bfd *abfd, flagword flags)
252b5132
RH
3348{
3349 if (elf_flags_init (abfd)
3350 && elf_elfheader (abfd)->e_flags != flags)
3351 {
fc830a83
NC
3352 if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
3353 {
fd2ec330 3354 if (flags & EF_ARM_INTERWORK)
d003868e
AM
3355 (*_bfd_error_handler)
3356 (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
3357 abfd);
fc830a83 3358 else
d003868e
AM
3359 _bfd_error_handler
3360 (_("Warning: Clearing the interworking flag of %B due to outside request"),
3361 abfd);
fc830a83 3362 }
252b5132
RH
3363 }
3364 else
3365 {
3366 elf_elfheader (abfd)->e_flags = flags;
b34976b6 3367 elf_flags_init (abfd) = TRUE;
252b5132
RH
3368 }
3369
b34976b6 3370 return TRUE;
252b5132
RH
3371}
3372
fc830a83 3373/* Copy backend specific data from one object module to another. */
9b485d32 3374
b34976b6 3375static bfd_boolean
57e8b36a 3376elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
252b5132
RH
3377{
3378 flagword in_flags;
3379 flagword out_flags;
3380
fc830a83 3381 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
252b5132 3382 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 3383 return TRUE;
252b5132 3384
fc830a83 3385 in_flags = elf_elfheader (ibfd)->e_flags;
252b5132
RH
3386 out_flags = elf_elfheader (obfd)->e_flags;
3387
fc830a83
NC
3388 if (elf_flags_init (obfd)
3389 && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
3390 && in_flags != out_flags)
252b5132 3391 {
252b5132 3392 /* Cannot mix APCS26 and APCS32 code. */
fd2ec330 3393 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
b34976b6 3394 return FALSE;
252b5132
RH
3395
3396 /* Cannot mix float APCS and non-float APCS code. */
fd2ec330 3397 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
b34976b6 3398 return FALSE;
252b5132
RH
3399
3400 /* If the src and dest have different interworking flags
3401 then turn off the interworking bit. */
fd2ec330 3402 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
252b5132 3403 {
fd2ec330 3404 if (out_flags & EF_ARM_INTERWORK)
d003868e
AM
3405 _bfd_error_handler
3406 (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
3407 obfd, ibfd);
252b5132 3408
fd2ec330 3409 in_flags &= ~EF_ARM_INTERWORK;
252b5132 3410 }
1006ba19
PB
3411
3412 /* Likewise for PIC, though don't warn for this case. */
fd2ec330
PB
3413 if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
3414 in_flags &= ~EF_ARM_PIC;
252b5132
RH
3415 }
3416
3417 elf_elfheader (obfd)->e_flags = in_flags;
b34976b6 3418 elf_flags_init (obfd) = TRUE;
252b5132 3419
94a3258f
PB
3420 /* Also copy the EI_OSABI field. */
3421 elf_elfheader (obfd)->e_ident[EI_OSABI] =
3422 elf_elfheader (ibfd)->e_ident[EI_OSABI];
3423
b34976b6 3424 return TRUE;
252b5132
RH
3425}
3426
3427/* Merge backend specific data from an object file to the output
3428 object file when linking. */
9b485d32 3429
b34976b6 3430static bfd_boolean
57e8b36a 3431elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
252b5132
RH
3432{
3433 flagword out_flags;
3434 flagword in_flags;
b34976b6 3435 bfd_boolean flags_compatible = TRUE;
cf919dfd 3436 asection *sec;
252b5132 3437
9b485d32 3438 /* Check if we have the same endianess. */
82e51918 3439 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
b34976b6 3440 return FALSE;
1fe494a5 3441
252b5132
RH
3442 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3443 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 3444 return TRUE;
252b5132 3445
252b5132
RH
3446 /* The input BFD must have had its flags initialised. */
3447 /* The following seems bogus to me -- The flags are initialized in
3448 the assembler but I don't think an elf_flags_init field is
9b485d32 3449 written into the object. */
252b5132
RH
3450 /* BFD_ASSERT (elf_flags_init (ibfd)); */
3451
3452 in_flags = elf_elfheader (ibfd)->e_flags;
3453 out_flags = elf_elfheader (obfd)->e_flags;
3454
3455 if (!elf_flags_init (obfd))
3456 {
fe077fa6
NC
3457 /* If the input is the default architecture and had the default
3458 flags then do not bother setting the flags for the output
3459 architecture, instead allow future merges to do this. If no
3460 future merges ever set these flags then they will retain their
3461 uninitialised values, which surprise surprise, correspond
252b5132 3462 to the default values. */
fe077fa6
NC
3463 if (bfd_get_arch_info (ibfd)->the_default
3464 && elf_elfheader (ibfd)->e_flags == 0)
b34976b6 3465 return TRUE;
252b5132 3466
b34976b6 3467 elf_flags_init (obfd) = TRUE;
252b5132
RH
3468 elf_elfheader (obfd)->e_flags = in_flags;
3469
3470 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3471 && bfd_get_arch_info (obfd)->the_default)
3472 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3473
b34976b6 3474 return TRUE;
252b5132
RH
3475 }
3476
5a6c6817
NC
3477 /* Determine what should happen if the input ARM architecture
3478 does not match the output ARM architecture. */
3479 if (! bfd_arm_merge_machines (ibfd, obfd))
3480 return FALSE;
e16bb312 3481
1006ba19 3482 /* Identical flags must be compatible. */
252b5132 3483 if (in_flags == out_flags)
b34976b6 3484 return TRUE;
252b5132 3485
35a0f415
DJ
3486 /* Check to see if the input BFD actually contains any sections. If
3487 not, its flags may not have been initialised either, but it
3488 cannot actually cause any incompatibility. Do not short-circuit
3489 dynamic objects; their section list may be emptied by
d1f161ea 3490 elf_link_add_object_symbols.
35a0f415 3491
d1f161ea
NC
3492 Also check to see if there are no code sections in the input.
3493 In this case there is no need to check for code specific flags.
3494 XXX - do we need to worry about floating-point format compatability
3495 in data sections ? */
35a0f415 3496 if (!(ibfd->flags & DYNAMIC))
cf919dfd 3497 {
35a0f415 3498 bfd_boolean null_input_bfd = TRUE;
d1f161ea 3499 bfd_boolean only_data_sections = TRUE;
35a0f415
DJ
3500
3501 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
cf919dfd 3502 {
35a0f415
DJ
3503 /* Ignore synthetic glue sections. */
3504 if (strcmp (sec->name, ".glue_7")
3505 && strcmp (sec->name, ".glue_7t"))
3506 {
d1f161ea
NC
3507 if ((bfd_get_section_flags (ibfd, sec)
3508 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3509 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3510 only_data_sections = FALSE;
3511
35a0f415
DJ
3512 null_input_bfd = FALSE;
3513 break;
3514 }
cf919dfd 3515 }
d1f161ea
NC
3516
3517 if (null_input_bfd || only_data_sections)
35a0f415 3518 return TRUE;
cf919dfd 3519 }
cf919dfd 3520
252b5132 3521 /* Complain about various flag mismatches. */
fc830a83
NC
3522 if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
3523 {
d003868e 3524 _bfd_error_handler
3656d5e3 3525 (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
d003868e
AM
3526 ibfd, obfd,
3527 (in_flags & EF_ARM_EABIMASK) >> 24,
3528 (out_flags & EF_ARM_EABIMASK) >> 24);
b34976b6 3529 return FALSE;
fc830a83 3530 }
252b5132 3531
1006ba19
PB
3532 /* Not sure what needs to be checked for EABI versions >= 1. */
3533 if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
3534 {
fd2ec330 3535 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
1006ba19 3536 {
d003868e
AM
3537 _bfd_error_handler
3538 (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
3539 ibfd, obfd,
3540 in_flags & EF_ARM_APCS_26 ? 26 : 32,
3541 out_flags & EF_ARM_APCS_26 ? 26 : 32);
b34976b6 3542 flags_compatible = FALSE;
1006ba19 3543 }
252b5132 3544
fd2ec330 3545 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
1006ba19 3546 {
5eefb65f 3547 if (in_flags & EF_ARM_APCS_FLOAT)
d003868e
AM
3548 _bfd_error_handler
3549 (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
3550 ibfd, obfd);
5eefb65f 3551 else
d003868e
AM
3552 _bfd_error_handler
3553 (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
3554 ibfd, obfd);
63b0f745 3555
b34976b6 3556 flags_compatible = FALSE;
1006ba19 3557 }
252b5132 3558
96a846ea 3559 if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
1006ba19 3560 {
96a846ea 3561 if (in_flags & EF_ARM_VFP_FLOAT)
d003868e
AM
3562 _bfd_error_handler
3563 (_("ERROR: %B uses VFP instructions, whereas %B does not"),
3564 ibfd, obfd);
5eefb65f 3565 else
d003868e
AM
3566 _bfd_error_handler
3567 (_("ERROR: %B uses FPA instructions, whereas %B does not"),
3568 ibfd, obfd);
fde78edd
NC
3569
3570 flags_compatible = FALSE;
3571 }
3572
3573 if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
3574 {
3575 if (in_flags & EF_ARM_MAVERICK_FLOAT)
d003868e
AM
3576 _bfd_error_handler
3577 (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
3578 ibfd, obfd);
fde78edd 3579 else
d003868e
AM
3580 _bfd_error_handler
3581 (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
3582 ibfd, obfd);
63b0f745 3583
b34976b6 3584 flags_compatible = FALSE;
1006ba19 3585 }
96a846ea
RE
3586
3587#ifdef EF_ARM_SOFT_FLOAT
3588 if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
3589 {
3590 /* We can allow interworking between code that is VFP format
3591 layout, and uses either soft float or integer regs for
3592 passing floating point arguments and results. We already
3593 know that the APCS_FLOAT flags match; similarly for VFP
3594 flags. */
3595 if ((in_flags & EF_ARM_APCS_FLOAT) != 0
3596 || (in_flags & EF_ARM_VFP_FLOAT) == 0)
3597 {
3598 if (in_flags & EF_ARM_SOFT_FLOAT)
d003868e
AM
3599 _bfd_error_handler
3600 (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
3601 ibfd, obfd);
96a846ea 3602 else
d003868e
AM
3603 _bfd_error_handler
3604 (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
3605 ibfd, obfd);
96a846ea 3606
b34976b6 3607 flags_compatible = FALSE;
96a846ea
RE
3608 }
3609 }
ee43f35e 3610#endif
252b5132 3611
1006ba19 3612 /* Interworking mismatch is only a warning. */
fd2ec330 3613 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
8f615d07 3614 {
e3c8793a
NC
3615 if (in_flags & EF_ARM_INTERWORK)
3616 {
d003868e
AM
3617 _bfd_error_handler
3618 (_("Warning: %B supports interworking, whereas %B does not"),
3619 ibfd, obfd);
e3c8793a
NC
3620 }
3621 else
3622 {
d003868e
AM
3623 _bfd_error_handler
3624 (_("Warning: %B does not support interworking, whereas %B does"),
3625 ibfd, obfd);
e3c8793a 3626 }
8f615d07 3627 }
252b5132 3628 }
63b0f745 3629
1006ba19 3630 return flags_compatible;
252b5132
RH
3631}
3632
9b485d32
NC
3633/* Display the flags field. */
3634
b34976b6 3635static bfd_boolean
57e8b36a 3636elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
252b5132 3637{
fc830a83
NC
3638 FILE * file = (FILE *) ptr;
3639 unsigned long flags;
252b5132
RH
3640
3641 BFD_ASSERT (abfd != NULL && ptr != NULL);
3642
3643 /* Print normal ELF private data. */
3644 _bfd_elf_print_private_bfd_data (abfd, ptr);
3645
fc830a83 3646 flags = elf_elfheader (abfd)->e_flags;
9b485d32
NC
3647 /* Ignore init flag - it may not be set, despite the flags field
3648 containing valid data. */
252b5132
RH
3649
3650 /* xgettext:c-format */
9b485d32 3651 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
252b5132 3652
fc830a83
NC
3653 switch (EF_ARM_EABI_VERSION (flags))
3654 {
3655 case EF_ARM_EABI_UNKNOWN:
4cc11e76 3656 /* The following flag bits are GNU extensions and not part of the
fc830a83
NC
3657 official ARM ELF extended ABI. Hence they are only decoded if
3658 the EABI version is not set. */
fd2ec330 3659 if (flags & EF_ARM_INTERWORK)
9b485d32 3660 fprintf (file, _(" [interworking enabled]"));
9a5aca8c 3661
fd2ec330 3662 if (flags & EF_ARM_APCS_26)
6c571f00 3663 fprintf (file, " [APCS-26]");
fc830a83 3664 else
6c571f00 3665 fprintf (file, " [APCS-32]");
9a5aca8c 3666
96a846ea
RE
3667 if (flags & EF_ARM_VFP_FLOAT)
3668 fprintf (file, _(" [VFP float format]"));
fde78edd
NC
3669 else if (flags & EF_ARM_MAVERICK_FLOAT)
3670 fprintf (file, _(" [Maverick float format]"));
96a846ea
RE
3671 else
3672 fprintf (file, _(" [FPA float format]"));
3673
fd2ec330 3674 if (flags & EF_ARM_APCS_FLOAT)
9b485d32 3675 fprintf (file, _(" [floats passed in float registers]"));
9a5aca8c 3676
fd2ec330 3677 if (flags & EF_ARM_PIC)
9b485d32 3678 fprintf (file, _(" [position independent]"));
fc830a83 3679
fd2ec330 3680 if (flags & EF_ARM_NEW_ABI)
9b485d32 3681 fprintf (file, _(" [new ABI]"));
9a5aca8c 3682
fd2ec330 3683 if (flags & EF_ARM_OLD_ABI)
9b485d32 3684 fprintf (file, _(" [old ABI]"));
9a5aca8c 3685
fd2ec330 3686 if (flags & EF_ARM_SOFT_FLOAT)
9b485d32 3687 fprintf (file, _(" [software FP]"));
9a5aca8c 3688
96a846ea
RE
3689 flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
3690 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
fde78edd
NC
3691 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
3692 | EF_ARM_MAVERICK_FLOAT);
fc830a83 3693 break;
9a5aca8c 3694
fc830a83 3695 case EF_ARM_EABI_VER1:
9b485d32 3696 fprintf (file, _(" [Version1 EABI]"));
9a5aca8c 3697
fc830a83 3698 if (flags & EF_ARM_SYMSARESORTED)
9b485d32 3699 fprintf (file, _(" [sorted symbol table]"));
fc830a83 3700 else
9b485d32 3701 fprintf (file, _(" [unsorted symbol table]"));
9a5aca8c 3702
fc830a83
NC
3703 flags &= ~ EF_ARM_SYMSARESORTED;
3704 break;
9a5aca8c 3705
fd2ec330
PB
3706 case EF_ARM_EABI_VER2:
3707 fprintf (file, _(" [Version2 EABI]"));
3708
3709 if (flags & EF_ARM_SYMSARESORTED)
3710 fprintf (file, _(" [sorted symbol table]"));
3711 else
3712 fprintf (file, _(" [unsorted symbol table]"));
3713
3714 if (flags & EF_ARM_DYNSYMSUSESEGIDX)
3715 fprintf (file, _(" [dynamic symbols use segment index]"));
3716
3717 if (flags & EF_ARM_MAPSYMSFIRST)
3718 fprintf (file, _(" [mapping symbols precede others]"));
3719
99e4ae17 3720 flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
fd2ec330
PB
3721 | EF_ARM_MAPSYMSFIRST);
3722 break;
3723
d507cf36
PB
3724 case EF_ARM_EABI_VER3:
3725 fprintf (file, _(" [Version3 EABI]"));
8cb51566
PB
3726 break;
3727
3728 case EF_ARM_EABI_VER4:
3729 fprintf (file, _(" [Version4 EABI]"));
d507cf36
PB
3730
3731 if (flags & EF_ARM_BE8)
3732 fprintf (file, _(" [BE8]"));
3733
3734 if (flags & EF_ARM_LE8)
3735 fprintf (file, _(" [LE8]"));
3736
3737 flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
3738 break;
3739
fc830a83 3740 default:
9b485d32 3741 fprintf (file, _(" <EABI version unrecognised>"));
fc830a83
NC
3742 break;
3743 }
252b5132 3744
fc830a83 3745 flags &= ~ EF_ARM_EABIMASK;
252b5132 3746
fc830a83 3747 if (flags & EF_ARM_RELEXEC)
9b485d32 3748 fprintf (file, _(" [relocatable executable]"));
252b5132 3749
fc830a83 3750 if (flags & EF_ARM_HASENTRY)
9b485d32 3751 fprintf (file, _(" [has entry point]"));
252b5132 3752
fc830a83
NC
3753 flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
3754
3755 if (flags)
9b485d32 3756 fprintf (file, _("<Unrecognised flag bits set>"));
9a5aca8c 3757
252b5132
RH
3758 fputc ('\n', file);
3759
b34976b6 3760 return TRUE;
252b5132
RH
3761}
3762
3763static int
57e8b36a 3764elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
252b5132 3765{
2f0ca46a
NC
3766 switch (ELF_ST_TYPE (elf_sym->st_info))
3767 {
3768 case STT_ARM_TFUNC:
3769 return ELF_ST_TYPE (elf_sym->st_info);
ce855c42 3770
2f0ca46a
NC
3771 case STT_ARM_16BIT:
3772 /* If the symbol is not an object, return the STT_ARM_16BIT flag.
3773 This allows us to distinguish between data used by Thumb instructions
3774 and non-data (which is probably code) inside Thumb regions of an
3775 executable. */
3776 if (type != STT_OBJECT)
3777 return ELF_ST_TYPE (elf_sym->st_info);
3778 break;
9a5aca8c 3779
ce855c42
NC
3780 default:
3781 break;
2f0ca46a
NC
3782 }
3783
3784 return type;
252b5132 3785}
f21f3fe0 3786
252b5132 3787static asection *
57e8b36a
NC
3788elf32_arm_gc_mark_hook (asection * sec,
3789 struct bfd_link_info * info ATTRIBUTE_UNUSED,
3790 Elf_Internal_Rela * rel,
3791 struct elf_link_hash_entry * h,
3792 Elf_Internal_Sym * sym)
252b5132
RH
3793{
3794 if (h != NULL)
3795 {
3796 switch (ELF32_R_TYPE (rel->r_info))
3797 {
3798 case R_ARM_GNU_VTINHERIT:
3799 case R_ARM_GNU_VTENTRY:
3800 break;
3801
3802 default:
3803 switch (h->root.type)
3804 {
3805 case bfd_link_hash_defined:
3806 case bfd_link_hash_defweak:
3807 return h->root.u.def.section;
3808
3809 case bfd_link_hash_common:
3810 return h->root.u.c.p->section;
e049a0de
ILT
3811
3812 default:
3813 break;
252b5132
RH
3814 }
3815 }
3816 }
3817 else
1e2f5b6e 3818 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9ad5cbcf 3819
252b5132
RH
3820 return NULL;
3821}
3822
780a67af
NC
3823/* Update the got entry reference counts for the section being removed. */
3824
b34976b6 3825static bfd_boolean
57e8b36a
NC
3826elf32_arm_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
3827 struct bfd_link_info * info ATTRIBUTE_UNUSED,
3828 asection * sec ATTRIBUTE_UNUSED,
3829 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
252b5132 3830{
5e681ec4
PB
3831 Elf_Internal_Shdr *symtab_hdr;
3832 struct elf_link_hash_entry **sym_hashes;
3833 bfd_signed_vma *local_got_refcounts;
3834 const Elf_Internal_Rela *rel, *relend;
eb043451
PB
3835 struct elf32_arm_link_hash_table * globals;
3836
3837 globals = elf32_arm_hash_table (info);
5e681ec4
PB
3838
3839 elf_section_data (sec)->local_dynrel = NULL;
3840
3841 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3842 sym_hashes = elf_sym_hashes (abfd);
3843 local_got_refcounts = elf_local_got_refcounts (abfd);
3844
3845 relend = relocs + sec->reloc_count;
3846 for (rel = relocs; rel < relend; rel++)
eb043451 3847 {
3eb128b2
AM
3848 unsigned long r_symndx;
3849 struct elf_link_hash_entry *h = NULL;
eb043451 3850 int r_type;
5e681ec4 3851
3eb128b2
AM
3852 r_symndx = ELF32_R_SYM (rel->r_info);
3853 if (r_symndx >= symtab_hdr->sh_info)
3854 {
3855 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3856 while (h->root.type == bfd_link_hash_indirect
3857 || h->root.type == bfd_link_hash_warning)
3858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3859 }
3860
eb043451 3861 r_type = ELF32_R_TYPE (rel->r_info);
b6ee372a 3862#ifndef OLD_ARM_ABI
eb043451 3863 r_type = arm_real_reloc_type (globals, r_type);
b6ee372a 3864#endif
eb043451
PB
3865 switch (r_type)
3866 {
3867 case R_ARM_GOT32:
3868#ifndef OLD_ARM_ABI
3869 case R_ARM_GOT_PREL:
3870#endif
3eb128b2 3871 if (h != NULL)
eb043451 3872 {
eb043451
PB
3873 if (h->got.refcount > 0)
3874 h->got.refcount -= 1;
3875 }
3876 else if (local_got_refcounts != NULL)
3877 {
3878 if (local_got_refcounts[r_symndx] > 0)
3879 local_got_refcounts[r_symndx] -= 1;
3880 }
3881 break;
3882
3883 case R_ARM_ABS32:
3884 case R_ARM_REL32:
3885 case R_ARM_PC24:
3886 case R_ARM_PLT32:
3887#ifndef OLD_ARM_ABI
5b5bb741
PB
3888 case R_ARM_CALL:
3889 case R_ARM_JUMP24:
eb043451
PB
3890 case R_ARM_PREL31:
3891#endif
b7693d02
DJ
3892 case R_ARM_THM_PC22:
3893 /* Should the interworking branches be here also? */
3894
3eb128b2 3895 if (h != NULL)
eb043451
PB
3896 {
3897 struct elf32_arm_link_hash_entry *eh;
3898 struct elf32_arm_relocs_copied **pp;
3899 struct elf32_arm_relocs_copied *p;
5e681ec4 3900
b7693d02 3901 eh = (struct elf32_arm_link_hash_entry *) h;
5e681ec4 3902
eb043451 3903 if (h->plt.refcount > 0)
b7693d02
DJ
3904 {
3905 h->plt.refcount -= 1;
3906 if (ELF32_R_TYPE (rel->r_info) == R_ARM_THM_PC22)
3907 eh->plt_thumb_refcount--;
3908 }
5e681ec4 3909
eb043451 3910 if (r_type == R_ARM_ABS32
eb043451
PB
3911 || r_type == R_ARM_REL32)
3912 {
eb043451
PB
3913 for (pp = &eh->relocs_copied; (p = *pp) != NULL;
3914 pp = &p->next)
3915 if (p->section == sec)
3916 {
3917 p->count -= 1;
3918 if (p->count == 0)
3919 *pp = p->next;
3920 break;
3921 }
3922 }
3923 }
3924 break;
5e681ec4 3925
eb043451
PB
3926 default:
3927 break;
3928 }
3929 }
5e681ec4 3930
b34976b6 3931 return TRUE;
252b5132
RH
3932}
3933
780a67af
NC
3934/* Look through the relocs for a section during the first phase. */
3935
b34976b6 3936static bfd_boolean
57e8b36a
NC
3937elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
3938 asection *sec, const Elf_Internal_Rela *relocs)
252b5132 3939{
b34976b6
AM
3940 Elf_Internal_Shdr *symtab_hdr;
3941 struct elf_link_hash_entry **sym_hashes;
3942 struct elf_link_hash_entry **sym_hashes_end;
3943 const Elf_Internal_Rela *rel;
3944 const Elf_Internal_Rela *rel_end;
3945 bfd *dynobj;
5e681ec4 3946 asection *sreloc;
b34976b6 3947 bfd_vma *local_got_offsets;
5e681ec4 3948 struct elf32_arm_link_hash_table *htab;
9a5aca8c 3949
1049f94e 3950 if (info->relocatable)
b34976b6 3951 return TRUE;
9a5aca8c 3952
5e681ec4
PB
3953 htab = elf32_arm_hash_table (info);
3954 sreloc = NULL;
9a5aca8c 3955
67687978
PB
3956 /* Create dynamic sections for relocatable executables so that we can
3957 copy relocations. */
3958 if (htab->root.is_relocatable_executable
3959 && ! htab->root.dynamic_sections_created)
3960 {
3961 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3962 return FALSE;
3963 }
3964
252b5132
RH
3965 dynobj = elf_hash_table (info)->dynobj;
3966 local_got_offsets = elf_local_got_offsets (abfd);
f21f3fe0 3967
252b5132
RH
3968 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3969 sym_hashes = elf_sym_hashes (abfd);
9b485d32
NC
3970 sym_hashes_end = sym_hashes
3971 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
3972
252b5132
RH
3973 if (!elf_bad_symtab (abfd))
3974 sym_hashes_end -= symtab_hdr->sh_info;
9b485d32 3975
252b5132
RH
3976 rel_end = relocs + sec->reloc_count;
3977 for (rel = relocs; rel < rel_end; rel++)
3978 {
3979 struct elf_link_hash_entry *h;
b7693d02 3980 struct elf32_arm_link_hash_entry *eh;
252b5132 3981 unsigned long r_symndx;
eb043451 3982 int r_type;
9a5aca8c 3983
252b5132 3984 r_symndx = ELF32_R_SYM (rel->r_info);
eb043451
PB
3985 r_type = ELF32_R_TYPE (rel->r_info);
3986#ifndef OLD_ARM_ABI
3987 r_type = arm_real_reloc_type (htab, r_type);
3988#endif
252b5132
RH
3989 if (r_symndx < symtab_hdr->sh_info)
3990 h = NULL;
3991 else
3992 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
9a5aca8c 3993
b7693d02
DJ
3994 eh = (struct elf32_arm_link_hash_entry *) h;
3995
eb043451 3996 switch (r_type)
252b5132 3997 {
5e681ec4 3998 case R_ARM_GOT32:
eb043451
PB
3999#ifndef OLD_ARM_ABI
4000 case R_ARM_GOT_PREL:
4001#endif
5e681ec4 4002 /* This symbol requires a global offset table entry. */
252b5132
RH
4003 if (h != NULL)
4004 {
5e681ec4 4005 h->got.refcount++;
252b5132
RH
4006 }
4007 else
4008 {
5e681ec4
PB
4009 bfd_signed_vma *local_got_refcounts;
4010
4011 /* This is a global offset table entry for a local symbol. */
4012 local_got_refcounts = elf_local_got_refcounts (abfd);
4013 if (local_got_refcounts == NULL)
252b5132 4014 {
dc810e39 4015 bfd_size_type size;
252b5132 4016
dc810e39 4017 size = symtab_hdr->sh_info;
57e8b36a
NC
4018 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4019 local_got_refcounts = bfd_zalloc (abfd, size);
5e681ec4 4020 if (local_got_refcounts == NULL)
b34976b6 4021 return FALSE;
5e681ec4 4022 elf_local_got_refcounts (abfd) = local_got_refcounts;
252b5132 4023 }
5e681ec4 4024 local_got_refcounts[r_symndx] += 1;
252b5132 4025 }
eb043451
PB
4026 if (r_type == R_ARM_GOT32)
4027 break;
4028 /* Fall through. */
252b5132 4029
5e681ec4
PB
4030 case R_ARM_GOTOFF:
4031 case R_ARM_GOTPC:
4032 if (htab->sgot == NULL)
4033 {
4034 if (htab->root.dynobj == NULL)
4035 htab->root.dynobj = abfd;
4036 if (!create_got_section (htab->root.dynobj, info))
4037 return FALSE;
4038 }
252b5132
RH
4039 break;
4040
4041 case R_ARM_ABS32:
4042 case R_ARM_REL32:
4043 case R_ARM_PC24:
7359ea65 4044 case R_ARM_PLT32:
eb043451 4045#ifndef OLD_ARM_ABI
5b5bb741
PB
4046 case R_ARM_CALL:
4047 case R_ARM_JUMP24:
eb043451
PB
4048 case R_ARM_PREL31:
4049#endif
b7693d02
DJ
4050 case R_ARM_THM_PC22:
4051 /* Should the interworking branches be listed here? */
7359ea65 4052 if (h != NULL)
5e681ec4
PB
4053 {
4054 /* If this reloc is in a read-only section, we might
4055 need a copy reloc. We can't check reliably at this
4056 stage whether the section is read-only, as input
4057 sections have not yet been mapped to output sections.
4058 Tentatively set the flag for now, and correct in
4059 adjust_dynamic_symbol. */
7359ea65 4060 if (!info->shared)
f5385ebf 4061 h->non_got_ref = 1;
7359ea65 4062
5e681ec4 4063 /* We may need a .plt entry if the function this reloc
c84cd8ee
DJ
4064 refers to is in a different object. We can't tell for
4065 sure yet, because something later might force the
4066 symbol local. */
eb043451 4067 if (r_type == R_ARM_PC24
5b5bb741
PB
4068#ifndef OLD_ARM_ABI
4069 || r_type == R_ARM_CALL
4070 || r_type == R_ARM_JUMP24
ee06dc07 4071 || r_type == R_ARM_PREL31
5b5bb741 4072#endif
b7693d02
DJ
4073 || r_type == R_ARM_PLT32
4074 || r_type == R_ARM_THM_PC22)
f5385ebf 4075 h->needs_plt = 1;
4f199be3
DJ
4076
4077 /* If we create a PLT entry, this relocation will reference
4078 it, even if it's an ABS32 relocation. */
4079 h->plt.refcount += 1;
b7693d02
DJ
4080
4081 if (r_type == R_ARM_THM_PC22)
4082 eh->plt_thumb_refcount += 1;
5e681ec4
PB
4083 }
4084
67687978
PB
4085 /* If we are creating a shared library or relocatable executable,
4086 and this is a reloc against a global symbol, or a non PC
4087 relative reloc against a local symbol, then we need to copy
4088 the reloc into the shared library. However, if we are linking
4089 with -Bsymbolic, we do not need to copy a reloc against a
252b5132
RH
4090 global symbol which is defined in an object we are
4091 including in the link (i.e., DEF_REGULAR is set). At
4092 this point we have not seen all the input files, so it is
4093 possible that DEF_REGULAR is not set now but will be set
4094 later (it is never cleared). We account for that
4095 possibility below by storing information in the
5e681ec4 4096 relocs_copied field of the hash table entry. */
67687978 4097 if ((info->shared || htab->root.is_relocatable_executable)
5e681ec4 4098 && (sec->flags & SEC_ALLOC) != 0
71a976dd
DJ
4099 && (r_type == R_ARM_ABS32
4100 || (h != NULL && ! h->needs_plt
4101 && (! info->symbolic || ! h->def_regular))))
252b5132 4102 {
5e681ec4
PB
4103 struct elf32_arm_relocs_copied *p, **head;
4104
252b5132
RH
4105 /* When creating a shared object, we must copy these
4106 reloc types into the output file. We create a reloc
4107 section in dynobj and make room for this reloc. */
4108 if (sreloc == NULL)
4109 {
4110 const char * name;
4111
4112 name = (bfd_elf_string_from_elf_section
4113 (abfd,
4114 elf_elfheader (abfd)->e_shstrndx,
4115 elf_section_data (sec)->rel_hdr.sh_name));
4116 if (name == NULL)
b34976b6 4117 return FALSE;
252b5132
RH
4118
4119 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
99e4ae17 4120 && strcmp (bfd_get_section_name (abfd, sec),
252b5132
RH
4121 name + 4) == 0);
4122
4123 sreloc = bfd_get_section_by_name (dynobj, name);
4124 if (sreloc == NULL)
4125 {
4126 flagword flags;
4127
4128 sreloc = bfd_make_section (dynobj, name);
4129 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4130 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
e5a52504
MM
4131 if ((sec->flags & SEC_ALLOC) != 0
4132 /* BPABI objects never have dynamic
4133 relocations mapped. */
4134 && !htab->symbian_p)
252b5132
RH
4135 flags |= SEC_ALLOC | SEC_LOAD;
4136 if (sreloc == NULL
4137 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4138 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
b34976b6 4139 return FALSE;
252b5132 4140 }
5e681ec4
PB
4141
4142 elf_section_data (sec)->sreloc = sreloc;
252b5132
RH
4143 }
4144
5e681ec4
PB
4145 /* If this is a global symbol, we count the number of
4146 relocations we need for this symbol. */
4147 if (h != NULL)
252b5132 4148 {
5e681ec4
PB
4149 head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
4150 }
4151 else
4152 {
4153 /* Track dynamic relocs needed for local syms too.
4154 We really need local syms available to do this
4155 easily. Oh well. */
57e8b36a 4156
5e681ec4
PB
4157 asection *s;
4158 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4159 sec, r_symndx);
4160 if (s == NULL)
4161 return FALSE;
57e8b36a 4162
5e681ec4
PB
4163 head = ((struct elf32_arm_relocs_copied **)
4164 &elf_section_data (s)->local_dynrel);
4165 }
57e8b36a 4166
5e681ec4
PB
4167 p = *head;
4168 if (p == NULL || p->section != sec)
4169 {
4170 bfd_size_type amt = sizeof *p;
57e8b36a 4171
5e681ec4 4172 p = bfd_alloc (htab->root.dynobj, amt);
252b5132 4173 if (p == NULL)
5e681ec4
PB
4174 return FALSE;
4175 p->next = *head;
4176 *head = p;
4177 p->section = sec;
4178 p->count = 0;
252b5132 4179 }
57e8b36a 4180
71a976dd 4181 p->count += 1;
252b5132
RH
4182 }
4183 break;
4184
4185 /* This relocation describes the C++ object vtable hierarchy.
4186 Reconstruct it for later use during GC. */
4187 case R_ARM_GNU_VTINHERIT:
c152c796 4188 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4189 return FALSE;
252b5132 4190 break;
9a5aca8c 4191
252b5132
RH
4192 /* This relocation describes which C++ vtable entries are actually
4193 used. Record for later use during GC. */
4194 case R_ARM_GNU_VTENTRY:
c152c796 4195 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 4196 return FALSE;
252b5132
RH
4197 break;
4198 }
4199 }
f21f3fe0 4200
b34976b6 4201 return TRUE;
252b5132
RH
4202}
4203
b34976b6 4204static bfd_boolean
0367ecfb 4205is_arm_mapping_symbol_name (const char * name)
252b5132 4206{
0367ecfb
NC
4207 return (name != NULL)
4208 && (name[0] == '$')
4209 && ((name[1] == 'a') || (name[1] == 't') || (name[1] == 'd'))
4210 && (name[2] == 0);
4211}
f21f3fe0 4212
3c9458e9
NC
4213/* Treat mapping symbols as special target symbols. */
4214
4215static bfd_boolean
4216elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
4217{
4218 return is_arm_mapping_symbol_name (sym->name);
4219}
4220
0367ecfb
NC
4221/* This is a copy of elf_find_function() from elf.c except that
4222 ARM mapping symbols are ignored when looking for function names
4223 and STT_ARM_TFUNC is considered to a function type. */
252b5132 4224
0367ecfb
NC
4225static bfd_boolean
4226arm_elf_find_function (bfd * abfd ATTRIBUTE_UNUSED,
4227 asection * section,
4228 asymbol ** symbols,
4229 bfd_vma offset,
4230 const char ** filename_ptr,
4231 const char ** functionname_ptr)
4232{
4233 const char * filename = NULL;
4234 asymbol * func = NULL;
4235 bfd_vma low_func = 0;
4236 asymbol ** p;
252b5132
RH
4237
4238 for (p = symbols; *p != NULL; p++)
4239 {
4240 elf_symbol_type *q;
4241
4242 q = (elf_symbol_type *) *p;
4243
252b5132
RH
4244 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4245 {
4246 default:
4247 break;
4248 case STT_FILE:
4249 filename = bfd_asymbol_name (&q->symbol);
4250 break;
252b5132
RH
4251 case STT_FUNC:
4252 case STT_ARM_TFUNC:
0367ecfb
NC
4253 /* Skip $a and $t symbols. */
4254 if ((q->symbol.flags & BSF_LOCAL)
4255 && is_arm_mapping_symbol_name (q->symbol.name))
4256 continue;
4257 /* Fall through. */
4258 case STT_NOTYPE:
6b40fcba 4259 if (bfd_get_section (&q->symbol) == section
252b5132
RH
4260 && q->symbol.value >= low_func
4261 && q->symbol.value <= offset)
4262 {
4263 func = (asymbol *) q;
4264 low_func = q->symbol.value;
4265 }
4266 break;
4267 }
4268 }
4269
4270 if (func == NULL)
b34976b6 4271 return FALSE;
252b5132 4272
0367ecfb
NC
4273 if (filename_ptr)
4274 *filename_ptr = filename;
4275 if (functionname_ptr)
4276 *functionname_ptr = bfd_asymbol_name (func);
4277
4278 return TRUE;
4279}
4280
4281
4282/* Find the nearest line to a particular section and offset, for error
4283 reporting. This code is a duplicate of the code in elf.c, except
4284 that it uses arm_elf_find_function. */
4285
4286static bfd_boolean
4287elf32_arm_find_nearest_line (bfd * abfd,
4288 asection * section,
4289 asymbol ** symbols,
4290 bfd_vma offset,
4291 const char ** filename_ptr,
4292 const char ** functionname_ptr,
4293 unsigned int * line_ptr)
4294{
4295 bfd_boolean found = FALSE;
4296
4297 /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it. */
4298
4299 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4300 filename_ptr, functionname_ptr,
4301 line_ptr, 0,
4302 & elf_tdata (abfd)->dwarf2_find_line_info))
4303 {
4304 if (!*functionname_ptr)
4305 arm_elf_find_function (abfd, section, symbols, offset,
4306 *filename_ptr ? NULL : filename_ptr,
4307 functionname_ptr);
f21f3fe0 4308
0367ecfb
NC
4309 return TRUE;
4310 }
4311
4312 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4313 & found, filename_ptr,
4314 functionname_ptr, line_ptr,
4315 & elf_tdata (abfd)->line_info))
4316 return FALSE;
4317
4318 if (found && (*functionname_ptr || *line_ptr))
4319 return TRUE;
4320
4321 if (symbols == NULL)
4322 return FALSE;
4323
4324 if (! arm_elf_find_function (abfd, section, symbols, offset,
4325 filename_ptr, functionname_ptr))
4326 return FALSE;
4327
4328 *line_ptr = 0;
b34976b6 4329 return TRUE;
252b5132
RH
4330}
4331
4332/* Adjust a symbol defined by a dynamic object and referenced by a
4333 regular object. The current definition is in some section of the
4334 dynamic object, but we're not including those sections. We have to
4335 change the definition to something the rest of the link can
4336 understand. */
4337
b34976b6 4338static bfd_boolean
57e8b36a
NC
4339elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
4340 struct elf_link_hash_entry * h)
252b5132
RH
4341{
4342 bfd * dynobj;
4343 asection * s;
4344 unsigned int power_of_two;
b7693d02 4345 struct elf32_arm_link_hash_entry * eh;
67687978 4346 struct elf32_arm_link_hash_table *globals;
252b5132 4347
67687978 4348 globals = elf32_arm_hash_table (info);
252b5132
RH
4349 dynobj = elf_hash_table (info)->dynobj;
4350
4351 /* Make sure we know what is going on here. */
4352 BFD_ASSERT (dynobj != NULL
f5385ebf 4353 && (h->needs_plt
f6e332e6 4354 || h->u.weakdef != NULL
f5385ebf
AM
4355 || (h->def_dynamic
4356 && h->ref_regular
4357 && !h->def_regular)));
252b5132 4358
b7693d02
DJ
4359 eh = (struct elf32_arm_link_hash_entry *) h;
4360
252b5132
RH
4361 /* If this is a function, put it in the procedure linkage table. We
4362 will fill in the contents of the procedure linkage table later,
4363 when we know the address of the .got section. */
b7693d02 4364 if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
f5385ebf 4365 || h->needs_plt)
252b5132 4366 {
5e681ec4
PB
4367 if (h->plt.refcount <= 0
4368 || SYMBOL_CALLS_LOCAL (info, h)
4369 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4370 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
4371 {
4372 /* This case can occur if we saw a PLT32 reloc in an input
5e681ec4
PB
4373 file, but the symbol was never referred to by a dynamic
4374 object, or if all references were garbage collected. In
4375 such a case, we don't actually need to build a procedure
4376 linkage table, and we can just do a PC24 reloc instead. */
4377 h->plt.offset = (bfd_vma) -1;
b7693d02 4378 eh->plt_thumb_refcount = 0;
f5385ebf 4379 h->needs_plt = 0;
252b5132
RH
4380 }
4381
b34976b6 4382 return TRUE;
252b5132 4383 }
5e681ec4 4384 else
b7693d02
DJ
4385 {
4386 /* It's possible that we incorrectly decided a .plt reloc was
4387 needed for an R_ARM_PC24 or similar reloc to a non-function sym
4388 in check_relocs. We can't decide accurately between function
4389 and non-function syms in check-relocs; Objects loaded later in
4390 the link may change h->type. So fix it now. */
4391 h->plt.offset = (bfd_vma) -1;
4392 eh->plt_thumb_refcount = 0;
4393 }
252b5132
RH
4394
4395 /* If this is a weak symbol, and there is a real definition, the
4396 processor independent code will have arranged for us to see the
4397 real definition first, and we can just use the same value. */
f6e332e6 4398 if (h->u.weakdef != NULL)
252b5132 4399 {
f6e332e6
AM
4400 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4401 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4402 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4403 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 4404 return TRUE;
252b5132
RH
4405 }
4406
4407 /* This is a reference to a symbol defined by a dynamic object which
4408 is not a function. */
4409
4410 /* If we are creating a shared library, we must presume that the
4411 only references to the symbol are via the global offset table.
4412 For such cases we need not do anything here; the relocations will
67687978
PB
4413 be handled correctly by relocate_section. Relocatable executables
4414 can reference data in shared objects directly, so we don't need to
4415 do anything here. */
4416 if (info->shared || globals->root.is_relocatable_executable)
b34976b6 4417 return TRUE;
252b5132
RH
4418
4419 /* We must allocate the symbol in our .dynbss section, which will
4420 become part of the .bss section of the executable. There will be
4421 an entry for this symbol in the .dynsym section. The dynamic
4422 object will contain position independent code, so all references
4423 from the dynamic object to this symbol will go through the global
4424 offset table. The dynamic linker will use the .dynsym entry to
4425 determine the address it must put in the global offset table, so
4426 both the dynamic object and the regular object will refer to the
4427 same memory location for the variable. */
252b5132
RH
4428 s = bfd_get_section_by_name (dynobj, ".dynbss");
4429 BFD_ASSERT (s != NULL);
4430
4431 /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
4432 copy the initial value out of the dynamic object and into the
4433 runtime process image. We need to remember the offset into the
4434 .rel.bss section we are going to use. */
4435 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4436 {
4437 asection *srel;
4438
4439 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
4440 BFD_ASSERT (srel != NULL);
eea6121a 4441 srel->size += sizeof (Elf32_External_Rel);
f5385ebf 4442 h->needs_copy = 1;
252b5132
RH
4443 }
4444
4445 /* We need to figure out the alignment required for this symbol. I
4446 have no idea how ELF linkers handle this. */
4447 power_of_two = bfd_log2 (h->size);
4448 if (power_of_two > 3)
4449 power_of_two = 3;
4450
4451 /* Apply the required alignment. */
eea6121a 4452 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
252b5132
RH
4453 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4454 {
4455 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
b34976b6 4456 return FALSE;
252b5132
RH
4457 }
4458
4459 /* Define the symbol as being at this point in the section. */
4460 h->root.u.def.section = s;
eea6121a 4461 h->root.u.def.value = s->size;
252b5132
RH
4462
4463 /* Increment the section size to make room for the symbol. */
eea6121a 4464 s->size += h->size;
252b5132 4465
b34976b6 4466 return TRUE;
252b5132
RH
4467}
4468
5e681ec4
PB
4469/* Allocate space in .plt, .got and associated reloc sections for
4470 dynamic relocs. */
4471
4472static bfd_boolean
57e8b36a 4473allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
5e681ec4
PB
4474{
4475 struct bfd_link_info *info;
4476 struct elf32_arm_link_hash_table *htab;
4477 struct elf32_arm_link_hash_entry *eh;
4478 struct elf32_arm_relocs_copied *p;
4479
b7693d02
DJ
4480 eh = (struct elf32_arm_link_hash_entry *) h;
4481
5e681ec4
PB
4482 if (h->root.type == bfd_link_hash_indirect)
4483 return TRUE;
4484
4485 if (h->root.type == bfd_link_hash_warning)
4486 /* When warning symbols are created, they **replace** the "real"
4487 entry in the hash table, thus we never get to see the real
4488 symbol in a hash traversal. So look at it now. */
4489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4490
4491 info = (struct bfd_link_info *) inf;
4492 htab = elf32_arm_hash_table (info);
4493
4494 if (htab->root.dynamic_sections_created
4495 && h->plt.refcount > 0)
4496 {
4497 /* Make sure this symbol is output as a dynamic symbol.
4498 Undefined weak syms won't yet be marked as dynamic. */
4499 if (h->dynindx == -1
f5385ebf 4500 && !h->forced_local)
5e681ec4 4501 {
c152c796 4502 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5e681ec4
PB
4503 return FALSE;
4504 }
4505
4506 if (info->shared
7359ea65 4507 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
5e681ec4
PB
4508 {
4509 asection *s = htab->splt;
4510
4511 /* If this is the first .plt entry, make room for the special
4512 first entry. */
eea6121a 4513 if (s->size == 0)
e5a52504 4514 s->size += htab->plt_header_size;
5e681ec4 4515
eea6121a 4516 h->plt.offset = s->size;
5e681ec4 4517
b7693d02
DJ
4518 /* If we will insert a Thumb trampoline before this PLT, leave room
4519 for it. */
4520 if (!htab->symbian_p && eh->plt_thumb_refcount > 0)
4521 {
4522 h->plt.offset += PLT_THUMB_STUB_SIZE;
4523 s->size += PLT_THUMB_STUB_SIZE;
4524 }
4525
5e681ec4
PB
4526 /* If this symbol is not defined in a regular file, and we are
4527 not generating a shared library, then set the symbol to this
4528 location in the .plt. This is required to make function
4529 pointers compare as equal between the normal executable and
4530 the shared library. */
4531 if (! info->shared
f5385ebf 4532 && !h->def_regular)
5e681ec4
PB
4533 {
4534 h->root.u.def.section = s;
4535 h->root.u.def.value = h->plt.offset;
b7693d02
DJ
4536
4537 /* Make sure the function is not marked as Thumb, in case
4538 it is the target of an ABS32 relocation, which will
4539 point to the PLT entry. */
4540 if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
4541 h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
5e681ec4
PB
4542 }
4543
4544 /* Make room for this entry. */
e5a52504 4545 s->size += htab->plt_entry_size;
5e681ec4 4546
e5a52504 4547 if (!htab->symbian_p)
b7693d02
DJ
4548 {
4549 /* We also need to make an entry in the .got.plt section, which
4550 will be placed in the .got section by the linker script. */
4551 eh->plt_got_offset = htab->sgotplt->size;
4552 htab->sgotplt->size += 4;
4553 }
5e681ec4
PB
4554
4555 /* We also need to make an entry in the .rel.plt section. */
eea6121a 4556 htab->srelplt->size += sizeof (Elf32_External_Rel);
5e681ec4
PB
4557 }
4558 else
4559 {
4560 h->plt.offset = (bfd_vma) -1;
f5385ebf 4561 h->needs_plt = 0;
5e681ec4
PB
4562 }
4563 }
4564 else
4565 {
4566 h->plt.offset = (bfd_vma) -1;
f5385ebf 4567 h->needs_plt = 0;
5e681ec4
PB
4568 }
4569
4570 if (h->got.refcount > 0)
4571 {
4572 asection *s;
4573 bfd_boolean dyn;
4574
4575 /* Make sure this symbol is output as a dynamic symbol.
4576 Undefined weak syms won't yet be marked as dynamic. */
4577 if (h->dynindx == -1
f5385ebf 4578 && !h->forced_local)
5e681ec4 4579 {
c152c796 4580 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5e681ec4
PB
4581 return FALSE;
4582 }
4583
e5a52504
MM
4584 if (!htab->symbian_p)
4585 {
4586 s = htab->sgot;
4587 h->got.offset = s->size;
4588 s->size += 4;
4589 dyn = htab->root.dynamic_sections_created;
4590 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4591 || h->root.type != bfd_link_hash_undefweak)
4592 && (info->shared
4593 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
4594 htab->srelgot->size += sizeof (Elf32_External_Rel);
4595 }
5e681ec4
PB
4596 }
4597 else
4598 h->got.offset = (bfd_vma) -1;
4599
5e681ec4
PB
4600 if (eh->relocs_copied == NULL)
4601 return TRUE;
4602
4603 /* In the shared -Bsymbolic case, discard space allocated for
4604 dynamic pc-relative relocs against symbols which turn out to be
4605 defined in regular objects. For the normal shared case, discard
4606 space for pc-relative relocs that have become local due to symbol
4607 visibility changes. */
4608
67687978 4609 if (info->shared || htab->root.is_relocatable_executable)
5e681ec4 4610 {
7359ea65
DJ
4611 /* Discard relocs on undefined weak syms with non-default
4612 visibility. */
5e681ec4
PB
4613 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4614 && h->root.type == bfd_link_hash_undefweak)
4615 eh->relocs_copied = NULL;
67687978
PB
4616 else if (htab->root.is_relocatable_executable && h->dynindx == -1
4617 && h->root.type == bfd_link_hash_new)
4618 {
4619 /* Output absolute symbols so that we can create relocations
4620 against them. For normal symbols we output a relocation
4621 against the section that contains them. */
4622 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4623 return FALSE;
4624 }
4625
5e681ec4
PB
4626 }
4627 else
4628 {
4629 /* For the non-shared case, discard space for relocs against
4630 symbols which turn out to need copy relocs or are not
4631 dynamic. */
4632
f5385ebf
AM
4633 if (!h->non_got_ref
4634 && ((h->def_dynamic
4635 && !h->def_regular)
5e681ec4
PB
4636 || (htab->root.dynamic_sections_created
4637 && (h->root.type == bfd_link_hash_undefweak
4638 || h->root.type == bfd_link_hash_undefined))))
4639 {
4640 /* Make sure this symbol is output as a dynamic symbol.
4641 Undefined weak syms won't yet be marked as dynamic. */
4642 if (h->dynindx == -1
f5385ebf 4643 && !h->forced_local)
5e681ec4 4644 {
c152c796 4645 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5e681ec4
PB
4646 return FALSE;
4647 }
4648
4649 /* If that succeeded, we know we'll be keeping all the
4650 relocs. */
4651 if (h->dynindx != -1)
4652 goto keep;
4653 }
4654
4655 eh->relocs_copied = NULL;
4656
4657 keep: ;
4658 }
4659
4660 /* Finally, allocate space. */
4661 for (p = eh->relocs_copied; p != NULL; p = p->next)
4662 {
4663 asection *sreloc = elf_section_data (p->section)->sreloc;
eea6121a 4664 sreloc->size += p->count * sizeof (Elf32_External_Rel);
5e681ec4
PB
4665 }
4666
4667 return TRUE;
4668}
4669
08d1f311
DJ
4670/* Find any dynamic relocs that apply to read-only sections. */
4671
4672static bfd_boolean
4673elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
4674{
4675 struct elf32_arm_link_hash_entry *eh;
4676 struct elf32_arm_relocs_copied *p;
4677
4678 if (h->root.type == bfd_link_hash_warning)
4679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4680
4681 eh = (struct elf32_arm_link_hash_entry *) h;
4682 for (p = eh->relocs_copied; p != NULL; p = p->next)
4683 {
4684 asection *s = p->section;
4685
4686 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4687 {
4688 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4689
4690 info->flags |= DF_TEXTREL;
4691
4692 /* Not an error, just cut short the traversal. */
4693 return FALSE;
4694 }
4695 }
4696 return TRUE;
4697}
4698
252b5132
RH
4699/* Set the sizes of the dynamic sections. */
4700
b34976b6 4701static bfd_boolean
57e8b36a
NC
4702elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
4703 struct bfd_link_info * info)
252b5132
RH
4704{
4705 bfd * dynobj;
4706 asection * s;
b34976b6
AM
4707 bfd_boolean plt;
4708 bfd_boolean relocs;
5e681ec4
PB
4709 bfd *ibfd;
4710 struct elf32_arm_link_hash_table *htab;
252b5132 4711
5e681ec4 4712 htab = elf32_arm_hash_table (info);
252b5132
RH
4713 dynobj = elf_hash_table (info)->dynobj;
4714 BFD_ASSERT (dynobj != NULL);
4715
4716 if (elf_hash_table (info)->dynamic_sections_created)
4717 {
4718 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 4719 if (info->executable)
252b5132
RH
4720 {
4721 s = bfd_get_section_by_name (dynobj, ".interp");
4722 BFD_ASSERT (s != NULL);
eea6121a 4723 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
252b5132
RH
4724 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4725 }
4726 }
5e681ec4
PB
4727
4728 /* Set up .got offsets for local syms, and space for local dynamic
4729 relocs. */
4730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
252b5132 4731 {
5e681ec4
PB
4732 bfd_signed_vma *local_got;
4733 bfd_signed_vma *end_local_got;
4734 char *local_tls_type;
4735 bfd_size_type locsymcount;
4736 Elf_Internal_Shdr *symtab_hdr;
4737 asection *srel;
4738
4739 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4740 continue;
4741
4742 for (s = ibfd->sections; s != NULL; s = s->next)
4743 {
4744 struct elf32_arm_relocs_copied *p;
4745
4746 for (p = *((struct elf32_arm_relocs_copied **)
4747 &elf_section_data (s)->local_dynrel);
4748 p != NULL;
4749 p = p->next)
4750 {
4751 if (!bfd_is_abs_section (p->section)
4752 && bfd_is_abs_section (p->section->output_section))
4753 {
4754 /* Input section has been discarded, either because
4755 it is a copy of a linkonce section or due to
4756 linker script /DISCARD/, so we'll be discarding
4757 the relocs too. */
4758 }
4759 else if (p->count != 0)
4760 {
4761 srel = elf_section_data (p->section)->sreloc;
eea6121a 4762 srel->size += p->count * sizeof (Elf32_External_Rel);
5e681ec4
PB
4763 if ((p->section->output_section->flags & SEC_READONLY) != 0)
4764 info->flags |= DF_TEXTREL;
4765 }
4766 }
4767 }
4768
4769 local_got = elf_local_got_refcounts (ibfd);
4770 if (!local_got)
4771 continue;
4772
4773 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4774 locsymcount = symtab_hdr->sh_info;
4775 end_local_got = local_got + locsymcount;
4776 s = htab->sgot;
4777 srel = htab->srelgot;
4778 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
4779 {
4780 if (*local_got > 0)
4781 {
eea6121a
AM
4782 *local_got = s->size;
4783 s->size += 4;
5e681ec4 4784 if (info->shared)
eea6121a 4785 srel->size += sizeof (Elf32_External_Rel);
5e681ec4
PB
4786 }
4787 else
4788 *local_got = (bfd_vma) -1;
4789 }
252b5132
RH
4790 }
4791
5e681ec4
PB
4792 /* Allocate global sym .plt and .got entries, and space for global
4793 sym dynamic relocs. */
57e8b36a 4794 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
252b5132
RH
4795
4796 /* The check_relocs and adjust_dynamic_symbol entry points have
4797 determined the sizes of the various dynamic sections. Allocate
4798 memory for them. */
b34976b6
AM
4799 plt = FALSE;
4800 relocs = FALSE;
252b5132
RH
4801 for (s = dynobj->sections; s != NULL; s = s->next)
4802 {
4803 const char * name;
b34976b6 4804 bfd_boolean strip;
252b5132
RH
4805
4806 if ((s->flags & SEC_LINKER_CREATED) == 0)
4807 continue;
4808
4809 /* It's OK to base decisions on the section name, because none
4810 of the dynobj section names depend upon the input files. */
4811 name = bfd_get_section_name (dynobj, s);
4812
b34976b6 4813 strip = FALSE;
252b5132 4814
24a1ba0f 4815 if (strcmp (name, ".plt") == 0)
252b5132 4816 {
eea6121a 4817 if (s->size == 0)
252b5132
RH
4818 {
4819 /* Strip this section if we don't need it; see the
4820 comment below. */
b34976b6 4821 strip = TRUE;
252b5132
RH
4822 }
4823 else
4824 {
4825 /* Remember whether there is a PLT. */
b34976b6 4826 plt = TRUE;
252b5132
RH
4827 }
4828 }
4829 else if (strncmp (name, ".rel", 4) == 0)
4830 {
eea6121a 4831 if (s->size == 0)
252b5132
RH
4832 {
4833 /* If we don't need this section, strip it from the
4834 output file. This is mostly to handle .rel.bss and
4835 .rel.plt. We must create both sections in
4836 create_dynamic_sections, because they must be created
4837 before the linker maps input sections to output
4838 sections. The linker does that before
4839 adjust_dynamic_symbol is called, and it is that
4840 function which decides whether anything needs to go
4841 into these sections. */
b34976b6 4842 strip = TRUE;
252b5132
RH
4843 }
4844 else
4845 {
252b5132
RH
4846 /* Remember whether there are any reloc sections other
4847 than .rel.plt. */
4848 if (strcmp (name, ".rel.plt") != 0)
b34976b6 4849 relocs = TRUE;
252b5132
RH
4850
4851 /* We use the reloc_count field as a counter if we need
4852 to copy relocs into the output file. */
4853 s->reloc_count = 0;
4854 }
4855 }
4856 else if (strncmp (name, ".got", 4) != 0)
4857 {
4858 /* It's not one of our sections, so don't allocate space. */
4859 continue;
4860 }
4861
4862 if (strip)
4863 {
52585bb8 4864 _bfd_strip_section_from_output (info, s);
252b5132
RH
4865 continue;
4866 }
4867
4868 /* Allocate memory for the section contents. */
eea6121a
AM
4869 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4870 if (s->contents == NULL && s->size != 0)
b34976b6 4871 return FALSE;
252b5132
RH
4872 }
4873
4874 if (elf_hash_table (info)->dynamic_sections_created)
4875 {
4876 /* Add some entries to the .dynamic section. We fill in the
4877 values later, in elf32_arm_finish_dynamic_sections, but we
4878 must add the entries now so that we get the correct size for
4879 the .dynamic section. The DT_DEBUG entry is filled in by the
4880 dynamic linker and used by the debugger. */
dc810e39 4881#define add_dynamic_entry(TAG, VAL) \
5a580b3a 4882 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39
AM
4883
4884 if (!info->shared)
252b5132 4885 {
dc810e39 4886 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 4887 return FALSE;
252b5132
RH
4888 }
4889
4890 if (plt)
4891 {
dc810e39
AM
4892 if ( !add_dynamic_entry (DT_PLTGOT, 0)
4893 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4894 || !add_dynamic_entry (DT_PLTREL, DT_REL)
4895 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 4896 return FALSE;
252b5132
RH
4897 }
4898
4899 if (relocs)
4900 {
dc810e39
AM
4901 if ( !add_dynamic_entry (DT_REL, 0)
4902 || !add_dynamic_entry (DT_RELSZ, 0)
4903 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
b34976b6 4904 return FALSE;
252b5132
RH
4905 }
4906
08d1f311
DJ
4907 /* If any dynamic relocs apply to a read-only section,
4908 then we need a DT_TEXTREL entry. */
4909 if ((info->flags & DF_TEXTREL) == 0)
4910 elf_link_hash_traverse (&htab->root, elf32_arm_readonly_dynrelocs,
4911 (PTR) info);
4912
99e4ae17 4913 if ((info->flags & DF_TEXTREL) != 0)
252b5132 4914 {
dc810e39 4915 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 4916 return FALSE;
d6cf2879 4917 info->flags |= DF_TEXTREL;
252b5132
RH
4918 }
4919 }
dc810e39 4920#undef add_synamic_entry
252b5132 4921
b34976b6 4922 return TRUE;
252b5132
RH
4923}
4924
252b5132
RH
4925/* Finish up dynamic symbol handling. We set the contents of various
4926 dynamic sections here. */
4927
b34976b6 4928static bfd_boolean
57e8b36a
NC
4929elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
4930 struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
252b5132
RH
4931{
4932 bfd * dynobj;
e5a52504 4933 struct elf32_arm_link_hash_table *htab;
b7693d02 4934 struct elf32_arm_link_hash_entry *eh;
252b5132
RH
4935
4936 dynobj = elf_hash_table (info)->dynobj;
e5a52504 4937 htab = elf32_arm_hash_table (info);
b7693d02 4938 eh = (struct elf32_arm_link_hash_entry *) h;
252b5132
RH
4939
4940 if (h->plt.offset != (bfd_vma) -1)
4941 {
4942 asection * splt;
252b5132 4943 asection * srel;
e5a52504 4944 bfd_byte *loc;
24a1ba0f 4945 bfd_vma plt_index;
947216bf 4946 Elf_Internal_Rela rel;
252b5132
RH
4947
4948 /* This symbol has an entry in the procedure linkage table. Set
4949 it up. */
4950
4951 BFD_ASSERT (h->dynindx != -1);
4952
4953 splt = bfd_get_section_by_name (dynobj, ".plt");
252b5132 4954 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
e5a52504 4955 BFD_ASSERT (splt != NULL && srel != NULL);
252b5132 4956
e5a52504
MM
4957 /* Fill in the entry in the procedure linkage table. */
4958 if (htab->symbian_p)
4959 {
4960 unsigned i;
4961 for (i = 0; i < htab->plt_entry_size / 4; ++i)
4962 bfd_put_32 (output_bfd,
4963 elf32_arm_symbian_plt_entry[i],
4964 splt->contents + h->plt.offset + 4 * i);
4965
4966 /* Fill in the entry in the .rel.plt section. */
2a1b9a48
MM
4967 rel.r_offset = (splt->output_section->vma
4968 + splt->output_offset
e5a52504
MM
4969 + h->plt.offset + 4 * (i - 1));
4970 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
b7693d02
DJ
4971
4972 /* Get the index in the procedure linkage table which
4973 corresponds to this symbol. This is the index of this symbol
4974 in all the symbols for which we are making plt entries. The
4975 first entry in the procedure linkage table is reserved. */
4976 plt_index = ((h->plt.offset - htab->plt_header_size)
4977 / htab->plt_entry_size);
e5a52504
MM
4978 }
4979 else
4980 {
4981 bfd_vma got_offset;
4982 bfd_vma got_displacement;
4983 asection * sgot;
4984
4985 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4986 BFD_ASSERT (sgot != NULL);
4987
b7693d02
DJ
4988 /* Get the offset into the .got.plt table of the entry that
4989 corresponds to this function. */
4990 got_offset = eh->plt_got_offset;
4991
4992 /* Get the index in the procedure linkage table which
4993 corresponds to this symbol. This is the index of this symbol
4994 in all the symbols for which we are making plt entries. The
4995 first three entries in .got.plt are reserved; after that
4996 symbols appear in the same order as in .plt. */
4997 plt_index = (got_offset - 12) / 4;
e5a52504
MM
4998
4999 /* Calculate the displacement between the PLT slot and the
b7693d02
DJ
5000 entry in the GOT. The eight-byte offset accounts for the
5001 value produced by adding to pc in the first instruction
5002 of the PLT stub. */
e5a52504
MM
5003 got_displacement = (sgot->output_section->vma
5004 + sgot->output_offset
5005 + got_offset
5006 - splt->output_section->vma
5007 - splt->output_offset
5008 - h->plt.offset
5009 - 8);
5e681ec4 5010
e5a52504 5011 BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
5e681ec4 5012
b7693d02
DJ
5013 if (eh->plt_thumb_refcount > 0)
5014 {
5015 bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[0],
5016 splt->contents + h->plt.offset - 4);
5017 bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[1],
5018 splt->contents + h->plt.offset - 2);
5019 }
5020
e5a52504
MM
5021 bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
5022 splt->contents + h->plt.offset + 0);
5023 bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
5024 splt->contents + h->plt.offset + 4);
5025 bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
5026 splt->contents + h->plt.offset + 8);
5e681ec4 5027#ifdef FOUR_WORD_PLT
e5a52504
MM
5028 bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
5029 splt->contents + h->plt.offset + 12);
5e681ec4 5030#endif
252b5132 5031
e5a52504
MM
5032 /* Fill in the entry in the global offset table. */
5033 bfd_put_32 (output_bfd,
5034 (splt->output_section->vma
5035 + splt->output_offset),
5036 sgot->contents + got_offset);
5037
5038 /* Fill in the entry in the .rel.plt section. */
5039 rel.r_offset = (sgot->output_section->vma
5040 + sgot->output_offset
5041 + got_offset);
5042 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
5043 }
57e8b36a 5044
947216bf
AM
5045 loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
5046 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132 5047
f5385ebf 5048 if (!h->def_regular)
252b5132
RH
5049 {
5050 /* Mark the symbol as undefined, rather than as defined in
5051 the .plt section. Leave the value alone. */
5052 sym->st_shndx = SHN_UNDEF;
d982ba73
PB
5053 /* If the symbol is weak, we do need to clear the value.
5054 Otherwise, the PLT entry would provide a definition for
5055 the symbol even if the symbol wasn't defined anywhere,
5056 and so the symbol would never be NULL. */
f5385ebf 5057 if (!h->ref_regular_nonweak)
d982ba73 5058 sym->st_value = 0;
252b5132
RH
5059 }
5060 }
5061
5062 if (h->got.offset != (bfd_vma) -1)
5063 {
5064 asection * sgot;
5065 asection * srel;
947216bf
AM
5066 Elf_Internal_Rela rel;
5067 bfd_byte *loc;
252b5132
RH
5068
5069 /* This symbol has an entry in the global offset table. Set it
5070 up. */
252b5132
RH
5071 sgot = bfd_get_section_by_name (dynobj, ".got");
5072 srel = bfd_get_section_by_name (dynobj, ".rel.got");
5073 BFD_ASSERT (sgot != NULL && srel != NULL);
5074
5075 rel.r_offset = (sgot->output_section->vma
5076 + sgot->output_offset
dc810e39 5077 + (h->got.offset &~ (bfd_vma) 1));
252b5132 5078
5e681ec4
PB
5079 /* If this is a static link, or it is a -Bsymbolic link and the
5080 symbol is defined locally or was forced to be local because
5081 of a version file, we just want to emit a RELATIVE reloc.
5082 The entry in the global offset table will already have been
5083 initialized in the relocate_section function. */
252b5132 5084 if (info->shared
5e681ec4
PB
5085 && SYMBOL_REFERENCES_LOCAL (info, h))
5086 {
5087 BFD_ASSERT((h->got.offset & 1) != 0);
5088 rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
5089 }
252b5132
RH
5090 else
5091 {
5e681ec4 5092 BFD_ASSERT((h->got.offset & 1) == 0);
252b5132
RH
5093 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5094 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
5095 }
5096
947216bf
AM
5097 loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
5098 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
5099 }
5100
f5385ebf 5101 if (h->needs_copy)
252b5132
RH
5102 {
5103 asection * s;
947216bf
AM
5104 Elf_Internal_Rela rel;
5105 bfd_byte *loc;
252b5132
RH
5106
5107 /* This symbol needs a copy reloc. Set it up. */
252b5132
RH
5108 BFD_ASSERT (h->dynindx != -1
5109 && (h->root.type == bfd_link_hash_defined
5110 || h->root.type == bfd_link_hash_defweak));
5111
5112 s = bfd_get_section_by_name (h->root.u.def.section->owner,
5113 ".rel.bss");
5114 BFD_ASSERT (s != NULL);
5115
5116 rel.r_offset = (h->root.u.def.value
5117 + h->root.u.def.section->output_section->vma
5118 + h->root.u.def.section->output_offset);
5119 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
947216bf
AM
5120 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
5121 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
252b5132
RH
5122 }
5123
5124 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5125 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5126 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5127 sym->st_shndx = SHN_ABS;
5128
b34976b6 5129 return TRUE;
252b5132
RH
5130}
5131
5132/* Finish up the dynamic sections. */
5133
b34976b6 5134static bfd_boolean
57e8b36a 5135elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
252b5132
RH
5136{
5137 bfd * dynobj;
5138 asection * sgot;
5139 asection * sdyn;
5140
5141 dynobj = elf_hash_table (info)->dynobj;
5142
5143 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
229fcec5 5144 BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
252b5132
RH
5145 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5146
5147 if (elf_hash_table (info)->dynamic_sections_created)
5148 {
5149 asection *splt;
5150 Elf32_External_Dyn *dyncon, *dynconend;
229fcec5 5151 struct elf32_arm_link_hash_table *htab;
252b5132 5152
229fcec5 5153 htab = elf32_arm_hash_table (info);
252b5132 5154 splt = bfd_get_section_by_name (dynobj, ".plt");
24a1ba0f 5155 BFD_ASSERT (splt != NULL && sdyn != NULL);
252b5132
RH
5156
5157 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 5158 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
9b485d32 5159
252b5132
RH
5160 for (; dyncon < dynconend; dyncon++)
5161 {
5162 Elf_Internal_Dyn dyn;
5163 const char * name;
5164 asection * s;
5165
5166 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5167
5168 switch (dyn.d_tag)
5169 {
229fcec5
MM
5170 unsigned int type;
5171
252b5132
RH
5172 default:
5173 break;
5174
229fcec5
MM
5175 case DT_HASH:
5176 name = ".hash";
5177 goto get_vma_if_bpabi;
5178 case DT_STRTAB:
5179 name = ".dynstr";
5180 goto get_vma_if_bpabi;
5181 case DT_SYMTAB:
5182 name = ".dynsym";
5183 goto get_vma_if_bpabi;
c0042f5d
MM
5184 case DT_VERSYM:
5185 name = ".gnu.version";
5186 goto get_vma_if_bpabi;
5187 case DT_VERDEF:
5188 name = ".gnu.version_d";
5189 goto get_vma_if_bpabi;
5190 case DT_VERNEED:
5191 name = ".gnu.version_r";
5192 goto get_vma_if_bpabi;
5193
252b5132
RH
5194 case DT_PLTGOT:
5195 name = ".got";
5196 goto get_vma;
5197 case DT_JMPREL:
5198 name = ".rel.plt";
5199 get_vma:
5200 s = bfd_get_section_by_name (output_bfd, name);
5201 BFD_ASSERT (s != NULL);
229fcec5
MM
5202 if (!htab->symbian_p)
5203 dyn.d_un.d_ptr = s->vma;
5204 else
5205 /* In the BPABI, tags in the PT_DYNAMIC section point
5206 at the file offset, not the memory address, for the
5207 convenience of the post linker. */
5208 dyn.d_un.d_ptr = s->filepos;
252b5132
RH
5209 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5210 break;
5211
229fcec5
MM
5212 get_vma_if_bpabi:
5213 if (htab->symbian_p)
5214 goto get_vma;
5215 break;
5216
252b5132
RH
5217 case DT_PLTRELSZ:
5218 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5219 BFD_ASSERT (s != NULL);
eea6121a 5220 dyn.d_un.d_val = s->size;
252b5132
RH
5221 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5222 break;
229fcec5 5223
252b5132 5224 case DT_RELSZ:
229fcec5
MM
5225 if (!htab->symbian_p)
5226 {
5227 /* My reading of the SVR4 ABI indicates that the
5228 procedure linkage table relocs (DT_JMPREL) should be
5229 included in the overall relocs (DT_REL). This is
5230 what Solaris does. However, UnixWare can not handle
5231 that case. Therefore, we override the DT_RELSZ entry
5232 here to make it not include the JMPREL relocs. Since
5233 the linker script arranges for .rel.plt to follow all
5234 other relocation sections, we don't have to worry
5235 about changing the DT_REL entry. */
5236 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5237 if (s != NULL)
5238 dyn.d_un.d_val -= s->size;
5239 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5240 break;
5241 }
5242 /* Fall through */
5243
5244 case DT_REL:
5245 case DT_RELA:
5246 case DT_RELASZ:
5247 /* In the BPABI, the DT_REL tag must point at the file
5248 offset, not the VMA, of the first relocation
5249 section. So, we use code similar to that in
5250 elflink.c, but do not check for SHF_ALLOC on the
5251 relcoation section, since relocations sections are
5252 never allocated under the BPABI. The comments above
5253 about Unixware notwithstanding, we include all of the
5254 relocations here. */
5255 if (htab->symbian_p)
5256 {
5257 unsigned int i;
5258 type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5259 ? SHT_REL : SHT_RELA);
5260 dyn.d_un.d_val = 0;
5261 for (i = 1; i < elf_numsections (output_bfd); i++)
5262 {
5263 Elf_Internal_Shdr *hdr
5264 = elf_elfsections (output_bfd)[i];
5265 if (hdr->sh_type == type)
5266 {
5267 if (dyn.d_tag == DT_RELSZ
5268 || dyn.d_tag == DT_RELASZ)
5269 dyn.d_un.d_val += hdr->sh_size;
de52dba4
AM
5270 else if ((ufile_ptr) hdr->sh_offset
5271 <= dyn.d_un.d_val - 1)
229fcec5
MM
5272 dyn.d_un.d_val = hdr->sh_offset;
5273 }
5274 }
5275 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5276 }
252b5132 5277 break;
88f7bcd5
NC
5278
5279 /* Set the bottom bit of DT_INIT/FINI if the
5280 corresponding function is Thumb. */
5281 case DT_INIT:
5282 name = info->init_function;
5283 goto get_sym;
5284 case DT_FINI:
5285 name = info->fini_function;
5286 get_sym:
5287 /* If it wasn't set by elf_bfd_final_link
4cc11e76 5288 then there is nothing to adjust. */
88f7bcd5
NC
5289 if (dyn.d_un.d_val != 0)
5290 {
5291 struct elf_link_hash_entry * eh;
5292
5293 eh = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 5294 FALSE, FALSE, TRUE);
88f7bcd5
NC
5295 if (eh != (struct elf_link_hash_entry *) NULL
5296 && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
5297 {
5298 dyn.d_un.d_val |= 1;
b34976b6 5299 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
88f7bcd5
NC
5300 }
5301 }
5302 break;
252b5132
RH
5303 }
5304 }
5305
24a1ba0f 5306 /* Fill in the first entry in the procedure linkage table. */
e5a52504 5307 if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
f7a74f8c 5308 {
5e681ec4
PB
5309 bfd_vma got_displacement;
5310
5311 /* Calculate the displacement between the PLT slot and &GOT[0]. */
5312 got_displacement = (sgot->output_section->vma
5313 + sgot->output_offset
5314 - splt->output_section->vma
5315 - splt->output_offset
5316 - 16);
5317
f7a74f8c
NC
5318 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents + 0);
5319 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents + 4);
5320 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents + 8);
5321 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
5e681ec4
PB
5322#ifdef FOUR_WORD_PLT
5323 /* The displacement value goes in the otherwise-unused last word of
5324 the second entry. */
5325 bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
5326#else
5327 bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
5328#endif
f7a74f8c 5329 }
252b5132
RH
5330
5331 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5332 really seem like the right value. */
5333 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5334 }
5335
5336 /* Fill in the first three entries in the global offset table. */
229fcec5 5337 if (sgot)
252b5132 5338 {
229fcec5
MM
5339 if (sgot->size > 0)
5340 {
5341 if (sdyn == NULL)
5342 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5343 else
5344 bfd_put_32 (output_bfd,
5345 sdyn->output_section->vma + sdyn->output_offset,
5346 sgot->contents);
5347 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5348 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5349 }
252b5132 5350
229fcec5
MM
5351 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5352 }
252b5132 5353
b34976b6 5354 return TRUE;
252b5132
RH
5355}
5356
ba96a88f 5357static void
57e8b36a 5358elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
ba96a88f 5359{
9b485d32 5360 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
e489d0ae 5361 struct elf32_arm_link_hash_table *globals;
ba96a88f
NC
5362
5363 i_ehdrp = elf_elfheader (abfd);
5364
94a3258f
PB
5365 if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
5366 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
5367 else
5368 i_ehdrp->e_ident[EI_OSABI] = 0;
ba96a88f 5369 i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
e489d0ae 5370
93204d3a
PB
5371 if (link_info)
5372 {
5373 globals = elf32_arm_hash_table (link_info);
5374 if (globals->byteswap_code)
5375 i_ehdrp->e_flags |= EF_ARM_BE8;
5376 }
ba96a88f
NC
5377}
5378
99e4ae17 5379static enum elf_reloc_type_class
57e8b36a 5380elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
99e4ae17 5381{
f51e552e 5382 switch ((int) ELF32_R_TYPE (rela->r_info))
99e4ae17
AJ
5383 {
5384 case R_ARM_RELATIVE:
5385 return reloc_class_relative;
5386 case R_ARM_JUMP_SLOT:
5387 return reloc_class_plt;
5388 case R_ARM_COPY:
5389 return reloc_class_copy;
5390 default:
5391 return reloc_class_normal;
5392 }
5393}
5394
e16bb312
NC
5395/* Set the right machine number for an Arm ELF file. */
5396
5397static bfd_boolean
57e8b36a 5398elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
e16bb312
NC
5399{
5400 if (hdr->sh_type == SHT_NOTE)
5401 *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
5402
5403 return TRUE;
5404}
5405
e489d0ae 5406static void
57e8b36a 5407elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
e16bb312 5408{
5a6c6817 5409 bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
e16bb312
NC
5410}
5411
40a18ebd
NC
5412/* Return TRUE if this is an unwinding table entry. */
5413
5414static bfd_boolean
5415is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
5416{
5417 size_t len1, len2;
5418
5419 len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
5420 len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
5421 return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
5422 || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
5423}
5424
5425
5426/* Set the type and flags for an ARM section. We do this by
5427 the section name, which is a hack, but ought to work. */
5428
5429static bfd_boolean
5430elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
5431{
5432 const char * name;
5433
5434 name = bfd_get_section_name (abfd, sec);
5435
5436 if (is_arm_elf_unwind_section_name (abfd, name))
5437 {
5438 hdr->sh_type = SHT_ARM_EXIDX;
5439 hdr->sh_flags |= SHF_LINK_ORDER;
5440 }
5441 return TRUE;
5442}
5443
6dc132d9
L
5444/* Handle an ARM specific section when reading an object file. This is
5445 called when bfd_section_from_shdr finds a section with an unknown
5446 type. */
40a18ebd
NC
5447
5448static bfd_boolean
5449elf32_arm_section_from_shdr (bfd *abfd,
5450 Elf_Internal_Shdr * hdr,
6dc132d9
L
5451 const char *name,
5452 int shindex)
40a18ebd
NC
5453{
5454 /* There ought to be a place to keep ELF backend specific flags, but
5455 at the moment there isn't one. We just keep track of the
5456 sections by their name, instead. Fortunately, the ABI gives
5457 names for all the ARM specific sections, so we will probably get
5458 away with this. */
5459 switch (hdr->sh_type)
5460 {
5461 case SHT_ARM_EXIDX:
5462 break;
5463
5464 default:
5465 return FALSE;
5466 }
5467
6dc132d9 5468 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
40a18ebd
NC
5469 return FALSE;
5470
5471 return TRUE;
5472}
e489d0ae
PB
5473
5474/* Called for each symbol. Builds a section map based on mapping symbols.
5475 Does not alter any of the symbols. */
5476
5477static bfd_boolean
5478elf32_arm_output_symbol_hook (struct bfd_link_info *info,
5479 const char *name,
5480 Elf_Internal_Sym *elfsym,
5481 asection *input_sec,
5482 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5483{
5484 int mapcount;
5485 elf32_arm_section_map *map;
5486 struct elf32_arm_link_hash_table *globals;
5487
5488 /* Only do this on final link. */
5489 if (info->relocatable)
5490 return TRUE;
5491
5492 /* Only build a map if we need to byteswap code. */
5493 globals = elf32_arm_hash_table (info);
5494 if (!globals->byteswap_code)
5495 return TRUE;
5496
5497 /* We only want mapping symbols. */
0367ecfb 5498 if (! is_arm_mapping_symbol_name (name))
e489d0ae
PB
5499 return TRUE;
5500
5501 mapcount = ++(elf32_arm_section_data (input_sec)->mapcount);
5502 map = elf32_arm_section_data (input_sec)->map;
5503 /* TODO: This may be inefficient, but we probably don't usually have many
5504 mapping symbols per section. */
5505 map = bfd_realloc (map, mapcount * sizeof (elf32_arm_section_map));
5506 elf32_arm_section_data (input_sec)->map = map;
57e8b36a 5507
e489d0ae
PB
5508 map[mapcount - 1].vma = elfsym->st_value;
5509 map[mapcount - 1].type = name[1];
5510 return TRUE;
5511}
5512
5513
5514/* Allocate target specific section data. */
5515
5516static bfd_boolean
5517elf32_arm_new_section_hook (bfd *abfd, asection *sec)
5518{
5519 struct _arm_elf_section_data *sdata;
5520 bfd_size_type amt = sizeof (*sdata);
5521
5522 sdata = bfd_zalloc (abfd, amt);
5523 if (sdata == NULL)
5524 return FALSE;
5525 sec->used_by_bfd = sdata;
5526
5527 return _bfd_elf_new_section_hook (abfd, sec);
5528}
5529
5530
5531/* Used to order a list of mapping symbols by address. */
5532
5533static int
5534elf32_arm_compare_mapping (const void * a, const void * b)
5535{
5536 return ((const elf32_arm_section_map *) a)->vma
5537 > ((const elf32_arm_section_map *) b)->vma;
5538}
5539
5540
5541/* Do code byteswapping. Return FALSE afterwards so that the section is
5542 written out as normal. */
5543
5544static bfd_boolean
5545elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
5546 bfd_byte *contents)
5547{
5548 int mapcount;
5549 elf32_arm_section_map *map;
5550 bfd_vma ptr;
5551 bfd_vma end;
5552 bfd_vma offset;
5553 bfd_byte tmp;
5554 int i;
57e8b36a 5555
e489d0ae
PB
5556 mapcount = elf32_arm_section_data (sec)->mapcount;
5557 map = elf32_arm_section_data (sec)->map;
5558
5559 if (mapcount == 0)
5560 return FALSE;
5561
5562 qsort (map, mapcount, sizeof (elf32_arm_section_map),
5563 elf32_arm_compare_mapping);
5564
5565 offset = sec->output_section->vma + sec->output_offset;
5566 ptr = map[0].vma - offset;
5567 for (i = 0; i < mapcount; i++)
5568 {
5569 if (i == mapcount - 1)
eea6121a 5570 end = sec->size;
e489d0ae
PB
5571 else
5572 end = map[i + 1].vma - offset;
57e8b36a 5573
e489d0ae
PB
5574 switch (map[i].type)
5575 {
5576 case 'a':
5577 /* Byte swap code words. */
5578 while (ptr + 3 < end)
5579 {
5580 tmp = contents[ptr];
5581 contents[ptr] = contents[ptr + 3];
5582 contents[ptr + 3] = tmp;
5583 tmp = contents[ptr + 1];
5584 contents[ptr + 1] = contents[ptr + 2];
5585 contents[ptr + 2] = tmp;
5586 ptr += 4;
5587 }
5588 break;
5589
5590 case 't':
5591 /* Byte swap code halfwords. */
5592 while (ptr + 1 < end)
5593 {
5594 tmp = contents[ptr];
5595 contents[ptr] = contents[ptr + 1];
5596 contents[ptr + 1] = tmp;
5597 ptr += 2;
5598 }
5599 break;
5600
5601 case 'd':
5602 /* Leave data alone. */
5603 break;
5604 }
5605 ptr = end;
5606 }
93204d3a 5607 free (map);
e489d0ae
PB
5608 return FALSE;
5609}
5610
b7693d02
DJ
5611/* Display STT_ARM_TFUNC symbols as functions. */
5612
5613static void
5614elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5615 asymbol *asym)
5616{
5617 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5618
5619 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
5620 elfsym->symbol.flags |= BSF_FUNCTION;
5621}
5622
0beaef2b
PB
5623
5624/* Mangle thumb function symbols as we read them in. */
5625
5626static void
5627elf32_arm_swap_symbol_in (bfd * abfd,
5628 const void *psrc,
5629 const void *pshn,
5630 Elf_Internal_Sym *dst)
5631{
5632 bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst);
5633
5634 /* New EABI objects mark thumb function symbols by setting the low bit of
5635 the address. Turn these into STT_ARM_TFUNC. */
5636 if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
5637 && (dst->st_value & 1))
5638 {
5639 dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_ARM_TFUNC);
5640 dst->st_value &= ~(bfd_vma) 1;
5641 }
5642}
5643
5644
5645/* Mangle thumb function symbols as we write them out. */
5646
5647static void
5648elf32_arm_swap_symbol_out (bfd *abfd,
5649 const Elf_Internal_Sym *src,
5650 void *cdst,
5651 void *shndx)
5652{
5653 Elf_Internal_Sym newsym;
5654
5655 /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
5656 of the address set, as per the new EABI. We do this unconditionally
5657 because objcopy does not set the elf header flags until after
5658 it writes out the symbol table. */
5659 if (ELF_ST_TYPE (src->st_info) == STT_ARM_TFUNC)
5660 {
5661 newsym = *src;
5662 newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
5663 newsym.st_value |= 1;
5664
5665 src = &newsym;
5666 }
5667 bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
5668}
5669
b294bdf8
MM
5670/* Add the PT_ARM_EXIDX program header. */
5671
5672static bfd_boolean
5673elf32_arm_modify_segment_map (bfd *abfd,
5674 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5675{
5676 struct elf_segment_map *m;
5677 asection *sec;
5678
5679 sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
5680 if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
5681 {
5682 /* If there is already a PT_ARM_EXIDX header, then we do not
5683 want to add another one. This situation arises when running
5684 "strip"; the input binary already has the header. */
5685 m = elf_tdata (abfd)->segment_map;
5686 while (m && m->p_type != PT_ARM_EXIDX)
5687 m = m->next;
5688 if (!m)
5689 {
5690 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
5691 if (m == NULL)
5692 return FALSE;
5693 m->p_type = PT_ARM_EXIDX;
5694 m->count = 1;
5695 m->sections[0] = sec;
5696
5697 m->next = elf_tdata (abfd)->segment_map;
5698 elf_tdata (abfd)->segment_map = m;
5699 }
5700 }
5701
5702 return TRUE;
5703}
5704
5705/* We may add a PT_ARM_EXIDX program header. */
5706
5707static int
5708elf32_arm_additional_program_headers (bfd *abfd)
5709{
5710 asection *sec;
5711
5712 sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
5713 if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
5714 return 1;
5715 else
5716 return 0;
5717}
5718
0beaef2b
PB
5719/* We use this to override swap_symbol_in and swap_symbol_out. */
5720const struct elf_size_info elf32_arm_size_info = {
5721 sizeof (Elf32_External_Ehdr),
5722 sizeof (Elf32_External_Phdr),
5723 sizeof (Elf32_External_Shdr),
5724 sizeof (Elf32_External_Rel),
5725 sizeof (Elf32_External_Rela),
5726 sizeof (Elf32_External_Sym),
5727 sizeof (Elf32_External_Dyn),
5728 sizeof (Elf_External_Note),
5729 4,
5730 1,
5731 32, 2,
5732 ELFCLASS32, EV_CURRENT,
5733 bfd_elf32_write_out_phdrs,
5734 bfd_elf32_write_shdrs_and_ehdr,
5735 bfd_elf32_write_relocs,
5736 elf32_arm_swap_symbol_in,
5737 elf32_arm_swap_symbol_out,
5738 bfd_elf32_slurp_reloc_table,
5739 bfd_elf32_slurp_symbol_table,
5740 bfd_elf32_swap_dyn_in,
5741 bfd_elf32_swap_dyn_out,
5742 bfd_elf32_swap_reloc_in,
5743 bfd_elf32_swap_reloc_out,
5744 bfd_elf32_swap_reloca_in,
5745 bfd_elf32_swap_reloca_out
5746};
5747
252b5132
RH
5748#define ELF_ARCH bfd_arch_arm
5749#define ELF_MACHINE_CODE EM_ARM
d0facd1b
NC
5750#ifdef __QNXTARGET__
5751#define ELF_MAXPAGESIZE 0x1000
5752#else
f21f3fe0 5753#define ELF_MAXPAGESIZE 0x8000
d0facd1b 5754#endif
b1342370 5755#define ELF_MINPAGESIZE 0x1000
252b5132 5756
99e4ae17
AJ
5757#define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
5758#define bfd_elf32_bfd_merge_private_bfd_data elf32_arm_merge_private_bfd_data
252b5132
RH
5759#define bfd_elf32_bfd_set_private_flags elf32_arm_set_private_flags
5760#define bfd_elf32_bfd_print_private_bfd_data elf32_arm_print_private_bfd_data
5761#define bfd_elf32_bfd_link_hash_table_create elf32_arm_link_hash_table_create
dc810e39 5762#define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup
252b5132 5763#define bfd_elf32_find_nearest_line elf32_arm_find_nearest_line
e489d0ae 5764#define bfd_elf32_new_section_hook elf32_arm_new_section_hook
3c9458e9 5765#define bfd_elf32_bfd_is_target_special_symbol elf32_arm_is_target_special_symbol
252b5132
RH
5766
5767#define elf_backend_get_symbol_type elf32_arm_get_symbol_type
5768#define elf_backend_gc_mark_hook elf32_arm_gc_mark_hook
5769#define elf_backend_gc_sweep_hook elf32_arm_gc_sweep_hook
5770#define elf_backend_check_relocs elf32_arm_check_relocs
dc810e39 5771#define elf_backend_relocate_section elf32_arm_relocate_section
e489d0ae 5772#define elf_backend_write_section elf32_arm_write_section
252b5132 5773#define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
5e681ec4 5774#define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
252b5132
RH
5775#define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
5776#define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
e489d0ae 5777#define elf_backend_link_output_symbol_hook elf32_arm_output_symbol_hook
252b5132 5778#define elf_backend_size_dynamic_sections elf32_arm_size_dynamic_sections
ba96a88f 5779#define elf_backend_post_process_headers elf32_arm_post_process_headers
99e4ae17 5780#define elf_backend_reloc_type_class elf32_arm_reloc_type_class
c178919b 5781#define elf_backend_object_p elf32_arm_object_p
e16bb312 5782#define elf_backend_section_flags elf32_arm_section_flags
40a18ebd
NC
5783#define elf_backend_fake_sections elf32_arm_fake_sections
5784#define elf_backend_section_from_shdr elf32_arm_section_from_shdr
e16bb312 5785#define elf_backend_final_write_processing elf32_arm_final_write_processing
5e681ec4 5786#define elf_backend_copy_indirect_symbol elf32_arm_copy_indirect_symbol
b7693d02 5787#define elf_backend_symbol_processing elf32_arm_symbol_processing
0beaef2b 5788#define elf_backend_size_info elf32_arm_size_info
b294bdf8
MM
5789#define elf_backend_modify_segment_map elf32_arm_modify_segment_map
5790#define elf_backend_additional_program_headers \
5791 elf32_arm_additional_program_headers
252b5132 5792
5e681ec4 5793#define elf_backend_can_refcount 1
252b5132
RH
5794#define elf_backend_can_gc_sections 1
5795#define elf_backend_plt_readonly 1
5796#define elf_backend_want_got_plt 1
5797#define elf_backend_want_plt_sym 0
4e7fd91e
PB
5798#define elf_backend_may_use_rel_p 1
5799#define elf_backend_may_use_rela_p 0
5800#define elf_backend_default_use_rela_p 0
5801#define elf_backend_rela_normal 0
252b5132 5802
04f7c78d 5803#define elf_backend_got_header_size 12
04f7c78d 5804
252b5132 5805#include "elf32-target.h"
7f266840 5806
4e7fd91e
PB
5807/* VxWorks Targets */
5808
5809#undef TARGET_LITTLE_SYM
5810#define TARGET_LITTLE_SYM bfd_elf32_littlearm_vxworks_vec
5811#undef TARGET_LITTLE_NAME
5812#define TARGET_LITTLE_NAME "elf32-littlearm-vxworks"
5813#undef TARGET_BIG_SYM
5814#define TARGET_BIG_SYM bfd_elf32_bigarm_vxworks_vec
5815#undef TARGET_BIG_NAME
5816#define TARGET_BIG_NAME "elf32-bigarm-vxworks"
5817
5818/* Like elf32_arm_link_hash_table_create -- but overrides
5819 appropriately for VxWorks. */
5820static struct bfd_link_hash_table *
5821elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
5822{
5823 struct bfd_link_hash_table *ret;
5824
5825 ret = elf32_arm_link_hash_table_create (abfd);
5826 if (ret)
5827 {
5828 struct elf32_arm_link_hash_table *htab
5829 = (struct elf32_arm_link_hash_table *)ret;
5830 htab->use_rel = 0;
5831 }
5832 return ret;
5833}
5834
5835#undef elf32_bed
5836#define elf32_bed elf32_arm_vxworks_bed
5837
5838#undef bfd_elf32_bfd_link_hash_table_create
5839#define bfd_elf32_bfd_link_hash_table_create \
5840 elf32_arm_vxworks_link_hash_table_create
5841
5842#undef elf_backend_may_use_rel_p
5843#define elf_backend_may_use_rel_p 0
5844#undef elf_backend_may_use_rela_p
5845#define elf_backend_may_use_rela_p 1
5846#undef elf_backend_default_use_rela_p
5847#define elf_backend_default_use_rela_p 1
5848#undef elf_backend_rela_normal
5849#define elf_backend_rela_normal 1
5850
5851#include "elf32-target.h"
5852
5853
7f266840
DJ
5854/* Symbian OS Targets */
5855
5856#undef TARGET_LITTLE_SYM
5857#define TARGET_LITTLE_SYM bfd_elf32_littlearm_symbian_vec
5858#undef TARGET_LITTLE_NAME
5859#define TARGET_LITTLE_NAME "elf32-littlearm-symbian"
5860#undef TARGET_BIG_SYM
5861#define TARGET_BIG_SYM bfd_elf32_bigarm_symbian_vec
5862#undef TARGET_BIG_NAME
5863#define TARGET_BIG_NAME "elf32-bigarm-symbian"
5864
5865/* Like elf32_arm_link_hash_table_create -- but overrides
5866 appropriately for Symbian OS. */
5867static struct bfd_link_hash_table *
5868elf32_arm_symbian_link_hash_table_create (bfd *abfd)
5869{
5870 struct bfd_link_hash_table *ret;
5871
5872 ret = elf32_arm_link_hash_table_create (abfd);
5873 if (ret)
5874 {
5875 struct elf32_arm_link_hash_table *htab
5876 = (struct elf32_arm_link_hash_table *)ret;
5877 /* There is no PLT header for Symbian OS. */
5878 htab->plt_header_size = 0;
5879 /* The PLT entries are each three instructions. */
5880 htab->plt_entry_size = 4 * NUM_ELEM (elf32_arm_symbian_plt_entry);
5881 htab->symbian_p = 1;
67687978 5882 htab->root.is_relocatable_executable = 1;
7f266840
DJ
5883 }
5884 return ret;
5885}
5886
7f266840
DJ
5887static struct bfd_elf_special_section const
5888 elf32_arm_symbian_special_sections[]=
5889{
5cd3778d
MM
5890 /* In a BPABI executable, the dynamic linking sections do not go in
5891 the loadable read-only segment. The post-linker may wish to
5892 refer to these sections, but they are not part of the final
5893 program image. */
7f266840
DJ
5894 { ".dynamic", 8, 0, SHT_DYNAMIC, 0 },
5895 { ".dynstr", 7, 0, SHT_STRTAB, 0 },
5896 { ".dynsym", 7, 0, SHT_DYNSYM, 0 },
5897 { ".got", 4, 0, SHT_PROGBITS, 0 },
5898 { ".hash", 5, 0, SHT_HASH, 0 },
5cd3778d
MM
5899 /* These sections do not need to be writable as the SymbianOS
5900 postlinker will arrange things so that no dynamic relocation is
5901 required. */
5902 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC },
5903 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC },
5904 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
7f266840
DJ
5905 { NULL, 0, 0, 0, 0 }
5906};
5907
c3c76620 5908static void
b34af79c
MM
5909elf32_arm_symbian_begin_write_processing (bfd *abfd,
5910 struct bfd_link_info *link_info
5911 ATTRIBUTE_UNUSED)
c3c76620
MM
5912{
5913 /* BPABI objects are never loaded directly by an OS kernel; they are
5914 processed by a postlinker first, into an OS-specific format. If
5915 the D_PAGED bit is set on the file, BFD will align segments on
5916 page boundaries, so that an OS can directly map the file. With
5917 BPABI objects, that just results in wasted space. In addition,
5918 because we clear the D_PAGED bit, map_sections_to_segments will
5919 recognize that the program headers should not be mapped into any
5920 loadable segment. */
5921 abfd->flags &= ~D_PAGED;
5922}
7f266840
DJ
5923
5924static bfd_boolean
b34af79c 5925elf32_arm_symbian_modify_segment_map (bfd *abfd,
b294bdf8 5926 struct bfd_link_info *info)
7f266840
DJ
5927{
5928 struct elf_segment_map *m;
5929 asection *dynsec;
5930
7f266840
DJ
5931 /* BPABI shared libraries and executables should have a PT_DYNAMIC
5932 segment. However, because the .dynamic section is not marked
5933 with SEC_LOAD, the generic ELF code will not create such a
5934 segment. */
5935 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5936 if (dynsec)
5937 {
5938 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5939 m->next = elf_tdata (abfd)->segment_map;
5940 elf_tdata (abfd)->segment_map = m;
5941 }
5942
b294bdf8
MM
5943 /* Also call the generic arm routine. */
5944 return elf32_arm_modify_segment_map (abfd, info);
7f266840
DJ
5945}
5946
5947#undef elf32_bed
5948#define elf32_bed elf32_arm_symbian_bed
5949
5950/* The dynamic sections are not allocated on SymbianOS; the postlinker
5951 will process them and then discard them. */
5952#undef ELF_DYNAMIC_SEC_FLAGS
5953#define ELF_DYNAMIC_SEC_FLAGS \
5954 (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
5955
5956#undef bfd_elf32_bfd_link_hash_table_create
5957#define bfd_elf32_bfd_link_hash_table_create \
5958 elf32_arm_symbian_link_hash_table_create
5959
5960#undef elf_backend_special_sections
5961#define elf_backend_special_sections elf32_arm_symbian_special_sections
5962
c3c76620
MM
5963#undef elf_backend_begin_write_processing
5964#define elf_backend_begin_write_processing \
5965 elf32_arm_symbian_begin_write_processing
5966
7f266840
DJ
5967#undef elf_backend_modify_segment_map
5968#define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
5969
5970/* There is no .got section for BPABI objects, and hence no header. */
5971#undef elf_backend_got_header_size
5972#define elf_backend_got_header_size 0
5973
5974/* Similarly, there is no .got.plt section. */
5975#undef elf_backend_want_got_plt
5976#define elf_backend_want_got_plt 0
5977
4e7fd91e
PB
5978#undef elf_backend_may_use_rel_p
5979#define elf_backend_may_use_rel_p 1
5980#undef elf_backend_may_use_rela_p
5981#define elf_backend_may_use_rela_p 0
5982#undef elf_backend_default_use_rela_p
dc4c9c19 5983#define elf_backend_default_use_rela_p 0
4e7fd91e
PB
5984#undef elf_backend_rela_normal
5985#define elf_backend_rela_normal 0
5986
7f266840 5987#include "elf32-target.h"
This page took 0.747034 seconds and 4 git commands to generate.