2001-02-08 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf32-i370.c
blobb8afdbac8070abfdd2c2910bbec20df5376fc898
1 /* i370-specific support for 32-bit ELF
2 Copyright 1994, 95, 96, 97, 98, 2000 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4 Hacked by Linas Vepstas for i370 linas@linas.org
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is based on a preliminary PowerPC ELF ABI.
23 But its been hacked on for the IBM 360/370 architectures.
24 Basically, the 31bit relocation works, and just about everything
25 else is a wild card. In particular, don't expect shared libs or
26 dynamic loading to work ... its never been tested ...
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/i370.h"
36 #define USE_RELA /* we want RELA relocations, not REL */
38 /* i370 relocations */
39 /* Note that there is really just one relocation that we currently
40 * support (and only one that we seem to need, at the moment), and
41 * that is the 31-bit address relocation. Note that the 370/390
42 * only supports a 31-bit (2GB) address space.
44 enum i370_reloc_type
46 R_I370_NONE = 0,
47 R_I370_ADDR31 = 1,
48 R_I370_ADDR32 = 2,
49 R_I370_ADDR16 = 3,
50 R_I370_REL31 = 4,
51 R_I370_REL32 = 5,
52 R_I370_ADDR12 = 6,
53 R_I370_REL12 = 7,
54 R_I370_ADDR8 = 8,
55 R_I370_REL8 = 9,
56 R_I370_COPY = 10,
57 R_I370_RELATIVE = 11,
59 R_I370_max
62 static reloc_howto_type *i370_elf_howto_table[ (int)R_I370_max ];
64 static reloc_howto_type i370_elf_howto_raw[] =
66 /* This reloc does nothing. */
67 HOWTO (R_I370_NONE, /* type */
68 0, /* rightshift */
69 2, /* size (0 = byte, 1 = short, 2 = long) */
70 32, /* bitsize */
71 false, /* pc_relative */
72 0, /* bitpos */
73 complain_overflow_bitfield, /* complain_on_overflow */
74 bfd_elf_generic_reloc, /* special_function */
75 "R_I370_NONE", /* name */
76 false, /* partial_inplace */
77 0, /* src_mask */
78 0, /* dst_mask */
79 false), /* pcrel_offset */
81 /* A standard 31 bit relocation. */
82 HOWTO (R_I370_ADDR31, /* type */
83 0, /* rightshift */
84 2, /* size (0 = byte, 1 = short, 2 = long) */
85 31, /* bitsize */
86 false, /* pc_relative */
87 0, /* bitpos */
88 complain_overflow_bitfield, /* complain_on_overflow */
89 bfd_elf_generic_reloc, /* special_function */
90 "R_I370_ADDR31", /* name */
91 false, /* partial_inplace */
92 0, /* src_mask */
93 0x7fffffff, /* dst_mask */
94 false), /* pcrel_offset */
96 /* A standard 32 bit relocation. */
97 HOWTO (R_I370_ADDR32, /* type */
98 0, /* rightshift */
99 2, /* size (0 = byte, 1 = short, 2 = long) */
100 32, /* bitsize */
101 false, /* pc_relative */
102 0, /* bitpos */
103 complain_overflow_bitfield, /* complain_on_overflow */
104 bfd_elf_generic_reloc, /* special_function */
105 "R_I370_ADDR32", /* name */
106 false, /* partial_inplace */
107 0, /* src_mask */
108 0xffffffff, /* dst_mask */
109 false), /* pcrel_offset */
111 /* A standard 16 bit relocation. */
112 HOWTO (R_I370_ADDR16, /* type */
113 0, /* rightshift */
114 1, /* size (0 = byte, 1 = short, 2 = long) */
115 16, /* bitsize */
116 false, /* pc_relative */
117 0, /* bitpos */
118 complain_overflow_bitfield, /* complain_on_overflow */
119 bfd_elf_generic_reloc, /* special_function */
120 "R_I370_ADDR16", /* name */
121 false, /* partial_inplace */
122 0, /* src_mask */
123 0xffff, /* dst_mask */
124 false), /* pcrel_offset */
126 /* 31-bit PC relative */
127 HOWTO (R_I370_REL31, /* type */
128 0, /* rightshift */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
130 31, /* bitsize */
131 true, /* pc_relative */
132 0, /* bitpos */
133 complain_overflow_bitfield, /* complain_on_overflow */
134 bfd_elf_generic_reloc, /* special_function */
135 "R_I370_REL31", /* name */
136 false, /* partial_inplace */
137 0, /* src_mask */
138 0x7fffffff, /* dst_mask */
139 true), /* pcrel_offset */
141 /* 32-bit PC relative */
142 HOWTO (R_I370_REL32, /* type */
143 0, /* rightshift */
144 2, /* size (0 = byte, 1 = short, 2 = long) */
145 32, /* bitsize */
146 true, /* pc_relative */
147 0, /* bitpos */
148 complain_overflow_bitfield, /* complain_on_overflow */
149 bfd_elf_generic_reloc, /* special_function */
150 "R_I370_REL32", /* name */
151 false, /* partial_inplace */
152 0, /* src_mask */
153 0xffffffff, /* dst_mask */
154 true), /* pcrel_offset */
156 /* A standard 12 bit relocation. */
157 HOWTO (R_I370_ADDR12, /* type */
158 0, /* rightshift */
159 1, /* size (0 = byte, 1 = short, 2 = long) */
160 12, /* bitsize */
161 false, /* pc_relative */
162 0, /* bitpos */
163 complain_overflow_bitfield, /* complain_on_overflow */
164 bfd_elf_generic_reloc, /* special_function */
165 "R_I370_ADDR12", /* name */
166 false, /* partial_inplace */
167 0, /* src_mask */
168 0xfff, /* dst_mask */
169 false), /* pcrel_offset */
171 /* 12-bit PC relative */
172 HOWTO (R_I370_REL12, /* type */
173 0, /* rightshift */
174 1, /* size (0 = byte, 1 = short, 2 = long) */
175 12, /* bitsize */
176 true, /* pc_relative */
177 0, /* bitpos */
178 complain_overflow_bitfield, /* complain_on_overflow */
179 bfd_elf_generic_reloc, /* special_function */
180 "R_I370_REL12", /* name */
181 false, /* partial_inplace */
182 0, /* src_mask */
183 0xfff, /* dst_mask */
184 true), /* pcrel_offset */
186 /* A standard 8 bit relocation. */
187 HOWTO (R_I370_ADDR8, /* type */
188 0, /* rightshift */
189 0, /* size (0 = byte, 1 = short, 2 = long) */
190 8, /* bitsize */
191 false, /* pc_relative */
192 0, /* bitpos */
193 complain_overflow_bitfield, /* complain_on_overflow */
194 bfd_elf_generic_reloc, /* special_function */
195 "R_I370_ADDR8", /* name */
196 false, /* partial_inplace */
197 0, /* src_mask */
198 0xff, /* dst_mask */
199 false), /* pcrel_offset */
201 /* 8-bit PC relative */
202 HOWTO (R_I370_REL8, /* type */
203 0, /* rightshift */
204 0, /* size (0 = byte, 1 = short, 2 = long) */
205 8, /* bitsize */
206 true, /* pc_relative */
207 0, /* bitpos */
208 complain_overflow_bitfield, /* complain_on_overflow */
209 bfd_elf_generic_reloc, /* special_function */
210 "R_I370_REL8", /* name */
211 false, /* partial_inplace */
212 0, /* src_mask */
213 0xff, /* dst_mask */
214 true), /* pcrel_offset */
216 /* This is used only by the dynamic linker. The symbol should exist
217 both in the object being run and in some shared library. The
218 dynamic linker copies the data addressed by the symbol from the
219 shared library into the object, because the object being
220 run has to have the data at some particular address. */
221 HOWTO (R_I370_COPY, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 false, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_bitfield, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_I370_COPY", /* name */
230 false, /* partial_inplace */
231 0, /* src_mask */
232 0, /* dst_mask */
233 false), /* pcrel_offset */
235 /* Used only by the dynamic linker. When the object is run, this
236 longword is set to the load address of the object, plus the
237 addend. */
238 HOWTO (R_I370_RELATIVE, /* type */
239 0, /* rightshift */
240 2, /* size (0 = byte, 1 = short, 2 = long) */
241 32, /* bitsize */
242 false, /* pc_relative */
243 0, /* bitpos */
244 complain_overflow_bitfield, /* complain_on_overflow */
245 bfd_elf_generic_reloc, /* special_function */
246 "R_I370_RELATIVE", /* name */
247 false, /* partial_inplace */
248 0, /* src_mask */
249 0xffffffff, /* dst_mask */
250 false), /* pcrel_offset */
254 static void i370_elf_howto_init PARAMS ((void));
255 static void i370_elf_info_to_howto PARAMS ((bfd *abfd, arelent *cache_ptr,
256 Elf32_Internal_Rela *dst));
257 static boolean i370_elf_set_private_flags PARAMS ((bfd *, flagword));
259 /* Initialize the i370_elf_howto_table, so that linear accesses can be done. */
261 static void
262 i370_elf_howto_init ()
264 unsigned int i, type;
266 for (i = 0; i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]); i++)
268 type = i370_elf_howto_raw[i].type;
269 BFD_ASSERT (type < sizeof (i370_elf_howto_table) / sizeof (i370_elf_howto_table[0]));
270 i370_elf_howto_table[type] = &i370_elf_howto_raw[i];
274 static reloc_howto_type *
275 i370_elf_reloc_type_lookup (abfd, code)
276 bfd *abfd ATTRIBUTE_UNUSED;
277 bfd_reloc_code_real_type code;
279 enum i370_reloc_type i370_reloc = R_I370_NONE;
281 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table if needed */
282 i370_elf_howto_init ();
284 switch ((int)code)
286 default:
287 return (reloc_howto_type *)NULL;
289 case BFD_RELOC_NONE: i370_reloc = R_I370_NONE; break;
290 case BFD_RELOC_32: i370_reloc = R_I370_ADDR31; break;
291 case BFD_RELOC_16: i370_reloc = R_I370_ADDR16; break;
292 case BFD_RELOC_32_PCREL: i370_reloc = R_I370_REL31; break;
293 case BFD_RELOC_CTOR: i370_reloc = R_I370_ADDR31; break;
294 case BFD_RELOC_I370_D12: i370_reloc = R_I370_ADDR12; break;
297 return i370_elf_howto_table[ (int)i370_reloc ];
300 static boolean i370_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
301 static boolean i370_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
303 static boolean i370_elf_relocate_section PARAMS ((bfd *,
304 struct bfd_link_info *info,
305 bfd *,
306 asection *,
307 bfd_byte *,
308 Elf_Internal_Rela *relocs,
309 Elf_Internal_Sym *local_syms,
310 asection **));
312 static boolean i370_elf_create_dynamic_sections PARAMS ((bfd *,
313 struct bfd_link_info *));
315 static boolean i370_elf_section_from_shdr PARAMS ((bfd *,
316 Elf32_Internal_Shdr *,
317 char *));
318 static boolean i370_elf_fake_sections PARAMS ((bfd *,
319 Elf32_Internal_Shdr *,
320 asection *));
321 #if 0
322 static elf_linker_section_t *i370_elf_create_linker_section
323 PARAMS ((bfd *abfd,
324 struct bfd_link_info *info,
325 enum elf_linker_section_enum));
326 #endif
327 static boolean i370_elf_check_relocs PARAMS ((bfd *,
328 struct bfd_link_info *,
329 asection *,
330 const Elf_Internal_Rela *));
332 static boolean i370_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
333 struct elf_link_hash_entry *));
335 static boolean i370_elf_adjust_dynindx PARAMS ((struct elf_link_hash_entry *, PTR));
337 static boolean i370_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
339 static boolean i370_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
341 /* The name of the dynamic interpreter. This is put in the .interp
342 section. */
344 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
346 /* Set the howto pointer for an i370 ELF reloc. */
348 static void
349 i370_elf_info_to_howto (abfd, cache_ptr, dst)
350 bfd *abfd ATTRIBUTE_UNUSED;
351 arelent *cache_ptr;
352 Elf32_Internal_Rela *dst;
354 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table */
355 i370_elf_howto_init ();
357 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_I370_max);
358 cache_ptr->howto = i370_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
361 /* hack alert -- the following several routines look generic to me ...
362 * why are we bothering with them ???
364 /* Function to set whether a module needs the -mrelocatable bit set. */
365 static boolean
366 i370_elf_set_private_flags (abfd, flags)
367 bfd *abfd;
368 flagword flags;
370 BFD_ASSERT (!elf_flags_init (abfd)
371 || elf_elfheader (abfd)->e_flags == flags);
373 elf_elfheader (abfd)->e_flags = flags;
374 elf_flags_init (abfd) = true;
375 return true;
378 /* Copy backend specific data from one object module to another */
379 static boolean
380 i370_elf_copy_private_bfd_data (ibfd, obfd)
381 bfd *ibfd;
382 bfd *obfd;
384 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
385 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
386 return true;
388 BFD_ASSERT (!elf_flags_init (obfd)
389 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
391 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
392 elf_flags_init (obfd) = true;
393 return true;
396 /* Merge backend specific data from an object file to the output
397 object file when linking */
398 static boolean
399 i370_elf_merge_private_bfd_data (ibfd, obfd)
400 bfd *ibfd;
401 bfd *obfd;
403 flagword old_flags;
404 flagword new_flags;
406 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
407 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
408 return true;
410 new_flags = elf_elfheader (ibfd)->e_flags;
411 old_flags = elf_elfheader (obfd)->e_flags;
412 if (!elf_flags_init (obfd)) /* First call, no flags set */
414 elf_flags_init (obfd) = true;
415 elf_elfheader (obfd)->e_flags = new_flags;
418 else if (new_flags == old_flags) /* Compatible flags are ok */
421 else /* Incompatible flags */
423 (*_bfd_error_handler)
424 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
425 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
427 bfd_set_error (bfd_error_bad_value);
428 return false;
431 return true;
434 /* Handle an i370 specific section when reading an object file. This
435 is called when elfcode.h finds a section with an unknown type. */
436 /* XXX hack alert bogus This routine is mostly all junk and almost
437 * certainly does the wrong thing. Its here simply because it does
438 * just enough to allow glibc-2.1 ld.so to compile & link.
441 static boolean
442 i370_elf_section_from_shdr (abfd, hdr, name)
443 bfd *abfd;
444 Elf32_Internal_Shdr *hdr;
445 char *name;
447 asection *newsect;
448 flagword flags;
450 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
451 return false;
453 newsect = hdr->bfd_section;
454 flags = bfd_get_section_flags (abfd, newsect);
455 if (hdr->sh_flags & SHF_EXCLUDE)
456 flags |= SEC_EXCLUDE;
458 if (hdr->sh_type == SHT_ORDERED)
459 flags |= SEC_SORT_ENTRIES;
461 bfd_set_section_flags (abfd, newsect, flags);
462 return true;
465 /* Set up any other section flags and such that may be necessary. */
466 /* XXX hack alert bogus This routine is mostly all junk and almost
467 * certainly does the wrong thing. Its here simply because it does
468 * just enough to allow glibc-2.1 ld.so to compile & link.
471 static boolean
472 i370_elf_fake_sections (abfd, shdr, asect)
473 bfd *abfd ATTRIBUTE_UNUSED;
474 Elf32_Internal_Shdr *shdr;
475 asection *asect;
477 if ((asect->flags & SEC_EXCLUDE) != 0)
478 shdr->sh_flags |= SHF_EXCLUDE;
480 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
481 shdr->sh_type = SHT_ORDERED;
483 return true;
486 #if 0
487 /* Create a special linker section */
488 /* XXX hack alert bogus This routine is mostly all junk and almost
489 * certainly does the wrong thing. Its here simply because it does
490 * just enough to allow glibc-2.1 ld.so to compile & link.
493 static elf_linker_section_t *
494 i370_elf_create_linker_section (abfd, info, which)
495 bfd *abfd;
496 struct bfd_link_info *info;
497 enum elf_linker_section_enum which;
499 bfd *dynobj = elf_hash_table (info)->dynobj;
500 elf_linker_section_t *lsect;
502 /* Record the first bfd section that needs the special section */
503 if (!dynobj)
504 dynobj = elf_hash_table (info)->dynobj = abfd;
506 /* If this is the first time, create the section */
507 lsect = elf_linker_section (dynobj, which);
508 if (!lsect)
510 elf_linker_section_t defaults;
511 static elf_linker_section_t zero_section;
513 defaults = zero_section;
514 defaults.which = which;
515 defaults.hole_written_p = false;
516 defaults.alignment = 2;
518 /* Both of these sections are (technically) created by the user
519 putting data in them, so they shouldn't be marked
520 SEC_LINKER_CREATED.
522 The linker creates them so it has somewhere to attach their
523 respective symbols. In fact, if they were empty it would
524 be OK to leave the symbol set to 0 (or any random number), because
525 the appropriate register should never be used. */
526 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
527 | SEC_IN_MEMORY);
529 switch (which)
531 default:
532 (*_bfd_error_handler) ("%s: Unknown special linker type %d",
533 bfd_get_filename (abfd),
534 (int)which);
536 bfd_set_error (bfd_error_bad_value);
537 return (elf_linker_section_t *)0;
539 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
540 defaults.name = ".sdata";
541 defaults.rel_name = ".rela.sdata";
542 defaults.bss_name = ".sbss";
543 defaults.sym_name = "_SDA_BASE_";
544 defaults.sym_offset = 32768;
545 break;
547 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
548 defaults.name = ".sdata2";
549 defaults.rel_name = ".rela.sdata2";
550 defaults.bss_name = ".sbss2";
551 defaults.sym_name = "_SDA2_BASE_";
552 defaults.sym_offset = 32768;
553 defaults.flags |= SEC_READONLY;
554 break;
557 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
560 return lsect;
562 #endif
564 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
565 to output sections (just like _bfd_elf_create_dynamic_sections has
566 to create .dynbss and .rela.bss). */
567 /* XXX hack alert bogus This routine is mostly all junk and almost
568 * certainly does the wrong thing. Its here simply because it does
569 * just enough to allow glibc-2.1 ld.so to compile & link.
572 static boolean
573 i370_elf_create_dynamic_sections (abfd, info)
574 bfd *abfd;
575 struct bfd_link_info *info;
577 register asection *s;
578 flagword flags;
580 if (!_bfd_elf_create_dynamic_sections(abfd, info))
581 return false;
583 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
584 | SEC_LINKER_CREATED);
586 s = bfd_make_section (abfd, ".dynsbss");
587 if (s == NULL
588 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
589 return false;
591 if (! info->shared)
593 s = bfd_make_section (abfd, ".rela.sbss");
594 if (s == NULL
595 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
596 || ! bfd_set_section_alignment (abfd, s, 2))
597 return false;
600 /* xxx beats me, seem to need a rela.text ... */
601 s = bfd_make_section (abfd, ".rela.text");
602 if (s == NULL
603 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
604 || ! bfd_set_section_alignment (abfd, s, 2))
605 return false;
606 return true;
609 /* Adjust a symbol defined by a dynamic object and referenced by a
610 regular object. The current definition is in some section of the
611 dynamic object, but we're not including those sections. We have to
612 change the definition to something the rest of the link can
613 understand. */
614 /* XXX hack alert bogus This routine is mostly all junk and almost
615 * certainly does the wrong thing. Its here simply because it does
616 * just enough to allow glibc-2.1 ld.so to compile & link.
619 static boolean
620 i370_elf_adjust_dynamic_symbol (info, h)
621 struct bfd_link_info *info;
622 struct elf_link_hash_entry *h;
624 bfd *dynobj = elf_hash_table (info)->dynobj;
625 asection *s;
626 unsigned int power_of_two;
628 #ifdef DEBUG
629 fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
630 h->root.root.string);
631 #endif
633 /* Make sure we know what is going on here. */
634 BFD_ASSERT (dynobj != NULL
635 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
636 || h->weakdef != NULL
637 || ((h->elf_link_hash_flags
638 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
639 && (h->elf_link_hash_flags
640 & ELF_LINK_HASH_REF_REGULAR) != 0
641 && (h->elf_link_hash_flags
642 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
644 s = bfd_get_section_by_name (dynobj, ".rela.text");
645 BFD_ASSERT (s != NULL);
646 s->_raw_size += sizeof (Elf32_External_Rela);
648 /* If this is a weak symbol, and there is a real definition, the
649 processor independent code will have arranged for us to see the
650 real definition first, and we can just use the same value. */
651 if (h->weakdef != NULL)
653 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
654 || h->weakdef->root.type == bfd_link_hash_defweak);
655 h->root.u.def.section = h->weakdef->root.u.def.section;
656 h->root.u.def.value = h->weakdef->root.u.def.value;
657 return true;
660 /* This is a reference to a symbol defined by a dynamic object which
661 is not a function. */
663 /* If we are creating a shared library, we must presume that the
664 only references to the symbol are via the global offset table.
665 For such cases we need not do anything here; the relocations will
666 be handled correctly by relocate_section. */
667 if (info->shared)
668 return true;
670 /* We must allocate the symbol in our .dynbss section, which will
671 become part of the .bss section of the executable. There will be
672 an entry for this symbol in the .dynsym section. The dynamic
673 object will contain position independent code, so all references
674 from the dynamic object to this symbol will go through the global
675 offset table. The dynamic linker will use the .dynsym entry to
676 determine the address it must put in the global offset table, so
677 both the dynamic object and the regular object will refer to the
678 same memory location for the variable.
680 Of course, if the symbol is sufficiently small, we must instead
681 allocate it in .sbss. FIXME: It would be better to do this if and
682 only if there were actually SDAREL relocs for that symbol. */
684 if (h->size <= elf_gp_size (dynobj))
685 s = bfd_get_section_by_name (dynobj, ".dynsbss");
686 else
687 s = bfd_get_section_by_name (dynobj, ".dynbss");
688 BFD_ASSERT (s != NULL);
690 /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
691 copy the initial value out of the dynamic object and into the
692 runtime process image. We need to remember the offset into the
693 .rela.bss section we are going to use. */
694 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
696 asection *srel;
698 if (h->size <= elf_gp_size (dynobj))
699 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
700 else
701 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
702 BFD_ASSERT (srel != NULL);
703 srel->_raw_size += sizeof (Elf32_External_Rela);
704 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
707 /* We need to figure out the alignment required for this symbol. I
708 have no idea how ELF linkers handle this. */
709 power_of_two = bfd_log2 (h->size);
710 if (power_of_two > 4)
711 power_of_two = 4;
713 /* Apply the required alignment. */
714 s->_raw_size = BFD_ALIGN (s->_raw_size,
715 (bfd_size_type) (1 << power_of_two));
716 if (power_of_two > bfd_get_section_alignment (dynobj, s))
718 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
719 return false;
722 /* Define the symbol as being at this point in the section. */
723 h->root.u.def.section = s;
724 h->root.u.def.value = s->_raw_size;
726 /* Increment the section size to make room for the symbol. */
727 s->_raw_size += h->size;
729 return true;
732 /* Increment the index of a dynamic symbol by a given amount. Called
733 via elf_link_hash_traverse. */
734 /* XXX hack alert bogus This routine is mostly all junk and almost
735 * certainly does the wrong thing. Its here simply because it does
736 * just enough to allow glibc-2.1 ld.so to compile & link.
739 static boolean
740 i370_elf_adjust_dynindx (h, cparg)
741 struct elf_link_hash_entry *h;
742 PTR cparg;
744 int *cp = (int *) cparg;
746 #ifdef DEBUG
747 fprintf (stderr,
748 "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",
749 h->dynindx, *cp);
750 #endif
752 if (h->dynindx != -1)
753 h->dynindx += *cp;
755 return true;
758 /* Set the sizes of the dynamic sections. */
759 /* XXX hack alert bogus This routine is mostly all junk and almost
760 * certainly does the wrong thing. Its here simply because it does
761 * just enough to allow glibc-2.1 ld.so to compile & link.
764 static boolean
765 i370_elf_size_dynamic_sections (output_bfd, info)
766 bfd *output_bfd;
767 struct bfd_link_info *info;
769 bfd *dynobj;
770 asection *s;
771 boolean plt;
772 boolean relocs;
773 boolean reltext;
775 #ifdef DEBUG
776 fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
777 #endif
779 dynobj = elf_hash_table (info)->dynobj;
780 BFD_ASSERT (dynobj != NULL);
782 if (elf_hash_table (info)->dynamic_sections_created)
784 /* Set the contents of the .interp section to the interpreter. */
785 if (! info->shared)
787 s = bfd_get_section_by_name (dynobj, ".interp");
788 BFD_ASSERT (s != NULL);
789 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
790 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
793 else
795 /* We may have created entries in the .rela.got, .rela.sdata, and
796 .rela.sdata2 sections. However, if we are not creating the
797 dynamic sections, we will not actually use these entries. Reset
798 the size of .rela.got, et al, which will cause it to get
799 stripped from the output file below. */
800 static char *rela_sections[] = { ".rela.got", ".rela.sdata",
801 ".rela.sdata2", ".rela.sbss",
802 (char *)0 };
803 char **p;
805 for (p = rela_sections; *p != (char *)0; p++)
807 s = bfd_get_section_by_name (dynobj, *p);
808 if (s != NULL)
809 s->_raw_size = 0;
813 /* The check_relocs and adjust_dynamic_symbol entry points have
814 determined the sizes of the various dynamic sections. Allocate
815 memory for them. */
816 plt = false;
817 relocs = false;
818 reltext = false;
819 for (s = dynobj->sections; s != NULL; s = s->next)
821 const char *name;
822 boolean strip;
824 if ((s->flags & SEC_LINKER_CREATED) == 0)
825 continue;
827 /* It's OK to base decisions on the section name, because none
828 of the dynobj section names depend upon the input files. */
829 name = bfd_get_section_name (dynobj, s);
830 strip = false;
832 if (strcmp (name, ".plt") == 0)
834 if (s->_raw_size == 0)
836 /* Strip this section if we don't need it; see the
837 comment below. */
838 strip = true;
840 else
842 /* Remember whether there is a PLT. */
843 plt = true;
846 else if (strncmp (name, ".rela", 5) == 0)
848 if (s->_raw_size == 0)
850 /* If we don't need this section, strip it from the
851 output file. This is mostly to handle .rela.bss and
852 .rela.plt. We must create both sections in
853 create_dynamic_sections, because they must be created
854 before the linker maps input sections to output
855 sections. The linker does that before
856 adjust_dynamic_symbol is called, and it is that
857 function which decides whether anything needs to go
858 into these sections. */
859 strip = true;
861 else
863 asection *target;
864 const char *outname;
866 /* Remember whether there are any relocation sections. */
867 relocs = true;
869 /* If this relocation section applies to a read only
870 section, then we probably need a DT_TEXTREL entry. */
871 outname = bfd_get_section_name (output_bfd,
872 s->output_section);
873 target = bfd_get_section_by_name (output_bfd, outname + 5);
874 if (target != NULL
875 && (target->flags & SEC_READONLY) != 0
876 && (target->flags & SEC_ALLOC) != 0)
877 reltext = true;
879 /* We use the reloc_count field as a counter if we need
880 to copy relocs into the output file. */
881 s->reloc_count = 0;
884 else if (strcmp (name, ".got") != 0
885 && strcmp (name, ".sdata") != 0
886 && strcmp (name, ".sdata2") != 0)
888 /* It's not one of our sections, so don't allocate space. */
889 continue;
892 if (strip)
894 asection **spp;
896 for (spp = &s->output_section->owner->sections;
897 *spp != s->output_section;
898 spp = &(*spp)->next)
900 *spp = s->output_section->next;
901 --s->output_section->owner->section_count;
903 continue;
905 /* Allocate memory for the section contents. */
906 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
907 if (s->contents == NULL && s->_raw_size != 0)
908 return false;
911 if (elf_hash_table (info)->dynamic_sections_created)
913 /* Add some entries to the .dynamic section. We fill in the
914 values later, in i370_elf_finish_dynamic_sections, but we
915 must add the entries now so that we get the correct size for
916 the .dynamic section. The DT_DEBUG entry is filled in by the
917 dynamic linker and used by the debugger. */
918 if (! info->shared)
920 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
921 return false;
924 if (plt)
926 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
927 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
928 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
929 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
930 return false;
933 if (relocs)
935 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
936 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
937 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
938 sizeof (Elf32_External_Rela)))
939 return false;
942 if (reltext)
944 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
945 return false;
946 info->flags |= DF_TEXTREL;
950 /* If we are generating a shared library, we generate a section
951 symbol for each output section. These are local symbols, which
952 means that they must come first in the dynamic symbol table.
953 That means we must increment the dynamic symbol index of every
954 other dynamic symbol.
956 FIXME: We assume that there will never be relocations to
957 locations in linker-created sections that do not have
958 externally-visible names. Instead, we should work out precisely
959 which sections relocations are targetted at. */
960 if (info->shared)
962 int c;
964 for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
966 if ((s->flags & SEC_LINKER_CREATED) != 0
967 || (s->flags & SEC_ALLOC) == 0)
969 elf_section_data (s)->dynindx = -1;
970 continue;
973 /* These symbols will have no names, so we don't need to
974 fiddle with dynstr_index. */
976 elf_section_data (s)->dynindx = c + 1;
978 c++;
981 elf_link_hash_traverse (elf_hash_table (info),
982 i370_elf_adjust_dynindx,
983 (PTR) &c);
984 elf_hash_table (info)->dynsymcount += c;
987 return true;
990 /* Look through the relocs for a section during the first phase, and
991 allocate space in the global offset table or procedure linkage
992 table. */
993 /* XXX hack alert bogus This routine is mostly all junk and almost
994 * certainly does the wrong thing. Its here simply because it does
995 * just enough to allow glibc-2.1 ld.so to compile & link.
998 static boolean
999 i370_elf_check_relocs (abfd, info, sec, relocs)
1000 bfd *abfd;
1001 struct bfd_link_info *info;
1002 asection *sec;
1003 const Elf_Internal_Rela *relocs;
1005 bfd *dynobj;
1006 Elf_Internal_Shdr *symtab_hdr;
1007 struct elf_link_hash_entry **sym_hashes;
1008 const Elf_Internal_Rela *rel;
1009 const Elf_Internal_Rela *rel_end;
1010 bfd_vma *local_got_offsets;
1011 asection *sreloc;
1013 if (info->relocateable)
1014 return true;
1016 #ifdef DEBUG
1017 fprintf (stderr, "i370_elf_check_relocs called for section %s in %s\n",
1018 bfd_get_section_name (abfd, sec),
1019 bfd_get_filename (abfd));
1020 #endif
1022 dynobj = elf_hash_table (info)->dynobj;
1023 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1024 sym_hashes = elf_sym_hashes (abfd);
1025 local_got_offsets = elf_local_got_offsets (abfd);
1027 sreloc = NULL;
1029 rel_end = relocs + sec->reloc_count;
1030 for (rel = relocs; rel < rel_end; rel++)
1032 unsigned long r_symndx;
1033 struct elf_link_hash_entry *h;
1035 r_symndx = ELF32_R_SYM (rel->r_info);
1036 if (r_symndx < symtab_hdr->sh_info)
1037 h = NULL;
1038 else
1039 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1041 if (info->shared)
1043 #ifdef DEBUG
1044 fprintf (stderr,
1045 "i370_elf_check_relocs needs to create relocation for %s\n",
1046 (h && h->root.root.string)
1047 ? h->root.root.string : "<unknown>");
1048 #endif
1049 if (sreloc == NULL)
1051 const char *name;
1053 name = (bfd_elf_string_from_elf_section
1054 (abfd,
1055 elf_elfheader (abfd)->e_shstrndx,
1056 elf_section_data (sec)->rel_hdr.sh_name));
1057 if (name == NULL)
1058 return false;
1060 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1061 && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0);
1063 sreloc = bfd_get_section_by_name (dynobj, name);
1064 if (sreloc == NULL)
1066 flagword flags;
1068 sreloc = bfd_make_section (dynobj, name);
1069 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1070 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1071 if ((sec->flags & SEC_ALLOC) != 0)
1072 flags |= SEC_ALLOC | SEC_LOAD;
1073 if (sreloc == NULL
1074 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1075 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1076 return false;
1080 sreloc->_raw_size += sizeof (Elf32_External_Rela);
1082 /* FIXME: We should here do what the m68k and i386
1083 backends do: if the reloc is pc-relative, record it
1084 in case it turns out that the reloc is unnecessary
1085 because the symbol is forced local by versioning or
1086 we are linking with -Bdynamic. Fortunately this
1087 case is not frequent. */
1091 return true;
1094 /* Finish up the dynamic sections. */
1095 /* XXX hack alert bogus This routine is mostly all junk and almost
1096 * certainly does the wrong thing. Its here simply because it does
1097 * just enough to allow glibc-2.1 ld.so to compile & link.
1100 static boolean
1101 i370_elf_finish_dynamic_sections (output_bfd, info)
1102 bfd *output_bfd;
1103 struct bfd_link_info *info;
1105 asection *sdyn;
1106 bfd *dynobj = elf_hash_table (info)->dynobj;
1107 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1109 #ifdef DEBUG
1110 fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
1111 #endif
1113 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1115 if (elf_hash_table (info)->dynamic_sections_created)
1117 asection *splt;
1118 Elf32_External_Dyn *dyncon, *dynconend;
1120 splt = bfd_get_section_by_name (dynobj, ".plt");
1121 BFD_ASSERT (splt != NULL && sdyn != NULL);
1123 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1124 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1125 for (; dyncon < dynconend; dyncon++)
1127 Elf_Internal_Dyn dyn;
1128 const char *name;
1129 boolean size;
1131 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1133 switch (dyn.d_tag)
1135 case DT_PLTGOT: name = ".plt"; size = false; break;
1136 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1137 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1138 default: name = NULL; size = false; break;
1141 if (name != NULL)
1143 asection *s;
1145 s = bfd_get_section_by_name (output_bfd, name);
1146 if (s == NULL)
1147 dyn.d_un.d_val = 0;
1148 else
1150 if (! size)
1151 dyn.d_un.d_ptr = s->vma;
1152 else
1154 if (s->_cooked_size != 0)
1155 dyn.d_un.d_val = s->_cooked_size;
1156 else
1157 dyn.d_un.d_val = s->_raw_size;
1160 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1165 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
1166 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
1167 /* XXX this is clearly very wrong for the 370 arch */
1168 if (sgot)
1170 unsigned char *contents = sgot->contents;
1171 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
1173 if (sdyn == NULL)
1174 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
1175 else
1176 bfd_put_32 (output_bfd,
1177 sdyn->output_section->vma + sdyn->output_offset,
1178 contents+4);
1180 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1183 if (info->shared)
1185 asection *sdynsym;
1186 asection *s;
1187 Elf_Internal_Sym sym;
1188 int maxdindx = 0;
1190 /* Set up the section symbols for the output sections. */
1192 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1193 BFD_ASSERT (sdynsym != NULL);
1195 sym.st_size = 0;
1196 sym.st_name = 0;
1197 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1198 sym.st_other = 0;
1200 for (s = output_bfd->sections; s != NULL; s = s->next)
1202 int indx, dindx;
1204 sym.st_value = s->vma;
1206 indx = elf_section_data (s)->this_idx;
1207 dindx = elf_section_data (s)->dynindx;
1208 if (dindx != -1)
1210 BFD_ASSERT(indx > 0);
1211 BFD_ASSERT(dindx > 0);
1213 if (dindx > maxdindx)
1214 maxdindx = dindx;
1216 sym.st_shndx = indx;
1218 bfd_elf32_swap_symbol_out (output_bfd, &sym,
1219 (PTR) (((Elf32_External_Sym *)
1220 sdynsym->contents)
1221 + dindx));
1225 /* Set the sh_info field of the output .dynsym section to the
1226 index of the first global symbol. */
1227 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1228 maxdindx + 1;
1231 return true;
1234 /* The RELOCATE_SECTION function is called by the ELF backend linker
1235 to handle the relocations for a section.
1237 The relocs are always passed as Rela structures; if the section
1238 actually uses Rel structures, the r_addend field will always be
1239 zero.
1241 This function is responsible for adjust the section contents as
1242 necessary, and (if using Rela relocs and generating a
1243 relocateable output file) adjusting the reloc addend as
1244 necessary.
1246 This function does not have to worry about setting the reloc
1247 address or the reloc symbol index.
1249 LOCAL_SYMS is a pointer to the swapped in local symbols.
1251 LOCAL_SECTIONS is an array giving the section in the input file
1252 corresponding to the st_shndx field of each local symbol.
1254 The global hash table entry for the global symbols can be found
1255 via elf_sym_hashes (input_bfd).
1257 When generating relocateable output, this function must handle
1258 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1259 going to be the section symbol corresponding to the output
1260 section, which means that the addend must be adjusted
1261 accordingly. */
1263 static boolean
1264 i370_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1265 contents, relocs, local_syms, local_sections)
1266 bfd *output_bfd;
1267 struct bfd_link_info *info;
1268 bfd *input_bfd;
1269 asection *input_section;
1270 bfd_byte *contents;
1271 Elf_Internal_Rela *relocs;
1272 Elf_Internal_Sym *local_syms;
1273 asection **local_sections;
1275 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1276 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1277 bfd *dynobj = elf_hash_table (info)->dynobj;
1278 Elf_Internal_Rela *rel = relocs;
1279 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
1280 asection *sreloc = NULL;
1281 bfd_vma *local_got_offsets;
1282 boolean ret = true;
1284 #ifdef DEBUG
1285 fprintf (stderr, "i370_elf_relocate_section called for %s section %s, %ld relocations%s\n",
1286 bfd_get_filename (input_bfd),
1287 bfd_section_name(input_bfd, input_section),
1288 (long)input_section->reloc_count,
1289 (info->relocateable) ? " (relocatable)" : "");
1290 #endif
1292 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table if needed */
1293 i370_elf_howto_init ();
1295 local_got_offsets = elf_local_got_offsets (input_bfd);
1297 for (; rel < relend; rel++)
1299 enum i370_reloc_type r_type = (enum i370_reloc_type)ELF32_R_TYPE (rel->r_info);
1300 bfd_vma offset = rel->r_offset;
1301 bfd_vma addend = rel->r_addend;
1302 bfd_reloc_status_type r = bfd_reloc_other;
1303 Elf_Internal_Sym *sym = (Elf_Internal_Sym *)0;
1304 asection *sec = (asection *)0;
1305 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)0;
1306 const char *sym_name = (const char *)0;
1307 reloc_howto_type *howto;
1308 unsigned long r_symndx;
1309 bfd_vma relocation;
1311 /* Unknown relocation handling */
1312 if ((unsigned)r_type >= (unsigned)R_I370_max
1313 || !i370_elf_howto_table[(int)r_type])
1315 (*_bfd_error_handler) ("%s: unknown relocation type %d",
1316 bfd_get_filename (input_bfd),
1317 (int)r_type);
1319 bfd_set_error (bfd_error_bad_value);
1320 ret = false;
1321 continue;
1324 howto = i370_elf_howto_table[(int)r_type];
1325 r_symndx = ELF32_R_SYM (rel->r_info);
1327 if (info->relocateable)
1329 /* This is a relocateable link. We don't have to change
1330 anything, unless the reloc is against a section symbol,
1331 in which case we have to adjust according to where the
1332 section symbol winds up in the output section. */
1333 if (r_symndx < symtab_hdr->sh_info)
1335 sym = local_syms + r_symndx;
1336 if ((unsigned)ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1338 sec = local_sections[r_symndx];
1339 addend = rel->r_addend += sec->output_offset + sym->st_value;
1343 #ifdef DEBUG
1344 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
1345 howto->name,
1346 (int)r_type,
1347 r_symndx,
1348 (long)offset,
1349 (long)addend);
1350 #endif
1351 continue;
1354 /* This is a final link. */
1355 if (r_symndx < symtab_hdr->sh_info)
1357 sym = local_syms + r_symndx;
1358 sec = local_sections[r_symndx];
1359 sym_name = "<local symbol>";
1361 relocation = (sec->output_section->vma
1362 + sec->output_offset
1363 + sym->st_value);
1365 else
1367 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1368 while (h->root.type == bfd_link_hash_indirect
1369 || h->root.type == bfd_link_hash_warning)
1370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1371 sym_name = h->root.root.string;
1372 if (h->root.type == bfd_link_hash_defined
1373 || h->root.type == bfd_link_hash_defweak)
1375 sec = h->root.u.def.section;
1376 if (info->shared
1377 && ((! info->symbolic && h->dynindx != -1)
1378 || (h->elf_link_hash_flags
1379 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1380 && (input_section->flags & SEC_ALLOC) != 0
1381 && (r_type == R_I370_ADDR31
1382 || r_type == R_I370_COPY
1383 || r_type == R_I370_ADDR16
1384 || r_type == R_I370_RELATIVE))
1386 /* In these cases, we don't need the relocation
1387 value. We check specially because in some
1388 obscure cases sec->output_section will be NULL. */
1389 relocation = 0;
1391 else
1392 relocation = (h->root.u.def.value
1393 + sec->output_section->vma
1394 + sec->output_offset);
1396 else if (h->root.type == bfd_link_hash_undefweak)
1397 relocation = 0;
1398 else if (info->shared
1399 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1400 relocation = 0;
1401 else
1403 (*info->callbacks->undefined_symbol) (info,
1404 h->root.root.string,
1405 input_bfd,
1406 input_section,
1407 rel->r_offset,
1408 true);
1409 ret = false;
1410 continue;
1414 switch ((int)r_type)
1416 default:
1417 (*_bfd_error_handler) ("%s: unknown relocation type %d for symbol %s",
1418 bfd_get_filename (input_bfd),
1419 (int)r_type, sym_name);
1421 bfd_set_error (bfd_error_bad_value);
1422 ret = false;
1423 continue;
1425 /* Relocations that may need to be propagated if this is a shared
1426 object. */
1427 case (int)R_I370_REL31:
1428 /* If these relocations are not to a named symbol, they can be
1429 handled right here, no need to bother the dynamic linker. */
1430 if (h == NULL
1431 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1432 break;
1433 /* fall through */
1435 /* Relocations that always need to be propagated if this is a shared
1436 object. */
1437 case (int)R_I370_NONE:
1438 case (int)R_I370_ADDR31:
1439 case (int)R_I370_ADDR16:
1440 if (info->shared)
1442 Elf_Internal_Rela outrel;
1443 boolean skip;
1445 #ifdef DEBUG
1446 fprintf (stderr,
1447 "i370_elf_relocate_section needs to create relocation for %s\n",
1448 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
1449 #endif
1451 /* When generating a shared object, these relocations
1452 are copied into the output file to be resolved at run
1453 time. */
1455 if (sreloc == NULL)
1457 const char *name;
1459 name = (bfd_elf_string_from_elf_section
1460 (input_bfd,
1461 elf_elfheader (input_bfd)->e_shstrndx,
1462 elf_section_data (input_section)->rel_hdr.sh_name));
1463 if (name == NULL)
1464 return false;
1466 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1467 && strcmp (bfd_get_section_name (input_bfd,
1468 input_section),
1469 name + 5) == 0);
1471 sreloc = bfd_get_section_by_name (dynobj, name);
1472 BFD_ASSERT (sreloc != NULL);
1475 skip = false;
1477 if (elf_section_data (input_section)->stab_info == NULL)
1478 outrel.r_offset = rel->r_offset;
1479 else
1481 bfd_vma off;
1483 off = (_bfd_stab_section_offset
1484 (output_bfd, &elf_hash_table (info)->stab_info,
1485 input_section,
1486 &elf_section_data (input_section)->stab_info,
1487 rel->r_offset));
1488 if (off == (bfd_vma) -1)
1489 skip = true;
1490 outrel.r_offset = off;
1493 outrel.r_offset += (input_section->output_section->vma
1494 + input_section->output_offset);
1496 if (skip)
1497 memset (&outrel, 0, sizeof outrel);
1498 /* h->dynindx may be -1 if this symbol was marked to
1499 become local. */
1500 else if (h != NULL
1501 && ((! info->symbolic && h->dynindx != -1)
1502 || (h->elf_link_hash_flags
1503 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1505 BFD_ASSERT (h->dynindx != -1);
1506 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1507 outrel.r_addend = rel->r_addend;
1509 else
1511 if (r_type == R_I370_ADDR31)
1513 outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
1514 outrel.r_addend = relocation + rel->r_addend;
1516 else
1518 long indx;
1520 if (h == NULL)
1521 sec = local_sections[r_symndx];
1522 else
1524 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1525 || (h->root.type
1526 == bfd_link_hash_defweak));
1527 sec = h->root.u.def.section;
1529 if (sec != NULL && bfd_is_abs_section (sec))
1530 indx = 0;
1531 else if (sec == NULL || sec->owner == NULL)
1533 bfd_set_error (bfd_error_bad_value);
1534 return false;
1536 else
1538 asection *osec;
1540 osec = sec->output_section;
1541 indx = elf_section_data (osec)->dynindx;
1542 BFD_ASSERT(indx > 0);
1543 #ifdef DEBUG
1544 if (indx <= 0)
1546 printf ("indx=%d section=%s flags=%08x name=%s\n",
1547 indx, osec->name, osec->flags,
1548 h->root.root.string);
1550 #endif
1553 outrel.r_info = ELF32_R_INFO (indx, r_type);
1554 outrel.r_addend = relocation + rel->r_addend;
1558 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1559 (((Elf32_External_Rela *)
1560 sreloc->contents)
1561 + sreloc->reloc_count));
1562 ++sreloc->reloc_count;
1564 /* This reloc will be computed at runtime, so there's no
1565 need to do anything now, unless this is a RELATIVE
1566 reloc in an unallocated section. */
1567 if (skip
1568 || (input_section->flags & SEC_ALLOC) != 0
1569 || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
1570 continue;
1572 break;
1574 case (int)R_I370_COPY:
1575 case (int)R_I370_RELATIVE:
1576 (*_bfd_error_handler) ("%s: Relocation %s is not yet supported for symbol %s.",
1577 bfd_get_filename (input_bfd),
1578 i370_elf_howto_table[ (int)r_type ]->name,
1579 sym_name);
1581 bfd_set_error (bfd_error_invalid_operation);
1582 ret = false;
1583 continue;
1586 #ifdef DEBUG
1587 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
1588 howto->name,
1589 (int)r_type,
1590 sym_name,
1591 r_symndx,
1592 (long)offset,
1593 (long)addend);
1594 #endif
1596 r = _bfd_final_link_relocate (howto,
1597 input_bfd,
1598 input_section,
1599 contents,
1600 offset,
1601 relocation,
1602 addend);
1604 if (r != bfd_reloc_ok)
1606 ret = false;
1607 switch (r)
1609 default:
1610 break;
1612 case bfd_reloc_overflow:
1614 const char *name;
1616 if (h != NULL)
1617 name = h->root.root.string;
1618 else
1620 name = bfd_elf_string_from_elf_section (input_bfd,
1621 symtab_hdr->sh_link,
1622 sym->st_name);
1623 if (name == NULL)
1624 break;
1626 if (*name == '\0')
1627 name = bfd_section_name (input_bfd, sec);
1630 (*info->callbacks->reloc_overflow) (info,
1631 name,
1632 howto->name,
1633 (bfd_vma) 0,
1634 input_bfd,
1635 input_section,
1636 offset);
1638 break;
1644 #ifdef DEBUG
1645 fprintf (stderr, "\n");
1646 #endif
1648 return ret;
1651 static void
1652 i370_elf_post_process_headers (abfd, link_info)
1653 bfd * abfd;
1654 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1656 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
1658 i_ehdrp = elf_elfheader (abfd);
1659 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1662 #define TARGET_BIG_SYM bfd_elf32_i370_vec
1663 #define TARGET_BIG_NAME "elf32-i370"
1664 #define ELF_ARCH bfd_arch_i370
1665 #define ELF_MACHINE_CODE EM_S370
1666 #ifdef EM_I370_OLD
1667 #define ELF_MACHINE_ALT1 EM_I370_OLD
1668 #endif
1669 #define ELF_MAXPAGESIZE 0x1000
1670 #define elf_info_to_howto i370_elf_info_to_howto
1672 #define elf_backend_plt_not_loaded 1
1673 #define elf_backend_got_symbol_offset 4
1675 #define bfd_elf32_bfd_reloc_type_lookup i370_elf_reloc_type_lookup
1676 #define bfd_elf32_bfd_set_private_flags i370_elf_set_private_flags
1677 #define bfd_elf32_bfd_copy_private_bfd_data i370_elf_copy_private_bfd_data
1678 #define bfd_elf32_bfd_merge_private_bfd_data i370_elf_merge_private_bfd_data
1679 #define elf_backend_relocate_section i370_elf_relocate_section
1681 /* dynamic loader support is mostly broken; just enough here to be able to
1682 * link glibc's ld.so without errors.
1684 #define elf_backend_create_dynamic_sections i370_elf_create_dynamic_sections
1685 #define elf_backend_size_dynamic_sections i370_elf_size_dynamic_sections
1686 #define elf_backend_finish_dynamic_sections i370_elf_finish_dynamic_sections
1687 #define elf_backend_fake_sections i370_elf_fake_sections
1688 #define elf_backend_section_from_shdr i370_elf_section_from_shdr
1689 #define elf_backend_adjust_dynamic_symbol i370_elf_adjust_dynamic_symbol
1690 #define elf_backend_check_relocs i370_elf_check_relocs
1693 #define elf_backend_add_symbol_hook i370_elf_add_symbol_hook
1694 #define elf_backend_finish_dynamic_symbol i370_elf_finish_dynamic_symbol
1695 #define elf_backend_additional_program_headers i370_elf_additional_program_headers
1696 #define elf_backend_modify_segment_map i370_elf_modify_segment_map
1699 #define elf_backend_post_process_headers i370_elf_post_process_headers
1701 int i370_noop()
1703 return 1;
1706 /* we need to define these at least as no-ops to link glibc ld.so */
1708 #define elf_backend_add_symbol_hook \
1709 (boolean (*) PARAMS ((bfd *, struct bfd_link_info *, \
1710 const Elf_Internal_Sym *, const char **, flagword *, \
1711 asection **, bfd_vma *))) i370_noop
1712 #define elf_backend_finish_dynamic_symbol \
1713 (boolean (*) PARAMS ((bfd *, struct bfd_link_info *, \
1714 struct elf_link_hash_entry *, \
1715 Elf_Internal_Sym *))) i370_noop
1716 #define elf_backend_additional_program_headers \
1717 (int (*) PARAMS ((bfd *))) i370_noop
1718 #define elf_backend_modify_segment_map \
1719 (boolean (*) PARAMS ((bfd *))) i370_noop
1721 #include "elf32-target.h"