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