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