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