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