s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[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
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 #if 0
294 static elf_linker_section_t *i370_elf_create_linker_section
295   PARAMS ((bfd *abfd, struct bfd_link_info *info,
296            enum elf_linker_section_enum));
297 #endif
298 static bfd_boolean i370_elf_check_relocs
299   PARAMS ((bfd *, struct bfd_link_info *, asection *,
300            const Elf_Internal_Rela *));
301 static bfd_boolean i370_elf_adjust_dynamic_symbol
302   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
303 static bfd_boolean i370_elf_adjust_dynindx
304   PARAMS ((struct elf_link_hash_entry *, PTR));
305 static bfd_boolean i370_elf_size_dynamic_sections
306   PARAMS ((bfd *, struct bfd_link_info *));
307 static bfd_boolean i370_elf_finish_dynamic_sections
308   PARAMS ((bfd *, struct bfd_link_info *));
309
310 /* The name of the dynamic interpreter.  This is put in the .interp
311     section.  */
312
313 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
314
315 /* Set the howto pointer for an i370 ELF reloc.  */
316
317 static void
318 i370_elf_info_to_howto (abfd, cache_ptr, dst)
319      bfd *abfd ATTRIBUTE_UNUSED;
320      arelent *cache_ptr;
321      Elf_Internal_Rela *dst;
322 {
323   if (!i370_elf_howto_table[ R_I370_ADDR31 ])   /* Initialize howto table */
324     i370_elf_howto_init ();
325
326   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_I370_max);
327   cache_ptr->howto = i370_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
328 }
329
330 /* hack alert --  the following several routines look generic to me ...
331  * why are we bothering with them ???
332  */
333 /* Function to set whether a module needs the -mrelocatable bit set.  */
334 static bfd_boolean
335 i370_elf_set_private_flags (abfd, flags)
336      bfd *abfd;
337      flagword flags;
338 {
339   BFD_ASSERT (!elf_flags_init (abfd)
340               || elf_elfheader (abfd)->e_flags == flags);
341
342   elf_elfheader (abfd)->e_flags = flags;
343   elf_flags_init (abfd) = TRUE;
344   return TRUE;
345 }
346
347 /* Merge backend specific data from an object file to the output
348    object file when linking */
349 static bfd_boolean
350 i370_elf_merge_private_bfd_data (ibfd, obfd)
351      bfd *ibfd;
352      bfd *obfd;
353 {
354   flagword old_flags;
355   flagword new_flags;
356
357   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
358       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
359     return TRUE;
360
361   new_flags = elf_elfheader (ibfd)->e_flags;
362   old_flags = elf_elfheader (obfd)->e_flags;
363   if (!elf_flags_init (obfd))   /* First call, no flags set */
364     {
365       elf_flags_init (obfd) = TRUE;
366       elf_elfheader (obfd)->e_flags = new_flags;
367     }
368
369   else if (new_flags == old_flags)      /* Compatible flags are ok */
370     ;
371
372   else                                  /* Incompatible flags */
373     {
374       (*_bfd_error_handler)
375         ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
376          bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
377
378       bfd_set_error (bfd_error_bad_value);
379       return FALSE;
380     }
381
382   return TRUE;
383 }
384 \f
385 /* Handle an i370 specific section when reading an object file.  This
386    is called when elfcode.h finds a section with an unknown type.  */
387 /* XXX hack alert bogus This routine is mostly all junk and almost
388  * certainly does the wrong thing.  Its here simply because it does
389  * just enough to allow glibc-2.1 ld.so to compile & link.
390  */
391
392 static bfd_boolean
393 i370_elf_section_from_shdr (abfd, hdr, name)
394      bfd *abfd;
395      Elf_Internal_Shdr *hdr;
396      const char *name;
397 {
398   asection *newsect;
399   flagword flags;
400
401   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
402     return FALSE;
403
404   newsect = hdr->bfd_section;
405   flags = bfd_get_section_flags (abfd, newsect);
406   if (hdr->sh_flags & SHF_EXCLUDE)
407     flags |= SEC_EXCLUDE;
408
409   if (hdr->sh_type == SHT_ORDERED)
410     flags |= SEC_SORT_ENTRIES;
411
412   bfd_set_section_flags (abfd, newsect, flags);
413   return TRUE;
414 }
415 \f
416 /* Set up any other section flags and such that may be necessary.  */
417 /* XXX hack alert bogus This routine is mostly all junk and almost
418  * certainly does the wrong thing.  Its here simply because it does
419  * just enough to allow glibc-2.1 ld.so to compile & link.
420  */
421
422 static bfd_boolean
423 i370_elf_fake_sections (abfd, shdr, asect)
424      bfd *abfd ATTRIBUTE_UNUSED;
425      Elf_Internal_Shdr *shdr;
426      asection *asect;
427 {
428   if ((asect->flags & SEC_EXCLUDE) != 0)
429     shdr->sh_flags |= SHF_EXCLUDE;
430
431   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
432     shdr->sh_type = SHT_ORDERED;
433
434   return TRUE;
435 }
436 \f
437 #if 0
438 /* Create a special linker section */
439 /* XXX hack alert bogus This routine is mostly all junk and almost
440  * certainly does the wrong thing.  Its here simply because it does
441  * just enough to allow glibc-2.1 ld.so to compile & link.
442  */
443
444 static elf_linker_section_t *
445 i370_elf_create_linker_section (abfd, info, which)
446      bfd *abfd;
447      struct bfd_link_info *info;
448      enum elf_linker_section_enum which;
449 {
450   bfd *dynobj = elf_hash_table (info)->dynobj;
451   elf_linker_section_t *lsect;
452
453   /* Record the first bfd section that needs the special section */
454   if (!dynobj)
455     dynobj = elf_hash_table (info)->dynobj = abfd;
456
457   /* If this is the first time, create the section */
458   lsect = elf_linker_section (dynobj, which);
459   if (!lsect)
460     {
461       elf_linker_section_t defaults;
462       static elf_linker_section_t zero_section;
463
464       defaults = zero_section;
465       defaults.which = which;
466       defaults.hole_written_p = FALSE;
467       defaults.alignment = 2;
468
469       /* Both of these sections are (technically) created by the user
470          putting data in them, so they shouldn't be marked
471          SEC_LINKER_CREATED.
472
473          The linker creates them so it has somewhere to attach their
474          respective symbols. In fact, if they were empty it would
475          be OK to leave the symbol set to 0 (or any random number), because
476          the appropriate register should never be used.  */
477       defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
478                         | SEC_IN_MEMORY);
479
480       switch (which)
481         {
482         default:
483           (*_bfd_error_handler) ("%s: Unknown special linker type %d",
484                                  bfd_archive_filename (abfd),
485                                  (int) which);
486
487           bfd_set_error (bfd_error_bad_value);
488           return (elf_linker_section_t *)0;
489
490         case LINKER_SECTION_SDATA:      /* .sdata/.sbss section */
491           defaults.name           = ".sdata";
492           defaults.rel_name       = ".rela.sdata";
493           defaults.bss_name       = ".sbss";
494           defaults.sym_name       = "_SDA_BASE_";
495           defaults.sym_offset     = 32768;
496           break;
497
498         case LINKER_SECTION_SDATA2:     /* .sdata2/.sbss2 section */
499           defaults.name           = ".sdata2";
500           defaults.rel_name       = ".rela.sdata2";
501           defaults.bss_name       = ".sbss2";
502           defaults.sym_name       = "_SDA2_BASE_";
503           defaults.sym_offset     = 32768;
504           defaults.flags         |= SEC_READONLY;
505           break;
506         }
507
508       lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
509     }
510
511   return lsect;
512 }
513 #endif
514 \f
515 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
516    to output sections (just like _bfd_elf_create_dynamic_sections has
517    to create .dynbss and .rela.bss).  */
518 /* XXX hack alert bogus This routine is mostly all junk and almost
519  * certainly does the wrong thing.  Its here simply because it does
520  * just enough to allow glibc-2.1 ld.so to compile & link.
521  */
522
523 static bfd_boolean
524 i370_elf_create_dynamic_sections (abfd, info)
525      bfd *abfd;
526      struct bfd_link_info *info;
527 {
528   register asection *s;
529   flagword flags;
530
531   if (!_bfd_elf_create_dynamic_sections(abfd, info))
532     return FALSE;
533
534   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
535            | SEC_LINKER_CREATED);
536
537   s = bfd_make_section (abfd, ".dynsbss");
538   if (s == NULL
539       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
540     return FALSE;
541
542   if (! info->shared)
543     {
544       s = bfd_make_section (abfd, ".rela.sbss");
545       if (s == NULL
546           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
547           || ! bfd_set_section_alignment (abfd, s, 2))
548         return FALSE;
549     }
550
551    /* xxx beats me, seem to need a rela.text ...  */
552    s = bfd_make_section (abfd, ".rela.text");
553    if (s == NULL
554       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
555       || ! bfd_set_section_alignment (abfd, s, 2))
556     return FALSE;
557   return TRUE;
558 }
559
560 /* Adjust a symbol defined by a dynamic object and referenced by a
561    regular object.  The current definition is in some section of the
562    dynamic object, but we're not including those sections.  We have to
563    change the definition to something the rest of the link can
564    understand.  */
565 /* XXX hack alert bogus This routine is mostly all junk and almost
566  * certainly does the wrong thing.  Its here simply because it does
567  * just enough to allow glibc-2.1 ld.so to compile & link.
568  */
569
570 static bfd_boolean
571 i370_elf_adjust_dynamic_symbol (info, h)
572      struct bfd_link_info *info;
573      struct elf_link_hash_entry *h;
574 {
575   bfd *dynobj = elf_hash_table (info)->dynobj;
576   asection *s;
577   unsigned int power_of_two;
578
579 #ifdef DEBUG
580   fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
581            h->root.root.string);
582 #endif
583
584   /* Make sure we know what is going on here.  */
585   BFD_ASSERT (dynobj != NULL
586               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
587                   || h->weakdef != NULL
588                   || ((h->elf_link_hash_flags
589                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
590                       && (h->elf_link_hash_flags
591                           & ELF_LINK_HASH_REF_REGULAR) != 0
592                       && (h->elf_link_hash_flags
593                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
594
595   s = bfd_get_section_by_name (dynobj, ".rela.text");
596   BFD_ASSERT (s != NULL);
597   s->_raw_size += sizeof (Elf32_External_Rela);
598
599   /* If this is a weak symbol, and there is a real definition, the
600      processor independent code will have arranged for us to see the
601      real definition first, and we can just use the same value.  */
602   if (h->weakdef != NULL)
603     {
604       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
605                   || h->weakdef->root.type == bfd_link_hash_defweak);
606       h->root.u.def.section = h->weakdef->root.u.def.section;
607       h->root.u.def.value = h->weakdef->root.u.def.value;
608       return TRUE;
609     }
610
611   /* This is a reference to a symbol defined by a dynamic object which
612      is not a function.  */
613
614   /* If we are creating a shared library, we must presume that the
615      only references to the symbol are via the global offset table.
616      For such cases we need not do anything here; the relocations will
617      be handled correctly by relocate_section.  */
618   if (info->shared)
619     return TRUE;
620
621   /* We must allocate the symbol in our .dynbss section, which will
622      become part of the .bss section of the executable.  There will be
623      an entry for this symbol in the .dynsym section.  The dynamic
624      object will contain position independent code, so all references
625      from the dynamic object to this symbol will go through the global
626      offset table.  The dynamic linker will use the .dynsym entry to
627      determine the address it must put in the global offset table, so
628      both the dynamic object and the regular object will refer to the
629      same memory location for the variable.
630
631      Of course, if the symbol is sufficiently small, we must instead
632      allocate it in .sbss.  FIXME: It would be better to do this if and
633      only if there were actually SDAREL relocs for that symbol.  */
634
635   if (h->size <= elf_gp_size (dynobj))
636     s = bfd_get_section_by_name (dynobj, ".dynsbss");
637   else
638     s = bfd_get_section_by_name (dynobj, ".dynbss");
639   BFD_ASSERT (s != NULL);
640
641   /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
642      copy the initial value out of the dynamic object and into the
643      runtime process image.  We need to remember the offset into the
644      .rela.bss section we are going to use.  */
645   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
646     {
647       asection *srel;
648
649       if (h->size <= elf_gp_size (dynobj))
650         srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
651       else
652         srel = bfd_get_section_by_name (dynobj, ".rela.bss");
653       BFD_ASSERT (srel != NULL);
654       srel->_raw_size += sizeof (Elf32_External_Rela);
655       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
656     }
657
658   /* We need to figure out the alignment required for this symbol.  I
659      have no idea how ELF linkers handle this.  */
660   power_of_two = bfd_log2 (h->size);
661   if (power_of_two > 4)
662     power_of_two = 4;
663
664   /* Apply the required alignment.  */
665   s->_raw_size = BFD_ALIGN (s->_raw_size,
666                             (bfd_size_type) (1 << power_of_two));
667   if (power_of_two > bfd_get_section_alignment (dynobj, s))
668     {
669       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
670         return FALSE;
671     }
672
673   /* Define the symbol as being at this point in the section.  */
674   h->root.u.def.section = s;
675   h->root.u.def.value = s->_raw_size;
676
677   /* Increment the section size to make room for the symbol.  */
678   s->_raw_size += h->size;
679
680   return TRUE;
681 }
682 \f
683 /* Increment the index of a dynamic symbol by a given amount.  Called
684    via elf_link_hash_traverse.  */
685 /* XXX hack alert bogus This routine is mostly all junk and almost
686  * certainly does the wrong thing.  Its here simply because it does
687  * just enough to allow glibc-2.1 ld.so to compile & link.
688  */
689
690 static bfd_boolean
691 i370_elf_adjust_dynindx (h, cparg)
692      struct elf_link_hash_entry *h;
693      PTR cparg;
694 {
695   int *cp = (int *) cparg;
696
697 #ifdef DEBUG
698   fprintf (stderr,
699            "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",
700            h->dynindx, *cp);
701 #endif
702
703   if (h->root.type == bfd_link_hash_warning)
704     h = (struct elf_link_hash_entry *) h->root.u.i.link;
705
706   if (h->dynindx != -1)
707     h->dynindx += *cp;
708
709   return TRUE;
710 }
711 \f
712 /* Set the sizes of the dynamic sections.  */
713 /* XXX hack alert bogus This routine is mostly all junk and almost
714  * certainly does the wrong thing.  Its here simply because it does
715  * just enough to allow glibc-2.1 ld.so to compile & link.
716  */
717
718 static bfd_boolean
719 i370_elf_size_dynamic_sections (output_bfd, info)
720      bfd *output_bfd;
721      struct bfd_link_info *info;
722 {
723   bfd *dynobj;
724   asection *s;
725   bfd_boolean plt;
726   bfd_boolean relocs;
727   bfd_boolean reltext;
728
729 #ifdef DEBUG
730   fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
731 #endif
732
733   dynobj = elf_hash_table (info)->dynobj;
734   BFD_ASSERT (dynobj != NULL);
735
736   if (elf_hash_table (info)->dynamic_sections_created)
737     {
738       /* Set the contents of the .interp section to the interpreter.  */
739       if (! info->shared)
740         {
741           s = bfd_get_section_by_name (dynobj, ".interp");
742           BFD_ASSERT (s != NULL);
743           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
744           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
745         }
746     }
747   else
748     {
749       /* We may have created entries in the .rela.got, .rela.sdata, and
750          .rela.sdata2 sections.  However, if we are not creating the
751          dynamic sections, we will not actually use these entries.  Reset
752          the size of .rela.got, et al, which will cause it to get
753          stripped from the output file below.  */
754       static char *rela_sections[] = { ".rela.got", ".rela.sdata",
755                                        ".rela.sdata2", ".rela.sbss",
756                                        (char *)0 };
757       char **p;
758
759       for (p = rela_sections; *p != (char *)0; p++)
760         {
761           s = bfd_get_section_by_name (dynobj, *p);
762           if (s != NULL)
763             s->_raw_size = 0;
764         }
765     }
766
767   /* The check_relocs and adjust_dynamic_symbol entry points have
768      determined the sizes of the various dynamic sections.  Allocate
769      memory for them.  */
770   plt = FALSE;
771   relocs = FALSE;
772   reltext = FALSE;
773   for (s = dynobj->sections; s != NULL; s = s->next)
774     {
775       const char *name;
776       bfd_boolean strip;
777
778       if ((s->flags & SEC_LINKER_CREATED) == 0)
779         continue;
780
781       /* It's OK to base decisions on the section name, because none
782          of the dynobj section names depend upon the input files.  */
783       name = bfd_get_section_name (dynobj, s);
784       strip = FALSE;
785
786       if (strcmp (name, ".plt") == 0)
787         {
788           if (s->_raw_size == 0)
789             {
790               /* Strip this section if we don't need it; see the
791                  comment below.  */
792               strip = TRUE;
793             }
794           else
795             {
796               /* Remember whether there is a PLT.  */
797               plt = TRUE;
798             }
799         }
800       else if (strncmp (name, ".rela", 5) == 0)
801         {
802           if (s->_raw_size == 0)
803             {
804               /* If we don't need this section, strip it from the
805                  output file.  This is mostly to handle .rela.bss and
806                  .rela.plt.  We must create both sections in
807                  create_dynamic_sections, because they must be created
808                  before the linker maps input sections to output
809                  sections.  The linker does that before
810                  adjust_dynamic_symbol is called, and it is that
811                  function which decides whether anything needs to go
812                  into these sections.  */
813               strip = TRUE;
814             }
815           else
816             {
817               asection *target;
818               const char *outname;
819
820               /* Remember whether there are any relocation sections.  */
821               relocs = TRUE;
822
823               /* If this relocation section applies to a read only
824                  section, then we probably need a DT_TEXTREL entry.  */
825               outname = bfd_get_section_name (output_bfd,
826                                               s->output_section);
827               target = bfd_get_section_by_name (output_bfd, outname + 5);
828               if (target != NULL
829                   && (target->flags & SEC_READONLY) != 0
830                   && (target->flags & SEC_ALLOC) != 0)
831                 reltext = TRUE;
832
833               /* We use the reloc_count field as a counter if we need
834                  to copy relocs into the output file.  */
835               s->reloc_count = 0;
836             }
837         }
838       else if (strcmp (name, ".got") != 0
839                && strcmp (name, ".sdata") != 0
840                && strcmp (name, ".sdata2") != 0)
841         {
842           /* It's not one of our sections, so don't allocate space.  */
843           continue;
844         }
845
846       if (strip)
847         {
848           asection **spp;
849
850           for (spp = &s->output_section->owner->sections;
851                *spp != NULL;
852                spp = &(*spp)->next)
853             {
854               if (*spp == s->output_section)
855                 {
856                   bfd_section_list_remove (s->output_section->owner, spp);
857                   --s->output_section->owner->section_count;
858                   break;
859                 }
860             }
861           continue;
862         }
863       /* Allocate memory for the section contents.  */
864       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
865       if (s->contents == NULL && s->_raw_size != 0)
866         return FALSE;
867     }
868
869   if (elf_hash_table (info)->dynamic_sections_created)
870     {
871       /* Add some entries to the .dynamic section.  We fill in the
872          values later, in i370_elf_finish_dynamic_sections, but we
873          must add the entries now so that we get the correct size for
874          the .dynamic section.  The DT_DEBUG entry is filled in by the
875          dynamic linker and used by the debugger.  */
876 #define add_dynamic_entry(TAG, VAL) \
877   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
878
879       if (!info->shared)
880         {
881           if (!add_dynamic_entry (DT_DEBUG, 0))
882             return FALSE;
883         }
884
885       if (plt)
886         {
887           if (!add_dynamic_entry (DT_PLTGOT, 0)
888               || !add_dynamic_entry (DT_PLTRELSZ, 0)
889               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
890               || !add_dynamic_entry (DT_JMPREL, 0))
891             return FALSE;
892         }
893
894       if (relocs)
895         {
896           if (!add_dynamic_entry (DT_RELA, 0)
897               || !add_dynamic_entry (DT_RELASZ, 0)
898               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
899             return FALSE;
900         }
901
902       if (reltext)
903         {
904           if (!add_dynamic_entry (DT_TEXTREL, 0))
905             return FALSE;
906           info->flags |= DF_TEXTREL;
907         }
908     }
909 #undef add_dynamic_entry
910
911   /* If we are generating a shared library, we generate a section
912      symbol for each output section.  These are local symbols, which
913      means that they must come first in the dynamic symbol table.
914      That means we must increment the dynamic symbol index of every
915      other dynamic symbol.
916
917      FIXME: We assume that there will never be relocations to
918      locations in linker-created sections that do not have
919      externally-visible names. Instead, we should work out precisely
920      which sections relocations are targetted at.  */
921   if (info->shared)
922     {
923       int c;
924
925       for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
926         {
927           if ((s->flags & SEC_LINKER_CREATED) != 0
928               || (s->flags & SEC_ALLOC) == 0)
929             {
930               elf_section_data (s)->dynindx = -1;
931               continue;
932             }
933
934           /* These symbols will have no names, so we don't need to
935              fiddle with dynstr_index.  */
936
937           elf_section_data (s)->dynindx = c + 1;
938
939           c++;
940         }
941
942       elf_link_hash_traverse (elf_hash_table (info),
943                               i370_elf_adjust_dynindx,
944                               (PTR) &c);
945       elf_hash_table (info)->dynsymcount += c;
946     }
947
948   return TRUE;
949 }
950 \f
951 /* Look through the relocs for a section during the first phase, and
952    allocate space in the global offset table or procedure linkage
953    table.  */
954 /* XXX hack alert bogus This routine is mostly all junk and almost
955  * certainly does the wrong thing.  Its here simply because it does
956  * just enough to allow glibc-2.1 ld.so to compile & link.
957  */
958
959 static bfd_boolean
960 i370_elf_check_relocs (abfd, info, sec, relocs)
961      bfd *abfd;
962      struct bfd_link_info *info;
963      asection *sec;
964      const Elf_Internal_Rela *relocs;
965 {
966   bfd *dynobj;
967   Elf_Internal_Shdr *symtab_hdr;
968   struct elf_link_hash_entry **sym_hashes;
969   const Elf_Internal_Rela *rel;
970   const Elf_Internal_Rela *rel_end;
971   bfd_vma *local_got_offsets;
972   asection *sreloc;
973
974   if (info->relocateable)
975     return TRUE;
976
977 #ifdef DEBUG
978   fprintf (stderr, "i370_elf_check_relocs called for section %s in %s\n",
979            bfd_get_section_name (abfd, sec),
980            bfd_archive_filename (abfd));
981 #endif
982
983   dynobj = elf_hash_table (info)->dynobj;
984   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
985   sym_hashes = elf_sym_hashes (abfd);
986   local_got_offsets = elf_local_got_offsets (abfd);
987
988   sreloc = NULL;
989
990   rel_end = relocs + sec->reloc_count;
991   for (rel = relocs; rel < rel_end; rel++)
992     {
993       unsigned long r_symndx;
994       struct elf_link_hash_entry *h;
995
996       r_symndx = ELF32_R_SYM (rel->r_info);
997       if (r_symndx < symtab_hdr->sh_info)
998         h = NULL;
999       else
1000         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1001
1002       if (info->shared)
1003         {
1004 #ifdef DEBUG
1005           fprintf (stderr,
1006                    "i370_elf_check_relocs needs to create relocation for %s\n",
1007                    (h && h->root.root.string)
1008                    ? h->root.root.string : "<unknown>");
1009 #endif
1010           if (sreloc == NULL)
1011             {
1012               const char *name;
1013
1014               name = (bfd_elf_string_from_elf_section
1015                       (abfd,
1016                        elf_elfheader (abfd)->e_shstrndx,
1017                        elf_section_data (sec)->rel_hdr.sh_name));
1018               if (name == NULL)
1019                 return FALSE;
1020
1021               BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1022                           && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0);
1023
1024               sreloc = bfd_get_section_by_name (dynobj, name);
1025               if (sreloc == NULL)
1026                 {
1027                   flagword flags;
1028
1029                   sreloc = bfd_make_section (dynobj, name);
1030                   flags = (SEC_HAS_CONTENTS | SEC_READONLY
1031                            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1032                   if ((sec->flags & SEC_ALLOC) != 0)
1033                     flags |= SEC_ALLOC | SEC_LOAD;
1034                   if (sreloc == NULL
1035                       || ! bfd_set_section_flags (dynobj, sreloc, flags)
1036                       || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1037                     return FALSE;
1038                 }
1039             }
1040
1041           sreloc->_raw_size += sizeof (Elf32_External_Rela);
1042
1043           /* FIXME: We should here do what the m68k and i386
1044              backends do: if the reloc is pc-relative, record it
1045              in case it turns out that the reloc is unnecessary
1046              because the symbol is forced local by versioning or
1047              we are linking with -Bdynamic.  Fortunately this
1048              case is not frequent.  */
1049         }
1050     }
1051
1052   return TRUE;
1053 }
1054 \f
1055 /* Finish up the dynamic sections.  */
1056 /* XXX hack alert bogus This routine is mostly all junk and almost
1057  * certainly does the wrong thing.  Its here simply because it does
1058  * just enough to allow glibc-2.1 ld.so to compile & link.
1059  */
1060
1061 static bfd_boolean
1062 i370_elf_finish_dynamic_sections (output_bfd, info)
1063      bfd *output_bfd;
1064      struct bfd_link_info *info;
1065 {
1066   asection *sdyn;
1067   bfd *dynobj = elf_hash_table (info)->dynobj;
1068   asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1069
1070 #ifdef DEBUG
1071   fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
1072 #endif
1073
1074   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1075
1076   if (elf_hash_table (info)->dynamic_sections_created)
1077     {
1078       asection *splt;
1079       Elf32_External_Dyn *dyncon, *dynconend;
1080
1081       splt = bfd_get_section_by_name (dynobj, ".plt");
1082       BFD_ASSERT (splt != NULL && sdyn != NULL);
1083
1084       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1085       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1086       for (; dyncon < dynconend; dyncon++)
1087         {
1088           Elf_Internal_Dyn dyn;
1089           const char *name;
1090           bfd_boolean size;
1091
1092           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1093
1094           switch (dyn.d_tag)
1095             {
1096             case DT_PLTGOT:   name = ".plt";      size = FALSE; break;
1097             case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE;  break;
1098             case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
1099             default:          name = NULL;        size = FALSE; break;
1100             }
1101
1102           if (name != NULL)
1103             {
1104               asection *s;
1105
1106               s = bfd_get_section_by_name (output_bfd, name);
1107               if (s == NULL)
1108                 dyn.d_un.d_val = 0;
1109               else
1110                 {
1111                   if (! size)
1112                     dyn.d_un.d_ptr = s->vma;
1113                   else
1114                     {
1115                       if (s->_cooked_size != 0)
1116                         dyn.d_un.d_val = s->_cooked_size;
1117                       else
1118                         dyn.d_un.d_val = s->_raw_size;
1119                     }
1120                 }
1121               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1122             }
1123         }
1124     }
1125
1126   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
1127      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
1128 /* XXX this is clearly very wrong for the 370 arch */
1129   if (sgot)
1130     {
1131       unsigned char *contents = sgot->contents;
1132       bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
1133
1134       if (sdyn == NULL)
1135         bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
1136       else
1137         bfd_put_32 (output_bfd,
1138                     sdyn->output_section->vma + sdyn->output_offset,
1139                     contents+4);
1140
1141       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1142     }
1143
1144   if (info->shared)
1145     {
1146       asection *sdynsym;
1147       asection *s;
1148       Elf_Internal_Sym sym;
1149       int maxdindx = 0;
1150
1151       /* Set up the section symbols for the output sections.  */
1152
1153       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1154       BFD_ASSERT (sdynsym != NULL);
1155
1156       sym.st_size = 0;
1157       sym.st_name = 0;
1158       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1159       sym.st_other = 0;
1160
1161       for (s = output_bfd->sections; s != NULL; s = s->next)
1162         {
1163           int indx, dindx;
1164           Elf32_External_Sym *esym;
1165
1166           sym.st_value = s->vma;
1167
1168           indx = elf_section_data (s)->this_idx;
1169           dindx = elf_section_data (s)->dynindx;
1170           if (dindx != -1)
1171             {
1172               BFD_ASSERT(indx > 0);
1173               BFD_ASSERT(dindx > 0);
1174
1175               if (dindx > maxdindx)
1176                 maxdindx = dindx;
1177
1178               sym.st_shndx = indx;
1179
1180               esym = (Elf32_External_Sym *) sdynsym->contents + dindx;
1181               bfd_elf32_swap_symbol_out (output_bfd, &sym, (PTR) esym, (PTR) 0);
1182             }
1183         }
1184
1185       /* Set the sh_info field of the output .dynsym section to the
1186          index of the first global symbol.  */
1187       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1188         maxdindx + 1;
1189     }
1190
1191   return TRUE;
1192 }
1193 \f
1194 /* The RELOCATE_SECTION function is called by the ELF backend linker
1195    to handle the relocations for a section.
1196
1197    The relocs are always passed as Rela structures; if the section
1198    actually uses Rel structures, the r_addend field will always be
1199    zero.
1200
1201    This function is responsible for adjust the section contents as
1202    necessary, and (if using Rela relocs and generating a
1203    relocateable output file) adjusting the reloc addend as
1204    necessary.
1205
1206    This function does not have to worry about setting the reloc
1207    address or the reloc symbol index.
1208
1209    LOCAL_SYMS is a pointer to the swapped in local symbols.
1210
1211    LOCAL_SECTIONS is an array giving the section in the input file
1212    corresponding to the st_shndx field of each local symbol.
1213
1214    The global hash table entry for the global symbols can be found
1215    via elf_sym_hashes (input_bfd).
1216
1217    When generating relocateable output, this function must handle
1218    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1219    going to be the section symbol corresponding to the output
1220    section, which means that the addend must be adjusted
1221    accordingly.  */
1222
1223 static bfd_boolean
1224 i370_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1225                            contents, relocs, local_syms, local_sections)
1226      bfd *output_bfd;
1227      struct bfd_link_info *info;
1228      bfd *input_bfd;
1229      asection *input_section;
1230      bfd_byte *contents;
1231      Elf_Internal_Rela *relocs;
1232      Elf_Internal_Sym *local_syms;
1233      asection **local_sections;
1234 {
1235   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1236   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1237   bfd *dynobj = elf_hash_table (info)->dynobj;
1238   Elf_Internal_Rela *rel = relocs;
1239   Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
1240   asection *sreloc = NULL;
1241   bfd_vma *local_got_offsets;
1242   bfd_boolean ret = TRUE;
1243
1244   if (info->relocateable)
1245     return TRUE;
1246
1247 #ifdef DEBUG
1248   fprintf (stderr, "i370_elf_relocate_section called for %s section %s, %ld relocations%s\n",
1249            bfd_archive_filename (input_bfd),
1250            bfd_section_name(input_bfd, input_section),
1251            (long) input_section->reloc_count,
1252            (info->relocateable) ? " (relocatable)" : "");
1253 #endif
1254
1255   if (!i370_elf_howto_table[ R_I370_ADDR31 ])   /* Initialize howto table if needed */
1256     i370_elf_howto_init ();
1257
1258   local_got_offsets = elf_local_got_offsets (input_bfd);
1259
1260   for (; rel < relend; rel++)
1261     {
1262       enum i370_reloc_type r_type       = (enum i370_reloc_type)ELF32_R_TYPE (rel->r_info);
1263       bfd_vma offset                    = rel->r_offset;
1264       bfd_vma addend                    = rel->r_addend;
1265       bfd_reloc_status_type r           = bfd_reloc_other;
1266       Elf_Internal_Sym *sym             = (Elf_Internal_Sym *)0;
1267       asection *sec                     = (asection *)0;
1268       struct elf_link_hash_entry *h     = (struct elf_link_hash_entry *)0;
1269       const char *sym_name              = (const char *)0;
1270       reloc_howto_type *howto;
1271       unsigned long r_symndx;
1272       bfd_vma relocation;
1273
1274       /* Unknown relocation handling */
1275       if ((unsigned)r_type >= (unsigned)R_I370_max
1276           || !i370_elf_howto_table[(int)r_type])
1277         {
1278           (*_bfd_error_handler) ("%s: unknown relocation type %d",
1279                                  bfd_archive_filename (input_bfd),
1280                                  (int) r_type);
1281
1282           bfd_set_error (bfd_error_bad_value);
1283           ret = FALSE;
1284           continue;
1285         }
1286
1287       howto = i370_elf_howto_table[(int)r_type];
1288       r_symndx = ELF32_R_SYM (rel->r_info);
1289
1290       if (r_symndx < symtab_hdr->sh_info)
1291         {
1292           sym = local_syms + r_symndx;
1293           sec = local_sections[r_symndx];
1294           sym_name = "<local symbol>";
1295
1296           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1297           addend = rel->r_addend;
1298         }
1299       else
1300         {
1301           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1302           while (h->root.type == bfd_link_hash_indirect
1303                  || h->root.type == bfd_link_hash_warning)
1304             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1305           sym_name = h->root.root.string;
1306           if (h->root.type == bfd_link_hash_defined
1307               || h->root.type == bfd_link_hash_defweak)
1308             {
1309               sec = h->root.u.def.section;
1310               if (info->shared
1311                   && ((! info->symbolic && h->dynindx != -1)
1312                       || (h->elf_link_hash_flags
1313                           & ELF_LINK_HASH_DEF_REGULAR) == 0)
1314                   && (input_section->flags & SEC_ALLOC) != 0
1315                   && (r_type == R_I370_ADDR31
1316                       || r_type == R_I370_COPY
1317                       || r_type == R_I370_ADDR16
1318                       || r_type == R_I370_RELATIVE))
1319                 {
1320                   /* In these cases, we don't need the relocation
1321                      value.  We check specially because in some
1322                      obscure cases sec->output_section will be NULL.  */
1323                   relocation = 0;
1324                 }
1325               else
1326                 relocation = (h->root.u.def.value
1327                               + sec->output_section->vma
1328                               + sec->output_offset);
1329             }
1330           else if (h->root.type == bfd_link_hash_undefweak)
1331             relocation = 0;
1332           else if (info->shared
1333                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1334             relocation = 0;
1335           else
1336             {
1337               (*info->callbacks->undefined_symbol) (info,
1338                                                     h->root.root.string,
1339                                                     input_bfd,
1340                                                     input_section,
1341                                                     rel->r_offset,
1342                                                     TRUE);
1343               ret = FALSE;
1344               continue;
1345             }
1346         }
1347
1348       switch ((int) r_type)
1349         {
1350         default:
1351           (*_bfd_error_handler)
1352             ("%s: unknown relocation type %d for symbol %s",
1353              bfd_archive_filename (input_bfd),
1354              (int) r_type, sym_name);
1355
1356           bfd_set_error (bfd_error_bad_value);
1357           ret = FALSE;
1358           continue;
1359
1360         case (int)R_I370_NONE:
1361           continue;
1362
1363         /* Relocations that may need to be propagated if this is a shared
1364            object.  */
1365         case (int)R_I370_REL31:
1366           /* If these relocations are not to a named symbol, they can be
1367              handled right here, no need to bother the dynamic linker.  */
1368           if (h == NULL
1369               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1370             break;
1371         /* fall through */
1372
1373         /* Relocations that always need to be propagated if this is a shared
1374            object.  */
1375         case (int)R_I370_ADDR31:
1376         case (int)R_I370_ADDR16:
1377           if (info->shared
1378               && r_symndx != 0)
1379             {
1380               Elf_Internal_Rela outrel;
1381               bfd_byte *loc;
1382               int skip;
1383
1384 #ifdef DEBUG
1385               fprintf (stderr,
1386                        "i370_elf_relocate_section needs to create relocation for %s\n",
1387                        (h && h->root.root.string) ? h->root.root.string : "<unknown>");
1388 #endif
1389
1390               /* When generating a shared object, these relocations
1391                  are copied into the output file to be resolved at run
1392                  time.  */
1393
1394               if (sreloc == NULL)
1395                 {
1396                   const char *name;
1397
1398                   name = (bfd_elf_string_from_elf_section
1399                           (input_bfd,
1400                            elf_elfheader (input_bfd)->e_shstrndx,
1401                            elf_section_data (input_section)->rel_hdr.sh_name));
1402                   if (name == NULL)
1403                     return FALSE;
1404
1405                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1406                               && strcmp (bfd_get_section_name (input_bfd,
1407                                                                input_section),
1408                                          name + 5) == 0);
1409
1410                   sreloc = bfd_get_section_by_name (dynobj, name);
1411                   BFD_ASSERT (sreloc != NULL);
1412                 }
1413
1414               skip = 0;
1415
1416               outrel.r_offset =
1417                 _bfd_elf_section_offset (output_bfd, info, input_section,
1418                                          rel->r_offset);
1419               if (outrel.r_offset == (bfd_vma) -1
1420                   || outrel.r_offset == (bfd_vma) -2)
1421                 skip = (int) outrel.r_offset;
1422               outrel.r_offset += (input_section->output_section->vma
1423                                   + input_section->output_offset);
1424
1425               if (skip)
1426                 memset (&outrel, 0, sizeof outrel);
1427               /* h->dynindx may be -1 if this symbol was marked to
1428                  become local.  */
1429               else if (h != NULL
1430                        && ((! info->symbolic && h->dynindx != -1)
1431                            || (h->elf_link_hash_flags
1432                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
1433                 {
1434                   BFD_ASSERT (h->dynindx != -1);
1435                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1436                   outrel.r_addend = rel->r_addend;
1437                 }
1438               else
1439                 {
1440                   if (r_type == R_I370_ADDR31)
1441                     {
1442                       outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
1443                       outrel.r_addend = relocation + rel->r_addend;
1444                     }
1445                   else
1446                     {
1447                       long indx;
1448
1449                       if (h == NULL)
1450                         sec = local_sections[r_symndx];
1451                       else
1452                         {
1453                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1454                                       || (h->root.type
1455                                           == bfd_link_hash_defweak));
1456                           sec = h->root.u.def.section;
1457                         }
1458                       if (sec != NULL && bfd_is_abs_section (sec))
1459                         indx = 0;
1460                       else if (sec == NULL || sec->owner == NULL)
1461                         {
1462                           bfd_set_error (bfd_error_bad_value);
1463                           return FALSE;
1464                         }
1465                       else
1466                         {
1467                           asection *osec;
1468
1469                           osec = sec->output_section;
1470                           indx = elf_section_data (osec)->dynindx;
1471                           BFD_ASSERT(indx > 0);
1472 #ifdef DEBUG
1473                           if (indx <= 0)
1474                             {
1475                               printf ("indx=%d section=%s flags=%08x name=%s\n",
1476                                       indx, osec->name, osec->flags,
1477                                       h->root.root.string);
1478                             }
1479 #endif
1480                         }
1481
1482                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1483                       outrel.r_addend = relocation + rel->r_addend;
1484                     }
1485                 }
1486
1487               loc = sreloc->contents;
1488               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1489               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1490
1491               /* This reloc will be computed at runtime, so there's no
1492                  need to do anything now, unless this is a RELATIVE
1493                  reloc in an unallocated section.  */
1494               if (skip == -1
1495                   || (input_section->flags & SEC_ALLOC) != 0
1496                   || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
1497                 continue;
1498             }
1499           break;
1500
1501         case (int)R_I370_COPY:
1502         case (int)R_I370_RELATIVE:
1503           (*_bfd_error_handler)
1504             ("%s: Relocation %s is not yet supported for symbol %s.",
1505              bfd_archive_filename (input_bfd),
1506              i370_elf_howto_table[(int) r_type]->name,
1507              sym_name);
1508
1509           bfd_set_error (bfd_error_invalid_operation);
1510           ret = FALSE;
1511           continue;
1512         }
1513
1514 #ifdef DEBUG
1515       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
1516                howto->name,
1517                (int)r_type,
1518                sym_name,
1519                r_symndx,
1520                (long)offset,
1521                (long)addend);
1522 #endif
1523
1524       r = _bfd_final_link_relocate (howto,
1525                                     input_bfd,
1526                                     input_section,
1527                                     contents,
1528                                     offset,
1529                                     relocation,
1530                                     addend);
1531
1532       if (r != bfd_reloc_ok)
1533         {
1534           ret = FALSE;
1535           switch (r)
1536             {
1537             default:
1538               break;
1539
1540             case bfd_reloc_overflow:
1541               {
1542                 const char *name;
1543
1544                 if (h != NULL)
1545                   name = h->root.root.string;
1546                 else
1547                   {
1548                     name = bfd_elf_string_from_elf_section (input_bfd,
1549                                                             symtab_hdr->sh_link,
1550                                                             sym->st_name);
1551                     if (name == NULL)
1552                       break;
1553
1554                     if (*name == '\0')
1555                       name = bfd_section_name (input_bfd, sec);
1556                   }
1557
1558                 (*info->callbacks->reloc_overflow) (info,
1559                                                     name,
1560                                                     howto->name,
1561                                                     (bfd_vma) 0,
1562                                                     input_bfd,
1563                                                     input_section,
1564                                                     offset);
1565               }
1566               break;
1567
1568             }
1569         }
1570     }
1571
1572 #ifdef DEBUG
1573   fprintf (stderr, "\n");
1574 #endif
1575
1576   return ret;
1577 }
1578
1579 static void
1580 i370_elf_post_process_headers (abfd, link_info)
1581     bfd * abfd;
1582     struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1583 {
1584   Elf_Internal_Ehdr * i_ehdrp;  /* Elf file header, internal form */
1585
1586   i_ehdrp = elf_elfheader (abfd);
1587   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1588 }
1589 \f
1590 #define TARGET_BIG_SYM          bfd_elf32_i370_vec
1591 #define TARGET_BIG_NAME         "elf32-i370"
1592 #define ELF_ARCH                bfd_arch_i370
1593 #define ELF_MACHINE_CODE        EM_S370
1594 #ifdef EM_I370_OLD
1595 #define ELF_MACHINE_ALT1        EM_I370_OLD
1596 #endif
1597 #define ELF_MAXPAGESIZE         0x1000
1598 #define elf_info_to_howto       i370_elf_info_to_howto
1599
1600 #define elf_backend_plt_not_loaded 1
1601 #define elf_backend_got_symbol_offset 4
1602 #define elf_backend_rela_normal 1
1603
1604 #define bfd_elf32_bfd_reloc_type_lookup         i370_elf_reloc_type_lookup
1605 #define bfd_elf32_bfd_set_private_flags         i370_elf_set_private_flags
1606 #define bfd_elf32_bfd_merge_private_bfd_data    i370_elf_merge_private_bfd_data
1607 #define elf_backend_relocate_section            i370_elf_relocate_section
1608
1609 /* dynamic loader support is mostly broken; just enough here to be able to
1610  * link glibc's ld.so without errors.
1611  */
1612 #define elf_backend_create_dynamic_sections     i370_elf_create_dynamic_sections
1613 #define elf_backend_size_dynamic_sections       i370_elf_size_dynamic_sections
1614 #define elf_backend_finish_dynamic_sections     i370_elf_finish_dynamic_sections
1615 #define elf_backend_fake_sections               i370_elf_fake_sections
1616 #define elf_backend_section_from_shdr           i370_elf_section_from_shdr
1617 #define elf_backend_adjust_dynamic_symbol       i370_elf_adjust_dynamic_symbol
1618 #define elf_backend_check_relocs                i370_elf_check_relocs
1619
1620 /*
1621 #define elf_backend_add_symbol_hook             i370_elf_add_symbol_hook
1622 #define elf_backend_finish_dynamic_symbol       i370_elf_finish_dynamic_symbol
1623 #define elf_backend_additional_program_headers  i370_elf_additional_program_headers
1624 #define elf_backend_modify_segment_map          i370_elf_modify_segment_map
1625 */
1626
1627 #define elf_backend_post_process_headers        i370_elf_post_process_headers
1628
1629 static int i370_noop
1630   PARAMS ((void));
1631
1632 static int i370_noop ()
1633 {
1634   return 1;
1635 }
1636
1637 /* we need to define these at least as no-ops to link glibc ld.so */
1638
1639 #define elf_backend_add_symbol_hook \
1640   (bfd_boolean (*) \
1641      PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, \
1642               const char **, flagword *, asection **, bfd_vma *))) i370_noop
1643 #define elf_backend_finish_dynamic_symbol \
1644   (bfd_boolean (*) \
1645      PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, \
1646               Elf_Internal_Sym *))) i370_noop
1647 #define elf_backend_additional_program_headers \
1648   (int (*) PARAMS ((bfd *))) i370_noop
1649 #define elf_backend_modify_segment_map \
1650   (bfd_boolean (*) PARAMS ((bfd *))) i370_noop
1651
1652 #include "elf32-target.h"