gas/
[platform/upstream/binutils.git] / bfd / elf32-i370.c
1 /* i370-specific support for 32-bit ELF
2    Copyright 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4    Written by Ian Lance Taylor, Cygnus Support.
5    Hacked by Linas Vepstas for i370 linas@linas.org
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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.  */
22
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 ...
28 */
29
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"
36
37 static reloc_howto_type *i370_elf_howto_table[ (int)R_I370_max ];
38
39 static reloc_howto_type i370_elf_howto_raw[] =
40 {
41   /* This reloc does nothing.  */
42   HOWTO (R_I370_NONE,           /* type */
43          0,                     /* rightshift */
44          2,                     /* size (0 = byte, 1 = short, 2 = long) */
45          32,                    /* bitsize */
46          FALSE,                 /* pc_relative */
47          0,                     /* bitpos */
48          complain_overflow_bitfield, /* complain_on_overflow */
49          bfd_elf_generic_reloc, /* special_function */
50          "R_I370_NONE",         /* name */
51          FALSE,                 /* partial_inplace */
52          0,                     /* src_mask */
53          0,                     /* dst_mask */
54          FALSE),                /* pcrel_offset */
55
56   /* A standard 31 bit relocation.  */
57   HOWTO (R_I370_ADDR31,         /* type */
58          0,                     /* rightshift */
59          2,                     /* size (0 = byte, 1 = short, 2 = long) */
60          31,                    /* bitsize */
61          FALSE,                 /* pc_relative */
62          0,                     /* bitpos */
63          complain_overflow_bitfield, /* complain_on_overflow */
64          bfd_elf_generic_reloc, /* special_function */
65          "R_I370_ADDR31",       /* name */
66          FALSE,                 /* partial_inplace */
67          0,                     /* src_mask */
68          0x7fffffff,            /* dst_mask */
69          FALSE),                /* pcrel_offset */
70
71   /* A standard 32 bit relocation.  */
72   HOWTO (R_I370_ADDR32,         /* type */
73          0,                     /* rightshift */
74          2,                     /* size (0 = byte, 1 = short, 2 = long) */
75          32,                    /* bitsize */
76          FALSE,                 /* pc_relative */
77          0,                     /* bitpos */
78          complain_overflow_bitfield, /* complain_on_overflow */
79          bfd_elf_generic_reloc, /* special_function */
80          "R_I370_ADDR32",       /* name */
81          FALSE,                 /* partial_inplace */
82          0,                     /* src_mask */
83          0xffffffff,            /* dst_mask */
84          FALSE),                /* pcrel_offset */
85
86   /* A standard 16 bit relocation.  */
87   HOWTO (R_I370_ADDR16,         /* type */
88          0,                     /* rightshift */
89          1,                     /* size (0 = byte, 1 = short, 2 = long) */
90          16,                    /* bitsize */
91          FALSE,                 /* pc_relative */
92          0,                     /* bitpos */
93          complain_overflow_bitfield, /* complain_on_overflow */
94          bfd_elf_generic_reloc, /* special_function */
95          "R_I370_ADDR16",       /* name */
96          FALSE,                 /* partial_inplace */
97          0,                     /* src_mask */
98          0xffff,                /* dst_mask */
99          FALSE),                /* pcrel_offset */
100
101   /* 31-bit PC relative */
102   HOWTO (R_I370_REL31,          /* type */
103          0,                     /* rightshift */
104          2,                     /* size (0 = byte, 1 = short, 2 = long) */
105          31,                    /* bitsize */
106          TRUE,                  /* pc_relative */
107          0,                     /* bitpos */
108          complain_overflow_bitfield, /* complain_on_overflow */
109          bfd_elf_generic_reloc, /* special_function */
110          "R_I370_REL31",        /* name */
111          FALSE,                 /* partial_inplace */
112          0,                     /* src_mask */
113          0x7fffffff,            /* dst_mask */
114          TRUE),                 /* pcrel_offset */
115
116   /* 32-bit PC relative */
117   HOWTO (R_I370_REL32,          /* type */
118          0,                     /* rightshift */
119          2,                     /* size (0 = byte, 1 = short, 2 = long) */
120          32,                    /* bitsize */
121          TRUE,                  /* pc_relative */
122          0,                     /* bitpos */
123          complain_overflow_bitfield, /* complain_on_overflow */
124          bfd_elf_generic_reloc, /* special_function */
125          "R_I370_REL32",        /* name */
126          FALSE,                 /* partial_inplace */
127          0,                     /* src_mask */
128          0xffffffff,            /* dst_mask */
129          TRUE),                 /* pcrel_offset */
130
131   /* A standard 12 bit relocation.  */
132   HOWTO (R_I370_ADDR12,         /* type */
133          0,                     /* rightshift */
134          1,                     /* size (0 = byte, 1 = short, 2 = long) */
135          12,                    /* bitsize */
136          FALSE,                 /* pc_relative */
137          0,                     /* bitpos */
138          complain_overflow_bitfield, /* complain_on_overflow */
139          bfd_elf_generic_reloc, /* special_function */
140          "R_I370_ADDR12",       /* name */
141          FALSE,                 /* partial_inplace */
142          0,                     /* src_mask */
143          0xfff,                 /* dst_mask */
144          FALSE),                /* pcrel_offset */
145
146   /* 12-bit PC relative */
147   HOWTO (R_I370_REL12,          /* type */
148          0,                     /* rightshift */
149          1,                     /* size (0 = byte, 1 = short, 2 = long) */
150          12,                    /* bitsize */
151          TRUE,                  /* pc_relative */
152          0,                     /* bitpos */
153          complain_overflow_bitfield, /* complain_on_overflow */
154          bfd_elf_generic_reloc, /* special_function */
155          "R_I370_REL12",        /* name */
156          FALSE,                 /* partial_inplace */
157          0,                     /* src_mask */
158          0xfff,                 /* dst_mask */
159          TRUE),                 /* pcrel_offset */
160
161   /* A standard 8 bit relocation.  */
162   HOWTO (R_I370_ADDR8,          /* type */
163          0,                     /* rightshift */
164          0,                     /* size (0 = byte, 1 = short, 2 = long) */
165          8,                     /* bitsize */
166          FALSE,                 /* pc_relative */
167          0,                     /* bitpos */
168          complain_overflow_bitfield, /* complain_on_overflow */
169          bfd_elf_generic_reloc, /* special_function */
170          "R_I370_ADDR8",        /* name */
171          FALSE,                 /* partial_inplace */
172          0,                     /* src_mask */
173          0xff,                  /* dst_mask */
174          FALSE),                /* pcrel_offset */
175
176   /* 8-bit PC relative */
177   HOWTO (R_I370_REL8,           /* type */
178          0,                     /* rightshift */
179          0,                     /* size (0 = byte, 1 = short, 2 = long) */
180          8,                     /* bitsize */
181          TRUE,                  /* pc_relative */
182          0,                     /* bitpos */
183          complain_overflow_bitfield, /* complain_on_overflow */
184          bfd_elf_generic_reloc, /* special_function */
185          "R_I370_REL8",         /* name */
186          FALSE,                 /* partial_inplace */
187          0,                     /* src_mask */
188          0xff,                  /* dst_mask */
189          TRUE),                 /* pcrel_offset */
190
191   /* This is used only by the dynamic linker.  The symbol should exist
192      both in the object being run and in some shared library.  The
193      dynamic linker copies the data addressed by the symbol from the
194      shared library into the object, because the object being
195      run has to have the data at some particular address.  */
196   HOWTO (R_I370_COPY,           /* type */
197          0,                     /* rightshift */
198          2,                     /* size (0 = byte, 1 = short, 2 = long) */
199          32,                    /* bitsize */
200          FALSE,                 /* pc_relative */
201          0,                     /* bitpos */
202          complain_overflow_bitfield, /* complain_on_overflow */
203          bfd_elf_generic_reloc,  /* special_function */
204          "R_I370_COPY",         /* name */
205          FALSE,                 /* partial_inplace */
206          0,                     /* src_mask */
207          0,                     /* dst_mask */
208          FALSE),                /* pcrel_offset */
209
210   /* Used only by the dynamic linker.  When the object is run, this
211      longword is set to the load address of the object, plus the
212      addend.  */
213   HOWTO (R_I370_RELATIVE,       /* type */
214          0,                     /* rightshift */
215          2,                     /* size (0 = byte, 1 = short, 2 = long) */
216          32,                    /* bitsize */
217          FALSE,                 /* pc_relative */
218          0,                     /* bitpos */
219          complain_overflow_bitfield, /* complain_on_overflow */
220          bfd_elf_generic_reloc,  /* special_function */
221          "R_I370_RELATIVE",     /* name */
222          FALSE,                 /* partial_inplace */
223          0,                     /* src_mask */
224          0xffffffff,            /* dst_mask */
225          FALSE),                /* pcrel_offset */
226
227 };
228 \f
229 static void i370_elf_howto_init
230   PARAMS ((void));
231 static reloc_howto_type *i370_elf_reloc_type_lookup
232   PARAMS ((bfd *, bfd_reloc_code_real_type));
233 static void i370_elf_info_to_howto
234   PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
235 static bfd_boolean i370_elf_set_private_flags
236   PARAMS ((bfd *, flagword));
237 \f
238 /* Initialize the i370_elf_howto_table, so that linear accesses can be done.  */
239
240 static void
241 i370_elf_howto_init ()
242 {
243   unsigned int i, type;
244
245   for (i = 0; i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]); i++)
246     {
247       type = i370_elf_howto_raw[i].type;
248       BFD_ASSERT (type < sizeof (i370_elf_howto_table) / sizeof (i370_elf_howto_table[0]));
249       i370_elf_howto_table[type] = &i370_elf_howto_raw[i];
250     }
251 }
252 \f
253 static reloc_howto_type *
254 i370_elf_reloc_type_lookup (abfd, code)
255      bfd *abfd ATTRIBUTE_UNUSED;
256      bfd_reloc_code_real_type code;
257 {
258   enum i370_reloc_type i370_reloc = R_I370_NONE;
259
260   if (!i370_elf_howto_table[ R_I370_ADDR31 ])   /* Initialize howto table if needed */
261     i370_elf_howto_init ();
262
263   switch ((int)code)
264     {
265     default:
266       return (reloc_howto_type *)NULL;
267
268     case BFD_RELOC_NONE:        i370_reloc = R_I370_NONE;       break;
269     case BFD_RELOC_32:          i370_reloc = R_I370_ADDR31;     break;
270     case BFD_RELOC_16:          i370_reloc = R_I370_ADDR16;     break;
271     case BFD_RELOC_32_PCREL:    i370_reloc = R_I370_REL31;      break;
272     case BFD_RELOC_CTOR:        i370_reloc = R_I370_ADDR31;     break;
273     case BFD_RELOC_I370_D12:    i370_reloc = R_I370_ADDR12;     break;
274     }
275
276   return i370_elf_howto_table[ (int)i370_reloc ];
277 };
278
279 static bfd_boolean i370_elf_merge_private_bfd_data
280   PARAMS ((bfd *, bfd *));
281 static bfd_boolean i370_elf_relocate_section
282   PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
283            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
284            asection **));
285 static void i370_elf_post_process_headers
286   PARAMS ((bfd *, struct bfd_link_info *));
287 static bfd_boolean i370_elf_create_dynamic_sections
288   PARAMS ((bfd *, struct bfd_link_info *));
289 static bfd_boolean i370_elf_section_from_shdr
290   PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
291 static bfd_boolean i370_elf_fake_sections
292   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
293 static bfd_boolean i370_elf_check_relocs
294   PARAMS ((bfd *, struct bfd_link_info *, asection *,
295            const Elf_Internal_Rela *));
296 static bfd_boolean i370_elf_adjust_dynamic_symbol
297   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
298 static bfd_boolean i370_elf_adjust_dynindx
299   PARAMS ((struct elf_link_hash_entry *, PTR));
300 static bfd_boolean i370_elf_size_dynamic_sections
301   PARAMS ((bfd *, struct bfd_link_info *));
302 static bfd_boolean i370_elf_finish_dynamic_sections
303   PARAMS ((bfd *, struct bfd_link_info *));
304
305 /* The name of the dynamic interpreter.  This is put in the .interp
306     section.  */
307
308 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
309
310 /* Set the howto pointer for an i370 ELF reloc.  */
311
312 static void
313 i370_elf_info_to_howto (abfd, cache_ptr, dst)
314      bfd *abfd ATTRIBUTE_UNUSED;
315      arelent *cache_ptr;
316      Elf_Internal_Rela *dst;
317 {
318   if (!i370_elf_howto_table[ R_I370_ADDR31 ])   /* Initialize howto table */
319     i370_elf_howto_init ();
320
321   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_I370_max);
322   cache_ptr->howto = i370_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
323 }
324
325 /* hack alert --  the following several routines look generic to me ...
326  * why are we bothering with them ???
327  */
328 /* Function to set whether a module needs the -mrelocatable bit set.  */
329 static bfd_boolean
330 i370_elf_set_private_flags (abfd, flags)
331      bfd *abfd;
332      flagword flags;
333 {
334   BFD_ASSERT (!elf_flags_init (abfd)
335               || elf_elfheader (abfd)->e_flags == flags);
336
337   elf_elfheader (abfd)->e_flags = flags;
338   elf_flags_init (abfd) = TRUE;
339   return TRUE;
340 }
341
342 /* Merge backend specific data from an object file to the output
343    object file when linking */
344 static bfd_boolean
345 i370_elf_merge_private_bfd_data (ibfd, obfd)
346      bfd *ibfd;
347      bfd *obfd;
348 {
349   flagword old_flags;
350   flagword new_flags;
351
352   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
353       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
354     return TRUE;
355
356   new_flags = elf_elfheader (ibfd)->e_flags;
357   old_flags = elf_elfheader (obfd)->e_flags;
358   if (!elf_flags_init (obfd))   /* First call, no flags set */
359     {
360       elf_flags_init (obfd) = TRUE;
361       elf_elfheader (obfd)->e_flags = new_flags;
362     }
363
364   else if (new_flags == old_flags)      /* Compatible flags are ok */
365     ;
366
367   else                                  /* Incompatible flags */
368     {
369       (*_bfd_error_handler)
370         ("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
371          ibfd, (long) new_flags, (long) old_flags);
372
373       bfd_set_error (bfd_error_bad_value);
374       return FALSE;
375     }
376
377   return TRUE;
378 }
379 \f
380 /* Handle an i370 specific section when reading an object file.  This
381    is called when elfcode.h finds a section with an unknown type.  */
382 /* XXX hack alert bogus This routine is mostly all junk and almost
383  * certainly does the wrong thing.  Its here simply because it does
384  * just enough to allow glibc-2.1 ld.so to compile & link.
385  */
386
387 static bfd_boolean
388 i370_elf_section_from_shdr (abfd, hdr, name)
389      bfd *abfd;
390      Elf_Internal_Shdr *hdr;
391      const char *name;
392 {
393   asection *newsect;
394   flagword flags;
395
396   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
397     return FALSE;
398
399   newsect = hdr->bfd_section;
400   flags = bfd_get_section_flags (abfd, newsect);
401   if (hdr->sh_flags & SHF_EXCLUDE)
402     flags |= SEC_EXCLUDE;
403
404   if (hdr->sh_type == SHT_ORDERED)
405     flags |= SEC_SORT_ENTRIES;
406
407   bfd_set_section_flags (abfd, newsect, flags);
408   return TRUE;
409 }
410 \f
411 /* Set up any other section flags and such that may be necessary.  */
412 /* XXX hack alert bogus This routine is mostly all junk and almost
413  * certainly does the wrong thing.  Its here simply because it does
414  * just enough to allow glibc-2.1 ld.so to compile & link.
415  */
416
417 static bfd_boolean
418 i370_elf_fake_sections (abfd, shdr, asect)
419      bfd *abfd ATTRIBUTE_UNUSED;
420      Elf_Internal_Shdr *shdr;
421      asection *asect;
422 {
423   if ((asect->flags & SEC_EXCLUDE) != 0)
424     shdr->sh_flags |= SHF_EXCLUDE;
425
426   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
427     shdr->sh_type = SHT_ORDERED;
428
429   return TRUE;
430 }
431 \f
432 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
433    to output sections (just like _bfd_elf_create_dynamic_sections has
434    to create .dynbss and .rela.bss).  */
435 /* XXX hack alert bogus This routine is mostly all junk and almost
436  * certainly does the wrong thing.  Its here simply because it does
437  * just enough to allow glibc-2.1 ld.so to compile & link.
438  */
439
440 static bfd_boolean
441 i370_elf_create_dynamic_sections (abfd, info)
442      bfd *abfd;
443      struct bfd_link_info *info;
444 {
445   register asection *s;
446   flagword flags;
447
448   if (!_bfd_elf_create_dynamic_sections(abfd, info))
449     return FALSE;
450
451   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
452            | SEC_LINKER_CREATED);
453
454   s = bfd_make_section (abfd, ".dynsbss");
455   if (s == NULL
456       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
457     return FALSE;
458
459   if (! info->shared)
460     {
461       s = bfd_make_section (abfd, ".rela.sbss");
462       if (s == NULL
463           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
464           || ! bfd_set_section_alignment (abfd, s, 2))
465         return FALSE;
466     }
467
468    /* xxx beats me, seem to need a rela.text ...  */
469    s = bfd_make_section (abfd, ".rela.text");
470    if (s == NULL
471       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
472       || ! bfd_set_section_alignment (abfd, s, 2))
473     return FALSE;
474   return TRUE;
475 }
476
477 /* Adjust a symbol defined by a dynamic object and referenced by a
478    regular object.  The current definition is in some section of the
479    dynamic object, but we're not including those sections.  We have to
480    change the definition to something the rest of the link can
481    understand.  */
482 /* XXX hack alert bogus This routine is mostly all junk and almost
483  * certainly does the wrong thing.  Its here simply because it does
484  * just enough to allow glibc-2.1 ld.so to compile & link.
485  */
486
487 static bfd_boolean
488 i370_elf_adjust_dynamic_symbol (info, h)
489      struct bfd_link_info *info;
490      struct elf_link_hash_entry *h;
491 {
492   bfd *dynobj = elf_hash_table (info)->dynobj;
493   asection *s;
494   unsigned int power_of_two;
495
496 #ifdef DEBUG
497   fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
498            h->root.root.string);
499 #endif
500
501   /* Make sure we know what is going on here.  */
502   BFD_ASSERT (dynobj != NULL
503               && (h->needs_plt
504                   || h->u.weakdef != NULL
505                   || (h->def_dynamic
506                       && h->ref_regular
507                       && !h->def_regular)));
508
509   s = bfd_get_section_by_name (dynobj, ".rela.text");
510   BFD_ASSERT (s != NULL);
511   s->size += sizeof (Elf32_External_Rela);
512
513   /* If this is a weak symbol, and there is a real definition, the
514      processor independent code will have arranged for us to see the
515      real definition first, and we can just use the same value.  */
516   if (h->u.weakdef != NULL)
517     {
518       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
519                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
520       h->root.u.def.section = h->u.weakdef->root.u.def.section;
521       h->root.u.def.value = h->u.weakdef->root.u.def.value;
522       return TRUE;
523     }
524
525   /* This is a reference to a symbol defined by a dynamic object which
526      is not a function.  */
527
528   /* If we are creating a shared library, we must presume that the
529      only references to the symbol are via the global offset table.
530      For such cases we need not do anything here; the relocations will
531      be handled correctly by relocate_section.  */
532   if (info->shared)
533     return TRUE;
534
535   /* We must allocate the symbol in our .dynbss section, which will
536      become part of the .bss section of the executable.  There will be
537      an entry for this symbol in the .dynsym section.  The dynamic
538      object will contain position independent code, so all references
539      from the dynamic object to this symbol will go through the global
540      offset table.  The dynamic linker will use the .dynsym entry to
541      determine the address it must put in the global offset table, so
542      both the dynamic object and the regular object will refer to the
543      same memory location for the variable.
544
545      Of course, if the symbol is sufficiently small, we must instead
546      allocate it in .sbss.  FIXME: It would be better to do this if and
547      only if there were actually SDAREL relocs for that symbol.  */
548
549   if (h->size <= elf_gp_size (dynobj))
550     s = bfd_get_section_by_name (dynobj, ".dynsbss");
551   else
552     s = bfd_get_section_by_name (dynobj, ".dynbss");
553   BFD_ASSERT (s != NULL);
554
555   /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
556      copy the initial value out of the dynamic object and into the
557      runtime process image.  We need to remember the offset into the
558      .rela.bss section we are going to use.  */
559   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
560     {
561       asection *srel;
562
563       if (h->size <= elf_gp_size (dynobj))
564         srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
565       else
566         srel = bfd_get_section_by_name (dynobj, ".rela.bss");
567       BFD_ASSERT (srel != NULL);
568       srel->size += sizeof (Elf32_External_Rela);
569       h->needs_copy = 1;
570     }
571
572   /* We need to figure out the alignment required for this symbol.  I
573      have no idea how ELF linkers handle this.  */
574   power_of_two = bfd_log2 (h->size);
575   if (power_of_two > 4)
576     power_of_two = 4;
577
578   /* Apply the required alignment.  */
579   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
580   if (power_of_two > bfd_get_section_alignment (dynobj, s))
581     {
582       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
583         return FALSE;
584     }
585
586   /* Define the symbol as being at this point in the section.  */
587   h->root.u.def.section = s;
588   h->root.u.def.value = s->size;
589
590   /* Increment the section size to make room for the symbol.  */
591   s->size += h->size;
592
593   return TRUE;
594 }
595 \f
596 /* Increment the index of a dynamic symbol by a given amount.  Called
597    via elf_link_hash_traverse.  */
598 /* XXX hack alert bogus This routine is mostly all junk and almost
599  * certainly does the wrong thing.  Its here simply because it does
600  * just enough to allow glibc-2.1 ld.so to compile & link.
601  */
602
603 static bfd_boolean
604 i370_elf_adjust_dynindx (h, cparg)
605      struct elf_link_hash_entry *h;
606      PTR cparg;
607 {
608   int *cp = (int *) cparg;
609
610 #ifdef DEBUG
611   fprintf (stderr,
612            "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",
613            h->dynindx, *cp);
614 #endif
615
616   if (h->root.type == bfd_link_hash_warning)
617     h = (struct elf_link_hash_entry *) h->root.u.i.link;
618
619   if (h->dynindx != -1)
620     h->dynindx += *cp;
621
622   return TRUE;
623 }
624 \f
625 /* Set the sizes of the dynamic sections.  */
626 /* XXX hack alert bogus This routine is mostly all junk and almost
627  * certainly does the wrong thing.  Its here simply because it does
628  * just enough to allow glibc-2.1 ld.so to compile & link.
629  */
630
631 static bfd_boolean
632 i370_elf_size_dynamic_sections (output_bfd, info)
633      bfd *output_bfd;
634      struct bfd_link_info *info;
635 {
636   bfd *dynobj;
637   asection *s;
638   bfd_boolean plt;
639   bfd_boolean relocs;
640   bfd_boolean reltext;
641
642 #ifdef DEBUG
643   fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
644 #endif
645
646   dynobj = elf_hash_table (info)->dynobj;
647   BFD_ASSERT (dynobj != NULL);
648
649   if (elf_hash_table (info)->dynamic_sections_created)
650     {
651       /* Set the contents of the .interp section to the interpreter.  */
652       if (info->executable)
653         {
654           s = bfd_get_section_by_name (dynobj, ".interp");
655           BFD_ASSERT (s != NULL);
656           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
657           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
658         }
659     }
660   else
661     {
662       /* We may have created entries in the .rela.got, .rela.sdata, and
663          .rela.sdata2 sections.  However, if we are not creating the
664          dynamic sections, we will not actually use these entries.  Reset
665          the size of .rela.got, et al, which will cause it to get
666          stripped from the output file below.  */
667       static char *rela_sections[] = { ".rela.got", ".rela.sdata",
668                                        ".rela.sdata2", ".rela.sbss",
669                                        (char *)0 };
670       char **p;
671
672       for (p = rela_sections; *p != (char *)0; p++)
673         {
674           s = bfd_get_section_by_name (dynobj, *p);
675           if (s != NULL)
676             s->size = 0;
677         }
678     }
679
680   /* The check_relocs and adjust_dynamic_symbol entry points have
681      determined the sizes of the various dynamic sections.  Allocate
682      memory for them.  */
683   plt = FALSE;
684   relocs = FALSE;
685   reltext = FALSE;
686   for (s = dynobj->sections; s != NULL; s = s->next)
687     {
688       const char *name;
689       bfd_boolean strip;
690
691       if ((s->flags & SEC_LINKER_CREATED) == 0)
692         continue;
693
694       /* It's OK to base decisions on the section name, because none
695          of the dynobj section names depend upon the input files.  */
696       name = bfd_get_section_name (dynobj, s);
697       strip = FALSE;
698
699       if (strcmp (name, ".plt") == 0)
700         {
701           if (s->size == 0)
702             {
703               /* Strip this section if we don't need it; see the
704                  comment below.  */
705               strip = TRUE;
706             }
707           else
708             {
709               /* Remember whether there is a PLT.  */
710               plt = TRUE;
711             }
712         }
713       else if (strncmp (name, ".rela", 5) == 0)
714         {
715           if (s->size == 0)
716             {
717               /* If we don't need this section, strip it from the
718                  output file.  This is mostly to handle .rela.bss and
719                  .rela.plt.  We must create both sections in
720                  create_dynamic_sections, because they must be created
721                  before the linker maps input sections to output
722                  sections.  The linker does that before
723                  adjust_dynamic_symbol is called, and it is that
724                  function which decides whether anything needs to go
725                  into these sections.  */
726               strip = TRUE;
727             }
728           else
729             {
730               asection *target;
731               const char *outname;
732
733               /* Remember whether there are any relocation sections.  */
734               relocs = TRUE;
735
736               /* If this relocation section applies to a read only
737                  section, then we probably need a DT_TEXTREL entry.  */
738               outname = bfd_get_section_name (output_bfd,
739                                               s->output_section);
740               target = bfd_get_section_by_name (output_bfd, outname + 5);
741               if (target != NULL
742                   && (target->flags & SEC_READONLY) != 0
743                   && (target->flags & SEC_ALLOC) != 0)
744                 reltext = TRUE;
745
746               /* We use the reloc_count field as a counter if we need
747                  to copy relocs into the output file.  */
748               s->reloc_count = 0;
749             }
750         }
751       else if (strcmp (name, ".got") != 0
752                && strcmp (name, ".sdata") != 0
753                && strcmp (name, ".sdata2") != 0)
754         {
755           /* It's not one of our sections, so don't allocate space.  */
756           continue;
757         }
758
759       if (strip)
760         {
761           asection **spp;
762
763           for (spp = &s->output_section->owner->sections;
764                *spp != NULL;
765                spp = &(*spp)->next)
766             {
767               if (*spp == s->output_section)
768                 {
769                   bfd_section_list_remove (s->output_section->owner, spp);
770                   --s->output_section->owner->section_count;
771                   break;
772                 }
773             }
774           continue;
775         }
776       /* Allocate memory for the section contents.  */
777       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
778       if (s->contents == NULL && s->size != 0)
779         return FALSE;
780     }
781
782   if (elf_hash_table (info)->dynamic_sections_created)
783     {
784       /* Add some entries to the .dynamic section.  We fill in the
785          values later, in i370_elf_finish_dynamic_sections, but we
786          must add the entries now so that we get the correct size for
787          the .dynamic section.  The DT_DEBUG entry is filled in by the
788          dynamic linker and used by the debugger.  */
789 #define add_dynamic_entry(TAG, VAL) \
790   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
791
792       if (!info->shared)
793         {
794           if (!add_dynamic_entry (DT_DEBUG, 0))
795             return FALSE;
796         }
797
798       if (plt)
799         {
800           if (!add_dynamic_entry (DT_PLTGOT, 0)
801               || !add_dynamic_entry (DT_PLTRELSZ, 0)
802               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
803               || !add_dynamic_entry (DT_JMPREL, 0))
804             return FALSE;
805         }
806
807       if (relocs)
808         {
809           if (!add_dynamic_entry (DT_RELA, 0)
810               || !add_dynamic_entry (DT_RELASZ, 0)
811               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
812             return FALSE;
813         }
814
815       if (reltext)
816         {
817           if (!add_dynamic_entry (DT_TEXTREL, 0))
818             return FALSE;
819           info->flags |= DF_TEXTREL;
820         }
821     }
822 #undef add_dynamic_entry
823
824   /* If we are generating a shared library, we generate a section
825      symbol for each output section.  These are local symbols, which
826      means that they must come first in the dynamic symbol table.
827      That means we must increment the dynamic symbol index of every
828      other dynamic symbol.
829
830      FIXME: We assume that there will never be relocations to
831      locations in linker-created sections that do not have
832      externally-visible names. Instead, we should work out precisely
833      which sections relocations are targeted at.  */
834   if (info->shared)
835     {
836       int c;
837
838       for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
839         {
840           if ((s->flags & SEC_LINKER_CREATED) != 0
841               || (s->flags & SEC_ALLOC) == 0)
842             {
843               elf_section_data (s)->dynindx = -1;
844               continue;
845             }
846
847           /* These symbols will have no names, so we don't need to
848              fiddle with dynstr_index.  */
849
850           elf_section_data (s)->dynindx = c + 1;
851
852           c++;
853         }
854
855       elf_link_hash_traverse (elf_hash_table (info),
856                               i370_elf_adjust_dynindx,
857                               (PTR) &c);
858       elf_hash_table (info)->dynsymcount += c;
859     }
860
861   return TRUE;
862 }
863 \f
864 /* Look through the relocs for a section during the first phase, and
865    allocate space in the global offset table or procedure linkage
866    table.  */
867 /* XXX hack alert bogus This routine is mostly all junk and almost
868  * certainly does the wrong thing.  Its here simply because it does
869  * just enough to allow glibc-2.1 ld.so to compile & link.
870  */
871
872 static bfd_boolean
873 i370_elf_check_relocs (abfd, info, sec, relocs)
874      bfd *abfd;
875      struct bfd_link_info *info;
876      asection *sec;
877      const Elf_Internal_Rela *relocs;
878 {
879   bfd *dynobj;
880   Elf_Internal_Shdr *symtab_hdr;
881   struct elf_link_hash_entry **sym_hashes;
882   const Elf_Internal_Rela *rel;
883   const Elf_Internal_Rela *rel_end;
884   bfd_vma *local_got_offsets;
885   asection *sreloc;
886
887   if (info->relocatable)
888     return TRUE;
889
890 #ifdef DEBUG
891   _bfd_error_handler ("i370_elf_check_relocs called for section %A in %B",
892                       sec, abfd);
893 #endif
894
895   dynobj = elf_hash_table (info)->dynobj;
896   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
897   sym_hashes = elf_sym_hashes (abfd);
898   local_got_offsets = elf_local_got_offsets (abfd);
899
900   sreloc = NULL;
901
902   rel_end = relocs + sec->reloc_count;
903   for (rel = relocs; rel < rel_end; rel++)
904     {
905       unsigned long r_symndx;
906       struct elf_link_hash_entry *h;
907
908       r_symndx = ELF32_R_SYM (rel->r_info);
909       if (r_symndx < symtab_hdr->sh_info)
910         h = NULL;
911       else
912         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
913
914       if (info->shared)
915         {
916 #ifdef DEBUG
917           fprintf (stderr,
918                    "i370_elf_check_relocs needs to create relocation for %s\n",
919                    (h && h->root.root.string)
920                    ? h->root.root.string : "<unknown>");
921 #endif
922           if (sreloc == NULL)
923             {
924               const char *name;
925
926               name = (bfd_elf_string_from_elf_section
927                       (abfd,
928                        elf_elfheader (abfd)->e_shstrndx,
929                        elf_section_data (sec)->rel_hdr.sh_name));
930               if (name == NULL)
931                 return FALSE;
932
933               BFD_ASSERT (strncmp (name, ".rela", 5) == 0
934                           && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0);
935
936               sreloc = bfd_get_section_by_name (dynobj, name);
937               if (sreloc == NULL)
938                 {
939                   flagword flags;
940
941                   sreloc = bfd_make_section (dynobj, name);
942                   flags = (SEC_HAS_CONTENTS | SEC_READONLY
943                            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
944                   if ((sec->flags & SEC_ALLOC) != 0)
945                     flags |= SEC_ALLOC | SEC_LOAD;
946                   if (sreloc == NULL
947                       || ! bfd_set_section_flags (dynobj, sreloc, flags)
948                       || ! bfd_set_section_alignment (dynobj, sreloc, 2))
949                     return FALSE;
950                 }
951             }
952
953           sreloc->size += sizeof (Elf32_External_Rela);
954
955           /* FIXME: We should here do what the m68k and i386
956              backends do: if the reloc is pc-relative, record it
957              in case it turns out that the reloc is unnecessary
958              because the symbol is forced local by versioning or
959              we are linking with -Bdynamic.  Fortunately this
960              case is not frequent.  */
961         }
962     }
963
964   return TRUE;
965 }
966 \f
967 /* Finish up the dynamic sections.  */
968 /* XXX hack alert bogus This routine is mostly all junk and almost
969  * certainly does the wrong thing.  Its here simply because it does
970  * just enough to allow glibc-2.1 ld.so to compile & link.
971  */
972
973 static bfd_boolean
974 i370_elf_finish_dynamic_sections (output_bfd, info)
975      bfd *output_bfd;
976      struct bfd_link_info *info;
977 {
978   asection *sdyn;
979   bfd *dynobj = elf_hash_table (info)->dynobj;
980   asection *sgot = bfd_get_section_by_name (dynobj, ".got");
981
982 #ifdef DEBUG
983   fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
984 #endif
985
986   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
987
988   if (elf_hash_table (info)->dynamic_sections_created)
989     {
990       asection *splt;
991       Elf32_External_Dyn *dyncon, *dynconend;
992
993       splt = bfd_get_section_by_name (dynobj, ".plt");
994       BFD_ASSERT (splt != NULL && sdyn != NULL);
995
996       dyncon = (Elf32_External_Dyn *) sdyn->contents;
997       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
998       for (; dyncon < dynconend; dyncon++)
999         {
1000           Elf_Internal_Dyn dyn;
1001           const char *name;
1002           bfd_boolean size;
1003
1004           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1005
1006           switch (dyn.d_tag)
1007             {
1008             case DT_PLTGOT:   name = ".plt";      size = FALSE; break;
1009             case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE;  break;
1010             case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
1011             default:          name = NULL;        size = FALSE; break;
1012             }
1013
1014           if (name != NULL)
1015             {
1016               asection *s;
1017
1018               s = bfd_get_section_by_name (output_bfd, name);
1019               if (s == NULL)
1020                 dyn.d_un.d_val = 0;
1021               else
1022                 {
1023                   if (! size)
1024                     dyn.d_un.d_ptr = s->vma;
1025                   else
1026                     dyn.d_un.d_val = s->size;
1027                 }
1028               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1029             }
1030         }
1031     }
1032
1033   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
1034      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
1035 /* XXX this is clearly very wrong for the 370 arch */
1036   if (sgot)
1037     {
1038       unsigned char *contents = sgot->contents;
1039       bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
1040
1041       if (sdyn == NULL)
1042         bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
1043       else
1044         bfd_put_32 (output_bfd,
1045                     sdyn->output_section->vma + sdyn->output_offset,
1046                     contents+4);
1047
1048       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1049     }
1050
1051   if (info->shared)
1052     {
1053       asection *sdynsym;
1054       asection *s;
1055       Elf_Internal_Sym sym;
1056       int maxdindx = 0;
1057
1058       /* Set up the section symbols for the output sections.  */
1059
1060       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1061       BFD_ASSERT (sdynsym != NULL);
1062
1063       sym.st_size = 0;
1064       sym.st_name = 0;
1065       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1066       sym.st_other = 0;
1067
1068       for (s = output_bfd->sections; s != NULL; s = s->next)
1069         {
1070           int indx, dindx;
1071           Elf32_External_Sym *esym;
1072
1073           sym.st_value = s->vma;
1074
1075           indx = elf_section_data (s)->this_idx;
1076           dindx = elf_section_data (s)->dynindx;
1077           if (dindx != -1)
1078             {
1079               BFD_ASSERT(indx > 0);
1080               BFD_ASSERT(dindx > 0);
1081
1082               if (dindx > maxdindx)
1083                 maxdindx = dindx;
1084
1085               sym.st_shndx = indx;
1086
1087               esym = (Elf32_External_Sym *) sdynsym->contents + dindx;
1088               bfd_elf32_swap_symbol_out (output_bfd, &sym, (PTR) esym, (PTR) 0);
1089             }
1090         }
1091
1092       /* Set the sh_info field of the output .dynsym section to the
1093          index of the first global symbol.  */
1094       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1095         maxdindx + 1;
1096     }
1097
1098   return TRUE;
1099 }
1100 \f
1101 /* The RELOCATE_SECTION function is called by the ELF backend linker
1102    to handle the relocations for a section.
1103
1104    The relocs are always passed as Rela structures; if the section
1105    actually uses Rel structures, the r_addend field will always be
1106    zero.
1107
1108    This function is responsible for adjust the section contents as
1109    necessary, and (if using Rela relocs and generating a
1110    relocatable output file) adjusting the reloc addend as
1111    necessary.
1112
1113    This function does not have to worry about setting the reloc
1114    address or the reloc symbol index.
1115
1116    LOCAL_SYMS is a pointer to the swapped in local symbols.
1117
1118    LOCAL_SECTIONS is an array giving the section in the input file
1119    corresponding to the st_shndx field of each local symbol.
1120
1121    The global hash table entry for the global symbols can be found
1122    via elf_sym_hashes (input_bfd).
1123
1124    When generating relocatable output, this function must handle
1125    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1126    going to be the section symbol corresponding to the output
1127    section, which means that the addend must be adjusted
1128    accordingly.  */
1129
1130 static bfd_boolean
1131 i370_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1132                            contents, relocs, local_syms, local_sections)
1133      bfd *output_bfd;
1134      struct bfd_link_info *info;
1135      bfd *input_bfd;
1136      asection *input_section;
1137      bfd_byte *contents;
1138      Elf_Internal_Rela *relocs;
1139      Elf_Internal_Sym *local_syms;
1140      asection **local_sections;
1141 {
1142   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1143   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1144   bfd *dynobj = elf_hash_table (info)->dynobj;
1145   Elf_Internal_Rela *rel = relocs;
1146   Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
1147   asection *sreloc = NULL;
1148   bfd_vma *local_got_offsets;
1149   bfd_boolean ret = TRUE;
1150
1151   if (info->relocatable)
1152     return TRUE;
1153
1154 #ifdef DEBUG
1155   _bfd_error_handler ("i370_elf_relocate_section called for %B section %A, %ld relocations%s",
1156                       input_bfd, input_section,
1157                       (long) input_section->reloc_count,
1158                       (info->relocatable) ? " (relocatable)" : "");
1159 #endif
1160
1161   if (!i370_elf_howto_table[ R_I370_ADDR31 ])   /* Initialize howto table if needed */
1162     i370_elf_howto_init ();
1163
1164   local_got_offsets = elf_local_got_offsets (input_bfd);
1165
1166   for (; rel < relend; rel++)
1167     {
1168       enum i370_reloc_type r_type       = (enum i370_reloc_type)ELF32_R_TYPE (rel->r_info);
1169       bfd_vma offset                    = rel->r_offset;
1170       bfd_vma addend                    = rel->r_addend;
1171       bfd_reloc_status_type r           = bfd_reloc_other;
1172       Elf_Internal_Sym *sym             = (Elf_Internal_Sym *)0;
1173       asection *sec                     = (asection *)0;
1174       struct elf_link_hash_entry *h     = (struct elf_link_hash_entry *)0;
1175       const char *sym_name              = (const char *)0;
1176       reloc_howto_type *howto;
1177       unsigned long r_symndx;
1178       bfd_vma relocation;
1179
1180       /* Unknown relocation handling */
1181       if ((unsigned)r_type >= (unsigned)R_I370_max
1182           || !i370_elf_howto_table[(int)r_type])
1183         {
1184           (*_bfd_error_handler) ("%B: unknown relocation type %d",
1185                                  input_bfd,
1186                                  (int) r_type);
1187
1188           bfd_set_error (bfd_error_bad_value);
1189           ret = FALSE;
1190           continue;
1191         }
1192
1193       howto = i370_elf_howto_table[(int)r_type];
1194       r_symndx = ELF32_R_SYM (rel->r_info);
1195
1196       if (r_symndx < symtab_hdr->sh_info)
1197         {
1198           sym = local_syms + r_symndx;
1199           sec = local_sections[r_symndx];
1200           sym_name = "<local symbol>";
1201
1202           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1203           addend = rel->r_addend;
1204         }
1205       else
1206         {
1207           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1208           while (h->root.type == bfd_link_hash_indirect
1209                  || h->root.type == bfd_link_hash_warning)
1210             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1211           sym_name = h->root.root.string;
1212           if (h->root.type == bfd_link_hash_defined
1213               || h->root.type == bfd_link_hash_defweak)
1214             {
1215               sec = h->root.u.def.section;
1216               if (info->shared
1217                   && ((! info->symbolic && h->dynindx != -1)
1218                       || !h->def_regular)
1219                   && (input_section->flags & SEC_ALLOC) != 0
1220                   && (r_type == R_I370_ADDR31
1221                       || r_type == R_I370_COPY
1222                       || r_type == R_I370_ADDR16
1223                       || r_type == R_I370_RELATIVE))
1224                 {
1225                   /* In these cases, we don't need the relocation
1226                      value.  We check specially because in some
1227                      obscure cases sec->output_section will be NULL.  */
1228                   relocation = 0;
1229                 }
1230               else
1231                 relocation = (h->root.u.def.value
1232                               + sec->output_section->vma
1233                               + sec->output_offset);
1234             }
1235           else if (h->root.type == bfd_link_hash_undefweak)
1236             relocation = 0;
1237           else if (info->unresolved_syms_in_objects == RM_IGNORE
1238                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1239             relocation = 0;
1240           else
1241             {
1242               if ((*info->callbacks->undefined_symbol)
1243                   (info, h->root.root.string, input_bfd,
1244                    input_section, rel->r_offset,
1245                    (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
1246                     || ELF_ST_VISIBILITY (h->other))))
1247                 {
1248                   ret = FALSE;
1249                   continue;
1250                 }
1251               relocation = 0;
1252             }
1253         }
1254
1255       switch ((int) r_type)
1256         {
1257         default:
1258           (*_bfd_error_handler)
1259             ("%B: unknown relocation type %d for symbol %s",
1260              input_bfd, (int) r_type, sym_name);
1261
1262           bfd_set_error (bfd_error_bad_value);
1263           ret = FALSE;
1264           continue;
1265
1266         case (int)R_I370_NONE:
1267           continue;
1268
1269         /* Relocations that may need to be propagated if this is a shared
1270            object.  */
1271         case (int)R_I370_REL31:
1272           /* If these relocations are not to a named symbol, they can be
1273              handled right here, no need to bother the dynamic linker.  */
1274           if (h == NULL
1275               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1276             break;
1277         /* fall through */
1278
1279         /* Relocations that always need to be propagated if this is a shared
1280            object.  */
1281         case (int)R_I370_ADDR31:
1282         case (int)R_I370_ADDR16:
1283           if (info->shared
1284               && r_symndx != 0)
1285             {
1286               Elf_Internal_Rela outrel;
1287               bfd_byte *loc;
1288               int skip;
1289
1290 #ifdef DEBUG
1291               fprintf (stderr,
1292                        "i370_elf_relocate_section needs to create relocation for %s\n",
1293                        (h && h->root.root.string) ? h->root.root.string : "<unknown>");
1294 #endif
1295
1296               /* When generating a shared object, these relocations
1297                  are copied into the output file to be resolved at run
1298                  time.  */
1299
1300               if (sreloc == NULL)
1301                 {
1302                   const char *name;
1303
1304                   name = (bfd_elf_string_from_elf_section
1305                           (input_bfd,
1306                            elf_elfheader (input_bfd)->e_shstrndx,
1307                            elf_section_data (input_section)->rel_hdr.sh_name));
1308                   if (name == NULL)
1309                     return FALSE;
1310
1311                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1312                               && strcmp (bfd_get_section_name (input_bfd,
1313                                                                input_section),
1314                                          name + 5) == 0);
1315
1316                   sreloc = bfd_get_section_by_name (dynobj, name);
1317                   BFD_ASSERT (sreloc != NULL);
1318                 }
1319
1320               skip = 0;
1321
1322               outrel.r_offset =
1323                 _bfd_elf_section_offset (output_bfd, info, input_section,
1324                                          rel->r_offset);
1325               if (outrel.r_offset == (bfd_vma) -1
1326                   || outrel.r_offset == (bfd_vma) -2)
1327                 skip = (int) outrel.r_offset;
1328               outrel.r_offset += (input_section->output_section->vma
1329                                   + input_section->output_offset);
1330
1331               if (skip)
1332                 memset (&outrel, 0, sizeof outrel);
1333               /* h->dynindx may be -1 if this symbol was marked to
1334                  become local.  */
1335               else if (h != NULL
1336                        && ((! info->symbolic && h->dynindx != -1)
1337                            || !h->def_regular))
1338                 {
1339                   BFD_ASSERT (h->dynindx != -1);
1340                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1341                   outrel.r_addend = rel->r_addend;
1342                 }
1343               else
1344                 {
1345                   if (r_type == R_I370_ADDR31)
1346                     {
1347                       outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
1348                       outrel.r_addend = relocation + rel->r_addend;
1349                     }
1350                   else
1351                     {
1352                       long indx;
1353
1354                       if (bfd_is_abs_section (sec))
1355                         indx = 0;
1356                       else if (sec == NULL || sec->owner == NULL)
1357                         {
1358                           bfd_set_error (bfd_error_bad_value);
1359                           return FALSE;
1360                         }
1361                       else
1362                         {
1363                           asection *osec;
1364
1365                           osec = sec->output_section;
1366                           indx = elf_section_data (osec)->dynindx;
1367                           BFD_ASSERT(indx > 0);
1368 #ifdef DEBUG
1369                           if (indx <= 0)
1370                             {
1371                               printf ("indx=%d section=%s flags=%08x name=%s\n",
1372                                       indx, osec->name, osec->flags,
1373                                       h->root.root.string);
1374                             }
1375 #endif
1376                         }
1377
1378                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1379                       outrel.r_addend = relocation + rel->r_addend;
1380                     }
1381                 }
1382
1383               loc = sreloc->contents;
1384               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1385               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1386
1387               /* This reloc will be computed at runtime, so there's no
1388                  need to do anything now, unless this is a RELATIVE
1389                  reloc in an unallocated section.  */
1390               if (skip == -1
1391                   || (input_section->flags & SEC_ALLOC) != 0
1392                   || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
1393                 continue;
1394             }
1395           break;
1396
1397         case (int)R_I370_COPY:
1398         case (int)R_I370_RELATIVE:
1399           (*_bfd_error_handler)
1400             ("%B: Relocation %s is not yet supported for symbol %s.",
1401              input_bfd,
1402              i370_elf_howto_table[(int) r_type]->name,
1403              sym_name);
1404
1405           bfd_set_error (bfd_error_invalid_operation);
1406           ret = FALSE;
1407           continue;
1408         }
1409
1410 #ifdef DEBUG
1411       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
1412                howto->name,
1413                (int)r_type,
1414                sym_name,
1415                r_symndx,
1416                (long)offset,
1417                (long)addend);
1418 #endif
1419
1420       r = _bfd_final_link_relocate (howto,
1421                                     input_bfd,
1422                                     input_section,
1423                                     contents,
1424                                     offset,
1425                                     relocation,
1426                                     addend);
1427
1428       if (r != bfd_reloc_ok)
1429         {
1430           ret = FALSE;
1431           switch (r)
1432             {
1433             default:
1434               break;
1435
1436             case bfd_reloc_overflow:
1437               {
1438                 const char *name;
1439
1440                 if (h != NULL)
1441                   name = NULL;
1442                 else
1443                   {
1444                     name = bfd_elf_string_from_elf_section (input_bfd,
1445                                                             symtab_hdr->sh_link,
1446                                                             sym->st_name);
1447                     if (name == NULL)
1448                       break;
1449
1450                     if (*name == '\0')
1451                       name = bfd_section_name (input_bfd, sec);
1452                   }
1453
1454                 (*info->callbacks->reloc_overflow) (info,
1455                                                     (h ? &h->root : NULL),
1456                                                     name,
1457                                                     howto->name,
1458                                                     (bfd_vma) 0,
1459                                                     input_bfd,
1460                                                     input_section,
1461                                                     offset);
1462               }
1463               break;
1464
1465             }
1466         }
1467     }
1468
1469 #ifdef DEBUG
1470   fprintf (stderr, "\n");
1471 #endif
1472
1473   return ret;
1474 }
1475
1476 static void
1477 i370_elf_post_process_headers (abfd, link_info)
1478     bfd * abfd;
1479     struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1480 {
1481   Elf_Internal_Ehdr * i_ehdrp;  /* Elf file header, internal form */
1482
1483   i_ehdrp = elf_elfheader (abfd);
1484   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1485 }
1486 \f
1487 #define TARGET_BIG_SYM          bfd_elf32_i370_vec
1488 #define TARGET_BIG_NAME         "elf32-i370"
1489 #define ELF_ARCH                bfd_arch_i370
1490 #define ELF_MACHINE_CODE        EM_S370
1491 #ifdef EM_I370_OLD
1492 #define ELF_MACHINE_ALT1        EM_I370_OLD
1493 #endif
1494 #define ELF_MAXPAGESIZE         0x1000
1495 #define elf_info_to_howto       i370_elf_info_to_howto
1496
1497 #define elf_backend_plt_not_loaded 1
1498 #define elf_backend_got_symbol_offset 4
1499 #define elf_backend_rela_normal 1
1500
1501 #define bfd_elf32_bfd_reloc_type_lookup         i370_elf_reloc_type_lookup
1502 #define bfd_elf32_bfd_set_private_flags         i370_elf_set_private_flags
1503 #define bfd_elf32_bfd_merge_private_bfd_data    i370_elf_merge_private_bfd_data
1504 #define elf_backend_relocate_section            i370_elf_relocate_section
1505
1506 /* dynamic loader support is mostly broken; just enough here to be able to
1507  * link glibc's ld.so without errors.
1508  */
1509 #define elf_backend_create_dynamic_sections     i370_elf_create_dynamic_sections
1510 #define elf_backend_size_dynamic_sections       i370_elf_size_dynamic_sections
1511 #define elf_backend_finish_dynamic_sections     i370_elf_finish_dynamic_sections
1512 #define elf_backend_fake_sections               i370_elf_fake_sections
1513 #define elf_backend_section_from_shdr           i370_elf_section_from_shdr
1514 #define elf_backend_adjust_dynamic_symbol       i370_elf_adjust_dynamic_symbol
1515 #define elf_backend_check_relocs                i370_elf_check_relocs
1516
1517 /*
1518 #define elf_backend_add_symbol_hook             i370_elf_add_symbol_hook
1519 #define elf_backend_finish_dynamic_symbol       i370_elf_finish_dynamic_symbol
1520 #define elf_backend_additional_program_headers  i370_elf_additional_program_headers
1521 #define elf_backend_modify_segment_map          i370_elf_modify_segment_map
1522 */
1523
1524 #define elf_backend_post_process_headers        i370_elf_post_process_headers
1525
1526 static int i370_noop
1527   PARAMS ((void));
1528
1529 static int i370_noop ()
1530 {
1531   return 1;
1532 }
1533
1534 /* we need to define these at least as no-ops to link glibc ld.so */
1535
1536 #define elf_backend_add_symbol_hook \
1537   (bfd_boolean (*) \
1538      PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *, \
1539               const char **, flagword *, asection **, bfd_vma *))) i370_noop
1540 #define elf_backend_finish_dynamic_symbol \
1541   (bfd_boolean (*) \
1542      PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, \
1543               Elf_Internal_Sym *))) i370_noop
1544 #define elf_backend_additional_program_headers \
1545   (int (*) PARAMS ((bfd *))) i370_noop
1546 #define elf_backend_modify_segment_map \
1547   (bfd_boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) i370_noop
1548
1549 #include "elf32-target.h"