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