Coverage Report

Created: 2023-08-28 06:23

/src/binutils-gdb/bfd/elf32-hppa.c
Line
Count
Source (jump to first uncovered line)
1
/* BFD back-end for HP PA-RISC ELF files.
2
   Copyright (C) 1990-2023 Free Software Foundation, Inc.
3
4
   Original code by
5
  Center for Software Science
6
  Department of Computer Science
7
  University of Utah
8
   Largely rewritten by Alan Modra <alan@linuxcare.com.au>
9
   Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
10
   TLS support written by Randolph Chung <tausq@debian.org>
11
12
   This file is part of BFD, the Binary File Descriptor library.
13
14
   This program is free software; you can redistribute it and/or modify
15
   it under the terms of the GNU General Public License as published by
16
   the Free Software Foundation; either version 3 of the License, or
17
   (at your option) any later version.
18
19
   This program is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
   GNU General Public License for more details.
23
24
   You should have received a copy of the GNU General Public License
25
   along with this program; if not, write to the Free Software
26
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27
   MA 02110-1301, USA.  */
28
29
#include "sysdep.h"
30
#include "bfd.h"
31
#include "libbfd.h"
32
#include "elf-bfd.h"
33
#include "elf/hppa.h"
34
#include "libhppa.h"
35
#include "elf32-hppa.h"
36
#define ARCH_SIZE   32
37
#include "elf32-hppa.h"
38
#include "elf-hppa.h"
39
40
/* In order to gain some understanding of code in this file without
41
   knowing all the intricate details of the linker, note the
42
   following:
43
44
   Functions named elf32_hppa_* are called by external routines, other
45
   functions are only called locally.  elf32_hppa_* functions appear
46
   in this file more or less in the order in which they are called
47
   from external routines.  eg. elf32_hppa_check_relocs is called
48
   early in the link process, elf32_hppa_finish_dynamic_sections is
49
   one of the last functions.  */
50
51
/* We use two hash tables to hold information for linking PA ELF objects.
52
53
   The first is the elf32_hppa_link_hash_table which is derived
54
   from the standard ELF linker hash table.  We use this as a place to
55
   attach other hash tables and static information.
56
57
   The second is the stub hash table which is derived from the
58
   base BFD hash table.  The stub hash table holds the information
59
   necessary to build the linker stubs during a link.
60
61
   There are a number of different stubs generated by the linker.
62
63
   Long branch stub:
64
   :    ldil LR'X,%r1
65
   :    be,n RR'X(%sr4,%r1)
66
67
   PIC long branch stub:
68
   :    b,l .+8,%r1
69
   :    addil LR'X - ($PIC_pcrel$0 - 4),%r1
70
   :    be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
71
72
   Import stub to call shared library routine from normal object file
73
   (single sub-space version)
74
   :    addil LR'lt_ptr+ltoff,%dp ; get PLT address
75
   :    ldo RR'lt_ptr+ltoff(%r1),%r22   ; 
76
   :    ldw 0(%r22),%r21    ; get procedure entry point
77
   :    bv %r0(%r21)
78
   :    ldw 4(%r22),%r19    ; get new dlt value.
79
80
   Import stub to call shared library routine from shared library
81
   (single sub-space version)
82
   :    addil LR'ltoff,%r19   ; get PLT address
83
   :    ldo RR'ltoff(%r1),%r22
84
   :    ldw 0(%r22),%r21    ; get procedure entry point
85
   :    bv %r0(%r21)
86
   :    ldw 4(%r22),%r19    ; get new dlt value.
87
88
   Import stub to call shared library routine from normal object file
89
   (multiple sub-space support)
90
   :    addil LR'lt_ptr+ltoff,%dp ; get PLT address
91
   :    ldo RR'lt_ptr+ltoff(%r1),%r22   ; 
92
   :    ldw 0(%r22),%r21    ; get procedure entry point
93
   :    ldsid (%r21),%r1    ; get target sid
94
   :    ldw 4(%r22),%r19    ; get new dlt value.
95
   :    mtsp %r1,%sr0
96
   :    be 0(%sr0,%r21)     ; branch to target
97
   :    stw %rp,-24(%sp)    ; save rp
98
99
   Import stub to call shared library routine from shared library
100
   (multiple sub-space support)
101
   :    addil LR'ltoff,%r19   ; get PLT address
102
   :    ldo RR'ltoff(%r1),%r22
103
   :    ldw 0(%r22),%r21    ; get procedure entry point
104
   :    ldsid (%r21),%r1    ; get target sid
105
   :    ldw 4(%r22),%r19    ; get new dlt value.
106
   :    mtsp %r1,%sr0
107
   :    be 0(%sr0,%r21)     ; branch to target
108
   :    stw %rp,-24(%sp)    ; save rp
109
110
   Export stub to return from shared lib routine (multiple sub-space support)
111
   One of these is created for each exported procedure in a shared
112
   library (and stored in the shared lib).  Shared lib routines are
113
   called via the first instruction in the export stub so that we can
114
   do an inter-space return.  Not required for single sub-space.
115
   :    bl,n X,%rp      ; trap the return
116
   :    nop
117
   :    ldw -24(%sp),%rp    ; restore the original rp
118
   :    ldsid (%rp),%r1
119
   :    mtsp %r1,%sr0
120
   :    be,n 0(%sr0,%rp)    ; inter-space return.  */
121
122
123
/* Variable names follow a coding style.
124
   Please follow this (Apps Hungarian) style:
125
126
   Structure/Variable     Prefix
127
   elf_link_hash_table      "etab"
128
   elf_link_hash_entry      "eh"
129
130
   elf32_hppa_link_hash_table   "htab"
131
   elf32_hppa_link_hash_entry   "hh"
132
133
   bfd_hash_table     "btab"
134
   bfd_hash_entry     "bh"
135
136
   bfd_hash_table containing stubs  "bstab"
137
   elf32_hppa_stub_hash_entry   "hsh"
138
139
   Always remember to use GNU Coding Style. */
140
141
0
#define PLT_ENTRY_SIZE 8
142
0
#define GOT_ENTRY_SIZE 4
143
0
#define LONG_BRANCH_STUB_SIZE 8
144
0
#define LONG_BRANCH_SHARED_STUB_SIZE 12
145
0
#define IMPORT_STUB_SIZE 20
146
0
#define IMPORT_SHARED_STUB_SIZE 32
147
0
#define EXPORT_STUB_SIZE 24
148
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
149
150
static const bfd_byte plt_stub[] =
151
{
152
  0x0e, 0x80, 0x10, 0x95,  /* 1: ldw  0(%r20),%r21    */
153
  0xea, 0xa0, 0xc0, 0x00,  /*    bv %r0(%r21)   */
154
  0x0e, 0x88, 0x10, 0x95,  /*    ldw  4(%r20),%r21    */
155
#define PLT_STUB_ENTRY (3*4)
156
  0xea, 0x9f, 0x1f, 0xdd,  /*    b,l  1b,%r20     */
157
  0xd6, 0x80, 0x1c, 0x1e,  /*    depi 0,31,2,%r20   */
158
  0x00, 0xc0, 0xff, 0xee,  /* 9: .word  fixup_func    */
159
  0xde, 0xad, 0xbe, 0xef   /*    .word  fixup_ltp   */
160
};
161
162
/* Section name for stubs is the associated section name plus this
163
   string.  */
164
0
#define STUB_SUFFIX ".stub"
165
166
/* We don't need to copy certain PC- or GP-relative dynamic relocs
167
   into a shared object's dynamic section.  All the relocs of the
168
   limited class we are interested in, are absolute.  */
169
#ifndef RELATIVE_DYNRELOCS
170
#define RELATIVE_DYNRELOCS 0
171
0
#define IS_ABSOLUTE_RELOC(r_type) 1
172
0
#define pc_dynrelocs(hh) 0
173
#endif
174
175
/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
176
   copying dynamic variables from a shared lib into an app's dynbss
177
   section, and instead use a dynamic relocation to point into the
178
   shared lib.  */
179
0
#define ELIMINATE_COPY_RELOCS 1
180
181
enum elf32_hppa_stub_type
182
{
183
  hppa_stub_long_branch,
184
  hppa_stub_long_branch_shared,
185
  hppa_stub_import,
186
  hppa_stub_import_shared,
187
  hppa_stub_export,
188
  hppa_stub_none
189
};
190
191
struct elf32_hppa_stub_hash_entry
192
{
193
  /* Base hash table entry structure.  */
194
  struct bfd_hash_entry bh_root;
195
196
  /* The stub section.  */
197
  asection *stub_sec;
198
199
  /* Offset within stub_sec of the beginning of this stub.  */
200
  bfd_vma stub_offset;
201
202
  /* Given the symbol's value and its section we can determine its final
203
     value when building the stubs (so the stub knows where to jump.  */
204
  bfd_vma target_value;
205
  asection *target_section;
206
207
  enum elf32_hppa_stub_type stub_type;
208
209
  /* The symbol table entry, if any, that this was derived from.  */
210
  struct elf32_hppa_link_hash_entry *hh;
211
212
  /* Where this stub is being called from, or, in the case of combined
213
     stub sections, the first input section in the group.  */
214
  asection *id_sec;
215
};
216
217
enum _tls_type
218
  {
219
    GOT_UNKNOWN = 0,
220
    GOT_NORMAL = 1,
221
    GOT_TLS_GD = 2,
222
    GOT_TLS_LDM = 4,
223
    GOT_TLS_IE = 8
224
  };
225
226
struct elf32_hppa_link_hash_entry
227
{
228
  struct elf_link_hash_entry eh;
229
230
  /* A pointer to the most recently used stub hash entry against this
231
     symbol.  */
232
  struct elf32_hppa_stub_hash_entry *hsh_cache;
233
234
  ENUM_BITFIELD (_tls_type) tls_type : 8;
235
236
  /* Set if this symbol is used by a plabel reloc.  */
237
  unsigned int plabel:1;
238
};
239
240
struct elf32_hppa_link_hash_table
241
{
242
  /* The main hash table.  */
243
  struct elf_link_hash_table etab;
244
245
  /* The stub hash table.  */
246
  struct bfd_hash_table bstab;
247
248
  /* Linker stub bfd.  */
249
  bfd *stub_bfd;
250
251
  /* Linker call-backs.  */
252
  asection * (*add_stub_section) (const char *, asection *);
253
  void (*layout_sections_again) (void);
254
255
  /* Array to keep track of which stub sections have been created, and
256
     information on stub grouping.  */
257
  struct map_stub
258
  {
259
    /* This is the section to which stubs in the group will be
260
       attached.  */
261
    asection *link_sec;
262
    /* The stub section.  */
263
    asection *stub_sec;
264
  } *stub_group;
265
266
  /* Assorted information used by elf32_hppa_size_stubs.  */
267
  unsigned int bfd_count;
268
  unsigned int top_index;
269
  asection **input_list;
270
  Elf_Internal_Sym **all_local_syms;
271
272
  /* Used during a final link to store the base of the text and data
273
     segments so that we can perform SEGREL relocations.  */
274
  bfd_vma text_segment_base;
275
  bfd_vma data_segment_base;
276
277
  /* Whether we support multiple sub-spaces for shared libs.  */
278
  unsigned int multi_subspace:1;
279
280
  /* Flags set when various size branches are detected.  Used to
281
     select suitable defaults for the stub group size.  */
282
  unsigned int has_12bit_branch:1;
283
  unsigned int has_17bit_branch:1;
284
  unsigned int has_22bit_branch:1;
285
286
  /* Set if we need a .plt stub to support lazy dynamic linking.  */
287
  unsigned int need_plt_stub:1;
288
289
  /* Data for LDM relocations.  */
290
  union
291
  {
292
    bfd_signed_vma refcount;
293
    bfd_vma offset;
294
  } tls_ldm_got;
295
};
296
297
/* Various hash macros and functions.  */
298
#define hppa_link_hash_table(p) \
299
0
  ((is_elf_hash_table ((p)->hash)          \
300
0
    && elf_hash_table_id (elf_hash_table (p)) == HPPA32_ELF_DATA) \
301
0
   ? (struct elf32_hppa_link_hash_table *) (p)->hash : NULL)
302
303
#define hppa_elf_hash_entry(ent) \
304
0
  ((struct elf32_hppa_link_hash_entry *)(ent))
305
306
#define hppa_stub_hash_entry(ent) \
307
0
  ((struct elf32_hppa_stub_hash_entry *)(ent))
308
309
#define hppa_stub_hash_lookup(table, string, create, copy) \
310
0
  ((struct elf32_hppa_stub_hash_entry *) \
311
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
312
313
#define hppa_elf_local_got_tls_type(abfd) \
314
0
  ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
315
316
#define hh_name(hh) \
317
0
  (hh ? hh->eh.root.root.string : "<undef>")
318
319
#define eh_name(eh) \
320
0
  (eh ? eh->root.root.string : "<undef>")
321
322
/* Assorted hash table functions.  */
323
324
/* Initialize an entry in the stub hash table.  */
325
326
static struct bfd_hash_entry *
327
stub_hash_newfunc (struct bfd_hash_entry *entry,
328
       struct bfd_hash_table *table,
329
       const char *string)
330
0
{
331
  /* Allocate the structure if it has not already been allocated by a
332
     subclass.  */
333
0
  if (entry == NULL)
334
0
    {
335
0
      entry = bfd_hash_allocate (table,
336
0
         sizeof (struct elf32_hppa_stub_hash_entry));
337
0
      if (entry == NULL)
338
0
  return entry;
339
0
    }
340
341
  /* Call the allocation method of the superclass.  */
342
0
  entry = bfd_hash_newfunc (entry, table, string);
343
0
  if (entry != NULL)
344
0
    {
345
0
      struct elf32_hppa_stub_hash_entry *hsh;
346
347
      /* Initialize the local fields.  */
348
0
      hsh = hppa_stub_hash_entry (entry);
349
0
      hsh->stub_sec = NULL;
350
0
      hsh->stub_offset = 0;
351
0
      hsh->target_value = 0;
352
0
      hsh->target_section = NULL;
353
0
      hsh->stub_type = hppa_stub_long_branch;
354
0
      hsh->hh = NULL;
355
0
      hsh->id_sec = NULL;
356
0
    }
357
358
0
  return entry;
359
0
}
360
361
/* Initialize an entry in the link hash table.  */
362
363
static struct bfd_hash_entry *
364
hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
365
      struct bfd_hash_table *table,
366
      const char *string)
367
0
{
368
  /* Allocate the structure if it has not already been allocated by a
369
     subclass.  */
370
0
  if (entry == NULL)
371
0
    {
372
0
      entry = bfd_hash_allocate (table,
373
0
         sizeof (struct elf32_hppa_link_hash_entry));
374
0
      if (entry == NULL)
375
0
  return entry;
376
0
    }
377
378
  /* Call the allocation method of the superclass.  */
379
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
380
0
  if (entry != NULL)
381
0
    {
382
0
      struct elf32_hppa_link_hash_entry *hh;
383
384
      /* Initialize the local fields.  */
385
0
      hh = hppa_elf_hash_entry (entry);
386
0
      hh->hsh_cache = NULL;
387
0
      hh->plabel = 0;
388
0
      hh->tls_type = GOT_UNKNOWN;
389
0
    }
390
391
0
  return entry;
392
0
}
393
394
/* Free the derived linker hash table.  */
395
396
static void
397
elf32_hppa_link_hash_table_free (bfd *obfd)
398
0
{
399
0
  struct elf32_hppa_link_hash_table *htab
400
0
    = (struct elf32_hppa_link_hash_table *) obfd->link.hash;
401
402
0
  bfd_hash_table_free (&htab->bstab);
403
0
  _bfd_elf_link_hash_table_free (obfd);
404
0
}
405
406
/* Create the derived linker hash table.  The PA ELF port uses the derived
407
   hash table to keep information specific to the PA ELF linker (without
408
   using static variables).  */
409
410
static struct bfd_link_hash_table *
411
elf32_hppa_link_hash_table_create (bfd *abfd)
412
0
{
413
0
  struct elf32_hppa_link_hash_table *htab;
414
0
  size_t amt = sizeof (*htab);
415
416
0
  htab = bfd_zmalloc (amt);
417
0
  if (htab == NULL)
418
0
    return NULL;
419
420
0
  if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
421
0
              sizeof (struct elf32_hppa_link_hash_entry),
422
0
              HPPA32_ELF_DATA))
423
0
    {
424
0
      free (htab);
425
0
      return NULL;
426
0
    }
427
428
  /* Init the stub hash table too.  */
429
0
  if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
430
0
          sizeof (struct elf32_hppa_stub_hash_entry)))
431
0
    {
432
0
      _bfd_elf_link_hash_table_free (abfd);
433
0
      return NULL;
434
0
    }
435
0
  htab->etab.root.hash_table_free = elf32_hppa_link_hash_table_free;
436
0
  htab->etab.dt_pltgot_required = true;
437
438
0
  htab->text_segment_base = (bfd_vma) -1;
439
0
  htab->data_segment_base = (bfd_vma) -1;
440
0
  return &htab->etab.root;
441
0
}
442
443
/* Initialize the linker stubs BFD so that we can use it for linker
444
   created dynamic sections.  */
445
446
void
447
elf32_hppa_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
448
0
{
449
0
  struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
450
451
0
  elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS32;
452
0
  htab->etab.dynobj = abfd;
453
0
}
454
455
/* Build a name for an entry in the stub hash table.  */
456
457
static char *
458
hppa_stub_name (const asection *input_section,
459
    const asection *sym_sec,
460
    const struct elf32_hppa_link_hash_entry *hh,
461
    const Elf_Internal_Rela *rela)
462
0
{
463
0
  char *stub_name;
464
0
  bfd_size_type len;
465
466
0
  if (hh)
467
0
    {
468
0
      len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
469
0
      stub_name = bfd_malloc (len);
470
0
      if (stub_name != NULL)
471
0
  sprintf (stub_name, "%08x_%s+%x",
472
0
     input_section->id & 0xffffffff,
473
0
     hh_name (hh),
474
0
     (int) rela->r_addend & 0xffffffff);
475
0
    }
476
0
  else
477
0
    {
478
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
479
0
      stub_name = bfd_malloc (len);
480
0
      if (stub_name != NULL)
481
0
  sprintf (stub_name, "%08x_%x:%x+%x",
482
0
     input_section->id & 0xffffffff,
483
0
     sym_sec->id & 0xffffffff,
484
0
     (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
485
0
     (int) rela->r_addend & 0xffffffff);
486
0
    }
487
0
  return stub_name;
488
0
}
489
490
/* Look up an entry in the stub hash.  Stub entries are cached because
491
   creating the stub name takes a bit of time.  */
492
493
static struct elf32_hppa_stub_hash_entry *
494
hppa_get_stub_entry (const asection *input_section,
495
         const asection *sym_sec,
496
         struct elf32_hppa_link_hash_entry *hh,
497
         const Elf_Internal_Rela *rela,
498
         struct elf32_hppa_link_hash_table *htab)
499
0
{
500
0
  struct elf32_hppa_stub_hash_entry *hsh_entry;
501
0
  const asection *id_sec;
502
503
  /* If this input section is part of a group of sections sharing one
504
     stub section, then use the id of the first section in the group.
505
     Stub names need to include a section id, as there may well be
506
     more than one stub used to reach say, printf, and we need to
507
     distinguish between them.  */
508
0
  id_sec = htab->stub_group[input_section->id].link_sec;
509
0
  if (id_sec == NULL)
510
0
    return NULL;
511
512
0
  if (hh != NULL && hh->hsh_cache != NULL
513
0
      && hh->hsh_cache->hh == hh
514
0
      && hh->hsh_cache->id_sec == id_sec)
515
0
    {
516
0
      hsh_entry = hh->hsh_cache;
517
0
    }
518
0
  else
519
0
    {
520
0
      char *stub_name;
521
522
0
      stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
523
0
      if (stub_name == NULL)
524
0
  return NULL;
525
526
0
      hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
527
0
            stub_name, false, false);
528
0
      if (hh != NULL)
529
0
  hh->hsh_cache = hsh_entry;
530
531
0
      free (stub_name);
532
0
    }
533
534
0
  return hsh_entry;
535
0
}
536
537
/* Add a new stub entry to the stub hash.  Not all fields of the new
538
   stub entry are initialised.  */
539
540
static struct elf32_hppa_stub_hash_entry *
541
hppa_add_stub (const char *stub_name,
542
         asection *section,
543
         struct elf32_hppa_link_hash_table *htab)
544
0
{
545
0
  asection *link_sec;
546
0
  asection *stub_sec;
547
0
  struct elf32_hppa_stub_hash_entry *hsh;
548
549
0
  link_sec = htab->stub_group[section->id].link_sec;
550
0
  stub_sec = htab->stub_group[section->id].stub_sec;
551
0
  if (stub_sec == NULL)
552
0
    {
553
0
      stub_sec = htab->stub_group[link_sec->id].stub_sec;
554
0
      if (stub_sec == NULL)
555
0
  {
556
0
    size_t namelen;
557
0
    bfd_size_type len;
558
0
    char *s_name;
559
560
0
    namelen = strlen (link_sec->name);
561
0
    len = namelen + sizeof (STUB_SUFFIX);
562
0
    s_name = bfd_alloc (htab->stub_bfd, len);
563
0
    if (s_name == NULL)
564
0
      return NULL;
565
566
0
    memcpy (s_name, link_sec->name, namelen);
567
0
    memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
568
0
    stub_sec = (*htab->add_stub_section) (s_name, link_sec);
569
0
    if (stub_sec == NULL)
570
0
      return NULL;
571
0
    htab->stub_group[link_sec->id].stub_sec = stub_sec;
572
0
  }
573
0
      htab->stub_group[section->id].stub_sec = stub_sec;
574
0
    }
575
576
  /* Enter this entry into the linker stub hash table.  */
577
0
  hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
578
0
              true, false);
579
0
  if (hsh == NULL)
580
0
    {
581
      /* xgettext:c-format */
582
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
583
0
        section->owner, stub_name);
584
0
      return NULL;
585
0
    }
586
587
0
  hsh->stub_sec = stub_sec;
588
0
  hsh->stub_offset = 0;
589
0
  hsh->id_sec = link_sec;
590
0
  return hsh;
591
0
}
592
593
/* Determine the type of stub needed, if any, for a call.  */
594
595
static enum elf32_hppa_stub_type
596
hppa_type_of_stub (asection *input_sec,
597
       const Elf_Internal_Rela *rela,
598
       struct elf32_hppa_link_hash_entry *hh,
599
       bfd_vma destination,
600
       struct bfd_link_info *info)
601
0
{
602
0
  bfd_vma location;
603
0
  bfd_vma branch_offset;
604
0
  bfd_vma max_branch_offset;
605
0
  unsigned int r_type;
606
607
0
  if (hh != NULL
608
0
      && hh->eh.plt.offset != (bfd_vma) -1
609
0
      && hh->eh.dynindx != -1
610
0
      && !hh->plabel
611
0
      && (bfd_link_pic (info)
612
0
    || !hh->eh.def_regular
613
0
    || hh->eh.root.type == bfd_link_hash_defweak))
614
0
    {
615
      /* We need an import stub.  Decide between hppa_stub_import
616
   and hppa_stub_import_shared later.  */
617
0
      return hppa_stub_import;
618
0
    }
619
620
0
  if (destination == (bfd_vma) -1)
621
0
    return hppa_stub_none;
622
623
  /* Determine where the call point is.  */
624
0
  location = (input_sec->output_offset
625
0
        + input_sec->output_section->vma
626
0
        + rela->r_offset);
627
628
0
  branch_offset = destination - location - 8;
629
0
  r_type = ELF32_R_TYPE (rela->r_info);
630
631
  /* Determine if a long branch stub is needed.  parisc branch offsets
632
     are relative to the second instruction past the branch, ie. +8
633
     bytes on from the branch instruction location.  The offset is
634
     signed and counts in units of 4 bytes.  */
635
0
  if (r_type == (unsigned int) R_PARISC_PCREL17F)
636
0
    max_branch_offset = (1 << (17 - 1)) << 2;
637
638
0
  else if (r_type == (unsigned int) R_PARISC_PCREL12F)
639
0
    max_branch_offset = (1 << (12 - 1)) << 2;
640
641
0
  else /* R_PARISC_PCREL22F.  */
642
0
    max_branch_offset = (1 << (22 - 1)) << 2;
643
644
0
  if (branch_offset + max_branch_offset >= 2*max_branch_offset)
645
0
    return hppa_stub_long_branch;
646
647
0
  return hppa_stub_none;
648
0
}
649
650
/* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
651
   IN_ARG contains the link info pointer.  */
652
653
0
#define LDIL_R1   0x20200000  /* ldil  LR'XXX,%r1   */
654
0
#define BE_SR4_R1 0xe0202002  /* be,n  RR'XXX(%sr4,%r1) */
655
656
#define BL_R1   0xe8200000  /* b,l   .+8,%r1    */
657
0
#define ADDIL_R1  0x28200000  /* addil LR'XXX,%r1,%r1   */
658
#define DEPI_R1   0xd4201c1e  /* depi  0,31,2,%r1   */
659
660
0
#define ADDIL_DP  0x2b600000  /* addil LR'XXX,%dp,%r1   */
661
#define LDW_R1_R21  0x48350000  /* ldw   RR'XXX(%sr0,%r1),%r21  */
662
#define BV_R0_R21 0xeaa0c000  /* bv    %r0(%r21)    */
663
#define LDW_R1_R19  0x48330000  /* ldw   RR'XXX(%sr0,%r1),%r19  */
664
665
0
#define ADDIL_R19 0x2a600000  /* addil LR'XXX,%r19,%r1  */
666
#define LDW_R1_DP 0x483b0000  /* ldw   RR'XXX(%sr0,%r1),%dp */
667
668
0
#define LDO_R1_R22  0x34360000  /* ldo   RR'XXX(%r1),%r22 */
669
#define LDW_R22_R21 0x0ec01095  /* ldw   0(%r22),%r21   */
670
#define LDW_R22_R19 0x0ec81093  /* ldw   4(%r22),%r19   */
671
672
#define LDSID_R21_R1  0x02a010a1  /* ldsid (%sr0,%r21),%r1  */
673
#define MTSP_R1   0x00011820  /* mtsp  %r1,%sr0   */
674
#define BE_SR0_R21  0xe2a00000  /* be    0(%sr0,%r21)   */
675
#define STW_RP    0x6bc23fd1  /* stw   %rp,-24(%sr0,%sp)  */
676
677
0
#define BL22_RP   0xe800a002  /* b,l,n XXX,%rp    */
678
0
#define BL_RP   0xe8400002  /* b,l,n XXX,%rp    */
679
#define NOP   0x08000240  /* nop        */
680
#define LDW_RP    0x4bc23fd1  /* ldw   -24(%sr0,%sp),%rp  */
681
#define LDSID_RP_R1 0x004010a1  /* ldsid (%sr0,%rp),%r1   */
682
#define BE_SR0_RP 0xe0400002  /* be,n  0(%sr0,%rp)    */
683
684
#ifndef R19_STUBS
685
#define R19_STUBS 1
686
#endif
687
688
#if R19_STUBS
689
#define LDW_R1_DLT  LDW_R1_R19
690
#else
691
#define LDW_R1_DLT  LDW_R1_DP
692
#endif
693
694
static bool
695
hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
696
0
{
697
0
  struct elf32_hppa_stub_hash_entry *hsh;
698
0
  struct bfd_link_info *info;
699
0
  struct elf32_hppa_link_hash_table *htab;
700
0
  asection *stub_sec;
701
0
  bfd *stub_bfd;
702
0
  bfd_byte *loc;
703
0
  bfd_vma sym_value;
704
0
  bfd_vma insn;
705
0
  bfd_vma off;
706
0
  int val;
707
0
  int size;
708
709
  /* Massage our args to the form they really have.  */
710
0
  hsh = hppa_stub_hash_entry (bh);
711
0
  info = (struct bfd_link_info *)in_arg;
712
713
0
  htab = hppa_link_hash_table (info);
714
0
  if (htab == NULL)
715
0
    return false;
716
717
0
  stub_sec = hsh->stub_sec;
718
719
  /* Make a note of the offset within the stubs for this entry.  */
720
0
  hsh->stub_offset = stub_sec->size;
721
0
  loc = stub_sec->contents + hsh->stub_offset;
722
723
0
  stub_bfd = stub_sec->owner;
724
725
0
  switch (hsh->stub_type)
726
0
    {
727
0
    case hppa_stub_long_branch:
728
      /* Fail if the target section could not be assigned to an output
729
   section.  The user should fix his linker script.  */
730
0
      if (hsh->target_section->output_section == NULL
731
0
    && info->non_contiguous_regions)
732
0
  info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output "
733
0
          "section. Retry without "
734
0
          "--enable-non-contiguous-regions.\n"),
735
0
        hsh->target_section);
736
737
      /* Create the long branch.  A long branch is formed with "ldil"
738
   loading the upper bits of the target address into a register,
739
   then branching with "be" which adds in the lower bits.
740
   The "be" has its delay slot nullified.  */
741
0
      sym_value = (hsh->target_value
742
0
       + hsh->target_section->output_offset
743
0
       + hsh->target_section->output_section->vma);
744
745
0
      val = hppa_field_adjust (sym_value, 0, e_lrsel);
746
0
      insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
747
0
      bfd_put_32 (stub_bfd, insn, loc);
748
749
0
      val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
750
0
      insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
751
0
      bfd_put_32 (stub_bfd, insn, loc + 4);
752
753
0
      size = LONG_BRANCH_STUB_SIZE;
754
0
      break;
755
756
0
    case hppa_stub_long_branch_shared:
757
      /* Fail if the target section could not be assigned to an output
758
   section.  The user should fix his linker script.  */
759
0
      if (hsh->target_section->output_section == NULL
760
0
    && info->non_contiguous_regions)
761
0
  info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output "
762
0
          "section. Retry without "
763
0
          "--enable-non-contiguous-regions.\n"),
764
0
        hsh->target_section);
765
766
      /* Branches are relative.  This is where we are going to.  */
767
0
      sym_value = (hsh->target_value
768
0
       + hsh->target_section->output_offset
769
0
       + hsh->target_section->output_section->vma);
770
771
      /* And this is where we are coming from, more or less.  */
772
0
      sym_value -= (hsh->stub_offset
773
0
        + stub_sec->output_offset
774
0
        + stub_sec->output_section->vma);
775
776
0
      bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
777
0
      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
778
0
      insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
779
0
      bfd_put_32 (stub_bfd, insn, loc + 4);
780
781
0
      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
782
0
      insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
783
0
      bfd_put_32 (stub_bfd, insn, loc + 8);
784
0
      size = LONG_BRANCH_SHARED_STUB_SIZE;
785
0
      break;
786
787
0
    case hppa_stub_import:
788
0
    case hppa_stub_import_shared:
789
0
      off = hsh->hh->eh.plt.offset;
790
0
      if (off >= (bfd_vma) -2)
791
0
  abort ();
792
793
0
      off &= ~ (bfd_vma) 1;
794
0
      sym_value = (off
795
0
       + htab->etab.splt->output_offset
796
0
       + htab->etab.splt->output_section->vma
797
0
       - elf_gp (htab->etab.splt->output_section->owner));
798
799
0
      insn = ADDIL_DP;
800
0
#if R19_STUBS
801
0
      if (hsh->stub_type == hppa_stub_import_shared)
802
0
  insn = ADDIL_R19;
803
0
#endif
804
805
      /* Load function descriptor address into register %r22.  It is
806
   sometimes needed for lazy binding.  */
807
0
      val = hppa_field_adjust (sym_value, 0, e_lrsel),
808
0
      insn = hppa_rebuild_insn ((int) insn, val, 21);
809
0
      bfd_put_32 (stub_bfd, insn, loc);
810
811
0
      val = hppa_field_adjust (sym_value, 0, e_rrsel);
812
0
      insn = hppa_rebuild_insn ((int) LDO_R1_R22, val, 14);
813
0
      bfd_put_32 (stub_bfd, insn, loc + 4);
814
815
0
      bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R21, loc + 8);
816
817
0
      if (htab->multi_subspace)
818
0
  {
819
0
    bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
820
0
    bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19,  loc + 16);
821
0
    bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,      loc + 20);
822
0
    bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21,   loc + 24);
823
0
    bfd_put_32 (stub_bfd, (bfd_vma) STW_RP,       loc + 28);
824
825
0
    size = IMPORT_SHARED_STUB_SIZE;
826
0
  }
827
0
      else
828
0
  {
829
0
    bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 12);
830
0
    bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19, loc + 16);
831
832
0
    size = IMPORT_STUB_SIZE;
833
0
  }
834
835
0
      break;
836
837
0
    case hppa_stub_export:
838
      /* Fail if the target section could not be assigned to an output
839
   section.  The user should fix his linker script.  */
840
0
      if (hsh->target_section->output_section == NULL
841
0
    && info->non_contiguous_regions)
842
0
  info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output "
843
0
          "section. Retry without "
844
0
          "--enable-non-contiguous-regions.\n"),
845
0
        hsh->target_section);
846
847
      /* Branches are relative.  This is where we are going to.  */
848
0
      sym_value = (hsh->target_value
849
0
       + hsh->target_section->output_offset
850
0
       + hsh->target_section->output_section->vma);
851
852
      /* And this is where we are coming from.  */
853
0
      sym_value -= (hsh->stub_offset
854
0
        + stub_sec->output_offset
855
0
        + stub_sec->output_section->vma);
856
857
0
      if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
858
0
    && (!htab->has_22bit_branch
859
0
        || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
860
0
  {
861
0
    _bfd_error_handler
862
      /* xgettext:c-format */
863
0
      (_("%pB(%pA+%#" PRIx64 "): "
864
0
         "cannot reach %s, recompile with -ffunction-sections"),
865
0
       hsh->target_section->owner,
866
0
       stub_sec,
867
0
       (uint64_t) hsh->stub_offset,
868
0
       hsh->bh_root.string);
869
0
    bfd_set_error (bfd_error_bad_value);
870
0
    return false;
871
0
  }
872
873
0
      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
874
0
      if (!htab->has_22bit_branch)
875
0
  insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
876
0
      else
877
0
  insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
878
0
      bfd_put_32 (stub_bfd, insn, loc);
879
880
0
      bfd_put_32 (stub_bfd, (bfd_vma) NOP,     loc + 4);
881
0
      bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP,      loc + 8);
882
0
      bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
883
0
      bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,     loc + 16);
884
0
      bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP,   loc + 20);
885
886
      /* Point the function symbol at the stub.  */
887
0
      hsh->hh->eh.root.u.def.section = stub_sec;
888
0
      hsh->hh->eh.root.u.def.value = stub_sec->size;
889
890
0
      size = EXPORT_STUB_SIZE;
891
0
      break;
892
893
0
    default:
894
0
      BFD_FAIL ();
895
0
      return false;
896
0
    }
897
898
0
  stub_sec->size += size;
899
0
  return true;
900
0
}
901
902
#undef LDIL_R1
903
#undef BE_SR4_R1
904
#undef BL_R1
905
#undef ADDIL_R1
906
#undef DEPI_R1
907
#undef LDW_R1_R21
908
#undef LDW_R1_DLT
909
#undef LDW_R1_R19
910
#undef ADDIL_R19
911
#undef LDW_R1_DP
912
#undef LDSID_R21_R1
913
#undef MTSP_R1
914
#undef BE_SR0_R21
915
#undef STW_RP
916
#undef BV_R0_R21
917
#undef BL_RP
918
#undef NOP
919
#undef LDW_RP
920
#undef LDSID_RP_R1
921
#undef BE_SR0_RP
922
923
/* As above, but don't actually build the stub.  Just bump offset so
924
   we know stub section sizes.  */
925
926
static bool
927
hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
928
0
{
929
0
  struct elf32_hppa_stub_hash_entry *hsh;
930
0
  struct elf32_hppa_link_hash_table *htab;
931
0
  int size;
932
933
  /* Massage our args to the form they really have.  */
934
0
  hsh = hppa_stub_hash_entry (bh);
935
0
  htab = in_arg;
936
937
0
  if (hsh->stub_type == hppa_stub_long_branch)
938
0
    size = LONG_BRANCH_STUB_SIZE;
939
0
  else if (hsh->stub_type == hppa_stub_long_branch_shared)
940
0
    size = LONG_BRANCH_SHARED_STUB_SIZE;
941
0
  else if (hsh->stub_type == hppa_stub_export)
942
0
    size = EXPORT_STUB_SIZE;
943
0
  else /* hppa_stub_import or hppa_stub_import_shared.  */
944
0
    {
945
0
      if (htab->multi_subspace)
946
0
  size = IMPORT_SHARED_STUB_SIZE;
947
0
      else
948
0
  size = IMPORT_STUB_SIZE;
949
0
    }
950
951
0
  hsh->stub_sec->size += size;
952
0
  return true;
953
0
}
954
955
/* Return nonzero if ABFD represents an HPPA ELF32 file.
956
   Additionally we set the default architecture and machine.  */
957
958
static bool
959
elf32_hppa_object_p (bfd *abfd)
960
0
{
961
0
  Elf_Internal_Ehdr * i_ehdrp;
962
0
  unsigned int flags;
963
964
0
  i_ehdrp = elf_elfheader (abfd);
965
0
  if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
966
0
    {
967
      /* GCC on hppa-linux produces binaries with OSABI=GNU,
968
   but the kernel produces corefiles with OSABI=SysV.  */
969
0
      if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU &&
970
0
    i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
971
0
  return false;
972
0
    }
973
0
  else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
974
0
    {
975
      /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
976
   but the kernel produces corefiles with OSABI=SysV.  */
977
0
      if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
978
0
    i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
979
0
  return false;
980
0
    }
981
0
  else
982
0
    {
983
0
      if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
984
0
  return false;
985
0
    }
986
987
0
  flags = i_ehdrp->e_flags;
988
0
  switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
989
0
    {
990
0
    case EFA_PARISC_1_0:
991
0
      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
992
0
    case EFA_PARISC_1_1:
993
0
      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
994
0
    case EFA_PARISC_2_0:
995
0
      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
996
0
    case EFA_PARISC_2_0 | EF_PARISC_WIDE:
997
0
      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
998
0
    }
999
0
  return true;
1000
0
}
1001
1002
/* Create the .plt and .got sections, and set up our hash table
1003
   short-cuts to various dynamic sections.  */
1004
1005
static bool
1006
elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
1007
0
{
1008
0
  struct elf32_hppa_link_hash_table *htab;
1009
0
  struct elf_link_hash_entry *eh;
1010
1011
  /* Don't try to create the .plt and .got twice.  */
1012
0
  htab = hppa_link_hash_table (info);
1013
0
  if (htab == NULL)
1014
0
    return false;
1015
0
  if (htab->etab.splt != NULL)
1016
0
    return true;
1017
1018
  /* Call the generic code to do most of the work.  */
1019
0
  if (! _bfd_elf_create_dynamic_sections (abfd, info))
1020
0
    return false;
1021
1022
  /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
1023
     application, because __canonicalize_funcptr_for_compare needs it.  */
1024
0
  eh = elf_hash_table (info)->hgot;
1025
0
  eh->forced_local = 0;
1026
0
  eh->other = STV_DEFAULT;
1027
0
  return bfd_elf_link_record_dynamic_symbol (info, eh);
1028
0
}
1029
1030
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
1031
1032
static void
1033
elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
1034
         struct elf_link_hash_entry *eh_dir,
1035
         struct elf_link_hash_entry *eh_ind)
1036
0
{
1037
0
  struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
1038
1039
0
  hh_dir = hppa_elf_hash_entry (eh_dir);
1040
0
  hh_ind = hppa_elf_hash_entry (eh_ind);
1041
1042
0
  if (eh_ind->root.type == bfd_link_hash_indirect)
1043
0
    {
1044
0
      hh_dir->plabel |= hh_ind->plabel;
1045
0
      hh_dir->tls_type |= hh_ind->tls_type;
1046
0
      hh_ind->tls_type = GOT_UNKNOWN;
1047
0
    }
1048
1049
0
  _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
1050
0
}
1051
1052
static int
1053
elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1054
        int r_type, int is_local ATTRIBUTE_UNUSED)
1055
0
{
1056
  /* For now we don't support linker optimizations.  */
1057
0
  return r_type;
1058
0
}
1059
1060
/* Return a pointer to the local GOT, PLT and TLS reference counts
1061
   for ABFD.  Returns NULL if the storage allocation fails.  */
1062
1063
static bfd_signed_vma *
1064
hppa32_elf_local_refcounts (bfd *abfd)
1065
0
{
1066
0
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1067
0
  bfd_signed_vma *local_refcounts;
1068
1069
0
  local_refcounts = elf_local_got_refcounts (abfd);
1070
0
  if (local_refcounts == NULL)
1071
0
    {
1072
0
      bfd_size_type size;
1073
1074
      /* Allocate space for local GOT and PLT reference
1075
   counts.  Done this way to save polluting elf_obj_tdata
1076
   with another target specific pointer.  */
1077
0
      size = symtab_hdr->sh_info;
1078
0
      size *= 2 * sizeof (bfd_signed_vma);
1079
      /* Add in space to store the local GOT TLS types.  */
1080
0
      size += symtab_hdr->sh_info;
1081
0
      local_refcounts = bfd_zalloc (abfd, size);
1082
0
      if (local_refcounts == NULL)
1083
0
  return NULL;
1084
0
      elf_local_got_refcounts (abfd) = local_refcounts;
1085
0
      memset (hppa_elf_local_got_tls_type (abfd), GOT_UNKNOWN,
1086
0
        symtab_hdr->sh_info);
1087
0
    }
1088
0
  return local_refcounts;
1089
0
}
1090
1091
1092
/* Look through the relocs for a section during the first phase, and
1093
   calculate needed space in the global offset table, procedure linkage
1094
   table, and dynamic reloc sections.  At this point we haven't
1095
   necessarily read all the input files.  */
1096
1097
static bool
1098
elf32_hppa_check_relocs (bfd *abfd,
1099
       struct bfd_link_info *info,
1100
       asection *sec,
1101
       const Elf_Internal_Rela *relocs)
1102
0
{
1103
0
  Elf_Internal_Shdr *symtab_hdr;
1104
0
  struct elf_link_hash_entry **eh_syms;
1105
0
  const Elf_Internal_Rela *rela;
1106
0
  const Elf_Internal_Rela *rela_end;
1107
0
  struct elf32_hppa_link_hash_table *htab;
1108
0
  asection *sreloc;
1109
1110
0
  if (bfd_link_relocatable (info))
1111
0
    return true;
1112
1113
0
  htab = hppa_link_hash_table (info);
1114
0
  if (htab == NULL)
1115
0
    return false;
1116
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1117
0
  eh_syms = elf_sym_hashes (abfd);
1118
0
  sreloc = NULL;
1119
1120
0
  rela_end = relocs + sec->reloc_count;
1121
0
  for (rela = relocs; rela < rela_end; rela++)
1122
0
    {
1123
0
      enum {
1124
0
  NEED_GOT = 1,
1125
0
  NEED_PLT = 2,
1126
0
  NEED_DYNREL = 4,
1127
0
  PLT_PLABEL = 8
1128
0
      };
1129
1130
0
      unsigned int r_symndx, r_type;
1131
0
      struct elf32_hppa_link_hash_entry *hh;
1132
0
      int need_entry = 0;
1133
1134
0
      r_symndx = ELF32_R_SYM (rela->r_info);
1135
1136
0
      if (r_symndx < symtab_hdr->sh_info)
1137
0
  hh = NULL;
1138
0
      else
1139
0
  {
1140
0
    hh =  hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
1141
0
    while (hh->eh.root.type == bfd_link_hash_indirect
1142
0
     || hh->eh.root.type == bfd_link_hash_warning)
1143
0
      hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
1144
0
  }
1145
1146
0
      r_type = ELF32_R_TYPE (rela->r_info);
1147
0
      r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
1148
1149
0
      switch (r_type)
1150
0
  {
1151
0
  case R_PARISC_DLTIND14F:
1152
0
  case R_PARISC_DLTIND14R:
1153
0
  case R_PARISC_DLTIND21L:
1154
    /* This symbol requires a global offset table entry.  */
1155
0
    need_entry = NEED_GOT;
1156
0
    break;
1157
1158
0
  case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
1159
0
  case R_PARISC_PLABEL21L:
1160
0
  case R_PARISC_PLABEL32:
1161
    /* If the addend is non-zero, we break badly.  */
1162
0
    if (rela->r_addend != 0)
1163
0
      abort ();
1164
1165
    /* If we are creating a shared library, then we need to
1166
       create a PLT entry for all PLABELs, because PLABELs with
1167
       local symbols may be passed via a pointer to another
1168
       object.  Additionally, output a dynamic relocation
1169
       pointing to the PLT entry.
1170
1171
       For executables, the original 32-bit ABI allowed two
1172
       different styles of PLABELs (function pointers):  For
1173
       global functions, the PLABEL word points into the .plt
1174
       two bytes past a (function address, gp) pair, and for
1175
       local functions the PLABEL points directly at the
1176
       function.  The magic +2 for the first type allows us to
1177
       differentiate between the two.  As you can imagine, this
1178
       is a real pain when it comes to generating code to call
1179
       functions indirectly or to compare function pointers.
1180
       We avoid the mess by always pointing a PLABEL into the
1181
       .plt, even for local functions.  */
1182
0
    need_entry = PLT_PLABEL | NEED_PLT;
1183
0
    if (bfd_link_pic (info))
1184
0
      need_entry |= NEED_DYNREL;
1185
0
    break;
1186
1187
0
  case R_PARISC_PCREL12F:
1188
0
    htab->has_12bit_branch = 1;
1189
0
    goto branch_common;
1190
1191
0
  case R_PARISC_PCREL17C:
1192
0
  case R_PARISC_PCREL17F:
1193
0
    htab->has_17bit_branch = 1;
1194
0
    goto branch_common;
1195
1196
0
  case R_PARISC_PCREL22F:
1197
0
    htab->has_22bit_branch = 1;
1198
0
  branch_common:
1199
    /* Function calls might need to go through the .plt, and
1200
       might require long branch stubs.  */
1201
0
    if (hh == NULL)
1202
0
      {
1203
        /* We know local syms won't need a .plt entry, and if
1204
     they need a long branch stub we can't guarantee that
1205
     we can reach the stub.  So just flag an error later
1206
     if we're doing a shared link and find we need a long
1207
     branch stub.  */
1208
0
        continue;
1209
0
      }
1210
0
    else
1211
0
      {
1212
        /* Global symbols will need a .plt entry if they remain
1213
     global, and in most cases won't need a long branch
1214
     stub.  Unfortunately, we have to cater for the case
1215
     where a symbol is forced local by versioning, or due
1216
     to symbolic linking, and we lose the .plt entry.  */
1217
0
        need_entry = NEED_PLT;
1218
0
        if (hh->eh.type == STT_PARISC_MILLI)
1219
0
    need_entry = 0;
1220
0
      }
1221
0
    break;
1222
1223
0
  case R_PARISC_SEGBASE:  /* Used to set segment base.  */
1224
0
  case R_PARISC_SEGREL32: /* Relative reloc, used for unwind.  */
1225
0
  case R_PARISC_PCREL14F: /* PC relative load/store.  */
1226
0
  case R_PARISC_PCREL14R:
1227
0
  case R_PARISC_PCREL17R: /* External branches.  */
1228
0
  case R_PARISC_PCREL21L: /* As above, and for load/store too.  */
1229
0
  case R_PARISC_PCREL32:
1230
    /* We don't need to propagate the relocation if linking a
1231
       shared object since these are section relative.  */
1232
0
    continue;
1233
1234
0
  case R_PARISC_DPREL14F: /* Used for gp rel data load/store.  */
1235
0
  case R_PARISC_DPREL14R:
1236
0
  case R_PARISC_DPREL21L:
1237
0
    if (bfd_link_pic (info))
1238
0
      {
1239
0
        _bfd_error_handler
1240
    /* xgettext:c-format */
1241
0
    (_("%pB: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1242
0
     abfd,
1243
0
     elf_hppa_howto_table[r_type].name);
1244
0
        bfd_set_error (bfd_error_bad_value);
1245
0
        return false;
1246
0
      }
1247
    /* Fall through.  */
1248
1249
0
  case R_PARISC_DIR17F: /* Used for external branches.  */
1250
0
  case R_PARISC_DIR17R:
1251
0
  case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
1252
0
  case R_PARISC_DIR14R:
1253
0
  case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
1254
0
  case R_PARISC_DIR32: /* .word relocs.  */
1255
    /* We may want to output a dynamic relocation later.  */
1256
0
    need_entry = NEED_DYNREL;
1257
0
    break;
1258
1259
    /* This relocation describes the C++ object vtable hierarchy.
1260
       Reconstruct it for later use during GC.  */
1261
0
  case R_PARISC_GNU_VTINHERIT:
1262
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
1263
0
      return false;
1264
0
    continue;
1265
1266
    /* This relocation describes which C++ vtable entries are actually
1267
       used.  Record for later use during GC.  */
1268
0
  case R_PARISC_GNU_VTENTRY:
1269
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
1270
0
      return false;
1271
0
    continue;
1272
1273
0
  case R_PARISC_TLS_GD21L:
1274
0
  case R_PARISC_TLS_GD14R:
1275
0
  case R_PARISC_TLS_LDM21L:
1276
0
  case R_PARISC_TLS_LDM14R:
1277
0
    need_entry = NEED_GOT;
1278
0
    break;
1279
1280
0
  case R_PARISC_TLS_IE21L:
1281
0
  case R_PARISC_TLS_IE14R:
1282
0
    if (bfd_link_dll (info))
1283
0
      info->flags |= DF_STATIC_TLS;
1284
0
    need_entry = NEED_GOT;
1285
0
    break;
1286
1287
0
  default:
1288
0
    continue;
1289
0
  }
1290
1291
      /* Now carry out our orders.  */
1292
0
      if (need_entry & NEED_GOT)
1293
0
  {
1294
0
    int tls_type = GOT_NORMAL;
1295
1296
0
    switch (r_type)
1297
0
      {
1298
0
      default:
1299
0
        break;
1300
0
      case R_PARISC_TLS_GD21L:
1301
0
      case R_PARISC_TLS_GD14R:
1302
0
        tls_type = GOT_TLS_GD;
1303
0
        break;
1304
0
      case R_PARISC_TLS_LDM21L:
1305
0
      case R_PARISC_TLS_LDM14R:
1306
0
        tls_type = GOT_TLS_LDM;
1307
0
        break;
1308
0
      case R_PARISC_TLS_IE21L:
1309
0
      case R_PARISC_TLS_IE14R:
1310
0
        tls_type = GOT_TLS_IE;
1311
0
        break;
1312
0
      }
1313
1314
    /* Allocate space for a GOT entry, as well as a dynamic
1315
       relocation for this entry.  */
1316
0
    if (htab->etab.sgot == NULL)
1317
0
      {
1318
0
        if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
1319
0
    return false;
1320
0
      }
1321
1322
0
    if (hh != NULL)
1323
0
      {
1324
0
        if (tls_type == GOT_TLS_LDM)
1325
0
    htab->tls_ldm_got.refcount += 1;
1326
0
        else
1327
0
    hh->eh.got.refcount += 1;
1328
0
        hh->tls_type |= tls_type;
1329
0
      }
1330
0
    else
1331
0
      {
1332
0
        bfd_signed_vma *local_got_refcounts;
1333
1334
        /* This is a global offset table entry for a local symbol.  */
1335
0
        local_got_refcounts = hppa32_elf_local_refcounts (abfd);
1336
0
        if (local_got_refcounts == NULL)
1337
0
    return false;
1338
0
        if (tls_type == GOT_TLS_LDM)
1339
0
    htab->tls_ldm_got.refcount += 1;
1340
0
        else
1341
0
    local_got_refcounts[r_symndx] += 1;
1342
1343
0
        hppa_elf_local_got_tls_type (abfd) [r_symndx] |= tls_type;
1344
0
      }
1345
0
  }
1346
1347
0
      if (need_entry & NEED_PLT)
1348
0
  {
1349
    /* If we are creating a shared library, and this is a reloc
1350
       against a weak symbol or a global symbol in a dynamic
1351
       object, then we will be creating an import stub and a
1352
       .plt entry for the symbol.  Similarly, on a normal link
1353
       to symbols defined in a dynamic object we'll need the
1354
       import stub and a .plt entry.  We don't know yet whether
1355
       the symbol is defined or not, so make an entry anyway and
1356
       clean up later in adjust_dynamic_symbol.  */
1357
0
    if ((sec->flags & SEC_ALLOC) != 0)
1358
0
      {
1359
0
        if (hh != NULL)
1360
0
    {
1361
0
      hh->eh.needs_plt = 1;
1362
0
      hh->eh.plt.refcount += 1;
1363
1364
      /* If this .plt entry is for a plabel, mark it so
1365
         that adjust_dynamic_symbol will keep the entry
1366
         even if it appears to be local.  */
1367
0
      if (need_entry & PLT_PLABEL)
1368
0
        hh->plabel = 1;
1369
0
    }
1370
0
        else if (need_entry & PLT_PLABEL)
1371
0
    {
1372
0
      bfd_signed_vma *local_got_refcounts;
1373
0
      bfd_signed_vma *local_plt_refcounts;
1374
1375
0
      local_got_refcounts = hppa32_elf_local_refcounts (abfd);
1376
0
      if (local_got_refcounts == NULL)
1377
0
        return false;
1378
0
      local_plt_refcounts = (local_got_refcounts
1379
0
           + symtab_hdr->sh_info);
1380
0
      local_plt_refcounts[r_symndx] += 1;
1381
0
    }
1382
0
      }
1383
0
  }
1384
1385
0
      if ((need_entry & NEED_DYNREL) != 0
1386
0
    && (sec->flags & SEC_ALLOC) != 0)
1387
0
  {
1388
    /* Flag this symbol as having a non-got, non-plt reference
1389
       so that we generate copy relocs if it turns out to be
1390
       dynamic.  */
1391
0
    if (hh != NULL)
1392
0
      hh->eh.non_got_ref = 1;
1393
1394
    /* If we are creating a shared library then we need to copy
1395
       the reloc into the shared library.  However, if we are
1396
       linking with -Bsymbolic, we need only copy absolute
1397
       relocs or relocs against symbols that are not defined in
1398
       an object we are including in the link.  PC- or DP- or
1399
       DLT-relative relocs against any local sym or global sym
1400
       with DEF_REGULAR set, can be discarded.  At this point we
1401
       have not seen all the input files, so it is possible that
1402
       DEF_REGULAR is not set now but will be set later (it is
1403
       never cleared).  We account for that possibility below by
1404
       storing information in the dyn_relocs field of the
1405
       hash table entry.
1406
1407
       A similar situation to the -Bsymbolic case occurs when
1408
       creating shared libraries and symbol visibility changes
1409
       render the symbol local.
1410
1411
       As it turns out, all the relocs we will be creating here
1412
       are absolute, so we cannot remove them on -Bsymbolic
1413
       links or visibility changes anyway.  A STUB_REL reloc
1414
       is absolute too, as in that case it is the reloc in the
1415
       stub we will be creating, rather than copying the PCREL
1416
       reloc in the branch.
1417
1418
       If on the other hand, we are creating an executable, we
1419
       may need to keep relocations for symbols satisfied by a
1420
       dynamic library if we manage to avoid copy relocs for the
1421
       symbol.  */
1422
0
    if ((bfd_link_pic (info)
1423
0
         && (IS_ABSOLUTE_RELOC (r_type)
1424
0
       || (hh != NULL
1425
0
           && (!SYMBOLIC_BIND (info, &hh->eh)
1426
0
         || hh->eh.root.type == bfd_link_hash_defweak
1427
0
         || !hh->eh.def_regular))))
1428
0
        || (ELIMINATE_COPY_RELOCS
1429
0
      && !bfd_link_pic (info)
1430
0
      && hh != NULL
1431
0
      && (hh->eh.root.type == bfd_link_hash_defweak
1432
0
          || !hh->eh.def_regular)))
1433
0
      {
1434
0
        struct elf_dyn_relocs *hdh_p;
1435
0
        struct elf_dyn_relocs **hdh_head;
1436
1437
        /* Create a reloc section in dynobj and make room for
1438
     this reloc.  */
1439
0
        if (sreloc == NULL)
1440
0
    {
1441
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
1442
0
        (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ true);
1443
1444
0
      if (sreloc == NULL)
1445
0
        {
1446
0
          bfd_set_error (bfd_error_bad_value);
1447
0
          return false;
1448
0
        }
1449
0
    }
1450
1451
        /* If this is a global symbol, we count the number of
1452
     relocations we need for this symbol.  */
1453
0
        if (hh != NULL)
1454
0
    {
1455
0
      hdh_head = &hh->eh.dyn_relocs;
1456
0
    }
1457
0
        else
1458
0
    {
1459
      /* Track dynamic relocs needed for local syms too.
1460
         We really need local syms available to do this
1461
         easily.  Oh well.  */
1462
0
      asection *sr;
1463
0
      void *vpp;
1464
0
      Elf_Internal_Sym *isym;
1465
1466
0
      isym = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
1467
0
            abfd, r_symndx);
1468
0
      if (isym == NULL)
1469
0
        return false;
1470
1471
0
      sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
1472
0
      if (sr == NULL)
1473
0
        sr = sec;
1474
1475
0
      vpp = &elf_section_data (sr)->local_dynrel;
1476
0
      hdh_head = (struct elf_dyn_relocs **) vpp;
1477
0
    }
1478
1479
0
        hdh_p = *hdh_head;
1480
0
        if (hdh_p == NULL || hdh_p->sec != sec)
1481
0
    {
1482
0
      hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
1483
0
      if (hdh_p == NULL)
1484
0
        return false;
1485
0
      hdh_p->next = *hdh_head;
1486
0
      *hdh_head = hdh_p;
1487
0
      hdh_p->sec = sec;
1488
0
      hdh_p->count = 0;
1489
#if RELATIVE_DYNRELOCS
1490
      hdh_p->pc_count = 0;
1491
#endif
1492
0
    }
1493
1494
0
        hdh_p->count += 1;
1495
#if RELATIVE_DYNRELOCS
1496
        if (!IS_ABSOLUTE_RELOC (rtype))
1497
    hdh_p->pc_count += 1;
1498
#endif
1499
0
      }
1500
0
  }
1501
0
    }
1502
1503
0
  return true;
1504
0
}
1505
1506
/* Return the section that should be marked against garbage collection
1507
   for a given relocation.  */
1508
1509
static asection *
1510
elf32_hppa_gc_mark_hook (asection *sec,
1511
       struct bfd_link_info *info,
1512
       Elf_Internal_Rela *rela,
1513
       struct elf_link_hash_entry *hh,
1514
       Elf_Internal_Sym *sym)
1515
0
{
1516
0
  if (hh != NULL)
1517
0
    switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
1518
0
      {
1519
0
      case R_PARISC_GNU_VTINHERIT:
1520
0
      case R_PARISC_GNU_VTENTRY:
1521
0
  return NULL;
1522
0
      }
1523
1524
0
  return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
1525
0
}
1526
1527
/* Support for core dump NOTE sections.  */
1528
1529
static bool
1530
elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1531
0
{
1532
0
  int offset;
1533
0
  size_t size;
1534
1535
0
  switch (note->descsz)
1536
0
    {
1537
0
      default:
1538
0
  return false;
1539
1540
0
      case 396:   /* Linux/hppa */
1541
  /* pr_cursig */
1542
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1543
1544
  /* pr_pid */
1545
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
1546
1547
  /* pr_reg */
1548
0
  offset = 72;
1549
0
  size = 320;
1550
1551
0
  break;
1552
0
    }
1553
1554
  /* Make a ".reg/999" section.  */
1555
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1556
0
            size, note->descpos + offset);
1557
0
}
1558
1559
static bool
1560
elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1561
0
{
1562
0
  switch (note->descsz)
1563
0
    {
1564
0
      default:
1565
0
  return false;
1566
1567
0
      case 124:   /* Linux/hppa elf_prpsinfo.  */
1568
0
  elf_tdata (abfd)->core->program
1569
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1570
0
  elf_tdata (abfd)->core->command
1571
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1572
0
    }
1573
1574
  /* Note that for some reason, a spurious space is tacked
1575
     onto the end of the args in some (at least one anyway)
1576
     implementations, so strip it off if it exists.  */
1577
0
  {
1578
0
    char *command = elf_tdata (abfd)->core->command;
1579
0
    int n = strlen (command);
1580
1581
0
    if (0 < n && command[n - 1] == ' ')
1582
0
      command[n - 1] = '\0';
1583
0
  }
1584
1585
0
  return true;
1586
0
}
1587
1588
/* Our own version of hide_symbol, so that we can keep plt entries for
1589
   plabels.  */
1590
1591
static void
1592
elf32_hppa_hide_symbol (struct bfd_link_info *info,
1593
      struct elf_link_hash_entry *eh,
1594
      bool force_local)
1595
0
{
1596
0
  if (force_local)
1597
0
    {
1598
0
      eh->forced_local = 1;
1599
0
      if (eh->dynindx != -1)
1600
0
  {
1601
0
    eh->dynindx = -1;
1602
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1603
0
          eh->dynstr_index);
1604
0
  }
1605
1606
      /* PR 16082: Remove version information from hidden symbol.  */
1607
0
      eh->verinfo.verdef = NULL;
1608
0
      eh->verinfo.vertree = NULL;
1609
0
    }
1610
1611
  /* STT_GNU_IFUNC symbol must go through PLT.  */
1612
0
  if (! hppa_elf_hash_entry (eh)->plabel
1613
0
      && eh->type != STT_GNU_IFUNC)
1614
0
    {
1615
0
      eh->needs_plt = 0;
1616
0
      eh->plt = elf_hash_table (info)->init_plt_offset;
1617
0
    }
1618
0
}
1619
1620
/* Return true if we have dynamic relocs against H or any of its weak
1621
   aliases, that apply to read-only sections.  Cannot be used after
1622
   size_dynamic_sections.  */
1623
1624
static bool
1625
alias_readonly_dynrelocs (struct elf_link_hash_entry *eh)
1626
0
{
1627
0
  struct elf32_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1628
0
  do
1629
0
    {
1630
0
      if (_bfd_elf_readonly_dynrelocs (&hh->eh))
1631
0
  return true;
1632
0
      hh = hppa_elf_hash_entry (hh->eh.u.alias);
1633
0
    } while (hh != NULL && &hh->eh != eh);
1634
1635
0
  return false;
1636
0
}
1637
1638
/* Adjust a symbol defined by a dynamic object and referenced by a
1639
   regular object.  The current definition is in some section of the
1640
   dynamic object, but we're not including those sections.  We have to
1641
   change the definition to something the rest of the link can
1642
   understand.  */
1643
1644
static bool
1645
elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1646
          struct elf_link_hash_entry *eh)
1647
0
{
1648
0
  struct elf32_hppa_link_hash_table *htab;
1649
0
  asection *sec, *srel;
1650
1651
  /* If this is a function, put it in the procedure linkage table.  We
1652
     will fill in the contents of the procedure linkage table later.  */
1653
0
  if (eh->type == STT_FUNC
1654
0
      || eh->needs_plt)
1655
0
    {
1656
0
      bool local = (SYMBOL_CALLS_LOCAL (info, eh)
1657
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh));
1658
      /* Discard dyn_relocs when non-pic if we've decided that a
1659
   function symbol is local.  */
1660
0
      if (!bfd_link_pic (info) && local)
1661
0
  eh->dyn_relocs = NULL;
1662
1663
      /* If the symbol is used by a plabel, we must allocate a PLT slot.
1664
   The refcounts are not reliable when it has been hidden since
1665
   hide_symbol can be called before the plabel flag is set.  */
1666
0
      if (hppa_elf_hash_entry (eh)->plabel)
1667
0
  eh->plt.refcount = 1;
1668
1669
      /* Note that unlike some other backends, the refcount is not
1670
   incremented for a non-call (and non-plabel) function reference.  */
1671
0
      else if (eh->plt.refcount <= 0
1672
0
         || local)
1673
0
  {
1674
    /* The .plt entry is not needed when:
1675
       a) Garbage collection has removed all references to the
1676
       symbol, or
1677
       b) We know for certain the symbol is defined in this
1678
       object, and it's not a weak definition, nor is the symbol
1679
       used by a plabel relocation.  Either this object is the
1680
       application or we are doing a shared symbolic link.  */
1681
0
    eh->plt.offset = (bfd_vma) -1;
1682
0
    eh->needs_plt = 0;
1683
0
  }
1684
1685
      /* Unlike other targets, elf32-hppa.c does not define a function
1686
   symbol in a non-pic executable on PLT stub code, so we don't
1687
   have a local definition in that case.  ie. dyn_relocs can't
1688
   be discarded.  */
1689
1690
      /* Function symbols can't have copy relocs.  */
1691
0
      return true;
1692
0
    }
1693
0
  else
1694
0
    eh->plt.offset = (bfd_vma) -1;
1695
1696
0
  htab = hppa_link_hash_table (info);
1697
0
  if (htab == NULL)
1698
0
    return false;
1699
1700
  /* If this is a weak symbol, and there is a real definition, the
1701
     processor independent code will have arranged for us to see the
1702
     real definition first, and we can just use the same value.  */
1703
0
  if (eh->is_weakalias)
1704
0
    {
1705
0
      struct elf_link_hash_entry *def = weakdef (eh);
1706
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1707
0
      eh->root.u.def.section = def->root.u.def.section;
1708
0
      eh->root.u.def.value = def->root.u.def.value;
1709
0
      if (def->root.u.def.section == htab->etab.sdynbss
1710
0
    || def->root.u.def.section == htab->etab.sdynrelro)
1711
0
  eh->dyn_relocs = NULL;
1712
0
      return true;
1713
0
    }
1714
1715
  /* This is a reference to a symbol defined by a dynamic object which
1716
     is not a function.  */
1717
1718
  /* If we are creating a shared library, we must presume that the
1719
     only references to the symbol are via the global offset table.
1720
     For such cases we need not do anything here; the relocations will
1721
     be handled correctly by relocate_section.  */
1722
0
  if (bfd_link_pic (info))
1723
0
    return true;
1724
1725
  /* If there are no references to this symbol that do not use the
1726
     GOT, we don't need to generate a copy reloc.  */
1727
0
  if (!eh->non_got_ref)
1728
0
    return true;
1729
1730
  /* If -z nocopyreloc was given, we won't generate them either.  */
1731
0
  if (info->nocopyreloc)
1732
0
    return true;
1733
1734
  /* If we don't find any dynamic relocs in read-only sections, then
1735
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1736
0
  if (ELIMINATE_COPY_RELOCS
1737
0
      && !alias_readonly_dynrelocs (eh))
1738
0
    return true;
1739
1740
  /* We must allocate the symbol in our .dynbss section, which will
1741
     become part of the .bss section of the executable.  There will be
1742
     an entry for this symbol in the .dynsym section.  The dynamic
1743
     object will contain position independent code, so all references
1744
     from the dynamic object to this symbol will go through the global
1745
     offset table.  The dynamic linker will use the .dynsym entry to
1746
     determine the address it must put in the global offset table, so
1747
     both the dynamic object and the regular object will refer to the
1748
     same memory location for the variable.  */
1749
0
  if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
1750
0
    {
1751
0
      sec = htab->etab.sdynrelro;
1752
0
      srel = htab->etab.sreldynrelro;
1753
0
    }
1754
0
  else
1755
0
    {
1756
0
      sec = htab->etab.sdynbss;
1757
0
      srel = htab->etab.srelbss;
1758
0
    }
1759
0
  if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
1760
0
    {
1761
      /* We must generate a COPY reloc to tell the dynamic linker to
1762
   copy the initial value out of the dynamic object and into the
1763
   runtime process image.  */
1764
0
      srel->size += sizeof (Elf32_External_Rela);
1765
0
      eh->needs_copy = 1;
1766
0
    }
1767
1768
  /* We no longer want dyn_relocs.  */
1769
0
  eh->dyn_relocs = NULL;
1770
0
  return _bfd_elf_adjust_dynamic_copy (info, eh, sec);
1771
0
}
1772
1773
/* If EH is undefined, make it dynamic if that makes sense.  */
1774
1775
static bool
1776
ensure_undef_dynamic (struct bfd_link_info *info,
1777
          struct elf_link_hash_entry *eh)
1778
0
{
1779
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
1780
1781
0
  if (htab->dynamic_sections_created
1782
0
      && (eh->root.type == bfd_link_hash_undefweak
1783
0
    || eh->root.type == bfd_link_hash_undefined)
1784
0
      && eh->dynindx == -1
1785
0
      && !eh->forced_local
1786
0
      && eh->type != STT_PARISC_MILLI
1787
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh)
1788
0
      && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
1789
0
    return bfd_elf_link_record_dynamic_symbol (info, eh);
1790
0
  return true;
1791
0
}
1792
1793
/* Allocate space in the .plt for entries that won't have relocations.
1794
   ie. plabel entries.  */
1795
1796
static bool
1797
allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
1798
0
{
1799
0
  struct bfd_link_info *info;
1800
0
  struct elf32_hppa_link_hash_table *htab;
1801
0
  struct elf32_hppa_link_hash_entry *hh;
1802
0
  asection *sec;
1803
1804
0
  if (eh->root.type == bfd_link_hash_indirect)
1805
0
    return true;
1806
1807
0
  info = (struct bfd_link_info *) inf;
1808
0
  hh = hppa_elf_hash_entry (eh);
1809
0
  htab = hppa_link_hash_table (info);
1810
0
  if (htab == NULL)
1811
0
    return false;
1812
1813
0
  if (htab->etab.dynamic_sections_created
1814
0
      && eh->plt.refcount > 0)
1815
0
    {
1816
0
      if (!ensure_undef_dynamic (info, eh))
1817
0
  return false;
1818
1819
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
1820
0
  {
1821
    /* Allocate these later.  From this point on, h->plabel
1822
       means that the plt entry is only used by a plabel.
1823
       We'll be using a normal plt entry for this symbol, so
1824
       clear the plabel indicator.  */
1825
1826
0
    hh->plabel = 0;
1827
0
  }
1828
0
      else if (hh->plabel)
1829
0
  {
1830
    /* Make an entry in the .plt section for plabel references
1831
       that won't have a .plt entry for other reasons.  */
1832
0
    sec = htab->etab.splt;
1833
0
    eh->plt.offset = sec->size;
1834
0
    sec->size += PLT_ENTRY_SIZE;
1835
0
    if (bfd_link_pic (info))
1836
0
      htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
1837
0
  }
1838
0
      else
1839
0
  {
1840
    /* No .plt entry needed.  */
1841
0
    eh->plt.offset = (bfd_vma) -1;
1842
0
    eh->needs_plt = 0;
1843
0
  }
1844
0
    }
1845
0
  else
1846
0
    {
1847
0
      eh->plt.offset = (bfd_vma) -1;
1848
0
      eh->needs_plt = 0;
1849
0
    }
1850
1851
0
  return true;
1852
0
}
1853
1854
/* Calculate size of GOT entries for symbol given its TLS_TYPE.  */
1855
1856
static inline unsigned int
1857
got_entries_needed (int tls_type)
1858
0
{
1859
0
  unsigned int need = 0;
1860
1861
0
  if ((tls_type & GOT_NORMAL) != 0)
1862
0
    need += GOT_ENTRY_SIZE;
1863
0
  if ((tls_type & GOT_TLS_GD) != 0)
1864
0
    need += GOT_ENTRY_SIZE * 2;
1865
0
  if ((tls_type & GOT_TLS_IE) != 0)
1866
0
    need += GOT_ENTRY_SIZE;
1867
0
  return need;
1868
0
}
1869
1870
/* Calculate size of relocs needed for symbol given its TLS_TYPE and
1871
   NEEDed GOT entries.  TPREL_KNOWN says a TPREL offset can be
1872
   calculated at link time.  DTPREL_KNOWN says the same for a DTPREL
1873
   offset.  */
1874
1875
static inline unsigned int
1876
got_relocs_needed (int tls_type, unsigned int need,
1877
       bool dtprel_known, bool tprel_known)
1878
0
{
1879
  /* All the entries we allocated need relocs.
1880
     Except for GD and IE with local symbols.  */
1881
0
  if ((tls_type & GOT_TLS_GD) != 0 && dtprel_known)
1882
0
    need -= GOT_ENTRY_SIZE;
1883
0
  if ((tls_type & GOT_TLS_IE) != 0 && tprel_known)
1884
0
    need -= GOT_ENTRY_SIZE;
1885
0
  return need * sizeof (Elf32_External_Rela) / GOT_ENTRY_SIZE;
1886
0
}
1887
1888
/* Allocate space in .plt, .got and associated reloc sections for
1889
   global syms.  */
1890
1891
static bool
1892
allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
1893
0
{
1894
0
  struct bfd_link_info *info;
1895
0
  struct elf32_hppa_link_hash_table *htab;
1896
0
  asection *sec;
1897
0
  struct elf32_hppa_link_hash_entry *hh;
1898
0
  struct elf_dyn_relocs *hdh_p;
1899
1900
0
  if (eh->root.type == bfd_link_hash_indirect)
1901
0
    return true;
1902
1903
0
  info = inf;
1904
0
  htab = hppa_link_hash_table (info);
1905
0
  if (htab == NULL)
1906
0
    return false;
1907
1908
0
  hh = hppa_elf_hash_entry (eh);
1909
1910
0
  if (htab->etab.dynamic_sections_created
1911
0
      && eh->plt.offset != (bfd_vma) -1
1912
0
      && !hh->plabel
1913
0
      && eh->plt.refcount > 0)
1914
0
    {
1915
      /* Make an entry in the .plt section.  */
1916
0
      sec = htab->etab.splt;
1917
0
      eh->plt.offset = sec->size;
1918
0
      sec->size += PLT_ENTRY_SIZE;
1919
1920
      /* We also need to make an entry in the .rela.plt section.  */
1921
0
      htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
1922
0
      htab->need_plt_stub = 1;
1923
0
    }
1924
1925
0
  if (eh->got.refcount > 0)
1926
0
    {
1927
0
      unsigned int need;
1928
1929
0
      if (!ensure_undef_dynamic (info, eh))
1930
0
  return false;
1931
1932
0
      sec = htab->etab.sgot;
1933
0
      eh->got.offset = sec->size;
1934
0
      need = got_entries_needed (hh->tls_type);
1935
0
      sec->size += need;
1936
0
      if (htab->etab.dynamic_sections_created
1937
0
    && (bfd_link_dll (info)
1938
0
        || (bfd_link_pic (info) && (hh->tls_type & GOT_NORMAL) != 0)
1939
0
        || (eh->dynindx != -1
1940
0
      && !SYMBOL_REFERENCES_LOCAL (info, eh)))
1941
0
    && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
1942
0
  {
1943
0
    bool local = SYMBOL_REFERENCES_LOCAL (info, eh);
1944
0
    htab->etab.srelgot->size
1945
0
      += got_relocs_needed (hh->tls_type, need, local,
1946
0
          local && bfd_link_executable (info));
1947
0
  }
1948
0
    }
1949
0
  else
1950
0
    eh->got.offset = (bfd_vma) -1;
1951
1952
  /* If no dynamic sections we can't have dynamic relocs.  */
1953
0
  if (!htab->etab.dynamic_sections_created)
1954
0
    eh->dyn_relocs = NULL;
1955
1956
  /* Discard relocs on undefined syms with non-default visibility.  */
1957
0
  else if ((eh->root.type == bfd_link_hash_undefined
1958
0
      && ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
1959
0
     || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
1960
0
    eh->dyn_relocs = NULL;
1961
1962
0
  if (eh->dyn_relocs == NULL)
1963
0
    return true;
1964
1965
  /* If this is a -Bsymbolic shared link, then we need to discard all
1966
     space allocated for dynamic pc-relative relocs against symbols
1967
     defined in a regular object.  For the normal shared case, discard
1968
     space for relocs that have become local due to symbol visibility
1969
     changes.  */
1970
0
  if (bfd_link_pic (info))
1971
0
    {
1972
#if RELATIVE_DYNRELOCS
1973
      if (SYMBOL_CALLS_LOCAL (info, eh))
1974
  {
1975
    struct elf_dyn_relocs **hdh_pp;
1976
1977
    for (hdh_pp = &eh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
1978
      {
1979
        hdh_p->count -= hdh_p->pc_count;
1980
        hdh_p->pc_count = 0;
1981
        if (hdh_p->count == 0)
1982
    *hdh_pp = hdh_p->next;
1983
        else
1984
    hdh_pp = &hdh_p->next;
1985
      }
1986
  }
1987
#endif
1988
1989
0
      if (eh->dyn_relocs != NULL)
1990
0
  {
1991
0
    if (!ensure_undef_dynamic (info, eh))
1992
0
      return false;
1993
0
  }
1994
0
    }
1995
0
  else if (ELIMINATE_COPY_RELOCS)
1996
0
    {
1997
      /* For the non-shared case, discard space for relocs against
1998
   symbols which turn out to need copy relocs or are not
1999
   dynamic.  */
2000
2001
0
      if (eh->dynamic_adjusted
2002
0
    && !eh->def_regular
2003
0
    && !ELF_COMMON_DEF_P (eh))
2004
0
  {
2005
0
    if (!ensure_undef_dynamic (info, eh))
2006
0
      return false;
2007
2008
0
    if (eh->dynindx == -1)
2009
0
      eh->dyn_relocs = NULL;
2010
0
  }
2011
0
      else
2012
0
  eh->dyn_relocs = NULL;
2013
0
    }
2014
2015
  /* Finally, allocate space.  */
2016
0
  for (hdh_p = eh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->next)
2017
0
    {
2018
0
      asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2019
0
      sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2020
0
    }
2021
2022
0
  return true;
2023
0
}
2024
2025
/* This function is called via elf_link_hash_traverse to force
2026
   millicode symbols local so they do not end up as globals in the
2027
   dynamic symbol table.  We ought to be able to do this in
2028
   adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2029
   for all dynamic symbols.  Arguably, this is a bug in
2030
   elf_adjust_dynamic_symbol.  */
2031
2032
static bool
2033
clobber_millicode_symbols (struct elf_link_hash_entry *eh,
2034
         void *info)
2035
0
{
2036
0
  if (eh->type == STT_PARISC_MILLI
2037
0
      && !eh->forced_local)
2038
0
    elf32_hppa_hide_symbol ((struct bfd_link_info *) info, eh, true);
2039
0
  return true;
2040
0
}
2041
2042
/* Set the sizes of the dynamic sections.  */
2043
2044
static bool
2045
elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2046
          struct bfd_link_info *info)
2047
0
{
2048
0
  struct elf32_hppa_link_hash_table *htab;
2049
0
  bfd *dynobj;
2050
0
  bfd *ibfd;
2051
0
  asection *sec;
2052
0
  bool relocs;
2053
2054
0
  htab = hppa_link_hash_table (info);
2055
0
  if (htab == NULL)
2056
0
    return false;
2057
2058
0
  dynobj = htab->etab.dynobj;
2059
0
  if (dynobj == NULL)
2060
0
    abort ();
2061
2062
0
  if (htab->etab.dynamic_sections_created)
2063
0
    {
2064
      /* Set the contents of the .interp section to the interpreter.  */
2065
0
      if (bfd_link_executable (info) && !info->nointerp)
2066
0
  {
2067
0
    sec = bfd_get_linker_section (dynobj, ".interp");
2068
0
    if (sec == NULL)
2069
0
      abort ();
2070
0
    sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
2071
0
    sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2072
0
  }
2073
2074
      /* Force millicode symbols local.  */
2075
0
      elf_link_hash_traverse (&htab->etab,
2076
0
            clobber_millicode_symbols,
2077
0
            info);
2078
0
    }
2079
2080
  /* Set up .got and .plt offsets for local syms, and space for local
2081
     dynamic relocs.  */
2082
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2083
0
    {
2084
0
      bfd_signed_vma *local_got;
2085
0
      bfd_signed_vma *end_local_got;
2086
0
      bfd_signed_vma *local_plt;
2087
0
      bfd_signed_vma *end_local_plt;
2088
0
      bfd_size_type locsymcount;
2089
0
      Elf_Internal_Shdr *symtab_hdr;
2090
0
      asection *srel;
2091
0
      char *local_tls_type;
2092
2093
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2094
0
  continue;
2095
2096
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2097
0
  {
2098
0
    struct elf_dyn_relocs *hdh_p;
2099
2100
0
    for (hdh_p = ((struct elf_dyn_relocs *)
2101
0
        elf_section_data (sec)->local_dynrel);
2102
0
         hdh_p != NULL;
2103
0
         hdh_p = hdh_p->next)
2104
0
      {
2105
0
        if (!bfd_is_abs_section (hdh_p->sec)
2106
0
      && bfd_is_abs_section (hdh_p->sec->output_section))
2107
0
    {
2108
      /* Input section has been discarded, either because
2109
         it is a copy of a linkonce section or due to
2110
         linker script /DISCARD/, so we'll be discarding
2111
         the relocs too.  */
2112
0
    }
2113
0
        else if (hdh_p->count != 0)
2114
0
    {
2115
0
      srel = elf_section_data (hdh_p->sec)->sreloc;
2116
0
      srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2117
0
      if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2118
0
        info->flags |= DF_TEXTREL;
2119
0
    }
2120
0
      }
2121
0
  }
2122
2123
0
      local_got = elf_local_got_refcounts (ibfd);
2124
0
      if (!local_got)
2125
0
  continue;
2126
2127
0
      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2128
0
      locsymcount = symtab_hdr->sh_info;
2129
0
      end_local_got = local_got + locsymcount;
2130
0
      local_tls_type = hppa_elf_local_got_tls_type (ibfd);
2131
0
      sec = htab->etab.sgot;
2132
0
      srel = htab->etab.srelgot;
2133
0
      for (; local_got < end_local_got; ++local_got)
2134
0
  {
2135
0
    if (*local_got > 0)
2136
0
      {
2137
0
        unsigned int need;
2138
2139
0
        *local_got = sec->size;
2140
0
        need = got_entries_needed (*local_tls_type);
2141
0
        sec->size += need;
2142
0
        if (bfd_link_dll (info)
2143
0
      || (bfd_link_pic (info)
2144
0
          && (*local_tls_type & GOT_NORMAL) != 0))
2145
0
    htab->etab.srelgot->size
2146
0
      += got_relocs_needed (*local_tls_type, need, true,
2147
0
          bfd_link_executable (info));
2148
0
      }
2149
0
    else
2150
0
      *local_got = (bfd_vma) -1;
2151
2152
0
    ++local_tls_type;
2153
0
  }
2154
2155
0
      local_plt = end_local_got;
2156
0
      end_local_plt = local_plt + locsymcount;
2157
0
      if (! htab->etab.dynamic_sections_created)
2158
0
  {
2159
    /* Won't be used, but be safe.  */
2160
0
    for (; local_plt < end_local_plt; ++local_plt)
2161
0
      *local_plt = (bfd_vma) -1;
2162
0
  }
2163
0
      else
2164
0
  {
2165
0
    sec = htab->etab.splt;
2166
0
    srel = htab->etab.srelplt;
2167
0
    for (; local_plt < end_local_plt; ++local_plt)
2168
0
      {
2169
0
        if (*local_plt > 0)
2170
0
    {
2171
0
      *local_plt = sec->size;
2172
0
      sec->size += PLT_ENTRY_SIZE;
2173
0
      if (bfd_link_pic (info))
2174
0
        srel->size += sizeof (Elf32_External_Rela);
2175
0
    }
2176
0
        else
2177
0
    *local_plt = (bfd_vma) -1;
2178
0
      }
2179
0
  }
2180
0
    }
2181
2182
0
  if (htab->tls_ldm_got.refcount > 0)
2183
0
    {
2184
      /* Allocate 2 got entries and 1 dynamic reloc for
2185
   R_PARISC_TLS_DTPMOD32 relocs.  */
2186
0
      htab->tls_ldm_got.offset = htab->etab.sgot->size;
2187
0
      htab->etab.sgot->size += (GOT_ENTRY_SIZE * 2);
2188
0
      htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2189
0
    }
2190
0
  else
2191
0
    htab->tls_ldm_got.offset = -1;
2192
2193
  /* Do all the .plt entries without relocs first.  The dynamic linker
2194
     uses the last .plt reloc to find the end of the .plt (and hence
2195
     the start of the .got) for lazy linking.  */
2196
0
  elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
2197
2198
  /* Allocate global sym .plt and .got entries, and space for global
2199
     sym dynamic relocs.  */
2200
0
  elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2201
2202
  /* The check_relocs and adjust_dynamic_symbol entry points have
2203
     determined the sizes of the various dynamic sections.  Allocate
2204
     memory for them.  */
2205
0
  relocs = false;
2206
0
  for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2207
0
    {
2208
0
      if ((sec->flags & SEC_LINKER_CREATED) == 0)
2209
0
  continue;
2210
2211
0
      if (sec == htab->etab.splt)
2212
0
  {
2213
0
    if (htab->need_plt_stub)
2214
0
      {
2215
        /* Make space for the plt stub at the end of the .plt
2216
     section.  We want this stub right at the end, up
2217
     against the .got section.  */
2218
0
        int gotalign = bfd_section_alignment (htab->etab.sgot);
2219
0
        int pltalign = bfd_section_alignment (sec);
2220
0
        int align = gotalign > 3 ? gotalign : 3;
2221
0
        bfd_size_type mask;
2222
2223
0
        if (align > pltalign)
2224
0
    bfd_set_section_alignment (sec, align);
2225
0
        mask = ((bfd_size_type) 1 << gotalign) - 1;
2226
0
        sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
2227
0
      }
2228
0
  }
2229
0
      else if (sec == htab->etab.sgot
2230
0
         || sec == htab->etab.sdynbss
2231
0
         || sec == htab->etab.sdynrelro)
2232
0
  ;
2233
0
      else if (startswith (bfd_section_name (sec), ".rela"))
2234
0
  {
2235
0
    if (sec->size != 0)
2236
0
      {
2237
        /* Remember whether there are any reloc sections other
2238
     than .rela.plt.  */
2239
0
        if (sec != htab->etab.srelplt)
2240
0
    relocs = true;
2241
2242
        /* We use the reloc_count field as a counter if we need
2243
     to copy relocs into the output file.  */
2244
0
        sec->reloc_count = 0;
2245
0
      }
2246
0
  }
2247
0
      else
2248
0
  {
2249
    /* It's not one of our sections, so don't allocate space.  */
2250
0
    continue;
2251
0
  }
2252
2253
0
      if (sec->size == 0)
2254
0
  {
2255
    /* If we don't need this section, strip it from the
2256
       output file.  This is mostly to handle .rela.bss and
2257
       .rela.plt.  We must create both sections in
2258
       create_dynamic_sections, because they must be created
2259
       before the linker maps input sections to output
2260
       sections.  The linker does that before
2261
       adjust_dynamic_symbol is called, and it is that
2262
       function which decides whether anything needs to go
2263
       into these sections.  */
2264
0
    sec->flags |= SEC_EXCLUDE;
2265
0
    continue;
2266
0
  }
2267
2268
0
      if ((sec->flags & SEC_HAS_CONTENTS) == 0)
2269
0
  continue;
2270
2271
      /* Allocate memory for the section contents.  Zero it, because
2272
   we may not fill in all the reloc sections.  */
2273
0
      sec->contents = bfd_zalloc (dynobj, sec->size);
2274
0
      if (sec->contents == NULL)
2275
0
  return false;
2276
0
    }
2277
2278
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
2279
0
}
2280
2281
/* External entry points for sizing and building linker stubs.  */
2282
2283
/* Set up various things so that we can make a list of input sections
2284
   for each output section included in the link.  Returns -1 on error,
2285
   0 when no stubs will be needed, and 1 on success.  */
2286
2287
int
2288
elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2289
0
{
2290
0
  bfd *input_bfd;
2291
0
  unsigned int bfd_count;
2292
0
  unsigned int top_id, top_index;
2293
0
  asection *section;
2294
0
  asection **input_list, **list;
2295
0
  size_t amt;
2296
0
  struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2297
2298
0
  if (htab == NULL)
2299
0
    return -1;
2300
2301
  /* Count the number of input BFDs and find the top input section id.  */
2302
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2303
0
       input_bfd != NULL;
2304
0
       input_bfd = input_bfd->link.next)
2305
0
    {
2306
0
      bfd_count += 1;
2307
0
      for (section = input_bfd->sections;
2308
0
     section != NULL;
2309
0
     section = section->next)
2310
0
  {
2311
0
    if (top_id < section->id)
2312
0
      top_id = section->id;
2313
0
  }
2314
0
    }
2315
0
  htab->bfd_count = bfd_count;
2316
2317
0
  amt = sizeof (struct map_stub) * (top_id + 1);
2318
0
  htab->stub_group = bfd_zmalloc (amt);
2319
0
  if (htab->stub_group == NULL)
2320
0
    return -1;
2321
2322
  /* We can't use output_bfd->section_count here to find the top output
2323
     section index as some sections may have been removed, and
2324
     strip_excluded_output_sections doesn't renumber the indices.  */
2325
0
  for (section = output_bfd->sections, top_index = 0;
2326
0
       section != NULL;
2327
0
       section = section->next)
2328
0
    {
2329
0
      if (top_index < section->index)
2330
0
  top_index = section->index;
2331
0
    }
2332
2333
0
  htab->top_index = top_index;
2334
0
  amt = sizeof (asection *) * (top_index + 1);
2335
0
  input_list = bfd_malloc (amt);
2336
0
  htab->input_list = input_list;
2337
0
  if (input_list == NULL)
2338
0
    return -1;
2339
2340
  /* For sections we aren't interested in, mark their entries with a
2341
     value we can check later.  */
2342
0
  list = input_list + top_index;
2343
0
  do
2344
0
    *list = bfd_abs_section_ptr;
2345
0
  while (list-- != input_list);
2346
2347
0
  for (section = output_bfd->sections;
2348
0
       section != NULL;
2349
0
       section = section->next)
2350
0
    {
2351
0
      if ((section->flags & SEC_CODE) != 0)
2352
0
  input_list[section->index] = NULL;
2353
0
    }
2354
2355
0
  return 1;
2356
0
}
2357
2358
/* The linker repeatedly calls this function for each input section,
2359
   in the order that input sections are linked into output sections.
2360
   Build lists of input sections to determine groupings between which
2361
   we may insert linker stubs.  */
2362
2363
void
2364
elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2365
0
{
2366
0
  struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2367
2368
0
  if (htab == NULL)
2369
0
    return;
2370
2371
0
  if (isec->output_section->index <= htab->top_index)
2372
0
    {
2373
0
      asection **list = htab->input_list + isec->output_section->index;
2374
0
      if (*list != bfd_abs_section_ptr)
2375
0
  {
2376
    /* Steal the link_sec pointer for our list.  */
2377
0
#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2378
    /* This happens to make the list in reverse order,
2379
       which is what we want.  */
2380
0
    PREV_SEC (isec) = *list;
2381
0
    *list = isec;
2382
0
  }
2383
0
    }
2384
0
}
2385
2386
/* See whether we can group stub sections together.  Grouping stub
2387
   sections may result in fewer stubs.  More importantly, we need to
2388
   put all .init* and .fini* stubs at the beginning of the .init or
2389
   .fini output sections respectively, because glibc splits the
2390
   _init and _fini functions into multiple parts.  Putting a stub in
2391
   the middle of a function is not a good idea.  */
2392
2393
static void
2394
group_sections (struct elf32_hppa_link_hash_table *htab,
2395
    bfd_size_type stub_group_size,
2396
    bool stubs_always_before_branch)
2397
0
{
2398
0
  asection **list = htab->input_list + htab->top_index;
2399
0
  do
2400
0
    {
2401
0
      asection *tail = *list;
2402
0
      if (tail == bfd_abs_section_ptr)
2403
0
  continue;
2404
0
      while (tail != NULL)
2405
0
  {
2406
0
    asection *curr;
2407
0
    asection *prev;
2408
0
    bfd_size_type total;
2409
0
    bool big_sec;
2410
2411
0
    curr = tail;
2412
0
    total = tail->size;
2413
0
    big_sec = total >= stub_group_size;
2414
2415
0
    while ((prev = PREV_SEC (curr)) != NULL
2416
0
     && ((total += curr->output_offset - prev->output_offset)
2417
0
         < stub_group_size))
2418
0
      curr = prev;
2419
2420
    /* OK, the size from the start of CURR to the end is less
2421
       than 240000 bytes and thus can be handled by one stub
2422
       section.  (or the tail section is itself larger than
2423
       240000 bytes, in which case we may be toast.)
2424
       We should really be keeping track of the total size of
2425
       stubs added here, as stubs contribute to the final output
2426
       section size.  That's a little tricky, and this way will
2427
       only break if stubs added total more than 22144 bytes, or
2428
       2768 long branch stubs.  It seems unlikely for more than
2429
       2768 different functions to be called, especially from
2430
       code only 240000 bytes long.  This limit used to be
2431
       250000, but c++ code tends to generate lots of little
2432
       functions, and sometimes violated the assumption.  */
2433
0
    do
2434
0
      {
2435
0
        prev = PREV_SEC (tail);
2436
        /* Set up this stub group.  */
2437
0
        htab->stub_group[tail->id].link_sec = curr;
2438
0
      }
2439
0
    while (tail != curr && (tail = prev) != NULL);
2440
2441
    /* But wait, there's more!  Input sections up to 240000
2442
       bytes before the stub section can be handled by it too.
2443
       Don't do this if we have a really large section after the
2444
       stubs, as adding more stubs increases the chance that
2445
       branches may not reach into the stub section.  */
2446
0
    if (!stubs_always_before_branch && !big_sec)
2447
0
      {
2448
0
        total = 0;
2449
0
        while (prev != NULL
2450
0
         && ((total += tail->output_offset - prev->output_offset)
2451
0
       < stub_group_size))
2452
0
    {
2453
0
      tail = prev;
2454
0
      prev = PREV_SEC (tail);
2455
0
      htab->stub_group[tail->id].link_sec = curr;
2456
0
    }
2457
0
      }
2458
0
    tail = prev;
2459
0
  }
2460
0
    }
2461
0
  while (list-- != htab->input_list);
2462
0
  free (htab->input_list);
2463
0
#undef PREV_SEC
2464
0
}
2465
2466
/* Read in all local syms for all input bfds, and create hash entries
2467
   for export stubs if we are building a multi-subspace shared lib.
2468
   Returns -1 on error, 1 if export stubs created, 0 otherwise.  */
2469
2470
static int
2471
get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2472
0
{
2473
0
  unsigned int bfd_indx;
2474
0
  Elf_Internal_Sym *local_syms, **all_local_syms;
2475
0
  int stub_changed = 0;
2476
0
  struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2477
2478
0
  if (htab == NULL)
2479
0
    return -1;
2480
2481
  /* We want to read in symbol extension records only once.  To do this
2482
     we need to read in the local symbols in parallel and save them for
2483
     later use; so hold pointers to the local symbols in an array.  */
2484
0
  size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2485
0
  all_local_syms = bfd_zmalloc (amt);
2486
0
  htab->all_local_syms = all_local_syms;
2487
0
  if (all_local_syms == NULL)
2488
0
    return -1;
2489
2490
  /* Walk over all the input BFDs, swapping in local symbols.
2491
     If we are creating a shared library, create hash entries for the
2492
     export stubs.  */
2493
0
  for (bfd_indx = 0;
2494
0
       input_bfd != NULL;
2495
0
       input_bfd = input_bfd->link.next, bfd_indx++)
2496
0
    {
2497
0
      Elf_Internal_Shdr *symtab_hdr;
2498
2499
      /* We'll need the symbol table in a second.  */
2500
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2501
0
      if (symtab_hdr->sh_info == 0)
2502
0
  continue;
2503
2504
      /* We need an array of the local symbols attached to the input bfd.  */
2505
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2506
0
      if (local_syms == NULL)
2507
0
  {
2508
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2509
0
               symtab_hdr->sh_info, 0,
2510
0
               NULL, NULL, NULL);
2511
    /* Cache them for elf_link_input_bfd.  */
2512
0
    symtab_hdr->contents = (unsigned char *) local_syms;
2513
0
  }
2514
0
      if (local_syms == NULL)
2515
0
  return -1;
2516
2517
0
      all_local_syms[bfd_indx] = local_syms;
2518
2519
0
      if (bfd_link_pic (info) && htab->multi_subspace)
2520
0
  {
2521
0
    struct elf_link_hash_entry **eh_syms;
2522
0
    struct elf_link_hash_entry **eh_symend;
2523
0
    unsigned int symcount;
2524
2525
0
    symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2526
0
          - symtab_hdr->sh_info);
2527
0
    eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
2528
0
    eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
2529
2530
    /* Look through the global syms for functions;  We need to
2531
       build export stubs for all globally visible functions.  */
2532
0
    for (; eh_syms < eh_symend; eh_syms++)
2533
0
      {
2534
0
        struct elf32_hppa_link_hash_entry *hh;
2535
2536
0
        hh = hppa_elf_hash_entry (*eh_syms);
2537
2538
0
        while (hh->eh.root.type == bfd_link_hash_indirect
2539
0
         || hh->eh.root.type == bfd_link_hash_warning)
2540
0
       hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2541
2542
        /* At this point in the link, undefined syms have been
2543
     resolved, so we need to check that the symbol was
2544
     defined in this BFD.  */
2545
0
        if ((hh->eh.root.type == bfd_link_hash_defined
2546
0
       || hh->eh.root.type == bfd_link_hash_defweak)
2547
0
      && hh->eh.type == STT_FUNC
2548
0
      && hh->eh.root.u.def.section->output_section != NULL
2549
0
      && (hh->eh.root.u.def.section->output_section->owner
2550
0
          == output_bfd)
2551
0
      && hh->eh.root.u.def.section->owner == input_bfd
2552
0
      && hh->eh.def_regular
2553
0
      && !hh->eh.forced_local
2554
0
      && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
2555
0
    {
2556
0
      asection *sec;
2557
0
      const char *stub_name;
2558
0
      struct elf32_hppa_stub_hash_entry *hsh;
2559
2560
0
      sec = hh->eh.root.u.def.section;
2561
0
      stub_name = hh_name (hh);
2562
0
      hsh = hppa_stub_hash_lookup (&htab->bstab,
2563
0
                  stub_name,
2564
0
                  false, false);
2565
0
      if (hsh == NULL)
2566
0
        {
2567
0
          hsh = hppa_add_stub (stub_name, sec, htab);
2568
0
          if (!hsh)
2569
0
      return -1;
2570
2571
0
          hsh->target_value = hh->eh.root.u.def.value;
2572
0
          hsh->target_section = hh->eh.root.u.def.section;
2573
0
          hsh->stub_type = hppa_stub_export;
2574
0
          hsh->hh = hh;
2575
0
          stub_changed = 1;
2576
0
        }
2577
0
      else
2578
0
        {
2579
          /* xgettext:c-format */
2580
0
          _bfd_error_handler (_("%pB: duplicate export stub %s"),
2581
0
            input_bfd, stub_name);
2582
0
        }
2583
0
    }
2584
0
      }
2585
0
  }
2586
0
    }
2587
2588
0
  return stub_changed;
2589
0
}
2590
2591
/* Determine and set the size of the stub section for a final link.
2592
2593
   The basic idea here is to examine all the relocations looking for
2594
   PC-relative calls to a target that is unreachable with a "bl"
2595
   instruction.  */
2596
2597
bool
2598
elf32_hppa_size_stubs
2599
  (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2600
   bool multi_subspace, bfd_signed_vma group_size,
2601
   asection * (*add_stub_section) (const char *, asection *),
2602
   void (*layout_sections_again) (void))
2603
0
{
2604
0
  bfd_size_type stub_group_size;
2605
0
  bool stubs_always_before_branch;
2606
0
  bool stub_changed;
2607
0
  struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2608
2609
0
  if (htab == NULL)
2610
0
    return false;
2611
2612
  /* Stash our params away.  */
2613
0
  htab->stub_bfd = stub_bfd;
2614
0
  htab->multi_subspace = multi_subspace;
2615
0
  htab->add_stub_section = add_stub_section;
2616
0
  htab->layout_sections_again = layout_sections_again;
2617
0
  stubs_always_before_branch = group_size < 0;
2618
0
  if (group_size < 0)
2619
0
    stub_group_size = -group_size;
2620
0
  else
2621
0
    stub_group_size = group_size;
2622
0
  if (stub_group_size == 1)
2623
0
    {
2624
      /* Default values.  */
2625
0
      if (stubs_always_before_branch)
2626
0
  {
2627
0
    stub_group_size = 7680000;
2628
0
    if (htab->has_17bit_branch || htab->multi_subspace)
2629
0
      stub_group_size = 240000;
2630
0
    if (htab->has_12bit_branch)
2631
0
      stub_group_size = 7500;
2632
0
  }
2633
0
      else
2634
0
  {
2635
0
    stub_group_size = 6971392;
2636
0
    if (htab->has_17bit_branch || htab->multi_subspace)
2637
0
      stub_group_size = 217856;
2638
0
    if (htab->has_12bit_branch)
2639
0
      stub_group_size = 6808;
2640
0
  }
2641
0
    }
2642
2643
0
  group_sections (htab, stub_group_size, stubs_always_before_branch);
2644
2645
0
  switch (get_local_syms (output_bfd, info->input_bfds, info))
2646
0
    {
2647
0
    default:
2648
0
      if (htab->all_local_syms)
2649
0
  goto error_ret_free_local;
2650
0
      return false;
2651
2652
0
    case 0:
2653
0
      stub_changed = false;
2654
0
      break;
2655
2656
0
    case 1:
2657
0
      stub_changed = true;
2658
0
      break;
2659
0
    }
2660
2661
0
  while (1)
2662
0
    {
2663
0
      bfd *input_bfd;
2664
0
      unsigned int bfd_indx;
2665
0
      asection *stub_sec;
2666
2667
0
      for (input_bfd = info->input_bfds, bfd_indx = 0;
2668
0
     input_bfd != NULL;
2669
0
     input_bfd = input_bfd->link.next, bfd_indx++)
2670
0
  {
2671
0
    Elf_Internal_Shdr *symtab_hdr;
2672
0
    asection *section;
2673
0
    Elf_Internal_Sym *local_syms;
2674
2675
    /* We'll need the symbol table in a second.  */
2676
0
    symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2677
0
    if (symtab_hdr->sh_info == 0)
2678
0
      continue;
2679
2680
0
    local_syms = htab->all_local_syms[bfd_indx];
2681
2682
    /* Walk over each section attached to the input bfd.  */
2683
0
    for (section = input_bfd->sections;
2684
0
         section != NULL;
2685
0
         section = section->next)
2686
0
      {
2687
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2688
2689
        /* If there aren't any relocs, then there's nothing more
2690
     to do.  */
2691
0
        if ((section->flags & SEC_RELOC) == 0
2692
0
      || (section->flags & SEC_ALLOC) == 0
2693
0
      || (section->flags & SEC_LOAD) == 0
2694
0
      || (section->flags & SEC_CODE) == 0
2695
0
      || section->reloc_count == 0)
2696
0
    continue;
2697
2698
        /* If this section is a link-once section that will be
2699
     discarded, then don't create any stubs.  */
2700
0
        if (section->output_section == NULL
2701
0
      || section->output_section->owner != output_bfd)
2702
0
    continue;
2703
2704
        /* Get the relocs.  */
2705
0
        internal_relocs
2706
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2707
0
               info->keep_memory);
2708
0
        if (internal_relocs == NULL)
2709
0
    goto error_ret_free_local;
2710
2711
        /* Now examine each relocation.  */
2712
0
        irela = internal_relocs;
2713
0
        irelaend = irela + section->reloc_count;
2714
0
        for (; irela < irelaend; irela++)
2715
0
    {
2716
0
      unsigned int r_type, r_indx;
2717
0
      enum elf32_hppa_stub_type stub_type;
2718
0
      struct elf32_hppa_stub_hash_entry *hsh;
2719
0
      asection *sym_sec;
2720
0
      bfd_vma sym_value;
2721
0
      bfd_vma destination;
2722
0
      struct elf32_hppa_link_hash_entry *hh;
2723
0
      char *stub_name;
2724
0
      const asection *id_sec;
2725
2726
0
      r_type = ELF32_R_TYPE (irela->r_info);
2727
0
      r_indx = ELF32_R_SYM (irela->r_info);
2728
2729
0
      if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2730
0
        {
2731
0
          bfd_set_error (bfd_error_bad_value);
2732
0
        error_ret_free_internal:
2733
0
          if (elf_section_data (section)->relocs == NULL)
2734
0
      free (internal_relocs);
2735
0
          goto error_ret_free_local;
2736
0
        }
2737
2738
      /* Only look for stubs on call instructions.  */
2739
0
      if (r_type != (unsigned int) R_PARISC_PCREL12F
2740
0
          && r_type != (unsigned int) R_PARISC_PCREL17F
2741
0
          && r_type != (unsigned int) R_PARISC_PCREL22F)
2742
0
        continue;
2743
2744
      /* Now determine the call target, its name, value,
2745
         section.  */
2746
0
      sym_sec = NULL;
2747
0
      sym_value = 0;
2748
0
      destination = -1;
2749
0
      hh = NULL;
2750
0
      if (r_indx < symtab_hdr->sh_info)
2751
0
        {
2752
          /* It's a local symbol.  */
2753
0
          Elf_Internal_Sym *sym;
2754
0
          Elf_Internal_Shdr *hdr;
2755
0
          unsigned int shndx;
2756
2757
0
          sym = local_syms + r_indx;
2758
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2759
0
      sym_value = sym->st_value;
2760
0
          shndx = sym->st_shndx;
2761
0
          if (shndx < elf_numsections (input_bfd))
2762
0
      {
2763
0
        hdr = elf_elfsections (input_bfd)[shndx];
2764
0
        sym_sec = hdr->bfd_section;
2765
0
        destination = (sym_value + irela->r_addend
2766
0
           + sym_sec->output_offset
2767
0
           + sym_sec->output_section->vma);
2768
0
      }
2769
0
        }
2770
0
      else
2771
0
        {
2772
          /* It's an external symbol.  */
2773
0
          int e_indx;
2774
2775
0
          e_indx = r_indx - symtab_hdr->sh_info;
2776
0
          hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
2777
2778
0
          while (hh->eh.root.type == bfd_link_hash_indirect
2779
0
           || hh->eh.root.type == bfd_link_hash_warning)
2780
0
      hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2781
2782
0
          if (hh->eh.root.type == bfd_link_hash_defined
2783
0
        || hh->eh.root.type == bfd_link_hash_defweak)
2784
0
      {
2785
0
        sym_sec = hh->eh.root.u.def.section;
2786
0
        sym_value = hh->eh.root.u.def.value;
2787
0
        if (sym_sec->output_section != NULL)
2788
0
          destination = (sym_value + irela->r_addend
2789
0
             + sym_sec->output_offset
2790
0
             + sym_sec->output_section->vma);
2791
0
      }
2792
0
          else if (hh->eh.root.type == bfd_link_hash_undefweak)
2793
0
      {
2794
0
        if (! bfd_link_pic (info))
2795
0
          continue;
2796
0
      }
2797
0
          else if (hh->eh.root.type == bfd_link_hash_undefined)
2798
0
      {
2799
0
        if (! (info->unresolved_syms_in_objects == RM_IGNORE
2800
0
         && (ELF_ST_VISIBILITY (hh->eh.other)
2801
0
             == STV_DEFAULT)
2802
0
         && hh->eh.type != STT_PARISC_MILLI))
2803
0
          continue;
2804
0
      }
2805
0
          else
2806
0
      {
2807
0
        bfd_set_error (bfd_error_bad_value);
2808
0
        goto error_ret_free_internal;
2809
0
      }
2810
0
        }
2811
2812
      /* Determine what (if any) linker stub is needed.  */
2813
0
      stub_type = hppa_type_of_stub (section, irela, hh,
2814
0
             destination, info);
2815
0
      if (stub_type == hppa_stub_none)
2816
0
        continue;
2817
2818
      /* Support for grouping stub sections.  */
2819
0
      id_sec = htab->stub_group[section->id].link_sec;
2820
2821
      /* Get the name of this stub.  */
2822
0
      stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
2823
0
      if (!stub_name)
2824
0
        goto error_ret_free_internal;
2825
2826
0
      hsh = hppa_stub_hash_lookup (&htab->bstab,
2827
0
                  stub_name,
2828
0
                  false, false);
2829
0
      if (hsh != NULL)
2830
0
        {
2831
          /* The proper stub has already been created.  */
2832
0
          free (stub_name);
2833
0
          continue;
2834
0
        }
2835
2836
0
      hsh = hppa_add_stub (stub_name, section, htab);
2837
0
      if (hsh == NULL)
2838
0
        {
2839
0
          free (stub_name);
2840
0
          goto error_ret_free_internal;
2841
0
        }
2842
2843
0
      hsh->target_value = sym_value;
2844
0
      hsh->target_section = sym_sec;
2845
0
      hsh->stub_type = stub_type;
2846
0
      if (bfd_link_pic (info))
2847
0
        {
2848
0
          if (stub_type == hppa_stub_import)
2849
0
      hsh->stub_type = hppa_stub_import_shared;
2850
0
          else if (stub_type == hppa_stub_long_branch)
2851
0
      hsh->stub_type = hppa_stub_long_branch_shared;
2852
0
        }
2853
0
      hsh->hh = hh;
2854
0
      stub_changed = true;
2855
0
    }
2856
2857
        /* We're done with the internal relocs, free them.  */
2858
0
        if (elf_section_data (section)->relocs == NULL)
2859
0
    free (internal_relocs);
2860
0
      }
2861
0
  }
2862
2863
0
      if (!stub_changed)
2864
0
  break;
2865
2866
      /* OK, we've added some stubs.  Find out the new size of the
2867
   stub sections.  */
2868
0
      for (stub_sec = htab->stub_bfd->sections;
2869
0
     stub_sec != NULL;
2870
0
     stub_sec = stub_sec->next)
2871
0
  if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2872
0
    stub_sec->size = 0;
2873
2874
0
      bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
2875
2876
      /* Ask the linker to do its stuff.  */
2877
0
      (*htab->layout_sections_again) ();
2878
0
      stub_changed = false;
2879
0
    }
2880
2881
0
  free (htab->all_local_syms);
2882
0
  return true;
2883
2884
0
 error_ret_free_local:
2885
0
  free (htab->all_local_syms);
2886
0
  return false;
2887
0
}
2888
2889
/* For a final link, this function is called after we have sized the
2890
   stubs to provide a value for __gp.  */
2891
2892
bool
2893
elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
2894
0
{
2895
0
  struct bfd_link_hash_entry *h;
2896
0
  asection *sec = NULL;
2897
0
  bfd_vma gp_val = 0;
2898
2899
0
  h = bfd_link_hash_lookup (info->hash, "$global$", false, false, false);
2900
2901
0
  if (h != NULL
2902
0
      && (h->type == bfd_link_hash_defined
2903
0
    || h->type == bfd_link_hash_defweak))
2904
0
    {
2905
0
      gp_val = h->u.def.value;
2906
0
      sec = h->u.def.section;
2907
0
    }
2908
0
  else
2909
0
    {
2910
0
      asection *splt = bfd_get_section_by_name (abfd, ".plt");
2911
0
      asection *sgot = bfd_get_section_by_name (abfd, ".got");
2912
2913
      /* Choose to point our LTP at, in this order, one of .plt, .got,
2914
   or .data, if these sections exist.  In the case of choosing
2915
   .plt try to make the LTP ideal for addressing anywhere in the
2916
   .plt or .got with a 14 bit signed offset.  Typically, the end
2917
   of the .plt is the start of the .got, so choose .plt + 0x2000
2918
   if either the .plt or .got is larger than 0x2000.  If both
2919
   the .plt and .got are smaller than 0x2000, choose the end of
2920
   the .plt section.  */
2921
0
      sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
2922
0
    ? NULL : splt;
2923
0
      if (sec != NULL)
2924
0
  {
2925
0
    gp_val = sec->size;
2926
0
    if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
2927
0
      {
2928
0
        gp_val = 0x2000;
2929
0
      }
2930
0
  }
2931
0
      else
2932
0
  {
2933
0
    sec = sgot;
2934
0
    if (sec != NULL)
2935
0
      {
2936
0
        if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
2937
0
    {
2938
      /* We know we don't have a .plt.  If .got is large,
2939
         offset our LTP.  */
2940
0
      if (sec->size > 0x2000)
2941
0
        gp_val = 0x2000;
2942
0
    }
2943
0
      }
2944
0
    else
2945
0
      {
2946
        /* No .plt or .got.  Who cares what the LTP is?  */
2947
0
        sec = bfd_get_section_by_name (abfd, ".data");
2948
0
      }
2949
0
  }
2950
2951
0
      if (h != NULL)
2952
0
  {
2953
0
    h->type = bfd_link_hash_defined;
2954
0
    h->u.def.value = gp_val;
2955
0
    if (sec != NULL)
2956
0
      h->u.def.section = sec;
2957
0
    else
2958
0
      h->u.def.section = bfd_abs_section_ptr;
2959
0
  }
2960
0
    }
2961
2962
0
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2963
0
    {
2964
0
      if (sec != NULL && sec->output_section != NULL)
2965
0
  gp_val += sec->output_section->vma + sec->output_offset;
2966
2967
0
      elf_gp (abfd) = gp_val;
2968
0
    }
2969
0
  return true;
2970
0
}
2971
2972
/* Build all the stubs associated with the current output file.  The
2973
   stubs are kept in a hash table attached to the main linker hash
2974
   table.  We also set up the .plt entries for statically linked PIC
2975
   functions here.  This function is called via hppaelf_finish in the
2976
   linker.  */
2977
2978
bool
2979
elf32_hppa_build_stubs (struct bfd_link_info *info)
2980
0
{
2981
0
  asection *stub_sec;
2982
0
  struct bfd_hash_table *table;
2983
0
  struct elf32_hppa_link_hash_table *htab;
2984
2985
0
  htab = hppa_link_hash_table (info);
2986
0
  if (htab == NULL)
2987
0
    return false;
2988
2989
0
  for (stub_sec = htab->stub_bfd->sections;
2990
0
       stub_sec != NULL;
2991
0
       stub_sec = stub_sec->next)
2992
0
    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
2993
0
  && stub_sec->size != 0)
2994
0
      {
2995
  /* Allocate memory to hold the linker stubs.  */
2996
0
  stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
2997
0
  if (stub_sec->contents == NULL)
2998
0
    return false;
2999
0
  stub_sec->size = 0;
3000
0
      }
3001
3002
  /* Build the stubs as directed by the stub hash table.  */
3003
0
  table = &htab->bstab;
3004
0
  bfd_hash_traverse (table, hppa_build_one_stub, info);
3005
3006
0
  return true;
3007
0
}
3008
3009
/* Return the base vma address which should be subtracted from the real
3010
   address when resolving a dtpoff relocation.
3011
   This is PT_TLS segment p_vaddr.  */
3012
3013
static bfd_vma
3014
dtpoff_base (struct bfd_link_info *info)
3015
0
{
3016
  /* If tls_sec is NULL, we should have signalled an error already.  */
3017
0
  if (elf_hash_table (info)->tls_sec == NULL)
3018
0
    return 0;
3019
0
  return elf_hash_table (info)->tls_sec->vma;
3020
0
}
3021
3022
/* Return the relocation value for R_PARISC_TLS_TPOFF*..  */
3023
3024
static bfd_vma
3025
tpoff (struct bfd_link_info *info, bfd_vma address)
3026
0
{
3027
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
3028
3029
  /* If tls_sec is NULL, we should have signalled an error already.  */
3030
0
  if (htab->tls_sec == NULL)
3031
0
    return 0;
3032
  /* hppa TLS ABI is variant I and static TLS block start just after
3033
     tcbhead structure which has 2 pointer fields.  */
3034
0
  return (address - htab->tls_sec->vma
3035
0
    + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
3036
0
}
3037
3038
/* Perform a final link.  */
3039
3040
static bool
3041
elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3042
0
{
3043
0
  struct stat buf;
3044
3045
  /* Invoke the regular ELF linker to do all the work.  */
3046
0
  if (!bfd_elf_final_link (abfd, info))
3047
0
    return false;
3048
3049
  /* If we're producing a final executable, sort the contents of the
3050
     unwind section.  */
3051
0
  if (bfd_link_relocatable (info))
3052
0
    return true;
3053
3054
  /* Do not attempt to sort non-regular files.  This is here
3055
     especially for configure scripts and kernel builds which run
3056
     tests with "ld [...] -o /dev/null".  */
3057
0
  if (stat (bfd_get_filename (abfd), &buf) != 0
3058
0
      || !S_ISREG(buf.st_mode))
3059
0
    return true;
3060
3061
0
  return elf_hppa_sort_unwind (abfd);
3062
0
}
3063
3064
/* Record the lowest address for the data and text segments.  */
3065
3066
static void
3067
hppa_record_segment_addr (bfd *abfd, asection *section, void *data)
3068
0
{
3069
0
  struct elf32_hppa_link_hash_table *htab;
3070
3071
0
  htab = (struct elf32_hppa_link_hash_table*) data;
3072
0
  if (htab == NULL)
3073
0
    return;
3074
3075
0
  if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3076
0
    {
3077
0
      bfd_vma value;
3078
0
      Elf_Internal_Phdr *p;
3079
3080
0
      p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
3081
0
      BFD_ASSERT (p != NULL);
3082
0
      value = p->p_vaddr;
3083
3084
0
      if ((section->flags & SEC_READONLY) != 0)
3085
0
  {
3086
0
    if (value < htab->text_segment_base)
3087
0
      htab->text_segment_base = value;
3088
0
  }
3089
0
      else
3090
0
  {
3091
0
    if (value < htab->data_segment_base)
3092
0
      htab->data_segment_base = value;
3093
0
  }
3094
0
    }
3095
0
}
3096
3097
/* Perform a relocation as part of a final link.  */
3098
3099
static bfd_reloc_status_type
3100
final_link_relocate (asection *input_section,
3101
         bfd_byte *contents,
3102
         const Elf_Internal_Rela *rela,
3103
         bfd_vma value,
3104
         struct elf32_hppa_link_hash_table *htab,
3105
         asection *sym_sec,
3106
         struct elf32_hppa_link_hash_entry *hh,
3107
         struct bfd_link_info *info)
3108
0
{
3109
0
  unsigned int insn;
3110
0
  unsigned int r_type = ELF32_R_TYPE (rela->r_info);
3111
0
  unsigned int orig_r_type = r_type;
3112
0
  reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3113
0
  int r_format = howto->bitsize;
3114
0
  enum hppa_reloc_field_selector_type_alt r_field;
3115
0
  bfd *input_bfd = input_section->owner;
3116
0
  bfd_vma offset = rela->r_offset;
3117
0
  bfd_vma max_branch_offset = 0;
3118
0
  bfd_byte *hit_data = contents + offset;
3119
0
  bfd_signed_vma addend = rela->r_addend;
3120
0
  bfd_vma location;
3121
0
  struct elf32_hppa_stub_hash_entry *hsh = NULL;
3122
0
  int val;
3123
3124
0
  if (r_type == R_PARISC_NONE)
3125
0
    return bfd_reloc_ok;
3126
3127
0
  insn = bfd_get_32 (input_bfd, hit_data);
3128
3129
  /* Find out where we are and where we're going.  */
3130
0
  location = (offset +
3131
0
        input_section->output_offset +
3132
0
        input_section->output_section->vma);
3133
3134
  /* If we are not building a shared library, convert DLTIND relocs to
3135
     DPREL relocs.  */
3136
0
  if (!bfd_link_pic (info))
3137
0
    {
3138
0
      switch (r_type)
3139
0
  {
3140
0
    case R_PARISC_DLTIND21L:
3141
0
    case R_PARISC_TLS_GD21L:
3142
0
    case R_PARISC_TLS_LDM21L:
3143
0
    case R_PARISC_TLS_IE21L:
3144
0
      r_type = R_PARISC_DPREL21L;
3145
0
      break;
3146
3147
0
    case R_PARISC_DLTIND14R:
3148
0
    case R_PARISC_TLS_GD14R:
3149
0
    case R_PARISC_TLS_LDM14R:
3150
0
    case R_PARISC_TLS_IE14R:
3151
0
      r_type = R_PARISC_DPREL14R;
3152
0
      break;
3153
3154
0
    case R_PARISC_DLTIND14F:
3155
0
      r_type = R_PARISC_DPREL14F;
3156
0
      break;
3157
0
  }
3158
0
    }
3159
3160
0
  switch (r_type)
3161
0
    {
3162
0
    case R_PARISC_PCREL12F:
3163
0
    case R_PARISC_PCREL17F:
3164
0
    case R_PARISC_PCREL22F:
3165
      /* If this call should go via the plt, find the import stub in
3166
   the stub hash.  */
3167
0
      if (sym_sec == NULL
3168
0
    || sym_sec->output_section == NULL
3169
0
    || (hh != NULL
3170
0
        && hh->eh.plt.offset != (bfd_vma) -1
3171
0
        && hh->eh.dynindx != -1
3172
0
        && !hh->plabel
3173
0
        && (bfd_link_pic (info)
3174
0
      || !hh->eh.def_regular
3175
0
      || hh->eh.root.type == bfd_link_hash_defweak)))
3176
0
  {
3177
0
    hsh = hppa_get_stub_entry (input_section, sym_sec,
3178
0
             hh, rela, htab);
3179
0
    if (hsh != NULL)
3180
0
      {
3181
0
        value = (hsh->stub_offset
3182
0
           + hsh->stub_sec->output_offset
3183
0
           + hsh->stub_sec->output_section->vma);
3184
0
        addend = 0;
3185
0
      }
3186
0
    else if (sym_sec == NULL && hh != NULL
3187
0
       && hh->eh.root.type == bfd_link_hash_undefweak)
3188
0
      {
3189
        /* It's OK if undefined weak.  Calls to undefined weak
3190
     symbols behave as if the "called" function
3191
     immediately returns.  We can thus call to a weak
3192
     function without first checking whether the function
3193
     is defined.  */
3194
0
        value = location;
3195
0
        addend = 8;
3196
0
      }
3197
0
    else
3198
0
      return bfd_reloc_undefined;
3199
0
  }
3200
      /* Fall thru.  */
3201
3202
0
    case R_PARISC_PCREL21L:
3203
0
    case R_PARISC_PCREL17C:
3204
0
    case R_PARISC_PCREL17R:
3205
0
    case R_PARISC_PCREL14R:
3206
0
    case R_PARISC_PCREL14F:
3207
0
    case R_PARISC_PCREL32:
3208
      /* Make it a pc relative offset.  */
3209
0
      value -= location;
3210
0
      addend -= 8;
3211
0
      break;
3212
3213
0
    case R_PARISC_DPREL21L:
3214
0
    case R_PARISC_DPREL14R:
3215
0
    case R_PARISC_DPREL14F:
3216
      /* Convert instructions that use the linkage table pointer (r19) to
3217
   instructions that use the global data pointer (dp).  This is the
3218
   most efficient way of using PIC code in an incomplete executable,
3219
   but the user must follow the standard runtime conventions for
3220
   accessing data for this to work.  */
3221
0
      if (orig_r_type != r_type)
3222
0
  {
3223
0
    if (r_type == R_PARISC_DPREL21L)
3224
0
      {
3225
        /* GCC sometimes uses a register other than r19 for the
3226
     operation, so we must convert any addil instruction
3227
     that uses this relocation.  */
3228
0
        if ((insn & 0xfc000000) == OP_ADDIL << 26)
3229
0
    insn = ADDIL_DP;
3230
0
        else
3231
    /* We must have a ldil instruction.  It's too hard to find
3232
       and convert the associated add instruction, so issue an
3233
       error.  */
3234
0
    _bfd_error_handler
3235
      /* xgettext:c-format */
3236
0
      (_("%pB(%pA+%#" PRIx64 "): %s fixup for insn %#x "
3237
0
         "is not supported in a non-shared link"),
3238
0
       input_bfd,
3239
0
       input_section,
3240
0
       (uint64_t) offset,
3241
0
       howto->name,
3242
0
       insn);
3243
0
      }
3244
0
    else if (r_type == R_PARISC_DPREL14F)
3245
0
      {
3246
        /* This must be a format 1 load/store.  Change the base
3247
     register to dp.  */
3248
0
        insn = (insn & 0xfc1ffff) | (27 << 21);
3249
0
      }
3250
0
  }
3251
3252
      /* For all the DP relative relocations, we need to examine the symbol's
3253
   section.  If it has no section or if it's a code section, then
3254
   "data pointer relative" makes no sense.  In that case we don't
3255
   adjust the "value", and for 21 bit addil instructions, we change the
3256
   source addend register from %dp to %r0.  This situation commonly
3257
   arises for undefined weak symbols and when a variable's "constness"
3258
   is declared differently from the way the variable is defined.  For
3259
   instance: "extern int foo" with foo defined as "const int foo".  */
3260
0
      if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3261
0
  {
3262
0
    if ((insn & ((0x3fu << 26) | (0x1f << 21)))
3263
0
        == ((OP_ADDIL << 26) | (27 << 21)))
3264
0
      {
3265
0
        insn &= ~ (0x1f << 21);
3266
0
      }
3267
    /* Now try to make things easy for the dynamic linker.  */
3268
3269
0
    break;
3270
0
  }
3271
      /* Fall thru.  */
3272
3273
0
    case R_PARISC_DLTIND21L:
3274
0
    case R_PARISC_DLTIND14R:
3275
0
    case R_PARISC_DLTIND14F:
3276
0
    case R_PARISC_TLS_GD21L:
3277
0
    case R_PARISC_TLS_LDM21L:
3278
0
    case R_PARISC_TLS_IE21L:
3279
0
    case R_PARISC_TLS_GD14R:
3280
0
    case R_PARISC_TLS_LDM14R:
3281
0
    case R_PARISC_TLS_IE14R:
3282
0
      value -= elf_gp (input_section->output_section->owner);
3283
0
      break;
3284
3285
0
    case R_PARISC_SEGREL32:
3286
0
      if ((sym_sec->flags & SEC_CODE) != 0)
3287
0
  value -= htab->text_segment_base;
3288
0
      else
3289
0
  value -= htab->data_segment_base;
3290
0
      break;
3291
3292
0
    default:
3293
0
      break;
3294
0
    }
3295
3296
0
  switch (r_type)
3297
0
    {
3298
0
    case R_PARISC_DIR32:
3299
0
    case R_PARISC_DIR14F:
3300
0
    case R_PARISC_DIR17F:
3301
0
    case R_PARISC_PCREL17C:
3302
0
    case R_PARISC_PCREL14F:
3303
0
    case R_PARISC_PCREL32:
3304
0
    case R_PARISC_DPREL14F:
3305
0
    case R_PARISC_PLABEL32:
3306
0
    case R_PARISC_DLTIND14F:
3307
0
    case R_PARISC_SEGBASE:
3308
0
    case R_PARISC_SEGREL32:
3309
0
    case R_PARISC_TLS_DTPMOD32:
3310
0
    case R_PARISC_TLS_DTPOFF32:
3311
0
    case R_PARISC_TLS_TPREL32:
3312
0
      r_field = e_fsel;
3313
0
      break;
3314
3315
0
    case R_PARISC_DLTIND21L:
3316
0
    case R_PARISC_PCREL21L:
3317
0
    case R_PARISC_PLABEL21L:
3318
0
      r_field = e_lsel;
3319
0
      break;
3320
3321
0
    case R_PARISC_DIR21L:
3322
0
    case R_PARISC_DPREL21L:
3323
0
    case R_PARISC_TLS_GD21L:
3324
0
    case R_PARISC_TLS_LDM21L:
3325
0
    case R_PARISC_TLS_LDO21L:
3326
0
    case R_PARISC_TLS_IE21L:
3327
0
    case R_PARISC_TLS_LE21L:
3328
0
      r_field = e_lrsel;
3329
0
      break;
3330
3331
0
    case R_PARISC_PCREL17R:
3332
0
    case R_PARISC_PCREL14R:
3333
0
    case R_PARISC_PLABEL14R:
3334
0
    case R_PARISC_DLTIND14R:
3335
0
      r_field = e_rsel;
3336
0
      break;
3337
3338
0
    case R_PARISC_DIR17R:
3339
0
    case R_PARISC_DIR14R:
3340
0
    case R_PARISC_DPREL14R:
3341
0
    case R_PARISC_TLS_GD14R:
3342
0
    case R_PARISC_TLS_LDM14R:
3343
0
    case R_PARISC_TLS_LDO14R:
3344
0
    case R_PARISC_TLS_IE14R:
3345
0
    case R_PARISC_TLS_LE14R:
3346
0
      r_field = e_rrsel;
3347
0
      break;
3348
3349
0
    case R_PARISC_PCREL12F:
3350
0
    case R_PARISC_PCREL17F:
3351
0
    case R_PARISC_PCREL22F:
3352
0
      r_field = e_fsel;
3353
3354
0
      if (r_type == (unsigned int) R_PARISC_PCREL17F)
3355
0
  {
3356
0
    max_branch_offset = (1 << (17-1)) << 2;
3357
0
  }
3358
0
      else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3359
0
  {
3360
0
    max_branch_offset = (1 << (12-1)) << 2;
3361
0
  }
3362
0
      else
3363
0
  {
3364
0
    max_branch_offset = (1 << (22-1)) << 2;
3365
0
  }
3366
3367
      /* sym_sec is NULL on undefined weak syms or when shared on
3368
   undefined syms.  We've already checked for a stub for the
3369
   shared undefined case.  */
3370
0
      if (sym_sec == NULL)
3371
0
  break;
3372
3373
      /* If the branch is out of reach, then redirect the
3374
   call to the local stub for this function.  */
3375
0
      if (value + addend + max_branch_offset >= 2*max_branch_offset)
3376
0
  {
3377
0
    hsh = hppa_get_stub_entry (input_section, sym_sec,
3378
0
             hh, rela, htab);
3379
0
    if (hsh == NULL)
3380
0
      return bfd_reloc_undefined;
3381
3382
    /* Munge up the value and addend so that we call the stub
3383
       rather than the procedure directly.  */
3384
0
    value = (hsh->stub_offset
3385
0
       + hsh->stub_sec->output_offset
3386
0
       + hsh->stub_sec->output_section->vma
3387
0
       - location);
3388
0
    addend = -8;
3389
0
  }
3390
0
      break;
3391
3392
    /* Something we don't know how to handle.  */
3393
0
    default:
3394
0
      return bfd_reloc_notsupported;
3395
0
    }
3396
3397
  /* Make sure we can reach the stub.  */
3398
0
  if (max_branch_offset != 0
3399
0
      && value + addend + max_branch_offset >= 2*max_branch_offset)
3400
0
    {
3401
0
      _bfd_error_handler
3402
  /* xgettext:c-format */
3403
0
  (_("%pB(%pA+%#" PRIx64 "): cannot reach %s, "
3404
0
     "recompile with -ffunction-sections"),
3405
0
   input_bfd,
3406
0
   input_section,
3407
0
   (uint64_t) offset,
3408
0
   hsh->bh_root.string);
3409
0
      bfd_set_error (bfd_error_bad_value);
3410
0
      return bfd_reloc_notsupported;
3411
0
    }
3412
3413
0
  val = hppa_field_adjust (value, addend, r_field);
3414
3415
0
  switch (r_type)
3416
0
    {
3417
0
    case R_PARISC_PCREL12F:
3418
0
    case R_PARISC_PCREL17C:
3419
0
    case R_PARISC_PCREL17F:
3420
0
    case R_PARISC_PCREL17R:
3421
0
    case R_PARISC_PCREL22F:
3422
0
    case R_PARISC_DIR17F:
3423
0
    case R_PARISC_DIR17R:
3424
      /* This is a branch.  Divide the offset by four.
3425
   Note that we need to decide whether it's a branch or
3426
   otherwise by inspecting the reloc.  Inspecting insn won't
3427
   work as insn might be from a .word directive.  */
3428
0
      val >>= 2;
3429
0
      break;
3430
3431
0
    default:
3432
0
      break;
3433
0
    }
3434
3435
0
  insn = hppa_rebuild_insn (insn, val, r_format);
3436
3437
  /* Update the instruction word.  */
3438
0
  bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3439
0
  return bfd_reloc_ok;
3440
0
}
3441
3442
/* Relocate an HPPA ELF section.  */
3443
3444
static int
3445
elf32_hppa_relocate_section (bfd *output_bfd,
3446
           struct bfd_link_info *info,
3447
           bfd *input_bfd,
3448
           asection *input_section,
3449
           bfd_byte *contents,
3450
           Elf_Internal_Rela *relocs,
3451
           Elf_Internal_Sym *local_syms,
3452
           asection **local_sections)
3453
0
{
3454
0
  bfd_vma *local_got_offsets;
3455
0
  struct elf32_hppa_link_hash_table *htab;
3456
0
  Elf_Internal_Shdr *symtab_hdr;
3457
0
  Elf_Internal_Rela *rela;
3458
0
  Elf_Internal_Rela *relend;
3459
3460
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3461
3462
0
  htab = hppa_link_hash_table (info);
3463
0
  if (htab == NULL)
3464
0
    return false;
3465
3466
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
3467
3468
0
  rela = relocs;
3469
0
  relend = relocs + input_section->reloc_count;
3470
0
  for (; rela < relend; rela++)
3471
0
    {
3472
0
      unsigned int r_type;
3473
0
      reloc_howto_type *howto;
3474
0
      unsigned int r_symndx;
3475
0
      struct elf32_hppa_link_hash_entry *hh;
3476
0
      Elf_Internal_Sym *sym;
3477
0
      asection *sym_sec;
3478
0
      bfd_vma relocation;
3479
0
      bfd_reloc_status_type rstatus;
3480
0
      const char *sym_name;
3481
0
      bool plabel;
3482
0
      bool warned_undef;
3483
3484
0
      r_type = ELF32_R_TYPE (rela->r_info);
3485
0
      if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3486
0
  {
3487
0
    bfd_set_error (bfd_error_bad_value);
3488
0
    return false;
3489
0
  }
3490
0
      if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3491
0
    || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3492
0
  continue;
3493
3494
0
      r_symndx = ELF32_R_SYM (rela->r_info);
3495
0
      hh = NULL;
3496
0
      sym = NULL;
3497
0
      sym_sec = NULL;
3498
0
      warned_undef = false;
3499
0
      if (r_symndx < symtab_hdr->sh_info)
3500
0
  {
3501
    /* This is a local symbol, h defaults to NULL.  */
3502
0
    sym = local_syms + r_symndx;
3503
0
    sym_sec = local_sections[r_symndx];
3504
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
3505
0
  }
3506
0
      else
3507
0
  {
3508
0
    struct elf_link_hash_entry *eh;
3509
0
    bool unresolved_reloc, ignored;
3510
0
    struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3511
3512
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
3513
0
           r_symndx, symtab_hdr, sym_hashes,
3514
0
           eh, sym_sec, relocation,
3515
0
           unresolved_reloc, warned_undef,
3516
0
           ignored);
3517
3518
0
    if (!bfd_link_relocatable (info)
3519
0
        && relocation == 0
3520
0
        && eh->root.type != bfd_link_hash_defined
3521
0
        && eh->root.type != bfd_link_hash_defweak
3522
0
        && eh->root.type != bfd_link_hash_undefweak)
3523
0
      {
3524
0
        if (info->unresolved_syms_in_objects == RM_IGNORE
3525
0
      && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3526
0
      && eh->type == STT_PARISC_MILLI)
3527
0
    {
3528
0
      (*info->callbacks->undefined_symbol)
3529
0
        (info, eh_name (eh), input_bfd,
3530
0
         input_section, rela->r_offset, false);
3531
0
      warned_undef = true;
3532
0
    }
3533
0
      }
3534
0
    hh = hppa_elf_hash_entry (eh);
3535
0
  }
3536
3537
0
      if (sym_sec != NULL && discarded_section (sym_sec))
3538
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3539
0
           rela, 1, relend,
3540
0
           elf_hppa_howto_table + r_type, 0,
3541
0
           contents);
3542
3543
0
      if (bfd_link_relocatable (info))
3544
0
  continue;
3545
3546
      /* Do any required modifications to the relocation value, and
3547
   determine what types of dynamic info we need to output, if
3548
   any.  */
3549
0
      plabel = 0;
3550
0
      switch (r_type)
3551
0
  {
3552
0
  case R_PARISC_DLTIND14F:
3553
0
  case R_PARISC_DLTIND14R:
3554
0
  case R_PARISC_DLTIND21L:
3555
0
    {
3556
0
      bfd_vma off;
3557
0
      bool do_got = false;
3558
0
      bool reloc = bfd_link_pic (info);
3559
3560
      /* Relocation is to the entry for this symbol in the
3561
         global offset table.  */
3562
0
      if (hh != NULL)
3563
0
        {
3564
0
    bool dyn;
3565
3566
0
    off = hh->eh.got.offset;
3567
0
    dyn = htab->etab.dynamic_sections_created;
3568
0
    reloc = (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh)
3569
0
       && (reloc
3570
0
           || (hh->eh.dynindx != -1
3571
0
         && !SYMBOL_REFERENCES_LOCAL (info, &hh->eh))));
3572
0
    if (!reloc
3573
0
        || !WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3574
0
               bfd_link_pic (info),
3575
0
               &hh->eh))
3576
0
      {
3577
        /* If we aren't going to call finish_dynamic_symbol,
3578
           then we need to handle initialisation of the .got
3579
           entry and create needed relocs here.  Since the
3580
           offset must always be a multiple of 4, we use the
3581
           least significant bit to record whether we have
3582
           initialised it already.  */
3583
0
        if ((off & 1) != 0)
3584
0
          off &= ~1;
3585
0
        else
3586
0
          {
3587
0
      hh->eh.got.offset |= 1;
3588
0
      do_got = true;
3589
0
          }
3590
0
      }
3591
0
        }
3592
0
      else
3593
0
        {
3594
    /* Local symbol case.  */
3595
0
    if (local_got_offsets == NULL)
3596
0
      abort ();
3597
3598
0
    off = local_got_offsets[r_symndx];
3599
3600
    /* The offset must always be a multiple of 4.  We use
3601
       the least significant bit to record whether we have
3602
       already generated the necessary reloc.  */
3603
0
    if ((off & 1) != 0)
3604
0
      off &= ~1;
3605
0
    else
3606
0
      {
3607
0
        local_got_offsets[r_symndx] |= 1;
3608
0
        do_got = true;
3609
0
      }
3610
0
        }
3611
3612
0
      if (do_got)
3613
0
        {
3614
0
    if (reloc)
3615
0
      {
3616
        /* Output a dynamic relocation for this GOT entry.
3617
           In this case it is relative to the base of the
3618
           object because the symbol index is zero.  */
3619
0
        Elf_Internal_Rela outrel;
3620
0
        bfd_byte *loc;
3621
0
        asection *sec = htab->etab.srelgot;
3622
3623
0
        outrel.r_offset = (off
3624
0
               + htab->etab.sgot->output_offset
3625
0
               + htab->etab.sgot->output_section->vma);
3626
0
        outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3627
0
        outrel.r_addend = relocation;
3628
0
        loc = sec->contents;
3629
0
        loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
3630
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3631
0
      }
3632
0
    else
3633
0
      bfd_put_32 (output_bfd, relocation,
3634
0
            htab->etab.sgot->contents + off);
3635
0
        }
3636
3637
0
      if (off >= (bfd_vma) -2)
3638
0
        abort ();
3639
3640
      /* Add the base of the GOT to the relocation value.  */
3641
0
      relocation = (off
3642
0
        + htab->etab.sgot->output_offset
3643
0
        + htab->etab.sgot->output_section->vma);
3644
0
    }
3645
0
    break;
3646
3647
0
  case R_PARISC_SEGREL32:
3648
    /* If this is the first SEGREL relocation, then initialize
3649
       the segment base values.  */
3650
0
    if (htab->text_segment_base == (bfd_vma) -1)
3651
0
      bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3652
0
    break;
3653
3654
0
  case R_PARISC_PLABEL14R:
3655
0
  case R_PARISC_PLABEL21L:
3656
0
  case R_PARISC_PLABEL32:
3657
0
    if (htab->etab.dynamic_sections_created)
3658
0
      {
3659
0
        bfd_vma off;
3660
0
        bool do_plt = 0;
3661
        /* If we have a global symbol with a PLT slot, then
3662
     redirect this relocation to it.  */
3663
0
        if (hh != NULL)
3664
0
    {
3665
0
      off = hh->eh.plt.offset;
3666
0
      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1,
3667
0
               bfd_link_pic (info),
3668
0
               &hh->eh))
3669
0
        {
3670
          /* In a non-shared link, adjust_dynamic_symbol
3671
       isn't called for symbols forced local.  We
3672
       need to write out the plt entry here.  */
3673
0
          if ((off & 1) != 0)
3674
0
      off &= ~1;
3675
0
          else
3676
0
      {
3677
0
        hh->eh.plt.offset |= 1;
3678
0
        do_plt = 1;
3679
0
      }
3680
0
        }
3681
0
    }
3682
0
        else
3683
0
    {
3684
0
      bfd_vma *local_plt_offsets;
3685
3686
0
      if (local_got_offsets == NULL)
3687
0
        abort ();
3688
3689
0
      local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3690
0
      off = local_plt_offsets[r_symndx];
3691
3692
      /* As for the local .got entry case, we use the last
3693
         bit to record whether we've already initialised
3694
         this local .plt entry.  */
3695
0
      if ((off & 1) != 0)
3696
0
        off &= ~1;
3697
0
      else
3698
0
        {
3699
0
          local_plt_offsets[r_symndx] |= 1;
3700
0
          do_plt = 1;
3701
0
        }
3702
0
    }
3703
3704
0
        if (do_plt)
3705
0
    {
3706
0
      if (bfd_link_pic (info))
3707
0
        {
3708
          /* Output a dynamic IPLT relocation for this
3709
       PLT entry.  */
3710
0
          Elf_Internal_Rela outrel;
3711
0
          bfd_byte *loc;
3712
0
          asection *s = htab->etab.srelplt;
3713
3714
0
          outrel.r_offset = (off
3715
0
           + htab->etab.splt->output_offset
3716
0
           + htab->etab.splt->output_section->vma);
3717
0
          outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3718
0
          outrel.r_addend = relocation;
3719
0
          loc = s->contents;
3720
0
          loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3721
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3722
0
        }
3723
0
      else
3724
0
        {
3725
0
          bfd_put_32 (output_bfd,
3726
0
          relocation,
3727
0
          htab->etab.splt->contents + off);
3728
0
          bfd_put_32 (output_bfd,
3729
0
          elf_gp (htab->etab.splt->output_section->owner),
3730
0
          htab->etab.splt->contents + off + 4);
3731
0
        }
3732
0
    }
3733
3734
0
        if (off >= (bfd_vma) -2)
3735
0
    abort ();
3736
3737
        /* PLABELs contain function pointers.  Relocation is to
3738
     the entry for the function in the .plt.  The magic +2
3739
     offset signals to $$dyncall that the function pointer
3740
     is in the .plt and thus has a gp pointer too.
3741
     Exception:  Undefined PLABELs should have a value of
3742
     zero.  */
3743
0
        if (hh == NULL
3744
0
      || (hh->eh.root.type != bfd_link_hash_undefweak
3745
0
          && hh->eh.root.type != bfd_link_hash_undefined))
3746
0
    {
3747
0
      relocation = (off
3748
0
        + htab->etab.splt->output_offset
3749
0
        + htab->etab.splt->output_section->vma
3750
0
        + 2);
3751
0
    }
3752
0
        plabel = 1;
3753
0
      }
3754
    /* Fall through.  */
3755
3756
0
  case R_PARISC_DIR17F:
3757
0
  case R_PARISC_DIR17R:
3758
0
  case R_PARISC_DIR14F:
3759
0
  case R_PARISC_DIR14R:
3760
0
  case R_PARISC_DIR21L:
3761
0
  case R_PARISC_DPREL14F:
3762
0
  case R_PARISC_DPREL14R:
3763
0
  case R_PARISC_DPREL21L:
3764
0
  case R_PARISC_DIR32:
3765
0
    if ((input_section->flags & SEC_ALLOC) == 0)
3766
0
      break;
3767
3768
0
    if (bfd_link_pic (info)
3769
0
        ? ((hh == NULL
3770
0
      || hh->eh.dyn_relocs != NULL)
3771
0
     && ((hh != NULL && pc_dynrelocs (hh))
3772
0
         || IS_ABSOLUTE_RELOC (r_type)))
3773
0
        : (hh != NULL
3774
0
     && hh->eh.dyn_relocs != NULL))
3775
0
      {
3776
0
        Elf_Internal_Rela outrel;
3777
0
        bool skip;
3778
0
        asection *sreloc;
3779
0
        bfd_byte *loc;
3780
3781
        /* When generating a shared object, these relocations
3782
     are copied into the output file to be resolved at run
3783
     time.  */
3784
3785
0
        outrel.r_addend = rela->r_addend;
3786
0
        outrel.r_offset =
3787
0
    _bfd_elf_section_offset (output_bfd, info, input_section,
3788
0
           rela->r_offset);
3789
0
        skip = (outrel.r_offset == (bfd_vma) -1
3790
0
          || outrel.r_offset == (bfd_vma) -2);
3791
0
        outrel.r_offset += (input_section->output_offset
3792
0
          + input_section->output_section->vma);
3793
3794
0
        if (skip)
3795
0
    {
3796
0
      memset (&outrel, 0, sizeof (outrel));
3797
0
    }
3798
0
        else if (hh != NULL
3799
0
           && hh->eh.dynindx != -1
3800
0
           && (plabel
3801
0
         || !IS_ABSOLUTE_RELOC (r_type)
3802
0
         || !bfd_link_pic (info)
3803
0
         || !SYMBOLIC_BIND (info, &hh->eh)
3804
0
         || !hh->eh.def_regular))
3805
0
    {
3806
0
      outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
3807
0
    }
3808
0
        else /* It's a local symbol, or one marked to become local.  */
3809
0
    {
3810
0
      int indx = 0;
3811
3812
      /* Add the absolute offset of the symbol.  */
3813
0
      outrel.r_addend += relocation;
3814
3815
      /* Global plabels need to be processed by the
3816
         dynamic linker so that functions have at most one
3817
         fptr.  For this reason, we need to differentiate
3818
         between global and local plabels, which we do by
3819
         providing the function symbol for a global plabel
3820
         reloc, and no symbol for local plabels.  */
3821
0
      if (! plabel
3822
0
          && sym_sec != NULL
3823
0
          && sym_sec->output_section != NULL
3824
0
          && ! bfd_is_abs_section (sym_sec))
3825
0
        {
3826
0
          asection *osec;
3827
3828
0
          osec = sym_sec->output_section;
3829
0
          indx = elf_section_data (osec)->dynindx;
3830
0
          if (indx == 0)
3831
0
      {
3832
0
        osec = htab->etab.text_index_section;
3833
0
        indx = elf_section_data (osec)->dynindx;
3834
0
      }
3835
0
          BFD_ASSERT (indx != 0);
3836
3837
          /* We are turning this relocation into one
3838
       against a section symbol, so subtract out the
3839
       output section's address but not the offset
3840
       of the input section in the output section.  */
3841
0
          outrel.r_addend -= osec->vma;
3842
0
        }
3843
3844
0
      outrel.r_info = ELF32_R_INFO (indx, r_type);
3845
0
    }
3846
0
        sreloc = elf_section_data (input_section)->sreloc;
3847
0
        if (sreloc == NULL)
3848
0
    abort ();
3849
3850
0
        loc = sreloc->contents;
3851
0
        loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3852
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3853
0
      }
3854
0
    break;
3855
3856
0
  case R_PARISC_TLS_LDM21L:
3857
0
  case R_PARISC_TLS_LDM14R:
3858
0
    {
3859
0
      bfd_vma off;
3860
3861
0
      off = htab->tls_ldm_got.offset;
3862
0
      if (off & 1)
3863
0
        off &= ~1;
3864
0
      else
3865
0
        {
3866
0
    Elf_Internal_Rela outrel;
3867
0
    bfd_byte *loc;
3868
3869
0
    outrel.r_offset = (off
3870
0
           + htab->etab.sgot->output_section->vma
3871
0
           + htab->etab.sgot->output_offset);
3872
0
    outrel.r_addend = 0;
3873
0
    outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
3874
0
    loc = htab->etab.srelgot->contents;
3875
0
    loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3876
3877
0
    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3878
0
    htab->tls_ldm_got.offset |= 1;
3879
0
        }
3880
3881
      /* Add the base of the GOT to the relocation value.  */
3882
0
      relocation = (off
3883
0
        + htab->etab.sgot->output_offset
3884
0
        + htab->etab.sgot->output_section->vma);
3885
3886
0
      break;
3887
0
    }
3888
3889
0
  case R_PARISC_TLS_LDO21L:
3890
0
  case R_PARISC_TLS_LDO14R:
3891
0
    relocation -= dtpoff_base (info);
3892
0
    break;
3893
3894
0
  case R_PARISC_TLS_GD21L:
3895
0
  case R_PARISC_TLS_GD14R:
3896
0
  case R_PARISC_TLS_IE21L:
3897
0
  case R_PARISC_TLS_IE14R:
3898
0
    {
3899
0
      bfd_vma off;
3900
0
      int indx;
3901
0
      char tls_type;
3902
3903
0
      indx = 0;
3904
0
      if (hh != NULL)
3905
0
        {
3906
0
    if (!htab->etab.dynamic_sections_created
3907
0
        || hh->eh.dynindx == -1
3908
0
        || SYMBOL_REFERENCES_LOCAL (info, &hh->eh)
3909
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))
3910
      /* This is actually a static link, or it is a
3911
         -Bsymbolic link and the symbol is defined
3912
         locally, or the symbol was forced to be local
3913
         because of a version file.  */
3914
0
      ;
3915
0
    else
3916
0
      indx = hh->eh.dynindx;
3917
0
    off = hh->eh.got.offset;
3918
0
    tls_type = hh->tls_type;
3919
0
        }
3920
0
      else
3921
0
        {
3922
0
    off = local_got_offsets[r_symndx];
3923
0
    tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
3924
0
        }
3925
3926
0
      if (tls_type == GOT_UNKNOWN)
3927
0
        abort ();
3928
3929
0
      if ((off & 1) != 0)
3930
0
        off &= ~1;
3931
0
      else
3932
0
        {
3933
0
    bool need_relocs = false;
3934
0
    Elf_Internal_Rela outrel;
3935
0
    bfd_byte *loc = NULL;
3936
0
    int cur_off = off;
3937
3938
    /* The GOT entries have not been initialized yet.  Do it
3939
       now, and emit any relocations.  If both an IE GOT and a
3940
       GD GOT are necessary, we emit the GD first.  */
3941
3942
0
    if (indx != 0
3943
0
        || (bfd_link_dll (info)
3944
0
      && (hh == NULL
3945
0
          || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))))
3946
0
      {
3947
0
        need_relocs = true;
3948
0
        loc = htab->etab.srelgot->contents;
3949
0
        loc += (htab->etab.srelgot->reloc_count
3950
0
          * sizeof (Elf32_External_Rela));
3951
0
      }
3952
3953
0
    if (tls_type & GOT_TLS_GD)
3954
0
      {
3955
0
        if (need_relocs)
3956
0
          {
3957
0
      outrel.r_offset
3958
0
        = (cur_off
3959
0
           + htab->etab.sgot->output_section->vma
3960
0
           + htab->etab.sgot->output_offset);
3961
0
      outrel.r_info
3962
0
        = ELF32_R_INFO (indx, R_PARISC_TLS_DTPMOD32);
3963
0
      outrel.r_addend = 0;
3964
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3965
0
      htab->etab.srelgot->reloc_count++;
3966
0
      loc += sizeof (Elf32_External_Rela);
3967
0
      bfd_put_32 (output_bfd, 0,
3968
0
            htab->etab.sgot->contents + cur_off);
3969
0
          }
3970
0
        else
3971
          /* If we are not emitting relocations for a
3972
       general dynamic reference, then we must be in a
3973
       static link or an executable link with the
3974
       symbol binding locally.  Mark it as belonging
3975
       to module 1, the executable.  */
3976
0
          bfd_put_32 (output_bfd, 1,
3977
0
          htab->etab.sgot->contents + cur_off);
3978
3979
0
        if (indx != 0)
3980
0
          {
3981
0
      outrel.r_info
3982
0
        = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
3983
0
      outrel.r_offset += 4;
3984
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3985
0
      htab->etab.srelgot->reloc_count++;
3986
0
      loc += sizeof (Elf32_External_Rela);
3987
0
      bfd_put_32 (output_bfd, 0,
3988
0
            htab->etab.sgot->contents + cur_off + 4);
3989
0
          }
3990
0
        else
3991
0
          bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3992
0
          htab->etab.sgot->contents + cur_off + 4);
3993
0
        cur_off += 8;
3994
0
      }
3995
3996
0
    if (tls_type & GOT_TLS_IE)
3997
0
      {
3998
0
        if (need_relocs
3999
0
      && !(bfd_link_executable (info)
4000
0
           && SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
4001
0
          {
4002
0
      outrel.r_offset
4003
0
        = (cur_off
4004
0
           + htab->etab.sgot->output_section->vma
4005
0
           + htab->etab.sgot->output_offset);
4006
0
      outrel.r_info = ELF32_R_INFO (indx,
4007
0
                  R_PARISC_TLS_TPREL32);
4008
0
      if (indx == 0)
4009
0
        outrel.r_addend = relocation - dtpoff_base (info);
4010
0
      else
4011
0
        outrel.r_addend = 0;
4012
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4013
0
      htab->etab.srelgot->reloc_count++;
4014
0
      loc += sizeof (Elf32_External_Rela);
4015
0
          }
4016
0
        else
4017
0
          bfd_put_32 (output_bfd, tpoff (info, relocation),
4018
0
          htab->etab.sgot->contents + cur_off);
4019
0
        cur_off += 4;
4020
0
      }
4021
4022
0
    if (hh != NULL)
4023
0
      hh->eh.got.offset |= 1;
4024
0
    else
4025
0
      local_got_offsets[r_symndx] |= 1;
4026
0
        }
4027
4028
0
      if ((tls_type & GOT_NORMAL) != 0
4029
0
    && (tls_type & (GOT_TLS_GD | GOT_TLS_LDM | GOT_TLS_IE)) != 0)
4030
0
        {
4031
0
    if (hh != NULL)
4032
0
      _bfd_error_handler (_("%s has both normal and TLS relocs"),
4033
0
              hh_name (hh));
4034
0
    else
4035
0
      {
4036
0
        Elf_Internal_Sym *isym
4037
0
          = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
4038
0
                 input_bfd, r_symndx);
4039
0
        if (isym == NULL)
4040
0
          return false;
4041
0
        sym_name
4042
0
          = bfd_elf_string_from_elf_section (input_bfd,
4043
0
               symtab_hdr->sh_link,
4044
0
               isym->st_name);
4045
0
        if (sym_name == NULL)
4046
0
          return false;
4047
0
        if (*sym_name == '\0')
4048
0
          sym_name = bfd_section_name (sym_sec);
4049
0
        _bfd_error_handler
4050
0
          (_("%pB:%s has both normal and TLS relocs"),
4051
0
           input_bfd, sym_name);
4052
0
      }
4053
0
    bfd_set_error (bfd_error_bad_value);
4054
0
    return false;
4055
0
        }
4056
4057
0
      if ((tls_type & GOT_TLS_GD)
4058
0
    && r_type != R_PARISC_TLS_GD21L
4059
0
    && r_type != R_PARISC_TLS_GD14R)
4060
0
        off += 2 * GOT_ENTRY_SIZE;
4061
4062
      /* Add the base of the GOT to the relocation value.  */
4063
0
      relocation = (off
4064
0
        + htab->etab.sgot->output_offset
4065
0
        + htab->etab.sgot->output_section->vma);
4066
4067
0
      break;
4068
0
    }
4069
4070
0
  case R_PARISC_TLS_LE21L:
4071
0
  case R_PARISC_TLS_LE14R:
4072
0
    {
4073
0
      relocation = tpoff (info, relocation);
4074
0
      break;
4075
0
    }
4076
0
    break;
4077
4078
0
  default:
4079
0
    break;
4080
0
  }
4081
4082
0
      rstatus = final_link_relocate (input_section, contents, rela, relocation,
4083
0
             htab, sym_sec, hh, info);
4084
4085
0
      if (rstatus == bfd_reloc_ok)
4086
0
  continue;
4087
4088
0
      if (hh != NULL)
4089
0
  sym_name = hh_name (hh);
4090
0
      else
4091
0
  {
4092
0
    sym_name = bfd_elf_string_from_elf_section (input_bfd,
4093
0
                  symtab_hdr->sh_link,
4094
0
                  sym->st_name);
4095
0
    if (sym_name == NULL)
4096
0
      return false;
4097
0
    if (*sym_name == '\0')
4098
0
      sym_name = bfd_section_name (sym_sec);
4099
0
  }
4100
4101
0
      howto = elf_hppa_howto_table + r_type;
4102
4103
0
      if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
4104
0
  {
4105
0
    if (rstatus == bfd_reloc_notsupported || !warned_undef)
4106
0
      {
4107
0
        _bfd_error_handler
4108
    /* xgettext:c-format */
4109
0
    (_("%pB(%pA+%#" PRIx64 "): cannot handle %s for %s"),
4110
0
     input_bfd,
4111
0
     input_section,
4112
0
     (uint64_t) rela->r_offset,
4113
0
     howto->name,
4114
0
     sym_name);
4115
0
        bfd_set_error (bfd_error_bad_value);
4116
0
        return false;
4117
0
      }
4118
0
  }
4119
0
      else
4120
0
  (*info->callbacks->reloc_overflow)
4121
0
    (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
4122
0
     (bfd_vma) 0, input_bfd, input_section, rela->r_offset);
4123
0
    }
4124
4125
0
  return true;
4126
0
}
4127
4128
/* Finish up dynamic symbol handling.  We set the contents of various
4129
   dynamic sections here.  */
4130
4131
static bool
4132
elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
4133
          struct bfd_link_info *info,
4134
          struct elf_link_hash_entry *eh,
4135
          Elf_Internal_Sym *sym)
4136
0
{
4137
0
  struct elf32_hppa_link_hash_table *htab;
4138
0
  Elf_Internal_Rela rela;
4139
0
  bfd_byte *loc;
4140
4141
0
  htab = hppa_link_hash_table (info);
4142
0
  if (htab == NULL)
4143
0
    return false;
4144
4145
0
  if (eh->plt.offset != (bfd_vma) -1)
4146
0
    {
4147
0
      bfd_vma value;
4148
4149
0
      if (eh->plt.offset & 1)
4150
0
  abort ();
4151
4152
      /* This symbol has an entry in the procedure linkage table.  Set
4153
   it up.
4154
4155
   The format of a plt entry is
4156
   <funcaddr>
4157
   <__gp>
4158
      */
4159
0
      value = 0;
4160
0
      if (eh->root.type == bfd_link_hash_defined
4161
0
    || eh->root.type == bfd_link_hash_defweak)
4162
0
  {
4163
0
    value = eh->root.u.def.value;
4164
0
    if (eh->root.u.def.section->output_section != NULL)
4165
0
      value += (eh->root.u.def.section->output_offset
4166
0
          + eh->root.u.def.section->output_section->vma);
4167
0
  }
4168
4169
      /* Create a dynamic IPLT relocation for this entry.  */
4170
0
      rela.r_offset = (eh->plt.offset
4171
0
          + htab->etab.splt->output_offset
4172
0
          + htab->etab.splt->output_section->vma);
4173
0
      if (eh->dynindx != -1)
4174
0
  {
4175
0
    rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
4176
0
    rela.r_addend = 0;
4177
0
  }
4178
0
      else
4179
0
  {
4180
    /* This symbol has been marked to become local, and is
4181
       used by a plabel so must be kept in the .plt.  */
4182
0
    rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4183
0
    rela.r_addend = value;
4184
0
  }
4185
4186
0
      loc = htab->etab.srelplt->contents;
4187
0
      loc += htab->etab.srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
4188
0
      bfd_elf32_swap_reloca_out (htab->etab.splt->output_section->owner, &rela, loc);
4189
4190
0
      if (!eh->def_regular)
4191
0
  {
4192
    /* Mark the symbol as undefined, rather than as defined in
4193
       the .plt section.  Leave the value alone.  */
4194
0
    sym->st_shndx = SHN_UNDEF;
4195
0
  }
4196
0
    }
4197
4198
0
  if (eh->got.offset != (bfd_vma) -1
4199
0
      && (hppa_elf_hash_entry (eh)->tls_type & GOT_NORMAL) != 0
4200
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
4201
0
    {
4202
0
      bool is_dyn = (eh->dynindx != -1
4203
0
         && !SYMBOL_REFERENCES_LOCAL (info, eh));
4204
4205
0
      if (is_dyn || bfd_link_pic (info))
4206
0
  {
4207
    /* This symbol has an entry in the global offset table.  Set
4208
       it up.  */
4209
4210
0
    rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
4211
0
         + htab->etab.sgot->output_offset
4212
0
         + htab->etab.sgot->output_section->vma);
4213
4214
    /* If this is a -Bsymbolic link and the symbol is defined
4215
       locally or was forced to be local because of a version
4216
       file, we just want to emit a RELATIVE reloc.  The entry
4217
       in the global offset table will already have been
4218
       initialized in the relocate_section function.  */
4219
0
    if (!is_dyn)
4220
0
      {
4221
0
        rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4222
0
        rela.r_addend = (eh->root.u.def.value
4223
0
             + eh->root.u.def.section->output_offset
4224
0
             + eh->root.u.def.section->output_section->vma);
4225
0
      }
4226
0
    else
4227
0
      {
4228
0
        if ((eh->got.offset & 1) != 0)
4229
0
    abort ();
4230
4231
0
        bfd_put_32 (output_bfd, 0,
4232
0
        htab->etab.sgot->contents + (eh->got.offset & ~1));
4233
0
        rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
4234
0
        rela.r_addend = 0;
4235
0
      }
4236
4237
0
    loc = htab->etab.srelgot->contents;
4238
0
    loc += (htab->etab.srelgot->reloc_count++
4239
0
      * sizeof (Elf32_External_Rela));
4240
0
    bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4241
0
  }
4242
0
    }
4243
4244
0
  if (eh->needs_copy)
4245
0
    {
4246
0
      asection *sec;
4247
4248
      /* This symbol needs a copy reloc.  Set it up.  */
4249
4250
0
      if (! (eh->dynindx != -1
4251
0
       && (eh->root.type == bfd_link_hash_defined
4252
0
     || eh->root.type == bfd_link_hash_defweak)))
4253
0
  abort ();
4254
4255
0
      rela.r_offset = (eh->root.u.def.value
4256
0
          + eh->root.u.def.section->output_offset
4257
0
          + eh->root.u.def.section->output_section->vma);
4258
0
      rela.r_addend = 0;
4259
0
      rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
4260
0
      if (eh->root.u.def.section == htab->etab.sdynrelro)
4261
0
  sec = htab->etab.sreldynrelro;
4262
0
      else
4263
0
  sec = htab->etab.srelbss;
4264
0
      loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
4265
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4266
0
    }
4267
4268
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4269
0
  if (eh == htab->etab.hdynamic || eh == htab->etab.hgot)
4270
0
    {
4271
0
      sym->st_shndx = SHN_ABS;
4272
0
    }
4273
4274
0
  return true;
4275
0
}
4276
4277
/* Used to decide how to sort relocs in an optimal manner for the
4278
   dynamic linker, before writing them out.  */
4279
4280
static enum elf_reloc_type_class
4281
elf32_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4282
           const asection *rel_sec ATTRIBUTE_UNUSED,
4283
           const Elf_Internal_Rela *rela)
4284
0
{
4285
  /* Handle TLS relocs first; we don't want them to be marked
4286
     relative by the "if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)"
4287
     check below.  */
4288
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
4289
0
    {
4290
0
      case R_PARISC_TLS_DTPMOD32:
4291
0
      case R_PARISC_TLS_DTPOFF32:
4292
0
      case R_PARISC_TLS_TPREL32:
4293
0
  return reloc_class_normal;
4294
0
    }
4295
4296
0
  if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)
4297
0
    return reloc_class_relative;
4298
4299
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
4300
0
    {
4301
0
    case R_PARISC_IPLT:
4302
0
      return reloc_class_plt;
4303
0
    case R_PARISC_COPY:
4304
0
      return reloc_class_copy;
4305
0
    default:
4306
0
      return reloc_class_normal;
4307
0
    }
4308
0
}
4309
4310
/* Finish up the dynamic sections.  */
4311
4312
static bool
4313
elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4314
            struct bfd_link_info *info)
4315
0
{
4316
0
  bfd *dynobj;
4317
0
  struct elf32_hppa_link_hash_table *htab;
4318
0
  asection *sdyn;
4319
0
  asection * sgot;
4320
4321
0
  htab = hppa_link_hash_table (info);
4322
0
  if (htab == NULL)
4323
0
    return false;
4324
4325
0
  dynobj = htab->etab.dynobj;
4326
4327
0
  sgot = htab->etab.sgot;
4328
  /* A broken linker script might have discarded the dynamic sections.
4329
     Catch this here so that we do not seg-fault later on.  */
4330
0
  if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
4331
0
    return false;
4332
4333
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4334
4335
0
  if (htab->etab.dynamic_sections_created)
4336
0
    {
4337
0
      Elf32_External_Dyn *dyncon, *dynconend;
4338
4339
0
      if (sdyn == NULL)
4340
0
  abort ();
4341
4342
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4343
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4344
0
      for (; dyncon < dynconend; dyncon++)
4345
0
  {
4346
0
    Elf_Internal_Dyn dyn;
4347
0
    asection *s;
4348
4349
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4350
4351
0
    switch (dyn.d_tag)
4352
0
      {
4353
0
      default:
4354
0
        continue;
4355
4356
0
      case DT_PLTGOT:
4357
        /* Use PLTGOT to set the GOT register.  */
4358
0
        dyn.d_un.d_ptr = elf_gp (output_bfd);
4359
0
        break;
4360
4361
0
      case DT_JMPREL:
4362
0
        s = htab->etab.srelplt;
4363
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4364
0
        break;
4365
4366
0
      case DT_PLTRELSZ:
4367
0
        s = htab->etab.srelplt;
4368
0
        dyn.d_un.d_val = s->size;
4369
0
        break;
4370
0
      }
4371
4372
0
    bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4373
0
  }
4374
0
    }
4375
4376
0
  if (sgot != NULL && sgot->size != 0)
4377
0
    {
4378
      /* Fill in the first entry in the global offset table.
4379
   We use it to point to our dynamic section, if we have one.  */
4380
0
      bfd_put_32 (output_bfd,
4381
0
      sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4382
0
      sgot->contents);
4383
4384
      /* The second entry is reserved for use by the dynamic linker.  */
4385
0
      memset (sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4386
4387
      /* Set .got entry size.  */
4388
0
      elf_section_data (sgot->output_section)
4389
0
  ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4390
0
    }
4391
4392
0
  if (htab->etab.splt != NULL && htab->etab.splt->size != 0)
4393
0
    {
4394
      /* Set plt entry size to 0 instead of PLT_ENTRY_SIZE, since we add the
4395
   plt stubs and as such the section does not hold a table of fixed-size
4396
   entries.  */
4397
0
      elf_section_data (htab->etab.splt->output_section)->this_hdr.sh_entsize = 0;
4398
4399
0
      if (htab->need_plt_stub)
4400
0
  {
4401
    /* Set up the .plt stub.  */
4402
0
    memcpy (htab->etab.splt->contents
4403
0
      + htab->etab.splt->size - sizeof (plt_stub),
4404
0
      plt_stub, sizeof (plt_stub));
4405
4406
0
    if ((htab->etab.splt->output_offset
4407
0
         + htab->etab.splt->output_section->vma
4408
0
         + htab->etab.splt->size)
4409
0
        != (sgot->output_offset
4410
0
      + sgot->output_section->vma))
4411
0
      {
4412
0
        _bfd_error_handler
4413
0
    (_(".got section not immediately after .plt section"));
4414
0
        return false;
4415
0
      }
4416
0
  }
4417
0
    }
4418
4419
0
  return true;
4420
0
}
4421
4422
/* Called when writing out an object file to decide the type of a
4423
   symbol.  */
4424
static int
4425
elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4426
0
{
4427
0
  if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4428
0
    return STT_PARISC_MILLI;
4429
0
  else
4430
0
    return type;
4431
0
}
4432
4433
/* Misc BFD support code.  */
4434
#define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
4435
#define bfd_elf32_bfd_reloc_type_lookup      elf_hppa_reloc_type_lookup
4436
#define bfd_elf32_bfd_reloc_name_lookup      elf_hppa_reloc_name_lookup
4437
#define elf_info_to_howto        elf_hppa_info_to_howto
4438
#define elf_info_to_howto_rel        elf_hppa_info_to_howto_rel
4439
4440
/* Stuff for the BFD linker.  */
4441
#define bfd_elf32_bfd_final_link       elf32_hppa_final_link
4442
#define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4443
#define elf_backend_adjust_dynamic_symbol    elf32_hppa_adjust_dynamic_symbol
4444
#define elf_backend_copy_indirect_symbol     elf32_hppa_copy_indirect_symbol
4445
#define elf_backend_check_relocs       elf32_hppa_check_relocs
4446
#define elf_backend_relocs_compatible      _bfd_elf_relocs_compatible
4447
#define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
4448
#define elf_backend_fake_sections      elf_hppa_fake_sections
4449
#define elf_backend_relocate_section       elf32_hppa_relocate_section
4450
#define elf_backend_hide_symbol        elf32_hppa_hide_symbol
4451
#define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
4452
#define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
4453
#define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
4454
#define elf_backend_init_index_section       _bfd_elf_init_1_index_section
4455
#define elf_backend_gc_mark_hook       elf32_hppa_gc_mark_hook
4456
#define elf_backend_grok_prstatus      elf32_hppa_grok_prstatus
4457
#define elf_backend_grok_psinfo        elf32_hppa_grok_psinfo
4458
#define elf_backend_object_p         elf32_hppa_object_p
4459
#define elf_backend_final_write_processing   elf_hppa_final_write_processing
4460
#define elf_backend_get_symbol_type      elf32_hppa_elf_get_symbol_type
4461
#define elf_backend_reloc_type_class       elf32_hppa_reloc_type_class
4462
#define elf_backend_action_discarded       elf_hppa_action_discarded
4463
4464
#define elf_backend_can_gc_sections      1
4465
#define elf_backend_can_refcount       1
4466
#define elf_backend_plt_alignment      2
4467
#define elf_backend_want_got_plt       0
4468
#define elf_backend_plt_readonly       0
4469
#define elf_backend_want_plt_sym       0
4470
#define elf_backend_got_header_size      8
4471
#define elf_backend_want_dynrelro      1
4472
#define elf_backend_rela_normal        1
4473
#define elf_backend_dtrel_excludes_plt       1
4474
#define elf_backend_no_page_alias      1
4475
4476
#define TARGET_BIG_SYM    hppa_elf32_vec
4477
#define TARGET_BIG_NAME   "elf32-hppa"
4478
#define ELF_ARCH    bfd_arch_hppa
4479
#define ELF_TARGET_ID   HPPA32_ELF_DATA
4480
#define ELF_MACHINE_CODE  EM_PARISC
4481
#define ELF_MAXPAGESIZE   0x1000
4482
#define ELF_OSABI   ELFOSABI_HPUX
4483
#define elf32_bed   elf32_hppa_hpux_bed
4484
4485
#include "elf32-target.h"
4486
4487
#undef TARGET_BIG_SYM
4488
#define TARGET_BIG_SYM    hppa_elf32_linux_vec
4489
#undef TARGET_BIG_NAME
4490
#define TARGET_BIG_NAME   "elf32-hppa-linux"
4491
#undef ELF_OSABI
4492
#define ELF_OSABI   ELFOSABI_GNU
4493
#undef elf32_bed
4494
#define elf32_bed   elf32_hppa_linux_bed
4495
4496
#include "elf32-target.h"
4497
4498
#undef TARGET_BIG_SYM
4499
#define TARGET_BIG_SYM    hppa_elf32_nbsd_vec
4500
#undef TARGET_BIG_NAME
4501
#define TARGET_BIG_NAME   "elf32-hppa-netbsd"
4502
#undef ELF_OSABI
4503
#define ELF_OSABI   ELFOSABI_NETBSD
4504
#undef elf32_bed
4505
#define elf32_bed   elf32_hppa_netbsd_bed
4506
4507
#include "elf32-target.h"