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