2002-08-13 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf32-i370.c
blob3ba50ba851cb8a0ee1e0a4714ce45c4b912a956e
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 ...
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/i370.h"
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.
43 enum i370_reloc_type
45 R_I370_NONE = 0,
46 R_I370_ADDR31 = 1,
47 R_I370_ADDR32 = 2,
48 R_I370_ADDR16 = 3,
49 R_I370_REL31 = 4,
50 R_I370_REL32 = 5,
51 R_I370_ADDR12 = 6,
52 R_I370_REL12 = 7,
53 R_I370_ADDR8 = 8,
54 R_I370_REL8 = 9,
55 R_I370_COPY = 10,
56 R_I370_RELATIVE = 11,
58 R_I370_max
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 */
67 0, /* rightshift */
68 2, /* size (0 = byte, 1 = short, 2 = long) */
69 32, /* bitsize */
70 false, /* pc_relative */
71 0, /* bitpos */
72 complain_overflow_bitfield, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_I370_NONE", /* name */
75 false, /* partial_inplace */
76 0, /* src_mask */
77 0, /* dst_mask */
78 false), /* pcrel_offset */
80 /* A standard 31 bit relocation. */
81 HOWTO (R_I370_ADDR31, /* type */
82 0, /* rightshift */
83 2, /* size (0 = byte, 1 = short, 2 = long) */
84 31, /* bitsize */
85 false, /* pc_relative */
86 0, /* bitpos */
87 complain_overflow_bitfield, /* complain_on_overflow */
88 bfd_elf_generic_reloc, /* special_function */
89 "R_I370_ADDR31", /* name */
90 false, /* partial_inplace */
91 0, /* src_mask */
92 0x7fffffff, /* dst_mask */
93 false), /* pcrel_offset */
95 /* A standard 32 bit relocation. */
96 HOWTO (R_I370_ADDR32, /* type */
97 0, /* rightshift */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
99 32, /* bitsize */
100 false, /* pc_relative */
101 0, /* bitpos */
102 complain_overflow_bitfield, /* complain_on_overflow */
103 bfd_elf_generic_reloc, /* special_function */
104 "R_I370_ADDR32", /* name */
105 false, /* partial_inplace */
106 0, /* src_mask */
107 0xffffffff, /* dst_mask */
108 false), /* pcrel_offset */
110 /* A standard 16 bit relocation. */
111 HOWTO (R_I370_ADDR16, /* type */
112 0, /* rightshift */
113 1, /* size (0 = byte, 1 = short, 2 = long) */
114 16, /* bitsize */
115 false, /* pc_relative */
116 0, /* bitpos */
117 complain_overflow_bitfield, /* complain_on_overflow */
118 bfd_elf_generic_reloc, /* special_function */
119 "R_I370_ADDR16", /* name */
120 false, /* partial_inplace */
121 0, /* src_mask */
122 0xffff, /* dst_mask */
123 false), /* pcrel_offset */
125 /* 31-bit PC relative */
126 HOWTO (R_I370_REL31, /* type */
127 0, /* rightshift */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
129 31, /* bitsize */
130 true, /* pc_relative */
131 0, /* bitpos */
132 complain_overflow_bitfield, /* complain_on_overflow */
133 bfd_elf_generic_reloc, /* special_function */
134 "R_I370_REL31", /* name */
135 false, /* partial_inplace */
136 0, /* src_mask */
137 0x7fffffff, /* dst_mask */
138 true), /* pcrel_offset */
140 /* 32-bit PC relative */
141 HOWTO (R_I370_REL32, /* type */
142 0, /* rightshift */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
144 32, /* bitsize */
145 true, /* pc_relative */
146 0, /* bitpos */
147 complain_overflow_bitfield, /* complain_on_overflow */
148 bfd_elf_generic_reloc, /* special_function */
149 "R_I370_REL32", /* name */
150 false, /* partial_inplace */
151 0, /* src_mask */
152 0xffffffff, /* dst_mask */
153 true), /* pcrel_offset */
155 /* A standard 12 bit relocation. */
156 HOWTO (R_I370_ADDR12, /* type */
157 0, /* rightshift */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
159 12, /* bitsize */
160 false, /* pc_relative */
161 0, /* bitpos */
162 complain_overflow_bitfield, /* complain_on_overflow */
163 bfd_elf_generic_reloc, /* special_function */
164 "R_I370_ADDR12", /* name */
165 false, /* partial_inplace */
166 0, /* src_mask */
167 0xfff, /* dst_mask */
168 false), /* pcrel_offset */
170 /* 12-bit PC relative */
171 HOWTO (R_I370_REL12, /* type */
172 0, /* rightshift */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
174 12, /* bitsize */
175 true, /* pc_relative */
176 0, /* bitpos */
177 complain_overflow_bitfield, /* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_I370_REL12", /* name */
180 false, /* partial_inplace */
181 0, /* src_mask */
182 0xfff, /* dst_mask */
183 true), /* pcrel_offset */
185 /* A standard 8 bit relocation. */
186 HOWTO (R_I370_ADDR8, /* type */
187 0, /* rightshift */
188 0, /* size (0 = byte, 1 = short, 2 = long) */
189 8, /* bitsize */
190 false, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_bitfield, /* complain_on_overflow */
193 bfd_elf_generic_reloc, /* special_function */
194 "R_I370_ADDR8", /* name */
195 false, /* partial_inplace */
196 0, /* src_mask */
197 0xff, /* dst_mask */
198 false), /* pcrel_offset */
200 /* 8-bit PC relative */
201 HOWTO (R_I370_REL8, /* type */
202 0, /* rightshift */
203 0, /* size (0 = byte, 1 = short, 2 = long) */
204 8, /* bitsize */
205 true, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_I370_REL8", /* name */
210 false, /* partial_inplace */
211 0, /* src_mask */
212 0xff, /* dst_mask */
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 */
221 0, /* rightshift */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
223 32, /* bitsize */
224 false, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_bitfield, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_I370_COPY", /* name */
229 false, /* partial_inplace */
230 0, /* src_mask */
231 0, /* dst_mask */
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
236 addend. */
237 HOWTO (R_I370_RELATIVE, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 32, /* bitsize */
241 false, /* pc_relative */
242 0, /* bitpos */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_I370_RELATIVE", /* name */
246 false, /* partial_inplace */
247 0, /* src_mask */
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. */
263 static void
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 ();
286 switch ((int)code)
288 default:
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,
306 bfd *,
307 asection *,
308 bfd_byte *,
309 Elf_Internal_Rela *relocs,
310 Elf_Internal_Sym *local_syms,
311 asection **));
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 *,
320 const char *));
321 static boolean i370_elf_fake_sections PARAMS ((bfd *,
322 Elf32_Internal_Shdr *,
323 asection *));
324 #if 0
325 static elf_linker_section_t *i370_elf_create_linker_section
326 PARAMS ((bfd *abfd,
327 struct bfd_link_info *info,
328 enum elf_linker_section_enum));
329 #endif
330 static boolean i370_elf_check_relocs PARAMS ((bfd *,
331 struct bfd_link_info *,
332 asection *,
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
345 section. */
347 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
349 /* Set the howto pointer for an i370 ELF reloc. */
351 static void
352 i370_elf_info_to_howto (abfd, cache_ptr, dst)
353 bfd *abfd ATTRIBUTE_UNUSED;
354 arelent *cache_ptr;
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. */
368 static boolean
369 i370_elf_set_private_flags (abfd, flags)
370 bfd *abfd;
371 flagword 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;
378 return true;
381 /* Merge backend specific data from an object file to the output
382 object file when linking */
383 static boolean
384 i370_elf_merge_private_bfd_data (ibfd, obfd)
385 bfd *ibfd;
386 bfd *obfd;
388 flagword old_flags;
389 flagword new_flags;
391 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
392 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
393 return true;
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);
413 return false;
416 return true;
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.
426 static boolean
427 i370_elf_section_from_shdr (abfd, hdr, name)
428 bfd *abfd;
429 Elf32_Internal_Shdr *hdr;
430 const char *name;
432 asection *newsect;
433 flagword flags;
435 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
436 return false;
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);
447 return true;
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.
456 static boolean
457 i370_elf_fake_sections (abfd, shdr, asect)
458 bfd *abfd ATTRIBUTE_UNUSED;
459 Elf32_Internal_Shdr *shdr;
460 asection *asect;
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;
468 return true;
471 #if 0
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)
480 bfd *abfd;
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 */
488 if (!dynobj)
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);
493 if (!lsect)
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
505 SEC_LINKER_CREATED.
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
512 | SEC_IN_MEMORY);
514 switch (which)
516 default:
517 (*_bfd_error_handler) ("%s: Unknown special linker type %d",
518 bfd_archive_filename (abfd),
519 (int) which);
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;
530 break;
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;
539 break;
542 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
545 return lsect;
547 #endif
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.
557 static boolean
558 i370_elf_create_dynamic_sections (abfd, info)
559 bfd *abfd;
560 struct bfd_link_info *info;
562 register asection *s;
563 flagword flags;
565 if (!_bfd_elf_create_dynamic_sections(abfd, info))
566 return false;
568 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
569 | SEC_LINKER_CREATED);
571 s = bfd_make_section (abfd, ".dynsbss");
572 if (s == NULL
573 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
574 return false;
576 if (! info->shared)
578 s = bfd_make_section (abfd, ".rela.sbss");
579 if (s == NULL
580 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
581 || ! bfd_set_section_alignment (abfd, s, 2))
582 return false;
585 /* xxx beats me, seem to need a rela.text ... */
586 s = bfd_make_section (abfd, ".rela.text");
587 if (s == NULL
588 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
589 || ! bfd_set_section_alignment (abfd, s, 2))
590 return false;
591 return true;
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
598 understand. */
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.
604 static boolean
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;
610 asection *s;
611 unsigned int power_of_two;
613 #ifdef DEBUG
614 fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
615 h->root.root.string);
616 #endif
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;
642 return true;
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. */
652 if (info->shared)
653 return true;
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");
671 else
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)
681 asection *srel;
683 if (h->size <= elf_gp_size (dynobj))
684 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
685 else
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)
696 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))
704 return false;
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;
714 return true;
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.
724 static boolean
725 i370_elf_adjust_dynindx (h, cparg)
726 struct elf_link_hash_entry *h;
727 PTR cparg;
729 int *cp = (int *) cparg;
731 #ifdef DEBUG
732 fprintf (stderr,
733 "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",
734 h->dynindx, *cp);
735 #endif
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)
741 h->dynindx += *cp;
743 return true;
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.
752 static boolean
753 i370_elf_size_dynamic_sections (output_bfd, info)
754 bfd *output_bfd;
755 struct bfd_link_info *info;
757 bfd *dynobj;
758 asection *s;
759 boolean plt;
760 boolean relocs;
761 boolean reltext;
763 #ifdef DEBUG
764 fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
765 #endif
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. */
773 if (! info->shared)
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;
781 else
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",
790 (char *)0 };
791 char **p;
793 for (p = rela_sections; *p != (char *)0; p++)
795 s = bfd_get_section_by_name (dynobj, *p);
796 if (s != NULL)
797 s->_raw_size = 0;
801 /* The check_relocs and adjust_dynamic_symbol entry points have
802 determined the sizes of the various dynamic sections. Allocate
803 memory for them. */
804 plt = false;
805 relocs = false;
806 reltext = false;
807 for (s = dynobj->sections; s != NULL; s = s->next)
809 const char *name;
810 boolean strip;
812 if ((s->flags & SEC_LINKER_CREATED) == 0)
813 continue;
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);
818 strip = false;
820 if (strcmp (name, ".plt") == 0)
822 if (s->_raw_size == 0)
824 /* Strip this section if we don't need it; see the
825 comment below. */
826 strip = true;
828 else
830 /* Remember whether there is a PLT. */
831 plt = true;
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. */
847 strip = true;
849 else
851 asection *target;
852 const char *outname;
854 /* Remember whether there are any relocation sections. */
855 relocs = true;
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,
860 s->output_section);
861 target = bfd_get_section_by_name (output_bfd, outname + 5);
862 if (target != NULL
863 && (target->flags & SEC_READONLY) != 0
864 && (target->flags & SEC_ALLOC) != 0)
865 reltext = true;
867 /* We use the reloc_count field as a counter if we need
868 to copy relocs into the output file. */
869 s->reloc_count = 0;
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. */
877 continue;
880 if (strip)
882 asection **spp;
884 for (spp = &s->output_section->owner->sections;
885 *spp != NULL;
886 spp = &(*spp)->next)
888 if (*spp == s->output_section)
890 bfd_section_list_remove (s->output_section->owner, spp);
891 --s->output_section->owner->section_count;
892 break;
895 continue;
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)
900 return false;
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))
913 if (!info->shared)
915 if (!add_dynamic_entry (DT_DEBUG, 0))
916 return false;
919 if (plt)
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))
925 return false;
928 if (relocs)
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)))
933 return false;
936 if (reltext)
938 if (!add_dynamic_entry (DT_TEXTREL, 0))
939 return false;
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. */
955 if (info->shared)
957 int c;
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;
965 continue;
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;
973 c++;
976 elf_link_hash_traverse (elf_hash_table (info),
977 i370_elf_adjust_dynindx,
978 (PTR) &c);
979 elf_hash_table (info)->dynsymcount += c;
982 return true;
985 /* Look through the relocs for a section during the first phase, and
986 allocate space in the global offset table or procedure linkage
987 table. */
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.
993 static boolean
994 i370_elf_check_relocs (abfd, info, sec, relocs)
995 bfd *abfd;
996 struct bfd_link_info *info;
997 asection *sec;
998 const Elf_Internal_Rela *relocs;
1000 bfd *dynobj;
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;
1006 asection *sreloc;
1008 if (info->relocateable)
1009 return true;
1011 #ifdef DEBUG
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));
1015 #endif
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);
1022 sreloc = NULL;
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)
1032 h = NULL;
1033 else
1034 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1036 if (info->shared)
1038 #ifdef DEBUG
1039 fprintf (stderr,
1040 "i370_elf_check_relocs needs to create relocation for %s\n",
1041 (h && h->root.root.string)
1042 ? h->root.root.string : "<unknown>");
1043 #endif
1044 if (sreloc == NULL)
1046 const char *name;
1048 name = (bfd_elf_string_from_elf_section
1049 (abfd,
1050 elf_elfheader (abfd)->e_shstrndx,
1051 elf_section_data (sec)->rel_hdr.sh_name));
1052 if (name == NULL)
1053 return false;
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);
1059 if (sreloc == NULL)
1061 flagword flags;
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;
1068 if (sreloc == NULL
1069 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1070 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1071 return false;
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. */
1086 return true;
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.
1095 static boolean
1096 i370_elf_finish_dynamic_sections (output_bfd, info)
1097 bfd *output_bfd;
1098 struct bfd_link_info *info;
1100 asection *sdyn;
1101 bfd *dynobj = elf_hash_table (info)->dynobj;
1102 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1104 #ifdef DEBUG
1105 fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
1106 #endif
1108 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1110 if (elf_hash_table (info)->dynamic_sections_created)
1112 asection *splt;
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;
1123 const char *name;
1124 boolean size;
1126 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1128 switch (dyn.d_tag)
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;
1136 if (name != NULL)
1138 asection *s;
1140 s = bfd_get_section_by_name (output_bfd, name);
1141 if (s == NULL)
1142 dyn.d_un.d_val = 0;
1143 else
1145 if (! size)
1146 dyn.d_un.d_ptr = s->vma;
1147 else
1149 if (s->_cooked_size != 0)
1150 dyn.d_un.d_val = s->_cooked_size;
1151 else
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 */
1163 if (sgot)
1165 unsigned char *contents = sgot->contents;
1166 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
1168 if (sdyn == NULL)
1169 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
1170 else
1171 bfd_put_32 (output_bfd,
1172 sdyn->output_section->vma + sdyn->output_offset,
1173 contents+4);
1175 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1178 if (info->shared)
1180 asection *sdynsym;
1181 asection *s;
1182 Elf_Internal_Sym sym;
1183 int maxdindx = 0;
1185 /* Set up the section symbols for the output sections. */
1187 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1188 BFD_ASSERT (sdynsym != NULL);
1190 sym.st_size = 0;
1191 sym.st_name = 0;
1192 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1193 sym.st_other = 0;
1195 for (s = output_bfd->sections; s != NULL; s = s->next)
1197 int indx, dindx;
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;
1204 if (dindx != -1)
1206 BFD_ASSERT(indx > 0);
1207 BFD_ASSERT(dindx > 0);
1209 if (dindx > maxdindx)
1210 maxdindx = dindx;
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 =
1222 maxdindx + 1;
1225 return true;
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
1233 zero.
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
1238 necessary.
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
1255 accordingly. */
1257 static boolean
1258 i370_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1259 contents, relocs, local_syms, local_sections)
1260 bfd *output_bfd;
1261 struct bfd_link_info *info;
1262 bfd *input_bfd;
1263 asection *input_section;
1264 bfd_byte *contents;
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;
1276 boolean ret = true;
1278 if (info->relocateable)
1279 return true;
1281 #ifdef DEBUG
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)" : "");
1287 #endif
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;
1306 bfd_vma relocation;
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),
1314 (int) r_type);
1316 bfd_set_error (bfd_error_bad_value);
1317 ret = false;
1318 continue;
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;
1333 else
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;
1344 if (info->shared
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. */
1357 relocation = 0;
1359 else
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)
1365 relocation = 0;
1366 else if (info->shared
1367 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1368 relocation = 0;
1369 else
1371 (*info->callbacks->undefined_symbol) (info,
1372 h->root.root.string,
1373 input_bfd,
1374 input_section,
1375 rel->r_offset,
1376 true);
1377 ret = false;
1378 continue;
1382 switch ((int) r_type)
1384 default:
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);
1391 ret = false;
1392 continue;
1394 case (int)R_I370_NONE:
1395 continue;
1397 /* Relocations that may need to be propagated if this is a shared
1398 object. */
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. */
1402 if (h == NULL
1403 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1404 break;
1405 /* fall through */
1407 /* Relocations that always need to be propagated if this is a shared
1408 object. */
1409 case (int)R_I370_ADDR31:
1410 case (int)R_I370_ADDR16:
1411 if (info->shared
1412 && r_symndx != 0)
1414 Elf_Internal_Rela outrel;
1415 int skip;
1417 #ifdef DEBUG
1418 fprintf (stderr,
1419 "i370_elf_relocate_section needs to create relocation for %s\n",
1420 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
1421 #endif
1423 /* When generating a shared object, these relocations
1424 are copied into the output file to be resolved at run
1425 time. */
1427 if (sreloc == NULL)
1429 const char *name;
1431 name = (bfd_elf_string_from_elf_section
1432 (input_bfd,
1433 elf_elfheader (input_bfd)->e_shstrndx,
1434 elf_section_data (input_section)->rel_hdr.sh_name));
1435 if (name == NULL)
1436 return false;
1438 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1439 && strcmp (bfd_get_section_name (input_bfd,
1440 input_section),
1441 name + 5) == 0);
1443 sreloc = bfd_get_section_by_name (dynobj, name);
1444 BFD_ASSERT (sreloc != NULL);
1447 skip = 0;
1449 outrel.r_offset =
1450 _bfd_elf_section_offset (output_bfd, info, input_section,
1451 rel->r_offset);
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);
1458 if (skip)
1459 memset (&outrel, 0, sizeof outrel);
1460 /* h->dynindx may be -1 if this symbol was marked to
1461 become local. */
1462 else if (h != NULL
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;
1471 else
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;
1478 else
1480 long indx;
1482 if (h == NULL)
1483 sec = local_sections[r_symndx];
1484 else
1486 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1487 || (h->root.type
1488 == bfd_link_hash_defweak));
1489 sec = h->root.u.def.section;
1491 if (sec != NULL && bfd_is_abs_section (sec))
1492 indx = 0;
1493 else if (sec == NULL || sec->owner == NULL)
1495 bfd_set_error (bfd_error_bad_value);
1496 return false;
1498 else
1500 asection *osec;
1502 osec = sec->output_section;
1503 indx = elf_section_data (osec)->dynindx;
1504 BFD_ASSERT(indx > 0);
1505 #ifdef DEBUG
1506 if (indx <= 0)
1508 printf ("indx=%d section=%s flags=%08x name=%s\n",
1509 indx, osec->name, osec->flags,
1510 h->root.root.string);
1512 #endif
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 *)
1522 sreloc->contents)
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. */
1529 if (skip == -1
1530 || (input_section->flags & SEC_ALLOC) != 0
1531 || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
1532 continue;
1534 break;
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,
1542 sym_name);
1544 bfd_set_error (bfd_error_invalid_operation);
1545 ret = false;
1546 continue;
1549 #ifdef DEBUG
1550 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
1551 howto->name,
1552 (int)r_type,
1553 sym_name,
1554 r_symndx,
1555 (long)offset,
1556 (long)addend);
1557 #endif
1559 r = _bfd_final_link_relocate (howto,
1560 input_bfd,
1561 input_section,
1562 contents,
1563 offset,
1564 relocation,
1565 addend);
1567 if (r != bfd_reloc_ok)
1569 ret = false;
1570 switch (r)
1572 default:
1573 break;
1575 case bfd_reloc_overflow:
1577 const char *name;
1579 if (h != NULL)
1580 name = h->root.root.string;
1581 else
1583 name = bfd_elf_string_from_elf_section (input_bfd,
1584 symtab_hdr->sh_link,
1585 sym->st_name);
1586 if (name == NULL)
1587 break;
1589 if (*name == '\0')
1590 name = bfd_section_name (input_bfd, sec);
1593 (*info->callbacks->reloc_overflow) (info,
1594 name,
1595 howto->name,
1596 (bfd_vma) 0,
1597 input_bfd,
1598 input_section,
1599 offset);
1601 break;
1607 #ifdef DEBUG
1608 fprintf (stderr, "\n");
1609 #endif
1611 return ret;
1614 static void
1615 i370_elf_post_process_headers (abfd, link_info)
1616 bfd * abfd;
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
1629 #ifdef EM_I370_OLD
1630 #define ELF_MACHINE_ALT1 EM_I370_OLD
1631 #endif
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 ()
1668 return 1;
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"