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