1 /* i370-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5 Hacked by Linas Vepstas for i370 linas@linas.org
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file is based on a preliminary PowerPC ELF ABI.
24 But its been hacked on for the IBM 360/370 architectures.
25 Basically, the 31bit relocation works, and just about everything
26 else is a wild card. In particular, don't expect shared libs or
27 dynamic loading to work ... its never been tested ...
37 /* i370 relocations */
38 /* Note that there is really just one relocation that we currently
39 * support (and only one that we seem to need, at the moment), and
40 * that is the 31-bit address relocation. Note that the 370/390
41 * only supports a 31-bit (2GB) address space.
61 static reloc_howto_type *i370_elf_howto_table[ (int)R_I370_max ];
63 static reloc_howto_type i370_elf_howto_raw[] =
65 /* This reloc does nothing. */
66 HOWTO (R_I370_NONE, /* type */
68 2, /* size (0 = byte, 1 = short, 2 = long) */
70 false, /* pc_relative */
72 complain_overflow_bitfield, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_I370_NONE", /* name */
75 false, /* partial_inplace */
78 false), /* pcrel_offset */
80 /* A standard 31 bit relocation. */
81 HOWTO (R_I370_ADDR31, /* type */
83 2, /* size (0 = byte, 1 = short, 2 = long) */
85 false, /* pc_relative */
87 complain_overflow_bitfield, /* complain_on_overflow */
88 bfd_elf_generic_reloc, /* special_function */
89 "R_I370_ADDR31", /* name */
90 false, /* partial_inplace */
92 0x7fffffff, /* dst_mask */
93 false), /* pcrel_offset */
95 /* A standard 32 bit relocation. */
96 HOWTO (R_I370_ADDR32, /* type */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
100 false, /* pc_relative */
102 complain_overflow_bitfield, /* complain_on_overflow */
103 bfd_elf_generic_reloc, /* special_function */
104 "R_I370_ADDR32", /* name */
105 false, /* partial_inplace */
107 0xffffffff, /* dst_mask */
108 false), /* pcrel_offset */
110 /* A standard 16 bit relocation. */
111 HOWTO (R_I370_ADDR16, /* type */
113 1, /* size (0 = byte, 1 = short, 2 = long) */
115 false, /* pc_relative */
117 complain_overflow_bitfield, /* complain_on_overflow */
118 bfd_elf_generic_reloc, /* special_function */
119 "R_I370_ADDR16", /* name */
120 false, /* partial_inplace */
122 0xffff, /* dst_mask */
123 false), /* pcrel_offset */
125 /* 31-bit PC relative */
126 HOWTO (R_I370_REL31, /* type */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
130 true, /* pc_relative */
132 complain_overflow_bitfield, /* complain_on_overflow */
133 bfd_elf_generic_reloc, /* special_function */
134 "R_I370_REL31", /* name */
135 false, /* partial_inplace */
137 0x7fffffff, /* dst_mask */
138 true), /* pcrel_offset */
140 /* 32-bit PC relative */
141 HOWTO (R_I370_REL32, /* type */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
145 true, /* pc_relative */
147 complain_overflow_bitfield, /* complain_on_overflow */
148 bfd_elf_generic_reloc, /* special_function */
149 "R_I370_REL32", /* name */
150 false, /* partial_inplace */
152 0xffffffff, /* dst_mask */
153 true), /* pcrel_offset */
155 /* A standard 12 bit relocation. */
156 HOWTO (R_I370_ADDR12, /* type */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
160 false, /* pc_relative */
162 complain_overflow_bitfield, /* complain_on_overflow */
163 bfd_elf_generic_reloc, /* special_function */
164 "R_I370_ADDR12", /* name */
165 false, /* partial_inplace */
167 0xfff, /* dst_mask */
168 false), /* pcrel_offset */
170 /* 12-bit PC relative */
171 HOWTO (R_I370_REL12, /* type */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
175 true, /* pc_relative */
177 complain_overflow_bitfield, /* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_I370_REL12", /* name */
180 false, /* partial_inplace */
182 0xfff, /* dst_mask */
183 true), /* pcrel_offset */
185 /* A standard 8 bit relocation. */
186 HOWTO (R_I370_ADDR8, /* type */
188 0, /* size (0 = byte, 1 = short, 2 = long) */
190 false, /* pc_relative */
192 complain_overflow_bitfield, /* complain_on_overflow */
193 bfd_elf_generic_reloc, /* special_function */
194 "R_I370_ADDR8", /* name */
195 false, /* partial_inplace */
198 false), /* pcrel_offset */
200 /* 8-bit PC relative */
201 HOWTO (R_I370_REL8, /* type */
203 0, /* size (0 = byte, 1 = short, 2 = long) */
205 true, /* pc_relative */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_I370_REL8", /* name */
210 false, /* partial_inplace */
213 true), /* pcrel_offset */
215 /* This is used only by the dynamic linker. The symbol should exist
216 both in the object being run and in some shared library. The
217 dynamic linker copies the data addressed by the symbol from the
218 shared library into the object, because the object being
219 run has to have the data at some particular address. */
220 HOWTO (R_I370_COPY, /* type */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
224 false, /* pc_relative */
226 complain_overflow_bitfield, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_I370_COPY", /* name */
229 false, /* partial_inplace */
232 false), /* pcrel_offset */
234 /* Used only by the dynamic linker. When the object is run, this
235 longword is set to the load address of the object, plus the
237 HOWTO (R_I370_RELATIVE, /* type */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
241 false, /* pc_relative */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_I370_RELATIVE", /* name */
246 false, /* partial_inplace */
248 0xffffffff, /* dst_mask */
249 false), /* pcrel_offset */
253 static void i370_elf_howto_init PARAMS ((void));
254 static reloc_howto_type *i370_elf_reloc_type_lookup
255 PARAMS ((bfd *, bfd_reloc_code_real_type));
257 static void i370_elf_info_to_howto PARAMS ((bfd *abfd, arelent *cache_ptr,
258 Elf32_Internal_Rela *dst));
259 static boolean i370_elf_set_private_flags PARAMS ((bfd *, flagword));
261 /* Initialize the i370_elf_howto_table, so that linear accesses can be done. */
264 i370_elf_howto_init ()
266 unsigned int i, type;
268 for (i = 0; i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]); i++)
270 type = i370_elf_howto_raw[i].type;
271 BFD_ASSERT (type < sizeof (i370_elf_howto_table) / sizeof (i370_elf_howto_table[0]));
272 i370_elf_howto_table[type] = &i370_elf_howto_raw[i];
276 static reloc_howto_type *
277 i370_elf_reloc_type_lookup (abfd, code)
278 bfd *abfd ATTRIBUTE_UNUSED;
279 bfd_reloc_code_real_type code;
281 enum i370_reloc_type i370_reloc = R_I370_NONE;
283 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table if needed */
284 i370_elf_howto_init ();
289 return (reloc_howto_type *)NULL;
291 case BFD_RELOC_NONE: i370_reloc = R_I370_NONE; break;
292 case BFD_RELOC_32: i370_reloc = R_I370_ADDR31; break;
293 case BFD_RELOC_16: i370_reloc = R_I370_ADDR16; break;
294 case BFD_RELOC_32_PCREL: i370_reloc = R_I370_REL31; break;
295 case BFD_RELOC_CTOR: i370_reloc = R_I370_ADDR31; break;
296 case BFD_RELOC_I370_D12: i370_reloc = R_I370_ADDR12; break;
299 return i370_elf_howto_table[ (int)i370_reloc ];
302 static boolean i370_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
304 static boolean i370_elf_relocate_section PARAMS ((bfd *,
305 struct bfd_link_info *info,
309 Elf_Internal_Rela *relocs,
310 Elf_Internal_Sym *local_syms,
312 static void i370_elf_post_process_headers
313 PARAMS ((bfd *, struct bfd_link_info *));
315 static boolean i370_elf_create_dynamic_sections PARAMS ((bfd *,
316 struct bfd_link_info *));
318 static boolean i370_elf_section_from_shdr PARAMS ((bfd *,
319 Elf32_Internal_Shdr *,
321 static boolean i370_elf_fake_sections PARAMS ((bfd *,
322 Elf32_Internal_Shdr *,
325 static elf_linker_section_t *i370_elf_create_linker_section
327 struct bfd_link_info *info,
328 enum elf_linker_section_enum));
330 static boolean i370_elf_check_relocs PARAMS ((bfd *,
331 struct bfd_link_info *,
333 const Elf_Internal_Rela *));
335 static boolean i370_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
336 struct elf_link_hash_entry *));
338 static boolean i370_elf_adjust_dynindx PARAMS ((struct elf_link_hash_entry *, PTR));
340 static boolean i370_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
342 static boolean i370_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
344 /* The name of the dynamic interpreter. This is put in the .interp
347 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
349 /* Set the howto pointer for an i370 ELF reloc. */
352 i370_elf_info_to_howto (abfd, cache_ptr, dst)
353 bfd *abfd ATTRIBUTE_UNUSED;
355 Elf32_Internal_Rela *dst;
357 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table */
358 i370_elf_howto_init ();
360 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_I370_max);
361 cache_ptr->howto = i370_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
364 /* hack alert -- the following several routines look generic to me ...
365 * why are we bothering with them ???
367 /* Function to set whether a module needs the -mrelocatable bit set. */
369 i370_elf_set_private_flags (abfd, flags)
373 BFD_ASSERT (!elf_flags_init (abfd)
374 || elf_elfheader (abfd)->e_flags == flags);
376 elf_elfheader (abfd)->e_flags = flags;
377 elf_flags_init (abfd) = true;
381 /* Merge backend specific data from an object file to the output
382 object file when linking */
384 i370_elf_merge_private_bfd_data (ibfd, obfd)
391 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
392 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
395 new_flags = elf_elfheader (ibfd)->e_flags;
396 old_flags = elf_elfheader (obfd)->e_flags;
397 if (!elf_flags_init (obfd)) /* First call, no flags set */
399 elf_flags_init (obfd) = true;
400 elf_elfheader (obfd)->e_flags = new_flags;
403 else if (new_flags == old_flags) /* Compatible flags are ok */
406 else /* Incompatible flags */
408 (*_bfd_error_handler)
409 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
410 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
412 bfd_set_error (bfd_error_bad_value);
419 /* Handle an i370 specific section when reading an object file. This
420 is called when elfcode.h finds a section with an unknown type. */
421 /* XXX hack alert bogus This routine is mostly all junk and almost
422 * certainly does the wrong thing. Its here simply because it does
423 * just enough to allow glibc-2.1 ld.so to compile & link.
427 i370_elf_section_from_shdr (abfd, hdr, name)
429 Elf32_Internal_Shdr *hdr;
435 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
438 newsect = hdr->bfd_section;
439 flags = bfd_get_section_flags (abfd, newsect);
440 if (hdr->sh_flags & SHF_EXCLUDE)
441 flags |= SEC_EXCLUDE;
443 if (hdr->sh_type == SHT_ORDERED)
444 flags |= SEC_SORT_ENTRIES;
446 bfd_set_section_flags (abfd, newsect, flags);
450 /* Set up any other section flags and such that may be necessary. */
451 /* XXX hack alert bogus This routine is mostly all junk and almost
452 * certainly does the wrong thing. Its here simply because it does
453 * just enough to allow glibc-2.1 ld.so to compile & link.
457 i370_elf_fake_sections (abfd, shdr, asect)
458 bfd *abfd ATTRIBUTE_UNUSED;
459 Elf32_Internal_Shdr *shdr;
462 if ((asect->flags & SEC_EXCLUDE) != 0)
463 shdr->sh_flags |= SHF_EXCLUDE;
465 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
466 shdr->sh_type = SHT_ORDERED;
472 /* Create a special linker section */
473 /* XXX hack alert bogus This routine is mostly all junk and almost
474 * certainly does the wrong thing. Its here simply because it does
475 * just enough to allow glibc-2.1 ld.so to compile & link.
478 static elf_linker_section_t *
479 i370_elf_create_linker_section (abfd, info, which)
481 struct bfd_link_info *info;
482 enum elf_linker_section_enum which;
484 bfd *dynobj = elf_hash_table (info)->dynobj;
485 elf_linker_section_t *lsect;
487 /* Record the first bfd section that needs the special section */
489 dynobj = elf_hash_table (info)->dynobj = abfd;
491 /* If this is the first time, create the section */
492 lsect = elf_linker_section (dynobj, which);
495 elf_linker_section_t defaults;
496 static elf_linker_section_t zero_section;
498 defaults = zero_section;
499 defaults.which = which;
500 defaults.hole_written_p = false;
501 defaults.alignment = 2;
503 /* Both of these sections are (technically) created by the user
504 putting data in them, so they shouldn't be marked
507 The linker creates them so it has somewhere to attach their
508 respective symbols. In fact, if they were empty it would
509 be OK to leave the symbol set to 0 (or any random number), because
510 the appropriate register should never be used. */
511 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
517 (*_bfd_error_handler) ("%s: Unknown special linker type %d",
518 bfd_archive_filename (abfd),
521 bfd_set_error (bfd_error_bad_value);
522 return (elf_linker_section_t *)0;
524 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
525 defaults.name = ".sdata";
526 defaults.rel_name = ".rela.sdata";
527 defaults.bss_name = ".sbss";
528 defaults.sym_name = "_SDA_BASE_";
529 defaults.sym_offset = 32768;
532 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
533 defaults.name = ".sdata2";
534 defaults.rel_name = ".rela.sdata2";
535 defaults.bss_name = ".sbss2";
536 defaults.sym_name = "_SDA2_BASE_";
537 defaults.sym_offset = 32768;
538 defaults.flags |= SEC_READONLY;
542 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
549 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
550 to output sections (just like _bfd_elf_create_dynamic_sections has
551 to create .dynbss and .rela.bss). */
552 /* XXX hack alert bogus This routine is mostly all junk and almost
553 * certainly does the wrong thing. Its here simply because it does
554 * just enough to allow glibc-2.1 ld.so to compile & link.
558 i370_elf_create_dynamic_sections (abfd, info)
560 struct bfd_link_info *info;
562 register asection *s;
565 if (!_bfd_elf_create_dynamic_sections(abfd, info))
568 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
569 | SEC_LINKER_CREATED);
571 s = bfd_make_section (abfd, ".dynsbss");
573 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
578 s = bfd_make_section (abfd, ".rela.sbss");
580 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
581 || ! bfd_set_section_alignment (abfd, s, 2))
585 /* xxx beats me, seem to need a rela.text ... */
586 s = bfd_make_section (abfd, ".rela.text");
588 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
589 || ! bfd_set_section_alignment (abfd, s, 2))
594 /* Adjust a symbol defined by a dynamic object and referenced by a
595 regular object. The current definition is in some section of the
596 dynamic object, but we're not including those sections. We have to
597 change the definition to something the rest of the link can
599 /* XXX hack alert bogus This routine is mostly all junk and almost
600 * certainly does the wrong thing. Its here simply because it does
601 * just enough to allow glibc-2.1 ld.so to compile & link.
605 i370_elf_adjust_dynamic_symbol (info, h)
606 struct bfd_link_info *info;
607 struct elf_link_hash_entry *h;
609 bfd *dynobj = elf_hash_table (info)->dynobj;
611 unsigned int power_of_two;
614 fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
615 h->root.root.string);
618 /* Make sure we know what is going on here. */
619 BFD_ASSERT (dynobj != NULL
620 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
621 || h->weakdef != NULL
622 || ((h->elf_link_hash_flags
623 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
624 && (h->elf_link_hash_flags
625 & ELF_LINK_HASH_REF_REGULAR) != 0
626 && (h->elf_link_hash_flags
627 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
629 s = bfd_get_section_by_name (dynobj, ".rela.text");
630 BFD_ASSERT (s != NULL);
631 s->_raw_size += sizeof (Elf32_External_Rela);
633 /* If this is a weak symbol, and there is a real definition, the
634 processor independent code will have arranged for us to see the
635 real definition first, and we can just use the same value. */
636 if (h->weakdef != NULL)
638 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
639 || h->weakdef->root.type == bfd_link_hash_defweak);
640 h->root.u.def.section = h->weakdef->root.u.def.section;
641 h->root.u.def.value = h->weakdef->root.u.def.value;
645 /* This is a reference to a symbol defined by a dynamic object which
646 is not a function. */
648 /* If we are creating a shared library, we must presume that the
649 only references to the symbol are via the global offset table.
650 For such cases we need not do anything here; the relocations will
651 be handled correctly by relocate_section. */
655 /* We must allocate the symbol in our .dynbss section, which will
656 become part of the .bss section of the executable. There will be
657 an entry for this symbol in the .dynsym section. The dynamic
658 object will contain position independent code, so all references
659 from the dynamic object to this symbol will go through the global
660 offset table. The dynamic linker will use the .dynsym entry to
661 determine the address it must put in the global offset table, so
662 both the dynamic object and the regular object will refer to the
663 same memory location for the variable.
665 Of course, if the symbol is sufficiently small, we must instead
666 allocate it in .sbss. FIXME: It would be better to do this if and
667 only if there were actually SDAREL relocs for that symbol. */
669 if (h->size <= elf_gp_size (dynobj))
670 s = bfd_get_section_by_name (dynobj, ".dynsbss");
672 s = bfd_get_section_by_name (dynobj, ".dynbss");
673 BFD_ASSERT (s != NULL);
675 /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
676 copy the initial value out of the dynamic object and into the
677 runtime process image. We need to remember the offset into the
678 .rela.bss section we are going to use. */
679 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
683 if (h->size <= elf_gp_size (dynobj))
684 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
686 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
687 BFD_ASSERT (srel != NULL);
688 srel->_raw_size += sizeof (Elf32_External_Rela);
689 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
692 /* We need to figure out the alignment required for this symbol. I
693 have no idea how ELF linkers handle this. */
694 power_of_two = bfd_log2 (h->size);
695 if (power_of_two > 4)
698 /* Apply the required alignment. */
699 s->_raw_size = BFD_ALIGN (s->_raw_size,
700 (bfd_size_type) (1 << power_of_two));
701 if (power_of_two > bfd_get_section_alignment (dynobj, s))
703 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
707 /* Define the symbol as being at this point in the section. */
708 h->root.u.def.section = s;
709 h->root.u.def.value = s->_raw_size;
711 /* Increment the section size to make room for the symbol. */
712 s->_raw_size += h->size;
717 /* Increment the index of a dynamic symbol by a given amount. Called
718 via elf_link_hash_traverse. */
719 /* XXX hack alert bogus This routine is mostly all junk and almost
720 * certainly does the wrong thing. Its here simply because it does
721 * just enough to allow glibc-2.1 ld.so to compile & link.
725 i370_elf_adjust_dynindx (h, cparg)
726 struct elf_link_hash_entry *h;
729 int *cp = (int *) cparg;
733 "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",
737 if (h->root.type == bfd_link_hash_warning)
738 h = (struct elf_link_hash_entry *) h->root.u.i.link;
740 if (h->dynindx != -1)
746 /* Set the sizes of the dynamic sections. */
747 /* XXX hack alert bogus This routine is mostly all junk and almost
748 * certainly does the wrong thing. Its here simply because it does
749 * just enough to allow glibc-2.1 ld.so to compile & link.
753 i370_elf_size_dynamic_sections (output_bfd, info)
755 struct bfd_link_info *info;
764 fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
767 dynobj = elf_hash_table (info)->dynobj;
768 BFD_ASSERT (dynobj != NULL);
770 if (elf_hash_table (info)->dynamic_sections_created)
772 /* Set the contents of the .interp section to the interpreter. */
775 s = bfd_get_section_by_name (dynobj, ".interp");
776 BFD_ASSERT (s != NULL);
777 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
778 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
783 /* We may have created entries in the .rela.got, .rela.sdata, and
784 .rela.sdata2 sections. However, if we are not creating the
785 dynamic sections, we will not actually use these entries. Reset
786 the size of .rela.got, et al, which will cause it to get
787 stripped from the output file below. */
788 static char *rela_sections[] = { ".rela.got", ".rela.sdata",
789 ".rela.sdata2", ".rela.sbss",
793 for (p = rela_sections; *p != (char *)0; p++)
795 s = bfd_get_section_by_name (dynobj, *p);
801 /* The check_relocs and adjust_dynamic_symbol entry points have
802 determined the sizes of the various dynamic sections. Allocate
807 for (s = dynobj->sections; s != NULL; s = s->next)
812 if ((s->flags & SEC_LINKER_CREATED) == 0)
815 /* It's OK to base decisions on the section name, because none
816 of the dynobj section names depend upon the input files. */
817 name = bfd_get_section_name (dynobj, s);
820 if (strcmp (name, ".plt") == 0)
822 if (s->_raw_size == 0)
824 /* Strip this section if we don't need it; see the
830 /* Remember whether there is a PLT. */
834 else if (strncmp (name, ".rela", 5) == 0)
836 if (s->_raw_size == 0)
838 /* If we don't need this section, strip it from the
839 output file. This is mostly to handle .rela.bss and
840 .rela.plt. We must create both sections in
841 create_dynamic_sections, because they must be created
842 before the linker maps input sections to output
843 sections. The linker does that before
844 adjust_dynamic_symbol is called, and it is that
845 function which decides whether anything needs to go
846 into these sections. */
854 /* Remember whether there are any relocation sections. */
857 /* If this relocation section applies to a read only
858 section, then we probably need a DT_TEXTREL entry. */
859 outname = bfd_get_section_name (output_bfd,
861 target = bfd_get_section_by_name (output_bfd, outname + 5);
863 && (target->flags & SEC_READONLY) != 0
864 && (target->flags & SEC_ALLOC) != 0)
867 /* We use the reloc_count field as a counter if we need
868 to copy relocs into the output file. */
872 else if (strcmp (name, ".got") != 0
873 && strcmp (name, ".sdata") != 0
874 && strcmp (name, ".sdata2") != 0)
876 /* It's not one of our sections, so don't allocate space. */
884 for (spp = &s->output_section->owner->sections;
888 if (*spp == s->output_section)
890 bfd_section_list_remove (s->output_section->owner, spp);
891 --s->output_section->owner->section_count;
897 /* Allocate memory for the section contents. */
898 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
899 if (s->contents == NULL && s->_raw_size != 0)
903 if (elf_hash_table (info)->dynamic_sections_created)
905 /* Add some entries to the .dynamic section. We fill in the
906 values later, in i370_elf_finish_dynamic_sections, but we
907 must add the entries now so that we get the correct size for
908 the .dynamic section. The DT_DEBUG entry is filled in by the
909 dynamic linker and used by the debugger. */
910 #define add_dynamic_entry(TAG, VAL) \
911 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
915 if (!add_dynamic_entry (DT_DEBUG, 0))
921 if (!add_dynamic_entry (DT_PLTGOT, 0)
922 || !add_dynamic_entry (DT_PLTRELSZ, 0)
923 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
924 || !add_dynamic_entry (DT_JMPREL, 0))
930 if (!add_dynamic_entry (DT_RELA, 0)
931 || !add_dynamic_entry (DT_RELASZ, 0)
932 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
938 if (!add_dynamic_entry (DT_TEXTREL, 0))
940 info->flags |= DF_TEXTREL;
943 #undef add_dynamic_entry
945 /* If we are generating a shared library, we generate a section
946 symbol for each output section. These are local symbols, which
947 means that they must come first in the dynamic symbol table.
948 That means we must increment the dynamic symbol index of every
949 other dynamic symbol.
951 FIXME: We assume that there will never be relocations to
952 locations in linker-created sections that do not have
953 externally-visible names. Instead, we should work out precisely
954 which sections relocations are targetted at. */
959 for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
961 if ((s->flags & SEC_LINKER_CREATED) != 0
962 || (s->flags & SEC_ALLOC) == 0)
964 elf_section_data (s)->dynindx = -1;
968 /* These symbols will have no names, so we don't need to
969 fiddle with dynstr_index. */
971 elf_section_data (s)->dynindx = c + 1;
976 elf_link_hash_traverse (elf_hash_table (info),
977 i370_elf_adjust_dynindx,
979 elf_hash_table (info)->dynsymcount += c;
985 /* Look through the relocs for a section during the first phase, and
986 allocate space in the global offset table or procedure linkage
988 /* XXX hack alert bogus This routine is mostly all junk and almost
989 * certainly does the wrong thing. Its here simply because it does
990 * just enough to allow glibc-2.1 ld.so to compile & link.
994 i370_elf_check_relocs (abfd, info, sec, relocs)
996 struct bfd_link_info *info;
998 const Elf_Internal_Rela *relocs;
1001 Elf_Internal_Shdr *symtab_hdr;
1002 struct elf_link_hash_entry **sym_hashes;
1003 const Elf_Internal_Rela *rel;
1004 const Elf_Internal_Rela *rel_end;
1005 bfd_vma *local_got_offsets;
1008 if (info->relocateable)
1012 fprintf (stderr, "i370_elf_check_relocs called for section %s in %s\n",
1013 bfd_get_section_name (abfd, sec),
1014 bfd_archive_filename (abfd));
1017 dynobj = elf_hash_table (info)->dynobj;
1018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1019 sym_hashes = elf_sym_hashes (abfd);
1020 local_got_offsets = elf_local_got_offsets (abfd);
1024 rel_end = relocs + sec->reloc_count;
1025 for (rel = relocs; rel < rel_end; rel++)
1027 unsigned long r_symndx;
1028 struct elf_link_hash_entry *h;
1030 r_symndx = ELF32_R_SYM (rel->r_info);
1031 if (r_symndx < symtab_hdr->sh_info)
1034 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1040 "i370_elf_check_relocs needs to create relocation for %s\n",
1041 (h && h->root.root.string)
1042 ? h->root.root.string : "<unknown>");
1048 name = (bfd_elf_string_from_elf_section
1050 elf_elfheader (abfd)->e_shstrndx,
1051 elf_section_data (sec)->rel_hdr.sh_name));
1055 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1056 && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0);
1058 sreloc = bfd_get_section_by_name (dynobj, name);
1063 sreloc = bfd_make_section (dynobj, name);
1064 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1065 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1066 if ((sec->flags & SEC_ALLOC) != 0)
1067 flags |= SEC_ALLOC | SEC_LOAD;
1069 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1070 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1075 sreloc->_raw_size += sizeof (Elf32_External_Rela);
1077 /* FIXME: We should here do what the m68k and i386
1078 backends do: if the reloc is pc-relative, record it
1079 in case it turns out that the reloc is unnecessary
1080 because the symbol is forced local by versioning or
1081 we are linking with -Bdynamic. Fortunately this
1082 case is not frequent. */
1089 /* Finish up the dynamic sections. */
1090 /* XXX hack alert bogus This routine is mostly all junk and almost
1091 * certainly does the wrong thing. Its here simply because it does
1092 * just enough to allow glibc-2.1 ld.so to compile & link.
1096 i370_elf_finish_dynamic_sections (output_bfd, info)
1098 struct bfd_link_info *info;
1101 bfd *dynobj = elf_hash_table (info)->dynobj;
1102 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1105 fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
1108 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1110 if (elf_hash_table (info)->dynamic_sections_created)
1113 Elf32_External_Dyn *dyncon, *dynconend;
1115 splt = bfd_get_section_by_name (dynobj, ".plt");
1116 BFD_ASSERT (splt != NULL && sdyn != NULL);
1118 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1119 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1120 for (; dyncon < dynconend; dyncon++)
1122 Elf_Internal_Dyn dyn;
1126 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1130 case DT_PLTGOT: name = ".plt"; size = false; break;
1131 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1132 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1133 default: name = NULL; size = false; break;
1140 s = bfd_get_section_by_name (output_bfd, name);
1146 dyn.d_un.d_ptr = s->vma;
1149 if (s->_cooked_size != 0)
1150 dyn.d_un.d_val = s->_cooked_size;
1152 dyn.d_un.d_val = s->_raw_size;
1155 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1160 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
1161 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
1162 /* XXX this is clearly very wrong for the 370 arch */
1165 unsigned char *contents = sgot->contents;
1166 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
1169 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
1171 bfd_put_32 (output_bfd,
1172 sdyn->output_section->vma + sdyn->output_offset,
1175 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1182 Elf_Internal_Sym sym;
1185 /* Set up the section symbols for the output sections. */
1187 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1188 BFD_ASSERT (sdynsym != NULL);
1192 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1195 for (s = output_bfd->sections; s != NULL; s = s->next)
1198 Elf32_External_Sym *esym;
1200 sym.st_value = s->vma;
1202 indx = elf_section_data (s)->this_idx;
1203 dindx = elf_section_data (s)->dynindx;
1206 BFD_ASSERT(indx > 0);
1207 BFD_ASSERT(dindx > 0);
1209 if (dindx > maxdindx)
1212 sym.st_shndx = indx;
1214 esym = (Elf32_External_Sym *) sdynsym->contents + dindx;
1215 bfd_elf32_swap_symbol_out (output_bfd, &sym, (PTR) esym, (PTR) 0);
1219 /* Set the sh_info field of the output .dynsym section to the
1220 index of the first global symbol. */
1221 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1228 /* The RELOCATE_SECTION function is called by the ELF backend linker
1229 to handle the relocations for a section.
1231 The relocs are always passed as Rela structures; if the section
1232 actually uses Rel structures, the r_addend field will always be
1235 This function is responsible for adjust the section contents as
1236 necessary, and (if using Rela relocs and generating a
1237 relocateable output file) adjusting the reloc addend as
1240 This function does not have to worry about setting the reloc
1241 address or the reloc symbol index.
1243 LOCAL_SYMS is a pointer to the swapped in local symbols.
1245 LOCAL_SECTIONS is an array giving the section in the input file
1246 corresponding to the st_shndx field of each local symbol.
1248 The global hash table entry for the global symbols can be found
1249 via elf_sym_hashes (input_bfd).
1251 When generating relocateable output, this function must handle
1252 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1253 going to be the section symbol corresponding to the output
1254 section, which means that the addend must be adjusted
1258 i370_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1259 contents, relocs, local_syms, local_sections)
1261 struct bfd_link_info *info;
1263 asection *input_section;
1265 Elf_Internal_Rela *relocs;
1266 Elf_Internal_Sym *local_syms;
1267 asection **local_sections;
1269 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1270 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1271 bfd *dynobj = elf_hash_table (info)->dynobj;
1272 Elf_Internal_Rela *rel = relocs;
1273 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
1274 asection *sreloc = NULL;
1275 bfd_vma *local_got_offsets;
1278 if (info->relocateable)
1282 fprintf (stderr, "i370_elf_relocate_section called for %s section %s, %ld relocations%s\n",
1283 bfd_archive_filename (input_bfd),
1284 bfd_section_name(input_bfd, input_section),
1285 (long) input_section->reloc_count,
1286 (info->relocateable) ? " (relocatable)" : "");
1289 if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table if needed */
1290 i370_elf_howto_init ();
1292 local_got_offsets = elf_local_got_offsets (input_bfd);
1294 for (; rel < relend; rel++)
1296 enum i370_reloc_type r_type = (enum i370_reloc_type)ELF32_R_TYPE (rel->r_info);
1297 bfd_vma offset = rel->r_offset;
1298 bfd_vma addend = rel->r_addend;
1299 bfd_reloc_status_type r = bfd_reloc_other;
1300 Elf_Internal_Sym *sym = (Elf_Internal_Sym *)0;
1301 asection *sec = (asection *)0;
1302 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)0;
1303 const char *sym_name = (const char *)0;
1304 reloc_howto_type *howto;
1305 unsigned long r_symndx;
1308 /* Unknown relocation handling */
1309 if ((unsigned)r_type >= (unsigned)R_I370_max
1310 || !i370_elf_howto_table[(int)r_type])
1312 (*_bfd_error_handler) ("%s: unknown relocation type %d",
1313 bfd_archive_filename (input_bfd),
1316 bfd_set_error (bfd_error_bad_value);
1321 howto = i370_elf_howto_table[(int)r_type];
1322 r_symndx = ELF32_R_SYM (rel->r_info);
1324 if (r_symndx < symtab_hdr->sh_info)
1326 sym = local_syms + r_symndx;
1327 sec = local_sections[r_symndx];
1328 sym_name = "<local symbol>";
1330 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1331 addend = rel->r_addend;
1335 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1336 while (h->root.type == bfd_link_hash_indirect
1337 || h->root.type == bfd_link_hash_warning)
1338 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1339 sym_name = h->root.root.string;
1340 if (h->root.type == bfd_link_hash_defined
1341 || h->root.type == bfd_link_hash_defweak)
1343 sec = h->root.u.def.section;
1345 && ((! info->symbolic && h->dynindx != -1)
1346 || (h->elf_link_hash_flags
1347 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1348 && (input_section->flags & SEC_ALLOC) != 0
1349 && (r_type == R_I370_ADDR31
1350 || r_type == R_I370_COPY
1351 || r_type == R_I370_ADDR16
1352 || r_type == R_I370_RELATIVE))
1354 /* In these cases, we don't need the relocation
1355 value. We check specially because in some
1356 obscure cases sec->output_section will be NULL. */
1360 relocation = (h->root.u.def.value
1361 + sec->output_section->vma
1362 + sec->output_offset);
1364 else if (h->root.type == bfd_link_hash_undefweak)
1366 else if (info->shared
1367 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1371 (*info->callbacks->undefined_symbol) (info,
1372 h->root.root.string,
1382 switch ((int) r_type)
1385 (*_bfd_error_handler)
1386 ("%s: unknown relocation type %d for symbol %s",
1387 bfd_archive_filename (input_bfd),
1388 (int) r_type, sym_name);
1390 bfd_set_error (bfd_error_bad_value);
1394 case (int)R_I370_NONE:
1397 /* Relocations that may need to be propagated if this is a shared
1399 case (int)R_I370_REL31:
1400 /* If these relocations are not to a named symbol, they can be
1401 handled right here, no need to bother the dynamic linker. */
1403 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1407 /* Relocations that always need to be propagated if this is a shared
1409 case (int)R_I370_ADDR31:
1410 case (int)R_I370_ADDR16:
1414 Elf_Internal_Rela outrel;
1419 "i370_elf_relocate_section needs to create relocation for %s\n",
1420 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
1423 /* When generating a shared object, these relocations
1424 are copied into the output file to be resolved at run
1431 name = (bfd_elf_string_from_elf_section
1433 elf_elfheader (input_bfd)->e_shstrndx,
1434 elf_section_data (input_section)->rel_hdr.sh_name));
1438 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1439 && strcmp (bfd_get_section_name (input_bfd,
1443 sreloc = bfd_get_section_by_name (dynobj, name);
1444 BFD_ASSERT (sreloc != NULL);
1450 _bfd_elf_section_offset (output_bfd, info, input_section,
1452 if (outrel.r_offset == (bfd_vma) -1
1453 || outrel.r_offset == (bfd_vma) -2)
1454 skip = (int) outrel.r_offset;
1455 outrel.r_offset += (input_section->output_section->vma
1456 + input_section->output_offset);
1459 memset (&outrel, 0, sizeof outrel);
1460 /* h->dynindx may be -1 if this symbol was marked to
1463 && ((! info->symbolic && h->dynindx != -1)
1464 || (h->elf_link_hash_flags
1465 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1467 BFD_ASSERT (h->dynindx != -1);
1468 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1469 outrel.r_addend = rel->r_addend;
1473 if (r_type == R_I370_ADDR31)
1475 outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
1476 outrel.r_addend = relocation + rel->r_addend;
1483 sec = local_sections[r_symndx];
1486 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1488 == bfd_link_hash_defweak));
1489 sec = h->root.u.def.section;
1491 if (sec != NULL && bfd_is_abs_section (sec))
1493 else if (sec == NULL || sec->owner == NULL)
1495 bfd_set_error (bfd_error_bad_value);
1502 osec = sec->output_section;
1503 indx = elf_section_data (osec)->dynindx;
1504 BFD_ASSERT(indx > 0);
1508 printf ("indx=%d section=%s flags=%08x name=%s\n",
1509 indx, osec->name, osec->flags,
1510 h->root.root.string);
1515 outrel.r_info = ELF32_R_INFO (indx, r_type);
1516 outrel.r_addend = relocation + rel->r_addend;
1520 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1521 (((Elf32_External_Rela *)
1523 + sreloc->reloc_count));
1524 ++sreloc->reloc_count;
1526 /* This reloc will be computed at runtime, so there's no
1527 need to do anything now, unless this is a RELATIVE
1528 reloc in an unallocated section. */
1530 || (input_section->flags & SEC_ALLOC) != 0
1531 || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
1536 case (int)R_I370_COPY:
1537 case (int)R_I370_RELATIVE:
1538 (*_bfd_error_handler)
1539 ("%s: Relocation %s is not yet supported for symbol %s.",
1540 bfd_archive_filename (input_bfd),
1541 i370_elf_howto_table[(int) r_type]->name,
1544 bfd_set_error (bfd_error_invalid_operation);
1550 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
1559 r = _bfd_final_link_relocate (howto,
1567 if (r != bfd_reloc_ok)
1575 case bfd_reloc_overflow:
1580 name = h->root.root.string;
1583 name = bfd_elf_string_from_elf_section (input_bfd,
1584 symtab_hdr->sh_link,
1590 name = bfd_section_name (input_bfd, sec);
1593 (*info->callbacks->reloc_overflow) (info,
1608 fprintf (stderr, "\n");
1615 i370_elf_post_process_headers (abfd, link_info)
1617 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1619 Elf_Internal_Ehdr * i_ehdrp; /* Elf file header, internal form */
1621 i_ehdrp = elf_elfheader (abfd);
1622 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1625 #define TARGET_BIG_SYM bfd_elf32_i370_vec
1626 #define TARGET_BIG_NAME "elf32-i370"
1627 #define ELF_ARCH bfd_arch_i370
1628 #define ELF_MACHINE_CODE EM_S370
1630 #define ELF_MACHINE_ALT1 EM_I370_OLD
1632 #define ELF_MAXPAGESIZE 0x1000
1633 #define elf_info_to_howto i370_elf_info_to_howto
1635 #define elf_backend_plt_not_loaded 1
1636 #define elf_backend_got_symbol_offset 4
1637 #define elf_backend_rela_normal 1
1639 #define bfd_elf32_bfd_reloc_type_lookup i370_elf_reloc_type_lookup
1640 #define bfd_elf32_bfd_set_private_flags i370_elf_set_private_flags
1641 #define bfd_elf32_bfd_merge_private_bfd_data i370_elf_merge_private_bfd_data
1642 #define elf_backend_relocate_section i370_elf_relocate_section
1644 /* dynamic loader support is mostly broken; just enough here to be able to
1645 * link glibc's ld.so without errors.
1647 #define elf_backend_create_dynamic_sections i370_elf_create_dynamic_sections
1648 #define elf_backend_size_dynamic_sections i370_elf_size_dynamic_sections
1649 #define elf_backend_finish_dynamic_sections i370_elf_finish_dynamic_sections
1650 #define elf_backend_fake_sections i370_elf_fake_sections
1651 #define elf_backend_section_from_shdr i370_elf_section_from_shdr
1652 #define elf_backend_adjust_dynamic_symbol i370_elf_adjust_dynamic_symbol
1653 #define elf_backend_check_relocs i370_elf_check_relocs
1656 #define elf_backend_add_symbol_hook i370_elf_add_symbol_hook
1657 #define elf_backend_finish_dynamic_symbol i370_elf_finish_dynamic_symbol
1658 #define elf_backend_additional_program_headers i370_elf_additional_program_headers
1659 #define elf_backend_modify_segment_map i370_elf_modify_segment_map
1662 #define elf_backend_post_process_headers i370_elf_post_process_headers
1664 static int i370_noop PARAMS ((void));
1666 static int i370_noop ()
1671 /* we need to define these at least as no-ops to link glibc ld.so */
1673 #define elf_backend_add_symbol_hook \
1674 (boolean (*) PARAMS ((bfd *, struct bfd_link_info *, \
1675 const Elf_Internal_Sym *, const char **, flagword *, \
1676 asection **, bfd_vma *))) i370_noop
1677 #define elf_backend_finish_dynamic_symbol \
1678 (boolean (*) PARAMS ((bfd *, struct bfd_link_info *, \
1679 struct elf_link_hash_entry *, \
1680 Elf_Internal_Sym *))) i370_noop
1681 #define elf_backend_additional_program_headers \
1682 (int (*) PARAMS ((bfd *))) i370_noop
1683 #define elf_backend_modify_segment_map \
1684 (boolean (*) PARAMS ((bfd *))) i370_noop
1686 #include "elf32-target.h"