* opcode/arc-opc.c (insert_base): Modify ls_operand[LS_OFFSET] to reflect the
[binutils.git] / bfd / elf32-arm.c
blobb3edf6d5b83e8483443ceab0312f91b4d6dd95ba
1 /* 32-bit ELF support for ARM
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "elf/arm.h"
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
27 #ifndef NUM_ELEM
28 #define NUM_ELEM(a) (sizeof (a) / (sizeof (a)[0]))
29 #endif
31 #define USE_REL 1
33 #define elf_info_to_howto 0
34 #define elf_info_to_howto_rel elf32_arm_info_to_howto
36 #define ARM_ELF_ABI_VERSION 0
37 #define ARM_ELF_OS_ABI_VERSION ELFOSABI_ARM
39 static reloc_howto_type * elf32_arm_reloc_type_lookup
40 PARAMS ((bfd * abfd, bfd_reloc_code_real_type code));
41 static bfd_boolean elf32_arm_nabi_grok_prstatus
42 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
43 static bfd_boolean elf32_arm_nabi_grok_psinfo
44 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
46 /* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
47 R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
48 in that slot. */
50 static reloc_howto_type elf32_arm_howto_table[] =
52 /* No relocation */
53 HOWTO (R_ARM_NONE, /* type */
54 0, /* rightshift */
55 0, /* size (0 = byte, 1 = short, 2 = long) */
56 0, /* bitsize */
57 FALSE, /* pc_relative */
58 0, /* bitpos */
59 complain_overflow_dont,/* complain_on_overflow */
60 bfd_elf_generic_reloc, /* special_function */
61 "R_ARM_NONE", /* name */
62 FALSE, /* partial_inplace */
63 0, /* src_mask */
64 0, /* dst_mask */
65 FALSE), /* pcrel_offset */
67 HOWTO (R_ARM_PC24, /* type */
68 2, /* rightshift */
69 2, /* size (0 = byte, 1 = short, 2 = long) */
70 24, /* bitsize */
71 TRUE, /* pc_relative */
72 0, /* bitpos */
73 complain_overflow_signed,/* complain_on_overflow */
74 bfd_elf_generic_reloc, /* special_function */
75 "R_ARM_PC24", /* name */
76 FALSE, /* partial_inplace */
77 0x00ffffff, /* src_mask */
78 0x00ffffff, /* dst_mask */
79 TRUE), /* pcrel_offset */
81 /* 32 bit absolute */
82 HOWTO (R_ARM_ABS32, /* type */
83 0, /* rightshift */
84 2, /* size (0 = byte, 1 = short, 2 = long) */
85 32, /* bitsize */
86 FALSE, /* pc_relative */
87 0, /* bitpos */
88 complain_overflow_bitfield,/* complain_on_overflow */
89 bfd_elf_generic_reloc, /* special_function */
90 "R_ARM_ABS32", /* name */
91 FALSE, /* partial_inplace */
92 0xffffffff, /* src_mask */
93 0xffffffff, /* dst_mask */
94 FALSE), /* pcrel_offset */
96 /* standard 32bit pc-relative reloc */
97 HOWTO (R_ARM_REL32, /* type */
98 0, /* rightshift */
99 2, /* size (0 = byte, 1 = short, 2 = long) */
100 32, /* bitsize */
101 TRUE, /* pc_relative */
102 0, /* bitpos */
103 complain_overflow_bitfield,/* complain_on_overflow */
104 bfd_elf_generic_reloc, /* special_function */
105 "R_ARM_REL32", /* name */
106 FALSE, /* partial_inplace */
107 0xffffffff, /* src_mask */
108 0xffffffff, /* dst_mask */
109 TRUE), /* pcrel_offset */
111 /* 8 bit absolute */
112 HOWTO (R_ARM_PC13, /* type */
113 0, /* rightshift */
114 0, /* size (0 = byte, 1 = short, 2 = long) */
115 8, /* bitsize */
116 FALSE, /* pc_relative */
117 0, /* bitpos */
118 complain_overflow_bitfield,/* complain_on_overflow */
119 bfd_elf_generic_reloc, /* special_function */
120 "R_ARM_PC13", /* name */
121 FALSE, /* partial_inplace */
122 0x000000ff, /* src_mask */
123 0x000000ff, /* dst_mask */
124 FALSE), /* pcrel_offset */
126 /* 16 bit absolute */
127 HOWTO (R_ARM_ABS16, /* type */
128 0, /* rightshift */
129 1, /* size (0 = byte, 1 = short, 2 = long) */
130 16, /* bitsize */
131 FALSE, /* pc_relative */
132 0, /* bitpos */
133 complain_overflow_bitfield,/* complain_on_overflow */
134 bfd_elf_generic_reloc, /* special_function */
135 "R_ARM_ABS16", /* name */
136 FALSE, /* partial_inplace */
137 0x0000ffff, /* src_mask */
138 0x0000ffff, /* dst_mask */
139 FALSE), /* pcrel_offset */
141 /* 12 bit absolute */
142 HOWTO (R_ARM_ABS12, /* type */
143 0, /* rightshift */
144 2, /* size (0 = byte, 1 = short, 2 = long) */
145 12, /* bitsize */
146 FALSE, /* pc_relative */
147 0, /* bitpos */
148 complain_overflow_bitfield,/* complain_on_overflow */
149 bfd_elf_generic_reloc, /* special_function */
150 "R_ARM_ABS12", /* name */
151 FALSE, /* partial_inplace */
152 0x000008ff, /* src_mask */
153 0x000008ff, /* dst_mask */
154 FALSE), /* pcrel_offset */
156 HOWTO (R_ARM_THM_ABS5, /* type */
157 6, /* rightshift */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
159 5, /* bitsize */
160 FALSE, /* pc_relative */
161 0, /* bitpos */
162 complain_overflow_bitfield,/* complain_on_overflow */
163 bfd_elf_generic_reloc, /* special_function */
164 "R_ARM_THM_ABS5", /* name */
165 FALSE, /* partial_inplace */
166 0x000007e0, /* src_mask */
167 0x000007e0, /* dst_mask */
168 FALSE), /* pcrel_offset */
170 /* 8 bit absolute */
171 HOWTO (R_ARM_ABS8, /* type */
172 0, /* rightshift */
173 0, /* size (0 = byte, 1 = short, 2 = long) */
174 8, /* bitsize */
175 FALSE, /* pc_relative */
176 0, /* bitpos */
177 complain_overflow_bitfield,/* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_ARM_ABS8", /* name */
180 FALSE, /* partial_inplace */
181 0x000000ff, /* src_mask */
182 0x000000ff, /* dst_mask */
183 FALSE), /* pcrel_offset */
185 HOWTO (R_ARM_SBREL32, /* type */
186 0, /* rightshift */
187 2, /* size (0 = byte, 1 = short, 2 = long) */
188 32, /* bitsize */
189 FALSE, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_dont,/* complain_on_overflow */
192 bfd_elf_generic_reloc, /* special_function */
193 "R_ARM_SBREL32", /* name */
194 FALSE, /* partial_inplace */
195 0xffffffff, /* src_mask */
196 0xffffffff, /* dst_mask */
197 FALSE), /* pcrel_offset */
199 HOWTO (R_ARM_THM_PC22, /* type */
200 1, /* rightshift */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
202 23, /* bitsize */
203 TRUE, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_signed,/* complain_on_overflow */
206 bfd_elf_generic_reloc, /* special_function */
207 "R_ARM_THM_PC22", /* name */
208 FALSE, /* partial_inplace */
209 0x07ff07ff, /* src_mask */
210 0x07ff07ff, /* dst_mask */
211 TRUE), /* pcrel_offset */
213 HOWTO (R_ARM_THM_PC8, /* type */
214 1, /* rightshift */
215 1, /* size (0 = byte, 1 = short, 2 = long) */
216 8, /* bitsize */
217 TRUE, /* pc_relative */
218 0, /* bitpos */
219 complain_overflow_signed,/* complain_on_overflow */
220 bfd_elf_generic_reloc, /* special_function */
221 "R_ARM_THM_PC8", /* name */
222 FALSE, /* partial_inplace */
223 0x000000ff, /* src_mask */
224 0x000000ff, /* dst_mask */
225 TRUE), /* pcrel_offset */
227 HOWTO (R_ARM_AMP_VCALL9, /* type */
228 1, /* rightshift */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
230 8, /* bitsize */
231 TRUE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_signed,/* complain_on_overflow */
234 bfd_elf_generic_reloc, /* special_function */
235 "R_ARM_AMP_VCALL9", /* name */
236 FALSE, /* partial_inplace */
237 0x000000ff, /* src_mask */
238 0x000000ff, /* dst_mask */
239 TRUE), /* pcrel_offset */
241 HOWTO (R_ARM_SWI24, /* type */
242 0, /* rightshift */
243 0, /* size (0 = byte, 1 = short, 2 = long) */
244 0, /* bitsize */
245 FALSE, /* pc_relative */
246 0, /* bitpos */
247 complain_overflow_signed,/* complain_on_overflow */
248 bfd_elf_generic_reloc, /* special_function */
249 "R_ARM_SWI24", /* name */
250 FALSE, /* partial_inplace */
251 0x00000000, /* src_mask */
252 0x00000000, /* dst_mask */
253 FALSE), /* pcrel_offset */
255 HOWTO (R_ARM_THM_SWI8, /* type */
256 0, /* rightshift */
257 0, /* size (0 = byte, 1 = short, 2 = long) */
258 0, /* bitsize */
259 FALSE, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_signed,/* complain_on_overflow */
262 bfd_elf_generic_reloc, /* special_function */
263 "R_ARM_SWI8", /* name */
264 FALSE, /* partial_inplace */
265 0x00000000, /* src_mask */
266 0x00000000, /* dst_mask */
267 FALSE), /* pcrel_offset */
269 /* BLX instruction for the ARM. */
270 HOWTO (R_ARM_XPC25, /* type */
271 2, /* rightshift */
272 2, /* size (0 = byte, 1 = short, 2 = long) */
273 25, /* bitsize */
274 TRUE, /* pc_relative */
275 0, /* bitpos */
276 complain_overflow_signed,/* complain_on_overflow */
277 bfd_elf_generic_reloc, /* special_function */
278 "R_ARM_XPC25", /* name */
279 FALSE, /* partial_inplace */
280 0x00ffffff, /* src_mask */
281 0x00ffffff, /* dst_mask */
282 TRUE), /* pcrel_offset */
284 /* BLX instruction for the Thumb. */
285 HOWTO (R_ARM_THM_XPC22, /* type */
286 2, /* rightshift */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
288 22, /* bitsize */
289 TRUE, /* pc_relative */
290 0, /* bitpos */
291 complain_overflow_signed,/* complain_on_overflow */
292 bfd_elf_generic_reloc, /* special_function */
293 "R_ARM_THM_XPC22", /* name */
294 FALSE, /* partial_inplace */
295 0x07ff07ff, /* src_mask */
296 0x07ff07ff, /* dst_mask */
297 TRUE), /* pcrel_offset */
299 /* These next three relocs are not defined, but we need to fill the space. */
301 HOWTO (R_ARM_NONE, /* type */
302 0, /* rightshift */
303 0, /* size (0 = byte, 1 = short, 2 = long) */
304 0, /* bitsize */
305 FALSE, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_dont,/* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_ARM_unknown_17", /* name */
310 FALSE, /* partial_inplace */
311 0, /* src_mask */
312 0, /* dst_mask */
313 FALSE), /* pcrel_offset */
315 HOWTO (R_ARM_NONE, /* type */
316 0, /* rightshift */
317 0, /* size (0 = byte, 1 = short, 2 = long) */
318 0, /* bitsize */
319 FALSE, /* pc_relative */
320 0, /* bitpos */
321 complain_overflow_dont,/* complain_on_overflow */
322 bfd_elf_generic_reloc, /* special_function */
323 "R_ARM_unknown_18", /* name */
324 FALSE, /* partial_inplace */
325 0, /* src_mask */
326 0, /* dst_mask */
327 FALSE), /* pcrel_offset */
329 HOWTO (R_ARM_NONE, /* type */
330 0, /* rightshift */
331 0, /* size (0 = byte, 1 = short, 2 = long) */
332 0, /* bitsize */
333 FALSE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_dont,/* complain_on_overflow */
336 bfd_elf_generic_reloc, /* special_function */
337 "R_ARM_unknown_19", /* name */
338 FALSE, /* partial_inplace */
339 0, /* src_mask */
340 0, /* dst_mask */
341 FALSE), /* pcrel_offset */
343 /* Relocs used in ARM Linux */
345 HOWTO (R_ARM_COPY, /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 32, /* bitsize */
349 FALSE, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_bitfield,/* complain_on_overflow */
352 bfd_elf_generic_reloc, /* special_function */
353 "R_ARM_COPY", /* name */
354 TRUE, /* partial_inplace */
355 0xffffffff, /* src_mask */
356 0xffffffff, /* dst_mask */
357 FALSE), /* pcrel_offset */
359 HOWTO (R_ARM_GLOB_DAT, /* type */
360 0, /* rightshift */
361 2, /* size (0 = byte, 1 = short, 2 = long) */
362 32, /* bitsize */
363 FALSE, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_bitfield,/* complain_on_overflow */
366 bfd_elf_generic_reloc, /* special_function */
367 "R_ARM_GLOB_DAT", /* name */
368 TRUE, /* partial_inplace */
369 0xffffffff, /* src_mask */
370 0xffffffff, /* dst_mask */
371 FALSE), /* pcrel_offset */
373 HOWTO (R_ARM_JUMP_SLOT, /* type */
374 0, /* rightshift */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
376 32, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_bitfield,/* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_ARM_JUMP_SLOT", /* name */
382 TRUE, /* partial_inplace */
383 0xffffffff, /* src_mask */
384 0xffffffff, /* dst_mask */
385 FALSE), /* pcrel_offset */
387 HOWTO (R_ARM_RELATIVE, /* type */
388 0, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 32, /* bitsize */
391 FALSE, /* pc_relative */
392 0, /* bitpos */
393 complain_overflow_bitfield,/* complain_on_overflow */
394 bfd_elf_generic_reloc, /* special_function */
395 "R_ARM_RELATIVE", /* name */
396 TRUE, /* partial_inplace */
397 0xffffffff, /* src_mask */
398 0xffffffff, /* dst_mask */
399 FALSE), /* pcrel_offset */
401 HOWTO (R_ARM_GOTOFF, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 32, /* bitsize */
405 FALSE, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_bitfield,/* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_ARM_GOTOFF", /* name */
410 TRUE, /* partial_inplace */
411 0xffffffff, /* src_mask */
412 0xffffffff, /* dst_mask */
413 FALSE), /* pcrel_offset */
415 HOWTO (R_ARM_GOTPC, /* type */
416 0, /* rightshift */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
418 32, /* bitsize */
419 TRUE, /* pc_relative */
420 0, /* bitpos */
421 complain_overflow_bitfield,/* complain_on_overflow */
422 bfd_elf_generic_reloc, /* special_function */
423 "R_ARM_GOTPC", /* name */
424 TRUE, /* partial_inplace */
425 0xffffffff, /* src_mask */
426 0xffffffff, /* dst_mask */
427 TRUE), /* pcrel_offset */
429 HOWTO (R_ARM_GOT32, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 32, /* bitsize */
433 FALSE, /* pc_relative */
434 0, /* bitpos */
435 complain_overflow_bitfield,/* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_ARM_GOT32", /* name */
438 TRUE, /* partial_inplace */
439 0xffffffff, /* src_mask */
440 0xffffffff, /* dst_mask */
441 FALSE), /* pcrel_offset */
443 HOWTO (R_ARM_PLT32, /* type */
444 2, /* rightshift */
445 2, /* size (0 = byte, 1 = short, 2 = long) */
446 26, /* bitsize */
447 TRUE, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_bitfield,/* complain_on_overflow */
450 bfd_elf_generic_reloc, /* special_function */
451 "R_ARM_PLT32", /* name */
452 TRUE, /* partial_inplace */
453 0x00ffffff, /* src_mask */
454 0x00ffffff, /* dst_mask */
455 TRUE), /* pcrel_offset */
457 HOWTO (R_ARM_CALL, /* type */
458 2, /* rightshift */
459 2, /* size (0 = byte, 1 = short, 2 = long) */
460 24, /* bitsize */
461 TRUE, /* pc_relative */
462 0, /* bitpos */
463 complain_overflow_signed,/* complain_on_overflow */
464 bfd_elf_generic_reloc, /* special_function */
465 "R_ARM_CALL", /* name */
466 FALSE, /* partial_inplace */
467 0x00ffffff, /* src_mask */
468 0x00ffffff, /* dst_mask */
469 TRUE), /* pcrel_offset */
471 HOWTO (R_ARM_JUMP24, /* type */
472 2, /* rightshift */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
474 24, /* bitsize */
475 TRUE, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_signed,/* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_ARM_JUMP24", /* name */
480 FALSE, /* partial_inplace */
481 0x00ffffff, /* src_mask */
482 0x00ffffff, /* dst_mask */
483 TRUE), /* pcrel_offset */
485 HOWTO (R_ARM_NONE, /* type */
486 0, /* rightshift */
487 0, /* size (0 = byte, 1 = short, 2 = long) */
488 0, /* bitsize */
489 FALSE, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_dont,/* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_ARM_unknown_30", /* name */
494 FALSE, /* partial_inplace */
495 0, /* src_mask */
496 0, /* dst_mask */
497 FALSE), /* pcrel_offset */
499 HOWTO (R_ARM_NONE, /* type */
500 0, /* rightshift */
501 0, /* size (0 = byte, 1 = short, 2 = long) */
502 0, /* bitsize */
503 FALSE, /* pc_relative */
504 0, /* bitpos */
505 complain_overflow_dont,/* complain_on_overflow */
506 bfd_elf_generic_reloc, /* special_function */
507 "R_ARM_unknown_31", /* name */
508 FALSE, /* partial_inplace */
509 0, /* src_mask */
510 0, /* dst_mask */
511 FALSE), /* pcrel_offset */
513 HOWTO (R_ARM_ALU_PCREL7_0, /* type */
514 0, /* rightshift */
515 2, /* size (0 = byte, 1 = short, 2 = long) */
516 12, /* bitsize */
517 TRUE, /* pc_relative */
518 0, /* bitpos */
519 complain_overflow_dont,/* complain_on_overflow */
520 bfd_elf_generic_reloc, /* special_function */
521 "R_ARM_ALU_PCREL_7_0", /* name */
522 FALSE, /* partial_inplace */
523 0x00000fff, /* src_mask */
524 0x00000fff, /* dst_mask */
525 TRUE), /* pcrel_offset */
527 HOWTO (R_ARM_ALU_PCREL15_8, /* type */
528 0, /* rightshift */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
530 12, /* bitsize */
531 TRUE, /* pc_relative */
532 8, /* bitpos */
533 complain_overflow_dont,/* complain_on_overflow */
534 bfd_elf_generic_reloc, /* special_function */
535 "R_ARM_ALU_PCREL_15_8",/* name */
536 FALSE, /* partial_inplace */
537 0x00000fff, /* src_mask */
538 0x00000fff, /* dst_mask */
539 TRUE), /* pcrel_offset */
541 HOWTO (R_ARM_ALU_PCREL23_15, /* type */
542 0, /* rightshift */
543 2, /* size (0 = byte, 1 = short, 2 = long) */
544 12, /* bitsize */
545 TRUE, /* pc_relative */
546 16, /* bitpos */
547 complain_overflow_dont,/* complain_on_overflow */
548 bfd_elf_generic_reloc, /* special_function */
549 "R_ARM_ALU_PCREL_23_15",/* name */
550 FALSE, /* partial_inplace */
551 0x00000fff, /* src_mask */
552 0x00000fff, /* dst_mask */
553 TRUE), /* pcrel_offset */
555 HOWTO (R_ARM_LDR_SBREL_11_0, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 12, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont,/* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_ARM_LDR_SBREL_11_0",/* name */
564 FALSE, /* partial_inplace */
565 0x00000fff, /* src_mask */
566 0x00000fff, /* dst_mask */
567 FALSE), /* pcrel_offset */
569 HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
570 0, /* rightshift */
571 2, /* size (0 = byte, 1 = short, 2 = long) */
572 8, /* bitsize */
573 FALSE, /* pc_relative */
574 12, /* bitpos */
575 complain_overflow_dont,/* complain_on_overflow */
576 bfd_elf_generic_reloc, /* special_function */
577 "R_ARM_ALU_SBREL_19_12",/* name */
578 FALSE, /* partial_inplace */
579 0x000ff000, /* src_mask */
580 0x000ff000, /* dst_mask */
581 FALSE), /* pcrel_offset */
583 HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
584 0, /* rightshift */
585 2, /* size (0 = byte, 1 = short, 2 = long) */
586 8, /* bitsize */
587 FALSE, /* pc_relative */
588 20, /* bitpos */
589 complain_overflow_dont,/* complain_on_overflow */
590 bfd_elf_generic_reloc, /* special_function */
591 "R_ARM_ALU_SBREL_27_20",/* name */
592 FALSE, /* partial_inplace */
593 0x0ff00000, /* src_mask */
594 0x0ff00000, /* dst_mask */
595 FALSE), /* pcrel_offset */
597 HOWTO (R_ARM_TARGET1, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 32, /* bitsize */
601 FALSE, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_dont,/* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_ARM_TARGET1", /* name */
606 FALSE, /* partial_inplace */
607 0xffffffff, /* src_mask */
608 0xffffffff, /* dst_mask */
609 FALSE), /* pcrel_offset */
611 HOWTO (R_ARM_ROSEGREL32, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 32, /* bitsize */
615 FALSE, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_dont,/* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_ARM_ROSEGREL32", /* name */
620 FALSE, /* partial_inplace */
621 0xffffffff, /* src_mask */
622 0xffffffff, /* dst_mask */
623 FALSE), /* pcrel_offset */
625 HOWTO (R_ARM_V4BX, /* type */
626 0, /* rightshift */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
628 32, /* bitsize */
629 FALSE, /* pc_relative */
630 0, /* bitpos */
631 complain_overflow_dont,/* complain_on_overflow */
632 bfd_elf_generic_reloc, /* special_function */
633 "R_ARM_V4BX", /* name */
634 FALSE, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 FALSE), /* pcrel_offset */
639 HOWTO (R_ARM_TARGET2, /* type */
640 0, /* rightshift */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
642 32, /* bitsize */
643 FALSE, /* pc_relative */
644 0, /* bitpos */
645 complain_overflow_signed,/* complain_on_overflow */
646 bfd_elf_generic_reloc, /* special_function */
647 "R_ARM_TARGET2", /* name */
648 FALSE, /* partial_inplace */
649 0xffffffff, /* src_mask */
650 0xffffffff, /* dst_mask */
651 TRUE), /* pcrel_offset */
653 HOWTO (R_ARM_PREL31, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 31, /* bitsize */
657 TRUE, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_signed,/* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_ARM_PREL31", /* name */
662 FALSE, /* partial_inplace */
663 0x7fffffff, /* src_mask */
664 0x7fffffff, /* dst_mask */
665 TRUE), /* pcrel_offset */
668 /* GNU extension to record C++ vtable hierarchy */
669 static reloc_howto_type elf32_arm_vtinherit_howto =
670 HOWTO (R_ARM_GNU_VTINHERIT, /* type */
671 0, /* rightshift */
672 2, /* size (0 = byte, 1 = short, 2 = long) */
673 0, /* bitsize */
674 FALSE, /* pc_relative */
675 0, /* bitpos */
676 complain_overflow_dont, /* complain_on_overflow */
677 NULL, /* special_function */
678 "R_ARM_GNU_VTINHERIT", /* name */
679 FALSE, /* partial_inplace */
680 0, /* src_mask */
681 0, /* dst_mask */
682 FALSE); /* pcrel_offset */
684 /* GNU extension to record C++ vtable member usage */
685 static reloc_howto_type elf32_arm_vtentry_howto =
686 HOWTO (R_ARM_GNU_VTENTRY, /* type */
687 0, /* rightshift */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
689 0, /* bitsize */
690 FALSE, /* pc_relative */
691 0, /* bitpos */
692 complain_overflow_dont, /* complain_on_overflow */
693 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
694 "R_ARM_GNU_VTENTRY", /* name */
695 FALSE, /* partial_inplace */
696 0, /* src_mask */
697 0, /* dst_mask */
698 FALSE); /* pcrel_offset */
700 /* 12 bit pc relative */
701 static reloc_howto_type elf32_arm_thm_pc11_howto =
702 HOWTO (R_ARM_THM_PC11, /* type */
703 1, /* rightshift */
704 1, /* size (0 = byte, 1 = short, 2 = long) */
705 11, /* bitsize */
706 TRUE, /* pc_relative */
707 0, /* bitpos */
708 complain_overflow_signed, /* complain_on_overflow */
709 bfd_elf_generic_reloc, /* special_function */
710 "R_ARM_THM_PC11", /* name */
711 FALSE, /* partial_inplace */
712 0x000007ff, /* src_mask */
713 0x000007ff, /* dst_mask */
714 TRUE); /* pcrel_offset */
716 /* 12 bit pc relative */
717 static reloc_howto_type elf32_arm_thm_pc9_howto =
718 HOWTO (R_ARM_THM_PC9, /* type */
719 1, /* rightshift */
720 1, /* size (0 = byte, 1 = short, 2 = long) */
721 8, /* bitsize */
722 TRUE, /* pc_relative */
723 0, /* bitpos */
724 complain_overflow_signed, /* complain_on_overflow */
725 bfd_elf_generic_reloc, /* special_function */
726 "R_ARM_THM_PC9", /* name */
727 FALSE, /* partial_inplace */
728 0x000000ff, /* src_mask */
729 0x000000ff, /* dst_mask */
730 TRUE); /* pcrel_offset */
732 /* Place relative GOT-indirect. */
733 static reloc_howto_type elf32_arm_got_prel =
734 HOWTO (R_ARM_GOT_PREL, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 32, /* bitsize */
738 TRUE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_dont, /* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_ARM_GOT_PREL", /* name */
743 FALSE, /* partial_inplace */
744 0xffffffff, /* src_mask */
745 0xffffffff, /* dst_mask */
746 TRUE); /* pcrel_offset */
748 /* Currently unused relocations. */
749 static reloc_howto_type elf32_arm_r_howto[4] =
751 HOWTO (R_ARM_RREL32, /* type */
752 0, /* rightshift */
753 0, /* size (0 = byte, 1 = short, 2 = long) */
754 0, /* bitsize */
755 FALSE, /* pc_relative */
756 0, /* bitpos */
757 complain_overflow_dont,/* complain_on_overflow */
758 bfd_elf_generic_reloc, /* special_function */
759 "R_ARM_RREL32", /* name */
760 FALSE, /* partial_inplace */
761 0, /* src_mask */
762 0, /* dst_mask */
763 FALSE), /* pcrel_offset */
765 HOWTO (R_ARM_RABS32, /* type */
766 0, /* rightshift */
767 0, /* size (0 = byte, 1 = short, 2 = long) */
768 0, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_dont,/* complain_on_overflow */
772 bfd_elf_generic_reloc, /* special_function */
773 "R_ARM_RABS32", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0, /* dst_mask */
777 FALSE), /* pcrel_offset */
779 HOWTO (R_ARM_RPC24, /* type */
780 0, /* rightshift */
781 0, /* size (0 = byte, 1 = short, 2 = long) */
782 0, /* bitsize */
783 FALSE, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_dont,/* complain_on_overflow */
786 bfd_elf_generic_reloc, /* special_function */
787 "R_ARM_RPC24", /* name */
788 FALSE, /* partial_inplace */
789 0, /* src_mask */
790 0, /* dst_mask */
791 FALSE), /* pcrel_offset */
793 HOWTO (R_ARM_RBASE, /* type */
794 0, /* rightshift */
795 0, /* size (0 = byte, 1 = short, 2 = long) */
796 0, /* bitsize */
797 FALSE, /* pc_relative */
798 0, /* bitpos */
799 complain_overflow_dont,/* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 "R_ARM_RBASE", /* name */
802 FALSE, /* partial_inplace */
803 0, /* src_mask */
804 0, /* dst_mask */
805 FALSE) /* pcrel_offset */
808 static reloc_howto_type *
809 elf32_arm_howto_from_type (unsigned int r_type)
811 if (r_type < NUM_ELEM (elf32_arm_howto_table))
812 return &elf32_arm_howto_table[r_type];
814 switch (r_type)
816 case R_ARM_GOT_PREL:
817 return &elf32_arm_got_prel;
819 case R_ARM_GNU_VTINHERIT:
820 return &elf32_arm_vtinherit_howto;
822 case R_ARM_GNU_VTENTRY:
823 return &elf32_arm_vtentry_howto;
825 case R_ARM_THM_PC11:
826 return &elf32_arm_thm_pc11_howto;
828 case R_ARM_THM_PC9:
829 return &elf32_arm_thm_pc9_howto;
831 case R_ARM_RREL32:
832 case R_ARM_RABS32:
833 case R_ARM_RPC24:
834 case R_ARM_RBASE:
835 return &elf32_arm_r_howto[r_type - R_ARM_RREL32];
837 default:
838 return NULL;
842 static void
843 elf32_arm_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED, arelent * bfd_reloc,
844 Elf_Internal_Rela * elf_reloc)
846 unsigned int r_type;
848 r_type = ELF32_R_TYPE (elf_reloc->r_info);
849 bfd_reloc->howto = elf32_arm_howto_from_type (r_type);
852 struct elf32_arm_reloc_map
854 bfd_reloc_code_real_type bfd_reloc_val;
855 unsigned char elf_reloc_val;
858 /* All entries in this list must also be present in elf32_arm_howto_table. */
859 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
861 {BFD_RELOC_NONE, R_ARM_NONE},
862 {BFD_RELOC_ARM_PCREL_BRANCH, R_ARM_PC24},
863 {BFD_RELOC_ARM_PCREL_BLX, R_ARM_XPC25},
864 {BFD_RELOC_THUMB_PCREL_BLX, R_ARM_THM_XPC22},
865 {BFD_RELOC_32, R_ARM_ABS32},
866 {BFD_RELOC_32_PCREL, R_ARM_REL32},
867 {BFD_RELOC_8, R_ARM_ABS8},
868 {BFD_RELOC_16, R_ARM_ABS16},
869 {BFD_RELOC_ARM_OFFSET_IMM, R_ARM_ABS12},
870 {BFD_RELOC_ARM_THUMB_OFFSET, R_ARM_THM_ABS5},
871 {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_PC22},
872 {BFD_RELOC_ARM_COPY, R_ARM_COPY},
873 {BFD_RELOC_ARM_GLOB_DAT, R_ARM_GLOB_DAT},
874 {BFD_RELOC_ARM_JUMP_SLOT, R_ARM_JUMP_SLOT},
875 {BFD_RELOC_ARM_RELATIVE, R_ARM_RELATIVE},
876 {BFD_RELOC_ARM_GOTOFF, R_ARM_GOTOFF},
877 {BFD_RELOC_ARM_GOTPC, R_ARM_GOTPC},
878 {BFD_RELOC_ARM_GOT32, R_ARM_GOT32},
879 {BFD_RELOC_ARM_PLT32, R_ARM_PLT32},
880 {BFD_RELOC_ARM_TARGET1, R_ARM_TARGET1},
881 {BFD_RELOC_ARM_ROSEGREL32, R_ARM_ROSEGREL32},
882 {BFD_RELOC_ARM_SBREL32, R_ARM_SBREL32},
883 {BFD_RELOC_ARM_PREL31, R_ARM_PREL31},
884 {BFD_RELOC_ARM_TARGET2, R_ARM_TARGET2}
887 static reloc_howto_type *
888 elf32_arm_reloc_type_lookup (abfd, code)
889 bfd *abfd ATTRIBUTE_UNUSED;
890 bfd_reloc_code_real_type code;
892 unsigned int i;
894 switch (code)
896 case BFD_RELOC_VTABLE_INHERIT:
897 return & elf32_arm_vtinherit_howto;
899 case BFD_RELOC_VTABLE_ENTRY:
900 return & elf32_arm_vtentry_howto;
902 case BFD_RELOC_THUMB_PCREL_BRANCH12:
903 return & elf32_arm_thm_pc11_howto;
905 case BFD_RELOC_THUMB_PCREL_BRANCH9:
906 return & elf32_arm_thm_pc9_howto;
908 default:
909 for (i = 0; i < NUM_ELEM (elf32_arm_reloc_map); i ++)
910 if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
911 return & elf32_arm_howto_table[elf32_arm_reloc_map[i].elf_reloc_val];
913 return NULL;
917 /* Support for core dump NOTE sections */
918 static bfd_boolean
919 elf32_arm_nabi_grok_prstatus (abfd, note)
920 bfd *abfd;
921 Elf_Internal_Note *note;
923 int offset;
924 size_t size;
926 switch (note->descsz)
928 default:
929 return FALSE;
931 case 148: /* Linux/ARM 32-bit*/
932 /* pr_cursig */
933 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
935 /* pr_pid */
936 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
938 /* pr_reg */
939 offset = 72;
940 size = 72;
942 break;
945 /* Make a ".reg/999" section. */
946 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
947 size, note->descpos + offset);
950 static bfd_boolean
951 elf32_arm_nabi_grok_psinfo (abfd, note)
952 bfd *abfd;
953 Elf_Internal_Note *note;
955 switch (note->descsz)
957 default:
958 return FALSE;
960 case 124: /* Linux/ARM elf_prpsinfo */
961 elf_tdata (abfd)->core_program
962 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
963 elf_tdata (abfd)->core_command
964 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
967 /* Note that for some reason, a spurious space is tacked
968 onto the end of the args in some (at least one anyway)
969 implementations, so strip it off if it exists. */
972 char *command = elf_tdata (abfd)->core_command;
973 int n = strlen (command);
975 if (0 < n && command[n - 1] == ' ')
976 command[n - 1] = '\0';
979 return TRUE;
982 #define TARGET_LITTLE_SYM bfd_elf32_littlearm_vec
983 #define TARGET_LITTLE_NAME "elf32-littlearm"
984 #define TARGET_BIG_SYM bfd_elf32_bigarm_vec
985 #define TARGET_BIG_NAME "elf32-bigarm"
987 #define elf_backend_grok_prstatus elf32_arm_nabi_grok_prstatus
988 #define elf_backend_grok_psinfo elf32_arm_nabi_grok_psinfo
990 #ifndef USE_REL
991 #define USE_REL 0
992 #endif
994 typedef unsigned long int insn32;
995 typedef unsigned short int insn16;
997 /* In lieu of proper flags, assume all EABIv4 objects are interworkable. */
998 #define INTERWORK_FLAG(abfd) \
999 (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) == EF_ARM_EABI_VER4 \
1000 || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK))
1002 /* The linker script knows the section names for placement.
1003 The entry_names are used to do simple name mangling on the stubs.
1004 Given a function name, and its type, the stub can be found. The
1005 name can be changed. The only requirement is the %s be present. */
1006 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
1007 #define THUMB2ARM_GLUE_ENTRY_NAME "__%s_from_thumb"
1009 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
1010 #define ARM2THUMB_GLUE_ENTRY_NAME "__%s_from_arm"
1012 /* The name of the dynamic interpreter. This is put in the .interp
1013 section. */
1014 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
1016 #ifdef FOUR_WORD_PLT
1018 /* The first entry in a procedure linkage table looks like
1019 this. It is set up so that any shared library function that is
1020 called before the relocation has been set up calls the dynamic
1021 linker first. */
1022 static const bfd_vma elf32_arm_plt0_entry [] =
1024 0xe52de004, /* str lr, [sp, #-4]! */
1025 0xe59fe010, /* ldr lr, [pc, #16] */
1026 0xe08fe00e, /* add lr, pc, lr */
1027 0xe5bef008, /* ldr pc, [lr, #8]! */
1030 /* Subsequent entries in a procedure linkage table look like
1031 this. */
1032 static const bfd_vma elf32_arm_plt_entry [] =
1034 0xe28fc600, /* add ip, pc, #NN */
1035 0xe28cca00, /* add ip, ip, #NN */
1036 0xe5bcf000, /* ldr pc, [ip, #NN]! */
1037 0x00000000, /* unused */
1040 #else
1042 /* The first entry in a procedure linkage table looks like
1043 this. It is set up so that any shared library function that is
1044 called before the relocation has been set up calls the dynamic
1045 linker first. */
1046 static const bfd_vma elf32_arm_plt0_entry [] =
1048 0xe52de004, /* str lr, [sp, #-4]! */
1049 0xe59fe004, /* ldr lr, [pc, #4] */
1050 0xe08fe00e, /* add lr, pc, lr */
1051 0xe5bef008, /* ldr pc, [lr, #8]! */
1052 0x00000000, /* &GOT[0] - . */
1055 /* Subsequent entries in a procedure linkage table look like
1056 this. */
1057 static const bfd_vma elf32_arm_plt_entry [] =
1059 0xe28fc600, /* add ip, pc, #0xNN00000 */
1060 0xe28cca00, /* add ip, ip, #0xNN000 */
1061 0xe5bcf000, /* ldr pc, [ip, #0xNNN]! */
1064 #endif
1066 /* An initial stub used if the PLT entry is referenced from Thumb code. */
1067 #define PLT_THUMB_STUB_SIZE 4
1068 static const bfd_vma elf32_arm_plt_thumb_stub [] =
1070 0x4778, /* bx pc */
1071 0x46c0 /* nop */
1074 /* The entries in a PLT when using a DLL-based target with multiple
1075 address spaces. */
1076 static const bfd_vma elf32_arm_symbian_plt_entry [] =
1078 0xe51ff004, /* ldr pr, [pc, #-4] */
1079 0x00000000, /* dcd R_ARM_GLOB_DAT(X) */
1082 /* Used to build a map of a section. This is required for mixed-endian
1083 code/data. */
1085 typedef struct elf32_elf_section_map
1087 bfd_vma vma;
1088 char type;
1090 elf32_arm_section_map;
1092 struct _arm_elf_section_data
1094 struct bfd_elf_section_data elf;
1095 int mapcount;
1096 elf32_arm_section_map *map;
1099 #define elf32_arm_section_data(sec) \
1100 ((struct _arm_elf_section_data *) elf_section_data (sec))
1102 /* The ARM linker needs to keep track of the number of relocs that it
1103 decides to copy in check_relocs for each symbol. This is so that
1104 it can discard PC relative relocs if it doesn't need them when
1105 linking with -Bsymbolic. We store the information in a field
1106 extending the regular ELF linker hash table. */
1108 /* This structure keeps track of the number of PC relative relocs we
1109 have copied for a given symbol. */
1110 struct elf32_arm_relocs_copied
1112 /* Next section. */
1113 struct elf32_arm_relocs_copied * next;
1114 /* A section in dynobj. */
1115 asection * section;
1116 /* Number of relocs copied in this section. */
1117 bfd_size_type count;
1120 /* Arm ELF linker hash entry. */
1121 struct elf32_arm_link_hash_entry
1123 struct elf_link_hash_entry root;
1125 /* Number of PC relative relocs copied for this symbol. */
1126 struct elf32_arm_relocs_copied * relocs_copied;
1128 /* We reference count Thumb references to a PLT entry separately,
1129 so that we can emit the Thumb trampoline only if needed. */
1130 bfd_signed_vma plt_thumb_refcount;
1132 /* Since PLT entries have variable size if the Thumb prologue is
1133 used, we need to record the index into .got.plt instead of
1134 recomputing it from the PLT offset. */
1135 bfd_signed_vma plt_got_offset;
1138 /* Traverse an arm ELF linker hash table. */
1139 #define elf32_arm_link_hash_traverse(table, func, info) \
1140 (elf_link_hash_traverse \
1141 (&(table)->root, \
1142 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1143 (info)))
1145 /* Get the ARM elf linker hash table from a link_info structure. */
1146 #define elf32_arm_hash_table(info) \
1147 ((struct elf32_arm_link_hash_table *) ((info)->hash))
1149 /* ARM ELF linker hash table. */
1150 struct elf32_arm_link_hash_table
1152 /* The main hash table. */
1153 struct elf_link_hash_table root;
1155 /* The size in bytes of the section containing the Thumb-to-ARM glue. */
1156 bfd_size_type thumb_glue_size;
1158 /* The size in bytes of the section containing the ARM-to-Thumb glue. */
1159 bfd_size_type arm_glue_size;
1161 /* An arbitrary input BFD chosen to hold the glue sections. */
1162 bfd * bfd_of_glue_owner;
1164 /* A boolean indicating whether knowledge of the ARM's pipeline
1165 length should be applied by the linker. */
1166 int no_pipeline_knowledge;
1168 /* Nonzero to output a BE8 image. */
1169 int byteswap_code;
1171 /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
1172 Nonzero if R_ARM_TARGET1 means R_ARM_ABS32. */
1173 int target1_is_rel;
1175 /* The relocation to use for R_ARM_TARGET2 relocations. */
1176 int target2_reloc;
1178 /* The number of bytes in the initial entry in the PLT. */
1179 bfd_size_type plt_header_size;
1181 /* The number of bytes in the subsequent PLT etries. */
1182 bfd_size_type plt_entry_size;
1184 /* True if the target system is Symbian OS. */
1185 int symbian_p;
1187 /* Short-cuts to get to dynamic linker sections. */
1188 asection *sgot;
1189 asection *sgotplt;
1190 asection *srelgot;
1191 asection *splt;
1192 asection *srelplt;
1193 asection *sdynbss;
1194 asection *srelbss;
1196 /* Small local sym to section mapping cache. */
1197 struct sym_sec_cache sym_sec;
1199 /* For convenience in allocate_dynrelocs. */
1200 bfd * obfd;
1203 /* Create an entry in an ARM ELF linker hash table. */
1205 static struct bfd_hash_entry *
1206 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
1207 struct bfd_hash_table * table,
1208 const char * string)
1210 struct elf32_arm_link_hash_entry * ret =
1211 (struct elf32_arm_link_hash_entry *) entry;
1213 /* Allocate the structure if it has not already been allocated by a
1214 subclass. */
1215 if (ret == (struct elf32_arm_link_hash_entry *) NULL)
1216 ret = bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
1217 if (ret == NULL)
1218 return (struct bfd_hash_entry *) ret;
1220 /* Call the allocation method of the superclass. */
1221 ret = ((struct elf32_arm_link_hash_entry *)
1222 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1223 table, string));
1224 if (ret != NULL)
1226 ret->relocs_copied = NULL;
1227 ret->plt_thumb_refcount = 0;
1228 ret->plt_got_offset = -1;
1231 return (struct bfd_hash_entry *) ret;
1234 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
1235 shortcuts to them in our hash table. */
1237 static bfd_boolean
1238 create_got_section (bfd *dynobj, struct bfd_link_info *info)
1240 struct elf32_arm_link_hash_table *htab;
1242 htab = elf32_arm_hash_table (info);
1243 /* BPABI objects never have a GOT, or associated sections. */
1244 if (htab->symbian_p)
1245 return TRUE;
1247 if (! _bfd_elf_create_got_section (dynobj, info))
1248 return FALSE;
1250 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1251 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1252 if (!htab->sgot || !htab->sgotplt)
1253 abort ();
1255 htab->srelgot = bfd_make_section (dynobj, ".rel.got");
1256 if (htab->srelgot == NULL
1257 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1258 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1259 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1260 | SEC_READONLY))
1261 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1262 return FALSE;
1263 return TRUE;
1266 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1267 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1268 hash table. */
1270 static bfd_boolean
1271 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1273 struct elf32_arm_link_hash_table *htab;
1275 htab = elf32_arm_hash_table (info);
1276 if (!htab->sgot && !create_got_section (dynobj, info))
1277 return FALSE;
1279 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1280 return FALSE;
1282 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1283 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
1284 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1285 if (!info->shared)
1286 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
1288 if (!htab->splt
1289 || !htab->srelplt
1290 || !htab->sdynbss
1291 || (!info->shared && !htab->srelbss))
1292 abort ();
1294 return TRUE;
1297 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1299 static void
1300 elf32_arm_copy_indirect_symbol (const struct elf_backend_data *bed,
1301 struct elf_link_hash_entry *dir,
1302 struct elf_link_hash_entry *ind)
1304 struct elf32_arm_link_hash_entry *edir, *eind;
1306 edir = (struct elf32_arm_link_hash_entry *) dir;
1307 eind = (struct elf32_arm_link_hash_entry *) ind;
1309 if (eind->relocs_copied != NULL)
1311 if (edir->relocs_copied != NULL)
1313 struct elf32_arm_relocs_copied **pp;
1314 struct elf32_arm_relocs_copied *p;
1316 if (ind->root.type == bfd_link_hash_indirect)
1317 abort ();
1319 /* Add reloc counts against the weak sym to the strong sym
1320 list. Merge any entries against the same section. */
1321 for (pp = &eind->relocs_copied; (p = *pp) != NULL; )
1323 struct elf32_arm_relocs_copied *q;
1325 for (q = edir->relocs_copied; q != NULL; q = q->next)
1326 if (q->section == p->section)
1328 q->count += p->count;
1329 *pp = p->next;
1330 break;
1332 if (q == NULL)
1333 pp = &p->next;
1335 *pp = edir->relocs_copied;
1338 edir->relocs_copied = eind->relocs_copied;
1339 eind->relocs_copied = NULL;
1342 /* If the direct symbol already has an associated PLT entry, the
1343 indirect symbol should not. If it doesn't, swap refcount information
1344 from the indirect symbol. */
1345 if (edir->plt_thumb_refcount == 0)
1347 edir->plt_thumb_refcount = eind->plt_thumb_refcount;
1348 eind->plt_thumb_refcount = 0;
1350 else
1351 BFD_ASSERT (eind->plt_thumb_refcount == 0);
1353 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1356 /* Create an ARM elf linker hash table. */
1358 static struct bfd_link_hash_table *
1359 elf32_arm_link_hash_table_create (bfd *abfd)
1361 struct elf32_arm_link_hash_table *ret;
1362 bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
1364 ret = bfd_malloc (amt);
1365 if (ret == NULL)
1366 return NULL;
1368 if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
1369 elf32_arm_link_hash_newfunc))
1371 free (ret);
1372 return NULL;
1375 ret->sgot = NULL;
1376 ret->sgotplt = NULL;
1377 ret->srelgot = NULL;
1378 ret->splt = NULL;
1379 ret->srelplt = NULL;
1380 ret->sdynbss = NULL;
1381 ret->srelbss = NULL;
1382 ret->thumb_glue_size = 0;
1383 ret->arm_glue_size = 0;
1384 ret->bfd_of_glue_owner = NULL;
1385 ret->no_pipeline_knowledge = 0;
1386 ret->byteswap_code = 0;
1387 ret->target1_is_rel = 0;
1388 ret->target2_reloc = R_ARM_NONE;
1389 #ifdef FOUR_WORD_PLT
1390 ret->plt_header_size = 16;
1391 ret->plt_entry_size = 16;
1392 #else
1393 ret->plt_header_size = 20;
1394 ret->plt_entry_size = 12;
1395 #endif
1396 ret->symbian_p = 0;
1397 ret->sym_sec.abfd = NULL;
1398 ret->obfd = abfd;
1400 return &ret->root.root;
1403 /* Locate the Thumb encoded calling stub for NAME. */
1405 static struct elf_link_hash_entry *
1406 find_thumb_glue (struct bfd_link_info *link_info,
1407 const char *name,
1408 bfd *input_bfd)
1410 char *tmp_name;
1411 struct elf_link_hash_entry *hash;
1412 struct elf32_arm_link_hash_table *hash_table;
1414 /* We need a pointer to the armelf specific hash table. */
1415 hash_table = elf32_arm_hash_table (link_info);
1417 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1418 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
1420 BFD_ASSERT (tmp_name);
1422 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1424 hash = elf_link_hash_lookup
1425 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1427 if (hash == NULL)
1428 /* xgettext:c-format */
1429 (*_bfd_error_handler) (_("%B: unable to find THUMB glue '%s' for `%s'"),
1430 input_bfd, tmp_name, name);
1432 free (tmp_name);
1434 return hash;
1437 /* Locate the ARM encoded calling stub for NAME. */
1439 static struct elf_link_hash_entry *
1440 find_arm_glue (struct bfd_link_info *link_info,
1441 const char *name,
1442 bfd *input_bfd)
1444 char *tmp_name;
1445 struct elf_link_hash_entry *myh;
1446 struct elf32_arm_link_hash_table *hash_table;
1448 /* We need a pointer to the elfarm specific hash table. */
1449 hash_table = elf32_arm_hash_table (link_info);
1451 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1452 + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
1454 BFD_ASSERT (tmp_name);
1456 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1458 myh = elf_link_hash_lookup
1459 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1461 if (myh == NULL)
1462 /* xgettext:c-format */
1463 (*_bfd_error_handler) (_("%B: unable to find ARM glue '%s' for `%s'"),
1464 input_bfd, tmp_name, name);
1466 free (tmp_name);
1468 return myh;
1471 /* ARM->Thumb glue:
1473 .arm
1474 __func_from_arm:
1475 ldr r12, __func_addr
1476 bx r12
1477 __func_addr:
1478 .word func @ behave as if you saw a ARM_32 reloc. */
1480 #define ARM2THUMB_GLUE_SIZE 12
1481 static const insn32 a2t1_ldr_insn = 0xe59fc000;
1482 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
1483 static const insn32 a2t3_func_addr_insn = 0x00000001;
1485 /* Thumb->ARM: Thumb->(non-interworking aware) ARM
1487 .thumb .thumb
1488 .align 2 .align 2
1489 __func_from_thumb: __func_from_thumb:
1490 bx pc push {r6, lr}
1491 nop ldr r6, __func_addr
1492 .arm mov lr, pc
1493 __func_change_to_arm: bx r6
1494 b func .arm
1495 __func_back_to_thumb:
1496 ldmia r13! {r6, lr}
1497 bx lr
1498 __func_addr:
1499 .word func */
1501 #define THUMB2ARM_GLUE_SIZE 8
1502 static const insn16 t2a1_bx_pc_insn = 0x4778;
1503 static const insn16 t2a2_noop_insn = 0x46c0;
1504 static const insn32 t2a3_b_insn = 0xea000000;
1506 #ifndef ELFARM_NABI_C_INCLUDED
1507 bfd_boolean
1508 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
1510 asection * s;
1511 bfd_byte * foo;
1512 struct elf32_arm_link_hash_table * globals;
1514 globals = elf32_arm_hash_table (info);
1516 BFD_ASSERT (globals != NULL);
1518 if (globals->arm_glue_size != 0)
1520 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1522 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
1523 ARM2THUMB_GLUE_SECTION_NAME);
1525 BFD_ASSERT (s != NULL);
1527 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->arm_glue_size);
1529 s->size = globals->arm_glue_size;
1530 s->contents = foo;
1533 if (globals->thumb_glue_size != 0)
1535 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1537 s = bfd_get_section_by_name
1538 (globals->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1540 BFD_ASSERT (s != NULL);
1542 foo = bfd_alloc (globals->bfd_of_glue_owner, globals->thumb_glue_size);
1544 s->size = globals->thumb_glue_size;
1545 s->contents = foo;
1548 return TRUE;
1551 static void
1552 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
1553 struct elf_link_hash_entry * h)
1555 const char * name = h->root.root.string;
1556 asection * s;
1557 char * tmp_name;
1558 struct elf_link_hash_entry * myh;
1559 struct bfd_link_hash_entry * bh;
1560 struct elf32_arm_link_hash_table * globals;
1561 bfd_vma val;
1563 globals = elf32_arm_hash_table (link_info);
1565 BFD_ASSERT (globals != NULL);
1566 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1568 s = bfd_get_section_by_name
1569 (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
1571 BFD_ASSERT (s != NULL);
1573 tmp_name = bfd_malloc ((bfd_size_type) strlen (name) + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
1575 BFD_ASSERT (tmp_name);
1577 sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
1579 myh = elf_link_hash_lookup
1580 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
1582 if (myh != NULL)
1584 /* We've already seen this guy. */
1585 free (tmp_name);
1586 return;
1589 /* The only trick here is using hash_table->arm_glue_size as the value.
1590 Even though the section isn't allocated yet, this is where we will be
1591 putting it. */
1592 bh = NULL;
1593 val = globals->arm_glue_size + 1;
1594 _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
1595 tmp_name, BSF_GLOBAL, s, val,
1596 NULL, TRUE, FALSE, &bh);
1598 myh = (struct elf_link_hash_entry *) bh;
1599 myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1600 myh->forced_local = 1;
1602 free (tmp_name);
1604 globals->arm_glue_size += ARM2THUMB_GLUE_SIZE;
1606 return;
1609 static void
1610 record_thumb_to_arm_glue (struct bfd_link_info *link_info,
1611 struct elf_link_hash_entry *h)
1613 const char *name = h->root.root.string;
1614 asection *s;
1615 char *tmp_name;
1616 struct elf_link_hash_entry *myh;
1617 struct bfd_link_hash_entry *bh;
1618 struct elf32_arm_link_hash_table *hash_table;
1619 bfd_vma val;
1621 hash_table = elf32_arm_hash_table (link_info);
1623 BFD_ASSERT (hash_table != NULL);
1624 BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
1626 s = bfd_get_section_by_name
1627 (hash_table->bfd_of_glue_owner, THUMB2ARM_GLUE_SECTION_NAME);
1629 BFD_ASSERT (s != NULL);
1631 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1632 + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
1634 BFD_ASSERT (tmp_name);
1636 sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
1638 myh = elf_link_hash_lookup
1639 (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
1641 if (myh != NULL)
1643 /* We've already seen this guy. */
1644 free (tmp_name);
1645 return;
1648 bh = NULL;
1649 val = hash_table->thumb_glue_size + 1;
1650 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1651 tmp_name, BSF_GLOBAL, s, val,
1652 NULL, TRUE, FALSE, &bh);
1654 /* If we mark it 'Thumb', the disassembler will do a better job. */
1655 myh = (struct elf_link_hash_entry *) bh;
1656 myh->type = ELF_ST_INFO (STB_LOCAL, STT_ARM_TFUNC);
1657 myh->forced_local = 1;
1659 free (tmp_name);
1661 #define CHANGE_TO_ARM "__%s_change_to_arm"
1662 #define BACK_FROM_ARM "__%s_back_from_arm"
1664 /* Allocate another symbol to mark where we switch to Arm mode. */
1665 tmp_name = bfd_malloc ((bfd_size_type) strlen (name)
1666 + strlen (CHANGE_TO_ARM) + 1);
1668 BFD_ASSERT (tmp_name);
1670 sprintf (tmp_name, CHANGE_TO_ARM, name);
1672 bh = NULL;
1673 val = hash_table->thumb_glue_size + 4,
1674 _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
1675 tmp_name, BSF_LOCAL, s, val,
1676 NULL, TRUE, FALSE, &bh);
1678 free (tmp_name);
1680 hash_table->thumb_glue_size += THUMB2ARM_GLUE_SIZE;
1682 return;
1685 /* Add the glue sections to ABFD. This function is called from the
1686 linker scripts in ld/emultempl/{armelf}.em. */
1688 bfd_boolean
1689 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
1690 struct bfd_link_info *info)
1692 flagword flags;
1693 asection *sec;
1695 /* If we are only performing a partial
1696 link do not bother adding the glue. */
1697 if (info->relocatable)
1698 return TRUE;
1700 sec = bfd_get_section_by_name (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1702 if (sec == NULL)
1704 /* Note: we do not include the flag SEC_LINKER_CREATED, as this
1705 will prevent elf_link_input_bfd() from processing the contents
1706 of this section. */
1707 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE | SEC_READONLY;
1709 sec = bfd_make_section (abfd, ARM2THUMB_GLUE_SECTION_NAME);
1711 if (sec == NULL
1712 || !bfd_set_section_flags (abfd, sec, flags)
1713 || !bfd_set_section_alignment (abfd, sec, 2))
1714 return FALSE;
1716 /* Set the gc mark to prevent the section from being removed by garbage
1717 collection, despite the fact that no relocs refer to this section. */
1718 sec->gc_mark = 1;
1721 sec = bfd_get_section_by_name (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1723 if (sec == NULL)
1725 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1726 | SEC_CODE | SEC_READONLY;
1728 sec = bfd_make_section (abfd, THUMB2ARM_GLUE_SECTION_NAME);
1730 if (sec == NULL
1731 || !bfd_set_section_flags (abfd, sec, flags)
1732 || !bfd_set_section_alignment (abfd, sec, 2))
1733 return FALSE;
1735 sec->gc_mark = 1;
1738 return TRUE;
1741 /* Select a BFD to be used to hold the sections used by the glue code.
1742 This function is called from the linker scripts in ld/emultempl/
1743 {armelf/pe}.em */
1745 bfd_boolean
1746 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
1748 struct elf32_arm_link_hash_table *globals;
1750 /* If we are only performing a partial link
1751 do not bother getting a bfd to hold the glue. */
1752 if (info->relocatable)
1753 return TRUE;
1755 /* Make sure we don't attach the glue sections to a dynamic object. */
1756 BFD_ASSERT (!(abfd->flags & DYNAMIC));
1758 globals = elf32_arm_hash_table (info);
1760 BFD_ASSERT (globals != NULL);
1762 if (globals->bfd_of_glue_owner != NULL)
1763 return TRUE;
1765 /* Save the bfd for later use. */
1766 globals->bfd_of_glue_owner = abfd;
1768 return TRUE;
1771 bfd_boolean
1772 bfd_elf32_arm_process_before_allocation (bfd *abfd,
1773 struct bfd_link_info *link_info,
1774 int no_pipeline_knowledge,
1775 int byteswap_code)
1777 Elf_Internal_Shdr *symtab_hdr;
1778 Elf_Internal_Rela *internal_relocs = NULL;
1779 Elf_Internal_Rela *irel, *irelend;
1780 bfd_byte *contents = NULL;
1782 asection *sec;
1783 struct elf32_arm_link_hash_table *globals;
1785 /* If we are only performing a partial link do not bother
1786 to construct any glue. */
1787 if (link_info->relocatable)
1788 return TRUE;
1790 /* Here we have a bfd that is to be included on the link. We have a hook
1791 to do reloc rummaging, before section sizes are nailed down. */
1792 globals = elf32_arm_hash_table (link_info);
1794 BFD_ASSERT (globals != NULL);
1795 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
1797 globals->no_pipeline_knowledge = no_pipeline_knowledge;
1799 if (byteswap_code && !bfd_big_endian (abfd))
1801 _bfd_error_handler (_("%B: BE8 images only valid in big-endian mode."),
1802 abfd);
1803 return FALSE;
1805 globals->byteswap_code = byteswap_code;
1807 /* Rummage around all the relocs and map the glue vectors. */
1808 sec = abfd->sections;
1810 if (sec == NULL)
1811 return TRUE;
1813 for (; sec != NULL; sec = sec->next)
1815 if (sec->reloc_count == 0)
1816 continue;
1818 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1820 /* Load the relocs. */
1821 internal_relocs
1822 = _bfd_elf_link_read_relocs (abfd, sec, (void *) NULL,
1823 (Elf_Internal_Rela *) NULL, FALSE);
1825 if (internal_relocs == NULL)
1826 goto error_return;
1828 irelend = internal_relocs + sec->reloc_count;
1829 for (irel = internal_relocs; irel < irelend; irel++)
1831 long r_type;
1832 unsigned long r_index;
1834 struct elf_link_hash_entry *h;
1836 r_type = ELF32_R_TYPE (irel->r_info);
1837 r_index = ELF32_R_SYM (irel->r_info);
1839 /* These are the only relocation types we care about. */
1840 if ( r_type != R_ARM_PC24
1841 && r_type != R_ARM_PLT32
1842 #ifndef OLD_ARM_ABI
1843 && r_type != R_ARM_CALL
1844 && r_type != R_ARM_JUMP24
1845 #endif
1846 && r_type != R_ARM_THM_PC22)
1847 continue;
1849 /* Get the section contents if we haven't done so already. */
1850 if (contents == NULL)
1852 /* Get cached copy if it exists. */
1853 if (elf_section_data (sec)->this_hdr.contents != NULL)
1854 contents = elf_section_data (sec)->this_hdr.contents;
1855 else
1857 /* Go get them off disk. */
1858 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1859 goto error_return;
1863 /* If the relocation is not against a symbol it cannot concern us. */
1864 h = NULL;
1866 /* We don't care about local symbols. */
1867 if (r_index < symtab_hdr->sh_info)
1868 continue;
1870 /* This is an external symbol. */
1871 r_index -= symtab_hdr->sh_info;
1872 h = (struct elf_link_hash_entry *)
1873 elf_sym_hashes (abfd)[r_index];
1875 /* If the relocation is against a static symbol it must be within
1876 the current section and so cannot be a cross ARM/Thumb relocation. */
1877 if (h == NULL)
1878 continue;
1880 /* If the call will go through a PLT entry then we do not need
1881 glue. */
1882 if (globals->splt != NULL && h->plt.offset != (bfd_vma) -1)
1883 continue;
1885 switch (r_type)
1887 case R_ARM_PC24:
1888 #ifndef OLD_ARM_ABI
1889 case R_ARM_CALL:
1890 case R_ARM_JUMP24:
1891 #endif
1892 /* This one is a call from arm code. We need to look up
1893 the target of the call. If it is a thumb target, we
1894 insert glue. */
1895 if (ELF_ST_TYPE(h->type) == STT_ARM_TFUNC)
1896 record_arm_to_thumb_glue (link_info, h);
1897 break;
1899 case R_ARM_THM_PC22:
1900 /* This one is a call from thumb code. We look
1901 up the target of the call. If it is not a thumb
1902 target, we insert glue. */
1903 if (ELF_ST_TYPE (h->type) != STT_ARM_TFUNC)
1904 record_thumb_to_arm_glue (link_info, h);
1905 break;
1907 default:
1908 break;
1912 if (contents != NULL
1913 && elf_section_data (sec)->this_hdr.contents != contents)
1914 free (contents);
1915 contents = NULL;
1917 if (internal_relocs != NULL
1918 && elf_section_data (sec)->relocs != internal_relocs)
1919 free (internal_relocs);
1920 internal_relocs = NULL;
1923 return TRUE;
1925 error_return:
1926 if (contents != NULL
1927 && elf_section_data (sec)->this_hdr.contents != contents)
1928 free (contents);
1929 if (internal_relocs != NULL
1930 && elf_section_data (sec)->relocs != internal_relocs)
1931 free (internal_relocs);
1933 return FALSE;
1935 #endif
1938 #ifndef OLD_ARM_ABI
1939 /* Set target relocation values needed during linking. */
1941 void
1942 bfd_elf32_arm_set_target_relocs (struct bfd_link_info *link_info,
1943 int target1_is_rel,
1944 char * target2_type)
1946 struct elf32_arm_link_hash_table *globals;
1948 globals = elf32_arm_hash_table (link_info);
1950 globals->target1_is_rel = target1_is_rel;
1951 if (strcmp (target2_type, "rel") == 0)
1952 globals->target2_reloc = R_ARM_REL32;
1953 else if (strcmp (target2_type, "abs") == 0)
1954 globals->target2_reloc = R_ARM_ABS32;
1955 else if (strcmp (target2_type, "got-rel") == 0)
1956 globals->target2_reloc = R_ARM_GOT_PREL;
1957 else
1959 _bfd_error_handler (_("Invalid TARGET2 relocation type '%s'."),
1960 target2_type);
1963 #endif
1965 /* The thumb form of a long branch is a bit finicky, because the offset
1966 encoding is split over two fields, each in it's own instruction. They
1967 can occur in any order. So given a thumb form of long branch, and an
1968 offset, insert the offset into the thumb branch and return finished
1969 instruction.
1971 It takes two thumb instructions to encode the target address. Each has
1972 11 bits to invest. The upper 11 bits are stored in one (identified by
1973 H-0.. see below), the lower 11 bits are stored in the other (identified
1974 by H-1).
1976 Combine together and shifted left by 1 (it's a half word address) and
1977 there you have it.
1979 Op: 1111 = F,
1980 H-0, upper address-0 = 000
1981 Op: 1111 = F,
1982 H-1, lower address-0 = 800
1984 They can be ordered either way, but the arm tools I've seen always put
1985 the lower one first. It probably doesn't matter. krk@cygnus.com
1987 XXX: Actually the order does matter. The second instruction (H-1)
1988 moves the computed address into the PC, so it must be the second one
1989 in the sequence. The problem, however is that whilst little endian code
1990 stores the instructions in HI then LOW order, big endian code does the
1991 reverse. nickc@cygnus.com. */
1993 #define LOW_HI_ORDER 0xF800F000
1994 #define HI_LOW_ORDER 0xF000F800
1996 static insn32
1997 insert_thumb_branch (insn32 br_insn, int rel_off)
1999 unsigned int low_bits;
2000 unsigned int high_bits;
2002 BFD_ASSERT ((rel_off & 1) != 1);
2004 rel_off >>= 1; /* Half word aligned address. */
2005 low_bits = rel_off & 0x000007FF; /* The bottom 11 bits. */
2006 high_bits = (rel_off >> 11) & 0x000007FF; /* The top 11 bits. */
2008 if ((br_insn & LOW_HI_ORDER) == LOW_HI_ORDER)
2009 br_insn = LOW_HI_ORDER | (low_bits << 16) | high_bits;
2010 else if ((br_insn & HI_LOW_ORDER) == HI_LOW_ORDER)
2011 br_insn = HI_LOW_ORDER | (high_bits << 16) | low_bits;
2012 else
2013 /* FIXME: abort is probably not the right call. krk@cygnus.com */
2014 abort (); /* Error - not a valid branch instruction form. */
2016 return br_insn;
2019 /* Thumb code calling an ARM function. */
2021 static int
2022 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
2023 const char * name,
2024 bfd * input_bfd,
2025 bfd * output_bfd,
2026 asection * input_section,
2027 bfd_byte * hit_data,
2028 asection * sym_sec,
2029 bfd_vma offset,
2030 bfd_signed_vma addend,
2031 bfd_vma val)
2033 asection * s = 0;
2034 bfd_vma my_offset;
2035 unsigned long int tmp;
2036 long int ret_offset;
2037 struct elf_link_hash_entry * myh;
2038 struct elf32_arm_link_hash_table * globals;
2040 myh = find_thumb_glue (info, name, input_bfd);
2041 if (myh == NULL)
2042 return FALSE;
2044 globals = elf32_arm_hash_table (info);
2046 BFD_ASSERT (globals != NULL);
2047 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2049 my_offset = myh->root.u.def.value;
2051 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2052 THUMB2ARM_GLUE_SECTION_NAME);
2054 BFD_ASSERT (s != NULL);
2055 BFD_ASSERT (s->contents != NULL);
2056 BFD_ASSERT (s->output_section != NULL);
2058 if ((my_offset & 0x01) == 0x01)
2060 if (sym_sec != NULL
2061 && sym_sec->owner != NULL
2062 && !INTERWORK_FLAG (sym_sec->owner))
2064 (*_bfd_error_handler)
2065 (_("%B(%s): warning: interworking not enabled.\n"
2066 " first occurrence: %B: thumb call to arm"),
2067 sym_sec->owner, input_bfd, name);
2069 return FALSE;
2072 --my_offset;
2073 myh->root.u.def.value = my_offset;
2075 bfd_put_16 (output_bfd, (bfd_vma) t2a1_bx_pc_insn,
2076 s->contents + my_offset);
2078 bfd_put_16 (output_bfd, (bfd_vma) t2a2_noop_insn,
2079 s->contents + my_offset + 2);
2081 ret_offset =
2082 /* Address of destination of the stub. */
2083 ((bfd_signed_vma) val)
2084 - ((bfd_signed_vma)
2085 /* Offset from the start of the current section
2086 to the start of the stubs. */
2087 (s->output_offset
2088 /* Offset of the start of this stub from the start of the stubs. */
2089 + my_offset
2090 /* Address of the start of the current section. */
2091 + s->output_section->vma)
2092 /* The branch instruction is 4 bytes into the stub. */
2094 /* ARM branches work from the pc of the instruction + 8. */
2095 + 8);
2097 bfd_put_32 (output_bfd,
2098 (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
2099 s->contents + my_offset + 4);
2102 BFD_ASSERT (my_offset <= globals->thumb_glue_size);
2104 /* Now go back and fix up the original BL insn to point to here. */
2105 ret_offset =
2106 /* Address of where the stub is located. */
2107 (s->output_section->vma + s->output_offset + my_offset)
2108 /* Address of where the BL is located. */
2109 - (input_section->output_section->vma + input_section->output_offset
2110 + offset)
2111 /* Addend in the relocation. */
2112 - addend
2113 /* Biassing for PC-relative addressing. */
2114 - 8;
2116 tmp = bfd_get_32 (input_bfd, hit_data
2117 - input_section->vma);
2119 bfd_put_32 (output_bfd,
2120 (bfd_vma) insert_thumb_branch (tmp, ret_offset),
2121 hit_data - input_section->vma);
2123 return TRUE;
2126 /* Arm code calling a Thumb function. */
2128 static int
2129 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
2130 const char * name,
2131 bfd * input_bfd,
2132 bfd * output_bfd,
2133 asection * input_section,
2134 bfd_byte * hit_data,
2135 asection * sym_sec,
2136 bfd_vma offset,
2137 bfd_signed_vma addend,
2138 bfd_vma val)
2140 unsigned long int tmp;
2141 bfd_vma my_offset;
2142 asection * s;
2143 long int ret_offset;
2144 struct elf_link_hash_entry * myh;
2145 struct elf32_arm_link_hash_table * globals;
2147 myh = find_arm_glue (info, name, input_bfd);
2148 if (myh == NULL)
2149 return FALSE;
2151 globals = elf32_arm_hash_table (info);
2153 BFD_ASSERT (globals != NULL);
2154 BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
2156 my_offset = myh->root.u.def.value;
2157 s = bfd_get_section_by_name (globals->bfd_of_glue_owner,
2158 ARM2THUMB_GLUE_SECTION_NAME);
2159 BFD_ASSERT (s != NULL);
2160 BFD_ASSERT (s->contents != NULL);
2161 BFD_ASSERT (s->output_section != NULL);
2163 if ((my_offset & 0x01) == 0x01)
2165 if (sym_sec != NULL
2166 && sym_sec->owner != NULL
2167 && !INTERWORK_FLAG (sym_sec->owner))
2169 (*_bfd_error_handler)
2170 (_("%B(%s): warning: interworking not enabled.\n"
2171 " first occurrence: %B: arm call to thumb"),
2172 sym_sec->owner, input_bfd, name);
2175 --my_offset;
2176 myh->root.u.def.value = my_offset;
2178 bfd_put_32 (output_bfd, (bfd_vma) a2t1_ldr_insn,
2179 s->contents + my_offset);
2181 bfd_put_32 (output_bfd, (bfd_vma) a2t2_bx_r12_insn,
2182 s->contents + my_offset + 4);
2184 /* It's a thumb address. Add the low order bit. */
2185 bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
2186 s->contents + my_offset + 8);
2189 BFD_ASSERT (my_offset <= globals->arm_glue_size);
2191 tmp = bfd_get_32 (input_bfd, hit_data);
2192 tmp = tmp & 0xFF000000;
2194 /* Somehow these are both 4 too far, so subtract 8. */
2195 ret_offset = (s->output_offset
2196 + my_offset
2197 + s->output_section->vma
2198 - (input_section->output_offset
2199 + input_section->output_section->vma
2200 + offset + addend)
2201 - 8);
2203 tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
2205 bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
2207 return TRUE;
2211 #ifndef OLD_ARM_ABI
2212 /* Some relocations map to different relocations depending on the
2213 target. Return the real relocation. */
2214 static int
2215 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
2216 int r_type)
2218 switch (r_type)
2220 case R_ARM_TARGET1:
2221 if (globals->target1_is_rel)
2222 return R_ARM_REL32;
2223 else
2224 return R_ARM_ABS32;
2226 case R_ARM_TARGET2:
2227 return globals->target2_reloc;
2229 default:
2230 return r_type;
2233 #endif /* OLD_ARM_ABI */
2236 /* Perform a relocation as part of a final link. */
2238 static bfd_reloc_status_type
2239 elf32_arm_final_link_relocate (reloc_howto_type * howto,
2240 bfd * input_bfd,
2241 bfd * output_bfd,
2242 asection * input_section,
2243 bfd_byte * contents,
2244 Elf_Internal_Rela * rel,
2245 bfd_vma value,
2246 struct bfd_link_info * info,
2247 asection * sym_sec,
2248 const char * sym_name,
2249 int sym_flags,
2250 struct elf_link_hash_entry * h)
2252 unsigned long r_type = howto->type;
2253 unsigned long r_symndx;
2254 bfd_byte * hit_data = contents + rel->r_offset;
2255 bfd * dynobj = NULL;
2256 Elf_Internal_Shdr * symtab_hdr;
2257 struct elf_link_hash_entry ** sym_hashes;
2258 bfd_vma * local_got_offsets;
2259 asection * sgot = NULL;
2260 asection * splt = NULL;
2261 asection * sreloc = NULL;
2262 bfd_vma addend;
2263 bfd_signed_vma signed_addend;
2264 struct elf32_arm_link_hash_table * globals;
2266 globals = elf32_arm_hash_table (info);
2268 #ifndef OLD_ARM_ABI
2269 /* Some relocation type map to different relocations depending on the
2270 target. We pick the right one here. */
2271 r_type = arm_real_reloc_type (globals, r_type);
2272 if (r_type != howto->type)
2273 howto = elf32_arm_howto_from_type (r_type);
2274 #endif /* OLD_ARM_ABI */
2276 /* If the start address has been set, then set the EF_ARM_HASENTRY
2277 flag. Setting this more than once is redundant, but the cost is
2278 not too high, and it keeps the code simple.
2280 The test is done here, rather than somewhere else, because the
2281 start address is only set just before the final link commences.
2283 Note - if the user deliberately sets a start address of 0, the
2284 flag will not be set. */
2285 if (bfd_get_start_address (output_bfd) != 0)
2286 elf_elfheader (output_bfd)->e_flags |= EF_ARM_HASENTRY;
2288 dynobj = elf_hash_table (info)->dynobj;
2289 if (dynobj)
2291 sgot = bfd_get_section_by_name (dynobj, ".got");
2292 splt = bfd_get_section_by_name (dynobj, ".plt");
2294 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2295 sym_hashes = elf_sym_hashes (input_bfd);
2296 local_got_offsets = elf_local_got_offsets (input_bfd);
2297 r_symndx = ELF32_R_SYM (rel->r_info);
2299 #if USE_REL
2300 addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
2302 if (addend & ((howto->src_mask + 1) >> 1))
2304 signed_addend = -1;
2305 signed_addend &= ~ howto->src_mask;
2306 signed_addend |= addend;
2308 else
2309 signed_addend = addend;
2310 #else
2311 addend = signed_addend = rel->r_addend;
2312 #endif
2314 switch (r_type)
2316 case R_ARM_NONE:
2317 return bfd_reloc_ok;
2319 case R_ARM_PC24:
2320 case R_ARM_ABS32:
2321 case R_ARM_REL32:
2322 #ifndef OLD_ARM_ABI
2323 case R_ARM_CALL:
2324 case R_ARM_JUMP24:
2325 case R_ARM_XPC25:
2326 case R_ARM_PREL31:
2327 #endif
2328 case R_ARM_PLT32:
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;
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. */
2339 if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32)
2340 && h != NULL
2341 && splt != NULL
2342 && h->plt.offset != (bfd_vma) -1)
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. */
2347 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
2349 value = (splt->output_section->vma
2350 + splt->output_offset
2351 + h->plt.offset);
2352 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2353 contents, rel->r_offset, value,
2354 (bfd_vma) 0);
2357 /* When generating a shared object, these relocations are copied
2358 into the output file to be resolved at run time. */
2359 if (info->shared
2360 && (input_section->flags & SEC_ALLOC)
2361 && (r_type != R_ARM_REL32
2362 || !SYMBOL_CALLS_LOCAL (info, h))
2363 && (h == NULL
2364 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2365 || h->root.type != bfd_link_hash_undefweak)
2366 && r_type != R_ARM_PC24
2367 #ifndef OLD_ARM_ABI
2368 && r_type != R_ARM_CALL
2369 && r_type != R_ARM_JUMP24
2370 && r_type != R_ARM_PREL31
2371 #endif
2372 && r_type != R_ARM_PLT32)
2374 Elf_Internal_Rela outrel;
2375 bfd_byte *loc;
2376 bfd_boolean skip, relocate;
2378 if (sreloc == NULL)
2380 const char * name;
2382 name = (bfd_elf_string_from_elf_section
2383 (input_bfd,
2384 elf_elfheader (input_bfd)->e_shstrndx,
2385 elf_section_data (input_section)->rel_hdr.sh_name));
2386 if (name == NULL)
2387 return bfd_reloc_notsupported;
2389 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
2390 && strcmp (bfd_get_section_name (input_bfd,
2391 input_section),
2392 name + 4) == 0);
2394 sreloc = bfd_get_section_by_name (dynobj, name);
2395 BFD_ASSERT (sreloc != NULL);
2398 skip = FALSE;
2399 relocate = FALSE;
2401 outrel.r_offset =
2402 _bfd_elf_section_offset (output_bfd, info, input_section,
2403 rel->r_offset);
2404 if (outrel.r_offset == (bfd_vma) -1)
2405 skip = TRUE;
2406 else if (outrel.r_offset == (bfd_vma) -2)
2407 skip = TRUE, relocate = TRUE;
2408 outrel.r_offset += (input_section->output_section->vma
2409 + input_section->output_offset);
2411 if (skip)
2412 memset (&outrel, 0, sizeof outrel);
2413 else if (h != NULL
2414 && h->dynindx != -1
2415 && (!info->shared
2416 || !info->symbolic
2417 || !h->def_regular))
2418 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2419 else
2421 int symbol;
2423 /* This symbol is local, or marked to become local. */
2424 relocate = TRUE;
2425 if (sym_flags == STT_ARM_TFUNC)
2426 value |= 1;
2427 if (globals->symbian_p)
2429 /* On Symbian OS, the data segment and text segement
2430 can be relocated independently. Therefore, we
2431 must indicate the segment to which this
2432 relocation is relative. The BPABI allows us to
2433 use any symbol in the right segment; we just use
2434 the section symbol as it is convenient. (We
2435 cannot use the symbol given by "h" directly as it
2436 will not appear in the dynamic symbol table.) */
2437 symbol = elf_section_data (sym_sec->output_section)->dynindx;
2438 BFD_ASSERT (symbol != 0);
2440 else
2441 /* On SVR4-ish systems, the dynamic loader cannot
2442 relocate the text and data segments independently,
2443 so the symbol does not matter. */
2444 symbol = 0;
2445 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
2448 loc = sreloc->contents;
2449 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2450 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2452 /* If this reloc is against an external symbol, we do not want to
2453 fiddle with the addend. Otherwise, we need to include the symbol
2454 value so that it becomes an addend for the dynamic reloc. */
2455 if (! relocate)
2456 return bfd_reloc_ok;
2458 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2459 contents, rel->r_offset, value,
2460 (bfd_vma) 0);
2462 else switch (r_type)
2464 #ifndef OLD_ARM_ABI
2465 case R_ARM_XPC25: /* Arm BLX instruction. */
2466 case R_ARM_CALL:
2467 case R_ARM_JUMP24:
2468 #endif
2469 case R_ARM_PC24: /* Arm B/BL instruction */
2470 case R_ARM_PLT32:
2471 #ifndef OLD_ARM_ABI
2472 if (r_type == R_ARM_XPC25)
2474 /* Check for Arm calling Arm function. */
2475 /* FIXME: Should we translate the instruction into a BL
2476 instruction instead ? */
2477 if (sym_flags != STT_ARM_TFUNC)
2478 (*_bfd_error_handler)
2479 (_("\%B: Warning: Arm BLX instruction targets Arm function '%s'."),
2480 input_bfd,
2481 h ? h->root.root.string : "(local)");
2483 else
2484 #endif
2486 /* Check for Arm calling Thumb function. */
2487 if (sym_flags == STT_ARM_TFUNC)
2489 elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
2490 output_bfd, input_section,
2491 hit_data, sym_sec, rel->r_offset,
2492 signed_addend, value);
2493 return bfd_reloc_ok;
2497 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
2498 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0)
2500 /* The old way of doing things. Trearing the addend as a
2501 byte sized field and adding in the pipeline offset. */
2502 value -= (input_section->output_section->vma
2503 + input_section->output_offset);
2504 value -= rel->r_offset;
2505 value += addend;
2507 if (! globals->no_pipeline_knowledge)
2508 value -= 8;
2510 else
2512 /* The ARM ELF ABI says that this reloc is computed as: S - P + A
2513 where:
2514 S is the address of the symbol in the relocation.
2515 P is address of the instruction being relocated.
2516 A is the addend (extracted from the instruction) in bytes.
2518 S is held in 'value'.
2519 P is the base address of the section containing the
2520 instruction plus the offset of the reloc into that
2521 section, ie:
2522 (input_section->output_section->vma +
2523 input_section->output_offset +
2524 rel->r_offset).
2525 A is the addend, converted into bytes, ie:
2526 (signed_addend * 4)
2528 Note: None of these operations have knowledge of the pipeline
2529 size of the processor, thus it is up to the assembler to
2530 encode this information into the addend. */
2531 value -= (input_section->output_section->vma
2532 + input_section->output_offset);
2533 value -= rel->r_offset;
2534 value += (signed_addend << howto->size);
2536 /* Previous versions of this code also used to add in the
2537 pipeline offset here. This is wrong because the linker is
2538 not supposed to know about such things, and one day it might
2539 change. In order to support old binaries that need the old
2540 behaviour however, so we attempt to detect which ABI was
2541 used to create the reloc. */
2542 if (! globals->no_pipeline_knowledge)
2544 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
2546 i_ehdrp = elf_elfheader (input_bfd);
2548 if (i_ehdrp->e_ident[EI_OSABI] == 0)
2549 value -= 8;
2553 signed_addend = value;
2554 signed_addend >>= howto->rightshift;
2556 /* It is not an error for an undefined weak reference to be
2557 out of range. Any program that branches to such a symbol
2558 is going to crash anyway, so there is no point worrying
2559 about getting the destination exactly right. */
2560 if (! h || h->root.type != bfd_link_hash_undefweak)
2562 /* Perform a signed range check. */
2563 if ( signed_addend > ((bfd_signed_vma) (howto->dst_mask >> 1))
2564 || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
2565 return bfd_reloc_overflow;
2568 #ifndef OLD_ARM_ABI
2569 /* If necessary set the H bit in the BLX instruction. */
2570 if (r_type == R_ARM_XPC25 && ((value & 2) == 2))
2571 value = (signed_addend & howto->dst_mask)
2572 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask))
2573 | (1 << 24);
2574 else
2575 #endif
2576 value = (signed_addend & howto->dst_mask)
2577 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
2578 break;
2580 case R_ARM_ABS32:
2581 value += addend;
2582 if (sym_flags == STT_ARM_TFUNC)
2583 value |= 1;
2584 break;
2586 case R_ARM_REL32:
2587 value -= (input_section->output_section->vma
2588 + input_section->output_offset + rel->r_offset);
2589 value += addend;
2590 break;
2592 #ifndef OLD_ARM_ABI
2593 case R_ARM_PREL31:
2594 value -= (input_section->output_section->vma
2595 + input_section->output_offset + rel->r_offset);
2596 value += signed_addend;
2597 if (! h || h->root.type != bfd_link_hash_undefweak)
2599 /* Check for overflow */
2600 if ((value ^ (value >> 1)) & (1 << 30))
2601 return bfd_reloc_overflow;
2603 value &= 0x7fffffff;
2604 value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
2605 if (sym_flags == STT_ARM_TFUNC)
2606 value |= 1;
2607 break;
2608 #endif
2611 bfd_put_32 (input_bfd, value, hit_data);
2612 return bfd_reloc_ok;
2614 case R_ARM_ABS8:
2615 value += addend;
2616 if ((long) value > 0x7f || (long) value < -0x80)
2617 return bfd_reloc_overflow;
2619 bfd_put_8 (input_bfd, value, hit_data);
2620 return bfd_reloc_ok;
2622 case R_ARM_ABS16:
2623 value += addend;
2625 if ((long) value > 0x7fff || (long) value < -0x8000)
2626 return bfd_reloc_overflow;
2628 bfd_put_16 (input_bfd, value, hit_data);
2629 return bfd_reloc_ok;
2631 case R_ARM_ABS12:
2632 /* Support ldr and str instruction for the arm */
2633 /* Also thumb b (unconditional branch). ??? Really? */
2634 value += addend;
2636 if ((long) value > 0x7ff || (long) value < -0x800)
2637 return bfd_reloc_overflow;
2639 value |= (bfd_get_32 (input_bfd, hit_data) & 0xfffff000);
2640 bfd_put_32 (input_bfd, value, hit_data);
2641 return bfd_reloc_ok;
2643 case R_ARM_THM_ABS5:
2644 /* Support ldr and str instructions for the thumb. */
2645 #if USE_REL
2646 /* Need to refetch addend. */
2647 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
2648 /* ??? Need to determine shift amount from operand size. */
2649 addend >>= howto->rightshift;
2650 #endif
2651 value += addend;
2653 /* ??? Isn't value unsigned? */
2654 if ((long) value > 0x1f || (long) value < -0x10)
2655 return bfd_reloc_overflow;
2657 /* ??? Value needs to be properly shifted into place first. */
2658 value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
2659 bfd_put_16 (input_bfd, value, hit_data);
2660 return bfd_reloc_ok;
2662 #ifndef OLD_ARM_ABI
2663 case R_ARM_THM_XPC22:
2664 #endif
2665 case R_ARM_THM_PC22:
2666 /* Thumb BL (branch long instruction). */
2668 bfd_vma relocation;
2669 bfd_boolean overflow = FALSE;
2670 bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
2671 bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
2672 bfd_signed_vma reloc_signed_max = ((1 << (howto->bitsize - 1)) - 1) >> howto->rightshift;
2673 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
2674 bfd_vma check;
2675 bfd_signed_vma signed_check;
2677 #if USE_REL
2678 /* Need to refetch the addend and squish the two 11 bit pieces
2679 together. */
2681 bfd_vma upper = upper_insn & 0x7ff;
2682 bfd_vma lower = lower_insn & 0x7ff;
2683 upper = (upper ^ 0x400) - 0x400; /* Sign extend. */
2684 addend = (upper << 12) | (lower << 1);
2685 signed_addend = addend;
2687 #endif
2688 #ifndef OLD_ARM_ABI
2689 if (r_type == R_ARM_THM_XPC22)
2691 /* Check for Thumb to Thumb call. */
2692 /* FIXME: Should we translate the instruction into a BL
2693 instruction instead ? */
2694 if (sym_flags == STT_ARM_TFUNC)
2695 (*_bfd_error_handler)
2696 (_("%B: Warning: Thumb BLX instruction targets thumb function '%s'."),
2697 input_bfd,
2698 h ? h->root.root.string : "(local)");
2700 else
2701 #endif
2703 /* If it is not a call to Thumb, assume call to Arm.
2704 If it is a call relative to a section name, then it is not a
2705 function call at all, but rather a long jump. Calls through
2706 the PLT do not require stubs. */
2707 if (sym_flags != STT_ARM_TFUNC && sym_flags != STT_SECTION
2708 && (h == NULL || splt == NULL
2709 || h->plt.offset == (bfd_vma) -1))
2711 if (elf32_thumb_to_arm_stub
2712 (info, sym_name, input_bfd, output_bfd, input_section,
2713 hit_data, sym_sec, rel->r_offset, signed_addend, value))
2714 return bfd_reloc_ok;
2715 else
2716 return bfd_reloc_dangerous;
2720 /* Handle calls via the PLT. */
2721 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
2723 value = (splt->output_section->vma
2724 + splt->output_offset
2725 + h->plt.offset);
2726 /* Target the Thumb stub before the ARM PLT entry. */
2727 value -= 4;
2730 relocation = value + signed_addend;
2732 relocation -= (input_section->output_section->vma
2733 + input_section->output_offset
2734 + rel->r_offset);
2736 if (! globals->no_pipeline_knowledge)
2738 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form. */
2740 i_ehdrp = elf_elfheader (input_bfd);
2742 /* Previous versions of this code also used to add in the pipline
2743 offset here. This is wrong because the linker is not supposed
2744 to know about such things, and one day it might change. In order
2745 to support old binaries that need the old behaviour however, so
2746 we attempt to detect which ABI was used to create the reloc. */
2747 if ( strcmp (bfd_get_target (input_bfd), "elf32-littlearm-oabi") == 0
2748 || strcmp (bfd_get_target (input_bfd), "elf32-bigarm-oabi") == 0
2749 || i_ehdrp->e_ident[EI_OSABI] == 0)
2750 relocation += 4;
2753 check = relocation >> howto->rightshift;
2755 /* If this is a signed value, the rightshift just dropped
2756 leading 1 bits (assuming twos complement). */
2757 if ((bfd_signed_vma) relocation >= 0)
2758 signed_check = check;
2759 else
2760 signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
2762 /* Assumes two's complement. */
2763 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
2764 overflow = TRUE;
2766 #ifndef OLD_ARM_ABI
2767 if (r_type == R_ARM_THM_XPC22
2768 && ((lower_insn & 0x1800) == 0x0800))
2769 /* For a BLX instruction, make sure that the relocation is rounded up
2770 to a word boundary. This follows the semantics of the instruction
2771 which specifies that bit 1 of the target address will come from bit
2772 1 of the base address. */
2773 relocation = (relocation + 2) & ~ 3;
2774 #endif
2775 /* Put RELOCATION back into the insn. */
2776 upper_insn = (upper_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 12) & 0x7ff);
2777 lower_insn = (lower_insn & ~(bfd_vma) 0x7ff) | ((relocation >> 1) & 0x7ff);
2779 /* Put the relocated value back in the object file: */
2780 bfd_put_16 (input_bfd, upper_insn, hit_data);
2781 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
2783 return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
2785 break;
2787 case R_ARM_THM_PC11:
2788 /* Thumb B (branch) instruction). */
2790 bfd_signed_vma relocation;
2791 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
2792 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
2793 bfd_signed_vma signed_check;
2795 #if USE_REL
2796 /* Need to refetch addend. */
2797 addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
2798 if (addend & ((howto->src_mask + 1) >> 1))
2800 signed_addend = -1;
2801 signed_addend &= ~ howto->src_mask;
2802 signed_addend |= addend;
2804 else
2805 signed_addend = addend;
2806 /* The value in the insn has been right shifted. We need to
2807 undo this, so that we can perform the address calculation
2808 in terms of bytes. */
2809 signed_addend <<= howto->rightshift;
2810 #endif
2811 relocation = value + signed_addend;
2813 relocation -= (input_section->output_section->vma
2814 + input_section->output_offset
2815 + rel->r_offset);
2817 relocation >>= howto->rightshift;
2818 signed_check = relocation;
2819 relocation &= howto->dst_mask;
2820 relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
2822 bfd_put_16 (input_bfd, relocation, hit_data);
2824 /* Assumes two's complement. */
2825 if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
2826 return bfd_reloc_overflow;
2828 return bfd_reloc_ok;
2831 #ifndef OLD_ARM_ABI
2832 case R_ARM_ALU_PCREL7_0:
2833 case R_ARM_ALU_PCREL15_8:
2834 case R_ARM_ALU_PCREL23_15:
2836 bfd_vma insn;
2837 bfd_vma relocation;
2839 insn = bfd_get_32 (input_bfd, hit_data);
2840 #if USE_REL
2841 /* Extract the addend. */
2842 addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
2843 signed_addend = addend;
2844 #endif
2845 relocation = value + signed_addend;
2847 relocation -= (input_section->output_section->vma
2848 + input_section->output_offset
2849 + rel->r_offset);
2850 insn = (insn & ~0xfff)
2851 | ((howto->bitpos << 7) & 0xf00)
2852 | ((relocation >> howto->bitpos) & 0xff);
2853 bfd_put_32 (input_bfd, value, hit_data);
2855 return bfd_reloc_ok;
2856 #endif
2858 case R_ARM_GNU_VTINHERIT:
2859 case R_ARM_GNU_VTENTRY:
2860 return bfd_reloc_ok;
2862 case R_ARM_COPY:
2863 return bfd_reloc_notsupported;
2865 case R_ARM_GLOB_DAT:
2866 return bfd_reloc_notsupported;
2868 case R_ARM_JUMP_SLOT:
2869 return bfd_reloc_notsupported;
2871 case R_ARM_RELATIVE:
2872 return bfd_reloc_notsupported;
2874 case R_ARM_GOTOFF:
2875 /* Relocation is relative to the start of the
2876 global offset table. */
2878 BFD_ASSERT (sgot != NULL);
2879 if (sgot == NULL)
2880 return bfd_reloc_notsupported;
2882 /* If we are addressing a Thumb function, we need to adjust the
2883 address by one, so that attempts to call the function pointer will
2884 correctly interpret it as Thumb code. */
2885 if (sym_flags == STT_ARM_TFUNC)
2886 value += 1;
2888 /* Note that sgot->output_offset is not involved in this
2889 calculation. We always want the start of .got. If we
2890 define _GLOBAL_OFFSET_TABLE in a different way, as is
2891 permitted by the ABI, we might have to change this
2892 calculation. */
2893 value -= sgot->output_section->vma;
2894 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2895 contents, rel->r_offset, value,
2896 (bfd_vma) 0);
2898 case R_ARM_GOTPC:
2899 /* Use global offset table as symbol value. */
2900 BFD_ASSERT (sgot != NULL);
2902 if (sgot == NULL)
2903 return bfd_reloc_notsupported;
2905 value = sgot->output_section->vma;
2906 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2907 contents, rel->r_offset, value,
2908 (bfd_vma) 0);
2910 case R_ARM_GOT32:
2911 #ifndef OLD_ARM_ABI
2912 case R_ARM_GOT_PREL:
2913 #endif
2914 /* Relocation is to the entry for this symbol in the
2915 global offset table. */
2916 if (sgot == NULL)
2917 return bfd_reloc_notsupported;
2919 if (h != NULL)
2921 bfd_vma off;
2922 bfd_boolean dyn;
2924 off = h->got.offset;
2925 BFD_ASSERT (off != (bfd_vma) -1);
2926 dyn = globals->root.dynamic_sections_created;
2928 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2929 || (info->shared
2930 && SYMBOL_REFERENCES_LOCAL (info, h))
2931 || (ELF_ST_VISIBILITY (h->other)
2932 && h->root.type == bfd_link_hash_undefweak))
2934 /* This is actually a static link, or it is a -Bsymbolic link
2935 and the symbol is defined locally. We must initialize this
2936 entry in the global offset table. Since the offset must
2937 always be a multiple of 4, we use the least significant bit
2938 to record whether we have initialized it already.
2940 When doing a dynamic link, we create a .rel.got relocation
2941 entry to initialize the value. This is done in the
2942 finish_dynamic_symbol routine. */
2943 if ((off & 1) != 0)
2944 off &= ~1;
2945 else
2947 /* If we are addressing a Thumb function, we need to
2948 adjust the address by one, so that attempts to
2949 call the function pointer will correctly
2950 interpret it as Thumb code. */
2951 if (sym_flags == STT_ARM_TFUNC)
2952 value |= 1;
2954 bfd_put_32 (output_bfd, value, sgot->contents + off);
2955 h->got.offset |= 1;
2959 value = sgot->output_offset + off;
2961 else
2963 bfd_vma off;
2965 BFD_ASSERT (local_got_offsets != NULL &&
2966 local_got_offsets[r_symndx] != (bfd_vma) -1);
2968 off = local_got_offsets[r_symndx];
2970 /* The offset must always be a multiple of 4. We use the
2971 least significant bit to record whether we have already
2972 generated the necessary reloc. */
2973 if ((off & 1) != 0)
2974 off &= ~1;
2975 else
2977 /* If we are addressing a Thumb function, we need to
2978 adjust the address by one, so that attempts to
2979 call the function pointer will correctly
2980 interpret it as Thumb code. */
2981 if (sym_flags == STT_ARM_TFUNC)
2982 value |= 1;
2984 bfd_put_32 (output_bfd, value, sgot->contents + off);
2986 if (info->shared)
2988 asection * srelgot;
2989 Elf_Internal_Rela outrel;
2990 bfd_byte *loc;
2992 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
2993 BFD_ASSERT (srelgot != NULL);
2995 outrel.r_offset = (sgot->output_section->vma
2996 + sgot->output_offset
2997 + off);
2998 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
2999 loc = srelgot->contents;
3000 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3001 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3004 local_got_offsets[r_symndx] |= 1;
3007 value = sgot->output_offset + off;
3009 if (r_type != R_ARM_GOT32)
3010 value += sgot->output_section->vma;
3012 return _bfd_final_link_relocate (howto, input_bfd, input_section,
3013 contents, rel->r_offset, value,
3014 (bfd_vma) 0);
3016 case R_ARM_SBREL32:
3017 return bfd_reloc_notsupported;
3019 case R_ARM_AMP_VCALL9:
3020 return bfd_reloc_notsupported;
3022 case R_ARM_RSBREL32:
3023 return bfd_reloc_notsupported;
3025 case R_ARM_THM_RPC22:
3026 return bfd_reloc_notsupported;
3028 case R_ARM_RREL32:
3029 return bfd_reloc_notsupported;
3031 case R_ARM_RABS32:
3032 return bfd_reloc_notsupported;
3034 case R_ARM_RPC24:
3035 return bfd_reloc_notsupported;
3037 case R_ARM_RBASE:
3038 return bfd_reloc_notsupported;
3040 default:
3041 return bfd_reloc_notsupported;
3045 #if USE_REL
3046 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. */
3047 static void
3048 arm_add_to_rel (bfd * abfd,
3049 bfd_byte * address,
3050 reloc_howto_type * howto,
3051 bfd_signed_vma increment)
3053 bfd_signed_vma addend;
3055 if (howto->type == R_ARM_THM_PC22)
3057 int upper_insn, lower_insn;
3058 int upper, lower;
3060 upper_insn = bfd_get_16 (abfd, address);
3061 lower_insn = bfd_get_16 (abfd, address + 2);
3062 upper = upper_insn & 0x7ff;
3063 lower = lower_insn & 0x7ff;
3065 addend = (upper << 12) | (lower << 1);
3066 addend += increment;
3067 addend >>= 1;
3069 upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
3070 lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
3072 bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
3073 bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
3075 else
3077 bfd_vma contents;
3079 contents = bfd_get_32 (abfd, address);
3081 /* Get the (signed) value from the instruction. */
3082 addend = contents & howto->src_mask;
3083 if (addend & ((howto->src_mask + 1) >> 1))
3085 bfd_signed_vma mask;
3087 mask = -1;
3088 mask &= ~ howto->src_mask;
3089 addend |= mask;
3092 /* Add in the increment, (which is a byte value). */
3093 switch (howto->type)
3095 default:
3096 addend += increment;
3097 break;
3099 case R_ARM_PC24:
3100 #ifndef OLD_ARM_ABI
3101 case R_ARM_CALL:
3102 case R_ARM_JUMP24:
3103 #endif
3104 addend <<= howto->size;
3105 addend += increment;
3107 /* Should we check for overflow here ? */
3109 /* Drop any undesired bits. */
3110 addend >>= howto->rightshift;
3111 break;
3114 contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
3116 bfd_put_32 (abfd, contents, address);
3119 #endif /* USE_REL */
3121 /* Relocate an ARM ELF section. */
3122 static bfd_boolean
3123 elf32_arm_relocate_section (bfd * output_bfd,
3124 struct bfd_link_info * info,
3125 bfd * input_bfd,
3126 asection * input_section,
3127 bfd_byte * contents,
3128 Elf_Internal_Rela * relocs,
3129 Elf_Internal_Sym * local_syms,
3130 asection ** local_sections)
3132 Elf_Internal_Shdr *symtab_hdr;
3133 struct elf_link_hash_entry **sym_hashes;
3134 Elf_Internal_Rela *rel;
3135 Elf_Internal_Rela *relend;
3136 const char *name;
3137 struct elf32_arm_link_hash_table * globals;
3139 #if !USE_REL
3140 if (info->relocatable)
3141 return TRUE;
3142 #endif
3144 globals = elf32_arm_hash_table (info);
3145 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
3146 sym_hashes = elf_sym_hashes (input_bfd);
3148 rel = relocs;
3149 relend = relocs + input_section->reloc_count;
3150 for (; rel < relend; rel++)
3152 int r_type;
3153 reloc_howto_type * howto;
3154 unsigned long r_symndx;
3155 Elf_Internal_Sym * sym;
3156 asection * sec;
3157 struct elf_link_hash_entry * h;
3158 bfd_vma relocation;
3159 bfd_reloc_status_type r;
3160 arelent bfd_reloc;
3162 r_symndx = ELF32_R_SYM (rel->r_info);
3163 r_type = ELF32_R_TYPE (rel->r_info);
3164 r_type = arm_real_reloc_type (globals, r_type);
3166 if ( r_type == R_ARM_GNU_VTENTRY
3167 || r_type == R_ARM_GNU_VTINHERIT)
3168 continue;
3170 bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
3171 howto = bfd_reloc.howto;
3173 #if USE_REL
3174 if (info->relocatable)
3176 /* This is a relocatable link. We don't have to change
3177 anything, unless the reloc is against a section symbol,
3178 in which case we have to adjust according to where the
3179 section symbol winds up in the output section. */
3180 if (r_symndx < symtab_hdr->sh_info)
3182 sym = local_syms + r_symndx;
3183 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3185 sec = local_sections[r_symndx];
3186 arm_add_to_rel (input_bfd, contents + rel->r_offset,
3187 howto,
3188 (bfd_signed_vma) (sec->output_offset
3189 + sym->st_value));
3193 continue;
3195 #endif
3197 /* This is a final link. */
3198 h = NULL;
3199 sym = NULL;
3200 sec = NULL;
3202 if (r_symndx < symtab_hdr->sh_info)
3204 sym = local_syms + r_symndx;
3205 sec = local_sections[r_symndx];
3206 #if USE_REL
3207 relocation = (sec->output_section->vma
3208 + sec->output_offset
3209 + sym->st_value);
3210 if ((sec->flags & SEC_MERGE)
3211 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3213 asection *msec;
3214 bfd_vma addend, value;
3216 if (howto->rightshift)
3218 (*_bfd_error_handler)
3219 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3220 input_bfd, input_section,
3221 (long) rel->r_offset, howto->name);
3222 return FALSE;
3225 value = bfd_get_32 (input_bfd, contents + rel->r_offset);
3227 /* Get the (signed) value from the instruction. */
3228 addend = value & howto->src_mask;
3229 if (addend & ((howto->src_mask + 1) >> 1))
3231 bfd_signed_vma mask;
3233 mask = -1;
3234 mask &= ~ howto->src_mask;
3235 addend |= mask;
3237 msec = sec;
3238 addend =
3239 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3240 - relocation;
3241 addend += msec->output_section->vma + msec->output_offset;
3242 value = (value & ~ howto->dst_mask) | (addend & howto->dst_mask);
3243 bfd_put_32 (input_bfd, value, contents + rel->r_offset);
3245 #else
3246 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3247 #endif
3249 else
3251 bfd_boolean warned;
3252 bfd_boolean unresolved_reloc;
3254 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3255 r_symndx, symtab_hdr, sym_hashes,
3256 h, sec, relocation,
3257 unresolved_reloc, warned);
3259 if (unresolved_reloc || relocation != 0)
3261 /* In these cases, we don't need the relocation value.
3262 We check specially because in some obscure cases
3263 sec->output_section will be NULL. */
3264 switch (r_type)
3266 case R_ARM_PC24:
3267 #ifndef OLD_ARM_ABI
3268 case R_ARM_CALL:
3269 case R_ARM_JUMP24:
3270 case R_ARM_PREL31:
3271 #endif
3272 case R_ARM_ABS32:
3273 case R_ARM_THM_PC22:
3274 case R_ARM_PLT32:
3276 if (info->shared
3277 && ((!info->symbolic && h->dynindx != -1)
3278 || !h->def_regular)
3279 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3280 && ((input_section->flags & SEC_ALLOC) != 0
3281 /* DWARF will emit R_ARM_ABS32 relocations in its
3282 sections against symbols defined externally
3283 in shared libraries. We can't do anything
3284 with them here. */
3285 || ((input_section->flags & SEC_DEBUGGING) != 0
3286 && h->def_dynamic))
3288 relocation = 0;
3289 break;
3291 case R_ARM_GOTPC:
3292 relocation = 0;
3293 break;
3295 case R_ARM_GOT32:
3296 #ifndef OLD_ARM_ABI
3297 case R_ARM_GOT_PREL:
3298 #endif
3299 if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL
3300 (elf_hash_table (info)->dynamic_sections_created,
3301 info->shared, h))
3302 && (!info->shared
3303 || (!info->symbolic && h->dynindx != -1)
3304 || !h->def_regular))
3305 relocation = 0;
3306 break;
3308 default:
3309 if (unresolved_reloc)
3310 _bfd_error_handler
3311 (_("%B(%A): warning: unresolvable relocation %d against symbol `%s'"),
3312 input_bfd, input_section,
3313 r_type,
3314 h->root.root.string);
3315 break;
3320 if (h != NULL)
3321 name = h->root.root.string;
3322 else
3324 name = (bfd_elf_string_from_elf_section
3325 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3326 if (name == NULL || *name == '\0')
3327 name = bfd_section_name (input_bfd, sec);
3330 r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
3331 input_section, contents, rel,
3332 relocation, info, sec, name,
3333 (h ? ELF_ST_TYPE (h->type) :
3334 ELF_ST_TYPE (sym->st_info)), h);
3336 if (r != bfd_reloc_ok)
3338 const char * msg = (const char *) 0;
3340 switch (r)
3342 case bfd_reloc_overflow:
3343 /* If the overflowing reloc was to an undefined symbol,
3344 we have already printed one error message and there
3345 is no point complaining again. */
3346 if ((! h ||
3347 h->root.type != bfd_link_hash_undefined)
3348 && (!((*info->callbacks->reloc_overflow)
3349 (info, (h ? &h->root : NULL), name, howto->name,
3350 (bfd_vma) 0, input_bfd, input_section,
3351 rel->r_offset))))
3352 return FALSE;
3353 break;
3355 case bfd_reloc_undefined:
3356 if (!((*info->callbacks->undefined_symbol)
3357 (info, name, input_bfd, input_section,
3358 rel->r_offset, TRUE)))
3359 return FALSE;
3360 break;
3362 case bfd_reloc_outofrange:
3363 msg = _("internal error: out of range error");
3364 goto common_error;
3366 case bfd_reloc_notsupported:
3367 msg = _("internal error: unsupported relocation error");
3368 goto common_error;
3370 case bfd_reloc_dangerous:
3371 msg = _("internal error: dangerous error");
3372 goto common_error;
3374 default:
3375 msg = _("internal error: unknown error");
3376 /* fall through */
3378 common_error:
3379 if (!((*info->callbacks->warning)
3380 (info, msg, name, input_bfd, input_section,
3381 rel->r_offset)))
3382 return FALSE;
3383 break;
3388 return TRUE;
3391 /* Set the right machine number. */
3393 static bfd_boolean
3394 elf32_arm_object_p (bfd *abfd)
3396 unsigned int mach;
3398 mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
3400 if (mach != bfd_mach_arm_unknown)
3401 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
3403 else if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
3404 bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_ep9312);
3406 else
3407 bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
3409 return TRUE;
3412 /* Function to keep ARM specific flags in the ELF header. */
3414 static bfd_boolean
3415 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
3417 if (elf_flags_init (abfd)
3418 && elf_elfheader (abfd)->e_flags != flags)
3420 if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
3422 if (flags & EF_ARM_INTERWORK)
3423 (*_bfd_error_handler)
3424 (_("Warning: Not setting interworking flag of %B since it has already been specified as non-interworking"),
3425 abfd);
3426 else
3427 _bfd_error_handler
3428 (_("Warning: Clearing the interworking flag of %B due to outside request"),
3429 abfd);
3432 else
3434 elf_elfheader (abfd)->e_flags = flags;
3435 elf_flags_init (abfd) = TRUE;
3438 return TRUE;
3441 /* Copy backend specific data from one object module to another. */
3443 static bfd_boolean
3444 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
3446 flagword in_flags;
3447 flagword out_flags;
3449 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3450 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3451 return TRUE;
3453 in_flags = elf_elfheader (ibfd)->e_flags;
3454 out_flags = elf_elfheader (obfd)->e_flags;
3456 if (elf_flags_init (obfd)
3457 && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
3458 && in_flags != out_flags)
3460 /* Cannot mix APCS26 and APCS32 code. */
3461 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
3462 return FALSE;
3464 /* Cannot mix float APCS and non-float APCS code. */
3465 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
3466 return FALSE;
3468 /* If the src and dest have different interworking flags
3469 then turn off the interworking bit. */
3470 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
3472 if (out_flags & EF_ARM_INTERWORK)
3473 _bfd_error_handler
3474 (_("Warning: Clearing the interworking flag of %B because non-interworking code in %B has been linked with it"),
3475 obfd, ibfd);
3477 in_flags &= ~EF_ARM_INTERWORK;
3480 /* Likewise for PIC, though don't warn for this case. */
3481 if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
3482 in_flags &= ~EF_ARM_PIC;
3485 elf_elfheader (obfd)->e_flags = in_flags;
3486 elf_flags_init (obfd) = TRUE;
3488 return TRUE;
3491 /* Merge backend specific data from an object file to the output
3492 object file when linking. */
3494 static bfd_boolean
3495 elf32_arm_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
3497 flagword out_flags;
3498 flagword in_flags;
3499 bfd_boolean flags_compatible = TRUE;
3500 asection *sec;
3502 /* Check if we have the same endianess. */
3503 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3504 return FALSE;
3506 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3507 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3508 return TRUE;
3510 /* The input BFD must have had its flags initialised. */
3511 /* The following seems bogus to me -- The flags are initialized in
3512 the assembler but I don't think an elf_flags_init field is
3513 written into the object. */
3514 /* BFD_ASSERT (elf_flags_init (ibfd)); */
3516 in_flags = elf_elfheader (ibfd)->e_flags;
3517 out_flags = elf_elfheader (obfd)->e_flags;
3519 if (!elf_flags_init (obfd))
3521 /* If the input is the default architecture and had the default
3522 flags then do not bother setting the flags for the output
3523 architecture, instead allow future merges to do this. If no
3524 future merges ever set these flags then they will retain their
3525 uninitialised values, which surprise surprise, correspond
3526 to the default values. */
3527 if (bfd_get_arch_info (ibfd)->the_default
3528 && elf_elfheader (ibfd)->e_flags == 0)
3529 return TRUE;
3531 elf_flags_init (obfd) = TRUE;
3532 elf_elfheader (obfd)->e_flags = in_flags;
3534 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3535 && bfd_get_arch_info (obfd)->the_default)
3536 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3538 return TRUE;
3541 /* Determine what should happen if the input ARM architecture
3542 does not match the output ARM architecture. */
3543 if (! bfd_arm_merge_machines (ibfd, obfd))
3544 return FALSE;
3546 /* Identical flags must be compatible. */
3547 if (in_flags == out_flags)
3548 return TRUE;
3550 /* Check to see if the input BFD actually contains any sections. If
3551 not, its flags may not have been initialised either, but it
3552 cannot actually cause any incompatibility. Do not short-circuit
3553 dynamic objects; their section list may be emptied by
3554 elf_link_add_object_symbols.
3556 Also check to see if there are no code sections in the input.
3557 In this case there is no need to check for code specific flags.
3558 XXX - do we need to worry about floating-point format compatability
3559 in data sections ? */
3560 if (!(ibfd->flags & DYNAMIC))
3562 bfd_boolean null_input_bfd = TRUE;
3563 bfd_boolean only_data_sections = TRUE;
3565 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3567 /* Ignore synthetic glue sections. */
3568 if (strcmp (sec->name, ".glue_7")
3569 && strcmp (sec->name, ".glue_7t"))
3571 if ((bfd_get_section_flags (ibfd, sec)
3572 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3573 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3574 only_data_sections = FALSE;
3576 null_input_bfd = FALSE;
3577 break;
3581 if (null_input_bfd || only_data_sections)
3582 return TRUE;
3585 /* Complain about various flag mismatches. */
3586 if (EF_ARM_EABI_VERSION (in_flags) != EF_ARM_EABI_VERSION (out_flags))
3588 _bfd_error_handler
3589 (_("ERROR: Source object %B has EABI version %d, but target %B has EABI version %d"),
3590 ibfd, obfd,
3591 (in_flags & EF_ARM_EABIMASK) >> 24,
3592 (out_flags & EF_ARM_EABIMASK) >> 24);
3593 return FALSE;
3596 /* Not sure what needs to be checked for EABI versions >= 1. */
3597 if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
3599 if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
3601 _bfd_error_handler
3602 (_("ERROR: %B is compiled for APCS-%d, whereas target %B uses APCS-%d"),
3603 ibfd, obfd,
3604 in_flags & EF_ARM_APCS_26 ? 26 : 32,
3605 out_flags & EF_ARM_APCS_26 ? 26 : 32);
3606 flags_compatible = FALSE;
3609 if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
3611 if (in_flags & EF_ARM_APCS_FLOAT)
3612 _bfd_error_handler
3613 (_("ERROR: %B passes floats in float registers, whereas %B passes them in integer registers"),
3614 ibfd, obfd);
3615 else
3616 _bfd_error_handler
3617 (_("ERROR: %B passes floats in integer registers, whereas %B passes them in float registers"),
3618 ibfd, obfd);
3620 flags_compatible = FALSE;
3623 if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
3625 if (in_flags & EF_ARM_VFP_FLOAT)
3626 _bfd_error_handler
3627 (_("ERROR: %B uses VFP instructions, whereas %B does not"),
3628 ibfd, obfd);
3629 else
3630 _bfd_error_handler
3631 (_("ERROR: %B uses FPA instructions, whereas %B does not"),
3632 ibfd, obfd);
3634 flags_compatible = FALSE;
3637 if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
3639 if (in_flags & EF_ARM_MAVERICK_FLOAT)
3640 _bfd_error_handler
3641 (_("ERROR: %B uses Maverick instructions, whereas %B does not"),
3642 ibfd, obfd);
3643 else
3644 _bfd_error_handler
3645 (_("ERROR: %B does not use Maverick instructions, whereas %B does"),
3646 ibfd, obfd);
3648 flags_compatible = FALSE;
3651 #ifdef EF_ARM_SOFT_FLOAT
3652 if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
3654 /* We can allow interworking between code that is VFP format
3655 layout, and uses either soft float or integer regs for
3656 passing floating point arguments and results. We already
3657 know that the APCS_FLOAT flags match; similarly for VFP
3658 flags. */
3659 if ((in_flags & EF_ARM_APCS_FLOAT) != 0
3660 || (in_flags & EF_ARM_VFP_FLOAT) == 0)
3662 if (in_flags & EF_ARM_SOFT_FLOAT)
3663 _bfd_error_handler
3664 (_("ERROR: %B uses software FP, whereas %B uses hardware FP"),
3665 ibfd, obfd);
3666 else
3667 _bfd_error_handler
3668 (_("ERROR: %B uses hardware FP, whereas %B uses software FP"),
3669 ibfd, obfd);
3671 flags_compatible = FALSE;
3674 #endif
3676 /* Interworking mismatch is only a warning. */
3677 if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
3679 if (in_flags & EF_ARM_INTERWORK)
3681 _bfd_error_handler
3682 (_("Warning: %B supports interworking, whereas %B does not"),
3683 ibfd, obfd);
3685 else
3687 _bfd_error_handler
3688 (_("Warning: %B does not support interworking, whereas %B does"),
3689 ibfd, obfd);
3694 return flags_compatible;
3697 /* Display the flags field. */
3699 static bfd_boolean
3700 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
3702 FILE * file = (FILE *) ptr;
3703 unsigned long flags;
3705 BFD_ASSERT (abfd != NULL && ptr != NULL);
3707 /* Print normal ELF private data. */
3708 _bfd_elf_print_private_bfd_data (abfd, ptr);
3710 flags = elf_elfheader (abfd)->e_flags;
3711 /* Ignore init flag - it may not be set, despite the flags field
3712 containing valid data. */
3714 /* xgettext:c-format */
3715 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3717 switch (EF_ARM_EABI_VERSION (flags))
3719 case EF_ARM_EABI_UNKNOWN:
3720 /* The following flag bits are GNU extensions and not part of the
3721 official ARM ELF extended ABI. Hence they are only decoded if
3722 the EABI version is not set. */
3723 if (flags & EF_ARM_INTERWORK)
3724 fprintf (file, _(" [interworking enabled]"));
3726 if (flags & EF_ARM_APCS_26)
3727 fprintf (file, " [APCS-26]");
3728 else
3729 fprintf (file, " [APCS-32]");
3731 if (flags & EF_ARM_VFP_FLOAT)
3732 fprintf (file, _(" [VFP float format]"));
3733 else if (flags & EF_ARM_MAVERICK_FLOAT)
3734 fprintf (file, _(" [Maverick float format]"));
3735 else
3736 fprintf (file, _(" [FPA float format]"));
3738 if (flags & EF_ARM_APCS_FLOAT)
3739 fprintf (file, _(" [floats passed in float registers]"));
3741 if (flags & EF_ARM_PIC)
3742 fprintf (file, _(" [position independent]"));
3744 if (flags & EF_ARM_NEW_ABI)
3745 fprintf (file, _(" [new ABI]"));
3747 if (flags & EF_ARM_OLD_ABI)
3748 fprintf (file, _(" [old ABI]"));
3750 if (flags & EF_ARM_SOFT_FLOAT)
3751 fprintf (file, _(" [software FP]"));
3753 flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
3754 | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
3755 | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
3756 | EF_ARM_MAVERICK_FLOAT);
3757 break;
3759 case EF_ARM_EABI_VER1:
3760 fprintf (file, _(" [Version1 EABI]"));
3762 if (flags & EF_ARM_SYMSARESORTED)
3763 fprintf (file, _(" [sorted symbol table]"));
3764 else
3765 fprintf (file, _(" [unsorted symbol table]"));
3767 flags &= ~ EF_ARM_SYMSARESORTED;
3768 break;
3770 case EF_ARM_EABI_VER2:
3771 fprintf (file, _(" [Version2 EABI]"));
3773 if (flags & EF_ARM_SYMSARESORTED)
3774 fprintf (file, _(" [sorted symbol table]"));
3775 else
3776 fprintf (file, _(" [unsorted symbol table]"));
3778 if (flags & EF_ARM_DYNSYMSUSESEGIDX)
3779 fprintf (file, _(" [dynamic symbols use segment index]"));
3781 if (flags & EF_ARM_MAPSYMSFIRST)
3782 fprintf (file, _(" [mapping symbols precede others]"));
3784 flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
3785 | EF_ARM_MAPSYMSFIRST);
3786 break;
3788 case EF_ARM_EABI_VER3:
3789 fprintf (file, _(" [Version3 EABI]"));
3790 break;
3792 case EF_ARM_EABI_VER4:
3793 fprintf (file, _(" [Version4 EABI]"));
3795 if (flags & EF_ARM_BE8)
3796 fprintf (file, _(" [BE8]"));
3798 if (flags & EF_ARM_LE8)
3799 fprintf (file, _(" [LE8]"));
3801 flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
3802 break;
3804 default:
3805 fprintf (file, _(" <EABI version unrecognised>"));
3806 break;
3809 flags &= ~ EF_ARM_EABIMASK;
3811 if (flags & EF_ARM_RELEXEC)
3812 fprintf (file, _(" [relocatable executable]"));
3814 if (flags & EF_ARM_HASENTRY)
3815 fprintf (file, _(" [has entry point]"));
3817 flags &= ~ (EF_ARM_RELEXEC | EF_ARM_HASENTRY);
3819 if (flags)
3820 fprintf (file, _("<Unrecognised flag bits set>"));
3822 fputc ('\n', file);
3824 return TRUE;
3827 static int
3828 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
3830 switch (ELF_ST_TYPE (elf_sym->st_info))
3832 case STT_ARM_TFUNC:
3833 return ELF_ST_TYPE (elf_sym->st_info);
3835 case STT_ARM_16BIT:
3836 /* If the symbol is not an object, return the STT_ARM_16BIT flag.
3837 This allows us to distinguish between data used by Thumb instructions
3838 and non-data (which is probably code) inside Thumb regions of an
3839 executable. */
3840 if (type != STT_OBJECT)
3841 return ELF_ST_TYPE (elf_sym->st_info);
3842 break;
3844 default:
3845 break;
3848 return type;
3851 static asection *
3852 elf32_arm_gc_mark_hook (asection * sec,
3853 struct bfd_link_info * info ATTRIBUTE_UNUSED,
3854 Elf_Internal_Rela * rel,
3855 struct elf_link_hash_entry * h,
3856 Elf_Internal_Sym * sym)
3858 if (h != NULL)
3860 switch (ELF32_R_TYPE (rel->r_info))
3862 case R_ARM_GNU_VTINHERIT:
3863 case R_ARM_GNU_VTENTRY:
3864 break;
3866 default:
3867 switch (h->root.type)
3869 case bfd_link_hash_defined:
3870 case bfd_link_hash_defweak:
3871 return h->root.u.def.section;
3873 case bfd_link_hash_common:
3874 return h->root.u.c.p->section;
3876 default:
3877 break;
3881 else
3882 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3884 return NULL;
3887 /* Update the got entry reference counts for the section being removed. */
3889 static bfd_boolean
3890 elf32_arm_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
3891 struct bfd_link_info * info ATTRIBUTE_UNUSED,
3892 asection * sec ATTRIBUTE_UNUSED,
3893 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
3895 Elf_Internal_Shdr *symtab_hdr;
3896 struct elf_link_hash_entry **sym_hashes;
3897 bfd_signed_vma *local_got_refcounts;
3898 const Elf_Internal_Rela *rel, *relend;
3899 unsigned long r_symndx;
3900 struct elf_link_hash_entry *h;
3901 struct elf32_arm_link_hash_table * globals;
3903 globals = elf32_arm_hash_table (info);
3905 elf_section_data (sec)->local_dynrel = NULL;
3907 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3908 sym_hashes = elf_sym_hashes (abfd);
3909 local_got_refcounts = elf_local_got_refcounts (abfd);
3911 relend = relocs + sec->reloc_count;
3912 for (rel = relocs; rel < relend; rel++)
3914 int r_type;
3916 r_type = ELF32_R_TYPE (rel->r_info);
3917 #ifndef OLD_ARM_ABI
3918 r_type = arm_real_reloc_type (globals, r_type);
3919 #endif
3920 switch (r_type)
3922 case R_ARM_GOT32:
3923 #ifndef OLD_ARM_ABI
3924 case R_ARM_GOT_PREL:
3925 #endif
3926 r_symndx = ELF32_R_SYM (rel->r_info);
3927 if (r_symndx >= symtab_hdr->sh_info)
3929 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3930 if (h->got.refcount > 0)
3931 h->got.refcount -= 1;
3933 else if (local_got_refcounts != NULL)
3935 if (local_got_refcounts[r_symndx] > 0)
3936 local_got_refcounts[r_symndx] -= 1;
3938 break;
3940 case R_ARM_ABS32:
3941 case R_ARM_REL32:
3942 case R_ARM_PC24:
3943 case R_ARM_PLT32:
3944 #ifndef OLD_ARM_ABI
3945 case R_ARM_CALL:
3946 case R_ARM_JUMP24:
3947 case R_ARM_PREL31:
3948 #endif
3949 case R_ARM_THM_PC22:
3950 /* Should the interworking branches be here also? */
3952 r_symndx = ELF32_R_SYM (rel->r_info);
3953 if (r_symndx >= symtab_hdr->sh_info)
3955 struct elf32_arm_link_hash_entry *eh;
3956 struct elf32_arm_relocs_copied **pp;
3957 struct elf32_arm_relocs_copied *p;
3959 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3960 eh = (struct elf32_arm_link_hash_entry *) h;
3962 if (h->plt.refcount > 0)
3964 h->plt.refcount -= 1;
3965 if (ELF32_R_TYPE (rel->r_info) == R_ARM_THM_PC22)
3966 eh->plt_thumb_refcount--;
3969 if (r_type == R_ARM_ABS32
3970 || r_type == R_ARM_REL32)
3972 for (pp = &eh->relocs_copied; (p = *pp) != NULL;
3973 pp = &p->next)
3974 if (p->section == sec)
3976 p->count -= 1;
3977 if (p->count == 0)
3978 *pp = p->next;
3979 break;
3983 break;
3985 default:
3986 break;
3990 return TRUE;
3993 /* Look through the relocs for a section during the first phase. */
3995 static bfd_boolean
3996 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
3997 asection *sec, const Elf_Internal_Rela *relocs)
3999 Elf_Internal_Shdr *symtab_hdr;
4000 struct elf_link_hash_entry **sym_hashes;
4001 struct elf_link_hash_entry **sym_hashes_end;
4002 const Elf_Internal_Rela *rel;
4003 const Elf_Internal_Rela *rel_end;
4004 bfd *dynobj;
4005 asection *sreloc;
4006 bfd_vma *local_got_offsets;
4007 struct elf32_arm_link_hash_table *htab;
4009 if (info->relocatable)
4010 return TRUE;
4012 htab = elf32_arm_hash_table (info);
4013 sreloc = NULL;
4015 dynobj = elf_hash_table (info)->dynobj;
4016 local_got_offsets = elf_local_got_offsets (abfd);
4018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4019 sym_hashes = elf_sym_hashes (abfd);
4020 sym_hashes_end = sym_hashes
4021 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4023 if (!elf_bad_symtab (abfd))
4024 sym_hashes_end -= symtab_hdr->sh_info;
4026 rel_end = relocs + sec->reloc_count;
4027 for (rel = relocs; rel < rel_end; rel++)
4029 struct elf_link_hash_entry *h;
4030 struct elf32_arm_link_hash_entry *eh;
4031 unsigned long r_symndx;
4032 int r_type;
4034 r_symndx = ELF32_R_SYM (rel->r_info);
4035 r_type = ELF32_R_TYPE (rel->r_info);
4036 #ifndef OLD_ARM_ABI
4037 r_type = arm_real_reloc_type (htab, r_type);
4038 #endif
4039 if (r_symndx < symtab_hdr->sh_info)
4040 h = NULL;
4041 else
4042 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4044 eh = (struct elf32_arm_link_hash_entry *) h;
4046 switch (r_type)
4048 case R_ARM_GOT32:
4049 #ifndef OLD_ARM_ABI
4050 case R_ARM_GOT_PREL:
4051 #endif
4052 /* This symbol requires a global offset table entry. */
4053 if (h != NULL)
4055 h->got.refcount++;
4057 else
4059 bfd_signed_vma *local_got_refcounts;
4061 /* This is a global offset table entry for a local symbol. */
4062 local_got_refcounts = elf_local_got_refcounts (abfd);
4063 if (local_got_refcounts == NULL)
4065 bfd_size_type size;
4067 size = symtab_hdr->sh_info;
4068 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4069 local_got_refcounts = bfd_zalloc (abfd, size);
4070 if (local_got_refcounts == NULL)
4071 return FALSE;
4072 elf_local_got_refcounts (abfd) = local_got_refcounts;
4074 local_got_refcounts[r_symndx] += 1;
4076 if (r_type == R_ARM_GOT32)
4077 break;
4078 /* Fall through. */
4080 case R_ARM_GOTOFF:
4081 case R_ARM_GOTPC:
4082 if (htab->sgot == NULL)
4084 if (htab->root.dynobj == NULL)
4085 htab->root.dynobj = abfd;
4086 if (!create_got_section (htab->root.dynobj, info))
4087 return FALSE;
4089 break;
4091 case R_ARM_ABS32:
4092 case R_ARM_REL32:
4093 case R_ARM_PC24:
4094 case R_ARM_PLT32:
4095 #ifndef OLD_ARM_ABI
4096 case R_ARM_CALL:
4097 case R_ARM_JUMP24:
4098 case R_ARM_PREL31:
4099 #endif
4100 case R_ARM_THM_PC22:
4101 /* Should the interworking branches be listed here? */
4102 if (h != NULL)
4104 /* If this reloc is in a read-only section, we might
4105 need a copy reloc. We can't check reliably at this
4106 stage whether the section is read-only, as input
4107 sections have not yet been mapped to output sections.
4108 Tentatively set the flag for now, and correct in
4109 adjust_dynamic_symbol. */
4110 if (!info->shared)
4111 h->non_got_ref = 1;
4113 /* We may need a .plt entry if the function this reloc
4114 refers to is in a different object. We can't tell for
4115 sure yet, because something later might force the
4116 symbol local. */
4117 if (r_type == R_ARM_PC24
4118 #ifndef OLD_ARM_ABI
4119 || r_type == R_ARM_CALL
4120 || r_type == R_ARM_JUMP24
4121 || r_type == R_ARM_PREL31
4122 #endif
4123 || r_type == R_ARM_PLT32
4124 || r_type == R_ARM_THM_PC22)
4125 h->needs_plt = 1;
4127 /* If we create a PLT entry, this relocation will reference
4128 it, even if it's an ABS32 relocation. */
4129 h->plt.refcount += 1;
4131 if (r_type == R_ARM_THM_PC22)
4132 eh->plt_thumb_refcount += 1;
4135 /* If we are creating a shared library, and this is a reloc
4136 against a global symbol, or a non PC relative reloc
4137 against a local symbol, then we need to copy the reloc
4138 into the shared library. However, if we are linking with
4139 -Bsymbolic, we do not need to copy a reloc against a
4140 global symbol which is defined in an object we are
4141 including in the link (i.e., DEF_REGULAR is set). At
4142 this point we have not seen all the input files, so it is
4143 possible that DEF_REGULAR is not set now but will be set
4144 later (it is never cleared). We account for that
4145 possibility below by storing information in the
4146 relocs_copied field of the hash table entry. */
4147 if (info->shared
4148 && (sec->flags & SEC_ALLOC) != 0
4149 && ((r_type != R_ARM_PC24
4150 && r_type != R_ARM_PLT32
4151 #ifndef OLD_ARM_ABI
4152 && r_type != R_ARM_CALL
4153 && r_type != R_ARM_JUMP24
4154 && r_type != R_ARM_PREL31
4155 #endif
4156 && r_type != R_ARM_REL32
4157 && r_type != R_ARM_THM_PC22)
4158 || (h != NULL
4159 && (! info->symbolic
4160 || !h->def_regular))))
4162 struct elf32_arm_relocs_copied *p, **head;
4164 /* When creating a shared object, we must copy these
4165 reloc types into the output file. We create a reloc
4166 section in dynobj and make room for this reloc. */
4167 if (sreloc == NULL)
4169 const char * name;
4171 name = (bfd_elf_string_from_elf_section
4172 (abfd,
4173 elf_elfheader (abfd)->e_shstrndx,
4174 elf_section_data (sec)->rel_hdr.sh_name));
4175 if (name == NULL)
4176 return FALSE;
4178 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
4179 && strcmp (bfd_get_section_name (abfd, sec),
4180 name + 4) == 0);
4182 sreloc = bfd_get_section_by_name (dynobj, name);
4183 if (sreloc == NULL)
4185 flagword flags;
4187 sreloc = bfd_make_section (dynobj, name);
4188 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4189 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4190 if ((sec->flags & SEC_ALLOC) != 0
4191 /* BPABI objects never have dynamic
4192 relocations mapped. */
4193 && !htab->symbian_p)
4194 flags |= SEC_ALLOC | SEC_LOAD;
4195 if (sreloc == NULL
4196 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4197 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4198 return FALSE;
4201 elf_section_data (sec)->sreloc = sreloc;
4204 /* If this is a global symbol, we count the number of
4205 relocations we need for this symbol. */
4206 if (h != NULL)
4208 head = &((struct elf32_arm_link_hash_entry *) h)->relocs_copied;
4210 else
4212 /* Track dynamic relocs needed for local syms too.
4213 We really need local syms available to do this
4214 easily. Oh well. */
4216 asection *s;
4217 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4218 sec, r_symndx);
4219 if (s == NULL)
4220 return FALSE;
4222 head = ((struct elf32_arm_relocs_copied **)
4223 &elf_section_data (s)->local_dynrel);
4226 p = *head;
4227 if (p == NULL || p->section != sec)
4229 bfd_size_type amt = sizeof *p;
4231 p = bfd_alloc (htab->root.dynobj, amt);
4232 if (p == NULL)
4233 return FALSE;
4234 p->next = *head;
4235 *head = p;
4236 p->section = sec;
4237 p->count = 0;
4240 if (r_type == R_ARM_ABS32
4241 || r_type == R_ARM_REL32)
4242 p->count += 1;
4244 break;
4246 /* This relocation describes the C++ object vtable hierarchy.
4247 Reconstruct it for later use during GC. */
4248 case R_ARM_GNU_VTINHERIT:
4249 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4250 return FALSE;
4251 break;
4253 /* This relocation describes which C++ vtable entries are actually
4254 used. Record for later use during GC. */
4255 case R_ARM_GNU_VTENTRY:
4256 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4257 return FALSE;
4258 break;
4262 return TRUE;
4265 static bfd_boolean
4266 is_arm_mapping_symbol_name (const char * name)
4268 return (name != NULL)
4269 && (name[0] == '$')
4270 && ((name[1] == 'a') || (name[1] == 't') || (name[1] == 'd'))
4271 && (name[2] == 0);
4274 /* Treat mapping symbols as special target symbols. */
4276 static bfd_boolean
4277 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
4279 return is_arm_mapping_symbol_name (sym->name);
4282 /* This is a copy of elf_find_function() from elf.c except that
4283 ARM mapping symbols are ignored when looking for function names
4284 and STT_ARM_TFUNC is considered to a function type. */
4286 static bfd_boolean
4287 arm_elf_find_function (bfd * abfd ATTRIBUTE_UNUSED,
4288 asection * section,
4289 asymbol ** symbols,
4290 bfd_vma offset,
4291 const char ** filename_ptr,
4292 const char ** functionname_ptr)
4294 const char * filename = NULL;
4295 asymbol * func = NULL;
4296 bfd_vma low_func = 0;
4297 asymbol ** p;
4299 for (p = symbols; *p != NULL; p++)
4301 elf_symbol_type *q;
4303 q = (elf_symbol_type *) *p;
4305 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4307 default:
4308 break;
4309 case STT_FILE:
4310 filename = bfd_asymbol_name (&q->symbol);
4311 break;
4312 case STT_FUNC:
4313 case STT_ARM_TFUNC:
4314 /* Skip $a and $t symbols. */
4315 if ((q->symbol.flags & BSF_LOCAL)
4316 && is_arm_mapping_symbol_name (q->symbol.name))
4317 continue;
4318 /* Fall through. */
4319 case STT_NOTYPE:
4320 if (bfd_get_section (&q->symbol) == section
4321 && q->symbol.value >= low_func
4322 && q->symbol.value <= offset)
4324 func = (asymbol *) q;
4325 low_func = q->symbol.value;
4327 break;
4331 if (func == NULL)
4332 return FALSE;
4334 if (filename_ptr)
4335 *filename_ptr = filename;
4336 if (functionname_ptr)
4337 *functionname_ptr = bfd_asymbol_name (func);
4339 return TRUE;
4343 /* Find the nearest line to a particular section and offset, for error
4344 reporting. This code is a duplicate of the code in elf.c, except
4345 that it uses arm_elf_find_function. */
4347 static bfd_boolean
4348 elf32_arm_find_nearest_line (bfd * abfd,
4349 asection * section,
4350 asymbol ** symbols,
4351 bfd_vma offset,
4352 const char ** filename_ptr,
4353 const char ** functionname_ptr,
4354 unsigned int * line_ptr)
4356 bfd_boolean found = FALSE;
4358 /* We skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain uses it. */
4360 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4361 filename_ptr, functionname_ptr,
4362 line_ptr, 0,
4363 & elf_tdata (abfd)->dwarf2_find_line_info))
4365 if (!*functionname_ptr)
4366 arm_elf_find_function (abfd, section, symbols, offset,
4367 *filename_ptr ? NULL : filename_ptr,
4368 functionname_ptr);
4370 return TRUE;
4373 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4374 & found, filename_ptr,
4375 functionname_ptr, line_ptr,
4376 & elf_tdata (abfd)->line_info))
4377 return FALSE;
4379 if (found && (*functionname_ptr || *line_ptr))
4380 return TRUE;
4382 if (symbols == NULL)
4383 return FALSE;
4385 if (! arm_elf_find_function (abfd, section, symbols, offset,
4386 filename_ptr, functionname_ptr))
4387 return FALSE;
4389 *line_ptr = 0;
4390 return TRUE;
4393 /* Adjust a symbol defined by a dynamic object and referenced by a
4394 regular object. The current definition is in some section of the
4395 dynamic object, but we're not including those sections. We have to
4396 change the definition to something the rest of the link can
4397 understand. */
4399 static bfd_boolean
4400 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
4401 struct elf_link_hash_entry * h)
4403 bfd * dynobj;
4404 asection * s;
4405 unsigned int power_of_two;
4406 struct elf32_arm_link_hash_entry * eh;
4408 dynobj = elf_hash_table (info)->dynobj;
4410 /* Make sure we know what is going on here. */
4411 BFD_ASSERT (dynobj != NULL
4412 && (h->needs_plt
4413 || h->u.weakdef != NULL
4414 || (h->def_dynamic
4415 && h->ref_regular
4416 && !h->def_regular)));
4418 eh = (struct elf32_arm_link_hash_entry *) h;
4420 /* If this is a function, put it in the procedure linkage table. We
4421 will fill in the contents of the procedure linkage table later,
4422 when we know the address of the .got section. */
4423 if (h->type == STT_FUNC || h->type == STT_ARM_TFUNC
4424 || h->needs_plt)
4426 if (h->plt.refcount <= 0
4427 || SYMBOL_CALLS_LOCAL (info, h)
4428 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4429 && h->root.type == bfd_link_hash_undefweak))
4431 /* This case can occur if we saw a PLT32 reloc in an input
4432 file, but the symbol was never referred to by a dynamic
4433 object, or if all references were garbage collected. In
4434 such a case, we don't actually need to build a procedure
4435 linkage table, and we can just do a PC24 reloc instead. */
4436 h->plt.offset = (bfd_vma) -1;
4437 eh->plt_thumb_refcount = 0;
4438 h->needs_plt = 0;
4441 return TRUE;
4443 else
4445 /* It's possible that we incorrectly decided a .plt reloc was
4446 needed for an R_ARM_PC24 or similar reloc to a non-function sym
4447 in check_relocs. We can't decide accurately between function
4448 and non-function syms in check-relocs; Objects loaded later in
4449 the link may change h->type. So fix it now. */
4450 h->plt.offset = (bfd_vma) -1;
4451 eh->plt_thumb_refcount = 0;
4454 /* If this is a weak symbol, and there is a real definition, the
4455 processor independent code will have arranged for us to see the
4456 real definition first, and we can just use the same value. */
4457 if (h->u.weakdef != NULL)
4459 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4460 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4461 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4462 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4463 return TRUE;
4466 /* This is a reference to a symbol defined by a dynamic object which
4467 is not a function. */
4469 /* If we are creating a shared library, we must presume that the
4470 only references to the symbol are via the global offset table.
4471 For such cases we need not do anything here; the relocations will
4472 be handled correctly by relocate_section. */
4473 if (info->shared)
4474 return TRUE;
4476 /* We must allocate the symbol in our .dynbss section, which will
4477 become part of the .bss section of the executable. There will be
4478 an entry for this symbol in the .dynsym section. The dynamic
4479 object will contain position independent code, so all references
4480 from the dynamic object to this symbol will go through the global
4481 offset table. The dynamic linker will use the .dynsym entry to
4482 determine the address it must put in the global offset table, so
4483 both the dynamic object and the regular object will refer to the
4484 same memory location for the variable. */
4485 s = bfd_get_section_by_name (dynobj, ".dynbss");
4486 BFD_ASSERT (s != NULL);
4488 /* We must generate a R_ARM_COPY reloc to tell the dynamic linker to
4489 copy the initial value out of the dynamic object and into the
4490 runtime process image. We need to remember the offset into the
4491 .rel.bss section we are going to use. */
4492 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4494 asection *srel;
4496 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
4497 BFD_ASSERT (srel != NULL);
4498 srel->size += sizeof (Elf32_External_Rel);
4499 h->needs_copy = 1;
4502 /* We need to figure out the alignment required for this symbol. I
4503 have no idea how ELF linkers handle this. */
4504 power_of_two = bfd_log2 (h->size);
4505 if (power_of_two > 3)
4506 power_of_two = 3;
4508 /* Apply the required alignment. */
4509 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4510 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4512 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4513 return FALSE;
4516 /* Define the symbol as being at this point in the section. */
4517 h->root.u.def.section = s;
4518 h->root.u.def.value = s->size;
4520 /* Increment the section size to make room for the symbol. */
4521 s->size += h->size;
4523 return TRUE;
4526 /* Allocate space in .plt, .got and associated reloc sections for
4527 dynamic relocs. */
4529 static bfd_boolean
4530 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
4532 struct bfd_link_info *info;
4533 struct elf32_arm_link_hash_table *htab;
4534 struct elf32_arm_link_hash_entry *eh;
4535 struct elf32_arm_relocs_copied *p;
4537 eh = (struct elf32_arm_link_hash_entry *) h;
4539 if (h->root.type == bfd_link_hash_indirect)
4540 return TRUE;
4542 if (h->root.type == bfd_link_hash_warning)
4543 /* When warning symbols are created, they **replace** the "real"
4544 entry in the hash table, thus we never get to see the real
4545 symbol in a hash traversal. So look at it now. */
4546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4548 info = (struct bfd_link_info *) inf;
4549 htab = elf32_arm_hash_table (info);
4551 if (htab->root.dynamic_sections_created
4552 && h->plt.refcount > 0)
4554 /* Make sure this symbol is output as a dynamic symbol.
4555 Undefined weak syms won't yet be marked as dynamic. */
4556 if (h->dynindx == -1
4557 && !h->forced_local)
4559 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4560 return FALSE;
4563 if (info->shared
4564 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
4566 asection *s = htab->splt;
4568 /* If this is the first .plt entry, make room for the special
4569 first entry. */
4570 if (s->size == 0)
4571 s->size += htab->plt_header_size;
4573 h->plt.offset = s->size;
4575 /* If we will insert a Thumb trampoline before this PLT, leave room
4576 for it. */
4577 if (!htab->symbian_p && eh->plt_thumb_refcount > 0)
4579 h->plt.offset += PLT_THUMB_STUB_SIZE;
4580 s->size += PLT_THUMB_STUB_SIZE;
4583 /* If this symbol is not defined in a regular file, and we are
4584 not generating a shared library, then set the symbol to this
4585 location in the .plt. This is required to make function
4586 pointers compare as equal between the normal executable and
4587 the shared library. */
4588 if (! info->shared
4589 && !h->def_regular)
4591 h->root.u.def.section = s;
4592 h->root.u.def.value = h->plt.offset;
4594 /* Make sure the function is not marked as Thumb, in case
4595 it is the target of an ABS32 relocation, which will
4596 point to the PLT entry. */
4597 if (ELF_ST_TYPE (h->type) == STT_ARM_TFUNC)
4598 h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
4601 /* Make room for this entry. */
4602 s->size += htab->plt_entry_size;
4604 if (!htab->symbian_p)
4606 /* We also need to make an entry in the .got.plt section, which
4607 will be placed in the .got section by the linker script. */
4608 eh->plt_got_offset = htab->sgotplt->size;
4609 htab->sgotplt->size += 4;
4612 /* We also need to make an entry in the .rel.plt section. */
4613 htab->srelplt->size += sizeof (Elf32_External_Rel);
4615 else
4617 h->plt.offset = (bfd_vma) -1;
4618 h->needs_plt = 0;
4621 else
4623 h->plt.offset = (bfd_vma) -1;
4624 h->needs_plt = 0;
4627 if (h->got.refcount > 0)
4629 asection *s;
4630 bfd_boolean dyn;
4632 /* Make sure this symbol is output as a dynamic symbol.
4633 Undefined weak syms won't yet be marked as dynamic. */
4634 if (h->dynindx == -1
4635 && !h->forced_local)
4637 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4638 return FALSE;
4641 if (!htab->symbian_p)
4643 s = htab->sgot;
4644 h->got.offset = s->size;
4645 s->size += 4;
4646 dyn = htab->root.dynamic_sections_created;
4647 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4648 || h->root.type != bfd_link_hash_undefweak)
4649 && (info->shared
4650 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
4651 htab->srelgot->size += sizeof (Elf32_External_Rel);
4654 else
4655 h->got.offset = (bfd_vma) -1;
4657 if (eh->relocs_copied == NULL)
4658 return TRUE;
4660 /* In the shared -Bsymbolic case, discard space allocated for
4661 dynamic pc-relative relocs against symbols which turn out to be
4662 defined in regular objects. For the normal shared case, discard
4663 space for pc-relative relocs that have become local due to symbol
4664 visibility changes. */
4666 if (info->shared)
4668 /* Discard relocs on undefined weak syms with non-default
4669 visibility. */
4670 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4671 && h->root.type == bfd_link_hash_undefweak)
4672 eh->relocs_copied = NULL;
4674 else
4676 /* For the non-shared case, discard space for relocs against
4677 symbols which turn out to need copy relocs or are not
4678 dynamic. */
4680 if (!h->non_got_ref
4681 && ((h->def_dynamic
4682 && !h->def_regular)
4683 || (htab->root.dynamic_sections_created
4684 && (h->root.type == bfd_link_hash_undefweak
4685 || h->root.type == bfd_link_hash_undefined))))
4687 /* Make sure this symbol is output as a dynamic symbol.
4688 Undefined weak syms won't yet be marked as dynamic. */
4689 if (h->dynindx == -1
4690 && !h->forced_local)
4692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4693 return FALSE;
4696 /* If that succeeded, we know we'll be keeping all the
4697 relocs. */
4698 if (h->dynindx != -1)
4699 goto keep;
4702 eh->relocs_copied = NULL;
4704 keep: ;
4707 /* Finally, allocate space. */
4708 for (p = eh->relocs_copied; p != NULL; p = p->next)
4710 asection *sreloc = elf_section_data (p->section)->sreloc;
4711 sreloc->size += p->count * sizeof (Elf32_External_Rel);
4714 return TRUE;
4717 /* Find any dynamic relocs that apply to read-only sections. */
4719 static bfd_boolean
4720 elf32_arm_readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
4722 struct elf32_arm_link_hash_entry *eh;
4723 struct elf32_arm_relocs_copied *p;
4725 if (h->root.type == bfd_link_hash_warning)
4726 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4728 eh = (struct elf32_arm_link_hash_entry *) h;
4729 for (p = eh->relocs_copied; p != NULL; p = p->next)
4731 asection *s = p->section;
4733 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4735 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4737 info->flags |= DF_TEXTREL;
4739 /* Not an error, just cut short the traversal. */
4740 return FALSE;
4743 return TRUE;
4746 /* Set the sizes of the dynamic sections. */
4748 static bfd_boolean
4749 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
4750 struct bfd_link_info * info)
4752 bfd * dynobj;
4753 asection * s;
4754 bfd_boolean plt;
4755 bfd_boolean relocs;
4756 bfd *ibfd;
4757 struct elf32_arm_link_hash_table *htab;
4759 htab = elf32_arm_hash_table (info);
4760 dynobj = elf_hash_table (info)->dynobj;
4761 BFD_ASSERT (dynobj != NULL);
4763 if (elf_hash_table (info)->dynamic_sections_created)
4765 /* Set the contents of the .interp section to the interpreter. */
4766 if (info->executable)
4768 s = bfd_get_section_by_name (dynobj, ".interp");
4769 BFD_ASSERT (s != NULL);
4770 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4771 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4775 /* Set up .got offsets for local syms, and space for local dynamic
4776 relocs. */
4777 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4779 bfd_signed_vma *local_got;
4780 bfd_signed_vma *end_local_got;
4781 char *local_tls_type;
4782 bfd_size_type locsymcount;
4783 Elf_Internal_Shdr *symtab_hdr;
4784 asection *srel;
4786 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4787 continue;
4789 for (s = ibfd->sections; s != NULL; s = s->next)
4791 struct elf32_arm_relocs_copied *p;
4793 for (p = *((struct elf32_arm_relocs_copied **)
4794 &elf_section_data (s)->local_dynrel);
4795 p != NULL;
4796 p = p->next)
4798 if (!bfd_is_abs_section (p->section)
4799 && bfd_is_abs_section (p->section->output_section))
4801 /* Input section has been discarded, either because
4802 it is a copy of a linkonce section or due to
4803 linker script /DISCARD/, so we'll be discarding
4804 the relocs too. */
4806 else if (p->count != 0)
4808 srel = elf_section_data (p->section)->sreloc;
4809 srel->size += p->count * sizeof (Elf32_External_Rel);
4810 if ((p->section->output_section->flags & SEC_READONLY) != 0)
4811 info->flags |= DF_TEXTREL;
4816 local_got = elf_local_got_refcounts (ibfd);
4817 if (!local_got)
4818 continue;
4820 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4821 locsymcount = symtab_hdr->sh_info;
4822 end_local_got = local_got + locsymcount;
4823 s = htab->sgot;
4824 srel = htab->srelgot;
4825 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
4827 if (*local_got > 0)
4829 *local_got = s->size;
4830 s->size += 4;
4831 if (info->shared)
4832 srel->size += sizeof (Elf32_External_Rel);
4834 else
4835 *local_got = (bfd_vma) -1;
4839 /* Allocate global sym .plt and .got entries, and space for global
4840 sym dynamic relocs. */
4841 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
4843 /* The check_relocs and adjust_dynamic_symbol entry points have
4844 determined the sizes of the various dynamic sections. Allocate
4845 memory for them. */
4846 plt = FALSE;
4847 relocs = FALSE;
4848 for (s = dynobj->sections; s != NULL; s = s->next)
4850 const char * name;
4851 bfd_boolean strip;
4853 if ((s->flags & SEC_LINKER_CREATED) == 0)
4854 continue;
4856 /* It's OK to base decisions on the section name, because none
4857 of the dynobj section names depend upon the input files. */
4858 name = bfd_get_section_name (dynobj, s);
4860 strip = FALSE;
4862 if (strcmp (name, ".plt") == 0)
4864 if (s->size == 0)
4866 /* Strip this section if we don't need it; see the
4867 comment below. */
4868 strip = TRUE;
4870 else
4872 /* Remember whether there is a PLT. */
4873 plt = TRUE;
4876 else if (strncmp (name, ".rel", 4) == 0)
4878 if (s->size == 0)
4880 /* If we don't need this section, strip it from the
4881 output file. This is mostly to handle .rel.bss and
4882 .rel.plt. We must create both sections in
4883 create_dynamic_sections, because they must be created
4884 before the linker maps input sections to output
4885 sections. The linker does that before
4886 adjust_dynamic_symbol is called, and it is that
4887 function which decides whether anything needs to go
4888 into these sections. */
4889 strip = TRUE;
4891 else
4893 /* Remember whether there are any reloc sections other
4894 than .rel.plt. */
4895 if (strcmp (name, ".rel.plt") != 0)
4896 relocs = TRUE;
4898 /* We use the reloc_count field as a counter if we need
4899 to copy relocs into the output file. */
4900 s->reloc_count = 0;
4903 else if (strncmp (name, ".got", 4) != 0)
4905 /* It's not one of our sections, so don't allocate space. */
4906 continue;
4909 if (strip)
4911 _bfd_strip_section_from_output (info, s);
4912 continue;
4915 /* Allocate memory for the section contents. */
4916 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4917 if (s->contents == NULL && s->size != 0)
4918 return FALSE;
4921 if (elf_hash_table (info)->dynamic_sections_created)
4923 /* Add some entries to the .dynamic section. We fill in the
4924 values later, in elf32_arm_finish_dynamic_sections, but we
4925 must add the entries now so that we get the correct size for
4926 the .dynamic section. The DT_DEBUG entry is filled in by the
4927 dynamic linker and used by the debugger. */
4928 #define add_dynamic_entry(TAG, VAL) \
4929 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4931 if (!info->shared)
4933 if (!add_dynamic_entry (DT_DEBUG, 0))
4934 return FALSE;
4937 if (plt)
4939 if ( !add_dynamic_entry (DT_PLTGOT, 0)
4940 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4941 || !add_dynamic_entry (DT_PLTREL, DT_REL)
4942 || !add_dynamic_entry (DT_JMPREL, 0))
4943 return FALSE;
4946 if (relocs)
4948 if ( !add_dynamic_entry (DT_REL, 0)
4949 || !add_dynamic_entry (DT_RELSZ, 0)
4950 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
4951 return FALSE;
4954 /* If any dynamic relocs apply to a read-only section,
4955 then we need a DT_TEXTREL entry. */
4956 if ((info->flags & DF_TEXTREL) == 0)
4957 elf_link_hash_traverse (&htab->root, elf32_arm_readonly_dynrelocs,
4958 (PTR) info);
4960 if ((info->flags & DF_TEXTREL) != 0)
4962 if (!add_dynamic_entry (DT_TEXTREL, 0))
4963 return FALSE;
4964 info->flags |= DF_TEXTREL;
4967 #undef add_synamic_entry
4969 return TRUE;
4972 /* Finish up dynamic symbol handling. We set the contents of various
4973 dynamic sections here. */
4975 static bfd_boolean
4976 elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct bfd_link_info * info,
4977 struct elf_link_hash_entry * h, Elf_Internal_Sym * sym)
4979 bfd * dynobj;
4980 struct elf32_arm_link_hash_table *htab;
4981 struct elf32_arm_link_hash_entry *eh;
4983 dynobj = elf_hash_table (info)->dynobj;
4984 htab = elf32_arm_hash_table (info);
4985 eh = (struct elf32_arm_link_hash_entry *) h;
4987 if (h->plt.offset != (bfd_vma) -1)
4989 asection * splt;
4990 asection * srel;
4991 bfd_byte *loc;
4992 bfd_vma plt_index;
4993 Elf_Internal_Rela rel;
4995 /* This symbol has an entry in the procedure linkage table. Set
4996 it up. */
4998 BFD_ASSERT (h->dynindx != -1);
5000 splt = bfd_get_section_by_name (dynobj, ".plt");
5001 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
5002 BFD_ASSERT (splt != NULL && srel != NULL);
5004 /* Fill in the entry in the procedure linkage table. */
5005 if (htab->symbian_p)
5007 unsigned i;
5008 for (i = 0; i < htab->plt_entry_size / 4; ++i)
5009 bfd_put_32 (output_bfd,
5010 elf32_arm_symbian_plt_entry[i],
5011 splt->contents + h->plt.offset + 4 * i);
5013 /* Fill in the entry in the .rel.plt section. */
5014 rel.r_offset = (splt->output_section->vma
5015 + splt->output_offset
5016 + h->plt.offset + 4 * (i - 1));
5017 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
5019 /* Get the index in the procedure linkage table which
5020 corresponds to this symbol. This is the index of this symbol
5021 in all the symbols for which we are making plt entries. The
5022 first entry in the procedure linkage table is reserved. */
5023 plt_index = ((h->plt.offset - htab->plt_header_size)
5024 / htab->plt_entry_size);
5026 else
5028 bfd_vma got_offset;
5029 bfd_vma got_displacement;
5030 asection * sgot;
5032 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5033 BFD_ASSERT (sgot != NULL);
5035 /* Get the offset into the .got.plt table of the entry that
5036 corresponds to this function. */
5037 got_offset = eh->plt_got_offset;
5039 /* Get the index in the procedure linkage table which
5040 corresponds to this symbol. This is the index of this symbol
5041 in all the symbols for which we are making plt entries. The
5042 first three entries in .got.plt are reserved; after that
5043 symbols appear in the same order as in .plt. */
5044 plt_index = (got_offset - 12) / 4;
5046 /* Calculate the displacement between the PLT slot and the
5047 entry in the GOT. The eight-byte offset accounts for the
5048 value produced by adding to pc in the first instruction
5049 of the PLT stub. */
5050 got_displacement = (sgot->output_section->vma
5051 + sgot->output_offset
5052 + got_offset
5053 - splt->output_section->vma
5054 - splt->output_offset
5055 - h->plt.offset
5056 - 8);
5058 BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
5060 if (eh->plt_thumb_refcount > 0)
5062 bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[0],
5063 splt->contents + h->plt.offset - 4);
5064 bfd_put_16 (output_bfd, elf32_arm_plt_thumb_stub[1],
5065 splt->contents + h->plt.offset - 2);
5068 bfd_put_32 (output_bfd, elf32_arm_plt_entry[0] | ((got_displacement & 0x0ff00000) >> 20),
5069 splt->contents + h->plt.offset + 0);
5070 bfd_put_32 (output_bfd, elf32_arm_plt_entry[1] | ((got_displacement & 0x000ff000) >> 12),
5071 splt->contents + h->plt.offset + 4);
5072 bfd_put_32 (output_bfd, elf32_arm_plt_entry[2] | (got_displacement & 0x00000fff),
5073 splt->contents + h->plt.offset + 8);
5074 #ifdef FOUR_WORD_PLT
5075 bfd_put_32 (output_bfd, elf32_arm_plt_entry[3],
5076 splt->contents + h->plt.offset + 12);
5077 #endif
5079 /* Fill in the entry in the global offset table. */
5080 bfd_put_32 (output_bfd,
5081 (splt->output_section->vma
5082 + splt->output_offset),
5083 sgot->contents + got_offset);
5085 /* Fill in the entry in the .rel.plt section. */
5086 rel.r_offset = (sgot->output_section->vma
5087 + sgot->output_offset
5088 + got_offset);
5089 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_JUMP_SLOT);
5092 loc = srel->contents + plt_index * sizeof (Elf32_External_Rel);
5093 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5095 if (!h->def_regular)
5097 /* Mark the symbol as undefined, rather than as defined in
5098 the .plt section. Leave the value alone. */
5099 sym->st_shndx = SHN_UNDEF;
5100 /* If the symbol is weak, we do need to clear the value.
5101 Otherwise, the PLT entry would provide a definition for
5102 the symbol even if the symbol wasn't defined anywhere,
5103 and so the symbol would never be NULL. */
5104 if (!h->ref_regular_nonweak)
5105 sym->st_value = 0;
5109 if (h->got.offset != (bfd_vma) -1)
5111 asection * sgot;
5112 asection * srel;
5113 Elf_Internal_Rela rel;
5114 bfd_byte *loc;
5116 /* This symbol has an entry in the global offset table. Set it
5117 up. */
5118 sgot = bfd_get_section_by_name (dynobj, ".got");
5119 srel = bfd_get_section_by_name (dynobj, ".rel.got");
5120 BFD_ASSERT (sgot != NULL && srel != NULL);
5122 rel.r_offset = (sgot->output_section->vma
5123 + sgot->output_offset
5124 + (h->got.offset &~ (bfd_vma) 1));
5126 /* If this is a static link, or it is a -Bsymbolic link and the
5127 symbol is defined locally or was forced to be local because
5128 of a version file, we just want to emit a RELATIVE reloc.
5129 The entry in the global offset table will already have been
5130 initialized in the relocate_section function. */
5131 if (info->shared
5132 && SYMBOL_REFERENCES_LOCAL (info, h))
5134 BFD_ASSERT((h->got.offset & 1) != 0);
5135 rel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
5137 else
5139 BFD_ASSERT((h->got.offset & 1) == 0);
5140 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5141 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
5144 loc = srel->contents + srel->reloc_count++ * sizeof (Elf32_External_Rel);
5145 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5148 if (h->needs_copy)
5150 asection * s;
5151 Elf_Internal_Rela rel;
5152 bfd_byte *loc;
5154 /* This symbol needs a copy reloc. Set it up. */
5155 BFD_ASSERT (h->dynindx != -1
5156 && (h->root.type == bfd_link_hash_defined
5157 || h->root.type == bfd_link_hash_defweak));
5159 s = bfd_get_section_by_name (h->root.u.def.section->owner,
5160 ".rel.bss");
5161 BFD_ASSERT (s != NULL);
5163 rel.r_offset = (h->root.u.def.value
5164 + h->root.u.def.section->output_section->vma
5165 + h->root.u.def.section->output_offset);
5166 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
5167 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rel);
5168 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5171 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5172 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5173 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5174 sym->st_shndx = SHN_ABS;
5176 return TRUE;
5179 /* Finish up the dynamic sections. */
5181 static bfd_boolean
5182 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
5184 bfd * dynobj;
5185 asection * sgot;
5186 asection * sdyn;
5188 dynobj = elf_hash_table (info)->dynobj;
5190 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5191 BFD_ASSERT (elf32_arm_hash_table (info)->symbian_p || sgot != NULL);
5192 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5194 if (elf_hash_table (info)->dynamic_sections_created)
5196 asection *splt;
5197 Elf32_External_Dyn *dyncon, *dynconend;
5198 struct elf32_arm_link_hash_table *htab;
5200 htab = elf32_arm_hash_table (info);
5201 splt = bfd_get_section_by_name (dynobj, ".plt");
5202 BFD_ASSERT (splt != NULL && sdyn != NULL);
5204 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5205 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5207 for (; dyncon < dynconend; dyncon++)
5209 Elf_Internal_Dyn dyn;
5210 const char * name;
5211 asection * s;
5213 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5215 switch (dyn.d_tag)
5217 unsigned int type;
5219 default:
5220 break;
5222 case DT_HASH:
5223 name = ".hash";
5224 goto get_vma_if_bpabi;
5225 case DT_STRTAB:
5226 name = ".dynstr";
5227 goto get_vma_if_bpabi;
5228 case DT_SYMTAB:
5229 name = ".dynsym";
5230 goto get_vma_if_bpabi;
5231 case DT_VERSYM:
5232 name = ".gnu.version";
5233 goto get_vma_if_bpabi;
5234 case DT_VERDEF:
5235 name = ".gnu.version_d";
5236 goto get_vma_if_bpabi;
5237 case DT_VERNEED:
5238 name = ".gnu.version_r";
5239 goto get_vma_if_bpabi;
5241 case DT_PLTGOT:
5242 name = ".got";
5243 goto get_vma;
5244 case DT_JMPREL:
5245 name = ".rel.plt";
5246 get_vma:
5247 s = bfd_get_section_by_name (output_bfd, name);
5248 BFD_ASSERT (s != NULL);
5249 if (!htab->symbian_p)
5250 dyn.d_un.d_ptr = s->vma;
5251 else
5252 /* In the BPABI, tags in the PT_DYNAMIC section point
5253 at the file offset, not the memory address, for the
5254 convenience of the post linker. */
5255 dyn.d_un.d_ptr = s->filepos;
5256 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5257 break;
5259 get_vma_if_bpabi:
5260 if (htab->symbian_p)
5261 goto get_vma;
5262 break;
5264 case DT_PLTRELSZ:
5265 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5266 BFD_ASSERT (s != NULL);
5267 dyn.d_un.d_val = s->size;
5268 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5269 break;
5271 case DT_RELSZ:
5272 if (!htab->symbian_p)
5274 /* My reading of the SVR4 ABI indicates that the
5275 procedure linkage table relocs (DT_JMPREL) should be
5276 included in the overall relocs (DT_REL). This is
5277 what Solaris does. However, UnixWare can not handle
5278 that case. Therefore, we override the DT_RELSZ entry
5279 here to make it not include the JMPREL relocs. Since
5280 the linker script arranges for .rel.plt to follow all
5281 other relocation sections, we don't have to worry
5282 about changing the DT_REL entry. */
5283 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
5284 if (s != NULL)
5285 dyn.d_un.d_val -= s->size;
5286 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5287 break;
5289 /* Fall through */
5291 case DT_REL:
5292 case DT_RELA:
5293 case DT_RELASZ:
5294 /* In the BPABI, the DT_REL tag must point at the file
5295 offset, not the VMA, of the first relocation
5296 section. So, we use code similar to that in
5297 elflink.c, but do not check for SHF_ALLOC on the
5298 relcoation section, since relocations sections are
5299 never allocated under the BPABI. The comments above
5300 about Unixware notwithstanding, we include all of the
5301 relocations here. */
5302 if (htab->symbian_p)
5304 unsigned int i;
5305 type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5306 ? SHT_REL : SHT_RELA);
5307 dyn.d_un.d_val = 0;
5308 for (i = 1; i < elf_numsections (output_bfd); i++)
5310 Elf_Internal_Shdr *hdr
5311 = elf_elfsections (output_bfd)[i];
5312 if (hdr->sh_type == type)
5314 if (dyn.d_tag == DT_RELSZ
5315 || dyn.d_tag == DT_RELASZ)
5316 dyn.d_un.d_val += hdr->sh_size;
5317 else if ((ufile_ptr) hdr->sh_offset
5318 <= dyn.d_un.d_val - 1)
5319 dyn.d_un.d_val = hdr->sh_offset;
5322 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5324 break;
5326 /* Set the bottom bit of DT_INIT/FINI if the
5327 corresponding function is Thumb. */
5328 case DT_INIT:
5329 name = info->init_function;
5330 goto get_sym;
5331 case DT_FINI:
5332 name = info->fini_function;
5333 get_sym:
5334 /* If it wasn't set by elf_bfd_final_link
5335 then there is nothing to adjust. */
5336 if (dyn.d_un.d_val != 0)
5338 struct elf_link_hash_entry * eh;
5340 eh = elf_link_hash_lookup (elf_hash_table (info), name,
5341 FALSE, FALSE, TRUE);
5342 if (eh != (struct elf_link_hash_entry *) NULL
5343 && ELF_ST_TYPE (eh->type) == STT_ARM_TFUNC)
5345 dyn.d_un.d_val |= 1;
5346 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5349 break;
5353 /* Fill in the first entry in the procedure linkage table. */
5354 if (splt->size > 0 && elf32_arm_hash_table (info)->plt_header_size)
5356 bfd_vma got_displacement;
5358 /* Calculate the displacement between the PLT slot and &GOT[0]. */
5359 got_displacement = (sgot->output_section->vma
5360 + sgot->output_offset
5361 - splt->output_section->vma
5362 - splt->output_offset
5363 - 16);
5365 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[0], splt->contents + 0);
5366 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[1], splt->contents + 4);
5367 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[2], splt->contents + 8);
5368 bfd_put_32 (output_bfd, elf32_arm_plt0_entry[3], splt->contents + 12);
5369 #ifdef FOUR_WORD_PLT
5370 /* The displacement value goes in the otherwise-unused last word of
5371 the second entry. */
5372 bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
5373 #else
5374 bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
5375 #endif
5378 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5379 really seem like the right value. */
5380 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5383 /* Fill in the first three entries in the global offset table. */
5384 if (sgot)
5386 if (sgot->size > 0)
5388 if (sdyn == NULL)
5389 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5390 else
5391 bfd_put_32 (output_bfd,
5392 sdyn->output_section->vma + sdyn->output_offset,
5393 sgot->contents);
5394 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5395 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5398 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5401 return TRUE;
5404 static void
5405 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
5407 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
5408 struct elf32_arm_link_hash_table *globals;
5410 i_ehdrp = elf_elfheader (abfd);
5412 i_ehdrp->e_ident[EI_OSABI] = ARM_ELF_OS_ABI_VERSION;
5413 i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
5415 if (link_info)
5417 globals = elf32_arm_hash_table (link_info);
5418 if (globals->byteswap_code)
5419 i_ehdrp->e_flags |= EF_ARM_BE8;
5423 static enum elf_reloc_type_class
5424 elf32_arm_reloc_type_class (const Elf_Internal_Rela *rela)
5426 switch ((int) ELF32_R_TYPE (rela->r_info))
5428 case R_ARM_RELATIVE:
5429 return reloc_class_relative;
5430 case R_ARM_JUMP_SLOT:
5431 return reloc_class_plt;
5432 case R_ARM_COPY:
5433 return reloc_class_copy;
5434 default:
5435 return reloc_class_normal;
5439 /* Set the right machine number for an Arm ELF file. */
5441 static bfd_boolean
5442 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
5444 if (hdr->sh_type == SHT_NOTE)
5445 *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
5447 return TRUE;
5450 static void
5451 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
5453 bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
5456 /* Return TRUE if this is an unwinding table entry. */
5458 static bfd_boolean
5459 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
5461 size_t len1, len2;
5463 len1 = sizeof (ELF_STRING_ARM_unwind) - 1;
5464 len2 = sizeof (ELF_STRING_ARM_unwind_once) - 1;
5465 return (strncmp (name, ELF_STRING_ARM_unwind, len1) == 0
5466 || strncmp (name, ELF_STRING_ARM_unwind_once, len2) == 0);
5470 /* Set the type and flags for an ARM section. We do this by
5471 the section name, which is a hack, but ought to work. */
5473 static bfd_boolean
5474 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
5476 const char * name;
5478 name = bfd_get_section_name (abfd, sec);
5480 if (is_arm_elf_unwind_section_name (abfd, name))
5482 hdr->sh_type = SHT_ARM_EXIDX;
5483 hdr->sh_flags |= SHF_LINK_ORDER;
5485 return TRUE;
5488 /* Handle an ARM specific section when reading an object file.
5489 This is called when elf.c finds a section with an unknown type. */
5491 static bfd_boolean
5492 elf32_arm_section_from_shdr (bfd *abfd,
5493 Elf_Internal_Shdr * hdr,
5494 const char *name)
5496 /* There ought to be a place to keep ELF backend specific flags, but
5497 at the moment there isn't one. We just keep track of the
5498 sections by their name, instead. Fortunately, the ABI gives
5499 names for all the ARM specific sections, so we will probably get
5500 away with this. */
5501 switch (hdr->sh_type)
5503 case SHT_ARM_EXIDX:
5504 break;
5506 default:
5507 return FALSE;
5510 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
5511 return FALSE;
5513 return TRUE;
5516 /* Called for each symbol. Builds a section map based on mapping symbols.
5517 Does not alter any of the symbols. */
5519 static bfd_boolean
5520 elf32_arm_output_symbol_hook (struct bfd_link_info *info,
5521 const char *name,
5522 Elf_Internal_Sym *elfsym,
5523 asection *input_sec,
5524 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5526 int mapcount;
5527 elf32_arm_section_map *map;
5528 struct elf32_arm_link_hash_table *globals;
5530 /* Only do this on final link. */
5531 if (info->relocatable)
5532 return TRUE;
5534 /* Only build a map if we need to byteswap code. */
5535 globals = elf32_arm_hash_table (info);
5536 if (!globals->byteswap_code)
5537 return TRUE;
5539 /* We only want mapping symbols. */
5540 if (! is_arm_mapping_symbol_name (name))
5541 return TRUE;
5543 mapcount = ++(elf32_arm_section_data (input_sec)->mapcount);
5544 map = elf32_arm_section_data (input_sec)->map;
5545 /* TODO: This may be inefficient, but we probably don't usually have many
5546 mapping symbols per section. */
5547 map = bfd_realloc (map, mapcount * sizeof (elf32_arm_section_map));
5548 elf32_arm_section_data (input_sec)->map = map;
5550 map[mapcount - 1].vma = elfsym->st_value;
5551 map[mapcount - 1].type = name[1];
5552 return TRUE;
5556 /* Allocate target specific section data. */
5558 static bfd_boolean
5559 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
5561 struct _arm_elf_section_data *sdata;
5562 bfd_size_type amt = sizeof (*sdata);
5564 sdata = bfd_zalloc (abfd, amt);
5565 if (sdata == NULL)
5566 return FALSE;
5567 sec->used_by_bfd = sdata;
5569 return _bfd_elf_new_section_hook (abfd, sec);
5573 /* Used to order a list of mapping symbols by address. */
5575 static int
5576 elf32_arm_compare_mapping (const void * a, const void * b)
5578 return ((const elf32_arm_section_map *) a)->vma
5579 > ((const elf32_arm_section_map *) b)->vma;
5583 /* Do code byteswapping. Return FALSE afterwards so that the section is
5584 written out as normal. */
5586 static bfd_boolean
5587 elf32_arm_write_section (bfd *output_bfd ATTRIBUTE_UNUSED, asection *sec,
5588 bfd_byte *contents)
5590 int mapcount;
5591 elf32_arm_section_map *map;
5592 bfd_vma ptr;
5593 bfd_vma end;
5594 bfd_vma offset;
5595 bfd_byte tmp;
5596 int i;
5598 mapcount = elf32_arm_section_data (sec)->mapcount;
5599 map = elf32_arm_section_data (sec)->map;
5601 if (mapcount == 0)
5602 return FALSE;
5604 qsort (map, mapcount, sizeof (elf32_arm_section_map),
5605 elf32_arm_compare_mapping);
5607 offset = sec->output_section->vma + sec->output_offset;
5608 ptr = map[0].vma - offset;
5609 for (i = 0; i < mapcount; i++)
5611 if (i == mapcount - 1)
5612 end = sec->size;
5613 else
5614 end = map[i + 1].vma - offset;
5616 switch (map[i].type)
5618 case 'a':
5619 /* Byte swap code words. */
5620 while (ptr + 3 < end)
5622 tmp = contents[ptr];
5623 contents[ptr] = contents[ptr + 3];
5624 contents[ptr + 3] = tmp;
5625 tmp = contents[ptr + 1];
5626 contents[ptr + 1] = contents[ptr + 2];
5627 contents[ptr + 2] = tmp;
5628 ptr += 4;
5630 break;
5632 case 't':
5633 /* Byte swap code halfwords. */
5634 while (ptr + 1 < end)
5636 tmp = contents[ptr];
5637 contents[ptr] = contents[ptr + 1];
5638 contents[ptr + 1] = tmp;
5639 ptr += 2;
5641 break;
5643 case 'd':
5644 /* Leave data alone. */
5645 break;
5647 ptr = end;
5649 free (map);
5650 return FALSE;
5653 /* Display STT_ARM_TFUNC symbols as functions. */
5655 static void
5656 elf32_arm_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5657 asymbol *asym)
5659 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5661 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_ARM_TFUNC)
5662 elfsym->symbol.flags |= BSF_FUNCTION;
5665 #define ELF_ARCH bfd_arch_arm
5666 #define ELF_MACHINE_CODE EM_ARM
5667 #ifdef __QNXTARGET__
5668 #define ELF_MAXPAGESIZE 0x1000
5669 #else
5670 #define ELF_MAXPAGESIZE 0x8000
5671 #endif
5673 #define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
5674 #define bfd_elf32_bfd_merge_private_bfd_data elf32_arm_merge_private_bfd_data
5675 #define bfd_elf32_bfd_set_private_flags elf32_arm_set_private_flags
5676 #define bfd_elf32_bfd_print_private_bfd_data elf32_arm_print_private_bfd_data
5677 #define bfd_elf32_bfd_link_hash_table_create elf32_arm_link_hash_table_create
5678 #define bfd_elf32_bfd_reloc_type_lookup elf32_arm_reloc_type_lookup
5679 #define bfd_elf32_find_nearest_line elf32_arm_find_nearest_line
5680 #define bfd_elf32_new_section_hook elf32_arm_new_section_hook
5681 #define bfd_elf32_bfd_is_target_special_symbol elf32_arm_is_target_special_symbol
5683 #define elf_backend_get_symbol_type elf32_arm_get_symbol_type
5684 #define elf_backend_gc_mark_hook elf32_arm_gc_mark_hook
5685 #define elf_backend_gc_sweep_hook elf32_arm_gc_sweep_hook
5686 #define elf_backend_check_relocs elf32_arm_check_relocs
5687 #define elf_backend_relocate_section elf32_arm_relocate_section
5688 #define elf_backend_write_section elf32_arm_write_section
5689 #define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
5690 #define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
5691 #define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
5692 #define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
5693 #define elf_backend_link_output_symbol_hook elf32_arm_output_symbol_hook
5694 #define elf_backend_size_dynamic_sections elf32_arm_size_dynamic_sections
5695 #define elf_backend_post_process_headers elf32_arm_post_process_headers
5696 #define elf_backend_reloc_type_class elf32_arm_reloc_type_class
5697 #define elf_backend_object_p elf32_arm_object_p
5698 #define elf_backend_section_flags elf32_arm_section_flags
5699 #define elf_backend_fake_sections elf32_arm_fake_sections
5700 #define elf_backend_section_from_shdr elf32_arm_section_from_shdr
5701 #define elf_backend_final_write_processing elf32_arm_final_write_processing
5702 #define elf_backend_copy_indirect_symbol elf32_arm_copy_indirect_symbol
5703 #define elf_backend_symbol_processing elf32_arm_symbol_processing
5705 #define elf_backend_can_refcount 1
5706 #define elf_backend_can_gc_sections 1
5707 #define elf_backend_plt_readonly 1
5708 #define elf_backend_want_got_plt 1
5709 #define elf_backend_want_plt_sym 0
5710 #if !USE_REL
5711 #define elf_backend_rela_normal 1
5712 #endif
5714 #define elf_backend_got_header_size 12
5716 #include "elf32-target.h"
5718 /* Symbian OS Targets */
5720 #undef TARGET_LITTLE_SYM
5721 #define TARGET_LITTLE_SYM bfd_elf32_littlearm_symbian_vec
5722 #undef TARGET_LITTLE_NAME
5723 #define TARGET_LITTLE_NAME "elf32-littlearm-symbian"
5724 #undef TARGET_BIG_SYM
5725 #define TARGET_BIG_SYM bfd_elf32_bigarm_symbian_vec
5726 #undef TARGET_BIG_NAME
5727 #define TARGET_BIG_NAME "elf32-bigarm-symbian"
5729 /* Like elf32_arm_link_hash_table_create -- but overrides
5730 appropriately for Symbian OS. */
5731 static struct bfd_link_hash_table *
5732 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
5734 struct bfd_link_hash_table *ret;
5736 ret = elf32_arm_link_hash_table_create (abfd);
5737 if (ret)
5739 struct elf32_arm_link_hash_table *htab
5740 = (struct elf32_arm_link_hash_table *)ret;
5741 /* There is no PLT header for Symbian OS. */
5742 htab->plt_header_size = 0;
5743 /* The PLT entries are each three instructions. */
5744 htab->plt_entry_size = 4 * NUM_ELEM (elf32_arm_symbian_plt_entry);
5745 htab->symbian_p = 1;
5747 return ret;
5750 /* In a BPABI executable, the dynamic linking sections do not go in
5751 the loadable read-only segment. The post-linker may wish to refer
5752 to these sections, but they are not part of the final program
5753 image. */
5754 static struct bfd_elf_special_section const
5755 elf32_arm_symbian_special_sections[]=
5757 { ".dynamic", 8, 0, SHT_DYNAMIC, 0 },
5758 { ".dynstr", 7, 0, SHT_STRTAB, 0 },
5759 { ".dynsym", 7, 0, SHT_DYNSYM, 0 },
5760 { ".got", 4, 0, SHT_PROGBITS, 0 },
5761 { ".hash", 5, 0, SHT_HASH, 0 },
5762 { NULL, 0, 0, 0, 0 }
5765 static void
5766 elf32_arm_symbian_begin_write_processing (bfd *abfd,
5767 struct bfd_link_info *link_info
5768 ATTRIBUTE_UNUSED)
5770 /* BPABI objects are never loaded directly by an OS kernel; they are
5771 processed by a postlinker first, into an OS-specific format. If
5772 the D_PAGED bit is set on the file, BFD will align segments on
5773 page boundaries, so that an OS can directly map the file. With
5774 BPABI objects, that just results in wasted space. In addition,
5775 because we clear the D_PAGED bit, map_sections_to_segments will
5776 recognize that the program headers should not be mapped into any
5777 loadable segment. */
5778 abfd->flags &= ~D_PAGED;
5781 static bfd_boolean
5782 elf32_arm_symbian_modify_segment_map (bfd *abfd,
5783 struct bfd_link_info *info
5784 ATTRIBUTE_UNUSED)
5786 struct elf_segment_map *m;
5787 asection *dynsec;
5789 /* BPABI shared libraries and executables should have a PT_DYNAMIC
5790 segment. However, because the .dynamic section is not marked
5791 with SEC_LOAD, the generic ELF code will not create such a
5792 segment. */
5793 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5794 if (dynsec)
5796 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5797 m->next = elf_tdata (abfd)->segment_map;
5798 elf_tdata (abfd)->segment_map = m;
5801 return TRUE;
5804 #undef elf32_bed
5805 #define elf32_bed elf32_arm_symbian_bed
5807 /* The dynamic sections are not allocated on SymbianOS; the postlinker
5808 will process them and then discard them. */
5809 #undef ELF_DYNAMIC_SEC_FLAGS
5810 #define ELF_DYNAMIC_SEC_FLAGS \
5811 (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
5813 #undef bfd_elf32_bfd_link_hash_table_create
5814 #define bfd_elf32_bfd_link_hash_table_create \
5815 elf32_arm_symbian_link_hash_table_create
5817 #undef elf_backend_special_sections
5818 #define elf_backend_special_sections elf32_arm_symbian_special_sections
5820 #undef elf_backend_begin_write_processing
5821 #define elf_backend_begin_write_processing \
5822 elf32_arm_symbian_begin_write_processing
5824 #undef elf_backend_modify_segment_map
5825 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
5827 /* There is no .got section for BPABI objects, and hence no header. */
5828 #undef elf_backend_got_header_size
5829 #define elf_backend_got_header_size 0
5831 /* Similarly, there is no .got.plt section. */
5832 #undef elf_backend_want_got_plt
5833 #define elf_backend_want_got_plt 0
5835 #include "elf32-target.h"