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