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