Back to index

cell-binutils  2.17cvs20070401
elf32-i370.c
Go to the documentation of this file.
00001 /* i370-specific support for 32-bit ELF
00002    Copyright 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004,
00003    2005, 2006, 2007 Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor, Cygnus Support.
00005    Hacked by Linas Vepstas for i370 linas@linas.org
00006 
00007    This file is part of BFD, the Binary File Descriptor library.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
00022    MA 02110-1301, USA.  */
00023 
00024 /* This file is based on a preliminary PowerPC ELF ABI.
00025    But its been hacked on for the IBM 360/370 architectures.
00026    Basically, the 31bit relocation works, and just about everything
00027    else is a wild card.  In particular, don't expect shared libs or
00028    dynamic loading to work ...  its never been tested.  */
00029 
00030 #include "bfd.h"
00031 #include "sysdep.h"
00032 #include "bfdlink.h"
00033 #include "libbfd.h"
00034 #include "elf-bfd.h"
00035 #include "elf/i370.h"
00036 
00037 static reloc_howto_type *i370_elf_howto_table[ (int)R_I370_max ];
00038 
00039 static reloc_howto_type i370_elf_howto_raw[] =
00040 {
00041   /* This reloc does nothing.  */
00042   HOWTO (R_I370_NONE,              /* type */
00043         0,                  /* rightshift */
00044         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00045         32,                 /* bitsize */
00046         FALSE,                     /* pc_relative */
00047         0,                  /* bitpos */
00048         complain_overflow_bitfield, /* complain_on_overflow */
00049         bfd_elf_generic_reloc,     /* special_function */
00050         "R_I370_NONE",             /* name */
00051         FALSE,                     /* partial_inplace */
00052         0,                  /* src_mask */
00053         0,                  /* dst_mask */
00054         FALSE),             /* pcrel_offset */
00055 
00056   /* A standard 31 bit relocation.  */
00057   HOWTO (R_I370_ADDR31,            /* type */
00058         0,                  /* rightshift */
00059         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00060         31,                 /* bitsize */
00061         FALSE,                     /* pc_relative */
00062         0,                  /* bitpos */
00063         complain_overflow_bitfield, /* complain_on_overflow */
00064         bfd_elf_generic_reloc,     /* special_function */
00065         "R_I370_ADDR31",    /* name */
00066         FALSE,                     /* partial_inplace */
00067         0,                  /* src_mask */
00068         0x7fffffff,         /* dst_mask */
00069         FALSE),             /* pcrel_offset */
00070 
00071   /* A standard 32 bit relocation.  */
00072   HOWTO (R_I370_ADDR32,            /* type */
00073         0,                  /* rightshift */
00074         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00075         32,                 /* bitsize */
00076         FALSE,                     /* pc_relative */
00077         0,                  /* bitpos */
00078         complain_overflow_bitfield, /* complain_on_overflow */
00079         bfd_elf_generic_reloc,     /* special_function */
00080         "R_I370_ADDR32",    /* name */
00081         FALSE,                     /* partial_inplace */
00082         0,                  /* src_mask */
00083         0xffffffff,         /* dst_mask */
00084         FALSE),             /* pcrel_offset */
00085 
00086   /* A standard 16 bit relocation.  */
00087   HOWTO (R_I370_ADDR16,            /* type */
00088         0,                  /* rightshift */
00089         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00090         16,                 /* bitsize */
00091         FALSE,                     /* pc_relative */
00092         0,                  /* bitpos */
00093         complain_overflow_bitfield, /* complain_on_overflow */
00094         bfd_elf_generic_reloc,     /* special_function */
00095         "R_I370_ADDR16",    /* name */
00096         FALSE,                     /* partial_inplace */
00097         0,                  /* src_mask */
00098         0xffff,             /* dst_mask */
00099         FALSE),             /* pcrel_offset */
00100 
00101   /* 31-bit PC relative.  */
00102   HOWTO (R_I370_REL31,             /* type */
00103         0,                  /* rightshift */
00104         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00105         31,                 /* bitsize */
00106         TRUE,               /* pc_relative */
00107         0,                  /* bitpos */
00108         complain_overflow_bitfield, /* complain_on_overflow */
00109         bfd_elf_generic_reloc,     /* special_function */
00110         "R_I370_REL31",     /* name */
00111         FALSE,                     /* partial_inplace */
00112         0,                  /* src_mask */
00113         0x7fffffff,         /* dst_mask */
00114         TRUE),                     /* pcrel_offset */
00115 
00116   /* 32-bit PC relative.  */
00117   HOWTO (R_I370_REL32,             /* type */
00118         0,                  /* rightshift */
00119         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00120         32,                 /* bitsize */
00121         TRUE,               /* pc_relative */
00122         0,                  /* bitpos */
00123         complain_overflow_bitfield, /* complain_on_overflow */
00124         bfd_elf_generic_reloc,     /* special_function */
00125         "R_I370_REL32",     /* name */
00126         FALSE,                     /* partial_inplace */
00127         0,                  /* src_mask */
00128         0xffffffff,         /* dst_mask */
00129         TRUE),                     /* pcrel_offset */
00130 
00131   /* A standard 12 bit relocation.  */
00132   HOWTO (R_I370_ADDR12,            /* type */
00133         0,                  /* rightshift */
00134         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00135         12,                 /* bitsize */
00136         FALSE,                     /* pc_relative */
00137         0,                  /* bitpos */
00138         complain_overflow_bitfield, /* complain_on_overflow */
00139         bfd_elf_generic_reloc,     /* special_function */
00140         "R_I370_ADDR12",    /* name */
00141         FALSE,                     /* partial_inplace */
00142         0,                  /* src_mask */
00143         0xfff,                     /* dst_mask */
00144         FALSE),             /* pcrel_offset */
00145 
00146   /* 12-bit PC relative.  */
00147   HOWTO (R_I370_REL12,             /* type */
00148         0,                  /* rightshift */
00149         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00150         12,                 /* bitsize */
00151         TRUE,               /* pc_relative */
00152         0,                  /* bitpos */
00153         complain_overflow_bitfield, /* complain_on_overflow */
00154         bfd_elf_generic_reloc,     /* special_function */
00155         "R_I370_REL12",     /* name */
00156         FALSE,                     /* partial_inplace */
00157         0,                  /* src_mask */
00158         0xfff,                     /* dst_mask */
00159         TRUE),                     /* pcrel_offset */
00160 
00161   /* A standard 8 bit relocation.  */
00162   HOWTO (R_I370_ADDR8,             /* type */
00163         0,                  /* rightshift */
00164         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00165         8,                  /* bitsize */
00166         FALSE,                     /* pc_relative */
00167         0,                  /* bitpos */
00168         complain_overflow_bitfield, /* complain_on_overflow */
00169         bfd_elf_generic_reloc,     /* special_function */
00170         "R_I370_ADDR8",     /* name */
00171         FALSE,                     /* partial_inplace */
00172         0,                  /* src_mask */
00173         0xff,               /* dst_mask */
00174         FALSE),             /* pcrel_offset */
00175 
00176   /* 8-bit PC relative.  */
00177   HOWTO (R_I370_REL8,              /* type */
00178         0,                  /* rightshift */
00179         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00180         8,                  /* bitsize */
00181         TRUE,               /* pc_relative */
00182         0,                  /* bitpos */
00183         complain_overflow_bitfield, /* complain_on_overflow */
00184         bfd_elf_generic_reloc,     /* special_function */
00185         "R_I370_REL8",             /* name */
00186         FALSE,                     /* partial_inplace */
00187         0,                  /* src_mask */
00188         0xff,               /* dst_mask */
00189         TRUE),                     /* pcrel_offset */
00190 
00191   /* This is used only by the dynamic linker.  The symbol should exist
00192      both in the object being run and in some shared library.  The
00193      dynamic linker copies the data addressed by the symbol from the
00194      shared library into the object, because the object being
00195      run has to have the data at some particular address.  */
00196   HOWTO (R_I370_COPY,              /* type */
00197         0,                  /* rightshift */
00198         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00199         32,                 /* bitsize */
00200         FALSE,                     /* pc_relative */
00201         0,                  /* bitpos */
00202         complain_overflow_bitfield, /* complain_on_overflow */
00203         bfd_elf_generic_reloc,      /* special_function */
00204         "R_I370_COPY",             /* name */
00205         FALSE,                     /* partial_inplace */
00206         0,                  /* src_mask */
00207         0,                  /* dst_mask */
00208         FALSE),             /* pcrel_offset */
00209 
00210   /* Used only by the dynamic linker.  When the object is run, this
00211      longword is set to the load address of the object, plus the
00212      addend.  */
00213   HOWTO (R_I370_RELATIVE,   /* type */
00214         0,                  /* rightshift */
00215         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00216         32,                 /* bitsize */
00217         FALSE,                     /* pc_relative */
00218         0,                  /* bitpos */
00219         complain_overflow_bitfield, /* complain_on_overflow */
00220         bfd_elf_generic_reloc,      /* special_function */
00221         "R_I370_RELATIVE",  /* name */
00222         FALSE,                     /* partial_inplace */
00223         0,                  /* src_mask */
00224         0xffffffff,         /* dst_mask */
00225         FALSE),             /* pcrel_offset */
00226 
00227 };
00228 
00229 /* Initialize the i370_elf_howto_table, so that linear accesses can be done.  */
00230 
00231 static void
00232 i370_elf_howto_init (void)
00233 {
00234   unsigned int i, type;
00235 
00236   for (i = 0; i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]); i++)
00237     {
00238       type = i370_elf_howto_raw[i].type;
00239       BFD_ASSERT (type < sizeof (i370_elf_howto_table) / sizeof (i370_elf_howto_table[0]));
00240       i370_elf_howto_table[type] = &i370_elf_howto_raw[i];
00241     }
00242 }
00243 
00244 static reloc_howto_type *
00245 i370_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00246                          bfd_reloc_code_real_type code)
00247 {
00248   enum i370_reloc_type i370_reloc = R_I370_NONE;
00249 
00250   if (!i370_elf_howto_table[ R_I370_ADDR31 ])
00251     /* Initialize howto table if needed.  */
00252     i370_elf_howto_init ();
00253 
00254   switch ((int) code)
00255     {
00256     default:
00257       return NULL;
00258 
00259     case BFD_RELOC_NONE:    i370_reloc = R_I370_NONE;   break;
00260     case BFD_RELOC_32:             i370_reloc = R_I370_ADDR31; break;
00261     case BFD_RELOC_16:             i370_reloc = R_I370_ADDR16; break;
00262     case BFD_RELOC_32_PCREL:       i370_reloc = R_I370_REL31;  break;
00263     case BFD_RELOC_CTOR:    i370_reloc = R_I370_ADDR31; break;
00264     case BFD_RELOC_I370_D12:       i370_reloc = R_I370_ADDR12; break;
00265     }
00266 
00267   return i370_elf_howto_table[ (int)i370_reloc ];
00268 };
00269 
00270 static reloc_howto_type *
00271 i370_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00272                          const char *r_name)
00273 {
00274   unsigned int i;
00275 
00276   for (i = 0;
00277        i < sizeof (i370_elf_howto_raw) / sizeof (i370_elf_howto_raw[0]);
00278        i++)
00279     if (i370_elf_howto_raw[i].name != NULL
00280        && strcasecmp (i370_elf_howto_raw[i].name, r_name) == 0)
00281       return &i370_elf_howto_raw[i];
00282 
00283   return NULL;
00284 }
00285 
00286 /* The name of the dynamic interpreter.  This is put in the .interp
00287     section.  */
00288 
00289 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
00290 
00291 /* Set the howto pointer for an i370 ELF reloc.  */
00292 
00293 static void
00294 i370_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
00295                      arelent *cache_ptr,
00296                      Elf_Internal_Rela *dst)
00297 {
00298   if (!i370_elf_howto_table[ R_I370_ADDR31 ])
00299     /* Initialize howto table.  */
00300     i370_elf_howto_init ();
00301 
00302   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_I370_max);
00303   cache_ptr->howto = i370_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
00304 }
00305 
00306 /* Hack alert --  the following several routines look generic to me ...
00307    why are we bothering with them ?  */
00308 /* Function to set whether a module needs the -mrelocatable bit set.  */
00309 
00310 static bfd_boolean
00311 i370_elf_set_private_flags (bfd *abfd, flagword flags)
00312 {
00313   BFD_ASSERT (!elf_flags_init (abfd)
00314              || elf_elfheader (abfd)->e_flags == flags);
00315 
00316   elf_elfheader (abfd)->e_flags = flags;
00317   elf_flags_init (abfd) = TRUE;
00318   return TRUE;
00319 }
00320 
00321 /* Merge backend specific data from an object file to the output
00322    object file when linking.  */
00323 
00324 static bfd_boolean
00325 i370_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
00326 {
00327   flagword old_flags;
00328   flagword new_flags;
00329 
00330   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
00331       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
00332     return TRUE;
00333 
00334   new_flags = elf_elfheader (ibfd)->e_flags;
00335   old_flags = elf_elfheader (obfd)->e_flags;
00336   if (!elf_flags_init (obfd))      /* First call, no flags set.  */
00337     {
00338       elf_flags_init (obfd) = TRUE;
00339       elf_elfheader (obfd)->e_flags = new_flags;
00340     }
00341 
00342   else if (new_flags == old_flags) /* Compatible flags are ok.  */
00343     ;
00344 
00345   else                             /* Incompatible flags.  */
00346     {
00347       (*_bfd_error_handler)
00348        ("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
00349         ibfd, (long) new_flags, (long) old_flags);
00350 
00351       bfd_set_error (bfd_error_bad_value);
00352       return FALSE;
00353     }
00354 
00355   return TRUE;
00356 }
00357 
00358 /* Handle an i370 specific section when reading an object file.  This
00359    is called when elfcode.h finds a section with an unknown type.  */
00360 /* XXX hack alert bogus This routine is mostly all junk and almost
00361    certainly does the wrong thing.  Its here simply because it does
00362    just enough to allow glibc-2.1 ld.so to compile & link.  */
00363 
00364 static bfd_boolean
00365 i370_elf_section_from_shdr (bfd *abfd,
00366                          Elf_Internal_Shdr *hdr,
00367                          const char *name,
00368                          int shindex)
00369 {
00370   asection *newsect;
00371   flagword flags;
00372 
00373   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
00374     return FALSE;
00375 
00376   newsect = hdr->bfd_section;
00377   flags = bfd_get_section_flags (abfd, newsect);
00378   if (hdr->sh_flags & SHF_EXCLUDE)
00379     flags |= SEC_EXCLUDE;
00380 
00381   if (hdr->sh_type == SHT_ORDERED)
00382     flags |= SEC_SORT_ENTRIES;
00383 
00384   bfd_set_section_flags (abfd, newsect, flags);
00385   return TRUE;
00386 }
00387 
00388 /* Set up any other section flags and such that may be necessary.  */
00389 /* XXX hack alert bogus This routine is mostly all junk and almost
00390    certainly does the wrong thing.  Its here simply because it does
00391    just enough to allow glibc-2.1 ld.so to compile & link.  */
00392 
00393 static bfd_boolean
00394 i370_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
00395                      Elf_Internal_Shdr *shdr,
00396                      asection *asect)
00397 {
00398   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
00399     shdr->sh_flags |= SHF_EXCLUDE;
00400 
00401   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
00402     shdr->sh_type = SHT_ORDERED;
00403 
00404   return TRUE;
00405 }
00406 
00407 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
00408    to output sections (just like _bfd_elf_create_dynamic_sections has
00409    to create .dynbss and .rela.bss).  */
00410 /* XXX hack alert bogus This routine is mostly all junk and almost
00411    certainly does the wrong thing.  Its here simply because it does
00412    just enough to allow glibc-2.1 ld.so to compile & link.  */
00413 
00414 static bfd_boolean
00415 i370_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
00416 {
00417   asection *s;
00418   flagword flags;
00419 
00420   if (!_bfd_elf_create_dynamic_sections(abfd, info))
00421     return FALSE;
00422 
00423   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
00424           | SEC_LINKER_CREATED);
00425 
00426   s = bfd_make_section_with_flags (abfd, ".dynsbss",
00427                                SEC_ALLOC | SEC_LINKER_CREATED);
00428   if (s == NULL)
00429     return FALSE;
00430 
00431   if (! info->shared)
00432     {
00433       s = bfd_make_section_with_flags (abfd, ".rela.sbss",
00434                                    flags | SEC_READONLY);
00435       if (s == NULL
00436          || ! bfd_set_section_alignment (abfd, s, 2))
00437        return FALSE;
00438     }
00439 
00440    /* XXX beats me, seem to need a rela.text ...  */
00441    s = bfd_make_section_with_flags (abfd, ".rela.text",
00442                                 flags | SEC_READONLY);
00443    if (s == NULL
00444       || ! bfd_set_section_alignment (abfd, s, 2))
00445     return FALSE;
00446   return TRUE;
00447 }
00448 
00449 /* Adjust a symbol defined by a dynamic object and referenced by a
00450    regular object.  The current definition is in some section of the
00451    dynamic object, but we're not including those sections.  We have to
00452    change the definition to something the rest of the link can
00453    understand.  */
00454 /* XXX hack alert bogus This routine is mostly all junk and almost
00455    certainly does the wrong thing.  Its here simply because it does
00456    just enough to allow glibc-2.1 ld.so to compile & link.  */
00457 
00458 static bfd_boolean
00459 i370_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
00460                             struct elf_link_hash_entry *h)
00461 {
00462   bfd *dynobj = elf_hash_table (info)->dynobj;
00463   asection *s;
00464   unsigned int power_of_two;
00465 
00466 #ifdef DEBUG
00467   fprintf (stderr, "i370_elf_adjust_dynamic_symbol called for %s\n",
00468           h->root.root.string);
00469 #endif
00470 
00471   /* Make sure we know what is going on here.  */
00472   BFD_ASSERT (dynobj != NULL
00473              && (h->needs_plt
00474                 || h->u.weakdef != NULL
00475                 || (h->def_dynamic
00476                     && h->ref_regular
00477                     && !h->def_regular)));
00478 
00479   s = bfd_get_section_by_name (dynobj, ".rela.text");
00480   BFD_ASSERT (s != NULL);
00481   s->size += sizeof (Elf32_External_Rela);
00482 
00483   /* If this is a weak symbol, and there is a real definition, the
00484      processor independent code will have arranged for us to see the
00485      real definition first, and we can just use the same value.  */
00486   if (h->u.weakdef != NULL)
00487     {
00488       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
00489                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
00490       h->root.u.def.section = h->u.weakdef->root.u.def.section;
00491       h->root.u.def.value = h->u.weakdef->root.u.def.value;
00492       return TRUE;
00493     }
00494 
00495   /* This is a reference to a symbol defined by a dynamic object which
00496      is not a function.  */
00497 
00498   /* If we are creating a shared library, we must presume that the
00499      only references to the symbol are via the global offset table.
00500      For such cases we need not do anything here; the relocations will
00501      be handled correctly by relocate_section.  */
00502   if (info->shared)
00503     return TRUE;
00504 
00505   if (h->size == 0)
00506     {
00507       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
00508                           h->root.root.string);
00509       return TRUE;
00510     }
00511 
00512   /* We must allocate the symbol in our .dynbss section, which will
00513      become part of the .bss section of the executable.  There will be
00514      an entry for this symbol in the .dynsym section.  The dynamic
00515      object will contain position independent code, so all references
00516      from the dynamic object to this symbol will go through the global
00517      offset table.  The dynamic linker will use the .dynsym entry to
00518      determine the address it must put in the global offset table, so
00519      both the dynamic object and the regular object will refer to the
00520      same memory location for the variable.
00521 
00522      Of course, if the symbol is sufficiently small, we must instead
00523      allocate it in .sbss.  FIXME: It would be better to do this if and
00524      only if there were actually SDAREL relocs for that symbol.  */
00525 
00526   if (h->size <= elf_gp_size (dynobj))
00527     s = bfd_get_section_by_name (dynobj, ".dynsbss");
00528   else
00529     s = bfd_get_section_by_name (dynobj, ".dynbss");
00530   BFD_ASSERT (s != NULL);
00531 
00532   /* We must generate a R_I370_COPY reloc to tell the dynamic linker to
00533      copy the initial value out of the dynamic object and into the
00534      runtime process image.  We need to remember the offset into the
00535      .rela.bss section we are going to use.  */
00536   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
00537     {
00538       asection *srel;
00539 
00540       if (h->size <= elf_gp_size (dynobj))
00541        srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
00542       else
00543        srel = bfd_get_section_by_name (dynobj, ".rela.bss");
00544       BFD_ASSERT (srel != NULL);
00545       srel->size += sizeof (Elf32_External_Rela);
00546       h->needs_copy = 1;
00547     }
00548 
00549   /* We need to figure out the alignment required for this symbol.  I
00550      have no idea how ELF linkers handle this.  */
00551   power_of_two = bfd_log2 (h->size);
00552   if (power_of_two > 4)
00553     power_of_two = 4;
00554 
00555   /* Apply the required alignment.  */
00556   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
00557   if (power_of_two > bfd_get_section_alignment (dynobj, s))
00558     {
00559       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
00560        return FALSE;
00561     }
00562 
00563   /* Define the symbol as being at this point in the section.  */
00564   h->root.u.def.section = s;
00565   h->root.u.def.value = s->size;
00566 
00567   /* Increment the section size to make room for the symbol.  */
00568   s->size += h->size;
00569 
00570   return TRUE;
00571 }
00572 
00573 /* Increment the index of a dynamic symbol by a given amount.  Called
00574    via elf_link_hash_traverse.  */
00575 /* XXX hack alert bogus This routine is mostly all junk and almost
00576    certainly does the wrong thing.  Its here simply because it does
00577    just enough to allow glibc-2.1 ld.so to compile & link.  */
00578 
00579 static bfd_boolean
00580 i370_elf_adjust_dynindx (struct elf_link_hash_entry *h, void * cparg)
00581 {
00582   int *cp = (int *) cparg;
00583 
00584 #ifdef DEBUG
00585   fprintf (stderr,
00586           "i370_elf_adjust_dynindx called, h->dynindx = %ld, *cp = %d\n",
00587           h->dynindx, *cp);
00588 #endif
00589 
00590   if (h->root.type == bfd_link_hash_warning)
00591     h = (struct elf_link_hash_entry *) h->root.u.i.link;
00592 
00593   if (h->dynindx != -1)
00594     h->dynindx += *cp;
00595 
00596   return TRUE;
00597 }
00598 
00599 /* Set the sizes of the dynamic sections.  */
00600 /* XXX hack alert bogus This routine is mostly all junk and almost
00601    certainly does the wrong thing.  Its here simply because it does
00602    just enough to allow glibc-2.1 ld.so to compile & link.  */
00603 
00604 static bfd_boolean
00605 i370_elf_size_dynamic_sections (bfd *output_bfd,
00606                             struct bfd_link_info *info)
00607 {
00608   bfd *dynobj;
00609   asection *s;
00610   bfd_boolean plt;
00611   bfd_boolean relocs;
00612   bfd_boolean reltext;
00613 
00614 #ifdef DEBUG
00615   fprintf (stderr, "i370_elf_size_dynamic_sections called\n");
00616 #endif
00617 
00618   dynobj = elf_hash_table (info)->dynobj;
00619   BFD_ASSERT (dynobj != NULL);
00620 
00621   if (elf_hash_table (info)->dynamic_sections_created)
00622     {
00623       /* Set the contents of the .interp section to the interpreter.  */
00624       if (info->executable)
00625        {
00626          s = bfd_get_section_by_name (dynobj, ".interp");
00627          BFD_ASSERT (s != NULL);
00628          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
00629          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
00630        }
00631     }
00632   else
00633     {
00634       /* We may have created entries in the .rela.got, .rela.sdata, and
00635         .rela.sdata2 sections.  However, if we are not creating the
00636         dynamic sections, we will not actually use these entries.  Reset
00637         the size of .rela.got, et al, which will cause it to get
00638         stripped from the output file below.  */
00639       static char *rela_sections[] = { ".rela.got", ".rela.sdata",
00640                                    ".rela.sdata2", ".rela.sbss",
00641                                    NULL };
00642       char **p;
00643 
00644       for (p = rela_sections; *p != NULL; p++)
00645        {
00646          s = bfd_get_section_by_name (dynobj, *p);
00647          if (s != NULL)
00648            s->size = 0;
00649        }
00650     }
00651 
00652   /* The check_relocs and adjust_dynamic_symbol entry points have
00653      determined the sizes of the various dynamic sections.  Allocate
00654      memory for them.  */
00655   plt = FALSE;
00656   relocs = FALSE;
00657   reltext = FALSE;
00658   for (s = dynobj->sections; s != NULL; s = s->next)
00659     {
00660       const char *name;
00661 
00662       if ((s->flags & SEC_LINKER_CREATED) == 0)
00663        continue;
00664 
00665       /* It's OK to base decisions on the section name, because none
00666         of the dynobj section names depend upon the input files.  */
00667       name = bfd_get_section_name (dynobj, s);
00668 
00669       if (strcmp (name, ".plt") == 0)
00670        {
00671          /* Remember whether there is a PLT.  */
00672          plt = s->size != 0;
00673        }
00674       else if (CONST_STRNEQ (name, ".rela"))
00675        {
00676          if (s->size != 0)
00677            {
00678              asection *target;
00679              const char *outname;
00680 
00681              /* Remember whether there are any relocation sections.  */
00682              relocs = TRUE;
00683 
00684              /* If this relocation section applies to a read only
00685                section, then we probably need a DT_TEXTREL entry.  */
00686              outname = bfd_get_section_name (output_bfd,
00687                                          s->output_section);
00688              target = bfd_get_section_by_name (output_bfd, outname + 5);
00689              if (target != NULL
00690                 && (target->flags & SEC_READONLY) != 0
00691                 && (target->flags & SEC_ALLOC) != 0)
00692               reltext = TRUE;
00693 
00694              /* We use the reloc_count field as a counter if we need
00695                to copy relocs into the output file.  */
00696              s->reloc_count = 0;
00697            }
00698        }
00699       else if (strcmp (name, ".got") != 0
00700               && strcmp (name, ".sdata") != 0
00701               && strcmp (name, ".sdata2") != 0
00702               && strcmp (name, ".dynbss") != 0
00703               && strcmp (name, ".dynsbss") != 0)
00704        {
00705          /* It's not one of our sections, so don't allocate space.  */
00706          continue;
00707        }
00708 
00709       if (s->size == 0)
00710        {
00711          /* If we don't need this section, strip it from the
00712             output file.  This is mostly to handle .rela.bss and
00713             .rela.plt.  We must create both sections in
00714             create_dynamic_sections, because they must be created
00715             before the linker maps input sections to output
00716             sections.  The linker does that before
00717             adjust_dynamic_symbol is called, and it is that
00718             function which decides whether anything needs to go
00719             into these sections.  */
00720          s->flags |= SEC_EXCLUDE;
00721          continue;
00722        }
00723 
00724       if ((s->flags & SEC_HAS_CONTENTS) == 0)
00725        continue;
00726 
00727       /* Allocate memory for the section contents.  */
00728       s->contents = bfd_zalloc (dynobj, s->size);
00729       if (s->contents == NULL)
00730        return FALSE;
00731     }
00732 
00733   if (elf_hash_table (info)->dynamic_sections_created)
00734     {
00735       /* Add some entries to the .dynamic section.  We fill in the
00736         values later, in i370_elf_finish_dynamic_sections, but we
00737         must add the entries now so that we get the correct size for
00738         the .dynamic section.  The DT_DEBUG entry is filled in by the
00739         dynamic linker and used by the debugger.  */
00740 #define add_dynamic_entry(TAG, VAL) \
00741   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
00742 
00743       if (!info->shared)
00744        {
00745          if (!add_dynamic_entry (DT_DEBUG, 0))
00746            return FALSE;
00747        }
00748 
00749       if (plt)
00750        {
00751          if (!add_dynamic_entry (DT_PLTGOT, 0)
00752              || !add_dynamic_entry (DT_PLTRELSZ, 0)
00753              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
00754              || !add_dynamic_entry (DT_JMPREL, 0))
00755            return FALSE;
00756        }
00757 
00758       if (relocs)
00759        {
00760          if (!add_dynamic_entry (DT_RELA, 0)
00761              || !add_dynamic_entry (DT_RELASZ, 0)
00762              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
00763            return FALSE;
00764        }
00765 
00766       if (reltext)
00767        {
00768          if (!add_dynamic_entry (DT_TEXTREL, 0))
00769            return FALSE;
00770          info->flags |= DF_TEXTREL;
00771        }
00772     }
00773 #undef add_dynamic_entry
00774 
00775   /* If we are generating a shared library, we generate a section
00776      symbol for each output section.  These are local symbols, which
00777      means that they must come first in the dynamic symbol table.
00778      That means we must increment the dynamic symbol index of every
00779      other dynamic symbol.
00780 
00781      FIXME: We assume that there will never be relocations to
00782      locations in linker-created sections that do not have
00783      externally-visible names. Instead, we should work out precisely
00784      which sections relocations are targeted at.  */
00785   if (info->shared)
00786     {
00787       int c;
00788 
00789       for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
00790        {
00791          if ((s->flags & SEC_LINKER_CREATED) != 0
00792              || (s->flags & SEC_ALLOC) == 0)
00793            {
00794              elf_section_data (s)->dynindx = -1;
00795              continue;
00796            }
00797 
00798          /* These symbols will have no names, so we don't need to
00799             fiddle with dynstr_index.  */
00800 
00801          elf_section_data (s)->dynindx = c + 1;
00802 
00803          c++;
00804        }
00805 
00806       elf_link_hash_traverse (elf_hash_table (info),
00807                            i370_elf_adjust_dynindx, & c);
00808       elf_hash_table (info)->dynsymcount += c;
00809     }
00810 
00811   return TRUE;
00812 }
00813 
00814 /* Look through the relocs for a section during the first phase, and
00815    allocate space in the global offset table or procedure linkage
00816    table.  */
00817 /* XXX hack alert bogus This routine is mostly all junk and almost
00818    certainly does the wrong thing.  Its here simply because it does
00819    just enough to allow glibc-2.1 ld.so to compile & link.  */
00820 
00821 static bfd_boolean
00822 i370_elf_check_relocs (bfd *abfd,
00823                      struct bfd_link_info *info,
00824                      asection *sec,
00825                      const Elf_Internal_Rela *relocs)
00826 {
00827   bfd *dynobj;
00828   Elf_Internal_Shdr *symtab_hdr;
00829   struct elf_link_hash_entry **sym_hashes;
00830   const Elf_Internal_Rela *rel;
00831   const Elf_Internal_Rela *rel_end;
00832   bfd_vma *local_got_offsets;
00833   asection *sreloc;
00834 
00835   if (info->relocatable)
00836     return TRUE;
00837 
00838 #ifdef DEBUG
00839   _bfd_error_handler ("i370_elf_check_relocs called for section %A in %B",
00840                     sec, abfd);
00841 #endif
00842 
00843   dynobj = elf_hash_table (info)->dynobj;
00844   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00845   sym_hashes = elf_sym_hashes (abfd);
00846   local_got_offsets = elf_local_got_offsets (abfd);
00847 
00848   sreloc = NULL;
00849 
00850   rel_end = relocs + sec->reloc_count;
00851   for (rel = relocs; rel < rel_end; rel++)
00852     {
00853       unsigned long r_symndx;
00854       struct elf_link_hash_entry *h;
00855 
00856       r_symndx = ELF32_R_SYM (rel->r_info);
00857       if (r_symndx < symtab_hdr->sh_info)
00858        h = NULL;
00859       else
00860        {
00861          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00862          while (h->root.type == bfd_link_hash_indirect
00863                || h->root.type == bfd_link_hash_warning)
00864            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00865        }
00866 
00867       if (info->shared)
00868        {
00869 #ifdef DEBUG
00870          fprintf (stderr,
00871                  "i370_elf_check_relocs needs to create relocation for %s\n",
00872                  (h && h->root.root.string)
00873                  ? h->root.root.string : "<unknown>");
00874 #endif
00875          if (sreloc == NULL)
00876            {
00877              const char *name;
00878 
00879              name = (bfd_elf_string_from_elf_section
00880                     (abfd,
00881                      elf_elfheader (abfd)->e_shstrndx,
00882                      elf_section_data (sec)->rel_hdr.sh_name));
00883              if (name == NULL)
00884               return FALSE;
00885 
00886              BFD_ASSERT (CONST_STRNEQ (name, ".rela")
00887                        && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0);
00888 
00889              sreloc = bfd_get_section_by_name (dynobj, name);
00890              if (sreloc == NULL)
00891               {
00892                 flagword flags;
00893 
00894                 flags = (SEC_HAS_CONTENTS | SEC_READONLY
00895                         | SEC_IN_MEMORY | SEC_LINKER_CREATED);
00896                 if ((sec->flags & SEC_ALLOC) != 0)
00897                   flags |= SEC_ALLOC | SEC_LOAD;
00898                 sreloc = bfd_make_section_with_flags (dynobj, name,
00899                                                  flags);
00900                 if (sreloc == NULL
00901                     || ! bfd_set_section_alignment (dynobj, sreloc, 2))
00902                   return FALSE;
00903               }
00904            }
00905 
00906          sreloc->size += sizeof (Elf32_External_Rela);
00907 
00908          /* FIXME: We should here do what the m68k and i386
00909             backends do: if the reloc is pc-relative, record it
00910             in case it turns out that the reloc is unnecessary
00911             because the symbol is forced local by versioning or
00912             we are linking with -Bdynamic.  Fortunately this
00913             case is not frequent.  */
00914        }
00915     }
00916 
00917   return TRUE;
00918 }
00919 
00920 /* Finish up the dynamic sections.  */
00921 /* XXX hack alert bogus This routine is mostly all junk and almost
00922    certainly does the wrong thing.  Its here simply because it does
00923    just enough to allow glibc-2.1 ld.so to compile & link.  */
00924 
00925 static bfd_boolean
00926 i370_elf_finish_dynamic_sections (bfd *output_bfd,
00927                               struct bfd_link_info *info)
00928 {
00929   asection *sdyn;
00930   bfd *dynobj = elf_hash_table (info)->dynobj;
00931   asection *sgot = bfd_get_section_by_name (dynobj, ".got");
00932 
00933 #ifdef DEBUG
00934   fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");
00935 #endif
00936 
00937   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
00938 
00939   if (elf_hash_table (info)->dynamic_sections_created)
00940     {
00941       asection *splt;
00942       Elf32_External_Dyn *dyncon, *dynconend;
00943 
00944       splt = bfd_get_section_by_name (dynobj, ".plt");
00945       BFD_ASSERT (splt != NULL && sdyn != NULL);
00946 
00947       dyncon = (Elf32_External_Dyn *) sdyn->contents;
00948       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
00949       for (; dyncon < dynconend; dyncon++)
00950        {
00951          Elf_Internal_Dyn dyn;
00952          const char *name;
00953          bfd_boolean size;
00954 
00955          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
00956 
00957          switch (dyn.d_tag)
00958            {
00959            case DT_PLTGOT:   name = ".plt";        size = FALSE; break;
00960            case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE;  break;
00961            case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
00962            default:        name = NULL;     size = FALSE; break;
00963            }
00964 
00965          if (name != NULL)
00966            {
00967              asection *s;
00968 
00969              s = bfd_get_section_by_name (output_bfd, name);
00970              if (s == NULL)
00971               dyn.d_un.d_val = 0;
00972              else
00973               {
00974                 if (! size)
00975                   dyn.d_un.d_ptr = s->vma;
00976                 else
00977                   dyn.d_un.d_val = s->size;
00978               }
00979              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
00980            }
00981        }
00982     }
00983 
00984   if (sgot && sgot->size != 0)
00985     {
00986       unsigned char *contents = sgot->contents;
00987 
00988       if (sdyn == NULL)
00989        bfd_put_32 (output_bfd, (bfd_vma) 0, contents);
00990       else
00991        bfd_put_32 (output_bfd,
00992                   sdyn->output_section->vma + sdyn->output_offset,
00993                   contents);
00994 
00995       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
00996     }
00997 
00998   if (info->shared)
00999     {
01000       asection *sdynsym;
01001       asection *s;
01002       Elf_Internal_Sym sym;
01003       int maxdindx = 0;
01004 
01005       /* Set up the section symbols for the output sections.  */
01006 
01007       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
01008       BFD_ASSERT (sdynsym != NULL);
01009 
01010       sym.st_size = 0;
01011       sym.st_name = 0;
01012       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
01013       sym.st_other = 0;
01014 
01015       for (s = output_bfd->sections; s != NULL; s = s->next)
01016        {
01017          int indx, dindx;
01018          Elf32_External_Sym *esym;
01019 
01020          sym.st_value = s->vma;
01021 
01022          indx = elf_section_data (s)->this_idx;
01023          dindx = elf_section_data (s)->dynindx;
01024          if (dindx != -1)
01025            {
01026              BFD_ASSERT(indx > 0);
01027              BFD_ASSERT(dindx > 0);
01028 
01029              if (dindx > maxdindx)
01030               maxdindx = dindx;
01031 
01032              sym.st_shndx = indx;
01033 
01034              esym = (Elf32_External_Sym *) sdynsym->contents + dindx;
01035              bfd_elf32_swap_symbol_out (output_bfd, &sym, esym, NULL);
01036            }
01037        }
01038 
01039       /* Set the sh_info field of the output .dynsym section to the
01040         index of the first global symbol.  */
01041       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
01042        maxdindx + 1;
01043     }
01044 
01045   return TRUE;
01046 }
01047 
01048 /* The RELOCATE_SECTION function is called by the ELF backend linker
01049    to handle the relocations for a section.
01050 
01051    The relocs are always passed as Rela structures; if the section
01052    actually uses Rel structures, the r_addend field will always be
01053    zero.
01054 
01055    This function is responsible for adjust the section contents as
01056    necessary, and (if using Rela relocs and generating a
01057    relocatable output file) adjusting the reloc addend as
01058    necessary.
01059 
01060    This function does not have to worry about setting the reloc
01061    address or the reloc symbol index.
01062 
01063    LOCAL_SYMS is a pointer to the swapped in local symbols.
01064 
01065    LOCAL_SECTIONS is an array giving the section in the input file
01066    corresponding to the st_shndx field of each local symbol.
01067 
01068    The global hash table entry for the global symbols can be found
01069    via elf_sym_hashes (input_bfd).
01070 
01071    When generating relocatable output, this function must handle
01072    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
01073    going to be the section symbol corresponding to the output
01074    section, which means that the addend must be adjusted
01075    accordingly.  */
01076 
01077 static bfd_boolean
01078 i370_elf_relocate_section (bfd *output_bfd,
01079                         struct bfd_link_info *info,
01080                         bfd *input_bfd,
01081                         asection *input_section,
01082                         bfd_byte *contents,
01083                         Elf_Internal_Rela *relocs,
01084                         Elf_Internal_Sym *local_syms,
01085                         asection **local_sections)
01086 {
01087   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
01088   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
01089   bfd *dynobj = elf_hash_table (info)->dynobj;
01090   Elf_Internal_Rela *rel = relocs;
01091   Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
01092   asection *sreloc = NULL;
01093   bfd_vma *local_got_offsets;
01094   bfd_boolean ret = TRUE;
01095 
01096 #ifdef DEBUG
01097   _bfd_error_handler ("i370_elf_relocate_section called for %B section %A, %ld relocations%s",
01098                     input_bfd, input_section,
01099                     (long) input_section->reloc_count,
01100                     (info->relocatable) ? " (relocatable)" : "");
01101 #endif
01102 
01103   if (!i370_elf_howto_table[ R_I370_ADDR31 ])
01104     /* Initialize howto table if needed.  */
01105     i370_elf_howto_init ();
01106 
01107   local_got_offsets = elf_local_got_offsets (input_bfd);
01108 
01109   for (; rel < relend; rel++)
01110     {
01111       enum i370_reloc_type r_type    = (enum i370_reloc_type) ELF32_R_TYPE (rel->r_info);
01112       bfd_vma offset             = rel->r_offset;
01113       bfd_vma addend             = rel->r_addend;
01114       bfd_reloc_status_type r           = bfd_reloc_other;
01115       Elf_Internal_Sym *sym      = NULL;
01116       asection *sec              = NULL;
01117       struct elf_link_hash_entry * h = NULL;
01118       const char *sym_name       = NULL;
01119       reloc_howto_type *howto;
01120       unsigned long r_symndx;
01121       bfd_vma relocation;
01122 
01123       /* Unknown relocation handling.  */
01124       if ((unsigned) r_type >= (unsigned) R_I370_max
01125          || !i370_elf_howto_table[(int)r_type])
01126        {
01127          (*_bfd_error_handler) ("%B: unknown relocation type %d",
01128                              input_bfd,
01129                              (int) r_type);
01130 
01131          bfd_set_error (bfd_error_bad_value);
01132          ret = FALSE;
01133          continue;
01134        }
01135 
01136       howto = i370_elf_howto_table[(int) r_type];
01137       r_symndx = ELF32_R_SYM (rel->r_info);
01138       relocation = 0;
01139 
01140       if (r_symndx < symtab_hdr->sh_info)
01141        {
01142          sym = local_syms + r_symndx;
01143          sec = local_sections[r_symndx];
01144          sym_name = "<local symbol>";
01145 
01146          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
01147          addend = rel->r_addend;
01148        }
01149       else
01150        {
01151          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01152          while (h->root.type == bfd_link_hash_indirect
01153                || h->root.type == bfd_link_hash_warning)
01154            h = (struct elf_link_hash_entry *) h->root.u.i.link;
01155          sym_name = h->root.root.string;
01156          if (h->root.type == bfd_link_hash_defined
01157              || h->root.type == bfd_link_hash_defweak)
01158            {
01159              sec = h->root.u.def.section;
01160              if (info->shared
01161                 && ((! info->symbolic && h->dynindx != -1)
01162                     || !h->def_regular)
01163                 && (input_section->flags & SEC_ALLOC) != 0
01164                 && (r_type == R_I370_ADDR31
01165                     || r_type == R_I370_COPY
01166                     || r_type == R_I370_ADDR16
01167                     || r_type == R_I370_RELATIVE))
01168               /* In these cases, we don't need the relocation
01169                  value.  We check specially because in some
01170                  obscure cases sec->output_section will be NULL.  */
01171               ;
01172              else
01173               relocation = (h->root.u.def.value
01174                            + sec->output_section->vma
01175                            + sec->output_offset);
01176            }
01177          else if (h->root.type == bfd_link_hash_undefweak)
01178            ;
01179          else if (info->unresolved_syms_in_objects == RM_IGNORE
01180                  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
01181            ;
01182          else if (!info->relocatable)
01183            {
01184              if ((*info->callbacks->undefined_symbol)
01185                 (info, h->root.root.string, input_bfd,
01186                  input_section, rel->r_offset,
01187                  (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
01188                   || ELF_ST_VISIBILITY (h->other))))
01189               {
01190                 ret = FALSE;
01191                 continue;
01192               }
01193            }
01194        }
01195 
01196       if (sec != NULL && elf_discarded_section (sec))
01197        {
01198          /* For relocs against symbols from removed linkonce sections,
01199             or sections discarded by a linker script, we just want the
01200             section contents zeroed.  Avoid any special processing.  */
01201          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
01202          rel->r_info = 0;
01203          rel->r_addend = 0;
01204          continue;
01205        }
01206 
01207       if (info->relocatable)
01208        continue;
01209 
01210       switch ((int) r_type)
01211        {
01212        default:
01213          (*_bfd_error_handler)
01214            ("%B: unknown relocation type %d for symbol %s",
01215             input_bfd, (int) r_type, sym_name);
01216 
01217          bfd_set_error (bfd_error_bad_value);
01218          ret = FALSE;
01219          continue;
01220 
01221        case (int) R_I370_NONE:
01222          continue;
01223 
01224        /* Relocations that may need to be propagated if this is a shared
01225           object.  */
01226        case (int) R_I370_REL31:
01227          /* If these relocations are not to a named symbol, they can be
01228             handled right here, no need to bother the dynamic linker.  */
01229          if (h == NULL
01230              || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
01231            break;
01232        /* Fall through.  */
01233 
01234        /* Relocations that always need to be propagated if this is a shared
01235           object.  */
01236        case (int) R_I370_ADDR31:
01237        case (int) R_I370_ADDR16:
01238          if (info->shared
01239              && r_symndx != 0)
01240            {
01241              Elf_Internal_Rela outrel;
01242              bfd_byte *loc;
01243              int skip;
01244 
01245 #ifdef DEBUG
01246              fprintf (stderr,
01247                      "i370_elf_relocate_section needs to create relocation for %s\n",
01248                      (h && h->root.root.string) ? h->root.root.string : "<unknown>");
01249 #endif
01250 
01251              /* When generating a shared object, these relocations
01252                are copied into the output file to be resolved at run
01253                time.  */
01254 
01255              if (sreloc == NULL)
01256               {
01257                 const char *name;
01258 
01259                 name = (bfd_elf_string_from_elf_section
01260                        (input_bfd,
01261                         elf_elfheader (input_bfd)->e_shstrndx,
01262                         elf_section_data (input_section)->rel_hdr.sh_name));
01263                 if (name == NULL)
01264                   return FALSE;
01265 
01266                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
01267                            && strcmp (bfd_get_section_name (input_bfd,
01268                                                         input_section),
01269                                     name + 5) == 0);
01270 
01271                 sreloc = bfd_get_section_by_name (dynobj, name);
01272                 BFD_ASSERT (sreloc != NULL);
01273               }
01274 
01275              skip = 0;
01276 
01277              outrel.r_offset =
01278               _bfd_elf_section_offset (output_bfd, info, input_section,
01279                                     rel->r_offset);
01280              if (outrel.r_offset == (bfd_vma) -1
01281                 || outrel.r_offset == (bfd_vma) -2)
01282               skip = (int) outrel.r_offset;
01283              outrel.r_offset += (input_section->output_section->vma
01284                               + input_section->output_offset);
01285 
01286              if (skip)
01287               memset (&outrel, 0, sizeof outrel);
01288              /* h->dynindx may be -1 if this symbol was marked to
01289                become local.  */
01290              else if (h != NULL
01291                      && ((! info->symbolic && h->dynindx != -1)
01292                         || !h->def_regular))
01293               {
01294                 BFD_ASSERT (h->dynindx != -1);
01295                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
01296                 outrel.r_addend = rel->r_addend;
01297               }
01298              else
01299               {
01300                 if (r_type == R_I370_ADDR31)
01301                   {
01302                     outrel.r_info = ELF32_R_INFO (0, R_I370_RELATIVE);
01303                     outrel.r_addend = relocation + rel->r_addend;
01304                   }
01305                 else
01306                   {
01307                     long indx;
01308 
01309                     if (bfd_is_abs_section (sec))
01310                      indx = 0;
01311                     else if (sec == NULL || sec->owner == NULL)
01312                      {
01313                        bfd_set_error (bfd_error_bad_value);
01314                        return FALSE;
01315                      }
01316                     else
01317                      {
01318                        asection *osec;
01319 
01320                        /* We are turning this relocation into one
01321                           against a section symbol.  It would be
01322                           proper to subtract the symbol's value,
01323                           osec->vma, from the emitted reloc addend,
01324                           but ld.so expects buggy relocs.  */
01325                        osec = sec->output_section;
01326                        indx = elf_section_data (osec)->dynindx;
01327                        if (indx == 0)
01328                          {
01329                            struct elf_link_hash_table *htab;
01330                            htab = elf_hash_table (info);
01331                            osec = htab->text_index_section;
01332                            indx = elf_section_data (osec)->dynindx;
01333                          }
01334                        BFD_ASSERT (indx != 0);
01335 #ifdef DEBUG
01336                        if (indx <= 0)
01337                          {
01338                            printf ("indx=%ld section=%s flags=%08x name=%s\n",
01339                                   indx, osec->name, osec->flags,
01340                                   h->root.root.string);
01341                          }
01342 #endif
01343                      }
01344 
01345                     outrel.r_info = ELF32_R_INFO (indx, r_type);
01346                     outrel.r_addend = relocation + rel->r_addend;
01347                   }
01348               }
01349 
01350              loc = sreloc->contents;
01351              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
01352              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
01353 
01354              /* This reloc will be computed at runtime, so there's no
01355                need to do anything now, unless this is a RELATIVE
01356                reloc in an unallocated section.  */
01357              if (skip == -1
01358                 || (input_section->flags & SEC_ALLOC) != 0
01359                 || ELF32_R_TYPE (outrel.r_info) != R_I370_RELATIVE)
01360               continue;
01361            }
01362          break;
01363 
01364        case (int) R_I370_COPY:
01365        case (int) R_I370_RELATIVE:
01366          (*_bfd_error_handler)
01367            ("%B: Relocation %s is not yet supported for symbol %s.",
01368             input_bfd,
01369             i370_elf_howto_table[(int) r_type]->name,
01370             sym_name);
01371 
01372          bfd_set_error (bfd_error_invalid_operation);
01373          ret = FALSE;
01374          continue;
01375        }
01376 
01377 #ifdef DEBUG
01378       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
01379               howto->name,
01380               (int)r_type,
01381               sym_name,
01382               r_symndx,
01383               (long) offset,
01384               (long) addend);
01385 #endif
01386 
01387       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
01388                                 offset, relocation, addend);
01389 
01390       if (r != bfd_reloc_ok)
01391        {
01392          ret = FALSE;
01393          switch (r)
01394            {
01395            default:
01396              break;
01397 
01398            case bfd_reloc_overflow:
01399              {
01400               const char *name;
01401 
01402               if (h != NULL)
01403                 name = NULL;
01404               else
01405                 {
01406                   name = bfd_elf_string_from_elf_section (input_bfd,
01407                                                      symtab_hdr->sh_link,
01408                                                      sym->st_name);
01409                   if (name == NULL)
01410                     break;
01411 
01412                   if (*name == '\0')
01413                     name = bfd_section_name (input_bfd, sec);
01414                 }
01415 
01416               (*info->callbacks->reloc_overflow) (info,
01417                                               (h ? &h->root : NULL),
01418                                               name,
01419                                               howto->name,
01420                                               (bfd_vma) 0,
01421                                               input_bfd,
01422                                               input_section,
01423                                               offset);
01424              }
01425              break;
01426            }
01427        }
01428     }
01429 
01430 #ifdef DEBUG
01431   fprintf (stderr, "\n");
01432 #endif
01433 
01434   return ret;
01435 }
01436 
01437 #define TARGET_BIG_SYM             bfd_elf32_i370_vec
01438 #define TARGET_BIG_NAME            "elf32-i370"
01439 #define ELF_ARCH            bfd_arch_i370
01440 #define ELF_MACHINE_CODE    EM_S370
01441 #ifdef EM_I370_OLD
01442 #define ELF_MACHINE_ALT1    EM_I370_OLD
01443 #endif
01444 #define ELF_MAXPAGESIZE            0x1000
01445 #define ELF_OSABI           ELFOSABI_LINUX
01446 
01447 #define elf_info_to_howto   i370_elf_info_to_howto
01448 
01449 #define elf_backend_plt_not_loaded 1
01450 #define elf_backend_rela_normal    1
01451 
01452 #define bfd_elf32_bfd_reloc_type_lookup          i370_elf_reloc_type_lookup
01453 #define bfd_elf32_bfd_reloc_name_lookup   i370_elf_reloc_name_lookup
01454 #define bfd_elf32_bfd_set_private_flags          i370_elf_set_private_flags
01455 #define bfd_elf32_bfd_merge_private_bfd_data     i370_elf_merge_private_bfd_data
01456 #define elf_backend_relocate_section             i370_elf_relocate_section
01457 
01458 /* Dynamic loader support is mostly broken; just enough here to be able to
01459    link glibc's ld.so without errors.  */
01460 #define elf_backend_create_dynamic_sections      i370_elf_create_dynamic_sections
01461 #define elf_backend_size_dynamic_sections i370_elf_size_dynamic_sections
01462 #define elf_backend_init_index_section           _bfd_elf_init_1_index_section
01463 #define elf_backend_finish_dynamic_sections      i370_elf_finish_dynamic_sections
01464 #define elf_backend_fake_sections         i370_elf_fake_sections
01465 #define elf_backend_section_from_shdr            i370_elf_section_from_shdr
01466 #define elf_backend_adjust_dynamic_symbol i370_elf_adjust_dynamic_symbol
01467 #define elf_backend_check_relocs          i370_elf_check_relocs
01468 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
01469 
01470 static int
01471 i370_noop (void)
01472 {
01473   return 1;
01474 }
01475 
01476 #define elf_backend_finish_dynamic_symbol \
01477   (bfd_boolean (*) \
01478      (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, \
01479       Elf_Internal_Sym *)) i370_noop
01480 
01481 #include "elf32-target.h"