Coverage Report

Created: 2026-11-21 09:00

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