source: trunk/src/binutils/bfd/elf32-i370.c@ 10

Last change on this file since 10 was 10, checked in by bird, 22 years ago

Initial revision

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