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