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